OpenCores
URL https://opencores.org/ocsvn/or1k/or1k/trunk

Subversion Repositories or1k

[/] [or1k/] [trunk/] [rc203soc/] [sw/] [uClinux/] [drivers/] [char/] [tpqic02.c] - Blame information for rev 1772

Go to most recent revision | Details | Compare with Previous | View Log

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

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.