1 /*-
2 * Implementation of SCSI Sequential Access Peripheral driver for CAM.
3 *
4 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
5 *
6 * Copyright (c) 1999, 2000 Matthew Jacob
7 * Copyright (c) 2013, 2014, 2015, 2021 Spectra Logic Corporation
8 * All rights reserved.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions, and the following disclaimer,
15 * without modification, immediately at the beginning of the file.
16 * 2. The name of the author may not be used to endorse or promote products
17 * derived from this software without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
23 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
30 */
31
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD: stable/12/sys/cam/scsi/scsi_sa.c 371661 2022-02-18 21:29:35Z ken $");
34
35 #include <sys/param.h>
36 #include <sys/queue.h>
37 #ifdef _KERNEL
38 #include <sys/systm.h>
39 #include <sys/kernel.h>
40 #endif
41 #include <sys/types.h>
42 #include <sys/time.h>
43 #include <sys/bio.h>
44 #include <sys/limits.h>
45 #include <sys/malloc.h>
46 #include <sys/mtio.h>
47 #ifdef _KERNEL
48 #include <sys/conf.h>
49 #include <sys/sbuf.h>
50 #include <sys/sysctl.h>
51 #include <sys/taskqueue.h>
52 #endif
53 #include <sys/fcntl.h>
54 #include <sys/devicestat.h>
55
56 #ifndef _KERNEL
57 #include <stdio.h>
58 #include <string.h>
59 #endif
60
61 #include <cam/cam.h>
62 #include <cam/cam_ccb.h>
63 #include <cam/cam_periph.h>
64 #include <cam/cam_xpt_periph.h>
65 #include <cam/cam_debug.h>
66
67 #include <cam/scsi/scsi_all.h>
68 #include <cam/scsi/scsi_message.h>
69 #include <cam/scsi/scsi_sa.h>
70
71 #ifdef _KERNEL
72
73 #include "opt_sa.h"
74
75 #ifndef SA_IO_TIMEOUT
76 #define SA_IO_TIMEOUT 32
77 #endif
78 #ifndef SA_SPACE_TIMEOUT
79 #define SA_SPACE_TIMEOUT 1 * 60
80 #endif
81 #ifndef SA_REWIND_TIMEOUT
82 #define SA_REWIND_TIMEOUT 2 * 60
83 #endif
84 #ifndef SA_ERASE_TIMEOUT
85 #define SA_ERASE_TIMEOUT 4 * 60
86 #endif
87 #ifndef SA_REP_DENSITY_TIMEOUT
88 #define SA_REP_DENSITY_TIMEOUT 1
89 #endif
90
91 #define SCSIOP_TIMEOUT (60 * 1000) /* not an option */
92
93 #define IO_TIMEOUT (SA_IO_TIMEOUT * 60 * 1000)
94 #define REWIND_TIMEOUT (SA_REWIND_TIMEOUT * 60 * 1000)
95 #define ERASE_TIMEOUT (SA_ERASE_TIMEOUT * 60 * 1000)
96 #define SPACE_TIMEOUT (SA_SPACE_TIMEOUT * 60 * 1000)
97 #define REP_DENSITY_TIMEOUT (SA_REP_DENSITY_TIMEOUT * 60 * 1000)
98
99 /*
100 * Additional options that can be set for config: SA_1FM_AT_EOT
101 */
102
103 #ifndef UNUSED_PARAMETER
104 #define UNUSED_PARAMETER(x) x = x
105 #endif
106
107 #define QFRLS(ccb) \
108 if (((ccb)->ccb_h.status & CAM_DEV_QFRZN) != 0) \
109 cam_release_devq((ccb)->ccb_h.path, 0, 0, 0, FALSE)
110
111 /*
112 * Driver states
113 */
114
115 static MALLOC_DEFINE(M_SCSISA, "SCSI sa", "SCSI sequential access buffers");
116
117 typedef enum {
118 SA_STATE_NORMAL, SA_STATE_PROBE, SA_STATE_ABNORMAL
119 } sa_state;
120
121 #define ccb_pflags ppriv_field0
122 #define ccb_bp ppriv_ptr1
123
124 /* bits in ccb_pflags */
125 #define SA_POSITION_UPDATED 0x1
126
127
128 typedef enum {
129 SA_FLAG_OPEN = 0x00001,
130 SA_FLAG_FIXED = 0x00002,
131 SA_FLAG_TAPE_LOCKED = 0x00004,
132 SA_FLAG_TAPE_MOUNTED = 0x00008,
133 SA_FLAG_TAPE_WP = 0x00010,
134 SA_FLAG_TAPE_WRITTEN = 0x00020,
135 SA_FLAG_EOM_PENDING = 0x00040,
136 SA_FLAG_EIO_PENDING = 0x00080,
137 SA_FLAG_EOF_PENDING = 0x00100,
138 SA_FLAG_ERR_PENDING = (SA_FLAG_EOM_PENDING|SA_FLAG_EIO_PENDING|
139 SA_FLAG_EOF_PENDING),
140 SA_FLAG_INVALID = 0x00200,
141 SA_FLAG_COMP_ENABLED = 0x00400,
142 SA_FLAG_COMP_SUPP = 0x00800,
143 SA_FLAG_COMP_UNSUPP = 0x01000,
144 SA_FLAG_TAPE_FROZEN = 0x02000,
145 SA_FLAG_PROTECT_SUPP = 0x04000,
146
147 SA_FLAG_COMPRESSION = (SA_FLAG_COMP_SUPP|SA_FLAG_COMP_ENABLED|
148 SA_FLAG_COMP_UNSUPP),
149 SA_FLAG_SCTX_INIT = 0x08000,
150 SA_FLAG_RSOC_TO_TRY = 0x10000,
151 } sa_flags;
152
153 typedef enum {
154 SA_MODE_REWIND = 0x00,
155 SA_MODE_NOREWIND = 0x01,
156 SA_MODE_OFFLINE = 0x02
157 } sa_mode;
158
159 typedef enum {
160 SA_TIMEOUT_ERASE,
161 SA_TIMEOUT_LOAD,
162 SA_TIMEOUT_LOCATE,
163 SA_TIMEOUT_MODE_SELECT,
164 SA_TIMEOUT_MODE_SENSE,
165 SA_TIMEOUT_PREVENT,
166 SA_TIMEOUT_READ,
167 SA_TIMEOUT_READ_BLOCK_LIMITS,
168 SA_TIMEOUT_READ_POSITION,
169 SA_TIMEOUT_REP_DENSITY,
170 SA_TIMEOUT_RESERVE,
171 SA_TIMEOUT_REWIND,
172 SA_TIMEOUT_SPACE,
173 SA_TIMEOUT_TUR,
174 SA_TIMEOUT_WRITE,
175 SA_TIMEOUT_WRITE_FILEMARKS,
176 SA_TIMEOUT_TYPE_MAX
177 } sa_timeout_types;
178
179 /*
180 * These are the default timeout values that apply to all tape drives.
181 *
182 * We get timeouts from the following places in order of increasing
183 * priority:
184 * 1. Driver default timeouts. (Set in the structure below.)
185 * 2. Timeouts loaded from the drive via REPORT SUPPORTED OPERATION
186 * CODES. (If the drive supports it, SPC-4/LTO-5 and newer should.)
187 * 3. Global loader tunables, used for all sa(4) driver instances on
188 * a machine.
189 * 4. Instance-specific loader tunables, used for say sa5.
190 * 5. On the fly user sysctl changes.
191 *
192 * Each step will overwrite the timeout value set from the one
193 * before, so you go from general to most specific.
194 */
195 static struct sa_timeout_desc {
196 const char *desc;
197 int value;
198 } sa_default_timeouts[SA_TIMEOUT_TYPE_MAX] = {
199 {"erase", ERASE_TIMEOUT},
200 {"load", REWIND_TIMEOUT},
201 {"locate", SPACE_TIMEOUT},
202 {"mode_select", SCSIOP_TIMEOUT},
203 {"mode_sense", SCSIOP_TIMEOUT},
204 {"prevent", SCSIOP_TIMEOUT},
205 {"read", IO_TIMEOUT},
206 {"read_block_limits", SCSIOP_TIMEOUT},
207 {"read_position", SCSIOP_TIMEOUT},
208 {"report_density", REP_DENSITY_TIMEOUT},
209 {"reserve", SCSIOP_TIMEOUT},
210 {"rewind", REWIND_TIMEOUT},
211 {"space", SPACE_TIMEOUT},
212 {"tur", SCSIOP_TIMEOUT},
213 {"write", IO_TIMEOUT},
214 {"write_filemarks", IO_TIMEOUT},
215 };
216
217 typedef enum {
218 SA_PARAM_NONE = 0x000,
219 SA_PARAM_BLOCKSIZE = 0x001,
220 SA_PARAM_DENSITY = 0x002,
221 SA_PARAM_COMPRESSION = 0x004,
222 SA_PARAM_BUFF_MODE = 0x008,
223 SA_PARAM_NUMBLOCKS = 0x010,
224 SA_PARAM_WP = 0x020,
225 SA_PARAM_SPEED = 0x040,
226 SA_PARAM_DENSITY_EXT = 0x080,
227 SA_PARAM_LBP = 0x100,
228 SA_PARAM_ALL = 0x1ff
229 } sa_params;
230
231 typedef enum {
232 SA_QUIRK_NONE = 0x000,
233 SA_QUIRK_NOCOMP = 0x001, /* Can't deal with compression at all*/
234 SA_QUIRK_FIXED = 0x002, /* Force fixed mode */
235 SA_QUIRK_VARIABLE = 0x004, /* Force variable mode */
236 SA_QUIRK_2FM = 0x008, /* Needs Two File Marks at EOD */
237 SA_QUIRK_1FM = 0x010, /* No more than 1 File Mark at EOD */
238 SA_QUIRK_NODREAD = 0x020, /* Don't try and dummy read density */
239 SA_QUIRK_NO_MODESEL = 0x040, /* Don't do mode select at all */
240 SA_QUIRK_NO_CPAGE = 0x080, /* Don't use DEVICE COMPRESSION page */
241 SA_QUIRK_NO_LONG_POS = 0x100 /* No long position information */
242 } sa_quirks;
243
244 #define SA_QUIRK_BIT_STRING \
245 "\020" \
246 "\001NOCOMP" \
247 "\002FIXED" \
248 "\003VARIABLE" \
249 "\0042FM" \
250 "\0051FM" \
251 "\006NODREAD" \
252 "\007NO_MODESEL" \
253 "\010NO_CPAGE" \
254 "\011NO_LONG_POS"
255
256 #define SAMODE(z) (dev2unit(z) & 0x3)
257 #define SA_IS_CTRL(z) (dev2unit(z) & (1 << 4))
258
259 #define SA_NOT_CTLDEV 0
260 #define SA_CTLDEV 1
261
262 #define SA_ATYPE_R 0
263 #define SA_ATYPE_NR 1
264 #define SA_ATYPE_ER 2
265 #define SA_NUM_ATYPES 3
266
267 #define SAMINOR(ctl, access) \
268 ((ctl << 4) | (access & 0x3))
269
270 struct sa_devs {
271 struct cdev *ctl_dev;
272 struct cdev *r_dev;
273 struct cdev *nr_dev;
274 struct cdev *er_dev;
275 };
276
277 #define SASBADDBASE(sb, indent, data, xfmt, name, type, xsize, desc) \
278 sbuf_printf(sb, "%*s<%s type=\"%s\" size=\"%zd\" " \
279 "fmt=\"%s\" desc=\"%s\">" #xfmt "</%s>\n", indent, "", \
280 #name, #type, xsize, #xfmt, desc ? desc : "", data, #name);
281
282 #define SASBADDINT(sb, indent, data, fmt, name) \
283 SASBADDBASE(sb, indent, data, fmt, name, int, sizeof(data), \
284 NULL)
285
286 #define SASBADDINTDESC(sb, indent, data, fmt, name, desc) \
287 SASBADDBASE(sb, indent, data, fmt, name, int, sizeof(data), \
288 desc)
289
290 #define SASBADDUINT(sb, indent, data, fmt, name) \
291 SASBADDBASE(sb, indent, data, fmt, name, uint, sizeof(data), \
292 NULL)
293
294 #define SASBADDUINTDESC(sb, indent, data, fmt, name, desc) \
295 SASBADDBASE(sb, indent, data, fmt, name, uint, sizeof(data), \
296 desc)
297
298 #define SASBADDFIXEDSTR(sb, indent, data, fmt, name) \
299 SASBADDBASE(sb, indent, data, fmt, name, str, sizeof(data), \
300 NULL)
301
302 #define SASBADDFIXEDSTRDESC(sb, indent, data, fmt, name, desc) \
303 SASBADDBASE(sb, indent, data, fmt, name, str, sizeof(data), \
304 desc)
305
306 #define SASBADDVARSTR(sb, indent, data, fmt, name, maxlen) \
307 SASBADDBASE(sb, indent, data, fmt, name, str, maxlen, NULL)
308
309 #define SASBADDVARSTRDESC(sb, indent, data, fmt, name, maxlen, desc) \
310 SASBADDBASE(sb, indent, data, fmt, name, str, maxlen, desc)
311
312 #define SASBADDNODE(sb, indent, name) { \
313 sbuf_printf(sb, "%*s<%s type=\"%s\">\n", indent, "", #name, \
314 "node"); \
315 indent += 2; \
316 }
317
318 #define SASBADDNODENUM(sb, indent, name, num) { \
319 sbuf_printf(sb, "%*s<%s type=\"%s\" num=\"%d\">\n", indent, "", \
320 #name, "node", num); \
321 indent += 2; \
322 }
323
324 #define SASBENDNODE(sb, indent, name) { \
325 indent -= 2; \
326 sbuf_printf(sb, "%*s</%s>\n", indent, "", #name); \
327 }
328
329 #define SA_DENSITY_TYPES 4
330
331 struct sa_prot_state {
332 int initialized;
333 uint32_t prot_method;
334 uint32_t pi_length;
335 uint32_t lbp_w;
336 uint32_t lbp_r;
337 uint32_t rbdp;
338 };
339
340 struct sa_prot_info {
341 struct sa_prot_state cur_prot_state;
342 struct sa_prot_state pending_prot_state;
343 };
344
345 /*
346 * A table mapping protection parameters to their types and values.
347 */
348 struct sa_prot_map {
349 char *name;
350 mt_param_set_type param_type;
351 off_t offset;
352 uint32_t min_val;
353 uint32_t max_val;
354 uint32_t *value;
355 } sa_prot_table[] = {
356 { "prot_method", MT_PARAM_SET_UNSIGNED,
357 __offsetof(struct sa_prot_state, prot_method),
358 /*min_val*/ 0, /*max_val*/ 255, NULL },
359 { "pi_length", MT_PARAM_SET_UNSIGNED,
360 __offsetof(struct sa_prot_state, pi_length),
361 /*min_val*/ 0, /*max_val*/ SA_CTRL_DP_PI_LENGTH_MASK, NULL },
362 { "lbp_w", MT_PARAM_SET_UNSIGNED,
363 __offsetof(struct sa_prot_state, lbp_w),
364 /*min_val*/ 0, /*max_val*/ 1, NULL },
365 { "lbp_r", MT_PARAM_SET_UNSIGNED,
366 __offsetof(struct sa_prot_state, lbp_r),
367 /*min_val*/ 0, /*max_val*/ 1, NULL },
368 { "rbdp", MT_PARAM_SET_UNSIGNED,
369 __offsetof(struct sa_prot_state, rbdp),
370 /*min_val*/ 0, /*max_val*/ 1, NULL }
371 };
372
373 #define SA_NUM_PROT_ENTS nitems(sa_prot_table)
374
375 #define SA_PROT_ENABLED(softc) ((softc->flags & SA_FLAG_PROTECT_SUPP) \
376 && (softc->prot_info.cur_prot_state.initialized != 0) \
377 && (softc->prot_info.cur_prot_state.prot_method != 0))
378
379 #define SA_PROT_LEN(softc) softc->prot_info.cur_prot_state.pi_length
380
381 struct sa_softc {
382 sa_state state;
383 sa_flags flags;
384 sa_quirks quirks;
385 u_int si_flags;
386 struct cam_periph *periph;
387 struct bio_queue_head bio_queue;
388 int queue_count;
389 struct devstat *device_stats;
390 struct sa_devs devs;
391 int open_count;
392 int num_devs_to_destroy;
393 int blk_gran;
394 int blk_mask;
395 int blk_shift;
396 u_int32_t max_blk;
397 u_int32_t min_blk;
398 u_int32_t maxio;
399 u_int32_t cpi_maxio;
400 int allow_io_split;
401 int inject_eom;
402 int set_pews_status;
403 u_int32_t comp_algorithm;
404 u_int32_t saved_comp_algorithm;
405 u_int32_t media_blksize;
406 u_int32_t last_media_blksize;
407 u_int32_t media_numblks;
408 u_int8_t media_density;
409 u_int8_t speed;
410 u_int8_t scsi_rev;
411 u_int8_t dsreg; /* mtio mt_dsreg, redux */
412 int buffer_mode;
413 int filemarks;
414 union ccb saved_ccb;
415 int last_resid_was_io;
416 uint8_t density_type_bits[SA_DENSITY_TYPES];
417 int density_info_valid[SA_DENSITY_TYPES];
418 uint8_t density_info[SA_DENSITY_TYPES][SRDS_MAX_LENGTH];
419 int timeout_info[SA_TIMEOUT_TYPE_MAX];
420
421 struct sa_prot_info prot_info;
422
423 int sili;
424 int eot_warn;
425
426 /*
427 * Current position information. -1 means that the given value is
428 * unknown. fileno and blkno are always calculated. blkno is
429 * relative to the previous file mark. rep_fileno and rep_blkno
430 * are as reported by the drive, if it supports the long form
431 * report for the READ POSITION command. rep_blkno is relative to
432 * the beginning of the partition.
433 *
434 * bop means that the drive is at the beginning of the partition.
435 * eop means that the drive is between early warning and end of
436 * partition, inside the current partition.
437 * bpew means that the position is in a PEWZ (Programmable Early
438 * Warning Zone)
439 */
440 daddr_t partition; /* Absolute from BOT */
441 daddr_t fileno; /* Relative to beginning of partition */
442 daddr_t blkno; /* Relative to last file mark */
443 daddr_t rep_blkno; /* Relative to beginning of partition */
444 daddr_t rep_fileno; /* Relative to beginning of partition */
445 int bop; /* Beginning of Partition */
446 int eop; /* End of Partition */
447 int bpew; /* Beyond Programmable Early Warning */
448
449 /*
450 * Latched Error Info
451 */
452 struct {
453 struct scsi_sense_data _last_io_sense;
454 u_int64_t _last_io_resid;
455 u_int8_t _last_io_cdb[CAM_MAX_CDBLEN];
456 struct scsi_sense_data _last_ctl_sense;
457 u_int64_t _last_ctl_resid;
458 u_int8_t _last_ctl_cdb[CAM_MAX_CDBLEN];
459 #define last_io_sense errinfo._last_io_sense
460 #define last_io_resid errinfo._last_io_resid
461 #define last_io_cdb errinfo._last_io_cdb
462 #define last_ctl_sense errinfo._last_ctl_sense
463 #define last_ctl_resid errinfo._last_ctl_resid
464 #define last_ctl_cdb errinfo._last_ctl_cdb
465 } errinfo;
466 /*
467 * Misc other flags/state
468 */
469 u_int32_t
470 : 29,
471 open_rdonly : 1, /* open read-only */
472 open_pending_mount : 1, /* open pending mount */
473 ctrl_mode : 1; /* control device open */
474
475 struct task sysctl_task;
476 struct sysctl_ctx_list sysctl_ctx;
477 struct sysctl_oid *sysctl_tree;
478 struct sysctl_ctx_list sysctl_timeout_ctx;
479 struct sysctl_oid *sysctl_timeout_tree;
480 };
481
482 struct sa_quirk_entry {
483 struct scsi_inquiry_pattern inq_pat; /* matching pattern */
484 sa_quirks quirks; /* specific quirk type */
485 u_int32_t prefblk; /* preferred blocksize when in fixed mode */
486 };
487
488 static struct sa_quirk_entry sa_quirk_table[] =
489 {
490 {
491 { T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "OnStream",
492 "ADR*", "*"}, SA_QUIRK_FIXED|SA_QUIRK_NODREAD |
493 SA_QUIRK_1FM|SA_QUIRK_NO_MODESEL, 32768
494 },
495 {
496 { T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "ARCHIVE",
497 "Python 06408*", "*"}, SA_QUIRK_NODREAD, 0
498 },
499 {
500 { T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "ARCHIVE",
501 "Python 25601*", "*"}, SA_QUIRK_NOCOMP|SA_QUIRK_NODREAD, 0
502 },
503 {
504 { T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "ARCHIVE",
505 "Python*", "*"}, SA_QUIRK_NODREAD, 0
506 },
507 {
508 { T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "ARCHIVE",
509 "VIPER 150*", "*"}, SA_QUIRK_FIXED|SA_QUIRK_1FM, 512
510 },
511 {
512 { T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "ARCHIVE",
513 "VIPER 2525 25462", "-011"},
514 SA_QUIRK_NOCOMP|SA_QUIRK_1FM|SA_QUIRK_NODREAD, 0
515 },
516 {
517 { T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "ARCHIVE",
518 "VIPER 2525*", "*"}, SA_QUIRK_FIXED|SA_QUIRK_1FM, 1024
519 },
520 #if 0
521 {
522 { T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "HP",
523 "C15*", "*"}, SA_QUIRK_VARIABLE|SA_QUIRK_NO_CPAGE, 0,
524 },
525 #endif
526 {
527 { T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "HP",
528 "C56*", "*"}, SA_QUIRK_VARIABLE|SA_QUIRK_2FM, 0
529 },
530 {
531 { T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "HP",
532 "T20*", "*"}, SA_QUIRK_FIXED|SA_QUIRK_1FM, 512
533 },
534 {
535 { T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "HP",
536 "T4000*", "*"}, SA_QUIRK_FIXED|SA_QUIRK_1FM, 512
537 },
538 {
539 { T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "HP",
540 "HP-88780*", "*"}, SA_QUIRK_VARIABLE|SA_QUIRK_2FM, 0
541 },
542 {
543 { T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "KENNEDY",
544 "*", "*"}, SA_QUIRK_VARIABLE|SA_QUIRK_2FM, 0
545 },
546 {
547 { T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "M4 DATA",
548 "123107 SCSI*", "*"}, SA_QUIRK_VARIABLE|SA_QUIRK_2FM, 0
549 },
550 { /* jreynold@primenet.com */
551 { T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "Seagate",
552 "STT8000N*", "*"}, SA_QUIRK_1FM, 0
553 },
554 { /* mike@sentex.net */
555 { T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "Seagate",
556 "STT20000*", "*"}, SA_QUIRK_1FM, 0
557 },
558 {
559 { T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "SEAGATE",
560 "DAT 06241-XXX", "*"}, SA_QUIRK_VARIABLE|SA_QUIRK_2FM, 0
561 },
562 {
563 { T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "TANDBERG",
564 " TDC 3600", "U07:"}, SA_QUIRK_NOCOMP|SA_QUIRK_1FM, 512
565 },
566 {
567 { T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "TANDBERG",
568 " TDC 3800", "*"}, SA_QUIRK_NOCOMP|SA_QUIRK_1FM, 512
569 },
570 {
571 { T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "TANDBERG",
572 " TDC 4100", "*"}, SA_QUIRK_NOCOMP|SA_QUIRK_1FM, 512
573 },
574 {
575 { T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "TANDBERG",
576 " TDC 4200", "*"}, SA_QUIRK_NOCOMP|SA_QUIRK_1FM, 512
577 },
578 {
579 { T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "TANDBERG",
580 " SLR*", "*"}, SA_QUIRK_1FM, 0
581 },
582 {
583 { T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "WANGTEK",
584 "5525ES*", "*"}, SA_QUIRK_FIXED|SA_QUIRK_1FM, 512
585 },
586 {
587 { T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "WANGTEK",
588 "51000*", "*"}, SA_QUIRK_FIXED|SA_QUIRK_1FM, 1024
589 }
590 };
591
592 static d_open_t saopen;
593 static d_close_t saclose;
594 static d_strategy_t sastrategy;
595 static d_ioctl_t saioctl;
596 static periph_init_t sainit;
597 static periph_ctor_t saregister;
598 static periph_oninv_t saoninvalidate;
599 static periph_dtor_t sacleanup;
600 static periph_start_t sastart;
601 static void saasync(void *callback_arg, u_int32_t code,
602 struct cam_path *path, void *arg);
603 static void sadone(struct cam_periph *periph,
604 union ccb *start_ccb);
605 static int saerror(union ccb *ccb, u_int32_t cam_flags,
606 u_int32_t sense_flags);
607 static int samarkswanted(struct cam_periph *);
608 static int sacheckeod(struct cam_periph *periph);
609 static int sagetparams(struct cam_periph *periph,
610 sa_params params_to_get,
611 u_int32_t *blocksize, u_int8_t *density,
612 u_int32_t *numblocks, int *buff_mode,
613 u_int8_t *write_protect, u_int8_t *speed,
614 int *comp_supported, int *comp_enabled,
615 u_int32_t *comp_algorithm,
616 sa_comp_t *comp_page,
617 struct scsi_control_data_prot_subpage
618 *prot_page, int dp_size,
619 int prot_changeable);
620 static int sasetprot(struct cam_periph *periph,
621 struct sa_prot_state *new_prot);
622 static int sasetparams(struct cam_periph *periph,
623 sa_params params_to_set,
624 u_int32_t blocksize, u_int8_t density,
625 u_int32_t comp_algorithm,
626 u_int32_t sense_flags);
627 static int sasetsili(struct cam_periph *periph,
628 struct mtparamset *ps, int num_params);
629 static int saseteotwarn(struct cam_periph *periph,
630 struct mtparamset *ps, int num_params);
631 static void safillprot(struct sa_softc *softc, int *indent,
632 struct sbuf *sb);
633 static void sapopulateprots(struct sa_prot_state *cur_state,
634 struct sa_prot_map *new_table,
635 int table_ents);
636 static struct sa_prot_map *safindprotent(char *name, struct sa_prot_map *table,
637 int table_ents);
638 static int sasetprotents(struct cam_periph *periph,
639 struct mtparamset *ps, int num_params);
640 static struct sa_param_ent *safindparament(struct mtparamset *ps);
641 static int saparamsetlist(struct cam_periph *periph,
642 struct mtsetlist *list, int need_copy);
643 static int saextget(struct cdev *dev, struct cam_periph *periph,
644 struct sbuf *sb, struct mtextget *g);
645 static int saparamget(struct sa_softc *softc, struct sbuf *sb);
646 static void saprevent(struct cam_periph *periph, int action);
647 static int sarewind(struct cam_periph *periph);
648 static int saspace(struct cam_periph *periph, int count,
649 scsi_space_code code);
650 static void sadevgonecb(void *arg);
651 static void sasetupdev(struct sa_softc *softc, struct cdev *dev);
652 static void saloadtotunables(struct sa_softc *softc);
653 static void sasysctlinit(void *context, int pending);
654 static int samount(struct cam_periph *, int, struct cdev *);
655 static int saretension(struct cam_periph *periph);
656 static int sareservereleaseunit(struct cam_periph *periph,
657 int reserve);
658 static int saloadunload(struct cam_periph *periph, int load);
659 static int saerase(struct cam_periph *periph, int longerase);
660 static int sawritefilemarks(struct cam_periph *periph,
661 int nmarks, int setmarks, int immed);
662 static int sagetpos(struct cam_periph *periph);
663 static int sardpos(struct cam_periph *periph, int, u_int32_t *);
664 static int sasetpos(struct cam_periph *periph, int,
665 struct mtlocate *);
666 static void safilldenstypesb(struct sbuf *sb, int *indent,
667 uint8_t *buf, int buf_len,
668 int is_density);
669 static void safilldensitysb(struct sa_softc *softc, int *indent,
670 struct sbuf *sb);
671 static void saloadtimeouts(struct sa_softc *softc, union ccb *ccb);
672
673
674 #ifndef SA_DEFAULT_IO_SPLIT
675 #define SA_DEFAULT_IO_SPLIT 0
676 #endif
677
678 static int sa_allow_io_split = SA_DEFAULT_IO_SPLIT;
679
680 /*
681 * Tunable to allow the user to set a global allow_io_split value. Note
682 * that this WILL GO AWAY in FreeBSD 11.0. Silently splitting the I/O up
683 * is bad behavior, because it hides the true tape block size from the
684 * application.
685 */
686 static SYSCTL_NODE(_kern_cam, OID_AUTO, sa, CTLFLAG_RD, 0,
687 "CAM Sequential Access Tape Driver");
688 SYSCTL_INT(_kern_cam_sa, OID_AUTO, allow_io_split, CTLFLAG_RDTUN,
689 &sa_allow_io_split, 0, "Default I/O split value");
690
691 static struct periph_driver sadriver =
692 {
693 sainit, "sa",
694 TAILQ_HEAD_INITIALIZER(sadriver.units), /* generation */ 0
695 };
696
697 PERIPHDRIVER_DECLARE(sa, sadriver);
698
699 /* For 2.2-stable support */
700 #ifndef D_TAPE
701 #define D_TAPE 0
702 #endif
703
704
705 static struct cdevsw sa_cdevsw = {
706 .d_version = D_VERSION,
707 .d_open = saopen,
708 .d_close = saclose,
709 .d_read = physread,
710 .d_write = physwrite,
711 .d_ioctl = saioctl,
712 .d_strategy = sastrategy,
713 .d_name = "sa",
714 .d_flags = D_TAPE | D_TRACKCLOSE,
715 };
716
717 static int
saopen(struct cdev * dev,int flags,int fmt,struct thread * td)718 saopen(struct cdev *dev, int flags, int fmt, struct thread *td)
719 {
720 struct cam_periph *periph;
721 struct sa_softc *softc;
722 int error;
723
724 periph = (struct cam_periph *)dev->si_drv1;
725 if (cam_periph_acquire(periph) != 0) {
726 return (ENXIO);
727 }
728
729 cam_periph_lock(periph);
730
731 softc = (struct sa_softc *)periph->softc;
732
733 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE|CAM_DEBUG_INFO,
734 ("saopen(%s): softc=0x%x\n", devtoname(dev), softc->flags));
735
736 if (SA_IS_CTRL(dev)) {
737 softc->ctrl_mode = 1;
738 softc->open_count++;
739 cam_periph_unlock(periph);
740 return (0);
741 }
742
743 if ((error = cam_periph_hold(periph, PRIBIO|PCATCH)) != 0) {
744 cam_periph_unlock(periph);
745 cam_periph_release(periph);
746 return (error);
747 }
748
749 if (softc->flags & SA_FLAG_OPEN) {
750 error = EBUSY;
751 } else if (softc->flags & SA_FLAG_INVALID) {
752 error = ENXIO;
753 } else {
754 /*
755 * Preserve whether this is a read_only open.
756 */
757 softc->open_rdonly = (flags & O_RDWR) == O_RDONLY;
758
759 /*
760 * The function samount ensures media is loaded and ready.
761 * It also does a device RESERVE if the tape isn't yet mounted.
762 *
763 * If the mount fails and this was a non-blocking open,
764 * make this a 'open_pending_mount' action.
765 */
766 error = samount(periph, flags, dev);
767 if (error && (flags & O_NONBLOCK)) {
768 softc->flags |= SA_FLAG_OPEN;
769 softc->open_pending_mount = 1;
770 softc->open_count++;
771 cam_periph_unhold(periph);
772 cam_periph_unlock(periph);
773 return (0);
774 }
775 }
776
777 if (error) {
778 cam_periph_unhold(periph);
779 cam_periph_unlock(periph);
780 cam_periph_release(periph);
781 return (error);
782 }
783
784 saprevent(periph, PR_PREVENT);
785 softc->flags |= SA_FLAG_OPEN;
786 softc->open_count++;
787
788 cam_periph_unhold(periph);
789 cam_periph_unlock(periph);
790 return (error);
791 }
792
793 static int
saclose(struct cdev * dev,int flag,int fmt,struct thread * td)794 saclose(struct cdev *dev, int flag, int fmt, struct thread *td)
795 {
796 struct cam_periph *periph;
797 struct sa_softc *softc;
798 int mode, error, writing, tmp, i;
799 int closedbits = SA_FLAG_OPEN;
800
801 mode = SAMODE(dev);
802 periph = (struct cam_periph *)dev->si_drv1;
803 cam_periph_lock(periph);
804
805 softc = (struct sa_softc *)periph->softc;
806
807 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE|CAM_DEBUG_INFO,
808 ("saclose(%s): softc=0x%x\n", devtoname(dev), softc->flags));
809
810
811 softc->open_rdonly = 0;
812 if (SA_IS_CTRL(dev)) {
813 softc->ctrl_mode = 0;
814 softc->open_count--;
815 cam_periph_unlock(periph);
816 cam_periph_release(periph);
817 return (0);
818 }
819
820 if (softc->open_pending_mount) {
821 softc->flags &= ~SA_FLAG_OPEN;
822 softc->open_pending_mount = 0;
823 softc->open_count--;
824 cam_periph_unlock(periph);
825 cam_periph_release(periph);
826 return (0);
827 }
828
829 if ((error = cam_periph_hold(periph, PRIBIO)) != 0) {
830 cam_periph_unlock(periph);
831 return (error);
832 }
833
834 /*
835 * Were we writing the tape?
836 */
837 writing = (softc->flags & SA_FLAG_TAPE_WRITTEN) != 0;
838
839 /*
840 * See whether or not we need to write filemarks. If this
841 * fails, we probably have to assume we've lost tape
842 * position.
843 */
844 error = sacheckeod(periph);
845 if (error) {
846 xpt_print(periph->path,
847 "failed to write terminating filemark(s)\n");
848 softc->flags |= SA_FLAG_TAPE_FROZEN;
849 }
850
851 /*
852 * Whatever we end up doing, allow users to eject tapes from here on.
853 */
854 saprevent(periph, PR_ALLOW);
855
856 /*
857 * Decide how to end...
858 */
859 if ((softc->flags & SA_FLAG_TAPE_MOUNTED) == 0) {
860 closedbits |= SA_FLAG_TAPE_FROZEN;
861 } else switch (mode) {
862 case SA_MODE_OFFLINE:
863 /*
864 * An 'offline' close is an unconditional release of
865 * frozen && mount conditions, irrespective of whether
866 * these operations succeeded. The reason for this is
867 * to allow at least some kind of programmatic way
868 * around our state getting all fouled up. If somebody
869 * issues an 'offline' command, that will be allowed
870 * to clear state.
871 */
872 (void) sarewind(periph);
873 (void) saloadunload(periph, FALSE);
874 closedbits |= SA_FLAG_TAPE_MOUNTED|SA_FLAG_TAPE_FROZEN;
875 break;
876 case SA_MODE_REWIND:
877 /*
878 * If the rewind fails, return an error- if anyone cares,
879 * but not overwriting any previous error.
880 *
881 * We don't clear the notion of mounted here, but we do
882 * clear the notion of frozen if we successfully rewound.
883 */
884 tmp = sarewind(periph);
885 if (tmp) {
886 if (error != 0)
887 error = tmp;
888 } else {
889 closedbits |= SA_FLAG_TAPE_FROZEN;
890 }
891 break;
892 case SA_MODE_NOREWIND:
893 /*
894 * If we're not rewinding/unloading the tape, find out
895 * whether we need to back up over one of two filemarks
896 * we wrote (if we wrote two filemarks) so that appends
897 * from this point on will be sane.
898 */
899 if (error == 0 && writing && (softc->quirks & SA_QUIRK_2FM)) {
900 tmp = saspace(periph, -1, SS_FILEMARKS);
901 if (tmp) {
902 xpt_print(periph->path, "unable to backspace "
903 "over one of double filemarks at end of "
904 "tape\n");
905 xpt_print(periph->path, "it is possible that "
906 "this device needs a SA_QUIRK_1FM quirk set"
907 "for it\n");
908 softc->flags |= SA_FLAG_TAPE_FROZEN;
909 }
910 }
911 break;
912 default:
913 xpt_print(periph->path, "unknown mode 0x%x in saclose\n", mode);
914 /* NOTREACHED */
915 break;
916 }
917
918 /*
919 * We wish to note here that there are no more filemarks to be written.
920 */
921 softc->filemarks = 0;
922 softc->flags &= ~SA_FLAG_TAPE_WRITTEN;
923
924 /*
925 * And we are no longer open for business.
926 */
927 softc->flags &= ~closedbits;
928 softc->open_count--;
929
930 /*
931 * Invalidate any density information that depends on having tape
932 * media in the drive.
933 */
934 for (i = 0; i < SA_DENSITY_TYPES; i++) {
935 if (softc->density_type_bits[i] & SRDS_MEDIA)
936 softc->density_info_valid[i] = 0;
937 }
938
939 /*
940 * Inform users if tape state if frozen....
941 */
942 if (softc->flags & SA_FLAG_TAPE_FROZEN) {
943 xpt_print(periph->path, "tape is now frozen- use an OFFLINE, "
944 "REWIND or MTEOM command to clear this state.\n");
945 }
946
947 /* release the device if it is no longer mounted */
948 if ((softc->flags & SA_FLAG_TAPE_MOUNTED) == 0)
949 sareservereleaseunit(periph, FALSE);
950
951 cam_periph_unhold(periph);
952 cam_periph_unlock(periph);
953 cam_periph_release(periph);
954
955 return (error);
956 }
957
958 /*
959 * Actually translate the requested transfer into one the physical driver
960 * can understand. The transfer is described by a buf and will include
961 * only one physical transfer.
962 */
963 static void
sastrategy(struct bio * bp)964 sastrategy(struct bio *bp)
965 {
966 struct cam_periph *periph;
967 struct sa_softc *softc;
968
969 bp->bio_resid = bp->bio_bcount;
970 if (SA_IS_CTRL(bp->bio_dev)) {
971 biofinish(bp, NULL, EINVAL);
972 return;
973 }
974 periph = (struct cam_periph *)bp->bio_dev->si_drv1;
975 cam_periph_lock(periph);
976
977 softc = (struct sa_softc *)periph->softc;
978
979 if (softc->flags & SA_FLAG_INVALID) {
980 cam_periph_unlock(periph);
981 biofinish(bp, NULL, ENXIO);
982 return;
983 }
984
985 if (softc->flags & SA_FLAG_TAPE_FROZEN) {
986 cam_periph_unlock(periph);
987 biofinish(bp, NULL, EPERM);
988 return;
989 }
990
991 /*
992 * This should actually never occur as the write(2)
993 * system call traps attempts to write to a read-only
994 * file descriptor.
995 */
996 if (bp->bio_cmd == BIO_WRITE && softc->open_rdonly) {
997 cam_periph_unlock(periph);
998 biofinish(bp, NULL, EBADF);
999 return;
1000 }
1001
1002 if (softc->open_pending_mount) {
1003 int error = samount(periph, 0, bp->bio_dev);
1004 if (error) {
1005 cam_periph_unlock(periph);
1006 biofinish(bp, NULL, ENXIO);
1007 return;
1008 }
1009 saprevent(periph, PR_PREVENT);
1010 softc->open_pending_mount = 0;
1011 }
1012
1013
1014 /*
1015 * If it's a null transfer, return immediately
1016 */
1017 if (bp->bio_bcount == 0) {
1018 cam_periph_unlock(periph);
1019 biodone(bp);
1020 return;
1021 }
1022
1023 /* valid request? */
1024 if (softc->flags & SA_FLAG_FIXED) {
1025 /*
1026 * Fixed block device. The byte count must
1027 * be a multiple of our block size.
1028 */
1029 if (((softc->blk_mask != ~0) &&
1030 ((bp->bio_bcount & softc->blk_mask) != 0)) ||
1031 ((softc->blk_mask == ~0) &&
1032 ((bp->bio_bcount % softc->min_blk) != 0))) {
1033 xpt_print(periph->path, "Invalid request. Fixed block "
1034 "device requests must be a multiple of %d bytes\n",
1035 softc->min_blk);
1036 cam_periph_unlock(periph);
1037 biofinish(bp, NULL, EINVAL);
1038 return;
1039 }
1040 } else if ((bp->bio_bcount > softc->max_blk) ||
1041 (bp->bio_bcount < softc->min_blk) ||
1042 (bp->bio_bcount & softc->blk_mask) != 0) {
1043
1044 xpt_print_path(periph->path);
1045 printf("Invalid request. Variable block "
1046 "device requests must be ");
1047 if (softc->blk_mask != 0) {
1048 printf("a multiple of %d ", (0x1 << softc->blk_gran));
1049 }
1050 printf("between %d and %d bytes\n", softc->min_blk,
1051 softc->max_blk);
1052 cam_periph_unlock(periph);
1053 biofinish(bp, NULL, EINVAL);
1054 return;
1055 }
1056
1057 /*
1058 * Place it at the end of the queue.
1059 */
1060 bioq_insert_tail(&softc->bio_queue, bp);
1061 softc->queue_count++;
1062 #if 0
1063 CAM_DEBUG(periph->path, CAM_DEBUG_INFO,
1064 ("sastrategy: queuing a %ld %s byte %s\n", bp->bio_bcount,
1065 (softc->flags & SA_FLAG_FIXED)? "fixed" : "variable",
1066 (bp->bio_cmd == BIO_READ)? "read" : "write"));
1067 #endif
1068 if (softc->queue_count > 1) {
1069 CAM_DEBUG(periph->path, CAM_DEBUG_INFO,
1070 ("sastrategy: queue count now %d\n", softc->queue_count));
1071 }
1072
1073 /*
1074 * Schedule ourselves for performing the work.
1075 */
1076 xpt_schedule(periph, CAM_PRIORITY_NORMAL);
1077 cam_periph_unlock(periph);
1078
1079 return;
1080 }
1081
1082 static int
sasetsili(struct cam_periph * periph,struct mtparamset * ps,int num_params)1083 sasetsili(struct cam_periph *periph, struct mtparamset *ps, int num_params)
1084 {
1085 uint32_t sili_blocksize;
1086 struct sa_softc *softc;
1087 int error;
1088
1089 error = 0;
1090 softc = (struct sa_softc *)periph->softc;
1091
1092 if (ps->value_type != MT_PARAM_SET_SIGNED) {
1093 snprintf(ps->error_str, sizeof(ps->error_str),
1094 "sili is a signed parameter");
1095 goto bailout;
1096 }
1097 if ((ps->value.value_signed < 0)
1098 || (ps->value.value_signed > 1)) {
1099 snprintf(ps->error_str, sizeof(ps->error_str),
1100 "invalid sili value %jd", (intmax_t)ps->value.value_signed);
1101 goto bailout_error;
1102 }
1103 /*
1104 * We only set the SILI flag in variable block
1105 * mode. You'll get a check condition in fixed
1106 * block mode if things don't line up in any case.
1107 */
1108 if (softc->flags & SA_FLAG_FIXED) {
1109 snprintf(ps->error_str, sizeof(ps->error_str),
1110 "can't set sili bit in fixed block mode");
1111 goto bailout_error;
1112 }
1113 if (softc->sili == ps->value.value_signed)
1114 goto bailout;
1115
1116 if (ps->value.value_signed == 1)
1117 sili_blocksize = 4;
1118 else
1119 sili_blocksize = 0;
1120
1121 error = sasetparams(periph, SA_PARAM_BLOCKSIZE,
1122 sili_blocksize, 0, 0, SF_QUIET_IR);
1123 if (error != 0) {
1124 snprintf(ps->error_str, sizeof(ps->error_str),
1125 "sasetparams() returned error %d", error);
1126 goto bailout_error;
1127 }
1128
1129 softc->sili = ps->value.value_signed;
1130
1131 bailout:
1132 ps->status = MT_PARAM_STATUS_OK;
1133 return (error);
1134
1135 bailout_error:
1136 ps->status = MT_PARAM_STATUS_ERROR;
1137 if (error == 0)
1138 error = EINVAL;
1139
1140 return (error);
1141 }
1142
1143 static int
saseteotwarn(struct cam_periph * periph,struct mtparamset * ps,int num_params)1144 saseteotwarn(struct cam_periph *periph, struct mtparamset *ps, int num_params)
1145 {
1146 struct sa_softc *softc;
1147 int error;
1148
1149 error = 0;
1150 softc = (struct sa_softc *)periph->softc;
1151
1152 if (ps->value_type != MT_PARAM_SET_SIGNED) {
1153 snprintf(ps->error_str, sizeof(ps->error_str),
1154 "eot_warn is a signed parameter");
1155 ps->status = MT_PARAM_STATUS_ERROR;
1156 goto bailout;
1157 }
1158 if ((ps->value.value_signed < 0)
1159 || (ps->value.value_signed > 1)) {
1160 snprintf(ps->error_str, sizeof(ps->error_str),
1161 "invalid eot_warn value %jd\n",
1162 (intmax_t)ps->value.value_signed);
1163 ps->status = MT_PARAM_STATUS_ERROR;
1164 goto bailout;
1165 }
1166 softc->eot_warn = ps->value.value_signed;
1167 ps->status = MT_PARAM_STATUS_OK;
1168 bailout:
1169 if (ps->status != MT_PARAM_STATUS_OK)
1170 error = EINVAL;
1171
1172 return (error);
1173 }
1174
1175
1176 static void
safillprot(struct sa_softc * softc,int * indent,struct sbuf * sb)1177 safillprot(struct sa_softc *softc, int *indent, struct sbuf *sb)
1178 {
1179 int tmpint;
1180
1181 SASBADDNODE(sb, *indent, protection);
1182 if (softc->flags & SA_FLAG_PROTECT_SUPP)
1183 tmpint = 1;
1184 else
1185 tmpint = 0;
1186 SASBADDINTDESC(sb, *indent, tmpint, %d, protection_supported,
1187 "Set to 1 if protection information is supported");
1188
1189 if ((tmpint != 0)
1190 && (softc->prot_info.cur_prot_state.initialized != 0)) {
1191 struct sa_prot_state *prot;
1192
1193 prot = &softc->prot_info.cur_prot_state;
1194
1195 SASBADDUINTDESC(sb, *indent, prot->prot_method, %u,
1196 prot_method, "Current Protection Method");
1197 SASBADDUINTDESC(sb, *indent, prot->pi_length, %u,
1198 pi_length, "Length of Protection Information");
1199 SASBADDUINTDESC(sb, *indent, prot->lbp_w, %u,
1200 lbp_w, "Check Protection on Writes");
1201 SASBADDUINTDESC(sb, *indent, prot->lbp_r, %u,
1202 lbp_r, "Check and Include Protection on Reads");
1203 SASBADDUINTDESC(sb, *indent, prot->rbdp, %u,
1204 rbdp, "Transfer Protection Information for RECOVER "
1205 "BUFFERED DATA command");
1206 }
1207 SASBENDNODE(sb, *indent, protection);
1208 }
1209
1210 static void
sapopulateprots(struct sa_prot_state * cur_state,struct sa_prot_map * new_table,int table_ents)1211 sapopulateprots(struct sa_prot_state *cur_state, struct sa_prot_map *new_table,
1212 int table_ents)
1213 {
1214 int i;
1215
1216 bcopy(sa_prot_table, new_table, min(table_ents * sizeof(*new_table),
1217 sizeof(sa_prot_table)));
1218
1219 table_ents = min(table_ents, SA_NUM_PROT_ENTS);
1220
1221 for (i = 0; i < table_ents; i++)
1222 new_table[i].value = (uint32_t *)((uint8_t *)cur_state +
1223 new_table[i].offset);
1224
1225 return;
1226 }
1227
1228 static struct sa_prot_map *
safindprotent(char * name,struct sa_prot_map * table,int table_ents)1229 safindprotent(char *name, struct sa_prot_map *table, int table_ents)
1230 {
1231 char *prot_name = "protection.";
1232 int i, prot_len;
1233
1234 prot_len = strlen(prot_name);
1235
1236 /*
1237 * This shouldn't happen, but we check just in case.
1238 */
1239 if (strncmp(name, prot_name, prot_len) != 0)
1240 goto bailout;
1241
1242 for (i = 0; i < table_ents; i++) {
1243 if (strcmp(&name[prot_len], table[i].name) != 0)
1244 continue;
1245 return (&table[i]);
1246 }
1247 bailout:
1248 return (NULL);
1249 }
1250
1251 static int
sasetprotents(struct cam_periph * periph,struct mtparamset * ps,int num_params)1252 sasetprotents(struct cam_periph *periph, struct mtparamset *ps, int num_params)
1253 {
1254 struct sa_softc *softc;
1255 struct sa_prot_map prot_ents[SA_NUM_PROT_ENTS];
1256 struct sa_prot_state new_state;
1257 int error;
1258 int i;
1259
1260 softc = (struct sa_softc *)periph->softc;
1261 error = 0;
1262
1263 /*
1264 * Make sure that this tape drive supports protection information.
1265 * Otherwise we can't set anything.
1266 */
1267 if ((softc->flags & SA_FLAG_PROTECT_SUPP) == 0) {
1268 snprintf(ps[0].error_str, sizeof(ps[0].error_str),
1269 "Protection information is not supported for this device");
1270 ps[0].status = MT_PARAM_STATUS_ERROR;
1271 goto bailout;
1272 }
1273
1274 /*
1275 * We can't operate with physio(9) splitting enabled, because there
1276 * is no way to insure (especially in variable block mode) that
1277 * what the user writes (with a checksum block at the end) will
1278 * make it into the sa(4) driver intact.
1279 */
1280 if ((softc->si_flags & SI_NOSPLIT) == 0) {
1281 snprintf(ps[0].error_str, sizeof(ps[0].error_str),
1282 "Protection information cannot be enabled with I/O "
1283 "splitting");
1284 ps[0].status = MT_PARAM_STATUS_ERROR;
1285 goto bailout;
1286 }
1287
1288 /*
1289 * Take the current cached protection state and use that as the
1290 * basis for our new entries.
1291 */
1292 bcopy(&softc->prot_info.cur_prot_state, &new_state, sizeof(new_state));
1293
1294 /*
1295 * Populate the table mapping property names to pointers into the
1296 * state structure.
1297 */
1298 sapopulateprots(&new_state, prot_ents, SA_NUM_PROT_ENTS);
1299
1300 /*
1301 * For each parameter the user passed in, make sure the name, type
1302 * and value are valid.
1303 */
1304 for (i = 0; i < num_params; i++) {
1305 struct sa_prot_map *ent;
1306
1307 ent = safindprotent(ps[i].value_name, prot_ents,
1308 SA_NUM_PROT_ENTS);
1309 if (ent == NULL) {
1310 ps[i].status = MT_PARAM_STATUS_ERROR;
1311 snprintf(ps[i].error_str, sizeof(ps[i].error_str),
1312 "Invalid protection entry name %s",
1313 ps[i].value_name);
1314 error = EINVAL;
1315 goto bailout;
1316 }
1317 if (ent->param_type != ps[i].value_type) {
1318 ps[i].status = MT_PARAM_STATUS_ERROR;
1319 snprintf(ps[i].error_str, sizeof(ps[i].error_str),
1320 "Supplied type %d does not match actual type %d",
1321 ps[i].value_type, ent->param_type);
1322 error = EINVAL;
1323 goto bailout;
1324 }
1325 if ((ps[i].value.value_unsigned < ent->min_val)
1326 || (ps[i].value.value_unsigned > ent->max_val)) {
1327 ps[i].status = MT_PARAM_STATUS_ERROR;
1328 snprintf(ps[i].error_str, sizeof(ps[i].error_str),
1329 "Value %ju is outside valid range %u - %u",
1330 (uintmax_t)ps[i].value.value_unsigned, ent->min_val,
1331 ent->max_val);
1332 error = EINVAL;
1333 goto bailout;
1334 }
1335 *(ent->value) = ps[i].value.value_unsigned;
1336 }
1337
1338 /*
1339 * Actually send the protection settings to the drive.
1340 */
1341 error = sasetprot(periph, &new_state);
1342 if (error != 0) {
1343 for (i = 0; i < num_params; i++) {
1344 ps[i].status = MT_PARAM_STATUS_ERROR;
1345 snprintf(ps[i].error_str, sizeof(ps[i].error_str),
1346 "Unable to set parameter, see dmesg(8)");
1347 }
1348 goto bailout;
1349 }
1350
1351 /*
1352 * Let the user know that his settings were stored successfully.
1353 */
1354 for (i = 0; i < num_params; i++)
1355 ps[i].status = MT_PARAM_STATUS_OK;
1356
1357 bailout:
1358 return (error);
1359 }
1360 /*
1361 * Entry handlers generally only handle a single entry. Node handlers will
1362 * handle a contiguous range of parameters to set in a single call.
1363 */
1364 typedef enum {
1365 SA_PARAM_TYPE_ENTRY,
1366 SA_PARAM_TYPE_NODE
1367 } sa_param_type;
1368
1369 struct sa_param_ent {
1370 char *name;
1371 sa_param_type param_type;
1372 int (*set_func)(struct cam_periph *periph, struct mtparamset *ps,
1373 int num_params);
1374 } sa_param_table[] = {
1375 {"sili", SA_PARAM_TYPE_ENTRY, sasetsili },
1376 {"eot_warn", SA_PARAM_TYPE_ENTRY, saseteotwarn },
1377 {"protection.", SA_PARAM_TYPE_NODE, sasetprotents }
1378 };
1379
1380 static struct sa_param_ent *
safindparament(struct mtparamset * ps)1381 safindparament(struct mtparamset *ps)
1382 {
1383 unsigned int i;
1384
1385 for (i = 0; i < nitems(sa_param_table); i++){
1386 /*
1387 * For entries, we compare all of the characters. For
1388 * nodes, we only compare the first N characters. The node
1389 * handler will decode the rest.
1390 */
1391 if (sa_param_table[i].param_type == SA_PARAM_TYPE_ENTRY) {
1392 if (strcmp(ps->value_name, sa_param_table[i].name) != 0)
1393 continue;
1394 } else {
1395 if (strncmp(ps->value_name, sa_param_table[i].name,
1396 strlen(sa_param_table[i].name)) != 0)
1397 continue;
1398 }
1399 return (&sa_param_table[i]);
1400 }
1401
1402 return (NULL);
1403 }
1404
1405 /*
1406 * Go through a list of parameters, coalescing contiguous parameters with
1407 * the same parent node into a single call to a set_func.
1408 */
1409 static int
saparamsetlist(struct cam_periph * periph,struct mtsetlist * list,int need_copy)1410 saparamsetlist(struct cam_periph *periph, struct mtsetlist *list,
1411 int need_copy)
1412 {
1413 int i, contig_ents;
1414 int error;
1415 struct mtparamset *params, *first;
1416 struct sa_param_ent *first_ent;
1417
1418 error = 0;
1419 params = NULL;
1420
1421 if (list->num_params == 0)
1422 /* Nothing to do */
1423 goto bailout;
1424
1425 /*
1426 * Verify that the user has the correct structure size.
1427 */
1428 if ((list->num_params * sizeof(struct mtparamset)) !=
1429 list->param_len) {
1430 xpt_print(periph->path, "%s: length of params %d != "
1431 "sizeof(struct mtparamset) %zd * num_params %d\n",
1432 __func__, list->param_len, sizeof(struct mtparamset),
1433 list->num_params);
1434 error = EINVAL;
1435 goto bailout;
1436 }
1437
1438 if (need_copy != 0) {
1439 /*
1440 * XXX KDM will dropping the lock cause an issue here?
1441 */
1442 cam_periph_unlock(periph);
1443 params = malloc(list->param_len, M_SCSISA, M_WAITOK | M_ZERO);
1444 error = copyin(list->params, params, list->param_len);
1445 cam_periph_lock(periph);
1446
1447 if (error != 0)
1448 goto bailout;
1449 } else {
1450 params = list->params;
1451 }
1452
1453 contig_ents = 0;
1454 first = NULL;
1455 first_ent = NULL;
1456 for (i = 0; i < list->num_params; i++) {
1457 struct sa_param_ent *ent;
1458
1459 ent = safindparament(¶ms[i]);
1460 if (ent == NULL) {
1461 snprintf(params[i].error_str,
1462 sizeof(params[i].error_str),
1463 "%s: cannot find parameter %s", __func__,
1464 params[i].value_name);
1465 params[i].status = MT_PARAM_STATUS_ERROR;
1466 break;
1467 }
1468
1469 if (first != NULL) {
1470 if (first_ent == ent) {
1471 /*
1472 * We're still in a contiguous list of
1473 * parameters that can be handled by one
1474 * node handler.
1475 */
1476 contig_ents++;
1477 continue;
1478 } else {
1479 error = first_ent->set_func(periph, first,
1480 contig_ents);
1481 first = NULL;
1482 first_ent = NULL;
1483 contig_ents = 0;
1484 if (error != 0) {
1485 error = 0;
1486 break;
1487 }
1488 }
1489 }
1490 if (ent->param_type == SA_PARAM_TYPE_NODE) {
1491 first = ¶ms[i];
1492 first_ent = ent;
1493 contig_ents = 1;
1494 } else {
1495 error = ent->set_func(periph, ¶ms[i], 1);
1496 if (error != 0) {
1497 error = 0;
1498 break;
1499 }
1500 }
1501 }
1502 if (first != NULL)
1503 first_ent->set_func(periph, first, contig_ents);
1504
1505 bailout:
1506 if (need_copy != 0) {
1507 if (error != EFAULT) {
1508 cam_periph_unlock(periph);
1509 copyout(params, list->params, list->param_len);
1510 cam_periph_lock(periph);
1511 }
1512 free(params, M_SCSISA);
1513 }
1514 return (error);
1515 }
1516
1517 static int
sagetparams_common(struct cdev * dev,struct cam_periph * periph)1518 sagetparams_common(struct cdev *dev, struct cam_periph *periph)
1519 {
1520 struct sa_softc *softc;
1521 u_int8_t write_protect;
1522 int comp_enabled, comp_supported, error;
1523
1524 softc = (struct sa_softc *)periph->softc;
1525
1526 if (softc->open_pending_mount)
1527 return (0);
1528
1529 /* The control device may issue getparams() if there are no opens. */
1530 if (SA_IS_CTRL(dev) && (softc->flags & SA_FLAG_OPEN) != 0)
1531 return (0);
1532
1533 error = sagetparams(periph, SA_PARAM_ALL, &softc->media_blksize,
1534 &softc->media_density, &softc->media_numblks, &softc->buffer_mode,
1535 &write_protect, &softc->speed, &comp_supported, &comp_enabled,
1536 &softc->comp_algorithm, NULL, NULL, 0, 0);
1537 if (error)
1538 return (error);
1539 if (write_protect)
1540 softc->flags |= SA_FLAG_TAPE_WP;
1541 else
1542 softc->flags &= ~SA_FLAG_TAPE_WP;
1543 softc->flags &= ~SA_FLAG_COMPRESSION;
1544 if (comp_supported) {
1545 if (softc->saved_comp_algorithm == 0)
1546 softc->saved_comp_algorithm =
1547 softc->comp_algorithm;
1548 softc->flags |= SA_FLAG_COMP_SUPP;
1549 if (comp_enabled)
1550 softc->flags |= SA_FLAG_COMP_ENABLED;
1551 } else
1552 softc->flags |= SA_FLAG_COMP_UNSUPP;
1553
1554 return (0);
1555 }
1556
1557 #define PENDING_MOUNT_CHECK(softc, periph, dev) \
1558 if (softc->open_pending_mount) { \
1559 error = samount(periph, 0, dev); \
1560 if (error) { \
1561 break; \
1562 } \
1563 saprevent(periph, PR_PREVENT); \
1564 softc->open_pending_mount = 0; \
1565 }
1566
1567 static int
saioctl(struct cdev * dev,u_long cmd,caddr_t arg,int flag,struct thread * td)1568 saioctl(struct cdev *dev, u_long cmd, caddr_t arg, int flag, struct thread *td)
1569 {
1570 struct cam_periph *periph;
1571 struct sa_softc *softc;
1572 scsi_space_code spaceop;
1573 int didlockperiph = 0;
1574 int mode;
1575 int error = 0;
1576
1577 mode = SAMODE(dev);
1578 error = 0; /* shut up gcc */
1579 spaceop = 0; /* shut up gcc */
1580
1581 periph = (struct cam_periph *)dev->si_drv1;
1582 cam_periph_lock(periph);
1583 softc = (struct sa_softc *)periph->softc;
1584
1585 /*
1586 * Check for control mode accesses. We allow MTIOCGET and
1587 * MTIOCERRSTAT (but need to be the only one open in order
1588 * to clear latched status), and MTSETBSIZE, MTSETDNSTY
1589 * and MTCOMP (but need to be the only one accessing this
1590 * device to run those).
1591 */
1592
1593 if (SA_IS_CTRL(dev)) {
1594 switch (cmd) {
1595 case MTIOCGETEOTMODEL:
1596 case MTIOCGET:
1597 case MTIOCEXTGET:
1598 case MTIOCPARAMGET:
1599 case MTIOCRBLIM:
1600 break;
1601 case MTIOCERRSTAT:
1602 /*
1603 * If the periph isn't already locked, lock it
1604 * so our MTIOCERRSTAT can reset latched error stats.
1605 *
1606 * If the periph is already locked, skip it because
1607 * we're just getting status and it'll be up to the
1608 * other thread that has this device open to do
1609 * an MTIOCERRSTAT that would clear latched status.
1610 */
1611 if ((periph->flags & CAM_PERIPH_LOCKED) == 0) {
1612 error = cam_periph_hold(periph, PRIBIO|PCATCH);
1613 if (error != 0) {
1614 cam_periph_unlock(periph);
1615 return (error);
1616 }
1617 didlockperiph = 1;
1618 }
1619 break;
1620
1621 case MTIOCTOP:
1622 {
1623 struct mtop *mt = (struct mtop *) arg;
1624
1625 /*
1626 * Check to make sure it's an OP we can perform
1627 * with no media inserted.
1628 */
1629 switch (mt->mt_op) {
1630 case MTSETBSIZ:
1631 case MTSETDNSTY:
1632 case MTCOMP:
1633 mt = NULL;
1634 /* FALLTHROUGH */
1635 default:
1636 break;
1637 }
1638 if (mt != NULL) {
1639 break;
1640 }
1641 /* FALLTHROUGH */
1642 }
1643 case MTIOCSETEOTMODEL:
1644 /*
1645 * We need to acquire the peripheral here rather
1646 * than at open time because we are sharing writable
1647 * access to data structures.
1648 */
1649 error = cam_periph_hold(periph, PRIBIO|PCATCH);
1650 if (error != 0) {
1651 cam_periph_unlock(periph);
1652 return (error);
1653 }
1654 didlockperiph = 1;
1655 break;
1656
1657 default:
1658 cam_periph_unlock(periph);
1659 return (EINVAL);
1660 }
1661 }
1662
1663 /*
1664 * Find the device that the user is talking about
1665 */
1666 switch (cmd) {
1667 case MTIOCGET:
1668 {
1669 struct mtget *g = (struct mtget *)arg;
1670
1671 error = sagetparams_common(dev, periph);
1672 if (error)
1673 break;
1674 bzero(g, sizeof(struct mtget));
1675 g->mt_type = MT_ISAR;
1676 if (softc->flags & SA_FLAG_COMP_UNSUPP) {
1677 g->mt_comp = MT_COMP_UNSUPP;
1678 g->mt_comp0 = MT_COMP_UNSUPP;
1679 g->mt_comp1 = MT_COMP_UNSUPP;
1680 g->mt_comp2 = MT_COMP_UNSUPP;
1681 g->mt_comp3 = MT_COMP_UNSUPP;
1682 } else {
1683 if ((softc->flags & SA_FLAG_COMP_ENABLED) == 0) {
1684 g->mt_comp = MT_COMP_DISABLED;
1685 } else {
1686 g->mt_comp = softc->comp_algorithm;
1687 }
1688 g->mt_comp0 = softc->comp_algorithm;
1689 g->mt_comp1 = softc->comp_algorithm;
1690 g->mt_comp2 = softc->comp_algorithm;
1691 g->mt_comp3 = softc->comp_algorithm;
1692 }
1693 g->mt_density = softc->media_density;
1694 g->mt_density0 = softc->media_density;
1695 g->mt_density1 = softc->media_density;
1696 g->mt_density2 = softc->media_density;
1697 g->mt_density3 = softc->media_density;
1698 g->mt_blksiz = softc->media_blksize;
1699 g->mt_blksiz0 = softc->media_blksize;
1700 g->mt_blksiz1 = softc->media_blksize;
1701 g->mt_blksiz2 = softc->media_blksize;
1702 g->mt_blksiz3 = softc->media_blksize;
1703 g->mt_fileno = softc->fileno;
1704 g->mt_blkno = softc->blkno;
1705 g->mt_dsreg = (short) softc->dsreg;
1706 /*
1707 * Yes, we know that this is likely to overflow
1708 */
1709 if (softc->last_resid_was_io) {
1710 if ((g->mt_resid = (short) softc->last_io_resid) != 0) {
1711 if (SA_IS_CTRL(dev) == 0 || didlockperiph) {
1712 softc->last_io_resid = 0;
1713 }
1714 }
1715 } else {
1716 if ((g->mt_resid = (short)softc->last_ctl_resid) != 0) {
1717 if (SA_IS_CTRL(dev) == 0 || didlockperiph) {
1718 softc->last_ctl_resid = 0;
1719 }
1720 }
1721 }
1722 error = 0;
1723 break;
1724 }
1725 case MTIOCEXTGET:
1726 case MTIOCPARAMGET:
1727 {
1728 struct mtextget *g = (struct mtextget *)arg;
1729 char *tmpstr2;
1730 struct sbuf *sb;
1731
1732 /*
1733 * Report drive status using an XML format.
1734 */
1735
1736 /*
1737 * XXX KDM will dropping the lock cause any problems here?
1738 */
1739 cam_periph_unlock(periph);
1740 sb = sbuf_new(NULL, NULL, g->alloc_len, SBUF_FIXEDLEN);
1741 if (sb == NULL) {
1742 g->status = MT_EXT_GET_ERROR;
1743 snprintf(g->error_str, sizeof(g->error_str),
1744 "Unable to allocate %d bytes for status info",
1745 g->alloc_len);
1746 cam_periph_lock(periph);
1747 goto extget_bailout;
1748 }
1749 cam_periph_lock(periph);
1750
1751 if (cmd == MTIOCEXTGET)
1752 error = saextget(dev, periph, sb, g);
1753 else
1754 error = saparamget(softc, sb);
1755
1756 if (error != 0)
1757 goto extget_bailout;
1758
1759 error = sbuf_finish(sb);
1760 if (error == ENOMEM) {
1761 g->status = MT_EXT_GET_NEED_MORE_SPACE;
1762 error = 0;
1763 } else if (error != 0) {
1764 g->status = MT_EXT_GET_ERROR;
1765 snprintf(g->error_str, sizeof(g->error_str),
1766 "Error %d returned from sbuf_finish()", error);
1767 } else
1768 g->status = MT_EXT_GET_OK;
1769
1770 error = 0;
1771 tmpstr2 = sbuf_data(sb);
1772 g->fill_len = strlen(tmpstr2) + 1;
1773 cam_periph_unlock(periph);
1774
1775 error = copyout(tmpstr2, g->status_xml, g->fill_len);
1776
1777 cam_periph_lock(periph);
1778
1779 extget_bailout:
1780 sbuf_delete(sb);
1781 break;
1782 }
1783 case MTIOCPARAMSET:
1784 {
1785 struct mtsetlist list;
1786 struct mtparamset *ps = (struct mtparamset *)arg;
1787
1788 bzero(&list, sizeof(list));
1789 list.num_params = 1;
1790 list.param_len = sizeof(*ps);
1791 list.params = ps;
1792
1793 error = saparamsetlist(periph, &list, /*need_copy*/ 0);
1794 break;
1795 }
1796 case MTIOCSETLIST:
1797 {
1798 struct mtsetlist *list = (struct mtsetlist *)arg;
1799
1800 error = saparamsetlist(periph, list, /*need_copy*/ 1);
1801 break;
1802 }
1803 case MTIOCERRSTAT:
1804 {
1805 struct scsi_tape_errors *sep =
1806 &((union mterrstat *)arg)->scsi_errstat;
1807
1808 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE,
1809 ("saioctl: MTIOCERRSTAT\n"));
1810
1811 bzero(sep, sizeof(*sep));
1812 sep->io_resid = softc->last_io_resid;
1813 bcopy((caddr_t) &softc->last_io_sense, sep->io_sense,
1814 sizeof (sep->io_sense));
1815 bcopy((caddr_t) &softc->last_io_cdb, sep->io_cdb,
1816 sizeof (sep->io_cdb));
1817 sep->ctl_resid = softc->last_ctl_resid;
1818 bcopy((caddr_t) &softc->last_ctl_sense, sep->ctl_sense,
1819 sizeof (sep->ctl_sense));
1820 bcopy((caddr_t) &softc->last_ctl_cdb, sep->ctl_cdb,
1821 sizeof (sep->ctl_cdb));
1822
1823 if ((SA_IS_CTRL(dev) == 0 && !softc->open_pending_mount) ||
1824 didlockperiph)
1825 bzero((caddr_t) &softc->errinfo,
1826 sizeof (softc->errinfo));
1827 error = 0;
1828 break;
1829 }
1830 case MTIOCTOP:
1831 {
1832 struct mtop *mt;
1833 int count;
1834
1835 PENDING_MOUNT_CHECK(softc, periph, dev);
1836
1837 mt = (struct mtop *)arg;
1838
1839
1840 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE,
1841 ("saioctl: op=0x%x count=0x%x\n",
1842 mt->mt_op, mt->mt_count));
1843
1844 count = mt->mt_count;
1845 switch (mt->mt_op) {
1846 case MTWEOF: /* write an end-of-file marker */
1847 /*
1848 * We don't need to clear the SA_FLAG_TAPE_WRITTEN
1849 * flag because by keeping track of filemarks
1850 * we have last written we know whether or not
1851 * we need to write more when we close the device.
1852 */
1853 error = sawritefilemarks(periph, count, FALSE, FALSE);
1854 break;
1855 case MTWEOFI:
1856 /* write an end-of-file marker without waiting */
1857 error = sawritefilemarks(periph, count, FALSE, TRUE);
1858 break;
1859 case MTWSS: /* write a setmark */
1860 error = sawritefilemarks(periph, count, TRUE, FALSE);
1861 break;
1862 case MTBSR: /* backward space record */
1863 case MTFSR: /* forward space record */
1864 case MTBSF: /* backward space file */
1865 case MTFSF: /* forward space file */
1866 case MTBSS: /* backward space setmark */
1867 case MTFSS: /* forward space setmark */
1868 case MTEOD: /* space to end of recorded medium */
1869 {
1870 int nmarks;
1871
1872 spaceop = SS_FILEMARKS;
1873 nmarks = softc->filemarks;
1874 error = sacheckeod(periph);
1875 if (error) {
1876 xpt_print(periph->path,
1877 "EOD check prior to spacing failed\n");
1878 softc->flags |= SA_FLAG_EIO_PENDING;
1879 break;
1880 }
1881 nmarks -= softc->filemarks;
1882 switch(mt->mt_op) {
1883 case MTBSR:
1884 count = -count;
1885 /* FALLTHROUGH */
1886 case MTFSR:
1887 spaceop = SS_BLOCKS;
1888 break;
1889 case MTBSF:
1890 count = -count;
1891 /* FALLTHROUGH */
1892 case MTFSF:
1893 break;
1894 case MTBSS:
1895 count = -count;
1896 /* FALLTHROUGH */
1897 case MTFSS:
1898 spaceop = SS_SETMARKS;
1899 break;
1900 case MTEOD:
1901 spaceop = SS_EOD;
1902 count = 0;
1903 nmarks = 0;
1904 break;
1905 default:
1906 error = EINVAL;
1907 break;
1908 }
1909 if (error)
1910 break;
1911
1912 nmarks = softc->filemarks;
1913 /*
1914 * XXX: Why are we checking again?
1915 */
1916 error = sacheckeod(periph);
1917 if (error)
1918 break;
1919 nmarks -= softc->filemarks;
1920 error = saspace(periph, count - nmarks, spaceop);
1921 /*
1922 * At this point, clear that we've written the tape
1923 * and that we've written any filemarks. We really
1924 * don't know what the applications wishes to do next-
1925 * the sacheckeod's will make sure we terminated the
1926 * tape correctly if we'd been writing, but the next
1927 * action the user application takes will set again
1928 * whether we need to write filemarks.
1929 */
1930 softc->flags &=
1931 ~(SA_FLAG_TAPE_WRITTEN|SA_FLAG_TAPE_FROZEN);
1932 softc->filemarks = 0;
1933 break;
1934 }
1935 case MTREW: /* rewind */
1936 PENDING_MOUNT_CHECK(softc, periph, dev);
1937 (void) sacheckeod(periph);
1938 error = sarewind(periph);
1939 /* see above */
1940 softc->flags &=
1941 ~(SA_FLAG_TAPE_WRITTEN|SA_FLAG_TAPE_FROZEN);
1942 softc->flags &= ~SA_FLAG_ERR_PENDING;
1943 softc->filemarks = 0;
1944 break;
1945 case MTERASE: /* erase */
1946 PENDING_MOUNT_CHECK(softc, periph, dev);
1947 error = saerase(periph, count);
1948 softc->flags &=
1949 ~(SA_FLAG_TAPE_WRITTEN|SA_FLAG_TAPE_FROZEN);
1950 softc->flags &= ~SA_FLAG_ERR_PENDING;
1951 break;
1952 case MTRETENS: /* re-tension tape */
1953 PENDING_MOUNT_CHECK(softc, periph, dev);
1954 error = saretension(periph);
1955 softc->flags &=
1956 ~(SA_FLAG_TAPE_WRITTEN|SA_FLAG_TAPE_FROZEN);
1957 softc->flags &= ~SA_FLAG_ERR_PENDING;
1958 break;
1959 case MTOFFL: /* rewind and put the drive offline */
1960
1961 PENDING_MOUNT_CHECK(softc, periph, dev);
1962
1963 (void) sacheckeod(periph);
1964 /* see above */
1965 softc->flags &= ~SA_FLAG_TAPE_WRITTEN;
1966 softc->filemarks = 0;
1967
1968 error = sarewind(periph);
1969 /* clear the frozen flag anyway */
1970 softc->flags &= ~SA_FLAG_TAPE_FROZEN;
1971
1972 /*
1973 * Be sure to allow media removal before ejecting.
1974 */
1975
1976 saprevent(periph, PR_ALLOW);
1977 if (error == 0) {
1978 error = saloadunload(periph, FALSE);
1979 if (error == 0) {
1980 softc->flags &= ~SA_FLAG_TAPE_MOUNTED;
1981 }
1982 }
1983 break;
1984
1985 case MTLOAD:
1986 error = saloadunload(periph, TRUE);
1987 break;
1988 case MTNOP: /* no operation, sets status only */
1989 case MTCACHE: /* enable controller cache */
1990 case MTNOCACHE: /* disable controller cache */
1991 error = 0;
1992 break;
1993
1994 case MTSETBSIZ: /* Set block size for device */
1995
1996 PENDING_MOUNT_CHECK(softc, periph, dev);
1997
1998 if ((softc->sili != 0)
1999 && (count != 0)) {
2000 xpt_print(periph->path, "Can't enter fixed "
2001 "block mode with SILI enabled\n");
2002 error = EINVAL;
2003 break;
2004 }
2005 error = sasetparams(periph, SA_PARAM_BLOCKSIZE, count,
2006 0, 0, 0);
2007 if (error == 0) {
2008 softc->last_media_blksize =
2009 softc->media_blksize;
2010 softc->media_blksize = count;
2011 if (count) {
2012 softc->flags |= SA_FLAG_FIXED;
2013 if (powerof2(count)) {
2014 softc->blk_shift =
2015 ffs(count) - 1;
2016 softc->blk_mask = count - 1;
2017 } else {
2018 softc->blk_mask = ~0;
2019 softc->blk_shift = 0;
2020 }
2021 /*
2022 * Make the user's desire 'persistent'.
2023 */
2024 softc->quirks &= ~SA_QUIRK_VARIABLE;
2025 softc->quirks |= SA_QUIRK_FIXED;
2026 } else {
2027 softc->flags &= ~SA_FLAG_FIXED;
2028 if (softc->max_blk == 0) {
2029 softc->max_blk = ~0;
2030 }
2031 softc->blk_shift = 0;
2032 if (softc->blk_gran != 0) {
2033 softc->blk_mask =
2034 softc->blk_gran - 1;
2035 } else {
2036 softc->blk_mask = 0;
2037 }
2038 /*
2039 * Make the user's desire 'persistent'.
2040 */
2041 softc->quirks |= SA_QUIRK_VARIABLE;
2042 softc->quirks &= ~SA_QUIRK_FIXED;
2043 }
2044 }
2045 break;
2046 case MTSETDNSTY: /* Set density for device and mode */
2047 PENDING_MOUNT_CHECK(softc, periph, dev);
2048
2049 if (count > UCHAR_MAX) {
2050 error = EINVAL;
2051 break;
2052 } else {
2053 error = sasetparams(periph, SA_PARAM_DENSITY,
2054 0, count, 0, 0);
2055 }
2056 break;
2057 case MTCOMP: /* enable compression */
2058 PENDING_MOUNT_CHECK(softc, periph, dev);
2059 /*
2060 * Some devices don't support compression, and
2061 * don't like it if you ask them for the
2062 * compression page.
2063 */
2064 if ((softc->quirks & SA_QUIRK_NOCOMP) ||
2065 (softc->flags & SA_FLAG_COMP_UNSUPP)) {
2066 error = ENODEV;
2067 break;
2068 }
2069 error = sasetparams(periph, SA_PARAM_COMPRESSION,
2070 0, 0, count, SF_NO_PRINT);
2071 break;
2072 default:
2073 error = EINVAL;
2074 }
2075 break;
2076 }
2077 case MTIOCIEOT:
2078 case MTIOCEEOT:
2079 error = 0;
2080 break;
2081 case MTIOCRDSPOS:
2082 PENDING_MOUNT_CHECK(softc, periph, dev);
2083 error = sardpos(periph, 0, (u_int32_t *) arg);
2084 break;
2085 case MTIOCRDHPOS:
2086 PENDING_MOUNT_CHECK(softc, periph, dev);
2087 error = sardpos(periph, 1, (u_int32_t *) arg);
2088 break;
2089 case MTIOCSLOCATE:
2090 case MTIOCHLOCATE: {
2091 struct mtlocate locate_info;
2092 int hard;
2093
2094 bzero(&locate_info, sizeof(locate_info));
2095 locate_info.logical_id = *((uint32_t *)arg);
2096 if (cmd == MTIOCSLOCATE)
2097 hard = 0;
2098 else
2099 hard = 1;
2100
2101 PENDING_MOUNT_CHECK(softc, periph, dev);
2102
2103 error = sasetpos(periph, hard, &locate_info);
2104 break;
2105 }
2106 case MTIOCEXTLOCATE:
2107 PENDING_MOUNT_CHECK(softc, periph, dev);
2108 error = sasetpos(periph, /*hard*/ 0, (struct mtlocate *)arg);
2109 softc->flags &=
2110 ~(SA_FLAG_TAPE_WRITTEN|SA_FLAG_TAPE_FROZEN);
2111 softc->flags &= ~SA_FLAG_ERR_PENDING;
2112 softc->filemarks = 0;
2113 break;
2114 case MTIOCGETEOTMODEL:
2115 error = 0;
2116 if (softc->quirks & SA_QUIRK_1FM)
2117 mode = 1;
2118 else
2119 mode = 2;
2120 *((u_int32_t *) arg) = mode;
2121 break;
2122 case MTIOCSETEOTMODEL:
2123 error = 0;
2124 switch (*((u_int32_t *) arg)) {
2125 case 1:
2126 softc->quirks &= ~SA_QUIRK_2FM;
2127 softc->quirks |= SA_QUIRK_1FM;
2128 break;
2129 case 2:
2130 softc->quirks &= ~SA_QUIRK_1FM;
2131 softc->quirks |= SA_QUIRK_2FM;
2132 break;
2133 default:
2134 error = EINVAL;
2135 break;
2136 }
2137 break;
2138 case MTIOCRBLIM: {
2139 struct mtrblim *rblim;
2140
2141 rblim = (struct mtrblim *)arg;
2142
2143 rblim->granularity = softc->blk_gran;
2144 rblim->min_block_length = softc->min_blk;
2145 rblim->max_block_length = softc->max_blk;
2146 break;
2147 }
2148 default:
2149 error = cam_periph_ioctl(periph, cmd, arg, saerror);
2150 break;
2151 }
2152
2153 /*
2154 * Check to see if we cleared a frozen state
2155 */
2156 if (error == 0 && (softc->flags & SA_FLAG_TAPE_FROZEN)) {
2157 switch(cmd) {
2158 case MTIOCRDSPOS:
2159 case MTIOCRDHPOS:
2160 case MTIOCSLOCATE:
2161 case MTIOCHLOCATE:
2162 /*
2163 * XXX KDM look at this.
2164 */
2165 softc->fileno = (daddr_t) -1;
2166 softc->blkno = (daddr_t) -1;
2167 softc->rep_blkno = (daddr_t) -1;
2168 softc->rep_fileno = (daddr_t) -1;
2169 softc->partition = (daddr_t) -1;
2170 softc->flags &= ~SA_FLAG_TAPE_FROZEN;
2171 xpt_print(periph->path,
2172 "tape state now unfrozen.\n");
2173 break;
2174 default:
2175 break;
2176 }
2177 }
2178 if (didlockperiph) {
2179 cam_periph_unhold(periph);
2180 }
2181 cam_periph_unlock(periph);
2182 return (error);
2183 }
2184
2185 static void
sainit(void)2186 sainit(void)
2187 {
2188 cam_status status;
2189
2190 /*
2191 * Install a global async callback.
2192 */
2193 status = xpt_register_async(AC_FOUND_DEVICE, saasync, NULL, NULL);
2194
2195 if (status != CAM_REQ_CMP) {
2196 printf("sa: Failed to attach master async callback "
2197 "due to status 0x%x!\n", status);
2198 }
2199 }
2200
2201 static void
sadevgonecb(void * arg)2202 sadevgonecb(void *arg)
2203 {
2204 struct cam_periph *periph;
2205 struct mtx *mtx;
2206 struct sa_softc *softc;
2207
2208 periph = (struct cam_periph *)arg;
2209 softc = (struct sa_softc *)periph->softc;
2210
2211 mtx = cam_periph_mtx(periph);
2212 mtx_lock(mtx);
2213
2214 softc->num_devs_to_destroy--;
2215 if (softc->num_devs_to_destroy == 0) {
2216 int i;
2217
2218 /*
2219 * When we have gotten all of our callbacks, we will get
2220 * no more close calls from devfs. So if we have any
2221 * dangling opens, we need to release the reference held
2222 * for that particular context.
2223 */
2224 for (i = 0; i < softc->open_count; i++)
2225 cam_periph_release_locked(periph);
2226
2227 softc->open_count = 0;
2228
2229 /*
2230 * Release the reference held for devfs, all of our
2231 * instances are gone now.
2232 */
2233 cam_periph_release_locked(periph);
2234 }
2235
2236 /*
2237 * We reference the lock directly here, instead of using
2238 * cam_periph_unlock(). The reason is that the final call to
2239 * cam_periph_release_locked() above could result in the periph
2240 * getting freed. If that is the case, dereferencing the periph
2241 * with a cam_periph_unlock() call would cause a page fault.
2242 */
2243 mtx_unlock(mtx);
2244 }
2245
2246 static void
saoninvalidate(struct cam_periph * periph)2247 saoninvalidate(struct cam_periph *periph)
2248 {
2249 struct sa_softc *softc;
2250
2251 softc = (struct sa_softc *)periph->softc;
2252
2253 /*
2254 * De-register any async callbacks.
2255 */
2256 xpt_register_async(0, saasync, periph, periph->path);
2257
2258 softc->flags |= SA_FLAG_INVALID;
2259
2260 /*
2261 * Return all queued I/O with ENXIO.
2262 * XXX Handle any transactions queued to the card
2263 * with XPT_ABORT_CCB.
2264 */
2265 bioq_flush(&softc->bio_queue, NULL, ENXIO);
2266 softc->queue_count = 0;
2267
2268 /*
2269 * Tell devfs that all of our devices have gone away, and ask for a
2270 * callback when it has cleaned up its state.
2271 */
2272 destroy_dev_sched_cb(softc->devs.ctl_dev, sadevgonecb, periph);
2273 destroy_dev_sched_cb(softc->devs.r_dev, sadevgonecb, periph);
2274 destroy_dev_sched_cb(softc->devs.nr_dev, sadevgonecb, periph);
2275 destroy_dev_sched_cb(softc->devs.er_dev, sadevgonecb, periph);
2276 }
2277
2278 static void
sacleanup(struct cam_periph * periph)2279 sacleanup(struct cam_periph *periph)
2280 {
2281 struct sa_softc *softc;
2282
2283 softc = (struct sa_softc *)periph->softc;
2284
2285 cam_periph_unlock(periph);
2286
2287 if ((softc->flags & SA_FLAG_SCTX_INIT) != 0
2288 && (((softc->sysctl_timeout_tree != NULL)
2289 && (sysctl_ctx_free(&softc->sysctl_timeout_ctx) != 0))
2290 || sysctl_ctx_free(&softc->sysctl_ctx) != 0))
2291 xpt_print(periph->path, "can't remove sysctl context\n");
2292
2293 cam_periph_lock(periph);
2294
2295 devstat_remove_entry(softc->device_stats);
2296
2297 free(softc, M_SCSISA);
2298 }
2299
2300 static void
saasync(void * callback_arg,u_int32_t code,struct cam_path * path,void * arg)2301 saasync(void *callback_arg, u_int32_t code,
2302 struct cam_path *path, void *arg)
2303 {
2304 struct cam_periph *periph;
2305
2306 periph = (struct cam_periph *)callback_arg;
2307 switch (code) {
2308 case AC_FOUND_DEVICE:
2309 {
2310 struct ccb_getdev *cgd;
2311 cam_status status;
2312
2313 cgd = (struct ccb_getdev *)arg;
2314 if (cgd == NULL)
2315 break;
2316
2317 if (cgd->protocol != PROTO_SCSI)
2318 break;
2319 if (SID_QUAL(&cgd->inq_data) != SID_QUAL_LU_CONNECTED)
2320 break;
2321 if (SID_TYPE(&cgd->inq_data) != T_SEQUENTIAL)
2322 break;
2323
2324 /*
2325 * Allocate a peripheral instance for
2326 * this device and start the probe
2327 * process.
2328 */
2329 status = cam_periph_alloc(saregister, saoninvalidate,
2330 sacleanup, sastart,
2331 "sa", CAM_PERIPH_BIO, path,
2332 saasync, AC_FOUND_DEVICE, cgd);
2333
2334 if (status != CAM_REQ_CMP
2335 && status != CAM_REQ_INPROG)
2336 printf("saasync: Unable to probe new device "
2337 "due to status 0x%x\n", status);
2338 break;
2339 }
2340 default:
2341 cam_periph_async(periph, code, path, arg);
2342 break;
2343 }
2344 }
2345
2346 static void
sasetupdev(struct sa_softc * softc,struct cdev * dev)2347 sasetupdev(struct sa_softc *softc, struct cdev *dev)
2348 {
2349
2350 dev->si_iosize_max = softc->maxio;
2351 dev->si_flags |= softc->si_flags;
2352 /*
2353 * Keep a count of how many non-alias devices we have created,
2354 * so we can make sure we clean them all up on shutdown. Aliases
2355 * are cleaned up when we destroy the device they're an alias for.
2356 */
2357 if ((dev->si_flags & SI_ALIAS) == 0)
2358 softc->num_devs_to_destroy++;
2359 }
2360
2361 /*
2362 * Load the global (for all sa(4) instances) and per-instance tunable
2363 * values for timeouts for various sa(4) commands. This should be run
2364 * after the default timeouts are fetched from the drive, so the user's
2365 * preference will override the drive's defaults.
2366 */
2367 static void
saloadtotunables(struct sa_softc * softc)2368 saloadtotunables(struct sa_softc *softc)
2369 {
2370 int i;
2371 char tmpstr[80];
2372
2373 for (i = 0; i < SA_TIMEOUT_TYPE_MAX; i++) {
2374 int tmpval, retval;
2375
2376 /* First grab any global timeout setting */
2377 snprintf(tmpstr, sizeof(tmpstr), "kern.cam.sa.timeout.%s",
2378 sa_default_timeouts[i].desc);
2379 retval = TUNABLE_INT_FETCH(tmpstr, &tmpval);
2380 if (retval != 0)
2381 softc->timeout_info[i] = tmpval;
2382
2383 /*
2384 * Then overwrite any global timeout settings with
2385 * per-instance timeout settings.
2386 */
2387 snprintf(tmpstr, sizeof(tmpstr), "kern.cam.sa.%u.timeout.%s",
2388 softc->periph->unit_number, sa_default_timeouts[i].desc);
2389 retval = TUNABLE_INT_FETCH(tmpstr, &tmpval);
2390 if (retval != 0)
2391 softc->timeout_info[i] = tmpval;
2392 }
2393 }
2394
2395 static void
sasysctlinit(void * context,int pending)2396 sasysctlinit(void *context, int pending)
2397 {
2398 struct cam_periph *periph;
2399 struct sa_softc *softc;
2400 char tmpstr[64], tmpstr2[16];
2401 int i;
2402
2403 periph = (struct cam_periph *)context;
2404 /*
2405 * If the periph is invalid, no need to setup the sysctls.
2406 */
2407 if (periph->flags & CAM_PERIPH_INVALID)
2408 goto bailout;
2409
2410 softc = (struct sa_softc *)periph->softc;
2411
2412 snprintf(tmpstr, sizeof(tmpstr), "CAM SA unit %d", periph->unit_number);
2413 snprintf(tmpstr2, sizeof(tmpstr2), "%u", periph->unit_number);
2414
2415 sysctl_ctx_init(&softc->sysctl_ctx);
2416 softc->flags |= SA_FLAG_SCTX_INIT;
2417 softc->sysctl_tree = SYSCTL_ADD_NODE_WITH_LABEL(&softc->sysctl_ctx,
2418 SYSCTL_STATIC_CHILDREN(_kern_cam_sa), OID_AUTO, tmpstr2,
2419 CTLFLAG_RD, 0, tmpstr, "device_index");
2420 if (softc->sysctl_tree == NULL)
2421 goto bailout;
2422
2423 SYSCTL_ADD_INT(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree),
2424 OID_AUTO, "allow_io_split", CTLFLAG_RDTUN | CTLFLAG_NOFETCH,
2425 &softc->allow_io_split, 0, "Allow Splitting I/O");
2426 SYSCTL_ADD_INT(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree),
2427 OID_AUTO, "maxio", CTLFLAG_RD,
2428 &softc->maxio, 0, "Maximum I/O size");
2429 SYSCTL_ADD_INT(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree),
2430 OID_AUTO, "cpi_maxio", CTLFLAG_RD,
2431 &softc->cpi_maxio, 0, "Maximum Controller I/O size");
2432 SYSCTL_ADD_INT(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree),
2433 OID_AUTO, "inject_eom", CTLFLAG_RW,
2434 &softc->inject_eom, 0, "Queue EOM for the next write/read");
2435
2436 sysctl_ctx_init(&softc->sysctl_timeout_ctx);
2437 softc->sysctl_timeout_tree = SYSCTL_ADD_NODE(&softc->sysctl_timeout_ctx,
2438 SYSCTL_CHILDREN(softc->sysctl_tree), OID_AUTO, "timeout",
2439 CTLFLAG_RD | CTLFLAG_MPSAFE, 0, "Timeouts");
2440 if (softc->sysctl_timeout_tree == NULL)
2441 goto bailout;
2442
2443 for (i = 0; i < SA_TIMEOUT_TYPE_MAX; i++) {
2444 snprintf(tmpstr, sizeof(tmpstr), "%s timeout",
2445 sa_default_timeouts[i].desc);
2446
2447 /*
2448 * Do NOT change this sysctl declaration to also load any
2449 * tunable values for this sa(4) instance. In other words,
2450 * do not change this to CTLFLAG_RWTUN. This function is
2451 * run in parallel with the probe routine that fetches
2452 * recommended timeout values from the tape drive, and we
2453 * don't want the values from the drive to override the
2454 * user's preference.
2455 */
2456 SYSCTL_ADD_INT(&softc->sysctl_timeout_ctx,
2457 SYSCTL_CHILDREN(softc->sysctl_timeout_tree),
2458 OID_AUTO, sa_default_timeouts[i].desc, CTLFLAG_RW,
2459 &softc->timeout_info[i], 0, tmpstr);
2460 }
2461
2462 bailout:
2463 /*
2464 * Release the reference that was held when this task was enqueued.
2465 */
2466 cam_periph_release(periph);
2467 }
2468
2469 static cam_status
saregister(struct cam_periph * periph,void * arg)2470 saregister(struct cam_periph *periph, void *arg)
2471 {
2472 struct sa_softc *softc;
2473 struct ccb_getdev *cgd;
2474 struct ccb_pathinq cpi;
2475 struct make_dev_args args;
2476 caddr_t match;
2477 char tmpstr[80];
2478 int error;
2479 int i;
2480 cgd = (struct ccb_getdev *)arg;
2481 if (cgd == NULL) {
2482 printf("saregister: no getdev CCB, can't register device\n");
2483 return (CAM_REQ_CMP_ERR);
2484 }
2485
2486 softc = (struct sa_softc *)
2487 malloc(sizeof (*softc), M_SCSISA, M_NOWAIT | M_ZERO);
2488 if (softc == NULL) {
2489 printf("saregister: Unable to probe new device. "
2490 "Unable to allocate softc\n");
2491 return (CAM_REQ_CMP_ERR);
2492 }
2493 softc->scsi_rev = SID_ANSI_REV(&cgd->inq_data);
2494 softc->state = SA_STATE_NORMAL;
2495 softc->fileno = (daddr_t) -1;
2496 softc->blkno = (daddr_t) -1;
2497 softc->rep_fileno = (daddr_t) -1;
2498 softc->rep_blkno = (daddr_t) -1;
2499 softc->partition = (daddr_t) -1;
2500 softc->bop = -1;
2501 softc->eop = -1;
2502 softc->bpew = -1;
2503
2504 bioq_init(&softc->bio_queue);
2505 softc->periph = periph;
2506 periph->softc = softc;
2507
2508 /*
2509 * See if this device has any quirks.
2510 */
2511 match = cam_quirkmatch((caddr_t)&cgd->inq_data,
2512 (caddr_t)sa_quirk_table,
2513 nitems(sa_quirk_table),
2514 sizeof(*sa_quirk_table), scsi_inquiry_match);
2515
2516 if (match != NULL) {
2517 softc->quirks = ((struct sa_quirk_entry *)match)->quirks;
2518 softc->last_media_blksize =
2519 ((struct sa_quirk_entry *)match)->prefblk;
2520 } else
2521 softc->quirks = SA_QUIRK_NONE;
2522
2523
2524 /*
2525 * Initialize the default timeouts. If this drive supports
2526 * timeout descriptors we'll overwrite these values with the
2527 * recommended timeouts from the drive.
2528 */
2529 for (i = 0; i < SA_TIMEOUT_TYPE_MAX; i++)
2530 softc->timeout_info[i] = sa_default_timeouts[i].value;
2531
2532 /*
2533 * Long format data for READ POSITION was introduced in SSC, which
2534 * was after SCSI-2. (Roughly equivalent to SCSI-3.) If the drive
2535 * reports that it is SCSI-2 or older, it is unlikely to support
2536 * long position data, but it might. Some drives from that era
2537 * claim to be SCSI-2, but do support long position information.
2538 * So, instead of immediately disabling long position information
2539 * for SCSI-2 devices, we'll try one pass through sagetpos(), and
2540 * then disable long position information if we get an error.
2541 */
2542 if (cgd->inq_data.version <= SCSI_REV_CCS)
2543 softc->quirks |= SA_QUIRK_NO_LONG_POS;
2544
2545 /*
2546 * The SCSI REPORT SUPPORTED OPERATION CODES command was added in
2547 * SPC-4. That command optionally includes timeout data for
2548 * different commands. Timeout values can vary wildly among
2549 * different drives, so if the drive itself has recommended values,
2550 * we will try to use them. Set this flag to indicate we're going
2551 * to ask the drive for timeout data. This flag also tells us to
2552 * wait on loading timeout tunables so we can properly override
2553 * timeouts with any user-specified values.
2554 */
2555 if (SID_ANSI_REV(&cgd->inq_data) >= SCSI_REV_SPC4)
2556 softc->flags |= SA_FLAG_RSOC_TO_TRY;
2557
2558 if (cgd->inq_data.spc3_flags & SPC3_SID_PROTECT) {
2559 struct ccb_dev_advinfo cdai;
2560 struct scsi_vpd_extended_inquiry_data ext_inq;
2561
2562 bzero(&ext_inq, sizeof(ext_inq));
2563
2564 xpt_setup_ccb(&cdai.ccb_h, periph->path, CAM_PRIORITY_NORMAL);
2565
2566 cdai.ccb_h.func_code = XPT_DEV_ADVINFO;
2567 cdai.flags = CDAI_FLAG_NONE;
2568 cdai.buftype = CDAI_TYPE_EXT_INQ;
2569 cdai.bufsiz = sizeof(ext_inq);
2570 cdai.buf = (uint8_t *)&ext_inq;
2571 xpt_action((union ccb *)&cdai);
2572
2573 if ((cdai.ccb_h.status & CAM_DEV_QFRZN) != 0)
2574 cam_release_devq(cdai.ccb_h.path, 0, 0, 0, FALSE);
2575 if ((cdai.ccb_h.status == CAM_REQ_CMP)
2576 && (ext_inq.flags1 & SVPD_EID_SA_SPT_LBP))
2577 softc->flags |= SA_FLAG_PROTECT_SUPP;
2578 }
2579
2580 xpt_path_inq(&cpi, periph->path);
2581
2582 /*
2583 * The SA driver supports a blocksize, but we don't know the
2584 * blocksize until we media is inserted. So, set a flag to
2585 * indicate that the blocksize is unavailable right now.
2586 */
2587 cam_periph_unlock(periph);
2588 softc->device_stats = devstat_new_entry("sa", periph->unit_number, 0,
2589 DEVSTAT_BS_UNAVAILABLE, SID_TYPE(&cgd->inq_data) |
2590 XPORT_DEVSTAT_TYPE(cpi.transport), DEVSTAT_PRIORITY_TAPE);
2591
2592 /*
2593 * Load the default value that is either compiled in, or loaded
2594 * in the global kern.cam.sa.allow_io_split tunable.
2595 */
2596 softc->allow_io_split = sa_allow_io_split;
2597
2598 /*
2599 * Load a per-instance tunable, if it exists. NOTE that this
2600 * tunable WILL GO AWAY in FreeBSD 11.0.
2601 */
2602 snprintf(tmpstr, sizeof(tmpstr), "kern.cam.sa.%u.allow_io_split",
2603 periph->unit_number);
2604 TUNABLE_INT_FETCH(tmpstr, &softc->allow_io_split);
2605
2606 /*
2607 * If maxio isn't set, we fall back to DFLTPHYS. Otherwise we take
2608 * the smaller of cpi.maxio or MAXPHYS.
2609 */
2610 if (cpi.maxio == 0)
2611 softc->maxio = DFLTPHYS;
2612 else if (cpi.maxio > MAXPHYS)
2613 softc->maxio = MAXPHYS;
2614 else
2615 softc->maxio = cpi.maxio;
2616
2617 /*
2618 * Record the controller's maximum I/O size so we can report it to
2619 * the user later.
2620 */
2621 softc->cpi_maxio = cpi.maxio;
2622
2623 /*
2624 * By default we tell physio that we do not want our I/O split.
2625 * The user needs to have a 1:1 mapping between the size of his
2626 * write to a tape character device and the size of the write
2627 * that actually goes down to the drive.
2628 */
2629 if (softc->allow_io_split == 0)
2630 softc->si_flags = SI_NOSPLIT;
2631 else
2632 softc->si_flags = 0;
2633
2634 TASK_INIT(&softc->sysctl_task, 0, sasysctlinit, periph);
2635
2636 /*
2637 * If the SIM supports unmapped I/O, let physio know that we can
2638 * handle unmapped buffers.
2639 */
2640 if (cpi.hba_misc & PIM_UNMAPPED)
2641 softc->si_flags |= SI_UNMAPPED;
2642
2643 /*
2644 * Acquire a reference to the periph before we create the devfs
2645 * instances for it. We'll release this reference once the devfs
2646 * instances have been freed.
2647 */
2648 if (cam_periph_acquire(periph) != 0) {
2649 xpt_print(periph->path, "%s: lost periph during "
2650 "registration!\n", __func__);
2651 cam_periph_lock(periph);
2652 return (CAM_REQ_CMP_ERR);
2653 }
2654
2655 make_dev_args_init(&args);
2656 args.mda_devsw = &sa_cdevsw;
2657 args.mda_si_drv1 = softc->periph;
2658 args.mda_uid = UID_ROOT;
2659 args.mda_gid = GID_OPERATOR;
2660 args.mda_mode = 0660;
2661
2662 args.mda_unit = SAMINOR(SA_CTLDEV, SA_ATYPE_R);
2663 error = make_dev_s(&args, &softc->devs.ctl_dev, "%s%d.ctl",
2664 periph->periph_name, periph->unit_number);
2665 if (error != 0) {
2666 cam_periph_lock(periph);
2667 return (CAM_REQ_CMP_ERR);
2668 }
2669 sasetupdev(softc, softc->devs.ctl_dev);
2670
2671 args.mda_unit = SAMINOR(SA_NOT_CTLDEV, SA_ATYPE_R);
2672 error = make_dev_s(&args, &softc->devs.r_dev, "%s%d",
2673 periph->periph_name, periph->unit_number);
2674 if (error != 0) {
2675 cam_periph_lock(periph);
2676 return (CAM_REQ_CMP_ERR);
2677 }
2678 sasetupdev(softc, softc->devs.r_dev);
2679
2680 args.mda_unit = SAMINOR(SA_NOT_CTLDEV, SA_ATYPE_NR);
2681 error = make_dev_s(&args, &softc->devs.nr_dev, "n%s%d",
2682 periph->periph_name, periph->unit_number);
2683 if (error != 0) {
2684 cam_periph_lock(periph);
2685 return (CAM_REQ_CMP_ERR);
2686 }
2687 sasetupdev(softc, softc->devs.nr_dev);
2688
2689 args.mda_unit = SAMINOR(SA_NOT_CTLDEV, SA_ATYPE_ER);
2690 error = make_dev_s(&args, &softc->devs.er_dev, "e%s%d",
2691 periph->periph_name, periph->unit_number);
2692 if (error != 0) {
2693 cam_periph_lock(periph);
2694 return (CAM_REQ_CMP_ERR);
2695 }
2696 sasetupdev(softc, softc->devs.er_dev);
2697
2698 cam_periph_lock(periph);
2699
2700 softc->density_type_bits[0] = 0;
2701 softc->density_type_bits[1] = SRDS_MEDIA;
2702 softc->density_type_bits[2] = SRDS_MEDIUM_TYPE;
2703 softc->density_type_bits[3] = SRDS_MEDIUM_TYPE | SRDS_MEDIA;
2704 /*
2705 * Bump the peripheral refcount for the sysctl thread, in case we
2706 * get invalidated before the thread has a chance to run. Note
2707 * that this runs in parallel with the probe for the timeout
2708 * values.
2709 */
2710 cam_periph_acquire(periph);
2711 taskqueue_enqueue(taskqueue_thread, &softc->sysctl_task);
2712
2713 /*
2714 * Add an async callback so that we get
2715 * notified if this device goes away.
2716 */
2717 xpt_register_async(AC_LOST_DEVICE, saasync, periph, periph->path);
2718
2719 /*
2720 * See comment above, try fetching timeout values for drives that
2721 * might support it. Otherwise, use the defaults.
2722 *
2723 * We get timeouts from the following places in order of increasing
2724 * priority:
2725 * 1. Driver default timeouts.
2726 * 2. Timeouts loaded from the drive via REPORT SUPPORTED OPERATION
2727 * CODES. (We kick that off here if SA_FLAG_RSOC_TO_TRY is set.)
2728 * 3. Global loader tunables, used for all sa(4) driver instances on
2729 * a machine.
2730 * 4. Instance-specific loader tunables, used for say sa5.
2731 * 5. On the fly user sysctl changes.
2732 *
2733 * Each step will overwrite the timeout value set from the one
2734 * before, so you go from general to most specific.
2735 */
2736 if (softc->flags & SA_FLAG_RSOC_TO_TRY) {
2737 /*
2738 * Bump the peripheral refcount while we are probing.
2739 */
2740 cam_periph_acquire(periph);
2741 softc->state = SA_STATE_PROBE;
2742 xpt_schedule(periph, CAM_PRIORITY_DEV);
2743 } else {
2744 /*
2745 * This drive doesn't support Report Supported Operation
2746 * Codes, so we load the tunables at this point to bring
2747 * in any user preferences.
2748 */
2749 saloadtotunables(softc);
2750
2751 xpt_announce_periph(periph, NULL);
2752 xpt_announce_quirks(periph, softc->quirks, SA_QUIRK_BIT_STRING);
2753 }
2754
2755 return (CAM_REQ_CMP);
2756 }
2757
2758 static void
sastart(struct cam_periph * periph,union ccb * start_ccb)2759 sastart(struct cam_periph *periph, union ccb *start_ccb)
2760 {
2761 struct sa_softc *softc;
2762
2763 softc = (struct sa_softc *)periph->softc;
2764
2765 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("sastart\n"));
2766
2767
2768 switch (softc->state) {
2769 case SA_STATE_NORMAL:
2770 {
2771 /* Pull a buffer from the queue and get going on it */
2772 struct bio *bp;
2773
2774 /*
2775 * See if there is a buf with work for us to do..
2776 */
2777 bp = bioq_first(&softc->bio_queue);
2778 if (bp == NULL) {
2779 xpt_release_ccb(start_ccb);
2780 } else if (((softc->flags & SA_FLAG_ERR_PENDING) != 0)
2781 || (softc->inject_eom != 0)) {
2782 struct bio *done_bp;
2783
2784 if (softc->inject_eom != 0) {
2785 softc->flags |= SA_FLAG_EOM_PENDING;
2786 softc->inject_eom = 0;
2787 /*
2788 * If we're injecting EOM for writes, we
2789 * need to keep PEWS set for 3 queries
2790 * to cover 2 position requests from the
2791 * kernel via sagetpos(), and then allow
2792 * for one for the user to see the BPEW
2793 * flag (e.g. via mt status). After that,
2794 * it will be cleared.
2795 */
2796 if (bp->bio_cmd == BIO_WRITE)
2797 softc->set_pews_status = 3;
2798 else
2799 softc->set_pews_status = 1;
2800 }
2801 again:
2802 softc->queue_count--;
2803 bioq_remove(&softc->bio_queue, bp);
2804 bp->bio_resid = bp->bio_bcount;
2805 done_bp = bp;
2806 if ((softc->flags & SA_FLAG_EOM_PENDING) != 0) {
2807 /*
2808 * We have two different behaviors for
2809 * writes when we hit either Early Warning
2810 * or the PEWZ (Programmable Early Warning
2811 * Zone). The default behavior is that
2812 * for all writes that are currently
2813 * queued after the write where we saw the
2814 * early warning, we will return the write
2815 * with the residual equal to the count.
2816 * i.e. tell the application that 0 bytes
2817 * were written.
2818 *
2819 * The alternate behavior, which is enabled
2820 * when eot_warn is set, is that in
2821 * addition to setting the residual equal
2822 * to the count, we will set the error
2823 * to ENOSPC.
2824 *
2825 * In either case, once queued writes are
2826 * cleared out, we clear the error flag
2827 * (see below) and the application is free to
2828 * attempt to write more.
2829 */
2830 if (softc->eot_warn != 0) {
2831 bp->bio_flags |= BIO_ERROR;
2832 bp->bio_error = ENOSPC;
2833 } else
2834 bp->bio_error = 0;
2835 } else if ((softc->flags & SA_FLAG_EOF_PENDING) != 0) {
2836 /*
2837 * This can only happen if we're reading
2838 * in fixed length mode. In this case,
2839 * we dump the rest of the list the
2840 * same way.
2841 */
2842 bp->bio_error = 0;
2843 if (bioq_first(&softc->bio_queue) != NULL) {
2844 biodone(done_bp);
2845 goto again;
2846 }
2847 } else if ((softc->flags & SA_FLAG_EIO_PENDING) != 0) {
2848 bp->bio_error = EIO;
2849 bp->bio_flags |= BIO_ERROR;
2850 }
2851 bp = bioq_first(&softc->bio_queue);
2852 /*
2853 * Only if we have no other buffers queued up
2854 * do we clear the pending error flag.
2855 */
2856 if (bp == NULL)
2857 softc->flags &= ~SA_FLAG_ERR_PENDING;
2858 CAM_DEBUG(periph->path, CAM_DEBUG_INFO,
2859 ("sastart- ERR_PENDING now 0x%x, bp is %sNULL, "
2860 "%d more buffers queued up\n",
2861 (softc->flags & SA_FLAG_ERR_PENDING),
2862 (bp != NULL)? "not " : " ", softc->queue_count));
2863 xpt_release_ccb(start_ccb);
2864 biodone(done_bp);
2865 } else {
2866 u_int32_t length;
2867
2868 bioq_remove(&softc->bio_queue, bp);
2869 softc->queue_count--;
2870
2871 length = bp->bio_bcount;
2872
2873 if ((softc->flags & SA_FLAG_FIXED) != 0) {
2874 if (softc->blk_shift != 0) {
2875 length = length >> softc->blk_shift;
2876 } else if (softc->media_blksize != 0) {
2877 length = length / softc->media_blksize;
2878 } else {
2879 bp->bio_error = EIO;
2880 xpt_print(periph->path, "zero blocksize"
2881 " for FIXED length writes?\n");
2882 biodone(bp);
2883 break;
2884 }
2885 #if 0
2886 CAM_DEBUG(start_ccb->ccb_h.path, CAM_DEBUG_INFO,
2887 ("issuing a %d fixed record %s\n",
2888 length, (bp->bio_cmd == BIO_READ)? "read" :
2889 "write"));
2890 #endif
2891 } else {
2892 #if 0
2893 CAM_DEBUG(start_ccb->ccb_h.path, CAM_DEBUG_INFO,
2894 ("issuing a %d variable byte %s\n",
2895 length, (bp->bio_cmd == BIO_READ)? "read" :
2896 "write"));
2897 #endif
2898 }
2899 devstat_start_transaction_bio(softc->device_stats, bp);
2900 /*
2901 * Some people have theorized that we should
2902 * suppress illegal length indication if we are
2903 * running in variable block mode so that we don't
2904 * have to request sense every time our requested
2905 * block size is larger than the written block.
2906 * The residual information from the ccb allows
2907 * us to identify this situation anyway. The only
2908 * problem with this is that we will not get
2909 * information about blocks that are larger than
2910 * our read buffer unless we set the block size
2911 * in the mode page to something other than 0.
2912 *
2913 * I believe that this is a non-issue. If user apps
2914 * don't adjust their read size to match our record
2915 * size, that's just life. Anyway, the typical usage
2916 * would be to issue, e.g., 64KB reads and occasionally
2917 * have to do deal with 512 byte or 1KB intermediate
2918 * records.
2919 *
2920 * That said, though, we now support setting the
2921 * SILI bit on reads, and we set the blocksize to 4
2922 * bytes when we do that. This gives us
2923 * compatibility with software that wants this,
2924 * although the only real difference between that
2925 * and not setting the SILI bit on reads is that we
2926 * won't get a check condition on reads where our
2927 * request size is larger than the block on tape.
2928 * That probably only makes a real difference in
2929 * non-packetized SCSI, where you have to go back
2930 * to the drive to request sense and thus incur
2931 * more latency.
2932 */
2933 softc->dsreg = (bp->bio_cmd == BIO_READ)?
2934 MTIO_DSREG_RD : MTIO_DSREG_WR;
2935 scsi_sa_read_write(&start_ccb->csio, 0, sadone,
2936 MSG_SIMPLE_Q_TAG, (bp->bio_cmd == BIO_READ ?
2937 SCSI_RW_READ : SCSI_RW_WRITE) |
2938 ((bp->bio_flags & BIO_UNMAPPED) != 0 ?
2939 SCSI_RW_BIO : 0), softc->sili,
2940 (softc->flags & SA_FLAG_FIXED) != 0, length,
2941 (bp->bio_flags & BIO_UNMAPPED) != 0 ? (void *)bp :
2942 bp->bio_data, bp->bio_bcount, SSD_FULL_SIZE,
2943 (bp->bio_cmd == BIO_READ) ?
2944 softc->timeout_info[SA_TIMEOUT_READ] :
2945 softc->timeout_info[SA_TIMEOUT_WRITE]);
2946 start_ccb->ccb_h.ccb_pflags &= ~SA_POSITION_UPDATED;
2947 start_ccb->ccb_h.ccb_bp = bp;
2948 bp = bioq_first(&softc->bio_queue);
2949 xpt_action(start_ccb);
2950 }
2951
2952 if (bp != NULL) {
2953 /* Have more work to do, so ensure we stay scheduled */
2954 xpt_schedule(periph, CAM_PRIORITY_NORMAL);
2955 }
2956 break;
2957 }
2958 case SA_STATE_PROBE: {
2959 int num_opcodes;
2960 size_t alloc_len;
2961 uint8_t *params;
2962
2963 /*
2964 * This is an arbitrary number. An IBM LTO-6 drive reports
2965 * 67 entries, and an IBM LTO-9 drive reports 71 entries.
2966 * There can theoretically be more than 256 because
2967 * service actions of a particular opcode are reported
2968 * separately, but we're far enough ahead of the practical
2969 * number here that we don't need to implement logic to
2970 * retry if we don't get all the timeout descriptors.
2971 */
2972 num_opcodes = 256;
2973
2974 alloc_len = num_opcodes *
2975 (sizeof(struct scsi_report_supported_opcodes_descr) +
2976 sizeof(struct scsi_report_supported_opcodes_timeout));
2977
2978 params = malloc(alloc_len, M_SCSISA, M_NOWAIT| M_ZERO);
2979 if (params == NULL) {
2980 /*
2981 * If this happens, go with default
2982 * timeouts and announce the drive.
2983 */
2984 saloadtotunables(softc);
2985
2986 softc->state = SA_STATE_NORMAL;
2987
2988 xpt_announce_periph(periph, NULL);
2989 xpt_announce_quirks(periph, softc->quirks,
2990 SA_QUIRK_BIT_STRING);
2991 xpt_release_ccb(start_ccb);
2992 cam_periph_release_locked(periph);
2993 return;
2994 }
2995
2996 scsi_report_supported_opcodes(&start_ccb->csio,
2997 /*retries*/ 3,
2998 /*cbfcnp*/ sadone,
2999 /*tag_action*/ MSG_SIMPLE_Q_TAG,
3000 /*options*/ RSO_RCTD,
3001 /*req_opcode*/ 0,
3002 /*req_service_action*/ 0,
3003 /*data_ptr*/ params,
3004 /*dxfer_len*/ alloc_len,
3005 /*sense_len*/ SSD_FULL_SIZE,
3006 /*timeout*/ softc->timeout_info[SA_TIMEOUT_TUR]);
3007
3008 xpt_action(start_ccb);
3009 break;
3010 }
3011 case SA_STATE_ABNORMAL:
3012 default:
3013 panic("state 0x%x in sastart", softc->state);
3014 break;
3015 }
3016 }
3017
3018
3019 static void
sadone(struct cam_periph * periph,union ccb * done_ccb)3020 sadone(struct cam_periph *periph, union ccb *done_ccb)
3021 {
3022 struct sa_softc *softc;
3023 struct ccb_scsiio *csio;
3024 struct bio *bp;
3025 int error;
3026
3027 softc = (struct sa_softc *)periph->softc;
3028 csio = &done_ccb->csio;
3029 error = 0;
3030
3031 if (softc->state == SA_STATE_NORMAL) {
3032 softc->dsreg = MTIO_DSREG_REST;
3033 bp = (struct bio *)done_ccb->ccb_h.ccb_bp;
3034
3035 if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
3036 if ((error = saerror(done_ccb, 0, 0)) == ERESTART) {
3037 /*
3038 * A retry was scheduled, so just return.
3039 */
3040 return;
3041 }
3042 }
3043 } else if (softc->state == SA_STATE_PROBE) {
3044 bp = NULL;
3045 if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
3046 /*
3047 * Note that on probe, we just run through
3048 * cam_periph_error(), since saerror() has a lot of
3049 * special handling for I/O errors. We don't need
3050 * that to get the opcodes. We either succeed
3051 * after a retry or two, or give up. We don't
3052 * print sense, we don't need to worry the user if
3053 * this drive doesn't support timeout descriptors.
3054 */
3055 if ((error = cam_periph_error(done_ccb, 0,
3056 SF_NO_PRINT)) == ERESTART) {
3057 /*
3058 * A retry was scheduled, so just return.
3059 */
3060 return;
3061 } else if (error != 0) {
3062 /* We failed to get opcodes. Give up. */
3063
3064 saloadtotunables(softc);
3065
3066 softc->state = SA_STATE_NORMAL;
3067
3068 xpt_release_ccb(done_ccb);
3069
3070 xpt_announce_periph(periph, NULL);
3071 xpt_announce_quirks(periph, softc->quirks,
3072 SA_QUIRK_BIT_STRING);
3073 cam_periph_release_locked(periph);
3074 return;
3075 }
3076 }
3077 /*
3078 * At this point, we have succeeded, so load the timeouts
3079 * and go into the normal state.
3080 */
3081 softc->state = SA_STATE_NORMAL;
3082
3083 /*
3084 * First, load the timeouts we got from the drive.
3085 */
3086 saloadtimeouts(softc, done_ccb);
3087
3088 /*
3089 * Next, overwrite the timeouts from the drive with any
3090 * loader tunables that the user set.
3091 */
3092 saloadtotunables(softc);
3093
3094 xpt_release_ccb(done_ccb);
3095 xpt_announce_periph(periph, NULL);
3096 xpt_announce_quirks(periph, softc->quirks,
3097 SA_QUIRK_BIT_STRING);
3098 cam_periph_release_locked(periph);
3099 return;
3100 } else {
3101 panic("state 0x%x in sadone", softc->state);
3102 }
3103
3104 if (error == EIO) {
3105
3106 /*
3107 * Catastrophic error. Mark the tape as frozen
3108 * (we no longer know tape position).
3109 *
3110 * Return all queued I/O with EIO, and unfreeze
3111 * our queue so that future transactions that
3112 * attempt to fix this problem can get to the
3113 * device.
3114 *
3115 */
3116
3117 softc->flags |= SA_FLAG_TAPE_FROZEN;
3118 bioq_flush(&softc->bio_queue, NULL, EIO);
3119 }
3120 if (error != 0) {
3121 bp->bio_resid = bp->bio_bcount;
3122 bp->bio_error = error;
3123 bp->bio_flags |= BIO_ERROR;
3124 /*
3125 * In the error case, position is updated in saerror.
3126 */
3127 } else {
3128 bp->bio_resid = csio->resid;
3129 bp->bio_error = 0;
3130 if (csio->resid != 0) {
3131 bp->bio_flags |= BIO_ERROR;
3132 }
3133 if (bp->bio_cmd == BIO_WRITE) {
3134 softc->flags |= SA_FLAG_TAPE_WRITTEN;
3135 softc->filemarks = 0;
3136 }
3137 if (!(csio->ccb_h.ccb_pflags & SA_POSITION_UPDATED) &&
3138 (softc->blkno != (daddr_t) -1)) {
3139 if ((softc->flags & SA_FLAG_FIXED) != 0) {
3140 u_int32_t l;
3141 if (softc->blk_shift != 0) {
3142 l = bp->bio_bcount >>
3143 softc->blk_shift;
3144 } else {
3145 l = bp->bio_bcount /
3146 softc->media_blksize;
3147 }
3148 softc->blkno += (daddr_t) l;
3149 } else {
3150 softc->blkno++;
3151 }
3152 }
3153 }
3154 /*
3155 * If we had an error (immediate or pending),
3156 * release the device queue now.
3157 */
3158 if (error || (softc->flags & SA_FLAG_ERR_PENDING))
3159 cam_release_devq(done_ccb->ccb_h.path, 0, 0, 0, 0);
3160 if (error || bp->bio_resid) {
3161 CAM_DEBUG(periph->path, CAM_DEBUG_INFO,
3162 ("error %d resid %ld count %ld\n", error,
3163 bp->bio_resid, bp->bio_bcount));
3164 }
3165 biofinish(bp, softc->device_stats, 0);
3166 xpt_release_ccb(done_ccb);
3167 }
3168
3169 /*
3170 * Mount the tape (make sure it's ready for I/O).
3171 */
3172 static int
samount(struct cam_periph * periph,int oflags,struct cdev * dev)3173 samount(struct cam_periph *periph, int oflags, struct cdev *dev)
3174 {
3175 struct sa_softc *softc;
3176 union ccb *ccb;
3177 int error;
3178
3179 /*
3180 * oflags can be checked for 'kind' of open (read-only check) - later
3181 * dev can be checked for a control-mode or compression open - later
3182 */
3183 UNUSED_PARAMETER(oflags);
3184 UNUSED_PARAMETER(dev);
3185
3186
3187 softc = (struct sa_softc *)periph->softc;
3188
3189 /*
3190 * This should determine if something has happened since the last
3191 * open/mount that would invalidate the mount. We do *not* want
3192 * to retry this command- we just want the status. But we only
3193 * do this if we're mounted already- if we're not mounted,
3194 * we don't care about the unit read state and can instead use
3195 * this opportunity to attempt to reserve the tape unit.
3196 */
3197
3198 if (softc->flags & SA_FLAG_TAPE_MOUNTED) {
3199 ccb = cam_periph_getccb(periph, 1);
3200 scsi_test_unit_ready(&ccb->csio, 0, NULL,
3201 MSG_SIMPLE_Q_TAG, SSD_FULL_SIZE,
3202 softc->timeout_info[SA_TIMEOUT_TUR]);
3203 error = cam_periph_runccb(ccb, saerror, 0, SF_NO_PRINT,
3204 softc->device_stats);
3205 if (error == ENXIO) {
3206 softc->flags &= ~SA_FLAG_TAPE_MOUNTED;
3207 scsi_test_unit_ready(&ccb->csio, 0, NULL,
3208 MSG_SIMPLE_Q_TAG, SSD_FULL_SIZE,
3209 softc->timeout_info[SA_TIMEOUT_TUR]);
3210 error = cam_periph_runccb(ccb, saerror, 0, SF_NO_PRINT,
3211 softc->device_stats);
3212 } else if (error) {
3213 /*
3214 * We don't need to freeze the tape because we
3215 * will now attempt to rewind/load it.
3216 */
3217 softc->flags &= ~SA_FLAG_TAPE_MOUNTED;
3218 if (CAM_DEBUGGED(periph->path, CAM_DEBUG_INFO)) {
3219 xpt_print(periph->path,
3220 "error %d on TUR in samount\n", error);
3221 }
3222 }
3223 } else {
3224 error = sareservereleaseunit(periph, TRUE);
3225 if (error) {
3226 return (error);
3227 }
3228 ccb = cam_periph_getccb(periph, 1);
3229 scsi_test_unit_ready(&ccb->csio, 0, NULL,
3230 MSG_SIMPLE_Q_TAG, SSD_FULL_SIZE,
3231 softc->timeout_info[SA_TIMEOUT_TUR]);
3232 error = cam_periph_runccb(ccb, saerror, 0, SF_NO_PRINT,
3233 softc->device_stats);
3234 }
3235
3236 if ((softc->flags & SA_FLAG_TAPE_MOUNTED) == 0) {
3237 struct scsi_read_block_limits_data *rblim = NULL;
3238 int comp_enabled, comp_supported;
3239 u_int8_t write_protect, guessing = 0;
3240
3241 /*
3242 * Clear out old state.
3243 */
3244 softc->flags &= ~(SA_FLAG_TAPE_WP|SA_FLAG_TAPE_WRITTEN|
3245 SA_FLAG_ERR_PENDING|SA_FLAG_COMPRESSION);
3246 softc->filemarks = 0;
3247
3248 /*
3249 * *Very* first off, make sure we're loaded to BOT.
3250 */
3251 scsi_load_unload(&ccb->csio, 2, NULL, MSG_SIMPLE_Q_TAG, FALSE,
3252 FALSE, FALSE, 1, SSD_FULL_SIZE,
3253 softc->timeout_info[SA_TIMEOUT_LOAD]);
3254 error = cam_periph_runccb(ccb, saerror, 0, SF_NO_PRINT,
3255 softc->device_stats);
3256
3257 /*
3258 * In case this doesn't work, do a REWIND instead
3259 */
3260 if (error) {
3261 scsi_rewind(&ccb->csio, 2, NULL, MSG_SIMPLE_Q_TAG,
3262 FALSE, SSD_FULL_SIZE,
3263 softc->timeout_info[SA_TIMEOUT_REWIND]);
3264 error = cam_periph_runccb(ccb, saerror, 0, SF_NO_PRINT,
3265 softc->device_stats);
3266 }
3267 if (error) {
3268 xpt_release_ccb(ccb);
3269 goto exit;
3270 }
3271
3272 /*
3273 * Do a dummy test read to force access to the
3274 * media so that the drive will really know what's
3275 * there. We actually don't really care what the
3276 * blocksize on tape is and don't expect to really
3277 * read a full record.
3278 */
3279 rblim = (struct scsi_read_block_limits_data *)
3280 malloc(8192, M_SCSISA, M_NOWAIT);
3281 if (rblim == NULL) {
3282 xpt_print(periph->path, "no memory for test read\n");
3283 xpt_release_ccb(ccb);
3284 error = ENOMEM;
3285 goto exit;
3286 }
3287
3288 if ((softc->quirks & SA_QUIRK_NODREAD) == 0) {
3289 scsi_sa_read_write(&ccb->csio, 0, NULL,
3290 MSG_SIMPLE_Q_TAG, 1, FALSE, 0, 8192,
3291 (void *) rblim, 8192, SSD_FULL_SIZE,
3292 softc->timeout_info[SA_TIMEOUT_READ]);
3293 (void) cam_periph_runccb(ccb, saerror, 0, SF_NO_PRINT,
3294 softc->device_stats);
3295 scsi_rewind(&ccb->csio, 1, NULL, MSG_SIMPLE_Q_TAG,
3296 FALSE, SSD_FULL_SIZE,
3297 softc->timeout_info[SA_TIMEOUT_REWIND]);
3298 error = cam_periph_runccb(ccb, saerror, CAM_RETRY_SELTO,
3299 SF_NO_PRINT | SF_RETRY_UA,
3300 softc->device_stats);
3301 if (error) {
3302 xpt_print(periph->path,
3303 "unable to rewind after test read\n");
3304 xpt_release_ccb(ccb);
3305 goto exit;
3306 }
3307 }
3308
3309 /*
3310 * Next off, determine block limits.
3311 */
3312 scsi_read_block_limits(&ccb->csio, 5, NULL, MSG_SIMPLE_Q_TAG,
3313 rblim, SSD_FULL_SIZE,
3314 softc->timeout_info[SA_TIMEOUT_READ_BLOCK_LIMITS]);
3315
3316 error = cam_periph_runccb(ccb, saerror, CAM_RETRY_SELTO,
3317 SF_NO_PRINT | SF_RETRY_UA, softc->device_stats);
3318
3319 xpt_release_ccb(ccb);
3320
3321 if (error != 0) {
3322 /*
3323 * If it's less than SCSI-2, READ BLOCK LIMITS is not
3324 * a MANDATORY command. Anyway- it doesn't matter-
3325 * we can proceed anyway.
3326 */
3327 softc->blk_gran = 0;
3328 softc->max_blk = ~0;
3329 softc->min_blk = 0;
3330 } else {
3331 if (softc->scsi_rev >= SCSI_REV_SPC) {
3332 softc->blk_gran = RBL_GRAN(rblim);
3333 } else {
3334 softc->blk_gran = 0;
3335 }
3336 /*
3337 * We take max_blk == min_blk to mean a default to
3338 * fixed mode- but note that whatever we get out of
3339 * sagetparams below will actually determine whether
3340 * we are actually *in* fixed mode.
3341 */
3342 softc->max_blk = scsi_3btoul(rblim->maximum);
3343 softc->min_blk = scsi_2btoul(rblim->minimum);
3344
3345
3346 }
3347 /*
3348 * Next, perform a mode sense to determine
3349 * current density, blocksize, compression etc.
3350 */
3351 error = sagetparams(periph, SA_PARAM_ALL,
3352 &softc->media_blksize,
3353 &softc->media_density,
3354 &softc->media_numblks,
3355 &softc->buffer_mode, &write_protect,
3356 &softc->speed, &comp_supported,
3357 &comp_enabled, &softc->comp_algorithm,
3358 NULL, NULL, 0, 0);
3359
3360 if (error != 0) {
3361 /*
3362 * We could work a little harder here. We could
3363 * adjust our attempts to get information. It
3364 * might be an ancient tape drive. If someone
3365 * nudges us, we'll do that.
3366 */
3367 goto exit;
3368 }
3369
3370 /*
3371 * If no quirk has determined that this is a device that is
3372 * preferred to be in fixed or variable mode, now is the time
3373 * to find out.
3374 */
3375 if ((softc->quirks & (SA_QUIRK_FIXED|SA_QUIRK_VARIABLE)) == 0) {
3376 guessing = 1;
3377 /*
3378 * This could be expensive to find out. Luckily we
3379 * only need to do this once. If we start out in
3380 * 'default' mode, try and set ourselves to one
3381 * of the densities that would determine a wad
3382 * of other stuff. Go from highest to lowest.
3383 */
3384 if (softc->media_density == SCSI_DEFAULT_DENSITY) {
3385 int i;
3386 static u_int8_t ctry[] = {
3387 SCSI_DENSITY_HALFINCH_PE,
3388 SCSI_DENSITY_HALFINCH_6250C,
3389 SCSI_DENSITY_HALFINCH_6250,
3390 SCSI_DENSITY_HALFINCH_1600,
3391 SCSI_DENSITY_HALFINCH_800,
3392 SCSI_DENSITY_QIC_4GB,
3393 SCSI_DENSITY_QIC_2GB,
3394 SCSI_DENSITY_QIC_525_320,
3395 SCSI_DENSITY_QIC_150,
3396 SCSI_DENSITY_QIC_120,
3397 SCSI_DENSITY_QIC_24,
3398 SCSI_DENSITY_QIC_11_9TRK,
3399 SCSI_DENSITY_QIC_11_4TRK,
3400 SCSI_DENSITY_QIC_1320,
3401 SCSI_DENSITY_QIC_3080,
3402 0
3403 };
3404 for (i = 0; ctry[i]; i++) {
3405 error = sasetparams(periph,
3406 SA_PARAM_DENSITY, 0, ctry[i],
3407 0, SF_NO_PRINT);
3408 if (error == 0) {
3409 softc->media_density = ctry[i];
3410 break;
3411 }
3412 }
3413 }
3414 switch (softc->media_density) {
3415 case SCSI_DENSITY_QIC_11_4TRK:
3416 case SCSI_DENSITY_QIC_11_9TRK:
3417 case SCSI_DENSITY_QIC_24:
3418 case SCSI_DENSITY_QIC_120:
3419 case SCSI_DENSITY_QIC_150:
3420 case SCSI_DENSITY_QIC_525_320:
3421 case SCSI_DENSITY_QIC_1320:
3422 case SCSI_DENSITY_QIC_3080:
3423 softc->quirks &= ~SA_QUIRK_2FM;
3424 softc->quirks |= SA_QUIRK_FIXED|SA_QUIRK_1FM;
3425 softc->last_media_blksize = 512;
3426 break;
3427 case SCSI_DENSITY_QIC_4GB:
3428 case SCSI_DENSITY_QIC_2GB:
3429 softc->quirks &= ~SA_QUIRK_2FM;
3430 softc->quirks |= SA_QUIRK_FIXED|SA_QUIRK_1FM;
3431 softc->last_media_blksize = 1024;
3432 break;
3433 default:
3434 softc->last_media_blksize =
3435 softc->media_blksize;
3436 softc->quirks |= SA_QUIRK_VARIABLE;
3437 break;
3438 }
3439 }
3440
3441 /*
3442 * If no quirk has determined that this is a device that needs
3443 * to have 2 Filemarks at EOD, now is the time to find out.
3444 */
3445
3446 if ((softc->quirks & SA_QUIRK_2FM) == 0) {
3447 switch (softc->media_density) {
3448 case SCSI_DENSITY_HALFINCH_800:
3449 case SCSI_DENSITY_HALFINCH_1600:
3450 case SCSI_DENSITY_HALFINCH_6250:
3451 case SCSI_DENSITY_HALFINCH_6250C:
3452 case SCSI_DENSITY_HALFINCH_PE:
3453 softc->quirks &= ~SA_QUIRK_1FM;
3454 softc->quirks |= SA_QUIRK_2FM;
3455 break;
3456 default:
3457 break;
3458 }
3459 }
3460
3461 /*
3462 * Now validate that some info we got makes sense.
3463 */
3464 if ((softc->max_blk < softc->media_blksize) ||
3465 (softc->min_blk > softc->media_blksize &&
3466 softc->media_blksize)) {
3467 xpt_print(periph->path,
3468 "BLOCK LIMITS (%d..%d) could not match current "
3469 "block settings (%d)- adjusting\n", softc->min_blk,
3470 softc->max_blk, softc->media_blksize);
3471 softc->max_blk = softc->min_blk =
3472 softc->media_blksize;
3473 }
3474
3475 /*
3476 * Now put ourselves into the right frame of mind based
3477 * upon quirks...
3478 */
3479 tryagain:
3480 /*
3481 * If we want to be in FIXED mode and our current blocksize
3482 * is not equal to our last blocksize (if nonzero), try and
3483 * set ourselves to this last blocksize (as the 'preferred'
3484 * block size). The initial quirkmatch at registry sets the
3485 * initial 'last' blocksize. If, for whatever reason, this
3486 * 'last' blocksize is zero, set the blocksize to 512,
3487 * or min_blk if that's larger.
3488 */
3489 if ((softc->quirks & SA_QUIRK_FIXED) &&
3490 (softc->quirks & SA_QUIRK_NO_MODESEL) == 0 &&
3491 (softc->media_blksize != softc->last_media_blksize)) {
3492 softc->media_blksize = softc->last_media_blksize;
3493 if (softc->media_blksize == 0) {
3494 softc->media_blksize = 512;
3495 if (softc->media_blksize < softc->min_blk) {
3496 softc->media_blksize = softc->min_blk;
3497 }
3498 }
3499 error = sasetparams(periph, SA_PARAM_BLOCKSIZE,
3500 softc->media_blksize, 0, 0, SF_NO_PRINT);
3501 if (error) {
3502 xpt_print(periph->path,
3503 "unable to set fixed blocksize to %d\n",
3504 softc->media_blksize);
3505 goto exit;
3506 }
3507 }
3508
3509 if ((softc->quirks & SA_QUIRK_VARIABLE) &&
3510 (softc->media_blksize != 0)) {
3511 softc->last_media_blksize = softc->media_blksize;
3512 softc->media_blksize = 0;
3513 error = sasetparams(periph, SA_PARAM_BLOCKSIZE,
3514 0, 0, 0, SF_NO_PRINT);
3515 if (error) {
3516 /*
3517 * If this fails and we were guessing, just
3518 * assume that we got it wrong and go try
3519 * fixed block mode. Don't even check against
3520 * density code at this point.
3521 */
3522 if (guessing) {
3523 softc->quirks &= ~SA_QUIRK_VARIABLE;
3524 softc->quirks |= SA_QUIRK_FIXED;
3525 if (softc->last_media_blksize == 0)
3526 softc->last_media_blksize = 512;
3527 goto tryagain;
3528 }
3529 xpt_print(periph->path,
3530 "unable to set variable blocksize\n");
3531 goto exit;
3532 }
3533 }
3534
3535 /*
3536 * Now that we have the current block size,
3537 * set up some parameters for sastart's usage.
3538 */
3539 if (softc->media_blksize) {
3540 softc->flags |= SA_FLAG_FIXED;
3541 if (powerof2(softc->media_blksize)) {
3542 softc->blk_shift =
3543 ffs(softc->media_blksize) - 1;
3544 softc->blk_mask = softc->media_blksize - 1;
3545 } else {
3546 softc->blk_mask = ~0;
3547 softc->blk_shift = 0;
3548 }
3549 } else {
3550 /*
3551 * The SCSI-3 spec allows 0 to mean "unspecified".
3552 * The SCSI-1 spec allows 0 to mean 'infinite'.
3553 *
3554 * Either works here.
3555 */
3556 if (softc->max_blk == 0) {
3557 softc->max_blk = ~0;
3558 }
3559 softc->blk_shift = 0;
3560 if (softc->blk_gran != 0) {
3561 softc->blk_mask = softc->blk_gran - 1;
3562 } else {
3563 softc->blk_mask = 0;
3564 }
3565 }
3566
3567 if (write_protect)
3568 softc->flags |= SA_FLAG_TAPE_WP;
3569
3570 if (comp_supported) {
3571 if (softc->saved_comp_algorithm == 0)
3572 softc->saved_comp_algorithm =
3573 softc->comp_algorithm;
3574 softc->flags |= SA_FLAG_COMP_SUPP;
3575 if (comp_enabled)
3576 softc->flags |= SA_FLAG_COMP_ENABLED;
3577 } else
3578 softc->flags |= SA_FLAG_COMP_UNSUPP;
3579
3580 if ((softc->buffer_mode == SMH_SA_BUF_MODE_NOBUF) &&
3581 (softc->quirks & SA_QUIRK_NO_MODESEL) == 0) {
3582 error = sasetparams(periph, SA_PARAM_BUFF_MODE, 0,
3583 0, 0, SF_NO_PRINT);
3584 if (error == 0) {
3585 softc->buffer_mode = SMH_SA_BUF_MODE_SIBUF;
3586 } else {
3587 xpt_print(periph->path,
3588 "unable to set buffered mode\n");
3589 }
3590 error = 0; /* not an error */
3591 }
3592
3593
3594 if (error == 0) {
3595 softc->flags |= SA_FLAG_TAPE_MOUNTED;
3596 }
3597 exit:
3598 if (rblim != NULL)
3599 free(rblim, M_SCSISA);
3600
3601 if (error != 0) {
3602 softc->dsreg = MTIO_DSREG_NIL;
3603 } else {
3604 softc->fileno = softc->blkno = 0;
3605 softc->rep_fileno = softc->rep_blkno = -1;
3606 softc->partition = 0;
3607 softc->dsreg = MTIO_DSREG_REST;
3608 }
3609 #ifdef SA_1FM_AT_EOD
3610 if ((softc->quirks & SA_QUIRK_2FM) == 0)
3611 softc->quirks |= SA_QUIRK_1FM;
3612 #else
3613 if ((softc->quirks & SA_QUIRK_1FM) == 0)
3614 softc->quirks |= SA_QUIRK_2FM;
3615 #endif
3616 } else
3617 xpt_release_ccb(ccb);
3618
3619 /*
3620 * If we return an error, we're not mounted any more,
3621 * so release any device reservation.
3622 */
3623 if (error != 0) {
3624 (void) sareservereleaseunit(periph, FALSE);
3625 } else {
3626 /*
3627 * Clear I/O residual.
3628 */
3629 softc->last_io_resid = 0;
3630 softc->last_ctl_resid = 0;
3631 }
3632 return (error);
3633 }
3634
3635 /*
3636 * How many filemarks do we need to write if we were to terminate the
3637 * tape session right now? Note that this can be a negative number
3638 */
3639
3640 static int
samarkswanted(struct cam_periph * periph)3641 samarkswanted(struct cam_periph *periph)
3642 {
3643 int markswanted;
3644 struct sa_softc *softc;
3645
3646 softc = (struct sa_softc *)periph->softc;
3647 markswanted = 0;
3648 if ((softc->flags & SA_FLAG_TAPE_WRITTEN) != 0) {
3649 markswanted++;
3650 if (softc->quirks & SA_QUIRK_2FM)
3651 markswanted++;
3652 }
3653 markswanted -= softc->filemarks;
3654 return (markswanted);
3655 }
3656
3657 static int
sacheckeod(struct cam_periph * periph)3658 sacheckeod(struct cam_periph *periph)
3659 {
3660 int error;
3661 int markswanted;
3662
3663 markswanted = samarkswanted(periph);
3664
3665 if (markswanted > 0) {
3666 error = sawritefilemarks(periph, markswanted, FALSE, FALSE);
3667 } else {
3668 error = 0;
3669 }
3670 return (error);
3671 }
3672
3673 static int
saerror(union ccb * ccb,u_int32_t cflgs,u_int32_t sflgs)3674 saerror(union ccb *ccb, u_int32_t cflgs, u_int32_t sflgs)
3675 {
3676 static const char *toobig =
3677 "%d-byte tape record bigger than supplied buffer\n";
3678 struct cam_periph *periph;
3679 struct sa_softc *softc;
3680 struct ccb_scsiio *csio;
3681 struct scsi_sense_data *sense;
3682 uint64_t resid = 0;
3683 int64_t info = 0;
3684 cam_status status;
3685 int error_code, sense_key, asc, ascq, error, aqvalid, stream_valid;
3686 int sense_len;
3687 uint8_t stream_bits;
3688
3689 periph = xpt_path_periph(ccb->ccb_h.path);
3690 softc = (struct sa_softc *)periph->softc;
3691 csio = &ccb->csio;
3692 sense = &csio->sense_data;
3693 sense_len = csio->sense_len - csio->sense_resid;
3694 scsi_extract_sense_len(sense, sense_len, &error_code, &sense_key,
3695 &asc, &ascq, /*show_errors*/ 1);
3696 if (asc != -1 && ascq != -1)
3697 aqvalid = 1;
3698 else
3699 aqvalid = 0;
3700 if (scsi_get_stream_info(sense, sense_len, NULL, &stream_bits) == 0)
3701 stream_valid = 1;
3702 else
3703 stream_valid = 0;
3704 error = 0;
3705
3706 status = csio->ccb_h.status & CAM_STATUS_MASK;
3707
3708 /*
3709 * Calculate/latch up, any residuals... We do this in a funny 2-step
3710 * so we can print stuff here if we have CAM_DEBUG enabled for this
3711 * unit.
3712 */
3713 if (status == CAM_SCSI_STATUS_ERROR) {
3714 if (scsi_get_sense_info(sense, sense_len, SSD_DESC_INFO, &resid,
3715 &info) == 0) {
3716 if ((softc->flags & SA_FLAG_FIXED) != 0)
3717 resid *= softc->media_blksize;
3718 } else {
3719 resid = csio->dxfer_len;
3720 info = resid;
3721 if ((softc->flags & SA_FLAG_FIXED) != 0) {
3722 if (softc->media_blksize)
3723 info /= softc->media_blksize;
3724 }
3725 }
3726 if (csio->cdb_io.cdb_bytes[0] == SA_READ ||
3727 csio->cdb_io.cdb_bytes[0] == SA_WRITE) {
3728 bcopy((caddr_t) sense, (caddr_t) &softc->last_io_sense,
3729 sizeof (struct scsi_sense_data));
3730 bcopy(csio->cdb_io.cdb_bytes, softc->last_io_cdb,
3731 (int) csio->cdb_len);
3732 softc->last_io_resid = resid;
3733 softc->last_resid_was_io = 1;
3734 } else {
3735 bcopy((caddr_t) sense, (caddr_t) &softc->last_ctl_sense,
3736 sizeof (struct scsi_sense_data));
3737 bcopy(csio->cdb_io.cdb_bytes, softc->last_ctl_cdb,
3738 (int) csio->cdb_len);
3739 softc->last_ctl_resid = resid;
3740 softc->last_resid_was_io = 0;
3741 }
3742 CAM_DEBUG(periph->path, CAM_DEBUG_INFO, ("CDB[0]=0x%x Key 0x%x "
3743 "ASC/ASCQ 0x%x/0x%x CAM STATUS 0x%x flags 0x%x resid %jd "
3744 "dxfer_len %d\n", csio->cdb_io.cdb_bytes[0] & 0xff,
3745 sense_key, asc, ascq, status,
3746 (stream_valid) ? stream_bits : 0, (intmax_t)resid,
3747 csio->dxfer_len));
3748 } else {
3749 CAM_DEBUG(periph->path, CAM_DEBUG_INFO,
3750 ("Cam Status 0x%x\n", status));
3751 }
3752
3753 switch (status) {
3754 case CAM_REQ_CMP:
3755 return (0);
3756 case CAM_SCSI_STATUS_ERROR:
3757 /*
3758 * If a read/write command, we handle it here.
3759 */
3760 if (csio->cdb_io.cdb_bytes[0] == SA_READ ||
3761 csio->cdb_io.cdb_bytes[0] == SA_WRITE) {
3762 break;
3763 }
3764 /*
3765 * If this was just EOM/EOP, Filemark, Setmark, ILI or
3766 * PEW detected on a non read/write command, we assume
3767 * it's not an error and propagate the residual and return.
3768 */
3769 if ((aqvalid && asc == 0 && ((ascq > 0 && ascq <= 5)
3770 || (ascq == 0x07)))
3771 || (aqvalid == 0 && sense_key == SSD_KEY_NO_SENSE)) {
3772 csio->resid = resid;
3773 QFRLS(ccb);
3774 return (0);
3775 }
3776 /*
3777 * Otherwise, we let the common code handle this.
3778 */
3779 return (cam_periph_error(ccb, cflgs, sflgs));
3780
3781 /*
3782 * XXX: To Be Fixed
3783 * We cannot depend upon CAM honoring retry counts for these.
3784 */
3785 case CAM_SCSI_BUS_RESET:
3786 case CAM_BDR_SENT:
3787 if (ccb->ccb_h.retry_count <= 0) {
3788 return (EIO);
3789 }
3790 /* FALLTHROUGH */
3791 default:
3792 return (cam_periph_error(ccb, cflgs, sflgs));
3793 }
3794
3795 /*
3796 * Handle filemark, end of tape, mismatched record sizes....
3797 * From this point out, we're only handling read/write cases.
3798 * Handle writes && reads differently.
3799 */
3800
3801 if (csio->cdb_io.cdb_bytes[0] == SA_WRITE) {
3802 if (sense_key == SSD_KEY_VOLUME_OVERFLOW) {
3803 csio->resid = resid;
3804 error = ENOSPC;
3805 } else if ((stream_valid != 0) && (stream_bits & SSD_EOM)) {
3806 softc->flags |= SA_FLAG_EOM_PENDING;
3807 /*
3808 * Grotesque as it seems, the few times
3809 * I've actually seen a non-zero resid,
3810 * the tape drive actually lied and had
3811 * written all the data!.
3812 */
3813 csio->resid = 0;
3814 }
3815 } else {
3816 csio->resid = resid;
3817 if (sense_key == SSD_KEY_BLANK_CHECK) {
3818 if (softc->quirks & SA_QUIRK_1FM) {
3819 error = 0;
3820 softc->flags |= SA_FLAG_EOM_PENDING;
3821 } else {
3822 error = EIO;
3823 }
3824 } else if ((stream_valid != 0) && (stream_bits & SSD_FILEMARK)){
3825 if (softc->flags & SA_FLAG_FIXED) {
3826 error = -1;
3827 softc->flags |= SA_FLAG_EOF_PENDING;
3828 }
3829 /*
3830 * Unconditionally, if we detected a filemark on a read,
3831 * mark that we've run moved a file ahead.
3832 */
3833 if (softc->fileno != (daddr_t) -1) {
3834 softc->fileno++;
3835 softc->blkno = 0;
3836 csio->ccb_h.ccb_pflags |= SA_POSITION_UPDATED;
3837 }
3838 }
3839 }
3840
3841 /*
3842 * Incorrect Length usually applies to read, but can apply to writes.
3843 */
3844 if (error == 0 && (stream_valid != 0) && (stream_bits & SSD_ILI)) {
3845 if (info < 0) {
3846 xpt_print(csio->ccb_h.path, toobig,
3847 csio->dxfer_len - info);
3848 csio->resid = csio->dxfer_len;
3849 error = EIO;
3850 } else {
3851 csio->resid = resid;
3852 if (softc->flags & SA_FLAG_FIXED) {
3853 softc->flags |= SA_FLAG_EIO_PENDING;
3854 }
3855 /*
3856 * Bump the block number if we hadn't seen a filemark.
3857 * Do this independent of errors (we've moved anyway).
3858 */
3859 if ((stream_valid == 0) ||
3860 (stream_bits & SSD_FILEMARK) == 0) {
3861 if (softc->blkno != (daddr_t) -1) {
3862 softc->blkno++;
3863 csio->ccb_h.ccb_pflags |=
3864 SA_POSITION_UPDATED;
3865 }
3866 }
3867 }
3868 }
3869
3870 if (error <= 0) {
3871 /*
3872 * Unfreeze the queue if frozen as we're not returning anything
3873 * to our waiters that would indicate an I/O error has occurred
3874 * (yet).
3875 */
3876 QFRLS(ccb);
3877 error = 0;
3878 }
3879 return (error);
3880 }
3881
3882 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,struct scsi_control_data_prot_subpage * prot_page,int dp_size,int prot_changeable)3883 sagetparams(struct cam_periph *periph, sa_params params_to_get,
3884 u_int32_t *blocksize, u_int8_t *density, u_int32_t *numblocks,
3885 int *buff_mode, u_int8_t *write_protect, u_int8_t *speed,
3886 int *comp_supported, int *comp_enabled, u_int32_t *comp_algorithm,
3887 sa_comp_t *tcs, struct scsi_control_data_prot_subpage *prot_page,
3888 int dp_size, int prot_changeable)
3889 {
3890 union ccb *ccb;
3891 void *mode_buffer;
3892 struct scsi_mode_header_6 *mode_hdr;
3893 struct scsi_mode_blk_desc *mode_blk;
3894 int mode_buffer_len;
3895 struct sa_softc *softc;
3896 u_int8_t cpage;
3897 int error;
3898 cam_status status;
3899
3900 softc = (struct sa_softc *)periph->softc;
3901 ccb = cam_periph_getccb(periph, 1);
3902 if (softc->quirks & SA_QUIRK_NO_CPAGE)
3903 cpage = SA_DEVICE_CONFIGURATION_PAGE;
3904 else
3905 cpage = SA_DATA_COMPRESSION_PAGE;
3906
3907 retry:
3908 mode_buffer_len = sizeof(*mode_hdr) + sizeof(*mode_blk);
3909
3910 if (params_to_get & SA_PARAM_COMPRESSION) {
3911 if (softc->quirks & SA_QUIRK_NOCOMP) {
3912 *comp_supported = FALSE;
3913 params_to_get &= ~SA_PARAM_COMPRESSION;
3914 } else
3915 mode_buffer_len += sizeof (sa_comp_t);
3916 }
3917
3918 /* XXX Fix M_NOWAIT */
3919 mode_buffer = malloc(mode_buffer_len, M_SCSISA, M_NOWAIT | M_ZERO);
3920 if (mode_buffer == NULL) {
3921 xpt_release_ccb(ccb);
3922 return (ENOMEM);
3923 }
3924 mode_hdr = (struct scsi_mode_header_6 *)mode_buffer;
3925 mode_blk = (struct scsi_mode_blk_desc *)&mode_hdr[1];
3926
3927 /* it is safe to retry this */
3928 scsi_mode_sense(&ccb->csio, 5, NULL, MSG_SIMPLE_Q_TAG, FALSE,
3929 SMS_PAGE_CTRL_CURRENT, (params_to_get & SA_PARAM_COMPRESSION) ?
3930 cpage : SMS_VENDOR_SPECIFIC_PAGE, mode_buffer, mode_buffer_len,
3931 SSD_FULL_SIZE, softc->timeout_info[SA_TIMEOUT_MODE_SENSE]);
3932
3933 error = cam_periph_runccb(ccb, saerror, 0, SF_NO_PRINT,
3934 softc->device_stats);
3935
3936 status = ccb->ccb_h.status & CAM_STATUS_MASK;
3937
3938 if (error == EINVAL && (params_to_get & SA_PARAM_COMPRESSION) != 0) {
3939 /*
3940 * Hmm. Let's see if we can try another page...
3941 * If we've already done that, give up on compression
3942 * for this device and remember this for the future
3943 * and attempt the request without asking for compression
3944 * info.
3945 */
3946 if (cpage == SA_DATA_COMPRESSION_PAGE) {
3947 cpage = SA_DEVICE_CONFIGURATION_PAGE;
3948 goto retry;
3949 }
3950 softc->quirks |= SA_QUIRK_NOCOMP;
3951 free(mode_buffer, M_SCSISA);
3952 goto retry;
3953 } else if (status == CAM_SCSI_STATUS_ERROR) {
3954 /* Tell the user about the fatal error. */
3955 scsi_sense_print(&ccb->csio);
3956 goto sagetparamsexit;
3957 }
3958
3959 /*
3960 * If the user only wants the compression information, and
3961 * the device doesn't send back the block descriptor, it's
3962 * no big deal. If the user wants more than just
3963 * compression, though, and the device doesn't pass back the
3964 * block descriptor, we need to send another mode sense to
3965 * get the block descriptor.
3966 */
3967 if ((mode_hdr->blk_desc_len == 0) &&
3968 (params_to_get & SA_PARAM_COMPRESSION) &&
3969 (params_to_get & ~(SA_PARAM_COMPRESSION))) {
3970
3971 /*
3972 * Decrease the mode buffer length by the size of
3973 * the compression page, to make sure the data
3974 * there doesn't get overwritten.
3975 */
3976 mode_buffer_len -= sizeof (sa_comp_t);
3977
3978 /*
3979 * Now move the compression page that we presumably
3980 * got back down the memory chunk a little bit so
3981 * it doesn't get spammed.
3982 */
3983 bcopy(&mode_hdr[0], &mode_hdr[1], sizeof (sa_comp_t));
3984 bzero(&mode_hdr[0], sizeof (mode_hdr[0]));
3985
3986 /*
3987 * Now, we issue another mode sense and just ask
3988 * for the block descriptor, etc.
3989 */
3990
3991 scsi_mode_sense(&ccb->csio, 2, NULL, MSG_SIMPLE_Q_TAG, FALSE,
3992 SMS_PAGE_CTRL_CURRENT, SMS_VENDOR_SPECIFIC_PAGE,
3993 mode_buffer, mode_buffer_len, SSD_FULL_SIZE,
3994 softc->timeout_info[SA_TIMEOUT_MODE_SENSE]);
3995
3996 error = cam_periph_runccb(ccb, saerror, 0, SF_NO_PRINT,
3997 softc->device_stats);
3998
3999 if (error != 0)
4000 goto sagetparamsexit;
4001 }
4002
4003 if (params_to_get & SA_PARAM_BLOCKSIZE)
4004 *blocksize = scsi_3btoul(mode_blk->blklen);
4005
4006 if (params_to_get & SA_PARAM_NUMBLOCKS)
4007 *numblocks = scsi_3btoul(mode_blk->nblocks);
4008
4009 if (params_to_get & SA_PARAM_BUFF_MODE)
4010 *buff_mode = mode_hdr->dev_spec & SMH_SA_BUF_MODE_MASK;
4011
4012 if (params_to_get & SA_PARAM_DENSITY)
4013 *density = mode_blk->density;
4014
4015 if (params_to_get & SA_PARAM_WP)
4016 *write_protect = (mode_hdr->dev_spec & SMH_SA_WP)? TRUE : FALSE;
4017
4018 if (params_to_get & SA_PARAM_SPEED)
4019 *speed = mode_hdr->dev_spec & SMH_SA_SPEED_MASK;
4020
4021 if (params_to_get & SA_PARAM_COMPRESSION) {
4022 sa_comp_t *ntcs = (sa_comp_t *) &mode_blk[1];
4023 if (cpage == SA_DATA_COMPRESSION_PAGE) {
4024 struct scsi_data_compression_page *cp = &ntcs->dcomp;
4025 *comp_supported =
4026 (cp->dce_and_dcc & SA_DCP_DCC)? TRUE : FALSE;
4027 *comp_enabled =
4028 (cp->dce_and_dcc & SA_DCP_DCE)? TRUE : FALSE;
4029 *comp_algorithm = scsi_4btoul(cp->comp_algorithm);
4030 } else {
4031 struct scsi_dev_conf_page *cp = &ntcs->dconf;
4032 /*
4033 * We don't really know whether this device supports
4034 * Data Compression if the algorithm field is
4035 * zero. Just say we do.
4036 */
4037 *comp_supported = TRUE;
4038 *comp_enabled =
4039 (cp->sel_comp_alg != SA_COMP_NONE)? TRUE : FALSE;
4040 *comp_algorithm = cp->sel_comp_alg;
4041 }
4042 if (tcs != NULL)
4043 bcopy(ntcs, tcs, sizeof (sa_comp_t));
4044 }
4045
4046 if ((params_to_get & SA_PARAM_DENSITY_EXT)
4047 && (softc->scsi_rev >= SCSI_REV_SPC)) {
4048 int i;
4049
4050 for (i = 0; i < SA_DENSITY_TYPES; i++) {
4051 scsi_report_density_support(&ccb->csio,
4052 /*retries*/ 1,
4053 /*cbfcnp*/ NULL,
4054 /*tag_action*/ MSG_SIMPLE_Q_TAG,
4055 /*media*/ softc->density_type_bits[i] & SRDS_MEDIA,
4056 /*medium_type*/ softc->density_type_bits[i] &
4057 SRDS_MEDIUM_TYPE,
4058 /*data_ptr*/ softc->density_info[i],
4059 /*length*/ sizeof(softc->density_info[i]),
4060 /*sense_len*/ SSD_FULL_SIZE,
4061 /*timeout*/
4062 softc->timeout_info[SA_TIMEOUT_REP_DENSITY]);
4063 error = cam_periph_runccb(ccb, saerror, 0, SF_NO_PRINT,
4064 softc->device_stats);
4065 status = ccb->ccb_h.status & CAM_STATUS_MASK;
4066
4067 /*
4068 * Some tape drives won't support this command at
4069 * all, but hopefully we'll minimize that with the
4070 * check for SPC or greater support above. If they
4071 * don't support the default report (neither the
4072 * MEDIA or MEDIUM_TYPE bits set), then there is
4073 * really no point in continuing on to look for
4074 * other reports.
4075 */
4076 if ((error != 0)
4077 || (status != CAM_REQ_CMP)) {
4078 error = 0;
4079 softc->density_info_valid[i] = 0;
4080 if (softc->density_type_bits[i] == 0)
4081 break;
4082 else
4083 continue;
4084 }
4085 softc->density_info_valid[i] = ccb->csio.dxfer_len -
4086 ccb->csio.resid;
4087 }
4088 }
4089
4090 /*
4091 * Get logical block protection parameters if the drive supports it.
4092 */
4093 if ((params_to_get & SA_PARAM_LBP)
4094 && (softc->flags & SA_FLAG_PROTECT_SUPP)) {
4095 struct scsi_mode_header_10 *mode10_hdr;
4096 struct scsi_control_data_prot_subpage *dp_page;
4097 struct scsi_mode_sense_10 *cdb;
4098 struct sa_prot_state *prot;
4099 int dp_len, returned_len;
4100
4101 if (dp_size == 0)
4102 dp_size = sizeof(*dp_page);
4103
4104 dp_len = sizeof(*mode10_hdr) + dp_size;
4105 mode10_hdr = malloc(dp_len, M_SCSISA, M_NOWAIT | M_ZERO);
4106 if (mode10_hdr == NULL) {
4107 error = ENOMEM;
4108 goto sagetparamsexit;
4109 }
4110
4111 scsi_mode_sense_len(&ccb->csio,
4112 /*retries*/ 5,
4113 /*cbfcnp*/ NULL,
4114 /*tag_action*/ MSG_SIMPLE_Q_TAG,
4115 /*dbd*/ TRUE,
4116 /*page_code*/ (prot_changeable == 0) ?
4117 SMS_PAGE_CTRL_CURRENT :
4118 SMS_PAGE_CTRL_CHANGEABLE,
4119 /*page*/ SMS_CONTROL_MODE_PAGE,
4120 /*param_buf*/ (uint8_t *)mode10_hdr,
4121 /*param_len*/ dp_len,
4122 /*minimum_cmd_size*/ 10,
4123 /*sense_len*/ SSD_FULL_SIZE,
4124 /*timeout*/
4125 softc->timeout_info[SA_TIMEOUT_MODE_SENSE]);
4126 /*
4127 * XXX KDM we need to be able to set the subpage in the
4128 * fill function.
4129 */
4130 cdb = (struct scsi_mode_sense_10 *)ccb->csio.cdb_io.cdb_bytes;
4131 cdb->subpage = SA_CTRL_DP_SUBPAGE_CODE;
4132
4133 error = cam_periph_runccb(ccb, saerror, 0, SF_NO_PRINT,
4134 softc->device_stats);
4135 if (error != 0) {
4136 free(mode10_hdr, M_SCSISA);
4137 goto sagetparamsexit;
4138 }
4139
4140 status = ccb->ccb_h.status & CAM_STATUS_MASK;
4141 if (status != CAM_REQ_CMP) {
4142 error = EINVAL;
4143 free(mode10_hdr, M_SCSISA);
4144 goto sagetparamsexit;
4145 }
4146
4147 /*
4148 * The returned data length at least has to be long enough
4149 * for us to look at length in the mode page header.
4150 */
4151 returned_len = ccb->csio.dxfer_len - ccb->csio.resid;
4152 if (returned_len < sizeof(mode10_hdr->data_length)) {
4153 error = EINVAL;
4154 free(mode10_hdr, M_SCSISA);
4155 goto sagetparamsexit;
4156 }
4157
4158 returned_len = min(returned_len,
4159 sizeof(mode10_hdr->data_length) +
4160 scsi_2btoul(mode10_hdr->data_length));
4161
4162 dp_page = (struct scsi_control_data_prot_subpage *)
4163 &mode10_hdr[1];
4164
4165 /*
4166 * We also have to have enough data to include the prot_bits
4167 * in the subpage.
4168 */
4169 if (returned_len < (sizeof(*mode10_hdr) +
4170 __offsetof(struct scsi_control_data_prot_subpage, prot_bits)
4171 + sizeof(dp_page->prot_bits))) {
4172 error = EINVAL;
4173 free(mode10_hdr, M_SCSISA);
4174 goto sagetparamsexit;
4175 }
4176
4177 prot = &softc->prot_info.cur_prot_state;
4178 prot->prot_method = dp_page->prot_method;
4179 prot->pi_length = dp_page->pi_length &
4180 SA_CTRL_DP_PI_LENGTH_MASK;
4181 prot->lbp_w = (dp_page->prot_bits & SA_CTRL_DP_LBP_W) ? 1 :0;
4182 prot->lbp_r = (dp_page->prot_bits & SA_CTRL_DP_LBP_R) ? 1 :0;
4183 prot->rbdp = (dp_page->prot_bits & SA_CTRL_DP_RBDP) ? 1 :0;
4184 prot->initialized = 1;
4185
4186 if (prot_page != NULL)
4187 bcopy(dp_page, prot_page, min(sizeof(*prot_page),
4188 sizeof(*dp_page)));
4189
4190 free(mode10_hdr, M_SCSISA);
4191 }
4192
4193 if (CAM_DEBUGGED(periph->path, CAM_DEBUG_INFO)) {
4194 int idx;
4195 char *xyz = mode_buffer;
4196 xpt_print_path(periph->path);
4197 printf("Mode Sense Data=");
4198 for (idx = 0; idx < mode_buffer_len; idx++)
4199 printf(" 0x%02x", xyz[idx] & 0xff);
4200 printf("\n");
4201 }
4202
4203 sagetparamsexit:
4204
4205 xpt_release_ccb(ccb);
4206 free(mode_buffer, M_SCSISA);
4207 return (error);
4208 }
4209
4210 /*
4211 * Set protection information to the pending protection information stored
4212 * in the softc.
4213 */
4214 static int
sasetprot(struct cam_periph * periph,struct sa_prot_state * new_prot)4215 sasetprot(struct cam_periph *periph, struct sa_prot_state *new_prot)
4216 {
4217 struct sa_softc *softc;
4218 struct scsi_control_data_prot_subpage *dp_page, *dp_changeable;
4219 struct scsi_mode_header_10 *mode10_hdr, *mode10_changeable;
4220 union ccb *ccb;
4221 uint8_t current_speed;
4222 size_t dp_size, dp_page_length;
4223 int dp_len, buff_mode;
4224 int error;
4225
4226 softc = (struct sa_softc *)periph->softc;
4227 mode10_hdr = NULL;
4228 mode10_changeable = NULL;
4229 ccb = NULL;
4230
4231 /*
4232 * Start off with the size set to the actual length of the page
4233 * that we have defined.
4234 */
4235 dp_size = sizeof(*dp_changeable);
4236 dp_page_length = dp_size -
4237 __offsetof(struct scsi_control_data_prot_subpage, prot_method);
4238
4239 retry_length:
4240
4241 dp_len = sizeof(*mode10_changeable) + dp_size;
4242 mode10_changeable = malloc(dp_len, M_SCSISA, M_NOWAIT | M_ZERO);
4243 if (mode10_changeable == NULL) {
4244 error = ENOMEM;
4245 goto bailout;
4246 }
4247
4248 dp_changeable =
4249 (struct scsi_control_data_prot_subpage *)&mode10_changeable[1];
4250
4251 /*
4252 * First get the data protection page changeable parameters mask.
4253 * We need to know which parameters the drive supports changing.
4254 * We also need to know what the drive claims that its page length
4255 * is. The reason is that IBM drives in particular are very picky
4256 * about the page length. They want it (the length set in the
4257 * page structure itself) to be 28 bytes, and they want the
4258 * parameter list length specified in the mode select header to be
4259 * 40 bytes. So, to work with IBM drives as well as any other tape
4260 * drive, find out what the drive claims the page length is, and
4261 * make sure that we match that.
4262 */
4263 error = sagetparams(periph, SA_PARAM_SPEED | SA_PARAM_LBP,
4264 NULL, NULL, NULL, &buff_mode, NULL, ¤t_speed, NULL, NULL,
4265 NULL, NULL, dp_changeable, dp_size, /*prot_changeable*/ 1);
4266 if (error != 0)
4267 goto bailout;
4268
4269 if (scsi_2btoul(dp_changeable->length) > dp_page_length) {
4270 dp_page_length = scsi_2btoul(dp_changeable->length);
4271 dp_size = dp_page_length +
4272 __offsetof(struct scsi_control_data_prot_subpage,
4273 prot_method);
4274 free(mode10_changeable, M_SCSISA);
4275 mode10_changeable = NULL;
4276 goto retry_length;
4277 }
4278
4279 mode10_hdr = malloc(dp_len, M_SCSISA, M_NOWAIT | M_ZERO);
4280 if (mode10_hdr == NULL) {
4281 error = ENOMEM;
4282 goto bailout;
4283 }
4284
4285 dp_page = (struct scsi_control_data_prot_subpage *)&mode10_hdr[1];
4286
4287 /*
4288 * Now grab the actual current settings in the page.
4289 */
4290 error = sagetparams(periph, SA_PARAM_SPEED | SA_PARAM_LBP,
4291 NULL, NULL, NULL, &buff_mode, NULL, ¤t_speed, NULL, NULL,
4292 NULL, NULL, dp_page, dp_size, /*prot_changeable*/ 0);
4293 if (error != 0)
4294 goto bailout;
4295
4296 /* These two fields need to be 0 for MODE SELECT */
4297 scsi_ulto2b(0, mode10_hdr->data_length);
4298 mode10_hdr->medium_type = 0;
4299 /* We are not including a block descriptor */
4300 scsi_ulto2b(0, mode10_hdr->blk_desc_len);
4301
4302 mode10_hdr->dev_spec = current_speed;
4303 /* if set, set single-initiator buffering mode */
4304 if (softc->buffer_mode == SMH_SA_BUF_MODE_SIBUF) {
4305 mode10_hdr->dev_spec |= SMH_SA_BUF_MODE_SIBUF;
4306 }
4307
4308 /*
4309 * For each field, make sure that the drive allows changing it
4310 * before bringing in the user's setting.
4311 */
4312 if (dp_changeable->prot_method != 0)
4313 dp_page->prot_method = new_prot->prot_method;
4314
4315 if (dp_changeable->pi_length & SA_CTRL_DP_PI_LENGTH_MASK) {
4316 dp_page->pi_length &= ~SA_CTRL_DP_PI_LENGTH_MASK;
4317 dp_page->pi_length |= (new_prot->pi_length &
4318 SA_CTRL_DP_PI_LENGTH_MASK);
4319 }
4320 if (dp_changeable->prot_bits & SA_CTRL_DP_LBP_W) {
4321 if (new_prot->lbp_w)
4322 dp_page->prot_bits |= SA_CTRL_DP_LBP_W;
4323 else
4324 dp_page->prot_bits &= ~SA_CTRL_DP_LBP_W;
4325 }
4326
4327 if (dp_changeable->prot_bits & SA_CTRL_DP_LBP_R) {
4328 if (new_prot->lbp_r)
4329 dp_page->prot_bits |= SA_CTRL_DP_LBP_R;
4330 else
4331 dp_page->prot_bits &= ~SA_CTRL_DP_LBP_R;
4332 }
4333
4334 if (dp_changeable->prot_bits & SA_CTRL_DP_RBDP) {
4335 if (new_prot->rbdp)
4336 dp_page->prot_bits |= SA_CTRL_DP_RBDP;
4337 else
4338 dp_page->prot_bits &= ~SA_CTRL_DP_RBDP;
4339 }
4340
4341 ccb = cam_periph_getccb(periph, 1);
4342
4343 scsi_mode_select_len(&ccb->csio,
4344 /*retries*/ 5,
4345 /*cbfcnp*/ NULL,
4346 /*tag_action*/ MSG_SIMPLE_Q_TAG,
4347 /*scsi_page_fmt*/ TRUE,
4348 /*save_pages*/ FALSE,
4349 /*param_buf*/ (uint8_t *)mode10_hdr,
4350 /*param_len*/ dp_len,
4351 /*minimum_cmd_size*/ 10,
4352 /*sense_len*/ SSD_FULL_SIZE,
4353 /*timeout*/
4354 softc->timeout_info[SA_TIMEOUT_MODE_SELECT]);
4355
4356 error = cam_periph_runccb(ccb, saerror, 0, 0, softc->device_stats);
4357 if (error != 0)
4358 goto bailout;
4359
4360 if ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
4361 error = EINVAL;
4362 goto bailout;
4363 }
4364
4365 /*
4366 * The operation was successful. We could just copy the settings
4367 * the user requested, but just in case the drive ignored some of
4368 * our settings, let's ask for status again.
4369 */
4370 error = sagetparams(periph, SA_PARAM_SPEED | SA_PARAM_LBP,
4371 NULL, NULL, NULL, &buff_mode, NULL, ¤t_speed, NULL, NULL,
4372 NULL, NULL, dp_page, dp_size, 0);
4373
4374 bailout:
4375 if (ccb != NULL)
4376 xpt_release_ccb(ccb);
4377 free(mode10_hdr, M_SCSISA);
4378 free(mode10_changeable, M_SCSISA);
4379 return (error);
4380 }
4381
4382 /*
4383 * The purpose of this function is to set one of four different parameters
4384 * for a tape drive:
4385 * - blocksize
4386 * - density
4387 * - compression / compression algorithm
4388 * - buffering mode
4389 *
4390 * The assumption is that this will be called from saioctl(), and therefore
4391 * from a process context. Thus the waiting malloc calls below. If that
4392 * assumption ever changes, the malloc calls should be changed to be
4393 * NOWAIT mallocs.
4394 *
4395 * Any or all of the four parameters may be set when this function is
4396 * called. It should handle setting more than one parameter at once.
4397 */
4398 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)4399 sasetparams(struct cam_periph *periph, sa_params params_to_set,
4400 u_int32_t blocksize, u_int8_t density, u_int32_t calg,
4401 u_int32_t sense_flags)
4402 {
4403 struct sa_softc *softc;
4404 u_int32_t current_blocksize;
4405 u_int32_t current_calg;
4406 u_int8_t current_density;
4407 u_int8_t current_speed;
4408 int comp_enabled, comp_supported;
4409 void *mode_buffer;
4410 int mode_buffer_len;
4411 struct scsi_mode_header_6 *mode_hdr;
4412 struct scsi_mode_blk_desc *mode_blk;
4413 sa_comp_t *ccomp, *cpage;
4414 int buff_mode;
4415 union ccb *ccb = NULL;
4416 int error;
4417
4418 softc = (struct sa_softc *)periph->softc;
4419
4420 ccomp = malloc(sizeof (sa_comp_t), M_SCSISA, M_NOWAIT);
4421 if (ccomp == NULL)
4422 return (ENOMEM);
4423
4424 /*
4425 * Since it doesn't make sense to set the number of blocks, or
4426 * write protection, we won't try to get the current value. We
4427 * always want to get the blocksize, so we can set it back to the
4428 * proper value.
4429 */
4430 error = sagetparams(periph,
4431 params_to_set | SA_PARAM_BLOCKSIZE | SA_PARAM_SPEED,
4432 ¤t_blocksize, ¤t_density, NULL, &buff_mode, NULL,
4433 ¤t_speed, &comp_supported, &comp_enabled,
4434 ¤t_calg, ccomp, NULL, 0, 0);
4435
4436 if (error != 0) {
4437 free(ccomp, M_SCSISA);
4438 return (error);
4439 }
4440
4441 mode_buffer_len = sizeof(*mode_hdr) + sizeof(*mode_blk);
4442 if (params_to_set & SA_PARAM_COMPRESSION)
4443 mode_buffer_len += sizeof (sa_comp_t);
4444
4445 mode_buffer = malloc(mode_buffer_len, M_SCSISA, M_NOWAIT | M_ZERO);
4446 if (mode_buffer == NULL) {
4447 free(ccomp, M_SCSISA);
4448 return (ENOMEM);
4449 }
4450
4451 mode_hdr = (struct scsi_mode_header_6 *)mode_buffer;
4452 mode_blk = (struct scsi_mode_blk_desc *)&mode_hdr[1];
4453
4454 ccb = cam_periph_getccb(periph, 1);
4455
4456 retry:
4457
4458 if (params_to_set & SA_PARAM_COMPRESSION) {
4459 if (mode_blk) {
4460 cpage = (sa_comp_t *)&mode_blk[1];
4461 } else {
4462 cpage = (sa_comp_t *)&mode_hdr[1];
4463 }
4464 bcopy(ccomp, cpage, sizeof (sa_comp_t));
4465 cpage->hdr.pagecode &= ~0x80;
4466 } else
4467 cpage = NULL;
4468
4469 /*
4470 * If the caller wants us to set the blocksize, use the one they
4471 * pass in. Otherwise, use the blocksize we got back from the
4472 * mode select above.
4473 */
4474 if (mode_blk) {
4475 if (params_to_set & SA_PARAM_BLOCKSIZE)
4476 scsi_ulto3b(blocksize, mode_blk->blklen);
4477 else
4478 scsi_ulto3b(current_blocksize, mode_blk->blklen);
4479
4480 /*
4481 * Set density if requested, else preserve old density.
4482 * SCSI_SAME_DENSITY only applies to SCSI-2 or better
4483 * devices, else density we've latched up in our softc.
4484 */
4485 if (params_to_set & SA_PARAM_DENSITY) {
4486 mode_blk->density = density;
4487 } else if (softc->scsi_rev > SCSI_REV_CCS) {
4488 mode_blk->density = SCSI_SAME_DENSITY;
4489 } else {
4490 mode_blk->density = softc->media_density;
4491 }
4492 }
4493
4494 /*
4495 * For mode selects, these two fields must be zero.
4496 */
4497 mode_hdr->data_length = 0;
4498 mode_hdr->medium_type = 0;
4499
4500 /* set the speed to the current value */
4501 mode_hdr->dev_spec = current_speed;
4502
4503 /* if set, set single-initiator buffering mode */
4504 if (softc->buffer_mode == SMH_SA_BUF_MODE_SIBUF) {
4505 mode_hdr->dev_spec |= SMH_SA_BUF_MODE_SIBUF;
4506 }
4507
4508 if (mode_blk)
4509 mode_hdr->blk_desc_len = sizeof(struct scsi_mode_blk_desc);
4510 else
4511 mode_hdr->blk_desc_len = 0;
4512
4513 /*
4514 * First, if the user wants us to set the compression algorithm or
4515 * just turn compression on, check to make sure that this drive
4516 * supports compression.
4517 */
4518 if (params_to_set & SA_PARAM_COMPRESSION) {
4519 /*
4520 * If the compression algorithm is 0, disable compression.
4521 * If the compression algorithm is non-zero, enable
4522 * compression and set the compression type to the
4523 * specified compression algorithm, unless the algorithm is
4524 * MT_COMP_ENABLE. In that case, we look at the
4525 * compression algorithm that is currently set and if it is
4526 * non-zero, we leave it as-is. If it is zero, and we have
4527 * saved a compression algorithm from a time when
4528 * compression was enabled before, set the compression to
4529 * the saved value.
4530 */
4531 switch (ccomp->hdr.pagecode & ~0x80) {
4532 case SA_DEVICE_CONFIGURATION_PAGE:
4533 {
4534 struct scsi_dev_conf_page *dcp = &cpage->dconf;
4535 if (calg == 0) {
4536 dcp->sel_comp_alg = SA_COMP_NONE;
4537 break;
4538 }
4539 if (calg != MT_COMP_ENABLE) {
4540 dcp->sel_comp_alg = calg;
4541 } else if (dcp->sel_comp_alg == SA_COMP_NONE &&
4542 softc->saved_comp_algorithm != 0) {
4543 dcp->sel_comp_alg = softc->saved_comp_algorithm;
4544 }
4545 break;
4546 }
4547 case SA_DATA_COMPRESSION_PAGE:
4548 if (ccomp->dcomp.dce_and_dcc & SA_DCP_DCC) {
4549 struct scsi_data_compression_page *dcp = &cpage->dcomp;
4550 if (calg == 0) {
4551 /*
4552 * Disable compression, but leave the
4553 * decompression and the capability bit
4554 * alone.
4555 */
4556 dcp->dce_and_dcc = SA_DCP_DCC;
4557 dcp->dde_and_red |= SA_DCP_DDE;
4558 break;
4559 }
4560 /* enable compression && decompression */
4561 dcp->dce_and_dcc = SA_DCP_DCE | SA_DCP_DCC;
4562 dcp->dde_and_red |= SA_DCP_DDE;
4563 /*
4564 * If there, use compression algorithm from caller.
4565 * Otherwise, if there's a saved compression algorithm
4566 * and there is no current algorithm, use the saved
4567 * algorithm. Else parrot back what we got and hope
4568 * for the best.
4569 */
4570 if (calg != MT_COMP_ENABLE) {
4571 scsi_ulto4b(calg, dcp->comp_algorithm);
4572 scsi_ulto4b(calg, dcp->decomp_algorithm);
4573 } else if (scsi_4btoul(dcp->comp_algorithm) == 0 &&
4574 softc->saved_comp_algorithm != 0) {
4575 scsi_ulto4b(softc->saved_comp_algorithm,
4576 dcp->comp_algorithm);
4577 scsi_ulto4b(softc->saved_comp_algorithm,
4578 dcp->decomp_algorithm);
4579 }
4580 break;
4581 }
4582 /*
4583 * Compression does not appear to be supported-
4584 * at least via the DATA COMPRESSION page. It
4585 * would be too much to ask us to believe that
4586 * the page itself is supported, but incorrectly
4587 * reports an ability to manipulate data compression,
4588 * so we'll assume that this device doesn't support
4589 * compression. We can just fall through for that.
4590 */
4591 /* FALLTHROUGH */
4592 default:
4593 /*
4594 * The drive doesn't seem to support compression,
4595 * so turn off the set compression bit.
4596 */
4597 params_to_set &= ~SA_PARAM_COMPRESSION;
4598 xpt_print(periph->path,
4599 "device does not seem to support compression\n");
4600
4601 /*
4602 * If that was the only thing the user wanted us to set,
4603 * clean up allocated resources and return with
4604 * 'operation not supported'.
4605 */
4606 if (params_to_set == SA_PARAM_NONE) {
4607 free(mode_buffer, M_SCSISA);
4608 xpt_release_ccb(ccb);
4609 return (ENODEV);
4610 }
4611
4612 /*
4613 * That wasn't the only thing the user wanted us to set.
4614 * So, decrease the stated mode buffer length by the
4615 * size of the compression mode page.
4616 */
4617 mode_buffer_len -= sizeof(sa_comp_t);
4618 }
4619 }
4620
4621 /* It is safe to retry this operation */
4622 scsi_mode_select(&ccb->csio, 5, NULL, MSG_SIMPLE_Q_TAG,
4623 (params_to_set & SA_PARAM_COMPRESSION)? TRUE : FALSE,
4624 FALSE, mode_buffer, mode_buffer_len, SSD_FULL_SIZE,
4625 softc->timeout_info[SA_TIMEOUT_MODE_SELECT]);
4626
4627 error = cam_periph_runccb(ccb, saerror, 0,
4628 sense_flags, softc->device_stats);
4629
4630 if (CAM_DEBUGGED(periph->path, CAM_DEBUG_INFO)) {
4631 int idx;
4632 char *xyz = mode_buffer;
4633 xpt_print_path(periph->path);
4634 printf("Err%d, Mode Select Data=", error);
4635 for (idx = 0; idx < mode_buffer_len; idx++)
4636 printf(" 0x%02x", xyz[idx] & 0xff);
4637 printf("\n");
4638 }
4639
4640
4641 if (error) {
4642 /*
4643 * If we can, try without setting density/blocksize.
4644 */
4645 if (mode_blk) {
4646 if ((params_to_set &
4647 (SA_PARAM_DENSITY|SA_PARAM_BLOCKSIZE)) == 0) {
4648 mode_blk = NULL;
4649 goto retry;
4650 }
4651 } else {
4652 mode_blk = (struct scsi_mode_blk_desc *)&mode_hdr[1];
4653 cpage = (sa_comp_t *)&mode_blk[1];
4654 }
4655
4656 /*
4657 * If we were setting the blocksize, and that failed, we
4658 * want to set it to its original value. If we weren't
4659 * setting the blocksize, we don't want to change it.
4660 */
4661 scsi_ulto3b(current_blocksize, mode_blk->blklen);
4662
4663 /*
4664 * Set density if requested, else preserve old density.
4665 * SCSI_SAME_DENSITY only applies to SCSI-2 or better
4666 * devices, else density we've latched up in our softc.
4667 */
4668 if (params_to_set & SA_PARAM_DENSITY) {
4669 mode_blk->density = current_density;
4670 } else if (softc->scsi_rev > SCSI_REV_CCS) {
4671 mode_blk->density = SCSI_SAME_DENSITY;
4672 } else {
4673 mode_blk->density = softc->media_density;
4674 }
4675
4676 if (params_to_set & SA_PARAM_COMPRESSION)
4677 bcopy(ccomp, cpage, sizeof (sa_comp_t));
4678
4679 /*
4680 * The retry count is the only CCB field that might have been
4681 * changed that we care about, so reset it back to 1.
4682 */
4683 ccb->ccb_h.retry_count = 1;
4684 cam_periph_runccb(ccb, saerror, 0, sense_flags,
4685 softc->device_stats);
4686 }
4687
4688 xpt_release_ccb(ccb);
4689
4690 if (ccomp != NULL)
4691 free(ccomp, M_SCSISA);
4692
4693 if (params_to_set & SA_PARAM_COMPRESSION) {
4694 if (error) {
4695 softc->flags &= ~SA_FLAG_COMP_ENABLED;
4696 /*
4697 * Even if we get an error setting compression,
4698 * do not say that we don't support it. We could
4699 * have been wrong, or it may be media specific.
4700 * softc->flags &= ~SA_FLAG_COMP_SUPP;
4701 */
4702 softc->saved_comp_algorithm = softc->comp_algorithm;
4703 softc->comp_algorithm = 0;
4704 } else {
4705 softc->flags |= SA_FLAG_COMP_ENABLED;
4706 softc->comp_algorithm = calg;
4707 }
4708 }
4709
4710 free(mode_buffer, M_SCSISA);
4711 return (error);
4712 }
4713
4714 static int
saextget(struct cdev * dev,struct cam_periph * periph,struct sbuf * sb,struct mtextget * g)4715 saextget(struct cdev *dev, struct cam_periph *periph, struct sbuf *sb,
4716 struct mtextget *g)
4717 {
4718 int indent, error;
4719 char tmpstr[80];
4720 struct sa_softc *softc;
4721 int tmpint;
4722 uint32_t maxio_tmp;
4723 struct ccb_getdev cgd;
4724
4725 softc = (struct sa_softc *)periph->softc;
4726
4727 error = 0;
4728
4729 error = sagetparams_common(dev, periph);
4730 if (error)
4731 goto extget_bailout;
4732 if (!SA_IS_CTRL(dev) && !softc->open_pending_mount)
4733 sagetpos(periph);
4734
4735 indent = 0;
4736 SASBADDNODE(sb, indent, mtextget);
4737 /*
4738 * Basic CAM peripheral information.
4739 */
4740 SASBADDVARSTR(sb, indent, periph->periph_name, %s, periph_name,
4741 strlen(periph->periph_name) + 1);
4742 SASBADDUINT(sb, indent, periph->unit_number, %u, unit_number);
4743 xpt_setup_ccb(&cgd.ccb_h,
4744 periph->path,
4745 CAM_PRIORITY_NORMAL);
4746 cgd.ccb_h.func_code = XPT_GDEV_TYPE;
4747 xpt_action((union ccb *)&cgd);
4748 if ((cgd.ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
4749 g->status = MT_EXT_GET_ERROR;
4750 snprintf(g->error_str, sizeof(g->error_str),
4751 "Error %#x returned for XPT_GDEV_TYPE CCB",
4752 cgd.ccb_h.status);
4753 goto extget_bailout;
4754 }
4755
4756 cam_strvis(tmpstr, cgd.inq_data.vendor,
4757 sizeof(cgd.inq_data.vendor), sizeof(tmpstr));
4758 SASBADDVARSTRDESC(sb, indent, tmpstr, %s, vendor,
4759 sizeof(cgd.inq_data.vendor) + 1, "SCSI Vendor ID");
4760
4761 cam_strvis(tmpstr, cgd.inq_data.product,
4762 sizeof(cgd.inq_data.product), sizeof(tmpstr));
4763 SASBADDVARSTRDESC(sb, indent, tmpstr, %s, product,
4764 sizeof(cgd.inq_data.product) + 1, "SCSI Product ID");
4765
4766 cam_strvis(tmpstr, cgd.inq_data.revision,
4767 sizeof(cgd.inq_data.revision), sizeof(tmpstr));
4768 SASBADDVARSTRDESC(sb, indent, tmpstr, %s, revision,
4769 sizeof(cgd.inq_data.revision) + 1, "SCSI Revision");
4770
4771 if (cgd.serial_num_len > 0) {
4772 char *tmpstr2;
4773 size_t ts2_len;
4774 int ts2_malloc;
4775
4776 ts2_len = 0;
4777
4778 if (cgd.serial_num_len > sizeof(tmpstr)) {
4779 ts2_len = cgd.serial_num_len + 1;
4780 ts2_malloc = 1;
4781 tmpstr2 = malloc(ts2_len, M_SCSISA, M_NOWAIT | M_ZERO);
4782 /*
4783 * The 80 characters allocated on the stack above
4784 * will handle the vast majority of serial numbers.
4785 * If we run into one that is larger than that, and
4786 * we can't malloc the length without blocking,
4787 * bail out with an out of memory error.
4788 */
4789 if (tmpstr2 == NULL) {
4790 error = ENOMEM;
4791 goto extget_bailout;
4792 }
4793 } else {
4794 ts2_len = sizeof(tmpstr);
4795 ts2_malloc = 0;
4796 tmpstr2 = tmpstr;
4797 }
4798
4799 cam_strvis(tmpstr2, cgd.serial_num, cgd.serial_num_len,
4800 ts2_len);
4801
4802 SASBADDVARSTRDESC(sb, indent, tmpstr2, %s, serial_num,
4803 (ssize_t)cgd.serial_num_len + 1, "Serial Number");
4804 if (ts2_malloc != 0)
4805 free(tmpstr2, M_SCSISA);
4806 } else {
4807 /*
4808 * We return a serial_num element in any case, but it will
4809 * be empty if the device has no serial number.
4810 */
4811 tmpstr[0] = '\0';
4812 SASBADDVARSTRDESC(sb, indent, tmpstr, %s, serial_num,
4813 (ssize_t)0, "Serial Number");
4814 }
4815
4816 SASBADDUINTDESC(sb, indent, softc->maxio, %u, maxio,
4817 "Maximum I/O size allowed by driver and controller");
4818
4819 SASBADDUINTDESC(sb, indent, softc->cpi_maxio, %u, cpi_maxio,
4820 "Maximum I/O size reported by controller");
4821
4822 SASBADDUINTDESC(sb, indent, softc->max_blk, %u, max_blk,
4823 "Maximum block size supported by tape drive and media");
4824
4825 SASBADDUINTDESC(sb, indent, softc->min_blk, %u, min_blk,
4826 "Minimum block size supported by tape drive and media");
4827
4828 SASBADDUINTDESC(sb, indent, softc->blk_gran, %u, blk_gran,
4829 "Block granularity supported by tape drive and media");
4830
4831 maxio_tmp = min(softc->max_blk, softc->maxio);
4832
4833 SASBADDUINTDESC(sb, indent, maxio_tmp, %u, max_effective_iosize,
4834 "Maximum possible I/O size");
4835
4836 SASBADDINTDESC(sb, indent, softc->flags & SA_FLAG_FIXED ? 1 : 0, %d,
4837 fixed_mode, "Set to 1 for fixed block mode, 0 for variable block");
4838
4839 /*
4840 * XXX KDM include SIM, bus, target, LUN?
4841 */
4842 if (softc->flags & SA_FLAG_COMP_UNSUPP)
4843 tmpint = 0;
4844 else
4845 tmpint = 1;
4846 SASBADDINTDESC(sb, indent, tmpint, %d, compression_supported,
4847 "Set to 1 if compression is supported, 0 if not");
4848 if (softc->flags & SA_FLAG_COMP_ENABLED)
4849 tmpint = 1;
4850 else
4851 tmpint = 0;
4852 SASBADDINTDESC(sb, indent, tmpint, %d, compression_enabled,
4853 "Set to 1 if compression is enabled, 0 if not");
4854 SASBADDUINTDESC(sb, indent, softc->comp_algorithm, %u,
4855 compression_algorithm, "Numeric compression algorithm");
4856
4857 safillprot(softc, &indent, sb);
4858
4859 SASBADDUINTDESC(sb, indent, softc->media_blksize, %u,
4860 media_blocksize, "Block size reported by drive or set by user");
4861 SASBADDINTDESC(sb, indent, (intmax_t)softc->fileno, %jd,
4862 calculated_fileno, "Calculated file number, -1 if unknown");
4863 SASBADDINTDESC(sb, indent, (intmax_t)softc->blkno, %jd,
4864 calculated_rel_blkno, "Calculated block number relative to file, "
4865 "set to -1 if unknown");
4866 SASBADDINTDESC(sb, indent, (intmax_t)softc->rep_fileno, %jd,
4867 reported_fileno, "File number reported by drive, -1 if unknown");
4868 SASBADDINTDESC(sb, indent, (intmax_t)softc->rep_blkno, %jd,
4869 reported_blkno, "Block number relative to BOP/BOT reported by "
4870 "drive, -1 if unknown");
4871 SASBADDINTDESC(sb, indent, (intmax_t)softc->partition, %jd,
4872 partition, "Current partition number, 0 is the default");
4873 SASBADDINTDESC(sb, indent, softc->bop, %d, bop,
4874 "Set to 1 if drive is at the beginning of partition/tape, 0 if "
4875 "not, -1 if unknown");
4876 SASBADDINTDESC(sb, indent, softc->eop, %d, eop,
4877 "Set to 1 if drive is past early warning, 0 if not, -1 if unknown");
4878 SASBADDINTDESC(sb, indent, softc->bpew, %d, bpew,
4879 "Set to 1 if drive is past programmable early warning, 0 if not, "
4880 "-1 if unknown");
4881 SASBADDINTDESC(sb, indent, (intmax_t)softc->last_io_resid, %jd,
4882 residual, "Residual for the last I/O");
4883 /*
4884 * XXX KDM should we send a string with the current driver
4885 * status already decoded instead of a numeric value?
4886 */
4887 SASBADDINTDESC(sb, indent, softc->dsreg, %d, dsreg,
4888 "Current state of the driver");
4889
4890 safilldensitysb(softc, &indent, sb);
4891
4892 SASBENDNODE(sb, indent, mtextget);
4893
4894 extget_bailout:
4895
4896 return (error);
4897 }
4898
4899 static int
saparamget(struct sa_softc * softc,struct sbuf * sb)4900 saparamget(struct sa_softc *softc, struct sbuf *sb)
4901 {
4902 int indent;
4903
4904 indent = 0;
4905 SASBADDNODE(sb, indent, mtparamget);
4906 SASBADDINTDESC(sb, indent, softc->sili, %d, sili,
4907 "Suppress an error on underlength variable reads");
4908 SASBADDINTDESC(sb, indent, softc->eot_warn, %d, eot_warn,
4909 "Return an error to warn that end of tape is approaching");
4910 safillprot(softc, &indent, sb);
4911 SASBENDNODE(sb, indent, mtparamget);
4912
4913 return (0);
4914 }
4915
4916 static void
saprevent(struct cam_periph * periph,int action)4917 saprevent(struct cam_periph *periph, int action)
4918 {
4919 struct sa_softc *softc;
4920 union ccb *ccb;
4921 int error, sf;
4922
4923 softc = (struct sa_softc *)periph->softc;
4924
4925 if ((action == PR_ALLOW) && (softc->flags & SA_FLAG_TAPE_LOCKED) == 0)
4926 return;
4927 if ((action == PR_PREVENT) && (softc->flags & SA_FLAG_TAPE_LOCKED) != 0)
4928 return;
4929
4930 /*
4931 * We can be quiet about illegal requests.
4932 */
4933 if (CAM_DEBUGGED(periph->path, CAM_DEBUG_INFO)) {
4934 sf = 0;
4935 } else
4936 sf = SF_QUIET_IR;
4937
4938 ccb = cam_periph_getccb(periph, 1);
4939
4940 /* It is safe to retry this operation */
4941 scsi_prevent(&ccb->csio, 5, NULL, MSG_SIMPLE_Q_TAG, action,
4942 SSD_FULL_SIZE, softc->timeout_info[SA_TIMEOUT_PREVENT]);
4943
4944 error = cam_periph_runccb(ccb, saerror, 0, sf, softc->device_stats);
4945 if (error == 0) {
4946 if (action == PR_ALLOW)
4947 softc->flags &= ~SA_FLAG_TAPE_LOCKED;
4948 else
4949 softc->flags |= SA_FLAG_TAPE_LOCKED;
4950 }
4951
4952 xpt_release_ccb(ccb);
4953 }
4954
4955 static int
sarewind(struct cam_periph * periph)4956 sarewind(struct cam_periph *periph)
4957 {
4958 union ccb *ccb;
4959 struct sa_softc *softc;
4960 int error;
4961
4962 softc = (struct sa_softc *)periph->softc;
4963
4964 ccb = cam_periph_getccb(periph, 1);
4965
4966 /* It is safe to retry this operation */
4967 scsi_rewind(&ccb->csio, 2, NULL, MSG_SIMPLE_Q_TAG, FALSE,
4968 SSD_FULL_SIZE, softc->timeout_info[SA_TIMEOUT_REWIND]);
4969
4970 softc->dsreg = MTIO_DSREG_REW;
4971 error = cam_periph_runccb(ccb, saerror, 0, 0, softc->device_stats);
4972 softc->dsreg = MTIO_DSREG_REST;
4973
4974 xpt_release_ccb(ccb);
4975 if (error == 0) {
4976 softc->partition = softc->fileno = softc->blkno = (daddr_t) 0;
4977 softc->rep_fileno = softc->rep_blkno = (daddr_t) 0;
4978 } else {
4979 softc->fileno = softc->blkno = (daddr_t) -1;
4980 softc->partition = (daddr_t) -1;
4981 softc->rep_fileno = softc->rep_blkno = (daddr_t) -1;
4982 }
4983 return (error);
4984 }
4985
4986 static int
saspace(struct cam_periph * periph,int count,scsi_space_code code)4987 saspace(struct cam_periph *periph, int count, scsi_space_code code)
4988 {
4989 union ccb *ccb;
4990 struct sa_softc *softc;
4991 int error;
4992
4993 softc = (struct sa_softc *)periph->softc;
4994
4995 ccb = cam_periph_getccb(periph, 1);
4996
4997 /* This cannot be retried */
4998
4999 scsi_space(&ccb->csio, 0, NULL, MSG_SIMPLE_Q_TAG, code, count,
5000 SSD_FULL_SIZE, softc->timeout_info[SA_TIMEOUT_SPACE]);
5001
5002 /*
5003 * Clear residual because we will be using it.
5004 */
5005 softc->last_ctl_resid = 0;
5006
5007 softc->dsreg = (count < 0)? MTIO_DSREG_REV : MTIO_DSREG_FWD;
5008 error = cam_periph_runccb(ccb, saerror, 0, 0, softc->device_stats);
5009 softc->dsreg = MTIO_DSREG_REST;
5010
5011 xpt_release_ccb(ccb);
5012
5013 /*
5014 * If a spacing operation has failed, we need to invalidate
5015 * this mount.
5016 *
5017 * If the spacing operation was setmarks or to end of recorded data,
5018 * we no longer know our relative position.
5019 *
5020 * If the spacing operations was spacing files in reverse, we
5021 * take account of the residual, but still check against less
5022 * than zero- if we've gone negative, we must have hit BOT.
5023 *
5024 * If the spacing operations was spacing records in reverse and
5025 * we have a residual, we've either hit BOT or hit a filemark.
5026 * In the former case, we know our new record number (0). In
5027 * the latter case, we have absolutely no idea what the real
5028 * record number is- we've stopped between the end of the last
5029 * record in the previous file and the filemark that stopped
5030 * our spacing backwards.
5031 */
5032 if (error) {
5033 softc->fileno = softc->blkno = (daddr_t) -1;
5034 softc->rep_blkno = softc->partition = (daddr_t) -1;
5035 softc->rep_fileno = (daddr_t) -1;
5036 } else if (code == SS_SETMARKS || code == SS_EOD) {
5037 softc->fileno = softc->blkno = (daddr_t) -1;
5038 } else if (code == SS_FILEMARKS && softc->fileno != (daddr_t) -1) {
5039 softc->fileno += (count - softc->last_ctl_resid);
5040 if (softc->fileno < 0) /* we must of hit BOT */
5041 softc->fileno = 0;
5042 softc->blkno = 0;
5043 } else if (code == SS_BLOCKS && softc->blkno != (daddr_t) -1) {
5044 softc->blkno += (count - softc->last_ctl_resid);
5045 if (count < 0) {
5046 if (softc->last_ctl_resid || softc->blkno < 0) {
5047 if (softc->fileno == 0) {
5048 softc->blkno = 0;
5049 } else {
5050 softc->blkno = (daddr_t) -1;
5051 }
5052 }
5053 }
5054 }
5055 if (error == 0)
5056 sagetpos(periph);
5057
5058 return (error);
5059 }
5060
5061 static int
sawritefilemarks(struct cam_periph * periph,int nmarks,int setmarks,int immed)5062 sawritefilemarks(struct cam_periph *periph, int nmarks, int setmarks, int immed)
5063 {
5064 union ccb *ccb;
5065 struct sa_softc *softc;
5066 int error, nwm = 0;
5067
5068 softc = (struct sa_softc *)periph->softc;
5069 if (softc->open_rdonly)
5070 return (EBADF);
5071
5072 ccb = cam_periph_getccb(periph, 1);
5073 /*
5074 * Clear residual because we will be using it.
5075 */
5076 softc->last_ctl_resid = 0;
5077
5078 softc->dsreg = MTIO_DSREG_FMK;
5079 /* this *must* not be retried */
5080 scsi_write_filemarks(&ccb->csio, 0, NULL, MSG_SIMPLE_Q_TAG,
5081 immed, setmarks, nmarks, SSD_FULL_SIZE,
5082 softc->timeout_info[SA_TIMEOUT_WRITE_FILEMARKS]);
5083 softc->dsreg = MTIO_DSREG_REST;
5084
5085
5086 error = cam_periph_runccb(ccb, saerror, 0, 0, softc->device_stats);
5087
5088 if (error == 0 && nmarks) {
5089 struct sa_softc *softc = (struct sa_softc *)periph->softc;
5090 nwm = nmarks - softc->last_ctl_resid;
5091 softc->filemarks += nwm;
5092 }
5093
5094 xpt_release_ccb(ccb);
5095
5096 /*
5097 * Update relative positions (if we're doing that).
5098 */
5099 if (error) {
5100 softc->fileno = softc->blkno = softc->partition = (daddr_t) -1;
5101 } else if (softc->fileno != (daddr_t) -1) {
5102 softc->fileno += nwm;
5103 softc->blkno = 0;
5104 }
5105
5106 /*
5107 * Ask the tape drive for position information.
5108 */
5109 sagetpos(periph);
5110
5111 /*
5112 * If we got valid position information, since we just wrote a file
5113 * mark, we know we're at the file mark and block 0 after that
5114 * filemark.
5115 */
5116 if (softc->rep_fileno != (daddr_t) -1) {
5117 softc->fileno = softc->rep_fileno;
5118 softc->blkno = 0;
5119 }
5120
5121 return (error);
5122 }
5123
5124 static int
sagetpos(struct cam_periph * periph)5125 sagetpos(struct cam_periph *periph)
5126 {
5127 union ccb *ccb;
5128 struct scsi_tape_position_long_data long_pos;
5129 struct sa_softc *softc = (struct sa_softc *)periph->softc;
5130 int error;
5131
5132 if (softc->quirks & SA_QUIRK_NO_LONG_POS) {
5133 softc->rep_fileno = (daddr_t) -1;
5134 softc->rep_blkno = (daddr_t) -1;
5135 softc->bop = softc->eop = softc->bpew = -1;
5136 return (EOPNOTSUPP);
5137 }
5138
5139 bzero(&long_pos, sizeof(long_pos));
5140
5141 ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL);
5142 scsi_read_position_10(&ccb->csio,
5143 /*retries*/ 1,
5144 /*cbfcnp*/ NULL,
5145 /*tag_action*/ MSG_SIMPLE_Q_TAG,
5146 /*service_action*/ SA_RPOS_LONG_FORM,
5147 /*data_ptr*/ (uint8_t *)&long_pos,
5148 /*length*/ sizeof(long_pos),
5149 /*sense_len*/ SSD_FULL_SIZE,
5150 /*timeout*/
5151 softc->timeout_info[SA_TIMEOUT_READ_POSITION]);
5152
5153 softc->dsreg = MTIO_DSREG_RBSY;
5154 error = cam_periph_runccb(ccb, saerror, 0, SF_QUIET_IR,
5155 softc->device_stats);
5156 softc->dsreg = MTIO_DSREG_REST;
5157
5158 if (error == 0) {
5159 if (long_pos.flags & SA_RPOS_LONG_MPU) {
5160 /*
5161 * If the drive doesn't know what file mark it is
5162 * on, our calculated filemark isn't going to be
5163 * accurate either.
5164 */
5165 softc->fileno = (daddr_t) -1;
5166 softc->rep_fileno = (daddr_t) -1;
5167 } else {
5168 softc->fileno = softc->rep_fileno =
5169 scsi_8btou64(long_pos.logical_file_num);
5170 }
5171
5172 if (long_pos.flags & SA_RPOS_LONG_LONU) {
5173 softc->partition = (daddr_t) -1;
5174 softc->rep_blkno = (daddr_t) -1;
5175 /*
5176 * If the tape drive doesn't know its block
5177 * position, we can't claim to know it either.
5178 */
5179 softc->blkno = (daddr_t) -1;
5180 } else {
5181 softc->partition = scsi_4btoul(long_pos.partition);
5182 softc->rep_blkno =
5183 scsi_8btou64(long_pos.logical_object_num);
5184 }
5185 if (long_pos.flags & SA_RPOS_LONG_BOP)
5186 softc->bop = 1;
5187 else
5188 softc->bop = 0;
5189
5190 if (long_pos.flags & SA_RPOS_LONG_EOP)
5191 softc->eop = 1;
5192 else
5193 softc->eop = 0;
5194
5195 if ((long_pos.flags & SA_RPOS_LONG_BPEW)
5196 || (softc->set_pews_status != 0)) {
5197 softc->bpew = 1;
5198 if (softc->set_pews_status > 0)
5199 softc->set_pews_status--;
5200 } else
5201 softc->bpew = 0;
5202 } else if (error == EINVAL) {
5203 /*
5204 * If this drive returned an invalid-request type error,
5205 * then it likely doesn't support the long form report.
5206 */
5207 softc->quirks |= SA_QUIRK_NO_LONG_POS;
5208 }
5209
5210 if (error != 0) {
5211 softc->rep_fileno = softc->rep_blkno = (daddr_t) -1;
5212 softc->partition = (daddr_t) -1;
5213 softc->bop = softc->eop = softc->bpew = -1;
5214 }
5215
5216 xpt_release_ccb(ccb);
5217
5218 return (error);
5219 }
5220
5221 static int
sardpos(struct cam_periph * periph,int hard,u_int32_t * blkptr)5222 sardpos(struct cam_periph *periph, int hard, u_int32_t *blkptr)
5223 {
5224 struct scsi_tape_position_data loc;
5225 union ccb *ccb;
5226 struct sa_softc *softc = (struct sa_softc *)periph->softc;
5227 int error;
5228
5229 /*
5230 * We try and flush any buffered writes here if we were writing
5231 * and we're trying to get hardware block position. It eats
5232 * up performance substantially, but I'm wary of drive firmware.
5233 *
5234 * I think that *logical* block position is probably okay-
5235 * but hardware block position might have to wait for data
5236 * to hit media to be valid. Caveat Emptor.
5237 */
5238
5239 if (hard && (softc->flags & SA_FLAG_TAPE_WRITTEN)) {
5240 error = sawritefilemarks(periph, 0, 0, 0);
5241 if (error && error != EACCES)
5242 return (error);
5243 }
5244
5245 ccb = cam_periph_getccb(periph, 1);
5246 scsi_read_position(&ccb->csio, 1, NULL, MSG_SIMPLE_Q_TAG,
5247 hard, &loc, SSD_FULL_SIZE,
5248 softc->timeout_info[SA_TIMEOUT_READ_POSITION]);
5249 softc->dsreg = MTIO_DSREG_RBSY;
5250 error = cam_periph_runccb(ccb, saerror, 0, 0, softc->device_stats);
5251 softc->dsreg = MTIO_DSREG_REST;
5252
5253 if (error == 0) {
5254 if (loc.flags & SA_RPOS_UNCERTAIN) {
5255 error = EINVAL; /* nothing is certain */
5256 } else {
5257 *blkptr = scsi_4btoul(loc.firstblk);
5258 }
5259 }
5260
5261 xpt_release_ccb(ccb);
5262 return (error);
5263 }
5264
5265 static int
sasetpos(struct cam_periph * periph,int hard,struct mtlocate * locate_info)5266 sasetpos(struct cam_periph *periph, int hard, struct mtlocate *locate_info)
5267 {
5268 union ccb *ccb;
5269 struct sa_softc *softc;
5270 int locate16;
5271 int immed, cp;
5272 int error;
5273
5274 /*
5275 * We used to try and flush any buffered writes here.
5276 * Now we push this onto user applications to either
5277 * flush the pending writes themselves (via a zero count
5278 * WRITE FILEMARKS command) or they can trust their tape
5279 * drive to do this correctly for them.
5280 */
5281
5282 softc = (struct sa_softc *)periph->softc;
5283 ccb = cam_periph_getccb(periph, 1);
5284
5285 cp = locate_info->flags & MT_LOCATE_FLAG_CHANGE_PART ? 1 : 0;
5286 immed = locate_info->flags & MT_LOCATE_FLAG_IMMED ? 1 : 0;
5287
5288 /*
5289 * Determine whether we have to use LOCATE or LOCATE16. The hard
5290 * bit is only possible with LOCATE, but the new ioctls do not
5291 * allow setting that bit. So we can't get into the situation of
5292 * having the hard bit set with a block address that is larger than
5293 * 32-bits.
5294 */
5295 if (hard != 0)
5296 locate16 = 0;
5297 else if ((locate_info->dest_type != MT_LOCATE_DEST_OBJECT)
5298 || (locate_info->block_address_mode != MT_LOCATE_BAM_IMPLICIT)
5299 || (locate_info->logical_id > SA_SPOS_MAX_BLK))
5300 locate16 = 1;
5301 else
5302 locate16 = 0;
5303
5304 if (locate16 != 0) {
5305 scsi_locate_16(&ccb->csio,
5306 /*retries*/ 1,
5307 /*cbfcnp*/ NULL,
5308 /*tag_action*/ MSG_SIMPLE_Q_TAG,
5309 /*immed*/ immed,
5310 /*cp*/ cp,
5311 /*dest_type*/ locate_info->dest_type,
5312 /*bam*/ locate_info->block_address_mode,
5313 /*partition*/ locate_info->partition,
5314 /*logical_id*/ locate_info->logical_id,
5315 /*sense_len*/ SSD_FULL_SIZE,
5316 /*timeout*/
5317 softc->timeout_info[SA_TIMEOUT_LOCATE]);
5318 } else {
5319 scsi_locate_10(&ccb->csio,
5320 /*retries*/ 1,
5321 /*cbfcnp*/ NULL,
5322 /*tag_action*/ MSG_SIMPLE_Q_TAG,
5323 /*immed*/ immed,
5324 /*cp*/ cp,
5325 /*hard*/ hard,
5326 /*partition*/ locate_info->partition,
5327 /*block_address*/ locate_info->logical_id,
5328 /*sense_len*/ SSD_FULL_SIZE,
5329 /*timeout*/
5330 softc->timeout_info[SA_TIMEOUT_LOCATE]);
5331 }
5332
5333 softc->dsreg = MTIO_DSREG_POS;
5334 error = cam_periph_runccb(ccb, saerror, 0, 0, softc->device_stats);
5335 softc->dsreg = MTIO_DSREG_REST;
5336 xpt_release_ccb(ccb);
5337
5338 /*
5339 * We assume the calculated file and block numbers are unknown
5340 * unless we have enough information to populate them.
5341 */
5342 softc->fileno = softc->blkno = (daddr_t) -1;
5343
5344 /*
5345 * If the user requested changing the partition and the request
5346 * succeeded, note the partition.
5347 */
5348 if ((error == 0)
5349 && (cp != 0))
5350 softc->partition = locate_info->partition;
5351 else
5352 softc->partition = (daddr_t) -1;
5353
5354 if (error == 0) {
5355 switch (locate_info->dest_type) {
5356 case MT_LOCATE_DEST_FILE:
5357 /*
5358 * This is the only case where we can reliably
5359 * calculate the file and block numbers.
5360 */
5361 softc->fileno = locate_info->logical_id;
5362 softc->blkno = 0;
5363 break;
5364 case MT_LOCATE_DEST_OBJECT:
5365 case MT_LOCATE_DEST_SET:
5366 case MT_LOCATE_DEST_EOD:
5367 default:
5368 break;
5369 }
5370 }
5371
5372 /*
5373 * Ask the drive for current position information.
5374 */
5375 sagetpos(periph);
5376
5377 return (error);
5378 }
5379
5380 static int
saretension(struct cam_periph * periph)5381 saretension(struct cam_periph *periph)
5382 {
5383 union ccb *ccb;
5384 struct sa_softc *softc;
5385 int error;
5386
5387 softc = (struct sa_softc *)periph->softc;
5388
5389 ccb = cam_periph_getccb(periph, 1);
5390
5391 /* It is safe to retry this operation */
5392 scsi_load_unload(&ccb->csio, 5, NULL, MSG_SIMPLE_Q_TAG, FALSE,
5393 FALSE, TRUE, TRUE, SSD_FULL_SIZE,
5394 softc->timeout_info[SA_TIMEOUT_LOAD]);
5395
5396 softc->dsreg = MTIO_DSREG_TEN;
5397 error = cam_periph_runccb(ccb, saerror, 0, 0, softc->device_stats);
5398 softc->dsreg = MTIO_DSREG_REST;
5399
5400 xpt_release_ccb(ccb);
5401 if (error == 0) {
5402 softc->partition = softc->fileno = softc->blkno = (daddr_t) 0;
5403 sagetpos(periph);
5404 } else
5405 softc->partition = softc->fileno = softc->blkno = (daddr_t) -1;
5406 return (error);
5407 }
5408
5409 static int
sareservereleaseunit(struct cam_periph * periph,int reserve)5410 sareservereleaseunit(struct cam_periph *periph, int reserve)
5411 {
5412 union ccb *ccb;
5413 struct sa_softc *softc;
5414 int error;
5415
5416 softc = (struct sa_softc *)periph->softc;
5417 ccb = cam_periph_getccb(periph, 1);
5418
5419 /* It is safe to retry this operation */
5420 scsi_reserve_release_unit(&ccb->csio, 2, NULL, MSG_SIMPLE_Q_TAG,
5421 FALSE, 0, SSD_FULL_SIZE, softc->timeout_info[SA_TIMEOUT_RESERVE],
5422 reserve);
5423 softc->dsreg = MTIO_DSREG_RBSY;
5424 error = cam_periph_runccb(ccb, saerror, 0,
5425 SF_RETRY_UA | SF_NO_PRINT, softc->device_stats);
5426 softc->dsreg = MTIO_DSREG_REST;
5427 xpt_release_ccb(ccb);
5428
5429 /*
5430 * If the error was Illegal Request, then the device doesn't support
5431 * RESERVE/RELEASE. This is not an error.
5432 */
5433 if (error == EINVAL) {
5434 error = 0;
5435 }
5436
5437 return (error);
5438 }
5439
5440 static int
saloadunload(struct cam_periph * periph,int load)5441 saloadunload(struct cam_periph *periph, int load)
5442 {
5443 union ccb *ccb;
5444 struct sa_softc *softc;
5445 int error;
5446
5447 softc = (struct sa_softc *)periph->softc;
5448
5449 ccb = cam_periph_getccb(periph, 1);
5450
5451 /* It is safe to retry this operation */
5452 scsi_load_unload(&ccb->csio, 5, NULL, MSG_SIMPLE_Q_TAG, FALSE,
5453 FALSE, FALSE, load, SSD_FULL_SIZE,
5454 softc->timeout_info[SA_TIMEOUT_LOAD]);
5455
5456 softc->dsreg = (load)? MTIO_DSREG_LD : MTIO_DSREG_UNL;
5457 error = cam_periph_runccb(ccb, saerror, 0, 0, softc->device_stats);
5458 softc->dsreg = MTIO_DSREG_REST;
5459 xpt_release_ccb(ccb);
5460
5461 if (error || load == 0) {
5462 softc->partition = softc->fileno = softc->blkno = (daddr_t) -1;
5463 softc->rep_fileno = softc->rep_blkno = (daddr_t) -1;
5464 } else if (error == 0) {
5465 softc->partition = softc->fileno = softc->blkno = (daddr_t) 0;
5466 sagetpos(periph);
5467 }
5468 return (error);
5469 }
5470
5471 static int
saerase(struct cam_periph * periph,int longerase)5472 saerase(struct cam_periph *periph, int longerase)
5473 {
5474
5475 union ccb *ccb;
5476 struct sa_softc *softc;
5477 int error;
5478
5479 softc = (struct sa_softc *)periph->softc;
5480 if (softc->open_rdonly)
5481 return (EBADF);
5482
5483 ccb = cam_periph_getccb(periph, 1);
5484
5485 scsi_erase(&ccb->csio, 1, NULL, MSG_SIMPLE_Q_TAG, FALSE, longerase,
5486 SSD_FULL_SIZE, softc->timeout_info[SA_TIMEOUT_ERASE]);
5487
5488 softc->dsreg = MTIO_DSREG_ZER;
5489 error = cam_periph_runccb(ccb, saerror, 0, 0, softc->device_stats);
5490 softc->dsreg = MTIO_DSREG_REST;
5491
5492 xpt_release_ccb(ccb);
5493 return (error);
5494 }
5495
5496 /*
5497 * Fill an sbuf with density data in XML format. This particular macro
5498 * works for multi-byte integer fields.
5499 *
5500 * Note that 1 byte fields aren't supported here. The reason is that the
5501 * compiler does not evaluate the sizeof(), and assumes that any of the
5502 * sizes are possible for a given field. So passing in a multi-byte
5503 * field will result in a warning that the assignment makes an integer
5504 * from a pointer without a cast, if there is an assignment in the 1 byte
5505 * case.
5506 */
5507 #define SAFILLDENSSB(dens_data, sb, indent, field, desc_remain, \
5508 len_to_go, cur_offset, desc){ \
5509 size_t cur_field_len; \
5510 \
5511 cur_field_len = sizeof(dens_data->field); \
5512 if (desc_remain < cur_field_len) { \
5513 len_to_go -= desc_remain; \
5514 cur_offset += desc_remain; \
5515 continue; \
5516 } \
5517 len_to_go -= cur_field_len; \
5518 cur_offset += cur_field_len; \
5519 desc_remain -= cur_field_len; \
5520 \
5521 switch (sizeof(dens_data->field)) { \
5522 case 1: \
5523 KASSERT(1 == 0, ("Programmer error, invalid 1 byte " \
5524 "field width for SAFILLDENSFIELD")); \
5525 break; \
5526 case 2: \
5527 SASBADDUINTDESC(sb, indent, \
5528 scsi_2btoul(dens_data->field), %u, field, desc); \
5529 break; \
5530 case 3: \
5531 SASBADDUINTDESC(sb, indent, \
5532 scsi_3btoul(dens_data->field), %u, field, desc); \
5533 break; \
5534 case 4: \
5535 SASBADDUINTDESC(sb, indent, \
5536 scsi_4btoul(dens_data->field), %u, field, desc); \
5537 break; \
5538 case 8: \
5539 SASBADDUINTDESC(sb, indent, \
5540 (uintmax_t)scsi_8btou64(dens_data->field), %ju, \
5541 field, desc); \
5542 break; \
5543 default: \
5544 break; \
5545 } \
5546 };
5547 /*
5548 * Fill an sbuf with density data in XML format. This particular macro
5549 * works for strings.
5550 */
5551 #define SAFILLDENSSBSTR(dens_data, sb, indent, field, desc_remain, \
5552 len_to_go, cur_offset, desc){ \
5553 size_t cur_field_len; \
5554 char tmpstr[32]; \
5555 \
5556 cur_field_len = sizeof(dens_data->field); \
5557 if (desc_remain < cur_field_len) { \
5558 len_to_go -= desc_remain; \
5559 cur_offset += desc_remain; \
5560 continue; \
5561 } \
5562 len_to_go -= cur_field_len; \
5563 cur_offset += cur_field_len; \
5564 desc_remain -= cur_field_len; \
5565 \
5566 cam_strvis(tmpstr, dens_data->field, \
5567 sizeof(dens_data->field), sizeof(tmpstr)); \
5568 SASBADDVARSTRDESC(sb, indent, tmpstr, %s, field, \
5569 strlen(tmpstr) + 1, desc); \
5570 };
5571
5572 /*
5573 * Fill an sbuf with density data descriptors.
5574 */
5575 static void
safilldenstypesb(struct sbuf * sb,int * indent,uint8_t * buf,int buf_len,int is_density)5576 safilldenstypesb(struct sbuf *sb, int *indent, uint8_t *buf, int buf_len,
5577 int is_density)
5578 {
5579 struct scsi_density_hdr *hdr;
5580 uint32_t hdr_len;
5581 int len_to_go, cur_offset;
5582 int length_offset;
5583 int num_reports, need_close;
5584
5585 /*
5586 * We need at least the header length. Note that this isn't an
5587 * error, not all tape drives will have every data type.
5588 */
5589 if (buf_len < sizeof(*hdr))
5590 goto bailout;
5591
5592
5593 hdr = (struct scsi_density_hdr *)buf;
5594 hdr_len = scsi_2btoul(hdr->length);
5595 len_to_go = min(buf_len - sizeof(*hdr), hdr_len);
5596 if (is_density) {
5597 length_offset = __offsetof(struct scsi_density_data,
5598 bits_per_mm);
5599 } else {
5600 length_offset = __offsetof(struct scsi_medium_type_data,
5601 num_density_codes);
5602 }
5603 cur_offset = sizeof(*hdr);
5604
5605 num_reports = 0;
5606 need_close = 0;
5607
5608 while (len_to_go > length_offset) {
5609 struct scsi_density_data *dens_data;
5610 struct scsi_medium_type_data *type_data;
5611 int desc_remain;
5612 size_t cur_field_len;
5613
5614 dens_data = NULL;
5615 type_data = NULL;
5616
5617 if (is_density) {
5618 dens_data =(struct scsi_density_data *)&buf[cur_offset];
5619 if (dens_data->byte2 & SDD_DLV)
5620 desc_remain = scsi_2btoul(dens_data->length);
5621 else
5622 desc_remain = SDD_DEFAULT_LENGTH -
5623 length_offset;
5624 } else {
5625 type_data = (struct scsi_medium_type_data *)
5626 &buf[cur_offset];
5627 desc_remain = scsi_2btoul(type_data->length);
5628 }
5629
5630 len_to_go -= length_offset;
5631 desc_remain = min(desc_remain, len_to_go);
5632 cur_offset += length_offset;
5633
5634 if (need_close != 0) {
5635 SASBENDNODE(sb, *indent, density_entry);
5636 }
5637
5638 SASBADDNODENUM(sb, *indent, density_entry, num_reports);
5639 num_reports++;
5640 need_close = 1;
5641
5642 if (is_density) {
5643 SASBADDUINTDESC(sb, *indent,
5644 dens_data->primary_density_code, %u,
5645 primary_density_code, "Primary Density Code");
5646 SASBADDUINTDESC(sb, *indent,
5647 dens_data->secondary_density_code, %u,
5648 secondary_density_code, "Secondary Density Code");
5649 SASBADDUINTDESC(sb, *indent,
5650 dens_data->byte2 & ~SDD_DLV, %#x, density_flags,
5651 "Density Flags");
5652
5653 SAFILLDENSSB(dens_data, sb, *indent, bits_per_mm,
5654 desc_remain, len_to_go, cur_offset, "Bits per mm");
5655 SAFILLDENSSB(dens_data, sb, *indent, media_width,
5656 desc_remain, len_to_go, cur_offset, "Media width");
5657 SAFILLDENSSB(dens_data, sb, *indent, tracks,
5658 desc_remain, len_to_go, cur_offset,
5659 "Number of Tracks");
5660 SAFILLDENSSB(dens_data, sb, *indent, capacity,
5661 desc_remain, len_to_go, cur_offset, "Capacity");
5662
5663 SAFILLDENSSBSTR(dens_data, sb, *indent, assigning_org,
5664 desc_remain, len_to_go, cur_offset,
5665 "Assigning Organization");
5666
5667 SAFILLDENSSBSTR(dens_data, sb, *indent, density_name,
5668 desc_remain, len_to_go, cur_offset, "Density Name");
5669
5670 SAFILLDENSSBSTR(dens_data, sb, *indent, description,
5671 desc_remain, len_to_go, cur_offset, "Description");
5672 } else {
5673 int i;
5674
5675 SASBADDUINTDESC(sb, *indent, type_data->medium_type,
5676 %u, medium_type, "Medium Type");
5677
5678 cur_field_len =
5679 __offsetof(struct scsi_medium_type_data,
5680 media_width) -
5681 __offsetof(struct scsi_medium_type_data,
5682 num_density_codes);
5683
5684 if (desc_remain < cur_field_len) {
5685 len_to_go -= desc_remain;
5686 cur_offset += desc_remain;
5687 continue;
5688 }
5689 len_to_go -= cur_field_len;
5690 cur_offset += cur_field_len;
5691 desc_remain -= cur_field_len;
5692
5693 SASBADDINTDESC(sb, *indent,
5694 type_data->num_density_codes, %d,
5695 num_density_codes, "Number of Density Codes");
5696 SASBADDNODE(sb, *indent, density_code_list);
5697 for (i = 0; i < type_data->num_density_codes;
5698 i++) {
5699 SASBADDUINTDESC(sb, *indent,
5700 type_data->primary_density_codes[i], %u,
5701 density_code, "Density Code");
5702 }
5703 SASBENDNODE(sb, *indent, density_code_list);
5704
5705 SAFILLDENSSB(type_data, sb, *indent, media_width,
5706 desc_remain, len_to_go, cur_offset,
5707 "Media width");
5708 SAFILLDENSSB(type_data, sb, *indent, medium_length,
5709 desc_remain, len_to_go, cur_offset,
5710 "Medium length");
5711
5712 /*
5713 * Account for the two reserved bytes.
5714 */
5715 cur_field_len = sizeof(type_data->reserved2);
5716 if (desc_remain < cur_field_len) {
5717 len_to_go -= desc_remain;
5718 cur_offset += desc_remain;
5719 continue;
5720 }
5721 len_to_go -= cur_field_len;
5722 cur_offset += cur_field_len;
5723 desc_remain -= cur_field_len;
5724
5725 SAFILLDENSSBSTR(type_data, sb, *indent, assigning_org,
5726 desc_remain, len_to_go, cur_offset,
5727 "Assigning Organization");
5728 SAFILLDENSSBSTR(type_data, sb, *indent,
5729 medium_type_name, desc_remain, len_to_go,
5730 cur_offset, "Medium type name");
5731 SAFILLDENSSBSTR(type_data, sb, *indent, description,
5732 desc_remain, len_to_go, cur_offset, "Description");
5733
5734 }
5735 }
5736 if (need_close != 0) {
5737 SASBENDNODE(sb, *indent, density_entry);
5738 }
5739
5740 bailout:
5741 return;
5742 }
5743
5744 /*
5745 * Fill an sbuf with density data information
5746 */
5747 static void
safilldensitysb(struct sa_softc * softc,int * indent,struct sbuf * sb)5748 safilldensitysb(struct sa_softc *softc, int *indent, struct sbuf *sb)
5749 {
5750 int i, is_density;
5751
5752 SASBADDNODE(sb, *indent, mtdensity);
5753 SASBADDUINTDESC(sb, *indent, softc->media_density, %u, media_density,
5754 "Current Medium Density");
5755 is_density = 0;
5756 for (i = 0; i < SA_DENSITY_TYPES; i++) {
5757 int tmpint;
5758
5759 if (softc->density_info_valid[i] == 0)
5760 continue;
5761
5762 SASBADDNODE(sb, *indent, density_report);
5763 if (softc->density_type_bits[i] & SRDS_MEDIUM_TYPE) {
5764 tmpint = 1;
5765 is_density = 0;
5766 } else {
5767 tmpint = 0;
5768 is_density = 1;
5769 }
5770 SASBADDINTDESC(sb, *indent, tmpint, %d, medium_type_report,
5771 "Medium type report");
5772
5773 if (softc->density_type_bits[i] & SRDS_MEDIA)
5774 tmpint = 1;
5775 else
5776 tmpint = 0;
5777 SASBADDINTDESC(sb, *indent, tmpint, %d, media_report,
5778 "Media report");
5779
5780 safilldenstypesb(sb, indent, softc->density_info[i],
5781 softc->density_info_valid[i], is_density);
5782 SASBENDNODE(sb, *indent, density_report);
5783 }
5784 SASBENDNODE(sb, *indent, mtdensity);
5785 }
5786
5787 /*
5788 * Given a completed REPORT SUPPORTED OPERATION CODES command with timeout
5789 * descriptors, go through the descriptors and set the sa(4) driver
5790 * timeouts to the recommended values.
5791 */
5792 static void
saloadtimeouts(struct sa_softc * softc,union ccb * ccb)5793 saloadtimeouts(struct sa_softc *softc, union ccb *ccb)
5794 {
5795 uint32_t valid_len, avail_len = 0, used_len = 0;
5796 struct scsi_report_supported_opcodes_all *hdr;
5797 struct scsi_report_supported_opcodes_descr *desc;
5798 uint8_t *buf;
5799
5800 hdr = (struct scsi_report_supported_opcodes_all *)ccb->csio.data_ptr;
5801 valid_len = ccb->csio.dxfer_len - ccb->csio.resid;
5802
5803 if (valid_len < sizeof(*hdr))
5804 return;
5805
5806 avail_len = scsi_4btoul(hdr->length) + sizeof(hdr->length);
5807 if ((avail_len != 0)
5808 && (avail_len > valid_len)) {
5809 xpt_print(softc->periph->path, "WARNING: available timeout "
5810 "descriptor len %zu > valid len %u\n", avail_len,valid_len);
5811 }
5812
5813 used_len = sizeof(hdr->length);
5814 avail_len = MIN(avail_len, valid_len - sizeof(*hdr));
5815 buf = ccb->csio.data_ptr;
5816 while ((avail_len - used_len) > sizeof(*desc)) {
5817 struct scsi_report_supported_opcodes_timeout *td;
5818 uint32_t td_len;
5819 uint32_t rec_time;
5820 uint8_t *cur_ptr;
5821
5822 cur_ptr = &buf[used_len];
5823 desc = (struct scsi_report_supported_opcodes_descr *)cur_ptr;
5824
5825 used_len += sizeof(*desc);
5826 /* If there's no timeout descriptor, keep going */
5827 if ((desc->flags & RSO_CTDP) == 0)
5828 continue;
5829
5830 /*
5831 * If we don't have enough space to fit a timeout
5832 * descriptor then we're done.
5833 */
5834 if ((avail_len - used_len) < sizeof(*td)) {
5835 used_len = avail_len;
5836 continue;
5837 }
5838
5839 cur_ptr = &buf[used_len];
5840 td = (struct scsi_report_supported_opcodes_timeout *)cur_ptr;
5841 td_len = scsi_2btoul(td->length);
5842 td_len += sizeof(td->length);
5843 used_len += td_len;
5844
5845 if (td_len < sizeof(*td))
5846 continue;
5847
5848 /*
5849 * Use the recommended timeout. The nominal time is the
5850 * time to wait before querying for status.
5851 */
5852 rec_time = scsi_4btoul(td->recommended_time);
5853
5854 /*
5855 * Our timeouts are set in thousandths of a seconds.
5856 */
5857 rec_time *= 1000;
5858
5859 switch(desc->opcode) {
5860 case ERASE:
5861 softc->timeout_info[SA_TIMEOUT_ERASE] = rec_time;
5862 break;
5863 case LOAD_UNLOAD:
5864 softc->timeout_info[SA_TIMEOUT_LOAD] = rec_time;
5865 break;
5866 case LOCATE:
5867 case LOCATE_16:
5868 /*
5869 * We are assuming these are the same timeout.
5870 */
5871 softc->timeout_info[SA_TIMEOUT_LOCATE] = rec_time;
5872 break;
5873 case MODE_SELECT_6:
5874 case MODE_SELECT_10:
5875 /*
5876 * We are assuming these are the same timeout.
5877 */
5878 softc->timeout_info[SA_TIMEOUT_MODE_SELECT] = rec_time;
5879 break;
5880 case MODE_SENSE_6:
5881 case MODE_SENSE_10:
5882 /*
5883 * We are assuming these are the same timeout.
5884 */
5885 softc->timeout_info[SA_TIMEOUT_MODE_SENSE] = rec_time;
5886 break;
5887 case PREVENT_ALLOW:
5888 softc->timeout_info[SA_TIMEOUT_PREVENT] = rec_time;
5889 break;
5890 case SA_READ:
5891 softc->timeout_info[SA_TIMEOUT_READ] = rec_time;
5892 break;
5893 case READ_BLOCK_LIMITS:
5894 softc->timeout_info[SA_TIMEOUT_READ_BLOCK_LIMITS] =
5895 rec_time;
5896 break;
5897 case READ_POSITION:
5898 /*
5899 * Note that this may show up multiple times for
5900 * the short form, long form and extended form
5901 * service actions. We're assuming they are all
5902 * the same.
5903 */
5904 softc->timeout_info[SA_TIMEOUT_READ_POSITION] =rec_time;
5905 break;
5906 case REPORT_DENSITY_SUPPORT:
5907 softc->timeout_info[SA_TIMEOUT_REP_DENSITY] = rec_time;
5908 break;
5909 case RESERVE_UNIT:
5910 case RELEASE_UNIT:
5911 /* We are assuming these are the same timeout.*/
5912 softc->timeout_info[SA_TIMEOUT_RESERVE] = rec_time;
5913 break;
5914 case REWIND:
5915 softc->timeout_info[SA_TIMEOUT_REWIND] = rec_time;
5916 break;
5917 case SPACE:
5918 softc->timeout_info[SA_TIMEOUT_SPACE] = rec_time;
5919 break;
5920 case TEST_UNIT_READY:
5921 softc->timeout_info[SA_TIMEOUT_TUR] = rec_time;
5922 break;
5923 case SA_WRITE:
5924 softc->timeout_info[SA_TIMEOUT_WRITE] = rec_time;
5925 break;
5926 case WRITE_FILEMARKS:
5927 softc->timeout_info[SA_TIMEOUT_WRITE_FILEMARKS] =
5928 rec_time;
5929 break;
5930 default:
5931 /*
5932 * We have explicit cases for all of the timeouts
5933 * we use.
5934 */
5935 break;
5936 }
5937 }
5938 }
5939
5940 #endif /* _KERNEL */
5941
5942 /*
5943 * Read tape block limits command.
5944 */
5945 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)5946 scsi_read_block_limits(struct ccb_scsiio *csio, u_int32_t retries,
5947 void (*cbfcnp)(struct cam_periph *, union ccb *),
5948 u_int8_t tag_action,
5949 struct scsi_read_block_limits_data *rlimit_buf,
5950 u_int8_t sense_len, u_int32_t timeout)
5951 {
5952 struct scsi_read_block_limits *scsi_cmd;
5953
5954 cam_fill_csio(csio, retries, cbfcnp, CAM_DIR_IN, tag_action,
5955 (u_int8_t *)rlimit_buf, sizeof(*rlimit_buf), sense_len,
5956 sizeof(*scsi_cmd), timeout);
5957
5958 scsi_cmd = (struct scsi_read_block_limits *)&csio->cdb_io.cdb_bytes;
5959 bzero(scsi_cmd, sizeof(*scsi_cmd));
5960 scsi_cmd->opcode = READ_BLOCK_LIMITS;
5961 }
5962
5963 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)5964 scsi_sa_read_write(struct ccb_scsiio *csio, u_int32_t retries,
5965 void (*cbfcnp)(struct cam_periph *, union ccb *),
5966 u_int8_t tag_action, int readop, int sli,
5967 int fixed, u_int32_t length, u_int8_t *data_ptr,
5968 u_int32_t dxfer_len, u_int8_t sense_len, u_int32_t timeout)
5969 {
5970 struct scsi_sa_rw *scsi_cmd;
5971 int read;
5972
5973 read = (readop & SCSI_RW_DIRMASK) == SCSI_RW_READ;
5974
5975 scsi_cmd = (struct scsi_sa_rw *)&csio->cdb_io.cdb_bytes;
5976 scsi_cmd->opcode = read ? SA_READ : SA_WRITE;
5977 scsi_cmd->sli_fixed = 0;
5978 if (sli && read)
5979 scsi_cmd->sli_fixed |= SAR_SLI;
5980 if (fixed)
5981 scsi_cmd->sli_fixed |= SARW_FIXED;
5982 scsi_ulto3b(length, scsi_cmd->length);
5983 scsi_cmd->control = 0;
5984
5985 cam_fill_csio(csio, retries, cbfcnp, (read ? CAM_DIR_IN : CAM_DIR_OUT) |
5986 ((readop & SCSI_RW_BIO) != 0 ? CAM_DATA_BIO : 0),
5987 tag_action, data_ptr, dxfer_len, sense_len,
5988 sizeof(*scsi_cmd), timeout);
5989 }
5990
5991 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)5992 scsi_load_unload(struct ccb_scsiio *csio, u_int32_t retries,
5993 void (*cbfcnp)(struct cam_periph *, union ccb *),
5994 u_int8_t tag_action, int immediate, int eot,
5995 int reten, int load, u_int8_t sense_len,
5996 u_int32_t timeout)
5997 {
5998 struct scsi_load_unload *scsi_cmd;
5999
6000 scsi_cmd = (struct scsi_load_unload *)&csio->cdb_io.cdb_bytes;
6001 bzero(scsi_cmd, sizeof(*scsi_cmd));
6002 scsi_cmd->opcode = LOAD_UNLOAD;
6003 if (immediate)
6004 scsi_cmd->immediate = SLU_IMMED;
6005 if (eot)
6006 scsi_cmd->eot_reten_load |= SLU_EOT;
6007 if (reten)
6008 scsi_cmd->eot_reten_load |= SLU_RETEN;
6009 if (load)
6010 scsi_cmd->eot_reten_load |= SLU_LOAD;
6011
6012 cam_fill_csio(csio, retries, cbfcnp, CAM_DIR_NONE, tag_action,
6013 NULL, 0, sense_len, sizeof(*scsi_cmd), timeout);
6014 }
6015
6016 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)6017 scsi_rewind(struct ccb_scsiio *csio, u_int32_t retries,
6018 void (*cbfcnp)(struct cam_periph *, union ccb *),
6019 u_int8_t tag_action, int immediate, u_int8_t sense_len,
6020 u_int32_t timeout)
6021 {
6022 struct scsi_rewind *scsi_cmd;
6023
6024 scsi_cmd = (struct scsi_rewind *)&csio->cdb_io.cdb_bytes;
6025 bzero(scsi_cmd, sizeof(*scsi_cmd));
6026 scsi_cmd->opcode = REWIND;
6027 if (immediate)
6028 scsi_cmd->immediate = SREW_IMMED;
6029
6030 cam_fill_csio(csio, retries, cbfcnp, CAM_DIR_NONE, tag_action, NULL,
6031 0, sense_len, sizeof(*scsi_cmd), timeout);
6032 }
6033
6034 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)6035 scsi_space(struct ccb_scsiio *csio, u_int32_t retries,
6036 void (*cbfcnp)(struct cam_periph *, union ccb *),
6037 u_int8_t tag_action, scsi_space_code code,
6038 u_int32_t count, u_int8_t sense_len, u_int32_t timeout)
6039 {
6040 struct scsi_space *scsi_cmd;
6041
6042 scsi_cmd = (struct scsi_space *)&csio->cdb_io.cdb_bytes;
6043 scsi_cmd->opcode = SPACE;
6044 scsi_cmd->code = code;
6045 scsi_ulto3b(count, scsi_cmd->count);
6046 scsi_cmd->control = 0;
6047
6048 cam_fill_csio(csio, retries, cbfcnp, CAM_DIR_NONE, tag_action, NULL,
6049 0, sense_len, sizeof(*scsi_cmd), timeout);
6050 }
6051
6052 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)6053 scsi_write_filemarks(struct ccb_scsiio *csio, u_int32_t retries,
6054 void (*cbfcnp)(struct cam_periph *, union ccb *),
6055 u_int8_t tag_action, int immediate, int setmark,
6056 u_int32_t num_marks, u_int8_t sense_len,
6057 u_int32_t timeout)
6058 {
6059 struct scsi_write_filemarks *scsi_cmd;
6060
6061 scsi_cmd = (struct scsi_write_filemarks *)&csio->cdb_io.cdb_bytes;
6062 bzero(scsi_cmd, sizeof(*scsi_cmd));
6063 scsi_cmd->opcode = WRITE_FILEMARKS;
6064 if (immediate)
6065 scsi_cmd->byte2 |= SWFMRK_IMMED;
6066 if (setmark)
6067 scsi_cmd->byte2 |= SWFMRK_WSMK;
6068
6069 scsi_ulto3b(num_marks, scsi_cmd->num_marks);
6070
6071 cam_fill_csio(csio, retries, cbfcnp, CAM_DIR_NONE, tag_action, NULL,
6072 0, sense_len, sizeof(*scsi_cmd), timeout);
6073 }
6074
6075 /*
6076 * The reserve and release unit commands differ only by their opcodes.
6077 */
6078 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)6079 scsi_reserve_release_unit(struct ccb_scsiio *csio, u_int32_t retries,
6080 void (*cbfcnp)(struct cam_periph *, union ccb *),
6081 u_int8_t tag_action, int third_party,
6082 int third_party_id, u_int8_t sense_len,
6083 u_int32_t timeout, int reserve)
6084 {
6085 struct scsi_reserve_release_unit *scsi_cmd;
6086
6087 scsi_cmd = (struct scsi_reserve_release_unit *)&csio->cdb_io.cdb_bytes;
6088 bzero(scsi_cmd, sizeof(*scsi_cmd));
6089
6090 if (reserve)
6091 scsi_cmd->opcode = RESERVE_UNIT;
6092 else
6093 scsi_cmd->opcode = RELEASE_UNIT;
6094
6095 if (third_party) {
6096 scsi_cmd->lun_thirdparty |= SRRU_3RD_PARTY;
6097 scsi_cmd->lun_thirdparty |=
6098 ((third_party_id << SRRU_3RD_SHAMT) & SRRU_3RD_MASK);
6099 }
6100
6101 cam_fill_csio(csio, retries, cbfcnp, CAM_DIR_NONE, tag_action, NULL,
6102 0, sense_len, sizeof(*scsi_cmd), timeout);
6103 }
6104
6105 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)6106 scsi_erase(struct ccb_scsiio *csio, u_int32_t retries,
6107 void (*cbfcnp)(struct cam_periph *, union ccb *),
6108 u_int8_t tag_action, int immediate, int long_erase,
6109 u_int8_t sense_len, u_int32_t timeout)
6110 {
6111 struct scsi_erase *scsi_cmd;
6112
6113 scsi_cmd = (struct scsi_erase *)&csio->cdb_io.cdb_bytes;
6114 bzero(scsi_cmd, sizeof(*scsi_cmd));
6115
6116 scsi_cmd->opcode = ERASE;
6117
6118 if (immediate)
6119 scsi_cmd->lun_imm_long |= SE_IMMED;
6120
6121 if (long_erase)
6122 scsi_cmd->lun_imm_long |= SE_LONG;
6123
6124 cam_fill_csio(csio, retries, cbfcnp, CAM_DIR_NONE, tag_action, NULL,
6125 0, sense_len, sizeof(*scsi_cmd), timeout);
6126 }
6127
6128 /*
6129 * Read Tape Position command.
6130 */
6131 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)6132 scsi_read_position(struct ccb_scsiio *csio, u_int32_t retries,
6133 void (*cbfcnp)(struct cam_periph *, union ccb *),
6134 u_int8_t tag_action, int hardsoft,
6135 struct scsi_tape_position_data *sbp,
6136 u_int8_t sense_len, u_int32_t timeout)
6137 {
6138 struct scsi_tape_read_position *scmd;
6139
6140 cam_fill_csio(csio, retries, cbfcnp, CAM_DIR_IN, tag_action,
6141 (u_int8_t *)sbp, sizeof (*sbp), sense_len, sizeof(*scmd), timeout);
6142 scmd = (struct scsi_tape_read_position *)&csio->cdb_io.cdb_bytes;
6143 bzero(scmd, sizeof(*scmd));
6144 scmd->opcode = READ_POSITION;
6145 scmd->byte1 = hardsoft;
6146 }
6147
6148 /*
6149 * Read Tape Position command.
6150 */
6151 void
scsi_read_position_10(struct ccb_scsiio * csio,u_int32_t retries,void (* cbfcnp)(struct cam_periph *,union ccb *),u_int8_t tag_action,int service_action,u_int8_t * data_ptr,u_int32_t length,u_int32_t sense_len,u_int32_t timeout)6152 scsi_read_position_10(struct ccb_scsiio *csio, u_int32_t retries,
6153 void (*cbfcnp)(struct cam_periph *, union ccb *),
6154 u_int8_t tag_action, int service_action,
6155 u_int8_t *data_ptr, u_int32_t length,
6156 u_int32_t sense_len, u_int32_t timeout)
6157 {
6158 struct scsi_tape_read_position *scmd;
6159
6160 cam_fill_csio(csio,
6161 retries,
6162 cbfcnp,
6163 /*flags*/CAM_DIR_IN,
6164 tag_action,
6165 /*data_ptr*/data_ptr,
6166 /*dxfer_len*/length,
6167 sense_len,
6168 sizeof(*scmd),
6169 timeout);
6170
6171
6172 scmd = (struct scsi_tape_read_position *)&csio->cdb_io.cdb_bytes;
6173 bzero(scmd, sizeof(*scmd));
6174 scmd->opcode = READ_POSITION;
6175 scmd->byte1 = service_action;
6176 /*
6177 * The length is only currently set (as of SSC4r03) if the extended
6178 * form is specified. The other forms have fixed lengths.
6179 */
6180 if (service_action == SA_RPOS_EXTENDED_FORM)
6181 scsi_ulto2b(length, scmd->length);
6182 }
6183
6184 /*
6185 * Set Tape Position command.
6186 */
6187 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)6188 scsi_set_position(struct ccb_scsiio *csio, u_int32_t retries,
6189 void (*cbfcnp)(struct cam_periph *, union ccb *),
6190 u_int8_t tag_action, int hardsoft, u_int32_t blkno,
6191 u_int8_t sense_len, u_int32_t timeout)
6192 {
6193 struct scsi_tape_locate *scmd;
6194
6195 cam_fill_csio(csio, retries, cbfcnp, CAM_DIR_NONE, tag_action,
6196 (u_int8_t *)NULL, 0, sense_len, sizeof(*scmd), timeout);
6197 scmd = (struct scsi_tape_locate *)&csio->cdb_io.cdb_bytes;
6198 bzero(scmd, sizeof(*scmd));
6199 scmd->opcode = LOCATE;
6200 if (hardsoft)
6201 scmd->byte1 |= SA_SPOS_BT;
6202 scsi_ulto4b(blkno, scmd->blkaddr);
6203 }
6204
6205 /*
6206 * XXX KDM figure out how to make a compatibility function.
6207 */
6208 void
scsi_locate_10(struct ccb_scsiio * csio,u_int32_t retries,void (* cbfcnp)(struct cam_periph *,union ccb *),u_int8_t tag_action,int immed,int cp,int hard,int64_t partition,u_int32_t block_address,int sense_len,u_int32_t timeout)6209 scsi_locate_10(struct ccb_scsiio *csio, u_int32_t retries,
6210 void (*cbfcnp)(struct cam_periph *, union ccb *),
6211 u_int8_t tag_action, int immed, int cp, int hard,
6212 int64_t partition, u_int32_t block_address,
6213 int sense_len, u_int32_t timeout)
6214 {
6215 struct scsi_tape_locate *scmd;
6216
6217 cam_fill_csio(csio,
6218 retries,
6219 cbfcnp,
6220 CAM_DIR_NONE,
6221 tag_action,
6222 /*data_ptr*/ NULL,
6223 /*dxfer_len*/ 0,
6224 sense_len,
6225 sizeof(*scmd),
6226 timeout);
6227 scmd = (struct scsi_tape_locate *)&csio->cdb_io.cdb_bytes;
6228 bzero(scmd, sizeof(*scmd));
6229 scmd->opcode = LOCATE;
6230 if (immed)
6231 scmd->byte1 |= SA_SPOS_IMMED;
6232 if (cp)
6233 scmd->byte1 |= SA_SPOS_CP;
6234 if (hard)
6235 scmd->byte1 |= SA_SPOS_BT;
6236 scsi_ulto4b(block_address, scmd->blkaddr);
6237 scmd->partition = partition;
6238 }
6239
6240 void
scsi_locate_16(struct ccb_scsiio * csio,u_int32_t retries,void (* cbfcnp)(struct cam_periph *,union ccb *),u_int8_t tag_action,int immed,int cp,u_int8_t dest_type,int bam,int64_t partition,u_int64_t logical_id,int sense_len,u_int32_t timeout)6241 scsi_locate_16(struct ccb_scsiio *csio, u_int32_t retries,
6242 void (*cbfcnp)(struct cam_periph *, union ccb *),
6243 u_int8_t tag_action, int immed, int cp, u_int8_t dest_type,
6244 int bam, int64_t partition, u_int64_t logical_id,
6245 int sense_len, u_int32_t timeout)
6246 {
6247
6248 struct scsi_locate_16 *scsi_cmd;
6249
6250 cam_fill_csio(csio,
6251 retries,
6252 cbfcnp,
6253 /*flags*/CAM_DIR_NONE,
6254 tag_action,
6255 /*data_ptr*/NULL,
6256 /*dxfer_len*/0,
6257 sense_len,
6258 sizeof(*scsi_cmd),
6259 timeout);
6260
6261 scsi_cmd = (struct scsi_locate_16 *)&csio->cdb_io.cdb_bytes;
6262 bzero(scsi_cmd, sizeof(*scsi_cmd));
6263 scsi_cmd->opcode = LOCATE_16;
6264 if (immed)
6265 scsi_cmd->byte1 |= SA_LC_IMMEDIATE;
6266 if (cp)
6267 scsi_cmd->byte1 |= SA_LC_CP;
6268 scsi_cmd->byte1 |= (dest_type << SA_LC_DEST_TYPE_SHIFT);
6269
6270 scsi_cmd->byte2 |= bam;
6271 scsi_cmd->partition = partition;
6272 scsi_u64to8b(logical_id, scsi_cmd->logical_id);
6273 }
6274
6275 void
scsi_report_density_support(struct ccb_scsiio * csio,u_int32_t retries,void (* cbfcnp)(struct cam_periph *,union ccb *),u_int8_t tag_action,int media,int medium_type,u_int8_t * data_ptr,u_int32_t length,u_int32_t sense_len,u_int32_t timeout)6276 scsi_report_density_support(struct ccb_scsiio *csio, u_int32_t retries,
6277 void (*cbfcnp)(struct cam_periph *, union ccb *),
6278 u_int8_t tag_action, int media, int medium_type,
6279 u_int8_t *data_ptr, u_int32_t length,
6280 u_int32_t sense_len, u_int32_t timeout)
6281 {
6282 struct scsi_report_density_support *scsi_cmd;
6283
6284 scsi_cmd =(struct scsi_report_density_support *)&csio->cdb_io.cdb_bytes;
6285 bzero(scsi_cmd, sizeof(*scsi_cmd));
6286
6287 scsi_cmd->opcode = REPORT_DENSITY_SUPPORT;
6288 if (media != 0)
6289 scsi_cmd->byte1 |= SRDS_MEDIA;
6290 if (medium_type != 0)
6291 scsi_cmd->byte1 |= SRDS_MEDIUM_TYPE;
6292
6293 scsi_ulto2b(length, scsi_cmd->length);
6294
6295 cam_fill_csio(csio,
6296 retries,
6297 cbfcnp,
6298 /*flags*/CAM_DIR_IN,
6299 tag_action,
6300 /*data_ptr*/data_ptr,
6301 /*dxfer_len*/length,
6302 sense_len,
6303 sizeof(*scsi_cmd),
6304 timeout);
6305 }
6306
6307 void
scsi_set_capacity(struct ccb_scsiio * csio,u_int32_t retries,void (* cbfcnp)(struct cam_periph *,union ccb *),u_int8_t tag_action,int byte1,u_int32_t proportion,u_int32_t sense_len,u_int32_t timeout)6308 scsi_set_capacity(struct ccb_scsiio *csio, u_int32_t retries,
6309 void (*cbfcnp)(struct cam_periph *, union ccb *),
6310 u_int8_t tag_action, int byte1, u_int32_t proportion,
6311 u_int32_t sense_len, u_int32_t timeout)
6312 {
6313 struct scsi_set_capacity *scsi_cmd;
6314
6315 scsi_cmd = (struct scsi_set_capacity *)&csio->cdb_io.cdb_bytes;
6316 bzero(scsi_cmd, sizeof(*scsi_cmd));
6317
6318 scsi_cmd->opcode = SET_CAPACITY;
6319
6320 scsi_cmd->byte1 = byte1;
6321 scsi_ulto2b(proportion, scsi_cmd->cap_proportion);
6322
6323 cam_fill_csio(csio,
6324 retries,
6325 cbfcnp,
6326 /*flags*/CAM_DIR_NONE,
6327 tag_action,
6328 /*data_ptr*/NULL,
6329 /*dxfer_len*/0,
6330 sense_len,
6331 sizeof(*scsi_cmd),
6332 timeout);
6333 }
6334
6335 void
scsi_format_medium(struct ccb_scsiio * csio,u_int32_t retries,void (* cbfcnp)(struct cam_periph *,union ccb *),u_int8_t tag_action,int byte1,int byte2,u_int8_t * data_ptr,u_int32_t dxfer_len,u_int32_t sense_len,u_int32_t timeout)6336 scsi_format_medium(struct ccb_scsiio *csio, u_int32_t retries,
6337 void (*cbfcnp)(struct cam_periph *, union ccb *),
6338 u_int8_t tag_action, int byte1, int byte2,
6339 u_int8_t *data_ptr, u_int32_t dxfer_len,
6340 u_int32_t sense_len, u_int32_t timeout)
6341 {
6342 struct scsi_format_medium *scsi_cmd;
6343
6344 scsi_cmd = (struct scsi_format_medium*)&csio->cdb_io.cdb_bytes;
6345 bzero(scsi_cmd, sizeof(*scsi_cmd));
6346
6347 scsi_cmd->opcode = FORMAT_MEDIUM;
6348
6349 scsi_cmd->byte1 = byte1;
6350 scsi_cmd->byte2 = byte2;
6351
6352 scsi_ulto2b(dxfer_len, scsi_cmd->length);
6353
6354 cam_fill_csio(csio,
6355 retries,
6356 cbfcnp,
6357 /*flags*/(dxfer_len > 0) ? CAM_DIR_OUT : CAM_DIR_NONE,
6358 tag_action,
6359 /*data_ptr*/ data_ptr,
6360 /*dxfer_len*/ dxfer_len,
6361 sense_len,
6362 sizeof(*scsi_cmd),
6363 timeout);
6364 }
6365
6366 void
scsi_allow_overwrite(struct ccb_scsiio * csio,u_int32_t retries,void (* cbfcnp)(struct cam_periph *,union ccb *),u_int8_t tag_action,int allow_overwrite,int partition,u_int64_t logical_id,u_int32_t sense_len,u_int32_t timeout)6367 scsi_allow_overwrite(struct ccb_scsiio *csio, u_int32_t retries,
6368 void (*cbfcnp)(struct cam_periph *, union ccb *),
6369 u_int8_t tag_action, int allow_overwrite, int partition,
6370 u_int64_t logical_id, u_int32_t sense_len, u_int32_t timeout)
6371 {
6372 struct scsi_allow_overwrite *scsi_cmd;
6373
6374 scsi_cmd = (struct scsi_allow_overwrite *)&csio->cdb_io.cdb_bytes;
6375 bzero(scsi_cmd, sizeof(*scsi_cmd));
6376
6377 scsi_cmd->opcode = ALLOW_OVERWRITE;
6378
6379 scsi_cmd->allow_overwrite = allow_overwrite;
6380 scsi_cmd->partition = partition;
6381 scsi_u64to8b(logical_id, scsi_cmd->logical_id);
6382
6383 cam_fill_csio(csio,
6384 retries,
6385 cbfcnp,
6386 CAM_DIR_NONE,
6387 tag_action,
6388 /*data_ptr*/ NULL,
6389 /*dxfer_len*/ 0,
6390 sense_len,
6391 sizeof(*scsi_cmd),
6392 timeout);
6393 }
6394