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