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