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