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

Subversion Repositories or1k

[/] [or1k/] [trunk/] [rc203soc/] [sw/] [uClinux/] [drivers/] [block/] [ide-tape.c] - Blame information for rev 1777

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

Line No. Rev Author Line
1 1626 jcastillo
/*
2
 * linux/drivers/block/ide-tape.c       Version 1.91            May  21, 1998
3
 *
4
 * Copyright (C) 1995, 1996 Gadi Oxman <gadio@netvision.net.il>
5
 *
6
 * This driver was constructed as a student project in the software laboratory
7
 * of the faculty of electrical engineering in the Technion - Israel's
8
 * Institute Of Technology, with the guide of Avner Lottem and Dr. Ilana David.
9
 *
10
 * It is hereby placed under the terms of the GNU general public license.
11
 * (See linux/COPYING).
12
 */
13
 
14
/*
15
 * IDE ATAPI streaming tape driver.
16
 *
17
 * This driver is a part of the Linux ide driver and works in co-operation
18
 * with linux/drivers/block/ide.c.
19
 *
20
 * The driver, in co-operation with ide.c, basically traverses the
21
 * request-list for the block device interface. The character device
22
 * interface, on the other hand, creates new requests, adds them
23
 * to the request-list of the block device, and waits for their completion.
24
 *
25
 * Pipelined operation mode is now supported on both reads and writes.
26
 *
27
 * The block device major and minor numbers are determined from the
28
 * tape's relative position in the ide interfaces, as explained in ide.c.
29
 *
30
 * The character device interface consists of two devices:
31
 *
32
 * ht0          major=37,minor=0        first IDE tape, rewind on close.
33
 * nht0         major=37,minor=128      first IDE tape, no rewind on close.
34
 *
35
 * Run /usr/src/linux/scripts/MAKEDEV.ide to create the above entries.
36
 * We currently support only one ide tape drive.
37
 *
38
 * The general magnetic tape commands compatible interface, as defined by
39
 * include/linux/mtio.h, is accessible through the character device.
40
 *
41
 * General ide driver configuration options, such as the interrupt-unmask
42
 * flag, can be configured by issuing an ioctl to the block device interface,
43
 * as any other ide device.
44
 *
45
 * Our own ide-tape ioctl's can be issued to either the block device or
46
 * the character device interface.
47
 *
48
 * Maximal throughput with minimal bus load will usually be achieved in the
49
 * following scenario:
50
 *
51
 *      1.      ide-tape is operating in the pipelined operation mode.
52
 *      2.      All character device read/write requests consist of an
53
 *              integral number of the tape's recommended data transfer unit
54
 *              (which is shown on initialization and can be received with
55
 *               an ioctl).
56
 *              As of version 1.3 of the driver, this is no longer as critical
57
 *              as it used to be.
58
 *      3.      No buffering is performed by the user backup program.
59
 *
60
 * Testing was done with a 2 GB CONNER CTMA 4000 IDE ATAPI Streaming Tape Drive.
61
 *
62
 * Ver 0.1   Nov  1 95   Pre-working code :-)
63
 * Ver 0.2   Nov 23 95   A short backup (few megabytes) and restore procedure
64
 *                        was successful ! (Using tar cvf ... on the block
65
 *                        device interface).
66
 *                       A longer backup resulted in major swapping, bad
67
 *                        overall Linux performance and eventually failed as
68
 *                        we received non serial read-ahead requests from the
69
 *                        buffer cache.
70
 * Ver 0.3   Nov 28 95   Long backups are now possible, thanks to the
71
 *                        character device interface. Linux's responsiveness
72
 *                        and performance doesn't seem to be much affected
73
 *                        from the background backup procedure.
74
 *                       Some general mtio.h magnetic tape operations are
75
 *                        now supported by our character device. As a result,
76
 *                        popular tape utilities are starting to work with
77
 *                        ide tapes :-)
78
 *                       The following configurations were tested:
79
 *                              1. An IDE ATAPI TAPE shares the same interface
80
 *                                 and irq with an IDE ATAPI CDROM.
81
 *                              2. An IDE ATAPI TAPE shares the same interface
82
 *                                 and irq with a normal IDE disk.
83
 *                        Both configurations seemed to work just fine !
84
 *                        However, to be on the safe side, it is meanwhile
85
 *                        recommended to give the IDE TAPE its own interface
86
 *                        and irq.
87
 *                       The one thing which needs to be done here is to
88
 *                        add a "request postpone" feature to ide.c,
89
 *                        so that we won't have to wait for the tape to finish
90
 *                        performing a long media access (DSC) request (such
91
 *                        as a rewind) before we can access the other device
92
 *                        on the same interface. This effect doesn't disturb
93
 *                        normal operation most of the time because read/write
94
 *                        requests are relatively fast, and once we are
95
 *                        performing one tape r/w request, a lot of requests
96
 *                        from the other device can be queued and ide.c will
97
 *                        service all of them after this single tape request.
98
 * Ver 1.0   Dec 11 95   Integrated into Linux 1.3.46 development tree.
99
 *                       On each read / write request, we now ask the drive
100
 *                        if we can transfer a constant number of bytes
101
 *                        (a parameter of the drive) only to its buffers,
102
 *                        without causing actual media access. If we can't,
103
 *                        we just wait until we can by polling the DSC bit.
104
 *                        This ensures that while we are not transferring
105
 *                        more bytes than the constant referred to above, the
106
 *                        interrupt latency will not become too high and
107
 *                        we won't cause an interrupt timeout, as happened
108
 *                        occasionally in the previous version.
109
 *                       While polling for DSC, the current request is
110
 *                        postponed and ide.c is free to handle requests from
111
 *                        the other device. This is handled transparently to
112
 *                        ide.c. The hwgroup locking method which was used
113
 *                        in the previous version was removed.
114
 *                       Use of new general features which are provided by
115
 *                        ide.c for use with atapi devices.
116
 *                        (Programming done by Mark Lord)
117
 *                       Few potential bug fixes (Again, suggested by Mark)
118
 *                       Single character device data transfers are now
119
 *                        not limited in size, as they were before.
120
 *                       We are asking the tape about its recommended
121
 *                        transfer unit and send a larger data transfer
122
 *                        as several transfers of the above size.
123
 *                        For best results, use an integral number of this
124
 *                        basic unit (which is shown during driver
125
 *                        initialization). I will soon add an ioctl to get
126
 *                        this important parameter.
127
 *                       Our data transfer buffer is allocated on startup,
128
 *                        rather than before each data transfer. This should
129
 *                        ensure that we will indeed have a data buffer.
130
 * Ver 1.1   Dec 14 95   Fixed random problems which occurred when the tape
131
 *                        shared an interface with another device.
132
 *                        (poll_for_dsc was a complete mess).
133
 *                       Removed some old (non-active) code which had
134
 *                        to do with supporting buffer cache originated
135
 *                        requests.
136
 *                       The block device interface can now be opened, so
137
 *                        that general ide driver features like the unmask
138
 *                        interrupts flag can be selected with an ioctl.
139
 *                        This is the only use of the block device interface.
140
 *                       New fast pipelined operation mode (currently only on
141
 *                        writes). When using the pipelined mode, the
142
 *                        throughput can potentially reach the maximum
143
 *                        tape supported throughput, regardless of the
144
 *                        user backup program. On my tape drive, it sometimes
145
 *                        boosted performance by a factor of 2. Pipelined
146
 *                        mode is enabled by default, but since it has a few
147
 *                        downfalls as well, you may want to disable it.
148
 *                        A short explanation of the pipelined operation mode
149
 *                        is available below.
150
 * Ver 1.2   Jan  1 96   Eliminated pipelined mode race condition.
151
 *                       Added pipeline read mode. As a result, restores
152
 *                        are now as fast as backups.
153
 *                       Optimized shared interface behavior. The new behavior
154
 *                        typically results in better IDE bus efficiency and
155
 *                        higher tape throughput.
156
 *                       Pre-calculation of the expected read/write request
157
 *                        service time, based on the tape's parameters. In
158
 *                        the pipelined operation mode, this allows us to
159
 *                        adjust our polling frequency to a much lower value,
160
 *                        and thus to dramatically reduce our load on Linux,
161
 *                        without any decrease in performance.
162
 *                       Implemented additional mtio.h operations.
163
 *                       The recommended user block size is returned by
164
 *                        the MTIOCGET ioctl.
165
 *                       Additional minor changes.
166
 * Ver 1.3   Feb  9 96   Fixed pipelined read mode bug which prevented the
167
 *                        use of some block sizes during a restore procedure.
168
 *                       The character device interface will now present a
169
 *                        continuous view of the media - any mix of block sizes
170
 *                        during a backup/restore procedure is supported. The
171
 *                        driver will buffer the requests internally and
172
 *                        convert them to the tape's recommended transfer
173
 *                        unit, making performance almost independent of the
174
 *                        chosen user block size.
175
 *                       Some improvements in error recovery.
176
 *                       By cooperating with triton.c, bus mastering DMA can
177
 *                        now sometimes be used with IDE tape drives as well.
178
 *                        Bus mastering DMA has the potential to dramatically
179
 *                        reduce the CPU's overhead when accessing the device,
180
 *                        and can be enabled by using hdparm -d1 on the tape's
181
 *                        block device interface. For more info, read the
182
 *                        comments in triton.c.
183
 * Ver 1.4   Mar 13 96   Fixed serialize support.
184
 * Ver 1.5   Apr 12 96   Fixed shared interface operation, broken in 1.3.85.
185
 *                       Fixed pipelined read mode inefficiency.
186
 *                       Fixed nasty null dereferencing bug.
187
 * Ver 1.6   Aug 16 96   Fixed FPU usage in the driver.
188
 *                       Fixed end of media bug.
189
 * Ver 1.7   Sep 10 96   Minor changes for the CONNER CTT8000-A model.
190
 * Ver 1.8   Sep 26 96   Attempt to find a better balance between good
191
 *                        interactive response and high system throughput.
192
 * Ver 1.9   Nov  5 96   Automatically cross encountered filemarks rather
193
 *                        than requiring an explicit FSF command.
194
 *                       Abort pending requests at end of media.
195
 *                       MTTELL was sometimes returning incorrect results.
196
 *                       Return the real block size in the MTIOCGET ioctl.
197
 *                       Some error recovery bug fixes.
198
 * Ver 1.91  May 21 98   Add support for INTERRUPT DRQ devices.
199
 *                       Add "speed == 0" work-around for HP COLORADO 5GB
200
 *
201
 * Here are some words from the first releases of hd.c, which are quoted
202
 * in ide.c and apply here as well:
203
 *
204
 * | Special care is recommended.  Have Fun!
205
 *
206
 */
207
 
208
/*
209
 * An overview of the pipelined operation mode.
210
 *
211
 * In the pipelined write mode, we will usually just add requests to our
212
 * pipeline and return immediately, before we even start to service them. The
213
 * user program will then have enough time to prepare the next request while
214
 * we are still busy servicing previous requests. In the pipelined read mode,
215
 * the situation is similar - we add read-ahead requests into the pipeline,
216
 * before the user even requested them.
217
 *
218
 * The pipeline can be viewed as a "safety net" which will be activated when
219
 * the system load is high and prevents the user backup program from keeping up
220
 * with the current tape speed. At this point, the pipeline will get
221
 * shorter and shorter but the tape will still be streaming at the same speed.
222
 * Assuming we have enough pipeline stages, the system load will hopefully
223
 * decrease before the pipeline is completely empty, and the backup program
224
 * will be able to "catch up" and refill the pipeline again.
225
 *
226
 * When using the pipelined mode, it would be best to disable any type of
227
 * buffering done by the user program, as ide-tape already provides all the
228
 * benefits in the kernel, where it can be done in a more efficient way.
229
 * As we will usually not block the user program on a request, the most
230
 * efficient user code will then be a simple read-write-read-... cycle.
231
 * Any additional logic will usually just slow down the backup process.
232
 *
233
 * Using the pipelined mode, I get a constant over 400 KBps throughput,
234
 * which seems to be the maximum throughput supported by my tape.
235
 *
236
 * However, there are some downfalls:
237
 *
238
 *      1.      We use memory (for data buffers) in proportional to the number
239
 *              of pipeline stages (each stage is about 26 KB with my tape).
240
 *      2.      In the pipelined write mode, we cheat and postpone error codes
241
 *              to the user task. In read mode, the actual tape position
242
 *              will be a bit further than the last requested block.
243
 *
244
 * Concerning (1):
245
 *
246
 *      1.      We allocate stages dynamically only when we need them. When
247
 *              we don't need them, we don't consume additional memory. In
248
 *              case we can't allocate stages, we just manage without them
249
 *              (at the expense of decreased throughput) so when Linux is
250
 *              tight in memory, we will not pose additional difficulties.
251
 *
252
 *      2.      The maximum number of stages (which is, in fact, the maximum
253
 *              amount of memory) which we allocate is limited by the compile
254
 *              time parameter IDETAPE_MAX_PIPELINE_STAGES.
255
 *
256
 *      3.      The maximum number of stages is a controlled parameter - We
257
 *              don't start from the user defined maximum number of stages
258
 *              but from the lower IDETAPE_MIN_PIPELINE_STAGES (again, we
259
 *              will not even allocate this amount of stages if the user
260
 *              program can't handle the speed). We then implement a feedback
261
 *              loop which checks if the pipeline is empty, and if it is, we
262
 *              increase the maximum number of stages as necessary until we
263
 *              reach the optimum value which just manages to keep the tape
264
 *              busy with minimum allocated memory or until we reach
265
 *              IDETAPE_MAX_PIPELINE_STAGES.
266
 *
267
 * Concerning (2):
268
 *
269
 *      In pipelined write mode, ide-tape can not return accurate error codes
270
 *      to the user program since we usually just add the request to the
271
 *      pipeline without waiting for it to be serviced. In case an error
272
 *      occurs, I will report it on the next user request.
273
 *
274
 *      In the pipelined read mode, subsequent read requests or forward
275
 *      filemark spacing will perform correctly, as we preserve all blocks
276
 *      and filemarks which we encountered during our excess read-ahead.
277
 *
278
 *      For accurate tape positioning and error reporting, disabling
279
 *      pipelined mode might be the best option.
280
 *
281
 * You can enable/disable/tune the pipelined operation mode by adjusting
282
 * the compile time parameters in ide-tape.h.
283
 */
284
 
285
/*
286
 *      Possible improvements.
287
 *
288
 *      1.      Support for the ATAPI overlap protocol.
289
 *
290
 *              In order to maximize bus throughput, we currently use the DSC
291
 *              overlap method which enables ide.c to service requests from the
292
 *              other device while the tape is busy executing a command. The
293
 *              DSC overlap method involves polling the tape's status register
294
 *              for the DSC bit, and servicing the other device while the tape
295
 *              isn't ready.
296
 *
297
 *              In the current QIC development standard (December 1995),
298
 *              it is recommended that new tape drives will *in addition*
299
 *              implement the ATAPI overlap protocol, which is used for the
300
 *              same purpose - efficient use of the IDE bus, but is interrupt
301
 *              driven and thus has much less CPU overhead.
302
 *
303
 *              ATAPI overlap is likely to be supported in most new ATAPI
304
 *              devices, including new ATAPI cdroms, and thus provides us
305
 *              a method by which we can achieve higher throughput when
306
 *              sharing a (fast) ATA-2 disk with any (slow) new ATAPI device.
307
 */
308
 
309
#include <linux/config.h>
310
#include <linux/hdreg.h>
311
#include <linux/types.h>
312
#include <linux/string.h>
313
#include <linux/kernel.h>
314
#include <linux/delay.h>
315
#include <linux/timer.h>
316
#include <linux/mm.h>
317
#include <linux/ioport.h>
318
#include <linux/interrupt.h>
319
#include <linux/major.h>
320
#include <linux/blkdev.h>
321
#include <linux/errno.h>
322
#include <linux/hdreg.h>
323
#include <linux/genhd.h>
324
#include <linux/malloc.h>
325
 
326
#include <asm/byteorder.h>
327
#include <asm/irq.h>
328
#include <asm/segment.h>
329
#include <asm/io.h>
330
 
331
/*
332
 *      Main Linux ide driver include file
333
 *
334
 *      Automatically includes our include file - ide-tape.h.
335
 */
336
 
337
#include "ide.h"                
338
 
339
/*
340
 *      Supported ATAPI tape drives packet commands
341
 */
342
 
343
#define IDETAPE_TEST_UNIT_READY_CMD     0x00
344
#define IDETAPE_REWIND_CMD              0x01
345
#define IDETAPE_REQUEST_SENSE_CMD       0x03
346
#define IDETAPE_READ_CMD                0x08
347
#define IDETAPE_WRITE_CMD               0x0a
348
#define IDETAPE_WRITE_FILEMARK_CMD      0x10
349
#define IDETAPE_SPACE_CMD               0x11
350
#define IDETAPE_INQUIRY_CMD             0x12
351
#define IDETAPE_ERASE_CMD               0x19
352
#define IDETAPE_MODE_SENSE_CMD          0x1a
353
#define IDETAPE_LOAD_UNLOAD_CMD         0x1b
354
#define IDETAPE_LOCATE_CMD              0x2b
355
#define IDETAPE_READ_POSITION_CMD       0x34
356
 
357
/*
358
 *      Some defines for the SPACE command
359
 *
360
 *      (The code field in the SPACE packet command).
361
 */
362
 
363
#define IDETAPE_SPACE_OVER_FILEMARK     1
364
#define IDETAPE_SPACE_TO_EOD            3
365
 
366
/*
367
 *      Some defines for the LOAD UNLOAD command
368
 */
369
 
370
#define IDETAPE_LU_LOAD_MASK            1
371
#define IDETAPE_LU_RETENSION_MASK       2
372
#define IDETAPE_LU_EOT_MASK             4
373
 
374
/*
375
 *      Our ioctls - We will use 0x034n and 0x035n
376
 *
377
 *      Nothing special meanwhile.
378
 *      mtio.h MTIOCTOP compatible commands are supported on the character
379
 *      device interface.
380
 */
381
 
382
/*
383
 *      Special requests for our block device strategy routine.
384
 *
385
 *      In order to service a character device command, we add special
386
 *      requests to the tail of our block device request queue and wait
387
 *      for their completion.
388
 *
389
 */
390
 
391
#define IDETAPE_FIRST_REQUEST                   90
392
 
393
/*
394
 *      IDETAPE_PACKET_COMMAND_REQUEST_TYPE1 is used to queue a packet command
395
 *      in the request queue. We will wait for DSC before issuing the command
396
 *      if it is still not set. In that case, we will temporary replace the
397
 *      cmd field to type 2 and restore it back to type 1 when we receive DSC
398
 *      and can start with sending the command.
399
 */
400
 
401
#define IDETAPE_PACKET_COMMAND_REQUEST_TYPE1    90
402
#define IDETAPE_PACKET_COMMAND_REQUEST_TYPE2    91
403
 
404
/*
405
 *      IDETAPE_READ_REQUEST and IDETAPE_WRITE_REQUEST are used by our
406
 *      character device interface to request read/write operations from
407
 *      our block device interface.
408
 *
409
 *      In case a read or write request was requested by the buffer cache
410
 *      and not by our character device interface, the cmd field in the
411
 *      request will contain READ and WRITE instead.
412
 *
413
 *      We handle both cases in a similar way. The main difference is that
414
 *      in our own requests, buffer head is NULL and idetape_end_request
415
 *      will update the errors field if the request was not completed.
416
 */
417
 
418
#define IDETAPE_READ_REQUEST                    92
419
#define IDETAPE_WRITE_REQUEST                   93
420
#define IDETAPE_ABORTED_WRITE_REQUEST           94
421
 
422
#define IDETAPE_LAST_REQUEST                    94
423
 
424
/*
425
 *      A macro which can be used to check if a we support a given
426
 *      request command.
427
 */
428
 
429
#define IDETAPE_REQUEST_CMD(cmd)        ((cmd >= IDETAPE_FIRST_REQUEST) && (cmd <= IDETAPE_LAST_REQUEST))
430
 
431
/*
432
 *      We are now able to postpone an idetape request in the stage
433
 *      where it is polling for DSC and service requests from the other
434
 *      ide device meanwhile.
435
 */
436
 
437
#define IDETAPE_RQ_POSTPONED            0x1234
438
 
439
/*
440
 *      Error codes which are returned in rq->errors to the higher part
441
 *      of the driver.
442
 */
443
 
444
#define IDETAPE_RQ_ERROR_GENERAL        1 
445
#define IDETAPE_RQ_ERROR_FILEMARK       2
446
#define IDETAPE_RQ_ERROR_EOD            3
447
 
448
/*
449
 *      ATAPI Task File Registers (Re-definition of the ATA Task File
450
 *      Registers for an ATAPI packet command).
451
 *      From Table 3-2 of QIC-157C.
452
 */
453
 
454
/* Read Access */
455
 
456
#define IDETAPE_DATA_OFFSET             (0)
457
#define IDETAPE_ERROR_OFFSET            (1)
458
#define IDETAPE_IREASON_OFFSET          (2)
459
#define IDETAPE_RESERVED3_OFFSET        (3)
460
#define IDETAPE_BCOUNTL_OFFSET          (4)
461
#define IDETAPE_BCOUNTH_OFFSET          (5)
462
#define IDETAPE_DRIVESEL_OFFSET         (6)
463
#define IDETAPE_STATUS_OFFSET           (7)
464
 
465
#define IDETAPE_DATA_REG                (HWIF(drive)->io_base+IDETAPE_DATA_OFFSET)
466
#define IDETAPE_ERROR_REG               (HWIF(drive)->io_base+IDETAPE_ERROR_OFFSET)
467
#define IDETAPE_IREASON_REG             (HWIF(drive)->io_base+IDETAPE_IREASON_OFFSET)
468
#define IDETAPE_RESERVED3_REG           (HWIF(drive)->io_base+IDETAPE_RESERVED3_OFFSET)
469
#define IDETAPE_BCOUNTL_REG             (HWIF(drive)->io_base+IDETAPE_BCOUNTL_OFFSET)
470
#define IDETAPE_BCOUNTH_REG             (HWIF(drive)->io_base+IDETAPE_BCOUNTH_OFFSET)
471
#define IDETAPE_DRIVESEL_REG            (HWIF(drive)->io_base+IDETAPE_DRIVESEL_OFFSET)
472
#define IDETAPE_STATUS_REG              (HWIF(drive)->io_base+IDETAPE_STATUS_OFFSET)
473
 
474
/* Write Access */
475
 
476
#define IDETAPE_FEATURES_OFFSET         (1)
477
#define IDETAPE_ATACOMMAND_OFFSET       (7)
478
 
479
#define IDETAPE_FEATURES_REG            (HWIF(drive)->io_base+IDETAPE_FEATURES_OFFSET)
480
#define IDETAPE_ATACOMMAND_REG          (HWIF(drive)->io_base+IDETAPE_ATACOMMAND_OFFSET)
481
#define IDETAPE_CONTROL_REG             (HWIF(drive)->ctl_port)
482
 
483
 
484
/*
485
 *      Structure of the various task file registers
486
 */
487
 
488
/*
489
 *      The ATAPI Status Register.
490
 */
491
 
492
typedef union {
493
        unsigned all                    :8;
494
        struct {
495
                unsigned check          :1;     /* Error occurred */
496
                unsigned idx            :1;     /* Reserved */
497
                unsigned corr           :1;     /* Correctable error occurred */
498
                unsigned drq            :1;     /* Data is request by the device */
499
                unsigned dsc            :1;     /* Set when a media access command is finished */
500
                                                /* Reads / Writes are NOT media access commands */
501
                unsigned reserved5      :1;     /* Reserved */
502
                unsigned drdy           :1;     /* Ignored for ATAPI commands */
503
                                                /* (The device is ready to accept ATA command) */
504
                unsigned bsy            :1;     /* The device has access to the command block */
505
        } b;
506
} idetape_status_reg_t;
507
 
508
/*
509
 *      The ATAPI error register.
510
 */
511
 
512
typedef union {
513
        unsigned all                    :8;
514
        struct {
515
                unsigned ili            :1;     /* Illegal Length Indication */
516
                unsigned eom            :1;     /* End Of Media Detected */
517
                unsigned abrt           :1;     /* Aborted command - As defined by ATA */
518
                unsigned mcr            :1;     /* Media Change Requested - As defined by ATA */
519
                unsigned sense_key      :4;     /* Sense key of the last failed packet command */
520
        } b;
521
} idetape_error_reg_t;
522
 
523
/*
524
 *      ATAPI Feature Register
525
 */
526
 
527
typedef union {
528
        unsigned all                    :8;
529
        struct {
530
                unsigned dma            :1;     /* Using DMA of PIO */
531
                unsigned reserved321    :3;     /* Reserved */
532
                unsigned reserved654    :3;     /* Reserved (Tag Type) */
533
                unsigned reserved7      :1;     /* Reserved */
534
        } b;
535
} idetape_feature_reg_t;
536
 
537
/*
538
 *      ATAPI Byte Count Register.
539
 */
540
 
541
typedef union {
542
        unsigned all                    :16;
543
        struct {
544
                unsigned low            :8;     /* LSB */
545
                unsigned high           :8;     /* MSB */
546
        } b;
547
} idetape_bcount_reg_t;
548
 
549
/*
550
 *      ATAPI Interrupt Reason Register.
551
 */
552
 
