maple.c revision 1.44.12.2 1 /* $NetBSD: maple.c,v 1.44.12.2 2014/08/20 00:02:51 tls Exp $ */
2
3 /*-
4 * Copyright (c) 2002 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by ITOH Yasufumi.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
30 */
31
32 /*-
33 * Copyright (c) 2001 Marcus Comstedt
34 * All rights reserved.
35 *
36 * Redistribution and use in source and binary forms, with or without
37 * modification, are permitted provided that the following conditions
38 * are met:
39 * 1. Redistributions of source code must retain the above copyright
40 * notice, this list of conditions and the following disclaimer.
41 * 2. Redistributions in binary form must reproduce the above copyright
42 * notice, this list of conditions and the following disclaimer in the
43 * documentation and/or other materials provided with the distribution.
44 * 3. All advertising materials mentioning features or use of this software
45 * must display the following acknowledgement:
46 * This product includes software developed by Marcus Comstedt.
47 * 4. Neither the name of The NetBSD Foundation nor the names of its
48 * contributors may be used to endorse or promote products derived
49 * from this software without specific prior written permission.
50 *
51 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
52 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
53 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
54 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
55 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
56 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
57 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
58 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
59 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
60 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
61 * POSSIBILITY OF SUCH DAMAGE.
62 */
63
64 #include <sys/cdefs.h>
65 __KERNEL_RCSID(0, "$NetBSD: maple.c,v 1.44.12.2 2014/08/20 00:02:51 tls Exp $");
66
67 #include <sys/param.h>
68 #include <sys/device.h>
69 #include <sys/fcntl.h>
70 #include <sys/kernel.h>
71 #include <sys/kthread.h>
72 #include <sys/poll.h>
73 #include <sys/select.h>
74 #include <sys/proc.h>
75 #include <sys/signalvar.h>
76 #include <sys/systm.h>
77 #include <sys/conf.h>
78 #include <sys/bus.h>
79
80 #include <uvm/uvm.h>
81
82 #include <machine/cpu.h>
83 #include <machine/sysasicvar.h>
84 #include <sh3/pmap.h>
85
86 #include <dreamcast/dev/maple/maple.h>
87 #include <dreamcast/dev/maple/mapleconf.h>
88 #include <dreamcast/dev/maple/maplevar.h>
89 #include <dreamcast/dev/maple/maplereg.h>
90 #include <dreamcast/dev/maple/mapleio.h>
91
92 #include "ioconf.h"
93 #include "locators.h"
94
95 /* Internal macros, functions, and variables. */
96
97 #define MAPLE_CALLOUT_TICKS 2
98
99 #define MAPLEBUSUNIT(dev) (minor(dev)>>5)
100 #define MAPLEPORT(dev) ((minor(dev) & 0x18) >> 3)
101 #define MAPLESUBUNIT(dev) (minor(dev) & 0x7)
102
103 /* interrupt priority level */
104 #define IPL_MAPLE IPL_BIO
105 #define splmaple() splbio()
106 #define IRL_MAPLE SYSASIC_IRL9
107
108 /*
109 * Function declarations.
110 */
111 static int maplematch(device_t, cfdata_t, void *);
112 static void mapleattach(device_t, device_t, void *);
113 static void maple_scanbus(struct maple_softc *);
114 static char * maple_unit_name(char *, size_t, int port, int subunit);
115 static void maple_begin_txbuf(struct maple_softc *);
116 static int maple_end_txbuf(struct maple_softc *);
117 static void maple_queue_command(struct maple_softc *, struct maple_unit *,
118 int command, int datalen, const void *dataaddr);
119 static void maple_write_command(struct maple_softc *, struct maple_unit *,
120 int, int, const void *);
121 static void maple_start(struct maple_softc *sc);
122 static void maple_start_poll(struct maple_softc *);
123 static void maple_check_subunit_change(struct maple_softc *,
124 struct maple_unit *);
125 static void maple_check_unit_change(struct maple_softc *,
126 struct maple_unit *);
127 static void maple_print_unit(void *, const char *);
128 static int maplesubmatch(device_t, cfdata_t, const int *, void *);
129 static int mapleprint(void *, const char *);
130 static void maple_attach_unit(struct maple_softc *, struct maple_unit *);
131 static void maple_detach_unit_nofix(struct maple_softc *,
132 struct maple_unit *);
133 static void maple_detach_unit(struct maple_softc *, struct maple_unit *);
134 static void maple_queue_cmds(struct maple_softc *,
135 struct maple_cmdq_head *);
136 static void maple_unit_probe(struct maple_softc *);
137 static void maple_unit_ping(struct maple_softc *);
138 static int maple_send_defered_periodic(struct maple_softc *);
139 static void maple_send_periodic(struct maple_softc *);
140 static void maple_remove_from_queues(struct maple_softc *,
141 struct maple_unit *);
142 static int maple_retry(struct maple_softc *, struct maple_unit *,
143 enum maple_dma_stat);
144 static void maple_queue_retry(struct maple_softc *);
145 static void maple_check_responses(struct maple_softc *);
146 static void maple_event_thread(void *);
147 static int maple_intr(void *);
148 static void maple_callout(void *);
149
150 int maple_alloc_dma(size_t, vaddr_t *, paddr_t *);
151 #if 0
152 void maple_free_dma(paddr_t, size_t);
153 #endif
154
155 /*
156 * Global variables.
157 */
158 int maple_polling; /* Are we polling? (Debugger mode) */
159
160 CFATTACH_DECL_NEW(maple, sizeof(struct maple_softc),
161 maplematch, mapleattach, NULL, NULL);
162
163 dev_type_open(mapleopen);
164 dev_type_close(mapleclose);
165 dev_type_ioctl(mapleioctl);
166
167 const struct cdevsw maple_cdevsw = {
168 .d_open = mapleopen,
169 .d_close = mapleclose,
170 .d_read = noread,
171 .d_write = nowrite,
172 .d_ioctl = mapleioctl,
173 .d_stop = nostop,
174 .d_tty = notty,
175 .d_poll = nopoll,
176 .d_mmap = nommap,
177 .d_kqfilter = nokqfilter,
178 .d_discard = nodiscard,
179 .d_flag = 0
180 };
181
182 static int
183 maplematch(device_t parent, cfdata_t cf, void *aux)
184 {
185
186 return 1;
187 }
188
189 static void
190 mapleattach(device_t parent, device_t self, void *aux)
191 {
192 struct maple_softc *sc;
193 struct maple_unit *u;
194 vaddr_t dmabuffer;
195 paddr_t dmabuffer_phys;
196 uint32_t *p;
197 int port, subunit, f;
198
199 sc = device_private(self);
200 sc->sc_dev = self;
201
202 printf(": %s\n", sysasic_intr_string(IRL_MAPLE));
203
204 if (maple_alloc_dma(MAPLE_DMABUF_SIZE, &dmabuffer, &dmabuffer_phys)) {
205 printf("%s: unable to allocate DMA buffers.\n",
206 device_xname(self));
207 return;
208 }
209
210 p = (uint32_t *)dmabuffer;
211
212 for (port = 0; port < MAPLE_PORTS; port++) {
213 for (subunit = 0; subunit < MAPLE_SUBUNITS; subunit++) {
214 u = &sc->sc_unit[port][subunit];
215 u->port = port;
216 u->subunit = subunit;
217 u->u_dma_stat = MAPLE_DMA_IDLE;
218 u->u_rxbuf = p;
219 u->u_rxbuf_phys = SH3_P2SEG_TO_PHYS(p);
220 p += 256;
221
222 for (f = 0; f < MAPLE_NFUNC; f++) {
223 u->u_func[f].f_funcno = f;
224 u->u_func[f].f_unit = u;
225 }
226 }
227 }
228
229 sc->sc_txbuf = p;
230 sc->sc_txbuf_phys = SH3_P2SEG_TO_PHYS(p);
231
232 SIMPLEQ_INIT(&sc->sc_retryq);
233 TAILQ_INIT(&sc->sc_probeq);
234 TAILQ_INIT(&sc->sc_pingq);
235 TAILQ_INIT(&sc->sc_periodicq);
236 TAILQ_INIT(&sc->sc_periodicdeferq);
237 TAILQ_INIT(&sc->sc_acmdq);
238 TAILQ_INIT(&sc->sc_pcmdq);
239
240 MAPLE_RESET = RESET_MAGIC;
241 MAPLE_RESET2 = 0;
242
243 MAPLE_SPEED = SPEED_2MBPS | TIMEOUT(50000);
244
245 MAPLE_ENABLE = 1;
246
247 maple_polling = 1;
248 maple_scanbus(sc);
249
250 callout_init(&sc->maple_callout_ch, 0);
251
252 sc->sc_intrhand = sysasic_intr_establish(SYSASIC_EVENT_MAPLE_DMADONE,
253 IPL_MAPLE, IRL_MAPLE, maple_intr, sc);
254
255 config_pending_incr(self); /* create thread before mounting root */
256
257 if (kthread_create(PRI_NONE, 0, NULL, maple_event_thread, sc,
258 &sc->event_thread, "%s", device_xname(self)) == 0)
259 return;
260
261 panic("%s: unable to create event thread", device_xname(self));
262 }
263
264 /*
265 * initial device attach
266 */
267 static void
268 maple_scanbus(struct maple_softc *sc)
269 {
270 struct maple_unit *u;
271 int port;
272 int last_port, last_subunit;
273 int i;
274
275 KASSERT(cold && maple_polling);
276
277 /* probe all ports */
278 for (port = 0; port < MAPLE_PORTS; port++) {
279 u = &sc->sc_unit[port][0];
280 #if defined(MAPLE_DEBUG) && MAPLE_DEBUG > 2
281 {
282 char buf[16];
283 printf("%s: queued to probe 1\n",
284 maple_unit_name(buf, sizeof(buf), u->port, u->subunit));
285 }
286 #endif
287 TAILQ_INSERT_TAIL(&sc->sc_probeq, u, u_q);
288 u->u_queuestat = MAPLE_QUEUE_PROBE;
289 }
290
291 last_port = last_subunit = -1;
292 maple_begin_txbuf(sc);
293 while ((u = TAILQ_FIRST(&sc->sc_probeq)) != NULL) {
294 /*
295 * Check wrap condition
296 */
297 if (u->port < last_port || u->subunit <= last_subunit)
298 break;
299 last_port = u->port;
300 if (u->port == MAPLE_PORTS - 1)
301 last_subunit = u->subunit;
302
303 maple_unit_probe(sc);
304 for (i = 10 /* just not forever */; maple_end_txbuf(sc); i--) {
305 maple_start_poll(sc);
306 maple_check_responses(sc);
307 if (i == 0)
308 break;
309 /* attach may issue cmds */
310 maple_queue_cmds(sc, &sc->sc_acmdq);
311 }
312 }
313 }
314
315 void
316 maple_run_polling(device_t dev)
317 {
318 struct maple_softc *sc;
319 int port, subunit;
320 int i;
321
322 sc = device_private(dev);
323
324 /*
325 * first, make sure polling works
326 */
327 while (MAPLE_STATE != 0) /* XXX may lost a DMA cycle */
328 ;
329
330 /* XXX this will break internal state */
331 for (port = 0; port < MAPLE_PORTS; port++)
332 for (subunit = 0; subunit < MAPLE_SUBUNITS; subunit++)
333 sc->sc_unit[port][subunit].u_dma_stat = MAPLE_DMA_IDLE;
334 SIMPLEQ_INIT(&sc->sc_retryq); /* XXX discard current retrys */
335
336 /*
337 * do polling (periodic status check only)
338 */
339 maple_begin_txbuf(sc);
340 maple_send_defered_periodic(sc);
341 maple_send_periodic(sc);
342 for (i = 10 /* just not forever */; maple_end_txbuf(sc); i--) {
343 maple_start_poll(sc);
344 maple_check_responses(sc);
345 if (i == 0)
346 break;
347
348 /* maple_check_responses() has executed maple_begin_txbuf() */
349 maple_queue_retry(sc);
350 maple_send_defered_periodic(sc);
351 }
352 }
353
354 static char *
355 maple_unit_name(char *buf, size_t len, int port, int subunit)
356 {
357 size_t l = snprintf(buf, len, "maple%c", port + 'A');
358 if (l > len)
359 l = len;
360 if (subunit)
361 snprintf(buf + l, len - l, "%d", subunit);
362
363 return buf;
364 }
365
366 int
367 maple_alloc_dma(size_t size, vaddr_t *vap, paddr_t *pap)
368 {
369 extern paddr_t avail_start, avail_end; /* from pmap.c */
370 struct pglist mlist;
371 struct vm_page *m;
372 int error;
373
374 size = round_page(size);
375
376 error = uvm_pglistalloc(size, avail_start, avail_end - PAGE_SIZE,
377 0, 0, &mlist, 1, 0);
378 if (error)
379 return error;
380
381 m = TAILQ_FIRST(&mlist);
382 *pap = VM_PAGE_TO_PHYS(m);
383 *vap = SH3_PHYS_TO_P2SEG(VM_PAGE_TO_PHYS(m));
384
385 return 0;
386 }
387
388 #if 0 /* currently unused */
389 void
390 maple_free_dma(paddr_t paddr, size_t size)
391 {
392 struct pglist mlist;
393 struct vm_page *m;
394 bus_addr_t addr;
395
396 TAILQ_INIT(&mlist);
397 for (addr = paddr; addr < paddr + size; addr += PAGE_SIZE) {
398 m = PHYS_TO_VM_PAGE(addr);
399 TAILQ_INSERT_TAIL(&mlist, m, pageq.queue);
400 }
401 uvm_pglistfree(&mlist);
402 }
403 #endif
404
405 static void
406 maple_begin_txbuf(struct maple_softc *sc)
407 {
408
409 sc->sc_txlink = sc->sc_txpos = sc->sc_txbuf;
410 SIMPLEQ_INIT(&sc->sc_dmaq);
411 }
412
413 static int
414 maple_end_txbuf(struct maple_softc *sc)
415 {
416
417 /* if no frame have been written, we can't mark the
418 list end, and so the DMA must not be activated */
419 if (sc->sc_txpos == sc->sc_txbuf)
420 return 0;
421
422 *sc->sc_txlink |= 0x80000000;
423
424 return 1;
425 }
426
427 static const int8_t subunit_code[] = { 0x20, 0x01, 0x02, 0x04, 0x08, 0x10 };
428
429 static void
430 maple_queue_command(struct maple_softc *sc, struct maple_unit *u,
431 int command, int datalen, const void *dataaddr)
432 {
433 int to, from;
434 uint32_t *p = sc->sc_txpos;
435
436 /* Max data length = 255 longs = 1020 bytes */
437 KASSERT(datalen >= 0 && datalen <= 255);
438
439 /* Compute sender and recipient address */
440 from = u->port << 6;
441 to = from | subunit_code[u->subunit];
442
443 sc->sc_txlink = p;
444
445 /* Set length of packet and destination port (A-D) */
446 *p++ = datalen | (u->port << 16);
447
448 /* Write address to receive buffer where the response
449 frame should be put */
450 *p++ = u->u_rxbuf_phys;
451
452 /* Create the frame header. The fields are assembled "backwards"
453 because of the Maple Bus big-endianness. */
454 *p++ = (command & 0xff) | (to << 8) | (from << 16) | (datalen << 24);
455
456 /* Copy parameter data, if any */
457 if (datalen > 0) {
458 const uint32_t *param = dataaddr;
459 int i;
460 for (i = 0; i < datalen; i++)
461 *p++ = *param++;
462 }
463
464 sc->sc_txpos = p;
465
466 SIMPLEQ_INSERT_TAIL(&sc->sc_dmaq, u, u_dmaq);
467 }
468
469 static void
470 maple_write_command(struct maple_softc *sc, struct maple_unit *u, int command,
471 int datalen, const void *dataaddr)
472 {
473 #if defined(MAPLE_DEBUG) && MAPLE_DEBUG > 2
474 char buf[16];
475
476 if (u->u_retrycnt)
477 printf("%s: retrycnt %d\n", maple_unit_name(buf, sizeof(buf),
478 u->port, u->subunit), u->u_retrycnt);
479 #endif
480 u->u_retrycnt = 0;
481 u->u_command = command;
482 u->u_datalen = datalen;
483 u->u_dataaddr = dataaddr;
484
485 maple_queue_command(sc, u, command, datalen, dataaddr);
486 }
487
488 /* start DMA */
489 static void
490 maple_start(struct maple_softc *sc)
491 {
492
493 MAPLE_DMAADDR = sc->sc_txbuf_phys;
494 MAPLE_STATE = 1;
495 }
496
497 /* start DMA -- wait until DMA done */
498 static void
499 maple_start_poll(struct maple_softc *sc)
500 {
501
502 MAPLE_DMAADDR = sc->sc_txbuf_phys;
503 MAPLE_STATE = 1;
504 while (MAPLE_STATE != 0)
505 ;
506 }
507
508 static void
509 maple_check_subunit_change(struct maple_softc *sc, struct maple_unit *u)
510 {
511 struct maple_unit *u1;
512 int port;
513 int8_t unit_map;
514 int units, un;
515 int i;
516
517 KASSERT(u->subunit == 0);
518
519 port = u->port;
520 unit_map = ((int8_t *) u->u_rxbuf)[2];
521 if (sc->sc_port_unit_map[port] == unit_map)
522 return;
523
524 units = ((unit_map & 0x1f) << 1) | 1;
525 #if defined(MAPLE_DEBUG) && MAPLE_DEBUG > 1
526 {
527 char buf[16];
528 printf("%s: unit_map 0x%x -> 0x%x (units 0x%x)\n",
529 maple_unit_name(buf, sizeof(buf), u->port, u->subunit),
530 sc->sc_port_unit_map[port], unit_map, units);
531 }
532 #endif
533 #if 0 /* this detects unit removal rapidly but is not reliable */
534 /* check for unit change */
535 un = sc->sc_port_units[port] & ~units;
536
537 /* detach removed devices */
538 for (i = MAPLE_SUBUNITS - 1; i > 0; i--)
539 if (un & (1 << i))
540 maple_detach_unit_nofix(sc, &sc->sc_unit[port][i]);
541 #endif
542
543 sc->sc_port_unit_map[port] = unit_map;
544
545 /* schedule scanning child devices */
546 un = units & ~sc->sc_port_units[port];
547 for (i = MAPLE_SUBUNITS - 1; i > 0; i--)
548 if (un & (1 << i)) {
549 u1 = &sc->sc_unit[port][i];
550 maple_remove_from_queues(sc, u1);
551 #if defined(MAPLE_DEBUG) && MAPLE_DEBUG > 2
552 {
553 char buf[16];
554 printf("%s: queued to probe 2\n",
555 maple_unit_name(buf, sizeof(buf),
556 u1->port, u1->subunit));
557 }
558 #endif
559 TAILQ_INSERT_HEAD(&sc->sc_probeq, u1, u_q);
560 u1->u_queuestat = MAPLE_QUEUE_PROBE;
561 u1->u_proberetry = 0;
562 }
563 }
564
565 static void
566 maple_check_unit_change(struct maple_softc *sc, struct maple_unit *u)
567 {
568 struct maple_devinfo *newinfo = (void *) (u->u_rxbuf + 1);
569
570 if (memcmp(&u->devinfo, newinfo, sizeof(struct maple_devinfo)) == 0)
571 goto out; /* no change */
572
573 /* unit inserted */
574
575 /* attach this device */
576 u->devinfo = *newinfo;
577 maple_attach_unit(sc, u);
578
579 out:
580 maple_remove_from_queues(sc, u);
581 #if defined(MAPLE_DEBUG) && MAPLE_DEBUG > 2
582 {
583 char buf[16];
584 printf("%s: queued to ping\n",
585 maple_unit_name(buf, sizeof(buf), u->port, u->subunit));
586 }
587 #endif
588 TAILQ_INSERT_TAIL(&sc->sc_pingq, u, u_q);
589 u->u_queuestat = MAPLE_QUEUE_PING;
590 }
591
592 static void
593 maple_print_unit(void *aux, const char *pnp)
594 {
595 struct maple_attach_args *ma = aux;
596 int port, subunit;
597 char buf[16];
598 char *prod, *p, oc;
599
600 port = ma->ma_unit->port;
601 subunit = ma->ma_unit->subunit;
602
603 if (pnp != NULL)
604 printf("%s at %s", maple_unit_name(buf, sizeof(buf), port,
605 subunit), pnp);
606
607 printf(" port %d", port);
608
609 if (subunit != 0)
610 printf(" subunit %d", subunit);
611
612 #ifdef MAPLE_DEBUG
613 printf(": a %#x c %#x fn %#x d %#x,%#x,%#x",
614 ma->ma_devinfo->di_area_code,
615 ma->ma_devinfo->di_connector_direction,
616 be32toh(ma->ma_devinfo->di_func),
617 be32toh(ma->ma_devinfo->di_function_data[0]),
618 be32toh(ma->ma_devinfo->di_function_data[1]),
619 be32toh(ma->ma_devinfo->di_function_data[2]));
620 #endif
621
622 /* nul termination */
623 prod = ma->ma_devinfo->di_product_name;
624 for (p = prod + sizeof ma->ma_devinfo->di_product_name; p >= prod; p--)
625 if (p[-1] != '\0' && p[-1] != ' ')
626 break;
627 oc = *p;
628 *p = '\0';
629
630 printf(": %s", prod);
631
632 *p = oc; /* restore */
633 }
634
635 static int
636 maplesubmatch(device_t parent, cfdata_t match, const int *ldesc, void *aux)
637 {
638 struct maple_attach_args *ma = aux;
639
640 if (match->cf_loc[MAPLECF_PORT] != MAPLECF_PORT_DEFAULT &&
641 match->cf_loc[MAPLECF_PORT] != ma->ma_unit->port)
642 return 0;
643
644 if (match->cf_loc[MAPLECF_SUBUNIT] != MAPLECF_SUBUNIT_DEFAULT &&
645 match->cf_loc[MAPLECF_SUBUNIT] != ma->ma_unit->subunit)
646 return 0;
647
648 return config_match(parent, match, aux);
649 }
650
651 static int
652 mapleprint(void *aux, const char *str)
653 {
654 struct maple_attach_args *ma = aux;
655
656 #ifdef MAPLE_DEBUG
657 if (str)
658 aprint_normal("%s", str);
659 aprint_normal(" function %d", ma->ma_function);
660
661 return UNCONF;
662 #else /* quiet */
663 if (!str)
664 aprint_normal(" function %d", ma->ma_function);
665
666 return QUIET;
667 #endif
668 }
669
670 static void
671 maple_attach_unit(struct maple_softc *sc, struct maple_unit *u)
672 {
673 struct maple_attach_args ma;
674 uint32_t func;
675 int f;
676 char oldxname[16];
677
678 ma.ma_unit = u;
679 ma.ma_devinfo = &u->devinfo;
680 ma.ma_basedevinfo = &sc->sc_unit[u->port][0].devinfo;
681 func = be32toh(ma.ma_devinfo->di_func);
682
683 maple_print_unit(&ma, device_xname(sc->sc_dev));
684 printf("\n");
685 strcpy(oldxname, device_xname(sc->sc_dev));
686 maple_unit_name(sc->sc_dev->dv_xname, sizeof(sc->sc_dev->dv_xname),
687 u->port, u->subunit);
688
689 for (f = 0; f < MAPLE_NFUNC; f++) {
690 u->u_func[f].f_callback = NULL;
691 u->u_func[f].f_arg = NULL;
692 u->u_func[f].f_cmdstat = MAPLE_CMDSTAT_NONE;
693 u->u_func[f].f_dev = NULL;
694 if (func & MAPLE_FUNC(f)) {
695 ma.ma_function = f;
696 u->u_func[f].f_dev = config_found_sm_loc(sc->sc_dev,
697 "maple", NULL, &ma, mapleprint, maplesubmatch);
698 u->u_ping_func = f; /* XXX using largest func */
699 }
700 }
701 #ifdef MAPLE_MEMCARD_PING_HACK
702 /*
703 * Some 3rd party memory card pretend to be Visual Memory,
704 * but need special handling for ping.
705 */
706 if (func == (MAPLE_FUNC(MAPLE_FN_MEMCARD) | MAPLE_FUNC(MAPLE_FN_LCD) |
707 MAPLE_FUNC(MAPLE_FN_CLOCK))) {
708 u->u_ping_func = MAPLE_FN_MEMCARD;
709 u->u_ping_stat = MAPLE_PING_MEMCARD;
710 } else {
711 u->u_ping_stat = MAPLE_PING_NORMAL;
712 }
713 #endif
714 strcpy(sc->sc_dev->dv_xname, oldxname);
715
716 sc->sc_port_units[u->port] |= 1 << u->subunit;
717 }
718
719 static void
720 maple_detach_unit_nofix(struct maple_softc *sc, struct maple_unit *u)
721 {
722 struct maple_func *fn;
723 device_t dev;
724 struct maple_unit *u1;
725 int port;
726 int error;
727 int i;
728 char buf[16];
729
730 #if defined(MAPLE_DEBUG) && MAPLE_DEBUG > 1
731 printf("%s: remove\n", maple_unit_name(buf, sizeof(buf), u->port, u->subunit));
732 #endif
733 maple_remove_from_queues(sc, u);
734 port = u->port;
735 sc->sc_port_units[port] &= ~(1 << u->subunit);
736
737 if (u->subunit == 0) {
738 for (i = MAPLE_SUBUNITS - 1; i > 0; i--)
739 maple_detach_unit_nofix(sc, &sc->sc_unit[port][i]);
740 }
741
742 for (fn = u->u_func; fn < &u->u_func[MAPLE_NFUNC]; fn++) {
743 if ((dev = fn->f_dev) != NULL) {
744 #if defined(MAPLE_DEBUG) && MAPLE_DEBUG > 1
745 printf("%s: detaching func %d\n",
746 maple_unit_name(buf, sizeof(buf), port, u->subunit),
747 fn->f_funcno);
748 #endif
749
750 /*
751 * Remove functions from command queue.
752 */
753 switch (fn->f_cmdstat) {
754 case MAPLE_CMDSTAT_ASYNC:
755 case MAPLE_CMDSTAT_PERIODIC_DEFERED:
756 TAILQ_REMOVE(&sc->sc_acmdq, fn, f_cmdq);
757 break;
758 case MAPLE_CMDSTAT_ASYNC_PERIODICQ:
759 case MAPLE_CMDSTAT_PERIODIC:
760 TAILQ_REMOVE(&sc->sc_pcmdq, fn, f_cmdq);
761 break;
762 default:
763 break;
764 }
765
766 /*
767 * Detach devices.
768 */
769 if ((error = config_detach(fn->f_dev, DETACH_FORCE))) {
770 printf("%s: failed to detach %s (func %d), errno %d\n",
771 maple_unit_name(buf, sizeof(buf), port, u->subunit),
772 device_xname(fn->f_dev), fn->f_funcno, error);
773 }
774 }
775
776 maple_enable_periodic(sc->sc_dev, u, fn->f_funcno, 0);
777
778 fn->f_dev = NULL;
779 fn->f_callback = NULL;
780 fn->f_arg = NULL;
781 fn->f_cmdstat = MAPLE_CMDSTAT_NONE;
782 }
783 if (u->u_dma_stat == MAPLE_DMA_RETRY) {
784 /* XXX expensive? */
785 SIMPLEQ_FOREACH(u1, &sc->sc_retryq, u_dmaq) {
786 if (u1 == u) {
787 #if defined(MAPLE_DEBUG) && MAPLE_DEBUG > 1
788 printf("%s: abort retry\n",
789 maple_unit_name(buf, sizeof(buf), port, u->subunit));
790 #endif
791 SIMPLEQ_REMOVE(&sc->sc_retryq, u, maple_unit,
792 u_dmaq);
793 break;
794 }
795 }
796 }
797 u->u_dma_stat = MAPLE_DMA_IDLE;
798 u->u_noping = 0;
799 /* u->u_dma_func = uninitialized; */
800 KASSERT(u->getcond_func_set == 0);
801 memset(&u->devinfo, 0, sizeof(struct maple_devinfo));
802
803 if (u->subunit == 0) {
804 sc->sc_port_unit_map[port] = 0;
805 #if defined(MAPLE_DEBUG) && MAPLE_DEBUG > 2
806 {
807 char buf2[16];
808 printf("%s: queued to probe 3\n",
809 maple_unit_name(buf2, sizeof(buf2), port, u->subunit));
810 }
811 #endif
812 TAILQ_INSERT_TAIL(&sc->sc_probeq, u, u_q);
813 u->u_queuestat = MAPLE_QUEUE_PROBE;
814 }
815 }
816
817 static void
818 maple_detach_unit(struct maple_softc *sc, struct maple_unit *u)
819 {
820
821 maple_detach_unit_nofix(sc, u);
822 if (u->subunit != 0)
823 sc->sc_port_unit_map[u->port] &= ~(1 << (u->subunit - 1));
824 }
825
826 /*
827 * Send a command (called by drivers)
828 *
829 * The "cataaddr" must not point at temporary storage like stack.
830 * Only one command (per function) is valid at a time.
831 */
832 void
833 maple_command(device_t dev, struct maple_unit *u, int func,
834 int command, int datalen, const void *dataaddr, int flags)
835 {
836 struct maple_softc *sc = device_private(dev);
837 struct maple_func *fn;
838 int s;
839
840 KASSERT(func >= 0 && func < 32);
841 KASSERT(command);
842 KASSERT((flags & ~MAPLE_FLAG_CMD_PERIODIC_TIMING) == 0);
843
844 s = splsoftclock();
845
846 fn = &u->u_func[func];
847 #if 1 /*def DIAGNOSTIC*/
848 {char buf[16];
849 if (fn->f_cmdstat != MAPLE_CMDSTAT_NONE)
850 panic("maple_command: %s func %d: requesting more than one commands",
851 maple_unit_name(buf, sizeof(buf), u->port, u->subunit), func);
852 }
853 #endif
854 fn->f_command = command;
855 fn->f_datalen = datalen;
856 fn->f_dataaddr = dataaddr;
857 if (flags & MAPLE_FLAG_CMD_PERIODIC_TIMING) {
858 fn->f_cmdstat = MAPLE_CMDSTAT_PERIODIC;
859 TAILQ_INSERT_TAIL(&sc->sc_pcmdq, fn, f_cmdq);
860 } else {
861 fn->f_cmdstat = MAPLE_CMDSTAT_ASYNC;
862 TAILQ_INSERT_TAIL(&sc->sc_acmdq, fn, f_cmdq);
863 wakeup(&sc->sc_event); /* wake for async event */
864 }
865 splx(s);
866 }
867
868 static void
869 maple_queue_cmds(struct maple_softc *sc,
870 struct maple_cmdq_head *head)
871 {
872 struct maple_func *fn, *nextfn;
873 struct maple_unit *u;
874
875 /*
876 * Note: since the queue element may be queued immediately,
877 * we can't use TAILQ_FOREACH.
878 */
879 fn = TAILQ_FIRST(head);
880 TAILQ_INIT(head);
881 for ( ; fn; fn = nextfn) {
882 nextfn = TAILQ_NEXT(fn, f_cmdq);
883
884 KASSERT(fn->f_cmdstat != MAPLE_CMDSTAT_NONE);
885 u = fn->f_unit;
886 if (u->u_dma_stat == MAPLE_DMA_IDLE) {
887 maple_write_command(sc, u,
888 fn->f_command, fn->f_datalen, fn->f_dataaddr);
889 u->u_dma_stat = (fn->f_cmdstat == MAPLE_CMDSTAT_ASYNC ||
890 fn->f_cmdstat == MAPLE_CMDSTAT_ASYNC_PERIODICQ) ?
891 MAPLE_DMA_ACMD : MAPLE_DMA_PCMD;
892 u->u_dma_func = fn->f_funcno;
893 fn->f_cmdstat = MAPLE_CMDSTAT_NONE;
894 } else if (u->u_dma_stat == MAPLE_DMA_RETRY) {
895 /* unit is busy --- try again */
896 /*
897 * always add to periodic command queue
898 * (wait until the next periodic timing),
899 * since the unit will never be freed until the
900 * next periodic timing.
901 */
902 switch (fn->f_cmdstat) {
903 case MAPLE_CMDSTAT_ASYNC:
904 fn->f_cmdstat = MAPLE_CMDSTAT_ASYNC_PERIODICQ;
905 break;
906 case MAPLE_CMDSTAT_PERIODIC_DEFERED:
907 fn->f_cmdstat = MAPLE_CMDSTAT_PERIODIC;
908 break;
909 default:
910 break;
911 }
912 TAILQ_INSERT_TAIL(&sc->sc_pcmdq, fn, f_cmdq);
913 } else {
914 /* unit is busy --- try again */
915 /*
916 * always add to async command queue
917 * (process immediately)
918 */
919 switch (fn->f_cmdstat) {
920 case MAPLE_CMDSTAT_ASYNC_PERIODICQ:
921 fn->f_cmdstat = MAPLE_CMDSTAT_ASYNC;
922 break;
923 case MAPLE_CMDSTAT_PERIODIC:
924 fn->f_cmdstat = MAPLE_CMDSTAT_PERIODIC_DEFERED;
925 break;
926 default:
927 break;
928 }
929 TAILQ_INSERT_TAIL(&sc->sc_acmdq, fn, f_cmdq);
930 }
931 }
932 }
933
934 /* schedule probing a device */
935 static void
936 maple_unit_probe(struct maple_softc *sc)
937 {
938 struct maple_unit *u;
939
940 if ((u = TAILQ_FIRST(&sc->sc_probeq)) != NULL) {
941 KASSERT(u->u_dma_stat == MAPLE_DMA_IDLE);
942 KASSERT(u->u_queuestat == MAPLE_QUEUE_PROBE);
943 maple_remove_from_queues(sc, u);
944 maple_write_command(sc, u, MAPLE_COMMAND_DEVINFO, 0, NULL);
945 u->u_dma_stat = MAPLE_DMA_PROBE;
946 /* u->u_dma_func = ignored; */
947 }
948 }
949
950 /*
951 * Enable/disable unit pinging (called by drivers)
952 */
953 /* ARGSUSED */
954 void
955 maple_enable_unit_ping(device_t dev, struct maple_unit *u, int func, int enable)
956 {
957 #if 0 /* currently unused */
958 struct maple_softc *sc = device_private(dev);
959 #endif
960
961 if (enable)
962 u->u_noping &= ~MAPLE_FUNC(func);
963 else
964 u->u_noping |= MAPLE_FUNC(func);
965 }
966
967 /* schedule pinging a device */
968 static void
969 maple_unit_ping(struct maple_softc *sc)
970 {
971 struct maple_unit *u;
972 struct maple_func *fn;
973 #ifdef MAPLE_MEMCARD_PING_HACK
974 static const uint32_t memcard_ping_arg[2] = {
975 0x02000000, /* htobe32(MAPLE_FUNC(MAPLE_FN_MEMCARD)) */
976 0 /* pt (1 byte) and unused 3 bytes */
977 };
978 #endif
979
980 if ((u = TAILQ_FIRST(&sc->sc_pingq)) != NULL) {
981 KASSERT(u->u_queuestat == MAPLE_QUEUE_PING);
982 maple_remove_from_queues(sc, u);
983 if (u->u_dma_stat == MAPLE_DMA_IDLE && u->u_noping == 0) {
984 #ifdef MAPLE_MEMCARD_PING_HACK
985 if (u->u_ping_stat == MAPLE_PING_MINFO) {
986 /* use MINFO for some memory cards */
987 maple_write_command(sc, u,
988 MAPLE_COMMAND_GETMINFO,
989 2, memcard_ping_arg);
990 } else
991 #endif
992 {
993 fn = &u->u_func[u->u_ping_func];
994 fn->f_work = htobe32(MAPLE_FUNC(u->u_ping_func));
995 maple_write_command(sc, u,
996 MAPLE_COMMAND_GETCOND,
997 1, &fn->f_work);
998 }
999 u->u_dma_stat = MAPLE_DMA_PING;
1000 /* u->u_dma_func = XXX; */
1001 } else {
1002 /* no need if periodic */
1003 TAILQ_INSERT_TAIL(&sc->sc_pingq, u, u_q);
1004 u->u_queuestat = MAPLE_QUEUE_PING;
1005 }
1006 }
1007 }
1008
1009 /*
1010 * Enable/disable periodic GETCOND (called by drivers)
1011 */
1012 void
1013 maple_enable_periodic(device_t dev, struct maple_unit *u, int func, int on)
1014 {
1015 struct maple_softc *sc = device_private(dev);
1016 struct maple_func *fn;
1017
1018 KASSERT(func >= 0 && func < 32);
1019
1020 fn = &u->u_func[func];
1021
1022 if (on) {
1023 if (fn->f_periodic_stat == MAPLE_PERIODIC_NONE) {
1024 TAILQ_INSERT_TAIL(&sc->sc_periodicq, fn, f_periodicq);
1025 fn->f_periodic_stat = MAPLE_PERIODIC_INQ;
1026 u->getcond_func_set |= MAPLE_FUNC(func);
1027 }
1028 } else {
1029 if (fn->f_periodic_stat == MAPLE_PERIODIC_INQ)
1030 TAILQ_REMOVE(&sc->sc_periodicq, fn, f_periodicq);
1031 else if (fn->f_periodic_stat == MAPLE_PERIODIC_DEFERED)
1032 TAILQ_REMOVE(&sc->sc_periodicdeferq, fn, f_periodicq);
1033 fn->f_periodic_stat = MAPLE_PERIODIC_NONE;
1034 u->getcond_func_set &= ~MAPLE_FUNC(func);
1035 }
1036 }
1037
1038 /*
1039 * queue periodic GETCOND
1040 */
1041 static int
1042 maple_send_defered_periodic(struct maple_softc *sc)
1043 {
1044 struct maple_unit *u;
1045 struct maple_func *fn, *nextfn;
1046 int defer_remain = 0;
1047
1048 for (fn = TAILQ_FIRST(&sc->sc_periodicdeferq); fn; fn = nextfn) {
1049 KASSERT(fn->f_periodic_stat == MAPLE_PERIODIC_DEFERED);
1050
1051 nextfn = TAILQ_NEXT(fn, f_periodicq);
1052
1053 u = fn->f_unit;
1054 if (u->u_dma_stat == MAPLE_DMA_IDLE ||
1055 u->u_dma_stat == MAPLE_DMA_RETRY) {
1056 /*
1057 * if IDLE -> queue this request
1058 * if RETRY -> the unit never be freed until the next
1059 * periodic timing, so just restore to
1060 * the normal periodic queue.
1061 */
1062 TAILQ_REMOVE(&sc->sc_periodicdeferq, fn, f_periodicq);
1063 TAILQ_INSERT_TAIL(&sc->sc_periodicq, fn, f_periodicq);
1064 fn->f_periodic_stat = MAPLE_PERIODIC_INQ;
1065
1066 if (u->u_dma_stat == MAPLE_DMA_IDLE) {
1067 /*
1068 * queue periodic command
1069 */
1070 fn->f_work = htobe32(MAPLE_FUNC(fn->f_funcno));
1071 maple_write_command(sc, u,
1072 MAPLE_COMMAND_GETCOND, 1, &fn->f_work);
1073 u->u_dma_stat = MAPLE_DMA_PERIODIC;
1074 u->u_dma_func = fn->f_funcno;
1075 }
1076 } else {
1077 defer_remain = 1;
1078 }
1079 }
1080
1081 return defer_remain;
1082 }
1083
1084 static void
1085 maple_send_periodic(struct maple_softc *sc)
1086 {
1087 struct maple_unit *u;
1088 struct maple_func *fn, *nextfn;
1089
1090 for (fn = TAILQ_FIRST(&sc->sc_periodicq); fn; fn = nextfn) {
1091 KASSERT(fn->f_periodic_stat == MAPLE_PERIODIC_INQ);
1092
1093 nextfn = TAILQ_NEXT(fn, f_periodicq);
1094
1095 u = fn->f_unit;
1096 if (u->u_dma_stat != MAPLE_DMA_IDLE) {
1097 if (u->u_dma_stat != MAPLE_DMA_RETRY) {
1098 /*
1099 * can't be queued --- move to defered queue
1100 */
1101 TAILQ_REMOVE(&sc->sc_periodicq, fn,
1102 f_periodicq);
1103 TAILQ_INSERT_TAIL(&sc->sc_periodicdeferq, fn,
1104 f_periodicq);
1105 fn->f_periodic_stat = MAPLE_PERIODIC_DEFERED;
1106 }
1107 } else {
1108 /*
1109 * queue periodic command
1110 */
1111 fn->f_work = htobe32(MAPLE_FUNC(fn->f_funcno));
1112 maple_write_command(sc, u, MAPLE_COMMAND_GETCOND,
1113 1, &fn->f_work);
1114 u->u_dma_stat = MAPLE_DMA_PERIODIC;
1115 u->u_dma_func = fn->f_funcno;
1116 }
1117 }
1118 }
1119
1120 static void
1121 maple_remove_from_queues(struct maple_softc *sc, struct maple_unit *u)
1122 {
1123
1124 /* remove from queues */
1125 if (u->u_queuestat == MAPLE_QUEUE_PROBE)
1126 TAILQ_REMOVE(&sc->sc_probeq, u, u_q);
1127 else if (u->u_queuestat == MAPLE_QUEUE_PING)
1128 TAILQ_REMOVE(&sc->sc_pingq, u, u_q);
1129 #ifdef DIAGNOSTIC
1130 else if (u->u_queuestat != MAPLE_QUEUE_NONE)
1131 panic("maple_remove_from_queues: queuestat %d", u->u_queuestat);
1132 #endif
1133 #if defined(MAPLE_DEBUG) && MAPLE_DEBUG > 2
1134 if (u->u_queuestat != MAPLE_QUEUE_NONE) {
1135 char buf[16];
1136 printf("%s: dequeued\n",
1137 maple_unit_name(buf, sizeof(buf), u->port, u->subunit));
1138 }
1139 #endif
1140
1141 u->u_queuestat = MAPLE_QUEUE_NONE;
1142 }
1143
1144 /*
1145 * retry current command at next periodic timing
1146 */
1147 static int
1148 maple_retry(struct maple_softc *sc, struct maple_unit *u,
1149 enum maple_dma_stat st)
1150 {
1151
1152 KASSERT(st != MAPLE_DMA_IDLE && st != MAPLE_DMA_RETRY);
1153
1154 #if defined(MAPLE_DEBUG) && MAPLE_DEBUG > 2
1155 if (u->u_retrycnt == 0) {
1156 char buf[16];
1157 printf("%s: retrying: %#x, %#x, %p\n",
1158 maple_unit_name(buf, sizeof(buf), u->port, u->subunit),
1159 u->u_command, u->u_datalen, u->u_dataaddr);
1160 }
1161 #endif
1162 if (u->u_retrycnt >= MAPLE_RETRY_MAX)
1163 return 1;
1164
1165 u->u_retrycnt++;
1166
1167 u->u_saved_dma_stat = st;
1168 u->u_dma_stat = MAPLE_DMA_RETRY; /* no new command before retry done */
1169 SIMPLEQ_INSERT_TAIL(&sc->sc_retryq, u, u_dmaq);
1170
1171 return 0;
1172 }
1173
1174 static void
1175 maple_queue_retry(struct maple_softc *sc)
1176 {
1177 struct maple_unit *u, *nextu;
1178
1179 /*
1180 * Note: since the queue element is queued immediately
1181 * in maple_queue_command, we can't use SIMPLEQ_FOREACH.
1182 */
1183 for (u = SIMPLEQ_FIRST(&sc->sc_retryq); u; u = nextu) {
1184 nextu = SIMPLEQ_NEXT(u, u_dmaq);
1185
1186 /*
1187 * Retrying is in the highest priority, and the unit shall
1188 * always be free.
1189 */
1190 KASSERT(u->u_dma_stat == MAPLE_DMA_RETRY);
1191 maple_queue_command(sc, u, u->u_command, u->u_datalen,
1192 u->u_dataaddr);
1193 u->u_dma_stat = u->u_saved_dma_stat;
1194
1195 #ifdef DIAGNOSTIC
1196 KASSERT(u->u_saved_dma_stat != MAPLE_DMA_IDLE);
1197 u->u_saved_dma_stat = MAPLE_DMA_IDLE;
1198 #endif
1199 }
1200 SIMPLEQ_INIT(&sc->sc_retryq);
1201 }
1202
1203 /*
1204 * Process DMA results.
1205 * Requires kernel context.
1206 */
1207 static void
1208 maple_check_responses(struct maple_softc *sc)
1209 {
1210 struct maple_unit *u, *nextu;
1211 struct maple_func *fn;
1212 maple_response_t response;
1213 int func_code, len;
1214 int flags;
1215 char buf[16];
1216
1217 /*
1218 * Note: since the queue element may be queued immediately,
1219 * we can't use SIMPLEQ_FOREACH.
1220 */
1221 for (u = SIMPLEQ_FIRST(&sc->sc_dmaq), maple_begin_txbuf(sc);
1222 u; u = nextu) {
1223 nextu = SIMPLEQ_NEXT(u, u_dmaq);
1224
1225 if (u->u_dma_stat == MAPLE_DMA_IDLE)
1226 continue; /* just detached or DDB was active */
1227
1228 /*
1229 * check for retransmission
1230 */
1231 if ((response = u->u_rxbuf[0]) == MAPLE_RESPONSE_AGAIN) {
1232 if (maple_retry(sc, u, u->u_dma_stat) == 0)
1233 continue;
1234 /* else pass error to upper layer */
1235 }
1236
1237 len = (u->u_rxbuf[0] >> 24); /* length in long */
1238 len <<= 2; /* length in byte */
1239
1240 /*
1241 * call handler
1242 */
1243 if (u->u_dma_stat == MAPLE_DMA_PERIODIC) {
1244 /*
1245 * periodic GETCOND
1246 */
1247 u->u_dma_stat = MAPLE_DMA_IDLE;
1248 func_code = u->u_dma_func;
1249 if (response == MAPLE_RESPONSE_DATATRF && len > 0 &&
1250 be32toh(u->u_rxbuf[1]) == MAPLE_FUNC(func_code)) {
1251 fn = &u->u_func[func_code];
1252 if (fn->f_dev)
1253 (*fn->f_callback)(fn->f_arg,
1254 (void *)u->u_rxbuf, len,
1255 MAPLE_FLAG_PERIODIC);
1256 } else if (response == MAPLE_RESPONSE_NONE) {
1257 /* XXX OK? */
1258 /* detach */
1259 #if defined(MAPLE_DEBUG) && MAPLE_DEBUG > 2
1260 printf("%s: func: %d: periodic response %d\n",
1261 maple_unit_name(buf, sizeof(buf), u->port, u->subunit),
1262 u->u_dma_func,
1263 response);
1264 #endif
1265 /*
1266 * Some 3rd party devices sometimes
1267 * do not respond.
1268 */
1269 if (maple_retry(sc, u, MAPLE_DMA_PERIODIC))
1270 maple_detach_unit(sc, u);
1271 }
1272 /* XXX check unexpected conditions? */
1273
1274 } else if (u->u_dma_stat == MAPLE_DMA_PROBE) {
1275 KASSERT(u->u_queuestat == MAPLE_QUEUE_NONE);
1276 u->u_dma_stat = MAPLE_DMA_IDLE;
1277 switch (response) {
1278 default:
1279 case MAPLE_RESPONSE_NONE:
1280 /*
1281 * Do not use maple_retry(), which conflicts
1282 * with probe structure.
1283 */
1284 if (u->subunit != 0 &&
1285 ++u->u_proberetry > MAPLE_PROBERETRY_MAX) {
1286 printf("%s: no response\n",
1287 maple_unit_name(buf, sizeof(buf),
1288 u->port, u->subunit));
1289 } else {
1290 /* probe again */
1291 #if defined(MAPLE_DEBUG) && MAPLE_DEBUG > 2
1292 printf("%s: queued to probe 4\n",
1293 maple_unit_name(buf, sizeof(buf), u->port, u->subunit));
1294 #endif
1295 TAILQ_INSERT_TAIL(&sc->sc_probeq, u,
1296 u_q);
1297 u->u_queuestat = MAPLE_QUEUE_PROBE;
1298 }
1299 break;
1300 case MAPLE_RESPONSE_DEVINFO:
1301 /* check if the unit is changed */
1302 maple_check_unit_change(sc, u);
1303 break;
1304 }
1305
1306 } else if (u->u_dma_stat == MAPLE_DMA_PING) {
1307 KASSERT(u->u_queuestat == MAPLE_QUEUE_NONE);
1308 u->u_dma_stat = MAPLE_DMA_IDLE;
1309 switch (response) {
1310 default:
1311 case MAPLE_RESPONSE_NONE:
1312 /*
1313 * Some 3rd party devices sometimes
1314 * do not respond.
1315 */
1316 if (maple_retry(sc, u, MAPLE_DMA_PING)) {
1317 /* detach */
1318 #if defined(MAPLE_DEBUG) && MAPLE_DEBUG > 1
1319 printf("%s: ping response %d\n",
1320 maple_unit_name(buf, sizeof(buf), u->port,
1321 u->subunit),
1322 response);
1323 #endif
1324 #ifdef MAPLE_MEMCARD_PING_HACK
1325 if (u->u_ping_stat
1326 == MAPLE_PING_MEMCARD) {
1327 /*
1328 * The unit claims itself to be
1329 * a Visual Memory, and has
1330 * never responded to GETCOND.
1331 * Try again using MINFO, in
1332 * case it is a poorly
1333 * implemented 3rd party card.
1334 */
1335 #ifdef MAPLE_DEBUG
1336 printf("%s: switching ping method\n",
1337 maple_unit_name(buf, sizeof(buf),
1338 u->port, u->subunit));
1339 #endif
1340 u->u_ping_stat
1341 = MAPLE_PING_MINFO;
1342 TAILQ_INSERT_TAIL(&sc->sc_pingq,
1343 u, u_q);
1344 u->u_queuestat
1345 = MAPLE_QUEUE_PING;
1346 } else
1347 #endif /* MAPLE_MEMCARD_PING_HACK */
1348 maple_detach_unit(sc, u);
1349 }
1350 break;
1351 case MAPLE_RESPONSE_BADCMD:
1352 case MAPLE_RESPONSE_BADFUNC:
1353 case MAPLE_RESPONSE_DATATRF:
1354 TAILQ_INSERT_TAIL(&sc->sc_pingq, u, u_q);
1355 u->u_queuestat = MAPLE_QUEUE_PING;
1356 #ifdef MAPLE_MEMCARD_PING_HACK
1357 /*
1358 * If the unit responds to GETCOND, it is a
1359 * normal implementation.
1360 */
1361 if (u->u_ping_stat == MAPLE_PING_MEMCARD)
1362 u->u_ping_stat = MAPLE_PING_NORMAL;
1363 #endif
1364 break;
1365 }
1366
1367 } else {
1368 /*
1369 * Note: Do not rely on the consistency of responses.
1370 */
1371
1372 if (response == MAPLE_RESPONSE_NONE) {
1373 if (maple_retry(sc, u, u->u_dma_stat)) {
1374 /* detach */
1375 #if defined(MAPLE_DEBUG) && MAPLE_DEBUG > 1
1376 printf("%s: command response %d\n",
1377 maple_unit_name(buf, sizeof(buf), u->port,
1378 u->subunit),
1379 response);
1380 #endif
1381 maple_detach_unit(sc, u);
1382 }
1383 continue;
1384 }
1385
1386 flags = (u->u_dma_stat == MAPLE_DMA_PCMD) ?
1387 MAPLE_FLAG_CMD_PERIODIC_TIMING : 0;
1388 u->u_dma_stat = MAPLE_DMA_IDLE;
1389
1390 func_code = u->u_dma_func;
1391 fn = &u->u_func[func_code];
1392 if (fn->f_dev == NULL) {
1393 /* detached right now */
1394 #ifdef MAPLE_DEBUG
1395 printf("%s: unknown function: function %d, response %d\n",
1396 maple_unit_name(buf, sizeof(buf), u->port, u->subunit),
1397 func_code, response);
1398 #endif
1399 continue;
1400 }
1401 if (fn->f_callback != NULL) {
1402 (*fn->f_callback)(fn->f_arg,
1403 (void *)u->u_rxbuf, len, flags);
1404 }
1405 }
1406
1407 /*
1408 * check for subunit change and schedule probing subunits
1409 */
1410 if (u->subunit == 0 && response != MAPLE_RESPONSE_NONE &&
1411 response != MAPLE_RESPONSE_AGAIN &&
1412 ((int8_t *) u->u_rxbuf)[2] != sc->sc_port_unit_map[u->port])
1413 maple_check_subunit_change(sc, u);
1414 }
1415 }
1416
1417 /*
1418 * Main Maple Bus thread
1419 */
1420 static void
1421 maple_event_thread(void *arg)
1422 {
1423 struct maple_softc *sc = arg;
1424 unsigned cnt = 1; /* timing counter */
1425 int s;
1426 #if defined(MAPLE_DEBUG) && MAPLE_DEBUG > 1
1427 int noreq = 0;
1428 #endif
1429
1430 #ifdef MAPLE_DEBUG
1431 printf("%s: forked event thread, pid %d\n",
1432 device_xname(sc->sc_dev), sc->event_thread->l_proc->p_pid);
1433 #endif
1434
1435 /* begin first DMA cycle */
1436 maple_begin_txbuf(sc);
1437
1438 sc->sc_event = 1;
1439
1440 /* OK, continue booting system */
1441 maple_polling = 0;
1442 config_pending_decr(sc->sc_dev);
1443
1444 for (;;) {
1445 /*
1446 * queue requests
1447 */
1448
1449 /* queue async commands */
1450 if (!TAILQ_EMPTY(&sc->sc_acmdq))
1451 maple_queue_cmds(sc, &sc->sc_acmdq);
1452
1453 /* send defered periodic command */
1454 if (!TAILQ_EMPTY(&sc->sc_periodicdeferq))
1455 maple_send_defered_periodic(sc);
1456
1457 /* queue periodic commands */
1458 if (sc->sc_event) {
1459 /* queue commands on periodic timing */
1460 if (!TAILQ_EMPTY(&sc->sc_pcmdq))
1461 maple_queue_cmds(sc, &sc->sc_pcmdq);
1462
1463 /* retry */
1464 if (!SIMPLEQ_EMPTY(&sc->sc_retryq))
1465 maple_queue_retry(sc);
1466
1467 if ((cnt & 31) == 0) /* XXX */
1468 maple_unit_probe(sc);
1469 cnt++;
1470
1471 maple_send_periodic(sc);
1472 if ((cnt & 7) == 0) /* XXX */
1473 maple_unit_ping(sc);
1474
1475 /*
1476 * schedule periodic event
1477 */
1478 sc->sc_event = 0;
1479 callout_reset(&sc->maple_callout_ch,
1480 MAPLE_CALLOUT_TICKS, maple_callout, sc);
1481 }
1482
1483 if (maple_end_txbuf(sc)) {
1484
1485 /*
1486 * start DMA
1487 */
1488 s = splmaple();
1489 maple_start(sc);
1490
1491 /*
1492 * wait until DMA done
1493 */
1494 if (tsleep(&sc->sc_dmadone, PWAIT, "mdma", hz)
1495 == EWOULDBLOCK) {
1496 /* was DDB active? */
1497 printf("%s: timed out\n",
1498 device_xname(sc->sc_dev));
1499 }
1500 splx(s);
1501
1502 /*
1503 * call handlers
1504 */
1505 maple_check_responses(sc);
1506 #if defined(MAPLE_DEBUG) && MAPLE_DEBUG > 1
1507 noreq = 0;
1508 #endif
1509 }
1510 #if defined(MAPLE_DEBUG) && MAPLE_DEBUG > 1
1511 else {
1512 /* weird if occurs in succession */
1513 #if MAPLE_DEBUG <= 2
1514 if (noreq) /* ignore first time */
1515 #endif
1516 printf("%s: no request %d\n",
1517 device_xname(sc->sc_dev), noreq);
1518 noreq++;
1519 }
1520 #endif
1521
1522 /*
1523 * wait for an event
1524 */
1525 s = splsoftclock();
1526 if (TAILQ_EMPTY(&sc->sc_acmdq) && sc->sc_event == 0 &&
1527 TAILQ_EMPTY(&sc->sc_periodicdeferq)) {
1528 if (tsleep(&sc->sc_event, PWAIT, "mslp", hz)
1529 == EWOULDBLOCK) {
1530 printf("%s: event timed out\n",
1531 device_xname(sc->sc_dev));
1532 }
1533
1534 }
1535 splx(s);
1536
1537 }
1538
1539 #if 0 /* maple root device can't be detached */
1540 kthread_exit(0);
1541 /* NOTREACHED */
1542 #endif
1543 }
1544
1545 static int
1546 maple_intr(void *arg)
1547 {
1548 struct maple_softc *sc = arg;
1549
1550 wakeup(&sc->sc_dmadone);
1551
1552 return 1;
1553 }
1554
1555 static void
1556 maple_callout(void *ctx)
1557 {
1558 struct maple_softc *sc = ctx;
1559
1560 sc->sc_event = 1; /* mark as periodic event */
1561 wakeup(&sc->sc_event);
1562 }
1563
1564 /*
1565 * Install callback handler (called by drivers)
1566 */
1567 /* ARGSUSED */
1568 void
1569 maple_set_callback(device_t dev, struct maple_unit *u, int func,
1570 void (*callback)(void *, struct maple_response *, int, int), void *arg)
1571 {
1572 #if 0 /* currently unused */
1573 struct maple_softc *sc = device_private(dev);
1574 #endif
1575 struct maple_func *fn;
1576
1577 KASSERT(func >= 0 && func < MAPLE_NFUNC);
1578
1579 fn = &u->u_func[func];
1580
1581 fn->f_callback = callback;
1582 fn->f_arg = arg;
1583 }
1584
1585 /*
1586 * Return function definition data (called by drivers)
1587 */
1588 uint32_t
1589 maple_get_function_data(struct maple_devinfo *devinfo, int function_code)
1590 {
1591 int i, p = 0;
1592 uint32_t func;
1593
1594 func = be32toh(devinfo->di_func);
1595 for (i = 31; i >= 0; --i)
1596 if (func & MAPLE_FUNC(i)) {
1597 if (function_code == i)
1598 return be32toh(devinfo->di_function_data[p]);
1599 else
1600 if (++p >= 3)
1601 break;
1602 }
1603
1604 return 0;
1605 }
1606
1607 /* Generic maple device interface */
1608
1609 int
1610 mapleopen(dev_t dev, int flag, int mode, struct lwp *l)
1611 {
1612 struct maple_softc *sc;
1613
1614 sc = device_lookup_private(&maple_cd, MAPLEBUSUNIT(dev));
1615 if (sc == NULL) /* make sure it was attached */
1616 return ENXIO;
1617
1618 if (MAPLEPORT(dev) >= MAPLE_PORTS)
1619 return ENXIO;
1620
1621 if (MAPLESUBUNIT(dev) >= MAPLE_SUBUNITS)
1622 return ENXIO;
1623
1624 if (!(sc->sc_port_units[MAPLEPORT(dev)] & (1 << MAPLESUBUNIT(dev))))
1625 return ENXIO;
1626
1627 sc->sc_port_units_open[MAPLEPORT(dev)] |= 1 << MAPLESUBUNIT(dev);
1628
1629 return 0;
1630 }
1631
1632 int
1633 mapleclose(dev_t dev, int flag, int mode, struct lwp *l)
1634 {
1635 struct maple_softc *sc;
1636
1637 sc = device_lookup_private(&maple_cd, MAPLEBUSUNIT(dev));
1638
1639 sc->sc_port_units_open[MAPLEPORT(dev)] &= ~(1 << MAPLESUBUNIT(dev));
1640
1641 return 0;
1642 }
1643
1644 int
1645 maple_unit_ioctl(device_t dev, struct maple_unit *u, u_long cmd,
1646 void *data, int flag, struct lwp *l)
1647 {
1648 struct maple_softc *sc = device_private(dev);
1649
1650 if (!(sc->sc_port_units[u->port] & (1 << u->subunit)))
1651 return ENXIO;
1652
1653 switch(cmd) {
1654 case MAPLEIO_GDEVINFO:
1655 memcpy(data, &u->devinfo, sizeof(struct maple_devinfo));
1656 break;
1657 default:
1658 return EPASSTHROUGH;
1659 }
1660
1661 return 0;
1662 }
1663
1664 int
1665 mapleioctl(dev_t dev, u_long cmd, void *data, int flag, struct lwp *l)
1666 {
1667 struct maple_softc *sc;
1668 struct maple_unit *u;
1669
1670 sc = device_lookup_private(&maple_cd, MAPLEBUSUNIT(dev));
1671 u = &sc->sc_unit[MAPLEPORT(dev)][MAPLESUBUNIT(dev)];
1672
1673 return maple_unit_ioctl(sc->sc_dev, u, cmd, data, flag, l);
1674 }
1675