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