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