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