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