1 /* $NetBSD: st.c,v 1.122 2000/05/19 06:55:42 kleink Exp $ */ 2 3 /*- 4 * Copyright (c) 1998 The NetBSD Foundation, Inc. 5 * All rights reserved. 6 * 7 * This code is derived from software contributed to The NetBSD Foundation 8 * by Charles M. Hannum. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 3. All advertising materials mentioning features or use of this software 19 * must display the following acknowledgement: 20 * This product includes software developed by the NetBSD 21 * Foundation, Inc. and its contributors. 22 * 4. Neither the name of The NetBSD Foundation nor the names of its 23 * contributors may be used to endorse or promote products derived 24 * from this software without specific prior written permission. 25 * 26 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 27 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 28 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 29 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 30 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 31 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 32 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 33 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 34 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 36 * POSSIBILITY OF SUCH DAMAGE. 37 */ 38 39 /* 40 * Originally written by Julian Elischer (julian@tfs.com) 41 * for TRW Financial Systems for use under the MACH(2.5) operating system. 42 * 43 * TRW Financial Systems, in accordance with their agreement with Carnegie 44 * Mellon University, makes this software available to CMU to distribute 45 * or use in any manner that they see fit as long as this message is kept with 46 * the software. For this reason TFS also grants any other persons or 47 * organisations permission to use or modify this software. 48 * 49 * TFS supplies this software to be publicly redistributed 50 * on the understanding that TFS is not responsible for the correct 51 * functioning of this software in any circumstances. 52 * 53 * Ported to run under 386BSD by Julian Elischer (julian@tfs.com) Sept 1992 54 * major changes by Julian Elischer (julian@jules.dialix.oz.au) May 1993 55 * 56 * A lot of rewhacking done by mjacob (mjacob@nas.nasa.gov). 57 */ 58 59 #include "opt_scsi.h" 60 #include "rnd.h" 61 62 #include <sys/types.h> 63 #include <sys/param.h> 64 #include <sys/systm.h> 65 #include <sys/fcntl.h> 66 #include <sys/errno.h> 67 #include <sys/ioctl.h> 68 #include <sys/malloc.h> 69 #include <sys/buf.h> 70 #include <sys/proc.h> 71 #include <sys/user.h> 72 #include <sys/mtio.h> 73 #include <sys/device.h> 74 #include <sys/conf.h> 75 #include <sys/kernel.h> 76 #if NRND > 0 77 #include <sys/rnd.h> 78 #endif 79 80 #include <dev/scsipi/scsipi_all.h> 81 #include <dev/scsipi/scsi_all.h> 82 #include <dev/scsipi/scsi_tape.h> 83 #include <dev/scsipi/scsiconf.h> 84 85 /* Defines for device specific stuff */ 86 #define DEF_FIXED_BSIZE 512 87 #define ST_RETRIES 4 /* only on non IO commands */ 88 89 #define STMODE(z) ( minor(z) & 0x03) 90 #define STDSTY(z) ((minor(z) >> 2) & 0x03) 91 #define STUNIT(z) ((minor(z) >> 4) ) 92 93 #define NORMAL_MODE 0 94 #define NOREW_MODE 1 95 #define EJECT_MODE 2 96 #define CTRL_MODE 3 97 98 #define FALSE 0 99 #define TRUE 1 100 101 #define ST_IO_TIME (3 * 60 * 1000) /* 3 minutes */ 102 #define ST_CTL_TIME (30 * 1000) /* 30 seconds */ 103 #define ST_SPC_TIME (4 * 60 * 60 * 1000) /* 4 hours */ 104 105 #ifndef ST_MOUNT_DELAY 106 #define ST_MOUNT_DELAY 0 107 #endif 108 109 /* 110 * Define various devices that we know mis-behave in some way, 111 * and note how they are bad, so we can correct for them 112 */ 113 struct modes { 114 u_int quirks; /* same definitions as in quirkdata */ 115 int blksize; 116 u_int8_t density; 117 }; 118 119 struct quirkdata { 120 u_int quirks; 121 #define ST_Q_FORCE_BLKSIZE 0x0001 122 #define ST_Q_SENSE_HELP 0x0002 /* must do READ for good MODE SENSE */ 123 #define ST_Q_IGNORE_LOADS 0x0004 124 #define ST_Q_BLKSIZE 0x0008 /* variable-block media_blksize > 0 */ 125 #define ST_Q_UNIMODAL 0x0010 /* unimode drive rejects mode select */ 126 u_int page_0_size; 127 #define MAX_PAGE_0_SIZE 64 128 struct modes modes[4]; 129 }; 130 131 struct st_quirk_inquiry_pattern { 132 struct scsipi_inquiry_pattern pattern; 133 struct quirkdata quirkdata; 134 }; 135 136 struct st_quirk_inquiry_pattern st_quirk_patterns[] = { 137 {{T_SEQUENTIAL, T_REMOV, 138 " ", " ", " "}, {0, 0, { 139 {ST_Q_FORCE_BLKSIZE, 512, 0}, /* minor 0-3 */ 140 {ST_Q_FORCE_BLKSIZE, 512, QIC_24}, /* minor 4-7 */ 141 {ST_Q_FORCE_BLKSIZE, 0, HALFINCH_1600}, /* minor 8-11 */ 142 {ST_Q_FORCE_BLKSIZE, 0, HALFINCH_6250} /* minor 12-15 */ 143 }}}, 144 {{T_SEQUENTIAL, T_REMOV, 145 "TANDBERG", " TDC 3600 ", ""}, {0, 12, { 146 {0, 0, 0}, /* minor 0-3 */ 147 {ST_Q_FORCE_BLKSIZE, 0, QIC_525}, /* minor 4-7 */ 148 {0, 0, QIC_150}, /* minor 8-11 */ 149 {0, 0, QIC_120} /* minor 12-15 */ 150 }}}, 151 {{T_SEQUENTIAL, T_REMOV, 152 "TANDBERG", " TDC 3800 ", ""}, {0, 0, { 153 {ST_Q_FORCE_BLKSIZE, 512, 0}, /* minor 0-3 */ 154 {0, 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 * lacking a manual for the 4200, it's not clear what the 160 * specific density codes should be- the device is a 2.5GB 161 * capable QIC drive, those density codes aren't readily 162 * availabel. The 'default' will just have to do. 163 */ 164 {{T_SEQUENTIAL, T_REMOV, 165 "TANDBERG", " TDC 4200 ", ""}, {0, 0, { 166 {ST_Q_FORCE_BLKSIZE, 512, 0}, /* minor 0-3 */ 167 {0, 0, QIC_525}, /* minor 4-7 */ 168 {0, 0, QIC_150}, /* minor 8-11 */ 169 {0, 0, QIC_120} /* minor 12-15 */ 170 }}}, 171 /* 172 * At least -005 and -007 need this. I'll assume they all do unless I 173 * hear otherwise. - mycroft, 31MAR1994 174 */ 175 {{T_SEQUENTIAL, T_REMOV, 176 "ARCHIVE ", "VIPER 2525 25462", ""}, {0, 0, { 177 {ST_Q_SENSE_HELP, 0, 0}, /* minor 0-3 */ 178 {ST_Q_SENSE_HELP, 0, QIC_525}, /* minor 4-7 */ 179 {0, 0, QIC_150}, /* minor 8-11 */ 180 {0, 0, QIC_120} /* minor 12-15 */ 181 }}}, 182 /* 183 * One user reports that this works for his tape drive. It probably 184 * needs more work. - mycroft, 09APR1994 185 */ 186 {{T_SEQUENTIAL, T_REMOV, 187 "SANKYO ", "CP525 ", ""}, {0, 0, { 188 {ST_Q_FORCE_BLKSIZE, 512, 0}, /* minor 0-3 */ 189 {ST_Q_FORCE_BLKSIZE, 512, QIC_525}, /* minor 4-7 */ 190 {0, 0, QIC_150}, /* minor 8-11 */ 191 {0, 0, QIC_120} /* minor 12-15 */ 192 }}}, 193 {{T_SEQUENTIAL, T_REMOV, 194 "ANRITSU ", "DMT780 ", ""}, {0, 0, { 195 {ST_Q_FORCE_BLKSIZE, 512, 0}, /* minor 0-3 */ 196 {ST_Q_FORCE_BLKSIZE, 512, QIC_525}, /* minor 4-7 */ 197 {0, 0, QIC_150}, /* minor 8-11 */ 198 {0, 0, QIC_120} /* minor 12-15 */ 199 }}}, 200 {{T_SEQUENTIAL, T_REMOV, 201 "ARCHIVE ", "VIPER 150 21247", ""}, {0, 12, { 202 {ST_Q_SENSE_HELP, 0, 0}, /* minor 0-3 */ 203 {0, 0, QIC_150}, /* minor 4-7 */ 204 {0, 0, QIC_120}, /* minor 8-11 */ 205 {0, 0, QIC_24} /* minor 12-15 */ 206 }}}, 207 {{T_SEQUENTIAL, T_REMOV, 208 "ARCHIVE ", "VIPER 150 21531", ""}, {0, 12, { 209 {ST_Q_SENSE_HELP, 0, 0}, /* minor 0-3 */ 210 {0, 0, QIC_150}, /* minor 4-7 */ 211 {0, 0, QIC_120}, /* minor 8-11 */ 212 {0, 0, QIC_24} /* minor 12-15 */ 213 }}}, 214 {{T_SEQUENTIAL, T_REMOV, 215 "WANGTEK ", "5099ES SCSI", ""}, {0, 0, { 216 {ST_Q_FORCE_BLKSIZE, 512, 0}, /* minor 0-3 */ 217 {0, 0, QIC_11}, /* minor 4-7 */ 218 {0, 0, QIC_24}, /* minor 8-11 */ 219 {0, 0, QIC_24} /* minor 12-15 */ 220 }}}, 221 {{T_SEQUENTIAL, T_REMOV, 222 "WANGTEK ", "5150ES SCSI", ""}, {0, 0, { 223 {ST_Q_FORCE_BLKSIZE, 512, 0}, /* minor 0-3 */ 224 {0, 0, QIC_24}, /* minor 4-7 */ 225 {0, 0, QIC_120}, /* minor 8-11 */ 226 {0, 0, QIC_150} /* minor 12-15 */ 227 }}}, 228 {{T_SEQUENTIAL, T_REMOV, 229 "WANGTEK ", "5525ES SCSI REV7", ""}, {0, 0, { 230 {0, 0, 0}, /* minor 0-3 */ 231 {ST_Q_BLKSIZE, 0, QIC_525}, /* minor 4-7 */ 232 {0, 0, QIC_150}, /* minor 8-11 */ 233 {0, 0, QIC_120} /* minor 12-15 */ 234 }}}, 235 {{T_SEQUENTIAL, T_REMOV, 236 "WangDAT ", "Model 1300 ", ""}, {0, 0, { 237 {0, 0, 0}, /* minor 0-3 */ 238 {ST_Q_FORCE_BLKSIZE, 512, DDS}, /* minor 4-7 */ 239 {ST_Q_FORCE_BLKSIZE, 1024, DDS}, /* minor 8-11 */ 240 {ST_Q_FORCE_BLKSIZE, 0, DDS} /* minor 12-15 */ 241 }}}, 242 {{T_SEQUENTIAL, T_REMOV, 243 "EXABYTE ", "EXB-8200 ", "263H"}, {0, 5, { 244 {0, 0, 0}, /* minor 0-3 */ 245 {0, 0, 0}, /* minor 4-7 */ 246 {0, 0, 0}, /* minor 8-11 */ 247 {0, 0, 0} /* minor 12-15 */ 248 }}}, 249 {{T_SEQUENTIAL, T_REMOV, 250 "STK", "9490", ""}, 251 {ST_Q_FORCE_BLKSIZE, 0, { 252 {0, 0, 0}, /* minor 0-3 */ 253 {0, 0, 0}, /* minor 4-7 */ 254 {0, 0, 0}, /* minor 8-11 */ 255 {0, 0, 0} /* minor 12-15 */ 256 }}}, 257 {{T_SEQUENTIAL, T_REMOV, 258 "STK", "SD-3", ""}, 259 {ST_Q_FORCE_BLKSIZE, 0, { 260 {0, 0, 0}, /* minor 0-3 */ 261 {0, 0, 0}, /* minor 4-7 */ 262 {0, 0, 0}, /* minor 8-11 */ 263 {0, 0, 0} /* minor 12-15 */ 264 }}}, 265 {{T_SEQUENTIAL, T_REMOV, 266 "IBM", "03590", ""}, {ST_Q_IGNORE_LOADS, 0, { 267 {0, 0, 0}, /* minor 0-3 */ 268 {0, 0, 0}, /* minor 4-7 */ 269 {0, 0, 0}, /* minor 8-11 */ 270 {0, 0, 0} /* minor 12-15 */ 271 }}}, 272 {{T_SEQUENTIAL, T_REMOV, 273 "HP ", "T4000s ", ""}, {ST_Q_UNIMODAL, 0, { 274 {0, 0, QIC_3095}, /* minor 0-3 */ 275 {0, 0, QIC_3095}, /* minor 4-7 */ 276 {0, 0, QIC_3095}, /* minor 8-11 */ 277 {0, 0, QIC_3095}, /* minor 12-15 */ 278 }}}, 279 #if 0 280 {{T_SEQUENTIAL, T_REMOV, 281 "EXABYTE ", "EXB-8200 ", ""}, {0, 12, { 282 {0, 0, 0}, /* minor 0-3 */ 283 {0, 0, 0}, /* minor 4-7 */ 284 {0, 0, 0}, /* minor 8-11 */ 285 {0, 0, 0} /* minor 12-15 */ 286 }}}, 287 #endif 288 {{T_SEQUENTIAL, T_REMOV, 289 "TEAC ", "MT-2ST/N50 ", ""}, {ST_Q_IGNORE_LOADS, 0, { 290 {0, 0, 0}, /* minor 0-3 */ 291 {0, 0, 0}, /* minor 4-7 */ 292 {0, 0, 0}, /* minor 8-11 */ 293 {0, 0, 0} /* minor 12-15 */ 294 }}}, 295 {{T_SEQUENTIAL, T_REMOV, 296 "OnStream", "ADR50 Drive", ""}, {ST_Q_UNIMODAL, 0, { 297 {ST_Q_FORCE_BLKSIZE, 512, 0}, /* minor 0-3 */ 298 {ST_Q_FORCE_BLKSIZE, 512, 0}, /* minor 4-7 */ 299 {ST_Q_FORCE_BLKSIZE, 512, 0}, /* minor 8-11 */ 300 {ST_Q_FORCE_BLKSIZE, 512, 0}, /* minor 12-15 */ 301 }}}, 302 }; 303 304 #define NOEJECT 0 305 #define EJECT 1 306 307 struct st_softc { 308 struct device sc_dev; 309 /*--------------------present operating parameters, flags etc.---------------*/ 310 int flags; /* see below */ 311 u_int quirks; /* quirks for the open mode */ 312 int blksize; /* blksize we are using */ 313 u_int8_t density; /* present density */ 314 u_int page_0_size; /* size of page 0 data */ 315 u_int last_dsty; /* last density opened */ 316 short mt_resid; /* last (short) resid */ 317 short mt_erreg; /* last error (sense key) seen */ 318 #define mt_key mt_erreg 319 u_int8_t asc; /* last asc code seen */ 320 u_int8_t ascq; /* last asc code seen */ 321 /*--------------------device/scsi parameters---------------------------------*/ 322 struct scsipi_link *sc_link; /* our link to the adpter etc. */ 323 /*--------------------parameters reported by the device ---------------------*/ 324 int blkmin; /* min blk size */ 325 int blkmax; /* max blk size */ 326 struct quirkdata *quirkdata; /* if we have a rogue entry */ 327 /*--------------------parameters reported by the device for this media-------*/ 328 u_long numblks; /* nominal blocks capacity */ 329 int media_blksize; /* 0 if not ST_FIXEDBLOCKS */ 330 u_int8_t media_density; /* this is what it said when asked */ 331 /*--------------------quirks for the whole drive-----------------------------*/ 332 u_int drive_quirks; /* quirks of this drive */ 333 /*--------------------How we should set up when opening each minor device----*/ 334 struct modes modes[4]; /* plus more for each mode */ 335 u_int8_t modeflags[4]; /* flags for the modes */ 336 #define DENSITY_SET_BY_USER 0x01 337 #define DENSITY_SET_BY_QUIRK 0x02 338 #define BLKSIZE_SET_BY_USER 0x04 339 #define BLKSIZE_SET_BY_QUIRK 0x08 340 /*--------------------storage for sense data returned by the drive-----------*/ 341 u_char sense_data[MAX_PAGE_0_SIZE]; /* 342 * additional sense data needed 343 * for mode sense/select. 344 */ 345 struct buf_queue buf_queue; /* the queue of pending IO */ 346 /* operations */ 347 #if NRND > 0 348 rndsource_element_t rnd_source; 349 #endif 350 }; 351 352 353 int stmatch __P((struct device *, struct cfdata *, void *)); 354 void stattach __P((struct device *, struct device *, void *)); 355 void st_identify_drive __P((struct st_softc *, 356 struct scsipi_inquiry_pattern *)); 357 void st_loadquirks __P((struct st_softc *)); 358 int st_mount_tape __P((dev_t, int)); 359 void st_unmount __P((struct st_softc *, boolean)); 360 int st_decide_mode __P((struct st_softc *, boolean)); 361 void ststart __P((void *)); 362 void stdone __P((struct scsipi_xfer *)); 363 int st_read __P((struct st_softc *, char *, int, int)); 364 int st_read_block_limits __P((struct st_softc *, int)); 365 int st_mode_sense __P((struct st_softc *, int)); 366 int st_mode_select __P((struct st_softc *, int)); 367 int st_cmprss __P((struct st_softc *, int)); 368 int st_space __P((struct st_softc *, int, u_int, int)); 369 int st_write_filemarks __P((struct st_softc *, int, int)); 370 int st_check_eod __P((struct st_softc *, boolean, int *, int)); 371 int st_load __P((struct st_softc *, u_int, int)); 372 int st_rewind __P((struct st_softc *, u_int, int)); 373 int st_interpret_sense __P((struct scsipi_xfer *)); 374 int st_touch_tape __P((struct st_softc *)); 375 int st_erase __P((struct st_softc *, int full, int flags)); 376 int st_rdpos __P((struct st_softc *, int, u_int32_t *)); 377 int st_setpos __P((struct st_softc *, int, u_int32_t *)); 378 379 struct cfattach st_ca = { 380 sizeof(struct st_softc), stmatch, stattach 381 }; 382 383 extern struct cfdriver st_cd; 384 385 struct scsipi_device st_switch = { 386 st_interpret_sense, 387 ststart, 388 NULL, 389 stdone 390 }; 391 392 #define ST_INFO_VALID 0x0001 393 #define ST_BLOCK_SET 0x0002 /* block size, mode set by ioctl */ 394 #define ST_WRITTEN 0x0004 /* data has been written, EOD needed */ 395 #define ST_FIXEDBLOCKS 0x0008 396 #define ST_AT_FILEMARK 0x0010 397 #define ST_EIO_PENDING 0x0020 /* error reporting deferred until next op */ 398 #define ST_NEW_MOUNT 0x0040 /* still need to decide mode */ 399 #define ST_READONLY 0x0080 /* st_mode_sense says write protected */ 400 #define ST_FM_WRITTEN 0x0100 /* 401 * EOF file mark written -- used with 402 * ~ST_WRITTEN to indicate that multiple file 403 * marks have been written 404 */ 405 #define ST_BLANK_READ 0x0200 /* BLANK CHECK encountered already */ 406 #define ST_2FM_AT_EOD 0x0400 /* write 2 file marks at EOD */ 407 #define ST_MOUNTED 0x0800 /* Device is presently mounted */ 408 #define ST_DONTBUFFER 0x1000 /* Disable buffering/caching */ 409 #define ST_EARLYWARN 0x2000 /* Do (deferred) EOM for variable mode */ 410 #define ST_EOM_PENDING 0x4000 /* EOM reporting deferred until next op */ 411 412 #define ST_PER_ACTION (ST_AT_FILEMARK | ST_EIO_PENDING | ST_EOM_PENDING | \ 413 ST_BLANK_READ) 414 #define ST_PER_MOUNT (ST_INFO_VALID | ST_BLOCK_SET | ST_WRITTEN | \ 415 ST_FIXEDBLOCKS | ST_READONLY | ST_FM_WRITTEN | \ 416 ST_2FM_AT_EOD | ST_PER_ACTION) 417 418 #if defined(ST_ENABLE_EARLYWARN) 419 #define ST_INIT_FLAGS ST_EARLYWARN 420 #else 421 #define ST_INIT_FLAGS 0 422 #endif 423 424 struct scsipi_inquiry_pattern st_patterns[] = { 425 {T_SEQUENTIAL, T_REMOV, 426 "", "", ""}, 427 }; 428 429 int 430 stmatch(parent, match, aux) 431 struct device *parent; 432 struct cfdata *match; 433 void *aux; 434 { 435 struct scsipibus_attach_args *sa = aux; 436 int priority; 437 438 (void)scsipi_inqmatch(&sa->sa_inqbuf, 439 (caddr_t)st_patterns, sizeof(st_patterns)/sizeof(st_patterns[0]), 440 sizeof(st_patterns[0]), &priority); 441 return (priority); 442 } 443 444 /* 445 * The routine called by the low level scsi routine when it discovers 446 * A device suitable for this driver 447 */ 448 void 449 stattach(parent, self, aux) 450 struct device *parent, *self; 451 void *aux; 452 { 453 struct st_softc *st = (void *)self; 454 struct scsipibus_attach_args *sa = aux; 455 struct scsipi_link *sc_link = sa->sa_sc_link; 456 457 SC_DEBUG(sc_link, SDEV_DB2, ("stattach: ")); 458 459 /* 460 * Store information needed to contact our base driver 461 */ 462 st->sc_link = sc_link; 463 sc_link->device = &st_switch; 464 sc_link->device_softc = st; 465 sc_link->openings = 1; 466 467 /* 468 * Set initial flags 469 */ 470 471 st->flags = ST_INIT_FLAGS; 472 473 /* 474 * Check if the drive is a known criminal and take 475 * Any steps needed to bring it into line 476 */ 477 st_identify_drive(st, &sa->sa_inqbuf); 478 /* 479 * Use the subdriver to request information regarding 480 * the drive. We cannot use interrupts yet, so the 481 * request must specify this. 482 */ 483 printf("\n"); 484 printf("%s: %s", st->sc_dev.dv_xname, st->quirkdata ? "rogue, " : ""); 485 if (scsipi_test_unit_ready(sc_link, 486 XS_CTL_DISCOVERY | XS_CTL_SILENT | XS_CTL_IGNORE_MEDIA_CHANGE) || 487 st_mode_sense(st, 488 XS_CTL_DISCOVERY | XS_CTL_SILENT | XS_CTL_IGNORE_MEDIA_CHANGE)) 489 printf("drive empty\n"); 490 else { 491 printf("density code 0x%x, ", st->media_density); 492 if (st->media_blksize > 0) 493 printf("%d-byte", st->media_blksize); 494 else 495 printf("variable"); 496 printf(" blocks, write-%s\n", 497 (st->flags & ST_READONLY) ? "protected" : "enabled"); 498 } 499 500 /* 501 * Set up the buf queue for this device 502 */ 503 BUFQ_INIT(&st->buf_queue); 504 505 #if NRND > 0 506 rnd_attach_source(&st->rnd_source, st->sc_dev.dv_xname, 507 RND_TYPE_TAPE, 0); 508 #endif 509 } 510 511 /* 512 * Use the inquiry routine in 'scsi_base' to get drive info so we can 513 * Further tailor our behaviour. 514 */ 515 void 516 st_identify_drive(st, inqbuf) 517 struct st_softc *st; 518 struct scsipi_inquiry_pattern *inqbuf; 519 { 520 struct st_quirk_inquiry_pattern *finger; 521 int priority; 522 523 finger = (struct st_quirk_inquiry_pattern *)scsipi_inqmatch(inqbuf, 524 (caddr_t)st_quirk_patterns, 525 sizeof(st_quirk_patterns) / sizeof(st_quirk_patterns[0]), 526 sizeof(st_quirk_patterns[0]), &priority); 527 if (priority != 0) { 528 st->quirkdata = &finger->quirkdata; 529 st->drive_quirks = finger->quirkdata.quirks; 530 st->quirks = finger->quirkdata.quirks; /* start value */ 531 st->page_0_size = finger->quirkdata.page_0_size; 532 st_loadquirks(st); 533 } 534 } 535 536 /* 537 * initialise the subdevices to the default (QUIRK) state. 538 * this will remove any setting made by the system operator or previous 539 * operations. 540 */ 541 void 542 st_loadquirks(st) 543 struct st_softc *st; 544 { 545 int i; 546 struct modes *mode; 547 struct modes *mode2; 548 549 mode = st->quirkdata->modes; 550 mode2 = st->modes; 551 for (i = 0; i < 4; i++) { 552 bzero(mode2, sizeof(struct modes)); 553 st->modeflags[i] &= ~(BLKSIZE_SET_BY_QUIRK | 554 DENSITY_SET_BY_QUIRK | BLKSIZE_SET_BY_USER | 555 DENSITY_SET_BY_USER); 556 if ((mode->quirks | st->drive_quirks) & ST_Q_FORCE_BLKSIZE) { 557 mode2->blksize = mode->blksize; 558 st->modeflags[i] |= BLKSIZE_SET_BY_QUIRK; 559 } 560 if (mode->density) { 561 mode2->density = mode->density; 562 st->modeflags[i] |= DENSITY_SET_BY_QUIRK; 563 } 564 mode++; 565 mode2++; 566 } 567 } 568 569 /* 570 * open the device. 571 */ 572 int 573 stopen(dev, flags, mode, p) 574 dev_t dev; 575 int flags; 576 int mode; 577 struct proc *p; 578 { 579 u_int stmode, dsty; 580 int error, sflags, unit, tries, ntries; 581 struct st_softc *st; 582 struct scsipi_link *sc_link; 583 584 unit = STUNIT(dev); 585 if (unit >= st_cd.cd_ndevs) 586 return (ENXIO); 587 st = st_cd.cd_devs[unit]; 588 if (st == NULL) 589 return (ENXIO); 590 591 stmode = STMODE(dev); 592 dsty = STDSTY(dev); 593 sc_link = st->sc_link; 594 595 SC_DEBUG(sc_link, SDEV_DB1, ("open: dev=0x%x (unit %d (of %d))\n", dev, 596 unit, st_cd.cd_ndevs)); 597 598 599 /* 600 * Only allow one at a time 601 */ 602 if (sc_link->flags & SDEV_OPEN) { 603 printf("%s: already open\n", st->sc_dev.dv_xname); 604 return (EBUSY); 605 } 606 607 if ((error = scsipi_adapter_addref(sc_link)) != 0) 608 return (error); 609 610 /* 611 * clear any latched errors. 612 */ 613 st->mt_resid = 0; 614 st->mt_erreg = 0; 615 st->asc = 0; 616 st->ascq = 0; 617 618 /* 619 * Catch any unit attention errors. Be silent about this 620 * unless we're already mounted. We ignore media change 621 * if we're in control mode or not mounted yet. 622 */ 623 if ((st->flags & ST_MOUNTED) == 0 || stmode == CTRL_MODE) { 624 #ifdef SCSIDEBUG 625 sflags = XS_CTL_IGNORE_MEDIA_CHANGE; 626 #else 627 sflags = XS_CTL_SILENT|XS_CTL_IGNORE_MEDIA_CHANGE; 628 #endif 629 } else 630 sflags = 0; 631 632 /* 633 * If we're already mounted or we aren't configured for 634 * a mount delay, only try a test unit ready once. Otherwise, 635 * try up to ST_MOUNT_DELAY times with a rest interval of 636 * one second between each try. 637 */ 638 639 if ((st->flags & ST_MOUNTED) || ST_MOUNT_DELAY == 0) { 640 ntries = 1; 641 } else { 642 ntries = ST_MOUNT_DELAY; 643 } 644 645 for (error = tries = 0; tries < ntries; tries++) { 646 int slpintr, oflags; 647 648 /* 649 * If we had no error, or we're opening the control mode 650 * device, we jump out right away. 651 */ 652 653 error = scsipi_test_unit_ready(sc_link, sflags); 654 if (error == 0 || stmode == CTRL_MODE) { 655 break; 656 } 657 658 /* 659 * We had an error. 660 * 661 * If we're already mounted or we aren't configured for 662 * a mount delay, or the error isn't a NOT READY error, 663 * skip to the error exit now. 664 */ 665 if ((st->flags & ST_MOUNTED) || ST_MOUNT_DELAY == 0 || 666 (st->mt_key != SKEY_NOT_READY)) { 667 goto bad; 668 } 669 670 /* 671 * clear any latched errors. 672 */ 673 st->mt_resid = 0; 674 st->mt_erreg = 0; 675 st->asc = 0; 676 st->ascq = 0; 677 678 /* 679 * Fake that we have the device open so 680 * we block other apps from getting in. 681 */ 682 683 oflags = sc_link->flags; 684 sc_link->flags |= SDEV_OPEN; 685 686 slpintr = tsleep(&lbolt, PUSER|PCATCH, "stload", 0); 687 688 sc_link->flags = oflags; /* restore flags */ 689 690 if (slpintr) { 691 goto bad; 692 } 693 } 694 695 696 /* 697 * If the mode is 3 (e.g. minor = 3,7,11,15) then the device has 698 * been opened to set defaults and perform other, usually non-I/O 699 * related, operations. In this case, do a quick check to see 700 * whether the unit actually had a tape loaded (this will be known 701 * as to whether or not we got a NOT READY for the above 702 * unit attention). If a tape is there, go do a mount sequence. 703 */ 704 if (stmode == CTRL_MODE && st->mt_key == SKEY_NOT_READY) { 705 sc_link->flags |= SDEV_OPEN; 706 return (0); 707 } 708 709 /* 710 * If we get this far and had an error set, that means we failed 711 * to pass the 'test unit ready' test for the non-controlmode device, 712 * so we bounce the open. 713 */ 714 715 if (error) 716 return (error); 717 718 /* 719 * Else, we're now committed to saying we're open. 720 */ 721 722 sc_link->flags |= SDEV_OPEN; /* unit attn are now errors */ 723 724 /* 725 * If it's a different mode, or if the media has been 726 * invalidated, unmount the tape from the previous 727 * session but continue with open processing 728 */ 729 if (st->last_dsty != dsty || !(sc_link->flags & SDEV_MEDIA_LOADED)) 730 st_unmount(st, NOEJECT); 731 732 /* 733 * If we are not mounted, then we should start a new 734 * mount session. 735 */ 736 if (!(st->flags & ST_MOUNTED)) { 737 st_mount_tape(dev, flags); 738 st->last_dsty = dsty; 739 } 740 741 SC_DEBUG(sc_link, SDEV_DB2, ("open complete\n")); 742 return (0); 743 744 bad: 745 st_unmount(st, NOEJECT); 746 scsipi_adapter_delref(sc_link); 747 sc_link->flags &= ~SDEV_OPEN; 748 return (error); 749 } 750 751 /* 752 * close the device.. only called if we are the LAST 753 * occurence of an open device 754 */ 755 int 756 stclose(dev, flags, mode, p) 757 dev_t dev; 758 int flags; 759 int mode; 760 struct proc *p; 761 { 762 int stxx, error = 0; 763 struct st_softc *st = st_cd.cd_devs[STUNIT(dev)]; 764 765 SC_DEBUG(st->sc_link, SDEV_DB1, ("closing\n")); 766 767 /* 768 * Make sure that a tape opened in write-only mode will have 769 * file marks written on it when closed, even if not written to. 770 * 771 * This is for SUN compatibility. Actually, the Sun way of 772 * things is to: 773 * 774 * only write filemarks if there are fmks to be written and 775 * - open for write (possibly read/write) 776 * - the last operation was a write 777 * or: 778 * - opened for wronly 779 * - no data was written (including filemarks) 780 */ 781 782 stxx = st->flags & (ST_WRITTEN | ST_FM_WRITTEN); 783 if (((flags & FWRITE) && stxx == ST_WRITTEN) || 784 ((flags & O_ACCMODE) == FWRITE && stxx == 0)) { 785 int nm; 786 error = st_check_eod(st, FALSE, &nm, 0); 787 } 788 789 switch (STMODE(dev)) { 790 case NORMAL_MODE: 791 st_unmount(st, NOEJECT); 792 break; 793 case NOREW_MODE: 794 case CTRL_MODE: 795 /* 796 * Leave mounted unless media seems to have been removed. 797 * 798 * Otherwise, if we're to terminate a tape with more than one 799 * filemark [ and because we're not rewinding here ], backspace 800 * one filemark so that later appends will see an unbroken 801 * sequence of: 802 * 803 * file - FMK - file - FMK ... file - FMK FMK (EOM) 804 */ 805 if (!(st->sc_link->flags & SDEV_MEDIA_LOADED)) { 806 st_unmount(st, NOEJECT); 807 } else if (error == 0) { 808 /* 809 * ST_WRITTEN was preserved from above. 810 * 811 * All we need to know here is: 812 * 813 * Were we writing this tape and was the last 814 * operation a write? 815 * 816 * Are there supposed to be 2FM at EOD? 817 * 818 * If both statements are true, then we backspace 819 * one filemark. 820 */ 821 stxx |= (st->flags & ST_2FM_AT_EOD); 822 if ((flags & FWRITE) != 0 && 823 (stxx == (ST_2FM_AT_EOD|ST_WRITTEN))) { 824 error = st_space(st, -1, SP_FILEMARKS, 0); 825 } 826 } 827 break; 828 case EJECT_MODE: 829 st_unmount(st, EJECT); 830 break; 831 } 832 833 scsipi_wait_drain(st->sc_link); 834 835 scsipi_adapter_delref(st->sc_link); 836 st->sc_link->flags &= ~SDEV_OPEN; 837 838 return (error); 839 } 840 841 /* 842 * Start a new mount session. 843 * Copy in all the default parameters from the selected device mode. 844 * and try guess any that seem to be defaulted. 845 */ 846 int 847 st_mount_tape(dev, flags) 848 dev_t dev; 849 int flags; 850 { 851 int unit; 852 u_int dsty; 853 struct st_softc *st; 854 struct scsipi_link *sc_link; 855 int error = 0; 856 857 unit = STUNIT(dev); 858 dsty = STDSTY(dev); 859 st = st_cd.cd_devs[unit]; 860 sc_link = st->sc_link; 861 862 if (st->flags & ST_MOUNTED) 863 return (0); 864 865 SC_DEBUG(sc_link, SDEV_DB1, ("mounting\n ")); 866 st->flags |= ST_NEW_MOUNT; 867 st->quirks = st->drive_quirks | st->modes[dsty].quirks; 868 /* 869 * If the media is new, then make sure we give it a chance to 870 * to do a 'load' instruction. (We assume it is new.) 871 */ 872 if ((error = st_load(st, LD_LOAD, XS_CTL_SILENT)) != 0) 873 return (error); 874 /* 875 * Throw another dummy instruction to catch 876 * 'Unit attention' errors. Many drives give 877 * these after doing a Load instruction (with 878 * the MEDIUM MAY HAVE CHANGED asc/ascq). 879 */ 880 scsipi_test_unit_ready(sc_link, XS_CTL_SILENT); /* XXX */ 881 882 /* 883 * Some devices can't tell you much until they have been 884 * asked to look at the media. This quirk does this. 885 */ 886 if (st->quirks & ST_Q_SENSE_HELP) 887 if ((error = st_touch_tape(st)) != 0) 888 return (error); 889 /* 890 * Load the physical device parameters 891 * loads: blkmin, blkmax 892 */ 893 if ((error = st_read_block_limits(st, 0)) != 0) 894 return (error); 895 /* 896 * Load the media dependent parameters 897 * includes: media_blksize,media_density,numblks 898 * As we have a tape in, it should be reflected here. 899 * If not you may need the "quirk" above. 900 */ 901 if ((error = st_mode_sense(st, 0)) != 0) 902 return (error); 903 /* 904 * If we have gained a permanent density from somewhere, 905 * then use it in preference to the one supplied by 906 * default by the driver. 907 */ 908 if (st->modeflags[dsty] & (DENSITY_SET_BY_QUIRK | DENSITY_SET_BY_USER)) 909 st->density = st->modes[dsty].density; 910 else 911 st->density = st->media_density; 912 /* 913 * If we have gained a permanent blocksize 914 * then use it in preference to the one supplied by 915 * default by the driver. 916 */ 917 st->flags &= ~ST_FIXEDBLOCKS; 918 if (st->modeflags[dsty] & 919 (BLKSIZE_SET_BY_QUIRK | BLKSIZE_SET_BY_USER)) { 920 st->blksize = st->modes[dsty].blksize; 921 if (st->blksize) 922 st->flags |= ST_FIXEDBLOCKS; 923 } else { 924 if ((error = st_decide_mode(st, FALSE)) != 0) 925 return (error); 926 } 927 if ((error = st_mode_select(st, 0)) != 0) { 928 printf("%s: cannot set selected mode\n", st->sc_dev.dv_xname); 929 return (error); 930 } 931 scsipi_prevent(sc_link, PR_PREVENT, 932 XS_CTL_IGNORE_ILLEGAL_REQUEST | XS_CTL_IGNORE_NOT_READY); 933 st->flags &= ~ST_NEW_MOUNT; 934 st->flags |= ST_MOUNTED; 935 sc_link->flags |= SDEV_MEDIA_LOADED; /* move earlier? */ 936 937 return (0); 938 } 939 940 /* 941 * End the present mount session. 942 * Rewind, and optionally eject the tape. 943 * Reset various flags to indicate that all new 944 * operations require another mount operation 945 */ 946 void 947 st_unmount(st, eject) 948 struct st_softc *st; 949 boolean eject; 950 { 951 struct scsipi_link *sc_link = st->sc_link; 952 int nmarks; 953 954 if (!(st->flags & ST_MOUNTED)) 955 return; 956 SC_DEBUG(sc_link, SDEV_DB1, ("unmounting\n")); 957 st_check_eod(st, FALSE, &nmarks, XS_CTL_IGNORE_NOT_READY); 958 st_rewind(st, 0, XS_CTL_IGNORE_NOT_READY); 959 scsipi_prevent(sc_link, PR_ALLOW, 960 XS_CTL_IGNORE_ILLEGAL_REQUEST | XS_CTL_IGNORE_NOT_READY); 961 if (eject) 962 st_load(st, LD_UNLOAD, XS_CTL_IGNORE_NOT_READY); 963 st->flags &= ~(ST_MOUNTED | ST_NEW_MOUNT); 964 sc_link->flags &= ~SDEV_MEDIA_LOADED; 965 } 966 967 /* 968 * Given all we know about the device, media, mode, 'quirks' and 969 * initial operation, make a decision as to how we should be set 970 * to run (regarding blocking and EOD marks) 971 */ 972 int 973 st_decide_mode(st, first_read) 974 struct st_softc *st; 975 boolean first_read; 976 { 977 #ifdef SCSIDEBUG 978 struct scsipi_link *sc_link = st->sc_link; 979 #endif 980 981 SC_DEBUG(sc_link, SDEV_DB2, ("starting block mode decision\n")); 982 983 /* 984 * If the drive can only handle fixed-length blocks and only at 985 * one size, perhaps we should just do that. 986 */ 987 if (st->blkmin && (st->blkmin == st->blkmax)) { 988 st->flags |= ST_FIXEDBLOCKS; 989 st->blksize = st->blkmin; 990 SC_DEBUG(sc_link, SDEV_DB3, 991 ("blkmin == blkmax of %d\n", st->blkmin)); 992 goto done; 993 } 994 /* 995 * If the tape density mandates (or even suggests) use of fixed 996 * or variable-length blocks, comply. 997 */ 998 switch (st->density) { 999 case HALFINCH_800: 1000 case HALFINCH_1600: 1001 case HALFINCH_6250: 1002 case DDS: 1003 st->flags &= ~ST_FIXEDBLOCKS; 1004 st->blksize = 0; 1005 SC_DEBUG(sc_link, SDEV_DB3, ("density specified variable\n")); 1006 goto done; 1007 case QIC_11: 1008 case QIC_24: 1009 case QIC_120: 1010 case QIC_150: 1011 case QIC_525: 1012 case QIC_1320: 1013 case QIC_3095: 1014 case QIC_3220: 1015 st->flags |= ST_FIXEDBLOCKS; 1016 if (st->media_blksize > 0) 1017 st->blksize = st->media_blksize; 1018 else 1019 st->blksize = DEF_FIXED_BSIZE; 1020 SC_DEBUG(sc_link, SDEV_DB3, ("density specified fixed\n")); 1021 goto done; 1022 } 1023 /* 1024 * If we're about to read the tape, perhaps we should choose 1025 * fixed or variable-length blocks and block size according to 1026 * what the drive found on the tape. 1027 */ 1028 if (first_read && 1029 (!(st->quirks & ST_Q_BLKSIZE) || (st->media_blksize == 0) || 1030 (st->media_blksize == DEF_FIXED_BSIZE) || 1031 (st->media_blksize == 1024))) { 1032 if (st->media_blksize > 0) 1033 st->flags |= ST_FIXEDBLOCKS; 1034 else 1035 st->flags &= ~ST_FIXEDBLOCKS; 1036 st->blksize = st->media_blksize; 1037 SC_DEBUG(sc_link, SDEV_DB3, 1038 ("Used media_blksize of %d\n", st->media_blksize)); 1039 goto done; 1040 } 1041 /* 1042 * We're getting no hints from any direction. Choose variable- 1043 * length blocks arbitrarily. 1044 */ 1045 st->flags &= ~ST_FIXEDBLOCKS; 1046 st->blksize = 0; 1047 SC_DEBUG(sc_link, SDEV_DB3, 1048 ("Give up and default to variable mode\n")); 1049 1050 done: 1051 /* 1052 * Decide whether or not to write two file marks to signify end- 1053 * of-data. Make the decision as a function of density. If 1054 * the decision is not to use a second file mark, the SCSI BLANK 1055 * CHECK condition code will be recognized as end-of-data when 1056 * first read. 1057 * (I think this should be a by-product of fixed/variable..julian) 1058 */ 1059 switch (st->density) { 1060 /* case 8 mm: What is the SCSI density code for 8 mm, anyway? */ 1061 case QIC_11: 1062 case QIC_24: 1063 case QIC_120: 1064 case QIC_150: 1065 case QIC_525: 1066 case QIC_1320: 1067 case QIC_3095: 1068 case QIC_3220: 1069 st->flags &= ~ST_2FM_AT_EOD; 1070 break; 1071 default: 1072 st->flags |= ST_2FM_AT_EOD; 1073 } 1074 return (0); 1075 } 1076 1077 /* 1078 * Actually translate the requested transfer into 1079 * one the physical driver can understand 1080 * The transfer is described by a buf and will include 1081 * only one physical transfer. 1082 */ 1083 void 1084 ststrategy(bp) 1085 struct buf *bp; 1086 { 1087 struct st_softc *st = st_cd.cd_devs[STUNIT(bp->b_dev)]; 1088 int s; 1089 1090 SC_DEBUG(st->sc_link, SDEV_DB1, 1091 ("ststrategy %ld bytes @ blk %d\n", bp->b_bcount, bp->b_blkno)); 1092 /* 1093 * If it's a null transfer, return immediatly 1094 */ 1095 if (bp->b_bcount == 0) 1096 goto done; 1097 1098 /* If offset is negative, error */ 1099 if (bp->b_blkno < 0) { 1100 bp->b_error = EINVAL; 1101 goto bad; 1102 } 1103 1104 /* 1105 * Odd sized request on fixed drives are verboten 1106 */ 1107 if (st->flags & ST_FIXEDBLOCKS) { 1108 if (bp->b_bcount % st->blksize) { 1109 printf("%s: bad request, must be multiple of %d\n", 1110 st->sc_dev.dv_xname, st->blksize); 1111 bp->b_error = EIO; 1112 goto bad; 1113 } 1114 } 1115 /* 1116 * as are out-of-range requests on variable drives. 1117 */ 1118 else if (bp->b_bcount < st->blkmin || 1119 (st->blkmax && bp->b_bcount > st->blkmax)) { 1120 printf("%s: bad request, must be between %d and %d\n", 1121 st->sc_dev.dv_xname, st->blkmin, st->blkmax); 1122 bp->b_error = EIO; 1123 goto bad; 1124 } 1125 s = splbio(); 1126 1127 /* 1128 * Place it in the queue of activities for this tape 1129 * at the end (a bit silly because we only have on user.. 1130 * (but it could fork())) 1131 */ 1132 BUFQ_INSERT_TAIL(&st->buf_queue, bp); 1133 1134 /* 1135 * Tell the device to get going on the transfer if it's 1136 * not doing anything, otherwise just wait for completion 1137 * (All a bit silly if we're only allowing 1 open but..) 1138 */ 1139 ststart(st); 1140 1141 splx(s); 1142 return; 1143 bad: 1144 bp->b_flags |= B_ERROR; 1145 done: 1146 /* 1147 * Correctly set the buf to indicate a completed xfer 1148 */ 1149 bp->b_resid = bp->b_bcount; 1150 biodone(bp); 1151 return; 1152 } 1153 1154 /* 1155 * ststart looks to see if there is a buf waiting for the device 1156 * and that the device is not already busy. If both are true, 1157 * It dequeues the buf and creates a scsi command to perform the 1158 * transfer required. The transfer request will call scsipi_done 1159 * on completion, which will in turn call this routine again 1160 * so that the next queued transfer is performed. 1161 * The bufs are queued by the strategy routine (ststrategy) 1162 * 1163 * This routine is also called after other non-queued requests 1164 * have been made of the scsi driver, to ensure that the queue 1165 * continues to be drained. 1166 * ststart() is called at splbio 1167 */ 1168 void 1169 ststart(v) 1170 void *v; 1171 { 1172 struct st_softc *st = v; 1173 struct scsipi_link *sc_link = st->sc_link; 1174 struct buf *bp; 1175 struct scsi_rw_tape cmd; 1176 int flags, error; 1177 1178 SC_DEBUG(sc_link, SDEV_DB2, ("ststart ")); 1179 /* 1180 * See if there is a buf to do and we are not already 1181 * doing one 1182 */ 1183 while (sc_link->active < sc_link->openings) { 1184 /* if a special awaits, let it proceed first */ 1185 if (sc_link->flags & SDEV_WAITING) { 1186 sc_link->flags &= ~SDEV_WAITING; 1187 wakeup((caddr_t)sc_link); 1188 return; 1189 } 1190 1191 if ((bp = BUFQ_FIRST(&st->buf_queue)) == NULL) 1192 return; 1193 BUFQ_REMOVE(&st->buf_queue, bp); 1194 1195 /* 1196 * If the device has been unmounted by the user 1197 * then throw away all requests until done. 1198 */ 1199 if (!(st->flags & ST_MOUNTED) || 1200 !(sc_link->flags & SDEV_MEDIA_LOADED)) { 1201 /* make sure that one implies the other.. */ 1202 sc_link->flags &= ~SDEV_MEDIA_LOADED; 1203 bp->b_flags |= B_ERROR; 1204 bp->b_error = EIO; 1205 bp->b_resid = bp->b_bcount; 1206 biodone(bp); 1207 continue; 1208 } 1209 /* 1210 * only FIXEDBLOCK devices have pending I/O or space operations. 1211 */ 1212 if (st->flags & ST_FIXEDBLOCKS) { 1213 /* 1214 * If we are at a filemark but have not reported it yet 1215 * then we should report it now 1216 */ 1217 if (st->flags & ST_AT_FILEMARK) { 1218 if ((bp->b_flags & B_READ) == B_WRITE) { 1219 /* 1220 * Handling of ST_AT_FILEMARK in 1221 * st_space will fill in the right file 1222 * mark count. 1223 * Back up over filemark 1224 */ 1225 if (st_space(st, 0, SP_FILEMARKS, 0)) { 1226 bp->b_flags |= B_ERROR; 1227 bp->b_error = EIO; 1228 biodone(bp); 1229 continue; 1230 } 1231 } else { 1232 bp->b_resid = bp->b_bcount; 1233 bp->b_error = 0; 1234 bp->b_flags &= ~B_ERROR; 1235 st->flags &= ~ST_AT_FILEMARK; 1236 biodone(bp); 1237 continue; /* seek more work */ 1238 } 1239 } 1240 } 1241 /* 1242 * If we are at EOM but have not reported it 1243 * yet then we should report it now. 1244 */ 1245 if (st->flags & (ST_EOM_PENDING|ST_EIO_PENDING)) { 1246 bp->b_resid = bp->b_bcount; 1247 if (st->flags & ST_EIO_PENDING) { 1248 bp->b_error = EIO; 1249 bp->b_flags |= B_ERROR; 1250 } 1251 st->flags &= ~(ST_EOM_PENDING|ST_EIO_PENDING); 1252 biodone(bp); 1253 continue; /* seek more work */ 1254 } 1255 1256 /* 1257 * Fill out the scsi command 1258 */ 1259 bzero(&cmd, sizeof(cmd)); 1260 if ((bp->b_flags & B_READ) == B_WRITE) { 1261 cmd.opcode = WRITE; 1262 st->flags &= ~ST_FM_WRITTEN; 1263 flags = XS_CTL_DATA_OUT; 1264 } else { 1265 cmd.opcode = READ; 1266 flags = XS_CTL_DATA_IN; 1267 } 1268 1269 /* 1270 * Handle "fixed-block-mode" tape drives by using the 1271 * block count instead of the length. 1272 */ 1273 if (st->flags & ST_FIXEDBLOCKS) { 1274 cmd.byte2 |= SRW_FIXED; 1275 _lto3b(bp->b_bcount / st->blksize, cmd.len); 1276 } else 1277 _lto3b(bp->b_bcount, cmd.len); 1278 1279 /* 1280 * go ask the adapter to do all this for us 1281 * XXX Really need NOSLEEP? 1282 */ 1283 error = scsipi_command(sc_link, 1284 (struct scsipi_generic *)&cmd, sizeof(cmd), 1285 (u_char *)bp->b_data, bp->b_bcount, 1286 0, ST_IO_TIME, bp, flags | XS_CTL_NOSLEEP | XS_CTL_ASYNC); 1287 if (error) { 1288 printf("%s: not queued, error %d\n", 1289 st->sc_dev.dv_xname, error); 1290 } 1291 } /* go back and see if we can cram more work in.. */ 1292 } 1293 1294 void 1295 stdone(xs) 1296 struct scsipi_xfer *xs; 1297 { 1298 1299 if (xs->bp != NULL) { 1300 struct st_softc *st = xs->sc_link->device_softc; 1301 if ((xs->bp->b_flags & B_READ) == B_WRITE) { 1302 st->flags |= ST_WRITTEN; 1303 } else { 1304 st->flags &= ~ST_WRITTEN; 1305 } 1306 #if NRND > 0 1307 rnd_add_uint32(&st->rnd_source, xs->bp->b_blkno); 1308 #endif 1309 } 1310 } 1311 1312 int 1313 stread(dev, uio, iomode) 1314 dev_t dev; 1315 struct uio *uio; 1316 int iomode; 1317 { 1318 struct st_softc *st = st_cd.cd_devs[STUNIT(dev)]; 1319 1320 return (physio(ststrategy, NULL, dev, B_READ, 1321 st->sc_link->adapter->scsipi_minphys, uio)); 1322 } 1323 1324 int 1325 stwrite(dev, uio, iomode) 1326 dev_t dev; 1327 struct uio *uio; 1328 int iomode; 1329 { 1330 struct st_softc *st = st_cd.cd_devs[STUNIT(dev)]; 1331 1332 return (physio(ststrategy, NULL, dev, B_WRITE, 1333 st->sc_link->adapter->scsipi_minphys, uio)); 1334 } 1335 1336 /* 1337 * Perform special action on behalf of the user; 1338 * knows about the internals of this device 1339 */ 1340 int 1341 stioctl(dev, cmd, arg, flag, p) 1342 dev_t dev; 1343 u_long cmd; 1344 caddr_t arg; 1345 int flag; 1346 struct proc *p; 1347 { 1348 int error = 0; 1349 int unit; 1350 int number, nmarks, dsty; 1351 int flags; 1352 struct st_softc *st; 1353 int hold_blksize; 1354 u_int8_t hold_density; 1355 struct mtop *mt = (struct mtop *) arg; 1356 1357 /* 1358 * Find the device that the user is talking about 1359 */ 1360 flags = 0; /* give error messages, act on errors etc. */ 1361 unit = STUNIT(dev); 1362 dsty = STDSTY(dev); 1363 st = st_cd.cd_devs[unit]; 1364 hold_blksize = st->blksize; 1365 hold_density = st->density; 1366 1367 switch ((u_int) cmd) { 1368 1369 case MTIOCGET: { 1370 struct mtget *g = (struct mtget *) arg; 1371 /* 1372 * (to get the current state of READONLY) 1373 */ 1374 error = st_mode_sense(st, XS_CTL_SILENT); 1375 if (error) 1376 break; 1377 SC_DEBUG(st->sc_link, SDEV_DB1, ("[ioctl: get status]\n")); 1378 bzero(g, sizeof(struct mtget)); 1379 g->mt_type = 0x7; /* Ultrix compat *//*? */ 1380 g->mt_blksiz = st->blksize; 1381 g->mt_density = st->density; 1382 g->mt_mblksiz[0] = st->modes[0].blksize; 1383 g->mt_mblksiz[1] = st->modes[1].blksize; 1384 g->mt_mblksiz[2] = st->modes[2].blksize; 1385 g->mt_mblksiz[3] = st->modes[3].blksize; 1386 g->mt_mdensity[0] = st->modes[0].density; 1387 g->mt_mdensity[1] = st->modes[1].density; 1388 g->mt_mdensity[2] = st->modes[2].density; 1389 g->mt_mdensity[3] = st->modes[3].density; 1390 if (st->flags & ST_READONLY) 1391 g->mt_dsreg |= MT_DS_RDONLY; 1392 if (st->flags & ST_MOUNTED) 1393 g->mt_dsreg |= MT_DS_MOUNTED; 1394 g->mt_resid = st->mt_resid; 1395 g->mt_erreg = st->mt_erreg; 1396 /* 1397 * clear latched errors. 1398 */ 1399 st->mt_resid = 0; 1400 st->mt_erreg = 0; 1401 st->asc = 0; 1402 st->ascq = 0; 1403 break; 1404 } 1405 case MTIOCTOP: { 1406 1407 SC_DEBUG(st->sc_link, SDEV_DB1, 1408 ("[ioctl: op=0x%x count=0x%x]\n", mt->mt_op, 1409 mt->mt_count)); 1410 1411 /* compat: in U*x it is a short */ 1412 number = mt->mt_count; 1413 switch ((short) (mt->mt_op)) { 1414 case MTWEOF: /* write an end-of-file record */ 1415 error = st_write_filemarks(st, number, flags); 1416 break; 1417 case MTBSF: /* backward space file */ 1418 number = -number; 1419 case MTFSF: /* forward space file */ 1420 error = st_check_eod(st, FALSE, &nmarks, flags); 1421 if (!error) 1422 error = st_space(st, number - nmarks, 1423 SP_FILEMARKS, flags); 1424 break; 1425 case MTBSR: /* backward space record */ 1426 number = -number; 1427 case MTFSR: /* forward space record */ 1428 error = st_check_eod(st, TRUE, &nmarks, flags); 1429 if (!error) 1430 error = st_space(st, number, SP_BLKS, flags); 1431 break; 1432 case MTREW: /* rewind */ 1433 error = st_rewind(st, 0, flags); 1434 break; 1435 case MTOFFL: /* rewind and put the drive offline */ 1436 st_unmount(st, EJECT); 1437 break; 1438 case MTNOP: /* no operation, sets status only */ 1439 break; 1440 case MTRETEN: /* retension the tape */ 1441 error = st_load(st, LD_RETENSION, flags); 1442 if (!error) 1443 error = st_load(st, LD_LOAD, flags); 1444 break; 1445 case MTEOM: /* forward space to end of media */ 1446 error = st_check_eod(st, FALSE, &nmarks, flags); 1447 if (!error) 1448 error = st_space(st, 1, SP_EOM, flags); 1449 break; 1450 case MTCACHE: /* enable controller cache */ 1451 st->flags &= ~ST_DONTBUFFER; 1452 goto try_new_value; 1453 case MTNOCACHE: /* disable controller cache */ 1454 st->flags |= ST_DONTBUFFER; 1455 goto try_new_value; 1456 case MTERASE: /* erase volume */ 1457 error = st_erase(st, number, flags); 1458 break; 1459 case MTSETBSIZ: /* Set block size for device */ 1460 #ifdef NOTYET 1461 if (!(st->flags & ST_NEW_MOUNT)) { 1462 uprintf("re-mount tape before changing blocksize"); 1463 error = EINVAL; 1464 break; 1465 } 1466 #endif 1467 if (number == 0) 1468 st->flags &= ~ST_FIXEDBLOCKS; 1469 else { 1470 if ((st->blkmin || st->blkmax) && 1471 (number < st->blkmin || 1472 number > st->blkmax)) { 1473 error = EINVAL; 1474 break; 1475 } 1476 st->flags |= ST_FIXEDBLOCKS; 1477 } 1478 st->blksize = number; 1479 st->flags |= ST_BLOCK_SET; /*XXX */ 1480 goto try_new_value; 1481 1482 case MTSETDNSTY: /* Set density for device and mode */ 1483 /* 1484 * Any number >= 0 and <= 0xff is legal. Numbers 1485 * above 0x80 are 'vendor unique'. 1486 */ 1487 if (number < 0 || number > 255) { 1488 error = EINVAL; 1489 break; 1490 } else 1491 st->density = number; 1492 goto try_new_value; 1493 1494 case MTCMPRESS: 1495 error = st_cmprss(st, number); 1496 break; 1497 1498 case MTEWARN: 1499 if (number) 1500 st->flags |= ST_EARLYWARN; 1501 else 1502 st->flags &= ~ST_EARLYWARN; 1503 break; 1504 1505 default: 1506 error = EINVAL; 1507 } 1508 break; 1509 } 1510 case MTIOCIEOT: 1511 case MTIOCEEOT: 1512 break; 1513 1514 case MTIOCRDSPOS: 1515 error = st_rdpos(st, 0, (u_int32_t *) arg); 1516 break; 1517 1518 case MTIOCRDHPOS: 1519 error = st_rdpos(st, 1, (u_int32_t *) arg); 1520 break; 1521 1522 case MTIOCSLOCATE: 1523 error = st_setpos(st, 0, (u_int32_t *) arg); 1524 break; 1525 1526 case MTIOCHLOCATE: 1527 error = st_setpos(st, 1, (u_int32_t *) arg); 1528 break; 1529 1530 1531 default: 1532 error = scsipi_do_ioctl(st->sc_link, dev, cmd, arg, 1533 flag, p); 1534 break; 1535 } 1536 return (error); 1537 /*-----------------------------*/ 1538 try_new_value: 1539 /* 1540 * Check that the mode being asked for is aggreeable to the 1541 * drive. If not, put it back the way it was. 1542 */ 1543 if ((error = st_mode_select(st, 0)) != 0) {/* put it back as it was */ 1544 printf("%s: cannot set selected mode\n", st->sc_dev.dv_xname); 1545 st->density = hold_density; 1546 st->blksize = hold_blksize; 1547 if (st->blksize) 1548 st->flags |= ST_FIXEDBLOCKS; 1549 else 1550 st->flags &= ~ST_FIXEDBLOCKS; 1551 return (error); 1552 } 1553 /* 1554 * As the drive liked it, if we are setting a new default, 1555 * set it into the structures as such. 1556 * 1557 * The means for deciding this are not finalised yet- but 1558 * if the device was opened in Control Mode, the values 1559 * are persistent now across mounts. 1560 */ 1561 if (STMODE(dev) == CTRL_MODE) { 1562 switch ((short) (mt->mt_op)) { 1563 case MTSETBSIZ: 1564 st->modes[dsty].blksize = st->blksize; 1565 st->modeflags[dsty] |= BLKSIZE_SET_BY_USER; 1566 break; 1567 case MTSETDNSTY: 1568 st->modes[dsty].density = st->density; 1569 st->modeflags[dsty] |= DENSITY_SET_BY_USER; 1570 break; 1571 } 1572 } 1573 return (0); 1574 } 1575 1576 /* 1577 * Do a synchronous read. 1578 */ 1579 int 1580 st_read(st, buf, size, flags) 1581 struct st_softc *st; 1582 int size; 1583 int flags; 1584 char *buf; 1585 { 1586 struct scsi_rw_tape cmd; 1587 1588 /* 1589 * If it's a null transfer, return immediatly 1590 */ 1591 if (size == 0) 1592 return (0); 1593 bzero(&cmd, sizeof(cmd)); 1594 cmd.opcode = READ; 1595 if (st->flags & ST_FIXEDBLOCKS) { 1596 cmd.byte2 |= SRW_FIXED; 1597 _lto3b(size / (st->blksize ? st->blksize : DEF_FIXED_BSIZE), 1598 cmd.len); 1599 } else 1600 _lto3b(size, cmd.len); 1601 return (scsipi_command(st->sc_link, 1602 (struct scsipi_generic *)&cmd, sizeof(cmd), 1603 (u_char *)buf, size, 0, ST_IO_TIME, NULL, flags | XS_CTL_DATA_IN)); 1604 } 1605 1606 /* 1607 * Ask the drive what it's min and max blk sizes are. 1608 */ 1609 int 1610 st_read_block_limits(st, flags) 1611 struct st_softc *st; 1612 int flags; 1613 { 1614 struct scsi_block_limits cmd; 1615 struct scsi_block_limits_data block_limits; 1616 struct scsipi_link *sc_link = st->sc_link; 1617 int error; 1618 1619 /* 1620 * First check if we have it all loaded 1621 */ 1622 if ((sc_link->flags & SDEV_MEDIA_LOADED)) 1623 return (0); 1624 1625 /* 1626 * do a 'Read Block Limits' 1627 */ 1628 bzero(&cmd, sizeof(cmd)); 1629 cmd.opcode = READ_BLOCK_LIMITS; 1630 1631 /* 1632 * do the command, update the global values 1633 */ 1634 error = scsipi_command(sc_link, (struct scsipi_generic *)&cmd, 1635 sizeof(cmd), (u_char *)&block_limits, sizeof(block_limits), 1636 ST_RETRIES, ST_CTL_TIME, NULL, flags | XS_CTL_DATA_IN); 1637 if (error) 1638 return (error); 1639 1640 st->blkmin = _2btol(block_limits.min_length); 1641 st->blkmax = _3btol(block_limits.max_length); 1642 1643 SC_DEBUG(sc_link, SDEV_DB3, 1644 ("(%d <= blksize <= %d)\n", st->blkmin, st->blkmax)); 1645 return (0); 1646 } 1647 1648 /* 1649 * Get the scsi driver to send a full inquiry to the 1650 * device and use the results to fill out the global 1651 * parameter structure. 1652 * 1653 * called from: 1654 * attach 1655 * open 1656 * ioctl (to reset original blksize) 1657 */ 1658 int 1659 st_mode_sense(st, flags) 1660 struct st_softc *st; 1661 int flags; 1662 { 1663 u_int scsipi_sense_len; 1664 int error; 1665 struct scsi_mode_sense cmd; 1666 struct scsipi_sense { 1667 struct scsi_mode_header header; 1668 struct scsi_blk_desc blk_desc; 1669 u_char sense_data[MAX_PAGE_0_SIZE]; 1670 } scsipi_sense; 1671 struct scsipi_link *sc_link = st->sc_link; 1672 1673 scsipi_sense_len = 12 + st->page_0_size; 1674 1675 /* 1676 * Set up a mode sense 1677 */ 1678 bzero(&cmd, sizeof(cmd)); 1679 cmd.opcode = SCSI_MODE_SENSE; 1680 cmd.length = scsipi_sense_len; 1681 1682 /* 1683 * do the command, but we don't need the results 1684 * just print them for our interest's sake, if asked, 1685 * or if we need it as a template for the mode select 1686 * store it away. 1687 */ 1688 error = scsipi_command(sc_link, (struct scsipi_generic *)&cmd, 1689 sizeof(cmd), (u_char *)&scsipi_sense, scsipi_sense_len, 1690 ST_RETRIES, ST_CTL_TIME, NULL, flags | XS_CTL_DATA_IN); 1691 if (error) 1692 return (error); 1693 1694 st->numblks = _3btol(scsipi_sense.blk_desc.nblocks); 1695 st->media_blksize = _3btol(scsipi_sense.blk_desc.blklen); 1696 st->media_density = scsipi_sense.blk_desc.density; 1697 if (scsipi_sense.header.dev_spec & SMH_DSP_WRITE_PROT) 1698 st->flags |= ST_READONLY; 1699 else 1700 st->flags &= ~ST_READONLY; 1701 SC_DEBUG(sc_link, SDEV_DB3, 1702 ("density code 0x%x, %d-byte blocks, write-%s, ", 1703 st->media_density, st->media_blksize, 1704 st->flags & ST_READONLY ? "protected" : "enabled")); 1705 SC_DEBUG(sc_link, SDEV_DB3, 1706 ("%sbuffered\n", 1707 scsipi_sense.header.dev_spec & SMH_DSP_BUFF_MODE ? "" : "un")); 1708 if (st->page_0_size) 1709 bcopy(scsipi_sense.sense_data, st->sense_data, 1710 st->page_0_size); 1711 sc_link->flags |= SDEV_MEDIA_LOADED; 1712 return (0); 1713 } 1714 1715 /* 1716 * Send a filled out parameter structure to the drive to 1717 * set it into the desire modes etc. 1718 */ 1719 int 1720 st_mode_select(st, flags) 1721 struct st_softc *st; 1722 int flags; 1723 { 1724 u_int scsi_select_len; 1725 struct scsi_mode_select cmd; 1726 struct scsi_select { 1727 struct scsi_mode_header header; 1728 struct scsi_blk_desc blk_desc; 1729 u_char sense_data[MAX_PAGE_0_SIZE]; 1730 } scsi_select; 1731 struct scsipi_link *sc_link = st->sc_link; 1732 1733 scsi_select_len = 12 + st->page_0_size; 1734 1735 /* 1736 * This quirk deals with drives that have only one valid mode 1737 * and think this gives them license to reject all mode selects, 1738 * even if the selected mode is the one that is supported. 1739 */ 1740 if (st->quirks & ST_Q_UNIMODAL) { 1741 SC_DEBUG(sc_link, SDEV_DB3, 1742 ("not setting density 0x%x blksize 0x%x\n", 1743 st->density, st->blksize)); 1744 return (0); 1745 } 1746 1747 /* 1748 * Set up for a mode select 1749 */ 1750 bzero(&cmd, sizeof(cmd)); 1751 cmd.opcode = SCSI_MODE_SELECT; 1752 cmd.length = scsi_select_len; 1753 1754 bzero(&scsi_select, scsi_select_len); 1755 scsi_select.header.blk_desc_len = sizeof(struct scsi_blk_desc); 1756 scsi_select.header.dev_spec &= ~SMH_DSP_BUFF_MODE; 1757 scsi_select.blk_desc.density = st->density; 1758 if (st->flags & ST_DONTBUFFER) 1759 scsi_select.header.dev_spec |= SMH_DSP_BUFF_MODE_OFF; 1760 else 1761 scsi_select.header.dev_spec |= SMH_DSP_BUFF_MODE_ON; 1762 if (st->flags & ST_FIXEDBLOCKS) 1763 _lto3b(st->blksize, scsi_select.blk_desc.blklen); 1764 if (st->page_0_size) 1765 bcopy(st->sense_data, scsi_select.sense_data, st->page_0_size); 1766 1767 /* 1768 * do the command 1769 */ 1770 return (scsipi_command(sc_link, (struct scsipi_generic *)&cmd, 1771 sizeof(cmd), (u_char *)&scsi_select, scsi_select_len, 1772 ST_RETRIES, ST_CTL_TIME, NULL, flags | XS_CTL_DATA_OUT)); 1773 } 1774 1775 int 1776 st_cmprss(st, onoff) 1777 struct st_softc *st; 1778 int onoff; 1779 { 1780 u_int scsi_dlen; 1781 struct scsi_mode_select mcmd; 1782 struct scsi_mode_sense scmd; 1783 struct scsi_select { 1784 struct scsi_mode_header header; 1785 struct scsi_blk_desc blk_desc; 1786 u_char pdata[max(sizeof(struct scsi_tape_dev_conf_page), 1787 sizeof(struct scsi_tape_dev_compression_page))]; 1788 } scsi_pdata; 1789 struct scsi_tape_dev_conf_page *ptr; 1790 struct scsi_tape_dev_compression_page *cptr; 1791 struct scsipi_link *sc_link = st->sc_link; 1792 int error, ison, flags; 1793 1794 scsi_dlen = sizeof(scsi_pdata); 1795 bzero(&scsi_pdata, scsi_dlen); 1796 1797 /* 1798 * Set up for a mode sense. 1799 * Do DATA COMPRESSION page first. 1800 */ 1801 bzero(&scmd, sizeof(scmd)); 1802 scmd.opcode = SCSI_MODE_SENSE; 1803 scmd.page = SMS_PAGE_CTRL_CURRENT | 0xf; 1804 scmd.length = scsi_dlen; 1805 1806 flags = XS_CTL_SILENT; 1807 1808 /* 1809 * Do the MODE SENSE command... 1810 */ 1811 again: 1812 bzero(&scsi_pdata, scsi_dlen); 1813 error = scsipi_command(sc_link, 1814 (struct scsipi_generic *)&scmd, sizeof(scmd), 1815 (u_char *)&scsi_pdata, scsi_dlen, 1816 ST_RETRIES, ST_CTL_TIME, NULL, flags | XS_CTL_DATA_IN); 1817 1818 if (error) { 1819 if (scmd.byte2 != SMS_DBD) { 1820 scmd.byte2 = SMS_DBD; 1821 goto again; 1822 } 1823 /* 1824 * Try a different page? 1825 */ 1826 if (scmd.page == (SMS_PAGE_CTRL_CURRENT | 0xf)) { 1827 scmd.page = SMS_PAGE_CTRL_CURRENT | 0x10; 1828 scmd.byte2 = 0; 1829 goto again; 1830 } 1831 return (error); 1832 } 1833 1834 if (scsi_pdata.header.blk_desc_len) 1835 ptr = (struct scsi_tape_dev_conf_page *) scsi_pdata.pdata; 1836 else 1837 ptr = (struct scsi_tape_dev_conf_page *) &scsi_pdata.blk_desc; 1838 1839 if ((scmd.page & SMS_PAGE_CODE) == 0xf) { 1840 cptr = (struct scsi_tape_dev_compression_page *) ptr; 1841 ison = (cptr->dce_dcc & DCP_DCE) != 0; 1842 if (onoff) 1843 cptr->dce_dcc |= DCP_DCE; 1844 else 1845 cptr->dce_dcc &= ~DCP_DCE; 1846 cptr->pagecode &= ~0x80; 1847 } else { 1848 ison = (ptr->sel_comp_alg != 0); 1849 if (onoff) 1850 ptr->sel_comp_alg = 1; 1851 else 1852 ptr->sel_comp_alg = 0; 1853 ptr->pagecode &= ~0x80; 1854 ptr->byte2 = 0; 1855 ptr->active_partition = 0; 1856 ptr->wb_full_ratio = 0; 1857 ptr->rb_empty_ratio = 0; 1858 ptr->byte8 &= ~0x30; 1859 ptr->gap_size = 0; 1860 ptr->byte10 &= ~0xe7; 1861 ptr->ew_bufsize[0] = 0; 1862 ptr->ew_bufsize[1] = 0; 1863 ptr->ew_bufsize[2] = 0; 1864 ptr->reserved = 0; 1865 } 1866 onoff = onoff ? 1 : 0; 1867 /* 1868 * There might be a virtue in actually doing the MODE SELECTS, 1869 * but let's not clog the bus over it. 1870 */ 1871 if (onoff == ison) 1872 return (0); 1873 1874 /* 1875 * Set up for a mode select 1876 */ 1877 1878 scsi_pdata.header.data_length = 0; 1879 scsi_pdata.header.medium_type = 0; 1880 if ((st->flags & ST_DONTBUFFER) == 0) 1881 scsi_pdata.header.dev_spec = SMH_DSP_BUFF_MODE_ON; 1882 else 1883 scsi_pdata.header.dev_spec = 0; 1884 1885 bzero(&mcmd, sizeof(mcmd)); 1886 mcmd.opcode = SCSI_MODE_SELECT; 1887 mcmd.byte2 = SMS_PF; 1888 mcmd.length = scsi_dlen; 1889 if (scsi_pdata.header.blk_desc_len) { 1890 scsi_pdata.blk_desc.density = 0; 1891 scsi_pdata.blk_desc.nblocks[0] = 0; 1892 scsi_pdata.blk_desc.nblocks[1] = 0; 1893 scsi_pdata.blk_desc.nblocks[2] = 0; 1894 } 1895 1896 /* 1897 * Do the command 1898 */ 1899 error = scsipi_command(sc_link, 1900 (struct scsipi_generic *)&mcmd, sizeof(mcmd), 1901 (u_char *)&scsi_pdata, scsi_dlen, 1902 ST_RETRIES, ST_CTL_TIME, NULL, flags | XS_CTL_DATA_OUT); 1903 1904 if (error && (scmd.page & SMS_PAGE_CODE) == 0xf) { 1905 /* 1906 * Try DEVICE CONFIGURATION page. 1907 */ 1908 scmd.page = SMS_PAGE_CTRL_CURRENT | 0x10; 1909 goto again; 1910 } 1911 return (error); 1912 } 1913 /* 1914 * issue an erase command 1915 */ 1916 int 1917 st_erase(st, full, flags) 1918 struct st_softc *st; 1919 int full, flags; 1920 { 1921 int tmo; 1922 struct scsi_erase cmd; 1923 1924 /* 1925 * Full erase means set LONG bit in erase command, which asks 1926 * the drive to erase the entire unit. Without this bit, we're 1927 * asking the drive to write an erase gap. 1928 */ 1929 bzero(&cmd, sizeof(cmd)); 1930 cmd.opcode = ERASE; 1931 if (full) { 1932 cmd.byte2 = SE_IMMED|SE_LONG; 1933 tmo = ST_SPC_TIME; 1934 } else { 1935 tmo = ST_IO_TIME; 1936 cmd.byte2 = SE_IMMED; 1937 } 1938 1939 /* 1940 * XXX We always do this asynchronously, for now. How long should 1941 * we wait if we want to (eventually) to it synchronously? 1942 */ 1943 return (scsipi_command(st->sc_link, 1944 (struct scsipi_generic *)&cmd, sizeof(cmd), 1945 0, 0, ST_RETRIES, tmo, NULL, flags)); 1946 } 1947 1948 /* 1949 * skip N blocks/filemarks/seq filemarks/eom 1950 */ 1951 int 1952 st_space(st, number, what, flags) 1953 struct st_softc *st; 1954 u_int what; 1955 int flags; 1956 int number; 1957 { 1958 struct scsi_space cmd; 1959 int error; 1960 1961 switch (what) { 1962 case SP_BLKS: 1963 if (st->flags & ST_PER_ACTION) { 1964 if (number > 0) { 1965 st->flags &= ~ST_PER_ACTION; 1966 return (EIO); 1967 } else if (number < 0) { 1968 if (st->flags & ST_AT_FILEMARK) { 1969 /* 1970 * Handling of ST_AT_FILEMARK 1971 * in st_space will fill in the 1972 * right file mark count. 1973 */ 1974 error = st_space(st, 0, SP_FILEMARKS, 1975 flags); 1976 if (error) 1977 return (error); 1978 } 1979 if (st->flags & ST_BLANK_READ) { 1980 st->flags &= ~ST_BLANK_READ; 1981 return (EIO); 1982 } 1983 st->flags &= ~(ST_EIO_PENDING|ST_EOM_PENDING); 1984 } 1985 } 1986 break; 1987 case SP_FILEMARKS: 1988 if (st->flags & ST_EIO_PENDING) { 1989 if (number > 0) { 1990 /* pretend we just discovered the error */ 1991 st->flags &= ~ST_EIO_PENDING; 1992 return (EIO); 1993 } else if (number < 0) { 1994 /* back away from the error */ 1995 st->flags &= ~ST_EIO_PENDING; 1996 } 1997 } 1998 if (st->flags & ST_AT_FILEMARK) { 1999 st->flags &= ~ST_AT_FILEMARK; 2000 number--; 2001 } 2002 if ((st->flags & ST_BLANK_READ) && (number < 0)) { 2003 /* back away from unwritten tape */ 2004 st->flags &= ~ST_BLANK_READ; 2005 number++; /* XXX dubious */ 2006 } 2007 break; 2008 case SP_EOM: 2009 if (st->flags & ST_EOM_PENDING) { 2010 /* we're already there */ 2011 st->flags &= ~ST_EOM_PENDING; 2012 return (0); 2013 } 2014 if (st->flags & ST_EIO_PENDING) { 2015 /* pretend we just discovered the error */ 2016 st->flags &= ~ST_EIO_PENDING; 2017 return (EIO); 2018 } 2019 if (st->flags & ST_AT_FILEMARK) 2020 st->flags &= ~ST_AT_FILEMARK; 2021 break; 2022 } 2023 if (number == 0) 2024 return (0); 2025 2026 bzero(&cmd, sizeof(cmd)); 2027 cmd.opcode = SPACE; 2028 cmd.byte2 = what; 2029 _lto3b(number, cmd.number); 2030 2031 return (scsipi_command(st->sc_link, 2032 (struct scsipi_generic *)&cmd, sizeof(cmd), 2033 0, 0, 0, ST_SPC_TIME, NULL, flags)); 2034 } 2035 2036 /* 2037 * write N filemarks 2038 */ 2039 int 2040 st_write_filemarks(st, number, flags) 2041 struct st_softc *st; 2042 int flags; 2043 int number; 2044 { 2045 struct scsi_write_filemarks cmd; 2046 2047 /* 2048 * It's hard to write a negative number of file marks. 2049 * Don't try. 2050 */ 2051 if (number < 0) 2052 return (EINVAL); 2053 switch (number) { 2054 case 0: /* really a command to sync the drive's buffers */ 2055 break; 2056 case 1: 2057 if (st->flags & ST_FM_WRITTEN) /* already have one down */ 2058 st->flags &= ~ST_WRITTEN; 2059 else 2060 st->flags |= ST_FM_WRITTEN; 2061 st->flags &= ~ST_PER_ACTION; 2062 break; 2063 default: 2064 st->flags &= ~(ST_PER_ACTION | ST_WRITTEN); 2065 } 2066 2067 bzero(&cmd, sizeof(cmd)); 2068 cmd.opcode = WRITE_FILEMARKS; 2069 _lto3b(number, cmd.number); 2070 2071 return (scsipi_command(st->sc_link, 2072 (struct scsipi_generic *)&cmd, sizeof(cmd), 2073 0, 0, 0, ST_IO_TIME * 4, NULL, flags)); 2074 } 2075 2076 /* 2077 * Make sure the right number of file marks is on tape if the 2078 * tape has been written. If the position argument is true, 2079 * leave the tape positioned where it was originally. 2080 * 2081 * nmarks returns the number of marks to skip (or, if position 2082 * true, which were skipped) to get back original position. 2083 */ 2084 int 2085 st_check_eod(st, position, nmarks, flags) 2086 struct st_softc *st; 2087 boolean position; 2088 int *nmarks; 2089 int flags; 2090 { 2091 int error; 2092 2093 switch (st->flags & (ST_WRITTEN | ST_FM_WRITTEN | ST_2FM_AT_EOD)) { 2094 default: 2095 *nmarks = 0; 2096 return (0); 2097 case ST_WRITTEN: 2098 case ST_WRITTEN | ST_FM_WRITTEN | ST_2FM_AT_EOD: 2099 *nmarks = 1; 2100 break; 2101 case ST_WRITTEN | ST_2FM_AT_EOD: 2102 *nmarks = 2; 2103 } 2104 error = st_write_filemarks(st, *nmarks, flags); 2105 if (position && !error) 2106 error = st_space(st, -*nmarks, SP_FILEMARKS, flags); 2107 return (error); 2108 } 2109 2110 /* 2111 * load/unload/retension 2112 */ 2113 int 2114 st_load(st, type, flags) 2115 struct st_softc *st; 2116 u_int type; 2117 int flags; 2118 { 2119 int error; 2120 struct scsi_load cmd; 2121 2122 if (type != LD_LOAD) { 2123 int nmarks; 2124 2125 error = st_check_eod(st, FALSE, &nmarks, flags); 2126 if (error) { 2127 printf("%s: failed to write closing filemarks at " 2128 "unload, errno=%d\n", st->sc_dev.dv_xname, error); 2129 return (error); 2130 } 2131 } 2132 if (st->quirks & ST_Q_IGNORE_LOADS) { 2133 if (type == LD_LOAD) { 2134 /* 2135 * If we ignore loads, at least we should try a rewind. 2136 */ 2137 return st_rewind(st, 0, flags); 2138 } 2139 /* otherwise, we should do what's asked of us */ 2140 } 2141 2142 bzero(&cmd, sizeof(cmd)); 2143 cmd.opcode = LOAD; 2144 cmd.how = type; 2145 2146 error = scsipi_command(st->sc_link, 2147 (struct scsipi_generic *)&cmd, sizeof(cmd), 2148 0, 0, ST_RETRIES, ST_SPC_TIME, NULL, flags); 2149 if (error) { 2150 printf("%s: error %d in st_load (op %d)\n", 2151 st->sc_dev.dv_xname, error, type); 2152 } 2153 return (error); 2154 } 2155 2156 /* 2157 * Rewind the device 2158 */ 2159 int 2160 st_rewind(st, immediate, flags) 2161 struct st_softc *st; 2162 u_int immediate; 2163 int flags; 2164 { 2165 struct scsi_rewind cmd; 2166 int error; 2167 int nmarks; 2168 2169 error = st_check_eod(st, FALSE, &nmarks, flags); 2170 if (error) { 2171 printf("%s: failed to write closing filemarks at " 2172 "rewind, errno=%d\n", st->sc_dev.dv_xname, error); 2173 return (error); 2174 } 2175 st->flags &= ~ST_PER_ACTION; 2176 2177 bzero(&cmd, sizeof(cmd)); 2178 cmd.opcode = REWIND; 2179 cmd.byte2 = immediate; 2180 2181 error = scsipi_command(st->sc_link, 2182 (struct scsipi_generic *)&cmd, sizeof(cmd), 0, 0, ST_RETRIES, 2183 immediate ? ST_CTL_TIME: ST_SPC_TIME, NULL, flags); 2184 if (error) { 2185 printf("%s: error %d trying to rewind\n", 2186 st->sc_dev.dv_xname, error); 2187 } 2188 return (error); 2189 } 2190 2191 int 2192 st_rdpos(st, hard, blkptr) 2193 struct st_softc *st; 2194 int hard; 2195 u_int32_t *blkptr; 2196 { 2197 int error; 2198 u_int8_t posdata[20]; 2199 struct scsi_tape_read_position cmd; 2200 2201 /* 2202 * First flush any pending writes... 2203 */ 2204 error = st_write_filemarks(st, 0, XS_CTL_SILENT); 2205 2206 /* 2207 * The latter case is for 'write protected' tapes 2208 * which are too stupid to recognize a zero count 2209 * for writing filemarks as a no-op. 2210 */ 2211 if (error != 0 && error != EACCES && error != EROFS) 2212 return (error); 2213 2214 bzero(&cmd, sizeof(cmd)); 2215 bzero(&posdata, sizeof(posdata)); 2216 cmd.opcode = READ_POSITION; 2217 if (hard) 2218 cmd.byte1 = 1; 2219 2220 error = scsipi_command(st->sc_link, 2221 (struct scsipi_generic *)&cmd, sizeof(cmd), (u_char *)&posdata, 2222 sizeof(posdata), ST_RETRIES, ST_CTL_TIME, NULL, 2223 XS_CTL_SILENT | XS_CTL_DATA_IN); 2224 2225 if (error == 0) { 2226 #if 0 2227 printf("posdata:"); 2228 for (hard = 0; hard < sizeof(posdata); hard++) 2229 printf("%02x ", posdata[hard] & 0xff); 2230 printf("\n"); 2231 #endif 2232 if (posdata[0] & 0x4) /* Block Position Unknown */ 2233 error = EINVAL; 2234 else 2235 *blkptr = _4btol(&posdata[4]); 2236 } 2237 return (error); 2238 } 2239 2240 int 2241 st_setpos(st, hard, blkptr) 2242 struct st_softc *st; 2243 int hard; 2244 u_int32_t *blkptr; 2245 { 2246 int error; 2247 struct scsi_tape_locate cmd; 2248 2249 /* 2250 * First flush any pending writes. Strictly speaking, 2251 * we're not supposed to have to worry about this, 2252 * but let's be untrusting. 2253 */ 2254 error = st_write_filemarks(st, 0, XS_CTL_SILENT); 2255 2256 /* 2257 * The latter case is for 'write protected' tapes 2258 * which are too stupid to recognize a zero count 2259 * for writing filemarks as a no-op. 2260 */ 2261 if (error != 0 && error != EACCES && error != EROFS) 2262 return (error); 2263 2264 bzero(&cmd, sizeof(cmd)); 2265 cmd.opcode = LOCATE; 2266 if (hard) 2267 cmd.byte2 = 1 << 2; 2268 _lto4b(*blkptr, cmd.blkaddr); 2269 error = scsipi_command(st->sc_link, 2270 (struct scsipi_generic *)&cmd, sizeof(cmd), 2271 NULL, 0, ST_RETRIES, ST_SPC_TIME, NULL, 0); 2272 /* 2273 * XXX: Note file && block number position now unknown (if 2274 * XXX: these things ever start being maintained in this driver) 2275 */ 2276 return (error); 2277 } 2278 2279 2280 /* 2281 * Look at the returned sense and act on the error and determine 2282 * the unix error number to pass back..., 0 (== report no error), 2283 * -1 = retry the operation, -2 continue error processing. 2284 */ 2285 int 2286 st_interpret_sense(xs) 2287 struct scsipi_xfer *xs; 2288 { 2289 struct scsipi_link *sc_link = xs->sc_link; 2290 struct scsipi_sense_data *sense = &xs->sense.scsi_sense; 2291 struct buf *bp = xs->bp; 2292 struct st_softc *st = sc_link->device_softc; 2293 int retval = SCSIRET_CONTINUE; 2294 int doprint = ((xs->xs_control & XS_CTL_SILENT) == 0); 2295 u_int8_t key; 2296 int32_t info; 2297 2298 /* 2299 * If it isn't a extended or extended/deferred error, let 2300 * the generic code handle it. 2301 */ 2302 if ((sense->error_code & SSD_ERRCODE) != 0x70 && 2303 (sense->error_code & SSD_ERRCODE) != 0x71) { /* DEFFERRED */ 2304 return (retval); 2305 } 2306 2307 if (sense->error_code & SSD_ERRCODE_VALID) 2308 info = _4btol(sense->info); 2309 else 2310 info = xs->datalen; /* bad choice if fixed blocks */ 2311 key = sense->flags & SSD_KEY; 2312 st->mt_erreg = key; 2313 st->asc = sense->add_sense_code; 2314 st->ascq = sense->add_sense_code_qual; 2315 st->mt_resid = (short) info; 2316 2317 /* 2318 * If the device is not open yet, let generic handle 2319 */ 2320 if ((sc_link->flags & SDEV_OPEN) == 0) { 2321 return (retval); 2322 } 2323 2324 2325 if (st->flags & ST_FIXEDBLOCKS) { 2326 xs->resid = info * st->blksize; 2327 if (sense->flags & SSD_EOM) { 2328 if ((st->flags & ST_EARLYWARN) == 0) 2329 st->flags |= ST_EIO_PENDING; 2330 st->flags |= ST_EOM_PENDING; 2331 if (bp) 2332 bp->b_resid = xs->resid; 2333 } 2334 if (sense->flags & SSD_FILEMARK) { 2335 st->flags |= ST_AT_FILEMARK; 2336 if (bp) 2337 bp->b_resid = xs->resid; 2338 } 2339 if (sense->flags & SSD_ILI) { 2340 st->flags |= ST_EIO_PENDING; 2341 if (bp) 2342 bp->b_resid = xs->resid; 2343 if (sense->error_code & SSD_ERRCODE_VALID && 2344 (xs->xs_control & XS_CTL_SILENT) == 0) 2345 printf("%s: block wrong size, %d blocks " 2346 "residual\n", st->sc_dev.dv_xname, info); 2347 2348 /* 2349 * This quirk code helps the drive read 2350 * the first tape block, regardless of 2351 * format. That is required for these 2352 * drives to return proper MODE SENSE 2353 * information. 2354 */ 2355 if ((st->quirks & ST_Q_SENSE_HELP) && 2356 !(sc_link->flags & SDEV_MEDIA_LOADED)) 2357 st->blksize -= 512; 2358 } 2359 /* 2360 * If data wanted and no data was tranfered, do it immediatly 2361 */ 2362 if (xs->datalen && xs->resid >= xs->datalen) { 2363 if (st->flags & ST_EIO_PENDING) 2364 return (EIO); 2365 if (st->flags & ST_AT_FILEMARK) { 2366 if (bp) 2367 bp->b_resid = xs->resid; 2368 return (SCSIRET_NOERROR); 2369 } 2370 } 2371 } else { /* must be variable mode */ 2372 if (sense->flags & SSD_EOM) { 2373 /* 2374 * The current semantics of this 2375 * driver requires EOM detection 2376 * to return EIO unless early 2377 * warning detection is enabled 2378 * for variable mode (this is always 2379 * on for fixed block mode). 2380 */ 2381 if (st->flags & ST_EARLYWARN) { 2382 st->flags |= ST_EOM_PENDING; 2383 retval = SCSIRET_NOERROR; 2384 } else { 2385 retval = EIO; 2386 } 2387 2388 /* 2389 * If it's an unadorned EOM detection, 2390 * suppress printing an error. 2391 */ 2392 if (key == SKEY_NO_SENSE) { 2393 doprint = 0; 2394 } 2395 } else if (sense->flags & SSD_FILEMARK) { 2396 retval = SCSIRET_NOERROR; 2397 } else if (sense->flags & SSD_ILI) { 2398 if (info < 0) { 2399 /* 2400 * The tape record was bigger than the read 2401 * we issued. 2402 */ 2403 if ((xs->xs_control & XS_CTL_SILENT) == 0) { 2404 printf("%s: %d-byte tape record too big" 2405 " for %d-byte user buffer\n", 2406 st->sc_dev.dv_xname, 2407 xs->datalen - info, xs->datalen); 2408 } 2409 retval = EIO; 2410 } else { 2411 retval = SCSIRET_NOERROR; 2412 } 2413 } 2414 xs->resid = info; 2415 if (bp) 2416 bp->b_resid = info; 2417 } 2418 2419 #ifndef SCSIDEBUG 2420 if (retval == SCSIRET_NOERROR && key == SKEY_NO_SENSE) { 2421 doprint = 0; 2422 } 2423 #endif 2424 if (key == SKEY_BLANK_CHECK) { 2425 /* 2426 * This quirk code helps the drive read the 2427 * first tape block, regardless of format. That 2428 * is required for these drives to return proper 2429 * MODE SENSE information. 2430 */ 2431 if ((st->quirks & ST_Q_SENSE_HELP) && 2432 !(sc_link->flags & SDEV_MEDIA_LOADED)) { 2433 /* still starting */ 2434 st->blksize -= 512; 2435 } else if (!(st->flags & (ST_2FM_AT_EOD | ST_BLANK_READ))) { 2436 st->flags |= ST_BLANK_READ; 2437 xs->resid = xs->datalen; 2438 if (bp) { 2439 bp->b_resid = xs->resid; 2440 /* return an EOF */ 2441 } 2442 retval = SCSIRET_NOERROR; 2443 } 2444 } 2445 #ifdef SCSIVERBOSE 2446 if (doprint) { 2447 scsipi_print_sense(xs, 0); 2448 } 2449 #else 2450 if (doprint) { 2451 xs->sc_link->sc_print_addr(xs->sc_link); 2452 printf("Sense Key 0x%02x", key); 2453 if ((sense->error_code & SSD_ERRCODE_VALID) != 0) { 2454 switch (key) { 2455 case SKEY_NOT_READY: 2456 case SKEY_ILLEGAL_REQUEST: 2457 case SKEY_UNIT_ATTENTION: 2458 case SKEY_WRITE_PROTECT: 2459 break; 2460 case SKEY_BLANK_CHECK: 2461 printf(", requested size: %d (decimal)", info); 2462 break; 2463 case SKEY_ABORTED_COMMAND: 2464 if (xs->retries) 2465 printf(", retrying"); 2466 printf(", cmd 0x%x, info 0x%x", 2467 xs->cmd->opcode, info); 2468 break; 2469 default: 2470 printf(", info = %d (decimal)", info); 2471 } 2472 } 2473 if (sense->extra_len != 0) { 2474 int n; 2475 printf(", data ="); 2476 for (n = 0; n < sense->extra_len; n++) 2477 printf(" %02x", sense->cmd_spec_info[n]); 2478 } 2479 printf("\n"); 2480 } 2481 #endif 2482 return (retval); 2483 } 2484 2485 /* 2486 * The quirk here is that the drive returns some value to st_mode_sense 2487 * incorrectly until the tape has actually passed by the head. 2488 * 2489 * The method is to set the drive to large fixed-block state (user-specified 2490 * density and 1024-byte blocks), then read and rewind to get it to sense the 2491 * tape. If that doesn't work, try 512-byte fixed blocks. If that doesn't 2492 * work, as a last resort, try variable- length blocks. The result will be 2493 * the ability to do an accurate st_mode_sense. 2494 * 2495 * We know we can do a rewind because we just did a load, which implies rewind. 2496 * Rewind seems preferable to space backward if we have a virgin tape. 2497 * 2498 * The rest of the code for this quirk is in ILI processing and BLANK CHECK 2499 * error processing, both part of st_interpret_sense. 2500 */ 2501 int 2502 st_touch_tape(st) 2503 struct st_softc *st; 2504 { 2505 char *buf; 2506 int readsize; 2507 int error; 2508 2509 buf = malloc(1024, M_TEMP, M_NOWAIT); 2510 if (buf == NULL) 2511 return (ENOMEM); 2512 2513 if ((error = st_mode_sense(st, 0)) != 0) 2514 goto bad; 2515 st->blksize = 1024; 2516 do { 2517 switch (st->blksize) { 2518 case 512: 2519 case 1024: 2520 readsize = st->blksize; 2521 st->flags |= ST_FIXEDBLOCKS; 2522 break; 2523 default: 2524 readsize = 1; 2525 st->flags &= ~ST_FIXEDBLOCKS; 2526 } 2527 if ((error = st_mode_select(st, 0)) != 0) 2528 goto bad; 2529 st_read(st, buf, readsize, XS_CTL_SILENT); /* XXX */ 2530 if ((error = st_rewind(st, 0, 0)) != 0) { 2531 bad: free(buf, M_TEMP); 2532 return (error); 2533 } 2534 } while (readsize != 1 && readsize > st->blksize); 2535 2536 free(buf, M_TEMP); 2537 return (0); 2538 } 2539 2540 int 2541 stdump(dev, blkno, va, size) 2542 dev_t dev; 2543 daddr_t blkno; 2544 caddr_t va; 2545 size_t size; 2546 { 2547 2548 /* Not implemented. */ 2549 return (ENXIO); 2550 } 2551