553
typedef union {
554
        unsigned all                    :8;
555
        struct {
556
                unsigned cod            :1;     /* Information transferred is command (1) or data (0) */
557
                unsigned io             :1;     /* The device requests us to read (1) or write (0) */
558
                unsigned reserved       :6;     /* Reserved */
559
        } b;
560
} idetape_ireason_reg_t;
561
 
562
/*
563
 *      ATAPI Drive Select Register
564
 */
565
 
566
typedef union {
567
        unsigned all                    :8;
568
        struct {
569
                unsigned sam_lun        :4;     /* Should be zero with ATAPI (not used) */
570
                unsigned drv            :1;     /* The responding drive will be drive 0 (0) or drive 1 (1) */
571
                unsigned one5           :1;     /* Should be set to 1 */
572
                unsigned reserved6      :1;     /* Reserved */
573
                unsigned one7           :1;     /* Should be set to 1 */
574
        } b;
575
} idetape_drivesel_reg_t;
576
 
577
/*
578
 *      ATAPI Device Control Register
579
 */
580
 
581
typedef union {
582
        unsigned all                    :8;
583
        struct {
584
                unsigned zero0          :1;     /* Should be set to zero */
585
                unsigned nien           :1;     /* Device interrupt is disabled (1) or enabled (0) */
586
                unsigned srst           :1;     /* ATA software reset. ATAPI devices should use the new ATAPI srst. */
587
                unsigned one3           :1;     /* Should be set to 1 */
588
                unsigned reserved4567   :4;     /* Reserved */
589
        } b;
590
} idetape_control_reg_t;
591
 
592
/*
593
 *      idetape_chrdev_t provides the link between out character device
594
 *      interface and our block device interface and the corresponding
595
 *      ide_drive_t structure.
596
 *
597
 *      We currently support only one tape drive.
598
 *
599
 */
600
 
601
typedef struct {
602
        ide_drive_t *drive;
603
        int major,minor;
604
        char name[4];
605
} idetape_chrdev_t;
606
 
607
/*
608
 *      The following is used to format the general configuration word of
609
 *      the ATAPI IDENTIFY DEVICE command.
610
 */
611
 
612
struct idetape_id_gcw {
613
 
614
        unsigned packet_size    :2;     /* Packet Size */
615
        unsigned reserved2      :1;     /* Reserved */
616
        unsigned reserved3      :1;     /* Reserved */
617
        unsigned reserved4      :1;     /* Reserved */
618
        unsigned drq_type       :2;     /* Command packet DRQ type */
619
        unsigned removable      :1;     /* Removable media */
620
        unsigned device_type    :5;     /* Device type */
621
        unsigned reserved13     :1;     /* Reserved */
622
        unsigned protocol       :2;     /* Protocol type */
623
};
624
 
625
/*
626
 *      INQUIRY packet command - Data Format (From Table 6-8 of QIC-157C)
627
 */
628
 
629
typedef struct {
630
        unsigned device_type    :5;     /* Peripheral Device Type */
631
        unsigned reserved0_765  :3;     /* Peripheral Qualifier - Reserved */
632
        unsigned reserved1_6t0  :7;     /* Reserved */
633
        unsigned rmb            :1;     /* Removable Medium Bit */
634
        unsigned ansi_version   :3;     /* ANSI Version */
635
        unsigned ecma_version   :3;     /* ECMA Version */
636
        unsigned iso_version    :2;     /* ISO Version */
637
        unsigned response_format :4;    /* Response Data Format */
638
        unsigned reserved3_45   :2;     /* Reserved */
639
        unsigned reserved3_6    :1;     /* TrmIOP - Reserved */
640
        unsigned reserved3_7    :1;     /* AENC - Reserved */
641
        byte additional_length;         /* Additional Length (total_length-4) */
642
        byte reserved_5;                /* Reserved */
643
        byte reserved_6;                /* Reserved */
644
        unsigned reserved7_0    :1;     /* SftRe - Reserved */
645
        unsigned reserved7_1    :1;     /* CmdQue - Reserved */
646
        unsigned reserved7_2    :1;     /* Reserved */
647
        unsigned reserved7_3    :1;     /* Linked - Reserved */
648
        unsigned reserved7_4    :1;     /* Sync - Reserved */
649
        unsigned reserved7_5    :1;     /* WBus16 - Reserved */
650
        unsigned reserved7_6    :1;     /* WBus32 - Reserved */
651
        unsigned reserved7_7    :1;     /* RelAdr - Reserved */
652
        byte vendor_id [8];             /* Vendor Identification */
653
        byte product_id [16];           /* Product Identification */
654
        byte revision_level [4];        /* Revision Level */
655
        byte vendor_specific [20];      /* Vendor Specific - Optional */
656
        byte reserved56t95 [40];        /* Reserved - Optional */
657
 
658
                                        /* Additional information may be returned */
659
} idetape_inquiry_result_t;
660
 
661
/*
662
 *      READ POSITION packet command - Data Format (From Table 6-57)
663
 */
664
 
665
typedef struct {
666
        unsigned reserved0_10   :2;     /* Reserved */
667
        unsigned bpu            :1;     /* Block Position Unknown */
668
        unsigned reserved0_543  :3;     /* Reserved */
669
        unsigned eop            :1;     /* End Of Partition */
670
        unsigned bop            :1;     /* Beginning Of Partition */
671
        byte partition_num;             /* Partition Number */
672
        byte reserved_2;                /* Reserved */
673
        byte reserved_3;                /* Reserved */
674
        unsigned long first_block;      /* First Block Location */
675
        unsigned long last_block;       /* Last Block Location (Optional) */
676
        byte reserved_12;               /* Reserved */
677
        byte blocks_in_buffer_2;        /* Blocks In Buffer - MSB (Optional) */
678
        byte blocks_in_buffer_1;
679
        byte blocks_in_buffer_0;        /* Blocks In Buffer - LSB (Optional) */
680
        unsigned long bytes_in_buffer;  /* Bytes In Buffer (Optional) */
681
} idetape_read_position_result_t;
682
 
683
/*
684
 *      REQUEST SENSE packet command result - Data Format.
685
 */
686
 
687
typedef struct {
688
        unsigned error_code     :7;     /* Current of deferred errors */
689
        unsigned valid          :1;     /* The information field conforms to QIC-157C */
690
        unsigned reserved_1     :8;     /* Segment Number - Reserved */
691
        unsigned sense_key      :4;     /* Sense Key */
692
        unsigned reserved2_4    :1;     /* Reserved */
693
        unsigned ili            :1;     /* Incorrect Length Indicator */
694
        unsigned eom            :1;     /* End Of Medium */
695
        unsigned filemark       :1;     /* Filemark */
696
 
697
        /*
698
         *      We can't use a 32 bit variable, since it will be re-aligned
699
         *      by GCC, as we are not on a 32 bit boundary.
700
         */
701
 
702
        byte information1;              /* MSB - Information - Command specific */
703
        byte information2;
704
        byte information3;
705
        byte information4;              /* LSB */
706
        byte asl;                       /* Additional sense length (n-7) */
707
        unsigned long command_specific; /* Additional command specific information */
708
        byte asc;                       /* Additional Sense Code */
709
        byte ascq;                      /* Additional Sense Code Qualifier */
710
        byte replaceable_unit_code;     /* Field Replaceable Unit Code */
711
        unsigned sk_specific1   :7;     /* Sense Key Specific */
712
        unsigned sksv           :1;     /* Sense Key Specific information is valid */
713
        byte sk_specific2;              /* Sense Key Specific */
714
        byte sk_specific3;              /* Sense Key Specific */
715
        byte pad [2];                   /* Padding to 20 bytes */
716
} idetape_request_sense_result_t;
717
 
718
/*
719
 *      Follows structures which are related to the SELECT SENSE / MODE SENSE
720
 *      packet commands. Those packet commands are still not supported
721
 *      by ide-tape.
722
 */
723
 
724
#define IDETAPE_CAPABILITIES_PAGE       0x2a
725
 
726
/*
727
 *      Mode Parameter Header for the MODE SENSE packet command
728
 */
729
 
730
typedef struct {
731
        byte mode_data_length;          /* The length of the following data that is */
732
                                        /* available to be transferred */
733
        byte medium_type;               /* Medium Type */
734
        byte dsp;                       /* Device Specific Parameter */
735
        byte bdl;                       /* Block Descriptor Length */
736
} idetape_mode_parameter_header_t;
737
 
738
/*
739
 *      Mode Parameter Block Descriptor the MODE SENSE packet command
740
 *
741
 *      Support for block descriptors is optional.
742
 */
743
 
744
typedef struct {
745
        byte density_code;              /* Medium density code */
746
        byte blocks1;                   /* Number of blocks - MSB */
747
        byte blocks2;                   /* Number of blocks - Middle byte */
748
        byte blocks3;                   /* Number of blocks - LSB */
749
        byte reserved4;                 /* Reserved */
750
        byte length1;                   /* Block Length - MSB */
751
        byte length2;                   /* Block Length - Middle byte */
752
        byte length3;                   /* Block Length - LSB */
753
} idetape_parameter_block_descriptor_t;
754
 
755
/*
756
 *      The Data Compression Page, as returned by the MODE SENSE packet command.
757
 */
758
 
759
typedef struct {
760
        unsigned page_code      :6;     /* Page Code - Should be 0xf */
761
        unsigned reserved       :1;     /* Reserved */
762
        unsigned ps             :1;
763
        byte page_length;               /* Page Length - Should be 14 */
764
        unsigned reserved2      :6;     /* Reserved */
765
        unsigned dcc            :1;     /* Data Compression Capable */
766
        unsigned dce            :1;     /* Data Compression Enable */
767
        unsigned reserved3      :5;     /* Reserved */
768
        unsigned red            :2;     /* Report Exception on Decompression */
769
        unsigned dde            :1;     /* Data Decompression Enable */
770
        unsigned long ca;               /* Compression Algorithm */
771
        unsigned long da;               /* Decompression Algorithm */
772
        byte reserved_12;               /* Reserved */
773
        byte reserved_13;               /* Reserved */
774
        byte reserved_14;               /* Reserved */
775
        byte reserved_15;               /* Reserved */
776
} idetape_data_compression_page_t;
777
 
778
/*
779
 *      The Medium Partition Page, as returned by the MODE SENSE packet command.
780
 */
781
 
782
typedef struct {
783
        unsigned page_code      :6;     /* Page Code - Should be 0x11 */
784
        unsigned reserved1_6    :1;     /* Reserved */
785
        unsigned ps             :1;
786
        byte page_length;               /* Page Length - Should be 6 */
787
        byte map;                       /* Maximum Additional Partitions - Should be 0 */
788
        byte apd;                       /* Additional Partitions Defined - Should be 0 */
789
        unsigned reserved4_012  :3;     /* Reserved */
790
        unsigned psum           :2;     /* Should be 0 */
791
        unsigned idp            :1;     /* Should be 0 */
792
        unsigned sdp            :1;     /* Should be 0 */
793
        unsigned fdp            :1;     /* Fixed Data Partitions */
794
        byte mfr;                       /* Medium Format Recognition */
795
        byte reserved6;                 /* Reserved */
796
        byte reserved7;                 /* Reserved */
797
} idetape_medium_partition_page_t;
798
 
799
/*
800
 *      Prototypes of various functions in ide-tape.c
801
 *
802
 *      The following functions are called from ide.c, and their prototypes
803
 *      are available in ide.h:
804
 *
805
 *              idetape_identify_device
806
 *              idetape_setup
807
 *              idetape_blkdev_ioctl
808
 *              idetape_do_request
809
 *              idetape_blkdev_open
810
 *              idetape_blkdev_release
811
 *              idetape_register_chrdev (void);
812
 */
813
 
814
/*
815
 *      The following functions are used to transfer data from / to the
816
 *      tape's data register.
817
 */
818
 
819
void idetape_input_data (ide_drive_t *drive,void *buffer, unsigned long bcount);
820
void idetape_output_data (ide_drive_t *drive,void *buffer, unsigned long bcount);
821
void idetape_discard_data (ide_drive_t *drive, unsigned long bcount);
822
 
823
/*
824
 *      Packet command related functions.
825
 */
826
 
827
void idetape_issue_packet_command  (ide_drive_t *drive,idetape_packet_command_t *pc,ide_handler_t *handler);
828
void idetape_pc_intr (ide_drive_t *drive);
829
 
830
/*
831
 *      DSC handling functions.
832
 */
833
 
834
void idetape_postpone_request (ide_drive_t *drive);
835
void idetape_poll_for_dsc (unsigned long data);
836
void idetape_poll_for_dsc_direct (unsigned long data);
837
void idetape_put_back_postponed_request (ide_drive_t *drive);
838
void idetape_media_access_finished (ide_drive_t *drive);
839
 
840
/*
841
 *      Some more packet command related functions.
842
 */
843
 
844
void idetape_pc_callback (ide_drive_t *drive);
845
void idetape_retry_pc (ide_drive_t *drive);
846
void idetape_zero_packet_command (idetape_packet_command_t *pc);
847
void idetape_queue_pc_head (ide_drive_t *drive,idetape_packet_command_t *pc,struct request *rq);
848
void idetape_analyze_error (ide_drive_t *drive,idetape_request_sense_result_t *result);
849
 
850
idetape_packet_command_t *idetape_next_pc_storage (ide_drive_t *drive);
851
struct request *idetape_next_rq_storage (ide_drive_t *drive);
852
 
853
/*
854
 *      Various packet commands
855
 */
856
 
857
void idetape_create_inquiry_cmd (idetape_packet_command_t *pc);
858
void idetape_inquiry_callback (ide_drive_t *drive);
859
void idetape_create_locate_cmd (idetape_packet_command_t *pc,unsigned long block,byte partition);
860
void idetape_create_rewind_cmd (idetape_packet_command_t *pc);
861
void idetape_create_write_filemark_cmd (idetape_packet_command_t *pc,int write_filemark);
862
void idetape_create_load_unload_cmd (idetape_packet_command_t *pc,int cmd);
863
void idetape_create_space_cmd (idetape_packet_command_t *pc,long count,byte cmd);
864
void idetape_create_erase_cmd (idetape_packet_command_t *pc);
865
void idetape_create_test_unit_ready_cmd (idetape_packet_command_t *pc);
866
void idetape_create_read_position_cmd (idetape_packet_command_t *pc);
867
void idetape_read_position_callback (ide_drive_t *drive);
868
void idetape_create_read_cmd (idetape_packet_command_t *pc,unsigned long length);
869
void idetape_read_callback (ide_drive_t *drive);
870
void idetape_create_write_cmd (idetape_packet_command_t *pc,unsigned long length);
871
void idetape_write_callback (ide_drive_t *drive);
872
void idetape_create_request_sense_cmd (idetape_packet_command_t *pc);
873
void idetape_create_mode_sense_cmd (idetape_packet_command_t *pc,byte page_code);
874
void idetape_request_sense_callback (ide_drive_t *drive);
875
 
876
void idetape_display_inquiry_result (byte *buffer);
877
 
878
/*
879
 *      Character device callback functions.
880
 *
881
 *      We currently support:
882
 *
883
 *              OPEN, RELEASE, READ, WRITE and IOCTL.
884
 */
885
 
886
int idetape_chrdev_read (struct inode *inode, struct file *file, char *buf, int count);
887
int idetape_chrdev_write (struct inode *inode, struct file *file, const char *buf, int count);
888
int idetape_chrdev_ioctl (struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg);
889
int idetape_chrdev_open (struct inode *inode, struct file *file);
890
void idetape_chrdev_release (struct inode *inode,struct file *file);
891
 
892
/*
893
 *      idetape_mtioctop implements general magnetic tape io control
894
 *      commands, as defined in include/linux/mtio.h. Those commands are
895
 *      accessed through the character device interface, using the MTIOCTOP
896
 *      ioctl.
897
 */
898
 
899
int idetape_mtioctop (ide_drive_t *drive,short mt_op,int mt_count);
900
 
901
/*
902
 *      idetape_space_over_filemarks handles the MTFSF, MTFSFM, ... mtio.h
903
 *      commands.
904
 */
905
 
906
int idetape_space_over_filemarks (ide_drive_t *drive,short mt_op,int mt_count);
907
 
908
/*
909
 *      idetape_add_chrdev_read_request is called from idetape_chrdev_read
910
 *      to service a character device read request and add read-ahead
911
 *      requests to our pipeline.
912
 */
913
 
914
int idetape_add_chrdev_read_request (ide_drive_t *drive,int blocks,char *buffer);
915
 
916
/*
917
 *      idetape_add_chrdev_write_request adds a character device write
918
 *      request to the pipeline.
919
 */
920
 
921
int idetape_add_chrdev_write_request (ide_drive_t *drive,int blocks,char *buffer);
922
 
923
/*
924
 *      idetape_queue_rw_tail will add a command to the tail of the device
925
 *      request queue and wait for it to finish. This is used when we
926
 *      can not allocate pipeline stages (or in non-pipelined mode).
927
 */
928
 
929
int idetape_queue_rw_tail (ide_drive_t *drive,int cmd,int blocks,char *buffer);
930
 
931
/*
932
 *      Adds a packet command request to the tail of the device request
933
 *      queue and waits for it to be serviced.
934
 */
935
 
936
int idetape_queue_pc_tail (ide_drive_t *drive,idetape_packet_command_t *pc);
937
 
938
int idetape_position_tape (ide_drive_t *drive,unsigned long block);
939
int idetape_rewind_tape (ide_drive_t *drive);
940
int idetape_flush_tape_buffers (ide_drive_t *drive);
941
 
942
/*
943
 *      Used to get device information
944
 */
945
 
946
void idetape_get_mode_sense_results (ide_drive_t *drive);
947
 
948
/*
949
 *      General utility functions
950
 */
951
 
952
unsigned long idetape_swap_long (unsigned long temp);
953
unsigned short idetape_swap_short (unsigned short temp);
954
 
955
#define IDETAPE_MIN(a,b)        ((a)<(b) ? (a):(b))
956
 
957
/*
958
 *      Pipeline related functions
959
 */
960
 
961
idetape_pipeline_stage_t *idetape_kmalloc_stage (ide_drive_t *drive);
962
void idetape_kfree_stage (idetape_pipeline_stage_t *stage);
963
void idetape_copy_buffer_from_stage (idetape_pipeline_stage_t *stage,char *buffer);
964
void idetape_copy_buffer_to_stage (idetape_pipeline_stage_t *stage,char *buffer);
965
void idetape_increase_max_pipeline_stages (ide_drive_t *drive);
966
void idetape_add_stage_tail (ide_drive_t *drive,idetape_pipeline_stage_t *stage);
967
void idetape_remove_stage_head (ide_drive_t *drive);
968
void idetape_active_next_stage (ide_drive_t *drive);
969
void idetape_wait_for_pipeline (ide_drive_t *drive);
970
void idetape_discard_read_pipeline (ide_drive_t *drive);
971
void idetape_empty_write_pipeline (ide_drive_t *drive);
972
void idetape_insert_pipeline_into_queue (ide_drive_t *drive);
973
 
974
/*
975
 *      For general magnetic tape device compatibility.
976
 */
977
 
978
#include <linux/mtio.h>
979
 
980
/*
981
 *      Global variables
982
 *
983
 *      The variables below are used for the character device interface.
984
 *
985
 *      Additional state variables are defined in our ide_drive_t structure.
986
 */
987
 
988
idetape_chrdev_t idetape_chrdev;                /* Character device interface information */
989
byte idetape_drive_already_found=0;              /* 1 when the above data structure is initialized */
990
 
991
/*
992
 *      Our character device supporting functions, passed to register_chrdev.
993
 */
994
 
995
static struct file_operations idetape_fops = {
996
        NULL,                   /* lseek - default */
997
        idetape_chrdev_read,    /* read  */
998
        idetape_chrdev_write,   /* write */
999
        NULL,                   /* readdir - bad */
1000
        NULL,                   /* select */
1001
        idetape_chrdev_ioctl,   /* ioctl */
1002
        NULL,                   /* mmap */
1003
        idetape_chrdev_open,    /* open */
1004
        idetape_chrdev_release, /* release */
1005
        NULL,                   /* fsync */
1006
        NULL,                   /* fasync */
1007
        NULL,                   /* check_media_change */
1008
        NULL                    /* revalidate */
1009
};
1010
 
1011
 
1012
/*
1013
 *      idetape_identify_device is called by do_identify in ide.c during
1014
 *      the device probing stage to check the contents of the ATAPI IDENTIFY
1015
 *      command results, in case the device type is tape. We return:
1016
 *
1017
 *      1       If the tape can be supported by us, based on the information
1018
 *              we have so far.
1019
 *
1020
 *      0       If this tape driver is not currently supported by us.
1021
 *
1022
 *      In case we decide to support the tape, we store the current drive
1023
 *      pointer in our character device global variables, so that we can
1024
 *      pass between both interfaces.
1025
 */
1026
 
1027
int idetape_identify_device (ide_drive_t *drive,struct hd_driveid *id)
1028
 
1029
{
1030
        struct idetape_id_gcw gcw;
1031
        unsigned short *ptr;
1032
        int support=1;
1033
#if IDETAPE_DEBUG_LOG
1034
        unsigned short mask,i;
1035
#endif /* IDETAPE_DEBUG_LOG */
1036
 
1037
        ptr=(unsigned short *) &gcw;
1038
        *ptr=id->config;
1039
 
1040
#if IDETAPE_DEBUG_LOG
1041
        printk ("Dumping ATAPI Identify Device tape parameters\n");
1042
 
1043
        printk ("Protocol Type: ");
1044
        switch (gcw.protocol) {
1045
                case 0: case 1: printk ("ATA\n");break;
1046
                case 2: printk ("ATAPI\n");break;
1047
                case 3: printk ("Reserved (Unknown to ide-tape)\n");break;
1048
        }
1049
 
1050
        printk ("Device Type: %x - ",gcw.device_type);
1051
        switch (gcw.device_type) {
1052
                case 0: printk ("Direct-access Device\n");break;
1053
                case 1: printk ("Streaming Tape Device\n");break;
1054
                case 2: case 3: case 4: printk ("Reserved\n");break;
1055
                case 5: printk ("CD-ROM Device\n");break;
1056
                case 6: printk ("Reserved\n");
1057
                case 7: printk ("Optical memory Device\n");break;
1058
                case 0x1f: printk ("Unknown or no Device type\n");break;
1059
                default: printk ("Reserved\n");
1060
        }
1061
        printk ("Removable: %s",gcw.removable ? "Yes\n":"No\n");
1062
 
1063
        printk ("Command Packet DRQ Type: ");
1064
        switch (gcw.drq_type) {
1065
                case 0: printk ("Microprocessor DRQ\n");break;
1066
                case 1: printk ("Interrupt DRQ\n");break;
1067
                case 2: printk ("Accelerated DRQ\n");break;
1068
                case 3: printk ("Reserved\n");break;
1069
        }
1070
 
1071
        printk ("Command Packet Size: ");
1072
        switch (gcw.packet_size) {
1073
                case 0: printk ("12 bytes\n");break;
1074
                case 1: printk ("16 bytes\n");break;
1075
                default: printk ("Reserved\n");break;
1076
        }
1077
        printk ("Model: %s\n",id->model);
1078
        printk ("Firmware Revision: %s\n",id->fw_rev);
1079
        printk ("Serial Number: %s\n",id->serial_no);
1080
        printk ("Write buffer size: %d bytes\n",id->buf_size*512);
1081
        printk ("DMA: %s",id->capability & 0x01 ? "Yes\n":"No\n");
1082
        printk ("LBA: %s",id->capability & 0x02 ? "Yes\n":"No\n");
1083
        printk ("IORDY can be disabled: %s",id->capability & 0x04 ? "Yes\n":"No\n");
1084
        printk ("IORDY supported: %s",id->capability & 0x08 ? "Yes\n":"Unknown\n");
1085
        printk ("ATAPI overlap supported: %s",id->capability & 0x20 ? "Yes\n":"No\n");
1086
        printk ("PIO Cycle Timing Category: %d\n",id->tPIO);
1087
        printk ("DMA Cycle Timing Category: %d\n",id->tDMA);
1088
        printk ("Single Word DMA supported modes: ");
1089
        for (i=0,mask=1;i<8;i++,mask=mask << 1) {
1090
                if (id->dma_1word & mask)
1091
                        printk ("%d ",i);
1092
                if (id->dma_1word & (mask << 8))
1093
                        printk ("(active) ");
1094
        }
1095
        printk ("\n");
1096
 
1097
        printk ("Multi Word DMA supported modes: ");
1098
        for (i=0,mask=1;i<8;i++,mask=mask << 1) {
1099
                if (id->dma_mword & mask)
1100
                        printk ("%d ",i);
1101
                if (id->dma_mword & (mask << 8))
1102
                        printk ("(active) ");
1103
        }
1104
        printk ("\n");
1105
 
1106
        if (id->field_valid & 0x0002) {
1107
                printk ("Enhanced PIO Modes: %s\n",id->eide_pio_modes & 1 ? "Mode 3":"None");
1108
                printk ("Minimum Multi-word DMA cycle per word: ");
1109
                if (id->eide_dma_min == 0)
1110
                        printk ("Not supported\n");
1111
                else
1112
                        printk ("%d ns\n",id->eide_dma_min);
1113
 
1114
                printk ("Manufacturer\'s Recommended Multi-word cycle: ");
1115
                if (id->eide_dma_time == 0)
1116
                        printk ("Not supported\n");
1117
                else
1118
                        printk ("%d ns\n",id->eide_dma_time);
1119
 
1120
                printk ("Minimum PIO cycle without IORDY: ");
1121
                if (id->eide_pio == 0)
1122
                        printk ("Not supported\n");
1123
                else
1124
                        printk ("%d ns\n",id->eide_pio);
1125
 
1126
                printk ("Minimum PIO cycle with IORDY: ");
1127
                if (id->eide_pio_iordy == 0)
1128
                        printk ("Not supported\n");
1129
                else
1130
                        printk ("%d ns\n",id->eide_pio_iordy);
1131
 
1132
        }
1133
 
1134
        else {
1135
                printk ("According to the device, fields 64-70 are not valid.\n");
1136
        }
1137
#endif /* IDETAPE_DEBUG_LOG */
1138
 
1139
        /* Check that we can support this device */
1140
 
1141
        if (gcw.protocol !=2 ) {
1142
                printk ("ide-tape: Protocol is not ATAPI\n");support=0;
1143
        }
1144
 
1145
        if (gcw.device_type != 1) {
1146
                printk ("ide-tape: Device type is not set to tape\n");support=0;
1147
        }
1148
 
1149
        if (!gcw.removable) {
1150
                printk ("ide-tape: The removable flag is not set\n");support=0;
1151
        }
1152
 
1153
        if (gcw.packet_size != 0) {
1154
                printk ("ide-tape: Packet size is not 12 bytes long\n");
1155
                if (gcw.packet_size == 1)
1156
                        printk ("ide-tape: Sorry, padding to 16 bytes is still not supported\n");
1157
                support=0;
1158
        }
1159
 
1160
        if (idetape_drive_already_found) {
1161
                printk ("ide-tape: Sorry, only one ide tape drive is supported by the driver\n");
1162
                support=0;
1163
        }
1164
        else {
1165
                idetape_drive_already_found=1;
1166
                idetape_chrdev.drive=drive;
1167
                idetape_chrdev.major=IDETAPE_MAJOR;
1168
                idetape_chrdev.minor=0;
1169
                idetape_chrdev.name[0]='h';
1170
                idetape_chrdev.name[1]='t';
1171
                idetape_chrdev.name[2]='0';
1172
                idetape_chrdev.name[3]=0;
1173
        }
1174
 
1175
        return (support);               /* In case support=0, we will not install the driver */
1176
}
1177
 
