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