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