blob: d6555984ee887093adf7944745fd6bd04c10a593 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Borislav Petkov5ce78af2008-02-02 19:56:48 +01002 * IDE ATAPI streaming tape driver.
3 *
Bartlomiej Zolnierkiewicz59bca8c2008-02-01 23:09:33 +01004 * Copyright (C) 1995-1999 Gadi Oxman <gadio@netvision.net.il>
5 * Copyright (C) 2003-2005 Bartlomiej Zolnierkiewicz
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 * 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).
Linus Torvalds1da177e2005-04-16 15:20:36 -070013 *
Borislav Petkov5ce78af2008-02-02 19:56:48 +010014 * For a historical changelog see
15 * Documentation/ide/ChangeLog.ide-tape.1995-2002
Linus Torvalds1da177e2005-04-16 15:20:36 -070016 */
17
Bartlomiej Zolnierkiewicz51509ee2008-10-10 22:39:34 +020018#define DRV_NAME "ide-tape"
19
Borislav Petkovdfe79932008-02-06 02:57:55 +010020#define IDETAPE_VERSION "1.20"
Linus Torvalds1da177e2005-04-16 15:20:36 -070021
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#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>
Marcelo Feitoza Parisi9bae1ff2006-03-28 01:56:46 -080030#include <linux/jiffies.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#include <linux/major.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070032#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>
Arjan van de Vencf8b8972006-03-23 03:00:45 -080040#include <linux/mutex.h>
Borislav Petkov90699ce2008-02-02 19:56:50 +010041#include <scsi/scsi.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070042
43#include <asm/byteorder.h>
Borislav Petkovc837cfa2008-02-06 02:57:54 +010044#include <linux/irq.h>
45#include <linux/uaccess.h>
46#include <linux/io.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070047#include <asm/unaligned.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070048#include <linux/mtio.h>
49
Borislav Petkov8004a8c2008-02-06 02:57:51 +010050enum {
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),
Borislav Petkov8004a8c2008-02-06 02:57:51 +010059};
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
Linus Torvalds1da177e2005-04-16 15:20:36 -070074/**************************** Tunable parameters *****************************/
Linus Torvalds1da177e2005-04-16 15:20:36 -070075/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +010076 * After each failed packet command we issue a request sense command and retry
77 * the packet command IDETAPE_MAX_PC_RETRIES times.
Linus Torvalds1da177e2005-04-16 15:20:36 -070078 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +010079 * Setting IDETAPE_MAX_PC_RETRIES to 0 will disable retries.
Linus Torvalds1da177e2005-04-16 15:20:36 -070080 */
81#define IDETAPE_MAX_PC_RETRIES 3
82
83/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +010084 * The following parameter is used to select the point in the internal tape fifo
85 * in which we will start to refill the buffer. Decreasing the following
86 * parameter will improve the system's latency and interactive response, while
87 * using a high value might improve system throughput.
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 */
Borislav Petkov3c98bf32008-02-06 02:57:53 +010089#define IDETAPE_FIFO_THRESHOLD 2
Linus Torvalds1da177e2005-04-16 15:20:36 -070090
91/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +010092 * DSC polling parameters.
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +010094 * Polling for DSC (a single bit in the status register) is a very important
95 * function in ide-tape. There are two cases in which we poll for DSC:
Linus Torvalds1da177e2005-04-16 15:20:36 -070096 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +010097 * 1. Before a read/write packet command, to ensure that we can transfer data
98 * from/to the tape's data buffers, without causing an actual media access.
99 * In case the tape is not ready yet, we take out our request from the device
100 * request queue, so that ide.c could service requests from the other device
101 * on the same interface in the meantime.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100103 * 2. After the successful initialization of a "media access packet command",
104 * which is a command that can take a long time to complete (the interval can
105 * range from several seconds to even an hour). Again, we postpone our request
106 * in the middle to free the bus for the other device. The polling frequency
107 * here should be lower than the read/write frequency since those media access
108 * commands are slow. We start from a "fast" frequency - IDETAPE_DSC_MA_FAST
109 * (1 second), and if we don't receive DSC after IDETAPE_DSC_MA_THRESHOLD
110 * (5 min), we switch it to a lower frequency - IDETAPE_DSC_MA_SLOW (1 min).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100112 * We also set a timeout for the timer, in case something goes wrong. The
113 * timeout should be longer then the maximum execution time of a tape operation.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 */
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100115
116/* DSC timings. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117#define IDETAPE_DSC_RW_MIN 5*HZ/100 /* 50 msec */
118#define IDETAPE_DSC_RW_MAX 40*HZ/100 /* 400 msec */
119#define IDETAPE_DSC_RW_TIMEOUT 2*60*HZ /* 2 minutes */
120#define IDETAPE_DSC_MA_FAST 2*HZ /* 2 seconds */
121#define IDETAPE_DSC_MA_THRESHOLD 5*60*HZ /* 5 minutes */
122#define IDETAPE_DSC_MA_SLOW 30*HZ /* 30 seconds */
123#define IDETAPE_DSC_MA_TIMEOUT 2*60*60*HZ /* 2 hours */
124
125/*************************** End of tunable parameters ***********************/
126
Borislav Petkov54abf372008-02-06 02:57:52 +0100127/* tape directions */
128enum {
129 IDETAPE_DIR_NONE = (1 << 0),
130 IDETAPE_DIR_READ = (1 << 1),
131 IDETAPE_DIR_WRITE = (1 << 2),
132};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133
134struct idetape_bh {
Stephen Rothwellab057962007-08-01 23:46:44 +0200135 u32 b_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 atomic_t b_count;
137 struct idetape_bh *b_reqnext;
138 char *b_data;
139};
140
Borislav Petkov03056b92008-04-18 00:46:26 +0200141/* Tape door status */
142#define DOOR_UNLOCKED 0
143#define DOOR_LOCKED 1
144#define DOOR_EXPLICITLY_LOCKED 2
145
146/* Some defines for the SPACE command */
147#define IDETAPE_SPACE_OVER_FILEMARK 1
148#define IDETAPE_SPACE_TO_EOD 3
149
150/* Some defines for the LOAD UNLOAD command */
151#define IDETAPE_LU_LOAD_MASK 1
152#define IDETAPE_LU_RETENSION_MASK 2
153#define IDETAPE_LU_EOT_MASK 4
154
Borislav Petkov03056b92008-04-18 00:46:26 +0200155/* Error codes returned in rq->errors to the higher part of the driver. */
156#define IDETAPE_ERROR_GENERAL 101
157#define IDETAPE_ERROR_FILEMARK 102
158#define IDETAPE_ERROR_EOD 103
159
160/* Structures related to the SELECT SENSE / MODE SENSE packet commands. */
161#define IDETAPE_BLOCK_DESCRIPTOR 0
162#define IDETAPE_CAPABILITIES_PAGE 0x2a
163
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100165 * Most of our global data which we need to save even as we leave the driver due
166 * to an interrupt or a timer event is stored in the struct defined below.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 */
168typedef struct ide_tape_obj {
Bartlomiej Zolnierkiewicz7f3c8682009-01-06 17:20:53 +0100169 ide_drive_t *drive;
170 struct ide_driver *driver;
171 struct gendisk *disk;
Bartlomiej Zolnierkiewicz8fed4362009-02-25 20:28:24 +0100172 struct device dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173
Bartlomiej Zolnierkiewicz2e8a6f82008-10-10 22:39:36 +0200174 /* used by REQ_IDETAPE_{READ,WRITE} requests */
175 struct ide_atapi_pc queued_pc;
Bartlomiej Zolnierkiewicz394a4c22008-10-10 22:39:35 +0200176
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 /*
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100178 * DSC polling variables.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100180 * While polling for DSC we use postponed_rq to postpone the current
181 * request so that ide.c will be able to service pending requests on the
182 * other device. Note that at most we will have only one DSC (usually
Borislav Petkov5bd50dc2008-04-27 15:38:28 +0200183 * data transfer) request in the device request queue.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 */
185 struct request *postponed_rq;
186 /* The time in which we started polling for DSC */
187 unsigned long dsc_polling_start;
188 /* Timer used to poll for dsc */
189 struct timer_list dsc_timer;
190 /* Read/Write dsc polling frequency */
Borislav Petkov54bb2072008-02-06 02:57:52 +0100191 unsigned long best_dsc_rw_freq;
192 unsigned long dsc_poll_freq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 unsigned long dsc_timeout;
194
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100195 /* Read position information */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 u8 partition;
197 /* Current block */
Borislav Petkov54bb2072008-02-06 02:57:52 +0100198 unsigned int first_frame;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100200 /* Last error information */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 u8 sense_key, asc, ascq;
202
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100203 /* Character device operation */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 unsigned int minor;
205 /* device name */
206 char name[4];
207 /* Current character device data transfer direction */
Borislav Petkov54abf372008-02-06 02:57:52 +0100208 u8 chrdev_dir;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209
Borislav Petkov54bb2072008-02-06 02:57:52 +0100210 /* tape block size, usually 512 or 1024 bytes */
211 unsigned short blk_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 int user_bs_factor;
Borislav Petkovb6422012008-02-02 19:56:49 +0100213
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 /* Copy of the tape's Capabilities and Mechanical Page */
Borislav Petkovb6422012008-02-02 19:56:49 +0100215 u8 caps[20];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216
217 /*
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100218 * Active data transfer request parameters.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100220 * At most, there is only one ide-tape originated data transfer request
221 * in the device request queue. This allows ide.c to easily service
222 * requests from the other device when we postpone our active request.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 */
Borislav Petkov83042b22008-04-27 15:38:27 +0200224
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100225 /* Data buffer size chosen based on the tape's recommendation */
Borislav Petkovf73850a2008-04-27 15:38:33 +0200226 int buffer_size;
Borislav Petkov077e3bd2008-04-27 15:38:34 +0200227 /* merge buffer */
228 struct idetape_bh *merge_bh;
Borislav Petkov01a63aeb2008-04-27 15:38:34 +0200229 /* size of the merge buffer */
230 int merge_bh_size;
Borislav Petkov077e3bd2008-04-27 15:38:34 +0200231 /* pointer to current buffer head within the merge buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 struct idetape_bh *bh;
233 char *b_data;
234 int b_count;
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100235
Borislav Petkova997a432008-04-27 15:38:33 +0200236 int pages_per_buffer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 /* Wasted space in each stage */
238 int excess_bh_size;
239
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100240 /* Measures average tape speed */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 unsigned long avg_time;
242 int avg_size;
243 int avg_speed;
244
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 /* the door is currently locked */
246 int door_locked;
247 /* the tape hardware is write protected */
248 char drv_write_prot;
249 /* the tape is write protected (hardware or opened as read-only) */
250 char write_prot;
251
Borislav Petkov8004a8c2008-02-06 02:57:51 +0100252 u32 debug_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253} idetape_tape_t;
254
Arjan van de Vencf8b8972006-03-23 03:00:45 -0800255static DEFINE_MUTEX(idetape_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256
Will Dysond5dee802005-09-16 02:55:07 -0700257static struct class *idetape_sysfs_class;
258
Bartlomiej Zolnierkiewicz8fed4362009-02-25 20:28:24 +0100259static void ide_tape_release(struct device *);
Bartlomiej Zolnierkiewicz08da5912008-07-24 22:53:15 +0200260
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261static struct ide_tape_obj *ide_tape_get(struct gendisk *disk)
262{
263 struct ide_tape_obj *tape = NULL;
264
Arjan van de Vencf8b8972006-03-23 03:00:45 -0800265 mutex_lock(&idetape_ref_mutex);
Borislav Petkov5aeddf92008-10-13 21:39:34 +0200266 tape = ide_drv_g(disk, ide_tape_obj);
Bartlomiej Zolnierkiewicz08da5912008-07-24 22:53:15 +0200267 if (tape) {
Bartlomiej Zolnierkiewiczd3e33ff2008-08-05 18:16:59 +0200268 if (ide_device_get(tape->drive))
Bartlomiej Zolnierkiewicz08da5912008-07-24 22:53:15 +0200269 tape = NULL;
Bartlomiej Zolnierkiewiczd3e33ff2008-08-05 18:16:59 +0200270 else
Bartlomiej Zolnierkiewicz8fed4362009-02-25 20:28:24 +0100271 get_device(&tape->dev);
Bartlomiej Zolnierkiewicz08da5912008-07-24 22:53:15 +0200272 }
Arjan van de Vencf8b8972006-03-23 03:00:45 -0800273 mutex_unlock(&idetape_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 return tape;
275}
276
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277static void ide_tape_put(struct ide_tape_obj *tape)
278{
Bartlomiej Zolnierkiewiczd3e33ff2008-08-05 18:16:59 +0200279 ide_drive_t *drive = tape->drive;
280
Arjan van de Vencf8b8972006-03-23 03:00:45 -0800281 mutex_lock(&idetape_ref_mutex);
Bartlomiej Zolnierkiewicz8fed4362009-02-25 20:28:24 +0100282 put_device(&tape->dev);
Bartlomiej Zolnierkiewiczd3e33ff2008-08-05 18:16:59 +0200283 ide_device_put(drive);
Arjan van de Vencf8b8972006-03-23 03:00:45 -0800284 mutex_unlock(&idetape_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285}
286
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100288 * The variables below are used for the character device interface. Additional
289 * state variables are defined in our ide_drive_t structure.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100291static struct ide_tape_obj *idetape_devs[MAX_HWIFS * MAX_DRIVES];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293static struct ide_tape_obj *ide_tape_chrdev_get(unsigned int i)
294{
295 struct ide_tape_obj *tape = NULL;
296
Arjan van de Vencf8b8972006-03-23 03:00:45 -0800297 mutex_lock(&idetape_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 tape = idetape_devs[i];
299 if (tape)
Bartlomiej Zolnierkiewicz8fed4362009-02-25 20:28:24 +0100300 get_device(&tape->dev);
Arjan van de Vencf8b8972006-03-23 03:00:45 -0800301 mutex_unlock(&idetape_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 return tape;
303}
304
Borislav Petkovd236d742008-04-18 00:46:27 +0200305static void idetape_input_buffers(ide_drive_t *drive, struct ide_atapi_pc *pc,
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100306 unsigned int bcount)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307{
308 struct idetape_bh *bh = pc->bh;
309 int count;
310
311 while (bcount) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 if (bh == NULL) {
313 printk(KERN_ERR "ide-tape: bh == NULL in "
314 "idetape_input_buffers\n");
Bartlomiej Zolnierkiewicz9f87abe2008-04-28 23:44:41 +0200315 ide_pad_transfer(drive, 0, bcount);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 return;
317 }
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100318 count = min(
319 (unsigned int)(bh->b_size - atomic_read(&bh->b_count)),
320 bcount);
Bartlomiej Zolnierkiewicz374e0422008-07-23 19:55:56 +0200321 drive->hwif->tp_ops->input_data(drive, NULL, bh->b_data +
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100322 atomic_read(&bh->b_count), count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 bcount -= count;
324 atomic_add(count, &bh->b_count);
325 if (atomic_read(&bh->b_count) == bh->b_size) {
326 bh = bh->b_reqnext;
327 if (bh)
328 atomic_set(&bh->b_count, 0);
329 }
330 }
331 pc->bh = bh;
332}
333
Borislav Petkovd236d742008-04-18 00:46:27 +0200334static void idetape_output_buffers(ide_drive_t *drive, struct ide_atapi_pc *pc,
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100335 unsigned int bcount)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336{
337 struct idetape_bh *bh = pc->bh;
338 int count;
339
340 while (bcount) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 if (bh == NULL) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100342 printk(KERN_ERR "ide-tape: bh == NULL in %s\n",
343 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 return;
345 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 count = min((unsigned int)pc->b_count, (unsigned int)bcount);
Bartlomiej Zolnierkiewicz374e0422008-07-23 19:55:56 +0200347 drive->hwif->tp_ops->output_data(drive, NULL, pc->b_data, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 bcount -= count;
349 pc->b_data += count;
350 pc->b_count -= count;
351 if (!pc->b_count) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100352 bh = bh->b_reqnext;
353 pc->bh = bh;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 if (bh) {
355 pc->b_data = bh->b_data;
356 pc->b_count = atomic_read(&bh->b_count);
357 }
358 }
359 }
360}
361
Bartlomiej Zolnierkiewicz646c0cb2008-07-15 21:22:03 +0200362static void idetape_update_buffers(ide_drive_t *drive, struct ide_atapi_pc *pc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363{
364 struct idetape_bh *bh = pc->bh;
365 int count;
Borislav Petkovd236d742008-04-18 00:46:27 +0200366 unsigned int bcount = pc->xferred;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367
Borislav Petkov346331f2008-04-18 00:46:26 +0200368 if (pc->flags & PC_FLAG_WRITING)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 return;
370 while (bcount) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 if (bh == NULL) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100372 printk(KERN_ERR "ide-tape: bh == NULL in %s\n",
373 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 return;
375 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 count = min((unsigned int)bh->b_size, (unsigned int)bcount);
377 atomic_set(&bh->b_count, count);
378 if (atomic_read(&bh->b_count) == bh->b_size)
379 bh = bh->b_reqnext;
380 bcount -= count;
381 }
382 pc->bh = bh;
383}
384
385/*
Borislav Petkov1b5db432008-02-02 19:56:48 +0100386 * called on each failed packet command retry to analyze the request sense. We
387 * currently do not utilize this information.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 */
Borislav Petkov1b5db432008-02-02 19:56:48 +0100389static void idetape_analyze_error(ide_drive_t *drive, u8 *sense)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390{
391 idetape_tape_t *tape = drive->driver_data;
Bartlomiej Zolnierkiewicz5e2040fd2009-03-27 12:46:34 +0100392 struct ide_atapi_pc *pc = drive->failed_pc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393
Borislav Petkov1b5db432008-02-02 19:56:48 +0100394 tape->sense_key = sense[2] & 0xF;
395 tape->asc = sense[12];
396 tape->ascq = sense[13];
Borislav Petkov8004a8c2008-02-06 02:57:51 +0100397
398 debug_log(DBG_ERR, "pc = %x, sense key = %x, asc = %x, ascq = %x\n",
399 pc->c[0], tape->sense_key, tape->asc, tape->ascq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400
Borislav Petkovd236d742008-04-18 00:46:27 +0200401 /* Correct pc->xferred by asking the tape. */
Borislav Petkov346331f2008-04-18 00:46:26 +0200402 if (pc->flags & PC_FLAG_DMA_ERROR) {
Borislav Petkovd236d742008-04-18 00:46:27 +0200403 pc->xferred = pc->req_xfer -
Borislav Petkov54bb2072008-02-06 02:57:52 +0100404 tape->blk_size *
Harvey Harrison5d0cc8a2008-07-15 21:21:41 +0200405 get_unaligned_be32(&sense[3]);
Bartlomiej Zolnierkiewicz646c0cb2008-07-15 21:22:03 +0200406 idetape_update_buffers(drive, pc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 }
408
409 /*
410 * If error was the result of a zero-length read or write command,
411 * with sense key=5, asc=0x22, ascq=0, let it slide. Some drives
412 * (i.e. Seagate STT3401A Travan) don't support 0-length read/writes.
413 */
Borislav Petkov90699ce2008-02-02 19:56:50 +0100414 if ((pc->c[0] == READ_6 || pc->c[0] == WRITE_6)
Borislav Petkov1b5db432008-02-02 19:56:48 +0100415 /* length == 0 */
416 && pc->c[4] == 0 && pc->c[3] == 0 && pc->c[2] == 0) {
417 if (tape->sense_key == 5) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 /* don't report an error, everything's ok */
419 pc->error = 0;
420 /* don't retry read/write */
Borislav Petkov346331f2008-04-18 00:46:26 +0200421 pc->flags |= PC_FLAG_ABORT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 }
423 }
Borislav Petkov90699ce2008-02-02 19:56:50 +0100424 if (pc->c[0] == READ_6 && (sense[2] & 0x80)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 pc->error = IDETAPE_ERROR_FILEMARK;
Borislav Petkov346331f2008-04-18 00:46:26 +0200426 pc->flags |= PC_FLAG_ABORT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 }
Borislav Petkov90699ce2008-02-02 19:56:50 +0100428 if (pc->c[0] == WRITE_6) {
Borislav Petkov1b5db432008-02-02 19:56:48 +0100429 if ((sense[2] & 0x40) || (tape->sense_key == 0xd
430 && tape->asc == 0x0 && tape->ascq == 0x2)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 pc->error = IDETAPE_ERROR_EOD;
Borislav Petkov346331f2008-04-18 00:46:26 +0200432 pc->flags |= PC_FLAG_ABORT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 }
434 }
Borislav Petkov90699ce2008-02-02 19:56:50 +0100435 if (pc->c[0] == READ_6 || pc->c[0] == WRITE_6) {
Borislav Petkov1b5db432008-02-02 19:56:48 +0100436 if (tape->sense_key == 8) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 pc->error = IDETAPE_ERROR_EOD;
Borislav Petkov346331f2008-04-18 00:46:26 +0200438 pc->flags |= PC_FLAG_ABORT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 }
Borislav Petkov346331f2008-04-18 00:46:26 +0200440 if (!(pc->flags & PC_FLAG_ABORT) &&
Borislav Petkovd236d742008-04-18 00:46:27 +0200441 pc->xferred)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 pc->retries = IDETAPE_MAX_PC_RETRIES + 1;
443 }
444}
445
Borislav Petkovd01dbc32008-04-27 15:38:33 +0200446/* Free data buffers completely. */
Borislav Petkov077e3bd2008-04-27 15:38:34 +0200447static void ide_tape_kfree_buffer(idetape_tape_t *tape)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448{
Borislav Petkov077e3bd2008-04-27 15:38:34 +0200449 struct idetape_bh *prev_bh, *bh = tape->merge_bh;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450
Borislav Petkovd01dbc32008-04-27 15:38:33 +0200451 while (bh) {
452 u32 size = bh->b_size;
453
454 while (size) {
455 unsigned int order = fls(size >> PAGE_SHIFT)-1;
456
457 if (bh->b_data)
458 free_pages((unsigned long)bh->b_data, order);
459
460 size &= (order-1);
461 bh->b_data += (1 << order) * PAGE_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 }
463 prev_bh = bh;
464 bh = bh->b_reqnext;
465 kfree(prev_bh);
466 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467}
468
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469static int idetape_end_request(ide_drive_t *drive, int uptodate, int nr_sects)
470{
Bartlomiej Zolnierkiewiczb65fac32009-01-06 17:20:50 +0100471 struct request *rq = drive->hwif->rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473
Borislav Petkov8004a8c2008-02-06 02:57:51 +0100474 debug_log(DBG_PROCS, "Enter %s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475
476 switch (uptodate) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100477 case 0: error = IDETAPE_ERROR_GENERAL; break;
478 case 1: error = 0; break;
479 default: error = uptodate;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 }
481 rq->errors = error;
482 if (error)
Bartlomiej Zolnierkiewicz5e2040fd2009-03-27 12:46:34 +0100483 drive->failed_pc = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484
Bartlomiej Zolnierkiewicz36872212008-01-26 20:13:10 +0100485 if (!blk_special_request(rq)) {
486 ide_end_request(drive, uptodate, nr_sects);
487 return 0;
488 }
489
Bartlomiej Zolnierkiewicza09485d2009-03-27 12:46:31 +0100490 ide_complete_rq(drive, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 return 0;
493}
494
Bartlomiej Zolnierkiewiczb14c7212008-10-13 21:39:30 +0200495static void ide_tape_handle_dsc(ide_drive_t *);
496
497static void ide_tape_callback(ide_drive_t *drive, int dsc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498{
499 idetape_tape_t *tape = drive->driver_data;
Bartlomiej Zolnierkiewicz2b9efba2008-10-13 21:39:31 +0200500 struct ide_atapi_pc *pc = drive->pc;
Bartlomiej Zolnierkiewicz5985e6a2008-07-15 21:21:55 +0200501 int uptodate = pc->error ? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502
Borislav Petkov8004a8c2008-02-06 02:57:51 +0100503 debug_log(DBG_PROCS, "Enter %s\n", __func__);
504
Bartlomiej Zolnierkiewiczb14c7212008-10-13 21:39:30 +0200505 if (dsc)
506 ide_tape_handle_dsc(drive);
507
Bartlomiej Zolnierkiewicz5e2040fd2009-03-27 12:46:34 +0100508 if (drive->failed_pc == pc)
509 drive->failed_pc = NULL;
Bartlomiej Zolnierkiewiczdd2e9a02008-07-15 21:22:01 +0200510
Bartlomiej Zolnierkiewicz5985e6a2008-07-15 21:21:55 +0200511 if (pc->c[0] == REQUEST_SENSE) {
512 if (uptodate)
513 idetape_analyze_error(drive, pc->buf);
514 else
515 printk(KERN_ERR "ide-tape: Error in REQUEST SENSE "
516 "itself - Aborting request!\n");
517 } else if (pc->c[0] == READ_6 || pc->c[0] == WRITE_6) {
Bartlomiej Zolnierkiewiczb65fac32009-01-06 17:20:50 +0100518 struct request *rq = drive->hwif->rq;
Bartlomiej Zolnierkiewicz5985e6a2008-07-15 21:21:55 +0200519 int blocks = pc->xferred / tape->blk_size;
520
521 tape->avg_size += blocks * tape->blk_size;
522
523 if (time_after_eq(jiffies, tape->avg_time + HZ)) {
524 tape->avg_speed = tape->avg_size * HZ /
525 (jiffies - tape->avg_time) / 1024;
526 tape->avg_size = 0;
527 tape->avg_time = jiffies;
528 }
529
530 tape->first_frame += blocks;
531 rq->current_nr_sectors -= blocks;
532
533 if (pc->error)
534 uptodate = pc->error;
535 } else if (pc->c[0] == READ_POSITION && uptodate) {
Bartlomiej Zolnierkiewicz2b9efba2008-10-13 21:39:31 +0200536 u8 *readpos = pc->buf;
Bartlomiej Zolnierkiewicz5985e6a2008-07-15 21:21:55 +0200537
538 debug_log(DBG_SENSE, "BOP - %s\n",
539 (readpos[0] & 0x80) ? "Yes" : "No");
540 debug_log(DBG_SENSE, "EOP - %s\n",
541 (readpos[0] & 0x40) ? "Yes" : "No");
542
543 if (readpos[0] & 0x4) {
544 printk(KERN_INFO "ide-tape: Block location is unknown"
545 "to the tape\n");
Borislav Petkovf2e3ab52008-07-23 19:56:01 +0200546 clear_bit(IDE_AFLAG_ADDRESS_VALID, &drive->atapi_flags);
Bartlomiej Zolnierkiewicz5985e6a2008-07-15 21:21:55 +0200547 uptodate = 0;
548 } else {
549 debug_log(DBG_SENSE, "Block Location - %u\n",
Harvey Harrisoncd740ab2008-07-24 22:53:33 +0200550 be32_to_cpup((__be32 *)&readpos[4]));
Bartlomiej Zolnierkiewicz5985e6a2008-07-15 21:21:55 +0200551
552 tape->partition = readpos[1];
Harvey Harrisoncd740ab2008-07-24 22:53:33 +0200553 tape->first_frame = be32_to_cpup((__be32 *)&readpos[4]);
Borislav Petkovf2e3ab52008-07-23 19:56:01 +0200554 set_bit(IDE_AFLAG_ADDRESS_VALID, &drive->atapi_flags);
Bartlomiej Zolnierkiewicz5985e6a2008-07-15 21:21:55 +0200555 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 }
Bartlomiej Zolnierkiewicz5985e6a2008-07-15 21:21:55 +0200557
558 idetape_end_request(drive, uptodate, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559}
560
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100562 * Postpone the current request so that ide.c will be able to service requests
Bartlomiej Zolnierkiewiczb65fac32009-01-06 17:20:50 +0100563 * from another device on the same port while we are polling for DSC.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100565static void idetape_postpone_request(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566{
567 idetape_tape_t *tape = drive->driver_data;
568
Borislav Petkov8004a8c2008-02-06 02:57:51 +0100569 debug_log(DBG_PROCS, "Enter %s\n", __func__);
570
Bartlomiej Zolnierkiewiczb65fac32009-01-06 17:20:50 +0100571 tape->postponed_rq = drive->hwif->rq;
572
Borislav Petkov54bb2072008-02-06 02:57:52 +0100573 ide_stall_queue(drive, tape->dsc_poll_freq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574}
575
Bartlomiej Zolnierkiewicz74e63e742008-07-15 21:22:01 +0200576static void ide_tape_handle_dsc(ide_drive_t *drive)
577{
578 idetape_tape_t *tape = drive->driver_data;
579
580 /* Media access command */
581 tape->dsc_polling_start = jiffies;
582 tape->dsc_poll_freq = IDETAPE_DSC_MA_FAST;
583 tape->dsc_timeout = jiffies + IDETAPE_DSC_MA_TIMEOUT;
584 /* Allow ide.c to handle other requests */
585 idetape_postpone_request(drive);
586}
587
Bartlomiej Zolnierkiewiczacaa0f52008-10-10 22:39:36 +0200588static int ide_tape_io_buffers(ide_drive_t *drive, struct ide_atapi_pc *pc,
Bartlomiej Zolnierkiewicz08424ac2008-07-15 21:22:01 +0200589 unsigned int bcount, int write)
590{
591 if (write)
592 idetape_output_buffers(drive, pc, bcount);
593 else
594 idetape_input_buffers(drive, pc, bcount);
Bartlomiej Zolnierkiewiczacaa0f52008-10-10 22:39:36 +0200595
596 return bcount;
Bartlomiej Zolnierkiewicz08424ac2008-07-15 21:22:01 +0200597}
Borislav Petkova1efc852008-02-06 02:57:52 +0100598
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100600 * Packet Command Interface
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 *
Bartlomiej Zolnierkiewicz2b9efba2008-10-13 21:39:31 +0200602 * The current Packet Command is available in drive->pc, and will not change
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100603 * until we finish handling it. Each packet command is associated with a
604 * callback function that will be called when the command is finished.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100606 * The handling will be done in three stages:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100608 * 1. idetape_issue_pc will send the packet command to the drive, and will set
Bartlomiej Zolnierkiewiczaa5d2de72008-10-13 21:39:32 +0200609 * the interrupt handler to ide_pc_intr.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 *
Bartlomiej Zolnierkiewiczaa5d2de72008-10-13 21:39:32 +0200611 * 2. On each interrupt, ide_pc_intr will be called. This step will be
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100612 * repeated until the device signals us that no more interrupts will be issued.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100614 * 3. ATAPI Tape media access commands have immediate status with a delayed
615 * process. In case of a successful initiation of a media access packet command,
616 * the DSC bit will be set when the actual execution of the command is finished.
617 * Since the tape drive will not issue an interrupt, we have to poll for this
618 * event. In this case, we define the request as "low priority request" by
619 * setting rq_status to IDETAPE_RQ_POSTPONED, set a timer to poll for DSC and
620 * exit the driver.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100622 * ide.c will then give higher priority to requests which originate from the
623 * other device, until will change rq_status to RQ_ACTIVE.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100625 * 4. When the packet command is finished, it will be checked for errors.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100627 * 5. In case an error was found, we queue a request sense packet command in
628 * front of the request queue and retry the operation up to
629 * IDETAPE_MAX_PC_RETRIES times.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100631 * 6. In case no error was found, or we decided to give up and not to retry
632 * again, the callback function will be called and then we will handle the next
633 * request.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635
Borislav Petkovd236d742008-04-18 00:46:27 +0200636static ide_startstop_t idetape_issue_pc(ide_drive_t *drive,
637 struct ide_atapi_pc *pc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639 idetape_tape_t *tape = drive->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640
Bartlomiej Zolnierkiewicz2b9efba2008-10-13 21:39:31 +0200641 if (drive->pc->c[0] == REQUEST_SENSE &&
Borislav Petkov90699ce2008-02-02 19:56:50 +0100642 pc->c[0] == REQUEST_SENSE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 printk(KERN_ERR "ide-tape: possible ide-tape.c bug - "
644 "Two request sense in serial were issued\n");
645 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646
Bartlomiej Zolnierkiewicz5e2040fd2009-03-27 12:46:34 +0100647 if (drive->failed_pc == NULL && pc->c[0] != REQUEST_SENSE)
648 drive->failed_pc = pc;
Bartlomiej Zolnierkiewicz2b9efba2008-10-13 21:39:31 +0200649
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650 /* Set the current packet command */
Bartlomiej Zolnierkiewicz2b9efba2008-10-13 21:39:31 +0200651 drive->pc = pc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652
653 if (pc->retries > IDETAPE_MAX_PC_RETRIES ||
Borislav Petkov346331f2008-04-18 00:46:26 +0200654 (pc->flags & PC_FLAG_ABORT)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655 /*
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100656 * We will "abort" retrying a packet command in case legitimate
657 * error code was received (crossing a filemark, or end of the
658 * media, for example).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659 */
Borislav Petkov346331f2008-04-18 00:46:26 +0200660 if (!(pc->flags & PC_FLAG_ABORT)) {
Borislav Petkov90699ce2008-02-02 19:56:50 +0100661 if (!(pc->c[0] == TEST_UNIT_READY &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662 tape->sense_key == 2 && tape->asc == 4 &&
663 (tape->ascq == 1 || tape->ascq == 8))) {
664 printk(KERN_ERR "ide-tape: %s: I/O error, "
665 "pc = %2x, key = %2x, "
666 "asc = %2x, ascq = %2x\n",
667 tape->name, pc->c[0],
668 tape->sense_key, tape->asc,
669 tape->ascq);
670 }
671 /* Giving up */
672 pc->error = IDETAPE_ERROR_GENERAL;
673 }
Bartlomiej Zolnierkiewicz5e2040fd2009-03-27 12:46:34 +0100674 drive->failed_pc = NULL;
Bartlomiej Zolnierkiewiczb14c7212008-10-13 21:39:30 +0200675 drive->pc_callback(drive, 0);
Bartlomiej Zolnierkiewicz92f5daf2008-07-15 21:21:55 +0200676 return ide_stopped;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 }
Borislav Petkov8004a8c2008-02-06 02:57:51 +0100678 debug_log(DBG_SENSE, "Retry #%d, cmd = %02X\n", pc->retries, pc->c[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679
680 pc->retries++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681
Borislav Petkov28ad91d2009-01-02 16:12:56 +0100682 return ide_issue_pc(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683}
684
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100685/* A mode sense command is used to "sense" tape parameters. */
Borislav Petkovd236d742008-04-18 00:46:27 +0200686static void idetape_create_mode_sense_cmd(struct ide_atapi_pc *pc, u8 page_code)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687{
Bartlomiej Zolnierkiewicz7bf74202008-10-10 22:39:37 +0200688 ide_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +0100689 pc->c[0] = MODE_SENSE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 if (page_code != IDETAPE_BLOCK_DESCRIPTOR)
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100691 /* DBD = 1 - Don't return block descriptors */
692 pc->c[1] = 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 pc->c[2] = page_code;
694 /*
695 * Changed pc->c[3] to 0 (255 will at best return unused info).
696 *
697 * For SCSI this byte is defined as subpage instead of high byte
698 * of length and some IDE drives seem to interpret it this way
699 * and return an error when 255 is used.
700 */
701 pc->c[3] = 0;
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100702 /* We will just discard data in that case */
703 pc->c[4] = 255;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 if (page_code == IDETAPE_BLOCK_DESCRIPTOR)
Borislav Petkovd236d742008-04-18 00:46:27 +0200705 pc->req_xfer = 12;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 else if (page_code == IDETAPE_CAPABILITIES_PAGE)
Borislav Petkovd236d742008-04-18 00:46:27 +0200707 pc->req_xfer = 24;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708 else
Borislav Petkovd236d742008-04-18 00:46:27 +0200709 pc->req_xfer = 50;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710}
711
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100712static ide_startstop_t idetape_media_access_finished(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713{
Bartlomiej Zolnierkiewiczb73c7ee2008-07-23 19:55:52 +0200714 ide_hwif_t *hwif = drive->hwif;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 idetape_tape_t *tape = drive->driver_data;
Bartlomiej Zolnierkiewicz2b9efba2008-10-13 21:39:31 +0200716 struct ide_atapi_pc *pc = drive->pc;
Bartlomiej Zolnierkiewicz22c525b2008-01-25 22:17:11 +0100717 u8 stat;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718
Bartlomiej Zolnierkiewicz374e0422008-07-23 19:55:56 +0200719 stat = hwif->tp_ops->read_status(hwif);
Bartlomiej Zolnierkiewiczc47137a2008-02-06 02:57:51 +0100720
Bartlomiej Zolnierkiewicz3a7d2482008-10-10 22:39:21 +0200721 if (stat & ATA_DSC) {
722 if (stat & ATA_ERR) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 /* Error detected */
Borislav Petkov90699ce2008-02-02 19:56:50 +0100724 if (pc->c[0] != TEST_UNIT_READY)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 printk(KERN_ERR "ide-tape: %s: I/O error, ",
726 tape->name);
727 /* Retry operation */
Bartlomiej Zolnierkiewicz6b0da282008-10-13 21:39:32 +0200728 ide_retry_pc(drive, tape->disk);
Bartlomiej Zolnierkiewicz258ec412008-07-15 21:21:55 +0200729 return ide_stopped;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 }
731 pc->error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732 } else {
733 pc->error = IDETAPE_ERROR_GENERAL;
Bartlomiej Zolnierkiewicz5e2040fd2009-03-27 12:46:34 +0100734 drive->failed_pc = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735 }
Bartlomiej Zolnierkiewiczb14c7212008-10-13 21:39:30 +0200736 drive->pc_callback(drive, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737 return ide_stopped;
738}
739
Borislav Petkovcd2abbf2008-07-15 21:22:03 +0200740static void ide_tape_create_rw_cmd(idetape_tape_t *tape,
Borislav Petkov0014c752008-07-23 19:56:00 +0200741 struct ide_atapi_pc *pc, struct request *rq,
742 u8 opcode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743{
Borislav Petkov0014c752008-07-23 19:56:00 +0200744 struct idetape_bh *bh = (struct idetape_bh *)rq->special;
745 unsigned int length = rq->current_nr_sectors;
746
Bartlomiej Zolnierkiewicz7bf74202008-10-10 22:39:37 +0200747 ide_init_pc(pc);
Borislav Petkov860ff5e2008-02-02 19:56:50 +0100748 put_unaligned(cpu_to_be32(length), (unsigned int *) &pc->c[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749 pc->c[1] = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750 pc->bh = bh;
Borislav Petkovd236d742008-04-18 00:46:27 +0200751 pc->buf = NULL;
752 pc->buf_size = length * tape->blk_size;
753 pc->req_xfer = pc->buf_size;
Borislav Petkovf73850a2008-04-27 15:38:33 +0200754 if (pc->req_xfer == tape->buffer_size)
Bartlomiej Zolnierkiewicz5e331092008-07-15 21:21:56 +0200755 pc->flags |= PC_FLAG_DMA_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756
Borislav Petkovcd2abbf2008-07-15 21:22:03 +0200757 if (opcode == READ_6) {
758 pc->c[0] = READ_6;
759 atomic_set(&bh->b_count, 0);
760 } else if (opcode == WRITE_6) {
761 pc->c[0] = WRITE_6;
762 pc->flags |= PC_FLAG_WRITING;
763 pc->b_data = bh->b_data;
764 pc->b_count = atomic_read(&bh->b_count);
765 }
Borislav Petkov0014c752008-07-23 19:56:00 +0200766
767 memcpy(rq->cmd, pc->c, 12);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768}
769
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770static ide_startstop_t idetape_do_request(ide_drive_t *drive,
771 struct request *rq, sector_t block)
772{
Bartlomiej Zolnierkiewiczb73c7ee2008-07-23 19:55:52 +0200773 ide_hwif_t *hwif = drive->hwif;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774 idetape_tape_t *tape = drive->driver_data;
Borislav Petkovd236d742008-04-18 00:46:27 +0200775 struct ide_atapi_pc *pc = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776 struct request *postponed_rq = tape->postponed_rq;
Bartlomiej Zolnierkiewicz22c525b2008-01-25 22:17:11 +0100777 u8 stat;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778
Mark de Wever730616b2008-10-10 22:39:17 +0200779 debug_log(DBG_SENSE, "sector: %llu, nr_sectors: %lu,"
780 " current_nr_sectors: %u\n",
781 (unsigned long long)rq->sector, rq->nr_sectors,
782 rq->current_nr_sectors);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783
Jens Axboe4aff5e22006-08-10 08:44:47 +0200784 if (!blk_special_request(rq)) {
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100785 /* We do not support buffer cache originated requests. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786 printk(KERN_NOTICE "ide-tape: %s: Unsupported request in "
Jens Axboe4aff5e22006-08-10 08:44:47 +0200787 "request queue (%d)\n", drive->name, rq->cmd_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788 ide_end_request(drive, 0, 0);
789 return ide_stopped;
790 }
791
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100792 /* Retry a failed packet command */
Bartlomiej Zolnierkiewicz5e2040fd2009-03-27 12:46:34 +0100793 if (drive->failed_pc && drive->pc->c[0] == REQUEST_SENSE) {
794 pc = drive->failed_pc;
Bartlomiej Zolnierkiewicz28c72142008-07-15 21:21:59 +0200795 goto out;
796 }
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100797
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 if (postponed_rq != NULL)
799 if (rq != postponed_rq) {
800 printk(KERN_ERR "ide-tape: ide-tape.c bug - "
801 "Two DSC requests were queued\n");
802 idetape_end_request(drive, 0, 0);
803 return ide_stopped;
804 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805
806 tape->postponed_rq = NULL;
807
808 /*
809 * If the tape is still busy, postpone our request and service
810 * the other device meanwhile.
811 */
Bartlomiej Zolnierkiewicz374e0422008-07-23 19:55:56 +0200812 stat = hwif->tp_ops->read_status(hwif);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +0200814 if ((drive->dev_flags & IDE_DFLAG_DSC_OVERLAP) == 0 &&
815 (rq->cmd[13] & REQ_IDETAPE_PC2) == 0)
Borislav Petkovf2e3ab52008-07-23 19:56:01 +0200816 set_bit(IDE_AFLAG_IGNORE_DSC, &drive->atapi_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +0200818 if (drive->dev_flags & IDE_DFLAG_POST_RESET) {
Borislav Petkovf2e3ab52008-07-23 19:56:01 +0200819 set_bit(IDE_AFLAG_IGNORE_DSC, &drive->atapi_flags);
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +0200820 drive->dev_flags &= ~IDE_DFLAG_POST_RESET;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821 }
822
Borislav Petkovf2e3ab52008-07-23 19:56:01 +0200823 if (!test_and_clear_bit(IDE_AFLAG_IGNORE_DSC, &drive->atapi_flags) &&
Bartlomiej Zolnierkiewicz3a7d2482008-10-10 22:39:21 +0200824 (stat & ATA_DSC) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825 if (postponed_rq == NULL) {
826 tape->dsc_polling_start = jiffies;
Borislav Petkov54bb2072008-02-06 02:57:52 +0100827 tape->dsc_poll_freq = tape->best_dsc_rw_freq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828 tape->dsc_timeout = jiffies + IDETAPE_DSC_RW_TIMEOUT;
829 } else if (time_after(jiffies, tape->dsc_timeout)) {
830 printk(KERN_ERR "ide-tape: %s: DSC timeout\n",
831 tape->name);
Borislav Petkov83dd5732008-07-23 19:56:00 +0200832 if (rq->cmd[13] & REQ_IDETAPE_PC2) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833 idetape_media_access_finished(drive);
834 return ide_stopped;
835 } else {
836 return ide_do_reset(drive);
837 }
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100838 } else if (time_after(jiffies,
839 tape->dsc_polling_start +
840 IDETAPE_DSC_MA_THRESHOLD))
Borislav Petkov54bb2072008-02-06 02:57:52 +0100841 tape->dsc_poll_freq = IDETAPE_DSC_MA_SLOW;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842 idetape_postpone_request(drive);
843 return ide_stopped;
844 }
Borislav Petkov83dd5732008-07-23 19:56:00 +0200845 if (rq->cmd[13] & REQ_IDETAPE_READ) {
Bartlomiej Zolnierkiewicz2e8a6f82008-10-10 22:39:36 +0200846 pc = &tape->queued_pc;
Borislav Petkov0014c752008-07-23 19:56:00 +0200847 ide_tape_create_rw_cmd(tape, pc, rq, READ_6);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848 goto out;
849 }
Borislav Petkov83dd5732008-07-23 19:56:00 +0200850 if (rq->cmd[13] & REQ_IDETAPE_WRITE) {
Bartlomiej Zolnierkiewicz2e8a6f82008-10-10 22:39:36 +0200851 pc = &tape->queued_pc;
Borislav Petkov0014c752008-07-23 19:56:00 +0200852 ide_tape_create_rw_cmd(tape, pc, rq, WRITE_6);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853 goto out;
854 }
Borislav Petkov83dd5732008-07-23 19:56:00 +0200855 if (rq->cmd[13] & REQ_IDETAPE_PC1) {
Borislav Petkovd236d742008-04-18 00:46:27 +0200856 pc = (struct ide_atapi_pc *) rq->buffer;
Borislav Petkov83dd5732008-07-23 19:56:00 +0200857 rq->cmd[13] &= ~(REQ_IDETAPE_PC1);
858 rq->cmd[13] |= REQ_IDETAPE_PC2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859 goto out;
860 }
Borislav Petkov83dd5732008-07-23 19:56:00 +0200861 if (rq->cmd[13] & REQ_IDETAPE_PC2) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862 idetape_media_access_finished(drive);
863 return ide_stopped;
864 }
865 BUG();
Bartlomiej Zolnierkiewicz28c72142008-07-15 21:21:59 +0200866
Borislav Petkovf2e3ab52008-07-23 19:56:01 +0200867out:
Borislav Petkov8d06bfa2008-02-06 02:57:53 +0100868 return idetape_issue_pc(drive, pc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869}
870
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871/*
Borislav Petkov41aa1702008-04-27 15:38:32 +0200872 * The function below uses __get_free_pages to allocate a data buffer of size
Borislav Petkovf73850a2008-04-27 15:38:33 +0200873 * tape->buffer_size (or a bit more). We attempt to combine sequential pages as
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100874 * much as possible.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875 *
Borislav Petkov41aa1702008-04-27 15:38:32 +0200876 * It returns a pointer to the newly allocated buffer, or NULL in case of
877 * failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878 */
Borislav Petkov077e3bd2008-04-27 15:38:34 +0200879static struct idetape_bh *ide_tape_kmalloc_buffer(idetape_tape_t *tape,
880 int full, int clear)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881{
Borislav Petkov077e3bd2008-04-27 15:38:34 +0200882 struct idetape_bh *prev_bh, *bh, *merge_bh;
Borislav Petkova997a432008-04-27 15:38:33 +0200883 int pages = tape->pages_per_buffer;
Borislav Petkov41aa1702008-04-27 15:38:32 +0200884 unsigned int order, b_allocd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885 char *b_data = NULL;
886
Borislav Petkov077e3bd2008-04-27 15:38:34 +0200887 merge_bh = kmalloc(sizeof(struct idetape_bh), GFP_KERNEL);
888 bh = merge_bh;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889 if (bh == NULL)
890 goto abort;
Borislav Petkov41aa1702008-04-27 15:38:32 +0200891
892 order = fls(pages) - 1;
893 bh->b_data = (char *) __get_free_pages(GFP_KERNEL, order);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100894 if (!bh->b_data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895 goto abort;
Borislav Petkov41aa1702008-04-27 15:38:32 +0200896 b_allocd = (1 << order) * PAGE_SIZE;
897 pages &= (order-1);
898
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899 if (clear)
Borislav Petkov41aa1702008-04-27 15:38:32 +0200900 memset(bh->b_data, 0, b_allocd);
901 bh->b_reqnext = NULL;
902 bh->b_size = b_allocd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903 atomic_set(&bh->b_count, full ? bh->b_size : 0);
904
Borislav Petkov41aa1702008-04-27 15:38:32 +0200905 while (pages) {
906 order = fls(pages) - 1;
907 b_data = (char *) __get_free_pages(GFP_KERNEL, order);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100908 if (!b_data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909 goto abort;
Borislav Petkov41aa1702008-04-27 15:38:32 +0200910 b_allocd = (1 << order) * PAGE_SIZE;
911
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912 if (clear)
Borislav Petkov41aa1702008-04-27 15:38:32 +0200913 memset(b_data, 0, b_allocd);
914
915 /* newly allocated page frames below buffer header or ...*/
916 if (bh->b_data == b_data + b_allocd) {
917 bh->b_size += b_allocd;
918 bh->b_data -= b_allocd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919 if (full)
Borislav Petkov41aa1702008-04-27 15:38:32 +0200920 atomic_add(b_allocd, &bh->b_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921 continue;
922 }
Borislav Petkov41aa1702008-04-27 15:38:32 +0200923 /* they are above the header */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924 if (b_data == bh->b_data + bh->b_size) {
Borislav Petkov41aa1702008-04-27 15:38:32 +0200925 bh->b_size += b_allocd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926 if (full)
Borislav Petkov41aa1702008-04-27 15:38:32 +0200927 atomic_add(b_allocd, &bh->b_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928 continue;
929 }
930 prev_bh = bh;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100931 bh = kmalloc(sizeof(struct idetape_bh), GFP_KERNEL);
932 if (!bh) {
Borislav Petkov41aa1702008-04-27 15:38:32 +0200933 free_pages((unsigned long) b_data, order);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934 goto abort;
935 }
936 bh->b_reqnext = NULL;
937 bh->b_data = b_data;
Borislav Petkov41aa1702008-04-27 15:38:32 +0200938 bh->b_size = b_allocd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939 atomic_set(&bh->b_count, full ? bh->b_size : 0);
940 prev_bh->b_reqnext = bh;
Borislav Petkov41aa1702008-04-27 15:38:32 +0200941
942 pages &= (order-1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943 }
Borislav Petkov41aa1702008-04-27 15:38:32 +0200944
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945 bh->b_size -= tape->excess_bh_size;
946 if (full)
947 atomic_sub(tape->excess_bh_size, &bh->b_count);
Borislav Petkov077e3bd2008-04-27 15:38:34 +0200948 return merge_bh;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949abort:
Borislav Petkov077e3bd2008-04-27 15:38:34 +0200950 ide_tape_kfree_buffer(tape);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951 return NULL;
952}
953
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100954static int idetape_copy_stage_from_user(idetape_tape_t *tape,
Borislav Petkov8646c882008-04-27 15:38:26 +0200955 const char __user *buf, int n)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956{
957 struct idetape_bh *bh = tape->bh;
958 int count;
Daniel Walkerdcd96372006-06-25 05:47:37 -0700959 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960
961 while (n) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962 if (bh == NULL) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100963 printk(KERN_ERR "ide-tape: bh == NULL in %s\n",
964 __func__);
Daniel Walkerdcd96372006-06-25 05:47:37 -0700965 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966 }
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100967 count = min((unsigned int)
968 (bh->b_size - atomic_read(&bh->b_count)),
969 (unsigned int)n);
970 if (copy_from_user(bh->b_data + atomic_read(&bh->b_count), buf,
971 count))
Daniel Walkerdcd96372006-06-25 05:47:37 -0700972 ret = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973 n -= count;
974 atomic_add(count, &bh->b_count);
975 buf += count;
976 if (atomic_read(&bh->b_count) == bh->b_size) {
977 bh = bh->b_reqnext;
978 if (bh)
979 atomic_set(&bh->b_count, 0);
980 }
981 }
982 tape->bh = bh;
Daniel Walkerdcd96372006-06-25 05:47:37 -0700983 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984}
985
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100986static int idetape_copy_stage_to_user(idetape_tape_t *tape, char __user *buf,
Borislav Petkov99d74e62008-04-27 15:38:25 +0200987 int n)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988{
989 struct idetape_bh *bh = tape->bh;
990 int count;
Daniel Walkerdcd96372006-06-25 05:47:37 -0700991 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992
993 while (n) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994 if (bh == NULL) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100995 printk(KERN_ERR "ide-tape: bh == NULL in %s\n",
996 __func__);
Daniel Walkerdcd96372006-06-25 05:47:37 -0700997 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999 count = min(tape->b_count, n);
Daniel Walkerdcd96372006-06-25 05:47:37 -07001000 if (copy_to_user(buf, tape->b_data, count))
1001 ret = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002 n -= count;
1003 tape->b_data += count;
1004 tape->b_count -= count;
1005 buf += count;
1006 if (!tape->b_count) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001007 bh = bh->b_reqnext;
1008 tape->bh = bh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009 if (bh) {
1010 tape->b_data = bh->b_data;
1011 tape->b_count = atomic_read(&bh->b_count);
1012 }
1013 }
1014 }
Daniel Walkerdcd96372006-06-25 05:47:37 -07001015 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016}
1017
Borislav Petkov077e3bd2008-04-27 15:38:34 +02001018static void idetape_init_merge_buffer(idetape_tape_t *tape)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019{
Borislav Petkov077e3bd2008-04-27 15:38:34 +02001020 struct idetape_bh *bh = tape->merge_bh;
1021 tape->bh = tape->merge_bh;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001022
Borislav Petkov54abf372008-02-06 02:57:52 +01001023 if (tape->chrdev_dir == IDETAPE_DIR_WRITE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024 atomic_set(&bh->b_count, 0);
1025 else {
1026 tape->b_data = bh->b_data;
1027 tape->b_count = atomic_read(&bh->b_count);
1028 }
1029}
1030
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001032 * Write a filemark if write_filemark=1. Flush the device buffers without
1033 * writing a filemark otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034 */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001035static void idetape_create_write_filemark_cmd(ide_drive_t *drive,
Borislav Petkovd236d742008-04-18 00:46:27 +02001036 struct ide_atapi_pc *pc, int write_filemark)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037{
Bartlomiej Zolnierkiewicz7bf74202008-10-10 22:39:37 +02001038 ide_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +01001039 pc->c[0] = WRITE_FILEMARKS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040 pc->c[4] = write_filemark;
Borislav Petkov346331f2008-04-18 00:46:26 +02001041 pc->flags |= PC_FLAG_WAIT_FOR_DSC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042}
1043
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044static int idetape_wait_ready(ide_drive_t *drive, unsigned long timeout)
1045{
1046 idetape_tape_t *tape = drive->driver_data;
Bartlomiej Zolnierkiewicz2ac07d92008-10-10 22:39:38 +02001047 struct gendisk *disk = tape->disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048 int load_attempted = 0;
1049
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001050 /* Wait for the tape to become ready */
Borislav Petkovf2e3ab52008-07-23 19:56:01 +02001051 set_bit(IDE_AFLAG_MEDIUM_PRESENT, &drive->atapi_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052 timeout += jiffies;
1053 while (time_before(jiffies, timeout)) {
Bartlomiej Zolnierkiewiczde699ad2008-10-10 22:39:39 +02001054 if (ide_do_test_unit_ready(drive, disk) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055 return 0;
1056 if ((tape->sense_key == 2 && tape->asc == 4 && tape->ascq == 2)
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001057 || (tape->asc == 0x3A)) {
1058 /* no media */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059 if (load_attempted)
1060 return -ENOMEDIUM;
Bartlomiej Zolnierkiewicz0c8a6c72008-10-10 22:39:39 +02001061 ide_do_start_stop(drive, disk, IDETAPE_LU_LOAD_MASK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062 load_attempted = 1;
1063 /* not about to be ready */
1064 } else if (!(tape->sense_key == 2 && tape->asc == 4 &&
1065 (tape->ascq == 1 || tape->ascq == 8)))
1066 return -EIO;
Nishanth Aravamudan80ce45f2005-09-10 00:27:08 -07001067 msleep(100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068 }
1069 return -EIO;
1070}
1071
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001072static int idetape_flush_tape_buffers(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073{
Bartlomiej Zolnierkiewicz2ac07d92008-10-10 22:39:38 +02001074 struct ide_tape_obj *tape = drive->driver_data;
Borislav Petkovd236d742008-04-18 00:46:27 +02001075 struct ide_atapi_pc pc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076 int rc;
1077
1078 idetape_create_write_filemark_cmd(drive, &pc, 0);
Bartlomiej Zolnierkiewicz2ac07d92008-10-10 22:39:38 +02001079 rc = ide_queue_pc_tail(drive, tape->disk, &pc);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001080 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081 return rc;
1082 idetape_wait_ready(drive, 60 * 5 * HZ);
1083 return 0;
1084}
1085
Borislav Petkovd236d742008-04-18 00:46:27 +02001086static void idetape_create_read_position_cmd(struct ide_atapi_pc *pc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087{
Bartlomiej Zolnierkiewicz7bf74202008-10-10 22:39:37 +02001088 ide_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +01001089 pc->c[0] = READ_POSITION;
Borislav Petkovd236d742008-04-18 00:46:27 +02001090 pc->req_xfer = 20;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091}
1092
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001093static int idetape_read_position(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094{
1095 idetape_tape_t *tape = drive->driver_data;
Borislav Petkovd236d742008-04-18 00:46:27 +02001096 struct ide_atapi_pc pc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097 int position;
1098
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001099 debug_log(DBG_PROCS, "Enter %s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100
1101 idetape_create_read_position_cmd(&pc);
Bartlomiej Zolnierkiewicz2ac07d92008-10-10 22:39:38 +02001102 if (ide_queue_pc_tail(drive, tape->disk, &pc))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103 return -1;
Borislav Petkov54bb2072008-02-06 02:57:52 +01001104 position = tape->first_frame;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105 return position;
1106}
1107
Borislav Petkovd236d742008-04-18 00:46:27 +02001108static void idetape_create_locate_cmd(ide_drive_t *drive,
1109 struct ide_atapi_pc *pc,
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001110 unsigned int block, u8 partition, int skip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111{
Bartlomiej Zolnierkiewicz7bf74202008-10-10 22:39:37 +02001112 ide_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +01001113 pc->c[0] = POSITION_TO_ELEMENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114 pc->c[1] = 2;
Borislav Petkov860ff5e2008-02-02 19:56:50 +01001115 put_unaligned(cpu_to_be32(block), (unsigned int *) &pc->c[3]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116 pc->c[8] = partition;
Borislav Petkov346331f2008-04-18 00:46:26 +02001117 pc->flags |= PC_FLAG_WAIT_FOR_DSC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118}
1119
Borislav Petkovec0fdb02008-04-27 15:38:34 +02001120static void __ide_tape_discard_merge_buffer(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121{
1122 idetape_tape_t *tape = drive->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123
Borislav Petkov54abf372008-02-06 02:57:52 +01001124 if (tape->chrdev_dir != IDETAPE_DIR_READ)
Borislav Petkov97986302008-04-27 15:38:34 +02001125 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126
Borislav Petkovf2e3ab52008-07-23 19:56:01 +02001127 clear_bit(IDE_AFLAG_FILEMARK, &drive->atapi_flags);
Borislav Petkov01a63aeb2008-04-27 15:38:34 +02001128 tape->merge_bh_size = 0;
Borislav Petkov077e3bd2008-04-27 15:38:34 +02001129 if (tape->merge_bh != NULL) {
1130 ide_tape_kfree_buffer(tape);
1131 tape->merge_bh = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132 }
1133
Borislav Petkov54abf372008-02-06 02:57:52 +01001134 tape->chrdev_dir = IDETAPE_DIR_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135}
1136
1137/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001138 * Position the tape to the requested block using the LOCATE packet command.
1139 * A READ POSITION command is then issued to check where we are positioned. Like
1140 * all higher level operations, we queue the commands at the tail of the request
1141 * queue and wait for their completion.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142 */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001143static int idetape_position_tape(ide_drive_t *drive, unsigned int block,
1144 u8 partition, int skip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145{
1146 idetape_tape_t *tape = drive->driver_data;
Bartlomiej Zolnierkiewicz2ac07d92008-10-10 22:39:38 +02001147 struct gendisk *disk = tape->disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148 int retval;
Borislav Petkovd236d742008-04-18 00:46:27 +02001149 struct ide_atapi_pc pc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150
Borislav Petkov54abf372008-02-06 02:57:52 +01001151 if (tape->chrdev_dir == IDETAPE_DIR_READ)
Borislav Petkovec0fdb02008-04-27 15:38:34 +02001152 __ide_tape_discard_merge_buffer(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153 idetape_wait_ready(drive, 60 * 5 * HZ);
1154 idetape_create_locate_cmd(drive, &pc, block, partition, skip);
Bartlomiej Zolnierkiewicz2ac07d92008-10-10 22:39:38 +02001155 retval = ide_queue_pc_tail(drive, disk, &pc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156 if (retval)
1157 return (retval);
1158
1159 idetape_create_read_position_cmd(&pc);
Bartlomiej Zolnierkiewicz2ac07d92008-10-10 22:39:38 +02001160 return ide_queue_pc_tail(drive, disk, &pc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161}
1162
Borislav Petkovec0fdb02008-04-27 15:38:34 +02001163static void ide_tape_discard_merge_buffer(ide_drive_t *drive,
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001164 int restore_position)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165{
1166 idetape_tape_t *tape = drive->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167 int seek, position;
1168
Borislav Petkovec0fdb02008-04-27 15:38:34 +02001169 __ide_tape_discard_merge_buffer(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170 if (restore_position) {
1171 position = idetape_read_position(drive);
Borislav Petkov97986302008-04-27 15:38:34 +02001172 seek = position > 0 ? position : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173 if (idetape_position_tape(drive, seek, 0, 0)) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001174 printk(KERN_INFO "ide-tape: %s: position_tape failed in"
Borislav Petkovec0fdb02008-04-27 15:38:34 +02001175 " %s\n", tape->name, __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176 return;
1177 }
1178 }
1179}
1180
1181/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001182 * Generate a read/write request for the block device interface and wait for it
1183 * to be serviced.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184 */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001185static int idetape_queue_rw_tail(ide_drive_t *drive, int cmd, int blocks,
1186 struct idetape_bh *bh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187{
1188 idetape_tape_t *tape = drive->driver_data;
FUJITA Tomonori64ea1b42008-07-15 21:21:43 +02001189 struct request *rq;
1190 int ret, errors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001192 debug_log(DBG_SENSE, "%s: cmd=%d\n", __func__, cmd);
1193
FUJITA Tomonori64ea1b42008-07-15 21:21:43 +02001194 rq = blk_get_request(drive->queue, READ, __GFP_WAIT);
1195 rq->cmd_type = REQ_TYPE_SPECIAL;
Borislav Petkov83dd5732008-07-23 19:56:00 +02001196 rq->cmd[13] = cmd;
FUJITA Tomonori64ea1b42008-07-15 21:21:43 +02001197 rq->rq_disk = tape->disk;
1198 rq->special = (void *)bh;
1199 rq->sector = tape->first_frame;
1200 rq->nr_sectors = blocks;
1201 rq->current_nr_sectors = blocks;
1202 blk_execute_rq(drive->queue, tape->disk, rq, 0);
1203
1204 errors = rq->errors;
1205 ret = tape->blk_size * (blocks - rq->current_nr_sectors);
1206 blk_put_request(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207
1208 if ((cmd & (REQ_IDETAPE_READ | REQ_IDETAPE_WRITE)) == 0)
1209 return 0;
1210
Borislav Petkov077e3bd2008-04-27 15:38:34 +02001211 if (tape->merge_bh)
1212 idetape_init_merge_buffer(tape);
FUJITA Tomonori64ea1b42008-07-15 21:21:43 +02001213 if (errors == IDETAPE_ERROR_GENERAL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214 return -EIO;
FUJITA Tomonori64ea1b42008-07-15 21:21:43 +02001215 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216}
1217
Borislav Petkovd236d742008-04-18 00:46:27 +02001218static void idetape_create_inquiry_cmd(struct ide_atapi_pc *pc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219{
Bartlomiej Zolnierkiewicz7bf74202008-10-10 22:39:37 +02001220 ide_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +01001221 pc->c[0] = INQUIRY;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001222 pc->c[4] = 254;
Borislav Petkovd236d742008-04-18 00:46:27 +02001223 pc->req_xfer = 254;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224}
1225
Borislav Petkovd236d742008-04-18 00:46:27 +02001226static void idetape_create_rewind_cmd(ide_drive_t *drive,
1227 struct ide_atapi_pc *pc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228{
Bartlomiej Zolnierkiewicz7bf74202008-10-10 22:39:37 +02001229 ide_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +01001230 pc->c[0] = REZERO_UNIT;
Borislav Petkov346331f2008-04-18 00:46:26 +02001231 pc->flags |= PC_FLAG_WAIT_FOR_DSC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232}
1233
Borislav Petkovd236d742008-04-18 00:46:27 +02001234static void idetape_create_erase_cmd(struct ide_atapi_pc *pc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235{
Bartlomiej Zolnierkiewicz7bf74202008-10-10 22:39:37 +02001236 ide_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +01001237 pc->c[0] = ERASE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238 pc->c[1] = 1;
Borislav Petkov346331f2008-04-18 00:46:26 +02001239 pc->flags |= PC_FLAG_WAIT_FOR_DSC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240}
1241
Borislav Petkovd236d742008-04-18 00:46:27 +02001242static void idetape_create_space_cmd(struct ide_atapi_pc *pc, int count, u8 cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243{
Bartlomiej Zolnierkiewicz7bf74202008-10-10 22:39:37 +02001244 ide_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +01001245 pc->c[0] = SPACE;
Borislav Petkov860ff5e2008-02-02 19:56:50 +01001246 put_unaligned(cpu_to_be32(count), (unsigned int *) &pc->c[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247 pc->c[1] = cmd;
Borislav Petkov346331f2008-04-18 00:46:26 +02001248 pc->flags |= PC_FLAG_WAIT_FOR_DSC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249}
1250
Borislav Petkov97c566c2008-04-27 15:38:25 +02001251/* Queue up a character device originated write request. */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001252static int idetape_add_chrdev_write_request(ide_drive_t *drive, int blocks)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253{
1254 idetape_tape_t *tape = drive->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001256 debug_log(DBG_CHRDEV, "Enter %s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257
Borislav Petkov0aa4b012008-04-27 15:38:27 +02001258 return idetape_queue_rw_tail(drive, REQ_IDETAPE_WRITE,
Borislav Petkov077e3bd2008-04-27 15:38:34 +02001259 blocks, tape->merge_bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260}
1261
Borislav Petkovd9df9372008-04-27 15:38:34 +02001262static void ide_tape_flush_merge_buffer(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263{
1264 idetape_tape_t *tape = drive->driver_data;
1265 int blocks, min;
1266 struct idetape_bh *bh;
Borislav Petkov55a5d292008-02-02 19:56:49 +01001267
Borislav Petkov54abf372008-02-06 02:57:52 +01001268 if (tape->chrdev_dir != IDETAPE_DIR_WRITE) {
Borislav Petkov077e3bd2008-04-27 15:38:34 +02001269 printk(KERN_ERR "ide-tape: bug: Trying to empty merge buffer"
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001270 " but we are not writing.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271 return;
1272 }
Borislav Petkov01a63aeb2008-04-27 15:38:34 +02001273 if (tape->merge_bh_size > tape->buffer_size) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274 printk(KERN_ERR "ide-tape: bug: merge_buffer too big\n");
Borislav Petkov01a63aeb2008-04-27 15:38:34 +02001275 tape->merge_bh_size = tape->buffer_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276 }
Borislav Petkov01a63aeb2008-04-27 15:38:34 +02001277 if (tape->merge_bh_size) {
1278 blocks = tape->merge_bh_size / tape->blk_size;
1279 if (tape->merge_bh_size % tape->blk_size) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280 unsigned int i;
1281
1282 blocks++;
Borislav Petkov01a63aeb2008-04-27 15:38:34 +02001283 i = tape->blk_size - tape->merge_bh_size %
Borislav Petkov54bb2072008-02-06 02:57:52 +01001284 tape->blk_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285 bh = tape->bh->b_reqnext;
1286 while (bh) {
1287 atomic_set(&bh->b_count, 0);
1288 bh = bh->b_reqnext;
1289 }
1290 bh = tape->bh;
1291 while (i) {
1292 if (bh == NULL) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001293 printk(KERN_INFO "ide-tape: bug,"
1294 " bh NULL\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295 break;
1296 }
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001297 min = min(i, (unsigned int)(bh->b_size -
1298 atomic_read(&bh->b_count)));
1299 memset(bh->b_data + atomic_read(&bh->b_count),
1300 0, min);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301 atomic_add(min, &bh->b_count);
1302 i -= min;
1303 bh = bh->b_reqnext;
1304 }
1305 }
1306 (void) idetape_add_chrdev_write_request(drive, blocks);
Borislav Petkov01a63aeb2008-04-27 15:38:34 +02001307 tape->merge_bh_size = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308 }
Borislav Petkov077e3bd2008-04-27 15:38:34 +02001309 if (tape->merge_bh != NULL) {
1310 ide_tape_kfree_buffer(tape);
1311 tape->merge_bh = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312 }
Borislav Petkov54abf372008-02-06 02:57:52 +01001313 tape->chrdev_dir = IDETAPE_DIR_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314}
1315
Borislav Petkov83042b22008-04-27 15:38:27 +02001316static int idetape_init_read(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317{
1318 idetape_tape_t *tape = drive->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319 int bytes_read;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320
1321 /* Initialize read operation */
Borislav Petkov54abf372008-02-06 02:57:52 +01001322 if (tape->chrdev_dir != IDETAPE_DIR_READ) {
1323 if (tape->chrdev_dir == IDETAPE_DIR_WRITE) {
Borislav Petkovd9df9372008-04-27 15:38:34 +02001324 ide_tape_flush_merge_buffer(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325 idetape_flush_tape_buffers(drive);
1326 }
Borislav Petkov077e3bd2008-04-27 15:38:34 +02001327 if (tape->merge_bh || tape->merge_bh_size) {
Borislav Petkov01a63aeb2008-04-27 15:38:34 +02001328 printk(KERN_ERR "ide-tape: merge_bh_size should be"
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001329 " 0 now\n");
Borislav Petkov01a63aeb2008-04-27 15:38:34 +02001330 tape->merge_bh_size = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331 }
Borislav Petkov077e3bd2008-04-27 15:38:34 +02001332 tape->merge_bh = ide_tape_kmalloc_buffer(tape, 0, 0);
1333 if (!tape->merge_bh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334 return -ENOMEM;
Borislav Petkov54abf372008-02-06 02:57:52 +01001335 tape->chrdev_dir = IDETAPE_DIR_READ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336
1337 /*
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001338 * Issue a read 0 command to ensure that DSC handshake is
1339 * switched from completion mode to buffer available mode.
1340 * No point in issuing this if DSC overlap isn't supported, some
1341 * drives (Seagate STT3401A) will return an error.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342 */
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +02001343 if (drive->dev_flags & IDE_DFLAG_DSC_OVERLAP) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001344 bytes_read = idetape_queue_rw_tail(drive,
1345 REQ_IDETAPE_READ, 0,
Borislav Petkov077e3bd2008-04-27 15:38:34 +02001346 tape->merge_bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347 if (bytes_read < 0) {
Borislav Petkov077e3bd2008-04-27 15:38:34 +02001348 ide_tape_kfree_buffer(tape);
1349 tape->merge_bh = NULL;
Borislav Petkov54abf372008-02-06 02:57:52 +01001350 tape->chrdev_dir = IDETAPE_DIR_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351 return bytes_read;
1352 }
1353 }
1354 }
Borislav Petkov5e69bd92008-04-27 15:38:25 +02001355
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356 return 0;
1357}
1358
Borislav Petkov5bd50dc2008-04-27 15:38:28 +02001359/* called from idetape_chrdev_read() to service a chrdev read request. */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001360static int idetape_add_chrdev_read_request(ide_drive_t *drive, int blocks)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361{
1362 idetape_tape_t *tape = drive->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001364 debug_log(DBG_PROCS, "Enter %s, %d blocks\n", __func__, blocks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001366 /* If we are at a filemark, return a read length of 0 */
Borislav Petkovf2e3ab52008-07-23 19:56:01 +02001367 if (test_bit(IDE_AFLAG_FILEMARK, &drive->atapi_flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368 return 0;
1369
Borislav Petkov83042b22008-04-27 15:38:27 +02001370 idetape_init_read(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371
Borislav Petkov5e69bd92008-04-27 15:38:25 +02001372 return idetape_queue_rw_tail(drive, REQ_IDETAPE_READ, blocks,
Borislav Petkov077e3bd2008-04-27 15:38:34 +02001373 tape->merge_bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374}
1375
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001376static void idetape_pad_zeros(ide_drive_t *drive, int bcount)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377{
1378 idetape_tape_t *tape = drive->driver_data;
1379 struct idetape_bh *bh;
1380 int blocks;
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001381
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382 while (bcount) {
1383 unsigned int count;
1384
Borislav Petkov077e3bd2008-04-27 15:38:34 +02001385 bh = tape->merge_bh;
Borislav Petkovf73850a2008-04-27 15:38:33 +02001386 count = min(tape->buffer_size, bcount);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387 bcount -= count;
Borislav Petkov54bb2072008-02-06 02:57:52 +01001388 blocks = count / tape->blk_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389 while (count) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001390 atomic_set(&bh->b_count,
1391 min(count, (unsigned int)bh->b_size));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392 memset(bh->b_data, 0, atomic_read(&bh->b_count));
1393 count -= atomic_read(&bh->b_count);
1394 bh = bh->b_reqnext;
1395 }
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001396 idetape_queue_rw_tail(drive, REQ_IDETAPE_WRITE, blocks,
Borislav Petkov077e3bd2008-04-27 15:38:34 +02001397 tape->merge_bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398 }
1399}
1400
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001402 * Rewinds the tape to the Beginning Of the current Partition (BOP). We
1403 * currently support only one partition.
1404 */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001405static int idetape_rewind_tape(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406{
Bartlomiej Zolnierkiewicz2ac07d92008-10-10 22:39:38 +02001407 struct ide_tape_obj *tape = drive->driver_data;
1408 struct gendisk *disk = tape->disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409 int retval;
Borislav Petkovd236d742008-04-18 00:46:27 +02001410 struct ide_atapi_pc pc;
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001411
1412 debug_log(DBG_SENSE, "Enter %s\n", __func__);
1413
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414 idetape_create_rewind_cmd(drive, &pc);
Bartlomiej Zolnierkiewicz2ac07d92008-10-10 22:39:38 +02001415 retval = ide_queue_pc_tail(drive, disk, &pc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416 if (retval)
1417 return retval;
1418
1419 idetape_create_read_position_cmd(&pc);
Bartlomiej Zolnierkiewicz2ac07d92008-10-10 22:39:38 +02001420 retval = ide_queue_pc_tail(drive, disk, &pc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421 if (retval)
1422 return retval;
1423 return 0;
1424}
1425
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001426/* mtio.h compatible commands should be issued to the chrdev interface. */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001427static int idetape_blkdev_ioctl(ide_drive_t *drive, unsigned int cmd,
1428 unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429{
1430 idetape_tape_t *tape = drive->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431 void __user *argp = (void __user *)arg;
1432
Borislav Petkovd59823f2008-02-02 19:56:51 +01001433 struct idetape_config {
1434 int dsc_rw_frequency;
1435 int dsc_media_access_frequency;
1436 int nr_stages;
1437 } config;
1438
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001439 debug_log(DBG_PROCS, "Enter %s\n", __func__);
1440
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441 switch (cmd) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001442 case 0x0340:
1443 if (copy_from_user(&config, argp, sizeof(config)))
1444 return -EFAULT;
1445 tape->best_dsc_rw_freq = config.dsc_rw_frequency;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001446 break;
1447 case 0x0350:
1448 config.dsc_rw_frequency = (int) tape->best_dsc_rw_freq;
Borislav Petkov83042b22008-04-27 15:38:27 +02001449 config.nr_stages = 1;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001450 if (copy_to_user(argp, &config, sizeof(config)))
1451 return -EFAULT;
1452 break;
1453 default:
1454 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455 }
1456 return 0;
1457}
1458
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001459static int idetape_space_over_filemarks(ide_drive_t *drive, short mt_op,
1460 int mt_count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461{
1462 idetape_tape_t *tape = drive->driver_data;
Bartlomiej Zolnierkiewicz2ac07d92008-10-10 22:39:38 +02001463 struct gendisk *disk = tape->disk;
Borislav Petkovd236d742008-04-18 00:46:27 +02001464 struct ide_atapi_pc pc;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001465 int retval, count = 0;
Borislav Petkovb6422012008-02-02 19:56:49 +01001466 int sprev = !!(tape->caps[4] & 0x20);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467
1468 if (mt_count == 0)
1469 return 0;
1470 if (MTBSF == mt_op || MTBSFM == mt_op) {
Borislav Petkovb6422012008-02-02 19:56:49 +01001471 if (!sprev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472 return -EIO;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001473 mt_count = -mt_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474 }
1475
Borislav Petkov54abf372008-02-06 02:57:52 +01001476 if (tape->chrdev_dir == IDETAPE_DIR_READ) {
Borislav Petkov01a63aeb2008-04-27 15:38:34 +02001477 tape->merge_bh_size = 0;
Borislav Petkovf2e3ab52008-07-23 19:56:01 +02001478 if (test_and_clear_bit(IDE_AFLAG_FILEMARK, &drive->atapi_flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479 ++count;
Borislav Petkovec0fdb02008-04-27 15:38:34 +02001480 ide_tape_discard_merge_buffer(drive, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481 }
1482
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483 switch (mt_op) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001484 case MTFSF:
1485 case MTBSF:
1486 idetape_create_space_cmd(&pc, mt_count - count,
1487 IDETAPE_SPACE_OVER_FILEMARK);
Bartlomiej Zolnierkiewicz2ac07d92008-10-10 22:39:38 +02001488 return ide_queue_pc_tail(drive, disk, &pc);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001489 case MTFSFM:
1490 case MTBSFM:
1491 if (!sprev)
1492 return -EIO;
1493 retval = idetape_space_over_filemarks(drive, MTFSF,
1494 mt_count - count);
1495 if (retval)
1496 return retval;
1497 count = (MTBSFM == mt_op ? 1 : -1);
1498 return idetape_space_over_filemarks(drive, MTFSF, count);
1499 default:
1500 printk(KERN_ERR "ide-tape: MTIO operation %d not supported\n",
1501 mt_op);
1502 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503 }
1504}
1505
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001507 * Our character device read / write functions.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001508 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001509 * The tape is optimized to maximize throughput when it is transferring an
1510 * integral number of the "continuous transfer limit", which is a parameter of
1511 * the specific tape (26kB on my particular tape, 32kB for Onstream).
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001513 * As of version 1.3 of the driver, the character device provides an abstract
1514 * continuous view of the media - any mix of block sizes (even 1 byte) on the
1515 * same backup/restore procedure is supported. The driver will internally
1516 * convert the requests to the recommended transfer unit, so that an unmatch
1517 * between the user's block size to the recommended size will only result in a
1518 * (slightly) increased driver overhead, but will no longer hit performance.
1519 * This is not applicable to Onstream.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520 */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001521static ssize_t idetape_chrdev_read(struct file *file, char __user *buf,
1522 size_t count, loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523{
Borislav Petkov5aeddf92008-10-13 21:39:34 +02001524 struct ide_tape_obj *tape = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001525 ide_drive_t *drive = tape->drive;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001526 ssize_t bytes_read, temp, actually_read = 0, rc;
Daniel Walkerdcd96372006-06-25 05:47:37 -07001527 ssize_t ret = 0;
Borislav Petkovb6422012008-02-02 19:56:49 +01001528 u16 ctl = *(u16 *)&tape->caps[12];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001530 debug_log(DBG_CHRDEV, "Enter %s, count %Zd\n", __func__, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531
Borislav Petkov54abf372008-02-06 02:57:52 +01001532 if (tape->chrdev_dir != IDETAPE_DIR_READ) {
Borislav Petkovf2e3ab52008-07-23 19:56:01 +02001533 if (test_bit(IDE_AFLAG_DETECT_BS, &drive->atapi_flags))
Borislav Petkov54bb2072008-02-06 02:57:52 +01001534 if (count > tape->blk_size &&
1535 (count % tape->blk_size) == 0)
1536 tape->user_bs_factor = count / tape->blk_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537 }
Borislav Petkov83042b22008-04-27 15:38:27 +02001538 rc = idetape_init_read(drive);
Borislav Petkov8d06bfa2008-02-06 02:57:53 +01001539 if (rc < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540 return rc;
1541 if (count == 0)
1542 return (0);
Borislav Petkov01a63aeb2008-04-27 15:38:34 +02001543 if (tape->merge_bh_size) {
1544 actually_read = min((unsigned int)(tape->merge_bh_size),
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001545 (unsigned int)count);
Borislav Petkov99d74e62008-04-27 15:38:25 +02001546 if (idetape_copy_stage_to_user(tape, buf, actually_read))
Daniel Walkerdcd96372006-06-25 05:47:37 -07001547 ret = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548 buf += actually_read;
Borislav Petkov01a63aeb2008-04-27 15:38:34 +02001549 tape->merge_bh_size -= actually_read;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550 count -= actually_read;
1551 }
Borislav Petkovf73850a2008-04-27 15:38:33 +02001552 while (count >= tape->buffer_size) {
Borislav Petkovb6422012008-02-02 19:56:49 +01001553 bytes_read = idetape_add_chrdev_read_request(drive, ctl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554 if (bytes_read <= 0)
1555 goto finish;
Borislav Petkov99d74e62008-04-27 15:38:25 +02001556 if (idetape_copy_stage_to_user(tape, buf, bytes_read))
Daniel Walkerdcd96372006-06-25 05:47:37 -07001557 ret = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558 buf += bytes_read;
1559 count -= bytes_read;
1560 actually_read += bytes_read;
1561 }
1562 if (count) {
Borislav Petkovb6422012008-02-02 19:56:49 +01001563 bytes_read = idetape_add_chrdev_read_request(drive, ctl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001564 if (bytes_read <= 0)
1565 goto finish;
1566 temp = min((unsigned long)count, (unsigned long)bytes_read);
Borislav Petkov99d74e62008-04-27 15:38:25 +02001567 if (idetape_copy_stage_to_user(tape, buf, temp))
Daniel Walkerdcd96372006-06-25 05:47:37 -07001568 ret = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569 actually_read += temp;
Borislav Petkov01a63aeb2008-04-27 15:38:34 +02001570 tape->merge_bh_size = bytes_read-temp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571 }
1572finish:
Borislav Petkovf2e3ab52008-07-23 19:56:01 +02001573 if (!actually_read && test_bit(IDE_AFLAG_FILEMARK, &drive->atapi_flags)) {
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001574 debug_log(DBG_SENSE, "%s: spacing over filemark\n", tape->name);
1575
Linus Torvalds1da177e2005-04-16 15:20:36 -07001576 idetape_space_over_filemarks(drive, MTFSF, 1);
1577 return 0;
1578 }
Daniel Walkerdcd96372006-06-25 05:47:37 -07001579
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001580 return ret ? ret : actually_read;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581}
1582
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001583static ssize_t idetape_chrdev_write(struct file *file, const char __user *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584 size_t count, loff_t *ppos)
1585{
Borislav Petkov5aeddf92008-10-13 21:39:34 +02001586 struct ide_tape_obj *tape = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587 ide_drive_t *drive = tape->drive;
Daniel Walkerdcd96372006-06-25 05:47:37 -07001588 ssize_t actually_written = 0;
1589 ssize_t ret = 0;
Borislav Petkovb6422012008-02-02 19:56:49 +01001590 u16 ctl = *(u16 *)&tape->caps[12];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591
1592 /* The drive is write protected. */
1593 if (tape->write_prot)
1594 return -EACCES;
1595
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001596 debug_log(DBG_CHRDEV, "Enter %s, count %Zd\n", __func__, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001597
1598 /* Initialize write operation */
Borislav Petkov54abf372008-02-06 02:57:52 +01001599 if (tape->chrdev_dir != IDETAPE_DIR_WRITE) {
1600 if (tape->chrdev_dir == IDETAPE_DIR_READ)
Borislav Petkovec0fdb02008-04-27 15:38:34 +02001601 ide_tape_discard_merge_buffer(drive, 1);
Borislav Petkov077e3bd2008-04-27 15:38:34 +02001602 if (tape->merge_bh || tape->merge_bh_size) {
Borislav Petkov01a63aeb2008-04-27 15:38:34 +02001603 printk(KERN_ERR "ide-tape: merge_bh_size "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604 "should be 0 now\n");
Borislav Petkov01a63aeb2008-04-27 15:38:34 +02001605 tape->merge_bh_size = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606 }
Borislav Petkov077e3bd2008-04-27 15:38:34 +02001607 tape->merge_bh = ide_tape_kmalloc_buffer(tape, 0, 0);
1608 if (!tape->merge_bh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001609 return -ENOMEM;
Borislav Petkov54abf372008-02-06 02:57:52 +01001610 tape->chrdev_dir = IDETAPE_DIR_WRITE;
Borislav Petkov077e3bd2008-04-27 15:38:34 +02001611 idetape_init_merge_buffer(tape);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001612
1613 /*
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001614 * Issue a write 0 command to ensure that DSC handshake is
1615 * switched from completion mode to buffer available mode. No
1616 * point in issuing this if DSC overlap isn't supported, some
1617 * drives (Seagate STT3401A) will return an error.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001618 */
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +02001619 if (drive->dev_flags & IDE_DFLAG_DSC_OVERLAP) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001620 ssize_t retval = idetape_queue_rw_tail(drive,
1621 REQ_IDETAPE_WRITE, 0,
Borislav Petkov077e3bd2008-04-27 15:38:34 +02001622 tape->merge_bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623 if (retval < 0) {
Borislav Petkov077e3bd2008-04-27 15:38:34 +02001624 ide_tape_kfree_buffer(tape);
1625 tape->merge_bh = NULL;
Borislav Petkov54abf372008-02-06 02:57:52 +01001626 tape->chrdev_dir = IDETAPE_DIR_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001627 return retval;
1628 }
1629 }
1630 }
1631 if (count == 0)
1632 return (0);
Borislav Petkov01a63aeb2008-04-27 15:38:34 +02001633 if (tape->merge_bh_size) {
1634 if (tape->merge_bh_size >= tape->buffer_size) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001635 printk(KERN_ERR "ide-tape: bug: merge buf too big\n");
Borislav Petkov01a63aeb2008-04-27 15:38:34 +02001636 tape->merge_bh_size = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001637 }
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001638 actually_written = min((unsigned int)
Borislav Petkov01a63aeb2008-04-27 15:38:34 +02001639 (tape->buffer_size - tape->merge_bh_size),
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001640 (unsigned int)count);
Borislav Petkov8646c882008-04-27 15:38:26 +02001641 if (idetape_copy_stage_from_user(tape, buf, actually_written))
Daniel Walkerdcd96372006-06-25 05:47:37 -07001642 ret = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643 buf += actually_written;
Borislav Petkov01a63aeb2008-04-27 15:38:34 +02001644 tape->merge_bh_size += actually_written;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001645 count -= actually_written;
1646
Borislav Petkov01a63aeb2008-04-27 15:38:34 +02001647 if (tape->merge_bh_size == tape->buffer_size) {
Daniel Walkerdcd96372006-06-25 05:47:37 -07001648 ssize_t retval;
Borislav Petkov01a63aeb2008-04-27 15:38:34 +02001649 tape->merge_bh_size = 0;
Borislav Petkovb6422012008-02-02 19:56:49 +01001650 retval = idetape_add_chrdev_write_request(drive, ctl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001651 if (retval <= 0)
1652 return (retval);
1653 }
1654 }
Borislav Petkovf73850a2008-04-27 15:38:33 +02001655 while (count >= tape->buffer_size) {
Daniel Walkerdcd96372006-06-25 05:47:37 -07001656 ssize_t retval;
Borislav Petkovf73850a2008-04-27 15:38:33 +02001657 if (idetape_copy_stage_from_user(tape, buf, tape->buffer_size))
Daniel Walkerdcd96372006-06-25 05:47:37 -07001658 ret = -EFAULT;
Borislav Petkovf73850a2008-04-27 15:38:33 +02001659 buf += tape->buffer_size;
1660 count -= tape->buffer_size;
Borislav Petkovb6422012008-02-02 19:56:49 +01001661 retval = idetape_add_chrdev_write_request(drive, ctl);
Borislav Petkovf73850a2008-04-27 15:38:33 +02001662 actually_written += tape->buffer_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001663 if (retval <= 0)
1664 return (retval);
1665 }
1666 if (count) {
1667 actually_written += count;
Borislav Petkov8646c882008-04-27 15:38:26 +02001668 if (idetape_copy_stage_from_user(tape, buf, count))
Daniel Walkerdcd96372006-06-25 05:47:37 -07001669 ret = -EFAULT;
Borislav Petkov01a63aeb2008-04-27 15:38:34 +02001670 tape->merge_bh_size += count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001671 }
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001672 return ret ? ret : actually_written;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001673}
1674
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001675static int idetape_write_filemark(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001676{
Bartlomiej Zolnierkiewicz2ac07d92008-10-10 22:39:38 +02001677 struct ide_tape_obj *tape = drive->driver_data;
Borislav Petkovd236d742008-04-18 00:46:27 +02001678 struct ide_atapi_pc pc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679
1680 /* Write a filemark */
1681 idetape_create_write_filemark_cmd(drive, &pc, 1);
Bartlomiej Zolnierkiewicz2ac07d92008-10-10 22:39:38 +02001682 if (ide_queue_pc_tail(drive, tape->disk, &pc)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001683 printk(KERN_ERR "ide-tape: Couldn't write a filemark\n");
1684 return -EIO;
1685 }
1686 return 0;
1687}
1688
1689/*
Borislav Petkovd99c9da2008-02-02 19:56:51 +01001690 * Called from idetape_chrdev_ioctl when the general mtio MTIOCTOP ioctl is
1691 * requested.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001692 *
Borislav Petkovd99c9da2008-02-02 19:56:51 +01001693 * Note: MTBSF and MTBSFM are not supported when the tape doesn't support
1694 * spacing over filemarks in the reverse direction. In this case, MTFSFM is also
Borislav Petkov5bd50dc2008-04-27 15:38:28 +02001695 * usually not supported.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001696 *
Borislav Petkovd99c9da2008-02-02 19:56:51 +01001697 * The following commands are currently not supported:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001698 *
Borislav Petkovd99c9da2008-02-02 19:56:51 +01001699 * MTFSS, MTBSS, MTWSM, MTSETDENSITY, MTSETDRVBUFFER, MT_ST_BOOLEANS,
1700 * MT_ST_WRITE_THRESHOLD.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001701 */
Borislav Petkovd99c9da2008-02-02 19:56:51 +01001702static int idetape_mtioctop(ide_drive_t *drive, short mt_op, int mt_count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001703{
1704 idetape_tape_t *tape = drive->driver_data;
Bartlomiej Zolnierkiewicz2ac07d92008-10-10 22:39:38 +02001705 struct gendisk *disk = tape->disk;
Borislav Petkovd236d742008-04-18 00:46:27 +02001706 struct ide_atapi_pc pc;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001707 int i, retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001708
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001709 debug_log(DBG_ERR, "Handling MTIOCTOP ioctl: mt_op=%d, mt_count=%d\n",
1710 mt_op, mt_count);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001711
Linus Torvalds1da177e2005-04-16 15:20:36 -07001712 switch (mt_op) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001713 case MTFSF:
1714 case MTFSFM:
1715 case MTBSF:
1716 case MTBSFM:
1717 if (!mt_count)
1718 return 0;
1719 return idetape_space_over_filemarks(drive, mt_op, mt_count);
1720 default:
1721 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001722 }
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001723
Linus Torvalds1da177e2005-04-16 15:20:36 -07001724 switch (mt_op) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001725 case MTWEOF:
1726 if (tape->write_prot)
1727 return -EACCES;
Borislav Petkovec0fdb02008-04-27 15:38:34 +02001728 ide_tape_discard_merge_buffer(drive, 1);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001729 for (i = 0; i < mt_count; i++) {
1730 retval = idetape_write_filemark(drive);
1731 if (retval)
1732 return retval;
1733 }
1734 return 0;
1735 case MTREW:
Borislav Petkovec0fdb02008-04-27 15:38:34 +02001736 ide_tape_discard_merge_buffer(drive, 0);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001737 if (idetape_rewind_tape(drive))
1738 return -EIO;
1739 return 0;
1740 case MTLOAD:
Borislav Petkovec0fdb02008-04-27 15:38:34 +02001741 ide_tape_discard_merge_buffer(drive, 0);
Bartlomiej Zolnierkiewicz0c8a6c72008-10-10 22:39:39 +02001742 return ide_do_start_stop(drive, disk, IDETAPE_LU_LOAD_MASK);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001743 case MTUNLOAD:
1744 case MTOFFL:
1745 /*
1746 * If door is locked, attempt to unlock before
1747 * attempting to eject.
1748 */
1749 if (tape->door_locked) {
Bartlomiej Zolnierkiewicz05780422008-10-10 22:39:38 +02001750 if (!ide_set_media_lock(drive, disk, 0))
Bartlomiej Zolnierkiewicz385a4b82008-10-10 22:39:37 +02001751 tape->door_locked = DOOR_UNLOCKED;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001752 }
Borislav Petkovec0fdb02008-04-27 15:38:34 +02001753 ide_tape_discard_merge_buffer(drive, 0);
Bartlomiej Zolnierkiewicz0c8a6c72008-10-10 22:39:39 +02001754 retval = ide_do_start_stop(drive, disk, !IDETAPE_LU_LOAD_MASK);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001755 if (!retval)
Borislav Petkovf2e3ab52008-07-23 19:56:01 +02001756 clear_bit(IDE_AFLAG_MEDIUM_PRESENT, &drive->atapi_flags);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001757 return retval;
1758 case MTNOP:
Borislav Petkovec0fdb02008-04-27 15:38:34 +02001759 ide_tape_discard_merge_buffer(drive, 0);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001760 return idetape_flush_tape_buffers(drive);
1761 case MTRETEN:
Borislav Petkovec0fdb02008-04-27 15:38:34 +02001762 ide_tape_discard_merge_buffer(drive, 0);
Bartlomiej Zolnierkiewicz0c8a6c72008-10-10 22:39:39 +02001763 return ide_do_start_stop(drive, disk,
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001764 IDETAPE_LU_RETENSION_MASK | IDETAPE_LU_LOAD_MASK);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001765 case MTEOM:
1766 idetape_create_space_cmd(&pc, 0, IDETAPE_SPACE_TO_EOD);
Bartlomiej Zolnierkiewicz2ac07d92008-10-10 22:39:38 +02001767 return ide_queue_pc_tail(drive, disk, &pc);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001768 case MTERASE:
1769 (void)idetape_rewind_tape(drive);
1770 idetape_create_erase_cmd(&pc);
Bartlomiej Zolnierkiewicz2ac07d92008-10-10 22:39:38 +02001771 return ide_queue_pc_tail(drive, disk, &pc);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001772 case MTSETBLK:
1773 if (mt_count) {
1774 if (mt_count < tape->blk_size ||
1775 mt_count % tape->blk_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001776 return -EIO;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001777 tape->user_bs_factor = mt_count / tape->blk_size;
Borislav Petkovf2e3ab52008-07-23 19:56:01 +02001778 clear_bit(IDE_AFLAG_DETECT_BS, &drive->atapi_flags);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001779 } else
Borislav Petkovf2e3ab52008-07-23 19:56:01 +02001780 set_bit(IDE_AFLAG_DETECT_BS, &drive->atapi_flags);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001781 return 0;
1782 case MTSEEK:
Borislav Petkovec0fdb02008-04-27 15:38:34 +02001783 ide_tape_discard_merge_buffer(drive, 0);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001784 return idetape_position_tape(drive,
1785 mt_count * tape->user_bs_factor, tape->partition, 0);
1786 case MTSETPART:
Borislav Petkovec0fdb02008-04-27 15:38:34 +02001787 ide_tape_discard_merge_buffer(drive, 0);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001788 return idetape_position_tape(drive, 0, mt_count, 0);
1789 case MTFSR:
1790 case MTBSR:
1791 case MTLOCK:
Bartlomiej Zolnierkiewicz05780422008-10-10 22:39:38 +02001792 retval = ide_set_media_lock(drive, disk, 1);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001793 if (retval)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001794 return retval;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001795 tape->door_locked = DOOR_EXPLICITLY_LOCKED;
1796 return 0;
1797 case MTUNLOCK:
Bartlomiej Zolnierkiewicz05780422008-10-10 22:39:38 +02001798 retval = ide_set_media_lock(drive, disk, 0);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001799 if (retval)
1800 return retval;
1801 tape->door_locked = DOOR_UNLOCKED;
1802 return 0;
1803 default:
1804 printk(KERN_ERR "ide-tape: MTIO operation %d not supported\n",
1805 mt_op);
1806 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807 }
1808}
1809
1810/*
Borislav Petkovd99c9da2008-02-02 19:56:51 +01001811 * Our character device ioctls. General mtio.h magnetic io commands are
1812 * supported here, and not in the corresponding block interface. Our own
1813 * ide-tape ioctls are supported on both interfaces.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001814 */
Borislav Petkovd99c9da2008-02-02 19:56:51 +01001815static int idetape_chrdev_ioctl(struct inode *inode, struct file *file,
1816 unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001817{
Borislav Petkov5aeddf92008-10-13 21:39:34 +02001818 struct ide_tape_obj *tape = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001819 ide_drive_t *drive = tape->drive;
1820 struct mtop mtop;
1821 struct mtget mtget;
1822 struct mtpos mtpos;
Borislav Petkov54bb2072008-02-06 02:57:52 +01001823 int block_offset = 0, position = tape->first_frame;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001824 void __user *argp = (void __user *)arg;
1825
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001826 debug_log(DBG_CHRDEV, "Enter %s, cmd=%u\n", __func__, cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001827
Borislav Petkov54abf372008-02-06 02:57:52 +01001828 if (tape->chrdev_dir == IDETAPE_DIR_WRITE) {
Borislav Petkovd9df9372008-04-27 15:38:34 +02001829 ide_tape_flush_merge_buffer(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001830 idetape_flush_tape_buffers(drive);
1831 }
1832 if (cmd == MTIOCGET || cmd == MTIOCPOS) {
Borislav Petkov01a63aeb2008-04-27 15:38:34 +02001833 block_offset = tape->merge_bh_size /
Borislav Petkov54bb2072008-02-06 02:57:52 +01001834 (tape->blk_size * tape->user_bs_factor);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001835 position = idetape_read_position(drive);
1836 if (position < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001837 return -EIO;
1838 }
1839 switch (cmd) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001840 case MTIOCTOP:
1841 if (copy_from_user(&mtop, argp, sizeof(struct mtop)))
1842 return -EFAULT;
1843 return idetape_mtioctop(drive, mtop.mt_op, mtop.mt_count);
1844 case MTIOCGET:
1845 memset(&mtget, 0, sizeof(struct mtget));
1846 mtget.mt_type = MT_ISSCSI2;
1847 mtget.mt_blkno = position / tape->user_bs_factor - block_offset;
1848 mtget.mt_dsreg =
1849 ((tape->blk_size * tape->user_bs_factor)
1850 << MT_ST_BLKSIZE_SHIFT) & MT_ST_BLKSIZE_MASK;
Borislav Petkov54bb2072008-02-06 02:57:52 +01001851
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001852 if (tape->drv_write_prot)
1853 mtget.mt_gstat |= GMT_WR_PROT(0xffffffff);
1854
1855 if (copy_to_user(argp, &mtget, sizeof(struct mtget)))
1856 return -EFAULT;
1857 return 0;
1858 case MTIOCPOS:
1859 mtpos.mt_blkno = position / tape->user_bs_factor - block_offset;
1860 if (copy_to_user(argp, &mtpos, sizeof(struct mtpos)))
1861 return -EFAULT;
1862 return 0;
1863 default:
1864 if (tape->chrdev_dir == IDETAPE_DIR_READ)
Borislav Petkovec0fdb02008-04-27 15:38:34 +02001865 ide_tape_discard_merge_buffer(drive, 1);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001866 return idetape_blkdev_ioctl(drive, cmd, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001867 }
1868}
1869
Borislav Petkov3cffb9c2008-02-02 19:56:50 +01001870/*
1871 * Do a mode sense page 0 with block descriptor and if it succeeds set the tape
1872 * block size with the reported value.
1873 */
1874static void ide_tape_get_bsize_from_bdesc(ide_drive_t *drive)
1875{
1876 idetape_tape_t *tape = drive->driver_data;
Borislav Petkovd236d742008-04-18 00:46:27 +02001877 struct ide_atapi_pc pc;
Borislav Petkov3cffb9c2008-02-02 19:56:50 +01001878
1879 idetape_create_mode_sense_cmd(&pc, IDETAPE_BLOCK_DESCRIPTOR);
Bartlomiej Zolnierkiewicz2ac07d92008-10-10 22:39:38 +02001880 if (ide_queue_pc_tail(drive, tape->disk, &pc)) {
Borislav Petkov3cffb9c2008-02-02 19:56:50 +01001881 printk(KERN_ERR "ide-tape: Can't get block descriptor\n");
Borislav Petkov54bb2072008-02-06 02:57:52 +01001882 if (tape->blk_size == 0) {
Borislav Petkov3cffb9c2008-02-02 19:56:50 +01001883 printk(KERN_WARNING "ide-tape: Cannot deal with zero "
1884 "block size, assuming 32k\n");
Borislav Petkov54bb2072008-02-06 02:57:52 +01001885 tape->blk_size = 32768;
Borislav Petkov3cffb9c2008-02-02 19:56:50 +01001886 }
1887 return;
1888 }
Borislav Petkovd236d742008-04-18 00:46:27 +02001889 tape->blk_size = (pc.buf[4 + 5] << 16) +
1890 (pc.buf[4 + 6] << 8) +
1891 pc.buf[4 + 7];
1892 tape->drv_write_prot = (pc.buf[2] & 0x80) >> 7;
Borislav Petkov3cffb9c2008-02-02 19:56:50 +01001893}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001894
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001895static int idetape_chrdev_open(struct inode *inode, struct file *filp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001896{
1897 unsigned int minor = iminor(inode), i = minor & ~0xc0;
1898 ide_drive_t *drive;
1899 idetape_tape_t *tape;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001900 int retval;
1901
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001902 if (i >= MAX_HWIFS * MAX_DRIVES)
1903 return -ENXIO;
1904
Jonathan Corbet04f4ac92008-05-15 12:01:56 -06001905 lock_kernel();
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001906 tape = ide_tape_chrdev_get(i);
Jonathan Corbet04f4ac92008-05-15 12:01:56 -06001907 if (!tape) {
1908 unlock_kernel();
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001909 return -ENXIO;
Jonathan Corbet04f4ac92008-05-15 12:01:56 -06001910 }
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001911
1912 debug_log(DBG_CHRDEV, "Enter %s\n", __func__);
1913
Linus Torvalds1da177e2005-04-16 15:20:36 -07001914 /*
1915 * We really want to do nonseekable_open(inode, filp); here, but some
1916 * versions of tar incorrectly call lseek on tapes and bail out if that
1917 * fails. So we disallow pread() and pwrite(), but permit lseeks.
1918 */
1919 filp->f_mode &= ~(FMODE_PREAD | FMODE_PWRITE);
1920
Linus Torvalds1da177e2005-04-16 15:20:36 -07001921 drive = tape->drive;
1922
1923 filp->private_data = tape;
1924
Borislav Petkovf2e3ab52008-07-23 19:56:01 +02001925 if (test_and_set_bit(IDE_AFLAG_BUSY, &drive->atapi_flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001926 retval = -EBUSY;
1927 goto out_put_tape;
1928 }
1929
1930 retval = idetape_wait_ready(drive, 60 * HZ);
1931 if (retval) {
Borislav Petkovf2e3ab52008-07-23 19:56:01 +02001932 clear_bit(IDE_AFLAG_BUSY, &drive->atapi_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001933 printk(KERN_ERR "ide-tape: %s: drive not ready\n", tape->name);
1934 goto out_put_tape;
1935 }
1936
1937 idetape_read_position(drive);
Borislav Petkovf2e3ab52008-07-23 19:56:01 +02001938 if (!test_bit(IDE_AFLAG_ADDRESS_VALID, &drive->atapi_flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001939 (void)idetape_rewind_tape(drive);
1940
Linus Torvalds1da177e2005-04-16 15:20:36 -07001941 /* Read block size and write protect status from drive. */
Borislav Petkov3cffb9c2008-02-02 19:56:50 +01001942 ide_tape_get_bsize_from_bdesc(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001943
1944 /* Set write protect flag if device is opened as read-only. */
1945 if ((filp->f_flags & O_ACCMODE) == O_RDONLY)
1946 tape->write_prot = 1;
1947 else
1948 tape->write_prot = tape->drv_write_prot;
1949
1950 /* Make sure drive isn't write protected if user wants to write. */
1951 if (tape->write_prot) {
1952 if ((filp->f_flags & O_ACCMODE) == O_WRONLY ||
1953 (filp->f_flags & O_ACCMODE) == O_RDWR) {
Borislav Petkovf2e3ab52008-07-23 19:56:01 +02001954 clear_bit(IDE_AFLAG_BUSY, &drive->atapi_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001955 retval = -EROFS;
1956 goto out_put_tape;
1957 }
1958 }
1959
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001960 /* Lock the tape drive door so user can't eject. */
Borislav Petkov54abf372008-02-06 02:57:52 +01001961 if (tape->chrdev_dir == IDETAPE_DIR_NONE) {
Bartlomiej Zolnierkiewicz05780422008-10-10 22:39:38 +02001962 if (!ide_set_media_lock(drive, tape->disk, 1)) {
Bartlomiej Zolnierkiewicz385a4b82008-10-10 22:39:37 +02001963 if (tape->door_locked != DOOR_EXPLICITLY_LOCKED)
1964 tape->door_locked = DOOR_LOCKED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001965 }
1966 }
Jonathan Corbet04f4ac92008-05-15 12:01:56 -06001967 unlock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001968 return 0;
1969
1970out_put_tape:
1971 ide_tape_put(tape);
Jonathan Corbet04f4ac92008-05-15 12:01:56 -06001972 unlock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001973 return retval;
1974}
1975
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001976static void idetape_write_release(ide_drive_t *drive, unsigned int minor)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001977{
1978 idetape_tape_t *tape = drive->driver_data;
1979
Borislav Petkovd9df9372008-04-27 15:38:34 +02001980 ide_tape_flush_merge_buffer(drive);
Borislav Petkov077e3bd2008-04-27 15:38:34 +02001981 tape->merge_bh = ide_tape_kmalloc_buffer(tape, 1, 0);
1982 if (tape->merge_bh != NULL) {
Borislav Petkov54bb2072008-02-06 02:57:52 +01001983 idetape_pad_zeros(drive, tape->blk_size *
1984 (tape->user_bs_factor - 1));
Borislav Petkov077e3bd2008-04-27 15:38:34 +02001985 ide_tape_kfree_buffer(tape);
1986 tape->merge_bh = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001987 }
1988 idetape_write_filemark(drive);
1989 idetape_flush_tape_buffers(drive);
1990 idetape_flush_tape_buffers(drive);
1991}
1992
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001993static int idetape_chrdev_release(struct inode *inode, struct file *filp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001994{
Borislav Petkov5aeddf92008-10-13 21:39:34 +02001995 struct ide_tape_obj *tape = filp->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001996 ide_drive_t *drive = tape->drive;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001997 unsigned int minor = iminor(inode);
1998
1999 lock_kernel();
2000 tape = drive->driver_data;
Borislav Petkov8004a8c2008-02-06 02:57:51 +01002001
2002 debug_log(DBG_CHRDEV, "Enter %s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002003
Borislav Petkov54abf372008-02-06 02:57:52 +01002004 if (tape->chrdev_dir == IDETAPE_DIR_WRITE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002005 idetape_write_release(drive, minor);
Borislav Petkov54abf372008-02-06 02:57:52 +01002006 if (tape->chrdev_dir == IDETAPE_DIR_READ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002007 if (minor < 128)
Borislav Petkovec0fdb02008-04-27 15:38:34 +02002008 ide_tape_discard_merge_buffer(drive, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002009 }
Borislav Petkovf64eee72008-04-27 15:38:25 +02002010
Borislav Petkovf2e3ab52008-07-23 19:56:01 +02002011 if (minor < 128 && test_bit(IDE_AFLAG_MEDIUM_PRESENT, &drive->atapi_flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002012 (void) idetape_rewind_tape(drive);
Borislav Petkov54abf372008-02-06 02:57:52 +01002013 if (tape->chrdev_dir == IDETAPE_DIR_NONE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002014 if (tape->door_locked == DOOR_LOCKED) {
Bartlomiej Zolnierkiewicz05780422008-10-10 22:39:38 +02002015 if (!ide_set_media_lock(drive, tape->disk, 0))
Bartlomiej Zolnierkiewicz385a4b82008-10-10 22:39:37 +02002016 tape->door_locked = DOOR_UNLOCKED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002017 }
2018 }
Borislav Petkovf2e3ab52008-07-23 19:56:01 +02002019 clear_bit(IDE_AFLAG_BUSY, &drive->atapi_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002020 ide_tape_put(tape);
2021 unlock_kernel();
2022 return 0;
2023}
2024
Borislav Petkov6d29c8f2008-02-02 19:56:49 +01002025static void idetape_get_inquiry_results(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002026{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002027 idetape_tape_t *tape = drive->driver_data;
Borislav Petkovd236d742008-04-18 00:46:27 +02002028 struct ide_atapi_pc pc;
Borislav Petkov801bd322008-09-27 19:32:17 +02002029 char fw_rev[4], vendor_id[8], product_id[16];
Borislav Petkov6d29c8f2008-02-02 19:56:49 +01002030
Linus Torvalds1da177e2005-04-16 15:20:36 -07002031 idetape_create_inquiry_cmd(&pc);
Bartlomiej Zolnierkiewicz2ac07d92008-10-10 22:39:38 +02002032 if (ide_queue_pc_tail(drive, tape->disk, &pc)) {
Borislav Petkov6d29c8f2008-02-02 19:56:49 +01002033 printk(KERN_ERR "ide-tape: %s: can't get INQUIRY results\n",
2034 tape->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002035 return;
2036 }
Borislav Petkovd236d742008-04-18 00:46:27 +02002037 memcpy(vendor_id, &pc.buf[8], 8);
2038 memcpy(product_id, &pc.buf[16], 16);
2039 memcpy(fw_rev, &pc.buf[32], 4);
Borislav Petkov6d29c8f2008-02-02 19:56:49 +01002040
Borislav Petkov801bd322008-09-27 19:32:17 +02002041 ide_fixstring(vendor_id, 8, 0);
2042 ide_fixstring(product_id, 16, 0);
2043 ide_fixstring(fw_rev, 4, 0);
Borislav Petkov41f81d542008-02-06 02:57:52 +01002044
Borislav Petkov801bd322008-09-27 19:32:17 +02002045 printk(KERN_INFO "ide-tape: %s <-> %s: %.8s %.16s rev %.4s\n",
Borislav Petkov41f81d542008-02-06 02:57:52 +01002046 drive->name, tape->name, vendor_id, product_id, fw_rev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002047}
2048
2049/*
Borislav Petkovb6422012008-02-02 19:56:49 +01002050 * Ask the tape about its various parameters. In particular, we will adjust our
2051 * data transfer buffer size to the recommended value as returned by the tape.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002052 */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002053static void idetape_get_mode_sense_results(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002054{
2055 idetape_tape_t *tape = drive->driver_data;
Borislav Petkovd236d742008-04-18 00:46:27 +02002056 struct ide_atapi_pc pc;
Borislav Petkovb6422012008-02-02 19:56:49 +01002057 u8 *caps;
2058 u8 speed, max_speed;
Borislav Petkov47314fa2008-02-02 19:56:48 +01002059
Linus Torvalds1da177e2005-04-16 15:20:36 -07002060 idetape_create_mode_sense_cmd(&pc, IDETAPE_CAPABILITIES_PAGE);
Bartlomiej Zolnierkiewicz2ac07d92008-10-10 22:39:38 +02002061 if (ide_queue_pc_tail(drive, tape->disk, &pc)) {
Borislav Petkovb6422012008-02-02 19:56:49 +01002062 printk(KERN_ERR "ide-tape: Can't get tape parameters - assuming"
2063 " some default values\n");
Borislav Petkov54bb2072008-02-06 02:57:52 +01002064 tape->blk_size = 512;
Borislav Petkovb6422012008-02-02 19:56:49 +01002065 put_unaligned(52, (u16 *)&tape->caps[12]);
2066 put_unaligned(540, (u16 *)&tape->caps[14]);
2067 put_unaligned(6*52, (u16 *)&tape->caps[16]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002068 return;
2069 }
Borislav Petkovd236d742008-04-18 00:46:27 +02002070 caps = pc.buf + 4 + pc.buf[3];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002071
Borislav Petkovb6422012008-02-02 19:56:49 +01002072 /* convert to host order and save for later use */
Harvey Harrisoncd740ab2008-07-24 22:53:33 +02002073 speed = be16_to_cpup((__be16 *)&caps[14]);
2074 max_speed = be16_to_cpup((__be16 *)&caps[8]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002075
Harvey Harrisoncd740ab2008-07-24 22:53:33 +02002076 *(u16 *)&caps[8] = max_speed;
2077 *(u16 *)&caps[12] = be16_to_cpup((__be16 *)&caps[12]);
2078 *(u16 *)&caps[14] = speed;
2079 *(u16 *)&caps[16] = be16_to_cpup((__be16 *)&caps[16]);
Borislav Petkovb6422012008-02-02 19:56:49 +01002080
2081 if (!speed) {
2082 printk(KERN_INFO "ide-tape: %s: invalid tape speed "
2083 "(assuming 650KB/sec)\n", drive->name);
Harvey Harrisoncd740ab2008-07-24 22:53:33 +02002084 *(u16 *)&caps[14] = 650;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002085 }
Borislav Petkovb6422012008-02-02 19:56:49 +01002086 if (!max_speed) {
2087 printk(KERN_INFO "ide-tape: %s: invalid max_speed "
2088 "(assuming 650KB/sec)\n", drive->name);
Harvey Harrisoncd740ab2008-07-24 22:53:33 +02002089 *(u16 *)&caps[8] = 650;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002090 }
2091
Borislav Petkovb6422012008-02-02 19:56:49 +01002092 memcpy(&tape->caps, caps, 20);
Bartlomiej Zolnierkiewicz05780422008-10-10 22:39:38 +02002093
2094 /* device lacks locking support according to capabilities page */
2095 if ((caps[6] & 1) == 0)
Bartlomiej Zolnierkiewicz42619d32008-10-17 18:09:11 +02002096 drive->dev_flags &= ~IDE_DFLAG_DOORLOCKING;
Bartlomiej Zolnierkiewicz05780422008-10-10 22:39:38 +02002097
Borislav Petkovb6422012008-02-02 19:56:49 +01002098 if (caps[7] & 0x02)
Borislav Petkov54bb2072008-02-06 02:57:52 +01002099 tape->blk_size = 512;
Borislav Petkovb6422012008-02-02 19:56:49 +01002100 else if (caps[7] & 0x04)
Borislav Petkov54bb2072008-02-06 02:57:52 +01002101 tape->blk_size = 1024;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002102}
2103
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +02002104#ifdef CONFIG_IDE_PROC_FS
Bartlomiej Zolnierkiewicz8185d5a2008-10-10 22:39:28 +02002105#define ide_tape_devset_get(name, field) \
2106static int get_##name(ide_drive_t *drive) \
2107{ \
2108 idetape_tape_t *tape = drive->driver_data; \
2109 return tape->field; \
Linus Torvalds1da177e2005-04-16 15:20:36 -07002110}
Bartlomiej Zolnierkiewicz8185d5a2008-10-10 22:39:28 +02002111
2112#define ide_tape_devset_set(name, field) \
2113static int set_##name(ide_drive_t *drive, int arg) \
2114{ \
2115 idetape_tape_t *tape = drive->driver_data; \
2116 tape->field = arg; \
2117 return 0; \
2118}
2119
Elias Oltmanns92f1f8f2008-10-10 22:39:40 +02002120#define ide_tape_devset_rw_field(_name, _field) \
Bartlomiej Zolnierkiewicz8185d5a2008-10-10 22:39:28 +02002121ide_tape_devset_get(_name, _field) \
2122ide_tape_devset_set(_name, _field) \
Elias Oltmanns92f1f8f2008-10-10 22:39:40 +02002123IDE_DEVSET(_name, DS_SYNC, get_##_name, set_##_name)
Bartlomiej Zolnierkiewicz8185d5a2008-10-10 22:39:28 +02002124
Elias Oltmanns92f1f8f2008-10-10 22:39:40 +02002125#define ide_tape_devset_r_field(_name, _field) \
Bartlomiej Zolnierkiewicz8185d5a2008-10-10 22:39:28 +02002126ide_tape_devset_get(_name, _field) \
Elias Oltmanns92f1f8f2008-10-10 22:39:40 +02002127IDE_DEVSET(_name, 0, get_##_name, NULL)
Bartlomiej Zolnierkiewicz8185d5a2008-10-10 22:39:28 +02002128
2129static int mulf_tdsc(ide_drive_t *drive) { return 1000; }
2130static int divf_tdsc(ide_drive_t *drive) { return HZ; }
2131static int divf_buffer(ide_drive_t *drive) { return 2; }
2132static int divf_buffer_size(ide_drive_t *drive) { return 1024; }
2133
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +02002134ide_devset_rw_flag(dsc_overlap, IDE_DFLAG_DSC_OVERLAP);
Bartlomiej Zolnierkiewicz8185d5a2008-10-10 22:39:28 +02002135
Elias Oltmanns92f1f8f2008-10-10 22:39:40 +02002136ide_tape_devset_rw_field(debug_mask, debug_mask);
2137ide_tape_devset_rw_field(tdsc, best_dsc_rw_freq);
Bartlomiej Zolnierkiewicz8185d5a2008-10-10 22:39:28 +02002138
Elias Oltmanns92f1f8f2008-10-10 22:39:40 +02002139ide_tape_devset_r_field(avg_speed, avg_speed);
2140ide_tape_devset_r_field(speed, caps[14]);
2141ide_tape_devset_r_field(buffer, caps[16]);
2142ide_tape_devset_r_field(buffer_size, buffer_size);
Bartlomiej Zolnierkiewicz8185d5a2008-10-10 22:39:28 +02002143
Elias Oltmanns92f1f8f2008-10-10 22:39:40 +02002144static const struct ide_proc_devset idetape_settings[] = {
2145 __IDE_PROC_DEVSET(avg_speed, 0, 0xffff, NULL, NULL),
2146 __IDE_PROC_DEVSET(buffer, 0, 0xffff, NULL, divf_buffer),
2147 __IDE_PROC_DEVSET(buffer_size, 0, 0xffff, NULL, divf_buffer_size),
2148 __IDE_PROC_DEVSET(debug_mask, 0, 0xffff, NULL, NULL),
2149 __IDE_PROC_DEVSET(dsc_overlap, 0, 1, NULL, NULL),
2150 __IDE_PROC_DEVSET(speed, 0, 0xffff, NULL, NULL),
2151 __IDE_PROC_DEVSET(tdsc, IDETAPE_DSC_RW_MIN, IDETAPE_DSC_RW_MAX,
2152 mulf_tdsc, divf_tdsc),
Hannes Eder71bfc7a2009-03-05 16:10:56 +01002153 { NULL },
Bartlomiej Zolnierkiewicz8185d5a2008-10-10 22:39:28 +02002154};
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +02002155#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002156
2157/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +01002158 * The function below is called to:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002159 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +01002160 * 1. Initialize our various state variables.
2161 * 2. Ask the tape for its capabilities.
2162 * 3. Allocate a buffer which will be used for data transfer. The buffer size
2163 * is chosen based on the recommendation which we received in step 2.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002164 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +01002165 * Note that at this point ide.c already assigned us an irq, so that we can
2166 * queue requests here and wait for their completion.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002167 */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002168static void idetape_setup(ide_drive_t *drive, idetape_tape_t *tape, int minor)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002169{
Borislav Petkov83042b22008-04-27 15:38:27 +02002170 unsigned long t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002171 int speed;
Borislav Petkovf73850a2008-04-27 15:38:33 +02002172 int buffer_size;
Borislav Petkovb6422012008-02-02 19:56:49 +01002173 u16 *ctl = (u16 *)&tape->caps[12];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002174
Bartlomiej Zolnierkiewicz85e39032008-10-13 21:39:32 +02002175 drive->pc_callback = ide_tape_callback;
2176 drive->pc_update_buffers = idetape_update_buffers;
2177 drive->pc_io_buffers = ide_tape_io_buffers;
Borislav Petkov776bb022008-07-23 19:55:59 +02002178
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +02002179 drive->dev_flags |= IDE_DFLAG_DSC_OVERLAP;
2180
Bartlomiej Zolnierkiewicz4166c192008-02-01 23:09:30 +01002181 if (drive->hwif->host_flags & IDE_HFLAG_NO_DSC) {
2182 printk(KERN_INFO "ide-tape: %s: disabling DSC overlap\n",
2183 tape->name);
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +02002184 drive->dev_flags &= ~IDE_DFLAG_DSC_OVERLAP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002185 }
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +02002186
Linus Torvalds1da177e2005-04-16 15:20:36 -07002187 /* Seagate Travan drives do not support DSC overlap. */
Bartlomiej Zolnierkiewicz4dde4492008-10-10 22:39:19 +02002188 if (strstr((char *)&drive->id[ATA_ID_PROD], "Seagate STT3401"))
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +02002189 drive->dev_flags &= ~IDE_DFLAG_DSC_OVERLAP;
2190
Linus Torvalds1da177e2005-04-16 15:20:36 -07002191 tape->minor = minor;
2192 tape->name[0] = 'h';
2193 tape->name[1] = 't';
2194 tape->name[2] = '0' + minor;
Borislav Petkov54abf372008-02-06 02:57:52 +01002195 tape->chrdev_dir = IDETAPE_DIR_NONE;
Bartlomiej Zolnierkiewicz4dde4492008-10-10 22:39:19 +02002196
Linus Torvalds1da177e2005-04-16 15:20:36 -07002197 idetape_get_inquiry_results(drive);
2198 idetape_get_mode_sense_results(drive);
Borislav Petkov3cffb9c2008-02-02 19:56:50 +01002199 ide_tape_get_bsize_from_bdesc(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002200 tape->user_bs_factor = 1;
Borislav Petkovf73850a2008-04-27 15:38:33 +02002201 tape->buffer_size = *ctl * tape->blk_size;
2202 while (tape->buffer_size > 0xffff) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002203 printk(KERN_NOTICE "ide-tape: decreasing stage size\n");
Borislav Petkovb6422012008-02-02 19:56:49 +01002204 *ctl /= 2;
Borislav Petkovf73850a2008-04-27 15:38:33 +02002205 tape->buffer_size = *ctl * tape->blk_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002206 }
Borislav Petkovf73850a2008-04-27 15:38:33 +02002207 buffer_size = tape->buffer_size;
Borislav Petkova997a432008-04-27 15:38:33 +02002208 tape->pages_per_buffer = buffer_size / PAGE_SIZE;
Borislav Petkovf73850a2008-04-27 15:38:33 +02002209 if (buffer_size % PAGE_SIZE) {
Borislav Petkova997a432008-04-27 15:38:33 +02002210 tape->pages_per_buffer++;
Borislav Petkovf73850a2008-04-27 15:38:33 +02002211 tape->excess_bh_size = PAGE_SIZE - buffer_size % PAGE_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002212 }
2213
Borislav Petkov83042b22008-04-27 15:38:27 +02002214 /* select the "best" DSC read/write polling freq */
Borislav Petkovb6422012008-02-02 19:56:49 +01002215 speed = max(*(u16 *)&tape->caps[14], *(u16 *)&tape->caps[8]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002216
Borislav Petkovf73850a2008-04-27 15:38:33 +02002217 t = (IDETAPE_FIFO_THRESHOLD * tape->buffer_size * HZ) / (speed * 1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002218
2219 /*
Borislav Petkov3c98bf32008-02-06 02:57:53 +01002220 * Ensure that the number we got makes sense; limit it within
2221 * IDETAPE_DSC_RW_MIN and IDETAPE_DSC_RW_MAX.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002222 */
Harvey Harrisona792bd52008-07-15 21:21:41 +02002223 tape->best_dsc_rw_freq = clamp_t(unsigned long, t, IDETAPE_DSC_RW_MIN,
2224 IDETAPE_DSC_RW_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002225 printk(KERN_INFO "ide-tape: %s <-> %s: %dKBps, %d*%dkB buffer, "
Borislav Petkov83042b22008-04-27 15:38:27 +02002226 "%lums tDSC%s\n",
Borislav Petkovb6422012008-02-02 19:56:49 +01002227 drive->name, tape->name, *(u16 *)&tape->caps[14],
Borislav Petkovf73850a2008-04-27 15:38:33 +02002228 (*(u16 *)&tape->caps[16] * 512) / tape->buffer_size,
2229 tape->buffer_size / 1024,
Borislav Petkov54bb2072008-02-06 02:57:52 +01002230 tape->best_dsc_rw_freq * 1000 / HZ,
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +02002231 (drive->dev_flags & IDE_DFLAG_USING_DMA) ? ", DMA" : "");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002232
Bartlomiej Zolnierkiewicz1e874f42008-10-10 22:39:27 +02002233 ide_proc_register_driver(drive, tape->driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002234}
2235
Russell King4031bbe2006-01-06 11:41:00 +00002236static void ide_tape_remove(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002237{
2238 idetape_tape_t *tape = drive->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002239
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +02002240 ide_proc_unregister_driver(drive, tape->driver);
Bartlomiej Zolnierkiewicz8fed4362009-02-25 20:28:24 +01002241 device_del(&tape->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002242 ide_unregister_region(tape->disk);
2243
Bartlomiej Zolnierkiewicz8fed4362009-02-25 20:28:24 +01002244 mutex_lock(&idetape_ref_mutex);
2245 put_device(&tape->dev);
2246 mutex_unlock(&idetape_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002247}
2248
Bartlomiej Zolnierkiewicz8fed4362009-02-25 20:28:24 +01002249static void ide_tape_release(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002250{
Bartlomiej Zolnierkiewicz8fed4362009-02-25 20:28:24 +01002251 struct ide_tape_obj *tape = to_ide_drv(dev, ide_tape_obj);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002252 ide_drive_t *drive = tape->drive;
2253 struct gendisk *g = tape->disk;
2254
Borislav Petkov01a63aeb2008-04-27 15:38:34 +02002255 BUG_ON(tape->merge_bh_size);
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02002256
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +02002257 drive->dev_flags &= ~IDE_DFLAG_DSC_OVERLAP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002258 drive->driver_data = NULL;
Tony Jonesdbc12722007-09-25 02:03:03 +02002259 device_destroy(idetape_sysfs_class, MKDEV(IDETAPE_MAJOR, tape->minor));
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002260 device_destroy(idetape_sysfs_class,
2261 MKDEV(IDETAPE_MAJOR, tape->minor + 128));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002262 idetape_devs[tape->minor] = NULL;
2263 g->private_data = NULL;
2264 put_disk(g);
2265 kfree(tape);
2266}
2267
Bartlomiej Zolnierkiewiczecfd80e2007-05-10 00:01:09 +02002268#ifdef CONFIG_IDE_PROC_FS
Linus Torvalds1da177e2005-04-16 15:20:36 -07002269static int proc_idetape_read_name
2270 (char *page, char **start, off_t off, int count, int *eof, void *data)
2271{
2272 ide_drive_t *drive = (ide_drive_t *) data;
2273 idetape_tape_t *tape = drive->driver_data;
2274 char *out = page;
2275 int len;
2276
2277 len = sprintf(out, "%s\n", tape->name);
2278 PROC_IDE_READ_RETURN(page, start, off, count, eof, len);
2279}
2280
2281static ide_proc_entry_t idetape_proc[] = {
2282 { "capacity", S_IFREG|S_IRUGO, proc_ide_read_capacity, NULL },
2283 { "name", S_IFREG|S_IRUGO, proc_idetape_read_name, NULL },
2284 { NULL, 0, NULL, NULL }
2285};
Bartlomiej Zolnierkiewicz79cb3802008-10-17 18:09:13 +02002286
2287static ide_proc_entry_t *ide_tape_proc_entries(ide_drive_t *drive)
2288{
2289 return idetape_proc;
2290}
2291
2292static const struct ide_proc_devset *ide_tape_proc_devsets(ide_drive_t *drive)
2293{
2294 return idetape_settings;
2295}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002296#endif
2297
Russell King4031bbe2006-01-06 11:41:00 +00002298static int ide_tape_probe(ide_drive_t *);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002299
Bartlomiej Zolnierkiewicz7f3c8682009-01-06 17:20:53 +01002300static struct ide_driver idetape_driver = {
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02002301 .gen_driver = {
Laurent Riffard4ef3b8f2005-11-18 22:15:40 +01002302 .owner = THIS_MODULE,
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02002303 .name = "ide-tape",
2304 .bus = &ide_bus_type,
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02002305 },
Russell King4031bbe2006-01-06 11:41:00 +00002306 .probe = ide_tape_probe,
2307 .remove = ide_tape_remove,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002308 .version = IDETAPE_VERSION,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002309 .do_request = idetape_do_request,
2310 .end_request = idetape_end_request,
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +02002311#ifdef CONFIG_IDE_PROC_FS
Bartlomiej Zolnierkiewicz79cb3802008-10-17 18:09:13 +02002312 .proc_entries = ide_tape_proc_entries,
2313 .proc_devsets = ide_tape_proc_devsets,
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +02002314#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002315};
2316
Borislav Petkov3c98bf32008-02-06 02:57:53 +01002317/* Our character device supporting functions, passed to register_chrdev. */
Arjan van de Ven2b8693c2007-02-12 00:55:32 -08002318static const struct file_operations idetape_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002319 .owner = THIS_MODULE,
2320 .read = idetape_chrdev_read,
2321 .write = idetape_chrdev_write,
2322 .ioctl = idetape_chrdev_ioctl,
2323 .open = idetape_chrdev_open,
2324 .release = idetape_chrdev_release,
2325};
2326
Al Viroa4600f82008-03-02 10:27:58 -05002327static int idetape_open(struct block_device *bdev, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002328{
Al Viroa4600f82008-03-02 10:27:58 -05002329 struct ide_tape_obj *tape = ide_tape_get(bdev->bd_disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002330
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002331 if (!tape)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002332 return -ENXIO;
2333
Linus Torvalds1da177e2005-04-16 15:20:36 -07002334 return 0;
2335}
2336
Al Viroa4600f82008-03-02 10:27:58 -05002337static int idetape_release(struct gendisk *disk, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002338{
Borislav Petkov5aeddf92008-10-13 21:39:34 +02002339 struct ide_tape_obj *tape = ide_drv_g(disk, ide_tape_obj);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002340
2341 ide_tape_put(tape);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002342 return 0;
2343}
2344
Al Viroa4600f82008-03-02 10:27:58 -05002345static int idetape_ioctl(struct block_device *bdev, fmode_t mode,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002346 unsigned int cmd, unsigned long arg)
2347{
Borislav Petkov5aeddf92008-10-13 21:39:34 +02002348 struct ide_tape_obj *tape = ide_drv_g(bdev->bd_disk, ide_tape_obj);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002349 ide_drive_t *drive = tape->drive;
Al Viro1bddd9e2008-09-02 17:19:43 -04002350 int err = generic_ide_ioctl(drive, bdev, cmd, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002351 if (err == -EINVAL)
2352 err = idetape_blkdev_ioctl(drive, cmd, arg);
2353 return err;
2354}
2355
2356static struct block_device_operations idetape_block_ops = {
2357 .owner = THIS_MODULE,
Al Viroa4600f82008-03-02 10:27:58 -05002358 .open = idetape_open,
2359 .release = idetape_release,
2360 .locked_ioctl = idetape_ioctl,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002361};
2362
Russell King4031bbe2006-01-06 11:41:00 +00002363static int ide_tape_probe(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002364{
2365 idetape_tape_t *tape;
2366 struct gendisk *g;
2367 int minor;
2368
2369 if (!strstr("ide-tape", drive->driver_req))
2370 goto failed;
Bartlomiej Zolnierkiewicz2a924662008-10-10 22:39:24 +02002371
Linus Torvalds1da177e2005-04-16 15:20:36 -07002372 if (drive->media != ide_tape)
2373 goto failed;
Bartlomiej Zolnierkiewicz2a924662008-10-10 22:39:24 +02002374
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +02002375 if ((drive->dev_flags & IDE_DFLAG_ID_READ) &&
2376 ide_check_atapi_device(drive, DRV_NAME) == 0) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002377 printk(KERN_ERR "ide-tape: %s: not supported by this version of"
2378 " the driver\n", drive->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002379 goto failed;
2380 }
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002381 tape = kzalloc(sizeof(idetape_tape_t), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002382 if (tape == NULL) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002383 printk(KERN_ERR "ide-tape: %s: Can't allocate a tape struct\n",
2384 drive->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002385 goto failed;
2386 }
2387
2388 g = alloc_disk(1 << PARTN_BITS);
2389 if (!g)
2390 goto out_free_tape;
2391
2392 ide_init_disk(g, drive);
2393
Bartlomiej Zolnierkiewicz8fed4362009-02-25 20:28:24 +01002394 tape->dev.parent = &drive->gendev;
2395 tape->dev.release = ide_tape_release;
2396 dev_set_name(&tape->dev, dev_name(&drive->gendev));
2397
2398 if (device_register(&tape->dev))
2399 goto out_free_disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002400
2401 tape->drive = drive;
2402 tape->driver = &idetape_driver;
2403 tape->disk = g;
2404
2405 g->private_data = &tape->driver;
2406
2407 drive->driver_data = tape;
2408
Arjan van de Vencf8b8972006-03-23 03:00:45 -08002409 mutex_lock(&idetape_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002410 for (minor = 0; idetape_devs[minor]; minor++)
2411 ;
2412 idetape_devs[minor] = tape;
Arjan van de Vencf8b8972006-03-23 03:00:45 -08002413 mutex_unlock(&idetape_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002414
2415 idetape_setup(drive, tape, minor);
2416
Greg Kroah-Hartman3ee074b2008-07-21 20:03:34 -07002417 device_create(idetape_sysfs_class, &drive->gendev,
2418 MKDEV(IDETAPE_MAJOR, minor), NULL, "%s", tape->name);
2419 device_create(idetape_sysfs_class, &drive->gendev,
2420 MKDEV(IDETAPE_MAJOR, minor + 128), NULL,
2421 "n%s", tape->name);
Will Dysond5dee802005-09-16 02:55:07 -07002422
Linus Torvalds1da177e2005-04-16 15:20:36 -07002423 g->fops = &idetape_block_ops;
2424 ide_register_region(g);
2425
2426 return 0;
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02002427
Bartlomiej Zolnierkiewicz8fed4362009-02-25 20:28:24 +01002428out_free_disk:
2429 put_disk(g);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002430out_free_tape:
2431 kfree(tape);
2432failed:
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02002433 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002434}
2435
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002436static void __exit idetape_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002437{
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02002438 driver_unregister(&idetape_driver.gen_driver);
Will Dysond5dee802005-09-16 02:55:07 -07002439 class_destroy(idetape_sysfs_class);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002440 unregister_chrdev(IDETAPE_MAJOR, "ht");
2441}
2442
Bartlomiej Zolnierkiewicz17514e82005-11-19 22:24:35 +01002443static int __init idetape_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002444{
Will Dysond5dee802005-09-16 02:55:07 -07002445 int error = 1;
2446 idetape_sysfs_class = class_create(THIS_MODULE, "ide_tape");
2447 if (IS_ERR(idetape_sysfs_class)) {
2448 idetape_sysfs_class = NULL;
2449 printk(KERN_ERR "Unable to create sysfs class for ide tapes\n");
2450 error = -EBUSY;
2451 goto out;
2452 }
2453
Linus Torvalds1da177e2005-04-16 15:20:36 -07002454 if (register_chrdev(IDETAPE_MAJOR, "ht", &idetape_fops)) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002455 printk(KERN_ERR "ide-tape: Failed to register chrdev"
2456 " interface\n");
Will Dysond5dee802005-09-16 02:55:07 -07002457 error = -EBUSY;
2458 goto out_free_class;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002459 }
Will Dysond5dee802005-09-16 02:55:07 -07002460
2461 error = driver_register(&idetape_driver.gen_driver);
2462 if (error)
2463 goto out_free_driver;
2464
2465 return 0;
2466
2467out_free_driver:
2468 driver_unregister(&idetape_driver.gen_driver);
2469out_free_class:
2470 class_destroy(idetape_sysfs_class);
2471out:
2472 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002473}
2474
Kay Sievers263756e2005-12-12 18:03:44 +01002475MODULE_ALIAS("ide:*m-tape*");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002476module_init(idetape_init);
2477module_exit(idetape_exit);
2478MODULE_ALIAS_CHARDEV_MAJOR(IDETAPE_MAJOR);
Borislav Petkov9c145762008-02-06 02:57:54 +01002479MODULE_DESCRIPTION("ATAPI Streaming TAPE Driver");
2480MODULE_LICENSE("GPL");