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