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