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