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