1 /* 2 * Bus independent FreeBSD shim for the aic7xxx based adaptec SCSI controllers 3 * 4 * Copyright (c) 1994, 1995, 1996, 1997, 1998, 1999, 2000 Justin T. Gibbs. 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions, and the following disclaimer, 12 * without modification. 13 * 2. The name of the author may not be used to endorse or promote products 14 * derived from this software without specific prior written permission. 15 * 16 * Alternatively, this software may be distributed under the terms of the 17 * GNU Public License ("GPL"). 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR 23 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 29 * SUCH DAMAGE. 30 * 31 * $Id$ 32 * 33 * $FreeBSD$ 34 */ 35 36 #include <dev/aic7xxx/aic7xxx_freebsd.h> 37 #include <dev/aic7xxx/aic7xxx_inline.h> 38 39 #include <sys/eventhandler.h> 40 41 #ifndef AHC_TMODE_ENABLE 42 #define AHC_TMODE_ENABLE 0 43 #endif 44 45 #define ccb_scb_ptr spriv_ptr0 46 47 #ifdef AHC_DEBUG 48 static int ahc_debug = AHC_DEBUG; 49 #endif 50 51 static void ahc_freebsd_intr(void *arg); 52 53 #if UNUSED 54 static void ahc_dump_targcmd(struct target_cmd *cmd); 55 #endif 56 static void ahc_action(struct cam_sim *sim, union ccb *ccb); 57 static void ahc_get_tran_settings(struct ahc_softc *ahc, 58 int our_id, char channel, 59 struct ccb_trans_settings *cts); 60 static void ahc_async(void *callback_arg, uint32_t code, 61 struct cam_path *path, void *arg); 62 static void ahc_execute_scb(void *arg, bus_dma_segment_t *dm_segs, 63 int nsegments, int error); 64 static void ahc_poll(struct cam_sim *sim); 65 static void ahc_setup_data(struct ahc_softc *ahc, struct cam_sim *sim, 66 struct ccb_scsiio *csio, struct scb *scb); 67 static void ahc_abort_ccb(struct ahc_softc *ahc, struct cam_sim *sim, 68 union ccb *ccb); 69 static int ahc_create_path(struct ahc_softc *ahc, 70 char channel, u_int target, u_int lun, 71 struct cam_path **path); 72 73 static void ahc_set_recoveryscb(struct ahc_softc *ahc, struct scb *scb); 74 75 static int 76 ahc_create_path(struct ahc_softc *ahc, char channel, u_int target, 77 u_int lun, struct cam_path **path) 78 { 79 path_id_t path_id; 80 81 if (channel == 'B') 82 path_id = cam_sim_path(ahc->platform_data->sim_b); 83 else 84 path_id = cam_sim_path(ahc->platform_data->sim); 85 86 return (xpt_create_path(path, /*periph*/NULL, 87 path_id, target, lun)); 88 } 89 90 /* 91 * Attach all the sub-devices we can find 92 */ 93 int 94 ahc_attach(struct ahc_softc *ahc) 95 { 96 char ahc_info[256]; 97 struct ccb_setasync csa; 98 struct cam_devq *devq; 99 int bus_id; 100 int bus_id2; 101 struct cam_sim *sim; 102 struct cam_sim *sim2; 103 struct cam_path *path; 104 struct cam_path *path2; 105 long s; 106 int count; 107 int error; 108 109 count = 0; 110 sim = NULL; 111 sim2 = NULL; 112 113 ahc_controller_info(ahc, ahc_info); 114 printf("%s\n", ahc_info); 115 ahc_lock(ahc, &s); 116 /* Hook up our interrupt handler */ 117 if ((error = bus_setup_intr(ahc->dev_softc, ahc->platform_data->irq, 118 INTR_TYPE_CAM, ahc_freebsd_intr, ahc, 119 &ahc->platform_data->ih)) != 0) { 120 device_printf(ahc->dev_softc, "bus_setup_intr() failed: %d\n", 121 error); 122 goto fail; 123 } 124 125 /* 126 * Attach secondary channel first if the user has 127 * declared it the primary channel. 128 */ 129 if ((ahc->flags & AHC_CHANNEL_B_PRIMARY) != 0) { 130 bus_id = 1; 131 bus_id2 = 0; 132 } else { 133 bus_id = 0; 134 bus_id2 = 1; 135 } 136 137 /* 138 * Create the device queue for our SIM(s). 139 */ 140 devq = cam_simq_alloc(AHC_SCB_MAX - 1); 141 if (devq == NULL) 142 goto fail; 143 144 /* 145 * Construct our first channel SIM entry 146 */ 147 sim = cam_sim_alloc(ahc_action, ahc_poll, "ahc", ahc, 148 device_get_unit(ahc->dev_softc), 149 1, AHC_SCB_MAX - 1, devq); 150 if (sim == NULL) { 151 cam_simq_free(devq); 152 goto fail; 153 } 154 155 if (xpt_bus_register(sim, bus_id) != CAM_SUCCESS) { 156 cam_sim_free(sim, /*free_devq*/TRUE); 157 sim = NULL; 158 goto fail; 159 } 160 161 if (xpt_create_path(&path, /*periph*/NULL, 162 cam_sim_path(sim), CAM_TARGET_WILDCARD, 163 CAM_LUN_WILDCARD) != CAM_REQ_CMP) { 164 xpt_bus_deregister(cam_sim_path(sim)); 165 cam_sim_free(sim, /*free_devq*/TRUE); 166 sim = NULL; 167 goto fail; 168 } 169 170 xpt_setup_ccb(&csa.ccb_h, path, /*priority*/5); 171 csa.ccb_h.func_code = XPT_SASYNC_CB; 172 csa.event_enable = AC_LOST_DEVICE; 173 csa.callback = ahc_async; 174 csa.callback_arg = sim; 175 xpt_action((union ccb *)&csa); 176 count++; 177 178 if (ahc->features & AHC_TWIN) { 179 sim2 = cam_sim_alloc(ahc_action, ahc_poll, "ahc", 180 ahc, device_get_unit(ahc->dev_softc), 1, 181 AHC_SCB_MAX - 1, devq); 182 183 if (sim2 == NULL) { 184 printf("ahc_attach: Unable to attach second " 185 "bus due to resource shortage"); 186 goto fail; 187 } 188 189 if (xpt_bus_register(sim2, bus_id2) != CAM_SUCCESS) { 190 printf("ahc_attach: Unable to attach second " 191 "bus due to resource shortage"); 192 /* 193 * We do not want to destroy the device queue 194 * because the first bus is using it. 195 */ 196 cam_sim_free(sim2, /*free_devq*/FALSE); 197 goto fail; 198 } 199 200 if (xpt_create_path(&path2, /*periph*/NULL, 201 cam_sim_path(sim2), 202 CAM_TARGET_WILDCARD, 203 CAM_LUN_WILDCARD) != CAM_REQ_CMP) { 204 xpt_bus_deregister(cam_sim_path(sim2)); 205 cam_sim_free(sim2, /*free_devq*/FALSE); 206 sim2 = NULL; 207 goto fail; 208 } 209 xpt_setup_ccb(&csa.ccb_h, path2, /*priority*/5); 210 csa.ccb_h.func_code = XPT_SASYNC_CB; 211 csa.event_enable = AC_LOST_DEVICE; 212 csa.callback = ahc_async; 213 csa.callback_arg = sim2; 214 xpt_action((union ccb *)&csa); 215 count++; 216 } 217 218 fail: 219 if ((ahc->flags & AHC_CHANNEL_B_PRIMARY) != 0) { 220 ahc->platform_data->sim_b = sim; 221 ahc->platform_data->path_b = path; 222 ahc->platform_data->sim = sim2; 223 ahc->platform_data->path = path2; 224 } else { 225 ahc->platform_data->sim = sim; 226 ahc->platform_data->path = path; 227 ahc->platform_data->sim_b = sim2; 228 ahc->platform_data->path_b = path2; 229 } 230 ahc_unlock(ahc, &s); 231 232 if (count != 0) 233 /* We have to wait until after any system dumps... */ 234 EVENTHANDLER_REGISTER(shutdown_final, ahc_shutdown, 235 ahc, SHUTDOWN_PRI_DEFAULT); 236 237 return (count); 238 } 239 240 /* 241 * Catch an interrupt from the adapter 242 */ 243 void 244 ahc_freebsd_intr(void *arg) 245 { 246 struct ahc_softc *ahc; 247 248 ahc = (struct ahc_softc *)arg; 249 ahc_intr(ahc); 250 } 251 252 /* 253 * We have an scb which has been processed by the 254 * adaptor, now we look to see how the operation 255 * went. 256 */ 257 void 258 ahc_done(struct ahc_softc *ahc, struct scb *scb) 259 { 260 union ccb *ccb; 261 262 CAM_DEBUG(scb->io_ctx->ccb_h.path, CAM_DEBUG_TRACE, 263 ("ahc_done - scb %d\n", scb->hscb->tag)); 264 265 ccb = scb->io_ctx; 266 LIST_REMOVE(scb, pending_links); 267 if ((scb->flags & SCB_UNTAGGEDQ) != 0) { 268 struct scb_tailq *untagged_q; 269 270 untagged_q = &ahc->untagged_queues[ccb->ccb_h.target_id]; 271 TAILQ_REMOVE(untagged_q, scb, links.tqe); 272 scb->flags &= ~SCB_UNTAGGEDQ; 273 ahc_run_untagged_queue(ahc, untagged_q); 274 } 275 276 untimeout(ahc_timeout, (caddr_t)scb, ccb->ccb_h.timeout_ch); 277 278 if ((ccb->ccb_h.flags & CAM_DIR_MASK) != CAM_DIR_NONE) { 279 bus_dmasync_op_t op; 280 281 if ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN) 282 op = BUS_DMASYNC_POSTREAD; 283 else 284 op = BUS_DMASYNC_POSTWRITE; 285 bus_dmamap_sync(ahc->buffer_dmat, scb->dmamap, op); 286 bus_dmamap_unload(ahc->buffer_dmat, scb->dmamap); 287 } 288 289 if (ccb->ccb_h.func_code == XPT_CONT_TARGET_IO) { 290 if (ahc_get_transaction_status(scb) == CAM_REQ_INPROG) 291 ccb->ccb_h.status |= CAM_REQ_CMP; 292 ccb->ccb_h.status &= ~CAM_SIM_QUEUED; 293 ahc_free_scb(ahc, scb); 294 xpt_done(ccb); 295 return; 296 } 297 298 /* 299 * If the recovery SCB completes, we have to be 300 * out of our timeout. 301 */ 302 if ((scb->flags & SCB_RECOVERY_SCB) != 0) { 303 struct scb *list_scb; 304 305 /* 306 * We were able to complete the command successfully, 307 * so reinstate the timeouts for all other pending 308 * commands. 309 */ 310 LIST_FOREACH(list_scb, &ahc->pending_scbs, pending_links) { 311 union ccb *ccb; 312 313 ccb = list_scb->io_ctx; 314 ccb->ccb_h.timeout_ch = 315 timeout(ahc_timeout, list_scb, 316 (ccb->ccb_h.timeout * hz)/1000); 317 } 318 319 if (ahc_get_transaction_status(scb) == CAM_BDR_SENT 320 || ahc_get_transaction_status(scb) == CAM_REQ_ABORTED) 321 ahc_set_transaction_status(scb, CAM_CMD_TIMEOUT); 322 ahc_print_path(ahc, scb); 323 printf("no longer in timeout, status = %x\n", 324 ccb->ccb_h.status); 325 } 326 327 /* Don't clobber any existing error state */ 328 if (ahc_get_transaction_status(scb) == CAM_REQ_INPROG) { 329 ccb->ccb_h.status |= CAM_REQ_CMP; 330 } else if ((scb->flags & SCB_SENSE) != 0) { 331 /* 332 * We performed autosense retrieval. 333 * 334 * Zero any sense not transferred by the 335 * device. The SCSI spec mandates that any 336 * untransfered data should be assumed to be 337 * zero. Complete the 'bounce' of sense information 338 * through buffers accessible via bus-space by 339 * copying it into the clients csio. 340 */ 341 memset(&ccb->csio.sense_data, 0, sizeof(ccb->csio.sense_data)); 342 memcpy(&ccb->csio.sense_data, 343 &ahc->scb_data->sense[scb->hscb->tag], 344 (scb->sg_list->len & AHC_SG_LEN_MASK) 345 - ccb->csio.sense_resid); 346 scb->io_ctx->ccb_h.status |= CAM_AUTOSNS_VALID; 347 } 348 ccb->ccb_h.status &= ~CAM_SIM_QUEUED; 349 ahc_free_scb(ahc, scb); 350 xpt_done(ccb); 351 } 352 353 static void 354 ahc_action(struct cam_sim *sim, union ccb *ccb) 355 { 356 struct ahc_softc *ahc; 357 struct tmode_lstate *lstate; 358 u_int target_id; 359 u_int our_id; 360 long s; 361 362 CAM_DEBUG(ccb->ccb_h.path, CAM_DEBUG_TRACE, ("ahc_action\n")); 363 364 ahc = (struct ahc_softc *)cam_sim_softc(sim); 365 366 target_id = ccb->ccb_h.target_id; 367 our_id = SIM_SCSI_ID(ahc, sim); 368 369 switch (ccb->ccb_h.func_code) { 370 /* Common cases first */ 371 case XPT_ACCEPT_TARGET_IO: /* Accept Host Target Mode CDB */ 372 case XPT_CONT_TARGET_IO:/* Continue Host Target I/O Connection*/ 373 { 374 struct tmode_tstate *tstate; 375 cam_status status; 376 377 status = ahc_find_tmode_devs(ahc, sim, ccb, &tstate, 378 &lstate, TRUE); 379 380 if (status != CAM_REQ_CMP) { 381 if (ccb->ccb_h.func_code == XPT_CONT_TARGET_IO) { 382 /* Response from the black hole device */ 383 tstate = NULL; 384 lstate = ahc->black_hole; 385 } else { 386 ccb->ccb_h.status = status; 387 xpt_done(ccb); 388 break; 389 } 390 } 391 if (ccb->ccb_h.func_code == XPT_ACCEPT_TARGET_IO) { 392 393 ahc_lock(ahc, &s); 394 SLIST_INSERT_HEAD(&lstate->accept_tios, &ccb->ccb_h, 395 sim_links.sle); 396 ccb->ccb_h.status = CAM_REQ_INPROG; 397 if ((ahc->flags & AHC_TQINFIFO_BLOCKED) != 0) 398 ahc_run_tqinfifo(ahc, /*paused*/FALSE); 399 ahc_unlock(ahc, &s); 400 break; 401 } 402 403 /* 404 * The target_id represents the target we attempt to 405 * select. In target mode, this is the initiator of 406 * the original command. 407 */ 408 our_id = target_id; 409 target_id = ccb->csio.init_id; 410 /* FALLTHROUGH */ 411 } 412 case XPT_SCSI_IO: /* Execute the requested I/O operation */ 413 case XPT_RESET_DEV: /* Bus Device Reset the specified SCSI device */ 414 { 415 struct scb *scb; 416 struct hardware_scb *hscb; 417 418 if ((ahc->flags & AHC_INITIATORROLE) == 0 419 && (ccb->ccb_h.func_code == XPT_SCSI_IO 420 || ccb->ccb_h.func_code == XPT_RESET_DEV)) { 421 ccb->ccb_h.status = CAM_PROVIDE_FAIL; 422 xpt_done(ccb); 423 } 424 425 /* 426 * get an scb to use. 427 */ 428 ahc_lock(ahc, &s); 429 if ((scb = ahc_get_scb(ahc)) == NULL) { 430 431 xpt_freeze_simq(sim, /*count*/1); 432 ahc->flags |= AHC_RESOURCE_SHORTAGE; 433 ahc_unlock(ahc, &s); 434 ccb->ccb_h.status = CAM_REQUEUE_REQ; 435 xpt_done(ccb); 436 return; 437 } 438 ahc_unlock(ahc, &s); 439 440 hscb = scb->hscb; 441 442 CAM_DEBUG(ccb->ccb_h.path, CAM_DEBUG_SUBTRACE, 443 ("start scb(%p)\n", scb)); 444 scb->io_ctx = ccb; 445 /* 446 * So we can find the SCB when an abort is requested 447 */ 448 ccb->ccb_h.ccb_scb_ptr = scb; 449 450 /* 451 * Put all the arguments for the xfer in the scb 452 */ 453 hscb->control = 0; 454 hscb->scsiid = BUILD_SCSIID(ahc, sim, target_id, our_id); 455 hscb->lun = ccb->ccb_h.target_lun; 456 if (ccb->ccb_h.func_code == XPT_RESET_DEV) { 457 hscb->cdb_len = 0; 458 scb->flags |= SCB_DEVICE_RESET; 459 hscb->control |= MK_MESSAGE; 460 ahc_execute_scb(scb, NULL, 0, 0); 461 } else { 462 if (ccb->ccb_h.func_code == XPT_CONT_TARGET_IO) { 463 struct target_data *tdata; 464 465 tdata = &hscb->shared_data.tdata; 466 if (ahc->pending_device == lstate) { 467 scb->flags |= SCB_TARGET_IMMEDIATE; 468 ahc->pending_device = NULL; 469 } 470 hscb->control |= TARGET_SCB; 471 tdata->target_phases = IDENTIFY_SEEN; 472 if ((ccb->ccb_h.flags & CAM_SEND_STATUS) != 0) { 473 tdata->target_phases |= SPHASE_PENDING; 474 tdata->scsi_status = 475 ccb->csio.scsi_status; 476 } 477 tdata->initiator_tag = ccb->csio.tag_id; 478 } 479 if (ccb->ccb_h.flags & CAM_TAG_ACTION_VALID) 480 hscb->control |= ccb->csio.tag_action; 481 482 ahc_setup_data(ahc, sim, &ccb->csio, scb); 483 } 484 break; 485 } 486 case XPT_NOTIFY_ACK: 487 case XPT_IMMED_NOTIFY: 488 { 489 struct tmode_tstate *tstate; 490 struct tmode_lstate *lstate; 491 cam_status status; 492 493 status = ahc_find_tmode_devs(ahc, sim, ccb, &tstate, 494 &lstate, TRUE); 495 496 if (status != CAM_REQ_CMP) { 497 ccb->ccb_h.status = status; 498 xpt_done(ccb); 499 break; 500 } 501 SLIST_INSERT_HEAD(&lstate->immed_notifies, &ccb->ccb_h, 502 sim_links.sle); 503 ccb->ccb_h.status = CAM_REQ_INPROG; 504 ahc_send_lstate_events(ahc, lstate); 505 break; 506 } 507 case XPT_EN_LUN: /* Enable LUN as a target */ 508 ahc_handle_en_lun(ahc, sim, ccb); 509 xpt_done(ccb); 510 break; 511 case XPT_ABORT: /* Abort the specified CCB */ 512 { 513 ahc_abort_ccb(ahc, sim, ccb); 514 break; 515 } 516 case XPT_SET_TRAN_SETTINGS: 517 { 518 #ifdef AHC_NEW_TRAN_SETTINGS 519 struct ahc_devinfo devinfo; 520 struct ccb_trans_settings *cts; 521 struct ccb_trans_settings_scsi *scsi; 522 struct ccb_trans_settings_spi *spi; 523 struct ahc_initiator_tinfo *tinfo; 524 struct tmode_tstate *tstate; 525 uint16_t *discenable; 526 uint16_t *tagenable; 527 u_int update_type; 528 529 cts = &ccb->cts; 530 scsi = &cts->proto_specific.scsi; 531 spi = &cts->xport_specific.spi; 532 ahc_compile_devinfo(&devinfo, SIM_SCSI_ID(ahc, sim), 533 cts->ccb_h.target_id, 534 cts->ccb_h.target_lun, 535 SIM_CHANNEL(ahc, sim), 536 ROLE_UNKNOWN); 537 tinfo = ahc_fetch_transinfo(ahc, devinfo.channel, 538 devinfo.our_scsiid, 539 devinfo.target, &tstate); 540 update_type = 0; 541 if (cts->type == CTS_TYPE_CURRENT_SETTINGS) { 542 update_type |= AHC_TRANS_GOAL; 543 discenable = &tstate->discenable; 544 tagenable = &tstate->tagenable; 545 tinfo->current.protocol_version = 546 cts->protocol_version; 547 tinfo->current.transport_version = 548 cts->transport_version; 549 tinfo->goal.protocol_version = 550 cts->protocol_version; 551 tinfo->goal.transport_version = 552 cts->transport_version; 553 } else if (cts->type == CTS_TYPE_USER_SETTINGS) { 554 update_type |= AHC_TRANS_USER; 555 discenable = &ahc->user_discenable; 556 tagenable = &ahc->user_tagenable; 557 tinfo->user.protocol_version = 558 cts->protocol_version; 559 tinfo->user.transport_version = 560 cts->transport_version; 561 } else { 562 ccb->ccb_h.status = CAM_REQ_INVALID; 563 xpt_done(ccb); 564 break; 565 } 566 567 ahc_lock(ahc, &s); 568 569 if ((spi->valid & CTS_SPI_VALID_DISC) != 0) { 570 if ((spi->flags & CTS_SPI_FLAGS_DISC_ENB) != 0) 571 *discenable |= devinfo.target_mask; 572 else 573 *discenable &= ~devinfo.target_mask; 574 } 575 576 if ((scsi->valid & CTS_SCSI_VALID_TQ) != 0) { 577 if ((scsi->flags & CTS_SCSI_FLAGS_TAG_ENB) != 0) 578 *tagenable |= devinfo.target_mask; 579 else 580 *tagenable &= ~devinfo.target_mask; 581 } 582 583 if ((spi->valid & CTS_SPI_VALID_BUS_WIDTH) != 0) { 584 ahc_validate_width(ahc, /*tinfo limit*/NULL, 585 &spi->bus_width, ROLE_UNKNOWN); 586 ahc_set_width(ahc, &devinfo, spi->bus_width, 587 update_type, /*paused*/FALSE); 588 } 589 590 if ((spi->valid & CTS_SPI_VALID_PPR_OPTIONS) == 0) { 591 if (update_type == AHC_TRANS_USER) 592 spi->ppr_options = tinfo->user.ppr_options; 593 else 594 spi->ppr_options = tinfo->goal.ppr_options; 595 } 596 597 if ((spi->valid & CTS_SPI_VALID_SYNC_OFFSET) == 0) { 598 if (update_type == AHC_TRANS_USER) 599 spi->sync_offset = tinfo->user.offset; 600 else 601 spi->sync_offset = tinfo->goal.offset; 602 } 603 604 if ((spi->valid & CTS_SPI_VALID_SYNC_RATE) == 0) { 605 if (update_type == AHC_TRANS_USER) 606 spi->sync_period = tinfo->user.period; 607 else 608 spi->sync_period = tinfo->goal.period; 609 } 610 611 if (((spi->valid & CTS_SPI_VALID_SYNC_RATE) != 0) 612 || ((spi->valid & CTS_SPI_VALID_SYNC_OFFSET) != 0)) { 613 struct ahc_syncrate *syncrate; 614 u_int maxsync; 615 616 if ((ahc->features & AHC_ULTRA2) != 0) 617 maxsync = AHC_SYNCRATE_DT; 618 else if ((ahc->features & AHC_ULTRA) != 0) 619 maxsync = AHC_SYNCRATE_ULTRA; 620 else 621 maxsync = AHC_SYNCRATE_FAST; 622 623 syncrate = ahc_find_syncrate(ahc, &spi->sync_period, 624 &spi->ppr_options, 625 maxsync); 626 ahc_validate_offset(ahc, /*tinfo limit*/NULL, 627 syncrate, &spi->sync_offset, 628 spi->bus_width, ROLE_UNKNOWN); 629 630 /* We use a period of 0 to represent async */ 631 if (spi->sync_offset == 0) { 632 spi->sync_period = 0; 633 spi->ppr_options = 0; 634 } 635 636 ahc_set_syncrate(ahc, &devinfo, syncrate, 637 spi->sync_period, spi->sync_offset, 638 spi->ppr_options, update_type, 639 /*paused*/FALSE); 640 } 641 ahc_unlock(ahc, &s); 642 ccb->ccb_h.status = CAM_REQ_CMP; 643 xpt_done(ccb); 644 #else 645 struct ahc_devinfo devinfo; 646 struct ccb_trans_settings *cts; 647 struct ahc_initiator_tinfo *tinfo; 648 struct tmode_tstate *tstate; 649 uint16_t *discenable; 650 uint16_t *tagenable; 651 u_int update_type; 652 long s; 653 654 cts = &ccb->cts; 655 ahc_compile_devinfo(&devinfo, SIM_SCSI_ID(ahc, sim), 656 cts->ccb_h.target_id, 657 cts->ccb_h.target_lun, 658 SIM_CHANNEL(ahc, sim), 659 ROLE_UNKNOWN); 660 tinfo = ahc_fetch_transinfo(ahc, devinfo.channel, 661 devinfo.our_scsiid, 662 devinfo.target, &tstate); 663 update_type = 0; 664 if ((cts->flags & CCB_TRANS_CURRENT_SETTINGS) != 0) { 665 update_type |= AHC_TRANS_GOAL; 666 discenable = &tstate->discenable; 667 tagenable = &tstate->tagenable; 668 } else if ((cts->flags & CCB_TRANS_USER_SETTINGS) != 0) { 669 update_type |= AHC_TRANS_USER; 670 discenable = &ahc->user_discenable; 671 tagenable = &ahc->user_tagenable; 672 } else { 673 ccb->ccb_h.status = CAM_REQ_INVALID; 674 xpt_done(ccb); 675 break; 676 } 677 678 ahc_lock(ahc, &s); 679 680 if ((cts->valid & CCB_TRANS_DISC_VALID) != 0) { 681 if ((cts->flags & CCB_TRANS_DISC_ENB) != 0) 682 *discenable |= devinfo.target_mask; 683 else 684 *discenable &= ~devinfo.target_mask; 685 } 686 687 if ((cts->valid & CCB_TRANS_TQ_VALID) != 0) { 688 if ((cts->flags & CCB_TRANS_TAG_ENB) != 0) 689 *tagenable |= devinfo.target_mask; 690 else 691 *tagenable &= ~devinfo.target_mask; 692 } 693 694 if ((cts->valid & CCB_TRANS_BUS_WIDTH_VALID) != 0) { 695 ahc_validate_width(ahc, /*tinfo limit*/NULL, 696 &cts->bus_width, ROLE_UNKNOWN); 697 ahc_set_width(ahc, &devinfo, cts->bus_width, 698 update_type, /*paused*/FALSE); 699 } 700 701 if ((cts->valid & CCB_TRANS_SYNC_OFFSET_VALID) == 0) { 702 if (update_type == AHC_TRANS_USER) 703 cts->sync_offset = tinfo->user.offset; 704 else 705 cts->sync_offset = tinfo->goal.offset; 706 } 707 708 if ((cts->valid & CCB_TRANS_SYNC_RATE_VALID) == 0) { 709 if (update_type == AHC_TRANS_USER) 710 cts->sync_period = tinfo->user.period; 711 else 712 cts->sync_period = tinfo->goal.period; 713 } 714 715 if (((cts->valid & CCB_TRANS_SYNC_RATE_VALID) != 0) 716 || ((cts->valid & CCB_TRANS_SYNC_OFFSET_VALID) != 0)) { 717 struct ahc_syncrate *syncrate; 718 u_int ppr_options; 719 u_int maxsync; 720 721 if ((ahc->features & AHC_ULTRA2) != 0) 722 maxsync = AHC_SYNCRATE_DT; 723 else if ((ahc->features & AHC_ULTRA) != 0) 724 maxsync = AHC_SYNCRATE_ULTRA; 725 else 726 maxsync = AHC_SYNCRATE_FAST; 727 728 ppr_options = 0; 729 if (cts->sync_period <= 9) 730 ppr_options = MSG_EXT_PPR_DT_REQ; 731 732 syncrate = ahc_find_syncrate(ahc, &cts->sync_period, 733 &ppr_options, 734 maxsync); 735 ahc_validate_offset(ahc, /*tinfo limit*/NULL, 736 syncrate, &cts->sync_offset, 737 MSG_EXT_WDTR_BUS_8_BIT, 738 ROLE_UNKNOWN); 739 740 /* We use a period of 0 to represent async */ 741 if (cts->sync_offset == 0) { 742 cts->sync_period = 0; 743 ppr_options = 0; 744 } 745 746 if (ppr_options == MSG_EXT_PPR_DT_REQ 747 && tinfo->user.transport_version >= 3) { 748 tinfo->goal.transport_version = 749 tinfo->user.transport_version; 750 tinfo->current.transport_version = 751 tinfo->user.transport_version; 752 } 753 754 ahc_set_syncrate(ahc, &devinfo, syncrate, 755 cts->sync_period, cts->sync_offset, 756 ppr_options, update_type, 757 /*paused*/FALSE); 758 } 759 ahc_unlock(ahc, &s); 760 ccb->ccb_h.status = CAM_REQ_CMP; 761 xpt_done(ccb); 762 #endif 763 break; 764 } 765 case XPT_GET_TRAN_SETTINGS: 766 /* Get default/user set transfer settings for the target */ 767 { 768 769 ahc_lock(ahc, &s); 770 ahc_get_tran_settings(ahc, SIM_SCSI_ID(ahc, sim), 771 SIM_CHANNEL(ahc, sim), &ccb->cts); 772 ahc_unlock(ahc, &s); 773 xpt_done(ccb); 774 break; 775 } 776 case XPT_CALC_GEOMETRY: 777 { 778 struct ccb_calc_geometry *ccg; 779 uint32_t size_mb; 780 uint32_t secs_per_cylinder; 781 int extended; 782 783 ccg = &ccb->ccg; 784 size_mb = ccg->volume_size 785 / ((1024L * 1024L) / ccg->block_size); 786 extended = SIM_IS_SCSIBUS_B(ahc, sim) 787 ? ahc->flags & AHC_EXTENDED_TRANS_B 788 : ahc->flags & AHC_EXTENDED_TRANS_A; 789 790 if (size_mb > 1024 && extended) { 791 ccg->heads = 255; 792 ccg->secs_per_track = 63; 793 } else { 794 ccg->heads = 64; 795 ccg->secs_per_track = 32; 796 } 797 secs_per_cylinder = ccg->heads * ccg->secs_per_track; 798 ccg->cylinders = ccg->volume_size / secs_per_cylinder; 799 ccb->ccb_h.status = CAM_REQ_CMP; 800 xpt_done(ccb); 801 break; 802 } 803 case XPT_RESET_BUS: /* Reset the specified SCSI bus */ 804 { 805 int found; 806 807 ahc_lock(ahc, &s); 808 found = ahc_reset_channel(ahc, SIM_CHANNEL(ahc, sim), 809 /*initiate reset*/TRUE); 810 ahc_unlock(ahc, &s); 811 if (bootverbose) { 812 xpt_print_path(SIM_PATH(ahc, sim)); 813 printf("SCSI bus reset delivered. " 814 "%d SCBs aborted.\n", found); 815 } 816 ccb->ccb_h.status = CAM_REQ_CMP; 817 xpt_done(ccb); 818 break; 819 } 820 case XPT_TERM_IO: /* Terminate the I/O process */ 821 /* XXX Implement */ 822 ccb->ccb_h.status = CAM_REQ_INVALID; 823 xpt_done(ccb); 824 break; 825 case XPT_PATH_INQ: /* Path routing inquiry */ 826 { 827 struct ccb_pathinq *cpi = &ccb->cpi; 828 829 cpi->version_num = 1; /* XXX??? */ 830 cpi->hba_inquiry = PI_SDTR_ABLE|PI_TAG_ABLE; 831 if ((ahc->features & AHC_WIDE) != 0) 832 cpi->hba_inquiry |= PI_WIDE_16; 833 if ((ahc->features & AHC_TARGETMODE) != 0) { 834 cpi->target_sprt = PIT_PROCESSOR 835 | PIT_DISCONNECT 836 | PIT_TERM_IO; 837 } else { 838 cpi->target_sprt = 0; 839 } 840 cpi->hba_misc = 0; 841 cpi->hba_eng_cnt = 0; 842 cpi->max_target = (ahc->features & AHC_WIDE) ? 15 : 7; 843 cpi->max_lun = 64; 844 if (SIM_IS_SCSIBUS_B(ahc, sim)) { 845 cpi->initiator_id = ahc->our_id_b; 846 if ((ahc->flags & AHC_RESET_BUS_B) == 0) 847 cpi->hba_misc |= PIM_NOBUSRESET; 848 } else { 849 cpi->initiator_id = ahc->our_id; 850 if ((ahc->flags & AHC_RESET_BUS_A) == 0) 851 cpi->hba_misc |= PIM_NOBUSRESET; 852 } 853 cpi->bus_id = cam_sim_bus(sim); 854 cpi->base_transfer_speed = 3300; 855 strncpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN); 856 strncpy(cpi->hba_vid, "Adaptec", HBA_IDLEN); 857 strncpy(cpi->dev_name, cam_sim_name(sim), DEV_IDLEN); 858 cpi->unit_number = cam_sim_unit(sim); 859 #ifdef AHC_NEW_TRAN_SETTINGS 860 cpi->protocol = PROTO_SCSI; 861 cpi->protocol_version = SCSI_REV_2; 862 cpi->transport = XPORT_SPI; 863 cpi->transport_version = 2; 864 cpi->xport_specific.spi.ppr_options = SID_SPI_CLOCK_ST; 865 if ((ahc->features & AHC_DT) != 0) { 866 cpi->transport_version = 3; 867 cpi->xport_specific.spi.ppr_options = 868 SID_SPI_CLOCK_DT_ST; 869 } 870 #endif 871 cpi->ccb_h.status = CAM_REQ_CMP; 872 xpt_done(ccb); 873 break; 874 } 875 default: 876 ccb->ccb_h.status = CAM_PROVIDE_FAIL; 877 xpt_done(ccb); 878 break; 879 } 880 } 881 882 static void 883 ahc_get_tran_settings(struct ahc_softc *ahc, int our_id, char channel, 884 struct ccb_trans_settings *cts) 885 { 886 #ifdef AHC_NEW_TRAN_SETTINGS 887 struct ahc_devinfo devinfo; 888 struct ccb_trans_settings_scsi *scsi; 889 struct ccb_trans_settings_spi *spi; 890 struct ahc_initiator_tinfo *targ_info; 891 struct tmode_tstate *tstate; 892 struct ahc_transinfo *tinfo; 893 894 scsi = &cts->proto_specific.scsi; 895 spi = &cts->xport_specific.spi; 896 ahc_compile_devinfo(&devinfo, our_id, 897 cts->ccb_h.target_id, 898 cts->ccb_h.target_lun, 899 channel, ROLE_UNKNOWN); 900 targ_info = ahc_fetch_transinfo(ahc, devinfo.channel, 901 devinfo.our_scsiid, 902 devinfo.target, &tstate); 903 904 if (cts->type == CTS_TYPE_CURRENT_SETTINGS) 905 tinfo = &targ_info->current; 906 else 907 tinfo = &targ_info->user; 908 909 scsi->flags &= ~CTS_SCSI_FLAGS_TAG_ENB; 910 spi->flags &= ~CTS_SPI_FLAGS_DISC_ENB; 911 if (cts->type == CTS_TYPE_USER_SETTINGS) { 912 if ((ahc->user_discenable & devinfo.target_mask) != 0) 913 spi->flags |= CTS_SPI_FLAGS_DISC_ENB; 914 915 if ((ahc->user_tagenable & devinfo.target_mask) != 0) 916 scsi->flags |= CTS_SCSI_FLAGS_TAG_ENB; 917 } else { 918 if ((tstate->discenable & devinfo.target_mask) != 0) 919 spi->flags |= CTS_SPI_FLAGS_DISC_ENB; 920 921 if ((tstate->tagenable & devinfo.target_mask) != 0) 922 scsi->flags |= CTS_SCSI_FLAGS_TAG_ENB; 923 } 924 cts->protocol_version = tinfo->protocol_version; 925 cts->transport_version = tinfo->transport_version; 926 927 spi->sync_period = tinfo->period; 928 spi->sync_offset = tinfo->offset; 929 spi->bus_width = tinfo->width; 930 spi->ppr_options = tinfo->ppr_options; 931 932 cts->protocol = PROTO_SCSI; 933 cts->transport = XPORT_SPI; 934 spi->valid = CTS_SPI_VALID_SYNC_RATE 935 | CTS_SPI_VALID_SYNC_OFFSET 936 | CTS_SPI_VALID_BUS_WIDTH 937 | CTS_SPI_VALID_PPR_OPTIONS; 938 939 if (cts->ccb_h.target_lun != CAM_LUN_WILDCARD) { 940 scsi->valid = CTS_SCSI_VALID_TQ; 941 spi->valid |= CTS_SPI_VALID_DISC; 942 } else { 943 scsi->valid = 0; 944 } 945 946 cts->ccb_h.status = CAM_REQ_CMP; 947 #else 948 struct ahc_devinfo devinfo; 949 struct ahc_initiator_tinfo *targ_info; 950 struct tmode_tstate *tstate; 951 struct ahc_transinfo *tinfo; 952 long s; 953 954 ahc_compile_devinfo(&devinfo, our_id, 955 cts->ccb_h.target_id, 956 cts->ccb_h.target_lun, 957 channel, ROLE_UNKNOWN); 958 targ_info = ahc_fetch_transinfo(ahc, devinfo.channel, 959 devinfo.our_scsiid, 960 devinfo.target, &tstate); 961 962 if ((cts->flags & CCB_TRANS_CURRENT_SETTINGS) != 0) 963 tinfo = &targ_info->current; 964 else 965 tinfo = &targ_info->user; 966 967 ahc_lock(ahc, &s); 968 969 cts->flags &= ~(CCB_TRANS_DISC_ENB|CCB_TRANS_TAG_ENB); 970 if ((cts->flags & CCB_TRANS_CURRENT_SETTINGS) == 0) { 971 if ((ahc->user_discenable & devinfo.target_mask) != 0) 972 cts->flags |= CCB_TRANS_DISC_ENB; 973 974 if ((ahc->user_tagenable & devinfo.target_mask) != 0) 975 cts->flags |= CCB_TRANS_TAG_ENB; 976 } else { 977 if ((tstate->discenable & devinfo.target_mask) != 0) 978 cts->flags |= CCB_TRANS_DISC_ENB; 979 980 if ((tstate->tagenable & devinfo.target_mask) != 0) 981 cts->flags |= CCB_TRANS_TAG_ENB; 982 } 983 cts->sync_period = tinfo->period; 984 cts->sync_offset = tinfo->offset; 985 cts->bus_width = tinfo->width; 986 987 ahc_unlock(ahc, &s); 988 989 cts->valid = CCB_TRANS_SYNC_RATE_VALID 990 | CCB_TRANS_SYNC_OFFSET_VALID 991 | CCB_TRANS_BUS_WIDTH_VALID; 992 993 if (cts->ccb_h.target_lun != CAM_LUN_WILDCARD) 994 cts->valid |= CCB_TRANS_DISC_VALID|CCB_TRANS_TQ_VALID; 995 996 cts->ccb_h.status = CAM_REQ_CMP; 997 #endif 998 } 999 1000 static void 1001 ahc_async(void *callback_arg, uint32_t code, struct cam_path *path, void *arg) 1002 { 1003 struct ahc_softc *ahc; 1004 struct cam_sim *sim; 1005 1006 sim = (struct cam_sim *)callback_arg; 1007 ahc = (struct ahc_softc *)cam_sim_softc(sim); 1008 switch (code) { 1009 case AC_LOST_DEVICE: 1010 { 1011 struct ahc_devinfo devinfo; 1012 long s; 1013 1014 ahc_compile_devinfo(&devinfo, SIM_SCSI_ID(ahc, sim), 1015 xpt_path_target_id(path), 1016 xpt_path_lun_id(path), 1017 SIM_CHANNEL(ahc, sim), 1018 ROLE_UNKNOWN); 1019 1020 /* 1021 * Revert to async/narrow transfers 1022 * for the next device. 1023 */ 1024 ahc_lock(ahc, &s); 1025 ahc_set_width(ahc, &devinfo, MSG_EXT_WDTR_BUS_8_BIT, 1026 AHC_TRANS_GOAL|AHC_TRANS_CUR, /*paused*/FALSE); 1027 ahc_set_syncrate(ahc, &devinfo, /*syncrate*/NULL, 1028 /*period*/0, /*offset*/0, /*ppr_options*/0, 1029 AHC_TRANS_GOAL|AHC_TRANS_CUR, 1030 /*paused*/FALSE); 1031 ahc_unlock(ahc, &s); 1032 break; 1033 } 1034 default: 1035 break; 1036 } 1037 } 1038 1039 static void 1040 ahc_execute_scb(void *arg, bus_dma_segment_t *dm_segs, int nsegments, 1041 int error) 1042 { 1043 struct scb *scb; 1044 union ccb *ccb; 1045 struct ahc_softc *ahc; 1046 struct ahc_initiator_tinfo *tinfo; 1047 struct tmode_tstate *tstate; 1048 u_int mask; 1049 long s; 1050 1051 scb = (struct scb *)arg; 1052 ccb = scb->io_ctx; 1053 ahc = scb->ahc_softc; 1054 1055 if (error != 0) { 1056 if (error == EFBIG) 1057 ahc_set_transaction_status(scb, CAM_REQ_TOO_BIG); 1058 else 1059 ahc_set_transaction_status(scb, CAM_REQ_CMP_ERR); 1060 if (nsegments != 0) 1061 bus_dmamap_unload(ahc->buffer_dmat, scb->dmamap); 1062 ahc_lock(ahc, &s); 1063 ahc_free_scb(ahc, scb); 1064 ahc_unlock(ahc, &s); 1065 xpt_done(ccb); 1066 return; 1067 } 1068 if (nsegments != 0) { 1069 struct ahc_dma_seg *sg; 1070 bus_dma_segment_t *end_seg; 1071 bus_dmasync_op_t op; 1072 1073 end_seg = dm_segs + nsegments; 1074 1075 /* Copy the segments into our SG list */ 1076 sg = scb->sg_list; 1077 while (dm_segs < end_seg) { 1078 sg->addr = dm_segs->ds_addr; 1079 /* XXX Add in the 5th byte of the address later. */ 1080 sg->len = dm_segs->ds_len; 1081 sg++; 1082 dm_segs++; 1083 } 1084 1085 /* 1086 * Note where to find the SG entries in bus space. 1087 * We also set the full residual flag which the 1088 * sequencer will clear as soon as a data transfer 1089 * occurs. 1090 */ 1091 scb->hscb->sgptr = scb->sg_list_phys | SG_FULL_RESID; 1092 1093 if ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN) 1094 op = BUS_DMASYNC_PREREAD; 1095 else 1096 op = BUS_DMASYNC_PREWRITE; 1097 1098 bus_dmamap_sync(ahc->buffer_dmat, scb->dmamap, op); 1099 1100 if (ccb->ccb_h.func_code == XPT_CONT_TARGET_IO) { 1101 struct target_data *tdata; 1102 1103 tdata = &scb->hscb->shared_data.tdata; 1104 tdata->target_phases |= DPHASE_PENDING; 1105 if ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_OUT) 1106 tdata->data_phase = P_DATAOUT; 1107 else 1108 tdata->data_phase = P_DATAIN; 1109 1110 /* 1111 * If the transfer is of an odd length and in the 1112 * "in" direction (scsi->HostBus), then it may 1113 * trigger a bug in the 'WideODD' feature of 1114 * non-Ultra2 chips. Force the total data-length 1115 * to be even by adding an extra, 1 byte, SG, 1116 * element. We do this even if we are not currently 1117 * negotiated wide as negotiation could occur before 1118 * this command is executed. 1119 */ 1120 if ((ahc->bugs & AHC_TMODE_WIDEODD_BUG) != 0 1121 && (ccb->csio.dxfer_len & 0x1) != 0 1122 && (ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN) { 1123 1124 nsegments++; 1125 if (nsegments > AHC_NSEG) { 1126 1127 ahc_set_transaction_status(scb, 1128 CAM_REQ_TOO_BIG); 1129 bus_dmamap_unload(ahc->buffer_dmat, 1130 scb->dmamap); 1131 ahc_lock(ahc, &s); 1132 ahc_free_scb(ahc, scb); 1133 ahc_unlock(ahc, &s); 1134 xpt_done(ccb); 1135 return; 1136 } 1137 sg->addr = ahc->dma_bug_buf; 1138 sg->len = 1; 1139 sg++; 1140 } 1141 } 1142 sg--; 1143 sg->len |= AHC_DMA_LAST_SEG; 1144 1145 /* Copy the first SG into the "current" data pointer area */ 1146 scb->hscb->dataptr = scb->sg_list->addr; 1147 scb->hscb->datacnt = scb->sg_list->len; 1148 } else { 1149 scb->hscb->sgptr = SG_LIST_NULL; 1150 scb->hscb->dataptr = 0; 1151 scb->hscb->datacnt = 0; 1152 } 1153 1154 scb->sg_count = nsegments; 1155 1156 ahc_lock(ahc, &s); 1157 1158 /* 1159 * Last time we need to check if this SCB needs to 1160 * be aborted. 1161 */ 1162 if (ahc_get_transaction_status(scb) != CAM_REQ_INPROG) { 1163 if (nsegments != 0) 1164 bus_dmamap_unload(ahc->buffer_dmat, 1165 scb->dmamap); 1166 ahc_free_scb(ahc, scb); 1167 ahc_unlock(ahc, &s); 1168 xpt_done(ccb); 1169 return; 1170 } 1171 1172 tinfo = ahc_fetch_transinfo(ahc, SCSIID_CHANNEL(ahc, scb->hscb->scsiid), 1173 SCSIID_OUR_ID(scb->hscb->scsiid), 1174 SCSIID_TARGET(ahc, scb->hscb->scsiid), 1175 &tstate); 1176 1177 mask = SCB_GET_TARGET_MASK(ahc, scb); 1178 scb->hscb->scsirate = tinfo->scsirate; 1179 scb->hscb->scsioffset = tinfo->current.offset; 1180 if ((tstate->ultraenb & mask) != 0) 1181 scb->hscb->control |= ULTRAENB; 1182 1183 if ((tstate->discenable & mask) != 0 1184 && (ccb->ccb_h.flags & CAM_DIS_DISCONNECT) == 0) 1185 scb->hscb->control |= DISCENB; 1186 1187 if ((ccb->ccb_h.flags & CAM_NEGOTIATE) != 0 1188 && (tinfo->current.width != 0 || tinfo->current.period != 0)) { 1189 scb->flags |= SCB_NEGOTIATE; 1190 scb->hscb->control |= MK_MESSAGE; 1191 } 1192 1193 LIST_INSERT_HEAD(&ahc->pending_scbs, scb, pending_links); 1194 1195 ccb->ccb_h.status |= CAM_SIM_QUEUED; 1196 1197 if (ccb->ccb_h.timeout != CAM_TIME_INFINITY) { 1198 if (ccb->ccb_h.timeout == CAM_TIME_DEFAULT) 1199 ccb->ccb_h.timeout = 5 * 1000; 1200 ccb->ccb_h.timeout_ch = 1201 timeout(ahc_timeout, (caddr_t)scb, 1202 (ccb->ccb_h.timeout * hz) / 1000); 1203 } 1204 1205 /* 1206 * We only allow one untagged transaction 1207 * per target in the initiator role unless 1208 * we are storing a full busy target *lun* 1209 * table in SCB space. 1210 */ 1211 if ((scb->hscb->control & (TARGET_SCB|TAG_ENB)) == 0 1212 && (ahc->features & AHC_SCB_BTT) == 0) { 1213 struct scb_tailq *untagged_q; 1214 1215 untagged_q = &(ahc->untagged_queues[ccb->ccb_h.target_id]); 1216 TAILQ_INSERT_TAIL(untagged_q, scb, links.tqe); 1217 scb->flags |= SCB_UNTAGGEDQ; 1218 if (TAILQ_FIRST(untagged_q) != scb) { 1219 ahc_unlock(ahc, &s); 1220 return; 1221 } 1222 } 1223 scb->flags |= SCB_ACTIVE; 1224 1225 if ((scb->flags & SCB_TARGET_IMMEDIATE) != 0) { 1226 pause_sequencer(ahc); 1227 if ((ahc->flags & AHC_PAGESCBS) == 0) 1228 ahc_outb(ahc, SCBPTR, scb->hscb->tag); 1229 ahc_outb(ahc, SCB_TAG, scb->hscb->tag); 1230 ahc_outb(ahc, RETURN_1, CONT_MSG_LOOP); 1231 unpause_sequencer(ahc); 1232 } else { 1233 ahc_queue_scb(ahc, scb); 1234 } 1235 1236 ahc_unlock(ahc, &s); 1237 } 1238 1239 static void 1240 ahc_poll(struct cam_sim *sim) 1241 { 1242 ahc_intr(cam_sim_softc(sim)); 1243 } 1244 1245 static void 1246 ahc_setup_data(struct ahc_softc *ahc, struct cam_sim *sim, 1247 struct ccb_scsiio *csio, struct scb *scb) 1248 { 1249 struct hardware_scb *hscb; 1250 struct ccb_hdr *ccb_h; 1251 1252 hscb = scb->hscb; 1253 ccb_h = &csio->ccb_h; 1254 1255 if (ccb_h->func_code == XPT_SCSI_IO) { 1256 hscb->cdb_len = csio->cdb_len; 1257 if ((ccb_h->flags & CAM_CDB_POINTER) != 0) { 1258 1259 if (hscb->cdb_len > sizeof(hscb->cdb32) 1260 || (ccb_h->flags & CAM_CDB_PHYS) != 0) { 1261 u_long s; 1262 1263 ahc_set_transaction_status(scb, 1264 CAM_REQ_INVALID); 1265 ahc_lock(ahc, &s); 1266 ahc_free_scb(ahc, scb); 1267 ahc_unlock(ahc, &s); 1268 xpt_done((union ccb *)csio); 1269 return; 1270 } 1271 if (hscb->cdb_len > 12) { 1272 memcpy(hscb->cdb32, 1273 csio->cdb_io.cdb_ptr, 1274 hscb->cdb_len); 1275 scb->flags |= SCB_CDB32_PTR; 1276 } else { 1277 memcpy(hscb->shared_data.cdb, 1278 csio->cdb_io.cdb_ptr, 1279 hscb->cdb_len); 1280 } 1281 } else { 1282 if (hscb->cdb_len > 12) { 1283 memcpy(hscb->cdb32, csio->cdb_io.cdb_bytes, 1284 hscb->cdb_len); 1285 scb->flags |= SCB_CDB32_PTR; 1286 } else { 1287 memcpy(hscb->shared_data.cdb, 1288 csio->cdb_io.cdb_bytes, 1289 hscb->cdb_len); 1290 } 1291 } 1292 } 1293 1294 /* Only use S/G if there is a transfer */ 1295 if ((ccb_h->flags & CAM_DIR_MASK) != CAM_DIR_NONE) { 1296 if ((ccb_h->flags & CAM_SCATTER_VALID) == 0) { 1297 /* We've been given a pointer to a single buffer */ 1298 if ((ccb_h->flags & CAM_DATA_PHYS) == 0) { 1299 int s; 1300 int error; 1301 1302 s = splsoftvm(); 1303 error = bus_dmamap_load(ahc->buffer_dmat, 1304 scb->dmamap, 1305 csio->data_ptr, 1306 csio->dxfer_len, 1307 ahc_execute_scb, 1308 scb, /*flags*/0); 1309 if (error == EINPROGRESS) { 1310 /* 1311 * So as to maintain ordering, 1312 * freeze the controller queue 1313 * until our mapping is 1314 * returned. 1315 */ 1316 xpt_freeze_simq(sim, 1317 /*count*/1); 1318 scb->io_ctx->ccb_h.status |= 1319 CAM_RELEASE_SIMQ; 1320 } 1321 splx(s); 1322 } else { 1323 struct bus_dma_segment seg; 1324 1325 /* Pointer to physical buffer */ 1326 if (csio->dxfer_len > AHC_MAXTRANSFER_SIZE) 1327 panic("ahc_setup_data - Transfer size " 1328 "larger than can device max"); 1329 1330 seg.ds_addr = (bus_addr_t)csio->data_ptr; 1331 seg.ds_len = csio->dxfer_len; 1332 ahc_execute_scb(scb, &seg, 1, 0); 1333 } 1334 } else { 1335 struct bus_dma_segment *segs; 1336 1337 if ((ccb_h->flags & CAM_DATA_PHYS) != 0) 1338 panic("ahc_setup_data - Physical segment " 1339 "pointers unsupported"); 1340 1341 if ((ccb_h->flags & CAM_SG_LIST_PHYS) == 0) 1342 panic("ahc_setup_data - Virtual segment " 1343 "addresses unsupported"); 1344 1345 /* Just use the segments provided */ 1346 segs = (struct bus_dma_segment *)csio->data_ptr; 1347 ahc_execute_scb(scb, segs, csio->sglist_cnt, 0); 1348 } 1349 } else { 1350 ahc_execute_scb(scb, NULL, 0, 0); 1351 } 1352 } 1353 1354 static void 1355 ahc_set_recoveryscb(struct ahc_softc *ahc, struct scb *scb) { 1356 1357 if ((scb->flags & SCB_RECOVERY_SCB) == 0) { 1358 struct scb *list_scb; 1359 1360 scb->flags |= SCB_RECOVERY_SCB; 1361 1362 /* 1363 * Take all queued, but not sent SCBs out of the equation. 1364 * Also ensure that no new CCBs are queued to us while we 1365 * try to fix this problem. 1366 */ 1367 if ((scb->io_ctx->ccb_h.status & CAM_RELEASE_SIMQ) == 0) { 1368 xpt_freeze_simq(SCB_GET_SIM(ahc, scb), /*count*/1); 1369 scb->io_ctx->ccb_h.status |= CAM_RELEASE_SIMQ; 1370 } 1371 1372 /* 1373 * Go through all of our pending SCBs and remove 1374 * any scheduled timeouts for them. We will reschedule 1375 * them after we've successfully fixed this problem. 1376 */ 1377 LIST_FOREACH(list_scb, &ahc->pending_scbs, pending_links) { 1378 union ccb *ccb; 1379 1380 ccb = list_scb->io_ctx; 1381 untimeout(ahc_timeout, list_scb, ccb->ccb_h.timeout_ch); 1382 } 1383 } 1384 } 1385 1386 void 1387 ahc_timeout(void *arg) 1388 { 1389 struct scb *scb; 1390 struct ahc_softc *ahc; 1391 long s; 1392 int found; 1393 u_int last_phase; 1394 int target; 1395 int lun; 1396 int i; 1397 char channel; 1398 1399 scb = (struct scb *)arg; 1400 ahc = (struct ahc_softc *)scb->ahc_softc; 1401 1402 ahc_lock(ahc, &s); 1403 1404 /* 1405 * Ensure that the card doesn't do anything 1406 * behind our back. Also make sure that we 1407 * didn't "just" miss an interrupt that would 1408 * affect this timeout. 1409 */ 1410 do { 1411 ahc_freebsd_intr(ahc); 1412 pause_sequencer(ahc); 1413 } while (ahc_inb(ahc, INTSTAT) & INT_PEND); 1414 1415 /* Make sure the sequencer is in a safe location. */ 1416 ahc_clear_critical_section(ahc); 1417 1418 if ((scb->flags & SCB_ACTIVE) == 0) { 1419 /* Previous timeout took care of me already */ 1420 printf("%s: Timedout SCB already complete. " 1421 "Interrupts may not be functioning.\n", ahc_name(ahc)); 1422 unpause_sequencer(ahc); 1423 ahc_unlock(ahc, &s); 1424 return; 1425 } 1426 1427 target = SCB_GET_TARGET(ahc, scb); 1428 channel = SCB_GET_CHANNEL(ahc, scb); 1429 lun = SCB_GET_LUN(scb); 1430 1431 ahc_print_path(ahc, scb); 1432 printf("SCB 0x%x - timed out ", scb->hscb->tag); 1433 /* 1434 * Take a snapshot of the bus state and print out 1435 * some information so we can track down driver bugs. 1436 */ 1437 last_phase = ahc_inb(ahc, LASTPHASE); 1438 1439 for (i = 0; i < num_phases; i++) { 1440 if (last_phase == phase_table[i].phase) 1441 break; 1442 } 1443 printf("%s", phase_table[i].phasemsg); 1444 1445 printf(", SEQADDR == 0x%x\n", 1446 ahc_inb(ahc, SEQADDR0) | (ahc_inb(ahc, SEQADDR1) << 8)); 1447 1448 printf("STACK == 0x%x, 0x%x, 0x%x, 0x%x\n", 1449 ahc_inb(ahc, STACK) | (ahc_inb(ahc, STACK) << 8), 1450 ahc_inb(ahc, STACK) | (ahc_inb(ahc, STACK) << 8), 1451 ahc_inb(ahc, STACK) | (ahc_inb(ahc, STACK) << 8), 1452 ahc_inb(ahc, STACK) | (ahc_inb(ahc, STACK) << 8)); 1453 1454 printf("SXFRCTL0 == 0x%x\n", ahc_inb(ahc, SXFRCTL0)); 1455 1456 ahc_dump_card_state(ahc); 1457 if (scb->sg_count > 0) { 1458 for (i = 0; i < scb->sg_count; i++) { 1459 printf("sg[%d] - Addr 0x%x : Length %d\n", 1460 i, 1461 scb->sg_list[i].addr, 1462 scb->sg_list[i].len & AHC_SG_LEN_MASK); 1463 } 1464 } 1465 if (scb->flags & (SCB_DEVICE_RESET|SCB_ABORT)) { 1466 /* 1467 * Been down this road before. 1468 * Do a full bus reset. 1469 */ 1470 bus_reset: 1471 ahc_set_transaction_status(scb, CAM_CMD_TIMEOUT); 1472 found = ahc_reset_channel(ahc, channel, /*Initiate Reset*/TRUE); 1473 printf("%s: Issued Channel %c Bus Reset. " 1474 "%d SCBs aborted\n", ahc_name(ahc), channel, found); 1475 } else { 1476 /* 1477 * If we are a target, transition to bus free and report 1478 * the timeout. 1479 * 1480 * The target/initiator that is holding up the bus may not 1481 * be the same as the one that triggered this timeout 1482 * (different commands have different timeout lengths). 1483 * If the bus is idle and we are actiing as the initiator 1484 * for this request, queue a BDR message to the timed out 1485 * target. Otherwise, if the timed out transaction is 1486 * active: 1487 * Initiator transaction: 1488 * Stuff the message buffer with a BDR message and assert 1489 * ATN in the hopes that the target will let go of the bus 1490 * and go to the mesgout phase. If this fails, we'll 1491 * get another timeout 2 seconds later which will attempt 1492 * a bus reset. 1493 * 1494 * Target transaction: 1495 * Transition to BUS FREE and report the error. 1496 * It's good to be the target! 1497 */ 1498 u_int active_scb_index; 1499 u_int saved_scbptr; 1500 1501 saved_scbptr = ahc_inb(ahc, SCBPTR); 1502 active_scb_index = ahc_inb(ahc, SCB_TAG); 1503 1504 if (last_phase != P_BUSFREE 1505 && (ahc_inb(ahc, SEQ_FLAGS) & IDENTIFY_SEEN) != 0 1506 && (active_scb_index < ahc->scb_data->numscbs)) { 1507 struct scb *active_scb; 1508 1509 /* 1510 * If the active SCB is not us, assume that 1511 * the active SCB has a longer timeout than 1512 * the timedout SCB, and wait for the active 1513 * SCB to timeout. 1514 */ 1515 active_scb = ahc_lookup_scb(ahc, active_scb_index); 1516 if (active_scb != scb) { 1517 struct ccb_hdr *ccbh; 1518 u_int newtimeout; 1519 1520 ahc_print_path(ahc, scb); 1521 printf("Other SCB Timeout%s", 1522 (scb->flags & SCB_OTHERTCL_TIMEOUT) != 0 1523 ? " again\n" : "\n"); 1524 scb->flags |= SCB_OTHERTCL_TIMEOUT; 1525 newtimeout = 1526 MAX(active_scb->io_ctx->ccb_h.timeout, 1527 scb->io_ctx->ccb_h.timeout); 1528 ccbh = &scb->io_ctx->ccb_h; 1529 scb->io_ctx->ccb_h.timeout_ch = 1530 timeout(ahc_timeout, scb, 1531 (newtimeout * hz) / 1000); 1532 ahc_unlock(ahc, &s); 1533 return; 1534 } 1535 1536 /* It's us */ 1537 if ((scb->hscb->control & TARGET_SCB) != 0) { 1538 1539 /* 1540 * Send back any queued up transactions 1541 * and properly record the error condition. 1542 */ 1543 ahc_freeze_devq(ahc, scb); 1544 ahc_set_transaction_status(scb, 1545 CAM_CMD_TIMEOUT); 1546 ahc_freeze_scb(scb); 1547 ahc_done(ahc, scb); 1548 1549 /* Will clear us from the bus */ 1550 restart_sequencer(ahc); 1551 ahc_unlock(ahc, &s); 1552 return; 1553 } 1554 1555 ahc_set_recoveryscb(ahc, active_scb); 1556 ahc_outb(ahc, MSG_OUT, HOST_MSG); 1557 ahc_outb(ahc, SCSISIGO, last_phase|ATNO); 1558 ahc_print_path(ahc, active_scb); 1559 printf("BDR message in message buffer\n"); 1560 active_scb->flags |= SCB_DEVICE_RESET; 1561 active_scb->io_ctx->ccb_h.timeout_ch = 1562 timeout(ahc_timeout, (caddr_t)active_scb, 2 * hz); 1563 unpause_sequencer(ahc); 1564 } else { 1565 int disconnected; 1566 1567 /* XXX Shouldn't panic. Just punt instead */ 1568 if ((scb->hscb->control & TARGET_SCB) != 0) 1569 panic("Timed-out target SCB but bus idle"); 1570 1571 if (last_phase != P_BUSFREE 1572 && (ahc_inb(ahc, SSTAT0) & TARGET) != 0) { 1573 /* XXX What happened to the SCB? */ 1574 /* Hung target selection. Goto busfree */ 1575 printf("%s: Hung target selection\n", 1576 ahc_name(ahc)); 1577 restart_sequencer(ahc); 1578 ahc_unlock(ahc, &s); 1579 return; 1580 } 1581 1582 if (ahc_search_qinfifo(ahc, target, channel, lun, 1583 scb->hscb->tag, ROLE_INITIATOR, 1584 /*status*/0, SEARCH_COUNT) > 0) { 1585 disconnected = FALSE; 1586 } else { 1587 disconnected = TRUE; 1588 } 1589 1590 if (disconnected) { 1591 1592 ahc_set_recoveryscb(ahc, scb); 1593 /* 1594 * Actually re-queue this SCB in an attempt 1595 * to select the device before it reconnects. 1596 * In either case (selection or reselection), 1597 * we will now issue a target reset to the 1598 * timed-out device. 1599 * 1600 * Set the MK_MESSAGE control bit indicating 1601 * that we desire to send a message. We 1602 * also set the disconnected flag since 1603 * in the paging case there is no guarantee 1604 * that our SCB control byte matches the 1605 * version on the card. We don't want the 1606 * sequencer to abort the command thinking 1607 * an unsolicited reselection occurred. 1608 */ 1609 scb->hscb->control |= MK_MESSAGE|DISCONNECTED; 1610 scb->flags |= SCB_DEVICE_RESET; 1611 1612 /* 1613 * Remove any cached copy of this SCB in the 1614 * disconnected list in preparation for the 1615 * queuing of our abort SCB. We use the 1616 * same element in the SCB, SCB_NEXT, for 1617 * both the qinfifo and the disconnected list. 1618 */ 1619 ahc_search_disc_list(ahc, target, channel, 1620 lun, scb->hscb->tag, 1621 /*stop_on_first*/TRUE, 1622 /*remove*/TRUE, 1623 /*save_state*/FALSE); 1624 1625 /* 1626 * In the non-paging case, the sequencer will 1627 * never re-reference the in-core SCB. 1628 * To make sure we are notified during 1629 * reslection, set the MK_MESSAGE flag in 1630 * the card's copy of the SCB. 1631 */ 1632 if ((ahc->flags & AHC_PAGESCBS) == 0) { 1633 ahc_outb(ahc, SCBPTR, scb->hscb->tag); 1634 ahc_outb(ahc, SCB_CONTROL, 1635 ahc_inb(ahc, SCB_CONTROL) 1636 | MK_MESSAGE); 1637 } 1638 1639 /* 1640 * Clear out any entries in the QINFIFO first 1641 * so we are the next SCB for this target 1642 * to run. 1643 */ 1644 ahc_search_qinfifo(ahc, 1645 SCB_GET_TARGET(ahc, scb), 1646 channel, SCB_GET_LUN(scb), 1647 SCB_LIST_NULL, 1648 ROLE_INITIATOR, 1649 CAM_REQUEUE_REQ, 1650 SEARCH_COMPLETE); 1651 ahc_print_path(ahc, scb); 1652 printf("Queuing a BDR SCB\n"); 1653 ahc_qinfifo_requeue_tail(ahc, scb); 1654 ahc_outb(ahc, SCBPTR, saved_scbptr); 1655 scb->io_ctx->ccb_h.timeout_ch = 1656 timeout(ahc_timeout, (caddr_t)scb, 2 * hz); 1657 unpause_sequencer(ahc); 1658 } else { 1659 /* Go "immediatly" to the bus reset */ 1660 /* This shouldn't happen */ 1661 ahc_set_recoveryscb(ahc, scb); 1662 ahc_print_path(ahc, scb); 1663 printf("SCB %d: Immediate reset. " 1664 "Flags = 0x%x\n", scb->hscb->tag, 1665 scb->flags); 1666 goto bus_reset; 1667 } 1668 } 1669 } 1670 ahc_unlock(ahc, &s); 1671 } 1672 1673 static void 1674 ahc_abort_ccb(struct ahc_softc *ahc, struct cam_sim *sim, union ccb *ccb) 1675 { 1676 union ccb *abort_ccb; 1677 1678 abort_ccb = ccb->cab.abort_ccb; 1679 switch (abort_ccb->ccb_h.func_code) { 1680 case XPT_ACCEPT_TARGET_IO: 1681 case XPT_IMMED_NOTIFY: 1682 case XPT_CONT_TARGET_IO: 1683 { 1684 struct tmode_tstate *tstate; 1685 struct tmode_lstate *lstate; 1686 struct ccb_hdr_slist *list; 1687 cam_status status; 1688 1689 status = ahc_find_tmode_devs(ahc, sim, abort_ccb, &tstate, 1690 &lstate, TRUE); 1691 1692 if (status != CAM_REQ_CMP) { 1693 ccb->ccb_h.status = status; 1694 break; 1695 } 1696 1697 if (abort_ccb->ccb_h.func_code == XPT_ACCEPT_TARGET_IO) 1698 list = &lstate->accept_tios; 1699 else if (abort_ccb->ccb_h.func_code == XPT_IMMED_NOTIFY) 1700 list = &lstate->immed_notifies; 1701 else 1702 list = NULL; 1703 1704 if (list != NULL) { 1705 struct ccb_hdr *curelm; 1706 int found; 1707 1708 curelm = SLIST_FIRST(list); 1709 found = 0; 1710 if (curelm == &abort_ccb->ccb_h) { 1711 found = 1; 1712 SLIST_REMOVE_HEAD(list, sim_links.sle); 1713 } else { 1714 while(curelm != NULL) { 1715 struct ccb_hdr *nextelm; 1716 1717 nextelm = 1718 SLIST_NEXT(curelm, sim_links.sle); 1719 1720 if (nextelm == &abort_ccb->ccb_h) { 1721 found = 1; 1722 SLIST_NEXT(curelm, 1723 sim_links.sle) = 1724 SLIST_NEXT(nextelm, 1725 sim_links.sle); 1726 break; 1727 } 1728 curelm = nextelm; 1729 } 1730 } 1731 1732 if (found) { 1733 abort_ccb->ccb_h.status = CAM_REQ_ABORTED; 1734 xpt_done(abort_ccb); 1735 ccb->ccb_h.status = CAM_REQ_CMP; 1736 } else { 1737 xpt_print_path(abort_ccb->ccb_h.path); 1738 printf("Not found\n"); 1739 ccb->ccb_h.status = CAM_PATH_INVALID; 1740 } 1741 break; 1742 } 1743 /* FALLTHROUGH */ 1744 } 1745 case XPT_SCSI_IO: 1746 /* XXX Fully implement the hard ones */ 1747 ccb->ccb_h.status = CAM_UA_ABORT; 1748 break; 1749 default: 1750 ccb->ccb_h.status = CAM_REQ_INVALID; 1751 break; 1752 } 1753 xpt_done(ccb); 1754 } 1755 1756 void 1757 ahc_send_async(struct ahc_softc *ahc, char channel, u_int target, 1758 u_int lun, ac_code code) 1759 { 1760 struct ccb_trans_settings cts; 1761 struct cam_path *path; 1762 void *arg; 1763 int error; 1764 1765 arg = NULL; 1766 error = ahc_create_path(ahc, channel, target, lun, &path); 1767 1768 if (error != CAM_REQ_CMP) 1769 return; 1770 1771 switch (code) { 1772 case AC_TRANSFER_NEG: 1773 #ifdef AHC_NEW_TRAN_SETTINGS 1774 cts.type = CTS_TYPE_CURRENT_SETTINGS; 1775 #else 1776 cts.flags = CCB_TRANS_CURRENT_SETTINGS; 1777 #endif 1778 cts.ccb_h.path = path; 1779 cts.ccb_h.target_id = target; 1780 cts.ccb_h.target_lun = lun; 1781 ahc_get_tran_settings(ahc, channel == 'A' ? ahc->our_id 1782 : ahc->our_id_b, 1783 channel, &cts); 1784 arg = &cts; 1785 break; 1786 case AC_SENT_BDR: 1787 case AC_BUS_RESET: 1788 break; 1789 default: 1790 panic("ahc_send_async: Unexpected async event"); 1791 } 1792 xpt_async(code, path, arg); 1793 xpt_free_path(path); 1794 } 1795 1796 void 1797 ahc_platform_set_tags(struct ahc_softc *ahc, 1798 struct ahc_devinfo *devinfo, int enable) 1799 { 1800 } 1801 1802 int 1803 ahc_platform_alloc(struct ahc_softc *ahc, void *platform_arg) 1804 { 1805 ahc->platform_data = malloc(sizeof(struct ahc_platform_data), M_DEVBUF, 1806 M_NOWAIT | M_ZERO); 1807 if (ahc->platform_data == NULL) 1808 return (ENOMEM); 1809 return (0); 1810 } 1811 1812 void 1813 ahc_platform_free(struct ahc_softc *ahc) 1814 { 1815 if (ahc->platform_data != NULL) { 1816 if (ahc->platform_data->regs != NULL) 1817 bus_release_resource(ahc->dev_softc, 1818 ahc->platform_data->regs_res_type, 1819 ahc->platform_data->regs_res_id, 1820 ahc->platform_data->regs); 1821 1822 if (ahc->platform_data->irq != NULL) 1823 bus_release_resource(ahc->dev_softc, 1824 ahc->platform_data->irq_res_type, 1825 0, ahc->platform_data->irq); 1826 1827 free(ahc->platform_data, M_DEVBUF); 1828 } 1829 } 1830 1831 int 1832 ahc_softc_comp(struct ahc_softc *lahc, struct ahc_softc *rahc) 1833 { 1834 /* We don't sort softcs under FreeBSD so report equal always */ 1835 return (0); 1836 } 1837 1838 #if UNUSED 1839 static void 1840 ahc_dump_targcmd(struct target_cmd *cmd) 1841 { 1842 uint8_t *byte; 1843 uint8_t *last_byte; 1844 int i; 1845 1846 byte = &cmd->initiator_channel; 1847 /* Debugging info for received commands */ 1848 last_byte = &cmd[1].initiator_channel; 1849 1850 i = 0; 1851 while (byte < last_byte) { 1852 if (i == 0) 1853 printf("\t"); 1854 printf("%#x", *byte++); 1855 i++; 1856 if (i == 8) { 1857 printf("\n"); 1858 i = 0; 1859 } else { 1860 printf(", "); 1861 } 1862 } 1863 } 1864 #endif 1865