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