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