scsi_pass.c revision 46581
1/* 2 * Copyright (c) 1997, 1998 Justin T. Gibbs. 3 * Copyright (c) 1997, 1998, 1999 Kenneth D. Merry. 4 * All rights reserved. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions, and the following disclaimer, 11 * without modification, immediately at the beginning of the file. 12 * 2. The name of the author may not be used to endorse or promote products 13 * derived from this software without specific prior written permission. 14 * 15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR 19 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 25 * SUCH DAMAGE. 26 * 27 * $Id: scsi_pass.c,v 1.6 1999/02/10 00:03:15 ken Exp $ 28 */ 29 30#include <sys/param.h> 31#include <sys/queue.h> 32#include <sys/systm.h> 33#include <sys/kernel.h> 34#include <sys/types.h> 35#include <sys/buf.h> 36#include <sys/dkbad.h> 37#include <sys/disklabel.h> 38#include <sys/diskslice.h> 39#include <sys/malloc.h> 40#include <sys/fcntl.h> 41#include <sys/stat.h> 42#include <sys/conf.h> 43#include <sys/buf.h> 44#include <sys/proc.h> 45#include <sys/errno.h> 46#include <sys/devicestat.h> 47 48#include <cam/cam.h> 49#include <cam/cam_ccb.h> 50#include <cam/cam_extend.h> 51#include <cam/cam_periph.h> 52#include <cam/cam_xpt_periph.h> 53#include <cam/cam_debug.h> 54 55#include <cam/scsi/scsi_all.h> 56#include <cam/scsi/scsi_message.h> 57#include <cam/scsi/scsi_da.h> 58#include <cam/scsi/scsi_pass.h> 59 60typedef enum { 61 PASS_FLAG_OPEN = 0x01, 62 PASS_FLAG_LOCKED = 0x02, 63 PASS_FLAG_INVALID = 0x04 64} pass_flags; 65 66typedef enum { 67 PASS_STATE_NORMAL 68} pass_state; 69 70typedef enum { 71 PASS_CCB_BUFFER_IO, 72 PASS_CCB_WAITING 73} pass_ccb_types; 74 75#define ccb_type ppriv_field0 76#define ccb_bp ppriv_ptr1 77 78struct pass_softc { 79 pass_state state; 80 pass_flags flags; 81 u_int8_t pd_type; 82 struct buf_queue_head buf_queue; 83 union ccb saved_ccb; 84 struct devstat device_stats; 85#ifdef DEVFS 86 void *pass_devfs_token; 87 void *ctl_devfs_token; 88#endif 89}; 90 91#ifndef MIN 92#define MIN(x,y) ((x<y) ? x : y) 93#endif 94 95#define PASS_CDEV_MAJOR 31 96 97static d_open_t passopen; 98static d_read_t passread; 99static d_write_t passwrite; 100static d_close_t passclose; 101static d_ioctl_t passioctl; 102static d_strategy_t passstrategy; 103 104static periph_init_t passinit; 105static periph_ctor_t passregister; 106static periph_oninv_t passoninvalidate; 107static periph_dtor_t passcleanup; 108static periph_start_t passstart; 109static void passasync(void *callback_arg, u_int32_t code, 110 struct cam_path *path, void *arg); 111static void passdone(struct cam_periph *periph, 112 union ccb *done_ccb); 113static int passerror(union ccb *ccb, u_int32_t cam_flags, 114 u_int32_t sense_flags); 115static int passsendccb(struct cam_periph *periph, union ccb *ccb, 116 union ccb *inccb); 117 118static struct periph_driver passdriver = 119{ 120 passinit, "pass", 121 TAILQ_HEAD_INITIALIZER(passdriver.units), /* generation */ 0 122}; 123 124DATA_SET(periphdriver_set, passdriver); 125 126static struct cdevsw pass_cdevsw = 127{ 128 /*d_open*/ passopen, 129 /*d_close*/ passclose, 130 /*d_read*/ passread, 131 /*d_write*/ passwrite, 132 /*d_ioctl*/ passioctl, 133 /*d_stop*/ nostop, 134 /*d_reset*/ noreset, 135 /*d_devtotty*/ nodevtotty, 136 /*d_poll*/ seltrue, 137 /*d_mmap*/ nommap, 138 /*d_strategy*/ passstrategy, 139 /*d_name*/ "pass", 140 /*d_spare*/ NULL, 141 /*d_maj*/ -1, 142 /*d_dump*/ nodump, 143 /*d_psize*/ nopsize, 144 /*d_flags*/ 0, 145 /*d_maxio*/ 0, 146 /*b_maj*/ -1 147}; 148 149static struct extend_array *passperiphs; 150 151static void 152passinit(void) 153{ 154 cam_status status; 155 struct cam_path *path; 156 157 /* 158 * Create our extend array for storing the devices we attach to. 159 */ 160 passperiphs = cam_extend_new(); 161 if (passperiphs == NULL) { 162 printf("passm: Failed to alloc extend array!\n"); 163 return; 164 } 165 166 /* 167 * Install a global async callback. This callback will 168 * receive async callbacks like "new device found". 169 */ 170 status = xpt_create_path(&path, /*periph*/NULL, CAM_XPT_PATH_ID, 171 CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD); 172 173 if (status == CAM_REQ_CMP) { 174 struct ccb_setasync csa; 175 176 xpt_setup_ccb(&csa.ccb_h, path, /*priority*/5); 177 csa.ccb_h.func_code = XPT_SASYNC_CB; 178 csa.event_enable = AC_FOUND_DEVICE; 179 csa.callback = passasync; 180 csa.callback_arg = NULL; 181 xpt_action((union ccb *)&csa); 182 status = csa.ccb_h.status; 183 xpt_free_path(path); 184 } 185 186 if (status != CAM_REQ_CMP) { 187 printf("pass: Failed to attach master async callback " 188 "due to status 0x%x!\n", status); 189 } else { 190 dev_t dev; 191 192 /* If we were successfull, register our devsw */ 193 dev = makedev(PASS_CDEV_MAJOR, 0); 194 cdevsw_add(&dev, &pass_cdevsw, NULL); 195 } 196 197} 198 199static void 200passoninvalidate(struct cam_periph *periph) 201{ 202 int s; 203 struct pass_softc *softc; 204 struct buf *q_bp; 205 struct ccb_setasync csa; 206 207 softc = (struct pass_softc *)periph->softc; 208 209 /* 210 * De-register any async callbacks. 211 */ 212 xpt_setup_ccb(&csa.ccb_h, periph->path, 213 /* priority */ 5); 214 csa.ccb_h.func_code = XPT_SASYNC_CB; 215 csa.event_enable = 0; 216 csa.callback = passasync; 217 csa.callback_arg = periph; 218 xpt_action((union ccb *)&csa); 219 220 softc->flags |= PASS_FLAG_INVALID; 221 222 /* 223 * Although the oninvalidate() routines are always called at 224 * splsoftcam, we need to be at splbio() here to keep the buffer 225 * queue from being modified while we traverse it. 226 */ 227 s = splbio(); 228 229 /* 230 * Return all queued I/O with ENXIO. 231 * XXX Handle any transactions queued to the card 232 * with XPT_ABORT_CCB. 233 */ 234 while ((q_bp = bufq_first(&softc->buf_queue)) != NULL){ 235 bufq_remove(&softc->buf_queue, q_bp); 236 q_bp->b_resid = q_bp->b_bcount; 237 q_bp->b_error = ENXIO; 238 q_bp->b_flags |= B_ERROR; 239 biodone(q_bp); 240 } 241 splx(s); 242 243 if (bootverbose) { 244 xpt_print_path(periph->path); 245 printf("lost device\n"); 246 } 247 248} 249 250static void 251passcleanup(struct cam_periph *periph) 252{ 253 struct pass_softc *softc; 254 255 softc = (struct pass_softc *)periph->softc; 256 257 devstat_remove_entry(&softc->device_stats); 258 259 cam_extend_release(passperiphs, periph->unit_number); 260 261 if (bootverbose) { 262 xpt_print_path(periph->path); 263 printf("removing device entry\n"); 264 } 265 free(softc, M_DEVBUF); 266} 267 268static void 269passasync(void *callback_arg, u_int32_t code, 270 struct cam_path *path, void *arg) 271{ 272 struct cam_periph *periph; 273 274 periph = (struct cam_periph *)callback_arg; 275 276 switch (code) { 277 case AC_FOUND_DEVICE: 278 { 279 struct ccb_getdev *cgd; 280 cam_status status; 281 282 cgd = (struct ccb_getdev *)arg; 283 284 /* 285 * Allocate a peripheral instance for 286 * this device and start the probe 287 * process. 288 */ 289 status = cam_periph_alloc(passregister, passoninvalidate, 290 passcleanup, passstart, "pass", 291 CAM_PERIPH_BIO, cgd->ccb_h.path, 292 passasync, AC_FOUND_DEVICE, cgd); 293 294 if (status != CAM_REQ_CMP 295 && status != CAM_REQ_INPROG) 296 printf("passasync: Unable to attach new device " 297 "due to status 0x%x\n", status); 298 299 break; 300 } 301 case AC_LOST_DEVICE: 302 cam_periph_invalidate(periph); 303 break; 304 case AC_TRANSFER_NEG: 305 case AC_SENT_BDR: 306 case AC_SCSI_AEN: 307 case AC_UNSOL_RESEL: 308 case AC_BUS_RESET: 309 default: 310 break; 311 } 312} 313 314static cam_status 315passregister(struct cam_periph *periph, void *arg) 316{ 317 struct pass_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("passregister: periph was NULL!!\n"); 324 return(CAM_REQ_CMP_ERR); 325 } 326 327 if (cgd == NULL) { 328 printf("passregister: no getdev CCB, can't register device\n"); 329 return(CAM_REQ_CMP_ERR); 330 } 331 332 softc = (struct pass_softc *)malloc(sizeof(*softc), 333 M_DEVBUF, M_NOWAIT); 334 335 if (softc == NULL) { 336 printf("passregister: Unable to probe new device. " 337 "Unable to allocate softc\n"); 338 return(CAM_REQ_CMP_ERR); 339 } 340 341 bzero(softc, sizeof(*softc)); 342 softc->state = PASS_STATE_NORMAL; 343 softc->pd_type = cgd->pd_type; 344 bufq_init(&softc->buf_queue); 345 346 periph->softc = softc; 347 348 cam_extend_set(passperiphs, periph->unit_number, periph); 349 /* 350 * We pass in 0 for a blocksize, since we don't 351 * know what the blocksize of this device is, if 352 * it even has a blocksize. 353 */ 354 devstat_add_entry(&softc->device_stats, "pass", periph->unit_number, 355 0, DEVSTAT_NO_BLOCKSIZE | DEVSTAT_NO_ORDERED_TAGS, 356 cgd->pd_type | 357 DEVSTAT_TYPE_IF_SCSI | 358 DEVSTAT_TYPE_PASS, 359 DEVSTAT_PRIORITY_PASS); 360 /* 361 * Add an async callback so that we get 362 * notified if this device goes away. 363 */ 364 xpt_setup_ccb(&csa.ccb_h, periph->path, /* priority */ 5); 365 csa.ccb_h.func_code = XPT_SASYNC_CB; 366 csa.event_enable = AC_LOST_DEVICE; 367 csa.callback = passasync; 368 csa.callback_arg = periph; 369 xpt_action((union ccb *)&csa); 370 371 if (bootverbose) 372 xpt_announce_periph(periph, NULL); 373 374 return(CAM_REQ_CMP); 375} 376 377static int 378passopen(dev_t dev, int flags, int fmt, struct proc *p) 379{ 380 struct cam_periph *periph; 381 struct pass_softc *softc; 382 int unit, error; 383 int s; 384 385 error = 0; /* default to no error */ 386 387 /* unit = dkunit(dev); */ 388 /* XXX KDM fix this */ 389 unit = minor(dev) & 0xff; 390 391 periph = cam_extend_get(passperiphs, unit); 392 393 if (periph == NULL) 394 return (ENXIO); 395 396 softc = (struct pass_softc *)periph->softc; 397 398 s = splsoftcam(); 399 if (softc->flags & PASS_FLAG_INVALID) { 400 splx(s); 401 return(ENXIO); 402 } 403 404 /* 405 * Don't allow access when we're running at a high securelvel. 406 */ 407 if (securelevel > 1) { 408 splx(s); 409 return(EPERM); 410 } 411 412 /* 413 * Only allow read-write access. 414 */ 415 if (((flags & FWRITE) == 0) || ((flags & FREAD) == 0)) { 416 splx(s); 417 return(EPERM); 418 } 419 420 /* 421 * We don't allow nonblocking access. 422 */ 423 if ((flags & O_NONBLOCK) != 0) { 424 xpt_print_path(periph->path); 425 printf("can't do nonblocking accesss\n"); 426 splx(s); 427 return(EINVAL); 428 } 429 430 if ((error = cam_periph_lock(periph, PRIBIO | PCATCH)) != 0) { 431 splx(s); 432 return (error); 433 } 434 435 splx(s); 436 437 if ((softc->flags & PASS_FLAG_OPEN) == 0) { 438 if (cam_periph_acquire(periph) != CAM_REQ_CMP) 439 return(ENXIO); 440 softc->flags |= PASS_FLAG_OPEN; 441 } 442 443 cam_periph_unlock(periph); 444 445 return (error); 446} 447 448static int 449passclose(dev_t dev, int flag, int fmt, struct proc *p) 450{ 451 struct cam_periph *periph; 452 struct pass_softc *softc; 453 int unit, error; 454 455 /* unit = dkunit(dev); */ 456 /* XXX KDM fix this */ 457 unit = minor(dev) & 0xff; 458 459 periph = cam_extend_get(passperiphs, unit); 460 if (periph == NULL) 461 return (ENXIO); 462 463 softc = (struct pass_softc *)periph->softc; 464 465 if ((error = cam_periph_lock(periph, PRIBIO)) != 0) 466 return (error); 467 468 softc->flags &= ~PASS_FLAG_OPEN; 469 470 cam_periph_unlock(periph); 471 cam_periph_release(periph); 472 473 return (0); 474} 475 476static int 477passread(dev_t dev, struct uio *uio, int ioflag) 478{ 479 return(physio(passstrategy, NULL, dev, 1, minphys, uio)); 480} 481 482static int 483passwrite(dev_t dev, struct uio *uio, int ioflag) 484{ 485 return(physio(passstrategy, NULL, dev, 0, minphys, uio)); 486} 487 488/* 489 * Actually translate the requested transfer into one the physical driver 490 * can understand. The transfer is described by a buf and will include 491 * only one physical transfer. 492 */ 493static void 494passstrategy(struct buf *bp) 495{ 496 struct cam_periph *periph; 497 struct pass_softc *softc; 498 u_int unit; 499 int s; 500 501 /* 502 * The read/write interface for the passthrough driver doesn't 503 * really work right now. So, we just pass back EINVAL to tell the 504 * user to go away. 505 */ 506 bp->b_error = EINVAL; 507 goto bad; 508 509 /* unit = dkunit(bp->b_dev); */ 510 /* XXX KDM fix this */ 511 unit = minor(bp->b_dev) & 0xff; 512 513 periph = cam_extend_get(passperiphs, unit); 514 if (periph == NULL) { 515 bp->b_error = ENXIO; 516 goto bad; 517 } 518 softc = (struct pass_softc *)periph->softc; 519 520 /* 521 * Odd number of bytes or negative offset 522 */ 523 /* valid request? */ 524 if (bp->b_blkno < 0) { 525 bp->b_error = EINVAL; 526 goto bad; 527 } 528 529 /* 530 * Mask interrupts so that the pack cannot be invalidated until 531 * after we are in the queue. Otherwise, we might not properly 532 * clean up one of the buffers. 533 */ 534 s = splbio(); 535 536 bufq_insert_tail(&softc->buf_queue, bp); 537 538 splx(s); 539 540 /* 541 * Schedule ourselves for performing the work. 542 */ 543 xpt_schedule(periph, /* XXX priority */1); 544 545 return; 546bad: 547 bp->b_flags |= B_ERROR; 548 549 /* 550 * Correctly set the buf to indicate a completed xfer 551 */ 552 bp->b_resid = bp->b_bcount; 553 biodone(bp); 554 return; 555} 556 557static void 558passstart(struct cam_periph *periph, union ccb *start_ccb) 559{ 560 struct pass_softc *softc; 561 int s; 562 563 softc = (struct pass_softc *)periph->softc; 564 565 switch (softc->state) { 566 case PASS_STATE_NORMAL: 567 { 568 struct buf *bp; 569 570 s = splbio(); 571 bp = bufq_first(&softc->buf_queue); 572 if (periph->immediate_priority <= periph->pinfo.priority) { 573 start_ccb->ccb_h.ccb_type = PASS_CCB_WAITING; 574 SLIST_INSERT_HEAD(&periph->ccb_list, &start_ccb->ccb_h, 575 periph_links.sle); 576 periph->immediate_priority = CAM_PRIORITY_NONE; 577 splx(s); 578 wakeup(&periph->ccb_list); 579 } else if (bp == NULL) { 580 splx(s); 581 xpt_release_ccb(start_ccb); 582 } else { 583 584 bufq_remove(&softc->buf_queue, bp); 585 586 devstat_start_transaction(&softc->device_stats); 587 588 /* 589 * XXX JGibbs - 590 * Interpret the contents of the bp as a CCB 591 * and pass it to a routine shared by our ioctl 592 * code and passtart. 593 * For now, just biodone it with EIO so we don't 594 * hang. 595 */ 596 bp->b_error = EIO; 597 bp->b_flags |= B_ERROR; 598 bp->b_resid = bp->b_bcount; 599 biodone(bp); 600 bp = bufq_first(&softc->buf_queue); 601 splx(s); 602 603 xpt_action(start_ccb); 604 605 } 606 if (bp != NULL) { 607 /* Have more work to do, so ensure we stay scheduled */ 608 xpt_schedule(periph, /* XXX priority */1); 609 } 610 break; 611 } 612 } 613} 614static void 615passdone(struct cam_periph *periph, union ccb *done_ccb) 616{ 617 struct pass_softc *softc; 618 struct ccb_scsiio *csio; 619 620 softc = (struct pass_softc *)periph->softc; 621 csio = &done_ccb->csio; 622 switch (csio->ccb_h.ccb_type) { 623 case PASS_CCB_BUFFER_IO: 624 { 625 struct buf *bp; 626 cam_status status; 627 u_int8_t scsi_status; 628 devstat_trans_flags ds_flags; 629 630 status = done_ccb->ccb_h.status; 631 scsi_status = done_ccb->csio.scsi_status; 632 bp = (struct buf *)done_ccb->ccb_h.ccb_bp; 633 /* XXX handle errors */ 634 if (!(((status & CAM_STATUS_MASK) == CAM_REQ_CMP) 635 && (scsi_status == SCSI_STATUS_OK))) { 636 int error; 637 638 if ((error = passerror(done_ccb, 0, 0)) == ERESTART) { 639 /* 640 * A retry was scheuled, so 641 * just return. 642 */ 643 return; 644 } 645 646 /* 647 * XXX unfreeze the queue after we complete 648 * the abort process 649 */ 650 bp->b_error = error; 651 bp->b_flags |= B_ERROR; 652 } 653 654 if ((done_ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN) 655 ds_flags = DEVSTAT_READ; 656 else if ((done_ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_OUT) 657 ds_flags = DEVSTAT_WRITE; 658 else 659 ds_flags = DEVSTAT_NO_DATA; 660 661 devstat_end_transaction(&softc->device_stats, bp->b_bcount, 662 done_ccb->csio.tag_action & 0xf, 663 ds_flags); 664 665 biodone(bp); 666 break; 667 } 668 case PASS_CCB_WAITING: 669 { 670 /* Caller will release the CCB */ 671 wakeup(&done_ccb->ccb_h.cbfcnp); 672 return; 673 } 674 } 675 xpt_release_ccb(done_ccb); 676} 677 678static int 679passioctl(dev_t dev, u_long cmd, caddr_t addr, int flag, struct proc *p) 680{ 681 struct cam_periph *periph; 682 struct pass_softc *softc; 683 u_int8_t unit; 684 int error; 685 686 687 /* unit = dkunit(dev); */ 688 /* XXX KDM fix this */ 689 unit = minor(dev) & 0xff; 690 691 periph = cam_extend_get(passperiphs, unit); 692 693 if (periph == NULL) 694 return(ENXIO); 695 696 softc = (struct pass_softc *)periph->softc; 697 698 error = 0; 699 700 switch (cmd) { 701 702 case CAMIOCOMMAND: 703 { 704 union ccb *inccb; 705 union ccb *ccb; 706 int ccb_malloced; 707 708 inccb = (union ccb *)addr; 709 710 /* 711 * Some CCB types, like scan bus and scan lun can only go 712 * through the transport layer device. 713 */ 714 if (inccb->ccb_h.func_code & XPT_FC_XPT_ONLY) { 715 xpt_print_path(periph->path); 716 printf("CCB function code %#x is restricted to the " 717 "XPT device\n", inccb->ccb_h.func_code); 718 error = ENODEV; 719 break; 720 } 721 722 /* 723 * Non-immediate CCBs need a CCB from the per-device pool 724 * of CCBs, which is scheduled by the transport layer. 725 * Immediate CCBs and user-supplied CCBs should just be 726 * malloced. 727 */ 728 if ((inccb->ccb_h.func_code & XPT_FC_QUEUED) 729 && ((inccb->ccb_h.func_code & XPT_FC_USER_CCB) == 0)) { 730 ccb = cam_periph_getccb(periph, 731 inccb->ccb_h.pinfo.priority); 732 ccb_malloced = 0; 733 } else { 734 ccb = xpt_alloc_ccb(); 735 736 if (ccb != NULL) 737 xpt_setup_ccb(&ccb->ccb_h, periph->path, 738 inccb->ccb_h.pinfo.priority); 739 ccb_malloced = 1; 740 } 741 742 if (ccb == NULL) { 743 xpt_print_path(periph->path); 744 printf("unable to allocate CCB\n"); 745 error = ENOMEM; 746 break; 747 } 748 749 error = passsendccb(periph, ccb, inccb); 750 751 if (ccb_malloced) 752 xpt_free_ccb(ccb); 753 else 754 xpt_release_ccb(ccb); 755 756 break; 757 } 758 default: 759 error = cam_periph_ioctl(periph, cmd, addr, passerror); 760 break; 761 } 762 763 return(error); 764} 765 766/* 767 * Generally, "ccb" should be the CCB supplied by the kernel. "inccb" 768 * should be the CCB that is copied in from the user. 769 */ 770static int 771passsendccb(struct cam_periph *periph, union ccb *ccb, union ccb *inccb) 772{ 773 struct pass_softc *softc; 774 struct cam_periph_map_info mapinfo; 775 int error, need_unmap; 776 777 softc = (struct pass_softc *)periph->softc; 778 779 need_unmap = 0; 780 781 /* 782 * There are some fields in the CCB header that need to be 783 * preserved, the rest we get from the user. 784 */ 785 xpt_merge_ccb(ccb, inccb); 786 787 /* 788 * There's no way for the user to have a completion 789 * function, so we put our own completion function in here. 790 */ 791 ccb->ccb_h.cbfcnp = passdone; 792 793 /* 794 * We only attempt to map the user memory into kernel space 795 * if they haven't passed in a physical memory pointer, 796 * and if there is actually an I/O operation to perform. 797 * Right now cam_periph_mapmem() only supports SCSI and device 798 * match CCBs. For the SCSI CCBs, we only pass the CCB in if 799 * there's actually data to map. cam_periph_mapmem() will do the 800 * right thing, even if there isn't data to map, but since CCBs 801 * without data are a reasonably common occurance (e.g. test unit 802 * ready), it will save a few cycles if we check for it here. 803 */ 804 if (((ccb->ccb_h.flags & CAM_DATA_PHYS) == 0) 805 && (((ccb->ccb_h.func_code == XPT_SCSI_IO) 806 && ((ccb->ccb_h.flags & CAM_DIR_MASK) != CAM_DIR_NONE)) 807 || (ccb->ccb_h.func_code == XPT_DEV_MATCH))) { 808 809 bzero(&mapinfo, sizeof(mapinfo)); 810 811 error = cam_periph_mapmem(ccb, &mapinfo); 812 813 /* 814 * cam_periph_mapmem returned an error, we can't continue. 815 * Return the error to the user. 816 */ 817 if (error) 818 return(error); 819 820 /* 821 * We successfully mapped the memory in, so we need to 822 * unmap it when the transaction is done. 823 */ 824 need_unmap = 1; 825 } 826 827 /* 828 * If the user wants us to perform any error recovery, then honor 829 * that request. Otherwise, it's up to the user to perform any 830 * error recovery. 831 */ 832 error = cam_periph_runccb(ccb, 833 (ccb->ccb_h.flags & CAM_PASS_ERR_RECOVER) ? 834 passerror : NULL, 835 /* cam_flags */ 0, 836 /* sense_flags */SF_RETRY_UA, 837 &softc->device_stats); 838 839 if (need_unmap != 0) 840 cam_periph_unmapmem(ccb, &mapinfo); 841 842 ccb->ccb_h.cbfcnp = NULL; 843 ccb->ccb_h.periph_priv = inccb->ccb_h.periph_priv; 844 bcopy(ccb, inccb, sizeof(union ccb)); 845 846 return(error); 847} 848 849static int 850passerror(union ccb *ccb, u_int32_t cam_flags, u_int32_t sense_flags) 851{ 852 struct cam_periph *periph; 853 struct pass_softc *softc; 854 855 periph = xpt_path_periph(ccb->ccb_h.path); 856 softc = (struct pass_softc *)periph->softc; 857 858 return(cam_periph_error(ccb, cam_flags, sense_flags, 859 &softc->saved_ccb)); 860} 861