1 /* $OpenBSD: st.c,v 1.177 2020/02/20 16:26:02 krw Exp $ */ 2 /* $NetBSD: st.c,v 1.71 1997/02/21 23:03:49 thorpej Exp $ */ 3 4 /* 5 * Copyright (c) 1994 Charles Hannum. All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 3. All advertising materials mentioning features or use of this software 16 * must display the following acknowledgement: 17 * This product includes software developed by Charles Hannum. 18 * 4. The name of the author may not be used to endorse or promote products 19 * derived from this software without specific prior written permission. 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 22 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 23 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 24 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 25 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 26 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 30 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 31 */ 32 33 /* 34 * Originally written by Julian Elischer (julian@tfs.com) 35 * for TRW Financial Systems for use under the MACH(2.5) operating system. 36 * 37 * TRW Financial Systems, in accordance with their agreement with Carnegie 38 * Mellon University, makes this software available to CMU to distribute 39 * or use in any manner that they see fit as long as this message is kept with 40 * the software. For this reason TFS also grants any other persons or 41 * organisations permission to use or modify this software. 42 * 43 * TFS supplies this software to be publicly redistributed 44 * on the understanding that TFS is not responsible for the correct 45 * functioning of this software in any circumstances. 46 * 47 * Ported to run under 386BSD by Julian Elischer (julian@tfs.com) Sept 1992 48 * major changes by Julian Elischer (julian@jules.dialix.oz.au) May 1993 49 */ 50 51 /* 52 * To do: 53 * work out some better way of guessing what a good timeout is going 54 * to be depending on whether we expect to retension or not. 55 */ 56 57 #include <sys/param.h> 58 #include <sys/systm.h> 59 #include <sys/timeout.h> 60 #include <sys/fcntl.h> 61 #include <sys/errno.h> 62 #include <sys/ioctl.h> 63 #include <sys/stat.h> 64 #include <sys/pool.h> 65 #include <sys/buf.h> 66 #include <sys/mtio.h> 67 #include <sys/device.h> 68 #include <sys/conf.h> 69 #include <sys/vnode.h> 70 71 #include <scsi/scsi_all.h> 72 #include <scsi/scsi_tape.h> 73 #include <scsi/scsiconf.h> 74 75 /* Defines for device specific stuff */ 76 #define DEF_FIXED_BSIZE 512 77 78 #define STMODE(z) ( minor(z) & 0x03) 79 #define STUNIT(z) ((minor(z) >> 4) ) 80 81 #define STMINOR(unit, mode) (((unit) << 4) + (mode)) 82 #define MAXSTMODES 16 /* Old max retained so minor's don't change. */ 83 84 #define ST_IO_TIME (3 * 60 * 1000) /* 3 minutes */ 85 #define ST_CTL_TIME (30 * 1000) /* 30 seconds */ 86 #define ST_SPC_TIME (4 * 60 * 60 * 1000) /* 4 hours */ 87 88 /* 89 * Maximum density code allowed in SCSI spec (SSC2R08f, Section 8.3). 90 */ 91 #define SCSI_MAX_DENSITY_CODE 0xff 92 93 /* 94 * Define various devices that we know mis-behave in some way, 95 * and note how they are bad, so we can correct for them 96 */ 97 struct mode { 98 int blksize; 99 u_int8_t density; 100 }; 101 102 struct quirkdata { 103 u_int quirks; 104 #define ST_Q_SENSE_HELP 0x0001 /* must do READ for good MODE SENSE */ 105 #define ST_Q_IGNORE_LOADS 0x0002 106 #define ST_Q_UNIMODAL 0x0004 /* unimode drive rejects mode select */ 107 struct mode mode; 108 }; 109 110 struct st_quirk_inquiry_pattern { 111 struct scsi_inquiry_pattern pattern; 112 struct quirkdata quirkdata; 113 }; 114 115 const struct st_quirk_inquiry_pattern st_quirk_patterns[] = { 116 {{T_SEQUENTIAL, T_REMOV, 117 " ", " ", " "}, {0, 118 {512, 0}}}, 119 {{T_SEQUENTIAL, T_REMOV, 120 "TANDBERG", " TDC 3800 ", ""}, {0, 121 {512, 0}}}, 122 {{T_SEQUENTIAL, T_REMOV, 123 "ARCHIVE ", "VIPER 2525 25462", ""}, {ST_Q_SENSE_HELP, 124 {0, 0}}}, 125 {{T_SEQUENTIAL, T_REMOV, 126 "SANKYO ", "CP525 ", ""}, {0, 127 {512, 0}}}, 128 {{T_SEQUENTIAL, T_REMOV, 129 "ANRITSU ", "DMT780 ", ""}, {0, 130 {512, 0}}}, 131 {{T_SEQUENTIAL, T_REMOV, 132 "ARCHIVE ", "VIPER 150 21531", ""}, {ST_Q_SENSE_HELP, 133 {0, 0}}}, 134 {{T_SEQUENTIAL, T_REMOV, 135 "WANGTEK ", "5099ES SCSI", ""}, {0, 136 {512, 0}}}, 137 {{T_SEQUENTIAL, T_REMOV, 138 "WANGTEK ", "5150ES SCSI", ""}, {0, 139 {512, 0}}}, 140 {{T_SEQUENTIAL, T_REMOV, 141 "HP ", "T4000s ", ""}, {ST_Q_UNIMODAL, 142 {0, QIC_3095}}}, 143 {{T_SEQUENTIAL, T_REMOV, 144 "WANGTEK ", "5150ES SCSI FA15", "01 A"}, {ST_Q_IGNORE_LOADS, 145 {0, 0}}}, 146 {{T_SEQUENTIAL, T_REMOV, 147 "TEAC ", "MT-2ST/N50 ", ""}, {ST_Q_IGNORE_LOADS, 148 {0, 0}}}, 149 }; 150 151 #define NOEJECT 0 152 #define EJECT 1 153 154 #define NOREWIND 0 155 #define DOREWIND 1 156 157 struct st_softc { 158 struct device sc_dev; 159 160 int flags; 161 #define ST_INFO_VALID 0x00000001 162 #define ST_WRITTEN 0x00000004 163 #define ST_FIXEDBLOCKS 0x00000008 164 #define ST_AT_FILEMARK 0x00000010 165 #define ST_EIO_PENDING 0x00000020 166 #define ST_EOM_PENDING 0x00000040 167 #define ST_EOD_DETECTED 0x00000080 168 #define ST_FM_WRITTEN 0x00000100 169 #define ST_BLANK_READ 0x00000200 170 #define ST_2FM_AT_EOD 0x00000400 171 #define ST_MOUNTED 0x00000800 172 #define ST_DONTBUFFER 0x00001000 173 #define ST_WAITING 0x00002000 174 #define ST_DYING 0x00004000 175 #define ST_BOD_DETECTED 0x00008000 176 #define ST_MODE_DENSITY 0x00010000 177 #define ST_MODE_BLKSIZE 0x00040000 178 179 u_int quirks; /* quirks for the open mode */ 180 int blksize; /* blksize we are using */ 181 u_int8_t density; /* present density */ 182 short mt_resid; /* last (short) resid */ 183 short mt_erreg; /* last error (sense key) seen */ 184 185 struct scsi_link *sc_link; /* our link to the adpter etc. */ 186 187 int blkmin; /* min blk size */ 188 int blkmax; /* max blk size */ 189 190 u_int32_t media_blksize; /* 0 if not ST_FIXEDBLOCKS */ 191 u_int32_t media_density; /* this is what it said when asked */ 192 int media_fileno; /* relative to BOT. -1 means unknown. */ 193 int media_blkno; /* relative to BOF. -1 means unknown. */ 194 int media_eom; /* relative to BOT. -1 means unknown. */ 195 196 struct mode mode; 197 struct bufq sc_bufq; 198 struct timeout sc_timeout; 199 struct scsi_xshandler sc_xsh; 200 }; 201 202 203 int stmatch(struct device *, void *, void *); 204 void stattach(struct device *, struct device *, void *); 205 int stactivate(struct device *, int); 206 int stdetach(struct device *, int); 207 void stminphys(struct buf *); 208 void ststart(struct scsi_xfer *); 209 210 int st_mount_tape(struct st_softc *, int); 211 void st_unmount(struct st_softc *, int, int); 212 int st_decide_mode(struct st_softc *, int); 213 void st_buf_done(struct scsi_xfer *); 214 int st_read(struct st_softc *, char *, int, int); 215 int st_read_block_limits(struct st_softc *, int); 216 int st_mode_sense(struct st_softc *, int); 217 int st_mode_select(struct st_softc *, int); 218 int st_space(struct st_softc *, int, u_int, int); 219 int st_write_filemarks(struct st_softc *, int, int); 220 int st_check_eod(struct st_softc *, int, int *, int); 221 int st_load(struct st_softc *, u_int, int); 222 int st_rewind(struct st_softc *, u_int, int); 223 int st_interpret_sense(struct scsi_xfer *); 224 int st_touch_tape(struct st_softc *); 225 int st_erase(struct st_softc *, int, int); 226 227 struct cfattach st_ca = { 228 sizeof(struct st_softc), stmatch, stattach, 229 stdetach, stactivate 230 }; 231 232 struct cfdriver st_cd = { 233 NULL, "st", DV_TAPE 234 }; 235 236 #define ST_PER_ACTION (ST_AT_FILEMARK | ST_EIO_PENDING | ST_EOM_PENDING | \ 237 ST_BLANK_READ) 238 239 #define stlookup(unit) (struct st_softc *)device_lookup(&st_cd, (unit)) 240 241 const struct scsi_inquiry_pattern st_patterns[] = { 242 {T_SEQUENTIAL, T_REMOV, 243 "", "", ""}, 244 }; 245 246 int 247 stmatch(struct device *parent, void *match, void *aux) 248 { 249 struct scsi_attach_args *sa = aux; 250 int priority; 251 252 (void)scsi_inqmatch(sa->sa_inqbuf, 253 st_patterns, nitems(st_patterns), 254 sizeof(st_patterns[0]), &priority); 255 return priority; 256 } 257 258 /* 259 * The routine called by the low level scsi routine when it discovers 260 * a device suitable for this driver. 261 */ 262 void 263 stattach(struct device *parent, struct device *self, void *aux) 264 { 265 const struct st_quirk_inquiry_pattern *finger; 266 struct st_softc *st = (void *)self; 267 struct scsi_attach_args *sa = aux; 268 struct scsi_link *link = sa->sa_sc_link; 269 int priority; 270 271 SC_DEBUG(link, SDEV_DB2, ("stattach:\n")); 272 273 /* 274 * Store information needed to contact our base driver 275 */ 276 st->sc_link = link; 277 link->interpret_sense = st_interpret_sense; 278 link->device_softc = st; 279 280 /* Get any quirks and mode information. */ 281 finger = (const struct st_quirk_inquiry_pattern *)scsi_inqmatch( 282 &link->inqdata, 283 st_quirk_patterns, 284 nitems(st_quirk_patterns), 285 sizeof(st_quirk_patterns[0]), &priority); 286 if (finger != NULL) { 287 st->quirks = finger->quirkdata.quirks; 288 st->mode = finger->quirkdata.mode; 289 CLR(st->flags, ST_MODE_BLKSIZE | ST_MODE_DENSITY); 290 if (st->mode.blksize != 0) 291 SET(st->flags, ST_MODE_BLKSIZE); 292 if (st->mode.density != 0) 293 SET(st->flags, ST_MODE_DENSITY); 294 } 295 printf("\n"); 296 297 scsi_xsh_set(&st->sc_xsh, link, ststart); 298 timeout_set(&st->sc_timeout, (void (*)(void *))scsi_xsh_set, 299 &st->sc_xsh); 300 301 /* Set up the buf queue for this device. */ 302 bufq_init(&st->sc_bufq, BUFQ_FIFO); 303 304 /* Start up with media position unknown. */ 305 st->media_fileno = -1; 306 st->media_blkno = -1; 307 st->media_eom = -1; 308 309 /* 310 * Reset the media loaded flag, sometimes the data 311 * acquired at boot time is not quite accurate. This 312 * will be checked again at the first open. 313 */ 314 CLR(link->flags, SDEV_MEDIA_LOADED); 315 } 316 317 int 318 stactivate(struct device *self, int act) 319 { 320 struct st_softc *st = (struct st_softc *)self; 321 322 switch (act) { 323 case DVACT_DEACTIVATE: 324 SET(st->flags, ST_DYING); 325 scsi_xsh_del(&st->sc_xsh); 326 break; 327 } 328 329 return 0; 330 } 331 332 int 333 stdetach(struct device *self, int flags) 334 { 335 struct st_softc *st = (struct st_softc *)self; 336 int cmaj, mn; 337 338 bufq_drain(&st->sc_bufq); 339 340 /* Locate the lowest minor number to be detached. */ 341 mn = STMINOR(self->dv_unit, 0); 342 343 for (cmaj = 0; cmaj < nchrdev; cmaj++) 344 if (cdevsw[cmaj].d_open == stopen) 345 vdevgone(cmaj, mn, mn + MAXSTMODES - 1, VCHR); 346 347 bufq_destroy(&st->sc_bufq); 348 349 return 0; 350 } 351 352 /* 353 * open the device. 354 */ 355 int 356 stopen(dev_t dev, int flags, int fmt, struct proc *p) 357 { 358 struct scsi_link *link; 359 struct st_softc *st; 360 int error = 0; 361 362 st = stlookup(STUNIT(dev)); 363 if (st == NULL) 364 return ENXIO; 365 if (ISSET(st->flags, ST_DYING)) { 366 error = ENXIO; 367 goto done; 368 } 369 link = st->sc_link; 370 371 if (ISSET(flags, FWRITE) && ISSET(link->flags, SDEV_READONLY)) { 372 error = EACCES; 373 goto done; 374 } 375 376 SC_DEBUG(link, SDEV_DB1, ("open: dev=0x%x (unit %d (of %d))\n", dev, 377 STUNIT(dev), st_cd.cd_ndevs)); 378 379 /* 380 * Tape is an exclusive media. Only one open at a time. 381 */ 382 if (ISSET(link->flags, SDEV_OPEN)) { 383 SC_DEBUG(link, SDEV_DB4, ("already open\n")); 384 error = EBUSY; 385 goto done; 386 } 387 388 /* Use st_interpret_sense() now. */ 389 SET(link->flags, SDEV_OPEN); 390 391 /* 392 * Check the unit status. This clears any outstanding errors and 393 * will ensure that media is present. 394 */ 395 error = scsi_test_unit_ready(link, TEST_READY_RETRIES, 396 SCSI_SILENT | SCSI_IGNORE_MEDIA_CHANGE | 397 SCSI_IGNORE_ILLEGAL_REQUEST); 398 399 /* 400 * Terminate any exising mount session if there is no media. 401 */ 402 if (!ISSET(link->flags, SDEV_MEDIA_LOADED)) 403 st_unmount(st, NOEJECT, DOREWIND); 404 405 if (error != 0) { 406 CLR(link->flags, SDEV_OPEN); 407 goto done; 408 } 409 410 error = st_mount_tape(st, flags); 411 if (error != 0) { 412 CLR(link->flags, SDEV_OPEN); 413 goto done; 414 } 415 416 /* 417 * Make sure that a tape opened in write-only mode will have 418 * file marks written on it when closed, even if not written to. 419 * This is for SUN compatibility 420 */ 421 if ((flags & O_ACCMODE) == FWRITE) 422 SET(st->flags, ST_WRITTEN); 423 424 done: 425 SC_DEBUG(link, SDEV_DB2, ("open complete\n")); 426 device_unref(&st->sc_dev); 427 return error; 428 } 429 430 /* 431 * close the device.. only called if we are the LAST 432 * occurrence of an open device 433 */ 434 int 435 stclose(dev_t dev, int flags, int mode, struct proc *p) 436 { 437 struct scsi_link *link; 438 struct st_softc *st; 439 int error = 0; 440 441 st = stlookup(STUNIT(dev)); 442 if (st == NULL) 443 return ENXIO; 444 if (ISSET(st->flags, ST_DYING)) { 445 error = ENXIO; 446 goto done; 447 } 448 link = st->sc_link; 449 450 SC_DEBUG(link, SDEV_DB1, ("closing\n")); 451 452 if (ISSET(st->flags, ST_WRITTEN) && !ISSET(st->flags, ST_FM_WRITTEN)) 453 st_write_filemarks(st, 1, 0); 454 455 switch (STMODE(dev)) { 456 case 0: /* /dev/rstN */ 457 st_unmount(st, NOEJECT, DOREWIND); 458 break; 459 case 1: /* /dev/nrstN */ 460 st_unmount(st, NOEJECT, NOREWIND); 461 break; 462 case 2: /* /dev/erstN */ 463 st_unmount(st, EJECT, DOREWIND); 464 break; 465 case 3: /* /dev/enrstN */ 466 st_unmount(st, EJECT, NOREWIND); 467 break; 468 } 469 CLR(link->flags, SDEV_OPEN); 470 timeout_del(&st->sc_timeout); 471 scsi_xsh_del(&st->sc_xsh); 472 473 done: 474 device_unref(&st->sc_dev); 475 return error; 476 } 477 478 /* 479 * Start a new mount session if needed. 480 */ 481 int 482 st_mount_tape(struct st_softc *st, int flags) 483 { 484 struct scsi_link *link = st->sc_link; 485 int error = 0; 486 487 if (ISSET(st->flags, ST_MOUNTED)) 488 return 0; 489 490 SC_DEBUG(link, SDEV_DB1, ("mounting\n")); 491 492 /* 493 * Assume the media is new and give it a chance to 494 * to do a 'load' instruction. 495 */ 496 if ((error = st_load(st, LD_LOAD, 0)) != 0) 497 goto done; 498 499 /* 500 * Throw another dummy instruction to catch 501 * 'Unit attention' errors. Some drives appear to give 502 * these after doing a Load instruction. 503 * (noteably some DAT drives) 504 */ 505 /* XXX */ 506 scsi_test_unit_ready(link, TEST_READY_RETRIES, SCSI_SILENT); 507 508 /* 509 * Some devices can't tell you much until they have been 510 * asked to look at the media. This quirk does this. 511 */ 512 if (ISSET(st->quirks, ST_Q_SENSE_HELP)) 513 if ((error = st_touch_tape(st)) != 0) 514 return error; 515 /* 516 * Load the physical device parameters 517 * loads: blkmin, blkmax 518 */ 519 if (!ISSET(link->flags, SDEV_ATAPI) && 520 (error = st_read_block_limits(st, 0)) != 0) 521 goto done; 522 523 /* 524 * Load the media dependent parameters 525 * includes: media_blksize,media_density 526 * As we have a tape in, it should be reflected here. 527 * If not you may need the "quirk" above. 528 */ 529 if ((error = st_mode_sense(st, 0)) != 0) 530 goto done; 531 532 /* 533 * If we have gained a permanent density from somewhere, 534 * then use it in preference to the one supplied by 535 * default by the driver. 536 */ 537 if (ISSET(st->flags, ST_MODE_DENSITY)) 538 st->density = st->mode.density; 539 else 540 st->density = st->media_density; 541 /* 542 * If we have gained a permanent blocksize 543 * then use it in preference to the one supplied by 544 * default by the driver. 545 */ 546 CLR(st->flags, ST_FIXEDBLOCKS); 547 if (ISSET(st->flags, ST_MODE_BLKSIZE)) { 548 st->blksize = st->mode.blksize; 549 if (st->blksize) 550 SET(st->flags, ST_FIXEDBLOCKS); 551 } else { 552 if ((error = st_decide_mode(st, 0)) != 0) 553 goto done; 554 } 555 if ((error = st_mode_select(st, 0)) != 0) { 556 printf("%s: cannot set selected mode\n", st->sc_dev.dv_xname); 557 goto done; 558 } 559 scsi_prevent(link, PR_PREVENT, 560 SCSI_IGNORE_ILLEGAL_REQUEST | SCSI_IGNORE_NOT_READY); 561 SET(st->flags, ST_MOUNTED); 562 SET(link->flags, SDEV_MEDIA_LOADED); /* move earlier? */ 563 st->media_fileno = 0; 564 st->media_blkno = 0; 565 st->media_eom = -1; 566 567 done: 568 return error; 569 } 570 571 /* 572 * End the present mount session. 573 * Rewind, and optionally eject the tape. 574 * Reset various flags to indicate that all new 575 * operations require another mount operation 576 */ 577 void 578 st_unmount(struct st_softc *st, int eject, int rewind) 579 { 580 struct scsi_link *link = st->sc_link; 581 int nmarks; 582 583 if (eject == NOEJECT && rewind == NOREWIND) { 584 if (ISSET(link->flags, SDEV_MEDIA_LOADED)) 585 return; 586 } 587 588 st->media_fileno = -1; 589 st->media_blkno = -1; 590 591 if (!ISSET(st->flags, ST_MOUNTED)) 592 return; 593 SC_DEBUG(link, SDEV_DB1, ("unmounting\n")); 594 st_check_eod(st, 0, &nmarks, SCSI_IGNORE_NOT_READY); 595 if (rewind == DOREWIND) 596 st_rewind(st, 0, SCSI_IGNORE_NOT_READY); 597 scsi_prevent(link, PR_ALLOW, 598 SCSI_IGNORE_ILLEGAL_REQUEST | SCSI_IGNORE_NOT_READY); 599 if (eject == EJECT) 600 st_load(st, LD_UNLOAD, SCSI_IGNORE_NOT_READY); 601 CLR(st->flags, ST_MOUNTED); 602 CLR(link->flags, SDEV_MEDIA_LOADED); 603 } 604 605 /* 606 * Given all we know about the device, media, mode, 'quirks' and 607 * initial operation, make a decision as to how we should be set 608 * to run (regarding blocking and EOD marks) 609 */ 610 int 611 st_decide_mode(struct st_softc *st, int first_read) 612 { 613 struct scsi_link *link = st->sc_link; 614 615 SC_DEBUG(link, SDEV_DB2, ("starting block mode decision\n")); 616 617 /* ATAPI tapes are always fixed blocksize. */ 618 if (ISSET(link->flags, SDEV_ATAPI)) { 619 SET(st->flags, ST_FIXEDBLOCKS); 620 if (st->media_blksize > 0) 621 st->blksize = st->media_blksize; 622 else 623 st->blksize = DEF_FIXED_BSIZE; 624 goto done; 625 } 626 627 /* 628 * If the drive can only handle fixed-length blocks and only at 629 * one size, perhaps we should just do that. 630 */ 631 if (st->blkmin && (st->blkmin == st->blkmax)) { 632 SET(st->flags, ST_FIXEDBLOCKS); 633 st->blksize = st->blkmin; 634 SC_DEBUG(link, SDEV_DB3, 635 ("blkmin == blkmax of %d\n", st->blkmin)); 636 goto done; 637 } 638 /* 639 * If the tape density mandates (or even suggests) use of fixed 640 * or variable-length blocks, comply. 641 */ 642 switch (st->density) { 643 case HALFINCH_800: 644 case HALFINCH_1600: 645 case HALFINCH_6250: 646 case DDS: 647 CLR(st->flags, ST_FIXEDBLOCKS); 648 st->blksize = 0; 649 SC_DEBUG(link, SDEV_DB3, ("density specified variable\n")); 650 goto done; 651 case QIC_11: 652 case QIC_24: 653 case QIC_120: 654 case QIC_150: 655 case QIC_525: 656 case QIC_1320: 657 SET(st->flags, ST_FIXEDBLOCKS); 658 if (st->media_blksize > 0) 659 st->blksize = st->media_blksize; 660 else 661 st->blksize = DEF_FIXED_BSIZE; 662 SC_DEBUG(link, SDEV_DB3, ("density specified fixed\n")); 663 goto done; 664 } 665 /* 666 * If we're about to read the tape, perhaps we should choose 667 * fixed or variable-length blocks and block size according to 668 * what the drive found on the tape. 669 */ 670 if (first_read) { 671 if (st->media_blksize > 0) 672 SET(st->flags, ST_FIXEDBLOCKS); 673 else 674 CLR(st->flags, ST_FIXEDBLOCKS); 675 st->blksize = st->media_blksize; 676 SC_DEBUG(link, SDEV_DB3, 677 ("Used media_blksize of %d\n", st->media_blksize)); 678 goto done; 679 } 680 /* 681 * We're getting no hints from any direction. Choose variable- 682 * length blocks arbitrarily. 683 */ 684 CLR(st->flags, ST_FIXEDBLOCKS); 685 st->blksize = 0; 686 SC_DEBUG(link, SDEV_DB3, 687 ("Give up and default to variable mode\n")); 688 689 done: 690 /* 691 * Decide whether or not to write two file marks to signify end- 692 * of-data. Make the decision as a function of density. If 693 * the decision is not to use a second file mark, the SCSI BLANK 694 * CHECK condition code will be recognized as end-of-data when 695 * first read. 696 * (I think this should be a by-product of fixed/variable..julian) 697 */ 698 switch (st->density) { 699 /* case 8 mm: What is the SCSI density code for 8 mm, anyway? */ 700 case QIC_11: 701 case QIC_24: 702 case QIC_120: 703 case QIC_150: 704 case QIC_525: 705 case QIC_1320: 706 CLR(st->flags, ST_2FM_AT_EOD); 707 break; 708 default: 709 SET(st->flags, ST_2FM_AT_EOD); 710 } 711 return 0; 712 } 713 714 /* 715 * Actually translate the requested transfer into 716 * one the physical driver can understand 717 * The transfer is described by a buf and will include 718 * only one physical transfer. 719 */ 720 void 721 ststrategy(struct buf *bp) 722 { 723 struct scsi_link *link; 724 struct st_softc *st; 725 int s; 726 727 st = stlookup(STUNIT(bp->b_dev)); 728 if (st == NULL) { 729 bp->b_error = ENXIO; 730 goto bad; 731 } 732 if (ISSET(st->flags, ST_DYING)) { 733 bp->b_error = ENXIO; 734 goto bad; 735 } 736 link = st->sc_link; 737 738 SC_DEBUG(link, SDEV_DB2, ("ststrategy: %ld bytes @ blk %lld\n", 739 bp->b_bcount, (long long)bp->b_blkno)); 740 741 /* 742 * If it's a null transfer, return immediately. 743 */ 744 if (bp->b_bcount == 0) 745 goto done; 746 /* 747 * Odd sized request on fixed drives are verboten 748 */ 749 if (ISSET(st->flags, ST_FIXEDBLOCKS)) { 750 if (bp->b_bcount % st->blksize) { 751 printf("%s: bad request, must be multiple of %d\n", 752 st->sc_dev.dv_xname, st->blksize); 753 bp->b_error = EIO; 754 goto bad; 755 } 756 } 757 /* 758 * as are out-of-range requests on variable drives. 759 */ 760 else if (bp->b_bcount < st->blkmin || 761 (st->blkmax && bp->b_bcount > st->blkmax)) { 762 printf("%s: bad request, must be between %d and %d\n", 763 st->sc_dev.dv_xname, st->blkmin, st->blkmax); 764 bp->b_error = EIO; 765 goto bad; 766 } 767 768 /* 769 * Place it in the queue of activities for this tape 770 * at the end (a bit silly because we only have on user.. 771 * (but it could fork())) 772 */ 773 bufq_queue(&st->sc_bufq, bp); 774 775 /* 776 * Tell the device to get going on the transfer if it's 777 * not doing anything, otherwise just wait for completion 778 * (All a bit silly if we're only allowing 1 open but..) 779 */ 780 scsi_xsh_add(&st->sc_xsh); 781 782 device_unref(&st->sc_dev); 783 return; 784 bad: 785 SET(bp->b_flags, B_ERROR); 786 done: 787 /* Set b_resid to indicate no xfer was done. */ 788 bp->b_resid = bp->b_bcount; 789 s = splbio(); 790 biodone(bp); 791 splx(s); 792 if (st) 793 device_unref(&st->sc_dev); 794 } 795 796 void 797 ststart(struct scsi_xfer *xs) 798 { 799 struct scsi_link *link = xs->sc_link; 800 struct st_softc *st = link->device_softc; 801 struct buf *bp; 802 struct scsi_rw_tape *cmd; 803 int s; 804 805 SC_DEBUG(link, SDEV_DB2, ("ststart\n")); 806 807 if (ISSET(st->flags, ST_DYING)) { 808 scsi_xs_put(xs); 809 return; 810 } 811 812 /* 813 * if the device has been unmounted by the user 814 * then throw away all requests until done 815 */ 816 if (!ISSET(st->flags, ST_MOUNTED) || 817 !ISSET(link->flags, SDEV_MEDIA_LOADED)) { 818 /* make sure that one implies the other.. */ 819 CLR(link->flags, SDEV_MEDIA_LOADED); 820 bufq_drain(&st->sc_bufq); 821 scsi_xs_put(xs); 822 return; 823 } 824 825 for (;;) { 826 bp = bufq_dequeue(&st->sc_bufq); 827 if (bp == NULL) { 828 scsi_xs_put(xs); 829 return; 830 } 831 832 /* 833 * Only FIXEDBLOCK devices have pending I/O or space 834 * operations. 835 */ 836 if (ISSET(st->flags, ST_FIXEDBLOCKS)) { 837 /* 838 * If we are at a filemark but have not reported it yet 839 * then we should report it now 840 */ 841 if (ISSET(st->flags, ST_AT_FILEMARK)) { 842 if ((bp->b_flags & B_READ) == B_WRITE) { 843 /* 844 * Handling of ST_AT_FILEMARK in 845 * st_space will fill in the right file 846 * mark count. 847 * Back up over filemark 848 */ 849 if (st_space(st, 0, SP_FILEMARKS, 0)) { 850 SET(bp->b_flags, B_ERROR); 851 bp->b_resid = bp->b_bcount; 852 bp->b_error = EIO; 853 s = splbio(); 854 biodone(bp); 855 splx(s); 856 continue; 857 } 858 } else { 859 bp->b_resid = bp->b_bcount; 860 bp->b_error = 0; 861 CLR(bp->b_flags, B_ERROR); 862 CLR(st->flags, ST_AT_FILEMARK); 863 s = splbio(); 864 biodone(bp); 865 splx(s); 866 continue; /* seek more work */ 867 } 868 } 869 } 870 871 /* 872 * If we are at EIO or EOM but have not reported it 873 * yet then we should report it now. 874 */ 875 if (ISSET(st->flags, ST_EOM_PENDING | ST_EIO_PENDING)) { 876 bp->b_resid = bp->b_bcount; 877 if (ISSET(st->flags, ST_EIO_PENDING)) { 878 bp->b_error = EIO; 879 SET(bp->b_flags, B_ERROR); 880 } 881 CLR(st->flags, ST_EOM_PENDING | ST_EIO_PENDING); 882 s = splbio(); 883 biodone(bp); 884 splx(s); 885 continue; /* seek more work */ 886 } 887 break; 888 } 889 890 /* 891 * Fill out the scsi command 892 */ 893 cmd = (struct scsi_rw_tape *)xs->cmd; 894 bzero(cmd, sizeof(*cmd)); 895 if ((bp->b_flags & B_READ) == B_WRITE) { 896 cmd->opcode = WRITE; 897 CLR(st->flags, ST_FM_WRITTEN); 898 SET(st->flags, ST_WRITTEN); 899 SET(xs->flags, SCSI_DATA_OUT); 900 } else { 901 cmd->opcode = READ; 902 SET(xs->flags, SCSI_DATA_IN); 903 } 904 905 /* 906 * Handle "fixed-block-mode" tape drives by using the 907 * block count instead of the length. 908 */ 909 if (ISSET(st->flags, ST_FIXEDBLOCKS)) { 910 SET(cmd->byte2, SRW_FIXED); 911 _lto3b(bp->b_bcount / st->blksize, cmd->len); 912 } else 913 _lto3b(bp->b_bcount, cmd->len); 914 915 if (st->media_blkno != -1) { 916 /* Update block count now, errors will set it to -1. */ 917 if (ISSET(st->flags, ST_FIXEDBLOCKS)) 918 st->media_blkno += _3btol(cmd->len); 919 else if (_3btol(cmd->len) != 0) 920 st->media_blkno++; 921 } 922 923 xs->cmdlen = sizeof(*cmd); 924 xs->timeout = ST_IO_TIME; 925 xs->data = bp->b_data; 926 xs->datalen = bp->b_bcount; 927 xs->done = st_buf_done; 928 xs->cookie = bp; 929 xs->bp = bp; 930 931 /* 932 * go ask the adapter to do all this for us 933 */ 934 scsi_xs_exec(xs); 935 936 /* 937 * should we try do more work now? 938 */ 939 if (ISSET(st->flags, ST_WAITING)) 940 CLR(st->flags, ST_WAITING); 941 else if (bufq_peek(&st->sc_bufq)) 942 scsi_xsh_add(&st->sc_xsh); 943 } 944 945 void 946 st_buf_done(struct scsi_xfer *xs) 947 { 948 struct buf *bp = xs->cookie; 949 int error, s; 950 951 switch (xs->error) { 952 case XS_NOERROR: 953 bp->b_error = 0; 954 CLR(bp->b_flags, B_ERROR); 955 bp->b_resid = xs->resid; 956 break; 957 958 case XS_SENSE: 959 case XS_SHORTSENSE: 960 SC_DEBUG_SENSE(xs); 961 error = st_interpret_sense(xs); 962 if (error == 0) { 963 bp->b_error = 0; 964 CLR(bp->b_flags, B_ERROR); 965 bp->b_resid = xs->resid; 966 break; 967 } 968 if (error != ERESTART) 969 xs->retries = 0; 970 goto retry; 971 972 case XS_BUSY: 973 if (xs->retries) { 974 if (scsi_delay(xs, 1) != ERESTART) 975 xs->retries = 0; 976 } 977 goto retry; 978 979 case XS_TIMEOUT: 980 retry: 981 if (xs->retries--) { 982 scsi_xs_exec(xs); 983 return; 984 } 985 /* FALLTHROUGH */ 986 987 default: 988 bp->b_error = EIO; 989 SET(bp->b_flags, B_ERROR); 990 bp->b_resid = bp->b_bcount; 991 break; 992 } 993 994 s = splbio(); 995 biodone(bp); 996 splx(s); 997 scsi_xs_put(xs); 998 } 999 1000 void 1001 stminphys(struct buf *bp) 1002 { 1003 struct scsi_link *link; 1004 struct st_softc *sc; 1005 1006 sc = stlookup(STUNIT(bp->b_dev)); 1007 if (sc == NULL) 1008 return; 1009 link = sc->sc_link; 1010 1011 if (link->adapter->dev_minphys != NULL) 1012 (*link->adapter->dev_minphys)(bp, link); 1013 else 1014 minphys(bp); 1015 1016 device_unref(&sc->sc_dev); 1017 } 1018 1019 int 1020 stread(dev_t dev, struct uio *uio, int iomode) 1021 { 1022 return physio(ststrategy, dev, B_READ, stminphys, uio); 1023 } 1024 1025 int 1026 stwrite(dev_t dev, struct uio *uio, int iomode) 1027 { 1028 return physio(ststrategy, dev, B_WRITE, stminphys, uio); 1029 } 1030 1031 /* 1032 * Perform special action on behalf of the user; 1033 * knows about the internals of this device 1034 */ 1035 int 1036 stioctl(dev_t dev, u_long cmd, caddr_t arg, int flag, struct proc *p) 1037 { 1038 int error = 0; 1039 int nmarks; 1040 int flags = 0; 1041 struct st_softc *st; 1042 int hold_blksize; 1043 u_int8_t hold_density; 1044 struct mtop *mt = (struct mtop *) arg; 1045 int number; 1046 1047 /* 1048 * Find the device that the user is talking about 1049 */ 1050 st = stlookup(STUNIT(dev)); 1051 if (st == NULL) 1052 return ENXIO; 1053 1054 if (ISSET(st->flags, ST_DYING)) { 1055 error = ENXIO; 1056 goto done; 1057 } 1058 1059 hold_blksize = st->blksize; 1060 hold_density = st->density; 1061 1062 switch (cmd) { 1063 1064 case MTIOCGET: { 1065 struct mtget *g = (struct mtget *) arg; 1066 1067 /* 1068 * (to get the current state of READONLY) 1069 */ 1070 error = st_mode_sense(st, SCSI_SILENT); 1071 if (error != 0) 1072 break; 1073 1074 SC_DEBUG(st->sc_link, SDEV_DB1, ("[ioctl: get status]\n")); 1075 bzero(g, sizeof(struct mtget)); 1076 g->mt_type = 0x7; /* Ultrix compat *//*? */ 1077 g->mt_blksiz = st->blksize; 1078 g->mt_density = st->density; 1079 g->mt_mblksiz = st->mode.blksize; 1080 g->mt_mdensity = st->mode.density; 1081 if (ISSET(st->sc_link->flags, SDEV_READONLY)) 1082 SET(g->mt_dsreg, MT_DS_RDONLY); 1083 if (ISSET(st->flags, ST_MOUNTED)) 1084 SET(g->mt_dsreg, MT_DS_MOUNTED); 1085 g->mt_resid = st->mt_resid; 1086 g->mt_erreg = st->mt_erreg; 1087 g->mt_fileno = st->media_fileno; 1088 g->mt_blkno = st->media_blkno; 1089 /* 1090 * clear latched errors. 1091 */ 1092 st->mt_resid = 0; 1093 st->mt_erreg = 0; 1094 break; 1095 } 1096 case MTIOCTOP: { 1097 1098 SC_DEBUG(st->sc_link, SDEV_DB1, 1099 ("[ioctl: op=0x%x count=0x%x]\n", mt->mt_op, mt->mt_count)); 1100 1101 number = mt->mt_count; 1102 switch (mt->mt_op) { 1103 case MTWEOF: /* write an end-of-file record */ 1104 error = st_write_filemarks(st, number, flags); 1105 break; 1106 case MTBSF: /* backward space file */ 1107 number = -number; 1108 case MTFSF: /* forward space file */ 1109 error = st_check_eod(st, 0, &nmarks, flags); 1110 if (error == 0) 1111 error = st_space(st, number - nmarks, 1112 SP_FILEMARKS, flags); 1113 break; 1114 case MTBSR: /* backward space record */ 1115 number = -number; 1116 case MTFSR: /* forward space record */ 1117 error = st_check_eod(st, 1, &nmarks, flags); 1118 if (error == 0) 1119 error = st_space(st, number, SP_BLKS, flags); 1120 break; 1121 case MTREW: /* rewind */ 1122 error = st_rewind(st, 0, flags); 1123 break; 1124 case MTOFFL: /* rewind and put the drive offline */ 1125 st_unmount(st, EJECT, DOREWIND); 1126 break; 1127 case MTNOP: /* no operation, sets status only */ 1128 break; 1129 case MTRETEN: /* retension the tape */ 1130 error = st_load(st, LD_RETENSION, flags); 1131 if (error == 0) 1132 error = st_load(st, LD_LOAD, flags); 1133 break; 1134 case MTEOM: /* forward space to end of media */ 1135 error = st_check_eod(st, 0, &nmarks, flags); 1136 if (error == 0) 1137 error = st_space(st, 1, SP_EOM, flags); 1138 break; 1139 case MTCACHE: /* enable controller cache */ 1140 CLR(st->flags, ST_DONTBUFFER); 1141 goto try_new_value; 1142 case MTNOCACHE: /* disable controller cache */ 1143 SET(st->flags, ST_DONTBUFFER); 1144 goto try_new_value; 1145 case MTERASE: /* erase volume */ 1146 error = st_erase(st, number, flags); 1147 break; 1148 case MTSETBSIZ: /* Set block size for device and mode. */ 1149 if (number == 0) { 1150 CLR(st->flags, ST_FIXEDBLOCKS); 1151 } else { 1152 if ((st->blkmin || st->blkmax) && 1153 (number < st->blkmin || 1154 number > st->blkmax)) { 1155 error = EINVAL; 1156 break; 1157 } 1158 SET(st->flags, ST_FIXEDBLOCKS); 1159 } 1160 st->blksize = number; 1161 goto try_new_value; 1162 1163 case MTSETDNSTY: /* Set density for device and mode. */ 1164 if (number < 0 || number > SCSI_MAX_DENSITY_CODE) { 1165 error = EINVAL; 1166 break; 1167 } 1168 st->density = number; 1169 goto try_new_value; 1170 1171 default: 1172 error = EINVAL; 1173 } 1174 break; 1175 } 1176 case MTIOCIEOT: 1177 case MTIOCEEOT: 1178 break; 1179 1180 #if 0 1181 case MTIOCRDSPOS: 1182 error = st_rdpos(st, 0, (u_int32_t *) arg); 1183 break; 1184 1185 case MTIOCRDHPOS: 1186 error = st_rdpos(st, 1, (u_int32_t *) arg); 1187 break; 1188 1189 case MTIOCSLOCATE: 1190 error = st_setpos(st, 0, (u_int32_t *) arg); 1191 break; 1192 1193 case MTIOCHLOCATE: 1194 error = st_setpos(st, 1, (u_int32_t *) arg); 1195 break; 1196 #endif /* 0 */ 1197 1198 default: 1199 error = scsi_do_ioctl(st->sc_link, cmd, arg, flag); 1200 break; 1201 } 1202 goto done; 1203 1204 try_new_value: 1205 /* 1206 * Check that the mode being asked for is aggreeable to the 1207 * drive. If not, put it back the way it was. 1208 */ 1209 if ((error = st_mode_select(st, 0)) != 0) {/* put it back as it was */ 1210 printf("%s: cannot set selected mode\n", st->sc_dev.dv_xname); 1211 st->density = hold_density; 1212 st->blksize = hold_blksize; 1213 if (st->blksize) 1214 SET(st->flags, ST_FIXEDBLOCKS); 1215 else 1216 CLR(st->flags, ST_FIXEDBLOCKS); 1217 goto done; 1218 } 1219 /* 1220 * As the drive liked it, if we are setting a new default, 1221 * set it into the structures as such. 1222 */ 1223 switch (mt->mt_op) { 1224 case MTSETBSIZ: 1225 st->mode.blksize = st->blksize; 1226 SET(st->flags, ST_MODE_BLKSIZE); 1227 break; 1228 case MTSETDNSTY: 1229 st->mode.density = st->density; 1230 SET(st->flags, ST_MODE_DENSITY); 1231 break; 1232 } 1233 1234 done: 1235 device_unref(&st->sc_dev); 1236 return error; 1237 } 1238 1239 /* 1240 * Do a synchronous read. 1241 */ 1242 int 1243 st_read(struct st_softc *st, char *buf, int size, int flags) 1244 { 1245 struct scsi_rw_tape *cmd; 1246 struct scsi_xfer *xs; 1247 int error; 1248 1249 if (size == 0) 1250 return 0; 1251 1252 xs = scsi_xs_get(st->sc_link, flags | SCSI_DATA_IN); 1253 if (xs == NULL) 1254 return ENOMEM; 1255 xs->cmdlen = sizeof(*cmd); 1256 xs->data = buf; 1257 xs->datalen = size; 1258 xs->retries = 0; 1259 xs->timeout = ST_IO_TIME; 1260 1261 cmd = (struct scsi_rw_tape *)xs->cmd; 1262 cmd->opcode = READ; 1263 if (ISSET(st->flags, ST_FIXEDBLOCKS)) { 1264 SET(cmd->byte2, SRW_FIXED); 1265 _lto3b(size / (st->blksize ? st->blksize : DEF_FIXED_BSIZE), 1266 cmd->len); 1267 } else 1268 _lto3b(size, cmd->len); 1269 1270 error = scsi_xs_sync(xs); 1271 scsi_xs_put(xs); 1272 1273 return error; 1274 } 1275 1276 /* 1277 * Ask the drive what its min and max blk sizes are. 1278 */ 1279 int 1280 st_read_block_limits(struct st_softc *st, int flags) 1281 { 1282 struct scsi_block_limits_data *block_limits = NULL; 1283 struct scsi_block_limits *cmd; 1284 struct scsi_link *link = st->sc_link; 1285 struct scsi_xfer *xs; 1286 int error = 0; 1287 1288 if (ISSET(link->flags, SDEV_MEDIA_LOADED)) 1289 return 0; 1290 1291 block_limits = dma_alloc(sizeof(*block_limits), PR_NOWAIT); 1292 if (block_limits == NULL) 1293 return ENOMEM; 1294 1295 xs = scsi_xs_get(link, flags | SCSI_DATA_IN); 1296 if (xs == NULL) { 1297 error = ENOMEM; 1298 goto done; 1299 } 1300 1301 xs->cmdlen = sizeof(*cmd); 1302 xs->data = (void *)block_limits; 1303 xs->datalen = sizeof(*block_limits); 1304 xs->timeout = ST_CTL_TIME; 1305 1306 cmd = (struct scsi_block_limits *)xs->cmd; 1307 cmd->opcode = READ_BLOCK_LIMITS; 1308 1309 error = scsi_xs_sync(xs); 1310 scsi_xs_put(xs); 1311 1312 if (error == 0) { 1313 st->blkmin = _2btol(block_limits->min_length); 1314 st->blkmax = _3btol(block_limits->max_length); 1315 SC_DEBUG(link, SDEV_DB3, 1316 ("(%d <= blksize <= %d)\n", st->blkmin, st->blkmax)); 1317 } 1318 1319 done: 1320 if (block_limits) 1321 dma_free(block_limits, sizeof(*block_limits)); 1322 return error; 1323 } 1324 1325 /* 1326 * Get the scsi driver to send a full inquiry to the 1327 * device and use the results to fill out the global 1328 * parameter structure. 1329 * 1330 * called from: 1331 * attach 1332 * open 1333 * ioctl (to reset original blksize) 1334 */ 1335 int 1336 st_mode_sense(struct st_softc *st, int flags) 1337 { 1338 union scsi_mode_sense_buf *data = NULL; 1339 struct scsi_link *link = st->sc_link; 1340 u_int64_t block_count; 1341 u_int32_t density, block_size; 1342 u_char *page0 = NULL; 1343 u_int8_t dev_spec; 1344 int error = 0, big; 1345 1346 data = dma_alloc(sizeof(*data), PR_NOWAIT); 1347 if (data == NULL) 1348 return ENOMEM; 1349 1350 /* 1351 * Ask for page 0 (vendor specific) mode sense data. 1352 */ 1353 density = 0; 1354 block_count = 0; 1355 block_size = 0; 1356 error = scsi_do_mode_sense(link, 0, data, (void **)&page0, 1, 1357 flags | SCSI_SILENT, &big); 1358 if (error != 0) 1359 goto done; 1360 scsi_parse_blkdesc(link, data, big, &density, &block_count, 1361 &block_size); 1362 1363 /* It is valid for no page0 to be available. */ 1364 1365 if (big) 1366 dev_spec = data->hdr_big.dev_spec; 1367 else 1368 dev_spec = data->hdr.dev_spec; 1369 1370 if (ISSET(dev_spec, SMH_DSP_WRITE_PROT)) 1371 SET(link->flags, SDEV_READONLY); 1372 else 1373 CLR(link->flags, SDEV_READONLY); 1374 1375 st->media_blksize = block_size; 1376 st->media_density = density; 1377 1378 SC_DEBUG(link, SDEV_DB3, 1379 ("density code 0x%x, %d-byte blocks, write-%s, %sbuffered\n", 1380 st->media_density, st->media_blksize, 1381 ISSET(link->flags, SDEV_READONLY) ? "protected" : "enabled", 1382 ISSET(dev_spec, SMH_DSP_BUFF_MODE) ? "" : "un")); 1383 1384 SET(link->flags, SDEV_MEDIA_LOADED); 1385 1386 done: 1387 if (data) 1388 dma_free(data, sizeof(*data)); 1389 return error; 1390 } 1391 1392 /* 1393 * Send a filled out parameter structure to the drive to 1394 * set it into the desire mode etc. 1395 */ 1396 int 1397 st_mode_select(struct st_softc *st, int flags) 1398 { 1399 union scsi_mode_sense_buf *inbuf = NULL, *outbuf = NULL; 1400 struct scsi_blk_desc general; 1401 struct scsi_link *link = st->sc_link; 1402 u_int8_t *page0 = NULL; 1403 int error = 0, big, page0_size; 1404 1405 inbuf = dma_alloc(sizeof(*inbuf), PR_NOWAIT); 1406 if (inbuf == NULL) { 1407 error = ENOMEM; 1408 goto done; 1409 } 1410 outbuf = dma_alloc(sizeof(*outbuf), PR_NOWAIT | PR_ZERO); 1411 if (outbuf == NULL) { 1412 error = ENOMEM; 1413 goto done; 1414 } 1415 1416 /* 1417 * This quirk deals with drives that have only one valid mode and think 1418 * this gives them license to reject all mode selects, even if the 1419 * selected mode is the one that is supported. 1420 */ 1421 if (ISSET(st->quirks, ST_Q_UNIMODAL)) { 1422 SC_DEBUG(link, SDEV_DB3, 1423 ("not setting density 0x%x blksize 0x%x\n", 1424 st->density, st->blksize)); 1425 error = 0; 1426 goto done; 1427 } 1428 1429 if (ISSET(link->flags, SDEV_ATAPI)) { 1430 error = 0; 1431 goto done; 1432 } 1433 1434 bzero(&general, sizeof(general)); 1435 1436 general.density = st->density; 1437 if (ISSET(st->flags, ST_FIXEDBLOCKS)) 1438 _lto3b(st->blksize, general.blklen); 1439 1440 /* 1441 * Ask for page 0 (vendor specific) mode sense data. 1442 * 1443 * page0 == NULL is a valid situation. 1444 */ 1445 error = scsi_do_mode_sense(link, 0, inbuf, (void **)&page0, 1, 1446 flags | SCSI_SILENT, &big); 1447 if (error != 0) 1448 goto done; 1449 1450 if (page0 == NULL) { 1451 page0_size = 0; 1452 } else if (big == 0) { 1453 page0_size = inbuf->hdr.data_length + 1454 sizeof(inbuf->hdr.data_length) - sizeof(inbuf->hdr) - 1455 inbuf->hdr.blk_desc_len; 1456 memcpy(&outbuf->buf[sizeof(outbuf->hdr)+ sizeof(general)], 1457 page0, page0_size); 1458 } else { 1459 page0_size = _2btol(inbuf->hdr_big.data_length) + 1460 sizeof(inbuf->hdr_big.data_length) - 1461 sizeof(inbuf->hdr_big) - 1462 _2btol(inbuf->hdr_big.blk_desc_len); 1463 memcpy(&outbuf->buf[sizeof(outbuf->hdr_big) + sizeof(general)], 1464 page0, page0_size); 1465 } 1466 1467 /* 1468 * Set up for a mode select. 1469 */ 1470 if (big == 0) { 1471 outbuf->hdr.data_length = sizeof(outbuf->hdr) + 1472 sizeof(general) + page0_size - 1473 sizeof(outbuf->hdr.data_length); 1474 if (!ISSET(st->flags, ST_DONTBUFFER)) 1475 outbuf->hdr.dev_spec = SMH_DSP_BUFF_MODE_ON; 1476 outbuf->hdr.blk_desc_len = sizeof(general); 1477 memcpy(&outbuf->buf[sizeof(outbuf->hdr)], 1478 &general, sizeof(general)); 1479 error = scsi_mode_select(st->sc_link, 0, &outbuf->hdr, 1480 flags, ST_CTL_TIME); 1481 goto done; 1482 } 1483 1484 /* MODE SENSE (10) header was returned, so use MODE SELECT (10). */ 1485 _lto2b((sizeof(outbuf->hdr_big) + sizeof(general) + page0_size - 1486 sizeof(outbuf->hdr_big.data_length)), outbuf->hdr_big.data_length); 1487 if (!ISSET(st->flags, ST_DONTBUFFER)) 1488 outbuf->hdr_big.dev_spec = SMH_DSP_BUFF_MODE_ON; 1489 _lto2b(sizeof(general), outbuf->hdr_big.blk_desc_len); 1490 memcpy(&outbuf->buf[sizeof(outbuf->hdr_big)], &general, 1491 sizeof(general)); 1492 1493 error = scsi_mode_select_big(st->sc_link, 0, &outbuf->hdr_big, 1494 flags, ST_CTL_TIME); 1495 done: 1496 if (inbuf) 1497 dma_free(inbuf, sizeof(*inbuf)); 1498 if (outbuf) 1499 dma_free(outbuf, sizeof(*outbuf)); 1500 return error; 1501 } 1502 1503 /* 1504 * issue an erase command 1505 */ 1506 int 1507 st_erase(struct st_softc *st, int full, int flags) 1508 { 1509 struct scsi_erase *cmd; 1510 struct scsi_xfer *xs; 1511 int error; 1512 1513 xs = scsi_xs_get(st->sc_link, flags); 1514 if (xs == NULL) 1515 return ENOMEM; 1516 xs->cmdlen = sizeof(*cmd); 1517 1518 /* 1519 * Full erase means set LONG bit in erase command, which asks 1520 * the drive to erase the entire unit. Without this bit, we're 1521 * asking the drive to write an erase gap. 1522 */ 1523 cmd = (struct scsi_erase *)xs->cmd; 1524 cmd->opcode = ERASE; 1525 if (full) { 1526 cmd->byte2 = SE_IMMED|SE_LONG; 1527 xs->timeout = ST_SPC_TIME; 1528 } else { 1529 cmd->byte2 = SE_IMMED; 1530 xs->timeout = ST_IO_TIME; 1531 } 1532 1533 /* 1534 * XXX We always do this asynchronously, for now. How long should 1535 * we wait if we want to (eventually) to it synchronously? 1536 */ 1537 error = scsi_xs_sync(xs); 1538 scsi_xs_put(xs); 1539 1540 return error; 1541 } 1542 1543 /* 1544 * skip N blocks/filemarks/seq filemarks/eom 1545 */ 1546 int 1547 st_space(struct st_softc *st, int number, u_int what, int flags) 1548 { 1549 struct scsi_space *cmd; 1550 struct scsi_xfer *xs; 1551 int error; 1552 1553 switch (what) { 1554 case SP_BLKS: 1555 if (ISSET(st->flags, ST_PER_ACTION)) { 1556 if (number > 0) { 1557 CLR(st->flags, ST_PER_ACTION); 1558 return EIO; 1559 } else if (number < 0) { 1560 if (ISSET(st->flags, ST_AT_FILEMARK)) { 1561 /* 1562 * Handling of ST_AT_FILEMARK 1563 * in st_space will fill in the 1564 * right file mark count. 1565 */ 1566 error = st_space(st, 0, SP_FILEMARKS, 1567 flags); 1568 if (error != 0) 1569 return error; 1570 } 1571 if (ISSET(st->flags, ST_BLANK_READ)) { 1572 CLR(st->flags, ST_BLANK_READ); 1573 return EIO; 1574 } 1575 CLR(st->flags, ST_EIO_PENDING | ST_EOM_PENDING); 1576 } 1577 } 1578 break; 1579 case SP_FILEMARKS: 1580 if (ISSET(st->flags, ST_EIO_PENDING)) { 1581 if (number > 0) { 1582 /* pretend we just discovered the error */ 1583 CLR(st->flags, ST_EIO_PENDING); 1584 return EIO; 1585 } else if (number < 0) { 1586 /* back away from the error */ 1587 CLR(st->flags, ST_EIO_PENDING); 1588 } 1589 } 1590 if (ISSET(st->flags, ST_AT_FILEMARK)) { 1591 CLR(st->flags, ST_AT_FILEMARK); 1592 number--; 1593 } 1594 if (ISSET(st->flags, ST_BLANK_READ) && (number < 0)) { 1595 /* back away from unwritten tape */ 1596 CLR(st->flags, ST_BLANK_READ); 1597 number++; /* XXX dubious */ 1598 } 1599 break; 1600 case SP_EOM: 1601 if (ISSET(st->flags, ST_EOM_PENDING)) { 1602 /* We are already there. */ 1603 CLR(st->flags, ST_EOM_PENDING); 1604 return 0; 1605 } 1606 if (ISSET(st->flags, ST_EIO_PENDING)) { 1607 /* pretend we just discovered the error */ 1608 CLR(st->flags, ST_EIO_PENDING); 1609 return EIO; 1610 } 1611 if (ISSET(st->flags, ST_AT_FILEMARK)) 1612 CLR(st->flags, ST_AT_FILEMARK); 1613 break; 1614 } 1615 if (number == 0) 1616 return 0; 1617 1618 xs = scsi_xs_get(st->sc_link, flags); 1619 if (xs == NULL) 1620 return ENOMEM; 1621 1622 cmd = (struct scsi_space *)xs->cmd; 1623 cmd->opcode = SPACE; 1624 cmd->byte2 = what; 1625 _lto3b(number, cmd->number); 1626 xs->cmdlen = sizeof(*cmd); 1627 xs->timeout = ST_SPC_TIME; 1628 1629 CLR(st->flags, ST_EOD_DETECTED); 1630 1631 error = scsi_xs_sync(xs); 1632 scsi_xs_put(xs); 1633 1634 if (error != 0) { 1635 st->media_fileno = -1; 1636 st->media_blkno = -1; 1637 } else { 1638 switch (what) { 1639 case SP_BLKS: 1640 if (st->media_blkno != -1) { 1641 st->media_blkno += number; 1642 if (st->media_blkno < 0) 1643 st->media_blkno = -1; 1644 } 1645 break; 1646 case SP_FILEMARKS: 1647 if (st->media_fileno != -1) { 1648 if (!ISSET(st->flags, ST_EOD_DETECTED)) 1649 st->media_fileno += number; 1650 if (st->media_fileno > st->media_eom) 1651 st->media_eom = st->media_fileno; 1652 st->media_blkno = 0; 1653 } 1654 break; 1655 case SP_EOM: 1656 if (st->media_eom != -1) { 1657 st->media_fileno = st->media_eom; 1658 st->media_blkno = 0; 1659 } else { 1660 st->media_fileno = -1; 1661 st->media_blkno = -1; 1662 } 1663 break; 1664 default: 1665 st->media_fileno = -1; 1666 st->media_blkno = -1; 1667 break; 1668 } 1669 } 1670 1671 return error; 1672 } 1673 1674 /* 1675 * write N filemarks 1676 */ 1677 int 1678 st_write_filemarks(struct st_softc *st, int number, int flags) 1679 { 1680 struct scsi_write_filemarks *cmd; 1681 struct scsi_xfer *xs; 1682 int error; 1683 1684 if (number < 0) 1685 return EINVAL; 1686 1687 xs = scsi_xs_get(st->sc_link, flags); 1688 if (xs == NULL) 1689 return ENOMEM; 1690 1691 xs->cmdlen = sizeof(*cmd); 1692 xs->timeout = ST_IO_TIME * 4; 1693 1694 switch (number) { 1695 case 0: /* really a command to sync the drive's buffers */ 1696 break; 1697 case 1: 1698 if (ISSET(st->flags, ST_FM_WRITTEN)) /* already have one down */ 1699 CLR(st->flags, ST_WRITTEN); 1700 else 1701 SET(st->flags, ST_FM_WRITTEN); 1702 CLR(st->flags, ST_PER_ACTION); 1703 break; 1704 default: 1705 CLR(st->flags, ST_PER_ACTION | ST_WRITTEN); 1706 break; 1707 } 1708 1709 cmd = (struct scsi_write_filemarks *)xs->cmd; 1710 cmd->opcode = WRITE_FILEMARKS; 1711 _lto3b(number, cmd->number); 1712 1713 error = scsi_xs_sync(xs); 1714 scsi_xs_put(xs); 1715 1716 if (error != 0) { 1717 st->media_fileno = -1; 1718 st->media_blkno = -1; 1719 st->media_eom = -1; 1720 } else if (st->media_fileno != -1) { 1721 st->media_fileno += number; 1722 st->media_eom = st->media_fileno; 1723 st->media_blkno = 0; 1724 } 1725 1726 return error; 1727 } 1728 1729 /* 1730 * Make sure the right number of file marks is on tape if the 1731 * tape has been written. If the position argument is true, 1732 * leave the tape positioned where it was originally. 1733 * 1734 * nmarks returns the number of marks to skip (or, if position 1735 * true, which were skipped) to get back original position. 1736 */ 1737 int 1738 st_check_eod(struct st_softc *st, int position, int *nmarks, int flags) 1739 { 1740 int error; 1741 1742 switch (st->flags & (ST_WRITTEN | ST_FM_WRITTEN | ST_2FM_AT_EOD)) { 1743 default: 1744 *nmarks = 0; 1745 return 0; 1746 case ST_WRITTEN: 1747 case ST_WRITTEN | ST_FM_WRITTEN | ST_2FM_AT_EOD: 1748 *nmarks = 1; 1749 break; 1750 case ST_WRITTEN | ST_2FM_AT_EOD: 1751 *nmarks = 2; 1752 } 1753 error = st_write_filemarks(st, *nmarks, flags); 1754 if (error == 0 && position != 0) 1755 error = st_space(st, -*nmarks, SP_FILEMARKS, flags); 1756 return error; 1757 } 1758 1759 /* 1760 * load/unload/retension 1761 */ 1762 int 1763 st_load(struct st_softc *st, u_int type, int flags) 1764 { 1765 struct scsi_load *cmd; 1766 struct scsi_xfer *xs; 1767 int error, nmarks; 1768 1769 st->media_fileno = -1; 1770 st->media_blkno = -1; 1771 st->media_eom = -1; 1772 1773 if (type != LD_LOAD) { 1774 error = st_check_eod(st, 0, &nmarks, flags); 1775 if (error != 0) 1776 return error; 1777 } 1778 1779 if (ISSET(st->quirks, ST_Q_IGNORE_LOADS)) { 1780 if (type == LD_LOAD) { 1781 /* 1782 * If we ignore loads, at least we should try a rewind. 1783 */ 1784 return st_rewind(st, 0, flags); 1785 } 1786 return 0; 1787 } 1788 1789 1790 xs = scsi_xs_get(st->sc_link, flags); 1791 if (xs == NULL) 1792 return ENOMEM; 1793 xs->cmdlen = sizeof(*cmd); 1794 xs->timeout = ST_SPC_TIME; 1795 1796 cmd = (struct scsi_load *)xs->cmd; 1797 cmd->opcode = LOAD; 1798 cmd->how = type; 1799 1800 error = scsi_xs_sync(xs); 1801 scsi_xs_put(xs); 1802 1803 return error; 1804 } 1805 1806 /* 1807 * Rewind the device 1808 */ 1809 int 1810 st_rewind(struct st_softc *st, u_int immediate, int flags) 1811 { 1812 struct scsi_rewind *cmd; 1813 struct scsi_xfer *xs; 1814 int error, nmarks; 1815 1816 error = st_check_eod(st, 0, &nmarks, flags); 1817 if (error != 0) 1818 return error; 1819 CLR(st->flags, ST_PER_ACTION); 1820 1821 xs = scsi_xs_get(st->sc_link, flags); 1822 if (xs == NULL) 1823 return ENOMEM; 1824 xs->cmdlen = sizeof(*cmd); 1825 xs->timeout = immediate ? ST_CTL_TIME : ST_SPC_TIME; 1826 1827 cmd = (struct scsi_rewind *)xs->cmd; 1828 cmd->opcode = REWIND; 1829 cmd->byte2 = immediate; 1830 1831 error = scsi_xs_sync(xs); 1832 scsi_xs_put(xs); 1833 1834 if (error == 0) { 1835 st->media_fileno = 0; 1836 st->media_blkno = 0; 1837 } 1838 1839 return error; 1840 } 1841 1842 /* 1843 * Look at the returned sense and act on the error and detirmine 1844 * The unix error number to pass back... (0 = report no error) 1845 * (-1 = continue processing) 1846 */ 1847 int 1848 st_interpret_sense(struct scsi_xfer *xs) 1849 { 1850 struct scsi_sense_data *sense = &xs->sense; 1851 struct scsi_link *link = xs->sc_link; 1852 struct scsi_space *space; 1853 struct st_softc *st = link->device_softc; 1854 u_int8_t serr = sense->error_code & SSD_ERRCODE; 1855 u_int8_t skey = sense->flags & SSD_KEY; 1856 int32_t resid, info, number; 1857 int datalen; 1858 1859 if (!ISSET(link->flags, SDEV_OPEN) || 1860 (serr != SSD_ERRCODE_CURRENT && serr != SSD_ERRCODE_DEFERRED)) 1861 return scsi_interpret_sense(xs); 1862 1863 info = (int32_t)_4btol(sense->info); 1864 1865 switch (skey) { 1866 1867 /* 1868 * We do custom processing in st for the unit becoming ready case. 1869 * in this case we do not allow xs->retries to be decremented 1870 * only on the "Unit Becoming Ready" case. This is because tape 1871 * drives report "Unit Becoming Ready" when loading media, etc. 1872 * and can take a long time. Rather than having a massive timeout 1873 * for all operations (which would cause other problems) we allow 1874 * operations to wait (but be interruptable with Ctrl-C) forever 1875 * as long as the drive is reporting that it is becoming ready. 1876 * all other cases are handled as per the default. 1877 */ 1878 1879 case SKEY_NOT_READY: 1880 if (ISSET(xs->flags, SCSI_IGNORE_NOT_READY)) 1881 return 0; 1882 switch (ASC_ASCQ(sense)) { 1883 case SENSE_NOT_READY_BECOMING_READY: 1884 SC_DEBUG(link, SDEV_DB1, ("not ready: busy (%#x)\n", 1885 sense->add_sense_code_qual)); 1886 /* don't count this as a retry */ 1887 xs->retries++; 1888 return scsi_delay(xs, 1); 1889 default: 1890 return scsi_interpret_sense(xs); 1891 } 1892 break; 1893 case SKEY_BLANK_CHECK: 1894 if (sense->error_code & SSD_ERRCODE_VALID && 1895 xs->cmd->opcode == SPACE) { 1896 switch (ASC_ASCQ(sense)) { 1897 case SENSE_END_OF_DATA_DETECTED: 1898 SET(st->flags, ST_EOD_DETECTED); 1899 space = (struct scsi_space *)xs->cmd; 1900 number = _3btol(space->number); 1901 st->media_fileno = number - info; 1902 st->media_eom = st->media_fileno; 1903 return 0; 1904 case SENSE_BEGINNING_OF_MEDIUM_DETECTED: 1905 /* Standard says: Position is undefined! */ 1906 SET(st->flags, ST_BOD_DETECTED); 1907 st->media_fileno = -1; 1908 st->media_blkno = -1; 1909 return 0; 1910 } 1911 } 1912 break; 1913 case SKEY_NO_SENSE: 1914 case SKEY_RECOVERED_ERROR: 1915 case SKEY_MEDIUM_ERROR: 1916 case SKEY_VOLUME_OVERFLOW: 1917 break; 1918 default: 1919 return scsi_interpret_sense(xs); 1920 } 1921 1922 /* 1923 * 'resid' can be in units of st->blksize or bytes. xs->resid and 1924 * xs->datalen are always in units of bytes. So we need a variable 1925 * to store datalen in the same units as resid and to adjust 1926 * xs->resid to be in bytes. 1927 */ 1928 if (ISSET(sense->error_code, SSD_ERRCODE_VALID)) { 1929 if (ISSET(st->flags, ST_FIXEDBLOCKS)) 1930 resid = info * st->blksize; /* XXXX overflow? */ 1931 else 1932 resid = info; 1933 } else { 1934 resid = xs->datalen; 1935 } 1936 1937 if (resid < 0 || resid > xs->datalen) 1938 xs->resid = xs->datalen; 1939 else 1940 xs->resid = resid; 1941 1942 datalen = xs->datalen; 1943 if (ISSET(st->flags, ST_FIXEDBLOCKS)) { 1944 resid /= st->blksize; 1945 datalen /= st->blksize; 1946 } 1947 1948 if (ISSET(sense->flags, SSD_FILEMARK)) { 1949 if (st->media_fileno != -1) { 1950 st->media_fileno++; 1951 if (st->media_fileno > st->media_eom) 1952 st->media_eom = st->media_fileno; 1953 st->media_blkno = 0; 1954 } 1955 if (!ISSET(st->flags, ST_FIXEDBLOCKS)) 1956 return 0; 1957 SET(st->flags, ST_AT_FILEMARK); 1958 } 1959 1960 if (ISSET(sense->flags, SSD_EOM)) { 1961 SET(st->flags, ST_EOM_PENDING); 1962 xs->resid = 0; 1963 if (ISSET(st->flags, ST_FIXEDBLOCKS)) 1964 return 0; 1965 } 1966 1967 if (ISSET(sense->flags, SSD_ILI)) { 1968 if (!ISSET(st->flags, ST_FIXEDBLOCKS)) { 1969 if (resid >= 0 && resid <= datalen) 1970 return 0; 1971 if (!ISSET(xs->flags, SCSI_SILENT)) 1972 printf( "%s: bad residual %d out of " 1973 "%d\n", st->sc_dev.dv_xname, resid, 1974 datalen); 1975 return EIO; 1976 } 1977 1978 /* Fixed size blocks. */ 1979 if (ISSET(sense->error_code, SSD_ERRCODE_VALID)) 1980 if (!ISSET(xs->flags, SCSI_SILENT)) 1981 printf("%s: block wrong size, %d blocks " 1982 "residual\n", st->sc_dev.dv_xname, resid); 1983 SET(st->flags, ST_EIO_PENDING); 1984 /* 1985 * This quirk code helps the drive read the first tape block, 1986 * regardless of format. That is required for these drives to 1987 * return proper MODE SENSE information. 1988 */ 1989 if (ISSET(st->quirks, ST_Q_SENSE_HELP) && 1990 !ISSET(link->flags, SDEV_MEDIA_LOADED)) 1991 st->blksize -= 512; 1992 } 1993 1994 if (ISSET(st->flags, ST_FIXEDBLOCKS) && xs->resid == xs->datalen) { 1995 if (ISSET(st->flags, ST_EIO_PENDING)) 1996 return EIO; 1997 if (ISSET(st->flags, ST_AT_FILEMARK)) 1998 return 0; 1999 } 2000 2001 if (skey == SKEY_BLANK_CHECK) { 2002 /* 2003 * This quirk code helps the drive read the first tape block, 2004 * regardless of format. That is required for these drives to 2005 * return proper MODE SENSE information. 2006 */ 2007 if (ISSET(st->quirks, ST_Q_SENSE_HELP) && 2008 !ISSET(link->flags, SDEV_MEDIA_LOADED)) { 2009 /* still starting */ 2010 st->blksize -= 512; 2011 } else if (!ISSET(st->flags, ST_2FM_AT_EOD | ST_BLANK_READ)) { 2012 SET(st->flags, ST_BLANK_READ); 2013 SET(st->flags, ST_EOM_PENDING); 2014 xs->resid = xs->datalen; 2015 return 0; 2016 } 2017 } 2018 2019 return scsi_interpret_sense(xs); 2020 } 2021 2022 /* 2023 * The quirk here is that the drive returns some value to st_mode_sense 2024 * incorrectly until the tape has actually passed by the head. 2025 * 2026 * The method is to set the drive to large fixed-block state (user-specified 2027 * density and 1024-byte blocks), then read and rewind to get it to sense the 2028 * tape. If that doesn't work, try 512-byte fixed blocks. If that doesn't 2029 * work, as a last resort, try variable- length blocks. The result will be 2030 * the ability to do an accurate st_mode_sense. 2031 * 2032 * We know we can do a rewind because we just did a load, which implies rewind. 2033 * Rewind seems preferable to space backward if we have a virgin tape. 2034 * 2035 * The rest of the code for this quirk is in ILI processing and BLANK CHECK 2036 * error processing, both part of st_interpret_sense. 2037 */ 2038 int 2039 st_touch_tape(struct st_softc *st) 2040 { 2041 char *buf = NULL; 2042 int readsize, maxblksize = 1024; 2043 int error = 0; 2044 2045 if ((error = st_mode_sense(st, 0)) != 0) 2046 goto done; 2047 buf = dma_alloc(maxblksize, PR_NOWAIT); 2048 if (buf == NULL) { 2049 error = ENOMEM; 2050 goto done; 2051 } 2052 2053 st->blksize = 1024; 2054 do { 2055 switch (st->blksize) { 2056 case 512: 2057 case 1024: 2058 readsize = st->blksize; 2059 SET(st->flags, ST_FIXEDBLOCKS); 2060 break; 2061 default: 2062 readsize = 1; 2063 CLR(st->flags, ST_FIXEDBLOCKS); 2064 } 2065 if ((error = st_mode_select(st, 0)) != 0) 2066 goto done; 2067 st_read(st, buf, readsize, SCSI_SILENT); /* XXX */ 2068 if ((error = st_rewind(st, 0, 0)) != 0) 2069 goto done; 2070 } while (readsize != 1 && readsize > st->blksize); 2071 done: 2072 dma_free(buf, maxblksize); 2073 return error; 2074 } 2075