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