xref: /netbsd-src/sys/dev/pci/amr.c (revision e5548b402ae4c44fb816de42c7bba9581ce23ef5)
1 /*	$NetBSD: amr.c,v 1.31 2005/12/11 19:34:47 jonathan Exp $	*/
2 
3 /*-
4  * Copyright (c) 2002, 2003 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Andrew Doran.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  * 3. All advertising materials mentioning features or use of this software
19  *    must display the following acknowledgement:
20  *        This product includes software developed by the NetBSD
21  *        Foundation, Inc. and its contributors.
22  * 4. Neither the name of The NetBSD Foundation nor the names of its
23  *    contributors may be used to endorse or promote products derived
24  *    from this software without specific prior written permission.
25  *
26  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36  * POSSIBILITY OF SUCH DAMAGE.
37  */
38 
39 /*-
40  * Copyright (c) 1999,2000 Michael Smith
41  * Copyright (c) 2000 BSDi
42  * All rights reserved.
43  *
44  * Redistribution and use in source and binary forms, with or without
45  * modification, are permitted provided that the following conditions
46  * are met:
47  * 1. Redistributions of source code must retain the above copyright
48  *    notice, this list of conditions and the following disclaimer.
49  * 2. Redistributions in binary form must reproduce the above copyright
50  *    notice, this list of conditions and the following disclaimer in the
51  *    documentation and/or other materials provided with the distribution.
52  *
53  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
54  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
55  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
56  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
57  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
58  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
59  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
60  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
61  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
62  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
63  * SUCH DAMAGE.
64  *
65  * from FreeBSD: amr_pci.c,v 1.5 2000/08/30 07:52:40 msmith Exp
66  * from FreeBSD: amr.c,v 1.16 2000/08/30 07:52:40 msmith Exp
67  */
68 
69 /*
70  * Driver for AMI RAID controllers.
71  */
72 
73 #include <sys/cdefs.h>
74 __KERNEL_RCSID(0, "$NetBSD: amr.c,v 1.31 2005/12/11 19:34:47 jonathan Exp $");
75 
76 #include <sys/param.h>
77 #include <sys/systm.h>
78 #include <sys/kernel.h>
79 #include <sys/device.h>
80 #include <sys/queue.h>
81 #include <sys/proc.h>
82 #include <sys/buf.h>
83 #include <sys/malloc.h>
84 #include <sys/kthread.h>
85 
86 #include <uvm/uvm_extern.h>
87 
88 #include <machine/endian.h>
89 #include <machine/bus.h>
90 
91 #include <dev/pci/pcidevs.h>
92 #include <dev/pci/pcivar.h>
93 #include <dev/pci/amrreg.h>
94 #include <dev/pci/amrvar.h>
95 
96 #include "locators.h"
97 
98 static void	amr_attach(struct device *, struct device *, void *);
99 static void	amr_ccb_dump(struct amr_softc *, struct amr_ccb *);
100 static void	*amr_enquire(struct amr_softc *, u_int8_t, u_int8_t, u_int8_t,
101 			     void *);
102 static int	amr_init(struct amr_softc *, const char *,
103 			 struct pci_attach_args *pa);
104 static int	amr_intr(void *);
105 static int	amr_match(struct device *, struct cfdata *, void *);
106 static int	amr_print(void *, const char *);
107 static void	amr_shutdown(void *);
108 static void	amr_teardown(struct amr_softc *);
109 static void	amr_thread(void *);
110 static void	amr_thread_create(void *);
111 
112 static int	amr_quartz_get_work(struct amr_softc *,
113 				    struct amr_mailbox_resp *);
114 static int	amr_quartz_submit(struct amr_softc *, struct amr_ccb *);
115 static int	amr_std_get_work(struct amr_softc *, struct amr_mailbox_resp *);
116 static int	amr_std_submit(struct amr_softc *, struct amr_ccb *);
117 
118 CFATTACH_DECL(amr, sizeof(struct amr_softc),
119     amr_match, amr_attach, NULL, NULL);
120 
121 #define AT_QUARTZ	0x01	/* `Quartz' chipset */
122 #define	AT_SIG		0x02	/* Check for signature */
123 
124 struct amr_pci_type {
125 	u_short	apt_vendor;
126 	u_short	apt_product;
127 	u_short	apt_flags;
128 } static const amr_pci_type[] = {
129 	{ PCI_VENDOR_AMI,   PCI_PRODUCT_AMI_MEGARAID,  0 },
130 	{ PCI_VENDOR_AMI,   PCI_PRODUCT_AMI_MEGARAID2, 0 },
131 	{ PCI_VENDOR_AMI,   PCI_PRODUCT_AMI_MEGARAID3, AT_QUARTZ },
132 	{ PCI_VENDOR_SYMBIOS, PCI_PRODUCT_AMI_MEGARAID3, AT_QUARTZ },
133 	{ PCI_VENDOR_INTEL, PCI_PRODUCT_AMI_MEGARAID3, AT_QUARTZ | AT_SIG },
134 	{ PCI_VENDOR_INTEL,  PCI_PRODUCT_SYMBIOS_MEGARAID_320X, AT_QUARTZ },
135 	{ PCI_VENDOR_INTEL,  PCI_PRODUCT_SYMBIOS_MEGARAID_320E, AT_QUARTZ },
136 	{ PCI_VENDOR_SYMBIOS,  PCI_PRODUCT_SYMBIOS_MEGARAID_300X, AT_QUARTZ },
137 	{ PCI_VENDOR_DELL,  PCI_PRODUCT_DELL_PERC_4DI, AT_QUARTZ },
138 	{ PCI_VENDOR_DELL,  PCI_PRODUCT_DELL_PERC_4DI_2, AT_QUARTZ },
139 	{ PCI_VENDOR_DELL,  PCI_PRODUCT_DELL_PERC_4ESI, AT_QUARTZ },
140 	{ PCI_VENDOR_SYMBIOS,  PCI_PRODUCT_SYMBIOS_PERC_4SC, AT_QUARTZ },
141 	{ PCI_VENDOR_SYMBIOS,  PCI_PRODUCT_SYMBIOS_MEGARAID_320X, AT_QUARTZ },
142 	{ PCI_VENDOR_SYMBIOS,  PCI_PRODUCT_SYMBIOS_MEGARAID_320E, AT_QUARTZ },
143 	{ PCI_VENDOR_SYMBIOS,  PCI_PRODUCT_SYMBIOS_MEGARAID_300X, AT_QUARTZ },
144 };
145 
146 struct amr_typestr {
147 	const char	*at_str;
148 	int		at_sig;
149 } static const amr_typestr[] = {
150 	{ "Series 431",			AMR_SIG_431 },
151 	{ "Series 438",			AMR_SIG_438 },
152 	{ "Series 466",			AMR_SIG_466 },
153 	{ "Series 467",			AMR_SIG_467 },
154 	{ "Series 490",			AMR_SIG_490 },
155 	{ "Series 762",			AMR_SIG_762 },
156 	{ "HP NetRAID (T5)",		AMR_SIG_T5 },
157 	{ "HP NetRAID (T7)",		AMR_SIG_T7 },
158 };
159 
160 struct {
161 	const char	*ds_descr;
162 	int	ds_happy;
163 } static const amr_dstate[] = {
164 	{ "offline",	0 },
165 	{ "degraded",	1 },
166 	{ "optimal",	1 },
167 	{ "online",	1 },
168 	{ "failed",	0 },
169 	{ "rebuilding",	1 },
170 	{ "hotspare",	0 },
171 };
172 
173 static void	*amr_sdh;
174 
175 static int	amr_max_segs;
176 int		amr_max_xfer;
177 
178 static inline u_int8_t
179 amr_inb(struct amr_softc *amr, int off)
180 {
181 
182 	bus_space_barrier(amr->amr_iot, amr->amr_ioh, off, 1,
183 	    BUS_SPACE_BARRIER_WRITE | BUS_SPACE_BARRIER_READ);
184 	return (bus_space_read_1(amr->amr_iot, amr->amr_ioh, off));
185 }
186 
187 static inline u_int32_t
188 amr_inl(struct amr_softc *amr, int off)
189 {
190 
191 	bus_space_barrier(amr->amr_iot, amr->amr_ioh, off, 4,
192 	    BUS_SPACE_BARRIER_WRITE | BUS_SPACE_BARRIER_READ);
193 	return (bus_space_read_4(amr->amr_iot, amr->amr_ioh, off));
194 }
195 
196 static inline void
197 amr_outb(struct amr_softc *amr, int off, u_int8_t val)
198 {
199 
200 	bus_space_write_1(amr->amr_iot, amr->amr_ioh, off, val);
201 	bus_space_barrier(amr->amr_iot, amr->amr_ioh, off, 1,
202 	    BUS_SPACE_BARRIER_WRITE);
203 }
204 
205 static inline void
206 amr_outl(struct amr_softc *amr, int off, u_int32_t val)
207 {
208 
209 	bus_space_write_4(amr->amr_iot, amr->amr_ioh, off, val);
210 	bus_space_barrier(amr->amr_iot, amr->amr_ioh, off, 4,
211 	    BUS_SPACE_BARRIER_WRITE);
212 }
213 
214 /*
215  * Match a supported device.
216  */
217 static int
218 amr_match(struct device *parent, struct cfdata *match, void *aux)
219 {
220 	struct pci_attach_args *pa;
221 	pcireg_t s;
222 	int i;
223 
224 	pa = (struct pci_attach_args *)aux;
225 
226 	/*
227 	 * Don't match the device if it's operating in I2O mode.  In this
228 	 * case it should be handled by the `iop' driver.
229 	 */
230 	if (PCI_CLASS(pa->pa_class) == PCI_CLASS_I2O)
231 		return (0);
232 
233 	for (i = 0; i < sizeof(amr_pci_type) / sizeof(amr_pci_type[0]); i++)
234 		if (PCI_VENDOR(pa->pa_id) == amr_pci_type[i].apt_vendor &&
235 		    PCI_PRODUCT(pa->pa_id) == amr_pci_type[i].apt_product)
236 		    	break;
237 
238 	if (i == sizeof(amr_pci_type) / sizeof(amr_pci_type[0]))
239 		return (0);
240 
241 	if ((amr_pci_type[i].apt_flags & AT_SIG) == 0)
242 		return (1);
243 
244 	s = pci_conf_read(pa->pa_pc, pa->pa_tag, AMR_QUARTZ_SIG_REG) & 0xffff;
245 	return (s == AMR_QUARTZ_SIG0 || s == AMR_QUARTZ_SIG1);
246 }
247 
248 /*
249  * Attach a supported device.
250  */
251 static void
252 amr_attach(struct device *parent, struct device *self, void *aux)
253 {
254 	struct pci_attach_args *pa;
255 	struct amr_attach_args amra;
256 	const struct amr_pci_type *apt;
257 	struct amr_softc *amr;
258 	pci_chipset_tag_t pc;
259 	pci_intr_handle_t ih;
260 	const char *intrstr;
261 	pcireg_t reg;
262 	int rseg, i, j, size, rv, memreg, ioreg;
263         struct amr_ccb *ac;
264 	int locs[AMRCF_NLOCS];
265 
266 	aprint_naive(": RAID controller\n");
267 
268 	amr = (struct amr_softc *)self;
269 	pa = (struct pci_attach_args *)aux;
270 	pc = pa->pa_pc;
271 
272 	for (i = 0; i < sizeof(amr_pci_type) / sizeof(amr_pci_type[0]); i++)
273 		if (PCI_VENDOR(pa->pa_id) == amr_pci_type[i].apt_vendor &&
274 		    PCI_PRODUCT(pa->pa_id) == amr_pci_type[i].apt_product)
275 			break;
276 	apt = amr_pci_type + i;
277 
278 	memreg = ioreg = 0;
279 	for (i = 0x10; i <= 0x14; i += 4) {
280 		reg = pci_conf_read(pc, pa->pa_tag, i);
281 		switch (PCI_MAPREG_TYPE(reg)) {
282 		case PCI_MAPREG_TYPE_MEM:
283 			if (PCI_MAPREG_MEM_SIZE(reg) != 0)
284 				memreg = i;
285 			break;
286 		case PCI_MAPREG_TYPE_IO:
287 			if (PCI_MAPREG_IO_SIZE(reg) != 0)
288 				ioreg = i;
289 			break;
290 
291 		}
292 	}
293 
294 	if (memreg && pci_mapreg_map(pa, memreg, PCI_MAPREG_TYPE_MEM, 0,
295 	    &amr->amr_iot, &amr->amr_ioh, NULL, &amr->amr_ios) == 0)
296 		;
297 	else if (ioreg && pci_mapreg_map(pa, ioreg, PCI_MAPREG_TYPE_IO, 0,
298 	    &amr->amr_iot, &amr->amr_ioh, NULL, &amr->amr_ios) == 0)
299 		;
300 	else {
301 		aprint_error("can't map control registers\n");
302 		amr_teardown(amr);
303 		return;
304 	}
305 
306 	amr->amr_flags |= AMRF_PCI_REGS;
307 	amr->amr_dmat = pa->pa_dmat;
308 	amr->amr_pc = pa->pa_pc;
309 
310 	/* Enable the device. */
311 	reg = pci_conf_read(pa->pa_pc, pa->pa_tag, PCI_COMMAND_STATUS_REG);
312 	pci_conf_write(pa->pa_pc, pa->pa_tag, PCI_COMMAND_STATUS_REG,
313 	    reg | PCI_COMMAND_MASTER_ENABLE);
314 
315 	/* Map and establish the interrupt. */
316 	if (pci_intr_map(pa, &ih)) {
317 		aprint_error("can't map interrupt\n");
318 		amr_teardown(amr);
319 		return;
320 	}
321 	intrstr = pci_intr_string(pc, ih);
322 	amr->amr_ih = pci_intr_establish(pc, ih, IPL_BIO, amr_intr, amr);
323 	if (amr->amr_ih == NULL) {
324 		aprint_error("can't establish interrupt");
325 		if (intrstr != NULL)
326 			aprint_normal(" at %s", intrstr);
327 		aprint_normal("\n");
328 		amr_teardown(amr);
329 		return;
330 	}
331 	amr->amr_flags |= AMRF_PCI_INTR;
332 
333 	/*
334 	 * Allocate space for the mailbox and S/G lists.  Some controllers
335 	 * don't like S/G lists to be located below 0x2000, so we allocate
336 	 * enough slop to enable us to compensate.
337 	 *
338 	 * The standard mailbox structure needs to be aligned on a 16-byte
339 	 * boundary.  The 64-bit mailbox has one extra field, 4 bytes in
340 	 * size, which preceeds the standard mailbox.
341 	 */
342 	size = AMR_SGL_SIZE * AMR_MAX_CMDS + 0x2000;
343 	amr->amr_dmasize = size;
344 
345 	if ((rv = bus_dmamem_alloc(amr->amr_dmat, size, PAGE_SIZE, 0,
346 	    &amr->amr_dmaseg, 1, &rseg, BUS_DMA_NOWAIT)) != 0) {
347 		aprint_error("%s: unable to allocate buffer, rv = %d\n",
348 		    amr->amr_dv.dv_xname, rv);
349 		amr_teardown(amr);
350 		return;
351 	}
352 	amr->amr_flags |= AMRF_DMA_ALLOC;
353 
354 	if ((rv = bus_dmamem_map(amr->amr_dmat, &amr->amr_dmaseg, rseg, size,
355 	    (caddr_t *)&amr->amr_mbox,
356 	    BUS_DMA_NOWAIT | BUS_DMA_COHERENT)) != 0) {
357 		aprint_error("%s: unable to map buffer, rv = %d\n",
358 		    amr->amr_dv.dv_xname, rv);
359 		amr_teardown(amr);
360 		return;
361 	}
362 	amr->amr_flags |= AMRF_DMA_MAP;
363 
364 	if ((rv = bus_dmamap_create(amr->amr_dmat, size, 1, size, 0,
365 	    BUS_DMA_NOWAIT, &amr->amr_dmamap)) != 0) {
366 		aprint_error("%s: unable to create buffer DMA map, rv = %d\n",
367 		    amr->amr_dv.dv_xname, rv);
368 		amr_teardown(amr);
369 		return;
370 	}
371 	amr->amr_flags |= AMRF_DMA_CREATE;
372 
373 	if ((rv = bus_dmamap_load(amr->amr_dmat, amr->amr_dmamap,
374 	    amr->amr_mbox, size, NULL, BUS_DMA_NOWAIT)) != 0) {
375 		aprint_error("%s: unable to load buffer DMA map, rv = %d\n",
376 		    amr->amr_dv.dv_xname, rv);
377 		amr_teardown(amr);
378 		return;
379 	}
380 	amr->amr_flags |= AMRF_DMA_LOAD;
381 
382 	memset(amr->amr_mbox, 0, size);
383 
384 	amr->amr_mbox_paddr = amr->amr_dmamap->dm_segs[0].ds_addr;
385 	amr->amr_sgls_paddr = (amr->amr_mbox_paddr + 0x1fff) & ~0x1fff;
386 	amr->amr_sgls = (struct amr_sgentry *)((caddr_t)amr->amr_mbox +
387 	    amr->amr_sgls_paddr - amr->amr_dmamap->dm_segs[0].ds_addr);
388 
389 	/*
390 	 * Allocate and initalise the command control blocks.
391 	 */
392 	ac = malloc(sizeof(*ac) * AMR_MAX_CMDS, M_DEVBUF, M_NOWAIT | M_ZERO);
393 	amr->amr_ccbs = ac;
394 	SLIST_INIT(&amr->amr_ccb_freelist);
395 	TAILQ_INIT(&amr->amr_ccb_active);
396 	amr->amr_flags |= AMRF_CCBS;
397 
398 	if (amr_max_xfer == 0) {
399 		amr_max_xfer = min(((AMR_MAX_SEGS - 1) * PAGE_SIZE), MAXPHYS);
400 		amr_max_segs = (amr_max_xfer + (PAGE_SIZE * 2) - 1) / PAGE_SIZE;
401 	}
402 
403 	for (i = 0; i < AMR_MAX_CMDS; i++, ac++) {
404 		rv = bus_dmamap_create(amr->amr_dmat, amr_max_xfer,
405 		    amr_max_segs, amr_max_xfer, 0,
406 		    BUS_DMA_NOWAIT | BUS_DMA_ALLOCNOW, &ac->ac_xfer_map);
407 		if (rv != 0)
408 			break;
409 
410 		ac->ac_ident = i;
411 		amr_ccb_free(amr, ac);
412 	}
413 	if (i != AMR_MAX_CMDS) {
414 		aprint_error("%s: memory exhausted\n", amr->amr_dv.dv_xname);
415 		amr_teardown(amr);
416 		return;
417 	}
418 
419 	/*
420 	 * Take care of model-specific tasks.
421 	 */
422 	if ((apt->apt_flags & AT_QUARTZ) != 0) {
423 		amr->amr_submit = amr_quartz_submit;
424 		amr->amr_get_work = amr_quartz_get_work;
425 	} else {
426 		amr->amr_submit = amr_std_submit;
427 		amr->amr_get_work = amr_std_get_work;
428 
429 		/* Notify the controller of the mailbox location. */
430 		amr_outl(amr, AMR_SREG_MBOX, (u_int32_t)amr->amr_mbox_paddr + 16);
431 		amr_outb(amr, AMR_SREG_MBOX_ENABLE, AMR_SMBOX_ENABLE_ADDR);
432 
433 		/* Clear outstanding interrupts and enable interrupts. */
434 		amr_outb(amr, AMR_SREG_CMD, AMR_SCMD_ACKINTR);
435 		amr_outb(amr, AMR_SREG_TOGL,
436 		    amr_inb(amr, AMR_SREG_TOGL) | AMR_STOGL_ENABLE);
437 	}
438 
439 	/*
440 	 * Retrieve parameters, and tell the world about us.
441 	 */
442 	amr->amr_enqbuf = malloc(AMR_ENQUIRY_BUFSIZE, M_DEVBUF, M_NOWAIT);
443 	amr->amr_flags |= AMRF_ENQBUF;
444 	amr->amr_maxqueuecnt = i;
445 	aprint_normal(": AMI RAID ");
446 	if (amr_init(amr, intrstr, pa) != 0) {
447 		amr_teardown(amr);
448 		return;
449 	}
450 
451 	/*
452 	 * Cap the maximum number of outstanding commands.  AMI's Linux
453 	 * driver doesn't trust the controller's reported value, and lockups
454 	 * have been seen when we do.
455 	 */
456 	amr->amr_maxqueuecnt = min(amr->amr_maxqueuecnt, AMR_MAX_CMDS);
457 	if (amr->amr_maxqueuecnt > i)
458 		amr->amr_maxqueuecnt = i;
459 
460 	/* Set our `shutdownhook' before we start any device activity. */
461 	if (amr_sdh == NULL)
462 		amr_sdh = shutdownhook_establish(amr_shutdown, NULL);
463 
464 	/* Attach sub-devices. */
465 	for (j = 0; j < amr->amr_numdrives; j++) {
466 		if (amr->amr_drive[j].al_size == 0)
467 			continue;
468 		amra.amra_unit = j;
469 
470 		locs[AMRCF_UNIT] = j;
471 
472 		amr->amr_drive[j].al_dv = config_found_sm_loc(&amr->amr_dv,
473 			"amr", locs, &amra, amr_print, config_stdsubmatch);
474 	}
475 
476 	SIMPLEQ_INIT(&amr->amr_ccb_queue);
477 
478 	/* XXX This doesn't work for newer boards yet. */
479 	if ((apt->apt_flags & AT_QUARTZ) == 0)
480 		kthread_create(amr_thread_create, amr);
481 }
482 
483 /*
484  * Free up resources.
485  */
486 static void
487 amr_teardown(struct amr_softc *amr)
488 {
489 	struct amr_ccb *ac;
490 	int fl;
491 
492 	fl = amr->amr_flags;
493 
494 	if ((fl & AMRF_THREAD) != 0) {
495 		amr->amr_flags |= AMRF_THREAD_EXIT;
496 		wakeup(amr_thread);
497 		while ((amr->amr_flags & AMRF_THREAD_EXIT) != 0)
498 			tsleep(&amr->amr_flags, PWAIT, "amrexit", 0);
499 	}
500 	if ((fl & AMRF_CCBS) != 0) {
501 		SLIST_FOREACH(ac, &amr->amr_ccb_freelist, ac_chain.slist) {
502 			bus_dmamap_destroy(amr->amr_dmat, ac->ac_xfer_map);
503 		}
504 		free(amr->amr_ccbs, M_DEVBUF);
505 	}
506 	if ((fl & AMRF_ENQBUF) != 0)
507 		free(amr->amr_enqbuf, M_DEVBUF);
508 	if ((fl & AMRF_DMA_LOAD) != 0)
509 		bus_dmamap_unload(amr->amr_dmat, amr->amr_dmamap);
510 	if ((fl & AMRF_DMA_MAP) != 0)
511 		bus_dmamem_unmap(amr->amr_dmat, (caddr_t)amr->amr_mbox,
512 		    amr->amr_dmasize);
513 	if ((fl & AMRF_DMA_ALLOC) != 0)
514 		bus_dmamem_free(amr->amr_dmat, &amr->amr_dmaseg, 1);
515 	if ((fl & AMRF_DMA_CREATE) != 0)
516 		bus_dmamap_destroy(amr->amr_dmat, amr->amr_dmamap);
517 	if ((fl & AMRF_PCI_INTR) != 0)
518 		pci_intr_disestablish(amr->amr_pc, amr->amr_ih);
519 	if ((fl & AMRF_PCI_REGS) != 0)
520 		bus_space_unmap(amr->amr_iot, amr->amr_ioh, amr->amr_ios);
521 }
522 
523 /*
524  * Print autoconfiguration message for a sub-device.
525  */
526 static int
527 amr_print(void *aux, const char *pnp)
528 {
529 	struct amr_attach_args *amra;
530 
531 	amra = (struct amr_attach_args *)aux;
532 
533 	if (pnp != NULL)
534 		aprint_normal("block device at %s", pnp);
535 	aprint_normal(" unit %d", amra->amra_unit);
536 	return (UNCONF);
537 }
538 
539 /*
540  * Retrieve operational parameters and describe the controller.
541  */
542 static int
543 amr_init(struct amr_softc *amr, const char *intrstr,
544 	 struct pci_attach_args *pa)
545 {
546 	struct amr_adapter_info *aa;
547 	struct amr_prodinfo *ap;
548 	struct amr_enquiry *ae;
549 	struct amr_enquiry3 *aex;
550 	const char *prodstr;
551 	u_int i, sig, ishp;
552 	char sbuf[64];
553 
554 	/*
555 	 * Try to get 40LD product info, which tells us what the card is
556 	 * labelled as.
557 	 */
558 	ap = amr_enquire(amr, AMR_CMD_CONFIG, AMR_CONFIG_PRODUCT_INFO, 0,
559 	    amr->amr_enqbuf);
560 	if (ap != NULL) {
561 		aprint_normal("<%.80s>\n", ap->ap_product);
562 		if (intrstr != NULL)
563 			aprint_normal("%s: interrupting at %s\n",
564 			    amr->amr_dv.dv_xname, intrstr);
565 		aprint_normal("%s: firmware %.16s, BIOS %.16s, %dMB RAM\n",
566 		    amr->amr_dv.dv_xname, ap->ap_firmware, ap->ap_bios,
567 		    le16toh(ap->ap_memsize));
568 
569 		amr->amr_maxqueuecnt = ap->ap_maxio;
570 
571 		/*
572 		 * Fetch and record state of logical drives.
573 		 */
574 		aex = amr_enquire(amr, AMR_CMD_CONFIG, AMR_CONFIG_ENQ3,
575 		    AMR_CONFIG_ENQ3_SOLICITED_FULL, amr->amr_enqbuf);
576 		if (aex == NULL) {
577 			aprint_error("%s ENQUIRY3 failed\n",
578 			    amr->amr_dv.dv_xname);
579 			return (-1);
580 		}
581 
582 		if (aex->ae_numldrives > AMR_MAX_UNITS) {
583 			aprint_error(
584 			    "%s: adjust AMR_MAX_UNITS to %d (currently %d)"
585 			    "\n", amr->amr_dv.dv_xname, AMR_MAX_UNITS,
586 			    amr->amr_numdrives);
587 			amr->amr_numdrives = AMR_MAX_UNITS;
588 		} else
589 			amr->amr_numdrives = aex->ae_numldrives;
590 
591 		for (i = 0; i < amr->amr_numdrives; i++) {
592 			amr->amr_drive[i].al_size =
593 			    le32toh(aex->ae_drivesize[i]);
594 			amr->amr_drive[i].al_state = aex->ae_drivestate[i];
595 			amr->amr_drive[i].al_properties = aex->ae_driveprop[i];
596 		}
597 
598 		return (0);
599 	}
600 
601 	/*
602 	 * Try 8LD extended ENQUIRY to get the controller signature.  Once
603 	 * found, search for a product description.
604 	 */
605 	ae = amr_enquire(amr, AMR_CMD_EXT_ENQUIRY2, 0, 0, amr->amr_enqbuf);
606 	if (ae != NULL) {
607 		i = 0;
608 		sig = le32toh(ae->ae_signature);
609 
610 		while (i < sizeof(amr_typestr) / sizeof(amr_typestr[0])) {
611 			if (amr_typestr[i].at_sig == sig)
612 				break;
613 			i++;
614 		}
615 		if (i == sizeof(amr_typestr) / sizeof(amr_typestr[0])) {
616 			snprintf(sbuf, sizeof(sbuf),
617 			    "unknown ENQUIRY2 sig (0x%08x)", sig);
618 			prodstr = sbuf;
619 		} else
620 			prodstr = amr_typestr[i].at_str;
621 	} else {
622 		ae = amr_enquire(amr, AMR_CMD_ENQUIRY, 0, 0, amr->amr_enqbuf);
623 		if (ae == NULL) {
624 			aprint_error("%s: unsupported controller\n",
625 			    amr->amr_dv.dv_xname);
626 			return (-1);
627 		}
628 
629 		switch (PCI_PRODUCT(pa->pa_id)) {
630 		case PCI_PRODUCT_AMI_MEGARAID:
631 			prodstr = "Series 428";
632 			break;
633 		case PCI_PRODUCT_AMI_MEGARAID2:
634 			prodstr = "Series 434";
635 			break;
636 		default:
637 			snprintf(sbuf, sizeof(sbuf), "unknown PCI dev (0x%04x)",
638 			    PCI_PRODUCT(pa->pa_id));
639 			prodstr = sbuf;
640 			break;
641 		}
642 	}
643 
644 	/*
645 	 * HP NetRaid controllers have a special encoding of the firmware
646 	 * and BIOS versions.  The AMI version seems to have it as strings
647 	 * whereas the HP version does it with a leading uppercase character
648 	 * and two binary numbers.
649 	*/
650 	aa = &ae->ae_adapter;
651 
652 	if (aa->aa_firmware[2] >= 'A' && aa->aa_firmware[2] <= 'Z' &&
653 	    aa->aa_firmware[1] <  ' ' && aa->aa_firmware[0] <  ' ' &&
654 	    aa->aa_bios[2] >= 'A' && aa->aa_bios[2] <= 'Z' &&
655 	    aa->aa_bios[1] <  ' ' && aa->aa_bios[0] <  ' ') {
656 		if (le32toh(ae->ae_signature) == AMR_SIG_438) {
657 			/* The AMI 438 is a NetRaid 3si in HP-land. */
658 			prodstr = "HP NetRaid 3si";
659 		}
660 		ishp = 1;
661 	} else
662 		ishp = 0;
663 
664 	aprint_normal("<%s>\n", prodstr);
665 	if (intrstr != NULL)
666 		aprint_normal("%s: interrupting at %s\n", amr->amr_dv.dv_xname,
667 		    intrstr);
668 
669 	if (ishp)
670 		aprint_normal("%s: firmware <%c.%02d.%02d>, BIOS <%c.%02d.%02d>"
671 		    ", %dMB RAM\n", amr->amr_dv.dv_xname, aa->aa_firmware[2],
672 		     aa->aa_firmware[1], aa->aa_firmware[0], aa->aa_bios[2],
673 		     aa->aa_bios[1], aa->aa_bios[0], aa->aa_memorysize);
674 	else
675 		aprint_normal("%s: firmware <%.4s>, BIOS <%.4s>, %dMB RAM\n",
676 		    amr->amr_dv.dv_xname, aa->aa_firmware, aa->aa_bios,
677 		    aa->aa_memorysize);
678 
679 	amr->amr_maxqueuecnt = aa->aa_maxio;
680 
681 	/*
682 	 * Record state of logical drives.
683 	 */
684 	if (ae->ae_ldrv.al_numdrives > AMR_MAX_UNITS) {
685 		aprint_error("%s: adjust AMR_MAX_UNITS to %d (currently %d)\n",
686 		    amr->amr_dv.dv_xname, ae->ae_ldrv.al_numdrives,
687 		    AMR_MAX_UNITS);
688 		amr->amr_numdrives = AMR_MAX_UNITS;
689 	} else
690 		amr->amr_numdrives = ae->ae_ldrv.al_numdrives;
691 
692 	for (i = 0; i < AMR_MAX_UNITS; i++) {
693 		amr->amr_drive[i].al_size = le32toh(ae->ae_ldrv.al_size[i]);
694 		amr->amr_drive[i].al_state = ae->ae_ldrv.al_state[i];
695 		amr->amr_drive[i].al_properties = ae->ae_ldrv.al_properties[i];
696 	}
697 
698 	return (0);
699 }
700 
701 /*
702  * Flush the internal cache on each configured controller.  Called at
703  * shutdown time.
704  */
705 static void
706 amr_shutdown(void *cookie)
707 {
708         extern struct cfdriver amr_cd;
709 	struct amr_softc *amr;
710 	struct amr_ccb *ac;
711 	int i, rv, s;
712 
713 	for (i = 0; i < amr_cd.cd_ndevs; i++) {
714 		if ((amr = device_lookup(&amr_cd, i)) == NULL)
715 			continue;
716 
717 		if ((rv = amr_ccb_alloc(amr, &ac)) == 0) {
718 			ac->ac_cmd.mb_command = AMR_CMD_FLUSH;
719 			s = splbio();
720 			rv = amr_ccb_poll(amr, ac, 30000);
721 			splx(s);
722 			amr_ccb_free(amr, ac);
723 		}
724 		if (rv != 0)
725 			printf("%s: unable to flush cache (%d)\n",
726 			    amr->amr_dv.dv_xname, rv);
727 	}
728 }
729 
730 /*
731  * Interrupt service routine.
732  */
733 static int
734 amr_intr(void *cookie)
735 {
736 	struct amr_softc *amr;
737 	struct amr_ccb *ac;
738 	struct amr_mailbox_resp mbox;
739 	u_int i, forus, idx;
740 
741 	amr = cookie;
742 	forus = 0;
743 
744 	while ((*amr->amr_get_work)(amr, &mbox) == 0) {
745 		/* Iterate over completed commands in this result. */
746 		for (i = 0; i < mbox.mb_nstatus; i++) {
747 			idx = mbox.mb_completed[i] - 1;
748 			ac = amr->amr_ccbs + idx;
749 
750 			if (idx >= amr->amr_maxqueuecnt) {
751 				printf("%s: bad status (bogus ID: %u=%u)\n",
752 				    amr->amr_dv.dv_xname, i, idx);
753 				continue;
754 			}
755 
756 			if ((ac->ac_flags & AC_ACTIVE) == 0) {
757 				printf("%s: bad status (not active; 0x04%x)\n",
758 				    amr->amr_dv.dv_xname, ac->ac_flags);
759 				continue;
760 			}
761 
762 			ac->ac_status = mbox.mb_status;
763 			ac->ac_flags = (ac->ac_flags & ~AC_ACTIVE) |
764 			    AC_COMPLETE;
765 			TAILQ_REMOVE(&amr->amr_ccb_active, ac, ac_chain.tailq);
766 
767 			if ((ac->ac_flags & AC_MOAN) != 0)
768 				printf("%s: ccb %d completed\n",
769 				    amr->amr_dv.dv_xname, ac->ac_ident);
770 
771 			/* Pass notification to upper layers. */
772 			if (ac->ac_handler != NULL)
773 				(*ac->ac_handler)(ac);
774 			else
775 				wakeup(ac);
776 		}
777 		forus = 1;
778 	}
779 
780 	if (forus)
781 		amr_ccb_enqueue(amr, NULL);
782 
783 	return (forus);
784 }
785 
786 /*
787  * Create the watchdog thread.
788  */
789 static void
790 amr_thread_create(void *cookie)
791 {
792 	struct amr_softc *amr;
793 	int rv;
794 
795 	amr = cookie;
796 
797 	if ((amr->amr_flags & AMRF_THREAD_EXIT) != 0) {
798 		amr->amr_flags ^= AMRF_THREAD_EXIT;
799 		wakeup(&amr->amr_flags);
800 		return;
801 	}
802 
803 	rv = kthread_create1(amr_thread, amr, &amr->amr_thread, "%s",
804 	    amr->amr_dv.dv_xname);
805  	if (rv != 0)
806 		aprint_error("%s: unable to create thread (%d)",
807  		    amr->amr_dv.dv_xname, rv);
808  	else
809  		amr->amr_flags |= AMRF_THREAD;
810 }
811 
812 /*
813  * Watchdog thread.
814  */
815 static void
816 amr_thread(void *cookie)
817 {
818 	struct amr_softc *amr;
819 	struct amr_ccb *ac;
820 	struct amr_logdrive *al;
821 	struct amr_enquiry *ae;
822 	time_t curtime;
823 	int rv, i, s;
824 
825 	amr = cookie;
826 	ae = amr->amr_enqbuf;
827 
828 	for (;;) {
829 		tsleep(amr_thread, PWAIT, "amrwdog", AMR_WDOG_TICKS);
830 
831 		if ((amr->amr_flags & AMRF_THREAD_EXIT) != 0) {
832 			amr->amr_flags ^= AMRF_THREAD_EXIT;
833 			wakeup(&amr->amr_flags);
834 			kthread_exit(0);
835 		}
836 
837 		s = splbio();
838 		amr_intr(cookie);
839 		curtime = (time_t)mono_time.tv_sec;
840 		ac = TAILQ_FIRST(&amr->amr_ccb_active);
841 		while (ac != NULL) {
842 			if (ac->ac_start_time + AMR_TIMEOUT > curtime)
843 				break;
844 			if ((ac->ac_flags & AC_MOAN) == 0) {
845 				printf("%s: ccb %d timed out; mailbox:\n",
846 				    amr->amr_dv.dv_xname, ac->ac_ident);
847 				amr_ccb_dump(amr, ac);
848 				ac->ac_flags |= AC_MOAN;
849 			}
850 			ac = TAILQ_NEXT(ac, ac_chain.tailq);
851 		}
852 		splx(s);
853 
854 		if ((rv = amr_ccb_alloc(amr, &ac)) != 0) {
855 			printf("%s: ccb_alloc failed (%d)\n",
856  			    amr->amr_dv.dv_xname, rv);
857 			continue;
858 		}
859 
860 		ac->ac_cmd.mb_command = AMR_CMD_ENQUIRY;
861 
862 		rv = amr_ccb_map(amr, ac, amr->amr_enqbuf,
863 		    AMR_ENQUIRY_BUFSIZE, 0);
864 		if (rv != 0) {
865 			printf("%s: ccb_map failed (%d)\n",
866  			    amr->amr_dv.dv_xname, rv);
867 			amr_ccb_free(amr, ac);
868 			continue;
869 		}
870 
871 		rv = amr_ccb_wait(amr, ac);
872 		amr_ccb_unmap(amr, ac);
873 		if (rv != 0) {
874 			printf("%s: enquiry failed (st=%d)\n",
875  			    amr->amr_dv.dv_xname, ac->ac_status);
876 			continue;
877 		}
878 		amr_ccb_free(amr, ac);
879 
880 		al = amr->amr_drive;
881 		for (i = 0; i < AMR_MAX_UNITS; i++, al++) {
882 			if (al->al_dv == NULL)
883 				continue;
884 			if (al->al_state == ae->ae_ldrv.al_state[i])
885 				continue;
886 
887 			printf("%s: state changed: %s -> %s\n",
888 			    al->al_dv->dv_xname,
889 			    amr_drive_state(al->al_state, NULL),
890 			    amr_drive_state(ae->ae_ldrv.al_state[i], NULL));
891 
892 			al->al_state = ae->ae_ldrv.al_state[i];
893 		}
894 	}
895 }
896 
897 /*
898  * Return a text description of a logical drive's current state.
899  */
900 const char *
901 amr_drive_state(int state, int *happy)
902 {
903 	const char *str;
904 
905 	state = AMR_DRV_CURSTATE(state);
906 	if (state >= sizeof(amr_dstate) / sizeof(amr_dstate[0])) {
907 		if (happy)
908 			*happy = 1;
909 		str = "status unknown";
910 	} else {
911 		if (happy)
912 			*happy = amr_dstate[state].ds_happy;
913 		str = amr_dstate[state].ds_descr;
914 	}
915 
916 	return (str);
917 }
918 
919 /*
920  * Run a generic enquiry-style command.
921  */
922 static void *
923 amr_enquire(struct amr_softc *amr, u_int8_t cmd, u_int8_t cmdsub,
924 	    u_int8_t cmdqual, void *sbuf)
925 {
926 	struct amr_ccb *ac;
927 	u_int8_t *mb;
928 	int rv;
929 
930 	if (amr_ccb_alloc(amr, &ac) != 0)
931 		return (NULL);
932 
933 	/* Build the command proper. */
934 	mb = (u_int8_t *)&ac->ac_cmd;
935 	mb[0] = cmd;
936 	mb[2] = cmdsub;
937 	mb[3] = cmdqual;
938 
939 	rv = amr_ccb_map(amr, ac, sbuf, AMR_ENQUIRY_BUFSIZE, 0);
940 	if (rv == 0) {
941 		rv = amr_ccb_poll(amr, ac, 2000);
942 		amr_ccb_unmap(amr, ac);
943 	}
944 	amr_ccb_free(amr, ac);
945 
946 	return (rv ? NULL : sbuf);
947 }
948 
949 /*
950  * Allocate and initialise a CCB.
951  */
952 int
953 amr_ccb_alloc(struct amr_softc *amr, struct amr_ccb **acp)
954 {
955 	int s;
956 
957 	s = splbio();
958 	if ((*acp = SLIST_FIRST(&amr->amr_ccb_freelist)) == NULL) {
959 		splx(s);
960 		return (EAGAIN);
961 	}
962 	SLIST_REMOVE_HEAD(&amr->amr_ccb_freelist, ac_chain.slist);
963 	splx(s);
964 
965 	return (0);
966 }
967 
968 /*
969  * Free a CCB.
970  */
971 void
972 amr_ccb_free(struct amr_softc *amr, struct amr_ccb *ac)
973 {
974 	int s;
975 
976 	memset(&ac->ac_cmd, 0, sizeof(ac->ac_cmd));
977 	ac->ac_cmd.mb_ident = ac->ac_ident + 1;
978 	ac->ac_cmd.mb_busy = 1;
979 	ac->ac_handler = NULL;
980 	ac->ac_flags = 0;
981 
982 	s = splbio();
983 	SLIST_INSERT_HEAD(&amr->amr_ccb_freelist, ac, ac_chain.slist);
984 	splx(s);
985 }
986 
987 /*
988  * If a CCB is specified, enqueue it.  Pull CCBs off the software queue in
989  * the order that they were enqueued and try to submit their command blocks
990  * to the controller for execution.
991  */
992 void
993 amr_ccb_enqueue(struct amr_softc *amr, struct amr_ccb *ac)
994 {
995 	int s;
996 
997 	s = splbio();
998 
999 	if (ac != NULL)
1000 		SIMPLEQ_INSERT_TAIL(&amr->amr_ccb_queue, ac, ac_chain.simpleq);
1001 
1002 	while ((ac = SIMPLEQ_FIRST(&amr->amr_ccb_queue)) != NULL) {
1003 		if ((*amr->amr_submit)(amr, ac) != 0)
1004 			break;
1005 		SIMPLEQ_REMOVE_HEAD(&amr->amr_ccb_queue, ac_chain.simpleq);
1006 		TAILQ_INSERT_TAIL(&amr->amr_ccb_active, ac, ac_chain.tailq);
1007 	}
1008 
1009 	splx(s);
1010 }
1011 
1012 /*
1013  * Map the specified CCB's data buffer onto the bus, and fill the
1014  * scatter-gather list.
1015  */
1016 int
1017 amr_ccb_map(struct amr_softc *amr, struct amr_ccb *ac, void *data, int size,
1018 	    int out)
1019 {
1020 	struct amr_sgentry *sge;
1021 	struct amr_mailbox_cmd *mb;
1022 	int nsegs, i, rv, sgloff;
1023 	bus_dmamap_t xfer;
1024 
1025 	xfer = ac->ac_xfer_map;
1026 
1027 	rv = bus_dmamap_load(amr->amr_dmat, xfer, data, size, NULL,
1028 	    BUS_DMA_NOWAIT);
1029 	if (rv != 0)
1030 		return (rv);
1031 
1032 	mb = &ac->ac_cmd;
1033 	ac->ac_xfer_size = size;
1034 	ac->ac_flags |= (out ? AC_XFER_OUT : AC_XFER_IN);
1035 	sgloff = AMR_SGL_SIZE * ac->ac_ident;
1036 
1037 	/* We don't need to use a scatter/gather list for just 1 segment. */
1038 	nsegs = xfer->dm_nsegs;
1039 	if (nsegs == 1) {
1040 		mb->mb_nsgelem = 0;
1041 		mb->mb_physaddr = htole32(xfer->dm_segs[0].ds_addr);
1042 		ac->ac_flags |= AC_NOSGL;
1043 	} else {
1044 		mb->mb_nsgelem = nsegs;
1045 		mb->mb_physaddr = htole32(amr->amr_sgls_paddr + sgloff);
1046 
1047 		sge = (struct amr_sgentry *)((caddr_t)amr->amr_sgls + sgloff);
1048 		for (i = 0; i < nsegs; i++, sge++) {
1049 			sge->sge_addr = htole32(xfer->dm_segs[i].ds_addr);
1050 			sge->sge_count = htole32(xfer->dm_segs[i].ds_len);
1051 		}
1052 	}
1053 
1054 	bus_dmamap_sync(amr->amr_dmat, xfer, 0, ac->ac_xfer_size,
1055 	    out ? BUS_DMASYNC_PREWRITE : BUS_DMASYNC_PREREAD);
1056 
1057 	if ((ac->ac_flags & AC_NOSGL) == 0)
1058 		bus_dmamap_sync(amr->amr_dmat, amr->amr_dmamap, sgloff,
1059 		    AMR_SGL_SIZE, BUS_DMASYNC_PREWRITE);
1060 
1061 	return (0);
1062 }
1063 
1064 /*
1065  * Unmap the specified CCB's data buffer.
1066  */
1067 void
1068 amr_ccb_unmap(struct amr_softc *amr, struct amr_ccb *ac)
1069 {
1070 
1071 	if ((ac->ac_flags & AC_NOSGL) == 0)
1072 		bus_dmamap_sync(amr->amr_dmat, amr->amr_dmamap,
1073 		    AMR_SGL_SIZE * ac->ac_ident, AMR_SGL_SIZE,
1074 		    BUS_DMASYNC_POSTWRITE);
1075 	bus_dmamap_sync(amr->amr_dmat, ac->ac_xfer_map, 0, ac->ac_xfer_size,
1076 	    (ac->ac_flags & AC_XFER_IN) != 0 ?
1077 	    BUS_DMASYNC_POSTREAD : BUS_DMASYNC_POSTWRITE);
1078 	bus_dmamap_unload(amr->amr_dmat, ac->ac_xfer_map);
1079 }
1080 
1081 /*
1082  * Submit a command to the controller and poll on completion.  Return
1083  * non-zero on timeout or error.  Must be called with interrupts blocked.
1084  */
1085 int
1086 amr_ccb_poll(struct amr_softc *amr, struct amr_ccb *ac, int timo)
1087 {
1088 	int rv;
1089 
1090 	if ((rv = (*amr->amr_submit)(amr, ac)) != 0)
1091 		return (rv);
1092 	TAILQ_INSERT_TAIL(&amr->amr_ccb_active, ac, ac_chain.tailq);
1093 
1094 	for (timo *= 10; timo != 0; timo--) {
1095 		amr_intr(amr);
1096 		if ((ac->ac_flags & AC_COMPLETE) != 0)
1097 			break;
1098 		DELAY(100);
1099 	}
1100 
1101 	return (timo == 0 || ac->ac_status != 0 ? EIO : 0);
1102 }
1103 
1104 /*
1105  * Submit a command to the controller and sleep on completion.  Return
1106  * non-zero on error.
1107  */
1108 int
1109 amr_ccb_wait(struct amr_softc *amr, struct amr_ccb *ac)
1110 {
1111 	int s;
1112 
1113 	s = splbio();
1114 	amr_ccb_enqueue(amr, ac);
1115 	tsleep(ac, PRIBIO, "amrcmd", 0);
1116 	splx(s);
1117 
1118 	return (ac->ac_status != 0 ? EIO : 0);
1119 }
1120 
1121 #if 0
1122 /*
1123  * Wait for the mailbox to become available.
1124  */
1125 static int
1126 amr_mbox_wait(struct amr_softc *amr)
1127 {
1128 	int timo;
1129 
1130 	for (timo = 10000; timo != 0; timo--) {
1131 		bus_dmamap_sync(amr->amr_dmat, amr->amr_dmamap, 0,
1132 		    sizeof(struct amr_mailbox), BUS_DMASYNC_POSTREAD);
1133 		if (amr->amr_mbox->mb_cmd.mb_busy == 0)
1134 			break;
1135 		DELAY(100);
1136 	}
1137 
1138 	if (timo == 0)
1139 		printf("%s: controller wedged\n", amr->amr_dv.dv_xname);
1140 
1141 	return (timo != 0 ? 0 : EAGAIN);
1142 }
1143 #endif
1144 
1145 /*
1146  * Tell the controller that the mailbox contains a valid command.  Must be
1147  * called with interrupts blocked.
1148  */
1149 static int
1150 amr_quartz_submit(struct amr_softc *amr, struct amr_ccb *ac)
1151 {
1152 	u_int32_t v;
1153 
1154 	amr->amr_mbox->mb_poll = 0;
1155 	amr->amr_mbox->mb_ack = 0;
1156 	bus_dmamap_sync(amr->amr_dmat, amr->amr_dmamap, 0,
1157 	    sizeof(struct amr_mailbox), BUS_DMASYNC_PREWRITE);
1158 	bus_dmamap_sync(amr->amr_dmat, amr->amr_dmamap, 0,
1159 	    sizeof(struct amr_mailbox), BUS_DMASYNC_POSTREAD);
1160 	if (amr->amr_mbox->mb_cmd.mb_busy != 0)
1161 		return (EAGAIN);
1162 
1163 	v = amr_inl(amr, AMR_QREG_IDB);
1164 	if ((v & AMR_QIDB_SUBMIT) != 0) {
1165 		amr->amr_mbox->mb_cmd.mb_busy = 0;
1166 		bus_dmamap_sync(amr->amr_dmat, amr->amr_dmamap, 0,
1167 		    sizeof(struct amr_mailbox), BUS_DMASYNC_PREWRITE);
1168 		bus_dmamap_sync(amr->amr_dmat, amr->amr_dmamap, 0,
1169 		    sizeof(struct amr_mailbox), BUS_DMASYNC_PREREAD);
1170 		return (EAGAIN);
1171 	}
1172 
1173 	amr->amr_mbox->mb_segment = 0;
1174 	memcpy(&amr->amr_mbox->mb_cmd, &ac->ac_cmd, sizeof(ac->ac_cmd));
1175 	bus_dmamap_sync(amr->amr_dmat, amr->amr_dmamap, 0,
1176 	    sizeof(struct amr_mailbox), BUS_DMASYNC_PREWRITE);
1177 
1178 	ac->ac_start_time = (time_t)mono_time.tv_sec;
1179 	ac->ac_flags |= AC_ACTIVE;
1180 	amr_outl(amr, AMR_QREG_IDB,
1181 	    (amr->amr_mbox_paddr + 16) | AMR_QIDB_SUBMIT);
1182 	return (0);
1183 }
1184 
1185 static int
1186 amr_std_submit(struct amr_softc *amr, struct amr_ccb *ac)
1187 {
1188 
1189 	amr->amr_mbox->mb_poll = 0;
1190 	amr->amr_mbox->mb_ack = 0;
1191 	bus_dmamap_sync(amr->amr_dmat, amr->amr_dmamap, 0,
1192 	    sizeof(struct amr_mailbox), BUS_DMASYNC_PREWRITE);
1193 	bus_dmamap_sync(amr->amr_dmat, amr->amr_dmamap, 0,
1194 	    sizeof(struct amr_mailbox), BUS_DMASYNC_POSTREAD);
1195 	if (amr->amr_mbox->mb_cmd.mb_busy != 0)
1196 		return (EAGAIN);
1197 
1198 	if ((amr_inb(amr, AMR_SREG_MBOX_BUSY) & AMR_SMBOX_BUSY_FLAG) != 0) {
1199 		amr->amr_mbox->mb_cmd.mb_busy = 0;
1200 		bus_dmamap_sync(amr->amr_dmat, amr->amr_dmamap, 0,
1201 		    sizeof(struct amr_mailbox), BUS_DMASYNC_PREWRITE);
1202 		bus_dmamap_sync(amr->amr_dmat, amr->amr_dmamap, 0,
1203 		    sizeof(struct amr_mailbox), BUS_DMASYNC_PREREAD);
1204 		return (EAGAIN);
1205 	}
1206 
1207 	amr->amr_mbox->mb_segment = 0;
1208 	memcpy(&amr->amr_mbox->mb_cmd, &ac->ac_cmd, sizeof(ac->ac_cmd));
1209 	bus_dmamap_sync(amr->amr_dmat, amr->amr_dmamap, 0,
1210 	    sizeof(struct amr_mailbox), BUS_DMASYNC_PREWRITE);
1211 
1212 	ac->ac_start_time = (time_t)mono_time.tv_sec;
1213 	ac->ac_flags |= AC_ACTIVE;
1214 	amr_outb(amr, AMR_SREG_CMD, AMR_SCMD_POST);
1215 	return (0);
1216 }
1217 
1218 /*
1219  * Claim any work that the controller has completed; acknowledge completion,
1220  * save details of the completion in (mbsave).  Must be called with
1221  * interrupts blocked.
1222  */
1223 static int
1224 amr_quartz_get_work(struct amr_softc *amr, struct amr_mailbox_resp *mbsave)
1225 {
1226 
1227 	/* Work waiting for us? */
1228 	if (amr_inl(amr, AMR_QREG_ODB) != AMR_QODB_READY)
1229 		return (-1);
1230 
1231 	bus_dmamap_sync(amr->amr_dmat, amr->amr_dmamap, 0,
1232 	    sizeof(struct amr_mailbox), BUS_DMASYNC_POSTREAD);
1233 
1234 	/* Save the mailbox, which contains a list of completed commands. */
1235 	memcpy(mbsave, &amr->amr_mbox->mb_resp, sizeof(*mbsave));
1236 
1237 	bus_dmamap_sync(amr->amr_dmat, amr->amr_dmamap, 0,
1238 	    sizeof(struct amr_mailbox), BUS_DMASYNC_PREREAD);
1239 
1240 	/* Ack the interrupt and mailbox transfer. */
1241 	amr_outl(amr, AMR_QREG_ODB, AMR_QODB_READY);
1242 	amr_outl(amr, AMR_QREG_IDB, (amr->amr_mbox_paddr+16) | AMR_QIDB_ACK);
1243 
1244 	/*
1245 	 * This waits for the controller to notice that we've taken the
1246 	 * command from it.  It's very inefficient, and we shouldn't do it,
1247 	 * but if we remove this code, we stop completing commands under
1248 	 * load.
1249 	 *
1250 	 * Peter J says we shouldn't do this.  The documentation says we
1251 	 * should.  Who is right?
1252 	 */
1253 	while ((amr_inl(amr, AMR_QREG_IDB) & AMR_QIDB_ACK) != 0)
1254 		DELAY(10);
1255 
1256 	return (0);
1257 }
1258 
1259 static int
1260 amr_std_get_work(struct amr_softc *amr, struct amr_mailbox_resp *mbsave)
1261 {
1262 	u_int8_t istat;
1263 
1264 	/* Check for valid interrupt status. */
1265 	if (((istat = amr_inb(amr, AMR_SREG_INTR)) & AMR_SINTR_VALID) == 0)
1266 		return (-1);
1267 
1268 	/* Ack the interrupt. */
1269 	amr_outb(amr, AMR_SREG_INTR, istat);
1270 
1271 	bus_dmamap_sync(amr->amr_dmat, amr->amr_dmamap, 0,
1272 	    sizeof(struct amr_mailbox), BUS_DMASYNC_POSTREAD);
1273 
1274 	/* Save mailbox, which contains a list of completed commands. */
1275 	memcpy(mbsave, &amr->amr_mbox->mb_resp, sizeof(*mbsave));
1276 
1277 	bus_dmamap_sync(amr->amr_dmat, amr->amr_dmamap, 0,
1278 	    sizeof(struct amr_mailbox), BUS_DMASYNC_PREREAD);
1279 
1280 	/* Ack mailbox transfer. */
1281 	amr_outb(amr, AMR_SREG_CMD, AMR_SCMD_ACKINTR);
1282 
1283 	return (0);
1284 }
1285 
1286 static void
1287 amr_ccb_dump(struct amr_softc *amr, struct amr_ccb *ac)
1288 {
1289 	int i;
1290 
1291 	printf("%s: ", amr->amr_dv.dv_xname);
1292 	for (i = 0; i < 4; i++)
1293 		printf("%08x ", ((u_int32_t *)&ac->ac_cmd)[i]);
1294 	printf("\n");
1295 }
1296