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