1 /* $NetBSD: st.c,v 1.46 1994/12/30 05:23:49 mycroft Exp $ */ 2 3 /* 4 * Copyright (c) 1994 Charles Hannum. 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 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 3. All advertising materials mentioning features or use of this software 15 * must display the following acknowledgement: 16 * This product includes software developed by Charles Hannum. 17 * 4. The name of the author may not be used to endorse or promote products 18 * derived from this software without specific prior written permission. 19 * 20 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 21 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 22 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 23 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 24 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 25 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 29 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30 */ 31 32 /* 33 * Originally written by Julian Elischer (julian@tfs.com) 34 * for TRW Financial Systems for use under the MACH(2.5) operating system. 35 * 36 * TRW Financial Systems, in accordance with their agreement with Carnegie 37 * Mellon University, makes this software available to CMU to distribute 38 * or use in any manner that they see fit as long as this message is kept with 39 * the software. For this reason TFS also grants any other persons or 40 * organisations permission to use or modify this software. 41 * 42 * TFS supplies this software to be publicly redistributed 43 * on the understanding that TFS is not responsible for the correct 44 * functioning of this software in any circumstances. 45 * 46 * Ported to run under 386BSD by Julian Elischer (julian@tfs.com) Sept 1992 47 * major changes by Julian Elischer (julian@jules.dialix.oz.au) May 1993 48 */ 49 50 /* 51 * To do: 52 * work out some better way of guessing what a good timeout is going 53 * to be depending on whether we expect to retension or not. 54 */ 55 56 #include <sys/types.h> 57 #include <sys/param.h> 58 #include <sys/systm.h> 59 #include <sys/fcntl.h> 60 #include <sys/errno.h> 61 #include <sys/ioctl.h> 62 #include <sys/malloc.h> 63 #include <sys/buf.h> 64 #include <sys/proc.h> 65 #include <sys/user.h> 66 #include <sys/mtio.h> 67 #include <sys/device.h> 68 69 #include <scsi/scsi_all.h> 70 #include <scsi/scsi_tape.h> 71 #include <scsi/scsiconf.h> 72 73 /* Defines for device specific stuff */ 74 #define DEF_FIXED_BSIZE 512 75 #define ST_RETRIES 4 /* only on non IO commands */ 76 77 #define STMODE(z) ( minor(z) & 0x03) 78 #define STDSTY(z) ((minor(z) >> 2) & 0x03) 79 #define STUNIT(z) ((minor(z) >> 4) ) 80 #define CTLMODE 3 81 82 #define SCSI_2_MAX_DENSITY_CODE 0x17 /* maximum density code specified 83 * in SCSI II spec. */ 84 /* 85 * Define various devices that we know mis-behave in some way, 86 * and note how they are bad, so we can correct for them 87 */ 88 struct modes { 89 u_int quirks; /* same definitions as in quirkdata */ 90 int blksize; 91 u_int8_t density; 92 }; 93 94 struct quirkdata { 95 u_int quirks; 96 #define ST_Q_FORCE_BLKSIZE 0x0001 97 #define ST_Q_SENSE_HELP 0x0002 /* must do READ for good MODE SENSE */ 98 #define ST_Q_IGNORE_LOADS 0x0004 99 #define ST_Q_BLKSIZE 0x0008 /* variable-block media_blksize > 0 */ 100 u_int page_0_size; 101 #define MAX_PAGE_0_SIZE 64 102 struct modes modes[4]; 103 }; 104 105 struct st_quirk_inquiry_pattern { 106 struct scsi_inquiry_pattern pattern; 107 struct quirkdata quirkdata; 108 }; 109 110 struct st_quirk_inquiry_pattern st_quirk_patterns[] = { 111 {T_SEQUENTIAL, T_REMOV, 112 " ", " ", " ", 0, 0, { 113 ST_Q_FORCE_BLKSIZE, 512, 0, /* minor 0-3 */ 114 ST_Q_FORCE_BLKSIZE, 512, QIC_24, /* minor 4-7 */ 115 ST_Q_FORCE_BLKSIZE, 0, HALFINCH_1600, /* minor 8-11 */ 116 ST_Q_FORCE_BLKSIZE, 0, HALFINCH_6250 /* minor 12-15 */ 117 }}, 118 {T_SEQUENTIAL, T_REMOV, 119 "TANDBERG", " TDC 3600 ", "", 0, 12, { 120 0, 0, 0, /* minor 0-3 */ 121 ST_Q_FORCE_BLKSIZE, 0, QIC_525, /* minor 4-7 */ 122 0, 0, QIC_150, /* minor 8-11 */ 123 0, 0, QIC_120 /* minor 12-15 */ 124 }}, 125 /* 126 * At least -005 and -007 need this. I'll assume they all do unless I 127 * hear otherwise. - mycroft, 31MAR1994 128 */ 129 {T_SEQUENTIAL, T_REMOV, 130 "ARCHIVE ", "VIPER 2525 25462", "", 0, 0, { 131 ST_Q_SENSE_HELP, 0, 0, /* minor 0-3 */ 132 ST_Q_SENSE_HELP, 0, QIC_525, /* minor 4-7 */ 133 0, 0, QIC_150, /* minor 8-11 */ 134 0, 0, QIC_120 /* minor 12-15 */ 135 }}, 136 /* 137 * One user reports that this works for his tape drive. It probably 138 * needs more work. - mycroft, 09APR1994 139 */ 140 {T_SEQUENTIAL, T_REMOV, 141 "SANKYO ", "CP525 ", "", 0, 0, { 142 ST_Q_FORCE_BLKSIZE, 512, 0, /* minor 0-3 */ 143 ST_Q_FORCE_BLKSIZE, 512, QIC_525, /* minor 4-7 */ 144 0, 0, QIC_150, /* minor 8-11 */ 145 0, 0, QIC_120 /* minor 12-15 */ 146 }}, 147 {T_SEQUENTIAL, T_REMOV, 148 "ARCHIVE ", "VIPER 150 21247", "", 0, 12, { 149 0, 0, 0, /* minor 0-3 */ 150 0, 0, QIC_150, /* minor 4-7 */ 151 0, 0, QIC_120, /* minor 8-11 */ 152 0, 0, QIC_24 /* minor 12-15 */ 153 }}, 154 {T_SEQUENTIAL, T_REMOV, 155 "WANGTEK ", "5525ES SCSI REV7", "", 0, 0, { 156 0, 0, 0, /* minor 0-3 */ 157 ST_Q_BLKSIZE, 0, QIC_525, /* minor 4-7 */ 158 0, 0, QIC_150, /* minor 8-11 */ 159 0, 0, QIC_120 /* minor 12-15 */ 160 }}, 161 {T_SEQUENTIAL, T_REMOV, 162 "WangDAT ", "Model 1300 ", "", 0, 0, { 163 0, 0, 0, /* minor 0-3 */ 164 ST_Q_FORCE_BLKSIZE, 512, DDS, /* minor 4-7 */ 165 ST_Q_FORCE_BLKSIZE, 1024, DDS, /* minor 8-11 */ 166 ST_Q_FORCE_BLKSIZE, 0, DDS /* minor 12-15 */ 167 }}, 168 #if 0 169 {T_SEQUENTIAL, T_REMOV, 170 "EXABYTE ", "EXB-8200 ", "", 0, 12, { 171 0, 0, 0, /* minor 0-3 */ 172 0, 0, 0, /* minor 4-7 */ 173 0, 0, 0, /* minor 8-11 */ 174 0, 0, 0 /* minor 12-15 */ 175 }}, 176 #endif 177 }; 178 179 #define NOEJECT 0 180 #define EJECT 1 181 182 struct st_softc { 183 struct device sc_dev; 184 /*--------------------present operating parameters, flags etc.----------------*/ 185 int flags; /* see below */ 186 u_int quirks; /* quirks for the open mode */ 187 int blksize; /* blksize we are using */ 188 u_int8_t density; /* present density */ 189 u_int page_0_size; /* size of page 0 data */ 190 u_int last_dsty; /* last density opened */ 191 /*--------------------device/scsi parameters----------------------------------*/ 192 struct scsi_link *sc_link; /* our link to the adpter etc. */ 193 /*--------------------parameters reported by the device ----------------------*/ 194 int blkmin; /* min blk size */ 195 int blkmax; /* max blk size */ 196 struct quirkdata *quirkdata; /* if we have a rogue entry */ 197 /*--------------------parameters reported by the device for this media--------*/ 198 u_long numblks; /* nominal blocks capacity */ 199 int media_blksize; /* 0 if not ST_FIXEDBLOCKS */ 200 u_int8_t media_density; /* this is what it said when asked */ 201 /*--------------------quirks for the whole drive------------------------------*/ 202 u_int drive_quirks; /* quirks of this drive */ 203 /*--------------------How we should set up when opening each minor device----*/ 204 struct modes modes[4]; /* plus more for each mode */ 205 u_int8_t modeflags[4]; /* flags for the modes */ 206 #define DENSITY_SET_BY_USER 0x01 207 #define DENSITY_SET_BY_QUIRK 0x02 208 #define BLKSIZE_SET_BY_USER 0x04 209 #define BLKSIZE_SET_BY_QUIRK 0x08 210 /*--------------------storage for sense data returned by the drive------------*/ 211 u_char sense_data[MAX_PAGE_0_SIZE]; /* 212 * additional sense data needed 213 * for mode sense/select. 214 */ 215 struct buf buf_queue; /* the queue of pending IO operations */ 216 }; 217 218 int stmatch __P((struct device *, void *, void *)); 219 void stattach __P((struct device *, struct device *, void *)); 220 221 struct cfdriver stcd = { 222 NULL, "st", stmatch, stattach, DV_TAPE, sizeof(struct st_softc) 223 }; 224 225 int st_space __P((struct st_softc *, int number, u_int what, int flags)); 226 int st_rewind __P((struct st_softc *, u_int immediate, int flags)); 227 int st_mode_sense __P((struct st_softc *, int flags)); 228 int st_decide_mode __P((struct st_softc *, boolean first_read)); 229 int st_read_block_limits __P((struct st_softc *, int flags)); 230 int st_touch_tape __P((struct st_softc *)); 231 int st_write_filemarks __P((struct st_softc *, int number, int flags)); 232 int st_load __P((struct st_softc *, u_int type, int flags)); 233 int st_mode_select __P((struct st_softc *, int flags)); 234 void ststrategy(); 235 void stminphys(); 236 int st_check_eod(); 237 void ststart(); 238 void st_unmount(); 239 int st_mount_tape(); 240 void st_loadquirks(); 241 void st_identify_drive(); 242 int st_interpret_sense(); 243 244 struct scsi_device st_switch = { 245 st_interpret_sense, 246 ststart, 247 NULL, 248 NULL, 249 }; 250 251 #define ST_INFO_VALID 0x0001 252 #define ST_BLOCK_SET 0x0002 /* block size, mode set by ioctl */ 253 #define ST_WRITTEN 0x0004 /* data have been written, EOD needed */ 254 #define ST_FIXEDBLOCKS 0x0008 255 #define ST_AT_FILEMARK 0x0010 256 #define ST_EIO_PENDING 0x0020 /* we couldn't report it then (had data) */ 257 #define ST_NEW_MOUNT 0x0040 /* still need to decide mode */ 258 #define ST_READONLY 0x0080 /* st_mode_sense says write protected */ 259 #define ST_FM_WRITTEN 0x0100 /* 260 * EOF file mark written -- used with 261 * ~ST_WRITTEN to indicate that multiple file 262 * marks have been written 263 */ 264 #define ST_BLANK_READ 0x0200 /* BLANK CHECK encountered already */ 265 #define ST_2FM_AT_EOD 0x0400 /* write 2 file marks at EOD */ 266 #define ST_MOUNTED 0x0800 /* Device is presently mounted */ 267 268 #define ST_PER_ACTION (ST_AT_FILEMARK | ST_EIO_PENDING | ST_BLANK_READ) 269 #define ST_PER_MOUNT (ST_INFO_VALID | ST_BLOCK_SET | ST_WRITTEN | \ 270 ST_FIXEDBLOCKS | ST_READONLY | ST_FM_WRITTEN | \ 271 ST_2FM_AT_EOD | ST_PER_ACTION) 272 273 struct scsi_inquiry_pattern st_patterns[] = { 274 {T_SEQUENTIAL, T_REMOV, 275 "", "", ""}, 276 }; 277 278 int 279 stmatch(parent, match, aux) 280 struct device *parent; 281 void *match, *aux; 282 { 283 struct cfdata *cf = match; 284 struct scsibus_attach_args *sa = aux; 285 int priority; 286 287 (void)scsi_inqmatch(sa->sa_inqbuf, 288 (caddr_t)st_patterns, sizeof(st_patterns)/sizeof(st_patterns[0]), 289 sizeof(st_patterns[0]), &priority); 290 return (priority); 291 } 292 293 /* 294 * The routine called by the low level scsi routine when it discovers 295 * A device suitable for this driver 296 */ 297 void 298 stattach(parent, self, aux) 299 struct device *parent, *self; 300 void *aux; 301 { 302 struct st_softc *st = (void *)self; 303 struct scsibus_attach_args *sa = aux; 304 struct scsi_link *sc_link = sa->sa_sc_link; 305 306 SC_DEBUG(sc_link, SDEV_DB2, ("stattach: ")); 307 308 /* 309 * Store information needed to contact our base driver 310 */ 311 st->sc_link = sc_link; 312 sc_link->device = &st_switch; 313 sc_link->device_softc = st; 314 sc_link->openings = 1; 315 316 /* 317 * Check if the drive is a known criminal and take 318 * Any steps needed to bring it into line 319 */ 320 st_identify_drive(st, sa->sa_inqbuf); 321 322 /* 323 * Use the subdriver to request information regarding 324 * the drive. We cannot use interrupts yet, so the 325 * request must specify this. 326 */ 327 printf(": %s", st->quirkdata ? "rogue, " : ""); 328 if (scsi_test_unit_ready(sc_link, 329 SCSI_AUTOCONF | SCSI_SILENT | SCSI_IGNORE_MEDIA_CHANGE) || 330 st_mode_sense(st, 331 SCSI_AUTOCONF | SCSI_SILENT | SCSI_IGNORE_MEDIA_CHANGE)) 332 printf("drive empty\n"); 333 else { 334 printf("density code 0x%x, ", st->media_density); 335 if (st->media_blksize > 0) 336 printf("%d-byte", st->media_blksize); 337 else 338 printf("variable"); 339 printf(" blocks, write-%s\n", 340 (st->flags & ST_READONLY) ? "protected" : "enabled"); 341 } 342 343 /* 344 * Set up the buf queue for this device 345 */ 346 st->buf_queue.b_active = 0; 347 st->buf_queue.b_actf = 0; 348 st->buf_queue.b_actb = &st->buf_queue.b_actf; 349 } 350 351 /* 352 * Use the inquiry routine in 'scsi_base' to get drive info so we can 353 * Further tailor our behaviour. 354 */ 355 void 356 st_identify_drive(st, inqbuf) 357 struct st_softc *st; 358 struct scsi_inquiry_data *inqbuf; 359 { 360 struct st_quirk_inquiry_pattern *finger; 361 int priority; 362 363 finger = (struct st_quirk_inquiry_pattern *)scsi_inqmatch(inqbuf, 364 (caddr_t)st_quirk_patterns, 365 sizeof(st_quirk_patterns)/sizeof(st_quirk_patterns[0]), 366 sizeof(st_quirk_patterns[0]), &priority); 367 if (priority != 0) { 368 st->quirkdata = &finger->quirkdata; 369 st->drive_quirks = finger->quirkdata.quirks; 370 st->quirks = finger->quirkdata.quirks; /* start value */ 371 st->page_0_size = finger->quirkdata.page_0_size; 372 st_loadquirks(st); 373 } 374 } 375 376 /* 377 * initialise the subdevices to the default (QUIRK) state. 378 * this will remove any setting made by the system operator or previous 379 * operations. 380 */ 381 void 382 st_loadquirks(st) 383 struct st_softc *st; 384 { 385 int i; 386 struct modes *mode; 387 struct modes *mode2; 388 389 mode = st->quirkdata->modes; 390 mode2 = st->modes; 391 for (i = 0; i < 4; i++) { 392 bzero(mode2, sizeof(struct modes)); 393 st->modeflags[i] &= ~(BLKSIZE_SET_BY_QUIRK | 394 DENSITY_SET_BY_QUIRK | BLKSIZE_SET_BY_USER | 395 DENSITY_SET_BY_USER); 396 if ((mode->quirks | st->drive_quirks) & ST_Q_FORCE_BLKSIZE) { 397 mode2->blksize = mode->blksize; 398 st->modeflags[i] |= BLKSIZE_SET_BY_QUIRK; 399 } 400 if (mode->density) { 401 mode2->density = mode->density; 402 st->modeflags[i] |= DENSITY_SET_BY_QUIRK; 403 } 404 mode++; 405 mode2++; 406 } 407 } 408 409 /* 410 * open the device. 411 */ 412 int 413 stopen(dev, flags) 414 dev_t dev; 415 int flags; 416 { 417 int unit; 418 u_int mode, dsty; 419 int error = 0; 420 struct st_softc *st; 421 struct scsi_link *sc_link; 422 423 unit = STUNIT(dev); 424 if (unit >= stcd.cd_ndevs) 425 return ENXIO; 426 st = stcd.cd_devs[unit]; 427 if (!st) 428 return ENXIO; 429 430 mode = STMODE(dev); 431 dsty = STDSTY(dev); 432 sc_link = st->sc_link; 433 434 SC_DEBUG(sc_link, SDEV_DB1, ("open: dev=0x%x (unit %d (of %d))\n", dev, 435 unit, stcd.cd_ndevs)); 436 437 /* 438 * Only allow one at a time 439 */ 440 if (sc_link->flags & SDEV_OPEN) { 441 printf("%s: already open\n", st->sc_dev.dv_xname); 442 return EBUSY; 443 } 444 445 /* 446 * Catch any unit attention errors. 447 */ 448 if (error = scsi_test_unit_ready(sc_link, 449 SCSI_IGNORE_MEDIA_CHANGE | (mode == CTLMODE ? SCSI_IGNORE_NOT_READY : 0))) 450 goto bad; 451 452 sc_link->flags |= SDEV_OPEN; /* unit attn are now errors */ 453 454 /* 455 * If the mode is 3 (e.g. minor = 3,7,11,15) 456 * then the device has been opened to set defaults 457 * This mode does NOT ALLOW I/O, only ioctls 458 */ 459 if (mode == CTLMODE) 460 return 0; 461 462 /* 463 * if it's a different mode, or if the media has been 464 * invalidated, unmount the tape from the previous 465 * session but continue with open processing 466 */ 467 if (st->last_dsty != dsty || !(sc_link->flags & SDEV_MEDIA_LOADED)) 468 st_unmount(st, NOEJECT); 469 470 /* 471 * If we are not mounted, then we should start a new 472 * mount session. 473 */ 474 if (!(st->flags & ST_MOUNTED)) { 475 st_mount_tape(dev, flags); 476 st->last_dsty = dsty; 477 } 478 479 /* 480 * Make sure that a tape opened in write-only mode will have 481 * file marks written on it when closed, even if not written to. 482 * This is for SUN compatibility 483 */ 484 if ((flags & O_ACCMODE) == FWRITE) 485 st->flags |= ST_WRITTEN; 486 487 SC_DEBUG(sc_link, SDEV_DB2, ("open complete\n")); 488 return 0; 489 490 bad: 491 st_unmount(st, NOEJECT); 492 sc_link->flags &= ~SDEV_OPEN; 493 return error; 494 } 495 496 /* 497 * close the device.. only called if we are the LAST 498 * occurence of an open device 499 */ 500 int 501 stclose(dev) 502 dev_t dev; 503 { 504 struct st_softc *st = stcd.cd_devs[STUNIT(dev)]; 505 506 SC_DEBUG(st->sc_link, SDEV_DB1, ("closing\n")); 507 if ((st->flags & (ST_WRITTEN | ST_FM_WRITTEN)) == ST_WRITTEN) 508 st_write_filemarks(st, 1, 0); 509 switch (STMODE(dev)) { 510 case 0: 511 case 3: /* for now */ 512 st_unmount(st, NOEJECT); 513 break; 514 case 1: 515 /* leave mounted unless media seems to have been removed */ 516 if (!(st->sc_link->flags & SDEV_MEDIA_LOADED)) 517 st_unmount(st, NOEJECT); 518 break; 519 case 2: 520 st_unmount(st, EJECT); 521 break; 522 } 523 st->sc_link->flags &= ~SDEV_OPEN; 524 525 return 0; 526 } 527 528 /* 529 * Start a new mount session. 530 * Copy in all the default parameters from the selected device mode. 531 * and try guess any that seem to be defaulted. 532 */ 533 int 534 st_mount_tape(dev, flags) 535 dev_t dev; 536 int flags; 537 { 538 int unit; 539 u_int mode, dsty; 540 struct st_softc *st; 541 struct scsi_link *sc_link; 542 int error = 0; 543 544 unit = STUNIT(dev); 545 mode = STMODE(dev); 546 dsty = STDSTY(dev); 547 st = stcd.cd_devs[unit]; 548 sc_link = st->sc_link; 549 550 if (st->flags & ST_MOUNTED) 551 return 0; 552 553 SC_DEBUG(sc_link, SDEV_DB1, ("mounting\n ")); 554 st->flags |= ST_NEW_MOUNT; 555 st->quirks = st->drive_quirks | st->modes[dsty].quirks; 556 /* 557 * If the media is new, then make sure we give it a chance to 558 * to do a 'load' instruction. (We assume it is new.) 559 */ 560 if (error = st_load(st, LD_LOAD, 0)) 561 return error; 562 /* 563 * Throw another dummy instruction to catch 564 * 'Unit attention' errors. Some drives appear to give 565 * these after doing a Load instruction. 566 * (noteably some DAT drives) 567 */ 568 scsi_test_unit_ready(sc_link, SCSI_SILENT); /* XXX */ 569 570 /* 571 * Some devices can't tell you much until they have been 572 * asked to look at the media. This quirk does this. 573 */ 574 if (st->quirks & ST_Q_SENSE_HELP) 575 if (error = st_touch_tape(st)) 576 return error; 577 /* 578 * Load the physical device parameters 579 * loads: blkmin, blkmax 580 */ 581 if (error = st_read_block_limits(st, 0)) 582 return error; 583 /* 584 * Load the media dependent parameters 585 * includes: media_blksize,media_density,numblks 586 * As we have a tape in, it should be reflected here. 587 * If not you may need the "quirk" above. 588 */ 589 if (error = st_mode_sense(st, 0)) 590 return error; 591 /* 592 * If we have gained a permanent density from somewhere, 593 * then use it in preference to the one supplied by 594 * default by the driver. 595 */ 596 if (st->modeflags[dsty] & (DENSITY_SET_BY_QUIRK | DENSITY_SET_BY_USER)) 597 st->density = st->modes[dsty].density; 598 else 599 st->density = st->media_density; 600 /* 601 * If we have gained a permanent blocksize 602 * then use it in preference to the one supplied by 603 * default by the driver. 604 */ 605 st->flags &= ~ST_FIXEDBLOCKS; 606 if (st->modeflags[dsty] & (BLKSIZE_SET_BY_QUIRK | BLKSIZE_SET_BY_USER)) { 607 st->blksize = st->modes[dsty].blksize; 608 if (st->blksize) 609 st->flags |= ST_FIXEDBLOCKS; 610 } else { 611 if (error = st_decide_mode(st, FALSE)) 612 return error; 613 } 614 if (error = st_mode_select(st, 0)) { 615 printf("%s: cannot set selected mode\n", st->sc_dev.dv_xname); 616 return error; 617 } 618 scsi_prevent(sc_link, PR_PREVENT, 619 SCSI_IGNORE_ILLEGAL_REQUEST | SCSI_IGNORE_NOT_READY); 620 st->flags &= ~ST_NEW_MOUNT; 621 st->flags |= ST_MOUNTED; 622 sc_link->flags |= SDEV_MEDIA_LOADED; /* move earlier? */ 623 624 return 0; 625 } 626 627 /* 628 * End the present mount session. 629 * Rewind, and optionally eject the tape. 630 * Reset various flags to indicate that all new 631 * operations require another mount operation 632 */ 633 void 634 st_unmount(st, eject) 635 struct st_softc *st; 636 boolean eject; 637 { 638 struct scsi_link *sc_link = st->sc_link; 639 int nmarks; 640 641 if (!(st->flags & ST_MOUNTED)) 642 return; 643 SC_DEBUG(sc_link, SDEV_DB1, ("unmounting\n")); 644 st_check_eod(st, FALSE, &nmarks, SCSI_IGNORE_NOT_READY); 645 st_rewind(st, 0, SCSI_IGNORE_NOT_READY); 646 scsi_prevent(sc_link, PR_ALLOW, 647 SCSI_IGNORE_ILLEGAL_REQUEST | SCSI_IGNORE_NOT_READY); 648 if (eject) 649 st_load(st, LD_UNLOAD, SCSI_IGNORE_NOT_READY); 650 st->flags &= ~(ST_MOUNTED | ST_NEW_MOUNT); 651 sc_link->flags &= ~SDEV_MEDIA_LOADED; 652 } 653 654 /* 655 * Given all we know about the device, media, mode, 'quirks' and 656 * initial operation, make a decision as to how we should be set 657 * to run (regarding blocking and EOD marks) 658 */ 659 int 660 st_decide_mode(st, first_read) 661 struct st_softc *st; 662 boolean first_read; 663 { 664 #ifdef SCSIDEBUG 665 struct scsi_link *sc_link = st->sc_link; 666 #endif 667 668 SC_DEBUG(sc_link, SDEV_DB2, ("starting block mode decision\n")); 669 670 /* 671 * If the drive can only handle fixed-length blocks and only at 672 * one size, perhaps we should just do that. 673 */ 674 if (st->blkmin && (st->blkmin == st->blkmax)) { 675 st->flags |= ST_FIXEDBLOCKS; 676 st->blksize = st->blkmin; 677 SC_DEBUG(sc_link, SDEV_DB3, 678 ("blkmin == blkmax of %d\n", st->blkmin)); 679 goto done; 680 } 681 /* 682 * If the tape density mandates (or even suggests) use of fixed 683 * or variable-length blocks, comply. 684 */ 685 switch (st->density) { 686 case HALFINCH_800: 687 case HALFINCH_1600: 688 case HALFINCH_6250: 689 case DDS: 690 st->flags &= ~ST_FIXEDBLOCKS; 691 st->blksize = 0; 692 SC_DEBUG(sc_link, SDEV_DB3, ("density specified variable\n")); 693 goto done; 694 case QIC_11: 695 case QIC_24: 696 case QIC_120: 697 case QIC_150: 698 case QIC_525: 699 case QIC_1320: 700 st->flags |= ST_FIXEDBLOCKS; 701 if (st->media_blksize > 0) 702 st->blksize = st->media_blksize; 703 else 704 st->blksize = DEF_FIXED_BSIZE; 705 SC_DEBUG(sc_link, SDEV_DB3, ("density specified fixed\n")); 706 goto done; 707 } 708 /* 709 * If we're about to read the tape, perhaps we should choose 710 * fixed or variable-length blocks and block size according to 711 * what the drive found on the tape. 712 */ 713 if (first_read && 714 (!(st->quirks & ST_Q_BLKSIZE) || (st->media_blksize == 0) || 715 (st->media_blksize == DEF_FIXED_BSIZE) || 716 (st->media_blksize == 1024))) { 717 if (st->media_blksize > 0) 718 st->flags |= ST_FIXEDBLOCKS; 719 else 720 st->flags &= ~ST_FIXEDBLOCKS; 721 st->blksize = st->media_blksize; 722 SC_DEBUG(sc_link, SDEV_DB3, 723 ("Used media_blksize of %d\n", st->media_blksize)); 724 goto done; 725 } 726 /* 727 * We're getting no hints from any direction. Choose variable- 728 * length blocks arbitrarily. 729 */ 730 st->flags &= ~ST_FIXEDBLOCKS; 731 st->blksize = 0; 732 SC_DEBUG(sc_link, SDEV_DB3, 733 ("Give up and default to variable mode\n")); 734 735 done: 736 /* 737 * Decide whether or not to write two file marks to signify end- 738 * of-data. Make the decision as a function of density. If 739 * the decision is not to use a second file mark, the SCSI BLANK 740 * CHECK condition code will be recognized as end-of-data when 741 * first read. 742 * (I think this should be a by-product of fixed/variable..julian) 743 */ 744 switch (st->density) { 745 /* case 8 mm: What is the SCSI density code for 8 mm, anyway? */ 746 case QIC_11: 747 case QIC_24: 748 case QIC_120: 749 case QIC_150: 750 case QIC_525: 751 case QIC_1320: 752 st->flags &= ~ST_2FM_AT_EOD; 753 break; 754 default: 755 st->flags |= ST_2FM_AT_EOD; 756 } 757 return 0; 758 } 759 760 /* 761 * trim the size of the transfer if needed, 762 * called by physio 763 * basically the smaller of our min and the scsi driver's 764 * minphys 765 */ 766 void 767 stminphys(bp) 768 struct buf *bp; 769 { 770 register struct st_softc *st = stcd.cd_devs[STUNIT(bp->b_dev)]; 771 772 (st->sc_link->adapter->scsi_minphys) (bp); 773 } 774 775 /* 776 * Actually translate the requested transfer into 777 * one the physical driver can understand 778 * The transfer is described by a buf and will include 779 * only one physical transfer. 780 */ 781 void 782 ststrategy(bp) 783 struct buf *bp; 784 { 785 struct st_softc *st = stcd.cd_devs[STUNIT(bp->b_dev)]; 786 struct buf *dp; 787 int opri; 788 789 SC_DEBUG(st->sc_link, SDEV_DB1, 790 ("ststrategy %d bytes @ blk %d\n", bp->b_bcount, bp->b_blkno)); 791 /* 792 * If it's a null transfer, return immediatly 793 */ 794 if (bp->b_bcount == 0) 795 goto done; 796 /* 797 * Odd sized request on fixed drives are verboten 798 */ 799 if (st->flags & ST_FIXEDBLOCKS) { 800 if (bp->b_bcount % st->blksize) { 801 printf("%s: bad request, must be multiple of %d\n", 802 st->sc_dev.dv_xname, st->blksize); 803 bp->b_error = EIO; 804 goto bad; 805 } 806 } 807 /* 808 * as are out-of-range requests on variable drives. 809 */ 810 else if (bp->b_bcount < st->blkmin || 811 (st->blkmax && bp->b_bcount > st->blkmax)) { 812 printf("%s: bad request, must be between %d and %d\n", 813 st->sc_dev.dv_xname, st->blkmin, st->blkmax); 814 bp->b_error = EIO; 815 goto bad; 816 } 817 stminphys(bp); 818 opri = splbio(); 819 820 /* 821 * Place it in the queue of activities for this tape 822 * at the end (a bit silly because we only have on user.. 823 * (but it could fork())) 824 */ 825 dp = &st->buf_queue; 826 bp->b_actf = NULL; 827 bp->b_actb = dp->b_actb; 828 *dp->b_actb = bp; 829 dp->b_actb = &bp->b_actf; 830 831 /* 832 * Tell the device to get going on the transfer if it's 833 * not doing anything, otherwise just wait for completion 834 * (All a bit silly if we're only allowing 1 open but..) 835 */ 836 ststart(st); 837 838 splx(opri); 839 return; 840 bad: 841 bp->b_flags |= B_ERROR; 842 done: 843 /* 844 * Correctly set the buf to indicate a completed xfer 845 */ 846 iodone(bp); 847 return; 848 } 849 850 /* 851 * ststart looks to see if there is a buf waiting for the device 852 * and that the device is not already busy. If both are true, 853 * It dequeues the buf and creates a scsi command to perform the 854 * transfer required. The transfer request will call scsi_done 855 * on completion, which will in turn call this routine again 856 * so that the next queued transfer is performed. 857 * The bufs are queued by the strategy routine (ststrategy) 858 * 859 * This routine is also called after other non-queued requests 860 * have been made of the scsi driver, to ensure that the queue 861 * continues to be drained. 862 * ststart() is called at splbio 863 */ 864 void 865 ststart(st) 866 struct st_softc *st; 867 { 868 struct scsi_link *sc_link = st->sc_link; 869 register struct buf *bp, *dp; 870 struct scsi_rw_tape cmd; 871 int flags; 872 873 SC_DEBUG(sc_link, SDEV_DB2, ("ststart ")); 874 /* 875 * See if there is a buf to do and we are not already 876 * doing one 877 */ 878 while (sc_link->openings > 0) { 879 /* if a special awaits, let it proceed first */ 880 if (sc_link->flags & SDEV_WAITING) { 881 sc_link->flags &= ~SDEV_WAITING; 882 wakeup((caddr_t)sc_link); 883 return; 884 } 885 886 bp = st->buf_queue.b_actf; 887 if (!bp) 888 return; /* no work to bother with */ 889 if (dp = bp->b_actf) 890 dp->b_actb = bp->b_actb; 891 else 892 st->buf_queue.b_actb = bp->b_actb; 893 *bp->b_actb = dp; 894 895 /* 896 * if the device has been unmounted byt the user 897 * then throw away all requests until done 898 */ 899 if (!(st->flags & ST_MOUNTED) || 900 !(sc_link->flags & SDEV_MEDIA_LOADED)) { 901 /* make sure that one implies the other.. */ 902 sc_link->flags &= ~SDEV_MEDIA_LOADED; 903 bp->b_flags |= B_ERROR; 904 bp->b_error = EIO; 905 biodone(bp); 906 continue; 907 } 908 /* 909 * only FIXEDBLOCK devices have pending operations 910 */ 911 if (st->flags & ST_FIXEDBLOCKS) { 912 /* 913 * If we are at a filemark but have not reported it yet 914 * then we should report it now 915 */ 916 if (st->flags & ST_AT_FILEMARK) { 917 if ((bp->b_flags & B_READ) == B_WRITE) { 918 /* 919 * Handling of ST_AT_FILEMARK in 920 * st_space will fill in the right file 921 * mark count. 922 * Back up over filemark 923 */ 924 if (st_space(st, 0, SP_FILEMARKS, 0)) { 925 bp->b_flags |= B_ERROR; 926 bp->b_error = EIO; 927 biodone(bp); 928 continue; 929 } 930 } else { 931 bp->b_resid = bp->b_bcount; 932 bp->b_error = 0; 933 bp->b_flags &= ~B_ERROR; 934 st->flags &= ~ST_AT_FILEMARK; 935 biodone(bp); 936 continue; /* seek more work */ 937 } 938 } 939 /* 940 * If we are at EIO (e.g. EOM) but have not reported it 941 * yet then we should report it now 942 */ 943 if (st->flags & ST_EIO_PENDING) { 944 bp->b_resid = bp->b_bcount; 945 bp->b_error = EIO; 946 bp->b_flags |= B_ERROR; 947 st->flags &= ~ST_EIO_PENDING; 948 biodone(bp); 949 continue; /* seek more work */ 950 } 951 } 952 953 /* 954 * Fill out the scsi command 955 */ 956 bzero(&cmd, sizeof(cmd)); 957 if ((bp->b_flags & B_READ) == B_WRITE) { 958 cmd.opcode = WRITE; 959 st->flags &= ~ST_FM_WRITTEN; 960 st->flags |= ST_WRITTEN; 961 flags = SCSI_DATA_OUT; 962 } else { 963 cmd.opcode = READ; 964 flags = SCSI_DATA_IN; 965 } 966 967 /* 968 * Handle "fixed-block-mode" tape drives by using the 969 * block count instead of the length. 970 */ 971 if (st->flags & ST_FIXEDBLOCKS) { 972 cmd.byte2 |= SRW_FIXED; 973 lto3b(bp->b_bcount / st->blksize, cmd.len); 974 } else 975 lto3b(bp->b_bcount, cmd.len); 976 977 /* 978 * go ask the adapter to do all this for us 979 */ 980 if (scsi_scsi_cmd(sc_link, (struct scsi_generic *) &cmd, 981 sizeof(cmd), (u_char *) bp->b_data, bp->b_bcount, 0, 982 100000, bp, flags | SCSI_NOSLEEP)) 983 printf("%s: not queued\n", st->sc_dev.dv_xname); 984 } /* go back and see if we can cram more work in.. */ 985 } 986 987 /* 988 * Perform special action on behalf of the user; 989 * knows about the internals of this device 990 */ 991 int 992 stioctl(dev, cmd, arg, flag, p) 993 dev_t dev; 994 u_long cmd; 995 caddr_t arg; 996 int flag; 997 struct proc *p; 998 { 999 int error = 0; 1000 int unit; 1001 int number, nmarks, dsty; 1002 int flags; 1003 struct st_softc *st; 1004 int hold_blksize; 1005 u_int8_t hold_density; 1006 struct mtop *mt = (struct mtop *) arg; 1007 1008 /* 1009 * Find the device that the user is talking about 1010 */ 1011 flags = 0; /* give error messages, act on errors etc. */ 1012 unit = STUNIT(dev); 1013 dsty = STDSTY(dev); 1014 st = stcd.cd_devs[unit]; 1015 hold_blksize = st->blksize; 1016 hold_density = st->density; 1017 1018 switch (cmd) { 1019 1020 case MTIOCGET: { 1021 struct mtget *g = (struct mtget *) arg; 1022 1023 SC_DEBUG(st->sc_link, SDEV_DB1, ("[ioctl: get status]\n")); 1024 bzero(g, sizeof(struct mtget)); 1025 g->mt_type = 0x7; /* Ultrix compat *//*? */ 1026 g->mt_blksiz = st->blksize; 1027 g->mt_density = st->density; 1028 g->mt_mblksiz[0] = st->modes[0].blksize; 1029 g->mt_mblksiz[1] = st->modes[1].blksize; 1030 g->mt_mblksiz[2] = st->modes[2].blksize; 1031 g->mt_mblksiz[3] = st->modes[3].blksize; 1032 g->mt_mdensity[0] = st->modes[0].density; 1033 g->mt_mdensity[1] = st->modes[1].density; 1034 g->mt_mdensity[2] = st->modes[2].density; 1035 g->mt_mdensity[3] = st->modes[3].density; 1036 break; 1037 } 1038 case MTIOCTOP: { 1039 1040 SC_DEBUG(st->sc_link, SDEV_DB1, 1041 ("[ioctl: op=0x%x count=0x%x]\n", mt->mt_op, mt->mt_count)); 1042 1043 /* compat: in U*x it is a short */ 1044 number = mt->mt_count; 1045 switch ((short) (mt->mt_op)) { 1046 case MTWEOF: /* write an end-of-file record */ 1047 error = st_write_filemarks(st, number, flags); 1048 break; 1049 case MTBSF: /* backward space file */ 1050 number = -number; 1051 case MTFSF: /* forward space file */ 1052 error = st_check_eod(st, FALSE, &nmarks, flags); 1053 if (!error) 1054 error = st_space(st, number - nmarks, 1055 SP_FILEMARKS, flags); 1056 break; 1057 case MTBSR: /* backward space record */ 1058 number = -number; 1059 case MTFSR: /* forward space record */ 1060 error = st_check_eod(st, TRUE, &nmarks, flags); 1061 if (!error) 1062 error = st_space(st, number, SP_BLKS, flags); 1063 break; 1064 case MTREW: /* rewind */ 1065 error = st_rewind(st, 0, flags); 1066 break; 1067 case MTOFFL: /* rewind and put the drive offline */ 1068 st_unmount(st, EJECT); 1069 break; 1070 case MTNOP: /* no operation, sets status only */ 1071 break; 1072 case MTRETEN: /* retension the tape */ 1073 error = st_load(st, LD_RETENSION, flags); 1074 if (!error) 1075 error = st_load(st, LD_LOAD, flags); 1076 break; 1077 case MTEOM: /* forward space to end of media */ 1078 error = st_check_eod(st, FALSE, &nmarks, flags); 1079 if (!error) 1080 error = st_space(st, 1, SP_EOM, flags); 1081 break; 1082 case MTCACHE: /* enable controller cache */ 1083 case MTNOCACHE: /* disable controller cache */ 1084 break; 1085 case MTSETBSIZ: /* Set block size for device */ 1086 #ifdef NOTYET 1087 if (!(st->flags & ST_NEW_MOUNT)) { 1088 uprintf("re-mount tape before changing blocksize"); 1089 error = EINVAL; 1090 break; 1091 } 1092 #endif 1093 if (number == 0) { 1094 st->flags &= ~ST_FIXEDBLOCKS; 1095 } else { 1096 if ((st->blkmin || st->blkmax) && 1097 (number < st->blkmin || 1098 number > st->blkmax)) { 1099 error = EINVAL; 1100 break; 1101 } 1102 st->flags |= ST_FIXEDBLOCKS; 1103 } 1104 st->blksize = number; 1105 st->flags |= ST_BLOCK_SET; /*XXX */ 1106 goto try_new_value; 1107 1108 case MTSETDNSTY: /* Set density for device and mode */ 1109 if (number > SCSI_2_MAX_DENSITY_CODE) 1110 error = EINVAL; 1111 else 1112 st->density = number; 1113 goto try_new_value; 1114 1115 default: 1116 error = EINVAL; 1117 } 1118 break; 1119 } 1120 case MTIOCIEOT: 1121 case MTIOCEEOT: 1122 break; 1123 default: 1124 if (STMODE(dev) == CTLMODE) 1125 error = scsi_do_ioctl(st->sc_link, dev, cmd, arg, flag, p); 1126 else 1127 error = ENOTTY; 1128 break; 1129 } 1130 return error; 1131 /*-----------------------------*/ 1132 try_new_value: 1133 /* 1134 * Check that the mode being asked for is aggreeable to the 1135 * drive. If not, put it back the way it was. 1136 */ 1137 if (error = st_mode_select(st, 0)) { /* put it back as it was */ 1138 printf("%s: cannot set selected mode\n", st->sc_dev.dv_xname); 1139 st->density = hold_density; 1140 st->blksize = hold_blksize; 1141 if (st->blksize) 1142 st->flags |= ST_FIXEDBLOCKS; 1143 else 1144 st->flags &= ~ST_FIXEDBLOCKS; 1145 return error; 1146 } 1147 /* 1148 * As the drive liked it, if we are setting a new default, 1149 * set it into the structures as such. 1150 * 1151 * The means for deciding this are not finalised yet 1152 */ 1153 if (STMODE(dev) == 0x03) { 1154 /* special mode */ 1155 /* XXX */ 1156 switch ((short) (mt->mt_op)) { 1157 case MTSETBSIZ: 1158 st->modes[dsty].blksize = st->blksize; 1159 st->modeflags[dsty] |= BLKSIZE_SET_BY_USER; 1160 break; 1161 case MTSETDNSTY: 1162 st->modes[dsty].density = st->density; 1163 st->modeflags[dsty] |= DENSITY_SET_BY_USER; 1164 break; 1165 } 1166 } 1167 return 0; 1168 } 1169 1170 /* 1171 * Do a synchronous read. 1172 */ 1173 int 1174 st_read(st, buf, size, flags) 1175 struct st_softc *st; 1176 int size; 1177 int flags; 1178 char *buf; 1179 { 1180 struct scsi_rw_tape cmd; 1181 1182 /* 1183 * If it's a null transfer, return immediatly 1184 */ 1185 if (size == 0) 1186 return 0; 1187 bzero(&cmd, sizeof(cmd)); 1188 cmd.opcode = READ; 1189 if (st->flags & ST_FIXEDBLOCKS) { 1190 cmd.byte2 |= SRW_FIXED; 1191 lto3b(size / (st->blksize ? st->blksize : DEF_FIXED_BSIZE), 1192 cmd.len); 1193 } else 1194 lto3b(size, cmd.len); 1195 return scsi_scsi_cmd(st->sc_link, (struct scsi_generic *) &cmd, 1196 sizeof(cmd), (u_char *) buf, size, 0, 100000, NULL, 1197 flags | SCSI_DATA_IN); 1198 } 1199 1200 #ifdef __STDC__ 1201 #define b2tol(a) (((unsigned)(a##_1) << 8) + (unsigned)a##_0) 1202 #else 1203 #define b2tol(a) (((unsigned)(a/**/_1) << 8) + (unsigned)a/**/_0) 1204 #endif 1205 1206 /* 1207 * Ask the drive what it's min and max blk sizes are. 1208 */ 1209 int 1210 st_read_block_limits(st, flags) 1211 struct st_softc *st; 1212 int flags; 1213 { 1214 struct scsi_block_limits cmd; 1215 struct scsi_block_limits_data block_limits; 1216 struct scsi_link *sc_link = st->sc_link; 1217 int error; 1218 1219 /* 1220 * First check if we have it all loaded 1221 */ 1222 if ((sc_link->flags & SDEV_MEDIA_LOADED)) 1223 return 0; 1224 1225 /* 1226 * do a 'Read Block Limits' 1227 */ 1228 bzero(&cmd, sizeof(cmd)); 1229 cmd.opcode = READ_BLOCK_LIMITS; 1230 1231 /* 1232 * do the command, update the global values 1233 */ 1234 if (error = scsi_scsi_cmd(sc_link, (struct scsi_generic *) &cmd, 1235 sizeof(cmd), (u_char *) &block_limits, sizeof(block_limits), 1236 ST_RETRIES, 5000, NULL, flags | SCSI_DATA_IN)) 1237 return error; 1238 1239 st->blkmin = b2tol(block_limits.min_length); 1240 st->blkmax = _3btol(&block_limits.max_length_2); 1241 1242 SC_DEBUG(sc_link, SDEV_DB3, 1243 ("(%d <= blksize <= %d)\n", st->blkmin, st->blkmax)); 1244 return 0; 1245 } 1246 1247 /* 1248 * Get the scsi driver to send a full inquiry to the 1249 * device and use the results to fill out the global 1250 * parameter structure. 1251 * 1252 * called from: 1253 * attach 1254 * open 1255 * ioctl (to reset original blksize) 1256 */ 1257 int 1258 st_mode_sense(st, flags) 1259 struct st_softc *st; 1260 int flags; 1261 { 1262 u_int scsi_sense_len; 1263 int error; 1264 struct scsi_mode_sense cmd; 1265 struct scsi_sense { 1266 struct scsi_mode_header header; 1267 struct scsi_blk_desc blk_desc; 1268 u_char sense_data[MAX_PAGE_0_SIZE]; 1269 } scsi_sense; 1270 struct scsi_link *sc_link = st->sc_link; 1271 1272 scsi_sense_len = 12 + st->page_0_size; 1273 1274 /* 1275 * Set up a mode sense 1276 */ 1277 bzero(&cmd, sizeof(cmd)); 1278 cmd.opcode = MODE_SENSE; 1279 cmd.length = scsi_sense_len; 1280 1281 /* 1282 * do the command, but we don't need the results 1283 * just print them for our interest's sake, if asked, 1284 * or if we need it as a template for the mode select 1285 * store it away. 1286 */ 1287 if (error = scsi_scsi_cmd(sc_link, (struct scsi_generic *) &cmd, 1288 sizeof(cmd), (u_char *) &scsi_sense, scsi_sense_len, 1289 ST_RETRIES, 5000, NULL, flags | SCSI_DATA_IN)) 1290 return error; 1291 1292 st->numblks = _3btol(scsi_sense.blk_desc.nblocks); 1293 st->media_blksize = _3btol(scsi_sense.blk_desc.blklen); 1294 st->media_density = scsi_sense.blk_desc.density; 1295 if (scsi_sense.header.dev_spec & SMH_DSP_WRITE_PROT) 1296 st->flags |= ST_READONLY; 1297 SC_DEBUG(sc_link, SDEV_DB3, 1298 ("density code 0x%x, %d-byte blocks, write-%s, ", 1299 st->media_density, st->media_blksize, 1300 st->flags & ST_READONLY ? "protected" : "enabled")); 1301 SC_DEBUG(sc_link, SDEV_DB3, 1302 ("%sbuffered\n", 1303 scsi_sense.header.dev_spec & SMH_DSP_BUFF_MODE ? "" : "un")); 1304 if (st->page_0_size) 1305 bcopy(scsi_sense.sense_data, st->sense_data, st->page_0_size); 1306 sc_link->flags |= SDEV_MEDIA_LOADED; 1307 return 0; 1308 } 1309 1310 /* 1311 * Send a filled out parameter structure to the drive to 1312 * set it into the desire modes etc. 1313 */ 1314 int 1315 st_mode_select(st, flags) 1316 struct st_softc *st; 1317 int flags; 1318 { 1319 u_int scsi_select_len; 1320 int error; 1321 struct scsi_mode_select cmd; 1322 struct scsi_select { 1323 struct scsi_mode_header header; 1324 struct scsi_blk_desc blk_desc; 1325 u_char sense_data[MAX_PAGE_0_SIZE]; 1326 } scsi_select; 1327 struct scsi_link *sc_link = st->sc_link; 1328 1329 scsi_select_len = 12 + st->page_0_size; 1330 1331 /* 1332 * Set up for a mode select 1333 */ 1334 bzero(&cmd, sizeof(cmd)); 1335 cmd.opcode = MODE_SELECT; 1336 cmd.length = scsi_select_len; 1337 1338 bzero(&scsi_select, scsi_select_len); 1339 scsi_select.header.blk_desc_len = sizeof(struct scsi_blk_desc); 1340 scsi_select.header.dev_spec |= SMH_DSP_BUFF_MODE_ON; 1341 scsi_select.blk_desc.density = st->density; 1342 if (st->flags & ST_FIXEDBLOCKS) 1343 lto3b(st->blksize, scsi_select.blk_desc.blklen); 1344 if (st->page_0_size) 1345 bcopy(st->sense_data, scsi_select.sense_data, st->page_0_size); 1346 1347 /* 1348 * do the command 1349 */ 1350 return scsi_scsi_cmd(sc_link, (struct scsi_generic *) &cmd, 1351 sizeof(cmd), (u_char *) &scsi_select, scsi_select_len, 1352 ST_RETRIES, 5000, NULL, flags | SCSI_DATA_OUT); 1353 } 1354 1355 /* 1356 * skip N blocks/filemarks/seq filemarks/eom 1357 */ 1358 int 1359 st_space(st, number, what, flags) 1360 struct st_softc *st; 1361 u_int what; 1362 int flags; 1363 int number; 1364 { 1365 struct scsi_space cmd; 1366 int error; 1367 1368 switch (what) { 1369 case SP_BLKS: 1370 if (st->flags & ST_PER_ACTION) { 1371 if (number > 0) { 1372 st->flags &= ~ST_PER_ACTION; 1373 return EIO; 1374 } else if (number < 0) { 1375 if (st->flags & ST_AT_FILEMARK) { 1376 /* 1377 * Handling of ST_AT_FILEMARK 1378 * in st_space will fill in the 1379 * right file mark count. 1380 */ 1381 error = st_space(st, 0, SP_FILEMARKS, 1382 flags); 1383 if (error) 1384 return error; 1385 } 1386 if (st->flags & ST_BLANK_READ) { 1387 st->flags &= ~ST_BLANK_READ; 1388 return EIO; 1389 } 1390 st->flags &= ~ST_EIO_PENDING; 1391 } 1392 } 1393 break; 1394 case SP_FILEMARKS: 1395 if (st->flags & ST_EIO_PENDING) { 1396 if (number > 0) { 1397 /* pretend we just discovered the error */ 1398 st->flags &= ~ST_EIO_PENDING; 1399 return EIO; 1400 } else if (number < 0) { 1401 /* back away from the error */ 1402 st->flags &= ~ST_EIO_PENDING; 1403 } 1404 } 1405 if (st->flags & ST_AT_FILEMARK) { 1406 st->flags &= ~ST_AT_FILEMARK; 1407 number--; 1408 } 1409 if ((st->flags & ST_BLANK_READ) && (number < 0)) { 1410 /* back away from unwritten tape */ 1411 st->flags &= ~ST_BLANK_READ; 1412 number++; /* XXX dubious */ 1413 } 1414 break; 1415 case SP_EOM: 1416 if (st->flags & ST_EIO_PENDING) { 1417 /* pretend we just discovered the error */ 1418 st->flags &= ~ST_EIO_PENDING; 1419 return EIO; 1420 } 1421 if (st->flags & ST_AT_FILEMARK) 1422 st->flags &= ~ST_AT_FILEMARK; 1423 break; 1424 } 1425 if (number == 0) 1426 return 0; 1427 1428 bzero(&cmd, sizeof(cmd)); 1429 cmd.opcode = SPACE; 1430 cmd.byte2 = what; 1431 lto3b(number, cmd.number); 1432 1433 return scsi_scsi_cmd(st->sc_link, (struct scsi_generic *) &cmd, 1434 sizeof(cmd), 0, 0, 0, 900000, NULL, flags); 1435 } 1436 1437 /* 1438 * write N filemarks 1439 */ 1440 int 1441 st_write_filemarks(st, number, flags) 1442 struct st_softc *st; 1443 int flags; 1444 int number; 1445 { 1446 struct scsi_write_filemarks cmd; 1447 1448 /* 1449 * It's hard to write a negative number of file marks. 1450 * Don't try. 1451 */ 1452 if (number < 0) 1453 return EINVAL; 1454 switch (number) { 1455 case 0: /* really a command to sync the drive's buffers */ 1456 break; 1457 case 1: 1458 if (st->flags & ST_FM_WRITTEN) /* already have one down */ 1459 st->flags &= ~ST_WRITTEN; 1460 else 1461 st->flags |= ST_FM_WRITTEN; 1462 st->flags &= ~ST_PER_ACTION; 1463 break; 1464 default: 1465 st->flags &= ~(ST_PER_ACTION | ST_WRITTEN); 1466 } 1467 1468 bzero(&cmd, sizeof(cmd)); 1469 cmd.opcode = WRITE_FILEMARKS; 1470 lto3b(number, cmd.number); 1471 1472 return scsi_scsi_cmd(st->sc_link, (struct scsi_generic *) &cmd, 1473 sizeof(cmd), 0, 0, 0, 100000, NULL, flags); 1474 } 1475 1476 /* 1477 * Make sure the right number of file marks is on tape if the 1478 * tape has been written. If the position argument is true, 1479 * leave the tape positioned where it was originally. 1480 * 1481 * nmarks returns the number of marks to skip (or, if position 1482 * true, which were skipped) to get back original position. 1483 */ 1484 int 1485 st_check_eod(st, position, nmarks, flags) 1486 struct st_softc *st; 1487 boolean position; 1488 int *nmarks; 1489 int flags; 1490 { 1491 int error; 1492 1493 switch (st->flags & (ST_WRITTEN | ST_FM_WRITTEN | ST_2FM_AT_EOD)) { 1494 default: 1495 *nmarks = 0; 1496 return 0; 1497 case ST_WRITTEN: 1498 case ST_WRITTEN | ST_FM_WRITTEN | ST_2FM_AT_EOD: 1499 *nmarks = 1; 1500 break; 1501 case ST_WRITTEN | ST_2FM_AT_EOD: 1502 *nmarks = 2; 1503 } 1504 error = st_write_filemarks(st, *nmarks, flags); 1505 if (position && !error) 1506 error = st_space(st, -*nmarks, SP_FILEMARKS, flags); 1507 return error; 1508 } 1509 1510 /* 1511 * load/unload/retension 1512 */ 1513 int 1514 st_load(st, type, flags) 1515 struct st_softc *st; 1516 u_int type; 1517 int flags; 1518 { 1519 struct scsi_load cmd; 1520 1521 if (type != LD_LOAD) { 1522 int error; 1523 int nmarks; 1524 1525 error = st_check_eod(st, FALSE, &nmarks, flags); 1526 if (error) 1527 return error; 1528 } 1529 if (st->quirks & ST_Q_IGNORE_LOADS) 1530 return 0; 1531 1532 bzero(&cmd, sizeof(cmd)); 1533 cmd.opcode = LOAD; 1534 cmd.how = type; 1535 1536 return scsi_scsi_cmd(st->sc_link, (struct scsi_generic *) &cmd, 1537 sizeof(cmd), 0, 0, ST_RETRIES, 300000, NULL, flags); 1538 } 1539 1540 /* 1541 * Rewind the device 1542 */ 1543 int 1544 st_rewind(st, immediate, flags) 1545 struct st_softc *st; 1546 u_int immediate; 1547 int flags; 1548 { 1549 struct scsi_rewind cmd; 1550 int error; 1551 int nmarks; 1552 1553 error = st_check_eod(st, FALSE, &nmarks, flags); 1554 if (error) 1555 return error; 1556 st->flags &= ~ST_PER_ACTION; 1557 1558 bzero(&cmd, sizeof(cmd)); 1559 cmd.opcode = REWIND; 1560 cmd.byte2 = immediate; 1561 1562 return scsi_scsi_cmd(st->sc_link, (struct scsi_generic *) &cmd, 1563 sizeof(cmd), 0, 0, ST_RETRIES, immediate ? 5000 : 300000, NULL, 1564 flags); 1565 } 1566 1567 /* 1568 * Look at the returned sense and act on the error and detirmine 1569 * The unix error number to pass back... (0 = report no error) 1570 * (-1 = continue processing) 1571 */ 1572 int 1573 st_interpret_sense(xs) 1574 struct scsi_xfer *xs; 1575 { 1576 struct scsi_link *sc_link = xs->sc_link; 1577 struct scsi_sense_data *sense = &xs->sense; 1578 struct buf *bp = xs->bp; 1579 struct st_softc *st = sc_link->device_softc; 1580 u_int8_t key; 1581 u_int32_t info; 1582 1583 /* 1584 * Get the sense fields and work out what code 1585 */ 1586 if (sense->error_code & SSD_ERRCODE_VALID) { 1587 bcopy(sense->extended_info, &info, sizeof info); 1588 info = ntohl(info); 1589 } else 1590 info = xs->datalen; /* bad choice if fixed blocks */ 1591 if ((sense->error_code & SSD_ERRCODE) != 0x70) 1592 return -1; /* let the generic code handle it */ 1593 if (st->flags & ST_FIXEDBLOCKS) { 1594 xs->resid = info * st->blksize; 1595 if (sense->extended_flags & SSD_EOM) { 1596 st->flags |= ST_EIO_PENDING; 1597 if (bp) 1598 bp->b_resid = xs->resid; 1599 } 1600 if (sense->extended_flags & SSD_FILEMARK) { 1601 st->flags |= ST_AT_FILEMARK; 1602 if (bp) 1603 bp->b_resid = xs->resid; 1604 } 1605 if (sense->extended_flags & SSD_ILI) { 1606 st->flags |= ST_EIO_PENDING; 1607 if (bp) 1608 bp->b_resid = xs->resid; 1609 if (sense->error_code & SSD_ERRCODE_VALID && 1610 (xs->flags & SCSI_SILENT) == 0) 1611 printf("%s: block wrong size, %d blocks residual\n", 1612 st->sc_dev.dv_xname, info); 1613 1614 /* 1615 * This quirk code helps the drive read 1616 * the first tape block, regardless of 1617 * format. That is required for these 1618 * drives to return proper MODE SENSE 1619 * information. 1620 */ 1621 if ((st->quirks & ST_Q_SENSE_HELP) && 1622 !(sc_link->flags & SDEV_MEDIA_LOADED)) 1623 st->blksize -= 512; 1624 } 1625 /* 1626 * If no data was tranfered, do it immediatly 1627 */ 1628 if (xs->resid >= xs->datalen) { 1629 if (st->flags & ST_EIO_PENDING) 1630 return EIO; 1631 if (st->flags & ST_AT_FILEMARK) { 1632 if (bp) 1633 bp->b_resid = xs->resid; 1634 return 0; 1635 } 1636 } 1637 } else { /* must be variable mode */ 1638 xs->resid = xs->datalen; /* to be sure */ 1639 if (sense->extended_flags & SSD_EOM) 1640 return EIO; 1641 if (sense->extended_flags & SSD_FILEMARK) { 1642 if (bp) 1643 bp->b_resid = bp->b_bcount; 1644 return 0; 1645 } 1646 if (sense->extended_flags & SSD_ILI) { 1647 if (info < 0) { 1648 /* 1649 * the record was bigger than the read 1650 */ 1651 if ((xs->flags & SCSI_SILENT) == 0) 1652 printf("%s: %d-byte record too big\n", 1653 st->sc_dev.dv_xname, 1654 xs->datalen - info); 1655 return EIO; 1656 } 1657 xs->resid = info; 1658 if (bp) 1659 bp->b_resid = info; 1660 } 1661 } 1662 key = sense->extended_flags & SSD_KEY; 1663 1664 if (key == 0x8) { 1665 /* 1666 * This quirk code helps the drive read the 1667 * first tape block, regardless of format. That 1668 * is required for these drives to return proper 1669 * MODE SENSE information. 1670 */ 1671 if ((st->quirks & ST_Q_SENSE_HELP) && 1672 !(sc_link->flags & SDEV_MEDIA_LOADED)) { 1673 /* still starting */ 1674 st->blksize -= 512; 1675 } else if (!(st->flags & (ST_2FM_AT_EOD | ST_BLANK_READ))) { 1676 st->flags |= ST_BLANK_READ; 1677 xs->resid = xs->datalen; 1678 if (bp) { 1679 bp->b_resid = xs->resid; 1680 /* return an EOF */ 1681 } 1682 return 0; 1683 } 1684 } 1685 return -1; /* let the default/generic handler handle it */ 1686 } 1687 1688 /* 1689 * The quirk here is that the drive returns some value to st_mode_sense 1690 * incorrectly until the tape has actually passed by the head. 1691 * 1692 * The method is to set the drive to large fixed-block state (user-specified 1693 * density and 1024-byte blocks), then read and rewind to get it to sense the 1694 * tape. If that doesn't work, try 512-byte fixed blocks. If that doesn't 1695 * work, as a last resort, try variable- length blocks. The result will be 1696 * the ability to do an accurate st_mode_sense. 1697 * 1698 * We know we can do a rewind because we just did a load, which implies rewind. 1699 * Rewind seems preferable to space backward if we have a virgin tape. 1700 * 1701 * The rest of the code for this quirk is in ILI processing and BLANK CHECK 1702 * error processing, both part of st_interpret_sense. 1703 */ 1704 int 1705 st_touch_tape(st) 1706 struct st_softc *st; 1707 { 1708 char *buf; 1709 int readsize; 1710 int error; 1711 1712 buf = malloc(1024, M_TEMP, M_NOWAIT); 1713 if (!buf) 1714 return ENOMEM; 1715 1716 if (error = st_mode_sense(st, 0)) 1717 goto bad; 1718 st->blksize = 1024; 1719 do { 1720 switch (st->blksize) { 1721 case 512: 1722 case 1024: 1723 readsize = st->blksize; 1724 st->flags |= ST_FIXEDBLOCKS; 1725 break; 1726 default: 1727 readsize = 1; 1728 st->flags &= ~ST_FIXEDBLOCKS; 1729 } 1730 if (error = st_mode_select(st, 0)) 1731 goto bad; 1732 st_read(st, buf, readsize, SCSI_SILENT); /* XXX */ 1733 if (error = st_rewind(st, 0, 0)) { 1734 bad: free(buf, M_TEMP); 1735 return error; 1736 } 1737 } while (readsize != 1 && readsize > st->blksize); 1738 1739 free(buf, M_TEMP); 1740 return 0; 1741 } 1742 1743 int 1744 stdump() 1745 { 1746 1747 /* Not implemented. */ 1748 return EINVAL; 1749 } 1750