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