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