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