1178
/*
1179
 *      idetape_register_chrdev calls register_chrdev to register our character
1180
 *      device interface. The connection to the ide_drive_t structure, which
1181
 *      is used by the entire ide driver is provided by our global variable
1182
 *      idetape_chrdev.drive, which was initialized earlier, during the device
1183
 *      probing stage.
1184
 */
1185
 
1186
void idetape_register_chrdev (void)
1187
 
1188
{
1189
        int major,minor;
1190
        ide_drive_t *drive;
1191
 
1192
        if (!idetape_drive_already_found)
1193
                return;
1194
 
1195
        drive=idetape_chrdev.drive;
1196
        major=idetape_chrdev.major;
1197
        minor=idetape_chrdev.minor;
1198
 
1199
        if (register_chrdev (major,idetape_chrdev.name,&idetape_fops)) {
1200
                printk ("Unable to register character device interface !\n");
1201
                /* ??? */
1202
        }
1203
}
1204
 
1205
/*
1206
 *      idetape_setup is called from the ide driver in the partition table
1207
 *      identification stage, to:
1208
 *
1209
 *              1.      Initialize our various state variables.
1210
 *              2.      Ask the tape for its capabilities.
1211
 *              3.      Allocate a buffer which will be used for data
1212
 *                      transfer. The buffer size is chosen based on
1213
 *                      the recommendation which we received in step (2).
1214
 *
1215
 *      Note that at this point ide.c already assigned us an irq, so that
1216
 *      we can queue requests here and wait for their completion.
1217
 */
1218
 
1219
void idetape_setup (ide_drive_t *drive)
1220
 
1221
{
1222
        idetape_tape_t *tape=&(drive->tape);
1223
        unsigned int allocation_length;
1224
        struct idetape_id_gcw gcw;
1225
 
1226
#if IDETAPE_ANTICIPATE_READ_WRITE_DSC
1227
        ide_hwif_t *hwif = HWIF(drive);
1228
        unsigned long t1, tmid, tn;
1229
#endif /* IDETAPE_ANTICIPATE_READ_WRITE_DSC */
1230
 
1231
#if IDETAPE_DEBUG_LOG
1232
        printk ("ide-tape: Reached idetape_setup\n");
1233
#endif /* IDETAPE_DEBUG_LOG */  
1234
 
1235
        drive->ready_stat = 0;                   /* With an ATAPI device, we can issue packet commands */
1236
                                                /* regardless of the state of DRDY */
1237
        HWIF(drive)->tape_drive=drive;
1238
 
1239
        tape->block_address=0;
1240
        tape->block_address_valid=0;
1241
        tape->pc_stack_index=0;
1242
        tape->failed_pc=NULL;
1243
        tape->postponed_rq=NULL;
1244
        tape->busy=0;
1245
        tape->active_data_request=NULL;
1246
        tape->current_number_of_stages=0;
1247
        tape->first_stage=tape->next_stage=tape->last_stage=NULL;
1248
        tape->error_in_pipeline_stage=0;
1249
        tape->request_status=0;
1250
        tape->chrdev_direction=idetape_direction_none;
1251
        tape->reset_issued=0;
1252
        tape->pc=&(tape->pc_stack [0]);
1253
 
1254
        *((unsigned short *) &gcw) = drive->id->config;
1255
        tape->drq_interrupt = (gcw.drq_type == 1) ? 1 : 0;
1256
 
1257
#if IDETAPE_PIPELINE
1258
        tape->max_number_of_stages=IDETAPE_MIN_PIPELINE_STAGES;
1259
#else
1260
        tape->max_number_of_stages=0;
1261
#endif /* IDETAPE_PIPELINE */
1262
 
1263
        idetape_get_mode_sense_results (drive);
1264
 
1265
        tape->data_buffer_size = tape->capabilities.ctl * tape->tape_block_size;
1266
        while (tape->data_buffer_size > 0xffff) {
1267
                tape->capabilities.ctl /= 2;
1268
                tape->data_buffer_size = tape->capabilities.ctl * tape->tape_block_size;
1269
        }
1270
        allocation_length=tape->data_buffer_size;
1271
        if (tape->data_buffer_size % IDETAPE_ALLOCATION_BLOCK)
1272
                allocation_length+=IDETAPE_ALLOCATION_BLOCK;
1273
 
1274
#if IDETAPE_MINIMIZE_IDLE_MEMORY_USAGE
1275
        tape->data_buffer=tape->merge_buffer=NULL;
1276
#else
1277
        tape->data_buffer=kmalloc (allocation_length,GFP_KERNEL);
1278
        tape->merge_buffer=kmalloc (allocation_length,GFP_KERNEL);
1279
        if (tape->data_buffer == NULL || tape->merge_buffer == NULL) {
1280
                printk ("ide-tape: FATAL - Can not allocate 2 buffers of %d bytes each\n",allocation_length);
1281
                printk ("ide-tape: Aborting character device installation\n");
1282
                idetape_drive_already_found=0;
1283
                unregister_chrdev (idetape_chrdev.major,idetape_chrdev.name);
1284
                return;
1285
        }
1286
#endif /* IDETAPE_MINIMIZE_IDLE_MEMORY_USAGE */
1287
 
1288
        tape->merge_buffer_size=tape->merge_buffer_offset=0;
1289
 
1290
#if IDETAPE_ANTICIPATE_READ_WRITE_DSC
1291
 
1292
        /*
1293
         *      Cleverly select the DSC read/write polling frequency, based
1294
         *      on the tape's speed, its recommended transfer unit, its
1295
         *      internal buffer size and our operation mode.
1296
         *
1297
         *      In the pipelined operation mode we aim for "catching" the
1298
         *      tape when its internal buffer is about 50% full. This will
1299
         *      dramatically reduce our polling frequency and will also
1300
         *      leave enough time for the ongoing request of the other device
1301
         *      to complete before the buffer is completely empty. We will
1302
         *      then completely refill the buffer with requests from our
1303
         *      internal pipeline.
1304
         *
1305
         *      When operating in the non-pipelined operation mode, we
1306
         *      can't allow ourself this luxury. Instead, we will try to take
1307
         *      full advantage of the internal tape buffer by waiting only
1308
         *      for one request to complete. This will increase our load
1309
         *      on linux but will usually still fail to keep the tape
1310
         *      constantly streaming.
1311
         */
1312
 
1313
        /*
1314
         *      We will ignore the above algorithm for now, as it can have
1315
         *      a bad effect on interactive response under some conditions.
1316
         *      The following attempts to find a balance between good latency
1317
         *      and good system throughput. It will be nice to have all this
1318
         *      configurable in run time at some point.
1319
         */
1320
        t1 = (tape->data_buffer_size * HZ) / (tape->capabilities.speed * 1000);
1321
        tmid = (tape->capabilities.buffer_size * 32 * HZ) / (tape->capabilities.speed * 125);
1322
        tn = (IDETAPE_FIFO_THRESHOLD * tape->data_buffer_size * HZ) / (tape->capabilities.speed * 1000);
1323
 
1324
        if (tape->max_number_of_stages) {
1325
                if (drive->using_dma)
1326
                        tape->best_dsc_rw_frequency = tmid;
1327
                else {
1328
                        if (hwif->drives[drive->select.b.unit ^ 1].present || hwif->next != hwif)
1329
                                tape->best_dsc_rw_frequency = IDETAPE_MIN ((tn + tmid) / 2, tmid);
1330
                        else
1331
                                tape->best_dsc_rw_frequency = IDETAPE_MIN (tn, tmid);
1332
                }
1333
        } else
1334
                tape->best_dsc_rw_frequency = t1;
1335
 
1336
        /*
1337
         *      Ensure that the number we got makes sense.
1338
         */
1339
 
1340
        if (tape->best_dsc_rw_frequency > IDETAPE_DSC_READ_WRITE_LOWEST_FREQUENCY) {
1341
                printk ("ide-tape: Although the recommended polling period is %lu jiffies, \n",tape->best_dsc_rw_frequency);
1342
                printk ("ide-tape: we will use %u jiffies\n",IDETAPE_DSC_READ_WRITE_LOWEST_FREQUENCY);
1343
                printk ("ide-tape: (It may well be that we are wrong here)\n");
1344
                tape->best_dsc_rw_frequency = IDETAPE_DSC_READ_WRITE_LOWEST_FREQUENCY;
1345
        }
1346
 
1347
        if (tape->best_dsc_rw_frequency < IDETAPE_DSC_READ_WRITE_FALLBACK_FREQUENCY) {
1348
                printk ("ide-tape: Although the recommended polling period is %lu jiffies, \n",tape->best_dsc_rw_frequency);
1349
                printk ("ide-tape: we will use %u jiffies\n",IDETAPE_DSC_READ_WRITE_FALLBACK_FREQUENCY);
1350
                tape->best_dsc_rw_frequency = IDETAPE_DSC_READ_WRITE_FALLBACK_FREQUENCY;
1351
        }
1352
 
1353
#else
1354
        tape->best_dsc_rw_frequency=IDETAPE_DSC_READ_WRITE_FALLBACK_FREQUENCY;
1355
#endif /* IDETAPE_ANTICIPATE_READ_WRITE_DSC */
1356
 
1357
        printk (KERN_INFO "ide-tape: %s <-> %s, %dKBps, %d*%dkB buffer, %dkB pipeline, %lums tDSC%s\n",
1358
                drive->name, "ht0", tape->capabilities.speed, (tape->capabilities.buffer_size * 512) / tape->data_buffer_size,
1359
                tape->data_buffer_size / 1024, tape->max_number_of_stages * tape->data_buffer_size / 1024,
1360
                tape->best_dsc_rw_frequency * 1000 / HZ, drive->using_dma ? ", DMA":"");
1361
        return;
1362
}
1363
 
1364
/*
1365
 *      idetape_get_mode_sense_results asks the tape about its various
1366
 *      parameters. In particular, we will adjust our data transfer buffer
1367
 *      size to the recommended value as returned by the tape.
1368
 */
1369
 
1370
void idetape_get_mode_sense_results (ide_drive_t *drive)
1371
 
1372
{
1373
        int retval;
1374
        idetape_tape_t *tape=&(drive->tape);
1375
        idetape_mode_parameter_header_t *header;
1376
        idetape_capabilities_page_t *capabilities;
1377
        idetape_packet_command_t pc;
1378
 
1379
        idetape_create_mode_sense_cmd (&pc,IDETAPE_CAPABILITIES_PAGE);
1380
        pc.buffer=pc.temp_buffer;
1381
        pc.buffer_size=IDETAPE_TEMP_BUFFER_SIZE;
1382
        pc.current_position=pc.temp_buffer;
1383
        retval=idetape_queue_pc_tail (drive,&pc);
1384
 
1385
        header=(idetape_mode_parameter_header_t *) pc.buffer;
1386
        capabilities=(idetape_capabilities_page_t *) (pc.buffer+sizeof (idetape_mode_parameter_header_t));
1387
 
1388
        capabilities->max_speed=idetape_swap_short (capabilities->max_speed);
1389
        capabilities->ctl=idetape_swap_short (capabilities->ctl);
1390
        capabilities->speed=idetape_swap_short (capabilities->speed);
1391
        capabilities->buffer_size=idetape_swap_short (capabilities->buffer_size);
1392
 
1393
        if (!capabilities->speed) {
1394
                printk("ide-tape: %s: overriding capabilities->speed (assuming 650KB/sec)\n", drive->name);
1395
                capabilities->speed = 650;
1396
        }
1397
        if (!capabilities->max_speed) {
1398
                printk("ide-tape: %s: overriding capabilities->max_speed (assuming 650KB/sec)\n", drive->name);
1399
                capabilities->max_speed = 650;
1400
        }
1401
 
1402
        tape->capabilities=*capabilities;               /* Save us a copy */
1403
        tape->tape_block_size=capabilities->blk512 ? 512:1024;
1404
 
1405
        if (retval) {
1406
                printk ("ide-tape: Can't get tape parameters\n");
1407
                printk ("ide-tape: Assuming some default parameters\n");
1408
                tape->tape_block_size=512;
1409
                tape->capabilities.ctl=52;
1410
                tape->capabilities.speed=450;
1411
                tape->capabilities.buffer_size=6*52;
1412
                return;
1413
        }
1414
 
1415
#if IDETAPE_DEBUG_LOG
1416
        printk ("Dumping the results of the MODE SENSE packet command\n");
1417
        printk ("Mode Parameter Header:\n");
1418
        printk ("Mode Data Length - %d\n",header->mode_data_length);
1419
        printk ("Medium Type - %d\n",header->medium_type);
1420
        printk ("Device Specific Parameter - %d\n",header->dsp);
1421
        printk ("Block Descriptor Length - %d\n",header->bdl);
1422
 
1423
        printk ("Capabilities and Mechanical Status Page:\n");
1424
        printk ("Page code - %d\n",capabilities->page_code);
1425
        printk ("Page length - %d\n",capabilities->page_length);
1426
        printk ("Read only - %s\n",capabilities->ro ? "Yes":"No");
1427
        printk ("Supports reverse space - %s\n",capabilities->sprev ? "Yes":"No");
1428
        printk ("Supports erase initiated formatting - %s\n",capabilities->efmt ? "Yes":"No");
1429
        printk ("Supports QFA two Partition format - %s\n",capabilities->qfa ? "Yes":"No");
1430
        printk ("Supports locking the medium - %s\n",capabilities->lock ? "Yes":"No");
1431
        printk ("The volume is currently locked - %s\n",capabilities->locked ? "Yes":"No");
1432
        printk ("The device defaults in the prevent state - %s\n",capabilities->prevent ? "Yes":"No");
1433
        printk ("Supports ejecting the medium - %s\n",capabilities->eject ? "Yes":"No");
1434
        printk ("Supports error correction - %s\n",capabilities->ecc ? "Yes":"No");
1435
        printk ("Supports data compression - %s\n",capabilities->cmprs ? "Yes":"No");
1436
        printk ("Supports 512 bytes block size - %s\n",capabilities->blk512 ? "Yes":"No");
1437
        printk ("Supports 1024 bytes block size - %s\n",capabilities->blk1024 ? "Yes":"No");
1438
        printk ("Restricted byte count for PIO transfers - %s\n",capabilities->slowb ? "Yes":"No");
1439
        printk ("Maximum supported speed in KBps - %d\n",capabilities->max_speed);
1440
        printk ("Continuous transfer limits in blocks - %d\n",capabilities->ctl);
1441
        printk ("Current speed in KBps - %d\n",capabilities->speed);
1442
        printk ("Buffer size - %d\n",capabilities->buffer_size*512);
1443
#endif /* IDETAPE_DEBUG_LOG */
1444
}
1445
 
1446
static void idetape_transfer_pc (ide_drive_t *drive)
1447
{
1448
        idetape_tape_t *tape=&(drive->tape);
1449
        idetape_packet_command_t *pc = tape->pc;
1450
        idetape_ireason_reg_t ireason;
1451
 
1452
        if (ide_wait_stat (drive,DRQ_STAT,BUSY_STAT,WAIT_READY)) {
1453
                printk (KERN_ERR "ide-tape: Strange, packet command initiated yet DRQ isn't asserted\n");
1454
                return;
1455
        }
1456
        ireason.all=IN_BYTE (IDE_IREASON_REG);
1457
        if (!ireason.b.cod || ireason.b.io) {
1458
                printk (KERN_ERR "ide-tape: (IO,CoD) != (0,1) while issuing a packet command\n");
1459
                ide_do_reset (drive);
1460
                return;
1461
        }
1462
        ide_set_handler (drive, &idetape_pc_intr, WAIT_CMD);    /* Set the interrupt routine */
1463
        ide_output_data (drive,pc->c,12/4);                     /* Send the actual packet */
1464
}
1465
 
1466
/*
1467
 *      Packet Command Interface
1468
 *
1469
 *      The current Packet Command is available in tape->pc, and will not
1470
 *      change until we finish handling it. Each packet command is associated
1471
 *      with a callback function that will be called when the command is
1472
 *      finished.
1473
 *
1474
 *      The handling will be done in three stages:
1475
 *
1476
 *      1.      idetape_issue_packet_command will send the packet command to the
1477
 *              drive, and will set the interrupt handler to idetape_pc_intr.
1478
 *
1479
 *      2.      On each interrupt, idetape_pc_intr will be called. This step
1480
 *              will be repeated until the device signals us that no more
1481
 *              interrupts will be issued.
1482
 *
1483
 *      3.      ATAPI Tape media access commands have immediate status with a
1484
 *              delayed process. In case of a successful initiation of a
1485
 *              media access packet command, the DSC bit will be set when the
1486
 *              actual execution of the command is finished.
1487
 *              Since the tape drive will not issue an interrupt, we have to
1488
 *              poll for this event. In this case, we define the request as
1489
 *              "low priority request" by setting rq_status to
1490
 *              IDETAPE_RQ_POSTPONED,   set a timer to poll for DSC and exit
1491
 *              the driver.
1492
 *
1493
 *              ide.c will then give higher priority to requests which
1494
 *              originate from the other device, until will change rq_status
1495
 *              to RQ_ACTIVE.
1496
 *
1497
 *      4.      When the packet command is finished, it will be checked for errors.
1498
 *
1499
 *      5.      In case an error was found, we queue a request sense packet command
1500
 *              in front of the request queue and retry the operation up to
1501
 *              IDETAPE_MAX_PC_RETRIES times.
1502
 *
1503
 *      6.      In case no error was found, or we decided to give up and not
1504
 *              to retry again, the callback function will be called and then
1505
 *              we will handle the next request.
1506
 *
1507
 */
1508
 
1509
void idetape_issue_packet_command  (ide_drive_t *drive,idetape_packet_command_t *pc,ide_handler_t *handler)
1510
 
1511
{
1512
        idetape_tape_t *tape;
1513
        idetape_bcount_reg_t bcount;
1514
        int dma_ok=0;
1515
 
1516
        tape=&(drive->tape);
1517
 
1518
#if IDETAPE_DEBUG_BUGS
1519
        if (tape->pc->c[0] == IDETAPE_REQUEST_SENSE_CMD && pc->c[0] == IDETAPE_REQUEST_SENSE_CMD) {
1520
                printk ("ide-tape: possible ide-tape.c bug - Two request sense in serial were issued\n");
1521
        }
1522
#endif /* IDETAPE_DEBUG_BUGS */
1523
 
1524
        if (tape->failed_pc == NULL && pc->c[0] != IDETAPE_REQUEST_SENSE_CMD)
1525
                tape->failed_pc=pc;
1526
        tape->pc=pc;                                                    /* Set the current packet command */
1527
 
1528
        if (pc->retries > IDETAPE_MAX_PC_RETRIES || pc->abort) {
1529
 
1530
                /*
1531
                 *      We will "abort" retrying a packet command in case
1532
                 *      a legitimate error code was received (crossing a
1533
                 *      filemark, or DMA error in the end of media, for
1534
                 *      example).
1535
                 */
1536
 
1537
                if (!pc->abort) {
1538
                        printk ("ide-tape: %s: I/O error, ",drive->name);
1539
                        printk ("pc = %x, key = %x, asc = %x, ascq = %x\n",pc->c[0],tape->sense_key,tape->asc,tape->ascq);
1540
#if IDETAPE_DEBUG_LOG
1541
                        printk ("ide-tape: Maximum retries reached - Giving up\n");
1542
#endif /* IDETAPE_DEBUG_LOG */
1543
                        pc->error=1;                                    /* Giving up */
1544
                }
1545
                tape->failed_pc=NULL;
1546
#if IDETAPE_DEBUG_BUGS
1547
                if (pc->callback==NULL)
1548
                        printk ("ide-tape: ide-tape bug - Callback function not set !\n");
1549
                else
1550
#endif /* IDETAPE_DEBUG_BUGS */
1551
                        (*pc->callback)(drive);
1552
                return;
1553
        }
1554
 
1555
#if IDETAPE_DEBUG_LOG
1556
        printk ("Retry number - %d\n",pc->retries);
1557
#endif /* IDETAPE_DEBUG_LOG */
1558
 
1559
        pc->retries++;
1560
 
1561
/*
1562
 *      We no longer call ide_wait_stat to wait for the drive to be ready,
1563
 *      as ide.c already does this for us in do_request.
1564
 */
1565
 
1566
        pc->actually_transferred=0;                                      /* We haven't transferred any data yet */
1567
        pc->current_position=pc->buffer;
1568
        bcount.all=pc->request_transfer;                                /* Request to transfer the entire buffer at once */
1569
 
1570
#ifdef CONFIG_BLK_DEV_TRITON
1571
        if (pc->dma_error) {
1572
                printk ("ide-tape: DMA disabled, reverting to PIO\n");
1573
                drive->using_dma=0;
1574
                pc->dma_error=0;
1575
        }
1576
        if (pc->request_transfer && pc->dma_recommended && drive->using_dma) {
1577
                dma_ok=!(HWIF(drive)->dmaproc(pc->writing ? ide_dma_write : ide_dma_read, drive));
1578
        }
1579
#endif /* CONFIG_BLK_DEV_TRITON */
1580
 
1581
        OUT_BYTE (drive->ctl,IDETAPE_CONTROL_REG);
1582
        OUT_BYTE (dma_ok ? 1:0,IDETAPE_FEATURES_REG);                    /* Use PIO/DMA */
1583
        OUT_BYTE (bcount.b.high,IDETAPE_BCOUNTH_REG);
1584
        OUT_BYTE (bcount.b.low,IDETAPE_BCOUNTL_REG);
1585
        OUT_BYTE (drive->select.all,IDETAPE_DRIVESEL_REG);
1586
 
1587
#ifdef CONFIG_BLK_DEV_TRITON
1588
        if ((pc->dma_in_progress=dma_ok)) {                     /* Begin DMA, if necessary */
1589
                pc->dma_error=0;
1590
                (void) (HWIF(drive)->dmaproc(ide_dma_begin, drive));
1591
        }
1592
#endif /* CONFIG_BLK_DEV_TRITON */
1593
 
1594
        if (tape->drq_interrupt) {
1595
                ide_set_handler (drive, &idetape_transfer_pc, WAIT_CMD);
1596
                OUT_BYTE (WIN_PACKETCMD, IDE_COMMAND_REG);              /* Issue the packet command */
1597
        } else {
1598
                OUT_BYTE (WIN_PACKETCMD, IDE_COMMAND_REG);
1599
                idetape_transfer_pc (drive);
1600
        }
1601
}
1602
 
1603
/*
1604
 *      idetape_pc_intr is the usual interrupt handler which will be called
1605
 *      during a packet command. We will transfer some of the data (as
1606
 *      requested by the drive) and will re-point interrupt handler to us.
1607
 *      When data transfer is finished, we will act according to the
1608
 *      algorithm described before idetape_issue_packet_command.
1609
 *
1610
 */
1611
 
1612
 
1613
void idetape_pc_intr (ide_drive_t *drive)
1614
 
