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