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