twe.c revision 1.37.2.4 1 1.37.2.4 skrll /* $NetBSD: twe.c,v 1.37.2.4 2004/09/21 13:31:08 skrll Exp $ */
2 1.1 ad
3 1.1 ad /*-
4 1.37.2.2 skrll * Copyright (c) 2000, 2001, 2002, 2003, 2004 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.37.2.2 skrll * by Andrew Doran; and by Jason R. Thorpe of Wasabi Systems, Inc.
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) 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: twe.c,v 1.1 2000/05/24 23:35:23 msmith Exp
66 1.1 ad */
67 1.1 ad
68 1.1 ad /*
69 1.1 ad * Driver for the 3ware Escalade family of RAID controllers.
70 1.1 ad */
71 1.21 lukem
72 1.21 lukem #include <sys/cdefs.h>
73 1.37.2.4 skrll __KERNEL_RCSID(0, "$NetBSD: twe.c,v 1.37.2.4 2004/09/21 13:31:08 skrll Exp $");
74 1.1 ad
75 1.1 ad #include <sys/param.h>
76 1.1 ad #include <sys/systm.h>
77 1.1 ad #include <sys/kernel.h>
78 1.1 ad #include <sys/device.h>
79 1.1 ad #include <sys/queue.h>
80 1.1 ad #include <sys/proc.h>
81 1.1 ad #include <sys/buf.h>
82 1.1 ad #include <sys/endian.h>
83 1.1 ad #include <sys/malloc.h>
84 1.33 christos #include <sys/conf.h>
85 1.1 ad #include <sys/disk.h>
86 1.37.2.2 skrll #include <sys/syslog.h>
87 1.1 ad
88 1.1 ad #include <uvm/uvm_extern.h>
89 1.1 ad
90 1.1 ad #include <machine/bswap.h>
91 1.1 ad #include <machine/bus.h>
92 1.1 ad
93 1.1 ad #include <dev/pci/pcireg.h>
94 1.1 ad #include <dev/pci/pcivar.h>
95 1.1 ad #include <dev/pci/pcidevs.h>
96 1.1 ad #include <dev/pci/twereg.h>
97 1.1 ad #include <dev/pci/twevar.h>
98 1.33 christos #include <dev/pci/tweio.h>
99 1.1 ad
100 1.37.2.3 skrll #include "locators.h"
101 1.37.2.3 skrll
102 1.1 ad #define PCI_CBIO 0x10
103 1.1 ad
104 1.37.2.2 skrll static int twe_aen_get(struct twe_softc *, uint16_t *);
105 1.1 ad static void twe_aen_handler(struct twe_ccb *, int);
106 1.37.2.2 skrll static void twe_aen_enqueue(struct twe_softc *sc, uint16_t, int);
107 1.37.2.2 skrll static uint16_t twe_aen_dequeue(struct twe_softc *);
108 1.37.2.2 skrll
109 1.1 ad static void twe_attach(struct device *, struct device *, void *);
110 1.1 ad static int twe_init_connection(struct twe_softc *);
111 1.1 ad static int twe_intr(void *);
112 1.1 ad static int twe_match(struct device *, struct cfdata *, void *);
113 1.33 christos static int twe_param_set(struct twe_softc *, int, int, size_t, void *);
114 1.1 ad static void twe_poll(struct twe_softc *);
115 1.1 ad static int twe_print(void *, const char *);
116 1.1 ad static int twe_reset(struct twe_softc *);
117 1.37.2.3 skrll static int twe_submatch(struct device *, struct cfdata *,
118 1.37.2.3 skrll const locdesc_t *, void *);
119 1.1 ad static int twe_status_check(struct twe_softc *, u_int);
120 1.1 ad static int twe_status_wait(struct twe_softc *, u_int, int);
121 1.37.2.2 skrll static void twe_describe_controller(struct twe_softc *);
122 1.37.2.2 skrll
123 1.37.2.2 skrll static int twe_add_unit(struct twe_softc *, int);
124 1.37.2.2 skrll static int twe_del_unit(struct twe_softc *, int);
125 1.1 ad
126 1.22 ad static inline u_int32_t twe_inl(struct twe_softc *, int);
127 1.33 christos static inline void twe_outl(struct twe_softc *, int, u_int32_t);
128 1.33 christos
129 1.33 christos dev_type_open(tweopen);
130 1.33 christos dev_type_close(tweclose);
131 1.33 christos dev_type_ioctl(tweioctl);
132 1.33 christos
133 1.33 christos const struct cdevsw twe_cdevsw = {
134 1.33 christos tweopen, tweclose, noread, nowrite, tweioctl,
135 1.33 christos nostop, notty, nopoll, nommap,
136 1.33 christos };
137 1.33 christos
138 1.33 christos extern struct cfdriver twe_cd;
139 1.22 ad
140 1.30 thorpej CFATTACH_DECL(twe, sizeof(struct twe_softc),
141 1.31 thorpej twe_match, twe_attach, NULL, NULL);
142 1.1 ad
143 1.37.2.2 skrll /*
144 1.37.2.2 skrll * Tables to convert numeric codes to strings.
145 1.37.2.2 skrll */
146 1.37.2.2 skrll const struct twe_code_table twe_table_status[] = {
147 1.37.2.2 skrll { 0x00, "successful completion" },
148 1.37.2.2 skrll
149 1.37.2.2 skrll /* info */
150 1.37.2.2 skrll { 0x42, "command in progress" },
151 1.37.2.2 skrll { 0x6c, "retrying interface CRC error from UDMA command" },
152 1.37.2.2 skrll
153 1.37.2.2 skrll /* warning */
154 1.37.2.2 skrll { 0x81, "redundant/inconsequential request ignored" },
155 1.37.2.2 skrll { 0x8e, "failed to write zeroes to LBA 0" },
156 1.37.2.2 skrll { 0x8f, "failed to profile TwinStor zones" },
157 1.37.2.2 skrll
158 1.37.2.2 skrll /* fatal */
159 1.37.2.2 skrll { 0xc1, "aborted due to system command or reconfiguration" },
160 1.37.2.2 skrll { 0xc4, "aborted" },
161 1.37.2.2 skrll { 0xc5, "access error" },
162 1.37.2.2 skrll { 0xc6, "access violation" },
163 1.37.2.2 skrll { 0xc7, "device failure" }, /* high byte may be port # */
164 1.37.2.2 skrll { 0xc8, "controller error" },
165 1.37.2.2 skrll { 0xc9, "timed out" },
166 1.37.2.2 skrll { 0xcb, "invalid unit number" },
167 1.37.2.2 skrll { 0xcf, "unit not available" },
168 1.37.2.2 skrll { 0xd2, "undefined opcode" },
169 1.37.2.2 skrll { 0xdb, "request incompatible with unit" },
170 1.37.2.2 skrll { 0xdc, "invalid request" },
171 1.37.2.2 skrll { 0xff, "firmware error, reset requested" },
172 1.37.2.2 skrll
173 1.37.2.2 skrll { 0, NULL }
174 1.37.2.2 skrll };
175 1.37.2.2 skrll
176 1.37.2.2 skrll const struct twe_code_table twe_table_unitstate[] = {
177 1.37.2.2 skrll { TWE_PARAM_UNITSTATUS_Normal, "Normal" },
178 1.37.2.2 skrll { TWE_PARAM_UNITSTATUS_Initialising, "Initializing" },
179 1.37.2.2 skrll { TWE_PARAM_UNITSTATUS_Degraded, "Degraded" },
180 1.37.2.2 skrll { TWE_PARAM_UNITSTATUS_Rebuilding, "Rebuilding" },
181 1.37.2.2 skrll { TWE_PARAM_UNITSTATUS_Verifying, "Verifying" },
182 1.37.2.2 skrll { TWE_PARAM_UNITSTATUS_Corrupt, "Corrupt" },
183 1.37.2.2 skrll { TWE_PARAM_UNITSTATUS_Missing, "Missing" },
184 1.37.2.2 skrll
185 1.37.2.2 skrll { 0, NULL }
186 1.37.2.2 skrll };
187 1.37.2.2 skrll
188 1.37.2.2 skrll const struct twe_code_table twe_table_unittype[] = {
189 1.37.2.2 skrll /* array descriptor configuration */
190 1.37.2.2 skrll { TWE_AD_CONFIG_RAID0, "RAID0" },
191 1.37.2.2 skrll { TWE_AD_CONFIG_RAID1, "RAID1" },
192 1.37.2.2 skrll { TWE_AD_CONFIG_TwinStor, "TwinStor" },
193 1.37.2.2 skrll { TWE_AD_CONFIG_RAID5, "RAID5" },
194 1.37.2.2 skrll { TWE_AD_CONFIG_RAID10, "RAID10" },
195 1.37.2.2 skrll
196 1.37.2.2 skrll { 0, NULL }
197 1.37.2.2 skrll };
198 1.37.2.2 skrll
199 1.37.2.2 skrll const struct twe_code_table twe_table_stripedepth[] = {
200 1.37.2.2 skrll { TWE_AD_STRIPE_4k, "4K" },
201 1.37.2.2 skrll { TWE_AD_STRIPE_8k, "8K" },
202 1.37.2.2 skrll { TWE_AD_STRIPE_16k, "16K" },
203 1.37.2.2 skrll { TWE_AD_STRIPE_32k, "32K" },
204 1.37.2.2 skrll { TWE_AD_STRIPE_64k, "64K" },
205 1.37.2.2 skrll
206 1.37.2.2 skrll { 0, NULL }
207 1.1 ad };
208 1.1 ad
209 1.26 christos /*
210 1.37.2.2 skrll * Asynchronous event notification messages are qualified:
211 1.37.2.2 skrll * a - not unit/port specific
212 1.37.2.2 skrll * u - unit specific
213 1.37.2.2 skrll * p - port specific
214 1.37.2.2 skrll *
215 1.37.2.2 skrll * They are further qualified with a severity:
216 1.37.2.2 skrll * E - LOG_EMERG
217 1.37.2.2 skrll * a - LOG_ALERT
218 1.37.2.2 skrll * c - LOG_CRIT
219 1.37.2.2 skrll * e - LOG_ERR
220 1.37.2.2 skrll * w - LOG_WARNING
221 1.37.2.2 skrll * n - LOG_NOTICE
222 1.37.2.2 skrll * i - LOG_INFO
223 1.37.2.2 skrll * d - LOG_DEBUG
224 1.37.2.2 skrll * blank - just use printf
225 1.37.2.2 skrll */
226 1.37.2.2 skrll const struct twe_code_table twe_table_aen[] = {
227 1.37.2.2 skrll { 0x00, "a queue empty" },
228 1.37.2.2 skrll { 0x01, "a soft reset" },
229 1.37.2.2 skrll { 0x02, "uc degraded mode" },
230 1.37.2.2 skrll { 0x03, "aa controller error" },
231 1.37.2.2 skrll { 0x04, "uE rebuild fail" },
232 1.37.2.2 skrll { 0x05, "un rebuild done" },
233 1.37.2.2 skrll { 0x06, "ue incomplete unit" },
234 1.37.2.2 skrll { 0x07, "un initialization done" },
235 1.37.2.2 skrll { 0x08, "uw unclean shutdown detected" },
236 1.37.2.2 skrll { 0x09, "pe drive timeout" },
237 1.37.2.2 skrll { 0x0a, "pc drive error" },
238 1.37.2.2 skrll { 0x0b, "un rebuild started" },
239 1.37.2.2 skrll { 0x0c, "un initialization started" },
240 1.37.2.2 skrll { 0x0d, "ui logical unit deleted" },
241 1.37.2.2 skrll { 0x0f, "pc SMART threshold exceeded" },
242 1.37.2.2 skrll { 0x15, "a table undefined" }, /* XXX: Not in FreeBSD's table */
243 1.37.2.2 skrll { 0x21, "pe ATA UDMA downgrade" },
244 1.37.2.2 skrll { 0x22, "pi ATA UDMA upgrade" },
245 1.37.2.2 skrll { 0x23, "pw sector repair occurred" },
246 1.37.2.2 skrll { 0x24, "aa SBUF integrity check failure" },
247 1.37.2.2 skrll { 0x25, "pa lost cached write" },
248 1.37.2.2 skrll { 0x26, "pa drive ECC error detected" },
249 1.37.2.2 skrll { 0x27, "pe DCB checksum error" },
250 1.37.2.2 skrll { 0x28, "pn DCB unsupported version" },
251 1.37.2.2 skrll { 0x29, "ui verify started" },
252 1.37.2.2 skrll { 0x2a, "ua verify failed" },
253 1.37.2.2 skrll { 0x2b, "ui verify complete" },
254 1.37.2.2 skrll { 0x2c, "pw overwrote bad sector during rebuild" },
255 1.37.2.2 skrll { 0x2d, "pa encountered bad sector during rebuild" },
256 1.37.2.2 skrll { 0x2e, "pe replacement drive too small" },
257 1.37.2.2 skrll { 0x2f, "ue array not previously initialized" },
258 1.37.2.2 skrll { 0x30, "p drive not supported" },
259 1.37.2.2 skrll { 0xff, "a aen queue full" },
260 1.37.2.2 skrll
261 1.37.2.2 skrll { 0, NULL },
262 1.26 christos };
263 1.26 christos
264 1.37.2.2 skrll const char *
265 1.37.2.2 skrll twe_describe_code(const struct twe_code_table *table, uint32_t code)
266 1.37.2.2 skrll {
267 1.37.2.2 skrll
268 1.37.2.2 skrll for (; table->string != NULL; table++) {
269 1.37.2.2 skrll if (table->code == code)
270 1.37.2.2 skrll return (table->string);
271 1.37.2.2 skrll }
272 1.37.2.2 skrll return (NULL);
273 1.37.2.2 skrll }
274 1.26 christos
275 1.22 ad static inline u_int32_t
276 1.22 ad twe_inl(struct twe_softc *sc, int off)
277 1.22 ad {
278 1.22 ad
279 1.22 ad bus_space_barrier(sc->sc_iot, sc->sc_ioh, off, 4,
280 1.22 ad BUS_SPACE_BARRIER_WRITE | BUS_SPACE_BARRIER_READ);
281 1.22 ad return (bus_space_read_4(sc->sc_iot, sc->sc_ioh, off));
282 1.22 ad }
283 1.22 ad
284 1.22 ad static inline void
285 1.22 ad twe_outl(struct twe_softc *sc, int off, u_int32_t val)
286 1.22 ad {
287 1.22 ad
288 1.22 ad bus_space_write_4(sc->sc_iot, sc->sc_ioh, off, val);
289 1.22 ad bus_space_barrier(sc->sc_iot, sc->sc_ioh, off, 4,
290 1.22 ad BUS_SPACE_BARRIER_WRITE);
291 1.22 ad }
292 1.22 ad
293 1.1 ad /*
294 1.1 ad * Match a supported board.
295 1.1 ad */
296 1.1 ad static int
297 1.1 ad twe_match(struct device *parent, struct cfdata *cfdata, void *aux)
298 1.1 ad {
299 1.1 ad struct pci_attach_args *pa;
300 1.1 ad
301 1.1 ad pa = aux;
302 1.1 ad
303 1.1 ad return (PCI_VENDOR(pa->pa_id) == PCI_VENDOR_3WARE &&
304 1.10 ad (PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_3WARE_ESCALADE ||
305 1.10 ad PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_3WARE_ESCALADE_ASIC));
306 1.1 ad }
307 1.1 ad
308 1.1 ad /*
309 1.1 ad * Attach a supported board.
310 1.1 ad *
311 1.1 ad * XXX This doesn't fail gracefully.
312 1.1 ad */
313 1.1 ad static void
314 1.1 ad twe_attach(struct device *parent, struct device *self, void *aux)
315 1.1 ad {
316 1.1 ad struct pci_attach_args *pa;
317 1.1 ad struct twe_softc *sc;
318 1.1 ad pci_chipset_tag_t pc;
319 1.1 ad pci_intr_handle_t ih;
320 1.1 ad pcireg_t csr;
321 1.1 ad const char *intrstr;
322 1.37.2.2 skrll int s, size, i, rv, rseg;
323 1.23 christos size_t max_segs, max_xfer;
324 1.1 ad bus_dma_segment_t seg;
325 1.1 ad struct twe_cmd *tc;
326 1.1 ad struct twe_ccb *ccb;
327 1.1 ad
328 1.1 ad sc = (struct twe_softc *)self;
329 1.1 ad pa = aux;
330 1.1 ad pc = pa->pa_pc;
331 1.1 ad sc->sc_dmat = pa->pa_dmat;
332 1.1 ad SIMPLEQ_INIT(&sc->sc_ccb_queue);
333 1.1 ad SLIST_INIT(&sc->sc_ccb_freelist);
334 1.1 ad
335 1.37.2.2 skrll aprint_naive(": RAID controller\n");
336 1.37.2.2 skrll aprint_normal(": 3ware Escalade\n");
337 1.1 ad
338 1.33 christos ccb = malloc(sizeof(*ccb) * TWE_MAX_QUEUECNT, M_DEVBUF, M_NOWAIT);
339 1.33 christos if (ccb == NULL) {
340 1.37.2.2 skrll aprint_error("%s: unable to allocate memory for ccbs\n",
341 1.33 christos sc->sc_dv.dv_xname);
342 1.33 christos return;
343 1.33 christos }
344 1.33 christos
345 1.1 ad if (pci_mapreg_map(pa, PCI_CBIO, PCI_MAPREG_TYPE_IO, 0,
346 1.1 ad &sc->sc_iot, &sc->sc_ioh, NULL, NULL)) {
347 1.37.2.2 skrll aprint_error("%s: can't map i/o space\n", sc->sc_dv.dv_xname);
348 1.1 ad return;
349 1.1 ad }
350 1.1 ad
351 1.1 ad /* Enable the device. */
352 1.1 ad csr = pci_conf_read(pa->pa_pc, pa->pa_tag, PCI_COMMAND_STATUS_REG);
353 1.1 ad pci_conf_write(pa->pa_pc, pa->pa_tag, PCI_COMMAND_STATUS_REG,
354 1.1 ad csr | PCI_COMMAND_MASTER_ENABLE);
355 1.1 ad
356 1.1 ad /* Map and establish the interrupt. */
357 1.5 sommerfe if (pci_intr_map(pa, &ih)) {
358 1.37.2.2 skrll aprint_error("%s: can't map interrupt\n", sc->sc_dv.dv_xname);
359 1.1 ad return;
360 1.1 ad }
361 1.37.2.2 skrll
362 1.1 ad intrstr = pci_intr_string(pc, ih);
363 1.1 ad sc->sc_ih = pci_intr_establish(pc, ih, IPL_BIO, twe_intr, sc);
364 1.1 ad if (sc->sc_ih == NULL) {
365 1.37.2.2 skrll aprint_error("%s: can't establish interrupt%s%s\n",
366 1.37.2.2 skrll sc->sc_dv.dv_xname,
367 1.37.2.2 skrll (intrstr) ? " at " : "",
368 1.37.2.2 skrll (intrstr) ? intrstr : "");
369 1.1 ad return;
370 1.1 ad }
371 1.37.2.2 skrll
372 1.1 ad if (intrstr != NULL)
373 1.37.2.2 skrll aprint_normal("%s: interrupting at %s\n",
374 1.37.2.2 skrll sc->sc_dv.dv_xname, intrstr);
375 1.1 ad
376 1.1 ad /*
377 1.1 ad * Allocate and initialise the command blocks and CCBs.
378 1.1 ad */
379 1.7 ad size = sizeof(struct twe_cmd) * TWE_MAX_QUEUECNT;
380 1.1 ad
381 1.4 thorpej if ((rv = bus_dmamem_alloc(sc->sc_dmat, size, PAGE_SIZE, 0, &seg, 1,
382 1.1 ad &rseg, BUS_DMA_NOWAIT)) != 0) {
383 1.37.2.2 skrll aprint_error("%s: unable to allocate commands, rv = %d\n",
384 1.1 ad sc->sc_dv.dv_xname, rv);
385 1.1 ad return;
386 1.1 ad }
387 1.1 ad
388 1.1 ad if ((rv = bus_dmamem_map(sc->sc_dmat, &seg, rseg, size,
389 1.1 ad (caddr_t *)&sc->sc_cmds,
390 1.1 ad BUS_DMA_NOWAIT | BUS_DMA_COHERENT)) != 0) {
391 1.37.2.2 skrll aprint_error("%s: unable to map commands, rv = %d\n",
392 1.1 ad sc->sc_dv.dv_xname, rv);
393 1.1 ad return;
394 1.1 ad }
395 1.1 ad
396 1.1 ad if ((rv = bus_dmamap_create(sc->sc_dmat, size, size, 1, 0,
397 1.1 ad BUS_DMA_NOWAIT, &sc->sc_dmamap)) != 0) {
398 1.37.2.2 skrll aprint_error("%s: unable to create command DMA map, rv = %d\n",
399 1.1 ad sc->sc_dv.dv_xname, rv);
400 1.1 ad return;
401 1.1 ad }
402 1.1 ad
403 1.1 ad if ((rv = bus_dmamap_load(sc->sc_dmat, sc->sc_dmamap, sc->sc_cmds,
404 1.1 ad size, NULL, BUS_DMA_NOWAIT)) != 0) {
405 1.37.2.2 skrll aprint_error("%s: unable to load command DMA map, rv = %d\n",
406 1.1 ad sc->sc_dv.dv_xname, rv);
407 1.1 ad return;
408 1.1 ad }
409 1.1 ad
410 1.1 ad sc->sc_cmds_paddr = sc->sc_dmamap->dm_segs[0].ds_addr;
411 1.1 ad memset(sc->sc_cmds, 0, size);
412 1.1 ad
413 1.1 ad sc->sc_ccbs = ccb;
414 1.1 ad tc = (struct twe_cmd *)sc->sc_cmds;
415 1.24 christos max_segs = twe_get_maxsegs();
416 1.24 christos max_xfer = twe_get_maxxfer(max_segs);
417 1.1 ad
418 1.7 ad for (i = 0; i < TWE_MAX_QUEUECNT; i++, tc++, ccb++) {
419 1.1 ad ccb->ccb_cmd = tc;
420 1.1 ad ccb->ccb_cmdid = i;
421 1.1 ad ccb->ccb_flags = 0;
422 1.23 christos rv = bus_dmamap_create(sc->sc_dmat, max_xfer,
423 1.23 christos max_segs, PAGE_SIZE, 0,
424 1.4 thorpej BUS_DMA_NOWAIT | BUS_DMA_ALLOCNOW,
425 1.1 ad &ccb->ccb_dmamap_xfer);
426 1.7 ad if (rv != 0) {
427 1.37.2.2 skrll aprint_error("%s: can't create dmamap, rv = %d\n",
428 1.7 ad sc->sc_dv.dv_xname, rv);
429 1.7 ad return;
430 1.7 ad }
431 1.37.2.2 skrll
432 1.37.2.2 skrll /* Save the first CCB for AEN retrieval. */
433 1.3 ad if (i != 0)
434 1.3 ad SLIST_INSERT_HEAD(&sc->sc_ccb_freelist, ccb,
435 1.3 ad ccb_chain.slist);
436 1.3 ad }
437 1.1 ad
438 1.1 ad /* Wait for the controller to become ready. */
439 1.1 ad if (twe_status_wait(sc, TWE_STS_MICROCONTROLLER_READY, 6)) {
440 1.37.2.2 skrll aprint_error("%s: microcontroller not ready\n",
441 1.37.2.2 skrll sc->sc_dv.dv_xname);
442 1.1 ad return;
443 1.1 ad }
444 1.1 ad
445 1.22 ad twe_outl(sc, TWE_REG_CTL, TWE_CTL_DISABLE_INTRS);
446 1.1 ad
447 1.1 ad /* Reset the controller. */
448 1.37.2.2 skrll s = splbio();
449 1.37.2.2 skrll rv = twe_reset(sc);
450 1.37.2.2 skrll splx(s);
451 1.37.2.2 skrll if (rv) {
452 1.37.2.2 skrll aprint_error("%s: reset failed\n", sc->sc_dv.dv_xname);
453 1.37.2.2 skrll return;
454 1.37.2.2 skrll }
455 1.37.2.2 skrll
456 1.37.2.2 skrll /* Initialise connection with controller. */
457 1.37.2.2 skrll twe_init_connection(sc);
458 1.37.2.2 skrll
459 1.37.2.2 skrll twe_describe_controller(sc);
460 1.37.2.2 skrll
461 1.37.2.2 skrll /* Find and attach RAID array units. */
462 1.37.2.2 skrll sc->sc_nunits = 0;
463 1.37.2.2 skrll for (i = 0; i < TWE_MAX_UNITS; i++)
464 1.37.2.2 skrll (void) twe_add_unit(sc, i);
465 1.37.2.2 skrll
466 1.37.2.2 skrll /* ...and finally, enable interrupts. */
467 1.37.2.2 skrll twe_outl(sc, TWE_REG_CTL, TWE_CTL_CLEAR_ATTN_INTR |
468 1.37.2.2 skrll TWE_CTL_UNMASK_RESP_INTR |
469 1.37.2.2 skrll TWE_CTL_ENABLE_INTRS);
470 1.37.2.2 skrll }
471 1.37.2.2 skrll
472 1.37.2.2 skrll void
473 1.37.2.2 skrll twe_register_callbacks(struct twe_softc *sc, int unit,
474 1.37.2.2 skrll const struct twe_callbacks *tcb)
475 1.37.2.2 skrll {
476 1.37.2.2 skrll
477 1.37.2.2 skrll sc->sc_units[unit].td_callbacks = tcb;
478 1.37.2.2 skrll }
479 1.37.2.2 skrll
480 1.37.2.2 skrll static void
481 1.37.2.2 skrll twe_recompute_openings(struct twe_softc *sc)
482 1.37.2.2 skrll {
483 1.37.2.2 skrll struct twe_drive *td;
484 1.37.2.2 skrll int unit, openings;
485 1.37.2.2 skrll
486 1.37.2.2 skrll if (sc->sc_nunits != 0)
487 1.37.2.2 skrll openings = (TWE_MAX_QUEUECNT - 1) / sc->sc_nunits;
488 1.37.2.2 skrll else
489 1.37.2.2 skrll openings = 0;
490 1.37.2.2 skrll if (openings == sc->sc_openings)
491 1.1 ad return;
492 1.37.2.2 skrll sc->sc_openings = openings;
493 1.37.2.2 skrll
494 1.37.2.2 skrll #ifdef TWE_DEBUG
495 1.37.2.2 skrll printf("%s: %d array%s, %d openings per array\n",
496 1.37.2.2 skrll sc->sc_dv.dv_xname, sc->sc_nunits,
497 1.37.2.2 skrll sc->sc_nunits == 1 ? "" : "s", sc->sc_openings);
498 1.37.2.2 skrll #endif
499 1.37.2.2 skrll
500 1.37.2.2 skrll for (unit = 0; unit < TWE_MAX_UNITS; unit++) {
501 1.37.2.2 skrll td = &sc->sc_units[unit];
502 1.37.2.2 skrll if (td->td_dev != NULL)
503 1.37.2.2 skrll (*td->td_callbacks->tcb_openings)(td->td_dev,
504 1.37.2.2 skrll sc->sc_openings);
505 1.1 ad }
506 1.37.2.2 skrll }
507 1.37.2.2 skrll
508 1.37.2.2 skrll static int
509 1.37.2.2 skrll twe_add_unit(struct twe_softc *sc, int unit)
510 1.37.2.2 skrll {
511 1.37.2.2 skrll struct twe_param *dtp, *atp;
512 1.37.2.2 skrll struct twe_array_descriptor *ad;
513 1.37.2.2 skrll struct twe_drive *td;
514 1.37.2.2 skrll struct twe_attach_args twea;
515 1.37.2.2 skrll uint32_t newsize;
516 1.37.2.2 skrll int rv;
517 1.37.2.2 skrll uint16_t dsize;
518 1.37.2.2 skrll uint8_t newtype, newstripe;
519 1.37.2.3 skrll int help[2];
520 1.37.2.3 skrll locdesc_t *ldesc = (void *)help; /* XXX */
521 1.37.2.2 skrll
522 1.37.2.2 skrll if (unit < 0 || unit >= TWE_MAX_UNITS)
523 1.37.2.2 skrll return (EINVAL);
524 1.1 ad
525 1.3 ad /* Find attached units. */
526 1.7 ad rv = twe_param_get(sc, TWE_PARAM_UNITSUMMARY,
527 1.37.2.2 skrll TWE_PARAM_UNITSUMMARY_Status, TWE_MAX_UNITS, NULL, &dtp);
528 1.7 ad if (rv != 0) {
529 1.37.2.2 skrll aprint_error("%s: error %d fetching unit summary\n",
530 1.7 ad sc->sc_dv.dv_xname, rv);
531 1.37.2.2 skrll return (rv);
532 1.1 ad }
533 1.1 ad
534 1.1 ad /* For each detected unit, collect size and store in an array. */
535 1.37.2.2 skrll td = &sc->sc_units[unit];
536 1.1 ad
537 1.37.2.2 skrll /* Unit present? */
538 1.37.2.2 skrll if ((dtp->tp_data[unit] & TWE_PARAM_UNITSTATUS_Online) == 0) {
539 1.37.2.2 skrll /*
540 1.37.2.2 skrll * XXX Should we check to see if a device has been
541 1.37.2.2 skrll * XXX attached at this index and detach it if it
542 1.37.2.2 skrll * XXX has? ("rescan" semantics)
543 1.37.2.2 skrll */
544 1.37.2.2 skrll rv = 0;
545 1.37.2.2 skrll goto out;
546 1.37.2.2 skrll }
547 1.1 ad
548 1.37.2.2 skrll rv = twe_param_get_2(sc, TWE_PARAM_UNITINFO + unit,
549 1.37.2.2 skrll TWE_PARAM_UNITINFO_DescriptorSize, &dsize);
550 1.37.2.2 skrll if (rv != 0) {
551 1.37.2.2 skrll aprint_error("%s: error %d fetching descriptor size "
552 1.37.2.2 skrll "for unit %d\n", sc->sc_dv.dv_xname, rv, unit);
553 1.37.2.2 skrll goto out;
554 1.37.2.2 skrll }
555 1.37.2.2 skrll
556 1.37.2.2 skrll rv = twe_param_get(sc, TWE_PARAM_UNITINFO + unit,
557 1.37.2.2 skrll TWE_PARAM_UNITINFO_Descriptor, dsize - 3, NULL, &atp);
558 1.37.2.2 skrll if (rv != 0) {
559 1.37.2.2 skrll aprint_error("%s: error %d fetching array descriptor "
560 1.37.2.2 skrll "for unit %d\n", sc->sc_dv.dv_xname, rv, unit);
561 1.37.2.2 skrll goto out;
562 1.37.2.2 skrll }
563 1.37.2.2 skrll
564 1.37.2.2 skrll ad = (struct twe_array_descriptor *)atp->tp_data;
565 1.37.2.2 skrll newtype = ad->configuration;
566 1.37.2.2 skrll newstripe = ad->stripe_size;
567 1.37.2.2 skrll free(atp, M_DEVBUF);
568 1.37.2.2 skrll
569 1.37.2.2 skrll rv = twe_param_get_4(sc, TWE_PARAM_UNITINFO + unit,
570 1.37.2.2 skrll TWE_PARAM_UNITINFO_Capacity, &newsize);
571 1.37.2.2 skrll if (rv != 0) {
572 1.37.2.2 skrll aprint_error(
573 1.37.2.2 skrll "%s: error %d fetching capacity for unit %d\n",
574 1.37.2.2 skrll sc->sc_dv.dv_xname, rv, unit);
575 1.37.2.2 skrll goto out;
576 1.1 ad }
577 1.37.2.2 skrll
578 1.37.2.2 skrll /*
579 1.37.2.2 skrll * Have a device, so we need to attach it. If there is currently
580 1.37.2.2 skrll * something sitting at the slot, and the parameters are different,
581 1.37.2.2 skrll * then we detach the old device before attaching the new one.
582 1.37.2.2 skrll */
583 1.37.2.2 skrll if (td->td_dev != NULL &&
584 1.37.2.2 skrll td->td_size == newsize &&
585 1.37.2.2 skrll td->td_type == newtype &&
586 1.37.2.2 skrll td->td_stripe == newstripe) {
587 1.37.2.2 skrll /* Same as the old device; just keep using it. */
588 1.37.2.2 skrll rv = 0;
589 1.37.2.2 skrll goto out;
590 1.37.2.2 skrll } else if (td->td_dev != NULL) {
591 1.37.2.2 skrll /* Detach the old device first. */
592 1.37.2.2 skrll (void) config_detach(td->td_dev, DETACH_FORCE);
593 1.37.2.2 skrll td->td_dev = NULL;
594 1.37.2.2 skrll } else if (td->td_size == 0)
595 1.37.2.2 skrll sc->sc_nunits++;
596 1.37.2.2 skrll
597 1.37.2.2 skrll /*
598 1.37.2.2 skrll * Committed to the new array unit; assign its parameters and
599 1.37.2.2 skrll * recompute the number of available command openings.
600 1.37.2.2 skrll */
601 1.37.2.2 skrll td->td_size = newsize;
602 1.37.2.2 skrll td->td_type = newtype;
603 1.37.2.2 skrll td->td_stripe = newstripe;
604 1.37.2.2 skrll twe_recompute_openings(sc);
605 1.37.2.2 skrll
606 1.37.2.2 skrll twea.twea_unit = unit;
607 1.37.2.3 skrll
608 1.37.2.3 skrll ldesc->len = 1;
609 1.37.2.3 skrll ldesc->locs[TWECF_UNIT] = unit;
610 1.37.2.3 skrll
611 1.37.2.3 skrll td->td_dev = config_found_sm_loc(&sc->sc_dv, "twe", NULL, &twea,
612 1.37.2.3 skrll twe_print, twe_submatch);
613 1.37.2.2 skrll
614 1.37.2.2 skrll rv = 0;
615 1.37.2.2 skrll out:
616 1.1 ad free(dtp, M_DEVBUF);
617 1.37.2.2 skrll return (rv);
618 1.37.2.2 skrll }
619 1.1 ad
620 1.37.2.2 skrll static int
621 1.37.2.2 skrll twe_del_unit(struct twe_softc *sc, int unit)
622 1.37.2.2 skrll {
623 1.37.2.2 skrll struct twe_drive *td;
624 1.1 ad
625 1.37.2.2 skrll if (unit < 0 || unit >= TWE_MAX_UNITS)
626 1.37.2.2 skrll return (EINVAL);
627 1.37.2.2 skrll
628 1.37.2.2 skrll td = &sc->sc_units[unit];
629 1.37.2.2 skrll if (td->td_size != 0)
630 1.37.2.2 skrll sc->sc_nunits--;
631 1.37.2.2 skrll td->td_size = 0;
632 1.37.2.2 skrll td->td_type = 0;
633 1.37.2.2 skrll td->td_stripe = 0;
634 1.37.2.2 skrll if (td->td_dev != NULL) {
635 1.37.2.2 skrll (void) config_detach(td->td_dev, DETACH_FORCE);
636 1.37.2.2 skrll td->td_dev = NULL;
637 1.1 ad }
638 1.37.2.2 skrll twe_recompute_openings(sc);
639 1.37.2.2 skrll return (0);
640 1.1 ad }
641 1.1 ad
642 1.1 ad /*
643 1.37.2.2 skrll * Reset the controller.
644 1.37.2.2 skrll * MUST BE CALLED AT splbio()!
645 1.1 ad */
646 1.1 ad static int
647 1.1 ad twe_reset(struct twe_softc *sc)
648 1.1 ad {
649 1.37.2.2 skrll uint16_t aen;
650 1.37.2.2 skrll u_int status;
651 1.1 ad volatile u_int32_t junk;
652 1.7 ad int got, rv;
653 1.1 ad
654 1.1 ad /* Issue a soft reset. */
655 1.22 ad twe_outl(sc, TWE_REG_CTL, TWE_CTL_ISSUE_SOFT_RESET |
656 1.1 ad TWE_CTL_CLEAR_HOST_INTR |
657 1.1 ad TWE_CTL_CLEAR_ATTN_INTR |
658 1.1 ad TWE_CTL_MASK_CMD_INTR |
659 1.1 ad TWE_CTL_MASK_RESP_INTR |
660 1.1 ad TWE_CTL_CLEAR_ERROR_STS |
661 1.1 ad TWE_CTL_DISABLE_INTRS);
662 1.1 ad
663 1.37.2.2 skrll /* Wait for attention... */
664 1.37.2.2 skrll if (twe_status_wait(sc, TWE_STS_ATTN_INTR, 30)) {
665 1.37.2.2 skrll printf("%s: timeout waiting for attention interrupt\n",
666 1.1 ad sc->sc_dv.dv_xname);
667 1.1 ad return (-1);
668 1.1 ad }
669 1.1 ad
670 1.37.2.2 skrll /* ...and ACK it. */
671 1.37.2.2 skrll twe_outl(sc, TWE_REG_CTL, TWE_CTL_CLEAR_ATTN_INTR);
672 1.37.2.2 skrll
673 1.37.2.2 skrll /*
674 1.37.2.2 skrll * Pull AENs out of the controller; look for a soft reset AEN.
675 1.37.2.2 skrll * Open code this, since we want to detect reset even if the
676 1.37.2.2 skrll * queue for management tools is full.
677 1.37.2.2 skrll *
678 1.37.2.2 skrll * Note that since:
679 1.37.2.2 skrll * - interrupts are blocked
680 1.37.2.2 skrll * - we have reset the controller
681 1.37.2.2 skrll * - acknowledged the pending ATTENTION
682 1.37.2.2 skrll * that there is no way a pending asynchronous AEN fetch would
683 1.37.2.2 skrll * finish, so clear the flag.
684 1.37.2.2 skrll */
685 1.37.2.2 skrll sc->sc_flags &= ~TWEF_AEN;
686 1.1 ad for (got = 0;;) {
687 1.37.2.2 skrll rv = twe_aen_get(sc, &aen);
688 1.7 ad if (rv != 0)
689 1.37.2.2 skrll printf("%s: error %d while draining event queue\n",
690 1.7 ad sc->sc_dv.dv_xname, rv);
691 1.37.2.2 skrll if (TWE_AEN_CODE(aen) == TWE_AEN_QUEUE_EMPTY)
692 1.1 ad break;
693 1.37.2.2 skrll if (TWE_AEN_CODE(aen) == TWE_AEN_SOFT_RESET)
694 1.1 ad got = 1;
695 1.37.2.2 skrll twe_aen_enqueue(sc, aen, 1);
696 1.1 ad }
697 1.37.2.2 skrll
698 1.1 ad if (!got) {
699 1.1 ad printf("%s: reset not reported\n", sc->sc_dv.dv_xname);
700 1.1 ad return (-1);
701 1.1 ad }
702 1.1 ad
703 1.1 ad /* Check controller status. */
704 1.22 ad status = twe_inl(sc, TWE_REG_STS);
705 1.1 ad if (twe_status_check(sc, status)) {
706 1.1 ad printf("%s: controller errors detected\n",
707 1.1 ad sc->sc_dv.dv_xname);
708 1.1 ad return (-1);
709 1.1 ad }
710 1.1 ad
711 1.1 ad /* Drain the response queue. */
712 1.1 ad for (;;) {
713 1.22 ad status = twe_inl(sc, TWE_REG_STS);
714 1.1 ad if (twe_status_check(sc, status) != 0) {
715 1.1 ad printf("%s: can't drain response queue\n",
716 1.1 ad sc->sc_dv.dv_xname);
717 1.1 ad return (-1);
718 1.1 ad }
719 1.1 ad if ((status & TWE_STS_RESP_QUEUE_EMPTY) != 0)
720 1.1 ad break;
721 1.22 ad junk = twe_inl(sc, TWE_REG_RESP_QUEUE);
722 1.1 ad }
723 1.1 ad
724 1.1 ad return (0);
725 1.1 ad }
726 1.1 ad
727 1.1 ad /*
728 1.1 ad * Print autoconfiguration message for a sub-device.
729 1.1 ad */
730 1.1 ad static int
731 1.1 ad twe_print(void *aux, const char *pnp)
732 1.1 ad {
733 1.1 ad struct twe_attach_args *twea;
734 1.1 ad
735 1.1 ad twea = aux;
736 1.1 ad
737 1.1 ad if (pnp != NULL)
738 1.35 thorpej aprint_normal("block device at %s", pnp);
739 1.35 thorpej aprint_normal(" unit %d", twea->twea_unit);
740 1.1 ad return (UNCONF);
741 1.1 ad }
742 1.1 ad
743 1.1 ad /*
744 1.1 ad * Match a sub-device.
745 1.1 ad */
746 1.1 ad static int
747 1.37.2.3 skrll twe_submatch(struct device *parent, struct cfdata *cf,
748 1.37.2.3 skrll const locdesc_t *ldesc, void *aux)
749 1.1 ad {
750 1.1 ad
751 1.37.2.3 skrll if (cf->cf_loc[TWECF_UNIT] != TWECF_UNIT_DEFAULT &&
752 1.37.2.3 skrll cf->cf_loc[TWECF_UNIT] != ldesc->locs[TWECF_UNIT])
753 1.1 ad return (0);
754 1.1 ad
755 1.28 thorpej return (config_match(parent, cf, aux));
756 1.1 ad }
757 1.1 ad
758 1.1 ad /*
759 1.1 ad * Interrupt service routine.
760 1.1 ad */
761 1.1 ad static int
762 1.1 ad twe_intr(void *arg)
763 1.1 ad {
764 1.1 ad struct twe_softc *sc;
765 1.1 ad u_int status;
766 1.7 ad int caught, rv;
767 1.1 ad
768 1.1 ad sc = arg;
769 1.1 ad caught = 0;
770 1.22 ad status = twe_inl(sc, TWE_REG_STS);
771 1.1 ad twe_status_check(sc, status);
772 1.1 ad
773 1.1 ad /* Host interrupts - purpose unknown. */
774 1.1 ad if ((status & TWE_STS_HOST_INTR) != 0) {
775 1.37.2.2 skrll #ifdef DEBUG
776 1.1 ad printf("%s: host interrupt\n", sc->sc_dv.dv_xname);
777 1.1 ad #endif
778 1.22 ad twe_outl(sc, TWE_REG_CTL, TWE_CTL_CLEAR_HOST_INTR);
779 1.1 ad caught = 1;
780 1.1 ad }
781 1.1 ad
782 1.1 ad /*
783 1.1 ad * Attention interrupts, signalled when a controller or child device
784 1.18 wiz * state change has occurred.
785 1.1 ad */
786 1.1 ad if ((status & TWE_STS_ATTN_INTR) != 0) {
787 1.37.2.2 skrll rv = twe_aen_get(sc, NULL);
788 1.37.2.2 skrll if (rv != 0)
789 1.37.2.2 skrll printf("%s: unable to retrieve AEN (%d)\n",
790 1.37.2.2 skrll sc->sc_dv.dv_xname, rv);
791 1.37.2.2 skrll else
792 1.37.2.2 skrll twe_outl(sc, TWE_REG_CTL, TWE_CTL_CLEAR_ATTN_INTR);
793 1.1 ad caught = 1;
794 1.1 ad }
795 1.1 ad
796 1.1 ad /*
797 1.1 ad * Command interrupts, signalled when the controller can accept more
798 1.1 ad * commands. We don't use this; instead, we try to submit commands
799 1.1 ad * when we receive them, and when other commands have completed.
800 1.1 ad * Mask it so we don't get another one.
801 1.1 ad */
802 1.1 ad if ((status & TWE_STS_CMD_INTR) != 0) {
803 1.37.2.2 skrll #ifdef DEBUG
804 1.1 ad printf("%s: command interrupt\n", sc->sc_dv.dv_xname);
805 1.1 ad #endif
806 1.22 ad twe_outl(sc, TWE_REG_CTL, TWE_CTL_MASK_CMD_INTR);
807 1.1 ad caught = 1;
808 1.1 ad }
809 1.1 ad
810 1.1 ad if ((status & TWE_STS_RESP_INTR) != 0) {
811 1.1 ad twe_poll(sc);
812 1.1 ad caught = 1;
813 1.1 ad }
814 1.1 ad
815 1.1 ad return (caught);
816 1.1 ad }
817 1.1 ad
818 1.1 ad /*
819 1.37.2.2 skrll * Fetch an AEN. Even though this is really like parameter
820 1.37.2.2 skrll * retrieval, we handle this specially, because we issue this
821 1.37.2.2 skrll * AEN retrieval command from interrupt context, and thus
822 1.37.2.2 skrll * reserve a CCB for it to avoid resource shortage.
823 1.37.2.2 skrll *
824 1.37.2.2 skrll * XXX There are still potential resource shortages we could
825 1.37.2.2 skrll * XXX encounter. Consider pre-allocating all AEN-related
826 1.37.2.2 skrll * XXX resources.
827 1.37.2.2 skrll *
828 1.37.2.2 skrll * MUST BE CALLED AT splbio()!
829 1.37.2.2 skrll */
830 1.37.2.2 skrll static int
831 1.37.2.2 skrll twe_aen_get(struct twe_softc *sc, uint16_t *aenp)
832 1.37.2.2 skrll {
833 1.37.2.2 skrll struct twe_ccb *ccb;
834 1.37.2.2 skrll struct twe_cmd *tc;
835 1.37.2.2 skrll struct twe_param *tp;
836 1.37.2.2 skrll int rv;
837 1.37.2.2 skrll
838 1.37.2.2 skrll /*
839 1.37.2.2 skrll * If we're already retrieving an AEN, just wait; another
840 1.37.2.2 skrll * retrieval will be chained after the current one completes.
841 1.37.2.2 skrll */
842 1.37.2.2 skrll if (sc->sc_flags & TWEF_AEN) {
843 1.37.2.2 skrll /*
844 1.37.2.2 skrll * It is a fatal software programming error to attempt
845 1.37.2.2 skrll * to fetch an AEN synchronously when an AEN fetch is
846 1.37.2.2 skrll * already pending.
847 1.37.2.2 skrll */
848 1.37.2.2 skrll KASSERT(aenp == NULL);
849 1.37.2.2 skrll return (0);
850 1.37.2.2 skrll }
851 1.37.2.2 skrll
852 1.37.2.2 skrll tp = malloc(TWE_SECTOR_SIZE, M_DEVBUF, M_NOWAIT);
853 1.37.2.2 skrll if (tp == NULL)
854 1.37.2.2 skrll return (ENOMEM);
855 1.37.2.2 skrll
856 1.37.2.2 skrll ccb = twe_ccb_alloc(sc,
857 1.37.2.2 skrll TWE_CCB_AEN | TWE_CCB_DATA_IN | TWE_CCB_DATA_OUT);
858 1.37.2.2 skrll KASSERT(ccb != NULL);
859 1.37.2.2 skrll
860 1.37.2.2 skrll ccb->ccb_data = tp;
861 1.37.2.2 skrll ccb->ccb_datasize = TWE_SECTOR_SIZE;
862 1.37.2.2 skrll ccb->ccb_tx.tx_handler = (aenp == NULL) ? twe_aen_handler : NULL;
863 1.37.2.2 skrll ccb->ccb_tx.tx_context = tp;
864 1.37.2.2 skrll ccb->ccb_tx.tx_dv = &sc->sc_dv;
865 1.37.2.2 skrll
866 1.37.2.2 skrll tc = ccb->ccb_cmd;
867 1.37.2.2 skrll tc->tc_size = 2;
868 1.37.2.2 skrll tc->tc_opcode = TWE_OP_GET_PARAM | (tc->tc_size << 5);
869 1.37.2.2 skrll tc->tc_unit = 0;
870 1.37.2.2 skrll tc->tc_count = htole16(1);
871 1.37.2.2 skrll
872 1.37.2.2 skrll /* Fill in the outbound parameter data. */
873 1.37.2.2 skrll tp->tp_table_id = htole16(TWE_PARAM_AEN);
874 1.37.2.2 skrll tp->tp_param_id = TWE_PARAM_AEN_UnitCode;
875 1.37.2.2 skrll tp->tp_param_size = 2;
876 1.37.2.2 skrll
877 1.37.2.2 skrll /* Map the transfer. */
878 1.37.2.2 skrll if ((rv = twe_ccb_map(sc, ccb)) != 0) {
879 1.37.2.2 skrll twe_ccb_free(sc, ccb);
880 1.37.2.2 skrll goto done;
881 1.37.2.2 skrll }
882 1.37.2.2 skrll
883 1.37.2.2 skrll /* Enqueue the command and wait. */
884 1.37.2.2 skrll if (aenp != NULL) {
885 1.37.2.2 skrll rv = twe_ccb_poll(sc, ccb, 5);
886 1.37.2.2 skrll twe_ccb_unmap(sc, ccb);
887 1.37.2.2 skrll twe_ccb_free(sc, ccb);
888 1.37.2.2 skrll if (rv == 0)
889 1.37.2.2 skrll *aenp = le16toh(*(uint16_t *)tp->tp_data);
890 1.37.2.2 skrll free(tp, M_DEVBUF);
891 1.37.2.2 skrll } else {
892 1.37.2.2 skrll sc->sc_flags |= TWEF_AEN;
893 1.37.2.2 skrll twe_ccb_enqueue(sc, ccb);
894 1.37.2.2 skrll rv = 0;
895 1.37.2.2 skrll }
896 1.37.2.2 skrll
897 1.37.2.2 skrll done:
898 1.37.2.2 skrll return (rv);
899 1.37.2.2 skrll }
900 1.37.2.2 skrll
901 1.37.2.2 skrll /*
902 1.1 ad * Handle an AEN returned by the controller.
903 1.37.2.2 skrll * MUST BE CALLED AT splbio()!
904 1.1 ad */
905 1.1 ad static void
906 1.1 ad twe_aen_handler(struct twe_ccb *ccb, int error)
907 1.1 ad {
908 1.1 ad struct twe_softc *sc;
909 1.1 ad struct twe_param *tp;
910 1.37.2.2 skrll uint16_t aen;
911 1.37.2.2 skrll int rv;
912 1.1 ad
913 1.1 ad sc = (struct twe_softc *)ccb->ccb_tx.tx_dv;
914 1.1 ad tp = ccb->ccb_tx.tx_context;
915 1.1 ad twe_ccb_unmap(sc, ccb);
916 1.1 ad
917 1.37.2.2 skrll sc->sc_flags &= ~TWEF_AEN;
918 1.37.2.2 skrll
919 1.3 ad if (error) {
920 1.1 ad printf("%s: error retrieving AEN\n", sc->sc_dv.dv_xname);
921 1.3 ad aen = TWE_AEN_QUEUE_EMPTY;
922 1.3 ad } else
923 1.1 ad aen = le16toh(*(u_int16_t *)tp->tp_data);
924 1.3 ad free(tp, M_DEVBUF);
925 1.3 ad twe_ccb_free(sc, ccb);
926 1.3 ad
927 1.7 ad if (TWE_AEN_CODE(aen) == TWE_AEN_QUEUE_EMPTY) {
928 1.22 ad twe_outl(sc, TWE_REG_CTL, TWE_CTL_CLEAR_ATTN_INTR);
929 1.7 ad return;
930 1.7 ad }
931 1.7 ad
932 1.37.2.2 skrll twe_aen_enqueue(sc, aen, 0);
933 1.3 ad
934 1.7 ad /*
935 1.7 ad * Chain another retrieval in case interrupts have been
936 1.7 ad * coalesced.
937 1.7 ad */
938 1.37.2.2 skrll rv = twe_aen_get(sc, NULL);
939 1.7 ad if (rv != 0)
940 1.7 ad printf("%s: unable to retrieve AEN (%d)\n",
941 1.7 ad sc->sc_dv.dv_xname, rv);
942 1.1 ad }
943 1.1 ad
944 1.37.2.2 skrll static void
945 1.37.2.2 skrll twe_aen_enqueue(struct twe_softc *sc, uint16_t aen, int quiet)
946 1.37.2.2 skrll {
947 1.37.2.2 skrll const char *str, *msg;
948 1.37.2.2 skrll int s, next, nextnext, level;
949 1.37.2.2 skrll
950 1.37.2.2 skrll /*
951 1.37.2.2 skrll * First report the AEN on the console. Maybe.
952 1.37.2.2 skrll */
953 1.37.2.2 skrll if (! quiet) {
954 1.37.2.2 skrll str = twe_describe_code(twe_table_aen, TWE_AEN_CODE(aen));
955 1.37.2.2 skrll if (str == NULL) {
956 1.37.2.2 skrll printf("%s: unknown AEN 0x%04x\n",
957 1.37.2.2 skrll sc->sc_dv.dv_xname, aen);
958 1.37.2.2 skrll } else {
959 1.37.2.2 skrll msg = str + 3;
960 1.37.2.2 skrll switch (str[1]) {
961 1.37.2.2 skrll case 'E': level = LOG_EMERG; break;
962 1.37.2.2 skrll case 'a': level = LOG_ALERT; break;
963 1.37.2.2 skrll case 'c': level = LOG_CRIT; break;
964 1.37.2.2 skrll case 'e': level = LOG_ERR; break;
965 1.37.2.2 skrll case 'w': level = LOG_WARNING; break;
966 1.37.2.2 skrll case 'n': level = LOG_NOTICE; break;
967 1.37.2.2 skrll case 'i': level = LOG_INFO; break;
968 1.37.2.2 skrll case 'd': level = LOG_DEBUG; break;
969 1.37.2.2 skrll default:
970 1.37.2.2 skrll /* Don't use syslog. */
971 1.37.2.2 skrll level = -1;
972 1.37.2.2 skrll }
973 1.37.2.2 skrll
974 1.37.2.2 skrll if (level < 0) {
975 1.37.2.2 skrll switch (str[0]) {
976 1.37.2.2 skrll case 'u':
977 1.37.2.2 skrll case 'p':
978 1.37.2.2 skrll printf("%s: %s %d: %s\n",
979 1.37.2.2 skrll sc->sc_dv.dv_xname,
980 1.37.2.2 skrll str[0] == 'u' ? "unit" : "port",
981 1.37.2.2 skrll TWE_AEN_UNIT(aen), msg);
982 1.37.2.2 skrll break;
983 1.37.2.2 skrll
984 1.37.2.2 skrll default:
985 1.37.2.2 skrll printf("%s: %s\n",
986 1.37.2.2 skrll sc->sc_dv.dv_xname, msg);
987 1.37.2.2 skrll }
988 1.37.2.2 skrll } else {
989 1.37.2.2 skrll switch (str[0]) {
990 1.37.2.2 skrll case 'u':
991 1.37.2.2 skrll case 'p':
992 1.37.2.2 skrll log(level, "%s: %s %d: %s\n",
993 1.37.2.2 skrll sc->sc_dv.dv_xname,
994 1.37.2.2 skrll str[0] == 'u' ? "unit" : "port",
995 1.37.2.2 skrll TWE_AEN_UNIT(aen), msg);
996 1.37.2.2 skrll break;
997 1.37.2.2 skrll
998 1.37.2.2 skrll default:
999 1.37.2.2 skrll log(level, "%s: %s\n",
1000 1.37.2.2 skrll sc->sc_dv.dv_xname, msg);
1001 1.37.2.2 skrll }
1002 1.37.2.2 skrll }
1003 1.37.2.2 skrll }
1004 1.37.2.2 skrll }
1005 1.37.2.2 skrll
1006 1.37.2.2 skrll /* Now enqueue the AEN for mangement tools. */
1007 1.37.2.2 skrll s = splbio();
1008 1.37.2.2 skrll
1009 1.37.2.2 skrll next = (sc->sc_aen_head + 1) % TWE_AEN_Q_LENGTH;
1010 1.37.2.2 skrll nextnext = (sc->sc_aen_head + 2) % TWE_AEN_Q_LENGTH;
1011 1.37.2.2 skrll
1012 1.37.2.2 skrll /*
1013 1.37.2.2 skrll * If this is the last free slot, then queue up a "queue
1014 1.37.2.2 skrll * full" message.
1015 1.37.2.2 skrll */
1016 1.37.2.2 skrll if (nextnext == sc->sc_aen_tail)
1017 1.37.2.2 skrll aen = TWE_AEN_QUEUE_FULL;
1018 1.37.2.2 skrll
1019 1.37.2.2 skrll if (next != sc->sc_aen_tail) {
1020 1.37.2.2 skrll sc->sc_aen_queue[sc->sc_aen_head] = aen;
1021 1.37.2.2 skrll sc->sc_aen_head = next;
1022 1.37.2.2 skrll }
1023 1.37.2.2 skrll
1024 1.37.2.2 skrll if (sc->sc_flags & TWEF_AENQ_WAIT) {
1025 1.37.2.2 skrll sc->sc_flags &= ~TWEF_AENQ_WAIT;
1026 1.37.2.2 skrll wakeup(&sc->sc_aen_queue);
1027 1.37.2.2 skrll }
1028 1.37.2.2 skrll
1029 1.37.2.2 skrll splx(s);
1030 1.37.2.2 skrll }
1031 1.37.2.2 skrll
1032 1.37.2.2 skrll /* NOTE: Must be called at splbio(). */
1033 1.37.2.2 skrll static uint16_t
1034 1.37.2.2 skrll twe_aen_dequeue(struct twe_softc *sc)
1035 1.37.2.2 skrll {
1036 1.37.2.2 skrll uint16_t aen;
1037 1.37.2.2 skrll
1038 1.37.2.2 skrll if (sc->sc_aen_tail == sc->sc_aen_head)
1039 1.37.2.2 skrll aen = TWE_AEN_QUEUE_EMPTY;
1040 1.37.2.2 skrll else {
1041 1.37.2.2 skrll aen = sc->sc_aen_queue[sc->sc_aen_tail];
1042 1.37.2.2 skrll sc->sc_aen_tail = (sc->sc_aen_tail + 1) & TWE_AEN_Q_LENGTH;
1043 1.37.2.2 skrll }
1044 1.37.2.2 skrll
1045 1.37.2.2 skrll return (aen);
1046 1.37.2.2 skrll }
1047 1.37.2.2 skrll
1048 1.37.2.2 skrll /*
1049 1.37.2.2 skrll * These are short-hand functions that execute TWE_OP_GET_PARAM to
1050 1.37.2.2 skrll * fetch 1, 2, and 4 byte parameter values, respectively.
1051 1.37.2.2 skrll */
1052 1.37.2.2 skrll int
1053 1.37.2.2 skrll twe_param_get_1(struct twe_softc *sc, int table_id, int param_id,
1054 1.37.2.2 skrll uint8_t *valp)
1055 1.37.2.2 skrll {
1056 1.37.2.2 skrll struct twe_param *tp;
1057 1.37.2.2 skrll int rv;
1058 1.37.2.2 skrll
1059 1.37.2.2 skrll rv = twe_param_get(sc, table_id, param_id, 1, NULL, &tp);
1060 1.37.2.2 skrll if (rv != 0)
1061 1.37.2.2 skrll return (rv);
1062 1.37.2.2 skrll *valp = *(uint8_t *)tp->tp_data;
1063 1.37.2.2 skrll free(tp, M_DEVBUF);
1064 1.37.2.2 skrll return (0);
1065 1.37.2.2 skrll }
1066 1.37.2.2 skrll
1067 1.37.2.2 skrll int
1068 1.37.2.2 skrll twe_param_get_2(struct twe_softc *sc, int table_id, int param_id,
1069 1.37.2.2 skrll uint16_t *valp)
1070 1.37.2.2 skrll {
1071 1.37.2.2 skrll struct twe_param *tp;
1072 1.37.2.2 skrll int rv;
1073 1.37.2.2 skrll
1074 1.37.2.2 skrll rv = twe_param_get(sc, table_id, param_id, 2, NULL, &tp);
1075 1.37.2.2 skrll if (rv != 0)
1076 1.37.2.2 skrll return (rv);
1077 1.37.2.2 skrll *valp = le16toh(*(uint16_t *)tp->tp_data);
1078 1.37.2.2 skrll free(tp, M_DEVBUF);
1079 1.37.2.2 skrll return (0);
1080 1.37.2.2 skrll }
1081 1.37.2.2 skrll
1082 1.37.2.2 skrll int
1083 1.37.2.2 skrll twe_param_get_4(struct twe_softc *sc, int table_id, int param_id,
1084 1.37.2.2 skrll uint32_t *valp)
1085 1.37.2.2 skrll {
1086 1.37.2.2 skrll struct twe_param *tp;
1087 1.37.2.2 skrll int rv;
1088 1.37.2.2 skrll
1089 1.37.2.2 skrll rv = twe_param_get(sc, table_id, param_id, 4, NULL, &tp);
1090 1.37.2.2 skrll if (rv != 0)
1091 1.37.2.2 skrll return (rv);
1092 1.37.2.2 skrll *valp = le32toh(*(uint32_t *)tp->tp_data);
1093 1.37.2.2 skrll free(tp, M_DEVBUF);
1094 1.37.2.2 skrll return (0);
1095 1.37.2.2 skrll }
1096 1.37.2.2 skrll
1097 1.1 ad /*
1098 1.1 ad * Execute a TWE_OP_GET_PARAM command. If a callback function is provided,
1099 1.1 ad * it will be called with generated context when the command has completed.
1100 1.1 ad * If no callback is provided, the command will be executed synchronously
1101 1.3 ad * and a pointer to a buffer containing the data returned.
1102 1.1 ad *
1103 1.3 ad * The caller or callback is responsible for freeing the buffer.
1104 1.37.2.2 skrll *
1105 1.37.2.2 skrll * NOTE: We assume we can sleep here to wait for a CCB to become available.
1106 1.1 ad */
1107 1.37.2.2 skrll int
1108 1.1 ad twe_param_get(struct twe_softc *sc, int table_id, int param_id, size_t size,
1109 1.37.2.2 skrll void (*func)(struct twe_ccb *, int), struct twe_param **pbuf)
1110 1.1 ad {
1111 1.1 ad struct twe_ccb *ccb;
1112 1.1 ad struct twe_cmd *tc;
1113 1.1 ad struct twe_param *tp;
1114 1.1 ad int rv, s;
1115 1.1 ad
1116 1.33 christos tp = malloc(TWE_SECTOR_SIZE, M_DEVBUF, M_NOWAIT);
1117 1.33 christos if (tp == NULL)
1118 1.33 christos return ENOMEM;
1119 1.33 christos
1120 1.37.2.2 skrll ccb = twe_ccb_alloc_wait(sc, TWE_CCB_DATA_IN | TWE_CCB_DATA_OUT);
1121 1.37.2.2 skrll KASSERT(ccb != NULL);
1122 1.1 ad
1123 1.1 ad ccb->ccb_data = tp;
1124 1.1 ad ccb->ccb_datasize = TWE_SECTOR_SIZE;
1125 1.1 ad ccb->ccb_tx.tx_handler = func;
1126 1.1 ad ccb->ccb_tx.tx_context = tp;
1127 1.1 ad ccb->ccb_tx.tx_dv = &sc->sc_dv;
1128 1.1 ad
1129 1.1 ad tc = ccb->ccb_cmd;
1130 1.1 ad tc->tc_size = 2;
1131 1.1 ad tc->tc_opcode = TWE_OP_GET_PARAM | (tc->tc_size << 5);
1132 1.1 ad tc->tc_unit = 0;
1133 1.1 ad tc->tc_count = htole16(1);
1134 1.1 ad
1135 1.1 ad /* Fill in the outbound parameter data. */
1136 1.1 ad tp->tp_table_id = htole16(table_id);
1137 1.1 ad tp->tp_param_id = param_id;
1138 1.1 ad tp->tp_param_size = size;
1139 1.1 ad
1140 1.1 ad /* Map the transfer. */
1141 1.7 ad if ((rv = twe_ccb_map(sc, ccb)) != 0) {
1142 1.2 ad twe_ccb_free(sc, ccb);
1143 1.33 christos goto done;
1144 1.1 ad }
1145 1.1 ad
1146 1.1 ad /* Submit the command and either wait or let the callback handle it. */
1147 1.1 ad if (func == NULL) {
1148 1.1 ad s = splbio();
1149 1.7 ad rv = twe_ccb_poll(sc, ccb, 5);
1150 1.1 ad twe_ccb_unmap(sc, ccb);
1151 1.2 ad twe_ccb_free(sc, ccb);
1152 1.1 ad splx(s);
1153 1.1 ad } else {
1154 1.37.2.2 skrll #ifdef DEBUG
1155 1.33 christos if (pbuf != NULL)
1156 1.33 christos panic("both func and pbuf defined");
1157 1.33 christos #endif
1158 1.1 ad twe_ccb_enqueue(sc, ccb);
1159 1.33 christos return 0;
1160 1.33 christos }
1161 1.33 christos
1162 1.33 christos done:
1163 1.33 christos if (pbuf == NULL || rv != 0)
1164 1.33 christos free(tp, M_DEVBUF);
1165 1.33 christos else if (pbuf != NULL && rv == 0)
1166 1.33 christos *pbuf = tp;
1167 1.33 christos return rv;
1168 1.33 christos }
1169 1.33 christos
1170 1.33 christos /*
1171 1.33 christos * Execute a TWE_OP_SET_PARAM command.
1172 1.37.2.2 skrll *
1173 1.37.2.2 skrll * NOTE: We assume we can sleep here to wait for a CCB to become available.
1174 1.33 christos */
1175 1.33 christos static int
1176 1.33 christos twe_param_set(struct twe_softc *sc, int table_id, int param_id, size_t size,
1177 1.33 christos void *buf)
1178 1.33 christos {
1179 1.33 christos struct twe_ccb *ccb;
1180 1.33 christos struct twe_cmd *tc;
1181 1.33 christos struct twe_param *tp;
1182 1.33 christos int rv, s;
1183 1.33 christos
1184 1.33 christos tp = malloc(TWE_SECTOR_SIZE, M_DEVBUF, M_NOWAIT);
1185 1.33 christos if (tp == NULL)
1186 1.33 christos return ENOMEM;
1187 1.33 christos
1188 1.37.2.2 skrll ccb = twe_ccb_alloc_wait(sc, TWE_CCB_DATA_IN | TWE_CCB_DATA_OUT);
1189 1.37.2.2 skrll KASSERT(ccb != NULL);
1190 1.33 christos
1191 1.33 christos ccb->ccb_data = tp;
1192 1.33 christos ccb->ccb_datasize = TWE_SECTOR_SIZE;
1193 1.33 christos ccb->ccb_tx.tx_handler = 0;
1194 1.33 christos ccb->ccb_tx.tx_context = tp;
1195 1.33 christos ccb->ccb_tx.tx_dv = &sc->sc_dv;
1196 1.33 christos
1197 1.33 christos tc = ccb->ccb_cmd;
1198 1.33 christos tc->tc_size = 2;
1199 1.33 christos tc->tc_opcode = TWE_OP_SET_PARAM | (tc->tc_size << 5);
1200 1.33 christos tc->tc_unit = 0;
1201 1.33 christos tc->tc_count = htole16(1);
1202 1.33 christos
1203 1.33 christos /* Fill in the outbound parameter data. */
1204 1.33 christos tp->tp_table_id = htole16(table_id);
1205 1.33 christos tp->tp_param_id = param_id;
1206 1.33 christos tp->tp_param_size = size;
1207 1.33 christos memcpy(tp->tp_data, buf, size);
1208 1.33 christos
1209 1.33 christos /* Map the transfer. */
1210 1.33 christos if ((rv = twe_ccb_map(sc, ccb)) != 0) {
1211 1.33 christos twe_ccb_free(sc, ccb);
1212 1.33 christos goto done;
1213 1.1 ad }
1214 1.1 ad
1215 1.33 christos /* Submit the command and wait. */
1216 1.33 christos s = splbio();
1217 1.33 christos rv = twe_ccb_poll(sc, ccb, 5);
1218 1.33 christos twe_ccb_unmap(sc, ccb);
1219 1.33 christos twe_ccb_free(sc, ccb);
1220 1.33 christos splx(s);
1221 1.33 christos done:
1222 1.33 christos free(tp, M_DEVBUF);
1223 1.7 ad return (rv);
1224 1.1 ad }
1225 1.1 ad
1226 1.1 ad /*
1227 1.1 ad * Execute a TWE_OP_INIT_CONNECTION command. Return non-zero on error.
1228 1.1 ad * Must be called with interrupts blocked.
1229 1.1 ad */
1230 1.1 ad static int
1231 1.1 ad twe_init_connection(struct twe_softc *sc)
1232 1.33 christos /*###762 [cc] warning: `twe_init_connection' was used with no prototype before its definition%%%*/
1233 1.33 christos /*###762 [cc] warning: `twe_init_connection' was declared implicitly `extern' and later `static'%%%*/
1234 1.1 ad {
1235 1.1 ad struct twe_ccb *ccb;
1236 1.1 ad struct twe_cmd *tc;
1237 1.1 ad int rv;
1238 1.1 ad
1239 1.37.2.2 skrll if ((ccb = twe_ccb_alloc(sc, 0)) == NULL)
1240 1.37.2.2 skrll return (EAGAIN);
1241 1.1 ad
1242 1.1 ad /* Build the command. */
1243 1.1 ad tc = ccb->ccb_cmd;
1244 1.1 ad tc->tc_size = 3;
1245 1.1 ad tc->tc_opcode = TWE_OP_INIT_CONNECTION;
1246 1.1 ad tc->tc_unit = 0;
1247 1.3 ad tc->tc_count = htole16(TWE_MAX_CMDS);
1248 1.1 ad tc->tc_args.init_connection.response_queue_pointer = 0;
1249 1.1 ad
1250 1.1 ad /* Submit the command for immediate execution. */
1251 1.7 ad rv = twe_ccb_poll(sc, ccb, 5);
1252 1.2 ad twe_ccb_free(sc, ccb);
1253 1.1 ad return (rv);
1254 1.1 ad }
1255 1.1 ad
1256 1.1 ad /*
1257 1.1 ad * Poll the controller for completed commands. Must be called with
1258 1.1 ad * interrupts blocked.
1259 1.1 ad */
1260 1.1 ad static void
1261 1.1 ad twe_poll(struct twe_softc *sc)
1262 1.1 ad {
1263 1.1 ad struct twe_ccb *ccb;
1264 1.1 ad int found;
1265 1.1 ad u_int status, cmdid;
1266 1.1 ad
1267 1.1 ad found = 0;
1268 1.1 ad
1269 1.1 ad for (;;) {
1270 1.22 ad status = twe_inl(sc, TWE_REG_STS);
1271 1.1 ad twe_status_check(sc, status);
1272 1.1 ad
1273 1.1 ad if ((status & TWE_STS_RESP_QUEUE_EMPTY))
1274 1.1 ad break;
1275 1.1 ad
1276 1.1 ad found = 1;
1277 1.22 ad cmdid = twe_inl(sc, TWE_REG_RESP_QUEUE);
1278 1.1 ad cmdid = (cmdid & TWE_RESP_MASK) >> TWE_RESP_SHIFT;
1279 1.7 ad if (cmdid >= TWE_MAX_QUEUECNT) {
1280 1.37.2.2 skrll printf("%s: bad cmdid %d\n", sc->sc_dv.dv_xname, cmdid);
1281 1.1 ad continue;
1282 1.1 ad }
1283 1.1 ad
1284 1.1 ad ccb = sc->sc_ccbs + cmdid;
1285 1.1 ad if ((ccb->ccb_flags & TWE_CCB_ACTIVE) == 0) {
1286 1.37.2.2 skrll printf("%s: CCB for cmdid %d not active\n",
1287 1.37.2.2 skrll sc->sc_dv.dv_xname, cmdid);
1288 1.1 ad continue;
1289 1.1 ad }
1290 1.1 ad ccb->ccb_flags ^= TWE_CCB_COMPLETE | TWE_CCB_ACTIVE;
1291 1.1 ad
1292 1.1 ad bus_dmamap_sync(sc->sc_dmat, sc->sc_dmamap,
1293 1.1 ad (caddr_t)ccb->ccb_cmd - sc->sc_cmds,
1294 1.1 ad sizeof(struct twe_cmd),
1295 1.1 ad BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
1296 1.1 ad
1297 1.1 ad /* Pass notification to upper layers. */
1298 1.1 ad if (ccb->ccb_tx.tx_handler != NULL)
1299 1.1 ad (*ccb->ccb_tx.tx_handler)(ccb,
1300 1.1 ad ccb->ccb_cmd->tc_status != 0 ? EIO : 0);
1301 1.1 ad }
1302 1.1 ad
1303 1.1 ad /* If any commands have completed, run the software queue. */
1304 1.1 ad if (found)
1305 1.1 ad twe_ccb_enqueue(sc, NULL);
1306 1.1 ad }
1307 1.1 ad
1308 1.1 ad /*
1309 1.1 ad * Wait for `status' to be set in the controller status register. Return
1310 1.1 ad * zero if found, non-zero if the operation timed out.
1311 1.1 ad */
1312 1.1 ad static int
1313 1.1 ad twe_status_wait(struct twe_softc *sc, u_int32_t status, int timo)
1314 1.1 ad {
1315 1.1 ad
1316 1.11 ad for (timo *= 10; timo != 0; timo--) {
1317 1.22 ad if ((twe_inl(sc, TWE_REG_STS) & status) == status)
1318 1.1 ad break;
1319 1.1 ad delay(100000);
1320 1.1 ad }
1321 1.1 ad
1322 1.1 ad return (timo == 0);
1323 1.1 ad }
1324 1.1 ad
1325 1.1 ad /*
1326 1.1 ad * Complain if the status bits aren't what we expect.
1327 1.1 ad */
1328 1.1 ad static int
1329 1.1 ad twe_status_check(struct twe_softc *sc, u_int status)
1330 1.1 ad {
1331 1.1 ad int rv;
1332 1.1 ad
1333 1.1 ad rv = 0;
1334 1.1 ad
1335 1.1 ad if ((status & TWE_STS_EXPECTED_BITS) != TWE_STS_EXPECTED_BITS) {
1336 1.1 ad printf("%s: missing status bits: 0x%08x\n", sc->sc_dv.dv_xname,
1337 1.1 ad status & ~TWE_STS_EXPECTED_BITS);
1338 1.1 ad rv = -1;
1339 1.1 ad }
1340 1.1 ad
1341 1.1 ad if ((status & TWE_STS_UNEXPECTED_BITS) != 0) {
1342 1.1 ad printf("%s: unexpected status bits: 0x%08x\n",
1343 1.1 ad sc->sc_dv.dv_xname, status & TWE_STS_UNEXPECTED_BITS);
1344 1.1 ad rv = -1;
1345 1.1 ad }
1346 1.1 ad
1347 1.1 ad return (rv);
1348 1.1 ad }
1349 1.1 ad
1350 1.1 ad /*
1351 1.1 ad * Allocate and initialise a CCB.
1352 1.1 ad */
1353 1.37.2.2 skrll static __inline void
1354 1.37.2.2 skrll twe_ccb_init(struct twe_softc *sc, struct twe_ccb *ccb, int flags)
1355 1.1 ad {
1356 1.1 ad struct twe_cmd *tc;
1357 1.37.2.2 skrll
1358 1.37.2.2 skrll ccb->ccb_tx.tx_handler = NULL;
1359 1.37.2.2 skrll ccb->ccb_flags = flags;
1360 1.37.2.2 skrll tc = ccb->ccb_cmd;
1361 1.37.2.2 skrll tc->tc_status = 0;
1362 1.37.2.2 skrll tc->tc_flags = 0;
1363 1.37.2.2 skrll tc->tc_cmdid = ccb->ccb_cmdid;
1364 1.37.2.2 skrll }
1365 1.37.2.2 skrll
1366 1.37.2.2 skrll struct twe_ccb *
1367 1.37.2.2 skrll twe_ccb_alloc(struct twe_softc *sc, int flags)
1368 1.37.2.2 skrll {
1369 1.1 ad struct twe_ccb *ccb;
1370 1.1 ad int s;
1371 1.1 ad
1372 1.7 ad s = splbio();
1373 1.37.2.2 skrll if (__predict_false((flags & TWE_CCB_AEN) != 0)) {
1374 1.37.2.2 skrll /* Use the reserved CCB. */
1375 1.3 ad ccb = sc->sc_ccbs;
1376 1.37.2.2 skrll } else {
1377 1.3 ad /* Allocate a CCB and command block. */
1378 1.37.2.2 skrll if (__predict_false((ccb =
1379 1.37.2.2 skrll SLIST_FIRST(&sc->sc_ccb_freelist)) == NULL)) {
1380 1.1 ad splx(s);
1381 1.37.2.2 skrll return (NULL);
1382 1.1 ad }
1383 1.3 ad SLIST_REMOVE_HEAD(&sc->sc_ccb_freelist, ccb_chain.slist);
1384 1.1 ad }
1385 1.3 ad #ifdef DIAGNOSTIC
1386 1.37.2.2 skrll if ((long)(ccb - sc->sc_ccbs) == 0 && (flags & TWE_CCB_AEN) == 0)
1387 1.37.2.2 skrll panic("twe_ccb_alloc: got reserved CCB for non-AEN");
1388 1.3 ad if ((ccb->ccb_flags & TWE_CCB_ALLOCED) != 0)
1389 1.37.2.2 skrll panic("twe_ccb_alloc: CCB %ld already allocated",
1390 1.37.2.2 skrll (long)(ccb - sc->sc_ccbs));
1391 1.3 ad flags |= TWE_CCB_ALLOCED;
1392 1.3 ad #endif
1393 1.7 ad splx(s);
1394 1.1 ad
1395 1.37.2.2 skrll twe_ccb_init(sc, ccb, flags);
1396 1.37.2.2 skrll return (ccb);
1397 1.37.2.2 skrll }
1398 1.1 ad
1399 1.37.2.2 skrll struct twe_ccb *
1400 1.37.2.2 skrll twe_ccb_alloc_wait(struct twe_softc *sc, int flags)
1401 1.37.2.2 skrll {
1402 1.37.2.2 skrll struct twe_ccb *ccb;
1403 1.37.2.2 skrll int s;
1404 1.37.2.2 skrll
1405 1.37.2.2 skrll KASSERT((flags & TWE_CCB_AEN) == 0);
1406 1.37.2.2 skrll
1407 1.37.2.2 skrll s = splbio();
1408 1.37.2.2 skrll while (__predict_false((ccb =
1409 1.37.2.2 skrll SLIST_FIRST(&sc->sc_ccb_freelist)) == NULL)) {
1410 1.37.2.2 skrll sc->sc_flags |= TWEF_WAIT_CCB;
1411 1.37.2.2 skrll (void) tsleep(&sc->sc_ccb_freelist, PRIBIO, "tweccb", 0);
1412 1.37.2.2 skrll }
1413 1.37.2.2 skrll SLIST_REMOVE_HEAD(&sc->sc_ccb_freelist, ccb_chain.slist);
1414 1.37.2.2 skrll #ifdef DIAGNOSTIC
1415 1.37.2.2 skrll if ((ccb->ccb_flags & TWE_CCB_ALLOCED) != 0)
1416 1.37.2.2 skrll panic("twe_ccb_alloc_wait: CCB %ld already allocated",
1417 1.37.2.2 skrll (long)(ccb - sc->sc_ccbs));
1418 1.37.2.2 skrll flags |= TWE_CCB_ALLOCED;
1419 1.37.2.2 skrll #endif
1420 1.37.2.2 skrll splx(s);
1421 1.37.2.2 skrll
1422 1.37.2.2 skrll twe_ccb_init(sc, ccb, flags);
1423 1.37.2.2 skrll return (ccb);
1424 1.1 ad }
1425 1.1 ad
1426 1.1 ad /*
1427 1.3 ad * Free a CCB.
1428 1.1 ad */
1429 1.1 ad void
1430 1.2 ad twe_ccb_free(struct twe_softc *sc, struct twe_ccb *ccb)
1431 1.1 ad {
1432 1.1 ad int s;
1433 1.1 ad
1434 1.3 ad s = splbio();
1435 1.37.2.2 skrll if ((ccb->ccb_flags & TWE_CCB_AEN) == 0) {
1436 1.3 ad SLIST_INSERT_HEAD(&sc->sc_ccb_freelist, ccb, ccb_chain.slist);
1437 1.37.2.2 skrll if (__predict_false((sc->sc_flags & TWEF_WAIT_CCB) != 0)) {
1438 1.37.2.2 skrll sc->sc_flags &= ~TWEF_WAIT_CCB;
1439 1.37.2.2 skrll wakeup(&sc->sc_ccb_freelist);
1440 1.37.2.2 skrll }
1441 1.37.2.2 skrll }
1442 1.1 ad ccb->ccb_flags = 0;
1443 1.1 ad splx(s);
1444 1.1 ad }
1445 1.1 ad
1446 1.1 ad /*
1447 1.1 ad * Map the specified CCB's command block and data buffer (if any) into
1448 1.1 ad * controller visible space. Perform DMA synchronisation.
1449 1.1 ad */
1450 1.1 ad int
1451 1.1 ad twe_ccb_map(struct twe_softc *sc, struct twe_ccb *ccb)
1452 1.1 ad {
1453 1.1 ad struct twe_cmd *tc;
1454 1.20 ad int flags, nsegs, i, s, rv;
1455 1.1 ad void *data;
1456 1.1 ad
1457 1.7 ad /*
1458 1.7 ad * The data as a whole must be 512-byte aligned.
1459 1.7 ad */
1460 1.1 ad if (((u_long)ccb->ccb_data & (TWE_ALIGNMENT - 1)) != 0) {
1461 1.20 ad s = splvm();
1462 1.20 ad /* XXX */
1463 1.20 ad ccb->ccb_abuf = uvm_km_kmemalloc(kmem_map, NULL,
1464 1.20 ad ccb->ccb_datasize, UVM_KMF_NOWAIT);
1465 1.20 ad splx(s);
1466 1.20 ad data = (void *)ccb->ccb_abuf;
1467 1.2 ad if ((ccb->ccb_flags & TWE_CCB_DATA_OUT) != 0)
1468 1.2 ad memcpy(data, ccb->ccb_data, ccb->ccb_datasize);
1469 1.1 ad } else {
1470 1.20 ad ccb->ccb_abuf = (vaddr_t)0;
1471 1.1 ad data = ccb->ccb_data;
1472 1.1 ad }
1473 1.1 ad
1474 1.7 ad /*
1475 1.7 ad * Map the data buffer into bus space and build the S/G list.
1476 1.7 ad */
1477 1.7 ad rv = bus_dmamap_load(sc->sc_dmat, ccb->ccb_dmamap_xfer, data,
1478 1.16 thorpej ccb->ccb_datasize, NULL, BUS_DMA_NOWAIT | BUS_DMA_STREAMING |
1479 1.16 thorpej ((ccb->ccb_flags & TWE_CCB_DATA_IN) ?
1480 1.22 ad BUS_DMA_READ : BUS_DMA_WRITE));
1481 1.7 ad if (rv != 0) {
1482 1.20 ad if (ccb->ccb_abuf != (vaddr_t)0) {
1483 1.20 ad s = splvm();
1484 1.20 ad /* XXX */
1485 1.20 ad uvm_km_free(kmem_map, ccb->ccb_abuf,
1486 1.7 ad ccb->ccb_datasize);
1487 1.20 ad splx(s);
1488 1.7 ad }
1489 1.7 ad return (rv);
1490 1.7 ad }
1491 1.1 ad
1492 1.1 ad nsegs = ccb->ccb_dmamap_xfer->dm_nsegs;
1493 1.1 ad tc = ccb->ccb_cmd;
1494 1.1 ad tc->tc_size += 2 * nsegs;
1495 1.1 ad
1496 1.1 ad /* The location of the S/G list is dependant upon command type. */
1497 1.1 ad switch (tc->tc_opcode >> 5) {
1498 1.1 ad case 2:
1499 1.1 ad for (i = 0; i < nsegs; i++) {
1500 1.1 ad tc->tc_args.param.sgl[i].tsg_address =
1501 1.1 ad htole32(ccb->ccb_dmamap_xfer->dm_segs[i].ds_addr);
1502 1.1 ad tc->tc_args.param.sgl[i].tsg_length =
1503 1.1 ad htole32(ccb->ccb_dmamap_xfer->dm_segs[i].ds_len);
1504 1.1 ad }
1505 1.1 ad /* XXX Needed? */
1506 1.1 ad for (; i < TWE_SG_SIZE; i++) {
1507 1.1 ad tc->tc_args.param.sgl[i].tsg_address = 0;
1508 1.1 ad tc->tc_args.param.sgl[i].tsg_length = 0;
1509 1.1 ad }
1510 1.1 ad break;
1511 1.1 ad case 3:
1512 1.1 ad for (i = 0; i < nsegs; i++) {
1513 1.1 ad tc->tc_args.io.sgl[i].tsg_address =
1514 1.1 ad htole32(ccb->ccb_dmamap_xfer->dm_segs[i].ds_addr);
1515 1.1 ad tc->tc_args.io.sgl[i].tsg_length =
1516 1.1 ad htole32(ccb->ccb_dmamap_xfer->dm_segs[i].ds_len);
1517 1.1 ad }
1518 1.1 ad /* XXX Needed? */
1519 1.1 ad for (; i < TWE_SG_SIZE; i++) {
1520 1.1 ad tc->tc_args.io.sgl[i].tsg_address = 0;
1521 1.1 ad tc->tc_args.io.sgl[i].tsg_length = 0;
1522 1.1 ad }
1523 1.1 ad break;
1524 1.1 ad default:
1525 1.37.2.2 skrll /*
1526 1.37.2.2 skrll * In all likelihood, this is a command passed from
1527 1.37.2.2 skrll * management tools in userspace where no S/G list is
1528 1.37.2.2 skrll * necessary because no data is being passed.
1529 1.37.2.2 skrll */
1530 1.37.2.2 skrll break;
1531 1.1 ad }
1532 1.1 ad
1533 1.1 ad if ((ccb->ccb_flags & TWE_CCB_DATA_IN) != 0)
1534 1.1 ad flags = BUS_DMASYNC_PREREAD;
1535 1.1 ad else
1536 1.1 ad flags = 0;
1537 1.1 ad if ((ccb->ccb_flags & TWE_CCB_DATA_OUT) != 0)
1538 1.1 ad flags |= BUS_DMASYNC_PREWRITE;
1539 1.1 ad
1540 1.1 ad bus_dmamap_sync(sc->sc_dmat, ccb->ccb_dmamap_xfer, 0,
1541 1.1 ad ccb->ccb_datasize, flags);
1542 1.1 ad return (0);
1543 1.1 ad }
1544 1.1 ad
1545 1.1 ad /*
1546 1.1 ad * Unmap the specified CCB's command block and data buffer (if any) and
1547 1.1 ad * perform DMA synchronisation.
1548 1.1 ad */
1549 1.1 ad void
1550 1.1 ad twe_ccb_unmap(struct twe_softc *sc, struct twe_ccb *ccb)
1551 1.1 ad {
1552 1.20 ad int flags, s;
1553 1.1 ad
1554 1.1 ad if ((ccb->ccb_flags & TWE_CCB_DATA_IN) != 0)
1555 1.1 ad flags = BUS_DMASYNC_POSTREAD;
1556 1.1 ad else
1557 1.1 ad flags = 0;
1558 1.1 ad if ((ccb->ccb_flags & TWE_CCB_DATA_OUT) != 0)
1559 1.1 ad flags |= BUS_DMASYNC_POSTWRITE;
1560 1.1 ad
1561 1.1 ad bus_dmamap_sync(sc->sc_dmat, ccb->ccb_dmamap_xfer, 0,
1562 1.1 ad ccb->ccb_datasize, flags);
1563 1.1 ad bus_dmamap_unload(sc->sc_dmat, ccb->ccb_dmamap_xfer);
1564 1.1 ad
1565 1.20 ad if (ccb->ccb_abuf != (vaddr_t)0) {
1566 1.2 ad if ((ccb->ccb_flags & TWE_CCB_DATA_IN) != 0)
1567 1.20 ad memcpy(ccb->ccb_data, (void *)ccb->ccb_abuf,
1568 1.2 ad ccb->ccb_datasize);
1569 1.20 ad s = splvm();
1570 1.20 ad /* XXX */
1571 1.20 ad uvm_km_free(kmem_map, ccb->ccb_abuf, ccb->ccb_datasize);
1572 1.20 ad splx(s);
1573 1.1 ad }
1574 1.1 ad }
1575 1.1 ad
1576 1.1 ad /*
1577 1.7 ad * Submit a command to the controller and poll on completion. Return
1578 1.7 ad * non-zero on timeout (but don't check status, as some command types don't
1579 1.7 ad * return status). Must be called with interrupts blocked.
1580 1.1 ad */
1581 1.1 ad int
1582 1.1 ad twe_ccb_poll(struct twe_softc *sc, struct twe_ccb *ccb, int timo)
1583 1.1 ad {
1584 1.7 ad int rv;
1585 1.7 ad
1586 1.7 ad if ((rv = twe_ccb_submit(sc, ccb)) != 0)
1587 1.7 ad return (rv);
1588 1.1 ad
1589 1.15 ad for (timo *= 1000; timo != 0; timo--) {
1590 1.1 ad twe_poll(sc);
1591 1.1 ad if ((ccb->ccb_flags & TWE_CCB_COMPLETE) != 0)
1592 1.1 ad break;
1593 1.15 ad DELAY(100);
1594 1.1 ad }
1595 1.1 ad
1596 1.1 ad return (timo == 0);
1597 1.1 ad }
1598 1.1 ad
1599 1.1 ad /*
1600 1.1 ad * If a CCB is specified, enqueue it. Pull CCBs off the software queue in
1601 1.1 ad * the order that they were enqueued and try to submit their command blocks
1602 1.1 ad * to the controller for execution.
1603 1.1 ad */
1604 1.1 ad void
1605 1.1 ad twe_ccb_enqueue(struct twe_softc *sc, struct twe_ccb *ccb)
1606 1.1 ad {
1607 1.1 ad int s;
1608 1.1 ad
1609 1.1 ad s = splbio();
1610 1.1 ad
1611 1.1 ad if (ccb != NULL)
1612 1.1 ad SIMPLEQ_INSERT_TAIL(&sc->sc_ccb_queue, ccb, ccb_chain.simpleq);
1613 1.1 ad
1614 1.1 ad while ((ccb = SIMPLEQ_FIRST(&sc->sc_ccb_queue)) != NULL) {
1615 1.1 ad if (twe_ccb_submit(sc, ccb))
1616 1.1 ad break;
1617 1.25 lukem SIMPLEQ_REMOVE_HEAD(&sc->sc_ccb_queue, ccb_chain.simpleq);
1618 1.1 ad }
1619 1.1 ad
1620 1.1 ad splx(s);
1621 1.1 ad }
1622 1.1 ad
1623 1.1 ad /*
1624 1.1 ad * Submit the command block associated with the specified CCB to the
1625 1.1 ad * controller for execution. Must be called with interrupts blocked.
1626 1.1 ad */
1627 1.1 ad int
1628 1.1 ad twe_ccb_submit(struct twe_softc *sc, struct twe_ccb *ccb)
1629 1.1 ad {
1630 1.1 ad bus_addr_t pa;
1631 1.1 ad int rv;
1632 1.1 ad u_int status;
1633 1.1 ad
1634 1.1 ad /* Check to see if we can post a command. */
1635 1.22 ad status = twe_inl(sc, TWE_REG_STS);
1636 1.1 ad twe_status_check(sc, status);
1637 1.1 ad
1638 1.1 ad if ((status & TWE_STS_CMD_QUEUE_FULL) == 0) {
1639 1.1 ad bus_dmamap_sync(sc->sc_dmat, sc->sc_dmamap,
1640 1.1 ad (caddr_t)ccb->ccb_cmd - sc->sc_cmds, sizeof(struct twe_cmd),
1641 1.1 ad BUS_DMASYNC_PREWRITE | BUS_DMASYNC_PREREAD);
1642 1.37.2.2 skrll #ifdef DIAGNOSTIC
1643 1.37.2.2 skrll if ((ccb->ccb_flags & TWE_CCB_ALLOCED) == 0)
1644 1.37.2.2 skrll panic("%s: CCB %ld not ALLOCED\n",
1645 1.37.2.2 skrll sc->sc_dv.dv_xname, (long)(ccb - sc->sc_ccbs));
1646 1.37.2.2 skrll #endif
1647 1.1 ad ccb->ccb_flags |= TWE_CCB_ACTIVE;
1648 1.1 ad pa = sc->sc_cmds_paddr +
1649 1.1 ad ccb->ccb_cmdid * sizeof(struct twe_cmd);
1650 1.22 ad twe_outl(sc, TWE_REG_CMD_QUEUE, (u_int32_t)pa);
1651 1.1 ad rv = 0;
1652 1.1 ad } else
1653 1.1 ad rv = EBUSY;
1654 1.1 ad
1655 1.1 ad return (rv);
1656 1.33 christos }
1657 1.33 christos
1658 1.33 christos
1659 1.33 christos /*
1660 1.33 christos * Accept an open operation on the control device.
1661 1.33 christos */
1662 1.33 christos int
1663 1.37.2.4 skrll tweopen(dev_t dev, int flag, int mode, struct lwp *l)
1664 1.33 christos {
1665 1.33 christos struct twe_softc *twe;
1666 1.33 christos
1667 1.33 christos if ((twe = device_lookup(&twe_cd, minor(dev))) == NULL)
1668 1.33 christos return (ENXIO);
1669 1.33 christos if ((twe->sc_flags & TWEF_OPEN) != 0)
1670 1.33 christos return (EBUSY);
1671 1.33 christos
1672 1.33 christos twe->sc_flags |= TWEF_OPEN;
1673 1.33 christos return (0);
1674 1.33 christos }
1675 1.33 christos
1676 1.33 christos /*
1677 1.33 christos * Accept the last close on the control device.
1678 1.33 christos */
1679 1.33 christos int
1680 1.37.2.4 skrll tweclose(dev_t dev, int flag, int mode, struct lwp *l)
1681 1.33 christos {
1682 1.33 christos struct twe_softc *twe;
1683 1.33 christos
1684 1.33 christos twe = device_lookup(&twe_cd, minor(dev));
1685 1.33 christos twe->sc_flags &= ~TWEF_OPEN;
1686 1.33 christos return (0);
1687 1.33 christos }
1688 1.33 christos
1689 1.37.2.2 skrll void
1690 1.37.2.2 skrll twe_ccb_wait_handler(struct twe_ccb *ccb, int error)
1691 1.37.2.2 skrll {
1692 1.37.2.2 skrll
1693 1.37.2.2 skrll /* Just wake up the sleeper. */
1694 1.37.2.2 skrll wakeup(ccb);
1695 1.37.2.2 skrll }
1696 1.37.2.2 skrll
1697 1.33 christos /*
1698 1.33 christos * Handle control operations.
1699 1.33 christos */
1700 1.33 christos int
1701 1.37.2.4 skrll tweioctl(dev_t dev, u_long cmd, caddr_t data, int flag, struct lwp *l)
1702 1.33 christos {
1703 1.33 christos struct twe_softc *twe;
1704 1.33 christos struct twe_ccb *ccb;
1705 1.33 christos struct twe_param *param;
1706 1.33 christos struct twe_usercommand *tu;
1707 1.33 christos struct twe_paramcommand *tp;
1708 1.37.2.2 skrll struct twe_drivecommand *td;
1709 1.33 christos void *pdata = NULL;
1710 1.37.2.2 skrll int s, error = 0;
1711 1.33 christos u_int8_t cmdid;
1712 1.33 christos
1713 1.33 christos if (securelevel >= 2)
1714 1.33 christos return (EPERM);
1715 1.33 christos
1716 1.33 christos twe = device_lookup(&twe_cd, minor(dev));
1717 1.33 christos tu = (struct twe_usercommand *)data;
1718 1.33 christos tp = (struct twe_paramcommand *)data;
1719 1.37.2.2 skrll td = (struct twe_drivecommand *)data;
1720 1.33 christos
1721 1.37.2.2 skrll /* This is intended to be compatible with the FreeBSD interface. */
1722 1.33 christos switch (cmd) {
1723 1.33 christos case TWEIO_COMMAND:
1724 1.37.2.2 skrll /* XXX mutex */
1725 1.33 christos if (tu->tu_size > 0) {
1726 1.37.2.2 skrll /*
1727 1.37.2.2 skrll * XXX Handle > TWE_SECTOR_SIZE? Let's see if
1728 1.37.2.2 skrll * it's really necessary, first.
1729 1.37.2.2 skrll */
1730 1.37.2.2 skrll if (tu->tu_size > TWE_SECTOR_SIZE) {
1731 1.37.2.2 skrll #ifdef TWE_DEBUG
1732 1.37.2.2 skrll printf("%s: TWEIO_COMMAND: tu_size = %d\n",
1733 1.37.2.2 skrll twe->sc_dv.dv_xname, tu->tu_size);
1734 1.37.2.2 skrll #endif
1735 1.33 christos return EINVAL;
1736 1.37.2.2 skrll }
1737 1.37.2.2 skrll pdata = malloc(TWE_SECTOR_SIZE, M_DEVBUF, M_WAITOK);
1738 1.33 christos error = copyin(tu->tu_data, pdata, tu->tu_size);
1739 1.33 christos if (error != 0)
1740 1.33 christos goto done;
1741 1.37.2.2 skrll ccb = twe_ccb_alloc_wait(twe,
1742 1.33 christos TWE_CCB_DATA_IN | TWE_CCB_DATA_OUT);
1743 1.37.2.2 skrll KASSERT(ccb != NULL);
1744 1.37.2.2 skrll ccb->ccb_data = pdata;
1745 1.37.2.2 skrll ccb->ccb_datasize = TWE_SECTOR_SIZE;
1746 1.33 christos } else {
1747 1.37.2.2 skrll ccb = twe_ccb_alloc_wait(twe, 0);
1748 1.37.2.2 skrll KASSERT(ccb != NULL);
1749 1.33 christos }
1750 1.37.2.2 skrll
1751 1.37.2.2 skrll ccb->ccb_tx.tx_handler = twe_ccb_wait_handler;
1752 1.37.2.2 skrll ccb->ccb_tx.tx_context = NULL;
1753 1.37.2.2 skrll ccb->ccb_tx.tx_dv = &twe->sc_dv;
1754 1.37.2.2 skrll
1755 1.33 christos cmdid = ccb->ccb_cmdid;
1756 1.33 christos memcpy(ccb->ccb_cmd, &tu->tu_cmd, sizeof(struct twe_cmd));
1757 1.37.2.2 skrll ccb->ccb_cmd->tc_cmdid = cmdid;
1758 1.37.2.2 skrll
1759 1.33 christos /* Map the transfer. */
1760 1.33 christos if ((error = twe_ccb_map(twe, ccb)) != 0) {
1761 1.33 christos twe_ccb_free(twe, ccb);
1762 1.33 christos goto done;
1763 1.33 christos }
1764 1.33 christos
1765 1.37.2.2 skrll /* Submit the command and wait up to 1 minute. */
1766 1.37.2.2 skrll error = 0;
1767 1.37.2.2 skrll twe_ccb_enqueue(twe, ccb);
1768 1.37.2.2 skrll s = splbio();
1769 1.37.2.2 skrll while ((ccb->ccb_flags & TWE_CCB_COMPLETE) == 0)
1770 1.37.2.2 skrll if ((error = tsleep(ccb, PRIBIO, "tweioctl",
1771 1.37.2.2 skrll 60 * hz)) != 0)
1772 1.37.2.2 skrll break;
1773 1.37.2.2 skrll splx(s);
1774 1.37.2.2 skrll
1775 1.37.2.2 skrll /* Copy the command back to the ioctl argument. */
1776 1.37.2.2 skrll memcpy(&tu->tu_cmd, ccb->ccb_cmd, sizeof(struct twe_cmd));
1777 1.37.2.2 skrll #ifdef TWE_DEBUG
1778 1.37.2.2 skrll printf("%s: TWEIO_COMMAND: tc_opcode = 0x%02x, "
1779 1.37.2.2 skrll "tc_status = 0x%02x\n", twe->sc_dv.dv_xname,
1780 1.37.2.2 skrll tu->tu_cmd.tc_opcode, tu->tu_cmd.tc_status);
1781 1.37.2.2 skrll #endif
1782 1.37.2.2 skrll
1783 1.33 christos s = splbio();
1784 1.33 christos twe_ccb_free(twe, ccb);
1785 1.33 christos splx(s);
1786 1.33 christos
1787 1.33 christos if (tu->tu_size > 0)
1788 1.33 christos error = copyout(pdata, tu->tu_data, tu->tu_size);
1789 1.33 christos goto done;
1790 1.33 christos
1791 1.33 christos case TWEIO_STATS:
1792 1.33 christos return (ENOENT);
1793 1.33 christos
1794 1.33 christos case TWEIO_AEN_POLL:
1795 1.37.2.2 skrll s = splbio();
1796 1.37.2.2 skrll *(u_int *)data = twe_aen_dequeue(twe);
1797 1.37.2.2 skrll splx(s);
1798 1.33 christos return (0);
1799 1.33 christos
1800 1.33 christos case TWEIO_AEN_WAIT:
1801 1.33 christos s = splbio();
1802 1.37.2.2 skrll while ((*(u_int *)data =
1803 1.37.2.2 skrll twe_aen_dequeue(twe)) == TWE_AEN_QUEUE_EMPTY) {
1804 1.37.2.2 skrll twe->sc_flags |= TWEF_AENQ_WAIT;
1805 1.37.2.2 skrll error = tsleep(&twe->sc_aen_queue, PRIBIO | PCATCH,
1806 1.37.2.2 skrll "tweaen", 0);
1807 1.37.2.2 skrll if (error == EINTR) {
1808 1.37.2.2 skrll splx(s);
1809 1.37.2.2 skrll return (error);
1810 1.37.2.2 skrll }
1811 1.33 christos }
1812 1.33 christos splx(s);
1813 1.33 christos return (0);
1814 1.33 christos
1815 1.33 christos case TWEIO_GET_PARAM:
1816 1.33 christos error = twe_param_get(twe, tp->tp_table_id, tp->tp_param_id,
1817 1.37.2.2 skrll tp->tp_size, 0, ¶m);
1818 1.33 christos if (error != 0)
1819 1.33 christos return (error);
1820 1.33 christos if (param->tp_param_size > tp->tp_size) {
1821 1.33 christos error = EFAULT;
1822 1.33 christos goto done;
1823 1.33 christos }
1824 1.33 christos error = copyout(param->tp_data, tp->tp_data,
1825 1.33 christos param->tp_param_size);
1826 1.33 christos goto done;
1827 1.33 christos
1828 1.33 christos case TWEIO_SET_PARAM:
1829 1.33 christos pdata = malloc(tp->tp_size, M_DEVBUF, M_WAITOK);
1830 1.33 christos if ((error = copyin(tp->tp_data, pdata, tp->tp_size)) != 0)
1831 1.33 christos goto done;
1832 1.33 christos error = twe_param_set(twe, tp->tp_table_id, tp->tp_param_id,
1833 1.33 christos tp->tp_size, pdata);
1834 1.33 christos goto done;
1835 1.33 christos
1836 1.33 christos case TWEIO_RESET:
1837 1.37.2.2 skrll s = splbio();
1838 1.33 christos twe_reset(twe);
1839 1.37.2.2 skrll splx(s);
1840 1.33 christos return (0);
1841 1.33 christos
1842 1.37.2.2 skrll case TWEIO_ADD_UNIT:
1843 1.37.2.2 skrll /* XXX mutex */
1844 1.37.2.2 skrll return (twe_add_unit(twe, td->td_unit));
1845 1.37.2.2 skrll
1846 1.37.2.2 skrll case TWEIO_DEL_UNIT:
1847 1.37.2.2 skrll /* XXX mutex */
1848 1.37.2.2 skrll return (twe_del_unit(twe, td->td_unit));
1849 1.37.2.2 skrll
1850 1.33 christos default:
1851 1.33 christos return EINVAL;
1852 1.33 christos }
1853 1.33 christos done:
1854 1.33 christos if (pdata)
1855 1.33 christos free(pdata, M_DEVBUF);
1856 1.33 christos return error;
1857 1.1 ad }
1858 1.37.2.2 skrll
1859 1.37.2.2 skrll /*
1860 1.37.2.2 skrll * Print some information about the controller
1861 1.37.2.2 skrll */
1862 1.37.2.2 skrll static void
1863 1.37.2.2 skrll twe_describe_controller(struct twe_softc *sc)
1864 1.37.2.2 skrll {
1865 1.37.2.2 skrll struct twe_param *p[6];
1866 1.37.2.2 skrll int i, rv = 0;
1867 1.37.2.2 skrll uint32_t dsize;
1868 1.37.2.2 skrll uint8_t ports;
1869 1.37.2.2 skrll
1870 1.37.2.2 skrll /* get the port count */
1871 1.37.2.2 skrll rv |= twe_param_get_1(sc, TWE_PARAM_CONTROLLER,
1872 1.37.2.2 skrll TWE_PARAM_CONTROLLER_PortCount, &ports);
1873 1.37.2.2 skrll
1874 1.37.2.2 skrll /* get version strings */
1875 1.37.2.2 skrll rv |= twe_param_get(sc, TWE_PARAM_VERSION, TWE_PARAM_VERSION_Mon,
1876 1.37.2.2 skrll 16, NULL, &p[0]);
1877 1.37.2.2 skrll rv |= twe_param_get(sc, TWE_PARAM_VERSION, TWE_PARAM_VERSION_FW,
1878 1.37.2.2 skrll 16, NULL, &p[1]);
1879 1.37.2.2 skrll rv |= twe_param_get(sc, TWE_PARAM_VERSION, TWE_PARAM_VERSION_BIOS,
1880 1.37.2.2 skrll 16, NULL, &p[2]);
1881 1.37.2.2 skrll rv |= twe_param_get(sc, TWE_PARAM_VERSION, TWE_PARAM_VERSION_PCB,
1882 1.37.2.2 skrll 8, NULL, &p[3]);
1883 1.37.2.2 skrll rv |= twe_param_get(sc, TWE_PARAM_VERSION, TWE_PARAM_VERSION_ATA,
1884 1.37.2.2 skrll 8, NULL, &p[4]);
1885 1.37.2.2 skrll rv |= twe_param_get(sc, TWE_PARAM_VERSION, TWE_PARAM_VERSION_PCI,
1886 1.37.2.2 skrll 8, NULL, &p[5]);
1887 1.37.2.2 skrll
1888 1.37.2.2 skrll if (rv) {
1889 1.37.2.2 skrll /* some error occurred */
1890 1.37.2.2 skrll aprint_error("%s: failed to fetch version information\n",
1891 1.37.2.2 skrll sc->sc_dv.dv_xname);
1892 1.37.2.2 skrll return;
1893 1.37.2.2 skrll }
1894 1.37.2.2 skrll
1895 1.37.2.2 skrll aprint_normal("%s: %d ports, Firmware %.16s, BIOS %.16s\n",
1896 1.37.2.2 skrll sc->sc_dv.dv_xname, ports,
1897 1.37.2.2 skrll p[1]->tp_data, p[2]->tp_data);
1898 1.37.2.2 skrll
1899 1.37.2.2 skrll aprint_verbose("%s: Monitor %.16s, PCB %.8s, Achip %.8s, Pchip %.8s\n",
1900 1.37.2.2 skrll sc->sc_dv.dv_xname,
1901 1.37.2.2 skrll p[0]->tp_data, p[3]->tp_data,
1902 1.37.2.2 skrll p[4]->tp_data, p[5]->tp_data);
1903 1.37.2.2 skrll
1904 1.37.2.2 skrll free(p[0], M_DEVBUF);
1905 1.37.2.2 skrll free(p[1], M_DEVBUF);
1906 1.37.2.2 skrll free(p[2], M_DEVBUF);
1907 1.37.2.2 skrll free(p[3], M_DEVBUF);
1908 1.37.2.2 skrll free(p[4], M_DEVBUF);
1909 1.37.2.2 skrll free(p[5], M_DEVBUF);
1910 1.37.2.2 skrll
1911 1.37.2.2 skrll rv = twe_param_get(sc, TWE_PARAM_DRIVESUMMARY,
1912 1.37.2.2 skrll TWE_PARAM_DRIVESUMMARY_Status, 16, NULL, &p[0]);
1913 1.37.2.2 skrll if (rv) {
1914 1.37.2.2 skrll aprint_error("%s: failed to get drive status summary\n",
1915 1.37.2.2 skrll sc->sc_dv.dv_xname);
1916 1.37.2.2 skrll return;
1917 1.37.2.2 skrll }
1918 1.37.2.2 skrll for (i = 0; i < ports; i++) {
1919 1.37.2.2 skrll if (p[0]->tp_data[i] != TWE_PARAM_DRIVESTATUS_Present)
1920 1.37.2.2 skrll continue;
1921 1.37.2.2 skrll rv = twe_param_get_4(sc, TWE_PARAM_DRIVEINFO + i,
1922 1.37.2.2 skrll TWE_PARAM_DRIVEINFO_Size, &dsize);
1923 1.37.2.2 skrll if (rv) {
1924 1.37.2.2 skrll aprint_error(
1925 1.37.2.2 skrll "%s: unable to get drive size for port %d\n",
1926 1.37.2.2 skrll sc->sc_dv.dv_xname, i);
1927 1.37.2.2 skrll continue;
1928 1.37.2.2 skrll }
1929 1.37.2.2 skrll rv = twe_param_get(sc, TWE_PARAM_DRIVEINFO + i,
1930 1.37.2.2 skrll TWE_PARAM_DRIVEINFO_Model, 40, NULL, &p[1]);
1931 1.37.2.2 skrll if (rv) {
1932 1.37.2.2 skrll aprint_error(
1933 1.37.2.2 skrll "%s: unable to get drive model for port %d\n",
1934 1.37.2.2 skrll sc->sc_dv.dv_xname, i);
1935 1.37.2.2 skrll continue;
1936 1.37.2.2 skrll }
1937 1.37.2.2 skrll aprint_verbose("%s: port %d: %.40s %d MB\n", sc->sc_dv.dv_xname,
1938 1.37.2.2 skrll i, p[1]->tp_data, dsize / 2048);
1939 1.37.2.2 skrll free(p[1], M_DEVBUF);
1940 1.37.2.2 skrll }
1941 1.37.2.2 skrll free(p[0], M_DEVBUF);
1942 1.37.2.2 skrll }
1943