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