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