scsi_pt.c revision 40398
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.1 1998/09/15 06:36:34 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_read_t ptread; 86static d_write_t ptwrite; 87static d_close_t ptclose; 88static d_strategy_t ptstrategy; 89static periph_init_t ptinit; 90static void ptasync(void *callback_arg, u_int32_t code, 91 struct cam_path *path, void *arg); 92static periph_ctor_t ptctor; 93static periph_dtor_t ptdtor; 94static periph_start_t ptstart; 95static void ptdone(struct cam_periph *periph, 96 union ccb *done_ccb); 97static int pterror(union ccb *ccb, u_int32_t cam_flags, 98 u_int32_t sense_flags); 99 100void scsi_send_receive(struct ccb_scsiio *csio, u_int32_t retries, 101 void (*cbfcnp)(struct cam_periph *, union ccb *), 102 u_int tag_action, int readop, u_int byte2, 103 u_int32_t xfer_len, u_int8_t *data_ptr, 104 u_int8_t sense_len, u_int32_t timeout); 105 106static struct periph_driver ptdriver = 107{ 108 ptinit, "pt", 109 TAILQ_HEAD_INITIALIZER(ptdriver.units), /* generation */ 0 110}; 111 112DATA_SET(periphdriver_set, ptdriver); 113 114#define PT_CDEV_MAJOR 61 115 116static struct cdevsw pt_cdevsw = 117{ 118 /*d_open*/ ptopen, 119 /*d_close*/ ptclose, 120 /*d_read*/ ptread, 121 /*d_write*/ ptwrite, 122 /*d_ioctl*/ noioctl, 123 /*d_stop*/ nostop, 124 /*d_reset*/ noreset, 125 /*d_devtotty*/ nodevtotty, 126 /*d_poll*/ seltrue, 127 /*d_mmap*/ nommap, 128 /*d_strategy*/ ptstrategy, 129 /*d_name*/ "pt", 130 /*d_spare*/ NULL, 131 /*d_maj*/ -1, 132 /*d_dump*/ nodump, 133 /*d_psize*/ nopsize, 134 /*d_flags*/ 0, 135 /*d_maxio*/ 0, 136 /*b_maj*/ -1 137}; 138 139static struct extend_array *ptperiphs; 140 141static int 142ptopen(dev_t dev, int flags, int fmt, struct proc *p) 143{ 144 struct cam_periph *periph; 145 struct pt_softc *softc; 146 int unit; 147 int error; 148 149 unit = minor(dev); 150 periph = cam_extend_get(ptperiphs, unit); 151 if (periph == NULL) 152 return (ENXIO); 153 154 softc = (struct pt_softc *)periph->softc; 155 156 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, 157 ("ptopen: dev=0x%x (unit %d)\n", dev, unit)); 158 159 if ((error = cam_periph_lock(periph, PRIBIO|PCATCH)) != 0) 160 return (error); /* error code from tsleep */ 161 162 if ((softc->flags & PT_FLAG_OPEN) == 0) { 163 if (cam_periph_acquire(periph) != CAM_REQ_CMP) 164 error = ENXIO; 165 else 166 softc->flags |= PT_FLAG_OPEN; 167 } else 168 error = EBUSY; 169 170 cam_periph_unlock(periph); 171 return (error); 172} 173 174static int 175ptclose(dev_t dev, int flag, int fmt, struct proc *p) 176{ 177 struct cam_periph *periph; 178 struct pt_softc *softc; 179 int unit; 180 int error; 181 182 unit = minor(dev); 183 periph = cam_extend_get(ptperiphs, unit); 184 if (periph == NULL) 185 return (ENXIO); 186 187 softc = (struct pt_softc *)periph->softc; 188 189 if ((error = cam_periph_lock(periph, PRIBIO)) != 0) 190 return (error); /* error code from tsleep */ 191 192 softc->flags &= ~PT_FLAG_OPEN; 193 cam_periph_unlock(periph); 194 cam_periph_release(periph); 195 return (0); 196} 197 198static int 199ptread(dev_t dev, struct uio *uio, int ioflag) 200{ 201 return(physio(ptstrategy, NULL, dev, 1, minphys, uio)); 202} 203 204static int 205ptwrite(dev_t dev, struct uio *uio, int ioflag) 206{ 207 return(physio(ptstrategy, NULL, dev, 0, minphys, uio)); 208} 209 210/* 211 * Actually translate the requested transfer into one the physical driver 212 * can understand. The transfer is described by a buf and will include 213 * only one physical transfer. 214 */ 215static void 216ptstrategy(struct buf *bp) 217{ 218 struct cam_periph *periph; 219 struct pt_softc *softc; 220 u_int unit; 221 int s; 222 223 unit = minor(bp->b_dev); 224 periph = cam_extend_get(ptperiphs, unit); 225 if (periph == NULL) { 226 bp->b_error = ENXIO; 227 goto bad; 228 } 229 softc = (struct pt_softc *)periph->softc; 230 231 /* 232 * Mask interrupts so that the pack cannot be invalidated until 233 * after we are in the queue. Otherwise, we might not properly 234 * clean up one of the buffers. 235 */ 236 s = splbio(); 237 238 /* 239 * If the device has been made invalid, error out 240 */ 241 if ((softc->flags & PT_FLAG_DEVICE_INVALID)) { 242 splx(s); 243 bp->b_error = ENXIO; 244 goto bad; 245 } 246 247 /* 248 * Place it in the queue of disk activities for this disk 249 */ 250 bufq_insert_tail(&softc->buf_queue, bp); 251 252 splx(s); 253 254 /* 255 * Schedule ourselves for performing the work. 256 */ 257 xpt_schedule(periph, /* XXX priority */1); 258 259 return; 260bad: 261 bp->b_flags |= B_ERROR; 262 263 /* 264 * Correctly set the buf to indicate a completed xfer 265 */ 266 bp->b_resid = bp->b_bcount; 267 biodone(bp); 268} 269 270static void 271ptinit(void) 272{ 273 cam_status status; 274 struct cam_path *path; 275 276 /* 277 * Create our extend array for storing the devices we attach to. 278 */ 279 ptperiphs = cam_extend_new(); 280 if (ptperiphs == NULL) { 281 printf("pt: Failed to alloc extend array!\n"); 282 return; 283 } 284 285 /* 286 * Install a global async callback. This callback will 287 * receive async callbacks like "new device found". 288 */ 289 status = xpt_create_path(&path, /*periph*/NULL, CAM_XPT_PATH_ID, 290 CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD); 291 292 if (status == CAM_REQ_CMP) { 293 struct ccb_setasync csa; 294 295 xpt_setup_ccb(&csa.ccb_h, path, /*priority*/5); 296 csa.ccb_h.func_code = XPT_SASYNC_CB; 297 csa.event_enable = AC_FOUND_DEVICE; 298 csa.callback = ptasync; 299 csa.callback_arg = NULL; 300 xpt_action((union ccb *)&csa); 301 status = csa.ccb_h.status; 302 xpt_free_path(path); 303 } 304 305 if (status != CAM_REQ_CMP) { 306 printf("pt: Failed to attach master async callback " 307 "due to status 0x%x!\n", status); 308 } else { 309 /* If we were successfull, register our devsw */ 310 dev_t dev; 311 312 dev = makedev(PT_CDEV_MAJOR, 0); 313 cdevsw_add(&dev,&pt_cdevsw, NULL); 314 } 315} 316 317static cam_status 318ptctor(struct cam_periph *periph, void *arg) 319{ 320 struct pt_softc *softc; 321 struct ccb_setasync csa; 322 struct ccb_getdev *cgd; 323 324 cgd = (struct ccb_getdev *)arg; 325 if (periph == NULL) { 326 printf("ptregister: periph was NULL!!\n"); 327 return(CAM_REQ_CMP_ERR); 328 } 329 330 if (cgd == NULL) { 331 printf("ptregister: no getdev CCB, can't register device\n"); 332 return(CAM_REQ_CMP_ERR); 333 } 334 335 softc = (struct pt_softc *)malloc(sizeof(*softc),M_DEVBUF,M_NOWAIT); 336 337 if (softc == NULL) { 338 printf("daregister: Unable to probe new device. " 339 "Unable to allocate softc\n"); 340 return(CAM_REQ_CMP_ERR); 341 } 342 343 bzero(softc, sizeof(*softc)); 344 LIST_INIT(&softc->pending_ccbs); 345 softc->state = PT_STATE_NORMAL; 346 bufq_init(&softc->buf_queue); 347 348 periph->softc = softc; 349 350 cam_extend_set(ptperiphs, periph->unit_number, periph); 351 352 /* 353 * The DA driver supports a blocksize, but 354 * we don't know the blocksize until we do 355 * a read capacity. So, set a flag to 356 * indicate that the blocksize is 357 * unavailable right now. We'll clear the 358 * flag as soon as we've done a read capacity. 359 */ 360 devstat_add_entry(&softc->device_stats, "pt", 361 periph->unit_number, 0, 362 DEVSTAT_NO_BLOCKSIZE, 363 cgd->pd_type | DEVSTAT_TYPE_IF_SCSI); 364 365 /* 366 * Add async callbacks for bus reset and 367 * bus device reset calls. I don't bother 368 * checking if this fails as, in most cases, 369 * the system will function just fine without 370 * them and the only alternative would be to 371 * not attach the device on failure. 372 */ 373 xpt_setup_ccb(&csa.ccb_h, periph->path, /*priority*/5); 374 csa.ccb_h.func_code = XPT_SASYNC_CB; 375 csa.event_enable = AC_SENT_BDR | AC_BUS_RESET | AC_LOST_DEVICE; 376 csa.callback = ptasync; 377 csa.callback_arg = periph; 378 xpt_action((union ccb *)&csa); 379 380 /* Tell the user we've attached to the device */ 381 xpt_announce_periph(periph, NULL); 382 383 return(CAM_REQ_CMP); 384} 385 386static void 387ptdtor(struct cam_periph *periph) 388{ 389 cam_extend_release(ptperiphs, periph->unit_number); 390 xpt_print_path(periph->path); 391 printf("removing device entry\n"); 392 free(periph->softc, M_DEVBUF); 393} 394 395static void 396ptasync(void *callback_arg, u_int32_t code, struct cam_path *path, void *arg) 397{ 398 struct cam_periph *periph; 399 400 periph = (struct cam_periph *)callback_arg; 401 switch (code) { 402 case AC_FOUND_DEVICE: 403 { 404 struct ccb_getdev *cgd; 405 cam_status status; 406 407 cgd = (struct ccb_getdev *)arg; 408 409 if (cgd->pd_type != T_PROCESSOR) 410 break; 411 412 /* 413 * Allocate a peripheral instance for 414 * this device and start the probe 415 * process. 416 */ 417 status = cam_periph_alloc(ptctor, ptdtor, ptstart, 418 "pt", CAM_PERIPH_BIO, cgd->ccb_h.path, 419 ptasync, AC_FOUND_DEVICE, cgd); 420 421 if (status != CAM_REQ_CMP 422 && status != CAM_REQ_INPROG) 423 printf("ptasync: Unable to attach to new device " 424 "due to status 0x%x\n", status); 425 break; 426 } 427 case AC_LOST_DEVICE: 428 { 429 int s; 430 struct pt_softc *softc; 431 struct buf *q_bp; 432 struct ccb_setasync csa; 433 434 softc = (struct pt_softc *)periph->softc; 435 436 /* 437 * Insure that no other async callbacks that 438 * might affect this peripheral can come through. 439 */ 440 s = splcam(); 441 442 /* 443 * De-register any async callbacks. 444 */ 445 xpt_setup_ccb(&csa.ccb_h, periph->path, 446 /* priority */ 5); 447 csa.ccb_h.func_code = XPT_SASYNC_CB; 448 csa.event_enable = 0; 449 csa.callback = ptasync; 450 csa.callback_arg = periph; 451 xpt_action((union ccb *)&csa); 452 453 softc->flags |= PT_FLAG_DEVICE_INVALID; 454 455 /* 456 * Return all queued I/O with ENXIO. 457 * XXX Handle any transactions queued to the card 458 * with XPT_ABORT_CCB. 459 */ 460 while ((q_bp = bufq_first(&softc->buf_queue)) != NULL){ 461 bufq_remove(&softc->buf_queue, q_bp); 462 q_bp->b_resid = q_bp->b_bcount; 463 q_bp->b_error = ENXIO; 464 q_bp->b_flags |= B_ERROR; 465 biodone(q_bp); 466 } 467 devstat_remove_entry(&softc->device_stats); 468 469 xpt_print_path(periph->path); 470 printf("lost device\n"); 471 472 splx(s); 473 474 cam_periph_invalidate(periph); 475 break; 476 } 477 case AC_SENT_BDR: 478 case AC_BUS_RESET: 479 { 480 struct pt_softc *softc; 481 struct ccb_hdr *ccbh; 482 int s; 483 484 softc = (struct pt_softc *)periph->softc; 485 s = splsoftcam(); 486 /* 487 * Don't fail on the expected unit attention 488 * that will occur. 489 */ 490 softc->flags |= PT_FLAG_RETRY_UA; 491 for (ccbh = LIST_FIRST(&softc->pending_ccbs); 492 ccbh != NULL; ccbh = LIST_NEXT(ccbh, periph_links.le)) 493 ccbh->ccb_state |= PT_CCB_RETRY_UA; 494 splx(s); 495 break; 496 } 497 case AC_TRANSFER_NEG: 498 case AC_SCSI_AEN: 499 case AC_UNSOL_RESEL: 500 default: 501 break; 502 } 503} 504 505static void 506ptstart(struct cam_periph *periph, union ccb *start_ccb) 507{ 508 struct pt_softc *softc; 509 struct buf *bp; 510 int s; 511 512 softc = (struct pt_softc *)periph->softc; 513 514 /* 515 * See if there is a buf with work for us to do.. 516 */ 517 s = splbio(); 518 bp = bufq_first(&softc->buf_queue); 519 if (periph->immediate_priority <= periph->pinfo.priority) { 520 CAM_DEBUG_PRINT(CAM_DEBUG_SUBTRACE, 521 ("queuing for immediate ccb\n")); 522 start_ccb->ccb_h.ccb_state = PT_CCB_WAITING; 523 SLIST_INSERT_HEAD(&periph->ccb_list, &start_ccb->ccb_h, 524 periph_links.sle); 525 periph->immediate_priority = CAM_PRIORITY_NONE; 526 splx(s); 527 wakeup(&periph->ccb_list); 528 } else if (bp == NULL) { 529 splx(s); 530 xpt_release_ccb(start_ccb); 531 } else { 532 int oldspl; 533 534 bufq_remove(&softc->buf_queue, bp); 535 536 devstat_start_transaction(&softc->device_stats); 537 538 scsi_send_receive(&start_ccb->csio, 539 /*retries*/4, 540 ptdone, 541 MSG_SIMPLE_Q_TAG, 542 bp->b_flags & B_READ, 543 /*byte2*/0, 544 bp->b_bcount, 545 bp->b_data, 546 /*sense_len*/SSD_FULL_SIZE, 547 /*timeout*/10000); 548 549 start_ccb->ccb_h.ccb_state = PT_CCB_BUFFER_IO; 550 551 /* 552 * Block out any asyncronous callbacks 553 * while we touch the pending ccb list. 554 */ 555 oldspl = splcam(); 556 LIST_INSERT_HEAD(&softc->pending_ccbs, &start_ccb->ccb_h, 557 periph_links.le); 558 splx(oldspl); 559 560 start_ccb->ccb_h.ccb_bp = bp; 561 bp = bufq_first(&softc->buf_queue); 562 splx(s); 563 564 xpt_action(start_ccb); 565 566 if (bp != NULL) { 567 /* Have more work to do, so ensure we stay scheduled */ 568 xpt_schedule(periph, /* XXX priority */1); 569 } 570 } 571} 572 573static void 574ptdone(struct cam_periph *periph, union ccb *done_ccb) 575{ 576 struct pt_softc *softc; 577 struct ccb_scsiio *csio; 578 579 softc = (struct pt_softc *)periph->softc; 580 csio = &done_ccb->csio; 581 switch (csio->ccb_h.ccb_state) { 582 case PT_CCB_BUFFER_IO: 583 case PT_CCB_BUFFER_IO_UA: 584 { 585 struct buf *bp; 586 int oldspl; 587 588 bp = (struct buf *)done_ccb->ccb_h.ccb_bp; 589 if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) { 590 int error; 591 int s; 592 int sf; 593 594 if ((csio->ccb_h.ccb_state & PT_CCB_RETRY_UA) != 0) 595 sf = SF_RETRY_UA; 596 else 597 sf = 0; 598 599 if ((error = pterror(done_ccb, 0, sf)) == ERESTART) { 600 /* 601 * A retry was scheuled, so 602 * just return. 603 */ 604 return; 605 } 606 if (error != 0) { 607 struct buf *q_bp; 608 609 s = splbio(); 610 611 if (error == ENXIO) { 612 /* 613 * Catastrophic error. Mark our device 614 * as invalid. 615 */ 616 xpt_print_path(periph->path); 617 printf("Invalidating device\n"); 618 softc->flags |= PT_FLAG_DEVICE_INVALID; 619 } 620 621 /* 622 * return all queued I/O with EIO, so that 623 * the client can retry these I/Os in the 624 * proper order should it attempt to recover. 625 */ 626 while ((q_bp = bufq_first(&softc->buf_queue)) 627 != NULL) { 628 bufq_remove(&softc->buf_queue, q_bp); 629 q_bp->b_resid = q_bp->b_bcount; 630 q_bp->b_error = EIO; 631 q_bp->b_flags |= B_ERROR; 632 biodone(q_bp); 633 } 634 splx(s); 635 bp->b_error = error; 636 bp->b_resid = bp->b_bcount; 637 bp->b_flags |= B_ERROR; 638 } else { 639 bp->b_resid = csio->resid; 640 bp->b_error = 0; 641 if (bp->b_resid != 0) { 642 /* Short transfer ??? */ 643 bp->b_flags |= B_ERROR; 644 } 645 } 646 if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) 647 cam_release_devq(done_ccb->ccb_h.path, 648 /*relsim_flags*/0, 649 /*reduction*/0, 650 /*timeout*/0, 651 /*getcount_only*/0); 652 } else { 653 bp->b_resid = csio->resid; 654 if (bp->b_resid != 0) 655 bp->b_flags |= B_ERROR; 656 } 657 658 /* 659 * Block out any asyncronous callbacks 660 * while we touch the pending ccb list. 661 */ 662 oldspl = splcam(); 663 LIST_REMOVE(&done_ccb->ccb_h, periph_links.le); 664 splx(oldspl); 665 666 devstat_end_transaction(&softc->device_stats, 667 bp->b_bcount - bp->b_resid, 668 done_ccb->csio.tag_action & 0xf, 669 (bp->b_flags & B_READ) ? DEVSTAT_READ 670 : DEVSTAT_WRITE); 671 672 biodone(bp); 673 break; 674 } 675 case PT_CCB_WAITING: 676 /* Caller will release the CCB */ 677 wakeup(&done_ccb->ccb_h.cbfcnp); 678 return; 679 } 680 xpt_release_ccb(done_ccb); 681} 682 683static int 684pterror(union ccb *ccb, u_int32_t cam_flags, u_int32_t sense_flags) 685{ 686 struct pt_softc *softc; 687 struct cam_periph *periph; 688 689 periph = xpt_path_periph(ccb->ccb_h.path); 690 softc = (struct pt_softc *)periph->softc; 691 692 return(cam_periph_error(ccb, cam_flags, sense_flags, 693 &softc->saved_ccb)); 694} 695 696void 697scsi_send_receive(struct ccb_scsiio *csio, u_int32_t retries, 698 void (*cbfcnp)(struct cam_periph *, union ccb *), 699 u_int tag_action, int readop, u_int byte2, 700 u_int32_t xfer_len, u_int8_t *data_ptr, u_int8_t sense_len, 701 u_int32_t timeout) 702{ 703 struct scsi_send_receive *scsi_cmd; 704 705 scsi_cmd = (struct scsi_send_receive *)&csio->cdb_io.cdb_bytes; 706 scsi_cmd->opcode = readop ? RECEIVE : SEND; 707 scsi_cmd->byte2 = byte2; 708 scsi_ulto3b(xfer_len, scsi_cmd->xfer_len); 709 scsi_cmd->control = 0; 710 711 cam_fill_csio(csio, 712 retries, 713 cbfcnp, 714 /*flags*/readop ? CAM_DIR_IN : CAM_DIR_OUT, 715 tag_action, 716 data_ptr, 717 xfer_len, 718 sense_len, 719 sizeof(*scsi_cmd), 720 timeout); 721} 722