1 /* 2 * $FreeBSD: src/sys/cam/scsi/scsi_sa.c,v 1.45.2.13 2002/12/17 17:08:50 trhodes Exp $ 3 * 4 * Implementation of SCSI Sequential Access Peripheral driver for CAM. 5 * 6 * Copyright (c) 1999, 2000 Matthew Jacob 7 * All rights reserved. 8 * 9 * Redistribution and use in source and binary forms, with or without 10 * modification, are permitted provided that the following conditions 11 * are met: 12 * 1. Redistributions of source code must retain the above copyright 13 * notice, this list of conditions, and the following disclaimer, 14 * without modification, immediately at the beginning of the file. 15 * 2. The name of the author may not be used to endorse or promote products 16 * derived from this software without specific prior written permission. 17 * 18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 21 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR 22 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 28 * SUCH DAMAGE. 29 * 30 */ 31 32 #include <sys/param.h> 33 #include <sys/queue.h> 34 #ifdef _KERNEL 35 #include <sys/systm.h> 36 #include <sys/kernel.h> 37 #endif 38 #include <sys/types.h> 39 #ifdef _KERNEL 40 #include <sys/buf.h> 41 #include <sys/malloc.h> 42 #endif 43 #include <sys/mtio.h> 44 #include <sys/conf.h> 45 #ifdef _KERNEL 46 #include <sys/proc.h> 47 #include <sys/buf2.h> 48 #include <sys/thread2.h> 49 #endif 50 #include <sys/fcntl.h> 51 #include <sys/devicestat.h> 52 #include <machine/limits.h> 53 54 #ifndef _KERNEL 55 #include <stdio.h> 56 #include <string.h> 57 #endif 58 59 #include "../cam.h" 60 #include "../cam_ccb.h" 61 #include "../cam_extend.h" 62 #include "../cam_periph.h" 63 #include "../cam_xpt_periph.h" 64 #include "../cam_debug.h" 65 66 #include "scsi_all.h" 67 #include "scsi_message.h" 68 #include "scsi_sa.h" 69 70 #ifdef _KERNEL 71 72 #include <opt_sa.h> 73 74 #ifndef SA_IO_TIMEOUT 75 #define SA_IO_TIMEOUT 4 76 #endif 77 #ifndef SA_SPACE_TIMEOUT 78 #define SA_SPACE_TIMEOUT 1 * 60 79 #endif 80 #ifndef SA_REWIND_TIMEOUT 81 #define SA_REWIND_TIMEOUT 2 * 60 82 #endif 83 #ifndef SA_ERASE_TIMEOUT 84 #define SA_ERASE_TIMEOUT 4 * 60 85 #endif 86 87 #define SCSIOP_TIMEOUT (60 * 1000) /* not an option */ 88 89 #define IO_TIMEOUT (SA_IO_TIMEOUT * 60 * 1000) 90 #define REWIND_TIMEOUT (SA_REWIND_TIMEOUT * 60 * 1000) 91 #define ERASE_TIMEOUT (SA_ERASE_TIMEOUT * 60 * 1000) 92 #define SPACE_TIMEOUT (SA_SPACE_TIMEOUT * 60 * 1000) 93 94 /* 95 * Additional options that can be set for config: SA_1FM_AT_EOT 96 */ 97 98 #define QFRLS(ccb) \ 99 if (((ccb)->ccb_h.status & CAM_DEV_QFRZN) != 0) \ 100 cam_release_devq((ccb)->ccb_h.path, 0, 0, 0, FALSE) 101 102 /* 103 * Driver states 104 */ 105 106 MALLOC_DEFINE(M_SCSISA, "SCSI sa", "SCSI sequential access buffers"); 107 108 typedef enum { 109 SA_STATE_NORMAL, SA_STATE_ABNORMAL 110 } sa_state; 111 112 #define ccb_pflags ppriv_field0 113 #define ccb_bio ppriv_ptr1 114 115 #define SA_CCB_BUFFER_IO 0x0 116 #define SA_CCB_WAITING 0x1 117 #define SA_CCB_TYPEMASK 0x1 118 #define SA_POSITION_UPDATED 0x2 119 120 #define Set_CCB_Type(x, type) \ 121 x->ccb_h.ccb_pflags &= ~SA_CCB_TYPEMASK; \ 122 x->ccb_h.ccb_pflags |= type 123 124 #define CCB_Type(x) (x->ccb_h.ccb_pflags & SA_CCB_TYPEMASK) 125 126 127 128 typedef enum { 129 SA_FLAG_OPEN = 0x0001, 130 SA_FLAG_FIXED = 0x0002, 131 SA_FLAG_TAPE_LOCKED = 0x0004, 132 SA_FLAG_TAPE_MOUNTED = 0x0008, 133 SA_FLAG_TAPE_WP = 0x0010, 134 SA_FLAG_TAPE_WRITTEN = 0x0020, 135 SA_FLAG_EOM_PENDING = 0x0040, 136 SA_FLAG_EIO_PENDING = 0x0080, 137 SA_FLAG_EOF_PENDING = 0x0100, 138 SA_FLAG_ERR_PENDING = (SA_FLAG_EOM_PENDING|SA_FLAG_EIO_PENDING| 139 SA_FLAG_EOF_PENDING), 140 SA_FLAG_INVALID = 0x0200, 141 SA_FLAG_COMP_ENABLED = 0x0400, 142 SA_FLAG_COMP_SUPP = 0x0800, 143 SA_FLAG_COMP_UNSUPP = 0x1000, 144 SA_FLAG_TAPE_FROZEN = 0x2000 145 } sa_flags; 146 147 typedef enum { 148 SA_MODE_REWIND = 0x00, 149 SA_MODE_NOREWIND = 0x01, 150 SA_MODE_OFFLINE = 0x02 151 } sa_mode; 152 153 typedef enum { 154 SA_PARAM_NONE = 0x00, 155 SA_PARAM_BLOCKSIZE = 0x01, 156 SA_PARAM_DENSITY = 0x02, 157 SA_PARAM_COMPRESSION = 0x04, 158 SA_PARAM_BUFF_MODE = 0x08, 159 SA_PARAM_NUMBLOCKS = 0x10, 160 SA_PARAM_WP = 0x20, 161 SA_PARAM_SPEED = 0x40, 162 SA_PARAM_ALL = 0x7f 163 } sa_params; 164 165 typedef enum { 166 SA_QUIRK_NONE = 0x00, 167 SA_QUIRK_NOCOMP = 0x01, /* Can't deal with compression at all */ 168 SA_QUIRK_FIXED = 0x02, /* Force fixed mode */ 169 SA_QUIRK_VARIABLE = 0x04, /* Force variable mode */ 170 SA_QUIRK_2FM = 0x08, /* Needs Two File Marks at EOD */ 171 SA_QUIRK_1FM = 0x10, /* No more than 1 File Mark at EOD */ 172 SA_QUIRK_NODREAD = 0x20, /* Don't try and dummy read density */ 173 SA_QUIRK_NO_MODESEL = 0x40, /* Don't do mode select at all */ 174 SA_QUIRK_NO_CPAGE = 0x80 /* Don't use DEVICE COMPRESSION page */ 175 } sa_quirks; 176 177 /* units are bits 4-7, 16-21 (1024 units) */ 178 #define SAUNIT(DEV) \ 179 (((minor(DEV) & 0xF0) >> 4) | ((minor(DEV) & 0x3f0000) >> 16)) 180 181 #define SAMODE(z) ((minor(z) & 0x3)) 182 #define SADENSITY(z) (((minor(z) >> 2) & 0x3)) 183 #define SA_IS_CTRL(z) (minor(z) & (1 << 29)) 184 185 #define SA_NOT_CTLDEV 0 186 #define SA_CTLDEV 1 187 188 #define SA_ATYPE_R 0 189 #define SA_ATYPE_NR 1 190 #define SA_ATYPE_ER 2 191 192 #define SAMINOR(ctl, unit, mode, access) \ 193 ((ctl << 29) | ((unit & 0x3f0) << 16) | ((unit & 0xf) << 4) | \ 194 (mode << 0x2) | (access & 0x3)) 195 196 #define SA_UNITMASK SAMINOR(0, -1, 0, 0) 197 #define SA_UNIT(unit) SAMINOR(0, unit, 0, 0) 198 199 #define SA_NUM_MODES 4 200 201 struct sa_softc { 202 sa_state state; 203 sa_flags flags; 204 sa_quirks quirks; 205 struct bio_queue_head bio_queue; 206 int queue_count; 207 struct devstat device_stats; 208 int blk_gran; 209 int blk_mask; 210 int blk_shift; 211 u_int32_t max_blk; 212 u_int32_t min_blk; 213 u_int32_t comp_algorithm; 214 u_int32_t saved_comp_algorithm; 215 u_int32_t media_blksize; 216 u_int32_t last_media_blksize; 217 u_int32_t media_numblks; 218 u_int8_t media_density; 219 u_int8_t speed; 220 u_int8_t scsi_rev; 221 u_int8_t dsreg; /* mtio mt_dsreg, redux */ 222 int buffer_mode; 223 int filemarks; 224 union ccb saved_ccb; 225 int last_resid_was_io; 226 227 /* 228 * Relative to BOT Location. 229 */ 230 daddr_t fileno; 231 daddr_t blkno; 232 233 /* 234 * Latched Error Info 235 */ 236 struct { 237 struct scsi_sense_data _last_io_sense; 238 u_int32_t _last_io_resid; 239 u_int8_t _last_io_cdb[CAM_MAX_CDBLEN]; 240 struct scsi_sense_data _last_ctl_sense; 241 u_int32_t _last_ctl_resid; 242 u_int8_t _last_ctl_cdb[CAM_MAX_CDBLEN]; 243 #define last_io_sense errinfo._last_io_sense 244 #define last_io_resid errinfo._last_io_resid 245 #define last_io_cdb errinfo._last_io_cdb 246 #define last_ctl_sense errinfo._last_ctl_sense 247 #define last_ctl_resid errinfo._last_ctl_resid 248 #define last_ctl_cdb errinfo._last_ctl_cdb 249 } errinfo; 250 /* 251 * Misc other flags/state 252 */ 253 u_int32_t 254 : 29, 255 open_rdonly : 1, /* open read-only */ 256 open_pending_mount : 1, /* open pending mount */ 257 ctrl_mode : 1; /* control device open */ 258 }; 259 260 struct sa_quirk_entry { 261 struct scsi_inquiry_pattern inq_pat; /* matching pattern */ 262 sa_quirks quirks; /* specific quirk type */ 263 u_int32_t prefblk; /* preferred blocksize when in fixed mode */ 264 }; 265 266 static struct sa_quirk_entry sa_quirk_table[] = 267 { 268 { 269 { T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "OnStream", 270 "ADR*", "*"}, SA_QUIRK_FIXED|SA_QUIRK_NODREAD | 271 SA_QUIRK_1FM|SA_QUIRK_NO_MODESEL, 32768 272 }, 273 { 274 { T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "ARCHIVE", 275 "Python 06408*", "*"}, SA_QUIRK_NODREAD, 0 276 }, 277 { 278 { T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "ARCHIVE", 279 "Python 25601*", "*"}, SA_QUIRK_NOCOMP|SA_QUIRK_NODREAD, 0 280 }, 281 { 282 { T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "ARCHIVE", 283 "Python*", "*"}, SA_QUIRK_NODREAD, 0 284 }, 285 { 286 { T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "ARCHIVE", 287 "VIPER 150*", "*"}, SA_QUIRK_FIXED|SA_QUIRK_1FM, 512 288 }, 289 { 290 { T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "ARCHIVE", 291 "VIPER 2525 25462", "-011"}, 292 SA_QUIRK_NOCOMP|SA_QUIRK_1FM|SA_QUIRK_NODREAD, 0 293 }, 294 { 295 { T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "ARCHIVE", 296 "VIPER 2525*", "*"}, SA_QUIRK_FIXED|SA_QUIRK_1FM, 1024 297 }, 298 #if 0 299 { 300 { T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "HP", 301 "C15*", "*"}, SA_QUIRK_VARIABLE|SA_QUIRK_NO_CPAGE, 0, 302 }, 303 #endif 304 { 305 { T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "HP", 306 "C56*", "*"}, SA_QUIRK_VARIABLE|SA_QUIRK_2FM, 0 307 }, 308 { 309 { T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "HP", 310 "T20*", "*"}, SA_QUIRK_FIXED|SA_QUIRK_1FM, 512 311 }, 312 { 313 { T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "HP", 314 "T4000*", "*"}, SA_QUIRK_FIXED|SA_QUIRK_1FM, 512 315 }, 316 { 317 { T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "HP", 318 "HP-88780*", "*"}, SA_QUIRK_VARIABLE|SA_QUIRK_2FM, 0 319 }, 320 { 321 { T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "KENNEDY", 322 "*", "*"}, SA_QUIRK_VARIABLE|SA_QUIRK_2FM, 0 323 }, 324 { 325 { T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "M4 DATA", 326 "123107 SCSI*", "*"}, SA_QUIRK_VARIABLE|SA_QUIRK_2FM, 0 327 }, 328 { /* jreynold@primenet.com */ 329 { T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "Seagate", 330 "STT8000N*", "*"}, SA_QUIRK_1FM, 0 331 }, 332 { /* mike@sentex.net */ 333 { T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "Seagate", 334 "STT20000*", "*"}, SA_QUIRK_1FM, 0 335 }, 336 { 337 { T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "TANDBERG", 338 " TDC 3600", "U07:"}, SA_QUIRK_NOCOMP|SA_QUIRK_1FM, 512 339 }, 340 { 341 { T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "TANDBERG", 342 " TDC 3800", "*"}, SA_QUIRK_NOCOMP|SA_QUIRK_1FM, 512 343 }, 344 { 345 { T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "TANDBERG", 346 " TDC 4100", "*"}, SA_QUIRK_NOCOMP|SA_QUIRK_1FM, 512 347 }, 348 { 349 { T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "TANDBERG", 350 " TDC 4200", "*"}, SA_QUIRK_NOCOMP|SA_QUIRK_1FM, 512 351 }, 352 { 353 { T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "TANDBERG", 354 " SLR*", "*"}, SA_QUIRK_1FM, 0 355 }, 356 { 357 { T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "WANGTEK", 358 "5525ES*", "*"}, SA_QUIRK_FIXED|SA_QUIRK_1FM, 512 359 }, 360 { 361 { T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "WANGTEK", 362 "51000*", "*"}, SA_QUIRK_FIXED|SA_QUIRK_1FM, 1024 363 } 364 }; 365 366 static d_open_t saopen; 367 static d_close_t saclose; 368 static d_strategy_t sastrategy; 369 static d_ioctl_t saioctl; 370 static periph_init_t sainit; 371 static periph_ctor_t saregister; 372 static periph_oninv_t saoninvalidate; 373 static periph_dtor_t sacleanup; 374 static periph_start_t sastart; 375 static void saasync(void *callback_arg, u_int32_t code, 376 struct cam_path *path, void *arg); 377 static void sadone(struct cam_periph *periph, 378 union ccb *start_ccb); 379 static int saerror(union ccb *ccb, u_int32_t cam_flags, 380 u_int32_t sense_flags); 381 static int samarkswanted(struct cam_periph *); 382 static int sacheckeod(struct cam_periph *periph); 383 static int sagetparams(struct cam_periph *periph, 384 sa_params params_to_get, 385 u_int32_t *blocksize, u_int8_t *density, 386 u_int32_t *numblocks, int *buff_mode, 387 u_int8_t *write_protect, u_int8_t *speed, 388 int *comp_supported, int *comp_enabled, 389 u_int32_t *comp_algorithm, 390 sa_comp_t *comp_page); 391 static int sasetparams(struct cam_periph *periph, 392 sa_params params_to_set, 393 u_int32_t blocksize, u_int8_t density, 394 u_int32_t comp_algorithm, 395 u_int32_t sense_flags); 396 static void saprevent(struct cam_periph *periph, int action); 397 static int sarewind(struct cam_periph *periph); 398 static int saspace(struct cam_periph *periph, int count, 399 scsi_space_code code); 400 static int samount(struct cam_periph *, int, cdev_t); 401 static int saretension(struct cam_periph *periph); 402 static int sareservereleaseunit(struct cam_periph *periph, 403 int reserve); 404 static int saloadunload(struct cam_periph *periph, int load); 405 static int saerase(struct cam_periph *periph, int longerase); 406 static int sawritefilemarks(struct cam_periph *periph, 407 int nmarks, int setmarks); 408 static int sardpos(struct cam_periph *periph, int, u_int32_t *); 409 static int sasetpos(struct cam_periph *periph, int, u_int32_t *); 410 411 412 static struct periph_driver sadriver = 413 { 414 sainit, "sa", 415 TAILQ_HEAD_INITIALIZER(sadriver.units), /* generation */ 0 416 }; 417 418 PERIPHDRIVER_DECLARE(sa, sadriver); 419 420 /* For 2.2-stable support */ 421 #ifndef D_TAPE 422 #define D_TAPE 0 423 #endif 424 425 static struct dev_ops sa_ops = { 426 { "sa", 0, D_TAPE }, 427 .d_open = saopen, 428 .d_close = saclose, 429 .d_read = physread, 430 .d_write = physwrite, 431 .d_ioctl = saioctl, 432 .d_strategy = sastrategy, 433 }; 434 435 static struct extend_array *saperiphs; 436 437 static int 438 saopen(struct dev_open_args *ap) 439 { 440 cdev_t dev = ap->a_head.a_dev; 441 struct cam_periph *periph; 442 struct sa_softc *softc; 443 int unit; 444 int error; 445 446 unit = SAUNIT(dev); 447 448 periph = cam_extend_get(saperiphs, unit); 449 if (cam_periph_acquire(periph) != CAM_REQ_CMP) { 450 return (ENXIO); 451 } 452 453 cam_periph_lock(periph); 454 455 softc = (struct sa_softc *)periph->softc; 456 457 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE|CAM_DEBUG_INFO, 458 ("saopen(%d): dev=0x%x softc=0x%x\n", unit, unit, softc->flags)); 459 460 if (SA_IS_CTRL(dev)) { 461 softc->ctrl_mode = 1; 462 cam_periph_unlock(periph); 463 return (0); 464 } 465 466 if ((error = cam_periph_hold(periph, PCATCH)) != 0) { 467 cam_periph_unlock(periph); 468 cam_periph_release(periph); 469 return (error); 470 } 471 472 if (softc->flags & SA_FLAG_OPEN) { 473 error = EBUSY; 474 } else if (softc->flags & SA_FLAG_INVALID) { 475 error = ENXIO; 476 } else { 477 /* 478 * Preserve whether this is a read_only open. 479 */ 480 softc->open_rdonly = (ap->a_oflags & O_RDWR) == O_RDONLY; 481 482 /* 483 * The function samount ensures media is loaded and ready. 484 * It also does a device RESERVE if the tape isn't yet mounted. 485 * 486 * If the mount fails and this was a non-blocking open, 487 * make this a 'open_pending_mount' action. 488 */ 489 error = samount(periph, ap->a_oflags, dev); 490 if (error && (ap->a_oflags & O_NONBLOCK)) { 491 softc->flags |= SA_FLAG_OPEN; 492 softc->open_pending_mount = 1; 493 cam_periph_unhold(periph, 1); 494 return (0); 495 } 496 } 497 498 if (error) { 499 cam_periph_unhold(periph, 1); 500 cam_periph_release(periph); 501 return (error); 502 } 503 504 saprevent(periph, PR_PREVENT); 505 softc->flags |= SA_FLAG_OPEN; 506 507 cam_periph_unhold(periph, 1); 508 return (error); 509 } 510 511 static int 512 saclose(struct dev_close_args *ap) 513 { 514 cdev_t dev = ap->a_head.a_dev; 515 struct cam_periph *periph; 516 struct sa_softc *softc; 517 int unit, mode, error, writing, tmp; 518 int closedbits = SA_FLAG_OPEN; 519 520 unit = SAUNIT(dev); 521 mode = SAMODE(dev); 522 periph = cam_extend_get(saperiphs, unit); 523 if (periph == NULL) 524 return (ENXIO); 525 526 cam_periph_lock(periph); 527 528 softc = (struct sa_softc *)periph->softc; 529 530 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE|CAM_DEBUG_INFO, 531 ("saclose(%d): dev=0x%x softc=0x%x\n", unit, unit, softc->flags)); 532 533 534 softc->open_rdonly = 0; 535 if (SA_IS_CTRL(dev)) { 536 softc->ctrl_mode = 0; 537 cam_periph_unlock(periph); 538 cam_periph_release(periph); 539 return (0); 540 } 541 542 if (softc->open_pending_mount) { 543 softc->flags &= ~SA_FLAG_OPEN; 544 softc->open_pending_mount = 0; 545 cam_periph_unlock(periph); 546 cam_periph_release(periph); 547 return (0); 548 } 549 550 if ((error = cam_periph_hold(periph, 0)) != 0) { 551 cam_periph_unlock(periph); 552 return (error); 553 } 554 555 /* 556 * Were we writing the tape? 557 */ 558 writing = (softc->flags & SA_FLAG_TAPE_WRITTEN) != 0; 559 560 /* 561 * See whether or not we need to write filemarks. If this 562 * fails, we probably have to assume we've lost tape 563 * position. 564 */ 565 error = sacheckeod(periph); 566 if (error) { 567 xpt_print(periph->path, 568 "failed to write terminating filemark(s)\n"); 569 softc->flags |= SA_FLAG_TAPE_FROZEN; 570 } 571 572 /* 573 * Whatever we end up doing, allow users to eject tapes from here on. 574 */ 575 saprevent(periph, PR_ALLOW); 576 577 /* 578 * Decide how to end... 579 */ 580 if ((softc->flags & SA_FLAG_TAPE_MOUNTED) == 0) { 581 closedbits |= SA_FLAG_TAPE_FROZEN; 582 } else switch (mode) { 583 case SA_MODE_OFFLINE: 584 /* 585 * An 'offline' close is an unconditional release of 586 * frozen && mount conditions, irrespective of whether 587 * these operations succeeded. The reason for this is 588 * to allow at least some kind of programmatic way 589 * around our state getting all fouled up. If somebody 590 * issues an 'offline' command, that will be allowed 591 * to clear state. 592 */ 593 sarewind(periph); 594 saloadunload(periph, FALSE); 595 closedbits |= SA_FLAG_TAPE_MOUNTED|SA_FLAG_TAPE_FROZEN; 596 break; 597 case SA_MODE_REWIND: 598 /* 599 * If the rewind fails, return an error- if anyone cares, 600 * but not overwriting any previous error. 601 * 602 * We don't clear the notion of mounted here, but we do 603 * clear the notion of frozen if we successfully rewound. 604 */ 605 tmp = sarewind(periph); 606 if (tmp) { 607 if (error != 0) 608 error = tmp; 609 } else { 610 closedbits |= SA_FLAG_TAPE_FROZEN; 611 } 612 break; 613 case SA_MODE_NOREWIND: 614 /* 615 * If we're not rewinding/unloading the tape, find out 616 * whether we need to back up over one of two filemarks 617 * we wrote (if we wrote two filemarks) so that appends 618 * from this point on will be sane. 619 */ 620 if (error == 0 && writing && (softc->quirks & SA_QUIRK_2FM)) { 621 tmp = saspace(periph, -1, SS_FILEMARKS); 622 if (tmp) { 623 xpt_print(periph->path, "unable to backspace " 624 "over one of double filemarks at end of " 625 "tape\n"); 626 xpt_print(periph->path, "it is possible that " 627 "this device needs a SA_QUIRK_1FM quirk set" 628 "for it\n"); 629 softc->flags |= SA_FLAG_TAPE_FROZEN; 630 } 631 } 632 break; 633 default: 634 xpt_print(periph->path, "unknown mode 0x%x in saclose\n", mode); 635 /* NOTREACHED */ 636 break; 637 } 638 639 /* 640 * We wish to note here that there are no more filemarks to be written. 641 */ 642 softc->filemarks = 0; 643 softc->flags &= ~SA_FLAG_TAPE_WRITTEN; 644 645 /* 646 * And we are no longer open for business. 647 */ 648 softc->flags &= ~closedbits; 649 650 /* 651 * Inform users if tape state if frozen.... 652 */ 653 if (softc->flags & SA_FLAG_TAPE_FROZEN) { 654 xpt_print(periph->path, "tape is now frozen- use an OFFLINE, " 655 "REWIND or MTEOM command to clear this state.\n"); 656 } 657 658 /* release the device if it is no longer mounted */ 659 if ((softc->flags & SA_FLAG_TAPE_MOUNTED) == 0) 660 sareservereleaseunit(periph, FALSE); 661 662 cam_periph_unhold(periph, 1); 663 cam_periph_release(periph); 664 665 return (error); 666 } 667 668 /* 669 * Actually translate the requested transfer into one the physical driver 670 * can understand. The transfer is described by a buf and will include 671 * only one physical transfer. 672 */ 673 static int 674 sastrategy(struct dev_strategy_args *ap) 675 { 676 cdev_t dev = ap->a_head.a_dev; 677 struct bio *bio = ap->a_bio; 678 struct buf *bp = bio->bio_buf; 679 struct cam_periph *periph; 680 struct sa_softc *softc; 681 u_int unit; 682 683 if (SA_IS_CTRL(dev)) { 684 bp->b_error = EINVAL; 685 goto bad; 686 } 687 unit = SAUNIT(dev); 688 periph = cam_extend_get(saperiphs, unit); 689 if (periph == NULL) { 690 bp->b_error = ENXIO; 691 goto bad; 692 } 693 cam_periph_lock(periph); 694 695 softc = (struct sa_softc *)periph->softc; 696 697 if (softc->flags & SA_FLAG_INVALID) { 698 cam_periph_unlock(periph); 699 bp->b_error = ENXIO; 700 goto bad; 701 } 702 703 if (softc->flags & SA_FLAG_TAPE_FROZEN) { 704 cam_periph_unlock(periph); 705 bp->b_error = EPERM; 706 goto bad; 707 } 708 709 /* 710 * This should actually never occur as the write(2) 711 * system call traps attempts to write to a read-only 712 * file descriptor. 713 */ 714 if (bp->b_cmd == BUF_CMD_WRITE && softc->open_rdonly) { 715 cam_periph_unlock(periph); 716 bp->b_error = EBADF; 717 goto bad; 718 } 719 720 if (softc->open_pending_mount) { 721 int error = samount(periph, 0, dev); 722 if (error) { 723 cam_periph_unlock(periph); 724 bp->b_error = ENXIO; 725 goto bad; 726 } 727 saprevent(periph, PR_PREVENT); 728 softc->open_pending_mount = 0; 729 } 730 731 /* 732 * If it's a null transfer, return immediately 733 */ 734 if (bp->b_bcount == 0) { 735 cam_periph_unlock(periph); 736 goto done; 737 } 738 739 /* valid request? */ 740 if (softc->flags & SA_FLAG_FIXED) { 741 /* 742 * Fixed block device. The byte count must 743 * be a multiple of our block size. 744 */ 745 if (((softc->blk_mask != ~0) && 746 ((bp->b_bcount & softc->blk_mask) != 0)) || 747 ((softc->blk_mask == ~0) && 748 ((bp->b_bcount % softc->min_blk) != 0))) { 749 xpt_print(periph->path, "Invalid request. Fixed block " 750 "device requests must be a multiple of %d bytes\n", 751 softc->min_blk); 752 cam_periph_unlock(periph); 753 bp->b_error = EINVAL; 754 goto bad; 755 } 756 } else if ((bp->b_bcount > softc->max_blk) || 757 (bp->b_bcount < softc->min_blk) || 758 (bp->b_bcount & softc->blk_mask) != 0) { 759 760 xpt_print_path(periph->path); 761 kprintf("Invalid request. Variable block " 762 "device requests must be "); 763 if (softc->blk_mask != 0) { 764 kprintf("a multiple of %d ", (0x1 << softc->blk_gran)); 765 } 766 kprintf("between %d and %d bytes\n", softc->min_blk, 767 softc->max_blk); 768 cam_periph_unlock(periph); 769 bp->b_error = EINVAL; 770 goto bad; 771 } 772 773 /* 774 * Place it at the end of the queue. 775 */ 776 bioq_insert_tail(&softc->bio_queue, bio); 777 softc->queue_count++; 778 #if 0 779 CAM_DEBUG(periph->path, CAM_DEBUG_INFO, 780 ("sastrategy: queuing a %ld %s byte %s\n", bp->bio_bcount, 781 (softc->flags & SA_FLAG_FIXED)? "fixed" : "variable", 782 (bp->bio_cmd == BIO_READ)? "read" : "write")); 783 #endif 784 if (softc->queue_count > 1) { 785 CAM_DEBUG(periph->path, CAM_DEBUG_INFO, 786 ("sastrategy: queue count now %d\n", softc->queue_count)); 787 } 788 789 /* 790 * Schedule ourselves for performing the work. 791 */ 792 xpt_schedule(periph, 1); 793 cam_periph_unlock(periph); 794 795 return(0); 796 bad: 797 bp->b_flags |= B_ERROR; 798 done: 799 800 /* 801 * Correctly set the buf to indicate a completed xfer 802 */ 803 bp->b_resid = bp->b_bcount; 804 biodone(bio); 805 return(0); 806 } 807 808 809 #define PENDING_MOUNT_CHECK(softc, periph, dev) \ 810 if (softc->open_pending_mount) { \ 811 error = samount(periph, 0, dev); \ 812 if (error) { \ 813 break; \ 814 } \ 815 saprevent(periph, PR_PREVENT); \ 816 softc->open_pending_mount = 0; \ 817 } 818 819 static int 820 saioctl(struct dev_ioctl_args *ap) 821 { 822 cdev_t dev = ap->a_head.a_dev; 823 caddr_t addr = ap->a_data; 824 struct cam_periph *periph; 825 struct sa_softc *softc; 826 scsi_space_code spaceop; 827 int didholdperiph = 0; 828 int unit; 829 int mode; 830 int error = 0; 831 832 unit = SAUNIT(dev); 833 mode = SAMODE(dev); 834 error = 0; /* shut up gcc */ 835 spaceop = 0; /* shut up gcc */ 836 837 periph = cam_extend_get(saperiphs, unit); 838 if (periph == NULL) 839 return (ENXIO); 840 841 cam_periph_lock(periph); 842 softc = (struct sa_softc *)periph->softc; 843 844 /* 845 * Check for control mode accesses. We allow MTIOCGET and 846 * MTIOCERRSTAT (but need to be the only one open in order 847 * to clear latched status), and MTSETBSIZE, MTSETDNSTY 848 * and MTCOMP (but need to be the only one accessing this 849 * device to run those). 850 */ 851 852 if (SA_IS_CTRL(dev)) { 853 switch (ap->a_cmd) { 854 case MTIOCGETEOTMODEL: 855 case MTIOCGET: 856 break; 857 case MTIOCERRSTAT: 858 /* 859 * If the periph isn't already locked, lock it 860 * so our MTIOCERRSTAT can reset latched error stats. 861 * 862 * If the periph is already locked, skip it because 863 * we're just getting status and it'll be up to the 864 * other thread that has this device open to do 865 * an MTIOCERRSTAT that would clear latched status. 866 */ 867 if ((periph->flags & CAM_PERIPH_LOCKED) == 0) { 868 error = cam_periph_hold(periph, PCATCH); 869 if (error != 0) { 870 return (error); 871 } 872 didholdperiph = 1; 873 } 874 break; 875 876 case MTIOCTOP: 877 { 878 struct mtop *mt = (struct mtop *) addr; 879 880 /* 881 * Check to make sure it's an OP we can perform 882 * with no media inserted. 883 */ 884 switch (mt->mt_op) { 885 case MTSETBSIZ: 886 case MTSETDNSTY: 887 case MTCOMP: 888 mt = NULL; 889 /* FALLTHROUGH */ 890 default: 891 break; 892 } 893 if (mt != NULL) { 894 break; 895 } 896 /* FALLTHROUGH */ 897 } 898 case MTIOCSETEOTMODEL: 899 /* 900 * We need to acquire the peripheral here rather 901 * than at open time because we are sharing writable 902 * access to data structures. 903 */ 904 error = cam_periph_hold(periph, PCATCH); 905 if (error != 0) { 906 return (error); 907 } 908 didholdperiph = 1; 909 break; 910 911 default: 912 return (EINVAL); 913 } 914 } 915 916 /* 917 * Find the device that the user is talking about 918 */ 919 switch (ap->a_cmd) { 920 case MTIOCGET: 921 { 922 struct mtget *g = (struct mtget *)addr; 923 924 /* 925 * If this isn't the control mode device, actually go out 926 * and ask the drive again what it's set to. 927 */ 928 if (!SA_IS_CTRL(dev) && !softc->open_pending_mount) { 929 u_int8_t write_protect = 0; /* silence gcc */ 930 int comp_enabled = 0; /* silence gcc */ 931 int comp_supported = 0; /* silence gcc */ 932 933 error = sagetparams(periph, SA_PARAM_ALL, 934 &softc->media_blksize, &softc->media_density, 935 &softc->media_numblks, &softc->buffer_mode, 936 &write_protect, &softc->speed, &comp_supported, 937 &comp_enabled, &softc->comp_algorithm, NULL); 938 if (error) 939 break; 940 if (write_protect) 941 softc->flags |= SA_FLAG_TAPE_WP; 942 else 943 softc->flags &= ~SA_FLAG_TAPE_WP; 944 softc->flags &= ~(SA_FLAG_COMP_SUPP| 945 SA_FLAG_COMP_ENABLED|SA_FLAG_COMP_UNSUPP); 946 if (comp_supported) { 947 if (softc->saved_comp_algorithm == 0) 948 softc->saved_comp_algorithm = 949 softc->comp_algorithm; 950 softc->flags |= SA_FLAG_COMP_SUPP; 951 if (comp_enabled) 952 softc->flags |= SA_FLAG_COMP_ENABLED; 953 } else 954 softc->flags |= SA_FLAG_COMP_UNSUPP; 955 } 956 bzero(g, sizeof(struct mtget)); 957 g->mt_type = MT_ISAR; 958 if (softc->flags & SA_FLAG_COMP_UNSUPP) { 959 g->mt_comp = MT_COMP_UNSUPP; 960 g->mt_comp0 = MT_COMP_UNSUPP; 961 g->mt_comp1 = MT_COMP_UNSUPP; 962 g->mt_comp2 = MT_COMP_UNSUPP; 963 g->mt_comp3 = MT_COMP_UNSUPP; 964 } else { 965 if ((softc->flags & SA_FLAG_COMP_ENABLED) == 0) { 966 g->mt_comp = MT_COMP_DISABLED; 967 } else { 968 g->mt_comp = softc->comp_algorithm; 969 } 970 g->mt_comp0 = softc->comp_algorithm; 971 g->mt_comp1 = softc->comp_algorithm; 972 g->mt_comp2 = softc->comp_algorithm; 973 g->mt_comp3 = softc->comp_algorithm; 974 } 975 g->mt_density = softc->media_density; 976 g->mt_density0 = softc->media_density; 977 g->mt_density1 = softc->media_density; 978 g->mt_density2 = softc->media_density; 979 g->mt_density3 = softc->media_density; 980 g->mt_blksiz = softc->media_blksize; 981 g->mt_blksiz0 = softc->media_blksize; 982 g->mt_blksiz1 = softc->media_blksize; 983 g->mt_blksiz2 = softc->media_blksize; 984 g->mt_blksiz3 = softc->media_blksize; 985 g->mt_fileno = softc->fileno; 986 g->mt_blkno = softc->blkno; 987 g->mt_dsreg = (short) softc->dsreg; 988 /* 989 * Yes, we know that this is likely to overflow 990 */ 991 if (softc->last_resid_was_io) { 992 if ((g->mt_resid = (short) softc->last_io_resid) != 0) { 993 if (SA_IS_CTRL(dev) == 0 || didholdperiph) { 994 softc->last_io_resid = 0; 995 } 996 } 997 } else { 998 if ((g->mt_resid = (short)softc->last_ctl_resid) != 0) { 999 if (SA_IS_CTRL(dev) == 0 || didholdperiph) { 1000 softc->last_ctl_resid = 0; 1001 } 1002 } 1003 } 1004 error = 0; 1005 break; 1006 } 1007 case MTIOCERRSTAT: 1008 { 1009 struct scsi_tape_errors *sep = 1010 &((union mterrstat *)addr)->scsi_errstat; 1011 1012 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, 1013 ("saioctl: MTIOCERRSTAT\n")); 1014 1015 bzero(sep, sizeof(*sep)); 1016 sep->io_resid = softc->last_io_resid; 1017 bcopy((caddr_t) &softc->last_io_sense, sep->io_sense, 1018 sizeof (sep->io_sense)); 1019 bcopy((caddr_t) &softc->last_io_cdb, sep->io_cdb, 1020 sizeof (sep->io_cdb)); 1021 sep->ctl_resid = softc->last_ctl_resid; 1022 bcopy((caddr_t) &softc->last_ctl_sense, sep->ctl_sense, 1023 sizeof (sep->ctl_sense)); 1024 bcopy((caddr_t) &softc->last_ctl_cdb, sep->ctl_cdb, 1025 sizeof (sep->ctl_cdb)); 1026 1027 if ((SA_IS_CTRL(dev) == 0 && softc->open_pending_mount) || 1028 didholdperiph) 1029 bzero((caddr_t) &softc->errinfo, 1030 sizeof (softc->errinfo)); 1031 error = 0; 1032 break; 1033 } 1034 case MTIOCTOP: 1035 { 1036 struct mtop *mt; 1037 int count; 1038 1039 PENDING_MOUNT_CHECK(softc, periph, dev); 1040 1041 mt = (struct mtop *)addr; 1042 1043 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, 1044 ("saioctl: op=0x%x count=0x%x\n", 1045 mt->mt_op, mt->mt_count)); 1046 1047 count = mt->mt_count; 1048 switch (mt->mt_op) { 1049 case MTWEOF: /* write an end-of-file marker */ 1050 /* 1051 * We don't need to clear the SA_FLAG_TAPE_WRITTEN 1052 * flag because by keeping track of filemarks 1053 * we have last written we know ehether or not 1054 * we need to write more when we close the device. 1055 */ 1056 error = sawritefilemarks(periph, count, FALSE); 1057 break; 1058 case MTWSS: /* write a setmark */ 1059 error = sawritefilemarks(periph, count, TRUE); 1060 break; 1061 case MTBSR: /* backward space record */ 1062 case MTFSR: /* forward space record */ 1063 case MTBSF: /* backward space file */ 1064 case MTFSF: /* forward space file */ 1065 case MTBSS: /* backward space setmark */ 1066 case MTFSS: /* forward space setmark */ 1067 case MTEOD: /* space to end of recorded medium */ 1068 { 1069 int nmarks; 1070 1071 spaceop = SS_FILEMARKS; 1072 nmarks = softc->filemarks; 1073 error = sacheckeod(periph); 1074 if (error) { 1075 xpt_print(periph->path, 1076 "EOD check prior to spacing failed\n"); 1077 softc->flags |= SA_FLAG_EIO_PENDING; 1078 break; 1079 } 1080 nmarks -= softc->filemarks; 1081 switch(mt->mt_op) { 1082 case MTBSR: 1083 count = -count; 1084 /* FALLTHROUGH */ 1085 case MTFSR: 1086 spaceop = SS_BLOCKS; 1087 break; 1088 case MTBSF: 1089 count = -count; 1090 /* FALLTHROUGH */ 1091 case MTFSF: 1092 break; 1093 case MTBSS: 1094 count = -count; 1095 /* FALLTHROUGH */ 1096 case MTFSS: 1097 spaceop = SS_SETMARKS; 1098 break; 1099 case MTEOD: 1100 spaceop = SS_EOD; 1101 count = 0; 1102 nmarks = 0; 1103 break; 1104 default: 1105 error = EINVAL; 1106 break; 1107 } 1108 if (error) 1109 break; 1110 1111 nmarks = softc->filemarks; 1112 /* 1113 * XXX: Why are we checking again? 1114 */ 1115 error = sacheckeod(periph); 1116 if (error) 1117 break; 1118 nmarks -= softc->filemarks; 1119 error = saspace(periph, count - nmarks, spaceop); 1120 /* 1121 * At this point, clear that we've written the tape 1122 * and that we've written any filemarks. We really 1123 * don't know what the applications wishes to do next- 1124 * the sacheckeod's will make sure we terminated the 1125 * tape correctly if we'd been writing, but the next 1126 * action the user application takes will set again 1127 * whether we need to write filemarks. 1128 */ 1129 softc->flags &= 1130 ~(SA_FLAG_TAPE_WRITTEN|SA_FLAG_TAPE_FROZEN); 1131 softc->filemarks = 0; 1132 break; 1133 } 1134 case MTREW: /* rewind */ 1135 PENDING_MOUNT_CHECK(softc, periph, dev); 1136 sacheckeod(periph); 1137 error = sarewind(periph); 1138 /* see above */ 1139 softc->flags &= 1140 ~(SA_FLAG_TAPE_WRITTEN|SA_FLAG_TAPE_FROZEN); 1141 softc->flags &= ~SA_FLAG_ERR_PENDING; 1142 softc->filemarks = 0; 1143 break; 1144 case MTERASE: /* erase */ 1145 PENDING_MOUNT_CHECK(softc, periph, dev); 1146 error = saerase(periph, count); 1147 softc->flags &= 1148 ~(SA_FLAG_TAPE_WRITTEN|SA_FLAG_TAPE_FROZEN); 1149 softc->flags &= ~SA_FLAG_ERR_PENDING; 1150 break; 1151 case MTRETENS: /* re-tension tape */ 1152 PENDING_MOUNT_CHECK(softc, periph, dev); 1153 error = saretension(periph); 1154 softc->flags &= 1155 ~(SA_FLAG_TAPE_WRITTEN|SA_FLAG_TAPE_FROZEN); 1156 softc->flags &= ~SA_FLAG_ERR_PENDING; 1157 break; 1158 case MTOFFL: /* rewind and put the drive offline */ 1159 1160 PENDING_MOUNT_CHECK(softc, periph, dev); 1161 1162 sacheckeod(periph); 1163 /* see above */ 1164 softc->flags &= ~SA_FLAG_TAPE_WRITTEN; 1165 softc->filemarks = 0; 1166 1167 error = sarewind(periph); 1168 /* clear the frozen flag anyway */ 1169 softc->flags &= ~SA_FLAG_TAPE_FROZEN; 1170 1171 /* 1172 * Be sure to allow media removal before ejecting. 1173 */ 1174 1175 saprevent(periph, PR_ALLOW); 1176 if (error == 0) { 1177 error = saloadunload(periph, FALSE); 1178 if (error == 0) { 1179 softc->flags &= ~SA_FLAG_TAPE_MOUNTED; 1180 } 1181 } 1182 break; 1183 1184 case MTNOP: /* no operation, sets status only */ 1185 case MTCACHE: /* enable controller cache */ 1186 case MTNOCACHE: /* disable controller cache */ 1187 error = 0; 1188 break; 1189 1190 case MTSETBSIZ: /* Set block size for device */ 1191 1192 PENDING_MOUNT_CHECK(softc, periph, dev); 1193 1194 error = sasetparams(periph, SA_PARAM_BLOCKSIZE, count, 1195 0, 0, 0); 1196 if (error == 0) { 1197 softc->last_media_blksize = 1198 softc->media_blksize; 1199 softc->media_blksize = count; 1200 if (count) { 1201 softc->flags |= SA_FLAG_FIXED; 1202 if (powerof2(count)) { 1203 softc->blk_shift = 1204 ffs(count) - 1; 1205 softc->blk_mask = count - 1; 1206 } else { 1207 softc->blk_mask = ~0; 1208 softc->blk_shift = 0; 1209 } 1210 /* 1211 * Make the user's desire 'persistent'. 1212 */ 1213 softc->quirks &= ~SA_QUIRK_VARIABLE; 1214 softc->quirks |= SA_QUIRK_FIXED; 1215 } else { 1216 softc->flags &= ~SA_FLAG_FIXED; 1217 if (softc->max_blk == 0) { 1218 softc->max_blk = ~0; 1219 } 1220 softc->blk_shift = 0; 1221 if (softc->blk_gran != 0) { 1222 softc->blk_mask = 1223 softc->blk_gran - 1; 1224 } else { 1225 softc->blk_mask = 0; 1226 } 1227 /* 1228 * Make the user's desire 'persistent'. 1229 */ 1230 softc->quirks |= SA_QUIRK_VARIABLE; 1231 softc->quirks &= ~SA_QUIRK_FIXED; 1232 } 1233 } 1234 break; 1235 case MTSETDNSTY: /* Set density for device and mode */ 1236 PENDING_MOUNT_CHECK(softc, periph, dev); 1237 1238 if (count > UCHAR_MAX) { 1239 error = EINVAL; 1240 break; 1241 } else { 1242 error = sasetparams(periph, SA_PARAM_DENSITY, 1243 0, count, 0, 0); 1244 } 1245 break; 1246 case MTCOMP: /* enable compression */ 1247 PENDING_MOUNT_CHECK(softc, periph, dev); 1248 /* 1249 * Some devices don't support compression, and 1250 * don't like it if you ask them for the 1251 * compression page. 1252 */ 1253 if ((softc->quirks & SA_QUIRK_NOCOMP) || 1254 (softc->flags & SA_FLAG_COMP_UNSUPP)) { 1255 error = ENODEV; 1256 break; 1257 } 1258 error = sasetparams(periph, SA_PARAM_COMPRESSION, 1259 0, 0, count, SF_NO_PRINT); 1260 break; 1261 default: 1262 error = EINVAL; 1263 } 1264 break; 1265 } 1266 case MTIOCIEOT: 1267 case MTIOCEEOT: 1268 error = 0; 1269 break; 1270 case MTIOCRDSPOS: 1271 PENDING_MOUNT_CHECK(softc, periph, dev); 1272 error = sardpos(periph, 0, (u_int32_t *) addr); 1273 break; 1274 case MTIOCRDHPOS: 1275 PENDING_MOUNT_CHECK(softc, periph, dev); 1276 error = sardpos(periph, 1, (u_int32_t *) addr); 1277 break; 1278 case MTIOCSLOCATE: 1279 PENDING_MOUNT_CHECK(softc, periph, dev); 1280 error = sasetpos(periph, 0, (u_int32_t *) addr); 1281 break; 1282 case MTIOCHLOCATE: 1283 PENDING_MOUNT_CHECK(softc, periph, dev); 1284 error = sasetpos(periph, 1, (u_int32_t *) addr); 1285 break; 1286 case MTIOCGETEOTMODEL: 1287 error = 0; 1288 if (softc->quirks & SA_QUIRK_1FM) 1289 mode = 1; 1290 else 1291 mode = 2; 1292 *((u_int32_t *) addr) = mode; 1293 break; 1294 case MTIOCSETEOTMODEL: 1295 error = 0; 1296 switch (*((u_int32_t *) addr)) { 1297 case 1: 1298 softc->quirks &= ~SA_QUIRK_2FM; 1299 softc->quirks |= SA_QUIRK_1FM; 1300 break; 1301 case 2: 1302 softc->quirks &= ~SA_QUIRK_1FM; 1303 softc->quirks |= SA_QUIRK_2FM; 1304 break; 1305 default: 1306 error = EINVAL; 1307 break; 1308 } 1309 break; 1310 default: 1311 error = cam_periph_ioctl(periph, ap->a_cmd, addr, saerror); 1312 break; 1313 } 1314 1315 /* 1316 * Check to see if we cleared a frozen state 1317 */ 1318 if (error == 0 && (softc->flags & SA_FLAG_TAPE_FROZEN)) { 1319 switch(ap->a_cmd) { 1320 case MTIOCRDSPOS: 1321 case MTIOCRDHPOS: 1322 case MTIOCSLOCATE: 1323 case MTIOCHLOCATE: 1324 softc->fileno = (daddr_t) -1; 1325 softc->blkno = (daddr_t) -1; 1326 softc->flags &= ~SA_FLAG_TAPE_FROZEN; 1327 xpt_print(periph->path, 1328 "tape state now unfrozen.\n"); 1329 break; 1330 default: 1331 break; 1332 } 1333 } 1334 if (didholdperiph) 1335 cam_periph_unhold(periph, 1); 1336 else 1337 cam_periph_unlock(periph); 1338 return (error); 1339 } 1340 1341 static void 1342 sainit(void) 1343 { 1344 cam_status status; 1345 1346 /* 1347 * Create our extend array for storing the devices we attach to. 1348 */ 1349 saperiphs = cam_extend_new(); 1350 if (saperiphs == NULL) { 1351 kprintf("sa: Failed to alloc extend array!\n"); 1352 return; 1353 } 1354 1355 /* 1356 * Install a global async callback. 1357 */ 1358 status = xpt_register_async(AC_FOUND_DEVICE, saasync, NULL, NULL); 1359 1360 if (status != CAM_REQ_CMP) { 1361 kprintf("sa: Failed to attach master async callback " 1362 "due to status 0x%x!\n", status); 1363 } 1364 } 1365 1366 static void 1367 saoninvalidate(struct cam_periph *periph) 1368 { 1369 struct sa_softc *softc; 1370 struct buf *q_bp; 1371 struct bio *q_bio; 1372 1373 softc = (struct sa_softc *)periph->softc; 1374 1375 /* 1376 * De-register any async callbacks. 1377 */ 1378 xpt_register_async(0, saasync, periph, periph->path); 1379 1380 softc->flags |= SA_FLAG_INVALID; 1381 1382 /* 1383 * Return all queued I/O with ENXIO. 1384 * XXX Handle any transactions queued to the card 1385 * with XPT_ABORT_CCB. 1386 */ 1387 while ((q_bio = bioq_first(&softc->bio_queue)) != NULL){ 1388 bioq_remove(&softc->bio_queue, q_bio); 1389 q_bp = q_bio->bio_buf; 1390 q_bp->b_resid = q_bp->b_bcount; 1391 q_bp->b_error = ENXIO; 1392 q_bp->b_flags |= B_ERROR; 1393 biodone(q_bio); 1394 } 1395 softc->queue_count = 0; 1396 1397 xpt_print(periph->path, "lost device\n"); 1398 1399 } 1400 1401 static void 1402 sacleanup(struct cam_periph *periph) 1403 { 1404 struct sa_softc *softc; 1405 1406 softc = (struct sa_softc *)periph->softc; 1407 1408 devstat_remove_entry(&softc->device_stats); 1409 1410 cam_extend_release(saperiphs, periph->unit_number); 1411 xpt_print(periph->path, "removing device entry\n"); 1412 dev_ops_remove_minor(&sa_ops, /*SA_UNITMASK,*/ SA_UNIT(periph->unit_number)); 1413 kfree(softc, M_SCSISA); 1414 } 1415 1416 static void 1417 saasync(void *callback_arg, u_int32_t code, 1418 struct cam_path *path, void *arg) 1419 { 1420 struct cam_periph *periph; 1421 1422 periph = (struct cam_periph *)callback_arg; 1423 switch (code) { 1424 case AC_FOUND_DEVICE: 1425 { 1426 struct ccb_getdev *cgd; 1427 cam_status status; 1428 1429 cgd = (struct ccb_getdev *)arg; 1430 if (cgd == NULL) 1431 break; 1432 1433 if (SID_TYPE(&cgd->inq_data) != T_SEQUENTIAL) 1434 break; 1435 1436 /* 1437 * Allocate a peripheral instance for 1438 * this device and start the probe 1439 * process. 1440 */ 1441 status = cam_periph_alloc(saregister, saoninvalidate, 1442 sacleanup, sastart, 1443 "sa", CAM_PERIPH_BIO, cgd->ccb_h.path, 1444 saasync, AC_FOUND_DEVICE, cgd); 1445 1446 if (status != CAM_REQ_CMP 1447 && status != CAM_REQ_INPROG) 1448 kprintf("saasync: Unable to probe new device " 1449 "due to status 0x%x\n", status); 1450 break; 1451 } 1452 default: 1453 cam_periph_async(periph, code, path, arg); 1454 break; 1455 } 1456 } 1457 1458 static cam_status 1459 saregister(struct cam_periph *periph, void *arg) 1460 { 1461 struct sa_softc *softc; 1462 struct ccb_getdev *cgd; 1463 caddr_t match; 1464 int i; 1465 1466 cgd = (struct ccb_getdev *)arg; 1467 if (periph == NULL) { 1468 kprintf("saregister: periph was NULL!!\n"); 1469 return (CAM_REQ_CMP_ERR); 1470 } 1471 1472 if (cgd == NULL) { 1473 kprintf("saregister: no getdev CCB, can't register device\n"); 1474 return (CAM_REQ_CMP_ERR); 1475 } 1476 1477 softc = kmalloc(sizeof (*softc), M_SCSISA, M_INTWAIT | M_ZERO); 1478 softc->scsi_rev = SID_ANSI_REV(&cgd->inq_data); 1479 softc->state = SA_STATE_NORMAL; 1480 softc->fileno = (daddr_t) -1; 1481 softc->blkno = (daddr_t) -1; 1482 1483 bioq_init(&softc->bio_queue); 1484 periph->softc = softc; 1485 cam_extend_set(saperiphs, periph->unit_number, periph); 1486 1487 /* 1488 * See if this device has any quirks. 1489 */ 1490 match = cam_quirkmatch((caddr_t)&cgd->inq_data, 1491 (caddr_t)sa_quirk_table, 1492 NELEM(sa_quirk_table), 1493 sizeof(*sa_quirk_table), scsi_inquiry_match); 1494 1495 if (match != NULL) { 1496 softc->quirks = ((struct sa_quirk_entry *)match)->quirks; 1497 softc->last_media_blksize = 1498 ((struct sa_quirk_entry *)match)->prefblk; 1499 #ifdef CAMDEBUG 1500 xpt_print(periph->path, "found quirk entry %d\n", 1501 (int) (((struct sa_quirk_entry *) match) - sa_quirk_table)); 1502 #endif 1503 } else 1504 softc->quirks = SA_QUIRK_NONE; 1505 1506 /* 1507 * The SA driver supports a blocksize, but we don't know the 1508 * blocksize until we media is inserted. So, set a flag to 1509 * indicate that the blocksize is unavailable right now. 1510 */ 1511 cam_periph_unlock(periph); 1512 devstat_add_entry(&softc->device_stats, "sa", periph->unit_number, 0, 1513 DEVSTAT_BS_UNAVAILABLE, SID_TYPE(&cgd->inq_data) | 1514 DEVSTAT_TYPE_IF_SCSI, DEVSTAT_PRIORITY_TAPE); 1515 1516 make_dev(&sa_ops, SAMINOR(SA_CTLDEV, 1517 periph->unit_number, 0, SA_ATYPE_R), UID_ROOT, GID_OPERATOR, 1518 0660, "%s%d.ctl", periph->periph_name, periph->unit_number); 1519 1520 make_dev(&sa_ops, SAMINOR(SA_NOT_CTLDEV, 1521 periph->unit_number, 0, SA_ATYPE_R), UID_ROOT, GID_OPERATOR, 1522 0660, "%s%d", periph->periph_name, periph->unit_number); 1523 1524 make_dev(&sa_ops, SAMINOR(SA_NOT_CTLDEV, 1525 periph->unit_number, 0, SA_ATYPE_NR), UID_ROOT, GID_OPERATOR, 1526 0660, "n%s%d", periph->periph_name, periph->unit_number); 1527 1528 make_dev(&sa_ops, SAMINOR(SA_NOT_CTLDEV, 1529 periph->unit_number, 0, SA_ATYPE_ER), UID_ROOT, GID_OPERATOR, 1530 0660, "e%s%d", periph->periph_name, periph->unit_number); 1531 1532 for (i = 0; i < SA_NUM_MODES; i++) { 1533 1534 make_dev(&sa_ops, 1535 SAMINOR(SA_NOT_CTLDEV, periph->unit_number, i, SA_ATYPE_R), 1536 UID_ROOT, GID_OPERATOR, 0660, "%s%d.%d", 1537 periph->periph_name, periph->unit_number, i); 1538 1539 make_dev(&sa_ops, 1540 SAMINOR(SA_NOT_CTLDEV, periph->unit_number, i, SA_ATYPE_NR), 1541 UID_ROOT, GID_OPERATOR, 0660, "n%s%d.%d", 1542 periph->periph_name, periph->unit_number, i); 1543 1544 1545 make_dev(&sa_ops, 1546 SAMINOR(SA_NOT_CTLDEV, periph->unit_number, i, SA_ATYPE_ER), 1547 UID_ROOT, GID_OPERATOR, 0660, "e%s%d.%d", 1548 periph->periph_name, periph->unit_number, i); 1549 } 1550 cam_periph_lock(periph); 1551 1552 /* 1553 * Add an async callback so that we get 1554 * notified if this device goes away. 1555 */ 1556 xpt_register_async(AC_LOST_DEVICE, saasync, periph, periph->path); 1557 1558 xpt_announce_periph(periph, NULL); 1559 1560 return (CAM_REQ_CMP); 1561 } 1562 1563 static void 1564 sastart(struct cam_periph *periph, union ccb *start_ccb) 1565 { 1566 struct sa_softc *softc; 1567 1568 softc = (struct sa_softc *)periph->softc; 1569 1570 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("sastart\n")); 1571 1572 1573 switch (softc->state) { 1574 case SA_STATE_NORMAL: 1575 { 1576 /* Pull a buffer from the queue and get going on it */ 1577 struct buf *bp; 1578 struct bio *bio; 1579 1580 /* 1581 * See if there is a buf with work for us to do.. 1582 */ 1583 bio = bioq_first(&softc->bio_queue); 1584 if (periph->immediate_priority <= periph->pinfo.priority) { 1585 CAM_DEBUG_PRINT(CAM_DEBUG_SUBTRACE, 1586 ("queuing for immediate ccb\n")); 1587 Set_CCB_Type(start_ccb, SA_CCB_WAITING); 1588 SLIST_INSERT_HEAD(&periph->ccb_list, &start_ccb->ccb_h, 1589 periph_links.sle); 1590 periph->immediate_priority = CAM_PRIORITY_NONE; 1591 wakeup(&periph->ccb_list); 1592 } else if (bio == NULL) { 1593 xpt_release_ccb(start_ccb); 1594 } else if ((softc->flags & SA_FLAG_ERR_PENDING) != 0) { 1595 struct bio *done_bio; 1596 again: 1597 softc->queue_count--; 1598 bioq_remove(&softc->bio_queue, bio); 1599 bp = bio->bio_buf; 1600 bp->b_resid = bp->b_bcount; 1601 done_bio = bio; 1602 if ((softc->flags & SA_FLAG_EOM_PENDING) != 0) { 1603 /* 1604 * We now just clear errors in this case 1605 * and let the residual be the notifier. 1606 */ 1607 bp->b_error = 0; 1608 } else if ((softc->flags & SA_FLAG_EOF_PENDING) != 0) { 1609 /* 1610 * This can only happen if we're reading 1611 * in fixed length mode. In this case, 1612 * we dump the rest of the list the 1613 * same way. 1614 */ 1615 bp->b_error = 0; 1616 if (bioq_first(&softc->bio_queue) != NULL) { 1617 biodone(done_bio); 1618 goto again; 1619 } 1620 } else if ((softc->flags & SA_FLAG_EIO_PENDING) != 0) { 1621 bp->b_error = EIO; 1622 bp->b_flags |= B_ERROR; 1623 } 1624 bio = bioq_first(&softc->bio_queue); 1625 /* 1626 * Only if we have no other buffers queued up 1627 * do we clear the pending error flag. 1628 */ 1629 if (bio == NULL) 1630 softc->flags &= ~SA_FLAG_ERR_PENDING; 1631 CAM_DEBUG(periph->path, CAM_DEBUG_INFO, 1632 ("sastart- ERR_PENDING now 0x%x, bio is %sNULL, " 1633 "%d more buffers queued up\n", 1634 (softc->flags & SA_FLAG_ERR_PENDING), 1635 (bio != NULL)? "not " : " ", softc->queue_count)); 1636 xpt_release_ccb(start_ccb); 1637 biodone(done_bio); 1638 } else { 1639 u_int32_t length; 1640 1641 bioq_remove(&softc->bio_queue, bio); 1642 bp = bio->bio_buf; 1643 softc->queue_count--; 1644 1645 if ((softc->flags & SA_FLAG_FIXED) != 0) { 1646 if (softc->blk_shift != 0) { 1647 length = 1648 bp->b_bcount >> softc->blk_shift; 1649 } else if (softc->media_blksize != 0) { 1650 length = 1651 bp->b_bcount / softc->media_blksize; 1652 } else { 1653 bp->b_error = EIO; 1654 xpt_print(periph->path, "zero blocksize" 1655 " for FIXED length writes?\n"); 1656 biodone(bio); 1657 break; 1658 } 1659 #if 0 1660 CAM_DEBUG(start_ccb->ccb_h.path, CAM_DEBUG_INFO, 1661 ("issuing a %d fixed record %s\n", 1662 length, (bp->bio_cmd == BIO_READ)? "read" : 1663 "write")); 1664 #endif 1665 } else { 1666 length = bp->b_bcount; 1667 #if 0 1668 CAM_DEBUG(start_ccb->ccb_h.path, CAM_DEBUG_INFO, 1669 ("issuing a %d variable byte %s\n", 1670 length, (bp->bio_cmd == BIO_READ)? "read" : 1671 "write")); 1672 #endif 1673 } 1674 devstat_start_transaction(&softc->device_stats); 1675 /* 1676 * Some people have theorized that we should 1677 * suppress illegal length indication if we are 1678 * running in variable block mode so that we don't 1679 * have to request sense every time our requested 1680 * block size is larger than the written block. 1681 * The residual information from the ccb allows 1682 * us to identify this situation anyway. The only 1683 * problem with this is that we will not get 1684 * information about blocks that are larger than 1685 * our read buffer unless we set the block size 1686 * in the mode page to something other than 0. 1687 * 1688 * I believe that this is a non-issue. If user apps 1689 * don't adjust their read size to match our record 1690 * size, that's just life. Anyway, the typical usage 1691 * would be to issue, e.g., 64KB reads and occasionally 1692 * have to do deal with 512 byte or 1KB intermediate 1693 * records. 1694 */ 1695 softc->dsreg = (bp->b_cmd == BUF_CMD_READ) ? 1696 MTIO_DSREG_RD : MTIO_DSREG_WR; 1697 scsi_sa_read_write(&start_ccb->csio, 0, sadone, 1698 MSG_SIMPLE_Q_TAG, (bp->b_cmd == BUF_CMD_READ) != 0, 1699 FALSE, (softc->flags & SA_FLAG_FIXED) != 0, 1700 length, bp->b_data, bp->b_bcount, SSD_FULL_SIZE, 1701 IO_TIMEOUT); 1702 start_ccb->ccb_h.ccb_pflags &= ~SA_POSITION_UPDATED; 1703 Set_CCB_Type(start_ccb, SA_CCB_BUFFER_IO); 1704 start_ccb->ccb_h.ccb_bio = bio; 1705 bio = bioq_first(&softc->bio_queue); 1706 xpt_action(start_ccb); 1707 } 1708 1709 if (bio != NULL) { 1710 /* Have more work to do, so ensure we stay scheduled */ 1711 xpt_schedule(periph, 1); 1712 } 1713 break; 1714 } 1715 case SA_STATE_ABNORMAL: 1716 default: 1717 panic("state 0x%x in sastart", softc->state); 1718 break; 1719 } 1720 } 1721 1722 1723 static void 1724 sadone(struct cam_periph *periph, union ccb *done_ccb) 1725 { 1726 struct sa_softc *softc; 1727 struct ccb_scsiio *csio; 1728 1729 softc = (struct sa_softc *)periph->softc; 1730 csio = &done_ccb->csio; 1731 switch (CCB_Type(csio)) { 1732 case SA_CCB_BUFFER_IO: 1733 { 1734 struct buf *bp; 1735 struct bio *bio; 1736 int error; 1737 1738 softc->dsreg = MTIO_DSREG_REST; 1739 bio = (struct bio *)done_ccb->ccb_h.ccb_bio; 1740 bp = bio->bio_buf; 1741 error = 0; 1742 if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) { 1743 if ((error = saerror(done_ccb, 0, 0)) == ERESTART) { 1744 /* 1745 * A retry was scheduled, so just return. 1746 */ 1747 return; 1748 } 1749 } 1750 1751 if (error == EIO) { 1752 struct buf *q_bp; 1753 struct bio *q_bio; 1754 1755 /* 1756 * Catastrophic error. Mark the tape as frozen 1757 * (we no longer know tape position). 1758 * 1759 * Return all queued I/O with EIO, and unfreeze 1760 * our queue so that future transactions that 1761 * attempt to fix this problem can get to the 1762 * device. 1763 * 1764 */ 1765 1766 softc->flags |= SA_FLAG_TAPE_FROZEN; 1767 while ((q_bio = bioq_first(&softc->bio_queue)) != NULL) { 1768 bioq_remove(&softc->bio_queue, q_bio); 1769 q_bp = q_bio->bio_buf; 1770 q_bp->b_resid = q_bp->b_bcount; 1771 q_bp->b_error = EIO; 1772 q_bp->b_flags |= B_ERROR; 1773 biodone(q_bio); 1774 } 1775 } 1776 if (error != 0) { 1777 bp->b_resid = bp->b_bcount; 1778 bp->b_error = error; 1779 bp->b_flags |= B_ERROR; 1780 /* 1781 * In the error case, position is updated in saerror. 1782 */ 1783 } else { 1784 bp->b_resid = csio->resid; 1785 bp->b_error = 0; 1786 if (csio->resid != 0) { 1787 bp->b_flags |= B_ERROR; 1788 } 1789 if (bp->b_cmd != BUF_CMD_READ) { 1790 softc->flags |= SA_FLAG_TAPE_WRITTEN; 1791 softc->filemarks = 0; 1792 } 1793 if (!(csio->ccb_h.ccb_pflags & SA_POSITION_UPDATED) && 1794 (softc->blkno != (daddr_t) -1)) { 1795 if ((softc->flags & SA_FLAG_FIXED) != 0) { 1796 u_int32_t l; 1797 if (softc->blk_shift != 0) { 1798 l = bp->b_bcount >> 1799 softc->blk_shift; 1800 } else { 1801 l = bp->b_bcount / 1802 softc->media_blksize; 1803 } 1804 softc->blkno += (daddr_t) l; 1805 } else { 1806 softc->blkno++; 1807 } 1808 } 1809 } 1810 /* 1811 * If we had an error (immediate or pending), 1812 * release the device queue now. 1813 */ 1814 if (error || (softc->flags & SA_FLAG_ERR_PENDING)) 1815 cam_release_devq(done_ccb->ccb_h.path, 0, 0, 0, 0); 1816 #ifdef CAMDEBUG 1817 if (error || bp->b_resid) { 1818 CAM_DEBUG(periph->path, CAM_DEBUG_INFO, 1819 ("error %d resid %d count %d\n", error, 1820 bp->b_resid, bp->b_bcount)); 1821 } 1822 #endif 1823 devstat_end_transaction_buf(&softc->device_stats, bp); 1824 biodone(bio); 1825 break; 1826 } 1827 case SA_CCB_WAITING: 1828 { 1829 /* Caller will release the CCB */ 1830 wakeup(&done_ccb->ccb_h.cbfcnp); 1831 return; 1832 } 1833 } 1834 xpt_release_ccb(done_ccb); 1835 } 1836 1837 /* 1838 * Mount the tape (make sure it's ready for I/O). 1839 * 1840 * oflags can be checked for 'kind' of open (read-only check) - later 1841 * dev can be checked for a control-mode or compression open - later 1842 */ 1843 static int 1844 samount(struct cam_periph *periph, __unused int oflags, __unused cdev_t dev) 1845 { 1846 struct sa_softc *softc; 1847 union ccb *ccb; 1848 int error; 1849 1850 softc = (struct sa_softc *)periph->softc; 1851 1852 /* 1853 * This should determine if something has happend since the last 1854 * open/mount that would invalidate the mount. We do *not* want 1855 * to retry this command- we just want the status. But we only 1856 * do this if we're mounted already- if we're not mounted, 1857 * we don't care about the unit read state and can instead use 1858 * this opportunity to attempt to reserve the tape unit. 1859 */ 1860 1861 if (softc->flags & SA_FLAG_TAPE_MOUNTED) { 1862 ccb = cam_periph_getccb(periph, 1); 1863 scsi_test_unit_ready(&ccb->csio, 0, sadone, 1864 MSG_SIMPLE_Q_TAG, SSD_FULL_SIZE, IO_TIMEOUT); 1865 error = cam_periph_runccb(ccb, saerror, 0, SF_NO_PRINT, 1866 &softc->device_stats); 1867 QFRLS(ccb); 1868 if (error == ENXIO) { 1869 softc->flags &= ~SA_FLAG_TAPE_MOUNTED; 1870 scsi_test_unit_ready(&ccb->csio, 0, sadone, 1871 MSG_SIMPLE_Q_TAG, SSD_FULL_SIZE, IO_TIMEOUT); 1872 error = cam_periph_runccb(ccb, saerror, 0, SF_NO_PRINT, 1873 &softc->device_stats); 1874 QFRLS(ccb); 1875 } else if (error) { 1876 /* 1877 * We don't need to freeze the tape because we 1878 * will now attempt to rewind/load it. 1879 */ 1880 softc->flags &= ~SA_FLAG_TAPE_MOUNTED; 1881 if (CAM_DEBUGGED(periph->path, CAM_DEBUG_INFO)) { 1882 xpt_print(periph->path, 1883 "error %d on TUR in samount\n", error); 1884 } 1885 } 1886 } else { 1887 error = sareservereleaseunit(periph, TRUE); 1888 if (error) { 1889 return (error); 1890 } 1891 ccb = cam_periph_getccb(periph, 1); 1892 scsi_test_unit_ready(&ccb->csio, 0, sadone, 1893 MSG_SIMPLE_Q_TAG, SSD_FULL_SIZE, IO_TIMEOUT); 1894 error = cam_periph_runccb(ccb, saerror, 0, SF_NO_PRINT, 1895 &softc->device_stats); 1896 QFRLS(ccb); 1897 } 1898 1899 if ((softc->flags & SA_FLAG_TAPE_MOUNTED) == 0) { 1900 struct scsi_read_block_limits_data *rblim = NULL; 1901 int comp_enabled = 0; /* silence gcc */ 1902 int comp_supported = 0; /* silence gcc */ 1903 u_int8_t write_protect = 0; /* silence gcc */ 1904 u_int8_t guessing = 0; 1905 1906 /* 1907 * Clear out old state. 1908 */ 1909 softc->flags &= ~(SA_FLAG_TAPE_WP|SA_FLAG_TAPE_WRITTEN| 1910 SA_FLAG_ERR_PENDING|SA_FLAG_COMP_ENABLED| 1911 SA_FLAG_COMP_SUPP|SA_FLAG_COMP_UNSUPP); 1912 softc->filemarks = 0; 1913 1914 /* 1915 * *Very* first off, make sure we're loaded to BOT. 1916 */ 1917 scsi_load_unload(&ccb->csio, 2, sadone, MSG_SIMPLE_Q_TAG, FALSE, 1918 FALSE, FALSE, 1, SSD_FULL_SIZE, REWIND_TIMEOUT); 1919 error = cam_periph_runccb(ccb, saerror, 0, SF_NO_PRINT, 1920 &softc->device_stats); 1921 QFRLS(ccb); 1922 1923 /* 1924 * In case this doesn't work, do a REWIND instead 1925 */ 1926 if (error) { 1927 scsi_rewind(&ccb->csio, 2, sadone, MSG_SIMPLE_Q_TAG, 1928 FALSE, SSD_FULL_SIZE, REWIND_TIMEOUT); 1929 error = cam_periph_runccb(ccb, saerror, 0, SF_NO_PRINT, 1930 &softc->device_stats); 1931 QFRLS(ccb); 1932 } 1933 if (error) { 1934 xpt_release_ccb(ccb); 1935 goto exit; 1936 } 1937 1938 /* 1939 * Do a dummy test read to force access to the 1940 * media so that the drive will really know what's 1941 * there. We actually don't really care what the 1942 * blocksize on tape is and don't expect to really 1943 * read a full record. 1944 */ 1945 rblim = kmalloc(8192, M_SCSISA, M_INTWAIT); 1946 1947 if ((softc->quirks & SA_QUIRK_NODREAD) == 0) { 1948 scsi_sa_read_write(&ccb->csio, 0, sadone, 1949 MSG_SIMPLE_Q_TAG, 1, FALSE, 0, 8192, 1950 (void *) rblim, 8192, SSD_FULL_SIZE, 1951 IO_TIMEOUT); 1952 cam_periph_runccb(ccb, saerror, 0, SF_NO_PRINT, 1953 &softc->device_stats); 1954 QFRLS(ccb); 1955 scsi_rewind(&ccb->csio, 1, sadone, MSG_SIMPLE_Q_TAG, 1956 FALSE, SSD_FULL_SIZE, REWIND_TIMEOUT); 1957 error = cam_periph_runccb(ccb, saerror, CAM_RETRY_SELTO, 1958 SF_NO_PRINT | SF_RETRY_UA, 1959 &softc->device_stats); 1960 QFRLS(ccb); 1961 if (error) { 1962 xpt_print(periph->path, 1963 "unable to rewind after test read\n"); 1964 xpt_release_ccb(ccb); 1965 goto exit; 1966 } 1967 } 1968 1969 /* 1970 * Next off, determine block limits. 1971 */ 1972 scsi_read_block_limits(&ccb->csio, 5, sadone, MSG_SIMPLE_Q_TAG, 1973 rblim, SSD_FULL_SIZE, SCSIOP_TIMEOUT); 1974 1975 error = cam_periph_runccb(ccb, saerror, CAM_RETRY_SELTO, 1976 SF_NO_PRINT | SF_RETRY_UA, &softc->device_stats); 1977 1978 QFRLS(ccb); 1979 xpt_release_ccb(ccb); 1980 1981 if (error != 0) { 1982 /* 1983 * If it's less than SCSI-2, READ BLOCK LIMITS is not 1984 * a MANDATORY command. Anyway- it doesn't matter- 1985 * we can proceed anyway. 1986 */ 1987 softc->blk_gran = 0; 1988 softc->max_blk = ~0; 1989 softc->min_blk = 0; 1990 } else { 1991 if (softc->scsi_rev >= SCSI_REV_SPC) { 1992 softc->blk_gran = RBL_GRAN(rblim); 1993 } else { 1994 softc->blk_gran = 0; 1995 } 1996 /* 1997 * We take max_blk == min_blk to mean a default to 1998 * fixed mode- but note that whatever we get out of 1999 * sagetparams below will actually determine whether 2000 * we are actually *in* fixed mode. 2001 */ 2002 softc->max_blk = scsi_3btoul(rblim->maximum); 2003 softc->min_blk = scsi_2btoul(rblim->minimum); 2004 2005 2006 } 2007 /* 2008 * Next, perform a mode sense to determine 2009 * current density, blocksize, compression etc. 2010 */ 2011 error = sagetparams(periph, SA_PARAM_ALL, 2012 &softc->media_blksize, 2013 &softc->media_density, 2014 &softc->media_numblks, 2015 &softc->buffer_mode, &write_protect, 2016 &softc->speed, &comp_supported, 2017 &comp_enabled, &softc->comp_algorithm, 2018 NULL); 2019 2020 if (error != 0) { 2021 /* 2022 * We could work a little harder here. We could 2023 * adjust our attempts to get information. It 2024 * might be an ancient tape drive. If someone 2025 * nudges us, we'll do that. 2026 */ 2027 goto exit; 2028 } 2029 2030 /* 2031 * If no quirk has determined that this is a device that is 2032 * preferred to be in fixed or variable mode, now is the time 2033 * to find out. 2034 */ 2035 if ((softc->quirks & (SA_QUIRK_FIXED|SA_QUIRK_VARIABLE)) == 0) { 2036 guessing = 1; 2037 /* 2038 * This could be expensive to find out. Luckily we 2039 * only need to do this once. If we start out in 2040 * 'default' mode, try and set ourselves to one 2041 * of the densities that would determine a wad 2042 * of other stuff. Go from highest to lowest. 2043 */ 2044 if (softc->media_density == SCSI_DEFAULT_DENSITY) { 2045 int i; 2046 static u_int8_t ctry[] = { 2047 SCSI_DENSITY_HALFINCH_PE, 2048 SCSI_DENSITY_HALFINCH_6250C, 2049 SCSI_DENSITY_HALFINCH_6250, 2050 SCSI_DENSITY_HALFINCH_1600, 2051 SCSI_DENSITY_HALFINCH_800, 2052 SCSI_DENSITY_QIC_4GB, 2053 SCSI_DENSITY_QIC_2GB, 2054 SCSI_DENSITY_QIC_525_320, 2055 SCSI_DENSITY_QIC_150, 2056 SCSI_DENSITY_QIC_120, 2057 SCSI_DENSITY_QIC_24, 2058 SCSI_DENSITY_QIC_11_9TRK, 2059 SCSI_DENSITY_QIC_11_4TRK, 2060 SCSI_DENSITY_QIC_1320, 2061 SCSI_DENSITY_QIC_3080, 2062 0 2063 }; 2064 for (i = 0; ctry[i]; i++) { 2065 error = sasetparams(periph, 2066 SA_PARAM_DENSITY, 0, ctry[i], 2067 0, SF_NO_PRINT); 2068 if (error == 0) { 2069 softc->media_density = ctry[i]; 2070 break; 2071 } 2072 } 2073 } 2074 switch (softc->media_density) { 2075 case SCSI_DENSITY_QIC_11_4TRK: 2076 case SCSI_DENSITY_QIC_11_9TRK: 2077 case SCSI_DENSITY_QIC_24: 2078 case SCSI_DENSITY_QIC_120: 2079 case SCSI_DENSITY_QIC_150: 2080 case SCSI_DENSITY_QIC_525_320: 2081 case SCSI_DENSITY_QIC_1320: 2082 case SCSI_DENSITY_QIC_3080: 2083 softc->quirks &= ~SA_QUIRK_2FM; 2084 softc->quirks |= SA_QUIRK_FIXED|SA_QUIRK_1FM; 2085 softc->last_media_blksize = 512; 2086 break; 2087 case SCSI_DENSITY_QIC_4GB: 2088 case SCSI_DENSITY_QIC_2GB: 2089 softc->quirks &= ~SA_QUIRK_2FM; 2090 softc->quirks |= SA_QUIRK_FIXED|SA_QUIRK_1FM; 2091 softc->last_media_blksize = 1024; 2092 break; 2093 default: 2094 softc->last_media_blksize = 2095 softc->media_blksize; 2096 softc->quirks |= SA_QUIRK_VARIABLE; 2097 break; 2098 } 2099 } 2100 2101 /* 2102 * If no quirk has determined that this is a device that needs 2103 * to have 2 Filemarks at EOD, now is the time to find out. 2104 */ 2105 2106 if ((softc->quirks & SA_QUIRK_2FM) == 0) { 2107 switch (softc->media_density) { 2108 case SCSI_DENSITY_HALFINCH_800: 2109 case SCSI_DENSITY_HALFINCH_1600: 2110 case SCSI_DENSITY_HALFINCH_6250: 2111 case SCSI_DENSITY_HALFINCH_6250C: 2112 case SCSI_DENSITY_HALFINCH_PE: 2113 softc->quirks &= ~SA_QUIRK_1FM; 2114 softc->quirks |= SA_QUIRK_2FM; 2115 break; 2116 default: 2117 break; 2118 } 2119 } 2120 2121 /* 2122 * Now validate that some info we got makes sense. 2123 */ 2124 if ((softc->max_blk < softc->media_blksize) || 2125 (softc->min_blk > softc->media_blksize && 2126 softc->media_blksize)) { 2127 xpt_print(periph->path, 2128 "BLOCK LIMITS (%d..%d) could not match current " 2129 "block settings (%d)- adjusting\n", softc->min_blk, 2130 softc->max_blk, softc->media_blksize); 2131 softc->max_blk = softc->min_blk = 2132 softc->media_blksize; 2133 } 2134 2135 /* 2136 * Now put ourselves into the right frame of mind based 2137 * upon quirks... 2138 */ 2139 tryagain: 2140 /* 2141 * If we want to be in FIXED mode and our current blocksize 2142 * is not equal to our last blocksize (if nonzero), try and 2143 * set ourselves to this last blocksize (as the 'preferred' 2144 * block size). The initial quirkmatch at registry sets the 2145 * initial 'last' blocksize. If, for whatever reason, this 2146 * 'last' blocksize is zero, set the blocksize to 512, 2147 * or min_blk if that's larger. 2148 */ 2149 if ((softc->quirks & SA_QUIRK_FIXED) && 2150 (softc->quirks & SA_QUIRK_NO_MODESEL) == 0 && 2151 (softc->media_blksize != softc->last_media_blksize)) { 2152 softc->media_blksize = softc->last_media_blksize; 2153 if (softc->media_blksize == 0) { 2154 softc->media_blksize = 512; 2155 if (softc->media_blksize < softc->min_blk) { 2156 softc->media_blksize = softc->min_blk; 2157 } 2158 } 2159 error = sasetparams(periph, SA_PARAM_BLOCKSIZE, 2160 softc->media_blksize, 0, 0, SF_NO_PRINT); 2161 if (error) { 2162 xpt_print(periph->path, 2163 "unable to set fixed blocksize to %d\n", 2164 softc->media_blksize); 2165 goto exit; 2166 } 2167 } 2168 2169 if ((softc->quirks & SA_QUIRK_VARIABLE) && 2170 (softc->media_blksize != 0)) { 2171 softc->last_media_blksize = softc->media_blksize; 2172 softc->media_blksize = 0; 2173 error = sasetparams(periph, SA_PARAM_BLOCKSIZE, 2174 0, 0, 0, SF_NO_PRINT); 2175 if (error) { 2176 /* 2177 * If this fails and we were guessing, just 2178 * assume that we got it wrong and go try 2179 * fixed block mode. Don't even check against 2180 * density code at this point. 2181 */ 2182 if (guessing) { 2183 softc->quirks &= ~SA_QUIRK_VARIABLE; 2184 softc->quirks |= SA_QUIRK_FIXED; 2185 if (softc->last_media_blksize == 0) 2186 softc->last_media_blksize = 512; 2187 goto tryagain; 2188 } 2189 xpt_print(periph->path, 2190 "unable to set variable blocksize\n"); 2191 goto exit; 2192 } 2193 } 2194 2195 /* 2196 * Now that we have the current block size, 2197 * set up some parameters for sastart's usage. 2198 */ 2199 if (softc->media_blksize) { 2200 softc->flags |= SA_FLAG_FIXED; 2201 if (powerof2(softc->media_blksize)) { 2202 softc->blk_shift = 2203 ffs(softc->media_blksize) - 1; 2204 softc->blk_mask = softc->media_blksize - 1; 2205 } else { 2206 softc->blk_mask = ~0; 2207 softc->blk_shift = 0; 2208 } 2209 } else { 2210 /* 2211 * The SCSI-3 spec allows 0 to mean "unspecified". 2212 * The SCSI-1 spec allows 0 to mean 'infinite'. 2213 * 2214 * Either works here. 2215 */ 2216 if (softc->max_blk == 0) { 2217 softc->max_blk = ~0; 2218 } 2219 softc->blk_shift = 0; 2220 if (softc->blk_gran != 0) { 2221 softc->blk_mask = softc->blk_gran - 1; 2222 } else { 2223 softc->blk_mask = 0; 2224 } 2225 } 2226 2227 if (write_protect) 2228 softc->flags |= SA_FLAG_TAPE_WP; 2229 2230 if (comp_supported) { 2231 if (softc->saved_comp_algorithm == 0) 2232 softc->saved_comp_algorithm = 2233 softc->comp_algorithm; 2234 softc->flags |= SA_FLAG_COMP_SUPP; 2235 if (comp_enabled) 2236 softc->flags |= SA_FLAG_COMP_ENABLED; 2237 } else 2238 softc->flags |= SA_FLAG_COMP_UNSUPP; 2239 2240 if ((softc->buffer_mode == SMH_SA_BUF_MODE_NOBUF) && 2241 (softc->quirks & SA_QUIRK_NO_MODESEL) == 0) { 2242 error = sasetparams(periph, SA_PARAM_BUFF_MODE, 0, 2243 0, 0, SF_NO_PRINT); 2244 if (error == 0) { 2245 softc->buffer_mode = SMH_SA_BUF_MODE_SIBUF; 2246 } else { 2247 xpt_print(periph->path, 2248 "unable to set buffered mode\n"); 2249 } 2250 error = 0; /* not an error */ 2251 } 2252 2253 2254 if (error == 0) { 2255 softc->flags |= SA_FLAG_TAPE_MOUNTED; 2256 } 2257 exit: 2258 if (rblim != NULL) 2259 kfree(rblim, M_SCSISA); 2260 2261 if (error != 0) { 2262 softc->dsreg = MTIO_DSREG_NIL; 2263 } else { 2264 softc->fileno = softc->blkno = 0; 2265 softc->dsreg = MTIO_DSREG_REST; 2266 } 2267 #ifdef SA_1FM_AT_EOD 2268 if ((softc->quirks & SA_QUIRK_2FM) == 0) 2269 softc->quirks |= SA_QUIRK_1FM; 2270 #else 2271 if ((softc->quirks & SA_QUIRK_1FM) == 0) 2272 softc->quirks |= SA_QUIRK_2FM; 2273 #endif 2274 } else 2275 xpt_release_ccb(ccb); 2276 2277 /* 2278 * If we return an error, we're not mounted any more, 2279 * so release any device reservation. 2280 */ 2281 if (error != 0) { 2282 sareservereleaseunit(periph, FALSE); 2283 } else { 2284 /* 2285 * Clear I/O residual. 2286 */ 2287 softc->last_io_resid = 0; 2288 softc->last_ctl_resid = 0; 2289 } 2290 return (error); 2291 } 2292 2293 /* 2294 * How many filemarks do we need to write if we were to terminate the 2295 * tape session right now? Note that this can be a negative number 2296 */ 2297 2298 static int 2299 samarkswanted(struct cam_periph *periph) 2300 { 2301 int markswanted; 2302 struct sa_softc *softc; 2303 2304 softc = (struct sa_softc *)periph->softc; 2305 markswanted = 0; 2306 if ((softc->flags & SA_FLAG_TAPE_WRITTEN) != 0) { 2307 markswanted++; 2308 if (softc->quirks & SA_QUIRK_2FM) 2309 markswanted++; 2310 } 2311 markswanted -= softc->filemarks; 2312 return (markswanted); 2313 } 2314 2315 static int 2316 sacheckeod(struct cam_periph *periph) 2317 { 2318 int error; 2319 int markswanted; 2320 2321 markswanted = samarkswanted(periph); 2322 2323 if (markswanted > 0) { 2324 error = sawritefilemarks(periph, markswanted, FALSE); 2325 } else { 2326 error = 0; 2327 } 2328 return (error); 2329 } 2330 2331 static int 2332 saerror(union ccb *ccb, u_int32_t cflgs, u_int32_t sflgs) 2333 { 2334 static const char *toobig = 2335 "%d-byte tape record bigger than supplied buffer\n"; 2336 struct cam_periph *periph; 2337 struct sa_softc *softc; 2338 struct ccb_scsiio *csio; 2339 struct scsi_sense_data *sense; 2340 u_int32_t resid = 0; 2341 int32_t info = 0; 2342 cam_status status; 2343 int error_code, sense_key, asc, ascq, error, aqvalid; 2344 2345 periph = xpt_path_periph(ccb->ccb_h.path); 2346 softc = (struct sa_softc *)periph->softc; 2347 csio = &ccb->csio; 2348 sense = &csio->sense_data; 2349 scsi_extract_sense(sense, &error_code, &sense_key, &asc, &ascq); 2350 aqvalid = sense->extra_len >= 6; 2351 error = 0; 2352 2353 status = csio->ccb_h.status & CAM_STATUS_MASK; 2354 2355 /* 2356 * Calculate/latch up, any residuals... We do this in a funny 2-step 2357 * so we can print stuff here if we have CAM_DEBUG enabled for this 2358 * unit. 2359 */ 2360 if (status == CAM_SCSI_STATUS_ERROR) { 2361 if ((sense->error_code & SSD_ERRCODE_VALID) != 0) { 2362 info = (int32_t) scsi_4btoul(sense->info); 2363 resid = info; 2364 if ((softc->flags & SA_FLAG_FIXED) != 0) 2365 resid *= softc->media_blksize; 2366 } else { 2367 resid = csio->dxfer_len; 2368 info = resid; 2369 if ((softc->flags & SA_FLAG_FIXED) != 0) { 2370 if (softc->media_blksize) 2371 info /= softc->media_blksize; 2372 } 2373 } 2374 if (CCB_Type(csio) == SA_CCB_BUFFER_IO) { 2375 bcopy((caddr_t) sense, (caddr_t) &softc->last_io_sense, 2376 sizeof (struct scsi_sense_data)); 2377 bcopy(csio->cdb_io.cdb_bytes, softc->last_io_cdb, 2378 (int) csio->cdb_len); 2379 softc->last_io_resid = resid; 2380 softc->last_resid_was_io = 1; 2381 } else { 2382 bcopy((caddr_t) sense, (caddr_t) &softc->last_ctl_sense, 2383 sizeof (struct scsi_sense_data)); 2384 bcopy(csio->cdb_io.cdb_bytes, softc->last_ctl_cdb, 2385 (int) csio->cdb_len); 2386 softc->last_ctl_resid = resid; 2387 softc->last_resid_was_io = 0; 2388 } 2389 CAM_DEBUG(periph->path, CAM_DEBUG_INFO, ("CDB[0]=0x%x Key 0x%x " 2390 "ASC/ASCQ 0x%x/0x%x CAM STATUS 0x%x flags 0x%x resid %d " 2391 "dxfer_len %d\n", csio->cdb_io.cdb_bytes[0] & 0xff, 2392 sense_key, asc, ascq, status, 2393 sense->flags & ~SSD_KEY_RESERVED, resid, csio->dxfer_len)); 2394 } else { 2395 CAM_DEBUG(periph->path, CAM_DEBUG_INFO, 2396 ("Cam Status 0x%x\n", status)); 2397 } 2398 2399 switch (status) { 2400 case CAM_REQ_CMP: 2401 return (0); 2402 case CAM_SCSI_STATUS_ERROR: 2403 /* 2404 * If a read/write command, we handle it here. 2405 */ 2406 if (CCB_Type(csio) != SA_CCB_WAITING) { 2407 break; 2408 } 2409 /* 2410 * If this was just EOM/EOP, Filemark, Setmark or ILI detected 2411 * on a non read/write command, we assume it's not an error 2412 * and propagate the residule and return. 2413 */ 2414 if ((aqvalid && asc == 0 && ascq > 0 && ascq <= 5) || 2415 (aqvalid == 0 && sense_key == SSD_KEY_NO_SENSE)) { 2416 csio->resid = resid; 2417 QFRLS(ccb); 2418 return (0); 2419 } 2420 /* 2421 * Otherwise, we let the common code handle this. 2422 */ 2423 return (cam_periph_error(ccb, cflgs, sflgs, &softc->saved_ccb)); 2424 2425 /* 2426 * XXX: To Be Fixed 2427 * We cannot depend upon CAM honoring retry counts for these. 2428 */ 2429 case CAM_SCSI_BUS_RESET: 2430 case CAM_BDR_SENT: 2431 if (ccb->ccb_h.retry_count <= 0) { 2432 return (EIO); 2433 } 2434 /* FALLTHROUGH */ 2435 default: 2436 return (cam_periph_error(ccb, cflgs, sflgs, &softc->saved_ccb)); 2437 } 2438 2439 /* 2440 * Handle filemark, end of tape, mismatched record sizes.... 2441 * From this point out, we're only handling read/write cases. 2442 * Handle writes && reads differently. 2443 */ 2444 2445 if (csio->cdb_io.cdb_bytes[0] == SA_WRITE) { 2446 if (sense_key == SSD_KEY_VOLUME_OVERFLOW) { 2447 csio->resid = resid; 2448 error = ENOSPC; 2449 } else if (sense->flags & SSD_EOM) { 2450 softc->flags |= SA_FLAG_EOM_PENDING; 2451 /* 2452 * Grotesque as it seems, the few times 2453 * I've actually seen a non-zero resid, 2454 * the tape drive actually lied and had 2455 * written all the data!. 2456 */ 2457 csio->resid = 0; 2458 } 2459 } else { 2460 csio->resid = resid; 2461 if (sense_key == SSD_KEY_BLANK_CHECK) { 2462 if (softc->quirks & SA_QUIRK_1FM) { 2463 error = 0; 2464 softc->flags |= SA_FLAG_EOM_PENDING; 2465 } else { 2466 error = EIO; 2467 } 2468 } else if (sense->flags & SSD_FILEMARK) { 2469 if (softc->flags & SA_FLAG_FIXED) { 2470 error = -1; 2471 softc->flags |= SA_FLAG_EOF_PENDING; 2472 } 2473 /* 2474 * Unconditionally, if we detected a filemark on a read, 2475 * mark that we've run moved a file ahead. 2476 */ 2477 if (softc->fileno != (daddr_t) -1) { 2478 softc->fileno++; 2479 softc->blkno = 0; 2480 csio->ccb_h.ccb_pflags |= SA_POSITION_UPDATED; 2481 } 2482 } 2483 } 2484 2485 /* 2486 * Incorrect Length usually applies to read, but can apply to writes. 2487 */ 2488 if (error == 0 && (sense->flags & SSD_ILI)) { 2489 if (info < 0) { 2490 xpt_print(csio->ccb_h.path, toobig, 2491 csio->dxfer_len - info); 2492 csio->resid = csio->dxfer_len; 2493 error = EIO; 2494 } else { 2495 csio->resid = resid; 2496 if (softc->flags & SA_FLAG_FIXED) { 2497 softc->flags |= SA_FLAG_EIO_PENDING; 2498 } 2499 /* 2500 * Bump the block number if we hadn't seen a filemark. 2501 * Do this independent of errors (we've moved anyway). 2502 */ 2503 if ((sense->flags & SSD_FILEMARK) == 0) { 2504 if (softc->blkno != (daddr_t) -1) { 2505 softc->blkno++; 2506 csio->ccb_h.ccb_pflags |= 2507 SA_POSITION_UPDATED; 2508 } 2509 } 2510 } 2511 } 2512 2513 if (error <= 0) { 2514 /* 2515 * Unfreeze the queue if frozen as we're not returning anything 2516 * to our waiters that would indicate an I/O error has occurred 2517 * (yet). 2518 */ 2519 QFRLS(ccb); 2520 error = 0; 2521 } 2522 return (error); 2523 } 2524 2525 static int 2526 sagetparams(struct cam_periph *periph, sa_params params_to_get, 2527 u_int32_t *blocksize, u_int8_t *density, u_int32_t *numblocks, 2528 int *buff_mode, u_int8_t *write_protect, u_int8_t *speed, 2529 int *comp_supported, int *comp_enabled, u_int32_t *comp_algorithm, 2530 sa_comp_t *tcs) 2531 { 2532 union ccb *ccb; 2533 void *mode_buffer; 2534 struct scsi_mode_header_6 *mode_hdr; 2535 struct scsi_mode_blk_desc *mode_blk; 2536 int mode_buffer_len; 2537 struct sa_softc *softc; 2538 u_int8_t cpage; 2539 int error; 2540 cam_status status; 2541 2542 softc = (struct sa_softc *)periph->softc; 2543 ccb = cam_periph_getccb(periph, 1); 2544 if (softc->quirks & SA_QUIRK_NO_CPAGE) 2545 cpage = SA_DEVICE_CONFIGURATION_PAGE; 2546 else 2547 cpage = SA_DATA_COMPRESSION_PAGE; 2548 2549 retry: 2550 mode_buffer_len = sizeof(*mode_hdr) + sizeof(*mode_blk); 2551 2552 if (params_to_get & SA_PARAM_COMPRESSION) { 2553 if (softc->quirks & SA_QUIRK_NOCOMP) { 2554 *comp_supported = FALSE; 2555 params_to_get &= ~SA_PARAM_COMPRESSION; 2556 } else 2557 mode_buffer_len += sizeof (sa_comp_t); 2558 } 2559 2560 mode_buffer = kmalloc(mode_buffer_len, M_SCSISA, M_INTWAIT | M_ZERO); 2561 mode_hdr = (struct scsi_mode_header_6 *)mode_buffer; 2562 mode_blk = (struct scsi_mode_blk_desc *)&mode_hdr[1]; 2563 2564 /* it is safe to retry this */ 2565 scsi_mode_sense(&ccb->csio, 5, sadone, MSG_SIMPLE_Q_TAG, FALSE, 2566 SMS_PAGE_CTRL_CURRENT, (params_to_get & SA_PARAM_COMPRESSION) ? 2567 cpage : SMS_VENDOR_SPECIFIC_PAGE, mode_buffer, mode_buffer_len, 2568 SSD_FULL_SIZE, SCSIOP_TIMEOUT); 2569 2570 error = cam_periph_runccb(ccb, saerror, 0, SF_NO_PRINT, 2571 &softc->device_stats); 2572 QFRLS(ccb); 2573 2574 status = ccb->ccb_h.status & CAM_STATUS_MASK; 2575 2576 if (error == EINVAL && (params_to_get & SA_PARAM_COMPRESSION) != 0) { 2577 /* 2578 * Hmm. Let's see if we can try another page... 2579 * If we've already done that, give up on compression 2580 * for this device and remember this for the future 2581 * and attempt the request without asking for compression 2582 * info. 2583 */ 2584 if (cpage == SA_DATA_COMPRESSION_PAGE) { 2585 cpage = SA_DEVICE_CONFIGURATION_PAGE; 2586 goto retry; 2587 } 2588 softc->quirks |= SA_QUIRK_NOCOMP; 2589 kfree(mode_buffer, M_SCSISA); 2590 goto retry; 2591 } else if (status == CAM_SCSI_STATUS_ERROR) { 2592 /* Tell the user about the fatal error. */ 2593 scsi_sense_print(&ccb->csio); 2594 goto sagetparamsexit; 2595 } 2596 2597 /* 2598 * If the user only wants the compression information, and 2599 * the device doesn't send back the block descriptor, it's 2600 * no big deal. If the user wants more than just 2601 * compression, though, and the device doesn't pass back the 2602 * block descriptor, we need to send another mode sense to 2603 * get the block descriptor. 2604 */ 2605 if ((mode_hdr->blk_desc_len == 0) && 2606 (params_to_get & SA_PARAM_COMPRESSION) && 2607 (params_to_get & ~(SA_PARAM_COMPRESSION))) { 2608 2609 /* 2610 * Decrease the mode buffer length by the size of 2611 * the compression page, to make sure the data 2612 * there doesn't get overwritten. 2613 */ 2614 mode_buffer_len -= sizeof (sa_comp_t); 2615 2616 /* 2617 * Now move the compression page that we presumably 2618 * got back down the memory chunk a little bit so 2619 * it doesn't get spammed. 2620 */ 2621 bcopy(&mode_hdr[0], &mode_hdr[1], sizeof (sa_comp_t)); 2622 bzero(&mode_hdr[0], sizeof (mode_hdr[0])); 2623 2624 /* 2625 * Now, we issue another mode sense and just ask 2626 * for the block descriptor, etc. 2627 */ 2628 2629 scsi_mode_sense(&ccb->csio, 2, sadone, MSG_SIMPLE_Q_TAG, FALSE, 2630 SMS_PAGE_CTRL_CURRENT, SMS_VENDOR_SPECIFIC_PAGE, 2631 mode_buffer, mode_buffer_len, SSD_FULL_SIZE, 2632 SCSIOP_TIMEOUT); 2633 2634 error = cam_periph_runccb(ccb, saerror, 0, SF_NO_PRINT, 2635 &softc->device_stats); 2636 QFRLS(ccb); 2637 2638 if (error != 0) 2639 goto sagetparamsexit; 2640 } 2641 2642 if (params_to_get & SA_PARAM_BLOCKSIZE) 2643 *blocksize = scsi_3btoul(mode_blk->blklen); 2644 2645 if (params_to_get & SA_PARAM_NUMBLOCKS) 2646 *numblocks = scsi_3btoul(mode_blk->nblocks); 2647 2648 if (params_to_get & SA_PARAM_BUFF_MODE) 2649 *buff_mode = mode_hdr->dev_spec & SMH_SA_BUF_MODE_MASK; 2650 2651 if (params_to_get & SA_PARAM_DENSITY) 2652 *density = mode_blk->density; 2653 2654 if (params_to_get & SA_PARAM_WP) 2655 *write_protect = (mode_hdr->dev_spec & SMH_SA_WP)? TRUE : FALSE; 2656 2657 if (params_to_get & SA_PARAM_SPEED) 2658 *speed = mode_hdr->dev_spec & SMH_SA_SPEED_MASK; 2659 2660 if (params_to_get & SA_PARAM_COMPRESSION) { 2661 sa_comp_t *ntcs = (sa_comp_t *) &mode_blk[1]; 2662 if (cpage == SA_DATA_COMPRESSION_PAGE) { 2663 struct scsi_data_compression_page *cp = &ntcs->dcomp; 2664 *comp_supported = 2665 (cp->dce_and_dcc & SA_DCP_DCC)? TRUE : FALSE; 2666 *comp_enabled = 2667 (cp->dce_and_dcc & SA_DCP_DCE)? TRUE : FALSE; 2668 *comp_algorithm = scsi_4btoul(cp->comp_algorithm); 2669 } else { 2670 struct scsi_dev_conf_page *cp = &ntcs->dconf; 2671 /* 2672 * We don't really know whether this device supports 2673 * Data Compression if the the algorithm field is 2674 * zero. Just say we do. 2675 */ 2676 *comp_supported = TRUE; 2677 *comp_enabled = 2678 (cp->sel_comp_alg != SA_COMP_NONE)? TRUE : FALSE; 2679 *comp_algorithm = cp->sel_comp_alg; 2680 } 2681 if (tcs != NULL) 2682 bcopy(ntcs, tcs, sizeof (sa_comp_t)); 2683 } 2684 2685 if (CAM_DEBUGGED(periph->path, CAM_DEBUG_INFO)) { 2686 int idx; 2687 char *xyz = mode_buffer; 2688 xpt_print_path(periph->path); 2689 kprintf("Mode Sense Data="); 2690 for (idx = 0; idx < mode_buffer_len; idx++) 2691 kprintf(" 0x%02x", xyz[idx] & 0xff); 2692 kprintf("\n"); 2693 } 2694 2695 sagetparamsexit: 2696 2697 xpt_release_ccb(ccb); 2698 kfree(mode_buffer, M_SCSISA); 2699 return (error); 2700 } 2701 2702 /* 2703 * The purpose of this function is to set one of four different parameters 2704 * for a tape drive: 2705 * - blocksize 2706 * - density 2707 * - compression / compression algorithm 2708 * - buffering mode 2709 * 2710 * The assumption is that this will be called from saioctl(), and therefore 2711 * from a process context. Thus the waiting malloc calls below. If that 2712 * assumption ever changes, the malloc calls should be changed to be 2713 * NOWAIT mallocs. 2714 * 2715 * Any or all of the four parameters may be set when this function is 2716 * called. It should handle setting more than one parameter at once. 2717 */ 2718 static int 2719 sasetparams(struct cam_periph *periph, sa_params params_to_set, 2720 u_int32_t blocksize, u_int8_t density, u_int32_t calg, 2721 u_int32_t sense_flags) 2722 { 2723 struct sa_softc *softc; 2724 u_int32_t current_blocksize = 0;/* silence gcc */ 2725 u_int32_t current_calg; 2726 u_int8_t current_density = 0; /* silence gcc */ 2727 u_int8_t current_speed = 0; /* silence gcc */ 2728 int comp_enabled, comp_supported; 2729 void *mode_buffer; 2730 int mode_buffer_len; 2731 struct scsi_mode_header_6 *mode_hdr; 2732 struct scsi_mode_blk_desc *mode_blk; 2733 sa_comp_t *ccomp, *cpage; 2734 int buff_mode; 2735 union ccb *ccb = NULL; 2736 int error; 2737 2738 softc = (struct sa_softc *)periph->softc; 2739 2740 ccomp = kmalloc(sizeof (sa_comp_t), M_SCSISA, M_INTWAIT); 2741 2742 /* 2743 * Since it doesn't make sense to set the number of blocks, or 2744 * write protection, we won't try to get the current value. We 2745 * always want to get the blocksize, so we can set it back to the 2746 * proper value. 2747 */ 2748 error = sagetparams(periph, 2749 params_to_set | SA_PARAM_BLOCKSIZE | SA_PARAM_SPEED, 2750 ¤t_blocksize, ¤t_density, NULL, &buff_mode, NULL, 2751 ¤t_speed, &comp_supported, &comp_enabled, 2752 ¤t_calg, ccomp); 2753 2754 if (error != 0) { 2755 kfree(ccomp, M_SCSISA); 2756 return (error); 2757 } 2758 2759 mode_buffer_len = sizeof(*mode_hdr) + sizeof(*mode_blk); 2760 if (params_to_set & SA_PARAM_COMPRESSION) 2761 mode_buffer_len += sizeof (sa_comp_t); 2762 2763 mode_buffer = kmalloc(mode_buffer_len, M_SCSISA, M_INTWAIT | M_ZERO); 2764 2765 mode_hdr = (struct scsi_mode_header_6 *)mode_buffer; 2766 mode_blk = (struct scsi_mode_blk_desc *)&mode_hdr[1]; 2767 2768 ccb = cam_periph_getccb(periph, 1); 2769 2770 retry: 2771 2772 if (params_to_set & SA_PARAM_COMPRESSION) { 2773 if (mode_blk) { 2774 cpage = (sa_comp_t *)&mode_blk[1]; 2775 } else { 2776 cpage = (sa_comp_t *)&mode_hdr[1]; 2777 } 2778 bcopy(ccomp, cpage, sizeof (sa_comp_t)); 2779 cpage->hdr.pagecode &= ~0x80; 2780 } else 2781 cpage = NULL; 2782 2783 /* 2784 * If the caller wants us to set the blocksize, use the one they 2785 * pass in. Otherwise, use the blocksize we got back from the 2786 * mode select above. 2787 */ 2788 if (mode_blk) { 2789 if (params_to_set & SA_PARAM_BLOCKSIZE) 2790 scsi_ulto3b(blocksize, mode_blk->blklen); 2791 else 2792 scsi_ulto3b(current_blocksize, mode_blk->blklen); 2793 2794 /* 2795 * Set density if requested, else preserve old density. 2796 * SCSI_SAME_DENSITY only applies to SCSI-2 or better 2797 * devices, else density we've latched up in our softc. 2798 */ 2799 if (params_to_set & SA_PARAM_DENSITY) { 2800 mode_blk->density = density; 2801 } else if (softc->scsi_rev > SCSI_REV_CCS) { 2802 mode_blk->density = SCSI_SAME_DENSITY; 2803 } else { 2804 mode_blk->density = softc->media_density; 2805 } 2806 } 2807 2808 /* 2809 * For mode selects, these two fields must be zero. 2810 */ 2811 mode_hdr->data_length = 0; 2812 mode_hdr->medium_type = 0; 2813 2814 /* set the speed to the current value */ 2815 mode_hdr->dev_spec = current_speed; 2816 2817 /* if set, set single-initiator buffering mode */ 2818 if (softc->buffer_mode == SMH_SA_BUF_MODE_SIBUF) { 2819 mode_hdr->dev_spec |= SMH_SA_BUF_MODE_SIBUF; 2820 } 2821 2822 if (mode_blk) 2823 mode_hdr->blk_desc_len = sizeof(struct scsi_mode_blk_desc); 2824 else 2825 mode_hdr->blk_desc_len = 0; 2826 2827 /* 2828 * First, if the user wants us to set the compression algorithm or 2829 * just turn compression on, check to make sure that this drive 2830 * supports compression. 2831 */ 2832 if (params_to_set & SA_PARAM_COMPRESSION) { 2833 /* 2834 * If the compression algorithm is 0, disable compression. 2835 * If the compression algorithm is non-zero, enable 2836 * compression and set the compression type to the 2837 * specified compression algorithm, unless the algorithm is 2838 * MT_COMP_ENABLE. In that case, we look at the 2839 * compression algorithm that is currently set and if it is 2840 * non-zero, we leave it as-is. If it is zero, and we have 2841 * saved a compression algorithm from a time when 2842 * compression was enabled before, set the compression to 2843 * the saved value. 2844 */ 2845 switch (ccomp->hdr.pagecode & ~0x80) { 2846 case SA_DEVICE_CONFIGURATION_PAGE: 2847 { 2848 struct scsi_dev_conf_page *dcp = &cpage->dconf; 2849 if (calg == 0) { 2850 dcp->sel_comp_alg = SA_COMP_NONE; 2851 break; 2852 } 2853 if (calg != MT_COMP_ENABLE) { 2854 dcp->sel_comp_alg = calg; 2855 } else if (dcp->sel_comp_alg == SA_COMP_NONE && 2856 softc->saved_comp_algorithm != 0) { 2857 dcp->sel_comp_alg = softc->saved_comp_algorithm; 2858 } 2859 break; 2860 } 2861 case SA_DATA_COMPRESSION_PAGE: 2862 if (ccomp->dcomp.dce_and_dcc & SA_DCP_DCC) { 2863 struct scsi_data_compression_page *dcp = &cpage->dcomp; 2864 if (calg == 0) { 2865 /* 2866 * Disable compression, but leave the 2867 * decompression and the capability bit 2868 * alone. 2869 */ 2870 dcp->dce_and_dcc = SA_DCP_DCC; 2871 dcp->dde_and_red |= SA_DCP_DDE; 2872 break; 2873 } 2874 /* enable compression && decompression */ 2875 dcp->dce_and_dcc = SA_DCP_DCE | SA_DCP_DCC; 2876 dcp->dde_and_red |= SA_DCP_DDE; 2877 /* 2878 * If there, use compression algorithm from caller. 2879 * Otherwise, if there's a saved compression algorithm 2880 * and there is no current algorithm, use the saved 2881 * algorithm. Else parrot back what we got and hope 2882 * for the best. 2883 */ 2884 if (calg != MT_COMP_ENABLE) { 2885 scsi_ulto4b(calg, dcp->comp_algorithm); 2886 scsi_ulto4b(calg, dcp->decomp_algorithm); 2887 } else if (scsi_4btoul(dcp->comp_algorithm) == 0 && 2888 softc->saved_comp_algorithm != 0) { 2889 scsi_ulto4b(softc->saved_comp_algorithm, 2890 dcp->comp_algorithm); 2891 scsi_ulto4b(softc->saved_comp_algorithm, 2892 dcp->decomp_algorithm); 2893 } 2894 break; 2895 } 2896 /* 2897 * Compression does not appear to be supported- 2898 * at least via the DATA COMPRESSION page. It 2899 * would be too much to ask us to believe that 2900 * the page itself is supported, but incorrectly 2901 * reports an ability to manipulate data compression, 2902 * so we'll assume that this device doesn't support 2903 * compression. We can just fall through for that. 2904 */ 2905 /* FALLTHROUGH */ 2906 default: 2907 /* 2908 * The drive doesn't seem to support compression, 2909 * so turn off the set compression bit. 2910 */ 2911 params_to_set &= ~SA_PARAM_COMPRESSION; 2912 xpt_print(periph->path, 2913 "device does not seem to support compression\n"); 2914 2915 /* 2916 * If that was the only thing the user wanted us to set, 2917 * clean up allocated resources and return with 2918 * 'operation not supported'. 2919 */ 2920 if (params_to_set == SA_PARAM_NONE) { 2921 kfree(mode_buffer, M_SCSISA); 2922 xpt_release_ccb(ccb); 2923 return (ENODEV); 2924 } 2925 2926 /* 2927 * That wasn't the only thing the user wanted us to set. 2928 * So, decrease the stated mode buffer length by the 2929 * size of the compression mode page. 2930 */ 2931 mode_buffer_len -= sizeof(sa_comp_t); 2932 } 2933 } 2934 2935 /* It is safe to retry this operation */ 2936 scsi_mode_select(&ccb->csio, 5, sadone, MSG_SIMPLE_Q_TAG, 2937 (params_to_set & SA_PARAM_COMPRESSION)? TRUE : FALSE, 2938 FALSE, mode_buffer, mode_buffer_len, SSD_FULL_SIZE, SCSIOP_TIMEOUT); 2939 2940 error = cam_periph_runccb(ccb, saerror, 0, 2941 sense_flags, &softc->device_stats); 2942 QFRLS(ccb); 2943 2944 if (CAM_DEBUGGED(periph->path, CAM_DEBUG_INFO)) { 2945 int idx; 2946 char *xyz = mode_buffer; 2947 xpt_print_path(periph->path); 2948 kprintf("Err%d, Mode Select Data=", error); 2949 for (idx = 0; idx < mode_buffer_len; idx++) 2950 kprintf(" 0x%02x", xyz[idx] & 0xff); 2951 kprintf("\n"); 2952 } 2953 2954 2955 if (error) { 2956 /* 2957 * If we can, try without setting density/blocksize. 2958 */ 2959 if (mode_blk) { 2960 if ((params_to_set & 2961 (SA_PARAM_DENSITY|SA_PARAM_BLOCKSIZE)) == 0) { 2962 mode_blk = NULL; 2963 goto retry; 2964 } 2965 } else { 2966 mode_blk = (struct scsi_mode_blk_desc *)&mode_hdr[1]; 2967 cpage = (sa_comp_t *)&mode_blk[1]; 2968 } 2969 2970 /* 2971 * If we were setting the blocksize, and that failed, we 2972 * want to set it to its original value. If we weren't 2973 * setting the blocksize, we don't want to change it. 2974 */ 2975 scsi_ulto3b(current_blocksize, mode_blk->blklen); 2976 2977 /* 2978 * Set density if requested, else preserve old density. 2979 * SCSI_SAME_DENSITY only applies to SCSI-2 or better 2980 * devices, else density we've latched up in our softc. 2981 */ 2982 if (params_to_set & SA_PARAM_DENSITY) { 2983 mode_blk->density = current_density; 2984 } else if (softc->scsi_rev > SCSI_REV_CCS) { 2985 mode_blk->density = SCSI_SAME_DENSITY; 2986 } else { 2987 mode_blk->density = softc->media_density; 2988 } 2989 2990 if (params_to_set & SA_PARAM_COMPRESSION) 2991 bcopy(ccomp, cpage, sizeof (sa_comp_t)); 2992 2993 /* 2994 * The retry count is the only CCB field that might have been 2995 * changed that we care about, so reset it back to 1. 2996 */ 2997 ccb->ccb_h.retry_count = 1; 2998 cam_periph_runccb(ccb, saerror, 0, sense_flags, 2999 &softc->device_stats); 3000 QFRLS(ccb); 3001 } 3002 3003 xpt_release_ccb(ccb); 3004 3005 if (ccomp != NULL) 3006 kfree(ccomp, M_SCSISA); 3007 3008 if (params_to_set & SA_PARAM_COMPRESSION) { 3009 if (error) { 3010 softc->flags &= ~SA_FLAG_COMP_ENABLED; 3011 /* 3012 * Even if we get an error setting compression, 3013 * do not say that we don't support it. We could 3014 * have been wrong, or it may be media specific. 3015 * softc->flags &= ~SA_FLAG_COMP_SUPP; 3016 */ 3017 softc->saved_comp_algorithm = softc->comp_algorithm; 3018 softc->comp_algorithm = 0; 3019 } else { 3020 softc->flags |= SA_FLAG_COMP_ENABLED; 3021 softc->comp_algorithm = calg; 3022 } 3023 } 3024 3025 kfree(mode_buffer, M_SCSISA); 3026 return (error); 3027 } 3028 3029 static void 3030 saprevent(struct cam_periph *periph, int action) 3031 { 3032 struct sa_softc *softc; 3033 union ccb *ccb; 3034 int error, sf; 3035 3036 softc = (struct sa_softc *)periph->softc; 3037 3038 if ((action == PR_ALLOW) && (softc->flags & SA_FLAG_TAPE_LOCKED) == 0) 3039 return; 3040 if ((action == PR_PREVENT) && (softc->flags & SA_FLAG_TAPE_LOCKED) != 0) 3041 return; 3042 3043 /* 3044 * We can be quiet about illegal requests. 3045 */ 3046 if (CAM_DEBUGGED(periph->path, CAM_DEBUG_INFO)) { 3047 sf = 0; 3048 } else 3049 sf = SF_QUIET_IR; 3050 3051 ccb = cam_periph_getccb(periph, 1); 3052 3053 /* It is safe to retry this operation */ 3054 scsi_prevent(&ccb->csio, 5, sadone, MSG_SIMPLE_Q_TAG, action, 3055 SSD_FULL_SIZE, SCSIOP_TIMEOUT); 3056 3057 error = cam_periph_runccb(ccb, saerror, 0, sf, &softc->device_stats); 3058 QFRLS(ccb); 3059 if (error == 0) { 3060 if (action == PR_ALLOW) 3061 softc->flags &= ~SA_FLAG_TAPE_LOCKED; 3062 else 3063 softc->flags |= SA_FLAG_TAPE_LOCKED; 3064 } 3065 3066 xpt_release_ccb(ccb); 3067 } 3068 3069 static int 3070 sarewind(struct cam_periph *periph) 3071 { 3072 union ccb *ccb; 3073 struct sa_softc *softc; 3074 int error; 3075 3076 softc = (struct sa_softc *)periph->softc; 3077 3078 ccb = cam_periph_getccb(periph, 1); 3079 3080 /* It is safe to retry this operation */ 3081 scsi_rewind(&ccb->csio, 2, sadone, MSG_SIMPLE_Q_TAG, FALSE, 3082 SSD_FULL_SIZE, REWIND_TIMEOUT); 3083 3084 softc->dsreg = MTIO_DSREG_REW; 3085 error = cam_periph_runccb(ccb, saerror, 0, 0, &softc->device_stats); 3086 softc->dsreg = MTIO_DSREG_REST; 3087 3088 if ((ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) 3089 cam_release_devq(ccb->ccb_h.path, 0, 0, 0, FALSE); 3090 3091 xpt_release_ccb(ccb); 3092 if (error == 0) 3093 softc->fileno = softc->blkno = (daddr_t) 0; 3094 else 3095 softc->fileno = softc->blkno = (daddr_t) -1; 3096 return (error); 3097 } 3098 3099 static int 3100 saspace(struct cam_periph *periph, int count, scsi_space_code code) 3101 { 3102 union ccb *ccb; 3103 struct sa_softc *softc; 3104 int error; 3105 3106 softc = (struct sa_softc *)periph->softc; 3107 3108 ccb = cam_periph_getccb(periph, 1); 3109 3110 /* This cannot be retried */ 3111 3112 scsi_space(&ccb->csio, 0, sadone, MSG_SIMPLE_Q_TAG, code, count, 3113 SSD_FULL_SIZE, SPACE_TIMEOUT); 3114 3115 /* 3116 * Clear residual because we will be using it. 3117 */ 3118 softc->last_ctl_resid = 0; 3119 3120 softc->dsreg = (count < 0)? MTIO_DSREG_REV : MTIO_DSREG_FWD; 3121 error = cam_periph_runccb(ccb, saerror, 0, 0, &softc->device_stats); 3122 softc->dsreg = MTIO_DSREG_REST; 3123 3124 if ((ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) 3125 cam_release_devq(ccb->ccb_h.path, 0, 0, 0, FALSE); 3126 3127 xpt_release_ccb(ccb); 3128 3129 /* 3130 * If a spacing operation has failed, we need to invalidate 3131 * this mount. 3132 * 3133 * If the spacing operation was setmarks or to end of recorded data, 3134 * we no longer know our relative position. 3135 * 3136 * If the spacing operations was spacing files in reverse, we 3137 * take account of the residual, but still check against less 3138 * than zero- if we've gone negative, we must have hit BOT. 3139 * 3140 * If the spacing operations was spacing records in reverse and 3141 * we have a residual, we've either hit BOT or hit a filemark. 3142 * In the former case, we know our new record number (0). In 3143 * the latter case, we have absolutely no idea what the real 3144 * record number is- we've stopped between the end of the last 3145 * record in the previous file and the filemark that stopped 3146 * our spacing backwards. 3147 */ 3148 if (error) { 3149 softc->fileno = softc->blkno = (daddr_t) -1; 3150 } else if (code == SS_SETMARKS || code == SS_EOD) { 3151 softc->fileno = softc->blkno = (daddr_t) -1; 3152 } else if (code == SS_FILEMARKS && softc->fileno != (daddr_t) -1) { 3153 softc->fileno += (count - softc->last_ctl_resid); 3154 if (softc->fileno < 0) /* we must of hit BOT */ 3155 softc->fileno = 0; 3156 softc->blkno = 0; 3157 } else if (code == SS_BLOCKS && softc->blkno != (daddr_t) -1) { 3158 softc->blkno += (count - softc->last_ctl_resid); 3159 if (count < 0) { 3160 if (softc->last_ctl_resid || softc->blkno < 0) { 3161 if (softc->fileno == 0) { 3162 softc->blkno = 0; 3163 } else { 3164 softc->blkno = (daddr_t) -1; 3165 } 3166 } 3167 } 3168 } 3169 return (error); 3170 } 3171 3172 static int 3173 sawritefilemarks(struct cam_periph *periph, int nmarks, int setmarks) 3174 { 3175 union ccb *ccb; 3176 struct sa_softc *softc; 3177 int error, nwm = 0; 3178 3179 softc = (struct sa_softc *)periph->softc; 3180 if (softc->open_rdonly) 3181 return (EBADF); 3182 3183 ccb = cam_periph_getccb(periph, 1); 3184 /* 3185 * Clear residual because we will be using it. 3186 */ 3187 softc->last_ctl_resid = 0; 3188 3189 softc->dsreg = MTIO_DSREG_FMK; 3190 /* this *must* not be retried */ 3191 scsi_write_filemarks(&ccb->csio, 0, sadone, MSG_SIMPLE_Q_TAG, 3192 FALSE, setmarks, nmarks, SSD_FULL_SIZE, IO_TIMEOUT); 3193 softc->dsreg = MTIO_DSREG_REST; 3194 3195 3196 error = cam_periph_runccb(ccb, saerror, 0, 0, &softc->device_stats); 3197 3198 if ((ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) 3199 cam_release_devq(ccb->ccb_h.path, 0, 0, 0, FALSE); 3200 3201 if (error == 0 && nmarks) { 3202 struct sa_softc *softc = (struct sa_softc *)periph->softc; 3203 nwm = nmarks - softc->last_ctl_resid; 3204 softc->filemarks += nwm; 3205 } 3206 3207 xpt_release_ccb(ccb); 3208 3209 /* 3210 * Update relative positions (if we're doing that). 3211 */ 3212 if (error) { 3213 softc->fileno = softc->blkno = (daddr_t) -1; 3214 } else if (softc->fileno != (daddr_t) -1) { 3215 softc->fileno += nwm; 3216 softc->blkno = 0; 3217 } 3218 return (error); 3219 } 3220 3221 static int 3222 sardpos(struct cam_periph *periph, int hard, u_int32_t *blkptr) 3223 { 3224 struct scsi_tape_position_data loc; 3225 union ccb *ccb; 3226 struct sa_softc *softc = (struct sa_softc *)periph->softc; 3227 int error; 3228 3229 /* 3230 * We try and flush any buffered writes here if we were writing 3231 * and we're trying to get hardware block position. It eats 3232 * up performance substantially, but I'm wary of drive firmware. 3233 * 3234 * I think that *logical* block position is probably okay- 3235 * but hardware block position might have to wait for data 3236 * to hit media to be valid. Caveat Emptor. 3237 */ 3238 3239 if (hard && (softc->flags & SA_FLAG_TAPE_WRITTEN)) { 3240 error = sawritefilemarks(periph, 0, 0); 3241 if (error && error != EACCES) 3242 return (error); 3243 } 3244 3245 ccb = cam_periph_getccb(periph, 1); 3246 scsi_read_position(&ccb->csio, 1, sadone, MSG_SIMPLE_Q_TAG, 3247 hard, &loc, SSD_FULL_SIZE, SCSIOP_TIMEOUT); 3248 softc->dsreg = MTIO_DSREG_RBSY; 3249 error = cam_periph_runccb(ccb, saerror, 0, 0, &softc->device_stats); 3250 softc->dsreg = MTIO_DSREG_REST; 3251 if ((ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) 3252 cam_release_devq(ccb->ccb_h.path, 0, 0, 0, 0); 3253 3254 if (error == 0) { 3255 if (loc.flags & SA_RPOS_UNCERTAIN) { 3256 error = EINVAL; /* nothing is certain */ 3257 } else { 3258 *blkptr = scsi_4btoul(loc.firstblk); 3259 } 3260 } 3261 3262 xpt_release_ccb(ccb); 3263 return (error); 3264 } 3265 3266 static int 3267 sasetpos(struct cam_periph *periph, int hard, u_int32_t *blkptr) 3268 { 3269 union ccb *ccb; 3270 struct sa_softc *softc; 3271 int error; 3272 3273 /* 3274 * We used to try and flush any buffered writes here. 3275 * Now we push this onto user applications to either 3276 * flush the pending writes themselves (via a zero count 3277 * WRITE FILEMARKS command) or they can trust their tape 3278 * drive to do this correctly for them. 3279 */ 3280 3281 softc = (struct sa_softc *)periph->softc; 3282 ccb = cam_periph_getccb(periph, 1); 3283 3284 3285 scsi_set_position(&ccb->csio, 1, sadone, MSG_SIMPLE_Q_TAG, 3286 hard, *blkptr, SSD_FULL_SIZE, SPACE_TIMEOUT); 3287 3288 3289 softc->dsreg = MTIO_DSREG_POS; 3290 error = cam_periph_runccb(ccb, saerror, 0, 0, &softc->device_stats); 3291 softc->dsreg = MTIO_DSREG_REST; 3292 if ((ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) 3293 cam_release_devq(ccb->ccb_h.path, 0, 0, 0, 0); 3294 xpt_release_ccb(ccb); 3295 /* 3296 * Note relative file && block number position as now unknown. 3297 */ 3298 softc->fileno = softc->blkno = (daddr_t) -1; 3299 return (error); 3300 } 3301 3302 static int 3303 saretension(struct cam_periph *periph) 3304 { 3305 union ccb *ccb; 3306 struct sa_softc *softc; 3307 int error; 3308 3309 softc = (struct sa_softc *)periph->softc; 3310 3311 ccb = cam_periph_getccb(periph, 1); 3312 3313 /* It is safe to retry this operation */ 3314 scsi_load_unload(&ccb->csio, 5, sadone, MSG_SIMPLE_Q_TAG, FALSE, 3315 FALSE, TRUE, TRUE, SSD_FULL_SIZE, ERASE_TIMEOUT); 3316 3317 softc->dsreg = MTIO_DSREG_TEN; 3318 error = cam_periph_runccb(ccb, saerror, 0, 0, &softc->device_stats); 3319 softc->dsreg = MTIO_DSREG_REST; 3320 3321 if ((ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) 3322 cam_release_devq(ccb->ccb_h.path, 0, 0, 0, FALSE); 3323 xpt_release_ccb(ccb); 3324 if (error == 0) 3325 softc->fileno = softc->blkno = (daddr_t) 0; 3326 else 3327 softc->fileno = softc->blkno = (daddr_t) -1; 3328 return (error); 3329 } 3330 3331 static int 3332 sareservereleaseunit(struct cam_periph *periph, int reserve) 3333 { 3334 union ccb *ccb; 3335 struct sa_softc *softc; 3336 int error; 3337 3338 softc = (struct sa_softc *)periph->softc; 3339 ccb = cam_periph_getccb(periph, 1); 3340 3341 /* It is safe to retry this operation */ 3342 scsi_reserve_release_unit(&ccb->csio, 2, sadone, MSG_SIMPLE_Q_TAG, 3343 FALSE, 0, SSD_FULL_SIZE, SCSIOP_TIMEOUT, reserve); 3344 softc->dsreg = MTIO_DSREG_RBSY; 3345 error = cam_periph_runccb(ccb, saerror, 0, 3346 SF_RETRY_UA | SF_NO_PRINT, &softc->device_stats); 3347 softc->dsreg = MTIO_DSREG_REST; 3348 QFRLS(ccb); 3349 xpt_release_ccb(ccb); 3350 3351 /* 3352 * If the error was Illegal Request, then the device doesn't support 3353 * RESERVE/RELEASE. This is not an error. 3354 */ 3355 if (error == EINVAL) { 3356 error = 0; 3357 } 3358 3359 return (error); 3360 } 3361 3362 static int 3363 saloadunload(struct cam_periph *periph, int load) 3364 { 3365 union ccb *ccb; 3366 struct sa_softc *softc; 3367 int error; 3368 3369 softc = (struct sa_softc *)periph->softc; 3370 3371 ccb = cam_periph_getccb(periph, 1); 3372 3373 /* It is safe to retry this operation */ 3374 scsi_load_unload(&ccb->csio, 5, sadone, MSG_SIMPLE_Q_TAG, FALSE, 3375 FALSE, FALSE, load, SSD_FULL_SIZE, REWIND_TIMEOUT); 3376 3377 softc->dsreg = (load)? MTIO_DSREG_LD : MTIO_DSREG_UNL; 3378 error = cam_periph_runccb(ccb, saerror, 0, 0, &softc->device_stats); 3379 softc->dsreg = MTIO_DSREG_REST; 3380 QFRLS(ccb); 3381 xpt_release_ccb(ccb); 3382 3383 if (error || load == 0) 3384 softc->fileno = softc->blkno = (daddr_t) -1; 3385 else if (error == 0) 3386 softc->fileno = softc->blkno = (daddr_t) 0; 3387 return (error); 3388 } 3389 3390 static int 3391 saerase(struct cam_periph *periph, int longerase) 3392 { 3393 3394 union ccb *ccb; 3395 struct sa_softc *softc; 3396 int error; 3397 3398 softc = (struct sa_softc *)periph->softc; 3399 if (softc->open_rdonly) 3400 return (EBADF); 3401 3402 ccb = cam_periph_getccb(periph, 1); 3403 3404 scsi_erase(&ccb->csio, 1, sadone, MSG_SIMPLE_Q_TAG, FALSE, longerase, 3405 SSD_FULL_SIZE, ERASE_TIMEOUT); 3406 3407 softc->dsreg = MTIO_DSREG_ZER; 3408 error = cam_periph_runccb(ccb, saerror, 0, 0, &softc->device_stats); 3409 softc->dsreg = MTIO_DSREG_REST; 3410 3411 if ((ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) 3412 cam_release_devq(ccb->ccb_h.path, 0, 0, 0, FALSE); 3413 xpt_release_ccb(ccb); 3414 return (error); 3415 } 3416 3417 #endif /* _KERNEL */ 3418 3419 /* 3420 * Read tape block limits command. 3421 */ 3422 void 3423 scsi_read_block_limits(struct ccb_scsiio *csio, u_int32_t retries, 3424 void (*cbfcnp)(struct cam_periph *, union ccb *), 3425 u_int8_t tag_action, 3426 struct scsi_read_block_limits_data *rlimit_buf, 3427 u_int8_t sense_len, u_int32_t timeout) 3428 { 3429 struct scsi_read_block_limits *scsi_cmd; 3430 3431 cam_fill_csio(csio, retries, cbfcnp, CAM_DIR_IN, tag_action, 3432 (u_int8_t *)rlimit_buf, sizeof(*rlimit_buf), sense_len, 3433 sizeof(*scsi_cmd), timeout); 3434 3435 scsi_cmd = (struct scsi_read_block_limits *)&csio->cdb_io.cdb_bytes; 3436 bzero(scsi_cmd, sizeof(*scsi_cmd)); 3437 scsi_cmd->opcode = READ_BLOCK_LIMITS; 3438 } 3439 3440 void 3441 scsi_sa_read_write(struct ccb_scsiio *csio, u_int32_t retries, 3442 void (*cbfcnp)(struct cam_periph *, union ccb *), 3443 u_int8_t tag_action, int readop, int sli, 3444 int fixed, u_int32_t length, u_int8_t *data_ptr, 3445 u_int32_t dxfer_len, u_int8_t sense_len, u_int32_t timeout) 3446 { 3447 struct scsi_sa_rw *scsi_cmd; 3448 3449 scsi_cmd = (struct scsi_sa_rw *)&csio->cdb_io.cdb_bytes; 3450 scsi_cmd->opcode = readop ? SA_READ : SA_WRITE; 3451 scsi_cmd->sli_fixed = 0; 3452 if (sli && readop) 3453 scsi_cmd->sli_fixed |= SAR_SLI; 3454 if (fixed) 3455 scsi_cmd->sli_fixed |= SARW_FIXED; 3456 scsi_ulto3b(length, scsi_cmd->length); 3457 scsi_cmd->control = 0; 3458 3459 cam_fill_csio(csio, retries, cbfcnp, readop ? CAM_DIR_IN : CAM_DIR_OUT, 3460 tag_action, data_ptr, dxfer_len, sense_len, 3461 sizeof(*scsi_cmd), timeout); 3462 } 3463 3464 void 3465 scsi_load_unload(struct ccb_scsiio *csio, u_int32_t retries, 3466 void (*cbfcnp)(struct cam_periph *, union ccb *), 3467 u_int8_t tag_action, int immediate, int eot, 3468 int reten, int load, u_int8_t sense_len, 3469 u_int32_t timeout) 3470 { 3471 struct scsi_load_unload *scsi_cmd; 3472 3473 scsi_cmd = (struct scsi_load_unload *)&csio->cdb_io.cdb_bytes; 3474 bzero(scsi_cmd, sizeof(*scsi_cmd)); 3475 scsi_cmd->opcode = LOAD_UNLOAD; 3476 if (immediate) 3477 scsi_cmd->immediate = SLU_IMMED; 3478 if (eot) 3479 scsi_cmd->eot_reten_load |= SLU_EOT; 3480 if (reten) 3481 scsi_cmd->eot_reten_load |= SLU_RETEN; 3482 if (load) 3483 scsi_cmd->eot_reten_load |= SLU_LOAD; 3484 3485 cam_fill_csio(csio, retries, cbfcnp, CAM_DIR_NONE, tag_action, 3486 NULL, 0, sense_len, sizeof(*scsi_cmd), timeout); 3487 } 3488 3489 void 3490 scsi_rewind(struct ccb_scsiio *csio, u_int32_t retries, 3491 void (*cbfcnp)(struct cam_periph *, union ccb *), 3492 u_int8_t tag_action, int immediate, u_int8_t sense_len, 3493 u_int32_t timeout) 3494 { 3495 struct scsi_rewind *scsi_cmd; 3496 3497 scsi_cmd = (struct scsi_rewind *)&csio->cdb_io.cdb_bytes; 3498 bzero(scsi_cmd, sizeof(*scsi_cmd)); 3499 scsi_cmd->opcode = REWIND; 3500 if (immediate) 3501 scsi_cmd->immediate = SREW_IMMED; 3502 3503 cam_fill_csio(csio, retries, cbfcnp, CAM_DIR_NONE, tag_action, NULL, 3504 0, sense_len, sizeof(*scsi_cmd), timeout); 3505 } 3506 3507 void 3508 scsi_space(struct ccb_scsiio *csio, u_int32_t retries, 3509 void (*cbfcnp)(struct cam_periph *, union ccb *), 3510 u_int8_t tag_action, scsi_space_code code, 3511 u_int32_t count, u_int8_t sense_len, u_int32_t timeout) 3512 { 3513 struct scsi_space *scsi_cmd; 3514 3515 scsi_cmd = (struct scsi_space *)&csio->cdb_io.cdb_bytes; 3516 scsi_cmd->opcode = SPACE; 3517 scsi_cmd->code = code; 3518 scsi_ulto3b(count, scsi_cmd->count); 3519 scsi_cmd->control = 0; 3520 3521 cam_fill_csio(csio, retries, cbfcnp, CAM_DIR_NONE, tag_action, NULL, 3522 0, sense_len, sizeof(*scsi_cmd), timeout); 3523 } 3524 3525 void 3526 scsi_write_filemarks(struct ccb_scsiio *csio, u_int32_t retries, 3527 void (*cbfcnp)(struct cam_periph *, union ccb *), 3528 u_int8_t tag_action, int immediate, int setmark, 3529 u_int32_t num_marks, u_int8_t sense_len, 3530 u_int32_t timeout) 3531 { 3532 struct scsi_write_filemarks *scsi_cmd; 3533 3534 scsi_cmd = (struct scsi_write_filemarks *)&csio->cdb_io.cdb_bytes; 3535 bzero(scsi_cmd, sizeof(*scsi_cmd)); 3536 scsi_cmd->opcode = WRITE_FILEMARKS; 3537 if (immediate) 3538 scsi_cmd->byte2 |= SWFMRK_IMMED; 3539 if (setmark) 3540 scsi_cmd->byte2 |= SWFMRK_WSMK; 3541 3542 scsi_ulto3b(num_marks, scsi_cmd->num_marks); 3543 3544 cam_fill_csio(csio, retries, cbfcnp, CAM_DIR_NONE, tag_action, NULL, 3545 0, sense_len, sizeof(*scsi_cmd), timeout); 3546 } 3547 3548 /* 3549 * The reserve and release unit commands differ only by their opcodes. 3550 */ 3551 void 3552 scsi_reserve_release_unit(struct ccb_scsiio *csio, u_int32_t retries, 3553 void (*cbfcnp)(struct cam_periph *, union ccb *), 3554 u_int8_t tag_action, int third_party, 3555 int third_party_id, u_int8_t sense_len, 3556 u_int32_t timeout, int reserve) 3557 { 3558 struct scsi_reserve_release_unit *scsi_cmd; 3559 3560 scsi_cmd = (struct scsi_reserve_release_unit *)&csio->cdb_io.cdb_bytes; 3561 bzero(scsi_cmd, sizeof(*scsi_cmd)); 3562 3563 if (reserve) 3564 scsi_cmd->opcode = RESERVE_UNIT; 3565 else 3566 scsi_cmd->opcode = RELEASE_UNIT; 3567 3568 if (third_party) { 3569 scsi_cmd->lun_thirdparty |= SRRU_3RD_PARTY; 3570 scsi_cmd->lun_thirdparty |= 3571 ((third_party_id << SRRU_3RD_SHAMT) & SRRU_3RD_MASK); 3572 } 3573 3574 cam_fill_csio(csio, retries, cbfcnp, CAM_DIR_NONE, tag_action, NULL, 3575 0, sense_len, sizeof(*scsi_cmd), timeout); 3576 } 3577 3578 void 3579 scsi_erase(struct ccb_scsiio *csio, u_int32_t retries, 3580 void (*cbfcnp)(struct cam_periph *, union ccb *), 3581 u_int8_t tag_action, int immediate, int long_erase, 3582 u_int8_t sense_len, u_int32_t timeout) 3583 { 3584 struct scsi_erase *scsi_cmd; 3585 3586 scsi_cmd = (struct scsi_erase *)&csio->cdb_io.cdb_bytes; 3587 bzero(scsi_cmd, sizeof(*scsi_cmd)); 3588 3589 scsi_cmd->opcode = ERASE; 3590 3591 if (immediate) 3592 scsi_cmd->lun_imm_long |= SE_IMMED; 3593 3594 if (long_erase) 3595 scsi_cmd->lun_imm_long |= SE_LONG; 3596 3597 cam_fill_csio(csio, retries, cbfcnp, CAM_DIR_NONE, tag_action, NULL, 3598 0, sense_len, sizeof(*scsi_cmd), timeout); 3599 } 3600 3601 /* 3602 * Read Tape Position command. 3603 */ 3604 void 3605 scsi_read_position(struct ccb_scsiio *csio, u_int32_t retries, 3606 void (*cbfcnp)(struct cam_periph *, union ccb *), 3607 u_int8_t tag_action, int hardsoft, 3608 struct scsi_tape_position_data *sbp, 3609 u_int8_t sense_len, u_int32_t timeout) 3610 { 3611 struct scsi_tape_read_position *scmd; 3612 3613 cam_fill_csio(csio, retries, cbfcnp, CAM_DIR_IN, tag_action, 3614 (u_int8_t *)sbp, sizeof (*sbp), sense_len, sizeof(*scmd), timeout); 3615 scmd = (struct scsi_tape_read_position *)&csio->cdb_io.cdb_bytes; 3616 bzero(scmd, sizeof(*scmd)); 3617 scmd->opcode = READ_POSITION; 3618 scmd->byte1 = hardsoft; 3619 } 3620 3621 /* 3622 * Set Tape Position command. 3623 */ 3624 void 3625 scsi_set_position(struct ccb_scsiio *csio, u_int32_t retries, 3626 void (*cbfcnp)(struct cam_periph *, union ccb *), 3627 u_int8_t tag_action, int hardsoft, u_int32_t blkno, 3628 u_int8_t sense_len, u_int32_t timeout) 3629 { 3630 struct scsi_tape_locate *scmd; 3631 3632 cam_fill_csio(csio, retries, cbfcnp, CAM_DIR_NONE, tag_action, 3633 NULL, 0, sense_len, sizeof(*scmd), timeout); 3634 scmd = (struct scsi_tape_locate *)&csio->cdb_io.cdb_bytes; 3635 bzero(scmd, sizeof(*scmd)); 3636 scmd->opcode = LOCATE; 3637 if (hardsoft) 3638 scmd->byte1 |= SA_SPOS_BT; 3639 scsi_ulto4b(blkno, scmd->blkaddr); 3640 } 3641