1 /* $OpenBSD: st.c,v 1.132 2016/09/04 10:51:24 naddy Exp $ */ 2 /* $NetBSD: st.c,v 1.71 1997/02/21 23:03:49 thorpej Exp $ */ 3 4 /* 5 * Copyright (c) 1994 Charles Hannum. All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 3. All advertising materials mentioning features or use of this software 16 * must display the following acknowledgement: 17 * This product includes software developed by Charles Hannum. 18 * 4. The name of the author may not be used to endorse or promote products 19 * derived from this software without specific prior written permission. 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 22 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 23 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 24 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 25 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 26 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 30 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 31 */ 32 33 /* 34 * Originally written by Julian Elischer (julian@tfs.com) 35 * for TRW Financial Systems for use under the MACH(2.5) operating system. 36 * 37 * TRW Financial Systems, in accordance with their agreement with Carnegie 38 * Mellon University, makes this software available to CMU to distribute 39 * or use in any manner that they see fit as long as this message is kept with 40 * the software. For this reason TFS also grants any other persons or 41 * organisations permission to use or modify this software. 42 * 43 * TFS supplies this software to be publicly redistributed 44 * on the understanding that TFS is not responsible for the correct 45 * functioning of this software in any circumstances. 46 * 47 * Ported to run under 386BSD by Julian Elischer (julian@tfs.com) Sept 1992 48 * major changes by Julian Elischer (julian@jules.dialix.oz.au) May 1993 49 */ 50 51 /* 52 * To do: 53 * work out some better way of guessing what a good timeout is going 54 * to be depending on whether we expect to retension or not. 55 */ 56 57 #include <sys/types.h> 58 #include <sys/param.h> 59 #include <sys/systm.h> 60 #include <sys/timeout.h> 61 #include <sys/fcntl.h> 62 #include <sys/errno.h> 63 #include <sys/ioctl.h> 64 #include <sys/stat.h> 65 #include <sys/pool.h> 66 #include <sys/buf.h> 67 #include <sys/mtio.h> 68 #include <sys/device.h> 69 #include <sys/conf.h> 70 #include <sys/vnode.h> 71 72 #include <scsi/scsi_all.h> 73 #include <scsi/scsi_tape.h> 74 #include <scsi/scsiconf.h> 75 76 /* Defines for device specific stuff */ 77 #define DEF_FIXED_BSIZE 512 78 79 #define STMODE(z) ( minor(z) & 0x03) 80 #define STUNIT(z) ((minor(z) >> 4) ) 81 82 #define STMINOR(unit, mode) (((unit) << 4) + (mode)) 83 #define MAXSTMODES 16 84 85 #define ST_IO_TIME (3 * 60 * 1000) /* 3 minutes */ 86 #define ST_CTL_TIME (30 * 1000) /* 30 seconds */ 87 #define ST_SPC_TIME (4 * 60 * 60 * 1000) /* 4 hours */ 88 89 /* 90 * Maximum density code allowed in SCSI spec (SSC2R08f, Section 8.3). 91 */ 92 #define SCSI_MAX_DENSITY_CODE 0xff 93 94 /* 95 * Define various devices that we know mis-behave in some way, 96 * and note how they are bad, so we can correct for them 97 */ 98 struct modes { 99 u_int quirks; /* same definitions as in quirkdata */ 100 int blksize; 101 u_int8_t density; 102 }; 103 104 struct quirkdata { 105 u_int quirks; 106 #define ST_Q_FORCE_BLKSIZE 0x0001 107 #define ST_Q_SENSE_HELP 0x0002 /* must do READ for good MODE SENSE */ 108 #define ST_Q_IGNORE_LOADS 0x0004 109 #define ST_Q_BLKSIZE 0x0008 /* variable-block media_blksize > 0 */ 110 #define ST_Q_UNIMODAL 0x0010 /* unimode drive rejects mode select */ 111 struct modes modes; 112 }; 113 114 struct st_quirk_inquiry_pattern { 115 struct scsi_inquiry_pattern pattern; 116 struct quirkdata quirkdata; 117 }; 118 119 const struct st_quirk_inquiry_pattern st_quirk_patterns[] = { 120 {{T_SEQUENTIAL, T_REMOV, 121 " ", " ", " "}, {0, 122 {ST_Q_FORCE_BLKSIZE, 512, 0}}}, /* minor 0-3 */ 123 {{T_SEQUENTIAL, T_REMOV, 124 "TANDBERG", " TDC 3600 ", ""}, {0, 125 {0, 0, 0}}}, /* minor 0-3 */ 126 {{T_SEQUENTIAL, T_REMOV, 127 "TANDBERG", " TDC 3800 ", ""}, {0, 128 {ST_Q_FORCE_BLKSIZE, 512, 0}}}, /* minor 0-3 */ 129 /* 130 * At least -005 and -007 need this. I'll assume they all do unless I 131 * hear otherwise. - mycroft, 31MAR1994 132 */ 133 {{T_SEQUENTIAL, T_REMOV, 134 "ARCHIVE ", "VIPER 2525 25462", ""}, {0, 135 {ST_Q_SENSE_HELP, 0, 0}}}, /* minor 0-3 */ 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, 142 {ST_Q_FORCE_BLKSIZE, 512, 0}}}, /* minor 0-3 */ 143 {{T_SEQUENTIAL, T_REMOV, 144 "ANRITSU ", "DMT780 ", ""}, {0, 145 {ST_Q_FORCE_BLKSIZE, 512, 0}}}, /* minor 0-3 */ 146 {{T_SEQUENTIAL, T_REMOV, 147 "ARCHIVE ", "VIPER 150 21247", ""}, {0, 148 {0, 0, 0}}}, /* minor 0-3 */ 149 {{T_SEQUENTIAL, T_REMOV, 150 "ARCHIVE ", "VIPER 150 21531", ""}, {0, 151 {ST_Q_SENSE_HELP, 0, 0}}}, /* minor 0-3 */ 152 {{T_SEQUENTIAL, T_REMOV, 153 "WANGTEK ", "5099ES SCSI", ""}, {0, 154 {ST_Q_FORCE_BLKSIZE, 512, 0}}}, /* minor 0-3 */ 155 {{T_SEQUENTIAL, T_REMOV, 156 "WANGTEK ", "5150ES SCSI", ""}, {0, 157 {ST_Q_FORCE_BLKSIZE, 512, 0}}}, /* minor 0-3 */ 158 {{T_SEQUENTIAL, T_REMOV, 159 "WANGTEK ", "5525ES SCSI REV7", ""}, {0, 160 {0, 0, 0}}}, /* minor 0-3 */ 161 {{T_SEQUENTIAL, T_REMOV, 162 "WangDAT ", "Model 1300 ", ""}, {0, 163 {0, 0, 0}}}, /* minor 0-3 */ 164 {{T_SEQUENTIAL, T_REMOV, 165 "EXABYTE ", "EXB-8200 ", "263H"}, {0, 166 {0, 0, 0}}}, /* minor 0-3 */ 167 {{T_SEQUENTIAL, T_REMOV, 168 "HP ", "T4000s ", ""}, {ST_Q_UNIMODAL, 169 {0, 0, QIC_3095}}}, /* minor 0-3 */ 170 #if 0 171 {{T_SEQUENTIAL, T_REMOV, 172 "EXABYTE ", "EXB-8200 ", ""}, {0, 173 {0, 0, 0}}}, /* minor 0-3 */ 174 #endif 175 {{T_SEQUENTIAL, T_REMOV, 176 "WANGTEK ", "5150ES SCSI FA15\0""01 A", "????"}, {0, 177 {ST_Q_IGNORE_LOADS, 0, 0}}}, /* minor 0-3 */ 178 {{T_SEQUENTIAL, T_REMOV, 179 "TEAC ", "MT-2ST/N50 ", ""}, {ST_Q_IGNORE_LOADS, 180 {0, 0, 0}}}, /* minor 0-3 */ 181 }; 182 183 #define NOEJECT 0 184 #define EJECT 1 185 186 #define NOREWIND 0 187 #define DOREWIND 1 188 189 struct st_softc { 190 struct device sc_dev; 191 192 int flags; /* see below */ 193 u_int quirks; /* quirks for the open mode */ 194 int blksize; /* blksize we are using */ 195 u_int8_t density; /* present density */ 196 short mt_resid; /* last (short) resid */ 197 short mt_erreg; /* last error (sense key) seen */ 198 199 struct scsi_link *sc_link; /* our link to the adpter etc. */ 200 201 int blkmin; /* min blk size */ 202 int blkmax; /* max blk size */ 203 const struct quirkdata *quirkdata; /* if we have a rogue entry */ 204 205 u_int64_t numblks; /* nominal blocks capacity */ 206 u_int32_t media_blksize; /* 0 if not ST_FIXEDBLOCKS */ 207 u_int32_t media_density; /* this is what it said when asked */ 208 int media_fileno; /* relative to BOT. -1 means unknown. */ 209 int media_blkno; /* relative to BOF. -1 means unknown. */ 210 int media_eom; /* relative to BOT. -1 means unknown. */ 211 212 u_int drive_quirks; /* quirks of this drive */ 213 214 struct modes modes; /* plus more for each mode */ 215 u_int8_t modeflags; /* flags for the modes */ 216 #define DENSITY_SET_BY_USER 0x01 217 #define DENSITY_SET_BY_QUIRK 0x02 218 #define BLKSIZE_SET_BY_USER 0x04 219 #define BLKSIZE_SET_BY_QUIRK 0x08 220 221 struct bufq sc_bufq; 222 struct timeout sc_timeout; 223 struct scsi_xshandler sc_xsh; 224 }; 225 226 227 int stmatch(struct device *, void *, void *); 228 void stattach(struct device *, struct device *, void *); 229 int stactivate(struct device *, int); 230 int stdetach(struct device *, int); 231 232 void stminphys(struct buf *); 233 void st_identify_drive(struct st_softc *, struct scsi_inquiry_data *); 234 void st_loadquirks(struct st_softc *); 235 int st_mount_tape(dev_t, int); 236 void st_unmount(struct st_softc *, int, int); 237 int st_decide_mode(struct st_softc *, int); 238 void ststart(struct scsi_xfer *); 239 void st_buf_done(struct scsi_xfer *); 240 int st_read(struct st_softc *, char *, int, int); 241 int st_read_block_limits(struct st_softc *, int); 242 int st_mode_sense(struct st_softc *, int); 243 int st_mode_select(struct st_softc *, int); 244 int st_space(struct st_softc *, int, u_int, int); 245 int st_write_filemarks(struct st_softc *, int, int); 246 int st_check_eod(struct st_softc *, int, int *, int); 247 int st_load(struct st_softc *, u_int, int); 248 int st_rewind(struct st_softc *, u_int, int); 249 int st_interpret_sense(struct scsi_xfer *); 250 int st_touch_tape(struct st_softc *); 251 int st_erase(struct st_softc *, int, int); 252 253 struct cfattach st_ca = { 254 sizeof(struct st_softc), stmatch, stattach, 255 stdetach, stactivate 256 }; 257 258 struct cfdriver st_cd = { 259 NULL, "st", DV_TAPE 260 }; 261 262 #define ST_INFO_VALID 0x0001 263 #define ST_BLOCK_SET 0x0002 /* block size, mode set by ioctl */ 264 #define ST_WRITTEN 0x0004 /* data have been written, EOD needed */ 265 #define ST_FIXEDBLOCKS 0x0008 266 #define ST_AT_FILEMARK 0x0010 267 #define ST_EIO_PENDING 0x0020 /* we couldn't report it then (had data) */ 268 #define ST_EOM_PENDING 0x0040 /* we couldn't report it then (had data) */ 269 #define ST_EOD_DETECTED 0x0080 270 #define ST_FM_WRITTEN 0x0100 /* 271 * EOF file mark written -- used with 272 * ~ST_WRITTEN to indicate that multiple file 273 * marks have been written 274 */ 275 #define ST_BLANK_READ 0x0200 /* BLANK CHECK encountered already */ 276 #define ST_2FM_AT_EOD 0x0400 /* write 2 file marks at EOD */ 277 #define ST_MOUNTED 0x0800 /* Device is presently mounted */ 278 #define ST_DONTBUFFER 0x1000 /* Disable buffering/caching */ 279 #define ST_WAITING 0x2000 280 #define ST_DYING 0x4000 /* dying, when deactivated */ 281 #define ST_BOD_DETECTED 0x8000 282 283 #define ST_PER_ACTION (ST_AT_FILEMARK | ST_EIO_PENDING | ST_EOM_PENDING | \ 284 ST_BLANK_READ) 285 286 #define stlookup(unit) (struct st_softc *)device_lookup(&st_cd, (unit)) 287 288 const struct scsi_inquiry_pattern st_patterns[] = { 289 {T_SEQUENTIAL, T_REMOV, 290 "", "", ""}, 291 }; 292 293 int 294 stmatch(struct device *parent, void *match, void *aux) 295 { 296 struct scsi_attach_args *sa = aux; 297 int priority; 298 299 (void)scsi_inqmatch(sa->sa_inqbuf, 300 st_patterns, nitems(st_patterns), 301 sizeof(st_patterns[0]), &priority); 302 return (priority); 303 } 304 305 /* 306 * The routine called by the low level scsi routine when it discovers 307 * A device suitable for this driver 308 */ 309 void 310 stattach(struct device *parent, struct device *self, void *aux) 311 { 312 struct st_softc *st = (void *)self; 313 struct scsi_attach_args *sa = aux; 314 struct scsi_link *link = sa->sa_sc_link; 315 316 SC_DEBUG(link, SDEV_DB2, ("stattach:\n")); 317 318 /* 319 * Store information needed to contact our base driver 320 */ 321 st->sc_link = link; 322 link->interpret_sense = st_interpret_sense; 323 link->device_softc = st; 324 325 /* 326 * Check if the drive is a known criminal and take 327 * Any steps needed to bring it into line 328 */ 329 st_identify_drive(st, sa->sa_inqbuf); 330 printf("\n"); 331 332 scsi_xsh_set(&st->sc_xsh, link, ststart); 333 timeout_set(&st->sc_timeout, (void (*)(void *))scsi_xsh_set, 334 &st->sc_xsh); 335 336 /* Set up the buf queue for this device. */ 337 bufq_init(&st->sc_bufq, BUFQ_FIFO); 338 339 /* Start up with media position unknown. */ 340 st->media_fileno = -1; 341 st->media_blkno = -1; 342 st->media_eom = -1; 343 344 /* 345 * Reset the media loaded flag, sometimes the data 346 * acquired at boot time is not quite accurate. This 347 * will be checked again at the first open. 348 */ 349 link->flags &= ~SDEV_MEDIA_LOADED; 350 } 351 352 int 353 stactivate(struct device *self, int act) 354 { 355 struct st_softc *st = (struct st_softc *)self; 356 int rv = 0; 357 358 switch (act) { 359 case DVACT_DEACTIVATE: 360 st->flags |= ST_DYING; 361 scsi_xsh_del(&st->sc_xsh); 362 break; 363 } 364 365 return (rv); 366 } 367 368 int 369 stdetach(struct device *self, int flags) 370 { 371 struct st_softc *st = (struct st_softc *)self; 372 int cmaj, mn; 373 374 bufq_drain(&st->sc_bufq); 375 376 /* Locate the lowest minor number to be detached. */ 377 mn = STMINOR(self->dv_unit, 0); 378 379 for (cmaj = 0; cmaj < nchrdev; cmaj++) 380 if (cdevsw[cmaj].d_open == stopen) 381 vdevgone(cmaj, mn, mn + MAXSTMODES - 1, VCHR); 382 383 bufq_destroy(&st->sc_bufq); 384 385 return (0); 386 } 387 388 /* 389 * Use the inquiry routine in 'scsi_base' to get drive info so we can 390 * Further tailor our behaviour. 391 */ 392 void 393 st_identify_drive(struct st_softc *st, struct scsi_inquiry_data *inqbuf) 394 { 395 const struct st_quirk_inquiry_pattern *finger; 396 int priority; 397 398 finger = (const struct st_quirk_inquiry_pattern *)scsi_inqmatch(inqbuf, 399 st_quirk_patterns, 400 nitems(st_quirk_patterns), 401 sizeof(st_quirk_patterns[0]), &priority); 402 if (priority != 0) { 403 st->quirkdata = &finger->quirkdata; 404 st->drive_quirks = finger->quirkdata.quirks; 405 st->quirks = finger->quirkdata.quirks; /* start value */ 406 st_loadquirks(st); 407 } 408 } 409 410 /* 411 * initialise the subdevices to the default (QUIRK) state. 412 * this will remove any setting made by the system operator or previous 413 * operations. 414 */ 415 void 416 st_loadquirks(struct st_softc *st) 417 { 418 const struct modes *mode; 419 struct modes *mode2; 420 421 mode = &st->quirkdata->modes; 422 mode2 = &st->modes; 423 bzero(mode2, sizeof(struct modes)); 424 st->modeflags &= ~(BLKSIZE_SET_BY_QUIRK | 425 DENSITY_SET_BY_QUIRK | BLKSIZE_SET_BY_USER | 426 DENSITY_SET_BY_USER); 427 if ((mode->quirks | st->drive_quirks) & ST_Q_FORCE_BLKSIZE) { 428 mode2->blksize = mode->blksize; 429 st->modeflags |= BLKSIZE_SET_BY_QUIRK; 430 } 431 if (mode->density) { 432 mode2->density = mode->density; 433 st->modeflags |= DENSITY_SET_BY_QUIRK; 434 } 435 } 436 437 /* 438 * open the device. 439 */ 440 int 441 stopen(dev_t dev, int flags, int fmt, struct proc *p) 442 { 443 struct scsi_link *link; 444 struct st_softc *st; 445 int error = 0; 446 447 st = stlookup(STUNIT(dev)); 448 if (st == NULL) 449 return (ENXIO); 450 link = st->sc_link; 451 452 if (st->flags & ST_DYING) { 453 error = ENXIO; 454 goto done; 455 } 456 if (ISSET(flags, FWRITE) && ISSET(link->flags, SDEV_READONLY)) { 457 error = EACCES; 458 goto done; 459 } 460 461 SC_DEBUG(link, SDEV_DB1, ("open: dev=0x%x (unit %d (of %d))\n", dev, 462 STUNIT(dev), st_cd.cd_ndevs)); 463 464 /* 465 * Tape is an exclusive media. Only one open at a time. 466 */ 467 if (link->flags & SDEV_OPEN) { 468 SC_DEBUG(link, SDEV_DB4, ("already open\n")); 469 error = EBUSY; 470 goto done; 471 } 472 473 /* Use st_interpret_sense() now. */ 474 link->flags |= SDEV_OPEN; 475 476 /* 477 * Check the unit status. This clears any outstanding errors and 478 * will ensure that media is present. 479 */ 480 error = scsi_test_unit_ready(link, TEST_READY_RETRIES, 481 SCSI_SILENT | SCSI_IGNORE_MEDIA_CHANGE | 482 SCSI_IGNORE_ILLEGAL_REQUEST); 483 484 /* 485 * Terminate any exising mount session if there is no media. 486 */ 487 if ((link->flags & SDEV_MEDIA_LOADED) == 0) 488 st_unmount(st, NOEJECT, DOREWIND); 489 490 if (error) { 491 link->flags &= ~SDEV_OPEN; 492 goto done; 493 } 494 495 if ((st->flags & ST_MOUNTED) == 0) { 496 error = st_mount_tape(dev, flags); 497 if (error) { 498 link->flags &= ~SDEV_OPEN; 499 goto done; 500 } 501 } 502 503 /* 504 * Make sure that a tape opened in write-only mode will have 505 * file marks written on it when closed, even if not written to. 506 * This is for SUN compatibility 507 */ 508 if ((flags & O_ACCMODE) == FWRITE) 509 st->flags |= ST_WRITTEN; 510 511 done: 512 SC_DEBUG(link, SDEV_DB2, ("open complete\n")); 513 device_unref(&st->sc_dev); 514 return (error); 515 } 516 517 /* 518 * close the device.. only called if we are the LAST 519 * occurrence of an open device 520 */ 521 int 522 stclose(dev_t dev, int flags, int mode, struct proc *p) 523 { 524 struct scsi_link *link; 525 struct st_softc *st; 526 int error = 0; 527 528 st = stlookup(STUNIT(dev)); 529 if (st == NULL) 530 return (ENXIO); 531 if (st->flags & ST_DYING) { 532 error = ENXIO; 533 goto done; 534 } 535 link = st->sc_link; 536 537 SC_DEBUG(link, SDEV_DB1, ("closing\n")); 538 539 if ((st->flags & (ST_WRITTEN | ST_FM_WRITTEN)) == ST_WRITTEN) 540 st_write_filemarks(st, 1, 0); 541 542 switch (STMODE(dev)) { 543 case 0: /* normal */ 544 st_unmount(st, NOEJECT, DOREWIND); 545 break; 546 case 3: /* eject, no rewind */ 547 st_unmount(st, EJECT, NOREWIND); 548 break; 549 case 1: /* no rewind */ 550 /* leave mounted unless media seems to have been removed */ 551 if (!(link->flags & SDEV_MEDIA_LOADED)) 552 st_unmount(st, NOEJECT, NOREWIND); 553 break; 554 case 2: /* rewind, eject */ 555 st_unmount(st, EJECT, DOREWIND); 556 break; 557 } 558 link->flags &= ~SDEV_OPEN; 559 timeout_del(&st->sc_timeout); 560 scsi_xsh_del(&st->sc_xsh); 561 562 done: 563 device_unref(&st->sc_dev); 564 return (error); 565 } 566 567 /* 568 * Start a new mount session. 569 * Copy in all the default parameters from the selected device mode. 570 * and try guess any that seem to be defaulted. 571 */ 572 int 573 st_mount_tape(dev_t dev, int flags) 574 { 575 struct st_softc *st; 576 struct scsi_link *link; 577 int error = 0; 578 579 st = stlookup(STUNIT(dev)); 580 if (st == NULL) 581 return (ENXIO); 582 if (st->flags & ST_DYING) { 583 error = ENXIO; 584 goto done; 585 } 586 link = st->sc_link; 587 588 SC_DEBUG(link, SDEV_DB1, ("mounting\n")); 589 590 if (st->flags & ST_MOUNTED) 591 goto done; 592 593 st->quirks = st->drive_quirks | st->modes.quirks; 594 /* 595 * If the media is new, then make sure we give it a chance to 596 * to do a 'load' instruction. (We assume it is new.) 597 */ 598 if ((error = st_load(st, LD_LOAD, 0)) != 0) 599 goto done; 600 601 /* 602 * Throw another dummy instruction to catch 603 * 'Unit attention' errors. Some drives appear to give 604 * these after doing a Load instruction. 605 * (noteably some DAT drives) 606 */ 607 /* XXX */ 608 scsi_test_unit_ready(link, TEST_READY_RETRIES, SCSI_SILENT); 609 610 /* 611 * Some devices can't tell you much until they have been 612 * asked to look at the media. This quirk does this. 613 */ 614 if (st->quirks & ST_Q_SENSE_HELP) 615 if ((error = st_touch_tape(st)) != 0) 616 return error; 617 /* 618 * Load the physical device parameters 619 * loads: blkmin, blkmax 620 */ 621 if (!(link->flags & SDEV_ATAPI) && 622 (error = st_read_block_limits(st, 0)) != 0) 623 goto done; 624 625 /* 626 * Load the media dependent parameters 627 * includes: media_blksize,media_density,numblks 628 * As we have a tape in, it should be reflected here. 629 * If not you may need the "quirk" above. 630 */ 631 if ((error = st_mode_sense(st, 0)) != 0) 632 goto done; 633 634 /* 635 * If we have gained a permanent density from somewhere, 636 * then use it in preference to the one supplied by 637 * default by the driver. 638 */ 639 if (st->modeflags & (DENSITY_SET_BY_QUIRK | DENSITY_SET_BY_USER)) 640 st->density = st->modes.density; 641 else 642 st->density = st->media_density; 643 /* 644 * If we have gained a permanent blocksize 645 * then use it in preference to the one supplied by 646 * default by the driver. 647 */ 648 st->flags &= ~ST_FIXEDBLOCKS; 649 if (st->modeflags & (BLKSIZE_SET_BY_QUIRK | BLKSIZE_SET_BY_USER)) { 650 st->blksize = st->modes.blksize; 651 if (st->blksize) 652 st->flags |= ST_FIXEDBLOCKS; 653 } else { 654 if ((error = st_decide_mode(st, 0)) != 0) 655 goto done; 656 } 657 if ((error = st_mode_select(st, 0)) != 0) { 658 printf("%s: cannot set selected mode\n", st->sc_dev.dv_xname); 659 goto done; 660 } 661 scsi_prevent(link, PR_PREVENT, 662 SCSI_IGNORE_ILLEGAL_REQUEST | SCSI_IGNORE_NOT_READY); 663 st->flags |= ST_MOUNTED; 664 link->flags |= SDEV_MEDIA_LOADED; /* move earlier? */ 665 st->media_fileno = 0; 666 st->media_blkno = 0; 667 st->media_eom = -1; 668 669 done: 670 device_unref(&st->sc_dev); 671 return (error); 672 } 673 674 /* 675 * End the present mount session. 676 * Rewind, and optionally eject the tape. 677 * Reset various flags to indicate that all new 678 * operations require another mount operation 679 */ 680 void 681 st_unmount(struct st_softc *st, int eject, int rewind) 682 { 683 struct scsi_link *link = st->sc_link; 684 int nmarks; 685 686 st->media_fileno = -1; 687 st->media_blkno = -1; 688 689 if (!(st->flags & ST_MOUNTED)) 690 return; 691 SC_DEBUG(link, SDEV_DB1, ("unmounting\n")); 692 st_check_eod(st, 0, &nmarks, SCSI_IGNORE_NOT_READY); 693 if (rewind) 694 st_rewind(st, 0, SCSI_IGNORE_NOT_READY); 695 scsi_prevent(link, PR_ALLOW, 696 SCSI_IGNORE_ILLEGAL_REQUEST | SCSI_IGNORE_NOT_READY); 697 if (eject) 698 st_load(st, LD_UNLOAD, SCSI_IGNORE_NOT_READY); 699 st->flags &= ~ST_MOUNTED; 700 link->flags &= ~SDEV_MEDIA_LOADED; 701 } 702 703 /* 704 * Given all we know about the device, media, mode, 'quirks' and 705 * initial operation, make a decision as to how we should be set 706 * to run (regarding blocking and EOD marks) 707 */ 708 int 709 st_decide_mode(struct st_softc *st, int first_read) 710 { 711 struct scsi_link *link = st->sc_link; 712 713 SC_DEBUG(link, SDEV_DB2, ("starting block mode decision\n")); 714 715 /* ATAPI tapes are always fixed blocksize. */ 716 if (link->flags & SDEV_ATAPI) { 717 st->flags |= ST_FIXEDBLOCKS; 718 if (st->media_blksize > 0) 719 st->blksize = st->media_blksize; 720 else 721 st->blksize = DEF_FIXED_BSIZE; 722 goto done; 723 } 724 725 /* 726 * If the drive can only handle fixed-length blocks and only at 727 * one size, perhaps we should just do that. 728 */ 729 if (st->blkmin && (st->blkmin == st->blkmax)) { 730 st->flags |= ST_FIXEDBLOCKS; 731 st->blksize = st->blkmin; 732 SC_DEBUG(link, SDEV_DB3, 733 ("blkmin == blkmax of %d\n", st->blkmin)); 734 goto done; 735 } 736 /* 737 * If the tape density mandates (or even suggests) use of fixed 738 * or variable-length blocks, comply. 739 */ 740 switch (st->density) { 741 case HALFINCH_800: 742 case HALFINCH_1600: 743 case HALFINCH_6250: 744 case DDS: 745 st->flags &= ~ST_FIXEDBLOCKS; 746 st->blksize = 0; 747 SC_DEBUG(link, SDEV_DB3, ("density specified variable\n")); 748 goto done; 749 case QIC_11: 750 case QIC_24: 751 case QIC_120: 752 case QIC_150: 753 case QIC_525: 754 case QIC_1320: 755 st->flags |= ST_FIXEDBLOCKS; 756 if (st->media_blksize > 0) 757 st->blksize = st->media_blksize; 758 else 759 st->blksize = DEF_FIXED_BSIZE; 760 SC_DEBUG(link, SDEV_DB3, ("density specified fixed\n")); 761 goto done; 762 } 763 /* 764 * If we're about to read the tape, perhaps we should choose 765 * fixed or variable-length blocks and block size according to 766 * what the drive found on the tape. 767 */ 768 if (first_read && 769 (!(st->quirks & ST_Q_BLKSIZE) || (st->media_blksize == 0) || 770 (st->media_blksize == DEF_FIXED_BSIZE) || 771 (st->media_blksize == 1024))) { 772 if (st->media_blksize > 0) 773 st->flags |= ST_FIXEDBLOCKS; 774 else 775 st->flags &= ~ST_FIXEDBLOCKS; 776 st->blksize = st->media_blksize; 777 SC_DEBUG(link, SDEV_DB3, 778 ("Used media_blksize of %d\n", st->media_blksize)); 779 goto done; 780 } 781 /* 782 * We're getting no hints from any direction. Choose variable- 783 * length blocks arbitrarily. 784 */ 785 st->flags &= ~ST_FIXEDBLOCKS; 786 st->blksize = 0; 787 SC_DEBUG(link, SDEV_DB3, 788 ("Give up and default to variable mode\n")); 789 790 done: 791 /* 792 * Decide whether or not to write two file marks to signify end- 793 * of-data. Make the decision as a function of density. If 794 * the decision is not to use a second file mark, the SCSI BLANK 795 * CHECK condition code will be recognized as end-of-data when 796 * first read. 797 * (I think this should be a by-product of fixed/variable..julian) 798 */ 799 switch (st->density) { 800 /* case 8 mm: What is the SCSI density code for 8 mm, anyway? */ 801 case QIC_11: 802 case QIC_24: 803 case QIC_120: 804 case QIC_150: 805 case QIC_525: 806 case QIC_1320: 807 st->flags &= ~ST_2FM_AT_EOD; 808 break; 809 default: 810 st->flags |= ST_2FM_AT_EOD; 811 } 812 return 0; 813 } 814 815 /* 816 * Actually translate the requested transfer into 817 * one the physical driver can understand 818 * The transfer is described by a buf and will include 819 * only one physical transfer. 820 */ 821 void 822 ststrategy(struct buf *bp) 823 { 824 struct scsi_link *link; 825 struct st_softc *st; 826 int s; 827 828 st = stlookup(STUNIT(bp->b_dev)); 829 if (st == NULL) { 830 bp->b_error = ENXIO; 831 goto bad; 832 } 833 if (st->flags & ST_DYING) { 834 bp->b_error = ENXIO; 835 goto bad; 836 } 837 838 link = st->sc_link; 839 840 SC_DEBUG(link, SDEV_DB2, ("ststrategy: %ld bytes @ blk %lld\n", 841 bp->b_bcount, (long long)bp->b_blkno)); 842 843 /* 844 * If it's a null transfer, return immediately. 845 */ 846 if (bp->b_bcount == 0) 847 goto done; 848 /* 849 * Odd sized request on fixed drives are verboten 850 */ 851 if (st->flags & ST_FIXEDBLOCKS) { 852 if (bp->b_bcount % st->blksize) { 853 printf("%s: bad request, must be multiple of %d\n", 854 st->sc_dev.dv_xname, st->blksize); 855 bp->b_error = EIO; 856 goto bad; 857 } 858 } 859 /* 860 * as are out-of-range requests on variable drives. 861 */ 862 else if (bp->b_bcount < st->blkmin || 863 (st->blkmax && bp->b_bcount > st->blkmax)) { 864 printf("%s: bad request, must be between %d and %d\n", 865 st->sc_dev.dv_xname, st->blkmin, st->blkmax); 866 bp->b_error = EIO; 867 goto bad; 868 } 869 870 /* 871 * Place it in the queue of activities for this tape 872 * at the end (a bit silly because we only have on user.. 873 * (but it could fork())) 874 */ 875 bufq_queue(&st->sc_bufq, bp); 876 877 /* 878 * Tell the device to get going on the transfer if it's 879 * not doing anything, otherwise just wait for completion 880 * (All a bit silly if we're only allowing 1 open but..) 881 */ 882 scsi_xsh_add(&st->sc_xsh); 883 884 device_unref(&st->sc_dev); 885 return; 886 bad: 887 bp->b_flags |= B_ERROR; 888 done: 889 /* Set b_resid to indicate no xfer was done. */ 890 bp->b_resid = bp->b_bcount; 891 s = splbio(); 892 biodone(bp); 893 splx(s); 894 if (st) 895 device_unref(&st->sc_dev); 896 } 897 898 void 899 ststart(struct scsi_xfer *xs) 900 { 901 struct scsi_link *link = xs->sc_link; 902 struct st_softc *st = link->device_softc; 903 struct buf *bp; 904 struct scsi_rw_tape *cmd; 905 int s; 906 907 SC_DEBUG(link, SDEV_DB2, ("ststart\n")); 908 909 if (st->flags & ST_DYING) { 910 scsi_xs_put(xs); 911 return; 912 } 913 914 /* 915 * if the device has been unmounted by the user 916 * then throw away all requests until done 917 */ 918 if (!(st->flags & ST_MOUNTED) || 919 !(link->flags & SDEV_MEDIA_LOADED)) { 920 /* make sure that one implies the other.. */ 921 link->flags &= ~SDEV_MEDIA_LOADED; 922 bufq_drain(&st->sc_bufq); 923 scsi_xs_put(xs); 924 return; 925 } 926 927 for (;;) { 928 bp = bufq_dequeue(&st->sc_bufq); 929 if (bp == NULL) { 930 scsi_xs_put(xs); 931 return; 932 } 933 934 /* 935 * Only FIXEDBLOCK devices have pending I/O or space 936 * operations. 937 */ 938 if (st->flags & ST_FIXEDBLOCKS) { 939 /* 940 * If we are at a filemark but have not reported it yet 941 * then we should report it now 942 */ 943 if (st->flags & ST_AT_FILEMARK) { 944 if ((bp->b_flags & B_READ) == B_WRITE) { 945 /* 946 * Handling of ST_AT_FILEMARK in 947 * st_space will fill in the right file 948 * mark count. 949 * Back up over filemark 950 */ 951 if (st_space(st, 0, SP_FILEMARKS, 0)) { 952 bp->b_flags |= B_ERROR; 953 bp->b_resid = bp->b_bcount; 954 bp->b_error = EIO; 955 s = splbio(); 956 biodone(bp); 957 splx(s); 958 continue; 959 } 960 } else { 961 bp->b_resid = bp->b_bcount; 962 bp->b_error = 0; 963 bp->b_flags &= ~B_ERROR; 964 st->flags &= ~ST_AT_FILEMARK; 965 s = splbio(); 966 biodone(bp); 967 splx(s); 968 continue; /* seek more work */ 969 } 970 } 971 } 972 973 /* 974 * If we are at EIO or EOM but have not reported it 975 * yet then we should report it now. 976 */ 977 if (st->flags & (ST_EOM_PENDING | ST_EIO_PENDING)) { 978 bp->b_resid = bp->b_bcount; 979 if (st->flags & ST_EIO_PENDING) { 980 bp->b_error = EIO; 981 bp->b_flags |= B_ERROR; 982 } 983 st->flags &= ~(ST_EOM_PENDING | ST_EIO_PENDING); 984 s = splbio(); 985 biodone(bp); 986 splx(s); 987 continue; /* seek more work */ 988 } 989 break; 990 } 991 992 /* 993 * Fill out the scsi command 994 */ 995 cmd = (struct scsi_rw_tape *)xs->cmd; 996 bzero(cmd, sizeof(*cmd)); 997 if ((bp->b_flags & B_READ) == B_WRITE) { 998 cmd->opcode = WRITE; 999 st->flags &= ~ST_FM_WRITTEN; 1000 st->flags |= ST_WRITTEN; 1001 xs->flags |= SCSI_DATA_OUT; 1002 } else { 1003 cmd->opcode = READ; 1004 xs->flags |= SCSI_DATA_IN; 1005 } 1006 1007 /* 1008 * Handle "fixed-block-mode" tape drives by using the 1009 * block count instead of the length. 1010 */ 1011 if (st->flags & ST_FIXEDBLOCKS) { 1012 cmd->byte2 |= SRW_FIXED; 1013 _lto3b(bp->b_bcount / st->blksize, cmd->len); 1014 } else 1015 _lto3b(bp->b_bcount, cmd->len); 1016 1017 if (st->media_blkno != -1) { 1018 /* Update block count now, errors will set it to -1. */ 1019 if (st->flags & ST_FIXEDBLOCKS) 1020 st->media_blkno += _3btol(cmd->len); 1021 else if (_3btol(cmd->len) != 0) 1022 st->media_blkno++; 1023 } 1024 1025 xs->cmdlen = sizeof(*cmd); 1026 xs->timeout = ST_IO_TIME; 1027 xs->data = bp->b_data; 1028 xs->datalen = bp->b_bcount; 1029 xs->done = st_buf_done; 1030 xs->cookie = bp; 1031 xs->bp = bp; 1032 1033 /* 1034 * go ask the adapter to do all this for us 1035 */ 1036 scsi_xs_exec(xs); 1037 1038 /* 1039 * should we try do more work now? 1040 */ 1041 if (ISSET(st->flags, ST_WAITING)) 1042 CLR(st->flags, ST_WAITING); 1043 else if (bufq_peek(&st->sc_bufq)) 1044 scsi_xsh_add(&st->sc_xsh); 1045 } 1046 1047 void 1048 st_buf_done(struct scsi_xfer *xs) 1049 { 1050 struct st_softc *st = xs->sc_link->device_softc; 1051 struct buf *bp = xs->cookie; 1052 int error, s; 1053 1054 switch (xs->error) { 1055 case XS_NOERROR: 1056 bp->b_error = 0; 1057 bp->b_resid = xs->resid; 1058 break; 1059 1060 case XS_NO_CCB: 1061 /* The adapter is busy, requeue the buf and try it later. */ 1062 bufq_requeue(&st->sc_bufq, bp); 1063 scsi_xs_put(xs); 1064 SET(st->flags, ST_WAITING); /* dont let ststart xsh_add */ 1065 timeout_add(&st->sc_timeout, 1); 1066 return; 1067 1068 case XS_SENSE: 1069 case XS_SHORTSENSE: 1070 #ifdef SCSIDEBUG 1071 scsi_sense_print_debug(xs); 1072 #endif 1073 error = st_interpret_sense(xs); 1074 if (error == 0) { 1075 bp->b_error = 0; 1076 bp->b_resid = xs->resid; 1077 break; 1078 } 1079 if (error != ERESTART) 1080 xs->retries = 0; 1081 goto retry; 1082 1083 case XS_BUSY: 1084 if (xs->retries) { 1085 if (scsi_delay(xs, 1) != ERESTART) 1086 xs->retries = 0; 1087 } 1088 goto retry; 1089 1090 case XS_TIMEOUT: 1091 retry: 1092 if (xs->retries--) { 1093 scsi_xs_exec(xs); 1094 return; 1095 } 1096 /* FALLTHROUGH */ 1097 1098 default: 1099 bp->b_error = EIO; 1100 bp->b_flags |= B_ERROR; 1101 bp->b_resid = bp->b_bcount; 1102 break; 1103 } 1104 1105 s = splbio(); 1106 biodone(bp); 1107 splx(s); 1108 scsi_xs_put(xs); 1109 } 1110 1111 void 1112 stminphys(struct buf *bp) 1113 { 1114 struct st_softc *st; 1115 1116 st = stlookup(STUNIT(bp->b_dev)); 1117 if (st == NULL) 1118 return; 1119 1120 (*st->sc_link->adapter->scsi_minphys)(bp, st->sc_link); 1121 1122 device_unref(&st->sc_dev); 1123 } 1124 1125 int 1126 stread(dev_t dev, struct uio *uio, int iomode) 1127 { 1128 return (physio(ststrategy, dev, B_READ, stminphys, uio)); 1129 } 1130 1131 int 1132 stwrite(dev_t dev, struct uio *uio, int iomode) 1133 { 1134 return (physio(ststrategy, dev, B_WRITE, stminphys, uio)); 1135 } 1136 1137 /* 1138 * Perform special action on behalf of the user; 1139 * knows about the internals of this device 1140 */ 1141 int 1142 stioctl(dev_t dev, u_long cmd, caddr_t arg, int flag, struct proc *p) 1143 { 1144 int error = 0; 1145 int nmarks; 1146 int flags = 0; 1147 struct st_softc *st; 1148 int hold_blksize; 1149 u_int8_t hold_density; 1150 struct mtop *mt = (struct mtop *) arg; 1151 int number; 1152 1153 /* 1154 * Find the device that the user is talking about 1155 */ 1156 st = stlookup(STUNIT(dev)); 1157 if (st == NULL) 1158 return (ENXIO); 1159 1160 if (st->flags & ST_DYING) { 1161 error = ENXIO; 1162 goto done; 1163 } 1164 1165 hold_blksize = st->blksize; 1166 hold_density = st->density; 1167 1168 switch (cmd) { 1169 1170 case MTIOCGET: { 1171 struct mtget *g = (struct mtget *) arg; 1172 1173 /* 1174 * (to get the current state of READONLY) 1175 */ 1176 error = st_mode_sense(st, SCSI_SILENT); 1177 if (error) 1178 break; 1179 1180 SC_DEBUG(st->sc_link, SDEV_DB1, ("[ioctl: get status]\n")); 1181 bzero(g, sizeof(struct mtget)); 1182 g->mt_type = 0x7; /* Ultrix compat *//*? */ 1183 g->mt_blksiz = st->blksize; 1184 g->mt_density = st->density; 1185 g->mt_mblksiz = st->modes.blksize; 1186 g->mt_mdensity = st->modes.density; 1187 if (st->sc_link->flags & SDEV_READONLY) 1188 g->mt_dsreg |= MT_DS_RDONLY; 1189 if (st->flags & ST_MOUNTED) 1190 g->mt_dsreg |= MT_DS_MOUNTED; 1191 g->mt_resid = st->mt_resid; 1192 g->mt_erreg = st->mt_erreg; 1193 g->mt_fileno = st->media_fileno; 1194 g->mt_blkno = st->media_blkno; 1195 /* 1196 * clear latched errors. 1197 */ 1198 st->mt_resid = 0; 1199 st->mt_erreg = 0; 1200 break; 1201 } 1202 case MTIOCTOP: { 1203 1204 SC_DEBUG(st->sc_link, SDEV_DB1, 1205 ("[ioctl: op=0x%x count=0x%x]\n", mt->mt_op, mt->mt_count)); 1206 1207 number = mt->mt_count; 1208 switch (mt->mt_op) { 1209 case MTWEOF: /* write an end-of-file record */ 1210 error = st_write_filemarks(st, number, flags); 1211 break; 1212 case MTBSF: /* backward space file */ 1213 number = -number; 1214 case MTFSF: /* forward space file */ 1215 error = st_check_eod(st, 0, &nmarks, flags); 1216 if (!error) 1217 error = st_space(st, number - nmarks, 1218 SP_FILEMARKS, flags); 1219 break; 1220 case MTBSR: /* backward space record */ 1221 number = -number; 1222 case MTFSR: /* forward space record */ 1223 error = st_check_eod(st, 1, &nmarks, flags); 1224 if (!error) 1225 error = st_space(st, number, SP_BLKS, flags); 1226 break; 1227 case MTREW: /* rewind */ 1228 error = st_rewind(st, 0, flags); 1229 break; 1230 case MTOFFL: /* rewind and put the drive offline */ 1231 st_unmount(st, EJECT, DOREWIND); 1232 break; 1233 case MTNOP: /* no operation, sets status only */ 1234 break; 1235 case MTRETEN: /* retension the tape */ 1236 error = st_load(st, LD_RETENSION, flags); 1237 if (!error) 1238 error = st_load(st, LD_LOAD, flags); 1239 break; 1240 case MTEOM: /* forward space to end of media */ 1241 error = st_check_eod(st, 0, &nmarks, flags); 1242 if (!error) 1243 error = st_space(st, 1, SP_EOM, flags); 1244 break; 1245 case MTCACHE: /* enable controller cache */ 1246 st->flags &= ~ST_DONTBUFFER; 1247 goto try_new_value; 1248 case MTNOCACHE: /* disable controller cache */ 1249 st->flags |= ST_DONTBUFFER; 1250 goto try_new_value; 1251 case MTERASE: /* erase volume */ 1252 error = st_erase(st, number, flags); 1253 break; 1254 case MTSETBSIZ: /* Set block size for device */ 1255 if (number == 0) { 1256 st->flags &= ~ST_FIXEDBLOCKS; 1257 } else { 1258 if ((st->blkmin || st->blkmax) && 1259 (number < st->blkmin || 1260 number > st->blkmax)) { 1261 error = EINVAL; 1262 break; 1263 } 1264 st->flags |= ST_FIXEDBLOCKS; 1265 } 1266 st->blksize = number; 1267 st->flags |= ST_BLOCK_SET; /*XXX */ 1268 goto try_new_value; 1269 1270 case MTSETDNSTY: /* Set density for device and mode */ 1271 if (number < 0 || number > SCSI_MAX_DENSITY_CODE) { 1272 error = EINVAL; 1273 break; 1274 } else 1275 st->density = number; 1276 goto try_new_value; 1277 1278 default: 1279 error = EINVAL; 1280 } 1281 break; 1282 } 1283 case MTIOCIEOT: 1284 case MTIOCEEOT: 1285 break; 1286 1287 #if 0 1288 case MTIOCRDSPOS: 1289 error = st_rdpos(st, 0, (u_int32_t *) arg); 1290 break; 1291 1292 case MTIOCRDHPOS: 1293 error = st_rdpos(st, 1, (u_int32_t *) arg); 1294 break; 1295 1296 case MTIOCSLOCATE: 1297 error = st_setpos(st, 0, (u_int32_t *) arg); 1298 break; 1299 1300 case MTIOCHLOCATE: 1301 error = st_setpos(st, 1, (u_int32_t *) arg); 1302 break; 1303 #endif 1304 1305 default: 1306 error = scsi_do_ioctl(st->sc_link, cmd, arg, flag); 1307 break; 1308 } 1309 goto done; 1310 1311 try_new_value: 1312 /* 1313 * Check that the mode being asked for is aggreeable to the 1314 * drive. If not, put it back the way it was. 1315 */ 1316 if ((error = st_mode_select(st, 0)) != 0) {/* put it back as it was */ 1317 printf("%s: cannot set selected mode\n", st->sc_dev.dv_xname); 1318 st->density = hold_density; 1319 st->blksize = hold_blksize; 1320 if (st->blksize) 1321 st->flags |= ST_FIXEDBLOCKS; 1322 else 1323 st->flags &= ~ST_FIXEDBLOCKS; 1324 goto done; 1325 } 1326 /* 1327 * As the drive liked it, if we are setting a new default, 1328 * set it into the structures as such. 1329 */ 1330 switch (mt->mt_op) { 1331 case MTSETBSIZ: 1332 st->modes.blksize = st->blksize; 1333 st->modeflags |= BLKSIZE_SET_BY_USER; 1334 break; 1335 case MTSETDNSTY: 1336 st->modes.density = st->density; 1337 st->modeflags |= DENSITY_SET_BY_USER; 1338 break; 1339 } 1340 1341 done: 1342 device_unref(&st->sc_dev); 1343 return (error); 1344 } 1345 1346 /* 1347 * Do a synchronous read. 1348 */ 1349 int 1350 st_read(struct st_softc *st, char *buf, int size, int flags) 1351 { 1352 struct scsi_rw_tape *cmd; 1353 struct scsi_xfer *xs; 1354 int error; 1355 1356 if (size == 0) 1357 return 0; 1358 1359 xs = scsi_xs_get(st->sc_link, flags | SCSI_DATA_IN); 1360 if (xs == NULL) 1361 return (ENOMEM); 1362 xs->cmdlen = sizeof(*cmd); 1363 xs->data = buf; 1364 xs->datalen = size; 1365 xs->retries = 0; 1366 xs->timeout = ST_IO_TIME; 1367 1368 cmd = (struct scsi_rw_tape *)xs->cmd; 1369 cmd->opcode = READ; 1370 if (st->flags & ST_FIXEDBLOCKS) { 1371 cmd->byte2 |= SRW_FIXED; 1372 _lto3b(size / (st->blksize ? st->blksize : DEF_FIXED_BSIZE), 1373 cmd->len); 1374 } else 1375 _lto3b(size, cmd->len); 1376 1377 error = scsi_xs_sync(xs); 1378 scsi_xs_put(xs); 1379 1380 return (error); 1381 } 1382 1383 /* 1384 * Ask the drive what its min and max blk sizes are. 1385 */ 1386 int 1387 st_read_block_limits(struct st_softc *st, int flags) 1388 { 1389 struct scsi_block_limits_data *block_limits = NULL; 1390 struct scsi_block_limits *cmd; 1391 struct scsi_link *link = st->sc_link; 1392 struct scsi_xfer *xs; 1393 int error = 0; 1394 1395 if ((link->flags & SDEV_MEDIA_LOADED)) 1396 return (0); 1397 1398 block_limits = dma_alloc(sizeof(*block_limits), PR_NOWAIT); 1399 if (block_limits == NULL) 1400 return (ENOMEM); 1401 1402 xs = scsi_xs_get(link, flags | SCSI_DATA_IN); 1403 if (xs == NULL) { 1404 error = ENOMEM; 1405 goto done; 1406 } 1407 1408 xs->cmdlen = sizeof(*cmd); 1409 xs->data = (void *)block_limits; 1410 xs->datalen = sizeof(*block_limits); 1411 xs->timeout = ST_CTL_TIME; 1412 1413 cmd = (struct scsi_block_limits *)xs->cmd; 1414 cmd->opcode = READ_BLOCK_LIMITS; 1415 1416 error = scsi_xs_sync(xs); 1417 scsi_xs_put(xs); 1418 1419 if (error == 0) { 1420 st->blkmin = _2btol(block_limits->min_length); 1421 st->blkmax = _3btol(block_limits->max_length); 1422 SC_DEBUG(link, SDEV_DB3, 1423 ("(%d <= blksize <= %d)\n", st->blkmin, st->blkmax)); 1424 } 1425 1426 done: 1427 if (block_limits) 1428 dma_free(block_limits, sizeof(*block_limits)); 1429 return (error); 1430 } 1431 1432 /* 1433 * Get the scsi driver to send a full inquiry to the 1434 * device and use the results to fill out the global 1435 * parameter structure. 1436 * 1437 * called from: 1438 * attach 1439 * open 1440 * ioctl (to reset original blksize) 1441 */ 1442 int 1443 st_mode_sense(struct st_softc *st, int flags) 1444 { 1445 union scsi_mode_sense_buf *data = NULL; 1446 struct scsi_link *link = st->sc_link; 1447 u_int64_t block_count; 1448 u_int32_t density, block_size; 1449 u_char *page0 = NULL; 1450 u_int8_t dev_spec; 1451 int error = 0, big; 1452 1453 data = dma_alloc(sizeof(*data), PR_NOWAIT); 1454 if (data == NULL) 1455 return (ENOMEM); 1456 1457 /* 1458 * Ask for page 0 (vendor specific) mode sense data. 1459 */ 1460 error = scsi_do_mode_sense(link, 0, data, (void **)&page0, 1461 &density, &block_count, &block_size, 1, flags | SCSI_SILENT, &big); 1462 if (error != 0) 1463 goto done; 1464 1465 /* It is valid for no page0 to be available. */ 1466 1467 if (big) 1468 dev_spec = data->hdr_big.dev_spec; 1469 else 1470 dev_spec = data->hdr.dev_spec; 1471 1472 if (dev_spec & SMH_DSP_WRITE_PROT) 1473 SET(link->flags, SDEV_READONLY); 1474 else 1475 CLR(link->flags, SDEV_READONLY); 1476 1477 st->numblks = block_count; 1478 st->media_blksize = block_size; 1479 st->media_density = density; 1480 1481 SC_DEBUG(link, SDEV_DB3, 1482 ("density code 0x%x, %d-byte blocks, write-%s, ", 1483 st->media_density, st->media_blksize, 1484 link->flags & SDEV_READONLY ? "protected" : "enabled")); 1485 SC_DEBUGN(link, SDEV_DB3, 1486 ("%sbuffered\n", dev_spec & SMH_DSP_BUFF_MODE ? "" : "un")); 1487 1488 link->flags |= SDEV_MEDIA_LOADED; 1489 1490 done: 1491 if (data) 1492 dma_free(data, sizeof(*data)); 1493 return (error); 1494 } 1495 1496 /* 1497 * Send a filled out parameter structure to the drive to 1498 * set it into the desire modes etc. 1499 */ 1500 int 1501 st_mode_select(struct st_softc *st, int flags) 1502 { 1503 union scsi_mode_sense_buf *inbuf = NULL, *outbuf = NULL; 1504 struct scsi_blk_desc general; 1505 struct scsi_link *link = st->sc_link; 1506 u_int8_t *page0 = NULL; 1507 int error = 0, big, page0_size; 1508 1509 inbuf = dma_alloc(sizeof(*inbuf), PR_NOWAIT); 1510 if (inbuf == NULL) { 1511 error = ENOMEM; 1512 goto done; 1513 } 1514 outbuf = dma_alloc(sizeof(*outbuf), PR_NOWAIT | PR_ZERO); 1515 if (outbuf == NULL) { 1516 error = ENOMEM; 1517 goto done; 1518 } 1519 1520 /* 1521 * This quirk deals with drives that have only one valid mode and think 1522 * this gives them license to reject all mode selects, even if the 1523 * selected mode is the one that is supported. 1524 */ 1525 if (st->quirks & ST_Q_UNIMODAL) { 1526 SC_DEBUG(link, SDEV_DB3, 1527 ("not setting density 0x%x blksize 0x%x\n", 1528 st->density, st->blksize)); 1529 error = 0; 1530 goto done; 1531 } 1532 1533 if (link->flags & SDEV_ATAPI) { 1534 error = 0; 1535 goto done; 1536 } 1537 1538 bzero(&general, sizeof(general)); 1539 1540 general.density = st->density; 1541 if (st->flags & ST_FIXEDBLOCKS) 1542 _lto3b(st->blksize, general.blklen); 1543 1544 /* 1545 * Ask for page 0 (vendor specific) mode sense data. 1546 */ 1547 error = scsi_do_mode_sense(link, 0, inbuf, (void **)&page0, NULL, 1548 NULL, NULL, 1, flags | SCSI_SILENT, &big); 1549 if (error != 0) 1550 goto done; 1551 1552 if (page0 == NULL) { 1553 page0_size = 0; 1554 } else if (big == 0) { 1555 page0_size = inbuf->hdr.data_length + 1556 sizeof(inbuf->hdr.data_length) - sizeof(inbuf->hdr) - 1557 inbuf->hdr.blk_desc_len; 1558 memcpy(&outbuf->buf[sizeof(outbuf->hdr)+ sizeof(general)], 1559 page0, page0_size); 1560 } else { 1561 page0_size = _2btol(inbuf->hdr_big.data_length) + 1562 sizeof(inbuf->hdr_big.data_length) - 1563 sizeof(inbuf->hdr_big) - 1564 _2btol(inbuf->hdr_big.blk_desc_len); 1565 memcpy(&outbuf->buf[sizeof(outbuf->hdr_big) + sizeof(general)], 1566 page0, page0_size); 1567 } 1568 1569 /* 1570 * Set up for a mode select. 1571 */ 1572 if (big == 0) { 1573 outbuf->hdr.data_length = sizeof(outbuf->hdr) + 1574 sizeof(general) + page0_size - 1575 sizeof(outbuf->hdr.data_length); 1576 if ((st->flags & ST_DONTBUFFER) == 0) 1577 outbuf->hdr.dev_spec = SMH_DSP_BUFF_MODE_ON; 1578 outbuf->hdr.blk_desc_len = sizeof(general); 1579 memcpy(&outbuf->buf[sizeof(outbuf->hdr)], 1580 &general, sizeof(general)); 1581 error = scsi_mode_select(st->sc_link, 0, &outbuf->hdr, 1582 flags, ST_CTL_TIME); 1583 goto done; 1584 } 1585 1586 /* MODE SENSE (10) header was returned, so use MODE SELECT (10). */ 1587 _lto2b((sizeof(outbuf->hdr_big) + sizeof(general) + page0_size - 1588 sizeof(outbuf->hdr_big.data_length)), outbuf->hdr_big.data_length); 1589 if ((st->flags & ST_DONTBUFFER) == 0) 1590 outbuf->hdr_big.dev_spec = SMH_DSP_BUFF_MODE_ON; 1591 _lto2b(sizeof(general), outbuf->hdr_big.blk_desc_len); 1592 memcpy(&outbuf->buf[sizeof(outbuf->hdr_big)], &general, 1593 sizeof(general)); 1594 1595 error = scsi_mode_select_big(st->sc_link, 0, &outbuf->hdr_big, 1596 flags, ST_CTL_TIME); 1597 done: 1598 if (inbuf) 1599 dma_free(inbuf, sizeof(*inbuf)); 1600 if (outbuf) 1601 dma_free(outbuf, sizeof(*outbuf)); 1602 return (error); 1603 } 1604 1605 /* 1606 * issue an erase command 1607 */ 1608 int 1609 st_erase(struct st_softc *st, int full, int flags) 1610 { 1611 struct scsi_erase *cmd; 1612 struct scsi_xfer *xs; 1613 int error; 1614 1615 xs = scsi_xs_get(st->sc_link, flags); 1616 if (xs == NULL) 1617 return (ENOMEM); 1618 xs->cmdlen = sizeof(*cmd); 1619 1620 /* 1621 * Full erase means set LONG bit in erase command, which asks 1622 * the drive to erase the entire unit. Without this bit, we're 1623 * asking the drive to write an erase gap. 1624 */ 1625 cmd = (struct scsi_erase *)xs->cmd; 1626 cmd->opcode = ERASE; 1627 if (full) { 1628 cmd->byte2 = SE_IMMED|SE_LONG; 1629 xs->timeout = ST_SPC_TIME; 1630 } else { 1631 cmd->byte2 = SE_IMMED; 1632 xs->timeout = ST_IO_TIME; 1633 } 1634 1635 /* 1636 * XXX We always do this asynchronously, for now. How long should 1637 * we wait if we want to (eventually) to it synchronously? 1638 */ 1639 error = scsi_xs_sync(xs); 1640 scsi_xs_put(xs); 1641 1642 return (error); 1643 } 1644 1645 /* 1646 * skip N blocks/filemarks/seq filemarks/eom 1647 */ 1648 int 1649 st_space(struct st_softc *st, int number, u_int what, int flags) 1650 { 1651 struct scsi_space *cmd; 1652 struct scsi_xfer *xs; 1653 int error; 1654 1655 switch (what) { 1656 case SP_BLKS: 1657 if (st->flags & ST_PER_ACTION) { 1658 if (number > 0) { 1659 st->flags &= ~ST_PER_ACTION; 1660 return EIO; 1661 } else if (number < 0) { 1662 if (st->flags & ST_AT_FILEMARK) { 1663 /* 1664 * Handling of ST_AT_FILEMARK 1665 * in st_space will fill in the 1666 * right file mark count. 1667 */ 1668 error = st_space(st, 0, SP_FILEMARKS, 1669 flags); 1670 if (error) 1671 return error; 1672 } 1673 if (st->flags & ST_BLANK_READ) { 1674 st->flags &= ~ST_BLANK_READ; 1675 return EIO; 1676 } 1677 st->flags &= ~(ST_EIO_PENDING | ST_EOM_PENDING); 1678 } 1679 } 1680 break; 1681 case SP_FILEMARKS: 1682 if (st->flags & ST_EIO_PENDING) { 1683 if (number > 0) { 1684 /* pretend we just discovered the error */ 1685 st->flags &= ~ST_EIO_PENDING; 1686 return EIO; 1687 } else if (number < 0) { 1688 /* back away from the error */ 1689 st->flags &= ~ST_EIO_PENDING; 1690 } 1691 } 1692 if (st->flags & ST_AT_FILEMARK) { 1693 st->flags &= ~ST_AT_FILEMARK; 1694 number--; 1695 } 1696 if ((st->flags & ST_BLANK_READ) && (number < 0)) { 1697 /* back away from unwritten tape */ 1698 st->flags &= ~ST_BLANK_READ; 1699 number++; /* XXX dubious */ 1700 } 1701 break; 1702 case SP_EOM: 1703 if (st->flags & ST_EOM_PENDING) { 1704 /* We are already there. */ 1705 st->flags &= ~ST_EOM_PENDING; 1706 return (0); 1707 } 1708 if (st->flags & ST_EIO_PENDING) { 1709 /* pretend we just discovered the error */ 1710 st->flags &= ~ST_EIO_PENDING; 1711 return EIO; 1712 } 1713 if (st->flags & ST_AT_FILEMARK) 1714 st->flags &= ~ST_AT_FILEMARK; 1715 break; 1716 } 1717 if (number == 0) 1718 return 0; 1719 1720 xs = scsi_xs_get(st->sc_link, flags); 1721 if (xs == NULL) 1722 return (ENOMEM); 1723 1724 cmd = (struct scsi_space *)xs->cmd; 1725 cmd->opcode = SPACE; 1726 cmd->byte2 = what; 1727 _lto3b(number, cmd->number); 1728 xs->cmdlen = sizeof(*cmd); 1729 xs->timeout = ST_SPC_TIME; 1730 1731 CLR(st->flags, ST_EOD_DETECTED); 1732 1733 error = scsi_xs_sync(xs); 1734 scsi_xs_put(xs); 1735 1736 if (error != 0) { 1737 st->media_fileno = -1; 1738 st->media_blkno = -1; 1739 } else { 1740 switch (what) { 1741 case SP_BLKS: 1742 if (st->media_blkno != -1) { 1743 st->media_blkno += number; 1744 if (st->media_blkno < 0) 1745 st->media_blkno = -1; 1746 } 1747 break; 1748 case SP_FILEMARKS: 1749 if (st->media_fileno != -1) { 1750 if (!ISSET(st->flags, ST_EOD_DETECTED)) 1751 st->media_fileno += number; 1752 if (st->media_fileno > st->media_eom) 1753 st->media_eom = st->media_fileno; 1754 st->media_blkno = 0; 1755 } 1756 break; 1757 case SP_EOM: 1758 if (st->media_eom != -1) { 1759 st->media_fileno = st->media_eom; 1760 st->media_blkno = 0; 1761 } else { 1762 st->media_fileno = -1; 1763 st->media_blkno = -1; 1764 } 1765 break; 1766 default: 1767 st->media_fileno = -1; 1768 st->media_blkno = -1; 1769 break; 1770 } 1771 } 1772 1773 return (error); 1774 } 1775 1776 /* 1777 * write N filemarks 1778 */ 1779 int 1780 st_write_filemarks(struct st_softc *st, int number, int flags) 1781 { 1782 struct scsi_write_filemarks *cmd; 1783 struct scsi_xfer *xs; 1784 int error; 1785 1786 if (number < 0) 1787 return (EINVAL); 1788 1789 xs = scsi_xs_get(st->sc_link, flags); 1790 if (xs == NULL) 1791 return (ENOMEM); 1792 1793 xs->cmdlen = sizeof(*cmd); 1794 xs->timeout = ST_IO_TIME * 4; 1795 1796 switch (number) { 1797 case 0: /* really a command to sync the drive's buffers */ 1798 break; 1799 case 1: 1800 if (st->flags & ST_FM_WRITTEN) /* already have one down */ 1801 st->flags &= ~ST_WRITTEN; 1802 else 1803 st->flags |= ST_FM_WRITTEN; 1804 st->flags &= ~ST_PER_ACTION; 1805 break; 1806 default: 1807 st->flags &= ~(ST_PER_ACTION | ST_WRITTEN); 1808 break; 1809 } 1810 1811 cmd = (struct scsi_write_filemarks *)xs->cmd; 1812 cmd->opcode = WRITE_FILEMARKS; 1813 _lto3b(number, cmd->number); 1814 1815 error = scsi_xs_sync(xs); 1816 scsi_xs_put(xs); 1817 1818 if (error != 0) { 1819 st->media_fileno = -1; 1820 st->media_blkno = -1; 1821 st->media_eom = -1; 1822 } else if (st->media_fileno != -1) { 1823 st->media_fileno += number; 1824 st->media_eom = st->media_fileno; 1825 st->media_blkno = 0; 1826 } 1827 1828 return (error); 1829 } 1830 1831 /* 1832 * Make sure the right number of file marks is on tape if the 1833 * tape has been written. If the position argument is true, 1834 * leave the tape positioned where it was originally. 1835 * 1836 * nmarks returns the number of marks to skip (or, if position 1837 * true, which were skipped) to get back original position. 1838 */ 1839 int 1840 st_check_eod(struct st_softc *st, int position, int *nmarks, int flags) 1841 { 1842 int error; 1843 1844 switch (st->flags & (ST_WRITTEN | ST_FM_WRITTEN | ST_2FM_AT_EOD)) { 1845 default: 1846 *nmarks = 0; 1847 return 0; 1848 case ST_WRITTEN: 1849 case ST_WRITTEN | ST_FM_WRITTEN | ST_2FM_AT_EOD: 1850 *nmarks = 1; 1851 break; 1852 case ST_WRITTEN | ST_2FM_AT_EOD: 1853 *nmarks = 2; 1854 } 1855 error = st_write_filemarks(st, *nmarks, flags); 1856 if (position && !error) 1857 error = st_space(st, -*nmarks, SP_FILEMARKS, flags); 1858 return error; 1859 } 1860 1861 /* 1862 * load/unload/retension 1863 */ 1864 int 1865 st_load(struct st_softc *st, u_int type, int flags) 1866 { 1867 struct scsi_load *cmd; 1868 struct scsi_xfer *xs; 1869 int error, nmarks; 1870 1871 st->media_fileno = -1; 1872 st->media_blkno = -1; 1873 st->media_eom = -1; 1874 1875 if (type != LD_LOAD) { 1876 error = st_check_eod(st, 0, &nmarks, flags); 1877 if (error) 1878 return (error); 1879 } 1880 1881 if (st->quirks & ST_Q_IGNORE_LOADS) { 1882 if (type == LD_LOAD) { 1883 /* 1884 * If we ignore loads, at least we should try a rewind. 1885 */ 1886 return (st_rewind(st, 0, flags)); 1887 } 1888 return (0); 1889 } 1890 1891 1892 xs = scsi_xs_get(st->sc_link, flags); 1893 if (xs == NULL) 1894 return (ENOMEM); 1895 xs->cmdlen = sizeof(*cmd); 1896 xs->timeout = ST_SPC_TIME; 1897 1898 cmd = (struct scsi_load *)xs->cmd; 1899 cmd->opcode = LOAD; 1900 cmd->how = type; 1901 1902 error = scsi_xs_sync(xs); 1903 scsi_xs_put(xs); 1904 1905 return (error); 1906 } 1907 1908 /* 1909 * Rewind the device 1910 */ 1911 int 1912 st_rewind(struct st_softc *st, u_int immediate, int flags) 1913 { 1914 struct scsi_rewind *cmd; 1915 struct scsi_xfer *xs; 1916 int error, nmarks; 1917 1918 error = st_check_eod(st, 0, &nmarks, flags); 1919 if (error) 1920 return (error); 1921 st->flags &= ~ST_PER_ACTION; 1922 1923 xs = scsi_xs_get(st->sc_link, flags); 1924 if (xs == NULL) 1925 return (ENOMEM); 1926 xs->cmdlen = sizeof(*cmd); 1927 xs->timeout = immediate ? ST_CTL_TIME : ST_SPC_TIME; 1928 1929 cmd = (struct scsi_rewind *)xs->cmd; 1930 cmd->opcode = REWIND; 1931 cmd->byte2 = immediate; 1932 1933 error = scsi_xs_sync(xs); 1934 scsi_xs_put(xs); 1935 1936 if (error == 0) { 1937 st->media_fileno = 0; 1938 st->media_blkno = 0; 1939 } 1940 1941 return (error); 1942 } 1943 1944 /* 1945 * Look at the returned sense and act on the error and detirmine 1946 * The unix error number to pass back... (0 = report no error) 1947 * (-1 = continue processing) 1948 */ 1949 int 1950 st_interpret_sense(struct scsi_xfer *xs) 1951 { 1952 struct scsi_sense_data *sense = &xs->sense; 1953 struct scsi_link *link = xs->sc_link; 1954 struct scsi_space *space; 1955 struct st_softc *st = link->device_softc; 1956 u_int8_t serr = sense->error_code & SSD_ERRCODE; 1957 u_int8_t skey = sense->flags & SSD_KEY; 1958 int32_t resid, info, number; 1959 int datalen; 1960 1961 if (((link->flags & SDEV_OPEN) == 0) || 1962 (serr != SSD_ERRCODE_CURRENT && serr != SSD_ERRCODE_DEFERRED)) 1963 return (scsi_interpret_sense(xs)); 1964 1965 info = (int32_t)_4btol(sense->info); 1966 1967 switch (skey) { 1968 1969 /* 1970 * We do custom processing in st for the unit becoming ready case. 1971 * in this case we do not allow xs->retries to be decremented 1972 * only on the "Unit Becoming Ready" case. This is because tape 1973 * drives report "Unit Becoming Ready" when loading media, etc. 1974 * and can take a long time. Rather than having a massive timeout 1975 * for all operations (which would cause other problems) we allow 1976 * operations to wait (but be interruptable with Ctrl-C) forever 1977 * as long as the drive is reporting that it is becoming ready. 1978 * all other cases are handled as per the default. 1979 */ 1980 1981 case SKEY_NOT_READY: 1982 if ((xs->flags & SCSI_IGNORE_NOT_READY) != 0) 1983 return (0); 1984 switch (ASC_ASCQ(sense)) { 1985 case SENSE_NOT_READY_BECOMING_READY: 1986 SC_DEBUG(link, SDEV_DB1, ("not ready: busy (%#x)\n", 1987 sense->add_sense_code_qual)); 1988 /* don't count this as a retry */ 1989 xs->retries++; 1990 return (scsi_delay(xs, 1)); 1991 default: 1992 return (scsi_interpret_sense(xs)); 1993 } 1994 break; 1995 case SKEY_BLANK_CHECK: 1996 if (sense->error_code & SSD_ERRCODE_VALID && 1997 xs->cmd->opcode == SPACE) { 1998 switch (ASC_ASCQ(sense)) { 1999 case SENSE_END_OF_DATA_DETECTED: 2000 st->flags |= ST_EOD_DETECTED; 2001 space = (struct scsi_space *)xs->cmd; 2002 number = _3btol(space->number); 2003 st->media_fileno = number - info; 2004 st->media_eom = st->media_fileno; 2005 return (0); 2006 case SENSE_BEGINNING_OF_MEDIUM_DETECTED: 2007 /* Standard says: Position is undefined! */ 2008 st->flags |= ST_BOD_DETECTED; 2009 st->media_fileno = -1; 2010 st->media_blkno = -1; 2011 return (0); 2012 } 2013 } 2014 break; 2015 case SKEY_NO_SENSE: 2016 case SKEY_RECOVERED_ERROR: 2017 case SKEY_MEDIUM_ERROR: 2018 case SKEY_VOLUME_OVERFLOW: 2019 break; 2020 default: 2021 return (scsi_interpret_sense(xs)); 2022 } 2023 2024 /* 2025 * 'resid' can be in units of st->blksize or bytes. xs->resid and 2026 * xs->datalen are always in units of bytes. So we need a variable 2027 * to store datalen in the same units as resid and to adjust 2028 * xs->resid to be in bytes. 2029 */ 2030 if (sense->error_code & SSD_ERRCODE_VALID) { 2031 if (st->flags & ST_FIXEDBLOCKS) 2032 resid = info * st->blksize; /* XXXX overflow? */ 2033 else 2034 resid = info; 2035 } else { 2036 resid = xs->datalen; 2037 } 2038 2039 if (resid < 0 || resid > xs->datalen) 2040 xs->resid = xs->datalen; 2041 else 2042 xs->resid = resid; 2043 2044 datalen = xs->datalen; 2045 if (st->flags & ST_FIXEDBLOCKS) { 2046 resid /= st->blksize; 2047 datalen /= st->blksize; 2048 } 2049 2050 if (sense->flags & SSD_FILEMARK) { 2051 if (st->media_fileno != -1) { 2052 st->media_fileno++; 2053 if (st->media_fileno > st->media_eom) 2054 st->media_eom = st->media_fileno; 2055 st->media_blkno = 0; 2056 } 2057 if ((st->flags & ST_FIXEDBLOCKS) == 0) 2058 return 0; 2059 st->flags |= ST_AT_FILEMARK; 2060 } 2061 2062 if (sense->flags & SSD_EOM) { 2063 st->flags |= ST_EOM_PENDING; 2064 xs->resid = 0; 2065 if (st->flags & ST_FIXEDBLOCKS) 2066 return (0); 2067 } 2068 2069 if (sense->flags & SSD_ILI) { 2070 if ((st->flags & ST_FIXEDBLOCKS) == 0) { 2071 if (resid >= 0 && resid <= datalen) 2072 return (0); 2073 if ((xs->flags & SCSI_SILENT) == 0) 2074 printf( "%s: bad residual %d out of " 2075 "%d\n", st->sc_dev.dv_xname, resid, 2076 datalen); 2077 return (EIO); 2078 } 2079 2080 /* Fixed size blocks. */ 2081 if (sense->error_code & SSD_ERRCODE_VALID) 2082 if ((xs->flags & SCSI_SILENT) == 0) 2083 printf("%s: block wrong size, %d blocks " 2084 "residual\n", st->sc_dev.dv_xname, resid); 2085 st->flags |= ST_EIO_PENDING; 2086 /* 2087 * This quirk code helps the drive read the first tape block, 2088 * regardless of format. That is required for these drives to 2089 * return proper MODE SENSE information. 2090 */ 2091 if ((st->quirks & ST_Q_SENSE_HELP) && 2092 !(link->flags & SDEV_MEDIA_LOADED)) 2093 st->blksize -= 512; 2094 } 2095 2096 if ((st->flags & ST_FIXEDBLOCKS) && xs->resid == xs->datalen) { 2097 if (st->flags & ST_EIO_PENDING) 2098 return EIO; 2099 if (st->flags & ST_AT_FILEMARK) 2100 return 0; 2101 } 2102 2103 if (skey == SKEY_BLANK_CHECK) { 2104 /* 2105 * This quirk code helps the drive read the first tape block, 2106 * regardless of format. That is required for these drives to 2107 * return proper MODE SENSE information. 2108 */ 2109 if ((st->quirks & ST_Q_SENSE_HELP) && 2110 !(link->flags & SDEV_MEDIA_LOADED)) { 2111 /* still starting */ 2112 st->blksize -= 512; 2113 } else if (!(st->flags & (ST_2FM_AT_EOD | ST_BLANK_READ))) { 2114 st->flags |= ST_BLANK_READ; 2115 st->flags |= ST_EOM_PENDING; 2116 xs->resid = xs->datalen; 2117 return (0); 2118 } 2119 } 2120 2121 return (scsi_interpret_sense(xs)); 2122 } 2123 2124 /* 2125 * The quirk here is that the drive returns some value to st_mode_sense 2126 * incorrectly until the tape has actually passed by the head. 2127 * 2128 * The method is to set the drive to large fixed-block state (user-specified 2129 * density and 1024-byte blocks), then read and rewind to get it to sense the 2130 * tape. If that doesn't work, try 512-byte fixed blocks. If that doesn't 2131 * work, as a last resort, try variable- length blocks. The result will be 2132 * the ability to do an accurate st_mode_sense. 2133 * 2134 * We know we can do a rewind because we just did a load, which implies rewind. 2135 * Rewind seems preferable to space backward if we have a virgin tape. 2136 * 2137 * The rest of the code for this quirk is in ILI processing and BLANK CHECK 2138 * error processing, both part of st_interpret_sense. 2139 */ 2140 int 2141 st_touch_tape(struct st_softc *st) 2142 { 2143 char *buf = NULL; 2144 int readsize, maxblksize = 1024; 2145 int error = 0; 2146 2147 if ((error = st_mode_sense(st, 0)) != 0) 2148 goto done; 2149 buf = dma_alloc(maxblksize, PR_NOWAIT); 2150 if (!buf) { 2151 error = ENOMEM; 2152 goto done; 2153 } 2154 2155 st->blksize = 1024; 2156 do { 2157 switch (st->blksize) { 2158 case 512: 2159 case 1024: 2160 readsize = st->blksize; 2161 st->flags |= ST_FIXEDBLOCKS; 2162 break; 2163 default: 2164 readsize = 1; 2165 st->flags &= ~ST_FIXEDBLOCKS; 2166 } 2167 if ((error = st_mode_select(st, 0)) != 0) 2168 goto done; 2169 st_read(st, buf, readsize, SCSI_SILENT); /* XXX */ 2170 if ((error = st_rewind(st, 0, 0)) != 0) 2171 goto done; 2172 } while (readsize != 1 && readsize > st->blksize); 2173 done: 2174 dma_free(buf, maxblksize); 2175 return (error); 2176 } 2177