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