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