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