amr.c revision 1.8 1 1.8 thorpej /* $NetBSD: amr.c,v 1.8 2003/01/31 00:07:40 thorpej Exp $ */
2 1.1 ad
3 1.1 ad /*-
4 1.1 ad * Copyright (c) 2002 The NetBSD Foundation, Inc.
5 1.1 ad * All rights reserved.
6 1.1 ad *
7 1.1 ad * This code is derived from software contributed to The NetBSD Foundation
8 1.1 ad * by Andrew Doran.
9 1.1 ad *
10 1.1 ad * Redistribution and use in source and binary forms, with or without
11 1.1 ad * modification, are permitted provided that the following conditions
12 1.1 ad * are met:
13 1.1 ad * 1. Redistributions of source code must retain the above copyright
14 1.1 ad * notice, this list of conditions and the following disclaimer.
15 1.1 ad * 2. Redistributions in binary form must reproduce the above copyright
16 1.1 ad * notice, this list of conditions and the following disclaimer in the
17 1.1 ad * documentation and/or other materials provided with the distribution.
18 1.1 ad * 3. All advertising materials mentioning features or use of this software
19 1.1 ad * must display the following acknowledgement:
20 1.1 ad * This product includes software developed by the NetBSD
21 1.1 ad * Foundation, Inc. and its contributors.
22 1.1 ad * 4. Neither the name of The NetBSD Foundation nor the names of its
23 1.1 ad * contributors may be used to endorse or promote products derived
24 1.1 ad * from this software without specific prior written permission.
25 1.1 ad *
26 1.1 ad * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 1.1 ad * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 1.1 ad * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 1.1 ad * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 1.1 ad * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 1.1 ad * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 1.1 ad * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 1.1 ad * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 1.1 ad * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 1.1 ad * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 1.1 ad * POSSIBILITY OF SUCH DAMAGE.
37 1.1 ad */
38 1.1 ad
39 1.1 ad /*-
40 1.1 ad * Copyright (c) 1999,2000 Michael Smith
41 1.1 ad * Copyright (c) 2000 BSDi
42 1.1 ad * All rights reserved.
43 1.1 ad *
44 1.1 ad * Redistribution and use in source and binary forms, with or without
45 1.1 ad * modification, are permitted provided that the following conditions
46 1.1 ad * are met:
47 1.1 ad * 1. Redistributions of source code must retain the above copyright
48 1.1 ad * notice, this list of conditions and the following disclaimer.
49 1.1 ad * 2. Redistributions in binary form must reproduce the above copyright
50 1.1 ad * notice, this list of conditions and the following disclaimer in the
51 1.1 ad * documentation and/or other materials provided with the distribution.
52 1.1 ad *
53 1.1 ad * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
54 1.1 ad * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
55 1.1 ad * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
56 1.1 ad * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
57 1.1 ad * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
58 1.1 ad * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
59 1.1 ad * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
60 1.1 ad * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
61 1.1 ad * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
62 1.1 ad * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
63 1.1 ad * SUCH DAMAGE.
64 1.1 ad *
65 1.1 ad * from FreeBSD: amr_pci.c,v 1.5 2000/08/30 07:52:40 msmith Exp
66 1.1 ad * from FreeBSD: amr.c,v 1.16 2000/08/30 07:52:40 msmith Exp
67 1.1 ad */
68 1.1 ad
69 1.1 ad /*
70 1.1 ad * Driver for AMI RAID controllers.
71 1.1 ad */
72 1.1 ad
73 1.1 ad #include <sys/cdefs.h>
74 1.8 thorpej __KERNEL_RCSID(0, "$NetBSD: amr.c,v 1.8 2003/01/31 00:07:40 thorpej Exp $");
75 1.1 ad
76 1.1 ad #include <sys/param.h>
77 1.1 ad #include <sys/systm.h>
78 1.1 ad #include <sys/kernel.h>
79 1.1 ad #include <sys/device.h>
80 1.1 ad #include <sys/queue.h>
81 1.1 ad #include <sys/proc.h>
82 1.1 ad #include <sys/buf.h>
83 1.1 ad #include <sys/malloc.h>
84 1.1 ad
85 1.1 ad #include <uvm/uvm_extern.h>
86 1.1 ad
87 1.1 ad #include <machine/endian.h>
88 1.1 ad #include <machine/bus.h>
89 1.1 ad
90 1.1 ad #include <dev/pci/pcidevs.h>
91 1.1 ad #include <dev/pci/pcivar.h>
92 1.1 ad #include <dev/pci/amrreg.h>
93 1.1 ad #include <dev/pci/amrvar.h>
94 1.1 ad
95 1.1 ad #if AMR_MAX_SEGS > 32
96 1.1 ad #error AMR_MAX_SEGS too high
97 1.1 ad #endif
98 1.1 ad
99 1.1 ad #define AMR_ENQUIRY_BUFSIZE 2048
100 1.1 ad #define AMR_SGL_SIZE (sizeof(struct amr_sgentry) * 32)
101 1.1 ad
102 1.1 ad void amr_attach(struct device *, struct device *, void *);
103 1.1 ad void *amr_enquire(struct amr_softc *, u_int8_t, u_int8_t, u_int8_t);
104 1.1 ad int amr_init(struct amr_softc *, const char *,
105 1.1 ad struct pci_attach_args *pa);
106 1.1 ad int amr_intr(void *);
107 1.1 ad int amr_match(struct device *, struct cfdata *, void *);
108 1.1 ad int amr_print(void *, const char *);
109 1.1 ad void amr_shutdown(void *);
110 1.1 ad int amr_submatch(struct device *, struct cfdata *, void *);
111 1.1 ad
112 1.1 ad int amr_mbox_wait(struct amr_softc *);
113 1.1 ad int amr_quartz_get_work(struct amr_softc *, struct amr_mailbox *);
114 1.1 ad int amr_quartz_submit(struct amr_softc *, struct amr_ccb *);
115 1.1 ad int amr_std_get_work(struct amr_softc *, struct amr_mailbox *);
116 1.1 ad int amr_std_submit(struct amr_softc *, struct amr_ccb *);
117 1.1 ad
118 1.1 ad static inline u_int8_t amr_inb(struct amr_softc *, int);
119 1.1 ad static inline u_int32_t amr_inl(struct amr_softc *, int);
120 1.1 ad static inline void amr_outb(struct amr_softc *, int, u_int8_t);
121 1.1 ad static inline void amr_outl(struct amr_softc *, int, u_int32_t);
122 1.1 ad
123 1.5 thorpej CFATTACH_DECL(amr, sizeof(struct amr_softc),
124 1.6 thorpej amr_match, amr_attach, NULL, NULL);
125 1.1 ad
126 1.1 ad #define AT_QUARTZ 0x01 /* `Quartz' chipset */
127 1.1 ad #define AT_SIG 0x02 /* Check for signature */
128 1.1 ad
129 1.1 ad struct amr_pci_type {
130 1.1 ad u_short apt_vendor;
131 1.1 ad u_short apt_product;
132 1.1 ad u_short apt_flags;
133 1.1 ad } static const amr_pci_type[] = {
134 1.1 ad { PCI_VENDOR_AMI, PCI_PRODUCT_AMI_MEGARAID, 0 },
135 1.1 ad { PCI_VENDOR_AMI, PCI_PRODUCT_AMI_MEGARAID2, 0 },
136 1.1 ad { PCI_VENDOR_AMI, PCI_PRODUCT_AMI_MEGARAID3, AT_QUARTZ },
137 1.1 ad { PCI_VENDOR_INTEL, PCI_PRODUCT_AMI_MEGARAID3, AT_QUARTZ | AT_SIG }
138 1.1 ad };
139 1.1 ad
140 1.1 ad struct amr_typestr {
141 1.1 ad const char *at_str;
142 1.1 ad int at_sig;
143 1.1 ad } static const amr_typestr[] = {
144 1.1 ad { "Series 431", AMR_SIG_431 },
145 1.1 ad { "Series 438", AMR_SIG_438 },
146 1.1 ad { "Series 466", AMR_SIG_466 },
147 1.1 ad { "Series 467", AMR_SIG_467 },
148 1.1 ad { "Series 490", AMR_SIG_490 },
149 1.1 ad { "Series 762", AMR_SIG_762 },
150 1.1 ad { "HP NetRAID (T5)", AMR_SIG_T5 },
151 1.1 ad { "HP NetRAID (T7)", AMR_SIG_T7 },
152 1.1 ad };
153 1.1 ad
154 1.1 ad static void *amr_sdh;
155 1.1 ad
156 1.1 ad static inline u_int8_t
157 1.1 ad amr_inb(struct amr_softc *amr, int off)
158 1.1 ad {
159 1.1 ad
160 1.1 ad bus_space_barrier(amr->amr_iot, amr->amr_ioh, off, 1,
161 1.1 ad BUS_SPACE_BARRIER_WRITE | BUS_SPACE_BARRIER_READ);
162 1.1 ad return (bus_space_read_1(amr->amr_iot, amr->amr_ioh, off));
163 1.1 ad }
164 1.1 ad
165 1.1 ad static inline u_int32_t
166 1.1 ad amr_inl(struct amr_softc *amr, int off)
167 1.1 ad {
168 1.1 ad
169 1.1 ad bus_space_barrier(amr->amr_iot, amr->amr_ioh, off, 4,
170 1.1 ad BUS_SPACE_BARRIER_WRITE | BUS_SPACE_BARRIER_READ);
171 1.1 ad return (bus_space_read_4(amr->amr_iot, amr->amr_ioh, off));
172 1.1 ad }
173 1.1 ad
174 1.1 ad static inline void
175 1.1 ad amr_outb(struct amr_softc *amr, int off, u_int8_t val)
176 1.1 ad {
177 1.1 ad
178 1.1 ad bus_space_write_1(amr->amr_iot, amr->amr_ioh, off, val);
179 1.1 ad bus_space_barrier(amr->amr_iot, amr->amr_ioh, off, 1,
180 1.1 ad BUS_SPACE_BARRIER_WRITE);
181 1.1 ad }
182 1.1 ad
183 1.1 ad static inline void
184 1.1 ad amr_outl(struct amr_softc *amr, int off, u_int32_t val)
185 1.1 ad {
186 1.1 ad
187 1.1 ad bus_space_write_4(amr->amr_iot, amr->amr_ioh, off, val);
188 1.1 ad bus_space_barrier(amr->amr_iot, amr->amr_ioh, off, 4,
189 1.1 ad BUS_SPACE_BARRIER_WRITE);
190 1.1 ad }
191 1.1 ad
192 1.1 ad /*
193 1.1 ad * Match a supported device.
194 1.1 ad */
195 1.1 ad int
196 1.1 ad amr_match(struct device *parent, struct cfdata *match, void *aux)
197 1.1 ad {
198 1.1 ad struct pci_attach_args *pa;
199 1.1 ad pcireg_t s;
200 1.1 ad int i;
201 1.1 ad
202 1.1 ad pa = (struct pci_attach_args *)aux;
203 1.1 ad
204 1.1 ad /*
205 1.1 ad * Don't match the device if it's operating in I2O mode. In this
206 1.1 ad * case it should be handled by the `iop' driver.
207 1.1 ad */
208 1.1 ad if (PCI_CLASS(pa->pa_class) == PCI_CLASS_I2O)
209 1.1 ad return (0);
210 1.1 ad
211 1.1 ad for (i = 0; i < sizeof(amr_pci_type) / sizeof(amr_pci_type[0]); i++)
212 1.1 ad if (PCI_VENDOR(pa->pa_id) == amr_pci_type[i].apt_vendor &&
213 1.1 ad PCI_PRODUCT(pa->pa_id) == amr_pci_type[i].apt_product)
214 1.1 ad break;
215 1.1 ad
216 1.1 ad if (i == sizeof(amr_pci_type) / sizeof(amr_pci_type[0]))
217 1.1 ad return (0);
218 1.1 ad
219 1.1 ad if ((amr_pci_type[i].apt_flags & AT_SIG) == 0)
220 1.1 ad return (1);
221 1.1 ad
222 1.1 ad s = pci_conf_read(pa->pa_pc, pa->pa_tag, AMR_QUARTZ_SIG_REG) & 0xffff;
223 1.1 ad return (s == AMR_QUARTZ_SIG0 || s == AMR_QUARTZ_SIG1);
224 1.1 ad }
225 1.1 ad
226 1.1 ad /*
227 1.1 ad * Attach a supported device. XXX This doesn't fail gracefully, and may
228 1.1 ad * over-allocate resources.
229 1.1 ad */
230 1.1 ad void
231 1.1 ad amr_attach(struct device *parent, struct device *self, void *aux)
232 1.1 ad {
233 1.1 ad bus_space_tag_t memt, iot;
234 1.1 ad bus_space_handle_t memh, ioh;
235 1.1 ad struct pci_attach_args *pa;
236 1.1 ad struct amr_attach_args amra;
237 1.1 ad const struct amr_pci_type *apt;
238 1.1 ad struct amr_softc *amr;
239 1.1 ad pci_chipset_tag_t pc;
240 1.1 ad pci_intr_handle_t ih;
241 1.1 ad const char *intrstr;
242 1.1 ad pcireg_t reg;
243 1.1 ad int rseg, i, size, rv, memreg, ioreg;
244 1.1 ad bus_dma_segment_t seg;
245 1.1 ad struct amr_ccb *ac;
246 1.1 ad
247 1.8 thorpej aprint_naive(": RAID controller\n");
248 1.8 thorpej
249 1.1 ad amr = (struct amr_softc *)self;
250 1.1 ad pa = (struct pci_attach_args *)aux;
251 1.1 ad pc = pa->pa_pc;
252 1.1 ad
253 1.1 ad for (i = 0; i < sizeof(amr_pci_type) / sizeof(amr_pci_type[0]); i++)
254 1.1 ad if (PCI_VENDOR(pa->pa_id) == amr_pci_type[i].apt_vendor &&
255 1.1 ad PCI_PRODUCT(pa->pa_id) == amr_pci_type[i].apt_product)
256 1.1 ad break;
257 1.1 ad apt = amr_pci_type + i;
258 1.1 ad
259 1.1 ad memreg = ioreg = 0;
260 1.1 ad for (i = 0x10; i <= 0x14; i += 4) {
261 1.1 ad reg = pci_conf_read(pc, pa->pa_tag, i);
262 1.1 ad switch (PCI_MAPREG_TYPE(reg)) {
263 1.1 ad case PCI_MAPREG_TYPE_MEM:
264 1.1 ad if (PCI_MAPREG_MEM_SIZE(reg) != 0)
265 1.1 ad memreg = i;
266 1.1 ad break;
267 1.1 ad case PCI_MAPREG_TYPE_IO:
268 1.1 ad if (PCI_MAPREG_IO_SIZE(reg) != 0)
269 1.1 ad ioreg = i;
270 1.1 ad break;
271 1.1 ad }
272 1.1 ad }
273 1.1 ad
274 1.1 ad if (memreg != 0)
275 1.1 ad if (pci_mapreg_map(pa, memreg, PCI_MAPREG_TYPE_MEM, 0,
276 1.1 ad &memt, &memh, NULL, NULL))
277 1.1 ad memreg = 0;
278 1.1 ad if (ioreg != 0)
279 1.1 ad if (pci_mapreg_map(pa, ioreg, PCI_MAPREG_TYPE_IO, 0,
280 1.1 ad &iot, &ioh, NULL, NULL))
281 1.1 ad ioreg = 0;
282 1.1 ad
283 1.1 ad if (memreg) {
284 1.1 ad amr->amr_iot = memt;
285 1.1 ad amr->amr_ioh = memh;
286 1.1 ad } else if (ioreg) {
287 1.1 ad amr->amr_iot = iot;
288 1.1 ad amr->amr_ioh = ioh;
289 1.1 ad } else {
290 1.8 thorpej aprint_error("can't map control registers\n");
291 1.1 ad return;
292 1.1 ad }
293 1.1 ad
294 1.1 ad amr->amr_dmat = pa->pa_dmat;
295 1.1 ad
296 1.1 ad /* Enable the device. */
297 1.1 ad reg = pci_conf_read(pa->pa_pc, pa->pa_tag, PCI_COMMAND_STATUS_REG);
298 1.1 ad pci_conf_write(pa->pa_pc, pa->pa_tag, PCI_COMMAND_STATUS_REG,
299 1.1 ad reg | PCI_COMMAND_MASTER_ENABLE);
300 1.1 ad
301 1.1 ad /* Map and establish the interrupt. */
302 1.1 ad if (pci_intr_map(pa, &ih)) {
303 1.8 thorpej aprint_error("can't map interrupt\n");
304 1.1 ad return;
305 1.1 ad }
306 1.1 ad intrstr = pci_intr_string(pc, ih);
307 1.1 ad amr->amr_ih = pci_intr_establish(pc, ih, IPL_BIO, amr_intr, amr);
308 1.1 ad if (amr->amr_ih == NULL) {
309 1.8 thorpej aprint_error("can't establish interrupt");
310 1.1 ad if (intrstr != NULL)
311 1.8 thorpej aprint_normal(" at %s", intrstr);
312 1.8 thorpej aprint_normal("\n");
313 1.1 ad return;
314 1.1 ad }
315 1.1 ad
316 1.1 ad /*
317 1.1 ad * Allocate space for the mailbox and S/G lists. Some controllers
318 1.1 ad * don't like S/G lists to be located below 0x2000, so we allocate
319 1.1 ad * enough slop to enable us to compensate.
320 1.1 ad *
321 1.1 ad * The standard mailbox structure needs to be aligned on a 16-byte
322 1.1 ad * boundary. The 64-bit mailbox has one extra field, 4 bytes in
323 1.1 ad * size, which preceeds the standard mailbox.
324 1.1 ad */
325 1.1 ad size = AMR_SGL_SIZE * AMR_MAX_CMDS + 0x2000;
326 1.1 ad
327 1.1 ad if ((rv = bus_dmamem_alloc(amr->amr_dmat, size, PAGE_SIZE, NULL, &seg,
328 1.1 ad 1, &rseg, BUS_DMA_NOWAIT)) != 0) {
329 1.8 thorpej aprint_error("%s: unable to allocate buffer, rv = %d\n",
330 1.1 ad amr->amr_dv.dv_xname, rv);
331 1.1 ad return;
332 1.1 ad }
333 1.1 ad
334 1.1 ad if ((rv = bus_dmamem_map(amr->amr_dmat, &seg, rseg, size,
335 1.1 ad (caddr_t *)&amr->amr_mbox,
336 1.1 ad BUS_DMA_NOWAIT | BUS_DMA_COHERENT)) != 0) {
337 1.8 thorpej aprint_error("%s: unable to map buffer, rv = %d\n",
338 1.1 ad amr->amr_dv.dv_xname, rv);
339 1.1 ad return;
340 1.1 ad }
341 1.1 ad
342 1.1 ad if ((rv = bus_dmamap_create(amr->amr_dmat, size, 1, size, 0,
343 1.1 ad BUS_DMA_NOWAIT, &amr->amr_dmamap)) != 0) {
344 1.8 thorpej aprint_error("%s: unable to create buffer DMA map, rv = %d\n",
345 1.1 ad amr->amr_dv.dv_xname, rv);
346 1.1 ad return;
347 1.1 ad }
348 1.1 ad
349 1.1 ad if ((rv = bus_dmamap_load(amr->amr_dmat, amr->amr_dmamap,
350 1.1 ad amr->amr_mbox, size, NULL, BUS_DMA_NOWAIT)) != 0) {
351 1.8 thorpej aprint_error("%s: unable to load buffer DMA map, rv = %d\n",
352 1.1 ad amr->amr_dv.dv_xname, rv);
353 1.1 ad return;
354 1.1 ad }
355 1.1 ad
356 1.1 ad memset(amr->amr_mbox, 0, size);
357 1.1 ad
358 1.1 ad amr->amr_mbox_paddr = amr->amr_dmamap->dm_segs[0].ds_addr + 16;
359 1.1 ad amr->amr_sgls_paddr = (amr->amr_mbox_paddr + 0x1fff) & ~0x1fff;
360 1.1 ad amr->amr_sgls = (struct amr_sgentry *)((caddr_t)amr->amr_mbox +
361 1.1 ad amr->amr_sgls_paddr - amr->amr_dmamap->dm_segs[0].ds_addr);
362 1.1 ad amr->amr_mbox = (struct amr_mailbox *)((caddr_t)amr->amr_mbox + 16);
363 1.1 ad
364 1.1 ad /*
365 1.1 ad * Allocate and initalise the command control blocks.
366 1.1 ad */
367 1.1 ad ac = malloc(sizeof(*ac) * AMR_MAX_CMDS, M_DEVBUF, M_NOWAIT | M_ZERO);
368 1.1 ad amr->amr_ccbs = ac;
369 1.1 ad SLIST_INIT(&amr->amr_ccb_freelist);
370 1.1 ad
371 1.1 ad for (i = 0; i < AMR_MAX_CMDS; i++, ac++) {
372 1.1 ad rv = bus_dmamap_create(amr->amr_dmat, AMR_MAX_XFER,
373 1.1 ad AMR_MAX_SEGS, AMR_MAX_XFER, 0,
374 1.1 ad BUS_DMA_NOWAIT | BUS_DMA_ALLOCNOW,
375 1.1 ad &ac->ac_xfer_map);
376 1.1 ad if (rv != 0)
377 1.1 ad break;
378 1.1 ad
379 1.1 ad ac->ac_ident = i;
380 1.1 ad SLIST_INSERT_HEAD(&amr->amr_ccb_freelist, ac, ac_chain.slist);
381 1.1 ad }
382 1.1 ad if (i != AMR_MAX_CMDS)
383 1.8 thorpej aprint_error("%s: %d/%d CCBs created\n", amr->amr_dv.dv_xname,
384 1.1 ad i, AMR_MAX_CMDS);
385 1.1 ad
386 1.1 ad /*
387 1.1 ad * Take care of model-specific tasks.
388 1.1 ad */
389 1.1 ad if ((apt->apt_flags & AT_QUARTZ) != 0) {
390 1.1 ad amr->amr_submit = amr_quartz_submit;
391 1.1 ad amr->amr_get_work = amr_quartz_get_work;
392 1.1 ad } else {
393 1.1 ad amr->amr_submit = amr_std_submit;
394 1.1 ad amr->amr_get_work = amr_std_get_work;
395 1.1 ad
396 1.1 ad /* Notify the controller of the mailbox location. */
397 1.1 ad amr_outl(amr, AMR_SREG_MBOX, amr->amr_mbox_paddr);
398 1.1 ad amr_outb(amr, AMR_SREG_MBOX_ENABLE, AMR_SMBOX_ENABLE_ADDR);
399 1.1 ad
400 1.1 ad /* Clear outstanding interrupts and enable interrupts. */
401 1.1 ad amr_outb(amr, AMR_SREG_CMD, AMR_SCMD_ACKINTR);
402 1.1 ad amr_outb(amr, AMR_SREG_TOGL,
403 1.1 ad amr_inb(amr, AMR_SREG_TOGL) | AMR_STOGL_ENABLE);
404 1.1 ad }
405 1.1 ad
406 1.1 ad /*
407 1.1 ad * Retrieve parameters, and tell the world about us.
408 1.1 ad */
409 1.1 ad amr->amr_maxqueuecnt = i;
410 1.8 thorpej aprint_normal(": AMI RAID ");
411 1.1 ad if (amr_init(amr, intrstr, pa) != 0)
412 1.1 ad return;
413 1.1 ad
414 1.1 ad /*
415 1.1 ad * Cap the maximum number of outstanding commands. AMI's Linux
416 1.1 ad * driver doesn't trust the controller's reported value, and lockups
417 1.1 ad * have been seen when we do.
418 1.1 ad */
419 1.1 ad amr->amr_maxqueuecnt = min(amr->amr_maxqueuecnt, AMR_MAX_CMDS);
420 1.1 ad if (amr->amr_maxqueuecnt > i)
421 1.1 ad amr->amr_maxqueuecnt = i;
422 1.1 ad
423 1.1 ad /* Set our `shutdownhook' before we start any device activity. */
424 1.1 ad if (amr_sdh == NULL)
425 1.1 ad amr_sdh = shutdownhook_establish(amr_shutdown, NULL);
426 1.1 ad
427 1.1 ad /* Attach sub-devices. */
428 1.1 ad for (i = 0; i < amr->amr_numdrives; i++) {
429 1.1 ad if (amr->amr_drive[i].al_size == 0)
430 1.1 ad continue;
431 1.1 ad amra.amra_unit = i;
432 1.1 ad config_found_sm(&amr->amr_dv, &amra, amr_print, amr_submatch);
433 1.1 ad }
434 1.1 ad
435 1.1 ad SIMPLEQ_INIT(&amr->amr_ccb_queue);
436 1.1 ad }
437 1.1 ad
438 1.1 ad /*
439 1.1 ad * Print autoconfiguration message for a sub-device.
440 1.1 ad */
441 1.1 ad int
442 1.1 ad amr_print(void *aux, const char *pnp)
443 1.1 ad {
444 1.1 ad struct amr_attach_args *amra;
445 1.1 ad
446 1.1 ad amra = (struct amr_attach_args *)aux;
447 1.1 ad
448 1.1 ad if (pnp != NULL)
449 1.7 thorpej aprint_normal("block device at %s", pnp);
450 1.7 thorpej aprint_normal(" unit %d", amra->amra_unit);
451 1.1 ad return (UNCONF);
452 1.1 ad }
453 1.1 ad
454 1.1 ad /*
455 1.1 ad * Match a sub-device.
456 1.1 ad */
457 1.1 ad int
458 1.1 ad amr_submatch(struct device *parent, struct cfdata *cf, void *aux)
459 1.1 ad {
460 1.1 ad struct amr_attach_args *amra;
461 1.1 ad
462 1.1 ad amra = (struct amr_attach_args *)aux;
463 1.1 ad
464 1.1 ad if (cf->amracf_unit != AMRCF_UNIT_DEFAULT &&
465 1.1 ad cf->amracf_unit != amra->amra_unit)
466 1.1 ad return (0);
467 1.1 ad
468 1.3 thorpej return (config_match(parent, cf, aux));
469 1.1 ad }
470 1.1 ad
471 1.1 ad /*
472 1.1 ad * Retrieve operational parameters and describe the controller.
473 1.1 ad */
474 1.1 ad int
475 1.1 ad amr_init(struct amr_softc *amr, const char *intrstr,
476 1.1 ad struct pci_attach_args *pa)
477 1.1 ad {
478 1.1 ad struct amr_prodinfo *ap;
479 1.1 ad struct amr_enquiry *ae;
480 1.1 ad struct amr_enquiry3 *aex;
481 1.1 ad const char *prodstr;
482 1.1 ad u_int i, sig;
483 1.1 ad char buf[64];
484 1.1 ad
485 1.1 ad /*
486 1.1 ad * Try to get 40LD product info, which tells us what the card is
487 1.1 ad * labelled as.
488 1.1 ad */
489 1.1 ad ap = amr_enquire(amr, AMR_CMD_CONFIG, AMR_CONFIG_PRODUCT_INFO, 0);
490 1.1 ad if (ap != NULL) {
491 1.8 thorpej aprint_normal("<%.80s>\n", ap->ap_product);
492 1.1 ad if (intrstr != NULL)
493 1.8 thorpej aprint_normal("%s: interrupting at %s\n",
494 1.1 ad amr->amr_dv.dv_xname, intrstr);
495 1.8 thorpej aprint_normal("%s: firmware %.16s, BIOS %.16s, %dMB RAM\n",
496 1.1 ad amr->amr_dv.dv_xname, ap->ap_firmware, ap->ap_bios,
497 1.1 ad le16toh(ap->ap_memsize));
498 1.1 ad
499 1.1 ad amr->amr_maxqueuecnt = ap->ap_maxio;
500 1.1 ad free(ap, M_DEVBUF);
501 1.1 ad
502 1.1 ad /*
503 1.1 ad * Fetch and record state of logical drives.
504 1.1 ad */
505 1.1 ad aex = amr_enquire(amr, AMR_CMD_CONFIG, AMR_CONFIG_ENQ3,
506 1.1 ad AMR_CONFIG_ENQ3_SOLICITED_FULL);
507 1.1 ad if (aex == NULL) {
508 1.8 thorpej aprint_error("%s ENQUIRY3 failed\n",
509 1.8 thorpej amr->amr_dv.dv_xname);
510 1.1 ad return (-1);
511 1.1 ad }
512 1.1 ad
513 1.1 ad if (aex->ae_numldrives > AMR_MAX_UNITS) {
514 1.8 thorpej aprint_error(
515 1.8 thorpej "%s: adjust AMR_MAX_UNITS to %d (currently %d)"
516 1.1 ad "\n", amr->amr_dv.dv_xname,
517 1.1 ad ae->ae_ldrv.al_numdrives, AMR_MAX_UNITS);
518 1.1 ad amr->amr_numdrives = AMR_MAX_UNITS;
519 1.1 ad } else
520 1.1 ad amr->amr_numdrives = aex->ae_numldrives;
521 1.1 ad
522 1.1 ad for (i = 0; i < amr->amr_numdrives; i++) {
523 1.1 ad amr->amr_drive[i].al_size =
524 1.1 ad le32toh(aex->ae_drivesize[i]);
525 1.1 ad amr->amr_drive[i].al_state = aex->ae_drivestate[i];
526 1.1 ad amr->amr_drive[i].al_properties = aex->ae_driveprop[i];
527 1.1 ad }
528 1.1 ad
529 1.1 ad free(aex, M_DEVBUF);
530 1.1 ad return (0);
531 1.1 ad }
532 1.1 ad
533 1.1 ad /*
534 1.1 ad * Try 8LD extended ENQUIRY to get the controller signature. Once
535 1.1 ad * found, search for a product description.
536 1.1 ad */
537 1.1 ad if ((ae = amr_enquire(amr, AMR_CMD_EXT_ENQUIRY2, 0, 0)) != NULL) {
538 1.1 ad i = 0;
539 1.1 ad sig = le32toh(ae->ae_signature);
540 1.1 ad
541 1.1 ad while (i < sizeof(amr_typestr) / sizeof(amr_typestr[0])) {
542 1.1 ad if (amr_typestr[i].at_sig == sig)
543 1.1 ad break;
544 1.1 ad i++;
545 1.1 ad }
546 1.1 ad if (i == sizeof(amr_typestr) / sizeof(amr_typestr[0])) {
547 1.1 ad sprintf(buf, "unknown ENQUIRY2 sig (0x%08x)", sig);
548 1.1 ad prodstr = buf;
549 1.1 ad } else
550 1.1 ad prodstr = amr_typestr[i].at_str;
551 1.1 ad } else {
552 1.1 ad if ((ae = amr_enquire(amr, AMR_CMD_ENQUIRY, 0, 0)) == NULL) {
553 1.8 thorpej aprint_error("%s: unsupported controller\n",
554 1.1 ad amr->amr_dv.dv_xname);
555 1.1 ad return (-1);
556 1.1 ad }
557 1.1 ad
558 1.1 ad switch (PCI_PRODUCT(pa->pa_id)) {
559 1.1 ad case PCI_PRODUCT_AMI_MEGARAID:
560 1.1 ad prodstr = "Series 428";
561 1.1 ad break;
562 1.1 ad case PCI_PRODUCT_AMI_MEGARAID2:
563 1.1 ad prodstr = "Series 434";
564 1.1 ad break;
565 1.1 ad default:
566 1.1 ad sprintf(buf, "unknown PCI dev (0x%04x)",
567 1.1 ad PCI_PRODUCT(pa->pa_id));
568 1.1 ad prodstr = buf;
569 1.1 ad break;
570 1.1 ad }
571 1.1 ad }
572 1.1 ad
573 1.8 thorpej aprint_normal("<%s>\n", prodstr);
574 1.1 ad if (intrstr != NULL)
575 1.8 thorpej aprint_normal("%s: interrupting at %s\n", amr->amr_dv.dv_xname,
576 1.1 ad intrstr);
577 1.8 thorpej aprint_normal("%s: firmware <%.4s>, BIOS <%.4s>, %dMB RAM\n",
578 1.1 ad amr->amr_dv.dv_xname, ae->ae_adapter.aa_firmware,
579 1.1 ad ae->ae_adapter.aa_bios, ae->ae_adapter.aa_memorysize);
580 1.1 ad
581 1.1 ad amr->amr_maxqueuecnt = ae->ae_adapter.aa_maxio;
582 1.1 ad
583 1.1 ad /*
584 1.1 ad * Record state of logical drives.
585 1.1 ad */
586 1.1 ad if (ae->ae_ldrv.al_numdrives > AMR_MAX_UNITS) {
587 1.8 thorpej aprint_error("%s: adjust AMR_MAX_UNITS to %d (currently %d)\n",
588 1.1 ad amr->amr_dv.dv_xname, ae->ae_ldrv.al_numdrives,
589 1.1 ad AMR_MAX_UNITS);
590 1.1 ad amr->amr_numdrives = AMR_MAX_UNITS;
591 1.1 ad } else
592 1.1 ad amr->amr_numdrives = ae->ae_ldrv.al_numdrives;
593 1.1 ad
594 1.1 ad for (i = 0; i < AMR_MAX_UNITS; i++) {
595 1.1 ad amr->amr_drive[i].al_size = le32toh(ae->ae_ldrv.al_size[i]);
596 1.1 ad amr->amr_drive[i].al_state = ae->ae_ldrv.al_state[i];
597 1.1 ad amr->amr_drive[i].al_properties = ae->ae_ldrv.al_properties[i];
598 1.1 ad }
599 1.1 ad
600 1.1 ad free(ae, M_DEVBUF);
601 1.1 ad return (0);
602 1.1 ad }
603 1.1 ad
604 1.1 ad /*
605 1.1 ad * Flush the internal cache on each configured controller. Called at
606 1.1 ad * shutdown time.
607 1.1 ad */
608 1.1 ad void
609 1.1 ad amr_shutdown(void *cookie)
610 1.1 ad {
611 1.1 ad extern struct cfdriver amr_cd;
612 1.1 ad struct amr_softc *amr;
613 1.1 ad struct amr_ccb *ac;
614 1.1 ad int i, rv;
615 1.1 ad
616 1.1 ad for (i = 0; i < amr_cd.cd_ndevs; i++) {
617 1.1 ad if ((amr = device_lookup(&amr_cd, i)) == NULL)
618 1.1 ad continue;
619 1.1 ad
620 1.1 ad if ((rv = amr_ccb_alloc(amr, &ac)) == 0) {
621 1.1 ad ac->ac_mbox.mb_command = AMR_CMD_FLUSH;
622 1.1 ad rv = amr_ccb_poll(amr, ac, 30000);
623 1.1 ad amr_ccb_free(amr, ac);
624 1.1 ad }
625 1.1 ad if (rv != 0)
626 1.1 ad printf("%s: unable to flush cache (%d)\n",
627 1.1 ad amr->amr_dv.dv_xname, rv);
628 1.1 ad }
629 1.1 ad }
630 1.1 ad
631 1.1 ad /*
632 1.1 ad * Interrupt service routine.
633 1.1 ad */
634 1.1 ad int
635 1.1 ad amr_intr(void *cookie)
636 1.1 ad {
637 1.1 ad struct amr_softc *amr;
638 1.1 ad struct amr_ccb *ac;
639 1.1 ad struct amr_mailbox mbox;
640 1.1 ad u_int i, forus, idx;
641 1.1 ad
642 1.1 ad amr = cookie;
643 1.1 ad forus = 0;
644 1.1 ad
645 1.1 ad while ((*amr->amr_get_work)(amr, &mbox) == 0) {
646 1.1 ad /* Iterate over completed commands in this result. */
647 1.1 ad for (i = 0; i < mbox.mb_nstatus; i++) {
648 1.1 ad idx = mbox.mb_completed[i] - 1;
649 1.1 ad ac = amr->amr_ccbs + idx;
650 1.1 ad
651 1.1 ad if (idx >= amr->amr_maxqueuecnt) {
652 1.1 ad printf("%s: bad status (bogus ID: %u=%u)\n",
653 1.1 ad amr->amr_dv.dv_xname, i, idx);
654 1.1 ad continue;
655 1.1 ad }
656 1.1 ad
657 1.1 ad if ((ac->ac_flags & AC_ACTIVE) == 0) {
658 1.1 ad printf("%s: bad status (not active; 0x04%x)\n",
659 1.1 ad amr->amr_dv.dv_xname, ac->ac_flags);
660 1.1 ad continue;
661 1.1 ad }
662 1.1 ad
663 1.1 ad ac->ac_status = mbox.mb_status;
664 1.1 ad ac->ac_flags = (ac->ac_flags & ~AC_ACTIVE) |
665 1.1 ad AC_COMPLETE;
666 1.1 ad
667 1.1 ad /* Pass notification to upper layers. */
668 1.1 ad if (ac->ac_handler != NULL)
669 1.1 ad (*ac->ac_handler)(ac);
670 1.1 ad }
671 1.1 ad forus = 1;
672 1.1 ad }
673 1.1 ad
674 1.1 ad if (forus)
675 1.1 ad amr_ccb_enqueue(amr, NULL);
676 1.1 ad return (forus);
677 1.1 ad }
678 1.1 ad
679 1.1 ad /*
680 1.1 ad * Run a generic enquiry-style command.
681 1.1 ad */
682 1.1 ad void *
683 1.1 ad amr_enquire(struct amr_softc *amr, u_int8_t cmd, u_int8_t cmdsub,
684 1.1 ad u_int8_t cmdqual)
685 1.1 ad {
686 1.1 ad struct amr_ccb *ac;
687 1.1 ad u_int8_t *mb;
688 1.1 ad void *buf;
689 1.1 ad int rv;
690 1.1 ad
691 1.1 ad if (amr_ccb_alloc(amr, &ac) != 0)
692 1.1 ad return (NULL);
693 1.1 ad buf = malloc(AMR_ENQUIRY_BUFSIZE, M_DEVBUF, M_NOWAIT);
694 1.1 ad
695 1.1 ad /* Build the command proper. */
696 1.1 ad mb = (u_int8_t *)&ac->ac_mbox;
697 1.1 ad mb[0] = cmd;
698 1.1 ad mb[2] = cmdsub;
699 1.1 ad mb[3] = cmdqual;
700 1.1 ad
701 1.1 ad if ((rv = amr_ccb_map(amr, ac, buf, AMR_ENQUIRY_BUFSIZE, 0)) == 0) {
702 1.1 ad rv = amr_ccb_poll(amr, ac, 2000);
703 1.1 ad amr_ccb_unmap(amr, ac);
704 1.1 ad }
705 1.1 ad
706 1.1 ad amr_ccb_free(amr, ac);
707 1.1 ad
708 1.1 ad if (rv != 0) {
709 1.1 ad free(buf, M_DEVBUF);
710 1.1 ad buf = NULL;
711 1.1 ad }
712 1.1 ad
713 1.1 ad return (buf);
714 1.1 ad }
715 1.1 ad
716 1.1 ad /*
717 1.1 ad * Allocate and initialise a CCB.
718 1.1 ad */
719 1.1 ad int
720 1.1 ad amr_ccb_alloc(struct amr_softc *amr, struct amr_ccb **acp)
721 1.1 ad {
722 1.1 ad struct amr_ccb *ac;
723 1.1 ad struct amr_mailbox *mb;
724 1.1 ad int s;
725 1.1 ad
726 1.1 ad s = splbio();
727 1.1 ad if ((ac = SLIST_FIRST(&amr->amr_ccb_freelist)) == NULL) {
728 1.1 ad splx(s);
729 1.1 ad return (EAGAIN);
730 1.1 ad }
731 1.1 ad SLIST_REMOVE_HEAD(&amr->amr_ccb_freelist, ac_chain.slist);
732 1.1 ad splx(s);
733 1.1 ad
734 1.1 ad ac->ac_handler = NULL;
735 1.1 ad mb = &ac->ac_mbox;
736 1.1 ad *acp = ac;
737 1.1 ad
738 1.1 ad memset(mb, 0, sizeof(*mb));
739 1.1 ad
740 1.1 ad mb->mb_ident = ac->ac_ident + 1;
741 1.1 ad mb->mb_busy = 1;
742 1.1 ad mb->mb_poll = 0;
743 1.1 ad mb->mb_ack = 0;
744 1.1 ad
745 1.1 ad return (0);
746 1.1 ad }
747 1.1 ad
748 1.1 ad /*
749 1.1 ad * Free a CCB.
750 1.1 ad */
751 1.1 ad void
752 1.1 ad amr_ccb_free(struct amr_softc *amr, struct amr_ccb *ac)
753 1.1 ad {
754 1.1 ad int s;
755 1.1 ad
756 1.1 ad ac->ac_flags = 0;
757 1.1 ad
758 1.1 ad s = splbio();
759 1.1 ad SLIST_INSERT_HEAD(&amr->amr_ccb_freelist, ac, ac_chain.slist);
760 1.1 ad splx(s);
761 1.1 ad }
762 1.1 ad
763 1.1 ad /*
764 1.1 ad * If a CCB is specified, enqueue it. Pull CCBs off the software queue in
765 1.1 ad * the order that they were enqueued and try to submit their command blocks
766 1.1 ad * to the controller for execution.
767 1.1 ad */
768 1.1 ad void
769 1.1 ad amr_ccb_enqueue(struct amr_softc *amr, struct amr_ccb *ac)
770 1.1 ad {
771 1.1 ad int s;
772 1.1 ad
773 1.1 ad s = splbio();
774 1.1 ad
775 1.1 ad if (ac != NULL)
776 1.1 ad SIMPLEQ_INSERT_TAIL(&amr->amr_ccb_queue, ac, ac_chain.simpleq);
777 1.1 ad
778 1.1 ad while ((ac = SIMPLEQ_FIRST(&amr->amr_ccb_queue)) != NULL) {
779 1.1 ad if ((*amr->amr_submit)(amr, ac) != 0)
780 1.1 ad break;
781 1.2 lukem SIMPLEQ_REMOVE_HEAD(&amr->amr_ccb_queue, ac_chain.simpleq);
782 1.1 ad }
783 1.1 ad
784 1.1 ad splx(s);
785 1.1 ad }
786 1.1 ad
787 1.1 ad /*
788 1.1 ad * Map the specified CCB's data buffer onto the bus, and fill the
789 1.1 ad * scatter-gather list.
790 1.1 ad */
791 1.1 ad int
792 1.1 ad amr_ccb_map(struct amr_softc *amr, struct amr_ccb *ac, void *data, int size,
793 1.1 ad int out)
794 1.1 ad {
795 1.1 ad struct amr_sgentry *sge;
796 1.1 ad struct amr_mailbox *mb;
797 1.1 ad int nsegs, i, rv, sgloff;
798 1.1 ad bus_dmamap_t xfer;
799 1.1 ad
800 1.1 ad xfer = ac->ac_xfer_map;
801 1.1 ad
802 1.1 ad rv = bus_dmamap_load(amr->amr_dmat, xfer, data, size, NULL,
803 1.1 ad BUS_DMA_NOWAIT);
804 1.1 ad if (rv != 0)
805 1.1 ad return (rv);
806 1.1 ad
807 1.1 ad mb = &ac->ac_mbox;
808 1.1 ad ac->ac_xfer_size = size;
809 1.1 ad ac->ac_flags |= (out ? AC_XFER_OUT : AC_XFER_IN);
810 1.1 ad sgloff = AMR_SGL_SIZE * ac->ac_ident;
811 1.1 ad
812 1.1 ad /* We don't need to use a scatter/gather list for just 1 segment. */
813 1.1 ad nsegs = xfer->dm_nsegs;
814 1.1 ad if (nsegs == 1) {
815 1.1 ad mb->mb_nsgelem = 0;
816 1.1 ad mb->mb_physaddr = htole32(xfer->dm_segs[0].ds_addr);
817 1.1 ad ac->ac_flags |= AC_NOSGL;
818 1.1 ad } else {
819 1.1 ad mb->mb_nsgelem = nsegs;
820 1.1 ad mb->mb_physaddr = htole32(amr->amr_sgls_paddr + sgloff);
821 1.1 ad
822 1.1 ad sge = (struct amr_sgentry *)((caddr_t)amr->amr_sgls + sgloff);
823 1.1 ad for (i = 0; i < nsegs; i++, sge++) {
824 1.1 ad sge->sge_addr = htole32(xfer->dm_segs[i].ds_addr);
825 1.1 ad sge->sge_count = htole32(xfer->dm_segs[i].ds_len);
826 1.1 ad }
827 1.1 ad }
828 1.1 ad
829 1.1 ad bus_dmamap_sync(amr->amr_dmat, xfer, 0, ac->ac_xfer_size,
830 1.1 ad out ? BUS_DMASYNC_PREWRITE : BUS_DMASYNC_PREREAD);
831 1.1 ad
832 1.1 ad if ((ac->ac_flags & AC_NOSGL) == 0)
833 1.1 ad bus_dmamap_sync(amr->amr_dmat, amr->amr_dmamap, sgloff,
834 1.1 ad AMR_SGL_SIZE, BUS_DMASYNC_PREWRITE);
835 1.1 ad
836 1.1 ad return (0);
837 1.1 ad }
838 1.1 ad
839 1.1 ad /*
840 1.1 ad * Unmap the specified CCB's data buffer.
841 1.1 ad */
842 1.1 ad void
843 1.1 ad amr_ccb_unmap(struct amr_softc *amr, struct amr_ccb *ac)
844 1.1 ad {
845 1.1 ad
846 1.1 ad if ((ac->ac_flags & AC_NOSGL) == 0)
847 1.1 ad bus_dmamap_sync(amr->amr_dmat, amr->amr_dmamap,
848 1.1 ad AMR_SGL_SIZE * ac->ac_ident, AMR_SGL_SIZE,
849 1.1 ad BUS_DMASYNC_POSTWRITE);
850 1.1 ad bus_dmamap_sync(amr->amr_dmat, ac->ac_xfer_map, 0, ac->ac_xfer_size,
851 1.1 ad (ac->ac_flags & AC_XFER_IN) != 0 ?
852 1.1 ad BUS_DMASYNC_POSTREAD : BUS_DMASYNC_POSTWRITE);
853 1.1 ad bus_dmamap_unload(amr->amr_dmat, ac->ac_xfer_map);
854 1.1 ad }
855 1.1 ad
856 1.1 ad /*
857 1.1 ad * Submit a command to the controller and poll on completion. Return
858 1.1 ad * non-zero on timeout or error. Must be called with interrupts blocked.
859 1.1 ad */
860 1.1 ad int
861 1.1 ad amr_ccb_poll(struct amr_softc *amr, struct amr_ccb *ac, int timo)
862 1.1 ad {
863 1.1 ad int rv;
864 1.1 ad
865 1.1 ad if ((rv = (*amr->amr_submit)(amr, ac)) != 0)
866 1.1 ad return (rv);
867 1.1 ad
868 1.1 ad for (timo *= 10; timo != 0; timo--) {
869 1.1 ad amr_intr(amr);
870 1.1 ad if ((ac->ac_flags & AC_COMPLETE) != 0)
871 1.1 ad break;
872 1.1 ad DELAY(100);
873 1.1 ad }
874 1.1 ad
875 1.1 ad return (timo == 0 || ac->ac_status != 0 ? EIO : 0);
876 1.1 ad }
877 1.1 ad
878 1.1 ad /*
879 1.1 ad * Wait for the mailbox to become available.
880 1.1 ad */
881 1.1 ad int
882 1.1 ad amr_mbox_wait(struct amr_softc *amr)
883 1.1 ad {
884 1.1 ad int timo;
885 1.1 ad
886 1.1 ad for (timo = 10000; timo != 0; timo--) {
887 1.1 ad if (amr->amr_mbox->mb_busy == 0)
888 1.1 ad break;
889 1.1 ad DELAY(100);
890 1.1 ad }
891 1.1 ad
892 1.1 ad #if 0
893 1.1 ad if (timo != 0)
894 1.1 ad printf("%s: controller wedged\n", amr->amr_dv.dv_xname);
895 1.1 ad #endif
896 1.1 ad
897 1.1 ad return (timo != 0 ? 0 : EIO);
898 1.1 ad }
899 1.1 ad
900 1.1 ad /*
901 1.1 ad * Tell the controller that the mailbox contains a valid command. Must be
902 1.1 ad * called with interrupts blocked.
903 1.1 ad */
904 1.1 ad int
905 1.1 ad amr_quartz_submit(struct amr_softc *amr, struct amr_ccb *ac)
906 1.1 ad {
907 1.1 ad u_int32_t v;
908 1.1 ad
909 1.1 ad v = amr_inl(amr, AMR_QREG_IDB);
910 1.1 ad if ((v & (AMR_QIDB_SUBMIT | AMR_QIDB_ACK)) != 0)
911 1.1 ad return (EBUSY);
912 1.1 ad
913 1.1 ad memcpy(amr->amr_mbox, &ac->ac_mbox, sizeof(ac->ac_mbox));
914 1.1 ad
915 1.1 ad ac->ac_flags |= AC_ACTIVE;
916 1.1 ad amr_outl(amr, AMR_QREG_IDB, amr->amr_mbox_paddr | AMR_QIDB_SUBMIT);
917 1.1 ad DELAY(10);
918 1.1 ad return (0);
919 1.1 ad }
920 1.1 ad
921 1.1 ad int
922 1.1 ad amr_std_submit(struct amr_softc *amr, struct amr_ccb *ac)
923 1.1 ad {
924 1.1 ad
925 1.1 ad if ((amr_inb(amr, AMR_SREG_MBOX_BUSY) & AMR_SMBOX_BUSY_FLAG) != 0)
926 1.1 ad return (EBUSY);
927 1.1 ad
928 1.1 ad memcpy(amr->amr_mbox, &ac->ac_mbox, sizeof(ac->ac_mbox));
929 1.1 ad
930 1.1 ad ac->ac_flags |= AC_ACTIVE;
931 1.1 ad amr_outb(amr, AMR_SREG_CMD, AMR_SCMD_POST);
932 1.1 ad return (0);
933 1.1 ad }
934 1.1 ad
935 1.1 ad /*
936 1.1 ad * Claim any work that the controller has completed; acknowledge completion,
937 1.1 ad * save details of the completion in (mbsave). Must be called with
938 1.1 ad * interrupts blocked.
939 1.1 ad */
940 1.1 ad int
941 1.1 ad amr_quartz_get_work(struct amr_softc *amr, struct amr_mailbox *mbsave)
942 1.1 ad {
943 1.1 ad u_int32_t v;
944 1.1 ad
945 1.1 ad if (amr_mbox_wait(amr))
946 1.1 ad return (EBUSY);
947 1.1 ad
948 1.1 ad v = amr_inl(amr, AMR_QREG_IDB);
949 1.1 ad if ((v & (AMR_QIDB_SUBMIT | AMR_QIDB_ACK)) != 0)
950 1.1 ad return (EBUSY);
951 1.1 ad
952 1.1 ad /* Work waiting for us? */
953 1.1 ad if (amr_inl(amr, AMR_QREG_ODB) != AMR_QODB_READY)
954 1.1 ad return (-1);
955 1.1 ad
956 1.1 ad /* Save the mailbox, which contains a list of completed commands. */
957 1.1 ad memcpy(mbsave, amr->amr_mbox, sizeof(*mbsave));
958 1.1 ad
959 1.1 ad /* Ack the interrupt and mailbox transfer. */
960 1.1 ad amr_outl(amr, AMR_QREG_ODB, AMR_QODB_READY);
961 1.1 ad amr_outl(amr, AMR_QREG_IDB, amr->amr_mbox_paddr | AMR_QIDB_ACK);
962 1.1 ad DELAY(10);
963 1.1 ad
964 1.1 ad #if 0
965 1.1 ad /*
966 1.1 ad * This waits for the controller to notice that we've taken the
967 1.1 ad * command from it. It's very inefficient, and we shouldn't do it,
968 1.1 ad * but if we remove this code, we stop completing commands under
969 1.1 ad * load.
970 1.1 ad *
971 1.1 ad * Peter J says we shouldn't do this. The documentation says we
972 1.1 ad * should. Who is right?
973 1.1 ad */
974 1.1 ad while ((amr_inl(amr, AMR_QREG_IDB) & AMR_QIDB_ACK) != 0)
975 1.1 ad ;
976 1.1 ad #endif
977 1.1 ad
978 1.1 ad return (0);
979 1.1 ad }
980 1.1 ad
981 1.1 ad int
982 1.1 ad amr_std_get_work(struct amr_softc *amr, struct amr_mailbox *mbsave)
983 1.1 ad {
984 1.1 ad u_int8_t istat;
985 1.1 ad
986 1.1 ad if (amr_mbox_wait(amr))
987 1.1 ad return (EBUSY);
988 1.1 ad
989 1.1 ad /* Puke if the mailbox is busy. */
990 1.1 ad if ((amr_inb(amr, AMR_SREG_MBOX_BUSY) & AMR_SMBOX_BUSY_FLAG) != 0)
991 1.1 ad return (-1);
992 1.1 ad
993 1.1 ad /* Check for valid interrupt status. */
994 1.1 ad if (((istat = amr_inb(amr, AMR_SREG_INTR)) & AMR_SINTR_VALID) == 0)
995 1.1 ad return (-1);
996 1.1 ad
997 1.1 ad /* Ack the interrupt. */
998 1.1 ad amr_outb(amr, AMR_SREG_INTR, istat);
999 1.1 ad
1000 1.1 ad /* Save mailbox, which contains a list of completed commands. */
1001 1.1 ad memcpy(mbsave, amr->amr_mbox, sizeof(*mbsave));
1002 1.1 ad
1003 1.1 ad /* Ack mailbox transfer. */
1004 1.1 ad amr_outb(amr, AMR_SREG_CMD, AMR_SCMD_ACKINTR);
1005 1.1 ad
1006 1.1 ad return (0);
1007 1.1 ad }
1008