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