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