1 /* 2 * Copyright (c) 1997, 1998, 2000 Justin T. Gibbs. 3 * Copyright (c) 1997, 1998, 1999 Kenneth D. Merry. 4 * All rights reserved. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions, and the following disclaimer, 11 * without modification, immediately at the beginning of the file. 12 * 2. The name of the author may not be used to endorse or promote products 13 * derived from this software without specific prior written permission. 14 * 15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR 19 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 25 * SUCH DAMAGE. 26 * 27 * $FreeBSD: src/sys/cam/scsi/scsi_pass.c,v 1.19 2000/01/17 06:27:37 mjacob Exp $ 28 * $DragonFly: src/sys/bus/cam/scsi/scsi_pass.c,v 1.23 2007/11/18 17:53:01 pavalos Exp $ 29 */ 30 31 #include <sys/param.h> 32 #include <sys/systm.h> 33 #include <sys/kernel.h> 34 #include <sys/types.h> 35 #include <sys/buf.h> 36 #include <sys/malloc.h> 37 #include <sys/fcntl.h> 38 #include <sys/stat.h> 39 #include <sys/conf.h> 40 #include <sys/errno.h> 41 #include <sys/devicestat.h> 42 #include <sys/buf2.h> 43 #include <sys/thread2.h> 44 45 #include "../cam.h" 46 #include "../cam_ccb.h" 47 #include "../cam_extend.h" 48 #include "../cam_periph.h" 49 #include "../cam_queue.h" 50 #include "../cam_xpt_periph.h" 51 #include "../cam_debug.h" 52 53 #include "scsi_all.h" 54 #include "scsi_message.h" 55 #include "scsi_da.h" 56 #include "scsi_pass.h" 57 58 typedef enum { 59 PASS_FLAG_OPEN = 0x01, 60 PASS_FLAG_LOCKED = 0x02, 61 PASS_FLAG_INVALID = 0x04 62 } pass_flags; 63 64 typedef enum { 65 PASS_STATE_NORMAL 66 } pass_state; 67 68 typedef enum { 69 PASS_CCB_BUFFER_IO, 70 PASS_CCB_WAITING 71 } pass_ccb_types; 72 73 #define ccb_type ppriv_field0 74 #define ccb_bio ppriv_ptr1 75 76 struct pass_softc { 77 pass_state state; 78 pass_flags flags; 79 u_int8_t pd_type; 80 union ccb saved_ccb; 81 struct devstat device_stats; 82 }; 83 84 #define PASS_CDEV_MAJOR 31 85 86 static d_open_t passopen; 87 static d_close_t passclose; 88 static d_ioctl_t passioctl; 89 90 static periph_init_t passinit; 91 static periph_ctor_t passregister; 92 static periph_oninv_t passoninvalidate; 93 static periph_dtor_t passcleanup; 94 static periph_start_t passstart; 95 static void passasync(void *callback_arg, u_int32_t code, 96 struct cam_path *path, void *arg); 97 static void passdone(struct cam_periph *periph, 98 union ccb *done_ccb); 99 static int passerror(union ccb *ccb, u_int32_t cam_flags, 100 u_int32_t sense_flags); 101 static int passsendccb(struct cam_periph *periph, union ccb *ccb, 102 union ccb *inccb); 103 104 static struct periph_driver passdriver = 105 { 106 passinit, "pass", 107 TAILQ_HEAD_INITIALIZER(passdriver.units), /* generation */ 0 108 }; 109 110 PERIPHDRIVER_DECLARE(pass, passdriver); 111 112 static struct dev_ops pass_ops = { 113 { "pass", PASS_CDEV_MAJOR, 0 }, 114 .d_open = passopen, 115 .d_close = passclose, 116 .d_read = noread, 117 .d_write = nowrite, 118 .d_ioctl = passioctl, 119 .d_strategy = nostrategy, 120 }; 121 122 static struct extend_array *passperiphs; 123 124 static void 125 passinit(void) 126 { 127 cam_status status; 128 struct cam_path *path; 129 130 /* 131 * Create our extend array for storing the devices we attach to. 132 */ 133 passperiphs = cam_extend_new(); 134 if (passperiphs == NULL) { 135 kprintf("passm: Failed to alloc extend array!\n"); 136 return; 137 } 138 139 /* 140 * Install a global async callback. This callback will 141 * receive async callbacks like "new device found". 142 */ 143 status = xpt_create_path(&path, /*periph*/NULL, CAM_XPT_PATH_ID, 144 CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD); 145 146 if (status == CAM_REQ_CMP) { 147 struct ccb_setasync csa; 148 149 xpt_setup_ccb(&csa.ccb_h, path, /*priority*/5); 150 csa.ccb_h.func_code = XPT_SASYNC_CB; 151 csa.event_enable = AC_FOUND_DEVICE; 152 csa.callback = passasync; 153 csa.callback_arg = NULL; 154 xpt_action((union ccb *)&csa); 155 status = csa.ccb_h.status; 156 xpt_free_path(path); 157 } 158 159 if (status != CAM_REQ_CMP) { 160 kprintf("pass: Failed to attach master async callback " 161 "due to status 0x%x!\n", status); 162 } 163 164 } 165 166 static void 167 passoninvalidate(struct cam_periph *periph) 168 { 169 struct pass_softc *softc; 170 struct ccb_setasync csa; 171 172 softc = (struct pass_softc *)periph->softc; 173 174 /* 175 * De-register any async callbacks. 176 */ 177 xpt_setup_ccb(&csa.ccb_h, periph->path, 178 /* priority */ 5); 179 csa.ccb_h.func_code = XPT_SASYNC_CB; 180 csa.event_enable = 0; 181 csa.callback = passasync; 182 csa.callback_arg = periph; 183 xpt_action((union ccb *)&csa); 184 185 softc->flags |= PASS_FLAG_INVALID; 186 187 if (bootverbose) { 188 xpt_print_path(periph->path); 189 kprintf("lost device\n"); 190 } 191 192 } 193 194 static void 195 passcleanup(struct cam_periph *periph) 196 { 197 struct pass_softc *softc; 198 199 softc = (struct pass_softc *)periph->softc; 200 201 devstat_remove_entry(&softc->device_stats); 202 203 cam_extend_release(passperiphs, periph->unit_number); 204 205 if (bootverbose) { 206 xpt_print_path(periph->path); 207 kprintf("removing device entry\n"); 208 } 209 dev_ops_remove(&pass_ops, -1, periph->unit_number); 210 kfree(softc, M_DEVBUF); 211 } 212 213 static void 214 passasync(void *callback_arg, u_int32_t code, 215 struct cam_path *path, void *arg) 216 { 217 struct cam_periph *periph; 218 219 periph = (struct cam_periph *)callback_arg; 220 221 switch (code) { 222 case AC_FOUND_DEVICE: 223 { 224 struct ccb_getdev *cgd; 225 cam_status status; 226 227 cgd = (struct ccb_getdev *)arg; 228 229 /* 230 * Allocate a peripheral instance for 231 * this device and start the probe 232 * process. 233 */ 234 status = cam_periph_alloc(passregister, passoninvalidate, 235 passcleanup, passstart, "pass", 236 CAM_PERIPH_BIO, cgd->ccb_h.path, 237 passasync, AC_FOUND_DEVICE, cgd); 238 239 if (status != CAM_REQ_CMP 240 && status != CAM_REQ_INPROG) { 241 const struct cam_status_entry *entry; 242 243 entry = cam_fetch_status_entry(status); 244 245 kprintf("passasync: Unable to attach new device " 246 "due to status %#x: %s\n", status, entry ? 247 entry->status_text : "Unknown"); 248 } 249 250 break; 251 } 252 default: 253 cam_periph_async(periph, code, path, arg); 254 break; 255 } 256 } 257 258 static cam_status 259 passregister(struct cam_periph *periph, void *arg) 260 { 261 struct pass_softc *softc; 262 struct ccb_setasync csa; 263 struct ccb_getdev *cgd; 264 int no_tags; 265 266 cgd = (struct ccb_getdev *)arg; 267 if (periph == NULL) { 268 kprintf("passregister: periph was NULL!!\n"); 269 return(CAM_REQ_CMP_ERR); 270 } 271 272 if (cgd == NULL) { 273 kprintf("passregister: no getdev CCB, can't register device\n"); 274 return(CAM_REQ_CMP_ERR); 275 } 276 277 softc = kmalloc(sizeof(*softc), M_DEVBUF, M_INTWAIT | M_ZERO); 278 softc->state = PASS_STATE_NORMAL; 279 softc->pd_type = SID_TYPE(&cgd->inq_data); 280 281 periph->softc = softc; 282 cam_extend_set(passperiphs, periph->unit_number, periph); 283 284 /* 285 * We pass in 0 for a blocksize, since we don't 286 * know what the blocksize of this device is, if 287 * it even has a blocksize. 288 */ 289 no_tags = (cgd->inq_data.flags & SID_CmdQue) == 0; 290 devstat_add_entry(&softc->device_stats, "pass", periph->unit_number, 0, 291 DEVSTAT_NO_BLOCKSIZE 292 | (no_tags ? DEVSTAT_NO_ORDERED_TAGS : 0), 293 softc->pd_type | 294 DEVSTAT_TYPE_IF_SCSI | 295 DEVSTAT_TYPE_PASS, 296 DEVSTAT_PRIORITY_PASS); 297 298 /* Register the device */ 299 dev_ops_add(&pass_ops, -1, periph->unit_number); 300 make_dev(&pass_ops, periph->unit_number, UID_ROOT, 301 GID_OPERATOR, 0600, "%s%d", periph->periph_name, 302 periph->unit_number); 303 304 /* 305 * Add an async callback so that we get 306 * notified if this device goes away. 307 */ 308 xpt_setup_ccb(&csa.ccb_h, periph->path, /* priority */ 5); 309 csa.ccb_h.func_code = XPT_SASYNC_CB; 310 csa.event_enable = AC_LOST_DEVICE; 311 csa.callback = passasync; 312 csa.callback_arg = periph; 313 xpt_action((union ccb *)&csa); 314 315 if (bootverbose) 316 xpt_announce_periph(periph, NULL); 317 318 return(CAM_REQ_CMP); 319 } 320 321 static int 322 passopen(struct dev_open_args *ap) 323 { 324 cdev_t dev = ap->a_head.a_dev; 325 struct cam_periph *periph; 326 struct pass_softc *softc; 327 int unit, error; 328 329 error = 0; /* default to no error */ 330 331 /* unit = dkunit(dev); */ 332 /* XXX KDM fix this */ 333 unit = minor(dev) & 0xff; 334 335 periph = cam_extend_get(passperiphs, unit); 336 337 if (periph == NULL) 338 return (ENXIO); 339 340 softc = (struct pass_softc *)periph->softc; 341 342 crit_enter(); 343 if (softc->flags & PASS_FLAG_INVALID) { 344 crit_exit(); 345 return(ENXIO); 346 } 347 348 /* 349 * Don't allow access when we're running at a high securelvel. 350 */ 351 if (securelevel > 1) { 352 crit_exit(); 353 return(EPERM); 354 } 355 356 /* 357 * Only allow read-write access. 358 */ 359 if (((ap->a_oflags & FWRITE) == 0) || ((ap->a_oflags & FREAD) == 0)) { 360 crit_exit(); 361 return(EPERM); 362 } 363 364 /* 365 * We don't allow nonblocking access. 366 */ 367 if ((ap->a_oflags & O_NONBLOCK) != 0) { 368 xpt_print_path(periph->path); 369 kprintf("can't do nonblocking accesss\n"); 370 crit_exit(); 371 return(EINVAL); 372 } 373 374 if ((error = cam_periph_lock(periph, PCATCH)) != 0) { 375 crit_exit(); 376 return (error); 377 } 378 379 crit_exit(); 380 381 if ((softc->flags & PASS_FLAG_OPEN) == 0) { 382 if (cam_periph_acquire(periph) != CAM_REQ_CMP) 383 return(ENXIO); 384 softc->flags |= PASS_FLAG_OPEN; 385 } 386 387 cam_periph_unlock(periph); 388 389 return (error); 390 } 391 392 static int 393 passclose(struct dev_close_args *ap) 394 { 395 cdev_t dev = ap->a_head.a_dev; 396 struct cam_periph *periph; 397 struct pass_softc *softc; 398 int unit, error; 399 400 /* unit = dkunit(dev); */ 401 /* XXX KDM fix this */ 402 unit = minor(dev) & 0xff; 403 404 periph = cam_extend_get(passperiphs, unit); 405 if (periph == NULL) 406 return (ENXIO); 407 408 softc = (struct pass_softc *)periph->softc; 409 410 if ((error = cam_periph_lock(periph, 0)) != 0) 411 return (error); 412 413 softc->flags &= ~PASS_FLAG_OPEN; 414 415 cam_periph_unlock(periph); 416 cam_periph_release(periph); 417 418 return (0); 419 } 420 421 static void 422 passstart(struct cam_periph *periph, union ccb *start_ccb) 423 { 424 struct pass_softc *softc; 425 426 softc = (struct pass_softc *)periph->softc; 427 428 switch (softc->state) { 429 case PASS_STATE_NORMAL: 430 crit_enter(); 431 start_ccb->ccb_h.ccb_type = PASS_CCB_WAITING; 432 SLIST_INSERT_HEAD(&periph->ccb_list, &start_ccb->ccb_h, 433 periph_links.sle); 434 periph->immediate_priority = CAM_PRIORITY_NONE; 435 crit_exit(); 436 wakeup(&periph->ccb_list); 437 break; 438 } 439 } 440 441 static void 442 passdone(struct cam_periph *periph, union ccb *done_ccb) 443 { 444 struct pass_softc *softc; 445 struct ccb_scsiio *csio; 446 447 softc = (struct pass_softc *)periph->softc; 448 csio = &done_ccb->csio; 449 switch (csio->ccb_h.ccb_type) { 450 case PASS_CCB_WAITING: 451 /* Caller will release the CCB */ 452 wakeup(&done_ccb->ccb_h.cbfcnp); 453 return; 454 } 455 xpt_release_ccb(done_ccb); 456 } 457 458 static int 459 passioctl(struct dev_ioctl_args *ap) 460 { 461 cdev_t dev = ap->a_head.a_dev; 462 caddr_t addr = ap->a_data; 463 struct cam_periph *periph; 464 struct pass_softc *softc; 465 u_int8_t unit; 466 int error; 467 468 469 /* unit = dkunit(dev); */ 470 /* XXX KDM fix this */ 471 unit = minor(dev) & 0xff; 472 473 periph = cam_extend_get(passperiphs, unit); 474 475 if (periph == NULL) 476 return(ENXIO); 477 478 softc = (struct pass_softc *)periph->softc; 479 480 error = 0; 481 482 switch (ap->a_cmd) { 483 484 case CAMIOCOMMAND: 485 { 486 union ccb *inccb; 487 union ccb *ccb; 488 int ccb_malloced; 489 490 inccb = (union ccb *)addr; 491 492 /* 493 * Some CCB types, like scan bus and scan lun can only go 494 * through the transport layer device. 495 */ 496 if (inccb->ccb_h.func_code & XPT_FC_XPT_ONLY) { 497 xpt_print_path(periph->path); 498 kprintf("CCB function code %#x is restricted to the " 499 "XPT device\n", inccb->ccb_h.func_code); 500 error = ENODEV; 501 break; 502 } 503 504 /* 505 * Non-immediate CCBs need a CCB from the per-device pool 506 * of CCBs, which is scheduled by the transport layer. 507 * Immediate CCBs and user-supplied CCBs should just be 508 * malloced. 509 */ 510 if ((inccb->ccb_h.func_code & XPT_FC_QUEUED) 511 && ((inccb->ccb_h.func_code & XPT_FC_USER_CCB) == 0)) { 512 ccb = cam_periph_getccb(periph, 513 inccb->ccb_h.pinfo.priority); 514 ccb_malloced = 0; 515 } else { 516 ccb = xpt_alloc_ccb(); 517 518 if (ccb != NULL) 519 xpt_setup_ccb(&ccb->ccb_h, periph->path, 520 inccb->ccb_h.pinfo.priority); 521 ccb_malloced = 1; 522 } 523 524 if (ccb == NULL) { 525 xpt_print_path(periph->path); 526 kprintf("unable to allocate CCB\n"); 527 error = ENOMEM; 528 break; 529 } 530 531 error = passsendccb(periph, ccb, inccb); 532 533 if (ccb_malloced) 534 xpt_free_ccb(ccb); 535 else 536 xpt_release_ccb(ccb); 537 538 break; 539 } 540 default: 541 error = cam_periph_ioctl(periph, ap->a_cmd, addr, passerror); 542 break; 543 } 544 545 return(error); 546 } 547 548 /* 549 * Generally, "ccb" should be the CCB supplied by the kernel. "inccb" 550 * should be the CCB that is copied in from the user. 551 */ 552 static int 553 passsendccb(struct cam_periph *periph, union ccb *ccb, union ccb *inccb) 554 { 555 struct pass_softc *softc; 556 struct cam_periph_map_info mapinfo; 557 int error, need_unmap; 558 559 softc = (struct pass_softc *)periph->softc; 560 561 need_unmap = 0; 562 563 /* 564 * There are some fields in the CCB header that need to be 565 * preserved, the rest we get from the user. 566 */ 567 xpt_merge_ccb(ccb, inccb); 568 569 /* 570 * There's no way for the user to have a completion 571 * function, so we put our own completion function in here. 572 */ 573 ccb->ccb_h.cbfcnp = passdone; 574 575 /* 576 * We only attempt to map the user memory into kernel space 577 * if they haven't passed in a physical memory pointer, 578 * and if there is actually an I/O operation to perform. 579 * Right now cam_periph_mapmem() only supports SCSI and device 580 * match CCBs. For the SCSI CCBs, we only pass the CCB in if 581 * there's actually data to map. cam_periph_mapmem() will do the 582 * right thing, even if there isn't data to map, but since CCBs 583 * without data are a reasonably common occurance (e.g. test unit 584 * ready), it will save a few cycles if we check for it here. 585 */ 586 if (((ccb->ccb_h.flags & CAM_DATA_PHYS) == 0) 587 && (((ccb->ccb_h.func_code == XPT_SCSI_IO) 588 && ((ccb->ccb_h.flags & CAM_DIR_MASK) != CAM_DIR_NONE)) 589 || (ccb->ccb_h.func_code == XPT_DEV_MATCH))) { 590 591 bzero(&mapinfo, sizeof(mapinfo)); 592 593 error = cam_periph_mapmem(ccb, &mapinfo); 594 595 /* 596 * cam_periph_mapmem returned an error, we can't continue. 597 * Return the error to the user. 598 */ 599 if (error) 600 return(error); 601 602 /* 603 * We successfully mapped the memory in, so we need to 604 * unmap it when the transaction is done. 605 */ 606 need_unmap = 1; 607 } 608 609 /* 610 * If the user wants us to perform any error recovery, then honor 611 * that request. Otherwise, it's up to the user to perform any 612 * error recovery. 613 */ 614 error = cam_periph_runccb(ccb, 615 (ccb->ccb_h.flags & CAM_PASS_ERR_RECOVER) ? 616 passerror : NULL, 617 /* cam_flags */ CAM_RETRY_SELTO, 618 /* sense_flags */SF_RETRY_UA, 619 &softc->device_stats); 620 621 if (need_unmap != 0) 622 cam_periph_unmapmem(ccb, &mapinfo); 623 624 ccb->ccb_h.cbfcnp = NULL; 625 ccb->ccb_h.periph_priv = inccb->ccb_h.periph_priv; 626 bcopy(ccb, inccb, sizeof(union ccb)); 627 628 return(error); 629 } 630 631 static int 632 passerror(union ccb *ccb, u_int32_t cam_flags, u_int32_t sense_flags) 633 { 634 struct cam_periph *periph; 635 struct pass_softc *softc; 636 637 periph = xpt_path_periph(ccb->ccb_h.path); 638 softc = (struct pass_softc *)periph->softc; 639 640 return(cam_periph_error(ccb, cam_flags, sense_flags, 641 &softc->saved_ccb)); 642 } 643