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