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