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.26 2007/12/02 04:24:11 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 if (cgd == NULL) 229 break; 230 231 /* 232 * Allocate a peripheral instance for 233 * this device and start the probe 234 * process. 235 */ 236 status = cam_periph_alloc(passregister, passoninvalidate, 237 passcleanup, passstart, "pass", 238 CAM_PERIPH_BIO, cgd->ccb_h.path, 239 passasync, AC_FOUND_DEVICE, cgd); 240 241 if (status != CAM_REQ_CMP 242 && status != CAM_REQ_INPROG) { 243 const struct cam_status_entry *entry; 244 245 entry = cam_fetch_status_entry(status); 246 247 kprintf("passasync: Unable to attach new device " 248 "due to status %#x: %s\n", status, entry ? 249 entry->status_text : "Unknown"); 250 } 251 252 break; 253 } 254 default: 255 cam_periph_async(periph, code, path, arg); 256 break; 257 } 258 } 259 260 static cam_status 261 passregister(struct cam_periph *periph, void *arg) 262 { 263 struct pass_softc *softc; 264 struct ccb_setasync csa; 265 struct ccb_getdev *cgd; 266 int no_tags; 267 268 cgd = (struct ccb_getdev *)arg; 269 if (periph == NULL) { 270 kprintf("passregister: periph was NULL!!\n"); 271 return(CAM_REQ_CMP_ERR); 272 } 273 274 if (cgd == NULL) { 275 kprintf("passregister: no getdev CCB, can't register device\n"); 276 return(CAM_REQ_CMP_ERR); 277 } 278 279 softc = kmalloc(sizeof(*softc), M_DEVBUF, M_INTWAIT | M_ZERO); 280 softc->state = PASS_STATE_NORMAL; 281 softc->pd_type = SID_TYPE(&cgd->inq_data); 282 283 periph->softc = softc; 284 cam_extend_set(passperiphs, periph->unit_number, periph); 285 286 /* 287 * We pass in 0 for a blocksize, since we don't 288 * know what the blocksize of this device is, if 289 * it even has a blocksize. 290 */ 291 no_tags = (cgd->inq_data.flags & SID_CmdQue) == 0; 292 devstat_add_entry(&softc->device_stats, "pass", periph->unit_number, 0, 293 DEVSTAT_NO_BLOCKSIZE 294 | (no_tags ? DEVSTAT_NO_ORDERED_TAGS : 0), 295 softc->pd_type | 296 DEVSTAT_TYPE_IF_SCSI | 297 DEVSTAT_TYPE_PASS, 298 DEVSTAT_PRIORITY_PASS); 299 300 /* Register the device */ 301 dev_ops_add(&pass_ops, -1, periph->unit_number); 302 make_dev(&pass_ops, periph->unit_number, UID_ROOT, 303 GID_OPERATOR, 0600, "%s%d", periph->periph_name, 304 periph->unit_number); 305 306 /* 307 * Add an async callback so that we get 308 * notified if this device goes away. 309 */ 310 xpt_setup_ccb(&csa.ccb_h, periph->path, /* priority */ 5); 311 csa.ccb_h.func_code = XPT_SASYNC_CB; 312 csa.event_enable = AC_LOST_DEVICE; 313 csa.callback = passasync; 314 csa.callback_arg = periph; 315 xpt_action((union ccb *)&csa); 316 317 if (bootverbose) 318 xpt_announce_periph(periph, NULL); 319 320 return(CAM_REQ_CMP); 321 } 322 323 static int 324 passopen(struct dev_open_args *ap) 325 { 326 cdev_t dev = ap->a_head.a_dev; 327 struct cam_periph *periph; 328 struct pass_softc *softc; 329 int unit, error; 330 331 error = 0; /* default to no error */ 332 333 /* unit = dkunit(dev); */ 334 /* XXX KDM fix this */ 335 unit = minor(dev) & 0xff; 336 337 periph = cam_extend_get(passperiphs, unit); 338 339 if (periph == NULL) 340 return (ENXIO); 341 342 softc = (struct pass_softc *)periph->softc; 343 344 crit_enter(); 345 if (softc->flags & PASS_FLAG_INVALID) { 346 crit_exit(); 347 return(ENXIO); 348 } 349 350 /* 351 * Don't allow access when we're running at a high securelevel. 352 */ 353 if (securelevel > 1) { 354 crit_exit(); 355 return(EPERM); 356 } 357 358 /* 359 * Only allow read-write access. 360 */ 361 if (((ap->a_oflags & FWRITE) == 0) || ((ap->a_oflags & FREAD) == 0)) { 362 crit_exit(); 363 return(EPERM); 364 } 365 366 /* 367 * We don't allow nonblocking access. 368 */ 369 if ((ap->a_oflags & O_NONBLOCK) != 0) { 370 xpt_print_path(periph->path); 371 kprintf("can't do nonblocking access\n"); 372 crit_exit(); 373 return(EINVAL); 374 } 375 376 if ((error = cam_periph_lock(periph, PCATCH)) != 0) { 377 crit_exit(); 378 return (error); 379 } 380 381 crit_exit(); 382 383 if ((softc->flags & PASS_FLAG_OPEN) == 0) { 384 if (cam_periph_acquire(periph) != CAM_REQ_CMP) 385 return(ENXIO); 386 softc->flags |= PASS_FLAG_OPEN; 387 } 388 389 cam_periph_unlock(periph); 390 391 return (error); 392 } 393 394 static int 395 passclose(struct dev_close_args *ap) 396 { 397 cdev_t dev = ap->a_head.a_dev; 398 struct cam_periph *periph; 399 struct pass_softc *softc; 400 int unit, error; 401 402 /* unit = dkunit(dev); */ 403 /* XXX KDM fix this */ 404 unit = minor(dev) & 0xff; 405 406 periph = cam_extend_get(passperiphs, unit); 407 if (periph == NULL) 408 return (ENXIO); 409 410 softc = (struct pass_softc *)periph->softc; 411 412 if ((error = cam_periph_lock(periph, 0)) != 0) 413 return (error); 414 415 softc->flags &= ~PASS_FLAG_OPEN; 416 417 cam_periph_unlock(periph); 418 cam_periph_release(periph); 419 420 return (0); 421 } 422 423 static void 424 passstart(struct cam_periph *periph, union ccb *start_ccb) 425 { 426 struct pass_softc *softc; 427 428 softc = (struct pass_softc *)periph->softc; 429 430 switch (softc->state) { 431 case PASS_STATE_NORMAL: 432 crit_enter(); 433 start_ccb->ccb_h.ccb_type = PASS_CCB_WAITING; 434 SLIST_INSERT_HEAD(&periph->ccb_list, &start_ccb->ccb_h, 435 periph_links.sle); 436 periph->immediate_priority = CAM_PRIORITY_NONE; 437 crit_exit(); 438 wakeup(&periph->ccb_list); 439 break; 440 } 441 } 442 443 static void 444 passdone(struct cam_periph *periph, union ccb *done_ccb) 445 { 446 struct pass_softc *softc; 447 struct ccb_scsiio *csio; 448 449 softc = (struct pass_softc *)periph->softc; 450 csio = &done_ccb->csio; 451 switch (csio->ccb_h.ccb_type) { 452 case PASS_CCB_WAITING: 453 /* Caller will release the CCB */ 454 wakeup(&done_ccb->ccb_h.cbfcnp); 455 return; 456 } 457 xpt_release_ccb(done_ccb); 458 } 459 460 static int 461 passioctl(struct dev_ioctl_args *ap) 462 { 463 cdev_t dev = ap->a_head.a_dev; 464 caddr_t addr = ap->a_data; 465 struct cam_periph *periph; 466 struct pass_softc *softc; 467 u_int8_t unit; 468 int error; 469 470 471 /* unit = dkunit(dev); */ 472 /* XXX KDM fix this */ 473 unit = minor(dev) & 0xff; 474 475 periph = cam_extend_get(passperiphs, unit); 476 477 if (periph == NULL) 478 return(ENXIO); 479 480 softc = (struct pass_softc *)periph->softc; 481 482 error = 0; 483 484 switch (ap->a_cmd) { 485 486 case CAMIOCOMMAND: 487 { 488 union ccb *inccb; 489 union ccb *ccb; 490 int ccb_malloced; 491 492 inccb = (union ccb *)addr; 493 494 /* 495 * Some CCB types, like scan bus and scan lun can only go 496 * through the transport layer device. 497 */ 498 if (inccb->ccb_h.func_code & XPT_FC_XPT_ONLY) { 499 xpt_print_path(periph->path); 500 kprintf("CCB function code %#x is restricted to the " 501 "XPT device\n", inccb->ccb_h.func_code); 502 error = ENODEV; 503 break; 504 } 505 506 /* 507 * Non-immediate CCBs need a CCB from the per-device pool 508 * of CCBs, which is scheduled by the transport layer. 509 * Immediate CCBs and user-supplied CCBs should just be 510 * malloced. 511 */ 512 if ((inccb->ccb_h.func_code & XPT_FC_QUEUED) 513 && ((inccb->ccb_h.func_code & XPT_FC_USER_CCB) == 0)) { 514 ccb = cam_periph_getccb(periph, 515 inccb->ccb_h.pinfo.priority); 516 ccb_malloced = 0; 517 } else { 518 ccb = xpt_alloc_ccb(); 519 520 if (ccb != NULL) 521 xpt_setup_ccb(&ccb->ccb_h, periph->path, 522 inccb->ccb_h.pinfo.priority); 523 ccb_malloced = 1; 524 } 525 526 if (ccb == NULL) { 527 xpt_print_path(periph->path); 528 kprintf("unable to allocate CCB\n"); 529 error = ENOMEM; 530 break; 531 } 532 533 error = passsendccb(periph, ccb, inccb); 534 535 if (ccb_malloced) 536 xpt_free_ccb(ccb); 537 else 538 xpt_release_ccb(ccb); 539 540 break; 541 } 542 default: 543 error = cam_periph_ioctl(periph, ap->a_cmd, addr, passerror); 544 break; 545 } 546 547 return(error); 548 } 549 550 /* 551 * Generally, "ccb" should be the CCB supplied by the kernel. "inccb" 552 * should be the CCB that is copied in from the user. 553 */ 554 static int 555 passsendccb(struct cam_periph *periph, union ccb *ccb, union ccb *inccb) 556 { 557 struct pass_softc *softc; 558 struct cam_periph_map_info mapinfo; 559 int error, need_unmap; 560 561 softc = (struct pass_softc *)periph->softc; 562 563 need_unmap = 0; 564 565 /* 566 * There are some fields in the CCB header that need to be 567 * preserved, the rest we get from the user. 568 */ 569 xpt_merge_ccb(ccb, inccb); 570 571 /* 572 * There's no way for the user to have a completion 573 * function, so we put our own completion function in here. 574 */ 575 ccb->ccb_h.cbfcnp = passdone; 576 577 /* 578 * We only attempt to map the user memory into kernel space 579 * if they haven't passed in a physical memory pointer, 580 * and if there is actually an I/O operation to perform. 581 * Right now cam_periph_mapmem() only supports SCSI and device 582 * match CCBs. For the SCSI CCBs, we only pass the CCB in if 583 * there's actually data to map. cam_periph_mapmem() will do the 584 * right thing, even if there isn't data to map, but since CCBs 585 * without data are a reasonably common occurance (e.g. test unit 586 * ready), it will save a few cycles if we check for it here. 587 */ 588 if (((ccb->ccb_h.flags & CAM_DATA_PHYS) == 0) 589 && (((ccb->ccb_h.func_code == XPT_SCSI_IO) 590 && ((ccb->ccb_h.flags & CAM_DIR_MASK) != CAM_DIR_NONE)) 591 || (ccb->ccb_h.func_code == XPT_DEV_MATCH))) { 592 593 bzero(&mapinfo, sizeof(mapinfo)); 594 595 error = cam_periph_mapmem(ccb, &mapinfo); 596 597 /* 598 * cam_periph_mapmem returned an error, we can't continue. 599 * Return the error to the user. 600 */ 601 if (error) 602 return(error); 603 604 /* 605 * We successfully mapped the memory in, so we need to 606 * unmap it when the transaction is done. 607 */ 608 need_unmap = 1; 609 } 610 611 /* 612 * If the user wants us to perform any error recovery, then honor 613 * that request. Otherwise, it's up to the user to perform any 614 * error recovery. 615 */ 616 error = cam_periph_runccb(ccb, 617 (ccb->ccb_h.flags & CAM_PASS_ERR_RECOVER) ? 618 passerror : NULL, 619 /* cam_flags */ CAM_RETRY_SELTO, 620 /* sense_flags */SF_RETRY_UA, 621 &softc->device_stats); 622 623 if (need_unmap != 0) 624 cam_periph_unmapmem(ccb, &mapinfo); 625 626 ccb->ccb_h.cbfcnp = NULL; 627 ccb->ccb_h.periph_priv = inccb->ccb_h.periph_priv; 628 bcopy(ccb, inccb, sizeof(union ccb)); 629 630 return(error); 631 } 632 633 static int 634 passerror(union ccb *ccb, u_int32_t cam_flags, u_int32_t sense_flags) 635 { 636 struct cam_periph *periph; 637 struct pass_softc *softc; 638 639 periph = xpt_path_periph(ccb->ccb_h.path); 640 softc = (struct pass_softc *)periph->softc; 641 642 return(cam_periph_error(ccb, cam_flags, sense_flags, 643 &softc->saved_ccb)); 644 } 645