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