]> www.pilppa.org Git - linux-2.6-omap-h63xx.git/blob - drivers/ide/ide-tape.c
ide-tape: remove pipeline-specific members from struct ide_tape_obj
[linux-2.6-omap-h63xx.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.20"
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 #include <scsi/scsi.h>
40
41 #include <asm/byteorder.h>
42 #include <linux/irq.h>
43 #include <linux/uaccess.h>
44 #include <linux/io.h>
45 #include <asm/unaligned.h>
46 #include <linux/mtio.h>
47
48 enum {
49         /* output errors only */
50         DBG_ERR =               (1 << 0),
51         /* output all sense key/asc */
52         DBG_SENSE =             (1 << 1),
53         /* info regarding all chrdev-related procedures */
54         DBG_CHRDEV =            (1 << 2),
55         /* all remaining procedures */
56         DBG_PROCS =             (1 << 3),
57         /* buffer alloc info (pc_stack & rq_stack) */
58         DBG_PCRQ_STACK =        (1 << 4),
59 };
60
61 /* define to see debug info */
62 #define IDETAPE_DEBUG_LOG               0
63
64 #if IDETAPE_DEBUG_LOG
65 #define debug_log(lvl, fmt, args...)                    \
66 {                                                       \
67         if (tape->debug_mask & lvl)                     \
68         printk(KERN_INFO "ide-tape: " fmt, ## args);    \
69 }
70 #else
71 #define debug_log(lvl, fmt, args...) do {} while (0)
72 #endif
73
74 /**************************** Tunable parameters *****************************/
75
76
77 /*
78  * Pipelined mode parameters.
79  *
80  * We try to use the minimum number of stages which is enough to keep the tape
81  * constantly streaming. To accomplish that, we implement a feedback loop around
82  * the maximum number of stages:
83  *
84  * We start from MIN maximum stages (we will not even use MIN stages if we don't
85  * need them), increment it by RATE*(MAX-MIN) whenever we sense that the
86  * pipeline is empty, until we reach the optimum value or until we reach MAX.
87  */
88 #define IDETAPE_MIN_PIPELINE_STAGES       1
89 #define IDETAPE_MAX_PIPELINE_STAGES     400
90 #define IDETAPE_INCREASE_STAGES_RATE     20
91
92 /*
93  * After each failed packet command we issue a request sense command and retry
94  * the packet command IDETAPE_MAX_PC_RETRIES times.
95  *
96  * Setting IDETAPE_MAX_PC_RETRIES to 0 will disable retries.
97  */
98 #define IDETAPE_MAX_PC_RETRIES          3
99
100 /*
101  * With each packet command, we allocate a buffer of IDETAPE_PC_BUFFER_SIZE
102  * bytes. This is used for several packet commands (Not for READ/WRITE commands)
103  */
104 #define IDETAPE_PC_BUFFER_SIZE          256
105
106 /*
107  *      In various places in the driver, we need to allocate storage
108  *      for packet commands and requests, which will remain valid while
109  *      we leave the driver to wait for an interrupt or a timeout event.
110  */
111 #define IDETAPE_PC_STACK                (10 + IDETAPE_MAX_PC_RETRIES)
112
113 /*
114  * Some drives (for example, Seagate STT3401A Travan) require a very long
115  * timeout, because they don't return an interrupt or clear their busy bit
116  * until after the command completes (even retension commands).
117  */
118 #define IDETAPE_WAIT_CMD                (900*HZ)
119
120 /*
121  * The following parameter is used to select the point in the internal tape fifo
122  * in which we will start to refill the buffer. Decreasing the following
123  * parameter will improve the system's latency and interactive response, while
124  * using a high value might improve system throughput.
125  */
126 #define IDETAPE_FIFO_THRESHOLD          2
127
128 /*
129  * DSC polling parameters.
130  *
131  * Polling for DSC (a single bit in the status register) is a very important
132  * function in ide-tape. There are two cases in which we poll for DSC:
133  *
134  * 1. Before a read/write packet command, to ensure that we can transfer data
135  * from/to the tape's data buffers, without causing an actual media access.
136  * In case the tape is not ready yet, we take out our request from the device
137  * request queue, so that ide.c could service requests from the other device
138  * on the same interface in the meantime.
139  *
140  * 2. After the successful initialization of a "media access packet command",
141  * which is a command that can take a long time to complete (the interval can
142  * range from several seconds to even an hour). Again, we postpone our request
143  * in the middle to free the bus for the other device. The polling frequency
144  * here should be lower than the read/write frequency since those media access
145  * commands are slow. We start from a "fast" frequency - IDETAPE_DSC_MA_FAST
146  * (1 second), and if we don't receive DSC after IDETAPE_DSC_MA_THRESHOLD
147  * (5 min), we switch it to a lower frequency - IDETAPE_DSC_MA_SLOW (1 min).
148  *
149  * We also set a timeout for the timer, in case something goes wrong. The
150  * timeout should be longer then the maximum execution time of a tape operation.
151  */
152
153 /* DSC timings. */
154 #define IDETAPE_DSC_RW_MIN              5*HZ/100        /* 50 msec */
155 #define IDETAPE_DSC_RW_MAX              40*HZ/100       /* 400 msec */
156 #define IDETAPE_DSC_RW_TIMEOUT          2*60*HZ         /* 2 minutes */
157 #define IDETAPE_DSC_MA_FAST             2*HZ            /* 2 seconds */
158 #define IDETAPE_DSC_MA_THRESHOLD        5*60*HZ         /* 5 minutes */
159 #define IDETAPE_DSC_MA_SLOW             30*HZ           /* 30 seconds */
160 #define IDETAPE_DSC_MA_TIMEOUT          2*60*60*HZ      /* 2 hours */
161
162 /*************************** End of tunable parameters ***********************/
163
164 /* Read/Write error simulation */
165 #define SIMULATE_ERRORS                 0
166
167 /* tape directions */
168 enum {
169         IDETAPE_DIR_NONE  = (1 << 0),
170         IDETAPE_DIR_READ  = (1 << 1),
171         IDETAPE_DIR_WRITE = (1 << 2),
172 };
173
174 struct idetape_bh {
175         u32 b_size;
176         atomic_t b_count;
177         struct idetape_bh *b_reqnext;
178         char *b_data;
179 };
180
181 /* Tape door status */
182 #define DOOR_UNLOCKED                   0
183 #define DOOR_LOCKED                     1
184 #define DOOR_EXPLICITLY_LOCKED          2
185
186 /* Some defines for the SPACE command */
187 #define IDETAPE_SPACE_OVER_FILEMARK     1
188 #define IDETAPE_SPACE_TO_EOD            3
189
190 /* Some defines for the LOAD UNLOAD command */
191 #define IDETAPE_LU_LOAD_MASK            1
192 #define IDETAPE_LU_RETENSION_MASK       2
193 #define IDETAPE_LU_EOT_MASK             4
194
195 /*
196  * Special requests for our block device strategy routine.
197  *
198  * In order to service a character device command, we add special requests to
199  * the tail of our block device request queue and wait for their completion.
200  */
201
202 enum {
203         REQ_IDETAPE_PC1         = (1 << 0), /* packet command (first stage) */
204         REQ_IDETAPE_PC2         = (1 << 1), /* packet command (second stage) */
205         REQ_IDETAPE_READ        = (1 << 2),
206         REQ_IDETAPE_WRITE       = (1 << 3),
207 };
208
209 /* Error codes returned in rq->errors to the higher part of the driver. */
210 #define IDETAPE_ERROR_GENERAL           101
211 #define IDETAPE_ERROR_FILEMARK          102
212 #define IDETAPE_ERROR_EOD               103
213
214 /* Structures related to the SELECT SENSE / MODE SENSE packet commands. */
215 #define IDETAPE_BLOCK_DESCRIPTOR        0
216 #define IDETAPE_CAPABILITIES_PAGE       0x2a
217
218 /* Tape flag bits values. */
219 enum {
220         IDETAPE_FLAG_IGNORE_DSC         = (1 << 0),
221         /* 0 When the tape position is unknown */
222         IDETAPE_FLAG_ADDRESS_VALID      = (1 << 1),
223         /* Device already opened */
224         IDETAPE_FLAG_BUSY               = (1 << 2),
225         /* Attempt to auto-detect the current user block size */
226         IDETAPE_FLAG_DETECT_BS          = (1 << 3),
227         /* Currently on a filemark */
228         IDETAPE_FLAG_FILEMARK           = (1 << 4),
229         /* DRQ interrupt device */
230         IDETAPE_FLAG_DRQ_INTERRUPT      = (1 << 5),
231         /* 0 = no tape is loaded, so we don't rewind after ejecting */
232         IDETAPE_FLAG_MEDIUM_PRESENT     = (1 << 6),
233 };
234
235 /* A pipeline stage. */
236 typedef struct idetape_stage_s {
237         struct request rq;                      /* The corresponding request */
238         struct idetape_bh *bh;                  /* The data buffers */
239         struct idetape_stage_s *next;           /* Pointer to the next stage */
240 } idetape_stage_t;
241
242 /*
243  * Most of our global data which we need to save even as we leave the driver due
244  * to an interrupt or a timer event is stored in the struct defined below.
245  */
246 typedef struct ide_tape_obj {
247         ide_drive_t     *drive;
248         ide_driver_t    *driver;
249         struct gendisk  *disk;
250         struct kref     kref;
251
252         /*
253          *      Since a typical character device operation requires more
254          *      than one packet command, we provide here enough memory
255          *      for the maximum of interconnected packet commands.
256          *      The packet commands are stored in the circular array pc_stack.
257          *      pc_stack_index points to the last used entry, and warps around
258          *      to the start when we get to the last array entry.
259          *
260          *      pc points to the current processed packet command.
261          *
262          *      failed_pc points to the last failed packet command, or contains
263          *      NULL if we do not need to retry any packet command. This is
264          *      required since an additional packet command is needed before the
265          *      retry, to get detailed information on what went wrong.
266          */
267         /* Current packet command */
268         struct ide_atapi_pc *pc;
269         /* Last failed packet command */
270         struct ide_atapi_pc *failed_pc;
271         /* Packet command stack */
272         struct ide_atapi_pc pc_stack[IDETAPE_PC_STACK];
273         /* Next free packet command storage space */
274         int pc_stack_index;
275         struct request rq_stack[IDETAPE_PC_STACK];
276         /* We implement a circular array */
277         int rq_stack_index;
278
279         /*
280          * DSC polling variables.
281          *
282          * While polling for DSC we use postponed_rq to postpone the current
283          * request so that ide.c will be able to service pending requests on the
284          * other device. Note that at most we will have only one DSC (usually
285          * data transfer) request in the device request queue. Additional
286          * requests can be queued in our internal pipeline, but they will be
287          * visible to ide.c only one at a time.
288          */
289         struct request *postponed_rq;
290         /* The time in which we started polling for DSC */
291         unsigned long dsc_polling_start;
292         /* Timer used to poll for dsc */
293         struct timer_list dsc_timer;
294         /* Read/Write dsc polling frequency */
295         unsigned long best_dsc_rw_freq;
296         unsigned long dsc_poll_freq;
297         unsigned long dsc_timeout;
298
299         /* Read position information */
300         u8 partition;
301         /* Current block */
302         unsigned int first_frame;
303
304         /* Last error information */
305         u8 sense_key, asc, ascq;
306
307         /* Character device operation */
308         unsigned int minor;
309         /* device name */
310         char name[4];
311         /* Current character device data transfer direction */
312         u8 chrdev_dir;
313
314         /* tape block size, usually 512 or 1024 bytes */
315         unsigned short blk_size;
316         int user_bs_factor;
317
318         /* Copy of the tape's Capabilities and Mechanical Page */
319         u8 caps[20];
320
321         /*
322          * Active data transfer request parameters.
323          *
324          * At most, there is only one ide-tape originated data transfer request
325          * in the device request queue. This allows ide.c to easily service
326          * requests from the other device when we postpone our active request.
327          */
328
329         /* Data buffer size chosen based on the tape's recommendation */
330         int stage_size;
331         idetape_stage_t *merge_stage;
332         int merge_stage_size;
333         struct idetape_bh *bh;
334         char *b_data;
335         int b_count;
336
337         /* Pipeline parameters. */
338         int pages_per_stage;
339         /* Wasted space in each stage */
340         int excess_bh_size;
341
342         /* Status/Action flags: long for set_bit */
343         unsigned long flags;
344         /* protects the ide-tape queue */
345         spinlock_t lock;
346
347         /* Measures average tape speed */
348         unsigned long avg_time;
349         int avg_size;
350         int avg_speed;
351
352         /* the door is currently locked */
353         int door_locked;
354         /* the tape hardware is write protected */
355         char drv_write_prot;
356         /* the tape is write protected (hardware or opened as read-only) */
357         char write_prot;
358
359         u32 debug_mask;
360 } idetape_tape_t;
361
362 static DEFINE_MUTEX(idetape_ref_mutex);
363
364 static struct class *idetape_sysfs_class;
365
366 #define to_ide_tape(obj) container_of(obj, struct ide_tape_obj, kref)
367
368 #define ide_tape_g(disk) \
369         container_of((disk)->private_data, struct ide_tape_obj, driver)
370
371 static struct ide_tape_obj *ide_tape_get(struct gendisk *disk)
372 {
373         struct ide_tape_obj *tape = NULL;
374
375         mutex_lock(&idetape_ref_mutex);
376         tape = ide_tape_g(disk);
377         if (tape)
378                 kref_get(&tape->kref);
379         mutex_unlock(&idetape_ref_mutex);
380         return tape;
381 }
382
383 static void ide_tape_release(struct kref *);
384
385 static void ide_tape_put(struct ide_tape_obj *tape)
386 {
387         mutex_lock(&idetape_ref_mutex);
388         kref_put(&tape->kref, ide_tape_release);
389         mutex_unlock(&idetape_ref_mutex);
390 }
391
392 /*
393  * The variables below are used for the character device interface. Additional
394  * state variables are defined in our ide_drive_t structure.
395  */
396 static struct ide_tape_obj *idetape_devs[MAX_HWIFS * MAX_DRIVES];
397
398 #define ide_tape_f(file) ((file)->private_data)
399
400 static struct ide_tape_obj *ide_tape_chrdev_get(unsigned int i)
401 {
402         struct ide_tape_obj *tape = NULL;
403
404         mutex_lock(&idetape_ref_mutex);
405         tape = idetape_devs[i];
406         if (tape)
407                 kref_get(&tape->kref);
408         mutex_unlock(&idetape_ref_mutex);
409         return tape;
410 }
411
412 static void idetape_input_buffers(ide_drive_t *drive, struct ide_atapi_pc *pc,
413                                   unsigned int bcount)
414 {
415         struct idetape_bh *bh = pc->bh;
416         int count;
417
418         while (bcount) {
419                 if (bh == NULL) {
420                         printk(KERN_ERR "ide-tape: bh == NULL in "
421                                 "idetape_input_buffers\n");
422                         ide_atapi_discard_data(drive, bcount);
423                         return;
424                 }
425                 count = min(
426                         (unsigned int)(bh->b_size - atomic_read(&bh->b_count)),
427                         bcount);
428                 HWIF(drive)->atapi_input_bytes(drive, bh->b_data +
429                                         atomic_read(&bh->b_count), count);
430                 bcount -= count;
431                 atomic_add(count, &bh->b_count);
432                 if (atomic_read(&bh->b_count) == bh->b_size) {
433                         bh = bh->b_reqnext;
434                         if (bh)
435                                 atomic_set(&bh->b_count, 0);
436                 }
437         }
438         pc->bh = bh;
439 }
440
441 static void idetape_output_buffers(ide_drive_t *drive, struct ide_atapi_pc *pc,
442                                    unsigned int bcount)
443 {
444         struct idetape_bh *bh = pc->bh;
445         int count;
446
447         while (bcount) {
448                 if (bh == NULL) {
449                         printk(KERN_ERR "ide-tape: bh == NULL in %s\n",
450                                         __func__);
451                         return;
452                 }
453                 count = min((unsigned int)pc->b_count, (unsigned int)bcount);
454                 HWIF(drive)->atapi_output_bytes(drive, pc->b_data, count);
455                 bcount -= count;
456                 pc->b_data += count;
457                 pc->b_count -= count;
458                 if (!pc->b_count) {
459                         bh = bh->b_reqnext;
460                         pc->bh = bh;
461                         if (bh) {
462                                 pc->b_data = bh->b_data;
463                                 pc->b_count = atomic_read(&bh->b_count);
464                         }
465                 }
466         }
467 }
468
469 static void idetape_update_buffers(struct ide_atapi_pc *pc)
470 {
471         struct idetape_bh *bh = pc->bh;
472         int count;
473         unsigned int bcount = pc->xferred;
474
475         if (pc->flags & PC_FLAG_WRITING)
476                 return;
477         while (bcount) {
478                 if (bh == NULL) {
479                         printk(KERN_ERR "ide-tape: bh == NULL in %s\n",
480                                         __func__);
481                         return;
482                 }
483                 count = min((unsigned int)bh->b_size, (unsigned int)bcount);
484                 atomic_set(&bh->b_count, count);
485                 if (atomic_read(&bh->b_count) == bh->b_size)
486                         bh = bh->b_reqnext;
487                 bcount -= count;
488         }
489         pc->bh = bh;
490 }
491
492 /*
493  *      idetape_next_pc_storage returns a pointer to a place in which we can
494  *      safely store a packet command, even though we intend to leave the
495  *      driver. A storage space for a maximum of IDETAPE_PC_STACK packet
496  *      commands is allocated at initialization time.
497  */
498 static struct ide_atapi_pc *idetape_next_pc_storage(ide_drive_t *drive)
499 {
500         idetape_tape_t *tape = drive->driver_data;
501
502         debug_log(DBG_PCRQ_STACK, "pc_stack_index=%d\n", tape->pc_stack_index);
503
504         if (tape->pc_stack_index == IDETAPE_PC_STACK)
505                 tape->pc_stack_index = 0;
506         return (&tape->pc_stack[tape->pc_stack_index++]);
507 }
508
509 /*
510  *      idetape_next_rq_storage is used along with idetape_next_pc_storage.
511  *      Since we queue packet commands in the request queue, we need to
512  *      allocate a request, along with the allocation of a packet command.
513  */
514
515 /**************************************************************
516  *                                                            *
517  *  This should get fixed to use kmalloc(.., GFP_ATOMIC)      *
518  *  followed later on by kfree().   -ml                       *
519  *                                                            *
520  **************************************************************/
521
522 static struct request *idetape_next_rq_storage(ide_drive_t *drive)
523 {
524         idetape_tape_t *tape = drive->driver_data;
525
526         debug_log(DBG_PCRQ_STACK, "rq_stack_index=%d\n", tape->rq_stack_index);
527
528         if (tape->rq_stack_index == IDETAPE_PC_STACK)
529                 tape->rq_stack_index = 0;
530         return (&tape->rq_stack[tape->rq_stack_index++]);
531 }
532
533 static void idetape_init_pc(struct ide_atapi_pc *pc)
534 {
535         memset(pc->c, 0, 12);
536         pc->retries = 0;
537         pc->flags = 0;
538         pc->req_xfer = 0;
539         pc->buf = pc->pc_buf;
540         pc->buf_size = IDETAPE_PC_BUFFER_SIZE;
541         pc->bh = NULL;
542         pc->b_data = NULL;
543 }
544
545 /*
546  * called on each failed packet command retry to analyze the request sense. We
547  * currently do not utilize this information.
548  */
549 static void idetape_analyze_error(ide_drive_t *drive, u8 *sense)
550 {
551         idetape_tape_t *tape = drive->driver_data;
552         struct ide_atapi_pc *pc = tape->failed_pc;
553
554         tape->sense_key = sense[2] & 0xF;
555         tape->asc       = sense[12];
556         tape->ascq      = sense[13];
557
558         debug_log(DBG_ERR, "pc = %x, sense key = %x, asc = %x, ascq = %x\n",
559                  pc->c[0], tape->sense_key, tape->asc, tape->ascq);
560
561         /* Correct pc->xferred by asking the tape.       */
562         if (pc->flags & PC_FLAG_DMA_ERROR) {
563                 pc->xferred = pc->req_xfer -
564                         tape->blk_size *
565                         be32_to_cpu(get_unaligned((u32 *)&sense[3]));
566                 idetape_update_buffers(pc);
567         }
568
569         /*
570          * If error was the result of a zero-length read or write command,
571          * with sense key=5, asc=0x22, ascq=0, let it slide.  Some drives
572          * (i.e. Seagate STT3401A Travan) don't support 0-length read/writes.
573          */
574         if ((pc->c[0] == READ_6 || pc->c[0] == WRITE_6)
575             /* length == 0 */
576             && pc->c[4] == 0 && pc->c[3] == 0 && pc->c[2] == 0) {
577                 if (tape->sense_key == 5) {
578                         /* don't report an error, everything's ok */
579                         pc->error = 0;
580                         /* don't retry read/write */
581                         pc->flags |= PC_FLAG_ABORT;
582                 }
583         }
584         if (pc->c[0] == READ_6 && (sense[2] & 0x80)) {
585                 pc->error = IDETAPE_ERROR_FILEMARK;
586                 pc->flags |= PC_FLAG_ABORT;
587         }
588         if (pc->c[0] == WRITE_6) {
589                 if ((sense[2] & 0x40) || (tape->sense_key == 0xd
590                      && tape->asc == 0x0 && tape->ascq == 0x2)) {
591                         pc->error = IDETAPE_ERROR_EOD;
592                         pc->flags |= PC_FLAG_ABORT;
593                 }
594         }
595         if (pc->c[0] == READ_6 || pc->c[0] == WRITE_6) {
596                 if (tape->sense_key == 8) {
597                         pc->error = IDETAPE_ERROR_EOD;
598                         pc->flags |= PC_FLAG_ABORT;
599                 }
600                 if (!(pc->flags & PC_FLAG_ABORT) &&
601                     pc->xferred)
602                         pc->retries = IDETAPE_MAX_PC_RETRIES + 1;
603         }
604 }
605
606 /* Free a stage along with its related buffers completely. */
607 static void __idetape_kfree_stage(idetape_stage_t *stage)
608 {
609         struct idetape_bh *prev_bh, *bh = stage->bh;
610         int size;
611
612         while (bh != NULL) {
613                 if (bh->b_data != NULL) {
614                         size = (int) bh->b_size;
615                         while (size > 0) {
616                                 free_page((unsigned long) bh->b_data);
617                                 size -= PAGE_SIZE;
618                                 bh->b_data += PAGE_SIZE;
619                         }
620                 }
621                 prev_bh = bh;
622                 bh = bh->b_reqnext;
623                 kfree(prev_bh);
624         }
625         kfree(stage);
626 }
627
628 /*
629  * Finish servicing a request and insert a pending pipeline request into the
630  * main device queue.
631  */
632 static int idetape_end_request(ide_drive_t *drive, int uptodate, int nr_sects)
633 {
634         struct request *rq = HWGROUP(drive)->rq;
635         idetape_tape_t *tape = drive->driver_data;
636         unsigned long flags;
637         int error;
638
639         debug_log(DBG_PROCS, "Enter %s\n", __func__);
640
641         switch (uptodate) {
642         case 0: error = IDETAPE_ERROR_GENERAL; break;
643         case 1: error = 0; break;
644         default: error = uptodate;
645         }
646         rq->errors = error;
647         if (error)
648                 tape->failed_pc = NULL;
649
650         if (!blk_special_request(rq)) {
651                 ide_end_request(drive, uptodate, nr_sects);
652                 return 0;
653         }
654
655         spin_lock_irqsave(&tape->lock, flags);
656
657         ide_end_drive_cmd(drive, 0, 0);
658
659         spin_unlock_irqrestore(&tape->lock, flags);
660         return 0;
661 }
662
663 static ide_startstop_t idetape_request_sense_callback(ide_drive_t *drive)
664 {
665         idetape_tape_t *tape = drive->driver_data;
666
667         debug_log(DBG_PROCS, "Enter %s\n", __func__);
668
669         if (!tape->pc->error) {
670                 idetape_analyze_error(drive, tape->pc->buf);
671                 idetape_end_request(drive, 1, 0);
672         } else {
673                 printk(KERN_ERR "ide-tape: Error in REQUEST SENSE itself - "
674                                 "Aborting request!\n");
675                 idetape_end_request(drive, 0, 0);
676         }
677         return ide_stopped;
678 }
679
680 static void idetape_create_request_sense_cmd(struct ide_atapi_pc *pc)
681 {
682         idetape_init_pc(pc);
683         pc->c[0] = REQUEST_SENSE;
684         pc->c[4] = 20;
685         pc->req_xfer = 20;
686         pc->idetape_callback = &idetape_request_sense_callback;
687 }
688
689 static void idetape_init_rq(struct request *rq, u8 cmd)
690 {
691         memset(rq, 0, sizeof(*rq));
692         rq->cmd_type = REQ_TYPE_SPECIAL;
693         rq->cmd[0] = cmd;
694 }
695
696 /*
697  * Generate a new packet command request in front of the request queue, before
698  * the current request, so that it will be processed immediately, on the next
699  * pass through the driver. The function below is called from the request
700  * handling part of the driver (the "bottom" part). Safe storage for the request
701  * should be allocated with ide_tape_next_{pc,rq}_storage() prior to that.
702  *
703  * Memory for those requests is pre-allocated at initialization time, and is
704  * limited to IDETAPE_PC_STACK requests. We assume that we have enough space for
705  * the maximum possible number of inter-dependent packet commands.
706  *
707  * The higher level of the driver - The ioctl handler and the character device
708  * handling functions should queue request to the lower level part and wait for
709  * their completion using idetape_queue_pc_tail or idetape_queue_rw_tail.
710  */
711 static void idetape_queue_pc_head(ide_drive_t *drive, struct ide_atapi_pc *pc,
712                                   struct request *rq)
713 {
714         struct ide_tape_obj *tape = drive->driver_data;
715
716         idetape_init_rq(rq, REQ_IDETAPE_PC1);
717         rq->buffer = (char *) pc;
718         rq->rq_disk = tape->disk;
719         (void) ide_do_drive_cmd(drive, rq, ide_preempt);
720 }
721
722 /*
723  *      idetape_retry_pc is called when an error was detected during the
724  *      last packet command. We queue a request sense packet command in
725  *      the head of the request list.
726  */
727 static ide_startstop_t idetape_retry_pc (ide_drive_t *drive)
728 {
729         idetape_tape_t *tape = drive->driver_data;
730         struct ide_atapi_pc *pc;
731         struct request *rq;
732
733         (void)ide_read_error(drive);
734         pc = idetape_next_pc_storage(drive);
735         rq = idetape_next_rq_storage(drive);
736         idetape_create_request_sense_cmd(pc);
737         set_bit(IDETAPE_FLAG_IGNORE_DSC, &tape->flags);
738         idetape_queue_pc_head(drive, pc, rq);
739         return ide_stopped;
740 }
741
742 /*
743  * Postpone the current request so that ide.c will be able to service requests
744  * from another device on the same hwgroup while we are polling for DSC.
745  */
746 static void idetape_postpone_request(ide_drive_t *drive)
747 {
748         idetape_tape_t *tape = drive->driver_data;
749
750         debug_log(DBG_PROCS, "Enter %s\n", __func__);
751
752         tape->postponed_rq = HWGROUP(drive)->rq;
753         ide_stall_queue(drive, tape->dsc_poll_freq);
754 }
755
756 typedef void idetape_io_buf(ide_drive_t *, struct ide_atapi_pc *, unsigned int);
757
758 /*
759  * This is the usual interrupt handler which will be called during a packet
760  * command. We will transfer some of the data (as requested by the drive) and
761  * will re-point interrupt handler to us. When data transfer is finished, we
762  * will act according to the algorithm described before
763  * idetape_issue_pc.
764  */
765 static ide_startstop_t idetape_pc_intr(ide_drive_t *drive)
766 {
767         ide_hwif_t *hwif = drive->hwif;
768         idetape_tape_t *tape = drive->driver_data;
769         struct ide_atapi_pc *pc = tape->pc;
770         xfer_func_t *xferfunc;
771         idetape_io_buf *iobuf;
772         unsigned int temp;
773 #if SIMULATE_ERRORS
774         static int error_sim_count;
775 #endif
776         u16 bcount;
777         u8 stat, ireason;
778
779         debug_log(DBG_PROCS, "Enter %s - interrupt handler\n", __func__);
780
781         /* Clear the interrupt */
782         stat = ide_read_status(drive);
783
784         if (pc->flags & PC_FLAG_DMA_IN_PROGRESS) {
785                 if (hwif->dma_ops->dma_end(drive) || (stat & ERR_STAT)) {
786                         /*
787                          * A DMA error is sometimes expected. For example,
788                          * if the tape is crossing a filemark during a
789                          * READ command, it will issue an irq and position
790                          * itself before the filemark, so that only a partial
791                          * data transfer will occur (which causes the DMA
792                          * error). In that case, we will later ask the tape
793                          * how much bytes of the original request were
794                          * actually transferred (we can't receive that
795                          * information from the DMA engine on most chipsets).
796                          */
797
798                         /*
799                          * On the contrary, a DMA error is never expected;
800                          * it usually indicates a hardware error or abort.
801                          * If the tape crosses a filemark during a READ
802                          * command, it will issue an irq and position itself
803                          * after the filemark (not before). Only a partial
804                          * data transfer will occur, but no DMA error.
805                          * (AS, 19 Apr 2001)
806                          */
807                         pc->flags |= PC_FLAG_DMA_ERROR;
808                 } else {
809                         pc->xferred = pc->req_xfer;
810                         idetape_update_buffers(pc);
811                 }
812                 debug_log(DBG_PROCS, "DMA finished\n");
813
814         }
815
816         /* No more interrupts */
817         if ((stat & DRQ_STAT) == 0) {
818                 debug_log(DBG_SENSE, "Packet command completed, %d bytes"
819                                 " transferred\n", pc->xferred);
820
821                 pc->flags &= ~PC_FLAG_DMA_IN_PROGRESS;
822                 local_irq_enable();
823
824 #if SIMULATE_ERRORS
825                 if ((pc->c[0] == WRITE_6 || pc->c[0] == READ_6) &&
826                     (++error_sim_count % 100) == 0) {
827                         printk(KERN_INFO "ide-tape: %s: simulating error\n",
828                                 tape->name);
829                         stat |= ERR_STAT;
830                 }
831 #endif
832                 if ((stat & ERR_STAT) && pc->c[0] == REQUEST_SENSE)
833                         stat &= ~ERR_STAT;
834                 if ((stat & ERR_STAT) || (pc->flags & PC_FLAG_DMA_ERROR)) {
835                         /* Error detected */
836                         debug_log(DBG_ERR, "%s: I/O error\n", tape->name);
837
838                         if (pc->c[0] == REQUEST_SENSE) {
839                                 printk(KERN_ERR "ide-tape: I/O error in request"
840                                                 " sense command\n");
841                                 return ide_do_reset(drive);
842                         }
843                         debug_log(DBG_ERR, "[cmd %x]: check condition\n",
844                                         pc->c[0]);
845
846                         /* Retry operation */
847                         return idetape_retry_pc(drive);
848                 }
849                 pc->error = 0;
850                 if ((pc->flags & PC_FLAG_WAIT_FOR_DSC) &&
851                     (stat & SEEK_STAT) == 0) {
852                         /* Media access command */
853                         tape->dsc_polling_start = jiffies;
854                         tape->dsc_poll_freq = IDETAPE_DSC_MA_FAST;
855                         tape->dsc_timeout = jiffies + IDETAPE_DSC_MA_TIMEOUT;
856                         /* Allow ide.c to handle other requests */
857                         idetape_postpone_request(drive);
858                         return ide_stopped;
859                 }
860                 if (tape->failed_pc == pc)
861                         tape->failed_pc = NULL;
862                 /* Command finished - Call the callback function */
863                 return pc->idetape_callback(drive);
864         }
865
866         if (pc->flags & PC_FLAG_DMA_IN_PROGRESS) {
867                 pc->flags &= ~PC_FLAG_DMA_IN_PROGRESS;
868                 printk(KERN_ERR "ide-tape: The tape wants to issue more "
869                                 "interrupts in DMA mode\n");
870                 printk(KERN_ERR "ide-tape: DMA disabled, reverting to PIO\n");
871                 ide_dma_off(drive);
872                 return ide_do_reset(drive);
873         }
874         /* Get the number of bytes to transfer on this interrupt. */
875         bcount = (hwif->INB(hwif->io_ports[IDE_BCOUNTH_OFFSET]) << 8) |
876                   hwif->INB(hwif->io_ports[IDE_BCOUNTL_OFFSET]);
877
878         ireason = hwif->INB(hwif->io_ports[IDE_IREASON_OFFSET]);
879
880         if (ireason & CD) {
881                 printk(KERN_ERR "ide-tape: CoD != 0 in %s\n", __func__);
882                 return ide_do_reset(drive);
883         }
884         if (((ireason & IO) == IO) == !!(pc->flags & PC_FLAG_WRITING)) {
885                 /* Hopefully, we will never get here */
886                 printk(KERN_ERR "ide-tape: We wanted to %s, ",
887                                 (ireason & IO) ? "Write" : "Read");
888                 printk(KERN_ERR "ide-tape: but the tape wants us to %s !\n",
889                                 (ireason & IO) ? "Read" : "Write");
890                 return ide_do_reset(drive);
891         }
892         if (!(pc->flags & PC_FLAG_WRITING)) {
893                 /* Reading - Check that we have enough space */
894                 temp = pc->xferred + bcount;
895                 if (temp > pc->req_xfer) {
896                         if (temp > pc->buf_size) {
897                                 printk(KERN_ERR "ide-tape: The tape wants to "
898                                         "send us more data than expected "
899                                         "- discarding data\n");
900                                 ide_atapi_discard_data(drive, bcount);
901                                 ide_set_handler(drive, &idetape_pc_intr,
902                                                 IDETAPE_WAIT_CMD, NULL);
903                                 return ide_started;
904                         }
905                         debug_log(DBG_SENSE, "The tape wants to send us more "
906                                 "data than expected - allowing transfer\n");
907                 }
908                 iobuf = &idetape_input_buffers;
909                 xferfunc = hwif->atapi_input_bytes;
910         } else {
911                 iobuf = &idetape_output_buffers;
912                 xferfunc = hwif->atapi_output_bytes;
913         }
914
915         if (pc->bh)
916                 iobuf(drive, pc, bcount);
917         else
918                 xferfunc(drive, pc->cur_pos, bcount);
919
920         /* Update the current position */
921         pc->xferred += bcount;
922         pc->cur_pos += bcount;
923
924         debug_log(DBG_SENSE, "[cmd %x] transferred %d bytes on that intr.\n",
925                         pc->c[0], bcount);
926
927         /* And set the interrupt handler again */
928         ide_set_handler(drive, &idetape_pc_intr, IDETAPE_WAIT_CMD, NULL);
929         return ide_started;
930 }
931
932 /*
933  * Packet Command Interface
934  *
935  * The current Packet Command is available in tape->pc, and will not change
936  * until we finish handling it. Each packet command is associated with a
937  * callback function that will be called when the command is finished.
938  *
939  * The handling will be done in three stages:
940  *
941  * 1. idetape_issue_pc will send the packet command to the drive, and will set
942  * the interrupt handler to idetape_pc_intr.
943  *
944  * 2. On each interrupt, idetape_pc_intr will be called. This step will be
945  * repeated until the device signals us that no more interrupts will be issued.
946  *
947  * 3. ATAPI Tape media access commands have immediate status with a delayed
948  * process. In case of a successful initiation of a media access packet command,
949  * the DSC bit will be set when the actual execution of the command is finished.
950  * Since the tape drive will not issue an interrupt, we have to poll for this
951  * event. In this case, we define the request as "low priority request" by
952  * setting rq_status to IDETAPE_RQ_POSTPONED, set a timer to poll for DSC and
953  * exit the driver.
954  *
955  * ide.c will then give higher priority to requests which originate from the
956  * other device, until will change rq_status to RQ_ACTIVE.
957  *
958  * 4. When the packet command is finished, it will be checked for errors.
959  *
960  * 5. In case an error was found, we queue a request sense packet command in
961  * front of the request queue and retry the operation up to
962  * IDETAPE_MAX_PC_RETRIES times.
963  *
964  * 6. In case no error was found, or we decided to give up and not to retry
965  * again, the callback function will be called and then we will handle the next
966  * request.
967  */
968 static ide_startstop_t idetape_transfer_pc(ide_drive_t *drive)
969 {
970         ide_hwif_t *hwif = drive->hwif;
971         idetape_tape_t *tape = drive->driver_data;
972         struct ide_atapi_pc *pc = tape->pc;
973         int retries = 100;
974         ide_startstop_t startstop;
975         u8 ireason;
976
977         if (ide_wait_stat(&startstop, drive, DRQ_STAT, BUSY_STAT, WAIT_READY)) {
978                 printk(KERN_ERR "ide-tape: Strange, packet command initiated "
979                                 "yet DRQ isn't asserted\n");
980                 return startstop;
981         }
982         ireason = hwif->INB(hwif->io_ports[IDE_IREASON_OFFSET]);
983         while (retries-- && ((ireason & CD) == 0 || (ireason & IO))) {
984                 printk(KERN_ERR "ide-tape: (IO,CoD != (0,1) while issuing "
985                                 "a packet command, retrying\n");
986                 udelay(100);
987                 ireason = hwif->INB(hwif->io_ports[IDE_IREASON_OFFSET]);
988                 if (retries == 0) {
989                         printk(KERN_ERR "ide-tape: (IO,CoD != (0,1) while "
990                                         "issuing a packet command, ignoring\n");
991                         ireason |= CD;
992                         ireason &= ~IO;
993                 }
994         }
995         if ((ireason & CD) == 0 || (ireason & IO)) {
996                 printk(KERN_ERR "ide-tape: (IO,CoD) != (0,1) while issuing "
997                                 "a packet command\n");
998                 return ide_do_reset(drive);
999         }
1000         /* Set the interrupt routine */
1001         ide_set_handler(drive, &idetape_pc_intr, IDETAPE_WAIT_CMD, NULL);
1002 #ifdef CONFIG_BLK_DEV_IDEDMA
1003         /* Begin DMA, if necessary */
1004         if (pc->flags & PC_FLAG_DMA_IN_PROGRESS)
1005                 hwif->dma_ops->dma_start(drive);
1006 #endif
1007         /* Send the actual packet */
1008         HWIF(drive)->atapi_output_bytes(drive, pc->c, 12);
1009         return ide_started;
1010 }
1011
1012 static ide_startstop_t idetape_issue_pc(ide_drive_t *drive,
1013                 struct ide_atapi_pc *pc)
1014 {
1015         ide_hwif_t *hwif = drive->hwif;
1016         idetape_tape_t *tape = drive->driver_data;
1017         int dma_ok = 0;
1018         u16 bcount;
1019
1020         if (tape->pc->c[0] == REQUEST_SENSE &&
1021             pc->c[0] == REQUEST_SENSE) {
1022                 printk(KERN_ERR "ide-tape: possible ide-tape.c bug - "
1023                         "Two request sense in serial were issued\n");
1024         }
1025
1026         if (tape->failed_pc == NULL && pc->c[0] != REQUEST_SENSE)
1027                 tape->failed_pc = pc;
1028         /* Set the current packet command */
1029         tape->pc = pc;
1030
1031         if (pc->retries > IDETAPE_MAX_PC_RETRIES ||
1032                 (pc->flags & PC_FLAG_ABORT)) {
1033                 /*
1034                  * We will "abort" retrying a packet command in case legitimate
1035                  * error code was received (crossing a filemark, or end of the
1036                  * media, for example).
1037                  */
1038                 if (!(pc->flags & PC_FLAG_ABORT)) {
1039                         if (!(pc->c[0] == TEST_UNIT_READY &&
1040                               tape->sense_key == 2 && tape->asc == 4 &&
1041                              (tape->ascq == 1 || tape->ascq == 8))) {
1042                                 printk(KERN_ERR "ide-tape: %s: I/O error, "
1043                                                 "pc = %2x, key = %2x, "
1044                                                 "asc = %2x, ascq = %2x\n",
1045                                                 tape->name, pc->c[0],
1046                                                 tape->sense_key, tape->asc,
1047                                                 tape->ascq);
1048                         }
1049                         /* Giving up */
1050                         pc->error = IDETAPE_ERROR_GENERAL;
1051                 }
1052                 tape->failed_pc = NULL;
1053                 return pc->idetape_callback(drive);
1054         }
1055         debug_log(DBG_SENSE, "Retry #%d, cmd = %02X\n", pc->retries, pc->c[0]);
1056
1057         pc->retries++;
1058         /* We haven't transferred any data yet */
1059         pc->xferred = 0;
1060         pc->cur_pos = pc->buf;
1061         /* Request to transfer the entire buffer at once */
1062         bcount = pc->req_xfer;
1063
1064         if (pc->flags & PC_FLAG_DMA_ERROR) {
1065                 pc->flags &= ~PC_FLAG_DMA_ERROR;
1066                 printk(KERN_WARNING "ide-tape: DMA disabled, "
1067                                 "reverting to PIO\n");
1068                 ide_dma_off(drive);
1069         }
1070         if ((pc->flags & PC_FLAG_DMA_RECOMMENDED) && drive->using_dma)
1071                 dma_ok = !hwif->dma_ops->dma_setup(drive);
1072
1073         ide_pktcmd_tf_load(drive, IDE_TFLAG_NO_SELECT_MASK |
1074                            IDE_TFLAG_OUT_DEVICE, bcount, dma_ok);
1075
1076         if (dma_ok)
1077                 /* Will begin DMA later */
1078                 pc->flags |= PC_FLAG_DMA_IN_PROGRESS;
1079         if (test_bit(IDETAPE_FLAG_DRQ_INTERRUPT, &tape->flags)) {
1080                 ide_execute_command(drive, WIN_PACKETCMD, &idetape_transfer_pc,
1081                                     IDETAPE_WAIT_CMD, NULL);
1082                 return ide_started;
1083         } else {
1084                 hwif->OUTB(WIN_PACKETCMD, hwif->io_ports[IDE_COMMAND_OFFSET]);
1085                 return idetape_transfer_pc(drive);
1086         }
1087 }
1088
1089 static ide_startstop_t idetape_pc_callback(ide_drive_t *drive)
1090 {
1091         idetape_tape_t *tape = drive->driver_data;
1092
1093         debug_log(DBG_PROCS, "Enter %s\n", __func__);
1094
1095         idetape_end_request(drive, tape->pc->error ? 0 : 1, 0);
1096         return ide_stopped;
1097 }
1098
1099 /* A mode sense command is used to "sense" tape parameters. */
1100 static void idetape_create_mode_sense_cmd(struct ide_atapi_pc *pc, u8 page_code)
1101 {
1102         idetape_init_pc(pc);
1103         pc->c[0] = MODE_SENSE;
1104         if (page_code != IDETAPE_BLOCK_DESCRIPTOR)
1105                 /* DBD = 1 - Don't return block descriptors */
1106                 pc->c[1] = 8;
1107         pc->c[2] = page_code;
1108         /*
1109          * Changed pc->c[3] to 0 (255 will at best return unused info).
1110          *
1111          * For SCSI this byte is defined as subpage instead of high byte
1112          * of length and some IDE drives seem to interpret it this way
1113          * and return an error when 255 is used.
1114          */
1115         pc->c[3] = 0;
1116         /* We will just discard data in that case */
1117         pc->c[4] = 255;
1118         if (page_code == IDETAPE_BLOCK_DESCRIPTOR)
1119                 pc->req_xfer = 12;
1120         else if (page_code == IDETAPE_CAPABILITIES_PAGE)
1121                 pc->req_xfer = 24;
1122         else
1123                 pc->req_xfer = 50;
1124         pc->idetape_callback = &idetape_pc_callback;
1125 }
1126
1127 static ide_startstop_t idetape_media_access_finished(ide_drive_t *drive)
1128 {
1129         idetape_tape_t *tape = drive->driver_data;
1130         struct ide_atapi_pc *pc = tape->pc;
1131         u8 stat;
1132
1133         stat = ide_read_status(drive);
1134
1135         if (stat & SEEK_STAT) {
1136                 if (stat & ERR_STAT) {
1137                         /* Error detected */
1138                         if (pc->c[0] != TEST_UNIT_READY)
1139                                 printk(KERN_ERR "ide-tape: %s: I/O error, ",
1140                                                 tape->name);
1141                         /* Retry operation */
1142                         return idetape_retry_pc(drive);
1143                 }
1144                 pc->error = 0;
1145                 if (tape->failed_pc == pc)
1146                         tape->failed_pc = NULL;
1147         } else {
1148                 pc->error = IDETAPE_ERROR_GENERAL;
1149                 tape->failed_pc = NULL;
1150         }
1151         return pc->idetape_callback(drive);
1152 }
1153
1154 static ide_startstop_t idetape_rw_callback(ide_drive_t *drive)
1155 {
1156         idetape_tape_t *tape = drive->driver_data;
1157         struct request *rq = HWGROUP(drive)->rq;
1158         int blocks = tape->pc->xferred / tape->blk_size;
1159
1160         tape->avg_size += blocks * tape->blk_size;
1161
1162         if (time_after_eq(jiffies, tape->avg_time + HZ)) {
1163                 tape->avg_speed = tape->avg_size * HZ /
1164                                 (jiffies - tape->avg_time) / 1024;
1165                 tape->avg_size = 0;
1166                 tape->avg_time = jiffies;
1167         }
1168         debug_log(DBG_PROCS, "Enter %s\n", __func__);
1169
1170         tape->first_frame += blocks;
1171         rq->current_nr_sectors -= blocks;
1172
1173         if (!tape->pc->error)
1174                 idetape_end_request(drive, 1, 0);
1175         else
1176                 idetape_end_request(drive, tape->pc->error, 0);
1177         return ide_stopped;
1178 }
1179
1180 static void idetape_create_read_cmd(idetape_tape_t *tape,
1181                 struct ide_atapi_pc *pc,
1182                 unsigned int length, struct idetape_bh *bh)
1183 {
1184         idetape_init_pc(pc);
1185         pc->c[0] = READ_6;
1186         put_unaligned(cpu_to_be32(length), (unsigned int *) &pc->c[1]);
1187         pc->c[1] = 1;
1188         pc->idetape_callback = &idetape_rw_callback;
1189         pc->bh = bh;
1190         atomic_set(&bh->b_count, 0);
1191         pc->buf = NULL;
1192         pc->buf_size = length * tape->blk_size;
1193         pc->req_xfer = pc->buf_size;
1194         if (pc->req_xfer == tape->stage_size)
1195                 pc->flags |= PC_FLAG_DMA_RECOMMENDED;
1196 }
1197
1198 static void idetape_create_write_cmd(idetape_tape_t *tape,
1199                 struct ide_atapi_pc *pc,
1200                 unsigned int length, struct idetape_bh *bh)
1201 {
1202         idetape_init_pc(pc);
1203         pc->c[0] = WRITE_6;
1204         put_unaligned(cpu_to_be32(length), (unsigned int *) &pc->c[1]);
1205         pc->c[1] = 1;
1206         pc->idetape_callback = &idetape_rw_callback;
1207         pc->flags |= PC_FLAG_WRITING;
1208         pc->bh = bh;
1209         pc->b_data = bh->b_data;
1210         pc->b_count = atomic_read(&bh->b_count);
1211         pc->buf = NULL;
1212         pc->buf_size = length * tape->blk_size;
1213         pc->req_xfer = pc->buf_size;
1214         if (pc->req_xfer == tape->stage_size)
1215                 pc->flags |= PC_FLAG_DMA_RECOMMENDED;
1216 }
1217
1218 static ide_startstop_t idetape_do_request(ide_drive_t *drive,
1219                                           struct request *rq, sector_t block)
1220 {
1221         idetape_tape_t *tape = drive->driver_data;
1222         struct ide_atapi_pc *pc = NULL;
1223         struct request *postponed_rq = tape->postponed_rq;
1224         u8 stat;
1225
1226         debug_log(DBG_SENSE, "sector: %ld, nr_sectors: %ld,"
1227                         " current_nr_sectors: %d\n",
1228                         rq->sector, rq->nr_sectors, rq->current_nr_sectors);
1229
1230         if (!blk_special_request(rq)) {
1231                 /* We do not support buffer cache originated requests. */
1232                 printk(KERN_NOTICE "ide-tape: %s: Unsupported request in "
1233                         "request queue (%d)\n", drive->name, rq->cmd_type);
1234                 ide_end_request(drive, 0, 0);
1235                 return ide_stopped;
1236         }
1237
1238         /* Retry a failed packet command */
1239         if (tape->failed_pc && tape->pc->c[0] == REQUEST_SENSE)
1240                 return idetape_issue_pc(drive, tape->failed_pc);
1241
1242         if (postponed_rq != NULL)
1243                 if (rq != postponed_rq) {
1244                         printk(KERN_ERR "ide-tape: ide-tape.c bug - "
1245                                         "Two DSC requests were queued\n");
1246                         idetape_end_request(drive, 0, 0);
1247                         return ide_stopped;
1248                 }
1249
1250         tape->postponed_rq = NULL;
1251
1252         /*
1253          * If the tape is still busy, postpone our request and service
1254          * the other device meanwhile.
1255          */
1256         stat = ide_read_status(drive);
1257
1258         if (!drive->dsc_overlap && !(rq->cmd[0] & REQ_IDETAPE_PC2))
1259                 set_bit(IDETAPE_FLAG_IGNORE_DSC, &tape->flags);
1260
1261         if (drive->post_reset == 1) {
1262                 set_bit(IDETAPE_FLAG_IGNORE_DSC, &tape->flags);
1263                 drive->post_reset = 0;
1264         }
1265
1266         if (!test_and_clear_bit(IDETAPE_FLAG_IGNORE_DSC, &tape->flags) &&
1267             (stat & SEEK_STAT) == 0) {
1268                 if (postponed_rq == NULL) {
1269                         tape->dsc_polling_start = jiffies;
1270                         tape->dsc_poll_freq = tape->best_dsc_rw_freq;
1271                         tape->dsc_timeout = jiffies + IDETAPE_DSC_RW_TIMEOUT;
1272                 } else if (time_after(jiffies, tape->dsc_timeout)) {
1273                         printk(KERN_ERR "ide-tape: %s: DSC timeout\n",
1274                                 tape->name);
1275                         if (rq->cmd[0] & REQ_IDETAPE_PC2) {
1276                                 idetape_media_access_finished(drive);
1277                                 return ide_stopped;
1278                         } else {
1279                                 return ide_do_reset(drive);
1280                         }
1281                 } else if (time_after(jiffies,
1282                                         tape->dsc_polling_start +
1283                                         IDETAPE_DSC_MA_THRESHOLD))
1284                         tape->dsc_poll_freq = IDETAPE_DSC_MA_SLOW;
1285                 idetape_postpone_request(drive);
1286                 return ide_stopped;
1287         }
1288         if (rq->cmd[0] & REQ_IDETAPE_READ) {
1289                 pc = idetape_next_pc_storage(drive);
1290                 idetape_create_read_cmd(tape, pc, rq->current_nr_sectors,
1291                                         (struct idetape_bh *)rq->special);
1292                 goto out;
1293         }
1294         if (rq->cmd[0] & REQ_IDETAPE_WRITE) {
1295                 pc = idetape_next_pc_storage(drive);
1296                 idetape_create_write_cmd(tape, pc, rq->current_nr_sectors,
1297                                          (struct idetape_bh *)rq->special);
1298                 goto out;
1299         }
1300         if (rq->cmd[0] & REQ_IDETAPE_PC1) {
1301                 pc = (struct ide_atapi_pc *) rq->buffer;
1302                 rq->cmd[0] &= ~(REQ_IDETAPE_PC1);
1303                 rq->cmd[0] |= REQ_IDETAPE_PC2;
1304                 goto out;
1305         }
1306         if (rq->cmd[0] & REQ_IDETAPE_PC2) {
1307                 idetape_media_access_finished(drive);
1308                 return ide_stopped;
1309         }
1310         BUG();
1311 out:
1312         return idetape_issue_pc(drive, pc);
1313 }
1314
1315 /* Pipeline related functions */
1316
1317 /*
1318  * The function below uses __get_free_page to allocate a pipeline stage, along
1319  * with all the necessary small buffers which together make a buffer of size
1320  * tape->stage_size (or a bit more). We attempt to combine sequential pages as
1321  * much as possible.
1322  *
1323  * It returns a pointer to the new allocated stage, or NULL if we can't (or
1324  * don't want to) allocate a stage.
1325  *
1326  * Pipeline stages are optional and are used to increase performance. If we
1327  * can't allocate them, we'll manage without them.
1328  */
1329 static idetape_stage_t *__idetape_kmalloc_stage(idetape_tape_t *tape, int full,
1330                                                 int clear)
1331 {
1332         idetape_stage_t *stage;
1333         struct idetape_bh *prev_bh, *bh;
1334         int pages = tape->pages_per_stage;
1335         char *b_data = NULL;
1336
1337         stage = kmalloc(sizeof(idetape_stage_t), GFP_KERNEL);
1338         if (!stage)
1339                 return NULL;
1340         stage->next = NULL;
1341
1342         stage->bh = kmalloc(sizeof(struct idetape_bh), GFP_KERNEL);
1343         bh = stage->bh;
1344         if (bh == NULL)
1345                 goto abort;
1346         bh->b_reqnext = NULL;
1347         bh->b_data = (char *) __get_free_page(GFP_KERNEL);
1348         if (!bh->b_data)
1349                 goto abort;
1350         if (clear)
1351                 memset(bh->b_data, 0, PAGE_SIZE);
1352         bh->b_size = PAGE_SIZE;
1353         atomic_set(&bh->b_count, full ? bh->b_size : 0);
1354
1355         while (--pages) {
1356                 b_data = (char *) __get_free_page(GFP_KERNEL);
1357                 if (!b_data)
1358                         goto abort;
1359                 if (clear)
1360                         memset(b_data, 0, PAGE_SIZE);
1361                 if (bh->b_data == b_data + PAGE_SIZE) {
1362                         bh->b_size += PAGE_SIZE;
1363                         bh->b_data -= PAGE_SIZE;
1364                         if (full)
1365                                 atomic_add(PAGE_SIZE, &bh->b_count);
1366                         continue;
1367                 }
1368                 if (b_data == bh->b_data + bh->b_size) {
1369                         bh->b_size += PAGE_SIZE;
1370                         if (full)
1371                                 atomic_add(PAGE_SIZE, &bh->b_count);
1372                         continue;
1373                 }
1374                 prev_bh = bh;
1375                 bh = kmalloc(sizeof(struct idetape_bh), GFP_KERNEL);
1376                 if (!bh) {
1377                         free_page((unsigned long) b_data);
1378                         goto abort;
1379                 }
1380                 bh->b_reqnext = NULL;
1381                 bh->b_data = b_data;
1382                 bh->b_size = PAGE_SIZE;
1383                 atomic_set(&bh->b_count, full ? bh->b_size : 0);
1384                 prev_bh->b_reqnext = bh;
1385         }
1386         bh->b_size -= tape->excess_bh_size;
1387         if (full)
1388                 atomic_sub(tape->excess_bh_size, &bh->b_count);
1389         return stage;
1390 abort:
1391         __idetape_kfree_stage(stage);
1392         return NULL;
1393 }
1394
1395 static int idetape_copy_stage_from_user(idetape_tape_t *tape,
1396                                         const char __user *buf, int n)
1397 {
1398         struct idetape_bh *bh = tape->bh;
1399         int count;
1400         int ret = 0;
1401
1402         while (n) {
1403                 if (bh == NULL) {
1404                         printk(KERN_ERR "ide-tape: bh == NULL in %s\n",
1405                                         __func__);
1406                         return 1;
1407                 }
1408                 count = min((unsigned int)
1409                                 (bh->b_size - atomic_read(&bh->b_count)),
1410                                 (unsigned int)n);
1411                 if (copy_from_user(bh->b_data + atomic_read(&bh->b_count), buf,
1412                                 count))
1413                         ret = 1;
1414                 n -= count;
1415                 atomic_add(count, &bh->b_count);
1416                 buf += count;
1417                 if (atomic_read(&bh->b_count) == bh->b_size) {
1418                         bh = bh->b_reqnext;
1419                         if (bh)
1420                                 atomic_set(&bh->b_count, 0);
1421                 }
1422         }
1423         tape->bh = bh;
1424         return ret;
1425 }
1426
1427 static int idetape_copy_stage_to_user(idetape_tape_t *tape, char __user *buf,
1428                                       int n)
1429 {
1430         struct idetape_bh *bh = tape->bh;
1431         int count;
1432         int ret = 0;
1433
1434         while (n) {
1435                 if (bh == NULL) {
1436                         printk(KERN_ERR "ide-tape: bh == NULL in %s\n",
1437                                         __func__);
1438                         return 1;
1439                 }
1440                 count = min(tape->b_count, n);
1441                 if  (copy_to_user(buf, tape->b_data, count))
1442                         ret = 1;
1443                 n -= count;
1444                 tape->b_data += count;
1445                 tape->b_count -= count;
1446                 buf += count;
1447                 if (!tape->b_count) {
1448                         bh = bh->b_reqnext;
1449                         tape->bh = bh;
1450                         if (bh) {
1451                                 tape->b_data = bh->b_data;
1452                                 tape->b_count = atomic_read(&bh->b_count);
1453                         }
1454                 }
1455         }
1456         return ret;
1457 }
1458
1459 static void idetape_init_merge_stage(idetape_tape_t *tape)
1460 {
1461         struct idetape_bh *bh = tape->merge_stage->bh;
1462
1463         tape->bh = bh;
1464         if (tape->chrdev_dir == IDETAPE_DIR_WRITE)
1465                 atomic_set(&bh->b_count, 0);
1466         else {
1467                 tape->b_data = bh->b_data;
1468                 tape->b_count = atomic_read(&bh->b_count);
1469         }
1470 }
1471
1472 static ide_startstop_t idetape_read_position_callback(ide_drive_t *drive)
1473 {
1474         idetape_tape_t *tape = drive->driver_data;
1475         u8 *readpos = tape->pc->buf;
1476
1477         debug_log(DBG_PROCS, "Enter %s\n", __func__);
1478
1479         if (!tape->pc->error) {
1480                 debug_log(DBG_SENSE, "BOP - %s\n",
1481                                 (readpos[0] & 0x80) ? "Yes" : "No");
1482                 debug_log(DBG_SENSE, "EOP - %s\n",
1483                                 (readpos[0] & 0x40) ? "Yes" : "No");
1484
1485                 if (readpos[0] & 0x4) {
1486                         printk(KERN_INFO "ide-tape: Block location is unknown"
1487                                          "to the tape\n");
1488                         clear_bit(IDETAPE_FLAG_ADDRESS_VALID, &tape->flags);
1489                         idetape_end_request(drive, 0, 0);
1490                 } else {
1491                         debug_log(DBG_SENSE, "Block Location - %u\n",
1492                                         be32_to_cpu(*(u32 *)&readpos[4]));
1493
1494                         tape->partition = readpos[1];
1495                         tape->first_frame =
1496                                 be32_to_cpu(*(u32 *)&readpos[4]);
1497                         set_bit(IDETAPE_FLAG_ADDRESS_VALID, &tape->flags);
1498                         idetape_end_request(drive, 1, 0);
1499                 }
1500         } else {
1501                 idetape_end_request(drive, 0, 0);
1502         }
1503         return ide_stopped;
1504 }
1505
1506 /*
1507  * Write a filemark if write_filemark=1. Flush the device buffers without
1508  * writing a filemark otherwise.
1509  */
1510 static void idetape_create_write_filemark_cmd(ide_drive_t *drive,
1511                 struct ide_atapi_pc *pc, int write_filemark)
1512 {
1513         idetape_init_pc(pc);
1514         pc->c[0] = WRITE_FILEMARKS;
1515         pc->c[4] = write_filemark;
1516         pc->flags |= PC_FLAG_WAIT_FOR_DSC;
1517         pc->idetape_callback = &idetape_pc_callback;
1518 }
1519
1520 static void idetape_create_test_unit_ready_cmd(struct ide_atapi_pc *pc)
1521 {
1522         idetape_init_pc(pc);
1523         pc->c[0] = TEST_UNIT_READY;
1524         pc->idetape_callback = &idetape_pc_callback;
1525 }
1526
1527 /*
1528  * We add a special packet command request to the tail of the request queue, and
1529  * wait for it to be serviced. This is not to be called from within the request
1530  * handling part of the driver! We allocate here data on the stack and it is
1531  * valid until the request is finished. This is not the case for the bottom part
1532  * of the driver, where we are always leaving the functions to wait for an
1533  * interrupt or a timer event.
1534  *
1535  * From the bottom part of the driver, we should allocate safe memory using
1536  * idetape_next_pc_storage() and ide_tape_next_rq_storage(), and add the request
1537  * to the request list without waiting for it to be serviced! In that case, we
1538  * usually use idetape_queue_pc_head().
1539  */
1540 static int idetape_queue_pc_tail(ide_drive_t *drive, struct ide_atapi_pc *pc)
1541 {
1542         struct ide_tape_obj *tape = drive->driver_data;
1543         struct request rq;
1544
1545         idetape_init_rq(&rq, REQ_IDETAPE_PC1);
1546         rq.buffer = (char *) pc;
1547         rq.rq_disk = tape->disk;
1548         return ide_do_drive_cmd(drive, &rq, ide_wait);
1549 }
1550
1551 static void idetape_create_load_unload_cmd(ide_drive_t *drive,
1552                 struct ide_atapi_pc *pc, int cmd)
1553 {
1554         idetape_init_pc(pc);
1555         pc->c[0] = START_STOP;
1556         pc->c[4] = cmd;
1557         pc->flags |= PC_FLAG_WAIT_FOR_DSC;
1558         pc->idetape_callback = &idetape_pc_callback;
1559 }
1560
1561 static int idetape_wait_ready(ide_drive_t *drive, unsigned long timeout)
1562 {
1563         idetape_tape_t *tape = drive->driver_data;
1564         struct ide_atapi_pc pc;
1565         int load_attempted = 0;
1566
1567         /* Wait for the tape to become ready */
1568         set_bit(IDETAPE_FLAG_MEDIUM_PRESENT, &tape->flags);
1569         timeout += jiffies;
1570         while (time_before(jiffies, timeout)) {
1571                 idetape_create_test_unit_ready_cmd(&pc);
1572                 if (!idetape_queue_pc_tail(drive, &pc))
1573                         return 0;
1574                 if ((tape->sense_key == 2 && tape->asc == 4 && tape->ascq == 2)
1575                     || (tape->asc == 0x3A)) {
1576                         /* no media */
1577                         if (load_attempted)
1578                                 return -ENOMEDIUM;
1579                         idetape_create_load_unload_cmd(drive, &pc,
1580                                                         IDETAPE_LU_LOAD_MASK);
1581                         idetape_queue_pc_tail(drive, &pc);
1582                         load_attempted = 1;
1583                 /* not about to be ready */
1584                 } else if (!(tape->sense_key == 2 && tape->asc == 4 &&
1585                              (tape->ascq == 1 || tape->ascq == 8)))
1586                         return -EIO;
1587                 msleep(100);
1588         }
1589         return -EIO;
1590 }
1591
1592 static int idetape_flush_tape_buffers(ide_drive_t *drive)
1593 {
1594         struct ide_atapi_pc pc;
1595         int rc;
1596
1597         idetape_create_write_filemark_cmd(drive, &pc, 0);
1598         rc = idetape_queue_pc_tail(drive, &pc);
1599         if (rc)
1600                 return rc;
1601         idetape_wait_ready(drive, 60 * 5 * HZ);
1602         return 0;
1603 }
1604
1605 static void idetape_create_read_position_cmd(struct ide_atapi_pc *pc)
1606 {
1607         idetape_init_pc(pc);
1608         pc->c[0] = READ_POSITION;
1609         pc->req_xfer = 20;
1610         pc->idetape_callback = &idetape_read_position_callback;
1611 }
1612
1613 static int idetape_read_position(ide_drive_t *drive)
1614 {
1615         idetape_tape_t *tape = drive->driver_data;
1616         struct ide_atapi_pc pc;
1617         int position;
1618
1619         debug_log(DBG_PROCS, "Enter %s\n", __func__);
1620
1621         idetape_create_read_position_cmd(&pc);
1622         if (idetape_queue_pc_tail(drive, &pc))
1623                 return -1;
1624         position = tape->first_frame;
1625         return position;
1626 }
1627
1628 static void idetape_create_locate_cmd(ide_drive_t *drive,
1629                 struct ide_atapi_pc *pc,
1630                 unsigned int block, u8 partition, int skip)
1631 {
1632         idetape_init_pc(pc);
1633         pc->c[0] = POSITION_TO_ELEMENT;
1634         pc->c[1] = 2;
1635         put_unaligned(cpu_to_be32(block), (unsigned int *) &pc->c[3]);
1636         pc->c[8] = partition;
1637         pc->flags |= PC_FLAG_WAIT_FOR_DSC;
1638         pc->idetape_callback = &idetape_pc_callback;
1639 }
1640
1641 static int idetape_create_prevent_cmd(ide_drive_t *drive,
1642                 struct ide_atapi_pc *pc, int prevent)
1643 {
1644         idetape_tape_t *tape = drive->driver_data;
1645
1646         /* device supports locking according to capabilities page */
1647         if (!(tape->caps[6] & 0x01))
1648                 return 0;
1649
1650         idetape_init_pc(pc);
1651         pc->c[0] = ALLOW_MEDIUM_REMOVAL;
1652         pc->c[4] = prevent;
1653         pc->idetape_callback = &idetape_pc_callback;
1654         return 1;
1655 }
1656
1657 static int __idetape_discard_read_pipeline(ide_drive_t *drive)
1658 {
1659         idetape_tape_t *tape = drive->driver_data;
1660
1661         if (tape->chrdev_dir != IDETAPE_DIR_READ)
1662                 return 0;
1663
1664         clear_bit(IDETAPE_FLAG_FILEMARK, &tape->flags);
1665         tape->merge_stage_size = 0;
1666         if (tape->merge_stage != NULL) {
1667                 __idetape_kfree_stage(tape->merge_stage);
1668                 tape->merge_stage = NULL;
1669         }
1670
1671         tape->chrdev_dir = IDETAPE_DIR_NONE;
1672
1673         return 0;
1674 }
1675
1676 /*
1677  * Position the tape to the requested block using the LOCATE packet command.
1678  * A READ POSITION command is then issued to check where we are positioned. Like
1679  * all higher level operations, we queue the commands at the tail of the request
1680  * queue and wait for their completion.
1681  */
1682 static int idetape_position_tape(ide_drive_t *drive, unsigned int block,
1683                 u8 partition, int skip)
1684 {
1685         idetape_tape_t *tape = drive->driver_data;
1686         int retval;
1687         struct ide_atapi_pc pc;
1688
1689         if (tape->chrdev_dir == IDETAPE_DIR_READ)
1690                 __idetape_discard_read_pipeline(drive);
1691         idetape_wait_ready(drive, 60 * 5 * HZ);
1692         idetape_create_locate_cmd(drive, &pc, block, partition, skip);
1693         retval = idetape_queue_pc_tail(drive, &pc);
1694         if (retval)
1695                 return (retval);
1696
1697         idetape_create_read_position_cmd(&pc);
1698         return (idetape_queue_pc_tail(drive, &pc));
1699 }
1700
1701 static void idetape_discard_read_pipeline(ide_drive_t *drive,
1702                                           int restore_position)
1703 {
1704         idetape_tape_t *tape = drive->driver_data;
1705         int cnt;
1706         int seek, position;
1707
1708         cnt = __idetape_discard_read_pipeline(drive);
1709         if (restore_position) {
1710                 position = idetape_read_position(drive);
1711                 seek = position > cnt ? position - cnt : 0;
1712                 if (idetape_position_tape(drive, seek, 0, 0)) {
1713                         printk(KERN_INFO "ide-tape: %s: position_tape failed in"
1714                                          " discard_pipeline()\n", tape->name);
1715                         return;
1716                 }
1717         }
1718 }
1719
1720 /*
1721  * Generate a read/write request for the block device interface and wait for it
1722  * to be serviced.
1723  */
1724 static int idetape_queue_rw_tail(ide_drive_t *drive, int cmd, int blocks,
1725                                  struct idetape_bh *bh)
1726 {
1727         idetape_tape_t *tape = drive->driver_data;
1728         struct request rq;
1729
1730         debug_log(DBG_SENSE, "%s: cmd=%d\n", __func__, cmd);
1731
1732         idetape_init_rq(&rq, cmd);
1733         rq.rq_disk = tape->disk;
1734         rq.special = (void *)bh;
1735         rq.sector = tape->first_frame;
1736         rq.nr_sectors           = blocks;
1737         rq.current_nr_sectors   = blocks;
1738         (void) ide_do_drive_cmd(drive, &rq, ide_wait);
1739
1740         if ((cmd & (REQ_IDETAPE_READ | REQ_IDETAPE_WRITE)) == 0)
1741                 return 0;
1742
1743         if (tape->merge_stage)
1744                 idetape_init_merge_stage(tape);
1745         if (rq.errors == IDETAPE_ERROR_GENERAL)
1746                 return -EIO;
1747         return (tape->blk_size * (blocks-rq.current_nr_sectors));
1748 }
1749
1750 static void idetape_create_inquiry_cmd(struct ide_atapi_pc *pc)
1751 {
1752         idetape_init_pc(pc);
1753         pc->c[0] = INQUIRY;
1754         pc->c[4] = 254;
1755         pc->req_xfer = 254;
1756         pc->idetape_callback = &idetape_pc_callback;
1757 }
1758
1759 static void idetape_create_rewind_cmd(ide_drive_t *drive,
1760                 struct ide_atapi_pc *pc)
1761 {
1762         idetape_init_pc(pc);
1763         pc->c[0] = REZERO_UNIT;
1764         pc->flags |= PC_FLAG_WAIT_FOR_DSC;
1765         pc->idetape_callback = &idetape_pc_callback;
1766 }
1767
1768 static void idetape_create_erase_cmd(struct ide_atapi_pc *pc)
1769 {
1770         idetape_init_pc(pc);
1771         pc->c[0] = ERASE;
1772         pc->c[1] = 1;
1773         pc->flags |= PC_FLAG_WAIT_FOR_DSC;
1774         pc->idetape_callback = &idetape_pc_callback;
1775 }
1776
1777 static void idetape_create_space_cmd(struct ide_atapi_pc *pc, int count, u8 cmd)
1778 {
1779         idetape_init_pc(pc);
1780         pc->c[0] = SPACE;
1781         put_unaligned(cpu_to_be32(count), (unsigned int *) &pc->c[1]);
1782         pc->c[1] = cmd;
1783         pc->flags |= PC_FLAG_WAIT_FOR_DSC;
1784         pc->idetape_callback = &idetape_pc_callback;
1785 }
1786
1787 /* Queue up a character device originated write request. */
1788 static int idetape_add_chrdev_write_request(ide_drive_t *drive, int blocks)
1789 {
1790         idetape_tape_t *tape = drive->driver_data;
1791
1792         debug_log(DBG_CHRDEV, "Enter %s\n", __func__);
1793
1794         return idetape_queue_rw_tail(drive, REQ_IDETAPE_WRITE,
1795                                      blocks, tape->merge_stage->bh);
1796 }
1797
1798 static void idetape_empty_write_pipeline(ide_drive_t *drive)
1799 {
1800         idetape_tape_t *tape = drive->driver_data;
1801         int blocks, min;
1802         struct idetape_bh *bh;
1803
1804         if (tape->chrdev_dir != IDETAPE_DIR_WRITE) {
1805                 printk(KERN_ERR "ide-tape: bug: Trying to empty write pipeline,"
1806                                 " but we are not writing.\n");
1807                 return;
1808         }
1809         if (tape->merge_stage_size > tape->stage_size) {
1810                 printk(KERN_ERR "ide-tape: bug: merge_buffer too big\n");
1811                 tape->merge_stage_size = tape->stage_size;
1812         }
1813         if (tape->merge_stage_size) {
1814                 blocks = tape->merge_stage_size / tape->blk_size;
1815                 if (tape->merge_stage_size % tape->blk_size) {
1816                         unsigned int i;
1817
1818                         blocks++;
1819                         i = tape->blk_size - tape->merge_stage_size %
1820                                 tape->blk_size;
1821                         bh = tape->bh->b_reqnext;
1822                         while (bh) {
1823                                 atomic_set(&bh->b_count, 0);
1824                                 bh = bh->b_reqnext;
1825                         }
1826                         bh = tape->bh;
1827                         while (i) {
1828                                 if (bh == NULL) {
1829                                         printk(KERN_INFO "ide-tape: bug,"
1830                                                          " bh NULL\n");
1831                                         break;
1832                                 }
1833                                 min = min(i, (unsigned int)(bh->b_size -
1834                                                 atomic_read(&bh->b_count)));
1835                                 memset(bh->b_data + atomic_read(&bh->b_count),
1836                                                 0, min);
1837                                 atomic_add(min, &bh->b_count);
1838                                 i -= min;
1839                                 bh = bh->b_reqnext;
1840                         }
1841                 }
1842                 (void) idetape_add_chrdev_write_request(drive, blocks);
1843                 tape->merge_stage_size = 0;
1844         }
1845         if (tape->merge_stage != NULL) {
1846                 __idetape_kfree_stage(tape->merge_stage);
1847                 tape->merge_stage = NULL;
1848         }
1849         tape->chrdev_dir = IDETAPE_DIR_NONE;
1850 }
1851
1852 static int idetape_init_read(ide_drive_t *drive)
1853 {
1854         idetape_tape_t *tape = drive->driver_data;
1855         int bytes_read;
1856
1857         /* Initialize read operation */
1858         if (tape->chrdev_dir != IDETAPE_DIR_READ) {
1859                 if (tape->chrdev_dir == IDETAPE_DIR_WRITE) {
1860                         idetape_empty_write_pipeline(drive);
1861                         idetape_flush_tape_buffers(drive);
1862                 }
1863                 if (tape->merge_stage || tape->merge_stage_size) {
1864                         printk(KERN_ERR "ide-tape: merge_stage_size should be"
1865                                          " 0 now\n");
1866                         tape->merge_stage_size = 0;
1867                 }
1868                 tape->merge_stage = __idetape_kmalloc_stage(tape, 0, 0);
1869                 if (!tape->merge_stage)
1870                         return -ENOMEM;
1871                 tape->chrdev_dir = IDETAPE_DIR_READ;
1872
1873                 /*
1874                  * Issue a read 0 command to ensure that DSC handshake is
1875                  * switched from completion mode to buffer available mode.
1876                  * No point in issuing this if DSC overlap isn't supported, some
1877                  * drives (Seagate STT3401A) will return an error.
1878                  */
1879                 if (drive->dsc_overlap) {
1880                         bytes_read = idetape_queue_rw_tail(drive,
1881                                                         REQ_IDETAPE_READ, 0,
1882                                                         tape->merge_stage->bh);
1883                         if (bytes_read < 0) {
1884                                 __idetape_kfree_stage(tape->merge_stage);
1885                                 tape->merge_stage = NULL;
1886                                 tape->chrdev_dir = IDETAPE_DIR_NONE;
1887                                 return bytes_read;
1888                         }
1889                 }
1890         }
1891
1892         return 0;
1893 }
1894
1895 /*
1896  * Called from idetape_chrdev_read() to service a character device read request
1897  * and add read-ahead requests to our pipeline.
1898  */
1899 static int idetape_add_chrdev_read_request(ide_drive_t *drive, int blocks)
1900 {
1901         idetape_tape_t *tape = drive->driver_data;
1902
1903         debug_log(DBG_PROCS, "Enter %s, %d blocks\n", __func__, blocks);
1904
1905         /* If we are at a filemark, return a read length of 0 */
1906         if (test_bit(IDETAPE_FLAG_FILEMARK, &tape->flags))
1907                 return 0;
1908
1909         idetape_init_read(drive);
1910
1911         return idetape_queue_rw_tail(drive, REQ_IDETAPE_READ, blocks,
1912                                      tape->merge_stage->bh);
1913 }
1914
1915 static void idetape_pad_zeros(ide_drive_t *drive, int bcount)
1916 {
1917         idetape_tape_t *tape = drive->driver_data;
1918         struct idetape_bh *bh;
1919         int blocks;
1920
1921         while (bcount) {
1922                 unsigned int count;
1923
1924                 bh = tape->merge_stage->bh;
1925                 count = min(tape->stage_size, bcount);
1926                 bcount -= count;
1927                 blocks = count / tape->blk_size;
1928                 while (count) {
1929                         atomic_set(&bh->b_count,
1930                                    min(count, (unsigned int)bh->b_size));
1931                         memset(bh->b_data, 0, atomic_read(&bh->b_count));
1932                         count -= atomic_read(&bh->b_count);
1933                         bh = bh->b_reqnext;
1934                 }
1935                 idetape_queue_rw_tail(drive, REQ_IDETAPE_WRITE, blocks,
1936                                       tape->merge_stage->bh);
1937         }
1938 }
1939
1940 /*
1941  * Rewinds the tape to the Beginning Of the current Partition (BOP). We
1942  * currently support only one partition.
1943  */
1944 static int idetape_rewind_tape(ide_drive_t *drive)
1945 {
1946         int retval;
1947         struct ide_atapi_pc pc;
1948         idetape_tape_t *tape;
1949         tape = drive->driver_data;
1950
1951         debug_log(DBG_SENSE, "Enter %s\n", __func__);
1952
1953         idetape_create_rewind_cmd(drive, &pc);
1954         retval = idetape_queue_pc_tail(drive, &pc);
1955         if (retval)
1956                 return retval;
1957
1958         idetape_create_read_position_cmd(&pc);
1959         retval = idetape_queue_pc_tail(drive, &pc);
1960         if (retval)
1961                 return retval;
1962         return 0;
1963 }
1964
1965 /* mtio.h compatible commands should be issued to the chrdev interface. */
1966 static int idetape_blkdev_ioctl(ide_drive_t *drive, unsigned int cmd,
1967                                 unsigned long arg)
1968 {
1969         idetape_tape_t *tape = drive->driver_data;
1970         void __user *argp = (void __user *)arg;
1971
1972         struct idetape_config {
1973                 int dsc_rw_frequency;
1974                 int dsc_media_access_frequency;
1975                 int nr_stages;
1976         } config;
1977
1978         debug_log(DBG_PROCS, "Enter %s\n", __func__);
1979
1980         switch (cmd) {
1981         case 0x0340:
1982                 if (copy_from_user(&config, argp, sizeof(config)))
1983                         return -EFAULT;
1984                 tape->best_dsc_rw_freq = config.dsc_rw_frequency;
1985                 break;
1986         case 0x0350:
1987                 config.dsc_rw_frequency = (int) tape->best_dsc_rw_freq;
1988                 config.nr_stages = 1;
1989                 if (copy_to_user(argp, &config, sizeof(config)))
1990                         return -EFAULT;
1991                 break;
1992         default:
1993                 return -EIO;
1994         }
1995         return 0;
1996 }
1997
1998 static int idetape_space_over_filemarks(ide_drive_t *drive, short mt_op,
1999                                         int mt_count)
2000 {
2001         idetape_tape_t *tape = drive->driver_data;
2002         struct ide_atapi_pc pc;
2003         int retval, count = 0;
2004         int sprev = !!(tape->caps[4] & 0x20);
2005
2006         if (mt_count == 0)
2007                 return 0;
2008         if (MTBSF == mt_op || MTBSFM == mt_op) {
2009                 if (!sprev)
2010                         return -EIO;
2011                 mt_count = -mt_count;
2012         }
2013
2014         if (tape->chrdev_dir == IDETAPE_DIR_READ) {
2015                 tape->merge_stage_size = 0;
2016                 if (test_and_clear_bit(IDETAPE_FLAG_FILEMARK, &tape->flags))
2017                         ++count;
2018                 idetape_discard_read_pipeline(drive, 0);
2019         }
2020
2021         /*
2022          * The filemark was not found in our internal pipeline; now we can issue
2023          * the space command.
2024          */
2025         switch (mt_op) {
2026         case MTFSF:
2027         case MTBSF:
2028                 idetape_create_space_cmd(&pc, mt_count - count,
2029                                          IDETAPE_SPACE_OVER_FILEMARK);
2030                 return idetape_queue_pc_tail(drive, &pc);
2031         case MTFSFM:
2032         case MTBSFM:
2033                 if (!sprev)
2034                         return -EIO;
2035                 retval = idetape_space_over_filemarks(drive, MTFSF,
2036                                                       mt_count - count);
2037                 if (retval)
2038                         return retval;
2039                 count = (MTBSFM == mt_op ? 1 : -1);
2040                 return idetape_space_over_filemarks(drive, MTFSF, count);
2041         default:
2042                 printk(KERN_ERR "ide-tape: MTIO operation %d not supported\n",
2043                                 mt_op);
2044                 return -EIO;
2045         }
2046 }
2047
2048 /*
2049  * Our character device read / write functions.
2050  *
2051  * The tape is optimized to maximize throughput when it is transferring an
2052  * integral number of the "continuous transfer limit", which is a parameter of
2053  * the specific tape (26kB on my particular tape, 32kB for Onstream).
2054  *
2055  * As of version 1.3 of the driver, the character device provides an abstract
2056  * continuous view of the media - any mix of block sizes (even 1 byte) on the
2057  * same backup/restore procedure is supported. The driver will internally
2058  * convert the requests to the recommended transfer unit, so that an unmatch
2059  * between the user's block size to the recommended size will only result in a
2060  * (slightly) increased driver overhead, but will no longer hit performance.
2061  * This is not applicable to Onstream.
2062  */
2063 static ssize_t idetape_chrdev_read(struct file *file, char __user *buf,
2064                                    size_t count, loff_t *ppos)
2065 {
2066         struct ide_tape_obj *tape = ide_tape_f(file);
2067         ide_drive_t *drive = tape->drive;
2068         ssize_t bytes_read, temp, actually_read = 0, rc;
2069         ssize_t ret = 0;
2070         u16 ctl = *(u16 *)&tape->caps[12];
2071
2072         debug_log(DBG_CHRDEV, "Enter %s, count %Zd\n", __func__, count);
2073
2074         if (tape->chrdev_dir != IDETAPE_DIR_READ) {
2075                 if (test_bit(IDETAPE_FLAG_DETECT_BS, &tape->flags))
2076                         if (count > tape->blk_size &&
2077                             (count % tape->blk_size) == 0)
2078                                 tape->user_bs_factor = count / tape->blk_size;
2079         }
2080         rc = idetape_init_read(drive);
2081         if (rc < 0)
2082                 return rc;
2083         if (count == 0)
2084                 return (0);
2085         if (tape->merge_stage_size) {
2086                 actually_read = min((unsigned int)(tape->merge_stage_size),
2087                                     (unsigned int)count);
2088                 if (idetape_copy_stage_to_user(tape, buf, actually_read))
2089                         ret = -EFAULT;
2090                 buf += actually_read;
2091                 tape->merge_stage_size -= actually_read;
2092                 count -= actually_read;
2093         }
2094         while (count >= tape->stage_size) {
2095                 bytes_read = idetape_add_chrdev_read_request(drive, ctl);
2096                 if (bytes_read <= 0)
2097                         goto finish;
2098                 if (idetape_copy_stage_to_user(tape, buf, bytes_read))
2099                         ret = -EFAULT;
2100                 buf += bytes_read;
2101                 count -= bytes_read;
2102                 actually_read += bytes_read;
2103         }
2104         if (count) {
2105                 bytes_read = idetape_add_chrdev_read_request(drive, ctl);
2106                 if (bytes_read <= 0)
2107                         goto finish;
2108                 temp = min((unsigned long)count, (unsigned long)bytes_read);
2109                 if (idetape_copy_stage_to_user(tape, buf, temp))
2110                         ret = -EFAULT;
2111                 actually_read += temp;
2112                 tape->merge_stage_size = bytes_read-temp;
2113         }
2114 finish:
2115         if (!actually_read && test_bit(IDETAPE_FLAG_FILEMARK, &tape->flags)) {
2116                 debug_log(DBG_SENSE, "%s: spacing over filemark\n", tape->name);
2117
2118                 idetape_space_over_filemarks(drive, MTFSF, 1);
2119                 return 0;
2120         }
2121
2122         return ret ? ret : actually_read;
2123 }
2124
2125 static ssize_t idetape_chrdev_write(struct file *file, const char __user *buf,
2126                                      size_t count, loff_t *ppos)
2127 {
2128         struct ide_tape_obj *tape = ide_tape_f(file);
2129         ide_drive_t *drive = tape->drive;
2130         ssize_t actually_written = 0;
2131         ssize_t ret = 0;
2132         u16 ctl = *(u16 *)&tape->caps[12];
2133
2134         /* The drive is write protected. */
2135         if (tape->write_prot)
2136                 return -EACCES;
2137
2138         debug_log(DBG_CHRDEV, "Enter %s, count %Zd\n", __func__, count);
2139
2140         /* Initialize write operation */
2141         if (tape->chrdev_dir != IDETAPE_DIR_WRITE) {
2142                 if (tape->chrdev_dir == IDETAPE_DIR_READ)
2143                         idetape_discard_read_pipeline(drive, 1);
2144                 if (tape->merge_stage || tape->merge_stage_size) {
2145                         printk(KERN_ERR "ide-tape: merge_stage_size "
2146                                 "should be 0 now\n");
2147                         tape->merge_stage_size = 0;
2148                 }
2149                 tape->merge_stage = __idetape_kmalloc_stage(tape, 0, 0);
2150                 if (!tape->merge_stage)
2151                         return -ENOMEM;
2152                 tape->chrdev_dir = IDETAPE_DIR_WRITE;
2153                 idetape_init_merge_stage(tape);
2154
2155                 /*
2156                  * Issue a write 0 command to ensure that DSC handshake is
2157                  * switched from completion mode to buffer available mode. No
2158                  * point in issuing this if DSC overlap isn't supported, some
2159                  * drives (Seagate STT3401A) will return an error.
2160                  */
2161                 if (drive->dsc_overlap) {
2162                         ssize_t retval = idetape_queue_rw_tail(drive,
2163                                                         REQ_IDETAPE_WRITE, 0,
2164                                                         tape->merge_stage->bh);
2165                         if (retval < 0) {
2166                                 __idetape_kfree_stage(tape->merge_stage);
2167                                 tape->merge_stage = NULL;
2168                                 tape->chrdev_dir = IDETAPE_DIR_NONE;
2169                                 return retval;
2170                         }
2171                 }
2172         }
2173         if (count == 0)
2174                 return (0);
2175         if (tape->merge_stage_size) {
2176                 if (tape->merge_stage_size >= tape->stage_size) {
2177                         printk(KERN_ERR "ide-tape: bug: merge buf too big\n");
2178                         tape->merge_stage_size = 0;
2179                 }
2180                 actually_written = min((unsigned int)
2181                                 (tape->stage_size - tape->merge_stage_size),
2182                                 (unsigned int)count);
2183                 if (idetape_copy_stage_from_user(tape, buf, actually_written))
2184                                 ret = -EFAULT;
2185                 buf += actually_written;
2186                 tape->merge_stage_size += actually_written;
2187                 count -= actually_written;
2188
2189                 if (tape->merge_stage_size == tape->stage_size) {
2190                         ssize_t retval;
2191                         tape->merge_stage_size = 0;
2192                         retval = idetape_add_chrdev_write_request(drive, ctl);
2193                         if (retval <= 0)
2194                                 return (retval);
2195                 }
2196         }
2197         while (count >= tape->stage_size) {
2198                 ssize_t retval;
2199                 if (idetape_copy_stage_from_user(tape, buf, tape->stage_size))
2200                         ret = -EFAULT;
2201                 buf += tape->stage_size;
2202                 count -= tape->stage_size;
2203                 retval = idetape_add_chrdev_write_request(drive, ctl);
2204                 actually_written += tape->stage_size;
2205                 if (retval <= 0)
2206                         return (retval);
2207         }
2208         if (count) {
2209                 actually_written += count;
2210                 if (idetape_copy_stage_from_user(tape, buf, count))
2211                         ret = -EFAULT;
2212                 tape->merge_stage_size += count;
2213         }
2214         return ret ? ret : actually_written;
2215 }
2216
2217 static int idetape_write_filemark(ide_drive_t *drive)
2218 {
2219         struct ide_atapi_pc pc;
2220
2221         /* Write a filemark */
2222         idetape_create_write_filemark_cmd(drive, &pc, 1);
2223         if (idetape_queue_pc_tail(drive, &pc)) {
2224                 printk(KERN_ERR "ide-tape: Couldn't write a filemark\n");
2225                 return -EIO;
2226         }
2227         return 0;
2228 }
2229
2230 /*
2231  * Called from idetape_chrdev_ioctl when the general mtio MTIOCTOP ioctl is
2232  * requested.
2233  *
2234  * Note: MTBSF and MTBSFM are not supported when the tape doesn't support
2235  * spacing over filemarks in the reverse direction. In this case, MTFSFM is also
2236  * usually not supported (it is supported in the rare case in which we crossed
2237  * the filemark during our read-ahead pipelined operation mode).
2238  *
2239  * The following commands are currently not supported:
2240  *
2241  * MTFSS, MTBSS, MTWSM, MTSETDENSITY, MTSETDRVBUFFER, MT_ST_BOOLEANS,
2242  * MT_ST_WRITE_THRESHOLD.
2243  */
2244 static int idetape_mtioctop(ide_drive_t *drive, short mt_op, int mt_count)
2245 {
2246         idetape_tape_t *tape = drive->driver_data;
2247         struct ide_atapi_pc pc;
2248         int i, retval;
2249
2250         debug_log(DBG_ERR, "Handling MTIOCTOP ioctl: mt_op=%d, mt_count=%d\n",
2251                         mt_op, mt_count);
2252
2253         /* Commands which need our pipelined read-ahead stages. */
2254         switch (mt_op) {
2255         case MTFSF:
2256         case MTFSFM:
2257         case MTBSF:
2258         case MTBSFM:
2259                 if (!mt_count)
2260                         return 0;
2261                 return idetape_space_over_filemarks(drive, mt_op, mt_count);
2262         default:
2263                 break;
2264         }
2265
2266         switch (mt_op) {
2267         case MTWEOF:
2268                 if (tape->write_prot)
2269                         return -EACCES;
2270                 idetape_discard_read_pipeline(drive, 1);
2271                 for (i = 0; i < mt_count; i++) {
2272                         retval = idetape_write_filemark(drive);
2273                         if (retval)
2274                                 return retval;
2275                 }
2276                 return 0;
2277         case MTREW:
2278                 idetape_discard_read_pipeline(drive, 0);
2279                 if (idetape_rewind_tape(drive))
2280                         return -EIO;
2281                 return 0;
2282         case MTLOAD:
2283                 idetape_discard_read_pipeline(drive, 0);
2284                 idetape_create_load_unload_cmd(drive, &pc,
2285                                                IDETAPE_LU_LOAD_MASK);
2286                 return idetape_queue_pc_tail(drive, &pc);
2287         case MTUNLOAD:
2288         case MTOFFL:
2289                 /*
2290                  * If door is locked, attempt to unlock before
2291                  * attempting to eject.
2292                  */
2293                 if (tape->door_locked) {
2294                         if (idetape_create_prevent_cmd(drive, &pc, 0))
2295                                 if (!idetape_queue_pc_tail(drive, &pc))
2296                                         tape->door_locked = DOOR_UNLOCKED;
2297                 }
2298                 idetape_discard_read_pipeline(drive, 0);
2299                 idetape_create_load_unload_cmd(drive, &pc,
2300                                               !IDETAPE_LU_LOAD_MASK);
2301                 retval = idetape_queue_pc_tail(drive, &pc);
2302                 if (!retval)
2303                         clear_bit(IDETAPE_FLAG_MEDIUM_PRESENT, &tape->flags);
2304                 return retval;
2305         case MTNOP:
2306                 idetape_discard_read_pipeline(drive, 0);
2307                 return idetape_flush_tape_buffers(drive);
2308         case MTRETEN:
2309                 idetape_discard_read_pipeline(drive, 0);
2310                 idetape_create_load_unload_cmd(drive, &pc,
2311                         IDETAPE_LU_RETENSION_MASK | IDETAPE_LU_LOAD_MASK);
2312                 return idetape_queue_pc_tail(drive, &pc);
2313         case MTEOM:
2314                 idetape_create_space_cmd(&pc, 0, IDETAPE_SPACE_TO_EOD);
2315                 return idetape_queue_pc_tail(drive, &pc);
2316         case MTERASE:
2317                 (void)idetape_rewind_tape(drive);
2318                 idetape_create_erase_cmd(&pc);
2319                 return idetape_queue_pc_tail(drive, &pc);
2320         case MTSETBLK:
2321                 if (mt_count) {
2322                         if (mt_count < tape->blk_size ||
2323                             mt_count % tape->blk_size)
2324                                 return -EIO;
2325                         tape->user_bs_factor = mt_count / tape->blk_size;
2326                         clear_bit(IDETAPE_FLAG_DETECT_BS, &tape->flags);
2327                 } else
2328                         set_bit(IDETAPE_FLAG_DETECT_BS, &tape->flags);
2329                 return 0;
2330         case MTSEEK:
2331                 idetape_discard_read_pipeline(drive, 0);
2332                 return idetape_position_tape(drive,
2333                         mt_count * tape->user_bs_factor, tape->partition, 0);
2334         case MTSETPART:
2335                 idetape_discard_read_pipeline(drive, 0);
2336                 return idetape_position_tape(drive, 0, mt_count, 0);
2337         case MTFSR:
2338         case MTBSR:
2339         case MTLOCK:
2340                 if (!idetape_create_prevent_cmd(drive, &pc, 1))
2341                         return 0;
2342                 retval = idetape_queue_pc_tail(drive, &pc);
2343                 if (retval)
2344                         return retval;
2345                 tape->door_locked = DOOR_EXPLICITLY_LOCKED;
2346                 return 0;
2347         case MTUNLOCK:
2348                 if (!idetape_create_prevent_cmd(drive, &pc, 0))
2349                         return 0;
2350                 retval = idetape_queue_pc_tail(drive, &pc);
2351                 if (retval)
2352                         return retval;
2353                 tape->door_locked = DOOR_UNLOCKED;
2354                 return 0;
2355         default:
2356                 printk(KERN_ERR "ide-tape: MTIO operation %d not supported\n",
2357                                 mt_op);
2358                 return -EIO;
2359         }
2360 }
2361
2362 /*
2363  * Our character device ioctls. General mtio.h magnetic io commands are
2364  * supported here, and not in the corresponding block interface. Our own
2365  * ide-tape ioctls are supported on both interfaces.
2366  */
2367 static int idetape_chrdev_ioctl(struct inode *inode, struct file *file,
2368                                 unsigned int cmd, unsigned long arg)
2369 {
2370         struct ide_tape_obj *tape = ide_tape_f(file);
2371         ide_drive_t *drive = tape->drive;
2372         struct mtop mtop;
2373         struct mtget mtget;
2374         struct mtpos mtpos;
2375         int block_offset = 0, position = tape->first_frame;
2376         void __user *argp = (void __user *)arg;
2377
2378         debug_log(DBG_CHRDEV, "Enter %s, cmd=%u\n", __func__, cmd);
2379
2380         if (tape->chrdev_dir == IDETAPE_DIR_WRITE) {
2381                 idetape_empty_write_pipeline(drive);
2382                 idetape_flush_tape_buffers(drive);
2383         }
2384         if (cmd == MTIOCGET || cmd == MTIOCPOS) {
2385                 block_offset = tape->merge_stage_size /
2386                         (tape->blk_size * tape->user_bs_factor);
2387                 position = idetape_read_position(drive);
2388                 if (position < 0)
2389                         return -EIO;
2390         }
2391         switch (cmd) {
2392         case MTIOCTOP:
2393                 if (copy_from_user(&mtop, argp, sizeof(struct mtop)))
2394                         return -EFAULT;
2395                 return idetape_mtioctop(drive, mtop.mt_op, mtop.mt_count);
2396         case MTIOCGET:
2397                 memset(&mtget, 0, sizeof(struct mtget));
2398                 mtget.mt_type = MT_ISSCSI2;
2399                 mtget.mt_blkno = position / tape->user_bs_factor - block_offset;
2400                 mtget.mt_dsreg =
2401                         ((tape->blk_size * tape->user_bs_factor)
2402                          << MT_ST_BLKSIZE_SHIFT) & MT_ST_BLKSIZE_MASK;
2403
2404                 if (tape->drv_write_prot)
2405                         mtget.mt_gstat |= GMT_WR_PROT(0xffffffff);
2406
2407                 if (copy_to_user(argp, &mtget, sizeof(struct mtget)))
2408                         return -EFAULT;
2409                 return 0;
2410         case MTIOCPOS:
2411                 mtpos.mt_blkno = position / tape->user_bs_factor - block_offset;
2412                 if (copy_to_user(argp, &mtpos, sizeof(struct mtpos)))
2413                         return -EFAULT;
2414                 return 0;
2415         default:
2416                 if (tape->chrdev_dir == IDETAPE_DIR_READ)
2417                         idetape_discard_read_pipeline(drive, 1);
2418                 return idetape_blkdev_ioctl(drive, cmd, arg);
2419         }
2420 }
2421
2422 /*
2423  * Do a mode sense page 0 with block descriptor and if it succeeds set the tape
2424  * block size with the reported value.
2425  */
2426 static void ide_tape_get_bsize_from_bdesc(ide_drive_t *drive)
2427 {
2428         idetape_tape_t *tape = drive->driver_data;
2429         struct ide_atapi_pc pc;
2430
2431         idetape_create_mode_sense_cmd(&pc, IDETAPE_BLOCK_DESCRIPTOR);
2432         if (idetape_queue_pc_tail(drive, &pc)) {
2433                 printk(KERN_ERR "ide-tape: Can't get block descriptor\n");
2434                 if (tape->blk_size == 0) {
2435                         printk(KERN_WARNING "ide-tape: Cannot deal with zero "
2436                                             "block size, assuming 32k\n");
2437                         tape->blk_size = 32768;
2438                 }
2439                 return;
2440         }
2441         tape->blk_size = (pc.buf[4 + 5] << 16) +
2442                                 (pc.buf[4 + 6] << 8)  +
2443                                  pc.buf[4 + 7];
2444         tape->drv_write_prot = (pc.buf[2] & 0x80) >> 7;
2445 }
2446
2447 static int idetape_chrdev_open(struct inode *inode, struct file *filp)
2448 {
2449         unsigned int minor = iminor(inode), i = minor & ~0xc0;
2450         ide_drive_t *drive;
2451         idetape_tape_t *tape;
2452         struct ide_atapi_pc pc;
2453         int retval;
2454
2455         if (i >= MAX_HWIFS * MAX_DRIVES)
2456                 return -ENXIO;
2457
2458         tape = ide_tape_chrdev_get(i);
2459         if (!tape)
2460                 return -ENXIO;
2461
2462         debug_log(DBG_CHRDEV, "Enter %s\n", __func__);
2463
2464         /*
2465          * We really want to do nonseekable_open(inode, filp); here, but some
2466          * versions of tar incorrectly call lseek on tapes and bail out if that
2467          * fails.  So we disallow pread() and pwrite(), but permit lseeks.
2468          */
2469         filp->f_mode &= ~(FMODE_PREAD | FMODE_PWRITE);
2470
2471         drive = tape->drive;
2472
2473         filp->private_data = tape;
2474
2475         if (test_and_set_bit(IDETAPE_FLAG_BUSY, &tape->flags)) {
2476                 retval = -EBUSY;
2477                 goto out_put_tape;
2478         }
2479
2480         retval = idetape_wait_ready(drive, 60 * HZ);
2481         if (retval) {
2482                 clear_bit(IDETAPE_FLAG_BUSY, &tape->flags);
2483                 printk(KERN_ERR "ide-tape: %s: drive not ready\n", tape->name);
2484                 goto out_put_tape;
2485         }
2486
2487         idetape_read_position(drive);
2488         if (!test_bit(IDETAPE_FLAG_ADDRESS_VALID, &tape->flags))
2489                 (void)idetape_rewind_tape(drive);
2490
2491         /* Read block size and write protect status from drive. */
2492         ide_tape_get_bsize_from_bdesc(drive);
2493
2494         /* Set write protect flag if device is opened as read-only. */
2495         if ((filp->f_flags & O_ACCMODE) == O_RDONLY)
2496                 tape->write_prot = 1;
2497         else
2498                 tape->write_prot = tape->drv_write_prot;
2499
2500         /* Make sure drive isn't write protected if user wants to write. */
2501         if (tape->write_prot) {
2502                 if ((filp->f_flags & O_ACCMODE) == O_WRONLY ||
2503                     (filp->f_flags & O_ACCMODE) == O_RDWR) {
2504                         clear_bit(IDETAPE_FLAG_BUSY, &tape->flags);
2505                         retval = -EROFS;
2506                         goto out_put_tape;
2507                 }
2508         }
2509
2510         /* Lock the tape drive door so user can't eject. */
2511         if (tape->chrdev_dir == IDETAPE_DIR_NONE) {
2512                 if (idetape_create_prevent_cmd(drive, &pc, 1)) {
2513                         if (!idetape_queue_pc_tail(drive, &pc)) {
2514                                 if (tape->door_locked != DOOR_EXPLICITLY_LOCKED)
2515                                         tape->door_locked = DOOR_LOCKED;
2516                         }
2517                 }
2518         }
2519         return 0;
2520
2521 out_put_tape:
2522         ide_tape_put(tape);
2523         return retval;
2524 }
2525
2526 static void idetape_write_release(ide_drive_t *drive, unsigned int minor)
2527 {
2528         idetape_tape_t *tape = drive->driver_data;
2529
2530         idetape_empty_write_pipeline(drive);
2531         tape->merge_stage = __idetape_kmalloc_stage(tape, 1, 0);
2532         if (tape->merge_stage != NULL) {
2533                 idetape_pad_zeros(drive, tape->blk_size *
2534                                 (tape->user_bs_factor - 1));
2535                 __idetape_kfree_stage(tape->merge_stage);
2536                 tape->merge_stage = NULL;
2537         }
2538         idetape_write_filemark(drive);
2539         idetape_flush_tape_buffers(drive);
2540         idetape_flush_tape_buffers(drive);
2541 }
2542
2543 static int idetape_chrdev_release(struct inode *inode, struct file *filp)
2544 {
2545         struct ide_tape_obj *tape = ide_tape_f(filp);
2546         ide_drive_t *drive = tape->drive;
2547         struct ide_atapi_pc pc;
2548         unsigned int minor = iminor(inode);
2549
2550         lock_kernel();
2551         tape = drive->driver_data;
2552
2553         debug_log(DBG_CHRDEV, "Enter %s\n", __func__);
2554
2555         if (tape->chrdev_dir == IDETAPE_DIR_WRITE)
2556                 idetape_write_release(drive, minor);
2557         if (tape->chrdev_dir == IDETAPE_DIR_READ) {
2558                 if (minor < 128)
2559                         idetape_discard_read_pipeline(drive, 1);
2560         }
2561
2562         if (minor < 128 && test_bit(IDETAPE_FLAG_MEDIUM_PRESENT, &tape->flags))
2563                 (void) idetape_rewind_tape(drive);
2564         if (tape->chrdev_dir == IDETAPE_DIR_NONE) {
2565                 if (tape->door_locked == DOOR_LOCKED) {
2566                         if (idetape_create_prevent_cmd(drive, &pc, 0)) {
2567                                 if (!idetape_queue_pc_tail(drive, &pc))
2568                                         tape->door_locked = DOOR_UNLOCKED;
2569                         }
2570                 }
2571         }
2572         clear_bit(IDETAPE_FLAG_BUSY, &tape->flags);
2573         ide_tape_put(tape);
2574         unlock_kernel();
2575         return 0;
2576 }
2577
2578 /*
2579  * check the contents of the ATAPI IDENTIFY command results. We return:
2580  *
2581  * 1 - If the tape can be supported by us, based on the information we have so
2582  * far.
2583  *
2584  * 0 - If this tape driver is not currently supported by us.
2585  */
2586 static int idetape_identify_device(ide_drive_t *drive)
2587 {
2588         u8 gcw[2], protocol, device_type, removable, packet_size;
2589
2590         if (drive->id_read == 0)
2591                 return 1;
2592
2593         *((unsigned short *) &gcw) = drive->id->config;
2594
2595         protocol        =   (gcw[1] & 0xC0) >> 6;
2596         device_type     =    gcw[1] & 0x1F;
2597         removable       = !!(gcw[0] & 0x80);
2598         packet_size     =    gcw[0] & 0x3;
2599
2600         /* Check that we can support this device */
2601         if (protocol != 2)
2602                 printk(KERN_ERR "ide-tape: Protocol (0x%02x) is not ATAPI\n",
2603                                 protocol);
2604         else if (device_type != 1)
2605                 printk(KERN_ERR "ide-tape: Device type (0x%02x) is not set "
2606                                 "to tape\n", device_type);
2607         else if (!removable)
2608                 printk(KERN_ERR "ide-tape: The removable flag is not set\n");
2609         else if (packet_size != 0) {
2610                 printk(KERN_ERR "ide-tape: Packet size (0x%02x) is not 12"
2611                                 " bytes\n", packet_size);
2612         } else
2613                 return 1;
2614         return 0;
2615 }
2616
2617 static void idetape_get_inquiry_results(ide_drive_t *drive)
2618 {
2619         idetape_tape_t *tape = drive->driver_data;
2620         struct ide_atapi_pc pc;
2621         char fw_rev[6], vendor_id[10], product_id[18];
2622
2623         idetape_create_inquiry_cmd(&pc);
2624         if (idetape_queue_pc_tail(drive, &pc)) {
2625                 printk(KERN_ERR "ide-tape: %s: can't get INQUIRY results\n",
2626                                 tape->name);
2627                 return;
2628         }
2629         memcpy(vendor_id, &pc.buf[8], 8);
2630         memcpy(product_id, &pc.buf[16], 16);
2631         memcpy(fw_rev, &pc.buf[32], 4);
2632
2633         ide_fixstring(vendor_id, 10, 0);
2634         ide_fixstring(product_id, 18, 0);
2635         ide_fixstring(fw_rev, 6, 0);
2636
2637         printk(KERN_INFO "ide-tape: %s <-> %s: %s %s rev %s\n",
2638                         drive->name, tape->name, vendor_id, product_id, fw_rev);
2639 }
2640
2641 /*
2642  * Ask the tape about its various parameters. In particular, we will adjust our
2643  * data transfer buffer size to the recommended value as returned by the tape.
2644  */
2645 static void idetape_get_mode_sense_results(ide_drive_t *drive)
2646 {
2647         idetape_tape_t *tape = drive->driver_data;
2648         struct ide_atapi_pc pc;
2649         u8 *caps;
2650         u8 speed, max_speed;
2651
2652         idetape_create_mode_sense_cmd(&pc, IDETAPE_CAPABILITIES_PAGE);
2653         if (idetape_queue_pc_tail(drive, &pc)) {
2654                 printk(KERN_ERR "ide-tape: Can't get tape parameters - assuming"
2655                                 " some default values\n");
2656                 tape->blk_size = 512;
2657                 put_unaligned(52,   (u16 *)&tape->caps[12]);
2658                 put_unaligned(540,  (u16 *)&tape->caps[14]);
2659                 put_unaligned(6*52, (u16 *)&tape->caps[16]);
2660                 return;
2661         }
2662         caps = pc.buf + 4 + pc.buf[3];
2663
2664         /* convert to host order and save for later use */
2665         speed = be16_to_cpu(*(u16 *)&caps[14]);
2666         max_speed = be16_to_cpu(*(u16 *)&caps[8]);
2667
2668         put_unaligned(max_speed, (u16 *)&caps[8]);
2669         put_unaligned(be16_to_cpu(*(u16 *)&caps[12]), (u16 *)&caps[12]);
2670         put_unaligned(speed, (u16 *)&caps[14]);
2671         put_unaligned(be16_to_cpu(*(u16 *)&caps[16]), (u16 *)&caps[16]);
2672
2673         if (!speed) {
2674                 printk(KERN_INFO "ide-tape: %s: invalid tape speed "
2675                                 "(assuming 650KB/sec)\n", drive->name);
2676                 put_unaligned(650, (u16 *)&caps[14]);
2677         }
2678         if (!max_speed) {
2679                 printk(KERN_INFO "ide-tape: %s: invalid max_speed "
2680                                 "(assuming 650KB/sec)\n", drive->name);
2681                 put_unaligned(650, (u16 *)&caps[8]);
2682         }
2683
2684         memcpy(&tape->caps, caps, 20);
2685         if (caps[7] & 0x02)
2686                 tape->blk_size = 512;
2687         else if (caps[7] & 0x04)
2688                 tape->blk_size = 1024;
2689 }
2690
2691 #ifdef CONFIG_IDE_PROC_FS
2692 static void idetape_add_settings(ide_drive_t *drive)
2693 {
2694         idetape_tape_t *tape = drive->driver_data;
2695
2696         ide_add_setting(drive, "buffer", SETTING_READ, TYPE_SHORT, 0, 0xffff,
2697                         1, 2, (u16 *)&tape->caps[16], NULL);
2698         ide_add_setting(drive, "speed", SETTING_READ, TYPE_SHORT, 0, 0xffff,
2699                         1, 1, (u16 *)&tape->caps[14], NULL);
2700         ide_add_setting(drive, "stage", SETTING_READ, TYPE_INT, 0, 0xffff, 1,
2701                         1024, &tape->stage_size, NULL);
2702         ide_add_setting(drive, "tdsc", SETTING_RW, TYPE_INT, IDETAPE_DSC_RW_MIN,
2703                         IDETAPE_DSC_RW_MAX, 1000, HZ, &tape->best_dsc_rw_freq,
2704                         NULL);
2705         ide_add_setting(drive, "dsc_overlap", SETTING_RW, TYPE_BYTE, 0, 1, 1,
2706                         1, &drive->dsc_overlap, NULL);
2707         ide_add_setting(drive, "avg_speed", SETTING_READ, TYPE_INT, 0, 0xffff,
2708                         1, 1, &tape->avg_speed, NULL);
2709         ide_add_setting(drive, "debug_mask", SETTING_RW, TYPE_INT, 0, 0xffff, 1,
2710                         1, &tape->debug_mask, NULL);
2711 }
2712 #else
2713 static inline void idetape_add_settings(ide_drive_t *drive) { ; }
2714 #endif
2715
2716 /*
2717  * The function below is called to:
2718  *
2719  * 1. Initialize our various state variables.
2720  * 2. Ask the tape for its capabilities.
2721  * 3. Allocate a buffer which will be used for data transfer. The buffer size
2722  * is chosen based on the recommendation which we received in step 2.
2723  *
2724  * Note that at this point ide.c already assigned us an irq, so that we can
2725  * queue requests here and wait for their completion.
2726  */
2727 static void idetape_setup(ide_drive_t *drive, idetape_tape_t *tape, int minor)
2728 {
2729         unsigned long t;
2730         int speed;
2731         int stage_size;
2732         u8 gcw[2];
2733         u16 *ctl = (u16 *)&tape->caps[12];
2734
2735         spin_lock_init(&tape->lock);
2736         drive->dsc_overlap = 1;
2737         if (drive->hwif->host_flags & IDE_HFLAG_NO_DSC) {
2738                 printk(KERN_INFO "ide-tape: %s: disabling DSC overlap\n",
2739                                  tape->name);
2740                 drive->dsc_overlap = 0;
2741         }
2742         /* Seagate Travan drives do not support DSC overlap. */
2743         if (strstr(drive->id->model, "Seagate STT3401"))
2744                 drive->dsc_overlap = 0;
2745         tape->minor = minor;
2746         tape->name[0] = 'h';
2747         tape->name[1] = 't';
2748         tape->name[2] = '0' + minor;
2749         tape->chrdev_dir = IDETAPE_DIR_NONE;
2750         tape->pc = tape->pc_stack;
2751         *((unsigned short *) &gcw) = drive->id->config;
2752
2753         /* Command packet DRQ type */
2754         if (((gcw[0] & 0x60) >> 5) == 1)
2755                 set_bit(IDETAPE_FLAG_DRQ_INTERRUPT, &tape->flags);
2756
2757         idetape_get_inquiry_results(drive);
2758         idetape_get_mode_sense_results(drive);
2759         ide_tape_get_bsize_from_bdesc(drive);
2760         tape->user_bs_factor = 1;
2761         tape->stage_size = *ctl * tape->blk_size;
2762         while (tape->stage_size > 0xffff) {
2763                 printk(KERN_NOTICE "ide-tape: decreasing stage size\n");
2764                 *ctl /= 2;
2765                 tape->stage_size = *ctl * tape->blk_size;
2766         }
2767         stage_size = tape->stage_size;
2768         tape->pages_per_stage = stage_size / PAGE_SIZE;
2769         if (stage_size % PAGE_SIZE) {
2770                 tape->pages_per_stage++;
2771                 tape->excess_bh_size = PAGE_SIZE - stage_size % PAGE_SIZE;
2772         }
2773
2774         /* select the "best" DSC read/write polling freq */
2775         speed = max(*(u16 *)&tape->caps[14], *(u16 *)&tape->caps[8]);
2776
2777         t = (IDETAPE_FIFO_THRESHOLD * tape->stage_size * HZ) / (speed * 1000);
2778
2779         /*
2780          * Ensure that the number we got makes sense; limit it within
2781          * IDETAPE_DSC_RW_MIN and IDETAPE_DSC_RW_MAX.
2782          */
2783         tape->best_dsc_rw_freq = max_t(unsigned long,
2784                                 min_t(unsigned long, t, IDETAPE_DSC_RW_MAX),
2785                                 IDETAPE_DSC_RW_MIN);
2786         printk(KERN_INFO "ide-tape: %s <-> %s: %dKBps, %d*%dkB buffer, "
2787                 "%lums tDSC%s\n",
2788                 drive->name, tape->name, *(u16 *)&tape->caps[14],
2789                 (*(u16 *)&tape->caps[16] * 512) / tape->stage_size,
2790                 tape->stage_size / 1024,
2791                 tape->best_dsc_rw_freq * 1000 / HZ,
2792                 drive->using_dma ? ", DMA":"");
2793
2794         idetape_add_settings(drive);
2795 }
2796
2797 static void ide_tape_remove(ide_drive_t *drive)
2798 {
2799         idetape_tape_t *tape = drive->driver_data;
2800
2801         ide_proc_unregister_driver(drive, tape->driver);
2802
2803         ide_unregister_region(tape->disk);
2804
2805         ide_tape_put(tape);
2806 }
2807
2808 static void ide_tape_release(struct kref *kref)
2809 {
2810         struct ide_tape_obj *tape = to_ide_tape(kref);
2811         ide_drive_t *drive = tape->drive;
2812         struct gendisk *g = tape->disk;
2813
2814         BUG_ON(tape->merge_stage_size);
2815
2816         drive->dsc_overlap = 0;
2817         drive->driver_data = NULL;
2818         device_destroy(idetape_sysfs_class, MKDEV(IDETAPE_MAJOR, tape->minor));
2819         device_destroy(idetape_sysfs_class,
2820                         MKDEV(IDETAPE_MAJOR, tape->minor + 128));
2821         idetape_devs[tape->minor] = NULL;
2822         g->private_data = NULL;
2823         put_disk(g);
2824         kfree(tape);
2825 }
2826
2827 #ifdef CONFIG_IDE_PROC_FS
2828 static int proc_idetape_read_name
2829         (char *page, char **start, off_t off, int count, int *eof, void *data)
2830 {
2831         ide_drive_t     *drive = (ide_drive_t *) data;
2832         idetape_tape_t  *tape = drive->driver_data;
2833         char            *out = page;
2834         int             len;
2835
2836         len = sprintf(out, "%s\n", tape->name);
2837         PROC_IDE_READ_RETURN(page, start, off, count, eof, len);
2838 }
2839
2840 static ide_proc_entry_t idetape_proc[] = {
2841         { "capacity",   S_IFREG|S_IRUGO,        proc_ide_read_capacity, NULL },
2842         { "name",       S_IFREG|S_IRUGO,        proc_idetape_read_name, NULL },
2843         { NULL, 0, NULL, NULL }
2844 };
2845 #endif
2846
2847 static int ide_tape_probe(ide_drive_t *);
2848
2849 static ide_driver_t idetape_driver = {
2850         .gen_driver = {
2851                 .owner          = THIS_MODULE,
2852                 .name           = "ide-tape",
2853                 .bus            = &ide_bus_type,
2854         },
2855         .probe                  = ide_tape_probe,
2856         .remove                 = ide_tape_remove,
2857         .version                = IDETAPE_VERSION,
2858         .media                  = ide_tape,
2859         .supports_dsc_overlap   = 1,
2860         .do_request             = idetape_do_request,
2861         .end_request            = idetape_end_request,
2862         .error                  = __ide_error,
2863         .abort                  = __ide_abort,
2864 #ifdef CONFIG_IDE_PROC_FS
2865         .proc                   = idetape_proc,
2866 #endif
2867 };
2868
2869 /* Our character device supporting functions, passed to register_chrdev. */
2870 static const struct file_operations idetape_fops = {
2871         .owner          = THIS_MODULE,
2872         .read           = idetape_chrdev_read,
2873         .write          = idetape_chrdev_write,
2874         .ioctl          = idetape_chrdev_ioctl,
2875         .open           = idetape_chrdev_open,
2876         .release        = idetape_chrdev_release,
2877 };
2878
2879 static int idetape_open(struct inode *inode, struct file *filp)
2880 {
2881         struct gendisk *disk = inode->i_bdev->bd_disk;
2882         struct ide_tape_obj *tape;
2883
2884         tape = ide_tape_get(disk);
2885         if (!tape)
2886                 return -ENXIO;
2887
2888         return 0;
2889 }
2890
2891 static int idetape_release(struct inode *inode, struct file *filp)
2892 {
2893         struct gendisk *disk = inode->i_bdev->bd_disk;
2894         struct ide_tape_obj *tape = ide_tape_g(disk);
2895
2896         ide_tape_put(tape);
2897
2898         return 0;
2899 }
2900
2901 static int idetape_ioctl(struct inode *inode, struct file *file,
2902                         unsigned int cmd, unsigned long arg)
2903 {
2904         struct block_device *bdev = inode->i_bdev;
2905         struct ide_tape_obj *tape = ide_tape_g(bdev->bd_disk);
2906         ide_drive_t *drive = tape->drive;
2907         int err = generic_ide_ioctl(drive, file, bdev, cmd, arg);
2908         if (err == -EINVAL)
2909                 err = idetape_blkdev_ioctl(drive, cmd, arg);
2910         return err;
2911 }
2912
2913 static struct block_device_operations idetape_block_ops = {
2914         .owner          = THIS_MODULE,
2915         .open           = idetape_open,
2916         .release        = idetape_release,
2917         .ioctl          = idetape_ioctl,
2918 };
2919
2920 static int ide_tape_probe(ide_drive_t *drive)
2921 {
2922         idetape_tape_t *tape;
2923         struct gendisk *g;
2924         int minor;
2925
2926         if (!strstr("ide-tape", drive->driver_req))
2927                 goto failed;
2928         if (!drive->present)
2929                 goto failed;
2930         if (drive->media != ide_tape)
2931                 goto failed;
2932         if (!idetape_identify_device(drive)) {
2933                 printk(KERN_ERR "ide-tape: %s: not supported by this version of"
2934                                 " the driver\n", drive->name);
2935                 goto failed;
2936         }
2937         if (drive->scsi) {
2938                 printk(KERN_INFO "ide-tape: passing drive %s to ide-scsi"
2939                                  " emulation.\n", drive->name);
2940                 goto failed;
2941         }
2942         tape = kzalloc(sizeof(idetape_tape_t), GFP_KERNEL);
2943         if (tape == NULL) {
2944                 printk(KERN_ERR "ide-tape: %s: Can't allocate a tape struct\n",
2945                                 drive->name);
2946                 goto failed;
2947         }
2948
2949         g = alloc_disk(1 << PARTN_BITS);
2950         if (!g)
2951                 goto out_free_tape;
2952
2953         ide_init_disk(g, drive);
2954
2955         ide_proc_register_driver(drive, &idetape_driver);
2956
2957         kref_init(&tape->kref);
2958
2959         tape->drive = drive;
2960         tape->driver = &idetape_driver;
2961         tape->disk = g;
2962
2963         g->private_data = &tape->driver;
2964
2965         drive->driver_data = tape;
2966
2967         mutex_lock(&idetape_ref_mutex);
2968         for (minor = 0; idetape_devs[minor]; minor++)
2969                 ;
2970         idetape_devs[minor] = tape;
2971         mutex_unlock(&idetape_ref_mutex);
2972
2973         idetape_setup(drive, tape, minor);
2974
2975         device_create(idetape_sysfs_class, &drive->gendev,
2976                       MKDEV(IDETAPE_MAJOR, minor), "%s", tape->name);
2977         device_create(idetape_sysfs_class, &drive->gendev,
2978                         MKDEV(IDETAPE_MAJOR, minor + 128), "n%s", tape->name);
2979
2980         g->fops = &idetape_block_ops;
2981         ide_register_region(g);
2982
2983         return 0;
2984
2985 out_free_tape:
2986         kfree(tape);
2987 failed:
2988         return -ENODEV;
2989 }
2990
2991 static void __exit idetape_exit(void)
2992 {
2993         driver_unregister(&idetape_driver.gen_driver);
2994         class_destroy(idetape_sysfs_class);
2995         unregister_chrdev(IDETAPE_MAJOR, "ht");
2996 }
2997
2998 static int __init idetape_init(void)
2999 {
3000         int error = 1;
3001         idetape_sysfs_class = class_create(THIS_MODULE, "ide_tape");
3002         if (IS_ERR(idetape_sysfs_class)) {
3003                 idetape_sysfs_class = NULL;
3004                 printk(KERN_ERR "Unable to create sysfs class for ide tapes\n");
3005                 error = -EBUSY;
3006                 goto out;
3007         }
3008
3009         if (register_chrdev(IDETAPE_MAJOR, "ht", &idetape_fops)) {
3010                 printk(KERN_ERR "ide-tape: Failed to register chrdev"
3011                                 " interface\n");
3012                 error = -EBUSY;
3013                 goto out_free_class;
3014         }
3015
3016         error = driver_register(&idetape_driver.gen_driver);
3017         if (error)
3018                 goto out_free_driver;
3019
3020         return 0;
3021
3022 out_free_driver:
3023         driver_unregister(&idetape_driver.gen_driver);
3024 out_free_class:
3025         class_destroy(idetape_sysfs_class);
3026 out:
3027         return error;
3028 }
3029
3030 MODULE_ALIAS("ide:*m-tape*");
3031 module_init(idetape_init);
3032 module_exit(idetape_exit);
3033 MODULE_ALIAS_CHARDEV_MAJOR(IDETAPE_MAJOR);
3034 MODULE_DESCRIPTION("ATAPI Streaming TAPE Driver");
3035 MODULE_LICENSE("GPL");