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