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