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