1615
{
1616
        idetape_tape_t *tape=&(drive->tape);
1617
        idetape_status_reg_t status;
1618
        idetape_bcount_reg_t bcount;
1619
        idetape_ireason_reg_t ireason;
1620
        idetape_packet_command_t *pc=tape->pc;
1621
        unsigned long temp;
1622
 
1623
#ifdef CONFIG_BLK_DEV_TRITON
1624
        if (pc->dma_in_progress) {
1625
                if ((pc->dma_error=HWIF(drive)->dmaproc(ide_dma_status_bad, drive)))
1626
                        /*
1627
                         *      We will currently correct the following in
1628
                         *      idetape_analyze_error.
1629
                         */
1630
                        pc->actually_transferred=HWIF(drive)->dmaproc(ide_dma_transferred, drive);
1631
                else
1632
                        pc->actually_transferred=pc->request_transfer;
1633
                (void) (HWIF(drive)->dmaproc(ide_dma_abort, drive));    /* End DMA */
1634
#if IDETAPE_DEBUG_LOG
1635
                printk ("ide-tape: DMA finished\n");
1636
#endif /* IDETAPE_DEBUG_LOG */
1637
        }
1638
#endif /* CONFIG_BLK_DEV_TRITON */
1639
 
1640
        status.all=IN_BYTE (IDETAPE_STATUS_REG);                /* Clear the interrupt */
1641
 
1642
#if IDETAPE_DEBUG_LOG
1643
        printk ("ide-tape: Reached idetape_pc_intr interrupt handler\n");
1644
#endif /* IDETAPE_DEBUG_LOG */  
1645
 
1646
        if (!status.b.drq) {                                    /* No more interrupts */
1647
#if IDETAPE_DEBUG_LOG
1648
                printk ("Packet command completed\n");
1649
                printk ("Total bytes transferred: %lu\n",pc->actually_transferred);
1650
#endif /* IDETAPE_DEBUG_LOG */
1651
                pc->dma_in_progress=0;
1652
 
1653
                sti ();
1654
 
1655
                if (status.b.check || pc->dma_error) {                  /* Error detected */
1656
#if IDETAPE_DEBUG_LOG
1657
        /*
1658
         *      Without debugging, we only log an error if we decided to
1659
         *      give up retrying.
1660
         */
1661
                        printk ("ide-tape: %s: I/O error, ",drive->name);
1662
#endif /* IDETAPE_DEBUG_LOG */
1663
                        if (pc->c[0] == IDETAPE_REQUEST_SENSE_CMD) {
1664
                                printk ("ide-tape: I/O error in request sense command\n");
1665
                                ide_do_reset (drive);
1666
                                return;
1667
                        }
1668
 
1669
                        idetape_retry_pc (drive);                       /* Retry operation */
1670
                        return;
1671
                }
1672
                pc->error=0;
1673
                if (pc->wait_for_dsc && !status.b.dsc) {                                /* Media access command */
1674
                        tape->dsc_polling_frequency=IDETAPE_DSC_FAST_MEDIA_ACCESS_FREQUENCY;
1675
                        idetape_postpone_request (drive);               /* Allow ide.c to handle other requests */
1676
                        return;
1677
                }
1678
                if (tape->failed_pc == pc)
1679
                        tape->failed_pc=NULL;
1680
#if IDETAPE_DEBUG_BUGS
1681
                if (pc->callback==NULL)
1682
                        printk ("ide-tape: ide-tape bug - Callback function not set !\n");
1683
                else
1684
#endif /* IDETAPE_DEBUG_BUGS */
1685
                        (*pc->callback)(drive);                 /* Command finished - Call the callback function */
1686
                return;
1687
        }
1688
#ifdef CONFIG_BLK_DEV_TRITON
1689
        if (pc->dma_in_progress) {
1690
                pc->dma_in_progress=0;
1691
                printk ("ide-tape: The tape wants to issue more interrupts in DMA mode\n");
1692
                printk ("ide-tape: DMA disabled, reverting to PIO\n");
1693
                drive->using_dma=0;
1694
                ide_do_reset (drive);
1695
                return;
1696
        }
1697
#endif /* CONFIG_BLK_DEV_TRITON */
1698
        bcount.b.high=IN_BYTE (IDETAPE_BCOUNTH_REG);                    /* Get the number of bytes to transfer */
1699
        bcount.b.low=IN_BYTE (IDETAPE_BCOUNTL_REG);                     /* on this interrupt */
1700
        ireason.all=IN_BYTE (IDETAPE_IREASON_REG);                      /* Read the interrupt reason register */
1701
 
1702
        if (ireason.b.cod) {
1703
                printk ("ide-tape: CoD != 0 in idetape_pc_intr\n");
1704
                ide_do_reset (drive);
1705
                return;
1706
        }
1707
        if (ireason.b.io != !(pc->writing)) {                   /* Hopefully, we will never get here */
1708
                printk ("ide-tape: We wanted to %s, ",pc->writing ? "Write":"Read");
1709
                printk ("but the tape wants us to %s !\n",ireason.b.io ? "Read":"Write");
1710
                ide_do_reset (drive);
1711
                return;
1712
        }
1713
 
1714
        if (!pc->writing) {                                     /* Reading - Check that we have enough space */
1715
                temp=(unsigned long) pc->actually_transferred + bcount.all;
1716
                if ( temp > pc->request_transfer) {
1717
                        if (temp > pc->buffer_size) {
1718
                                printk ("ide-tape: The tape wants to send us more data than requested - discarding data\n");
1719
                                idetape_discard_data (drive,bcount.all);
1720
                                ide_set_handler (drive,&idetape_pc_intr,WAIT_CMD);
1721
                                return;
1722
                        }
1723
#if IDETAPE_DEBUG_LOG
1724
                        printk ("ide-tape: The tape wants to send us more data than requested - allowing transfer\n");
1725
#endif /* IDETAPE_DEBUG_LOG */
1726
                }
1727
        }
1728
#if IDETAPE_DEBUG_BUGS  
1729
        if (bcount.all && !pc->buffer) {
1730
                printk ("ide-tape: ide-tape.c bug - Buffer not set in idetape_pc_intr. Discarding data.\n");
1731
 
1732
                if (!pc->writing) {
1733
                        printk ("ide-tape: Discarding data\n");
1734
                        idetape_discard_data (drive,bcount.all);
1735
                        ide_set_handler (drive,&idetape_pc_intr,WAIT_CMD);
1736
                        return;
1737
                }
1738
                else {  /* ??? */
1739
                }
1740
        }
1741
#endif /* IDETAPE_DEBUG_BUGS */
1742
        if (pc->writing)
1743
                idetape_output_data (drive,pc->current_position,bcount.all);    /* Write the current buffer */
1744
        else
1745
                idetape_input_data (drive,pc->current_position,bcount.all);     /* Read the current buffer */
1746
#if IDETAPE_DEBUG_LOG
1747
        printk ("ide-tape: %s %d bytes\n",pc->writing ? "Wrote":"Received",bcount.all);
1748
#endif /* IDETAPE_DEBUG_LOG */
1749
        pc->actually_transferred+=bcount.all;                                   /* Update the current position */
1750
        pc->current_position+=bcount.all;
1751
 
1752
        ide_set_handler (drive,&idetape_pc_intr,WAIT_CMD);              /* And set the interrupt handler again */
1753
}
1754
 
1755
/*
1756
 *      idetape_postpone_request postpones the current request so that
1757
 *      ide.c will be able to service requests from another device on
1758
 *      the same hwgroup while we are polling for DSC.
1759
 */
1760
 
1761
void idetape_postpone_request (ide_drive_t *drive)
1762
 
1763
{
1764
        idetape_tape_t *tape=&(drive->tape);
1765
        struct request *rq;
1766
        idetape_status_reg_t status;
1767
 
1768
#if IDETAPE_DEBUG_LOG
1769
        printk ("Reached idetape_postpone_request\n");
1770
#endif /* IDETAPE_DEBUG_LOG */
1771
#if IDETAPE_DEBUG_BUGS
1772
        if (tape->postponed_rq != NULL)
1773
                printk ("ide-tape.c bug - postponed_rq not NULL in idetape_postpone_request\n");
1774
#endif /* IDETAPE_DEBUG_BUGS */
1775
 
1776
        tape->dsc_timer.expires=jiffies + tape->dsc_polling_frequency;  /* Set timer to poll for */
1777
        tape->dsc_timeout=jiffies+IDETAPE_DSC_TIMEOUT;                  /* actual completion */
1778
        tape->dsc_timer.data=(unsigned long) drive;
1779
        tape->dsc_timer.function=&idetape_poll_for_dsc;
1780
        init_timer (&(tape->dsc_timer));
1781
 
1782
        /*
1783
         * Remove current request from the request queue:
1784
         */
1785
 
1786
        tape->postponed_rq = rq = HWGROUP(drive)->rq;
1787
        rq->rq_status = IDETAPE_RQ_POSTPONED;
1788
        blk_dev[MAJOR(rq->rq_dev)].current_request = rq->next;
1789
        HWGROUP(drive)->rq = NULL;
1790
 
1791
        /*
1792
         *      Check the status again - Maybe we can save one polling period.
1793
         */
1794
 
1795
        status.all=IN_BYTE (IDETAPE_STATUS_REG);
1796
        tape->last_status=status.all;
1797
        tape->request_status=1;
1798
 
1799
        tape->dsc_polling_start=jiffies;
1800
        add_timer(&(tape->dsc_timer));          /* Activate the polling timer */
1801
}
1802
 
1803
/*
1804
 *      idetape_poll_for_dsc_direct is called from idetape_poll_for_dsc
1805
 *      to handle the case in which we can safely communicate with the tape
1806
 *      (since no other request for this hwgroup is active).
1807
 */
1808
 
1809
void idetape_poll_for_dsc_direct (unsigned long data)
1810
 
1811
{
1812
        ide_drive_t *drive=(ide_drive_t *) data;
1813
        idetape_tape_t *tape=&(drive->tape);
1814
        idetape_status_reg_t status;
1815
 
1816
#if IDETAPE_DEBUG_LOG
1817
        printk ("%s: idetape_poll_for_dsc_direct called\n",drive->name);
1818
#endif /* IDETAPE_DEBUG_LOG */  
1819
 
1820
        OUT_BYTE(drive->select.all,IDE_SELECT_REG);
1821
        status.all=IN_BYTE (IDETAPE_STATUS_REG);
1822
 
1823
        if (status.b.dsc) {                                     /* DSC received */
1824
                tape->dsc_received=1;
1825
                del_timer (&(tape->dsc_timer));                 /* Stop polling and put back the postponed */
1826
                idetape_put_back_postponed_request (drive);     /* request in the request queue */
1827
                return;
1828
        }
1829
 
1830
        if (jiffies > tape->dsc_timeout)        { /* Timeout */
1831
                tape->dsc_received=0;
1832
                del_timer (&(tape->dsc_timer));
1833
                /* ??? */
1834
                idetape_put_back_postponed_request (drive);
1835
                return;
1836
        }
1837
 
1838
        /* Poll again */
1839
 
1840
        if (jiffies - tape->dsc_polling_start > IDETAPE_FAST_SLOW_THRESHOLD)
1841
                tape->dsc_timer.expires = jiffies + IDETAPE_DSC_SLOW_MEDIA_ACCESS_FREQUENCY;
1842
        else
1843
                tape->dsc_timer.expires = jiffies + tape->dsc_polling_frequency;
1844
        add_timer(&(tape->dsc_timer));
1845
        return;
1846
}
1847
 
1848
/*
1849
 *      idetape_poll_for_dsc gets invoked by a timer (which was set
1850
 *      by idetape_postpone_request) to poll for the DSC bit
1851
 *      in the status register.
1852
 *
1853
 *      We take care not to perform any tape access if the driver is
1854
 *      accessing the other device. We will instead ask ide.c to sample
1855
 *      the tape status register on our behalf in the next call to do_request,
1856
 *      at the point in which the other device is idle, or assume that
1857
 *      DSC was received even though we won't verify it (but when we assume
1858
 *      that, it will usually have a solid basis).
1859
 *
1860
 *      The use of cli () below is a must, as we inspect and change
1861
 *      the device request list while another request is active.
1862
 */
1863
 
1864
void idetape_poll_for_dsc (unsigned long data)
1865
 
1866
{
1867
        ide_drive_t *drive=(ide_drive_t *) data;
1868
        unsigned int major = HWIF(drive)->major;
1869
        idetape_tape_t *tape=&(drive->tape);
1870
        struct blk_dev_struct *bdev = &blk_dev[major];
1871
        struct request *next_rq;
1872
        unsigned long flags;
1873
        idetape_status_reg_t status;
1874
 
1875
#if IDETAPE_DEBUG_LOG
1876
        printk ("%s: idetape_poll_for_dsc called\n",drive->name);
1877
#endif /* IDETAPE_DEBUG_LOG */  
1878
 
1879
        save_flags (flags);cli ();
1880
 
1881
        /*
1882
         *      Check if the other device is idle. If there are no requests,
1883
         *      we can safely access the tape.
1884
         */
1885
 
1886
        if (HWGROUP (drive)->rq == NULL) {
1887
                sti ();
1888
                idetape_poll_for_dsc_direct (data);
1889
                return;
1890
        }
1891
 
1892
        /*
1893
         *      If DSC was received, re-insert our postponed request into
1894
         *      the request queue (using ide_next).
1895
         */
1896
 
1897
        status.all=tape->last_status;
1898
 
1899
        if (status.b.dsc) {                                     /* DSC received */
1900
                tape->dsc_received=1;
1901
                idetape_put_back_postponed_request (drive);
1902
                del_timer (&(tape->dsc_timer));
1903
                restore_flags (flags);
1904
                return;
1905
        }
1906
 
1907
        /*
1908
         *      At this point, DSC may have been received, but we can't
1909
         *      check it. We now have two options:
1910
         *
1911
         *              1.      The "simple" method - We can continue polling
1912
         *                      until we know the value of DSC.
1913
         *
1914
         *      but we also have a more clever option :-)
1915
         *
1916
         *              2.      We can sometimes more or less anticipate in
1917
         *                      advance how much time it will take for
1918
         *                      the tape to perform the request. This is the
1919
         *                      place to take advantage of this !
1920
         *
1921
         *                      We can assume that DSC was received, put
1922
         *                      back our request, and hope that we will have
1923
         *                      a "cache hit". This will only work when
1924
         *                      we haven't initiated the packet command yet,
1925
         *                      but this is the common read/write case. As
1926
         *                      for the slower media access commands, fallback
1927
         *                      to method 1 above.
1928
         *
1929
         *      When using method 2, we can also take advantage of the
1930
         *      knowledge of the tape's internal buffer size - We can
1931
         *      precalculate the time it will take for the tape to complete
1932
         *      servicing not only one request, but rather, say, 50% of its
1933
         *      internal buffer. The polling period will then be much larger,
1934
         *      decreasing our load on Linux, and we will also call
1935
         *      idetape_postpone_request less often, as there will usually
1936
         *      be more room in the internal tape buffer while we are in
1937
         *      idetape_do_request.
1938
         *
1939
         *      For this method to work well, the ongoing request of the
1940
         *      other device should be serviced by the time the tape is
1941
         *      still working on its remaining 50% internal buffer. This
1942
         *      will usually happen when the other device is much faster
1943
         *      than the tape.
1944
         */
1945
 
1946
#if IDETAPE_ANTICIPATE_READ_WRITE_DSC
1947
 
1948
        /*
1949
         *      Method 2.
1950
         *
1951
         *      There is a high chance that DSC was received, even though
1952
         *      we couldn't verify it. Let's hope that it's a "cache hit"
1953
         *      rather than a "cache miss". Someday I will probably add a
1954
         *      feedback loop around the number of "cache hits" which will
1955
         *      fine-tune the polling period.
1956
         */
1957
 
1958
        if (tape->postponed_rq->cmd != IDETAPE_PACKET_COMMAND_REQUEST_TYPE1) {
1959
 
1960
                /*
1961
                 *      We can use this method only when the packet command
1962
                 *      was still not initiated.
1963
                 */
1964
 
1965
                idetape_put_back_postponed_request (drive);
1966
                del_timer (&(tape->dsc_timer));
1967
                restore_flags (flags);
1968
                return;
1969
        }
1970
#endif /* IDETAPE_ANTICIPATE_READ_WRITE_DSC */
1971
 
1972
        /*
1973
         *      Fallback to method 1.
1974
         */
1975
 
1976
        next_rq=bdev->current_request;
1977
        if (next_rq == HWGROUP (drive)->rq)
1978
                next_rq=next_rq->next;
1979
 
1980
        if (next_rq == NULL) {
1981
 
1982
                /*
1983
                 *      There will not be another request after the currently
1984
                 *      ongoing request, so ide.c won't be able to sample
1985
                 *      the status register on our behalf in do_request.
1986
                 *
1987
                 *      In case we are waiting for DSC before the packet
1988
                 *      command was initiated, we will put back our postponed
1989
                 *      request and have another look at the status register
1990
                 *      in idetape_do_request, as done in method 2 above.
1991
                 *
1992
                 *      In case we already initiated the command, we can't
1993
                 *      put it back, but it is anyway a slow media access
1994
                 *      command. We will just give up and poll again until
1995
                 *      we are lucky.
1996
                 */
1997
 
1998
                if (tape->postponed_rq->cmd == IDETAPE_PACKET_COMMAND_REQUEST_TYPE1) {
1999
 
2000
                        /*
2001
                         *      Media access command - Poll again.
2002
                         *
2003
                         *      We set tape->request_status to 1, just in case
2004
                         *      other requests are added while we are waiting.
2005
                         */
2006
 
2007
                        tape->request_status=1;
2008
                        restore_flags (flags);
2009
                        tape->dsc_timer.expires = jiffies + tape->dsc_polling_frequency;
2010
                        add_timer(&(tape->dsc_timer));
2011
                        return;
2012
                }
2013
 
2014
                /*
2015
                 *      The packet command hasn't been sent to the tape yet -
2016
                 *      We can safely put back the request and have another
2017
                 *      look at the status register in idetape_do_request.
2018
                 */
2019
 
2020
                idetape_put_back_postponed_request (drive);
2021
                del_timer (&(tape->dsc_timer));
2022
                restore_flags (flags);
2023
                return;
2024
        }
2025
 
2026
        /*
2027
         *      There will be another request after the current request.
2028
         *
2029
         *      Request ide.c to sample for us the tape's status register
2030
         *      before the next request.
2031
         */
2032
 
2033
        tape->request_status=1;
2034
        restore_flags (flags);
2035
 
2036
        if (jiffies > tape->dsc_timeout)        {               /* Timeout */
2037
                tape->dsc_received=0;
2038
                /* ??? */
2039
                idetape_put_back_postponed_request (drive);
2040
                del_timer (&(tape->dsc_timer));
2041
                restore_flags (flags);
2042
                return;
2043
        }
2044
 
2045
        /* Poll again */
2046
 
2047
        if (jiffies - tape->dsc_polling_start > IDETAPE_FAST_SLOW_THRESHOLD)
2048
                tape->dsc_timer.expires = jiffies + IDETAPE_DSC_SLOW_MEDIA_ACCESS_FREQUENCY;
2049
        else
2050
                tape->dsc_timer.expires = jiffies + tape->dsc_polling_frequency;
2051
        add_timer(&(tape->dsc_timer));
2052
        return;
2053
}
2054
 
2055
/*
2056
 *      idetape_put_back_postponed_request gets called when we decided to
2057
 *      stop polling for DSC and continue servicing our postponed request.
2058
 */
2059
 
2060
void idetape_put_back_postponed_request (ide_drive_t *drive)
2061
 
2062
{
2063
        idetape_tape_t *tape = &(drive->tape);
2064
 
2065
#if IDETAPE_DEBUG_LOG
2066
        printk ("ide-tape: Putting back postponed request\n");
2067
#endif /* IDETAPE_DEBUG_LOG */
2068
#if IDETAPE_DEBUG_BUGS
2069
        if (tape->postponed_rq == NULL) {
2070
                printk ("tape->postponed_rq is NULL in put_back_postponed_request\n");
2071
                return;
2072
        }
2073
#endif /* IDETAPE_DEBUG_BUGS */
2074
        (void) ide_do_drive_cmd (drive, tape->postponed_rq, ide_next);
2075
 
2076
        /*
2077
         *      Note that the procedure done here is different than the method
2078
         *      we are using in idetape_queue_pc_head - There we are putting
2079
         *      request(s) before our currently called request.
2080
         *
2081
         *      Here, on the other hand, HWGROUP(drive)->rq is not our
2082
         *      request but rather a request to another device. Therefore,
2083
         *      we will let it finish and only then service our postponed
2084
         *      request --> We don't touch HWGROUP(drive)->rq.
2085
         */
2086
}
2087
 
2088
void idetape_media_access_finished (ide_drive_t *drive)
2089
 
2090
{
2091
        idetape_tape_t *tape=&(drive->tape);
2092
        idetape_status_reg_t status;
2093
        idetape_packet_command_t *pc;
2094
 
2095
        pc=tape->pc;
2096
 
2097
        status.all=IN_BYTE (IDETAPE_STATUS_REG);
2098
 
2099
        if (tape->dsc_received) {
2100
#if IDETAPE_DEBUG_LOG
2101
                printk ("DSC received\n");
2102
#endif /* IDETAPE_DEBUG_LOG */
2103
                if (status.b.check) {                                   /* Error detected */
2104
                        printk ("ide-tape: %s: I/O error, ",drive->name);
2105
                        idetape_retry_pc (drive);                       /* Retry operation */
2106
                        return;
2107
                }
2108
                pc->error=0;
2109
                if (tape->failed_pc == pc)
2110
                        tape->failed_pc=NULL;
2111
#if IDETAPE_DEBUG_BUGS
2112
                if (pc->callback==NULL)
2113
                        printk ("ide-tape: ide-tape bug - Callback function not set !\n");
2114
                else
2115
#endif /* IDETAPE_DEBUG_BUGS */
2116
                        (*pc->callback)(drive);
2117
 
2118
                return;
2119
        }
2120
        else {
2121
                printk ("ide-tape: %s: DSC timeout.\n",drive->name);
2122
                /* ??? */
2123
                pc->error=1;
2124
                tape->failed_pc=NULL;
2125
#if IDETAPE_DEBUG_BUGS
2126
                if (pc->callback==NULL)
2127
                        printk ("ide-tape: ide-tape bug - Callback function not set !\n");
2128
                else
2129
#endif /* IDETAPE_DEBUG_BUGS */
2130
                        (*pc->callback)(drive);
2131
                return;
2132
        }
2133
}
2134
 
2135
 
2136
/*
2137
 *      idetape_retry_pc is called when an error was detected during the
2138
 *      last packet command. We queue a request sense packet command in
2139
 *      the head of the request list.
2140
 */
2141
 
2142
void idetape_retry_pc (ide_drive_t *drive)
2143
 
2144
{
2145
        idetape_tape_t *tape = &drive->tape;
2146
        idetape_packet_command_t *pc;
2147
        struct request *new_rq;
2148
 
2149
        idetape_error_reg_t error;
2150
        error.all=IN_BYTE (IDETAPE_ERROR_REG);
2151
        pc=idetape_next_pc_storage (drive);
2152
        new_rq=idetape_next_rq_storage (drive);
2153
        idetape_create_request_sense_cmd (pc);
2154
        pc->buffer=pc->temp_buffer;
2155
        pc->buffer_size=IDETAPE_TEMP_BUFFER_SIZE;
2156
        pc->current_position=pc->temp_buffer;
2157
        tape->reset_issued = 1;
2158
        idetape_queue_pc_head (drive,pc,new_rq);
2159
}
2160
 
2161
/*
2162
 *      General packet command callback function.
2163
 */
2164
 
2165
void idetape_pc_callback (ide_drive_t *drive)
2166
 
2167
{
2168
        idetape_tape_t *tape;
2169
        struct request *rq;
2170
 
2171
        tape=&(drive->tape);
2172
        rq=HWGROUP(drive)->rq;
2173
 
2174
#if IDETAPE_DEBUG_LOG
2175
        printk ("ide-tape: Reached idetape_pc_callback\n");
2176
#endif /* IDETAPE_DEBUG_LOG */
2177
        if (!tape->pc->error) {
2178
#if IDETAPE_DEBUG_LOG
2179
                printk ("Request completed\n");
2180
#endif /* IDETAPE_DEBUG_LOG */
2181
                idetape_end_request (1,HWGROUP (drive));
2182
        }
2183
        else {
2184
                idetape_end_request (0,HWGROUP (drive));
2185
        }
2186
        return;
2187
}
2188
 
2189
 
2190
void idetape_read_callback (ide_drive_t *drive)
2191
 
2192
{
2193
        idetape_tape_t *tape=&(drive->tape);
2194
        struct request *rq=HWGROUP(drive)->rq;
2195
        int blocks_read=tape->pc->actually_transferred/tape->tape_block_size;
2196
 
2197
#if IDETAPE_DEBUG_LOG   
2198
        printk ("ide-tape: Reached idetape_read_callback\n");
2199
#endif /* IDETAPE_DEBUG_LOG */
2200
 
2201
        tape->block_address+=blocks_read;
2202
        rq->current_nr_sectors-=blocks_read;
2203
 
2204
        if (!tape->pc->error)
2205
                idetape_end_request (1,HWGROUP (drive));
2206
        else {
2207
                rq->errors=tape->pc->error;
2208
                switch (rq->errors) {
2209
                        case IDETAPE_RQ_ERROR_FILEMARK:
2210
                        case IDETAPE_RQ_ERROR_EOD:
2211
                                break;
2212
                }
2213
                idetape_end_request (0,HWGROUP (drive));
2214
        }
2215
        return;
2216
}
2217
 
2218
void idetape_write_callback (ide_drive_t *drive)
2219
 
