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$ 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/malloc.h> 37 #include <sys/poll.h> 38 #include <sys/vnode.h> 39 #include <sys/lock.h> 40 #include <sys/mutex.h> 41 #include <sys/devicestat.h> 42 43 #include <cam/cam.h> 44 #include <cam/cam_ccb.h> 45 #include <cam/cam_periph.h> 46 #include <cam/cam_xpt_periph.h> 47 #include <cam/scsi/scsi_targetio.h> 48 49 /* Transaction information attached to each CCB sent by the user */ 50 struct targ_cmd_descr { 51 struct cam_periph_map_info mapinfo; 52 TAILQ_ENTRY(targ_cmd_descr) tqe; 53 union ccb *user_ccb; 54 int priority; 55 int func_code; 56 }; 57 58 /* Offset into the private CCB area for storing our descriptor */ 59 #define targ_descr periph_priv.entries[1].ptr 60 61 TAILQ_HEAD(descr_queue, targ_cmd_descr); 62 63 typedef enum { 64 TARG_STATE_RESV = 0x00, /* Invalid state */ 65 TARG_STATE_OPENED = 0x01, /* Device opened, softc initialized */ 66 TARG_STATE_LUN_ENABLED = 0x02 /* Device enabled for a path */ 67 } targ_state; 68 69 /* Per-instance device software context */ 70 struct targ_softc { 71 /* CCBs (CTIOs, ATIOs, INOTs) pending on the controller */ 72 struct ccb_queue pending_ccb_queue; 73 74 /* Command descriptors awaiting CTIO resources from the XPT */ 75 struct descr_queue work_queue; 76 77 /* Command descriptors that have been aborted back to the user. */ 78 struct descr_queue abort_queue; 79 80 /* 81 * Queue of CCBs that have been copied out to userland, but our 82 * userland daemon has not yet seen. 83 */ 84 struct ccb_queue user_ccb_queue; 85 86 struct cam_periph *periph; 87 struct cam_path *path; 88 targ_state state; 89 struct selinfo read_select; 90 struct devstat device_stats; 91 struct mtx mtx; 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_poll_t targpoll; 100 static d_kqfilter_t targkqfilter; 101 static void targreadfiltdetach(struct knote *kn); 102 static int targreadfilt(struct knote *kn, long hint); 103 static struct filterops targread_filtops = 104 { 1, NULL, targreadfiltdetach, targreadfilt }; 105 106 #define TARG_CDEV_MAJOR 65 107 static struct cdevsw targ_cdevsw = { 108 /* open */ targopen, 109 /* close */ targclose, 110 /* read */ targread, 111 /* write */ targwrite, 112 /* ioctl */ targioctl, 113 /* poll */ targpoll, 114 /* mmap */ nommap, 115 /* strategy */ nostrategy, 116 /* name */ "targ", 117 /* maj */ TARG_CDEV_MAJOR, 118 /* dump */ nodump, 119 /* psize */ nopsize, 120 /* flags */ D_KQFILTER, 121 /* kqfilter */ targkqfilter 122 }; 123 124 static cam_status targendislun(struct cam_path *path, int enable, 125 int grp6_len, int grp7_len); 126 static cam_status targenable(struct targ_softc *softc, 127 struct cam_path *path, 128 int grp6_len, int grp7_len); 129 static cam_status targdisable(struct targ_softc *softc); 130 static periph_ctor_t targctor; 131 static periph_dtor_t targdtor; 132 static periph_start_t targstart; 133 static int targusermerge(struct targ_softc *softc, 134 struct targ_cmd_descr *descr, 135 union ccb *ccb); 136 static int targsendccb(struct targ_softc *softc, union ccb *ccb, 137 struct targ_cmd_descr *descr); 138 static void targdone(struct cam_periph *periph, 139 union ccb *done_ccb); 140 static int targreturnccb(struct targ_softc *softc, 141 union ccb *ccb); 142 static union ccb * targgetccb(struct targ_softc *softc, xpt_opcode type, 143 int priority); 144 static void targfreeccb(struct targ_softc *softc, union ccb *ccb); 145 static struct targ_cmd_descr * 146 targgetdescr(struct targ_softc *softc); 147 static periph_init_t targinit; 148 static void targclone(void *arg, char *name, int namelen, 149 dev_t *dev); 150 static void targasync(void *callback_arg, u_int32_t code, 151 struct cam_path *path, void *arg); 152 static void abort_all_pending(struct targ_softc *softc); 153 static void notify_user(struct targ_softc *softc); 154 static int targcamstatus(cam_status status); 155 static size_t targccblen(xpt_opcode func_code); 156 157 static struct periph_driver targdriver = 158 { 159 targinit, "targ", 160 TAILQ_HEAD_INITIALIZER(targdriver.units), /* generation */ 0 161 }; 162 PERIPHDRIVER_DECLARE(targ, targdriver); 163 164 static struct mtx targ_mtx; 165 #define TARG_LOCK(softc) mtx_lock(&(softc)->mtx) 166 #define TARG_UNLOCK(softc) mtx_unlock(&(softc)->mtx) 167 168 static MALLOC_DEFINE(M_TARG, "TARG", "TARG data"); 169 170 /* Create softc and initialize it. Only one proc can open each targ device. */ 171 static int 172 targopen(dev_t dev, int flags, int fmt, struct thread *td) 173 { 174 struct targ_softc *softc; 175 176 mtx_lock(&targ_mtx); 177 if (dev->si_drv1 != 0) { 178 mtx_unlock(&targ_mtx); 179 return (EBUSY); 180 } 181 182 /* Mark device busy before any potentially blocking operations */ 183 dev->si_drv1 = (void *)~0; 184 mtx_unlock(&targ_mtx); 185 186 /* Create the targ device, allocate its softc, initialize it */ 187 if ((dev->si_flags & SI_NAMED) == 0) { 188 make_dev(&targ_cdevsw, minor(dev), UID_ROOT, GID_WHEEL, 0600, 189 "targ%d", dev2unit(dev)); 190 } 191 MALLOC(softc, struct targ_softc *, sizeof(*softc), M_TARG, 192 M_ZERO); 193 dev->si_drv1 = softc; 194 softc->state = TARG_STATE_OPENED; 195 softc->periph = NULL; 196 softc->path = NULL; 197 mtx_init(&softc->mtx, devtoname(dev), "targ cdev", MTX_DEF); 198 199 TAILQ_INIT(&softc->pending_ccb_queue); 200 TAILQ_INIT(&softc->work_queue); 201 TAILQ_INIT(&softc->abort_queue); 202 TAILQ_INIT(&softc->user_ccb_queue); 203 204 return (0); 205 } 206 207 /* Disable LUN if enabled and teardown softc */ 208 static int 209 targclose(dev_t dev, int flag, int fmt, struct thread *td) 210 { 211 struct targ_softc *softc; 212 int error; 213 214 softc = (struct targ_softc *)dev->si_drv1; 215 TARG_LOCK(softc); 216 error = targdisable(softc); 217 if (error == CAM_REQ_CMP) { 218 dev->si_drv1 = 0; 219 mtx_lock(&targ_mtx); 220 if (softc->periph != NULL) { 221 cam_periph_invalidate(softc->periph); 222 softc->periph = NULL; 223 } 224 mtx_unlock(&targ_mtx); 225 TARG_UNLOCK(softc); 226 mtx_destroy(&softc->mtx); 227 destroy_dev(dev); 228 FREE(softc, M_TARG); 229 } else { 230 TARG_UNLOCK(softc); 231 } 232 return (error); 233 } 234 235 /* Enable/disable LUNs, set debugging level */ 236 static int 237 targioctl(dev_t dev, u_long cmd, caddr_t addr, int flag, struct thread *td) 238 { 239 struct targ_softc *softc; 240 cam_status status; 241 242 softc = (struct targ_softc *)dev->si_drv1; 243 244 switch (cmd) { 245 case TARGIOCENABLE: 246 { 247 struct ioc_enable_lun *new_lun; 248 struct cam_path *path; 249 250 new_lun = (struct ioc_enable_lun *)addr; 251 status = xpt_create_path(&path, /*periph*/NULL, 252 new_lun->path_id, 253 new_lun->target_id, 254 new_lun->lun_id); 255 if (status != CAM_REQ_CMP) { 256 printf("Couldn't create path, status %#x\n", status); 257 break; 258 } 259 TARG_LOCK(softc); 260 status = targenable(softc, path, new_lun->grp6_len, 261 new_lun->grp7_len); 262 TARG_UNLOCK(softc); 263 xpt_free_path(path); 264 break; 265 } 266 case TARGIOCDISABLE: 267 TARG_LOCK(softc); 268 status = targdisable(softc); 269 TARG_UNLOCK(softc); 270 break; 271 case TARGIOCDEBUG: 272 { 273 #ifdef CAMDEBUG 274 struct ccb_debug cdbg; 275 276 bzero(&cdbg, sizeof cdbg); 277 if (*((int *)addr) != 0) 278 cdbg.flags = CAM_DEBUG_PERIPH; 279 else 280 cdbg.flags = CAM_DEBUG_NONE; 281 xpt_setup_ccb(&cdbg.ccb_h, softc->path, /*priority*/0); 282 cdbg.ccb_h.func_code = XPT_DEBUG; 283 cdbg.ccb_h.cbfcnp = targdone; 284 285 /* If no periph available, disallow debugging changes */ 286 TARG_LOCK(softc); 287 if ((softc->state & TARG_STATE_LUN_ENABLED) == 0) { 288 status = CAM_DEV_NOT_THERE; 289 TARG_UNLOCK(softc); 290 break; 291 } 292 xpt_action((union ccb *)&cdbg); 293 TARG_UNLOCK(softc); 294 status = cdbg.ccb_h.status & CAM_STATUS_MASK; 295 #else 296 status = CAM_FUNC_NOTAVAIL; 297 #endif 298 break; 299 } 300 default: 301 status = CAM_PROVIDE_FAIL; 302 break; 303 } 304 305 return (targcamstatus(status)); 306 } 307 308 /* Writes are always ready, reads wait for user_ccb_queue or abort_queue */ 309 static int 310 targpoll(dev_t dev, int poll_events, struct thread *td) 311 { 312 struct targ_softc *softc; 313 int revents; 314 315 softc = (struct targ_softc *)dev->si_drv1; 316 317 /* Poll for write() is always ok. */ 318 revents = poll_events & (POLLOUT | POLLWRNORM); 319 if ((poll_events & (POLLIN | POLLRDNORM)) != 0) { 320 /* Poll for read() depends on user and abort queues. */ 321 TARG_LOCK(softc); 322 if (!TAILQ_EMPTY(&softc->user_ccb_queue) || 323 !TAILQ_EMPTY(&softc->abort_queue)) { 324 revents |= poll_events & (POLLIN | POLLRDNORM); 325 } 326 /* Only sleep if the user didn't poll for write. */ 327 if (revents == 0) 328 selrecord(td, &softc->read_select); 329 TARG_UNLOCK(softc); 330 } 331 332 return (revents); 333 } 334 335 static int 336 targkqfilter(dev_t dev, struct knote *kn) 337 { 338 struct targ_softc *softc; 339 340 softc = (struct targ_softc *)dev->si_drv1; 341 kn->kn_hook = (caddr_t)softc; 342 kn->kn_fop = &targread_filtops; 343 TARG_LOCK(softc); 344 SLIST_INSERT_HEAD(&softc->read_select.si_note, kn, kn_selnext); 345 TARG_UNLOCK(softc); 346 return (0); 347 } 348 349 static void 350 targreadfiltdetach(struct knote *kn) 351 { 352 struct targ_softc *softc; 353 354 softc = (struct targ_softc *)kn->kn_hook; 355 TARG_LOCK(softc); 356 SLIST_REMOVE(&softc->read_select.si_note, kn, knote, kn_selnext); 357 TARG_UNLOCK(softc); 358 } 359 360 /* Notify the user's kqueue when the user queue or abort queue gets a CCB */ 361 static int 362 targreadfilt(struct knote *kn, long hint) 363 { 364 struct targ_softc *softc; 365 int retval; 366 367 softc = (struct targ_softc *)kn->kn_hook; 368 TARG_LOCK(softc); 369 retval = !TAILQ_EMPTY(&softc->user_ccb_queue) || 370 !TAILQ_EMPTY(&softc->abort_queue); 371 TARG_UNLOCK(softc); 372 return (retval); 373 } 374 375 /* Send the HBA the enable/disable message */ 376 static cam_status 377 targendislun(struct cam_path *path, int enable, int grp6_len, int grp7_len) 378 { 379 struct ccb_en_lun en_ccb; 380 cam_status status; 381 382 /* Tell the lun to begin answering selects */ 383 xpt_setup_ccb(&en_ccb.ccb_h, path, /*priority*/1); 384 en_ccb.ccb_h.func_code = XPT_EN_LUN; 385 /* Don't need support for any vendor specific commands */ 386 en_ccb.grp6_len = grp6_len; 387 en_ccb.grp7_len = grp7_len; 388 en_ccb.enable = enable ? 1 : 0; 389 xpt_action((union ccb *)&en_ccb); 390 status = en_ccb.ccb_h.status & CAM_STATUS_MASK; 391 if (status != CAM_REQ_CMP) { 392 xpt_print_path(path); 393 printf("%sable lun CCB rejected, status %#x\n", 394 enable ? "en" : "dis", status); 395 } 396 return (status); 397 } 398 399 /* Enable target mode on a LUN, given its path */ 400 static cam_status 401 targenable(struct targ_softc *softc, struct cam_path *path, int grp6_len, 402 int grp7_len) 403 { 404 struct cam_periph *periph; 405 struct ccb_pathinq cpi; 406 cam_status status; 407 408 if ((softc->state & TARG_STATE_LUN_ENABLED) != 0) 409 return (CAM_LUN_ALRDY_ENA); 410 411 /* Make sure SIM supports target mode */ 412 xpt_setup_ccb(&cpi.ccb_h, path, /*priority*/1); 413 cpi.ccb_h.func_code = XPT_PATH_INQ; 414 xpt_action((union ccb *)&cpi); 415 status = cpi.ccb_h.status & CAM_STATUS_MASK; 416 if (status != CAM_REQ_CMP) { 417 printf("pathinq failed, status %#x\n", status); 418 goto enable_fail; 419 } 420 if ((cpi.target_sprt & PIT_PROCESSOR) == 0) { 421 printf("controller does not support target mode\n"); 422 status = CAM_FUNC_NOTAVAIL; 423 goto enable_fail; 424 } 425 426 /* Destroy any periph on our path if it is disabled */ 427 mtx_lock(&targ_mtx); 428 periph = cam_periph_find(path, "targ"); 429 if (periph != NULL) { 430 struct targ_softc *del_softc; 431 432 del_softc = (struct targ_softc *)periph->softc; 433 if ((del_softc->state & TARG_STATE_LUN_ENABLED) == 0) { 434 cam_periph_invalidate(del_softc->periph); 435 del_softc->periph = NULL; 436 } else { 437 printf("Requested path still in use by targ%d\n", 438 periph->unit_number); 439 mtx_unlock(&targ_mtx); 440 status = CAM_LUN_ALRDY_ENA; 441 goto enable_fail; 442 } 443 } 444 445 /* Create a periph instance attached to this path */ 446 status = cam_periph_alloc(targctor, NULL, targdtor, targstart, 447 "targ", CAM_PERIPH_BIO, path, targasync, 0, softc); 448 mtx_unlock(&targ_mtx); 449 if (status != CAM_REQ_CMP) { 450 printf("cam_periph_alloc failed, status %#x\n", status); 451 goto enable_fail; 452 } 453 454 /* Ensure that the periph now exists. */ 455 if (cam_periph_find(path, "targ") == NULL) { 456 panic("targenable: succeeded but no periph?"); 457 /* NOTREACHED */ 458 } 459 460 /* Send the enable lun message */ 461 status = targendislun(path, /*enable*/1, grp6_len, grp7_len); 462 if (status != CAM_REQ_CMP) { 463 printf("enable lun failed, status %#x\n", status); 464 goto enable_fail; 465 } 466 softc->state |= TARG_STATE_LUN_ENABLED; 467 468 enable_fail: 469 return (status); 470 } 471 472 /* Disable this softc's target instance if enabled */ 473 static cam_status 474 targdisable(struct targ_softc *softc) 475 { 476 cam_status status; 477 478 if ((softc->state & TARG_STATE_LUN_ENABLED) == 0) 479 return (CAM_REQ_CMP); 480 481 CAM_DEBUG(softc->path, CAM_DEBUG_PERIPH, ("targdisable\n")); 482 483 /* Abort any ccbs pending on the controller */ 484 abort_all_pending(softc); 485 486 /* Disable this lun */ 487 status = targendislun(softc->path, /*enable*/0, 488 /*grp6_len*/0, /*grp7_len*/0); 489 if (status == CAM_REQ_CMP) 490 softc->state &= ~TARG_STATE_LUN_ENABLED; 491 else 492 printf("Disable lun failed, status %#x\n", status); 493 494 return (status); 495 } 496 497 /* Initialize a periph (called from cam_periph_alloc) */ 498 static cam_status 499 targctor(struct cam_periph *periph, void *arg) 500 { 501 struct targ_softc *softc; 502 503 /* Store pointer to softc for periph-driven routines */ 504 softc = (struct targ_softc *)arg; 505 periph->softc = softc; 506 softc->periph = periph; 507 softc->path = periph->path; 508 return (CAM_REQ_CMP); 509 } 510 511 static void 512 targdtor(struct cam_periph *periph) 513 { 514 struct targ_softc *softc; 515 struct ccb_hdr *ccb_h; 516 struct targ_cmd_descr *descr; 517 518 softc = (struct targ_softc *)periph->softc; 519 520 /* 521 * targdisable() aborts CCBs back to the user and leaves them 522 * on user_ccb_queue and abort_queue in case the user is still 523 * interested in them. We free them now. 524 */ 525 while ((ccb_h = TAILQ_FIRST(&softc->user_ccb_queue)) != NULL) { 526 TAILQ_REMOVE(&softc->user_ccb_queue, ccb_h, periph_links.tqe); 527 targfreeccb(softc, (union ccb *)ccb_h); 528 } 529 while ((descr = TAILQ_FIRST(&softc->abort_queue)) != NULL) { 530 TAILQ_REMOVE(&softc->abort_queue, descr, tqe); 531 FREE(descr, M_TARG); 532 } 533 534 softc->periph = NULL; 535 softc->path = NULL; 536 periph->softc = NULL; 537 } 538 539 /* Receive CCBs from user mode proc and send them to the HBA */ 540 static int 541 targwrite(dev_t dev, struct uio *uio, int ioflag) 542 { 543 union ccb *user_ccb; 544 struct targ_softc *softc; 545 struct targ_cmd_descr *descr; 546 int write_len, error; 547 int func_code, priority; 548 549 softc = (struct targ_softc *)dev->si_drv1; 550 write_len = error = 0; 551 CAM_DEBUG(softc->path, CAM_DEBUG_PERIPH, 552 ("write - uio_resid %d\n", uio->uio_resid)); 553 while (uio->uio_resid >= sizeof(user_ccb) && error == 0) { 554 union ccb *ccb; 555 int error; 556 557 error = uiomove((caddr_t)&user_ccb, sizeof(user_ccb), uio); 558 if (error != 0) { 559 CAM_DEBUG(softc->path, CAM_DEBUG_PERIPH, 560 ("write - uiomove failed (%d)\n", error)); 561 break; 562 } 563 priority = fuword(&user_ccb->ccb_h.pinfo.priority); 564 if (priority == -1) { 565 error = EINVAL; 566 break; 567 } 568 func_code = fuword(&user_ccb->ccb_h.func_code); 569 switch (func_code) { 570 case XPT_ACCEPT_TARGET_IO: 571 case XPT_IMMED_NOTIFY: 572 ccb = targgetccb(softc, func_code, priority); 573 descr = (struct targ_cmd_descr *)ccb->ccb_h.targ_descr; 574 descr->user_ccb = user_ccb; 575 descr->func_code = func_code; 576 CAM_DEBUG(softc->path, CAM_DEBUG_PERIPH, 577 ("Sent ATIO/INOT (%p)\n", user_ccb)); 578 xpt_action(ccb); 579 TARG_LOCK(softc); 580 TAILQ_INSERT_TAIL(&softc->pending_ccb_queue, 581 &ccb->ccb_h, 582 periph_links.tqe); 583 TARG_UNLOCK(softc); 584 break; 585 default: 586 if ((func_code & XPT_FC_QUEUED) != 0) { 587 CAM_DEBUG(softc->path, CAM_DEBUG_PERIPH, 588 ("Sending queued ccb %#x (%p)\n", 589 func_code, user_ccb)); 590 descr = targgetdescr(softc); 591 descr->user_ccb = user_ccb; 592 descr->priority = priority; 593 descr->func_code = func_code; 594 TARG_LOCK(softc); 595 TAILQ_INSERT_TAIL(&softc->work_queue, 596 descr, tqe); 597 TARG_UNLOCK(softc); 598 xpt_schedule(softc->periph, priority); 599 } else { 600 CAM_DEBUG(softc->path, CAM_DEBUG_PERIPH, 601 ("Sending inline ccb %#x (%p)\n", 602 func_code, user_ccb)); 603 ccb = targgetccb(softc, func_code, priority); 604 descr = (struct targ_cmd_descr *) 605 ccb->ccb_h.targ_descr; 606 descr->user_ccb = user_ccb; 607 descr->priority = priority; 608 descr->func_code = func_code; 609 if (targusermerge(softc, descr, ccb) != EFAULT) 610 targsendccb(softc, ccb, descr); 611 targreturnccb(softc, ccb); 612 } 613 break; 614 } 615 write_len += sizeof(user_ccb); 616 } 617 618 /* 619 * If we've successfully taken in some amount of 620 * data, return success for that data first. If 621 * an error is persistent, it will be reported 622 * on the next write. 623 */ 624 if (error != 0 && write_len == 0) 625 return (error); 626 if (write_len == 0 && uio->uio_resid != 0) 627 return (ENOSPC); 628 return (0); 629 } 630 631 /* Process requests (descrs) via the periph-supplied CCBs */ 632 static void 633 targstart(struct cam_periph *periph, union ccb *start_ccb) 634 { 635 struct targ_softc *softc; 636 struct targ_cmd_descr *descr, *next_descr; 637 int error; 638 639 softc = (struct targ_softc *)periph->softc; 640 CAM_DEBUG(softc->path, CAM_DEBUG_PERIPH, ("targstart %p\n", start_ccb)); 641 642 TARG_LOCK(softc); 643 descr = TAILQ_FIRST(&softc->work_queue); 644 if (descr == NULL) { 645 TARG_UNLOCK(softc); 646 xpt_release_ccb(start_ccb); 647 } else { 648 TAILQ_REMOVE(&softc->work_queue, descr, tqe); 649 next_descr = TAILQ_FIRST(&softc->work_queue); 650 TARG_UNLOCK(softc); 651 652 /* Initiate a transaction using the descr and supplied CCB */ 653 error = targusermerge(softc, descr, start_ccb); 654 if (error == 0) 655 error = targsendccb(softc, start_ccb, descr); 656 if (error != 0) { 657 xpt_print_path(periph->path); 658 printf("targsendccb failed, err %d\n", error); 659 xpt_release_ccb(start_ccb); 660 suword(&descr->user_ccb->ccb_h.status, 661 CAM_REQ_CMP_ERR); 662 TARG_LOCK(softc); 663 TAILQ_INSERT_TAIL(&softc->abort_queue, descr, tqe); 664 TARG_UNLOCK(softc); 665 notify_user(softc); 666 } 667 668 /* If we have more work to do, stay scheduled */ 669 if (next_descr != NULL) 670 xpt_schedule(periph, next_descr->priority); 671 } 672 } 673 674 static int 675 targusermerge(struct targ_softc *softc, struct targ_cmd_descr *descr, 676 union ccb *ccb) 677 { 678 struct ccb_hdr *u_ccbh, *k_ccbh; 679 size_t ccb_len; 680 int error; 681 682 u_ccbh = &descr->user_ccb->ccb_h; 683 k_ccbh = &ccb->ccb_h; 684 685 /* 686 * There are some fields in the CCB header that need to be 687 * preserved, the rest we get from the user ccb. (See xpt_merge_ccb) 688 */ 689 xpt_setup_ccb(k_ccbh, softc->path, descr->priority); 690 k_ccbh->retry_count = fuword(&u_ccbh->retry_count); 691 k_ccbh->func_code = descr->func_code; 692 k_ccbh->flags = fuword(&u_ccbh->flags); 693 k_ccbh->timeout = fuword(&u_ccbh->timeout); 694 ccb_len = targccblen(k_ccbh->func_code) - sizeof(struct ccb_hdr); 695 error = copyin(u_ccbh + 1, k_ccbh + 1, ccb_len); 696 if (error != 0) { 697 k_ccbh->status = CAM_REQ_CMP_ERR; 698 return (error); 699 } 700 701 /* Translate usermode abort_ccb pointer to its kernel counterpart */ 702 if (k_ccbh->func_code == XPT_ABORT) { 703 struct ccb_abort *cab; 704 struct ccb_hdr *ccb_h; 705 706 cab = (struct ccb_abort *)ccb; 707 TARG_LOCK(softc); 708 TAILQ_FOREACH(ccb_h, &softc->pending_ccb_queue, 709 periph_links.tqe) { 710 struct targ_cmd_descr *ab_descr; 711 712 ab_descr = (struct targ_cmd_descr *)ccb_h->targ_descr; 713 if (ab_descr->user_ccb == cab->abort_ccb) { 714 CAM_DEBUG(softc->path, CAM_DEBUG_PERIPH, 715 ("Changing abort for %p to %p\n", 716 cab->abort_ccb, ccb_h)); 717 cab->abort_ccb = (union ccb *)ccb_h; 718 break; 719 } 720 } 721 TARG_UNLOCK(softc); 722 /* CCB not found, set appropriate status */ 723 if (ccb_h == NULL) { 724 k_ccbh->status = CAM_PATH_INVALID; 725 error = ESRCH; 726 } 727 } 728 729 return (error); 730 } 731 732 /* Build and send a kernel CCB formed from descr->user_ccb */ 733 static int 734 targsendccb(struct targ_softc *softc, union ccb *ccb, 735 struct targ_cmd_descr *descr) 736 { 737 struct cam_periph_map_info *mapinfo; 738 struct ccb_hdr *ccb_h; 739 int error; 740 741 ccb_h = &ccb->ccb_h; 742 mapinfo = &descr->mapinfo; 743 mapinfo->num_bufs_used = 0; 744 745 /* 746 * There's no way for the user to have a completion 747 * function, so we put our own completion function in here. 748 * We also stash in a reference to our descriptor so targreturnccb() 749 * can find our mapping info. 750 */ 751 ccb_h->cbfcnp = targdone; 752 ccb_h->targ_descr = descr; 753 754 /* 755 * We only attempt to map the user memory into kernel space 756 * if they haven't passed in a physical memory pointer, 757 * and if there is actually an I/O operation to perform. 758 * Right now cam_periph_mapmem() only supports SCSI and device 759 * match CCBs. For the SCSI CCBs, we only pass the CCB in if 760 * there's actually data to map. cam_periph_mapmem() will do the 761 * right thing, even if there isn't data to map, but since CCBs 762 * without data are a reasonably common occurance (e.g. test unit 763 * ready), it will save a few cycles if we check for it here. 764 */ 765 if (((ccb_h->flags & CAM_DATA_PHYS) == 0) 766 && (((ccb_h->func_code == XPT_CONT_TARGET_IO) 767 && ((ccb_h->flags & CAM_DIR_MASK) != CAM_DIR_NONE)) 768 || (ccb_h->func_code == XPT_DEV_MATCH))) { 769 770 error = cam_periph_mapmem(ccb, mapinfo); 771 772 /* 773 * cam_periph_mapmem returned an error, we can't continue. 774 * Return the error to the user. 775 */ 776 if (error) { 777 ccb_h->status = CAM_REQ_CMP_ERR; 778 mapinfo->num_bufs_used = 0; 779 return (error); 780 } 781 } 782 783 /* 784 * Once queued on the pending CCB list, this CCB will be protected 785 * by our error recovery handler. 786 */ 787 CAM_DEBUG(softc->path, CAM_DEBUG_PERIPH, ("sendccb %p\n", ccb)); 788 if (XPT_FC_IS_QUEUED(ccb)) { 789 TARG_LOCK(softc); 790 TAILQ_INSERT_TAIL(&softc->pending_ccb_queue, ccb_h, 791 periph_links.tqe); 792 TARG_UNLOCK(softc); 793 } 794 xpt_action(ccb); 795 796 return (0); 797 } 798 799 /* Completion routine for CCBs (called at splsoftcam) */ 800 static void 801 targdone(struct cam_periph *periph, union ccb *done_ccb) 802 { 803 struct targ_softc *softc; 804 cam_status status; 805 806 CAM_DEBUG(periph->path, CAM_DEBUG_PERIPH, ("targdone %p\n", done_ccb)); 807 softc = (struct targ_softc *)periph->softc; 808 TARG_LOCK(softc); 809 TAILQ_REMOVE(&softc->pending_ccb_queue, &done_ccb->ccb_h, 810 periph_links.tqe); 811 status = done_ccb->ccb_h.status & CAM_STATUS_MASK; 812 813 /* If we're no longer enabled, throw away CCB */ 814 if ((softc->state & TARG_STATE_LUN_ENABLED) == 0) { 815 targfreeccb(softc, done_ccb); 816 return; 817 } 818 /* abort_all_pending() waits for pending queue to be empty */ 819 if (TAILQ_EMPTY(&softc->pending_ccb_queue)) 820 wakeup(&softc->pending_ccb_queue); 821 822 switch (done_ccb->ccb_h.func_code) { 823 /* All FC_*_QUEUED CCBs go back to userland */ 824 case XPT_IMMED_NOTIFY: 825 case XPT_ACCEPT_TARGET_IO: 826 case XPT_CONT_TARGET_IO: 827 TAILQ_INSERT_TAIL(&softc->user_ccb_queue, &done_ccb->ccb_h, 828 periph_links.tqe); 829 notify_user(softc); 830 break; 831 default: 832 panic("targdone: impossible xpt opcode %#x", 833 done_ccb->ccb_h.func_code); 834 /* NOTREACHED */ 835 } 836 TARG_UNLOCK(softc); 837 } 838 839 /* Return CCBs to the user from the user queue and abort queue */ 840 static int 841 targread(dev_t dev, struct uio *uio, int ioflag) 842 { 843 struct descr_queue *abort_queue; 844 struct targ_cmd_descr *user_descr; 845 struct targ_softc *softc; 846 struct ccb_queue *user_queue; 847 struct ccb_hdr *ccb_h; 848 union ccb *user_ccb; 849 int read_len, error; 850 851 error = 0; 852 read_len = 0; 853 softc = (struct targ_softc *)dev->si_drv1; 854 user_queue = &softc->user_ccb_queue; 855 abort_queue = &softc->abort_queue; 856 CAM_DEBUG(softc->path, CAM_DEBUG_PERIPH, ("targread\n")); 857 858 /* If no data is available, wait or return immediately */ 859 TARG_LOCK(softc); 860 ccb_h = TAILQ_FIRST(user_queue); 861 user_descr = TAILQ_FIRST(abort_queue); 862 while (ccb_h == NULL && user_descr == NULL) { 863 if ((ioflag & IO_NDELAY) == 0) { 864 error = msleep(user_queue, &softc->mtx, 865 PRIBIO | PCATCH, "targrd", 0); 866 ccb_h = TAILQ_FIRST(user_queue); 867 user_descr = TAILQ_FIRST(abort_queue); 868 if (error != 0) { 869 if (error == ERESTART) { 870 continue; 871 } else { 872 TARG_UNLOCK(softc); 873 goto read_fail; 874 } 875 } 876 } else { 877 TARG_UNLOCK(softc); 878 return (EAGAIN); 879 } 880 } 881 882 /* Data is available so fill the user's buffer */ 883 while (ccb_h != NULL) { 884 struct targ_cmd_descr *descr; 885 886 if (uio->uio_resid < sizeof(user_ccb)) 887 break; 888 TAILQ_REMOVE(user_queue, ccb_h, periph_links.tqe); 889 TARG_UNLOCK(softc); 890 descr = (struct targ_cmd_descr *)ccb_h->targ_descr; 891 user_ccb = descr->user_ccb; 892 CAM_DEBUG(softc->path, CAM_DEBUG_PERIPH, 893 ("targread ccb %p (%p)\n", ccb_h, user_ccb)); 894 error = targreturnccb(softc, (union ccb *)ccb_h); 895 if (error != 0) 896 goto read_fail; 897 error = uiomove((caddr_t)&user_ccb, sizeof(user_ccb), uio); 898 if (error != 0) 899 goto read_fail; 900 read_len += sizeof(user_ccb); 901 902 TARG_LOCK(softc); 903 ccb_h = TAILQ_FIRST(user_queue); 904 } 905 906 /* Flush out any aborted descriptors */ 907 while (user_descr != NULL) { 908 if (uio->uio_resid < sizeof(user_ccb)) 909 break; 910 TAILQ_REMOVE(abort_queue, user_descr, tqe); 911 TARG_UNLOCK(softc); 912 user_ccb = user_descr->user_ccb; 913 CAM_DEBUG(softc->path, CAM_DEBUG_PERIPH, 914 ("targread aborted descr %p (%p)\n", 915 user_descr, user_ccb)); 916 suword(&user_ccb->ccb_h.status, CAM_REQ_ABORTED); 917 error = uiomove((caddr_t)&user_ccb, sizeof(user_ccb), uio); 918 if (error != 0) 919 goto read_fail; 920 read_len += sizeof(user_ccb); 921 922 TARG_LOCK(softc); 923 user_descr = TAILQ_FIRST(abort_queue); 924 } 925 TARG_UNLOCK(softc); 926 927 /* 928 * If we've successfully read some amount of data, don't report an 929 * error. If the error is persistent, it will be reported on the 930 * next read(). 931 */ 932 if (read_len == 0 && uio->uio_resid != 0) 933 error = ENOSPC; 934 935 read_fail: 936 return (error); 937 } 938 939 /* Copy completed ccb back to the user */ 940 static int 941 targreturnccb(struct targ_softc *softc, union ccb *ccb) 942 { 943 struct targ_cmd_descr *descr; 944 struct ccb_hdr *u_ccbh; 945 size_t ccb_len; 946 int error; 947 948 CAM_DEBUG(softc->path, CAM_DEBUG_PERIPH, ("targreturnccb %p\n", ccb)); 949 descr = (struct targ_cmd_descr *)ccb->ccb_h.targ_descr; 950 u_ccbh = &descr->user_ccb->ccb_h; 951 952 /* Copy out the central portion of the ccb_hdr */ 953 copyout(&ccb->ccb_h.retry_count, &u_ccbh->retry_count, 954 offsetof(struct ccb_hdr, periph_priv) - 955 offsetof(struct ccb_hdr, retry_count)); 956 957 /* Copy out the rest of the ccb (after the ccb_hdr) */ 958 ccb_len = targccblen(ccb->ccb_h.func_code) - sizeof(struct ccb_hdr); 959 if (descr->mapinfo.num_bufs_used != 0) 960 cam_periph_unmapmem(ccb, &descr->mapinfo); 961 error = copyout(&ccb->ccb_h + 1, u_ccbh + 1, ccb_len); 962 if (error != 0) { 963 xpt_print_path(softc->path); 964 printf("targreturnccb - CCB copyout failed (%d)\n", 965 error); 966 } 967 /* Free CCB or send back to devq. */ 968 targfreeccb(softc, ccb); 969 970 return (error); 971 } 972 973 static union ccb * 974 targgetccb(struct targ_softc *softc, xpt_opcode type, int priority) 975 { 976 union ccb *ccb; 977 int ccb_len; 978 979 ccb_len = targccblen(type); 980 MALLOC(ccb, union ccb *, ccb_len, M_TARG, 0); 981 CAM_DEBUG(softc->path, CAM_DEBUG_PERIPH, ("getccb %p\n", ccb)); 982 983 xpt_setup_ccb(&ccb->ccb_h, softc->path, priority); 984 ccb->ccb_h.func_code = type; 985 ccb->ccb_h.cbfcnp = targdone; 986 ccb->ccb_h.targ_descr = targgetdescr(softc); 987 return (ccb); 988 } 989 990 static void 991 targfreeccb(struct targ_softc *softc, union ccb *ccb) 992 { 993 CAM_DEBUG_PRINT(CAM_DEBUG_PERIPH, ("targfreeccb descr %p and\n", 994 ccb->ccb_h.targ_descr)); 995 FREE(ccb->ccb_h.targ_descr, M_TARG); 996 997 switch (ccb->ccb_h.func_code) { 998 case XPT_ACCEPT_TARGET_IO: 999 case XPT_IMMED_NOTIFY: 1000 CAM_DEBUG_PRINT(CAM_DEBUG_PERIPH, ("freeing ccb %p\n", ccb)); 1001 FREE(ccb, M_TARG); 1002 break; 1003 default: 1004 /* Send back CCB if we got it from the periph */ 1005 if (XPT_FC_IS_QUEUED(ccb)) { 1006 CAM_DEBUG_PRINT(CAM_DEBUG_PERIPH, 1007 ("returning queued ccb %p\n", ccb)); 1008 xpt_release_ccb(ccb); 1009 } else { 1010 CAM_DEBUG_PRINT(CAM_DEBUG_PERIPH, 1011 ("freeing ccb %p\n", ccb)); 1012 FREE(ccb, M_TARG); 1013 } 1014 break; 1015 } 1016 } 1017 1018 static struct targ_cmd_descr * 1019 targgetdescr(struct targ_softc *softc) 1020 { 1021 struct targ_cmd_descr *descr; 1022 1023 MALLOC(descr, struct targ_cmd_descr *, sizeof(*descr), M_TARG, 1024 0); 1025 descr->mapinfo.num_bufs_used = 0; 1026 return (descr); 1027 } 1028 1029 static void 1030 targinit(void) 1031 { 1032 mtx_init(&targ_mtx, "targ global", NULL, MTX_DEF); 1033 EVENTHANDLER_REGISTER(dev_clone, targclone, 0, 1000); 1034 cdevsw_add(&targ_cdevsw); 1035 } 1036 1037 static void 1038 targclone(void *arg, char *name, int namelen, dev_t *dev) 1039 { 1040 int u; 1041 1042 if (*dev != NODEV) 1043 return; 1044 if (dev_stdclone(name, NULL, "targ", &u) != 1) 1045 return; 1046 *dev = make_dev(&targ_cdevsw, unit2minor(u), UID_ROOT, GID_WHEEL, 1047 0600, "targ%d", u); 1048 (*dev)->si_flags |= SI_CHEAPCLONE; 1049 } 1050 1051 static void 1052 targasync(void *callback_arg, u_int32_t code, struct cam_path *path, void *arg) 1053 { 1054 /* All events are handled in usermode by INOTs */ 1055 panic("targasync() called, should be an INOT instead"); 1056 } 1057 1058 /* Cancel all pending requests and CCBs awaiting work. */ 1059 static void 1060 abort_all_pending(struct targ_softc *softc) 1061 { 1062 struct targ_cmd_descr *descr; 1063 struct ccb_abort cab; 1064 struct ccb_hdr *ccb_h; 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_path(cab.ccb_h.path); 1090 printf("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 msleep(&softc->pending_ccb_queue, &softc->mtx, 1098 PRIBIO | PCATCH, "tgabrt", 0); 1099 } 1100 1101 /* If we aborted anything from the work queue, wakeup user. */ 1102 if (!TAILQ_EMPTY(&softc->user_ccb_queue) 1103 || !TAILQ_EMPTY(&softc->abort_queue)) 1104 notify_user(softc); 1105 } 1106 1107 /* Notify the user that data is ready */ 1108 static void 1109 notify_user(struct targ_softc *softc) 1110 { 1111 /* 1112 * Notify users sleeping via poll(), kqueue(), and 1113 * blocking read(). 1114 */ 1115 selwakeup(&softc->read_select); 1116 KNOTE(&softc->read_select.si_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