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