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