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