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