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_srb32 *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 /* Resetting via the passthrough causes problems. */ 264 cpi->hba_misc = PIM_NOBUSRESET; 265 cpi->hba_eng_cnt = 0; 266 cpi->max_target = camsc->inf->TargetsPerBus; 267 cpi->max_lun = 8; /* Per the controller spec */ 268 cpi->initiator_id = camsc->inf->InitiatorBusId; 269 cpi->bus_id = camsc->inf->BusNumber; 270 cpi->base_transfer_speed = 3300; 271 strncpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN); 272 strncpy(cpi->hba_vid, "Adaptec", HBA_IDLEN); 273 strncpy(cpi->dev_name, cam_sim_name(sim), DEV_IDLEN); 274 cpi->unit_number = cam_sim_unit(sim); 275 cpi->transport = XPORT_SPI; 276 cpi->transport_version = 2; 277 cpi->protocol = PROTO_SCSI; 278 cpi->protocol_version = SCSI_REV_2; 279 ccb->ccb_h.status = CAM_REQ_CMP; 280 xpt_done(ccb); 281 return; 282 } 283 case XPT_GET_TRAN_SETTINGS: 284 { 285 struct ccb_trans_settings_scsi *scsi = 286 &ccb->cts.proto_specific.scsi; 287 struct ccb_trans_settings_spi *spi = 288 &ccb->cts.xport_specific.spi; 289 ccb->cts.protocol = PROTO_SCSI; 290 ccb->cts.protocol_version = SCSI_REV_2; 291 ccb->cts.transport = XPORT_SPI; 292 ccb->cts.transport_version = 2; 293 if (ccb->ccb_h.target_lun != CAM_LUN_WILDCARD) { 294 scsi->valid = CTS_SCSI_VALID_TQ; 295 spi->valid |= CTS_SPI_VALID_DISC; 296 } else { 297 scsi->valid = 0; 298 } 299 ccb->ccb_h.status = CAM_REQ_CMP; 300 xpt_done(ccb); 301 return; 302 } 303 case XPT_SET_TRAN_SETTINGS: 304 ccb->ccb_h.status = CAM_FUNC_NOTAVAIL; 305 xpt_done(ccb); 306 return; 307 case XPT_RESET_BUS: 308 if (!(sc->flags & AAC_FLAGS_CAM_NORESET)) { 309 ccb->ccb_h.status = aac_cam_reset_bus(sim, ccb); 310 } else { 311 ccb->ccb_h.status = CAM_REQ_CMP; 312 } 313 xpt_done(ccb); 314 return; 315 case XPT_ABORT: 316 ccb->ccb_h.status = aac_cam_abort_ccb(sim, ccb); 317 xpt_done(ccb); 318 return; 319 case XPT_TERM_IO: 320 ccb->ccb_h.status = aac_cam_term_io(sim, ccb); 321 xpt_done(ccb); 322 return; 323 default: 324 device_printf(sc->aac_dev, "Unsupported command 0x%x\n", 325 ccb->ccb_h.func_code); 326 ccb->ccb_h.status = CAM_PROVIDE_FAIL; 327 xpt_done(ccb); 328 return; 329 } 330 331 /* Async ops that require communcation with the controller */ 332 333 if (aac_alloc_command(sc, &cm)) { 334 struct aac_event *event; 335 336 xpt_freeze_simq(sim, 1); 337 ccb->ccb_h.status = CAM_RESRC_UNAVAIL; 338 ccb->ccb_h.sim_priv.entries[0].ptr = camsc; 339 event = malloc(sizeof(struct aac_event), M_AACCAM, 340 M_NOWAIT | M_ZERO); 341 if (event == NULL) { 342 device_printf(sc->aac_dev, 343 "Warning, out of memory for event\n"); 344 return; 345 } 346 event->ev_callback = aac_cam_event; 347 event->ev_arg = ccb; 348 event->ev_type = AAC_EVENT_CMFREE; 349 aac_add_event(sc, event); 350 return; 351 } 352 353 fib = cm->cm_fib; 354 srb = (struct aac_srb32 *)&fib->data[0]; 355 cm->cm_datalen = 0; 356 357 switch (ccb->ccb_h.flags & CAM_DIR_MASK) { 358 case CAM_DIR_IN: 359 srb->flags = AAC_SRB_FLAGS_DATA_IN; 360 cm->cm_flags |= AAC_CMD_DATAIN; 361 break; 362 case CAM_DIR_OUT: 363 srb->flags = AAC_SRB_FLAGS_DATA_OUT; 364 cm->cm_flags |= AAC_CMD_DATAOUT; 365 break; 366 case CAM_DIR_NONE: 367 srb->flags = AAC_SRB_FLAGS_NO_DATA_XFER; 368 break; 369 default: 370 srb->flags = AAC_SRB_FLAGS_UNSPECIFIED_DIRECTION; 371 cm->cm_flags |= AAC_CMD_DATAIN | AAC_CMD_DATAOUT; 372 break; 373 } 374 375 switch(ccb->ccb_h.func_code) { 376 case XPT_SCSI_IO: 377 { 378 struct ccb_scsiio *csio = &ccb->csio; 379 380 srb->function = AAC_SRB_FUNC_EXECUTE_SCSI; 381 382 /* 383 * Copy the CDB into the SRB. It's only 6-16 bytes, 384 * so a copy is not too expensive. 385 */ 386 srb->cdb_len = csio->cdb_len; 387 if (ccb->ccb_h.flags & CAM_CDB_POINTER) 388 bcopy(csio->cdb_io.cdb_ptr, (u_int8_t *)&srb->cdb[0], 389 srb->cdb_len); 390 else 391 bcopy(csio->cdb_io.cdb_bytes, (u_int8_t *)&srb->cdb[0], 392 srb->cdb_len); 393 394 /* Set command */ 395 fib->Header.Command = (sc->flags & AAC_FLAGS_SG_64BIT) ? 396 ScsiPortCommandU64 : ScsiPortCommand; 397 398 /* Map the s/g list. XXX 32bit addresses only! */ 399 if ((ccb->ccb_h.flags & CAM_DIR_MASK) != CAM_DIR_NONE) { 400 if ((ccb->ccb_h.flags & CAM_SCATTER_VALID) == 0) { 401 srb->data_len = csio->dxfer_len; 402 if (ccb->ccb_h.flags & CAM_DATA_PHYS) { 403 /* Send a 32bit command */ 404 fib->Header.Command = ScsiPortCommand; 405 srb->sg_map32.SgCount = 1; 406 srb->sg_map32.SgEntry[0].SgAddress = 407 (uint32_t)(uintptr_t)csio->data_ptr; 408 srb->sg_map32.SgEntry[0].SgByteCount = 409 csio->dxfer_len; 410 } else { 411 /* 412 * Arrange things so that the S/G 413 * map will get set up automagically 414 */ 415 cm->cm_data = (void *)csio->data_ptr; 416 cm->cm_datalen = csio->dxfer_len; 417 cm->cm_sgtable = &srb->sg_map32; 418 } 419 } else { 420 /* XXX Need to handle multiple s/g elements */ 421 panic("aac_cam: multiple s/g elements"); 422 } 423 } else { 424 srb->sg_map32.SgCount = 0; 425 srb->sg_map32.SgEntry[0].SgByteCount = 0; 426 srb->data_len = 0; 427 } 428 429 break; 430 } 431 case XPT_RESET_DEV: 432 if (!(sc->flags & AAC_FLAGS_CAM_NORESET)) { 433 srb->function = AAC_SRB_FUNC_RESET_DEVICE; 434 break; 435 } else { 436 ccb->ccb_h.status = CAM_REQ_CMP; 437 xpt_done(ccb); 438 return; 439 } 440 default: 441 break; 442 } 443 444 srb->bus = camsc->inf->BusNumber; /* Bus number relative to the card */ 445 srb->target = ccb->ccb_h.target_id; 446 srb->lun = ccb->ccb_h.target_lun; 447 srb->timeout = ccb->ccb_h.timeout; /* XXX */ 448 srb->retry_limit = 0; 449 450 cm->cm_complete = aac_cam_complete; 451 cm->cm_private = ccb; 452 cm->cm_timestamp = time_uptime; 453 cm->cm_queue = AAC_ADAP_NORM_CMD_QUEUE; 454 455 fib->Header.XferState = 456 AAC_FIBSTATE_HOSTOWNED | 457 AAC_FIBSTATE_INITIALISED | 458 AAC_FIBSTATE_FROMHOST | 459 AAC_FIBSTATE_REXPECTED | 460 AAC_FIBSTATE_NORM; 461 fib->Header.Size = sizeof(struct aac_fib_header) + 462 sizeof(struct aac_srb32); 463 464 aac_enqueue_ready(cm); 465 aac_startio(cm->cm_sc); 466 467 return; 468 } 469 470 static void 471 aac_cam_poll(struct cam_sim *sim) 472 { 473 /* 474 * Pinging the interrupt routine isn't very safe, nor is it 475 * really necessary. Do nothing. 476 */ 477 } 478 479 static void 480 aac_cam_complete(struct aac_command *cm) 481 { 482 union ccb *ccb; 483 struct aac_srb_response *srbr; 484 struct aac_softc *sc; 485 486 sc = cm->cm_sc; 487 fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, ""); 488 ccb = cm->cm_private; 489 srbr = (struct aac_srb_response *)&cm->cm_fib->data[0]; 490 491 if (srbr->fib_status != 0) { 492 device_printf(sc->aac_dev, "Passthru FIB failed!\n"); 493 ccb->ccb_h.status = CAM_REQ_ABORTED; 494 } else { 495 /* 496 * The SRB error codes just happen to match the CAM error 497 * codes. How convienient! 498 */ 499 ccb->ccb_h.status = srbr->srb_status; 500 501 /* Take care of SCSI_IO ops. */ 502 if (ccb->ccb_h.func_code == XPT_SCSI_IO) { 503 u_int8_t command, device; 504 505 ccb->csio.scsi_status = srbr->scsi_status; 506 507 /* Take care of autosense */ 508 if (srbr->sense_len) { 509 int sense_len, scsi_sense_len; 510 511 scsi_sense_len = sizeof(struct scsi_sense_data); 512 bzero(&ccb->csio.sense_data, scsi_sense_len); 513 sense_len = (srbr->sense_len > 514 scsi_sense_len) ? scsi_sense_len : 515 srbr->sense_len; 516 bcopy(&srbr->sense[0], &ccb->csio.sense_data, 517 srbr->sense_len); 518 ccb->csio.sense_len = sense_len; 519 ccb->ccb_h.status |= CAM_AUTOSNS_VALID; 520 // scsi_sense_print(&ccb->csio); 521 } 522 523 /* If this is an inquiry command, fake things out */ 524 if (ccb->ccb_h.flags & CAM_CDB_POINTER) 525 command = ccb->csio.cdb_io.cdb_ptr[0]; 526 else 527 command = ccb->csio.cdb_io.cdb_bytes[0]; 528 529 if (command == INQUIRY) { 530 if (ccb->ccb_h.status == CAM_REQ_CMP) { 531 device = ccb->csio.data_ptr[0] & 0x1f; 532 /* 533 * We want DASD and PROC devices to only be 534 * visible through the pass device. 535 */ 536 if ((device == T_DIRECT) || 537 (device == T_PROCESSOR) || 538 (sc->flags & AAC_FLAGS_CAM_PASSONLY)) 539 ccb->csio.data_ptr[0] = 540 ((device & 0xe0) | T_NODEVICE); 541 } else if (ccb->ccb_h.status == CAM_SEL_TIMEOUT && 542 ccb->ccb_h.target_lun != 0) { 543 /* fix for INQUIRYs on Lun>0 */ 544 ccb->ccb_h.status = CAM_DEV_NOT_THERE; 545 } 546 } 547 } 548 } 549 550 aac_release_command(cm); 551 xpt_done(ccb); 552 553 return; 554 } 555 556 static u_int32_t 557 aac_cam_reset_bus(struct cam_sim *sim, union ccb *ccb) 558 { 559 struct aac_fib *fib; 560 struct aac_softc *sc; 561 struct aac_cam *camsc; 562 struct aac_vmioctl *vmi; 563 struct aac_resetbus *rbc; 564 int e; 565 566 camsc = (struct aac_cam *)cam_sim_softc(sim); 567 sc = camsc->inf->aac_sc; 568 569 if (sc == NULL) { 570 printf("Null sc?\n"); 571 return (CAM_REQ_ABORTED); 572 } 573 574 aac_alloc_sync_fib(sc, &fib); 575 576 vmi = (struct aac_vmioctl *)&fib->data[0]; 577 bzero(vmi, sizeof(struct aac_vmioctl)); 578 579 vmi->Command = VM_Ioctl; 580 vmi->ObjType = FT_DRIVE; 581 vmi->MethId = sc->scsi_method_id; 582 vmi->ObjId = 0; 583 vmi->IoctlCmd = ResetBus; 584 585 rbc = (struct aac_resetbus *)&vmi->IoctlBuf[0]; 586 rbc->BusNumber = camsc->inf->BusNumber; 587 588 e = aac_sync_fib(sc, ContainerCommand, 0, fib, 589 sizeof(struct aac_vmioctl)); 590 if (e) { 591 device_printf(sc->aac_dev,"Error %d sending ResetBus command\n", 592 e); 593 aac_release_sync_fib(sc); 594 return (CAM_REQ_ABORTED); 595 } 596 597 aac_release_sync_fib(sc); 598 return (CAM_REQ_CMP); 599 } 600 601 static u_int32_t 602 aac_cam_abort_ccb(struct cam_sim *sim, union ccb *ccb) 603 { 604 return (CAM_UA_ABORT); 605 } 606 607 static u_int32_t 608 aac_cam_term_io(struct cam_sim *sim, union ccb *ccb) 609 { 610 return (CAM_UA_TERMIO); 611 } 612 613