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