2220
{
2221
        idetape_tape_t *tape=&(drive->tape);
2222
        struct request *rq=HWGROUP(drive)->rq;
2223
        int blocks_written=tape->pc->actually_transferred/tape->tape_block_size;
2224
 
2225
#if IDETAPE_DEBUG_LOG   
2226
        printk ("ide-tape: Reached idetape_write_callback\n");
2227
#endif /* IDETAPE_DEBUG_LOG */
2228
 
2229
        tape->block_address+=blocks_written;
2230
        rq->current_nr_sectors-=blocks_written;
2231
 
2232
        if (!tape->pc->error)
2233
                idetape_end_request (1,HWGROUP (drive));
2234
        else {
2235
                rq->errors=tape->pc->error;
2236
                idetape_end_request (0,HWGROUP (drive));
2237
        }
2238
        return;
2239
}
2240
 
2241
void idetape_inquiry_callback (ide_drive_t *drive)
2242
 
2243
{
2244
        idetape_tape_t *tape;
2245
 
2246
        tape=&(drive->tape);
2247
 
2248
        idetape_display_inquiry_result (tape->pc->buffer);
2249
        idetape_pc_callback (drive);
2250
        return;
2251
}
2252
 
2253
/*
2254
 *      idetape_input_data is called to read data from the tape's data
2255
 *      register. We basically let ide_input_data do the job, but we also
2256
 *      take care about the remaining bytes which can not be transferred
2257
 *      in 32-bit data transfers.
2258
 */
2259
 
2260
void idetape_input_data (ide_drive_t *drive,void *buffer, unsigned long bcount)
2261
 
2262
{
2263
        unsigned long wcount;
2264
 
2265
        wcount=bcount >> 2;
2266
        bcount -= 4*wcount;
2267
 
2268
        if (wcount)
2269
                ide_input_data (drive,buffer,wcount);
2270
 
2271
        if (bcount) {
2272
                ((byte *)buffer) += 4*wcount;
2273
                insb (IDETAPE_DATA_REG,buffer,bcount);
2274
        }
2275
}
2276
 
2277
/*
2278
 *      idetape_output_data is used to write data to the tape.
2279
 */
2280
 
2281
void idetape_output_data (ide_drive_t *drive,void *buffer, unsigned long bcount)
2282
 
2283
{
2284
        unsigned long wcount;
2285
 
2286
        wcount=bcount >> 2;
2287
        bcount -= 4*wcount;
2288
 
2289
        if (wcount)
2290
                ide_output_data (drive,buffer,wcount);
2291
 
2292
        if (bcount) {
2293
                ((byte *)buffer) += 4*wcount;
2294
                outsb (IDETAPE_DATA_REG,buffer,bcount);
2295
        }
2296
}
2297
 
2298
/*
2299
 *      Too bad. The drive wants to send us data which we are not ready to accept.
2300
 *      Just throw it away.
2301
 */
2302
 
2303
void idetape_discard_data (ide_drive_t *drive, unsigned long bcount)
2304
 
2305
{
2306
        unsigned long i;
2307
 
2308
        for (i=0;i<bcount;i++)
2309
                IN_BYTE (IDETAPE_DATA_REG);
2310
}
2311
 
2312
/*
2313
 *      Issue an INQUIRY packet command.
2314
 */
2315
 
2316
void idetape_create_inquiry_cmd (idetape_packet_command_t *pc)
2317
 
2318
{
2319
#if IDETAPE_DEBUG_LOG
2320
        printk ("ide-tape: Creating INQUIRY packet command\n");
2321
#endif /* IDETAPE_DEBUG_LOG */  
2322
        pc->request_transfer=36;
2323
        pc->callback=&idetape_inquiry_callback;
2324
        pc->writing=0;
2325
 
2326
        idetape_zero_packet_command (pc);
2327
        pc->c[0]=IDETAPE_INQUIRY_CMD;
2328
        pc->c[4]=255;
2329
}
2330
 
2331
/*
2332
 *      Format the INQUIRY command results.
2333
 */
2334
 
2335
void idetape_display_inquiry_result (byte *buffer)
2336
 
2337
{
2338
        idetape_inquiry_result_t *result;
2339
 
2340
        result=(idetape_inquiry_result_t *) buffer;
2341
        ide_fixstring (result->vendor_id,8,0);
2342
        ide_fixstring (result->product_id,16,0);
2343
        ide_fixstring (result->revision_level,4,0);
2344
 
2345
        if (result->response_format != 2) {
2346
                printk ("The INQUIRY Data Format is unknown to us !\n");
2347
                printk ("Assuming QIC-157C format.\n");
2348
        }
2349
 
2350
#if IDETAPE_DEBUG_LOG
2351
        printk ("Dumping INQUIRY command results:\n");
2352
        printk ("Response Data Format: %d - ",result->response_format);
2353
        switch (result->response_format) {
2354
                case 2:
2355
                        printk ("As specified in QIC-157 Revision C\n");
2356
                        break;
2357
                default:
2358
                        printk ("Unknown\n");
2359
                        break;
2360
        }
2361
 
2362
        printk ("Device Type: %x - ",result->device_type);
2363
        switch (result->device_type) {
2364
                case 0: printk ("Direct-access Device\n");break;
2365
                case 1: printk ("Streaming Tape Device\n");break;
2366
                case 2: case 3: case 4: printk ("Reserved\n");break;
2367
                case 5: printk ("CD-ROM Device\n");break;
2368
                case 6: printk ("Reserved\n");
2369
                case 7: printk ("Optical memory Device\n");break;
2370
                case 0x1f: printk ("Unknown or no Device type\n");break;
2371
                default: printk ("Reserved\n");
2372
        }
2373
 
2374
        printk ("Removable Medium: %s",result->rmb ? "Yes\n":"No\n");
2375
 
2376
        printk ("ANSI Version: %d - ",result->ansi_version);
2377
        switch (result->ansi_version) {
2378
                case 2:
2379
                        printk ("QIC-157 Revision C\n");
2380
                        break;
2381
                default:
2382
                        printk ("Unknown\n");
2383
                        break;
2384
        }
2385
 
2386
        printk ("ECMA Version: ");
2387
        if (result->ecma_version)
2388
                printk ("%d\n",result->ecma_version);
2389
        else
2390
                printk ("Not supported\n");
2391
 
2392
        printk ("ISO Version: ");
2393
        if (result->iso_version)
2394
                printk ("%d\n",result->iso_version);
2395
        else
2396
                printk ("Not supported\n");
2397
 
2398
        printk ("Additional Length: %d\n",result->additional_length);
2399
        printk ("Vendor Identification: %s\n",result->vendor_id);
2400
        printk ("Product Identification: %s\n",result->product_id);
2401
        printk ("Product Revision Level: %s\n",result->revision_level);
2402
#endif /* IDETAPE_DEBUG_LOG */
2403
 
2404
        if (result->device_type != 1)
2405
                printk ("Device type is not set to tape\n");
2406
 
2407
        if (!result->rmb)
2408
                printk ("The removable flag is not set\n");
2409
 
2410
        if (result->ansi_version != 2) {
2411
                printk ("The Ansi Version is unknown to us !\n");
2412
                printk ("Assuming compliance with QIC-157C specification.\n");
2413
        }
2414
}
2415
 
2416
void idetape_create_request_sense_cmd (idetape_packet_command_t *pc)
2417
 
2418
{
2419
#if IDETAPE_DEBUG_LOG
2420
        printk ("ide-tape: Creating REQUEST SENSE packet command\n");
2421
#endif /* IDETAPE_DEBUG_LOG */  
2422
        pc->request_transfer=18;
2423
        pc->callback=&idetape_request_sense_callback;
2424
        pc->writing=0;
2425
 
2426
        idetape_zero_packet_command (pc);
2427
        pc->c[0]=IDETAPE_REQUEST_SENSE_CMD;
2428
        pc->c[4]=255;
2429
}
2430
 
2431
void idetape_request_sense_callback (ide_drive_t *drive)
2432
 
2433
{
2434
        idetape_tape_t *tape=&(drive->tape);
2435
 
2436
#if IDETAPE_DEBUG_LOG
2437
        printk ("ide-tape: Reached idetape_request_sense_callback\n");
2438
#endif /* IDETAPE_DEBUG_LOG */
2439
        if (!tape->pc->error) {
2440
#if IDETAPE_DEBUG_LOG
2441
                printk ("Request completed\n");
2442
#endif /* IDETAPE_DEBUG_LOG */
2443
                idetape_analyze_error (drive,(idetape_request_sense_result_t *) tape->pc->buffer);
2444
                idetape_end_request (1,HWGROUP (drive));
2445
        }
2446
        else {
2447
                printk ("Error in REQUEST SENSE itself - Aborting request!\n");
2448
                idetape_end_request (0,HWGROUP (drive));
2449
        }
2450
        return;
2451
}
2452
 
2453
/*
2454
 *      idetape_analyze_error is called on each failed packet command retry
2455
 *      to analyze the request sense. We currently do not utilize this
2456
 *      information.
2457
 */
2458
 
2459
void idetape_analyze_error (ide_drive_t *drive,idetape_request_sense_result_t *result)
2460
 
2461
{
2462
        idetape_tape_t *tape=&(drive->tape);
2463
        idetape_packet_command_t *pc=tape->failed_pc;
2464
 
2465
        tape->sense_key=result->sense_key;
2466
        tape->asc=result->asc;
2467
        tape->ascq=result->ascq;
2468
 
2469
#if IDETAPE_DEBUG_LOG   
2470
        /*
2471
         *      Without debugging, we only log an error if we decided to
2472
         *      give up retrying.
2473
         */
2474
        printk ("ide-tape: pc = %x, sense key = %x, asc = %x, ascq = %x\n",pc->c[0],result->sense_key,result->asc,result->ascq);
2475
#endif /* IDETAPE_DEBUG_LOG */
2476
 
2477
        if (pc->c[0] == IDETAPE_READ_CMD) {
2478
                if (result->filemark) {
2479
                        pc->error=IDETAPE_RQ_ERROR_FILEMARK;
2480
                        pc->abort=1;
2481
                }
2482
        }
2483
        if (pc->c[0] == IDETAPE_WRITE_CMD) {
2484
                if (result->eom || (result->sense_key == 0xd && result->asc == 0x0 && result->ascq == 0x2)) {
2485
                        pc->error = IDETAPE_RQ_ERROR_EOD;
2486
                        pc->abort = 1;
2487
                }
2488
        }
2489
        if (pc->c[0] == IDETAPE_READ_CMD || pc->c[0] == IDETAPE_WRITE_CMD) {
2490
                if (result->sense_key == 8) {
2491
                        pc->error=IDETAPE_RQ_ERROR_EOD;
2492
                        pc->abort=1;
2493
                }
2494
        }
2495
 
2496
#if 1
2497
#ifdef CONFIG_BLK_DEV_TRITON
2498
 
2499
        /*
2500
         *      Correct pc->actually_transferred by asking the tape.
2501
         */
2502
 
2503
        if (pc->dma_error && pc->abort) {
2504
                unsigned long *long_ptr=(unsigned long *) &(result->information1);
2505
                pc->actually_transferred=pc->request_transfer-tape->tape_block_size*idetape_swap_long (*long_ptr);
2506
        }
2507
#endif /* CONFIG_BLK_DEV_TRITON */
2508
#endif
2509
}
2510
 
2511
void idetape_create_test_unit_ready_cmd (idetape_packet_command_t *pc)
2512
 
2513
{
2514
#if IDETAPE_DEBUG_LOG
2515
        printk ("ide-tape: Creating TEST UNIT READY packet command\n");
2516
#endif /* IDETAPE_DEBUG_LOG */  
2517
        pc->request_transfer=0;
2518
        pc->buffer=NULL;
2519
        pc->current_position=NULL;
2520
        pc->callback=&idetape_pc_callback;
2521
        pc->writing=0;
2522
 
2523
        idetape_zero_packet_command (pc);
2524
        pc->c[0]=IDETAPE_TEST_UNIT_READY_CMD;
2525
}
2526
 
2527
void idetape_create_locate_cmd (idetape_packet_command_t *pc,unsigned long block,byte partition)
2528
 
2529
{
2530
        unsigned long *ptr;
2531
 
2532
#if IDETAPE_DEBUG_LOG
2533
        printk ("ide-tape: Creating LOCATE packet command\n");
2534
#endif /* IDETAPE_DEBUG_LOG */
2535
        pc->request_transfer=0;
2536
        pc->buffer=NULL;
2537
        pc->current_position=NULL;
2538
        pc->buffer_size=0;
2539
        pc->wait_for_dsc=1;
2540
        pc->callback=&idetape_pc_callback;
2541
        pc->writing=0;
2542
 
2543
        idetape_zero_packet_command (pc);
2544
        pc->c [0]=IDETAPE_LOCATE_CMD;
2545
        pc->c [1]=2;
2546
        ptr=(unsigned long *) &(pc->c[3]);
2547
        *ptr=idetape_swap_long (block);
2548
        pc->c[8]=partition;
2549
}
2550
 
2551
void idetape_create_rewind_cmd (idetape_packet_command_t *pc)
2552
 
2553
{
2554
#if IDETAPE_DEBUG_LOG
2555
        printk ("ide-tape: Creating REWIND packet command\n");
2556
#endif /* IDETAPE_DEBUG_LOG */
2557
        pc->request_transfer=0;
2558
        pc->buffer=NULL;
2559
        pc->current_position=NULL;
2560
        pc->buffer_size=0;
2561
        pc->wait_for_dsc=1;
2562
        pc->callback=&idetape_pc_callback;
2563
        pc->writing=0;
2564
 
2565
        idetape_zero_packet_command (pc);
2566
        pc->c [0]=IDETAPE_REWIND_CMD;
2567
}
2568
 
2569
/*
2570
 *      A mode sense command is used to "sense" tape parameters.
2571
 */
2572
 
2573
void idetape_create_mode_sense_cmd (idetape_packet_command_t *pc,byte page_code)
2574
 
2575
{
2576
#if IDETAPE_DEBUG_LOG
2577
        printk ("ide-tape: Creating MODE SENSE packet command - Page %d\n",page_code);
2578
#endif /* IDETAPE_DEBUG_LOG */
2579
 
2580
        pc->wait_for_dsc=0;
2581
        pc->callback=&idetape_pc_callback;
2582
        pc->writing=0;
2583
 
2584
        switch (page_code) {
2585
                case IDETAPE_CAPABILITIES_PAGE:
2586
                        pc->request_transfer=24;
2587
        }
2588
 
2589
        idetape_zero_packet_command (pc);
2590
        pc->c [0]=IDETAPE_MODE_SENSE_CMD;
2591
        pc->c [1]=8;                            /* DBD = 1 - Don't return block descriptors for now */
2592
        pc->c [2]=page_code;
2593
        pc->c [3]=255;                          /* Don't limit the returned information */
2594
        pc->c [4]=255;                          /* (We will just discard data in that case) */
2595
}
2596
 
2597
/*
2598
 *      idetape_create_write_filemark_cmd will:
2599
 *
2600
 *              1.      Write a filemark if write_filemark=1.
2601
 *              2.      Flush the device buffers without writing a filemark
2602
 *                      if write_filemark=0.
2603
 *
2604
 */
2605
 
2606
void idetape_create_write_filemark_cmd (idetape_packet_command_t *pc,int write_filemark)
2607
 
2608
{
2609
#if IDETAPE_DEBUG_LOG
2610
        printk ("Creating WRITE FILEMARK packet command\n");
2611
        if (!write_filemark)
2612
                printk ("which will only flush buffered data\n");
2613
#endif /* IDETAPE_DEBUG_LOG */
2614
        pc->request_transfer=0;
2615
        pc->buffer=NULL;
2616
        pc->current_position=NULL;
2617
        pc->buffer_size=0;
2618
        pc->wait_for_dsc=1;
2619
        pc->callback=&idetape_pc_callback;
2620
        pc->writing=0;
2621
 
2622
        idetape_zero_packet_command (pc);
2623
        pc->c [0]=IDETAPE_WRITE_FILEMARK_CMD;
2624
        if (write_filemark)
2625
                pc->c [4]=1;
2626
}
2627
 
2628
void idetape_create_load_unload_cmd (idetape_packet_command_t *pc,int cmd)
2629
 
2630
{
2631
#if IDETAPE_DEBUG_LOG
2632
        printk ("Creating LOAD UNLOAD packet command, cmd=%d\n",cmd);
2633
#endif /* IDETAPE_DEBUG_LOG */
2634
        pc->request_transfer=0;
2635
        pc->buffer=NULL;
2636
        pc->current_position=NULL;
2637
        pc->buffer_size=0;
2638
        pc->wait_for_dsc=1;
2639
        pc->callback=&idetape_pc_callback;
2640
        pc->writing=0;
2641
 
2642
        idetape_zero_packet_command (pc);
2643
        pc->c [0]=IDETAPE_LOAD_UNLOAD_CMD;
2644
        pc->c [4]=cmd;
2645
}
2646
 
2647
void idetape_create_erase_cmd (idetape_packet_command_t *pc)
2648
 
2649
{
2650
 
2651
#if IDETAPE_DEBUG_LOG
2652
        printk ("Creating ERASE command\n");
2653
#endif /* IDETAPE_DEBUG_LOG */
2654
 
2655
        pc->request_transfer=0;
2656
        pc->buffer=NULL;
2657
        pc->current_position=NULL;
2658
        pc->buffer_size=0;
2659
        pc->wait_for_dsc=1;
2660
        pc->callback=&idetape_pc_callback;
2661
        pc->writing=0;
2662
 
2663
        idetape_zero_packet_command (pc);
2664
        pc->c [0]=IDETAPE_ERASE_CMD;
2665
        pc->c [1]=1;
2666
}
2667
 
2668
void idetape_create_read_cmd (idetape_packet_command_t *pc,unsigned long length)
2669
 
2670
{
2671
        union convert {
2672
                unsigned all    :32;
2673
                struct {
2674
                        unsigned b1     :8;
2675
                        unsigned b2     :8;
2676
                        unsigned b3     :8;
2677
                        unsigned b4     :8;
2678
                } b;
2679
        } original;
2680
 
2681
#if IDETAPE_DEBUG_LOG
2682
        printk ("ide-tape: Creating READ packet command\n");
2683
#endif /* IDETAPE_DEBUG_LOG */
2684
 
2685
        original.all=length;
2686
 
2687
        pc->wait_for_dsc=0;
2688
        pc->callback=&idetape_read_callback;
2689
        pc->writing=0;
2690
 
2691
        idetape_zero_packet_command (pc);
2692
 
2693
        pc->c [0]=IDETAPE_READ_CMD;
2694
        pc->c [1]=1;
2695
        pc->c [4]=original.b.b1;
2696
        pc->c [3]=original.b.b2;
2697
        pc->c [2]=original.b.b3;
2698
 
2699
        if (length)
2700
                pc->dma_recommended=1;
2701
 
2702
        return;
2703
}
2704
 
2705
void idetape_create_space_cmd (idetape_packet_command_t *pc,long count,byte cmd)
2706
 
2707
{
2708
        union convert {
2709
                unsigned all    :32;
2710
                struct {
2711
                        unsigned b1     :8;
2712
                        unsigned b2     :8;
2713
                        unsigned b3     :8;
2714
                        unsigned b4     :8;
2715
                } b;
2716
        } original;
2717
 
2718
#if IDETAPE_DEBUG_LOG
2719
        printk ("ide-tape: Creating SPACE packet command\n");
2720
#endif /* IDETAPE_DEBUG_LOG */
2721
 
2722
        original.all=count;
2723
 
2724
        pc->request_transfer=0;
2725
        pc->buffer=NULL;
2726
        pc->current_position=NULL;
2727
        pc->buffer_size=0;
2728
        pc->wait_for_dsc=1;
2729
        pc->callback=&idetape_pc_callback;
2730
        pc->writing=0;
2731
 
2732
        idetape_zero_packet_command (pc);
2733
        pc->c [0]=IDETAPE_SPACE_CMD;
2734
        pc->c [1]=cmd;
2735
        pc->c [4]=original.b.b1;
2736
        pc->c [3]=original.b.b2;
2737
        pc->c [2]=original.b.b3;
2738
 
2739
        return;
2740
}
2741
 
2742
void idetape_create_write_cmd (idetape_packet_command_t *pc,unsigned long length)
2743
 
2744
{
2745
        union convert {
2746
                unsigned all    :32;
2747
                struct {
2748
                        unsigned b1     :8;
2749
                        unsigned b2     :8;
2750
                        unsigned b3     :8;
2751
                        unsigned b4     :8;
2752
                } b;
2753
        } original;
2754
 
2755
#if IDETAPE_DEBUG_LOG
2756
        printk ("ide-tape: Creating WRITE packet command\n");
2757
#endif /* IDETAPE_DEBUG_LOG */
2758
 
2759
        original.all=length;
2760
 
2761
        pc->wait_for_dsc=0;
2762
        pc->callback=&idetape_write_callback;
2763
        pc->writing=1;
2764
 
2765
        idetape_zero_packet_command (pc);
2766
 
2767
        pc->c [0]=IDETAPE_WRITE_CMD;
2768
        pc->c [1]=1;
2769
        pc->c [4]=original.b.b1;
2770
        pc->c [3]=original.b.b2;
2771
        pc->c [2]=original.b.b3;
2772
 
2773
        if (length)
2774
                pc->dma_recommended=1;
2775
 
2776
        return;
2777
}
2778
 
2779
void idetape_create_read_position_cmd (idetape_packet_command_t *pc)
2780
 
2781
{
2782
#if IDETAPE_DEBUG_LOG
2783
        printk ("ide-tape: Creating READ POSITION packet command\n");
2784
#endif /* IDETAPE_DEBUG_LOG */
2785
 
2786
        pc->request_transfer=20;
2787
        pc->wait_for_dsc=0;
2788
        pc->callback=&idetape_read_position_callback;
2789
        pc->writing=0;
2790
 
2791
        idetape_zero_packet_command (pc);
2792
        pc->c [0]=IDETAPE_READ_POSITION_CMD;
2793
        pc->c [1]=0;
2794
}
2795
 
2796
void idetape_read_position_callback (ide_drive_t *drive)
2797
 
2798
{
2799
        idetape_tape_t *tape;
2800
        struct request *rq;
2801
        idetape_read_position_result_t *result;
2802
 
2803
        tape=&(drive->tape);
2804
 
2805
#if IDETAPE_DEBUG_LOG
2806
        printk ("ide-tape: Reached idetape_read_position_callback\n");
2807
#endif /* IDETAPE_DEBUG_LOG */
2808
 
2809
        rq=HWGROUP(drive)->rq;
2810
 
2811
        if (!tape->pc->error) {
2812
                result=(idetape_read_position_result_t *) tape->pc->buffer;
2813
#if IDETAPE_DEBUG_LOG
2814
                printk ("Request completed\n");
2815
                printk ("Dumping the results of the READ POSITION command\n");
2816
                printk ("BOP - %s\n",result->bop ? "Yes":"No");
2817
                printk ("EOP - %s\n",result->eop ? "Yes":"No");
2818
#endif /* IDETAPE_DEBUG_LOG */
2819
                if (result->bpu) {
2820
                        printk ("ide-tape: Block location is unknown to the tape\n");
2821
                        printk ("Aborting request\n");
2822
                        tape->block_address_valid=0;
2823
                        idetape_end_request (0,HWGROUP (drive));
2824
                }
2825
                else {
2826
#if IDETAPE_DEBUG_LOG
2827
                        printk ("Block Location - %lu\n",idetape_swap_long (result->first_block));
2828
#endif /* IDETAPE_DEBUG_LOG */
2829
                        tape->block_address=idetape_swap_long (result->first_block);
2830
                        tape->block_address_valid=1;
2831
                        idetape_end_request (1,HWGROUP (drive));
2832
                }
2833
        }
2834
        else {
2835
                printk ("Aborting request\n");
2836
                idetape_end_request (0,HWGROUP (drive));
2837
        }
2838
        return;
2839
}
2840
 
2841
/*
2842
 *      Our special ide-tape ioctl's.
2843
 *
2844
 *      Currently there aren't any significant ioctl's.
2845
 *      mtio.h compatible commands should be issued to the character device
2846
 *      interface.
2847
 */
2848
 
2849
int idetape_blkdev_ioctl (ide_drive_t *drive, struct inode *inode, struct file *file,
2850
                        unsigned int cmd, unsigned long arg)
2851
{
2852
        idetape_packet_command_t pc;
2853
 
2854
        pc.buffer=pc.temp_buffer;
2855
        pc.buffer_size=IDETAPE_TEMP_BUFFER_SIZE;
2856
        pc.current_position=pc.temp_buffer;
2857
 
2858
#if IDETAPE_DEBUG_LOG   
2859
        printk ("ide-tape: Reached idetape_blkdev_ioctl\n");
2860
#endif /* IDETAPE_DEBUG_LOG */
2861
        switch (cmd) {
2862
                default:
2863
                        return -EIO;
2864
        }
2865
}
2866
 
2867
static void idetape_abort_pipeline (ide_drive_t *drive)
2868
{
2869
        idetape_tape_t *tape = &drive->tape;
2870
        idetape_pipeline_stage_t *stage = tape->next_stage;
2871
 
2872
        while (stage) {
2873
                stage->rq.cmd = IDETAPE_ABORTED_WRITE_REQUEST;
2874
                stage = stage->next;
2875
        }
2876
}
2877
 
2878
/*
2879
 *      Functions which handle requests.
2880
 */
2881
 
2882
/*
2883
 *      idetape_end_request is used to end a request.
2884
 */
2885
 
