twa.c revision 1.16.12.1 1 /* $NetBSD: twa.c,v 1.16.12.1 2009/08/25 18:21:31 snj 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.16.12.1 2009/08/25 18:21:31 snj 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 <machine/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", sc->twa_dv.dv_xname, 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("%s: unable to allocate "
843 "command packets, rv = %d\n",
844 sc->twa_dv.dv_xname, rv);
845 return (ENOMEM);
846 }
847
848 if ((rv = bus_dmamem_map(sc->twa_dma_tag,
849 &seg, rseg, size, (caddr_t *)&sc->twa_cmds,
850 BUS_DMA_NOWAIT | BUS_DMA_COHERENT)) != 0) {
851 aprint_error("%s: unable to map commands, rv = %d\n",
852 sc->twa_dv.dv_xname, rv);
853 return (1);
854 }
855
856 if ((rv = bus_dmamap_create(sc->twa_dma_tag,
857 size, num_reqs, size,
858 0, BUS_DMA_NOWAIT, &sc->twa_cmd_map)) != 0) {
859 aprint_error("%s: unable to create command DMA map, "
860 "rv = %d\n", sc->twa_dv.dv_xname, rv);
861 return (ENOMEM);
862 }
863
864 if ((rv = bus_dmamap_load(sc->twa_dma_tag, sc->twa_cmd_map,
865 sc->twa_cmds, size, NULL,
866 BUS_DMA_NOWAIT)) != 0) {
867 aprint_error("%s: unable to load command DMA map, "
868 "rv = %d\n", sc->twa_dv.dv_xname, rv);
869 return (1);
870 }
871
872 if ((uintptr_t)sc->twa_cmds % TWA_ALIGNMENT) {
873 aprint_error("%s: DMA map memory not aligned on %d boundary\n",
874 sc->twa_dv.dv_xname, TWA_ALIGNMENT);
875
876 return (1);
877 }
878 tc = sc->twa_cmd_pkt_buf = (struct twa_command_packet *)sc->twa_cmds;
879 sc->twa_cmd_pkt_phys = sc->twa_cmd_map->dm_segs[0].ds_addr;
880
881 memset(sc->twa_req_buf, 0, num_reqs * sizeof(struct twa_request));
882 memset(sc->twa_cmd_pkt_buf, 0,
883 num_reqs * sizeof(struct twa_command_packet));
884
885 sc->sc_twa_request = sc->twa_req_buf;
886 max_segs = twa_get_maxsegs();
887 max_xfer = twa_get_maxxfer(max_segs);
888
889 for (i = 0; i < num_reqs; i++, tc++) {
890 tr = &(sc->twa_req_buf[i]);
891 tr->tr_command = tc;
892 tr->tr_cmd_phys = sc->twa_cmd_pkt_phys +
893 (i * sizeof(struct twa_command_packet));
894 tr->tr_request_id = i;
895 tr->tr_sc = sc;
896
897 /*
898 * Create a map for data buffers. maxsize (256 * 1024) used in
899 * bus_dma_tag_create above should suffice the bounce page needs
900 * for data buffers, since the max I/O size we support is 128KB.
901 * If we supported I/O's bigger than 256KB, we would have to
902 * create a second dma_tag, with the appropriate maxsize.
903 */
904 if ((rv = bus_dmamap_create(sc->twa_dma_tag,
905 max_xfer, max_segs, 1, 0, BUS_DMA_NOWAIT,
906 &tr->tr_dma_map)) != 0) {
907 aprint_error("%s: unable to create command "
908 "DMA map, rv = %d\n",
909 sc->twa_dv.dv_xname, rv);
910 return (ENOMEM);
911 }
912 /* Insert request into the free queue. */
913 if (i != 0) {
914 sc->twa_lookup[i] = tr;
915 twa_release_request(tr);
916 } else
917 tr->tr_flags |= TWA_CMD_AEN;
918 }
919 return(0);
920 }
921
922 static void
923 twa_recompute_openings(struct twa_softc *sc)
924 {
925 struct twa_drive *td;
926 int unit;
927 int openings;
928
929 if (sc->sc_nunits != 0)
930 openings = ((TWA_Q_LENGTH / 2) / sc->sc_nunits);
931 else
932 openings = 0;
933 if (openings == sc->sc_openings)
934 return;
935 sc->sc_openings = openings;
936
937 #ifdef TWA_DEBUG
938 printf("%s: %d array%s, %d openings per array\n",
939 sc->twa_dv.dv_xname, sc->sc_nunits,
940 sc->sc_nunits == 1 ? "" : "s", sc->sc_openings);
941 #endif
942 for (unit = 0; unit < TWA_MAX_UNITS; unit++) {
943 td = &sc->sc_units[unit];
944 if (td->td_dev != NULL)
945 (*td->td_callbacks->tcb_openings)(td->td_dev,
946 sc->sc_openings);
947 }
948 }
949
950 static int
951 twa_request_bus_scan(struct twa_softc *sc)
952 {
953 struct twa_drive *td;
954 struct twa_request *tr;
955 struct twa_attach_args twaa;
956 int locs[TWACF_NLOCS];
957 int s, unit;
958
959 s = splbio();
960 for (unit = 0; unit < TWA_MAX_UNITS; unit++) {
961
962 if ((tr = twa_get_request(sc, 0)) == NULL) {
963 splx(s);
964 return (EIO);
965 }
966
967 tr->tr_cmd_pkt_type |= TWA_CMD_PKT_TYPE_INTERNAL;
968
969 tr->tr_data = malloc(TWA_SECTOR_SIZE, M_DEVBUF, M_NOWAIT);
970
971 if (tr->tr_data == NULL) {
972 twa_release_request(tr);
973 splx(s);
974 return (ENOMEM);
975 }
976 td = &sc->sc_units[unit];
977
978 if (twa_inquiry(tr, unit) == 0) {
979 if (td->td_dev == NULL) {
980 twa_print_inquiry_data(sc,
981 ((struct scsipi_inquiry_data *)tr->tr_data));
982
983 sc->sc_nunits++;
984
985 sc->sc_units[unit].td_size =
986 twa_read_capacity(tr, unit);
987
988 twaa.twaa_unit = unit;
989
990 twa_recompute_openings(sc);
991
992 locs[TWACF_UNIT] = unit;
993
994 sc->sc_units[unit].td_dev =
995 config_found_sm_loc(&sc->twa_dv, "twa",
996 locs, &twaa, twa_print, config_stdsubmatch);
997 }
998 } else {
999 if (td->td_dev != NULL) {
1000 sc->sc_nunits--;
1001
1002 (void) config_detach(td->td_dev, DETACH_FORCE);
1003 td->td_dev = NULL;
1004 td->td_size = 0;
1005
1006 twa_recompute_openings(sc);
1007 }
1008 }
1009 free(tr->tr_data, M_DEVBUF);
1010
1011 twa_release_request(tr);
1012 }
1013 splx(s);
1014
1015 return (0);
1016 }
1017
1018
1019 #ifdef DIAGNOSTIC
1020 static inline void
1021 twa_check_busy_q(struct twa_request *tr)
1022 {
1023 struct twa_request *rq;
1024 struct twa_softc *sc = tr->tr_sc;
1025
1026 TAILQ_FOREACH(rq, &sc->twa_busy, tr_link) {
1027 if (tr->tr_request_id == rq->tr_request_id) {
1028 panic("cannot submit same request more than once");
1029 } else if (tr->bp == rq->bp && tr->bp != 0) {
1030 /* XXX A check for 0 for the buf ptr is needed to
1031 * guard against ioctl requests with a buf ptr of
1032 * 0 and also aen notifications. Looking for
1033 * external cmds only.
1034 */
1035 panic("cannot submit same buf more than once");
1036 } else {
1037 /* Empty else statement */
1038 }
1039 }
1040 }
1041 #endif
1042
1043 static int
1044 twa_start(struct twa_request *tr)
1045 {
1046 struct twa_softc *sc = tr->tr_sc;
1047 uint32_t status_reg;
1048 int s;
1049 int error;
1050
1051 s = splbio();
1052 /* Check to see if we can post a command. */
1053 status_reg = twa_inl(sc, TWA_STATUS_REGISTER_OFFSET);
1054 if ((error = twa_check_ctlr_state(sc, status_reg)))
1055 goto out;
1056
1057 if (status_reg & TWA_STATUS_COMMAND_QUEUE_FULL) {
1058 if (tr->tr_status != TWA_CMD_PENDING) {
1059 tr->tr_status = TWA_CMD_PENDING;
1060 TAILQ_INSERT_TAIL(&tr->tr_sc->twa_pending,
1061 tr, tr_link);
1062 }
1063 twa_outl(sc, TWA_CONTROL_REGISTER_OFFSET,
1064 TWA_CONTROL_UNMASK_COMMAND_INTERRUPT);
1065 error = EBUSY;
1066 } else {
1067 bus_dmamap_sync(sc->twa_dma_tag, sc->twa_cmd_map,
1068 (caddr_t)tr->tr_command - sc->twa_cmds,
1069 sizeof(struct twa_command_packet),
1070 BUS_DMASYNC_PREWRITE | BUS_DMASYNC_PREREAD);
1071
1072 /* Cmd queue is not full. Post the command. */
1073 TWA_WRITE_COMMAND_QUEUE(sc, tr->tr_cmd_phys +
1074 sizeof(struct twa_command_header));
1075
1076 /* Mark the request as currently being processed. */
1077 tr->tr_status = TWA_CMD_BUSY;
1078
1079 #ifdef DIAGNOSTIC
1080 twa_check_busy_q(tr);
1081 #endif
1082
1083 /* Move the request into the busy queue. */
1084 TAILQ_INSERT_TAIL(&tr->tr_sc->twa_busy, tr, tr_link);
1085 }
1086 out:
1087 splx(s);
1088 return(error);
1089 }
1090
1091 static int
1092 twa_drain_response_queue(struct twa_softc *sc)
1093 {
1094 union twa_response_queue rq;
1095 uint32_t status_reg;
1096
1097 for (;;) {
1098 status_reg = twa_inl(sc, TWA_STATUS_REGISTER_OFFSET);
1099 if (twa_check_ctlr_state(sc, status_reg))
1100 return(1);
1101 if (status_reg & TWA_STATUS_RESPONSE_QUEUE_EMPTY)
1102 return(0); /* no more response queue entries */
1103 rq = (union twa_response_queue)twa_inl(sc,
1104 TWA_RESPONSE_QUEUE_OFFSET);
1105 }
1106 }
1107
1108 static void
1109 twa_drain_busy_queue(struct twa_softc *sc)
1110 {
1111 struct twa_request *tr;
1112
1113 /* Walk the busy queue. */
1114
1115 while ((tr = TAILQ_FIRST(&sc->twa_busy)) != NULL) {
1116 TAILQ_REMOVE(&sc->twa_busy, tr, tr_link);
1117
1118 twa_unmap_request(tr);
1119 if ((tr->tr_cmd_pkt_type & TWA_CMD_PKT_TYPE_INTERNAL) ||
1120 (tr->tr_cmd_pkt_type & TWA_CMD_PKT_TYPE_IOCTL)) {
1121 /* It's an internal/ioctl request. Simply free it. */
1122 if (tr->tr_data)
1123 free(tr->tr_data, M_DEVBUF);
1124 twa_release_request(tr);
1125 } else {
1126 /* It's a SCSI request. Complete it. */
1127 tr->tr_command->command.cmd_pkt_9k.status = EIO;
1128 if (tr->tr_callback)
1129 tr->tr_callback(tr);
1130 }
1131 }
1132 }
1133
1134 static int
1135 twa_drain_pending_queue(struct twa_softc *sc)
1136 {
1137 struct twa_request *tr;
1138 int s, error = 0;
1139
1140 /*
1141 * Pull requests off the pending queue, and submit them.
1142 */
1143 s = splbio();
1144 while ((tr = TAILQ_FIRST(&sc->twa_pending)) != NULL) {
1145 TAILQ_REMOVE(&sc->twa_pending, tr, tr_link);
1146
1147 if ((error = twa_start(tr))) {
1148 if (error == EBUSY) {
1149 tr->tr_status = TWA_CMD_PENDING;
1150
1151 /* queue at the head */
1152 TAILQ_INSERT_HEAD(&tr->tr_sc->twa_pending,
1153 tr, tr_link);
1154 error = 0;
1155 break;
1156 } else {
1157 if (tr->tr_flags & TWA_CMD_SLEEP_ON_REQUEST) {
1158 tr->tr_error = error;
1159 tr->tr_callback(tr);
1160 error = EIO;
1161 }
1162 }
1163 }
1164 }
1165 splx(s);
1166
1167 return(error);
1168 }
1169
1170 static int
1171 twa_drain_aen_queue(struct twa_softc *sc)
1172 {
1173 int s, error = 0;
1174 struct twa_request *tr;
1175 struct twa_command_header *cmd_hdr;
1176 struct timeval t1;
1177 uint32_t timeout;
1178
1179 for (;;) {
1180 if ((tr = twa_get_request(sc, 0)) == NULL) {
1181 error = EIO;
1182 break;
1183 }
1184 tr->tr_cmd_pkt_type |= TWA_CMD_PKT_TYPE_INTERNAL;
1185 tr->tr_callback = NULL;
1186
1187 tr->tr_data = malloc(TWA_SECTOR_SIZE, M_DEVBUF, M_NOWAIT);
1188
1189 if (tr->tr_data == NULL) {
1190 error = 1;
1191 goto out;
1192 }
1193
1194 if (twa_request_sense(tr, 0) != 0) {
1195 error = 1;
1196 break;
1197 }
1198
1199 timeout = (1000/*ms*/ * 100/*us*/ * TWA_REQUEST_TIMEOUT_PERIOD);
1200
1201 microtime(&t1);
1202
1203 timeout += t1.tv_usec;
1204
1205 do {
1206 s = splbio();
1207 twa_done(tr->tr_sc);
1208 splx(s);
1209 if (tr->tr_status != TWA_CMD_BUSY)
1210 break;
1211 microtime(&t1);
1212 } while (t1.tv_usec <= timeout);
1213
1214 if (tr->tr_status != TWA_CMD_COMPLETE) {
1215 error = ETIMEDOUT;
1216 break;
1217 }
1218
1219 if ((error = tr->tr_command->command.cmd_pkt_9k.status))
1220 break;
1221
1222 cmd_hdr = (struct twa_command_header *)(tr->tr_data);
1223 if ((cmd_hdr->status_block.error) /* aen_code */
1224 == TWA_AEN_QUEUE_EMPTY)
1225 break;
1226 (void)twa_enqueue_aen(sc, cmd_hdr);
1227
1228 free(tr->tr_data, M_DEVBUF);
1229 twa_release_request(tr);
1230 }
1231 out:
1232 if (tr) {
1233 if (tr->tr_data)
1234 free(tr->tr_data, M_DEVBUF);
1235
1236 twa_release_request(tr);
1237 }
1238 return(error);
1239 }
1240
1241
1242 #if 0
1243 static void
1244 twa_check_response_q(struct twa_request *tr, int clear)
1245 {
1246 int j;
1247 static int i = 0;
1248 static struct twa_request *req = 0;
1249 static struct buf *hist[255];
1250
1251
1252 if (clear) {
1253 i = 0;
1254 for (j = 0; j < 255; j++)
1255 hist[j] = 0;
1256 return;
1257 }
1258
1259 if (req == 0)
1260 req = tr;
1261
1262 if ((tr->tr_cmd_pkt_type & TWA_CMD_PKT_TYPE_EXTERNAL) != 0) {
1263 XXX this is bogus ! req can't be anything else but tr !
1264 if (req->tr_request_id == tr->tr_request_id)
1265 panic("req id: %d on controller queue twice",
1266 tr->tr_request_id);
1267
1268 for (j = 0; j < i; j++)
1269 if (tr->bp == hist[j])
1270 panic("req id: %d buf found twice",
1271 tr->tr_request_id);
1272 }
1273 req = tr;
1274
1275 hist[i++] = req->bp;
1276 }
1277 #endif
1278
1279 static int
1280 twa_done(struct twa_softc *sc)
1281 {
1282 union twa_response_queue rq;
1283 struct twa_request *tr;
1284 int rv = 0;
1285 uint32_t status_reg;
1286
1287 for (;;) {
1288 status_reg = twa_inl(sc, TWA_STATUS_REGISTER_OFFSET);
1289 if ((rv = twa_check_ctlr_state(sc, status_reg)))
1290 break;
1291 if (status_reg & TWA_STATUS_RESPONSE_QUEUE_EMPTY)
1292 break;
1293 /* Response queue is not empty. */
1294 rq = (union twa_response_queue)twa_inl(sc,
1295 TWA_RESPONSE_QUEUE_OFFSET);
1296 tr = sc->sc_twa_request + rq.u.response_id;
1297 #if 0
1298 twa_check_response_q(tr, 0);
1299 #endif
1300 /* Unmap the command packet, and any associated data buffer. */
1301 twa_unmap_request(tr);
1302
1303 tr->tr_status = TWA_CMD_COMPLETE;
1304 TAILQ_REMOVE(&tr->tr_sc->twa_busy, tr, tr_link);
1305
1306 if (tr->tr_callback)
1307 tr->tr_callback(tr);
1308 }
1309 (void)twa_drain_pending_queue(sc);
1310
1311 #if 0
1312 twa_check_response_q(NULL, 1);
1313 #endif
1314 return(rv);
1315 }
1316
1317 /*
1318 * Function name: twa_init_ctlr
1319 * Description: Establishes a logical connection with the controller.
1320 * If bundled with firmware, determines whether or not
1321 * to flash firmware, based on arch_id, fw SRL (Spec.
1322 * Revision Level), branch & build #'s. Also determines
1323 * whether or not the driver is compatible with the
1324 * firmware on the controller, before proceeding to work
1325 * with it.
1326 *
1327 * Input: sc -- ptr to per ctlr structure
1328 * Output: None
1329 * Return value: 0 -- success
1330 * non-zero-- failure
1331 */
1332 static int
1333 twa_init_ctlr(struct twa_softc *sc)
1334 {
1335 uint16_t fw_on_ctlr_srl = 0;
1336 uint16_t fw_on_ctlr_arch_id = 0;
1337 uint16_t fw_on_ctlr_branch = 0;
1338 uint16_t fw_on_ctlr_build = 0;
1339 uint32_t init_connect_result = 0;
1340 int error = 0;
1341 #if 0
1342 int8_t fw_flashed = FALSE;
1343 int8_t fw_flash_failed = FALSE;
1344 #endif
1345
1346 /* Wait for the controller to become ready. */
1347 if (twa_wait_status(sc, TWA_STATUS_MICROCONTROLLER_READY,
1348 TWA_REQUEST_TIMEOUT_PERIOD)) {
1349 return(ENXIO);
1350 }
1351 /* Drain the response queue. */
1352 if (twa_drain_response_queue(sc))
1353 return(1);
1354
1355 /* Establish a logical connection with the controller. */
1356 if ((error = twa_init_connection(sc, TWA_INIT_MESSAGE_CREDITS,
1357 TWA_EXTENDED_INIT_CONNECT, TWA_CURRENT_FW_SRL,
1358 TWA_9000_ARCH_ID, TWA_CURRENT_FW_BRANCH,
1359 TWA_CURRENT_FW_BUILD, &fw_on_ctlr_srl,
1360 &fw_on_ctlr_arch_id, &fw_on_ctlr_branch,
1361 &fw_on_ctlr_build, &init_connect_result))) {
1362 return(error);
1363 }
1364 #if 0
1365 if ((init_connect_result & TWA_BUNDLED_FW_SAFE_TO_FLASH) &&
1366 (init_connect_result & TWA_CTLR_FW_RECOMMENDS_FLASH)) {
1367 /*
1368 * The bundled firmware is safe to flash, and the firmware
1369 * on the controller recommends a flash. So, flash!
1370 */
1371 printf("%s: flashing bundled firmware...\n",
1372 sc->twa_dv.dv_xname);
1373
1374 if ((error = twa_flash_firmware(sc))) {
1375 fw_flash_failed = TRUE;
1376
1377 printf("%s: unable to flash bundled firmware.\n",
1378 sc->twa_dv.dv_xname);
1379 } else {
1380 printf("%s: successfully flashed bundled firmware.\n",
1381 sc->twa_dv.dv_xname);
1382 fw_flashed = TRUE;
1383 }
1384 }
1385 if (fw_flashed) {
1386 /* The firmware was flashed. Have the new image loaded */
1387 error = twa_hard_reset(sc);
1388 if (error == 0)
1389 error = twa_init_ctlr(sc);
1390 /*
1391 * If hard reset of controller failed, we need to return.
1392 * Otherwise, the above recursive call to twa_init_ctlr will
1393 * have completed the rest of the initialization (starting
1394 * from twa_drain_aen_queue below). Don't do it again.
1395 * Just return.
1396 */
1397 return(error);
1398 } else {
1399 /*
1400 * Either we are not bundled with a firmware image, or
1401 * the bundled firmware is not safe to flash,
1402 * or flash failed for some reason. See if we can at
1403 * least work with the firmware on the controller in the
1404 * current mode.
1405 */
1406 if (init_connect_result & TWA_CTLR_FW_COMPATIBLE) {
1407 /* Yes, we can. Make note of the operating mode. */
1408 sc->working_srl = TWA_CURRENT_FW_SRL;
1409 sc->working_branch = TWA_CURRENT_FW_BRANCH;
1410 sc->working_build = TWA_CURRENT_FW_BUILD;
1411 } else {
1412 /*
1413 * No, we can't. See if we can at least work with
1414 * it in the base mode. We should never come here
1415 * if firmware has just been flashed.
1416 */
1417 printf("%s: Driver/Firmware mismatch. Negotiating "
1418 "for base level.\n", sc->twa_dv.dv_xname);
1419 if ((error = twa_init_connection(sc,
1420 TWA_INIT_MESSAGE_CREDITS,
1421 TWA_EXTENDED_INIT_CONNECT, TWA_BASE_FW_SRL,
1422 TWA_9000_ARCH_ID, TWA_BASE_FW_BRANCH,
1423 TWA_BASE_FW_BUILD, &fw_on_ctlr_srl,
1424 &fw_on_ctlr_arch_id, &fw_on_ctlr_branch,
1425 &fw_on_ctlr_build, &init_connect_result))) {
1426 printf("%s: can't initialize connection in "
1427 "base mode.\n", sc->twa_dv.dv_xname);
1428 return(error);
1429 }
1430 if (!(init_connect_result & TWA_CTLR_FW_COMPATIBLE)) {
1431 /*
1432 * The firmware on the controller is not even
1433 * compatible with our base mode. We cannot
1434 * work with it. Bail...
1435 */
1436 printf("Incompatible firmware on controller\n");
1437 #ifdef TWA_FLASH_FIRMWARE
1438 if (fw_flash_failed)
1439 printf("...and could not flash bundled "
1440 "firmware.\n");
1441 else
1442 printf("...and bundled firmware not "
1443 "safe to flash.\n");
1444 #endif /* TWA_FLASH_FIRMWARE */
1445 return(1);
1446 }
1447 /*
1448 * We can work with this firmware, but only in
1449 * base mode.
1450 */
1451 sc->working_srl = TWA_BASE_FW_SRL;
1452 sc->working_branch = TWA_BASE_FW_BRANCH;
1453 sc->working_build = TWA_BASE_FW_BUILD;
1454 sc->twa_operating_mode = TWA_BASE_MODE;
1455 }
1456 }
1457 #endif
1458 twa_drain_aen_queue(sc);
1459
1460 /* Set controller state to initialized. */
1461 sc->twa_state &= ~TWA_STATE_SHUTDOWN;
1462 return(0);
1463 }
1464
1465 static int
1466 twa_setup(struct twa_softc *sc)
1467 {
1468 struct tw_cl_event_packet *aen_queue;
1469 uint32_t i = 0;
1470 int error = 0;
1471
1472 /* Initialize request queues. */
1473 TAILQ_INIT(&sc->twa_free);
1474 TAILQ_INIT(&sc->twa_busy);
1475 TAILQ_INIT(&sc->twa_pending);
1476
1477 sc->sc_nunits = 0;
1478 sc->twa_sc_flags = 0;
1479
1480 if (twa_alloc_req_pkts(sc, TWA_Q_LENGTH)) {
1481
1482 return(ENOMEM);
1483 }
1484
1485 /* Allocate memory for the AEN queue. */
1486 if ((aen_queue = malloc(sizeof(struct tw_cl_event_packet) *
1487 TWA_Q_LENGTH, M_DEVBUF, M_WAITOK)) == NULL) {
1488 /*
1489 * This should not cause us to return error. We will only be
1490 * unable to support AEN's. But then, we will have to check
1491 * time and again to see if we can support AEN's, if we
1492 * continue. So, we will just return error.
1493 */
1494 return (ENOMEM);
1495 }
1496 /* Initialize the aen queue. */
1497 memset(aen_queue, 0, sizeof(struct tw_cl_event_packet) * TWA_Q_LENGTH);
1498
1499 for (i = 0; i < TWA_Q_LENGTH; i++)
1500 sc->twa_aen_queue[i] = &(aen_queue[i]);
1501
1502 twa_outl(sc, TWA_CONTROL_REGISTER_OFFSET,
1503 TWA_CONTROL_DISABLE_INTERRUPTS);
1504
1505 /* Initialize the controller. */
1506 if ((error = twa_init_ctlr(sc))) {
1507 /* Soft reset the controller, and try one more time. */
1508
1509 printf("%s: controller initialization failed. "
1510 "Retrying initialization\n", sc->twa_dv.dv_xname);
1511
1512 if ((error = twa_soft_reset(sc)) == 0)
1513 error = twa_init_ctlr(sc);
1514 }
1515
1516 twa_describe_controller(sc);
1517
1518 error = twa_request_bus_scan(sc);
1519
1520 twa_outl(sc, TWA_CONTROL_REGISTER_OFFSET,
1521 TWA_CONTROL_CLEAR_ATTENTION_INTERRUPT |
1522 TWA_CONTROL_UNMASK_RESPONSE_INTERRUPT |
1523 TWA_CONTROL_ENABLE_INTERRUPTS);
1524
1525 return (error);
1526 }
1527
1528 void *twa_sdh;
1529
1530 static void
1531 twa_attach(struct device *parent, struct device *self, void *aux)
1532 {
1533 struct pci_attach_args *pa;
1534 struct twa_softc *sc;
1535 pci_chipset_tag_t pc;
1536 pcireg_t csr;
1537 pci_intr_handle_t ih;
1538 const char *intrstr;
1539 struct ctlname ctlnames[] = CTL_NAMES;
1540 const struct sysctlnode *node;
1541 int i;
1542
1543 sc = (struct twa_softc *)self;
1544
1545 pa = aux;
1546 pc = pa->pa_pc;
1547 sc->pc = pa->pa_pc;
1548 sc->tag = pa->pa_tag;
1549 sc->twa_dma_tag = pa->pa_dmat;
1550
1551 aprint_naive(": RAID controller\n");
1552 aprint_normal(": 3ware Apache\n");
1553
1554 if (PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_3WARE_9000) {
1555 if (pci_mapreg_map(pa, PCI_MAPREG_START, PCI_MAPREG_TYPE_IO, 0,
1556 &sc->twa_bus_iot, &sc->twa_bus_ioh, NULL, NULL)) {
1557 aprint_error("%s: can't map i/o space\n",
1558 sc->twa_dv.dv_xname);
1559 return;
1560 }
1561 } else if (PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_3WARE_9550) {
1562 if (pci_mapreg_map(pa, PCI_MAPREG_START + 0x08,
1563 PCI_MAPREG_MEM_TYPE_64BIT, 0, &sc->twa_bus_iot,
1564 &sc->twa_bus_ioh, NULL, NULL)) {
1565 aprint_error("%s: can't map mem space\n",
1566 sc->twa_dv.dv_xname);
1567 return;
1568 }
1569 } else {
1570 aprint_error("%s: product id 0x%02x not recognized\n",
1571 sc->twa_dv.dv_xname, PCI_PRODUCT(pa->pa_id));
1572 return;
1573 }
1574 /* Enable the device. */
1575 csr = pci_conf_read(pa->pa_pc, pa->pa_tag, PCI_COMMAND_STATUS_REG);
1576
1577 pci_conf_write(pa->pa_pc, pa->pa_tag, PCI_COMMAND_STATUS_REG,
1578 csr | PCI_COMMAND_MASTER_ENABLE);
1579
1580 /* Map and establish the interrupt. */
1581 if (pci_intr_map(pa, &ih)) {
1582 aprint_error("%s: can't map interrupt\n", sc->twa_dv.dv_xname);
1583 return;
1584 }
1585 intrstr = pci_intr_string(pc, ih);
1586
1587 sc->twa_ih = pci_intr_establish(pc, ih, IPL_BIO, twa_intr, sc);
1588 if (sc->twa_ih == NULL) {
1589 aprint_error("%s: can't establish interrupt%s%s\n",
1590 sc->twa_dv.dv_xname,
1591 (intrstr) ? " at " : "",
1592 (intrstr) ? intrstr : "");
1593 return;
1594 }
1595
1596 if (intrstr != NULL)
1597 aprint_normal("%s: interrupting at %s\n",
1598 sc->twa_dv.dv_xname, intrstr);
1599
1600 twa_setup(sc);
1601
1602 if (twa_sdh == NULL)
1603 twa_sdh = shutdownhook_establish(twa_shutdown, NULL);
1604
1605 /* sysctl set-up for 3ware cli */
1606 if (sysctl_createv(NULL, 0, NULL, NULL,
1607 CTLFLAG_PERMANENT, CTLTYPE_NODE, "hw",
1608 NULL, NULL, 0, NULL, 0,
1609 CTL_HW, CTL_EOL) != 0) {
1610 printf("%s: could not create %s sysctl node\n",
1611 sc->twa_dv.dv_xname, ctlnames[CTL_HW].ctl_name);
1612 return;
1613 }
1614 if (sysctl_createv(NULL, 0, NULL, &node,
1615 0, CTLTYPE_NODE, sc->twa_dv.dv_xname,
1616 SYSCTL_DESCR("twa driver information"),
1617 NULL, 0, NULL, 0,
1618 CTL_HW, CTL_CREATE, CTL_EOL) != 0) {
1619 printf("%s: could not create %s.%s sysctl node\n",
1620 sc->twa_dv.dv_xname, ctlnames[CTL_HW].ctl_name,
1621 sc->twa_dv.dv_xname);
1622 return;
1623 }
1624 if ((i = sysctl_createv(NULL, 0, NULL, NULL,
1625 0, CTLTYPE_STRING, "driver_version",
1626 SYSCTL_DESCR("twa driver version"),
1627 NULL, 0, &twaver, 0,
1628 CTL_HW, node->sysctl_num, CTL_CREATE, CTL_EOL))
1629 != 0) {
1630 printf("%s: could not create %s.%s.driver_version sysctl\n",
1631 sc->twa_dv.dv_xname, ctlnames[CTL_HW].ctl_name,
1632 sc->twa_dv.dv_xname);
1633 return;
1634 }
1635
1636 return;
1637 }
1638
1639 static void
1640 twa_shutdown(void *arg)
1641 {
1642 extern struct cfdriver twa_cd;
1643 struct twa_softc *sc;
1644 int i, rv, unit;
1645
1646 for (i = 0; i < twa_cd.cd_ndevs; i++) {
1647 if ((sc = device_lookup(&twa_cd, i)) == NULL)
1648 continue;
1649
1650 for (unit = 0; unit < TWA_MAX_UNITS; unit++)
1651 if (sc->sc_units[unit].td_dev != NULL)
1652 (void) config_detach(sc->sc_units[unit].td_dev,
1653 DETACH_FORCE | DETACH_QUIET);
1654
1655 twa_outl(sc, TWA_CONTROL_REGISTER_OFFSET,
1656 TWA_CONTROL_DISABLE_INTERRUPTS);
1657
1658 /* Let the controller know that we are going down. */
1659 rv = twa_init_connection(sc, TWA_SHUTDOWN_MESSAGE_CREDITS,
1660 0, 0, 0, 0, 0,
1661 NULL, NULL, NULL, NULL, NULL);
1662 }
1663 }
1664
1665 void
1666 twa_register_callbacks(struct twa_softc *sc, int unit,
1667 const struct twa_callbacks *tcb)
1668 {
1669
1670 sc->sc_units[unit].td_callbacks = tcb;
1671 }
1672
1673 /*
1674 * Print autoconfiguration message for a sub-device
1675 */
1676 static int
1677 twa_print(void *aux, const char *pnp)
1678 {
1679 struct twa_attach_args *twaa;
1680
1681 twaa = aux;
1682
1683 if (pnp !=NULL)
1684 aprint_normal("block device at %s\n", pnp);
1685 aprint_normal(" unit %d\n", twaa->twaa_unit);
1686 return (UNCONF);
1687 }
1688
1689 static void
1690 twa_fillin_sgl(struct twa_sg *sgl, bus_dma_segment_t *segs, int nsegments)
1691 {
1692 int i;
1693 for (i = 0; i < nsegments; i++) {
1694 sgl[i].address = segs[i].ds_addr;
1695 sgl[i].length = (uint32_t)(segs[i].ds_len);
1696 }
1697 }
1698
1699 static int
1700 twa_submit_io(struct twa_request *tr)
1701 {
1702 int error;
1703
1704 if ((error = twa_start(tr))) {
1705 if (error == EBUSY)
1706 error = 0; /* request is in the pending queue */
1707 else {
1708 tr->tr_error = error;
1709 }
1710 }
1711 return(error);
1712 }
1713
1714 /*
1715 * Function name: twa_setup_data_dmamap
1716 * Description: Callback of bus_dmamap_load for the buffer associated
1717 * with data. Updates the cmd pkt (size/sgl_entries
1718 * fields, as applicable) to reflect the number of sg
1719 * elements.
1720 *
1721 * Input: arg -- ptr to request pkt
1722 * segs -- ptr to a list of segment descriptors
1723 * nsegments--# of segments
1724 * error -- 0 if no errors encountered before callback,
1725 * non-zero if errors were encountered
1726 * Output: None
1727 * Return value: None
1728 */
1729 static int
1730 twa_setup_data_dmamap(void *arg, bus_dma_segment_t *segs, int nsegments,
1731 int error)
1732 {
1733 struct twa_request *tr = (struct twa_request *)arg;
1734 struct twa_command_packet *cmdpkt = tr->tr_command;
1735 struct twa_command_9k *cmd9k;
1736 union twa_command_7k *cmd7k;
1737 uint8_t sgl_offset;
1738
1739 if (error == EFBIG) {
1740 tr->tr_error = error;
1741 goto out;
1742 }
1743
1744 if (tr->tr_cmd_pkt_type & TWA_CMD_PKT_TYPE_9K) {
1745 cmd9k = &(cmdpkt->command.cmd_pkt_9k);
1746 twa_fillin_sgl(&(cmd9k->sg_list[0]), segs, nsegments);
1747 cmd9k->sgl_entries += nsegments - 1;
1748 } else {
1749 /* It's a 7000 command packet. */
1750 cmd7k = &(cmdpkt->command.cmd_pkt_7k);
1751 if ((sgl_offset = cmdpkt->command.cmd_pkt_7k.generic.sgl_offset))
1752 twa_fillin_sgl((struct twa_sg *)
1753 (((uint32_t *)cmd7k) + sgl_offset),
1754 segs, nsegments);
1755 /* Modify the size field, based on sg address size. */
1756 cmd7k->generic.size +=
1757 ((TWA_64BIT_ADDRESSES ? 3 : 2) * nsegments);
1758 }
1759
1760 if (tr->tr_flags & TWA_CMD_DATA_IN)
1761 bus_dmamap_sync(tr->tr_sc->twa_dma_tag, tr->tr_dma_map, 0,
1762 tr->tr_length, BUS_DMASYNC_PREREAD);
1763 if (tr->tr_flags & TWA_CMD_DATA_OUT) {
1764 /*
1765 * If we're using an alignment buffer, and we're
1766 * writing data, copy the real data out.
1767 */
1768 if (tr->tr_flags & TWA_CMD_DATA_COPY_NEEDED)
1769 memcpy(tr->tr_data, tr->tr_real_data,
1770 tr->tr_real_length);
1771 bus_dmamap_sync(tr->tr_sc->twa_dma_tag, tr->tr_dma_map, 0,
1772 tr->tr_length, BUS_DMASYNC_PREWRITE);
1773 }
1774 error = twa_submit_io(tr);
1775
1776 out:
1777 if (error) {
1778 twa_unmap_request(tr);
1779 /*
1780 * If the caller had been returned EINPROGRESS, and he has
1781 * registered a callback for handling completion, the callback
1782 * will never get called because we were unable to submit the
1783 * request. So, free up the request right here.
1784 */
1785 if ((tr->tr_flags & TWA_CMD_IN_PROGRESS) && (tr->tr_callback))
1786 twa_release_request(tr);
1787 }
1788 return (error);
1789 }
1790
1791 /*
1792 * Function name: twa_map_request
1793 * Description: Maps a cmd pkt and data associated with it, into
1794 * DMA'able memory.
1795 *
1796 * Input: tr -- ptr to request pkt
1797 * Output: None
1798 * Return value: 0 -- success
1799 * non-zero-- failure
1800 */
1801 int
1802 twa_map_request(struct twa_request *tr)
1803 {
1804 struct twa_softc *sc = tr->tr_sc;
1805 int s, rv, error = 0;
1806
1807 /* If the command involves data, map that too. */
1808 if (tr->tr_data != NULL) {
1809
1810 if (((u_long)tr->tr_data & (511)) != 0) {
1811 tr->tr_flags |= TWA_CMD_DATA_COPY_NEEDED;
1812 tr->tr_real_data = tr->tr_data;
1813 tr->tr_real_length = tr->tr_length;
1814 s = splvm();
1815 tr->tr_data = (void *)uvm_km_alloc(kmem_map,
1816 tr->tr_length, 512, UVM_KMF_NOWAIT|UVM_KMF_WIRED);
1817 splx(s);
1818
1819 if (tr->tr_data == NULL) {
1820 tr->tr_data = tr->tr_real_data;
1821 tr->tr_length = tr->tr_real_length;
1822 return(ENOMEM);
1823 }
1824 if ((tr->tr_flags & TWA_CMD_DATA_IN) != 0)
1825 memcpy(tr->tr_data, tr->tr_real_data,
1826 tr->tr_length);
1827 }
1828
1829 /*
1830 * Map the data buffer into bus space and build the S/G list.
1831 */
1832 rv = bus_dmamap_load(sc->twa_dma_tag, tr->tr_dma_map,
1833 tr->tr_data, tr->tr_length, NULL, BUS_DMA_NOWAIT |
1834 BUS_DMA_STREAMING | (tr->tr_flags & TWA_CMD_DATA_OUT) ?
1835 BUS_DMA_READ : BUS_DMA_WRITE);
1836
1837 if (rv != 0) {
1838 if ((tr->tr_flags & TWA_CMD_DATA_COPY_NEEDED) != 0) {
1839 s = splvm();
1840 uvm_km_free(kmem_map, (vaddr_t)tr->tr_data,
1841 tr->tr_length, UVM_KMF_WIRED);
1842 splx(s);
1843 }
1844 return (rv);
1845 }
1846
1847 if ((rv = twa_setup_data_dmamap(tr,
1848 tr->tr_dma_map->dm_segs,
1849 tr->tr_dma_map->dm_nsegs, error))) {
1850
1851 if (tr->tr_flags & TWA_CMD_DATA_COPY_NEEDED) {
1852 s = splvm();
1853 uvm_km_free(kmem_map, (vaddr_t)tr->tr_data,
1854 tr->tr_length, UVM_KMF_WIRED);
1855 splx(s);
1856 tr->tr_data = tr->tr_real_data;
1857 tr->tr_length = tr->tr_real_length;
1858 }
1859 } else
1860 error = tr->tr_error;
1861
1862 } else
1863 if ((rv = twa_submit_io(tr)))
1864 twa_unmap_request(tr);
1865
1866 return (rv);
1867 }
1868
1869 #if 0
1870 /*
1871 * Function name: twa_flash_firmware
1872 * Description: Flashes bundled firmware image onto controller.
1873 *
1874 * Input: sc -- ptr to per ctlr structure
1875 * Output: None
1876 * Return value: 0 -- success
1877 * non-zero-- failure
1878 */
1879 static int
1880 twa_flash_firmware(struct twa_softc *sc)
1881 {
1882 struct twa_request *tr;
1883 struct twa_command_download_firmware *cmd;
1884 uint32_t count;
1885 uint32_t fw_img_chunk_size;
1886 uint32_t this_chunk_size = 0;
1887 uint32_t remaining_img_size = 0;
1888 int s, error = 0;
1889 int i;
1890
1891 if ((tr = twa_get_request(sc, 0)) == NULL) {
1892 /* No free request packets available. Can't proceed. */
1893 error = EIO;
1894 goto out;
1895 }
1896
1897 count = (twa_fw_img_size / 65536);
1898
1899 count += ((twa_fw_img_size % 65536) != 0) ? 1 : 0;
1900
1901 tr->tr_cmd_pkt_type |= TWA_CMD_PKT_TYPE_INTERNAL;
1902 /* Allocate sufficient memory to hold a chunk of the firmware image. */
1903 fw_img_chunk_size = ((twa_fw_img_size / count) + 511) & ~511;
1904
1905 s = splvm();
1906 tr->tr_data = (void *)uvm_km_alloc(kmem_map, fw_img_chunk_size, 512,
1907 UVM_KMF_WIRED);
1908 splx(s);
1909
1910 if (tr->tr_data == NULL) {
1911 error = ENOMEM;
1912 goto out;
1913 }
1914
1915 remaining_img_size = twa_fw_img_size;
1916 cmd = &(tr->tr_command->command.cmd_pkt_7k.download_fw);
1917
1918 for (i = 0; i < count; i++) {
1919 /* Build a cmd pkt for downloading firmware. */
1920 memset(tr->tr_command, 0, sizeof(struct twa_command_packet));
1921
1922 tr->tr_command->cmd_hdr.header_desc.size_header = 128;
1923
1924 cmd->opcode = TWA_OP_DOWNLOAD_FIRMWARE;
1925 cmd->sgl_offset = 2; /* offset in dwords, to the beginning
1926 of sg list */
1927 cmd->size = 2; /* this field will be updated at data
1928 map time */
1929 cmd->request_id = tr->tr_request_id;
1930 cmd->unit = 0;
1931 cmd->status = 0;
1932 cmd->flags = 0;
1933 cmd->param = 8; /* prom image */
1934
1935 if (i != (count - 1))
1936 this_chunk_size = fw_img_chunk_size;
1937 else /* last chunk */
1938 this_chunk_size = remaining_img_size;
1939
1940 remaining_img_size -= this_chunk_size;
1941
1942 memset(tr->tr_data, 0, fw_img_chunk_size);
1943
1944 memcpy(tr->tr_data, twa_fw_img + (i * fw_img_chunk_size),
1945 this_chunk_size);
1946 /*
1947 * The next line will effect only the last chunk.
1948 */
1949 tr->tr_length = (this_chunk_size + 511) & ~511;
1950
1951 tr->tr_flags |= TWA_CMD_DATA_OUT;
1952
1953 error = twa_immediate_request(tr, TWA_REQUEST_TIMEOUT_PERIOD);
1954
1955 if (error) {
1956 if (error == ETIMEDOUT)
1957 /* clean-up done by twa_immediate_request */
1958 return(error);
1959 break;
1960 }
1961 error = cmd->status;
1962
1963 if (i != (count - 1)) {
1964
1965 /*
1966 * XXX FreeBSD code doesn't check for no error condition
1967 * but based on observation, error seems to return 0
1968 */
1969 if ((error =
1970 tr->tr_command->cmd_hdr.status_block.error) == 0) {
1971 continue;
1972 } else if ((error =
1973 tr->tr_command->cmd_hdr.status_block.error) ==
1974 TWA_ERROR_MORE_DATA) {
1975 continue;
1976 } else {
1977 twa_hard_reset(sc);
1978 break;
1979 }
1980 } else /* last chunk */
1981 if (error) {
1982 printf("%s: firmware flash request failed. "
1983 "error = 0x%x\n", sc->twa_dv.dv_xname,
1984 error);
1985 twa_hard_reset(sc);
1986 }
1987 }
1988
1989 if (tr->tr_data) {
1990 s = splvm();
1991 uvm_km_free(kmem_map, (vaddr_t)tr->tr_data,
1992 fw_img_chunk_size, UVM_KMF_WIRED);
1993 splx(s);
1994 }
1995 out:
1996 if (tr)
1997 twa_release_request(tr);
1998 return(error);
1999 }
2000
2001 /*
2002 * Function name: twa_hard_reset
2003 * Description: Hard reset the controller.
2004 *
2005 * Input: sc -- ptr to per ctlr structure
2006 * Output: None
2007 * Return value: 0 -- success
2008 * non-zero-- failure
2009 */
2010 static int
2011 twa_hard_reset(struct twa_softc *sc)
2012 {
2013 struct twa_request *tr;
2014 struct twa_command_reset_firmware *cmd;
2015 int error;
2016
2017 if ((tr = twa_get_request(sc, 0)) == NULL)
2018 return(EIO);
2019 tr->tr_cmd_pkt_type |= TWA_CMD_PKT_TYPE_INTERNAL;
2020 /* Build a cmd pkt for sending down the hard reset command. */
2021 tr->tr_command->cmd_hdr.header_desc.size_header = 128;
2022
2023 cmd = &(tr->tr_command->command.cmd_pkt_7k.reset_fw);
2024 cmd->opcode = TWA_OP_RESET_FIRMWARE;
2025 cmd->size = 2; /* this field will be updated at data map time */
2026 cmd->request_id = tr->tr_request_id;
2027 cmd->unit = 0;
2028 cmd->status = 0;
2029 cmd->flags = 0;
2030 cmd->param = 0; /* don't reload FPGA logic */
2031
2032 tr->tr_data = NULL;
2033 tr->tr_length = 0;
2034
2035 error = twa_immediate_request(tr, TWA_REQUEST_TIMEOUT_PERIOD);
2036 if (error) {
2037 printf("%s: hard reset request could not be posted. "
2038 "error = 0x%x\n", sc->twa_dv.dv_xname, error);
2039 if (error == ETIMEDOUT)
2040 /* clean-up done by twa_immediate_request */
2041 return(error);
2042 goto out;
2043 }
2044 if ((error = cmd->status)) {
2045 printf("%s: hard reset request failed. error = 0x%x\n",
2046 sc->twa_dv.dv_xname, error);
2047 }
2048
2049 out:
2050 if (tr)
2051 twa_release_request(tr);
2052 return(error);
2053 }
2054 #endif
2055
2056 /*
2057 * Function name: twa_intr
2058 * Description: Interrupt handler. Determines the kind of interrupt,
2059 * and calls the appropriate handler.
2060 *
2061 * Input: sc -- ptr to per ctlr structure
2062 * Output: None
2063 * Return value: None
2064 */
2065
2066 static int
2067 twa_intr(void *arg)
2068 {
2069 int caught, s, rv;
2070 struct twa_softc *sc;
2071 uint32_t status_reg;
2072 sc = (struct twa_softc *)arg;
2073
2074 caught = 0;
2075 /* Collect current interrupt status. */
2076 status_reg = twa_inl(sc, TWA_STATUS_REGISTER_OFFSET);
2077 if (twa_check_ctlr_state(sc, status_reg)) {
2078 caught = 1;
2079 goto bail;
2080 }
2081 /* Dispatch based on the kind of interrupt. */
2082 if (status_reg & TWA_STATUS_HOST_INTERRUPT) {
2083 twa_outl(sc, TWA_CONTROL_REGISTER_OFFSET,
2084 TWA_CONTROL_CLEAR_HOST_INTERRUPT);
2085 caught = 1;
2086 }
2087 if ((status_reg & TWA_STATUS_ATTENTION_INTERRUPT) != 0) {
2088 twa_outl(sc, TWA_CONTROL_REGISTER_OFFSET,
2089 TWA_CONTROL_CLEAR_ATTENTION_INTERRUPT);
2090 rv = twa_fetch_aen(sc);
2091 #ifdef DIAGNOSTIC
2092 if (rv != 0)
2093 printf("%s: unable to retrieve AEN (%d)\n",
2094 sc->twa_dv.dv_xname, rv);
2095 #endif
2096 caught = 1;
2097 }
2098 if (status_reg & TWA_STATUS_COMMAND_INTERRUPT) {
2099 /* Start any requests that might be in the pending queue. */
2100 twa_outl(sc, TWA_CONTROL_REGISTER_OFFSET,
2101 TWA_CONTROL_MASK_COMMAND_INTERRUPT);
2102 (void)twa_drain_pending_queue(sc);
2103 caught = 1;
2104 }
2105 if (status_reg & TWA_STATUS_RESPONSE_INTERRUPT) {
2106 s = splbio();
2107 twa_done(sc);
2108 splx(s);
2109 caught = 1;
2110 }
2111 bail:
2112 return (caught);
2113 }
2114
2115 /*
2116 * Accept an open operation on the control device.
2117 */
2118 static int
2119 twaopen(dev_t dev, int flag, int mode, struct lwp *l)
2120 {
2121 struct twa_softc *twa;
2122
2123 if ((twa = device_lookup(&twa_cd, minor(dev))) == NULL)
2124 return (ENXIO);
2125
2126 twa->twa_sc_flags |= TWA_STATE_OPEN;
2127
2128 return (0);
2129 }
2130
2131 /*
2132 * Accept the last close on the control device.
2133 */
2134 static int
2135 twaclose(dev_t dev, int flag, int mode,
2136 struct lwp *l)
2137 {
2138 struct twa_softc *twa;
2139
2140 twa = device_lookup(&twa_cd, minor(dev));
2141 twa->twa_sc_flags &= ~TWA_STATE_OPEN;
2142 return (0);
2143 }
2144
2145 /*
2146 * Function name: twaioctl
2147 * Description: ioctl handler.
2148 *
2149 * Input: sc -- ptr to per ctlr structure
2150 * cmd -- ioctl cmd
2151 * buf -- ptr to buffer in kernel memory, which is
2152 * a copy of the input buffer in user-space
2153 * Output: buf -- ptr to buffer in kernel memory, which will
2154 * be copied of the output buffer in user-space
2155 * Return value: 0 -- success
2156 * non-zero-- failure
2157 */
2158 static int
2159 twaioctl(dev_t dev, u_long cmd, caddr_t data, int flag,
2160 struct lwp *l)
2161 {
2162 struct twa_softc *sc;
2163 struct twa_ioctl_9k *user_buf = (struct twa_ioctl_9k *)data;
2164 struct tw_cl_event_packet event_buf;
2165 struct twa_request *tr = 0;
2166 int32_t event_index = 0;
2167 int32_t start_index;
2168 int s, error = 0;
2169
2170 sc = device_lookup(&twa_cd, minor(dev));
2171
2172 switch (cmd) {
2173 case TW_OSL_IOCTL_FIRMWARE_PASS_THROUGH:
2174 {
2175 struct twa_command_packet *cmdpkt;
2176 uint32_t data_buf_size_adjusted;
2177
2178 /* Get a request packet */
2179 tr = twa_get_request_wait(sc, 0);
2180 KASSERT(tr != NULL);
2181 /*
2182 * Make sure that the data buffer sent to firmware is a
2183 * 512 byte multiple in size.
2184 */
2185 data_buf_size_adjusted =
2186 (user_buf->twa_drvr_pkt.buffer_length + 511) & ~511;
2187
2188 if ((tr->tr_length = data_buf_size_adjusted)) {
2189 if ((tr->tr_data = malloc(data_buf_size_adjusted,
2190 M_DEVBUF, M_WAITOK)) == NULL) {
2191 error = ENOMEM;
2192 goto fw_passthru_done;
2193 }
2194 /* Copy the payload. */
2195 if ((error = copyin((void *) (user_buf->pdata),
2196 (void *) (tr->tr_data),
2197 user_buf->twa_drvr_pkt.buffer_length)) != 0) {
2198 goto fw_passthru_done;
2199 }
2200 tr->tr_flags |= TWA_CMD_DATA_IN | TWA_CMD_DATA_OUT;
2201 }
2202 tr->tr_cmd_pkt_type |= TWA_CMD_PKT_TYPE_IOCTL;
2203 cmdpkt = tr->tr_command;
2204
2205 /* Copy the command packet. */
2206 memcpy(cmdpkt, &(user_buf->twa_cmd_pkt),
2207 sizeof(struct twa_command_packet));
2208 cmdpkt->command.cmd_pkt_7k.generic.request_id =
2209 tr->tr_request_id;
2210
2211 /* Send down the request, and wait for it to complete. */
2212 if ((error = twa_wait_request(tr, TWA_REQUEST_TIMEOUT_PERIOD))) {
2213 if (error == ETIMEDOUT)
2214 break; /* clean-up done by twa_wait_request */
2215 goto fw_passthru_done;
2216 }
2217
2218 /* Copy the command packet back into user space. */
2219 memcpy(&user_buf->twa_cmd_pkt, cmdpkt,
2220 sizeof(struct twa_command_packet));
2221
2222 /* If there was a payload, copy it back too. */
2223 if (tr->tr_length)
2224 error = copyout(tr->tr_data, user_buf->pdata,
2225 user_buf->twa_drvr_pkt.buffer_length);
2226 fw_passthru_done:
2227 /* Free resources. */
2228 if (tr->tr_data)
2229 free(tr->tr_data, M_DEVBUF);
2230
2231 if (tr)
2232 twa_release_request(tr);
2233 break;
2234 }
2235
2236 case TW_OSL_IOCTL_SCAN_BUS:
2237 twa_request_bus_scan(sc);
2238 break;
2239
2240 case TW_CL_IOCTL_GET_FIRST_EVENT:
2241 if (sc->twa_aen_queue_wrapped) {
2242 if (sc->twa_aen_queue_overflow) {
2243 /*
2244 * The aen queue has wrapped, even before some
2245 * events have been retrieved. Let the caller
2246 * know that he missed out on some AEN's.
2247 */
2248 user_buf->twa_drvr_pkt.status =
2249 TWA_ERROR_AEN_OVERFLOW;
2250 sc->twa_aen_queue_overflow = FALSE;
2251 } else
2252 user_buf->twa_drvr_pkt.status = 0;
2253 event_index = sc->twa_aen_head;
2254 } else {
2255 if (sc->twa_aen_head == sc->twa_aen_tail) {
2256 user_buf->twa_drvr_pkt.status =
2257 TWA_ERROR_AEN_NO_EVENTS;
2258 break;
2259 }
2260 user_buf->twa_drvr_pkt.status = 0;
2261 event_index = sc->twa_aen_tail; /* = 0 */
2262 }
2263 if ((error = copyout(sc->twa_aen_queue[event_index],
2264 user_buf->pdata, sizeof(struct tw_cl_event_packet))) != 0)
2265 (sc->twa_aen_queue[event_index])->retrieved =
2266 TWA_AEN_RETRIEVED;
2267 break;
2268
2269 case TW_CL_IOCTL_GET_LAST_EVENT:
2270 if (sc->twa_aen_queue_wrapped) {
2271 if (sc->twa_aen_queue_overflow) {
2272 /*
2273 * The aen queue has wrapped, even before some
2274 * events have been retrieved. Let the caller
2275 * know that he missed out on some AEN's.
2276 */
2277 user_buf->twa_drvr_pkt.status =
2278 TWA_ERROR_AEN_OVERFLOW;
2279 sc->twa_aen_queue_overflow = FALSE;
2280 } else
2281 user_buf->twa_drvr_pkt.status = 0;
2282 } else {
2283 if (sc->twa_aen_head == sc->twa_aen_tail) {
2284 user_buf->twa_drvr_pkt.status =
2285 TWA_ERROR_AEN_NO_EVENTS;
2286 break;
2287 }
2288 user_buf->twa_drvr_pkt.status = 0;
2289 }
2290 event_index =
2291 (sc->twa_aen_head - 1 + TWA_Q_LENGTH) % TWA_Q_LENGTH;
2292 if ((error = copyout(sc->twa_aen_queue[event_index],
2293 user_buf->pdata, sizeof(struct tw_cl_event_packet))) != 0)
2294 (sc->twa_aen_queue[event_index])->retrieved =
2295 TWA_AEN_RETRIEVED;
2296 break;
2297
2298 case TW_CL_IOCTL_GET_NEXT_EVENT:
2299 user_buf->twa_drvr_pkt.status = 0;
2300 if (sc->twa_aen_queue_wrapped) {
2301
2302 if (sc->twa_aen_queue_overflow) {
2303 /*
2304 * The aen queue has wrapped, even before some
2305 * events have been retrieved. Let the caller
2306 * know that he missed out on some AEN's.
2307 */
2308 user_buf->twa_drvr_pkt.status =
2309 TWA_ERROR_AEN_OVERFLOW;
2310 sc->twa_aen_queue_overflow = FALSE;
2311 }
2312 start_index = sc->twa_aen_head;
2313 } else {
2314 if (sc->twa_aen_head == sc->twa_aen_tail) {
2315 user_buf->twa_drvr_pkt.status =
2316 TWA_ERROR_AEN_NO_EVENTS;
2317 break;
2318 }
2319 start_index = sc->twa_aen_tail; /* = 0 */
2320 }
2321 error = copyin(user_buf->pdata, &event_buf,
2322 sizeof(struct tw_cl_event_packet));
2323
2324 event_index = (start_index + event_buf.sequence_id -
2325 (sc->twa_aen_queue[start_index])->sequence_id + 1)
2326 % TWA_Q_LENGTH;
2327
2328 if (!((sc->twa_aen_queue[event_index])->sequence_id >
2329 event_buf.sequence_id)) {
2330 if (user_buf->twa_drvr_pkt.status ==
2331 TWA_ERROR_AEN_OVERFLOW)
2332 /* so we report the overflow next time */
2333 sc->twa_aen_queue_overflow = TRUE;
2334 user_buf->twa_drvr_pkt.status = TWA_ERROR_AEN_NO_EVENTS;
2335 break;
2336 }
2337 if ((error = copyout(sc->twa_aen_queue[event_index],
2338 user_buf->pdata, sizeof(struct tw_cl_event_packet))) != 0)
2339 (sc->twa_aen_queue[event_index])->retrieved =
2340 TWA_AEN_RETRIEVED;
2341 break;
2342
2343 case TW_CL_IOCTL_GET_PREVIOUS_EVENT:
2344 user_buf->twa_drvr_pkt.status = 0;
2345 if (sc->twa_aen_queue_wrapped) {
2346 if (sc->twa_aen_queue_overflow) {
2347 /*
2348 * The aen queue has wrapped, even before some
2349 * events have been retrieved. Let the caller
2350 * know that he missed out on some AEN's.
2351 */
2352 user_buf->twa_drvr_pkt.status =
2353 TWA_ERROR_AEN_OVERFLOW;
2354 sc->twa_aen_queue_overflow = FALSE;
2355 }
2356 start_index = sc->twa_aen_head;
2357 } else {
2358 if (sc->twa_aen_head == sc->twa_aen_tail) {
2359 user_buf->twa_drvr_pkt.status =
2360 TWA_ERROR_AEN_NO_EVENTS;
2361 break;
2362 }
2363 start_index = sc->twa_aen_tail; /* = 0 */
2364 }
2365 if ((error = copyin(user_buf->pdata, &event_buf,
2366 sizeof(struct tw_cl_event_packet))) != 0)
2367
2368 event_index = (start_index + event_buf.sequence_id -
2369 (sc->twa_aen_queue[start_index])->sequence_id - 1)
2370 % TWA_Q_LENGTH;
2371 if (!((sc->twa_aen_queue[event_index])->sequence_id <
2372 event_buf.sequence_id)) {
2373 if (user_buf->twa_drvr_pkt.status ==
2374 TWA_ERROR_AEN_OVERFLOW)
2375 /* so we report the overflow next time */
2376 sc->twa_aen_queue_overflow = TRUE;
2377 user_buf->twa_drvr_pkt.status =
2378 TWA_ERROR_AEN_NO_EVENTS;
2379 break;
2380 }
2381 if ((error = copyout(sc->twa_aen_queue [event_index],
2382 user_buf->pdata, sizeof(struct tw_cl_event_packet))) != 0)
2383 aprint_error("%s: get_previous: Could not copyout to "
2384 "event_buf. error = %x\n", sc->twa_dv.dv_xname,
2385 error);
2386 (sc->twa_aen_queue[event_index])->retrieved = TWA_AEN_RETRIEVED;
2387 break;
2388
2389 case TW_CL_IOCTL_GET_LOCK:
2390 {
2391 struct tw_cl_lock_packet twa_lock;
2392
2393 copyin(user_buf->pdata, &twa_lock,
2394 sizeof(struct tw_cl_lock_packet));
2395 s = splbio();
2396 if ((sc->twa_ioctl_lock.lock == TWA_LOCK_FREE) ||
2397 (twa_lock.force_flag) ||
2398 (time_second >= sc->twa_ioctl_lock.timeout)) {
2399
2400 sc->twa_ioctl_lock.lock = TWA_LOCK_HELD;
2401 sc->twa_ioctl_lock.timeout = time_second +
2402 (twa_lock.timeout_msec / 1000);
2403 twa_lock.time_remaining_msec = twa_lock.timeout_msec;
2404 user_buf->twa_drvr_pkt.status = 0;
2405 } else {
2406 twa_lock.time_remaining_msec =
2407 (sc->twa_ioctl_lock.timeout - time_second) *
2408 1000;
2409 user_buf->twa_drvr_pkt.status =
2410 TWA_ERROR_IOCTL_LOCK_ALREADY_HELD;
2411 }
2412 splx(s);
2413 copyout(&twa_lock, user_buf->pdata,
2414 sizeof(struct tw_cl_lock_packet));
2415 break;
2416 }
2417
2418 case TW_CL_IOCTL_RELEASE_LOCK:
2419 s = splbio();
2420 if (sc->twa_ioctl_lock.lock == TWA_LOCK_FREE) {
2421 user_buf->twa_drvr_pkt.status =
2422 TWA_ERROR_IOCTL_LOCK_NOT_HELD;
2423 } else {
2424 sc->twa_ioctl_lock.lock = TWA_LOCK_FREE;
2425 user_buf->twa_drvr_pkt.status = 0;
2426 }
2427 splx(s);
2428 break;
2429
2430 case TW_CL_IOCTL_GET_COMPATIBILITY_INFO:
2431 {
2432 struct tw_cl_compatibility_packet comp_pkt;
2433
2434 memcpy(comp_pkt.driver_version, TWA_DRIVER_VERSION_STRING,
2435 sizeof(TWA_DRIVER_VERSION_STRING));
2436 comp_pkt.working_srl = sc->working_srl;
2437 comp_pkt.working_branch = sc->working_branch;
2438 comp_pkt.working_build = sc->working_build;
2439 user_buf->twa_drvr_pkt.status = 0;
2440
2441 /* Copy compatibility information to user space. */
2442 copyout(&comp_pkt, user_buf->pdata,
2443 min(sizeof(struct tw_cl_compatibility_packet),
2444 user_buf->twa_drvr_pkt.buffer_length));
2445 break;
2446 }
2447
2448 case TWA_IOCTL_GET_UNITNAME: /* WASABI EXTENSION */
2449 {
2450 struct twa_unitname *tn;
2451 struct twa_drive *tdr;
2452
2453 tn = (struct twa_unitname *)data;
2454 /* XXX mutex */
2455 if (tn->tn_unit < 0 || tn->tn_unit >= TWA_MAX_UNITS)
2456 return (EINVAL);
2457 tdr = &sc->sc_units[tn->tn_unit];
2458 if (tdr->td_dev == NULL)
2459 tn->tn_name[0] = '\0';
2460 else
2461 strlcpy(tn->tn_name, tdr->td_dev->dv_xname,
2462 sizeof(tn->tn_name));
2463 return (0);
2464 }
2465
2466 default:
2467 /* Unknown opcode. */
2468 error = ENOTTY;
2469 }
2470
2471 return(error);
2472 }
2473
2474 const struct cdevsw twa_cdevsw = {
2475 twaopen, twaclose, noread, nowrite, twaioctl,
2476 nostop, notty, nopoll, nommap, nokqfilter, D_OTHER,
2477 };
2478
2479 /*
2480 * Function name: twa_get_param
2481 * Description: Get a firmware parameter.
2482 *
2483 * Input: sc -- ptr to per ctlr structure
2484 * table_id -- parameter table #
2485 * param_id -- index of the parameter in the table
2486 * param_size -- size of the parameter in bytes
2487 * callback -- ptr to function, if any, to be called
2488 * back on completion; NULL if no callback.
2489 * Output: None
2490 * Return value: ptr to param structure -- success
2491 * NULL -- failure
2492 */
2493 static int
2494 twa_get_param(struct twa_softc *sc, int table_id, int param_id,
2495 size_t param_size, void (* callback)(struct twa_request *tr),
2496 struct twa_param_9k **param)
2497 {
2498 int rv = 0;
2499 struct twa_request *tr;
2500 union twa_command_7k *cmd;
2501
2502 /* Get a request packet. */
2503 if ((tr = twa_get_request(sc, 0)) == NULL) {
2504 rv = EAGAIN;
2505 goto out;
2506 }
2507
2508 tr->tr_cmd_pkt_type |= TWA_CMD_PKT_TYPE_INTERNAL;
2509
2510 /* Allocate memory to read data into. */
2511 if ((*param = (struct twa_param_9k *)
2512 malloc(TWA_SECTOR_SIZE, M_DEVBUF, M_NOWAIT)) == NULL) {
2513 rv = ENOMEM;
2514 goto out;
2515 }
2516
2517 memset(*param, 0, sizeof(struct twa_param_9k) - 1 + param_size);
2518 tr->tr_data = *param;
2519 tr->tr_length = TWA_SECTOR_SIZE;
2520 tr->tr_flags = TWA_CMD_DATA_IN | TWA_CMD_DATA_OUT;
2521
2522 /* Build the cmd pkt. */
2523 cmd = &(tr->tr_command->command.cmd_pkt_7k);
2524
2525 tr->tr_command->cmd_hdr.header_desc.size_header = 128;
2526
2527 cmd->param.opcode = TWA_OP_GET_PARAM;
2528 cmd->param.sgl_offset = 2;
2529 cmd->param.size = 2;
2530 cmd->param.request_id = tr->tr_request_id;
2531 cmd->param.unit = 0;
2532 cmd->param.param_count = 1;
2533
2534 /* Specify which parameter we need. */
2535 (*param)->table_id = table_id | TWA_9K_PARAM_DESCRIPTOR;
2536 (*param)->parameter_id = param_id;
2537 (*param)->parameter_size_bytes = param_size;
2538
2539 /* Submit the command. */
2540 if (callback == NULL) {
2541 /* There's no call back; wait till the command completes. */
2542 rv = twa_immediate_request(tr, TWA_REQUEST_TIMEOUT_PERIOD);
2543
2544 if (rv != 0)
2545 goto out;
2546
2547 if ((rv = cmd->param.status) != 0) {
2548 /* twa_drain_complete_queue will have done the unmapping */
2549 goto out;
2550 }
2551 twa_release_request(tr);
2552 return (rv);
2553 } else {
2554 /* There's a call back. Simply submit the command. */
2555 tr->tr_callback = callback;
2556 rv = twa_map_request(tr);
2557 return (rv);
2558 }
2559 out:
2560 if (tr)
2561 twa_release_request(tr);
2562 return(rv);
2563 }
2564
2565 /*
2566 * Function name: twa_set_param
2567 * Description: Set a firmware parameter.
2568 *
2569 * Input: sc -- ptr to per ctlr structure
2570 * table_id -- parameter table #
2571 * param_id -- index of the parameter in the table
2572 * param_size -- size of the parameter in bytes
2573 * callback -- ptr to function, if any, to be called
2574 * back on completion; NULL if no callback.
2575 * Output: None
2576 * Return value: 0 -- success
2577 * non-zero-- failure
2578 */
2579 static int
2580 twa_set_param(struct twa_softc *sc, int table_id, int param_id, int param_size,
2581 void *data, void (* callback)(struct twa_request *tr))
2582 {
2583 struct twa_request *tr;
2584 union twa_command_7k *cmd;
2585 struct twa_param_9k *param = NULL;
2586 int error = ENOMEM;
2587
2588 tr = twa_get_request(sc, 0);
2589 if (tr == NULL)
2590 return (EAGAIN);
2591
2592 tr->tr_cmd_pkt_type |= TWA_CMD_PKT_TYPE_INTERNAL;
2593
2594 /* Allocate memory to send data using. */
2595 if ((param = (struct twa_param_9k *)
2596 malloc(TWA_SECTOR_SIZE, M_DEVBUF, M_NOWAIT)) == NULL)
2597 goto out;
2598 memset(param, 0, sizeof(struct twa_param_9k) - 1 + param_size);
2599 tr->tr_data = param;
2600 tr->tr_length = TWA_SECTOR_SIZE;
2601 tr->tr_flags = TWA_CMD_DATA_IN | TWA_CMD_DATA_OUT;
2602
2603 /* Build the cmd pkt. */
2604 cmd = &(tr->tr_command->command.cmd_pkt_7k);
2605
2606 tr->tr_command->cmd_hdr.header_desc.size_header = 128;
2607
2608 cmd->param.opcode = TWA_OP_SET_PARAM;
2609 cmd->param.sgl_offset = 2;
2610 cmd->param.size = 2;
2611 cmd->param.request_id = tr->tr_request_id;
2612 cmd->param.unit = 0;
2613 cmd->param.param_count = 1;
2614
2615 /* Specify which parameter we want to set. */
2616 param->table_id = table_id | TWA_9K_PARAM_DESCRIPTOR;
2617 param->parameter_id = param_id;
2618 param->parameter_size_bytes = param_size;
2619 memcpy(param->data, data, param_size);
2620
2621 /* Submit the command. */
2622 if (callback == NULL) {
2623 /* There's no call back; wait till the command completes. */
2624 error = twa_immediate_request(tr, TWA_REQUEST_TIMEOUT_PERIOD);
2625 if (error == ETIMEDOUT)
2626 /* clean-up done by twa_immediate_request */
2627 return(error);
2628 if (error)
2629 goto out;
2630 if ((error = cmd->param.status)) {
2631 /*
2632 * twa_drain_complete_queue will have done the
2633 * unmapping.
2634 */
2635 goto out;
2636 }
2637 free(param, M_DEVBUF);
2638 twa_release_request(tr);
2639 return(error);
2640 } else {
2641 /* There's a call back. Simply submit the command. */
2642 tr->tr_callback = callback;
2643 if ((error = twa_map_request(tr)))
2644 goto out;
2645
2646 return (0);
2647 }
2648 out:
2649 if (param)
2650 free(param, M_DEVBUF);
2651 if (tr)
2652 twa_release_request(tr);
2653 return(error);
2654 }
2655
2656 /*
2657 * Function name: twa_init_connection
2658 * Description: Send init_connection cmd to firmware
2659 *
2660 * Input: sc -- ptr to per ctlr structure
2661 * message_credits -- max # of requests that we might send
2662 * down simultaneously. This will be
2663 * typically set to 256 at init-time or
2664 * after a reset, and to 1 at shutdown-time
2665 * set_features -- indicates if we intend to use 64-bit
2666 * sg, also indicates if we want to do a
2667 * basic or an extended init_connection;
2668 *
2669 * Note: The following input/output parameters are valid, only in case of an
2670 * extended init_connection:
2671 *
2672 * current_fw_srl -- srl of fw we are bundled
2673 * with, if any; 0 otherwise
2674 * current_fw_arch_id -- arch_id of fw we are bundled
2675 * with, if any; 0 otherwise
2676 * current_fw_branch -- branch # of fw we are bundled
2677 * with, if any; 0 otherwise
2678 * current_fw_build -- build # of fw we are bundled
2679 * with, if any; 0 otherwise
2680 * Output: fw_on_ctlr_srl -- srl of fw on ctlr
2681 * fw_on_ctlr_arch_id -- arch_id of fw on ctlr
2682 * fw_on_ctlr_branch -- branch # of fw on ctlr
2683 * fw_on_ctlr_build -- build # of fw on ctlr
2684 * init_connect_result -- result bitmap of fw response
2685 * Return value: 0 -- success
2686 * non-zero-- failure
2687 */
2688 static int
2689 twa_init_connection(struct twa_softc *sc, uint16_t message_credits,
2690 uint32_t set_features, uint16_t current_fw_srl,
2691 uint16_t current_fw_arch_id, uint16_t current_fw_branch,
2692 uint16_t current_fw_build, uint16_t *fw_on_ctlr_srl,
2693 uint16_t *fw_on_ctlr_arch_id, uint16_t *fw_on_ctlr_branch,
2694 uint16_t *fw_on_ctlr_build, uint32_t *init_connect_result)
2695 {
2696 struct twa_request *tr;
2697 struct twa_command_init_connect *init_connect;
2698 int error = 1;
2699
2700 /* Get a request packet. */
2701 if ((tr = twa_get_request(sc, 0)) == NULL)
2702 goto out;
2703 tr->tr_cmd_pkt_type |= TWA_CMD_PKT_TYPE_INTERNAL;
2704 /* Build the cmd pkt. */
2705 init_connect = &(tr->tr_command->command.cmd_pkt_7k.init_connect);
2706
2707 tr->tr_command->cmd_hdr.header_desc.size_header = 128;
2708
2709 init_connect->opcode = TWA_OP_INIT_CONNECTION;
2710 init_connect->request_id = tr->tr_request_id;
2711 init_connect->message_credits = message_credits;
2712 init_connect->features = set_features;
2713 if (TWA_64BIT_ADDRESSES) {
2714 printf("64 bit addressing supported for scatter/gather list\n");
2715 init_connect->features |= TWA_64BIT_SG_ADDRESSES;
2716 }
2717 if (set_features & TWA_EXTENDED_INIT_CONNECT) {
2718 /*
2719 * Fill in the extra fields needed for
2720 * an extended init_connect.
2721 */
2722 init_connect->size = 6;
2723 init_connect->fw_srl = current_fw_srl;
2724 init_connect->fw_arch_id = current_fw_arch_id;
2725 init_connect->fw_branch = current_fw_branch;
2726 } else
2727 init_connect->size = 3;
2728
2729 /* Submit the command, and wait for it to complete. */
2730 error = twa_immediate_request(tr, TWA_REQUEST_TIMEOUT_PERIOD);
2731 if (error == ETIMEDOUT)
2732 return(error); /* clean-up done by twa_immediate_request */
2733 if (error)
2734 goto out;
2735 if ((error = init_connect->status)) {
2736 /* twa_drain_complete_queue will have done the unmapping */
2737 goto out;
2738 }
2739 if (set_features & TWA_EXTENDED_INIT_CONNECT) {
2740 *fw_on_ctlr_srl = init_connect->fw_srl;
2741 *fw_on_ctlr_arch_id = init_connect->fw_arch_id;
2742 *fw_on_ctlr_branch = init_connect->fw_branch;
2743 *fw_on_ctlr_build = init_connect->fw_build;
2744 *init_connect_result = init_connect->result;
2745 }
2746 twa_release_request(tr);
2747 return(error);
2748
2749 out:
2750 if (tr)
2751 twa_release_request(tr);
2752 return(error);
2753 }
2754
2755 static int
2756 twa_reset(struct twa_softc *sc)
2757 {
2758 int s;
2759 int error = 0;
2760
2761 /*
2762 * Disable interrupts from the controller, and mask any
2763 * accidental entry into our interrupt handler.
2764 */
2765 twa_outl(sc, TWA_CONTROL_REGISTER_OFFSET,
2766 TWA_CONTROL_DISABLE_INTERRUPTS);
2767
2768 s = splbio();
2769
2770 /* Soft reset the controller. */
2771 if ((error = twa_soft_reset(sc)))
2772 goto out;
2773
2774 /* Re-establish logical connection with the controller. */
2775 if ((error = twa_init_connection(sc, TWA_INIT_MESSAGE_CREDITS,
2776 0, 0, 0, 0, 0,
2777 NULL, NULL, NULL, NULL, NULL))) {
2778 goto out;
2779 }
2780 /*
2781 * Complete all requests in the complete queue; error back all requests
2782 * in the busy queue. Any internal requests will be simply freed.
2783 * Re-submit any requests in the pending queue.
2784 */
2785 twa_drain_busy_queue(sc);
2786
2787 out:
2788 splx(s);
2789 /*
2790 * Enable interrupts, and also clear attention and response interrupts.
2791 */
2792 twa_outl(sc, TWA_CONTROL_REGISTER_OFFSET,
2793 TWA_CONTROL_CLEAR_ATTENTION_INTERRUPT |
2794 TWA_CONTROL_UNMASK_RESPONSE_INTERRUPT |
2795 TWA_CONTROL_ENABLE_INTERRUPTS);
2796 return(error);
2797 }
2798
2799 static int
2800 twa_soft_reset(struct twa_softc *sc)
2801 {
2802 uint32_t status_reg;
2803
2804 twa_outl(sc, TWA_CONTROL_REGISTER_OFFSET,
2805 TWA_CONTROL_ISSUE_SOFT_RESET |
2806 TWA_CONTROL_CLEAR_HOST_INTERRUPT |
2807 TWA_CONTROL_CLEAR_ATTENTION_INTERRUPT |
2808 TWA_CONTROL_MASK_COMMAND_INTERRUPT |
2809 TWA_CONTROL_MASK_RESPONSE_INTERRUPT |
2810 TWA_CONTROL_DISABLE_INTERRUPTS);
2811
2812 if (twa_wait_status(sc, TWA_STATUS_MICROCONTROLLER_READY |
2813 TWA_STATUS_ATTENTION_INTERRUPT, 30)) {
2814 aprint_error("%s: no attention interrupt after reset.\n",
2815 sc->twa_dv.dv_xname);
2816 return(1);
2817 }
2818 twa_outl(sc, TWA_CONTROL_REGISTER_OFFSET,
2819 TWA_CONTROL_CLEAR_ATTENTION_INTERRUPT);
2820
2821 if (twa_drain_response_queue(sc)) {
2822 aprint_error("%s: cannot drain response queue.\n",
2823 sc->twa_dv.dv_xname);
2824 return(1);
2825 }
2826 if (twa_drain_aen_queue(sc)) {
2827 aprint_error("%s: cannot drain AEN queue.\n",
2828 sc->twa_dv.dv_xname);
2829 return(1);
2830 }
2831 if (twa_find_aen(sc, TWA_AEN_SOFT_RESET)) {
2832 aprint_error("%s: reset not reported by controller.\n",
2833 sc->twa_dv.dv_xname);
2834 return(1);
2835 }
2836 status_reg = twa_inl(sc, TWA_STATUS_REGISTER_OFFSET);
2837 if (TWA_STATUS_ERRORS(status_reg) ||
2838 twa_check_ctlr_state(sc, status_reg)) {
2839 aprint_error("%s: controller errors detected.\n",
2840 sc->twa_dv.dv_xname);
2841 return(1);
2842 }
2843 return(0);
2844 }
2845
2846 static int
2847 twa_wait_status(struct twa_softc *sc, uint32_t status, uint32_t timeout)
2848 {
2849 struct timeval t1;
2850 time_t end_time;
2851 uint32_t status_reg;
2852
2853 timeout = (timeout * 1000 * 100);
2854
2855 microtime(&t1);
2856
2857 end_time = t1.tv_usec + timeout;
2858
2859 do {
2860 status_reg = twa_inl(sc, TWA_STATUS_REGISTER_OFFSET);
2861 /* got the required bit(s)? */
2862 if ((status_reg & status) == status)
2863 return(0);
2864 DELAY(100000);
2865 microtime(&t1);
2866 } while (t1.tv_usec <= end_time);
2867
2868 return(1);
2869 }
2870
2871 static int
2872 twa_fetch_aen(struct twa_softc *sc)
2873 {
2874 struct twa_request *tr;
2875 int s, error = 0;
2876
2877 s = splbio();
2878
2879 if ((tr = twa_get_request(sc, TWA_CMD_AEN)) == NULL) {
2880 splx(s);
2881 return(EIO);
2882 }
2883 tr->tr_cmd_pkt_type |= TWA_CMD_PKT_TYPE_INTERNAL;
2884 tr->tr_callback = twa_aen_callback;
2885 tr->tr_data = malloc(TWA_SECTOR_SIZE, M_DEVBUF, M_NOWAIT);
2886 if (twa_request_sense(tr, 0) != 0) {
2887 if (tr->tr_data)
2888 free(tr->tr_data, M_DEVBUF);
2889 twa_release_request(tr);
2890 error = 1;
2891 }
2892 splx(s);
2893
2894 return(error);
2895 }
2896
2897 /*
2898 * Function name: twa_aen_callback
2899 * Description: Callback for requests to fetch AEN's.
2900 *
2901 * Input: tr -- ptr to completed request pkt
2902 * Output: None
2903 * Return value: None
2904 */
2905 static void
2906 twa_aen_callback(struct twa_request *tr)
2907 {
2908 int i;
2909 int fetch_more_aens = 0;
2910 struct twa_softc *sc = tr->tr_sc;
2911 struct twa_command_header *cmd_hdr =
2912 (struct twa_command_header *)(tr->tr_data);
2913 struct twa_command_9k *cmd =
2914 &(tr->tr_command->command.cmd_pkt_9k);
2915
2916 if (! cmd->status) {
2917 if ((tr->tr_cmd_pkt_type & TWA_CMD_PKT_TYPE_9K) &&
2918 (cmd->cdb[0] == 0x3 /* REQUEST_SENSE */))
2919 if (twa_enqueue_aen(sc, cmd_hdr)
2920 != TWA_AEN_QUEUE_EMPTY)
2921 fetch_more_aens = 1;
2922 } else {
2923 cmd_hdr->err_specific_desc[sizeof(cmd_hdr->err_specific_desc) - 1] = '\0';
2924 for (i = 0; i < 18; i++)
2925 printf("%x\t", tr->tr_command->cmd_hdr.sense_data[i]);
2926
2927 printf(""); /* print new line */
2928
2929 for (i = 0; i < 128; i++)
2930 printf("%x\t", ((int8_t *)(tr->tr_data))[i]);
2931 }
2932 if (tr->tr_data)
2933 free(tr->tr_data, M_DEVBUF);
2934 twa_release_request(tr);
2935
2936 if (fetch_more_aens)
2937 twa_fetch_aen(sc);
2938 }
2939
2940 /*
2941 * Function name: twa_enqueue_aen
2942 * Description: Queues AEN's to be supplied to user-space tools on request.
2943 *
2944 * Input: sc -- ptr to per ctlr structure
2945 * cmd_hdr -- ptr to hdr of fw cmd pkt, from where the AEN
2946 * details can be retrieved.
2947 * Output: None
2948 * Return value: None
2949 */
2950 static uint16_t
2951 twa_enqueue_aen(struct twa_softc *sc, struct twa_command_header *cmd_hdr)
2952 {
2953 int rv, s;
2954 struct tw_cl_event_packet *event;
2955 uint16_t aen_code;
2956 unsigned long sync_time;
2957
2958 s = splbio();
2959 aen_code = cmd_hdr->status_block.error;
2960
2961 switch (aen_code) {
2962 case TWA_AEN_SYNC_TIME_WITH_HOST:
2963
2964 sync_time = (time_second - (3 * 86400)) % 604800;
2965 rv = twa_set_param(sc, TWA_PARAM_TIME_TABLE,
2966 TWA_PARAM_TIME_SchedulerTime, 4,
2967 &sync_time, twa_aen_callback);
2968 #ifdef DIAGNOSTIC
2969 if (rv != 0)
2970 printf("%s: unable to sync time with ctlr\n",
2971 sc->twa_dv.dv_xname);
2972 #endif
2973 break;
2974
2975 case TWA_AEN_QUEUE_EMPTY:
2976 break;
2977
2978 default:
2979 /* Queue the event. */
2980 event = sc->twa_aen_queue[sc->twa_aen_head];
2981 if (event->retrieved == TWA_AEN_NOT_RETRIEVED)
2982 sc->twa_aen_queue_overflow = TRUE;
2983 event->severity =
2984 cmd_hdr->status_block.substatus_block.severity;
2985 event->time_stamp_sec = time_second;
2986 event->aen_code = aen_code;
2987 event->retrieved = TWA_AEN_NOT_RETRIEVED;
2988 event->sequence_id = ++(sc->twa_current_sequence_id);
2989 cmd_hdr->err_specific_desc[sizeof(cmd_hdr->err_specific_desc) - 1] = '\0';
2990 event->parameter_len = strlen(cmd_hdr->err_specific_desc);
2991 memcpy(event->parameter_data, cmd_hdr->err_specific_desc,
2992 event->parameter_len);
2993
2994 if (event->severity < TWA_AEN_SEVERITY_DEBUG) {
2995 printf("%s: AEN 0x%04X: %s: %s: %s\n",
2996 sc->twa_dv.dv_xname,
2997 aen_code,
2998 twa_aen_severity_table[event->severity],
2999 twa_find_msg_string(twa_aen_table, aen_code),
3000 event->parameter_data);
3001 }
3002
3003 if ((sc->twa_aen_head + 1) == TWA_Q_LENGTH)
3004 sc->twa_aen_queue_wrapped = TRUE;
3005 sc->twa_aen_head = (sc->twa_aen_head + 1) % TWA_Q_LENGTH;
3006 break;
3007 } /* switch */
3008 splx(s);
3009
3010 return (aen_code);
3011 }
3012
3013 /*
3014 * Function name: twa_find_aen
3015 * Description: Reports whether a given AEN ever occurred.
3016 *
3017 * Input: sc -- ptr to per ctlr structure
3018 * aen_code-- AEN to look for
3019 * Output: None
3020 * Return value: 0 -- success
3021 * non-zero-- failure
3022 */
3023 static int
3024 twa_find_aen(struct twa_softc *sc, uint16_t aen_code)
3025 {
3026 uint32_t last_index;
3027 int s;
3028 int i;
3029
3030 s = splbio();
3031
3032 if (sc->twa_aen_queue_wrapped)
3033 last_index = sc->twa_aen_head;
3034 else
3035 last_index = 0;
3036
3037 i = sc->twa_aen_head;
3038 do {
3039 i = (i + TWA_Q_LENGTH - 1) % TWA_Q_LENGTH;
3040 if ((sc->twa_aen_queue[i])->aen_code == aen_code) {
3041 splx(s);
3042 return(0);
3043 }
3044 } while (i != last_index);
3045
3046 splx(s);
3047 return(1);
3048 }
3049
3050 static inline void
3051 twa_request_init(struct twa_request *tr, int flags)
3052 {
3053 tr->tr_data = NULL;
3054 tr->tr_real_data = NULL;
3055 tr->tr_length = 0;
3056 tr->tr_real_length = 0;
3057 tr->tr_status = TWA_CMD_SETUP;/* command is in setup phase */
3058 tr->tr_flags = flags;
3059 tr->tr_error = 0;
3060 tr->tr_callback = NULL;
3061 tr->tr_cmd_pkt_type = 0;
3062 tr->bp = 0;
3063
3064 /*
3065 * Look at the status field in the command packet to see how
3066 * it completed the last time it was used, and zero out only
3067 * the portions that might have changed. Note that we don't
3068 * care to zero out the sglist.
3069 */
3070 if (tr->tr_command->command.cmd_pkt_9k.status)
3071 memset(tr->tr_command, 0,
3072 sizeof(struct twa_command_header) + 28);
3073 else
3074 memset(&(tr->tr_command->command), 0, 28);
3075 }
3076
3077 struct twa_request *
3078 twa_get_request_wait(struct twa_softc *sc, int flags)
3079 {
3080 struct twa_request *tr;
3081 int s;
3082
3083 KASSERT((flags & TWA_CMD_AEN) == 0);
3084
3085 s = splbio();
3086 while ((tr = TAILQ_FIRST(&sc->twa_free)) == NULL) {
3087 sc->twa_sc_flags |= TWA_STATE_REQUEST_WAIT;
3088 (void) tsleep(&sc->twa_free, PRIBIO, "twaccb", hz);
3089 }
3090 TAILQ_REMOVE(&sc->twa_free, tr, tr_link);
3091
3092 splx(s);
3093
3094 twa_request_init(tr, flags);
3095
3096 return(tr);
3097 }
3098
3099 struct twa_request *
3100 twa_get_request(struct twa_softc *sc, int flags)
3101 {
3102 int s;
3103 struct twa_request *tr;
3104
3105 /* Get a free request packet. */
3106 s = splbio();
3107 if (__predict_false((flags & TWA_CMD_AEN) != 0)) {
3108
3109 if ((sc->sc_twa_request->tr_flags & TWA_CMD_AEN_BUSY) == 0) {
3110 tr = sc->sc_twa_request;
3111 flags |= TWA_CMD_AEN_BUSY;
3112 } else {
3113 splx(s);
3114 return (NULL);
3115 }
3116 } else {
3117 if (__predict_false((tr =
3118 TAILQ_FIRST(&sc->twa_free)) == NULL)) {
3119 splx(s);
3120 return (NULL);
3121 }
3122 TAILQ_REMOVE(&sc->twa_free, tr, tr_link);
3123 }
3124 splx(s);
3125
3126 twa_request_init(tr, flags);
3127
3128 return(tr);
3129 }
3130
3131 /*
3132 * Print some information about the controller
3133 */
3134 static void
3135 twa_describe_controller(struct twa_softc *sc)
3136 {
3137 struct twa_param_9k *p[10];
3138 int i, rv = 0;
3139 uint32_t dsize;
3140 uint8_t ports;
3141
3142 memset(p, sizeof(struct twa_param_9k *), 10);
3143
3144 /* Get the port count. */
3145 rv |= twa_get_param(sc, TWA_PARAM_CONTROLLER,
3146 TWA_PARAM_CONTROLLER_PortCount, 1, NULL, &p[0]);
3147
3148 /* get version strings */
3149 rv |= twa_get_param(sc, TWA_PARAM_VERSION, TWA_PARAM_VERSION_FW,
3150 16, NULL, &p[1]);
3151 rv |= twa_get_param(sc, TWA_PARAM_VERSION, TWA_PARAM_VERSION_BIOS,
3152 16, NULL, &p[2]);
3153 rv |= twa_get_param(sc, TWA_PARAM_VERSION, TWA_PARAM_VERSION_Mon,
3154 16, NULL, &p[3]);
3155 rv |= twa_get_param(sc, TWA_PARAM_VERSION, TWA_PARAM_VERSION_PCBA,
3156 8, NULL, &p[4]);
3157 rv |= twa_get_param(sc, TWA_PARAM_VERSION, TWA_PARAM_VERSION_ATA,
3158 8, NULL, &p[5]);
3159 rv |= twa_get_param(sc, TWA_PARAM_VERSION, TWA_PARAM_VERSION_PCI,
3160 8, NULL, &p[6]);
3161 rv |= twa_get_param(sc, TWA_PARAM_DRIVESUMMARY, TWA_PARAM_DRIVESTATUS,
3162 16, NULL, &p[7]);
3163
3164 if (rv) {
3165 /* some error occurred */
3166 aprint_error("%s: failed to fetch version information\n",
3167 sc->twa_dv.dv_xname);
3168 goto bail;
3169 }
3170
3171 ports = *(uint8_t *)(p[0]->data);
3172
3173 aprint_normal("%s: %d ports, Firmware %.16s, BIOS %.16s\n",
3174 sc->twa_dv.dv_xname, ports,
3175 p[1]->data, p[2]->data);
3176
3177 aprint_verbose("%s: Monitor %.16s, PCB %.8s, Achip %.8s, Pchip %.8s\n",
3178 sc->twa_dv.dv_xname,
3179 p[3]->data, p[4]->data,
3180 p[5]->data, p[6]->data);
3181
3182 for (i = 0; i < ports; i++) {
3183
3184 if ((*((char *)(p[7]->data + i)) & TWA_DRIVE_DETECTED) == 0)
3185 continue;
3186
3187 rv = twa_get_param(sc, TWA_PARAM_DRIVE_TABLE + i,
3188 TWA_PARAM_DRIVEMODELINDEX,
3189 TWA_PARAM_DRIVEMODEL_LENGTH, NULL, &p[8]);
3190
3191 if (rv != 0) {
3192 aprint_error("%s: unable to get drive model for port"
3193 " %d\n", sc->twa_dv.dv_xname, i);
3194 continue;
3195 }
3196
3197 rv = twa_get_param(sc, TWA_PARAM_DRIVE_TABLE + i,
3198 TWA_PARAM_DRIVESIZEINDEX,
3199 TWA_PARAM_DRIVESIZE_LENGTH, NULL, &p[9]);
3200
3201 if (rv != 0) {
3202 aprint_error("%s: unable to get drive size"
3203 " for port %d\n", sc->twa_dv.dv_xname,
3204 i);
3205 free(p[8], M_DEVBUF);
3206 continue;
3207 }
3208
3209 dsize = *(uint32_t *)(p[9]->data);
3210
3211 aprint_verbose("%s: port %d: %.40s %d MB\n",
3212 sc->twa_dv.dv_xname, i, p[8]->data, dsize / 2048);
3213
3214 if (p[8])
3215 free(p[8], M_DEVBUF);
3216 if (p[9])
3217 free(p[9], M_DEVBUF);
3218 }
3219 bail:
3220 if (p[0])
3221 free(p[0], M_DEVBUF);
3222 if (p[1])
3223 free(p[1], M_DEVBUF);
3224 if (p[2])
3225 free(p[2], M_DEVBUF);
3226 if (p[3])
3227 free(p[3], M_DEVBUF);
3228 if (p[4])
3229 free(p[4], M_DEVBUF);
3230 if (p[5])
3231 free(p[5], M_DEVBUF);
3232 if (p[6])
3233 free(p[6], M_DEVBUF);
3234 }
3235
3236 /*
3237 * Function name: twa_check_ctlr_state
3238 * Description: Makes sure that the fw status register reports a
3239 * proper status.
3240 *
3241 * Input: sc -- ptr to per ctlr structure
3242 * status_reg -- value in the status register
3243 * Output: None
3244 * Return value: 0 -- no errors
3245 * non-zero-- errors
3246 */
3247 static int
3248 twa_check_ctlr_state(struct twa_softc *sc, uint32_t status_reg)
3249 {
3250 int result = 0;
3251 struct timeval t1;
3252 static time_t last_warning[2] = {0, 0};
3253
3254 /* Check if the 'micro-controller ready' bit is not set. */
3255 if ((status_reg & TWA_STATUS_EXPECTED_BITS) !=
3256 TWA_STATUS_EXPECTED_BITS) {
3257
3258 microtime(&t1);
3259
3260 last_warning[0] += (5 * 1000 * 100);
3261
3262 if (t1.tv_usec > last_warning[0]) {
3263 microtime(&t1);
3264 last_warning[0] = t1.tv_usec;
3265 }
3266 result = 1;
3267 }
3268
3269 /* Check if any error bits are set. */
3270 if ((status_reg & TWA_STATUS_UNEXPECTED_BITS) != 0) {
3271
3272 microtime(&t1);
3273 last_warning[1] += (5 * 1000 * 100);
3274 if (t1.tv_usec > last_warning[1]) {
3275 microtime(&t1);
3276 last_warning[1] = t1.tv_usec;
3277 }
3278 if (status_reg & TWA_STATUS_PCI_PARITY_ERROR_INTERRUPT) {
3279 aprint_error("%s: clearing PCI parity error "
3280 "re-seat/move/replace card.\n",
3281 sc->twa_dv.dv_xname);
3282 twa_outl(sc, TWA_CONTROL_REGISTER_OFFSET,
3283 TWA_CONTROL_CLEAR_PARITY_ERROR);
3284 pci_conf_write(sc->pc, sc->tag,
3285 PCI_COMMAND_STATUS_REG,
3286 TWA_PCI_CONFIG_CLEAR_PARITY_ERROR);
3287 result = 1;
3288 }
3289 if (status_reg & TWA_STATUS_PCI_ABORT_INTERRUPT) {
3290 aprint_error("%s: clearing PCI abort\n",
3291 sc->twa_dv.dv_xname);
3292 twa_outl(sc, TWA_CONTROL_REGISTER_OFFSET,
3293 TWA_CONTROL_CLEAR_PCI_ABORT);
3294 pci_conf_write(sc->pc, sc->tag,
3295 PCI_COMMAND_STATUS_REG,
3296 TWA_PCI_CONFIG_CLEAR_PCI_ABORT);
3297 result = 1;
3298 }
3299 if (status_reg & TWA_STATUS_QUEUE_ERROR_INTERRUPT) {
3300 aprint_error("%s: clearing controller queue error\n",
3301 sc->twa_dv.dv_xname);
3302 twa_outl(sc, TWA_CONTROL_REGISTER_OFFSET,
3303 TWA_CONTROL_CLEAR_PCI_ABORT);
3304 result = 1;
3305 }
3306 if (status_reg & TWA_STATUS_SBUF_WRITE_ERROR) {
3307 aprint_error("%s: clearing SBUF write error\n",
3308 sc->twa_dv.dv_xname);
3309 twa_outl(sc, TWA_CONTROL_REGISTER_OFFSET,
3310 TWA_CONTROL_CLEAR_SBUF_WRITE_ERROR);
3311 result = 1;
3312 }
3313 if (status_reg & TWA_STATUS_MICROCONTROLLER_ERROR) {
3314 aprint_error("%s: micro-controller error\n",
3315 sc->twa_dv.dv_xname);
3316 result = 1;
3317 }
3318 }
3319 return(result);
3320 }
3321