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