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 42 #include <cam/cam.h> 43 #include <cam/cam_ccb.h> 44 #include <cam/cam_debug.h> 45 #include <cam/cam_sim.h> 46 #include <cam/cam_xpt_sim.h> 47 #include <cam/scsi/scsi_all.h> 48 #include <cam/scsi/scsi_message.h> 49 50 #include <sys/bus.h> 51 #include <sys/conf.h> 52 #include <sys/disk.h> 53 54 #include <machine/md_var.h> 55 #include <machine/bus.h> 56 #include <sys/rman.h> 57 58 #include <vm/vm.h> 59 #include <vm/pmap.h> 60 61 #include <dev/aac/aacreg.h> 62 #include <dev/aac/aac_ioctl.h> 63 #include <dev/aac/aacvar.h> 64 65 struct aac_cam { 66 device_t dev; 67 struct aac_sim *inf; 68 struct cam_sim *sim; 69 struct cam_path *path; 70 }; 71 72 static int aac_cam_probe(device_t dev); 73 static int aac_cam_attach(device_t dev); 74 static int aac_cam_detach(device_t dev); 75 static void aac_cam_action(struct cam_sim *, union ccb *); 76 static void aac_cam_poll(struct cam_sim *); 77 static void aac_cam_complete(struct aac_command *); 78 static u_int32_t aac_cam_reset_bus(struct cam_sim *, union ccb *); 79 static u_int32_t aac_cam_abort_ccb(struct cam_sim *, union ccb *); 80 static u_int32_t aac_cam_term_io(struct cam_sim *, union ccb *); 81 static int aac_cam_get_tran_settings(struct aac_softc *, struct ccb_trans_settings *, u_int32_t); 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 int 104 aac_cam_probe(device_t dev) 105 { 106 debug_called(2); 107 108 return (0); 109 } 110 111 static int 112 aac_cam_detach(device_t dev) 113 { 114 struct aac_cam *camsc; 115 debug_called(2); 116 117 camsc = (struct aac_cam *)device_get_softc(dev); 118 119 mtx_lock(&Giant); 120 121 xpt_async(AC_LOST_DEVICE, camsc->path, NULL); 122 xpt_free_path(camsc->path); 123 xpt_bus_deregister(cam_sim_path(camsc->sim)); 124 cam_sim_free(camsc->sim, /*free_devq*/TRUE); 125 126 mtx_unlock(&Giant); 127 128 return (0); 129 } 130 131 /* 132 * Register the driver as a CAM SIM 133 */ 134 static int 135 aac_cam_attach(device_t dev) 136 { 137 struct cam_devq *devq; 138 struct cam_sim *sim; 139 struct cam_path *path; 140 struct aac_cam *camsc; 141 struct aac_sim *inf; 142 143 debug_called(1); 144 145 camsc = (struct aac_cam *)device_get_softc(dev); 146 inf = (struct aac_sim *)device_get_ivars(dev); 147 camsc->inf = inf; 148 149 devq = cam_simq_alloc(inf->TargetsPerBus); 150 if (devq == NULL) 151 return (EIO); 152 153 sim = cam_sim_alloc(aac_cam_action, aac_cam_poll, "aacp", camsc, 154 device_get_unit(dev), 1, 1, devq); 155 if (sim == NULL) { 156 cam_simq_free(devq); 157 return (EIO); 158 } 159 160 /* Since every bus has it's own sim, every bus 'appears' as bus 0 */ 161 if (xpt_bus_register(sim, 0) != CAM_SUCCESS) { 162 cam_sim_free(sim, TRUE); 163 return (EIO); 164 } 165 166 if (xpt_create_path(&path, NULL, cam_sim_path(sim), 167 CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD) != CAM_REQ_CMP) { 168 xpt_bus_deregister(cam_sim_path(sim)); 169 cam_sim_free(sim, TRUE); 170 return (EIO); 171 } 172 173 camsc->sim = sim; 174 camsc->path = path; 175 176 return (0); 177 } 178 179 static void 180 aac_cam_action(struct cam_sim *sim, union ccb *ccb) 181 { 182 struct aac_cam *camsc; 183 struct aac_softc *sc; 184 struct aac_srb32 *srb; 185 struct aac_fib *fib; 186 struct aac_command *cm; 187 188 debug_called(2); 189 190 camsc = (struct aac_cam *)cam_sim_softc(sim); 191 sc = camsc->inf->aac_sc; 192 193 /* Synchronous ops, and ops that don't require communication with the 194 * controller */ 195 switch(ccb->ccb_h.func_code) { 196 case XPT_SCSI_IO: 197 case XPT_RESET_DEV: 198 /* These are handled down below */ 199 break; 200 case XPT_CALC_GEOMETRY: 201 { 202 struct ccb_calc_geometry *ccg; 203 u_int32_t size_mb; 204 u_int32_t secs_per_cylinder; 205 206 ccg = &ccb->ccg; 207 size_mb = ccg->volume_size / 208 ((1024L * 1024L) / ccg->block_size); 209 if (size_mb >= (2 * 1024)) { /* 2GB */ 210 ccg->heads = 255; 211 ccg->secs_per_track = 63; 212 } else if (size_mb >= (1 * 1024)) { /* 1GB */ 213 ccg->heads = 128; 214 ccg->secs_per_track = 32; 215 } else { 216 ccg->heads = 64; 217 ccg->secs_per_track = 32; 218 } 219 secs_per_cylinder = ccg->heads * ccg->secs_per_track; 220 ccg->cylinders = ccg->volume_size / secs_per_cylinder; 221 222 ccb->ccb_h.status = CAM_REQ_CMP; 223 xpt_done(ccb); 224 return; 225 } 226 case XPT_PATH_INQ: 227 { 228 struct ccb_pathinq *cpi = &ccb->cpi; 229 230 cpi->version_num = 1; 231 cpi->hba_inquiry = PI_WIDE_16; 232 cpi->target_sprt = 0; 233 234 /* Resetting via the passthrough causes problems. */ 235 cpi->hba_misc = PIM_NOBUSRESET; 236 cpi->hba_eng_cnt = 0; 237 cpi->max_target = camsc->inf->TargetsPerBus; 238 cpi->max_lun = 8; /* Per the controller spec */ 239 cpi->initiator_id = camsc->inf->InitiatorBusId; 240 cpi->bus_id = camsc->inf->BusNumber; 241 cpi->base_transfer_speed = 3300; 242 strncpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN); 243 strncpy(cpi->hba_vid, "Adaptec", HBA_IDLEN); 244 strncpy(cpi->dev_name, cam_sim_name(sim), DEV_IDLEN); 245 cpi->unit_number = cam_sim_unit(sim); 246 247 ccb->ccb_h.status = CAM_REQ_CMP; 248 xpt_done(ccb); 249 return; 250 } 251 case XPT_GET_TRAN_SETTINGS: 252 { 253 u_int32_t handle; 254 255 handle = AAC_BTL_TO_HANDLE(camsc->inf->BusNumber, 256 ccb->ccb_h.target_id, ccb->ccb_h.target_lun); 257 ccb->ccb_h.status = aac_cam_get_tran_settings(sc, &ccb->cts, 258 handle); 259 xpt_done(ccb); 260 return; 261 } 262 case XPT_SET_TRAN_SETTINGS: 263 ccb->ccb_h.status = CAM_FUNC_NOTAVAIL; 264 xpt_done(ccb); 265 return; 266 case XPT_RESET_BUS: 267 if (!(sc->flags & AAC_FLAGS_CAM_NORESET)) { 268 ccb->ccb_h.status = aac_cam_reset_bus(sim, ccb); 269 } else { 270 ccb->ccb_h.status = CAM_REQ_CMP; 271 } 272 xpt_done(ccb); 273 return; 274 case XPT_ABORT: 275 ccb->ccb_h.status = aac_cam_abort_ccb(sim, ccb); 276 xpt_done(ccb); 277 return; 278 case XPT_TERM_IO: 279 ccb->ccb_h.status = aac_cam_term_io(sim, ccb); 280 xpt_done(ccb); 281 return; 282 default: 283 device_printf(sc->aac_dev, "Unsupported command 0x%x\n", 284 ccb->ccb_h.func_code); 285 ccb->ccb_h.status = CAM_PROVIDE_FAIL; 286 xpt_done(ccb); 287 return; 288 } 289 290 /* Async ops that require communcation with the controller */ 291 292 AAC_LOCK_ACQUIRE(&sc->aac_io_lock); 293 if (aac_alloc_command(sc, &cm)) { 294 AAC_LOCK_RELEASE(&sc->aac_io_lock); 295 xpt_freeze_simq(sim, 1); 296 ccb->ccb_h.status = CAM_REQUEUE_REQ; 297 xpt_done(ccb); 298 return; 299 } 300 301 fib = cm->cm_fib; 302 srb = (struct aac_srb32 *)&fib->data[0]; 303 cm->cm_datalen = 0; 304 305 switch (ccb->ccb_h.flags & CAM_DIR_MASK) { 306 case CAM_DIR_IN: 307 srb->flags = AAC_SRB_FLAGS_DATA_IN; 308 cm->cm_flags |= AAC_CMD_DATAIN; 309 break; 310 case CAM_DIR_OUT: 311 srb->flags = AAC_SRB_FLAGS_DATA_OUT; 312 cm->cm_flags |= AAC_CMD_DATAOUT; 313 break; 314 case CAM_DIR_NONE: 315 srb->flags = AAC_SRB_FLAGS_NO_DATA_XFER; 316 break; 317 default: 318 srb->flags = AAC_SRB_FLAGS_UNSPECIFIED_DIRECTION; 319 cm->cm_flags |= AAC_CMD_DATAIN | AAC_CMD_DATAOUT; 320 break; 321 } 322 323 switch(ccb->ccb_h.func_code) { 324 case XPT_SCSI_IO: 325 { 326 struct ccb_scsiio *csio = &ccb->csio; 327 328 srb->function = AAC_SRB_FUNC_EXECUTE_SCSI; 329 330 /* 331 * Copy the CDB into the SRB. It's only 6-16 bytes, 332 * so a copy is not too expensive. 333 */ 334 srb->cdb_len = csio->cdb_len; 335 if (ccb->ccb_h.flags & CAM_CDB_POINTER) 336 bcopy(csio->cdb_io.cdb_ptr, (u_int8_t *)&srb->cdb[0], 337 srb->cdb_len); 338 else 339 bcopy(csio->cdb_io.cdb_bytes, (u_int8_t *)&srb->cdb[0], 340 srb->cdb_len); 341 342 /* Map the s/g list. XXX 32bit addresses only! */ 343 if ((ccb->ccb_h.flags & CAM_DIR_MASK) != CAM_DIR_NONE) { 344 if ((ccb->ccb_h.flags & CAM_SCATTER_VALID) == 0) { 345 srb->data_len = csio->dxfer_len; 346 if (ccb->ccb_h.flags & CAM_DATA_PHYS) { 347 /* 348 * XXX This isn't 64-bit clean. 349 * However, this condition is not 350 * normally used in CAM. 351 */ 352 srb->sg_map32.SgCount = 1; 353 srb->sg_map32.SgEntry[0].SgAddress = 354 (uint32_t)(uintptr_t)csio->data_ptr; 355 srb->sg_map32.SgEntry[0].SgByteCount = 356 csio->dxfer_len; 357 } else { 358 /* 359 * Arrange things so that the S/G 360 * map will get set up automagically 361 */ 362 cm->cm_data = (void *)csio->data_ptr; 363 cm->cm_datalen = csio->dxfer_len; 364 cm->cm_sgtable = &srb->sg_map32; 365 } 366 } else { 367 /* XXX Need to handle multiple s/g elements */ 368 panic("aac_cam: multiple s/g elements"); 369 } 370 } else { 371 srb->sg_map32.SgCount = 0; 372 srb->sg_map32.SgEntry[0].SgByteCount = 0; 373 srb->data_len = 0; 374 } 375 376 break; 377 } 378 case XPT_RESET_DEV: 379 if (!(sc->flags & AAC_FLAGS_CAM_NORESET)) { 380 srb->function = AAC_SRB_FUNC_RESET_DEVICE; 381 break; 382 } else { 383 ccb->ccb_h.status = CAM_REQ_CMP; 384 xpt_done(ccb); 385 return; 386 } 387 default: 388 break; 389 } 390 391 srb->bus = camsc->inf->BusNumber; /* Bus number relative to the card */ 392 srb->target = ccb->ccb_h.target_id; 393 srb->lun = ccb->ccb_h.target_lun; 394 srb->timeout = ccb->ccb_h.timeout; /* XXX */ 395 srb->retry_limit = 0; 396 397 cm->cm_complete = aac_cam_complete; 398 cm->cm_private = ccb; 399 cm->cm_timestamp = time_second; 400 cm->cm_queue = AAC_ADAP_NORM_CMD_QUEUE; 401 402 fib->Header.XferState = 403 AAC_FIBSTATE_HOSTOWNED | 404 AAC_FIBSTATE_INITIALISED | 405 AAC_FIBSTATE_FROMHOST | 406 AAC_FIBSTATE_REXPECTED | 407 AAC_FIBSTATE_NORM; 408 fib->Header.Command = ScsiPortCommand; 409 fib->Header.Size = sizeof(struct aac_fib_header) + 410 sizeof(struct aac_srb32); 411 412 aac_enqueue_ready(cm); 413 aac_startio(cm->cm_sc); 414 415 AAC_LOCK_RELEASE(&sc->aac_io_lock); 416 417 return; 418 } 419 420 static void 421 aac_cam_poll(struct cam_sim *sim) 422 { 423 /* 424 * Pinging the interrupt routine isn't very safe, nor is it 425 * really necessary. Do nothing. 426 */ 427 } 428 429 static void 430 aac_cam_complete(struct aac_command *cm) 431 { 432 union ccb *ccb; 433 struct aac_srb_response *srbr; 434 struct aac_softc *sc; 435 436 debug_called(2); 437 438 sc = cm->cm_sc; 439 ccb = cm->cm_private; 440 srbr = (struct aac_srb_response *)&cm->cm_fib->data[0]; 441 442 if (srbr->fib_status != 0) { 443 device_printf(sc->aac_dev, "Passthru FIB failed!\n"); 444 ccb->ccb_h.status = CAM_REQ_ABORTED; 445 } else { 446 /* 447 * The SRB error codes just happen to match the CAM error 448 * codes. How convienient! 449 */ 450 ccb->ccb_h.status = srbr->srb_status; 451 452 /* Take care of SCSI_IO ops. */ 453 if (ccb->ccb_h.func_code == XPT_SCSI_IO) { 454 u_int8_t command, device; 455 456 ccb->csio.scsi_status = srbr->scsi_status; 457 458 /* Take care of autosense */ 459 if (srbr->sense_len) { 460 int sense_len, scsi_sense_len; 461 462 scsi_sense_len = sizeof(struct scsi_sense_data); 463 bzero(&ccb->csio.sense_data, scsi_sense_len); 464 sense_len = (srbr->sense_len > 465 scsi_sense_len) ? scsi_sense_len : 466 srbr->sense_len; 467 bcopy(&srbr->sense[0], &ccb->csio.sense_data, 468 srbr->sense_len); 469 ccb->csio.sense_len = sense_len; 470 ccb->ccb_h.status |= CAM_AUTOSNS_VALID; 471 scsi_sense_print(&ccb->csio); 472 } 473 474 /* If this is an inquiry command, fake things out */ 475 if (ccb->ccb_h.flags & CAM_CDB_POINTER) 476 command = ccb->csio.cdb_io.cdb_ptr[0]; 477 else 478 command = ccb->csio.cdb_io.cdb_bytes[0]; 479 480 if ((command == INQUIRY) && 481 (ccb->ccb_h.status == CAM_REQ_CMP)) { 482 device = ccb->csio.data_ptr[0] & 0x1f; 483 /* 484 * We want DASD and PROC devices to only be 485 * visible through the pass device. 486 */ 487 if ((device == T_DIRECT) || 488 (device == T_PROCESSOR) || 489 (sc->flags & AAC_FLAGS_CAM_PASSONLY)) 490 ccb->csio.data_ptr[0] = 491 ((device & 0xe0) | T_NODEVICE); 492 } 493 } 494 } 495 496 aac_release_command(cm); 497 498 AAC_LOCK_RELEASE(&sc->aac_io_lock); 499 mtx_lock(&Giant); 500 xpt_done(ccb); 501 mtx_unlock(&Giant); 502 AAC_LOCK_ACQUIRE(&sc->aac_io_lock); 503 504 return; 505 } 506 507 static u_int32_t 508 aac_cam_reset_bus(struct cam_sim *sim, union ccb *ccb) 509 { 510 struct aac_fib *fib; 511 struct aac_softc *sc; 512 struct aac_cam *camsc; 513 struct aac_vmioctl *vmi; 514 struct aac_resetbus *rbc; 515 int e; 516 517 camsc = (struct aac_cam *)cam_sim_softc(sim); 518 sc = camsc->inf->aac_sc; 519 520 if (sc == NULL) { 521 printf("Null sc?\n"); 522 return (CAM_REQ_ABORTED); 523 } 524 525 aac_alloc_sync_fib(sc, &fib, 0); 526 527 vmi = (struct aac_vmioctl *)&fib->data[0]; 528 bzero(vmi, sizeof(struct aac_vmioctl)); 529 530 vmi->Command = VM_Ioctl; 531 vmi->ObjType = FT_DRIVE; 532 vmi->MethId = sc->scsi_method_id; 533 vmi->ObjId = 0; 534 vmi->IoctlCmd = ResetBus; 535 536 rbc = (struct aac_resetbus *)&vmi->IoctlBuf[0]; 537 rbc->BusNumber = camsc->inf->BusNumber; 538 539 e = aac_sync_fib(sc, ContainerCommand, 0, fib, 540 sizeof(struct aac_vmioctl)); 541 if (e) { 542 device_printf(sc->aac_dev,"Error %d sending ResetBus command\n", 543 e); 544 aac_release_sync_fib(sc); 545 return (CAM_REQ_ABORTED); 546 } 547 548 aac_release_sync_fib(sc); 549 return (CAM_REQ_CMP); 550 } 551 552 static u_int32_t 553 aac_cam_abort_ccb(struct cam_sim *sim, union ccb *ccb) 554 { 555 return (CAM_UA_ABORT); 556 } 557 558 static u_int32_t 559 aac_cam_term_io(struct cam_sim *sim, union ccb *ccb) 560 { 561 return (CAM_UA_TERMIO); 562 } 563 564 static int 565 aac_cam_get_tran_settings(struct aac_softc *sc, struct ccb_trans_settings *cts, u_int32_t handle) 566 { 567 struct aac_fib *fib; 568 struct aac_vmioctl *vmi; 569 struct aac_vmi_devinfo_resp *vmi_resp; 570 int error; 571 572 aac_alloc_sync_fib(sc, &fib, 0); 573 vmi = (struct aac_vmioctl *)&fib->data[0]; 574 bzero(vmi, sizeof(struct aac_vmioctl)); 575 576 vmi->Command = VM_Ioctl; 577 vmi->ObjType = FT_DRIVE; 578 vmi->MethId = sc->scsi_method_id; 579 vmi->ObjId = handle; 580 vmi->IoctlCmd = GetDeviceProbeInfo; 581 582 error = aac_sync_fib(sc, ContainerCommand, 0, fib, 583 sizeof(struct aac_vmioctl)); 584 if (error) { 585 device_printf(sc->aac_dev, "Error %d sending GetDeviceProbeInfo" 586 " command\n", error); 587 aac_release_sync_fib(sc); 588 return (CAM_REQ_INVALID); 589 } 590 591 vmi_resp = (struct aac_vmi_devinfo_resp *)&fib->data[0]; 592 if (vmi_resp->Status != ST_OK) { 593 /* 594 * The only reason why this command will return an error is 595 * if the requested device doesn't exist. 596 */ 597 debug(1, "GetDeviceProbeInfo returned %d\n", vmi_resp->Status); 598 aac_release_sync_fib(sc); 599 return (CAM_DEV_NOT_THERE); 600 } 601 602 cts->bus_width = ((vmi_resp->Inquiry7 & 0x60) >> 5); 603 cts->valid = CCB_TRANS_BUS_WIDTH_VALID; 604 605 if (vmi_resp->ScsiRate) { 606 cts->sync_period = 607 scsi_calc_syncparam((10000 / vmi_resp->ScsiRate)); 608 cts->sync_offset = vmi_resp->ScsiOffset; 609 cts->valid |= CCB_TRANS_SYNC_RATE_VALID | 610 CCB_TRANS_SYNC_OFFSET_VALID; 611 } 612 613 cts->flags &= ~(CCB_TRANS_DISC_ENB | CCB_TRANS_TAG_ENB); 614 cts->valid |= CCB_TRANS_DISC_VALID | 615 CCB_TRANS_TQ_VALID; 616 617 aac_release_sync_fib(sc); 618 return (CAM_REQ_CMP); 619 } 620