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