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