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