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