2886
void idetape_end_request (byte uptodate, ide_hwgroup_t *hwgroup)
2887
 
2888
{
2889
        ide_drive_t *drive = hwgroup->drive;
2890
        struct request *rq = hwgroup->rq;
2891
        idetape_tape_t *tape = &(drive->tape);
2892
        unsigned int major = HWIF(drive)->major;
2893
        struct blk_dev_struct *bdev = &blk_dev[major];
2894
        int error;
2895
 
2896
#if IDETAPE_DEBUG_LOG
2897
        printk ("Reached idetape_end_request\n");
2898
#endif /* IDETAPE_DEBUG_LOG */
2899
 
2900
        bdev->current_request=rq;                       /* Since we may have taken it out */
2901
 
2902
        if (!rq->errors)                                /* In case rq->errors is already set, */
2903
                rq->errors=!uptodate;                   /* we won't change it. */
2904
        error=rq->errors;
2905
        if (error)
2906
                tape->failed_pc = NULL;
2907
 
2908
        if (tape->active_data_request == rq) {          /* The request was a pipelined data transfer request */
2909
 
2910
                if (rq->cmd == IDETAPE_READ_REQUEST) {
2911
#if IDETAPE_DEBUG_BUGS
2912
                        if (tape->active_stage == NULL)
2913
                                printk ("ide-tape: bug: active_stage is NULL in idetape_end_request\n");
2914
                        else
2915
#endif /* IDETAPE_DEBUG_BUGS */
2916
                        idetape_copy_buffer_to_stage (tape->active_stage,tape->data_buffer);
2917
                }
2918
 
2919
                tape->active_stage=NULL;
2920
                tape->active_data_request=NULL;
2921
 
2922
                if (rq->cmd == IDETAPE_WRITE_REQUEST) {
2923
                        if (rq->errors) {
2924
                                tape->error_in_pipeline_stage=rq->errors;
2925
                                if (error == IDETAPE_RQ_ERROR_EOD)
2926
                                        idetape_abort_pipeline (drive);
2927
                        }
2928
                        idetape_remove_stage_head (drive);
2929
                }
2930
 
2931
                if (tape->next_stage == NULL) {
2932
                        if (!error)
2933
                                idetape_increase_max_pipeline_stages (drive);
2934
                        ide_end_drive_cmd (drive, 0, 0);
2935
                        return;
2936
                }
2937
 
2938
                idetape_active_next_stage (drive);
2939
 
2940
                /*
2941
                 *      Insert the next request into the request queue.
2942
                 *
2943
                 *      The choice of using ide_next or ide_end is now left
2944
                 *      to the user.
2945
                 */
2946
 
2947
#if IDETAPE_LOW_TAPE_PRIORITY
2948
                (void) ide_do_drive_cmd (drive,tape->active_data_request,ide_end);
2949
#else
2950
                (void) ide_do_drive_cmd (drive,tape->active_data_request,ide_next);
2951
#endif /* IDETAPE_LOW_TAPE_PRIORITY */
2952
        }
2953
        ide_end_drive_cmd (drive, 0, 0);
2954
}
2955
 
2956
/*
2957
 *      idetape_do_request is our request handling function.
2958
 */
2959
 
2960
void idetape_do_request (ide_drive_t *drive, struct request *rq, unsigned long block)
2961
 
2962
{
2963
        idetape_tape_t *tape=&(drive->tape);
2964
        idetape_packet_command_t *pc;
2965
        unsigned int major = HWIF(drive)->major;
2966
        struct blk_dev_struct *bdev = &blk_dev[major];
2967
        idetape_status_reg_t status;
2968
 
2969
#if IDETAPE_DEBUG_LOG
2970
        printk ("Current request:\n");
2971
        printk ("rq_status: %d, rq_dev: %u, cmd: %d, errors: %d\n",rq->rq_status,(unsigned int) rq->rq_dev,rq->cmd,rq->errors);
2972
        printk ("sector: %ld, nr_sectors: %ld, current_nr_sectors: %ld\n",rq->sector,rq->nr_sectors,rq->current_nr_sectors);
2973
#endif /* IDETAPE_DEBUG_LOG */
2974
 
2975
        if (!IDETAPE_REQUEST_CMD (rq->cmd)) {
2976
 
2977
                /*
2978
                 *      We do not support buffer cache originated requests.
2979
                 */
2980
 
2981
                printk ("ide-tape: Unsupported command in request queue\n");
2982
                printk ("ide-tape: The block device interface should not be used for data transfers.\n");
2983
                printk ("ide-tape: Use the character device interfaces\n");
2984
                printk ("ide-tape: /dev/ht0 and /dev/nht0 instead.\n");
2985
                printk ("ide-tape: (Run linux/scripts/MAKEDEV.ide to create them)\n");
2986
                printk ("ide-tape: Aborting request.\n");
2987
 
2988
                ide_end_request (0,HWGROUP (drive));                     /* Let the common code handle it */
2989
                return;
2990
        }
2991
 
2992
        /*
2993
         *      This is an important point. We will try to remove our request
2994
         *      from the block device request queue while we service the
2995
         *      request. Note that the request must be returned to
2996
         *      bdev->current_request before the next call to
2997
         *      ide_end_drive_cmd or ide_do_drive_cmd to conform with the
2998
         *      normal behavior of the IDE driver, which leaves the active
2999
         *      request in bdev->current_request during I/O.
3000
         *
3001
         *      This will eliminate fragmentation of disk/cdrom requests
3002
         *      around a tape request, now that we are using ide_next to
3003
         *      insert pending pipeline requests, since we have only one
3004
         *      ide-tape.c data request in the device request queue, and
3005
         *      thus once removed, ll_rw_blk.c will only see requests from
3006
         *      the other device.
3007
         *
3008
         *      The potential fragmentation inefficiency was pointed to me
3009
         *      by Mark Lord.
3010
         */
3011
 
3012
        if (rq->next != NULL && rq->rq_dev != rq->next->rq_dev)
3013
                bdev->current_request=rq->next;
3014
 
3015
        /* Retry a failed packet command */
3016
 
3017
        if (tape->failed_pc != NULL && tape->pc->c[0] == IDETAPE_REQUEST_SENSE_CMD) {
3018
                idetape_issue_packet_command (drive,tape->failed_pc,&idetape_pc_intr);
3019
                return;
3020
        }
3021
 
3022
        /* Check if we have a postponed request */
3023
 
3024
        if (tape->postponed_rq != NULL) {
3025
#if IDETAPE_DEBUG_BUGS
3026
                if (tape->postponed_rq->rq_status != RQ_ACTIVE || rq != tape->postponed_rq) {
3027
                        printk ("ide-tape: ide-tape.c bug - Two DSC requests were queued\n");
3028
                        idetape_end_request (0,HWGROUP (drive));
3029
                        return;
3030
                }
3031
#endif /* IDETAPE_DEBUG_BUGS */
3032
                if (rq->cmd == IDETAPE_PACKET_COMMAND_REQUEST_TYPE1) {
3033
 
3034
                        /* Media access command */
3035
 
3036
                        tape->postponed_rq = NULL;
3037
                        idetape_media_access_finished (drive);
3038
                        return;
3039
                }
3040
 
3041
                /*
3042
                 * Read / Write command - DSC polling was done before the
3043
                 * actual command - Continue normally so that the command
3044
                 * will be performed below.
3045
                 */
3046
 
3047
                 tape->postponed_rq = NULL;
3048
        }
3049
 
3050
        status.all=IN_BYTE (IDETAPE_STATUS_REG);
3051
 
3052
        /*
3053
         *      After a software reset, the status register is locked. We
3054
         *      will ignore the DSC value for our very first packet command,
3055
         *      which will restore DSC operation.
3056
         */
3057
 
3058
        if (tape->reset_issued) {
3059
                status.b.dsc=1;
3060
                tape->reset_issued=0;
3061
        }
3062
 
3063
        switch (rq->cmd) {
3064
                case IDETAPE_READ_REQUEST:
3065
                        if (!status.b.dsc) {                            /* Tape buffer not ready to accept r/w command */
3066
#if IDETAPE_DEBUG_LOG
3067
                                printk ("ide-tape: DSC != 1 - Postponing read request\n");
3068
#endif /* IDETAPE_DEBUG_LOG */  
3069
                                tape->dsc_polling_frequency=tape->best_dsc_rw_frequency;
3070
                                idetape_postpone_request (drive);       /* Allow ide.c to process requests from */
3071
                                return;
3072
                        }
3073
 
3074
                        pc=idetape_next_pc_storage (drive);
3075
 
3076
                        idetape_create_read_cmd (pc,rq->current_nr_sectors);
3077
 
3078
                        pc->buffer=rq->buffer;
3079
                        pc->buffer_size=rq->current_nr_sectors*tape->tape_block_size;
3080
                        pc->current_position=rq->buffer;
3081
                        pc->request_transfer=rq->current_nr_sectors*tape->tape_block_size;
3082
 
3083
                        idetape_issue_packet_command (drive,pc,&idetape_pc_intr);
3084
                        return;
3085
 
3086
                case IDETAPE_WRITE_REQUEST:
3087
                        if (!status.b.dsc) {                            /* Tape buffer not ready to accept r/w command */
3088
#if IDETAPE_DEBUG_LOG
3089
                                printk ("ide-tape: DSC != 1 - Postponing write request\n");
3090
#endif /* IDETAPE_DEBUG_LOG */  
3091
                                tape->dsc_polling_frequency=tape->best_dsc_rw_frequency;
3092
                                idetape_postpone_request (drive);       /* Allow ide.c to process requests from */
3093
                                return;
3094
                        }
3095
 
3096
                        pc=idetape_next_pc_storage (drive);
3097
 
3098
                        idetape_create_write_cmd (pc,rq->current_nr_sectors);
3099
 
3100
                        pc->buffer=rq->buffer;
3101
                        pc->buffer_size=rq->current_nr_sectors*tape->tape_block_size;
3102
                        pc->current_position=rq->buffer;
3103
                        pc->request_transfer=rq->current_nr_sectors*tape->tape_block_size;
3104
 
3105
                        idetape_issue_packet_command (drive,pc,&idetape_pc_intr);
3106
                        return;
3107
 
3108
                case IDETAPE_ABORTED_WRITE_REQUEST:
3109
                        rq->cmd = IDETAPE_WRITE_REQUEST;
3110
                        rq->errors = IDETAPE_RQ_ERROR_EOD;
3111
                        idetape_end_request (1, HWGROUP(drive));
3112
                        return;
3113
 
3114
                case IDETAPE_PACKET_COMMAND_REQUEST_TYPE1:
3115
                case IDETAPE_PACKET_COMMAND_REQUEST_TYPE2:
3116
/*
3117
 *      This should be unnecessary (postponing of a general packet command),
3118
 *      but I have occasionally missed DSC on a media access command otherwise.
3119
 *      ??? Still have to figure it out ...
3120
 */
3121
                        if (!status.b.dsc) {                            /* Tape buffers are still not ready */
3122
#if IDETAPE_DEBUG_LOG
3123
                                printk ("ide-tape: DSC != 1 - Postponing packet command request\n");
3124
#endif /* IDETAPE_DEBUG_LOG */
3125
                                rq->cmd=IDETAPE_PACKET_COMMAND_REQUEST_TYPE2;   /* Note that we are waiting for DSC *before* we */
3126
                                                                                /* even issued the command */
3127
                                tape->dsc_polling_frequency=IDETAPE_DSC_READ_WRITE_FALLBACK_FREQUENCY;
3128
                                idetape_postpone_request (drive);       /* Allow ide.c to process requests from */
3129
                                return;
3130
                        }
3131
                        rq->cmd=IDETAPE_PACKET_COMMAND_REQUEST_TYPE1;
3132
                        pc=(idetape_packet_command_t *) rq->buffer;
3133
                        idetape_issue_packet_command (drive,pc,&idetape_pc_intr);
3134
                        return;
3135
#if IDETAPE_DEBUG_BUGS
3136
                default:
3137
                        printk ("ide-tape: bug in IDETAPE_REQUEST_CMD macro\n");
3138
                        idetape_end_request (0,HWGROUP (drive));
3139
#endif /* IDETAPE_DEBUG_BUGS */
3140
        }
3141
}
3142
 
3143
/*
3144
 *      idetape_queue_pc_tail is based on the following functions:
3145
 *
3146
 *      ide_do_drive_cmd from ide.c
3147
 *      cdrom_queue_request and cdrom_queue_packet_command from ide-cd.c
3148
 *
3149
 *      We add a special packet command request to the tail of the request queue,
3150
 *      and wait for it to be serviced.
3151
 *
3152
 *      This is not to be called from within the request handling part
3153
 *      of the driver ! We allocate here data in the stack, and it is valid
3154
 *      until the request is finished. This is not the case for the bottom
3155
 *      part of the driver, where we are always leaving the functions to wait
3156
 *      for an interrupt or a timer event.
3157
 *
3158
 *      From the bottom part of the driver, we should allocate safe memory
3159
 *      using idetape_next_pc_storage and idetape_next_rq_storage, and add
3160
 *      the request to the request list without waiting for it to be serviced !
3161
 *      In that case, we usually use idetape_queue_pc_head.
3162
 */
3163
 
3164
int idetape_queue_pc_tail (ide_drive_t *drive,idetape_packet_command_t *pc)
3165
{
3166
        struct request rq;
3167
 
3168
        ide_init_drive_cmd (&rq);
3169
        rq.buffer = (char *) pc;
3170
        rq.cmd = IDETAPE_PACKET_COMMAND_REQUEST_TYPE1;
3171
        return ide_do_drive_cmd (drive, &rq, ide_wait);
3172
}
3173
 
3174
/*
3175
 *      idetape_queue_pc_head generates a new packet command request in front
3176
 *      of the request queue, before the current request, so that it will be
3177
 *      processed immediately, on the next pass through the driver.
3178
 *
3179
 *      idetape_queue_pc_head is called from the request handling part of
3180
 *      the driver (the "bottom" part). Safe storage for the request should
3181
 *      be allocated with idetape_next_pc_storage and idetape_next_rq_storage
3182
 *      before calling idetape_queue_pc_head.
3183
 *
3184
 *      Memory for those requests is pre-allocated at initialization time, and
3185
 *      is limited to IDETAPE_PC_STACK requests. We assume that we have enough
3186
 *      space for the maximum possible number of inter-dependent packet commands.
3187
 *
3188
 *      The higher level of the driver - The ioctl handler and the character
3189
 *      device handling functions should queue request to the lower level part
3190
 *      and wait for their completion using idetape_queue_pc_tail or
3191
 *      idetape_queue_rw_tail.
3192
 */
3193
 
3194
void idetape_queue_pc_head (ide_drive_t *drive,idetape_packet_command_t *pc,struct request *rq)
3195
 
3196
{
3197
        unsigned int major = HWIF(drive)->major;
3198
        struct blk_dev_struct *bdev = &blk_dev[major];
3199
 
3200
        bdev->current_request=HWGROUP (drive)->rq;              /* Since we may have taken it out */
3201
 
3202
        ide_init_drive_cmd (rq);
3203
        rq->buffer = (char *) pc;
3204
        rq->cmd = IDETAPE_PACKET_COMMAND_REQUEST_TYPE1;
3205
        (void) ide_do_drive_cmd (drive, rq, ide_preempt);
3206
}
3207
 
3208
/*
3209
 *      idetape_wait_for_request installs a semaphore in a pending request
3210
 *      and sleeps until it is serviced.
3211
 *
3212
 *      The caller should ensure that the request will not be serviced
3213
 *      before we install the semaphore (usually by disabling interrupts).
3214
 */
3215
 
3216
void idetape_wait_for_request (struct request *rq)
3217
 
3218
{
3219
        struct semaphore sem = MUTEX_LOCKED;
3220
 
3221
#if IDETAPE_DEBUG_BUGS
3222
        if (rq == NULL || !IDETAPE_REQUEST_CMD (rq->cmd)) {
3223
                printk ("ide-tape: bug: Trying to sleep on non-valid request\n");
3224
                return;
3225
        }
3226
#endif /* IDETAPE_DEBUG_BUGS */
3227
 
3228
        rq->sem=&sem;
3229
        down (&sem);
3230
}
3231
 
3232
/*
3233
 *      idetape_queue_rw_tail generates a read/write request for the block
3234
 *      device interface and wait for it to be serviced.
3235
 */
3236
 
3237
int idetape_queue_rw_tail (ide_drive_t *drive,int cmd,int blocks,char *buffer)
3238
 
3239
{
3240
        idetape_tape_t *tape = &(drive->tape);
3241
        struct request rq;
3242
 
3243
#if IDETAPE_DEBUG_LOG
3244
        printk ("idetape_queue_rw_tail: cmd=%d\n",cmd);
3245
#endif /* IDETAPE_DEBUG_LOG */
3246
#if IDETAPE_DEBUG_BUGS
3247
        if (tape->active_data_request != NULL) {
3248
                printk ("ide-tape: bug: the pipeline is active in idetape_queue_rw_tail\n");
3249
                return (0);
3250
        }
3251
#endif /* IDETAPE_DEBUG_BUGS */ 
3252
 
3253
        ide_init_drive_cmd (&rq);
3254
        rq.buffer = buffer;
3255
        rq.cmd = cmd;
3256
        rq.sector = tape->block_address;
3257
        rq.nr_sectors = rq.current_nr_sectors = blocks;
3258
        (void) ide_do_drive_cmd (drive, &rq, ide_wait);
3259
 
3260
        return (tape->tape_block_size*(blocks-rq.current_nr_sectors));
3261
}
3262
 
3263
/*
3264
 *      idetape_add_chrdev_read_request handles character device read requests
3265
 *      when operating in the pipelined operation mode.
3266
 */
3267
 
3268
int idetape_add_chrdev_read_request (ide_drive_t *drive,int blocks,char *buffer)
3269
 
3270
{
3271
        idetape_tape_t *tape = &(drive->tape);
3272
        idetape_pipeline_stage_t *new_stage;
3273
        unsigned long flags;
3274
        struct request rq,*rq_ptr;
3275
        int bytes_read;
3276
 
3277
#if IDETAPE_DEBUG_LOG
3278
        printk ("Reached idetape_add_chrdev_read_request\n");
3279
#endif /* IDETAPE_DEBUG_LOG */
3280
 
3281
        ide_init_drive_cmd (&rq);
3282
        rq.cmd = IDETAPE_READ_REQUEST;
3283
        rq.sector = tape->block_address;
3284
        rq.nr_sectors = rq.current_nr_sectors = blocks;
3285
 
3286
        if (tape->active_data_request != NULL || tape->current_number_of_stages <= tape->max_number_of_stages / 4) {
3287
                new_stage=idetape_kmalloc_stage (drive);
3288
                while (new_stage != NULL) {
3289
                        new_stage->rq=rq;
3290
                        save_flags (flags);cli ();
3291
                        idetape_add_stage_tail (drive,new_stage);
3292
                        restore_flags (flags);
3293
                        new_stage=idetape_kmalloc_stage (drive);
3294
                }
3295
                if (tape->active_data_request == NULL)
3296
                        idetape_insert_pipeline_into_queue (drive);
3297
        }
3298
 
3299
        if (tape->first_stage == NULL) {
3300
 
3301
                /*
3302
                 *      Linux is short on memory. Revert to non-pipelined
3303
                 *      operation mode for this request.
3304
                 */
3305
 
3306
                return (idetape_queue_rw_tail (drive,IDETAPE_READ_REQUEST,blocks,buffer));
3307
        }
3308
 
3309
        save_flags (flags);cli ();
3310
        if (tape->active_data_request == &(tape->first_stage->rq))
3311
                idetape_wait_for_request (tape->active_data_request);
3312
        restore_flags (flags);
3313
 
3314
        rq_ptr=&(tape->first_stage->rq);
3315
        bytes_read=tape->tape_block_size*(rq_ptr->nr_sectors-rq_ptr->current_nr_sectors);
3316
        rq_ptr->nr_sectors=rq_ptr->current_nr_sectors=0;
3317
        idetape_copy_buffer_from_stage (tape->first_stage,buffer);
3318
        if (rq_ptr->errors != IDETAPE_RQ_ERROR_FILEMARK) {
3319
                tape->filemark = 0;
3320
                idetape_remove_stage_head (drive);
3321
        } else
3322
                tape->filemark = 1;
3323
#if IDETAPE_DEBUG_BUGS
3324
        if (bytes_read > blocks*tape->tape_block_size) {
3325
                printk ("ide-tape: bug: trying to return more bytes than requested\n");
3326
                bytes_read=blocks*tape->tape_block_size;
3327
        }
3328
#endif /* IDETAPE_DEBUG_BUGS */
3329
        return (bytes_read);
3330
}
3331
 
3332
/*
3333
 *      idetape_add_chrdev_write_request tries to add a character device
3334
 *      originated write request to our pipeline. In case we don't succeed,
3335
 *      we revert to non-pipelined operation mode for this request.
3336
 *
3337
 *      1.      Try to allocate a new pipeline stage.
3338
 *      2.      If we can't, wait for more and more requests to be serviced
3339
 *              and try again each time.
3340
 *      3.      If we still can't allocate a stage, fallback to
3341
 *              non-pipelined operation mode for this request.
3342
 */
3343
 
3344
int idetape_add_chrdev_write_request (ide_drive_t *drive,int blocks,char *buffer)
3345
 
3346
{
3347
        idetape_tape_t *tape = &(drive->tape);
3348
        idetape_pipeline_stage_t *new_stage;
3349
        unsigned long flags;
3350
        struct request *rq;
3351
 
3352
#if IDETAPE_DEBUG_LOG
3353
        printk ("Reached idetape_add_chrdev_write_request\n");
3354
#endif /* IDETAPE_DEBUG_LOG */
3355
 
3356
 
3357
        new_stage=idetape_kmalloc_stage (drive);
3358
 
3359
        /*
3360
         *      If we don't have a new stage, wait for more and more requests
3361
         *      to finish, and try to allocate after each one.
3362
         *
3363
         *      Pay special attention to possible race conditions.
3364
         */
3365
 
3366
        while (new_stage == NULL) {
3367
                save_flags (flags);cli ();
3368
                if (tape->active_data_request != NULL) {
3369
                        idetape_wait_for_request (tape->active_data_request);
3370
                        restore_flags (flags);
3371
                        new_stage=idetape_kmalloc_stage (drive);
3372
                }
3373
                else {
3374
                        /*
3375
                         *      Linux is short on memory. Fallback to
3376
                         *      non-pipelined operation mode for this request.
3377
                         */
3378
 
3379
                        restore_flags (flags);
3380
                        return (idetape_queue_rw_tail (drive,IDETAPE_WRITE_REQUEST,blocks,buffer));
3381
                }
3382
        }
3383
 
3384
        rq=&(new_stage->rq);
3385
 
3386
        ide_init_drive_cmd (rq);
3387
        rq->cmd = IDETAPE_WRITE_REQUEST;
3388
        rq->sector = tape->block_address;       /* Doesn't actually matter - We always assume sequential access */
3389
        rq->nr_sectors = blocks;
3390
        rq->current_nr_sectors = blocks;
3391
 
3392
        idetape_copy_buffer_to_stage (new_stage,buffer);
3393
 
3394
        save_flags (flags);cli ();
3395
        idetape_add_stage_tail (drive,new_stage);
3396
        restore_flags (flags);
3397
 
3398
        /*
3399
         *      Check if we are currently servicing requests in the bottom
3400
         *      part of the driver.
3401
         *
3402
         *      If not, wait for the pipeline to be full enough (75%) before
3403
         *      starting to service requests, so that we will be able to
3404
         *      keep up with the higher speeds of the tape.
3405
         */
3406
 
3407
        if (tape->active_data_request == NULL && tape->current_number_of_stages >= (3 * tape->max_number_of_stages) / 4)
3408
                idetape_insert_pipeline_into_queue (drive);
3409
 
3410
        if (tape->error_in_pipeline_stage) {            /* Return a deferred error */
3411
                tape->error_in_pipeline_stage=0;
3412
                return (-EIO);
3413
        }
3414
 
3415
        return (blocks);
3416
}
3417
 
3418
void idetape_discard_read_pipeline (ide_drive_t *drive)
3419
 
3420
{
3421
        idetape_tape_t *tape = &(drive->tape);
3422
        unsigned long flags;
3423
 
3424
#if IDETAPE_DEBUG_BUGS
3425
        if (tape->chrdev_direction != idetape_direction_read) {
3426
                printk ("ide-tape: bug: Trying to discard read pipeline, but we are not reading.\n");
3427
                return;
3428
        }
3429
#endif /* IDETAPE_DEBUG_BUGS */
3430
 
3431
        tape->merge_buffer_size=tape->merge_buffer_offset=0;
3432
        tape->chrdev_direction=idetape_direction_none;
3433
 
3434
        if (tape->first_stage == NULL)
3435
                return;
3436
 
3437
        save_flags (flags);cli ();
3438
        tape->next_stage=NULL;
3439
        if (tape->active_data_request != NULL)
3440
                idetape_wait_for_request (tape->active_data_request);
3441
        restore_flags (flags);
3442
 
3443
        while (tape->first_stage != NULL)
3444
                idetape_remove_stage_head (drive);
3445
 
3446
#if IDETAPE_PIPELINE
3447
        tape->max_number_of_stages=IDETAPE_MIN_PIPELINE_STAGES;
3448
#else
3449
        tape->max_number_of_stages=0;
3450
#endif /* IDETAPE_PIPELINE */
3451
}
3452
 
