scsi_pt.c revision 47625
1/* 2 * Implementation of SCSI Processor Target Peripheral driver for CAM. 3 * 4 * Copyright (c) 1998 Justin T. Gibbs. 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions, and the following disclaimer, 12 * without modification, immediately at the beginning of the file. 13 * 2. The name of the author may not be used to endorse or promote products 14 * derived from this software without specific prior written permission. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR 20 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26 * SUCH DAMAGE. 27 * 28 * $Id: scsi_pt.c,v 1.8 1999/05/22 22:00:22 gibbs Exp $ 29 */ 30 31#include <sys/param.h> 32#include <sys/queue.h> 33#include <sys/systm.h> 34#include <sys/kernel.h> 35#include <sys/types.h> 36#include <sys/buf.h> 37#include <sys/devicestat.h> 38#include <sys/malloc.h> 39#include <sys/conf.h> 40 41#include <cam/cam.h> 42#include <cam/cam_ccb.h> 43#include <cam/cam_extend.h> 44#include <cam/cam_periph.h> 45#include <cam/cam_xpt_periph.h> 46#include <cam/cam_debug.h> 47 48#include <cam/scsi/scsi_all.h> 49#include <cam/scsi/scsi_message.h> 50#include <cam/scsi/scsi_pt.h> 51 52typedef enum { 53 PT_STATE_PROBE, 54 PT_STATE_NORMAL 55} pt_state; 56 57typedef enum { 58 PT_FLAG_NONE = 0x00, 59 PT_FLAG_OPEN = 0x01, 60 PT_FLAG_DEVICE_INVALID = 0x02, 61 PT_FLAG_RETRY_UA = 0x04 62} pt_flags; 63 64typedef enum { 65 PT_CCB_BUFFER_IO = 0x01, 66 PT_CCB_WAITING = 0x02, 67 PT_CCB_RETRY_UA = 0x04, 68 PT_CCB_BUFFER_IO_UA = PT_CCB_BUFFER_IO|PT_CCB_RETRY_UA 69} pt_ccb_state; 70 71/* Offsets into our private area for storing information */ 72#define ccb_state ppriv_field0 73#define ccb_bp ppriv_ptr1 74 75struct pt_softc { 76 struct buf_queue_head buf_queue; 77 struct devstat device_stats; 78 LIST_HEAD(, ccb_hdr) pending_ccbs; 79 pt_state state; 80 pt_flags flags; 81 union ccb saved_ccb; 82}; 83 84static d_open_t ptopen; 85static d_close_t ptclose; 86static d_strategy_t ptstrategy; 87static periph_init_t ptinit; 88static void ptasync(void *callback_arg, u_int32_t code, 89 struct cam_path *path, void *arg); 90static periph_ctor_t ptctor; 91static periph_oninv_t ptoninvalidate; 92static periph_dtor_t ptdtor; 93static periph_start_t ptstart; 94static void ptdone(struct cam_periph *periph, 95 union ccb *done_ccb); 96static int pterror(union ccb *ccb, u_int32_t cam_flags, 97 u_int32_t sense_flags); 98 99void scsi_send_receive(struct ccb_scsiio *csio, u_int32_t retries, 100 void (*cbfcnp)(struct cam_periph *, union ccb *), 101 u_int tag_action, int readop, u_int byte2, 102 u_int32_t xfer_len, u_int8_t *data_ptr, 103 u_int8_t sense_len, u_int32_t timeout); 104 105static struct periph_driver ptdriver = 106{ 107 ptinit, "pt", 108 TAILQ_HEAD_INITIALIZER(ptdriver.units), /* generation */ 0 109}; 110 111DATA_SET(periphdriver_set, ptdriver); 112 113#define PT_CDEV_MAJOR 61 114 115static struct cdevsw pt_cdevsw = { 116 /* open */ ptopen, 117 /* close */ ptclose, 118 /* read */ physread, 119 /* write */ physwrite, 120 /* ioctl */ noioctl, 121 /* stop */ nostop, 122 /* reset */ noreset, 123 /* devtotty */ nodevtotty, 124 /* poll */ nopoll, 125 /* mmap */ nommap, 126 /* strategy */ ptstrategy, 127 /* name */ "pt", 128 /* parms */ noparms, 129 /* maj */ PT_CDEV_MAJOR, 130 /* dump */ nodump, 131 /* psize */ nopsize, 132 /* flags */ 0, 133 /* maxio */ 0, 134 /* bmaj */ -1 135}; 136 137static struct extend_array *ptperiphs; 138 139static int 140ptopen(dev_t dev, int flags, int fmt, struct proc *p) 141{ 142 struct cam_periph *periph; 143 struct pt_softc *softc; 144 int unit; 145 int error; 146 int s; 147 148 unit = minor(dev); 149 periph = cam_extend_get(ptperiphs, unit); 150 if (periph == NULL) 151 return (ENXIO); 152 153 softc = (struct pt_softc *)periph->softc; 154 155 s = splsoftcam(); 156 if (softc->flags & PT_FLAG_DEVICE_INVALID) { 157 splx(s); 158 return(ENXIO); 159 } 160 161 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, 162 ("ptopen: dev=0x%x (unit %d)\n", dev, unit)); 163 164 if ((error = cam_periph_lock(periph, PRIBIO|PCATCH)) != 0) { 165 splx(s); 166 return (error); /* error code from tsleep */ 167 } 168 169 splx(s); 170 171 if ((softc->flags & PT_FLAG_OPEN) == 0) { 172 if (cam_periph_acquire(periph) != CAM_REQ_CMP) 173 error = ENXIO; 174 else 175 softc->flags |= PT_FLAG_OPEN; 176 } else 177 error = EBUSY; 178 179 cam_periph_unlock(periph); 180 return (error); 181} 182 183static int 184ptclose(dev_t dev, int flag, int fmt, struct proc *p) 185{ 186 struct cam_periph *periph; 187 struct pt_softc *softc; 188 int unit; 189 int error; 190 191 unit = minor(dev); 192 periph = cam_extend_get(ptperiphs, unit); 193 if (periph == NULL) 194 return (ENXIO); 195 196 softc = (struct pt_softc *)periph->softc; 197 198 if ((error = cam_periph_lock(periph, PRIBIO)) != 0) 199 return (error); /* error code from tsleep */ 200 201 softc->flags &= ~PT_FLAG_OPEN; 202 cam_periph_unlock(periph); 203 cam_periph_release(periph); 204 return (0); 205} 206 207/* 208 * Actually translate the requested transfer into one the physical driver 209 * can understand. The transfer is described by a buf and will include 210 * only one physical transfer. 211 */ 212static void 213ptstrategy(struct buf *bp) 214{ 215 struct cam_periph *periph; 216 struct pt_softc *softc; 217 u_int unit; 218 int s; 219 220 unit = minor(bp->b_dev); 221 periph = cam_extend_get(ptperiphs, unit); 222 if (periph == NULL) { 223 bp->b_error = ENXIO; 224 goto bad; 225 } 226 softc = (struct pt_softc *)periph->softc; 227 228 /* 229 * Mask interrupts so that the pack cannot be invalidated until 230 * after we are in the queue. Otherwise, we might not properly 231 * clean up one of the buffers. 232 */ 233 s = splbio(); 234 235 /* 236 * If the device has been made invalid, error out 237 */ 238 if ((softc->flags & PT_FLAG_DEVICE_INVALID)) { 239 splx(s); 240 bp->b_error = ENXIO; 241 goto bad; 242 } 243 244 /* 245 * Place it in the queue of disk activities for this disk 246 */ 247 bufq_insert_tail(&softc->buf_queue, bp); 248 249 splx(s); 250 251 /* 252 * Schedule ourselves for performing the work. 253 */ 254 xpt_schedule(periph, /* XXX priority */1); 255 256 return; 257bad: 258 bp->b_flags |= B_ERROR; 259 260 /* 261 * Correctly set the buf to indicate a completed xfer 262 */ 263 bp->b_resid = bp->b_bcount; 264 biodone(bp); 265} 266 267static void 268ptinit(void) 269{ 270 cam_status status; 271 struct cam_path *path; 272 273 /* 274 * Create our extend array for storing the devices we attach to. 275 */ 276 ptperiphs = cam_extend_new(); 277 if (ptperiphs == NULL) { 278 printf("pt: Failed to alloc extend array!\n"); 279 return; 280 } 281 282 /* 283 * Install a global async callback. This callback will 284 * receive async callbacks like "new device found". 285 */ 286 status = xpt_create_path(&path, /*periph*/NULL, CAM_XPT_PATH_ID, 287 CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD); 288 289 if (status == CAM_REQ_CMP) { 290 struct ccb_setasync csa; 291 292 xpt_setup_ccb(&csa.ccb_h, path, /*priority*/5); 293 csa.ccb_h.func_code = XPT_SASYNC_CB; 294 csa.event_enable = AC_FOUND_DEVICE; 295 csa.callback = ptasync; 296 csa.callback_arg = NULL; 297 xpt_action((union ccb *)&csa); 298 status = csa.ccb_h.status; 299 xpt_free_path(path); 300 } 301 302 if (status != CAM_REQ_CMP) { 303 printf("pt: Failed to attach master async callback " 304 "due to status 0x%x!\n", status); 305 } else { 306 /* If we were successfull, register our devsw */ 307 dev_t dev; 308 309 dev = makedev(PT_CDEV_MAJOR, 0); 310 cdevsw_add(&dev,&pt_cdevsw, NULL); 311 } 312} 313 314static cam_status 315ptctor(struct cam_periph *periph, void *arg) 316{ 317 struct pt_softc *softc; 318 struct ccb_setasync csa; 319 struct ccb_getdev *cgd; 320 321 cgd = (struct ccb_getdev *)arg; 322 if (periph == NULL) { 323 printf("ptregister: periph was NULL!!\n"); 324 return(CAM_REQ_CMP_ERR); 325 } 326 327 if (cgd == NULL) { 328 printf("ptregister: no getdev CCB, can't register device\n"); 329 return(CAM_REQ_CMP_ERR); 330 } 331 332 softc = (struct pt_softc *)malloc(sizeof(*softc),M_DEVBUF,M_NOWAIT); 333 334 if (softc == NULL) { 335 printf("daregister: Unable to probe new device. " 336 "Unable to allocate softc\n"); 337 return(CAM_REQ_CMP_ERR); 338 } 339 340 bzero(softc, sizeof(*softc)); 341 LIST_INIT(&softc->pending_ccbs); 342 softc->state = PT_STATE_NORMAL; 343 bufq_init(&softc->buf_queue); 344 345 periph->softc = softc; 346 347 cam_extend_set(ptperiphs, periph->unit_number, periph); 348 349 /* 350 * The DA driver supports a blocksize, but 351 * we don't know the blocksize until we do 352 * a read capacity. So, set a flag to 353 * indicate that the blocksize is 354 * unavailable right now. We'll clear the 355 * flag as soon as we've done a read capacity. 356 */ 357 devstat_add_entry(&softc->device_stats, "pt", 358 periph->unit_number, 0, 359 DEVSTAT_NO_BLOCKSIZE, 360 cgd->pd_type | DEVSTAT_TYPE_IF_SCSI, 361 DEVSTAT_PRIORITY_OTHER); 362 363 /* 364 * Add async callbacks for bus reset and 365 * bus device reset calls. I don't bother 366 * checking if this fails as, in most cases, 367 * the system will function just fine without 368 * them and the only alternative would be to 369 * not attach the device on failure. 370 */ 371 xpt_setup_ccb(&csa.ccb_h, periph->path, /*priority*/5); 372 csa.ccb_h.func_code = XPT_SASYNC_CB; 373 csa.event_enable = AC_SENT_BDR | AC_BUS_RESET | AC_LOST_DEVICE; 374 csa.callback = ptasync; 375 csa.callback_arg = periph; 376 xpt_action((union ccb *)&csa); 377 378 /* Tell the user we've attached to the device */ 379 xpt_announce_periph(periph, NULL); 380 381 return(CAM_REQ_CMP); 382} 383 384static void 385ptoninvalidate(struct cam_periph *periph) 386{ 387 int s; 388 struct pt_softc *softc; 389 struct buf *q_bp; 390 struct ccb_setasync csa; 391 392 softc = (struct pt_softc *)periph->softc; 393 394 /* 395 * De-register any async callbacks. 396 */ 397 xpt_setup_ccb(&csa.ccb_h, periph->path, 398 /* priority */ 5); 399 csa.ccb_h.func_code = XPT_SASYNC_CB; 400 csa.event_enable = 0; 401 csa.callback = ptasync; 402 csa.callback_arg = periph; 403 xpt_action((union ccb *)&csa); 404 405 softc->flags |= PT_FLAG_DEVICE_INVALID; 406 407 /* 408 * Although the oninvalidate() routines are always called at 409 * splsoftcam, we need to be at splbio() here to keep the buffer 410 * queue from being modified while we traverse it. 411 */ 412 s = splbio(); 413 414 /* 415 * Return all queued I/O with ENXIO. 416 * XXX Handle any transactions queued to the card 417 * with XPT_ABORT_CCB. 418 */ 419 while ((q_bp = bufq_first(&softc->buf_queue)) != NULL){ 420 bufq_remove(&softc->buf_queue, q_bp); 421 q_bp->b_resid = q_bp->b_bcount; 422 q_bp->b_error = ENXIO; 423 q_bp->b_flags |= B_ERROR; 424 biodone(q_bp); 425 } 426 427 splx(s); 428 429 xpt_print_path(periph->path); 430 printf("lost device\n"); 431} 432 433static void 434ptdtor(struct cam_periph *periph) 435{ 436 struct pt_softc *softc; 437 438 softc = (struct pt_softc *)periph->softc; 439 440 devstat_remove_entry(&softc->device_stats); 441 442 cam_extend_release(ptperiphs, periph->unit_number); 443 xpt_print_path(periph->path); 444 printf("removing device entry\n"); 445 free(softc, M_DEVBUF); 446} 447 448static void 449ptasync(void *callback_arg, u_int32_t code, struct cam_path *path, void *arg) 450{ 451 struct cam_periph *periph; 452 453 periph = (struct cam_periph *)callback_arg; 454 switch (code) { 455 case AC_FOUND_DEVICE: 456 { 457 struct ccb_getdev *cgd; 458 cam_status status; 459 460 cgd = (struct ccb_getdev *)arg; 461 462 if (cgd->pd_type != T_PROCESSOR) 463 break; 464 465 /* 466 * Allocate a peripheral instance for 467 * this device and start the probe 468 * process. 469 */ 470 status = cam_periph_alloc(ptctor, ptoninvalidate, ptdtor, 471 ptstart, "pt", CAM_PERIPH_BIO, 472 cgd->ccb_h.path, ptasync, 473 AC_FOUND_DEVICE, cgd); 474 475 if (status != CAM_REQ_CMP 476 && status != CAM_REQ_INPROG) 477 printf("ptasync: Unable to attach to new device " 478 "due to status 0x%x\n", status); 479 break; 480 } 481 case AC_SENT_BDR: 482 case AC_BUS_RESET: 483 { 484 struct pt_softc *softc; 485 struct ccb_hdr *ccbh; 486 int s; 487 488 softc = (struct pt_softc *)periph->softc; 489 s = splsoftcam(); 490 /* 491 * Don't fail on the expected unit attention 492 * that will occur. 493 */ 494 softc->flags |= PT_FLAG_RETRY_UA; 495 for (ccbh = LIST_FIRST(&softc->pending_ccbs); 496 ccbh != NULL; ccbh = LIST_NEXT(ccbh, periph_links.le)) 497 ccbh->ccb_state |= PT_CCB_RETRY_UA; 498 splx(s); 499 /* FALLTHROUGH */ 500 } 501 default: 502 cam_periph_async(periph, code, path, arg); 503 break; 504 } 505} 506 507static void 508ptstart(struct cam_periph *periph, union ccb *start_ccb) 509{ 510 struct pt_softc *softc; 511 struct buf *bp; 512 int s; 513 514 softc = (struct pt_softc *)periph->softc; 515 516 /* 517 * See if there is a buf with work for us to do.. 518 */ 519 s = splbio(); 520 bp = bufq_first(&softc->buf_queue); 521 if (periph->immediate_priority <= periph->pinfo.priority) { 522 CAM_DEBUG_PRINT(CAM_DEBUG_SUBTRACE, 523 ("queuing for immediate ccb\n")); 524 start_ccb->ccb_h.ccb_state = PT_CCB_WAITING; 525 SLIST_INSERT_HEAD(&periph->ccb_list, &start_ccb->ccb_h, 526 periph_links.sle); 527 periph->immediate_priority = CAM_PRIORITY_NONE; 528 splx(s); 529 wakeup(&periph->ccb_list); 530 } else if (bp == NULL) { 531 splx(s); 532 xpt_release_ccb(start_ccb); 533 } else { 534 int oldspl; 535 536 bufq_remove(&softc->buf_queue, bp); 537 538 devstat_start_transaction(&softc->device_stats); 539 540 scsi_send_receive(&start_ccb->csio, 541 /*retries*/4, 542 ptdone, 543 MSG_SIMPLE_Q_TAG, 544 bp->b_flags & B_READ, 545 /*byte2*/0, 546 bp->b_bcount, 547 bp->b_data, 548 /*sense_len*/SSD_FULL_SIZE, 549 /*timeout*/10000); 550 551 start_ccb->ccb_h.ccb_state = PT_CCB_BUFFER_IO; 552 553 /* 554 * Block out any asyncronous callbacks 555 * while we touch the pending ccb list. 556 */ 557 oldspl = splcam(); 558 LIST_INSERT_HEAD(&softc->pending_ccbs, &start_ccb->ccb_h, 559 periph_links.le); 560 splx(oldspl); 561 562 start_ccb->ccb_h.ccb_bp = bp; 563 bp = bufq_first(&softc->buf_queue); 564 splx(s); 565 566 xpt_action(start_ccb); 567 568 if (bp != NULL) { 569 /* Have more work to do, so ensure we stay scheduled */ 570 xpt_schedule(periph, /* XXX priority */1); 571 } 572 } 573} 574 575static void 576ptdone(struct cam_periph *periph, union ccb *done_ccb) 577{ 578 struct pt_softc *softc; 579 struct ccb_scsiio *csio; 580 581 softc = (struct pt_softc *)periph->softc; 582 csio = &done_ccb->csio; 583 switch (csio->ccb_h.ccb_state) { 584 case PT_CCB_BUFFER_IO: 585 case PT_CCB_BUFFER_IO_UA: 586 { 587 struct buf *bp; 588 int oldspl; 589 590 bp = (struct buf *)done_ccb->ccb_h.ccb_bp; 591 if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) { 592 int error; 593 int s; 594 int sf; 595 596 if ((csio->ccb_h.ccb_state & PT_CCB_RETRY_UA) != 0) 597 sf = SF_RETRY_UA; 598 else 599 sf = 0; 600 601 sf |= SF_RETRY_SELTO; 602 603 if ((error = pterror(done_ccb, 0, sf)) == ERESTART) { 604 /* 605 * A retry was scheuled, so 606 * just return. 607 */ 608 return; 609 } 610 if (error != 0) { 611 struct buf *q_bp; 612 613 s = splbio(); 614 615 if (error == ENXIO) { 616 /* 617 * Catastrophic error. Mark our device 618 * as invalid. 619 */ 620 xpt_print_path(periph->path); 621 printf("Invalidating device\n"); 622 softc->flags |= PT_FLAG_DEVICE_INVALID; 623 } 624 625 /* 626 * return all queued I/O with EIO, so that 627 * the client can retry these I/Os in the 628 * proper order should it attempt to recover. 629 */ 630 while ((q_bp = bufq_first(&softc->buf_queue)) 631 != NULL) { 632 bufq_remove(&softc->buf_queue, q_bp); 633 q_bp->b_resid = q_bp->b_bcount; 634 q_bp->b_error = EIO; 635 q_bp->b_flags |= B_ERROR; 636 biodone(q_bp); 637 } 638 splx(s); 639 bp->b_error = error; 640 bp->b_resid = bp->b_bcount; 641 bp->b_flags |= B_ERROR; 642 } else { 643 bp->b_resid = csio->resid; 644 bp->b_error = 0; 645 if (bp->b_resid != 0) { 646 /* Short transfer ??? */ 647 bp->b_flags |= B_ERROR; 648 } 649 } 650 if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) 651 cam_release_devq(done_ccb->ccb_h.path, 652 /*relsim_flags*/0, 653 /*reduction*/0, 654 /*timeout*/0, 655 /*getcount_only*/0); 656 } else { 657 bp->b_resid = csio->resid; 658 if (bp->b_resid != 0) 659 bp->b_flags |= B_ERROR; 660 } 661 662 /* 663 * Block out any asyncronous callbacks 664 * while we touch the pending ccb list. 665 */ 666 oldspl = splcam(); 667 LIST_REMOVE(&done_ccb->ccb_h, periph_links.le); 668 splx(oldspl); 669 670 devstat_end_transaction(&softc->device_stats, 671 bp->b_bcount - bp->b_resid, 672 done_ccb->csio.tag_action & 0xf, 673 (bp->b_flags & B_READ) ? DEVSTAT_READ 674 : DEVSTAT_WRITE); 675 676 biodone(bp); 677 break; 678 } 679 case PT_CCB_WAITING: 680 /* Caller will release the CCB */ 681 wakeup(&done_ccb->ccb_h.cbfcnp); 682 return; 683 } 684 xpt_release_ccb(done_ccb); 685} 686 687static int 688pterror(union ccb *ccb, u_int32_t cam_flags, u_int32_t sense_flags) 689{ 690 struct pt_softc *softc; 691 struct cam_periph *periph; 692 693 periph = xpt_path_periph(ccb->ccb_h.path); 694 softc = (struct pt_softc *)periph->softc; 695 696 return(cam_periph_error(ccb, cam_flags, sense_flags, 697 &softc->saved_ccb)); 698} 699 700void 701scsi_send_receive(struct ccb_scsiio *csio, u_int32_t retries, 702 void (*cbfcnp)(struct cam_periph *, union ccb *), 703 u_int tag_action, int readop, u_int byte2, 704 u_int32_t xfer_len, u_int8_t *data_ptr, u_int8_t sense_len, 705 u_int32_t timeout) 706{ 707 struct scsi_send_receive *scsi_cmd; 708 709 scsi_cmd = (struct scsi_send_receive *)&csio->cdb_io.cdb_bytes; 710 scsi_cmd->opcode = readop ? RECEIVE : SEND; 711 scsi_cmd->byte2 = byte2; 712 scsi_ulto3b(xfer_len, scsi_cmd->xfer_len); 713 scsi_cmd->control = 0; 714 715 cam_fill_csio(csio, 716 retries, 717 cbfcnp, 718 /*flags*/readop ? CAM_DIR_IN : CAM_DIR_OUT, 719 tag_action, 720 data_ptr, 721 xfer_len, 722 sense_len, 723 sizeof(*scsi_cmd), 724 timeout); 725} 726