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