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