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