1 /* $OpenBSD: ch.c,v 1.6 1996/08/12 10:21:41 deraadt Exp $ */ 2 /* $NetBSD: ch.c,v 1.21 1996/04/19 00:02:29 christos Exp $ */ 3 4 /* 5 * Copyright (c) 1996 Jason R. Thorpe <thorpej@and.com> 6 * All rights reserved. 7 * 8 * Partially based on an autochanger driver written by Stefan Grefen 9 * and on an autochanger driver written by the Systems Programming Group 10 * at the University of Utah Computer Science Department. 11 * 12 * Redistribution and use in source and binary forms, with or without 13 * modification, are permitted provided that the following conditions 14 * are met: 15 * 1. Redistributions of source code must retain the above copyright 16 * notice, this list of conditions and the following disclaimer. 17 * 2. Redistributions in binary form must reproduce the above copyright 18 * notice, this list of conditions and the following disclaimer in the 19 * documentation and/or other materials provided with the distribution. 20 * 3. All advertising materials mentioning features or use of this software 21 * must display the following acknowledgements: 22 * This product includes software developed by Jason R. Thorpe 23 * for And Communications, http://www.and.com/ 24 * 4. The name of the author may not be used to endorse or promote products 25 * derived from this software without specific prior written permission. 26 * 27 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 28 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 29 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 30 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 31 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 32 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 33 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 34 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 35 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 36 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 37 * SUCH DAMAGE. 38 */ 39 40 #include <sys/param.h> 41 #include <sys/systm.h> 42 #include <sys/errno.h> 43 #include <sys/ioctl.h> 44 #include <sys/buf.h> 45 #include <sys/proc.h> 46 #include <sys/user.h> 47 #include <sys/chio.h> 48 #include <sys/device.h> 49 #include <sys/malloc.h> 50 #include <sys/conf.h> 51 52 #include <scsi/scsi_all.h> 53 #include <scsi/scsi_changer.h> 54 #include <scsi/scsiconf.h> 55 56 #define CHRETRIES 2 57 #define CHUNIT(x) (minor((x))) 58 59 struct ch_softc { 60 struct device sc_dev; /* generic device info */ 61 struct scsi_link *sc_link; /* link in the SCSI bus */ 62 63 int sc_picker; /* current picker */ 64 65 /* 66 * The following information is obtained from the 67 * element address assignment page. 68 */ 69 int sc_firsts[4]; /* firsts, indexed by CHET_* */ 70 int sc_counts[4]; /* counts, indexed by CHET_* */ 71 72 /* 73 * The following mask defines the legal combinations 74 * of elements for the MOVE MEDIUM command. 75 */ 76 u_int8_t sc_movemask[4]; 77 78 /* 79 * As above, but for EXCHANGE MEDIUM. 80 */ 81 u_int8_t sc_exchangemask[4]; 82 83 int flags; /* misc. info */ 84 }; 85 86 /* sc_flags */ 87 #define CHF_ROTATE 0x01 /* picker can rotate */ 88 89 /* Autoconfiguration glue */ 90 int chmatch __P((struct device *, void *, void *)); 91 void chattach __P((struct device *, struct device *, void *)); 92 93 struct cfattach ch_ca = { 94 sizeof(struct ch_softc), chmatch, chattach 95 }; 96 97 struct cfdriver ch_cd = { 98 NULL, "ch", DV_DULL 99 }; 100 101 struct scsi_inquiry_pattern ch_patterns[] = { 102 {T_CHANGER, T_REMOV, 103 "", "", ""}, 104 }; 105 106 /* SCSI glue */ 107 struct scsi_device ch_switch = { 108 NULL, NULL, NULL, NULL 109 }; 110 111 int ch_move __P((struct ch_softc *, struct changer_move *)); 112 int ch_exchange __P((struct ch_softc *, struct changer_exchange *)); 113 int ch_position __P((struct ch_softc *, struct changer_position *)); 114 int ch_usergetelemstatus __P((struct ch_softc *, int, u_int8_t *)); 115 int ch_getelemstatus __P((struct ch_softc *, int, int, caddr_t, size_t)); 116 int ch_get_params __P((struct ch_softc *, int)); 117 118 int 119 chmatch(parent, match, aux) 120 struct device *parent; 121 void *match, *aux; 122 { 123 struct scsibus_attach_args *sa = aux; 124 int priority; 125 126 (void)scsi_inqmatch(sa->sa_inqbuf, 127 (caddr_t)ch_patterns, sizeof(ch_patterns)/sizeof(ch_patterns[0]), 128 sizeof(ch_patterns[0]), &priority); 129 130 return (priority); 131 } 132 133 void 134 chattach(parent, self, aux) 135 struct device *parent, *self; 136 void *aux; 137 { 138 struct ch_softc *sc = (struct ch_softc *)self; 139 struct scsibus_attach_args *sa = aux; 140 struct scsi_link *link = sa->sa_sc_link; 141 142 /* Glue into the SCSI bus */ 143 sc->sc_link = link; 144 link->device = &ch_switch; 145 link->device_softc = sc; 146 link->openings = 1; 147 148 printf("\n"); 149 150 /* 151 * Get information about the device. Note we can't use 152 * interrupts yet. 153 */ 154 if (ch_get_params(sc, SCSI_AUTOCONF)) 155 printf("%s: offline\n", sc->sc_dev.dv_xname); 156 else { 157 printf("%s: %d slot%s, %d drive%s, %d picker%s", 158 sc->sc_dev.dv_xname, 159 sc->sc_counts[CHET_ST], (sc->sc_counts[CHET_ST] > 1) ? 160 "s" : "", 161 sc->sc_counts[CHET_DT], (sc->sc_counts[CHET_DT] > 1) ? 162 "s" : "", 163 sc->sc_counts[CHET_MT], (sc->sc_counts[CHET_MT] > 1) ? 164 "s" : ""); 165 if (sc->sc_counts[CHET_IE]) 166 printf(", %d portal%s", sc->sc_counts[CHET_IE], 167 (sc->sc_counts[CHET_IE] > 1) ? "s" : ""); 168 printf("\n"); 169 #ifdef CHANGER_DEBUG 170 printf("%s: move mask: 0x%x 0x%x 0x%x 0x%x\n", 171 sc->sc_dev.dv_xname, 172 sc->sc_movemask[CHET_MT], sc->sc_movemask[CHET_ST], 173 sc->sc_movemask[CHET_IE], sc->sc_movemask[CHET_DT]); 174 printf("%s: exchange mask: 0x%x 0x%x 0x%x 0x%x\n", 175 sc->sc_dev.dv_xname, 176 sc->sc_exchangemask[CHET_MT], sc->sc_exchangemask[CHET_ST], 177 sc->sc_exchangemask[CHET_IE], sc->sc_exchangemask[CHET_DT]); 178 #endif /* CHANGER_DEBUG */ 179 } 180 181 /* Default the current picker. */ 182 sc->sc_picker = sc->sc_firsts[CHET_MT]; 183 } 184 185 int 186 chopen(dev, flags, fmt, p) 187 dev_t dev; 188 int flags, fmt; 189 struct proc *p; 190 { 191 struct ch_softc *sc; 192 int unit, error = 0; 193 194 unit = CHUNIT(dev); 195 if ((unit >= ch_cd.cd_ndevs) || 196 ((sc = ch_cd.cd_devs[unit]) == NULL)) 197 return (ENXIO); 198 199 /* 200 * Only allow one open at a time. 201 */ 202 if (sc->sc_link->flags & SDEV_OPEN) 203 return (EBUSY); 204 205 sc->sc_link->flags |= SDEV_OPEN; 206 207 /* 208 * Absorb any unit attention errors. Ignore "not ready" 209 * since this might occur if e.g. a tape isn't actually 210 * loaded in the drive. 211 */ 212 error = scsi_test_unit_ready(sc->sc_link, 213 SCSI_IGNORE_NOT_READY|SCSI_IGNORE_MEDIA_CHANGE); 214 if (error) 215 goto bad; 216 217 /* 218 * Make sure our parameters are up to date. 219 */ 220 if ((error = ch_get_params(sc, 0)) != 0) 221 goto bad; 222 223 return (0); 224 225 bad: 226 sc->sc_link->flags &= ~SDEV_OPEN; 227 return (error); 228 } 229 230 int 231 chclose(dev, flags, fmt, p) 232 dev_t dev; 233 int flags, fmt; 234 struct proc *p; 235 { 236 struct ch_softc *sc = ch_cd.cd_devs[CHUNIT(dev)]; 237 238 sc->sc_link->flags &= ~SDEV_OPEN; 239 return (0); 240 } 241 242 int 243 chioctl(dev, cmd, data, flags, p) 244 dev_t dev; 245 u_long cmd; 246 caddr_t data; 247 int flags; 248 struct proc *p; 249 { 250 struct ch_softc *sc = ch_cd.cd_devs[CHUNIT(dev)]; 251 int error = 0; 252 253 switch (cmd) { 254 case CHIOMOVE: 255 error = ch_move(sc, (struct changer_move *)data); 256 break; 257 258 case CHIOEXCHANGE: 259 error = ch_exchange(sc, (struct changer_exchange *)data); 260 break; 261 262 case CHIOPOSITION: 263 error = ch_position(sc, (struct changer_position *)data); 264 break; 265 266 case CHIOGPICKER: 267 *(int *)data = sc->sc_picker - sc->sc_firsts[CHET_MT]; 268 break; 269 270 case CHIOSPICKER: { 271 int new_picker = *(int *)data; 272 273 if (new_picker > (sc->sc_counts[CHET_MT] - 1)) 274 return (EINVAL); 275 sc->sc_picker = sc->sc_firsts[CHET_MT] + new_picker; 276 break; } 277 278 case CHIOGPARAMS: { 279 struct changer_params *cp = (struct changer_params *)data; 280 281 cp->cp_curpicker = sc->sc_picker - sc->sc_firsts[CHET_MT]; 282 cp->cp_npickers = sc->sc_counts[CHET_MT]; 283 cp->cp_nslots = sc->sc_counts[CHET_ST]; 284 cp->cp_nportals = sc->sc_counts[CHET_IE]; 285 cp->cp_ndrives = sc->sc_counts[CHET_DT]; 286 break; } 287 288 case CHIOGSTATUS: { 289 struct changer_element_status *ces = 290 (struct changer_element_status *)data; 291 292 error = ch_usergetelemstatus(sc, ces->ces_type, ces->ces_data); 293 break; } 294 295 /* Implement prevent/allow? */ 296 297 default: 298 error = scsi_do_safeioctl(sc->sc_link, dev, cmd, data, 299 flags, p); 300 break; 301 } 302 303 return (error); 304 } 305 306 int 307 ch_move(sc, cm) 308 struct ch_softc *sc; 309 struct changer_move *cm; 310 { 311 struct scsi_move_medium cmd; 312 u_int16_t fromelem, toelem; 313 314 /* 315 * Check arguments. 316 */ 317 if ((cm->cm_fromtype > CHET_DT) || (cm->cm_totype > CHET_DT)) 318 return (EINVAL); 319 if ((cm->cm_fromunit > (sc->sc_counts[cm->cm_fromtype] - 1)) || 320 (cm->cm_tounit > (sc->sc_counts[cm->cm_totype] - 1))) 321 return (ENODEV); 322 323 /* 324 * Check the request against the changer's capabilities. 325 */ 326 if ((sc->sc_movemask[cm->cm_fromtype] & (1 << cm->cm_totype)) == 0) 327 return (EINVAL); 328 329 /* 330 * Calculate the source and destination elements. 331 */ 332 fromelem = sc->sc_firsts[cm->cm_fromtype] + cm->cm_fromunit; 333 toelem = sc->sc_firsts[cm->cm_totype] + cm->cm_tounit; 334 335 /* 336 * Build the SCSI command. 337 */ 338 bzero(&cmd, sizeof(cmd)); 339 cmd.opcode = MOVE_MEDIUM; 340 _lto2b(sc->sc_picker, cmd.tea); 341 _lto2b(fromelem, cmd.src); 342 _lto2b(toelem, cmd.dst); 343 if (cm->cm_flags & CM_INVERT) 344 cmd.flags |= MOVE_MEDIUM_INVERT; 345 346 /* 347 * Send command to changer. 348 */ 349 return (scsi_scsi_cmd(sc->sc_link, (struct scsi_generic *)&cmd, 350 sizeof(cmd), NULL, 0, CHRETRIES, 100000, NULL, 0)); 351 } 352 353 int 354 ch_exchange(sc, ce) 355 struct ch_softc *sc; 356 struct changer_exchange *ce; 357 { 358 struct scsi_exchange_medium cmd; 359 u_int16_t src, dst1, dst2; 360 361 /* 362 * Check arguments. 363 */ 364 if ((ce->ce_srctype > CHET_DT) || (ce->ce_fdsttype > CHET_DT) || 365 (ce->ce_sdsttype > CHET_DT)) 366 return (EINVAL); 367 if ((ce->ce_srcunit > (sc->sc_counts[ce->ce_srctype] - 1)) || 368 (ce->ce_fdstunit > (sc->sc_counts[ce->ce_fdsttype] - 1)) || 369 (ce->ce_sdstunit > (sc->sc_counts[ce->ce_sdsttype] - 1))) 370 return (ENODEV); 371 372 /* 373 * Check the request against the changer's capabilities. 374 */ 375 if (((sc->sc_exchangemask[ce->ce_srctype] & 376 (1 << ce->ce_fdsttype)) == 0) || 377 ((sc->sc_exchangemask[ce->ce_fdsttype] & 378 (1 << ce->ce_sdsttype)) == 0)) 379 return (EINVAL); 380 381 /* 382 * Calculate the source and destination elements. 383 */ 384 src = sc->sc_firsts[ce->ce_srctype] + ce->ce_srcunit; 385 dst1 = sc->sc_firsts[ce->ce_fdsttype] + ce->ce_fdstunit; 386 dst2 = sc->sc_firsts[ce->ce_sdsttype] + ce->ce_sdstunit; 387 388 /* 389 * Build the SCSI command. 390 */ 391 bzero(&cmd, sizeof(cmd)); 392 cmd.opcode = EXCHANGE_MEDIUM; 393 _lto2b(sc->sc_picker, cmd.tea); 394 _lto2b(src, cmd.src); 395 _lto2b(dst1, cmd.fdst); 396 _lto2b(dst2, cmd.sdst); 397 if (ce->ce_flags & CE_INVERT1) 398 cmd.flags |= EXCHANGE_MEDIUM_INV1; 399 if (ce->ce_flags & CE_INVERT2) 400 cmd.flags |= EXCHANGE_MEDIUM_INV2; 401 402 /* 403 * Send command to changer. 404 */ 405 return (scsi_scsi_cmd(sc->sc_link, (struct scsi_generic *)&cmd, 406 sizeof(cmd), NULL, 0, CHRETRIES, 100000, NULL, 0)); 407 } 408 409 int 410 ch_position(sc, cp) 411 struct ch_softc *sc; 412 struct changer_position *cp; 413 { 414 struct scsi_position_to_element cmd; 415 u_int16_t dst; 416 417 /* 418 * Check arguments. 419 */ 420 if (cp->cp_type > CHET_DT) 421 return (EINVAL); 422 if (cp->cp_unit > (sc->sc_counts[cp->cp_type] - 1)) 423 return (ENODEV); 424 425 /* 426 * Calculate the destination element. 427 */ 428 dst = sc->sc_firsts[cp->cp_type] + cp->cp_unit; 429 430 /* 431 * Build the SCSI command. 432 */ 433 bzero(&cmd, sizeof(cmd)); 434 cmd.opcode = POSITION_TO_ELEMENT; 435 _lto2b(sc->sc_picker, cmd.tea); 436 _lto2b(dst, cmd.dst); 437 if (cp->cp_flags & CP_INVERT) 438 cmd.flags |= POSITION_TO_ELEMENT_INVERT; 439 440 /* 441 * Send command to changer. 442 */ 443 return (scsi_scsi_cmd(sc->sc_link, (struct scsi_generic *)&cmd, 444 sizeof(cmd), NULL, 0, CHRETRIES, 100000, NULL, 0)); 445 } 446 447 /* 448 * Perform a READ ELEMENT STATUS on behalf of the user, and return to 449 * the user only the data the user is interested in (i.e. an array of 450 * flags bytes). 451 */ 452 int 453 ch_usergetelemstatus(sc, chet, uptr) 454 struct ch_softc *sc; 455 int chet; 456 u_int8_t *uptr; 457 { 458 struct read_element_status_header *st_hdr; 459 struct read_element_status_page_header *pg_hdr; 460 struct read_element_status_descriptor *desc; 461 caddr_t data = NULL; 462 size_t size, desclen; 463 int avail, i, error = 0; 464 u_int8_t *user_data = NULL; 465 466 /* 467 * If there are no elements of the requested type in the changer, 468 * the request is invalid. 469 */ 470 if (sc->sc_counts[chet] == 0) 471 return (EINVAL); 472 473 /* 474 * Request one descriptor for the given element type. This 475 * is used to determine the size of the descriptor so that 476 * we can allocate enough storage for all of them. We assume 477 * that the first one can fit into 1k. 478 */ 479 data = (caddr_t)malloc(1024, M_DEVBUF, M_WAITOK); 480 error = ch_getelemstatus(sc, sc->sc_firsts[chet], 1, data, 1024); 481 if (error) 482 goto done; 483 484 st_hdr = (struct read_element_status_header *)data; 485 pg_hdr = (struct read_element_status_page_header *)((u_long)st_hdr + 486 sizeof(struct read_element_status_header)); 487 desclen = _2btol(pg_hdr->edl); 488 489 size = sizeof(struct read_element_status_header) + 490 sizeof(struct read_element_status_page_header) + 491 (desclen * sc->sc_counts[chet]); 492 493 /* 494 * Reallocate storage for descriptors and get them from the 495 * device. 496 */ 497 free(data, M_DEVBUF); 498 data = (caddr_t)malloc(size, M_DEVBUF, M_WAITOK); 499 error = ch_getelemstatus(sc, sc->sc_firsts[chet], 500 sc->sc_counts[chet], data, size); 501 if (error) 502 goto done; 503 504 /* 505 * Fill in the user status array. 506 */ 507 st_hdr = (struct read_element_status_header *)data; 508 avail = _2btol(st_hdr->count); 509 if (avail != sc->sc_counts[chet]) 510 printf("%s: warning, READ ELEMENT STATUS avail != count\n", 511 sc->sc_dev.dv_xname); 512 513 user_data = (u_int8_t *)malloc(avail, M_DEVBUF, M_WAITOK); 514 515 desc = (struct read_element_status_descriptor *)((u_long)data + 516 sizeof(struct read_element_status_header) + 517 sizeof(struct read_element_status_page_header)); 518 for (i = 0; i < avail; ++i) { 519 user_data[i] = desc->flags1; 520 (u_long)desc += desclen; 521 } 522 523 /* Copy flags array out to userspace. */ 524 error = copyout(user_data, uptr, avail); 525 526 done: 527 if (data != NULL) 528 free(data, M_DEVBUF); 529 if (user_data != NULL) 530 free(user_data, M_DEVBUF); 531 return (error); 532 } 533 534 int 535 ch_getelemstatus(sc, first, count, data, datalen) 536 struct ch_softc *sc; 537 int first, count; 538 caddr_t data; 539 size_t datalen; 540 { 541 struct scsi_read_element_status cmd; 542 543 /* 544 * Build SCSI command. 545 */ 546 bzero(&cmd, sizeof(cmd)); 547 cmd.opcode = READ_ELEMENT_STATUS; 548 _lto2b(first, cmd.sea); 549 _lto2b(count, cmd.count); 550 _lto3b(datalen, cmd.len); 551 552 /* 553 * Send command to changer. 554 */ 555 return (scsi_scsi_cmd(sc->sc_link, (struct scsi_generic *)&cmd, 556 sizeof(cmd), (u_char *)data, datalen, CHRETRIES, 100000, NULL, 0)); 557 } 558 559 560 /* 561 * Ask the device about itself and fill in the parameters in our 562 * softc. 563 */ 564 int 565 ch_get_params(sc, scsiflags) 566 struct ch_softc *sc; 567 int scsiflags; 568 { 569 struct scsi_mode_sense cmd; 570 struct scsi_mode_sense_data { 571 struct scsi_mode_header header; 572 union { 573 struct page_element_address_assignment ea; 574 struct page_transport_geometry_parameters tg; 575 struct page_device_capabilities cap; 576 } pages; 577 } sense_data; 578 int error, from; 579 u_int8_t *moves, *exchanges; 580 581 /* 582 * Grab info from the element address assignment page. 583 */ 584 bzero(&cmd, sizeof(cmd)); 585 bzero(&sense_data, sizeof(sense_data)); 586 cmd.opcode = MODE_SENSE; 587 cmd.byte2 |= 0x08; /* disable block descriptors */ 588 cmd.page = 0x1d; 589 cmd.length = (sizeof(sense_data) & 0xff); 590 error = scsi_scsi_cmd(sc->sc_link, (struct scsi_generic *)&cmd, 591 sizeof(cmd), (u_char *)&sense_data, sizeof(sense_data), CHRETRIES, 592 6000, NULL, scsiflags | SCSI_DATA_IN); 593 if (error) { 594 printf("%s: could not sense element address page\n", 595 sc->sc_dev.dv_xname); 596 return (error); 597 } 598 599 sc->sc_firsts[CHET_MT] = _2btol(sense_data.pages.ea.mtea); 600 sc->sc_counts[CHET_MT] = _2btol(sense_data.pages.ea.nmte); 601 sc->sc_firsts[CHET_ST] = _2btol(sense_data.pages.ea.fsea); 602 sc->sc_counts[CHET_ST] = _2btol(sense_data.pages.ea.nse); 603 sc->sc_firsts[CHET_IE] = _2btol(sense_data.pages.ea.fieea); 604 sc->sc_counts[CHET_IE] = _2btol(sense_data.pages.ea.niee); 605 sc->sc_firsts[CHET_DT] = _2btol(sense_data.pages.ea.fdtea); 606 sc->sc_counts[CHET_DT] = _2btol(sense_data.pages.ea.ndte); 607 608 /* XXX ask for page trasport geom */ 609 610 /* 611 * Grab info from the capabilities page. 612 */ 613 bzero(&cmd, sizeof(cmd)); 614 bzero(&sense_data, sizeof(sense_data)); 615 cmd.opcode = MODE_SENSE; 616 cmd.byte2 |= 0x08; /* disable block descriptors */ 617 cmd.page = 0x1f; 618 cmd.length = (sizeof(sense_data) & 0xff); 619 error = scsi_scsi_cmd(sc->sc_link, (struct scsi_generic *)&cmd, 620 sizeof(cmd), (u_char *)&sense_data, sizeof(sense_data), CHRETRIES, 621 6000, NULL, scsiflags | SCSI_DATA_IN); 622 if (error) { 623 printf("%s: could not sense capabilities page\n", 624 sc->sc_dev.dv_xname); 625 return (error); 626 } 627 628 bzero(sc->sc_movemask, sizeof(sc->sc_movemask)); 629 bzero(sc->sc_exchangemask, sizeof(sc->sc_exchangemask)); 630 moves = &sense_data.pages.cap.move_from_mt; 631 exchanges = &sense_data.pages.cap.exchange_with_mt; 632 for (from = CHET_MT; from <= CHET_DT; ++from) { 633 sc->sc_movemask[from] = moves[from]; 634 sc->sc_exchangemask[from] = exchanges[from]; 635 } 636 637 sc->sc_link->flags |= SDEV_MEDIA_LOADED; 638 return (0); 639 } 640