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