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