Initial import from FreeBSD RELENG_4:
[dragonfly.git] / sys / bus / cam / scsi / scsi_sa.c
... / ...
CommitLineData
1/*
2 * $FreeBSD: src/sys/cam/scsi/scsi_sa.c,v 1.45.2.13 2002/12/17 17:08:50 trhodes Exp $
3 *
4 * Implementation of SCSI Sequential Access Peripheral driver for CAM.
5 *
6 * Copyright (c) 1999, 2000 Matthew Jacob
7 * All rights reserved.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions, and the following disclaimer,
14 * without modification, immediately at the beginning of the file.
15 * 2. The name of the author may not be used to endorse or promote products
16 * derived from this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
22 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 * SUCH DAMAGE.
29 *
30 */
31
32#include <sys/param.h>
33#include <sys/queue.h>
34#ifdef _KERNEL
35#include <sys/systm.h>
36#include <sys/kernel.h>
37#endif
38#include <sys/types.h>
39#include <sys/buf.h>
40#include <sys/malloc.h>
41#include <sys/mtio.h>
42#include <sys/conf.h>
43#include <sys/buf.h>
44#include <sys/devicestat.h>
45#include <machine/limits.h>
46
47#ifndef _KERNEL
48#include <stdio.h>
49#include <string.h>
50#endif
51
52#include <cam/cam.h>
53#include <cam/cam_ccb.h>
54#include <cam/cam_extend.h>
55#include <cam/cam_periph.h>
56#include <cam/cam_xpt_periph.h>
57#include <cam/cam_debug.h>
58
59#include <cam/scsi/scsi_all.h>
60#include <cam/scsi/scsi_message.h>
61#include <cam/scsi/scsi_sa.h>
62
63#ifdef _KERNEL
64
65#include <opt_sa.h>
66
67#ifndef SA_IO_TIMEOUT
68#define SA_IO_TIMEOUT 4
69#endif
70#ifndef SA_SPACE_TIMEOUT
71#define SA_SPACE_TIMEOUT 1 * 60
72#endif
73#ifndef SA_REWIND_TIMEOUT
74#define SA_REWIND_TIMEOUT 2 * 60
75#endif
76#ifndef SA_ERASE_TIMEOUT
77#define SA_ERASE_TIMEOUT 4 * 60
78#endif
79
80#define SCSIOP_TIMEOUT (60 * 1000) /* not an option */
81
82#define IO_TIMEOUT (SA_IO_TIMEOUT * 60 * 1000)
83#define REWIND_TIMEOUT (SA_REWIND_TIMEOUT * 60 * 1000)
84#define ERASE_TIMEOUT (SA_ERASE_TIMEOUT * 60 * 1000)
85#define SPACE_TIMEOUT (SA_SPACE_TIMEOUT * 60 * 1000)
86
87/*
88 * Additional options that can be set for config: SA_1FM_AT_EOT
89 */
90
91#ifndef UNUSED_PARAMETER
92#define UNUSED_PARAMETER(x) x = x
93#endif
94
95#define QFRLS(ccb) \
96 if (((ccb)->ccb_h.status & CAM_DEV_QFRZN) != 0) \
97 cam_release_devq((ccb)->ccb_h.path, 0, 0, 0, FALSE)
98
99/*
100 * Driver states
101 */
102
103
104typedef enum {
105 SA_STATE_NORMAL, SA_STATE_ABNORMAL
106} sa_state;
107
108#define ccb_pflags ppriv_field0
109#define ccb_bp ppriv_ptr1
110
111#define SA_CCB_BUFFER_IO 0x0
112#define SA_CCB_WAITING 0x1
113#define SA_CCB_TYPEMASK 0x1
114#define SA_POSITION_UPDATED 0x2
115
116#define Set_CCB_Type(x, type) \
117 x->ccb_h.ccb_pflags &= ~SA_CCB_TYPEMASK; \
118 x->ccb_h.ccb_pflags |= type
119
120#define CCB_Type(x) (x->ccb_h.ccb_pflags & SA_CCB_TYPEMASK)
121
122
123
124typedef enum {
125 SA_FLAG_OPEN = 0x0001,
126 SA_FLAG_FIXED = 0x0002,
127 SA_FLAG_TAPE_LOCKED = 0x0004,
128 SA_FLAG_TAPE_MOUNTED = 0x0008,
129 SA_FLAG_TAPE_WP = 0x0010,
130 SA_FLAG_TAPE_WRITTEN = 0x0020,
131 SA_FLAG_EOM_PENDING = 0x0040,
132 SA_FLAG_EIO_PENDING = 0x0080,
133 SA_FLAG_EOF_PENDING = 0x0100,
134 SA_FLAG_ERR_PENDING = (SA_FLAG_EOM_PENDING|SA_FLAG_EIO_PENDING|
135 SA_FLAG_EOF_PENDING),
136 SA_FLAG_INVALID = 0x0200,
137 SA_FLAG_COMP_ENABLED = 0x0400,
138 SA_FLAG_COMP_SUPP = 0x0800,
139 SA_FLAG_COMP_UNSUPP = 0x1000,
140 SA_FLAG_TAPE_FROZEN = 0x2000
141} sa_flags;
142
143typedef enum {
144 SA_MODE_REWIND = 0x00,
145 SA_MODE_NOREWIND = 0x01,
146 SA_MODE_OFFLINE = 0x02
147} sa_mode;
148
149typedef enum {
150 SA_PARAM_NONE = 0x00,
151 SA_PARAM_BLOCKSIZE = 0x01,
152 SA_PARAM_DENSITY = 0x02,
153 SA_PARAM_COMPRESSION = 0x04,
154 SA_PARAM_BUFF_MODE = 0x08,
155 SA_PARAM_NUMBLOCKS = 0x10,
156 SA_PARAM_WP = 0x20,
157 SA_PARAM_SPEED = 0x40,
158 SA_PARAM_ALL = 0x7f
159} sa_params;
160
161typedef enum {
162 SA_QUIRK_NONE = 0x00,
163 SA_QUIRK_NOCOMP = 0x01, /* Can't deal with compression at all */
164 SA_QUIRK_FIXED = 0x02, /* Force fixed mode */
165 SA_QUIRK_VARIABLE = 0x04, /* Force variable mode */
166 SA_QUIRK_2FM = 0x08, /* Needs Two File Marks at EOD */
167 SA_QUIRK_1FM = 0x10, /* No more than 1 File Mark at EOD */
168 SA_QUIRK_NODREAD = 0x20, /* Don't try and dummy read density */
169 SA_QUIRK_NO_MODESEL = 0x40, /* Don't do mode select at all */
170 SA_QUIRK_NO_CPAGE = 0x80 /* Don't use DEVICE COMPRESSION page */
171} sa_quirks;
172
173/* units are bits 4-7, 16-21 (1024 units) */
174#define SAUNIT(DEV) \
175 (((minor(DEV) & 0xF0) >> 4) | ((minor(DEV) & 0x3f0000) >> 16))
176
177#define SAMODE(z) ((minor(z) & 0x3))
178#define SADENSITY(z) (((minor(z) >> 2) & 0x3))
179#define SA_IS_CTRL(z) (minor(z) & (1 << 29))
180
181#define SA_NOT_CTLDEV 0
182#define SA_CTLDEV 1
183
184#define SA_ATYPE_R 0
185#define SA_ATYPE_NR 1
186#define SA_ATYPE_ER 2
187
188#define SAMINOR(ctl, unit, mode, access) \
189 ((ctl << 29) | ((unit & 0x3f0) << 16) | ((unit & 0xf) << 4) | \
190 (mode << 0x2) | (access & 0x3))
191
192#define SA_NUM_MODES 4
193struct sa_devs {
194 dev_t ctl_dev;
195 struct sa_mode_devs {
196 dev_t r_dev;
197 dev_t nr_dev;
198 dev_t er_dev;
199 } mode_devs[SA_NUM_MODES];
200 dev_t r_dev;
201 dev_t nr_dev;
202 dev_t er_dev;
203};
204
205struct sa_softc {
206 sa_state state;
207 sa_flags flags;
208 sa_quirks quirks;
209 struct buf_queue_head buf_queue;
210 int queue_count;
211 struct devstat device_stats;
212 struct sa_devs devs;
213 int blk_gran;
214 int blk_mask;
215 int blk_shift;
216 u_int32_t max_blk;
217 u_int32_t min_blk;
218 u_int32_t comp_algorithm;
219 u_int32_t saved_comp_algorithm;
220 u_int32_t media_blksize;
221 u_int32_t last_media_blksize;
222 u_int32_t media_numblks;
223 u_int8_t media_density;
224 u_int8_t speed;
225 u_int8_t scsi_rev;
226 u_int8_t dsreg; /* mtio mt_dsreg, redux */
227 int buffer_mode;
228 int filemarks;
229 union ccb saved_ccb;
230 int last_resid_was_io;
231
232 /*
233 * Relative to BOT Location.
234 */
235 daddr_t fileno;
236 daddr_t blkno;
237
238 /*
239 * Latched Error Info
240 */
241 struct {
242 struct scsi_sense_data _last_io_sense;
243 u_int32_t _last_io_resid;
244 u_int8_t _last_io_cdb[CAM_MAX_CDBLEN];
245 struct scsi_sense_data _last_ctl_sense;
246 u_int32_t _last_ctl_resid;
247 u_int8_t _last_ctl_cdb[CAM_MAX_CDBLEN];
248#define last_io_sense errinfo._last_io_sense
249#define last_io_resid errinfo._last_io_resid
250#define last_io_cdb errinfo._last_io_cdb
251#define last_ctl_sense errinfo._last_ctl_sense
252#define last_ctl_resid errinfo._last_ctl_resid
253#define last_ctl_cdb errinfo._last_ctl_cdb
254 } errinfo;
255 /*
256 * Misc other flags/state
257 */
258 u_int32_t
259 : 31,
260 ctrl_mode : 1; /* control device open */
261};
262
263struct sa_quirk_entry {
264 struct scsi_inquiry_pattern inq_pat; /* matching pattern */
265 sa_quirks quirks; /* specific quirk type */
266 u_int32_t prefblk; /* preferred blocksize when in fixed mode */
267};
268
269static struct sa_quirk_entry sa_quirk_table[] =
270{
271 {
272 { T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "OnStream",
273 "ADR*", "*"}, SA_QUIRK_FIXED|SA_QUIRK_NODREAD |
274 SA_QUIRK_1FM|SA_QUIRK_NO_MODESEL, 32768
275 },
276 {
277 { T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "ARCHIVE",
278 "Python 06408*", "*"}, SA_QUIRK_NODREAD, 0
279 },
280 {
281 { T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "ARCHIVE",
282 "Python 25601*", "*"}, SA_QUIRK_NOCOMP|SA_QUIRK_NODREAD, 0
283 },
284 {
285 { T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "ARCHIVE",
286 "Python*", "*"}, SA_QUIRK_NODREAD, 0
287 },
288 {
289 { T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "ARCHIVE",
290 "VIPER 150*", "*"}, SA_QUIRK_FIXED|SA_QUIRK_1FM, 512
291 },
292 {
293 { T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "ARCHIVE",
294 "VIPER 2525 25462", "-011"},
295 SA_QUIRK_NOCOMP|SA_QUIRK_1FM|SA_QUIRK_NODREAD, 0
296 },
297 {
298 { T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "ARCHIVE",
299 "VIPER 2525*", "*"}, SA_QUIRK_FIXED|SA_QUIRK_1FM, 1024
300 },
301#if 0
302 {
303 { T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "HP",
304 "C15*", "*"}, SA_QUIRK_VARIABLE|SA_QUIRK_NO_CPAGE, 0,
305 },
306#endif
307 {
308 { T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "HP",
309 "C56*", "*"}, SA_QUIRK_VARIABLE|SA_QUIRK_2FM, 0
310 },
311 {
312 { T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "HP",
313 "T20*", "*"}, SA_QUIRK_FIXED|SA_QUIRK_1FM, 512
314 },
315 {
316 { T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "HP",
317 "T4000*", "*"}, SA_QUIRK_FIXED|SA_QUIRK_1FM, 512
318 },
319 {
320 { T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "HP",
321 "HP-88780*", "*"}, SA_QUIRK_VARIABLE|SA_QUIRK_2FM, 0
322 },
323 {
324 { T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "KENNEDY",
325 "*", "*"}, SA_QUIRK_VARIABLE|SA_QUIRK_2FM, 0
326 },
327 {
328 { T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "M4 DATA",
329 "123107 SCSI*", "*"}, SA_QUIRK_VARIABLE|SA_QUIRK_2FM, 0
330 },
331 { /* jreynold@primenet.com */
332 { T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "Seagate",
333 "STT8000N*", "*"}, SA_QUIRK_1FM, 0
334 },
335 { /* mike@sentex.net */
336 { T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "Seagate",
337 "STT20000*", "*"}, SA_QUIRK_1FM, 0
338 },
339 {
340 { T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "TANDBERG",
341 " TDC 3600", "U07:"}, SA_QUIRK_NOCOMP|SA_QUIRK_1FM, 512
342 },
343 {
344 { T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "TANDBERG",
345 " TDC 3800", "*"}, SA_QUIRK_NOCOMP|SA_QUIRK_1FM, 512
346 },
347 {
348 { T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "TANDBERG",
349 " TDC 4100", "*"}, SA_QUIRK_NOCOMP|SA_QUIRK_1FM, 512
350 },
351 {
352 { T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "TANDBERG",
353 " TDC 4200", "*"}, SA_QUIRK_NOCOMP|SA_QUIRK_1FM, 512
354 },
355 {
356 { T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "TANDBERG",
357 " SLR*", "*"}, SA_QUIRK_1FM, 0
358 },
359 {
360 { T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "WANGTEK",
361 "5525ES*", "*"}, SA_QUIRK_FIXED|SA_QUIRK_1FM, 512
362 },
363 {
364 { T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "WANGTEK",
365 "51000*", "*"}, SA_QUIRK_FIXED|SA_QUIRK_1FM, 1024
366 }
367};
368
369static d_open_t saopen;
370static d_close_t saclose;
371static d_strategy_t sastrategy;
372static d_ioctl_t saioctl;
373static periph_init_t sainit;
374static periph_ctor_t saregister;
375static periph_oninv_t saoninvalidate;
376static periph_dtor_t sacleanup;
377static periph_start_t sastart;
378static void saasync(void *callback_arg, u_int32_t code,
379 struct cam_path *path, void *arg);
380static void sadone(struct cam_periph *periph,
381 union ccb *start_ccb);
382static int saerror(union ccb *ccb, u_int32_t cam_flags,
383 u_int32_t sense_flags);
384static int samarkswanted(struct cam_periph *);
385static int sacheckeod(struct cam_periph *periph);
386static int sagetparams(struct cam_periph *periph,
387 sa_params params_to_get,
388 u_int32_t *blocksize, u_int8_t *density,
389 u_int32_t *numblocks, int *buff_mode,
390 u_int8_t *write_protect, u_int8_t *speed,
391 int *comp_supported, int *comp_enabled,
392 u_int32_t *comp_algorithm,
393 sa_comp_t *comp_page);
394static int sasetparams(struct cam_periph *periph,
395 sa_params params_to_set,
396 u_int32_t blocksize, u_int8_t density,
397 u_int32_t comp_algorithm,
398 u_int32_t sense_flags);
399static void saprevent(struct cam_periph *periph, int action);
400static int sarewind(struct cam_periph *periph);
401static int saspace(struct cam_periph *periph, int count,
402 scsi_space_code code);
403static int samount(struct cam_periph *, int, dev_t);
404static int saretension(struct cam_periph *periph);
405static int sareservereleaseunit(struct cam_periph *periph,
406 int reserve);
407static int saloadunload(struct cam_periph *periph, int load);
408static int saerase(struct cam_periph *periph, int longerase);
409static int sawritefilemarks(struct cam_periph *periph,
410 int nmarks, int setmarks);
411static int sardpos(struct cam_periph *periph, int, u_int32_t *);
412static int sasetpos(struct cam_periph *periph, int, u_int32_t *);
413
414
415static struct periph_driver sadriver =
416{
417 sainit, "sa",
418 TAILQ_HEAD_INITIALIZER(sadriver.units), /* generation */ 0
419};
420
421DATA_SET(periphdriver_set, sadriver);
422
423/* For 2.2-stable support */
424#ifndef D_TAPE
425#define D_TAPE 0
426#endif
427
428#define SA_CDEV_MAJOR 14
429
430static struct cdevsw sa_cdevsw = {
431 /* open */ saopen,
432 /* close */ saclose,
433 /* read */ physread,
434 /* write */ physwrite,
435 /* ioctl */ saioctl,
436 /* poll */ nopoll,
437 /* mmap */ nommap,
438 /* strategy */ sastrategy,
439 /* name */ "sa",
440 /* maj */ SA_CDEV_MAJOR,
441 /* dump */ nodump,
442 /* psize */ nopsize,
443 /* flags */ D_TAPE,
444 /* bmaj */ -1
445};
446
447static struct extend_array *saperiphs;
448
449static int
450saopen(dev_t dev, int flags, int fmt, struct proc *p)
451{
452 struct cam_periph *periph;
453 struct sa_softc *softc;
454 int unit;
455 int mode;
456 int density;
457 int error;
458 int s;
459
460 unit = SAUNIT(dev);
461 mode = SAMODE(dev);
462 density = SADENSITY(dev);
463
464 s = splsoftcam();
465 periph = cam_extend_get(saperiphs, unit);
466 if (periph == NULL) {
467 (void) splx(s);
468 return (ENXIO);
469 }
470 softc = (struct sa_softc *)periph->softc;
471 if ((error = cam_periph_lock(periph, PRIBIO|PCATCH)) != 0) {
472 splx(s);
473 return (error);
474 }
475 splx(s);
476
477 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE|CAM_DEBUG_INFO,
478 ("saopen(%d): dev=0x%x softc=0x%x\n", unit, unit, softc->flags));
479
480 if (cam_periph_acquire(periph) != CAM_REQ_CMP) {
481 cam_periph_unlock(periph);
482 return (ENXIO);
483 }
484 if (SA_IS_CTRL(dev)) {
485 softc->ctrl_mode = 1;
486 cam_periph_unlock(periph);
487 return (0);
488 }
489
490
491 if (softc->flags & SA_FLAG_OPEN) {
492 error = EBUSY;
493 } else if (softc->flags & SA_FLAG_INVALID) {
494 error = ENXIO;
495 } else {
496 /*
497 * The function samount ensures media is loaded and ready.
498 * It also does a device RESERVE if the tape isn't yet mounted.
499 */
500 error = samount(periph, flags, dev);
501 }
502
503 if (error) {
504 cam_periph_release(periph);
505 } else {
506 saprevent(periph, PR_PREVENT);
507 softc->flags |= SA_FLAG_OPEN;
508 }
509 cam_periph_unlock(periph);
510 return (error);
511}
512
513static int
514saclose(dev_t dev, int flag, int fmt, struct proc *p)
515{
516 struct cam_periph *periph;
517 struct sa_softc *softc;
518 int unit, mode, error, writing, tmp;
519 int closedbits = SA_FLAG_OPEN;
520
521 unit = SAUNIT(dev);
522 mode = SAMODE(dev);
523 periph = cam_extend_get(saperiphs, unit);
524 if (periph == NULL)
525 return (ENXIO);
526
527 softc = (struct sa_softc *)periph->softc;
528
529 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE|CAM_DEBUG_INFO,
530 ("saclose(%d): dev=0x%x softc=0x%x\n", unit, unit, softc->flags));
531
532
533 if ((error = cam_periph_lock(periph, PRIBIO)) != 0) {
534 return (error);
535 }
536
537 if (SA_IS_CTRL(dev)) {
538 softc->ctrl_mode = 0;
539 cam_periph_release(periph);
540 cam_periph_unlock(periph);
541 return (0);
542 }
543
544 /*
545 * Were we writing the tape?
546 */
547 writing = (softc->flags & SA_FLAG_TAPE_WRITTEN) != 0;
548
549 /*
550 * See whether or not we need to write filemarks. If this
551 * fails, we probably have to assume we've lost tape
552 * position.
553 */
554 error = sacheckeod(periph);
555 if (error) {
556 xpt_print_path(periph->path);
557 printf("failed to write terminating filemark(s)\n");
558 softc->flags |= SA_FLAG_TAPE_FROZEN;
559 }
560
561 /*
562 * Whatever we end up doing, allow users to eject tapes from here on.
563 */
564 saprevent(periph, PR_ALLOW);
565
566 /*
567 * Decide how to end...
568 */
569 if ((softc->flags & SA_FLAG_TAPE_MOUNTED) == 0) {
570 closedbits |= SA_FLAG_TAPE_FROZEN;
571 } else switch (mode) {
572 case SA_MODE_OFFLINE:
573 /*
574 * An 'offline' close is an unconditional release of
575 * frozen && mount conditions, irrespective of whether
576 * these operations succeeded. The reason for this is
577 * to allow at least some kind of programmatic way
578 * around our state getting all fouled up. If somebody
579 * issues an 'offline' command, that will be allowed
580 * to clear state.
581 */
582 (void) sarewind(periph);
583 (void) saloadunload(periph, FALSE);
584 closedbits |= SA_FLAG_TAPE_MOUNTED|SA_FLAG_TAPE_FROZEN;
585 break;
586 case SA_MODE_REWIND:
587 /*
588 * If the rewind fails, return an error- if anyone cares,
589 * but not overwriting any previous error.
590 *
591 * We don't clear the notion of mounted here, but we do
592 * clear the notion of frozen if we successfully rewound.
593 */
594 tmp = sarewind(periph);
595 if (tmp) {
596 if (error != 0)
597 error = tmp;
598 } else {
599 closedbits |= SA_FLAG_TAPE_FROZEN;
600 }
601 break;
602 case SA_MODE_NOREWIND:
603 /*
604 * If we're not rewinding/unloading the tape, find out
605 * whether we need to back up over one of two filemarks
606 * we wrote (if we wrote two filemarks) so that appends
607 * from this point on will be sane.
608 */
609 if (error == 0 && writing && (softc->quirks & SA_QUIRK_2FM)) {
610 tmp = saspace(periph, -1, SS_FILEMARKS);
611 if (tmp) {
612 xpt_print_path(periph->path);
613 printf("unable to backspace over one of double"
614 " filemarks at end of tape\n");
615 xpt_print_path(periph->path);
616 printf("it is possible that this device"
617 " needs a SA_QUIRK_1FM quirk set for it\n");
618 softc->flags |= SA_FLAG_TAPE_FROZEN;
619 }
620 }
621 break;
622 default:
623 xpt_print_path(periph->path);
624 panic("unknown mode 0x%x in saclose\n", mode);
625 /* NOTREACHED */
626 break;
627 }
628
629 /*
630 * We wish to note here that there are no more filemarks to be written.
631 */
632 softc->filemarks = 0;
633 softc->flags &= ~SA_FLAG_TAPE_WRITTEN;
634
635 /*
636 * And we are no longer open for business.
637 */
638 softc->flags &= ~closedbits;
639
640 /*
641 * Inform users if tape state if frozen....
642 */
643 if (softc->flags & SA_FLAG_TAPE_FROZEN) {
644 xpt_print_path(periph->path);
645 printf("tape is now frozen- use an OFFLINE, REWIND or MTEOM "
646 "command to clear this state.\n");
647 }
648
649 /* release the device if it is no longer mounted */
650 if ((softc->flags & SA_FLAG_TAPE_MOUNTED) == 0)
651 sareservereleaseunit(periph, FALSE);
652
653 cam_periph_unlock(periph);
654 cam_periph_release(periph);
655
656 return (error);
657}
658
659/*
660 * Actually translate the requested transfer into one the physical driver
661 * can understand. The transfer is described by a buf and will include
662 * only one physical transfer.
663 */
664static void
665sastrategy(struct buf *bp)
666{
667 struct cam_periph *periph;
668 struct sa_softc *softc;
669 u_int unit;
670 int s;
671
672 if (SA_IS_CTRL(bp->b_dev)) {
673 bp->b_error = EINVAL;
674 goto bad;
675 }
676 unit = SAUNIT(bp->b_dev);
677 periph = cam_extend_get(saperiphs, unit);
678 if (periph == NULL) {
679 bp->b_error = ENXIO;
680 goto bad;
681 }
682 softc = (struct sa_softc *)periph->softc;
683
684 s = splsoftcam();
685
686 if (softc->flags & SA_FLAG_INVALID) {
687 splx(s);
688 bp->b_error = ENXIO;
689 goto bad;
690 }
691
692 if (softc->flags & SA_FLAG_TAPE_FROZEN) {
693 splx(s);
694 bp->b_error = EPERM;
695 goto bad;
696 }
697
698 splx(s);
699
700 /*
701 * If it's a null transfer, return immediatly
702 */
703 if (bp->b_bcount == 0)
704 goto done;
705
706 /* valid request? */
707 if (softc->flags & SA_FLAG_FIXED) {
708 /*
709 * Fixed block device. The byte count must
710 * be a multiple of our block size.
711 */
712 if (((softc->blk_mask != ~0) &&
713 ((bp->b_bcount & softc->blk_mask) != 0)) ||
714 ((softc->blk_mask == ~0) &&
715 ((bp->b_bcount % softc->min_blk) != 0))) {
716 xpt_print_path(periph->path);
717 printf("Invalid request. Fixed block device "
718 "requests must be a multiple "
719 "of %d bytes\n", softc->media_blksize);
720 bp->b_error = EINVAL;
721 goto bad;
722 }
723 } else if ((bp->b_bcount > softc->max_blk) ||
724 (bp->b_bcount < softc->min_blk) ||
725 (bp->b_bcount & softc->blk_mask) != 0) {
726
727 xpt_print_path(periph->path);
728 printf("Invalid request. Variable block device "
729 "requests must be ");
730 if (softc->blk_mask != 0) {
731 printf("a multiple of %d ", (0x1 << softc->blk_gran));
732 }
733 printf("between %d and %d bytes\n", softc->min_blk,
734 softc->max_blk);
735 bp->b_error = EINVAL;
736 goto bad;
737 }
738
739 /*
740 * Mask interrupts so that the device cannot be invalidated until
741 * after we are in the queue. Otherwise, we might not properly
742 * clean up one of the buffers.
743 */
744 s = splbio();
745
746 /*
747 * Place it at the end of the queue.
748 */
749 bufq_insert_tail(&softc->buf_queue, bp);
750
751 softc->queue_count++;
752 CAM_DEBUG(periph->path, CAM_DEBUG_INFO, ("sastrategy: enqueuing a %d "
753 "%s byte %s queue count now %d\n", (int) bp->b_bcount,
754 (softc->flags & SA_FLAG_FIXED)? "fixed" : "variable",
755 (bp->b_flags & B_READ)? "read" : "write", softc->queue_count));
756
757 splx(s);
758
759 /*
760 * Schedule ourselves for performing the work.
761 */
762 xpt_schedule(periph, 1);
763
764 return;
765bad:
766 bp->b_flags |= B_ERROR;
767done:
768
769 /*
770 * Correctly set the buf to indicate a completed xfer
771 */
772 bp->b_resid = bp->b_bcount;
773 biodone(bp);
774}
775
776static int
777saioctl(dev_t dev, u_long cmd, caddr_t arg, int flag, struct proc *p)
778{
779 struct cam_periph *periph;
780 struct sa_softc *softc;
781 scsi_space_code spaceop;
782 int didlockperiph = 0;
783 int s;
784 int unit;
785 int mode;
786 int density;
787 int error = 0;
788
789 unit = SAUNIT(dev);
790 mode = SAMODE(dev);
791 density = SADENSITY(dev);
792 error = 0; /* shut up gcc */
793 spaceop = 0; /* shut up gcc */
794
795 periph = cam_extend_get(saperiphs, unit);
796 if (periph == NULL)
797 return (ENXIO);
798
799 softc = (struct sa_softc *)periph->softc;
800
801 /*
802 * Check for control mode accesses. We allow MTIOCGET and
803 * MTIOCERRSTAT (but need to be the only one open in order
804 * to clear latched status), and MTSETBSIZE, MTSETDNSTY
805 * and MTCOMP (but need to be the only one accessing this
806 * device to run those).
807 */
808
809 if (SA_IS_CTRL(dev)) {
810 switch (cmd) {
811 case MTIOCGETEOTMODEL:
812 case MTIOCGET:
813 break;
814 case MTIOCERRSTAT:
815 /*
816 * If the periph isn't already locked, lock it
817 * so our MTIOCERRSTAT can reset latched error stats.
818 *
819 * If the periph is already locked, skip it because
820 * we're just getting status and it'll be up to the
821 * other thread that has this device open to do
822 * an MTIOCERRSTAT that would clear latched status.
823 */
824 s = splsoftcam();
825 if ((periph->flags & CAM_PERIPH_LOCKED) == 0) {
826 error = cam_periph_lock(periph, PRIBIO|PCATCH);
827 if (error != 0) {
828 splx(s);
829 return (error);
830 }
831 didlockperiph = 1;
832 }
833 break;
834
835 case MTIOCSETEOTMODEL:
836 case MTSETBSIZ:
837 case MTSETDNSTY:
838 case MTCOMP:
839 /*
840 * We need to acquire the peripheral here rather
841 * than at open time because we are sharing writable
842 * access to data structures.
843 */
844 s = splsoftcam();
845 error = cam_periph_lock(periph, PRIBIO|PCATCH);
846 if (error != 0) {
847 splx(s);
848 return (error);
849 }
850 didlockperiph = 1;
851 break;
852
853 default:
854 return (EINVAL);
855 }
856 }
857
858 /*
859 * Find the device that the user is talking about
860 */
861 switch (cmd) {
862 case MTIOCGET:
863 {
864 struct mtget *g = (struct mtget *)arg;
865
866 /*
867 * If this isn't the control mode device, actually go out
868 * and ask the drive again what it's set to.
869 */
870 if (!SA_IS_CTRL(dev)) {
871 u_int8_t write_protect;
872 int comp_enabled, comp_supported;
873 error = sagetparams(periph, SA_PARAM_ALL,
874 &softc->media_blksize, &softc->media_density,
875 &softc->media_numblks, &softc->buffer_mode,
876 &write_protect, &softc->speed, &comp_supported,
877 &comp_enabled, &softc->comp_algorithm, NULL);
878 if (error)
879 break;
880 if (write_protect)
881 softc->flags |= SA_FLAG_TAPE_WP;
882 else
883 softc->flags &= ~SA_FLAG_TAPE_WP;
884 softc->flags &= ~(SA_FLAG_COMP_SUPP|
885 SA_FLAG_COMP_ENABLED|SA_FLAG_COMP_UNSUPP);
886 if (comp_supported) {
887 if (softc->saved_comp_algorithm == 0)
888 softc->saved_comp_algorithm =
889 softc->comp_algorithm;
890 softc->flags |= SA_FLAG_COMP_SUPP;
891 if (comp_enabled)
892 softc->flags |= SA_FLAG_COMP_ENABLED;
893 } else
894 softc->flags |= SA_FLAG_COMP_UNSUPP;
895 }
896 bzero(g, sizeof(struct mtget));
897 g->mt_type = MT_ISAR;
898 if (softc->flags & SA_FLAG_COMP_UNSUPP) {
899 g->mt_comp = MT_COMP_UNSUPP;
900 g->mt_comp0 = MT_COMP_UNSUPP;
901 g->mt_comp1 = MT_COMP_UNSUPP;
902 g->mt_comp2 = MT_COMP_UNSUPP;
903 g->mt_comp3 = MT_COMP_UNSUPP;
904 } else {
905 if ((softc->flags & SA_FLAG_COMP_ENABLED) == 0) {
906 g->mt_comp = MT_COMP_DISABLED;
907 } else {
908 g->mt_comp = softc->comp_algorithm;
909 }
910 g->mt_comp0 = softc->comp_algorithm;
911 g->mt_comp1 = softc->comp_algorithm;
912 g->mt_comp2 = softc->comp_algorithm;
913 g->mt_comp3 = softc->comp_algorithm;
914 }
915 g->mt_density = softc->media_density;
916 g->mt_density0 = softc->media_density;
917 g->mt_density1 = softc->media_density;
918 g->mt_density2 = softc->media_density;
919 g->mt_density3 = softc->media_density;
920 g->mt_blksiz = softc->media_blksize;
921 g->mt_blksiz0 = softc->media_blksize;
922 g->mt_blksiz1 = softc->media_blksize;
923 g->mt_blksiz2 = softc->media_blksize;
924 g->mt_blksiz3 = softc->media_blksize;
925 g->mt_fileno = softc->fileno;
926 g->mt_blkno = softc->blkno;
927 g->mt_dsreg = (short) softc->dsreg;
928 /*
929 * Yes, we know that this is likely to overflow
930 */
931 if (softc->last_resid_was_io) {
932 if ((g->mt_resid = (short) softc->last_io_resid) != 0) {
933 if (SA_IS_CTRL(dev) == 0 || didlockperiph) {
934 softc->last_io_resid = 0;
935 }
936 }
937 } else {
938 if ((g->mt_resid = (short)softc->last_ctl_resid) != 0) {
939 if (SA_IS_CTRL(dev) == 0 || didlockperiph) {
940 softc->last_ctl_resid = 0;
941 }
942 }
943 }
944 error = 0;
945 break;
946 }
947 case MTIOCERRSTAT:
948 {
949 struct scsi_tape_errors *sep =
950 &((union mterrstat *)arg)->scsi_errstat;
951
952 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE,
953 ("saioctl: MTIOCERRSTAT\n"));
954
955 bzero(sep, sizeof(*sep));
956 sep->io_resid = softc->last_io_resid;
957 bcopy((caddr_t) &softc->last_io_sense, sep->io_sense,
958 sizeof (sep->io_sense));
959 bcopy((caddr_t) &softc->last_io_cdb, sep->io_cdb,
960 sizeof (sep->io_cdb));
961 sep->ctl_resid = softc->last_ctl_resid;
962 bcopy((caddr_t) &softc->last_ctl_sense, sep->ctl_sense,
963 sizeof (sep->ctl_sense));
964 bcopy((caddr_t) &softc->last_ctl_cdb, sep->ctl_cdb,
965 sizeof (sep->ctl_cdb));
966
967 if (SA_IS_CTRL(dev) == 0 || didlockperiph)
968 bzero((caddr_t) &softc->errinfo,
969 sizeof (softc->errinfo));
970 error = 0;
971 break;
972 }
973 case MTIOCTOP:
974 {
975 struct mtop *mt;
976 int count;
977
978 mt = (struct mtop *)arg;
979
980 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE,
981 ("saioctl: op=0x%x count=0x%x\n",
982 mt->mt_op, mt->mt_count));
983
984 count = mt->mt_count;
985 switch (mt->mt_op) {
986 case MTWEOF: /* write an end-of-file marker */
987 /*
988 * We don't need to clear the SA_FLAG_TAPE_WRITTEN
989 * flag because by keeping track of filemarks
990 * we have last written we know ehether or not
991 * we need to write more when we close the device.
992 */
993 error = sawritefilemarks(periph, count, FALSE);
994 break;
995 case MTWSS: /* write a setmark */
996 error = sawritefilemarks(periph, count, TRUE);
997 break;
998 case MTBSR: /* backward space record */
999 case MTFSR: /* forward space record */
1000 case MTBSF: /* backward space file */
1001 case MTFSF: /* forward space file */
1002 case MTBSS: /* backward space setmark */
1003 case MTFSS: /* forward space setmark */
1004 case MTEOD: /* space to end of recorded medium */
1005 {
1006 int nmarks;
1007
1008 spaceop = SS_FILEMARKS;
1009 nmarks = softc->filemarks;
1010 error = sacheckeod(periph);
1011 if (error) {
1012 xpt_print_path(periph->path);
1013 printf("EOD check prior to spacing failed\n");
1014 softc->flags |= SA_FLAG_EIO_PENDING;
1015 break;
1016 }
1017 nmarks -= softc->filemarks;
1018 switch(mt->mt_op) {
1019 case MTBSR:
1020 count = -count;
1021 /* FALLTHROUGH */
1022 case MTFSR:
1023 spaceop = SS_BLOCKS;
1024 break;
1025 case MTBSF:
1026 count = -count;
1027 /* FALLTHROUGH */
1028 case MTFSF:
1029 break;
1030 case MTBSS:
1031 count = -count;
1032 /* FALLTHROUGH */
1033 case MTFSS:
1034 spaceop = SS_SETMARKS;
1035 break;
1036 case MTEOD:
1037 spaceop = SS_EOD;
1038 count = 0;
1039 nmarks = 0;
1040 break;
1041 default:
1042 error = EINVAL;
1043 break;
1044 }
1045 if (error)
1046 break;
1047
1048 nmarks = softc->filemarks;
1049 /*
1050 * XXX: Why are we checking again?
1051 */
1052 error = sacheckeod(periph);
1053 if (error)
1054 break;
1055 nmarks -= softc->filemarks;
1056 error = saspace(periph, count - nmarks, spaceop);
1057 /*
1058 * At this point, clear that we've written the tape
1059 * and that we've written any filemarks. We really
1060 * don't know what the applications wishes to do next-
1061 * the sacheckeod's will make sure we terminated the
1062 * tape correctly if we'd been writing, but the next
1063 * action the user application takes will set again
1064 * whether we need to write filemarks.
1065 */
1066 softc->flags &=
1067 ~(SA_FLAG_TAPE_WRITTEN|SA_FLAG_TAPE_FROZEN);
1068 softc->filemarks = 0;
1069 break;
1070 }
1071 case MTREW: /* rewind */
1072 (void) sacheckeod(periph);
1073 error = sarewind(periph);
1074 /* see above */
1075 softc->flags &=
1076 ~(SA_FLAG_TAPE_WRITTEN|SA_FLAG_TAPE_FROZEN);
1077 softc->flags &= ~SA_FLAG_ERR_PENDING;
1078 softc->filemarks = 0;
1079 break;
1080 case MTERASE: /* erase */
1081 error = saerase(periph, count);
1082 softc->flags &=
1083 ~(SA_FLAG_TAPE_WRITTEN|SA_FLAG_TAPE_FROZEN);
1084 softc->flags &= ~SA_FLAG_ERR_PENDING;
1085 break;
1086 case MTRETENS: /* re-tension tape */
1087 error = saretension(periph);
1088 softc->flags &=
1089 ~(SA_FLAG_TAPE_WRITTEN|SA_FLAG_TAPE_FROZEN);
1090 softc->flags &= ~SA_FLAG_ERR_PENDING;
1091 break;
1092 case MTOFFL: /* rewind and put the drive offline */
1093
1094 (void) sacheckeod(periph);
1095 /* see above */
1096 softc->flags &= ~SA_FLAG_TAPE_WRITTEN;
1097 softc->filemarks = 0;
1098
1099 error = sarewind(periph);
1100 /* clear the frozen flag anyway */
1101 softc->flags &= ~SA_FLAG_TAPE_FROZEN;
1102
1103 /*
1104 * Be sure to allow media removal before ejecting.
1105 */
1106
1107 saprevent(periph, PR_ALLOW);
1108 if (error == 0) {
1109 error = saloadunload(periph, FALSE);
1110 if (error == 0) {
1111 softc->flags &= ~SA_FLAG_TAPE_MOUNTED;
1112 }
1113 }
1114 break;
1115
1116 case MTNOP: /* no operation, sets status only */
1117 case MTCACHE: /* enable controller cache */
1118 case MTNOCACHE: /* disable controller cache */
1119 error = 0;
1120 break;
1121
1122 case MTSETBSIZ: /* Set block size for device */
1123
1124 error = sasetparams(periph, SA_PARAM_BLOCKSIZE, count,
1125 0, 0, 0);
1126 if (error == 0) {
1127 softc->last_media_blksize =
1128 softc->media_blksize;
1129 softc->media_blksize = count;
1130 if (count) {
1131 softc->flags |= SA_FLAG_FIXED;
1132 if (powerof2(count)) {
1133 softc->blk_shift =
1134 ffs(count) - 1;
1135 softc->blk_mask = count - 1;
1136 } else {
1137 softc->blk_mask = ~0;
1138 softc->blk_shift = 0;
1139 }
1140 /*
1141 * Make the user's desire 'persistent'.
1142 */
1143 softc->quirks &= ~SA_QUIRK_VARIABLE;
1144 softc->quirks |= SA_QUIRK_FIXED;
1145 } else {
1146 softc->flags &= ~SA_FLAG_FIXED;
1147 if (softc->max_blk == 0) {
1148 softc->max_blk = ~0;
1149 }
1150 softc->blk_shift = 0;
1151 if (softc->blk_gran != 0) {
1152 softc->blk_mask =
1153 softc->blk_gran - 1;
1154 } else {
1155 softc->blk_mask = 0;
1156 }
1157 /*
1158 * Make the user's desire 'persistent'.
1159 */
1160 softc->quirks |= SA_QUIRK_VARIABLE;
1161 softc->quirks &= ~SA_QUIRK_FIXED;
1162 }
1163 }
1164 break;
1165 case MTSETDNSTY: /* Set density for device and mode */
1166 if (count > UCHAR_MAX) {
1167 error = EINVAL;
1168 break;
1169 } else {
1170 error = sasetparams(periph, SA_PARAM_DENSITY,
1171 0, count, 0, 0);
1172 }
1173 break;
1174 case MTCOMP: /* enable compression */
1175 /*
1176 * Some devices don't support compression, and
1177 * don't like it if you ask them for the
1178 * compression page.
1179 */
1180 if ((softc->quirks & SA_QUIRK_NOCOMP) ||
1181 (softc->flags & SA_FLAG_COMP_UNSUPP)) {
1182 error = ENODEV;
1183 break;
1184 }
1185 error = sasetparams(periph, SA_PARAM_COMPRESSION,
1186 0, 0, count, SF_NO_PRINT);
1187 break;
1188 default:
1189 error = EINVAL;
1190 }
1191 break;
1192 }
1193 case MTIOCIEOT:
1194 case MTIOCEEOT:
1195 error = 0;
1196 break;
1197 case MTIOCRDSPOS:
1198 error = sardpos(periph, 0, (u_int32_t *) arg);
1199 break;
1200 case MTIOCRDHPOS:
1201 error = sardpos(periph, 1, (u_int32_t *) arg);
1202 break;
1203 case MTIOCSLOCATE:
1204 error = sasetpos(periph, 0, (u_int32_t *) arg);
1205 break;
1206 case MTIOCHLOCATE:
1207 error = sasetpos(periph, 1, (u_int32_t *) arg);
1208 break;
1209 case MTIOCGETEOTMODEL:
1210 error = 0;
1211 if (softc->quirks & SA_QUIRK_1FM)
1212 mode = 1;
1213 else
1214 mode = 2;
1215 *((u_int32_t *) arg) = mode;
1216 break;
1217 case MTIOCSETEOTMODEL:
1218 error = 0;
1219 switch (*((u_int32_t *) arg)) {
1220 case 1:
1221 softc->quirks &= ~SA_QUIRK_2FM;
1222 softc->quirks |= SA_QUIRK_1FM;
1223 break;
1224 case 2:
1225 softc->quirks &= ~SA_QUIRK_1FM;
1226 softc->quirks |= SA_QUIRK_2FM;
1227 break;
1228 default:
1229 error = EINVAL;
1230 break;
1231 }
1232 break;
1233 default:
1234 error = cam_periph_ioctl(periph, cmd, arg, saerror);
1235 break;
1236 }
1237
1238 /*
1239 * Check to see if we cleared a frozen state
1240 */
1241 if (error == 0 && (softc->flags & SA_FLAG_TAPE_FROZEN)) {
1242 switch(cmd) {
1243 case MTIOCRDSPOS:
1244 case MTIOCRDHPOS:
1245 case MTIOCSLOCATE:
1246 case MTIOCHLOCATE:
1247 softc->fileno = (daddr_t) -1;
1248 softc->blkno = (daddr_t) -1;
1249 softc->flags &= ~SA_FLAG_TAPE_FROZEN;
1250 xpt_print_path(periph->path);
1251 printf("tape state now unfrozen.\n");
1252 break;
1253 default:
1254 break;
1255 }
1256 }
1257 if (didlockperiph) {
1258 cam_periph_unlock(periph);
1259 }
1260 return (error);
1261}
1262
1263static void
1264sainit(void)
1265{
1266 cam_status status;
1267 struct cam_path *path;
1268
1269 /*
1270 * Create our extend array for storing the devices we attach to.
1271 */
1272 saperiphs = cam_extend_new();
1273 if (saperiphs == NULL) {
1274 printf("sa: Failed to alloc extend array!\n");
1275 return;
1276 }
1277
1278 /*
1279 * Install a global async callback.
1280 */
1281 status = xpt_create_path(&path, NULL, CAM_XPT_PATH_ID,
1282 CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD);
1283
1284 if (status == CAM_REQ_CMP) {
1285 /* Register the async callbacks of interrest */
1286 struct ccb_setasync csa; /*
1287 * This is an immediate CCB,
1288 * so using the stack is OK
1289 */
1290 xpt_setup_ccb(&csa.ccb_h, path, 5);
1291 csa.ccb_h.func_code = XPT_SASYNC_CB;
1292 csa.event_enable = AC_FOUND_DEVICE;
1293 csa.callback = saasync;
1294 csa.callback_arg = NULL;
1295 xpt_action((union ccb *)&csa);
1296 status = csa.ccb_h.status;
1297 xpt_free_path(path);
1298 }
1299
1300 if (status != CAM_REQ_CMP) {
1301 printf("sa: Failed to attach master async callback "
1302 "due to status 0x%x!\n", status);
1303 }
1304}
1305
1306static void
1307saoninvalidate(struct cam_periph *periph)
1308{
1309 struct sa_softc *softc;
1310 struct buf *q_bp;
1311 struct ccb_setasync csa;
1312 int s;
1313
1314 softc = (struct sa_softc *)periph->softc;
1315
1316 /*
1317 * De-register any async callbacks.
1318 */
1319 xpt_setup_ccb(&csa.ccb_h, periph->path,
1320 /* priority */ 5);
1321 csa.ccb_h.func_code = XPT_SASYNC_CB;
1322 csa.event_enable = 0;
1323 csa.callback = saasync;
1324 csa.callback_arg = periph;
1325 xpt_action((union ccb *)&csa);
1326
1327 softc->flags |= SA_FLAG_INVALID;
1328
1329 /*
1330 * Although the oninvalidate() routines are always called at
1331 * splsoftcam, we need to be at splbio() here to keep the buffer
1332 * queue from being modified while we traverse it.
1333 */
1334 s = splbio();
1335
1336 /*
1337 * Return all queued I/O with ENXIO.
1338 * XXX Handle any transactions queued to the card
1339 * with XPT_ABORT_CCB.
1340 */
1341 while ((q_bp = bufq_first(&softc->buf_queue)) != NULL){
1342 bufq_remove(&softc->buf_queue, q_bp);
1343 q_bp->b_resid = q_bp->b_bcount;
1344 q_bp->b_error = ENXIO;
1345 q_bp->b_flags |= B_ERROR;
1346 biodone(q_bp);
1347 }
1348 softc->queue_count = 0;
1349 splx(s);
1350
1351 xpt_print_path(periph->path);
1352 printf("lost device\n");
1353
1354}
1355
1356static void
1357sacleanup(struct cam_periph *periph)
1358{
1359 struct sa_softc *softc;
1360 int i;
1361
1362 softc = (struct sa_softc *)periph->softc;
1363
1364 devstat_remove_entry(&softc->device_stats);
1365
1366 destroy_dev(softc->devs.ctl_dev);
1367 destroy_dev(softc->devs.r_dev);
1368 destroy_dev(softc->devs.nr_dev);
1369 destroy_dev(softc->devs.er_dev);
1370
1371 for (i = 0; i < SA_NUM_MODES; i++) {
1372 destroy_dev(softc->devs.mode_devs[i].r_dev);
1373 destroy_dev(softc->devs.mode_devs[i].nr_dev);
1374 destroy_dev(softc->devs.mode_devs[i].er_dev);
1375 }
1376
1377 cam_extend_release(saperiphs, periph->unit_number);
1378 xpt_print_path(periph->path);
1379 printf("removing device entry\n");
1380 free(softc, M_DEVBUF);
1381}
1382
1383static void
1384saasync(void *callback_arg, u_int32_t code,
1385 struct cam_path *path, void *arg)
1386{
1387 struct cam_periph *periph;
1388
1389 periph = (struct cam_periph *)callback_arg;
1390 switch (code) {
1391 case AC_FOUND_DEVICE:
1392 {
1393 struct ccb_getdev *cgd;
1394 cam_status status;
1395
1396 cgd = (struct ccb_getdev *)arg;
1397 if (cgd == NULL)
1398 break;
1399
1400 if (SID_TYPE(&cgd->inq_data) != T_SEQUENTIAL)
1401 break;
1402
1403 /*
1404 * Allocate a peripheral instance for
1405 * this device and start the probe
1406 * process.
1407 */
1408 status = cam_periph_alloc(saregister, saoninvalidate,
1409 sacleanup, sastart,
1410 "sa", CAM_PERIPH_BIO, cgd->ccb_h.path,
1411 saasync, AC_FOUND_DEVICE, cgd);
1412
1413 if (status != CAM_REQ_CMP
1414 && status != CAM_REQ_INPROG)
1415 printf("saasync: Unable to probe new device "
1416 "due to status 0x%x\n", status);
1417 break;
1418 }
1419 default:
1420 cam_periph_async(periph, code, path, arg);
1421 break;
1422 }
1423}
1424
1425static cam_status
1426saregister(struct cam_periph *periph, void *arg)
1427{
1428 struct sa_softc *softc;
1429 struct ccb_setasync csa;
1430 struct ccb_getdev *cgd;
1431 caddr_t match;
1432 int i;
1433
1434 cgd = (struct ccb_getdev *)arg;
1435 if (periph == NULL) {
1436 printf("saregister: periph was NULL!!\n");
1437 return (CAM_REQ_CMP_ERR);
1438 }
1439
1440 if (cgd == NULL) {
1441 printf("saregister: no getdev CCB, can't register device\n");
1442 return (CAM_REQ_CMP_ERR);
1443 }
1444
1445 softc = (struct sa_softc *)malloc(sizeof (*softc), M_DEVBUF, M_NOWAIT);
1446 if (softc == NULL) {
1447 printf("saregister: Unable to probe new device. "
1448 "Unable to allocate softc\n");
1449 return (CAM_REQ_CMP_ERR);
1450 }
1451
1452 bzero(softc, sizeof(*softc));
1453 softc->scsi_rev = SID_ANSI_REV(&cgd->inq_data);
1454 softc->state = SA_STATE_NORMAL;
1455 softc->fileno = (daddr_t) -1;
1456 softc->blkno = (daddr_t) -1;
1457
1458 bufq_init(&softc->buf_queue);
1459 periph->softc = softc;
1460 cam_extend_set(saperiphs, periph->unit_number, periph);
1461
1462 /*
1463 * See if this device has any quirks.
1464 */
1465 match = cam_quirkmatch((caddr_t)&cgd->inq_data,
1466 (caddr_t)sa_quirk_table,
1467 sizeof(sa_quirk_table)/sizeof(*sa_quirk_table),
1468 sizeof(*sa_quirk_table), scsi_inquiry_match);
1469
1470 if (match != NULL) {
1471 softc->quirks = ((struct sa_quirk_entry *)match)->quirks;
1472 softc->last_media_blksize =
1473 ((struct sa_quirk_entry *)match)->prefblk;
1474#ifdef CAMDEBUG
1475 xpt_print_path(periph->path);
1476 printf("found quirk entry %d\n", (int)
1477 (((struct sa_quirk_entry *) match) - sa_quirk_table));
1478#endif
1479 } else
1480 softc->quirks = SA_QUIRK_NONE;
1481
1482 /*
1483 * The SA driver supports a blocksize, but we don't know the
1484 * blocksize until we media is inserted. So, set a flag to
1485 * indicate that the blocksize is unavailable right now.
1486 */
1487 devstat_add_entry(&softc->device_stats, "sa", periph->unit_number, 0,
1488 DEVSTAT_BS_UNAVAILABLE, SID_TYPE(&cgd->inq_data) |
1489 DEVSTAT_TYPE_IF_SCSI, DEVSTAT_PRIORITY_TAPE);
1490
1491 softc->devs.ctl_dev = make_dev(&sa_cdevsw, SAMINOR(SA_CTLDEV,
1492 periph->unit_number, 0, SA_ATYPE_R), UID_ROOT, GID_OPERATOR,
1493 0660, "r%s%d.ctl", periph->periph_name, periph->unit_number);
1494
1495 softc->devs.r_dev = make_dev(&sa_cdevsw, SAMINOR(SA_NOT_CTLDEV,
1496 periph->unit_number, 0, SA_ATYPE_R), UID_ROOT, GID_OPERATOR,
1497 0660, "r%s%d", periph->periph_name, periph->unit_number);
1498
1499 softc->devs.nr_dev = make_dev(&sa_cdevsw, SAMINOR(SA_NOT_CTLDEV,
1500 periph->unit_number, 0, SA_ATYPE_NR), UID_ROOT, GID_OPERATOR,
1501 0660, "nr%s%d", periph->periph_name, periph->unit_number);
1502
1503 softc->devs.er_dev = make_dev(&sa_cdevsw, SAMINOR(SA_NOT_CTLDEV,
1504 periph->unit_number, 0, SA_ATYPE_ER), UID_ROOT, GID_OPERATOR,
1505 0660, "er%s%d", periph->periph_name, periph->unit_number);
1506
1507 for (i = 0; i < SA_NUM_MODES; i++) {
1508
1509 softc->devs.mode_devs[i].r_dev = make_dev(&sa_cdevsw,
1510 SAMINOR(SA_NOT_CTLDEV, periph->unit_number, i, SA_ATYPE_R),
1511 UID_ROOT, GID_OPERATOR, 0660, "r%s%d.%d",
1512 periph->periph_name, periph->unit_number, i);
1513
1514 softc->devs.mode_devs[i].nr_dev = make_dev(&sa_cdevsw,
1515 SAMINOR(SA_NOT_CTLDEV, periph->unit_number, i, SA_ATYPE_NR),
1516 UID_ROOT, GID_OPERATOR, 0660, "nr%s%d.%d",
1517 periph->periph_name, periph->unit_number, i);
1518
1519
1520 softc->devs.mode_devs[i].er_dev = make_dev(&sa_cdevsw,
1521 SAMINOR(SA_NOT_CTLDEV, periph->unit_number, i, SA_ATYPE_ER),
1522 UID_ROOT, GID_OPERATOR, 0660, "er%s%d.%d",
1523 periph->periph_name, periph->unit_number, i);
1524 }
1525
1526 /*
1527 * Add an async callback so that we get
1528 * notified if this device goes away.
1529 */
1530 xpt_setup_ccb(&csa.ccb_h, periph->path, /* priority */ 5);
1531 csa.ccb_h.func_code = XPT_SASYNC_CB;
1532 csa.event_enable = AC_LOST_DEVICE;
1533 csa.callback = saasync;
1534 csa.callback_arg = periph;
1535 xpt_action((union ccb *)&csa);
1536
1537 xpt_announce_periph(periph, NULL);
1538
1539 return (CAM_REQ_CMP);
1540}
1541
1542static void
1543sastart(struct cam_periph *periph, union ccb *start_ccb)
1544{
1545 struct sa_softc *softc;
1546
1547 softc = (struct sa_softc *)periph->softc;
1548
1549 CAM_DEBUG(periph->path, CAM_DEBUG_INFO, ("sastart"));
1550
1551
1552 switch (softc->state) {
1553 case SA_STATE_NORMAL:
1554 {
1555 /* Pull a buffer from the queue and get going on it */
1556 struct buf *bp;
1557 int s;
1558
1559 /*
1560 * See if there is a buf with work for us to do..
1561 */
1562 s = splbio();
1563 bp = bufq_first(&softc->buf_queue);
1564 if (periph->immediate_priority <= periph->pinfo.priority) {
1565 CAM_DEBUG_PRINT(CAM_DEBUG_SUBTRACE,
1566 ("queuing for immediate ccb\n"));
1567 Set_CCB_Type(start_ccb, SA_CCB_WAITING);
1568 SLIST_INSERT_HEAD(&periph->ccb_list, &start_ccb->ccb_h,
1569 periph_links.sle);
1570 periph->immediate_priority = CAM_PRIORITY_NONE;
1571 splx(s);
1572 wakeup(&periph->ccb_list);
1573 } else if (bp == NULL) {
1574 splx(s);
1575 xpt_release_ccb(start_ccb);
1576 } else if ((softc->flags & SA_FLAG_ERR_PENDING) != 0) {
1577 struct buf *done_bp;
1578again:
1579 softc->queue_count--;
1580 bufq_remove(&softc->buf_queue, bp);
1581 bp->b_resid = bp->b_bcount;
1582 done_bp = bp;
1583 if ((softc->flags & SA_FLAG_EOM_PENDING) != 0) {
1584 /*
1585 * We now just clear errors in this case
1586 * and let the residual be the notifier.
1587 */
1588 bp->b_error = 0;
1589 } else if ((softc->flags & SA_FLAG_EOF_PENDING) != 0) {
1590 /*
1591 * This can only happen if we're reading
1592 * in fixed length mode. In this case,
1593 * we dump the rest of the list the
1594 * same way.
1595 */
1596 bp->b_error = 0;
1597 if (bufq_first(&softc->buf_queue) != NULL) {
1598 biodone(done_bp);
1599 goto again;
1600 }
1601 } else if ((softc->flags & SA_FLAG_EIO_PENDING) != 0) {
1602 bp->b_error = EIO;
1603 bp->b_flags |= B_ERROR;
1604 }
1605 bp = bufq_first(&softc->buf_queue);
1606 /*
1607 * Only if we have no other buffers queued up
1608 * do we clear the pending error flag.
1609 */
1610 if (bp == NULL)
1611 softc->flags &= ~SA_FLAG_ERR_PENDING;
1612 CAM_DEBUG(periph->path, CAM_DEBUG_INFO,
1613 ("sastart- ERR_PENDING now 0x%x, bp is %sNULL, "
1614 "%d more buffers queued up\n",
1615 (softc->flags & SA_FLAG_ERR_PENDING),
1616 (bp != NULL)? "not " : " ", softc->queue_count));
1617 splx(s);
1618 xpt_release_ccb(start_ccb);
1619 biodone(done_bp);
1620 } else {
1621 u_int32_t length;
1622
1623 bufq_remove(&softc->buf_queue, bp);
1624 softc->queue_count--;
1625
1626 if ((softc->flags & SA_FLAG_FIXED) != 0) {
1627 if (softc->blk_shift != 0) {
1628 length =
1629 bp->b_bcount >> softc->blk_shift;
1630 } else if (softc->media_blksize != 0) {
1631 length =
1632 bp->b_bcount / softc->media_blksize;
1633 } else {
1634 bp->b_error = EIO;
1635 xpt_print_path(periph->path);
1636 printf("zero blocksize for "
1637 "FIXED length writes?\n");
1638 splx(s);
1639 biodone(bp);
1640 break;
1641 }
1642 CAM_DEBUG(periph->path, CAM_DEBUG_INFO,
1643 ("Fixed Record Count is %d\n", length));
1644 } else {
1645 length = bp->b_bcount;
1646 CAM_DEBUG(start_ccb->ccb_h.path, CAM_DEBUG_INFO,
1647 ("Variable Record Count is %d\n", length));
1648 }
1649 devstat_start_transaction(&softc->device_stats);
1650 /*
1651 * Some people have theorized that we should
1652 * suppress illegal length indication if we are
1653 * running in variable block mode so that we don't
1654 * have to request sense every time our requested
1655 * block size is larger than the written block.
1656 * The residual information from the ccb allows
1657 * us to identify this situation anyway. The only
1658 * problem with this is that we will not get
1659 * information about blocks that are larger than
1660 * our read buffer unless we set the block size
1661 * in the mode page to something other than 0.
1662 *
1663 * I believe that this is a non-issue. If user apps
1664 * don't adjust their read size to match our record
1665 * size, that's just life. Anyway, the typical usage
1666 * would be to issue, e.g., 64KB reads and occasionally
1667 * have to do deal with 512 byte or 1KB intermediate
1668 * records.
1669 */
1670 softc->dsreg = (bp->b_flags & B_READ)?
1671 MTIO_DSREG_RD : MTIO_DSREG_WR;
1672 scsi_sa_read_write(&start_ccb->csio, 0, sadone,
1673 MSG_SIMPLE_Q_TAG, (bp->b_flags & B_READ) != 0,
1674 FALSE, (softc->flags & SA_FLAG_FIXED) != 0,
1675 length, bp->b_data, bp->b_bcount, SSD_FULL_SIZE,
1676 IO_TIMEOUT);
1677 start_ccb->ccb_h.ccb_pflags &= ~SA_POSITION_UPDATED;
1678 Set_CCB_Type(start_ccb, SA_CCB_BUFFER_IO);
1679 start_ccb->ccb_h.ccb_bp = bp;
1680 bp = bufq_first(&softc->buf_queue);
1681 splx(s);
1682 xpt_action(start_ccb);
1683 }
1684
1685 if (bp != NULL) {
1686 /* Have more work to do, so ensure we stay scheduled */
1687 xpt_schedule(periph, 1);
1688 }
1689 break;
1690 }
1691 case SA_STATE_ABNORMAL:
1692 default:
1693 panic("state 0x%x in sastart", softc->state);
1694 break;
1695 }
1696}
1697
1698
1699static void
1700sadone(struct cam_periph *periph, union ccb *done_ccb)
1701{
1702 struct sa_softc *softc;
1703 struct ccb_scsiio *csio;
1704
1705 softc = (struct sa_softc *)periph->softc;
1706 csio = &done_ccb->csio;
1707 switch (CCB_Type(csio)) {
1708 case SA_CCB_BUFFER_IO:
1709 {
1710 struct buf *bp;
1711 int error;
1712
1713 softc->dsreg = MTIO_DSREG_REST;
1714 bp = (struct buf *)done_ccb->ccb_h.ccb_bp;
1715 error = 0;
1716 if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
1717 if ((error = saerror(done_ccb, 0, 0)) == ERESTART) {
1718 /*
1719 * A retry was scheduled, so just return.
1720 */
1721 return;
1722 }
1723 }
1724
1725 if (error == EIO) {
1726 int s;
1727 struct buf *q_bp;
1728
1729 /*
1730 * Catastrophic error. Mark the tape as frozen
1731 * (we no longer know tape position).
1732 *
1733 * Return all queued I/O with EIO, and unfreeze
1734 * our queue so that future transactions that
1735 * attempt to fix this problem can get to the
1736 * device.
1737 *
1738 */
1739
1740 s = splbio();
1741 softc->flags |= SA_FLAG_TAPE_FROZEN;
1742 while ((q_bp = bufq_first(&softc->buf_queue)) != NULL) {
1743 bufq_remove(&softc->buf_queue, q_bp);
1744 q_bp->b_resid = q_bp->b_bcount;
1745 q_bp->b_error = EIO;
1746 q_bp->b_flags |= B_ERROR;
1747 biodone(q_bp);
1748 }
1749 splx(s);
1750 }
1751 if (error != 0) {
1752 bp->b_resid = bp->b_bcount;
1753 bp->b_error = error;
1754 bp->b_flags |= B_ERROR;
1755 /*
1756 * In the error case, position is updated in saerror.
1757 */
1758 } else {
1759 bp->b_resid = csio->resid;
1760 bp->b_error = 0;
1761 if (csio->resid != 0) {
1762 bp->b_flags |= B_ERROR;
1763 }
1764 if ((bp->b_flags & B_READ) == 0) {
1765 softc->flags |= SA_FLAG_TAPE_WRITTEN;
1766 softc->filemarks = 0;
1767 }
1768 if (!(csio->ccb_h.ccb_pflags & SA_POSITION_UPDATED) &&
1769 (softc->blkno != (daddr_t) -1)) {
1770 if ((softc->flags & SA_FLAG_FIXED) != 0) {
1771 u_int32_t l;
1772 if (softc->blk_shift != 0) {
1773 l = bp->b_bcount >>
1774 softc->blk_shift;
1775 } else {
1776 l = bp->b_bcount /
1777 softc->media_blksize;
1778 }
1779 softc->blkno += (daddr_t) l;
1780 } else {
1781 softc->blkno++;
1782 }
1783 }
1784 }
1785 /*
1786 * If we had an error (immediate or pending),
1787 * release the device queue now.
1788 */
1789 if (error || (softc->flags & SA_FLAG_ERR_PENDING))
1790 cam_release_devq(done_ccb->ccb_h.path, 0, 0, 0, 0);
1791#ifdef CAMDEBUG
1792 if (error || bp->b_resid) {
1793 CAM_DEBUG(periph->path, CAM_DEBUG_INFO,
1794 ("error %d resid %ld count %ld\n", error,
1795 bp->b_resid, bp->b_bcount));
1796 }
1797#endif
1798 devstat_end_transaction_buf(&softc->device_stats, bp);
1799 biodone(bp);
1800 break;
1801 }
1802 case SA_CCB_WAITING:
1803 {
1804 /* Caller will release the CCB */
1805 wakeup(&done_ccb->ccb_h.cbfcnp);
1806 return;
1807 }
1808 }
1809 xpt_release_ccb(done_ccb);
1810}
1811
1812/*
1813 * Mount the tape (make sure it's ready for I/O).
1814 */
1815static int
1816samount(struct cam_periph *periph, int oflags, dev_t dev)
1817{
1818 struct sa_softc *softc;
1819 union ccb *ccb;
1820 int error;
1821
1822 /*
1823 * oflags can be checked for 'kind' of open (read-only check) - later
1824 * dev can be checked for a control-mode or compression open - later
1825 */
1826 UNUSED_PARAMETER(oflags);
1827 UNUSED_PARAMETER(dev);
1828
1829
1830 softc = (struct sa_softc *)periph->softc;
1831
1832 /*
1833 * This should determine if something has happend since the last
1834 * open/mount that would invalidate the mount. We do *not* want
1835 * to retry this command- we just want the status. But we only
1836 * do this if we're mounted already- if we're not mounted,
1837 * we don't care about the unit read state and can instead use
1838 * this opportunity to attempt to reserve the tape unit.
1839 */
1840
1841 if (softc->flags & SA_FLAG_TAPE_MOUNTED) {
1842 ccb = cam_periph_getccb(periph, 1);
1843 scsi_test_unit_ready(&ccb->csio, 0, sadone,
1844 MSG_SIMPLE_Q_TAG, SSD_FULL_SIZE, IO_TIMEOUT);
1845 error = cam_periph_runccb(ccb, saerror, 0, SF_NO_PRINT,
1846 &softc->device_stats);
1847 QFRLS(ccb);
1848 if (error == ENXIO) {
1849 softc->flags &= ~SA_FLAG_TAPE_MOUNTED;
1850 scsi_test_unit_ready(&ccb->csio, 0, sadone,
1851 MSG_SIMPLE_Q_TAG, SSD_FULL_SIZE, IO_TIMEOUT);
1852 error = cam_periph_runccb(ccb, saerror, 0, SF_NO_PRINT,
1853 &softc->device_stats);
1854 QFRLS(ccb);
1855 } else if (error) {
1856 /*
1857 * We don't need to freeze the tape because we
1858 * will now attempt to rewind/load it.
1859 */
1860 softc->flags &= ~SA_FLAG_TAPE_MOUNTED;
1861 if (CAM_DEBUGGED(ccb->ccb_h.path, CAM_DEBUG_INFO)) {
1862 xpt_print_path(ccb->ccb_h.path);
1863 printf("error %d on TUR in samount\n", error);
1864 }
1865 }
1866 } else {
1867 error = sareservereleaseunit(periph, TRUE);
1868 if (error) {
1869 return (error);
1870 }
1871 ccb = cam_periph_getccb(periph, 1);
1872 scsi_test_unit_ready(&ccb->csio, 0, sadone,
1873 MSG_SIMPLE_Q_TAG, SSD_FULL_SIZE, IO_TIMEOUT);
1874 error = cam_periph_runccb(ccb, saerror, 0, SF_NO_PRINT,
1875 &softc->device_stats);
1876 QFRLS(ccb);
1877 }
1878
1879 if ((softc->flags & SA_FLAG_TAPE_MOUNTED) == 0) {
1880 struct scsi_read_block_limits_data *rblim = NULL;
1881 int comp_enabled, comp_supported;
1882 u_int8_t write_protect, guessing = 0;
1883
1884 /*
1885 * Clear out old state.
1886 */
1887 softc->flags &= ~(SA_FLAG_TAPE_WP|SA_FLAG_TAPE_WRITTEN|
1888 SA_FLAG_ERR_PENDING|SA_FLAG_COMP_ENABLED|
1889 SA_FLAG_COMP_SUPP|SA_FLAG_COMP_UNSUPP);
1890 softc->filemarks = 0;
1891
1892 /*
1893 * *Very* first off, make sure we're loaded to BOT.
1894 */
1895 scsi_load_unload(&ccb->csio, 2, sadone, MSG_SIMPLE_Q_TAG, FALSE,
1896 FALSE, FALSE, 1, SSD_FULL_SIZE, REWIND_TIMEOUT);
1897 error = cam_periph_runccb(ccb, saerror, 0, SF_NO_PRINT,
1898 &softc->device_stats);
1899 QFRLS(ccb);
1900
1901 /*
1902 * In case this doesn't work, do a REWIND instead
1903 */
1904 if (error) {
1905 scsi_rewind(&ccb->csio, 2, sadone, MSG_SIMPLE_Q_TAG,
1906 FALSE, SSD_FULL_SIZE, REWIND_TIMEOUT);
1907 error = cam_periph_runccb(ccb, saerror, 0, SF_NO_PRINT,
1908 &softc->device_stats);
1909 QFRLS(ccb);
1910 }
1911 if (error) {
1912 xpt_release_ccb(ccb);
1913 goto exit;
1914 }
1915
1916 /*
1917 * Do a dummy test read to force access to the
1918 * media so that the drive will really know what's
1919 * there. We actually don't really care what the
1920 * blocksize on tape is and don't expect to really
1921 * read a full record.
1922 */
1923 rblim = (struct scsi_read_block_limits_data *)
1924 malloc(8192, M_TEMP, M_WAITOK);
1925 if (rblim == NULL) {
1926 xpt_print_path(ccb->ccb_h.path);
1927 printf("no memory for test read\n");
1928 xpt_release_ccb(ccb);
1929 error = ENOMEM;
1930 goto exit;
1931 }
1932
1933 if ((softc->quirks & SA_QUIRK_NODREAD) == 0) {
1934 scsi_sa_read_write(&ccb->csio, 0, sadone,
1935 MSG_SIMPLE_Q_TAG, 1, FALSE, 0, 8192,
1936 (void *) rblim, 8192, SSD_FULL_SIZE,
1937 IO_TIMEOUT);
1938 (void) cam_periph_runccb(ccb, saerror, 0, SF_NO_PRINT,
1939 &softc->device_stats);
1940 QFRLS(ccb);
1941 scsi_rewind(&ccb->csio, 1, sadone, MSG_SIMPLE_Q_TAG,
1942 FALSE, SSD_FULL_SIZE, REWIND_TIMEOUT);
1943 error = cam_periph_runccb(ccb, saerror, 0,
1944 SF_NO_PRINT | SF_RETRY_SELTO | SF_RETRY_UA,
1945 &softc->device_stats);
1946 QFRLS(ccb);
1947 if (error) {
1948 xpt_print_path(ccb->ccb_h.path);
1949 printf("unable to rewind after test read\n");
1950 xpt_release_ccb(ccb);
1951 goto exit;
1952 }
1953 }
1954
1955 /*
1956 * Next off, determine block limits.
1957 */
1958 scsi_read_block_limits(&ccb->csio, 5, sadone, MSG_SIMPLE_Q_TAG,
1959 rblim, SSD_FULL_SIZE, SCSIOP_TIMEOUT);
1960
1961 error = cam_periph_runccb(ccb, saerror, 0,
1962 SF_NO_PRINT | SF_RETRY_UA | SF_RETRY_SELTO,
1963 &softc->device_stats);
1964 QFRLS(ccb);
1965 xpt_release_ccb(ccb);
1966
1967 if (error != 0) {
1968 /*
1969 * If it's less than SCSI-2, READ BLOCK LIMITS is not
1970 * a MANDATORY command. Anyway- it doesn't matter-
1971 * we can proceed anyway.
1972 */
1973 softc->blk_gran = 0;
1974 softc->max_blk = ~0;
1975 softc->min_blk = 0;
1976 } else {
1977 if (softc->scsi_rev >= SCSI_REV_3) {
1978 softc->blk_gran = RBL_GRAN(rblim);
1979 } else {
1980 softc->blk_gran = 0;
1981 }
1982 /*
1983 * We take max_blk == min_blk to mean a default to
1984 * fixed mode- but note that whatever we get out of
1985 * sagetparams below will actually determine whether
1986 * we are actually *in* fixed mode.
1987 */
1988 softc->max_blk = scsi_3btoul(rblim->maximum);
1989 softc->min_blk = scsi_2btoul(rblim->minimum);
1990
1991
1992 }
1993 /*
1994 * Next, perform a mode sense to determine
1995 * current density, blocksize, compression etc.
1996 */
1997 error = sagetparams(periph, SA_PARAM_ALL,
1998 &softc->media_blksize,
1999 &softc->media_density,
2000 &softc->media_numblks,
2001 &softc->buffer_mode, &write_protect,
2002 &softc->speed, &comp_supported,
2003 &comp_enabled, &softc->comp_algorithm,
2004 NULL);
2005
2006 if (error != 0) {
2007 /*
2008 * We could work a little harder here. We could
2009 * adjust our attempts to get information. It
2010 * might be an ancient tape drive. If someone
2011 * nudges us, we'll do that.
2012 */
2013 goto exit;
2014 }
2015
2016 /*
2017 * If no quirk has determined that this is a device that is
2018 * preferred to be in fixed or variable mode, now is the time
2019 * to find out.
2020 */
2021 if ((softc->quirks & (SA_QUIRK_FIXED|SA_QUIRK_VARIABLE)) == 0) {
2022 guessing = 1;
2023 /*
2024 * This could be expensive to find out. Luckily we
2025 * only need to do this once. If we start out in
2026 * 'default' mode, try and set ourselves to one
2027 * of the densities that would determine a wad
2028 * of other stuff. Go from highest to lowest.
2029 */
2030 if (softc->media_density == SCSI_DEFAULT_DENSITY) {
2031 int i;
2032 static u_int8_t ctry[] = {
2033 SCSI_DENSITY_HALFINCH_PE,
2034 SCSI_DENSITY_HALFINCH_6250C,
2035 SCSI_DENSITY_HALFINCH_6250,
2036 SCSI_DENSITY_HALFINCH_1600,
2037 SCSI_DENSITY_HALFINCH_800,
2038 SCSI_DENSITY_QIC_4GB,
2039 SCSI_DENSITY_QIC_2GB,
2040 SCSI_DENSITY_QIC_525_320,
2041 SCSI_DENSITY_QIC_150,
2042 SCSI_DENSITY_QIC_120,
2043 SCSI_DENSITY_QIC_24,
2044 SCSI_DENSITY_QIC_11_9TRK,
2045 SCSI_DENSITY_QIC_11_4TRK,
2046 SCSI_DENSITY_QIC_1320,
2047 SCSI_DENSITY_QIC_3080,
2048 0
2049 };
2050 for (i = 0; ctry[i]; i++) {
2051 error = sasetparams(periph,
2052 SA_PARAM_DENSITY, 0, ctry[i],
2053 0, SF_NO_PRINT);
2054 if (error == 0) {
2055 softc->media_density = ctry[i];
2056 break;
2057 }
2058 }
2059 }
2060 switch (softc->media_density) {
2061 case SCSI_DENSITY_QIC_11_4TRK:
2062 case SCSI_DENSITY_QIC_11_9TRK:
2063 case SCSI_DENSITY_QIC_24:
2064 case SCSI_DENSITY_QIC_120:
2065 case SCSI_DENSITY_QIC_150:
2066 case SCSI_DENSITY_QIC_525_320:
2067 case SCSI_DENSITY_QIC_1320:
2068 case SCSI_DENSITY_QIC_3080:
2069 softc->quirks &= ~SA_QUIRK_2FM;
2070 softc->quirks |= SA_QUIRK_FIXED|SA_QUIRK_1FM;
2071 softc->last_media_blksize = 512;
2072 break;
2073 case SCSI_DENSITY_QIC_4GB:
2074 case SCSI_DENSITY_QIC_2GB:
2075 softc->quirks &= ~SA_QUIRK_2FM;
2076 softc->quirks |= SA_QUIRK_FIXED|SA_QUIRK_1FM;
2077 softc->last_media_blksize = 1024;
2078 break;
2079 default:
2080 softc->last_media_blksize =
2081 softc->media_blksize;
2082 softc->quirks |= SA_QUIRK_VARIABLE;
2083 break;
2084 }
2085 }
2086
2087 /*
2088 * If no quirk has determined that this is a device that needs
2089 * to have 2 Filemarks at EOD, now is the time to find out.
2090 */
2091
2092 if ((softc->quirks & SA_QUIRK_2FM) == 0) {
2093 switch (softc->media_density) {
2094 case SCSI_DENSITY_HALFINCH_800:
2095 case SCSI_DENSITY_HALFINCH_1600:
2096 case SCSI_DENSITY_HALFINCH_6250:
2097 case SCSI_DENSITY_HALFINCH_6250C:
2098 case SCSI_DENSITY_HALFINCH_PE:
2099 softc->quirks &= ~SA_QUIRK_1FM;
2100 softc->quirks |= SA_QUIRK_2FM;
2101 break;
2102 default:
2103 break;
2104 }
2105 }
2106
2107 /*
2108 * Now validate that some info we got makes sense.
2109 */
2110 if ((softc->max_blk < softc->media_blksize) ||
2111 (softc->min_blk > softc->media_blksize &&
2112 softc->media_blksize)) {
2113 xpt_print_path(ccb->ccb_h.path);
2114 printf("BLOCK LIMITS (%d..%d) could not match current "
2115 "block settings (%d)- adjusting\n", softc->min_blk,
2116 softc->max_blk, softc->media_blksize);
2117 softc->max_blk = softc->min_blk =
2118 softc->media_blksize;
2119 }
2120
2121 /*
2122 * Now put ourselves into the right frame of mind based
2123 * upon quirks...
2124 */
2125tryagain:
2126 /*
2127 * If we want to be in FIXED mode and our current blocksize
2128 * is not equal to our last blocksize (if nonzero), try and
2129 * set ourselves to this last blocksize (as the 'preferred'
2130 * block size). The initial quirkmatch at registry sets the
2131 * initial 'last' blocksize. If, for whatever reason, this
2132 * 'last' blocksize is zero, set the blocksize to 512,
2133 * or min_blk if that's larger.
2134 */
2135 if ((softc->quirks & SA_QUIRK_FIXED) &&
2136 (softc->quirks & SA_QUIRK_NO_MODESEL) == 0 &&
2137 (softc->media_blksize != softc->last_media_blksize)) {
2138 softc->media_blksize = softc->last_media_blksize;
2139 if (softc->media_blksize == 0) {
2140 softc->media_blksize = 512;
2141 if (softc->media_blksize < softc->min_blk) {
2142 softc->media_blksize = softc->min_blk;
2143 }
2144 }
2145 error = sasetparams(periph, SA_PARAM_BLOCKSIZE,
2146 softc->media_blksize, 0, 0, SF_NO_PRINT);
2147 if (error) {
2148 xpt_print_path(ccb->ccb_h.path);
2149 printf("unable to set fixed blocksize to %d\n",
2150 softc->media_blksize);
2151 goto exit;
2152 }
2153 }
2154
2155 if ((softc->quirks & SA_QUIRK_VARIABLE) &&
2156 (softc->media_blksize != 0)) {
2157 softc->last_media_blksize = softc->media_blksize;
2158 softc->media_blksize = 0;
2159 error = sasetparams(periph, SA_PARAM_BLOCKSIZE,
2160 0, 0, 0, SF_NO_PRINT);
2161 if (error) {
2162 /*
2163 * If this fails and we were guessing, just
2164 * assume that we got it wrong and go try
2165 * fixed block mode. Don't even check against
2166 * density code at this point.
2167 */
2168 if (guessing) {
2169 softc->quirks &= ~SA_QUIRK_VARIABLE;
2170 softc->quirks |= SA_QUIRK_FIXED;
2171 if (softc->last_media_blksize == 0)
2172 softc->last_media_blksize = 512;
2173 goto tryagain;
2174 }
2175 xpt_print_path(ccb->ccb_h.path);
2176 printf("unable to set variable blocksize\n");
2177 goto exit;
2178 }
2179 }
2180
2181 /*
2182 * Now that we have the current block size,
2183 * set up some parameters for sastart's usage.
2184 */
2185 if (softc->media_blksize) {
2186 softc->flags |= SA_FLAG_FIXED;
2187 if (powerof2(softc->media_blksize)) {
2188 softc->blk_shift =
2189 ffs(softc->media_blksize) - 1;
2190 softc->blk_mask = softc->media_blksize - 1;
2191 } else {
2192 softc->blk_mask = ~0;
2193 softc->blk_shift = 0;
2194 }
2195 } else {
2196 /*
2197 * The SCSI-3 spec allows 0 to mean "unspecified".
2198 * The SCSI-1 spec allows 0 to mean 'infinite'.
2199 *
2200 * Either works here.
2201 */
2202 if (softc->max_blk == 0) {
2203 softc->max_blk = ~0;
2204 }
2205 softc->blk_shift = 0;
2206 if (softc->blk_gran != 0) {
2207 softc->blk_mask = softc->blk_gran - 1;
2208 } else {
2209 softc->blk_mask = 0;
2210 }
2211 }
2212
2213 if (write_protect)
2214 softc->flags |= SA_FLAG_TAPE_WP;
2215
2216 if (comp_supported) {
2217 if (softc->saved_comp_algorithm == 0)
2218 softc->saved_comp_algorithm =
2219 softc->comp_algorithm;
2220 softc->flags |= SA_FLAG_COMP_SUPP;
2221 if (comp_enabled)
2222 softc->flags |= SA_FLAG_COMP_ENABLED;
2223 } else
2224 softc->flags |= SA_FLAG_COMP_UNSUPP;
2225
2226 if ((softc->buffer_mode == SMH_SA_BUF_MODE_NOBUF) &&
2227 (softc->quirks & SA_QUIRK_NO_MODESEL) == 0) {
2228 error = sasetparams(periph, SA_PARAM_BUFF_MODE, 0,
2229 0, 0, SF_NO_PRINT);
2230 if (error == 0) {
2231 softc->buffer_mode = SMH_SA_BUF_MODE_SIBUF;
2232 } else {
2233 xpt_print_path(ccb->ccb_h.path);
2234 printf("unable to set buffered mode\n");
2235 }
2236 error = 0; /* not an error */
2237 }
2238
2239
2240 if (error == 0) {
2241 softc->flags |= SA_FLAG_TAPE_MOUNTED;
2242 }
2243exit:
2244 if (rblim != NULL)
2245 free(rblim, M_TEMP);
2246
2247 if (error != 0) {
2248 softc->dsreg = MTIO_DSREG_NIL;
2249 } else {
2250 softc->fileno = softc->blkno = 0;
2251 softc->dsreg = MTIO_DSREG_REST;
2252 }
2253#ifdef SA_1FM_AT_EOD
2254 if ((softc->quirks & SA_QUIRK_2FM) == 0)
2255 softc->quirks |= SA_QUIRK_1FM;
2256#else
2257 if ((softc->quirks & SA_QUIRK_1FM) == 0)
2258 softc->quirks |= SA_QUIRK_2FM;
2259#endif
2260 } else
2261 xpt_release_ccb(ccb);
2262
2263 /*
2264 * If we return an error, we're not mounted any more,
2265 * so release any device reservation.
2266 */
2267 if (error != 0) {
2268 (void) sareservereleaseunit(periph, FALSE);
2269 } else {
2270 /*
2271 * Clear I/O residual.
2272 */
2273 softc->last_io_resid = 0;
2274 softc->last_ctl_resid = 0;
2275 }
2276 return (error);
2277}
2278
2279/*
2280 * How many filemarks do we need to write if we were to terminate the
2281 * tape session right now? Note that this can be a negative number
2282 */
2283
2284static int
2285samarkswanted(struct cam_periph *periph)
2286{
2287 int markswanted;
2288 struct sa_softc *softc;
2289
2290 softc = (struct sa_softc *)periph->softc;
2291 markswanted = 0;
2292 if ((softc->flags & SA_FLAG_TAPE_WRITTEN) != 0) {
2293 markswanted++;
2294 if (softc->quirks & SA_QUIRK_2FM)
2295 markswanted++;
2296 }
2297 markswanted -= softc->filemarks;
2298 return (markswanted);
2299}
2300
2301static int
2302sacheckeod(struct cam_periph *periph)
2303{
2304 int error;
2305 int markswanted;
2306 struct sa_softc *softc;
2307
2308 softc = (struct sa_softc *)periph->softc;
2309 markswanted = samarkswanted(periph);
2310
2311 if (markswanted > 0) {
2312 error = sawritefilemarks(periph, markswanted, FALSE);
2313 } else {
2314 error = 0;
2315 }
2316 return (error);
2317}
2318
2319static int
2320saerror(union ccb *ccb, u_int32_t cflgs, u_int32_t sflgs)
2321{
2322 static const char *toobig =
2323 "%d-byte tape record bigger than supplied buffer\n";
2324 struct cam_periph *periph;
2325 struct sa_softc *softc;
2326 struct ccb_scsiio *csio;
2327 struct scsi_sense_data *sense;
2328 u_int32_t resid = 0;
2329 int32_t info = 0;
2330 cam_status status;
2331 int error_code, sense_key, asc, ascq, error, aqvalid;
2332
2333 periph = xpt_path_periph(ccb->ccb_h.path);
2334 softc = (struct sa_softc *)periph->softc;
2335 csio = &ccb->csio;
2336 sense = &csio->sense_data;
2337 scsi_extract_sense(sense, &error_code, &sense_key, &asc, &ascq);
2338 aqvalid = sense->extra_len >= 6;
2339 error = 0;
2340
2341 status = csio->ccb_h.status & CAM_STATUS_MASK;
2342
2343 /*
2344 * Calculate/latch up, any residuals... We do this in a funny 2-step
2345 * so we can print stuff here if we have CAM_DEBUG enabled for this
2346 * unit.
2347 */
2348 if (status == CAM_SCSI_STATUS_ERROR) {
2349 if ((sense->error_code & SSD_ERRCODE_VALID) != 0) {
2350 info = (int32_t) scsi_4btoul(sense->info);
2351 resid = info;
2352 if ((softc->flags & SA_FLAG_FIXED) != 0)
2353 resid *= softc->media_blksize;
2354 } else {
2355 resid = csio->dxfer_len;
2356 info = resid;
2357 if ((softc->flags & SA_FLAG_FIXED) != 0) {
2358 if (softc->media_blksize)
2359 info /= softc->media_blksize;
2360 }
2361 }
2362 if (CCB_Type(csio) == SA_CCB_BUFFER_IO) {
2363 bcopy((caddr_t) sense, (caddr_t) &softc->last_io_sense,
2364 sizeof (struct scsi_sense_data));
2365 bcopy(csio->cdb_io.cdb_bytes, softc->last_io_cdb,
2366 (int) csio->cdb_len);
2367 softc->last_io_resid = resid;
2368 softc->last_resid_was_io = 1;
2369 } else {
2370 bcopy((caddr_t) sense, (caddr_t) &softc->last_ctl_sense,
2371 sizeof (struct scsi_sense_data));
2372 bcopy(csio->cdb_io.cdb_bytes, softc->last_ctl_cdb,
2373 (int) csio->cdb_len);
2374 softc->last_ctl_resid = resid;
2375 softc->last_resid_was_io = 0;
2376 }
2377 CAM_DEBUG(periph->path, CAM_DEBUG_INFO, ("CDB[0]=0x%x Key 0x%x "
2378 "ASC/ASCQ 0x%x/0x%x CAM STATUS 0x%x flags 0x%x resid %d "
2379 "dxfer_len %d\n", csio->cdb_io.cdb_bytes[0] & 0xff,
2380 sense_key, asc, ascq, status,
2381 sense->flags & ~SSD_KEY_RESERVED, resid, csio->dxfer_len));
2382 } else {
2383 CAM_DEBUG(periph->path, CAM_DEBUG_INFO,
2384 ("Cam Status 0x%x\n", status));
2385 }
2386
2387 switch (status) {
2388 case CAM_REQ_CMP:
2389 return (0);
2390 case CAM_SCSI_STATUS_ERROR:
2391 /*
2392 * If a read/write command, we handle it here.
2393 */
2394 if (CCB_Type(csio) != SA_CCB_WAITING) {
2395 break;
2396 }
2397 /*
2398 * If this was just EOM/EOP, Filemark, Setmark or ILI detected
2399 * on a non read/write command, we assume it's not an error
2400 * and propagate the residule and return.
2401 */
2402 if ((aqvalid && asc == 0 && ascq > 0 && ascq <= 5) ||
2403 (aqvalid == 0 && sense_key == SSD_KEY_NO_SENSE)) {
2404 csio->resid = resid;
2405 QFRLS(ccb);
2406 return (0);
2407 }
2408 /*
2409 * Otherwise, we let the common code handle this.
2410 */
2411 return (cam_periph_error(ccb, cflgs, sflgs, &softc->saved_ccb));
2412
2413 /*
2414 * XXX: To Be Fixed
2415 * We cannot depend upon CAM honoring retry counts for these.
2416 */
2417 case CAM_SCSI_BUS_RESET:
2418 case CAM_BDR_SENT:
2419 if (ccb->ccb_h.retry_count <= 0) {
2420 return (EIO);
2421 break;
2422 }
2423 /* FALLTHROUGH */
2424 default:
2425 return (cam_periph_error(ccb, cflgs, sflgs, &softc->saved_ccb));
2426 }
2427
2428 /*
2429 * Handle filemark, end of tape, mismatched record sizes....
2430 * From this point out, we're only handling read/write cases.
2431 * Handle writes && reads differently.
2432 */
2433
2434 if (csio->cdb_io.cdb_bytes[0] == SA_WRITE) {
2435 if (sense_key == SSD_KEY_VOLUME_OVERFLOW) {
2436 csio->resid = resid;
2437 error = ENOSPC;
2438 } else if (sense->flags & SSD_EOM) {
2439 softc->flags |= SA_FLAG_EOM_PENDING;
2440 /*
2441 * Grotesque as it seems, the few times
2442 * I've actually seen a non-zero resid,
2443 * the tape drive actually lied and had
2444 * writtent all the data!.
2445 */
2446 csio->resid = 0;
2447 }
2448 } else {
2449 csio->resid = resid;
2450 if (sense_key == SSD_KEY_BLANK_CHECK) {
2451 if (softc->quirks & SA_QUIRK_1FM) {
2452 error = 0;
2453 softc->flags |= SA_FLAG_EOM_PENDING;
2454 } else {
2455 error = EIO;
2456 }
2457 } else if (sense->flags & SSD_FILEMARK) {
2458 if (softc->flags & SA_FLAG_FIXED) {
2459 error = -1;
2460 softc->flags |= SA_FLAG_EOF_PENDING;
2461 }
2462 /*
2463 * Unconditionally, if we detected a filemark on a read,
2464 * mark that we've run moved a file ahead.
2465 */
2466 if (softc->fileno != (daddr_t) -1) {
2467 softc->fileno++;
2468 softc->blkno = 0;
2469 csio->ccb_h.ccb_pflags |= SA_POSITION_UPDATED;
2470 }
2471 }
2472 }
2473
2474 /*
2475 * Incorrect Length usually applies to read, but can apply to writes.
2476 */
2477 if (error == 0 && (sense->flags & SSD_ILI)) {
2478 if (info < 0) {
2479 xpt_print_path(csio->ccb_h.path);
2480 printf(toobig, csio->dxfer_len - info);
2481 csio->resid = csio->dxfer_len;
2482 error = EIO;
2483 } else {
2484 csio->resid = resid;
2485 if (softc->flags & SA_FLAG_FIXED) {
2486 softc->flags |= SA_FLAG_EIO_PENDING;
2487 }
2488 /*
2489 * Bump the block number if we hadn't seen a filemark.
2490 * Do this independent of errors (we've moved anyway).
2491 */
2492 if ((sense->flags & SSD_FILEMARK) == 0) {
2493 if (softc->blkno != (daddr_t) -1) {
2494 softc->blkno++;
2495 csio->ccb_h.ccb_pflags |=
2496 SA_POSITION_UPDATED;
2497 }
2498 }
2499 }
2500 }
2501
2502 if (error <= 0) {
2503 /*
2504 * Unfreeze the queue if frozen as we're not returning anything
2505 * to our waiters that would indicate an I/O error has occurred
2506 * (yet).
2507 */
2508 QFRLS(ccb);
2509 error = 0;
2510 }
2511 return (error);
2512}
2513
2514static int
2515sagetparams(struct cam_periph *periph, sa_params params_to_get,
2516 u_int32_t *blocksize, u_int8_t *density, u_int32_t *numblocks,
2517 int *buff_mode, u_int8_t *write_protect, u_int8_t *speed,
2518 int *comp_supported, int *comp_enabled, u_int32_t *comp_algorithm,
2519 sa_comp_t *tcs)
2520{
2521 union ccb *ccb;
2522 void *mode_buffer;
2523 struct scsi_mode_header_6 *mode_hdr;
2524 struct scsi_mode_blk_desc *mode_blk;
2525 int mode_buffer_len;
2526 struct sa_softc *softc;
2527 u_int8_t cpage;
2528 int error;
2529 cam_status status;
2530
2531 softc = (struct sa_softc *)periph->softc;
2532 ccb = cam_periph_getccb(periph, 1);
2533 if (softc->quirks & SA_QUIRK_NO_CPAGE)
2534 cpage = SA_DEVICE_CONFIGURATION_PAGE;
2535 else
2536 cpage = SA_DATA_COMPRESSION_PAGE;
2537
2538retry:
2539 mode_buffer_len = sizeof(*mode_hdr) + sizeof(*mode_blk);
2540
2541 if (params_to_get & SA_PARAM_COMPRESSION) {
2542 if (softc->quirks & SA_QUIRK_NOCOMP) {
2543 *comp_supported = FALSE;
2544 params_to_get &= ~SA_PARAM_COMPRESSION;
2545 } else
2546 mode_buffer_len += sizeof (sa_comp_t);
2547 }
2548
2549 mode_buffer = malloc(mode_buffer_len, M_TEMP, M_WAITOK);
2550 bzero(mode_buffer, mode_buffer_len);
2551 mode_hdr = (struct scsi_mode_header_6 *)mode_buffer;
2552 mode_blk = (struct scsi_mode_blk_desc *)&mode_hdr[1];
2553
2554 /* it is safe to retry this */
2555 scsi_mode_sense(&ccb->csio, 5, sadone, MSG_SIMPLE_Q_TAG, FALSE,
2556 SMS_PAGE_CTRL_CURRENT, (params_to_get & SA_PARAM_COMPRESSION) ?
2557 cpage : SMS_VENDOR_SPECIFIC_PAGE, mode_buffer, mode_buffer_len,
2558 SSD_FULL_SIZE, SCSIOP_TIMEOUT);
2559
2560 error = cam_periph_runccb(ccb, saerror, 0, SF_NO_PRINT,
2561 &softc->device_stats);
2562 QFRLS(ccb);
2563
2564 status = ccb->ccb_h.status & CAM_STATUS_MASK;
2565
2566 if (error == EINVAL && (params_to_get & SA_PARAM_COMPRESSION) != 0) {
2567 /*
2568 * Hmm. Let's see if we can try another page...
2569 * If we've already done that, give up on compression
2570 * for this device and remember this for the future
2571 * and attempt the request without asking for compression
2572 * info.
2573 */
2574 if (cpage == SA_DATA_COMPRESSION_PAGE) {
2575 cpage = SA_DEVICE_CONFIGURATION_PAGE;
2576 goto retry;
2577 }
2578 softc->quirks |= SA_QUIRK_NOCOMP;
2579 free(mode_buffer, M_TEMP);
2580 goto retry;
2581 } else if (status == CAM_SCSI_STATUS_ERROR) {
2582 /* Tell the user about the fatal error. */
2583 scsi_sense_print(&ccb->csio);
2584 goto sagetparamsexit;
2585 }
2586
2587 /*
2588 * If the user only wants the compression information, and
2589 * the device doesn't send back the block descriptor, it's
2590 * no big deal. If the user wants more than just
2591 * compression, though, and the device doesn't pass back the
2592 * block descriptor, we need to send another mode sense to
2593 * get the block descriptor.
2594 */
2595 if ((mode_hdr->blk_desc_len == 0) &&
2596 (params_to_get & SA_PARAM_COMPRESSION) &&
2597 (params_to_get & ~(SA_PARAM_COMPRESSION))) {
2598
2599 /*
2600 * Decrease the mode buffer length by the size of
2601 * the compression page, to make sure the data
2602 * there doesn't get overwritten.
2603 */
2604 mode_buffer_len -= sizeof (sa_comp_t);
2605
2606 /*
2607 * Now move the compression page that we presumably
2608 * got back down the memory chunk a little bit so
2609 * it doesn't get spammed.
2610 */
2611 bcopy(&mode_hdr[0], &mode_hdr[1], sizeof (sa_comp_t));
2612 bzero(&mode_hdr[0], sizeof (mode_hdr[0]));
2613
2614 /*
2615 * Now, we issue another mode sense and just ask
2616 * for the block descriptor, etc.
2617 */
2618
2619 scsi_mode_sense(&ccb->csio, 2, sadone, MSG_SIMPLE_Q_TAG, FALSE,
2620 SMS_PAGE_CTRL_CURRENT, SMS_VENDOR_SPECIFIC_PAGE,
2621 mode_buffer, mode_buffer_len, SSD_FULL_SIZE,
2622 SCSIOP_TIMEOUT);
2623
2624 error = cam_periph_runccb(ccb, saerror, 0, SF_NO_PRINT,
2625 &softc->device_stats);
2626 QFRLS(ccb);
2627
2628 if (error != 0)
2629 goto sagetparamsexit;
2630 }
2631
2632 if (params_to_get & SA_PARAM_BLOCKSIZE)
2633 *blocksize = scsi_3btoul(mode_blk->blklen);
2634
2635 if (params_to_get & SA_PARAM_NUMBLOCKS)
2636 *numblocks = scsi_3btoul(mode_blk->nblocks);
2637
2638 if (params_to_get & SA_PARAM_BUFF_MODE)
2639 *buff_mode = mode_hdr->dev_spec & SMH_SA_BUF_MODE_MASK;
2640
2641 if (params_to_get & SA_PARAM_DENSITY)
2642 *density = mode_blk->density;
2643
2644 if (params_to_get & SA_PARAM_WP)
2645 *write_protect = (mode_hdr->dev_spec & SMH_SA_WP)? TRUE : FALSE;
2646
2647 if (params_to_get & SA_PARAM_SPEED)
2648 *speed = mode_hdr->dev_spec & SMH_SA_SPEED_MASK;
2649
2650 if (params_to_get & SA_PARAM_COMPRESSION) {
2651 sa_comp_t *ntcs = (sa_comp_t *) &mode_blk[1];
2652 if (cpage == SA_DATA_COMPRESSION_PAGE) {
2653 struct scsi_data_compression_page *cp = &ntcs->dcomp;
2654 *comp_supported =
2655 (cp->dce_and_dcc & SA_DCP_DCC)? TRUE : FALSE;
2656 *comp_enabled =
2657 (cp->dce_and_dcc & SA_DCP_DCE)? TRUE : FALSE;
2658 *comp_algorithm = scsi_4btoul(cp->comp_algorithm);
2659 } else {
2660 struct scsi_dev_conf_page *cp = &ntcs->dconf;
2661 /*
2662 * We don't really know whether this device supports
2663 * Data Compression if the the algorithm field is
2664 * zero. Just say we do.
2665 */
2666 *comp_supported = TRUE;
2667 *comp_enabled =
2668 (cp->sel_comp_alg != SA_COMP_NONE)? TRUE : FALSE;
2669 *comp_algorithm = cp->sel_comp_alg;
2670 }
2671 if (tcs != NULL)
2672 bcopy(ntcs, tcs, sizeof (sa_comp_t));
2673 }
2674
2675 if (CAM_DEBUGGED(periph->path, CAM_DEBUG_INFO)) {
2676 int idx;
2677 char *xyz = mode_buffer;
2678 xpt_print_path(periph->path);
2679 printf("Mode Sense Data=");
2680 for (idx = 0; idx < mode_buffer_len; idx++)
2681 printf(" 0x%02x", xyz[idx] & 0xff);
2682 printf("\n");
2683 }
2684
2685sagetparamsexit:
2686
2687 xpt_release_ccb(ccb);
2688 free(mode_buffer, M_TEMP);
2689 return (error);
2690}
2691
2692/*
2693 * The purpose of this function is to set one of four different parameters
2694 * for a tape drive:
2695 * - blocksize
2696 * - density
2697 * - compression / compression algorithm
2698 * - buffering mode
2699 *
2700 * The assumption is that this will be called from saioctl(), and therefore
2701 * from a process context. Thus the waiting malloc calls below. If that
2702 * assumption ever changes, the malloc calls should be changed to be
2703 * NOWAIT mallocs.
2704 *
2705 * Any or all of the four parameters may be set when this function is
2706 * called. It should handle setting more than one parameter at once.
2707 */
2708static int
2709sasetparams(struct cam_periph *periph, sa_params params_to_set,
2710 u_int32_t blocksize, u_int8_t density, u_int32_t calg,
2711 u_int32_t sense_flags)
2712{
2713 struct sa_softc *softc;
2714 u_int32_t current_blocksize;
2715 u_int32_t current_calg;
2716 u_int8_t current_density;
2717 u_int8_t current_speed;
2718 int comp_enabled, comp_supported;
2719 void *mode_buffer;
2720 int mode_buffer_len;
2721 struct scsi_mode_header_6 *mode_hdr;
2722 struct scsi_mode_blk_desc *mode_blk;
2723 sa_comp_t *ccomp, *cpage;
2724 int buff_mode;
2725 union ccb *ccb = NULL;
2726 int error;
2727
2728 softc = (struct sa_softc *)periph->softc;
2729
2730 ccomp = malloc(sizeof (sa_comp_t), M_TEMP, M_WAITOK);
2731
2732 /*
2733 * Since it doesn't make sense to set the number of blocks, or
2734 * write protection, we won't try to get the current value. We
2735 * always want to get the blocksize, so we can set it back to the
2736 * proper value.
2737 */
2738 error = sagetparams(periph,
2739 params_to_set | SA_PARAM_BLOCKSIZE | SA_PARAM_SPEED,
2740 &current_blocksize, &current_density, NULL, &buff_mode, NULL,
2741 &current_speed, &comp_supported, &comp_enabled,
2742 &current_calg, ccomp);
2743
2744 if (error != 0) {
2745 free(ccomp, M_TEMP);
2746 return (error);
2747 }
2748
2749 mode_buffer_len = sizeof(*mode_hdr) + sizeof(*mode_blk);
2750 if (params_to_set & SA_PARAM_COMPRESSION)
2751 mode_buffer_len += sizeof (sa_comp_t);
2752
2753 mode_buffer = malloc(mode_buffer_len, M_TEMP, M_WAITOK);
2754 bzero(mode_buffer, mode_buffer_len);
2755
2756 mode_hdr = (struct scsi_mode_header_6 *)mode_buffer;
2757 mode_blk = (struct scsi_mode_blk_desc *)&mode_hdr[1];
2758
2759 ccb = cam_periph_getccb(periph, 1);
2760
2761retry:
2762
2763 if (params_to_set & SA_PARAM_COMPRESSION) {
2764 if (mode_blk) {
2765 cpage = (sa_comp_t *)&mode_blk[1];
2766 } else {
2767 cpage = (sa_comp_t *)&mode_hdr[1];
2768 }
2769 bcopy(ccomp, cpage, sizeof (sa_comp_t));
2770 cpage->hdr.pagecode &= ~0x80;
2771 } else
2772 cpage = NULL;
2773
2774 /*
2775 * If the caller wants us to set the blocksize, use the one they
2776 * pass in. Otherwise, use the blocksize we got back from the
2777 * mode select above.
2778 */
2779 if (mode_blk) {
2780 if (params_to_set & SA_PARAM_BLOCKSIZE)
2781 scsi_ulto3b(blocksize, mode_blk->blklen);
2782 else
2783 scsi_ulto3b(current_blocksize, mode_blk->blklen);
2784
2785 /*
2786 * Set density if requested, else preserve old density.
2787 * SCSI_SAME_DENSITY only applies to SCSI-2 or better
2788 * devices, else density we've latched up in our softc.
2789 */
2790 if (params_to_set & SA_PARAM_DENSITY) {
2791 mode_blk->density = density;
2792 } else if (softc->scsi_rev > SCSI_REV_CCS) {
2793 mode_blk->density = SCSI_SAME_DENSITY;
2794 } else {
2795 mode_blk->density = softc->media_density;
2796 }
2797 }
2798
2799 /*
2800 * For mode selects, these two fields must be zero.
2801 */
2802 mode_hdr->data_length = 0;
2803 mode_hdr->medium_type = 0;
2804
2805 /* set the speed to the current value */
2806 mode_hdr->dev_spec = current_speed;
2807
2808 /* set single-initiator buffering mode */
2809 mode_hdr->dev_spec |= SMH_SA_BUF_MODE_SIBUF;
2810
2811 if (mode_blk)
2812 mode_hdr->blk_desc_len = sizeof(struct scsi_mode_blk_desc);
2813 else
2814 mode_hdr->blk_desc_len = 0;
2815
2816 /*
2817 * First, if the user wants us to set the compression algorithm or
2818 * just turn compression on, check to make sure that this drive
2819 * supports compression.
2820 */
2821 if (params_to_set & SA_PARAM_COMPRESSION) {
2822 /*
2823 * If the compression algorithm is 0, disable compression.
2824 * If the compression algorithm is non-zero, enable
2825 * compression and set the compression type to the
2826 * specified compression algorithm, unless the algorithm is
2827 * MT_COMP_ENABLE. In that case, we look at the
2828 * compression algorithm that is currently set and if it is
2829 * non-zero, we leave it as-is. If it is zero, and we have
2830 * saved a compression algorithm from a time when
2831 * compression was enabled before, set the compression to
2832 * the saved value.
2833 */
2834 switch (ccomp->hdr.pagecode & ~0x80) {
2835 case SA_DATA_COMPRESSION_PAGE:
2836 if (ccomp->dcomp.dce_and_dcc & SA_DCP_DCC) {
2837 struct scsi_data_compression_page *dcp = &cpage->dcomp;
2838 if (calg == 0) {
2839 /*
2840 * Disable compression, but leave the
2841 * decompression and the capability bit
2842 * alone.
2843 */
2844 dcp->dce_and_dcc = SA_DCP_DCC;
2845 dcp->dde_and_red |= SA_DCP_DDE;
2846 break;
2847 }
2848 /* enable compression && decompression */
2849 dcp->dce_and_dcc = SA_DCP_DCE | SA_DCP_DCC;
2850 dcp->dde_and_red |= SA_DCP_DDE;
2851 /*
2852 * If there, use compression algorithm from caller.
2853 * Otherwise, if there's a saved compression algorithm
2854 * and there is no current algorithm, use the saved
2855 * algorithm. Else parrot back what we got and hope
2856 * for the best.
2857 */
2858 if (calg != MT_COMP_ENABLE) {
2859 scsi_ulto4b(calg, dcp->comp_algorithm);
2860 scsi_ulto4b(calg, dcp->decomp_algorithm);
2861 } else if (scsi_4btoul(dcp->comp_algorithm) == 0 &&
2862 softc->saved_comp_algorithm != 0) {
2863 scsi_ulto4b(softc->saved_comp_algorithm,
2864 dcp->comp_algorithm);
2865 scsi_ulto4b(softc->saved_comp_algorithm,
2866 dcp->decomp_algorithm);
2867 }
2868 break;
2869 }
2870 case SA_DEVICE_CONFIGURATION_PAGE:
2871 {
2872 struct scsi_dev_conf_page *dcp = &cpage->dconf;
2873 if (calg == 0) {
2874 dcp->sel_comp_alg = SA_COMP_NONE;
2875 break;
2876 }
2877 if (calg != MT_COMP_ENABLE) {
2878 dcp->sel_comp_alg = calg;
2879 } else if (dcp->sel_comp_alg == SA_COMP_NONE &&
2880 softc->saved_comp_algorithm != 0) {
2881 dcp->sel_comp_alg = softc->saved_comp_algorithm;
2882 }
2883 break;
2884 }
2885 default:
2886 /*
2887 * The drive doesn't seem to support compression,
2888 * so turn off the set compression bit.
2889 */
2890 params_to_set &= ~SA_PARAM_COMPRESSION;
2891 xpt_print_path(periph->path);
2892 printf("device does not seem to support compression\n");
2893
2894 /*
2895 * If that was the only thing the user wanted us to set,
2896 * clean up allocated resources and return with
2897 * 'operation not supported'.
2898 */
2899 if (params_to_set == SA_PARAM_NONE) {
2900 free(mode_buffer, M_TEMP);
2901 xpt_release_ccb(ccb);
2902 return (ENODEV);
2903 }
2904
2905 /*
2906 * That wasn't the only thing the user wanted us to set.
2907 * So, decrease the stated mode buffer length by the
2908 * size of the compression mode page.
2909 */
2910 mode_buffer_len -= sizeof(sa_comp_t);
2911 }
2912 }
2913
2914 /* It is safe to retry this operation */
2915 scsi_mode_select(&ccb->csio, 5, sadone, MSG_SIMPLE_Q_TAG,
2916 (params_to_set & SA_PARAM_COMPRESSION)? TRUE : FALSE,
2917 FALSE, mode_buffer, mode_buffer_len, SSD_FULL_SIZE, SCSIOP_TIMEOUT);
2918
2919 error = cam_periph_runccb(ccb, saerror, 0,
2920 sense_flags, &softc->device_stats);
2921 QFRLS(ccb);
2922
2923 if (CAM_DEBUGGED(periph->path, CAM_DEBUG_INFO)) {
2924 int idx;
2925 char *xyz = mode_buffer;
2926 xpt_print_path(periph->path);
2927 printf("Err%d, Mode Select Data=", error);
2928 for (idx = 0; idx < mode_buffer_len; idx++)
2929 printf(" 0x%02x", xyz[idx] & 0xff);
2930 printf("\n");
2931 }
2932
2933
2934 if (error) {
2935 /*
2936 * If we can, try without setting density/blocksize.
2937 */
2938 if (mode_blk) {
2939 if ((params_to_set &
2940 (SA_PARAM_DENSITY|SA_PARAM_BLOCKSIZE)) == 0) {
2941 mode_blk = NULL;
2942 goto retry;
2943 }
2944 } else {
2945 mode_blk = (struct scsi_mode_blk_desc *)&mode_hdr[1];
2946 cpage = (sa_comp_t *)&mode_blk[1];
2947 }
2948
2949 /*
2950 * If we were setting the blocksize, and that failed, we
2951 * want to set it to its original value. If we weren't
2952 * setting the blocksize, we don't want to change it.
2953 */
2954 scsi_ulto3b(current_blocksize, mode_blk->blklen);
2955
2956 /*
2957 * Set density if requested, else preserve old density.
2958 * SCSI_SAME_DENSITY only applies to SCSI-2 or better
2959 * devices, else density we've latched up in our softc.
2960 */
2961 if (params_to_set & SA_PARAM_DENSITY) {
2962 mode_blk->density = current_density;
2963 } else if (softc->scsi_rev > SCSI_REV_CCS) {
2964 mode_blk->density = SCSI_SAME_DENSITY;
2965 } else {
2966 mode_blk->density = softc->media_density;
2967 }
2968
2969 if (params_to_set & SA_PARAM_COMPRESSION)
2970 bcopy(ccomp, cpage, sizeof (sa_comp_t));
2971
2972 /*
2973 * The retry count is the only CCB field that might have been
2974 * changed that we care about, so reset it back to 1.
2975 */
2976 ccb->ccb_h.retry_count = 1;
2977 cam_periph_runccb(ccb, saerror, 0, sense_flags,
2978 &softc->device_stats);
2979 QFRLS(ccb);
2980 }
2981
2982 xpt_release_ccb(ccb);
2983
2984 if (ccomp != NULL)
2985 free(ccomp, M_TEMP);
2986
2987 if (params_to_set & SA_PARAM_COMPRESSION) {
2988 if (error) {
2989 softc->flags &= ~SA_FLAG_COMP_ENABLED;
2990 /*
2991 * Even if we get an error setting compression,
2992 * do not say that we don't support it. We could
2993 * have been wrong, or it may be media specific.
2994 * softc->flags &= ~SA_FLAG_COMP_SUPP;
2995 */
2996 softc->saved_comp_algorithm = softc->comp_algorithm;
2997 softc->comp_algorithm = 0;
2998 } else {
2999 softc->flags |= SA_FLAG_COMP_ENABLED;
3000 softc->comp_algorithm = calg;
3001 }
3002 }
3003
3004 free(mode_buffer, M_TEMP);
3005 return (error);
3006}
3007
3008static void
3009saprevent(struct cam_periph *periph, int action)
3010{
3011 struct sa_softc *softc;
3012 union ccb *ccb;
3013 int error, sf;
3014
3015 softc = (struct sa_softc *)periph->softc;
3016
3017 if ((action == PR_ALLOW) && (softc->flags & SA_FLAG_TAPE_LOCKED) == 0)
3018 return;
3019 if ((action == PR_PREVENT) && (softc->flags & SA_FLAG_TAPE_LOCKED) != 0)
3020 return;
3021
3022 /*
3023 * We can be quiet about illegal requests.
3024 */
3025 if (CAM_DEBUGGED(periph->path, CAM_DEBUG_INFO)) {
3026 sf = 0;
3027 } else
3028 sf = SF_QUIET_IR;
3029
3030 ccb = cam_periph_getccb(periph, 1);
3031
3032 /* It is safe to retry this operation */
3033 scsi_prevent(&ccb->csio, 5, sadone, MSG_SIMPLE_Q_TAG, action,
3034 SSD_FULL_SIZE, SCSIOP_TIMEOUT);
3035
3036 error = cam_periph_runccb(ccb, saerror, 0, sf, &softc->device_stats);
3037 QFRLS(ccb);
3038 if (error == 0) {
3039 if (action == PR_ALLOW)
3040 softc->flags &= ~SA_FLAG_TAPE_LOCKED;
3041 else
3042 softc->flags |= SA_FLAG_TAPE_LOCKED;
3043 }
3044
3045 xpt_release_ccb(ccb);
3046}
3047
3048static int
3049sarewind(struct cam_periph *periph)
3050{
3051 union ccb *ccb;
3052 struct sa_softc *softc;
3053 int error;
3054
3055 softc = (struct sa_softc *)periph->softc;
3056
3057 ccb = cam_periph_getccb(periph, 1);
3058
3059 /* It is safe to retry this operation */
3060 scsi_rewind(&ccb->csio, 2, sadone, MSG_SIMPLE_Q_TAG, FALSE,
3061 SSD_FULL_SIZE, REWIND_TIMEOUT);
3062
3063 softc->dsreg = MTIO_DSREG_REW;
3064 error = cam_periph_runccb(ccb, saerror, 0, 0, &softc->device_stats);
3065 softc->dsreg = MTIO_DSREG_REST;
3066
3067 if ((ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
3068 cam_release_devq(ccb->ccb_h.path, 0, 0, 0, FALSE);
3069
3070 xpt_release_ccb(ccb);
3071 if (error == 0)
3072 softc->fileno = softc->blkno = (daddr_t) 0;
3073 else
3074 softc->fileno = softc->blkno = (daddr_t) -1;
3075 return (error);
3076}
3077
3078static int
3079saspace(struct cam_periph *periph, int count, scsi_space_code code)
3080{
3081 union ccb *ccb;
3082 struct sa_softc *softc;
3083 int error;
3084
3085 softc = (struct sa_softc *)periph->softc;
3086
3087 ccb = cam_periph_getccb(periph, 1);
3088
3089 /* This cannot be retried */
3090
3091 scsi_space(&ccb->csio, 0, sadone, MSG_SIMPLE_Q_TAG, code, count,
3092 SSD_FULL_SIZE, SPACE_TIMEOUT);
3093
3094 /*
3095 * Clear residual because we will be using it.
3096 */
3097 softc->last_ctl_resid = 0;
3098
3099 softc->dsreg = (count < 0)? MTIO_DSREG_REV : MTIO_DSREG_FWD;
3100 error = cam_periph_runccb(ccb, saerror, 0, 0, &softc->device_stats);
3101 softc->dsreg = MTIO_DSREG_REST;
3102
3103 if ((ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
3104 cam_release_devq(ccb->ccb_h.path, 0, 0, 0, FALSE);
3105
3106 xpt_release_ccb(ccb);
3107
3108 /*
3109 * If a spacing operation has failed, we need to invalidate
3110 * this mount.
3111 *
3112 * If the spacing operation was setmarks or to end of recorded data,
3113 * we no longer know our relative position.
3114 *
3115 * If the spacing operations was spacing files in reverse, we
3116 * take account of the residual, but still check against less
3117 * than zero- if we've gone negative, we must have hit BOT.
3118 *
3119 * If the spacing operations was spacing records in reverse and
3120 * we have a residual, we've either hit BOT or hit a filemark.
3121 * In the former case, we know our new record number (0). In
3122 * the latter case, we have absolutely no idea what the real
3123 * record number is- we've stopped between the end of the last
3124 * record in the previous file and the filemark that stopped
3125 * our spacing backwards.
3126 */
3127 if (error) {
3128 softc->fileno = softc->blkno = (daddr_t) -1;
3129 } else if (code == SS_SETMARKS || code == SS_EOD) {
3130 softc->fileno = softc->blkno = (daddr_t) -1;
3131 } else if (code == SS_FILEMARKS && softc->fileno != (daddr_t) -1) {
3132 softc->fileno += (count - softc->last_ctl_resid);
3133 if (softc->fileno < 0) /* we must of hit BOT */
3134 softc->fileno = 0;
3135 softc->blkno = 0;
3136 } else if (code == SS_BLOCKS && softc->blkno != (daddr_t) -1) {
3137 softc->blkno += (count - softc->last_ctl_resid);
3138 if (count < 0) {
3139 if (softc->last_ctl_resid || softc->blkno < 0) {
3140 if (softc->fileno == 0) {
3141 softc->blkno = 0;
3142 } else {
3143 softc->blkno = (daddr_t) -1;
3144 }
3145 }
3146 }
3147 }
3148 return (error);
3149}
3150
3151static int
3152sawritefilemarks(struct cam_periph *periph, int nmarks, int setmarks)
3153{
3154 union ccb *ccb;
3155 struct sa_softc *softc;
3156 int error, nwm = 0;
3157
3158 softc = (struct sa_softc *)periph->softc;
3159
3160 ccb = cam_periph_getccb(periph, 1);
3161 /*
3162 * Clear residual because we will be using it.
3163 */
3164 softc->last_ctl_resid = 0;
3165
3166 softc->dsreg = MTIO_DSREG_FMK;
3167 /* this *must* not be retried */
3168 scsi_write_filemarks(&ccb->csio, 0, sadone, MSG_SIMPLE_Q_TAG,
3169 FALSE, setmarks, nmarks, SSD_FULL_SIZE, IO_TIMEOUT);
3170 softc->dsreg = MTIO_DSREG_REST;
3171
3172
3173 error = cam_periph_runccb(ccb, saerror, 0, 0, &softc->device_stats);
3174
3175 if ((ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
3176 cam_release_devq(ccb->ccb_h.path, 0, 0, 0, FALSE);
3177
3178 if (error == 0 && nmarks) {
3179 struct sa_softc *softc = (struct sa_softc *)periph->softc;
3180 nwm = nmarks - softc->last_ctl_resid;
3181 softc->filemarks += nwm;
3182 }
3183
3184 xpt_release_ccb(ccb);
3185
3186 /*
3187 * Update relative positions (if we're doing that).
3188 */
3189 if (error) {
3190 softc->fileno = softc->blkno = (daddr_t) -1;
3191 } else if (softc->fileno != (daddr_t) -1) {
3192 softc->fileno += nwm;
3193 softc->blkno = 0;
3194 }
3195 return (error);
3196}
3197
3198static int
3199sardpos(struct cam_periph *periph, int hard, u_int32_t *blkptr)
3200{
3201 struct scsi_tape_position_data loc;
3202 union ccb *ccb;
3203 struct sa_softc *softc = (struct sa_softc *)periph->softc;
3204 int error;
3205
3206 /*
3207 * We try and flush any buffered writes here if we were writing
3208 * and we're trying to get hardware block position. It eats
3209 * up performance substantially, but I'm wary of drive firmware.
3210 *
3211 * I think that *logical* block position is probably okay-
3212 * but hardware block position might have to wait for data
3213 * to hit media to be valid. Caveat Emptor.
3214 */
3215
3216 if (hard && (softc->flags & SA_FLAG_TAPE_WRITTEN)) {
3217 error = sawritefilemarks(periph, 0, 0);
3218 if (error && error != EACCES)
3219 return (error);
3220 }
3221
3222 ccb = cam_periph_getccb(periph, 1);
3223 scsi_read_position(&ccb->csio, 1, sadone, MSG_SIMPLE_Q_TAG,
3224 hard, &loc, SSD_FULL_SIZE, SCSIOP_TIMEOUT);
3225 softc->dsreg = MTIO_DSREG_RBSY;
3226 error = cam_periph_runccb(ccb, saerror, 0, 0, &softc->device_stats);
3227 softc->dsreg = MTIO_DSREG_REST;
3228 if ((ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
3229 cam_release_devq(ccb->ccb_h.path, 0, 0, 0, 0);
3230
3231 if (error == 0) {
3232 if (loc.flags & SA_RPOS_UNCERTAIN) {
3233 error = EINVAL; /* nothing is certain */
3234 } else {
3235 *blkptr = scsi_4btoul(loc.firstblk);
3236 }
3237 }
3238
3239 xpt_release_ccb(ccb);
3240 return (error);
3241}
3242
3243static int
3244sasetpos(struct cam_periph *periph, int hard, u_int32_t *blkptr)
3245{
3246 union ccb *ccb;
3247 struct sa_softc *softc;
3248 int error;
3249
3250 /*
3251 * We used to try and flush any buffered writes here.
3252 * Now we push this onto user applications to either
3253 * flush the pending writes themselves (via a zero count
3254 * WRITE FILEMARKS command) or they can trust their tape
3255 * drive to do this correctly for them.
3256 */
3257
3258 softc = (struct sa_softc *)periph->softc;
3259 ccb = cam_periph_getccb(periph, 1);
3260
3261
3262 scsi_set_position(&ccb->csio, 1, sadone, MSG_SIMPLE_Q_TAG,
3263 hard, *blkptr, SSD_FULL_SIZE, SPACE_TIMEOUT);
3264
3265
3266 softc->dsreg = MTIO_DSREG_POS;
3267 error = cam_periph_runccb(ccb, saerror, 0, 0, &softc->device_stats);
3268 softc->dsreg = MTIO_DSREG_REST;
3269 if ((ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
3270 cam_release_devq(ccb->ccb_h.path, 0, 0, 0, 0);
3271 xpt_release_ccb(ccb);
3272 /*
3273 * Note relative file && block number position as now unknown.
3274 */
3275 softc->fileno = softc->blkno = (daddr_t) -1;
3276 return (error);
3277}
3278
3279static int
3280saretension(struct cam_periph *periph)
3281{
3282 union ccb *ccb;
3283 struct sa_softc *softc;
3284 int error;
3285
3286 softc = (struct sa_softc *)periph->softc;
3287
3288 ccb = cam_periph_getccb(periph, 1);
3289
3290 /* It is safe to retry this operation */
3291 scsi_load_unload(&ccb->csio, 5, sadone, MSG_SIMPLE_Q_TAG, FALSE,
3292 FALSE, TRUE, TRUE, SSD_FULL_SIZE, ERASE_TIMEOUT);
3293
3294 softc->dsreg = MTIO_DSREG_TEN;
3295 error = cam_periph_runccb(ccb, saerror, 0, 0, &softc->device_stats);
3296 softc->dsreg = MTIO_DSREG_REST;
3297
3298 if ((ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
3299 cam_release_devq(ccb->ccb_h.path, 0, 0, 0, FALSE);
3300 xpt_release_ccb(ccb);
3301 if (error == 0)
3302 softc->fileno = softc->blkno = (daddr_t) 0;
3303 else
3304 softc->fileno = softc->blkno = (daddr_t) -1;
3305 return (error);
3306}
3307
3308static int
3309sareservereleaseunit(struct cam_periph *periph, int reserve)
3310{
3311 union ccb *ccb;
3312 struct sa_softc *softc;
3313 int error;
3314
3315 softc = (struct sa_softc *)periph->softc;
3316 ccb = cam_periph_getccb(periph, 1);
3317
3318 /* It is safe to retry this operation */
3319 scsi_reserve_release_unit(&ccb->csio, 2, sadone, MSG_SIMPLE_Q_TAG,
3320 FALSE, 0, SSD_FULL_SIZE, SCSIOP_TIMEOUT, reserve);
3321 softc->dsreg = MTIO_DSREG_RBSY;
3322 error = cam_periph_runccb(ccb, saerror, 0,
3323 SF_RETRY_UA | SF_NO_PRINT, &softc->device_stats);
3324 softc->dsreg = MTIO_DSREG_REST;
3325 QFRLS(ccb);
3326 xpt_release_ccb(ccb);
3327
3328 /*
3329 * If the error was Illegal Request, then the device doesn't support
3330 * RESERVE/RELEASE. This is not an error.
3331 */
3332 if (error == EINVAL) {
3333 error = 0;
3334 }
3335
3336 return (error);
3337}
3338
3339static int
3340saloadunload(struct cam_periph *periph, int load)
3341{
3342 union ccb *ccb;
3343 struct sa_softc *softc;
3344 int error;
3345
3346 softc = (struct sa_softc *)periph->softc;
3347
3348 ccb = cam_periph_getccb(periph, 1);
3349
3350 /* It is safe to retry this operation */
3351 scsi_load_unload(&ccb->csio, 5, sadone, MSG_SIMPLE_Q_TAG, FALSE,
3352 FALSE, FALSE, load, SSD_FULL_SIZE, REWIND_TIMEOUT);
3353
3354 softc->dsreg = (load)? MTIO_DSREG_LD : MTIO_DSREG_UNL;
3355 error = cam_periph_runccb(ccb, saerror, 0, 0, &softc->device_stats);
3356 softc->dsreg = MTIO_DSREG_REST;
3357 QFRLS(ccb);
3358 xpt_release_ccb(ccb);
3359
3360 if (error || load == 0)
3361 softc->fileno = softc->blkno = (daddr_t) -1;
3362 else if (error == 0)
3363 softc->fileno = softc->blkno = (daddr_t) 0;
3364 return (error);
3365}
3366
3367static int
3368saerase(struct cam_periph *periph, int longerase)
3369{
3370
3371 union ccb *ccb;
3372 struct sa_softc *softc;
3373 int error;
3374
3375 softc = (struct sa_softc *)periph->softc;
3376
3377 ccb = cam_periph_getccb(periph, 1);
3378
3379 scsi_erase(&ccb->csio, 1, sadone, MSG_SIMPLE_Q_TAG, FALSE, longerase,
3380 SSD_FULL_SIZE, ERASE_TIMEOUT);
3381
3382 softc->dsreg = MTIO_DSREG_ZER;
3383 error = cam_periph_runccb(ccb, saerror, 0, 0, &softc->device_stats);
3384 softc->dsreg = MTIO_DSREG_REST;
3385
3386 if ((ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
3387 cam_release_devq(ccb->ccb_h.path, 0, 0, 0, FALSE);
3388 xpt_release_ccb(ccb);
3389 return (error);
3390}
3391
3392#endif /* _KERNEL */
3393
3394/*
3395 * Read tape block limits command.
3396 */
3397void
3398scsi_read_block_limits(struct ccb_scsiio *csio, u_int32_t retries,
3399 void (*cbfcnp)(struct cam_periph *, union ccb *),
3400 u_int8_t tag_action,
3401 struct scsi_read_block_limits_data *rlimit_buf,
3402 u_int8_t sense_len, u_int32_t timeout)
3403{
3404 struct scsi_read_block_limits *scsi_cmd;
3405
3406 cam_fill_csio(csio, retries, cbfcnp, CAM_DIR_IN, tag_action,
3407 (u_int8_t *)rlimit_buf, sizeof(*rlimit_buf), sense_len,
3408 sizeof(*scsi_cmd), timeout);
3409
3410 scsi_cmd = (struct scsi_read_block_limits *)&csio->cdb_io.cdb_bytes;
3411 bzero(scsi_cmd, sizeof(*scsi_cmd));
3412 scsi_cmd->opcode = READ_BLOCK_LIMITS;
3413}
3414
3415void
3416scsi_sa_read_write(struct ccb_scsiio *csio, u_int32_t retries,
3417 void (*cbfcnp)(struct cam_periph *, union ccb *),
3418 u_int8_t tag_action, int readop, int sli,
3419 int fixed, u_int32_t length, u_int8_t *data_ptr,
3420 u_int32_t dxfer_len, u_int8_t sense_len, u_int32_t timeout)
3421{
3422 struct scsi_sa_rw *scsi_cmd;
3423
3424 scsi_cmd = (struct scsi_sa_rw *)&csio->cdb_io.cdb_bytes;
3425 scsi_cmd->opcode = readop ? SA_READ : SA_WRITE;
3426 scsi_cmd->sli_fixed = 0;
3427 if (sli && readop)
3428 scsi_cmd->sli_fixed |= SAR_SLI;
3429 if (fixed)
3430 scsi_cmd->sli_fixed |= SARW_FIXED;
3431 scsi_ulto3b(length, scsi_cmd->length);
3432 scsi_cmd->control = 0;
3433
3434 cam_fill_csio(csio, retries, cbfcnp, readop ? CAM_DIR_IN : CAM_DIR_OUT,
3435 tag_action, data_ptr, dxfer_len, sense_len,
3436 sizeof(*scsi_cmd), timeout);
3437}
3438
3439void
3440scsi_load_unload(struct ccb_scsiio *csio, u_int32_t retries,
3441 void (*cbfcnp)(struct cam_periph *, union ccb *),
3442 u_int8_t tag_action, int immediate, int eot,
3443 int reten, int load, u_int8_t sense_len,
3444 u_int32_t timeout)
3445{
3446 struct scsi_load_unload *scsi_cmd;
3447
3448 scsi_cmd = (struct scsi_load_unload *)&csio->cdb_io.cdb_bytes;
3449 bzero(scsi_cmd, sizeof(*scsi_cmd));
3450 scsi_cmd->opcode = LOAD_UNLOAD;
3451 if (immediate)
3452 scsi_cmd->immediate = SLU_IMMED;
3453 if (eot)
3454 scsi_cmd->eot_reten_load |= SLU_EOT;
3455 if (reten)
3456 scsi_cmd->eot_reten_load |= SLU_RETEN;
3457 if (load)
3458 scsi_cmd->eot_reten_load |= SLU_LOAD;
3459
3460 cam_fill_csio(csio, retries, cbfcnp, CAM_DIR_NONE, tag_action,
3461 NULL, 0, sense_len, sizeof(*scsi_cmd), timeout);
3462}
3463
3464void
3465scsi_rewind(struct ccb_scsiio *csio, u_int32_t retries,
3466 void (*cbfcnp)(struct cam_periph *, union ccb *),
3467 u_int8_t tag_action, int immediate, u_int8_t sense_len,
3468 u_int32_t timeout)
3469{
3470 struct scsi_rewind *scsi_cmd;
3471
3472 scsi_cmd = (struct scsi_rewind *)&csio->cdb_io.cdb_bytes;
3473 bzero(scsi_cmd, sizeof(*scsi_cmd));
3474 scsi_cmd->opcode = REWIND;
3475 if (immediate)
3476 scsi_cmd->immediate = SREW_IMMED;
3477
3478 cam_fill_csio(csio, retries, cbfcnp, CAM_DIR_NONE, tag_action, NULL,
3479 0, sense_len, sizeof(*scsi_cmd), timeout);
3480}
3481
3482void
3483scsi_space(struct ccb_scsiio *csio, u_int32_t retries,
3484 void (*cbfcnp)(struct cam_periph *, union ccb *),
3485 u_int8_t tag_action, scsi_space_code code,
3486 u_int32_t count, u_int8_t sense_len, u_int32_t timeout)
3487{
3488 struct scsi_space *scsi_cmd;
3489
3490 scsi_cmd = (struct scsi_space *)&csio->cdb_io.cdb_bytes;
3491 scsi_cmd->opcode = SPACE;
3492 scsi_cmd->code = code;
3493 scsi_ulto3b(count, scsi_cmd->count);
3494 scsi_cmd->control = 0;
3495
3496 cam_fill_csio(csio, retries, cbfcnp, CAM_DIR_NONE, tag_action, NULL,
3497 0, sense_len, sizeof(*scsi_cmd), timeout);
3498}
3499
3500void
3501scsi_write_filemarks(struct ccb_scsiio *csio, u_int32_t retries,
3502 void (*cbfcnp)(struct cam_periph *, union ccb *),
3503 u_int8_t tag_action, int immediate, int setmark,
3504 u_int32_t num_marks, u_int8_t sense_len,
3505 u_int32_t timeout)
3506{
3507 struct scsi_write_filemarks *scsi_cmd;
3508
3509 scsi_cmd = (struct scsi_write_filemarks *)&csio->cdb_io.cdb_bytes;
3510 bzero(scsi_cmd, sizeof(*scsi_cmd));
3511 scsi_cmd->opcode = WRITE_FILEMARKS;
3512 if (immediate)
3513 scsi_cmd->byte2 |= SWFMRK_IMMED;
3514 if (setmark)
3515 scsi_cmd->byte2 |= SWFMRK_WSMK;
3516
3517 scsi_ulto3b(num_marks, scsi_cmd->num_marks);
3518
3519 cam_fill_csio(csio, retries, cbfcnp, CAM_DIR_NONE, tag_action, NULL,
3520 0, sense_len, sizeof(*scsi_cmd), timeout);
3521}
3522
3523/*
3524 * The reserve and release unit commands differ only by their opcodes.
3525 */
3526void
3527scsi_reserve_release_unit(struct ccb_scsiio *csio, u_int32_t retries,
3528 void (*cbfcnp)(struct cam_periph *, union ccb *),
3529 u_int8_t tag_action, int third_party,
3530 int third_party_id, u_int8_t sense_len,
3531 u_int32_t timeout, int reserve)
3532{
3533 struct scsi_reserve_release_unit *scsi_cmd;
3534
3535 scsi_cmd = (struct scsi_reserve_release_unit *)&csio->cdb_io.cdb_bytes;
3536 bzero(scsi_cmd, sizeof(*scsi_cmd));
3537
3538 if (reserve)
3539 scsi_cmd->opcode = RESERVE_UNIT;
3540 else
3541 scsi_cmd->opcode = RELEASE_UNIT;
3542
3543 if (third_party) {
3544 scsi_cmd->lun_thirdparty |= SRRU_3RD_PARTY;
3545 scsi_cmd->lun_thirdparty |=
3546 ((third_party_id << SRRU_3RD_SHAMT) & SRRU_3RD_MASK);
3547 }
3548
3549 cam_fill_csio(csio, retries, cbfcnp, CAM_DIR_NONE, tag_action, NULL,
3550 0, sense_len, sizeof(*scsi_cmd), timeout);
3551}
3552
3553void
3554scsi_erase(struct ccb_scsiio *csio, u_int32_t retries,
3555 void (*cbfcnp)(struct cam_periph *, union ccb *),
3556 u_int8_t tag_action, int immediate, int long_erase,
3557 u_int8_t sense_len, u_int32_t timeout)
3558{
3559 struct scsi_erase *scsi_cmd;
3560
3561 scsi_cmd = (struct scsi_erase *)&csio->cdb_io.cdb_bytes;
3562 bzero(scsi_cmd, sizeof(*scsi_cmd));
3563
3564 scsi_cmd->opcode = ERASE;
3565
3566 if (immediate)
3567 scsi_cmd->lun_imm_long |= SE_IMMED;
3568
3569 if (long_erase)
3570 scsi_cmd->lun_imm_long |= SE_LONG;
3571
3572 cam_fill_csio(csio, retries, cbfcnp, CAM_DIR_NONE, tag_action, NULL,
3573 0, sense_len, sizeof(*scsi_cmd), timeout);
3574}
3575
3576/*
3577 * Read Tape Position command.
3578 */
3579void
3580scsi_read_position(struct ccb_scsiio *csio, u_int32_t retries,
3581 void (*cbfcnp)(struct cam_periph *, union ccb *),
3582 u_int8_t tag_action, int hardsoft,
3583 struct scsi_tape_position_data *sbp,
3584 u_int8_t sense_len, u_int32_t timeout)
3585{
3586 struct scsi_tape_read_position *scmd;
3587
3588 cam_fill_csio(csio, retries, cbfcnp, CAM_DIR_IN, tag_action,
3589 (u_int8_t *)sbp, sizeof (*sbp), sense_len, sizeof(*scmd), timeout);
3590 scmd = (struct scsi_tape_read_position *)&csio->cdb_io.cdb_bytes;
3591 bzero(scmd, sizeof(*scmd));
3592 scmd->opcode = READ_POSITION;
3593 scmd->byte1 = hardsoft;
3594}
3595
3596/*
3597 * Set Tape Position command.
3598 */
3599void
3600scsi_set_position(struct ccb_scsiio *csio, u_int32_t retries,
3601 void (*cbfcnp)(struct cam_periph *, union ccb *),
3602 u_int8_t tag_action, int hardsoft, u_int32_t blkno,
3603 u_int8_t sense_len, u_int32_t timeout)
3604{
3605 struct scsi_tape_locate *scmd;
3606
3607 cam_fill_csio(csio, retries, cbfcnp, CAM_DIR_NONE, tag_action,
3608 (u_int8_t *)NULL, 0, sense_len, sizeof(*scmd), timeout);
3609 scmd = (struct scsi_tape_locate *)&csio->cdb_io.cdb_bytes;
3610 bzero(scmd, sizeof(*scmd));
3611 scmd->opcode = LOCATE;
3612 if (hardsoft)
3613 scmd->byte1 |= SA_SPOS_BT;
3614 scsi_ulto4b(blkno, scmd->blkaddr);
3615}