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