1 /*- 2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD 3 * 4 * Implementation of SCSI Processor Target Peripheral driver for CAM. 5 * 6 * Copyright (c) 1998 Justin T. Gibbs. 7 * All rights reserved. 8 * 9 * Redistribution and use in source and binary forms, with or without 10 * modification, are permitted provided that the following conditions 11 * are met: 12 * 1. Redistributions of source code must retain the above copyright 13 * notice, this list of conditions, and the following disclaimer, 14 * without modification, immediately at the beginning of the file. 15 * 2. The name of the author may not be used to endorse or promote products 16 * derived from this software without specific prior written permission. 17 * 18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 21 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR 22 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 28 * SUCH DAMAGE. 29 */ 30 31 #include <sys/cdefs.h> 32 __FBSDID("$FreeBSD$"); 33 34 #include <sys/param.h> 35 #include <sys/queue.h> 36 #include <sys/systm.h> 37 #include <sys/kernel.h> 38 #include <sys/types.h> 39 #include <sys/bio.h> 40 #include <sys/devicestat.h> 41 #include <sys/malloc.h> 42 #include <sys/conf.h> 43 #include <sys/ptio.h> 44 45 #include <cam/cam.h> 46 #include <cam/cam_ccb.h> 47 #include <cam/cam_periph.h> 48 #include <cam/cam_xpt_periph.h> 49 #include <cam/cam_debug.h> 50 51 #include <cam/scsi/scsi_all.h> 52 #include <cam/scsi/scsi_message.h> 53 #include <cam/scsi/scsi_pt.h> 54 55 #include "opt_pt.h" 56 57 typedef enum { 58 PT_STATE_PROBE, 59 PT_STATE_NORMAL 60 } pt_state; 61 62 typedef enum { 63 PT_FLAG_NONE = 0x00, 64 PT_FLAG_OPEN = 0x01, 65 PT_FLAG_DEVICE_INVALID = 0x02, 66 PT_FLAG_RETRY_UA = 0x04 67 } pt_flags; 68 69 typedef enum { 70 PT_CCB_BUFFER_IO = 0x01, 71 PT_CCB_RETRY_UA = 0x04, 72 PT_CCB_BUFFER_IO_UA = PT_CCB_BUFFER_IO|PT_CCB_RETRY_UA 73 } pt_ccb_state; 74 75 /* Offsets into our private area for storing information */ 76 #define ccb_state ppriv_field0 77 #define ccb_bp ppriv_ptr1 78 79 struct pt_softc { 80 struct bio_queue_head bio_queue; 81 struct devstat *device_stats; 82 LIST_HEAD(, ccb_hdr) pending_ccbs; 83 pt_state state; 84 pt_flags flags; 85 union ccb saved_ccb; 86 int io_timeout; 87 struct cdev *dev; 88 }; 89 90 static d_open_t ptopen; 91 static d_close_t ptclose; 92 static d_strategy_t ptstrategy; 93 static periph_init_t ptinit; 94 static void ptasync(void *callback_arg, u_int32_t code, 95 struct cam_path *path, void *arg); 96 static periph_ctor_t ptctor; 97 static periph_oninv_t ptoninvalidate; 98 static periph_dtor_t ptdtor; 99 static periph_start_t ptstart; 100 static void ptdone(struct cam_periph *periph, 101 union ccb *done_ccb); 102 static d_ioctl_t ptioctl; 103 static int pterror(union ccb *ccb, u_int32_t cam_flags, 104 u_int32_t sense_flags); 105 106 void scsi_send_receive(struct ccb_scsiio *csio, u_int32_t retries, 107 void (*cbfcnp)(struct cam_periph *, union ccb *), 108 u_int tag_action, int readop, u_int byte2, 109 u_int32_t xfer_len, u_int8_t *data_ptr, 110 u_int8_t sense_len, u_int32_t timeout); 111 112 static struct periph_driver ptdriver = 113 { 114 ptinit, "pt", 115 TAILQ_HEAD_INITIALIZER(ptdriver.units), /* generation */ 0 116 }; 117 118 PERIPHDRIVER_DECLARE(pt, ptdriver); 119 120 121 static struct cdevsw pt_cdevsw = { 122 .d_version = D_VERSION, 123 .d_flags = 0, 124 .d_open = ptopen, 125 .d_close = ptclose, 126 .d_read = physread, 127 .d_write = physwrite, 128 .d_ioctl = ptioctl, 129 .d_strategy = ptstrategy, 130 .d_name = "pt", 131 }; 132 133 #ifndef SCSI_PT_DEFAULT_TIMEOUT 134 #define SCSI_PT_DEFAULT_TIMEOUT 60 135 #endif 136 137 static int 138 ptopen(struct cdev *dev, int flags, int fmt, struct thread *td) 139 { 140 struct cam_periph *periph; 141 struct pt_softc *softc; 142 int error = 0; 143 144 periph = (struct cam_periph *)dev->si_drv1; 145 if (cam_periph_acquire(periph) != CAM_REQ_CMP) 146 return (ENXIO); 147 148 softc = (struct pt_softc *)periph->softc; 149 150 cam_periph_lock(periph); 151 if (softc->flags & PT_FLAG_DEVICE_INVALID) { 152 cam_periph_release_locked(periph); 153 cam_periph_unlock(periph); 154 return(ENXIO); 155 } 156 157 if ((softc->flags & PT_FLAG_OPEN) == 0) 158 softc->flags |= PT_FLAG_OPEN; 159 else { 160 error = EBUSY; 161 cam_periph_release(periph); 162 } 163 164 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, 165 ("ptopen: dev=%s\n", devtoname(dev))); 166 167 cam_periph_unlock(periph); 168 return (error); 169 } 170 171 static int 172 ptclose(struct cdev *dev, int flag, int fmt, struct thread *td) 173 { 174 struct cam_periph *periph; 175 struct pt_softc *softc; 176 177 periph = (struct cam_periph *)dev->si_drv1; 178 softc = (struct pt_softc *)periph->softc; 179 180 cam_periph_lock(periph); 181 182 softc->flags &= ~PT_FLAG_OPEN; 183 cam_periph_release_locked(periph); 184 cam_periph_unlock(periph); 185 return (0); 186 } 187 188 /* 189 * Actually translate the requested transfer into one the physical driver 190 * can understand. The transfer is described by a buf and will include 191 * only one physical transfer. 192 */ 193 static void 194 ptstrategy(struct bio *bp) 195 { 196 struct cam_periph *periph; 197 struct pt_softc *softc; 198 199 periph = (struct cam_periph *)bp->bio_dev->si_drv1; 200 bp->bio_resid = bp->bio_bcount; 201 if (periph == NULL) { 202 biofinish(bp, NULL, ENXIO); 203 return; 204 } 205 cam_periph_lock(periph); 206 softc = (struct pt_softc *)periph->softc; 207 208 /* 209 * If the device has been made invalid, error out 210 */ 211 if ((softc->flags & PT_FLAG_DEVICE_INVALID)) { 212 cam_periph_unlock(periph); 213 biofinish(bp, NULL, ENXIO); 214 return; 215 } 216 217 /* 218 * Place it in the queue of disk activities for this disk 219 */ 220 bioq_insert_tail(&softc->bio_queue, bp); 221 222 /* 223 * Schedule ourselves for performing the work. 224 */ 225 xpt_schedule(periph, CAM_PRIORITY_NORMAL); 226 cam_periph_unlock(periph); 227 228 return; 229 } 230 231 static void 232 ptinit(void) 233 { 234 cam_status status; 235 236 /* 237 * Install a global async callback. This callback will 238 * receive async callbacks like "new device found". 239 */ 240 status = xpt_register_async(AC_FOUND_DEVICE, ptasync, NULL, NULL); 241 242 if (status != CAM_REQ_CMP) { 243 printf("pt: Failed to attach master async callback " 244 "due to status 0x%x!\n", status); 245 } 246 } 247 248 static cam_status 249 ptctor(struct cam_periph *periph, void *arg) 250 { 251 struct pt_softc *softc; 252 struct ccb_getdev *cgd; 253 struct ccb_pathinq cpi; 254 struct make_dev_args args; 255 int error; 256 257 cgd = (struct ccb_getdev *)arg; 258 if (cgd == NULL) { 259 printf("ptregister: no getdev CCB, can't register device\n"); 260 return(CAM_REQ_CMP_ERR); 261 } 262 263 softc = (struct pt_softc *)malloc(sizeof(*softc),M_DEVBUF,M_NOWAIT); 264 265 if (softc == NULL) { 266 printf("daregister: Unable to probe new device. " 267 "Unable to allocate softc\n"); 268 return(CAM_REQ_CMP_ERR); 269 } 270 271 bzero(softc, sizeof(*softc)); 272 LIST_INIT(&softc->pending_ccbs); 273 softc->state = PT_STATE_NORMAL; 274 bioq_init(&softc->bio_queue); 275 276 softc->io_timeout = SCSI_PT_DEFAULT_TIMEOUT * 1000; 277 278 periph->softc = softc; 279 280 bzero(&cpi, sizeof(cpi)); 281 xpt_setup_ccb(&cpi.ccb_h, periph->path, CAM_PRIORITY_NORMAL); 282 cpi.ccb_h.func_code = XPT_PATH_INQ; 283 xpt_action((union ccb *)&cpi); 284 285 cam_periph_unlock(periph); 286 287 make_dev_args_init(&args); 288 args.mda_devsw = &pt_cdevsw; 289 args.mda_unit = periph->unit_number; 290 args.mda_uid = UID_ROOT; 291 args.mda_gid = GID_OPERATOR; 292 args.mda_mode = 0600; 293 args.mda_si_drv1 = periph; 294 error = make_dev_s(&args, &softc->dev, "%s%d", periph->periph_name, 295 periph->unit_number); 296 if (error != 0) { 297 cam_periph_lock(periph); 298 return (CAM_REQ_CMP_ERR); 299 } 300 301 softc->device_stats = devstat_new_entry("pt", 302 periph->unit_number, 0, 303 DEVSTAT_NO_BLOCKSIZE, 304 SID_TYPE(&cgd->inq_data) | 305 XPORT_DEVSTAT_TYPE(cpi.transport), 306 DEVSTAT_PRIORITY_OTHER); 307 308 cam_periph_lock(periph); 309 310 /* 311 * Add async callbacks for bus reset and 312 * bus device reset calls. I don't bother 313 * checking if this fails as, in most cases, 314 * the system will function just fine without 315 * them and the only alternative would be to 316 * not attach the device on failure. 317 */ 318 xpt_register_async(AC_SENT_BDR | AC_BUS_RESET | AC_LOST_DEVICE, 319 ptasync, periph, periph->path); 320 321 /* Tell the user we've attached to the device */ 322 xpt_announce_periph(periph, NULL); 323 324 return(CAM_REQ_CMP); 325 } 326 327 static void 328 ptoninvalidate(struct cam_periph *periph) 329 { 330 struct pt_softc *softc; 331 332 softc = (struct pt_softc *)periph->softc; 333 334 /* 335 * De-register any async callbacks. 336 */ 337 xpt_register_async(0, ptasync, periph, periph->path); 338 339 softc->flags |= PT_FLAG_DEVICE_INVALID; 340 341 /* 342 * Return all queued I/O with ENXIO. 343 * XXX Handle any transactions queued to the card 344 * with XPT_ABORT_CCB. 345 */ 346 bioq_flush(&softc->bio_queue, NULL, ENXIO); 347 } 348 349 static void 350 ptdtor(struct cam_periph *periph) 351 { 352 struct pt_softc *softc; 353 354 softc = (struct pt_softc *)periph->softc; 355 356 devstat_remove_entry(softc->device_stats); 357 cam_periph_unlock(periph); 358 destroy_dev(softc->dev); 359 cam_periph_lock(periph); 360 free(softc, M_DEVBUF); 361 } 362 363 static void 364 ptasync(void *callback_arg, u_int32_t code, struct cam_path *path, void *arg) 365 { 366 struct cam_periph *periph; 367 368 periph = (struct cam_periph *)callback_arg; 369 switch (code) { 370 case AC_FOUND_DEVICE: 371 { 372 struct ccb_getdev *cgd; 373 cam_status status; 374 375 cgd = (struct ccb_getdev *)arg; 376 if (cgd == NULL) 377 break; 378 379 if (cgd->protocol != PROTO_SCSI) 380 break; 381 if (SID_QUAL(&cgd->inq_data) != SID_QUAL_LU_CONNECTED) 382 break; 383 if (SID_TYPE(&cgd->inq_data) != T_PROCESSOR) 384 break; 385 386 /* 387 * Allocate a peripheral instance for 388 * this device and start the probe 389 * process. 390 */ 391 status = cam_periph_alloc(ptctor, ptoninvalidate, ptdtor, 392 ptstart, "pt", CAM_PERIPH_BIO, 393 path, ptasync, 394 AC_FOUND_DEVICE, cgd); 395 396 if (status != CAM_REQ_CMP 397 && status != CAM_REQ_INPROG) 398 printf("ptasync: Unable to attach to new device " 399 "due to status 0x%x\n", status); 400 break; 401 } 402 case AC_SENT_BDR: 403 case AC_BUS_RESET: 404 { 405 struct pt_softc *softc; 406 struct ccb_hdr *ccbh; 407 408 softc = (struct pt_softc *)periph->softc; 409 /* 410 * Don't fail on the expected unit attention 411 * that will occur. 412 */ 413 softc->flags |= PT_FLAG_RETRY_UA; 414 LIST_FOREACH(ccbh, &softc->pending_ccbs, periph_links.le) 415 ccbh->ccb_state |= PT_CCB_RETRY_UA; 416 } 417 /* FALLTHROUGH */ 418 default: 419 cam_periph_async(periph, code, path, arg); 420 break; 421 } 422 } 423 424 static void 425 ptstart(struct cam_periph *periph, union ccb *start_ccb) 426 { 427 struct pt_softc *softc; 428 struct bio *bp; 429 430 softc = (struct pt_softc *)periph->softc; 431 432 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("ptstart\n")); 433 434 /* 435 * See if there is a buf with work for us to do.. 436 */ 437 bp = bioq_first(&softc->bio_queue); 438 if (bp == NULL) { 439 xpt_release_ccb(start_ccb); 440 } else { 441 bioq_remove(&softc->bio_queue, bp); 442 443 devstat_start_transaction_bio(softc->device_stats, bp); 444 445 scsi_send_receive(&start_ccb->csio, 446 /*retries*/4, 447 ptdone, 448 MSG_SIMPLE_Q_TAG, 449 bp->bio_cmd == BIO_READ, 450 /*byte2*/0, 451 bp->bio_bcount, 452 bp->bio_data, 453 /*sense_len*/SSD_FULL_SIZE, 454 /*timeout*/softc->io_timeout); 455 456 start_ccb->ccb_h.ccb_state = PT_CCB_BUFFER_IO_UA; 457 458 /* 459 * Block out any asynchronous callbacks 460 * while we touch the pending ccb list. 461 */ 462 LIST_INSERT_HEAD(&softc->pending_ccbs, &start_ccb->ccb_h, 463 periph_links.le); 464 465 start_ccb->ccb_h.ccb_bp = bp; 466 bp = bioq_first(&softc->bio_queue); 467 468 xpt_action(start_ccb); 469 470 if (bp != NULL) { 471 /* Have more work to do, so ensure we stay scheduled */ 472 xpt_schedule(periph, CAM_PRIORITY_NORMAL); 473 } 474 } 475 } 476 477 static void 478 ptdone(struct cam_periph *periph, union ccb *done_ccb) 479 { 480 struct pt_softc *softc; 481 struct ccb_scsiio *csio; 482 483 softc = (struct pt_softc *)periph->softc; 484 485 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("ptdone\n")); 486 487 csio = &done_ccb->csio; 488 switch (csio->ccb_h.ccb_state) { 489 case PT_CCB_BUFFER_IO: 490 case PT_CCB_BUFFER_IO_UA: 491 { 492 struct bio *bp; 493 494 bp = (struct bio *)done_ccb->ccb_h.ccb_bp; 495 if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) { 496 int error; 497 int sf; 498 499 if ((csio->ccb_h.ccb_state & PT_CCB_RETRY_UA) != 0) 500 sf = SF_RETRY_UA; 501 else 502 sf = 0; 503 504 error = pterror(done_ccb, CAM_RETRY_SELTO, sf); 505 if (error == ERESTART) { 506 /* 507 * A retry was scheuled, so 508 * just return. 509 */ 510 return; 511 } 512 if (error != 0) { 513 if (error == ENXIO) { 514 /* 515 * Catastrophic error. Mark our device 516 * as invalid. 517 */ 518 xpt_print(periph->path, 519 "Invalidating device\n"); 520 softc->flags |= PT_FLAG_DEVICE_INVALID; 521 } 522 523 /* 524 * return all queued I/O with EIO, so that 525 * the client can retry these I/Os in the 526 * proper order should it attempt to recover. 527 */ 528 bioq_flush(&softc->bio_queue, NULL, EIO); 529 bp->bio_error = error; 530 bp->bio_resid = bp->bio_bcount; 531 bp->bio_flags |= BIO_ERROR; 532 } else { 533 bp->bio_resid = csio->resid; 534 bp->bio_error = 0; 535 if (bp->bio_resid != 0) { 536 /* Short transfer ??? */ 537 bp->bio_flags |= BIO_ERROR; 538 } 539 } 540 if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) 541 cam_release_devq(done_ccb->ccb_h.path, 542 /*relsim_flags*/0, 543 /*reduction*/0, 544 /*timeout*/0, 545 /*getcount_only*/0); 546 } else { 547 bp->bio_resid = csio->resid; 548 if (bp->bio_resid != 0) 549 bp->bio_flags |= BIO_ERROR; 550 } 551 552 /* 553 * Block out any asynchronous callbacks 554 * while we touch the pending ccb list. 555 */ 556 LIST_REMOVE(&done_ccb->ccb_h, periph_links.le); 557 558 biofinish(bp, softc->device_stats, 0); 559 break; 560 } 561 } 562 xpt_release_ccb(done_ccb); 563 } 564 565 static int 566 pterror(union ccb *ccb, u_int32_t cam_flags, u_int32_t sense_flags) 567 { 568 struct pt_softc *softc; 569 struct cam_periph *periph; 570 571 periph = xpt_path_periph(ccb->ccb_h.path); 572 softc = (struct pt_softc *)periph->softc; 573 574 return(cam_periph_error(ccb, cam_flags, sense_flags, 575 &softc->saved_ccb)); 576 } 577 578 static int 579 ptioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag, struct thread *td) 580 { 581 struct cam_periph *periph; 582 struct pt_softc *softc; 583 int error = 0; 584 585 periph = (struct cam_periph *)dev->si_drv1; 586 softc = (struct pt_softc *)periph->softc; 587 588 cam_periph_lock(periph); 589 590 switch(cmd) { 591 case PTIOCGETTIMEOUT: 592 if (softc->io_timeout >= 1000) 593 *(int *)addr = softc->io_timeout / 1000; 594 else 595 *(int *)addr = 0; 596 break; 597 case PTIOCSETTIMEOUT: 598 if (*(int *)addr < 1) { 599 error = EINVAL; 600 break; 601 } 602 603 softc->io_timeout = *(int *)addr * 1000; 604 605 break; 606 default: 607 error = cam_periph_ioctl(periph, cmd, addr, pterror); 608 break; 609 } 610 611 cam_periph_unlock(periph); 612 613 return(error); 614 } 615 616 void 617 scsi_send_receive(struct ccb_scsiio *csio, u_int32_t retries, 618 void (*cbfcnp)(struct cam_periph *, union ccb *), 619 u_int tag_action, int readop, u_int byte2, 620 u_int32_t xfer_len, u_int8_t *data_ptr, u_int8_t sense_len, 621 u_int32_t timeout) 622 { 623 struct scsi_send_receive *scsi_cmd; 624 625 scsi_cmd = (struct scsi_send_receive *)&csio->cdb_io.cdb_bytes; 626 scsi_cmd->opcode = readop ? RECEIVE : SEND; 627 scsi_cmd->byte2 = byte2; 628 scsi_ulto3b(xfer_len, scsi_cmd->xfer_len); 629 scsi_cmd->control = 0; 630 631 cam_fill_csio(csio, 632 retries, 633 cbfcnp, 634 /*flags*/readop ? CAM_DIR_IN : CAM_DIR_OUT, 635 tag_action, 636 data_ptr, 637 xfer_len, 638 sense_len, 639 sizeof(*scsi_cmd), 640 timeout); 641 } 642