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