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