1 /*- 2 * Copyright (c) 2002 Adaptec, Inc. 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24 * SUCH DAMAGE. 25 */ 26 27 #include <sys/cdefs.h> 28 __FBSDID("$FreeBSD$"); 29 30 /* 31 * CAM front-end for communicating with non-DASD devices 32 */ 33 34 #include "opt_aac.h" 35 36 #include <sys/param.h> 37 #include <sys/systm.h> 38 #include <sys/kernel.h> 39 #include <sys/sysctl.h> 40 #include <sys/malloc.h> 41 #include <sys/module.h> 42 43 #include <cam/cam.h> 44 #include <cam/cam_ccb.h> 45 #include <cam/cam_debug.h> 46 #include <cam/cam_sim.h> 47 #include <cam/cam_xpt_sim.h> 48 #include <cam/scsi/scsi_all.h> 49 #include <cam/scsi/scsi_message.h> 50 51 #include <sys/bus.h> 52 #include <sys/conf.h> 53 #include <sys/disk.h> 54 55 #include <machine/md_var.h> 56 #include <machine/bus.h> 57 #include <sys/rman.h> 58 59 #include <vm/vm.h> 60 #include <vm/pmap.h> 61 62 #include <dev/aac/aacreg.h> 63 #include <sys/aac_ioctl.h> 64 #include <dev/aac/aacvar.h> 65 66 struct aac_cam { 67 device_t dev; 68 struct aac_sim *inf; 69 struct cam_sim *sim; 70 struct cam_path *path; 71 }; 72 73 static int aac_cam_probe(device_t dev); 74 static int aac_cam_attach(device_t dev); 75 static int aac_cam_detach(device_t dev); 76 static void aac_cam_action(struct cam_sim *, union ccb *); 77 static void aac_cam_poll(struct cam_sim *); 78 static void aac_cam_complete(struct aac_command *); 79 static u_int32_t aac_cam_reset_bus(struct cam_sim *, union ccb *); 80 static u_int32_t aac_cam_abort_ccb(struct cam_sim *, union ccb *); 81 static u_int32_t aac_cam_term_io(struct cam_sim *, union ccb *); 82 83 static devclass_t aac_pass_devclass; 84 85 static device_method_t aac_pass_methods[] = { 86 DEVMETHOD(device_probe, aac_cam_probe), 87 DEVMETHOD(device_attach, aac_cam_attach), 88 DEVMETHOD(device_detach, aac_cam_detach), 89 { 0, 0 } 90 }; 91 92 static driver_t aac_pass_driver = { 93 "aacp", 94 aac_pass_methods, 95 sizeof(struct aac_cam) 96 }; 97 98 DRIVER_MODULE(aacp, aac, aac_pass_driver, aac_pass_devclass, 0, 0); 99 MODULE_DEPEND(aacp, cam, 1, 1, 1); 100 101 MALLOC_DEFINE(M_AACCAM, "aaccam", "AAC CAM info"); 102 103 static void 104 aac_cam_event(struct aac_softc *sc, struct aac_event *event, void *arg) 105 { 106 union ccb *ccb; 107 struct aac_cam *camsc; 108 109 switch (event->ev_type) { 110 case AAC_EVENT_CMFREE: 111 ccb = arg; 112 camsc = ccb->ccb_h.sim_priv.entries[0].ptr; 113 free(event, M_AACCAM); 114 xpt_release_simq(camsc->sim, 1); 115 ccb->ccb_h.status = CAM_REQUEUE_REQ; 116 xpt_done(ccb); 117 break; 118 default: 119 device_printf(sc->aac_dev, "unknown event %d in aac_cam\n", 120 event->ev_type); 121 break; 122 } 123 124 return; 125 } 126 127 static int 128 aac_cam_probe(device_t dev) 129 { 130 fwprintf(NULL, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, ""); 131 132 return (0); 133 } 134 135 static int 136 aac_cam_detach(device_t dev) 137 { 138 struct aac_softc *sc; 139 struct aac_cam *camsc; 140 fwprintf(NULL, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, ""); 141 142 camsc = (struct aac_cam *)device_get_softc(dev); 143 sc = camsc->inf->aac_sc; 144 145 mtx_lock(&sc->aac_io_lock); 146 147 xpt_async(AC_LOST_DEVICE, camsc->path, NULL); 148 xpt_free_path(camsc->path); 149 xpt_bus_deregister(cam_sim_path(camsc->sim)); 150 cam_sim_free(camsc->sim, /*free_devq*/TRUE); 151 152 mtx_unlock(&sc->aac_io_lock); 153 154 return (0); 155 } 156 157 /* 158 * Register the driver as a CAM SIM 159 */ 160 static int 161 aac_cam_attach(device_t dev) 162 { 163 struct cam_devq *devq; 164 struct cam_sim *sim; 165 struct cam_path *path; 166 struct aac_cam *camsc; 167 struct aac_sim *inf; 168 169 fwprintf(NULL, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, ""); 170 171 camsc = (struct aac_cam *)device_get_softc(dev); 172 inf = (struct aac_sim *)device_get_ivars(dev); 173 camsc->inf = inf; 174 175 devq = cam_simq_alloc(inf->TargetsPerBus); 176 if (devq == NULL) 177 return (EIO); 178 179 sim = cam_sim_alloc(aac_cam_action, aac_cam_poll, "aacp", camsc, 180 device_get_unit(dev), &inf->aac_sc->aac_io_lock, 1, 1, devq); 181 if (sim == NULL) { 182 cam_simq_free(devq); 183 return (EIO); 184 } 185 186 /* Since every bus has it's own sim, every bus 'appears' as bus 0 */ 187 mtx_lock(&inf->aac_sc->aac_io_lock); 188 if (xpt_bus_register(sim, dev, 0) != CAM_SUCCESS) { 189 cam_sim_free(sim, TRUE); 190 mtx_unlock(&inf->aac_sc->aac_io_lock); 191 return (EIO); 192 } 193 194 if (xpt_create_path(&path, NULL, cam_sim_path(sim), 195 CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD) != CAM_REQ_CMP) { 196 xpt_bus_deregister(cam_sim_path(sim)); 197 cam_sim_free(sim, TRUE); 198 mtx_unlock(&inf->aac_sc->aac_io_lock); 199 return (EIO); 200 } 201 mtx_unlock(&inf->aac_sc->aac_io_lock); 202 203 camsc->sim = sim; 204 camsc->path = path; 205 206 return (0); 207 } 208 209 static void 210 aac_cam_action(struct cam_sim *sim, union ccb *ccb) 211 { 212 struct aac_cam *camsc; 213 struct aac_softc *sc; 214 struct aac_srb *srb; 215 struct aac_fib *fib; 216 struct aac_command *cm; 217 218 camsc = (struct aac_cam *)cam_sim_softc(sim); 219 sc = camsc->inf->aac_sc; 220 fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, ""); 221 222 /* Synchronous ops, and ops that don't require communication with the 223 * controller */ 224 switch(ccb->ccb_h.func_code) { 225 case XPT_SCSI_IO: 226 case XPT_RESET_DEV: 227 /* These are handled down below */ 228 break; 229 case XPT_CALC_GEOMETRY: 230 { 231 struct ccb_calc_geometry *ccg; 232 u_int32_t size_mb; 233 u_int32_t secs_per_cylinder; 234 235 ccg = &ccb->ccg; 236 size_mb = ccg->volume_size / 237 ((1024L * 1024L) / ccg->block_size); 238 if (size_mb >= (2 * 1024)) { /* 2GB */ 239 ccg->heads = 255; 240 ccg->secs_per_track = 63; 241 } else if (size_mb >= (1 * 1024)) { /* 1GB */ 242 ccg->heads = 128; 243 ccg->secs_per_track = 32; 244 } else { 245 ccg->heads = 64; 246 ccg->secs_per_track = 32; 247 } 248 secs_per_cylinder = ccg->heads * ccg->secs_per_track; 249 ccg->cylinders = ccg->volume_size / secs_per_cylinder; 250 251 ccb->ccb_h.status = CAM_REQ_CMP; 252 xpt_done(ccb); 253 return; 254 } 255 case XPT_PATH_INQ: 256 { 257 struct ccb_pathinq *cpi = &ccb->cpi; 258 259 cpi->version_num = 1; 260 cpi->hba_inquiry = PI_WIDE_16; 261 cpi->target_sprt = 0; 262 263 /* 264 * Resetting via the passthrough or parallel bus scan 265 * causes problems. 266 */ 267 cpi->hba_misc = PIM_NOBUSRESET | PIM_SEQSCAN; 268 cpi->hba_eng_cnt = 0; 269 cpi->max_target = camsc->inf->TargetsPerBus; 270 cpi->max_lun = 8; /* Per the controller spec */ 271 cpi->initiator_id = camsc->inf->InitiatorBusId; 272 cpi->bus_id = camsc->inf->BusNumber; 273 cpi->base_transfer_speed = 3300; 274 strncpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN); 275 strncpy(cpi->hba_vid, "Adaptec", HBA_IDLEN); 276 strncpy(cpi->dev_name, cam_sim_name(sim), DEV_IDLEN); 277 cpi->unit_number = cam_sim_unit(sim); 278 cpi->transport = XPORT_SPI; 279 cpi->transport_version = 2; 280 cpi->protocol = PROTO_SCSI; 281 cpi->protocol_version = SCSI_REV_2; 282 ccb->ccb_h.status = CAM_REQ_CMP; 283 xpt_done(ccb); 284 return; 285 } 286 case XPT_GET_TRAN_SETTINGS: 287 { 288 struct ccb_trans_settings_scsi *scsi = 289 &ccb->cts.proto_specific.scsi; 290 struct ccb_trans_settings_spi *spi = 291 &ccb->cts.xport_specific.spi; 292 ccb->cts.protocol = PROTO_SCSI; 293 ccb->cts.protocol_version = SCSI_REV_2; 294 ccb->cts.transport = XPORT_SPI; 295 ccb->cts.transport_version = 2; 296 if (ccb->ccb_h.target_lun != CAM_LUN_WILDCARD) { 297 scsi->valid = CTS_SCSI_VALID_TQ; 298 spi->valid |= CTS_SPI_VALID_DISC; 299 } else { 300 scsi->valid = 0; 301 } 302 ccb->ccb_h.status = CAM_REQ_CMP; 303 xpt_done(ccb); 304 return; 305 } 306 case XPT_SET_TRAN_SETTINGS: 307 ccb->ccb_h.status = CAM_FUNC_NOTAVAIL; 308 xpt_done(ccb); 309 return; 310 case XPT_RESET_BUS: 311 if (!(sc->flags & AAC_FLAGS_CAM_NORESET)) { 312 ccb->ccb_h.status = aac_cam_reset_bus(sim, ccb); 313 } else { 314 ccb->ccb_h.status = CAM_REQ_CMP; 315 } 316 xpt_done(ccb); 317 return; 318 case XPT_ABORT: 319 ccb->ccb_h.status = aac_cam_abort_ccb(sim, ccb); 320 xpt_done(ccb); 321 return; 322 case XPT_TERM_IO: 323 ccb->ccb_h.status = aac_cam_term_io(sim, ccb); 324 xpt_done(ccb); 325 return; 326 default: 327 device_printf(sc->aac_dev, "Unsupported command 0x%x\n", 328 ccb->ccb_h.func_code); 329 ccb->ccb_h.status = CAM_PROVIDE_FAIL; 330 xpt_done(ccb); 331 return; 332 } 333 334 /* Async ops that require communcation with the controller */ 335 336 if (aac_alloc_command(sc, &cm)) { 337 struct aac_event *event; 338 339 xpt_freeze_simq(sim, 1); 340 ccb->ccb_h.status = CAM_RESRC_UNAVAIL; 341 ccb->ccb_h.sim_priv.entries[0].ptr = camsc; 342 event = malloc(sizeof(struct aac_event), M_AACCAM, 343 M_NOWAIT | M_ZERO); 344 if (event == NULL) { 345 device_printf(sc->aac_dev, 346 "Warning, out of memory for event\n"); 347 return; 348 } 349 event->ev_callback = aac_cam_event; 350 event->ev_arg = ccb; 351 event->ev_type = AAC_EVENT_CMFREE; 352 aac_add_event(sc, event); 353 return; 354 } 355 356 fib = cm->cm_fib; 357 srb = (struct aac_srb *)&fib->data[0]; 358 cm->cm_datalen = 0; 359 360 switch (ccb->ccb_h.flags & CAM_DIR_MASK) { 361 case CAM_DIR_IN: 362 srb->flags = AAC_SRB_FLAGS_DATA_IN; 363 cm->cm_flags |= AAC_CMD_DATAIN; 364 break; 365 case CAM_DIR_OUT: 366 srb->flags = AAC_SRB_FLAGS_DATA_OUT; 367 cm->cm_flags |= AAC_CMD_DATAOUT; 368 break; 369 case CAM_DIR_NONE: 370 srb->flags = AAC_SRB_FLAGS_NO_DATA_XFER; 371 break; 372 default: 373 srb->flags = AAC_SRB_FLAGS_UNSPECIFIED_DIRECTION; 374 cm->cm_flags |= AAC_CMD_DATAIN | AAC_CMD_DATAOUT; 375 break; 376 } 377 378 switch(ccb->ccb_h.func_code) { 379 case XPT_SCSI_IO: 380 { 381 struct ccb_scsiio *csio = &ccb->csio; 382 383 srb->function = AAC_SRB_FUNC_EXECUTE_SCSI; 384 385 /* 386 * Copy the CDB into the SRB. It's only 6-16 bytes, 387 * so a copy is not too expensive. 388 */ 389 srb->cdb_len = csio->cdb_len; 390 if (ccb->ccb_h.flags & CAM_CDB_POINTER) 391 bcopy(csio->cdb_io.cdb_ptr, (u_int8_t *)&srb->cdb[0], 392 srb->cdb_len); 393 else 394 bcopy(csio->cdb_io.cdb_bytes, (u_int8_t *)&srb->cdb[0], 395 srb->cdb_len); 396 397 /* Set command */ 398 fib->Header.Command = (sc->flags & AAC_FLAGS_SG_64BIT) ? 399 ScsiPortCommandU64 : ScsiPortCommand; 400 401 /* Map the s/g list. XXX 32bit addresses only! */ 402 if ((ccb->ccb_h.flags & CAM_DIR_MASK) != CAM_DIR_NONE) { 403 if ((ccb->ccb_h.flags & CAM_SCATTER_VALID) == 0) { 404 srb->data_len = csio->dxfer_len; 405 if (ccb->ccb_h.flags & CAM_DATA_PHYS) { 406 /* Send a 32bit command */ 407 fib->Header.Command = ScsiPortCommand; 408 srb->sg_map.SgCount = 1; 409 srb->sg_map.SgEntry[0].SgAddress = 410 (uint32_t)(uintptr_t)csio->data_ptr; 411 srb->sg_map.SgEntry[0].SgByteCount = 412 csio->dxfer_len; 413 } else { 414 /* 415 * Arrange things so that the S/G 416 * map will get set up automagically 417 */ 418 cm->cm_data = (void *)csio->data_ptr; 419 cm->cm_datalen = csio->dxfer_len; 420 cm->cm_sgtable = &srb->sg_map; 421 } 422 } else { 423 /* XXX Need to handle multiple s/g elements */ 424 panic("aac_cam: multiple s/g elements"); 425 } 426 } else { 427 srb->sg_map.SgCount = 0; 428 srb->sg_map.SgEntry[0].SgByteCount = 0; 429 srb->data_len = 0; 430 } 431 432 break; 433 } 434 case XPT_RESET_DEV: 435 if (!(sc->flags & AAC_FLAGS_CAM_NORESET)) { 436 srb->function = AAC_SRB_FUNC_RESET_DEVICE; 437 break; 438 } else { 439 ccb->ccb_h.status = CAM_REQ_CMP; 440 xpt_done(ccb); 441 return; 442 } 443 default: 444 break; 445 } 446 447 srb->bus = camsc->inf->BusNumber; /* Bus number relative to the card */ 448 srb->target = ccb->ccb_h.target_id; 449 srb->lun = ccb->ccb_h.target_lun; 450 srb->timeout = ccb->ccb_h.timeout; /* XXX */ 451 srb->retry_limit = 0; 452 453 cm->cm_complete = aac_cam_complete; 454 cm->cm_private = ccb; 455 cm->cm_timestamp = time_uptime; 456 457 fib->Header.XferState = 458 AAC_FIBSTATE_HOSTOWNED | 459 AAC_FIBSTATE_INITIALISED | 460 AAC_FIBSTATE_FROMHOST | 461 AAC_FIBSTATE_REXPECTED | 462 AAC_FIBSTATE_NORM; 463 fib->Header.Size = sizeof(struct aac_fib_header) + 464 sizeof(struct aac_srb); 465 466 aac_enqueue_ready(cm); 467 aac_startio(cm->cm_sc); 468 469 return; 470 } 471 472 static void 473 aac_cam_poll(struct cam_sim *sim) 474 { 475 /* 476 * Pinging the interrupt routine isn't very safe, nor is it 477 * really necessary. Do nothing. 478 */ 479 } 480 481 static void 482 aac_cam_complete(struct aac_command *cm) 483 { 484 union ccb *ccb; 485 struct aac_srb_response *srbr; 486 struct aac_softc *sc; 487 488 sc = cm->cm_sc; 489 fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, ""); 490 ccb = cm->cm_private; 491 srbr = (struct aac_srb_response *)&cm->cm_fib->data[0]; 492 493 if (srbr->fib_status != 0) { 494 device_printf(sc->aac_dev, "Passthru FIB failed!\n"); 495 ccb->ccb_h.status = CAM_REQ_ABORTED; 496 } else { 497 /* 498 * The SRB error codes just happen to match the CAM error 499 * codes. How convienient! 500 */ 501 ccb->ccb_h.status = srbr->srb_status; 502 503 /* Take care of SCSI_IO ops. */ 504 if (ccb->ccb_h.func_code == XPT_SCSI_IO) { 505 u_int8_t command, device; 506 507 ccb->csio.scsi_status = srbr->scsi_status; 508 509 /* Take care of autosense */ 510 if (srbr->sense_len) { 511 int sense_len, scsi_sense_len; 512 513 scsi_sense_len = sizeof(struct scsi_sense_data); 514 bzero(&ccb->csio.sense_data, scsi_sense_len); 515 sense_len = (srbr->sense_len > 516 scsi_sense_len) ? scsi_sense_len : 517 srbr->sense_len; 518 bcopy(&srbr->sense[0], &ccb->csio.sense_data, 519 srbr->sense_len); 520 ccb->csio.sense_len = sense_len; 521 ccb->ccb_h.status |= CAM_AUTOSNS_VALID; 522 // scsi_sense_print(&ccb->csio); 523 } 524 525 /* If this is an inquiry command, fake things out */ 526 if (ccb->ccb_h.flags & CAM_CDB_POINTER) 527 command = ccb->csio.cdb_io.cdb_ptr[0]; 528 else 529 command = ccb->csio.cdb_io.cdb_bytes[0]; 530 531 if (command == INQUIRY) { 532 if (ccb->ccb_h.status == CAM_REQ_CMP) { 533 device = ccb->csio.data_ptr[0] & 0x1f; 534 /* 535 * We want DASD and PROC devices to only be 536 * visible through the pass device. 537 */ 538 if ((device == T_DIRECT) || 539 (device == T_PROCESSOR) || 540 (sc->flags & AAC_FLAGS_CAM_PASSONLY)) 541 ccb->csio.data_ptr[0] = 542 ((device & 0xe0) | T_NODEVICE); 543 } else if (ccb->ccb_h.status == CAM_SEL_TIMEOUT && 544 ccb->ccb_h.target_lun != 0) { 545 /* fix for INQUIRYs on Lun>0 */ 546 ccb->ccb_h.status = CAM_DEV_NOT_THERE; 547 } 548 } 549 } 550 } 551 552 aac_release_command(cm); 553 xpt_done(ccb); 554 555 return; 556 } 557 558 static u_int32_t 559 aac_cam_reset_bus(struct cam_sim *sim, union ccb *ccb) 560 { 561 struct aac_fib *fib; 562 struct aac_softc *sc; 563 struct aac_cam *camsc; 564 struct aac_vmioctl *vmi; 565 struct aac_resetbus *rbc; 566 int e; 567 568 camsc = (struct aac_cam *)cam_sim_softc(sim); 569 sc = camsc->inf->aac_sc; 570 571 if (sc == NULL) { 572 printf("Null sc?\n"); 573 return (CAM_REQ_ABORTED); 574 } 575 576 aac_alloc_sync_fib(sc, &fib); 577 578 vmi = (struct aac_vmioctl *)&fib->data[0]; 579 bzero(vmi, sizeof(struct aac_vmioctl)); 580 581 vmi->Command = VM_Ioctl; 582 vmi->ObjType = FT_DRIVE; 583 vmi->MethId = sc->scsi_method_id; 584 vmi->ObjId = 0; 585 vmi->IoctlCmd = ResetBus; 586 587 rbc = (struct aac_resetbus *)&vmi->IoctlBuf[0]; 588 rbc->BusNumber = camsc->inf->BusNumber; 589 590 e = aac_sync_fib(sc, ContainerCommand, 0, fib, 591 sizeof(struct aac_vmioctl)); 592 if (e) { 593 device_printf(sc->aac_dev,"Error %d sending ResetBus command\n", 594 e); 595 aac_release_sync_fib(sc); 596 return (CAM_REQ_ABORTED); 597 } 598 599 aac_release_sync_fib(sc); 600 return (CAM_REQ_CMP); 601 } 602 603 static u_int32_t 604 aac_cam_abort_ccb(struct cam_sim *sim, union ccb *ccb) 605 { 606 return (CAM_UA_ABORT); 607 } 608 609 static u_int32_t 610 aac_cam_term_io(struct cam_sim *sim, union ccb *ccb) 611 { 612 return (CAM_UA_TERMIO); 613 } 614 615