3453
/*
3454
 *      idetape_wait_for_pipeline will wait until all pending pipeline
3455
 *      requests are serviced. Typically called on device close.
3456
 */
3457
 
3458
void idetape_wait_for_pipeline (ide_drive_t *drive)
3459
 
3460
{
3461
        idetape_tape_t *tape = &(drive->tape);
3462
        unsigned long flags;
3463
 
3464
        if (tape->active_data_request == NULL)
3465
                idetape_insert_pipeline_into_queue (drive);
3466
 
3467
        save_flags (flags);cli ();
3468
        if (tape->active_data_request == NULL) {
3469
                restore_flags (flags);
3470
                return;
3471
        }
3472
 
3473
        if (tape->last_stage != NULL)
3474
                idetape_wait_for_request (&(tape->last_stage->rq));
3475
 
3476
        else if (tape->active_data_request != NULL)
3477
                idetape_wait_for_request (tape->active_data_request);
3478
        restore_flags (flags);
3479
}
3480
 
3481
void idetape_empty_write_pipeline (ide_drive_t *drive)
3482
 
3483
{
3484
        idetape_tape_t *tape = &(drive->tape);
3485
        int blocks;
3486
 
3487
#if IDETAPE_DEBUG_BUGS
3488
        if (tape->chrdev_direction != idetape_direction_write) {
3489
                printk ("ide-tape: bug: Trying to empty write pipeline, but we are not writing.\n");
3490
                return;
3491
        }
3492
        if (tape->merge_buffer_size > tape->data_buffer_size) {
3493
                printk ("ide-tape: bug: merge_buffer too big\n");
3494
                tape->merge_buffer_size = tape->data_buffer_size;
3495
        }
3496
#endif /* IDETAPE_DEBUG_BUGS */
3497
 
3498
        if (tape->merge_buffer_size) {
3499
                blocks=tape->merge_buffer_size/tape->tape_block_size;
3500
                if (tape->merge_buffer_size % tape->tape_block_size) {
3501
                        blocks++;
3502
                        memset (tape->merge_buffer+tape->merge_buffer_size,0,tape->data_buffer_size-tape->merge_buffer_size);
3503
                }
3504
                (void) idetape_add_chrdev_write_request (drive,blocks,tape->merge_buffer);
3505
                tape->merge_buffer_size=0;
3506
        }
3507
 
3508
        idetape_wait_for_pipeline (drive);
3509
 
3510
        tape->error_in_pipeline_stage=0;
3511
        tape->chrdev_direction=idetape_direction_none;
3512
 
3513
        /*
3514
         *      On the next backup, perform the feedback loop again.
3515
         *      (I don't want to keep sense information between backups,
3516
         *       as some systems are constantly on, and the system load
3517
         *       can be totally different on the next backup).
3518
         */
3519
 
3520
#if IDETAPE_PIPELINE
3521
        tape->max_number_of_stages=IDETAPE_MIN_PIPELINE_STAGES;
3522
#else
3523
        tape->max_number_of_stages=0;
3524
#endif /* IDETAPE_PIPELINE */
3525
#if IDETAPE_DEBUG_BUGS
3526
        if (tape->first_stage != NULL || tape->next_stage != NULL || tape->last_stage != NULL || tape->current_number_of_stages != 0) {
3527
                printk ("ide-tape: ide-tape pipeline bug\n");
3528
        }
3529
#endif /* IDETAPE_DEBUG_BUGS */
3530
}
3531
 
3532
/*
3533
 *      idetape_zero_packet_command just zeros a packet command and
3534
 *      sets the number of retries to 0, as we haven't retried it yet.
3535
 */
3536
 
3537
void idetape_zero_packet_command (idetape_packet_command_t *pc)
3538
 
3539
{
3540
        int i;
3541
 
3542
        for (i=0;i<12;i++)
3543
                pc->c[i]=0;
3544
        pc->retries=0;
3545
        pc->abort=0;
3546
        pc->dma_recommended=0;
3547
        pc->dma_error=0;
3548
}
3549
 
3550
/*
3551
 *      idetape_swap_shorts converts a 16 bit number from little endian
3552
 *      to big endian format.
3553
 */
3554
 
3555
unsigned short idetape_swap_short (unsigned short temp)
3556
 
3557
{
3558
        union convert {
3559
                unsigned all    :16;
3560
                struct {
3561
                        unsigned b1     :8;
3562
                        unsigned b2     :8;
3563
                } b;
3564
        } original,converted;
3565
 
3566
        original.all=temp;
3567
        converted.b.b1=original.b.b2;
3568
        converted.b.b2=original.b.b1;
3569
        return (converted.all);
3570
}
3571
 
3572
/*
3573
 *      idetape_swap_long converts from little endian to big endian format.
3574
 */
3575
 
3576
unsigned long idetape_swap_long (unsigned long temp)
3577
 
3578
{
3579
        union convert {
3580
                unsigned all    :32;
3581
                struct {
3582
                        unsigned b1     :8;
3583
                        unsigned b2     :8;
3584
                        unsigned b3     :8;
3585
                        unsigned b4     :8;
3586
                } b;
3587
        } original,converted;
3588
 
3589
        original.all=temp;
3590
        converted.b.b1=original.b.b4;
3591
        converted.b.b2=original.b.b3;
3592
        converted.b.b3=original.b.b2;
3593
        converted.b.b4=original.b.b1;
3594
        return (converted.all);
3595
}
3596
 
3597
 
3598
/*
3599
 *      idetape_next_pc_storage returns a pointer to a place in which we can
3600
 *      safely store a packet command, even though we intend to leave the
3601
 *      driver. A storage space for a maximum of IDETAPE_PC_STACK packet
3602
 *      commands is allocated at initialization time.
3603
 */
3604
 
3605
idetape_packet_command_t *idetape_next_pc_storage (ide_drive_t *drive)
3606
 
3607
{
3608
        idetape_tape_t *tape;
3609
 
3610
        tape=&(drive->tape);
3611
#if IDETAPE_DEBUG_LOG
3612
        printk ("ide-tape: pc_stack_index=%d\n",tape->pc_stack_index);
3613
#endif /* IDETAPE_DEBUG_LOG */
3614
        if (tape->pc_stack_index==IDETAPE_PC_STACK)
3615
                tape->pc_stack_index=0;
3616
        return (&(tape->pc_stack [tape->pc_stack_index++]));
3617
}
3618
 
3619
/*
3620
 *      idetape_next_rq_storage is used along with idetape_next_pc_storage.
3621
 *      Since we queue packet commands in the request queue, we need to
3622
 *      allocate a request, along with the allocation of a packet command.
3623
 */
3624
 
3625
/**************************************************************
3626
 *                                                            *
3627
 *  This should get fixed to use kmalloc(GFP_ATOMIC, ..)      *
3628
 *  followed later on by kfree().   -ml                       *
3629
 *                                                            *
3630
 **************************************************************/
3631
 
3632
struct request *idetape_next_rq_storage (ide_drive_t *drive)
3633
 
3634
{
3635
        idetape_tape_t *tape;
3636
 
3637
        tape=&(drive->tape);
3638
 
3639
#if IDETAPE_DEBUG_LOG
3640
        printk ("ide-tape: rq_stack_index=%d\n",tape->rq_stack_index);
3641
#endif /* IDETAPE_DEBUG_LOG */
3642
        if (tape->rq_stack_index==IDETAPE_PC_STACK)
3643
                tape->rq_stack_index=0;
3644
        return (&(tape->rq_stack [tape->rq_stack_index++]));
3645
}
3646
 
3647
/*
3648
 *      Block device interface functions
3649
 *
3650
 *      The block device interface should not be used for data transfers.
3651
 *      However, we still allow opening it so that we can issue general
3652
 *      ide driver configuration ioctl's, such as the interrupt unmask feature.
3653
 */
3654
 
3655
int idetape_blkdev_open (struct inode *inode, struct file *filp, ide_drive_t *drive)
3656
 
3657
{
3658
        idetape_tape_t *tape=&(drive->tape);
3659
        unsigned long flags;
3660
 
3661
        save_flags (flags);cli ();
3662
 
3663
#if IDETAPE_DEBUG_LOG
3664
        printk ("Reached idetape_blkdev_open\n");
3665
#endif /* IDETAPE_DEBUG_LOG */
3666
 
3667
        if (tape->busy) {
3668
                restore_flags (flags);          /* Allowing access only through one */
3669
                return (-EBUSY);                /* one file descriptor */
3670
        }
3671
 
3672
        tape->busy=1;
3673
        restore_flags (flags);
3674
 
3675
        return (0);
3676
}
3677
 
3678
void idetape_blkdev_release (struct inode *inode, struct file *filp, ide_drive_t *drive)
3679
 
3680
{
3681
        idetape_tape_t *tape=&(drive->tape);
3682
        unsigned long flags;
3683
 
3684
#if IDETAPE_DEBUG_LOG
3685
        printk ("Reached idetape_blkdev_release\n");
3686
#endif /* IDETAPE_DEBUG_LOG */
3687
 
3688
        save_flags (flags);cli ();
3689
        tape->busy=0;
3690
        restore_flags (flags);
3691
 
3692
        return;
3693
}
3694
 
3695
/*
3696
 *      Character device interface functions
3697
 */
3698
 
3699
/*
3700
 *      Our character device read / write functions.
3701
 *
3702
 *      The tape is optimized to maximize throughput when it is transferring
3703
 *      an integral number of the "continuous transfer limit", which is
3704
 *      a parameter of the specific tape (26 KB on my particular tape).
3705
 *
3706
 *      For best results use an integral number of the tape's parameter
3707
 *      (which is displayed in the driver installation stage and is returned
3708
 *       by the MTIOCGET ioctl).
3709
 *
3710
 *      As of version 1.3 of the driver, the character device provides an
3711
 *      abstract continuous view of the media - any mix of block sizes (even 1
3712
 *      byte) on the same backup/restore procedure is supported. The driver
3713
 *      will internally convert the requests to the recommended transfer unit,
3714
 *      so that an unmatch between the user's block size to the recommended
3715
 *      size will only result in a (slightly) increased driver overhead, but
3716
 *      will no longer hit performance.
3717
 */
3718
 
3719
int idetape_chrdev_read (struct inode *inode, struct file *file, char *buf, int count)
3720
 
3721
{
3722
        ide_drive_t *drive=idetape_chrdev.drive;
3723
        idetape_tape_t *tape=&(drive->tape);
3724
        char *buf_ptr=buf;
3725
        int bytes_read,temp,actually_read=0;
3726
 
3727
#if IDETAPE_DEBUG_LOG
3728
        printk ("Reached idetape_chrdev_read\n");
3729
#endif /* IDETAPE_DEBUG_LOG */
3730
 
3731
        if (tape->chrdev_direction != idetape_direction_read) {         /* Initialize read operation */
3732
                if (tape->chrdev_direction == idetape_direction_write) {
3733
                        idetape_empty_write_pipeline (drive);
3734
                        idetape_flush_tape_buffers (drive);
3735
                }
3736
 
3737
                /*
3738
                 *      Issue a read 0 command to ensure that DSC handshake
3739
                 *      is switched from completion mode to buffer available
3740
                 *      mode.
3741
                 */
3742
 
3743
                bytes_read=idetape_queue_rw_tail (drive,IDETAPE_READ_REQUEST,0,tape->merge_buffer);
3744
                if (bytes_read < 0)
3745
                        return (bytes_read);
3746
 
3747
                tape->chrdev_direction=idetape_direction_read;
3748
        }
3749
 
3750
        if (count==0)
3751
                return (0);
3752
 
3753
        if (tape->merge_buffer_size) {
3754
#if IDETAPE_DEBUG_BUGS
3755
                if (tape->merge_buffer_offset+tape->merge_buffer_size > tape->data_buffer_size) {
3756
                        printk ("ide-tape: bug: merge buffer too big\n");
3757
                        tape->merge_buffer_offset=0;tape->merge_buffer_size=tape->data_buffer_size-1;
3758
                }
3759
#endif /* IDETAPE_DEBUG_BUGS */
3760
                actually_read=IDETAPE_MIN (tape->merge_buffer_size,count);
3761
                memcpy_tofs (buf_ptr,tape->merge_buffer+tape->merge_buffer_offset,actually_read);
3762
                buf_ptr+=actually_read;tape->merge_buffer_size-=actually_read;
3763
                count-=actually_read;tape->merge_buffer_offset+=actually_read;
3764
        }
3765
 
3766
        while (count >= tape->data_buffer_size) {
3767
                bytes_read=idetape_add_chrdev_read_request (drive,tape->capabilities.ctl,tape->merge_buffer);
3768
                if (bytes_read <= 0)
3769
                        goto finish;
3770
                memcpy_tofs (buf_ptr,tape->merge_buffer,bytes_read);
3771
                buf_ptr+=bytes_read;count-=bytes_read;actually_read+=bytes_read;
3772
        }
3773
 
3774
        if (count) {
3775
                bytes_read=idetape_add_chrdev_read_request (drive,tape->capabilities.ctl,tape->merge_buffer);
3776
                if (bytes_read <= 0)
3777
                        goto finish;
3778
                temp=IDETAPE_MIN (count,bytes_read);
3779
                memcpy_tofs (buf_ptr,tape->merge_buffer,temp);
3780
                actually_read+=temp;
3781
                tape->merge_buffer_offset=temp;
3782
                tape->merge_buffer_size=bytes_read-temp;
3783
        }
3784
finish:
3785
        if (!actually_read && tape->filemark)
3786
                idetape_space_over_filemarks (drive, MTFSF, 1);
3787
        return (actually_read);
3788
}
3789
 
3790
int idetape_chrdev_write (struct inode *inode, struct file *file, const char *buf, int count)
3791
 
3792
{
3793
        ide_drive_t *drive=idetape_chrdev.drive;
3794
        idetape_tape_t *tape=&(drive->tape);
3795
        const char *buf_ptr=buf;
3796
        int retval,actually_written=0;
3797
 
3798
#if IDETAPE_DEBUG_LOG
3799
        printk ("Reached idetape_chrdev_write\n");
3800
#endif /* IDETAPE_DEBUG_LOG */
3801
 
3802
        if (tape->chrdev_direction != idetape_direction_write) {        /* Initialize write operation */
3803
                if (tape->chrdev_direction == idetape_direction_read)
3804
                        idetape_discard_read_pipeline (drive);
3805
 
3806
                /*
3807
                 *      Issue a write 0 command to ensure that DSC handshake
3808
                 *      is switched from completion mode to buffer available
3809
                 *      mode.
3810
                 */
3811
 
3812
                retval=idetape_queue_rw_tail (drive,IDETAPE_WRITE_REQUEST,0,tape->merge_buffer);
3813
                if (retval < 0)
3814
                        return (retval);
3815
 
3816
                tape->chrdev_direction=idetape_direction_write;
3817
        }
3818
 
3819
        if (count==0)
3820
                return (0);
3821
 
3822
        if (tape->merge_buffer_size) {
3823
#if IDETAPE_DEBUG_BUGS
3824
                if (tape->merge_buffer_size >= tape->data_buffer_size) {
3825
                        printk ("ide-tape: bug: merge buffer too big\n");
3826
                        tape->merge_buffer_size=0;
3827
                }
3828
#endif /* IDETAPE_DEBUG_BUGS */
3829
 
3830
                actually_written=IDETAPE_MIN (tape->data_buffer_size-tape->merge_buffer_size,count);
3831
                memcpy_fromfs (tape->merge_buffer+tape->merge_buffer_size,buf_ptr,actually_written);
3832
                buf_ptr+=actually_written;tape->merge_buffer_size+=actually_written;count-=actually_written;
3833
 
3834
                if (tape->merge_buffer_size == tape->data_buffer_size) {
3835
                        tape->merge_buffer_size=0;
3836
                        retval=idetape_add_chrdev_write_request (drive,tape->capabilities.ctl,tape->merge_buffer);
3837
                        if (retval <= 0)
3838
                                return (retval);
3839
                }
3840
        }
3841
 
3842
        while (count >= tape->data_buffer_size) {
3843
                memcpy_fromfs (tape->merge_buffer,buf_ptr,tape->data_buffer_size);
3844
                buf_ptr+=tape->data_buffer_size;count-=tape->data_buffer_size;
3845
                retval=idetape_add_chrdev_write_request (drive,tape->capabilities.ctl,tape->merge_buffer);
3846
                actually_written+=tape->data_buffer_size;
3847
                if (retval <= 0)
3848
                        return (retval);
3849
        }
3850
 
3851
        if (count) {
3852
                actually_written+=count;
3853
                memcpy_fromfs (tape->merge_buffer,buf_ptr,count);
3854
                tape->merge_buffer_size+=count;
3855
        }
3856
        return (actually_written);
3857
}
3858
 
3859
static int idetape_pipeline_size (ide_drive_t *drive)
3860
{
3861
        idetape_tape_t *tape = &drive->tape;
3862
        idetape_pipeline_stage_t *stage;
3863
        struct request *rq;
3864
        int size = 0;
3865
 
3866
        idetape_wait_for_pipeline (drive);
3867
        stage = tape->first_stage;
3868
        while (stage != NULL) {
3869
                rq = &stage->rq;
3870
                size += tape->tape_block_size * (rq->nr_sectors-rq->current_nr_sectors);
3871
                if (rq->errors == IDETAPE_RQ_ERROR_FILEMARK)
3872
                        size += tape->tape_block_size;
3873
                stage = stage->next;
3874
        }
3875
        size += tape->merge_buffer_size;
3876
        return size;
3877
}
3878
 
3879
/*
3880
 *      Our character device ioctls.
3881
 *
3882
 *      General mtio.h magnetic io commands are supported here, and not in
3883
 *      the corresponding block interface.
3884
 *
3885
 *      The following ioctls are supported:
3886
 *
3887
 *      MTIOCTOP -      Refer to idetape_mtioctop for detailed description.
3888
 *
3889
 *      MTIOCGET -      Some of the fields are not supported.
3890
 *
3891
 *      MTIOCPOS -      The current tape "position" is returned.
3892
 *                      (A unique number which can be used with the MTSEEK
3893
 *                       operation to return to this position in some
3894
 *                       future time, provided this place was not overwritten
3895
 *                       meanwhile).
3896
 *
3897
 *      Our own ide-tape ioctls are supported on both interfaces.
3898
 */
3899
 
3900
int idetape_chrdev_ioctl (struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg)
3901
 
3902
{
3903
        ide_drive_t *drive=idetape_chrdev.drive;
3904
        idetape_tape_t *tape=&(drive->tape);
3905
        idetape_packet_command_t pc;
3906
        struct mtop mtop;
3907
        struct mtget mtget;
3908
        struct mtpos mtpos;
3909
        int retval, block_offset = 0;
3910
 
3911
#if IDETAPE_DEBUG_LOG
3912
        printk ("Reached idetape_chrdev_ioctl, cmd=%u\n",cmd);
3913
#endif /* IDETAPE_DEBUG_LOG */
3914
 
3915
        if (tape->chrdev_direction == idetape_direction_write) {
3916
                idetape_empty_write_pipeline (drive);
3917
                idetape_flush_tape_buffers (drive);
3918
        }
3919
        pc.buffer=pc.temp_buffer;
3920
        pc.buffer_size=IDETAPE_TEMP_BUFFER_SIZE;
3921
        pc.current_position=pc.temp_buffer;
3922
 
3923
        if (cmd == MTIOCGET || cmd == MTIOCPOS) {
3924
                block_offset = idetape_pipeline_size (drive) / tape->tape_block_size;
3925
                idetape_create_read_position_cmd (&pc);
3926
                retval=idetape_queue_pc_tail (drive,&pc);
3927
                if (retval) return (retval);
3928
        }
3929
        switch (cmd) {
3930
                case MTIOCTOP:
3931
                        retval=verify_area (VERIFY_READ,(char *) arg,sizeof (struct mtop));
3932
                        if (retval) return (retval);
3933
                        memcpy_fromfs ((char *) &mtop, (char *) arg, sizeof (struct mtop));
3934
                        return (idetape_mtioctop (drive,mtop.mt_op,mtop.mt_count));
3935
                case MTIOCGET:
3936
                        memset (&mtget, 0, sizeof (struct mtget));
3937
                        mtget.mt_blkno = tape->block_address - block_offset;
3938
                        mtget.mt_dsreg=(tape->data_buffer_size << MT_ST_BLKSIZE_SHIFT) & MT_ST_BLKSIZE_MASK;
3939
                        retval=verify_area (VERIFY_WRITE,(char *) arg,sizeof (struct mtget));
3940
                        if (retval) return (retval);
3941
                        memcpy_tofs ((char *) arg,(char *) &mtget, sizeof (struct mtget));
3942
                        return (0);
3943
                case MTIOCPOS:
3944
                        mtpos.mt_blkno = tape->block_address - block_offset;
3945
                        retval=verify_area (VERIFY_WRITE,(char *) arg,sizeof (struct mtpos));
3946
                        if (retval) return (retval);
3947
                        memcpy_tofs ((char *) arg,(char *) &mtpos, sizeof (struct mtpos));
3948
                        return (0);
3949
                default:
3950
                        return (idetape_blkdev_ioctl (drive,inode,file,cmd,arg));
3951
        }
3952
}
3953
 
3954
/*
3955
 *      idetape_mtioctop is called from idetape_chrdev_ioctl when
3956
 *      the general mtio MTIOCTOP ioctl is requested.
3957
 *
3958
 *      We currently support the following mtio.h operations:
3959
 *
3960
 *      MTFSF   -       Space over mt_count filemarks in the positive direction.
3961
 *                      The tape is positioned after the last spaced filemark.
3962
 *
3963
 *      MTFSFM  -       Same as MTFSF, but the tape is positioned before the
3964
 *                      last filemark.
3965
 *
3966
 *      MTBSF   -       Steps background over mt_count filemarks, tape is
3967
 *                      positioned before the last filemark.
3968
 *
3969
 *      MTBSFM  -       Like MTBSF, only tape is positioned after the last filemark.
3970
 *
3971
 *
3972
 *      Note:
3973
 *
3974
 *              MTBSF and MTBSFM are not supported when the tape doesn't
3975
 *              supports spacing over filemarks in the reverse direction.
3976
 *              In this case, MTFSFM is also usually not supported (it is
3977
 *              supported in the rare case in which we crossed the filemark
3978
 *              during our read-ahead pipelined operation mode).
3979
 *
3980
 *      MTWEOF  -       Writes mt_count filemarks. Tape is positioned after
3981
 *                      the last written filemark.
3982
 *
3983
 *      MTREW   -       Rewinds tape.
3984
 *
3985
 *      MTOFFL  -       Puts the tape drive "Offline": Rewinds the tape and
3986
 *                      prevents further access until the media is replaced.
3987
 *
3988
 *      MTNOP   -       Flushes tape buffers.
3989
 *
3990
 *      MTRETEN -       Retension media. This typically consists of one end
3991
 *                      to end pass on the media.
3992
 *
3993
 *      MTEOM   -       Moves to the end of recorded data.
3994
 *
3995
 *      MTERASE -       Erases tape.
3996
 *
3997
 *      MTSEEK  -       Positions the tape in a specific block number, which
3998
 *                      was previously received using the MTIOCPOS ioctl,
3999
 *                      assuming this place was not overwritten meanwhile.
4000
 *
4001
 *      The following commands are currently not supported:
4002
 *
4003
 *      MTFSR, MTBSR, MTFSS, MTBSS, MTWSM, MTSETBLK, MTSETDENSITY,
4004
 *      MTSETDRVBUFFER, MT_ST_BOOLEANS, MT_ST_WRITE_THRESHOLD.
4005
 */
4006
 
4007
int idetape_mtioctop (ide_drive_t *drive,short mt_op,int mt_count)
4008
 
4009
{
4010
        idetape_tape_t *tape=&(drive->tape);
4011
        idetape_packet_command_t pc;
4012
        int i,retval;
4013
 
4014
        pc.buffer=pc.temp_buffer;
4015
        pc.buffer_size=IDETAPE_TEMP_BUFFER_SIZE;
4016
        pc.current_position=pc.temp_buffer;
4017
 
4018
#if IDETAPE_DEBUG_LOG
4019
        printk ("Handling MTIOCTOP ioctl: mt_op=%d, mt_count=%d\n",mt_op,mt_count);
4020
#endif /* IDETAPE_DEBUG_LOG */
4021
 
4022
        /*
4023
         *      Commands which need our pipelined read-ahead stages.
4024
         */
4025
 
4026
        switch (mt_op) {
4027
                case MTFSF:
4028
                case MTFSFM:
4029
                case MTBSF:
4030
                case MTBSFM:
4031
                        if (!mt_count)
4032
                                return (0);
4033
                        return (idetape_space_over_filemarks (drive,mt_op,mt_count));
4034
                default:
4035
                        break;
4036
        }
4037
 
4038
        /*
4039
         *      Empty the pipeline.
4040
         */
4041
 
4042
        if (tape->chrdev_direction == idetape_direction_read)
4043
                idetape_discard_read_pipeline (drive);
4044
 
4045
        switch (mt_op) {
4046
                case MTWEOF:
4047
                        for (i=0;i<mt_count;i++) {
4048
                                idetape_create_write_filemark_cmd (&pc,1);
4049
                                retval=idetape_queue_pc_tail (drive,&pc);
4050
                                if (retval) return (retval);
4051
                        }
4052
                        return (0);
4053
                case MTREW:
4054
                        return (idetape_rewind_tape (drive));
4055
                case MTOFFL:
4056
                        idetape_create_load_unload_cmd (&pc,!IDETAPE_LU_LOAD_MASK);
4057
                        return (idetape_queue_pc_tail (drive,&pc));
4058
                case MTNOP:
4059
                        return (idetape_flush_tape_buffers (drive));
4060
                case MTRETEN:
4061
                        idetape_create_load_unload_cmd (&pc,IDETAPE_LU_RETENSION_MASK | IDETAPE_LU_LOAD_MASK);
4062
                        return (idetape_queue_pc_tail (drive,&pc));
4063
                case MTEOM:
4064
                        idetape_create_space_cmd (&pc,0,IDETAPE_SPACE_TO_EOD);
4065
                        return (idetape_queue_pc_tail (drive,&pc));
4066
                case MTERASE:
4067
                        retval=idetape_rewind_tape (drive);
4068
                        if (retval) return (retval);
4069
                        idetape_create_erase_cmd (&pc);
4070
                        return (idetape_queue_pc_tail (drive,&pc));
4071
                case MTSEEK:
4072
                        return (idetape_position_tape (drive,mt_count));
4073
                default:
4074
                        printk ("ide-tape: MTIO operation %d not supported\n",mt_op);
4075
                        return (-EIO);
4076
        }
4077
}
4078
 
