1 /*- 2 * Copyright (c) 2001-2003 Thomas Quinot <thomas@cuivre.fr.eu.org> 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 * without modification, immediately at the beginning of the file. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 3. The name of the author may not be used to endorse or promote products 15 * derived from this software without specific prior written permission. 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27 * 28 * $FreeBSD: src/sys/dev/ata/atapi-cam.c,v 1.44 2006/03/31 08:09:05 sos Exp $ 29 * $DragonFly: src/sys/dev/disk/nata/atapi-cam.c,v 1.8 2007/12/23 07:00:56 pavalos Exp $ 30 */ 31 32 #include "opt_ata.h" 33 #include "opt_scsi.h" 34 35 #include <sys/param.h> 36 #include <sys/bus.h> 37 #include <sys/libkern.h> 38 #include <sys/lock.h> /* for {get,rel}_mplock() */ 39 #include <sys/malloc.h> 40 #include <sys/module.h> 41 #include <sys/nata.h> 42 #include <sys/spinlock.h> 43 #include <sys/spinlock2.h> 44 #include <sys/queue.h> 45 #include <sys/systm.h> 46 #include <sys/thread2.h> 47 48 #include <bus/cam/cam.h> 49 #include <bus/cam/cam_ccb.h> 50 #include <bus/cam/cam_periph.h> 51 #include <bus/cam/cam_sim.h> 52 #include <bus/cam/cam_xpt_sim.h> 53 #include <bus/cam/cam_debug.h> 54 #include <bus/cam/scsi/scsi_all.h> 55 56 #include "ata-all.h" 57 #include "ata_if.h" 58 59 /* private data associated with an ATA bus */ 60 struct atapi_xpt_softc { 61 struct ata_device atapi_cam_dev; /* must be first */ 62 device_t dev; 63 device_t parent; 64 struct ata_channel *ata_ch; 65 struct cam_path *path; 66 struct cam_sim *sim; 67 int flags; 68 #define BUS_REGISTERED 0x01 69 #define RESOURCE_SHORTAGE 0x02 70 #define DETACHING 0x04 71 72 TAILQ_HEAD(,atapi_hcb) pending_hcbs; 73 struct ata_device *atadev[2]; 74 struct spinlock state_lock; 75 }; 76 77 /* hardware command descriptor block */ 78 struct atapi_hcb { 79 struct atapi_xpt_softc *softc; 80 int unit; 81 int bus; 82 int target; 83 int lun; 84 union ccb *ccb; 85 int flags; 86 #define QUEUED 0x0001 87 #define AUTOSENSE 0x0002 88 char *dxfer_alloc; 89 TAILQ_ENTRY(atapi_hcb) chain; 90 }; 91 92 enum reinit_reason { ATTACH, RESET }; 93 94 /* Device methods */ 95 static void atapi_cam_identify(device_t *dev, device_t parent); 96 static int atapi_cam_probe(device_t dev); 97 static int atapi_cam_attach(device_t dev); 98 static int atapi_cam_detach(device_t dev); 99 static int atapi_cam_reinit(device_t dev); 100 101 /* CAM XPT methods */ 102 static void atapi_action(struct cam_sim *, union ccb *); 103 static void atapi_poll(struct cam_sim *); 104 static void atapi_async(void *, u_int32_t, struct cam_path *, void *); 105 static void atapi_cb(struct ata_request *); 106 107 /* Module methods */ 108 static int atapi_cam_event_handler(module_t mod, int what, void *arg); 109 110 /* internal functions */ 111 static void reinit_bus(struct atapi_xpt_softc *scp, enum reinit_reason reason); 112 static void setup_async_cb(struct atapi_xpt_softc *, uint32_t); 113 static void cam_rescan_callback(struct cam_periph *, union ccb *); 114 static void cam_rescan(struct cam_sim *); 115 static void free_hcb_and_ccb_done(struct atapi_hcb *, u_int32_t); 116 static struct atapi_hcb *allocate_hcb(struct atapi_xpt_softc *, int, int, union ccb *); 117 static void free_hcb(struct atapi_hcb *hcb); 118 static void free_softc(struct atapi_xpt_softc *scp); 119 120 static MALLOC_DEFINE(M_ATACAM, "ata_cam", "ATA driver CAM-XPT layer"); 121 122 static device_method_t atapi_cam_methods[] = { 123 DEVMETHOD(device_identify, atapi_cam_identify), 124 DEVMETHOD(device_probe, atapi_cam_probe), 125 DEVMETHOD(device_attach, atapi_cam_attach), 126 DEVMETHOD(device_detach, atapi_cam_detach), 127 DEVMETHOD(ata_reinit, atapi_cam_reinit), 128 {0, 0} 129 }; 130 131 static driver_t atapi_cam_driver = { 132 "atapicam", 133 atapi_cam_methods, 134 sizeof(struct atapi_xpt_softc) 135 }; 136 137 static devclass_t atapi_cam_devclass; 138 DRIVER_MODULE(atapicam, ata, 139 atapi_cam_driver, 140 atapi_cam_devclass, 141 atapi_cam_event_handler, 142 /*arg*/NULL); 143 MODULE_VERSION(atapicam, 1); 144 MODULE_DEPEND(atapicam, ata, 1, 1, 1); 145 MODULE_DEPEND(atapicam, cam, 1, 1, 1); 146 147 static void 148 atapi_cam_identify(device_t *dev, device_t parent) 149 { 150 struct atapi_xpt_softc *scp = 151 kmalloc(sizeof(struct atapi_xpt_softc), M_ATACAM, M_INTWAIT|M_ZERO); 152 device_t child; 153 154 /* Assume one atapicam instance per parent channel instance. */ 155 child = device_add_child(parent, "atapicam", -1); 156 if (child == NULL) { 157 /* XXX TGEN Use device_printf()? */ 158 kprintf("atapi_cam_identify: out of memory, can't add child"); 159 kfree(scp, M_ATACAM); 160 return; 161 } 162 scp->atapi_cam_dev.unit = -1; 163 scp->atapi_cam_dev.dev = child; 164 device_quiet(child); 165 device_set_softc(child, scp); 166 } 167 168 static int 169 atapi_cam_probe(device_t dev) 170 { 171 struct ata_device *atadev = device_get_softc (dev); 172 173 KASSERT(atadev != NULL, ("expect valid struct ata_device")); 174 if (atadev->unit < 0) { 175 device_set_desc(dev, "ATAPI CAM Attachment"); 176 return(0); 177 } else { 178 return ENXIO; 179 } 180 } 181 182 static int 183 atapi_cam_attach(device_t dev) 184 { 185 struct atapi_xpt_softc *scp = NULL; 186 struct cam_devq *devq = NULL; 187 struct cam_sim *sim = NULL; 188 struct cam_path *path = NULL; 189 int unit, error; 190 191 scp = (struct atapi_xpt_softc *)device_get_softc(dev); 192 if (scp == NULL) { 193 device_printf(dev, "Cannot get softc\n"); 194 return ENOMEM; 195 } 196 197 spin_init(&scp->state_lock); 198 199 scp->dev = dev; 200 scp->parent = device_get_parent(dev); 201 scp->ata_ch = device_get_softc(scp->parent); 202 TAILQ_INIT(&scp->pending_hcbs); 203 unit = device_get_unit(dev); 204 205 if ((devq = cam_simq_alloc(16)) == NULL) { 206 error = ENOMEM; 207 goto out; 208 } 209 210 if ((sim = cam_sim_alloc(atapi_action, atapi_poll, "ata", 211 (void *)scp, unit, 1, 1, devq)) == NULL) { 212 error = ENOMEM; 213 goto out; 214 } 215 scp->sim = sim; 216 217 if (xpt_bus_register(sim, 0) != CAM_SUCCESS) { 218 error = EINVAL; 219 goto out; 220 } 221 scp->flags |= BUS_REGISTERED; 222 223 if (xpt_create_path(&path, /*periph*/ NULL, 224 cam_sim_path(sim), CAM_TARGET_WILDCARD, 225 CAM_LUN_WILDCARD) != CAM_REQ_CMP) { 226 error = ENOMEM; 227 goto out; 228 } 229 scp->path = path; 230 231 CAM_DEBUG(path, CAM_DEBUG_TRACE, ("Registered SIM for ata%d\n", unit)); 232 233 setup_async_cb(scp, AC_LOST_DEVICE); 234 reinit_bus(scp, ATTACH); 235 error = 0; 236 237 out: 238 if (error != 0) 239 free_softc(scp); 240 241 return(error); 242 } 243 244 static int 245 atapi_cam_detach(device_t dev) 246 { 247 struct atapi_xpt_softc *scp = device_get_softc(dev); 248 249 get_mplock(); 250 xpt_freeze_simq(scp->sim, 1 /*count*/); 251 rel_mplock(); 252 spin_lock_wr(&scp->state_lock); 253 scp->flags |= DETACHING; 254 spin_unlock_wr(&scp->state_lock); 255 free_softc(scp); 256 return (0); 257 } 258 259 static int 260 atapi_cam_reinit(device_t dev) { 261 struct atapi_xpt_softc *scp = device_get_softc(dev); 262 263 /* 264 * scp might be null if the bus is being reinitialised during 265 * the boot-up sequence, before the ATAPI bus is registered. 266 */ 267 268 if (scp != NULL) { 269 reinit_bus(scp, RESET); 270 } 271 return 0; 272 } 273 274 static void 275 reinit_bus(struct atapi_xpt_softc *scp, enum reinit_reason reason) { 276 struct ata_device *atadev; 277 device_t *children; 278 int nchildren, i; 279 280 if (device_get_children(scp->parent, &children, &nchildren) != 0) { 281 return; 282 } 283 284 spin_lock_wr(&scp->state_lock); 285 scp->atadev[0] = NULL; 286 scp->atadev[1] = NULL; 287 288 for (i = 0; i < nchildren; i++) { 289 /* XXX Does the child need to actually be attached yet? */ 290 if (children[i] != NULL) { 291 atadev = device_get_softc(children[i]); 292 if ((atadev->unit == ATA_MASTER) && 293 (scp->ata_ch->devices & ATA_ATAPI_MASTER) != 0) 294 scp->atadev[0] = atadev; 295 if ((atadev->unit == ATA_SLAVE) && 296 (scp->ata_ch->devices & ATA_ATAPI_SLAVE) != 0) 297 scp->atadev[1] = atadev; 298 } 299 } 300 spin_unlock_wr(&scp->state_lock); 301 kfree(children, M_TEMP); 302 303 switch (reason) { 304 case RESET: 305 xpt_async(AC_BUS_RESET, scp->path, NULL); 306 /*FALLTHROUGH*/ 307 case ATTACH: 308 cam_rescan(scp->sim); 309 break; 310 } 311 } 312 313 static void 314 setup_async_cb(struct atapi_xpt_softc *scp, uint32_t events) 315 { 316 struct ccb_setasync csa; 317 318 get_mplock(); 319 xpt_setup_ccb(&csa.ccb_h, scp->path, /*priority*/ 5); 320 csa.ccb_h.func_code = XPT_SASYNC_CB; 321 csa.event_enable = events; 322 csa.callback = &atapi_async; 323 csa.callback_arg = scp->sim; 324 xpt_action((union ccb *) &csa); 325 rel_mplock(); 326 } 327 328 static void 329 atapi_action(struct cam_sim *sim, union ccb *ccb) 330 { 331 struct atapi_xpt_softc *softc = (struct atapi_xpt_softc*)cam_sim_softc(sim); 332 struct ccb_hdr *ccb_h = &ccb->ccb_h; 333 struct atapi_hcb *hcb = NULL; 334 struct ata_request *request = NULL; 335 int unit = cam_sim_unit(sim); 336 int bus = cam_sim_bus(sim); 337 int len; 338 char *buf; 339 340 switch (ccb_h->func_code) { 341 case XPT_PATH_INQ: { 342 struct ccb_pathinq *cpi = &ccb->cpi; 343 int tid = ccb_h->target_id; 344 345 cpi->version_num = 1; 346 cpi->hba_inquiry = 0; 347 cpi->target_sprt = 0; 348 cpi->hba_misc = PIM_NO_6_BYTE; 349 cpi->hba_eng_cnt = 0; 350 bzero(cpi->vuhba_flags, sizeof(cpi->vuhba_flags)); 351 cpi->max_target = 1; 352 cpi->max_lun = 0; 353 cpi->async_flags = 0; 354 cpi->hpath_id = 0; 355 cpi->initiator_id = 7; 356 strncpy(cpi->sim_vid, "FreeBSD", sizeof(cpi->sim_vid)); 357 strncpy(cpi->hba_vid, "ATAPI", sizeof(cpi->hba_vid)); 358 strncpy(cpi->dev_name, cam_sim_name(sim), sizeof cpi->dev_name); 359 cpi->unit_number = cam_sim_unit(sim); 360 cpi->bus_id = cam_sim_bus(sim); 361 cpi->base_transfer_speed = 3300; 362 #ifdef CAM_NEW_TRAN_CODE 363 cpi->transport = XPORT_ATA; 364 cpi->transport_version = 2; 365 cpi->protocol = PROTO_SCSI; 366 cpi->protocol_version = SCSI_REV_2; 367 #endif 368 369 if (softc->ata_ch && tid != CAM_TARGET_WILDCARD) { 370 spin_lock_wr(&softc->state_lock); 371 if (softc->atadev[tid] == NULL) { 372 ccb->ccb_h.status = CAM_DEV_NOT_THERE; 373 xpt_done(ccb); 374 spin_unlock_wr(&softc->state_lock); 375 return; 376 } 377 switch (softc->atadev[ccb_h->target_id]->mode) { 378 case ATA_PIO1: 379 cpi->base_transfer_speed = 5200; 380 break; 381 case ATA_PIO2: 382 cpi->base_transfer_speed = 7000; 383 break; 384 case ATA_PIO3: 385 cpi->base_transfer_speed = 11000; 386 break; 387 case ATA_PIO4: 388 case ATA_DMA: 389 case ATA_WDMA2: 390 cpi->base_transfer_speed = 16000; 391 break; 392 case ATA_UDMA2: 393 cpi->base_transfer_speed = 33000; 394 break; 395 case ATA_UDMA4: 396 cpi->base_transfer_speed = 66000; 397 break; 398 case ATA_UDMA5: 399 cpi->base_transfer_speed = 100000; 400 break; 401 case ATA_UDMA6: 402 cpi->base_transfer_speed = 133000; 403 break; 404 default: 405 break; 406 } 407 spin_unlock_wr(&softc->state_lock); 408 } 409 ccb->ccb_h.status = CAM_REQ_CMP; 410 xpt_done(ccb); 411 return; 412 } 413 414 case XPT_RESET_DEV: { 415 int tid = ccb_h->target_id; 416 417 CAM_DEBUG(ccb->ccb_h.path, CAM_DEBUG_SUBTRACE, ("dev reset\n")); 418 ata_controlcmd(softc->atadev[tid]->dev, ATA_DEVICE_RESET, 0, 0, 0); 419 ccb->ccb_h.status = CAM_REQ_CMP; 420 xpt_done(ccb); 421 return; 422 } 423 424 case XPT_RESET_BUS: 425 CAM_DEBUG(ccb->ccb_h.path, CAM_DEBUG_SUBTRACE, ("bus reset\n")); 426 ata_reinit(softc->parent); 427 ccb->ccb_h.status = CAM_REQ_CMP; 428 xpt_done(ccb); 429 return; 430 431 case XPT_SET_TRAN_SETTINGS: 432 /* ignore these, we're not doing SCSI here */ 433 CAM_DEBUG(ccb->ccb_h.path, CAM_DEBUG_SUBTRACE, 434 ("SET_TRAN_SETTINGS not supported\n")); 435 ccb->ccb_h.status = CAM_FUNC_NOTAVAIL; 436 xpt_done(ccb); 437 return; 438 439 case XPT_GET_TRAN_SETTINGS: { 440 struct ccb_trans_settings *cts = &ccb->cts; 441 #ifdef CAM_NEW_TRAN_CODE 442 cts->protocol = PROTO_SCSI; 443 cts->protocol_version = SCSI_REV_2; 444 cts->transport = XPORT_ATA; 445 cts->transport_version = XPORT_VERSION_UNSPECIFIED; 446 cts->proto_specific.valid = 0; 447 cts->xport_specific.valid = 0; 448 /* nothing more to do */ 449 #else 450 /* 451 * The default CAM transport code is very SCSI-specific and 452 * doesn't understand IDE speeds very well. Be silent about it 453 * here and let it default to what is set in XPT_PATH_INQ 454 */ 455 cts->valid = (CCB_TRANS_DISC_VALID | CCB_TRANS_TQ_VALID); 456 cts->flags &= ~(CCB_TRANS_DISC_ENB | CCB_TRANS_TAG_ENB); 457 #endif 458 ccb->ccb_h.status = CAM_REQ_CMP; 459 CAM_DEBUG(ccb->ccb_h.path, CAM_DEBUG_SUBTRACE, ("GET_TRAN_SETTINGS\n")); 460 xpt_done(ccb); 461 return; 462 } 463 464 case XPT_CALC_GEOMETRY: { 465 CAM_DEBUG(ccb->ccb_h.path, CAM_DEBUG_SUBTRACE, ("CALC_GEOMETRY\n")); 466 cam_calc_geometry(&ccb->ccg, /*extended*/1); 467 xpt_done(ccb); 468 return; 469 } 470 471 case XPT_SCSI_IO: { 472 struct ccb_scsiio *csio = &ccb->csio; 473 int tid = ccb_h->target_id, lid = ccb_h->target_lun; 474 int request_flags = ATA_R_QUIET | ATA_R_ATAPI; 475 476 CAM_DEBUG(ccb_h->path, CAM_DEBUG_SUBTRACE, ("XPT_SCSI_IO\n")); 477 478 spin_lock_wr(&softc->state_lock); 479 if (softc->flags & DETACHING) { 480 ccb->ccb_h.status = CAM_REQ_ABORTED; 481 xpt_done(ccb); 482 spin_unlock_wr(&softc->state_lock); 483 return; 484 } 485 486 if (softc->atadev[tid] == NULL) { 487 ccb->ccb_h.status = CAM_DEV_NOT_THERE; 488 xpt_done(ccb); 489 spin_unlock_wr(&softc->state_lock); 490 return; 491 } 492 493 /* check that this request was not aborted already */ 494 if ((ccb_h->status & CAM_STATUS_MASK) != CAM_REQ_INPROG) { 495 kprintf("XPT_SCSI_IO received but already in progress?\n"); 496 xpt_done(ccb); 497 spin_unlock_wr(&softc->state_lock); 498 return; 499 } 500 if (lid > 0) { 501 CAM_DEBUG(ccb_h->path, CAM_DEBUG_SUBTRACE, 502 ("SCSI IO received for invalid lun %d\n", lid)); 503 goto action_invalid; 504 } 505 if (csio->cdb_len > sizeof request->u.atapi.ccb) { 506 CAM_DEBUG(ccb_h->path, CAM_DEBUG_SUBTRACE, 507 ("CAM CCB too long for ATAPI")); 508 goto action_invalid; 509 } 510 if ((ccb_h->flags & CAM_SCATTER_VALID)) { 511 /* scatter-gather not supported */ 512 xpt_print_path(ccb_h->path); 513 kprintf("ATAPI/CAM does not support scatter-gather yet!\n"); 514 goto action_invalid; 515 } 516 517 switch (ccb_h->flags & CAM_DIR_MASK) { 518 case CAM_DIR_IN: 519 request_flags |= ATA_R_READ|ATA_R_DMA; 520 break; 521 case CAM_DIR_OUT: 522 request_flags |= ATA_R_WRITE|ATA_R_DMA; 523 break; 524 case CAM_DIR_NONE: 525 /* No flags need to be set */ 526 break; 527 default: 528 device_printf(softc->dev, "unknown IO operation\n"); 529 goto action_invalid; 530 } 531 if (softc->atadev[tid]->mode < ATA_DMA) 532 request_flags &= ~ATA_R_DMA; 533 534 if ((hcb = allocate_hcb(softc, unit, bus, ccb)) == NULL) { 535 kprintf("cannot allocate ATAPI/CAM hcb\n"); 536 goto action_oom; 537 } 538 if ((request = ata_alloc_request()) == NULL) { 539 kprintf("cannot allocate ATAPI/CAM request\n"); 540 goto action_oom; 541 } 542 543 bcopy((ccb_h->flags & CAM_CDB_POINTER) ? 544 csio->cdb_io.cdb_ptr : csio->cdb_io.cdb_bytes, 545 request->u.atapi.ccb, csio->cdb_len); 546 #ifdef CAMDEBUG 547 if (CAM_DEBUGGED(ccb_h->path, CAM_DEBUG_CDB)) { 548 char cdb_str[(SCSI_MAX_CDBLEN * 3) + 1]; 549 550 kprintf("atapi_action: hcb@%p: %s\n", hcb, 551 scsi_cdb_string(request->u.atapi.ccb, cdb_str, sizeof(cdb_str))); 552 } 553 #endif 554 555 len = csio->dxfer_len; 556 buf = csio->data_ptr; 557 558 /* some SCSI commands require special processing */ 559 switch (request->u.atapi.ccb[0]) { 560 case INQUIRY: { 561 /* 562 * many ATAPI devices seem to report more than 563 * SHORT_INQUIRY_LENGTH bytes of available INQUIRY 564 * information, but respond with some incorrect condition 565 * when actually asked for it, so we are going to pretend 566 * that only SHORT_INQUIRY_LENGTH are expected, anyway. 567 */ 568 struct scsi_inquiry *inq = (struct scsi_inquiry *) &request->u.atapi.ccb[0]; 569 570 if (inq->byte2 == 0 && inq->page_code == 0 && 571 inq->length > SHORT_INQUIRY_LENGTH) { 572 bzero(buf, len); 573 len = inq->length = SHORT_INQUIRY_LENGTH; 574 } 575 break; 576 } 577 case READ_6: 578 /* FALLTHROUGH */ 579 580 case WRITE_6: 581 CAM_DEBUG(ccb_h->path, CAM_DEBUG_SUBTRACE, 582 ("Translating %s into _10 equivalent\n", 583 (request->u.atapi.ccb[0] == READ_6) ? "READ_6" : "WRITE_6")); 584 request->u.atapi.ccb[0] |= 0x20; 585 request->u.atapi.ccb[9] = request->u.atapi.ccb[5]; 586 request->u.atapi.ccb[8] = request->u.atapi.ccb[4]; 587 request->u.atapi.ccb[7] = 0; 588 request->u.atapi.ccb[6] = 0; 589 request->u.atapi.ccb[5] = request->u.atapi.ccb[3]; 590 request->u.atapi.ccb[4] = request->u.atapi.ccb[2]; 591 request->u.atapi.ccb[3] = request->u.atapi.ccb[1] & 0x1f; 592 request->u.atapi.ccb[2] = 0; 593 request->u.atapi.ccb[1] = 0; 594 break; 595 } 596 597 if ((ccb_h->flags & CAM_DIR_MASK) == CAM_DIR_IN && (len & 1)) { 598 /* ATA always transfers an even number of bytes */ 599 buf = hcb->dxfer_alloc = 600 kmalloc(++len, M_ATACAM, M_INTWAIT | M_ZERO); 601 } 602 request->dev = softc->atadev[tid]->dev; 603 request->driver = hcb; 604 request->data = buf; 605 request->bytecount = len; 606 request->transfersize = min(request->bytecount, 607 min(softc->ata_ch->dma->max_iosize, 65534)); 608 request->timeout = ccb_h->timeout / 1000; /* XXX lost granularity */ 609 request->retries = 2; 610 request->callback = &atapi_cb; 611 request->flags = request_flags; 612 613 TAILQ_INSERT_TAIL(&softc->pending_hcbs, hcb, chain); 614 hcb->flags |= QUEUED; 615 ccb_h->status |= CAM_SIM_QUEUED; 616 spin_unlock_wr(&softc->state_lock); 617 618 ata_queue_request(request); 619 return; 620 } 621 622 default: 623 CAM_DEBUG(ccb_h->path, CAM_DEBUG_SUBTRACE, 624 ("unsupported function code 0x%02x\n", ccb_h->func_code)); 625 goto action_invalid; 626 } 627 628 /* NOTREACHED */ 629 630 action_oom: 631 if (request != NULL) 632 ata_free_request(request); 633 if (hcb != NULL) 634 free_hcb(hcb); 635 spin_unlock_wr(&softc->state_lock); 636 get_mplock(); 637 xpt_print_path(ccb_h->path); 638 kprintf("out of memory, freezing queue.\n"); 639 softc->flags |= RESOURCE_SHORTAGE; 640 xpt_freeze_simq(sim, /*count*/ 1); 641 rel_mplock(); 642 ccb_h->status = CAM_REQUEUE_REQ; 643 xpt_done(ccb); 644 return; 645 646 action_invalid: 647 spin_unlock_wr(&softc->state_lock); 648 ccb_h->status = CAM_REQ_INVALID; 649 xpt_done(ccb); 650 return; 651 } 652 653 static void 654 atapi_poll(struct cam_sim *sim) 655 { 656 /* do nothing - we do not actually service any interrupts */ 657 kprintf("atapi_poll called!\n"); 658 } 659 660 static void 661 atapi_cb(struct ata_request *request) 662 { 663 struct atapi_xpt_softc *scp; 664 struct atapi_hcb *hcb; 665 struct ccb_scsiio *csio; 666 u_int32_t rc; 667 668 hcb = (struct atapi_hcb *)request->driver; 669 scp = hcb->softc; 670 csio = &hcb->ccb->csio; 671 672 #ifdef CAMDEBUG 673 # define err (request->u.atapi.sense.key) 674 if (CAM_DEBUGGED(csio->ccb_h.path, CAM_DEBUG_CDB)) { 675 kprintf("atapi_cb: hcb@%p error = %02x: (sk = %02x%s%s%s)\n", 676 hcb, err, err >> 4, 677 (err & 4) ? " ABRT" : "", 678 (err & 2) ? " EOM" : "", 679 (err & 1) ? " ILI" : ""); 680 kprintf("dev %s: cmd %02x status %02x result %02x\n", 681 device_get_nameunit(request->dev), request->u.atapi.ccb[0], 682 request->status, request->result); 683 } 684 #endif 685 686 if ((hcb->flags & AUTOSENSE) != 0) { 687 rc = CAM_SCSI_STATUS_ERROR; 688 if (request->result == 0) { 689 csio->ccb_h.status |= CAM_AUTOSNS_VALID; 690 } 691 } else if (request->result != 0) { 692 rc = CAM_SCSI_STATUS_ERROR; 693 csio->scsi_status = SCSI_STATUS_CHECK_COND; 694 695 if ((csio->ccb_h.flags & CAM_DIS_AUTOSENSE) == 0) { 696 #if 0 697 static const int8_t ccb[16] = { ATAPI_REQUEST_SENSE, 0, 0, 0, 698 sizeof(struct atapi_sense), 0, 0, 0, 0, 0, 0, 699 0, 0, 0, 0, 0 }; 700 701 bcopy (ccb, request->u.atapi.ccb, sizeof ccb); 702 request->data = (caddr_t)&csio->sense_data; 703 request->bytecount = sizeof(struct atapi_sense); 704 request->transfersize = min(request->bytecount, 65534); 705 request->timeout = csio->ccb_h.timeout / 1000; 706 request->retries = 2; 707 request->flags = ATA_R_QUIET|ATA_R_ATAPI|ATA_R_IMMEDIATE; 708 hcb->flags |= AUTOSENSE; 709 710 ata_queue_request(request); 711 return; 712 #else 713 /* The ATA driver has already requested sense for us. */ 714 if (request->error == 0) { 715 /* The ATA autosense suceeded. */ 716 bcopy (&request->u.atapi.sense, &csio->sense_data, sizeof(struct atapi_sense)); 717 csio->ccb_h.status |= CAM_AUTOSNS_VALID; 718 } 719 #endif 720 } 721 } else { 722 rc = CAM_REQ_CMP; 723 csio->scsi_status = SCSI_STATUS_OK; 724 if (((csio->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN) && 725 hcb->dxfer_alloc != NULL) 726 { 727 bcopy(hcb->dxfer_alloc, csio->data_ptr, csio->dxfer_len); 728 } 729 } 730 731 spin_lock_wr(&scp->state_lock); 732 free_hcb_and_ccb_done(hcb, rc); 733 spin_unlock_wr(&scp->state_lock); 734 735 ata_free_request(request); 736 } 737 738 static void 739 free_hcb_and_ccb_done(struct atapi_hcb *hcb, u_int32_t status) 740 { 741 struct atapi_xpt_softc *softc; 742 union ccb *ccb; 743 744 if (hcb == NULL) 745 return; 746 747 softc = hcb->softc; 748 ccb = hcb->ccb; 749 750 /* we're about to free a hcb, so the shortage has ended */ 751 if (softc->flags & RESOURCE_SHORTAGE) { 752 softc->flags &= ~RESOURCE_SHORTAGE; 753 status |= CAM_RELEASE_SIMQ; 754 } 755 free_hcb(hcb); 756 ccb->ccb_h.status = 757 status | (ccb->ccb_h.status & ~(CAM_STATUS_MASK | CAM_SIM_QUEUED)); 758 xpt_done(ccb); 759 } 760 761 static void 762 atapi_async(void *callback_arg, u_int32_t code, 763 struct cam_path* path, void *arg) 764 { 765 struct atapi_xpt_softc *softc; 766 struct cam_sim *sim; 767 int targ; 768 769 crit_enter(); 770 771 sim = (struct cam_sim *) callback_arg; 772 softc = (struct atapi_xpt_softc *) cam_sim_softc(sim); 773 switch (code) { 774 case AC_LOST_DEVICE: 775 targ = xpt_path_target_id(path); 776 xpt_print_path(path); 777 if (targ == -1) 778 kprintf("Lost host adapter\n"); 779 else 780 kprintf("Lost target %d???\n", targ); 781 break; 782 783 default: 784 break; 785 } 786 787 crit_exit(); 788 } 789 790 static void 791 cam_rescan_callback(struct cam_periph *periph, union ccb *ccb) 792 { 793 if (ccb->ccb_h.status != CAM_REQ_CMP) { 794 CAM_DEBUG(ccb->ccb_h.path, CAM_DEBUG_TRACE, 795 ("Rescan failed, 0x%04x\n", ccb->ccb_h.status)); 796 } else { 797 CAM_DEBUG(ccb->ccb_h.path, CAM_DEBUG_TRACE, 798 ("Rescan succeeded\n")); 799 } 800 xpt_free_path(ccb->ccb_h.path); 801 kfree(ccb, M_ATACAM); 802 } 803 804 static void 805 cam_rescan(struct cam_sim *sim) 806 { 807 struct cam_path *path; 808 union ccb *ccb = kmalloc(sizeof(union ccb), M_ATACAM, M_WAITOK | M_ZERO); 809 810 get_mplock(); 811 if (xpt_create_path(&path, xpt_periph, cam_sim_path(sim), 812 CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD) != CAM_REQ_CMP) { 813 rel_mplock(); 814 kfree(ccb, M_ATACAM); 815 return; 816 } 817 818 CAM_DEBUG(ccb->ccb_h.path, CAM_DEBUG_TRACE, ("Rescanning ATAPI bus.\n")); 819 xpt_setup_ccb(&ccb->ccb_h, path, 5/*priority (low)*/); 820 ccb->ccb_h.func_code = XPT_SCAN_BUS; 821 ccb->ccb_h.cbfcnp = cam_rescan_callback; 822 ccb->crcn.flags = CAM_FLAG_NONE; 823 xpt_action(ccb); 824 /* scan is in progress now */ 825 rel_mplock(); 826 } 827 828 static struct atapi_hcb * 829 allocate_hcb(struct atapi_xpt_softc *softc, int unit, int bus, union ccb *ccb) 830 { 831 struct atapi_hcb *hcb = (struct atapi_hcb *) 832 kmalloc(sizeof(struct atapi_hcb), M_ATACAM, M_INTWAIT | M_ZERO); 833 834 hcb->softc = softc; 835 hcb->unit = unit; 836 hcb->bus = bus; 837 hcb->ccb = ccb; 838 return hcb; 839 } 840 841 static void 842 free_hcb(struct atapi_hcb *hcb) 843 { 844 if ((hcb->flags & QUEUED) != 0) 845 TAILQ_REMOVE(&hcb->softc->pending_hcbs, hcb, chain); 846 if (hcb->dxfer_alloc != NULL) 847 kfree(hcb->dxfer_alloc, M_ATACAM); 848 kfree(hcb, M_ATACAM); 849 } 850 851 static void 852 free_softc(struct atapi_xpt_softc *scp) 853 { 854 struct atapi_hcb *hcb; 855 856 if (scp != NULL) { 857 spin_lock_wr(&scp->state_lock); 858 TAILQ_FOREACH(hcb, &scp->pending_hcbs, chain) { 859 free_hcb_and_ccb_done(hcb, CAM_UNREC_HBA_ERROR); 860 } 861 spin_unlock_wr(&scp->state_lock); 862 get_mplock(); 863 if (scp->path != NULL) { 864 setup_async_cb(scp, 0); 865 xpt_free_path(scp->path); 866 } 867 if ((scp->flags & BUS_REGISTERED) != 0) { 868 if (xpt_bus_deregister(cam_sim_path(scp->sim)) == CAM_REQ_CMP) 869 scp->flags &= ~BUS_REGISTERED; 870 } 871 if (scp->sim != NULL) { 872 if ((scp->flags & BUS_REGISTERED) == 0) 873 cam_sim_free(scp->sim); 874 else 875 kprintf("Can't free %s SIM (still registered)\n", 876 cam_sim_name(scp->sim)); 877 } 878 rel_mplock(); 879 spin_uninit(&scp->state_lock); 880 } 881 } 882 883 static int 884 atapi_cam_event_handler(module_t mod, int what, void *arg) { 885 device_t *devlist; 886 int devcount; 887 888 switch (what) { 889 case MOD_UNLOAD: 890 if (devclass_get_devices(atapi_cam_devclass, &devlist, &devcount) 891 != 0) 892 return ENXIO; 893 if (devlist != NULL) { 894 while (devlist != NULL && devcount > 0) { 895 device_t child = devlist[--devcount]; 896 struct atapi_xpt_softc *scp = device_get_softc(child); 897 898 device_delete_child(device_get_parent(child),child); 899 if (scp != NULL) 900 kfree(scp, M_ATACAM); 901 } 902 kfree(devlist, M_TEMP); 903 } 904 break; 905 906 default: 907 break; 908 } 909 return 0; 910 } 911