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