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