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