1 /* 2 * Generic SCSI Target Kernel Mode Driver 3 * 4 * Copyright (c) 2002 Nate Lawson. 5 * Copyright (c) 1998, 1999, 2001, 2002 Justin T. Gibbs. 6 * All rights reserved. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions, and the following disclaimer, 13 * without modification, immediately at the beginning of the file. 14 * 2. 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 AND CONTRIBUTORS ``AS IS'' AND 18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR 21 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27 * SUCH DAMAGE. 28 * 29 * $FreeBSD: src/sys/cam/scsi/scsi_target.c,v 1.22.2.7 2003/02/18 22:07:10 njl Exp $ 30 */ 31 32 #include <sys/param.h> 33 #include <sys/systm.h> 34 #include <sys/kernel.h> 35 #include <sys/conf.h> 36 #include <sys/device.h> 37 #include <sys/malloc.h> 38 #include <sys/vnode.h> 39 #include <sys/devicestat.h> 40 #include <sys/thread2.h> 41 #include <sys/devfs.h> 42 43 #include "../cam.h" 44 #include "../cam_ccb.h" 45 #include "../cam_periph.h" 46 #include "../cam_xpt_periph.h" 47 #include "../cam_sim.h" 48 #include "scsi_targetio.h" 49 50 /* Transaction information attached to each CCB sent by the user */ 51 struct targ_cmd_descr { 52 struct cam_periph_map_info mapinfo; 53 TAILQ_ENTRY(targ_cmd_descr) tqe; 54 union ccb *user_ccb; 55 int priority; 56 int func_code; 57 }; 58 59 /* Offset into the private CCB area for storing our descriptor */ 60 #define targ_descr periph_priv.entries[1].ptr 61 62 TAILQ_HEAD(descr_queue, targ_cmd_descr); 63 64 typedef enum { 65 TARG_STATE_RESV = 0x00, /* Invalid state */ 66 TARG_STATE_OPENED = 0x01, /* Device opened, softc initialized */ 67 TARG_STATE_LUN_ENABLED = 0x02 /* Device enabled for a path */ 68 } targ_state; 69 70 /* Per-instance device software context */ 71 struct targ_softc { 72 /* CCBs (CTIOs, ATIOs, INOTs) pending on the controller */ 73 struct ccb_queue pending_ccb_queue; 74 75 /* Command descriptors awaiting CTIO resources from the XPT */ 76 struct descr_queue work_queue; 77 78 /* Command descriptors that have been aborted back to the user. */ 79 struct descr_queue abort_queue; 80 81 /* 82 * Queue of CCBs that have been copied out to userland, but our 83 * userland daemon has not yet seen. 84 */ 85 struct ccb_queue user_ccb_queue; 86 87 struct cam_periph *periph; 88 struct cam_path *path; 89 targ_state state; 90 struct kqinfo read_kq; 91 struct devstat device_stats; 92 }; 93 94 static d_open_t targopen; 95 static d_close_t targclose; 96 static d_read_t targread; 97 static d_write_t targwrite; 98 static d_ioctl_t targioctl; 99 static d_kqfilter_t targkqfilter; 100 static d_clone_t targclone; 101 DEVFS_DECLARE_CLONE_BITMAP(targ); 102 103 static void targfiltdetach(struct knote *kn); 104 static int targreadfilt(struct knote *kn, long hint); 105 static int targwritefilt(struct knote *kn, long hint); 106 static struct filterops targread_filtops = 107 { FILTEROP_ISFD, NULL, targfiltdetach, targreadfilt }; 108 static struct filterops targwrite_filtops = 109 { FILTEROP_ISFD, NULL, targfiltdetach, targwritefilt }; 110 111 static struct dev_ops targ_ops = { 112 { "targ", 0, 0 }, 113 .d_open = targopen, 114 .d_close = targclose, 115 .d_read = targread, 116 .d_write = targwrite, 117 .d_ioctl = targioctl, 118 .d_kqfilter = targkqfilter 119 }; 120 121 static cam_status targendislun(struct cam_path *path, int enable, 122 int grp6_len, int grp7_len); 123 static cam_status targenable(struct targ_softc *softc, 124 struct cam_path *path, 125 int grp6_len, int grp7_len); 126 static cam_status targdisable(struct targ_softc *softc); 127 static periph_ctor_t targctor; 128 static periph_dtor_t targdtor; 129 static periph_start_t targstart; 130 static int targusermerge(struct targ_softc *softc, 131 struct targ_cmd_descr *descr, 132 union ccb *ccb); 133 static int targsendccb(struct targ_softc *softc, union ccb *ccb, 134 struct targ_cmd_descr *descr); 135 static void targdone(struct cam_periph *periph, 136 union ccb *done_ccb); 137 static int targreturnccb(struct targ_softc *softc, 138 union ccb *ccb); 139 static union ccb * targgetccb(struct targ_softc *softc, xpt_opcode type, 140 int priority); 141 static void targfreeccb(struct targ_softc *softc, union ccb *ccb); 142 static struct targ_cmd_descr * 143 targgetdescr(struct targ_softc *softc); 144 static periph_init_t targinit; 145 static void targasync(void *callback_arg, u_int32_t code, 146 struct cam_path *path, void *arg); 147 static void abort_all_pending(struct targ_softc *softc); 148 static void notify_user(struct targ_softc *softc); 149 static int targcamstatus(cam_status status); 150 static size_t targccblen(xpt_opcode func_code); 151 152 static struct periph_driver targdriver = 153 { 154 targinit, "targ", 155 TAILQ_HEAD_INITIALIZER(targdriver.units), /* generation */ 0 156 }; 157 PERIPHDRIVER_DECLARE(targ, targdriver); 158 159 static MALLOC_DEFINE(M_TARG, "TARG", "TARG data"); 160 161 /* 162 * Create softc and initialize it. Only one proc can open each targ device. 163 * There is no locking here because a periph doesn't get created until an 164 * ioctl is issued to do so, and that can't happen until this method returns. 165 */ 166 static int 167 targopen(struct dev_open_args *ap) 168 { 169 cdev_t dev = ap->a_head.a_dev; 170 struct targ_softc *softc; 171 172 if (dev->si_drv1 != 0) { 173 return (EBUSY); 174 } 175 176 /* Mark device busy before any potentially blocking operations */ 177 dev->si_drv1 = (void *)~0; 178 reference_dev(dev); /* save ref for later destroy_dev() */ 179 180 /* Create the targ device, allocate its softc, initialize it */ 181 #if 0 182 make_dev(&targ_ops, minor(dev), UID_ROOT, GID_WHEEL, 0600, 183 "targ%d", lminor(dev)); 184 #endif 185 MALLOC(softc, struct targ_softc *, sizeof(*softc), M_TARG, 186 M_INTWAIT | M_ZERO); 187 dev->si_drv1 = softc; 188 softc->state = TARG_STATE_OPENED; 189 softc->periph = NULL; 190 softc->path = NULL; 191 192 TAILQ_INIT(&softc->pending_ccb_queue); 193 TAILQ_INIT(&softc->work_queue); 194 TAILQ_INIT(&softc->abort_queue); 195 TAILQ_INIT(&softc->user_ccb_queue); 196 197 return (0); 198 } 199 200 /* Disable LUN if enabled and teardown softc */ 201 static int 202 targclose(struct dev_close_args *ap) 203 { 204 cdev_t dev = ap->a_head.a_dev; 205 struct targ_softc *softc; 206 struct cam_periph *periph; 207 int error; 208 209 softc = (struct targ_softc *)dev->si_drv1; 210 if ((softc->periph == NULL) || 211 (softc->state & TARG_STATE_LUN_ENABLED) == 0) { 212 devfs_clone_bitmap_put(&DEVFS_CLONE_BITMAP(targ), dev->si_uminor); 213 destroy_dev(dev); 214 FREE(softc, M_TARG); 215 return (0); 216 } 217 218 /* 219 * Acquire a hold on the periph so that it doesn't go away before 220 * we are ready at the end of the function. 221 */ 222 periph = softc->periph; 223 cam_periph_acquire(periph); 224 cam_periph_lock(periph); 225 error = targdisable(softc); 226 if (error == CAM_REQ_CMP) { 227 dev->si_drv1 = 0; 228 if (softc->periph != NULL) { 229 cam_periph_invalidate(softc->periph); 230 softc->periph = NULL; 231 } 232 destroy_dev(dev); /* eats the open ref */ 233 devfs_clone_bitmap_put(&DEVFS_CLONE_BITMAP(targ), dev->si_uminor); 234 FREE(softc, M_TARG); 235 } else { 236 release_dev(dev); 237 } 238 cam_periph_unlock(periph); 239 cam_periph_release(periph); 240 241 return (error); 242 } 243 244 /* Enable/disable LUNs, set debugging level */ 245 static int 246 targioctl(struct dev_ioctl_args *ap) 247 { 248 struct targ_softc *softc; 249 cam_status status; 250 251 softc = (struct targ_softc *)ap->a_head.a_dev->si_drv1; 252 253 switch (ap->a_cmd) { 254 case TARGIOCENABLE: 255 { 256 struct ioc_enable_lun *new_lun; 257 struct cam_path *path; 258 struct cam_sim *sim; 259 260 new_lun = (struct ioc_enable_lun *)ap->a_data; 261 status = xpt_create_path_unlocked(&path, /*periph*/NULL, 262 new_lun->path_id, 263 new_lun->target_id, 264 new_lun->lun_id); 265 if (status != CAM_REQ_CMP) { 266 kprintf("Couldn't create path, status %#x\n", status); 267 break; 268 } 269 sim = xpt_path_sim(path); 270 CAM_SIM_LOCK(sim); 271 status = targenable(softc, path, new_lun->grp6_len, 272 new_lun->grp7_len); 273 xpt_free_path(path); 274 CAM_SIM_UNLOCK(sim); 275 break; 276 } 277 case TARGIOCDISABLE: 278 if (softc->periph == NULL) { 279 status = CAM_DEV_NOT_THERE; 280 break; 281 } 282 cam_periph_lock(softc->periph); 283 status = targdisable(softc); 284 cam_periph_unlock(softc->periph); 285 break; 286 case TARGIOCDEBUG: 287 { 288 #ifdef CAMDEBUG 289 struct ccb_debug cdbg; 290 291 /* If no periph available, disallow debugging changes */ 292 if ((softc->state & TARG_STATE_LUN_ENABLED) == 0) { 293 status = CAM_DEV_NOT_THERE; 294 break; 295 } 296 bzero(&cdbg, sizeof cdbg); 297 if (*((int *)ap->a_data) != 0) 298 cdbg.flags = CAM_DEBUG_PERIPH; 299 else 300 cdbg.flags = CAM_DEBUG_NONE; 301 cam_periph_lock(softc->periph); 302 xpt_setup_ccb(&cdbg.ccb_h, softc->path, /*priority*/0); 303 cdbg.ccb_h.func_code = XPT_DEBUG; 304 cdbg.ccb_h.cbfcnp = targdone; 305 306 xpt_action((union ccb *)&cdbg); 307 cam_periph_unlock(softc->periph); 308 status = cdbg.ccb_h.status & CAM_STATUS_MASK; 309 #else 310 status = CAM_FUNC_NOTAVAIL; 311 #endif 312 break; 313 } 314 default: 315 status = CAM_PROVIDE_FAIL; 316 break; 317 } 318 319 return (targcamstatus(status)); 320 } 321 322 static int 323 targkqfilter(struct dev_kqfilter_args *ap) 324 { 325 struct knote *kn = ap->a_kn; 326 struct targ_softc *softc; 327 328 softc = (struct targ_softc *)ap->a_head.a_dev->si_drv1; 329 330 ap->a_result = 0; 331 332 switch (kn->kn_filter) { 333 case EVFILT_READ: 334 kn->kn_hook = (caddr_t)softc; 335 kn->kn_fop = &targread_filtops; 336 break; 337 case EVFILT_WRITE: 338 kn->kn_hook = (caddr_t)softc; 339 kn->kn_fop = &targwrite_filtops; 340 default: 341 ap->a_result = EOPNOTSUPP; 342 return (0); 343 } 344 345 knote_insert(&softc->read_kq.ki_note, kn); 346 return (0); 347 } 348 349 static void 350 targfiltdetach(struct knote *kn) 351 { 352 struct targ_softc *softc; 353 354 softc = (struct targ_softc *)kn->kn_hook; 355 knote_remove(&softc->read_kq.ki_note, kn); 356 } 357 358 /* Notify the user's kqueue when the user queue or abort queue gets a CCB */ 359 static int 360 targreadfilt(struct knote *kn, long hint) 361 { 362 struct targ_softc *softc; 363 int retval; 364 365 softc = (struct targ_softc *)kn->kn_hook; 366 cam_periph_lock(softc->periph); 367 retval = !TAILQ_EMPTY(&softc->user_ccb_queue) || 368 !TAILQ_EMPTY(&softc->abort_queue); 369 cam_periph_unlock(softc->periph); 370 return (retval); 371 } 372 373 /* write() is always ok */ 374 static int 375 targwritefilt(struct knote *kn, long hint) 376 { 377 return (1); 378 } 379 380 /* Send the HBA the enable/disable message */ 381 static cam_status 382 targendislun(struct cam_path *path, int enable, int grp6_len, int grp7_len) 383 { 384 struct ccb_en_lun en_ccb; 385 cam_status status; 386 387 /* Tell the lun to begin answering selects */ 388 xpt_setup_ccb(&en_ccb.ccb_h, path, /*priority*/1); 389 en_ccb.ccb_h.func_code = XPT_EN_LUN; 390 /* Don't need support for any vendor specific commands */ 391 en_ccb.grp6_len = grp6_len; 392 en_ccb.grp7_len = grp7_len; 393 en_ccb.enable = enable ? 1 : 0; 394 xpt_action((union ccb *)&en_ccb); 395 status = en_ccb.ccb_h.status & CAM_STATUS_MASK; 396 if (status != CAM_REQ_CMP) { 397 xpt_print(path, "%sable lun CCB rejected, status %#x\n", 398 enable ? "en" : "dis", status); 399 } 400 return (status); 401 } 402 403 /* Enable target mode on a LUN, given its path */ 404 static cam_status 405 targenable(struct targ_softc *softc, struct cam_path *path, int grp6_len, 406 int grp7_len) 407 { 408 struct cam_periph *periph; 409 struct ccb_pathinq cpi; 410 cam_status status; 411 412 if ((softc->state & TARG_STATE_LUN_ENABLED) != 0) 413 return (CAM_LUN_ALRDY_ENA); 414 415 /* Make sure SIM supports target mode */ 416 xpt_setup_ccb(&cpi.ccb_h, path, /*priority*/1); 417 cpi.ccb_h.func_code = XPT_PATH_INQ; 418 xpt_action((union ccb *)&cpi); 419 status = cpi.ccb_h.status & CAM_STATUS_MASK; 420 if (status != CAM_REQ_CMP) { 421 kprintf("pathinq failed, status %#x\n", status); 422 goto enable_fail; 423 } 424 if ((cpi.target_sprt & PIT_PROCESSOR) == 0) { 425 kprintf("controller does not support target mode\n"); 426 status = CAM_FUNC_NOTAVAIL; 427 goto enable_fail; 428 } 429 430 /* Destroy any periph on our path if it is disabled */ 431 periph = cam_periph_find(path, "targ"); 432 if (periph != NULL) { 433 struct targ_softc *del_softc; 434 435 del_softc = (struct targ_softc *)periph->softc; 436 if ((del_softc->state & TARG_STATE_LUN_ENABLED) == 0) { 437 cam_periph_invalidate(del_softc->periph); 438 del_softc->periph = NULL; 439 } else { 440 kprintf("Requested path still in use by targ%d\n", 441 periph->unit_number); 442 status = CAM_LUN_ALRDY_ENA; 443 goto enable_fail; 444 } 445 } 446 447 /* Create a periph instance attached to this path */ 448 status = cam_periph_alloc(targctor, NULL, targdtor, targstart, 449 "targ", CAM_PERIPH_BIO, path, targasync, 0, softc); 450 if (status != CAM_REQ_CMP) { 451 kprintf("cam_periph_alloc failed, status %#x\n", status); 452 goto enable_fail; 453 } 454 455 /* Ensure that the periph now exists. */ 456 if (cam_periph_find(path, "targ") == NULL) { 457 panic("targenable: succeeded but no periph?"); 458 /* NOTREACHED */ 459 } 460 461 /* Send the enable lun message */ 462 status = targendislun(path, /*enable*/1, grp6_len, grp7_len); 463 if (status != CAM_REQ_CMP) { 464 kprintf("enable lun failed, status %#x\n", status); 465 goto enable_fail; 466 } 467 softc->state |= TARG_STATE_LUN_ENABLED; 468 469 enable_fail: 470 return (status); 471 } 472 473 /* Disable this softc's target instance if enabled */ 474 static cam_status 475 targdisable(struct targ_softc *softc) 476 { 477 cam_status status; 478 479 if ((softc->state & TARG_STATE_LUN_ENABLED) == 0) 480 return (CAM_REQ_CMP); 481 482 CAM_DEBUG(softc->path, CAM_DEBUG_PERIPH, ("targdisable\n")); 483 484 /* Abort any ccbs pending on the controller */ 485 crit_enter(); 486 abort_all_pending(softc); 487 crit_exit(); 488 489 /* Disable this lun */ 490 status = targendislun(softc->path, /*enable*/0, 491 /*grp6_len*/0, /*grp7_len*/0); 492 if (status == CAM_REQ_CMP) 493 softc->state &= ~TARG_STATE_LUN_ENABLED; 494 else 495 kprintf("Disable lun failed, status %#x\n", status); 496 497 return (status); 498 } 499 500 /* Initialize a periph (called from cam_periph_alloc) */ 501 static cam_status 502 targctor(struct cam_periph *periph, void *arg) 503 { 504 struct targ_softc *softc; 505 506 /* Store pointer to softc for periph-driven routines */ 507 softc = (struct targ_softc *)arg; 508 periph->softc = softc; 509 softc->periph = periph; 510 softc->path = periph->path; 511 return (CAM_REQ_CMP); 512 } 513 514 static void 515 targdtor(struct cam_periph *periph) 516 { 517 struct targ_softc *softc; 518 struct ccb_hdr *ccb_h; 519 struct targ_cmd_descr *descr; 520 521 softc = (struct targ_softc *)periph->softc; 522 523 /* 524 * targdisable() aborts CCBs back to the user and leaves them 525 * on user_ccb_queue and abort_queue in case the user is still 526 * interested in them. We free them now. 527 */ 528 while ((ccb_h = TAILQ_FIRST(&softc->user_ccb_queue)) != NULL) { 529 TAILQ_REMOVE(&softc->user_ccb_queue, ccb_h, periph_links.tqe); 530 targfreeccb(softc, (union ccb *)ccb_h); 531 } 532 while ((descr = TAILQ_FIRST(&softc->abort_queue)) != NULL) { 533 TAILQ_REMOVE(&softc->abort_queue, descr, tqe); 534 FREE(descr, M_TARG); 535 } 536 537 softc->periph = NULL; 538 softc->path = NULL; 539 periph->softc = NULL; 540 } 541 542 /* Receive CCBs from user mode proc and send them to the HBA */ 543 static int 544 targwrite(struct dev_write_args *ap) 545 { 546 struct uio *uio = ap->a_uio; 547 union ccb *user_ccb; 548 struct targ_softc *softc; 549 struct targ_cmd_descr *descr; 550 int write_len, error; 551 int func_code, priority; 552 553 softc = (struct targ_softc *)ap->a_head.a_dev->si_drv1; 554 write_len = error = 0; 555 CAM_DEBUG(softc->path, CAM_DEBUG_PERIPH, 556 ("write - uio_resid %zu\n", uio->uio_resid)); 557 while (uio->uio_resid >= sizeof(user_ccb) && error == 0) { 558 union ccb *ccb; 559 560 error = uiomove((caddr_t)&user_ccb, sizeof(user_ccb), uio); 561 if (error != 0) { 562 CAM_DEBUG(softc->path, CAM_DEBUG_PERIPH, 563 ("write - uiomove failed (%d)\n", error)); 564 break; 565 } 566 priority = fuword(&user_ccb->ccb_h.pinfo.priority); 567 if (priority == -1) { 568 error = EINVAL; 569 break; 570 } 571 func_code = fuword(&user_ccb->ccb_h.func_code); 572 switch (func_code) { 573 case XPT_ACCEPT_TARGET_IO: 574 case XPT_IMMED_NOTIFY: 575 cam_periph_lock(softc->periph); 576 ccb = targgetccb(softc, func_code, priority); 577 descr = (struct targ_cmd_descr *)ccb->ccb_h.targ_descr; 578 descr->user_ccb = user_ccb; 579 descr->func_code = func_code; 580 CAM_DEBUG(softc->path, CAM_DEBUG_PERIPH, 581 ("Sent ATIO/INOT (%p)\n", user_ccb)); 582 xpt_action(ccb); 583 TAILQ_INSERT_TAIL(&softc->pending_ccb_queue, 584 &ccb->ccb_h, 585 periph_links.tqe); 586 cam_periph_unlock(softc->periph); 587 break; 588 default: 589 cam_periph_lock(softc->periph); 590 if ((func_code & XPT_FC_QUEUED) != 0) { 591 CAM_DEBUG(softc->path, CAM_DEBUG_PERIPH, 592 ("Sending queued ccb %#x (%p)\n", 593 func_code, user_ccb)); 594 descr = targgetdescr(softc); 595 descr->user_ccb = user_ccb; 596 descr->priority = priority; 597 descr->func_code = func_code; 598 crit_enter(); 599 TAILQ_INSERT_TAIL(&softc->work_queue, 600 descr, tqe); 601 crit_exit(); 602 xpt_schedule(softc->periph, priority); 603 } else { 604 CAM_DEBUG(softc->path, CAM_DEBUG_PERIPH, 605 ("Sending inline ccb %#x (%p)\n", 606 func_code, user_ccb)); 607 ccb = targgetccb(softc, func_code, priority); 608 descr = (struct targ_cmd_descr *) 609 ccb->ccb_h.targ_descr; 610 descr->user_ccb = user_ccb; 611 descr->priority = priority; 612 descr->func_code = func_code; 613 if (targusermerge(softc, descr, ccb) != EFAULT) 614 targsendccb(softc, ccb, descr); 615 targreturnccb(softc, ccb); 616 } 617 cam_periph_unlock(softc->periph); 618 break; 619 } 620 write_len += sizeof(user_ccb); 621 } 622 623 /* 624 * If we've successfully taken in some amount of 625 * data, return success for that data first. If 626 * an error is persistent, it will be reported 627 * on the next write. 628 */ 629 if (error != 0 && write_len == 0) 630 return (error); 631 if (write_len == 0 && uio->uio_resid != 0) 632 return (ENOSPC); 633 return (0); 634 } 635 636 /* Process requests (descrs) via the periph-supplied CCBs */ 637 static void 638 targstart(struct cam_periph *periph, union ccb *start_ccb) 639 { 640 struct targ_softc *softc; 641 struct targ_cmd_descr *descr, *next_descr; 642 int error; 643 644 softc = (struct targ_softc *)periph->softc; 645 CAM_DEBUG(softc->path, CAM_DEBUG_PERIPH, ("targstart %p\n", start_ccb)); 646 647 crit_enter(); 648 descr = TAILQ_FIRST(&softc->work_queue); 649 if (descr == NULL) { 650 crit_exit(); 651 xpt_release_ccb(start_ccb); 652 } else { 653 TAILQ_REMOVE(&softc->work_queue, descr, tqe); 654 next_descr = TAILQ_FIRST(&softc->work_queue); 655 crit_exit(); 656 657 /* Initiate a transaction using the descr and supplied CCB */ 658 error = targusermerge(softc, descr, start_ccb); 659 if (error == 0) 660 error = targsendccb(softc, start_ccb, descr); 661 if (error != 0) { 662 xpt_print(periph->path, 663 "targsendccb failed, err %d\n", error); 664 xpt_release_ccb(start_ccb); 665 suword(&descr->user_ccb->ccb_h.status, 666 CAM_REQ_CMP_ERR); 667 crit_enter(); 668 TAILQ_INSERT_TAIL(&softc->abort_queue, descr, tqe); 669 crit_exit(); 670 notify_user(softc); 671 } 672 673 /* If we have more work to do, stay scheduled */ 674 if (next_descr != NULL) 675 xpt_schedule(periph, next_descr->priority); 676 } 677 } 678 679 static int 680 targusermerge(struct targ_softc *softc, struct targ_cmd_descr *descr, 681 union ccb *ccb) 682 { 683 struct ccb_hdr *u_ccbh, *k_ccbh; 684 size_t ccb_len; 685 int error; 686 687 u_ccbh = &descr->user_ccb->ccb_h; 688 k_ccbh = &ccb->ccb_h; 689 690 /* 691 * There are some fields in the CCB header that need to be 692 * preserved, the rest we get from the user ccb. (See xpt_merge_ccb) 693 */ 694 xpt_setup_ccb(k_ccbh, softc->path, descr->priority); 695 k_ccbh->retry_count = fuword(&u_ccbh->retry_count); 696 k_ccbh->func_code = descr->func_code; 697 k_ccbh->flags = fuword(&u_ccbh->flags); 698 k_ccbh->timeout = fuword(&u_ccbh->timeout); 699 ccb_len = targccblen(k_ccbh->func_code) - sizeof(struct ccb_hdr); 700 error = copyin(u_ccbh + 1, k_ccbh + 1, ccb_len); 701 if (error != 0) { 702 k_ccbh->status = CAM_REQ_CMP_ERR; 703 return (error); 704 } 705 706 /* Translate usermode abort_ccb pointer to its kernel counterpart */ 707 if (k_ccbh->func_code == XPT_ABORT) { 708 struct ccb_abort *cab; 709 struct ccb_hdr *ccb_h; 710 711 cab = (struct ccb_abort *)ccb; 712 crit_enter(); 713 TAILQ_FOREACH(ccb_h, &softc->pending_ccb_queue, 714 periph_links.tqe) { 715 struct targ_cmd_descr *ab_descr; 716 717 ab_descr = (struct targ_cmd_descr *)ccb_h->targ_descr; 718 if (ab_descr->user_ccb == cab->abort_ccb) { 719 CAM_DEBUG(softc->path, CAM_DEBUG_PERIPH, 720 ("Changing abort for %p to %p\n", 721 cab->abort_ccb, ccb_h)); 722 cab->abort_ccb = (union ccb *)ccb_h; 723 break; 724 } 725 } 726 crit_exit(); 727 /* CCB not found, set appropriate status */ 728 if (ccb_h == NULL) { 729 k_ccbh->status = CAM_PATH_INVALID; 730 error = ESRCH; 731 } 732 } 733 734 return (error); 735 } 736 737 /* Build and send a kernel CCB formed from descr->user_ccb */ 738 static int 739 targsendccb(struct targ_softc *softc, union ccb *ccb, 740 struct targ_cmd_descr *descr) 741 { 742 struct cam_periph_map_info *mapinfo; 743 struct ccb_hdr *ccb_h; 744 int error; 745 746 ccb_h = &ccb->ccb_h; 747 mapinfo = &descr->mapinfo; 748 mapinfo->num_bufs_used = 0; 749 750 /* 751 * There's no way for the user to have a completion 752 * function, so we put our own completion function in here. 753 * We also stash in a reference to our descriptor so targreturnccb() 754 * can find our mapping info. 755 */ 756 ccb_h->cbfcnp = targdone; 757 ccb_h->targ_descr = descr; 758 759 /* 760 * We only attempt to map the user memory into kernel space 761 * if they haven't passed in a physical memory pointer, 762 * and if there is actually an I/O operation to perform. 763 * Right now cam_periph_mapmem() only supports SCSI and device 764 * match CCBs. For the SCSI CCBs, we only pass the CCB in if 765 * there's actually data to map. cam_periph_mapmem() will do the 766 * right thing, even if there isn't data to map, but since CCBs 767 * without data are a reasonably common occurance (e.g. test unit 768 * ready), it will save a few cycles if we check for it here. 769 */ 770 if (((ccb_h->flags & CAM_DATA_PHYS) == 0) 771 && (((ccb_h->func_code == XPT_CONT_TARGET_IO) 772 && ((ccb_h->flags & CAM_DIR_MASK) != CAM_DIR_NONE)) 773 || (ccb_h->func_code == XPT_DEV_MATCH))) { 774 775 error = cam_periph_mapmem(ccb, mapinfo); 776 777 /* 778 * cam_periph_mapmem returned an error, we can't continue. 779 * Return the error to the user. 780 */ 781 if (error) { 782 ccb_h->status = CAM_REQ_CMP_ERR; 783 mapinfo->num_bufs_used = 0; 784 return (error); 785 } 786 } 787 788 /* 789 * Once queued on the pending CCB list, this CCB will be protected 790 * by our error recovery handler. 791 */ 792 CAM_DEBUG(softc->path, CAM_DEBUG_PERIPH, ("sendccb %p\n", ccb)); 793 if (XPT_FC_IS_QUEUED(ccb)) { 794 crit_enter(); 795 TAILQ_INSERT_TAIL(&softc->pending_ccb_queue, ccb_h, 796 periph_links.tqe); 797 crit_exit(); 798 } 799 xpt_action(ccb); 800 801 return (0); 802 } 803 804 /* Completion routine for CCBs (called in a critical section) */ 805 static void 806 targdone(struct cam_periph *periph, union ccb *done_ccb) 807 { 808 struct targ_softc *softc; 809 cam_status status; 810 811 CAM_DEBUG(periph->path, CAM_DEBUG_PERIPH, ("targdone %p\n", done_ccb)); 812 softc = (struct targ_softc *)periph->softc; 813 TAILQ_REMOVE(&softc->pending_ccb_queue, &done_ccb->ccb_h, 814 periph_links.tqe); 815 status = done_ccb->ccb_h.status & CAM_STATUS_MASK; 816 817 /* If we're no longer enabled, throw away CCB */ 818 if ((softc->state & TARG_STATE_LUN_ENABLED) == 0) { 819 targfreeccb(softc, done_ccb); 820 return; 821 } 822 /* abort_all_pending() waits for pending queue to be empty */ 823 if (TAILQ_EMPTY(&softc->pending_ccb_queue)) 824 wakeup(&softc->pending_ccb_queue); 825 826 switch (done_ccb->ccb_h.func_code) { 827 /* All FC_*_QUEUED CCBs go back to userland */ 828 case XPT_IMMED_NOTIFY: 829 case XPT_ACCEPT_TARGET_IO: 830 case XPT_CONT_TARGET_IO: 831 TAILQ_INSERT_TAIL(&softc->user_ccb_queue, &done_ccb->ccb_h, 832 periph_links.tqe); 833 notify_user(softc); 834 break; 835 default: 836 panic("targdone: impossible xpt opcode %#x", 837 done_ccb->ccb_h.func_code); 838 /* NOTREACHED */ 839 } 840 } 841 842 /* Return CCBs to the user from the user queue and abort queue */ 843 static int 844 targread(struct dev_read_args *ap) 845 { 846 struct uio *uio = ap->a_uio; 847 struct descr_queue *abort_queue; 848 struct targ_cmd_descr *user_descr; 849 struct targ_softc *softc; 850 struct ccb_queue *user_queue; 851 struct ccb_hdr *ccb_h; 852 union ccb *user_ccb; 853 int read_len, error; 854 855 error = 0; 856 read_len = 0; 857 softc = (struct targ_softc *)ap->a_head.a_dev->si_drv1; 858 user_queue = &softc->user_ccb_queue; 859 abort_queue = &softc->abort_queue; 860 CAM_DEBUG(softc->path, CAM_DEBUG_PERIPH, ("targread\n")); 861 862 /* If no data is available, wait or return immediately */ 863 cam_periph_lock(softc->periph); 864 ccb_h = TAILQ_FIRST(user_queue); 865 user_descr = TAILQ_FIRST(abort_queue); 866 while (ccb_h == NULL && user_descr == NULL) { 867 if ((ap->a_ioflag & IO_NDELAY) == 0) { 868 error = sim_lock_sleep(user_queue, PCATCH, "targrd", 0, 869 softc->periph->sim->lock); 870 ccb_h = TAILQ_FIRST(user_queue); 871 user_descr = TAILQ_FIRST(abort_queue); 872 if (error != 0) { 873 if (error == ERESTART) { 874 continue; 875 } else { 876 goto read_fail; 877 } 878 } 879 } else { 880 cam_periph_unlock(softc->periph); 881 return (EAGAIN); 882 } 883 } 884 885 /* Data is available so fill the user's buffer */ 886 while (ccb_h != NULL) { 887 struct targ_cmd_descr *descr; 888 889 if (uio->uio_resid < sizeof(user_ccb)) 890 break; 891 TAILQ_REMOVE(user_queue, ccb_h, periph_links.tqe); 892 descr = (struct targ_cmd_descr *)ccb_h->targ_descr; 893 user_ccb = descr->user_ccb; 894 CAM_DEBUG(softc->path, CAM_DEBUG_PERIPH, 895 ("targread ccb %p (%p)\n", ccb_h, user_ccb)); 896 error = targreturnccb(softc, (union ccb *)ccb_h); 897 if (error != 0) 898 goto read_fail; 899 cam_periph_unlock(softc->periph); 900 error = uiomove((caddr_t)&user_ccb, sizeof(user_ccb), uio); 901 cam_periph_lock(softc->periph); 902 if (error != 0) 903 goto read_fail; 904 read_len += sizeof(user_ccb); 905 906 ccb_h = TAILQ_FIRST(user_queue); 907 } 908 909 /* Flush out any aborted descriptors */ 910 while (user_descr != NULL) { 911 if (uio->uio_resid < sizeof(user_ccb)) 912 break; 913 TAILQ_REMOVE(abort_queue, user_descr, tqe); 914 user_ccb = user_descr->user_ccb; 915 CAM_DEBUG(softc->path, CAM_DEBUG_PERIPH, 916 ("targread aborted descr %p (%p)\n", 917 user_descr, user_ccb)); 918 suword(&user_ccb->ccb_h.status, CAM_REQ_ABORTED); 919 cam_periph_unlock(softc->periph); 920 error = uiomove((caddr_t)&user_ccb, sizeof(user_ccb), uio); 921 cam_periph_lock(softc->periph); 922 if (error != 0) 923 goto read_fail; 924 read_len += sizeof(user_ccb); 925 926 user_descr = TAILQ_FIRST(abort_queue); 927 } 928 929 /* 930 * If we've successfully read some amount of data, don't report an 931 * error. If the error is persistent, it will be reported on the 932 * next read(). 933 */ 934 if (read_len == 0 && uio->uio_resid != 0) 935 error = ENOSPC; 936 937 read_fail: 938 cam_periph_unlock(softc->periph); 939 return (error); 940 } 941 942 /* Copy completed ccb back to the user */ 943 static int 944 targreturnccb(struct targ_softc *softc, union ccb *ccb) 945 { 946 struct targ_cmd_descr *descr; 947 struct ccb_hdr *u_ccbh; 948 size_t ccb_len; 949 int error; 950 951 CAM_DEBUG(softc->path, CAM_DEBUG_PERIPH, ("targreturnccb %p\n", ccb)); 952 descr = (struct targ_cmd_descr *)ccb->ccb_h.targ_descr; 953 u_ccbh = &descr->user_ccb->ccb_h; 954 955 /* Copy out the central portion of the ccb_hdr */ 956 copyout(&ccb->ccb_h.retry_count, &u_ccbh->retry_count, 957 offsetof(struct ccb_hdr, periph_priv) - 958 offsetof(struct ccb_hdr, retry_count)); 959 960 /* Copy out the rest of the ccb (after the ccb_hdr) */ 961 ccb_len = targccblen(ccb->ccb_h.func_code) - sizeof(struct ccb_hdr); 962 if (descr->mapinfo.num_bufs_used != 0) 963 cam_periph_unmapmem(ccb, &descr->mapinfo); 964 error = copyout(&ccb->ccb_h + 1, u_ccbh + 1, ccb_len); 965 if (error != 0) { 966 xpt_print(softc->path, 967 "targreturnccb - CCB copyout failed (%d)\n", error); 968 } 969 /* Free CCB or send back to devq. */ 970 targfreeccb(softc, ccb); 971 972 return (error); 973 } 974 975 static union ccb * 976 targgetccb(struct targ_softc *softc, xpt_opcode type, int priority) 977 { 978 union ccb *ccb; 979 int ccb_len; 980 981 ccb_len = targccblen(type); 982 MALLOC(ccb, union ccb *, ccb_len, M_TARG, M_INTWAIT); 983 CAM_DEBUG(softc->path, CAM_DEBUG_PERIPH, ("getccb %p\n", ccb)); 984 985 xpt_setup_ccb(&ccb->ccb_h, softc->path, priority); 986 ccb->ccb_h.func_code = type; 987 ccb->ccb_h.cbfcnp = targdone; 988 ccb->ccb_h.targ_descr = targgetdescr(softc); 989 return (ccb); 990 } 991 992 static void 993 targfreeccb(struct targ_softc *softc, union ccb *ccb) 994 { 995 CAM_DEBUG_PRINT(CAM_DEBUG_PERIPH, ("targfreeccb descr %p and\n", 996 ccb->ccb_h.targ_descr)); 997 FREE(ccb->ccb_h.targ_descr, M_TARG); 998 999 switch (ccb->ccb_h.func_code) { 1000 case XPT_ACCEPT_TARGET_IO: 1001 case XPT_IMMED_NOTIFY: 1002 CAM_DEBUG_PRINT(CAM_DEBUG_PERIPH, ("freeing ccb %p\n", ccb)); 1003 FREE(ccb, M_TARG); 1004 break; 1005 default: 1006 /* Send back CCB if we got it from the periph */ 1007 if (XPT_FC_IS_QUEUED(ccb)) { 1008 CAM_DEBUG_PRINT(CAM_DEBUG_PERIPH, 1009 ("returning queued ccb %p\n", ccb)); 1010 xpt_release_ccb(ccb); 1011 } else { 1012 CAM_DEBUG_PRINT(CAM_DEBUG_PERIPH, 1013 ("freeing ccb %p\n", ccb)); 1014 FREE(ccb, M_TARG); 1015 } 1016 break; 1017 } 1018 } 1019 1020 static struct targ_cmd_descr * 1021 targgetdescr(struct targ_softc *softc) 1022 { 1023 struct targ_cmd_descr *descr; 1024 1025 MALLOC(descr, struct targ_cmd_descr *, sizeof(*descr), 1026 M_TARG, M_INTWAIT); 1027 descr->mapinfo.num_bufs_used = 0; 1028 return (descr); 1029 } 1030 1031 static int 1032 targclone(struct dev_clone_args *ap) 1033 { 1034 int unit; 1035 1036 unit = devfs_clone_bitmap_get(&DEVFS_CLONE_BITMAP(targ), 0); 1037 ap->a_dev = make_only_dev(&targ_ops, unit, UID_ROOT, GID_WHEEL, 1038 0600, "targ%d", unit); 1039 return 0; 1040 } 1041 1042 static void 1043 targinit(void) 1044 { 1045 make_autoclone_dev(&targ_ops, &DEVFS_CLONE_BITMAP(targ), 1046 targclone, UID_ROOT, GID_WHEEL, 0600, "targ"); 1047 /* XXX: need uninit or so? */ 1048 } 1049 1050 static void 1051 targasync(void *callback_arg, u_int32_t code, struct cam_path *path, void *arg) 1052 { 1053 /* All events are handled in usermode by INOTs */ 1054 panic("targasync() called, should be an INOT instead"); 1055 } 1056 1057 /* Cancel all pending requests and CCBs awaiting work. */ 1058 static void 1059 abort_all_pending(struct targ_softc *softc) 1060 { 1061 struct targ_cmd_descr *descr; 1062 struct ccb_abort cab; 1063 struct ccb_hdr *ccb_h; 1064 struct cam_sim *sim; 1065 1066 CAM_DEBUG(softc->path, CAM_DEBUG_PERIPH, ("abort_all_pending\n")); 1067 1068 /* First abort the descriptors awaiting resources */ 1069 while ((descr = TAILQ_FIRST(&softc->work_queue)) != NULL) { 1070 CAM_DEBUG(softc->path, CAM_DEBUG_PERIPH, 1071 ("Aborting descr from workq %p\n", descr)); 1072 TAILQ_REMOVE(&softc->work_queue, descr, tqe); 1073 TAILQ_INSERT_TAIL(&softc->abort_queue, descr, tqe); 1074 } 1075 1076 /* 1077 * Then abort all pending CCBs. 1078 * targdone() will return the aborted CCB via user_ccb_queue 1079 */ 1080 xpt_setup_ccb(&cab.ccb_h, softc->path, /*priority*/0); 1081 cab.ccb_h.func_code = XPT_ABORT; 1082 cab.ccb_h.status = CAM_REQ_CMP_ERR; 1083 TAILQ_FOREACH(ccb_h, &softc->pending_ccb_queue, periph_links.tqe) { 1084 CAM_DEBUG(softc->path, CAM_DEBUG_PERIPH, 1085 ("Aborting pending CCB %p\n", ccb_h)); 1086 cab.abort_ccb = (union ccb *)ccb_h; 1087 xpt_action((union ccb *)&cab); 1088 if (cab.ccb_h.status != CAM_REQ_CMP) { 1089 xpt_print(cab.ccb_h.path, 1090 "Unable to abort CCB, status %#x\n", 1091 cab.ccb_h.status); 1092 } 1093 } 1094 1095 /* If we aborted at least one pending CCB ok, wait for it. */ 1096 if (cab.ccb_h.status == CAM_REQ_CMP) { 1097 sim = xpt_path_sim(softc->path); 1098 sim_lock_sleep(&softc->pending_ccb_queue, PCATCH, "tgabrt", 0, 1099 sim->lock); 1100 } 1101 1102 /* If we aborted anything from the work queue, wakeup user. */ 1103 if (!TAILQ_EMPTY(&softc->user_ccb_queue) 1104 || !TAILQ_EMPTY(&softc->abort_queue)) 1105 notify_user(softc); 1106 } 1107 1108 /* Notify the user that data is ready */ 1109 static void 1110 notify_user(struct targ_softc *softc) 1111 { 1112 /* 1113 * Notify users sleeping via poll(), kqueue(), and 1114 * blocking read(). 1115 */ 1116 KNOTE(&softc->read_kq.ki_note, 0); 1117 wakeup(&softc->user_ccb_queue); 1118 } 1119 1120 /* Convert CAM status to errno values */ 1121 static int 1122 targcamstatus(cam_status status) 1123 { 1124 switch (status & CAM_STATUS_MASK) { 1125 case CAM_REQ_CMP: /* CCB request completed without error */ 1126 return (0); 1127 case CAM_REQ_INPROG: /* CCB request is in progress */ 1128 return (EINPROGRESS); 1129 case CAM_REQ_CMP_ERR: /* CCB request completed with an error */ 1130 return (EIO); 1131 case CAM_PROVIDE_FAIL: /* Unable to provide requested capability */ 1132 return (ENOTTY); 1133 case CAM_FUNC_NOTAVAIL: /* The requested function is not available */ 1134 return (ENOTSUP); 1135 case CAM_LUN_ALRDY_ENA: /* LUN is already enabled for target mode */ 1136 return (EADDRINUSE); 1137 case CAM_PATH_INVALID: /* Supplied Path ID is invalid */ 1138 case CAM_DEV_NOT_THERE: /* SCSI Device Not Installed/there */ 1139 return (ENOENT); 1140 case CAM_REQ_ABORTED: /* CCB request aborted by the host */ 1141 return (ECANCELED); 1142 case CAM_CMD_TIMEOUT: /* Command timeout */ 1143 return (ETIMEDOUT); 1144 case CAM_REQUEUE_REQ: /* Requeue to preserve transaction ordering */ 1145 return (EAGAIN); 1146 case CAM_REQ_INVALID: /* CCB request was invalid */ 1147 return (EINVAL); 1148 case CAM_RESRC_UNAVAIL: /* Resource Unavailable */ 1149 return (ENOMEM); 1150 case CAM_BUSY: /* CAM subsytem is busy */ 1151 case CAM_UA_ABORT: /* Unable to abort CCB request */ 1152 return (EBUSY); 1153 default: 1154 return (ENXIO); 1155 } 1156 } 1157 1158 static size_t 1159 targccblen(xpt_opcode func_code) 1160 { 1161 int len; 1162 1163 /* Codes we expect to see as a target */ 1164 switch (func_code) { 1165 case XPT_CONT_TARGET_IO: 1166 case XPT_SCSI_IO: 1167 len = sizeof(struct ccb_scsiio); 1168 break; 1169 case XPT_ACCEPT_TARGET_IO: 1170 len = sizeof(struct ccb_accept_tio); 1171 break; 1172 case XPT_IMMED_NOTIFY: 1173 len = sizeof(struct ccb_immed_notify); 1174 break; 1175 case XPT_REL_SIMQ: 1176 len = sizeof(struct ccb_relsim); 1177 break; 1178 case XPT_PATH_INQ: 1179 len = sizeof(struct ccb_pathinq); 1180 break; 1181 case XPT_DEBUG: 1182 len = sizeof(struct ccb_debug); 1183 break; 1184 case XPT_ABORT: 1185 len = sizeof(struct ccb_abort); 1186 break; 1187 case XPT_EN_LUN: 1188 len = sizeof(struct ccb_en_lun); 1189 break; 1190 default: 1191 len = sizeof(union ccb); 1192 break; 1193 } 1194 1195 return (len); 1196 } 1197