1/* $Id: tpqic02.c,v 1.1.1.1 2008/10/15 03:26:28 james26_jang Exp $ 2 * 3 * Driver for tape drive support for Linux-i386 4 * 5 * Copyright (c) 1992--1996 by H. H. Bergman. All rights reserved. 6 * Current e-mail address: hennus@cybercomm.nl 7 * 8 * Distribution of this program in executable form is only allowed if 9 * all of the corresponding source files are made available through the same 10 * medium at no extra cost. 11 * 12 * I will not accept any responsibility for damage caused directly or 13 * indirectly by this program, or code derived from this program. 14 * 15 * Use this code at your own risk. Don't blame me if it destroys your data! 16 * Make sure you have a backup before you try this code. 17 * 18 * If you make changes to my code and redistribute it in source or binary 19 * form you must make it clear to even casual users of your code that you 20 * have modified my code, clearly point out what the changes exactly are 21 * (preferably in the form of a context diff file), how to undo your changes, 22 * where the original can be obtained, and that complaints/requests about the 23 * modified code should be directed to you instead of me. 24 * 25 * This driver was partially inspired by the 'wt' driver in the 386BSD 26 * source distribution, which carries the following copyright notice: 27 * 28 * Copyright (c) 1991 The Regents of the University of California. 29 * All rights reserved. 30 * 31 * You are not allowed to change this line nor the text above. 32 * 33 * 2001/02/26 Minor s/suser/capable/ 34 * 35 * 1996/10/10 Emerald changes 36 * 37 * 1996/05/21 Misc changes+merges+cleanups + I/O reservations 38 * 39 * 1996/05/20 Module support patches submitted by Brian McCauley. 40 * 41 * 1994/05/03 Initial attempt at Mountain support for the Mountain 7150. 42 * Based on patches provided by Erik Jacobson. Still incomplete, I suppose. 43 * 44 * 1994/02/07 Archive changes & some cleanups by Eddy Olk. 45 * 46 * 1994/01/19 Speed measuring stuff moved from aperf.h to delay.h. 47 * BogoMips (tm) introduced by Linus. 48 * 49 * 1993/01/25 Kernel udelay. Eof fixups. 50 * 51 * 1992/09/19 Some changes based on patches by Eddy Olk to support 52 * Archive SC402/SC499R controller cards. 53 * 54 * 1992/05/27 First release. 55 * 56 * 1992/05/26 Initial version. Copyright H. H. Bergman 1992 57 */ 58 59/* After the legalese, now the important bits: 60 * 61 * This is a driver for the Wangtek 5150 tape drive with 62 * a QIC-02 controller for ISA-PC type computers. 63 * Hopefully it will work with other QIC-02 tape drives as well. 64 * 65 * Make sure your setup matches the configuration parameters. 66 * Also, be careful to avoid IO conflicts with other devices! 67 */ 68 69 70/* 71#define TDEBUG 72*/ 73 74#define REALLY_SLOW_IO /* it sure is ... */ 75 76#include <linux/module.h> 77 78#include <linux/config.h> 79 80#include <linux/sched.h> 81#include <linux/timer.h> 82#include <linux/fs.h> 83#include <linux/kernel.h> 84#include <linux/major.h> 85#include <linux/errno.h> 86#include <linux/mtio.h> 87#include <linux/fcntl.h> 88#include <linux/delay.h> 89#include <linux/ioport.h> 90#include <linux/tpqic02.h> 91#include <linux/mm.h> 92#include <linux/slab.h> 93#include <linux/init.h> 94#include <linux/smp_lock.h> 95#include <linux/devfs_fs_kernel.h> 96 97#include <asm/dma.h> 98#include <asm/system.h> 99#include <asm/io.h> 100#include <asm/uaccess.h> 101 102/* check existence of required configuration parameters */ 103#if !defined(QIC02_CMD_PORT) || !defined(QIC02_TAPE_IRQ) || !defined(QIC02_TAPE_DMA) 104# error qic02_tape configuration error 105#endif 106 107 108#define TPQIC02_NAME "tpqic02" 109 110/* Linux outb() commands have (value,port) as parameters. 111 * One might expect (port,value) instead, so beware! 112 */ 113 114#ifdef CONFIG_QIC02_DYNCONF 115/* This holds the dynamic configuration info for the interface 116 * card+drive info if runtime configuration has been selected. 117 */ 118 119static struct mtconfiginfo qic02_tape_dynconf = /* user settable */ 120{ 0, 0, BOGUS_IRQ, 0, 0, TPQD_DEFAULT_FLAGS, }; 121static struct qic02_ccb qic02_tape_ccb; /* private stuff */ 122 123#else 124 125unsigned long qic02_tape_debug = TPQD_DEFAULT_FLAGS; 126 127# if ((QIC02_TAPE_IFC!=WANGTEK) && (QIC02_TAPE_IFC!=ARCHIVE) && (QIC02_TAPE_IFC!=MOUNTAIN)) 128# error No valid interface card specified 129# endif 130#endif /* CONFIG_QIC02_DYNCONF */ 131 132static volatile int ctlbits; /* control reg bits for tape interface */ 133 134static wait_queue_head_t qic02_tape_transfer; /* sync rw with interrupts */ 135 136static volatile struct mtget ioctl_status; /* current generic status */ 137 138static volatile struct tpstatus tperror; /* last drive status */ 139 140static char rcs_revision[] = "$Revision: 1.1.1.1 $"; 141static char rcs_date[] = "$Date: 2008/10/15 03:26:28 $"; 142 143/* Flag bits for status and outstanding requests. 144 * (Could all be put in one bit-field-struct.) 145 * Some variables need `volatile' because they may be modified 146 * by an interrupt. 147 */ 148static volatile flag status_dead = YES; /* device is legally dead until proven alive */ 149static flag status_zombie = YES; /* it's `zombie' until irq/dma allocated */ 150 151static volatile flag status_bytes_wr = NO; /* write FM at close or not */ 152static volatile flag status_bytes_rd = NO; /* (rd|wr) used for rewinding */ 153 154static volatile unsigned long status_cmd_pending; /* cmd in progress */ 155static volatile flag status_expect_int = NO; /* ready for interrupts */ 156static volatile flag status_timer_on = NO; /* using time-out */ 157static volatile int status_error; /* int handler may detect error */ 158static volatile flag status_eof_detected = NO; /* end of file */ 159static volatile flag status_eom_detected = NO; /* end of recorded media */ 160static volatile flag status_eot_detected = NO; /* end of tape */ 161static volatile flag doing_read = NO; 162static volatile flag doing_write = NO; 163 164static volatile unsigned long dma_bytes_todo; 165static volatile unsigned long dma_bytes_done; 166static volatile unsigned dma_mode; /* !=0 also means DMA in use */ 167static flag need_rewind = YES; 168 169static kdev_t current_tape_dev; 170static int extra_blocks_left = BLOCKS_BEYOND_EW; 171 172static struct timer_list tp_timer; 173 174/* return_*_eof: 175 * NO: not at EOF, 176 * YES: tell app EOF was reached (return 0). 177 * 178 * return_*_eof==YES && reported_*_eof==NO ==> 179 * return current buffer, next time(s) return EOF. 180 * 181 * return_*_eof==YES && reported_*_eof==YES ==> 182 * at EOF and application knows it, so we can 183 * move on to the next file. 184 * 185 */ 186static flag return_read_eof = NO; /* set to signal app EOF was reached */ 187static flag return_write_eof = NO; 188static flag reported_read_eof = NO; /* set when we've done that */ 189static flag reported_write_eof = NO; 190 191 192/* This is for doing `mt seek <blocknr>' */ 193static char seek_addr_buf[AR_SEEK_BUF_SIZE]; 194 195 196/* In write mode, we have to write a File Mark after the last block written, 197 * when the tape device is closed. Tape repositioning and reading in write 198 * mode is allowed as long as no actual writing has been done. After writing 199 * the File Mark, repositioning and reading are allowed again. 200 */ 201static int mode_access; /* access mode: READ or WRITE */ 202 203static int qic02_get_resources(void); 204static void qic02_release_resources(void); 205 206/* This is a pointer to the actual kernel buffer where the interrupt routines 207 * read from/write to. It is needed because the DMA channels 1 and 3 cannot 208 * always access the user buffers. [The kernel buffer must reside in the 209 * lower 16MBytes of system memory because of the DMA controller.] The user 210 * must ensure that a large enough buffer is passed to the kernel, in order 211 * to reduce tape repositioning wear and tear. 212 */ 213static void *buffaddr; /* virtual address of buffer */ 214 215/* This translates minor numbers to the corresponding recording format: */ 216static const char *format_names[] = { 217 "not set", /* for dumb drives unable to handle format selection */ 218 "11", /* extinct */ 219 "24", 220 "120", 221 "150", 222 "300", /* untested. */ 223 "600" /* untested. */ 224}; 225 226 227/* `exception_list' is needed for exception status reporting. 228 * Exceptions 1..14 are defined by QIC-02 rev F. 229 * The drive status is matched sequentially to each entry, 230 * ignoring irrelevant bits, until a match is found. If no 231 * match is found, exception number 0 is used. (That should of 232 * course never happen...) The original table was based on the 233 * "Exception Status Summary" in QIC-02 rev F, but some changes 234 * were required to make it work with real-world drives. 235 * 236 * Exception 2 (CNI) is changed to also cover status 0x00e0 (mask USL), 237 * Exception 4 (EOM) is changed to also cover status 0x8288 (mask EOR), 238 * Exception 11 (FIL) is changed to also cover status 0x0089 (mask EOM). 239 * Exception 15 (EOR) is added for seek-to-end-of-data (catch EOR), 240 * Exception 16 (BOM) is added for beginning-of-media (catch BOM). 241 * 242 * Had to swap EXC_NDRV and EXC_NCART to ensure that extended EXC_NCART 243 * (because of the incorrect Wangtek status code) doesn't catch the 244 * EXC_NDRV first. 245 */ 246static struct exception_list_type { 247 unsigned short mask, code; 248 const char *msg; 249 /* EXC_nr attribute should match with tpqic02.h */ 250} exception_list[] = { 251 { 252 0, 0, "Unknown exception status code", /* extra: 0 */ }, 253 { 254 ~(0), TP_ST0 | TP_CNI | TP_USL | TP_WRP, 255 "Drive not online" /* 1 */ }, 256 /* Drive presence goes before cartridge presence. */ 257 { 258 ~(TP_WRP | TP_USL), TP_ST0 | TP_CNI, 259 /* My Wangtek 5150EQ sometimes reports a status code 260 * of 0x00e0, which is not a valid exception code, but 261 * I think it should be recognized as "NO CARTRIDGE". 262 */ 263 "Cartridge not in place" /* 2 */ }, 264 { 265 (unsigned short) ~(TP_ST1 | TP_BOM), (TP_ST0 | TP_WRP), 266 "Write protected cartridge" /* 3 */ }, 267 { 268 (unsigned short) ~(TP_ST1 | TP_EOR), (TP_ST0 | TP_EOM), 269 "End of media" /* 4 */ }, 270 { 271 ~TP_WRP, TP_ST0 | TP_UDA | TP_ST1 | TP_BOM, 272 "Read or Write abort. Rewind tape." /* 5 */ }, 273 { 274 ~TP_WRP, TP_ST0 | TP_UDA, 275 "Read error. Bad block transferred." /* 6 */ }, 276 { 277 ~TP_WRP, TP_ST0 | TP_UDA | TP_BNL, 278 "Read error. Filler block transferred." /* 7 */ }, 279 { 280 ~TP_WRP, TP_ST0 | TP_UDA | TP_BNL | TP_ST1 | TP_NDT, 281 "Read error. No data detected." /* 8 */ }, 282 { 283 ~TP_WRP, 284 TP_ST0 | TP_EOM | TP_UDA | TP_BNL | TP_ST1 | 285 TP_NDT, "Read error. No data detected. EOM." /* 9 */ }, 286 { 287 ~(TP_WRP | TP_MBD | TP_PAR | TP_EOR), 288 TP_ST0 | TP_UDA | TP_BNL | TP_ST1 | TP_NDT | 289 TP_BOM, 290 "Read error. No data detected. BOM." /* 10 */ }, 291 { 292 ~(TP_WRP | TP_EOM), TP_ST0 | TP_FIL, 293 /* Status 0x0089 (EOM & FM) is viewed as an FM, 294 * because it can only happen during a read. 295 * EOM is checked separately for an FM condition. 296 */ 297 "File mark detected" /* 11 */ }, 298 { 299 ~(TP_ST0 | TP_CNI | TP_USL | TP_WRP | TP_BOM), 300 TP_ST1 | TP_ILL, "Illegal command" /* 12 */ }, 301 { 302 ~(TP_ST0 | TP_CNI | TP_USL | TP_WRP | TP_BOM), 303 TP_ST1 | TP_POR, "Reset occurred" /* 13 */ }, 304 { 305 ~TP_WRP, TP_ST0 | TP_FIL | TP_MBD, /* NOTE: ST1 not set! */ 306 "Marginal block detected" /* 14 */ }, 307 { 308 ~(TP_ST0 | TP_WRP | TP_EOM | TP_UDA | TP_BNL | TP_FIL | 309 TP_NDT), TP_ST1 | TP_EOR, 310 /********** Is the extra TP_NDT really needed Eddy? **********/ 311 "End of recorded media" /* extra: 15 */ }, 312 /* 15 is returned when SEEKEOD completes successfully */ 313 { 314 ~(TP_WRP | TP_ST0), TP_ST1 | TP_BOM, "Beginning of media" /* extra: 16 */ } 315}; 316 317#define NR_OF_EXC (sizeof(exception_list)/sizeof(struct exception_list_type)) 318 319/* Compare expected struct size and actual struct size. This 320 * is useful to catch programs compiled with old #includes. 321 */ 322#define CHECK_IOC_SIZE(structure) \ 323 if (_IOC_SIZE(iocmd) != sizeof(struct structure)) { \ 324 tpqputs(TPQD_ALWAYS, "sizeof(struct " #structure \ 325 ") does not match!"); \ 326 return -EFAULT; \ 327 } \ 328 329static void tpqputs(unsigned long flags, const char *s) 330{ 331 if ((flags & TPQD_ALWAYS) || (flags & QIC02_TAPE_DEBUG)) 332 printk(TPQIC02_NAME ": %s\n", s); 333} /* tpqputs */ 334 335 336static inline void byte_swap_w(volatile unsigned short *w) 337{ 338 int t = *w; 339 *w = (t >> 8) | ((t & 0xff) << 8); 340} 341 342 343 344/* Init control register bits on interface card. 345 * For Archive, interrupts must be enabled explicitly. 346 * Wangtek interface card requires ONLINE to be set, Archive SC402/SC499R 347 * cards keep it active all the time. 348 */ 349static void ifc_init(void) 350{ 351 if (QIC02_TAPE_IFC == WANGTEK) { /* || (QIC02_TAPE_IFC == EVEREX) */ 352 ctlbits = WT_CTL_ONLINE; /* online */ 353 outb_p(ctlbits, QIC02_CTL_PORT); 354 } else if (QIC02_TAPE_IFC == ARCHIVE) { 355 ctlbits = 0; /* no interrupts yet */ 356 outb_p(ctlbits, QIC02_CTL_PORT); 357 outb_p(0, AR_RESET_DMA_PORT); /* dummy write to reset DMA */ 358 } else { /* MOUNTAIN */ 359 360 ctlbits = MTN_CTL_ONLINE; /* online, and logic enabled */ 361 outb_p(ctlbits, QIC02_CTL_PORT); 362 } 363} /* ifc_init */ 364 365 366static void report_qic_exception(unsigned n) 367{ 368 if (n >= NR_OF_EXC) { 369 tpqputs(TPQD_ALWAYS, "Oops -- report_qic_exception"); 370 n = 0; 371 } 372 if (TPQDBG(SENSE_TEXT) || n == 0) { 373 printk(TPQIC02_NAME ": sense: %s\n", 374 exception_list[n].msg); 375 } 376} /* report_qic_exception */ 377 378 379/* Try to map the drive-exception bits `s' to a predefined "exception number", 380 * by comparing the significant exception bits for each entry in the 381 * exception table (`exception_list[]'). 382 * It is assumed that s!=0. 383 */ 384static int decode_qic_exception_nr(unsigned s) 385{ 386 int i; 387 388 for (i = 1; i < NR_OF_EXC; i++) { 389 if ((s & exception_list[i].mask) == exception_list[i].code) { 390 return i; 391 } 392 } 393 printk(TPQIC02_NAME 394 ": decode_qic_exception_nr: exception(%x) not recognized\n", 395 s); 396 return 0; 397} /* decode_qic_exception_nr */ 398 399 400 401/* Perform appropriate action for certain exceptions. 402 * should return a value to indicate stop/continue (in case of bad blocks) 403 */ 404static void handle_qic_exception(int exnr, int exbits) 405{ 406 if (exnr == EXC_NCART) { 407 /* Cartridge was changed. Redo sense(). 408 * EXC_NCART should be handled in open(). 409 * It is not permitted to remove the tape while 410 * the tape driver has open files. 411 */ 412 need_rewind = YES; 413 status_eof_detected = NO; 414 status_eom_detected = NO; 415 } else if (exnr == EXC_XFILLER) { 416 tpqputs(TPQD_ALWAYS, 417 "[Bad block -- filler data transferred.]"); 418 } else if (exnr == EXC_XBAD) { 419 tpqputs(TPQD_ALWAYS, "[CRC failed!]"); 420 } else if (exnr == EXC_MARGINAL) { 421 /* A marginal block behaves much like a FM. 422 * User may continue reading, if desired. 423 */ 424 tpqputs(TPQD_ALWAYS, "[Marginal block]"); 425 doing_read = NO; 426 } else if (exnr == EXC_FM) { 427 doing_read = NO; 428 } 429} /* handle_qic_exception */ 430 431 432static inline int is_exception(void) 433{ 434 return (inb(QIC02_STAT_PORT) & QIC02_STAT_EXCEPTION) == 0; 435} /* is_exception */ 436 437 438/* Reset the tape drive and controller. 439 * When reset fails, it marks the drive as dead and all 440 * requests (except reset) are to be ignored (ENXIO). 441 */ 442static int tape_reset(int verbose) 443{ 444 ifc_init(); /* reset interface card */ 445 446 /* assert reset */ 447 if (QIC02_TAPE_IFC == MOUNTAIN) { 448 outb_p(ctlbits & ~MTN_QIC02_CTL_RESET_NOT, QIC02_CTL_PORT); 449 } else { /* WANGTEK, ARCHIVE */ 450 451 outb_p(ctlbits | QIC02_CTL_RESET, QIC02_CTL_PORT); 452 } 453 454 /* Next, we need to wait >=25 usec. */ 455 udelay(30); 456 457 /* after reset, we will be at BOT (modulo an automatic rewind) */ 458 status_eof_detected = NO; 459 status_eom_detected = NO; 460 status_cmd_pending = 0; 461 need_rewind = YES; 462 doing_read = doing_write = NO; 463 ioctl_status.mt_fileno = ioctl_status.mt_blkno = 0; 464 465 /* de-assert reset */ 466 if (QIC02_TAPE_IFC == MOUNTAIN) { 467 outb_p(ctlbits | MTN_QIC02_CTL_RESET_NOT, QIC02_CTL_PORT); 468 } else { 469 outb_p(ctlbits & ~QIC02_CTL_RESET, QIC02_CTL_PORT); 470 } 471 472 /* KLUDGE FOR G++ BUG */ 473 { 474 int stat = inb_p(QIC02_STAT_PORT); 475 status_dead = 476 ((stat & QIC02_STAT_RESETMASK) != QIC02_STAT_RESETVAL); 477 } 478 /* if successful, inb(STAT) returned RESETVAL */ 479 if (status_dead == YES) { 480 printk(TPQIC02_NAME ": reset failed!\n"); 481 } else if (verbose) { 482 printk(TPQIC02_NAME ": reset successful\n"); 483 } 484 485 return (status_dead == YES) ? TE_DEAD : TE_OK; 486} /* tape_reset */ 487 488 489 490/* Notify tape drive of a new command. It only waits for the 491 * command to be accepted, not for the actual command to complete. 492 * 493 * Before calling this routine, QIC02_CMD_PORT must have been loaded 494 * with the command to be executed. 495 * After this routine, the exception bit must be checked. 496 * This routine is also used by rdstatus(), so in that case, any exception 497 * must be ignored (`ignore_ex' flag). 498 */ 499static int notify_cmd(char cmd, short ignore_ex) 500{ 501 int i; 502 503 outb_p(cmd, QIC02_CMD_PORT); /* output the command */ 504 505 /* wait 1 usec before asserting /REQUEST */ 506 udelay(1); 507 508 if ((!ignore_ex) && is_exception()) { 509 tpqputs(TPQD_ALWAYS, "*** exception detected in notify_cmd"); 510 /** force a reset here **/ 511 if (tape_reset(1) == TE_DEAD) 512 return TE_DEAD; 513 if (is_exception()) { 514 tpqputs(TPQD_ALWAYS, "exception persists after reset."); 515 tpqputs(TPQD_ALWAYS, " ^ exception ignored."); 516 } 517 } 518 519 outb_p(ctlbits | QIC02_CTL_REQUEST, QIC02_CTL_PORT); /* set request bit */ 520 i = TAPE_NOTIFY_TIMEOUT; 521 /* The specs say this takes about 500 usec, but there is no upper limit! 522 * If the drive were busy retensioning or something like that, 523 * it could be *much* longer! 524 */ 525 while ((inb_p(QIC02_STAT_PORT) & QIC02_STAT_READY) && (--i > 0)) 526 /*skip */ ; 527 /* wait for ready */ 528 if (i == 0) { 529 tpqputs(TPQD_ALWAYS, 530 "timed out waiting for ready in notify_cmd"); 531 status_dead = YES; 532 return TE_TIM; 533 } 534 535 outb_p(ctlbits & ~QIC02_CTL_REQUEST, QIC02_CTL_PORT); /* reset request bit */ 536 i = TAPE_NOTIFY_TIMEOUT; 537 /* according to the specs this one should never time-out */ 538 while (((inb_p(QIC02_STAT_PORT) & QIC02_STAT_READY) == 0) && (--i > 0)) 539 /*skip */ ; 540 /* wait for not ready */ 541 if (i == 0) { 542 tpqputs(TPQD_ALWAYS, "timed out waiting for !ready in notify_cmd"); 543 status_dead = YES; 544 return TE_TIM; 545 } 546 /* command accepted */ 547 return TE_OK; 548} /* notify_cmd */ 549 550 551 552/* Wait for a command to complete, with timeout */ 553static int wait_for_ready(time_t timeout) 554{ 555 int stat; 556 time_t spin_t; 557 558 /* Wait for ready or exception, without driving the loadavg up too much. 559 * In most cases, the tape drive already has READY asserted, 560 * so optimize for that case. 561 * 562 * First, busy wait a few usec: 563 */ 564 spin_t = 50; 565 while (((stat = inb_p(QIC02_STAT_PORT) & QIC02_STAT_MASK) == QIC02_STAT_MASK) && (--spin_t > 0)) 566 /*SKIP*/; 567 if ((stat & QIC02_STAT_READY) == 0) 568 return TE_OK; /* covers 99.99% of all calls */ 569 570 /* Then use schedule() a few times */ 571 spin_t = 3; /* max 0.03 sec busy waiting */ 572 if (spin_t > timeout) 573 spin_t = timeout; 574 timeout -= spin_t; 575 spin_t += jiffies; 576 577 while (((stat = inb_p(QIC02_STAT_PORT) & QIC02_STAT_MASK) == QIC02_STAT_MASK) 578 && time_before(jiffies, spin_t)) 579 schedule(); /* don't waste all the CPU time */ 580 if ((stat & QIC02_STAT_READY) == 0) 581 return TE_OK; 582 583 /* If we reach this point, we probably need to wait much longer, or 584 * an exception occurred. Either case is not very time-critical. 585 * Check the status port only a few times every second. 586 * A interval of less than 0.10 sec will not be noticed by the user, 587 * more than 0.40 sec may give noticeable delays. 588 */ 589 spin_t += timeout; 590 TPQDEB({printk("wait_for_ready: additional timeout: %d\n", spin_t);}) 591 592 /* not ready and no exception && timeout not expired yet */ 593 while (((stat = inb_p(QIC02_STAT_PORT) & QIC02_STAT_MASK) == QIC02_STAT_MASK) && time_before(jiffies, spin_t)) { 594 /* be `nice` to other processes on long operations... */ 595 current->state = TASK_INTERRUPTIBLE; 596 /* nap 0.30 sec between checks, */ 597 /* but could be woken up earlier by signals... */ 598 schedule_timeout(3 * HZ / 10); 599 } 600 601 /* don't use jiffies for this test because it may have changed by now */ 602 if ((stat & QIC02_STAT_MASK) == QIC02_STAT_MASK) { 603 tpqputs(TPQD_ALWAYS, "wait_for_ready() timed out"); 604 return TE_TIM; 605 } 606 607 if ((stat & QIC02_STAT_EXCEPTION) == 0) { 608 tpqputs(TPQD_ALWAYS, 609 "exception detected after waiting_for_ready"); 610 return TE_EX; 611 } else { 612 return TE_OK; 613 } 614} /* wait_for_ready */ 615 616 617 618/* Send some data to the drive */ 619static int send_qic02_data(char sb[], unsigned size, int ignore_ex) 620{ 621 int i, stat; 622 623 for (i = 0; i < size; i++) { 624 625 stat = wait_for_ready(TIM_S); 626 if (stat != TE_OK) 627 return stat; 628 629 stat = notify_cmd(sb[i], ignore_ex); 630 if (stat != TE_OK) 631 return stat; 632 } 633 return TE_OK; 634 635} /* send_qic02_data */ 636 637 638/* Send a QIC-02 command (`cmd') to the tape drive, with 639 * a time-out (`timeout'). 640 * This one is also used by tp_sense(), so we must have 641 * a flag to disable exception checking (`ignore_ex'). 642 * 643 * On entry, the controller is supposed to be READY. 644 */ 645static int send_qic02_cmd(int cmd, time_t timeout, int ignore_ex) 646{ 647 int stat; 648 649 stat = inb_p(QIC02_STAT_PORT); 650 if ((stat & QIC02_STAT_EXCEPTION) == 0) { /* if exception */ 651 tpqputs(TPQD_ALWAYS, "send_qic02_cmd: Exception!"); 652 return TE_EX; 653 } 654 if (stat & QIC02_STAT_READY) { /* if not ready */ 655 tpqputs(TPQD_ALWAYS, "send_qic02_cmd: not Ready!"); 656 return TE_ERR; 657 } 658 659 /* assert(ready & !exception) */ 660 661 /* Remember current command for later re-use with dma transfers. 662 * (For reading/writing multiple blocks.) 663 */ 664 status_cmd_pending = cmd; 665 666 stat = notify_cmd(cmd, ignore_ex); /* tell drive new command was loaded, */ 667 /* inherit exception check. */ 668 if (TP_HAVE_SEEK && (cmd == AR_QCMDV_SEEK_BLK)) { 669 /* This one needs to send 3 more bytes, MSB first */ 670 stat = send_qic02_data(seek_addr_buf, sizeof(seek_addr_buf), ignore_ex); 671 } 672 673 if (stat != TE_OK) { 674 tpqputs(TPQD_ALWAYS, "send_qic02_cmd failed"); 675 } 676 return stat; 677} /* send_qic02_cmd */ 678 679 680 681/* Get drive status. Assume drive is ready or has exception set. 682 * (or will be in <1000 usec.) 683 * Extra parameters added because of 'Read Extended Status 3' command. 684 */ 685static int rdstatus(char *stp, unsigned size, char qcmd) 686{ 687 int s, n; 688 char *q = stp; 689 690 /* Try to busy-wait a few (700) usec, after that de-schedule. 691 * 692 * The problem is, if we don't de-schedule, performance will 693 * drop to zero when the drive is not responding and if we 694 * de-schedule immediately, we waste a lot of time because a 695 * task switch is much longer than we usually have to wait here. 696 */ 697 n = 1000; /* 500 is not enough on a 486/33 */ 698 while ((n > 0) && ((inb_p(QIC02_STAT_PORT) & QIC02_STAT_MASK) == QIC02_STAT_MASK)) 699 n--; /* wait for ready or exception or timeout */ 700 if (n == 0) { 701 /* n (above) should be chosen such that on your machine 702 * you rarely ever see the message below, and it should 703 * be small enough to give reasonable response time.] 704 */ 705 tpqputs(TPQD_ALWAYS, "waiting looong in rdstatus() -- drive dead?"); 706 while ((inb_p(QIC02_STAT_PORT) & QIC02_STAT_MASK) == QIC02_STAT_MASK) 707 schedule(); 708 tpqputs(TPQD_ALWAYS, "finished waiting in rdstatus()"); 709 } 710 711 (void) notify_cmd(qcmd, 1); /* send read status command */ 712 /* ignore return code -- should always be ok, STAT may contain 713 * exception flag from previous exception which we are trying to clear. 714 */ 715 716 if (TP_DIAGS(current_tape_dev)) 717 printk(TPQIC02_NAME ": reading status bytes: "); 718 719 for (q = stp; q < stp + size; q++) { 720 do 721 s = inb_p(QIC02_STAT_PORT); 722 while ((s & QIC02_STAT_MASK) == QIC02_STAT_MASK); /* wait for ready or exception */ 723 724 if ((s & QIC02_STAT_EXCEPTION) == 0) { /* if exception */ 725 tpqputs(TPQD_ALWAYS, "rdstatus: exception error"); 726 ioctl_status.mt_erreg = 0; /* dunno... */ 727 return TE_NS; /* error, shouldn't happen... */ 728 } 729 730 *q = inb_p(QIC02_DATA_PORT); /* read status byte */ 731 732 if (TP_DIAGS(current_tape_dev)) 733 printk("[%1d]=0x%x ", q - stp, 734 (unsigned) (*q) & 0xff); 735 736 outb_p(ctlbits | QIC02_CTL_REQUEST, QIC02_CTL_PORT); /* set request */ 737 738 while ((inb_p(QIC02_STAT_PORT) & QIC02_STAT_READY) == 0); /* wait for not ready */ 739 740 udelay(22); /* delay >20 usec */ 741 742 outb_p(ctlbits & ~QIC02_CTL_REQUEST, QIC02_CTL_PORT); /* un-set request */ 743 744 } 745 746 /* Specs say we should wait for READY here. 747 * My drive doesn't seem to need it here yet, but others do? 748 */ 749 while (inb_p(QIC02_STAT_PORT) & QIC02_STAT_READY) 750 /*skip */ ; 751 /* wait for ready */ 752 753 if (TP_DIAGS(current_tape_dev)) 754 printk("\n"); 755 756 return TE_OK; 757} /* rdstatus */ 758 759 760 761/* Get standard status (6 bytes). 762 * The `.dec' and `.urc' fields are in MSB-first byte-order, 763 * so they have to be swapped first. 764 */ 765static int get_status(volatile struct tpstatus *stp) 766{ 767 int stat = rdstatus((char *) stp, TPSTATSIZE, QCMD_RD_STAT); 768#if defined(__i386__) || defined(__x86_64__) 769 byte_swap_w(&(stp->dec)); 770 byte_swap_w(&(stp->urc)); 771#else 772#warning Undefined architecture 773 /* should probably swap status bytes #definition */ 774#endif 775 return stat; 776} /* get_status */ 777 778 779 780 781/* Read drive status and set generic status too. 782 * NOTE: Once we do a tp_sense(), read/write transfers are killed. 783 */ 784static int tp_sense(int ignore) 785{ 786 unsigned err = 0, exnr = 0, gs = 0; 787 static void finish_rw(int cmd); 788 789 if (TPQDBG(SENSE_TEXT)) 790 printk(TPQIC02_NAME ": tp_sense(ignore=0x%x) enter\n", 791 ignore); 792 793 /* sense() is not allowed during a read or write cycle */ 794 if (doing_write == YES) 795 tpqputs(TPQD_ALWAYS, "Warning: File Mark inserted because of sense() request"); 796 /* The extra test is to avoid calling finish_rw during booting */ 797 if ((doing_read != NO) || (doing_write != NO)) 798 finish_rw(QCMD_RD_STAT); 799 800 if (get_status(&tperror) != TE_OK) { 801 tpqputs(TPQD_ALWAYS, "tp_sense: could not read tape drive status"); 802 return TE_ERR; 803 } 804 805 err = tperror.exs; /* get exception status bits */ 806 if (err & (TP_ST0 | TP_ST1)) 807 printk(TPQIC02_NAME ": tp_sense: status: %x, error count: %d, underruns: %d\n", 808 tperror.exs, tperror.dec, tperror.urc); 809 else if ((tperror.dec != 0) || (tperror.urc != 0) 810 || TPQDBG(SENSE_CNTS)) 811 printk(TPQIC02_NAME 812 ": tp_sense: no hard errors, soft error count: %d, underruns: %d\n", 813 tperror.dec, tperror.urc); 814 815 /* Set generic status. HP-UX defines these, but some extra would 816 * be useful. Problem is to remain compatible. [Do we want to be 817 * compatible??] 818 */ 819 if (err & TP_ST0) { 820 if (err & TP_CNI) /* no cartridge */ 821 gs |= GMT_DR_OPEN(-1); 822 if (status_dead == NO) 823 gs |= GMT_ONLINE(-1); /* always online */ 824 if (err & TP_USL) /* not online */ 825 gs &= ~GMT_ONLINE(-1); 826 if (err & TP_WRP) 827 gs |= GMT_WR_PROT(-1); 828 if (err & TP_EOM) { /* end of media */ 829 gs |= GMT_EOT(-1); /* not sure this is correct for writes */ 830 status_eom_detected = YES; 831 /* I don't know whether drive always reports EOF at or before EOM. */ 832 status_eof_detected = YES; 833 } 834 /** if (err & TP_UDA) "Unrecoverable data error" **/ 835 /** if (err & TP_BNL) "Bad block not located" **/ 836 if (err & TP_FIL) { 837 gs |= GMT_EOF(-1); 838 status_eof_detected = YES; 839 } 840 } 841 if (err & TP_ST1) { 842 /** if (err & TP_ILL) "Illegal command" **/ 843 /** if (err & TP_NDT) "No data detected" **/ 844 /** if (err & TP_MBD) "Marginal block detected" **/ 845 if (err & TP_BOM) 846 gs |= GMT_BOT(-1); /* beginning of tape */ 847 } 848 ioctl_status.mt_gstat = gs; 849 ioctl_status.mt_dsreg = tperror.exs; /* "drive status" */ 850 ioctl_status.mt_erreg = tperror.dec; /* "sense key error" */ 851 852 if (err & (TP_ST0 | TP_ST1)) { 853 /* My Wangtek occasionally reports `status' 1212 which should be ignored. */ 854 exnr = decode_qic_exception_nr(err); 855 handle_qic_exception(exnr, err); /* update driver state wrt drive status */ 856 report_qic_exception(exnr); 857 } 858 err &= ~ignore; /* mask unwanted errors -- not the correct way, use exception nrs?? */ 859 if (((err & TP_ST0) && (err & REPORT_ERR0)) || 860 ((err & TP_ST1) && (err & REPORT_ERR1))) 861 return TE_ERR; 862 return TE_OK; 863} /* tp_sense */ 864 865 866 867/* Wait for a wind or rewind operation to finish or 868 * to time-out. (May take very long). 869 */ 870static int wait_for_rewind(time_t timeout) 871{ 872 int stat; 873 874 stat = inb(QIC02_STAT_PORT) & QIC02_STAT_MASK; 875 if (TPQDBG(REWIND)) 876 printk(TPQIC02_NAME 877 ": Waiting for (re-)wind to finish: stat=0x%x\n", 878 stat); 879 880 stat = wait_for_ready(timeout); 881 882 if (stat != TE_OK) { 883 tpqputs(TPQD_ALWAYS, "(re-) winding failed\n"); 884 } 885 return stat; 886} /* wait_for_rewind */ 887 888 889 890/* Perform a full QIC02 command, and wait for completion, 891 * check status when done. Complain about exceptions. 892 * 893 * This function should return an OS error code when 894 * something goes wrong, 0 otherwise. 895 */ 896static int ll_do_qic_cmd(int cmd, time_t timeout) 897{ 898 int stat; 899 900 if (status_dead == YES) { 901 tpqputs(TPQD_ALWAYS, "Drive is dead. Do a `mt reset`."); 902 return -ENXIO; /* User should do an MTRESET. */ 903 } 904 905 stat = wait_for_ready(timeout); /* wait for ready or exception */ 906 if (stat == TE_EX) { 907 if (tp_sense(TP_WRP | TP_BOM | TP_EOM | TP_FIL) != TE_OK) 908 return -EIO; 909 /* else nothing to worry about, I hope */ 910 stat = TE_OK; 911 } 912 if (stat != TE_OK) { 913 printk(TPQIC02_NAME ": ll_do_qic_cmd(%x, %ld) failed\n", 914 cmd, (long) timeout); 915 return -EIO; 916 } 917#if OBSOLETE 918 /* wait for ready since it may not be active immediately after reading status */ 919 while ((inb_p(QIC02_STAT_PORT) & QIC02_STAT_READY) != 0); 920#endif 921 922 stat = send_qic02_cmd(cmd, timeout, 0); /* (checks for exceptions) */ 923 924 if (cmd == QCMD_RD_FM) { 925 status_eof_detected = NO; 926 ioctl_status.mt_fileno++; 927 /* Should update block count as well, but can't. 928 * Can do a `read address' for some drives, when MTNOP is done. 929 */ 930 } else if (cmd == QCMD_WRT_FM) { 931 status_eof_detected = NO; 932 ioctl_status.mt_fileno++; 933 } else if ((cmd == QCMD_REWIND) || (cmd == QCMD_ERASE) 934 || (cmd == QCMD_RETEN)) { 935 status_eof_detected = NO; 936 status_eom_detected = NO; 937 status_eot_detected = NO; 938 need_rewind = NO; 939 ioctl_status.mt_fileno = ioctl_status.mt_blkno = 0; 940 extra_blocks_left = BLOCKS_BEYOND_EW; 941 return_write_eof = NO; 942 return_read_eof = NO; 943 reported_read_eof = NO; 944 reported_write_eof = NO; 945 } 946 /* sense() will set eof/eom as required */ 947 if (stat == TE_EX) { 948 if (tp_sense(TP_WRP | TP_BOM | TP_EOM | TP_FIL) != TE_OK) { 949 printk(TPQIC02_NAME 950 ": Exception persist in ll_do_qic_cmd[1](%x, %ld)", 951 cmd, (long) timeout); 952 status_dead = YES; 953 return -ENXIO; 954 /* if rdstatus fails too, we're in trouble */ 955 } 956 } else if (stat != TE_OK) { 957 printk(TPQIC02_NAME 958 ": ll_do_qic_cmd: send_qic02_cmd failed, stat = 0x%x\n", 959 stat); 960 return -EIO; /*** -EIO is probably not always appropriate */ 961 } 962 963 964 if (timeout == TIM_R) 965 stat = wait_for_rewind(timeout); 966 else 967 stat = wait_for_ready(timeout); 968 969 if (stat == TE_EX) { 970 if (tp_sense((cmd == QCMD_SEEK_EOD ? /*****************************/ 971 TP_EOR | TP_NDT | TP_UDA | TP_BNL | TP_WRP | 972 TP_BOM | TP_EOM | TP_FIL : TP_WRP | TP_BOM | 973 TP_EOM | TP_FIL)) != TE_OK) { 974 printk(TPQIC02_NAME 975 ": Exception persist in ll_do_qic_cmd[2](%x, %ld)\n", 976 cmd, (long) timeout); 977 if (cmd != QCMD_RD_FM) 978 status_dead = YES; 979 return -ENXIO; 980 /* if rdstatus fails too, we're in trouble */ 981 } 982 } else if (stat != TE_OK) { 983 printk(TPQIC02_NAME 984 ": ll_do_qic_cmd %x: wait failed, stat == 0x%x\n", 985 cmd, stat); 986 return -EIO; 987 } 988 return 0; 989} /* ll_do_qic_cmd */ 990 991 992/* 993 * Problem: What to do when the user cancels a read/write operation 994 * in-progress? 995 * 996 * "Deactivating ONLINE during a READ also causes the" 997 * "tape to be rewound to BOT." Ditto for WRITEs, except 998 * a FM is written first. "The host may alternatively terminate 999 * the READ/WRITE command by issuing a RFM/WFM command." 1000 * 1001 * For READs: 1002 * Neither option will leave the tape positioned where it was. 1003 * Another (better?) solution is to terminate the READ by two 1004 * subsequent sense() operations, the first to stop the current 1005 * READ cycle, the second to clear the `Illegal command' exception, 1006 * because the QIC-02 specs didn't anticipate this. This is 1007 * delayed until actually needed, so a tar listing can be aborted 1008 * by the user and continued later. 1009 * If anybody has a better solution, let me know! [Also, let me 1010 * know if your drive (mine is a Wangtek5150EQ) does not accept 1011 * this sequence for canceling the read-cycle.] 1012 * 1013 * For WRITEs it's simple: Just do a WRITE_FM, leaving the tape 1014 * positioned after the FM. 1015 */ 1016 1017static void terminate_read(int cmd) 1018{ 1019 if (doing_read == YES) { 1020 doing_read = NO; 1021 if (cmd != QCMD_RD_FM) { 1022 /* if the command is a RFM, there is no need to do this 1023 * because a RFM will legally terminate the read-cycle. 1024 */ 1025 tpqputs(TPQD_ALWAYS, "terminating pending read-cycle"); 1026 1027 /* I'm not too sure about this part -- hhb */ 1028 if (QIC02_TAPE_IFC == MOUNTAIN) { 1029 /* Mountain reference says can terminate by de-asserting online */ 1030 ctlbits &= ~MTN_QIC02_CTL_ONLINE; 1031 } 1032 1033 if (tp_sense(TP_FIL | TP_EOM | TP_WRP) != TE_OK) { 1034 tpqputs(TPQD_ALWAYS, 1035 "finish_rw[read1]: ignore the 2 lines above"); 1036 if (is_exception()) { 1037 if (tp_sense 1038 (TP_ILL | TP_FIL | TP_EOM | 1039 TP_WRP) != TE_OK) 1040 tpqputs(TPQD_ALWAYS,"finish_rw[read2]: read cycle error"); 1041 } 1042 } 1043 } 1044 } 1045} /* terminate_read */ 1046 1047 1048static void terminate_write(int cmd) 1049{ 1050 int stat; 1051 1052 if (doing_write == YES) { 1053 doing_write = NO; 1054 /* Finish writing by appending a FileMark at the end. */ 1055 if (cmd != QCMD_WRT_FM) { 1056 /* finish off write cycle */ 1057 stat = ll_do_qic_cmd(QCMD_WRT_FM, TIM_M); 1058 if (stat != TE_OK) 1059 tpqputs(TPQD_ALWAYS, 1060 "Couldn't finish write cycle properly"); 1061 (void) tp_sense(0); 1062 } 1063 /* If there is an EOF token waiting to be returned to 1064 * the (writing) application, discard it now. 1065 * We could be at EOT, so don't reset return_write_eof. 1066 */ 1067 reported_write_eof = YES; 1068 } 1069} /* terminate_write */ 1070 1071 1072/* terminate read or write cycle because of command `cmd' */ 1073static void finish_rw(int cmd) 1074{ 1075 if (wait_for_ready(TIM_S) != TE_OK) { 1076 tpqputs(TPQD_ALWAYS, 1077 "error: drive not ready in finish_rw() !"); 1078 return; 1079 } 1080 terminate_read(cmd); 1081 terminate_write(cmd); 1082} /* finish_rw */ 1083 1084 1085/* Perform a QIC command through ll_do_qic_cmd(). 1086 * If necessary, rewind the tape first. 1087 * Return an OS error code if something goes wrong, 0 if all is well. 1088 */ 1089static int do_qic_cmd(int cmd, time_t timeout) 1090{ 1091 int stat; 1092 1093 1094 finish_rw(cmd); 1095 1096 if (need_rewind) { 1097 tpqputs(TPQD_REWIND, "Rewinding tape..."); 1098 stat = ll_do_qic_cmd(QCMD_REWIND, TIM_R); 1099 if (stat != 0) { 1100 printk(TPQIC02_NAME ": rewind failed in do_qic_cmd(). stat=0x%2x", stat); 1101 return stat; 1102 } 1103 need_rewind = NO; 1104 if (cmd == QCMD_REWIND) /* don't wind beyond BOT ;-) */ 1105 return 0; 1106 } 1107 1108 return ll_do_qic_cmd(cmd, timeout); 1109} /* do_qic_cmd */ 1110 1111 1112/* Not all ioctls are supported for all drives. Some rely on 1113 * optional QIC-02 commands. Check tpqic02.h for configuration. 1114 * Some of these commands may require ONLINE to be active. 1115 */ 1116static int do_ioctl_cmd(int cmd) 1117{ 1118 int stat; 1119 1120 /* It is not permitted to read or wind the tape after bytes have 1121 * been written. It is not permitted to write the tape while in 1122 * read mode. 1123 * We try to be kind and allow reading again after writing a FM... 1124 */ 1125 1126 switch (cmd) { 1127 case MTRESET: 1128 /* reset verbose */ 1129 return (tape_reset(1) == TE_OK) ? 0 : -EIO; 1130 1131 case MTFSF: 1132 tpqputs(TPQD_IOCTLS, "MTFSF forward searching filemark"); 1133 if ((mode_access == WRITE) && status_bytes_wr) 1134 return -EACCES; 1135 return do_qic_cmd(QCMD_RD_FM, TIM_F); 1136 1137 case MTBSF: 1138 if (TP_HAVE_BSF) { 1139 tpqputs(TPQD_IOCTLS, 1140 "MTBSF backward searching filemark -- optional command"); 1141 if ((mode_access == WRITE) && status_bytes_wr) 1142 return -EACCES; 1143 stat = do_qic_cmd(QCMD_RD_FM_BCK, TIM_F); 1144 } else { 1145 stat = -ENXIO; 1146 } 1147 status_eom_detected = status_eof_detected = NO; 1148 return stat; 1149 1150 case MTFSR: 1151 if (TP_HAVE_FSR) { /* This is an optional QIC-02 command */ 1152 tpqputs(TPQD_IOCTLS, "MTFSR forward space record"); 1153 if ((mode_access == WRITE) && status_bytes_wr) 1154 return -EACCES; 1155 stat = do_qic_cmd(QCMD_SPACE_FWD, TIM_F); 1156 } else { 1157 /**** fake it by doing a read data block command? ******/ 1158 tpqputs(TPQD_IOCTLS, "MTFSR not supported"); 1159 stat = -ENXIO; 1160 } 1161 return stat; 1162 1163 case MTBSR: 1164 if (TP_HAVE_BSR) { /* This is an optional QIC-02 command */ 1165 /* we need this for appending files with GNU tar!! */ 1166 tpqputs(TPQD_IOCTLS, "MTFSR backward space record"); 1167 if ((mode_access == WRITE) && status_bytes_wr) 1168 return -EACCES; 1169 stat = do_qic_cmd(QCMD_SPACE_BCK, TIM_F); 1170 } else { 1171 tpqputs(TPQD_IOCTLS, "MTBSR not supported"); 1172 stat = -ENXIO; 1173 } 1174 status_eom_detected = status_eof_detected = NO; 1175 return stat; 1176 1177 case MTWEOF: 1178 tpqputs(TPQD_IOCTLS, "MTWEOF write eof mark"); 1179 /* Plain GNU mt(1) 2.2 uses read-only mode for writing FM. :-( */ 1180 if (mode_access == READ) 1181 return -EACCES; 1182 1183 /* allow tape movement after writing FM */ 1184 status_bytes_rd = status_bytes_wr; /* Kludge-O-Matic */ 1185 status_bytes_wr = NO; 1186 return do_qic_cmd(QCMD_WRT_FM, TIM_M); 1187 /* not sure what to do with status_bytes when WFM should fail */ 1188 1189 case MTREW: 1190 tpqputs(TPQD_IOCTLS, "MTREW rewinding tape"); 1191 if ((mode_access == WRITE) && status_bytes_wr) 1192 return -EACCES; 1193 status_eom_detected = status_eof_detected = NO; 1194 return do_qic_cmd(QCMD_REWIND, TIM_R); 1195 1196 case MTOFFL: 1197 tpqputs(TPQD_IOCTLS, "MTOFFL rewinding & going offline"); 1198 /* Doing a drive select will clear (unlock) the current drive. 1199 * But that requires support for multiple drives and locking. 1200 */ 1201 if ((mode_access == WRITE) && status_bytes_wr) 1202 return -EACCES; 1203 status_eom_detected = status_eof_detected = NO; 1204 /**** do rewind depending on minor bits??? ***/ 1205 stat = do_qic_cmd(QCMD_REWIND, TIM_R); 1206 return stat; 1207 1208 case MTNOP: 1209 tpqputs(TPQD_IOCTLS, "MTNOP setting status only"); 1210 /********** should do `read position' for drives that support it **********/ 1211 return (tp_sense(-1) == TE_OK) ? 0 : -EIO; /**** check return codes ****/ 1212 1213 case MTRETEN: 1214 tpqputs(TPQD_IOCTLS, "MTRETEN retension tape"); 1215 if ((mode_access == WRITE) && status_bytes_wr) 1216 return -EACCES; 1217 status_eom_detected = status_eof_detected = NO; 1218 return do_qic_cmd(QCMD_RETEN, TIM_R); 1219 1220 case MTBSFM: 1221 /* Think think is like MTBSF, except that 1222 * we shouldn't skip the FM. Tricky. 1223 * Maybe use RD_FM_BCK, then do a SPACE_FWD? 1224 */ 1225 tpqputs(TPQD_IOCTLS, "MTBSFM not supported"); 1226 if ((mode_access == WRITE) && status_bytes_wr) 1227 return -EACCES; 1228 return -ENXIO; 1229 1230 case MTFSFM: 1231 /* I think this is like MTFSF, except that 1232 * we shouldn't skip the FM. Tricky. 1233 * Maybe use QCMD_RD_DATA until we get a TP_FIL exception? 1234 * But then the FM will have been skipped... 1235 * Maybe use RD_FM, then RD_FM_BCK, but not all 1236 * drives will support that! 1237 */ 1238 tpqputs(TPQD_IOCTLS, "MTFSFM not supported"); 1239 if ((mode_access == WRITE) && status_bytes_wr) 1240 return -EACCES; 1241 return -ENXIO; 1242 1243 case MTEOM: 1244 /* This should leave the tape ready for appending 1245 * another file to the end, such that it would append 1246 * after the last FM on tape. 1247 */ 1248 tpqputs(TPQD_IOCTLS, "MTEOM search for End Of recorded Media"); 1249 if ((mode_access == WRITE) && status_bytes_wr) 1250 return -EACCES; 1251 if (TP_HAVE_EOD) { 1252 /* Use faster seeking when possible. 1253 * This requires the absence of data beyond the EOM. 1254 * It seems that my drive does not always perform the 1255 * SEEK_EOD correctly, unless it is preceded by a 1256 * rewind command. 1257 */ 1258 stat = do_qic_cmd(QCMD_REWIND, TIM_R); 1259 if (stat) 1260 return stat; 1261 stat = do_qic_cmd(QCMD_SEEK_EOD, TIM_F); 1262 /* After a successful seek, TP_EOR should be returned */ 1263 } else { 1264 /* else just seek until the drive returns exception "No Data" */ 1265 stat = 0; 1266 while ((stat == 0) && (!status_eom_detected)) { 1267 stat = do_qic_cmd(QCMD_RD_FM, TIM_F); /***** should use MTFSFM here???? ******/ 1268 } 1269 if (tperror.exs & TP_NDT) 1270 return 0; 1271 } 1272 return stat; 1273 1274 case MTERASE: 1275 tpqputs(TPQD_IOCTLS, "MTERASE -- ERASE TAPE !"); 1276 if ((tperror.exs & TP_ST0) && (tperror.exs & TP_WRP)) { 1277 tpqputs(TPQD_ALWAYS, "Cartridge is write-protected."); 1278 return -EACCES; 1279 } else { 1280 time_t t = jiffies; 1281 1282 /* Plain GNU mt(1) 2.2 erases a tape in O_RDONLY. :-( */ 1283 if (mode_access == READ) 1284 return -EACCES; 1285 1286 /* give user a few seconds to pull out tape */ 1287 while (jiffies - t < 4 * HZ) 1288 schedule(); 1289 } 1290 1291 /* don't bother writing filemark first */ 1292 status_eom_detected = status_eof_detected = NO; 1293 return do_qic_cmd(QCMD_ERASE, TIM_R); 1294 1295 case MTRAS1: 1296 if (TP_HAVE_RAS1) { 1297 tpqputs(TPQD_IOCTLS, "MTRAS1: non-destructive self test"); 1298 stat = do_qic_cmd(QCMD_SELF_TST1, TIM_R); 1299 if (stat != 0) { 1300 tpqputs(TPQD_ALWAYS, "RAS1 failed"); 1301 return stat; 1302 } 1303 return (tp_sense(0) == TE_OK) ? 0 : -EIO; /* get_ext_status3(); */ 1304 } 1305 tpqputs(TPQD_IOCTLS, "RAS1 not supported"); 1306 return -ENXIO; 1307 1308 case MTRAS2: 1309 if (TP_HAVE_RAS2) { 1310 tpqputs(TPQD_IOCTLS, "MTRAS2: destructive self test"); 1311 stat = do_qic_cmd(QCMD_SELF_TST2, TIM_R); 1312 if (stat != 0) { 1313 tpqputs(TPQD_ALWAYS, "RAS2 failed"); 1314 return stat; 1315 } 1316 return (tp_sense(0) == TE_OK) ? 0 : -EIO; /* get_ext_status3(); */ 1317 } 1318 tpqputs(TPQD_IOCTLS, "RAS2 not supported"); 1319 return -ENXIO; 1320 1321 case MTSEEK: 1322 if (TP_HAVE_SEEK && (QIC02_TAPE_IFC == ARCHIVE)) { 1323 tpqputs(TPQD_IOCTLS, "MTSEEK seeking block"); 1324 if ((mode_access == WRITE) && status_bytes_wr) 1325 return -EACCES; 1326 /* NOTE: address (24 bits) is in seek_addr_buf[] */ 1327 return do_qic_cmd(AR_QCMDV_SEEK_BLK, TIM_F); 1328 } else 1329 return -ENOTTY; 1330 1331 default: 1332 return -ENOTTY; 1333 } 1334} /* do_ioctl_cmd */ 1335 1336 1337/* dma_transfer(): This routine is called for every 512 bytes to be read 1338 * from/written to the tape controller. Speed is important here! 1339 * (There must be enough time left for the hd controller!) 1340 * When other devices use DMA they must ensure they use un-interruptible 1341 * double byte accesses to the DMA controller. Floppy.c is ok. 1342 * Must have interrupts disabled when this function is invoked, 1343 * otherwise, the double-byte transfers to the DMA controller will not 1344 * be atomic. That could lead to nasty problems when they are interrupted 1345 * by other DMA interrupt-routines. 1346 * 1347 * This routine merely does the least possible to keep 1348 * the transfers going: 1349 * - set the DMA count register for the next 512 bytes 1350 * - adjust the DMA address and page registers 1351 * - adjust the timeout 1352 * - tell the tape controller to start transferring 1353 * We assume the dma address and mode are, and remain, valid. 1354 */ 1355static inline void dma_transfer(void) 1356{ 1357 unsigned long flags; 1358 1359 if (QIC02_TAPE_IFC == WANGTEK) /* or EVEREX */ 1360 outb_p(WT_CTL_ONLINE, QIC02_CTL_PORT); /* back to normal */ 1361 else if (QIC02_TAPE_IFC == ARCHIVE) 1362 outb_p(0, AR_RESET_DMA_PORT); 1363 else /* QIC02_TAPE_IFC == MOUNTAIN */ 1364 outb_p(ctlbits, QIC02_CTL_PORT); 1365 1366 1367 flags = claim_dma_lock(); 1368 clear_dma_ff(QIC02_TAPE_DMA); 1369 set_dma_mode(QIC02_TAPE_DMA, dma_mode); 1370 set_dma_addr(QIC02_TAPE_DMA, 1371 virt_to_bus(buffaddr) + dma_bytes_done); 1372 set_dma_count(QIC02_TAPE_DMA, TAPE_BLKSIZE); 1373 1374 /* start tape DMA controller */ 1375 if (QIC02_TAPE_IFC == WANGTEK) /* or EVEREX */ 1376 outb_p(WT_CTL_DMA | WT_CTL_ONLINE, QIC02_CTL_PORT); /* trigger DMA transfer */ 1377 1378 else if (QIC02_TAPE_IFC == ARCHIVE) { 1379 outb_p(AR_CTL_IEN | AR_CTL_DNIEN, QIC02_CTL_PORT); /* enable interrupts again */ 1380 outb_p(0, AR_START_DMA_PORT); /* start DMA transfer */ 1381 /* In dma_end() AR_RESET_DMA_PORT is written too. */ 1382 1383 } else { /* QIC02_TAPE_IFC == MOUNTAIN */ 1384 1385 inb(MTN_R_DESELECT_DMA_PORT); 1386 outb_p(ctlbits | (MTN_CTL_EXC_IEN | MTN_CTL_DNIEN), 1387 QIC02_CTL_PORT); 1388 outb_p(0, MTN_W_SELECT_DMA_PORT); /* start DMA transfer */ 1389 if (dma_mode == DMA_MODE_WRITE) 1390 outb_p(0, MTN_W_DMA_WRITE_PORT); /* start DMA transfer */ 1391 } 1392 1393 /* start computer DMA controller */ 1394 enable_dma(QIC02_TAPE_DMA); 1395 1396 release_dma_lock(flags); 1397 1398 /* block transfer should start now, jumping to the 1399 * interrupt routine when done or an exception was detected. 1400 */ 1401} /* dma_transfer */ 1402 1403 1404/* start_dma() sets a DMA transfer up between the tape controller and 1405 * the kernel qic02_tape_buf buffer. 1406 * Normally bytes_todo==dma_bytes_done at the end of a DMA transfer. If not, 1407 * a filemark was read, or an attempt to write beyond the End Of Tape 1408 * was made. [Or some other bad thing happened.] 1409 * Must do a sense() before returning error. 1410 */ 1411static int start_dma(short mode, unsigned long bytes_todo) 1412/* assume 'bytes_todo'>0 */ 1413{ 1414 int stat; 1415 unsigned long flags; 1416 1417 tpqputs(TPQD_DEBUG, "start_dma() enter"); 1418 TPQDEB( {printk(TPQIC02_NAME ": doing_read==%d, doing_write==%d\n", 1419 doing_read, doing_write);}) 1420 1421 dma_bytes_done = 0; 1422 dma_bytes_todo = bytes_todo; 1423 status_error = NO; 1424 /* dma_mode!=0 indicates that the dma controller is in use */ 1425 dma_mode = (mode == WRITE) ? DMA_MODE_WRITE : DMA_MODE_READ; 1426 1427 /* Only give READ/WRITE DATA command to tape drive if we haven't 1428 * done that already. Otherwise the drive will rewind to the beginning 1429 * of the current file on tape. Any QIC command given other than 1430 * R/W FM will break the read/write transfer cycle. 1431 * do_qic_cmd() will terminate doing_{read,write} 1432 */ 1433 if ((doing_read == NO) && (doing_write == NO)) { 1434 /* First, we have to clear the status -- maybe remove TP_FIL??? 1435 */ 1436 1437 /* TP_CNI should now be handled in open(). -Hennus */ 1438 1439 stat = 1440 tp_sense(((mode == 1441 WRITE) ? 0 : TP_WRP) | TP_BOM | TP_FIL); 1442 if (stat != TE_OK) 1443 return stat; 1444 1445#if OBSOLETE 1446 /************* not needed iff rd_status() would wait for ready!!!!!! **********/ 1447 if (wait_for_ready(TIM_S) != TE_OK) { /*** not sure this is needed ***/ 1448 tpqputs(TPQD_ALWAYS, 1449 "wait_for_ready failed in start_dma"); 1450 return -EIO; 1451 } 1452#endif 1453 1454 if (QIC02_TAPE_IFC == MOUNTAIN) { 1455 /* Set control bits to select ONLINE during command */ 1456 ctlbits |= MTN_QIC02_CTL_ONLINE; 1457 } 1458 1459 /* Tell the controller the data direction */ 1460 1461 /* r/w, timeout medium, check exceptions, sets status_cmd_pending. */ 1462 stat = send_qic02_cmd((mode == WRITE) 1463 ? QCMD_WRT_DATA : QCMD_RD_DATA, TIM_M, 0); 1464 if (stat != TE_OK) { 1465 printk(TPQIC02_NAME ": start_dma: init %s failed\n", 1466 (mode == WRITE) ? "write" : "read"); 1467 (void) tp_sense(0); 1468 return stat; 1469 } 1470 1471 /* Do this last, because sense() will clear the doing_{read,write} 1472 * flags, causing trouble next time around. 1473 */ 1474 if (wait_for_ready(TIM_M) != TE_OK) 1475 return -EIO; 1476 switch (mode) { 1477 case READ: 1478 doing_read = YES; 1479 break; 1480 case WRITE: 1481 doing_write = YES; 1482 break; 1483 default: 1484 printk(TPQIC02_NAME 1485 ": requested unknown mode %d\n", mode); 1486 panic(TPQIC02_NAME 1487 ": invalid mode in start_dma()"); 1488 } 1489 1490 } else if (is_exception()) { 1491 /* This is for Archive drives, to handle reads with 0 bytes 1492 * left for the last read request. 1493 * 1494 * ******** this also affects EOF/EOT handling! ************ 1495 */ 1496 tpqputs(TPQD_ALWAYS, 1497 "detected exception in start_dma() while transfer in progress"); 1498 status_error = YES; 1499 return TE_END; 1500 } 1501 1502 1503 status_expect_int = YES; 1504 1505 /* This assumes tape is already positioned, but these 1506 * semi-'intelligent' drives are unpredictable... 1507 */ 1508 TIMERON(TIM_M * 2); 1509 1510 /* initiate first data block read from/write to the tape controller */ 1511 1512 save_flags(flags); 1513 cli(); 1514 dma_transfer(); 1515 restore_flags(flags); 1516 1517 TPQPUTS("start_dma() end"); 1518 return TE_OK; 1519} /* start_dma */ 1520 1521 1522/* This cleans up after the dma transfer has completed 1523 * (or failed). If an exception occurred, a sense() 1524 * must be done. If the exception was caused by a FM, 1525 * sense() will set `status_eof_detected' and 1526 * `status_eom_detected', as required. 1527 */ 1528static void end_dma(unsigned long *bytes_done) 1529{ 1530 int stat = TE_OK; 1531 unsigned long flags; 1532 1533 TIMEROFF; 1534 1535 TPQPUTS("end_dma() enter"); 1536 1537 flags = claim_dma_lock(); 1538 1539 disable_dma(QIC02_TAPE_DMA); 1540 clear_dma_ff(QIC02_TAPE_DMA); 1541 1542 release_dma_lock(flags); 1543 1544 if (QIC02_TAPE_IFC == WANGTEK) /* or EVEREX */ 1545 outb_p(WT_CTL_ONLINE, QIC02_CTL_PORT); /* back to normal */ 1546 else if (QIC02_TAPE_IFC == ARCHIVE) 1547 outb_p(0, AR_RESET_DMA_PORT); 1548 else { /* QIC02_TAPE_IFC == MOUNTAIN */ 1549 1550 /* Clear control bits, de-select ONLINE during tp_sense */ 1551 ctlbits &= ~MTN_QIC02_CTL_ONLINE; 1552 } 1553 1554 stat = wait_for_ready(TIM_M); 1555 if (status_error || (stat != TE_OK)) { 1556 tpqputs(TPQD_DMAX, "DMA transfer exception"); 1557 stat = tp_sense((dma_mode == READ) ? TP_WRP : 0); 1558 /* no return here -- got to clean up first! */ 1559 } else { /* if (QIC02_TAPE_IFC == MOUNTAIN) */ 1560 1561 outb_p(ctlbits, QIC02_CTL_PORT); 1562 } 1563 1564 if (QIC02_TAPE_IFC == MOUNTAIN) 1565 inb(MTN_R_DESELECT_DMA_PORT); 1566 1567 /* take the tape controller offline */ 1568 1569 /* finish off DMA stuff */ 1570 1571 1572 dma_mode = 0; 1573 /* Note: The drive is left on-line, ready for the next 1574 * data transfer. 1575 * If the next command to the drive does not continue 1576 * the pending cycle, it must do 2 sense()s first. 1577 */ 1578 1579 *bytes_done = dma_bytes_done; 1580 status_expect_int = NO; 1581 ioctl_status.mt_blkno += (dma_bytes_done / TAPE_BLKSIZE); 1582 1583 TPQPUTS("end_dma() exit"); 1584 /*** could return stat here ***/ 1585} /* end_dma */ 1586 1587/*********** Below are the (public) OS-interface procedures ***********/ 1588 1589 1590/* qic02_tape_times_out() is called when a DMA transfer doesn't complete 1591 * quickly enough. Usually this means there is something seriously wrong 1592 * with the hardware/software, but it could just be that the controller 1593 * has decided to do a long rewind, just when I didn't expect it. 1594 * Just try again. 1595 */ 1596static void qic02_tape_times_out(unsigned long dummy) 1597{ 1598 printk("time-out in %s driver\n", TPQIC02_NAME); 1599 if ((status_cmd_pending > 0) || dma_mode) { 1600 /* takes tooo long, shut it down */ 1601 status_dead = YES; 1602 status_cmd_pending = 0; 1603 status_timer_on = NO; 1604 status_expect_int = NO; 1605 status_error = YES; 1606 if (dma_mode) { 1607 dma_mode = 0; /* signal end to read/write routine */ 1608 wake_up(&qic02_tape_transfer); 1609 } 1610 } 1611} /* qic02_tape_times_out */ 1612 1613/* 1614 * Interrupt handling: 1615 * 1616 * 1) Interrupt is generated iff at the end of 1617 * a 512-DMA-block transfer. 1618 * 2) EXCEPTION is not raised unless something 1619 * is wrong or EOT/FM is detected. 1620 * 3) FM EXCEPTION is set *after* the last byte has 1621 * been transferred by DMA. By the time the interrupt 1622 * is handled, the EXCEPTION may already be set. 1623 * 1624 * So, 1625 * 1) On EXCEPTION, assume data has been transferred, so 1626 * continue as usual, but set a flag to indicate the 1627 * exception was detected. 1628 * Do a sense status when the flag is found set. 1629 * 2) Do not attempt to continue a transfer after an exception. 1630 * [??? What about marginal blocks???????] 1631 */ 1632 1633 1634/* qic02_tape_interrupt() is called when the tape controller completes 1635 * a DMA transfer. 1636 * We are not allowed to sleep here! 1637 * 1638 * Check if the transfer was successful, check if we need to transfer 1639 * more. If the buffer contains enough data/is empty enough, signal the 1640 * read/write() thread to copy to/from user space. 1641 * When we are finished, set flags to indicate end, disable timer. 1642 * NOTE: This *must* be fast! 1643 */ 1644static void qic02_tape_interrupt(int irq, void *dev_id, 1645 struct pt_regs *regs) 1646{ 1647 int stat, r, i; 1648 unsigned long flags; 1649 1650 TIMEROFF; 1651 1652 if (status_expect_int) { 1653#ifdef WANT_EXTRA_FULL_DEBUGGING 1654 if (TP_DIAGS(current_tape_dev)) 1655 printk("@"); 1656#endif 1657 stat = inb(QIC02_STAT_PORT); /* Knock, knock */ 1658 if (QIC02_TAPE_IFC == ARCHIVE) { /* "Who's there?" */ 1659 if (((stat & (AR_STAT_DMADONE)) == 0) && 1660 ((stat & (QIC02_STAT_EXCEPTION)) != 0)) { 1661 TIMERCONT; 1662 return; /* "Linux with IRQ sharing" */ 1663 } 1664 } 1665 1666 if ((stat & QIC02_STAT_EXCEPTION) == 0) { /* exception occurred */ 1667 /* Possible causes for an exception during a transfer: 1668 * - during a write-cycle: end of tape (EW) hole detected. 1669 * - during a read-cycle: filemark or EOD detected. 1670 * - something went wrong 1671 * So don't continue with the next block. 1672 */ 1673 tpqputs(TPQD_ALWAYS, 1674 "isr: exception on tape controller"); 1675 printk(" status %02x\n", stat); 1676 status_error = TE_EX; 1677 1678 dma_bytes_done += TAPE_BLKSIZE; 1679 1680 dma_mode = 0; /* wake up rw() */ 1681 status_expect_int = NO; 1682 wake_up(&qic02_tape_transfer); 1683 return; 1684 } 1685 /* return if tape controller not ready, or 1686 * if dma channel hasn't finished last byte yet. 1687 */ 1688 r = 0; 1689 1690 /* Skip next ready check for Archive controller because 1691 * it may be busy reading ahead. Weird. --hhb 1692 */ 1693 if (QIC02_TAPE_IFC == WANGTEK) /* I think this is a drive-dependency, not IFC -- hhb */ 1694 if (stat & QIC02_STAT_READY) { /* not ready */ 1695 tpqputs(TPQD_ALWAYS, 1696 "isr: ? Tape controller not ready"); 1697 r = 1; 1698 } 1699 1700 flags = claim_dma_lock(); 1701 1702 if ((i = get_dma_residue(QIC02_TAPE_DMA)) != 0) { 1703 printk(TPQIC02_NAME ": dma_residue == %x !!!\n", 1704 i); 1705 r = 1; /* big trouble, but can't do much about it... */ 1706 } 1707 1708 release_dma_lock(flags); 1709 1710 if (r) 1711 return; 1712 1713 /* finish DMA cycle */ 1714 1715 /* no errors detected, continue */ 1716 dma_bytes_done += TAPE_BLKSIZE; 1717 if (dma_bytes_done >= dma_bytes_todo) { 1718 /* finished! Wakeup rw() */ 1719 dma_mode = 0; 1720 status_expect_int = NO; 1721 TPQPUTS("isr: dma_bytes_done"); 1722 wake_up(&qic02_tape_transfer); 1723 } else { 1724 /* start next transfer, account for track-switching time */ 1725 mod_timer(&tp_timer, jiffies + 6 * HZ); 1726 dma_transfer(); 1727 } 1728 } else { 1729 printk(TPQIC02_NAME ": Unexpected interrupt, stat == %x\n", 1730 inb(QIC02_STAT_PORT)); 1731 } 1732} /* qic02_tape_interrupt */ 1733 1734 1735 1736/* 1737 * Problem: tar(1) doesn't always read the entire file. Sometimes the entire file 1738 * has been read, but the EOF token is never returned to tar(1), simply because 1739 * tar(1) knows it has already read all of the data it needs. So we must use 1740 * open/release to reset the `reported_read_eof' flag. If we don't, the next read 1741 * request would return the EOF flag for the previous file. 1742 */ 1743 1744static ssize_t qic02_tape_read(struct file *filp, char *buf, size_t count, 1745 loff_t * ppos) 1746{ 1747 kdev_t dev = filp->f_dentry->d_inode->i_rdev; 1748 unsigned short flags = filp->f_flags; 1749 unsigned long bytes_todo, bytes_done, total_bytes_done = 0; 1750 int stat; 1751 1752 if (status_zombie == YES) { 1753 tpqputs(TPQD_ALWAYS, "configs not set"); 1754 return -ENXIO; 1755 } 1756 1757 if (TP_DIAGS(current_tape_dev)) 1758 /* can't print a ``long long'' (for filp->f_pos), so chop it */ 1759 printk(TPQIC02_NAME 1760 ": request READ, minor=%x, buf=%p, count=%lx" 1761 ", pos=%lx, flags=%x\n", MINOR(dev), buf, 1762 (long) count, (unsigned long) filp->f_pos, flags); 1763 1764 if (count % TAPE_BLKSIZE) { /* Only allow mod 512 bytes at a time. */ 1765 tpqputs(TPQD_BLKSZ, "Wrong block size"); 1766 return -EINVAL; 1767 } 1768 1769 /* Just assume everything is ok. Controller will scream if not. */ 1770 1771 if (status_bytes_wr) { /* Once written, no more reads, 'till after WFM. */ 1772 return -EACCES; 1773 } 1774 1775 /* This is rather ugly because it has to implement a finite state 1776 * machine in order to handle the EOF situations properly. 1777 */ 1778 while ((signed) count >= 0) { 1779 bytes_done = 0; 1780 /* see how much fits in the kernel buffer */ 1781 bytes_todo = TPQBUF_SIZE; 1782 if (bytes_todo > count) { 1783 bytes_todo = count; 1784 } 1785 1786 /* Must ensure that user program sees exactly one EOF token (==0) */ 1787 if (return_read_eof == YES) { 1788 if (TPQDBG(DEBUG)) { 1789 printk 1790 ("read: return_read_eof==%d, reported_read_eof==%d, total_bytes_done==%lu\n", 1791 return_read_eof, reported_read_eof, 1792 total_bytes_done); 1793 } 1794 1795 if (reported_read_eof == NO) { 1796 /* have not yet returned EOF to user program */ 1797 if (total_bytes_done > 0) { 1798 return total_bytes_done; /* next time return EOF */ 1799 } else { 1800 reported_read_eof = YES; /* move on next time */ 1801 return 0; /* return EOF */ 1802 } 1803 } else { 1804 /* Application program has already received EOF 1805 * (above), now continue with next file on tape, 1806 * if possible. 1807 * When the FM is reached, EXCEPTION is set, 1808 * causing a sense(). Subsequent read/writes will 1809 * continue after the FM. 1810 */ 1811 /*********** ?????????? this should check for (EOD|NDT), not EOM, 'cause we can read past EW: ************/ 1812 if (status_eom_detected) { 1813 /* If EOM, nothing left to read, so keep returning EOFs. 1814 *** should probably set some flag to avoid clearing 1815 *** status_eom_detected through ioctls or something 1816 */ 1817 return 0; 1818 } else { 1819 /* just eof, there may be more files ahead... */ 1820 return_read_eof = NO; 1821 reported_read_eof = NO; 1822 status_eof_detected = NO; /* reset this too */ 1823 /*fall through */ 1824 } 1825 } 1826 } 1827 1828 /*****************************/ 1829 if (bytes_todo == 0) { 1830 return total_bytes_done; 1831 } 1832 1833 if (bytes_todo > 0) { 1834 /* start reading data */ 1835 if (is_exception()) { 1836/****************************************/ 1837 tpqputs(TPQD_DMAX, 1838 "is_exception() before start_dma()!"); 1839 } 1840 1841/****************************************************************** 1842 ***** if start_dma() fails because the head is positioned 0 bytes 1843 ***** before the FM, (causing EXCEPTION to be set) return_read_eof should 1844 ***** be set to YES, and we should return total_bytes_done, rather than -ENXIO. 1845 ***** The app should recognize this as an EOF condition. 1846 ***************************************************************************/ 1847 stat = start_dma(READ, bytes_todo); 1848 if (stat == TE_OK) { 1849 /* Wait for transfer to complete, interrupt should wake us */ 1850 while (dma_mode != 0) { 1851 sleep_on(&qic02_tape_transfer); 1852 } 1853 if (status_error) { 1854 return_read_eof = YES; 1855 } 1856 1857 } else if (stat != TE_END) { 1858 /* should do sense() on error here */ 1859 printk("Trouble: stat==%02x\n", stat); 1860 return_read_eof = YES; 1861 /*************** check EOF/EOT handling!!!!!! **/ 1862 } 1863 end_dma(&bytes_done); 1864 if (bytes_done > bytes_todo) { 1865 tpqputs(TPQD_ALWAYS, 1866 "read: Oops, read more bytes than requested"); 1867 return -EIO; 1868 } 1869 /* copy buffer to user-space in one go */ 1870 if (bytes_done > 0) { 1871 if (copy_to_user(buf, buffaddr, bytes_done)) 1872 return -EFAULT; 1873 } 1874 /* Checks Ton's patch below */ 1875 if ((return_read_eof == NO) 1876 && (status_eof_detected == YES)) { 1877 printk(TPQIC02_NAME 1878 ": read(): return_read_eof=%d, status_eof_detected=YES. return_read_eof:=YES\n", 1879 return_read_eof); 1880 } 1881 if ((bytes_todo != bytes_done) 1882 || (status_eof_detected == YES)) { 1883 /* EOF or EOM detected. return EOF next time. */ 1884 return_read_eof = YES; 1885 } 1886 1887 } 1888 /* else: ignore read request for 0 bytes */ 1889 if (bytes_done > 0) { 1890 status_bytes_rd = YES; 1891 buf += bytes_done; 1892 *ppos += bytes_done; 1893 total_bytes_done += bytes_done; 1894 count -= bytes_done; 1895 } 1896 } 1897 tpqputs(TPQD_ALWAYS, "read request for <0 bytes"); 1898 return -EINVAL; 1899} /* qic02_tape_read */ 1900 1901 1902 1903/* The drive detects near-EOT by means of the holes in the tape. 1904 * When the holes are detected, there is some space left. The drive 1905 * reports this as a TP_EOM exception. After clearing the exception, 1906 * the drive should accept two extra blocks. 1907 * 1908 * It seems there are some archiver programs that would like to use the 1909 * extra space for writing a continuation marker. The driver should return 1910 * end-of-file to the user program on writes, when the holes are detected. 1911 * If the user-program wants to use the extra space, it should use the 1912 * MTNOP ioctl() to get the generic status register and may then continue 1913 * writing (max 1kB). ----------- doesn't work yet............... 1914 * 1915 * EOF behaviour on writes: 1916 * If there is enough room, write all of the data. 1917 * If there is insufficient room, write as much as will fit and 1918 * return the amount written. If the requested amount differs from the 1919 * written amount, the application program should recognize that as the 1920 * end of file. Subsequent writes will return -ENOSPC. 1921 * Unless the minor bits specify a rewind-on-close, the tape will not 1922 * be rewound when it is full. The user-program should do that, if desired. 1923 * If the driver were to do that automatically, a user-program could be 1924 * confused about the EOT/BOT condition after re-opening the tape device. 1925 * 1926 * Multiple volume support: Tar closes the tape device before prompting for 1927 * the next tape. The user may then insert a new tape and tar will open the 1928 * tape device again. The driver will detect an exception status in (No Cartridge) 1929 * and force a rewind. After that tar may continue writing. 1930 */ 1931static ssize_t qic02_tape_write(struct file *filp, const char *buf, 1932 size_t count, loff_t * ppos) 1933{ 1934 kdev_t dev = filp->f_dentry->d_inode->i_rdev; 1935 unsigned short flags = filp->f_flags; 1936 unsigned long bytes_todo, bytes_done, total_bytes_done = 0; 1937 1938 if (status_zombie == YES) { 1939 tpqputs(TPQD_ALWAYS, "configs not set"); 1940 return -ENXIO; 1941 } 1942 1943 if (TP_DIAGS(current_tape_dev)) { 1944 /* can't print a ``long long'' (for filp->f_pos), so chop it */ 1945 printk(TPQIC02_NAME ": request WRITE, minor=%x, buf=%p" 1946 ", count=%lx, pos=%lx, flags=%x\n", 1947 MINOR(dev), buf, 1948 (long) count, (unsigned long) filp->f_pos, flags); 1949 } 1950 1951 if (count % TAPE_BLKSIZE) { /* only allow mod 512 bytes at a time */ 1952 tpqputs(TPQD_BLKSZ, "Wrong block size"); 1953 return -EINVAL; 1954 } 1955 1956 if (mode_access == READ) { 1957 tpqputs(TPQD_ALWAYS, "Not in write mode"); 1958 return -EACCES; 1959 } 1960 1961 /* open() does a sense() and we can assume the tape isn't changed 1962 * between open() and release(), so the tperror.exs bits will still 1963 * be valid. 1964 */ 1965 if ((tperror.exs & TP_ST0) && (tperror.exs & TP_WRP)) { 1966 tpqputs(TPQD_ALWAYS, "Cartridge is write-protected."); 1967 return -EACCES; /* don't even try when write protected */ 1968 } 1969 1970 if (doing_read == YES) { 1971 terminate_read(0); 1972 } 1973 1974 while ((signed) count >= 0) { 1975 /* see how much fits in the kernel buffer */ 1976 bytes_done = 0; 1977 bytes_todo = TPQBUF_SIZE; 1978 if (bytes_todo > count) { 1979 bytes_todo = count; 1980 } 1981 1982 if (return_write_eof == YES) { 1983 /* return_write_eof should be reset on reverse tape movements. */ 1984 1985 if (reported_write_eof == NO) { 1986 if (bytes_todo > 0) { 1987 tpqputs(TPQD_ALWAYS, 1988 "partial write"); 1989 /* partial write signals EOF to user program */ 1990 } 1991 reported_write_eof = YES; 1992 return total_bytes_done; 1993 } else { 1994 return -ENOSPC; /* return error */ 1995 } 1996 } 1997 1998 /* Quit when done. */ 1999 if (bytes_todo == 0) { 2000 return total_bytes_done; 2001 } 2002 2003 /* copy from user to DMA buffer and initiate transfer. */ 2004 if (bytes_todo > 0) { 2005 if (copy_from_user(buffaddr, buf, bytes_todo)) 2006 return -EFAULT; 2007 2008/****************** similar problem with read() at FM could happen here at EOT. 2009 ******************/ 2010 2011/***** if at EOT, 0 bytes can be written. start_dma() will 2012 ***** fail and write() will return ENXIO error 2013 *****/ 2014 if (start_dma(WRITE, bytes_todo) != TE_OK) { 2015 tpqputs(TPQD_ALWAYS, 2016 "write: start_dma() failed"); 2017 /* should do sense() on error here */ 2018 return -ENXIO; 2019 /*********** FIXTHIS **************/ 2020 } 2021 2022 /* Wait for write to complete, interrupt should wake us. */ 2023 while ((status_error == 0) && (dma_mode != 0)) { 2024 sleep_on(&qic02_tape_transfer); 2025 } 2026 2027 end_dma(&bytes_done); 2028 if (bytes_done > bytes_todo) { 2029 tpqputs(TPQD_ALWAYS, 2030 "write: Oops, wrote more bytes than requested"); 2031 return -EIO; 2032 } 2033 /* If the dma-transfer was aborted because of an exception, 2034 * status_error will have been set in the interrupt handler. 2035 * Then end_dma() will do a sense(). 2036 * If the exception was EXC_EOM, the EW-hole was encountered 2037 * and two more blocks could be written. For the time being we'll 2038 * just consider this to be the EOT. 2039 * Otherwise, something Bad happened, such as the maximum number 2040 * of block-rewrites was exceeded. [e.g. A very bad spot on tape was 2041 * encountered. Normally short dropouts are compensated for by 2042 * rewriting the block in error, up to 16 times. I'm not sure 2043 * QIC-24 drives can do this.] 2044 */ 2045 if (status_error) { 2046 if (status_eom_detected == YES) { 2047 tpqputs(TPQD_ALWAYS, 2048 "write: EW detected"); 2049 return_write_eof = YES; 2050 } else { 2051 /* probably EXC_RWA */ 2052 tpqputs(TPQD_ALWAYS, 2053 "write: dma: error in writing"); 2054 return -EIO; 2055 } 2056 } 2057 if (bytes_todo != bytes_done) { 2058 /* EOF or EOM detected. return EOT next time. */ 2059 return_write_eof = YES; 2060 } 2061 } 2062 /* else: ignore write request for 0 bytes. */ 2063 2064 if (bytes_done > 0) { 2065 status_bytes_wr = YES; 2066 buf += bytes_done; 2067 *ppos += bytes_done; 2068 total_bytes_done += bytes_done; 2069 count -= bytes_done; 2070 } 2071 } 2072 2073 tpqputs(TPQD_ALWAYS, "write request for <0 bytes"); 2074 if (TPQDBG(DEBUG)) { 2075 printk(TPQIC02_NAME ": status_bytes_wr %x, buf %p" 2076 ", total_bytes_done %lx, count %lx\n", 2077 status_bytes_wr, buf, total_bytes_done, 2078 (long) count); 2079 } 2080 return -EINVAL; 2081} /* qic02_tape_write */ 2082 2083 2084 2085/* qic02_tape_open() 2086 * We allow the device to be opened, even if it is marked 'dead' because 2087 * we want to be able to reset the tape device without rebooting. 2088 * Only one open tape file at a time, except when minor=255. 2089 * Minor 255 is only allowed for resetting and always returns <0. 2090 * 2091 * The density command is only allowed when TP_BOM is set. Thus, remember 2092 * the most recently used minor bits. When they are different from the 2093 * remembered values, rewind the tape and set the required density. 2094 * Don't rewind if the minor bits specify density 0. 2095 */ 2096 2097static int qic02_tape_open(struct inode *inode, struct file *filp) 2098{ 2099 static int qic02_tape_open_no_use_count(struct inode *, 2100 struct file *); 2101 int open_error; 2102 2103 open_error = qic02_tape_open_no_use_count(inode, filp); 2104 return open_error; 2105} 2106 2107static int qic02_tape_open_no_use_count(struct inode *inode, 2108 struct file *filp) 2109{ 2110 kdev_t dev = inode->i_rdev; 2111 unsigned short flags = filp->f_flags; 2112 unsigned short dens = 0; 2113 int s; 2114 2115 2116 if (TP_DIAGS(dev)) { 2117 printk("qic02_tape_open: dev=%s, flags=%x ", 2118 kdevname(dev), flags); 2119 } 2120 2121 if (MINOR(dev) == 255) { /* special case for resetting */ 2122 if (capable(CAP_SYS_ADMIN)) { 2123 return (tape_reset(1) == TE_OK) ? -EAGAIN : -ENXIO; 2124 } else { 2125 return -EPERM; 2126 } 2127 } 2128 2129 if (status_dead == YES) { 2130 /* Allow `mt reset' ioctl() even when already open()ed. */ 2131 return 0; 2132 } 2133 2134 /* Only one at a time from here on... */ 2135 if (file_count(filp) > 1) { /* filp->f_count==1 for the first open() */ 2136 return -EBUSY; 2137 } 2138 2139 if (status_zombie == YES) { 2140 /* no irq/dma/port stuff allocated yet, no reset done 2141 * yet, so return until MTSETCONFIG has been done. 2142 */ 2143 return 0; 2144 } 2145 2146 status_bytes_rd = NO; 2147 status_bytes_wr = NO; 2148 2149 return_read_eof = NO; /********????????????????*****/ 2150 return_write_eof = (status_eot_detected) ? YES : NO; 2151 2152 /* Clear this in case user app close()d before reading EOF token */ 2153 status_eof_detected = NO; 2154 2155 reported_read_eof = NO; 2156 reported_write_eof = NO; 2157 2158 2159 switch (flags & O_ACCMODE) { 2160 case O_RDONLY: 2161 mode_access = READ; 2162 break; 2163 case O_WRONLY: /* Fallthru... Strictly speaking this is not correct... */ 2164 case O_RDWR: /* Reads are allowed as long as nothing is written */ 2165 mode_access = WRITE; 2166 break; 2167 } 2168 2169 /* This is to avoid tape-changed problems (TP_CNI exception). 2170 * 2171 * Since removing the cartridge will not raise an exception, 2172 * we always do a tp_sense() to make sure we have the proper 2173 * CNI status, the 2150L may need an additional sense.... - Eddy 2174 */ 2175 s = tp_sense(TP_WRP | TP_EOM | TP_BOM | TP_CNI | TP_EOR); 2176 2177 if (s == TE_OK) { 2178 /* Try to clear cartridge-changed status for Archive-2150L */ 2179 if ((tperror.exs & TP_ST0) && (tperror.exs & TP_CNI)) { 2180 s = tp_sense(TP_WRP | TP_EOM | TP_BOM | TP_CNI | 2181 TP_EOR); 2182 } 2183 } 2184 2185 if (s != TE_OK) { 2186 tpqputs(TPQD_ALWAYS, "open: sense() failed"); 2187 return -EIO; 2188 } 2189 2190 /* exception bits should be up-to-date now, so check for 2191 * tape presence and exit if absent. 2192 * Even `mt stat' will fail without a tape. 2193 */ 2194 if ((tperror.exs & TP_ST0) && (tperror.exs & TP_CNI)) { 2195 tpqputs(TPQD_ALWAYS, "No tape present."); 2196 return -EIO; 2197 } 2198 2199 /* At this point we can assume that a tape is present and 2200 * that it will remain present until release() is called. 2201 */ 2202 2203 /* not allowed to do QCMD_DENS_* unless tape is rewound */ 2204 if ((TP_DENS(dev) != 0) 2205 && (TP_DENS(current_tape_dev) != TP_DENS(dev))) { 2206 /* force rewind if minor bits have changed, 2207 * i.e. user wants to use tape in different format. 2208 * [assuming single drive operation] 2209 */ 2210 if (TP_HAVE_DENS) { 2211 tpqputs(TPQD_REWIND, 2212 "Density minor bits have changed. Forcing rewind."); 2213 need_rewind = YES; 2214 } 2215 } else { 2216 /* density bits still the same, but TP_DIAGS bit 2217 * may have changed. 2218 */ 2219 current_tape_dev = dev; 2220 } 2221 2222 if (need_rewind == YES) { 2223/***************** CHECK THIS!!!!!!!! **********/ 2224 s = do_qic_cmd(QCMD_REWIND, TIM_R); 2225 if (s != 0) { 2226 tpqputs(TPQD_ALWAYS, "open: rewind failed"); 2227 return -EIO; 2228 } 2229 } 2230 2231 2232/* Note: After a reset command, the controller will rewind the tape 2233 * just before performing any tape movement operation! ************ SO SET need_rewind flag!!!!! 2234 */ 2235 if (status_dead == YES) { 2236 tpqputs(TPQD_ALWAYS, "open: tape dead, attempting reset"); 2237 if (tape_reset(1) != TE_OK) { 2238 return -ENXIO; 2239 } else { 2240 status_dead = NO; 2241 if (tp_sense(~(TP_ST1 | TP_ILL)) != TE_OK) { 2242 tpqputs(TPQD_ALWAYS, 2243 "open: tp_sense() failed\n"); 2244 status_dead = YES; /* try reset next time */ 2245 return -EIO; 2246 } 2247 } 2248 } 2249 2250 /* things should be ok, once we get here */ 2251 2252 2253 /* set density: only allowed when TP_BOM status bit is set, 2254 * so we must have done a rewind by now. If not, just skip over. 2255 * Only give set density command when minor bits have changed. 2256 */ 2257 if (TP_DENS(current_tape_dev) == TP_DENS(dev)) { 2258 return 0; 2259 } 2260 2261 current_tape_dev = dev; 2262 need_rewind = NO; 2263 if (TP_HAVE_DENS) { 2264 dens = TP_DENS(dev); 2265 } 2266 2267 if (dens < sizeof(format_names) / sizeof(char *)) { 2268 printk(TPQIC02_NAME ": format: %s%s\n", 2269 (dens != 0) ? "QIC-" : "", format_names[dens]); 2270 } else { 2271 tpqputs(TPQD_REWIND, "Wait for retensioning..."); 2272 } 2273 2274 switch (TP_DENS(dev)) { 2275 case 0: /* Minor 0 is for drives without set-density support */ 2276 s = 0; 2277 break; 2278 case 1: 2279 s = do_qic_cmd(QCMD_DENS_11, TIM_S); 2280 break; 2281 case 2: 2282 s = do_qic_cmd(QCMD_DENS_24, TIM_S); 2283 break; 2284 case 3: 2285 s = do_qic_cmd(QCMD_DENS_120, TIM_S); 2286 break; 2287 case 4: 2288 s = do_qic_cmd(QCMD_DENS_150, TIM_S); 2289 break; 2290 case 5: 2291 s = do_qic_cmd(QCMD_DENS_300, TIM_S); 2292 break; 2293 case 6: 2294 s = do_qic_cmd(QCMD_DENS_600, TIM_S); 2295 break; 2296 default: /* otherwise do a retension before anything else */ 2297 s = do_qic_cmd(QCMD_RETEN, TIM_R); 2298 } 2299 if (s != 0) { 2300 status_dead = YES; /* force reset */ 2301 current_tape_dev = 0; /* earlier 0xff80 */ 2302 return -EIO; 2303 } 2304 2305 return 0; 2306} /* qic02_tape_open */ 2307 2308 2309static int qic02_tape_release(struct inode *inode, struct file *filp) 2310{ 2311 kdev_t dev = inode->i_rdev; 2312 2313 lock_kernel(); 2314 if (TP_DIAGS(dev)) { 2315 printk("qic02_tape_release: dev=%s\n", kdevname(dev)); 2316 } 2317 2318 if (status_zombie == NO) { /* don't rewind in zombie mode */ 2319 /* Terminate any pending write cycle. Terminating the read-cycle 2320 * is delayed until it is required to do so for a new command. 2321 */ 2322 terminate_write(-1); 2323 2324 if (status_dead == YES) { 2325 tpqputs(TPQD_ALWAYS, "release: device dead!?"); 2326 } 2327 2328 /* Rewind only if minor number requires it AND 2329 * read/writes have been done. ************* IS THIS CORRECT?????????? 2330 */ 2331 if ((TP_REWCLOSE(dev)) 2332 && (status_bytes_rd | status_bytes_wr)) { 2333 tpqputs(TPQD_REWIND, "release: Doing rewind..."); 2334 (void) do_qic_cmd(QCMD_REWIND, TIM_R); 2335 } 2336 } 2337 unlock_kernel(); 2338 return 0; 2339} /* qic02_tape_release */ 2340 2341 2342#ifdef CONFIG_QIC02_DYNCONF 2343/* Set masks etc. based on the interface card type. */ 2344static int update_ifc_masks(int ifc) 2345{ 2346 QIC02_TAPE_IFC = ifc; 2347 2348 if ((QIC02_TAPE_IFC == WANGTEK) || (QIC02_TAPE_IFC == EVEREX)) { 2349 QIC02_STAT_PORT = QIC02_TAPE_PORT; 2350 QIC02_CTL_PORT = QIC02_TAPE_PORT; 2351 QIC02_CMD_PORT = QIC02_TAPE_PORT + 1; 2352 QIC02_DATA_PORT = QIC02_TAPE_PORT + 1; 2353 QIC02_STAT_READY = WT_QIC02_STAT_READY; 2354 QIC02_STAT_EXCEPTION = WT_QIC02_STAT_EXCEPTION; 2355 QIC02_STAT_MASK = WT_QIC02_STAT_MASK; 2356 2357 QIC02_STAT_RESETMASK = WT_QIC02_STAT_RESETMASK; 2358 QIC02_STAT_RESETVAL = WT_QIC02_STAT_RESETVAL; 2359 2360 QIC02_CTL_RESET = WT_QIC02_CTL_RESET; 2361 QIC02_CTL_REQUEST = WT_QIC02_CTL_REQUEST; 2362 2363 if (QIC02_TAPE_DMA == 3) { 2364 WT_CTL_DMA = WT_CTL_DMA3; 2365 } else if (QIC02_TAPE_DMA == 1) { 2366 WT_CTL_DMA = WT_CTL_DMA1; 2367 } else { 2368 tpqputs(TPQD_ALWAYS, 2369 "Unsupported or incorrect DMA channel"); 2370 return -EIO; 2371 } 2372 2373 if (QIC02_TAPE_IFC == EVEREX) { 2374 /* Everex is a special case for Wangtek (actually 2375 * it's the other way 'round, but I saw Wangtek first) 2376 */ 2377 if (QIC02_TAPE_DMA == 3) { 2378 WT_CTL_DMA = WT_CTL_DMA1; 2379 } 2380 2381 /* Fixup the kernel copy of the IFC type to that 2382 * we don't have to distinguish between Wangtek and 2383 * and Everex at runtime. 2384 */ 2385 QIC02_TAPE_IFC = WANGTEK; 2386 } 2387 } else if (QIC02_TAPE_IFC == ARCHIVE) { 2388 QIC02_STAT_PORT = QIC02_TAPE_PORT + 1; 2389 QIC02_CTL_PORT = QIC02_TAPE_PORT + 1; 2390 QIC02_CMD_PORT = QIC02_TAPE_PORT; 2391 QIC02_DATA_PORT = QIC02_TAPE_PORT; 2392 QIC02_STAT_READY = AR_QIC02_STAT_READY; 2393 QIC02_STAT_EXCEPTION = AR_QIC02_STAT_EXCEPTION; 2394 QIC02_STAT_MASK = AR_QIC02_STAT_MASK; 2395 2396 QIC02_STAT_RESETMASK = AR_QIC02_STAT_RESETMASK; 2397 QIC02_STAT_RESETVAL = AR_QIC02_STAT_RESETVAL; 2398 2399 QIC02_CTL_RESET = AR_QIC02_CTL_RESET; 2400 QIC02_CTL_REQUEST = AR_QIC02_CTL_REQUEST; 2401 2402 if (QIC02_TAPE_DMA > 3) { 2403 tpqputs(TPQD_ALWAYS, 2404 "Unsupported or incorrect DMA channel"); 2405 return -EIO; 2406 } 2407 } else if (QIC02_TAPE_IFC == MOUNTAIN) { 2408 QIC02_STAT_PORT = QIC02_TAPE_PORT + 1; 2409 QIC02_CTL_PORT = QIC02_TAPE_PORT + 1; 2410 QIC02_CMD_PORT = QIC02_TAPE_PORT; 2411 QIC02_DATA_PORT = QIC02_TAPE_PORT; 2412 2413 QIC02_STAT_READY = MTN_QIC02_STAT_READY; 2414 QIC02_STAT_EXCEPTION = MTN_QIC02_STAT_EXCEPTION; 2415 QIC02_STAT_MASK = MTN_QIC02_STAT_MASK; 2416 2417 QIC02_STAT_RESETMASK = MTN_QIC02_STAT_RESETMASK; 2418 QIC02_STAT_RESETVAL = MTN_QIC02_STAT_RESETVAL; 2419 2420 QIC02_CTL_RESET = MTN_QIC02_CTL_RESET; 2421 QIC02_CTL_REQUEST = MTN_QIC02_CTL_REQUEST; 2422 2423 if (QIC02_TAPE_DMA > 3) { 2424 tpqputs(TPQD_ALWAYS, 2425 "Unsupported or incorrect DMA channel"); 2426 return -EIO; 2427 } 2428 } else { 2429 tpqputs(TPQD_ALWAYS, "Invalid interface type"); 2430 return -ENXIO; 2431 } 2432 return qic02_get_resources(); 2433} /* update_ifc_masks */ 2434#endif 2435 2436 2437/* ioctl allows user programs to rewind the tape and stuff like that */ 2438static int qic02_tape_ioctl(struct inode *inode, struct file *filp, 2439 unsigned int iocmd, unsigned long ioarg) 2440{ 2441 int error; 2442 int dev_maj = MAJOR(inode->i_rdev); 2443 int c; 2444 struct mtop operation; 2445 unsigned char blk_addr[6]; 2446 struct mtpos ioctl_tell; 2447 2448 2449 if (TP_DIAGS(current_tape_dev)) { 2450 printk(TPQIC02_NAME ": ioctl(%4x, %4x, %4lx)\n", dev_maj, 2451 iocmd, ioarg); 2452 } 2453 2454 if (!inode || !ioarg) { 2455 return -EINVAL; 2456 } 2457 2458 /* check iocmd first */ 2459 2460 if (dev_maj != QIC02_TAPE_MAJOR) { 2461 printk(TPQIC02_NAME ": Oops! Wrong device?\n"); 2462 /* A panic() would be appropriate here */ 2463 return -ENODEV; 2464 } 2465 2466 c = _IOC_NR(iocmd); 2467 2468#ifdef CONFIG_QIC02_DYNCONF 2469 if (c == _IOC_NR(MTIOCGETCONFIG)) { 2470 CHECK_IOC_SIZE(mtconfiginfo); 2471 2472 if (copy_to_user 2473 ((char *) ioarg, (char *) &qic02_tape_dynconf, 2474 sizeof(qic02_tape_dynconf))) { 2475 return -EFAULT; 2476 } 2477 return 0; 2478 2479 } else if (c == _IOC_NR(MTIOCSETCONFIG)) { 2480 /* One should always do a MTIOCGETCONFIG first, then update 2481 * user-settings, then write back with MTIOCSETCONFIG. 2482 * The qic02conf program should re-open() the device before actual 2483 * use, to make sure everything is initialized. 2484 */ 2485 2486 CHECK_IOC_SIZE(mtconfiginfo); 2487 2488 if (!capable(CAP_SYS_ADMIN)) { 2489 return -EPERM; 2490 } 2491 2492 if ((doing_read != NO) || (doing_write != NO)) { 2493 return -EBUSY; 2494 } 2495 2496 if (status_zombie == NO) { 2497 qic02_release_resources(); /* and go zombie */ 2498 } 2499 2500 /* copy struct from user space to kernel space */ 2501 if (copy_from_user 2502 ((char *) &qic02_tape_dynconf, (char *) ioarg, 2503 sizeof(qic02_tape_dynconf))) { 2504 return -EFAULT; 2505 } 2506 return update_ifc_masks(qic02_tape_dynconf.ifc_type); 2507 } 2508 if (status_zombie == YES) { 2509 tpqputs(TPQD_ALWAYS, "Configs not set"); 2510 return -ENXIO; 2511 } 2512#endif 2513 if (c == _IOC_NR(MTIOCTOP)) { 2514 CHECK_IOC_SIZE(mtop); 2515 2516 /* copy mtop struct from user space to kernel space */ 2517 if (copy_from_user 2518 ((char *) &operation, (char *) ioarg, 2519 sizeof(operation))) { 2520 return -EFAULT; 2521 } 2522 2523 /* ---note: mt_count is signed, negative seeks must be 2524 * --- translated to seeks in opposite direction! 2525 * (only needed for Sun-programs, I think.) 2526 */ 2527 /* ---note: MTFSF with count 0 should position the 2528 * --- tape at the beginning of the current file. 2529 */ 2530 2531 if (TP_DIAGS(current_tape_dev)) { 2532 printk("OP op=%4x, count=%4x\n", operation.mt_op, 2533 operation.mt_count); 2534 } 2535 2536 if (operation.mt_count < 0) { 2537 tpqputs(TPQD_ALWAYS, 2538 "Warning: negative mt_count ignored"); 2539 } 2540 2541 ioctl_status.mt_resid = operation.mt_count; 2542 if (operation.mt_op == MTSEEK) { 2543 if (!TP_HAVE_SEEK) { 2544 return -ENOTTY; 2545 } 2546 2547 seek_addr_buf[0] = 2548 (operation.mt_count >> 16) & 0xff; 2549 seek_addr_buf[1] = 2550 (operation.mt_count >> 8) & 0xff; 2551 seek_addr_buf[2] = (operation.mt_count) & 0xff; 2552 if (operation.mt_count >> 24) { 2553 return -EINVAL; 2554 } 2555 if ((error = do_ioctl_cmd(operation.mt_op)) != 0) { 2556 return error; 2557 } 2558 2559 ioctl_status.mt_resid = 0; 2560 } else { 2561 while (operation.mt_count > 0) { 2562 operation.mt_count--; 2563 if ((error = 2564 do_ioctl_cmd(operation.mt_op)) != 0) { 2565 return error; 2566 } 2567 2568 ioctl_status.mt_resid = operation.mt_count; 2569 } 2570 } 2571 return 0; 2572 2573 } else if (c == _IOC_NR(MTIOCGET)) { 2574 if (TP_DIAGS(current_tape_dev)) { 2575 printk("GET "); 2576 } 2577 2578 CHECK_IOC_SIZE(mtget); 2579 2580 /* It appears (gmt(1)) that it is normal behaviour to 2581 * first set the status with MTNOP, and then to read 2582 * it out with MTIOCGET 2583 */ 2584 2585 /* copy results to user space */ 2586 if (copy_to_user 2587 ((char *) ioarg, (char *) &ioctl_status, 2588 sizeof(ioctl_status))) { 2589 return -EFAULT; 2590 } 2591 return 0; 2592 } else if (TP_HAVE_TELL && (c == _IOC_NR(MTIOCPOS))) { 2593 if (TP_DIAGS(current_tape_dev)) { 2594 printk("POS "); 2595 } 2596 2597 CHECK_IOC_SIZE(mtpos); 2598 2599 tpqputs(TPQD_IOCTLS, "MTTELL reading block address"); 2600 if ((doing_read == YES) || (doing_write == YES)) { 2601 finish_rw(AR_QCMDV_TELL_BLK); 2602 } 2603 2604 c = rdstatus((char *) blk_addr, sizeof(blk_addr), 2605 AR_QCMDV_TELL_BLK); 2606 if (c != TE_OK) { 2607 return -EIO; 2608 } 2609 2610 ioctl_tell.mt_blkno = 2611 (blk_addr[3] << 16) | (blk_addr[4] << 8) | blk_addr[5]; 2612 2613 /* copy results to user space */ 2614 if (copy_to_user 2615 ((char *) ioarg, (char *) &ioctl_tell, 2616 sizeof(ioctl_tell))) { 2617 return -EFAULT; 2618 } 2619 return 0; 2620 2621 } else { 2622 return -ENOTTY; /* Other cmds not supported. */ 2623 } 2624} /* qic02_tape_ioctl */ 2625 2626 2627 2628/* These are (most) of the interface functions: */ 2629static struct file_operations qic02_tape_fops = { 2630 owner:THIS_MODULE, 2631 llseek:no_llseek, 2632 read:qic02_tape_read, 2633 write:qic02_tape_write, 2634 ioctl:qic02_tape_ioctl, 2635 open:qic02_tape_open, 2636 release:qic02_tape_release, 2637}; 2638 2639 2640static void qic02_release_resources(void) 2641{ 2642 free_irq(QIC02_TAPE_IRQ, NULL); 2643 free_dma(QIC02_TAPE_DMA); 2644 release_region(QIC02_TAPE_PORT, QIC02_TAPE_PORT_RANGE); 2645 if (buffaddr) { 2646 free_pages((unsigned long) buffaddr, 2647 get_order(TPQBUF_SIZE)); 2648 } 2649 buffaddr = 0; /* Better to cause a panic than overwite someone else */ 2650 status_zombie = YES; 2651} /* qic02_release_resources */ 2652 2653 2654static int qic02_get_resources(void) 2655{ 2656 /* First perform some checks. If one of them fails, 2657 * the tape driver will not be registered to the system. 2658 */ 2659 if (QIC02_TAPE_IRQ > 16) { 2660 tpqputs(TPQD_ALWAYS, "Bogus interrupt number."); 2661 return -ENXIO; 2662 } 2663 2664 /* for DYNCONF, allocating IO, DMA and IRQ should not be done until 2665 * the config parameters have been set using MTSETCONFIG. 2666 */ 2667 2668 /* Grab the IO region. */ 2669 if (!request_region(QIC02_TAPE_PORT, QIC02_TAPE_PORT_RANGE, 2670 TPQIC02_NAME)) { 2671 printk(TPQIC02_NAME 2672 ": IO space at 0x%x [%d ports] already reserved\n", 2673 QIC02_TAPE_PORT, QIC02_TAPE_PORT_RANGE); 2674 return -ENXIO; 2675 } 2676 2677 /* get IRQ */ 2678 if (request_irq 2679 (QIC02_TAPE_IRQ, qic02_tape_interrupt, SA_INTERRUPT, "QIC-02", 2680 NULL)) { 2681 printk(TPQIC02_NAME 2682 ": can't allocate IRQ%d for QIC-02 tape\n", 2683 QIC02_TAPE_IRQ); 2684 release_region(QIC02_TAPE_PORT, QIC02_TAPE_PORT_RANGE); 2685 return -EBUSY; 2686 } 2687 2688 /* After IRQ, allocate DMA channel */ 2689 if (request_dma(QIC02_TAPE_DMA, "QIC-02")) { 2690 printk(TPQIC02_NAME 2691 ": can't allocate DMA%d for QIC-02 tape\n", 2692 QIC02_TAPE_DMA); 2693 free_irq(QIC02_TAPE_IRQ, NULL); 2694 release_region(QIC02_TAPE_PORT, QIC02_TAPE_PORT_RANGE); 2695 return -EBUSY; 2696 } 2697 2698 /* Setup the page-address for the dma transfer. */ 2699 buffaddr = 2700 (void *) __get_dma_pages(GFP_KERNEL, get_order(TPQBUF_SIZE)); 2701 2702 if (!buffaddr) { 2703 qic02_release_resources(); 2704 return -EBUSY; /* Not ideal, EAGAIN perhaps? */ 2705 } 2706 2707 memset(buffaddr, 0, TPQBUF_SIZE); 2708 2709 printk(TPQIC02_NAME 2710 ": Settings: IRQ %d, DMA %d, IO 0x%x, IFC %s\n", 2711 QIC02_TAPE_IRQ, QIC02_TAPE_DMA, ((QIC02_TAPE_IFC == ARCHIVE) 2712 || (QIC02_TAPE_IFC == 2713 MOUNTAIN)) ? 2714 QIC02_CMD_PORT : QIC02_STAT_PORT, 2715 (QIC02_TAPE_IFC == 2716 MOUNTAIN) ? "Mountain" : ((QIC02_TAPE_IFC == 2717 ARCHIVE) ? "Archive" : 2718 "Wangtek")); 2719 2720 if (tape_reset(0) != TE_OK 2721 || tp_sense(TP_WRP | TP_POR | TP_CNI) != TE_OK) { 2722 /* No drive detected, so vanish */ 2723 tpqputs(TPQD_ALWAYS, 2724 "No drive detected -- releasing IO/IRQ/DMA."); 2725 status_dead = YES; 2726 qic02_release_resources(); 2727 return -EIO; 2728 } 2729 2730 /* All should be ok now */ 2731 status_zombie = NO; 2732 return 0; 2733} /* qic02_get_resources */ 2734 2735int __init qic02_tape_init(void) 2736{ 2737 if (TPSTATSIZE != 6) { 2738 printk(TPQIC02_NAME 2739 ": internal error: tpstatus struct incorrect!\n"); 2740 return -ENODEV; 2741 } 2742 if ((TPQBUF_SIZE < 512) || (TPQBUF_SIZE >= 0x10000)) { 2743 printk(TPQIC02_NAME 2744 ": internal error: DMA buffer size out of range\n"); 2745 return -ENODEV; 2746 } 2747 2748 current_tape_dev = MKDEV(QIC02_TAPE_MAJOR, 0); 2749 2750#ifndef CONFIG_QIC02_DYNCONF 2751 printk(TPQIC02_NAME ": IRQ %d, DMA %d, IO 0x%x, IFC %s, %s, %s\n", 2752 QIC02_TAPE_IRQ, QIC02_TAPE_DMA, 2753# if QIC02_TAPE_IFC == WANGTEK 2754 QIC02_STAT_PORT, "Wangtek", 2755# elif QIC02_TAPE_IFC == ARCHIVE 2756 QIC02_CMD_PORT, "Archive", 2757# elif QIC02_TAPE_IFC == MOUNTAIN 2758 QIC02_CMD_PORT, "Mountain", 2759# else 2760# error 2761# endif 2762 rcs_revision, rcs_date); 2763 if (qic02_get_resources()) { 2764 return -ENODEV; 2765 } 2766#else 2767 printk(TPQIC02_NAME ": Runtime config, %s, %s\n", 2768 rcs_revision, rcs_date); 2769#endif 2770 printk(TPQIC02_NAME ": DMA buffers: %u blocks\n", NR_BLK_BUF); 2771 /* If we got this far, install driver functions */ 2772 if (devfs_register_chrdev 2773 (QIC02_TAPE_MAJOR, TPQIC02_NAME, &qic02_tape_fops)) { 2774 printk(TPQIC02_NAME ": Unable to get chrdev major %d\n", 2775 QIC02_TAPE_MAJOR); 2776#ifndef CONFIG_QIC02_DYNCONF 2777 qic02_release_resources(); 2778#endif 2779 return -ENODEV; 2780 } 2781 devfs_register(NULL, "ntpqic11", DEVFS_FL_DEFAULT, 2782 QIC02_TAPE_MAJOR, 2, 2783 S_IFCHR | S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP, 2784 &qic02_tape_fops, NULL); 2785 devfs_register(NULL, "tpqic11", DEVFS_FL_DEFAULT, 2786 QIC02_TAPE_MAJOR, 3, 2787 S_IFCHR | S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP, 2788 &qic02_tape_fops, NULL); 2789 devfs_register(NULL, "ntpqic24", DEVFS_FL_DEFAULT, 2790 QIC02_TAPE_MAJOR, 4, 2791 S_IFCHR | S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP, 2792 &qic02_tape_fops, NULL); 2793 devfs_register(NULL, "tpqic24", DEVFS_FL_DEFAULT, 2794 QIC02_TAPE_MAJOR, 5, 2795 S_IFCHR | S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP, 2796 &qic02_tape_fops, NULL); 2797 devfs_register(NULL, "ntpqic120", DEVFS_FL_DEFAULT, 2798 QIC02_TAPE_MAJOR, 6, 2799 S_IFCHR | S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP, 2800 &qic02_tape_fops, NULL); 2801 devfs_register(NULL, "tpqic120", DEVFS_FL_DEFAULT, 2802 QIC02_TAPE_MAJOR, 7, 2803 S_IFCHR | S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP, 2804 &qic02_tape_fops, NULL); 2805 devfs_register(NULL, "ntpqic150", DEVFS_FL_DEFAULT, 2806 QIC02_TAPE_MAJOR, 8, 2807 S_IFCHR | S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP, 2808 &qic02_tape_fops, NULL); 2809 devfs_register(NULL, "tpqic150", DEVFS_FL_DEFAULT, 2810 QIC02_TAPE_MAJOR, 9, 2811 S_IFCHR | S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP, 2812 &qic02_tape_fops, NULL); 2813 init_waitqueue_head(&qic02_tape_transfer); 2814 /* prepare timer */ 2815 TIMEROFF; 2816 init_timer(&tp_timer); 2817 tp_timer.function = qic02_tape_times_out; 2818 2819#ifndef CONFIG_QIC02_DYNCONF 2820 if (tape_reset(0) != TE_OK 2821 || tp_sense(TP_WRP | TP_POR | TP_CNI) != TE_OK) { 2822 /* No drive detected, so vanish */ 2823 tpqputs(TPQD_ALWAYS, 2824 "No drive detected -- driver going on vacation..."); 2825 qic02_release_resources(); 2826 status_dead = YES; 2827 return -ENODEV; 2828 } else { 2829 if (is_exception()) { 2830 tpqputs(TPQD_ALWAYS, "exception detected\n"); 2831 (void) tp_sense(TP_WRP | TP_POR | TP_CNI); 2832 } 2833 } 2834#endif 2835 2836 /* initialize generic status for ioctl requests */ 2837 2838 ioctl_status.mt_type = QIC02_TAPE_DRIVE; /* MT_IS* id nr */ 2839 2840 ioctl_status.mt_resid = 0; /* ---residual count */ 2841 ioctl_status.mt_gstat = 0; /* ---generic status */ 2842 ioctl_status.mt_erreg = 0; /* not used */ 2843 ioctl_status.mt_fileno = 0; /* number of current file on tape */ 2844 ioctl_status.mt_blkno = 0; /* number of current (logical) block */ 2845 2846 return 0; 2847} /* qic02_tape_init */ 2848 2849#ifdef MODULE 2850 2851void cleanup_module(void) 2852{ 2853 if (status_zombie == NO) { 2854 qic02_release_resources(); 2855 } 2856 devfs_unregister_chrdev(QIC02_TAPE_MAJOR, TPQIC02_NAME); 2857 devfs_unregister(devfs_find_handle 2858 (NULL, "ntpqic11", QIC02_TAPE_MAJOR, 2, 2859 DEVFS_SPECIAL_CHR, 0)); 2860 devfs_unregister(devfs_find_handle 2861 (NULL, "tpqic11", QIC02_TAPE_MAJOR, 3, 2862 DEVFS_SPECIAL_CHR, 0)); 2863 devfs_unregister(devfs_find_handle 2864 (NULL, "ntpqic24", QIC02_TAPE_MAJOR, 4, 2865 DEVFS_SPECIAL_CHR, 0)); 2866 devfs_unregister(devfs_find_handle 2867 (NULL, "tpqic24", QIC02_TAPE_MAJOR, 5, 2868 DEVFS_SPECIAL_CHR, 0)); 2869 devfs_unregister(devfs_find_handle 2870 (NULL, "ntpqic120", QIC02_TAPE_MAJOR, 6, 2871 DEVFS_SPECIAL_CHR, 0)); 2872 devfs_unregister(devfs_find_handle 2873 (NULL, "tpqic120", QIC02_TAPE_MAJOR, 7, 2874 DEVFS_SPECIAL_CHR, 0)); 2875 devfs_unregister(devfs_find_handle 2876 (NULL, "ntpqic150", QIC02_TAPE_MAJOR, 8, 2877 DEVFS_SPECIAL_CHR, 0)); 2878 devfs_unregister(devfs_find_handle 2879 (NULL, "tpqic150", QIC02_TAPE_MAJOR, 9, 2880 DEVFS_SPECIAL_CHR, 0)); 2881} 2882 2883int init_module(void) 2884{ 2885 int retval; 2886 retval = qic02_tape_init(); 2887# ifdef CONFIG_QIC02_DYNCONF 2888 /* This allows the dynamic config program to setup the card 2889 * by presetting qic02_tape_dynconf via insmod 2890 */ 2891 if (!retval && qic02_tape_dynconf.ifc_type) { 2892 retval = update_ifc_masks(qic02_tape_dynconf.ifc_type); 2893 if (retval) { 2894 cleanup_module(); 2895 } 2896 } 2897# endif 2898 return retval; 2899} 2900#endif 2901 2902MODULE_LICENSE("GPL"); 2903EXPORT_NO_SYMBOLS; 2904