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