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