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