ide-tape: dump gcw fields on error in idetape_identify_device()
[sfrench/cifs-2.6.git] / drivers / ide / ide-tape.c
1 /*
2  * IDE ATAPI streaming tape driver.
3  *
4  * Copyright (C) 1995-1999  Gadi Oxman <gadio@netvision.net.il>
5  * Copyright (C) 2003-2005  Bartlomiej Zolnierkiewicz
6  *
7  * This driver was constructed as a student project in the software laboratory
8  * of the faculty of electrical engineering in the Technion - Israel's
9  * Institute Of Technology, with the guide of Avner Lottem and Dr. Ilana David.
10  *
11  * It is hereby placed under the terms of the GNU general public license.
12  * (See linux/COPYING).
13  *
14  * For a historical changelog see
15  * Documentation/ide/ChangeLog.ide-tape.1995-2002
16  */
17
18 #define IDETAPE_VERSION "1.19"
19
20 #include <linux/module.h>
21 #include <linux/types.h>
22 #include <linux/string.h>
23 #include <linux/kernel.h>
24 #include <linux/delay.h>
25 #include <linux/timer.h>
26 #include <linux/mm.h>
27 #include <linux/interrupt.h>
28 #include <linux/jiffies.h>
29 #include <linux/major.h>
30 #include <linux/errno.h>
31 #include <linux/genhd.h>
32 #include <linux/slab.h>
33 #include <linux/pci.h>
34 #include <linux/ide.h>
35 #include <linux/smp_lock.h>
36 #include <linux/completion.h>
37 #include <linux/bitops.h>
38 #include <linux/mutex.h>
39
40 #include <asm/byteorder.h>
41 #include <asm/irq.h>
42 #include <asm/uaccess.h>
43 #include <asm/io.h>
44 #include <asm/unaligned.h>
45
46 /*
47  * partition
48  */
49 typedef struct os_partition_s {
50         __u8    partition_num;
51         __u8    par_desc_ver;
52         __u16   wrt_pass_cntr;
53         __u32   first_frame_addr;
54         __u32   last_frame_addr;
55         __u32   eod_frame_addr;
56 } os_partition_t;
57
58 /*
59  * DAT entry
60  */
61 typedef struct os_dat_entry_s {
62         __u32   blk_sz;
63         __u16   blk_cnt;
64         __u8    flags;
65         __u8    reserved;
66 } os_dat_entry_t;
67
68 /*
69  * DAT
70  */
71 #define OS_DAT_FLAGS_DATA       (0xc)
72 #define OS_DAT_FLAGS_MARK       (0x1)
73
74 typedef struct os_dat_s {
75         __u8            dat_sz;
76         __u8            reserved1;
77         __u8            entry_cnt;
78         __u8            reserved3;
79         os_dat_entry_t  dat_list[16];
80 } os_dat_t;
81
82 #include <linux/mtio.h>
83
84 /**************************** Tunable parameters *****************************/
85
86
87 /*
88  *      Pipelined mode parameters.
89  *
90  *      We try to use the minimum number of stages which is enough to
91  *      keep the tape constantly streaming. To accomplish that, we implement
92  *      a feedback loop around the maximum number of stages:
93  *
94  *      We start from MIN maximum stages (we will not even use MIN stages
95  *      if we don't need them), increment it by RATE*(MAX-MIN)
96  *      whenever we sense that the pipeline is empty, until we reach
97  *      the optimum value or until we reach MAX.
98  *
99  *      Setting the following parameter to 0 is illegal: the pipelined mode
100  *      cannot be disabled (calculate_speeds() divides by tape->max_stages.)
101  */
102 #define IDETAPE_MIN_PIPELINE_STAGES       1
103 #define IDETAPE_MAX_PIPELINE_STAGES     400
104 #define IDETAPE_INCREASE_STAGES_RATE     20
105
106 /*
107  *      The following are used to debug the driver:
108  *
109  *      Setting IDETAPE_DEBUG_INFO to 1 will report device capabilities.
110  *      Setting IDETAPE_DEBUG_LOG to 1 will log driver flow control.
111  *      Setting IDETAPE_DEBUG_BUGS to 1 will enable self-sanity checks in
112  *      some places.
113  *
114  *      Setting them to 0 will restore normal operation mode:
115  *
116  *              1.      Disable logging normal successful operations.
117  *              2.      Disable self-sanity checks.
118  *              3.      Errors will still be logged, of course.
119  *
120  *      All the #if DEBUG code will be removed some day, when the driver
121  *      is verified to be stable enough. This will make it much more
122  *      esthetic.
123  */
124 #define IDETAPE_DEBUG_INFO              0
125 #define IDETAPE_DEBUG_LOG               0
126 #define IDETAPE_DEBUG_BUGS              1
127
128 /*
129  *      After each failed packet command we issue a request sense command
130  *      and retry the packet command IDETAPE_MAX_PC_RETRIES times.
131  *
132  *      Setting IDETAPE_MAX_PC_RETRIES to 0 will disable retries.
133  */
134 #define IDETAPE_MAX_PC_RETRIES          3
135
136 /*
137  *      With each packet command, we allocate a buffer of
138  *      IDETAPE_PC_BUFFER_SIZE bytes. This is used for several packet
139  *      commands (Not for READ/WRITE commands).
140  */
141 #define IDETAPE_PC_BUFFER_SIZE          256
142
143 /*
144  *      In various places in the driver, we need to allocate storage
145  *      for packet commands and requests, which will remain valid while
146  *      we leave the driver to wait for an interrupt or a timeout event.
147  */
148 #define IDETAPE_PC_STACK                (10 + IDETAPE_MAX_PC_RETRIES)
149
150 /*
151  * Some drives (for example, Seagate STT3401A Travan) require a very long
152  * timeout, because they don't return an interrupt or clear their busy bit
153  * until after the command completes (even retension commands).
154  */
155 #define IDETAPE_WAIT_CMD                (900*HZ)
156
157 /*
158  *      The following parameter is used to select the point in the internal
159  *      tape fifo in which we will start to refill the buffer. Decreasing
160  *      the following parameter will improve the system's latency and
161  *      interactive response, while using a high value might improve system
162  *      throughput.
163  */
164 #define IDETAPE_FIFO_THRESHOLD          2
165
166 /*
167  *      DSC polling parameters.
168  *
169  *      Polling for DSC (a single bit in the status register) is a very
170  *      important function in ide-tape. There are two cases in which we
171  *      poll for DSC:
172  *
173  *      1.      Before a read/write packet command, to ensure that we
174  *              can transfer data from/to the tape's data buffers, without
175  *              causing an actual media access. In case the tape is not
176  *              ready yet, we take out our request from the device
177  *              request queue, so that ide.c will service requests from
178  *              the other device on the same interface meanwhile.
179  *
180  *      2.      After the successful initialization of a "media access
181  *              packet command", which is a command which can take a long
182  *              time to complete (it can be several seconds or even an hour).
183  *
184  *              Again, we postpone our request in the middle to free the bus
185  *              for the other device. The polling frequency here should be
186  *              lower than the read/write frequency since those media access
187  *              commands are slow. We start from a "fast" frequency -
188  *              IDETAPE_DSC_MA_FAST (one second), and if we don't receive DSC
189  *              after IDETAPE_DSC_MA_THRESHOLD (5 minutes), we switch it to a
190  *              lower frequency - IDETAPE_DSC_MA_SLOW (1 minute).
191  *
192  *      We also set a timeout for the timer, in case something goes wrong.
193  *      The timeout should be longer then the maximum execution time of a
194  *      tape operation.
195  */
196  
197 /*
198  *      DSC timings.
199  */
200 #define IDETAPE_DSC_RW_MIN              5*HZ/100        /* 50 msec */
201 #define IDETAPE_DSC_RW_MAX              40*HZ/100       /* 400 msec */
202 #define IDETAPE_DSC_RW_TIMEOUT          2*60*HZ         /* 2 minutes */
203 #define IDETAPE_DSC_MA_FAST             2*HZ            /* 2 seconds */
204 #define IDETAPE_DSC_MA_THRESHOLD        5*60*HZ         /* 5 minutes */
205 #define IDETAPE_DSC_MA_SLOW             30*HZ           /* 30 seconds */
206 #define IDETAPE_DSC_MA_TIMEOUT          2*60*60*HZ      /* 2 hours */
207
208 /*************************** End of tunable parameters ***********************/
209
210 /*
211  *      Read/Write error simulation
212  */
213 #define SIMULATE_ERRORS                 0
214
215 /*
216  *      For general magnetic tape device compatibility.
217  */
218 typedef enum {
219         idetape_direction_none,
220         idetape_direction_read,
221         idetape_direction_write
222 } idetape_chrdev_direction_t;
223
224 struct idetape_bh {
225         u32 b_size;
226         atomic_t b_count;
227         struct idetape_bh *b_reqnext;
228         char *b_data;
229 };
230
231 /*
232  *      Our view of a packet command.
233  */
234 typedef struct idetape_packet_command_s {
235         u8 c[12];                               /* Actual packet bytes */
236         int retries;                            /* On each retry, we increment retries */
237         int error;                              /* Error code */
238         int request_transfer;                   /* Bytes to transfer */
239         int actually_transferred;               /* Bytes actually transferred */
240         int buffer_size;                        /* Size of our data buffer */
241         struct idetape_bh *bh;
242         char *b_data;
243         int b_count;
244         u8 *buffer;                             /* Data buffer */
245         u8 *current_position;                   /* Pointer into the above buffer */
246         ide_startstop_t (*callback) (ide_drive_t *);    /* Called when this packet command is completed */
247         u8 pc_buffer[IDETAPE_PC_BUFFER_SIZE];   /* Temporary buffer */
248         unsigned long flags;                    /* Status/Action bit flags: long for set_bit */
249 } idetape_pc_t;
250
251 /*
252  *      Packet command flag bits.
253  */
254 /* Set when an error is considered normal - We won't retry */
255 #define PC_ABORT                        0
256 /* 1 When polling for DSC on a media access command */
257 #define PC_WAIT_FOR_DSC                 1
258 /* 1 when we prefer to use DMA if possible */
259 #define PC_DMA_RECOMMENDED              2
260 /* 1 while DMA in progress */
261 #define PC_DMA_IN_PROGRESS              3
262 /* 1 when encountered problem during DMA */
263 #define PC_DMA_ERROR                    4
264 /* Data direction */
265 #define PC_WRITING                      5
266
267 /*
268  *      Capabilities and Mechanical Status Page
269  */
270 typedef struct {
271         unsigned        page_code       :6;     /* Page code - Should be 0x2a */
272         __u8            reserved0_6     :1;
273         __u8            ps              :1;     /* parameters saveable */
274         __u8            page_length;            /* Page Length - Should be 0x12 */
275         __u8            reserved2, reserved3;
276         unsigned        ro              :1;     /* Read Only Mode */
277         unsigned        reserved4_1234  :4;
278         unsigned        sprev           :1;     /* Supports SPACE in the reverse direction */
279         unsigned        reserved4_67    :2;
280         unsigned        reserved5_012   :3;
281         unsigned        efmt            :1;     /* Supports ERASE command initiated formatting */
282         unsigned        reserved5_4     :1;
283         unsigned        qfa             :1;     /* Supports the QFA two partition formats */
284         unsigned        reserved5_67    :2;
285         unsigned        lock            :1;     /* Supports locking the volume */
286         unsigned        locked          :1;     /* The volume is locked */
287         unsigned        prevent         :1;     /* The device defaults in the prevent state after power up */   
288         unsigned        eject           :1;     /* The device can eject the volume */
289         __u8            disconnect      :1;     /* The device can break request > ctl */        
290         __u8            reserved6_5     :1;
291         unsigned        ecc             :1;     /* Supports error correction */
292         unsigned        cmprs           :1;     /* Supports data compression */
293         unsigned        reserved7_0     :1;
294         unsigned        blk512          :1;     /* Supports 512 bytes block size */
295         unsigned        blk1024         :1;     /* Supports 1024 bytes block size */
296         unsigned        reserved7_3_6   :4;
297         unsigned        blk32768        :1;     /* slowb - the device restricts the byte count for PIO */
298                                                 /* transfers for slow buffer memory ??? */
299                                                 /* Also 32768 block size in some cases */
300         __u16           max_speed;              /* Maximum speed supported in KBps */
301         __u8            reserved10, reserved11;
302         __u16           ctl;                    /* Continuous Transfer Limit in blocks */
303         __u16           speed;                  /* Current Speed, in KBps */
304         __u16           buffer_size;            /* Buffer Size, in 512 bytes */
305         __u8            reserved18, reserved19;
306 } idetape_capabilities_page_t;
307
308 /*
309  *      Block Size Page
310  */
311 typedef struct {
312         unsigned        page_code       :6;     /* Page code - Should be 0x30 */
313         unsigned        reserved1_6     :1;
314         unsigned        ps              :1;
315         __u8            page_length;            /* Page Length - Should be 2 */
316         __u8            reserved2;
317         unsigned        play32          :1;
318         unsigned        play32_5        :1;
319         unsigned        reserved2_23    :2;
320         unsigned        record32        :1;
321         unsigned        record32_5      :1;
322         unsigned        reserved2_6     :1;
323         unsigned        one             :1;
324 } idetape_block_size_page_t;
325
326 /*
327  *      A pipeline stage.
328  */
329 typedef struct idetape_stage_s {
330         struct request rq;                      /* The corresponding request */
331         struct idetape_bh *bh;                  /* The data buffers */
332         struct idetape_stage_s *next;           /* Pointer to the next stage */
333 } idetape_stage_t;
334
335 /*
336  *      Most of our global data which we need to save even as we leave the
337  *      driver due to an interrupt or a timer event is stored in a variable
338  *      of type idetape_tape_t, defined below.
339  */
340 typedef struct ide_tape_obj {
341         ide_drive_t     *drive;
342         ide_driver_t    *driver;
343         struct gendisk  *disk;
344         struct kref     kref;
345
346         /*
347          *      Since a typical character device operation requires more
348          *      than one packet command, we provide here enough memory
349          *      for the maximum of interconnected packet commands.
350          *      The packet commands are stored in the circular array pc_stack.
351          *      pc_stack_index points to the last used entry, and warps around
352          *      to the start when we get to the last array entry.
353          *
354          *      pc points to the current processed packet command.
355          *
356          *      failed_pc points to the last failed packet command, or contains
357          *      NULL if we do not need to retry any packet command. This is
358          *      required since an additional packet command is needed before the
359          *      retry, to get detailed information on what went wrong.
360          */
361         /* Current packet command */
362         idetape_pc_t *pc;
363         /* Last failed packet command */
364         idetape_pc_t *failed_pc;
365         /* Packet command stack */
366         idetape_pc_t pc_stack[IDETAPE_PC_STACK];
367         /* Next free packet command storage space */
368         int pc_stack_index;
369         struct request rq_stack[IDETAPE_PC_STACK];
370         /* We implement a circular array */
371         int rq_stack_index;
372
373         /*
374          *      DSC polling variables.
375          *
376          *      While polling for DSC we use postponed_rq to postpone the
377          *      current request so that ide.c will be able to service
378          *      pending requests on the other device. Note that at most
379          *      we will have only one DSC (usually data transfer) request
380          *      in the device request queue. Additional requests can be
381          *      queued in our internal pipeline, but they will be visible
382          *      to ide.c only one at a time.
383          */
384         struct request *postponed_rq;
385         /* The time in which we started polling for DSC */
386         unsigned long dsc_polling_start;
387         /* Timer used to poll for dsc */
388         struct timer_list dsc_timer;
389         /* Read/Write dsc polling frequency */
390         unsigned long best_dsc_rw_frequency;
391         /* The current polling frequency */
392         unsigned long dsc_polling_frequency;
393         /* Maximum waiting time */
394         unsigned long dsc_timeout;
395
396         /*
397          *      Read position information
398          */
399         u8 partition;
400         /* Current block */
401         unsigned int first_frame_position;
402         unsigned int last_frame_position;
403         unsigned int blocks_in_buffer;
404
405         /*
406          *      Last error information
407          */
408         u8 sense_key, asc, ascq;
409
410         /*
411          *      Character device operation
412          */
413         unsigned int minor;
414         /* device name */
415         char name[4];
416         /* Current character device data transfer direction */
417         idetape_chrdev_direction_t chrdev_direction;
418
419         /*
420          *      Device information
421          */
422         /* Usually 512 or 1024 bytes */
423         unsigned short tape_block_size;
424         int user_bs_factor;
425         /* Copy of the tape's Capabilities and Mechanical Page */
426         idetape_capabilities_page_t capabilities;
427
428         /*
429          *      Active data transfer request parameters.
430          *
431          *      At most, there is only one ide-tape originated data transfer
432          *      request in the device request queue. This allows ide.c to
433          *      easily service requests from the other device when we
434          *      postpone our active request. In the pipelined operation
435          *      mode, we use our internal pipeline structure to hold
436          *      more data requests.
437          *
438          *      The data buffer size is chosen based on the tape's
439          *      recommendation.
440          */
441         /* Pointer to the request which is waiting in the device request queue */
442         struct request *active_data_request;
443         /* Data buffer size (chosen based on the tape's recommendation */
444         int stage_size;
445         idetape_stage_t *merge_stage;
446         int merge_stage_size;
447         struct idetape_bh *bh;
448         char *b_data;
449         int b_count;
450         
451         /*
452          *      Pipeline parameters.
453          *
454          *      To accomplish non-pipelined mode, we simply set the following
455          *      variables to zero (or NULL, where appropriate).
456          */
457         /* Number of currently used stages */
458         int nr_stages;
459         /* Number of pending stages */
460         int nr_pending_stages;
461         /* We will not allocate more than this number of stages */
462         int max_stages, min_pipeline, max_pipeline;
463         /* The first stage which will be removed from the pipeline */
464         idetape_stage_t *first_stage;
465         /* The currently active stage */
466         idetape_stage_t *active_stage;
467         /* Will be serviced after the currently active request */
468         idetape_stage_t *next_stage;
469         /* New requests will be added to the pipeline here */
470         idetape_stage_t *last_stage;
471         /* Optional free stage which we can use */
472         idetape_stage_t *cache_stage;
473         int pages_per_stage;
474         /* Wasted space in each stage */
475         int excess_bh_size;
476
477         /* Status/Action flags: long for set_bit */
478         unsigned long flags;
479         /* protects the ide-tape queue */
480         spinlock_t spinlock;
481
482         /*
483          * Measures average tape speed
484          */
485         unsigned long avg_time;
486         int avg_size;
487         int avg_speed;
488
489         char vendor_id[10];
490         char product_id[18];
491         char firmware_revision[6];
492         int firmware_revision_num;
493
494         /* the door is currently locked */
495         int door_locked;
496         /* the tape hardware is write protected */
497         char drv_write_prot;
498         /* the tape is write protected (hardware or opened as read-only) */
499         char write_prot;
500
501         /*
502          * Limit the number of times a request can
503          * be postponed, to avoid an infinite postpone
504          * deadlock.
505          */
506         /* request postpone count limit */
507         int postpone_cnt;
508
509         /*
510          * Measures number of frames:
511          *
512          * 1. written/read to/from the driver pipeline (pipeline_head).
513          * 2. written/read to/from the tape buffers (idetape_bh).
514          * 3. written/read by the tape to/from the media (tape_head).
515          */
516         int pipeline_head;
517         int buffer_head;
518         int tape_head;
519         int last_tape_head;
520
521         /*
522          * Speed control at the tape buffers input/output
523          */
524         unsigned long insert_time;
525         int insert_size;
526         int insert_speed;
527         int max_insert_speed;
528         int measure_insert_time;
529
530         /*
531          * Measure tape still time, in milliseconds
532          */
533         unsigned long tape_still_time_begin;
534         int tape_still_time;
535
536         /*
537          * Speed regulation negative feedback loop
538          */
539         int speed_control;
540         int pipeline_head_speed;
541         int controlled_pipeline_head_speed;
542         int uncontrolled_pipeline_head_speed;
543         int controlled_last_pipeline_head;
544         int uncontrolled_last_pipeline_head;
545         unsigned long uncontrolled_pipeline_head_time;
546         unsigned long controlled_pipeline_head_time;
547         int controlled_previous_pipeline_head;
548         int uncontrolled_previous_pipeline_head;
549         unsigned long controlled_previous_head_time;
550         unsigned long uncontrolled_previous_head_time;
551         int restart_speed_control_req;
552
553         /*
554          * Debug_level determines amount of debugging output;
555          * can be changed using /proc/ide/hdx/settings
556          * 0 : almost no debugging output
557          * 1 : 0+output errors only
558          * 2 : 1+output all sensekey/asc
559          * 3 : 2+follow all chrdev related procedures
560          * 4 : 3+follow all procedures
561          * 5 : 4+include pc_stack rq_stack info
562          * 6 : 5+USE_COUNT updates
563          */
564          int debug_level; 
565 } idetape_tape_t;
566
567 static DEFINE_MUTEX(idetape_ref_mutex);
568
569 static struct class *idetape_sysfs_class;
570
571 #define to_ide_tape(obj) container_of(obj, struct ide_tape_obj, kref)
572
573 #define ide_tape_g(disk) \
574         container_of((disk)->private_data, struct ide_tape_obj, driver)
575
576 static struct ide_tape_obj *ide_tape_get(struct gendisk *disk)
577 {
578         struct ide_tape_obj *tape = NULL;
579
580         mutex_lock(&idetape_ref_mutex);
581         tape = ide_tape_g(disk);
582         if (tape)
583                 kref_get(&tape->kref);
584         mutex_unlock(&idetape_ref_mutex);
585         return tape;
586 }
587
588 static void ide_tape_release(struct kref *);
589
590 static void ide_tape_put(struct ide_tape_obj *tape)
591 {
592         mutex_lock(&idetape_ref_mutex);
593         kref_put(&tape->kref, ide_tape_release);
594         mutex_unlock(&idetape_ref_mutex);
595 }
596
597 /*
598  *      Tape door status
599  */
600 #define DOOR_UNLOCKED                   0
601 #define DOOR_LOCKED                     1
602 #define DOOR_EXPLICITLY_LOCKED          2
603
604 /*
605  *      Tape flag bits values.
606  */
607 #define IDETAPE_IGNORE_DSC              0
608 #define IDETAPE_ADDRESS_VALID           1       /* 0 When the tape position is unknown */
609 #define IDETAPE_BUSY                    2       /* Device already opened */
610 #define IDETAPE_PIPELINE_ERROR          3       /* Error detected in a pipeline stage */
611 #define IDETAPE_DETECT_BS               4       /* Attempt to auto-detect the current user block size */
612 #define IDETAPE_FILEMARK                5       /* Currently on a filemark */
613 #define IDETAPE_DRQ_INTERRUPT           6       /* DRQ interrupt device */
614 #define IDETAPE_READ_ERROR              7
615 #define IDETAPE_PIPELINE_ACTIVE         8       /* pipeline active */
616 /* 0 = no tape is loaded, so we don't rewind after ejecting */
617 #define IDETAPE_MEDIUM_PRESENT          9
618
619 /*
620  *      Supported ATAPI tape drives packet commands
621  */
622 #define IDETAPE_TEST_UNIT_READY_CMD     0x00
623 #define IDETAPE_REWIND_CMD              0x01
624 #define IDETAPE_REQUEST_SENSE_CMD       0x03
625 #define IDETAPE_READ_CMD                0x08
626 #define IDETAPE_WRITE_CMD               0x0a
627 #define IDETAPE_WRITE_FILEMARK_CMD      0x10
628 #define IDETAPE_SPACE_CMD               0x11
629 #define IDETAPE_INQUIRY_CMD             0x12
630 #define IDETAPE_ERASE_CMD               0x19
631 #define IDETAPE_MODE_SENSE_CMD          0x1a
632 #define IDETAPE_MODE_SELECT_CMD         0x15
633 #define IDETAPE_LOAD_UNLOAD_CMD         0x1b
634 #define IDETAPE_PREVENT_CMD             0x1e
635 #define IDETAPE_LOCATE_CMD              0x2b
636 #define IDETAPE_READ_POSITION_CMD       0x34
637 #define IDETAPE_READ_BUFFER_CMD         0x3c
638 #define IDETAPE_SET_SPEED_CMD           0xbb
639
640 /*
641  *      Some defines for the READ BUFFER command
642  */
643 #define IDETAPE_RETRIEVE_FAULTY_BLOCK   6
644
645 /*
646  *      Some defines for the SPACE command
647  */
648 #define IDETAPE_SPACE_OVER_FILEMARK     1
649 #define IDETAPE_SPACE_TO_EOD            3
650
651 /*
652  *      Some defines for the LOAD UNLOAD command
653  */
654 #define IDETAPE_LU_LOAD_MASK            1
655 #define IDETAPE_LU_RETENSION_MASK       2
656 #define IDETAPE_LU_EOT_MASK             4
657
658 /*
659  *      Special requests for our block device strategy routine.
660  *
661  *      In order to service a character device command, we add special
662  *      requests to the tail of our block device request queue and wait
663  *      for their completion.
664  */
665
666 enum {
667         REQ_IDETAPE_PC1         = (1 << 0), /* packet command (first stage) */
668         REQ_IDETAPE_PC2         = (1 << 1), /* packet command (second stage) */
669         REQ_IDETAPE_READ        = (1 << 2),
670         REQ_IDETAPE_WRITE       = (1 << 3),
671         REQ_IDETAPE_READ_BUFFER = (1 << 4),
672 };
673
674 /*
675  *      Error codes which are returned in rq->errors to the higher part
676  *      of the driver.
677  */
678 #define IDETAPE_ERROR_GENERAL           101
679 #define IDETAPE_ERROR_FILEMARK          102
680 #define IDETAPE_ERROR_EOD               103
681
682 /*
683  *      The following is used to format the general configuration word of
684  *      the ATAPI IDENTIFY DEVICE command.
685  */
686 struct idetape_id_gcw { 
687         unsigned packet_size            :2;     /* Packet Size */
688         unsigned reserved234            :3;     /* Reserved */
689         unsigned drq_type               :2;     /* Command packet DRQ type */
690         unsigned removable              :1;     /* Removable media */
691         unsigned device_type            :5;     /* Device type */
692         unsigned reserved13             :1;     /* Reserved */
693         unsigned protocol               :2;     /* Protocol type */
694 };
695
696 /*
697  *      INQUIRY packet command - Data Format (From Table 6-8 of QIC-157C)
698  */
699 typedef struct {
700         unsigned        device_type     :5;     /* Peripheral Device Type */
701         unsigned        reserved0_765   :3;     /* Peripheral Qualifier - Reserved */
702         unsigned        reserved1_6t0   :7;     /* Reserved */
703         unsigned        rmb             :1;     /* Removable Medium Bit */
704         unsigned        ansi_version    :3;     /* ANSI Version */
705         unsigned        ecma_version    :3;     /* ECMA Version */
706         unsigned        iso_version     :2;     /* ISO Version */
707         unsigned        response_format :4;     /* Response Data Format */
708         unsigned        reserved3_45    :2;     /* Reserved */
709         unsigned        reserved3_6     :1;     /* TrmIOP - Reserved */
710         unsigned        reserved3_7     :1;     /* AENC - Reserved */
711         __u8            additional_length;      /* Additional Length (total_length-4) */
712         __u8            rsv5, rsv6, rsv7;       /* Reserved */
713         __u8            vendor_id[8];           /* Vendor Identification */
714         __u8            product_id[16];         /* Product Identification */
715         __u8            revision_level[4];      /* Revision Level */
716         __u8            vendor_specific[20];    /* Vendor Specific - Optional */
717         __u8            reserved56t95[40];      /* Reserved - Optional */
718                                                 /* Additional information may be returned */
719 } idetape_inquiry_result_t;
720
721 /*
722  *      READ POSITION packet command - Data Format (From Table 6-57)
723  */
724 typedef struct {
725         unsigned        reserved0_10    :2;     /* Reserved */
726         unsigned        bpu             :1;     /* Block Position Unknown */    
727         unsigned        reserved0_543   :3;     /* Reserved */
728         unsigned        eop             :1;     /* End Of Partition */
729         unsigned        bop             :1;     /* Beginning Of Partition */
730         u8              partition;              /* Partition Number */
731         u8              reserved2, reserved3;   /* Reserved */
732         u32             first_block;            /* First Block Location */
733         u32             last_block;             /* Last Block Location (Optional) */
734         u8              reserved12;             /* Reserved */
735         u8              blocks_in_buffer[3];    /* Blocks In Buffer - (Optional) */
736         u32             bytes_in_buffer;        /* Bytes In Buffer (Optional) */
737 } idetape_read_position_result_t;
738
739 /*
740  *      Follows structures which are related to the SELECT SENSE / MODE SENSE
741  *      packet commands. Those packet commands are still not supported
742  *      by ide-tape.
743  */
744 #define IDETAPE_BLOCK_DESCRIPTOR        0
745 #define IDETAPE_CAPABILITIES_PAGE       0x2a
746 #define IDETAPE_PARAMTR_PAGE            0x2b   /* Onstream DI-x0 only */
747 #define IDETAPE_BLOCK_SIZE_PAGE         0x30
748 #define IDETAPE_BUFFER_FILLING_PAGE     0x33
749
750 /*
751  *      Mode Parameter Block Descriptor the MODE SENSE packet command
752  *
753  *      Support for block descriptors is optional.
754  */
755 typedef struct {
756         __u8            density_code;           /* Medium density code */
757         __u8            blocks[3];              /* Number of blocks */
758         __u8            reserved4;              /* Reserved */
759         __u8            length[3];              /* Block Length */
760 } idetape_parameter_block_descriptor_t;
761
762 /*
763  *      The Data Compression Page, as returned by the MODE SENSE packet command.
764  */
765 typedef struct {
766         unsigned        page_code       :6;     /* Page Code - Should be 0xf */
767         unsigned        reserved0       :1;     /* Reserved */
768         unsigned        ps              :1;
769         __u8            page_length;            /* Page Length - Should be 14 */
770         unsigned        reserved2       :6;     /* Reserved */
771         unsigned        dcc             :1;     /* Data Compression Capable */
772         unsigned        dce             :1;     /* Data Compression Enable */
773         unsigned        reserved3       :5;     /* Reserved */
774         unsigned        red             :2;     /* Report Exception on Decompression */
775         unsigned        dde             :1;     /* Data Decompression Enable */
776         __u32           ca;                     /* Compression Algorithm */
777         __u32           da;                     /* Decompression Algorithm */
778         __u8            reserved[4];            /* Reserved */
779 } idetape_data_compression_page_t;
780
781 /*
782  *      The Medium Partition Page, as returned by the MODE SENSE packet command.
783  */
784 typedef struct {
785         unsigned        page_code       :6;     /* Page Code - Should be 0x11 */
786         unsigned        reserved1_6     :1;     /* Reserved */
787         unsigned        ps              :1;
788         __u8            page_length;            /* Page Length - Should be 6 */
789         __u8            map;                    /* Maximum Additional Partitions - Should be 0 */
790         __u8            apd;                    /* Additional Partitions Defined - Should be 0 */
791         unsigned        reserved4_012   :3;     /* Reserved */
792         unsigned        psum            :2;     /* Should be 0 */
793         unsigned        idp             :1;     /* Should be 0 */
794         unsigned        sdp             :1;     /* Should be 0 */
795         unsigned        fdp             :1;     /* Fixed Data Partitions */
796         __u8            mfr;                    /* Medium Format Recognition */
797         __u8            reserved[2];            /* Reserved */
798 } idetape_medium_partition_page_t;
799
800 /*
801  *      Run time configurable parameters.
802  */
803 typedef struct {
804         int     dsc_rw_frequency;
805         int     dsc_media_access_frequency;
806         int     nr_stages;
807 } idetape_config_t;
808
809 /*
810  *      The variables below are used for the character device interface.
811  *      Additional state variables are defined in our ide_drive_t structure.
812  */
813 static struct ide_tape_obj * idetape_devs[MAX_HWIFS * MAX_DRIVES];
814
815 #define ide_tape_f(file) ((file)->private_data)
816
817 static struct ide_tape_obj *ide_tape_chrdev_get(unsigned int i)
818 {
819         struct ide_tape_obj *tape = NULL;
820
821         mutex_lock(&idetape_ref_mutex);
822         tape = idetape_devs[i];
823         if (tape)
824                 kref_get(&tape->kref);
825         mutex_unlock(&idetape_ref_mutex);
826         return tape;
827 }
828
829 /*
830  *      Function declarations
831  *
832  */
833 static int idetape_chrdev_release (struct inode *inode, struct file *filp);
834 static void idetape_write_release (ide_drive_t *drive, unsigned int minor);
835
836 /*
837  * Too bad. The drive wants to send us data which we are not ready to accept.
838  * Just throw it away.
839  */
840 static void idetape_discard_data (ide_drive_t *drive, unsigned int bcount)
841 {
842         while (bcount--)
843                 (void) HWIF(drive)->INB(IDE_DATA_REG);
844 }
845
846 static void idetape_input_buffers (ide_drive_t *drive, idetape_pc_t *pc, unsigned int bcount)
847 {
848         struct idetape_bh *bh = pc->bh;
849         int count;
850
851         while (bcount) {
852 #if IDETAPE_DEBUG_BUGS
853                 if (bh == NULL) {
854                         printk(KERN_ERR "ide-tape: bh == NULL in "
855                                 "idetape_input_buffers\n");
856                         idetape_discard_data(drive, bcount);
857                         return;
858                 }
859 #endif /* IDETAPE_DEBUG_BUGS */
860                 count = min((unsigned int)(bh->b_size - atomic_read(&bh->b_count)), bcount);
861                 HWIF(drive)->atapi_input_bytes(drive, bh->b_data + atomic_read(&bh->b_count), count);
862                 bcount -= count;
863                 atomic_add(count, &bh->b_count);
864                 if (atomic_read(&bh->b_count) == bh->b_size) {
865                         bh = bh->b_reqnext;
866                         if (bh)
867                                 atomic_set(&bh->b_count, 0);
868                 }
869         }
870         pc->bh = bh;
871 }
872
873 static void idetape_output_buffers (ide_drive_t *drive, idetape_pc_t *pc, unsigned int bcount)
874 {
875         struct idetape_bh *bh = pc->bh;
876         int count;
877
878         while (bcount) {
879 #if IDETAPE_DEBUG_BUGS
880                 if (bh == NULL) {
881                         printk(KERN_ERR "ide-tape: bh == NULL in "
882                                 "idetape_output_buffers\n");
883                         return;
884                 }
885 #endif /* IDETAPE_DEBUG_BUGS */
886                 count = min((unsigned int)pc->b_count, (unsigned int)bcount);
887                 HWIF(drive)->atapi_output_bytes(drive, pc->b_data, count);
888                 bcount -= count;
889                 pc->b_data += count;
890                 pc->b_count -= count;
891                 if (!pc->b_count) {
892                         pc->bh = bh = bh->b_reqnext;
893                         if (bh) {
894                                 pc->b_data = bh->b_data;
895                                 pc->b_count = atomic_read(&bh->b_count);
896                         }
897                 }
898         }
899 }
900
901 static void idetape_update_buffers (idetape_pc_t *pc)
902 {
903         struct idetape_bh *bh = pc->bh;
904         int count;
905         unsigned int bcount = pc->actually_transferred;
906
907         if (test_bit(PC_WRITING, &pc->flags))
908                 return;
909         while (bcount) {
910 #if IDETAPE_DEBUG_BUGS
911                 if (bh == NULL) {
912                         printk(KERN_ERR "ide-tape: bh == NULL in "
913                                 "idetape_update_buffers\n");
914                         return;
915                 }
916 #endif /* IDETAPE_DEBUG_BUGS */
917                 count = min((unsigned int)bh->b_size, (unsigned int)bcount);
918                 atomic_set(&bh->b_count, count);
919                 if (atomic_read(&bh->b_count) == bh->b_size)
920                         bh = bh->b_reqnext;
921                 bcount -= count;
922         }
923         pc->bh = bh;
924 }
925
926 /*
927  *      idetape_next_pc_storage returns a pointer to a place in which we can
928  *      safely store a packet command, even though we intend to leave the
929  *      driver. A storage space for a maximum of IDETAPE_PC_STACK packet
930  *      commands is allocated at initialization time.
931  */
932 static idetape_pc_t *idetape_next_pc_storage (ide_drive_t *drive)
933 {
934         idetape_tape_t *tape = drive->driver_data;
935
936 #if IDETAPE_DEBUG_LOG
937         if (tape->debug_level >= 5)
938                 printk(KERN_INFO "ide-tape: pc_stack_index=%d\n",
939                         tape->pc_stack_index);
940 #endif /* IDETAPE_DEBUG_LOG */
941         if (tape->pc_stack_index == IDETAPE_PC_STACK)
942                 tape->pc_stack_index=0;
943         return (&tape->pc_stack[tape->pc_stack_index++]);
944 }
945
946 /*
947  *      idetape_next_rq_storage is used along with idetape_next_pc_storage.
948  *      Since we queue packet commands in the request queue, we need to
949  *      allocate a request, along with the allocation of a packet command.
950  */
951  
952 /**************************************************************
953  *                                                            *
954  *  This should get fixed to use kmalloc(.., GFP_ATOMIC)      *
955  *  followed later on by kfree().   -ml                       *
956  *                                                            *
957  **************************************************************/
958  
959 static struct request *idetape_next_rq_storage (ide_drive_t *drive)
960 {
961         idetape_tape_t *tape = drive->driver_data;
962
963 #if IDETAPE_DEBUG_LOG
964         if (tape->debug_level >= 5)
965                 printk(KERN_INFO "ide-tape: rq_stack_index=%d\n",
966                         tape->rq_stack_index);
967 #endif /* IDETAPE_DEBUG_LOG */
968         if (tape->rq_stack_index == IDETAPE_PC_STACK)
969                 tape->rq_stack_index=0;
970         return (&tape->rq_stack[tape->rq_stack_index++]);
971 }
972
973 /*
974  *      idetape_init_pc initializes a packet command.
975  */
976 static void idetape_init_pc (idetape_pc_t *pc)
977 {
978         memset(pc->c, 0, 12);
979         pc->retries = 0;
980         pc->flags = 0;
981         pc->request_transfer = 0;
982         pc->buffer = pc->pc_buffer;
983         pc->buffer_size = IDETAPE_PC_BUFFER_SIZE;
984         pc->bh = NULL;
985         pc->b_data = NULL;
986 }
987
988 /*
989  * called on each failed packet command retry to analyze the request sense. We
990  * currently do not utilize this information.
991  */
992 static void idetape_analyze_error(ide_drive_t *drive, u8 *sense)
993 {
994         idetape_tape_t *tape = drive->driver_data;
995         idetape_pc_t *pc = tape->failed_pc;
996
997         tape->sense_key = sense[2] & 0xF;
998         tape->asc       = sense[12];
999         tape->ascq      = sense[13];
1000 #if IDETAPE_DEBUG_LOG
1001         /*
1002          * Without debugging, we only log an error if we decided to give up
1003          * retrying.
1004          */
1005         if (tape->debug_level >= 1)
1006                 printk(KERN_INFO "ide-tape: pc = %x, sense key = %x, "
1007                         "asc = %x, ascq = %x\n",
1008                         pc->c[0], tape->sense_key,
1009                         tape->asc, tape->ascq);
1010 #endif /* IDETAPE_DEBUG_LOG */
1011
1012         /* Correct pc->actually_transferred by asking the tape.  */
1013         if (test_bit(PC_DMA_ERROR, &pc->flags)) {
1014                 pc->actually_transferred = pc->request_transfer -
1015                         tape->tape_block_size *
1016                         ntohl(get_unaligned((u32 *)&sense[3]));
1017                 idetape_update_buffers(pc);
1018         }
1019
1020         /*
1021          * If error was the result of a zero-length read or write command,
1022          * with sense key=5, asc=0x22, ascq=0, let it slide.  Some drives
1023          * (i.e. Seagate STT3401A Travan) don't support 0-length read/writes.
1024          */
1025         if ((pc->c[0] == IDETAPE_READ_CMD || pc->c[0] == IDETAPE_WRITE_CMD)
1026             /* length == 0 */
1027             && pc->c[4] == 0 && pc->c[3] == 0 && pc->c[2] == 0) {
1028                 if (tape->sense_key == 5) {
1029                         /* don't report an error, everything's ok */
1030                         pc->error = 0;
1031                         /* don't retry read/write */
1032                         set_bit(PC_ABORT, &pc->flags);
1033                 }
1034         }
1035         if (pc->c[0] == IDETAPE_READ_CMD && (sense[2] & 0x80)) {
1036                 pc->error = IDETAPE_ERROR_FILEMARK;
1037                 set_bit(PC_ABORT, &pc->flags);
1038         }
1039         if (pc->c[0] == IDETAPE_WRITE_CMD) {
1040                 if ((sense[2] & 0x40) || (tape->sense_key == 0xd
1041                      && tape->asc == 0x0 && tape->ascq == 0x2)) {
1042                         pc->error = IDETAPE_ERROR_EOD;
1043                         set_bit(PC_ABORT, &pc->flags);
1044                 }
1045         }
1046         if (pc->c[0] == IDETAPE_READ_CMD || pc->c[0] == IDETAPE_WRITE_CMD) {
1047                 if (tape->sense_key == 8) {
1048                         pc->error = IDETAPE_ERROR_EOD;
1049                         set_bit(PC_ABORT, &pc->flags);
1050                 }
1051                 if (!test_bit(PC_ABORT, &pc->flags) &&
1052                     pc->actually_transferred)
1053                         pc->retries = IDETAPE_MAX_PC_RETRIES + 1;
1054         }
1055 }
1056
1057 /*
1058  * idetape_active_next_stage will declare the next stage as "active".
1059  */
1060 static void idetape_active_next_stage (ide_drive_t *drive)
1061 {
1062         idetape_tape_t *tape = drive->driver_data;
1063         idetape_stage_t *stage = tape->next_stage;
1064         struct request *rq = &stage->rq;
1065
1066 #if IDETAPE_DEBUG_LOG
1067         if (tape->debug_level >= 4)
1068                 printk(KERN_INFO "ide-tape: Reached idetape_active_next_stage\n");
1069 #endif /* IDETAPE_DEBUG_LOG */
1070 #if IDETAPE_DEBUG_BUGS
1071         if (stage == NULL) {
1072                 printk(KERN_ERR "ide-tape: bug: Trying to activate a non existing stage\n");
1073                 return;
1074         }
1075 #endif /* IDETAPE_DEBUG_BUGS */ 
1076
1077         rq->rq_disk = tape->disk;
1078         rq->buffer = NULL;
1079         rq->special = (void *)stage->bh;
1080         tape->active_data_request = rq;
1081         tape->active_stage = stage;
1082         tape->next_stage = stage->next;
1083 }
1084
1085 /*
1086  *      idetape_increase_max_pipeline_stages is a part of the feedback
1087  *      loop which tries to find the optimum number of stages. In the
1088  *      feedback loop, we are starting from a minimum maximum number of
1089  *      stages, and if we sense that the pipeline is empty, we try to
1090  *      increase it, until we reach the user compile time memory limit.
1091  */
1092 static void idetape_increase_max_pipeline_stages (ide_drive_t *drive)
1093 {
1094         idetape_tape_t *tape = drive->driver_data;
1095         int increase = (tape->max_pipeline - tape->min_pipeline) / 10;
1096         
1097 #if IDETAPE_DEBUG_LOG
1098         if (tape->debug_level >= 4)
1099                 printk (KERN_INFO "ide-tape: Reached idetape_increase_max_pipeline_stages\n");
1100 #endif /* IDETAPE_DEBUG_LOG */
1101
1102         tape->max_stages += max(increase, 1);
1103         tape->max_stages = max(tape->max_stages, tape->min_pipeline);
1104         tape->max_stages = min(tape->max_stages, tape->max_pipeline);
1105 }
1106
1107 /*
1108  *      idetape_kfree_stage calls kfree to completely free a stage, along with
1109  *      its related buffers.
1110  */
1111 static void __idetape_kfree_stage (idetape_stage_t *stage)
1112 {
1113         struct idetape_bh *prev_bh, *bh = stage->bh;
1114         int size;
1115
1116         while (bh != NULL) {
1117                 if (bh->b_data != NULL) {
1118                         size = (int) bh->b_size;
1119                         while (size > 0) {
1120                                 free_page((unsigned long) bh->b_data);
1121                                 size -= PAGE_SIZE;
1122                                 bh->b_data += PAGE_SIZE;
1123                         }
1124                 }
1125                 prev_bh = bh;
1126                 bh = bh->b_reqnext;
1127                 kfree(prev_bh);
1128         }
1129         kfree(stage);
1130 }
1131
1132 static void idetape_kfree_stage (idetape_tape_t *tape, idetape_stage_t *stage)
1133 {
1134         __idetape_kfree_stage(stage);
1135 }
1136
1137 /*
1138  *      idetape_remove_stage_head removes tape->first_stage from the pipeline.
1139  *      The caller should avoid race conditions.
1140  */
1141 static void idetape_remove_stage_head (ide_drive_t *drive)
1142 {
1143         idetape_tape_t *tape = drive->driver_data;
1144         idetape_stage_t *stage;
1145         
1146 #if IDETAPE_DEBUG_LOG
1147         if (tape->debug_level >= 4)
1148                 printk(KERN_INFO "ide-tape: Reached idetape_remove_stage_head\n");
1149 #endif /* IDETAPE_DEBUG_LOG */
1150 #if IDETAPE_DEBUG_BUGS
1151         if (tape->first_stage == NULL) {
1152                 printk(KERN_ERR "ide-tape: bug: tape->first_stage is NULL\n");
1153                 return;         
1154         }
1155         if (tape->active_stage == tape->first_stage) {
1156                 printk(KERN_ERR "ide-tape: bug: Trying to free our active pipeline stage\n");
1157                 return;
1158         }
1159 #endif /* IDETAPE_DEBUG_BUGS */
1160         stage = tape->first_stage;
1161         tape->first_stage = stage->next;
1162         idetape_kfree_stage(tape, stage);
1163         tape->nr_stages--;
1164         if (tape->first_stage == NULL) {
1165                 tape->last_stage = NULL;
1166 #if IDETAPE_DEBUG_BUGS
1167                 if (tape->next_stage != NULL)
1168                         printk(KERN_ERR "ide-tape: bug: tape->next_stage != NULL\n");
1169                 if (tape->nr_stages)
1170                         printk(KERN_ERR "ide-tape: bug: nr_stages should be 0 now\n");
1171 #endif /* IDETAPE_DEBUG_BUGS */
1172         }
1173 }
1174
1175 /*
1176  * This will free all the pipeline stages starting from new_last_stage->next
1177  * to the end of the list, and point tape->last_stage to new_last_stage.
1178  */
1179 static void idetape_abort_pipeline(ide_drive_t *drive,
1180                                    idetape_stage_t *new_last_stage)
1181 {
1182         idetape_tape_t *tape = drive->driver_data;
1183         idetape_stage_t *stage = new_last_stage->next;
1184         idetape_stage_t *nstage;
1185
1186 #if IDETAPE_DEBUG_LOG
1187         if (tape->debug_level >= 4)
1188                 printk(KERN_INFO "ide-tape: %s: idetape_abort_pipeline called\n", tape->name);
1189 #endif
1190         while (stage) {
1191                 nstage = stage->next;
1192                 idetape_kfree_stage(tape, stage);
1193                 --tape->nr_stages;
1194                 --tape->nr_pending_stages;
1195                 stage = nstage;
1196         }
1197         if (new_last_stage)
1198                 new_last_stage->next = NULL;
1199         tape->last_stage = new_last_stage;
1200         tape->next_stage = NULL;
1201 }
1202
1203 /*
1204  *      idetape_end_request is used to finish servicing a request, and to
1205  *      insert a pending pipeline request into the main device queue.
1206  */
1207 static int idetape_end_request(ide_drive_t *drive, int uptodate, int nr_sects)
1208 {
1209         struct request *rq = HWGROUP(drive)->rq;
1210         idetape_tape_t *tape = drive->driver_data;
1211         unsigned long flags;
1212         int error;
1213         int remove_stage = 0;
1214         idetape_stage_t *active_stage;
1215
1216 #if IDETAPE_DEBUG_LOG
1217         if (tape->debug_level >= 4)
1218         printk(KERN_INFO "ide-tape: Reached idetape_end_request\n");
1219 #endif /* IDETAPE_DEBUG_LOG */
1220
1221         switch (uptodate) {
1222                 case 0: error = IDETAPE_ERROR_GENERAL; break;
1223                 case 1: error = 0; break;
1224                 default: error = uptodate;
1225         }
1226         rq->errors = error;
1227         if (error)
1228                 tape->failed_pc = NULL;
1229
1230         if (!blk_special_request(rq)) {
1231                 ide_end_request(drive, uptodate, nr_sects);
1232                 return 0;
1233         }
1234
1235         spin_lock_irqsave(&tape->spinlock, flags);
1236
1237         /* The request was a pipelined data transfer request */
1238         if (tape->active_data_request == rq) {
1239                 active_stage = tape->active_stage;
1240                 tape->active_stage = NULL;
1241                 tape->active_data_request = NULL;
1242                 tape->nr_pending_stages--;
1243                 if (rq->cmd[0] & REQ_IDETAPE_WRITE) {
1244                         remove_stage = 1;
1245                         if (error) {
1246                                 set_bit(IDETAPE_PIPELINE_ERROR, &tape->flags);
1247                                 if (error == IDETAPE_ERROR_EOD)
1248                                         idetape_abort_pipeline(drive, active_stage);
1249                         }
1250                 } else if (rq->cmd[0] & REQ_IDETAPE_READ) {
1251                         if (error == IDETAPE_ERROR_EOD) {
1252                                 set_bit(IDETAPE_PIPELINE_ERROR, &tape->flags);
1253                                 idetape_abort_pipeline(drive, active_stage);
1254                         }
1255                 }
1256                 if (tape->next_stage != NULL) {
1257                         idetape_active_next_stage(drive);
1258
1259                         /*
1260                          * Insert the next request into the request queue.
1261                          */
1262                         (void) ide_do_drive_cmd(drive, tape->active_data_request, ide_end);
1263                 } else if (!error) {
1264                                 idetape_increase_max_pipeline_stages(drive);
1265                 }
1266         }
1267         ide_end_drive_cmd(drive, 0, 0);
1268 //      blkdev_dequeue_request(rq);
1269 //      drive->rq = NULL;
1270 //      end_that_request_last(rq);
1271
1272         if (remove_stage)
1273                 idetape_remove_stage_head(drive);
1274         if (tape->active_data_request == NULL)
1275                 clear_bit(IDETAPE_PIPELINE_ACTIVE, &tape->flags);
1276         spin_unlock_irqrestore(&tape->spinlock, flags);
1277         return 0;
1278 }
1279
1280 static ide_startstop_t idetape_request_sense_callback (ide_drive_t *drive)
1281 {
1282         idetape_tape_t *tape = drive->driver_data;
1283
1284 #if IDETAPE_DEBUG_LOG
1285         if (tape->debug_level >= 4)
1286                 printk(KERN_INFO "ide-tape: Reached idetape_request_sense_callback\n");
1287 #endif /* IDETAPE_DEBUG_LOG */
1288         if (!tape->pc->error) {
1289                 idetape_analyze_error(drive, tape->pc->buffer);
1290                 idetape_end_request(drive, 1, 0);
1291         } else {
1292                 printk(KERN_ERR "ide-tape: Error in REQUEST SENSE itself - Aborting request!\n");
1293                 idetape_end_request(drive, 0, 0);
1294         }
1295         return ide_stopped;
1296 }
1297
1298 static void idetape_create_request_sense_cmd (idetape_pc_t *pc)
1299 {
1300         idetape_init_pc(pc);    
1301         pc->c[0] = IDETAPE_REQUEST_SENSE_CMD;
1302         pc->c[4] = 20;
1303         pc->request_transfer = 20;
1304         pc->callback = &idetape_request_sense_callback;
1305 }
1306
1307 static void idetape_init_rq(struct request *rq, u8 cmd)
1308 {
1309         memset(rq, 0, sizeof(*rq));
1310         rq->cmd_type = REQ_TYPE_SPECIAL;
1311         rq->cmd[0] = cmd;
1312 }
1313
1314 /*
1315  *      idetape_queue_pc_head generates a new packet command request in front
1316  *      of the request queue, before the current request, so that it will be
1317  *      processed immediately, on the next pass through the driver.
1318  *
1319  *      idetape_queue_pc_head is called from the request handling part of
1320  *      the driver (the "bottom" part). Safe storage for the request should
1321  *      be allocated with idetape_next_pc_storage and idetape_next_rq_storage
1322  *      before calling idetape_queue_pc_head.
1323  *
1324  *      Memory for those requests is pre-allocated at initialization time, and
1325  *      is limited to IDETAPE_PC_STACK requests. We assume that we have enough
1326  *      space for the maximum possible number of inter-dependent packet commands.
1327  *
1328  *      The higher level of the driver - The ioctl handler and the character
1329  *      device handling functions should queue request to the lower level part
1330  *      and wait for their completion using idetape_queue_pc_tail or
1331  *      idetape_queue_rw_tail.
1332  */
1333 static void idetape_queue_pc_head (ide_drive_t *drive, idetape_pc_t *pc,struct request *rq)
1334 {
1335         struct ide_tape_obj *tape = drive->driver_data;
1336
1337         idetape_init_rq(rq, REQ_IDETAPE_PC1);
1338         rq->buffer = (char *) pc;
1339         rq->rq_disk = tape->disk;
1340         (void) ide_do_drive_cmd(drive, rq, ide_preempt);
1341 }
1342
1343 /*
1344  *      idetape_retry_pc is called when an error was detected during the
1345  *      last packet command. We queue a request sense packet command in
1346  *      the head of the request list.
1347  */
1348 static ide_startstop_t idetape_retry_pc (ide_drive_t *drive)
1349 {
1350         idetape_tape_t *tape = drive->driver_data;
1351         idetape_pc_t *pc;
1352         struct request *rq;
1353
1354         (void)drive->hwif->INB(IDE_ERROR_REG);
1355         pc = idetape_next_pc_storage(drive);
1356         rq = idetape_next_rq_storage(drive);
1357         idetape_create_request_sense_cmd(pc);
1358         set_bit(IDETAPE_IGNORE_DSC, &tape->flags);
1359         idetape_queue_pc_head(drive, pc, rq);
1360         return ide_stopped;
1361 }
1362
1363 /*
1364  *      idetape_postpone_request postpones the current request so that
1365  *      ide.c will be able to service requests from another device on
1366  *      the same hwgroup while we are polling for DSC.
1367  */
1368 static void idetape_postpone_request (ide_drive_t *drive)
1369 {
1370         idetape_tape_t *tape = drive->driver_data;
1371
1372 #if IDETAPE_DEBUG_LOG
1373         if (tape->debug_level >= 4)
1374                 printk(KERN_INFO "ide-tape: idetape_postpone_request\n");
1375 #endif
1376         tape->postponed_rq = HWGROUP(drive)->rq;
1377         ide_stall_queue(drive, tape->dsc_polling_frequency);
1378 }
1379
1380 /*
1381  *      idetape_pc_intr is the usual interrupt handler which will be called
1382  *      during a packet command. We will transfer some of the data (as
1383  *      requested by the drive) and will re-point interrupt handler to us.
1384  *      When data transfer is finished, we will act according to the
1385  *      algorithm described before idetape_issue_packet_command.
1386  *
1387  */
1388 static ide_startstop_t idetape_pc_intr (ide_drive_t *drive)
1389 {
1390         ide_hwif_t *hwif = drive->hwif;
1391         idetape_tape_t *tape = drive->driver_data;
1392         idetape_pc_t *pc = tape->pc;
1393         unsigned int temp;
1394 #if SIMULATE_ERRORS
1395         static int error_sim_count = 0;
1396 #endif
1397         u16 bcount;
1398         u8 stat, ireason;
1399
1400 #if IDETAPE_DEBUG_LOG
1401         if (tape->debug_level >= 4)
1402                 printk(KERN_INFO "ide-tape: Reached idetape_pc_intr "
1403                                 "interrupt handler\n");
1404 #endif /* IDETAPE_DEBUG_LOG */  
1405
1406         /* Clear the interrupt */
1407         stat = hwif->INB(IDE_STATUS_REG);
1408
1409         if (test_bit(PC_DMA_IN_PROGRESS, &pc->flags)) {
1410                 if (hwif->ide_dma_end(drive) || (stat & ERR_STAT)) {
1411                         /*
1412                          * A DMA error is sometimes expected. For example,
1413                          * if the tape is crossing a filemark during a
1414                          * READ command, it will issue an irq and position
1415                          * itself before the filemark, so that only a partial
1416                          * data transfer will occur (which causes the DMA
1417                          * error). In that case, we will later ask the tape
1418                          * how much bytes of the original request were
1419                          * actually transferred (we can't receive that
1420                          * information from the DMA engine on most chipsets).
1421                          */
1422
1423                         /*
1424                          * On the contrary, a DMA error is never expected;
1425                          * it usually indicates a hardware error or abort.
1426                          * If the tape crosses a filemark during a READ
1427                          * command, it will issue an irq and position itself
1428                          * after the filemark (not before). Only a partial
1429                          * data transfer will occur, but no DMA error.
1430                          * (AS, 19 Apr 2001)
1431                          */
1432                         set_bit(PC_DMA_ERROR, &pc->flags);
1433                 } else {
1434                         pc->actually_transferred = pc->request_transfer;
1435                         idetape_update_buffers(pc);
1436                 }
1437 #if IDETAPE_DEBUG_LOG
1438                 if (tape->debug_level >= 4)
1439                         printk(KERN_INFO "ide-tape: DMA finished\n");
1440 #endif /* IDETAPE_DEBUG_LOG */
1441         }
1442
1443         /* No more interrupts */
1444         if ((stat & DRQ_STAT) == 0) {
1445 #if IDETAPE_DEBUG_LOG
1446                 if (tape->debug_level >= 2)
1447                         printk(KERN_INFO "ide-tape: Packet command completed, %d bytes transferred\n", pc->actually_transferred);
1448 #endif /* IDETAPE_DEBUG_LOG */
1449                 clear_bit(PC_DMA_IN_PROGRESS, &pc->flags);
1450
1451                 local_irq_enable();
1452
1453 #if SIMULATE_ERRORS
1454                 if ((pc->c[0] == IDETAPE_WRITE_CMD ||
1455                      pc->c[0] == IDETAPE_READ_CMD) &&
1456                     (++error_sim_count % 100) == 0) {
1457                         printk(KERN_INFO "ide-tape: %s: simulating error\n",
1458                                 tape->name);
1459                         stat |= ERR_STAT;
1460                 }
1461 #endif
1462                 if ((stat & ERR_STAT) && pc->c[0] == IDETAPE_REQUEST_SENSE_CMD)
1463                         stat &= ~ERR_STAT;
1464                 if ((stat & ERR_STAT) || test_bit(PC_DMA_ERROR, &pc->flags)) {
1465                         /* Error detected */
1466 #if IDETAPE_DEBUG_LOG
1467                         if (tape->debug_level >= 1)
1468                                 printk(KERN_INFO "ide-tape: %s: I/O error\n",
1469                                         tape->name);
1470 #endif /* IDETAPE_DEBUG_LOG */
1471                         if (pc->c[0] == IDETAPE_REQUEST_SENSE_CMD) {
1472                                 printk(KERN_ERR "ide-tape: I/O error in request sense command\n");
1473                                 return ide_do_reset(drive);
1474                         }
1475 #if IDETAPE_DEBUG_LOG
1476                         if (tape->debug_level >= 1)
1477                                 printk(KERN_INFO "ide-tape: [cmd %x]: check condition\n", pc->c[0]);
1478 #endif
1479                         /* Retry operation */
1480                         return idetape_retry_pc(drive);
1481                 }
1482                 pc->error = 0;
1483                 if (test_bit(PC_WAIT_FOR_DSC, &pc->flags) &&
1484                     (stat & SEEK_STAT) == 0) {
1485                         /* Media access command */
1486                         tape->dsc_polling_start = jiffies;
1487                         tape->dsc_polling_frequency = IDETAPE_DSC_MA_FAST;
1488                         tape->dsc_timeout = jiffies + IDETAPE_DSC_MA_TIMEOUT;
1489                         /* Allow ide.c to handle other requests */
1490                         idetape_postpone_request(drive);
1491                         return ide_stopped;
1492                 }
1493                 if (tape->failed_pc == pc)
1494                         tape->failed_pc = NULL;
1495                 /* Command finished - Call the callback function */
1496                 return pc->callback(drive);
1497         }
1498         if (test_and_clear_bit(PC_DMA_IN_PROGRESS, &pc->flags)) {
1499                 printk(KERN_ERR "ide-tape: The tape wants to issue more "
1500                                 "interrupts in DMA mode\n");
1501                 printk(KERN_ERR "ide-tape: DMA disabled, reverting to PIO\n");
1502                 ide_dma_off(drive);
1503                 return ide_do_reset(drive);
1504         }
1505         /* Get the number of bytes to transfer on this interrupt. */
1506         bcount = (hwif->INB(IDE_BCOUNTH_REG) << 8) |
1507                   hwif->INB(IDE_BCOUNTL_REG);
1508
1509         ireason = hwif->INB(IDE_IREASON_REG);
1510
1511         if (ireason & CD) {
1512                 printk(KERN_ERR "ide-tape: CoD != 0 in idetape_pc_intr\n");
1513                 return ide_do_reset(drive);
1514         }
1515         if (((ireason & IO) == IO) == test_bit(PC_WRITING, &pc->flags)) {
1516                 /* Hopefully, we will never get here */
1517                 printk(KERN_ERR "ide-tape: We wanted to %s, ",
1518                                 (ireason & IO) ? "Write" : "Read");
1519                 printk(KERN_ERR "ide-tape: but the tape wants us to %s !\n",
1520                                 (ireason & IO) ? "Read" : "Write");
1521                 return ide_do_reset(drive);
1522         }
1523         if (!test_bit(PC_WRITING, &pc->flags)) {
1524                 /* Reading - Check that we have enough space */
1525                 temp = pc->actually_transferred + bcount;
1526                 if (temp > pc->request_transfer) {
1527                         if (temp > pc->buffer_size) {
1528                                 printk(KERN_ERR "ide-tape: The tape wants to send us more data than expected - discarding data\n");
1529                                 idetape_discard_data(drive, bcount);
1530                                 ide_set_handler(drive, &idetape_pc_intr, IDETAPE_WAIT_CMD, NULL);
1531                                 return ide_started;
1532                         }
1533 #if IDETAPE_DEBUG_LOG
1534                         if (tape->debug_level >= 2)
1535                                 printk(KERN_NOTICE "ide-tape: The tape wants to send us more data than expected - allowing transfer\n");
1536 #endif /* IDETAPE_DEBUG_LOG */
1537                 }
1538         }
1539         if (test_bit(PC_WRITING, &pc->flags)) {
1540                 if (pc->bh != NULL)
1541                         idetape_output_buffers(drive, pc, bcount);
1542                 else
1543                         /* Write the current buffer */
1544                         hwif->atapi_output_bytes(drive, pc->current_position,
1545                                                  bcount);
1546         } else {
1547                 if (pc->bh != NULL)
1548                         idetape_input_buffers(drive, pc, bcount);
1549                 else
1550                         /* Read the current buffer */
1551                         hwif->atapi_input_bytes(drive, pc->current_position,
1552                                                 bcount);
1553         }
1554         /* Update the current position */
1555         pc->actually_transferred += bcount;
1556         pc->current_position += bcount;
1557 #if IDETAPE_DEBUG_LOG
1558         if (tape->debug_level >= 2)
1559                 printk(KERN_INFO "ide-tape: [cmd %x] transferred %d bytes "
1560                                  "on that interrupt\n", pc->c[0], bcount);
1561 #endif
1562         /* And set the interrupt handler again */
1563         ide_set_handler(drive, &idetape_pc_intr, IDETAPE_WAIT_CMD, NULL);
1564         return ide_started;
1565 }
1566
1567 /*
1568  *      Packet Command Interface
1569  *
1570  *      The current Packet Command is available in tape->pc, and will not
1571  *      change until we finish handling it. Each packet command is associated
1572  *      with a callback function that will be called when the command is
1573  *      finished.
1574  *
1575  *      The handling will be done in three stages:
1576  *
1577  *      1.      idetape_issue_packet_command will send the packet command to the
1578  *              drive, and will set the interrupt handler to idetape_pc_intr.
1579  *
1580  *      2.      On each interrupt, idetape_pc_intr will be called. This step
1581  *              will be repeated until the device signals us that no more
1582  *              interrupts will be issued.
1583  *
1584  *      3.      ATAPI Tape media access commands have immediate status with a
1585  *              delayed process. In case of a successful initiation of a
1586  *              media access packet command, the DSC bit will be set when the
1587  *              actual execution of the command is finished. 
1588  *              Since the tape drive will not issue an interrupt, we have to
1589  *              poll for this event. In this case, we define the request as
1590  *              "low priority request" by setting rq_status to
1591  *              IDETAPE_RQ_POSTPONED,   set a timer to poll for DSC and exit
1592  *              the driver.
1593  *
1594  *              ide.c will then give higher priority to requests which
1595  *              originate from the other device, until will change rq_status
1596  *              to RQ_ACTIVE.
1597  *
1598  *      4.      When the packet command is finished, it will be checked for errors.
1599  *
1600  *      5.      In case an error was found, we queue a request sense packet
1601  *              command in front of the request queue and retry the operation
1602  *              up to IDETAPE_MAX_PC_RETRIES times.
1603  *
1604  *      6.      In case no error was found, or we decided to give up and not
1605  *              to retry again, the callback function will be called and then
1606  *              we will handle the next request.
1607  *
1608  */
1609 static ide_startstop_t idetape_transfer_pc(ide_drive_t *drive)
1610 {
1611         ide_hwif_t *hwif = drive->hwif;
1612         idetape_tape_t *tape = drive->driver_data;
1613         idetape_pc_t *pc = tape->pc;
1614         int retries = 100;
1615         ide_startstop_t startstop;
1616         u8 ireason;
1617
1618         if (ide_wait_stat(&startstop,drive,DRQ_STAT,BUSY_STAT,WAIT_READY)) {
1619                 printk(KERN_ERR "ide-tape: Strange, packet command initiated yet DRQ isn't asserted\n");
1620                 return startstop;
1621         }
1622         ireason = hwif->INB(IDE_IREASON_REG);
1623         while (retries-- && ((ireason & CD) == 0 || (ireason & IO))) {
1624                 printk(KERN_ERR "ide-tape: (IO,CoD != (0,1) while issuing "
1625                                 "a packet command, retrying\n");
1626                 udelay(100);
1627                 ireason = hwif->INB(IDE_IREASON_REG);
1628                 if (retries == 0) {
1629                         printk(KERN_ERR "ide-tape: (IO,CoD != (0,1) while "
1630                                         "issuing a packet command, ignoring\n");
1631                         ireason |= CD;
1632                         ireason &= ~IO;
1633                 }
1634         }
1635         if ((ireason & CD) == 0 || (ireason & IO)) {
1636                 printk(KERN_ERR "ide-tape: (IO,CoD) != (0,1) while issuing "
1637                                 "a packet command\n");
1638                 return ide_do_reset(drive);
1639         }
1640         /* Set the interrupt routine */
1641         ide_set_handler(drive, &idetape_pc_intr, IDETAPE_WAIT_CMD, NULL);
1642 #ifdef CONFIG_BLK_DEV_IDEDMA
1643         /* Begin DMA, if necessary */
1644         if (test_bit(PC_DMA_IN_PROGRESS, &pc->flags))
1645                 hwif->dma_start(drive);
1646 #endif
1647         /* Send the actual packet */
1648         HWIF(drive)->atapi_output_bytes(drive, pc->c, 12);
1649         return ide_started;
1650 }
1651
1652 static ide_startstop_t idetape_issue_packet_command (ide_drive_t *drive, idetape_pc_t *pc)
1653 {
1654         ide_hwif_t *hwif = drive->hwif;
1655         idetape_tape_t *tape = drive->driver_data;
1656         int dma_ok = 0;
1657         u16 bcount;
1658
1659 #if IDETAPE_DEBUG_BUGS
1660         if (tape->pc->c[0] == IDETAPE_REQUEST_SENSE_CMD &&
1661             pc->c[0] == IDETAPE_REQUEST_SENSE_CMD) {
1662                 printk(KERN_ERR "ide-tape: possible ide-tape.c bug - "
1663                         "Two request sense in serial were issued\n");
1664         }
1665 #endif /* IDETAPE_DEBUG_BUGS */
1666
1667         if (tape->failed_pc == NULL && pc->c[0] != IDETAPE_REQUEST_SENSE_CMD)
1668                 tape->failed_pc = pc;
1669         /* Set the current packet command */
1670         tape->pc = pc;
1671
1672         if (pc->retries > IDETAPE_MAX_PC_RETRIES ||
1673             test_bit(PC_ABORT, &pc->flags)) {
1674                 /*
1675                  *      We will "abort" retrying a packet command in case
1676                  *      a legitimate error code was received (crossing a
1677                  *      filemark, or end of the media, for example).
1678                  */
1679                 if (!test_bit(PC_ABORT, &pc->flags)) {
1680                         if (!(pc->c[0] == IDETAPE_TEST_UNIT_READY_CMD &&
1681                               tape->sense_key == 2 && tape->asc == 4 &&
1682                              (tape->ascq == 1 || tape->ascq == 8))) {
1683                                 printk(KERN_ERR "ide-tape: %s: I/O error, "
1684                                                 "pc = %2x, key = %2x, "
1685                                                 "asc = %2x, ascq = %2x\n",
1686                                                 tape->name, pc->c[0],
1687                                                 tape->sense_key, tape->asc,
1688                                                 tape->ascq);
1689                         }
1690                         /* Giving up */
1691                         pc->error = IDETAPE_ERROR_GENERAL;
1692                 }
1693                 tape->failed_pc = NULL;
1694                 return pc->callback(drive);
1695         }
1696 #if IDETAPE_DEBUG_LOG
1697         if (tape->debug_level >= 2)
1698                 printk(KERN_INFO "ide-tape: Retry number - %d, cmd = %02X\n", pc->retries, pc->c[0]);
1699 #endif /* IDETAPE_DEBUG_LOG */
1700
1701         pc->retries++;
1702         /* We haven't transferred any data yet */
1703         pc->actually_transferred = 0;
1704         pc->current_position = pc->buffer;
1705         /* Request to transfer the entire buffer at once */
1706         bcount = pc->request_transfer;
1707
1708         if (test_and_clear_bit(PC_DMA_ERROR, &pc->flags)) {
1709                 printk(KERN_WARNING "ide-tape: DMA disabled, "
1710                                 "reverting to PIO\n");
1711                 ide_dma_off(drive);
1712         }
1713         if (test_bit(PC_DMA_RECOMMENDED, &pc->flags) && drive->using_dma)
1714                 dma_ok = !hwif->dma_setup(drive);
1715
1716         ide_pktcmd_tf_load(drive, IDE_TFLAG_NO_SELECT_MASK |
1717                            IDE_TFLAG_OUT_DEVICE, bcount, dma_ok);
1718
1719         if (dma_ok)                     /* Will begin DMA later */
1720                 set_bit(PC_DMA_IN_PROGRESS, &pc->flags);
1721         if (test_bit(IDETAPE_DRQ_INTERRUPT, &tape->flags)) {
1722                 ide_execute_command(drive, WIN_PACKETCMD, &idetape_transfer_pc,
1723                                     IDETAPE_WAIT_CMD, NULL);
1724                 return ide_started;
1725         } else {
1726                 hwif->OUTB(WIN_PACKETCMD, IDE_COMMAND_REG);
1727                 return idetape_transfer_pc(drive);
1728         }
1729 }
1730
1731 /*
1732  *      General packet command callback function.
1733  */
1734 static ide_startstop_t idetape_pc_callback (ide_drive_t *drive)
1735 {
1736         idetape_tape_t *tape = drive->driver_data;
1737         
1738 #if IDETAPE_DEBUG_LOG
1739         if (tape->debug_level >= 4)
1740                 printk(KERN_INFO "ide-tape: Reached idetape_pc_callback\n");
1741 #endif /* IDETAPE_DEBUG_LOG */
1742
1743         idetape_end_request(drive, tape->pc->error ? 0 : 1, 0);
1744         return ide_stopped;
1745 }
1746
1747 /*
1748  *      A mode sense command is used to "sense" tape parameters.
1749  */
1750 static void idetape_create_mode_sense_cmd (idetape_pc_t *pc, u8 page_code)
1751 {
1752         idetape_init_pc(pc);
1753         pc->c[0] = IDETAPE_MODE_SENSE_CMD;
1754         if (page_code != IDETAPE_BLOCK_DESCRIPTOR)
1755                 pc->c[1] = 8;   /* DBD = 1 - Don't return block descriptors */
1756         pc->c[2] = page_code;
1757         /*
1758          * Changed pc->c[3] to 0 (255 will at best return unused info).
1759          *
1760          * For SCSI this byte is defined as subpage instead of high byte
1761          * of length and some IDE drives seem to interpret it this way
1762          * and return an error when 255 is used.
1763          */
1764         pc->c[3] = 0;
1765         pc->c[4] = 255;         /* (We will just discard data in that case) */
1766         if (page_code == IDETAPE_BLOCK_DESCRIPTOR)
1767                 pc->request_transfer = 12;
1768         else if (page_code == IDETAPE_CAPABILITIES_PAGE)
1769                 pc->request_transfer = 24;
1770         else
1771                 pc->request_transfer = 50;
1772         pc->callback = &idetape_pc_callback;
1773 }
1774
1775 static void calculate_speeds(ide_drive_t *drive)
1776 {
1777         idetape_tape_t *tape = drive->driver_data;
1778         int full = 125, empty = 75;
1779
1780         if (time_after(jiffies, tape->controlled_pipeline_head_time + 120 * HZ)) {
1781                 tape->controlled_previous_pipeline_head = tape->controlled_last_pipeline_head;
1782                 tape->controlled_previous_head_time = tape->controlled_pipeline_head_time;
1783                 tape->controlled_last_pipeline_head = tape->pipeline_head;
1784                 tape->controlled_pipeline_head_time = jiffies;
1785         }
1786         if (time_after(jiffies, tape->controlled_pipeline_head_time + 60 * HZ))
1787                 tape->controlled_pipeline_head_speed = (tape->pipeline_head - tape->controlled_last_pipeline_head) * 32 * HZ / (jiffies - tape->controlled_pipeline_head_time);
1788         else if (time_after(jiffies, tape->controlled_previous_head_time))
1789                 tape->controlled_pipeline_head_speed = (tape->pipeline_head - tape->controlled_previous_pipeline_head) * 32 * HZ / (jiffies - tape->controlled_previous_head_time);
1790
1791         if (tape->nr_pending_stages < tape->max_stages /*- 1 */) {
1792                 /* -1 for read mode error recovery */
1793                 if (time_after(jiffies, tape->uncontrolled_previous_head_time + 10 * HZ)) {
1794                         tape->uncontrolled_pipeline_head_time = jiffies;
1795                         tape->uncontrolled_pipeline_head_speed = (tape->pipeline_head - tape->uncontrolled_previous_pipeline_head) * 32 * HZ / (jiffies - tape->uncontrolled_previous_head_time);
1796                 }
1797         } else {
1798                 tape->uncontrolled_previous_head_time = jiffies;
1799                 tape->uncontrolled_previous_pipeline_head = tape->pipeline_head;
1800                 if (time_after(jiffies, tape->uncontrolled_pipeline_head_time + 30 * HZ)) {
1801                         tape->uncontrolled_pipeline_head_time = jiffies;
1802                 }
1803         }
1804         tape->pipeline_head_speed = max(tape->uncontrolled_pipeline_head_speed, tape->controlled_pipeline_head_speed);
1805         if (tape->speed_control == 0) {
1806                 tape->max_insert_speed = 5000;
1807         } else if (tape->speed_control == 1) {
1808                 if (tape->nr_pending_stages >= tape->max_stages / 2)
1809                         tape->max_insert_speed = tape->pipeline_head_speed +
1810                                 (1100 - tape->pipeline_head_speed) * 2 * (tape->nr_pending_stages - tape->max_stages / 2) / tape->max_stages;
1811                 else
1812                         tape->max_insert_speed = 500 +
1813                                 (tape->pipeline_head_speed - 500) * 2 * tape->nr_pending_stages / tape->max_stages;
1814                 if (tape->nr_pending_stages >= tape->max_stages * 99 / 100)
1815                         tape->max_insert_speed = 5000;
1816         } else if (tape->speed_control == 2) {
1817                 tape->max_insert_speed = tape->pipeline_head_speed * empty / 100 +
1818                         (tape->pipeline_head_speed * full / 100 - tape->pipeline_head_speed * empty / 100) * tape->nr_pending_stages / tape->max_stages;
1819         } else
1820                 tape->max_insert_speed = tape->speed_control;
1821         tape->max_insert_speed = max(tape->max_insert_speed, 500);
1822 }
1823
1824 static ide_startstop_t idetape_media_access_finished (ide_drive_t *drive)
1825 {
1826         idetape_tape_t *tape = drive->driver_data;
1827         idetape_pc_t *pc = tape->pc;
1828         u8 stat;
1829
1830         stat = drive->hwif->INB(IDE_STATUS_REG);
1831         if (stat & SEEK_STAT) {
1832                 if (stat & ERR_STAT) {
1833                         /* Error detected */
1834                         if (pc->c[0] != IDETAPE_TEST_UNIT_READY_CMD)
1835                                 printk(KERN_ERR "ide-tape: %s: I/O error, ",
1836                                                 tape->name);
1837                         /* Retry operation */
1838                         return idetape_retry_pc(drive);
1839                 }
1840                 pc->error = 0;
1841                 if (tape->failed_pc == pc)
1842                         tape->failed_pc = NULL;
1843         } else {
1844                 pc->error = IDETAPE_ERROR_GENERAL;
1845                 tape->failed_pc = NULL;
1846         }
1847         return pc->callback(drive);
1848 }
1849
1850 static ide_startstop_t idetape_rw_callback (ide_drive_t *drive)
1851 {
1852         idetape_tape_t *tape = drive->driver_data;
1853         struct request *rq = HWGROUP(drive)->rq;
1854         int blocks = tape->pc->actually_transferred / tape->tape_block_size;
1855
1856         tape->avg_size += blocks * tape->tape_block_size;
1857         tape->insert_size += blocks * tape->tape_block_size;
1858         if (tape->insert_size > 1024 * 1024)
1859                 tape->measure_insert_time = 1;
1860         if (tape->measure_insert_time) {
1861                 tape->measure_insert_time = 0;
1862                 tape->insert_time = jiffies;
1863                 tape->insert_size = 0;
1864         }
1865         if (time_after(jiffies, tape->insert_time))
1866                 tape->insert_speed = tape->insert_size / 1024 * HZ / (jiffies - tape->insert_time);
1867         if (time_after_eq(jiffies, tape->avg_time + HZ)) {
1868                 tape->avg_speed = tape->avg_size * HZ / (jiffies - tape->avg_time) / 1024;
1869                 tape->avg_size = 0;
1870                 tape->avg_time = jiffies;
1871         }
1872
1873 #if IDETAPE_DEBUG_LOG   
1874         if (tape->debug_level >= 4)
1875                 printk(KERN_INFO "ide-tape: Reached idetape_rw_callback\n");
1876 #endif /* IDETAPE_DEBUG_LOG */
1877
1878         tape->first_frame_position += blocks;
1879         rq->current_nr_sectors -= blocks;
1880
1881         if (!tape->pc->error)
1882                 idetape_end_request(drive, 1, 0);
1883         else
1884                 idetape_end_request(drive, tape->pc->error, 0);
1885         return ide_stopped;
1886 }
1887
1888 static void idetape_create_read_cmd(idetape_tape_t *tape, idetape_pc_t *pc, unsigned int length, struct idetape_bh *bh)
1889 {
1890         idetape_init_pc(pc);
1891         pc->c[0] = IDETAPE_READ_CMD;
1892         put_unaligned(htonl(length), (unsigned int *) &pc->c[1]);
1893         pc->c[1] = 1;
1894         pc->callback = &idetape_rw_callback;
1895         pc->bh = bh;
1896         atomic_set(&bh->b_count, 0);
1897         pc->buffer = NULL;
1898         pc->request_transfer = pc->buffer_size = length * tape->tape_block_size;
1899         if (pc->request_transfer == tape->stage_size)
1900                 set_bit(PC_DMA_RECOMMENDED, &pc->flags);
1901 }
1902
1903 static void idetape_create_read_buffer_cmd(idetape_tape_t *tape, idetape_pc_t *pc, unsigned int length, struct idetape_bh *bh)
1904 {
1905         int size = 32768;
1906         struct idetape_bh *p = bh;
1907
1908         idetape_init_pc(pc);
1909         pc->c[0] = IDETAPE_READ_BUFFER_CMD;
1910         pc->c[1] = IDETAPE_RETRIEVE_FAULTY_BLOCK;
1911         pc->c[7] = size >> 8;
1912         pc->c[8] = size & 0xff;
1913         pc->callback = &idetape_pc_callback;
1914         pc->bh = bh;
1915         atomic_set(&bh->b_count, 0);
1916         pc->buffer = NULL;
1917         while (p) {
1918                 atomic_set(&p->b_count, 0);
1919                 p = p->b_reqnext;
1920         }
1921         pc->request_transfer = pc->buffer_size = size;
1922 }
1923
1924 static void idetape_create_write_cmd(idetape_tape_t *tape, idetape_pc_t *pc, unsigned int length, struct idetape_bh *bh)
1925 {
1926         idetape_init_pc(pc);
1927         pc->c[0] = IDETAPE_WRITE_CMD;
1928         put_unaligned(htonl(length), (unsigned int *) &pc->c[1]);
1929         pc->c[1] = 1;
1930         pc->callback = &idetape_rw_callback;
1931         set_bit(PC_WRITING, &pc->flags);
1932         pc->bh = bh;
1933         pc->b_data = bh->b_data;
1934         pc->b_count = atomic_read(&bh->b_count);
1935         pc->buffer = NULL;
1936         pc->request_transfer = pc->buffer_size = length * tape->tape_block_size;
1937         if (pc->request_transfer == tape->stage_size)
1938                 set_bit(PC_DMA_RECOMMENDED, &pc->flags);
1939 }
1940
1941 /*
1942  * idetape_do_request is our request handling function. 
1943  */
1944 static ide_startstop_t idetape_do_request(ide_drive_t *drive,
1945                                           struct request *rq, sector_t block)
1946 {
1947         idetape_tape_t *tape = drive->driver_data;
1948         idetape_pc_t *pc = NULL;
1949         struct request *postponed_rq = tape->postponed_rq;
1950         u8 stat;
1951
1952 #if IDETAPE_DEBUG_LOG
1953         if (tape->debug_level >= 2)
1954                 printk(KERN_INFO "ide-tape: sector: %ld, "
1955                         "nr_sectors: %ld, current_nr_sectors: %d\n",
1956                         rq->sector, rq->nr_sectors, rq->current_nr_sectors);
1957 #endif /* IDETAPE_DEBUG_LOG */
1958
1959         if (!blk_special_request(rq)) {
1960                 /*
1961                  * We do not support buffer cache originated requests.
1962                  */
1963                 printk(KERN_NOTICE "ide-tape: %s: Unsupported request in "
1964                         "request queue (%d)\n", drive->name, rq->cmd_type);
1965                 ide_end_request(drive, 0, 0);
1966                 return ide_stopped;
1967         }
1968
1969         /*
1970          *      Retry a failed packet command
1971          */
1972         if (tape->failed_pc != NULL &&
1973             tape->pc->c[0] == IDETAPE_REQUEST_SENSE_CMD) {
1974                 return idetape_issue_packet_command(drive, tape->failed_pc);
1975         }
1976 #if IDETAPE_DEBUG_BUGS
1977         if (postponed_rq != NULL)
1978                 if (rq != postponed_rq) {
1979                         printk(KERN_ERR "ide-tape: ide-tape.c bug - "
1980                                         "Two DSC requests were queued\n");
1981                         idetape_end_request(drive, 0, 0);
1982                         return ide_stopped;
1983                 }
1984 #endif /* IDETAPE_DEBUG_BUGS */
1985
1986         tape->postponed_rq = NULL;
1987
1988         /*
1989          * If the tape is still busy, postpone our request and service
1990          * the other device meanwhile.
1991          */
1992         stat = drive->hwif->INB(IDE_STATUS_REG);
1993
1994         if (!drive->dsc_overlap && !(rq->cmd[0] & REQ_IDETAPE_PC2))
1995                 set_bit(IDETAPE_IGNORE_DSC, &tape->flags);
1996
1997         if (drive->post_reset == 1) {
1998                 set_bit(IDETAPE_IGNORE_DSC, &tape->flags);
1999                 drive->post_reset = 0;
2000         }
2001
2002         if (tape->tape_still_time > 100 && tape->tape_still_time < 200)
2003                 tape->measure_insert_time = 1;
2004         if (time_after(jiffies, tape->insert_time))
2005                 tape->insert_speed = tape->insert_size / 1024 * HZ / (jiffies - tape->insert_time);
2006         calculate_speeds(drive);
2007         if (!test_and_clear_bit(IDETAPE_IGNORE_DSC, &tape->flags) &&
2008             (stat & SEEK_STAT) == 0) {
2009                 if (postponed_rq == NULL) {
2010                         tape->dsc_polling_start = jiffies;
2011                         tape->dsc_polling_frequency = tape->best_dsc_rw_frequency;
2012                         tape->dsc_timeout = jiffies + IDETAPE_DSC_RW_TIMEOUT;
2013                 } else if (time_after(jiffies, tape->dsc_timeout)) {
2014                         printk(KERN_ERR "ide-tape: %s: DSC timeout\n",
2015                                 tape->name);
2016                         if (rq->cmd[0] & REQ_IDETAPE_PC2) {
2017                                 idetape_media_access_finished(drive);
2018                                 return ide_stopped;
2019                         } else {
2020                                 return ide_do_reset(drive);
2021                         }
2022                 } else if (time_after(jiffies, tape->dsc_polling_start + IDETAPE_DSC_MA_THRESHOLD))
2023                         tape->dsc_polling_frequency = IDETAPE_DSC_MA_SLOW;
2024                 idetape_postpone_request(drive);
2025                 return ide_stopped;
2026         }
2027         if (rq->cmd[0] & REQ_IDETAPE_READ) {
2028                 tape->buffer_head++;
2029                 tape->postpone_cnt = 0;
2030                 pc = idetape_next_pc_storage(drive);
2031                 idetape_create_read_cmd(tape, pc, rq->current_nr_sectors, (struct idetape_bh *)rq->special);
2032                 goto out;
2033         }
2034         if (rq->cmd[0] & REQ_IDETAPE_WRITE) {
2035                 tape->buffer_head++;
2036                 tape->postpone_cnt = 0;
2037                 pc = idetape_next_pc_storage(drive);
2038                 idetape_create_write_cmd(tape, pc, rq->current_nr_sectors, (struct idetape_bh *)rq->special);
2039                 goto out;
2040         }
2041         if (rq->cmd[0] & REQ_IDETAPE_READ_BUFFER) {
2042                 tape->postpone_cnt = 0;
2043                 pc = idetape_next_pc_storage(drive);
2044                 idetape_create_read_buffer_cmd(tape, pc, rq->current_nr_sectors, (struct idetape_bh *)rq->special);
2045                 goto out;
2046         }
2047         if (rq->cmd[0] & REQ_IDETAPE_PC1) {
2048                 pc = (idetape_pc_t *) rq->buffer;
2049                 rq->cmd[0] &= ~(REQ_IDETAPE_PC1);
2050                 rq->cmd[0] |= REQ_IDETAPE_PC2;
2051                 goto out;
2052         }
2053         if (rq->cmd[0] & REQ_IDETAPE_PC2) {
2054                 idetape_media_access_finished(drive);
2055                 return ide_stopped;
2056         }
2057         BUG();
2058 out:
2059         return idetape_issue_packet_command(drive, pc);
2060 }
2061
2062 /*
2063  *      Pipeline related functions
2064  */
2065 static inline int idetape_pipeline_active (idetape_tape_t *tape)
2066 {
2067         int rc1, rc2;
2068
2069         rc1 = test_bit(IDETAPE_PIPELINE_ACTIVE, &tape->flags);
2070         rc2 = (tape->active_data_request != NULL);
2071         return rc1;
2072 }
2073
2074 /*
2075  *      idetape_kmalloc_stage uses __get_free_page to allocate a pipeline
2076  *      stage, along with all the necessary small buffers which together make
2077  *      a buffer of size tape->stage_size (or a bit more). We attempt to
2078  *      combine sequential pages as much as possible.
2079  *
2080  *      Returns a pointer to the new allocated stage, or NULL if we
2081  *      can't (or don't want to) allocate a stage.
2082  *
2083  *      Pipeline stages are optional and are used to increase performance.
2084  *      If we can't allocate them, we'll manage without them.
2085  */
2086 static idetape_stage_t *__idetape_kmalloc_stage (idetape_tape_t *tape, int full, int clear)
2087 {
2088         idetape_stage_t *stage;
2089         struct idetape_bh *prev_bh, *bh;
2090         int pages = tape->pages_per_stage;
2091         char *b_data = NULL;
2092
2093         if ((stage = kmalloc(sizeof (idetape_stage_t),GFP_KERNEL)) == NULL)
2094                 return NULL;
2095         stage->next = NULL;
2096
2097         bh = stage->bh = kmalloc(sizeof(struct idetape_bh), GFP_KERNEL);
2098         if (bh == NULL)
2099                 goto abort;
2100         bh->b_reqnext = NULL;
2101         if ((bh->b_data = (char *) __get_free_page (GFP_KERNEL)) == NULL)
2102                 goto abort;
2103         if (clear)
2104                 memset(bh->b_data, 0, PAGE_SIZE);
2105         bh->b_size = PAGE_SIZE;
2106         atomic_set(&bh->b_count, full ? bh->b_size : 0);
2107
2108         while (--pages) {
2109                 if ((b_data = (char *) __get_free_page (GFP_KERNEL)) == NULL)
2110                         goto abort;
2111                 if (clear)
2112                         memset(b_data, 0, PAGE_SIZE);
2113                 if (bh->b_data == b_data + PAGE_SIZE) {
2114                         bh->b_size += PAGE_SIZE;
2115                         bh->b_data -= PAGE_SIZE;
2116                         if (full)
2117                                 atomic_add(PAGE_SIZE, &bh->b_count);
2118                         continue;
2119                 }
2120                 if (b_data == bh->b_data + bh->b_size) {
2121                         bh->b_size += PAGE_SIZE;
2122                         if (full)
2123                                 atomic_add(PAGE_SIZE, &bh->b_count);
2124                         continue;
2125                 }
2126                 prev_bh = bh;
2127                 if ((bh = kmalloc(sizeof(struct idetape_bh), GFP_KERNEL)) == NULL) {
2128                         free_page((unsigned long) b_data);
2129                         goto abort;
2130                 }
2131                 bh->b_reqnext = NULL;
2132                 bh->b_data = b_data;
2133                 bh->b_size = PAGE_SIZE;
2134                 atomic_set(&bh->b_count, full ? bh->b_size : 0);
2135                 prev_bh->b_reqnext = bh;
2136         }
2137         bh->b_size -= tape->excess_bh_size;
2138         if (full)
2139                 atomic_sub(tape->excess_bh_size, &bh->b_count);
2140         return stage;
2141 abort:
2142         __idetape_kfree_stage(stage);
2143         return NULL;
2144 }
2145
2146 static idetape_stage_t *idetape_kmalloc_stage (idetape_tape_t *tape)
2147 {
2148         idetape_stage_t *cache_stage = tape->cache_stage;
2149
2150 #if IDETAPE_DEBUG_LOG
2151         if (tape->debug_level >= 4)
2152                 printk(KERN_INFO "ide-tape: Reached idetape_kmalloc_stage\n");
2153 #endif /* IDETAPE_DEBUG_LOG */
2154
2155         if (tape->nr_stages >= tape->max_stages)
2156                 return NULL;
2157         if (cache_stage != NULL) {
2158                 tape->cache_stage = NULL;
2159                 return cache_stage;
2160         }
2161         return __idetape_kmalloc_stage(tape, 0, 0);
2162 }
2163
2164 static int idetape_copy_stage_from_user (idetape_tape_t *tape, idetape_stage_t *stage, const char __user *buf, int n)
2165 {
2166         struct idetape_bh *bh = tape->bh;
2167         int count;
2168         int ret = 0;
2169
2170         while (n) {
2171 #if IDETAPE_DEBUG_BUGS
2172                 if (bh == NULL) {
2173                         printk(KERN_ERR "ide-tape: bh == NULL in "
2174                                 "idetape_copy_stage_from_user\n");
2175                         return 1;
2176                 }
2177 #endif /* IDETAPE_DEBUG_BUGS */
2178                 count = min((unsigned int)(bh->b_size - atomic_read(&bh->b_count)), (unsigned int)n);
2179                 if (copy_from_user(bh->b_data + atomic_read(&bh->b_count), buf, count))
2180                         ret = 1;
2181                 n -= count;
2182                 atomic_add(count, &bh->b_count);
2183                 buf += count;
2184                 if (atomic_read(&bh->b_count) == bh->b_size) {
2185                         bh = bh->b_reqnext;
2186                         if (bh)
2187                                 atomic_set(&bh->b_count, 0);
2188                 }
2189         }
2190         tape->bh = bh;
2191         return ret;
2192 }
2193
2194 static int idetape_copy_stage_to_user (idetape_tape_t *tape, char __user *buf, idetape_stage_t *stage, int n)
2195 {
2196         struct idetape_bh *bh = tape->bh;
2197         int count;
2198         int ret = 0;
2199
2200         while (n) {
2201 #if IDETAPE_DEBUG_BUGS
2202                 if (bh == NULL) {
2203                         printk(KERN_ERR "ide-tape: bh == NULL in "
2204                                 "idetape_copy_stage_to_user\n");
2205                         return 1;
2206                 }
2207 #endif /* IDETAPE_DEBUG_BUGS */
2208                 count = min(tape->b_count, n);
2209                 if  (copy_to_user(buf, tape->b_data, count))
2210                         ret = 1;
2211                 n -= count;
2212                 tape->b_data += count;
2213                 tape->b_count -= count;
2214                 buf += count;
2215                 if (!tape->b_count) {
2216                         tape->bh = bh = bh->b_reqnext;
2217                         if (bh) {
2218                                 tape->b_data = bh->b_data;
2219                                 tape->b_count = atomic_read(&bh->b_count);
2220                         }
2221                 }
2222         }
2223         return ret;
2224 }
2225
2226 static void idetape_init_merge_stage (idetape_tape_t *tape)
2227 {
2228         struct idetape_bh *bh = tape->merge_stage->bh;
2229         
2230         tape->bh = bh;
2231         if (tape->chrdev_direction == idetape_direction_write)
2232                 atomic_set(&bh->b_count, 0);
2233         else {
2234                 tape->b_data = bh->b_data;
2235                 tape->b_count = atomic_read(&bh->b_count);
2236         }
2237 }
2238
2239 static void idetape_switch_buffers (idetape_tape_t *tape, idetape_stage_t *stage)
2240 {
2241         struct idetape_bh *tmp;
2242
2243         tmp = stage->bh;
2244         stage->bh = tape->merge_stage->bh;
2245         tape->merge_stage->bh = tmp;
2246         idetape_init_merge_stage(tape);
2247 }
2248
2249 /*
2250  *      idetape_add_stage_tail adds a new stage at the end of the pipeline.
2251  */
2252 static void idetape_add_stage_tail (ide_drive_t *drive,idetape_stage_t *stage)
2253 {
2254         idetape_tape_t *tape = drive->driver_data;
2255         unsigned long flags;
2256         
2257 #if IDETAPE_DEBUG_LOG
2258         if (tape->debug_level >= 4)
2259                 printk (KERN_INFO "ide-tape: Reached idetape_add_stage_tail\n");
2260 #endif /* IDETAPE_DEBUG_LOG */
2261         spin_lock_irqsave(&tape->spinlock, flags);
2262         stage->next = NULL;
2263         if (tape->last_stage != NULL)
2264                 tape->last_stage->next=stage;
2265         else
2266                 tape->first_stage = tape->next_stage=stage;
2267         tape->last_stage = stage;
2268         if (tape->next_stage == NULL)
2269                 tape->next_stage = tape->last_stage;
2270         tape->nr_stages++;
2271         tape->nr_pending_stages++;
2272         spin_unlock_irqrestore(&tape->spinlock, flags);
2273 }
2274
2275 /*
2276  *      idetape_wait_for_request installs a completion in a pending request
2277  *      and sleeps until it is serviced.
2278  *
2279  *      The caller should ensure that the request will not be serviced
2280  *      before we install the completion (usually by disabling interrupts).
2281  */
2282 static void idetape_wait_for_request (ide_drive_t *drive, struct request *rq)
2283 {
2284         DECLARE_COMPLETION_ONSTACK(wait);
2285         idetape_tape_t *tape = drive->driver_data;
2286
2287 #if IDETAPE_DEBUG_BUGS
2288         if (rq == NULL || !blk_special_request(rq)) {
2289                 printk (KERN_ERR "ide-tape: bug: Trying to sleep on non-valid request\n");
2290                 return;
2291         }
2292 #endif /* IDETAPE_DEBUG_BUGS */
2293         rq->end_io_data = &wait;
2294         rq->end_io = blk_end_sync_rq;
2295         spin_unlock_irq(&tape->spinlock);
2296         wait_for_completion(&wait);
2297         /* The stage and its struct request have been deallocated */
2298         spin_lock_irq(&tape->spinlock);
2299 }
2300
2301 static ide_startstop_t idetape_read_position_callback (ide_drive_t *drive)
2302 {
2303         idetape_tape_t *tape = drive->driver_data;
2304         idetape_read_position_result_t *result;
2305         
2306 #if IDETAPE_DEBUG_LOG
2307         if (tape->debug_level >= 4)
2308                 printk(KERN_INFO "ide-tape: Reached idetape_read_position_callback\n");
2309 #endif /* IDETAPE_DEBUG_LOG */
2310
2311         if (!tape->pc->error) {
2312                 result = (idetape_read_position_result_t *) tape->pc->buffer;
2313 #if IDETAPE_DEBUG_LOG
2314                 if (tape->debug_level >= 2)
2315                         printk(KERN_INFO "ide-tape: BOP - %s\n",result->bop ? "Yes":"No");
2316                 if (tape->debug_level >= 2)
2317                         printk(KERN_INFO "ide-tape: EOP - %s\n",result->eop ? "Yes":"No");
2318 #endif /* IDETAPE_DEBUG_LOG */
2319                 if (result->bpu) {
2320                         printk(KERN_INFO "ide-tape: Block location is unknown to the tape\n");
2321                         clear_bit(IDETAPE_ADDRESS_VALID, &tape->flags);
2322                         idetape_end_request(drive, 0, 0);
2323                 } else {
2324 #if IDETAPE_DEBUG_LOG
2325                         if (tape->debug_level >= 2)
2326                                 printk(KERN_INFO "ide-tape: Block Location - %u\n", ntohl(result->first_block));
2327 #endif /* IDETAPE_DEBUG_LOG */
2328                         tape->partition = result->partition;
2329                         tape->first_frame_position = ntohl(result->first_block);
2330                         tape->last_frame_position = ntohl(result->last_block);
2331                         tape->blocks_in_buffer = result->blocks_in_buffer[2];
2332                         set_bit(IDETAPE_ADDRESS_VALID, &tape->flags);
2333                         idetape_end_request(drive, 1, 0);
2334                 }
2335         } else {
2336                 idetape_end_request(drive, 0, 0);
2337         }
2338         return ide_stopped;
2339 }
2340
2341 /*
2342  *      idetape_create_write_filemark_cmd will:
2343  *
2344  *              1.      Write a filemark if write_filemark=1.
2345  *              2.      Flush the device buffers without writing a filemark
2346  *                      if write_filemark=0.
2347  *
2348  */
2349 static void idetape_create_write_filemark_cmd (ide_drive_t *drive, idetape_pc_t *pc,int write_filemark)
2350 {
2351         idetape_init_pc(pc);
2352         pc->c[0] = IDETAPE_WRITE_FILEMARK_CMD;
2353         pc->c[4] = write_filemark;
2354         set_bit(PC_WAIT_FOR_DSC, &pc->flags);
2355         pc->callback = &idetape_pc_callback;
2356 }
2357
2358 static void idetape_create_test_unit_ready_cmd(idetape_pc_t *pc)
2359 {
2360         idetape_init_pc(pc);
2361         pc->c[0] = IDETAPE_TEST_UNIT_READY_CMD;
2362         pc->callback = &idetape_pc_callback;
2363 }
2364
2365 /*
2366  *      idetape_queue_pc_tail is based on the following functions:
2367  *
2368  *      ide_do_drive_cmd from ide.c
2369  *      cdrom_queue_request and cdrom_queue_packet_command from ide-cd.c
2370  *
2371  *      We add a special packet command request to the tail of the request
2372  *      queue, and wait for it to be serviced.
2373  *
2374  *      This is not to be called from within the request handling part
2375  *      of the driver ! We allocate here data in the stack, and it is valid
2376  *      until the request is finished. This is not the case for the bottom
2377  *      part of the driver, where we are always leaving the functions to wait
2378  *      for an interrupt or a timer event.
2379  *
2380  *      From the bottom part of the driver, we should allocate safe memory
2381  *      using idetape_next_pc_storage and idetape_next_rq_storage, and add
2382  *      the request to the request list without waiting for it to be serviced !
2383  *      In that case, we usually use idetape_queue_pc_head.
2384  */
2385 static int __idetape_queue_pc_tail (ide_drive_t *drive, idetape_pc_t *pc)
2386 {
2387         struct ide_tape_obj *tape = drive->driver_data;
2388         struct request rq;
2389
2390         idetape_init_rq(&rq, REQ_IDETAPE_PC1);
2391         rq.buffer = (char *) pc;
2392         rq.rq_disk = tape->disk;
2393         return ide_do_drive_cmd(drive, &rq, ide_wait);
2394 }
2395
2396 static void idetape_create_load_unload_cmd (ide_drive_t *drive, idetape_pc_t *pc,int cmd)
2397 {
2398         idetape_init_pc(pc);
2399         pc->c[0] = IDETAPE_LOAD_UNLOAD_CMD;
2400         pc->c[4] = cmd;
2401         set_bit(PC_WAIT_FOR_DSC, &pc->flags);
2402         pc->callback = &idetape_pc_callback;
2403 }
2404
2405 static int idetape_wait_ready(ide_drive_t *drive, unsigned long timeout)
2406 {
2407         idetape_tape_t *tape = drive->driver_data;
2408         idetape_pc_t pc;
2409         int load_attempted = 0;
2410
2411         /*
2412          * Wait for the tape to become ready
2413          */
2414         set_bit(IDETAPE_MEDIUM_PRESENT, &tape->flags);
2415         timeout += jiffies;
2416         while (time_before(jiffies, timeout)) {
2417                 idetape_create_test_unit_ready_cmd(&pc);
2418                 if (!__idetape_queue_pc_tail(drive, &pc))
2419                         return 0;
2420                 if ((tape->sense_key == 2 && tape->asc == 4 && tape->ascq == 2)
2421                     || (tape->asc == 0x3A)) {   /* no media */
2422                         if (load_attempted)
2423                                 return -ENOMEDIUM;
2424                         idetape_create_load_unload_cmd(drive, &pc, IDETAPE_LU_LOAD_MASK);
2425                         __idetape_queue_pc_tail(drive, &pc);
2426                         load_attempted = 1;
2427                 /* not about to be ready */
2428                 } else if (!(tape->sense_key == 2 && tape->asc == 4 &&
2429                              (tape->ascq == 1 || tape->ascq == 8)))
2430                         return -EIO;
2431                 msleep(100);
2432         }
2433         return -EIO;
2434 }
2435
2436 static int idetape_queue_pc_tail (ide_drive_t *drive,idetape_pc_t *pc)
2437 {
2438         return __idetape_queue_pc_tail(drive, pc);
2439 }
2440
2441 static int idetape_flush_tape_buffers (ide_drive_t *drive)
2442 {
2443         idetape_pc_t pc;
2444         int rc;
2445
2446         idetape_create_write_filemark_cmd(drive, &pc, 0);
2447         if ((rc = idetape_queue_pc_tail(drive, &pc)))
2448                 return rc;
2449         idetape_wait_ready(drive, 60 * 5 * HZ);
2450         return 0;
2451 }
2452
2453 static void idetape_create_read_position_cmd (idetape_pc_t *pc)
2454 {
2455         idetape_init_pc(pc);
2456         pc->c[0] = IDETAPE_READ_POSITION_CMD;
2457         pc->request_transfer = 20;
2458         pc->callback = &idetape_read_position_callback;
2459 }
2460
2461 static int idetape_read_position (ide_drive_t *drive)
2462 {
2463         idetape_tape_t *tape = drive->driver_data;
2464         idetape_pc_t pc;
2465         int position;
2466
2467 #if IDETAPE_DEBUG_LOG
2468         if (tape->debug_level >= 4)
2469                 printk(KERN_INFO "ide-tape: Reached idetape_read_position\n");
2470 #endif /* IDETAPE_DEBUG_LOG */
2471
2472         idetape_create_read_position_cmd(&pc);
2473         if (idetape_queue_pc_tail(drive, &pc))
2474                 return -1;
2475         position = tape->first_frame_position;
2476         return position;
2477 }
2478
2479 static void idetape_create_locate_cmd (ide_drive_t *drive, idetape_pc_t *pc, unsigned int block, u8 partition, int skip)
2480 {
2481         idetape_init_pc(pc);
2482         pc->c[0] = IDETAPE_LOCATE_CMD;
2483         pc->c[1] = 2;
2484         put_unaligned(htonl(block), (unsigned int *) &pc->c[3]);
2485         pc->c[8] = partition;
2486         set_bit(PC_WAIT_FOR_DSC, &pc->flags);
2487         pc->callback = &idetape_pc_callback;
2488 }
2489
2490 static int idetape_create_prevent_cmd (ide_drive_t *drive, idetape_pc_t *pc, int prevent)
2491 {
2492         idetape_tape_t *tape = drive->driver_data;
2493
2494         if (!tape->capabilities.lock)
2495                 return 0;
2496
2497         idetape_init_pc(pc);
2498         pc->c[0] = IDETAPE_PREVENT_CMD;
2499         pc->c[4] = prevent;
2500         pc->callback = &idetape_pc_callback;
2501         return 1;
2502 }
2503
2504 static int __idetape_discard_read_pipeline (ide_drive_t *drive)
2505 {
2506         idetape_tape_t *tape = drive->driver_data;
2507         unsigned long flags;
2508         int cnt;
2509
2510         if (tape->chrdev_direction != idetape_direction_read)
2511                 return 0;
2512
2513         /* Remove merge stage. */
2514         cnt = tape->merge_stage_size / tape->tape_block_size;
2515         if (test_and_clear_bit(IDETAPE_FILEMARK, &tape->flags))
2516                 ++cnt;          /* Filemarks count as 1 sector */
2517         tape->merge_stage_size = 0;
2518         if (tape->merge_stage != NULL) {
2519                 __idetape_kfree_stage(tape->merge_stage);
2520                 tape->merge_stage = NULL;
2521         }
2522
2523         /* Clear pipeline flags. */
2524         clear_bit(IDETAPE_PIPELINE_ERROR, &tape->flags);
2525         tape->chrdev_direction = idetape_direction_none;
2526
2527         /* Remove pipeline stages. */
2528         if (tape->first_stage == NULL)
2529                 return 0;
2530
2531         spin_lock_irqsave(&tape->spinlock, flags);
2532         tape->next_stage = NULL;
2533         if (idetape_pipeline_active(tape))
2534                 idetape_wait_for_request(drive, tape->active_data_request);
2535         spin_unlock_irqrestore(&tape->spinlock, flags);
2536
2537         while (tape->first_stage != NULL) {
2538                 struct request *rq_ptr = &tape->first_stage->rq;
2539
2540                 cnt += rq_ptr->nr_sectors - rq_ptr->current_nr_sectors; 
2541                 if (rq_ptr->errors == IDETAPE_ERROR_FILEMARK)
2542                         ++cnt;
2543                 idetape_remove_stage_head(drive);
2544         }
2545         tape->nr_pending_stages = 0;
2546         tape->max_stages = tape->min_pipeline;
2547         return cnt;
2548 }
2549
2550 /*
2551  *      idetape_position_tape positions the tape to the requested block
2552  *      using the LOCATE packet command. A READ POSITION command is then
2553  *      issued to check where we are positioned.
2554  *
2555  *      Like all higher level operations, we queue the commands at the tail
2556  *      of the request queue and wait for their completion.
2557  *      
2558  */
2559 static int idetape_position_tape (ide_drive_t *drive, unsigned int block, u8 partition, int skip)
2560 {
2561         idetape_tape_t *tape = drive->driver_data;
2562         int retval;
2563         idetape_pc_t pc;
2564
2565         if (tape->chrdev_direction == idetape_direction_read)
2566                 __idetape_discard_read_pipeline(drive);
2567         idetape_wait_ready(drive, 60 * 5 * HZ);
2568         idetape_create_locate_cmd(drive, &pc, block, partition, skip);
2569         retval = idetape_queue_pc_tail(drive, &pc);
2570         if (retval)
2571                 return (retval);
2572
2573         idetape_create_read_position_cmd(&pc);
2574         return (idetape_queue_pc_tail(drive, &pc));
2575 }
2576
2577 static void idetape_discard_read_pipeline (ide_drive_t *drive, int restore_position)
2578 {
2579         idetape_tape_t *tape = drive->driver_data;
2580         int cnt;
2581         int seek, position;
2582
2583         cnt = __idetape_discard_read_pipeline(drive);
2584         if (restore_position) {
2585                 position = idetape_read_position(drive);
2586                 seek = position > cnt ? position - cnt : 0;
2587                 if (idetape_position_tape(drive, seek, 0, 0)) {
2588                         printk(KERN_INFO "ide-tape: %s: position_tape failed in discard_pipeline()\n", tape->name);
2589                         return;
2590                 }
2591         }
2592 }
2593
2594 /*
2595  * idetape_queue_rw_tail generates a read/write request for the block
2596  * device interface and wait for it to be serviced.
2597  */
2598 static int idetape_queue_rw_tail(ide_drive_t *drive, int cmd, int blocks, struct idetape_bh *bh)
2599 {
2600         idetape_tape_t *tape = drive->driver_data;
2601         struct request rq;
2602
2603 #if IDETAPE_DEBUG_LOG
2604         if (tape->debug_level >= 2)
2605                 printk(KERN_INFO "ide-tape: idetape_queue_rw_tail: cmd=%d\n",cmd);
2606 #endif /* IDETAPE_DEBUG_LOG */
2607 #if IDETAPE_DEBUG_BUGS
2608         if (idetape_pipeline_active(tape)) {
2609                 printk(KERN_ERR "ide-tape: bug: the pipeline is active in idetape_queue_rw_tail\n");
2610                 return (0);
2611         }
2612 #endif /* IDETAPE_DEBUG_BUGS */ 
2613
2614         idetape_init_rq(&rq, cmd);
2615         rq.rq_disk = tape->disk;
2616         rq.special = (void *)bh;
2617         rq.sector = tape->first_frame_position;
2618         rq.nr_sectors = rq.current_nr_sectors = blocks;
2619         (void) ide_do_drive_cmd(drive, &rq, ide_wait);
2620
2621         if ((cmd & (REQ_IDETAPE_READ | REQ_IDETAPE_WRITE)) == 0)
2622                 return 0;
2623
2624         if (tape->merge_stage)
2625                 idetape_init_merge_stage(tape);
2626         if (rq.errors == IDETAPE_ERROR_GENERAL)
2627                 return -EIO;
2628         return (tape->tape_block_size * (blocks-rq.current_nr_sectors));
2629 }
2630
2631 /*
2632  *      idetape_insert_pipeline_into_queue is used to start servicing the
2633  *      pipeline stages, starting from tape->next_stage.
2634  */
2635 static void idetape_insert_pipeline_into_queue (ide_drive_t *drive)
2636 {
2637         idetape_tape_t *tape = drive->driver_data;
2638
2639         if (tape->next_stage == NULL)
2640                 return;
2641         if (!idetape_pipeline_active(tape)) {
2642                 set_bit(IDETAPE_PIPELINE_ACTIVE, &tape->flags);
2643                 idetape_active_next_stage(drive);
2644                 (void) ide_do_drive_cmd(drive, tape->active_data_request, ide_end);
2645         }
2646 }
2647
2648 static void idetape_create_inquiry_cmd (idetape_pc_t *pc)
2649 {
2650         idetape_init_pc(pc);
2651         pc->c[0] = IDETAPE_INQUIRY_CMD;
2652         pc->c[4] = pc->request_transfer = 254;
2653         pc->callback = &idetape_pc_callback;
2654 }
2655
2656 static void idetape_create_rewind_cmd (ide_drive_t *drive, idetape_pc_t *pc)
2657 {
2658         idetape_init_pc(pc);
2659         pc->c[0] = IDETAPE_REWIND_CMD;
2660         set_bit(PC_WAIT_FOR_DSC, &pc->flags);
2661         pc->callback = &idetape_pc_callback;
2662 }
2663
2664 static void idetape_create_erase_cmd (idetape_pc_t *pc)
2665 {
2666         idetape_init_pc(pc);
2667         pc->c[0] = IDETAPE_ERASE_CMD;
2668         pc->c[1] = 1;
2669         set_bit(PC_WAIT_FOR_DSC, &pc->flags);
2670         pc->callback = &idetape_pc_callback;
2671 }
2672
2673 static void idetape_create_space_cmd (idetape_pc_t *pc,int count, u8 cmd)
2674 {
2675         idetape_init_pc(pc);
2676         pc->c[0] = IDETAPE_SPACE_CMD;
2677         put_unaligned(htonl(count), (unsigned int *) &pc->c[1]);
2678         pc->c[1] = cmd;
2679         set_bit(PC_WAIT_FOR_DSC, &pc->flags);
2680         pc->callback = &idetape_pc_callback;
2681 }
2682
2683 static void idetape_wait_first_stage (ide_drive_t *drive)
2684 {
2685         idetape_tape_t *tape = drive->driver_data;
2686         unsigned long flags;
2687
2688         if (tape->first_stage == NULL)
2689                 return;
2690         spin_lock_irqsave(&tape->spinlock, flags);
2691         if (tape->active_stage == tape->first_stage)
2692                 idetape_wait_for_request(drive, tape->active_data_request);
2693         spin_unlock_irqrestore(&tape->spinlock, flags);
2694 }
2695
2696 /*
2697  *      idetape_add_chrdev_write_request tries to add a character device
2698  *      originated write request to our pipeline. In case we don't succeed,
2699  *      we revert to non-pipelined operation mode for this request.
2700  *
2701  *      1.      Try to allocate a new pipeline stage.
2702  *      2.      If we can't, wait for more and more requests to be serviced
2703  *              and try again each time.
2704  *      3.      If we still can't allocate a stage, fallback to
2705  *              non-pipelined operation mode for this request.
2706  */
2707 static int idetape_add_chrdev_write_request (ide_drive_t *drive, int blocks)
2708 {
2709         idetape_tape_t *tape = drive->driver_data;
2710         idetape_stage_t *new_stage;
2711         unsigned long flags;
2712         struct request *rq;
2713
2714 #if IDETAPE_DEBUG_LOG
2715         if (tape->debug_level >= 3)
2716                 printk(KERN_INFO "ide-tape: Reached idetape_add_chrdev_write_request\n");
2717 #endif /* IDETAPE_DEBUG_LOG */
2718
2719         /*
2720          *      Attempt to allocate a new stage.
2721          *      Pay special attention to possible race conditions.
2722          */
2723         while ((new_stage = idetape_kmalloc_stage(tape)) == NULL) {
2724                 spin_lock_irqsave(&tape->spinlock, flags);
2725                 if (idetape_pipeline_active(tape)) {
2726                         idetape_wait_for_request(drive, tape->active_data_request);
2727                         spin_unlock_irqrestore(&tape->spinlock, flags);
2728                 } else {
2729                         spin_unlock_irqrestore(&tape->spinlock, flags);
2730                         idetape_insert_pipeline_into_queue(drive);
2731                         if (idetape_pipeline_active(tape))
2732                                 continue;
2733                         /*
2734                          *      Linux is short on memory. Fallback to
2735                          *      non-pipelined operation mode for this request.
2736                          */
2737                         return idetape_queue_rw_tail(drive, REQ_IDETAPE_WRITE, blocks, tape->merge_stage->bh);
2738                 }
2739         }
2740         rq = &new_stage->rq;
2741         idetape_init_rq(rq, REQ_IDETAPE_WRITE);
2742         /* Doesn't actually matter - We always assume sequential access */
2743         rq->sector = tape->first_frame_position;
2744         rq->nr_sectors = rq->current_nr_sectors = blocks;
2745
2746         idetape_switch_buffers(tape, new_stage);
2747         idetape_add_stage_tail(drive, new_stage);
2748         tape->pipeline_head++;
2749         calculate_speeds(drive);
2750
2751         /*
2752          *      Estimate whether the tape has stopped writing by checking
2753          *      if our write pipeline is currently empty. If we are not
2754          *      writing anymore, wait for the pipeline to be full enough
2755          *      (90%) before starting to service requests, so that we will
2756          *      be able to keep up with the higher speeds of the tape.
2757          */
2758         if (!idetape_pipeline_active(tape)) {
2759                 if (tape->nr_stages >= tape->max_stages * 9 / 10 ||
2760                     tape->nr_stages >= tape->max_stages - tape->uncontrolled_pipeline_head_speed * 3 * 1024 / tape->tape_block_size) {
2761                         tape->measure_insert_time = 1;
2762                         tape->insert_time = jiffies;
2763                         tape->insert_size = 0;
2764                         tape->insert_speed = 0;
2765                         idetape_insert_pipeline_into_queue(drive);
2766                 }
2767         }
2768         if (test_and_clear_bit(IDETAPE_PIPELINE_ERROR, &tape->flags))
2769                 /* Return a deferred error */
2770                 return -EIO;
2771         return blocks;
2772 }
2773
2774 /*
2775  *      idetape_wait_for_pipeline will wait until all pending pipeline
2776  *      requests are serviced. Typically called on device close.
2777  */
2778 static void idetape_wait_for_pipeline (ide_drive_t *drive)
2779 {
2780         idetape_tape_t *tape = drive->driver_data;
2781         unsigned long flags;
2782
2783         while (tape->next_stage || idetape_pipeline_active(tape)) {
2784                 idetape_insert_pipeline_into_queue(drive);
2785                 spin_lock_irqsave(&tape->spinlock, flags);
2786                 if (idetape_pipeline_active(tape))
2787                         idetape_wait_for_request(drive, tape->active_data_request);
2788                 spin_unlock_irqrestore(&tape->spinlock, flags);
2789         }
2790 }
2791
2792 static void idetape_empty_write_pipeline (ide_drive_t *drive)
2793 {
2794         idetape_tape_t *tape = drive->driver_data;
2795         int blocks, min;
2796         struct idetape_bh *bh;
2797         
2798 #if IDETAPE_DEBUG_BUGS
2799         if (tape->chrdev_direction != idetape_direction_write) {
2800                 printk(KERN_ERR "ide-tape: bug: Trying to empty write pipeline, but we are not writing.\n");
2801                 return;
2802         }
2803         if (tape->merge_stage_size > tape->stage_size) {
2804                 printk(KERN_ERR "ide-tape: bug: merge_buffer too big\n");
2805                 tape->merge_stage_size = tape->stage_size;
2806         }
2807 #endif /* IDETAPE_DEBUG_BUGS */
2808         if (tape->merge_stage_size) {
2809                 blocks = tape->merge_stage_size / tape->tape_block_size;
2810                 if (tape->merge_stage_size % tape->tape_block_size) {
2811                         unsigned int i;
2812
2813                         blocks++;
2814                         i = tape->tape_block_size - tape->merge_stage_size % tape->tape_block_size;
2815                         bh = tape->bh->b_reqnext;
2816                         while (bh) {
2817                                 atomic_set(&bh->b_count, 0);
2818                                 bh = bh->b_reqnext;
2819                         }
2820                         bh = tape->bh;
2821                         while (i) {
2822                                 if (bh == NULL) {
2823
2824                                         printk(KERN_INFO "ide-tape: bug, bh NULL\n");
2825                                         break;
2826                                 }
2827                                 min = min(i, (unsigned int)(bh->b_size - atomic_read(&bh->b_count)));
2828                                 memset(bh->b_data + atomic_read(&bh->b_count), 0, min);
2829                                 atomic_add(min, &bh->b_count);
2830                                 i -= min;
2831                                 bh = bh->b_reqnext;
2832                         }
2833                 }
2834                 (void) idetape_add_chrdev_write_request(drive, blocks);
2835                 tape->merge_stage_size = 0;
2836         }
2837         idetape_wait_for_pipeline(drive);
2838         if (tape->merge_stage != NULL) {
2839                 __idetape_kfree_stage(tape->merge_stage);
2840                 tape->merge_stage = NULL;
2841         }
2842         clear_bit(IDETAPE_PIPELINE_ERROR, &tape->flags);
2843         tape->chrdev_direction = idetape_direction_none;
2844
2845         /*
2846          *      On the next backup, perform the feedback loop again.
2847          *      (I don't want to keep sense information between backups,
2848          *       as some systems are constantly on, and the system load
2849          *       can be totally different on the next backup).
2850          */
2851         tape->max_stages = tape->min_pipeline;
2852 #if IDETAPE_DEBUG_BUGS
2853         if (tape->first_stage != NULL ||
2854             tape->next_stage != NULL ||
2855             tape->last_stage != NULL ||
2856             tape->nr_stages != 0) {
2857                 printk(KERN_ERR "ide-tape: ide-tape pipeline bug, "
2858                         "first_stage %p, next_stage %p, "
2859                         "last_stage %p, nr_stages %d\n",
2860                         tape->first_stage, tape->next_stage,
2861                         tape->last_stage, tape->nr_stages);
2862         }
2863 #endif /* IDETAPE_DEBUG_BUGS */
2864 }
2865
2866 static void idetape_restart_speed_control (ide_drive_t *drive)
2867 {
2868         idetape_tape_t *tape = drive->driver_data;
2869
2870         tape->restart_speed_control_req = 0;
2871         tape->pipeline_head = 0;
2872         tape->controlled_last_pipeline_head = tape->uncontrolled_last_pipeline_head = 0;
2873         tape->controlled_previous_pipeline_head = tape->uncontrolled_previous_pipeline_head = 0;
2874         tape->pipeline_head_speed = tape->controlled_pipeline_head_speed = 5000;
2875         tape->uncontrolled_pipeline_head_speed = 0;
2876         tape->controlled_pipeline_head_time = tape->uncontrolled_pipeline_head_time = jiffies;
2877         tape->controlled_previous_head_time = tape->uncontrolled_previous_head_time = jiffies;
2878 }
2879
2880 static int idetape_initiate_read (ide_drive_t *drive, int max_stages)
2881 {
2882         idetape_tape_t *tape = drive->driver_data;
2883         idetape_stage_t *new_stage;
2884         struct request rq;
2885         int bytes_read;
2886         int blocks = tape->capabilities.ctl;
2887
2888         /* Initialize read operation */
2889         if (tape->chrdev_direction != idetape_direction_read) {
2890                 if (tape->chrdev_direction == idetape_direction_write) {
2891                         idetape_empty_write_pipeline(drive);
2892                         idetape_flush_tape_buffers(drive);
2893                 }
2894 #if IDETAPE_DEBUG_BUGS
2895                 if (tape->merge_stage || tape->merge_stage_size) {
2896                         printk (KERN_ERR "ide-tape: merge_stage_size should be 0 now\n");
2897                         tape->merge_stage_size = 0;
2898                 }
2899 #endif /* IDETAPE_DEBUG_BUGS */
2900                 if ((tape->merge_stage = __idetape_kmalloc_stage(tape, 0, 0)) == NULL)
2901                         return -ENOMEM;
2902                 tape->chrdev_direction = idetape_direction_read;
2903
2904                 /*
2905                  *      Issue a read 0 command to ensure that DSC handshake
2906                  *      is switched from completion mode to buffer available
2907                  *      mode.
2908                  *      No point in issuing this if DSC overlap isn't supported,
2909                  *      some drives (Seagate STT3401A) will return an error.
2910                  */
2911                 if (drive->dsc_overlap) {
2912                         bytes_read = idetape_queue_rw_tail(drive, REQ_IDETAPE_READ, 0, tape->merge_stage->bh);
2913                         if (bytes_read < 0) {
2914                                 __idetape_kfree_stage(tape->merge_stage);
2915                                 tape->merge_stage = NULL;
2916                                 tape->chrdev_direction = idetape_direction_none;
2917                                 return bytes_read;
2918                         }
2919                 }
2920         }
2921         if (tape->restart_speed_control_req)
2922                 idetape_restart_speed_control(drive);
2923         idetape_init_rq(&rq, REQ_IDETAPE_READ);
2924         rq.sector = tape->first_frame_position;
2925         rq.nr_sectors = rq.current_nr_sectors = blocks;
2926         if (!test_bit(IDETAPE_PIPELINE_ERROR, &tape->flags) &&
2927             tape->nr_stages < max_stages) {
2928                 new_stage = idetape_kmalloc_stage(tape);
2929                 while (new_stage != NULL) {
2930                         new_stage->rq = rq;
2931                         idetape_add_stage_tail(drive, new_stage);
2932                         if (tape->nr_stages >= max_stages)
2933                                 break;
2934                         new_stage = idetape_kmalloc_stage(tape);
2935                 }
2936         }
2937         if (!idetape_pipeline_active(tape)) {
2938                 if (tape->nr_pending_stages >= 3 * max_stages / 4) {
2939                         tape->measure_insert_time = 1;
2940                         tape->insert_time = jiffies;
2941                         tape->insert_size = 0;
2942                         tape->insert_speed = 0;
2943                         idetape_insert_pipeline_into_queue(drive);
2944                 }
2945         }
2946         return 0;
2947 }
2948
2949 /*
2950  *      idetape_add_chrdev_read_request is called from idetape_chrdev_read
2951  *      to service a character device read request and add read-ahead
2952  *      requests to our pipeline.
2953  */
2954 static int idetape_add_chrdev_read_request (ide_drive_t *drive,int blocks)
2955 {
2956         idetape_tape_t *tape = drive->driver_data;
2957         unsigned long flags;
2958         struct request *rq_ptr;
2959         int bytes_read;
2960
2961 #if IDETAPE_DEBUG_LOG
2962         if (tape->debug_level >= 4)
2963                 printk(KERN_INFO "ide-tape: Reached idetape_add_chrdev_read_request, %d blocks\n", blocks);
2964 #endif /* IDETAPE_DEBUG_LOG */
2965
2966         /*
2967          * If we are at a filemark, return a read length of 0
2968          */
2969         if (test_bit(IDETAPE_FILEMARK, &tape->flags))
2970                 return 0;
2971
2972         /*
2973          * Wait for the next block to be available at the head
2974          * of the pipeline
2975          */
2976         idetape_initiate_read(drive, tape->max_stages);
2977         if (tape->first_stage == NULL) {
2978                 if (test_bit(IDETAPE_PIPELINE_ERROR, &tape->flags))
2979                         return 0;
2980                 return idetape_queue_rw_tail(drive, REQ_IDETAPE_READ, blocks, tape->merge_stage->bh);
2981         }
2982         idetape_wait_first_stage(drive);
2983         rq_ptr = &tape->first_stage->rq;
2984         bytes_read = tape->tape_block_size * (rq_ptr->nr_sectors - rq_ptr->current_nr_sectors);
2985         rq_ptr->nr_sectors = rq_ptr->current_nr_sectors = 0;
2986
2987
2988         if (rq_ptr->errors == IDETAPE_ERROR_EOD)
2989                 return 0;
2990         else {
2991                 idetape_switch_buffers(tape, tape->first_stage);
2992                 if (rq_ptr->errors == IDETAPE_ERROR_FILEMARK)
2993                         set_bit(IDETAPE_FILEMARK, &tape->flags);
2994                 spin_lock_irqsave(&tape->spinlock, flags);
2995                 idetape_remove_stage_head(drive);
2996                 spin_unlock_irqrestore(&tape->spinlock, flags);
2997                 tape->pipeline_head++;
2998                 calculate_speeds(drive);
2999         }
3000 #if IDETAPE_DEBUG_BUGS
3001         if (bytes_read > blocks * tape->tape_block_size) {
3002                 printk(KERN_ERR "ide-tape: bug: trying to return more bytes than requested\n");
3003                 bytes_read = blocks * tape->tape_block_size;
3004         }
3005 #endif /* IDETAPE_DEBUG_BUGS */
3006         return (bytes_read);
3007 }
3008
3009 static void idetape_pad_zeros (ide_drive_t *drive, int bcount)
3010 {
3011         idetape_tape_t *tape = drive->driver_data;
3012         struct idetape_bh *bh;
3013         int blocks;
3014         
3015         while (bcount) {
3016                 unsigned int count;
3017
3018                 bh = tape->merge_stage->bh;
3019                 count = min(tape->stage_size, bcount);
3020                 bcount -= count;
3021                 blocks = count / tape->tape_block_size;
3022                 while (count) {
3023                         atomic_set(&bh->b_count, min(count, (unsigned int)bh->b_size));
3024                         memset(bh->b_data, 0, atomic_read(&bh->b_count));
3025                         count -= atomic_read(&bh->b_count);
3026                         bh = bh->b_reqnext;
3027                 }
3028                 idetape_queue_rw_tail(drive, REQ_IDETAPE_WRITE, blocks, tape->merge_stage->bh);
3029         }
3030 }
3031
3032 static int idetape_pipeline_size (ide_drive_t *drive)
3033 {
3034         idetape_tape_t *tape = drive->driver_data;
3035         idetape_stage_t *stage;
3036         struct request *rq;
3037         int size = 0;
3038
3039         idetape_wait_for_pipeline(drive);
3040         stage = tape->first_stage;
3041         while (stage != NULL) {
3042                 rq = &stage->rq;
3043                 size += tape->tape_block_size * (rq->nr_sectors-rq->current_nr_sectors);
3044                 if (rq->errors == IDETAPE_ERROR_FILEMARK)
3045                         size += tape->tape_block_size;
3046                 stage = stage->next;
3047         }
3048         size += tape->merge_stage_size;
3049         return size;
3050 }
3051
3052 /*
3053  *      Rewinds the tape to the Beginning Of the current Partition (BOP).
3054  *
3055  *      We currently support only one partition.
3056  */ 
3057 static int idetape_rewind_tape (ide_drive_t *drive)
3058 {
3059         int retval;
3060         idetape_pc_t pc;
3061 #if IDETAPE_DEBUG_LOG
3062         idetape_tape_t *tape = drive->driver_data;
3063         if (tape->debug_level >= 2)
3064                 printk(KERN_INFO "ide-tape: Reached idetape_rewind_tape\n");
3065 #endif /* IDETAPE_DEBUG_LOG */  
3066         
3067         idetape_create_rewind_cmd(drive, &pc);
3068         retval = idetape_queue_pc_tail(drive, &pc);
3069         if (retval)
3070                 return retval;
3071
3072         idetape_create_read_position_cmd(&pc);
3073         retval = idetape_queue_pc_tail(drive, &pc);
3074         if (retval)
3075                 return retval;
3076         return 0;
3077 }
3078
3079 /*
3080  *      Our special ide-tape ioctl's.
3081  *
3082  *      Currently there aren't any ioctl's.
3083  *      mtio.h compatible commands should be issued to the character device
3084  *      interface.
3085  */
3086 static int idetape_blkdev_ioctl(ide_drive_t *drive, unsigned int cmd, unsigned long arg)
3087 {
3088         idetape_tape_t *tape = drive->driver_data;
3089         idetape_config_t config;
3090         void __user *argp = (void __user *)arg;
3091
3092 #if IDETAPE_DEBUG_LOG   
3093         if (tape->debug_level >= 4)
3094                 printk(KERN_INFO "ide-tape: Reached idetape_blkdev_ioctl\n");
3095 #endif /* IDETAPE_DEBUG_LOG */
3096         switch (cmd) {
3097                 case 0x0340:
3098                         if (copy_from_user(&config, argp, sizeof (idetape_config_t)))
3099                                 return -EFAULT;
3100                         tape->best_dsc_rw_frequency = config.dsc_rw_frequency;
3101                         tape->max_stages = config.nr_stages;
3102                         break;
3103                 case 0x0350:
3104                         config.dsc_rw_frequency = (int) tape->best_dsc_rw_frequency;
3105                         config.nr_stages = tape->max_stages; 
3106                         if (copy_to_user(argp, &config, sizeof (idetape_config_t)))
3107                                 return -EFAULT;
3108                         break;
3109                 default:
3110                         return -EIO;
3111         }
3112         return 0;
3113 }
3114
3115 /*
3116  *      idetape_space_over_filemarks is now a bit more complicated than just
3117  *      passing the command to the tape since we may have crossed some
3118  *      filemarks during our pipelined read-ahead mode.
3119  *
3120  *      As a minor side effect, the pipeline enables us to support MTFSFM when
3121  *      the filemark is in our internal pipeline even if the tape doesn't
3122  *      support spacing over filemarks in the reverse direction.
3123  */
3124 static int idetape_space_over_filemarks (ide_drive_t *drive,short mt_op,int mt_count)
3125 {
3126         idetape_tape_t *tape = drive->driver_data;
3127         idetape_pc_t pc;
3128         unsigned long flags;
3129         int retval,count=0;
3130
3131         if (mt_count == 0)
3132                 return 0;
3133         if (MTBSF == mt_op || MTBSFM == mt_op) {
3134                 if (!tape->capabilities.sprev)
3135                         return -EIO;
3136                 mt_count = - mt_count;
3137         }
3138
3139         if (tape->chrdev_direction == idetape_direction_read) {
3140                 /*
3141                  *      We have a read-ahead buffer. Scan it for crossed
3142                  *      filemarks.
3143                  */
3144                 tape->merge_stage_size = 0;
3145                 if (test_and_clear_bit(IDETAPE_FILEMARK, &tape->flags))
3146                         ++count;
3147   &nb