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