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