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