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