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