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