4079
/*
4080
 *      idetape_space_over_filemarks is now a bit more complicated than just
4081
 *      passing the command to the tape since we may have crossed some
4082
 *      filemarks during our pipelined read-ahead mode.
4083
 *
4084
 *      As a minor side effect, the pipeline enables us to support MTFSFM when
4085
 *      the filemark is in our internal pipeline even if the tape doesn't
4086
 *      support spacing over filemarks in the reverse direction.
4087
 */
4088
 
4089
int idetape_space_over_filemarks (ide_drive_t *drive,short mt_op,int mt_count)
4090
 
4091
{
4092
        idetape_tape_t *tape=&(drive->tape);
4093
        idetape_packet_command_t pc;
4094
        unsigned long flags;
4095
        int retval,count=0,errors;
4096
 
4097
        if (tape->chrdev_direction == idetape_direction_read) {
4098
 
4099
                /*
4100
                 *      We have a read-ahead buffer. Scan it for crossed
4101
                 *      filemarks.
4102
                 */
4103
 
4104
                tape->merge_buffer_size=tape->merge_buffer_offset=0;
4105
                tape->filemark = 0;
4106
                while (tape->first_stage != NULL) {
4107
 
4108
                        /*
4109
                         *      Wait until the first read-ahead request
4110
                         *      is serviced.
4111
                         */
4112
 
4113
                        save_flags (flags);cli ();
4114
                        if (tape->active_data_request == &(tape->first_stage->rq))
4115
                                idetape_wait_for_request (tape->active_data_request);
4116
                        restore_flags (flags);
4117
 
4118
                        errors=tape->first_stage->rq.errors;
4119
                        if (errors == IDETAPE_RQ_ERROR_FILEMARK)
4120
                                count++;
4121
 
4122
                        if (count == mt_count) {
4123
                                switch (mt_op) {
4124
                                        case MTFSF:
4125
                                                idetape_remove_stage_head (drive);
4126
                                        case MTFSFM:
4127
                                                return (0);
4128
                                }
4129
                        }
4130
                        idetape_remove_stage_head (drive);
4131
                }
4132
                idetape_discard_read_pipeline (drive);
4133
        }
4134
 
4135
        /*
4136
         *      The filemark was not found in our internal pipeline.
4137
         *      Now we can issue the space command.
4138
         */
4139
 
4140
        pc.buffer=pc.temp_buffer;
4141
        pc.buffer_size=IDETAPE_TEMP_BUFFER_SIZE;
4142
        pc.current_position=pc.temp_buffer;
4143
 
4144
        switch (mt_op) {
4145
                case MTFSF:
4146
                        idetape_create_space_cmd (&pc,mt_count-count,IDETAPE_SPACE_OVER_FILEMARK);
4147
                        return (idetape_queue_pc_tail (drive,&pc));
4148
                case MTFSFM:
4149
                        if (!tape->capabilities.sprev)
4150
                                return (-EIO);
4151
                        retval=idetape_mtioctop (drive,MTFSF,mt_count-count);
4152
                        if (retval) return (retval);
4153
                        return (idetape_mtioctop (drive,MTBSF,1));
4154
                case MTBSF:
4155
                        if (!tape->capabilities.sprev)
4156
                                return (-EIO);
4157
                        idetape_create_space_cmd (&pc,-(mt_count+count),IDETAPE_SPACE_OVER_FILEMARK);
4158
                        return (idetape_queue_pc_tail (drive,&pc));
4159
                case MTBSFM:
4160
                        if (!tape->capabilities.sprev)
4161
                                return (-EIO);
4162
                        retval=idetape_mtioctop (drive,MTBSF,mt_count+count);
4163
                        if (retval) return (retval);
4164
                        return (idetape_mtioctop (drive,MTFSF,1));
4165
                default:
4166
                        printk ("ide-tape: MTIO operation %d not supported\n",mt_op);
4167
                        return (-EIO);
4168
        }
4169
}
4170
 
4171
/*
4172
 *      Our character device open function.
4173
 */
4174
 
4175
int idetape_chrdev_open (struct inode *inode, struct file *filp)
4176
 
4177
{
4178
        ide_drive_t *drive=idetape_chrdev.drive;
4179
        idetape_tape_t *tape=&(drive->tape);
4180
        unsigned long flags;
4181
        unsigned int minor=MINOR (inode->i_rdev),allocation_length;
4182
 
4183
        save_flags (flags);cli ();
4184
 
4185
#if IDETAPE_DEBUG_LOG
4186
        printk ("Reached idetape_chrdev_open\n");
4187
#endif /* IDETAPE_DEBUG_LOG */
4188
 
4189
        if (minor!=0 && minor!=128) {            /* Currently supporting only one */
4190
                restore_flags (flags);          /* tape drive */
4191
                return (-ENXIO);
4192
        }
4193
 
4194
        if (tape->busy) {
4195
                restore_flags (flags);          /* Allowing access only through one */
4196
                return (-EBUSY);                /* one file descriptor */
4197
        }
4198
 
4199
        tape->busy=1;
4200
        restore_flags (flags);
4201
 
4202
        allocation_length=tape->data_buffer_size;
4203
        if (tape->data_buffer_size % IDETAPE_ALLOCATION_BLOCK)
4204
                allocation_length+=IDETAPE_ALLOCATION_BLOCK;
4205
 
4206
#if IDETAPE_MINIMIZE_IDLE_MEMORY_USAGE
4207
        if (tape->data_buffer == NULL)
4208
                tape->data_buffer=kmalloc (allocation_length,GFP_KERNEL);
4209
        if (tape->data_buffer == NULL)
4210
                goto sorry;
4211
        if (tape->merge_buffer == NULL)
4212
                tape->merge_buffer=kmalloc (allocation_length,GFP_KERNEL);
4213
        if (tape->merge_buffer == NULL) {
4214
                kfree (tape->data_buffer);
4215
        sorry:
4216
                printk ("ide-tape: FATAL - Can not allocate continuous buffer of %d bytes\n",allocation_length);
4217
                tape->busy=0;
4218
                return (-EIO);
4219
        }
4220
#endif /* IDETAPE_MINIMIZE_IDLE_MEMORY_USAGE */
4221
 
4222
        if (!tape->block_address_valid) {
4223
                if (idetape_rewind_tape (drive)) {
4224
                        printk ("ide-tape: Rewinding tape failed\n");
4225
                        tape->busy=0;
4226
                        return (-EIO);
4227
                }
4228
        }
4229
 
4230
        return (0);
4231
}
4232
 
4233
/*
4234
 *      Our character device release function.
4235
 */
4236
 
4237
void idetape_chrdev_release (struct inode *inode, struct file *filp)
4238
 
4239
{
4240
        ide_drive_t *drive=idetape_chrdev.drive;
4241
        idetape_tape_t *tape=&(drive->tape);
4242
        unsigned int minor=MINOR (inode->i_rdev);
4243
        idetape_packet_command_t pc;
4244
        unsigned long flags;
4245
 
4246
#if IDETAPE_DEBUG_LOG
4247
        printk ("Reached idetape_chrdev_release\n");
4248
#endif /* IDETAPE_DEBUG_LOG */
4249
 
4250
        if (tape->chrdev_direction == idetape_direction_write) {
4251
                idetape_empty_write_pipeline (drive);
4252
                idetape_create_write_filemark_cmd (&pc,1);      /* Write a filemark */
4253
                if (idetape_queue_pc_tail (drive,&pc))
4254
                        printk ("ide-tape: Couldn't write a filemark\n");
4255
        }
4256
 
4257
        if (tape->chrdev_direction == idetape_direction_read) {
4258
                if (minor < 128)
4259
                        idetape_discard_read_pipeline (drive);
4260
                else
4261
                        idetape_wait_for_pipeline (drive);
4262
        }
4263
 
4264
        if (minor < 128)
4265
                if (idetape_rewind_tape (drive))
4266
                        printk ("ide-tape: Rewinding tape failed\n");
4267
 
4268
#if IDETAPE_MINIMIZE_IDLE_MEMORY_USAGE
4269
        kfree (tape->data_buffer);
4270
        tape->data_buffer=NULL;
4271
        if (!tape->merge_buffer_size) {
4272
                kfree (tape->merge_buffer);
4273
                tape->merge_buffer=NULL;
4274
        }
4275
#endif /* IDETAPE_MINIMIZE_IDLE_MEMORY_USAGE */
4276
 
4277
        save_flags (flags);cli ();
4278
        tape->busy=0;
4279
        restore_flags (flags);
4280
 
4281
        return;
4282
}
4283
 
4284
/*
4285
 *      idetape_position_tape positions the tape to the requested block
4286
 *      using the LOCATE packet command. A READ POSITION command is then
4287
 *      issued to check where we are positioned.
4288
 *
4289
 *      Like all higher level operations, we queue the commands at the tail
4290
 *      of the request queue and wait for their completion.
4291
 *
4292
 */
4293
 
4294
int idetape_position_tape (ide_drive_t *drive,unsigned long block)
4295
 
4296
{
4297
        int retval;
4298
        idetape_packet_command_t pc;
4299
 
4300
        idetape_create_locate_cmd (&pc,block,0);
4301
        retval=idetape_queue_pc_tail (drive,&pc);
4302
        if (retval!=0) return (retval);
4303
 
4304
        idetape_create_read_position_cmd (&pc);
4305
        pc.buffer=pc.temp_buffer;
4306
        pc.buffer_size=IDETAPE_TEMP_BUFFER_SIZE;
4307
        pc.current_position=pc.temp_buffer;
4308
        return (idetape_queue_pc_tail (drive,&pc));
4309
}
4310
 
4311
/*
4312
 *      Rewinds the tape to the Beginning Of the current Partition (BOP).
4313
 *
4314
 *      We currently support only one partition.
4315
 */
4316
 
4317
int idetape_rewind_tape (ide_drive_t *drive)
4318
 
4319
{
4320
        int retval;
4321
        idetape_packet_command_t pc;
4322
#if IDETAPE_DEBUG_LOG
4323
        printk ("Reached idetape_rewind_tape\n");
4324
#endif /* IDETAPE_DEBUG_LOG */  
4325
 
4326
        idetape_create_rewind_cmd (&pc);
4327
        retval=idetape_queue_pc_tail (drive,&pc);
4328
        if (retval) return (retval);
4329
 
4330
        idetape_create_read_position_cmd (&pc);
4331
        pc.buffer=pc.temp_buffer;
4332
        pc.buffer_size=IDETAPE_TEMP_BUFFER_SIZE;
4333
        pc.current_position=pc.temp_buffer;
4334
        return (idetape_queue_pc_tail (drive,&pc));
4335
}
4336
 
4337
int idetape_flush_tape_buffers (ide_drive_t *drive)
4338
 
4339
{
4340
        idetape_packet_command_t pc;
4341
 
4342
        idetape_create_write_filemark_cmd (&pc,0);
4343
        return (idetape_queue_pc_tail (drive,&pc));
4344
}
4345
 
4346
/*
4347
 *      Pipeline related functions
4348
 */
4349
 
4350
/*
4351
 *      idetape_kmalloc_stage uses kmalloc to allocate a pipeline stage,
4352
 *      along with all the necessary small buffers which together make
4353
 *      a buffer of size tape->data_buffer_size or a bit more, in case
4354
 *      it is not a multiply of IDETAPE_ALLOCATION_BLOCK (it isn't ...).
4355
 *
4356
 *      Returns a pointer to the new allocated stage, or NULL if we
4357
 *      can't (or don't want to, in case we already have too many stages)
4358
 *      allocate a stage.
4359
 *
4360
 *      Pipeline stages are optional and are used to increase performance.
4361
 *      If we can't allocate them, we'll manage without them.
4362
 */
4363
 
4364
idetape_pipeline_stage_t *idetape_kmalloc_stage (ide_drive_t *drive)
4365
 
4366
{
4367
        idetape_tape_t *tape=&(drive->tape);
4368
        idetape_pipeline_stage_t *new_stage;
4369
        idetape_buffer_head_t *prev_bh,*bh;
4370
        int buffers_num,i;
4371
 
4372
#if IDETAPE_DEBUG_LOG
4373
        printk ("Reached idetape_kmalloc_stage\n");
4374
#endif /* IDETAPE_DEBUG_LOG */
4375
 
4376
        if (tape->current_number_of_stages>=tape->max_number_of_stages) {
4377
                return (NULL);
4378
        }
4379
 
4380
        new_stage=(idetape_pipeline_stage_t *) kmalloc (sizeof (idetape_pipeline_stage_t),GFP_KERNEL);
4381
        if (new_stage==NULL)
4382
                return (NULL);
4383
 
4384
        new_stage->next=new_stage->prev=NULL;
4385
 
4386
        buffers_num=tape->data_buffer_size / IDETAPE_ALLOCATION_BLOCK;
4387
        if (tape->data_buffer_size % IDETAPE_ALLOCATION_BLOCK)
4388
                buffers_num++;
4389
 
4390
        prev_bh=new_stage->bh=(idetape_buffer_head_t *) kmalloc (sizeof (idetape_buffer_head_t),GFP_KERNEL);
4391
        if (new_stage->bh==NULL) {
4392
                idetape_kfree_stage (new_stage);
4393
                return (NULL);
4394
        }
4395
        new_stage->bh->next=NULL;
4396
 
4397
        new_stage->bh->data=kmalloc (IDETAPE_ALLOCATION_BLOCK,GFP_KERNEL);
4398
        if (new_stage->bh->data==NULL) {
4399
                idetape_kfree_stage (new_stage);
4400
                return (NULL);
4401
        }
4402
 
4403
        for (i=1;i<buffers_num;i++) {
4404
                bh=(idetape_buffer_head_t *) kmalloc (sizeof (idetape_buffer_head_t),GFP_KERNEL);
4405
                if (bh==NULL) {
4406
                        idetape_kfree_stage (new_stage);
4407
                        return (NULL);
4408
                }
4409
                bh->next=NULL;
4410
                prev_bh->next=bh;
4411
                bh->data=kmalloc (IDETAPE_ALLOCATION_BLOCK,GFP_KERNEL);
4412
                if (bh->data == NULL) {
4413
                        idetape_kfree_stage (new_stage);
4414
                        return (NULL);
4415
                }
4416
                prev_bh=bh;
4417
        }
4418
        return (new_stage);
4419
}
4420
 
4421
/*
4422
 *      idetape_kfree_stage calls kfree to completely free a stage, along with
4423
 *      its related buffers.
4424
 */
4425
 
4426
void idetape_kfree_stage (idetape_pipeline_stage_t *stage)
4427
 
4428
{
4429
        idetape_buffer_head_t *prev_bh,*bh;
4430
 
4431
        if (stage == NULL)
4432
                return;
4433
 
4434
#if IDETAPE_DEBUG_LOG
4435
        printk ("Reached idetape_kfree_stage\n");
4436
#endif /* IDETAPE_DEBUG_LOG */
4437
 
4438
        bh=stage->bh;
4439
 
4440
        while (bh != NULL) {
4441
                prev_bh=bh;
4442
                if (bh->data != NULL)
4443
                        kfree (bh->data);
4444
                bh=bh->next;
4445
                kfree (prev_bh);
4446
        }
4447
 
4448
        kfree (stage);
4449
        return;
4450
}
4451
 
4452
/*
4453
 *      idetape_copy_buffer_from_stage and idetape_copy_buffer_to_stage
4454
 *      copy data from/to the small buffers into/from a continuous buffer.
4455
 */
4456
 
4457
void idetape_copy_buffer_from_stage (idetape_pipeline_stage_t *stage,char *buffer)
4458
 
4459
{
4460
        idetape_buffer_head_t *bh;
4461
        char *ptr;
4462
 
4463
#if IDETAPE_DEBUG_LOG
4464
        printk ("Reached idetape_copy_buffer_from_stage\n");
4465
#endif /* IDETAPE_DEBUG_LOG */
4466
#if IDETAPE_DEBUG_BUGS
4467
        if (buffer == NULL) {
4468
                printk ("ide-tape: bug: buffer is null in copy_buffer_from_stage\n");
4469
                return;
4470
        }
4471
#endif /* IDETAPE_DEBUG_BUGS */
4472
 
4473
        ptr=buffer;
4474
        bh=stage->bh;
4475
 
4476
        while (bh != NULL) {
4477
#if IDETAPE_DEBUG_BUGS
4478
                if (bh->data == NULL) {
4479
                        printk ("ide-tape: bug: bh->data is null\n");
4480
                        return;
4481
                }
4482
#endif /* IDETAPE_DEBUG_BUGS */
4483
                memcpy (ptr,bh->data,IDETAPE_ALLOCATION_BLOCK);
4484
                bh=bh->next;
4485
                ptr=ptr+IDETAPE_ALLOCATION_BLOCK;
4486
        }
4487
        return;
4488
}
4489
 
4490
/*
4491
 *      Here we copy a continuous data buffer to the various small buffers
4492
 *      in the pipeline stage.
4493
 */
4494
 
4495
void idetape_copy_buffer_to_stage (idetape_pipeline_stage_t *stage,char *buffer)
4496
 
4497
{
4498
        idetape_buffer_head_t *bh;
4499
        char *ptr;
4500
 
4501
#if IDETAPE_DEBUG_LOG
4502
        printk ("Reached idetape_copy_buffer_to_stage\n");
4503
#endif /* IDETAPE_DEBUG_LOG */
4504
#if IDETAPE_DEBUG_BUGS
4505
        if (buffer == NULL) {
4506
                printk ("ide-tape: bug: buffer is null in copy_buffer_to_stage\n");
4507
                return;
4508
        }
4509
#endif /* IDETAPE_DEBUG_BUGS */
4510
 
4511
        ptr=buffer;
4512
        bh=stage->bh;
4513
 
4514
        while (bh != NULL) {
4515
#if IDETAPE_DEBUG_BUGS
4516
                if (bh->data == NULL) {
4517
                        printk ("ide-tape: bug: bh->data is null\n");
4518
                        return;
4519
                }
4520
#endif /* IDETAPE_DEBUG_BUGS */
4521
                memcpy (bh->data,ptr,IDETAPE_ALLOCATION_BLOCK);
4522
                bh=bh->next;
4523
                ptr=ptr+IDETAPE_ALLOCATION_BLOCK;
4524
        }
4525
        return;
4526
}
4527
 
4528
/*
4529
 *      idetape_increase_max_pipeline_stages is a part of the feedback
4530
 *      loop which tries to find the optimum number of stages. In the
4531
 *      feedback loop, we are starting from a minimum maximum number of
4532
 *      stages, and if we sense that the pipeline is empty, we try to
4533
 *      increase it, until we reach the user compile time memory limit.
4534
 */
4535
 
4536
void idetape_increase_max_pipeline_stages (ide_drive_t *drive)
4537
 
4538
{
4539
        idetape_tape_t *tape=&(drive->tape);
4540
 
4541
#if IDETAPE_DEBUG_LOG
4542
        printk ("Reached idetape_increase_max_pipeline_stages\n");
4543
#endif /* IDETAPE_DEBUG_LOG */
4544
 
4545
        tape->max_number_of_stages+=IDETAPE_INCREASE_STAGES_RATE;
4546
 
4547
        if (tape->max_number_of_stages >= IDETAPE_MAX_PIPELINE_STAGES)
4548
                tape->max_number_of_stages = IDETAPE_MAX_PIPELINE_STAGES;
4549
 
4550
#if IDETAPE_DEBUG_LOG
4551
        printk ("Maximum number of stages: %d\n",tape->max_number_of_stages);
4552
#endif /* IDETAPE_DEBUG_LOG */
4553
 
4554
        return;
4555
}
4556
 
4557
/*
4558
 *      idetape_add_stage_tail adds a new stage at the end of the pipeline.
4559
 *
4560
 *      Caller should disable interrupts, if necessary.
4561
 */
4562
 
4563
void idetape_add_stage_tail (ide_drive_t *drive,idetape_pipeline_stage_t *stage)
4564
 
4565
{
4566
        idetape_tape_t *tape=&(drive->tape);
4567
 
4568
#if IDETAPE_DEBUG_LOG
4569
                printk ("Reached idetape_add_stage_tail\n");
4570
#endif /* IDETAPE_DEBUG_LOG */
4571
 
4572
        stage->next=NULL;
4573
        stage->prev=tape->last_stage;
4574
        if (tape->last_stage != NULL)
4575
                tape->last_stage->next=stage;
4576
        else
4577
                tape->first_stage=tape->next_stage=stage;
4578
        tape->last_stage=stage;
4579
        if (tape->next_stage == NULL)
4580
                tape->next_stage=tape->last_stage;
4581
        tape->current_number_of_stages++;
4582
}
4583
 
4584
/*
4585
 *      idetape_remove_stage_head removes tape->first_stage from the pipeline.
4586
 *
4587
 *      Again, caller should avoid race conditions.
4588
 */
4589
 
4590
void idetape_remove_stage_head (ide_drive_t *drive)
4591
 
4592
{
4593
        idetape_tape_t *tape=&(drive->tape);
4594
        idetape_pipeline_stage_t *stage;
4595
 
4596
#if IDETAPE_DEBUG_LOG
4597
                printk ("Reached idetape_remove_stage_head\n");
4598
#endif /* IDETAPE_DEBUG_LOG */
4599
#if IDETAPE_DEBUG_BUGS
4600
        if (tape->first_stage == NULL) {
4601
                printk ("ide-tape: bug: tape->first_stage is NULL\n");
4602
                return;
4603
        }
4604
        if (tape->active_stage == tape->first_stage) {
4605
                printk ("ide-tape: bug: Trying to free our active pipeline stage\n");
4606
                return;
4607
        }
4608
#endif /* IDETAPE_DEBUG_BUGS */
4609
        stage=tape->first_stage;
4610
        tape->first_stage=stage->next;
4611
        idetape_kfree_stage (stage);
4612
        tape->current_number_of_stages--;
4613
        if (tape->first_stage == NULL) {
4614
                tape->last_stage=NULL;
4615
#if IDETAPE_DEBUG_BUGS
4616
                if (tape->next_stage != NULL)
4617
                        printk ("ide-tape: bug: tape->next_stage != NULL\n");
4618
                if (tape->current_number_of_stages)
4619
                        printk ("ide-tape: bug: current_number_of_stages should be 0 now\n");
4620
#endif /* IDETAPE_DEBUG_BUGS */
4621
        }
4622
}
4623
 
4624
/*
4625
 *      idetape_insert_pipeline_into_queue is used to start servicing the
4626
 *      pipeline stages, starting from tape->next_stage.
4627
 */
4628
 
4629
void idetape_insert_pipeline_into_queue (ide_drive_t *drive)
4630
 
4631
{
4632
        idetape_tape_t *tape=&(drive->tape);
4633
 
4634
        if (tape->next_stage == NULL)
4635
                return;
4636
 
4637
        if (tape->active_data_request == NULL) {
4638
                idetape_active_next_stage (drive);
4639
                (void) (ide_do_drive_cmd (drive,tape->active_data_request,ide_end));
4640
                return;
4641
        }
4642
}
4643
 
4644
/*
4645
 *      idetape_active_next_stage will declare the next stage as "active".
4646
 */
4647
 
4648
void idetape_active_next_stage (ide_drive_t *drive)
4649
 
4650
{
4651
        idetape_tape_t *tape=&(drive->tape);
4652
        idetape_pipeline_stage_t *stage=tape->next_stage;
4653
        struct request *rq=&(stage->rq);
4654
 
4655
#if IDETAPE_DEBUG_LOG
4656
        printk ("Reached idetape_active_next_stage\n");
4657
#endif /* IDETAPE_DEBUG_LOG */
4658
#if IDETAPE_DEBUG_BUGS
4659
        if (stage == NULL) {
4660
                printk ("ide-tape: bug: Trying to activate a non existing stage\n");
4661
                return;
4662
        }
4663
#endif /* IDETAPE_DEBUG_BUGS */ 
4664
        if (rq->cmd == IDETAPE_WRITE_REQUEST)
4665
                idetape_copy_buffer_from_stage (stage,tape->data_buffer);
4666
 
4667
        rq->buffer=tape->data_buffer;
4668
        tape->active_data_request=rq;
4669
        tape->active_stage=stage;
4670
        tape->next_stage=stage->next;
4671
}

powered by: WebSVN 2.1.0

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