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