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