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