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