blob: 5c879fbcca87ef15ba97bbc9f9a254d38f9951ce [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/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070084 * Some drives (for example, Seagate STT3401A Travan) require a very long
85 * timeout, because they don't return an interrupt or clear their busy bit
86 * until after the command completes (even retension commands).
87 */
88#define IDETAPE_WAIT_CMD (900*HZ)
89
90/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +010091 * The following parameter is used to select the point in the internal tape fifo
92 * in which we will start to refill the buffer. Decreasing the following
93 * parameter will improve the system's latency and interactive response, while
94 * using a high value might improve system throughput.
Linus Torvalds1da177e2005-04-16 15:20:36 -070095 */
Borislav Petkov3c98bf32008-02-06 02:57:53 +010096#define IDETAPE_FIFO_THRESHOLD 2
Linus Torvalds1da177e2005-04-16 15:20:36 -070097
98/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +010099 * DSC polling parameters.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100101 * Polling for DSC (a single bit in the status register) is a very important
102 * function in ide-tape. There are two cases in which we poll for DSC:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100104 * 1. Before a read/write packet command, to ensure that we can transfer data
105 * from/to the tape's data buffers, without causing an actual media access.
106 * In case the tape is not ready yet, we take out our request from the device
107 * request queue, so that ide.c could service requests from the other device
108 * on the same interface in the meantime.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100110 * 2. After the successful initialization of a "media access packet command",
111 * which is a command that can take a long time to complete (the interval can
112 * range from several seconds to even an hour). Again, we postpone our request
113 * in the middle to free the bus for the other device. The polling frequency
114 * here should be lower than the read/write frequency since those media access
115 * commands are slow. We start from a "fast" frequency - IDETAPE_DSC_MA_FAST
116 * (1 second), and if we don't receive DSC after IDETAPE_DSC_MA_THRESHOLD
117 * (5 min), we switch it to a lower frequency - IDETAPE_DSC_MA_SLOW (1 min).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100119 * We also set a timeout for the timer, in case something goes wrong. The
120 * timeout should be longer then the maximum execution time of a tape operation.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 */
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100122
123/* DSC timings. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124#define IDETAPE_DSC_RW_MIN 5*HZ/100 /* 50 msec */
125#define IDETAPE_DSC_RW_MAX 40*HZ/100 /* 400 msec */
126#define IDETAPE_DSC_RW_TIMEOUT 2*60*HZ /* 2 minutes */
127#define IDETAPE_DSC_MA_FAST 2*HZ /* 2 seconds */
128#define IDETAPE_DSC_MA_THRESHOLD 5*60*HZ /* 5 minutes */
129#define IDETAPE_DSC_MA_SLOW 30*HZ /* 30 seconds */
130#define IDETAPE_DSC_MA_TIMEOUT 2*60*60*HZ /* 2 hours */
131
132/*************************** End of tunable parameters ***********************/
133
Borislav Petkov54abf372008-02-06 02:57:52 +0100134/* tape directions */
135enum {
136 IDETAPE_DIR_NONE = (1 << 0),
137 IDETAPE_DIR_READ = (1 << 1),
138 IDETAPE_DIR_WRITE = (1 << 2),
139};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140
141struct idetape_bh {
Stephen Rothwellab057962007-08-01 23:46:44 +0200142 u32 b_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143 atomic_t b_count;
144 struct idetape_bh *b_reqnext;
145 char *b_data;
146};
147
Borislav Petkov03056b92008-04-18 00:46:26 +0200148/* Tape door status */
149#define DOOR_UNLOCKED 0
150#define DOOR_LOCKED 1
151#define DOOR_EXPLICITLY_LOCKED 2
152
153/* Some defines for the SPACE command */
154#define IDETAPE_SPACE_OVER_FILEMARK 1
155#define IDETAPE_SPACE_TO_EOD 3
156
157/* Some defines for the LOAD UNLOAD command */
158#define IDETAPE_LU_LOAD_MASK 1
159#define IDETAPE_LU_RETENSION_MASK 2
160#define IDETAPE_LU_EOT_MASK 4
161
162/*
163 * Special requests for our block device strategy routine.
164 *
165 * In order to service a character device command, we add special requests to
166 * the tail of our block device request queue and wait for their completion.
167 */
168
169enum {
170 REQ_IDETAPE_PC1 = (1 << 0), /* packet command (first stage) */
171 REQ_IDETAPE_PC2 = (1 << 1), /* packet command (second stage) */
172 REQ_IDETAPE_READ = (1 << 2),
173 REQ_IDETAPE_WRITE = (1 << 3),
174};
175
176/* Error codes returned in rq->errors to the higher part of the driver. */
177#define IDETAPE_ERROR_GENERAL 101
178#define IDETAPE_ERROR_FILEMARK 102
179#define IDETAPE_ERROR_EOD 103
180
181/* Structures related to the SELECT SENSE / MODE SENSE packet commands. */
182#define IDETAPE_BLOCK_DESCRIPTOR 0
183#define IDETAPE_CAPABILITIES_PAGE 0x2a
184
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100186 * Most of our global data which we need to save even as we leave the driver due
187 * to an interrupt or a timer event is stored in the struct defined below.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 */
189typedef struct ide_tape_obj {
190 ide_drive_t *drive;
191 ide_driver_t *driver;
192 struct gendisk *disk;
193 struct kref kref;
194
195 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 * pc points to the current processed packet command.
197 *
198 * failed_pc points to the last failed packet command, or contains
199 * NULL if we do not need to retry any packet command. This is
200 * required since an additional packet command is needed before the
201 * retry, to get detailed information on what went wrong.
202 */
203 /* Current packet command */
Borislav Petkovd236d742008-04-18 00:46:27 +0200204 struct ide_atapi_pc *pc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 /* Last failed packet command */
Borislav Petkovd236d742008-04-18 00:46:27 +0200206 struct ide_atapi_pc *failed_pc;
Bartlomiej Zolnierkiewicz2e8a6f82008-10-10 22:39:36 +0200207 /* used by REQ_IDETAPE_{READ,WRITE} requests */
208 struct ide_atapi_pc queued_pc;
Bartlomiej Zolnierkiewicz394a4c22008-10-10 22:39:35 +0200209
Bartlomiej Zolnierkiewicz2e8a6f82008-10-10 22:39:36 +0200210 struct ide_atapi_pc request_sense_pc;
Bartlomiej Zolnierkiewicz394a4c22008-10-10 22:39:35 +0200211 struct request request_sense_rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212
213 /*
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100214 * DSC polling variables.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100216 * While polling for DSC we use postponed_rq to postpone the current
217 * request so that ide.c will be able to service pending requests on the
218 * other device. Note that at most we will have only one DSC (usually
Borislav Petkov5bd50dc2008-04-27 15:38:28 +0200219 * data transfer) request in the device request queue.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 */
221 struct request *postponed_rq;
222 /* The time in which we started polling for DSC */
223 unsigned long dsc_polling_start;
224 /* Timer used to poll for dsc */
225 struct timer_list dsc_timer;
226 /* Read/Write dsc polling frequency */
Borislav Petkov54bb2072008-02-06 02:57:52 +0100227 unsigned long best_dsc_rw_freq;
228 unsigned long dsc_poll_freq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 unsigned long dsc_timeout;
230
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100231 /* Read position information */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 u8 partition;
233 /* Current block */
Borislav Petkov54bb2072008-02-06 02:57:52 +0100234 unsigned int first_frame;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100236 /* Last error information */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 u8 sense_key, asc, ascq;
238
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100239 /* Character device operation */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 unsigned int minor;
241 /* device name */
242 char name[4];
243 /* Current character device data transfer direction */
Borislav Petkov54abf372008-02-06 02:57:52 +0100244 u8 chrdev_dir;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245
Borislav Petkov54bb2072008-02-06 02:57:52 +0100246 /* tape block size, usually 512 or 1024 bytes */
247 unsigned short blk_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 int user_bs_factor;
Borislav Petkovb6422012008-02-02 19:56:49 +0100249
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 /* Copy of the tape's Capabilities and Mechanical Page */
Borislav Petkovb6422012008-02-02 19:56:49 +0100251 u8 caps[20];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252
253 /*
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100254 * Active data transfer request parameters.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100256 * At most, there is only one ide-tape originated data transfer request
257 * in the device request queue. This allows ide.c to easily service
258 * requests from the other device when we postpone our active request.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 */
Borislav Petkov83042b22008-04-27 15:38:27 +0200260
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100261 /* Data buffer size chosen based on the tape's recommendation */
Borislav Petkovf73850a2008-04-27 15:38:33 +0200262 int buffer_size;
Borislav Petkov077e3bd2008-04-27 15:38:34 +0200263 /* merge buffer */
264 struct idetape_bh *merge_bh;
Borislav Petkov01a63aeb2008-04-27 15:38:34 +0200265 /* size of the merge buffer */
266 int merge_bh_size;
Borislav Petkov077e3bd2008-04-27 15:38:34 +0200267 /* pointer to current buffer head within the merge buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 struct idetape_bh *bh;
269 char *b_data;
270 int b_count;
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100271
Borislav Petkova997a432008-04-27 15:38:33 +0200272 int pages_per_buffer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 /* Wasted space in each stage */
274 int excess_bh_size;
275
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 /* protects the ide-tape queue */
Borislav Petkov54bb2072008-02-06 02:57:52 +0100277 spinlock_t lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100279 /* Measures average tape speed */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 unsigned long avg_time;
281 int avg_size;
282 int avg_speed;
283
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 /* the door is currently locked */
285 int door_locked;
286 /* the tape hardware is write protected */
287 char drv_write_prot;
288 /* the tape is write protected (hardware or opened as read-only) */
289 char write_prot;
290
Borislav Petkov8004a8c2008-02-06 02:57:51 +0100291 u32 debug_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292} idetape_tape_t;
293
Arjan van de Vencf8b8972006-03-23 03:00:45 -0800294static DEFINE_MUTEX(idetape_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295
Will Dysond5dee802005-09-16 02:55:07 -0700296static struct class *idetape_sysfs_class;
297
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298#define to_ide_tape(obj) container_of(obj, struct ide_tape_obj, kref)
299
300#define ide_tape_g(disk) \
301 container_of((disk)->private_data, struct ide_tape_obj, driver)
302
Bartlomiej Zolnierkiewicz08da5912008-07-24 22:53:15 +0200303static void ide_tape_release(struct kref *);
304
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305static struct ide_tape_obj *ide_tape_get(struct gendisk *disk)
306{
307 struct ide_tape_obj *tape = NULL;
308
Arjan van de Vencf8b8972006-03-23 03:00:45 -0800309 mutex_lock(&idetape_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 tape = ide_tape_g(disk);
Bartlomiej Zolnierkiewicz08da5912008-07-24 22:53:15 +0200311 if (tape) {
Bartlomiej Zolnierkiewiczd3e33ff2008-08-05 18:16:59 +0200312 if (ide_device_get(tape->drive))
Bartlomiej Zolnierkiewicz08da5912008-07-24 22:53:15 +0200313 tape = NULL;
Bartlomiej Zolnierkiewiczd3e33ff2008-08-05 18:16:59 +0200314 else
315 kref_get(&tape->kref);
Bartlomiej Zolnierkiewicz08da5912008-07-24 22:53:15 +0200316 }
Arjan van de Vencf8b8972006-03-23 03:00:45 -0800317 mutex_unlock(&idetape_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 return tape;
319}
320
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321static void ide_tape_put(struct ide_tape_obj *tape)
322{
Bartlomiej Zolnierkiewiczd3e33ff2008-08-05 18:16:59 +0200323 ide_drive_t *drive = tape->drive;
324
Arjan van de Vencf8b8972006-03-23 03:00:45 -0800325 mutex_lock(&idetape_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 kref_put(&tape->kref, ide_tape_release);
Bartlomiej Zolnierkiewiczd3e33ff2008-08-05 18:16:59 +0200327 ide_device_put(drive);
Arjan van de Vencf8b8972006-03-23 03:00:45 -0800328 mutex_unlock(&idetape_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329}
330
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100332 * The variables below are used for the character device interface. Additional
333 * state variables are defined in our ide_drive_t structure.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100335static struct ide_tape_obj *idetape_devs[MAX_HWIFS * MAX_DRIVES];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336
337#define ide_tape_f(file) ((file)->private_data)
338
339static struct ide_tape_obj *ide_tape_chrdev_get(unsigned int i)
340{
341 struct ide_tape_obj *tape = NULL;
342
Arjan van de Vencf8b8972006-03-23 03:00:45 -0800343 mutex_lock(&idetape_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 tape = idetape_devs[i];
345 if (tape)
346 kref_get(&tape->kref);
Arjan van de Vencf8b8972006-03-23 03:00:45 -0800347 mutex_unlock(&idetape_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 return tape;
349}
350
Borislav Petkovd236d742008-04-18 00:46:27 +0200351static void idetape_input_buffers(ide_drive_t *drive, struct ide_atapi_pc *pc,
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100352 unsigned int bcount)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353{
354 struct idetape_bh *bh = pc->bh;
355 int count;
356
357 while (bcount) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 if (bh == NULL) {
359 printk(KERN_ERR "ide-tape: bh == NULL in "
360 "idetape_input_buffers\n");
Bartlomiej Zolnierkiewicz9f87abe2008-04-28 23:44:41 +0200361 ide_pad_transfer(drive, 0, bcount);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 return;
363 }
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100364 count = min(
365 (unsigned int)(bh->b_size - atomic_read(&bh->b_count)),
366 bcount);
Bartlomiej Zolnierkiewicz374e0422008-07-23 19:55:56 +0200367 drive->hwif->tp_ops->input_data(drive, NULL, bh->b_data +
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100368 atomic_read(&bh->b_count), count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 bcount -= count;
370 atomic_add(count, &bh->b_count);
371 if (atomic_read(&bh->b_count) == bh->b_size) {
372 bh = bh->b_reqnext;
373 if (bh)
374 atomic_set(&bh->b_count, 0);
375 }
376 }
377 pc->bh = bh;
378}
379
Borislav Petkovd236d742008-04-18 00:46:27 +0200380static void idetape_output_buffers(ide_drive_t *drive, struct ide_atapi_pc *pc,
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100381 unsigned int bcount)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382{
383 struct idetape_bh *bh = pc->bh;
384 int count;
385
386 while (bcount) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 if (bh == NULL) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100388 printk(KERN_ERR "ide-tape: bh == NULL in %s\n",
389 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 return;
391 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 count = min((unsigned int)pc->b_count, (unsigned int)bcount);
Bartlomiej Zolnierkiewicz374e0422008-07-23 19:55:56 +0200393 drive->hwif->tp_ops->output_data(drive, NULL, pc->b_data, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 bcount -= count;
395 pc->b_data += count;
396 pc->b_count -= count;
397 if (!pc->b_count) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100398 bh = bh->b_reqnext;
399 pc->bh = bh;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 if (bh) {
401 pc->b_data = bh->b_data;
402 pc->b_count = atomic_read(&bh->b_count);
403 }
404 }
405 }
406}
407
Bartlomiej Zolnierkiewicz646c0cb2008-07-15 21:22:03 +0200408static void idetape_update_buffers(ide_drive_t *drive, struct ide_atapi_pc *pc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409{
410 struct idetape_bh *bh = pc->bh;
411 int count;
Borislav Petkovd236d742008-04-18 00:46:27 +0200412 unsigned int bcount = pc->xferred;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413
Borislav Petkov346331f2008-04-18 00:46:26 +0200414 if (pc->flags & PC_FLAG_WRITING)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 return;
416 while (bcount) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 if (bh == NULL) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100418 printk(KERN_ERR "ide-tape: bh == NULL in %s\n",
419 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 return;
421 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 count = min((unsigned int)bh->b_size, (unsigned int)bcount);
423 atomic_set(&bh->b_count, count);
424 if (atomic_read(&bh->b_count) == bh->b_size)
425 bh = bh->b_reqnext;
426 bcount -= count;
427 }
428 pc->bh = bh;
429}
430
431/*
Borislav Petkov1b5db432008-02-02 19:56:48 +0100432 * called on each failed packet command retry to analyze the request sense. We
433 * currently do not utilize this information.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 */
Borislav Petkov1b5db432008-02-02 19:56:48 +0100435static void idetape_analyze_error(ide_drive_t *drive, u8 *sense)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436{
437 idetape_tape_t *tape = drive->driver_data;
Borislav Petkovd236d742008-04-18 00:46:27 +0200438 struct ide_atapi_pc *pc = tape->failed_pc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439
Borislav Petkov1b5db432008-02-02 19:56:48 +0100440 tape->sense_key = sense[2] & 0xF;
441 tape->asc = sense[12];
442 tape->ascq = sense[13];
Borislav Petkov8004a8c2008-02-06 02:57:51 +0100443
444 debug_log(DBG_ERR, "pc = %x, sense key = %x, asc = %x, ascq = %x\n",
445 pc->c[0], tape->sense_key, tape->asc, tape->ascq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446
Borislav Petkovd236d742008-04-18 00:46:27 +0200447 /* Correct pc->xferred by asking the tape. */
Borislav Petkov346331f2008-04-18 00:46:26 +0200448 if (pc->flags & PC_FLAG_DMA_ERROR) {
Borislav Petkovd236d742008-04-18 00:46:27 +0200449 pc->xferred = pc->req_xfer -
Borislav Petkov54bb2072008-02-06 02:57:52 +0100450 tape->blk_size *
Harvey Harrison5d0cc8a2008-07-15 21:21:41 +0200451 get_unaligned_be32(&sense[3]);
Bartlomiej Zolnierkiewicz646c0cb2008-07-15 21:22:03 +0200452 idetape_update_buffers(drive, pc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 }
454
455 /*
456 * If error was the result of a zero-length read or write command,
457 * with sense key=5, asc=0x22, ascq=0, let it slide. Some drives
458 * (i.e. Seagate STT3401A Travan) don't support 0-length read/writes.
459 */
Borislav Petkov90699ce2008-02-02 19:56:50 +0100460 if ((pc->c[0] == READ_6 || pc->c[0] == WRITE_6)
Borislav Petkov1b5db432008-02-02 19:56:48 +0100461 /* length == 0 */
462 && pc->c[4] == 0 && pc->c[3] == 0 && pc->c[2] == 0) {
463 if (tape->sense_key == 5) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 /* don't report an error, everything's ok */
465 pc->error = 0;
466 /* don't retry read/write */
Borislav Petkov346331f2008-04-18 00:46:26 +0200467 pc->flags |= PC_FLAG_ABORT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 }
469 }
Borislav Petkov90699ce2008-02-02 19:56:50 +0100470 if (pc->c[0] == READ_6 && (sense[2] & 0x80)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 pc->error = IDETAPE_ERROR_FILEMARK;
Borislav Petkov346331f2008-04-18 00:46:26 +0200472 pc->flags |= PC_FLAG_ABORT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 }
Borislav Petkov90699ce2008-02-02 19:56:50 +0100474 if (pc->c[0] == WRITE_6) {
Borislav Petkov1b5db432008-02-02 19:56:48 +0100475 if ((sense[2] & 0x40) || (tape->sense_key == 0xd
476 && tape->asc == 0x0 && tape->ascq == 0x2)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477 pc->error = IDETAPE_ERROR_EOD;
Borislav Petkov346331f2008-04-18 00:46:26 +0200478 pc->flags |= PC_FLAG_ABORT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 }
480 }
Borislav Petkov90699ce2008-02-02 19:56:50 +0100481 if (pc->c[0] == READ_6 || pc->c[0] == WRITE_6) {
Borislav Petkov1b5db432008-02-02 19:56:48 +0100482 if (tape->sense_key == 8) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 pc->error = IDETAPE_ERROR_EOD;
Borislav Petkov346331f2008-04-18 00:46:26 +0200484 pc->flags |= PC_FLAG_ABORT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 }
Borislav Petkov346331f2008-04-18 00:46:26 +0200486 if (!(pc->flags & PC_FLAG_ABORT) &&
Borislav Petkovd236d742008-04-18 00:46:27 +0200487 pc->xferred)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 pc->retries = IDETAPE_MAX_PC_RETRIES + 1;
489 }
490}
491
Borislav Petkovd01dbc32008-04-27 15:38:33 +0200492/* Free data buffers completely. */
Borislav Petkov077e3bd2008-04-27 15:38:34 +0200493static void ide_tape_kfree_buffer(idetape_tape_t *tape)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494{
Borislav Petkov077e3bd2008-04-27 15:38:34 +0200495 struct idetape_bh *prev_bh, *bh = tape->merge_bh;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496
Borislav Petkovd01dbc32008-04-27 15:38:33 +0200497 while (bh) {
498 u32 size = bh->b_size;
499
500 while (size) {
501 unsigned int order = fls(size >> PAGE_SHIFT)-1;
502
503 if (bh->b_data)
504 free_pages((unsigned long)bh->b_data, order);
505
506 size &= (order-1);
507 bh->b_data += (1 << order) * PAGE_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 }
509 prev_bh = bh;
510 bh = bh->b_reqnext;
511 kfree(prev_bh);
512 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513}
514
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515static int idetape_end_request(ide_drive_t *drive, int uptodate, int nr_sects)
516{
517 struct request *rq = HWGROUP(drive)->rq;
518 idetape_tape_t *tape = drive->driver_data;
519 unsigned long flags;
520 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521
Borislav Petkov8004a8c2008-02-06 02:57:51 +0100522 debug_log(DBG_PROCS, "Enter %s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523
524 switch (uptodate) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100525 case 0: error = IDETAPE_ERROR_GENERAL; break;
526 case 1: error = 0; break;
527 default: error = uptodate;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 }
529 rq->errors = error;
530 if (error)
531 tape->failed_pc = NULL;
532
Bartlomiej Zolnierkiewicz36872212008-01-26 20:13:10 +0100533 if (!blk_special_request(rq)) {
534 ide_end_request(drive, uptodate, nr_sects);
535 return 0;
536 }
537
Borislav Petkov54bb2072008-02-06 02:57:52 +0100538 spin_lock_irqsave(&tape->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 ide_end_drive_cmd(drive, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541
Borislav Petkov54bb2072008-02-06 02:57:52 +0100542 spin_unlock_irqrestore(&tape->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 return 0;
544}
545
Bartlomiej Zolnierkiewicz92f5daf2008-07-15 21:21:55 +0200546static void ide_tape_callback(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547{
548 idetape_tape_t *tape = drive->driver_data;
Bartlomiej Zolnierkiewicz5985e6a2008-07-15 21:21:55 +0200549 struct ide_atapi_pc *pc = tape->pc;
550 int uptodate = pc->error ? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551
Borislav Petkov8004a8c2008-02-06 02:57:51 +0100552 debug_log(DBG_PROCS, "Enter %s\n", __func__);
553
Bartlomiej Zolnierkiewiczdd2e9a02008-07-15 21:22:01 +0200554 if (tape->failed_pc == pc)
555 tape->failed_pc = NULL;
556
Bartlomiej Zolnierkiewicz5985e6a2008-07-15 21:21:55 +0200557 if (pc->c[0] == REQUEST_SENSE) {
558 if (uptodate)
559 idetape_analyze_error(drive, pc->buf);
560 else
561 printk(KERN_ERR "ide-tape: Error in REQUEST SENSE "
562 "itself - Aborting request!\n");
563 } else if (pc->c[0] == READ_6 || pc->c[0] == WRITE_6) {
564 struct request *rq = drive->hwif->hwgroup->rq;
565 int blocks = pc->xferred / tape->blk_size;
566
567 tape->avg_size += blocks * tape->blk_size;
568
569 if (time_after_eq(jiffies, tape->avg_time + HZ)) {
570 tape->avg_speed = tape->avg_size * HZ /
571 (jiffies - tape->avg_time) / 1024;
572 tape->avg_size = 0;
573 tape->avg_time = jiffies;
574 }
575
576 tape->first_frame += blocks;
577 rq->current_nr_sectors -= blocks;
578
579 if (pc->error)
580 uptodate = pc->error;
581 } else if (pc->c[0] == READ_POSITION && uptodate) {
582 u8 *readpos = tape->pc->buf;
583
584 debug_log(DBG_SENSE, "BOP - %s\n",
585 (readpos[0] & 0x80) ? "Yes" : "No");
586 debug_log(DBG_SENSE, "EOP - %s\n",
587 (readpos[0] & 0x40) ? "Yes" : "No");
588
589 if (readpos[0] & 0x4) {
590 printk(KERN_INFO "ide-tape: Block location is unknown"
591 "to the tape\n");
Borislav Petkovf2e3ab52008-07-23 19:56:01 +0200592 clear_bit(IDE_AFLAG_ADDRESS_VALID, &drive->atapi_flags);
Bartlomiej Zolnierkiewicz5985e6a2008-07-15 21:21:55 +0200593 uptodate = 0;
594 } else {
595 debug_log(DBG_SENSE, "Block Location - %u\n",
Harvey Harrisoncd740ab2008-07-24 22:53:33 +0200596 be32_to_cpup((__be32 *)&readpos[4]));
Bartlomiej Zolnierkiewicz5985e6a2008-07-15 21:21:55 +0200597
598 tape->partition = readpos[1];
Harvey Harrisoncd740ab2008-07-24 22:53:33 +0200599 tape->first_frame = be32_to_cpup((__be32 *)&readpos[4]);
Borislav Petkovf2e3ab52008-07-23 19:56:01 +0200600 set_bit(IDE_AFLAG_ADDRESS_VALID, &drive->atapi_flags);
Bartlomiej Zolnierkiewicz5985e6a2008-07-15 21:21:55 +0200601 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 }
Bartlomiej Zolnierkiewicz5985e6a2008-07-15 21:21:55 +0200603
604 idetape_end_request(drive, uptodate, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605}
606
Borislav Petkovd236d742008-04-18 00:46:27 +0200607static void idetape_create_request_sense_cmd(struct ide_atapi_pc *pc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608{
Bartlomiej Zolnierkiewicz7bf74202008-10-10 22:39:37 +0200609 ide_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +0100610 pc->c[0] = REQUEST_SENSE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611 pc->c[4] = 20;
Borislav Petkovd236d742008-04-18 00:46:27 +0200612 pc->req_xfer = 20;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613}
614
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100616 * Generate a new packet command request in front of the request queue, before
617 * the current request, so that it will be processed immediately, on the next
Bartlomiej Zolnierkiewicz394a4c22008-10-10 22:39:35 +0200618 * pass through the driver.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 */
Borislav Petkovd236d742008-04-18 00:46:27 +0200620static void idetape_queue_pc_head(ide_drive_t *drive, struct ide_atapi_pc *pc,
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100621 struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622{
623 struct ide_tape_obj *tape = drive->driver_data;
624
Bartlomiej Zolnierkiewiczf025ffd2008-10-10 22:39:34 +0200625 blk_rq_init(NULL, rq);
626 rq->cmd_type = REQ_TYPE_SPECIAL;
Bartlomiej Zolnierkiewicze8a96aa2008-07-15 21:21:41 +0200627 rq->cmd_flags |= REQ_PREEMPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628 rq->buffer = (char *) pc;
629 rq->rq_disk = tape->disk;
Borislav Petkov0014c752008-07-23 19:56:00 +0200630 memcpy(rq->cmd, pc->c, 12);
Bartlomiej Zolnierkiewiczf025ffd2008-10-10 22:39:34 +0200631 rq->cmd[13] = REQ_IDETAPE_PC1;
FUJITA Tomonori63f5abb2008-07-15 21:21:51 +0200632 ide_do_drive_cmd(drive, rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633}
634
635/*
636 * idetape_retry_pc is called when an error was detected during the
637 * last packet command. We queue a request sense packet command in
638 * the head of the request list.
639 */
Bartlomiej Zolnierkiewicz258ec412008-07-15 21:21:55 +0200640static void idetape_retry_pc(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641{
Bartlomiej Zolnierkiewicz394a4c22008-10-10 22:39:35 +0200642 struct ide_tape_obj *tape = drive->driver_data;
643 struct request *rq = &tape->request_sense_rq;
Bartlomiej Zolnierkiewicz2e8a6f82008-10-10 22:39:36 +0200644 struct ide_atapi_pc *pc = &tape->request_sense_pc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645
Bartlomiej Zolnierkiewicz64a57fe2008-02-06 02:57:51 +0100646 (void)ide_read_error(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647 idetape_create_request_sense_cmd(pc);
Borislav Petkovf2e3ab52008-07-23 19:56:01 +0200648 set_bit(IDE_AFLAG_IGNORE_DSC, &drive->atapi_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 idetape_queue_pc_head(drive, pc, rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650}
651
652/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100653 * Postpone the current request so that ide.c will be able to service requests
654 * from another device on the same hwgroup while we are polling for DSC.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655 */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100656static void idetape_postpone_request(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657{
658 idetape_tape_t *tape = drive->driver_data;
659
Borislav Petkov8004a8c2008-02-06 02:57:51 +0100660 debug_log(DBG_PROCS, "Enter %s\n", __func__);
661
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662 tape->postponed_rq = HWGROUP(drive)->rq;
Borislav Petkov54bb2072008-02-06 02:57:52 +0100663 ide_stall_queue(drive, tape->dsc_poll_freq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664}
665
Bartlomiej Zolnierkiewicz74e63e742008-07-15 21:22:01 +0200666static void ide_tape_handle_dsc(ide_drive_t *drive)
667{
668 idetape_tape_t *tape = drive->driver_data;
669
670 /* Media access command */
671 tape->dsc_polling_start = jiffies;
672 tape->dsc_poll_freq = IDETAPE_DSC_MA_FAST;
673 tape->dsc_timeout = jiffies + IDETAPE_DSC_MA_TIMEOUT;
674 /* Allow ide.c to handle other requests */
675 idetape_postpone_request(drive);
676}
677
Bartlomiej Zolnierkiewiczacaa0f52008-10-10 22:39:36 +0200678static int ide_tape_io_buffers(ide_drive_t *drive, struct ide_atapi_pc *pc,
Bartlomiej Zolnierkiewicz08424ac2008-07-15 21:22:01 +0200679 unsigned int bcount, int write)
680{
681 if (write)
682 idetape_output_buffers(drive, pc, bcount);
683 else
684 idetape_input_buffers(drive, pc, bcount);
Bartlomiej Zolnierkiewiczacaa0f52008-10-10 22:39:36 +0200685
686 return bcount;
Bartlomiej Zolnierkiewicz08424ac2008-07-15 21:22:01 +0200687}
Borislav Petkova1efc852008-02-06 02:57:52 +0100688
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689/*
Borislav Petkova1efc852008-02-06 02:57:52 +0100690 * This is the usual interrupt handler which will be called during a packet
691 * command. We will transfer some of the data (as requested by the drive) and
692 * will re-point interrupt handler to us. When data transfer is finished, we
693 * will act according to the algorithm described before
Borislav Petkov8d06bfa2008-02-06 02:57:53 +0100694 * idetape_issue_pc.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 */
Borislav Petkova1efc852008-02-06 02:57:52 +0100696static ide_startstop_t idetape_pc_intr(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 idetape_tape_t *tape = drive->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699
Bartlomiej Zolnierkiewicz646c0cb2008-07-15 21:22:03 +0200700 return ide_pc_intr(drive, tape->pc, idetape_pc_intr, IDETAPE_WAIT_CMD,
701 NULL, idetape_update_buffers, idetape_retry_pc,
702 ide_tape_handle_dsc, ide_tape_io_buffers);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703}
704
705/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100706 * Packet Command Interface
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100708 * The current Packet Command is available in tape->pc, and will not change
709 * until we finish handling it. Each packet command is associated with a
710 * callback function that will be called when the command is finished.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100712 * The handling will be done in three stages:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100714 * 1. idetape_issue_pc will send the packet command to the drive, and will set
715 * the interrupt handler to idetape_pc_intr.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100717 * 2. On each interrupt, idetape_pc_intr will be called. This step will be
718 * repeated until the device signals us that no more interrupts will be issued.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100720 * 3. ATAPI Tape media access commands have immediate status with a delayed
721 * process. In case of a successful initiation of a media access packet command,
722 * the DSC bit will be set when the actual execution of the command is finished.
723 * Since the tape drive will not issue an interrupt, we have to poll for this
724 * event. In this case, we define the request as "low priority request" by
725 * setting rq_status to IDETAPE_RQ_POSTPONED, set a timer to poll for DSC and
726 * exit the driver.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100728 * ide.c will then give higher priority to requests which originate from the
729 * other device, until will change rq_status to RQ_ACTIVE.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100731 * 4. When the packet command is finished, it will be checked for errors.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100733 * 5. In case an error was found, we queue a request sense packet command in
734 * front of the request queue and retry the operation up to
735 * IDETAPE_MAX_PC_RETRIES times.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100737 * 6. In case no error was found, or we decided to give up and not to retry
738 * again, the callback function will be called and then we will handle the next
739 * request.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740 */
741static ide_startstop_t idetape_transfer_pc(ide_drive_t *drive)
742{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743 idetape_tape_t *tape = drive->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744
Bartlomiej Zolnierkiewicz594c16d2008-07-15 21:21:58 +0200745 return ide_transfer_pc(drive, tape->pc, idetape_pc_intr,
746 IDETAPE_WAIT_CMD, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747}
748
Borislav Petkovd236d742008-04-18 00:46:27 +0200749static ide_startstop_t idetape_issue_pc(ide_drive_t *drive,
750 struct ide_atapi_pc *pc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752 idetape_tape_t *tape = drive->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753
Borislav Petkov90699ce2008-02-02 19:56:50 +0100754 if (tape->pc->c[0] == REQUEST_SENSE &&
755 pc->c[0] == REQUEST_SENSE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756 printk(KERN_ERR "ide-tape: possible ide-tape.c bug - "
757 "Two request sense in serial were issued\n");
758 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759
Borislav Petkov90699ce2008-02-02 19:56:50 +0100760 if (tape->failed_pc == NULL && pc->c[0] != REQUEST_SENSE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 tape->failed_pc = pc;
762 /* Set the current packet command */
763 tape->pc = pc;
764
765 if (pc->retries > IDETAPE_MAX_PC_RETRIES ||
Borislav Petkov346331f2008-04-18 00:46:26 +0200766 (pc->flags & PC_FLAG_ABORT)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767 /*
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100768 * We will "abort" retrying a packet command in case legitimate
769 * error code was received (crossing a filemark, or end of the
770 * media, for example).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771 */
Borislav Petkov346331f2008-04-18 00:46:26 +0200772 if (!(pc->flags & PC_FLAG_ABORT)) {
Borislav Petkov90699ce2008-02-02 19:56:50 +0100773 if (!(pc->c[0] == TEST_UNIT_READY &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774 tape->sense_key == 2 && tape->asc == 4 &&
775 (tape->ascq == 1 || tape->ascq == 8))) {
776 printk(KERN_ERR "ide-tape: %s: I/O error, "
777 "pc = %2x, key = %2x, "
778 "asc = %2x, ascq = %2x\n",
779 tape->name, pc->c[0],
780 tape->sense_key, tape->asc,
781 tape->ascq);
782 }
783 /* Giving up */
784 pc->error = IDETAPE_ERROR_GENERAL;
785 }
786 tape->failed_pc = NULL;
Borislav Petkov776bb022008-07-23 19:55:59 +0200787 drive->pc_callback(drive);
Bartlomiej Zolnierkiewicz92f5daf2008-07-15 21:21:55 +0200788 return ide_stopped;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789 }
Borislav Petkov8004a8c2008-02-06 02:57:51 +0100790 debug_log(DBG_SENSE, "Retry #%d, cmd = %02X\n", pc->retries, pc->c[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791
792 pc->retries++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793
Bartlomiej Zolnierkiewicz6bf16412008-07-15 21:22:00 +0200794 return ide_issue_pc(drive, pc, idetape_transfer_pc,
795 IDETAPE_WAIT_CMD, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796}
797
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100798/* A mode sense command is used to "sense" tape parameters. */
Borislav Petkovd236d742008-04-18 00:46:27 +0200799static void idetape_create_mode_sense_cmd(struct ide_atapi_pc *pc, u8 page_code)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800{
Bartlomiej Zolnierkiewicz7bf74202008-10-10 22:39:37 +0200801 ide_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +0100802 pc->c[0] = MODE_SENSE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803 if (page_code != IDETAPE_BLOCK_DESCRIPTOR)
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100804 /* DBD = 1 - Don't return block descriptors */
805 pc->c[1] = 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806 pc->c[2] = page_code;
807 /*
808 * Changed pc->c[3] to 0 (255 will at best return unused info).
809 *
810 * For SCSI this byte is defined as subpage instead of high byte
811 * of length and some IDE drives seem to interpret it this way
812 * and return an error when 255 is used.
813 */
814 pc->c[3] = 0;
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100815 /* We will just discard data in that case */
816 pc->c[4] = 255;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817 if (page_code == IDETAPE_BLOCK_DESCRIPTOR)
Borislav Petkovd236d742008-04-18 00:46:27 +0200818 pc->req_xfer = 12;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819 else if (page_code == IDETAPE_CAPABILITIES_PAGE)
Borislav Petkovd236d742008-04-18 00:46:27 +0200820 pc->req_xfer = 24;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821 else
Borislav Petkovd236d742008-04-18 00:46:27 +0200822 pc->req_xfer = 50;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823}
824
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100825static ide_startstop_t idetape_media_access_finished(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826{
Bartlomiej Zolnierkiewiczb73c7ee2008-07-23 19:55:52 +0200827 ide_hwif_t *hwif = drive->hwif;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828 idetape_tape_t *tape = drive->driver_data;
Borislav Petkovd236d742008-04-18 00:46:27 +0200829 struct ide_atapi_pc *pc = tape->pc;
Bartlomiej Zolnierkiewicz22c525b2008-01-25 22:17:11 +0100830 u8 stat;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831
Bartlomiej Zolnierkiewicz374e0422008-07-23 19:55:56 +0200832 stat = hwif->tp_ops->read_status(hwif);
Bartlomiej Zolnierkiewiczc47137a2008-02-06 02:57:51 +0100833
Bartlomiej Zolnierkiewicz3a7d2482008-10-10 22:39:21 +0200834 if (stat & ATA_DSC) {
835 if (stat & ATA_ERR) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836 /* Error detected */
Borislav Petkov90699ce2008-02-02 19:56:50 +0100837 if (pc->c[0] != TEST_UNIT_READY)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838 printk(KERN_ERR "ide-tape: %s: I/O error, ",
839 tape->name);
840 /* Retry operation */
Bartlomiej Zolnierkiewicz258ec412008-07-15 21:21:55 +0200841 idetape_retry_pc(drive);
842 return ide_stopped;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843 }
844 pc->error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845 } else {
846 pc->error = IDETAPE_ERROR_GENERAL;
847 tape->failed_pc = NULL;
848 }
Borislav Petkov776bb022008-07-23 19:55:59 +0200849 drive->pc_callback(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850 return ide_stopped;
851}
852
Borislav Petkovcd2abbf2008-07-15 21:22:03 +0200853static void ide_tape_create_rw_cmd(idetape_tape_t *tape,
Borislav Petkov0014c752008-07-23 19:56:00 +0200854 struct ide_atapi_pc *pc, struct request *rq,
855 u8 opcode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856{
Borislav Petkov0014c752008-07-23 19:56:00 +0200857 struct idetape_bh *bh = (struct idetape_bh *)rq->special;
858 unsigned int length = rq->current_nr_sectors;
859
Bartlomiej Zolnierkiewicz7bf74202008-10-10 22:39:37 +0200860 ide_init_pc(pc);
Borislav Petkov860ff5e2008-02-02 19:56:50 +0100861 put_unaligned(cpu_to_be32(length), (unsigned int *) &pc->c[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862 pc->c[1] = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863 pc->bh = bh;
Borislav Petkovd236d742008-04-18 00:46:27 +0200864 pc->buf = NULL;
865 pc->buf_size = length * tape->blk_size;
866 pc->req_xfer = pc->buf_size;
Borislav Petkovf73850a2008-04-27 15:38:33 +0200867 if (pc->req_xfer == tape->buffer_size)
Bartlomiej Zolnierkiewicz5e331092008-07-15 21:21:56 +0200868 pc->flags |= PC_FLAG_DMA_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869
Borislav Petkovcd2abbf2008-07-15 21:22:03 +0200870 if (opcode == READ_6) {
871 pc->c[0] = READ_6;
872 atomic_set(&bh->b_count, 0);
873 } else if (opcode == WRITE_6) {
874 pc->c[0] = WRITE_6;
875 pc->flags |= PC_FLAG_WRITING;
876 pc->b_data = bh->b_data;
877 pc->b_count = atomic_read(&bh->b_count);
878 }
Borislav Petkov0014c752008-07-23 19:56:00 +0200879
880 memcpy(rq->cmd, pc->c, 12);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881}
882
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883static ide_startstop_t idetape_do_request(ide_drive_t *drive,
884 struct request *rq, sector_t block)
885{
Bartlomiej Zolnierkiewiczb73c7ee2008-07-23 19:55:52 +0200886 ide_hwif_t *hwif = drive->hwif;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887 idetape_tape_t *tape = drive->driver_data;
Borislav Petkovd236d742008-04-18 00:46:27 +0200888 struct ide_atapi_pc *pc = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889 struct request *postponed_rq = tape->postponed_rq;
Bartlomiej Zolnierkiewicz22c525b2008-01-25 22:17:11 +0100890 u8 stat;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891
Mark de Wever730616b2008-10-10 22:39:17 +0200892 debug_log(DBG_SENSE, "sector: %llu, nr_sectors: %lu,"
893 " current_nr_sectors: %u\n",
894 (unsigned long long)rq->sector, rq->nr_sectors,
895 rq->current_nr_sectors);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896
Jens Axboe4aff5e22006-08-10 08:44:47 +0200897 if (!blk_special_request(rq)) {
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100898 /* We do not support buffer cache originated requests. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899 printk(KERN_NOTICE "ide-tape: %s: Unsupported request in "
Jens Axboe4aff5e22006-08-10 08:44:47 +0200900 "request queue (%d)\n", drive->name, rq->cmd_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901 ide_end_request(drive, 0, 0);
902 return ide_stopped;
903 }
904
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100905 /* Retry a failed packet command */
Bartlomiej Zolnierkiewicz28c72142008-07-15 21:21:59 +0200906 if (tape->failed_pc && tape->pc->c[0] == REQUEST_SENSE) {
907 pc = tape->failed_pc;
908 goto out;
909 }
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100910
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911 if (postponed_rq != NULL)
912 if (rq != postponed_rq) {
913 printk(KERN_ERR "ide-tape: ide-tape.c bug - "
914 "Two DSC requests were queued\n");
915 idetape_end_request(drive, 0, 0);
916 return ide_stopped;
917 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918
919 tape->postponed_rq = NULL;
920
921 /*
922 * If the tape is still busy, postpone our request and service
923 * the other device meanwhile.
924 */
Bartlomiej Zolnierkiewicz374e0422008-07-23 19:55:56 +0200925 stat = hwif->tp_ops->read_status(hwif);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926
Borislav Petkov83dd5732008-07-23 19:56:00 +0200927 if (!drive->dsc_overlap && !(rq->cmd[13] & REQ_IDETAPE_PC2))
Borislav Petkovf2e3ab52008-07-23 19:56:01 +0200928 set_bit(IDE_AFLAG_IGNORE_DSC, &drive->atapi_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929
930 if (drive->post_reset == 1) {
Borislav Petkovf2e3ab52008-07-23 19:56:01 +0200931 set_bit(IDE_AFLAG_IGNORE_DSC, &drive->atapi_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932 drive->post_reset = 0;
933 }
934
Borislav Petkovf2e3ab52008-07-23 19:56:01 +0200935 if (!test_and_clear_bit(IDE_AFLAG_IGNORE_DSC, &drive->atapi_flags) &&
Bartlomiej Zolnierkiewicz3a7d2482008-10-10 22:39:21 +0200936 (stat & ATA_DSC) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937 if (postponed_rq == NULL) {
938 tape->dsc_polling_start = jiffies;
Borislav Petkov54bb2072008-02-06 02:57:52 +0100939 tape->dsc_poll_freq = tape->best_dsc_rw_freq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940 tape->dsc_timeout = jiffies + IDETAPE_DSC_RW_TIMEOUT;
941 } else if (time_after(jiffies, tape->dsc_timeout)) {
942 printk(KERN_ERR "ide-tape: %s: DSC timeout\n",
943 tape->name);
Borislav Petkov83dd5732008-07-23 19:56:00 +0200944 if (rq->cmd[13] & REQ_IDETAPE_PC2) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945 idetape_media_access_finished(drive);
946 return ide_stopped;
947 } else {
948 return ide_do_reset(drive);
949 }
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100950 } else if (time_after(jiffies,
951 tape->dsc_polling_start +
952 IDETAPE_DSC_MA_THRESHOLD))
Borislav Petkov54bb2072008-02-06 02:57:52 +0100953 tape->dsc_poll_freq = IDETAPE_DSC_MA_SLOW;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954 idetape_postpone_request(drive);
955 return ide_stopped;
956 }
Borislav Petkov83dd5732008-07-23 19:56:00 +0200957 if (rq->cmd[13] & REQ_IDETAPE_READ) {
Bartlomiej Zolnierkiewicz2e8a6f82008-10-10 22:39:36 +0200958 pc = &tape->queued_pc;
Borislav Petkov0014c752008-07-23 19:56:00 +0200959 ide_tape_create_rw_cmd(tape, pc, rq, READ_6);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960 goto out;
961 }
Borislav Petkov83dd5732008-07-23 19:56:00 +0200962 if (rq->cmd[13] & REQ_IDETAPE_WRITE) {
Bartlomiej Zolnierkiewicz2e8a6f82008-10-10 22:39:36 +0200963 pc = &tape->queued_pc;
Borislav Petkov0014c752008-07-23 19:56:00 +0200964 ide_tape_create_rw_cmd(tape, pc, rq, WRITE_6);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965 goto out;
966 }
Borislav Petkov83dd5732008-07-23 19:56:00 +0200967 if (rq->cmd[13] & REQ_IDETAPE_PC1) {
Borislav Petkovd236d742008-04-18 00:46:27 +0200968 pc = (struct ide_atapi_pc *) rq->buffer;
Borislav Petkov83dd5732008-07-23 19:56:00 +0200969 rq->cmd[13] &= ~(REQ_IDETAPE_PC1);
970 rq->cmd[13] |= REQ_IDETAPE_PC2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971 goto out;
972 }
Borislav Petkov83dd5732008-07-23 19:56:00 +0200973 if (rq->cmd[13] & REQ_IDETAPE_PC2) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974 idetape_media_access_finished(drive);
975 return ide_stopped;
976 }
977 BUG();
Bartlomiej Zolnierkiewicz28c72142008-07-15 21:21:59 +0200978
Borislav Petkovf2e3ab52008-07-23 19:56:01 +0200979out:
Borislav Petkov8d06bfa2008-02-06 02:57:53 +0100980 return idetape_issue_pc(drive, pc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981}
982
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983/*
Borislav Petkov41aa1702008-04-27 15:38:32 +0200984 * The function below uses __get_free_pages to allocate a data buffer of size
Borislav Petkovf73850a2008-04-27 15:38:33 +0200985 * tape->buffer_size (or a bit more). We attempt to combine sequential pages as
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100986 * much as possible.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987 *
Borislav Petkov41aa1702008-04-27 15:38:32 +0200988 * It returns a pointer to the newly allocated buffer, or NULL in case of
989 * failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990 */
Borislav Petkov077e3bd2008-04-27 15:38:34 +0200991static struct idetape_bh *ide_tape_kmalloc_buffer(idetape_tape_t *tape,
992 int full, int clear)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993{
Borislav Petkov077e3bd2008-04-27 15:38:34 +0200994 struct idetape_bh *prev_bh, *bh, *merge_bh;
Borislav Petkova997a432008-04-27 15:38:33 +0200995 int pages = tape->pages_per_buffer;
Borislav Petkov41aa1702008-04-27 15:38:32 +0200996 unsigned int order, b_allocd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997 char *b_data = NULL;
998
Borislav Petkov077e3bd2008-04-27 15:38:34 +0200999 merge_bh = kmalloc(sizeof(struct idetape_bh), GFP_KERNEL);
1000 bh = merge_bh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001 if (bh == NULL)
1002 goto abort;
Borislav Petkov41aa1702008-04-27 15:38:32 +02001003
1004 order = fls(pages) - 1;
1005 bh->b_data = (char *) __get_free_pages(GFP_KERNEL, order);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001006 if (!bh->b_data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007 goto abort;
Borislav Petkov41aa1702008-04-27 15:38:32 +02001008 b_allocd = (1 << order) * PAGE_SIZE;
1009 pages &= (order-1);
1010
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011 if (clear)
Borislav Petkov41aa1702008-04-27 15:38:32 +02001012 memset(bh->b_data, 0, b_allocd);
1013 bh->b_reqnext = NULL;
1014 bh->b_size = b_allocd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015 atomic_set(&bh->b_count, full ? bh->b_size : 0);
1016
Borislav Petkov41aa1702008-04-27 15:38:32 +02001017 while (pages) {
1018 order = fls(pages) - 1;
1019 b_data = (char *) __get_free_pages(GFP_KERNEL, order);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001020 if (!b_data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021 goto abort;
Borislav Petkov41aa1702008-04-27 15:38:32 +02001022 b_allocd = (1 << order) * PAGE_SIZE;
1023
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024 if (clear)
Borislav Petkov41aa1702008-04-27 15:38:32 +02001025 memset(b_data, 0, b_allocd);
1026
1027 /* newly allocated page frames below buffer header or ...*/
1028 if (bh->b_data == b_data + b_allocd) {
1029 bh->b_size += b_allocd;
1030 bh->b_data -= b_allocd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031 if (full)
Borislav Petkov41aa1702008-04-27 15:38:32 +02001032 atomic_add(b_allocd, &bh->b_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033 continue;
1034 }
Borislav Petkov41aa1702008-04-27 15:38:32 +02001035 /* they are above the header */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036 if (b_data == bh->b_data + bh->b_size) {
Borislav Petkov41aa1702008-04-27 15:38:32 +02001037 bh->b_size += b_allocd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038 if (full)
Borislav Petkov41aa1702008-04-27 15:38:32 +02001039 atomic_add(b_allocd, &bh->b_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040 continue;
1041 }
1042 prev_bh = bh;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001043 bh = kmalloc(sizeof(struct idetape_bh), GFP_KERNEL);
1044 if (!bh) {
Borislav Petkov41aa1702008-04-27 15:38:32 +02001045 free_pages((unsigned long) b_data, order);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046 goto abort;
1047 }
1048 bh->b_reqnext = NULL;
1049 bh->b_data = b_data;
Borislav Petkov41aa1702008-04-27 15:38:32 +02001050 bh->b_size = b_allocd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051 atomic_set(&bh->b_count, full ? bh->b_size : 0);
1052 prev_bh->b_reqnext = bh;
Borislav Petkov41aa1702008-04-27 15:38:32 +02001053
1054 pages &= (order-1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055 }
Borislav Petkov41aa1702008-04-27 15:38:32 +02001056
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057 bh->b_size -= tape->excess_bh_size;
1058 if (full)
1059 atomic_sub(tape->excess_bh_size, &bh->b_count);
Borislav Petkov077e3bd2008-04-27 15:38:34 +02001060 return merge_bh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061abort:
Borislav Petkov077e3bd2008-04-27 15:38:34 +02001062 ide_tape_kfree_buffer(tape);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063 return NULL;
1064}
1065
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001066static int idetape_copy_stage_from_user(idetape_tape_t *tape,
Borislav Petkov8646c882008-04-27 15:38:26 +02001067 const char __user *buf, int n)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068{
1069 struct idetape_bh *bh = tape->bh;
1070 int count;
Daniel Walkerdcd96372006-06-25 05:47:37 -07001071 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072
1073 while (n) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074 if (bh == NULL) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001075 printk(KERN_ERR "ide-tape: bh == NULL in %s\n",
1076 __func__);
Daniel Walkerdcd96372006-06-25 05:47:37 -07001077 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078 }
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001079 count = min((unsigned int)
1080 (bh->b_size - atomic_read(&bh->b_count)),
1081 (unsigned int)n);
1082 if (copy_from_user(bh->b_data + atomic_read(&bh->b_count), buf,
1083 count))
Daniel Walkerdcd96372006-06-25 05:47:37 -07001084 ret = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085 n -= count;
1086 atomic_add(count, &bh->b_count);
1087 buf += count;
1088 if (atomic_read(&bh->b_count) == bh->b_size) {
1089 bh = bh->b_reqnext;
1090 if (bh)
1091 atomic_set(&bh->b_count, 0);
1092 }
1093 }
1094 tape->bh = bh;
Daniel Walkerdcd96372006-06-25 05:47:37 -07001095 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096}
1097
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001098static int idetape_copy_stage_to_user(idetape_tape_t *tape, char __user *buf,
Borislav Petkov99d74e62008-04-27 15:38:25 +02001099 int n)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100{
1101 struct idetape_bh *bh = tape->bh;
1102 int count;
Daniel Walkerdcd96372006-06-25 05:47:37 -07001103 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104
1105 while (n) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106 if (bh == NULL) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001107 printk(KERN_ERR "ide-tape: bh == NULL in %s\n",
1108 __func__);
Daniel Walkerdcd96372006-06-25 05:47:37 -07001109 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111 count = min(tape->b_count, n);
Daniel Walkerdcd96372006-06-25 05:47:37 -07001112 if (copy_to_user(buf, tape->b_data, count))
1113 ret = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114 n -= count;
1115 tape->b_data += count;
1116 tape->b_count -= count;
1117 buf += count;
1118 if (!tape->b_count) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001119 bh = bh->b_reqnext;
1120 tape->bh = bh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121 if (bh) {
1122 tape->b_data = bh->b_data;
1123 tape->b_count = atomic_read(&bh->b_count);
1124 }
1125 }
1126 }
Daniel Walkerdcd96372006-06-25 05:47:37 -07001127 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128}
1129
Borislav Petkov077e3bd2008-04-27 15:38:34 +02001130static void idetape_init_merge_buffer(idetape_tape_t *tape)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131{
Borislav Petkov077e3bd2008-04-27 15:38:34 +02001132 struct idetape_bh *bh = tape->merge_bh;
1133 tape->bh = tape->merge_bh;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001134
Borislav Petkov54abf372008-02-06 02:57:52 +01001135 if (tape->chrdev_dir == IDETAPE_DIR_WRITE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136 atomic_set(&bh->b_count, 0);
1137 else {
1138 tape->b_data = bh->b_data;
1139 tape->b_count = atomic_read(&bh->b_count);
1140 }
1141}
1142
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001144 * Write a filemark if write_filemark=1. Flush the device buffers without
1145 * writing a filemark otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146 */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001147static void idetape_create_write_filemark_cmd(ide_drive_t *drive,
Borislav Petkovd236d742008-04-18 00:46:27 +02001148 struct ide_atapi_pc *pc, int write_filemark)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149{
Bartlomiej Zolnierkiewicz7bf74202008-10-10 22:39:37 +02001150 ide_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +01001151 pc->c[0] = WRITE_FILEMARKS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152 pc->c[4] = write_filemark;
Borislav Petkov346331f2008-04-18 00:46:26 +02001153 pc->flags |= PC_FLAG_WAIT_FOR_DSC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154}
1155
Borislav Petkovd236d742008-04-18 00:46:27 +02001156static void idetape_create_test_unit_ready_cmd(struct ide_atapi_pc *pc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157{
Bartlomiej Zolnierkiewicz7bf74202008-10-10 22:39:37 +02001158 ide_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +01001159 pc->c[0] = TEST_UNIT_READY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160}
1161
1162/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001163 * We add a special packet command request to the tail of the request queue, and
Bartlomiej Zolnierkiewicz394a4c22008-10-10 22:39:35 +02001164 * wait for it to be serviced.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165 */
Borislav Petkovea1ab3d2008-04-27 15:38:27 +02001166static int idetape_queue_pc_tail(ide_drive_t *drive, struct ide_atapi_pc *pc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167{
1168 struct ide_tape_obj *tape = drive->driver_data;
FUJITA Tomonori64ea1b42008-07-15 21:21:43 +02001169 struct request *rq;
1170 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171
FUJITA Tomonori64ea1b42008-07-15 21:21:43 +02001172 rq = blk_get_request(drive->queue, READ, __GFP_WAIT);
1173 rq->cmd_type = REQ_TYPE_SPECIAL;
Borislav Petkov83dd5732008-07-23 19:56:00 +02001174 rq->cmd[13] = REQ_IDETAPE_PC1;
FUJITA Tomonori64ea1b42008-07-15 21:21:43 +02001175 rq->buffer = (char *)pc;
Borislav Petkov0014c752008-07-23 19:56:00 +02001176 memcpy(rq->cmd, pc->c, 12);
FUJITA Tomonori64ea1b42008-07-15 21:21:43 +02001177 error = blk_execute_rq(drive->queue, tape->disk, rq, 0);
1178 blk_put_request(rq);
1179 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180}
1181
Borislav Petkovd236d742008-04-18 00:46:27 +02001182static void idetape_create_load_unload_cmd(ide_drive_t *drive,
1183 struct ide_atapi_pc *pc, int cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184{
Bartlomiej Zolnierkiewicz7bf74202008-10-10 22:39:37 +02001185 ide_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +01001186 pc->c[0] = START_STOP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187 pc->c[4] = cmd;
Borislav Petkov346331f2008-04-18 00:46:26 +02001188 pc->flags |= PC_FLAG_WAIT_FOR_DSC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189}
1190
1191static int idetape_wait_ready(ide_drive_t *drive, unsigned long timeout)
1192{
1193 idetape_tape_t *tape = drive->driver_data;
Borislav Petkovd236d742008-04-18 00:46:27 +02001194 struct ide_atapi_pc pc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195 int load_attempted = 0;
1196
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001197 /* Wait for the tape to become ready */
Borislav Petkovf2e3ab52008-07-23 19:56:01 +02001198 set_bit(IDE_AFLAG_MEDIUM_PRESENT, &drive->atapi_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199 timeout += jiffies;
1200 while (time_before(jiffies, timeout)) {
1201 idetape_create_test_unit_ready_cmd(&pc);
Borislav Petkovea1ab3d2008-04-27 15:38:27 +02001202 if (!idetape_queue_pc_tail(drive, &pc))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203 return 0;
1204 if ((tape->sense_key == 2 && tape->asc == 4 && tape->ascq == 2)
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001205 || (tape->asc == 0x3A)) {
1206 /* no media */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207 if (load_attempted)
1208 return -ENOMEDIUM;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001209 idetape_create_load_unload_cmd(drive, &pc,
1210 IDETAPE_LU_LOAD_MASK);
Borislav Petkovea1ab3d2008-04-27 15:38:27 +02001211 idetape_queue_pc_tail(drive, &pc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212 load_attempted = 1;
1213 /* not about to be ready */
1214 } else if (!(tape->sense_key == 2 && tape->asc == 4 &&
1215 (tape->ascq == 1 || tape->ascq == 8)))
1216 return -EIO;
Nishanth Aravamudan80ce45f2005-09-10 00:27:08 -07001217 msleep(100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218 }
1219 return -EIO;
1220}
1221
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001222static int idetape_flush_tape_buffers(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223{
Borislav Petkovd236d742008-04-18 00:46:27 +02001224 struct ide_atapi_pc pc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225 int rc;
1226
1227 idetape_create_write_filemark_cmd(drive, &pc, 0);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001228 rc = idetape_queue_pc_tail(drive, &pc);
1229 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230 return rc;
1231 idetape_wait_ready(drive, 60 * 5 * HZ);
1232 return 0;
1233}
1234
Borislav Petkovd236d742008-04-18 00:46:27 +02001235static void idetape_create_read_position_cmd(struct ide_atapi_pc *pc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236{
Bartlomiej Zolnierkiewicz7bf74202008-10-10 22:39:37 +02001237 ide_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +01001238 pc->c[0] = READ_POSITION;
Borislav Petkovd236d742008-04-18 00:46:27 +02001239 pc->req_xfer = 20;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240}
1241
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001242static int idetape_read_position(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243{
1244 idetape_tape_t *tape = drive->driver_data;
Borislav Petkovd236d742008-04-18 00:46:27 +02001245 struct ide_atapi_pc pc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246 int position;
1247
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001248 debug_log(DBG_PROCS, "Enter %s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249
1250 idetape_create_read_position_cmd(&pc);
1251 if (idetape_queue_pc_tail(drive, &pc))
1252 return -1;
Borislav Petkov54bb2072008-02-06 02:57:52 +01001253 position = tape->first_frame;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254 return position;
1255}
1256
Borislav Petkovd236d742008-04-18 00:46:27 +02001257static void idetape_create_locate_cmd(ide_drive_t *drive,
1258 struct ide_atapi_pc *pc,
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001259 unsigned int block, u8 partition, int skip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260{
Bartlomiej Zolnierkiewicz7bf74202008-10-10 22:39:37 +02001261 ide_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +01001262 pc->c[0] = POSITION_TO_ELEMENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263 pc->c[1] = 2;
Borislav Petkov860ff5e2008-02-02 19:56:50 +01001264 put_unaligned(cpu_to_be32(block), (unsigned int *) &pc->c[3]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265 pc->c[8] = partition;
Borislav Petkov346331f2008-04-18 00:46:26 +02001266 pc->flags |= PC_FLAG_WAIT_FOR_DSC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267}
1268
Borislav Petkovd236d742008-04-18 00:46:27 +02001269static int idetape_create_prevent_cmd(ide_drive_t *drive,
1270 struct ide_atapi_pc *pc, int prevent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271{
1272 idetape_tape_t *tape = drive->driver_data;
1273
Borislav Petkovb6422012008-02-02 19:56:49 +01001274 /* device supports locking according to capabilities page */
1275 if (!(tape->caps[6] & 0x01))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276 return 0;
1277
Bartlomiej Zolnierkiewicz7bf74202008-10-10 22:39:37 +02001278 ide_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +01001279 pc->c[0] = ALLOW_MEDIUM_REMOVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280 pc->c[4] = prevent;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281 return 1;
1282}
1283
Bartlomiej Zolnierkiewicz385a4b82008-10-10 22:39:37 +02001284static int ide_tape_set_media_lock(ide_drive_t *drive, int on)
1285{
1286 struct ide_atapi_pc pc;
1287
1288 if (!idetape_create_prevent_cmd(drive, &pc, on))
1289 return 0;
1290
1291 return idetape_queue_pc_tail(drive, &pc);
1292}
1293
Borislav Petkovec0fdb02008-04-27 15:38:34 +02001294static void __ide_tape_discard_merge_buffer(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295{
1296 idetape_tape_t *tape = drive->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297
Borislav Petkov54abf372008-02-06 02:57:52 +01001298 if (tape->chrdev_dir != IDETAPE_DIR_READ)
Borislav Petkov97986302008-04-27 15:38:34 +02001299 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300
Borislav Petkovf2e3ab52008-07-23 19:56:01 +02001301 clear_bit(IDE_AFLAG_FILEMARK, &drive->atapi_flags);
Borislav Petkov01a63aeb2008-04-27 15:38:34 +02001302 tape->merge_bh_size = 0;
Borislav Petkov077e3bd2008-04-27 15:38:34 +02001303 if (tape->merge_bh != NULL) {
1304 ide_tape_kfree_buffer(tape);
1305 tape->merge_bh = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306 }
1307
Borislav Petkov54abf372008-02-06 02:57:52 +01001308 tape->chrdev_dir = IDETAPE_DIR_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309}
1310
1311/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001312 * Position the tape to the requested block using the LOCATE packet command.
1313 * A READ POSITION command is then issued to check where we are positioned. Like
1314 * all higher level operations, we queue the commands at the tail of the request
1315 * queue and wait for their completion.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316 */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001317static int idetape_position_tape(ide_drive_t *drive, unsigned int block,
1318 u8 partition, int skip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319{
1320 idetape_tape_t *tape = drive->driver_data;
1321 int retval;
Borislav Petkovd236d742008-04-18 00:46:27 +02001322 struct ide_atapi_pc pc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323
Borislav Petkov54abf372008-02-06 02:57:52 +01001324 if (tape->chrdev_dir == IDETAPE_DIR_READ)
Borislav Petkovec0fdb02008-04-27 15:38:34 +02001325 __ide_tape_discard_merge_buffer(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326 idetape_wait_ready(drive, 60 * 5 * HZ);
1327 idetape_create_locate_cmd(drive, &pc, block, partition, skip);
1328 retval = idetape_queue_pc_tail(drive, &pc);
1329 if (retval)
1330 return (retval);
1331
1332 idetape_create_read_position_cmd(&pc);
1333 return (idetape_queue_pc_tail(drive, &pc));
1334}
1335
Borislav Petkovec0fdb02008-04-27 15:38:34 +02001336static void ide_tape_discard_merge_buffer(ide_drive_t *drive,
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001337 int restore_position)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338{
1339 idetape_tape_t *tape = drive->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340 int seek, position;
1341
Borislav Petkovec0fdb02008-04-27 15:38:34 +02001342 __ide_tape_discard_merge_buffer(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343 if (restore_position) {
1344 position = idetape_read_position(drive);
Borislav Petkov97986302008-04-27 15:38:34 +02001345 seek = position > 0 ? position : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346 if (idetape_position_tape(drive, seek, 0, 0)) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001347 printk(KERN_INFO "ide-tape: %s: position_tape failed in"
Borislav Petkovec0fdb02008-04-27 15:38:34 +02001348 " %s\n", tape->name, __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349 return;
1350 }
1351 }
1352}
1353
1354/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001355 * Generate a read/write request for the block device interface and wait for it
1356 * to be serviced.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357 */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001358static int idetape_queue_rw_tail(ide_drive_t *drive, int cmd, int blocks,
1359 struct idetape_bh *bh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360{
1361 idetape_tape_t *tape = drive->driver_data;
FUJITA Tomonori64ea1b42008-07-15 21:21:43 +02001362 struct request *rq;
1363 int ret, errors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001365 debug_log(DBG_SENSE, "%s: cmd=%d\n", __func__, cmd);
1366
FUJITA Tomonori64ea1b42008-07-15 21:21:43 +02001367 rq = blk_get_request(drive->queue, READ, __GFP_WAIT);
1368 rq->cmd_type = REQ_TYPE_SPECIAL;
Borislav Petkov83dd5732008-07-23 19:56:00 +02001369 rq->cmd[13] = cmd;
FUJITA Tomonori64ea1b42008-07-15 21:21:43 +02001370 rq->rq_disk = tape->disk;
1371 rq->special = (void *)bh;
1372 rq->sector = tape->first_frame;
1373 rq->nr_sectors = blocks;
1374 rq->current_nr_sectors = blocks;
1375 blk_execute_rq(drive->queue, tape->disk, rq, 0);
1376
1377 errors = rq->errors;
1378 ret = tape->blk_size * (blocks - rq->current_nr_sectors);
1379 blk_put_request(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380
1381 if ((cmd & (REQ_IDETAPE_READ | REQ_IDETAPE_WRITE)) == 0)
1382 return 0;
1383
Borislav Petkov077e3bd2008-04-27 15:38:34 +02001384 if (tape->merge_bh)
1385 idetape_init_merge_buffer(tape);
FUJITA Tomonori64ea1b42008-07-15 21:21:43 +02001386 if (errors == IDETAPE_ERROR_GENERAL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387 return -EIO;
FUJITA Tomonori64ea1b42008-07-15 21:21:43 +02001388 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389}
1390
Borislav Petkovd236d742008-04-18 00:46:27 +02001391static void idetape_create_inquiry_cmd(struct ide_atapi_pc *pc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392{
Bartlomiej Zolnierkiewicz7bf74202008-10-10 22:39:37 +02001393 ide_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +01001394 pc->c[0] = INQUIRY;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001395 pc->c[4] = 254;
Borislav Petkovd236d742008-04-18 00:46:27 +02001396 pc->req_xfer = 254;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397}
1398
Borislav Petkovd236d742008-04-18 00:46:27 +02001399static void idetape_create_rewind_cmd(ide_drive_t *drive,
1400 struct ide_atapi_pc *pc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401{
Bartlomiej Zolnierkiewicz7bf74202008-10-10 22:39:37 +02001402 ide_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +01001403 pc->c[0] = REZERO_UNIT;
Borislav Petkov346331f2008-04-18 00:46:26 +02001404 pc->flags |= PC_FLAG_WAIT_FOR_DSC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405}
1406
Borislav Petkovd236d742008-04-18 00:46:27 +02001407static void idetape_create_erase_cmd(struct ide_atapi_pc *pc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408{
Bartlomiej Zolnierkiewicz7bf74202008-10-10 22:39:37 +02001409 ide_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +01001410 pc->c[0] = ERASE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411 pc->c[1] = 1;
Borislav Petkov346331f2008-04-18 00:46:26 +02001412 pc->flags |= PC_FLAG_WAIT_FOR_DSC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413}
1414
Borislav Petkovd236d742008-04-18 00:46:27 +02001415static void idetape_create_space_cmd(struct ide_atapi_pc *pc, int count, u8 cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416{
Bartlomiej Zolnierkiewicz7bf74202008-10-10 22:39:37 +02001417 ide_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +01001418 pc->c[0] = SPACE;
Borislav Petkov860ff5e2008-02-02 19:56:50 +01001419 put_unaligned(cpu_to_be32(count), (unsigned int *) &pc->c[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420 pc->c[1] = cmd;
Borislav Petkov346331f2008-04-18 00:46:26 +02001421 pc->flags |= PC_FLAG_WAIT_FOR_DSC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422}
1423
Borislav Petkov97c566c2008-04-27 15:38:25 +02001424/* Queue up a character device originated write request. */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001425static int idetape_add_chrdev_write_request(ide_drive_t *drive, int blocks)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426{
1427 idetape_tape_t *tape = drive->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001429 debug_log(DBG_CHRDEV, "Enter %s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430
Borislav Petkov0aa4b012008-04-27 15:38:27 +02001431 return idetape_queue_rw_tail(drive, REQ_IDETAPE_WRITE,
Borislav Petkov077e3bd2008-04-27 15:38:34 +02001432 blocks, tape->merge_bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433}
1434
Borislav Petkovd9df9372008-04-27 15:38:34 +02001435static void ide_tape_flush_merge_buffer(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436{
1437 idetape_tape_t *tape = drive->driver_data;
1438 int blocks, min;
1439 struct idetape_bh *bh;
Borislav Petkov55a5d292008-02-02 19:56:49 +01001440
Borislav Petkov54abf372008-02-06 02:57:52 +01001441 if (tape->chrdev_dir != IDETAPE_DIR_WRITE) {
Borislav Petkov077e3bd2008-04-27 15:38:34 +02001442 printk(KERN_ERR "ide-tape: bug: Trying to empty merge buffer"
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001443 " but we are not writing.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444 return;
1445 }
Borislav Petkov01a63aeb2008-04-27 15:38:34 +02001446 if (tape->merge_bh_size > tape->buffer_size) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447 printk(KERN_ERR "ide-tape: bug: merge_buffer too big\n");
Borislav Petkov01a63aeb2008-04-27 15:38:34 +02001448 tape->merge_bh_size = tape->buffer_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449 }
Borislav Petkov01a63aeb2008-04-27 15:38:34 +02001450 if (tape->merge_bh_size) {
1451 blocks = tape->merge_bh_size / tape->blk_size;
1452 if (tape->merge_bh_size % tape->blk_size) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453 unsigned int i;
1454
1455 blocks++;
Borislav Petkov01a63aeb2008-04-27 15:38:34 +02001456 i = tape->blk_size - tape->merge_bh_size %
Borislav Petkov54bb2072008-02-06 02:57:52 +01001457 tape->blk_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458 bh = tape->bh->b_reqnext;
1459 while (bh) {
1460 atomic_set(&bh->b_count, 0);
1461 bh = bh->b_reqnext;
1462 }
1463 bh = tape->bh;
1464 while (i) {
1465 if (bh == NULL) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001466 printk(KERN_INFO "ide-tape: bug,"
1467 " bh NULL\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468 break;
1469 }
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001470 min = min(i, (unsigned int)(bh->b_size -
1471 atomic_read(&bh->b_count)));
1472 memset(bh->b_data + atomic_read(&bh->b_count),
1473 0, min);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474 atomic_add(min, &bh->b_count);
1475 i -= min;
1476 bh = bh->b_reqnext;
1477 }
1478 }
1479 (void) idetape_add_chrdev_write_request(drive, blocks);
Borislav Petkov01a63aeb2008-04-27 15:38:34 +02001480 tape->merge_bh_size = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481 }
Borislav Petkov077e3bd2008-04-27 15:38:34 +02001482 if (tape->merge_bh != NULL) {
1483 ide_tape_kfree_buffer(tape);
1484 tape->merge_bh = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485 }
Borislav Petkov54abf372008-02-06 02:57:52 +01001486 tape->chrdev_dir = IDETAPE_DIR_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487}
1488
Borislav Petkov83042b22008-04-27 15:38:27 +02001489static int idetape_init_read(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490{
1491 idetape_tape_t *tape = drive->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492 int bytes_read;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493
1494 /* Initialize read operation */
Borislav Petkov54abf372008-02-06 02:57:52 +01001495 if (tape->chrdev_dir != IDETAPE_DIR_READ) {
1496 if (tape->chrdev_dir == IDETAPE_DIR_WRITE) {
Borislav Petkovd9df9372008-04-27 15:38:34 +02001497 ide_tape_flush_merge_buffer(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498 idetape_flush_tape_buffers(drive);
1499 }
Borislav Petkov077e3bd2008-04-27 15:38:34 +02001500 if (tape->merge_bh || tape->merge_bh_size) {
Borislav Petkov01a63aeb2008-04-27 15:38:34 +02001501 printk(KERN_ERR "ide-tape: merge_bh_size should be"
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001502 " 0 now\n");
Borislav Petkov01a63aeb2008-04-27 15:38:34 +02001503 tape->merge_bh_size = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504 }
Borislav Petkov077e3bd2008-04-27 15:38:34 +02001505 tape->merge_bh = ide_tape_kmalloc_buffer(tape, 0, 0);
1506 if (!tape->merge_bh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507 return -ENOMEM;
Borislav Petkov54abf372008-02-06 02:57:52 +01001508 tape->chrdev_dir = IDETAPE_DIR_READ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509
1510 /*
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001511 * Issue a read 0 command to ensure that DSC handshake is
1512 * switched from completion mode to buffer available mode.
1513 * No point in issuing this if DSC overlap isn't supported, some
1514 * drives (Seagate STT3401A) will return an error.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515 */
1516 if (drive->dsc_overlap) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001517 bytes_read = idetape_queue_rw_tail(drive,
1518 REQ_IDETAPE_READ, 0,
Borislav Petkov077e3bd2008-04-27 15:38:34 +02001519 tape->merge_bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520 if (bytes_read < 0) {
Borislav Petkov077e3bd2008-04-27 15:38:34 +02001521 ide_tape_kfree_buffer(tape);
1522 tape->merge_bh = NULL;
Borislav Petkov54abf372008-02-06 02:57:52 +01001523 tape->chrdev_dir = IDETAPE_DIR_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524 return bytes_read;
1525 }
1526 }
1527 }
Borislav Petkov5e69bd92008-04-27 15:38:25 +02001528
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529 return 0;
1530}
1531
Borislav Petkov5bd50dc2008-04-27 15:38:28 +02001532/* called from idetape_chrdev_read() to service a chrdev read request. */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001533static int idetape_add_chrdev_read_request(ide_drive_t *drive, int blocks)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534{
1535 idetape_tape_t *tape = drive->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001537 debug_log(DBG_PROCS, "Enter %s, %d blocks\n", __func__, blocks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001538
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001539 /* If we are at a filemark, return a read length of 0 */
Borislav Petkovf2e3ab52008-07-23 19:56:01 +02001540 if (test_bit(IDE_AFLAG_FILEMARK, &drive->atapi_flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541 return 0;
1542
Borislav Petkov83042b22008-04-27 15:38:27 +02001543 idetape_init_read(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544
Borislav Petkov5e69bd92008-04-27 15:38:25 +02001545 return idetape_queue_rw_tail(drive, REQ_IDETAPE_READ, blocks,
Borislav Petkov077e3bd2008-04-27 15:38:34 +02001546 tape->merge_bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001547}
1548
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001549static void idetape_pad_zeros(ide_drive_t *drive, int bcount)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550{
1551 idetape_tape_t *tape = drive->driver_data;
1552 struct idetape_bh *bh;
1553 int blocks;
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001554
Linus Torvalds1da177e2005-04-16 15:20:36 -07001555 while (bcount) {
1556 unsigned int count;
1557
Borislav Petkov077e3bd2008-04-27 15:38:34 +02001558 bh = tape->merge_bh;
Borislav Petkovf73850a2008-04-27 15:38:33 +02001559 count = min(tape->buffer_size, bcount);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560 bcount -= count;
Borislav Petkov54bb2072008-02-06 02:57:52 +01001561 blocks = count / tape->blk_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001562 while (count) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001563 atomic_set(&bh->b_count,
1564 min(count, (unsigned int)bh->b_size));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565 memset(bh->b_data, 0, atomic_read(&bh->b_count));
1566 count -= atomic_read(&bh->b_count);
1567 bh = bh->b_reqnext;
1568 }
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001569 idetape_queue_rw_tail(drive, REQ_IDETAPE_WRITE, blocks,
Borislav Petkov077e3bd2008-04-27 15:38:34 +02001570 tape->merge_bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571 }
1572}
1573
Linus Torvalds1da177e2005-04-16 15:20:36 -07001574/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001575 * Rewinds the tape to the Beginning Of the current Partition (BOP). We
1576 * currently support only one partition.
1577 */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001578static int idetape_rewind_tape(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579{
1580 int retval;
Borislav Petkovd236d742008-04-18 00:46:27 +02001581 struct ide_atapi_pc pc;
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001582 idetape_tape_t *tape;
1583 tape = drive->driver_data;
1584
1585 debug_log(DBG_SENSE, "Enter %s\n", __func__);
1586
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587 idetape_create_rewind_cmd(drive, &pc);
1588 retval = idetape_queue_pc_tail(drive, &pc);
1589 if (retval)
1590 return retval;
1591
1592 idetape_create_read_position_cmd(&pc);
1593 retval = idetape_queue_pc_tail(drive, &pc);
1594 if (retval)
1595 return retval;
1596 return 0;
1597}
1598
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001599/* mtio.h compatible commands should be issued to the chrdev interface. */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001600static int idetape_blkdev_ioctl(ide_drive_t *drive, unsigned int cmd,
1601 unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001602{
1603 idetape_tape_t *tape = drive->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604 void __user *argp = (void __user *)arg;
1605
Borislav Petkovd59823f2008-02-02 19:56:51 +01001606 struct idetape_config {
1607 int dsc_rw_frequency;
1608 int dsc_media_access_frequency;
1609 int nr_stages;
1610 } config;
1611
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001612 debug_log(DBG_PROCS, "Enter %s\n", __func__);
1613
Linus Torvalds1da177e2005-04-16 15:20:36 -07001614 switch (cmd) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001615 case 0x0340:
1616 if (copy_from_user(&config, argp, sizeof(config)))
1617 return -EFAULT;
1618 tape->best_dsc_rw_freq = config.dsc_rw_frequency;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001619 break;
1620 case 0x0350:
1621 config.dsc_rw_frequency = (int) tape->best_dsc_rw_freq;
Borislav Petkov83042b22008-04-27 15:38:27 +02001622 config.nr_stages = 1;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001623 if (copy_to_user(argp, &config, sizeof(config)))
1624 return -EFAULT;
1625 break;
1626 default:
1627 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001628 }
1629 return 0;
1630}
1631
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001632static int idetape_space_over_filemarks(ide_drive_t *drive, short mt_op,
1633 int mt_count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634{
1635 idetape_tape_t *tape = drive->driver_data;
Borislav Petkovd236d742008-04-18 00:46:27 +02001636 struct ide_atapi_pc pc;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001637 int retval, count = 0;
Borislav Petkovb6422012008-02-02 19:56:49 +01001638 int sprev = !!(tape->caps[4] & 0x20);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001639
1640 if (mt_count == 0)
1641 return 0;
1642 if (MTBSF == mt_op || MTBSFM == mt_op) {
Borislav Petkovb6422012008-02-02 19:56:49 +01001643 if (!sprev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644 return -EIO;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001645 mt_count = -mt_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001646 }
1647
Borislav Petkov54abf372008-02-06 02:57:52 +01001648 if (tape->chrdev_dir == IDETAPE_DIR_READ) {
Borislav Petkov01a63aeb2008-04-27 15:38:34 +02001649 tape->merge_bh_size = 0;
Borislav Petkovf2e3ab52008-07-23 19:56:01 +02001650 if (test_and_clear_bit(IDE_AFLAG_FILEMARK, &drive->atapi_flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001651 ++count;
Borislav Petkovec0fdb02008-04-27 15:38:34 +02001652 ide_tape_discard_merge_buffer(drive, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001653 }
1654
Linus Torvalds1da177e2005-04-16 15:20:36 -07001655 switch (mt_op) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001656 case MTFSF:
1657 case MTBSF:
1658 idetape_create_space_cmd(&pc, mt_count - count,
1659 IDETAPE_SPACE_OVER_FILEMARK);
1660 return idetape_queue_pc_tail(drive, &pc);
1661 case MTFSFM:
1662 case MTBSFM:
1663 if (!sprev)
1664 return -EIO;
1665 retval = idetape_space_over_filemarks(drive, MTFSF,
1666 mt_count - count);
1667 if (retval)
1668 return retval;
1669 count = (MTBSFM == mt_op ? 1 : -1);
1670 return idetape_space_over_filemarks(drive, MTFSF, count);
1671 default:
1672 printk(KERN_ERR "ide-tape: MTIO operation %d not supported\n",
1673 mt_op);
1674 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675 }
1676}
1677
Linus Torvalds1da177e2005-04-16 15:20:36 -07001678/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001679 * Our character device read / write functions.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001680 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001681 * The tape is optimized to maximize throughput when it is transferring an
1682 * integral number of the "continuous transfer limit", which is a parameter of
1683 * the specific tape (26kB on my particular tape, 32kB for Onstream).
Linus Torvalds1da177e2005-04-16 15:20:36 -07001684 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001685 * As of version 1.3 of the driver, the character device provides an abstract
1686 * continuous view of the media - any mix of block sizes (even 1 byte) on the
1687 * same backup/restore procedure is supported. The driver will internally
1688 * convert the requests to the recommended transfer unit, so that an unmatch
1689 * between the user's block size to the recommended size will only result in a
1690 * (slightly) increased driver overhead, but will no longer hit performance.
1691 * This is not applicable to Onstream.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001692 */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001693static ssize_t idetape_chrdev_read(struct file *file, char __user *buf,
1694 size_t count, loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001695{
1696 struct ide_tape_obj *tape = ide_tape_f(file);
1697 ide_drive_t *drive = tape->drive;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001698 ssize_t bytes_read, temp, actually_read = 0, rc;
Daniel Walkerdcd96372006-06-25 05:47:37 -07001699 ssize_t ret = 0;
Borislav Petkovb6422012008-02-02 19:56:49 +01001700 u16 ctl = *(u16 *)&tape->caps[12];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001701
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001702 debug_log(DBG_CHRDEV, "Enter %s, count %Zd\n", __func__, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001703
Borislav Petkov54abf372008-02-06 02:57:52 +01001704 if (tape->chrdev_dir != IDETAPE_DIR_READ) {
Borislav Petkovf2e3ab52008-07-23 19:56:01 +02001705 if (test_bit(IDE_AFLAG_DETECT_BS, &drive->atapi_flags))
Borislav Petkov54bb2072008-02-06 02:57:52 +01001706 if (count > tape->blk_size &&
1707 (count % tape->blk_size) == 0)
1708 tape->user_bs_factor = count / tape->blk_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001709 }
Borislav Petkov83042b22008-04-27 15:38:27 +02001710 rc = idetape_init_read(drive);
Borislav Petkov8d06bfa2008-02-06 02:57:53 +01001711 if (rc < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001712 return rc;
1713 if (count == 0)
1714 return (0);
Borislav Petkov01a63aeb2008-04-27 15:38:34 +02001715 if (tape->merge_bh_size) {
1716 actually_read = min((unsigned int)(tape->merge_bh_size),
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001717 (unsigned int)count);
Borislav Petkov99d74e62008-04-27 15:38:25 +02001718 if (idetape_copy_stage_to_user(tape, buf, actually_read))
Daniel Walkerdcd96372006-06-25 05:47:37 -07001719 ret = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001720 buf += actually_read;
Borislav Petkov01a63aeb2008-04-27 15:38:34 +02001721 tape->merge_bh_size -= actually_read;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001722 count -= actually_read;
1723 }
Borislav Petkovf73850a2008-04-27 15:38:33 +02001724 while (count >= tape->buffer_size) {
Borislav Petkovb6422012008-02-02 19:56:49 +01001725 bytes_read = idetape_add_chrdev_read_request(drive, ctl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001726 if (bytes_read <= 0)
1727 goto finish;
Borislav Petkov99d74e62008-04-27 15:38:25 +02001728 if (idetape_copy_stage_to_user(tape, buf, bytes_read))
Daniel Walkerdcd96372006-06-25 05:47:37 -07001729 ret = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001730 buf += bytes_read;
1731 count -= bytes_read;
1732 actually_read += bytes_read;
1733 }
1734 if (count) {
Borislav Petkovb6422012008-02-02 19:56:49 +01001735 bytes_read = idetape_add_chrdev_read_request(drive, ctl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001736 if (bytes_read <= 0)
1737 goto finish;
1738 temp = min((unsigned long)count, (unsigned long)bytes_read);
Borislav Petkov99d74e62008-04-27 15:38:25 +02001739 if (idetape_copy_stage_to_user(tape, buf, temp))
Daniel Walkerdcd96372006-06-25 05:47:37 -07001740 ret = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001741 actually_read += temp;
Borislav Petkov01a63aeb2008-04-27 15:38:34 +02001742 tape->merge_bh_size = bytes_read-temp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001743 }
1744finish:
Borislav Petkovf2e3ab52008-07-23 19:56:01 +02001745 if (!actually_read && test_bit(IDE_AFLAG_FILEMARK, &drive->atapi_flags)) {
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001746 debug_log(DBG_SENSE, "%s: spacing over filemark\n", tape->name);
1747
Linus Torvalds1da177e2005-04-16 15:20:36 -07001748 idetape_space_over_filemarks(drive, MTFSF, 1);
1749 return 0;
1750 }
Daniel Walkerdcd96372006-06-25 05:47:37 -07001751
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001752 return ret ? ret : actually_read;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001753}
1754
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001755static ssize_t idetape_chrdev_write(struct file *file, const char __user *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001756 size_t count, loff_t *ppos)
1757{
1758 struct ide_tape_obj *tape = ide_tape_f(file);
1759 ide_drive_t *drive = tape->drive;
Daniel Walkerdcd96372006-06-25 05:47:37 -07001760 ssize_t actually_written = 0;
1761 ssize_t ret = 0;
Borislav Petkovb6422012008-02-02 19:56:49 +01001762 u16 ctl = *(u16 *)&tape->caps[12];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001763
1764 /* The drive is write protected. */
1765 if (tape->write_prot)
1766 return -EACCES;
1767
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001768 debug_log(DBG_CHRDEV, "Enter %s, count %Zd\n", __func__, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001769
1770 /* Initialize write operation */
Borislav Petkov54abf372008-02-06 02:57:52 +01001771 if (tape->chrdev_dir != IDETAPE_DIR_WRITE) {
1772 if (tape->chrdev_dir == IDETAPE_DIR_READ)
Borislav Petkovec0fdb02008-04-27 15:38:34 +02001773 ide_tape_discard_merge_buffer(drive, 1);
Borislav Petkov077e3bd2008-04-27 15:38:34 +02001774 if (tape->merge_bh || tape->merge_bh_size) {
Borislav Petkov01a63aeb2008-04-27 15:38:34 +02001775 printk(KERN_ERR "ide-tape: merge_bh_size "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001776 "should be 0 now\n");
Borislav Petkov01a63aeb2008-04-27 15:38:34 +02001777 tape->merge_bh_size = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001778 }
Borislav Petkov077e3bd2008-04-27 15:38:34 +02001779 tape->merge_bh = ide_tape_kmalloc_buffer(tape, 0, 0);
1780 if (!tape->merge_bh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001781 return -ENOMEM;
Borislav Petkov54abf372008-02-06 02:57:52 +01001782 tape->chrdev_dir = IDETAPE_DIR_WRITE;
Borislav Petkov077e3bd2008-04-27 15:38:34 +02001783 idetape_init_merge_buffer(tape);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001784
1785 /*
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001786 * Issue a write 0 command to ensure that DSC handshake is
1787 * switched from completion mode to buffer available mode. No
1788 * point in issuing this if DSC overlap isn't supported, some
1789 * drives (Seagate STT3401A) will return an error.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790 */
1791 if (drive->dsc_overlap) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001792 ssize_t retval = idetape_queue_rw_tail(drive,
1793 REQ_IDETAPE_WRITE, 0,
Borislav Petkov077e3bd2008-04-27 15:38:34 +02001794 tape->merge_bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001795 if (retval < 0) {
Borislav Petkov077e3bd2008-04-27 15:38:34 +02001796 ide_tape_kfree_buffer(tape);
1797 tape->merge_bh = NULL;
Borislav Petkov54abf372008-02-06 02:57:52 +01001798 tape->chrdev_dir = IDETAPE_DIR_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001799 return retval;
1800 }
1801 }
1802 }
1803 if (count == 0)
1804 return (0);
Borislav Petkov01a63aeb2008-04-27 15:38:34 +02001805 if (tape->merge_bh_size) {
1806 if (tape->merge_bh_size >= tape->buffer_size) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001807 printk(KERN_ERR "ide-tape: bug: merge buf too big\n");
Borislav Petkov01a63aeb2008-04-27 15:38:34 +02001808 tape->merge_bh_size = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001809 }
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001810 actually_written = min((unsigned int)
Borislav Petkov01a63aeb2008-04-27 15:38:34 +02001811 (tape->buffer_size - tape->merge_bh_size),
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001812 (unsigned int)count);
Borislav Petkov8646c882008-04-27 15:38:26 +02001813 if (idetape_copy_stage_from_user(tape, buf, actually_written))
Daniel Walkerdcd96372006-06-25 05:47:37 -07001814 ret = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001815 buf += actually_written;
Borislav Petkov01a63aeb2008-04-27 15:38:34 +02001816 tape->merge_bh_size += actually_written;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001817 count -= actually_written;
1818
Borislav Petkov01a63aeb2008-04-27 15:38:34 +02001819 if (tape->merge_bh_size == tape->buffer_size) {
Daniel Walkerdcd96372006-06-25 05:47:37 -07001820 ssize_t retval;
Borislav Petkov01a63aeb2008-04-27 15:38:34 +02001821 tape->merge_bh_size = 0;
Borislav Petkovb6422012008-02-02 19:56:49 +01001822 retval = idetape_add_chrdev_write_request(drive, ctl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001823 if (retval <= 0)
1824 return (retval);
1825 }
1826 }
Borislav Petkovf73850a2008-04-27 15:38:33 +02001827 while (count >= tape->buffer_size) {
Daniel Walkerdcd96372006-06-25 05:47:37 -07001828 ssize_t retval;
Borislav Petkovf73850a2008-04-27 15:38:33 +02001829 if (idetape_copy_stage_from_user(tape, buf, tape->buffer_size))
Daniel Walkerdcd96372006-06-25 05:47:37 -07001830 ret = -EFAULT;
Borislav Petkovf73850a2008-04-27 15:38:33 +02001831 buf += tape->buffer_size;
1832 count -= tape->buffer_size;
Borislav Petkovb6422012008-02-02 19:56:49 +01001833 retval = idetape_add_chrdev_write_request(drive, ctl);
Borislav Petkovf73850a2008-04-27 15:38:33 +02001834 actually_written += tape->buffer_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001835 if (retval <= 0)
1836 return (retval);
1837 }
1838 if (count) {
1839 actually_written += count;
Borislav Petkov8646c882008-04-27 15:38:26 +02001840 if (idetape_copy_stage_from_user(tape, buf, count))
Daniel Walkerdcd96372006-06-25 05:47:37 -07001841 ret = -EFAULT;
Borislav Petkov01a63aeb2008-04-27 15:38:34 +02001842 tape->merge_bh_size += count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001843 }
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001844 return ret ? ret : actually_written;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001845}
1846
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001847static int idetape_write_filemark(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001848{
Borislav Petkovd236d742008-04-18 00:46:27 +02001849 struct ide_atapi_pc pc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001850
1851 /* Write a filemark */
1852 idetape_create_write_filemark_cmd(drive, &pc, 1);
1853 if (idetape_queue_pc_tail(drive, &pc)) {
1854 printk(KERN_ERR "ide-tape: Couldn't write a filemark\n");
1855 return -EIO;
1856 }
1857 return 0;
1858}
1859
1860/*
Borislav Petkovd99c9da2008-02-02 19:56:51 +01001861 * Called from idetape_chrdev_ioctl when the general mtio MTIOCTOP ioctl is
1862 * requested.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001863 *
Borislav Petkovd99c9da2008-02-02 19:56:51 +01001864 * Note: MTBSF and MTBSFM are not supported when the tape doesn't support
1865 * spacing over filemarks in the reverse direction. In this case, MTFSFM is also
Borislav Petkov5bd50dc2008-04-27 15:38:28 +02001866 * usually not supported.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001867 *
Borislav Petkovd99c9da2008-02-02 19:56:51 +01001868 * The following commands are currently not supported:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001869 *
Borislav Petkovd99c9da2008-02-02 19:56:51 +01001870 * MTFSS, MTBSS, MTWSM, MTSETDENSITY, MTSETDRVBUFFER, MT_ST_BOOLEANS,
1871 * MT_ST_WRITE_THRESHOLD.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001872 */
Borislav Petkovd99c9da2008-02-02 19:56:51 +01001873static int idetape_mtioctop(ide_drive_t *drive, short mt_op, int mt_count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001874{
1875 idetape_tape_t *tape = drive->driver_data;
Borislav Petkovd236d742008-04-18 00:46:27 +02001876 struct ide_atapi_pc pc;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001877 int i, retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001878
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001879 debug_log(DBG_ERR, "Handling MTIOCTOP ioctl: mt_op=%d, mt_count=%d\n",
1880 mt_op, mt_count);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001881
Linus Torvalds1da177e2005-04-16 15:20:36 -07001882 switch (mt_op) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001883 case MTFSF:
1884 case MTFSFM:
1885 case MTBSF:
1886 case MTBSFM:
1887 if (!mt_count)
1888 return 0;
1889 return idetape_space_over_filemarks(drive, mt_op, mt_count);
1890 default:
1891 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001892 }
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001893
Linus Torvalds1da177e2005-04-16 15:20:36 -07001894 switch (mt_op) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001895 case MTWEOF:
1896 if (tape->write_prot)
1897 return -EACCES;
Borislav Petkovec0fdb02008-04-27 15:38:34 +02001898 ide_tape_discard_merge_buffer(drive, 1);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001899 for (i = 0; i < mt_count; i++) {
1900 retval = idetape_write_filemark(drive);
1901 if (retval)
1902 return retval;
1903 }
1904 return 0;
1905 case MTREW:
Borislav Petkovec0fdb02008-04-27 15:38:34 +02001906 ide_tape_discard_merge_buffer(drive, 0);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001907 if (idetape_rewind_tape(drive))
1908 return -EIO;
1909 return 0;
1910 case MTLOAD:
Borislav Petkovec0fdb02008-04-27 15:38:34 +02001911 ide_tape_discard_merge_buffer(drive, 0);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001912 idetape_create_load_unload_cmd(drive, &pc,
1913 IDETAPE_LU_LOAD_MASK);
1914 return idetape_queue_pc_tail(drive, &pc);
1915 case MTUNLOAD:
1916 case MTOFFL:
1917 /*
1918 * If door is locked, attempt to unlock before
1919 * attempting to eject.
1920 */
1921 if (tape->door_locked) {
Bartlomiej Zolnierkiewicz385a4b82008-10-10 22:39:37 +02001922 if (!ide_tape_set_media_lock(drive, 0))
1923 tape->door_locked = DOOR_UNLOCKED;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001924 }
Borislav Petkovec0fdb02008-04-27 15:38:34 +02001925 ide_tape_discard_merge_buffer(drive, 0);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001926 idetape_create_load_unload_cmd(drive, &pc,
1927 !IDETAPE_LU_LOAD_MASK);
1928 retval = idetape_queue_pc_tail(drive, &pc);
1929 if (!retval)
Borislav Petkovf2e3ab52008-07-23 19:56:01 +02001930 clear_bit(IDE_AFLAG_MEDIUM_PRESENT, &drive->atapi_flags);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001931 return retval;
1932 case MTNOP:
Borislav Petkovec0fdb02008-04-27 15:38:34 +02001933 ide_tape_discard_merge_buffer(drive, 0);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001934 return idetape_flush_tape_buffers(drive);
1935 case MTRETEN:
Borislav Petkovec0fdb02008-04-27 15:38:34 +02001936 ide_tape_discard_merge_buffer(drive, 0);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001937 idetape_create_load_unload_cmd(drive, &pc,
1938 IDETAPE_LU_RETENSION_MASK | IDETAPE_LU_LOAD_MASK);
1939 return idetape_queue_pc_tail(drive, &pc);
1940 case MTEOM:
1941 idetape_create_space_cmd(&pc, 0, IDETAPE_SPACE_TO_EOD);
1942 return idetape_queue_pc_tail(drive, &pc);
1943 case MTERASE:
1944 (void)idetape_rewind_tape(drive);
1945 idetape_create_erase_cmd(&pc);
1946 return idetape_queue_pc_tail(drive, &pc);
1947 case MTSETBLK:
1948 if (mt_count) {
1949 if (mt_count < tape->blk_size ||
1950 mt_count % tape->blk_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001951 return -EIO;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001952 tape->user_bs_factor = mt_count / tape->blk_size;
Borislav Petkovf2e3ab52008-07-23 19:56:01 +02001953 clear_bit(IDE_AFLAG_DETECT_BS, &drive->atapi_flags);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001954 } else
Borislav Petkovf2e3ab52008-07-23 19:56:01 +02001955 set_bit(IDE_AFLAG_DETECT_BS, &drive->atapi_flags);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001956 return 0;
1957 case MTSEEK:
Borislav Petkovec0fdb02008-04-27 15:38:34 +02001958 ide_tape_discard_merge_buffer(drive, 0);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001959 return idetape_position_tape(drive,
1960 mt_count * tape->user_bs_factor, tape->partition, 0);
1961 case MTSETPART:
Borislav Petkovec0fdb02008-04-27 15:38:34 +02001962 ide_tape_discard_merge_buffer(drive, 0);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001963 return idetape_position_tape(drive, 0, mt_count, 0);
1964 case MTFSR:
1965 case MTBSR:
1966 case MTLOCK:
Bartlomiej Zolnierkiewicz385a4b82008-10-10 22:39:37 +02001967 retval = ide_tape_set_media_lock(drive, 1);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001968 if (retval)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001969 return retval;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001970 tape->door_locked = DOOR_EXPLICITLY_LOCKED;
1971 return 0;
1972 case MTUNLOCK:
Bartlomiej Zolnierkiewicz385a4b82008-10-10 22:39:37 +02001973 retval = ide_tape_set_media_lock(drive, 0);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001974 if (retval)
1975 return retval;
1976 tape->door_locked = DOOR_UNLOCKED;
1977 return 0;
1978 default:
1979 printk(KERN_ERR "ide-tape: MTIO operation %d not supported\n",
1980 mt_op);
1981 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001982 }
1983}
1984
1985/*
Borislav Petkovd99c9da2008-02-02 19:56:51 +01001986 * Our character device ioctls. General mtio.h magnetic io commands are
1987 * supported here, and not in the corresponding block interface. Our own
1988 * ide-tape ioctls are supported on both interfaces.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001989 */
Borislav Petkovd99c9da2008-02-02 19:56:51 +01001990static int idetape_chrdev_ioctl(struct inode *inode, struct file *file,
1991 unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001992{
1993 struct ide_tape_obj *tape = ide_tape_f(file);
1994 ide_drive_t *drive = tape->drive;
1995 struct mtop mtop;
1996 struct mtget mtget;
1997 struct mtpos mtpos;
Borislav Petkov54bb2072008-02-06 02:57:52 +01001998 int block_offset = 0, position = tape->first_frame;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001999 void __user *argp = (void __user *)arg;
2000
Borislav Petkov8004a8c2008-02-06 02:57:51 +01002001 debug_log(DBG_CHRDEV, "Enter %s, cmd=%u\n", __func__, cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002002
Borislav Petkov54abf372008-02-06 02:57:52 +01002003 if (tape->chrdev_dir == IDETAPE_DIR_WRITE) {
Borislav Petkovd9df9372008-04-27 15:38:34 +02002004 ide_tape_flush_merge_buffer(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002005 idetape_flush_tape_buffers(drive);
2006 }
2007 if (cmd == MTIOCGET || cmd == MTIOCPOS) {
Borislav Petkov01a63aeb2008-04-27 15:38:34 +02002008 block_offset = tape->merge_bh_size /
Borislav Petkov54bb2072008-02-06 02:57:52 +01002009 (tape->blk_size * tape->user_bs_factor);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002010 position = idetape_read_position(drive);
2011 if (position < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002012 return -EIO;
2013 }
2014 switch (cmd) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002015 case MTIOCTOP:
2016 if (copy_from_user(&mtop, argp, sizeof(struct mtop)))
2017 return -EFAULT;
2018 return idetape_mtioctop(drive, mtop.mt_op, mtop.mt_count);
2019 case MTIOCGET:
2020 memset(&mtget, 0, sizeof(struct mtget));
2021 mtget.mt_type = MT_ISSCSI2;
2022 mtget.mt_blkno = position / tape->user_bs_factor - block_offset;
2023 mtget.mt_dsreg =
2024 ((tape->blk_size * tape->user_bs_factor)
2025 << MT_ST_BLKSIZE_SHIFT) & MT_ST_BLKSIZE_MASK;
Borislav Petkov54bb2072008-02-06 02:57:52 +01002026
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002027 if (tape->drv_write_prot)
2028 mtget.mt_gstat |= GMT_WR_PROT(0xffffffff);
2029
2030 if (copy_to_user(argp, &mtget, sizeof(struct mtget)))
2031 return -EFAULT;
2032 return 0;
2033 case MTIOCPOS:
2034 mtpos.mt_blkno = position / tape->user_bs_factor - block_offset;
2035 if (copy_to_user(argp, &mtpos, sizeof(struct mtpos)))
2036 return -EFAULT;
2037 return 0;
2038 default:
2039 if (tape->chrdev_dir == IDETAPE_DIR_READ)
Borislav Petkovec0fdb02008-04-27 15:38:34 +02002040 ide_tape_discard_merge_buffer(drive, 1);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002041 return idetape_blkdev_ioctl(drive, cmd, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002042 }
2043}
2044
Borislav Petkov3cffb9c2008-02-02 19:56:50 +01002045/*
2046 * Do a mode sense page 0 with block descriptor and if it succeeds set the tape
2047 * block size with the reported value.
2048 */
2049static void ide_tape_get_bsize_from_bdesc(ide_drive_t *drive)
2050{
2051 idetape_tape_t *tape = drive->driver_data;
Borislav Petkovd236d742008-04-18 00:46:27 +02002052 struct ide_atapi_pc pc;
Borislav Petkov3cffb9c2008-02-02 19:56:50 +01002053
2054 idetape_create_mode_sense_cmd(&pc, IDETAPE_BLOCK_DESCRIPTOR);
2055 if (idetape_queue_pc_tail(drive, &pc)) {
2056 printk(KERN_ERR "ide-tape: Can't get block descriptor\n");
Borislav Petkov54bb2072008-02-06 02:57:52 +01002057 if (tape->blk_size == 0) {
Borislav Petkov3cffb9c2008-02-02 19:56:50 +01002058 printk(KERN_WARNING "ide-tape: Cannot deal with zero "
2059 "block size, assuming 32k\n");
Borislav Petkov54bb2072008-02-06 02:57:52 +01002060 tape->blk_size = 32768;
Borislav Petkov3cffb9c2008-02-02 19:56:50 +01002061 }
2062 return;
2063 }
Borislav Petkovd236d742008-04-18 00:46:27 +02002064 tape->blk_size = (pc.buf[4 + 5] << 16) +
2065 (pc.buf[4 + 6] << 8) +
2066 pc.buf[4 + 7];
2067 tape->drv_write_prot = (pc.buf[2] & 0x80) >> 7;
Borislav Petkov3cffb9c2008-02-02 19:56:50 +01002068}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002069
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002070static int idetape_chrdev_open(struct inode *inode, struct file *filp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002071{
2072 unsigned int minor = iminor(inode), i = minor & ~0xc0;
2073 ide_drive_t *drive;
2074 idetape_tape_t *tape;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002075 int retval;
2076
Borislav Petkov8004a8c2008-02-06 02:57:51 +01002077 if (i >= MAX_HWIFS * MAX_DRIVES)
2078 return -ENXIO;
2079
Jonathan Corbet04f4ac92008-05-15 12:01:56 -06002080 lock_kernel();
Borislav Petkov8004a8c2008-02-06 02:57:51 +01002081 tape = ide_tape_chrdev_get(i);
Jonathan Corbet04f4ac92008-05-15 12:01:56 -06002082 if (!tape) {
2083 unlock_kernel();
Borislav Petkov8004a8c2008-02-06 02:57:51 +01002084 return -ENXIO;
Jonathan Corbet04f4ac92008-05-15 12:01:56 -06002085 }
Borislav Petkov8004a8c2008-02-06 02:57:51 +01002086
2087 debug_log(DBG_CHRDEV, "Enter %s\n", __func__);
2088
Linus Torvalds1da177e2005-04-16 15:20:36 -07002089 /*
2090 * We really want to do nonseekable_open(inode, filp); here, but some
2091 * versions of tar incorrectly call lseek on tapes and bail out if that
2092 * fails. So we disallow pread() and pwrite(), but permit lseeks.
2093 */
2094 filp->f_mode &= ~(FMODE_PREAD | FMODE_PWRITE);
2095
Linus Torvalds1da177e2005-04-16 15:20:36 -07002096 drive = tape->drive;
2097
2098 filp->private_data = tape;
2099
Borislav Petkovf2e3ab52008-07-23 19:56:01 +02002100 if (test_and_set_bit(IDE_AFLAG_BUSY, &drive->atapi_flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002101 retval = -EBUSY;
2102 goto out_put_tape;
2103 }
2104
2105 retval = idetape_wait_ready(drive, 60 * HZ);
2106 if (retval) {
Borislav Petkovf2e3ab52008-07-23 19:56:01 +02002107 clear_bit(IDE_AFLAG_BUSY, &drive->atapi_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002108 printk(KERN_ERR "ide-tape: %s: drive not ready\n", tape->name);
2109 goto out_put_tape;
2110 }
2111
2112 idetape_read_position(drive);
Borislav Petkovf2e3ab52008-07-23 19:56:01 +02002113 if (!test_bit(IDE_AFLAG_ADDRESS_VALID, &drive->atapi_flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002114 (void)idetape_rewind_tape(drive);
2115
Linus Torvalds1da177e2005-04-16 15:20:36 -07002116 /* Read block size and write protect status from drive. */
Borislav Petkov3cffb9c2008-02-02 19:56:50 +01002117 ide_tape_get_bsize_from_bdesc(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002118
2119 /* Set write protect flag if device is opened as read-only. */
2120 if ((filp->f_flags & O_ACCMODE) == O_RDONLY)
2121 tape->write_prot = 1;
2122 else
2123 tape->write_prot = tape->drv_write_prot;
2124
2125 /* Make sure drive isn't write protected if user wants to write. */
2126 if (tape->write_prot) {
2127 if ((filp->f_flags & O_ACCMODE) == O_WRONLY ||
2128 (filp->f_flags & O_ACCMODE) == O_RDWR) {
Borislav Petkovf2e3ab52008-07-23 19:56:01 +02002129 clear_bit(IDE_AFLAG_BUSY, &drive->atapi_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002130 retval = -EROFS;
2131 goto out_put_tape;
2132 }
2133 }
2134
Borislav Petkov3c98bf32008-02-06 02:57:53 +01002135 /* Lock the tape drive door so user can't eject. */
Borislav Petkov54abf372008-02-06 02:57:52 +01002136 if (tape->chrdev_dir == IDETAPE_DIR_NONE) {
Bartlomiej Zolnierkiewicz385a4b82008-10-10 22:39:37 +02002137 if (!ide_tape_set_media_lock(drive, 1)) {
2138 if (tape->door_locked != DOOR_EXPLICITLY_LOCKED)
2139 tape->door_locked = DOOR_LOCKED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002140 }
2141 }
Jonathan Corbet04f4ac92008-05-15 12:01:56 -06002142 unlock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002143 return 0;
2144
2145out_put_tape:
2146 ide_tape_put(tape);
Jonathan Corbet04f4ac92008-05-15 12:01:56 -06002147 unlock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002148 return retval;
2149}
2150
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002151static void idetape_write_release(ide_drive_t *drive, unsigned int minor)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002152{
2153 idetape_tape_t *tape = drive->driver_data;
2154
Borislav Petkovd9df9372008-04-27 15:38:34 +02002155 ide_tape_flush_merge_buffer(drive);
Borislav Petkov077e3bd2008-04-27 15:38:34 +02002156 tape->merge_bh = ide_tape_kmalloc_buffer(tape, 1, 0);
2157 if (tape->merge_bh != NULL) {
Borislav Petkov54bb2072008-02-06 02:57:52 +01002158 idetape_pad_zeros(drive, tape->blk_size *
2159 (tape->user_bs_factor - 1));
Borislav Petkov077e3bd2008-04-27 15:38:34 +02002160 ide_tape_kfree_buffer(tape);
2161 tape->merge_bh = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002162 }
2163 idetape_write_filemark(drive);
2164 idetape_flush_tape_buffers(drive);
2165 idetape_flush_tape_buffers(drive);
2166}
2167
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002168static int idetape_chrdev_release(struct inode *inode, struct file *filp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002169{
2170 struct ide_tape_obj *tape = ide_tape_f(filp);
2171 ide_drive_t *drive = tape->drive;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002172 unsigned int minor = iminor(inode);
2173
2174 lock_kernel();
2175 tape = drive->driver_data;
Borislav Petkov8004a8c2008-02-06 02:57:51 +01002176
2177 debug_log(DBG_CHRDEV, "Enter %s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002178
Borislav Petkov54abf372008-02-06 02:57:52 +01002179 if (tape->chrdev_dir == IDETAPE_DIR_WRITE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002180 idetape_write_release(drive, minor);
Borislav Petkov54abf372008-02-06 02:57:52 +01002181 if (tape->chrdev_dir == IDETAPE_DIR_READ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002182 if (minor < 128)
Borislav Petkovec0fdb02008-04-27 15:38:34 +02002183 ide_tape_discard_merge_buffer(drive, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002184 }
Borislav Petkovf64eee72008-04-27 15:38:25 +02002185
Borislav Petkovf2e3ab52008-07-23 19:56:01 +02002186 if (minor < 128 && test_bit(IDE_AFLAG_MEDIUM_PRESENT, &drive->atapi_flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002187 (void) idetape_rewind_tape(drive);
Borislav Petkov54abf372008-02-06 02:57:52 +01002188 if (tape->chrdev_dir == IDETAPE_DIR_NONE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002189 if (tape->door_locked == DOOR_LOCKED) {
Bartlomiej Zolnierkiewicz385a4b82008-10-10 22:39:37 +02002190 if (!ide_tape_set_media_lock(drive, 0))
2191 tape->door_locked = DOOR_UNLOCKED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002192 }
2193 }
Borislav Petkovf2e3ab52008-07-23 19:56:01 +02002194 clear_bit(IDE_AFLAG_BUSY, &drive->atapi_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002195 ide_tape_put(tape);
2196 unlock_kernel();
2197 return 0;
2198}
2199
Borislav Petkov6d29c8f2008-02-02 19:56:49 +01002200static void idetape_get_inquiry_results(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002201{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002202 idetape_tape_t *tape = drive->driver_data;
Borislav Petkovd236d742008-04-18 00:46:27 +02002203 struct ide_atapi_pc pc;
Borislav Petkov801bd322008-09-27 19:32:17 +02002204 char fw_rev[4], vendor_id[8], product_id[16];
Borislav Petkov6d29c8f2008-02-02 19:56:49 +01002205
Linus Torvalds1da177e2005-04-16 15:20:36 -07002206 idetape_create_inquiry_cmd(&pc);
2207 if (idetape_queue_pc_tail(drive, &pc)) {
Borislav Petkov6d29c8f2008-02-02 19:56:49 +01002208 printk(KERN_ERR "ide-tape: %s: can't get INQUIRY results\n",
2209 tape->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002210 return;
2211 }
Borislav Petkovd236d742008-04-18 00:46:27 +02002212 memcpy(vendor_id, &pc.buf[8], 8);
2213 memcpy(product_id, &pc.buf[16], 16);
2214 memcpy(fw_rev, &pc.buf[32], 4);
Borislav Petkov6d29c8f2008-02-02 19:56:49 +01002215
Borislav Petkov801bd322008-09-27 19:32:17 +02002216 ide_fixstring(vendor_id, 8, 0);
2217 ide_fixstring(product_id, 16, 0);
2218 ide_fixstring(fw_rev, 4, 0);
Borislav Petkov41f81d542008-02-06 02:57:52 +01002219
Borislav Petkov801bd322008-09-27 19:32:17 +02002220 printk(KERN_INFO "ide-tape: %s <-> %s: %.8s %.16s rev %.4s\n",
Borislav Petkov41f81d542008-02-06 02:57:52 +01002221 drive->name, tape->name, vendor_id, product_id, fw_rev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002222}
2223
2224/*
Borislav Petkovb6422012008-02-02 19:56:49 +01002225 * Ask the tape about its various parameters. In particular, we will adjust our
2226 * data transfer buffer size to the recommended value as returned by the tape.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002227 */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002228static void idetape_get_mode_sense_results(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002229{
2230 idetape_tape_t *tape = drive->driver_data;
Borislav Petkovd236d742008-04-18 00:46:27 +02002231 struct ide_atapi_pc pc;
Borislav Petkovb6422012008-02-02 19:56:49 +01002232 u8 *caps;
2233 u8 speed, max_speed;
Borislav Petkov47314fa2008-02-02 19:56:48 +01002234
Linus Torvalds1da177e2005-04-16 15:20:36 -07002235 idetape_create_mode_sense_cmd(&pc, IDETAPE_CAPABILITIES_PAGE);
2236 if (idetape_queue_pc_tail(drive, &pc)) {
Borislav Petkovb6422012008-02-02 19:56:49 +01002237 printk(KERN_ERR "ide-tape: Can't get tape parameters - assuming"
2238 " some default values\n");
Borislav Petkov54bb2072008-02-06 02:57:52 +01002239 tape->blk_size = 512;
Borislav Petkovb6422012008-02-02 19:56:49 +01002240 put_unaligned(52, (u16 *)&tape->caps[12]);
2241 put_unaligned(540, (u16 *)&tape->caps[14]);
2242 put_unaligned(6*52, (u16 *)&tape->caps[16]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002243 return;
2244 }
Borislav Petkovd236d742008-04-18 00:46:27 +02002245 caps = pc.buf + 4 + pc.buf[3];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002246
Borislav Petkovb6422012008-02-02 19:56:49 +01002247 /* convert to host order and save for later use */
Harvey Harrisoncd740ab2008-07-24 22:53:33 +02002248 speed = be16_to_cpup((__be16 *)&caps[14]);
2249 max_speed = be16_to_cpup((__be16 *)&caps[8]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002250
Harvey Harrisoncd740ab2008-07-24 22:53:33 +02002251 *(u16 *)&caps[8] = max_speed;
2252 *(u16 *)&caps[12] = be16_to_cpup((__be16 *)&caps[12]);
2253 *(u16 *)&caps[14] = speed;
2254 *(u16 *)&caps[16] = be16_to_cpup((__be16 *)&caps[16]);
Borislav Petkovb6422012008-02-02 19:56:49 +01002255
2256 if (!speed) {
2257 printk(KERN_INFO "ide-tape: %s: invalid tape speed "
2258 "(assuming 650KB/sec)\n", drive->name);
Harvey Harrisoncd740ab2008-07-24 22:53:33 +02002259 *(u16 *)&caps[14] = 650;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002260 }
Borislav Petkovb6422012008-02-02 19:56:49 +01002261 if (!max_speed) {
2262 printk(KERN_INFO "ide-tape: %s: invalid max_speed "
2263 "(assuming 650KB/sec)\n", drive->name);
Harvey Harrisoncd740ab2008-07-24 22:53:33 +02002264 *(u16 *)&caps[8] = 650;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002265 }
2266
Borislav Petkovb6422012008-02-02 19:56:49 +01002267 memcpy(&tape->caps, caps, 20);
2268 if (caps[7] & 0x02)
Borislav Petkov54bb2072008-02-06 02:57:52 +01002269 tape->blk_size = 512;
Borislav Petkovb6422012008-02-02 19:56:49 +01002270 else if (caps[7] & 0x04)
Borislav Petkov54bb2072008-02-06 02:57:52 +01002271 tape->blk_size = 1024;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002272}
2273
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +02002274#ifdef CONFIG_IDE_PROC_FS
Bartlomiej Zolnierkiewicz8185d5a2008-10-10 22:39:28 +02002275#define ide_tape_devset_get(name, field) \
2276static int get_##name(ide_drive_t *drive) \
2277{ \
2278 idetape_tape_t *tape = drive->driver_data; \
2279 return tape->field; \
Linus Torvalds1da177e2005-04-16 15:20:36 -07002280}
Bartlomiej Zolnierkiewicz8185d5a2008-10-10 22:39:28 +02002281
2282#define ide_tape_devset_set(name, field) \
2283static int set_##name(ide_drive_t *drive, int arg) \
2284{ \
2285 idetape_tape_t *tape = drive->driver_data; \
2286 tape->field = arg; \
2287 return 0; \
2288}
2289
2290#define ide_tape_devset_rw(_name, _min, _max, _field, _mulf, _divf) \
2291ide_tape_devset_get(_name, _field) \
2292ide_tape_devset_set(_name, _field) \
2293__IDE_DEVSET(_name, S_RW, _min, _max, get_##_name, set_##_name, _mulf, _divf)
2294
2295#define ide_tape_devset_r(_name, _min, _max, _field, _mulf, _divf) \
2296ide_tape_devset_get(_name, _field) \
2297__IDE_DEVSET(_name, S_READ, _min, _max, get_##_name, NULL, _mulf, _divf)
2298
2299static int mulf_tdsc(ide_drive_t *drive) { return 1000; }
2300static int divf_tdsc(ide_drive_t *drive) { return HZ; }
2301static int divf_buffer(ide_drive_t *drive) { return 2; }
2302static int divf_buffer_size(ide_drive_t *drive) { return 1024; }
2303
2304ide_devset_rw(dsc_overlap, 0, 1, dsc_overlap);
2305
2306ide_tape_devset_rw(debug_mask, 0, 0xffff, debug_mask, NULL, NULL);
2307ide_tape_devset_rw(tdsc, IDETAPE_DSC_RW_MIN, IDETAPE_DSC_RW_MAX,
2308 best_dsc_rw_freq, mulf_tdsc, divf_tdsc);
2309
2310ide_tape_devset_r(avg_speed, 0, 0xffff, avg_speed, NULL, NULL);
2311ide_tape_devset_r(speed, 0, 0xffff, caps[14], NULL, NULL);
2312ide_tape_devset_r(buffer, 0, 0xffff, caps[16], NULL, divf_buffer);
2313ide_tape_devset_r(buffer_size, 0, 0xffff, buffer_size, NULL, divf_buffer_size);
2314
2315static const struct ide_devset *idetape_settings[] = {
2316 &ide_devset_avg_speed,
2317 &ide_devset_buffer,
2318 &ide_devset_buffer_size,
2319 &ide_devset_debug_mask,
2320 &ide_devset_dsc_overlap,
2321 &ide_devset_speed,
2322 &ide_devset_tdsc,
2323 NULL
2324};
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +02002325#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002326
2327/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +01002328 * The function below is called to:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002329 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +01002330 * 1. Initialize our various state variables.
2331 * 2. Ask the tape for its capabilities.
2332 * 3. Allocate a buffer which will be used for data transfer. The buffer size
2333 * is chosen based on the recommendation which we received in step 2.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002334 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +01002335 * Note that at this point ide.c already assigned us an irq, so that we can
2336 * queue requests here and wait for their completion.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002337 */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002338static void idetape_setup(ide_drive_t *drive, idetape_tape_t *tape, int minor)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002339{
Borislav Petkov83042b22008-04-27 15:38:27 +02002340 unsigned long t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002341 int speed;
Borislav Petkovf73850a2008-04-27 15:38:33 +02002342 int buffer_size;
Borislav Petkov71071b82008-02-06 02:57:53 +01002343 u8 gcw[2];
Borislav Petkovb6422012008-02-02 19:56:49 +01002344 u16 *ctl = (u16 *)&tape->caps[12];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002345
Borislav Petkov776bb022008-07-23 19:55:59 +02002346 drive->pc_callback = ide_tape_callback;
2347
Borislav Petkov54bb2072008-02-06 02:57:52 +01002348 spin_lock_init(&tape->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002349 drive->dsc_overlap = 1;
Bartlomiej Zolnierkiewicz4166c192008-02-01 23:09:30 +01002350 if (drive->hwif->host_flags & IDE_HFLAG_NO_DSC) {
2351 printk(KERN_INFO "ide-tape: %s: disabling DSC overlap\n",
2352 tape->name);
2353 drive->dsc_overlap = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002354 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002355 /* Seagate Travan drives do not support DSC overlap. */
Bartlomiej Zolnierkiewicz4dde4492008-10-10 22:39:19 +02002356 if (strstr((char *)&drive->id[ATA_ID_PROD], "Seagate STT3401"))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002357 drive->dsc_overlap = 0;
2358 tape->minor = minor;
2359 tape->name[0] = 'h';
2360 tape->name[1] = 't';
2361 tape->name[2] = '0' + minor;
Borislav Petkov54abf372008-02-06 02:57:52 +01002362 tape->chrdev_dir = IDETAPE_DIR_NONE;
Bartlomiej Zolnierkiewicz4dde4492008-10-10 22:39:19 +02002363
2364 *((u16 *)&gcw) = drive->id[ATA_ID_CONFIG];
Borislav Petkov71071b82008-02-06 02:57:53 +01002365
2366 /* Command packet DRQ type */
2367 if (((gcw[0] & 0x60) >> 5) == 1)
Borislav Petkovf2e3ab52008-07-23 19:56:01 +02002368 set_bit(IDE_AFLAG_DRQ_INTERRUPT, &drive->atapi_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002369
Linus Torvalds1da177e2005-04-16 15:20:36 -07002370 idetape_get_inquiry_results(drive);
2371 idetape_get_mode_sense_results(drive);
Borislav Petkov3cffb9c2008-02-02 19:56:50 +01002372 ide_tape_get_bsize_from_bdesc(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002373 tape->user_bs_factor = 1;
Borislav Petkovf73850a2008-04-27 15:38:33 +02002374 tape->buffer_size = *ctl * tape->blk_size;
2375 while (tape->buffer_size > 0xffff) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002376 printk(KERN_NOTICE "ide-tape: decreasing stage size\n");
Borislav Petkovb6422012008-02-02 19:56:49 +01002377 *ctl /= 2;
Borislav Petkovf73850a2008-04-27 15:38:33 +02002378 tape->buffer_size = *ctl * tape->blk_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002379 }
Borislav Petkovf73850a2008-04-27 15:38:33 +02002380 buffer_size = tape->buffer_size;
Borislav Petkova997a432008-04-27 15:38:33 +02002381 tape->pages_per_buffer = buffer_size / PAGE_SIZE;
Borislav Petkovf73850a2008-04-27 15:38:33 +02002382 if (buffer_size % PAGE_SIZE) {
Borislav Petkova997a432008-04-27 15:38:33 +02002383 tape->pages_per_buffer++;
Borislav Petkovf73850a2008-04-27 15:38:33 +02002384 tape->excess_bh_size = PAGE_SIZE - buffer_size % PAGE_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002385 }
2386
Borislav Petkov83042b22008-04-27 15:38:27 +02002387 /* select the "best" DSC read/write polling freq */
Borislav Petkovb6422012008-02-02 19:56:49 +01002388 speed = max(*(u16 *)&tape->caps[14], *(u16 *)&tape->caps[8]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002389
Borislav Petkovf73850a2008-04-27 15:38:33 +02002390 t = (IDETAPE_FIFO_THRESHOLD * tape->buffer_size * HZ) / (speed * 1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002391
2392 /*
Borislav Petkov3c98bf32008-02-06 02:57:53 +01002393 * Ensure that the number we got makes sense; limit it within
2394 * IDETAPE_DSC_RW_MIN and IDETAPE_DSC_RW_MAX.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002395 */
Harvey Harrisona792bd52008-07-15 21:21:41 +02002396 tape->best_dsc_rw_freq = clamp_t(unsigned long, t, IDETAPE_DSC_RW_MIN,
2397 IDETAPE_DSC_RW_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002398 printk(KERN_INFO "ide-tape: %s <-> %s: %dKBps, %d*%dkB buffer, "
Borislav Petkov83042b22008-04-27 15:38:27 +02002399 "%lums tDSC%s\n",
Borislav Petkovb6422012008-02-02 19:56:49 +01002400 drive->name, tape->name, *(u16 *)&tape->caps[14],
Borislav Petkovf73850a2008-04-27 15:38:33 +02002401 (*(u16 *)&tape->caps[16] * 512) / tape->buffer_size,
2402 tape->buffer_size / 1024,
Borislav Petkov54bb2072008-02-06 02:57:52 +01002403 tape->best_dsc_rw_freq * 1000 / HZ,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002404 drive->using_dma ? ", DMA":"");
2405
Bartlomiej Zolnierkiewicz1e874f42008-10-10 22:39:27 +02002406 ide_proc_register_driver(drive, tape->driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002407}
2408
Russell King4031bbe2006-01-06 11:41:00 +00002409static void ide_tape_remove(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002410{
2411 idetape_tape_t *tape = drive->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002412
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +02002413 ide_proc_unregister_driver(drive, tape->driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002414
2415 ide_unregister_region(tape->disk);
2416
2417 ide_tape_put(tape);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002418}
2419
2420static void ide_tape_release(struct kref *kref)
2421{
2422 struct ide_tape_obj *tape = to_ide_tape(kref);
2423 ide_drive_t *drive = tape->drive;
2424 struct gendisk *g = tape->disk;
2425
Borislav Petkov01a63aeb2008-04-27 15:38:34 +02002426 BUG_ON(tape->merge_bh_size);
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02002427
Linus Torvalds1da177e2005-04-16 15:20:36 -07002428 drive->dsc_overlap = 0;
2429 drive->driver_data = NULL;
Tony Jonesdbc12722007-09-25 02:03:03 +02002430 device_destroy(idetape_sysfs_class, MKDEV(IDETAPE_MAJOR, tape->minor));
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002431 device_destroy(idetape_sysfs_class,
2432 MKDEV(IDETAPE_MAJOR, tape->minor + 128));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002433 idetape_devs[tape->minor] = NULL;
2434 g->private_data = NULL;
2435 put_disk(g);
2436 kfree(tape);
2437}
2438
Bartlomiej Zolnierkiewiczecfd80e2007-05-10 00:01:09 +02002439#ifdef CONFIG_IDE_PROC_FS
Linus Torvalds1da177e2005-04-16 15:20:36 -07002440static int proc_idetape_read_name
2441 (char *page, char **start, off_t off, int count, int *eof, void *data)
2442{
2443 ide_drive_t *drive = (ide_drive_t *) data;
2444 idetape_tape_t *tape = drive->driver_data;
2445 char *out = page;
2446 int len;
2447
2448 len = sprintf(out, "%s\n", tape->name);
2449 PROC_IDE_READ_RETURN(page, start, off, count, eof, len);
2450}
2451
2452static ide_proc_entry_t idetape_proc[] = {
2453 { "capacity", S_IFREG|S_IRUGO, proc_ide_read_capacity, NULL },
2454 { "name", S_IFREG|S_IRUGO, proc_idetape_read_name, NULL },
2455 { NULL, 0, NULL, NULL }
2456};
Linus Torvalds1da177e2005-04-16 15:20:36 -07002457#endif
2458
Russell King4031bbe2006-01-06 11:41:00 +00002459static int ide_tape_probe(ide_drive_t *);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002460
Linus Torvalds1da177e2005-04-16 15:20:36 -07002461static ide_driver_t idetape_driver = {
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02002462 .gen_driver = {
Laurent Riffard4ef3b8f2005-11-18 22:15:40 +01002463 .owner = THIS_MODULE,
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02002464 .name = "ide-tape",
2465 .bus = &ide_bus_type,
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02002466 },
Russell King4031bbe2006-01-06 11:41:00 +00002467 .probe = ide_tape_probe,
2468 .remove = ide_tape_remove,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002469 .version = IDETAPE_VERSION,
2470 .media = ide_tape,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002471 .do_request = idetape_do_request,
2472 .end_request = idetape_end_request,
2473 .error = __ide_error,
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +02002474#ifdef CONFIG_IDE_PROC_FS
Linus Torvalds1da177e2005-04-16 15:20:36 -07002475 .proc = idetape_proc,
Bartlomiej Zolnierkiewicz8185d5a2008-10-10 22:39:28 +02002476 .settings = idetape_settings,
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +02002477#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002478};
2479
Borislav Petkov3c98bf32008-02-06 02:57:53 +01002480/* Our character device supporting functions, passed to register_chrdev. */
Arjan van de Ven2b8693c2007-02-12 00:55:32 -08002481static const struct file_operations idetape_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002482 .owner = THIS_MODULE,
2483 .read = idetape_chrdev_read,
2484 .write = idetape_chrdev_write,
2485 .ioctl = idetape_chrdev_ioctl,
2486 .open = idetape_chrdev_open,
2487 .release = idetape_chrdev_release,
2488};
2489
2490static int idetape_open(struct inode *inode, struct file *filp)
2491{
2492 struct gendisk *disk = inode->i_bdev->bd_disk;
2493 struct ide_tape_obj *tape;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002494
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002495 tape = ide_tape_get(disk);
2496 if (!tape)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002497 return -ENXIO;
2498
Linus Torvalds1da177e2005-04-16 15:20:36 -07002499 return 0;
2500}
2501
2502static int idetape_release(struct inode *inode, struct file *filp)
2503{
2504 struct gendisk *disk = inode->i_bdev->bd_disk;
2505 struct ide_tape_obj *tape = ide_tape_g(disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002506
2507 ide_tape_put(tape);
2508
2509 return 0;
2510}
2511
2512static int idetape_ioctl(struct inode *inode, struct file *file,
2513 unsigned int cmd, unsigned long arg)
2514{
2515 struct block_device *bdev = inode->i_bdev;
2516 struct ide_tape_obj *tape = ide_tape_g(bdev->bd_disk);
2517 ide_drive_t *drive = tape->drive;
2518 int err = generic_ide_ioctl(drive, file, bdev, cmd, arg);
2519 if (err == -EINVAL)
2520 err = idetape_blkdev_ioctl(drive, cmd, arg);
2521 return err;
2522}
2523
2524static struct block_device_operations idetape_block_ops = {
2525 .owner = THIS_MODULE,
2526 .open = idetape_open,
2527 .release = idetape_release,
2528 .ioctl = idetape_ioctl,
2529};
2530
Russell King4031bbe2006-01-06 11:41:00 +00002531static int ide_tape_probe(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002532{
2533 idetape_tape_t *tape;
2534 struct gendisk *g;
2535 int minor;
2536
2537 if (!strstr("ide-tape", drive->driver_req))
2538 goto failed;
Bartlomiej Zolnierkiewicz2a924662008-10-10 22:39:24 +02002539
Linus Torvalds1da177e2005-04-16 15:20:36 -07002540 if (drive->media != ide_tape)
2541 goto failed;
Bartlomiej Zolnierkiewicz2a924662008-10-10 22:39:24 +02002542
Bartlomiej Zolnierkiewicz51509ee2008-10-10 22:39:34 +02002543 if (drive->id_read == 1 && !ide_check_atapi_device(drive, DRV_NAME)) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002544 printk(KERN_ERR "ide-tape: %s: not supported by this version of"
2545 " the driver\n", drive->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002546 goto failed;
2547 }
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002548 tape = kzalloc(sizeof(idetape_tape_t), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002549 if (tape == NULL) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002550 printk(KERN_ERR "ide-tape: %s: Can't allocate a tape struct\n",
2551 drive->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002552 goto failed;
2553 }
2554
2555 g = alloc_disk(1 << PARTN_BITS);
2556 if (!g)
2557 goto out_free_tape;
2558
2559 ide_init_disk(g, drive);
2560
Linus Torvalds1da177e2005-04-16 15:20:36 -07002561 kref_init(&tape->kref);
2562
2563 tape->drive = drive;
2564 tape->driver = &idetape_driver;
2565 tape->disk = g;
2566
2567 g->private_data = &tape->driver;
2568
2569 drive->driver_data = tape;
2570
Arjan van de Vencf8b8972006-03-23 03:00:45 -08002571 mutex_lock(&idetape_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002572 for (minor = 0; idetape_devs[minor]; minor++)
2573 ;
2574 idetape_devs[minor] = tape;
Arjan van de Vencf8b8972006-03-23 03:00:45 -08002575 mutex_unlock(&idetape_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002576
2577 idetape_setup(drive, tape, minor);
2578
Greg Kroah-Hartman6ecaaf92008-05-21 12:52:33 -07002579 device_create_drvdata(idetape_sysfs_class, &drive->gendev,
2580 MKDEV(IDETAPE_MAJOR, minor), NULL,
2581 "%s", tape->name);
2582 device_create_drvdata(idetape_sysfs_class, &drive->gendev,
2583 MKDEV(IDETAPE_MAJOR, minor + 128), NULL,
2584 "n%s", tape->name);
Will Dysond5dee802005-09-16 02:55:07 -07002585
Linus Torvalds1da177e2005-04-16 15:20:36 -07002586 g->fops = &idetape_block_ops;
2587 ide_register_region(g);
2588
2589 return 0;
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02002590
Linus Torvalds1da177e2005-04-16 15:20:36 -07002591out_free_tape:
2592 kfree(tape);
2593failed:
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02002594 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002595}
2596
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002597static void __exit idetape_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002598{
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02002599 driver_unregister(&idetape_driver.gen_driver);
Will Dysond5dee802005-09-16 02:55:07 -07002600 class_destroy(idetape_sysfs_class);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002601 unregister_chrdev(IDETAPE_MAJOR, "ht");
2602}
2603
Bartlomiej Zolnierkiewicz17514e82005-11-19 22:24:35 +01002604static int __init idetape_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002605{
Will Dysond5dee802005-09-16 02:55:07 -07002606 int error = 1;
2607 idetape_sysfs_class = class_create(THIS_MODULE, "ide_tape");
2608 if (IS_ERR(idetape_sysfs_class)) {
2609 idetape_sysfs_class = NULL;
2610 printk(KERN_ERR "Unable to create sysfs class for ide tapes\n");
2611 error = -EBUSY;
2612 goto out;
2613 }
2614
Linus Torvalds1da177e2005-04-16 15:20:36 -07002615 if (register_chrdev(IDETAPE_MAJOR, "ht", &idetape_fops)) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002616 printk(KERN_ERR "ide-tape: Failed to register chrdev"
2617 " interface\n");
Will Dysond5dee802005-09-16 02:55:07 -07002618 error = -EBUSY;
2619 goto out_free_class;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002620 }
Will Dysond5dee802005-09-16 02:55:07 -07002621
2622 error = driver_register(&idetape_driver.gen_driver);
2623 if (error)
2624 goto out_free_driver;
2625
2626 return 0;
2627
2628out_free_driver:
2629 driver_unregister(&idetape_driver.gen_driver);
2630out_free_class:
2631 class_destroy(idetape_sysfs_class);
2632out:
2633 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002634}
2635
Kay Sievers263756e2005-12-12 18:03:44 +01002636MODULE_ALIAS("ide:*m-tape*");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002637module_init(idetape_init);
2638module_exit(idetape_exit);
2639MODULE_ALIAS_CHARDEV_MAJOR(IDETAPE_MAJOR);
Borislav Petkov9c145762008-02-06 02:57:54 +01002640MODULE_DESCRIPTION("ATAPI Streaming TAPE Driver");
2641MODULE_LICENSE("GPL");