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