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