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 */ 29 30 #include <sys/param.h> 31 #include <sys/systm.h> 32 #include <sys/kernel.h> 33 #include <sys/types.h> 34 #include <sys/buf.h> 35 #include <sys/malloc.h> 36 #include <sys/fcntl.h> 37 #include <sys/stat.h> 38 #include <sys/conf.h> 39 #include <sys/errno.h> 40 #include <sys/devicestat.h> 41 #include <sys/buf2.h> 42 #include <sys/thread2.h> 43 44 #include "../cam.h" 45 #include "../cam_ccb.h" 46 #include "../cam_extend.h" 47 #include "../cam_periph.h" 48 #include "../cam_queue.h" 49 #include "../cam_xpt_periph.h" 50 #include "../cam_debug.h" 51 #include "../cam_sim.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 static d_open_t passopen; 85 static d_close_t passclose; 86 static d_ioctl_t passioctl; 87 88 static periph_init_t passinit; 89 static periph_ctor_t passregister; 90 static periph_oninv_t passoninvalidate; 91 static periph_dtor_t passcleanup; 92 static periph_start_t passstart; 93 static void passasync(void *callback_arg, u_int32_t code, 94 struct cam_path *path, void *arg); 95 static void passdone(struct cam_periph *periph, 96 union ccb *done_ccb); 97 static int passerror(union ccb *ccb, u_int32_t cam_flags, 98 u_int32_t sense_flags); 99 static int passsendccb(struct cam_periph *periph, union ccb *ccb, 100 union ccb *inccb); 101 102 static struct periph_driver passdriver = 103 { 104 passinit, "pass", 105 TAILQ_HEAD_INITIALIZER(passdriver.units), /* generation */ 0 106 }; 107 108 PERIPHDRIVER_DECLARE(pass, passdriver); 109 110 static struct dev_ops pass_ops = { 111 { "pass", 0, 0 }, 112 .d_open = passopen, 113 .d_close = passclose, 114 .d_ioctl = passioctl, 115 }; 116 117 static struct extend_array *passperiphs; 118 119 static void 120 passinit(void) 121 { 122 cam_status status; 123 124 /* 125 * Create our extend array for storing the devices we attach to. 126 */ 127 passperiphs = cam_extend_new(); 128 if (passperiphs == NULL) { 129 kprintf("passm: Failed to alloc extend array!\n"); 130 return; 131 } 132 133 /* 134 * Install a global async callback. This callback will 135 * receive async callbacks like "new device found". 136 */ 137 status = xpt_register_async(AC_FOUND_DEVICE, passasync, NULL, NULL); 138 139 if (status != CAM_REQ_CMP) { 140 kprintf("pass: Failed to attach master async callback " 141 "due to status 0x%x!\n", status); 142 } 143 144 } 145 146 static void 147 passoninvalidate(struct cam_periph *periph) 148 { 149 struct pass_softc *softc; 150 151 softc = (struct pass_softc *)periph->softc; 152 153 /* 154 * De-register any async callbacks. 155 */ 156 xpt_register_async(0, passasync, periph, periph->path); 157 158 softc->flags |= PASS_FLAG_INVALID; 159 160 if (bootverbose) { 161 xpt_print(periph->path, "lost device\n"); 162 } 163 164 } 165 166 static void 167 passcleanup(struct cam_periph *periph) 168 { 169 struct pass_softc *softc; 170 171 softc = (struct pass_softc *)periph->softc; 172 173 devstat_remove_entry(&softc->device_stats); 174 175 cam_extend_release(passperiphs, periph->unit_number); 176 177 if (bootverbose) { 178 xpt_print(periph->path, "removing device entry\n"); 179 } 180 dev_ops_remove_minor(&pass_ops, periph->unit_number); 181 kfree(softc, M_DEVBUF); 182 } 183 184 static void 185 passasync(void *callback_arg, u_int32_t code, 186 struct cam_path *path, void *arg) 187 { 188 struct cam_periph *periph; 189 190 periph = (struct cam_periph *)callback_arg; 191 192 switch (code) { 193 case AC_FOUND_DEVICE: 194 { 195 struct ccb_getdev *cgd; 196 cam_status status; 197 198 cgd = (struct ccb_getdev *)arg; 199 if (cgd == NULL) 200 break; 201 202 /* 203 * Don't complain if a valid peripheral is already attached. 204 */ 205 periph = cam_periph_find(cgd->ccb_h.path, "pass"); 206 if (periph && (periph->flags & CAM_PERIPH_INVALID) == 0) 207 break; 208 209 /* 210 * Allocate a peripheral instance for 211 * this device and start the probe 212 * process. 213 */ 214 status = cam_periph_alloc(passregister, passoninvalidate, 215 passcleanup, passstart, "pass", 216 CAM_PERIPH_BIO, cgd->ccb_h.path, 217 passasync, AC_FOUND_DEVICE, cgd); 218 219 if (status != CAM_REQ_CMP && status != CAM_REQ_INPROG) { 220 const struct cam_status_entry *entry; 221 222 entry = cam_fetch_status_entry(status); 223 224 kprintf("passasync: Unable to attach new device " 225 "due to status %#x: %s\n", status, entry ? 226 entry->status_text : "Unknown"); 227 } 228 229 break; 230 } 231 default: 232 cam_periph_async(periph, code, path, arg); 233 break; 234 } 235 } 236 237 static cam_status 238 passregister(struct cam_periph *periph, void *arg) 239 { 240 struct pass_softc *softc; 241 struct ccb_getdev *cgd; 242 int no_tags; 243 244 cgd = (struct ccb_getdev *)arg; 245 if (periph == NULL) { 246 kprintf("passregister: periph was NULL!!\n"); 247 return(CAM_REQ_CMP_ERR); 248 } 249 250 if (cgd == NULL) { 251 kprintf("passregister: no getdev CCB, can't register device\n"); 252 return(CAM_REQ_CMP_ERR); 253 } 254 255 softc = kmalloc(sizeof(*softc), M_DEVBUF, M_INTWAIT | M_ZERO); 256 softc->state = PASS_STATE_NORMAL; 257 softc->pd_type = SID_TYPE(&cgd->inq_data); 258 259 periph->softc = softc; 260 cam_extend_set(passperiphs, periph->unit_number, periph); 261 262 /* 263 * We pass in 0 for a blocksize, since we don't 264 * know what the blocksize of this device is, if 265 * it even has a blocksize. 266 */ 267 CAM_SIM_UNLOCK(periph->sim); 268 no_tags = (cgd->inq_data.flags & SID_CmdQue) == 0; 269 devstat_add_entry(&softc->device_stats, "pass", periph->unit_number, 0, 270 DEVSTAT_NO_BLOCKSIZE 271 | (no_tags ? DEVSTAT_NO_ORDERED_TAGS : 0), 272 softc->pd_type | 273 DEVSTAT_TYPE_IF_SCSI | 274 DEVSTAT_TYPE_PASS, 275 DEVSTAT_PRIORITY_PASS); 276 277 /* Register the device */ 278 make_dev(&pass_ops, periph->unit_number, UID_ROOT, 279 GID_OPERATOR, 0600, "%s%d", periph->periph_name, 280 periph->unit_number); 281 CAM_SIM_LOCK(periph->sim); 282 /* 283 * Add an async callback so that we get 284 * notified if this device goes away. 285 */ 286 xpt_register_async(AC_LOST_DEVICE, passasync, periph, periph->path); 287 288 if (bootverbose) 289 xpt_announce_periph(periph, NULL); 290 291 return(CAM_REQ_CMP); 292 } 293 294 static int 295 passopen(struct dev_open_args *ap) 296 { 297 cdev_t dev = ap->a_head.a_dev; 298 struct cam_periph *periph; 299 struct pass_softc *softc; 300 int unit, error; 301 302 error = 0; /* default to no error */ 303 304 /* unit = dkunit(dev); */ 305 /* XXX KDM fix this */ 306 unit = minor(dev) & 0xff; 307 308 periph = cam_extend_get(passperiphs, unit); 309 310 if (cam_periph_acquire(periph) != CAM_REQ_CMP) 311 return (ENXIO); 312 313 cam_periph_lock(periph); 314 315 softc = (struct pass_softc *)periph->softc; 316 317 if (softc->flags & PASS_FLAG_INVALID) { 318 cam_periph_unlock(periph); 319 cam_periph_release(periph); 320 return(ENXIO); 321 } 322 323 /* 324 * Don't allow access when we're running at a high securelevel. 325 */ 326 if (securelevel > 1) { 327 cam_periph_unlock(periph); 328 cam_periph_release(periph); 329 return(EPERM); 330 } 331 332 /* 333 * Only allow read-write access. 334 */ 335 if (((ap->a_oflags & FWRITE) == 0) || ((ap->a_oflags & FREAD) == 0)) { 336 cam_periph_unlock(periph); 337 cam_periph_release(periph); 338 return(EPERM); 339 } 340 341 /* 342 * We don't allow nonblocking access. 343 */ 344 if ((ap->a_oflags & O_NONBLOCK) != 0) { 345 xpt_print(periph->path, "can't do nonblocking access\n"); 346 cam_periph_unlock(periph); 347 cam_periph_release(periph); 348 return(EINVAL); 349 } 350 351 if ((softc->flags & PASS_FLAG_OPEN) == 0) { 352 softc->flags |= PASS_FLAG_OPEN; 353 } else { 354 /* Device closes aren't symmertical, so fix up the refcount */ 355 cam_periph_release(periph); 356 } 357 358 cam_periph_unlock(periph); 359 360 return (error); 361 } 362 363 static int 364 passclose(struct dev_close_args *ap) 365 { 366 cdev_t dev = ap->a_head.a_dev; 367 struct cam_periph *periph; 368 struct pass_softc *softc; 369 int unit; 370 371 /* unit = dkunit(dev); */ 372 /* XXX KDM fix this */ 373 unit = minor(dev) & 0xff; 374 375 periph = cam_extend_get(passperiphs, unit); 376 if (periph == NULL) 377 return (ENXIO); 378 379 cam_periph_lock(periph); 380 381 softc = (struct pass_softc *)periph->softc; 382 softc->flags &= ~PASS_FLAG_OPEN; 383 384 cam_periph_unlock(periph); 385 cam_periph_release(periph); 386 387 return (0); 388 } 389 390 static void 391 passstart(struct cam_periph *periph, union ccb *start_ccb) 392 { 393 struct pass_softc *softc; 394 395 softc = (struct pass_softc *)periph->softc; 396 397 switch (softc->state) { 398 case PASS_STATE_NORMAL: 399 start_ccb->ccb_h.ccb_type = PASS_CCB_WAITING; 400 SLIST_INSERT_HEAD(&periph->ccb_list, &start_ccb->ccb_h, 401 periph_links.sle); 402 periph->immediate_priority = CAM_PRIORITY_NONE; 403 wakeup(&periph->ccb_list); 404 break; 405 } 406 } 407 408 static void 409 passdone(struct cam_periph *periph, union ccb *done_ccb) 410 { 411 struct ccb_scsiio *csio; 412 413 csio = &done_ccb->csio; 414 switch (csio->ccb_h.ccb_type) { 415 case PASS_CCB_WAITING: 416 /* Caller will release the CCB */ 417 wakeup(&done_ccb->ccb_h.cbfcnp); 418 return; 419 } 420 xpt_release_ccb(done_ccb); 421 } 422 423 static int 424 passioctl(struct dev_ioctl_args *ap) 425 { 426 cdev_t dev = ap->a_head.a_dev; 427 caddr_t addr = ap->a_data; 428 struct cam_periph *periph; 429 u_int8_t unit; 430 int error; 431 432 433 /* unit = dkunit(dev); */ 434 /* XXX KDM fix this */ 435 unit = minor(dev) & 0xff; 436 437 periph = cam_extend_get(passperiphs, unit); 438 439 if (periph == NULL) 440 return(ENXIO); 441 442 cam_periph_lock(periph); 443 444 error = 0; 445 446 switch (ap->a_cmd) { 447 448 case CAMIOCOMMAND: 449 { 450 union ccb *inccb; 451 union ccb *ccb; 452 int ccb_malloced; 453 454 inccb = (union ccb *)addr; 455 456 /* 457 * Some CCB types, like scan bus and scan lun can only go 458 * through the transport layer device. 459 */ 460 if (inccb->ccb_h.func_code & XPT_FC_XPT_ONLY) { 461 xpt_print(periph->path, "CCB function code %#x is " 462 "restricted to the XPT device\n", 463 inccb->ccb_h.func_code); 464 error = ENODEV; 465 break; 466 } 467 468 /* 469 * Non-immediate CCBs need a CCB from the per-device pool 470 * of CCBs, which is scheduled by the transport layer. 471 * Immediate CCBs and user-supplied CCBs should just be 472 * malloced. 473 */ 474 if ((inccb->ccb_h.func_code & XPT_FC_QUEUED) 475 && ((inccb->ccb_h.func_code & XPT_FC_USER_CCB) == 0)) { 476 ccb = cam_periph_getccb(periph, 477 inccb->ccb_h.pinfo.priority); 478 ccb_malloced = 0; 479 } else { 480 ccb = xpt_alloc_ccb(); 481 482 if (ccb != NULL) 483 xpt_setup_ccb(&ccb->ccb_h, periph->path, 484 inccb->ccb_h.pinfo.priority); 485 ccb_malloced = 1; 486 } 487 488 if (ccb == NULL) { 489 xpt_print(periph->path, "unable to allocate CCB\n"); 490 error = ENOMEM; 491 break; 492 } 493 494 error = passsendccb(periph, ccb, inccb); 495 496 if (ccb_malloced) 497 xpt_free_ccb(ccb); 498 else 499 xpt_release_ccb(ccb); 500 501 break; 502 } 503 default: 504 error = cam_periph_ioctl(periph, ap->a_cmd, addr, passerror); 505 break; 506 } 507 508 cam_periph_unlock(periph); 509 return(error); 510 } 511 512 /* 513 * Generally, "ccb" should be the CCB supplied by the kernel. "inccb" 514 * should be the CCB that is copied in from the user. 515 */ 516 static int 517 passsendccb(struct cam_periph *periph, union ccb *ccb, union ccb *inccb) 518 { 519 struct pass_softc *softc; 520 struct cam_periph_map_info mapinfo; 521 int error, need_unmap; 522 523 softc = (struct pass_softc *)periph->softc; 524 525 need_unmap = 0; 526 527 /* 528 * There are some fields in the CCB header that need to be 529 * preserved, the rest we get from the user. 530 */ 531 xpt_merge_ccb(ccb, inccb); 532 533 /* 534 * There's no way for the user to have a completion 535 * function, so we put our own completion function in here. 536 */ 537 ccb->ccb_h.cbfcnp = passdone; 538 539 /* 540 * We only attempt to map the user memory into kernel space 541 * if they haven't passed in a physical memory pointer, 542 * and if there is actually an I/O operation to perform. 543 * Right now cam_periph_mapmem() only supports SCSI and device 544 * match CCBs. For the SCSI CCBs, we only pass the CCB in if 545 * there's actually data to map. cam_periph_mapmem() will do the 546 * right thing, even if there isn't data to map, but since CCBs 547 * without data are a reasonably common occurance (e.g. test unit 548 * ready), it will save a few cycles if we check for it here. 549 */ 550 if (((ccb->ccb_h.flags & CAM_DATA_PHYS) == 0) 551 && (((ccb->ccb_h.func_code == XPT_SCSI_IO) 552 && ((ccb->ccb_h.flags & CAM_DIR_MASK) != CAM_DIR_NONE)) 553 || (ccb->ccb_h.func_code == XPT_DEV_MATCH))) { 554 555 bzero(&mapinfo, sizeof(mapinfo)); 556 557 /* 558 * cam_periph_mapmem calls into proc and vm functions that can 559 * sleep as well as trigger I/O, so we can't hold the lock. 560 * Dropping it here is reasonably safe. 561 */ 562 cam_periph_unlock(periph); 563 error = cam_periph_mapmem(ccb, &mapinfo); 564 cam_periph_lock(periph); 565 566 /* 567 * cam_periph_mapmem returned an error, we can't continue. 568 * Return the error to the user. 569 */ 570 if (error) 571 return(error); 572 573 /* 574 * We successfully mapped the memory in, so we need to 575 * unmap it when the transaction is done. 576 */ 577 need_unmap = 1; 578 } 579 580 /* 581 * If the user wants us to perform any error recovery, then honor 582 * that request. Otherwise, it's up to the user to perform any 583 * error recovery. 584 */ 585 error = cam_periph_runccb(ccb, 586 (ccb->ccb_h.flags & CAM_PASS_ERR_RECOVER) ? 587 passerror : NULL, 588 /* cam_flags */ CAM_RETRY_SELTO, 589 /* sense_flags */SF_RETRY_UA, 590 &softc->device_stats); 591 592 if (need_unmap != 0) 593 cam_periph_unmapmem(ccb, &mapinfo); 594 595 ccb->ccb_h.cbfcnp = NULL; 596 ccb->ccb_h.periph_priv = inccb->ccb_h.periph_priv; 597 bcopy(ccb, inccb, sizeof(union ccb)); 598 599 return(error); 600 } 601 602 static int 603 passerror(union ccb *ccb, u_int32_t cam_flags, u_int32_t sense_flags) 604 { 605 struct cam_periph *periph; 606 struct pass_softc *softc; 607 608 periph = xpt_path_periph(ccb->ccb_h.path); 609 softc = (struct pass_softc *)periph->softc; 610 611 return(cam_periph_error(ccb, cam_flags, sense_flags, 612 &softc->saved_ccb)); 613 } 614