1 /* 2 * Copyright (c) 1997 Justin T. Gibbs. 3 * Copyright (c) 1997, 1998 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 * $Id: scsi_ch.c,v 1.8 1998/12/12 23:52:46 gibbs Exp $ 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 { 219 /*d_open*/ chopen, 220 /*d_close*/ chclose, 221 /*d_read*/ noread, 222 /*d_write*/ nowrite, 223 /*d_ioctl*/ chioctl, 224 /*d_stop*/ nostop, 225 /*d_reset*/ noreset, 226 /*d_devtotty*/ nodevtotty, 227 /*d_poll*/ seltrue, 228 /*d_mmap*/ nommap, 229 /*d_strategy*/ nostrategy, 230 /*d_name*/ "ch", 231 /*d_spare*/ NULL, 232 /*d_maj*/ -1, 233 /*d_dump*/ nodump, 234 /*d_psize*/ nopsize, 235 /*d_flags*/ 0, 236 /*d_maxio*/ 0, 237 /*b_maj*/ -1 238 }; 239 240 static struct extend_array *chperiphs; 241 242 void 243 chinit(void) 244 { 245 cam_status status; 246 struct cam_path *path; 247 248 /* 249 * Create our extend array for storing the devices we attach to. 250 */ 251 chperiphs = cam_extend_new(); 252 if (chperiphs == NULL) { 253 printf("ch: Failed to alloc extend array!\n"); 254 return; 255 } 256 257 /* 258 * Install a global async callback. This callback will 259 * receive async callbacks like "new device found". 260 */ 261 status = xpt_create_path(&path, /*periph*/NULL, CAM_XPT_PATH_ID, 262 CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD); 263 264 if (status == CAM_REQ_CMP) { 265 struct ccb_setasync csa; 266 267 xpt_setup_ccb(&csa.ccb_h, path, /*priority*/5); 268 csa.ccb_h.func_code = XPT_SASYNC_CB; 269 csa.event_enable = AC_FOUND_DEVICE; 270 csa.callback = chasync; 271 csa.callback_arg = NULL; 272 xpt_action((union ccb *)&csa); 273 status = csa.ccb_h.status; 274 xpt_free_path(path); 275 } 276 277 if (status != CAM_REQ_CMP) { 278 printf("ch: Failed to attach master async callback " 279 "due to status 0x%x!\n", status); 280 } else { 281 dev_t dev; 282 283 /* If we were successfull, register our devsw */ 284 dev = makedev(CH_CDEV_MAJOR, 0); 285 cdevsw_add(&dev, &ch_cdevsw, NULL); 286 } 287 } 288 289 static void 290 choninvalidate(struct cam_periph *periph) 291 { 292 struct ch_softc *softc; 293 struct ccb_setasync csa; 294 295 softc = (struct ch_softc *)periph->softc; 296 297 /* 298 * De-register any async callbacks. 299 */ 300 xpt_setup_ccb(&csa.ccb_h, periph->path, 301 /* priority */ 5); 302 csa.ccb_h.func_code = XPT_SASYNC_CB; 303 csa.event_enable = 0; 304 csa.callback = chasync; 305 csa.callback_arg = periph; 306 xpt_action((union ccb *)&csa); 307 308 softc->flags |= CH_FLAG_INVALID; 309 310 xpt_print_path(periph->path); 311 printf("lost device\n"); 312 313 } 314 315 static void 316 chcleanup(struct cam_periph *periph) 317 { 318 struct ch_softc *softc; 319 320 softc = (struct ch_softc *)periph->softc; 321 322 devstat_remove_entry(&softc->device_stats); 323 cam_extend_release(chperiphs, periph->unit_number); 324 xpt_print_path(periph->path); 325 printf("removing device entry\n"); 326 free(softc, M_DEVBUF); 327 } 328 329 static void 330 chasync(void *callback_arg, u_int32_t code, struct cam_path *path, void *arg) 331 { 332 struct cam_periph *periph; 333 334 periph = (struct cam_periph *)callback_arg; 335 336 switch(code) { 337 case AC_FOUND_DEVICE: 338 { 339 struct ccb_getdev *cgd; 340 cam_status status; 341 342 cgd = (struct ccb_getdev *)arg; 343 344 if (cgd->pd_type != T_CHANGER) 345 break; 346 347 /* 348 * Allocate a peripheral instance for 349 * this device and start the probe 350 * process. 351 */ 352 status = cam_periph_alloc(chregister, choninvalidate, 353 chcleanup, chstart, "ch", 354 CAM_PERIPH_BIO, cgd->ccb_h.path, 355 chasync, AC_FOUND_DEVICE, cgd); 356 357 if (status != CAM_REQ_CMP 358 && status != CAM_REQ_INPROG) 359 printf("chasync: Unable to probe new device " 360 "due to status 0x%x\n", status); 361 362 break; 363 364 } 365 case AC_LOST_DEVICE: 366 cam_periph_invalidate(periph); 367 break; 368 case AC_TRANSFER_NEG: 369 case AC_SENT_BDR: 370 case AC_SCSI_AEN: 371 case AC_UNSOL_RESEL: 372 case AC_BUS_RESET: 373 default: 374 break; 375 } 376 } 377 378 static cam_status 379 chregister(struct cam_periph *periph, void *arg) 380 { 381 struct ch_softc *softc; 382 struct ccb_setasync csa; 383 struct ccb_getdev *cgd; 384 385 cgd = (struct ccb_getdev *)arg; 386 if (periph == NULL) { 387 printf("chregister: periph was NULL!!\n"); 388 return(CAM_REQ_CMP_ERR); 389 } 390 391 if (cgd == NULL) { 392 printf("chregister: no getdev CCB, can't register device\n"); 393 return(CAM_REQ_CMP_ERR); 394 } 395 396 softc = (struct ch_softc *)malloc(sizeof(*softc),M_DEVBUF,M_NOWAIT); 397 398 if (softc == NULL) { 399 printf("chregister: Unable to probe new device. " 400 "Unable to allocate softc\n"); 401 return(CAM_REQ_CMP_ERR); 402 } 403 404 bzero(softc, sizeof(*softc)); 405 softc->state = CH_STATE_PROBE; 406 periph->softc = softc; 407 cam_extend_set(chperiphs, periph->unit_number, periph); 408 softc->quirks = CH_Q_NONE; 409 410 /* 411 * Changers don't have a blocksize, and obviously don't support 412 * tagged queueing. 413 */ 414 devstat_add_entry(&softc->device_stats, "ch", 415 periph->unit_number, 0, 416 DEVSTAT_NO_BLOCKSIZE | DEVSTAT_NO_ORDERED_TAGS, 417 cgd->pd_type | DEVSTAT_TYPE_IF_SCSI); 418 419 /* 420 * Add an async callback so that we get 421 * notified if this device goes away. 422 */ 423 xpt_setup_ccb(&csa.ccb_h, periph->path, /* priority */ 5); 424 csa.ccb_h.func_code = XPT_SASYNC_CB; 425 csa.event_enable = AC_LOST_DEVICE; 426 csa.callback = chasync; 427 csa.callback_arg = periph; 428 xpt_action((union ccb *)&csa); 429 430 /* 431 * Lock this peripheral until we are setup. 432 * This first call can't block 433 */ 434 (void)cam_periph_lock(periph, PRIBIO); 435 xpt_schedule(periph, /*priority*/5); 436 437 return(CAM_REQ_CMP); 438 } 439 440 static int 441 chopen(dev_t dev, int flags, int fmt, struct proc *p) 442 { 443 struct cam_periph *periph; 444 struct ch_softc *softc; 445 int unit, error; 446 int s; 447 448 unit = CHUNIT(dev); 449 periph = cam_extend_get(chperiphs, unit); 450 451 if (periph == NULL) 452 return(ENXIO); 453 454 softc = (struct ch_softc *)periph->softc; 455 456 s = splsoftcam(); 457 if (softc->flags & CH_FLAG_INVALID) { 458 splx(s); 459 return(ENXIO); 460 } 461 462 if ((error = cam_periph_lock(periph, PRIBIO | PCATCH)) != 0) { 463 splx(s); 464 return (error); 465 } 466 467 splx(s); 468 469 if ((softc->flags & CH_FLAG_OPEN) == 0) { 470 if (cam_periph_acquire(periph) != CAM_REQ_CMP) 471 return(ENXIO); 472 softc->flags |= CH_FLAG_OPEN; 473 } 474 475 /* 476 * Load information about this changer device into the softc. 477 */ 478 if ((error = chgetparams(periph)) != 0) { 479 softc->flags &= ~CH_FLAG_OPEN; 480 cam_periph_unlock(periph); 481 cam_periph_release(periph); 482 return(error); 483 } 484 485 cam_periph_unlock(periph); 486 487 return(error); 488 } 489 490 static int 491 chclose(dev_t dev, int flag, int fmt, struct proc *p) 492 { 493 struct cam_periph *periph; 494 struct ch_softc *softc; 495 int unit, error; 496 497 error = 0; 498 499 unit = CHUNIT(dev); 500 periph = cam_extend_get(chperiphs, unit); 501 if (periph == NULL) 502 return(ENXIO); 503 504 softc = (struct ch_softc *)periph->softc; 505 506 if ((error = cam_periph_lock(periph, PRIBIO)) != 0) 507 return(error); 508 509 softc->flags &= ~CH_FLAG_OPEN; 510 511 cam_periph_unlock(periph); 512 cam_periph_release(periph); 513 514 return(0); 515 } 516 517 static void 518 chstart(struct cam_periph *periph, union ccb *start_ccb) 519 { 520 struct ch_softc *softc; 521 int s; 522 523 softc = (struct ch_softc *)periph->softc; 524 525 switch (softc->state) { 526 case CH_STATE_NORMAL: 527 { 528 s = splbio(); 529 if (periph->immediate_priority <= periph->pinfo.priority){ 530 start_ccb->ccb_h.ccb_state = CH_CCB_WAITING; 531 532 SLIST_INSERT_HEAD(&periph->ccb_list, &start_ccb->ccb_h, 533 periph_links.sle); 534 periph->immediate_priority = CAM_PRIORITY_NONE; 535 splx(s); 536 wakeup(&periph->ccb_list); 537 } else 538 splx(s); 539 break; 540 } 541 case CH_STATE_PROBE: 542 { 543 int mode_buffer_len; 544 void *mode_buffer; 545 546 /* 547 * Include the block descriptor when calculating the mode 548 * buffer length, 549 */ 550 mode_buffer_len = sizeof(struct scsi_mode_header_6) + 551 sizeof(struct scsi_mode_blk_desc) + 552 sizeof(struct page_element_address_assignment); 553 554 mode_buffer = malloc(mode_buffer_len, M_TEMP, M_NOWAIT); 555 556 if (mode_buffer == NULL) { 557 printf("chstart: couldn't malloc mode sense data\n"); 558 break; 559 } 560 bzero(mode_buffer, mode_buffer_len); 561 562 /* 563 * Get the element address assignment page. 564 */ 565 scsi_mode_sense(&start_ccb->csio, 566 /* retries */ 1, 567 /* cbfcnp */ chdone, 568 /* tag_action */ MSG_SIMPLE_Q_TAG, 569 /* dbd */ (softc->quirks & CH_Q_NO_DBD) ? 570 FALSE : TRUE, 571 /* page_code */ SMS_PAGE_CTRL_CURRENT, 572 /* page */ CH_ELEMENT_ADDR_ASSIGN_PAGE, 573 /* param_buf */ (u_int8_t *)mode_buffer, 574 /* param_len */ mode_buffer_len, 575 /* sense_len */ SSD_FULL_SIZE, 576 /* timeout */ CH_TIMEOUT_MODE_SENSE); 577 578 start_ccb->ccb_h.ccb_bp = NULL; 579 start_ccb->ccb_h.ccb_state = CH_CCB_PROBE; 580 xpt_action(start_ccb); 581 break; 582 } 583 } 584 } 585 586 static void 587 chdone(struct cam_periph *periph, union ccb *done_ccb) 588 { 589 struct ch_softc *softc; 590 struct ccb_scsiio *csio; 591 592 softc = (struct ch_softc *)periph->softc; 593 csio = &done_ccb->csio; 594 595 switch(done_ccb->ccb_h.ccb_state) { 596 case CH_CCB_PROBE: 597 { 598 struct scsi_mode_header_6 *mode_header; 599 struct page_element_address_assignment *ea; 600 char announce_buf[80]; 601 602 603 mode_header = (struct scsi_mode_header_6 *)csio->data_ptr; 604 605 ea = (struct page_element_address_assignment *) 606 find_mode_page_6(mode_header); 607 608 if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP){ 609 610 softc->sc_firsts[CHET_MT] = scsi_2btoul(ea->mtea); 611 softc->sc_counts[CHET_MT] = scsi_2btoul(ea->nmte); 612 softc->sc_firsts[CHET_ST] = scsi_2btoul(ea->fsea); 613 softc->sc_counts[CHET_ST] = scsi_2btoul(ea->nse); 614 softc->sc_firsts[CHET_IE] = scsi_2btoul(ea->fieea); 615 softc->sc_counts[CHET_IE] = scsi_2btoul(ea->niee); 616 softc->sc_firsts[CHET_DT] = scsi_2btoul(ea->fdtea); 617 softc->sc_counts[CHET_DT] = scsi_2btoul(ea->ndte); 618 softc->sc_picker = softc->sc_firsts[CHET_MT]; 619 620 #define PLURAL(c) (c) == 1 ? "" : "s" 621 snprintf(announce_buf, sizeof(announce_buf), 622 "%d slot%s, %d drive%s, " 623 "%d picker%s, %d portal%s", 624 softc->sc_counts[CHET_ST], 625 PLURAL(softc->sc_counts[CHET_ST]), 626 softc->sc_counts[CHET_DT], 627 PLURAL(softc->sc_counts[CHET_DT]), 628 softc->sc_counts[CHET_MT], 629 PLURAL(softc->sc_counts[CHET_MT]), 630 softc->sc_counts[CHET_IE], 631 PLURAL(softc->sc_counts[CHET_IE])); 632 #undef PLURAL 633 } else { 634 int error; 635 636 error = cherror(done_ccb, 0, SF_RETRY_UA|SF_NO_PRINT); 637 /* 638 * Retry any UNIT ATTENTION type errors. They 639 * are expected at boot. 640 */ 641 if (error == ERESTART) { 642 /* 643 * A retry was scheuled, so 644 * just return. 645 */ 646 return; 647 } else if (error != 0) { 648 int retry_scheduled; 649 struct scsi_mode_sense_6 *sms; 650 651 sms = (struct scsi_mode_sense_6 *) 652 done_ccb->csio.cdb_io.cdb_bytes; 653 654 /* 655 * Check to see if block descriptors were 656 * disabled. Some devices don't like that. 657 * We're taking advantage of the fact that 658 * the first few bytes of the 6 and 10 byte 659 * mode sense commands are the same. If 660 * block descriptors were disabled, enable 661 * them and re-send the command. 662 */ 663 if (sms->byte2 & SMS_DBD) { 664 sms->byte2 &= ~SMS_DBD; 665 xpt_action(done_ccb); 666 softc->quirks |= CH_Q_NO_DBD; 667 retry_scheduled = 1; 668 } else 669 retry_scheduled = 0; 670 671 /* Don't wedge this device's queue */ 672 cam_release_devq(done_ccb->ccb_h.path, 673 /*relsim_flags*/0, 674 /*reduction*/0, 675 /*timeout*/0, 676 /*getcount_only*/0); 677 678 if (retry_scheduled) 679 return; 680 681 if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) 682 == CAM_SCSI_STATUS_ERROR) 683 scsi_sense_print(&done_ccb->csio); 684 else { 685 xpt_print_path(periph->path); 686 printf("got CAM status %#x\n", 687 done_ccb->ccb_h.status); 688 } 689 xpt_print_path(periph->path); 690 printf("fatal error, failed to attach to" 691 " device\n"); 692 693 cam_periph_invalidate(periph); 694 695 announce_buf[0] = '\0'; 696 } 697 } 698 if (announce_buf[0] != '\0') 699 xpt_announce_periph(periph, announce_buf); 700 softc->state = CH_STATE_NORMAL; 701 free(mode_header, M_TEMP); 702 cam_periph_unlock(periph); 703 break; 704 } 705 case CH_CCB_WAITING: 706 { 707 /* Caller will release the CCB */ 708 wakeup(&done_ccb->ccb_h.cbfcnp); 709 return; 710 } 711 } 712 xpt_release_ccb(done_ccb); 713 } 714 715 static int 716 cherror(union ccb *ccb, u_int32_t cam_flags, u_int32_t sense_flags) 717 { 718 struct ch_softc *softc; 719 struct cam_periph *periph; 720 721 periph = xpt_path_periph(ccb->ccb_h.path); 722 softc = (struct ch_softc *)periph->softc; 723 724 return (cam_periph_error(ccb, cam_flags, sense_flags, 725 &softc->saved_ccb)); 726 } 727 728 static int 729 chioctl(dev_t dev, u_long cmd, caddr_t addr, int flag, struct proc *p) 730 { 731 struct cam_periph *periph; 732 struct ch_softc *softc; 733 u_int8_t unit; 734 int error; 735 736 unit = CHUNIT(dev); 737 738 periph = cam_extend_get(chperiphs, unit); 739 if (periph == NULL) 740 return(ENXIO); 741 742 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("entering chioctl\n")); 743 744 softc = (struct ch_softc *)periph->softc; 745 746 error = 0; 747 748 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, 749 ("trying to do ioctl %#lx\n", cmd)); 750 751 /* 752 * If this command can change the device's state, we must 753 * have the device open for writing. 754 */ 755 switch (cmd) { 756 case CHIOGPICKER: 757 case CHIOGPARAMS: 758 case CHIOGSTATUS: 759 break; 760 761 default: 762 if ((flag & FWRITE) == 0) 763 return (EBADF); 764 } 765 766 switch (cmd) { 767 case CHIOMOVE: 768 error = chmove(periph, (struct changer_move *)addr); 769 break; 770 771 case CHIOEXCHANGE: 772 error = chexchange(periph, (struct changer_exchange *)addr); 773 break; 774 775 case CHIOPOSITION: 776 error = chposition(periph, (struct changer_position *)addr); 777 break; 778 779 case CHIOGPICKER: 780 *(int *)addr = softc->sc_picker - softc->sc_firsts[CHET_MT]; 781 break; 782 783 case CHIOSPICKER: 784 { 785 int new_picker = *(int *)addr; 786 787 if (new_picker > (softc->sc_counts[CHET_MT] - 1)) 788 return (EINVAL); 789 softc->sc_picker = softc->sc_firsts[CHET_MT] + new_picker; 790 break; 791 } 792 case CHIOGPARAMS: 793 { 794 struct changer_params *cp = (struct changer_params *)addr; 795 796 cp->cp_npickers = softc->sc_counts[CHET_MT]; 797 cp->cp_nslots = softc->sc_counts[CHET_ST]; 798 cp->cp_nportals = softc->sc_counts[CHET_IE]; 799 cp->cp_ndrives = softc->sc_counts[CHET_DT]; 800 break; 801 } 802 case CHIOIELEM: 803 error = chielem(periph, *(unsigned int *)addr); 804 break; 805 806 case CHIOGSTATUS: 807 { 808 error = chgetelemstatus(periph, 809 (struct changer_element_status_request *) addr); 810 break; 811 } 812 813 case CHIOSETVOLTAG: 814 { 815 error = chsetvoltag(periph, 816 (struct changer_set_voltag_request *) addr); 817 break; 818 } 819 820 /* Implement prevent/allow? */ 821 822 default: 823 error = cam_periph_ioctl(periph, cmd, addr, cherror); 824 break; 825 } 826 827 return (error); 828 } 829 830 static int 831 chmove(struct cam_periph *periph, struct changer_move *cm) 832 { 833 struct ch_softc *softc; 834 u_int16_t fromelem, toelem; 835 union ccb *ccb; 836 int error; 837 838 error = 0; 839 softc = (struct ch_softc *)periph->softc; 840 841 /* 842 * Check arguments. 843 */ 844 if ((cm->cm_fromtype > CHET_DT) || (cm->cm_totype > CHET_DT)) 845 return (EINVAL); 846 if ((cm->cm_fromunit > (softc->sc_counts[cm->cm_fromtype] - 1)) || 847 (cm->cm_tounit > (softc->sc_counts[cm->cm_totype] - 1))) 848 return (ENODEV); 849 850 /* 851 * Check the request against the changer's capabilities. 852 */ 853 if ((softc->sc_movemask[cm->cm_fromtype] & (1 << cm->cm_totype)) == 0) 854 return (ENODEV); 855 856 /* 857 * Calculate the source and destination elements. 858 */ 859 fromelem = softc->sc_firsts[cm->cm_fromtype] + cm->cm_fromunit; 860 toelem = softc->sc_firsts[cm->cm_totype] + cm->cm_tounit; 861 862 ccb = cam_periph_getccb(periph, /*priority*/ 1); 863 864 scsi_move_medium(&ccb->csio, 865 /* retries */ 1, 866 /* cbfcnp */ chdone, 867 /* tag_action */ MSG_SIMPLE_Q_TAG, 868 /* tea */ softc->sc_picker, 869 /* src */ fromelem, 870 /* dst */ toelem, 871 /* invert */ (cm->cm_flags & CM_INVERT) ? TRUE : FALSE, 872 /* sense_len */ SSD_FULL_SIZE, 873 /* timeout */ CH_TIMEOUT_MOVE_MEDIUM); 874 875 error = cam_periph_runccb(ccb, cherror, /*cam_flags*/0, 876 /*sense_flags*/ SF_RETRY_UA, 877 &softc->device_stats); 878 879 xpt_release_ccb(ccb); 880 881 return(error); 882 } 883 884 static int 885 chexchange(struct cam_periph *periph, struct changer_exchange *ce) 886 { 887 struct ch_softc *softc; 888 u_int16_t src, dst1, dst2; 889 union ccb *ccb; 890 int error; 891 892 error = 0; 893 softc = (struct ch_softc *)periph->softc; 894 /* 895 * Check arguments. 896 */ 897 if ((ce->ce_srctype > CHET_DT) || (ce->ce_fdsttype > CHET_DT) || 898 (ce->ce_sdsttype > CHET_DT)) 899 return (EINVAL); 900 if ((ce->ce_srcunit > (softc->sc_counts[ce->ce_srctype] - 1)) || 901 (ce->ce_fdstunit > (softc->sc_counts[ce->ce_fdsttype] - 1)) || 902 (ce->ce_sdstunit > (softc->sc_counts[ce->ce_sdsttype] - 1))) 903 return (ENODEV); 904 905 /* 906 * Check the request against the changer's capabilities. 907 */ 908 if (((softc->sc_exchangemask[ce->ce_srctype] & 909 (1 << ce->ce_fdsttype)) == 0) || 910 ((softc->sc_exchangemask[ce->ce_fdsttype] & 911 (1 << ce->ce_sdsttype)) == 0)) 912 return (ENODEV); 913 914 /* 915 * Calculate the source and destination elements. 916 */ 917 src = softc->sc_firsts[ce->ce_srctype] + ce->ce_srcunit; 918 dst1 = softc->sc_firsts[ce->ce_fdsttype] + ce->ce_fdstunit; 919 dst2 = softc->sc_firsts[ce->ce_sdsttype] + ce->ce_sdstunit; 920 921 ccb = cam_periph_getccb(periph, /*priority*/ 1); 922 923 scsi_exchange_medium(&ccb->csio, 924 /* retries */ 1, 925 /* cbfcnp */ chdone, 926 /* tag_action */ MSG_SIMPLE_Q_TAG, 927 /* tea */ softc->sc_picker, 928 /* src */ src, 929 /* dst1 */ dst1, 930 /* dst2 */ dst2, 931 /* invert1 */ (ce->ce_flags & CE_INVERT1) ? 932 TRUE : FALSE, 933 /* invert2 */ (ce->ce_flags & CE_INVERT2) ? 934 TRUE : FALSE, 935 /* sense_len */ SSD_FULL_SIZE, 936 /* timeout */ CH_TIMEOUT_EXCHANGE_MEDIUM); 937 938 error = cam_periph_runccb(ccb, cherror, /*cam_flags*/0, 939 /*sense_flags*/ SF_RETRY_UA, 940 &softc->device_stats); 941 942 xpt_release_ccb(ccb); 943 944 return(error); 945 } 946 947 static int 948 chposition(struct cam_periph *periph, struct changer_position *cp) 949 { 950 struct ch_softc *softc; 951 u_int16_t dst; 952 union ccb *ccb; 953 int error; 954 955 error = 0; 956 softc = (struct ch_softc *)periph->softc; 957 958 /* 959 * Check arguments. 960 */ 961 if (cp->cp_type > CHET_DT) 962 return (EINVAL); 963 if (cp->cp_unit > (softc->sc_counts[cp->cp_type] - 1)) 964 return (ENODEV); 965 966 /* 967 * Calculate the destination element. 968 */ 969 dst = softc->sc_firsts[cp->cp_type] + cp->cp_unit; 970 971 ccb = cam_periph_getccb(periph, /*priority*/ 1); 972 973 scsi_position_to_element(&ccb->csio, 974 /* retries */ 1, 975 /* cbfcnp */ chdone, 976 /* tag_action */ MSG_SIMPLE_Q_TAG, 977 /* tea */ softc->sc_picker, 978 /* dst */ dst, 979 /* invert */ (cp->cp_flags & CP_INVERT) ? 980 TRUE : FALSE, 981 /* sense_len */ SSD_FULL_SIZE, 982 /* timeout */ CH_TIMEOUT_POSITION_TO_ELEMENT); 983 984 error = cam_periph_runccb(ccb, cherror, /*cam_flags*/ 0, 985 /*sense_flags*/ SF_RETRY_UA, 986 &softc->device_stats); 987 988 xpt_release_ccb(ccb); 989 990 return(error); 991 } 992 993 /* 994 * Copy a volume tag to a volume_tag struct, converting SCSI byte order 995 * to host native byte order in the volume serial number. The volume 996 * label as returned by the changer is transferred to user mode as 997 * nul-terminated string. Volume labels are truncated at the first 998 * space, as suggested by SCSI-2. 999 */ 1000 static void 1001 copy_voltag(struct changer_voltag *uvoltag, struct volume_tag *voltag) 1002 { 1003 int i; 1004 for (i=0; i<CH_VOLTAG_MAXLEN; i++) { 1005 char c = voltag->vif[i]; 1006 if (c && c != ' ') 1007 uvoltag->cv_volid[i] = c; 1008 else 1009 break; 1010 } 1011 uvoltag->cv_serial = scsi_2btoul(voltag->vsn); 1012 } 1013 1014 /* 1015 * Copy an an element status descriptor to a user-mode 1016 * changer_element_status structure. 1017 */ 1018 1019 static void 1020 copy_element_status(struct ch_softc *softc, 1021 u_int16_t flags, 1022 struct read_element_status_descriptor *desc, 1023 struct changer_element_status *ces) 1024 { 1025 u_int16_t eaddr = scsi_2btoul(desc->eaddr); 1026 u_int16_t et; 1027 1028 ces->ces_int_addr = eaddr; 1029 /* set up logical address in element status */ 1030 for (et = CHET_MT; et <= CHET_DT; et++) { 1031 if ((softc->sc_firsts[et] <= eaddr) 1032 && ((softc->sc_firsts[et] + softc->sc_counts[et]) 1033 > eaddr)) { 1034 ces->ces_addr = eaddr - softc->sc_firsts[et]; 1035 ces->ces_type = et; 1036 break; 1037 } 1038 } 1039 1040 ces->ces_flags = desc->flags1; 1041 1042 ces->ces_sensecode = desc->sense_code; 1043 ces->ces_sensequal = desc->sense_qual; 1044 1045 if (desc->flags2 & READ_ELEMENT_STATUS_INVERT) 1046 ces->ces_flags |= CES_INVERT; 1047 1048 if (desc->flags2 & READ_ELEMENT_STATUS_SVALID) { 1049 1050 eaddr = scsi_2btoul(desc->ssea); 1051 1052 /* convert source address to logical format */ 1053 for (et = CHET_MT; et <= CHET_DT; et++) { 1054 if ((softc->sc_firsts[et] <= eaddr) 1055 && ((softc->sc_firsts[et] + softc->sc_counts[et]) 1056 > eaddr)) { 1057 ces->ces_source_addr = 1058 eaddr - softc->sc_firsts[et]; 1059 ces->ces_source_type = et; 1060 ces->ces_flags |= CES_SOURCE_VALID; 1061 break; 1062 } 1063 } 1064 1065 if (!(ces->ces_flags & CES_SOURCE_VALID)) 1066 printf("ch: warning: could not map element source " 1067 "address %ud to a valid element type", 1068 eaddr); 1069 } 1070 1071 1072 if (flags & READ_ELEMENT_STATUS_PVOLTAG) 1073 copy_voltag(&(ces->ces_pvoltag), &(desc->pvoltag)); 1074 if (flags & READ_ELEMENT_STATUS_AVOLTAG) 1075 copy_voltag(&(ces->ces_avoltag), &(desc->avoltag)); 1076 1077 if (desc->dt_scsi_flags & READ_ELEMENT_STATUS_DT_IDVALID) { 1078 ces->ces_flags |= CES_SCSIID_VALID; 1079 ces->ces_scsi_id = desc->dt_scsi_addr; 1080 } 1081 1082 if (desc->dt_scsi_addr & READ_ELEMENT_STATUS_DT_LUVALID) { 1083 ces->ces_flags |= CES_LUN_VALID; 1084 ces->ces_scsi_lun = 1085 desc->dt_scsi_flags & READ_ELEMENT_STATUS_DT_LUNMASK; 1086 } 1087 } 1088 1089 static int 1090 chgetelemstatus(struct cam_periph *periph, 1091 struct changer_element_status_request *cesr) 1092 { 1093 struct read_element_status_header *st_hdr; 1094 struct read_element_status_page_header *pg_hdr; 1095 struct read_element_status_descriptor *desc; 1096 caddr_t data = NULL; 1097 size_t size, desclen; 1098 int avail, i, error = 0; 1099 struct changer_element_status *user_data = NULL; 1100 struct ch_softc *softc; 1101 union ccb *ccb; 1102 int chet = cesr->cesr_element_type; 1103 int want_voltags = (cesr->cesr_flags & CESR_VOLTAGS) ? 1 : 0; 1104 1105 softc = (struct ch_softc *)periph->softc; 1106 1107 /* perform argument checking */ 1108 1109 /* 1110 * Perform a range check on the cesr_element_{base,count} 1111 * request argument fields. 1112 */ 1113 if ((softc->sc_counts[chet] - cesr->cesr_element_base) <= 0 1114 || (cesr->cesr_element_base + cesr->cesr_element_count) 1115 > softc->sc_counts[chet]) 1116 return (EINVAL); 1117 1118 /* 1119 * Request one descriptor for the given element type. This 1120 * is used to determine the size of the descriptor so that 1121 * we can allocate enough storage for all of them. We assume 1122 * that the first one can fit into 1k. 1123 */ 1124 data = (caddr_t)malloc(1024, M_DEVBUF, M_WAITOK); 1125 1126 ccb = cam_periph_getccb(periph, /*priority*/ 1); 1127 1128 scsi_read_element_status(&ccb->csio, 1129 /* retries */ 1, 1130 /* cbfcnp */ chdone, 1131 /* tag_action */ MSG_SIMPLE_Q_TAG, 1132 /* voltag */ want_voltags, 1133 /* sea */ softc->sc_firsts[chet], 1134 /* count */ 1, 1135 /* data_ptr */ data, 1136 /* dxfer_len */ 1024, 1137 /* sense_len */ SSD_FULL_SIZE, 1138 /* timeout */ CH_TIMEOUT_READ_ELEMENT_STATUS); 1139 1140 error = cam_periph_runccb(ccb, cherror, /*cam_flags*/ 0, 1141 /* sense_flags */ SF_RETRY_UA, 1142 &softc->device_stats); 1143 1144 if (error) 1145 goto done; 1146 1147 st_hdr = (struct read_element_status_header *)data; 1148 pg_hdr = (struct read_element_status_page_header *)((u_long)st_hdr + 1149 sizeof(struct read_element_status_header)); 1150 desclen = scsi_2btoul(pg_hdr->edl); 1151 1152 size = sizeof(struct read_element_status_header) + 1153 sizeof(struct read_element_status_page_header) + 1154 (desclen * cesr->cesr_element_count); 1155 1156 /* 1157 * Reallocate storage for descriptors and get them from the 1158 * device. 1159 */ 1160 free(data, M_DEVBUF); 1161 data = (caddr_t)malloc(size, M_DEVBUF, M_WAITOK); 1162 1163 scsi_read_element_status(&ccb->csio, 1164 /* retries */ 1, 1165 /* cbfcnp */ chdone, 1166 /* tag_action */ MSG_SIMPLE_Q_TAG, 1167 /* voltag */ want_voltags, 1168 /* sea */ softc->sc_firsts[chet] 1169 + cesr->cesr_element_base, 1170 /* count */ cesr->cesr_element_count, 1171 /* data_ptr */ data, 1172 /* dxfer_len */ size, 1173 /* sense_len */ SSD_FULL_SIZE, 1174 /* timeout */ CH_TIMEOUT_READ_ELEMENT_STATUS); 1175 1176 error = cam_periph_runccb(ccb, cherror, /*cam_flags*/ 0, 1177 /* sense_flags */ SF_RETRY_UA, 1178 &softc->device_stats); 1179 1180 if (error) 1181 goto done; 1182 1183 /* 1184 * Fill in the user status array. 1185 */ 1186 st_hdr = (struct read_element_status_header *)data; 1187 avail = scsi_2btoul(st_hdr->count); 1188 1189 if (avail != cesr->cesr_element_count) { 1190 xpt_print_path(periph->path); 1191 printf("warning, READ ELEMENT STATUS avail != count\n"); 1192 } 1193 1194 user_data = (struct changer_element_status *) 1195 malloc(avail * sizeof(struct changer_element_status), 1196 M_DEVBUF, M_WAITOK); 1197 bzero(user_data, avail * sizeof(struct changer_element_status)); 1198 1199 desc = (struct read_element_status_descriptor *)((u_long)data + 1200 sizeof(struct read_element_status_header) + 1201 sizeof(struct read_element_status_page_header)); 1202 /* 1203 * Set up the individual element status structures 1204 */ 1205 for (i = 0; i < avail; ++i) { 1206 struct changer_element_status *ces = &(user_data[i]); 1207 1208 copy_element_status(softc, pg_hdr->flags, desc, ces); 1209 1210 (u_long)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, 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, 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 |SF_NO_PRINT, 1408 &softc->device_stats); 1409 1410 if (error) { 1411 if (dbd) { 1412 struct scsi_mode_sense_6 *sms; 1413 1414 sms = (struct scsi_mode_sense_6 *) 1415 ccb->csio.cdb_io.cdb_bytes; 1416 1417 sms->byte2 &= ~SMS_DBD; 1418 error = cam_periph_runccb(ccb, cherror, /*cam_flags*/ 0, 1419 /* sense_flags */ SF_RETRY_UA, 1420 &softc->device_stats); 1421 } else { 1422 /* 1423 * Since we disabled sense printing above, print 1424 * out the sense here since we got an error. 1425 */ 1426 scsi_sense_print(&ccb->csio); 1427 } 1428 1429 if (error) { 1430 xpt_print_path(periph->path); 1431 printf("chgetparams: error getting element " 1432 "address page\n"); 1433 xpt_release_ccb(ccb); 1434 free(mode_buffer, M_TEMP); 1435 return(error); 1436 } 1437 } 1438 1439 ea = (struct page_element_address_assignment *) 1440 find_mode_page_6((struct scsi_mode_header_6 *)mode_buffer); 1441 1442 softc->sc_firsts[CHET_MT] = scsi_2btoul(ea->mtea); 1443 softc->sc_counts[CHET_MT] = scsi_2btoul(ea->nmte); 1444 softc->sc_firsts[CHET_ST] = scsi_2btoul(ea->fsea); 1445 softc->sc_counts[CHET_ST] = scsi_2btoul(ea->nse); 1446 softc->sc_firsts[CHET_IE] = scsi_2btoul(ea->fieea); 1447 softc->sc_counts[CHET_IE] = scsi_2btoul(ea->niee); 1448 softc->sc_firsts[CHET_DT] = scsi_2btoul(ea->fdtea); 1449 softc->sc_counts[CHET_DT] = scsi_2btoul(ea->ndte); 1450 1451 bzero(mode_buffer, mode_buffer_len); 1452 1453 /* 1454 * Now get the device capabilities page. 1455 */ 1456 scsi_mode_sense(&ccb->csio, 1457 /* retries */ 1, 1458 /* cbfcnp */ chdone, 1459 /* tag_action */ MSG_SIMPLE_Q_TAG, 1460 /* dbd */ dbd, 1461 /* page_code */ SMS_PAGE_CTRL_CURRENT, 1462 /* page */ CH_DEVICE_CAP_PAGE, 1463 /* param_buf */ (u_int8_t *)mode_buffer, 1464 /* param_len */ mode_buffer_len, 1465 /* sense_len */ SSD_FULL_SIZE, 1466 /* timeout */ CH_TIMEOUT_MODE_SENSE); 1467 1468 error = cam_periph_runccb(ccb, cherror, /*cam_flags*/ 0, 1469 /* sense_flags */ SF_RETRY_UA|SF_NO_PRINT, 1470 &softc->device_stats); 1471 1472 if (error) { 1473 if (dbd) { 1474 struct scsi_mode_sense_6 *sms; 1475 1476 sms = (struct scsi_mode_sense_6 *) 1477 ccb->csio.cdb_io.cdb_bytes; 1478 1479 sms->byte2 &= ~SMS_DBD; 1480 error = cam_periph_runccb(ccb, cherror, /*cam_flags*/ 0, 1481 /* sense_flags */ SF_RETRY_UA, 1482 &softc->device_stats); 1483 } else { 1484 /* 1485 * Since we disabled sense printing above, print 1486 * out the sense here since we got an error. 1487 */ 1488 scsi_sense_print(&ccb->csio); 1489 } 1490 1491 if (error) { 1492 xpt_print_path(periph->path); 1493 printf("chgetparams: error getting device " 1494 "capabilities page\n"); 1495 xpt_release_ccb(ccb); 1496 free(mode_buffer, M_TEMP); 1497 return(error); 1498 } 1499 } 1500 1501 xpt_release_ccb(ccb); 1502 1503 cap = (struct page_device_capabilities *) 1504 find_mode_page_6((struct scsi_mode_header_6 *)mode_buffer); 1505 1506 bzero(softc->sc_movemask, sizeof(softc->sc_movemask)); 1507 bzero(softc->sc_exchangemask, sizeof(softc->sc_exchangemask)); 1508 moves = &cap->move_from_mt; 1509 exchanges = &cap->exchange_with_mt; 1510 for (from = CHET_MT; from <= CHET_DT; ++from) { 1511 softc->sc_movemask[from] = moves[from]; 1512 softc->sc_exchangemask[from] = exchanges[from]; 1513 } 1514 1515 free(mode_buffer, M_TEMP); 1516 1517 return(error); 1518 } 1519 1520 void 1521 scsi_move_medium(struct ccb_scsiio *csio, u_int32_t retries, 1522 void (*cbfcnp)(struct cam_periph *, union ccb *), 1523 u_int8_t tag_action, u_int32_t tea, u_int32_t src, 1524 u_int32_t dst, int invert, u_int8_t sense_len, 1525 u_int32_t timeout) 1526 { 1527 struct scsi_move_medium *scsi_cmd; 1528 1529 scsi_cmd = (struct scsi_move_medium *)&csio->cdb_io.cdb_bytes; 1530 bzero(scsi_cmd, sizeof(*scsi_cmd)); 1531 1532 scsi_cmd->opcode = MOVE_MEDIUM; 1533 1534 scsi_ulto2b(tea, scsi_cmd->tea); 1535 scsi_ulto2b(src, scsi_cmd->src); 1536 scsi_ulto2b(dst, scsi_cmd->dst); 1537 1538 if (invert) 1539 scsi_cmd->invert |= MOVE_MEDIUM_INVERT; 1540 1541 cam_fill_csio(csio, 1542 retries, 1543 cbfcnp, 1544 /*flags*/ CAM_DIR_NONE, 1545 tag_action, 1546 /*data_ptr*/ NULL, 1547 /*dxfer_len*/ 0, 1548 sense_len, 1549 sizeof(*scsi_cmd), 1550 timeout); 1551 } 1552 1553 void 1554 scsi_exchange_medium(struct ccb_scsiio *csio, u_int32_t retries, 1555 void (*cbfcnp)(struct cam_periph *, union ccb *), 1556 u_int8_t tag_action, u_int32_t tea, u_int32_t src, 1557 u_int32_t dst1, u_int32_t dst2, int invert1, 1558 int invert2, u_int8_t sense_len, u_int32_t timeout) 1559 { 1560 struct scsi_exchange_medium *scsi_cmd; 1561 1562 scsi_cmd = (struct scsi_exchange_medium *)&csio->cdb_io.cdb_bytes; 1563 bzero(scsi_cmd, sizeof(*scsi_cmd)); 1564 1565 scsi_cmd->opcode = EXCHANGE_MEDIUM; 1566 1567 scsi_ulto2b(tea, scsi_cmd->tea); 1568 scsi_ulto2b(src, scsi_cmd->src); 1569 scsi_ulto2b(dst1, scsi_cmd->fdst); 1570 scsi_ulto2b(dst2, scsi_cmd->sdst); 1571 1572 if (invert1) 1573 scsi_cmd->invert |= EXCHANGE_MEDIUM_INV1; 1574 1575 if (invert2) 1576 scsi_cmd->invert |= EXCHANGE_MEDIUM_INV2; 1577 1578 cam_fill_csio(csio, 1579 retries, 1580 cbfcnp, 1581 /*flags*/ CAM_DIR_NONE, 1582 tag_action, 1583 /*data_ptr*/ NULL, 1584 /*dxfer_len*/ 0, 1585 sense_len, 1586 sizeof(*scsi_cmd), 1587 timeout); 1588 } 1589 1590 void 1591 scsi_position_to_element(struct ccb_scsiio *csio, u_int32_t retries, 1592 void (*cbfcnp)(struct cam_periph *, union ccb *), 1593 u_int8_t tag_action, u_int32_t tea, u_int32_t dst, 1594 int invert, u_int8_t sense_len, u_int32_t timeout) 1595 { 1596 struct scsi_position_to_element *scsi_cmd; 1597 1598 scsi_cmd = (struct scsi_position_to_element *)&csio->cdb_io.cdb_bytes; 1599 bzero(scsi_cmd, sizeof(*scsi_cmd)); 1600 1601 scsi_cmd->opcode = POSITION_TO_ELEMENT; 1602 1603 scsi_ulto2b(tea, scsi_cmd->tea); 1604 scsi_ulto2b(dst, scsi_cmd->dst); 1605 1606 if (invert) 1607 scsi_cmd->invert |= POSITION_TO_ELEMENT_INVERT; 1608 1609 cam_fill_csio(csio, 1610 retries, 1611 cbfcnp, 1612 /*flags*/ CAM_DIR_NONE, 1613 tag_action, 1614 /*data_ptr*/ NULL, 1615 /*dxfer_len*/ 0, 1616 sense_len, 1617 sizeof(*scsi_cmd), 1618 timeout); 1619 } 1620 1621 void 1622 scsi_read_element_status(struct ccb_scsiio *csio, u_int32_t retries, 1623 void (*cbfcnp)(struct cam_periph *, union ccb *), 1624 u_int8_t tag_action, int voltag, u_int32_t sea, 1625 u_int32_t count, u_int8_t *data_ptr, 1626 u_int32_t dxfer_len, u_int8_t sense_len, 1627 u_int32_t timeout) 1628 { 1629 struct scsi_read_element_status *scsi_cmd; 1630 1631 scsi_cmd = (struct scsi_read_element_status *)&csio->cdb_io.cdb_bytes; 1632 bzero(scsi_cmd, sizeof(*scsi_cmd)); 1633 1634 scsi_cmd->opcode = READ_ELEMENT_STATUS; 1635 1636 scsi_ulto2b(sea, scsi_cmd->sea); 1637 scsi_ulto2b(count, scsi_cmd->count); 1638 scsi_ulto3b(dxfer_len, scsi_cmd->len); 1639 1640 if (voltag) 1641 scsi_cmd->byte2 |= READ_ELEMENT_STATUS_VOLTAG; 1642 1643 cam_fill_csio(csio, 1644 retries, 1645 cbfcnp, 1646 /*flags*/ CAM_DIR_IN, 1647 tag_action, 1648 data_ptr, 1649 dxfer_len, 1650 sense_len, 1651 sizeof(*scsi_cmd), 1652 timeout); 1653 } 1654 1655 void 1656 scsi_initialize_element_status(struct ccb_scsiio *csio, u_int32_t retries, 1657 void (*cbfcnp)(struct cam_periph *, union ccb *), 1658 u_int8_t tag_action, u_int8_t sense_len, 1659 u_int32_t timeout) 1660 { 1661 struct scsi_initialize_element_status *scsi_cmd; 1662 1663 scsi_cmd = (struct scsi_initialize_element_status *) 1664 &csio->cdb_io.cdb_bytes; 1665 bzero(scsi_cmd, sizeof(*scsi_cmd)); 1666 1667 scsi_cmd->opcode = INITIALIZE_ELEMENT_STATUS; 1668 1669 cam_fill_csio(csio, 1670 retries, 1671 cbfcnp, 1672 /*flags*/ CAM_DIR_NONE, 1673 tag_action, 1674 /* data_ptr */ NULL, 1675 /* dxfer_len */ 0, 1676 sense_len, 1677 sizeof(*scsi_cmd), 1678 timeout); 1679 } 1680 1681 void 1682 scsi_send_volume_tag(struct ccb_scsiio *csio, u_int32_t retries, 1683 void (*cbfcnp)(struct cam_periph *, union ccb *), 1684 u_int8_t tag_action, 1685 u_int16_t element_address, 1686 u_int8_t send_action_code, 1687 struct scsi_send_volume_tag_parameters *parameters, 1688 u_int8_t sense_len, u_int32_t timeout) 1689 { 1690 struct scsi_send_volume_tag *scsi_cmd; 1691 1692 scsi_cmd = (struct scsi_send_volume_tag *) &csio->cdb_io.cdb_bytes; 1693 bzero(scsi_cmd, sizeof(*scsi_cmd)); 1694 1695 scsi_cmd->opcode = SEND_VOLUME_TAG; 1696 scsi_ulto2b(element_address, scsi_cmd->ea); 1697 scsi_cmd->sac = send_action_code; 1698 scsi_ulto2b(sizeof(*parameters), scsi_cmd->pll); 1699 1700 cam_fill_csio(csio, 1701 retries, 1702 cbfcnp, 1703 /*flags*/ CAM_DIR_OUT, 1704 tag_action, 1705 /* data_ptr */ (u_int8_t *) parameters, 1706 sizeof(*parameters), 1707 sense_len, 1708 sizeof(*scsi_cmd), 1709 timeout); 1710 } 1711