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