1 /*- 2 * Copyright (c) 2000 Michael Smith 3 * Copyright (c) 2003 Paul Saab 4 * Copyright (c) 2003 Vinod Kashyap 5 * Copyright (c) 2000 BSDi 6 * All rights reserved. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27 * SUCH DAMAGE. 28 * 29 * $FreeBSD: src/sys/dev/twe/twe.c,v 1.1.2.10 2004/06/11 18:57:31 vkashyap Exp $ 30 * $DragonFly: src/sys/dev/raid/twe/twe.c,v 1.12 2005/08/22 21:16:20 hmp Exp $ 31 */ 32 33 /* 34 * Driver for the 3ware Escalade family of IDE RAID controllers. 35 */ 36 37 #include <dev/raid/twe/twe_compat.h> 38 #include <dev/raid/twe/twereg.h> 39 #define TWE_DEFINE_TABLES 40 #include <dev/raid/twe/twe_tables.h> 41 #include <dev/raid/twe/tweio.h> 42 #include <dev/raid/twe/twevar.h> 43 44 /* 45 * Command submission. 46 */ 47 static int twe_get_param_1(struct twe_softc *sc, int table_id, int param_id, u_int8_t *result); 48 static int twe_get_param_2(struct twe_softc *sc, int table_id, int param_id, u_int16_t *result); 49 static int twe_get_param_4(struct twe_softc *sc, int table_id, int param_id, u_int32_t *result); 50 static void *twe_get_param(struct twe_softc *sc, int table_id, int parameter_id, size_t size, 51 void (* func)(struct twe_request *tr)); 52 #ifdef TWE_SHUTDOWN_NOTIFICATION 53 static int twe_set_param_1(struct twe_softc *sc, int table_id, int param_id, u_int8_t value); 54 #endif 55 #if 0 56 static int twe_set_param_2(struct twe_softc *sc, int table_id, int param_id, u_int16_t value); 57 static int twe_set_param_4(struct twe_softc *sc, int table_id, int param_id, u_int32_t value); 58 #endif 59 static int twe_set_param(struct twe_softc *sc, int table_id, int param_id, int param_size, 60 void *data); 61 static int twe_init_connection(struct twe_softc *sc, int mode); 62 static int twe_wait_request(struct twe_request *tr); 63 static int twe_immediate_request(struct twe_request *tr); 64 static void twe_completeio(struct twe_request *tr); 65 static void twe_reset(struct twe_softc *sc); 66 static int twe_add_unit(struct twe_softc *sc, int unit); 67 static int twe_del_unit(struct twe_softc *sc, int unit); 68 69 /* 70 * Command I/O to controller. 71 */ 72 static void twe_done(struct twe_softc *sc); 73 static void twe_complete(struct twe_softc *sc); 74 static int twe_wait_status(struct twe_softc *sc, u_int32_t status, int timeout); 75 static int twe_drain_response_queue(struct twe_softc *sc); 76 static int twe_check_bits(struct twe_softc *sc, u_int32_t status_reg); 77 static int twe_soft_reset(struct twe_softc *sc); 78 79 /* 80 * Interrupt handling. 81 */ 82 static void twe_host_intr(struct twe_softc *sc); 83 static void twe_attention_intr(struct twe_softc *sc); 84 static void twe_command_intr(struct twe_softc *sc); 85 86 /* 87 * Asynchronous event handling. 88 */ 89 static int twe_fetch_aen(struct twe_softc *sc); 90 static void twe_handle_aen(struct twe_request *tr); 91 static void twe_enqueue_aen(struct twe_softc *sc, u_int16_t aen); 92 static u_int16_t twe_dequeue_aen(struct twe_softc *sc); 93 static int twe_drain_aen_queue(struct twe_softc *sc); 94 static int twe_find_aen(struct twe_softc *sc, u_int16_t aen); 95 96 /* 97 * Command buffer management. 98 */ 99 static int twe_get_request(struct twe_softc *sc, struct twe_request **tr); 100 static void twe_release_request(struct twe_request *tr); 101 102 /* 103 * Debugging. 104 */ 105 static char *twe_format_aen(struct twe_softc *sc, u_int16_t aen); 106 static int twe_report_request(struct twe_request *tr); 107 static void twe_panic(struct twe_softc *sc, char *reason); 108 109 /******************************************************************************** 110 ******************************************************************************** 111 Public Interfaces 112 ******************************************************************************** 113 ********************************************************************************/ 114 115 /******************************************************************************** 116 * Initialise the controller, set up driver data structures. 117 */ 118 int 119 twe_setup(struct twe_softc *sc) 120 { 121 struct twe_request *tr; 122 u_int32_t status_reg; 123 int i; 124 125 debug_called(4); 126 127 /* 128 * Initialise request queues. 129 */ 130 twe_initq_free(sc); 131 twe_initq_bio(sc); 132 twe_initq_ready(sc); 133 twe_initq_busy(sc); 134 twe_initq_complete(sc); 135 sc->twe_wait_aen = -1; 136 137 /* 138 * Allocate request structures up front. 139 */ 140 for (i = 0; i < TWE_Q_LENGTH; i++) { 141 if ((tr = twe_allocate_request(sc)) == NULL) 142 return(ENOMEM); 143 /* 144 * Set global defaults that won't change. 145 */ 146 tr->tr_command.generic.host_id = sc->twe_host_id; /* controller-assigned host ID */ 147 tr->tr_command.generic.request_id = i; /* our index number */ 148 sc->twe_lookup[i] = tr; 149 150 /* 151 * Put command onto the freelist. 152 */ 153 twe_release_request(tr); 154 } 155 156 /* 157 * Check status register for errors, clear them. 158 */ 159 status_reg = TWE_STATUS(sc); 160 twe_check_bits(sc, status_reg); 161 162 /* 163 * Wait for the controller to come ready. 164 */ 165 if (twe_wait_status(sc, TWE_STATUS_MICROCONTROLLER_READY, 60)) { 166 twe_printf(sc, "microcontroller not ready\n"); 167 return(ENXIO); 168 } 169 170 /* 171 * Disable interrupts from the card. 172 */ 173 twe_disable_interrupts(sc); 174 175 /* 176 * Soft reset the controller, look for the AEN acknowledging the reset, 177 * check for errors, drain the response queue. 178 */ 179 for (i = 0; i < TWE_MAX_RESET_TRIES; i++) { 180 181 if (i > 0) 182 twe_printf(sc, "reset %d failed, trying again\n", i); 183 184 if (!twe_soft_reset(sc)) 185 break; /* reset process complete */ 186 } 187 /* did we give up? */ 188 if (i >= TWE_MAX_RESET_TRIES) { 189 twe_printf(sc, "can't initialise controller, giving up\n"); 190 return(ENXIO); 191 } 192 193 return(0); 194 } 195 196 static int 197 twe_add_unit(struct twe_softc *sc, int unit) 198 { 199 struct twe_drive *dr; 200 int table; 201 u_int16_t dsize; 202 TWE_Param *drives = NULL, *param = NULL; 203 TWE_Unit_Descriptor *ud; 204 int error = 0; 205 206 if (unit < 0 || unit > TWE_MAX_UNITS) 207 return (EINVAL); 208 /* 209 * The controller is in a safe state, so try to find drives attached to it. 210 */ 211 if ((drives = twe_get_param(sc, TWE_PARAM_UNITSUMMARY, TWE_PARAM_UNITSUMMARY_Status, 212 TWE_MAX_UNITS, NULL)) == NULL) { 213 twe_printf(sc, "can't detect attached units\n"); 214 return (EIO); 215 } 216 217 dr = &sc->twe_drive[unit]; 218 /* check that the drive is online */ 219 if (!(drives->data[unit] & TWE_PARAM_UNITSTATUS_Online)) { 220 error = ENXIO; 221 goto out; 222 } 223 224 table = TWE_PARAM_UNITINFO + unit; 225 226 if (twe_get_param_4(sc, table, TWE_PARAM_UNITINFO_Capacity, &dr->td_size)) { 227 twe_printf(sc, "error fetching capacity for unit %d\n", unit); 228 error = EIO; 229 goto out; 230 } 231 if (twe_get_param_1(sc, table, TWE_PARAM_UNITINFO_Status, &dr->td_state)) { 232 twe_printf(sc, "error fetching state for unit %d\n", unit); 233 error = EIO; 234 goto out; 235 } 236 if (twe_get_param_2(sc, table, TWE_PARAM_UNITINFO_DescriptorSize, &dsize)) { 237 twe_printf(sc, "error fetching descriptor size for unit %d\n", unit); 238 error = EIO; 239 goto out; 240 } 241 if ((param = twe_get_param(sc, table, TWE_PARAM_UNITINFO_Descriptor, dsize - 3, NULL)) == NULL) { 242 twe_printf(sc, "error fetching descriptor for unit %d\n", unit); 243 error = EIO; 244 goto out; 245 } 246 ud = (TWE_Unit_Descriptor *)param->data; 247 dr->td_type = ud->configuration; 248 249 /* build synthetic geometry as per controller internal rules */ 250 if (dr->td_size > 0x200000) { 251 dr->td_heads = 255; 252 dr->td_sectors = 63; 253 } else { 254 dr->td_heads = 64; 255 dr->td_sectors = 32; 256 } 257 dr->td_cylinders = dr->td_size / (dr->td_heads * dr->td_sectors); 258 dr->td_twe_unit = unit; 259 260 error = twe_attach_drive(sc, dr); 261 262 out: 263 if (param != NULL) 264 free(param, M_DEVBUF); 265 if (drives != NULL) 266 free(drives, M_DEVBUF); 267 return (error); 268 } 269 270 static int 271 twe_del_unit(struct twe_softc *sc, int unit) 272 { 273 int error; 274 275 if (unit < 0 || unit >= TWE_MAX_UNITS) 276 return (ENXIO); 277 278 if (sc->twe_drive[unit].td_disk == NULL) 279 return (ENXIO); 280 281 error = twe_detach_drive(sc, unit); 282 return (error); 283 } 284 285 /******************************************************************************** 286 * Locate disk devices and attach children to them. 287 */ 288 void 289 twe_init(struct twe_softc *sc) 290 { 291 int i; 292 293 /* 294 * Scan for drives 295 */ 296 for (i = 0; i < TWE_MAX_UNITS; i++) 297 twe_add_unit(sc, i); 298 299 /* 300 * Initialise connection with controller. 301 */ 302 twe_init_connection(sc, TWE_INIT_MESSAGE_CREDITS); 303 304 #ifdef TWE_SHUTDOWN_NOTIFICATION 305 /* 306 * Tell the controller we support shutdown notification. 307 */ 308 twe_set_param_1(sc, TWE_PARAM_FEATURES, TWE_PARAM_FEATURES_DriverShutdown, 1); 309 #endif 310 311 /* 312 * Mark controller up and ready to run. 313 */ 314 sc->twe_state &= ~TWE_STATE_SHUTDOWN; 315 316 /* 317 * Finally enable interrupts. 318 */ 319 twe_enable_interrupts(sc); 320 } 321 322 /******************************************************************************** 323 * Stop the controller 324 */ 325 void 326 twe_deinit(struct twe_softc *sc) 327 { 328 /* 329 * Mark the controller as shutting down, and disable any further interrupts. 330 */ 331 sc->twe_state |= TWE_STATE_SHUTDOWN; 332 twe_disable_interrupts(sc); 333 334 #ifdef TWE_SHUTDOWN_NOTIFICATION 335 /* 336 * Disconnect from the controller 337 */ 338 twe_init_connection(sc, TWE_SHUTDOWN_MESSAGE_CREDITS); 339 #endif 340 } 341 342 /******************************************************************************* 343 * Take an interrupt, or be poked by other code to look for interrupt-worthy 344 * status. 345 */ 346 void 347 twe_intr(struct twe_softc *sc) 348 { 349 u_int32_t status_reg; 350 351 debug_called(4); 352 353 /* 354 * Collect current interrupt status. 355 */ 356 status_reg = TWE_STATUS(sc); 357 twe_check_bits(sc, status_reg); 358 359 /* 360 * Dispatch based on interrupt status 361 */ 362 if (status_reg & TWE_STATUS_HOST_INTERRUPT) 363 twe_host_intr(sc); 364 if (status_reg & TWE_STATUS_ATTENTION_INTERRUPT) 365 twe_attention_intr(sc); 366 if (status_reg & TWE_STATUS_COMMAND_INTERRUPT) 367 twe_command_intr(sc); 368 if (status_reg & TWE_STATUS_RESPONSE_INTERRUPT) 369 twe_done(sc); 370 } 371 372 /******************************************************************************** 373 * Pull as much work off the softc's work queue as possible and give it to the 374 * controller. 375 */ 376 void 377 twe_startio(struct twe_softc *sc) 378 { 379 struct twe_request *tr; 380 TWE_Command *cmd; 381 twe_bio *bp; 382 int error; 383 384 debug_called(4); 385 386 if (sc->twe_state & (TWE_STATE_CTLR_BUSY | TWE_STATE_FRZN)) 387 return; 388 389 /* spin until something prevents us from doing any work */ 390 for (;;) { 391 392 /* try to get a command that's already ready to go */ 393 tr = twe_dequeue_ready(sc); 394 395 /* build a command from an outstanding bio */ 396 if (tr == NULL) { 397 398 /* get a command to handle the bio with */ 399 if (twe_get_request(sc, &tr)) 400 break; 401 402 /* see if there's work to be done */ 403 if ((bp = twe_dequeue_bio(sc)) == NULL) { 404 twe_release_request(tr); 405 break; 406 } 407 408 /* connect the bio to the command */ 409 tr->tr_complete = twe_completeio; 410 tr->tr_private = bp; 411 tr->tr_data = TWE_BIO_DATA(bp); 412 tr->tr_length = TWE_BIO_LENGTH(bp); 413 cmd = &tr->tr_command; 414 if (TWE_BIO_IS_READ(bp)) { 415 tr->tr_flags |= TWE_CMD_DATAIN; 416 cmd->io.opcode = TWE_OP_READ; 417 } else { 418 tr->tr_flags |= TWE_CMD_DATAOUT; 419 cmd->io.opcode = TWE_OP_WRITE; 420 } 421 422 /* build a suitable I/O command (assumes 512-byte rounded transfers) */ 423 cmd->io.size = 3; 424 cmd->io.unit = TWE_BIO_UNIT(bp); 425 cmd->io.block_count = (tr->tr_length + TWE_BLOCK_SIZE - 1) / TWE_BLOCK_SIZE; 426 cmd->io.lba = TWE_BIO_LBA(bp); 427 } 428 429 /* did we find something to do? */ 430 if (tr == NULL) 431 break; 432 433 /* try to map and submit the command to controller */ 434 error = twe_map_request(tr); 435 436 if (error != 0) { 437 if (error == EBUSY) 438 break; 439 tr->tr_status = TWE_CMD_ERROR; 440 if (tr->tr_private != NULL) { 441 bp = (twe_bio *)(tr->tr_private); 442 TWE_BIO_SET_ERROR(bp, error); 443 tr->tr_private = NULL; 444 twed_intr(bp); 445 twe_release_request(tr); 446 } else if (tr->tr_flags & TWE_CMD_SLEEPER) 447 wakeup_one(tr); /* wakeup the sleeping owner */ 448 } 449 } 450 } 451 452 /******************************************************************************** 453 * Write blocks from memory to disk, for system crash dumps. 454 */ 455 int 456 twe_dump_blocks(struct twe_softc *sc, int unit, u_int32_t lba, void *data, int nblks) 457 { 458 struct twe_request *tr; 459 TWE_Command *cmd; 460 int error; 461 462 if (twe_get_request(sc, &tr)) 463 return(ENOMEM); 464 465 tr->tr_data = data; 466 tr->tr_status = TWE_CMD_SETUP; 467 tr->tr_length = nblks * TWE_BLOCK_SIZE; 468 tr->tr_flags = TWE_CMD_DATAOUT; 469 470 cmd = &tr->tr_command; 471 cmd->io.opcode = TWE_OP_WRITE; 472 cmd->io.size = 3; 473 cmd->io.unit = unit; 474 cmd->io.block_count = nblks; 475 cmd->io.lba = lba; 476 477 error = twe_immediate_request(tr); 478 if (error == 0) 479 if (twe_report_request(tr)) 480 error = EIO; 481 twe_release_request(tr); 482 return(error); 483 } 484 485 /******************************************************************************** 486 * Handle controller-specific control operations. 487 */ 488 int 489 twe_ioctl(struct twe_softc *sc, int cmd, void *addr) 490 { 491 struct twe_usercommand *tu = (struct twe_usercommand *)addr; 492 struct twe_paramcommand *tp = (struct twe_paramcommand *)addr; 493 struct twe_drivecommand *td = (struct twe_drivecommand *)addr; 494 union twe_statrequest *ts = (union twe_statrequest *)addr; 495 TWE_Param *param; 496 void *data; 497 unsigned short *aen_code = (unsigned short *) addr; 498 struct twe_request *tr; 499 u_int8_t srid; 500 int error; 501 502 error = 0; 503 switch(cmd) { 504 /* handle a command from userspace */ 505 case TWEIO_COMMAND: 506 /* get a request */ 507 while (twe_get_request(sc, &tr)) 508 tsleep(sc, 0, "twioctl", hz); 509 510 /* 511 * Save the command's request ID, copy the user-supplied command in, 512 * restore the request ID. 513 */ 514 srid = tr->tr_command.generic.request_id; 515 bcopy(&tu->tu_command, &tr->tr_command, sizeof(TWE_Command)); 516 tr->tr_command.generic.request_id = srid; 517 518 /* 519 * if there's a data buffer, allocate and copy it in. 520 * Must be in multipled of 512 bytes. 521 */ 522 tr->tr_length = (tu->tu_size + 511) & ~511; 523 if (tr->tr_length > 0) { 524 if ((tr->tr_data = malloc(tr->tr_length, M_DEVBUF, M_WAITOK)) == NULL) { 525 error = ENOMEM; 526 goto cmd_done; 527 } 528 if ((error = copyin(tu->tu_data, tr->tr_data, tu->tu_size)) != 0) 529 goto cmd_done; 530 tr->tr_flags |= TWE_CMD_DATAIN | TWE_CMD_DATAOUT; 531 } 532 533 /* run the command */ 534 error = twe_wait_request(tr); 535 if (error) 536 goto cmd_done; 537 538 /* copy the command out again */ 539 bcopy(&tr->tr_command, &tu->tu_command, sizeof(TWE_Command)); 540 541 /* if there was a data buffer, copy it out */ 542 if (tr->tr_length > 0) 543 error = copyout(tr->tr_data, tu->tu_data, tu->tu_size); 544 545 cmd_done: 546 /* free resources */ 547 if (tr->tr_data != NULL) 548 free(tr->tr_data, M_DEVBUF); 549 if (tr != NULL) 550 twe_release_request(tr); 551 552 break; 553 554 /* fetch statistics counter */ 555 case TWEIO_STATS: 556 switch (ts->ts_item) { 557 #ifdef TWE_PERFORMANCE_MONITOR 558 case TWEQ_FREE: 559 case TWEQ_BIO: 560 case TWEQ_READY: 561 case TWEQ_BUSY: 562 case TWEQ_COMPLETE: 563 bcopy(&sc->twe_qstat[ts->ts_item], &ts->ts_qstat, sizeof(struct twe_qstat)); 564 break; 565 #endif 566 default: 567 error = ENOENT; 568 break; 569 } 570 break; 571 572 /* poll for an AEN */ 573 case TWEIO_AEN_POLL: 574 *aen_code = twe_dequeue_aen(sc); 575 break; 576 577 /* wait for another AEN to show up */ 578 case TWEIO_AEN_WAIT: 579 crit_enter(); 580 while ((*aen_code = twe_dequeue_aen(sc)) == TWE_AEN_QUEUE_EMPTY) { 581 error = tsleep(&sc->twe_aen_queue, PCATCH, "tweaen", 0); 582 if (error == EINTR) 583 break; 584 } 585 crit_exit(); 586 break; 587 588 case TWEIO_GET_PARAM: 589 if ((param = twe_get_param(sc, tp->tp_table_id, tp->tp_param_id, tp->tp_size, NULL)) == NULL) { 590 twe_printf(sc, "TWEIO_GET_PARAM failed for 0x%x/0x%x/%d\n", 591 tp->tp_table_id, tp->tp_param_id, tp->tp_size); 592 error = EINVAL; 593 } else { 594 if (param->parameter_size_bytes > tp->tp_size) { 595 twe_printf(sc, "TWEIO_GET_PARAM parameter too large (%d > %d)\n", 596 param->parameter_size_bytes, tp->tp_size); 597 error = EFAULT; 598 } else { 599 error = copyout(param->data, tp->tp_data, param->parameter_size_bytes); 600 } 601 free(param, M_DEVBUF); 602 } 603 break; 604 605 case TWEIO_SET_PARAM: 606 if ((data = malloc(tp->tp_size, M_DEVBUF, M_WAITOK)) == NULL) { 607 error = ENOMEM; 608 } else { 609 error = copyin(tp->tp_data, data, tp->tp_size); 610 if (error == 0) 611 error = twe_set_param(sc, tp->tp_table_id, tp->tp_param_id, tp->tp_size, data); 612 free(data, M_DEVBUF); 613 } 614 break; 615 616 case TWEIO_RESET: 617 twe_reset(sc); 618 break; 619 620 case TWEIO_ADD_UNIT: 621 error = twe_add_unit(sc, td->td_unit); 622 break; 623 624 case TWEIO_DEL_UNIT: 625 error = twe_del_unit(sc, td->td_unit); 626 break; 627 628 /* XXX implement ATA PASSTHROUGH */ 629 630 /* nothing we understand */ 631 default: 632 error = ENOTTY; 633 } 634 635 return(error); 636 } 637 638 /******************************************************************************** 639 * Enable the useful interrupts from the controller. 640 */ 641 void 642 twe_enable_interrupts(struct twe_softc *sc) 643 { 644 sc->twe_state |= TWE_STATE_INTEN; 645 TWE_CONTROL(sc, 646 TWE_CONTROL_CLEAR_ATTENTION_INTERRUPT | 647 TWE_CONTROL_UNMASK_RESPONSE_INTERRUPT | 648 TWE_CONTROL_ENABLE_INTERRUPTS); 649 } 650 651 /******************************************************************************** 652 * Disable interrupts from the controller. 653 */ 654 void 655 twe_disable_interrupts(struct twe_softc *sc) 656 { 657 TWE_CONTROL(sc, TWE_CONTROL_DISABLE_INTERRUPTS); 658 sc->twe_state &= ~TWE_STATE_INTEN; 659 } 660 661 /******************************************************************************** 662 ******************************************************************************** 663 Command Submission 664 ******************************************************************************** 665 ********************************************************************************/ 666 667 /******************************************************************************** 668 * Read integer parameter table entries. 669 */ 670 static int 671 twe_get_param_1(struct twe_softc *sc, int table_id, int param_id, u_int8_t *result) 672 { 673 TWE_Param *param; 674 675 if ((param = twe_get_param(sc, table_id, param_id, 1, NULL)) == NULL) 676 return(ENOENT); 677 *result = *(u_int8_t *)param->data; 678 free(param, M_DEVBUF); 679 return(0); 680 } 681 682 static int 683 twe_get_param_2(struct twe_softc *sc, int table_id, int param_id, u_int16_t *result) 684 { 685 TWE_Param *param; 686 687 if ((param = twe_get_param(sc, table_id, param_id, 2, NULL)) == NULL) 688 return(ENOENT); 689 *result = *(u_int16_t *)param->data; 690 free(param, M_DEVBUF); 691 return(0); 692 } 693 694 static int 695 twe_get_param_4(struct twe_softc *sc, int table_id, int param_id, u_int32_t *result) 696 { 697 TWE_Param *param; 698 699 if ((param = twe_get_param(sc, table_id, param_id, 4, NULL)) == NULL) 700 return(ENOENT); 701 *result = *(u_int32_t *)param->data; 702 free(param, M_DEVBUF); 703 return(0); 704 } 705 706 /******************************************************************************** 707 * Perform a TWE_OP_GET_PARAM command. If a callback function is provided, it 708 * will be called with the command when it's completed. If no callback is 709 * provided, we will wait for the command to complete and then return just the data. 710 * The caller is responsible for freeing the data when done with it. 711 */ 712 static void * 713 twe_get_param(struct twe_softc *sc, int table_id, int param_id, size_t param_size, 714 void (* func)(struct twe_request *tr)) 715 { 716 struct twe_request *tr; 717 TWE_Command *cmd; 718 TWE_Param *param; 719 int error; 720 721 debug_called(4); 722 723 tr = NULL; 724 param = NULL; 725 726 /* get a command */ 727 if (twe_get_request(sc, &tr)) 728 goto err; 729 730 /* get a buffer */ 731 param = (TWE_Param *)malloc(TWE_SECTOR_SIZE, M_DEVBUF, M_INTWAIT); 732 tr->tr_data = param; 733 tr->tr_length = TWE_SECTOR_SIZE; 734 tr->tr_flags = TWE_CMD_DATAIN | TWE_CMD_DATAOUT; 735 736 /* build the command for the controller */ 737 cmd = &tr->tr_command; 738 cmd->param.opcode = TWE_OP_GET_PARAM; 739 cmd->param.size = 2; 740 cmd->param.unit = 0; 741 cmd->param.param_count = 1; 742 743 /* fill in the outbound parameter data */ 744 param->table_id = table_id; 745 param->parameter_id = param_id; 746 param->parameter_size_bytes = param_size; 747 748 /* submit the command and either wait or let the callback handle it */ 749 if (func == NULL) { 750 /* XXX could use twe_wait_request here if interrupts were enabled? */ 751 error = twe_immediate_request(tr); 752 if (error == 0) { 753 if (twe_report_request(tr)) 754 goto err; 755 } 756 else 757 goto err; 758 twe_release_request(tr); 759 return(param); 760 } else { 761 tr->tr_complete = func; 762 error = twe_map_request(tr); 763 if ((error == 0) || (error == EBUSY)) 764 return(func); 765 } 766 767 /* something failed */ 768 err: 769 debug(1, "failed"); 770 if (tr != NULL) 771 twe_release_request(tr); 772 if (param != NULL) 773 free(param, M_DEVBUF); 774 return(NULL); 775 } 776 777 /******************************************************************************** 778 * Set integer parameter table entries. 779 */ 780 #ifdef TWE_SHUTDOWN_NOTIFICATION 781 static int 782 twe_set_param_1(struct twe_softc *sc, int table_id, int param_id, u_int8_t value) 783 { 784 return(twe_set_param(sc, table_id, param_id, sizeof(value), &value)); 785 } 786 #endif 787 788 #if 0 789 static int 790 twe_set_param_2(struct twe_softc *sc, int table_id, int param_id, u_int16_t value) 791 { 792 return(twe_set_param(sc, table_id, param_id, sizeof(value), &value)); 793 } 794 795 static int 796 twe_set_param_4(struct twe_softc *sc, int table_id, int param_id, u_int32_t value) 797 { 798 return(twe_set_param(sc, table_id, param_id, sizeof(value), &value)); 799 } 800 #endif 801 802 /******************************************************************************** 803 * Perform a TWE_OP_SET_PARAM command, returns nonzero on error. 804 */ 805 static int 806 twe_set_param(struct twe_softc *sc, int table_id, int param_id, int param_size, void *data) 807 { 808 struct twe_request *tr; 809 TWE_Command *cmd; 810 TWE_Param *param; 811 int error; 812 813 debug_called(4); 814 815 tr = NULL; 816 param = NULL; 817 error = ENOMEM; 818 819 /* get a command */ 820 if (twe_get_request(sc, &tr)) 821 goto out; 822 823 /* get a buffer */ 824 param = (TWE_Param *)malloc(TWE_SECTOR_SIZE, M_DEVBUF, M_INTWAIT); 825 tr->tr_data = param; 826 tr->tr_length = TWE_SECTOR_SIZE; 827 tr->tr_flags = TWE_CMD_DATAIN | TWE_CMD_DATAOUT; 828 829 /* build the command for the controller */ 830 cmd = &tr->tr_command; 831 cmd->param.opcode = TWE_OP_SET_PARAM; 832 cmd->param.size = 2; 833 cmd->param.unit = 0; 834 cmd->param.param_count = 1; 835 836 /* fill in the outbound parameter data */ 837 param->table_id = table_id; 838 param->parameter_id = param_id; 839 param->parameter_size_bytes = param_size; 840 bcopy(data, param->data, param_size); 841 842 /* XXX could use twe_wait_request here if interrupts were enabled? */ 843 error = twe_immediate_request(tr); 844 if (error == 0) { 845 if (twe_report_request(tr)) 846 error = EIO; 847 } 848 849 out: 850 if (tr != NULL) 851 twe_release_request(tr); 852 if (param != NULL) 853 free(param, M_DEVBUF); 854 return(error); 855 } 856 857 /******************************************************************************** 858 * Perform a TWE_OP_INIT_CONNECTION command, returns nonzero on error. 859 * 860 * Typically called with interrupts disabled. 861 */ 862 static int 863 twe_init_connection(struct twe_softc *sc, int mode) 864 { 865 struct twe_request *tr; 866 TWE_Command *cmd; 867 int error; 868 869 debug_called(4); 870 871 /* get a command */ 872 if (twe_get_request(sc, &tr)) 873 return(NULL); 874 875 /* build the command */ 876 cmd = &tr->tr_command; 877 cmd->initconnection.opcode = TWE_OP_INIT_CONNECTION; 878 cmd->initconnection.size = 3; 879 cmd->initconnection.host_id = 0; 880 cmd->initconnection.message_credits = mode; 881 cmd->initconnection.response_queue_pointer = 0; 882 883 /* submit the command */ 884 error = twe_immediate_request(tr); 885 twe_release_request(tr); 886 887 if (mode == TWE_INIT_MESSAGE_CREDITS) 888 sc->twe_host_id = cmd->initconnection.host_id; 889 return(error); 890 } 891 892 /******************************************************************************** 893 * Start the command (tr) and sleep waiting for it to complete. 894 * 895 * Successfully completed commands are dequeued. 896 */ 897 static int 898 twe_wait_request(struct twe_request *tr) 899 { 900 debug_called(4); 901 902 tr->tr_flags |= TWE_CMD_SLEEPER; 903 tr->tr_status = TWE_CMD_BUSY; 904 twe_enqueue_ready(tr); 905 twe_startio(tr->tr_sc); 906 crit_enter(); 907 while (tr->tr_status == TWE_CMD_BUSY) 908 tsleep(tr, 0, "twewait", 0); 909 crit_exit(); 910 911 return(tr->tr_status != TWE_CMD_COMPLETE); 912 } 913 914 /******************************************************************************** 915 * Start the command (tr) and busy-wait for it to complete. 916 * This should only be used when interrupts are actually disabled (although it 917 * will work if they are not). 918 */ 919 static int 920 twe_immediate_request(struct twe_request *tr) 921 { 922 int error; 923 924 debug_called(4); 925 926 tr->tr_status = TWE_CMD_BUSY; 927 if ((error = twe_map_request(tr)) != 0) 928 if (error != EBUSY) 929 return(error); 930 while (tr->tr_status == TWE_CMD_BUSY){ 931 twe_done(tr->tr_sc); 932 } 933 return(tr->tr_status != TWE_CMD_COMPLETE); 934 } 935 936 /******************************************************************************** 937 * Handle completion of an I/O command. 938 */ 939 static void 940 twe_completeio(struct twe_request *tr) 941 { 942 struct twe_softc *sc = tr->tr_sc; 943 twe_bio *bp = (twe_bio *)tr->tr_private; 944 945 debug_called(4); 946 947 if (tr->tr_status == TWE_CMD_COMPLETE) { 948 if (tr->tr_command.generic.status) 949 if (twe_report_request(tr)) 950 TWE_BIO_SET_ERROR(bp, EIO); 951 } else { 952 twe_panic(sc, "twe_completeio on incomplete command"); 953 } 954 tr->tr_private = NULL; 955 twed_intr(bp); 956 twe_release_request(tr); 957 } 958 959 /******************************************************************************** 960 * Reset the controller and pull all the active commands back onto the ready 961 * queue. Used to restart a controller that's exhibiting bad behaviour. 962 */ 963 static void 964 twe_reset(struct twe_softc *sc) 965 { 966 struct twe_request *tr; 967 int i; 968 969 /* 970 * Sleep for a short period to allow AENs to be signalled. 971 */ 972 tsleep(sc, 0, "twereset", hz); 973 974 /* 975 * Disable interrupts from the controller, and mask any accidental entry 976 * into our interrupt handler. 977 */ 978 twe_printf(sc, "controller reset in progress...\n"); 979 twe_disable_interrupts(sc); 980 crit_enter(); 981 982 /* 983 * Try to soft-reset the controller. 984 */ 985 for (i = 0; i < TWE_MAX_RESET_TRIES; i++) { 986 987 if (i > 0) 988 twe_printf(sc, "reset %d failed, trying again\n", i); 989 990 if (!twe_soft_reset(sc)) 991 break; /* reset process complete */ 992 } 993 /* did we give up? */ 994 if (i >= TWE_MAX_RESET_TRIES) { 995 twe_printf(sc, "can't reset controller, giving up\n"); 996 goto out; 997 } 998 999 /* 1000 * Move all of the commands that were busy back to the ready queue. 1001 */ 1002 i = 0; 1003 while ((tr = twe_dequeue_busy(sc)) != NULL) { 1004 twe_enqueue_ready(tr); 1005 i++; 1006 } 1007 1008 /* 1009 * Kick the controller to start things going again, then re-enable interrupts. 1010 */ 1011 twe_startio(sc); 1012 twe_enable_interrupts(sc); 1013 twe_printf(sc, "controller reset done, %d commands restarted\n", i); 1014 1015 out: 1016 crit_exit(); 1017 twe_enable_interrupts(sc); 1018 } 1019 1020 /******************************************************************************** 1021 ******************************************************************************** 1022 Command I/O to Controller 1023 ******************************************************************************** 1024 ********************************************************************************/ 1025 1026 /******************************************************************************** 1027 * Try to deliver (tr) to the controller. 1028 * 1029 * Can be called at any interrupt level, with or without interrupts enabled. 1030 */ 1031 int 1032 twe_start(struct twe_request *tr) 1033 { 1034 struct twe_softc *sc = tr->tr_sc; 1035 int i, done; 1036 u_int32_t status_reg; 1037 1038 debug_called(4); 1039 1040 /* mark the command as currently being processed */ 1041 tr->tr_status = TWE_CMD_BUSY; 1042 1043 /* 1044 * Spin briefly waiting for the controller to come ready 1045 * 1046 * XXX it might be more efficient to return EBUSY immediately 1047 * and let the command be rescheduled. 1048 */ 1049 for (i = 100000, done = 0; (i > 0) && !done; i--) { 1050 crit_enter(); 1051 1052 /* check to see if we can post a command */ 1053 status_reg = TWE_STATUS(sc); 1054 twe_check_bits(sc, status_reg); 1055 1056 if (!(status_reg & TWE_STATUS_COMMAND_QUEUE_FULL)) { 1057 /* move command to work queue */ 1058 twe_enqueue_busy(tr); 1059 TWE_COMMAND_QUEUE(sc, tr->tr_cmdphys); 1060 done = 1; 1061 #ifdef TWE_DEBUG 1062 if (tr->tr_complete != NULL) { 1063 debug(3, "queued request %d with callback %p", tr->tr_command.generic.request_id, tr->tr_complete); 1064 } else if (tr->tr_flags & TWE_CMD_SLEEPER) { 1065 debug(3, "queued request %d with wait channel %p", tr->tr_command.generic.request_id, tr); 1066 } else { 1067 debug(3, "queued request %d for polling caller", tr->tr_command.generic.request_id); 1068 } 1069 #endif 1070 } 1071 crit_exit(); /* drop critical section to allow completion interrupts */ 1072 } 1073 1074 /* command is enqueued */ 1075 if (done) 1076 return(0); 1077 1078 /* 1079 * We couldn't get the controller to take the command; try submitting it again later. 1080 * This should only happen if something is wrong with the controller, or if we have 1081 * overestimated the number of commands it can accept. (Should we actually reject 1082 * the command at this point?) 1083 */ 1084 return(EBUSY); 1085 } 1086 1087 /******************************************************************************** 1088 * Poll the controller (sc) for completed commands. 1089 * 1090 * Can be called at any interrupt level, with or without interrupts enabled. 1091 */ 1092 static void 1093 twe_done(struct twe_softc *sc) 1094 { 1095 TWE_Response_Queue rq; 1096 struct twe_request *tr; 1097 int found; 1098 u_int32_t status_reg; 1099 1100 debug_called(5); 1101 1102 /* loop collecting completed commands */ 1103 found = 0; 1104 crit_enter(); 1105 for (;;) { 1106 status_reg = TWE_STATUS(sc); 1107 twe_check_bits(sc, status_reg); /* XXX should this fail? */ 1108 1109 if (!(status_reg & TWE_STATUS_RESPONSE_QUEUE_EMPTY)) { 1110 found = 1; 1111 rq = TWE_RESPONSE_QUEUE(sc); 1112 tr = sc->twe_lookup[rq.u.response_id]; /* find command */ 1113 if (tr->tr_status != TWE_CMD_BUSY) 1114 twe_printf(sc, "completion event for nonbusy command\n"); 1115 tr->tr_status = TWE_CMD_COMPLETE; 1116 debug(3, "completed request id %d with status %d", 1117 tr->tr_command.generic.request_id, tr->tr_command.generic.status); 1118 /* move to completed queue */ 1119 twe_remove_busy(tr); 1120 twe_enqueue_complete(tr); 1121 sc->twe_state &= ~TWE_STATE_CTLR_BUSY; 1122 } else { 1123 break; /* no response ready */ 1124 } 1125 } 1126 crit_exit(); 1127 1128 /* if we've completed any commands, try posting some more */ 1129 if (found) 1130 twe_startio(sc); 1131 1132 /* handle completion and timeouts */ 1133 twe_complete(sc); /* XXX use deferred completion? */ 1134 } 1135 1136 /******************************************************************************** 1137 * Perform post-completion processing for commands on (sc). 1138 * 1139 * This is split from twe_done as it can be safely deferred and run at a lower 1140 * priority level should facilities for such a thing become available. 1141 */ 1142 static void 1143 twe_complete(struct twe_softc *sc) 1144 { 1145 struct twe_request *tr; 1146 1147 debug_called(5); 1148 1149 /* 1150 * Pull commands off the completed list, dispatch them appropriately 1151 */ 1152 while ((tr = twe_dequeue_complete(sc)) != NULL) { 1153 1154 /* unmap the command's data buffer */ 1155 twe_unmap_request(tr); 1156 1157 /* dispatch to suit command originator */ 1158 if (tr->tr_complete != NULL) { /* completion callback */ 1159 debug(2, "call completion handler %p", tr->tr_complete); 1160 tr->tr_complete(tr); 1161 1162 } else if (tr->tr_flags & TWE_CMD_SLEEPER) { /* caller is asleep waiting */ 1163 debug(2, "wake up command owner on %p", tr); 1164 wakeup_one(tr); 1165 1166 } else { /* caller is polling command */ 1167 debug(2, "command left for owner"); 1168 } 1169 } 1170 } 1171 1172 /******************************************************************************** 1173 * Wait for (status) to be set in the controller status register for up to 1174 * (timeout) seconds. Returns 0 if found, nonzero if we time out. 1175 * 1176 * Note: this busy-waits, rather than sleeping, since we may be called with 1177 * eg. clock interrupts masked. 1178 */ 1179 static int 1180 twe_wait_status(struct twe_softc *sc, u_int32_t status, int timeout) 1181 { 1182 time_t expiry; 1183 u_int32_t status_reg; 1184 1185 debug_called(4); 1186 1187 expiry = time_second + timeout; 1188 1189 do { 1190 status_reg = TWE_STATUS(sc); 1191 if (status_reg & status) /* got the required bit(s)? */ 1192 return(0); 1193 DELAY(100000); 1194 } while (time_second <= expiry); 1195 1196 return(1); 1197 } 1198 1199 /******************************************************************************** 1200 * Drain the response queue, which may contain responses to commands we know 1201 * nothing about. 1202 */ 1203 static int 1204 twe_drain_response_queue(struct twe_softc *sc) 1205 { 1206 TWE_Response_Queue rq; 1207 u_int32_t status_reg; 1208 1209 debug_called(4); 1210 1211 for (;;) { /* XXX give up eventually? */ 1212 status_reg = TWE_STATUS(sc); 1213 if (twe_check_bits(sc, status_reg)) 1214 return(1); 1215 if (status_reg & TWE_STATUS_RESPONSE_QUEUE_EMPTY) 1216 return(0); 1217 rq = TWE_RESPONSE_QUEUE(sc); 1218 } 1219 } 1220 1221 /******************************************************************************** 1222 * Soft-reset the controller 1223 */ 1224 static int 1225 twe_soft_reset(struct twe_softc *sc) 1226 { 1227 u_int32_t status_reg; 1228 1229 debug_called(2); 1230 1231 TWE_SOFT_RESET(sc); 1232 1233 if (twe_wait_status(sc, TWE_STATUS_ATTENTION_INTERRUPT, 30)) { 1234 twe_printf(sc, "no attention interrupt\n"); 1235 return(1); 1236 } 1237 TWE_CONTROL(sc, TWE_CONTROL_CLEAR_ATTENTION_INTERRUPT); 1238 if (twe_drain_aen_queue(sc)) { 1239 twe_printf(sc, "can't drain AEN queue\n"); 1240 return(1); 1241 } 1242 if (twe_find_aen(sc, TWE_AEN_SOFT_RESET)) { 1243 twe_printf(sc, "reset not reported\n"); 1244 return(1); 1245 } 1246 status_reg = TWE_STATUS(sc); 1247 if (TWE_STATUS_ERRORS(status_reg) || twe_check_bits(sc, status_reg)) { 1248 twe_printf(sc, "controller errors detected\n"); 1249 return(1); 1250 } 1251 if (twe_drain_response_queue(sc)) { 1252 twe_printf(sc, "can't drain response queue\n"); 1253 return(1); 1254 } 1255 return(0); 1256 } 1257 1258 /******************************************************************************** 1259 ******************************************************************************** 1260 Interrupt Handling 1261 ******************************************************************************** 1262 ********************************************************************************/ 1263 1264 /******************************************************************************** 1265 * Host interrupt. 1266 * 1267 * XXX what does this mean? 1268 */ 1269 static void 1270 twe_host_intr(struct twe_softc *sc) 1271 { 1272 debug_called(4); 1273 1274 twe_printf(sc, "host interrupt\n"); 1275 TWE_CONTROL(sc, TWE_CONTROL_CLEAR_HOST_INTERRUPT); 1276 } 1277 1278 /******************************************************************************** 1279 * Attention interrupt. 1280 * 1281 * Signalled when the controller has one or more AENs for us. 1282 */ 1283 static void 1284 twe_attention_intr(struct twe_softc *sc) 1285 { 1286 debug_called(4); 1287 1288 /* instigate a poll for AENs */ 1289 if (twe_fetch_aen(sc)) { 1290 twe_printf(sc, "error polling for signalled AEN\n"); 1291 } else { 1292 TWE_CONTROL(sc, TWE_CONTROL_CLEAR_ATTENTION_INTERRUPT); 1293 } 1294 } 1295 1296 /******************************************************************************** 1297 * Command interrupt. 1298 * 1299 * Signalled when the controller can handle more commands. 1300 */ 1301 static void 1302 twe_command_intr(struct twe_softc *sc) 1303 { 1304 debug_called(4); 1305 1306 /* 1307 * We don't use this, rather we try to submit commands when we receive 1308 * them, and when other commands have completed. Mask it so we don't get 1309 * another one. 1310 twe_printf(sc, "command interrupt\n"); 1311 */ 1312 TWE_CONTROL(sc, TWE_CONTROL_MASK_COMMAND_INTERRUPT); 1313 } 1314 1315 /******************************************************************************** 1316 ******************************************************************************** 1317 Asynchronous Event Handling 1318 ******************************************************************************** 1319 ********************************************************************************/ 1320 1321 /******************************************************************************** 1322 * Request an AEN from the controller. 1323 */ 1324 static int 1325 twe_fetch_aen(struct twe_softc *sc) 1326 { 1327 1328 debug_called(4); 1329 1330 if ((twe_get_param(sc, TWE_PARAM_AEN, TWE_PARAM_AEN_UnitCode, 2, twe_handle_aen)) == NULL) 1331 return(EIO); 1332 return(0); 1333 } 1334 1335 /******************************************************************************** 1336 * Handle an AEN returned by the controller. 1337 */ 1338 static void 1339 twe_handle_aen(struct twe_request *tr) 1340 { 1341 struct twe_softc *sc = tr->tr_sc; 1342 TWE_Param *param; 1343 u_int16_t aen; 1344 1345 debug_called(4); 1346 1347 /* XXX check for command success somehow? */ 1348 1349 param = (TWE_Param *)tr->tr_data; 1350 aen = *(u_int16_t *)(param->data); 1351 1352 free(tr->tr_data, M_DEVBUF); 1353 twe_release_request(tr); 1354 twe_enqueue_aen(sc, aen); 1355 1356 /* XXX poll for more AENs? */ 1357 } 1358 1359 /******************************************************************************** 1360 * Pull AENs out of the controller and park them in the queue, in a context where 1361 * interrupts aren't active. Return nonzero if we encounter any errors in the 1362 * process of obtaining all the available AENs. 1363 */ 1364 static int 1365 twe_drain_aen_queue(struct twe_softc *sc) 1366 { 1367 u_int16_t aen; 1368 1369 for (;;) { 1370 if (twe_get_param_2(sc, TWE_PARAM_AEN, TWE_PARAM_AEN_UnitCode, &aen)) 1371 return(1); 1372 if (aen == TWE_AEN_QUEUE_EMPTY) 1373 return(0); 1374 twe_enqueue_aen(sc, aen); 1375 } 1376 } 1377 1378 /******************************************************************************** 1379 * Push an AEN that we've received onto the queue. 1380 * 1381 * Note that we have to lock this against reentrance, since it may be called 1382 * from both interrupt and non-interrupt context. 1383 * 1384 * If someone is waiting for the AEN we have, wake them up. 1385 */ 1386 static void 1387 twe_enqueue_aen(struct twe_softc *sc, u_int16_t aen) 1388 { 1389 char *msg; 1390 int next, nextnext; 1391 1392 debug_called(4); 1393 1394 if ((msg = twe_format_aen(sc, aen)) != NULL) 1395 twe_printf(sc, "AEN: <%s>\n", msg); 1396 1397 crit_enter(); 1398 /* enqueue the AEN */ 1399 next = ((sc->twe_aen_head + 1) % TWE_Q_LENGTH); 1400 nextnext = ((sc->twe_aen_head + 2) % TWE_Q_LENGTH); 1401 1402 /* check to see if this is the last free slot, and subvert the AEN if it is */ 1403 if (nextnext == sc->twe_aen_tail) 1404 aen = TWE_AEN_QUEUE_FULL; 1405 1406 /* look to see if there's room for this AEN */ 1407 if (next != sc->twe_aen_tail) { 1408 sc->twe_aen_queue[sc->twe_aen_head] = aen; 1409 sc->twe_aen_head = next; 1410 } 1411 1412 /* wake up anyone asleep on the queue */ 1413 wakeup(&sc->twe_aen_queue); 1414 1415 /* anyone looking for this AEN? */ 1416 if (sc->twe_wait_aen == aen) { 1417 sc->twe_wait_aen = -1; 1418 wakeup(&sc->twe_wait_aen); 1419 } 1420 crit_exit(); 1421 } 1422 1423 /******************************************************************************** 1424 * Pop an AEN off the queue, or return -1 if there are none left. 1425 * 1426 * We are more or less interrupt-safe, so don't block interrupts. 1427 */ 1428 static u_int16_t 1429 twe_dequeue_aen(struct twe_softc *sc) 1430 { 1431 u_int16_t result; 1432 1433 debug_called(4); 1434 1435 if (sc->twe_aen_tail == sc->twe_aen_head) { 1436 result = TWE_AEN_QUEUE_EMPTY; 1437 } else { 1438 result = sc->twe_aen_queue[sc->twe_aen_tail]; 1439 sc->twe_aen_tail = ((sc->twe_aen_tail + 1) % TWE_Q_LENGTH); 1440 } 1441 return(result); 1442 } 1443 1444 /******************************************************************************** 1445 * Check to see if the requested AEN is in the queue. 1446 * 1447 * XXX we could probably avoid masking interrupts here 1448 */ 1449 static int 1450 twe_find_aen(struct twe_softc *sc, u_int16_t aen) 1451 { 1452 int i, missing; 1453 1454 missing = 1; 1455 crit_enter(); 1456 for (i = sc->twe_aen_tail; (i != sc->twe_aen_head) && missing; i = (i + 1) % TWE_Q_LENGTH) { 1457 if (sc->twe_aen_queue[i] == aen) 1458 missing = 0; 1459 } 1460 crit_exit(); 1461 return(missing); 1462 } 1463 1464 1465 #if 0 /* currently unused */ 1466 /******************************************************************************** 1467 * Sleep waiting for at least (timeout) seconds until we see (aen) as 1468 * requested. Returns nonzero on timeout or failure. 1469 * 1470 * XXX: this should not be used in cases where there may be more than one sleeper 1471 * without a mechanism for registering multiple sleepers. 1472 */ 1473 static int 1474 twe_wait_aen(struct twe_softc *sc, int aen, int timeout) 1475 { 1476 time_t expiry; 1477 int found; 1478 1479 debug_called(4); 1480 1481 expiry = time_second + timeout; 1482 found = 0; 1483 1484 crit_enter(); 1485 sc->twe_wait_aen = aen; 1486 do { 1487 twe_fetch_aen(sc); 1488 tsleep(&sc->twe_wait_aen, PZERO, "twewaen", hz); 1489 if (sc->twe_wait_aen == -1) 1490 found = 1; 1491 } while ((time_second <= expiry) && !found); 1492 crit_exit(); 1493 return(!found); 1494 } 1495 #endif 1496 1497 /******************************************************************************** 1498 ******************************************************************************** 1499 Command Buffer Management 1500 ******************************************************************************** 1501 ********************************************************************************/ 1502 1503 /******************************************************************************** 1504 * Get a new command buffer. 1505 * 1506 * This will return NULL if all command buffers are in use. 1507 */ 1508 static int 1509 twe_get_request(struct twe_softc *sc, struct twe_request **tr) 1510 { 1511 debug_called(4); 1512 1513 /* try to reuse an old buffer */ 1514 *tr = twe_dequeue_free(sc); 1515 1516 /* initialise some fields to their defaults */ 1517 if (*tr != NULL) { 1518 (*tr)->tr_data = NULL; 1519 (*tr)->tr_private = NULL; 1520 (*tr)->tr_status = TWE_CMD_SETUP; /* command is in setup phase */ 1521 (*tr)->tr_flags = 0; 1522 (*tr)->tr_complete = NULL; 1523 (*tr)->tr_command.generic.status = 0; /* before submission to controller */ 1524 (*tr)->tr_command.generic.flags = 0; /* not used */ 1525 } 1526 return(*tr == NULL); 1527 } 1528 1529 /******************************************************************************** 1530 * Release a command buffer for reuse. 1531 * 1532 */ 1533 static void 1534 twe_release_request(struct twe_request *tr) 1535 { 1536 debug_called(4); 1537 1538 if (tr->tr_private != NULL) 1539 twe_panic(tr->tr_sc, "tr_private != NULL"); 1540 twe_enqueue_free(tr); 1541 } 1542 1543 /******************************************************************************** 1544 ******************************************************************************** 1545 Debugging 1546 ******************************************************************************** 1547 ********************************************************************************/ 1548 1549 /******************************************************************************** 1550 * Print some information about the controller 1551 */ 1552 void 1553 twe_describe_controller(struct twe_softc *sc) 1554 { 1555 TWE_Param *p[6]; 1556 u_int8_t ports; 1557 u_int32_t size; 1558 int i; 1559 1560 debug_called(2); 1561 1562 /* get the port count */ 1563 twe_get_param_1(sc, TWE_PARAM_CONTROLLER, TWE_PARAM_CONTROLLER_PortCount, &ports); 1564 1565 /* get version strings */ 1566 p[0] = twe_get_param(sc, TWE_PARAM_VERSION, TWE_PARAM_VERSION_FW, 16, NULL); 1567 p[1] = twe_get_param(sc, TWE_PARAM_VERSION, TWE_PARAM_VERSION_BIOS, 16, NULL); 1568 1569 if (p[0] && p[1]) 1570 twe_printf(sc, "%d ports, Firmware %.16s, BIOS %.16s\n", ports, p[0]->data, p[1]->data); 1571 if (bootverbose) { 1572 p[2] = twe_get_param(sc, TWE_PARAM_VERSION, TWE_PARAM_VERSION_Mon, 16, NULL); 1573 p[3] = twe_get_param(sc, TWE_PARAM_VERSION, TWE_PARAM_VERSION_PCB, 8, NULL); 1574 p[4] = twe_get_param(sc, TWE_PARAM_VERSION, TWE_PARAM_VERSION_ATA, 8, NULL); 1575 p[5] = twe_get_param(sc, TWE_PARAM_VERSION, TWE_PARAM_VERSION_PCI, 8, NULL); 1576 1577 if (p[2] && p[3] && p[4] && p[5]) 1578 twe_printf(sc, "Monitor %.16s, PCB %.8s, Achip %.8s, Pchip %.8s\n", 1579 p[2]->data, p[3]->data, p[4]->data, p[5]->data); 1580 1581 if (p[2]) 1582 free(p[2], M_DEVBUF); 1583 if (p[3]) 1584 free(p[3], M_DEVBUF); 1585 if (p[4]) 1586 free(p[4], M_DEVBUF); 1587 if (p[5]) 1588 free(p[5], M_DEVBUF); 1589 } 1590 if (p[0]) 1591 free(p[0], M_DEVBUF); 1592 if (p[1]) 1593 free(p[1], M_DEVBUF); 1594 1595 /* print attached drives */ 1596 if (bootverbose) { 1597 p[0] = twe_get_param(sc, TWE_PARAM_DRIVESUMMARY, TWE_PARAM_DRIVESUMMARY_Status, 16, NULL); 1598 for (i = 0; i < ports; i++) { 1599 if (p[0]->data[i] != TWE_PARAM_DRIVESTATUS_Present) 1600 continue; 1601 twe_get_param_4(sc, TWE_PARAM_DRIVEINFO + i, TWE_PARAM_DRIVEINFO_Size, &size); 1602 p[1] = twe_get_param(sc, TWE_PARAM_DRIVEINFO + i, TWE_PARAM_DRIVEINFO_Model, 40, NULL); 1603 if (p[1] != NULL) { 1604 twe_printf(sc, "port %d: %.40s %dMB\n", i, p[1]->data, size / 2048); 1605 free(p[1], M_DEVBUF); 1606 } else { 1607 twe_printf(sc, "port %d, drive status unavailable\n", i); 1608 } 1609 } 1610 if (p[0]) 1611 free(p[0], M_DEVBUF); 1612 } 1613 } 1614 1615 1616 /******************************************************************************** 1617 * Look up a text description of a numeric code and return a pointer to same. 1618 */ 1619 char * 1620 twe_describe_code(struct twe_code_lookup *table, u_int32_t code) 1621 { 1622 int i; 1623 1624 for (i = 0; table[i].string != NULL; i++) 1625 if (table[i].code == code) 1626 return(table[i].string); 1627 return(table[i+1].string); 1628 } 1629 1630 1631 /******************************************************************************** 1632 * Complain if the status bits aren't what we're expecting. 1633 * 1634 * Rate-limit the complaints to at most one of each every five seconds, but 1635 * always return the correct status. 1636 */ 1637 static int 1638 twe_check_bits(struct twe_softc *sc, u_int32_t status_reg) 1639 { 1640 int result; 1641 static time_t lastwarn[2] = {0, 0}; 1642 1643 /* 1644 * This can be a little problematic, as twe_panic may call twe_reset if 1645 * TWE_DEBUG is not set, which will call us again as part of the soft reset. 1646 */ 1647 if ((status_reg & TWE_STATUS_PANIC_BITS) != 0) { 1648 twe_printf(sc, "FATAL STATUS BIT(S) %b\n", status_reg & TWE_STATUS_PANIC_BITS, 1649 TWE_STATUS_BITS_DESCRIPTION); 1650 twe_panic(sc, "fatal status bits"); 1651 } 1652 1653 result = 0; 1654 if ((status_reg & TWE_STATUS_EXPECTED_BITS) != TWE_STATUS_EXPECTED_BITS) { 1655 if (time_second > (lastwarn[0] + 5)) { 1656 twe_printf(sc, "missing expected status bit(s) %b\n", ~status_reg & TWE_STATUS_EXPECTED_BITS, 1657 TWE_STATUS_BITS_DESCRIPTION); 1658 lastwarn[0] = time_second; 1659 } 1660 result = 1; 1661 } 1662 1663 if ((status_reg & TWE_STATUS_UNEXPECTED_BITS) != 0) { 1664 if (time_second > (lastwarn[1] + 5)) { 1665 twe_printf(sc, "unexpected status bit(s) %b\n", status_reg & TWE_STATUS_UNEXPECTED_BITS, 1666 TWE_STATUS_BITS_DESCRIPTION); 1667 lastwarn[1] = time_second; 1668 } 1669 result = 1; 1670 if (status_reg & TWE_STATUS_PCI_PARITY_ERROR) { 1671 twe_printf(sc, "PCI parity error: Reseat card, move card or buggy device present."); 1672 twe_clear_pci_parity_error(sc); 1673 } 1674 if (status_reg & TWE_STATUS_PCI_ABORT) { 1675 twe_printf(sc, "PCI abort, clearing."); 1676 twe_clear_pci_abort(sc); 1677 } 1678 } 1679 1680 return(result); 1681 } 1682 1683 /******************************************************************************** 1684 * Return a string describing (aen). 1685 * 1686 * The low 8 bits of the aen are the code, the high 8 bits give the unit number 1687 * where an AEN is specific to a unit. 1688 * 1689 * Note that we could expand this routine to handle eg. up/downgrading the status 1690 * of a drive if we had some idea of what the drive's initial status was. 1691 */ 1692 1693 static char * 1694 twe_format_aen(struct twe_softc *sc, u_int16_t aen) 1695 { 1696 static char buf[80]; 1697 device_t child; 1698 char *code, *msg; 1699 1700 code = twe_describe_code(twe_table_aen, TWE_AEN_CODE(aen)); 1701 msg = code + 2; 1702 1703 switch (*code) { 1704 case 'q': 1705 if (!bootverbose) 1706 return(NULL); 1707 /* FALLTHROUGH */ 1708 case 'a': 1709 return(msg); 1710 1711 case 'c': 1712 if ((child = sc->twe_drive[TWE_AEN_UNIT(aen)].td_disk) != NULL) { 1713 sprintf(buf, "twed%d: %s", device_get_unit(child), msg); 1714 } else { 1715 sprintf(buf, "twe%d: %s for unknown unit %d", device_get_unit(sc->twe_dev), 1716 msg, TWE_AEN_UNIT(aen)); 1717 } 1718 return(buf); 1719 1720 case 'p': 1721 sprintf(buf, "twe%d: port %d: %s", device_get_unit(sc->twe_dev), TWE_AEN_UNIT(aen), 1722 msg); 1723 return(buf); 1724 1725 1726 case 'x': 1727 default: 1728 break; 1729 } 1730 sprintf(buf, "unknown AEN 0x%x", aen); 1731 return(buf); 1732 } 1733 1734 /******************************************************************************** 1735 * Print a diagnostic if the status of the command warrants it, and return 1736 * either zero (command was ok) or nonzero (command failed). 1737 */ 1738 static int 1739 twe_report_request(struct twe_request *tr) 1740 { 1741 struct twe_softc *sc = tr->tr_sc; 1742 TWE_Command *cmd = &tr->tr_command; 1743 int result = 0; 1744 1745 /* 1746 * Check the command status value and handle accordingly. 1747 */ 1748 if (cmd->generic.status == TWE_STATUS_RESET) { 1749 /* 1750 * The status code 0xff requests a controller reset. 1751 */ 1752 twe_printf(sc, "command returned with controller reset request\n"); 1753 twe_reset(sc); 1754 result = 1; 1755 } else if (cmd->generic.status > TWE_STATUS_FATAL) { 1756 /* 1757 * Fatal errors that don't require controller reset. 1758 * 1759 * We know a few special flags values. 1760 */ 1761 switch (cmd->generic.flags) { 1762 case 0x1b: 1763 device_printf(sc->twe_drive[cmd->generic.unit].td_disk, 1764 "drive timeout"); 1765 break; 1766 case 0x51: 1767 device_printf(sc->twe_drive[cmd->generic.unit].td_disk, 1768 "unrecoverable drive error"); 1769 break; 1770 default: 1771 device_printf(sc->twe_drive[cmd->generic.unit].td_disk, 1772 "controller error - %s (flags = 0x%x)\n", 1773 twe_describe_code(twe_table_status, cmd->generic.status), 1774 cmd->generic.flags); 1775 result = 1; 1776 } 1777 } else if (cmd->generic.status > TWE_STATUS_WARNING) { 1778 /* 1779 * Warning level status. 1780 */ 1781 device_printf(sc->twe_drive[cmd->generic.unit].td_disk, 1782 "warning - %s (flags = 0x%x)\n", 1783 twe_describe_code(twe_table_status, cmd->generic.status), 1784 cmd->generic.flags); 1785 } else if (cmd->generic.status > 0x40) { 1786 /* 1787 * Info level status. 1788 */ 1789 device_printf(sc->twe_drive[cmd->generic.unit].td_disk, 1790 "attention - %s (flags = 0x%x)\n", 1791 twe_describe_code(twe_table_status, cmd->generic.status), 1792 cmd->generic.flags); 1793 } 1794 1795 return(result); 1796 } 1797 1798 /******************************************************************************** 1799 * Print some controller state to aid in debugging error/panic conditions 1800 */ 1801 void 1802 twe_print_controller(struct twe_softc *sc) 1803 { 1804 u_int32_t status_reg; 1805 1806 status_reg = TWE_STATUS(sc); 1807 twe_printf(sc, "status %b\n", status_reg, TWE_STATUS_BITS_DESCRIPTION); 1808 twe_printf(sc, " current max min\n"); 1809 twe_printf(sc, "free %04d %04d %04d\n", sc->twe_qstat[TWEQ_FREE].q_length, sc->twe_qstat[TWEQ_FREE].q_max, sc->twe_qstat[TWEQ_FREE].q_min); 1810 twe_printf(sc, "ready %04d %04d %04d\n", sc->twe_qstat[TWEQ_READY].q_length, sc->twe_qstat[TWEQ_READY].q_max, sc->twe_qstat[TWEQ_READY].q_min); 1811 twe_printf(sc, "busy %04d %04d %04d\n", sc->twe_qstat[TWEQ_BUSY].q_length, sc->twe_qstat[TWEQ_BUSY].q_max, sc->twe_qstat[TWEQ_BUSY].q_min); 1812 twe_printf(sc, "complete %04d %04d %04d\n", sc->twe_qstat[TWEQ_COMPLETE].q_length, sc->twe_qstat[TWEQ_COMPLETE].q_max, sc->twe_qstat[TWEQ_COMPLETE].q_min); 1813 twe_printf(sc, "bioq %04d %04d %04d\n", sc->twe_qstat[TWEQ_BIO].q_length, sc->twe_qstat[TWEQ_BIO].q_max, sc->twe_qstat[TWEQ_BIO].q_min); 1814 twe_printf(sc, "AEN queue head %d tail %d\n", sc->twe_aen_head, sc->twe_aen_tail); 1815 } 1816 1817 static void 1818 twe_panic(struct twe_softc *sc, char *reason) 1819 { 1820 twe_print_controller(sc); 1821 #ifdef TWE_DEBUG 1822 panic(reason); 1823 #else 1824 twe_reset(sc); 1825 #endif 1826 } 1827 1828 #if 0 1829 /******************************************************************************** 1830 * Print a request/command in human-readable format. 1831 */ 1832 static void 1833 twe_print_request(struct twe_request *tr) 1834 { 1835 struct twe_softc *sc = tr->tr_sc; 1836 TWE_Command *cmd = &tr->tr_command; 1837 int i; 1838 1839 twe_printf(sc, "CMD: request_id %d opcode <%s> size %d unit %d host_id %d\n", 1840 cmd->generic.request_id, twe_describe_code(twe_table_opcode, cmd->generic.opcode), cmd->generic.size, 1841 cmd->generic.unit, cmd->generic.host_id); 1842 twe_printf(sc, " status %d flags 0x%x count %d sgl_offset %d\n", 1843 cmd->generic.status, cmd->generic.flags, cmd->generic.count, cmd->generic.sgl_offset); 1844 1845 switch(cmd->generic.opcode) { /* XXX add more opcodes? */ 1846 case TWE_OP_READ: 1847 case TWE_OP_WRITE: 1848 twe_printf(sc, " lba %d\n", cmd->io.lba); 1849 for (i = 0; (i < TWE_MAX_SGL_LENGTH) && (cmd->io.sgl[i].length != 0); i++) 1850 twe_printf(sc, " %d: 0x%x/%d\n", 1851 i, cmd->io.sgl[i].address, cmd->io.sgl[i].length); 1852 break; 1853 1854 case TWE_OP_GET_PARAM: 1855 case TWE_OP_SET_PARAM: 1856 for (i = 0; (i < TWE_MAX_SGL_LENGTH) && (cmd->param.sgl[i].length != 0); i++) 1857 twe_printf(sc, " %d: 0x%x/%d\n", 1858 i, cmd->param.sgl[i].address, cmd->param.sgl[i].length); 1859 break; 1860 1861 case TWE_OP_INIT_CONNECTION: 1862 twe_printf(sc, " response queue pointer 0x%x\n", 1863 cmd->initconnection.response_queue_pointer); 1864 break; 1865 1866 default: 1867 break; 1868 } 1869 twe_printf(sc, " tr_command %p/0x%x tr_data %p/0x%x,%d\n", 1870 tr, tr->tr_cmdphys, tr->tr_data, tr->tr_dataphys, tr->tr_length); 1871 twe_printf(sc, " tr_status %d tr_flags 0x%x tr_complete %p tr_private %p\n", 1872 tr->tr_status, tr->tr_flags, tr->tr_complete, tr->tr_private); 1873 } 1874 1875 #endif 1876