twa.c revision 1.19 1 /* $NetBSD: twa.c,v 1.19 2008/04/10 19:13:38 cegger Exp $ */
2 /* $wasabi: twa.c,v 1.27 2006/07/28 18:17:21 wrstuden Exp $ */
3
4 /*-
5 * Copyright (c) 2004 The NetBSD Foundation, Inc.
6 * All rights reserved.
7 *
8 * This code is derived from software contributed to The NetBSD Foundation
9 * by Jordan Rhody of Wasabi Systems, Inc.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. All advertising materials mentioning features or use of this software
20 * must display the following acknowledgement:
21 * This product includes software developed by the NetBSD
22 * Foundation, Inc. and its contributors.
23 * 4. Neither the name of The NetBSD Foundation nor the names of its
24 * contributors may be used to endorse or promote products derived
25 * from this software without specific prior written permission.
26 *
27 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
28 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
29 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
30 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
31 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
32 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
33 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
34 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
35 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
36 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
37 * POSSIBILITY OF SUCH DAMAGE.
38 */
39
40 /*-
41 * Copyright (c) 2003-04 3ware, Inc.
42 * Copyright (c) 2000 Michael Smith
43 * Copyright (c) 2000 BSDi
44 * All rights reserved.
45 *
46 * Redistribution and use in source and binary forms, with or without
47 * modification, are permitted provided that the following conditions
48 * are met:
49 * 1. Redistributions of source code must retain the above copyright
50 * notice, this list of conditions and the following disclaimer.
51 * 2. Redistributions in binary form must reproduce the above copyright
52 * notice, this list of conditions and the following disclaimer in the
53 * documentation and/or other materials provided with the distribution.
54 *
55 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
56 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
57 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
58 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
59 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
60 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
61 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
62 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
63 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
64 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
65 * SUCH DAMAGE.
66 *
67 * $FreeBSD: src/sys/dev/twa/twa.c,v 1.2 2004/04/02 15:09:57 des Exp $
68 */
69
70 /*
71 * 3ware driver for 9000 series storage controllers.
72 *
73 * Author: Vinod Kashyap
74 */
75
76 #include <sys/cdefs.h>
77 __KERNEL_RCSID(0, "$NetBSD: twa.c,v 1.19 2008/04/10 19:13:38 cegger Exp $");
78
79 #include <sys/param.h>
80 #include <sys/systm.h>
81 #include <sys/kernel.h>
82 #include <sys/device.h>
83 #include <sys/queue.h>
84 #include <sys/proc.h>
85 #include <sys/bswap.h>
86 #include <sys/buf.h>
87 #include <sys/bufq.h>
88 #include <sys/endian.h>
89 #include <sys/malloc.h>
90 #include <sys/conf.h>
91 #include <sys/disk.h>
92 #include <sys/sysctl.h>
93 #include <sys/syslog.h>
94 #if 1
95 #include <sys/ktrace.h>
96 #endif
97
98 #include <uvm/uvm_extern.h>
99
100 #include <sys/bus.h>
101
102 #include <dev/pci/pcireg.h>
103 #include <dev/pci/pcivar.h>
104 #include <dev/pci/pcidevs.h>
105 #include <dev/pci/twareg.h>
106 #include <dev/pci/twavar.h>
107 #include <dev/pci/twaio.h>
108
109 #include <dev/scsipi/scsipi_all.h>
110 #include <dev/scsipi/scsipi_disk.h>
111 #include <dev/scsipi/scsipiconf.h>
112 #include <dev/scsipi/scsi_spc.h>
113
114 #include <dev/ldvar.h>
115
116 #include "locators.h"
117
118 #define PCI_CBIO 0x10
119
120 static int twa_fetch_aen(struct twa_softc *);
121 static void twa_aen_callback(struct twa_request *);
122 static int twa_find_aen(struct twa_softc *sc, uint16_t);
123 static uint16_t twa_enqueue_aen(struct twa_softc *sc,
124 struct twa_command_header *);
125
126 static void twa_attach(struct device *, struct device *, void *);
127 static void twa_shutdown(void *);
128 static int twa_init_connection(struct twa_softc *, uint16_t, uint32_t,
129 uint16_t, uint16_t, uint16_t, uint16_t, uint16_t *,
130 uint16_t *, uint16_t *, uint16_t *, uint32_t *);
131 static int twa_intr(void *);
132 static int twa_match(struct device *, struct cfdata *, void *);
133 static int twa_reset(struct twa_softc *);
134
135 static int twa_print(void *, const char *);
136 static int twa_soft_reset(struct twa_softc *);
137
138 static int twa_check_ctlr_state(struct twa_softc *, uint32_t);
139 static int twa_get_param(struct twa_softc *, int, int, size_t,
140 void (* callback)(struct twa_request *),
141 struct twa_param_9k **);
142 static int twa_set_param(struct twa_softc *, int, int, int, void *,
143 void (* callback)(struct twa_request *));
144 static void twa_describe_controller(struct twa_softc *);
145 static int twa_wait_status(struct twa_softc *, uint32_t, uint32_t);
146 static int twa_done(struct twa_softc *);
147 #if 0
148 static int twa_flash_firmware(struct twa_softc *sc);
149 static int twa_hard_reset(struct twa_softc *sc);
150 #endif
151
152 extern struct cfdriver twa_cd;
153 extern uint32_t twa_fw_img_size;
154 extern uint8_t twa_fw_img[];
155
156 CFATTACH_DECL(twa, sizeof(struct twa_softc),
157 twa_match, twa_attach, NULL, NULL);
158
159 /* FreeBSD driver revision for sysctl expected by the 3ware cli */
160 const char twaver[] = "1.50.01.002";
161
162 /* AEN messages. */
163 static const struct twa_message twa_aen_table[] = {
164 {0x0000, "AEN queue empty"},
165 {0x0001, "Controller reset occurred"},
166 {0x0002, "Degraded unit detected"},
167 {0x0003, "Controller error occured"},
168 {0x0004, "Background rebuild failed"},
169 {0x0005, "Background rebuild done"},
170 {0x0006, "Incomplete unit detected"},
171 {0x0007, "Background initialize done"},
172 {0x0008, "Unclean shutdown detected"},
173 {0x0009, "Drive timeout detected"},
174 {0x000A, "Drive error detected"},
175 {0x000B, "Rebuild started"},
176 {0x000C, "Background initialize started"},
177 {0x000D, "Entire logical unit was deleted"},
178 {0x000E, "Background initialize failed"},
179 {0x000F, "SMART attribute exceeded threshold"},
180 {0x0010, "Power supply reported AC under range"},
181 {0x0011, "Power supply reported DC out of range"},
182 {0x0012, "Power supply reported a malfunction"},
183 {0x0013, "Power supply predicted malfunction"},
184 {0x0014, "Battery charge is below threshold"},
185 {0x0015, "Fan speed is below threshold"},
186 {0x0016, "Temperature sensor is above threshold"},
187 {0x0017, "Power supply was removed"},
188 {0x0018, "Power supply was inserted"},
189 {0x0019, "Drive was removed from a bay"},
190 {0x001A, "Drive was inserted into a bay"},
191 {0x001B, "Drive bay cover door was opened"},
192 {0x001C, "Drive bay cover door was closed"},
193 {0x001D, "Product case was opened"},
194 {0x0020, "Prepare for shutdown (power-off)"},
195 {0x0021, "Downgrade UDMA mode to lower speed"},
196 {0x0022, "Upgrade UDMA mode to higher speed"},
197 {0x0023, "Sector repair completed"},
198 {0x0024, "Sbuf memory test failed"},
199 {0x0025, "Error flushing cached write data to disk"},
200 {0x0026, "Drive reported data ECC error"},
201 {0x0027, "DCB has checksum error"},
202 {0x0028, "DCB version is unsupported"},
203 {0x0029, "Background verify started"},
204 {0x002A, "Background verify failed"},
205 {0x002B, "Background verify done"},
206 {0x002C, "Bad sector overwritten during rebuild"},
207 {0x002E, "Replace failed because replacement drive too small"},
208 {0x002F, "Verify failed because array was never initialized"},
209 {0x0030, "Unsupported ATA drive"},
210 {0x0031, "Synchronize host/controller time"},
211 {0x0032, "Spare capacity is inadequate for some units"},
212 {0x0033, "Background migration started"},
213 {0x0034, "Background migration failed"},
214 {0x0035, "Background migration done"},
215 {0x0036, "Verify detected and fixed data/parity mismatch"},
216 {0x0037, "SO-DIMM incompatible"},
217 {0x0038, "SO-DIMM not detected"},
218 {0x0039, "Corrected Sbuf ECC error"},
219 {0x003A, "Drive power on reset detected"},
220 {0x003B, "Background rebuild paused"},
221 {0x003C, "Background initialize paused"},
222 {0x003D, "Background verify paused"},
223 {0x003E, "Background migration paused"},
224 {0x003F, "Corrupt flash file system detected"},
225 {0x0040, "Flash file system repaired"},
226 {0x0041, "Unit number assignments were lost"},
227 {0x0042, "Error during read of primary DCB"},
228 {0x0043, "Latent error found in backup DCB"},
229 {0x0044, "Battery voltage is normal"},
230 {0x0045, "Battery voltage is low"},
231 {0x0046, "Battery voltage is high"},
232 {0x0047, "Battery voltage is too low"},
233 {0x0048, "Battery voltage is too high"},
234 {0x0049, "Battery temperature is normal"},
235 {0x004A, "Battery temperature is low"},
236 {0x004B, "Battery temperature is high"},
237 {0x004C, "Battery temperature is too low"},
238 {0x004D, "Battery temperature is too high"},
239 {0x004E, "Battery capacity test started"},
240 {0x004F, "Cache synchronization skipped"},
241 {0x0050, "Battery capacity test completed"},
242 {0x0051, "Battery health check started"},
243 {0x0052, "Battery health check completed"},
244 {0x0053, "Need to do a capacity test"},
245 {0x0054, "Charge termination voltage is at high level"},
246 {0x0055, "Battery charging started"},
247 {0x0056, "Battery charging completed"},
248 {0x0057, "Battery charging fault"},
249 {0x0058, "Battery capacity is below warning level"},
250 {0x0059, "Battery capacity is below error level"},
251 {0x005A, "Battery is present"},
252 {0x005B, "Battery is not present"},
253 {0x005C, "Battery is weak"},
254 {0x005D, "Battery health check failed"},
255 {0x005E, "Cache synchronized after power fail"},
256 {0x005F, "Cache synchronization failed; some data lost"},
257 {0x0060, "Bad cache meta data checksum"},
258 {0x0061, "Bad cache meta data signature"},
259 {0x0062, "Cache meta data restore failed"},
260 {0x0063, "BBU not found after power fail"},
261 {0x00FC, "Recovered/finished array membership update"},
262 {0x00FD, "Handler lockup"},
263 {0x00FE, "Retrying PCI transfer"},
264 {0x00FF, "AEN queue is full"},
265 {0xFFFFFFFF, (char *)NULL}
266 };
267
268 /* AEN severity table. */
269 static const char *twa_aen_severity_table[] = {
270 "None",
271 "ERROR",
272 "WARNING",
273 "INFO",
274 "DEBUG",
275 (char *)NULL
276 };
277
278 /* Error messages. */
279 static const struct twa_message twa_error_table[] = {
280 {0x0100, "SGL entry contains zero data"},
281 {0x0101, "Invalid command opcode"},
282 {0x0102, "SGL entry has unaligned address"},
283 {0x0103, "SGL size does not match command"},
284 {0x0104, "SGL entry has illegal length"},
285 {0x0105, "Command packet is not aligned"},
286 {0x0106, "Invalid request ID"},
287 {0x0107, "Duplicate request ID"},
288 {0x0108, "ID not locked"},
289 {0x0109, "LBA out of range"},
290 {0x010A, "Logical unit not supported"},
291 {0x010B, "Parameter table does not exist"},
292 {0x010C, "Parameter index does not exist"},
293 {0x010D, "Invalid field in CDB"},
294 {0x010E, "Specified port has invalid drive"},
295 {0x010F, "Parameter item size mismatch"},
296 {0x0110, "Failed memory allocation"},
297 {0x0111, "Memory request too large"},
298 {0x0112, "Out of memory segments"},
299 {0x0113, "Invalid address to deallocate"},
300 {0x0114, "Out of memory"},
301 {0x0115, "Out of heap"},
302 {0x0120, "Double degrade"},
303 {0x0121, "Drive not degraded"},
304 {0x0122, "Reconstruct error"},
305 {0x0123, "Replace not accepted"},
306 {0x0124, "Replace drive capacity too small"},
307 {0x0125, "Sector count not allowed"},
308 {0x0126, "No spares left"},
309 {0x0127, "Reconstruct error"},
310 {0x0128, "Unit is offline"},
311 {0x0129, "Cannot update status to DCB"},
312 {0x0130, "Invalid stripe handle"},
313 {0x0131, "Handle that was not locked"},
314 {0x0132, "Handle that was not empy"},
315 {0x0133, "Handle has different owner"},
316 {0x0140, "IPR has parent"},
317 {0x0150, "Illegal Pbuf address alignment"},
318 {0x0151, "Illegal Pbuf transfer length"},
319 {0x0152, "Illegal Sbuf address alignment"},
320 {0x0153, "Illegal Sbuf transfer length"},
321 {0x0160, "Command packet too large"},
322 {0x0161, "SGL exceeds maximum length"},
323 {0x0162, "SGL has too many entries"},
324 {0x0170, "Insufficient resources for rebuilder"},
325 {0x0171, "Verify error (data != parity)"},
326 {0x0180, "Requested segment not in directory of this DCB"},
327 {0x0181, "DCB segment has unsupported version"},
328 {0x0182, "DCB segment has checksum error"},
329 {0x0183, "DCB support (settings) segment invalid"},
330 {0x0184, "DCB UDB (unit descriptor block) segment invalid"},
331 {0x0185, "DCB GUID (globally unique identifier) segment invalid"},
332 {0x01A0, "Could not clear Sbuf"},
333 {0x01C0, "Flash identify failed"},
334 {0x01C1, "Flash out of bounds"},
335 {0x01C2, "Flash verify error"},
336 {0x01C3, "Flash file object not found"},
337 {0x01C4, "Flash file already present"},
338 {0x01C5, "Flash file system full"},
339 {0x01C6, "Flash file not present"},
340 {0x01C7, "Flash file size error"},
341 {0x01C8, "Bad flash file checksum"},
342 {0x01CA, "Corrupt flash file system detected"},
343 {0x01D0, "Invalid field in parameter list"},
344 {0x01D1, "Parameter list length error"},
345 {0x01D2, "Parameter item is not changeable"},
346 {0x01D3, "Parameter item is not saveable"},
347 {0x0200, "UDMA CRC error"},
348 {0x0201, "Internal CRC error"},
349 {0x0202, "Data ECC error"},
350 {0x0203, "ADP level 1 error"},
351 {0x0204, "Port timeout"},
352 {0x0205, "Drive power on reset"},
353 {0x0206, "ADP level 2 error"},
354 {0x0207, "Soft reset failed"},
355 {0x0208, "Drive not ready"},
356 {0x0209, "Unclassified port error"},
357 {0x020A, "Drive aborted command"},
358 {0x0210, "Internal CRC error"},
359 {0x0211, "Host PCI bus abort"},
360 {0x0212, "Host PCI parity error"},
361 {0x0213, "Port handler error"},
362 {0x0214, "Token interrupt count error"},
363 {0x0215, "Timeout waiting for PCI transfer"},
364 {0x0216, "Corrected buffer ECC"},
365 {0x0217, "Uncorrected buffer ECC"},
366 {0x0230, "Unsupported command during flash recovery"},
367 {0x0231, "Next image buffer expected"},
368 {0x0232, "Binary image architecture incompatible"},
369 {0x0233, "Binary image has no signature"},
370 {0x0234, "Binary image has bad checksum"},
371 {0x0235, "Image downloaded overflowed buffer"},
372 {0x0240, "I2C device not found"},
373 {0x0241, "I2C transaction aborted"},
374 {0x0242, "SO-DIMM parameter(s) incompatible using defaults"},
375 {0x0243, "SO-DIMM unsupported"},
376 {0x0248, "SPI transfer status error"},
377 {0x0249, "SPI transfer timeout error"},
378 {0x0250, "Invalid unit descriptor size in CreateUnit"},
379 {0x0251, "Unit descriptor size exceeds data buffer in CreateUnit"},
380 {0x0252, "Invalid value in CreateUnit descriptor"},
381 {0x0253, "Inadequate disk space to support descriptor in CreateUnit"},
382 {0x0254, "Unable to create data channel for this unit descriptor"},
383 {0x0255, "CreateUnit descriptor specifies a drive already in use"},
384 {0x0256, "Unable to write configuration to all disks during CreateUnit"},
385 {0x0257, "CreateUnit does not support this descriptor version"},
386 {0x0258, "Invalid subunit for RAID 0 or 5 in CreateUnit"},
387 {0x0259, "Too many descriptors in CreateUnit"},
388 {0x025A, "Invalid configuration specified in CreateUnit descriptor"},
389 {0x025B, "Invalid LBA offset specified in CreateUnit descriptor"},
390 {0x025C, "Invalid stripelet size specified in CreateUnit descriptor"},
391 {0x0260, "SMART attribute exceeded threshold"},
392 {0xFFFFFFFF, (char *)NULL}
393 };
394
395 struct twa_pci_identity {
396 uint32_t vendor_id;
397 uint32_t product_id;
398 const char *name;
399 };
400
401 static const struct twa_pci_identity pci_twa_products[] = {
402 { PCI_VENDOR_3WARE,
403 PCI_PRODUCT_3WARE_9000,
404 "3ware 9000 series",
405 },
406 { PCI_VENDOR_3WARE,
407 PCI_PRODUCT_3WARE_9550,
408 "3ware 9550SX series",
409 },
410 { 0,
411 0,
412 NULL,
413 },
414 };
415
416
417 static inline void
418 twa_outl(struct twa_softc *sc, int off, uint32_t val)
419 {
420
421 bus_space_write_4(sc->twa_bus_iot, sc->twa_bus_ioh, off, val);
422 bus_space_barrier(sc->twa_bus_iot, sc->twa_bus_ioh, off, 4,
423 BUS_SPACE_BARRIER_WRITE);
424 }
425
426 static inline uint32_t twa_inl(struct twa_softc *sc, int off)
427 {
428
429 bus_space_barrier(sc->twa_bus_iot, sc->twa_bus_ioh, off, 4,
430 BUS_SPACE_BARRIER_WRITE | BUS_SPACE_BARRIER_READ);
431 return (bus_space_read_4(sc->twa_bus_iot, sc->twa_bus_ioh, off));
432 }
433
434 void
435 twa_request_wait_handler(struct twa_request *tr)
436 {
437
438 wakeup(tr);
439 }
440
441 static int
442 twa_match(struct device *parent, struct cfdata *cfdata,
443 void *aux)
444 {
445 int i;
446 struct pci_attach_args *pa = aux;
447 const struct twa_pci_identity *entry = 0;
448
449 if (PCI_VENDOR(pa->pa_id) == PCI_VENDOR_3WARE) {
450 for (i = 0; (pci_twa_products[i].product_id); i++) {
451 entry = &pci_twa_products[i];
452 if (entry->product_id == PCI_PRODUCT(pa->pa_id)) {
453 aprint_normal("%s: (rev. 0x%02x)\n",
454 entry->name, PCI_REVISION(pa->pa_class));
455 return (1);
456 }
457 }
458 }
459 return (0);
460 }
461
462 static const char *
463 twa_find_msg_string(const struct twa_message *table, uint16_t code)
464 {
465 int i;
466
467 for (i = 0; table[i].message != NULL; i++)
468 if (table[i].code == code)
469 return(table[i].message);
470
471 return(table[i].message);
472 }
473
474 void
475 twa_release_request(struct twa_request *tr)
476 {
477 int s;
478 struct twa_softc *sc;
479
480 sc = tr->tr_sc;
481
482 if ((tr->tr_flags & TWA_CMD_AEN) == 0) {
483 s = splbio();
484 TAILQ_INSERT_TAIL(&tr->tr_sc->twa_free, tr, tr_link);
485 splx(s);
486 if (__predict_false((tr->tr_sc->twa_sc_flags &
487 TWA_STATE_REQUEST_WAIT) != 0)) {
488 tr->tr_sc->twa_sc_flags &= ~TWA_STATE_REQUEST_WAIT;
489 wakeup(&sc->twa_free);
490 }
491 } else
492 tr->tr_flags &= ~TWA_CMD_AEN_BUSY;
493 }
494
495 static void
496 twa_unmap_request(struct twa_request *tr)
497 {
498 struct twa_softc *sc = tr->tr_sc;
499 uint8_t cmd_status;
500 int s;
501
502 /* If the command involved data, unmap that too. */
503 if (tr->tr_data != NULL) {
504 if (tr->tr_cmd_pkt_type & TWA_CMD_PKT_TYPE_9K)
505 cmd_status = tr->tr_command->command.cmd_pkt_9k.status;
506 else
507 cmd_status =
508 tr->tr_command->command.cmd_pkt_7k.generic.status;
509
510 if (tr->tr_flags & TWA_CMD_DATA_OUT) {
511 bus_dmamap_sync(tr->tr_sc->twa_dma_tag, tr->tr_dma_map,
512 0, tr->tr_length, BUS_DMASYNC_POSTREAD);
513 /*
514 * If we are using a bounce buffer, and we are reading
515 * data, copy the real data in.
516 */
517 if (tr->tr_flags & TWA_CMD_DATA_COPY_NEEDED)
518 if (cmd_status == 0)
519 memcpy(tr->tr_real_data, tr->tr_data,
520 tr->tr_real_length);
521 }
522 if (tr->tr_flags & TWA_CMD_DATA_IN)
523 bus_dmamap_sync(tr->tr_sc->twa_dma_tag, tr->tr_dma_map,
524 0, tr->tr_length, BUS_DMASYNC_POSTWRITE);
525
526 bus_dmamap_unload(sc->twa_dma_tag, tr->tr_dma_map);
527 }
528
529 /* Free alignment buffer if it was used. */
530 if (tr->tr_flags & TWA_CMD_DATA_COPY_NEEDED) {
531 s = splvm();
532 uvm_km_free(kmem_map, (vaddr_t)tr->tr_data,
533 tr->tr_length, UVM_KMF_WIRED);
534 splx(s);
535 tr->tr_data = tr->tr_real_data;
536 tr->tr_length = tr->tr_real_length;
537 }
538 }
539
540 /*
541 * Function name: twa_wait_request
542 * Description: Sends down a firmware cmd, and waits for the completion,
543 * but NOT in a tight loop.
544 *
545 * Input: tr -- ptr to request pkt
546 * timeout -- max # of seconds to wait before giving up
547 * Output: None
548 * Return value: 0 -- success
549 * non-zero-- failure
550 */
551 static int
552 twa_wait_request(struct twa_request *tr, uint32_t timeout)
553 {
554 time_t end_time;
555 struct timeval t1;
556 int s, rv;
557
558 tr->tr_flags |= TWA_CMD_SLEEP_ON_REQUEST;
559 tr->tr_callback = twa_request_wait_handler;
560 tr->tr_status = TWA_CMD_BUSY;
561
562 rv = twa_map_request(tr);
563
564 if (rv != 0)
565 return (rv);
566
567 microtime(&t1);
568 end_time = t1.tv_usec +
569 (timeout * 1000 * 100);
570
571 while (tr->tr_status != TWA_CMD_COMPLETE) {
572 rv = tr->tr_error;
573 if (rv != 0)
574 return(rv);
575 if ((rv = tsleep(tr, PRIBIO, "twawait", timeout * hz)) == 0)
576 break;
577
578 if (rv == EWOULDBLOCK) {
579 /*
580 * We will reset the controller only if the request has
581 * already been submitted, so as to not lose the
582 * request packet. If a busy request timed out, the
583 * reset will take care of freeing resources. If a
584 * pending request timed out, we will free resources
585 * for that request, right here. So, the caller is
586 * expected to NOT cleanup when ETIMEDOUT is returned.
587 */
588 if (tr->tr_status == TWA_CMD_BUSY)
589 twa_reset(tr->tr_sc);
590 else {
591 /* Request was never submitted. Clean up. */
592 s = splbio();
593 TAILQ_REMOVE(&tr->tr_sc->twa_pending, tr,
594 tr_link);
595 splx(s);
596
597 twa_unmap_request(tr);
598 if (tr->tr_data)
599 free(tr->tr_data, M_DEVBUF);
600
601 twa_release_request(tr);
602 }
603 return(ETIMEDOUT);
604 }
605 /*
606 * Either the request got completed, or we were woken up by a
607 * signal. Calculate the new timeout, in case it was the
608 * latter.
609 */
610 microtime(&t1);
611
612 timeout = (end_time - t1.tv_usec) / (1000 * 100);
613 }
614 return(rv);
615 }
616
617 /*
618 * Function name: twa_immediate_request
619 * Description: Sends down a firmware cmd, and waits for the completion
620 * in a tight loop.
621 *
622 * Input: tr -- ptr to request pkt
623 * timeout -- max # of seconds to wait before giving up
624 * Output: None
625 * Return value: 0 -- success
626 * non-zero-- failure
627 */
628 static int
629 twa_immediate_request(struct twa_request *tr, uint32_t timeout)
630 {
631 struct timeval t1;
632 int s = 0, rv = 0;
633
634 rv = twa_map_request(tr);
635
636 if (rv != 0)
637 return(rv);
638
639 timeout = (timeout * 10000 * 10);
640
641 microtime(&t1);
642
643 timeout += t1.tv_usec;
644
645 do {
646 rv = tr->tr_error;
647 if (rv != 0)
648 return(rv);
649 s = splbio();
650 twa_done(tr->tr_sc);
651 splx(s);
652 if (tr->tr_status == TWA_CMD_COMPLETE)
653 return(rv);
654 microtime(&t1);
655 } while (t1.tv_usec <= timeout);
656
657 /*
658 * We will reset the controller only if the request has
659 * already been submitted, so as to not lose the
660 * request packet. If a busy request timed out, the
661 * reset will take care of freeing resources. If a
662 * pending request timed out, we will free resources
663 * for that request, right here. So, the caller is
664 * expected to NOT cleanup when ETIMEDOUT is returned.
665 */
666 rv = ETIMEDOUT;
667
668 if (tr->tr_status == TWA_CMD_BUSY)
669 twa_reset(tr->tr_sc);
670 else {
671 /* Request was never submitted. Clean up. */
672 s = splbio();
673 TAILQ_REMOVE(&tr->tr_sc->twa_pending, tr, tr_link);
674 splx(s);
675 twa_unmap_request(tr);
676 if (tr->tr_data)
677 free(tr->tr_data, M_DEVBUF);
678
679 twa_release_request(tr);
680 }
681 return (rv);
682 }
683
684 static int
685 twa_inquiry(struct twa_request *tr, int lunid)
686 {
687 int error;
688 struct twa_command_9k *tr_9k_cmd;
689
690 if (tr->tr_data == NULL)
691 return (ENOMEM);
692
693 memset(tr->tr_data, 0, TWA_SECTOR_SIZE);
694
695 tr->tr_length = TWA_SECTOR_SIZE;
696 tr->tr_cmd_pkt_type = TWA_CMD_PKT_TYPE_9K;
697 tr->tr_flags |= TWA_CMD_DATA_IN;
698
699 tr_9k_cmd = &tr->tr_command->command.cmd_pkt_9k;
700
701 tr_9k_cmd->command.opcode = TWA_OP_EXECUTE_SCSI_COMMAND;
702 tr_9k_cmd->unit = lunid;
703 tr_9k_cmd->request_id = tr->tr_request_id;
704 tr_9k_cmd->status = 0;
705 tr_9k_cmd->sgl_offset = 16;
706 tr_9k_cmd->sgl_entries = 1;
707 /* create the CDB here */
708 tr_9k_cmd->cdb[0] = INQUIRY;
709 tr_9k_cmd->cdb[1] = ((lunid << 5) & 0x0e);
710 tr_9k_cmd->cdb[4] = 255;
711
712 /* XXXX setup page data no lun device
713 * it seems 9000 series does not indicate
714 * NOTPRESENT - need more investigation
715 */
716 ((struct scsipi_inquiry_data *)tr->tr_data)->device =
717 SID_QUAL_LU_NOTPRESENT;
718
719 error = twa_immediate_request(tr, TWA_REQUEST_TIMEOUT_PERIOD);
720
721 if (error != 0)
722 return (error);
723
724 if (((struct scsipi_inquiry_data *)tr->tr_data)->device ==
725 SID_QUAL_LU_NOTPRESENT)
726 error = 1;
727
728 return (error);
729 }
730
731 static int
732 twa_print_inquiry_data(struct twa_softc *sc, struct scsipi_inquiry_data *scsipi)
733 {
734
735 printf("%s: %s\n", device_xname(&sc->twa_dv), scsipi->vendor);
736
737 return (1);
738 }
739
740
741 static uint64_t
742 twa_read_capacity(struct twa_request *tr, int lunid)
743 {
744 int error;
745 struct twa_command_9k *tr_9k_cmd;
746 uint64_t array_size = 0LL;
747
748 if (tr->tr_data == NULL)
749 return (ENOMEM);
750
751 memset(tr->tr_data, 0, TWA_SECTOR_SIZE);
752
753 tr->tr_length = TWA_SECTOR_SIZE;
754 tr->tr_cmd_pkt_type = TWA_CMD_PKT_TYPE_9K;
755 tr->tr_flags |= TWA_CMD_DATA_OUT;
756
757 tr_9k_cmd = &tr->tr_command->command.cmd_pkt_9k;
758
759 tr_9k_cmd->command.opcode = TWA_OP_EXECUTE_SCSI_COMMAND;
760 tr_9k_cmd->unit = lunid;
761 tr_9k_cmd->request_id = tr->tr_request_id;
762 tr_9k_cmd->status = 0;
763 tr_9k_cmd->sgl_offset = 16;
764 tr_9k_cmd->sgl_entries = 1;
765 /* create the CDB here */
766 tr_9k_cmd->cdb[0] = READ_CAPACITY_16;
767 tr_9k_cmd->cdb[1] = ((lunid << 5) & 0x0e) | SRC16_SERVICE_ACTION;
768
769 error = twa_immediate_request(tr, TWA_REQUEST_TIMEOUT_PERIOD);
770
771 if (error == 0) {
772 #if BYTE_ORDER == BIG_ENDIAN
773 array_size = bswap64(_8btol(
774 ((struct scsipi_read_capacity_16_data *)tr->tr_data->addr) + 1);
775 #else
776 array_size = _8btol(((struct scsipi_read_capacity_16_data *)
777 tr->tr_data)->addr) + 1;
778 #endif
779 }
780 return (array_size);
781 }
782
783 static int
784 twa_request_sense(struct twa_request *tr, int lunid)
785 {
786 int error = 1;
787 struct twa_command_9k *tr_9k_cmd;
788
789 if (tr->tr_data == NULL)
790 return (error);
791
792 memset(tr->tr_data, 0, TWA_SECTOR_SIZE);
793
794 tr->tr_length = TWA_SECTOR_SIZE;
795 tr->tr_cmd_pkt_type = TWA_CMD_PKT_TYPE_9K;
796 tr->tr_flags |= TWA_CMD_DATA_OUT;
797
798 tr_9k_cmd = &tr->tr_command->command.cmd_pkt_9k;
799
800 tr_9k_cmd->command.opcode = TWA_OP_EXECUTE_SCSI_COMMAND;
801 tr_9k_cmd->unit = lunid;
802 tr_9k_cmd->request_id = tr->tr_request_id;
803 tr_9k_cmd->status = 0;
804 tr_9k_cmd->sgl_offset = 16;
805 tr_9k_cmd->sgl_entries = 1;
806 /* create the CDB here */
807 tr_9k_cmd->cdb[0] = SCSI_REQUEST_SENSE;
808 tr_9k_cmd->cdb[1] = ((lunid << 5) & 0x0e);
809 tr_9k_cmd->cdb[4] = 255;
810
811 /*XXX AEN notification called in interrupt context
812 * so just queue the request. Return as quickly
813 * as possible from interrupt
814 */
815 if ((tr->tr_flags & TWA_CMD_AEN) != 0)
816 error = twa_map_request(tr);
817 else
818 error = twa_immediate_request(tr, TWA_REQUEST_TIMEOUT_PERIOD);
819
820 return (error);
821 }
822
823 static int
824 twa_alloc_req_pkts(struct twa_softc *sc, int num_reqs)
825 {
826 struct twa_request *tr;
827 struct twa_command_packet *tc;
828 bus_dma_segment_t seg;
829 size_t max_segs, max_xfer;
830 int i, rv, rseg, size;
831
832 if ((sc->twa_req_buf = malloc(num_reqs * sizeof(struct twa_request),
833 M_DEVBUF, M_NOWAIT)) == NULL)
834 return(ENOMEM);
835
836 size = num_reqs * sizeof(struct twa_command_packet);
837
838 /* Allocate memory for cmd pkts. */
839 if ((rv = bus_dmamem_alloc(sc->twa_dma_tag,
840 size, PAGE_SIZE, 0, &seg,
841 1, &rseg, BUS_DMA_NOWAIT)) != 0){
842 aprint_error_dev(&sc->twa_dv, "unable to allocate "
843 "command packets, rv = %d\n", rv);
844 return (ENOMEM);
845 }
846
847 if ((rv = bus_dmamem_map(sc->twa_dma_tag,
848 &seg, rseg, size, (void **)&sc->twa_cmds,
849 BUS_DMA_NOWAIT | BUS_DMA_COHERENT)) != 0) {
850 aprint_error_dev(&sc->twa_dv, "unable to map commands, rv = %d\n", rv);
851 return (1);
852 }
853
854 if ((rv = bus_dmamap_create(sc->twa_dma_tag,
855 size, num_reqs, size,
856 0, BUS_DMA_NOWAIT, &sc->twa_cmd_map)) != 0) {
857 aprint_error_dev(&sc->twa_dv, "unable to create command DMA map, "
858 "rv = %d\n", rv);
859 return (ENOMEM);
860 }
861
862 if ((rv = bus_dmamap_load(sc->twa_dma_tag, sc->twa_cmd_map,
863 sc->twa_cmds, size, NULL,
864 BUS_DMA_NOWAIT)) != 0) {
865 aprint_error_dev(&sc->twa_dv, "unable to load command DMA map, "
866 "rv = %d\n", rv);
867 return (1);
868 }
869
870 if ((uintptr_t)sc->twa_cmds % TWA_ALIGNMENT) {
871 aprint_error_dev(&sc->twa_dv, "DMA map memory not aligned on %d boundary\n", TWA_ALIGNMENT);
872
873 return (1);
874 }
875 tc = sc->twa_cmd_pkt_buf = (struct twa_command_packet *)sc->twa_cmds;
876 sc->twa_cmd_pkt_phys = sc->twa_cmd_map->dm_segs[0].ds_addr;
877
878 memset(sc->twa_req_buf, 0, num_reqs * sizeof(struct twa_request));
879 memset(sc->twa_cmd_pkt_buf, 0,
880 num_reqs * sizeof(struct twa_command_packet));
881
882 sc->sc_twa_request = sc->twa_req_buf;
883 max_segs = twa_get_maxsegs();
884 max_xfer = twa_get_maxxfer(max_segs);
885
886 for (i = 0; i < num_reqs; i++, tc++) {
887 tr = &(sc->twa_req_buf[i]);
888 tr->tr_command = tc;
889 tr->tr_cmd_phys = sc->twa_cmd_pkt_phys +
890 (i * sizeof(struct twa_command_packet));
891 tr->tr_request_id = i;
892 tr->tr_sc = sc;
893
894 /*
895 * Create a map for data buffers. maxsize (256 * 1024) used in
896 * bus_dma_tag_create above should suffice the bounce page needs
897 * for data buffers, since the max I/O size we support is 128KB.
898 * If we supported I/O's bigger than 256KB, we would have to
899 * create a second dma_tag, with the appropriate maxsize.
900 */
901 if ((rv = bus_dmamap_create(sc->twa_dma_tag,
902 max_xfer, max_segs, 1, 0, BUS_DMA_NOWAIT,
903 &tr->tr_dma_map)) != 0) {
904 aprint_error_dev(&sc->twa_dv, "unable to create command "
905 "DMA map, rv = %d\n", rv);
906 return (ENOMEM);
907 }
908 /* Insert request into the free queue. */
909 if (i != 0) {
910 sc->twa_lookup[i] = tr;
911 twa_release_request(tr);
912 } else
913 tr->tr_flags |= TWA_CMD_AEN;
914 }
915 return(0);
916 }
917
918 static void
919 twa_recompute_openings(struct twa_softc *sc)
920 {
921 struct twa_drive *td;
922 int unit;
923 int openings;
924
925 if (sc->sc_nunits != 0)
926 openings = ((TWA_Q_LENGTH / 2) / sc->sc_nunits);
927 else
928 openings = 0;
929 if (openings == sc->sc_openings)
930 return;
931 sc->sc_openings = openings;
932
933 #ifdef TWA_DEBUG
934 printf("%s: %d array%s, %d openings per array\n",
935 device_xname(&sc->twa_dv), sc->sc_nunits,
936 sc->sc_nunits == 1 ? "" : "s", sc->sc_openings);
937 #endif
938 for (unit = 0; unit < TWA_MAX_UNITS; unit++) {
939 td = &sc->sc_units[unit];
940 if (td->td_dev != NULL)
941 (*td->td_callbacks->tcb_openings)(td->td_dev,
942 sc->sc_openings);
943 }
944 }
945
946 static int
947 twa_request_bus_scan(struct twa_softc *sc)
948 {
949 struct twa_drive *td;
950 struct twa_request *tr;
951 struct twa_attach_args twaa;
952 int locs[TWACF_NLOCS];
953 int s, unit;
954
955 s = splbio();
956 for (unit = 0; unit < TWA_MAX_UNITS; unit++) {
957
958 if ((tr = twa_get_request(sc, 0)) == NULL) {
959 splx(s);
960 return (EIO);
961 }
962
963 tr->tr_cmd_pkt_type |= TWA_CMD_PKT_TYPE_INTERNAL;
964
965 tr->tr_data = malloc(TWA_SECTOR_SIZE, M_DEVBUF, M_NOWAIT);
966
967 if (tr->tr_data == NULL) {
968 twa_release_request(tr);
969 splx(s);
970 return (ENOMEM);
971 }
972 td = &sc->sc_units[unit];
973
974 if (twa_inquiry(tr, unit) == 0) {
975 if (td->td_dev == NULL) {
976 twa_print_inquiry_data(sc,
977 ((struct scsipi_inquiry_data *)tr->tr_data));
978
979 sc->sc_nunits++;
980
981 sc->sc_units[unit].td_size =
982 twa_read_capacity(tr, unit);
983
984 twaa.twaa_unit = unit;
985
986 twa_recompute_openings(sc);
987
988 locs[TWACF_UNIT] = unit;
989
990 sc->sc_units[unit].td_dev =
991 config_found_sm_loc(&sc->twa_dv, "twa",
992 locs, &twaa, twa_print, config_stdsubmatch);
993 }
994 } else {
995 if (td->td_dev != NULL) {
996 sc->sc_nunits--;
997
998 (void) config_detach(td->td_dev, DETACH_FORCE);
999 td->td_dev = NULL;
1000 td->td_size = 0;
1001
1002 twa_recompute_openings(sc);
1003 }
1004 }
1005 free(tr->tr_data, M_DEVBUF);
1006
1007 twa_release_request(tr);
1008 }
1009 splx(s);
1010
1011 return (0);
1012 }
1013
1014
1015 #ifdef DIAGNOSTIC
1016 static inline void
1017 twa_check_busy_q(struct twa_request *tr)
1018 {
1019 struct twa_request *rq;
1020 struct twa_softc *sc = tr->tr_sc;
1021
1022 TAILQ_FOREACH(rq, &sc->twa_busy, tr_link) {
1023 if (tr->tr_request_id == rq->tr_request_id) {
1024 panic("cannot submit same request more than once");
1025 } else if (tr->bp == rq->bp && tr->bp != 0) {
1026 /* XXX A check for 0 for the buf ptr is needed to
1027 * guard against ioctl requests with a buf ptr of
1028 * 0 and also aen notifications. Looking for
1029 * external cmds only.
1030 */
1031 panic("cannot submit same buf more than once");
1032 } else {
1033 /* Empty else statement */
1034 }
1035 }
1036 }
1037 #endif
1038
1039 static int
1040 twa_start(struct twa_request *tr)
1041 {
1042 struct twa_softc *sc = tr->tr_sc;
1043 uint32_t status_reg;
1044 int s;
1045 int error;
1046
1047 s = splbio();
1048 /* Check to see if we can post a command. */
1049 status_reg = twa_inl(sc, TWA_STATUS_REGISTER_OFFSET);
1050 if ((error = twa_check_ctlr_state(sc, status_reg)))
1051 goto out;
1052
1053 if (status_reg & TWA_STATUS_COMMAND_QUEUE_FULL) {
1054 if (tr->tr_status != TWA_CMD_PENDING) {
1055 tr->tr_status = TWA_CMD_PENDING;
1056 TAILQ_INSERT_TAIL(&tr->tr_sc->twa_pending,
1057 tr, tr_link);
1058 }
1059 twa_outl(sc, TWA_CONTROL_REGISTER_OFFSET,
1060 TWA_CONTROL_UNMASK_COMMAND_INTERRUPT);
1061 error = EBUSY;
1062 } else {
1063 bus_dmamap_sync(sc->twa_dma_tag, sc->twa_cmd_map,
1064 (char *)tr->tr_command - (char *)sc->twa_cmds,
1065 sizeof(struct twa_command_packet),
1066 BUS_DMASYNC_PREWRITE | BUS_DMASYNC_PREREAD);
1067
1068 /* Cmd queue is not full. Post the command. */
1069 TWA_WRITE_COMMAND_QUEUE(sc, tr->tr_cmd_phys +
1070 sizeof(struct twa_command_header));
1071
1072 /* Mark the request as currently being processed. */
1073 tr->tr_status = TWA_CMD_BUSY;
1074
1075 #ifdef DIAGNOSTIC
1076 twa_check_busy_q(tr);
1077 #endif
1078
1079 /* Move the request into the busy queue. */
1080 TAILQ_INSERT_TAIL(&tr->tr_sc->twa_busy, tr, tr_link);
1081 }
1082 out:
1083 splx(s);
1084 return(error);
1085 }
1086
1087 static int
1088 twa_drain_response_queue(struct twa_softc *sc)
1089 {
1090 union twa_response_queue rq;
1091 uint32_t status_reg;
1092
1093 for (;;) {
1094 status_reg = twa_inl(sc, TWA_STATUS_REGISTER_OFFSET);
1095 if (twa_check_ctlr_state(sc, status_reg))
1096 return(1);
1097 if (status_reg & TWA_STATUS_RESPONSE_QUEUE_EMPTY)
1098 return(0); /* no more response queue entries */
1099 rq = (union twa_response_queue)twa_inl(sc,
1100 TWA_RESPONSE_QUEUE_OFFSET);
1101 }
1102 }
1103
1104 static void
1105 twa_drain_busy_queue(struct twa_softc *sc)
1106 {
1107 struct twa_request *tr;
1108
1109 /* Walk the busy queue. */
1110
1111 while ((tr = TAILQ_FIRST(&sc->twa_busy)) != NULL) {
1112 TAILQ_REMOVE(&sc->twa_busy, tr, tr_link);
1113
1114 twa_unmap_request(tr);
1115 if ((tr->tr_cmd_pkt_type & TWA_CMD_PKT_TYPE_INTERNAL) ||
1116 (tr->tr_cmd_pkt_type & TWA_CMD_PKT_TYPE_IOCTL)) {
1117 /* It's an internal/ioctl request. Simply free it. */
1118 if (tr->tr_data)
1119 free(tr->tr_data, M_DEVBUF);
1120 twa_release_request(tr);
1121 } else {
1122 /* It's a SCSI request. Complete it. */
1123 tr->tr_command->command.cmd_pkt_9k.status = EIO;
1124 if (tr->tr_callback)
1125 tr->tr_callback(tr);
1126 }
1127 }
1128 }
1129
1130 static int
1131 twa_drain_pending_queue(struct twa_softc *sc)
1132 {
1133 struct twa_request *tr;
1134 int s, error = 0;
1135
1136 /*
1137 * Pull requests off the pending queue, and submit them.
1138 */
1139 s = splbio();
1140 while ((tr = TAILQ_FIRST(&sc->twa_pending)) != NULL) {
1141 TAILQ_REMOVE(&sc->twa_pending, tr, tr_link);
1142
1143 if ((error = twa_start(tr))) {
1144 if (error == EBUSY) {
1145 tr->tr_status = TWA_CMD_PENDING;
1146
1147 /* queue at the head */
1148 TAILQ_INSERT_HEAD(&tr->tr_sc->twa_pending,
1149 tr, tr_link);
1150 error = 0;
1151 break;
1152 } else {
1153 if (tr->tr_flags & TWA_CMD_SLEEP_ON_REQUEST) {
1154 tr->tr_error = error;
1155 tr->tr_callback(tr);
1156 error = EIO;
1157 }
1158 }
1159 }
1160 }
1161 splx(s);
1162
1163 return(error);
1164 }
1165
1166 static int
1167 twa_drain_aen_queue(struct twa_softc *sc)
1168 {
1169 int s, error = 0;
1170 struct twa_request *tr;
1171 struct twa_command_header *cmd_hdr;
1172 struct timeval t1;
1173 uint32_t timeout;
1174
1175 for (;;) {
1176 if ((tr = twa_get_request(sc, 0)) == NULL) {
1177 error = EIO;
1178 break;
1179 }
1180 tr->tr_cmd_pkt_type |= TWA_CMD_PKT_TYPE_INTERNAL;
1181 tr->tr_callback = NULL;
1182
1183 tr->tr_data = malloc(TWA_SECTOR_SIZE, M_DEVBUF, M_NOWAIT);
1184
1185 if (tr->tr_data == NULL) {
1186 error = 1;
1187 goto out;
1188 }
1189
1190 if (twa_request_sense(tr, 0) != 0) {
1191 error = 1;
1192 break;
1193 }
1194
1195 timeout = (1000/*ms*/ * 100/*us*/ * TWA_REQUEST_TIMEOUT_PERIOD);
1196
1197 microtime(&t1);
1198
1199 timeout += t1.tv_usec;
1200
1201 do {
1202 s = splbio();
1203 twa_done(tr->tr_sc);
1204 splx(s);
1205 if (tr->tr_status != TWA_CMD_BUSY)
1206 break;
1207 microtime(&t1);
1208 } while (t1.tv_usec <= timeout);
1209
1210 if (tr->tr_status != TWA_CMD_COMPLETE) {
1211 error = ETIMEDOUT;
1212 break;
1213 }
1214
1215 if ((error = tr->tr_command->command.cmd_pkt_9k.status))
1216 break;
1217
1218 cmd_hdr = (struct twa_command_header *)(tr->tr_data);
1219 if ((cmd_hdr->status_block.error) /* aen_code */
1220 == TWA_AEN_QUEUE_EMPTY)
1221 break;
1222 (void)twa_enqueue_aen(sc, cmd_hdr);
1223
1224 free(tr->tr_data, M_DEVBUF);
1225 twa_release_request(tr);
1226 }
1227 out:
1228 if (tr) {
1229 if (tr->tr_data)
1230 free(tr->tr_data, M_DEVBUF);
1231
1232 twa_release_request(tr);
1233 }
1234 return(error);
1235 }
1236
1237
1238 #ifdef DIAGNOSTIC
1239 static void
1240 twa_check_response_q(struct twa_request *tr, int clear)
1241 {
1242 int j;
1243 static int i = 0;
1244 static struct twa_request *req = 0;
1245 static struct buf *hist[255];
1246
1247
1248 if (clear) {
1249 i = 0;
1250 for (j = 0; j < 255; j++)
1251 hist[j] = 0;
1252 return;
1253 }
1254
1255 if (req == 0)
1256 req = tr;
1257
1258 if ((tr->tr_cmd_pkt_type & TWA_CMD_PKT_TYPE_EXTERNAL) != 0) {
1259 if (req->tr_request_id == tr->tr_request_id)
1260 panic("req id: %d on controller queue twice",
1261 tr->tr_request_id);
1262
1263 for (j = 0; j < i; j++)
1264 if (tr->bp == hist[j])
1265 panic("req id: %d buf found twice",
1266 tr->tr_request_id);
1267 }
1268 req = tr;
1269
1270 hist[i++] = req->bp;
1271 }
1272 #endif
1273
1274 static int
1275 twa_done(struct twa_softc *sc)
1276 {
1277 union twa_response_queue rq;
1278 struct twa_request *tr;
1279 int rv = 0;
1280 uint32_t status_reg;
1281
1282 for (;;) {
1283 status_reg = twa_inl(sc, TWA_STATUS_REGISTER_OFFSET);
1284 if ((rv = twa_check_ctlr_state(sc, status_reg)))
1285 break;
1286 if (status_reg & TWA_STATUS_RESPONSE_QUEUE_EMPTY)
1287 break;
1288 /* Response queue is not empty. */
1289 rq = (union twa_response_queue)twa_inl(sc,
1290 TWA_RESPONSE_QUEUE_OFFSET);
1291 tr = sc->sc_twa_request + rq.u.response_id;
1292 #ifdef DIAGNOSTIC
1293 twa_check_response_q(tr, 0);
1294 #endif
1295 /* Unmap the command packet, and any associated data buffer. */
1296 twa_unmap_request(tr);
1297
1298 tr->tr_status = TWA_CMD_COMPLETE;
1299 TAILQ_REMOVE(&tr->tr_sc->twa_busy, tr, tr_link);
1300
1301 if (tr->tr_callback)
1302 tr->tr_callback(tr);
1303 }
1304 (void)twa_drain_pending_queue(sc);
1305
1306 #ifdef DIAGNOSTIC
1307 twa_check_response_q(NULL, 1);
1308 #endif
1309 return(rv);
1310 }
1311
1312 /*
1313 * Function name: twa_init_ctlr
1314 * Description: Establishes a logical connection with the controller.
1315 * If bundled with firmware, determines whether or not
1316 * to flash firmware, based on arch_id, fw SRL (Spec.
1317 * Revision Level), branch & build #'s. Also determines
1318 * whether or not the driver is compatible with the
1319 * firmware on the controller, before proceeding to work
1320 * with it.
1321 *
1322 * Input: sc -- ptr to per ctlr structure
1323 * Output: None
1324 * Return value: 0 -- success
1325 * non-zero-- failure
1326 */
1327 static int
1328 twa_init_ctlr(struct twa_softc *sc)
1329 {
1330 uint16_t fw_on_ctlr_srl = 0;
1331 uint16_t fw_on_ctlr_arch_id = 0;
1332 uint16_t fw_on_ctlr_branch = 0;
1333 uint16_t fw_on_ctlr_build = 0;
1334 uint32_t init_connect_result = 0;
1335 int error = 0;
1336 #if 0
1337 int8_t fw_flashed = FALSE;
1338 int8_t fw_flash_failed = FALSE;
1339 #endif
1340
1341 /* Wait for the controller to become ready. */
1342 if (twa_wait_status(sc, TWA_STATUS_MICROCONTROLLER_READY,
1343 TWA_REQUEST_TIMEOUT_PERIOD)) {
1344 return(ENXIO);
1345 }
1346 /* Drain the response queue. */
1347 if (twa_drain_response_queue(sc))
1348 return(1);
1349
1350 /* Establish a logical connection with the controller. */
1351 if ((error = twa_init_connection(sc, TWA_INIT_MESSAGE_CREDITS,
1352 TWA_EXTENDED_INIT_CONNECT, TWA_CURRENT_FW_SRL,
1353 TWA_9000_ARCH_ID, TWA_CURRENT_FW_BRANCH,
1354 TWA_CURRENT_FW_BUILD, &fw_on_ctlr_srl,
1355 &fw_on_ctlr_arch_id, &fw_on_ctlr_branch,
1356 &fw_on_ctlr_build, &init_connect_result))) {
1357 return(error);
1358 }
1359 #if 0
1360 if ((init_connect_result & TWA_BUNDLED_FW_SAFE_TO_FLASH) &&
1361 (init_connect_result & TWA_CTLR_FW_RECOMMENDS_FLASH)) {
1362 /*
1363 * The bundled firmware is safe to flash, and the firmware
1364 * on the controller recommends a flash. So, flash!
1365 */
1366 printf("%s: flashing bundled firmware...\n",
1367 device_xname(&sc->twa_dv));
1368
1369 if ((error = twa_flash_firmware(sc))) {
1370 fw_flash_failed = TRUE;
1371
1372 printf("%s: unable to flash bundled firmware.\n",
1373 device_xname(&sc->twa_dv));
1374 } else {
1375 printf("%s: successfully flashed bundled firmware.\n",
1376 device_xname(&sc->twa_dv));
1377 fw_flashed = TRUE;
1378 }
1379 }
1380 if (fw_flashed) {
1381 /* The firmware was flashed. Have the new image loaded */
1382 error = twa_hard_reset(sc);
1383 if (error == 0)
1384 error = twa_init_ctlr(sc);
1385 /*
1386 * If hard reset of controller failed, we need to return.
1387 * Otherwise, the above recursive call to twa_init_ctlr will
1388 * have completed the rest of the initialization (starting
1389 * from twa_drain_aen_queue below). Don't do it again.
1390 * Just return.
1391 */
1392 return(error);
1393 } else {
1394 /*
1395 * Either we are not bundled with a firmware image, or
1396 * the bundled firmware is not safe to flash,
1397 * or flash failed for some reason. See if we can at
1398 * least work with the firmware on the controller in the
1399 * current mode.
1400 */
1401 if (init_connect_result & TWA_CTLR_FW_COMPATIBLE) {
1402 /* Yes, we can. Make note of the operating mode. */
1403 sc->working_srl = TWA_CURRENT_FW_SRL;
1404 sc->working_branch = TWA_CURRENT_FW_BRANCH;
1405 sc->working_build = TWA_CURRENT_FW_BUILD;
1406 } else {
1407 /*
1408 * No, we can't. See if we can at least work with
1409 * it in the base mode. We should never come here
1410 * if firmware has just been flashed.
1411 */
1412 printf("%s: Driver/Firmware mismatch. Negotiating "
1413 "for base level.\n", device_xname(&sc->twa_dv));
1414 if ((error = twa_init_connection(sc,
1415 TWA_INIT_MESSAGE_CREDITS,
1416 TWA_EXTENDED_INIT_CONNECT, TWA_BASE_FW_SRL,
1417 TWA_9000_ARCH_ID, TWA_BASE_FW_BRANCH,
1418 TWA_BASE_FW_BUILD, &fw_on_ctlr_srl,
1419 &fw_on_ctlr_arch_id, &fw_on_ctlr_branch,
1420 &fw_on_ctlr_build, &init_connect_result))) {
1421 printf("%s: can't initialize connection in "
1422 "base mode.\n", device_xname(&sc->twa_dv));
1423 return(error);
1424 }
1425 if (!(init_connect_result & TWA_CTLR_FW_COMPATIBLE)) {
1426 /*
1427 * The firmware on the controller is not even
1428 * compatible with our base mode. We cannot
1429 * work with it. Bail...
1430 */
1431 printf("Incompatible firmware on controller\n");
1432 #ifdef TWA_FLASH_FIRMWARE
1433 if (fw_flash_failed)
1434 printf("...and could not flash bundled "
1435 "firmware.\n");
1436 else
1437 printf("...and bundled firmware not "
1438 "safe to flash.\n");
1439 #endif /* TWA_FLASH_FIRMWARE */
1440 return(1);
1441 }
1442 /*
1443 * We can work with this firmware, but only in
1444 * base mode.
1445 */
1446 sc->working_srl = TWA_BASE_FW_SRL;
1447 sc->working_branch = TWA_BASE_FW_BRANCH;
1448 sc->working_build = TWA_BASE_FW_BUILD;
1449 sc->twa_operating_mode = TWA_BASE_MODE;
1450 }
1451 }
1452 #endif
1453 twa_drain_aen_queue(sc);
1454
1455 /* Set controller state to initialized. */
1456 sc->twa_state &= ~TWA_STATE_SHUTDOWN;
1457 return(0);
1458 }
1459
1460 static int
1461 twa_setup(struct twa_softc *sc)
1462 {
1463 struct tw_cl_event_packet *aen_queue;
1464 uint32_t i = 0;
1465 int error = 0;
1466
1467 /* Initialize request queues. */
1468 TAILQ_INIT(&sc->twa_free);
1469 TAILQ_INIT(&sc->twa_busy);
1470 TAILQ_INIT(&sc->twa_pending);
1471
1472 sc->sc_nunits = 0;
1473 sc->twa_sc_flags = 0;
1474
1475 if (twa_alloc_req_pkts(sc, TWA_Q_LENGTH)) {
1476
1477 return(ENOMEM);
1478 }
1479
1480 /* Allocate memory for the AEN queue. */
1481 if ((aen_queue = malloc(sizeof(struct tw_cl_event_packet) *
1482 TWA_Q_LENGTH, M_DEVBUF, M_WAITOK)) == NULL) {
1483 /*
1484 * This should not cause us to return error. We will only be
1485 * unable to support AEN's. But then, we will have to check
1486 * time and again to see if we can support AEN's, if we
1487 * continue. So, we will just return error.
1488 */
1489 return (ENOMEM);
1490 }
1491 /* Initialize the aen queue. */
1492 memset(aen_queue, 0, sizeof(struct tw_cl_event_packet) * TWA_Q_LENGTH);
1493
1494 for (i = 0; i < TWA_Q_LENGTH; i++)
1495 sc->twa_aen_queue[i] = &(aen_queue[i]);
1496
1497 twa_outl(sc, TWA_CONTROL_REGISTER_OFFSET,
1498 TWA_CONTROL_DISABLE_INTERRUPTS);
1499
1500 /* Initialize the controller. */
1501 if ((error = twa_init_ctlr(sc))) {
1502 /* Soft reset the controller, and try one more time. */
1503
1504 printf("%s: controller initialization failed. "
1505 "Retrying initialization\n", device_xname(&sc->twa_dv));
1506
1507 if ((error = twa_soft_reset(sc)) == 0)
1508 error = twa_init_ctlr(sc);
1509 }
1510
1511 twa_describe_controller(sc);
1512
1513 error = twa_request_bus_scan(sc);
1514
1515 twa_outl(sc, TWA_CONTROL_REGISTER_OFFSET,
1516 TWA_CONTROL_CLEAR_ATTENTION_INTERRUPT |
1517 TWA_CONTROL_UNMASK_RESPONSE_INTERRUPT |
1518 TWA_CONTROL_ENABLE_INTERRUPTS);
1519
1520 return (error);
1521 }
1522
1523 void *twa_sdh;
1524
1525 static void
1526 twa_attach(struct device *parent, struct device *self, void *aux)
1527 {
1528 struct pci_attach_args *pa;
1529 struct twa_softc *sc;
1530 pci_chipset_tag_t pc;
1531 pcireg_t csr;
1532 pci_intr_handle_t ih;
1533 const char *intrstr;
1534 struct ctlname ctlnames[] = CTL_NAMES;
1535 const struct sysctlnode *node;
1536 int i;
1537
1538 sc = (struct twa_softc *)self;
1539
1540 pa = aux;
1541 pc = pa->pa_pc;
1542 sc->pc = pa->pa_pc;
1543 sc->tag = pa->pa_tag;
1544 sc->twa_dma_tag = pa->pa_dmat;
1545
1546 aprint_naive(": RAID controller\n");
1547 aprint_normal(": 3ware Apache\n");
1548
1549 if (PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_3WARE_9000) {
1550 if (pci_mapreg_map(pa, PCI_MAPREG_START, PCI_MAPREG_TYPE_IO, 0,
1551 &sc->twa_bus_iot, &sc->twa_bus_ioh, NULL, NULL)) {
1552 aprint_error_dev(&sc->twa_dv, "can't map i/o space\n");
1553 return;
1554 }
1555 } else if (PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_3WARE_9550) {
1556 if (pci_mapreg_map(pa, PCI_MAPREG_START + 0x08,
1557 PCI_MAPREG_MEM_TYPE_64BIT, 0, &sc->twa_bus_iot,
1558 &sc->twa_bus_ioh, NULL, NULL)) {
1559 aprint_error_dev(&sc->twa_dv, "can't map mem space\n");
1560 return;
1561 }
1562 } else {
1563 aprint_error_dev(&sc->twa_dv, "product id 0x%02x not recognized\n",
1564 PCI_PRODUCT(pa->pa_id));
1565 return;
1566 }
1567 /* Enable the device. */
1568 csr = pci_conf_read(pa->pa_pc, pa->pa_tag, PCI_COMMAND_STATUS_REG);
1569
1570 pci_conf_write(pa->pa_pc, pa->pa_tag, PCI_COMMAND_STATUS_REG,
1571 csr | PCI_COMMAND_MASTER_ENABLE);
1572
1573 /* Map and establish the interrupt. */
1574 if (pci_intr_map(pa, &ih)) {
1575 aprint_error_dev(&sc->twa_dv, "can't map interrupt\n");
1576 return;
1577 }
1578 intrstr = pci_intr_string(pc, ih);
1579
1580 sc->twa_ih = pci_intr_establish(pc, ih, IPL_BIO, twa_intr, sc);
1581 if (sc->twa_ih == NULL) {
1582 aprint_error_dev(&sc->twa_dv, "can't establish interrupt%s%s\n",
1583 (intrstr) ? " at " : "",
1584 (intrstr) ? intrstr : "");
1585 return;
1586 }
1587
1588 if (intrstr != NULL)
1589 aprint_normal_dev(&sc->twa_dv, "interrupting at %s\n",
1590 intrstr);
1591
1592 twa_setup(sc);
1593
1594 if (twa_sdh == NULL)
1595 twa_sdh = shutdownhook_establish(twa_shutdown, NULL);
1596
1597 /* sysctl set-up for 3ware cli */
1598 if (sysctl_createv(NULL, 0, NULL, NULL,
1599 CTLFLAG_PERMANENT, CTLTYPE_NODE, "hw",
1600 NULL, NULL, 0, NULL, 0,
1601 CTL_HW, CTL_EOL) != 0) {
1602 aprint_error_dev(&sc->twa_dv, "could not create %s sysctl node\n",
1603 ctlnames[CTL_HW].ctl_name);
1604 return;
1605 }
1606 if (sysctl_createv(NULL, 0, NULL, &node,
1607 0, CTLTYPE_NODE, device_xname(&sc->twa_dv),
1608 SYSCTL_DESCR("twa driver information"),
1609 NULL, 0, NULL, 0,
1610 CTL_HW, CTL_CREATE, CTL_EOL) != 0) {
1611 aprint_error_dev(&sc->twa_dv, "could not create %s.%s sysctl node\n",
1612 ctlnames[CTL_HW].ctl_name,
1613 device_xname(&sc->twa_dv));
1614 return;
1615 }
1616 if ((i = sysctl_createv(NULL, 0, NULL, NULL,
1617 0, CTLTYPE_STRING, "driver_version",
1618 SYSCTL_DESCR("twa driver version"),
1619 NULL, 0, &twaver, 0,
1620 CTL_HW, node->sysctl_num, CTL_CREATE, CTL_EOL))
1621 != 0) {
1622 aprint_error_dev(&sc->twa_dv, "could not create %s.%s.driver_version sysctl\n",
1623 ctlnames[CTL_HW].ctl_name,
1624 device_xname(&sc->twa_dv));
1625 return;
1626 }
1627
1628 return;
1629 }
1630
1631 static void
1632 twa_shutdown(void *arg)
1633 {
1634 extern struct cfdriver twa_cd;
1635 struct twa_softc *sc;
1636 int i, rv, unit;
1637
1638 for (i = 0; i < twa_cd.cd_ndevs; i++) {
1639 if ((sc = device_lookup(&twa_cd, i)) == NULL)
1640 continue;
1641
1642 for (unit = 0; unit < TWA_MAX_UNITS; unit++)
1643 if (sc->sc_units[unit].td_dev != NULL)
1644 (void) config_detach(sc->sc_units[unit].td_dev,
1645 DETACH_FORCE | DETACH_QUIET);
1646
1647 twa_outl(sc, TWA_CONTROL_REGISTER_OFFSET,
1648 TWA_CONTROL_DISABLE_INTERRUPTS);
1649
1650 /* Let the controller know that we are going down. */
1651 rv = twa_init_connection(sc, TWA_SHUTDOWN_MESSAGE_CREDITS,
1652 0, 0, 0, 0, 0,
1653 NULL, NULL, NULL, NULL, NULL);
1654 }
1655 }
1656
1657 void
1658 twa_register_callbacks(struct twa_softc *sc, int unit,
1659 const struct twa_callbacks *tcb)
1660 {
1661
1662 sc->sc_units[unit].td_callbacks = tcb;
1663 }
1664
1665 /*
1666 * Print autoconfiguration message for a sub-device
1667 */
1668 static int
1669 twa_print(void *aux, const char *pnp)
1670 {
1671 struct twa_attach_args *twaa;
1672
1673 twaa = aux;
1674
1675 if (pnp !=NULL)
1676 aprint_normal("block device at %s\n", pnp);
1677 aprint_normal(" unit %d\n", twaa->twaa_unit);
1678 return (UNCONF);
1679 }
1680
1681 static void
1682 twa_fillin_sgl(struct twa_sg *sgl, bus_dma_segment_t *segs, int nsegments)
1683 {
1684 int i;
1685 for (i = 0; i < nsegments; i++) {
1686 sgl[i].address = segs[i].ds_addr;
1687 sgl[i].length = (uint32_t)(segs[i].ds_len);
1688 }
1689 }
1690
1691 static int
1692 twa_submit_io(struct twa_request *tr)
1693 {
1694 int error;
1695
1696 if ((error = twa_start(tr))) {
1697 if (error == EBUSY)
1698 error = 0; /* request is in the pending queue */
1699 else {
1700 tr->tr_error = error;
1701 }
1702 }
1703 return(error);
1704 }
1705
1706 /*
1707 * Function name: twa_setup_data_dmamap
1708 * Description: Callback of bus_dmamap_load for the buffer associated
1709 * with data. Updates the cmd pkt (size/sgl_entries
1710 * fields, as applicable) to reflect the number of sg
1711 * elements.
1712 *
1713 * Input: arg -- ptr to request pkt
1714 * segs -- ptr to a list of segment descriptors
1715 * nsegments--# of segments
1716 * error -- 0 if no errors encountered before callback,
1717 * non-zero if errors were encountered
1718 * Output: None
1719 * Return value: None
1720 */
1721 static int
1722 twa_setup_data_dmamap(void *arg, bus_dma_segment_t *segs, int nsegments,
1723 int error)
1724 {
1725 struct twa_request *tr = (struct twa_request *)arg;
1726 struct twa_command_packet *cmdpkt = tr->tr_command;
1727 struct twa_command_9k *cmd9k;
1728 union twa_command_7k *cmd7k;
1729 uint8_t sgl_offset;
1730
1731 if (error == EFBIG) {
1732 tr->tr_error = error;
1733 goto out;
1734 }
1735
1736 if (tr->tr_cmd_pkt_type & TWA_CMD_PKT_TYPE_9K) {
1737 cmd9k = &(cmdpkt->command.cmd_pkt_9k);
1738 twa_fillin_sgl(&(cmd9k->sg_list[0]), segs, nsegments);
1739 cmd9k->sgl_entries += nsegments - 1;
1740 } else {
1741 /* It's a 7000 command packet. */
1742 cmd7k = &(cmdpkt->command.cmd_pkt_7k);
1743 if ((sgl_offset = cmdpkt->command.cmd_pkt_7k.generic.sgl_offset))
1744 twa_fillin_sgl((struct twa_sg *)
1745 (((uint32_t *)cmd7k) + sgl_offset),
1746 segs, nsegments);
1747 /* Modify the size field, based on sg address size. */
1748 cmd7k->generic.size +=
1749 ((TWA_64BIT_ADDRESSES ? 3 : 2) * nsegments);
1750 }
1751
1752 if (tr->tr_flags & TWA_CMD_DATA_IN)
1753 bus_dmamap_sync(tr->tr_sc->twa_dma_tag, tr->tr_dma_map, 0,
1754 tr->tr_length, BUS_DMASYNC_PREREAD);
1755 if (tr->tr_flags & TWA_CMD_DATA_OUT) {
1756 /*
1757 * If we're using an alignment buffer, and we're
1758 * writing data, copy the real data out.
1759 */
1760 if (tr->tr_flags & TWA_CMD_DATA_COPY_NEEDED)
1761 memcpy(tr->tr_data, tr->tr_real_data,
1762 tr->tr_real_length);
1763 bus_dmamap_sync(tr->tr_sc->twa_dma_tag, tr->tr_dma_map, 0,
1764 tr->tr_length, BUS_DMASYNC_PREWRITE);
1765 }
1766 error = twa_submit_io(tr);
1767
1768 out:
1769 if (error) {
1770 twa_unmap_request(tr);
1771 /*
1772 * If the caller had been returned EINPROGRESS, and he has
1773 * registered a callback for handling completion, the callback
1774 * will never get called because we were unable to submit the
1775 * request. So, free up the request right here.
1776 */
1777 if ((tr->tr_flags & TWA_CMD_IN_PROGRESS) && (tr->tr_callback))
1778 twa_release_request(tr);
1779 }
1780 return (error);
1781 }
1782
1783 /*
1784 * Function name: twa_map_request
1785 * Description: Maps a cmd pkt and data associated with it, into
1786 * DMA'able memory.
1787 *
1788 * Input: tr -- ptr to request pkt
1789 * Output: None
1790 * Return value: 0 -- success
1791 * non-zero-- failure
1792 */
1793 int
1794 twa_map_request(struct twa_request *tr)
1795 {
1796 struct twa_softc *sc = tr->tr_sc;
1797 int s, rv, error = 0;
1798
1799 /* If the command involves data, map that too. */
1800 if (tr->tr_data != NULL) {
1801
1802 if (((u_long)tr->tr_data & (511)) != 0) {
1803 tr->tr_flags |= TWA_CMD_DATA_COPY_NEEDED;
1804 tr->tr_real_data = tr->tr_data;
1805 tr->tr_real_length = tr->tr_length;
1806 s = splvm();
1807 tr->tr_data = (void *)uvm_km_alloc(kmem_map,
1808 tr->tr_length, 512, UVM_KMF_NOWAIT|UVM_KMF_WIRED);
1809 splx(s);
1810
1811 if (tr->tr_data == NULL) {
1812 tr->tr_data = tr->tr_real_data;
1813 tr->tr_length = tr->tr_real_length;
1814 return(ENOMEM);
1815 }
1816 if ((tr->tr_flags & TWA_CMD_DATA_IN) != 0)
1817 memcpy(tr->tr_data, tr->tr_real_data,
1818 tr->tr_length);
1819 }
1820
1821 /*
1822 * Map the data buffer into bus space and build the S/G list.
1823 */
1824 rv = bus_dmamap_load(sc->twa_dma_tag, tr->tr_dma_map,
1825 tr->tr_data, tr->tr_length, NULL, BUS_DMA_NOWAIT |
1826 BUS_DMA_STREAMING | (tr->tr_flags & TWA_CMD_DATA_OUT) ?
1827 BUS_DMA_READ : BUS_DMA_WRITE);
1828
1829 if (rv != 0) {
1830 if ((tr->tr_flags & TWA_CMD_DATA_COPY_NEEDED) != 0) {
1831 s = splvm();
1832 uvm_km_free(kmem_map, (vaddr_t)tr->tr_data,
1833 tr->tr_length, UVM_KMF_WIRED);
1834 splx(s);
1835 }
1836 return (rv);
1837 }
1838
1839 if ((rv = twa_setup_data_dmamap(tr,
1840 tr->tr_dma_map->dm_segs,
1841 tr->tr_dma_map->dm_nsegs, error))) {
1842
1843 if (tr->tr_flags & TWA_CMD_DATA_COPY_NEEDED) {
1844 s = splvm();
1845 uvm_km_free(kmem_map, (vaddr_t)tr->tr_data,
1846 tr->tr_length, UVM_KMF_WIRED);
1847 splx(s);
1848 tr->tr_data = tr->tr_real_data;
1849 tr->tr_length = tr->tr_real_length;
1850 }
1851 } else
1852 error = tr->tr_error;
1853
1854 } else
1855 if ((rv = twa_submit_io(tr)))
1856 twa_unmap_request(tr);
1857
1858 return (rv);
1859 }
1860
1861 #if 0
1862 /*
1863 * Function name: twa_flash_firmware
1864 * Description: Flashes bundled firmware image onto controller.
1865 *
1866 * Input: sc -- ptr to per ctlr structure
1867 * Output: None
1868 * Return value: 0 -- success
1869 * non-zero-- failure
1870 */
1871 static int
1872 twa_flash_firmware(struct twa_softc *sc)
1873 {
1874 struct twa_request *tr;
1875 struct twa_command_download_firmware *cmd;
1876 uint32_t count;
1877 uint32_t fw_img_chunk_size;
1878 uint32_t this_chunk_size = 0;
1879 uint32_t remaining_img_size = 0;
1880 int s, error = 0;
1881 int i;
1882
1883 if ((tr = twa_get_request(sc, 0)) == NULL) {
1884 /* No free request packets available. Can't proceed. */
1885 error = EIO;
1886 goto out;
1887 }
1888
1889 count = (twa_fw_img_size / 65536);
1890
1891 count += ((twa_fw_img_size % 65536) != 0) ? 1 : 0;
1892
1893 tr->tr_cmd_pkt_type |= TWA_CMD_PKT_TYPE_INTERNAL;
1894 /* Allocate sufficient memory to hold a chunk of the firmware image. */
1895 fw_img_chunk_size = ((twa_fw_img_size / count) + 511) & ~511;
1896
1897 s = splvm();
1898 tr->tr_data = (void *)uvm_km_alloc(kmem_map, fw_img_chunk_size, 512,
1899 UVM_KMF_WIRED);
1900 splx(s);
1901
1902 if (tr->tr_data == NULL) {
1903 error = ENOMEM;
1904 goto out;
1905 }
1906
1907 remaining_img_size = twa_fw_img_size;
1908 cmd = &(tr->tr_command->command.cmd_pkt_7k.download_fw);
1909
1910 for (i = 0; i < count; i++) {
1911 /* Build a cmd pkt for downloading firmware. */
1912 memset(tr->tr_command, 0, sizeof(struct twa_command_packet));
1913
1914 tr->tr_command->cmd_hdr.header_desc.size_header = 128;
1915
1916 cmd->opcode = TWA_OP_DOWNLOAD_FIRMWARE;
1917 cmd->sgl_offset = 2; /* offset in dwords, to the beginning
1918 of sg list */
1919 cmd->size = 2; /* this field will be updated at data
1920 map time */
1921 cmd->request_id = tr->tr_request_id;
1922 cmd->unit = 0;
1923 cmd->status = 0;
1924 cmd->flags = 0;
1925 cmd->param = 8; /* prom image */
1926
1927 if (i != (count - 1))
1928 this_chunk_size = fw_img_chunk_size;
1929 else /* last chunk */
1930 this_chunk_size = remaining_img_size;
1931
1932 remaining_img_size -= this_chunk_size;
1933
1934 memset(tr->tr_data, 0, fw_img_chunk_size);
1935
1936 memcpy(tr->tr_data, twa_fw_img + (i * fw_img_chunk_size),
1937 this_chunk_size);
1938 /*
1939 * The next line will effect only the last chunk.
1940 */
1941 tr->tr_length = (this_chunk_size + 511) & ~511;
1942
1943 tr->tr_flags |= TWA_CMD_DATA_OUT;
1944
1945 error = twa_immediate_request(tr, TWA_REQUEST_TIMEOUT_PERIOD);
1946
1947 if (error) {
1948 if (error == ETIMEDOUT)
1949 /* clean-up done by twa_immediate_request */
1950 return(error);
1951 break;
1952 }
1953 error = cmd->status;
1954
1955 if (i != (count - 1)) {
1956
1957 /*
1958 * XXX FreeBSD code doesn't check for no error condition
1959 * but based on observation, error seems to return 0
1960 */
1961 if ((error =
1962 tr->tr_command->cmd_hdr.status_block.error) == 0) {
1963 continue;
1964 } else if ((error =
1965 tr->tr_command->cmd_hdr.status_block.error) ==
1966 TWA_ERROR_MORE_DATA) {
1967 continue;
1968 } else {
1969 twa_hard_reset(sc);
1970 break;
1971 }
1972 } else /* last chunk */
1973 if (error) {
1974 aprint_error_dev(&sc->twa_dv, "firmware flash request failed. "
1975 "error = 0x%x\n", error);
1976 twa_hard_reset(sc);
1977 }
1978 }
1979
1980 if (tr->tr_data) {
1981 s = splvm();
1982 uvm_km_free(kmem_map, (vaddr_t)tr->tr_data,
1983 fw_img_chunk_size, UVM_KMF_WIRED);
1984 splx(s);
1985 }
1986 out:
1987 if (tr)
1988 twa_release_request(tr);
1989 return(error);
1990 }
1991
1992 /*
1993 * Function name: twa_hard_reset
1994 * Description: Hard reset the controller.
1995 *
1996 * Input: sc -- ptr to per ctlr structure
1997 * Output: None
1998 * Return value: 0 -- success
1999 * non-zero-- failure
2000 */
2001 static int
2002 twa_hard_reset(struct twa_softc *sc)
2003 {
2004 struct twa_request *tr;
2005 struct twa_command_reset_firmware *cmd;
2006 int error;
2007
2008 if ((tr = twa_get_request(sc, 0)) == NULL)
2009 return(EIO);
2010 tr->tr_cmd_pkt_type |= TWA_CMD_PKT_TYPE_INTERNAL;
2011 /* Build a cmd pkt for sending down the hard reset command. */
2012 tr->tr_command->cmd_hdr.header_desc.size_header = 128;
2013
2014 cmd = &(tr->tr_command->command.cmd_pkt_7k.reset_fw);
2015 cmd->opcode = TWA_OP_RESET_FIRMWARE;
2016 cmd->size = 2; /* this field will be updated at data map time */
2017 cmd->request_id = tr->tr_request_id;
2018 cmd->unit = 0;
2019 cmd->status = 0;
2020 cmd->flags = 0;
2021 cmd->param = 0; /* don't reload FPGA logic */
2022
2023 tr->tr_data = NULL;
2024 tr->tr_length = 0;
2025
2026 error = twa_immediate_request(tr, TWA_REQUEST_TIMEOUT_PERIOD);
2027 if (error) {
2028 printf("%s: hard reset request could not be posted. "
2029 "error = 0x%x\n", device_xname(&sc->twa_dv), error);
2030 if (error == ETIMEDOUT)
2031 /* clean-up done by twa_immediate_request */
2032 return(error);
2033 goto out;
2034 }
2035 if ((error = cmd->status)) {
2036 aprint_error_dev(&sc->twa_dv, "hard reset request failed. error = 0x%x\n",
2037 error);
2038 }
2039
2040 out:
2041 if (tr)
2042 twa_release_request(tr);
2043 return(error);
2044 }
2045 #endif
2046
2047 /*
2048 * Function name: twa_intr
2049 * Description: Interrupt handler. Determines the kind of interrupt,
2050 * and calls the appropriate handler.
2051 *
2052 * Input: sc -- ptr to per ctlr structure
2053 * Output: None
2054 * Return value: None
2055 */
2056
2057 static int
2058 twa_intr(void *arg)
2059 {
2060 int caught, s, rv;
2061 struct twa_softc *sc;
2062 uint32_t status_reg;
2063 sc = (struct twa_softc *)arg;
2064
2065 caught = 0;
2066 /* Collect current interrupt status. */
2067 status_reg = twa_inl(sc, TWA_STATUS_REGISTER_OFFSET);
2068 if (twa_check_ctlr_state(sc, status_reg)) {
2069 caught = 1;
2070 goto bail;
2071 }
2072 /* Dispatch based on the kind of interrupt. */
2073 if (status_reg & TWA_STATUS_HOST_INTERRUPT) {
2074 twa_outl(sc, TWA_CONTROL_REGISTER_OFFSET,
2075 TWA_CONTROL_CLEAR_HOST_INTERRUPT);
2076 caught = 1;
2077 }
2078 if ((status_reg & TWA_STATUS_ATTENTION_INTERRUPT) != 0) {
2079 twa_outl(sc, TWA_CONTROL_REGISTER_OFFSET,
2080 TWA_CONTROL_CLEAR_ATTENTION_INTERRUPT);
2081 rv = twa_fetch_aen(sc);
2082 #ifdef DIAGNOSTIC
2083 if (rv != 0)
2084 printf("%s: unable to retrieve AEN (%d)\n",
2085 device_xname(&sc->twa_dv), rv);
2086 #endif
2087 caught = 1;
2088 }
2089 if (status_reg & TWA_STATUS_COMMAND_INTERRUPT) {
2090 /* Start any requests that might be in the pending queue. */
2091 twa_outl(sc, TWA_CONTROL_REGISTER_OFFSET,
2092 TWA_CONTROL_MASK_COMMAND_INTERRUPT);
2093 (void)twa_drain_pending_queue(sc);
2094 caught = 1;
2095 }
2096 if (status_reg & TWA_STATUS_RESPONSE_INTERRUPT) {
2097 s = splbio();
2098 twa_done(sc);
2099 splx(s);
2100 caught = 1;
2101 }
2102 bail:
2103 return (caught);
2104 }
2105
2106 /*
2107 * Accept an open operation on the control device.
2108 */
2109 static int
2110 twaopen(dev_t dev, int flag, int mode, struct lwp *l)
2111 {
2112 struct twa_softc *twa;
2113
2114 if ((twa = device_lookup(&twa_cd, minor(dev))) == NULL)
2115 return (ENXIO);
2116
2117 twa->twa_sc_flags |= TWA_STATE_OPEN;
2118
2119 return (0);
2120 }
2121
2122 /*
2123 * Accept the last close on the control device.
2124 */
2125 static int
2126 twaclose(dev_t dev, int flag, int mode,
2127 struct lwp *l)
2128 {
2129 struct twa_softc *twa;
2130
2131 twa = device_lookup(&twa_cd, minor(dev));
2132 twa->twa_sc_flags &= ~TWA_STATE_OPEN;
2133 return (0);
2134 }
2135
2136 /*
2137 * Function name: twaioctl
2138 * Description: ioctl handler.
2139 *
2140 * Input: sc -- ptr to per ctlr structure
2141 * cmd -- ioctl cmd
2142 * buf -- ptr to buffer in kernel memory, which is
2143 * a copy of the input buffer in user-space
2144 * Output: buf -- ptr to buffer in kernel memory, which will
2145 * be copied of the output buffer in user-space
2146 * Return value: 0 -- success
2147 * non-zero-- failure
2148 */
2149 static int
2150 twaioctl(dev_t dev, u_long cmd, void *data, int flag,
2151 struct lwp *l)
2152 {
2153 struct twa_softc *sc;
2154 struct twa_ioctl_9k *user_buf = (struct twa_ioctl_9k *)data;
2155 struct tw_cl_event_packet event_buf;
2156 struct twa_request *tr = 0;
2157 int32_t event_index = 0;
2158 int32_t start_index;
2159 int s, error = 0;
2160
2161 sc = device_lookup(&twa_cd, minor(dev));
2162
2163 switch (cmd) {
2164 case TW_OSL_IOCTL_FIRMWARE_PASS_THROUGH:
2165 {
2166 struct twa_command_packet *cmdpkt;
2167 uint32_t data_buf_size_adjusted;
2168
2169 /* Get a request packet */
2170 tr = twa_get_request_wait(sc, 0);
2171 KASSERT(tr != NULL);
2172 /*
2173 * Make sure that the data buffer sent to firmware is a
2174 * 512 byte multiple in size.
2175 */
2176 data_buf_size_adjusted =
2177 (user_buf->twa_drvr_pkt.buffer_length + 511) & ~511;
2178
2179 if ((tr->tr_length = data_buf_size_adjusted)) {
2180 if ((tr->tr_data = malloc(data_buf_size_adjusted,
2181 M_DEVBUF, M_WAITOK)) == NULL) {
2182 error = ENOMEM;
2183 goto fw_passthru_done;
2184 }
2185 /* Copy the payload. */
2186 if ((error = copyin((void *) (user_buf->pdata),
2187 (void *) (tr->tr_data),
2188 user_buf->twa_drvr_pkt.buffer_length)) != 0) {
2189 goto fw_passthru_done;
2190 }
2191 tr->tr_flags |= TWA_CMD_DATA_IN | TWA_CMD_DATA_OUT;
2192 }
2193 tr->tr_cmd_pkt_type |= TWA_CMD_PKT_TYPE_IOCTL;
2194 cmdpkt = tr->tr_command;
2195
2196 /* Copy the command packet. */
2197 memcpy(cmdpkt, &(user_buf->twa_cmd_pkt),
2198 sizeof(struct twa_command_packet));
2199 cmdpkt->command.cmd_pkt_7k.generic.request_id =
2200 tr->tr_request_id;
2201
2202 /* Send down the request, and wait for it to complete. */
2203 if ((error = twa_wait_request(tr, TWA_REQUEST_TIMEOUT_PERIOD))) {
2204 if (error == ETIMEDOUT)
2205 break; /* clean-up done by twa_wait_request */
2206 goto fw_passthru_done;
2207 }
2208
2209 /* Copy the command packet back into user space. */
2210 memcpy(&user_buf->twa_cmd_pkt, cmdpkt,
2211 sizeof(struct twa_command_packet));
2212
2213 /* If there was a payload, copy it back too. */
2214 if (tr->tr_length)
2215 error = copyout(tr->tr_data, user_buf->pdata,
2216 user_buf->twa_drvr_pkt.buffer_length);
2217 fw_passthru_done:
2218 /* Free resources. */
2219 if (tr->tr_data)
2220 free(tr->tr_data, M_DEVBUF);
2221
2222 if (tr)
2223 twa_release_request(tr);
2224 break;
2225 }
2226
2227 case TW_OSL_IOCTL_SCAN_BUS:
2228 twa_request_bus_scan(sc);
2229 break;
2230
2231 case TW_CL_IOCTL_GET_FIRST_EVENT:
2232 if (sc->twa_aen_queue_wrapped) {
2233 if (sc->twa_aen_queue_overflow) {
2234 /*
2235 * The aen queue has wrapped, even before some
2236 * events have been retrieved. Let the caller
2237 * know that he missed out on some AEN's.
2238 */
2239 user_buf->twa_drvr_pkt.status =
2240 TWA_ERROR_AEN_OVERFLOW;
2241 sc->twa_aen_queue_overflow = FALSE;
2242 } else
2243 user_buf->twa_drvr_pkt.status = 0;
2244 event_index = sc->twa_aen_head;
2245 } else {
2246 if (sc->twa_aen_head == sc->twa_aen_tail) {
2247 user_buf->twa_drvr_pkt.status =
2248 TWA_ERROR_AEN_NO_EVENTS;
2249 break;
2250 }
2251 user_buf->twa_drvr_pkt.status = 0;
2252 event_index = sc->twa_aen_tail; /* = 0 */
2253 }
2254 if ((error = copyout(sc->twa_aen_queue[event_index],
2255 user_buf->pdata, sizeof(struct tw_cl_event_packet))) != 0)
2256 (sc->twa_aen_queue[event_index])->retrieved =
2257 TWA_AEN_RETRIEVED;
2258 break;
2259
2260 case TW_CL_IOCTL_GET_LAST_EVENT:
2261 if (sc->twa_aen_queue_wrapped) {
2262 if (sc->twa_aen_queue_overflow) {
2263 /*
2264 * The aen queue has wrapped, even before some
2265 * events have been retrieved. Let the caller
2266 * know that he missed out on some AEN's.
2267 */
2268 user_buf->twa_drvr_pkt.status =
2269 TWA_ERROR_AEN_OVERFLOW;
2270 sc->twa_aen_queue_overflow = FALSE;
2271 } else
2272 user_buf->twa_drvr_pkt.status = 0;
2273 } else {
2274 if (sc->twa_aen_head == sc->twa_aen_tail) {
2275 user_buf->twa_drvr_pkt.status =
2276 TWA_ERROR_AEN_NO_EVENTS;
2277 break;
2278 }
2279 user_buf->twa_drvr_pkt.status = 0;
2280 }
2281 event_index =
2282 (sc->twa_aen_head - 1 + TWA_Q_LENGTH) % TWA_Q_LENGTH;
2283 if ((error = copyout(sc->twa_aen_queue[event_index],
2284 user_buf->pdata, sizeof(struct tw_cl_event_packet))) != 0)
2285 (sc->twa_aen_queue[event_index])->retrieved =
2286 TWA_AEN_RETRIEVED;
2287 break;
2288
2289 case TW_CL_IOCTL_GET_NEXT_EVENT:
2290 user_buf->twa_drvr_pkt.status = 0;
2291 if (sc->twa_aen_queue_wrapped) {
2292
2293 if (sc->twa_aen_queue_overflow) {
2294 /*
2295 * The aen queue has wrapped, even before some
2296 * events have been retrieved. Let the caller
2297 * know that he missed out on some AEN's.
2298 */
2299 user_buf->twa_drvr_pkt.status =
2300 TWA_ERROR_AEN_OVERFLOW;
2301 sc->twa_aen_queue_overflow = FALSE;
2302 }
2303 start_index = sc->twa_aen_head;
2304 } else {
2305 if (sc->twa_aen_head == sc->twa_aen_tail) {
2306 user_buf->twa_drvr_pkt.status =
2307 TWA_ERROR_AEN_NO_EVENTS;
2308 break;
2309 }
2310 start_index = sc->twa_aen_tail; /* = 0 */
2311 }
2312 error = copyin(user_buf->pdata, &event_buf,
2313 sizeof(struct tw_cl_event_packet));
2314
2315 event_index = (start_index + event_buf.sequence_id -
2316 (sc->twa_aen_queue[start_index])->sequence_id + 1)
2317 % TWA_Q_LENGTH;
2318
2319 if (!((sc->twa_aen_queue[event_index])->sequence_id >
2320 event_buf.sequence_id)) {
2321 if (user_buf->twa_drvr_pkt.status ==
2322 TWA_ERROR_AEN_OVERFLOW)
2323 /* so we report the overflow next time */
2324 sc->twa_aen_queue_overflow = TRUE;
2325 user_buf->twa_drvr_pkt.status = TWA_ERROR_AEN_NO_EVENTS;
2326 break;
2327 }
2328 if ((error = copyout(sc->twa_aen_queue[event_index],
2329 user_buf->pdata, sizeof(struct tw_cl_event_packet))) != 0)
2330 (sc->twa_aen_queue[event_index])->retrieved =
2331 TWA_AEN_RETRIEVED;
2332 break;
2333
2334 case TW_CL_IOCTL_GET_PREVIOUS_EVENT:
2335 user_buf->twa_drvr_pkt.status = 0;
2336 if (sc->twa_aen_queue_wrapped) {
2337 if (sc->twa_aen_queue_overflow) {
2338 /*
2339 * The aen queue has wrapped, even before some
2340 * events have been retrieved. Let the caller
2341 * know that he missed out on some AEN's.
2342 */
2343 user_buf->twa_drvr_pkt.status =
2344 TWA_ERROR_AEN_OVERFLOW;
2345 sc->twa_aen_queue_overflow = FALSE;
2346 }
2347 start_index = sc->twa_aen_head;
2348 } else {
2349 if (sc->twa_aen_head == sc->twa_aen_tail) {
2350 user_buf->twa_drvr_pkt.status =
2351 TWA_ERROR_AEN_NO_EVENTS;
2352 break;
2353 }
2354 start_index = sc->twa_aen_tail; /* = 0 */
2355 }
2356 if ((error = copyin(user_buf->pdata, &event_buf,
2357 sizeof(struct tw_cl_event_packet))) != 0)
2358
2359 event_index = (start_index + event_buf.sequence_id -
2360 (sc->twa_aen_queue[start_index])->sequence_id - 1)
2361 % TWA_Q_LENGTH;
2362 if (!((sc->twa_aen_queue[event_index])->sequence_id <
2363 event_buf.sequence_id)) {
2364 if (user_buf->twa_drvr_pkt.status ==
2365 TWA_ERROR_AEN_OVERFLOW)
2366 /* so we report the overflow next time */
2367 sc->twa_aen_queue_overflow = TRUE;
2368 user_buf->twa_drvr_pkt.status =
2369 TWA_ERROR_AEN_NO_EVENTS;
2370 break;
2371 }
2372 if ((error = copyout(sc->twa_aen_queue [event_index],
2373 user_buf->pdata, sizeof(struct tw_cl_event_packet))) != 0)
2374 aprint_error_dev(&sc->twa_dv, "get_previous: Could not copyout to "
2375 "event_buf. error = %x\n",
2376 error);
2377 (sc->twa_aen_queue[event_index])->retrieved = TWA_AEN_RETRIEVED;
2378 break;
2379
2380 case TW_CL_IOCTL_GET_LOCK:
2381 {
2382 struct tw_cl_lock_packet twa_lock;
2383
2384 copyin(user_buf->pdata, &twa_lock,
2385 sizeof(struct tw_cl_lock_packet));
2386 s = splbio();
2387 if ((sc->twa_ioctl_lock.lock == TWA_LOCK_FREE) ||
2388 (twa_lock.force_flag) ||
2389 (time_second >= sc->twa_ioctl_lock.timeout)) {
2390
2391 sc->twa_ioctl_lock.lock = TWA_LOCK_HELD;
2392 sc->twa_ioctl_lock.timeout = time_second +
2393 (twa_lock.timeout_msec / 1000);
2394 twa_lock.time_remaining_msec = twa_lock.timeout_msec;
2395 user_buf->twa_drvr_pkt.status = 0;
2396 } else {
2397 twa_lock.time_remaining_msec =
2398 (sc->twa_ioctl_lock.timeout - time_second) *
2399 1000;
2400 user_buf->twa_drvr_pkt.status =
2401 TWA_ERROR_IOCTL_LOCK_ALREADY_HELD;
2402 }
2403 splx(s);
2404 copyout(&twa_lock, user_buf->pdata,
2405 sizeof(struct tw_cl_lock_packet));
2406 break;
2407 }
2408
2409 case TW_CL_IOCTL_RELEASE_LOCK:
2410 s = splbio();
2411 if (sc->twa_ioctl_lock.lock == TWA_LOCK_FREE) {
2412 user_buf->twa_drvr_pkt.status =
2413 TWA_ERROR_IOCTL_LOCK_NOT_HELD;
2414 } else {
2415 sc->twa_ioctl_lock.lock = TWA_LOCK_FREE;
2416 user_buf->twa_drvr_pkt.status = 0;
2417 }
2418 splx(s);
2419 break;
2420
2421 case TW_CL_IOCTL_GET_COMPATIBILITY_INFO:
2422 {
2423 struct tw_cl_compatibility_packet comp_pkt;
2424
2425 memcpy(comp_pkt.driver_version, TWA_DRIVER_VERSION_STRING,
2426 sizeof(TWA_DRIVER_VERSION_STRING));
2427 comp_pkt.working_srl = sc->working_srl;
2428 comp_pkt.working_branch = sc->working_branch;
2429 comp_pkt.working_build = sc->working_build;
2430 user_buf->twa_drvr_pkt.status = 0;
2431
2432 /* Copy compatibility information to user space. */
2433 copyout(&comp_pkt, user_buf->pdata,
2434 min(sizeof(struct tw_cl_compatibility_packet),
2435 user_buf->twa_drvr_pkt.buffer_length));
2436 break;
2437 }
2438
2439 case TWA_IOCTL_GET_UNITNAME: /* WASABI EXTENSION */
2440 {
2441 struct twa_unitname *tn;
2442 struct twa_drive *tdr;
2443
2444 tn = (struct twa_unitname *)data;
2445 /* XXX mutex */
2446 if (tn->tn_unit < 0 || tn->tn_unit >= TWA_MAX_UNITS)
2447 return (EINVAL);
2448 tdr = &sc->sc_units[tn->tn_unit];
2449 if (tdr->td_dev == NULL)
2450 tn->tn_name[0] = '\0';
2451 else
2452 strlcpy(tn->tn_name, device_xname(tdr->td_dev),
2453 sizeof(tn->tn_name));
2454 return (0);
2455 }
2456
2457 default:
2458 /* Unknown opcode. */
2459 error = ENOTTY;
2460 }
2461
2462 return(error);
2463 }
2464
2465 const struct cdevsw twa_cdevsw = {
2466 twaopen, twaclose, noread, nowrite, twaioctl,
2467 nostop, notty, nopoll, nommap, nokqfilter, D_OTHER,
2468 };
2469
2470 /*
2471 * Function name: twa_get_param
2472 * Description: Get a firmware parameter.
2473 *
2474 * Input: sc -- ptr to per ctlr structure
2475 * table_id -- parameter table #
2476 * param_id -- index of the parameter in the table
2477 * param_size -- size of the parameter in bytes
2478 * callback -- ptr to function, if any, to be called
2479 * back on completion; NULL if no callback.
2480 * Output: None
2481 * Return value: ptr to param structure -- success
2482 * NULL -- failure
2483 */
2484 static int
2485 twa_get_param(struct twa_softc *sc, int table_id, int param_id,
2486 size_t param_size, void (* callback)(struct twa_request *tr),
2487 struct twa_param_9k **param)
2488 {
2489 int rv = 0;
2490 struct twa_request *tr;
2491 union twa_command_7k *cmd;
2492
2493 /* Get a request packet. */
2494 if ((tr = twa_get_request(sc, 0)) == NULL) {
2495 rv = EAGAIN;
2496 goto out;
2497 }
2498
2499 tr->tr_cmd_pkt_type |= TWA_CMD_PKT_TYPE_INTERNAL;
2500
2501 /* Allocate memory to read data into. */
2502 if ((*param = (struct twa_param_9k *)
2503 malloc(TWA_SECTOR_SIZE, M_DEVBUF, M_NOWAIT)) == NULL) {
2504 rv = ENOMEM;
2505 goto out;
2506 }
2507
2508 memset(*param, 0, sizeof(struct twa_param_9k) - 1 + param_size);
2509 tr->tr_data = *param;
2510 tr->tr_length = TWA_SECTOR_SIZE;
2511 tr->tr_flags = TWA_CMD_DATA_IN | TWA_CMD_DATA_OUT;
2512
2513 /* Build the cmd pkt. */
2514 cmd = &(tr->tr_command->command.cmd_pkt_7k);
2515
2516 tr->tr_command->cmd_hdr.header_desc.size_header = 128;
2517
2518 cmd->param.opcode = TWA_OP_GET_PARAM;
2519 cmd->param.sgl_offset = 2;
2520 cmd->param.size = 2;
2521 cmd->param.request_id = tr->tr_request_id;
2522 cmd->param.unit = 0;
2523 cmd->param.param_count = 1;
2524
2525 /* Specify which parameter we need. */
2526 (*param)->table_id = table_id | TWA_9K_PARAM_DESCRIPTOR;
2527 (*param)->parameter_id = param_id;
2528 (*param)->parameter_size_bytes = param_size;
2529
2530 /* Submit the command. */
2531 if (callback == NULL) {
2532 /* There's no call back; wait till the command completes. */
2533 rv = twa_immediate_request(tr, TWA_REQUEST_TIMEOUT_PERIOD);
2534
2535 if (rv != 0)
2536 goto out;
2537
2538 if ((rv = cmd->param.status) != 0) {
2539 /* twa_drain_complete_queue will have done the unmapping */
2540 goto out;
2541 }
2542 twa_release_request(tr);
2543 return (rv);
2544 } else {
2545 /* There's a call back. Simply submit the command. */
2546 tr->tr_callback = callback;
2547 rv = twa_map_request(tr);
2548 return (rv);
2549 }
2550 out:
2551 if (tr)
2552 twa_release_request(tr);
2553 return(rv);
2554 }
2555
2556 /*
2557 * Function name: twa_set_param
2558 * Description: Set a firmware parameter.
2559 *
2560 * Input: sc -- ptr to per ctlr structure
2561 * table_id -- parameter table #
2562 * param_id -- index of the parameter in the table
2563 * param_size -- size of the parameter in bytes
2564 * callback -- ptr to function, if any, to be called
2565 * back on completion; NULL if no callback.
2566 * Output: None
2567 * Return value: 0 -- success
2568 * non-zero-- failure
2569 */
2570 static int
2571 twa_set_param(struct twa_softc *sc, int table_id, int param_id, int param_size,
2572 void *data, void (* callback)(struct twa_request *tr))
2573 {
2574 struct twa_request *tr;
2575 union twa_command_7k *cmd;
2576 struct twa_param_9k *param = NULL;
2577 int error = ENOMEM;
2578
2579 tr = twa_get_request(sc, 0);
2580 if (tr == NULL)
2581 return (EAGAIN);
2582
2583 tr->tr_cmd_pkt_type |= TWA_CMD_PKT_TYPE_INTERNAL;
2584
2585 /* Allocate memory to send data using. */
2586 if ((param = (struct twa_param_9k *)
2587 malloc(TWA_SECTOR_SIZE, M_DEVBUF, M_NOWAIT)) == NULL)
2588 goto out;
2589 memset(param, 0, sizeof(struct twa_param_9k) - 1 + param_size);
2590 tr->tr_data = param;
2591 tr->tr_length = TWA_SECTOR_SIZE;
2592 tr->tr_flags = TWA_CMD_DATA_IN | TWA_CMD_DATA_OUT;
2593
2594 /* Build the cmd pkt. */
2595 cmd = &(tr->tr_command->command.cmd_pkt_7k);
2596
2597 tr->tr_command->cmd_hdr.header_desc.size_header = 128;
2598
2599 cmd->param.opcode = TWA_OP_SET_PARAM;
2600 cmd->param.sgl_offset = 2;
2601 cmd->param.size = 2;
2602 cmd->param.request_id = tr->tr_request_id;
2603 cmd->param.unit = 0;
2604 cmd->param.param_count = 1;
2605
2606 /* Specify which parameter we want to set. */
2607 param->table_id = table_id | TWA_9K_PARAM_DESCRIPTOR;
2608 param->parameter_id = param_id;
2609 param->parameter_size_bytes = param_size;
2610 memcpy(param->data, data, param_size);
2611
2612 /* Submit the command. */
2613 if (callback == NULL) {
2614 /* There's no call back; wait till the command completes. */
2615 error = twa_immediate_request(tr, TWA_REQUEST_TIMEOUT_PERIOD);
2616 if (error == ETIMEDOUT)
2617 /* clean-up done by twa_immediate_request */
2618 return(error);
2619 if (error)
2620 goto out;
2621 if ((error = cmd->param.status)) {
2622 /*
2623 * twa_drain_complete_queue will have done the
2624 * unmapping.
2625 */
2626 goto out;
2627 }
2628 free(param, M_DEVBUF);
2629 twa_release_request(tr);
2630 return(error);
2631 } else {
2632 /* There's a call back. Simply submit the command. */
2633 tr->tr_callback = callback;
2634 if ((error = twa_map_request(tr)))
2635 goto out;
2636
2637 return (0);
2638 }
2639 out:
2640 if (param)
2641 free(param, M_DEVBUF);
2642 if (tr)
2643 twa_release_request(tr);
2644 return(error);
2645 }
2646
2647 /*
2648 * Function name: twa_init_connection
2649 * Description: Send init_connection cmd to firmware
2650 *
2651 * Input: sc -- ptr to per ctlr structure
2652 * message_credits -- max # of requests that we might send
2653 * down simultaneously. This will be
2654 * typically set to 256 at init-time or
2655 * after a reset, and to 1 at shutdown-time
2656 * set_features -- indicates if we intend to use 64-bit
2657 * sg, also indicates if we want to do a
2658 * basic or an extended init_connection;
2659 *
2660 * Note: The following input/output parameters are valid, only in case of an
2661 * extended init_connection:
2662 *
2663 * current_fw_srl -- srl of fw we are bundled
2664 * with, if any; 0 otherwise
2665 * current_fw_arch_id -- arch_id of fw we are bundled
2666 * with, if any; 0 otherwise
2667 * current_fw_branch -- branch # of fw we are bundled
2668 * with, if any; 0 otherwise
2669 * current_fw_build -- build # of fw we are bundled
2670 * with, if any; 0 otherwise
2671 * Output: fw_on_ctlr_srl -- srl of fw on ctlr
2672 * fw_on_ctlr_arch_id -- arch_id of fw on ctlr
2673 * fw_on_ctlr_branch -- branch # of fw on ctlr
2674 * fw_on_ctlr_build -- build # of fw on ctlr
2675 * init_connect_result -- result bitmap of fw response
2676 * Return value: 0 -- success
2677 * non-zero-- failure
2678 */
2679 static int
2680 twa_init_connection(struct twa_softc *sc, uint16_t message_credits,
2681 uint32_t set_features, uint16_t current_fw_srl,
2682 uint16_t current_fw_arch_id, uint16_t current_fw_branch,
2683 uint16_t current_fw_build, uint16_t *fw_on_ctlr_srl,
2684 uint16_t *fw_on_ctlr_arch_id, uint16_t *fw_on_ctlr_branch,
2685 uint16_t *fw_on_ctlr_build, uint32_t *init_connect_result)
2686 {
2687 struct twa_request *tr;
2688 struct twa_command_init_connect *init_connect;
2689 int error = 1;
2690
2691 /* Get a request packet. */
2692 if ((tr = twa_get_request(sc, 0)) == NULL)
2693 goto out;
2694 tr->tr_cmd_pkt_type |= TWA_CMD_PKT_TYPE_INTERNAL;
2695 /* Build the cmd pkt. */
2696 init_connect = &(tr->tr_command->command.cmd_pkt_7k.init_connect);
2697
2698 tr->tr_command->cmd_hdr.header_desc.size_header = 128;
2699
2700 init_connect->opcode = TWA_OP_INIT_CONNECTION;
2701 init_connect->request_id = tr->tr_request_id;
2702 init_connect->message_credits = message_credits;
2703 init_connect->features = set_features;
2704 if (TWA_64BIT_ADDRESSES) {
2705 printf("64 bit addressing supported for scatter/gather list\n");
2706 init_connect->features |= TWA_64BIT_SG_ADDRESSES;
2707 }
2708 if (set_features & TWA_EXTENDED_INIT_CONNECT) {
2709 /*
2710 * Fill in the extra fields needed for
2711 * an extended init_connect.
2712 */
2713 init_connect->size = 6;
2714 init_connect->fw_srl = current_fw_srl;
2715 init_connect->fw_arch_id = current_fw_arch_id;
2716 init_connect->fw_branch = current_fw_branch;
2717 } else
2718 init_connect->size = 3;
2719
2720 /* Submit the command, and wait for it to complete. */
2721 error = twa_immediate_request(tr, TWA_REQUEST_TIMEOUT_PERIOD);
2722 if (error == ETIMEDOUT)
2723 return(error); /* clean-up done by twa_immediate_request */
2724 if (error)
2725 goto out;
2726 if ((error = init_connect->status)) {
2727 /* twa_drain_complete_queue will have done the unmapping */
2728 goto out;
2729 }
2730 if (set_features & TWA_EXTENDED_INIT_CONNECT) {
2731 *fw_on_ctlr_srl = init_connect->fw_srl;
2732 *fw_on_ctlr_arch_id = init_connect->fw_arch_id;
2733 *fw_on_ctlr_branch = init_connect->fw_branch;
2734 *fw_on_ctlr_build = init_connect->fw_build;
2735 *init_connect_result = init_connect->result;
2736 }
2737 twa_release_request(tr);
2738 return(error);
2739
2740 out:
2741 if (tr)
2742 twa_release_request(tr);
2743 return(error);
2744 }
2745
2746 static int
2747 twa_reset(struct twa_softc *sc)
2748 {
2749 int s;
2750 int error = 0;
2751
2752 /*
2753 * Disable interrupts from the controller, and mask any
2754 * accidental entry into our interrupt handler.
2755 */
2756 twa_outl(sc, TWA_CONTROL_REGISTER_OFFSET,
2757 TWA_CONTROL_DISABLE_INTERRUPTS);
2758
2759 s = splbio();
2760
2761 /* Soft reset the controller. */
2762 if ((error = twa_soft_reset(sc)))
2763 goto out;
2764
2765 /* Re-establish logical connection with the controller. */
2766 if ((error = twa_init_connection(sc, TWA_INIT_MESSAGE_CREDITS,
2767 0, 0, 0, 0, 0,
2768 NULL, NULL, NULL, NULL, NULL))) {
2769 goto out;
2770 }
2771 /*
2772 * Complete all requests in the complete queue; error back all requests
2773 * in the busy queue. Any internal requests will be simply freed.
2774 * Re-submit any requests in the pending queue.
2775 */
2776 twa_drain_busy_queue(sc);
2777
2778 out:
2779 splx(s);
2780 /*
2781 * Enable interrupts, and also clear attention and response interrupts.
2782 */
2783 twa_outl(sc, TWA_CONTROL_REGISTER_OFFSET,
2784 TWA_CONTROL_CLEAR_ATTENTION_INTERRUPT |
2785 TWA_CONTROL_UNMASK_RESPONSE_INTERRUPT |
2786 TWA_CONTROL_ENABLE_INTERRUPTS);
2787 return(error);
2788 }
2789
2790 static int
2791 twa_soft_reset(struct twa_softc *sc)
2792 {
2793 uint32_t status_reg;
2794
2795 twa_outl(sc, TWA_CONTROL_REGISTER_OFFSET,
2796 TWA_CONTROL_ISSUE_SOFT_RESET |
2797 TWA_CONTROL_CLEAR_HOST_INTERRUPT |
2798 TWA_CONTROL_CLEAR_ATTENTION_INTERRUPT |
2799 TWA_CONTROL_MASK_COMMAND_INTERRUPT |
2800 TWA_CONTROL_MASK_RESPONSE_INTERRUPT |
2801 TWA_CONTROL_DISABLE_INTERRUPTS);
2802
2803 if (twa_wait_status(sc, TWA_STATUS_MICROCONTROLLER_READY |
2804 TWA_STATUS_ATTENTION_INTERRUPT, 30)) {
2805 aprint_error_dev(&sc->twa_dv, "no attention interrupt after reset.\n");
2806 return(1);
2807 }
2808 twa_outl(sc, TWA_CONTROL_REGISTER_OFFSET,
2809 TWA_CONTROL_CLEAR_ATTENTION_INTERRUPT);
2810
2811 if (twa_drain_response_queue(sc)) {
2812 aprint_error_dev(&sc->twa_dv, "cannot drain response queue.\n");
2813 return(1);
2814 }
2815 if (twa_drain_aen_queue(sc)) {
2816 aprint_error_dev(&sc->twa_dv, "cannot drain AEN queue.\n");
2817 return(1);
2818 }
2819 if (twa_find_aen(sc, TWA_AEN_SOFT_RESET)) {
2820 aprint_error_dev(&sc->twa_dv, "reset not reported by controller.\n");
2821 return(1);
2822 }
2823 status_reg = twa_inl(sc, TWA_STATUS_REGISTER_OFFSET);
2824 if (TWA_STATUS_ERRORS(status_reg) ||
2825 twa_check_ctlr_state(sc, status_reg)) {
2826 aprint_error_dev(&sc->twa_dv, "controller errors detected.\n");
2827 return(1);
2828 }
2829 return(0);
2830 }
2831
2832 static int
2833 twa_wait_status(struct twa_softc *sc, uint32_t status, uint32_t timeout)
2834 {
2835 struct timeval t1;
2836 time_t end_time;
2837 uint32_t status_reg;
2838
2839 timeout = (timeout * 1000 * 100);
2840
2841 microtime(&t1);
2842
2843 end_time = t1.tv_usec + timeout;
2844
2845 do {
2846 status_reg = twa_inl(sc, TWA_STATUS_REGISTER_OFFSET);
2847 /* got the required bit(s)? */
2848 if ((status_reg & status) == status)
2849 return(0);
2850 DELAY(100000);
2851 microtime(&t1);
2852 } while (t1.tv_usec <= end_time);
2853
2854 return(1);
2855 }
2856
2857 static int
2858 twa_fetch_aen(struct twa_softc *sc)
2859 {
2860 struct twa_request *tr;
2861 int s, error = 0;
2862
2863 s = splbio();
2864
2865 if ((tr = twa_get_request(sc, TWA_CMD_AEN)) == NULL) {
2866 splx(s);
2867 return(EIO);
2868 }
2869 tr->tr_cmd_pkt_type |= TWA_CMD_PKT_TYPE_INTERNAL;
2870 tr->tr_callback = twa_aen_callback;
2871 tr->tr_data = malloc(TWA_SECTOR_SIZE, M_DEVBUF, M_NOWAIT);
2872 if (twa_request_sense(tr, 0) != 0) {
2873 if (tr->tr_data)
2874 free(tr->tr_data, M_DEVBUF);
2875 twa_release_request(tr);
2876 error = 1;
2877 }
2878 splx(s);
2879
2880 return(error);
2881 }
2882
2883 /*
2884 * Function name: twa_aen_callback
2885 * Description: Callback for requests to fetch AEN's.
2886 *
2887 * Input: tr -- ptr to completed request pkt
2888 * Output: None
2889 * Return value: None
2890 */
2891 static void
2892 twa_aen_callback(struct twa_request *tr)
2893 {
2894 int i;
2895 int fetch_more_aens = 0;
2896 struct twa_softc *sc = tr->tr_sc;
2897 struct twa_command_header *cmd_hdr =
2898 (struct twa_command_header *)(tr->tr_data);
2899 struct twa_command_9k *cmd =
2900 &(tr->tr_command->command.cmd_pkt_9k);
2901
2902 if (! cmd->status) {
2903 if ((tr->tr_cmd_pkt_type & TWA_CMD_PKT_TYPE_9K) &&
2904 (cmd->cdb[0] == 0x3 /* REQUEST_SENSE */))
2905 if (twa_enqueue_aen(sc, cmd_hdr)
2906 != TWA_AEN_QUEUE_EMPTY)
2907 fetch_more_aens = 1;
2908 } else {
2909 cmd_hdr->err_specific_desc[sizeof(cmd_hdr->err_specific_desc) - 1] = '\0';
2910 for (i = 0; i < 18; i++)
2911 printf("%x\t", tr->tr_command->cmd_hdr.sense_data[i]);
2912
2913 printf(""); /* print new line */
2914
2915 for (i = 0; i < 128; i++)
2916 printf("%x\t", ((int8_t *)(tr->tr_data))[i]);
2917 }
2918 if (tr->tr_data)
2919 free(tr->tr_data, M_DEVBUF);
2920 twa_release_request(tr);
2921
2922 if (fetch_more_aens)
2923 twa_fetch_aen(sc);
2924 }
2925
2926 /*
2927 * Function name: twa_enqueue_aen
2928 * Description: Queues AEN's to be supplied to user-space tools on request.
2929 *
2930 * Input: sc -- ptr to per ctlr structure
2931 * cmd_hdr -- ptr to hdr of fw cmd pkt, from where the AEN
2932 * details can be retrieved.
2933 * Output: None
2934 * Return value: None
2935 */
2936 static uint16_t
2937 twa_enqueue_aen(struct twa_softc *sc, struct twa_command_header *cmd_hdr)
2938 {
2939 int rv, s;
2940 struct tw_cl_event_packet *event;
2941 uint16_t aen_code;
2942 unsigned long sync_time;
2943
2944 s = splbio();
2945 aen_code = cmd_hdr->status_block.error;
2946
2947 switch (aen_code) {
2948 case TWA_AEN_SYNC_TIME_WITH_HOST:
2949
2950 sync_time = (time_second - (3 * 86400)) % 604800;
2951 rv = twa_set_param(sc, TWA_PARAM_TIME_TABLE,
2952 TWA_PARAM_TIME_SchedulerTime, 4,
2953 &sync_time, twa_aen_callback);
2954 #ifdef DIAGNOSTIC
2955 if (rv != 0)
2956 aprint_error_dev(&sc->twa_dv, "unable to sync time with ctlr\n");
2957 #endif
2958 break;
2959
2960 case TWA_AEN_QUEUE_EMPTY:
2961 break;
2962
2963 default:
2964 /* Queue the event. */
2965 event = sc->twa_aen_queue[sc->twa_aen_head];
2966 if (event->retrieved == TWA_AEN_NOT_RETRIEVED)
2967 sc->twa_aen_queue_overflow = TRUE;
2968 event->severity =
2969 cmd_hdr->status_block.substatus_block.severity;
2970 event->time_stamp_sec = time_second;
2971 event->aen_code = aen_code;
2972 event->retrieved = TWA_AEN_NOT_RETRIEVED;
2973 event->sequence_id = ++(sc->twa_current_sequence_id);
2974 cmd_hdr->err_specific_desc[sizeof(cmd_hdr->err_specific_desc) - 1] = '\0';
2975 event->parameter_len = strlen(cmd_hdr->err_specific_desc);
2976 memcpy(event->parameter_data, cmd_hdr->err_specific_desc,
2977 event->parameter_len);
2978
2979 if (event->severity < TWA_AEN_SEVERITY_DEBUG) {
2980 printf("%s: AEN 0x%04X: %s: %s: %s\n",
2981 device_xname(&sc->twa_dv),
2982 aen_code,
2983 twa_aen_severity_table[event->severity],
2984 twa_find_msg_string(twa_aen_table, aen_code),
2985 event->parameter_data);
2986 }
2987
2988 if ((sc->twa_aen_head + 1) == TWA_Q_LENGTH)
2989 sc->twa_aen_queue_wrapped = TRUE;
2990 sc->twa_aen_head = (sc->twa_aen_head + 1) % TWA_Q_LENGTH;
2991 break;
2992 } /* switch */
2993 splx(s);
2994
2995 return (aen_code);
2996 }
2997
2998 /*
2999 * Function name: twa_find_aen
3000 * Description: Reports whether a given AEN ever occurred.
3001 *
3002 * Input: sc -- ptr to per ctlr structure
3003 * aen_code-- AEN to look for
3004 * Output: None
3005 * Return value: 0 -- success
3006 * non-zero-- failure
3007 */
3008 static int
3009 twa_find_aen(struct twa_softc *sc, uint16_t aen_code)
3010 {
3011 uint32_t last_index;
3012 int s;
3013 int i;
3014
3015 s = splbio();
3016
3017 if (sc->twa_aen_queue_wrapped)
3018 last_index = sc->twa_aen_head;
3019 else
3020 last_index = 0;
3021
3022 i = sc->twa_aen_head;
3023 do {
3024 i = (i + TWA_Q_LENGTH - 1) % TWA_Q_LENGTH;
3025 if ((sc->twa_aen_queue[i])->aen_code == aen_code) {
3026 splx(s);
3027 return(0);
3028 }
3029 } while (i != last_index);
3030
3031 splx(s);
3032 return(1);
3033 }
3034
3035 static inline void
3036 twa_request_init(struct twa_request *tr, int flags)
3037 {
3038 tr->tr_data = NULL;
3039 tr->tr_real_data = NULL;
3040 tr->tr_length = 0;
3041 tr->tr_real_length = 0;
3042 tr->tr_status = TWA_CMD_SETUP;/* command is in setup phase */
3043 tr->tr_flags = flags;
3044 tr->tr_error = 0;
3045 tr->tr_callback = NULL;
3046 tr->tr_cmd_pkt_type = 0;
3047 tr->bp = 0;
3048
3049 /*
3050 * Look at the status field in the command packet to see how
3051 * it completed the last time it was used, and zero out only
3052 * the portions that might have changed. Note that we don't
3053 * care to zero out the sglist.
3054 */
3055 if (tr->tr_command->command.cmd_pkt_9k.status)
3056 memset(tr->tr_command, 0,
3057 sizeof(struct twa_command_header) + 28);
3058 else
3059 memset(&(tr->tr_command->command), 0, 28);
3060 }
3061
3062 struct twa_request *
3063 twa_get_request_wait(struct twa_softc *sc, int flags)
3064 {
3065 struct twa_request *tr;
3066 int s;
3067
3068 KASSERT((flags & TWA_CMD_AEN) == 0);
3069
3070 s = splbio();
3071 while ((tr = TAILQ_FIRST(&sc->twa_free)) == NULL) {
3072 sc->twa_sc_flags |= TWA_STATE_REQUEST_WAIT;
3073 (void) tsleep(&sc->twa_free, PRIBIO, "twaccb", hz);
3074 }
3075 TAILQ_REMOVE(&sc->twa_free, tr, tr_link);
3076
3077 splx(s);
3078
3079 twa_request_init(tr, flags);
3080
3081 return(tr);
3082 }
3083
3084 struct twa_request *
3085 twa_get_request(struct twa_softc *sc, int flags)
3086 {
3087 int s;
3088 struct twa_request *tr;
3089
3090 /* Get a free request packet. */
3091 s = splbio();
3092 if (__predict_false((flags & TWA_CMD_AEN) != 0)) {
3093
3094 if ((sc->sc_twa_request->tr_flags & TWA_CMD_AEN_BUSY) == 0) {
3095 tr = sc->sc_twa_request;
3096 flags |= TWA_CMD_AEN_BUSY;
3097 } else {
3098 splx(s);
3099 return (NULL);
3100 }
3101 } else {
3102 if (__predict_false((tr =
3103 TAILQ_FIRST(&sc->twa_free)) == NULL)) {
3104 splx(s);
3105 return (NULL);
3106 }
3107 TAILQ_REMOVE(&sc->twa_free, tr, tr_link);
3108 }
3109 splx(s);
3110
3111 twa_request_init(tr, flags);
3112
3113 return(tr);
3114 }
3115
3116 /*
3117 * Print some information about the controller
3118 */
3119 static void
3120 twa_describe_controller(struct twa_softc *sc)
3121 {
3122 struct twa_param_9k *p[10];
3123 int i, rv = 0;
3124 uint32_t dsize;
3125 uint8_t ports;
3126
3127 memset(p, sizeof(struct twa_param_9k *), 10);
3128
3129 /* Get the port count. */
3130 rv |= twa_get_param(sc, TWA_PARAM_CONTROLLER,
3131 TWA_PARAM_CONTROLLER_PortCount, 1, NULL, &p[0]);
3132
3133 /* get version strings */
3134 rv |= twa_get_param(sc, TWA_PARAM_VERSION, TWA_PARAM_VERSION_FW,
3135 16, NULL, &p[1]);
3136 rv |= twa_get_param(sc, TWA_PARAM_VERSION, TWA_PARAM_VERSION_BIOS,
3137 16, NULL, &p[2]);
3138 rv |= twa_get_param(sc, TWA_PARAM_VERSION, TWA_PARAM_VERSION_Mon,
3139 16, NULL, &p[3]);
3140 rv |= twa_get_param(sc, TWA_PARAM_VERSION, TWA_PARAM_VERSION_PCBA,
3141 8, NULL, &p[4]);
3142 rv |= twa_get_param(sc, TWA_PARAM_VERSION, TWA_PARAM_VERSION_ATA,
3143 8, NULL, &p[5]);
3144 rv |= twa_get_param(sc, TWA_PARAM_VERSION, TWA_PARAM_VERSION_PCI,
3145 8, NULL, &p[6]);
3146 rv |= twa_get_param(sc, TWA_PARAM_DRIVESUMMARY, TWA_PARAM_DRIVESTATUS,
3147 16, NULL, &p[7]);
3148
3149 if (rv) {
3150 /* some error occurred */
3151 aprint_error_dev(&sc->twa_dv, "failed to fetch version information\n");
3152 goto bail;
3153 }
3154
3155 ports = *(uint8_t *)(p[0]->data);
3156
3157 aprint_normal_dev(&sc->twa_dv, "%d ports, Firmware %.16s, BIOS %.16s\n",
3158 ports, p[1]->data, p[2]->data);
3159
3160 aprint_verbose_dev(&sc->twa_dv, "Monitor %.16s, PCB %.8s, Achip %.8s, Pchip %.8s\n",
3161 p[3]->data, p[4]->data,
3162 p[5]->data, p[6]->data);
3163
3164 for (i = 0; i < ports; i++) {
3165
3166 if ((*((char *)(p[7]->data + i)) & TWA_DRIVE_DETECTED) == 0)
3167 continue;
3168
3169 rv = twa_get_param(sc, TWA_PARAM_DRIVE_TABLE + i,
3170 TWA_PARAM_DRIVEMODELINDEX,
3171 TWA_PARAM_DRIVEMODEL_LENGTH, NULL, &p[8]);
3172
3173 if (rv != 0) {
3174 aprint_error_dev(&sc->twa_dv, "unable to get drive model for port"
3175 " %d\n", i);
3176 continue;
3177 }
3178
3179 rv = twa_get_param(sc, TWA_PARAM_DRIVE_TABLE + i,
3180 TWA_PARAM_DRIVESIZEINDEX,
3181 TWA_PARAM_DRIVESIZE_LENGTH, NULL, &p[9]);
3182
3183 if (rv != 0) {
3184 aprint_error_dev(&sc->twa_dv, "unable to get drive size"
3185 " for port %d\n", i);
3186 free(p[8], M_DEVBUF);
3187 continue;
3188 }
3189
3190 dsize = *(uint32_t *)(p[9]->data);
3191
3192 aprint_verbose_dev(&sc->twa_dv, "port %d: %.40s %d MB\n",
3193 i, p[8]->data, dsize / 2048);
3194
3195 if (p[8])
3196 free(p[8], M_DEVBUF);
3197 if (p[9])
3198 free(p[9], M_DEVBUF);
3199 }
3200 bail:
3201 if (p[0])
3202 free(p[0], M_DEVBUF);
3203 if (p[1])
3204 free(p[1], M_DEVBUF);
3205 if (p[2])
3206 free(p[2], M_DEVBUF);
3207 if (p[3])
3208 free(p[3], M_DEVBUF);
3209 if (p[4])
3210 free(p[4], M_DEVBUF);
3211 if (p[5])
3212 free(p[5], M_DEVBUF);
3213 if (p[6])
3214 free(p[6], M_DEVBUF);
3215 }
3216
3217 /*
3218 * Function name: twa_check_ctlr_state
3219 * Description: Makes sure that the fw status register reports a
3220 * proper status.
3221 *
3222 * Input: sc -- ptr to per ctlr structure
3223 * status_reg -- value in the status register
3224 * Output: None
3225 * Return value: 0 -- no errors
3226 * non-zero-- errors
3227 */
3228 static int
3229 twa_check_ctlr_state(struct twa_softc *sc, uint32_t status_reg)
3230 {
3231 int result = 0;
3232 struct timeval t1;
3233 static time_t last_warning[2] = {0, 0};
3234
3235 /* Check if the 'micro-controller ready' bit is not set. */
3236 if ((status_reg & TWA_STATUS_EXPECTED_BITS) !=
3237 TWA_STATUS_EXPECTED_BITS) {
3238
3239 microtime(&t1);
3240
3241 last_warning[0] += (5 * 1000 * 100);
3242
3243 if (t1.tv_usec > last_warning[0]) {
3244 microtime(&t1);
3245 last_warning[0] = t1.tv_usec;
3246 }
3247 result = 1;
3248 }
3249
3250 /* Check if any error bits are set. */
3251 if ((status_reg & TWA_STATUS_UNEXPECTED_BITS) != 0) {
3252
3253 microtime(&t1);
3254 last_warning[1] += (5 * 1000 * 100);
3255 if (t1.tv_usec > last_warning[1]) {
3256 microtime(&t1);
3257 last_warning[1] = t1.tv_usec;
3258 }
3259 if (status_reg & TWA_STATUS_PCI_PARITY_ERROR_INTERRUPT) {
3260 aprint_error_dev(&sc->twa_dv, "clearing PCI parity error "
3261 "re-seat/move/replace card.\n");
3262 twa_outl(sc, TWA_CONTROL_REGISTER_OFFSET,
3263 TWA_CONTROL_CLEAR_PARITY_ERROR);
3264 pci_conf_write(sc->pc, sc->tag,
3265 PCI_COMMAND_STATUS_REG,
3266 TWA_PCI_CONFIG_CLEAR_PARITY_ERROR);
3267 result = 1;
3268 }
3269 if (status_reg & TWA_STATUS_PCI_ABORT_INTERRUPT) {
3270 aprint_error_dev(&sc->twa_dv, "clearing PCI abort\n");
3271 twa_outl(sc, TWA_CONTROL_REGISTER_OFFSET,
3272 TWA_CONTROL_CLEAR_PCI_ABORT);
3273 pci_conf_write(sc->pc, sc->tag,
3274 PCI_COMMAND_STATUS_REG,
3275 TWA_PCI_CONFIG_CLEAR_PCI_ABORT);
3276 result = 1;
3277 }
3278 if (status_reg & TWA_STATUS_QUEUE_ERROR_INTERRUPT) {
3279 aprint_error_dev(&sc->twa_dv, "clearing controller queue error\n");
3280 twa_outl(sc, TWA_CONTROL_REGISTER_OFFSET,
3281 TWA_CONTROL_CLEAR_PCI_ABORT);
3282 result = 1;
3283 }
3284 if (status_reg & TWA_STATUS_SBUF_WRITE_ERROR) {
3285 aprint_error_dev(&sc->twa_dv, "clearing SBUF write error\n");
3286 twa_outl(sc, TWA_CONTROL_REGISTER_OFFSET,
3287 TWA_CONTROL_CLEAR_SBUF_WRITE_ERROR);
3288 result = 1;
3289 }
3290 if (status_reg & TWA_STATUS_MICROCONTROLLER_ERROR) {
3291 aprint_error_dev(&sc->twa_dv, "micro-controller error\n");
3292 result = 1;
3293 }
3294 }
3295 return(result);
3296 }
3297