1 /*- 2 * Copyright (c) 1997 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 28 /* 29 * Derived from the NetBSD SCSI changer driver. 30 * 31 * $NetBSD: ch.c,v 1.32 1998/01/12 09:49:12 thorpej Exp $ 32 * 33 */ 34 /*- 35 * Copyright (c) 1996, 1997 Jason R. Thorpe <thorpej@and.com> 36 * All rights reserved. 37 * 38 * Partially based on an autochanger driver written by Stefan Grefen 39 * and on an autochanger driver written by the Systems Programming Group 40 * at the University of Utah Computer Science Department. 41 * 42 * Redistribution and use in source and binary forms, with or without 43 * modification, are permitted provided that the following conditions 44 * are met: 45 * 1. Redistributions of source code must retain the above copyright 46 * notice, this list of conditions and the following disclaimer. 47 * 2. Redistributions in binary form must reproduce the above copyright 48 * notice, this list of conditions and the following disclaimer in the 49 * documentation and/or other materials provided with the distribution. 50 * 3. All advertising materials mentioning features or use of this software 51 * must display the following acknowledgements: 52 * This product includes software developed by Jason R. Thorpe 53 * for And Communications, http://www.and.com/ 54 * 4. The name of the author may not be used to endorse or promote products 55 * derived from this software without specific prior written permission. 56 * 57 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 58 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 59 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 60 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 61 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 62 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 63 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 64 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 65 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 66 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 67 * SUCH DAMAGE. 68 */ 69 70 #include <sys/cdefs.h> 71 __FBSDID("$FreeBSD$"); 72 73 #include <sys/param.h> 74 #include <sys/queue.h> 75 #include <sys/systm.h> 76 #include <sys/kernel.h> 77 #include <sys/types.h> 78 #include <sys/malloc.h> 79 #include <sys/fcntl.h> 80 #include <sys/conf.h> 81 #include <sys/chio.h> 82 #include <sys/errno.h> 83 #include <sys/devicestat.h> 84 85 #include <cam/cam.h> 86 #include <cam/cam_ccb.h> 87 #include <cam/cam_periph.h> 88 #include <cam/cam_xpt_periph.h> 89 #include <cam/cam_debug.h> 90 91 #include <cam/scsi/scsi_all.h> 92 #include <cam/scsi/scsi_message.h> 93 #include <cam/scsi/scsi_ch.h> 94 95 /* 96 * Timeout definitions for various changer related commands. They may 97 * be too short for some devices (especially the timeout for INITIALIZE 98 * ELEMENT STATUS). 99 */ 100 101 static const u_int32_t CH_TIMEOUT_MODE_SENSE = 6000; 102 static const u_int32_t CH_TIMEOUT_MOVE_MEDIUM = 15 * 60 * 1000; 103 static const u_int32_t CH_TIMEOUT_EXCHANGE_MEDIUM = 15 * 60 * 1000; 104 static const u_int32_t CH_TIMEOUT_POSITION_TO_ELEMENT = 15 * 60 * 1000; 105 static const u_int32_t CH_TIMEOUT_READ_ELEMENT_STATUS = 5 * 60 * 1000; 106 static const u_int32_t CH_TIMEOUT_SEND_VOLTAG = 10000; 107 static const u_int32_t CH_TIMEOUT_INITIALIZE_ELEMENT_STATUS = 500000; 108 109 typedef enum { 110 CH_FLAG_INVALID = 0x001 111 } ch_flags; 112 113 typedef enum { 114 CH_STATE_PROBE, 115 CH_STATE_NORMAL 116 } ch_state; 117 118 typedef enum { 119 CH_CCB_PROBE 120 } ch_ccb_types; 121 122 typedef enum { 123 CH_Q_NONE = 0x00, 124 CH_Q_NO_DBD = 0x01, 125 CH_Q_NO_DVCID = 0x02 126 } ch_quirks; 127 128 #define CH_Q_BIT_STRING \ 129 "\020" \ 130 "\001NO_DBD" \ 131 "\002NO_DVCID" 132 133 #define ccb_state ppriv_field0 134 #define ccb_bp ppriv_ptr1 135 136 struct scsi_mode_sense_data { 137 struct scsi_mode_header_6 header; 138 struct scsi_mode_blk_desc blk_desc; 139 union { 140 struct page_element_address_assignment ea; 141 struct page_transport_geometry_parameters tg; 142 struct page_device_capabilities cap; 143 } pages; 144 }; 145 146 struct ch_softc { 147 ch_flags flags; 148 ch_state state; 149 ch_quirks quirks; 150 union ccb saved_ccb; 151 struct devstat *device_stats; 152 struct cdev *dev; 153 int open_count; 154 155 int sc_picker; /* current picker */ 156 157 /* 158 * The following information is obtained from the 159 * element address assignment page. 160 */ 161 int sc_firsts[CHET_MAX + 1]; /* firsts */ 162 int sc_counts[CHET_MAX + 1]; /* counts */ 163 164 /* 165 * The following mask defines the legal combinations 166 * of elements for the MOVE MEDIUM command. 167 */ 168 u_int8_t sc_movemask[CHET_MAX + 1]; 169 170 /* 171 * As above, but for EXCHANGE MEDIUM. 172 */ 173 u_int8_t sc_exchangemask[CHET_MAX + 1]; 174 175 /* 176 * Quirks; see below. XXX KDM not implemented yet 177 */ 178 int sc_settledelay; /* delay for settle */ 179 }; 180 181 static d_open_t chopen; 182 static d_close_t chclose; 183 static d_ioctl_t chioctl; 184 static periph_init_t chinit; 185 static periph_ctor_t chregister; 186 static periph_oninv_t choninvalidate; 187 static periph_dtor_t chcleanup; 188 static periph_start_t chstart; 189 static void chasync(void *callback_arg, u_int32_t code, 190 struct cam_path *path, void *arg); 191 static void chdone(struct cam_periph *periph, 192 union ccb *done_ccb); 193 static int cherror(union ccb *ccb, u_int32_t cam_flags, 194 u_int32_t sense_flags); 195 static int chmove(struct cam_periph *periph, 196 struct changer_move *cm); 197 static int chexchange(struct cam_periph *periph, 198 struct changer_exchange *ce); 199 static int chposition(struct cam_periph *periph, 200 struct changer_position *cp); 201 static int chgetelemstatus(struct cam_periph *periph, 202 int scsi_version, u_long cmd, 203 struct changer_element_status_request *csr); 204 static int chsetvoltag(struct cam_periph *periph, 205 struct changer_set_voltag_request *csvr); 206 static int chielem(struct cam_periph *periph, 207 unsigned int timeout); 208 static int chgetparams(struct cam_periph *periph); 209 static int chscsiversion(struct cam_periph *periph); 210 211 static struct periph_driver chdriver = 212 { 213 chinit, "ch", 214 TAILQ_HEAD_INITIALIZER(chdriver.units), /* generation */ 0 215 }; 216 217 PERIPHDRIVER_DECLARE(ch, chdriver); 218 219 static struct cdevsw ch_cdevsw = { 220 .d_version = D_VERSION, 221 .d_flags = D_TRACKCLOSE, 222 .d_open = chopen, 223 .d_close = chclose, 224 .d_ioctl = chioctl, 225 .d_name = "ch", 226 }; 227 228 static MALLOC_DEFINE(M_SCSICH, "scsi_ch", "scsi_ch buffers"); 229 230 static void 231 chinit(void) 232 { 233 cam_status status; 234 235 /* 236 * Install a global async callback. This callback will 237 * receive async callbacks like "new device found". 238 */ 239 status = xpt_register_async(AC_FOUND_DEVICE, chasync, NULL, NULL); 240 241 if (status != CAM_REQ_CMP) { 242 printf("ch: Failed to attach master async callback " 243 "due to status 0x%x!\n", status); 244 } 245 } 246 247 static void 248 chdevgonecb(void *arg) 249 { 250 struct ch_softc *softc; 251 struct cam_periph *periph; 252 struct mtx *mtx; 253 int i; 254 255 periph = (struct cam_periph *)arg; 256 mtx = cam_periph_mtx(periph); 257 mtx_lock(mtx); 258 259 softc = (struct ch_softc *)periph->softc; 260 KASSERT(softc->open_count >= 0, ("Negative open count %d", 261 softc->open_count)); 262 263 /* 264 * When we get this callback, we will get no more close calls from 265 * devfs. So if we have any dangling opens, we need to release the 266 * reference held for that particular context. 267 */ 268 for (i = 0; i < softc->open_count; i++) 269 cam_periph_release_locked(periph); 270 271 softc->open_count = 0; 272 273 /* 274 * Release the reference held for the device node, it is gone now. 275 */ 276 cam_periph_release_locked(periph); 277 278 /* 279 * We reference the lock directly here, instead of using 280 * cam_periph_unlock(). The reason is that the final call to 281 * cam_periph_release_locked() above could result in the periph 282 * getting freed. If that is the case, dereferencing the periph 283 * with a cam_periph_unlock() call would cause a page fault. 284 */ 285 mtx_unlock(mtx); 286 } 287 288 static void 289 choninvalidate(struct cam_periph *periph) 290 { 291 struct ch_softc *softc; 292 293 softc = (struct ch_softc *)periph->softc; 294 295 /* 296 * De-register any async callbacks. 297 */ 298 xpt_register_async(0, chasync, periph, periph->path); 299 300 softc->flags |= CH_FLAG_INVALID; 301 302 /* 303 * Tell devfs this device has gone away, and ask for a callback 304 * when it has cleaned up its state. 305 */ 306 destroy_dev_sched_cb(softc->dev, chdevgonecb, periph); 307 } 308 309 static void 310 chcleanup(struct cam_periph *periph) 311 { 312 struct ch_softc *softc; 313 314 softc = (struct ch_softc *)periph->softc; 315 316 devstat_remove_entry(softc->device_stats); 317 318 free(softc, M_DEVBUF); 319 } 320 321 static void 322 chasync(void *callback_arg, u_int32_t code, struct cam_path *path, void *arg) 323 { 324 struct cam_periph *periph; 325 326 periph = (struct cam_periph *)callback_arg; 327 328 switch(code) { 329 case AC_FOUND_DEVICE: 330 { 331 struct ccb_getdev *cgd; 332 cam_status status; 333 334 cgd = (struct ccb_getdev *)arg; 335 if (cgd == NULL) 336 break; 337 338 if (cgd->protocol != PROTO_SCSI) 339 break; 340 341 if (SID_TYPE(&cgd->inq_data)!= T_CHANGER) 342 break; 343 344 /* 345 * Allocate a peripheral instance for 346 * this device and start the probe 347 * process. 348 */ 349 status = cam_periph_alloc(chregister, choninvalidate, 350 chcleanup, chstart, "ch", 351 CAM_PERIPH_BIO, path, 352 chasync, AC_FOUND_DEVICE, cgd); 353 354 if (status != CAM_REQ_CMP 355 && status != CAM_REQ_INPROG) 356 printf("chasync: Unable to probe new device " 357 "due to status 0x%x\n", status); 358 359 break; 360 361 } 362 default: 363 cam_periph_async(periph, code, path, arg); 364 break; 365 } 366 } 367 368 static cam_status 369 chregister(struct cam_periph *periph, void *arg) 370 { 371 struct ch_softc *softc; 372 struct ccb_getdev *cgd; 373 struct ccb_pathinq cpi; 374 375 cgd = (struct ccb_getdev *)arg; 376 if (cgd == NULL) { 377 printf("chregister: no getdev CCB, can't register device\n"); 378 return(CAM_REQ_CMP_ERR); 379 } 380 381 softc = (struct ch_softc *)malloc(sizeof(*softc),M_DEVBUF,M_NOWAIT); 382 383 if (softc == NULL) { 384 printf("chregister: Unable to probe new device. " 385 "Unable to allocate softc\n"); 386 return(CAM_REQ_CMP_ERR); 387 } 388 389 bzero(softc, sizeof(*softc)); 390 softc->state = CH_STATE_PROBE; 391 periph->softc = softc; 392 softc->quirks = CH_Q_NONE; 393 394 /* 395 * The DVCID and CURDATA bits were not introduced until the SMC 396 * spec. If this device claims SCSI-2 or earlier support, then it 397 * very likely does not support these bits. 398 */ 399 if (cgd->inq_data.version <= SCSI_REV_2) 400 softc->quirks |= CH_Q_NO_DVCID; 401 402 bzero(&cpi, sizeof(cpi)); 403 xpt_setup_ccb(&cpi.ccb_h, periph->path, CAM_PRIORITY_NORMAL); 404 cpi.ccb_h.func_code = XPT_PATH_INQ; 405 xpt_action((union ccb *)&cpi); 406 407 /* 408 * Changers don't have a blocksize, and obviously don't support 409 * tagged queueing. 410 */ 411 cam_periph_unlock(periph); 412 softc->device_stats = devstat_new_entry("ch", 413 periph->unit_number, 0, 414 DEVSTAT_NO_BLOCKSIZE | DEVSTAT_NO_ORDERED_TAGS, 415 SID_TYPE(&cgd->inq_data) | 416 XPORT_DEVSTAT_TYPE(cpi.transport), 417 DEVSTAT_PRIORITY_OTHER); 418 419 /* 420 * Acquire a reference to the periph before we create the devfs 421 * instance for it. We'll release this reference once the devfs 422 * instance has been freed. 423 */ 424 if (cam_periph_acquire(periph) != CAM_REQ_CMP) { 425 xpt_print(periph->path, "%s: lost periph during " 426 "registration!\n", __func__); 427 cam_periph_lock(periph); 428 return (CAM_REQ_CMP_ERR); 429 } 430 431 432 /* Register the device */ 433 softc->dev = make_dev(&ch_cdevsw, periph->unit_number, UID_ROOT, 434 GID_OPERATOR, 0600, "%s%d", periph->periph_name, 435 periph->unit_number); 436 cam_periph_lock(periph); 437 softc->dev->si_drv1 = periph; 438 439 /* 440 * Add an async callback so that we get 441 * notified if this device goes away. 442 */ 443 xpt_register_async(AC_LOST_DEVICE, chasync, periph, periph->path); 444 445 /* 446 * Lock this periph until we are setup. 447 * This first call can't block 448 */ 449 (void)cam_periph_hold(periph, PRIBIO); 450 xpt_schedule(periph, CAM_PRIORITY_DEV); 451 452 return(CAM_REQ_CMP); 453 } 454 455 static int 456 chopen(struct cdev *dev, int flags, int fmt, struct thread *td) 457 { 458 struct cam_periph *periph; 459 struct ch_softc *softc; 460 int error; 461 462 periph = (struct cam_periph *)dev->si_drv1; 463 if (cam_periph_acquire(periph) != CAM_REQ_CMP) 464 return (ENXIO); 465 466 softc = (struct ch_softc *)periph->softc; 467 468 cam_periph_lock(periph); 469 470 if (softc->flags & CH_FLAG_INVALID) { 471 cam_periph_release_locked(periph); 472 cam_periph_unlock(periph); 473 return(ENXIO); 474 } 475 476 if ((error = cam_periph_hold(periph, PRIBIO | PCATCH)) != 0) { 477 cam_periph_unlock(periph); 478 cam_periph_release(periph); 479 return (error); 480 } 481 482 /* 483 * Load information about this changer device into the softc. 484 */ 485 if ((error = chgetparams(periph)) != 0) { 486 cam_periph_unhold(periph); 487 cam_periph_release_locked(periph); 488 cam_periph_unlock(periph); 489 return(error); 490 } 491 492 cam_periph_unhold(periph); 493 494 softc->open_count++; 495 496 cam_periph_unlock(periph); 497 498 return(error); 499 } 500 501 static int 502 chclose(struct cdev *dev, int flag, int fmt, struct thread *td) 503 { 504 struct cam_periph *periph; 505 struct ch_softc *softc; 506 struct mtx *mtx; 507 508 periph = (struct cam_periph *)dev->si_drv1; 509 if (periph == NULL) 510 return(ENXIO); 511 mtx = cam_periph_mtx(periph); 512 mtx_lock(mtx); 513 514 softc = (struct ch_softc *)periph->softc; 515 softc->open_count--; 516 517 cam_periph_release_locked(periph); 518 519 /* 520 * We reference the lock directly here, instead of using 521 * cam_periph_unlock(). The reason is that the call to 522 * cam_periph_release_locked() above could result in the periph 523 * getting freed. If that is the case, dereferencing the periph 524 * with a cam_periph_unlock() call would cause a page fault. 525 * 526 * cam_periph_release() avoids this problem using the same method, 527 * but we're manually acquiring and dropping the lock here to 528 * protect the open count and avoid another lock acquisition and 529 * release. 530 */ 531 mtx_unlock(mtx); 532 533 return(0); 534 } 535 536 static void 537 chstart(struct cam_periph *periph, union ccb *start_ccb) 538 { 539 struct ch_softc *softc; 540 541 softc = (struct ch_softc *)periph->softc; 542 543 switch (softc->state) { 544 case CH_STATE_NORMAL: 545 { 546 xpt_release_ccb(start_ccb); 547 break; 548 } 549 case CH_STATE_PROBE: 550 { 551 int mode_buffer_len; 552 void *mode_buffer; 553 554 /* 555 * Include the block descriptor when calculating the mode 556 * buffer length, 557 */ 558 mode_buffer_len = sizeof(struct scsi_mode_header_6) + 559 sizeof(struct scsi_mode_blk_desc) + 560 sizeof(struct page_element_address_assignment); 561 562 mode_buffer = malloc(mode_buffer_len, M_SCSICH, M_NOWAIT); 563 564 if (mode_buffer == NULL) { 565 printf("chstart: couldn't malloc mode sense data\n"); 566 break; 567 } 568 bzero(mode_buffer, mode_buffer_len); 569 570 /* 571 * Get the element address assignment page. 572 */ 573 scsi_mode_sense(&start_ccb->csio, 574 /* retries */ 1, 575 /* cbfcnp */ chdone, 576 /* tag_action */ MSG_SIMPLE_Q_TAG, 577 /* dbd */ (softc->quirks & CH_Q_NO_DBD) ? 578 FALSE : TRUE, 579 /* page_code */ SMS_PAGE_CTRL_CURRENT, 580 /* page */ CH_ELEMENT_ADDR_ASSIGN_PAGE, 581 /* param_buf */ (u_int8_t *)mode_buffer, 582 /* param_len */ mode_buffer_len, 583 /* sense_len */ SSD_FULL_SIZE, 584 /* timeout */ CH_TIMEOUT_MODE_SENSE); 585 586 start_ccb->ccb_h.ccb_bp = NULL; 587 start_ccb->ccb_h.ccb_state = CH_CCB_PROBE; 588 xpt_action(start_ccb); 589 break; 590 } 591 } 592 } 593 594 static void 595 chdone(struct cam_periph *periph, union ccb *done_ccb) 596 { 597 struct ch_softc *softc; 598 struct ccb_scsiio *csio; 599 600 softc = (struct ch_softc *)periph->softc; 601 csio = &done_ccb->csio; 602 603 switch(done_ccb->ccb_h.ccb_state) { 604 case CH_CCB_PROBE: 605 { 606 struct scsi_mode_header_6 *mode_header; 607 struct page_element_address_assignment *ea; 608 char announce_buf[80]; 609 610 611 mode_header = (struct scsi_mode_header_6 *)csio->data_ptr; 612 613 ea = (struct page_element_address_assignment *) 614 find_mode_page_6(mode_header); 615 616 if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP){ 617 618 softc->sc_firsts[CHET_MT] = scsi_2btoul(ea->mtea); 619 softc->sc_counts[CHET_MT] = scsi_2btoul(ea->nmte); 620 softc->sc_firsts[CHET_ST] = scsi_2btoul(ea->fsea); 621 softc->sc_counts[CHET_ST] = scsi_2btoul(ea->nse); 622 softc->sc_firsts[CHET_IE] = scsi_2btoul(ea->fieea); 623 softc->sc_counts[CHET_IE] = scsi_2btoul(ea->niee); 624 softc->sc_firsts[CHET_DT] = scsi_2btoul(ea->fdtea); 625 softc->sc_counts[CHET_DT] = scsi_2btoul(ea->ndte); 626 softc->sc_picker = softc->sc_firsts[CHET_MT]; 627 628 #define PLURAL(c) (c) == 1 ? "" : "s" 629 snprintf(announce_buf, sizeof(announce_buf), 630 "%d slot%s, %d drive%s, " 631 "%d picker%s, %d portal%s", 632 softc->sc_counts[CHET_ST], 633 PLURAL(softc->sc_counts[CHET_ST]), 634 softc->sc_counts[CHET_DT], 635 PLURAL(softc->sc_counts[CHET_DT]), 636 softc->sc_counts[CHET_MT], 637 PLURAL(softc->sc_counts[CHET_MT]), 638 softc->sc_counts[CHET_IE], 639 PLURAL(softc->sc_counts[CHET_IE])); 640 #undef PLURAL 641 } else { 642 int error; 643 644 error = cherror(done_ccb, CAM_RETRY_SELTO, 645 SF_RETRY_UA | SF_NO_PRINT); 646 /* 647 * Retry any UNIT ATTENTION type errors. They 648 * are expected at boot. 649 */ 650 if (error == ERESTART) { 651 /* 652 * A retry was scheuled, so 653 * just return. 654 */ 655 return; 656 } else if (error != 0) { 657 int retry_scheduled; 658 struct scsi_mode_sense_6 *sms; 659 660 sms = (struct scsi_mode_sense_6 *) 661 done_ccb->csio.cdb_io.cdb_bytes; 662 663 /* 664 * Check to see if block descriptors were 665 * disabled. Some devices don't like that. 666 * We're taking advantage of the fact that 667 * the first few bytes of the 6 and 10 byte 668 * mode sense commands are the same. If 669 * block descriptors were disabled, enable 670 * them and re-send the command. 671 */ 672 if (sms->byte2 & SMS_DBD) { 673 sms->byte2 &= ~SMS_DBD; 674 xpt_action(done_ccb); 675 softc->quirks |= CH_Q_NO_DBD; 676 retry_scheduled = 1; 677 } else 678 retry_scheduled = 0; 679 680 /* Don't wedge this device's queue */ 681 if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) 682 cam_release_devq(done_ccb->ccb_h.path, 683 /*relsim_flags*/0, 684 /*reduction*/0, 685 /*timeout*/0, 686 /*getcount_only*/0); 687 688 if (retry_scheduled) 689 return; 690 691 if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) 692 == CAM_SCSI_STATUS_ERROR) 693 scsi_sense_print(&done_ccb->csio); 694 else { 695 xpt_print(periph->path, 696 "got CAM status %#x\n", 697 done_ccb->ccb_h.status); 698 } 699 xpt_print(periph->path, "fatal error, failed " 700 "to attach to device\n"); 701 702 cam_periph_invalidate(periph); 703 704 announce_buf[0] = '\0'; 705 } 706 } 707 if (announce_buf[0] != '\0') { 708 xpt_announce_periph(periph, announce_buf); 709 xpt_announce_quirks(periph, softc->quirks, 710 CH_Q_BIT_STRING); 711 } 712 softc->state = CH_STATE_NORMAL; 713 free(mode_header, M_SCSICH); 714 /* 715 * Since our peripheral may be invalidated by an error 716 * above or an external event, we must release our CCB 717 * before releasing the probe lock on the peripheral. 718 * The peripheral will only go away once the last lock 719 * is removed, and we need it around for the CCB release 720 * operation. 721 */ 722 xpt_release_ccb(done_ccb); 723 cam_periph_unhold(periph); 724 return; 725 } 726 default: 727 break; 728 } 729 xpt_release_ccb(done_ccb); 730 } 731 732 static int 733 cherror(union ccb *ccb, u_int32_t cam_flags, u_int32_t sense_flags) 734 { 735 struct ch_softc *softc; 736 struct cam_periph *periph; 737 738 periph = xpt_path_periph(ccb->ccb_h.path); 739 softc = (struct ch_softc *)periph->softc; 740 741 return (cam_periph_error(ccb, cam_flags, sense_flags, 742 &softc->saved_ccb)); 743 } 744 745 static int 746 chioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag, struct thread *td) 747 { 748 struct cam_periph *periph; 749 struct ch_softc *softc; 750 int error; 751 752 periph = (struct cam_periph *)dev->si_drv1; 753 if (periph == NULL) 754 return(ENXIO); 755 756 cam_periph_lock(periph); 757 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("entering chioctl\n")); 758 759 softc = (struct ch_softc *)periph->softc; 760 761 error = 0; 762 763 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, 764 ("trying to do ioctl %#lx\n", cmd)); 765 766 /* 767 * If this command can change the device's state, we must 768 * have the device open for writing. 769 */ 770 switch (cmd) { 771 case CHIOGPICKER: 772 case CHIOGPARAMS: 773 case OCHIOGSTATUS: 774 case CHIOGSTATUS: 775 break; 776 777 default: 778 if ((flag & FWRITE) == 0) { 779 cam_periph_unlock(periph); 780 return (EBADF); 781 } 782 } 783 784 switch (cmd) { 785 case CHIOMOVE: 786 error = chmove(periph, (struct changer_move *)addr); 787 break; 788 789 case CHIOEXCHANGE: 790 error = chexchange(periph, (struct changer_exchange *)addr); 791 break; 792 793 case CHIOPOSITION: 794 error = chposition(periph, (struct changer_position *)addr); 795 break; 796 797 case CHIOGPICKER: 798 *(int *)addr = softc->sc_picker - softc->sc_firsts[CHET_MT]; 799 break; 800 801 case CHIOSPICKER: 802 { 803 int new_picker = *(int *)addr; 804 805 if (new_picker > (softc->sc_counts[CHET_MT] - 1)) { 806 error = EINVAL; 807 break; 808 } 809 softc->sc_picker = softc->sc_firsts[CHET_MT] + new_picker; 810 break; 811 } 812 case CHIOGPARAMS: 813 { 814 struct changer_params *cp = (struct changer_params *)addr; 815 816 cp->cp_npickers = softc->sc_counts[CHET_MT]; 817 cp->cp_nslots = softc->sc_counts[CHET_ST]; 818 cp->cp_nportals = softc->sc_counts[CHET_IE]; 819 cp->cp_ndrives = softc->sc_counts[CHET_DT]; 820 break; 821 } 822 case CHIOIELEM: 823 error = chielem(periph, *(unsigned int *)addr); 824 break; 825 826 case OCHIOGSTATUS: 827 { 828 error = chgetelemstatus(periph, SCSI_REV_2, cmd, 829 (struct changer_element_status_request *)addr); 830 break; 831 } 832 833 case CHIOGSTATUS: 834 { 835 int scsi_version; 836 837 scsi_version = chscsiversion(periph); 838 if (scsi_version >= SCSI_REV_0) { 839 error = chgetelemstatus(periph, scsi_version, cmd, 840 (struct changer_element_status_request *)addr); 841 } 842 else { /* unable to determine the SCSI version */ 843 cam_periph_unlock(periph); 844 return (ENXIO); 845 } 846 break; 847 } 848 849 case CHIOSETVOLTAG: 850 { 851 error = chsetvoltag(periph, 852 (struct changer_set_voltag_request *) addr); 853 break; 854 } 855 856 /* Implement prevent/allow? */ 857 858 default: 859 error = cam_periph_ioctl(periph, cmd, addr, cherror); 860 break; 861 } 862 863 cam_periph_unlock(periph); 864 return (error); 865 } 866 867 static int 868 chmove(struct cam_periph *periph, struct changer_move *cm) 869 { 870 struct ch_softc *softc; 871 u_int16_t fromelem, toelem; 872 union ccb *ccb; 873 int error; 874 875 error = 0; 876 softc = (struct ch_softc *)periph->softc; 877 878 /* 879 * Check arguments. 880 */ 881 if ((cm->cm_fromtype > CHET_DT) || (cm->cm_totype > CHET_DT)) 882 return (EINVAL); 883 if ((cm->cm_fromunit > (softc->sc_counts[cm->cm_fromtype] - 1)) || 884 (cm->cm_tounit > (softc->sc_counts[cm->cm_totype] - 1))) 885 return (ENODEV); 886 887 /* 888 * Check the request against the changer's capabilities. 889 */ 890 if ((softc->sc_movemask[cm->cm_fromtype] & (1 << cm->cm_totype)) == 0) 891 return (ENODEV); 892 893 /* 894 * Calculate the source and destination elements. 895 */ 896 fromelem = softc->sc_firsts[cm->cm_fromtype] + cm->cm_fromunit; 897 toelem = softc->sc_firsts[cm->cm_totype] + cm->cm_tounit; 898 899 ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL); 900 901 scsi_move_medium(&ccb->csio, 902 /* retries */ 1, 903 /* cbfcnp */ chdone, 904 /* tag_action */ MSG_SIMPLE_Q_TAG, 905 /* tea */ softc->sc_picker, 906 /* src */ fromelem, 907 /* dst */ toelem, 908 /* invert */ (cm->cm_flags & CM_INVERT) ? TRUE : FALSE, 909 /* sense_len */ SSD_FULL_SIZE, 910 /* timeout */ CH_TIMEOUT_MOVE_MEDIUM); 911 912 error = cam_periph_runccb(ccb, cherror, /*cam_flags*/CAM_RETRY_SELTO, 913 /*sense_flags*/ SF_RETRY_UA, 914 softc->device_stats); 915 916 xpt_release_ccb(ccb); 917 918 return(error); 919 } 920 921 static int 922 chexchange(struct cam_periph *periph, struct changer_exchange *ce) 923 { 924 struct ch_softc *softc; 925 u_int16_t src, dst1, dst2; 926 union ccb *ccb; 927 int error; 928 929 error = 0; 930 softc = (struct ch_softc *)periph->softc; 931 /* 932 * Check arguments. 933 */ 934 if ((ce->ce_srctype > CHET_DT) || (ce->ce_fdsttype > CHET_DT) || 935 (ce->ce_sdsttype > CHET_DT)) 936 return (EINVAL); 937 if ((ce->ce_srcunit > (softc->sc_counts[ce->ce_srctype] - 1)) || 938 (ce->ce_fdstunit > (softc->sc_counts[ce->ce_fdsttype] - 1)) || 939 (ce->ce_sdstunit > (softc->sc_counts[ce->ce_sdsttype] - 1))) 940 return (ENODEV); 941 942 /* 943 * Check the request against the changer's capabilities. 944 */ 945 if (((softc->sc_exchangemask[ce->ce_srctype] & 946 (1 << ce->ce_fdsttype)) == 0) || 947 ((softc->sc_exchangemask[ce->ce_fdsttype] & 948 (1 << ce->ce_sdsttype)) == 0)) 949 return (ENODEV); 950 951 /* 952 * Calculate the source and destination elements. 953 */ 954 src = softc->sc_firsts[ce->ce_srctype] + ce->ce_srcunit; 955 dst1 = softc->sc_firsts[ce->ce_fdsttype] + ce->ce_fdstunit; 956 dst2 = softc->sc_firsts[ce->ce_sdsttype] + ce->ce_sdstunit; 957 958 ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL); 959 960 scsi_exchange_medium(&ccb->csio, 961 /* retries */ 1, 962 /* cbfcnp */ chdone, 963 /* tag_action */ MSG_SIMPLE_Q_TAG, 964 /* tea */ softc->sc_picker, 965 /* src */ src, 966 /* dst1 */ dst1, 967 /* dst2 */ dst2, 968 /* invert1 */ (ce->ce_flags & CE_INVERT1) ? 969 TRUE : FALSE, 970 /* invert2 */ (ce->ce_flags & CE_INVERT2) ? 971 TRUE : FALSE, 972 /* sense_len */ SSD_FULL_SIZE, 973 /* timeout */ CH_TIMEOUT_EXCHANGE_MEDIUM); 974 975 error = cam_periph_runccb(ccb, cherror, /*cam_flags*/CAM_RETRY_SELTO, 976 /*sense_flags*/ SF_RETRY_UA, 977 softc->device_stats); 978 979 xpt_release_ccb(ccb); 980 981 return(error); 982 } 983 984 static int 985 chposition(struct cam_periph *periph, struct changer_position *cp) 986 { 987 struct ch_softc *softc; 988 u_int16_t dst; 989 union ccb *ccb; 990 int error; 991 992 error = 0; 993 softc = (struct ch_softc *)periph->softc; 994 995 /* 996 * Check arguments. 997 */ 998 if (cp->cp_type > CHET_DT) 999 return (EINVAL); 1000 if (cp->cp_unit > (softc->sc_counts[cp->cp_type] - 1)) 1001 return (ENODEV); 1002 1003 /* 1004 * Calculate the destination element. 1005 */ 1006 dst = softc->sc_firsts[cp->cp_type] + cp->cp_unit; 1007 1008 ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL); 1009 1010 scsi_position_to_element(&ccb->csio, 1011 /* retries */ 1, 1012 /* cbfcnp */ chdone, 1013 /* tag_action */ MSG_SIMPLE_Q_TAG, 1014 /* tea */ softc->sc_picker, 1015 /* dst */ dst, 1016 /* invert */ (cp->cp_flags & CP_INVERT) ? 1017 TRUE : FALSE, 1018 /* sense_len */ SSD_FULL_SIZE, 1019 /* timeout */ CH_TIMEOUT_POSITION_TO_ELEMENT); 1020 1021 error = cam_periph_runccb(ccb, cherror, /*cam_flags*/ CAM_RETRY_SELTO, 1022 /*sense_flags*/ SF_RETRY_UA, 1023 softc->device_stats); 1024 1025 xpt_release_ccb(ccb); 1026 1027 return(error); 1028 } 1029 1030 /* 1031 * Copy a volume tag to a volume_tag struct, converting SCSI byte order 1032 * to host native byte order in the volume serial number. The volume 1033 * label as returned by the changer is transferred to user mode as 1034 * nul-terminated string. Volume labels are truncated at the first 1035 * space, as suggested by SCSI-2. 1036 */ 1037 static void 1038 copy_voltag(struct changer_voltag *uvoltag, struct volume_tag *voltag) 1039 { 1040 int i; 1041 for (i=0; i<CH_VOLTAG_MAXLEN; i++) { 1042 char c = voltag->vif[i]; 1043 if (c && c != ' ') 1044 uvoltag->cv_volid[i] = c; 1045 else 1046 break; 1047 } 1048 uvoltag->cv_serial = scsi_2btoul(voltag->vsn); 1049 } 1050 1051 /* 1052 * Copy an element status descriptor to a user-mode 1053 * changer_element_status structure. 1054 */ 1055 static void 1056 copy_element_status(struct ch_softc *softc, 1057 u_int16_t flags, 1058 struct read_element_status_descriptor *desc, 1059 struct changer_element_status *ces, 1060 int scsi_version) 1061 { 1062 u_int16_t eaddr = scsi_2btoul(desc->eaddr); 1063 u_int16_t et; 1064 struct volume_tag *pvol_tag = NULL, *avol_tag = NULL; 1065 struct read_element_status_device_id *devid = NULL; 1066 1067 ces->ces_int_addr = eaddr; 1068 /* set up logical address in element status */ 1069 for (et = CHET_MT; et <= CHET_DT; et++) { 1070 if ((softc->sc_firsts[et] <= eaddr) 1071 && ((softc->sc_firsts[et] + softc->sc_counts[et]) 1072 > eaddr)) { 1073 ces->ces_addr = eaddr - softc->sc_firsts[et]; 1074 ces->ces_type = et; 1075 break; 1076 } 1077 } 1078 1079 ces->ces_flags = desc->flags1; 1080 1081 ces->ces_sensecode = desc->sense_code; 1082 ces->ces_sensequal = desc->sense_qual; 1083 1084 if (desc->flags2 & READ_ELEMENT_STATUS_INVERT) 1085 ces->ces_flags |= CES_INVERT; 1086 1087 if (desc->flags2 & READ_ELEMENT_STATUS_SVALID) { 1088 1089 eaddr = scsi_2btoul(desc->ssea); 1090 1091 /* convert source address to logical format */ 1092 for (et = CHET_MT; et <= CHET_DT; et++) { 1093 if ((softc->sc_firsts[et] <= eaddr) 1094 && ((softc->sc_firsts[et] + softc->sc_counts[et]) 1095 > eaddr)) { 1096 ces->ces_source_addr = 1097 eaddr - softc->sc_firsts[et]; 1098 ces->ces_source_type = et; 1099 ces->ces_flags |= CES_SOURCE_VALID; 1100 break; 1101 } 1102 } 1103 1104 if (!(ces->ces_flags & CES_SOURCE_VALID)) 1105 printf("ch: warning: could not map element source " 1106 "address %ud to a valid element type\n", 1107 eaddr); 1108 } 1109 1110 /* 1111 * pvoltag and avoltag are common between SCSI-2 and later versions 1112 */ 1113 if (flags & READ_ELEMENT_STATUS_PVOLTAG) 1114 pvol_tag = &desc->voltag_devid.pvoltag; 1115 if (flags & READ_ELEMENT_STATUS_AVOLTAG) 1116 avol_tag = (flags & READ_ELEMENT_STATUS_PVOLTAG) ? 1117 &desc->voltag_devid.voltag[1] :&desc->voltag_devid.pvoltag; 1118 /* 1119 * For SCSI-3 and later, element status can carry designator and 1120 * other information. 1121 */ 1122 if (scsi_version >= SCSI_REV_SPC) { 1123 if ((flags & READ_ELEMENT_STATUS_PVOLTAG) ^ 1124 (flags & READ_ELEMENT_STATUS_AVOLTAG)) 1125 devid = &desc->voltag_devid.pvol_and_devid.devid; 1126 else if (!(flags & READ_ELEMENT_STATUS_PVOLTAG) && 1127 !(flags & READ_ELEMENT_STATUS_AVOLTAG)) 1128 devid = &desc->voltag_devid.devid; 1129 else /* Have both PVOLTAG and AVOLTAG */ 1130 devid = &desc->voltag_devid.vol_tags_and_devid.devid; 1131 } 1132 1133 if (pvol_tag) 1134 copy_voltag(&(ces->ces_pvoltag), pvol_tag); 1135 if (avol_tag) 1136 copy_voltag(&(ces->ces_pvoltag), avol_tag); 1137 if (devid != NULL) { 1138 if (devid->designator_length > 0) { 1139 bcopy((void *)devid->designator, 1140 (void *)ces->ces_designator, 1141 devid->designator_length); 1142 ces->ces_designator_length = devid->designator_length; 1143 /* 1144 * Make sure we are always NUL terminated. The 1145 * This won't matter for the binary code set, 1146 * since the user will only pay attention to the 1147 * length field. 1148 */ 1149 ces->ces_designator[devid->designator_length]= '\0'; 1150 } 1151 if (devid->piv_assoc_designator_type & 1152 READ_ELEMENT_STATUS_PIV_SET) { 1153 ces->ces_flags |= CES_PIV; 1154 ces->ces_protocol_id = 1155 READ_ELEMENT_STATUS_PROTOCOL_ID( 1156 devid->prot_code_set); 1157 } 1158 ces->ces_code_set = 1159 READ_ELEMENT_STATUS_CODE_SET(devid->prot_code_set); 1160 ces->ces_assoc = READ_ELEMENT_STATUS_ASSOCIATION( 1161 devid->piv_assoc_designator_type); 1162 ces->ces_designator_type = READ_ELEMENT_STATUS_DESIGNATOR_TYPE( 1163 devid->piv_assoc_designator_type); 1164 } else if (scsi_version > SCSI_REV_2) { 1165 /* SCSI-SPC and No devid, no designator */ 1166 ces->ces_designator_length = 0; 1167 ces->ces_designator[0] = '\0'; 1168 ces->ces_protocol_id = CES_PROTOCOL_ID_FCP_4; 1169 } 1170 1171 if (scsi_version <= SCSI_REV_2) { 1172 if (desc->dt_or_obsolete.scsi_2.dt_scsi_flags & 1173 READ_ELEMENT_STATUS_DT_IDVALID) { 1174 ces->ces_flags |= CES_SCSIID_VALID; 1175 ces->ces_scsi_id = 1176 desc->dt_or_obsolete.scsi_2.dt_scsi_addr; 1177 } 1178 1179 if (desc->dt_or_obsolete.scsi_2.dt_scsi_addr & 1180 READ_ELEMENT_STATUS_DT_LUVALID) { 1181 ces->ces_flags |= CES_LUN_VALID; 1182 ces->ces_scsi_lun = 1183 desc->dt_or_obsolete.scsi_2.dt_scsi_flags & 1184 READ_ELEMENT_STATUS_DT_LUNMASK; 1185 } 1186 } 1187 } 1188 1189 static int 1190 chgetelemstatus(struct cam_periph *periph, int scsi_version, u_long cmd, 1191 struct changer_element_status_request *cesr) 1192 { 1193 struct read_element_status_header *st_hdr; 1194 struct read_element_status_page_header *pg_hdr; 1195 struct read_element_status_descriptor *desc; 1196 caddr_t data = NULL; 1197 size_t size, desclen; 1198 int avail, i, error = 0; 1199 int curdata, dvcid, sense_flags; 1200 int try_no_dvcid = 0; 1201 struct changer_element_status *user_data = NULL; 1202 struct ch_softc *softc; 1203 union ccb *ccb; 1204 int chet = cesr->cesr_element_type; 1205 int want_voltags = (cesr->cesr_flags & CESR_VOLTAGS) ? 1 : 0; 1206 1207 softc = (struct ch_softc *)periph->softc; 1208 1209 /* perform argument checking */ 1210 1211 /* 1212 * Perform a range check on the cesr_element_{base,count} 1213 * request argument fields. 1214 */ 1215 if ((softc->sc_counts[chet] - cesr->cesr_element_base) <= 0 1216 || (cesr->cesr_element_base + cesr->cesr_element_count) 1217 > softc->sc_counts[chet]) 1218 return (EINVAL); 1219 1220 /* 1221 * Request one descriptor for the given element type. This 1222 * is used to determine the size of the descriptor so that 1223 * we can allocate enough storage for all of them. We assume 1224 * that the first one can fit into 1k. 1225 */ 1226 cam_periph_unlock(periph); 1227 data = (caddr_t)malloc(1024, M_DEVBUF, M_WAITOK); 1228 1229 cam_periph_lock(periph); 1230 ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL); 1231 1232 sense_flags = SF_RETRY_UA; 1233 if (softc->quirks & CH_Q_NO_DVCID) { 1234 dvcid = 0; 1235 curdata = 0; 1236 } else { 1237 dvcid = 1; 1238 curdata = 1; 1239 /* 1240 * Don't print anything for an Illegal Request, because 1241 * these flags can cause some changers to complain. We'll 1242 * retry without them if we get an error. 1243 */ 1244 sense_flags |= SF_QUIET_IR; 1245 } 1246 1247 retry_einval: 1248 1249 scsi_read_element_status(&ccb->csio, 1250 /* retries */ 1, 1251 /* cbfcnp */ chdone, 1252 /* tag_action */ MSG_SIMPLE_Q_TAG, 1253 /* voltag */ want_voltags, 1254 /* sea */ softc->sc_firsts[chet], 1255 /* curdata */ curdata, 1256 /* dvcid */ dvcid, 1257 /* count */ 1, 1258 /* data_ptr */ data, 1259 /* dxfer_len */ 1024, 1260 /* sense_len */ SSD_FULL_SIZE, 1261 /* timeout */ CH_TIMEOUT_READ_ELEMENT_STATUS); 1262 1263 error = cam_periph_runccb(ccb, cherror, /*cam_flags*/ CAM_RETRY_SELTO, 1264 /*sense_flags*/ sense_flags, 1265 softc->device_stats); 1266 1267 /* 1268 * An Illegal Request sense key (only used if there is no asc/ascq) 1269 * or 0x24,0x00 for an ASC/ASCQ both map to EINVAL. If dvcid or 1270 * curdata are set (we set both or neither), try turning them off 1271 * and see if the command is successful. 1272 */ 1273 if ((error == EINVAL) 1274 && (dvcid || curdata)) { 1275 dvcid = 0; 1276 curdata = 0; 1277 error = 0; 1278 /* At this point we want to report any Illegal Request */ 1279 sense_flags &= ~SF_QUIET_IR; 1280 try_no_dvcid = 1; 1281 goto retry_einval; 1282 } 1283 1284 /* 1285 * In this case, we tried a read element status with dvcid and 1286 * curdata set, and it failed. We retried without those bits, and 1287 * it succeeded. Suggest to the user that he set a quirk, so we 1288 * don't go through the retry process the first time in the future. 1289 * This should only happen on changers that claim SCSI-3 or higher, 1290 * but don't support these bits. 1291 */ 1292 if ((try_no_dvcid != 0) 1293 && (error == 0)) 1294 softc->quirks |= CH_Q_NO_DVCID; 1295 1296 if (error) 1297 goto done; 1298 cam_periph_unlock(periph); 1299 1300 st_hdr = (struct read_element_status_header *)data; 1301 pg_hdr = (struct read_element_status_page_header *)((uintptr_t)st_hdr + 1302 sizeof(struct read_element_status_header)); 1303 desclen = scsi_2btoul(pg_hdr->edl); 1304 1305 size = sizeof(struct read_element_status_header) + 1306 sizeof(struct read_element_status_page_header) + 1307 (desclen * cesr->cesr_element_count); 1308 /* 1309 * Reallocate storage for descriptors and get them from the 1310 * device. 1311 */ 1312 free(data, M_DEVBUF); 1313 data = (caddr_t)malloc(size, M_DEVBUF, M_WAITOK); 1314 1315 cam_periph_lock(periph); 1316 scsi_read_element_status(&ccb->csio, 1317 /* retries */ 1, 1318 /* cbfcnp */ chdone, 1319 /* tag_action */ MSG_SIMPLE_Q_TAG, 1320 /* voltag */ want_voltags, 1321 /* sea */ softc->sc_firsts[chet] 1322 + cesr->cesr_element_base, 1323 /* curdata */ curdata, 1324 /* dvcid */ dvcid, 1325 /* count */ cesr->cesr_element_count, 1326 /* data_ptr */ data, 1327 /* dxfer_len */ size, 1328 /* sense_len */ SSD_FULL_SIZE, 1329 /* timeout */ CH_TIMEOUT_READ_ELEMENT_STATUS); 1330 1331 error = cam_periph_runccb(ccb, cherror, /*cam_flags*/ CAM_RETRY_SELTO, 1332 /*sense_flags*/ SF_RETRY_UA, 1333 softc->device_stats); 1334 1335 if (error) 1336 goto done; 1337 cam_periph_unlock(periph); 1338 1339 /* 1340 * Fill in the user status array. 1341 */ 1342 st_hdr = (struct read_element_status_header *)data; 1343 pg_hdr = (struct read_element_status_page_header *)((uintptr_t)st_hdr + 1344 sizeof(struct read_element_status_header)); 1345 avail = scsi_2btoul(st_hdr->count); 1346 1347 if (avail != cesr->cesr_element_count) { 1348 xpt_print(periph->path, 1349 "warning, READ ELEMENT STATUS avail != count\n"); 1350 } 1351 1352 user_data = (struct changer_element_status *) 1353 malloc(avail * sizeof(struct changer_element_status), 1354 M_DEVBUF, M_WAITOK | M_ZERO); 1355 1356 desc = (struct read_element_status_descriptor *)((uintptr_t)data + 1357 sizeof(struct read_element_status_header) + 1358 sizeof(struct read_element_status_page_header)); 1359 /* 1360 * Set up the individual element status structures 1361 */ 1362 for (i = 0; i < avail; ++i) { 1363 struct changer_element_status *ces; 1364 1365 /* 1366 * In the changer_element_status structure, fields from 1367 * the beginning to the field of ces_scsi_lun are common 1368 * between SCSI-2 and SCSI-3, while all the rest are new 1369 * from SCSI-3. In order to maintain backward compatibility 1370 * of the chio command, the ces pointer, below, is computed 1371 * such that it lines up with the structure boundary 1372 * corresponding to the SCSI version. 1373 */ 1374 ces = cmd == OCHIOGSTATUS ? 1375 (struct changer_element_status *) 1376 ((unsigned char *)user_data + i * 1377 (offsetof(struct changer_element_status,ces_scsi_lun)+1)): 1378 &user_data[i]; 1379 1380 copy_element_status(softc, pg_hdr->flags, desc, 1381 ces, scsi_version); 1382 1383 desc = (struct read_element_status_descriptor *) 1384 ((unsigned char *)desc + desclen); 1385 } 1386 1387 /* Copy element status structures out to userspace. */ 1388 if (cmd == OCHIOGSTATUS) 1389 error = copyout(user_data, 1390 cesr->cesr_element_status, 1391 avail* (offsetof(struct changer_element_status, 1392 ces_scsi_lun) + 1)); 1393 else 1394 error = copyout(user_data, 1395 cesr->cesr_element_status, 1396 avail * sizeof(struct changer_element_status)); 1397 1398 cam_periph_lock(periph); 1399 1400 done: 1401 xpt_release_ccb(ccb); 1402 1403 if (data != NULL) 1404 free(data, M_DEVBUF); 1405 if (user_data != NULL) 1406 free(user_data, M_DEVBUF); 1407 1408 return (error); 1409 } 1410 1411 static int 1412 chielem(struct cam_periph *periph, 1413 unsigned int timeout) 1414 { 1415 union ccb *ccb; 1416 struct ch_softc *softc; 1417 int error; 1418 1419 if (!timeout) { 1420 timeout = CH_TIMEOUT_INITIALIZE_ELEMENT_STATUS; 1421 } else { 1422 timeout *= 1000; 1423 } 1424 1425 error = 0; 1426 softc = (struct ch_softc *)periph->softc; 1427 1428 ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL); 1429 1430 scsi_initialize_element_status(&ccb->csio, 1431 /* retries */ 1, 1432 /* cbfcnp */ chdone, 1433 /* tag_action */ MSG_SIMPLE_Q_TAG, 1434 /* sense_len */ SSD_FULL_SIZE, 1435 /* timeout */ timeout); 1436 1437 error = cam_periph_runccb(ccb, cherror, /*cam_flags*/ CAM_RETRY_SELTO, 1438 /*sense_flags*/ SF_RETRY_UA, 1439 softc->device_stats); 1440 1441 xpt_release_ccb(ccb); 1442 1443 return(error); 1444 } 1445 1446 static int 1447 chsetvoltag(struct cam_periph *periph, 1448 struct changer_set_voltag_request *csvr) 1449 { 1450 union ccb *ccb; 1451 struct ch_softc *softc; 1452 u_int16_t ea; 1453 u_int8_t sac; 1454 struct scsi_send_volume_tag_parameters ssvtp; 1455 int error; 1456 int i; 1457 1458 error = 0; 1459 softc = (struct ch_softc *)periph->softc; 1460 1461 bzero(&ssvtp, sizeof(ssvtp)); 1462 for (i=0; i<sizeof(ssvtp.vitf); i++) { 1463 ssvtp.vitf[i] = ' '; 1464 } 1465 1466 /* 1467 * Check arguments. 1468 */ 1469 if (csvr->csvr_type > CHET_DT) 1470 return EINVAL; 1471 if (csvr->csvr_addr > (softc->sc_counts[csvr->csvr_type] - 1)) 1472 return ENODEV; 1473 1474 ea = softc->sc_firsts[csvr->csvr_type] + csvr->csvr_addr; 1475 1476 if (csvr->csvr_flags & CSVR_ALTERNATE) { 1477 switch (csvr->csvr_flags & CSVR_MODE_MASK) { 1478 case CSVR_MODE_SET: 1479 sac = SEND_VOLUME_TAG_ASSERT_ALTERNATE; 1480 break; 1481 case CSVR_MODE_REPLACE: 1482 sac = SEND_VOLUME_TAG_REPLACE_ALTERNATE; 1483 break; 1484 case CSVR_MODE_CLEAR: 1485 sac = SEND_VOLUME_TAG_UNDEFINED_ALTERNATE; 1486 break; 1487 default: 1488 error = EINVAL; 1489 goto out; 1490 } 1491 } else { 1492 switch (csvr->csvr_flags & CSVR_MODE_MASK) { 1493 case CSVR_MODE_SET: 1494 sac = SEND_VOLUME_TAG_ASSERT_PRIMARY; 1495 break; 1496 case CSVR_MODE_REPLACE: 1497 sac = SEND_VOLUME_TAG_REPLACE_PRIMARY; 1498 break; 1499 case CSVR_MODE_CLEAR: 1500 sac = SEND_VOLUME_TAG_UNDEFINED_PRIMARY; 1501 break; 1502 default: 1503 error = EINVAL; 1504 goto out; 1505 } 1506 } 1507 1508 memcpy(ssvtp.vitf, csvr->csvr_voltag.cv_volid, 1509 min(strlen(csvr->csvr_voltag.cv_volid), sizeof(ssvtp.vitf))); 1510 scsi_ulto2b(csvr->csvr_voltag.cv_serial, ssvtp.minvsn); 1511 1512 ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL); 1513 1514 scsi_send_volume_tag(&ccb->csio, 1515 /* retries */ 1, 1516 /* cbfcnp */ chdone, 1517 /* tag_action */ MSG_SIMPLE_Q_TAG, 1518 /* element_address */ ea, 1519 /* send_action_code */ sac, 1520 /* parameters */ &ssvtp, 1521 /* sense_len */ SSD_FULL_SIZE, 1522 /* timeout */ CH_TIMEOUT_SEND_VOLTAG); 1523 1524 error = cam_periph_runccb(ccb, cherror, /*cam_flags*/ CAM_RETRY_SELTO, 1525 /*sense_flags*/ SF_RETRY_UA, 1526 softc->device_stats); 1527 1528 xpt_release_ccb(ccb); 1529 1530 out: 1531 return error; 1532 } 1533 1534 static int 1535 chgetparams(struct cam_periph *periph) 1536 { 1537 union ccb *ccb; 1538 struct ch_softc *softc; 1539 void *mode_buffer; 1540 int mode_buffer_len; 1541 struct page_element_address_assignment *ea; 1542 struct page_device_capabilities *cap; 1543 int error, from, dbd; 1544 u_int8_t *moves, *exchanges; 1545 1546 error = 0; 1547 1548 softc = (struct ch_softc *)periph->softc; 1549 1550 ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL); 1551 1552 /* 1553 * The scsi_mode_sense_data structure is just a convenience 1554 * structure that allows us to easily calculate the worst-case 1555 * storage size of the mode sense buffer. 1556 */ 1557 mode_buffer_len = sizeof(struct scsi_mode_sense_data); 1558 1559 mode_buffer = malloc(mode_buffer_len, M_SCSICH, M_NOWAIT); 1560 1561 if (mode_buffer == NULL) { 1562 printf("chgetparams: couldn't malloc mode sense data\n"); 1563 return(ENOSPC); 1564 } 1565 1566 bzero(mode_buffer, mode_buffer_len); 1567 1568 if (softc->quirks & CH_Q_NO_DBD) 1569 dbd = FALSE; 1570 else 1571 dbd = TRUE; 1572 1573 /* 1574 * Get the element address assignment page. 1575 */ 1576 scsi_mode_sense(&ccb->csio, 1577 /* retries */ 1, 1578 /* cbfcnp */ chdone, 1579 /* tag_action */ MSG_SIMPLE_Q_TAG, 1580 /* dbd */ dbd, 1581 /* page_code */ SMS_PAGE_CTRL_CURRENT, 1582 /* page */ CH_ELEMENT_ADDR_ASSIGN_PAGE, 1583 /* param_buf */ (u_int8_t *)mode_buffer, 1584 /* param_len */ mode_buffer_len, 1585 /* sense_len */ SSD_FULL_SIZE, 1586 /* timeout */ CH_TIMEOUT_MODE_SENSE); 1587 1588 error = cam_periph_runccb(ccb, cherror, /*cam_flags*/ CAM_RETRY_SELTO, 1589 /* sense_flags */ SF_RETRY_UA|SF_NO_PRINT, 1590 softc->device_stats); 1591 1592 if (error) { 1593 if (dbd) { 1594 struct scsi_mode_sense_6 *sms; 1595 1596 sms = (struct scsi_mode_sense_6 *) 1597 ccb->csio.cdb_io.cdb_bytes; 1598 1599 sms->byte2 &= ~SMS_DBD; 1600 error = cam_periph_runccb(ccb, cherror, 1601 /*cam_flags*/ CAM_RETRY_SELTO, 1602 /*sense_flags*/ SF_RETRY_UA, 1603 softc->device_stats); 1604 } else { 1605 /* 1606 * Since we disabled sense printing above, print 1607 * out the sense here since we got an error. 1608 */ 1609 scsi_sense_print(&ccb->csio); 1610 } 1611 1612 if (error) { 1613 xpt_print(periph->path, 1614 "chgetparams: error getting element " 1615 "address page\n"); 1616 xpt_release_ccb(ccb); 1617 free(mode_buffer, M_SCSICH); 1618 return(error); 1619 } 1620 } 1621 1622 ea = (struct page_element_address_assignment *) 1623 find_mode_page_6((struct scsi_mode_header_6 *)mode_buffer); 1624 1625 softc->sc_firsts[CHET_MT] = scsi_2btoul(ea->mtea); 1626 softc->sc_counts[CHET_MT] = scsi_2btoul(ea->nmte); 1627 softc->sc_firsts[CHET_ST] = scsi_2btoul(ea->fsea); 1628 softc->sc_counts[CHET_ST] = scsi_2btoul(ea->nse); 1629 softc->sc_firsts[CHET_IE] = scsi_2btoul(ea->fieea); 1630 softc->sc_counts[CHET_IE] = scsi_2btoul(ea->niee); 1631 softc->sc_firsts[CHET_DT] = scsi_2btoul(ea->fdtea); 1632 softc->sc_counts[CHET_DT] = scsi_2btoul(ea->ndte); 1633 1634 bzero(mode_buffer, mode_buffer_len); 1635 1636 /* 1637 * Now get the device capabilities page. 1638 */ 1639 scsi_mode_sense(&ccb->csio, 1640 /* retries */ 1, 1641 /* cbfcnp */ chdone, 1642 /* tag_action */ MSG_SIMPLE_Q_TAG, 1643 /* dbd */ dbd, 1644 /* page_code */ SMS_PAGE_CTRL_CURRENT, 1645 /* page */ CH_DEVICE_CAP_PAGE, 1646 /* param_buf */ (u_int8_t *)mode_buffer, 1647 /* param_len */ mode_buffer_len, 1648 /* sense_len */ SSD_FULL_SIZE, 1649 /* timeout */ CH_TIMEOUT_MODE_SENSE); 1650 1651 error = cam_periph_runccb(ccb, cherror, /*cam_flags*/ CAM_RETRY_SELTO, 1652 /* sense_flags */ SF_RETRY_UA | SF_NO_PRINT, 1653 softc->device_stats); 1654 1655 if (error) { 1656 if (dbd) { 1657 struct scsi_mode_sense_6 *sms; 1658 1659 sms = (struct scsi_mode_sense_6 *) 1660 ccb->csio.cdb_io.cdb_bytes; 1661 1662 sms->byte2 &= ~SMS_DBD; 1663 error = cam_periph_runccb(ccb, cherror, 1664 /*cam_flags*/ CAM_RETRY_SELTO, 1665 /*sense_flags*/ SF_RETRY_UA, 1666 softc->device_stats); 1667 } else { 1668 /* 1669 * Since we disabled sense printing above, print 1670 * out the sense here since we got an error. 1671 */ 1672 scsi_sense_print(&ccb->csio); 1673 } 1674 1675 if (error) { 1676 xpt_print(periph->path, 1677 "chgetparams: error getting device " 1678 "capabilities page\n"); 1679 xpt_release_ccb(ccb); 1680 free(mode_buffer, M_SCSICH); 1681 return(error); 1682 } 1683 } 1684 1685 xpt_release_ccb(ccb); 1686 1687 cap = (struct page_device_capabilities *) 1688 find_mode_page_6((struct scsi_mode_header_6 *)mode_buffer); 1689 1690 bzero(softc->sc_movemask, sizeof(softc->sc_movemask)); 1691 bzero(softc->sc_exchangemask, sizeof(softc->sc_exchangemask)); 1692 moves = cap->move_from; 1693 exchanges = cap->exchange_with; 1694 for (from = CHET_MT; from <= CHET_MAX; ++from) { 1695 softc->sc_movemask[from] = moves[from]; 1696 softc->sc_exchangemask[from] = exchanges[from]; 1697 } 1698 1699 free(mode_buffer, M_SCSICH); 1700 1701 return(error); 1702 } 1703 1704 static int 1705 chscsiversion(struct cam_periph *periph) 1706 { 1707 struct scsi_inquiry_data *inq_data; 1708 struct ccb_getdev *cgd; 1709 int dev_scsi_version; 1710 1711 cam_periph_assert(periph, MA_OWNED); 1712 if ((cgd = (struct ccb_getdev *)xpt_alloc_ccb_nowait()) == NULL) 1713 return (-1); 1714 /* 1715 * Get the device information. 1716 */ 1717 xpt_setup_ccb(&cgd->ccb_h, 1718 periph->path, 1719 CAM_PRIORITY_NORMAL); 1720 cgd->ccb_h.func_code = XPT_GDEV_TYPE; 1721 xpt_action((union ccb *)cgd); 1722 1723 if (cgd->ccb_h.status != CAM_REQ_CMP) { 1724 xpt_free_ccb((union ccb *)cgd); 1725 return -1; 1726 } 1727 1728 inq_data = &cgd->inq_data; 1729 dev_scsi_version = inq_data->version; 1730 xpt_free_ccb((union ccb *)cgd); 1731 1732 return dev_scsi_version; 1733 } 1734 1735 void 1736 scsi_move_medium(struct ccb_scsiio *csio, u_int32_t retries, 1737 void (*cbfcnp)(struct cam_periph *, union ccb *), 1738 u_int8_t tag_action, u_int32_t tea, u_int32_t src, 1739 u_int32_t dst, int invert, u_int8_t sense_len, 1740 u_int32_t timeout) 1741 { 1742 struct scsi_move_medium *scsi_cmd; 1743 1744 scsi_cmd = (struct scsi_move_medium *)&csio->cdb_io.cdb_bytes; 1745 bzero(scsi_cmd, sizeof(*scsi_cmd)); 1746 1747 scsi_cmd->opcode = MOVE_MEDIUM; 1748 1749 scsi_ulto2b(tea, scsi_cmd->tea); 1750 scsi_ulto2b(src, scsi_cmd->src); 1751 scsi_ulto2b(dst, scsi_cmd->dst); 1752 1753 if (invert) 1754 scsi_cmd->invert |= MOVE_MEDIUM_INVERT; 1755 1756 cam_fill_csio(csio, 1757 retries, 1758 cbfcnp, 1759 /*flags*/ CAM_DIR_NONE, 1760 tag_action, 1761 /*data_ptr*/ NULL, 1762 /*dxfer_len*/ 0, 1763 sense_len, 1764 sizeof(*scsi_cmd), 1765 timeout); 1766 } 1767 1768 void 1769 scsi_exchange_medium(struct ccb_scsiio *csio, u_int32_t retries, 1770 void (*cbfcnp)(struct cam_periph *, union ccb *), 1771 u_int8_t tag_action, u_int32_t tea, u_int32_t src, 1772 u_int32_t dst1, u_int32_t dst2, int invert1, 1773 int invert2, u_int8_t sense_len, u_int32_t timeout) 1774 { 1775 struct scsi_exchange_medium *scsi_cmd; 1776 1777 scsi_cmd = (struct scsi_exchange_medium *)&csio->cdb_io.cdb_bytes; 1778 bzero(scsi_cmd, sizeof(*scsi_cmd)); 1779 1780 scsi_cmd->opcode = EXCHANGE_MEDIUM; 1781 1782 scsi_ulto2b(tea, scsi_cmd->tea); 1783 scsi_ulto2b(src, scsi_cmd->src); 1784 scsi_ulto2b(dst1, scsi_cmd->fdst); 1785 scsi_ulto2b(dst2, scsi_cmd->sdst); 1786 1787 if (invert1) 1788 scsi_cmd->invert |= EXCHANGE_MEDIUM_INV1; 1789 1790 if (invert2) 1791 scsi_cmd->invert |= EXCHANGE_MEDIUM_INV2; 1792 1793 cam_fill_csio(csio, 1794 retries, 1795 cbfcnp, 1796 /*flags*/ CAM_DIR_NONE, 1797 tag_action, 1798 /*data_ptr*/ NULL, 1799 /*dxfer_len*/ 0, 1800 sense_len, 1801 sizeof(*scsi_cmd), 1802 timeout); 1803 } 1804 1805 void 1806 scsi_position_to_element(struct ccb_scsiio *csio, u_int32_t retries, 1807 void (*cbfcnp)(struct cam_periph *, union ccb *), 1808 u_int8_t tag_action, u_int32_t tea, u_int32_t dst, 1809 int invert, u_int8_t sense_len, u_int32_t timeout) 1810 { 1811 struct scsi_position_to_element *scsi_cmd; 1812 1813 scsi_cmd = (struct scsi_position_to_element *)&csio->cdb_io.cdb_bytes; 1814 bzero(scsi_cmd, sizeof(*scsi_cmd)); 1815 1816 scsi_cmd->opcode = POSITION_TO_ELEMENT; 1817 1818 scsi_ulto2b(tea, scsi_cmd->tea); 1819 scsi_ulto2b(dst, scsi_cmd->dst); 1820 1821 if (invert) 1822 scsi_cmd->invert |= POSITION_TO_ELEMENT_INVERT; 1823 1824 cam_fill_csio(csio, 1825 retries, 1826 cbfcnp, 1827 /*flags*/ CAM_DIR_NONE, 1828 tag_action, 1829 /*data_ptr*/ NULL, 1830 /*dxfer_len*/ 0, 1831 sense_len, 1832 sizeof(*scsi_cmd), 1833 timeout); 1834 } 1835 1836 void 1837 scsi_read_element_status(struct ccb_scsiio *csio, u_int32_t retries, 1838 void (*cbfcnp)(struct cam_periph *, union ccb *), 1839 u_int8_t tag_action, int voltag, u_int32_t sea, 1840 int curdata, int dvcid, 1841 u_int32_t count, u_int8_t *data_ptr, 1842 u_int32_t dxfer_len, u_int8_t sense_len, 1843 u_int32_t timeout) 1844 { 1845 struct scsi_read_element_status *scsi_cmd; 1846 1847 scsi_cmd = (struct scsi_read_element_status *)&csio->cdb_io.cdb_bytes; 1848 bzero(scsi_cmd, sizeof(*scsi_cmd)); 1849 1850 scsi_cmd->opcode = READ_ELEMENT_STATUS; 1851 1852 scsi_ulto2b(sea, scsi_cmd->sea); 1853 scsi_ulto2b(count, scsi_cmd->count); 1854 scsi_ulto3b(dxfer_len, scsi_cmd->len); 1855 if (dvcid) 1856 scsi_cmd->flags |= READ_ELEMENT_STATUS_DVCID; 1857 if (curdata) 1858 scsi_cmd->flags |= READ_ELEMENT_STATUS_CURDATA; 1859 1860 if (voltag) 1861 scsi_cmd->byte2 |= READ_ELEMENT_STATUS_VOLTAG; 1862 1863 cam_fill_csio(csio, 1864 retries, 1865 cbfcnp, 1866 /*flags*/ CAM_DIR_IN, 1867 tag_action, 1868 data_ptr, 1869 dxfer_len, 1870 sense_len, 1871 sizeof(*scsi_cmd), 1872 timeout); 1873 } 1874 1875 void 1876 scsi_initialize_element_status(struct ccb_scsiio *csio, u_int32_t retries, 1877 void (*cbfcnp)(struct cam_periph *, union ccb *), 1878 u_int8_t tag_action, u_int8_t sense_len, 1879 u_int32_t timeout) 1880 { 1881 struct scsi_initialize_element_status *scsi_cmd; 1882 1883 scsi_cmd = (struct scsi_initialize_element_status *) 1884 &csio->cdb_io.cdb_bytes; 1885 bzero(scsi_cmd, sizeof(*scsi_cmd)); 1886 1887 scsi_cmd->opcode = INITIALIZE_ELEMENT_STATUS; 1888 1889 cam_fill_csio(csio, 1890 retries, 1891 cbfcnp, 1892 /*flags*/ CAM_DIR_NONE, 1893 tag_action, 1894 /* data_ptr */ NULL, 1895 /* dxfer_len */ 0, 1896 sense_len, 1897 sizeof(*scsi_cmd), 1898 timeout); 1899 } 1900 1901 void 1902 scsi_send_volume_tag(struct ccb_scsiio *csio, u_int32_t retries, 1903 void (*cbfcnp)(struct cam_periph *, union ccb *), 1904 u_int8_t tag_action, 1905 u_int16_t element_address, 1906 u_int8_t send_action_code, 1907 struct scsi_send_volume_tag_parameters *parameters, 1908 u_int8_t sense_len, u_int32_t timeout) 1909 { 1910 struct scsi_send_volume_tag *scsi_cmd; 1911 1912 scsi_cmd = (struct scsi_send_volume_tag *) &csio->cdb_io.cdb_bytes; 1913 bzero(scsi_cmd, sizeof(*scsi_cmd)); 1914 1915 scsi_cmd->opcode = SEND_VOLUME_TAG; 1916 scsi_ulto2b(element_address, scsi_cmd->ea); 1917 scsi_cmd->sac = send_action_code; 1918 scsi_ulto2b(sizeof(*parameters), scsi_cmd->pll); 1919 1920 cam_fill_csio(csio, 1921 retries, 1922 cbfcnp, 1923 /*flags*/ CAM_DIR_OUT, 1924 tag_action, 1925 /* data_ptr */ (u_int8_t *) parameters, 1926 sizeof(*parameters), 1927 sense_len, 1928 sizeof(*scsi_cmd), 1929 timeout); 1930 } 1931