blob: 52c64dd1292a4d7c9df610a3e9fc1e6080e5dda2 [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
Borislav Petkovdfe79932008-02-06 02:57:55 +010018#define IDETAPE_VERSION "1.20"
Linus Torvalds1da177e2005-04-16 15:20:36 -070019
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include <linux/module.h>
21#include <linux/types.h>
22#include <linux/string.h>
23#include <linux/kernel.h>
24#include <linux/delay.h>
25#include <linux/timer.h>
26#include <linux/mm.h>
27#include <linux/interrupt.h>
Marcelo Feitoza Parisi9bae1ff2006-03-28 01:56:46 -080028#include <linux/jiffies.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#include <linux/major.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include <linux/errno.h>
31#include <linux/genhd.h>
32#include <linux/slab.h>
33#include <linux/pci.h>
34#include <linux/ide.h>
35#include <linux/smp_lock.h>
36#include <linux/completion.h>
37#include <linux/bitops.h>
Arjan van de Vencf8b8972006-03-23 03:00:45 -080038#include <linux/mutex.h>
Borislav Petkov90699ce2008-02-02 19:56:50 +010039#include <scsi/scsi.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070040
41#include <asm/byteorder.h>
Borislav Petkovc837cfa2008-02-06 02:57:54 +010042#include <linux/irq.h>
43#include <linux/uaccess.h>
44#include <linux/io.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070045#include <asm/unaligned.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070046#include <linux/mtio.h>
47
Borislav Petkov8004a8c2008-02-06 02:57:51 +010048enum {
49 /* output errors only */
50 DBG_ERR = (1 << 0),
51 /* output all sense key/asc */
52 DBG_SENSE = (1 << 1),
53 /* info regarding all chrdev-related procedures */
54 DBG_CHRDEV = (1 << 2),
55 /* all remaining procedures */
56 DBG_PROCS = (1 << 3),
57 /* buffer alloc info (pc_stack & rq_stack) */
58 DBG_PCRQ_STACK = (1 << 4),
59};
60
61/* define to see debug info */
62#define IDETAPE_DEBUG_LOG 0
63
64#if IDETAPE_DEBUG_LOG
65#define debug_log(lvl, fmt, args...) \
66{ \
67 if (tape->debug_mask & lvl) \
68 printk(KERN_INFO "ide-tape: " fmt, ## args); \
69}
70#else
71#define debug_log(lvl, fmt, args...) do {} while (0)
72#endif
73
Linus Torvalds1da177e2005-04-16 15:20:36 -070074/**************************** Tunable parameters *****************************/
Linus Torvalds1da177e2005-04-16 15:20:36 -070075/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +010076 * After each failed packet command we issue a request sense command and retry
77 * the packet command IDETAPE_MAX_PC_RETRIES times.
Linus Torvalds1da177e2005-04-16 15:20:36 -070078 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +010079 * Setting IDETAPE_MAX_PC_RETRIES to 0 will disable retries.
Linus Torvalds1da177e2005-04-16 15:20:36 -070080 */
81#define IDETAPE_MAX_PC_RETRIES 3
82
83/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +010084 * With each packet command, we allocate a buffer of IDETAPE_PC_BUFFER_SIZE
85 * bytes. This is used for several packet commands (Not for READ/WRITE commands)
Linus Torvalds1da177e2005-04-16 15:20:36 -070086 */
87#define IDETAPE_PC_BUFFER_SIZE 256
88
89/*
90 * In various places in the driver, we need to allocate storage
91 * for packet commands and requests, which will remain valid while
92 * we leave the driver to wait for an interrupt or a timeout event.
93 */
94#define IDETAPE_PC_STACK (10 + IDETAPE_MAX_PC_RETRIES)
95
96/*
97 * Some drives (for example, Seagate STT3401A Travan) require a very long
98 * timeout, because they don't return an interrupt or clear their busy bit
99 * until after the command completes (even retension commands).
100 */
101#define IDETAPE_WAIT_CMD (900*HZ)
102
103/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100104 * The following parameter is used to select the point in the internal tape fifo
105 * in which we will start to refill the buffer. Decreasing the following
106 * parameter will improve the system's latency and interactive response, while
107 * using a high value might improve system throughput.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 */
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100109#define IDETAPE_FIFO_THRESHOLD 2
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110
111/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100112 * DSC polling parameters.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100114 * Polling for DSC (a single bit in the status register) is a very important
115 * function in ide-tape. There are two cases in which we poll for DSC:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100117 * 1. Before a read/write packet command, to ensure that we can transfer data
118 * from/to the tape's data buffers, without causing an actual media access.
119 * In case the tape is not ready yet, we take out our request from the device
120 * request queue, so that ide.c could service requests from the other device
121 * on the same interface in the meantime.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100123 * 2. After the successful initialization of a "media access packet command",
124 * which is a command that can take a long time to complete (the interval can
125 * range from several seconds to even an hour). Again, we postpone our request
126 * in the middle to free the bus for the other device. The polling frequency
127 * here should be lower than the read/write frequency since those media access
128 * commands are slow. We start from a "fast" frequency - IDETAPE_DSC_MA_FAST
129 * (1 second), and if we don't receive DSC after IDETAPE_DSC_MA_THRESHOLD
130 * (5 min), we switch it to a lower frequency - IDETAPE_DSC_MA_SLOW (1 min).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100132 * We also set a timeout for the timer, in case something goes wrong. The
133 * timeout should be longer then the maximum execution time of a tape operation.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 */
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100135
136/* DSC timings. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137#define IDETAPE_DSC_RW_MIN 5*HZ/100 /* 50 msec */
138#define IDETAPE_DSC_RW_MAX 40*HZ/100 /* 400 msec */
139#define IDETAPE_DSC_RW_TIMEOUT 2*60*HZ /* 2 minutes */
140#define IDETAPE_DSC_MA_FAST 2*HZ /* 2 seconds */
141#define IDETAPE_DSC_MA_THRESHOLD 5*60*HZ /* 5 minutes */
142#define IDETAPE_DSC_MA_SLOW 30*HZ /* 30 seconds */
143#define IDETAPE_DSC_MA_TIMEOUT 2*60*60*HZ /* 2 hours */
144
145/*************************** End of tunable parameters ***********************/
146
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100147/* Read/Write error simulation */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148#define SIMULATE_ERRORS 0
149
Borislav Petkov54abf372008-02-06 02:57:52 +0100150/* tape directions */
151enum {
152 IDETAPE_DIR_NONE = (1 << 0),
153 IDETAPE_DIR_READ = (1 << 1),
154 IDETAPE_DIR_WRITE = (1 << 2),
155};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156
157struct idetape_bh {
Stephen Rothwellab057962007-08-01 23:46:44 +0200158 u32 b_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 atomic_t b_count;
160 struct idetape_bh *b_reqnext;
161 char *b_data;
162};
163
Borislav Petkov03056b92008-04-18 00:46:26 +0200164/* Tape door status */
165#define DOOR_UNLOCKED 0
166#define DOOR_LOCKED 1
167#define DOOR_EXPLICITLY_LOCKED 2
168
169/* Some defines for the SPACE command */
170#define IDETAPE_SPACE_OVER_FILEMARK 1
171#define IDETAPE_SPACE_TO_EOD 3
172
173/* Some defines for the LOAD UNLOAD command */
174#define IDETAPE_LU_LOAD_MASK 1
175#define IDETAPE_LU_RETENSION_MASK 2
176#define IDETAPE_LU_EOT_MASK 4
177
178/*
179 * Special requests for our block device strategy routine.
180 *
181 * In order to service a character device command, we add special requests to
182 * the tail of our block device request queue and wait for their completion.
183 */
184
185enum {
186 REQ_IDETAPE_PC1 = (1 << 0), /* packet command (first stage) */
187 REQ_IDETAPE_PC2 = (1 << 1), /* packet command (second stage) */
188 REQ_IDETAPE_READ = (1 << 2),
189 REQ_IDETAPE_WRITE = (1 << 3),
190};
191
192/* Error codes returned in rq->errors to the higher part of the driver. */
193#define IDETAPE_ERROR_GENERAL 101
194#define IDETAPE_ERROR_FILEMARK 102
195#define IDETAPE_ERROR_EOD 103
196
197/* Structures related to the SELECT SENSE / MODE SENSE packet commands. */
198#define IDETAPE_BLOCK_DESCRIPTOR 0
199#define IDETAPE_CAPABILITIES_PAGE 0x2a
200
Borislav Petkov346331f2008-04-18 00:46:26 +0200201/* Tape flag bits values. */
202enum {
203 IDETAPE_FLAG_IGNORE_DSC = (1 << 0),
204 /* 0 When the tape position is unknown */
205 IDETAPE_FLAG_ADDRESS_VALID = (1 << 1),
206 /* Device already opened */
Borislav Petkov42d54682008-04-27 15:38:27 +0200207 IDETAPE_FLAG_BUSY = (1 << 2),
Borislav Petkov346331f2008-04-18 00:46:26 +0200208 /* Attempt to auto-detect the current user block size */
Borislav Petkov42d54682008-04-27 15:38:27 +0200209 IDETAPE_FLAG_DETECT_BS = (1 << 3),
Borislav Petkov346331f2008-04-18 00:46:26 +0200210 /* Currently on a filemark */
Borislav Petkov42d54682008-04-27 15:38:27 +0200211 IDETAPE_FLAG_FILEMARK = (1 << 4),
Borislav Petkov346331f2008-04-18 00:46:26 +0200212 /* DRQ interrupt device */
Borislav Petkov42d54682008-04-27 15:38:27 +0200213 IDETAPE_FLAG_DRQ_INTERRUPT = (1 << 5),
Borislav Petkov346331f2008-04-18 00:46:26 +0200214 /* 0 = no tape is loaded, so we don't rewind after ejecting */
Borislav Petkov42d54682008-04-27 15:38:27 +0200215 IDETAPE_FLAG_MEDIUM_PRESENT = (1 << 6),
Borislav Petkov346331f2008-04-18 00:46:26 +0200216};
217
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100218/* A pipeline stage. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219typedef struct idetape_stage_s {
220 struct request rq; /* The corresponding request */
221 struct idetape_bh *bh; /* The data buffers */
222 struct idetape_stage_s *next; /* Pointer to the next stage */
223} idetape_stage_t;
224
225/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100226 * Most of our global data which we need to save even as we leave the driver due
227 * to an interrupt or a timer event is stored in the struct defined below.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 */
229typedef struct ide_tape_obj {
230 ide_drive_t *drive;
231 ide_driver_t *driver;
232 struct gendisk *disk;
233 struct kref kref;
234
235 /*
236 * Since a typical character device operation requires more
237 * than one packet command, we provide here enough memory
238 * for the maximum of interconnected packet commands.
239 * The packet commands are stored in the circular array pc_stack.
240 * pc_stack_index points to the last used entry, and warps around
241 * to the start when we get to the last array entry.
242 *
243 * pc points to the current processed packet command.
244 *
245 * failed_pc points to the last failed packet command, or contains
246 * NULL if we do not need to retry any packet command. This is
247 * required since an additional packet command is needed before the
248 * retry, to get detailed information on what went wrong.
249 */
250 /* Current packet command */
Borislav Petkovd236d742008-04-18 00:46:27 +0200251 struct ide_atapi_pc *pc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 /* Last failed packet command */
Borislav Petkovd236d742008-04-18 00:46:27 +0200253 struct ide_atapi_pc *failed_pc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 /* Packet command stack */
Borislav Petkovd236d742008-04-18 00:46:27 +0200255 struct ide_atapi_pc pc_stack[IDETAPE_PC_STACK];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 /* Next free packet command storage space */
257 int pc_stack_index;
258 struct request rq_stack[IDETAPE_PC_STACK];
259 /* We implement a circular array */
260 int rq_stack_index;
261
262 /*
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100263 * DSC polling variables.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100265 * While polling for DSC we use postponed_rq to postpone the current
266 * request so that ide.c will be able to service pending requests on the
267 * other device. Note that at most we will have only one DSC (usually
268 * data transfer) request in the device request queue. Additional
269 * requests can be queued in our internal pipeline, but they will be
270 * visible to ide.c only one at a time.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 */
272 struct request *postponed_rq;
273 /* The time in which we started polling for DSC */
274 unsigned long dsc_polling_start;
275 /* Timer used to poll for dsc */
276 struct timer_list dsc_timer;
277 /* Read/Write dsc polling frequency */
Borislav Petkov54bb2072008-02-06 02:57:52 +0100278 unsigned long best_dsc_rw_freq;
279 unsigned long dsc_poll_freq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 unsigned long dsc_timeout;
281
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100282 /* Read position information */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 u8 partition;
284 /* Current block */
Borislav Petkov54bb2072008-02-06 02:57:52 +0100285 unsigned int first_frame;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100287 /* Last error information */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 u8 sense_key, asc, ascq;
289
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100290 /* Character device operation */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 unsigned int minor;
292 /* device name */
293 char name[4];
294 /* Current character device data transfer direction */
Borislav Petkov54abf372008-02-06 02:57:52 +0100295 u8 chrdev_dir;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296
Borislav Petkov54bb2072008-02-06 02:57:52 +0100297 /* tape block size, usually 512 or 1024 bytes */
298 unsigned short blk_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 int user_bs_factor;
Borislav Petkovb6422012008-02-02 19:56:49 +0100300
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 /* Copy of the tape's Capabilities and Mechanical Page */
Borislav Petkovb6422012008-02-02 19:56:49 +0100302 u8 caps[20];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303
304 /*
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100305 * Active data transfer request parameters.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100307 * At most, there is only one ide-tape originated data transfer request
308 * in the device request queue. This allows ide.c to easily service
309 * requests from the other device when we postpone our active request.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 */
Borislav Petkov83042b22008-04-27 15:38:27 +0200311
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100312 /* Data buffer size chosen based on the tape's recommendation */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 int stage_size;
314 idetape_stage_t *merge_stage;
315 int merge_stage_size;
316 struct idetape_bh *bh;
317 char *b_data;
318 int b_count;
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100319
Borislav Petkov83042b22008-04-27 15:38:27 +0200320 /* Pipeline parameters. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 int pages_per_stage;
322 /* Wasted space in each stage */
323 int excess_bh_size;
324
325 /* Status/Action flags: long for set_bit */
326 unsigned long flags;
327 /* protects the ide-tape queue */
Borislav Petkov54bb2072008-02-06 02:57:52 +0100328 spinlock_t lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100330 /* Measures average tape speed */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 unsigned long avg_time;
332 int avg_size;
333 int avg_speed;
334
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 /* the door is currently locked */
336 int door_locked;
337 /* the tape hardware is write protected */
338 char drv_write_prot;
339 /* the tape is write protected (hardware or opened as read-only) */
340 char write_prot;
341
Borislav Petkov8004a8c2008-02-06 02:57:51 +0100342 u32 debug_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343} idetape_tape_t;
344
Arjan van de Vencf8b8972006-03-23 03:00:45 -0800345static DEFINE_MUTEX(idetape_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346
Will Dysond5dee802005-09-16 02:55:07 -0700347static struct class *idetape_sysfs_class;
348
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349#define to_ide_tape(obj) container_of(obj, struct ide_tape_obj, kref)
350
351#define ide_tape_g(disk) \
352 container_of((disk)->private_data, struct ide_tape_obj, driver)
353
354static struct ide_tape_obj *ide_tape_get(struct gendisk *disk)
355{
356 struct ide_tape_obj *tape = NULL;
357
Arjan van de Vencf8b8972006-03-23 03:00:45 -0800358 mutex_lock(&idetape_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 tape = ide_tape_g(disk);
360 if (tape)
361 kref_get(&tape->kref);
Arjan van de Vencf8b8972006-03-23 03:00:45 -0800362 mutex_unlock(&idetape_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 return tape;
364}
365
366static void ide_tape_release(struct kref *);
367
368static void ide_tape_put(struct ide_tape_obj *tape)
369{
Arjan van de Vencf8b8972006-03-23 03:00:45 -0800370 mutex_lock(&idetape_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 kref_put(&tape->kref, ide_tape_release);
Arjan van de Vencf8b8972006-03-23 03:00:45 -0800372 mutex_unlock(&idetape_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373}
374
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100376 * The variables below are used for the character device interface. Additional
377 * state variables are defined in our ide_drive_t structure.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100379static struct ide_tape_obj *idetape_devs[MAX_HWIFS * MAX_DRIVES];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380
381#define ide_tape_f(file) ((file)->private_data)
382
383static struct ide_tape_obj *ide_tape_chrdev_get(unsigned int i)
384{
385 struct ide_tape_obj *tape = NULL;
386
Arjan van de Vencf8b8972006-03-23 03:00:45 -0800387 mutex_lock(&idetape_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 tape = idetape_devs[i];
389 if (tape)
390 kref_get(&tape->kref);
Arjan van de Vencf8b8972006-03-23 03:00:45 -0800391 mutex_unlock(&idetape_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 return tape;
393}
394
Borislav Petkovd236d742008-04-18 00:46:27 +0200395static void idetape_input_buffers(ide_drive_t *drive, struct ide_atapi_pc *pc,
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100396 unsigned int bcount)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397{
398 struct idetape_bh *bh = pc->bh;
399 int count;
400
401 while (bcount) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 if (bh == NULL) {
403 printk(KERN_ERR "ide-tape: bh == NULL in "
404 "idetape_input_buffers\n");
Bartlomiej Zolnierkiewicz7616c0a2008-04-18 00:46:26 +0200405 ide_atapi_discard_data(drive, bcount);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 return;
407 }
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100408 count = min(
409 (unsigned int)(bh->b_size - atomic_read(&bh->b_count)),
410 bcount);
411 HWIF(drive)->atapi_input_bytes(drive, bh->b_data +
412 atomic_read(&bh->b_count), count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 bcount -= count;
414 atomic_add(count, &bh->b_count);
415 if (atomic_read(&bh->b_count) == bh->b_size) {
416 bh = bh->b_reqnext;
417 if (bh)
418 atomic_set(&bh->b_count, 0);
419 }
420 }
421 pc->bh = bh;
422}
423
Borislav Petkovd236d742008-04-18 00:46:27 +0200424static void idetape_output_buffers(ide_drive_t *drive, struct ide_atapi_pc *pc,
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100425 unsigned int bcount)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426{
427 struct idetape_bh *bh = pc->bh;
428 int count;
429
430 while (bcount) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 if (bh == NULL) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100432 printk(KERN_ERR "ide-tape: bh == NULL in %s\n",
433 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 return;
435 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 count = min((unsigned int)pc->b_count, (unsigned int)bcount);
437 HWIF(drive)->atapi_output_bytes(drive, pc->b_data, count);
438 bcount -= count;
439 pc->b_data += count;
440 pc->b_count -= count;
441 if (!pc->b_count) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100442 bh = bh->b_reqnext;
443 pc->bh = bh;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 if (bh) {
445 pc->b_data = bh->b_data;
446 pc->b_count = atomic_read(&bh->b_count);
447 }
448 }
449 }
450}
451
Borislav Petkovd236d742008-04-18 00:46:27 +0200452static void idetape_update_buffers(struct ide_atapi_pc *pc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453{
454 struct idetape_bh *bh = pc->bh;
455 int count;
Borislav Petkovd236d742008-04-18 00:46:27 +0200456 unsigned int bcount = pc->xferred;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457
Borislav Petkov346331f2008-04-18 00:46:26 +0200458 if (pc->flags & PC_FLAG_WRITING)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 return;
460 while (bcount) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 if (bh == NULL) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100462 printk(KERN_ERR "ide-tape: bh == NULL in %s\n",
463 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 return;
465 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 count = min((unsigned int)bh->b_size, (unsigned int)bcount);
467 atomic_set(&bh->b_count, count);
468 if (atomic_read(&bh->b_count) == bh->b_size)
469 bh = bh->b_reqnext;
470 bcount -= count;
471 }
472 pc->bh = bh;
473}
474
475/*
476 * idetape_next_pc_storage returns a pointer to a place in which we can
477 * safely store a packet command, even though we intend to leave the
478 * driver. A storage space for a maximum of IDETAPE_PC_STACK packet
479 * commands is allocated at initialization time.
480 */
Borislav Petkovd236d742008-04-18 00:46:27 +0200481static struct ide_atapi_pc *idetape_next_pc_storage(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482{
483 idetape_tape_t *tape = drive->driver_data;
484
Borislav Petkov8004a8c2008-02-06 02:57:51 +0100485 debug_log(DBG_PCRQ_STACK, "pc_stack_index=%d\n", tape->pc_stack_index);
486
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487 if (tape->pc_stack_index == IDETAPE_PC_STACK)
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100488 tape->pc_stack_index = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 return (&tape->pc_stack[tape->pc_stack_index++]);
490}
491
492/*
493 * idetape_next_rq_storage is used along with idetape_next_pc_storage.
494 * Since we queue packet commands in the request queue, we need to
495 * allocate a request, along with the allocation of a packet command.
496 */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100497
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498/**************************************************************
499 * *
500 * This should get fixed to use kmalloc(.., GFP_ATOMIC) *
501 * followed later on by kfree(). -ml *
502 * *
503 **************************************************************/
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100504
505static struct request *idetape_next_rq_storage(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506{
507 idetape_tape_t *tape = drive->driver_data;
508
Borislav Petkov8004a8c2008-02-06 02:57:51 +0100509 debug_log(DBG_PCRQ_STACK, "rq_stack_index=%d\n", tape->rq_stack_index);
510
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 if (tape->rq_stack_index == IDETAPE_PC_STACK)
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100512 tape->rq_stack_index = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 return (&tape->rq_stack[tape->rq_stack_index++]);
514}
515
Borislav Petkovd236d742008-04-18 00:46:27 +0200516static void idetape_init_pc(struct ide_atapi_pc *pc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517{
518 memset(pc->c, 0, 12);
519 pc->retries = 0;
520 pc->flags = 0;
Borislav Petkovd236d742008-04-18 00:46:27 +0200521 pc->req_xfer = 0;
522 pc->buf = pc->pc_buf;
523 pc->buf_size = IDETAPE_PC_BUFFER_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 pc->bh = NULL;
525 pc->b_data = NULL;
526}
527
528/*
Borislav Petkov1b5db432008-02-02 19:56:48 +0100529 * called on each failed packet command retry to analyze the request sense. We
530 * currently do not utilize this information.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 */
Borislav Petkov1b5db432008-02-02 19:56:48 +0100532static void idetape_analyze_error(ide_drive_t *drive, u8 *sense)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533{
534 idetape_tape_t *tape = drive->driver_data;
Borislav Petkovd236d742008-04-18 00:46:27 +0200535 struct ide_atapi_pc *pc = tape->failed_pc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536
Borislav Petkov1b5db432008-02-02 19:56:48 +0100537 tape->sense_key = sense[2] & 0xF;
538 tape->asc = sense[12];
539 tape->ascq = sense[13];
Borislav Petkov8004a8c2008-02-06 02:57:51 +0100540
541 debug_log(DBG_ERR, "pc = %x, sense key = %x, asc = %x, ascq = %x\n",
542 pc->c[0], tape->sense_key, tape->asc, tape->ascq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543
Borislav Petkovd236d742008-04-18 00:46:27 +0200544 /* Correct pc->xferred by asking the tape. */
Borislav Petkov346331f2008-04-18 00:46:26 +0200545 if (pc->flags & PC_FLAG_DMA_ERROR) {
Borislav Petkovd236d742008-04-18 00:46:27 +0200546 pc->xferred = pc->req_xfer -
Borislav Petkov54bb2072008-02-06 02:57:52 +0100547 tape->blk_size *
Borislav Petkov860ff5e2008-02-02 19:56:50 +0100548 be32_to_cpu(get_unaligned((u32 *)&sense[3]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 idetape_update_buffers(pc);
550 }
551
552 /*
553 * If error was the result of a zero-length read or write command,
554 * with sense key=5, asc=0x22, ascq=0, let it slide. Some drives
555 * (i.e. Seagate STT3401A Travan) don't support 0-length read/writes.
556 */
Borislav Petkov90699ce2008-02-02 19:56:50 +0100557 if ((pc->c[0] == READ_6 || pc->c[0] == WRITE_6)
Borislav Petkov1b5db432008-02-02 19:56:48 +0100558 /* length == 0 */
559 && pc->c[4] == 0 && pc->c[3] == 0 && pc->c[2] == 0) {
560 if (tape->sense_key == 5) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 /* don't report an error, everything's ok */
562 pc->error = 0;
563 /* don't retry read/write */
Borislav Petkov346331f2008-04-18 00:46:26 +0200564 pc->flags |= PC_FLAG_ABORT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 }
566 }
Borislav Petkov90699ce2008-02-02 19:56:50 +0100567 if (pc->c[0] == READ_6 && (sense[2] & 0x80)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 pc->error = IDETAPE_ERROR_FILEMARK;
Borislav Petkov346331f2008-04-18 00:46:26 +0200569 pc->flags |= PC_FLAG_ABORT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 }
Borislav Petkov90699ce2008-02-02 19:56:50 +0100571 if (pc->c[0] == WRITE_6) {
Borislav Petkov1b5db432008-02-02 19:56:48 +0100572 if ((sense[2] & 0x40) || (tape->sense_key == 0xd
573 && tape->asc == 0x0 && tape->ascq == 0x2)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 pc->error = IDETAPE_ERROR_EOD;
Borislav Petkov346331f2008-04-18 00:46:26 +0200575 pc->flags |= PC_FLAG_ABORT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 }
577 }
Borislav Petkov90699ce2008-02-02 19:56:50 +0100578 if (pc->c[0] == READ_6 || pc->c[0] == WRITE_6) {
Borislav Petkov1b5db432008-02-02 19:56:48 +0100579 if (tape->sense_key == 8) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 pc->error = IDETAPE_ERROR_EOD;
Borislav Petkov346331f2008-04-18 00:46:26 +0200581 pc->flags |= PC_FLAG_ABORT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 }
Borislav Petkov346331f2008-04-18 00:46:26 +0200583 if (!(pc->flags & PC_FLAG_ABORT) &&
Borislav Petkovd236d742008-04-18 00:46:27 +0200584 pc->xferred)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 pc->retries = IDETAPE_MAX_PC_RETRIES + 1;
586 }
587}
588
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100589/* Free a stage along with its related buffers completely. */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100590static void __idetape_kfree_stage(idetape_stage_t *stage)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591{
592 struct idetape_bh *prev_bh, *bh = stage->bh;
593 int size;
594
595 while (bh != NULL) {
596 if (bh->b_data != NULL) {
597 size = (int) bh->b_size;
598 while (size > 0) {
599 free_page((unsigned long) bh->b_data);
600 size -= PAGE_SIZE;
601 bh->b_data += PAGE_SIZE;
602 }
603 }
604 prev_bh = bh;
605 bh = bh->b_reqnext;
606 kfree(prev_bh);
607 }
608 kfree(stage);
609}
610
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100612 * Finish servicing a request and insert a pending pipeline request into the
613 * main device queue.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614 */
615static int idetape_end_request(ide_drive_t *drive, int uptodate, int nr_sects)
616{
617 struct request *rq = HWGROUP(drive)->rq;
618 idetape_tape_t *tape = drive->driver_data;
619 unsigned long flags;
620 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621
Borislav Petkov8004a8c2008-02-06 02:57:51 +0100622 debug_log(DBG_PROCS, "Enter %s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623
624 switch (uptodate) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100625 case 0: error = IDETAPE_ERROR_GENERAL; break;
626 case 1: error = 0; break;
627 default: error = uptodate;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628 }
629 rq->errors = error;
630 if (error)
631 tape->failed_pc = NULL;
632
Bartlomiej Zolnierkiewicz36872212008-01-26 20:13:10 +0100633 if (!blk_special_request(rq)) {
634 ide_end_request(drive, uptodate, nr_sects);
635 return 0;
636 }
637
Borislav Petkov54bb2072008-02-06 02:57:52 +0100638 spin_lock_irqsave(&tape->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 ide_end_drive_cmd(drive, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641
Borislav Petkov54bb2072008-02-06 02:57:52 +0100642 spin_unlock_irqrestore(&tape->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 return 0;
644}
645
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100646static ide_startstop_t idetape_request_sense_callback(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647{
648 idetape_tape_t *tape = drive->driver_data;
649
Borislav Petkov8004a8c2008-02-06 02:57:51 +0100650 debug_log(DBG_PROCS, "Enter %s\n", __func__);
651
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652 if (!tape->pc->error) {
Borislav Petkovd236d742008-04-18 00:46:27 +0200653 idetape_analyze_error(drive, tape->pc->buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 idetape_end_request(drive, 1, 0);
655 } else {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100656 printk(KERN_ERR "ide-tape: Error in REQUEST SENSE itself - "
657 "Aborting request!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 idetape_end_request(drive, 0, 0);
659 }
660 return ide_stopped;
661}
662
Borislav Petkovd236d742008-04-18 00:46:27 +0200663static void idetape_create_request_sense_cmd(struct ide_atapi_pc *pc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664{
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100665 idetape_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +0100666 pc->c[0] = REQUEST_SENSE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667 pc->c[4] = 20;
Borislav Petkovd236d742008-04-18 00:46:27 +0200668 pc->req_xfer = 20;
669 pc->idetape_callback = &idetape_request_sense_callback;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670}
671
672static void idetape_init_rq(struct request *rq, u8 cmd)
673{
674 memset(rq, 0, sizeof(*rq));
Jens Axboe4aff5e22006-08-10 08:44:47 +0200675 rq->cmd_type = REQ_TYPE_SPECIAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 rq->cmd[0] = cmd;
677}
678
679/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100680 * Generate a new packet command request in front of the request queue, before
681 * the current request, so that it will be processed immediately, on the next
682 * pass through the driver. The function below is called from the request
683 * handling part of the driver (the "bottom" part). Safe storage for the request
684 * should be allocated with ide_tape_next_{pc,rq}_storage() prior to that.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100686 * Memory for those requests is pre-allocated at initialization time, and is
687 * limited to IDETAPE_PC_STACK requests. We assume that we have enough space for
688 * the maximum possible number of inter-dependent packet commands.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100690 * The higher level of the driver - The ioctl handler and the character device
691 * handling functions should queue request to the lower level part and wait for
692 * their completion using idetape_queue_pc_tail or idetape_queue_rw_tail.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 */
Borislav Petkovd236d742008-04-18 00:46:27 +0200694static void idetape_queue_pc_head(ide_drive_t *drive, struct ide_atapi_pc *pc,
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100695 struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696{
697 struct ide_tape_obj *tape = drive->driver_data;
698
699 idetape_init_rq(rq, REQ_IDETAPE_PC1);
700 rq->buffer = (char *) pc;
701 rq->rq_disk = tape->disk;
702 (void) ide_do_drive_cmd(drive, rq, ide_preempt);
703}
704
705/*
706 * idetape_retry_pc is called when an error was detected during the
707 * last packet command. We queue a request sense packet command in
708 * the head of the request list.
709 */
710static ide_startstop_t idetape_retry_pc (ide_drive_t *drive)
711{
712 idetape_tape_t *tape = drive->driver_data;
Borislav Petkovd236d742008-04-18 00:46:27 +0200713 struct ide_atapi_pc *pc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 struct request *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715
Bartlomiej Zolnierkiewicz64a57fe2008-02-06 02:57:51 +0100716 (void)ide_read_error(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 pc = idetape_next_pc_storage(drive);
718 rq = idetape_next_rq_storage(drive);
719 idetape_create_request_sense_cmd(pc);
Borislav Petkov346331f2008-04-18 00:46:26 +0200720 set_bit(IDETAPE_FLAG_IGNORE_DSC, &tape->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721 idetape_queue_pc_head(drive, pc, rq);
722 return ide_stopped;
723}
724
725/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100726 * Postpone the current request so that ide.c will be able to service requests
727 * from another device on the same hwgroup while we are polling for DSC.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100729static void idetape_postpone_request(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730{
731 idetape_tape_t *tape = drive->driver_data;
732
Borislav Petkov8004a8c2008-02-06 02:57:51 +0100733 debug_log(DBG_PROCS, "Enter %s\n", __func__);
734
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735 tape->postponed_rq = HWGROUP(drive)->rq;
Borislav Petkov54bb2072008-02-06 02:57:52 +0100736 ide_stall_queue(drive, tape->dsc_poll_freq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737}
738
Borislav Petkovd236d742008-04-18 00:46:27 +0200739typedef void idetape_io_buf(ide_drive_t *, struct ide_atapi_pc *, unsigned int);
Borislav Petkova1efc852008-02-06 02:57:52 +0100740
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741/*
Borislav Petkova1efc852008-02-06 02:57:52 +0100742 * This is the usual interrupt handler which will be called during a packet
743 * command. We will transfer some of the data (as requested by the drive) and
744 * will re-point interrupt handler to us. When data transfer is finished, we
745 * will act according to the algorithm described before
Borislav Petkov8d06bfa2008-02-06 02:57:53 +0100746 * idetape_issue_pc.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747 */
Borislav Petkova1efc852008-02-06 02:57:52 +0100748static ide_startstop_t idetape_pc_intr(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749{
750 ide_hwif_t *hwif = drive->hwif;
751 idetape_tape_t *tape = drive->driver_data;
Borislav Petkovd236d742008-04-18 00:46:27 +0200752 struct ide_atapi_pc *pc = tape->pc;
Borislav Petkova1efc852008-02-06 02:57:52 +0100753 xfer_func_t *xferfunc;
754 idetape_io_buf *iobuf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755 unsigned int temp;
756#if SIMULATE_ERRORS
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100757 static int error_sim_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758#endif
Bartlomiej Zolnierkiewicz790d1232008-01-25 22:17:12 +0100759 u16 bcount;
Bartlomiej Zolnierkiewicz8e7657a2008-01-25 22:17:12 +0100760 u8 stat, ireason;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761
Borislav Petkov8004a8c2008-02-06 02:57:51 +0100762 debug_log(DBG_PROCS, "Enter %s - interrupt handler\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763
764 /* Clear the interrupt */
Bartlomiej Zolnierkiewiczc47137a2008-02-06 02:57:51 +0100765 stat = ide_read_status(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766
Borislav Petkov346331f2008-04-18 00:46:26 +0200767 if (pc->flags & PC_FLAG_DMA_IN_PROGRESS) {
Bartlomiej Zolnierkiewicz5e37bdc2008-04-26 22:25:24 +0200768 if (hwif->dma_ops->dma_end(drive) || (stat & ERR_STAT)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769 /*
770 * A DMA error is sometimes expected. For example,
771 * if the tape is crossing a filemark during a
772 * READ command, it will issue an irq and position
773 * itself before the filemark, so that only a partial
774 * data transfer will occur (which causes the DMA
775 * error). In that case, we will later ask the tape
776 * how much bytes of the original request were
777 * actually transferred (we can't receive that
778 * information from the DMA engine on most chipsets).
779 */
780
781 /*
782 * On the contrary, a DMA error is never expected;
783 * it usually indicates a hardware error or abort.
784 * If the tape crosses a filemark during a READ
785 * command, it will issue an irq and position itself
786 * after the filemark (not before). Only a partial
787 * data transfer will occur, but no DMA error.
788 * (AS, 19 Apr 2001)
789 */
Borislav Petkov346331f2008-04-18 00:46:26 +0200790 pc->flags |= PC_FLAG_DMA_ERROR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791 } else {
Borislav Petkovd236d742008-04-18 00:46:27 +0200792 pc->xferred = pc->req_xfer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793 idetape_update_buffers(pc);
794 }
Borislav Petkov8004a8c2008-02-06 02:57:51 +0100795 debug_log(DBG_PROCS, "DMA finished\n");
796
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797 }
798
799 /* No more interrupts */
Bartlomiej Zolnierkiewicz22c525b2008-01-25 22:17:11 +0100800 if ((stat & DRQ_STAT) == 0) {
Borislav Petkov8004a8c2008-02-06 02:57:51 +0100801 debug_log(DBG_SENSE, "Packet command completed, %d bytes"
Borislav Petkovd236d742008-04-18 00:46:27 +0200802 " transferred\n", pc->xferred);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803
Borislav Petkov346331f2008-04-18 00:46:26 +0200804 pc->flags &= ~PC_FLAG_DMA_IN_PROGRESS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805 local_irq_enable();
806
807#if SIMULATE_ERRORS
Borislav Petkov90699ce2008-02-02 19:56:50 +0100808 if ((pc->c[0] == WRITE_6 || pc->c[0] == READ_6) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809 (++error_sim_count % 100) == 0) {
810 printk(KERN_INFO "ide-tape: %s: simulating error\n",
811 tape->name);
Bartlomiej Zolnierkiewicz22c525b2008-01-25 22:17:11 +0100812 stat |= ERR_STAT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813 }
814#endif
Borislav Petkov90699ce2008-02-02 19:56:50 +0100815 if ((stat & ERR_STAT) && pc->c[0] == REQUEST_SENSE)
Bartlomiej Zolnierkiewicz22c525b2008-01-25 22:17:11 +0100816 stat &= ~ERR_STAT;
Borislav Petkov346331f2008-04-18 00:46:26 +0200817 if ((stat & ERR_STAT) || (pc->flags & PC_FLAG_DMA_ERROR)) {
Bartlomiej Zolnierkiewicz22c525b2008-01-25 22:17:11 +0100818 /* Error detected */
Borislav Petkov8004a8c2008-02-06 02:57:51 +0100819 debug_log(DBG_ERR, "%s: I/O error\n", tape->name);
820
Borislav Petkov90699ce2008-02-02 19:56:50 +0100821 if (pc->c[0] == REQUEST_SENSE) {
Borislav Petkova1efc852008-02-06 02:57:52 +0100822 printk(KERN_ERR "ide-tape: I/O error in request"
823 " sense command\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824 return ide_do_reset(drive);
825 }
Borislav Petkov8004a8c2008-02-06 02:57:51 +0100826 debug_log(DBG_ERR, "[cmd %x]: check condition\n",
827 pc->c[0]);
828
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829 /* Retry operation */
830 return idetape_retry_pc(drive);
831 }
832 pc->error = 0;
Borislav Petkov346331f2008-04-18 00:46:26 +0200833 if ((pc->flags & PC_FLAG_WAIT_FOR_DSC) &&
Bartlomiej Zolnierkiewicz22c525b2008-01-25 22:17:11 +0100834 (stat & SEEK_STAT) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835 /* Media access command */
836 tape->dsc_polling_start = jiffies;
Borislav Petkov54bb2072008-02-06 02:57:52 +0100837 tape->dsc_poll_freq = IDETAPE_DSC_MA_FAST;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838 tape->dsc_timeout = jiffies + IDETAPE_DSC_MA_TIMEOUT;
839 /* Allow ide.c to handle other requests */
840 idetape_postpone_request(drive);
841 return ide_stopped;
842 }
843 if (tape->failed_pc == pc)
844 tape->failed_pc = NULL;
845 /* Command finished - Call the callback function */
Borislav Petkovd236d742008-04-18 00:46:27 +0200846 return pc->idetape_callback(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847 }
Borislav Petkov346331f2008-04-18 00:46:26 +0200848
849 if (pc->flags & PC_FLAG_DMA_IN_PROGRESS) {
850 pc->flags &= ~PC_FLAG_DMA_IN_PROGRESS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851 printk(KERN_ERR "ide-tape: The tape wants to issue more "
852 "interrupts in DMA mode\n");
853 printk(KERN_ERR "ide-tape: DMA disabled, reverting to PIO\n");
Bartlomiej Zolnierkiewicz7469aaf2007-02-17 02:40:26 +0100854 ide_dma_off(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855 return ide_do_reset(drive);
856 }
857 /* Get the number of bytes to transfer on this interrupt. */
Bartlomiej Zolnierkiewicz23579a22008-04-18 00:46:26 +0200858 bcount = (hwif->INB(hwif->io_ports[IDE_BCOUNTH_OFFSET]) << 8) |
859 hwif->INB(hwif->io_ports[IDE_BCOUNTL_OFFSET]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860
Bartlomiej Zolnierkiewicz23579a22008-04-18 00:46:26 +0200861 ireason = hwif->INB(hwif->io_ports[IDE_IREASON_OFFSET]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862
Bartlomiej Zolnierkiewicz8e7657a2008-01-25 22:17:12 +0100863 if (ireason & CD) {
Borislav Petkova1efc852008-02-06 02:57:52 +0100864 printk(KERN_ERR "ide-tape: CoD != 0 in %s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865 return ide_do_reset(drive);
866 }
Borislav Petkov346331f2008-04-18 00:46:26 +0200867 if (((ireason & IO) == IO) == !!(pc->flags & PC_FLAG_WRITING)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868 /* Hopefully, we will never get here */
869 printk(KERN_ERR "ide-tape: We wanted to %s, ",
Bartlomiej Zolnierkiewicz8e7657a2008-01-25 22:17:12 +0100870 (ireason & IO) ? "Write" : "Read");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871 printk(KERN_ERR "ide-tape: but the tape wants us to %s !\n",
Bartlomiej Zolnierkiewicz8e7657a2008-01-25 22:17:12 +0100872 (ireason & IO) ? "Read" : "Write");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873 return ide_do_reset(drive);
874 }
Borislav Petkov346331f2008-04-18 00:46:26 +0200875 if (!(pc->flags & PC_FLAG_WRITING)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876 /* Reading - Check that we have enough space */
Borislav Petkovd236d742008-04-18 00:46:27 +0200877 temp = pc->xferred + bcount;
878 if (temp > pc->req_xfer) {
879 if (temp > pc->buf_size) {
Borislav Petkova1efc852008-02-06 02:57:52 +0100880 printk(KERN_ERR "ide-tape: The tape wants to "
881 "send us more data than expected "
882 "- discarding data\n");
Bartlomiej Zolnierkiewicz7616c0a2008-04-18 00:46:26 +0200883 ide_atapi_discard_data(drive, bcount);
Borislav Petkova1efc852008-02-06 02:57:52 +0100884 ide_set_handler(drive, &idetape_pc_intr,
885 IDETAPE_WAIT_CMD, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886 return ide_started;
887 }
Borislav Petkov8004a8c2008-02-06 02:57:51 +0100888 debug_log(DBG_SENSE, "The tape wants to send us more "
889 "data than expected - allowing transfer\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890 }
Borislav Petkova1efc852008-02-06 02:57:52 +0100891 iobuf = &idetape_input_buffers;
892 xferfunc = hwif->atapi_input_bytes;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893 } else {
Borislav Petkova1efc852008-02-06 02:57:52 +0100894 iobuf = &idetape_output_buffers;
895 xferfunc = hwif->atapi_output_bytes;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896 }
Borislav Petkova1efc852008-02-06 02:57:52 +0100897
898 if (pc->bh)
899 iobuf(drive, pc, bcount);
900 else
Borislav Petkovd236d742008-04-18 00:46:27 +0200901 xferfunc(drive, pc->cur_pos, bcount);
Borislav Petkova1efc852008-02-06 02:57:52 +0100902
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903 /* Update the current position */
Borislav Petkovd236d742008-04-18 00:46:27 +0200904 pc->xferred += bcount;
905 pc->cur_pos += bcount;
Borislav Petkov8004a8c2008-02-06 02:57:51 +0100906
907 debug_log(DBG_SENSE, "[cmd %x] transferred %d bytes on that intr.\n",
908 pc->c[0], bcount);
909
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910 /* And set the interrupt handler again */
911 ide_set_handler(drive, &idetape_pc_intr, IDETAPE_WAIT_CMD, NULL);
912 return ide_started;
913}
914
915/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100916 * Packet Command Interface
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100918 * The current Packet Command is available in tape->pc, and will not change
919 * until we finish handling it. Each packet command is associated with a
920 * callback function that will be called when the command is finished.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100922 * The handling will be done in three stages:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100924 * 1. idetape_issue_pc will send the packet command to the drive, and will set
925 * the interrupt handler to idetape_pc_intr.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100927 * 2. On each interrupt, idetape_pc_intr will be called. This step will be
928 * repeated until the device signals us that no more interrupts will be issued.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100930 * 3. ATAPI Tape media access commands have immediate status with a delayed
931 * process. In case of a successful initiation of a media access packet command,
932 * the DSC bit will be set when the actual execution of the command is finished.
933 * Since the tape drive will not issue an interrupt, we have to poll for this
934 * event. In this case, we define the request as "low priority request" by
935 * setting rq_status to IDETAPE_RQ_POSTPONED, set a timer to poll for DSC and
936 * exit the driver.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100938 * ide.c will then give higher priority to requests which originate from the
939 * other device, until will change rq_status to RQ_ACTIVE.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100941 * 4. When the packet command is finished, it will be checked for errors.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100943 * 5. In case an error was found, we queue a request sense packet command in
944 * front of the request queue and retry the operation up to
945 * IDETAPE_MAX_PC_RETRIES times.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100947 * 6. In case no error was found, or we decided to give up and not to retry
948 * again, the callback function will be called and then we will handle the next
949 * request.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950 */
951static ide_startstop_t idetape_transfer_pc(ide_drive_t *drive)
952{
953 ide_hwif_t *hwif = drive->hwif;
954 idetape_tape_t *tape = drive->driver_data;
Borislav Petkovd236d742008-04-18 00:46:27 +0200955 struct ide_atapi_pc *pc = tape->pc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956 int retries = 100;
957 ide_startstop_t startstop;
Bartlomiej Zolnierkiewicz8e7657a2008-01-25 22:17:12 +0100958 u8 ireason;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100960 if (ide_wait_stat(&startstop, drive, DRQ_STAT, BUSY_STAT, WAIT_READY)) {
961 printk(KERN_ERR "ide-tape: Strange, packet command initiated "
962 "yet DRQ isn't asserted\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963 return startstop;
964 }
Bartlomiej Zolnierkiewicz23579a22008-04-18 00:46:26 +0200965 ireason = hwif->INB(hwif->io_ports[IDE_IREASON_OFFSET]);
Bartlomiej Zolnierkiewicz8e7657a2008-01-25 22:17:12 +0100966 while (retries-- && ((ireason & CD) == 0 || (ireason & IO))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967 printk(KERN_ERR "ide-tape: (IO,CoD != (0,1) while issuing "
968 "a packet command, retrying\n");
969 udelay(100);
Bartlomiej Zolnierkiewicz23579a22008-04-18 00:46:26 +0200970 ireason = hwif->INB(hwif->io_ports[IDE_IREASON_OFFSET]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971 if (retries == 0) {
972 printk(KERN_ERR "ide-tape: (IO,CoD != (0,1) while "
973 "issuing a packet command, ignoring\n");
Bartlomiej Zolnierkiewicz8e7657a2008-01-25 22:17:12 +0100974 ireason |= CD;
975 ireason &= ~IO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976 }
977 }
Bartlomiej Zolnierkiewicz8e7657a2008-01-25 22:17:12 +0100978 if ((ireason & CD) == 0 || (ireason & IO)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979 printk(KERN_ERR "ide-tape: (IO,CoD) != (0,1) while issuing "
980 "a packet command\n");
981 return ide_do_reset(drive);
982 }
983 /* Set the interrupt routine */
984 ide_set_handler(drive, &idetape_pc_intr, IDETAPE_WAIT_CMD, NULL);
985#ifdef CONFIG_BLK_DEV_IDEDMA
986 /* Begin DMA, if necessary */
Borislav Petkov346331f2008-04-18 00:46:26 +0200987 if (pc->flags & PC_FLAG_DMA_IN_PROGRESS)
Bartlomiej Zolnierkiewicz5e37bdc2008-04-26 22:25:24 +0200988 hwif->dma_ops->dma_start(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989#endif
990 /* Send the actual packet */
991 HWIF(drive)->atapi_output_bytes(drive, pc->c, 12);
992 return ide_started;
993}
994
Borislav Petkovd236d742008-04-18 00:46:27 +0200995static ide_startstop_t idetape_issue_pc(ide_drive_t *drive,
996 struct ide_atapi_pc *pc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997{
998 ide_hwif_t *hwif = drive->hwif;
999 idetape_tape_t *tape = drive->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000 int dma_ok = 0;
Bartlomiej Zolnierkiewicz790d1232008-01-25 22:17:12 +01001001 u16 bcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002
Borislav Petkov90699ce2008-02-02 19:56:50 +01001003 if (tape->pc->c[0] == REQUEST_SENSE &&
1004 pc->c[0] == REQUEST_SENSE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005 printk(KERN_ERR "ide-tape: possible ide-tape.c bug - "
1006 "Two request sense in serial were issued\n");
1007 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008
Borislav Petkov90699ce2008-02-02 19:56:50 +01001009 if (tape->failed_pc == NULL && pc->c[0] != REQUEST_SENSE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010 tape->failed_pc = pc;
1011 /* Set the current packet command */
1012 tape->pc = pc;
1013
1014 if (pc->retries > IDETAPE_MAX_PC_RETRIES ||
Borislav Petkov346331f2008-04-18 00:46:26 +02001015 (pc->flags & PC_FLAG_ABORT)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016 /*
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001017 * We will "abort" retrying a packet command in case legitimate
1018 * error code was received (crossing a filemark, or end of the
1019 * media, for example).
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020 */
Borislav Petkov346331f2008-04-18 00:46:26 +02001021 if (!(pc->flags & PC_FLAG_ABORT)) {
Borislav Petkov90699ce2008-02-02 19:56:50 +01001022 if (!(pc->c[0] == TEST_UNIT_READY &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023 tape->sense_key == 2 && tape->asc == 4 &&
1024 (tape->ascq == 1 || tape->ascq == 8))) {
1025 printk(KERN_ERR "ide-tape: %s: I/O error, "
1026 "pc = %2x, key = %2x, "
1027 "asc = %2x, ascq = %2x\n",
1028 tape->name, pc->c[0],
1029 tape->sense_key, tape->asc,
1030 tape->ascq);
1031 }
1032 /* Giving up */
1033 pc->error = IDETAPE_ERROR_GENERAL;
1034 }
1035 tape->failed_pc = NULL;
Borislav Petkovd236d742008-04-18 00:46:27 +02001036 return pc->idetape_callback(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037 }
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001038 debug_log(DBG_SENSE, "Retry #%d, cmd = %02X\n", pc->retries, pc->c[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039
1040 pc->retries++;
1041 /* We haven't transferred any data yet */
Borislav Petkovd236d742008-04-18 00:46:27 +02001042 pc->xferred = 0;
1043 pc->cur_pos = pc->buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044 /* Request to transfer the entire buffer at once */
Borislav Petkovd236d742008-04-18 00:46:27 +02001045 bcount = pc->req_xfer;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046
Borislav Petkov346331f2008-04-18 00:46:26 +02001047 if (pc->flags & PC_FLAG_DMA_ERROR) {
1048 pc->flags &= ~PC_FLAG_DMA_ERROR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049 printk(KERN_WARNING "ide-tape: DMA disabled, "
1050 "reverting to PIO\n");
Bartlomiej Zolnierkiewicz7469aaf2007-02-17 02:40:26 +01001051 ide_dma_off(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052 }
Borislav Petkov346331f2008-04-18 00:46:26 +02001053 if ((pc->flags & PC_FLAG_DMA_RECOMMENDED) && drive->using_dma)
Bartlomiej Zolnierkiewicz5e37bdc2008-04-26 22:25:24 +02001054 dma_ok = !hwif->dma_ops->dma_setup(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055
Bartlomiej Zolnierkiewicz2fc57382008-01-25 22:17:13 +01001056 ide_pktcmd_tf_load(drive, IDE_TFLAG_NO_SELECT_MASK |
1057 IDE_TFLAG_OUT_DEVICE, bcount, dma_ok);
1058
Borislav Petkov346331f2008-04-18 00:46:26 +02001059 if (dma_ok)
1060 /* Will begin DMA later */
1061 pc->flags |= PC_FLAG_DMA_IN_PROGRESS;
1062 if (test_bit(IDETAPE_FLAG_DRQ_INTERRUPT, &tape->flags)) {
Bartlomiej Zolnierkiewiczc1c9dbc2008-02-02 19:56:44 +01001063 ide_execute_command(drive, WIN_PACKETCMD, &idetape_transfer_pc,
1064 IDETAPE_WAIT_CMD, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065 return ide_started;
1066 } else {
Bartlomiej Zolnierkiewicz23579a22008-04-18 00:46:26 +02001067 hwif->OUTB(WIN_PACKETCMD, hwif->io_ports[IDE_COMMAND_OFFSET]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068 return idetape_transfer_pc(drive);
1069 }
1070}
1071
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001072static ide_startstop_t idetape_pc_callback(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073{
1074 idetape_tape_t *tape = drive->driver_data;
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001075
1076 debug_log(DBG_PROCS, "Enter %s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077
1078 idetape_end_request(drive, tape->pc->error ? 0 : 1, 0);
1079 return ide_stopped;
1080}
1081
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001082/* A mode sense command is used to "sense" tape parameters. */
Borislav Petkovd236d742008-04-18 00:46:27 +02001083static void idetape_create_mode_sense_cmd(struct ide_atapi_pc *pc, u8 page_code)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084{
1085 idetape_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +01001086 pc->c[0] = MODE_SENSE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087 if (page_code != IDETAPE_BLOCK_DESCRIPTOR)
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001088 /* DBD = 1 - Don't return block descriptors */
1089 pc->c[1] = 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090 pc->c[2] = page_code;
1091 /*
1092 * Changed pc->c[3] to 0 (255 will at best return unused info).
1093 *
1094 * For SCSI this byte is defined as subpage instead of high byte
1095 * of length and some IDE drives seem to interpret it this way
1096 * and return an error when 255 is used.
1097 */
1098 pc->c[3] = 0;
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001099 /* We will just discard data in that case */
1100 pc->c[4] = 255;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101 if (page_code == IDETAPE_BLOCK_DESCRIPTOR)
Borislav Petkovd236d742008-04-18 00:46:27 +02001102 pc->req_xfer = 12;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103 else if (page_code == IDETAPE_CAPABILITIES_PAGE)
Borislav Petkovd236d742008-04-18 00:46:27 +02001104 pc->req_xfer = 24;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105 else
Borislav Petkovd236d742008-04-18 00:46:27 +02001106 pc->req_xfer = 50;
1107 pc->idetape_callback = &idetape_pc_callback;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108}
1109
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001110static ide_startstop_t idetape_media_access_finished(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111{
1112 idetape_tape_t *tape = drive->driver_data;
Borislav Petkovd236d742008-04-18 00:46:27 +02001113 struct ide_atapi_pc *pc = tape->pc;
Bartlomiej Zolnierkiewicz22c525b2008-01-25 22:17:11 +01001114 u8 stat;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115
Bartlomiej Zolnierkiewiczc47137a2008-02-06 02:57:51 +01001116 stat = ide_read_status(drive);
1117
Bartlomiej Zolnierkiewicz22c525b2008-01-25 22:17:11 +01001118 if (stat & SEEK_STAT) {
1119 if (stat & ERR_STAT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120 /* Error detected */
Borislav Petkov90699ce2008-02-02 19:56:50 +01001121 if (pc->c[0] != TEST_UNIT_READY)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122 printk(KERN_ERR "ide-tape: %s: I/O error, ",
1123 tape->name);
1124 /* Retry operation */
1125 return idetape_retry_pc(drive);
1126 }
1127 pc->error = 0;
1128 if (tape->failed_pc == pc)
1129 tape->failed_pc = NULL;
1130 } else {
1131 pc->error = IDETAPE_ERROR_GENERAL;
1132 tape->failed_pc = NULL;
1133 }
Borislav Petkovd236d742008-04-18 00:46:27 +02001134 return pc->idetape_callback(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135}
1136
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001137static ide_startstop_t idetape_rw_callback(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138{
1139 idetape_tape_t *tape = drive->driver_data;
1140 struct request *rq = HWGROUP(drive)->rq;
Borislav Petkovd236d742008-04-18 00:46:27 +02001141 int blocks = tape->pc->xferred / tape->blk_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142
Borislav Petkov54bb2072008-02-06 02:57:52 +01001143 tape->avg_size += blocks * tape->blk_size;
Borislav Petkov83042b22008-04-27 15:38:27 +02001144
Marcelo Feitoza Parisi9bae1ff2006-03-28 01:56:46 -08001145 if (time_after_eq(jiffies, tape->avg_time + HZ)) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001146 tape->avg_speed = tape->avg_size * HZ /
1147 (jiffies - tape->avg_time) / 1024;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148 tape->avg_size = 0;
1149 tape->avg_time = jiffies;
1150 }
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001151 debug_log(DBG_PROCS, "Enter %s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152
Borislav Petkov54bb2072008-02-06 02:57:52 +01001153 tape->first_frame += blocks;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154 rq->current_nr_sectors -= blocks;
1155
1156 if (!tape->pc->error)
1157 idetape_end_request(drive, 1, 0);
1158 else
1159 idetape_end_request(drive, tape->pc->error, 0);
1160 return ide_stopped;
1161}
1162
Borislav Petkovd236d742008-04-18 00:46:27 +02001163static void idetape_create_read_cmd(idetape_tape_t *tape,
1164 struct ide_atapi_pc *pc,
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001165 unsigned int length, struct idetape_bh *bh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166{
1167 idetape_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +01001168 pc->c[0] = READ_6;
Borislav Petkov860ff5e2008-02-02 19:56:50 +01001169 put_unaligned(cpu_to_be32(length), (unsigned int *) &pc->c[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170 pc->c[1] = 1;
Borislav Petkovd236d742008-04-18 00:46:27 +02001171 pc->idetape_callback = &idetape_rw_callback;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172 pc->bh = bh;
1173 atomic_set(&bh->b_count, 0);
Borislav Petkovd236d742008-04-18 00:46:27 +02001174 pc->buf = NULL;
1175 pc->buf_size = length * tape->blk_size;
1176 pc->req_xfer = pc->buf_size;
1177 if (pc->req_xfer == tape->stage_size)
Borislav Petkov346331f2008-04-18 00:46:26 +02001178 pc->flags |= PC_FLAG_DMA_RECOMMENDED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179}
1180
Borislav Petkovd236d742008-04-18 00:46:27 +02001181static void idetape_create_write_cmd(idetape_tape_t *tape,
1182 struct ide_atapi_pc *pc,
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001183 unsigned int length, struct idetape_bh *bh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184{
1185 idetape_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +01001186 pc->c[0] = WRITE_6;
Borislav Petkov860ff5e2008-02-02 19:56:50 +01001187 put_unaligned(cpu_to_be32(length), (unsigned int *) &pc->c[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188 pc->c[1] = 1;
Borislav Petkovd236d742008-04-18 00:46:27 +02001189 pc->idetape_callback = &idetape_rw_callback;
Borislav Petkov346331f2008-04-18 00:46:26 +02001190 pc->flags |= PC_FLAG_WRITING;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191 pc->bh = bh;
1192 pc->b_data = bh->b_data;
1193 pc->b_count = atomic_read(&bh->b_count);
Borislav Petkovd236d742008-04-18 00:46:27 +02001194 pc->buf = NULL;
1195 pc->buf_size = length * tape->blk_size;
1196 pc->req_xfer = pc->buf_size;
1197 if (pc->req_xfer == tape->stage_size)
Borislav Petkov346331f2008-04-18 00:46:26 +02001198 pc->flags |= PC_FLAG_DMA_RECOMMENDED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199}
1200
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201static ide_startstop_t idetape_do_request(ide_drive_t *drive,
1202 struct request *rq, sector_t block)
1203{
1204 idetape_tape_t *tape = drive->driver_data;
Borislav Petkovd236d742008-04-18 00:46:27 +02001205 struct ide_atapi_pc *pc = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206 struct request *postponed_rq = tape->postponed_rq;
Bartlomiej Zolnierkiewicz22c525b2008-01-25 22:17:11 +01001207 u8 stat;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001209 debug_log(DBG_SENSE, "sector: %ld, nr_sectors: %ld,"
1210 " current_nr_sectors: %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211 rq->sector, rq->nr_sectors, rq->current_nr_sectors);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212
Jens Axboe4aff5e22006-08-10 08:44:47 +02001213 if (!blk_special_request(rq)) {
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001214 /* We do not support buffer cache originated requests. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215 printk(KERN_NOTICE "ide-tape: %s: Unsupported request in "
Jens Axboe4aff5e22006-08-10 08:44:47 +02001216 "request queue (%d)\n", drive->name, rq->cmd_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217 ide_end_request(drive, 0, 0);
1218 return ide_stopped;
1219 }
1220
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001221 /* Retry a failed packet command */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001222 if (tape->failed_pc && tape->pc->c[0] == REQUEST_SENSE)
Borislav Petkov8d06bfa2008-02-06 02:57:53 +01001223 return idetape_issue_pc(drive, tape->failed_pc);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001224
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225 if (postponed_rq != NULL)
1226 if (rq != postponed_rq) {
1227 printk(KERN_ERR "ide-tape: ide-tape.c bug - "
1228 "Two DSC requests were queued\n");
1229 idetape_end_request(drive, 0, 0);
1230 return ide_stopped;
1231 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232
1233 tape->postponed_rq = NULL;
1234
1235 /*
1236 * If the tape is still busy, postpone our request and service
1237 * the other device meanwhile.
1238 */
Bartlomiej Zolnierkiewiczc47137a2008-02-06 02:57:51 +01001239 stat = ide_read_status(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240
1241 if (!drive->dsc_overlap && !(rq->cmd[0] & REQ_IDETAPE_PC2))
Borislav Petkov346331f2008-04-18 00:46:26 +02001242 set_bit(IDETAPE_FLAG_IGNORE_DSC, &tape->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243
1244 if (drive->post_reset == 1) {
Borislav Petkov346331f2008-04-18 00:46:26 +02001245 set_bit(IDETAPE_FLAG_IGNORE_DSC, &tape->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246 drive->post_reset = 0;
1247 }
1248
Borislav Petkov346331f2008-04-18 00:46:26 +02001249 if (!test_and_clear_bit(IDETAPE_FLAG_IGNORE_DSC, &tape->flags) &&
Bartlomiej Zolnierkiewicz22c525b2008-01-25 22:17:11 +01001250 (stat & SEEK_STAT) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251 if (postponed_rq == NULL) {
1252 tape->dsc_polling_start = jiffies;
Borislav Petkov54bb2072008-02-06 02:57:52 +01001253 tape->dsc_poll_freq = tape->best_dsc_rw_freq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254 tape->dsc_timeout = jiffies + IDETAPE_DSC_RW_TIMEOUT;
1255 } else if (time_after(jiffies, tape->dsc_timeout)) {
1256 printk(KERN_ERR "ide-tape: %s: DSC timeout\n",
1257 tape->name);
1258 if (rq->cmd[0] & REQ_IDETAPE_PC2) {
1259 idetape_media_access_finished(drive);
1260 return ide_stopped;
1261 } else {
1262 return ide_do_reset(drive);
1263 }
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001264 } else if (time_after(jiffies,
1265 tape->dsc_polling_start +
1266 IDETAPE_DSC_MA_THRESHOLD))
Borislav Petkov54bb2072008-02-06 02:57:52 +01001267 tape->dsc_poll_freq = IDETAPE_DSC_MA_SLOW;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268 idetape_postpone_request(drive);
1269 return ide_stopped;
1270 }
1271 if (rq->cmd[0] & REQ_IDETAPE_READ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272 pc = idetape_next_pc_storage(drive);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001273 idetape_create_read_cmd(tape, pc, rq->current_nr_sectors,
1274 (struct idetape_bh *)rq->special);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275 goto out;
1276 }
1277 if (rq->cmd[0] & REQ_IDETAPE_WRITE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278 pc = idetape_next_pc_storage(drive);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001279 idetape_create_write_cmd(tape, pc, rq->current_nr_sectors,
1280 (struct idetape_bh *)rq->special);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281 goto out;
1282 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283 if (rq->cmd[0] & REQ_IDETAPE_PC1) {
Borislav Petkovd236d742008-04-18 00:46:27 +02001284 pc = (struct ide_atapi_pc *) rq->buffer;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285 rq->cmd[0] &= ~(REQ_IDETAPE_PC1);
1286 rq->cmd[0] |= REQ_IDETAPE_PC2;
1287 goto out;
1288 }
1289 if (rq->cmd[0] & REQ_IDETAPE_PC2) {
1290 idetape_media_access_finished(drive);
1291 return ide_stopped;
1292 }
1293 BUG();
1294out:
Borislav Petkov8d06bfa2008-02-06 02:57:53 +01001295 return idetape_issue_pc(drive, pc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296}
1297
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001298/* Pipeline related functions */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299
1300/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001301 * The function below uses __get_free_page to allocate a pipeline stage, along
1302 * with all the necessary small buffers which together make a buffer of size
1303 * tape->stage_size (or a bit more). We attempt to combine sequential pages as
1304 * much as possible.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001306 * It returns a pointer to the new allocated stage, or NULL if we can't (or
1307 * don't want to) allocate a stage.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001309 * Pipeline stages are optional and are used to increase performance. If we
1310 * can't allocate them, we'll manage without them.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311 */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001312static idetape_stage_t *__idetape_kmalloc_stage(idetape_tape_t *tape, int full,
1313 int clear)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314{
1315 idetape_stage_t *stage;
1316 struct idetape_bh *prev_bh, *bh;
1317 int pages = tape->pages_per_stage;
1318 char *b_data = NULL;
1319
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001320 stage = kmalloc(sizeof(idetape_stage_t), GFP_KERNEL);
1321 if (!stage)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322 return NULL;
1323 stage->next = NULL;
1324
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001325 stage->bh = kmalloc(sizeof(struct idetape_bh), GFP_KERNEL);
1326 bh = stage->bh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327 if (bh == NULL)
1328 goto abort;
1329 bh->b_reqnext = NULL;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001330 bh->b_data = (char *) __get_free_page(GFP_KERNEL);
1331 if (!bh->b_data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332 goto abort;
1333 if (clear)
1334 memset(bh->b_data, 0, PAGE_SIZE);
1335 bh->b_size = PAGE_SIZE;
1336 atomic_set(&bh->b_count, full ? bh->b_size : 0);
1337
1338 while (--pages) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001339 b_data = (char *) __get_free_page(GFP_KERNEL);
1340 if (!b_data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341 goto abort;
1342 if (clear)
1343 memset(b_data, 0, PAGE_SIZE);
1344 if (bh->b_data == b_data + PAGE_SIZE) {
1345 bh->b_size += PAGE_SIZE;
1346 bh->b_data -= PAGE_SIZE;
1347 if (full)
1348 atomic_add(PAGE_SIZE, &bh->b_count);
1349 continue;
1350 }
1351 if (b_data == bh->b_data + bh->b_size) {
1352 bh->b_size += PAGE_SIZE;
1353 if (full)
1354 atomic_add(PAGE_SIZE, &bh->b_count);
1355 continue;
1356 }
1357 prev_bh = bh;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001358 bh = kmalloc(sizeof(struct idetape_bh), GFP_KERNEL);
1359 if (!bh) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360 free_page((unsigned long) b_data);
1361 goto abort;
1362 }
1363 bh->b_reqnext = NULL;
1364 bh->b_data = b_data;
1365 bh->b_size = PAGE_SIZE;
1366 atomic_set(&bh->b_count, full ? bh->b_size : 0);
1367 prev_bh->b_reqnext = bh;
1368 }
1369 bh->b_size -= tape->excess_bh_size;
1370 if (full)
1371 atomic_sub(tape->excess_bh_size, &bh->b_count);
1372 return stage;
1373abort:
1374 __idetape_kfree_stage(stage);
1375 return NULL;
1376}
1377
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001378static int idetape_copy_stage_from_user(idetape_tape_t *tape,
Borislav Petkov8646c882008-04-27 15:38:26 +02001379 const char __user *buf, int n)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380{
1381 struct idetape_bh *bh = tape->bh;
1382 int count;
Daniel Walkerdcd96372006-06-25 05:47:37 -07001383 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384
1385 while (n) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386 if (bh == NULL) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001387 printk(KERN_ERR "ide-tape: bh == NULL in %s\n",
1388 __func__);
Daniel Walkerdcd96372006-06-25 05:47:37 -07001389 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390 }
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001391 count = min((unsigned int)
1392 (bh->b_size - atomic_read(&bh->b_count)),
1393 (unsigned int)n);
1394 if (copy_from_user(bh->b_data + atomic_read(&bh->b_count), buf,
1395 count))
Daniel Walkerdcd96372006-06-25 05:47:37 -07001396 ret = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397 n -= count;
1398 atomic_add(count, &bh->b_count);
1399 buf += count;
1400 if (atomic_read(&bh->b_count) == bh->b_size) {
1401 bh = bh->b_reqnext;
1402 if (bh)
1403 atomic_set(&bh->b_count, 0);
1404 }
1405 }
1406 tape->bh = bh;
Daniel Walkerdcd96372006-06-25 05:47:37 -07001407 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408}
1409
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001410static int idetape_copy_stage_to_user(idetape_tape_t *tape, char __user *buf,
Borislav Petkov99d74e62008-04-27 15:38:25 +02001411 int n)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412{
1413 struct idetape_bh *bh = tape->bh;
1414 int count;
Daniel Walkerdcd96372006-06-25 05:47:37 -07001415 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416
1417 while (n) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418 if (bh == NULL) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001419 printk(KERN_ERR "ide-tape: bh == NULL in %s\n",
1420 __func__);
Daniel Walkerdcd96372006-06-25 05:47:37 -07001421 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423 count = min(tape->b_count, n);
Daniel Walkerdcd96372006-06-25 05:47:37 -07001424 if (copy_to_user(buf, tape->b_data, count))
1425 ret = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426 n -= count;
1427 tape->b_data += count;
1428 tape->b_count -= count;
1429 buf += count;
1430 if (!tape->b_count) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001431 bh = bh->b_reqnext;
1432 tape->bh = bh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433 if (bh) {
1434 tape->b_data = bh->b_data;
1435 tape->b_count = atomic_read(&bh->b_count);
1436 }
1437 }
1438 }
Daniel Walkerdcd96372006-06-25 05:47:37 -07001439 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440}
1441
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001442static void idetape_init_merge_stage(idetape_tape_t *tape)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443{
1444 struct idetape_bh *bh = tape->merge_stage->bh;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001445
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446 tape->bh = bh;
Borislav Petkov54abf372008-02-06 02:57:52 +01001447 if (tape->chrdev_dir == IDETAPE_DIR_WRITE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448 atomic_set(&bh->b_count, 0);
1449 else {
1450 tape->b_data = bh->b_data;
1451 tape->b_count = atomic_read(&bh->b_count);
1452 }
1453}
1454
Borislav Petkova2f5b7f2008-02-06 02:57:51 +01001455static ide_startstop_t idetape_read_position_callback(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456{
1457 idetape_tape_t *tape = drive->driver_data;
Borislav Petkovd236d742008-04-18 00:46:27 +02001458 u8 *readpos = tape->pc->buf;
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001459
1460 debug_log(DBG_PROCS, "Enter %s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461
1462 if (!tape->pc->error) {
Borislav Petkova2f5b7f2008-02-06 02:57:51 +01001463 debug_log(DBG_SENSE, "BOP - %s\n",
1464 (readpos[0] & 0x80) ? "Yes" : "No");
1465 debug_log(DBG_SENSE, "EOP - %s\n",
1466 (readpos[0] & 0x40) ? "Yes" : "No");
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001467
Borislav Petkova2f5b7f2008-02-06 02:57:51 +01001468 if (readpos[0] & 0x4) {
1469 printk(KERN_INFO "ide-tape: Block location is unknown"
1470 "to the tape\n");
Borislav Petkov346331f2008-04-18 00:46:26 +02001471 clear_bit(IDETAPE_FLAG_ADDRESS_VALID, &tape->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472 idetape_end_request(drive, 0, 0);
1473 } else {
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001474 debug_log(DBG_SENSE, "Block Location - %u\n",
Borislav Petkova2f5b7f2008-02-06 02:57:51 +01001475 be32_to_cpu(*(u32 *)&readpos[4]));
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001476
Borislav Petkova2f5b7f2008-02-06 02:57:51 +01001477 tape->partition = readpos[1];
Borislav Petkov54bb2072008-02-06 02:57:52 +01001478 tape->first_frame =
Borislav Petkova2f5b7f2008-02-06 02:57:51 +01001479 be32_to_cpu(*(u32 *)&readpos[4]);
Borislav Petkov346331f2008-04-18 00:46:26 +02001480 set_bit(IDETAPE_FLAG_ADDRESS_VALID, &tape->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481 idetape_end_request(drive, 1, 0);
1482 }
1483 } else {
1484 idetape_end_request(drive, 0, 0);
1485 }
1486 return ide_stopped;
1487}
1488
1489/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001490 * Write a filemark if write_filemark=1. Flush the device buffers without
1491 * writing a filemark otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492 */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001493static void idetape_create_write_filemark_cmd(ide_drive_t *drive,
Borislav Petkovd236d742008-04-18 00:46:27 +02001494 struct ide_atapi_pc *pc, int write_filemark)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495{
1496 idetape_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +01001497 pc->c[0] = WRITE_FILEMARKS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498 pc->c[4] = write_filemark;
Borislav Petkov346331f2008-04-18 00:46:26 +02001499 pc->flags |= PC_FLAG_WAIT_FOR_DSC;
Borislav Petkovd236d742008-04-18 00:46:27 +02001500 pc->idetape_callback = &idetape_pc_callback;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501}
1502
Borislav Petkovd236d742008-04-18 00:46:27 +02001503static void idetape_create_test_unit_ready_cmd(struct ide_atapi_pc *pc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504{
1505 idetape_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +01001506 pc->c[0] = TEST_UNIT_READY;
Borislav Petkovd236d742008-04-18 00:46:27 +02001507 pc->idetape_callback = &idetape_pc_callback;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001508}
1509
1510/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001511 * We add a special packet command request to the tail of the request queue, and
1512 * wait for it to be serviced. This is not to be called from within the request
1513 * handling part of the driver! We allocate here data on the stack and it is
1514 * valid until the request is finished. This is not the case for the bottom part
1515 * of the driver, where we are always leaving the functions to wait for an
1516 * interrupt or a timer event.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001518 * From the bottom part of the driver, we should allocate safe memory using
1519 * idetape_next_pc_storage() and ide_tape_next_rq_storage(), and add the request
1520 * to the request list without waiting for it to be serviced! In that case, we
1521 * usually use idetape_queue_pc_head().
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522 */
Borislav Petkovea1ab3d2008-04-27 15:38:27 +02001523static int idetape_queue_pc_tail(ide_drive_t *drive, struct ide_atapi_pc *pc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524{
1525 struct ide_tape_obj *tape = drive->driver_data;
1526 struct request rq;
1527
1528 idetape_init_rq(&rq, REQ_IDETAPE_PC1);
1529 rq.buffer = (char *) pc;
1530 rq.rq_disk = tape->disk;
1531 return ide_do_drive_cmd(drive, &rq, ide_wait);
1532}
1533
Borislav Petkovd236d742008-04-18 00:46:27 +02001534static void idetape_create_load_unload_cmd(ide_drive_t *drive,
1535 struct ide_atapi_pc *pc, int cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536{
1537 idetape_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +01001538 pc->c[0] = START_STOP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539 pc->c[4] = cmd;
Borislav Petkov346331f2008-04-18 00:46:26 +02001540 pc->flags |= PC_FLAG_WAIT_FOR_DSC;
Borislav Petkovd236d742008-04-18 00:46:27 +02001541 pc->idetape_callback = &idetape_pc_callback;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542}
1543
1544static int idetape_wait_ready(ide_drive_t *drive, unsigned long timeout)
1545{
1546 idetape_tape_t *tape = drive->driver_data;
Borislav Petkovd236d742008-04-18 00:46:27 +02001547 struct ide_atapi_pc pc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548 int load_attempted = 0;
1549
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001550 /* Wait for the tape to become ready */
Borislav Petkov346331f2008-04-18 00:46:26 +02001551 set_bit(IDETAPE_FLAG_MEDIUM_PRESENT, &tape->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001552 timeout += jiffies;
1553 while (time_before(jiffies, timeout)) {
1554 idetape_create_test_unit_ready_cmd(&pc);
Borislav Petkovea1ab3d2008-04-27 15:38:27 +02001555 if (!idetape_queue_pc_tail(drive, &pc))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556 return 0;
1557 if ((tape->sense_key == 2 && tape->asc == 4 && tape->ascq == 2)
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001558 || (tape->asc == 0x3A)) {
1559 /* no media */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560 if (load_attempted)
1561 return -ENOMEDIUM;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001562 idetape_create_load_unload_cmd(drive, &pc,
1563 IDETAPE_LU_LOAD_MASK);
Borislav Petkovea1ab3d2008-04-27 15:38:27 +02001564 idetape_queue_pc_tail(drive, &pc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565 load_attempted = 1;
1566 /* not about to be ready */
1567 } else if (!(tape->sense_key == 2 && tape->asc == 4 &&
1568 (tape->ascq == 1 || tape->ascq == 8)))
1569 return -EIO;
Nishanth Aravamudan80ce45f2005-09-10 00:27:08 -07001570 msleep(100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571 }
1572 return -EIO;
1573}
1574
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001575static int idetape_flush_tape_buffers(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001576{
Borislav Petkovd236d742008-04-18 00:46:27 +02001577 struct ide_atapi_pc pc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001578 int rc;
1579
1580 idetape_create_write_filemark_cmd(drive, &pc, 0);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001581 rc = idetape_queue_pc_tail(drive, &pc);
1582 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001583 return rc;
1584 idetape_wait_ready(drive, 60 * 5 * HZ);
1585 return 0;
1586}
1587
Borislav Petkovd236d742008-04-18 00:46:27 +02001588static void idetape_create_read_position_cmd(struct ide_atapi_pc *pc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001589{
1590 idetape_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +01001591 pc->c[0] = READ_POSITION;
Borislav Petkovd236d742008-04-18 00:46:27 +02001592 pc->req_xfer = 20;
1593 pc->idetape_callback = &idetape_read_position_callback;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594}
1595
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001596static int idetape_read_position(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001597{
1598 idetape_tape_t *tape = drive->driver_data;
Borislav Petkovd236d742008-04-18 00:46:27 +02001599 struct ide_atapi_pc pc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001600 int position;
1601
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001602 debug_log(DBG_PROCS, "Enter %s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001603
1604 idetape_create_read_position_cmd(&pc);
1605 if (idetape_queue_pc_tail(drive, &pc))
1606 return -1;
Borislav Petkov54bb2072008-02-06 02:57:52 +01001607 position = tape->first_frame;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608 return position;
1609}
1610
Borislav Petkovd236d742008-04-18 00:46:27 +02001611static void idetape_create_locate_cmd(ide_drive_t *drive,
1612 struct ide_atapi_pc *pc,
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001613 unsigned int block, u8 partition, int skip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001614{
1615 idetape_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +01001616 pc->c[0] = POSITION_TO_ELEMENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617 pc->c[1] = 2;
Borislav Petkov860ff5e2008-02-02 19:56:50 +01001618 put_unaligned(cpu_to_be32(block), (unsigned int *) &pc->c[3]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001619 pc->c[8] = partition;
Borislav Petkov346331f2008-04-18 00:46:26 +02001620 pc->flags |= PC_FLAG_WAIT_FOR_DSC;
Borislav Petkovd236d742008-04-18 00:46:27 +02001621 pc->idetape_callback = &idetape_pc_callback;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001622}
1623
Borislav Petkovd236d742008-04-18 00:46:27 +02001624static int idetape_create_prevent_cmd(ide_drive_t *drive,
1625 struct ide_atapi_pc *pc, int prevent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001626{
1627 idetape_tape_t *tape = drive->driver_data;
1628
Borislav Petkovb6422012008-02-02 19:56:49 +01001629 /* device supports locking according to capabilities page */
1630 if (!(tape->caps[6] & 0x01))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001631 return 0;
1632
1633 idetape_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +01001634 pc->c[0] = ALLOW_MEDIUM_REMOVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001635 pc->c[4] = prevent;
Borislav Petkovd236d742008-04-18 00:46:27 +02001636 pc->idetape_callback = &idetape_pc_callback;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001637 return 1;
1638}
1639
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001640static int __idetape_discard_read_pipeline(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641{
1642 idetape_tape_t *tape = drive->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643
Borislav Petkov54abf372008-02-06 02:57:52 +01001644 if (tape->chrdev_dir != IDETAPE_DIR_READ)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001645 return 0;
1646
Borislav Petkov83042b22008-04-27 15:38:27 +02001647 clear_bit(IDETAPE_FLAG_FILEMARK, &tape->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001648 tape->merge_stage_size = 0;
1649 if (tape->merge_stage != NULL) {
1650 __idetape_kfree_stage(tape->merge_stage);
1651 tape->merge_stage = NULL;
1652 }
1653
Borislav Petkov54abf372008-02-06 02:57:52 +01001654 tape->chrdev_dir = IDETAPE_DIR_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001655
Borislav Petkov83042b22008-04-27 15:38:27 +02001656 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001657}
1658
1659/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001660 * Position the tape to the requested block using the LOCATE packet command.
1661 * A READ POSITION command is then issued to check where we are positioned. Like
1662 * all higher level operations, we queue the commands at the tail of the request
1663 * queue and wait for their completion.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664 */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001665static int idetape_position_tape(ide_drive_t *drive, unsigned int block,
1666 u8 partition, int skip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001667{
1668 idetape_tape_t *tape = drive->driver_data;
1669 int retval;
Borislav Petkovd236d742008-04-18 00:46:27 +02001670 struct ide_atapi_pc pc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001671
Borislav Petkov54abf372008-02-06 02:57:52 +01001672 if (tape->chrdev_dir == IDETAPE_DIR_READ)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001673 __idetape_discard_read_pipeline(drive);
1674 idetape_wait_ready(drive, 60 * 5 * HZ);
1675 idetape_create_locate_cmd(drive, &pc, block, partition, skip);
1676 retval = idetape_queue_pc_tail(drive, &pc);
1677 if (retval)
1678 return (retval);
1679
1680 idetape_create_read_position_cmd(&pc);
1681 return (idetape_queue_pc_tail(drive, &pc));
1682}
1683
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001684static void idetape_discard_read_pipeline(ide_drive_t *drive,
1685 int restore_position)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001686{
1687 idetape_tape_t *tape = drive->driver_data;
1688 int cnt;
1689 int seek, position;
1690
1691 cnt = __idetape_discard_read_pipeline(drive);
1692 if (restore_position) {
1693 position = idetape_read_position(drive);
1694 seek = position > cnt ? position - cnt : 0;
1695 if (idetape_position_tape(drive, seek, 0, 0)) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001696 printk(KERN_INFO "ide-tape: %s: position_tape failed in"
1697 " discard_pipeline()\n", tape->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001698 return;
1699 }
1700 }
1701}
1702
1703/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001704 * Generate a read/write request for the block device interface and wait for it
1705 * to be serviced.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001706 */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001707static int idetape_queue_rw_tail(ide_drive_t *drive, int cmd, int blocks,
1708 struct idetape_bh *bh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001709{
1710 idetape_tape_t *tape = drive->driver_data;
1711 struct request rq;
1712
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001713 debug_log(DBG_SENSE, "%s: cmd=%d\n", __func__, cmd);
1714
Linus Torvalds1da177e2005-04-16 15:20:36 -07001715 idetape_init_rq(&rq, cmd);
1716 rq.rq_disk = tape->disk;
1717 rq.special = (void *)bh;
Borislav Petkov54bb2072008-02-06 02:57:52 +01001718 rq.sector = tape->first_frame;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001719 rq.nr_sectors = blocks;
1720 rq.current_nr_sectors = blocks;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001721 (void) ide_do_drive_cmd(drive, &rq, ide_wait);
1722
1723 if ((cmd & (REQ_IDETAPE_READ | REQ_IDETAPE_WRITE)) == 0)
1724 return 0;
1725
1726 if (tape->merge_stage)
1727 idetape_init_merge_stage(tape);
1728 if (rq.errors == IDETAPE_ERROR_GENERAL)
1729 return -EIO;
Borislav Petkov54bb2072008-02-06 02:57:52 +01001730 return (tape->blk_size * (blocks-rq.current_nr_sectors));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001731}
1732
Borislav Petkovd236d742008-04-18 00:46:27 +02001733static void idetape_create_inquiry_cmd(struct ide_atapi_pc *pc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001734{
1735 idetape_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +01001736 pc->c[0] = INQUIRY;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001737 pc->c[4] = 254;
Borislav Petkovd236d742008-04-18 00:46:27 +02001738 pc->req_xfer = 254;
1739 pc->idetape_callback = &idetape_pc_callback;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001740}
1741
Borislav Petkovd236d742008-04-18 00:46:27 +02001742static void idetape_create_rewind_cmd(ide_drive_t *drive,
1743 struct ide_atapi_pc *pc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001744{
1745 idetape_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +01001746 pc->c[0] = REZERO_UNIT;
Borislav Petkov346331f2008-04-18 00:46:26 +02001747 pc->flags |= PC_FLAG_WAIT_FOR_DSC;
Borislav Petkovd236d742008-04-18 00:46:27 +02001748 pc->idetape_callback = &idetape_pc_callback;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001749}
1750
Borislav Petkovd236d742008-04-18 00:46:27 +02001751static void idetape_create_erase_cmd(struct ide_atapi_pc *pc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001752{
1753 idetape_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +01001754 pc->c[0] = ERASE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001755 pc->c[1] = 1;
Borislav Petkov346331f2008-04-18 00:46:26 +02001756 pc->flags |= PC_FLAG_WAIT_FOR_DSC;
Borislav Petkovd236d742008-04-18 00:46:27 +02001757 pc->idetape_callback = &idetape_pc_callback;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001758}
1759
Borislav Petkovd236d742008-04-18 00:46:27 +02001760static void idetape_create_space_cmd(struct ide_atapi_pc *pc, int count, u8 cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001761{
1762 idetape_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +01001763 pc->c[0] = SPACE;
Borislav Petkov860ff5e2008-02-02 19:56:50 +01001764 put_unaligned(cpu_to_be32(count), (unsigned int *) &pc->c[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001765 pc->c[1] = cmd;
Borislav Petkov346331f2008-04-18 00:46:26 +02001766 pc->flags |= PC_FLAG_WAIT_FOR_DSC;
Borislav Petkovd236d742008-04-18 00:46:27 +02001767 pc->idetape_callback = &idetape_pc_callback;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001768}
1769
Borislav Petkov97c566c2008-04-27 15:38:25 +02001770/* Queue up a character device originated write request. */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001771static int idetape_add_chrdev_write_request(ide_drive_t *drive, int blocks)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001772{
1773 idetape_tape_t *tape = drive->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001774
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001775 debug_log(DBG_CHRDEV, "Enter %s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001776
Borislav Petkov0aa4b012008-04-27 15:38:27 +02001777 return idetape_queue_rw_tail(drive, REQ_IDETAPE_WRITE,
1778 blocks, tape->merge_stage->bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001779}
1780
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001781static void idetape_empty_write_pipeline(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001782{
1783 idetape_tape_t *tape = drive->driver_data;
1784 int blocks, min;
1785 struct idetape_bh *bh;
Borislav Petkov55a5d292008-02-02 19:56:49 +01001786
Borislav Petkov54abf372008-02-06 02:57:52 +01001787 if (tape->chrdev_dir != IDETAPE_DIR_WRITE) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001788 printk(KERN_ERR "ide-tape: bug: Trying to empty write pipeline,"
1789 " but we are not writing.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790 return;
1791 }
1792 if (tape->merge_stage_size > tape->stage_size) {
1793 printk(KERN_ERR "ide-tape: bug: merge_buffer too big\n");
1794 tape->merge_stage_size = tape->stage_size;
1795 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001796 if (tape->merge_stage_size) {
Borislav Petkov54bb2072008-02-06 02:57:52 +01001797 blocks = tape->merge_stage_size / tape->blk_size;
1798 if (tape->merge_stage_size % tape->blk_size) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001799 unsigned int i;
1800
1801 blocks++;
Borislav Petkov54bb2072008-02-06 02:57:52 +01001802 i = tape->blk_size - tape->merge_stage_size %
1803 tape->blk_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001804 bh = tape->bh->b_reqnext;
1805 while (bh) {
1806 atomic_set(&bh->b_count, 0);
1807 bh = bh->b_reqnext;
1808 }
1809 bh = tape->bh;
1810 while (i) {
1811 if (bh == NULL) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001812 printk(KERN_INFO "ide-tape: bug,"
1813 " bh NULL\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001814 break;
1815 }
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001816 min = min(i, (unsigned int)(bh->b_size -
1817 atomic_read(&bh->b_count)));
1818 memset(bh->b_data + atomic_read(&bh->b_count),
1819 0, min);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001820 atomic_add(min, &bh->b_count);
1821 i -= min;
1822 bh = bh->b_reqnext;
1823 }
1824 }
1825 (void) idetape_add_chrdev_write_request(drive, blocks);
1826 tape->merge_stage_size = 0;
1827 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001828 if (tape->merge_stage != NULL) {
1829 __idetape_kfree_stage(tape->merge_stage);
1830 tape->merge_stage = NULL;
1831 }
Borislav Petkov54abf372008-02-06 02:57:52 +01001832 tape->chrdev_dir = IDETAPE_DIR_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001833}
1834
Borislav Petkov83042b22008-04-27 15:38:27 +02001835static int idetape_init_read(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001836{
1837 idetape_tape_t *tape = drive->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001838 int bytes_read;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001839
1840 /* Initialize read operation */
Borislav Petkov54abf372008-02-06 02:57:52 +01001841 if (tape->chrdev_dir != IDETAPE_DIR_READ) {
1842 if (tape->chrdev_dir == IDETAPE_DIR_WRITE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001843 idetape_empty_write_pipeline(drive);
1844 idetape_flush_tape_buffers(drive);
1845 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001846 if (tape->merge_stage || tape->merge_stage_size) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001847 printk(KERN_ERR "ide-tape: merge_stage_size should be"
1848 " 0 now\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001849 tape->merge_stage_size = 0;
1850 }
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001851 tape->merge_stage = __idetape_kmalloc_stage(tape, 0, 0);
1852 if (!tape->merge_stage)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001853 return -ENOMEM;
Borislav Petkov54abf372008-02-06 02:57:52 +01001854 tape->chrdev_dir = IDETAPE_DIR_READ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001855
1856 /*
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001857 * Issue a read 0 command to ensure that DSC handshake is
1858 * switched from completion mode to buffer available mode.
1859 * No point in issuing this if DSC overlap isn't supported, some
1860 * drives (Seagate STT3401A) will return an error.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001861 */
1862 if (drive->dsc_overlap) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001863 bytes_read = idetape_queue_rw_tail(drive,
1864 REQ_IDETAPE_READ, 0,
1865 tape->merge_stage->bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001866 if (bytes_read < 0) {
1867 __idetape_kfree_stage(tape->merge_stage);
1868 tape->merge_stage = NULL;
Borislav Petkov54abf372008-02-06 02:57:52 +01001869 tape->chrdev_dir = IDETAPE_DIR_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001870 return bytes_read;
1871 }
1872 }
1873 }
Borislav Petkov5e69bd92008-04-27 15:38:25 +02001874
Linus Torvalds1da177e2005-04-16 15:20:36 -07001875 return 0;
1876}
1877
1878/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001879 * Called from idetape_chrdev_read() to service a character device read request
1880 * and add read-ahead requests to our pipeline.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001881 */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001882static int idetape_add_chrdev_read_request(ide_drive_t *drive, int blocks)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001883{
1884 idetape_tape_t *tape = drive->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001885
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001886 debug_log(DBG_PROCS, "Enter %s, %d blocks\n", __func__, blocks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001887
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001888 /* If we are at a filemark, return a read length of 0 */
Borislav Petkov346331f2008-04-18 00:46:26 +02001889 if (test_bit(IDETAPE_FLAG_FILEMARK, &tape->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001890 return 0;
1891
Borislav Petkov83042b22008-04-27 15:38:27 +02001892 idetape_init_read(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001893
Borislav Petkov5e69bd92008-04-27 15:38:25 +02001894 return idetape_queue_rw_tail(drive, REQ_IDETAPE_READ, blocks,
1895 tape->merge_stage->bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001896}
1897
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001898static void idetape_pad_zeros(ide_drive_t *drive, int bcount)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001899{
1900 idetape_tape_t *tape = drive->driver_data;
1901 struct idetape_bh *bh;
1902 int blocks;
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001903
Linus Torvalds1da177e2005-04-16 15:20:36 -07001904 while (bcount) {
1905 unsigned int count;
1906
1907 bh = tape->merge_stage->bh;
1908 count = min(tape->stage_size, bcount);
1909 bcount -= count;
Borislav Petkov54bb2072008-02-06 02:57:52 +01001910 blocks = count / tape->blk_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001911 while (count) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001912 atomic_set(&bh->b_count,
1913 min(count, (unsigned int)bh->b_size));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001914 memset(bh->b_data, 0, atomic_read(&bh->b_count));
1915 count -= atomic_read(&bh->b_count);
1916 bh = bh->b_reqnext;
1917 }
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001918 idetape_queue_rw_tail(drive, REQ_IDETAPE_WRITE, blocks,
1919 tape->merge_stage->bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001920 }
1921}
1922
Linus Torvalds1da177e2005-04-16 15:20:36 -07001923/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001924 * Rewinds the tape to the Beginning Of the current Partition (BOP). We
1925 * currently support only one partition.
1926 */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001927static int idetape_rewind_tape(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001928{
1929 int retval;
Borislav Petkovd236d742008-04-18 00:46:27 +02001930 struct ide_atapi_pc pc;
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001931 idetape_tape_t *tape;
1932 tape = drive->driver_data;
1933
1934 debug_log(DBG_SENSE, "Enter %s\n", __func__);
1935
Linus Torvalds1da177e2005-04-16 15:20:36 -07001936 idetape_create_rewind_cmd(drive, &pc);
1937 retval = idetape_queue_pc_tail(drive, &pc);
1938 if (retval)
1939 return retval;
1940
1941 idetape_create_read_position_cmd(&pc);
1942 retval = idetape_queue_pc_tail(drive, &pc);
1943 if (retval)
1944 return retval;
1945 return 0;
1946}
1947
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001948/* mtio.h compatible commands should be issued to the chrdev interface. */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001949static int idetape_blkdev_ioctl(ide_drive_t *drive, unsigned int cmd,
1950 unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001951{
1952 idetape_tape_t *tape = drive->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001953 void __user *argp = (void __user *)arg;
1954
Borislav Petkovd59823f2008-02-02 19:56:51 +01001955 struct idetape_config {
1956 int dsc_rw_frequency;
1957 int dsc_media_access_frequency;
1958 int nr_stages;
1959 } config;
1960
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001961 debug_log(DBG_PROCS, "Enter %s\n", __func__);
1962
Linus Torvalds1da177e2005-04-16 15:20:36 -07001963 switch (cmd) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001964 case 0x0340:
1965 if (copy_from_user(&config, argp, sizeof(config)))
1966 return -EFAULT;
1967 tape->best_dsc_rw_freq = config.dsc_rw_frequency;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001968 break;
1969 case 0x0350:
1970 config.dsc_rw_frequency = (int) tape->best_dsc_rw_freq;
Borislav Petkov83042b22008-04-27 15:38:27 +02001971 config.nr_stages = 1;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001972 if (copy_to_user(argp, &config, sizeof(config)))
1973 return -EFAULT;
1974 break;
1975 default:
1976 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001977 }
1978 return 0;
1979}
1980
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001981static int idetape_space_over_filemarks(ide_drive_t *drive, short mt_op,
1982 int mt_count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001983{
1984 idetape_tape_t *tape = drive->driver_data;
Borislav Petkovd236d742008-04-18 00:46:27 +02001985 struct ide_atapi_pc pc;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001986 int retval, count = 0;
Borislav Petkovb6422012008-02-02 19:56:49 +01001987 int sprev = !!(tape->caps[4] & 0x20);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001988
1989 if (mt_count == 0)
1990 return 0;
1991 if (MTBSF == mt_op || MTBSFM == mt_op) {
Borislav Petkovb6422012008-02-02 19:56:49 +01001992 if (!sprev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001993 return -EIO;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001994 mt_count = -mt_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001995 }
1996
Borislav Petkov54abf372008-02-06 02:57:52 +01001997 if (tape->chrdev_dir == IDETAPE_DIR_READ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001998 tape->merge_stage_size = 0;
Borislav Petkov346331f2008-04-18 00:46:26 +02001999 if (test_and_clear_bit(IDETAPE_FLAG_FILEMARK, &tape->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002000 ++count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002001 idetape_discard_read_pipeline(drive, 0);
2002 }
2003
2004 /*
Borislav Petkov3c98bf32008-02-06 02:57:53 +01002005 * The filemark was not found in our internal pipeline; now we can issue
2006 * the space command.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002007 */
2008 switch (mt_op) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002009 case MTFSF:
2010 case MTBSF:
2011 idetape_create_space_cmd(&pc, mt_count - count,
2012 IDETAPE_SPACE_OVER_FILEMARK);
2013 return idetape_queue_pc_tail(drive, &pc);
2014 case MTFSFM:
2015 case MTBSFM:
2016 if (!sprev)
2017 return -EIO;
2018 retval = idetape_space_over_filemarks(drive, MTFSF,
2019 mt_count - count);
2020 if (retval)
2021 return retval;
2022 count = (MTBSFM == mt_op ? 1 : -1);
2023 return idetape_space_over_filemarks(drive, MTFSF, count);
2024 default:
2025 printk(KERN_ERR "ide-tape: MTIO operation %d not supported\n",
2026 mt_op);
2027 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002028 }
2029}
2030
Linus Torvalds1da177e2005-04-16 15:20:36 -07002031/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +01002032 * Our character device read / write functions.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002033 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +01002034 * The tape is optimized to maximize throughput when it is transferring an
2035 * integral number of the "continuous transfer limit", which is a parameter of
2036 * the specific tape (26kB on my particular tape, 32kB for Onstream).
Linus Torvalds1da177e2005-04-16 15:20:36 -07002037 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +01002038 * As of version 1.3 of the driver, the character device provides an abstract
2039 * continuous view of the media - any mix of block sizes (even 1 byte) on the
2040 * same backup/restore procedure is supported. The driver will internally
2041 * convert the requests to the recommended transfer unit, so that an unmatch
2042 * between the user's block size to the recommended size will only result in a
2043 * (slightly) increased driver overhead, but will no longer hit performance.
2044 * This is not applicable to Onstream.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002045 */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002046static ssize_t idetape_chrdev_read(struct file *file, char __user *buf,
2047 size_t count, loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002048{
2049 struct ide_tape_obj *tape = ide_tape_f(file);
2050 ide_drive_t *drive = tape->drive;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002051 ssize_t bytes_read, temp, actually_read = 0, rc;
Daniel Walkerdcd96372006-06-25 05:47:37 -07002052 ssize_t ret = 0;
Borislav Petkovb6422012008-02-02 19:56:49 +01002053 u16 ctl = *(u16 *)&tape->caps[12];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002054
Borislav Petkov8004a8c2008-02-06 02:57:51 +01002055 debug_log(DBG_CHRDEV, "Enter %s, count %Zd\n", __func__, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002056
Borislav Petkov54abf372008-02-06 02:57:52 +01002057 if (tape->chrdev_dir != IDETAPE_DIR_READ) {
Borislav Petkov346331f2008-04-18 00:46:26 +02002058 if (test_bit(IDETAPE_FLAG_DETECT_BS, &tape->flags))
Borislav Petkov54bb2072008-02-06 02:57:52 +01002059 if (count > tape->blk_size &&
2060 (count % tape->blk_size) == 0)
2061 tape->user_bs_factor = count / tape->blk_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002062 }
Borislav Petkov83042b22008-04-27 15:38:27 +02002063 rc = idetape_init_read(drive);
Borislav Petkov8d06bfa2008-02-06 02:57:53 +01002064 if (rc < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002065 return rc;
2066 if (count == 0)
2067 return (0);
2068 if (tape->merge_stage_size) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002069 actually_read = min((unsigned int)(tape->merge_stage_size),
2070 (unsigned int)count);
Borislav Petkov99d74e62008-04-27 15:38:25 +02002071 if (idetape_copy_stage_to_user(tape, buf, actually_read))
Daniel Walkerdcd96372006-06-25 05:47:37 -07002072 ret = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002073 buf += actually_read;
2074 tape->merge_stage_size -= actually_read;
2075 count -= actually_read;
2076 }
2077 while (count >= tape->stage_size) {
Borislav Petkovb6422012008-02-02 19:56:49 +01002078 bytes_read = idetape_add_chrdev_read_request(drive, ctl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002079 if (bytes_read <= 0)
2080 goto finish;
Borislav Petkov99d74e62008-04-27 15:38:25 +02002081 if (idetape_copy_stage_to_user(tape, buf, bytes_read))
Daniel Walkerdcd96372006-06-25 05:47:37 -07002082 ret = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002083 buf += bytes_read;
2084 count -= bytes_read;
2085 actually_read += bytes_read;
2086 }
2087 if (count) {
Borislav Petkovb6422012008-02-02 19:56:49 +01002088 bytes_read = idetape_add_chrdev_read_request(drive, ctl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002089 if (bytes_read <= 0)
2090 goto finish;
2091 temp = min((unsigned long)count, (unsigned long)bytes_read);
Borislav Petkov99d74e62008-04-27 15:38:25 +02002092 if (idetape_copy_stage_to_user(tape, buf, temp))
Daniel Walkerdcd96372006-06-25 05:47:37 -07002093 ret = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002094 actually_read += temp;
2095 tape->merge_stage_size = bytes_read-temp;
2096 }
2097finish:
Borislav Petkov346331f2008-04-18 00:46:26 +02002098 if (!actually_read && test_bit(IDETAPE_FLAG_FILEMARK, &tape->flags)) {
Borislav Petkov8004a8c2008-02-06 02:57:51 +01002099 debug_log(DBG_SENSE, "%s: spacing over filemark\n", tape->name);
2100
Linus Torvalds1da177e2005-04-16 15:20:36 -07002101 idetape_space_over_filemarks(drive, MTFSF, 1);
2102 return 0;
2103 }
Daniel Walkerdcd96372006-06-25 05:47:37 -07002104
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002105 return ret ? ret : actually_read;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002106}
2107
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002108static ssize_t idetape_chrdev_write(struct file *file, const char __user *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002109 size_t count, loff_t *ppos)
2110{
2111 struct ide_tape_obj *tape = ide_tape_f(file);
2112 ide_drive_t *drive = tape->drive;
Daniel Walkerdcd96372006-06-25 05:47:37 -07002113 ssize_t actually_written = 0;
2114 ssize_t ret = 0;
Borislav Petkovb6422012008-02-02 19:56:49 +01002115 u16 ctl = *(u16 *)&tape->caps[12];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002116
2117 /* The drive is write protected. */
2118 if (tape->write_prot)
2119 return -EACCES;
2120
Borislav Petkov8004a8c2008-02-06 02:57:51 +01002121 debug_log(DBG_CHRDEV, "Enter %s, count %Zd\n", __func__, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002122
2123 /* Initialize write operation */
Borislav Petkov54abf372008-02-06 02:57:52 +01002124 if (tape->chrdev_dir != IDETAPE_DIR_WRITE) {
2125 if (tape->chrdev_dir == IDETAPE_DIR_READ)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002126 idetape_discard_read_pipeline(drive, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002127 if (tape->merge_stage || tape->merge_stage_size) {
2128 printk(KERN_ERR "ide-tape: merge_stage_size "
2129 "should be 0 now\n");
2130 tape->merge_stage_size = 0;
2131 }
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002132 tape->merge_stage = __idetape_kmalloc_stage(tape, 0, 0);
2133 if (!tape->merge_stage)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002134 return -ENOMEM;
Borislav Petkov54abf372008-02-06 02:57:52 +01002135 tape->chrdev_dir = IDETAPE_DIR_WRITE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002136 idetape_init_merge_stage(tape);
2137
2138 /*
Borislav Petkov3c98bf32008-02-06 02:57:53 +01002139 * Issue a write 0 command to ensure that DSC handshake is
2140 * switched from completion mode to buffer available mode. No
2141 * point in issuing this if DSC overlap isn't supported, some
2142 * drives (Seagate STT3401A) will return an error.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002143 */
2144 if (drive->dsc_overlap) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002145 ssize_t retval = idetape_queue_rw_tail(drive,
2146 REQ_IDETAPE_WRITE, 0,
2147 tape->merge_stage->bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002148 if (retval < 0) {
2149 __idetape_kfree_stage(tape->merge_stage);
2150 tape->merge_stage = NULL;
Borislav Petkov54abf372008-02-06 02:57:52 +01002151 tape->chrdev_dir = IDETAPE_DIR_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002152 return retval;
2153 }
2154 }
2155 }
2156 if (count == 0)
2157 return (0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002158 if (tape->merge_stage_size) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002159 if (tape->merge_stage_size >= tape->stage_size) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002160 printk(KERN_ERR "ide-tape: bug: merge buf too big\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002161 tape->merge_stage_size = 0;
2162 }
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002163 actually_written = min((unsigned int)
2164 (tape->stage_size - tape->merge_stage_size),
2165 (unsigned int)count);
Borislav Petkov8646c882008-04-27 15:38:26 +02002166 if (idetape_copy_stage_from_user(tape, buf, actually_written))
Daniel Walkerdcd96372006-06-25 05:47:37 -07002167 ret = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002168 buf += actually_written;
2169 tape->merge_stage_size += actually_written;
2170 count -= actually_written;
2171
2172 if (tape->merge_stage_size == tape->stage_size) {
Daniel Walkerdcd96372006-06-25 05:47:37 -07002173 ssize_t retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002174 tape->merge_stage_size = 0;
Borislav Petkovb6422012008-02-02 19:56:49 +01002175 retval = idetape_add_chrdev_write_request(drive, ctl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002176 if (retval <= 0)
2177 return (retval);
2178 }
2179 }
2180 while (count >= tape->stage_size) {
Daniel Walkerdcd96372006-06-25 05:47:37 -07002181 ssize_t retval;
Borislav Petkov8646c882008-04-27 15:38:26 +02002182 if (idetape_copy_stage_from_user(tape, buf, tape->stage_size))
Daniel Walkerdcd96372006-06-25 05:47:37 -07002183 ret = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002184 buf += tape->stage_size;
2185 count -= tape->stage_size;
Borislav Petkovb6422012008-02-02 19:56:49 +01002186 retval = idetape_add_chrdev_write_request(drive, ctl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002187 actually_written += tape->stage_size;
2188 if (retval <= 0)
2189 return (retval);
2190 }
2191 if (count) {
2192 actually_written += count;
Borislav Petkov8646c882008-04-27 15:38:26 +02002193 if (idetape_copy_stage_from_user(tape, buf, count))
Daniel Walkerdcd96372006-06-25 05:47:37 -07002194 ret = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002195 tape->merge_stage_size += count;
2196 }
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002197 return ret ? ret : actually_written;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002198}
2199
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002200static int idetape_write_filemark(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002201{
Borislav Petkovd236d742008-04-18 00:46:27 +02002202 struct ide_atapi_pc pc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002203
2204 /* Write a filemark */
2205 idetape_create_write_filemark_cmd(drive, &pc, 1);
2206 if (idetape_queue_pc_tail(drive, &pc)) {
2207 printk(KERN_ERR "ide-tape: Couldn't write a filemark\n");
2208 return -EIO;
2209 }
2210 return 0;
2211}
2212
2213/*
Borislav Petkovd99c9da2008-02-02 19:56:51 +01002214 * Called from idetape_chrdev_ioctl when the general mtio MTIOCTOP ioctl is
2215 * requested.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002216 *
Borislav Petkovd99c9da2008-02-02 19:56:51 +01002217 * Note: MTBSF and MTBSFM are not supported when the tape doesn't support
2218 * spacing over filemarks in the reverse direction. In this case, MTFSFM is also
2219 * usually not supported (it is supported in the rare case in which we crossed
2220 * the filemark during our read-ahead pipelined operation mode).
Linus Torvalds1da177e2005-04-16 15:20:36 -07002221 *
Borislav Petkovd99c9da2008-02-02 19:56:51 +01002222 * The following commands are currently not supported:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002223 *
Borislav Petkovd99c9da2008-02-02 19:56:51 +01002224 * MTFSS, MTBSS, MTWSM, MTSETDENSITY, MTSETDRVBUFFER, MT_ST_BOOLEANS,
2225 * MT_ST_WRITE_THRESHOLD.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002226 */
Borislav Petkovd99c9da2008-02-02 19:56:51 +01002227static int idetape_mtioctop(ide_drive_t *drive, short mt_op, int mt_count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002228{
2229 idetape_tape_t *tape = drive->driver_data;
Borislav Petkovd236d742008-04-18 00:46:27 +02002230 struct ide_atapi_pc pc;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002231 int i, retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002232
Borislav Petkov8004a8c2008-02-06 02:57:51 +01002233 debug_log(DBG_ERR, "Handling MTIOCTOP ioctl: mt_op=%d, mt_count=%d\n",
2234 mt_op, mt_count);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002235
Borislav Petkov3c98bf32008-02-06 02:57:53 +01002236 /* Commands which need our pipelined read-ahead stages. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002237 switch (mt_op) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002238 case MTFSF:
2239 case MTFSFM:
2240 case MTBSF:
2241 case MTBSFM:
2242 if (!mt_count)
2243 return 0;
2244 return idetape_space_over_filemarks(drive, mt_op, mt_count);
2245 default:
2246 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002247 }
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002248
Linus Torvalds1da177e2005-04-16 15:20:36 -07002249 switch (mt_op) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002250 case MTWEOF:
2251 if (tape->write_prot)
2252 return -EACCES;
2253 idetape_discard_read_pipeline(drive, 1);
2254 for (i = 0; i < mt_count; i++) {
2255 retval = idetape_write_filemark(drive);
2256 if (retval)
2257 return retval;
2258 }
2259 return 0;
2260 case MTREW:
2261 idetape_discard_read_pipeline(drive, 0);
2262 if (idetape_rewind_tape(drive))
2263 return -EIO;
2264 return 0;
2265 case MTLOAD:
2266 idetape_discard_read_pipeline(drive, 0);
2267 idetape_create_load_unload_cmd(drive, &pc,
2268 IDETAPE_LU_LOAD_MASK);
2269 return idetape_queue_pc_tail(drive, &pc);
2270 case MTUNLOAD:
2271 case MTOFFL:
2272 /*
2273 * If door is locked, attempt to unlock before
2274 * attempting to eject.
2275 */
2276 if (tape->door_locked) {
2277 if (idetape_create_prevent_cmd(drive, &pc, 0))
2278 if (!idetape_queue_pc_tail(drive, &pc))
2279 tape->door_locked = DOOR_UNLOCKED;
2280 }
2281 idetape_discard_read_pipeline(drive, 0);
2282 idetape_create_load_unload_cmd(drive, &pc,
2283 !IDETAPE_LU_LOAD_MASK);
2284 retval = idetape_queue_pc_tail(drive, &pc);
2285 if (!retval)
Borislav Petkov346331f2008-04-18 00:46:26 +02002286 clear_bit(IDETAPE_FLAG_MEDIUM_PRESENT, &tape->flags);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002287 return retval;
2288 case MTNOP:
2289 idetape_discard_read_pipeline(drive, 0);
2290 return idetape_flush_tape_buffers(drive);
2291 case MTRETEN:
2292 idetape_discard_read_pipeline(drive, 0);
2293 idetape_create_load_unload_cmd(drive, &pc,
2294 IDETAPE_LU_RETENSION_MASK | IDETAPE_LU_LOAD_MASK);
2295 return idetape_queue_pc_tail(drive, &pc);
2296 case MTEOM:
2297 idetape_create_space_cmd(&pc, 0, IDETAPE_SPACE_TO_EOD);
2298 return idetape_queue_pc_tail(drive, &pc);
2299 case MTERASE:
2300 (void)idetape_rewind_tape(drive);
2301 idetape_create_erase_cmd(&pc);
2302 return idetape_queue_pc_tail(drive, &pc);
2303 case MTSETBLK:
2304 if (mt_count) {
2305 if (mt_count < tape->blk_size ||
2306 mt_count % tape->blk_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002307 return -EIO;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002308 tape->user_bs_factor = mt_count / tape->blk_size;
Borislav Petkov346331f2008-04-18 00:46:26 +02002309 clear_bit(IDETAPE_FLAG_DETECT_BS, &tape->flags);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002310 } else
Borislav Petkov346331f2008-04-18 00:46:26 +02002311 set_bit(IDETAPE_FLAG_DETECT_BS, &tape->flags);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002312 return 0;
2313 case MTSEEK:
2314 idetape_discard_read_pipeline(drive, 0);
2315 return idetape_position_tape(drive,
2316 mt_count * tape->user_bs_factor, tape->partition, 0);
2317 case MTSETPART:
2318 idetape_discard_read_pipeline(drive, 0);
2319 return idetape_position_tape(drive, 0, mt_count, 0);
2320 case MTFSR:
2321 case MTBSR:
2322 case MTLOCK:
2323 if (!idetape_create_prevent_cmd(drive, &pc, 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002324 return 0;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002325 retval = idetape_queue_pc_tail(drive, &pc);
2326 if (retval)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002327 return retval;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002328 tape->door_locked = DOOR_EXPLICITLY_LOCKED;
2329 return 0;
2330 case MTUNLOCK:
2331 if (!idetape_create_prevent_cmd(drive, &pc, 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002332 return 0;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002333 retval = idetape_queue_pc_tail(drive, &pc);
2334 if (retval)
2335 return retval;
2336 tape->door_locked = DOOR_UNLOCKED;
2337 return 0;
2338 default:
2339 printk(KERN_ERR "ide-tape: MTIO operation %d not supported\n",
2340 mt_op);
2341 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002342 }
2343}
2344
2345/*
Borislav Petkovd99c9da2008-02-02 19:56:51 +01002346 * Our character device ioctls. General mtio.h magnetic io commands are
2347 * supported here, and not in the corresponding block interface. Our own
2348 * ide-tape ioctls are supported on both interfaces.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002349 */
Borislav Petkovd99c9da2008-02-02 19:56:51 +01002350static int idetape_chrdev_ioctl(struct inode *inode, struct file *file,
2351 unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002352{
2353 struct ide_tape_obj *tape = ide_tape_f(file);
2354 ide_drive_t *drive = tape->drive;
2355 struct mtop mtop;
2356 struct mtget mtget;
2357 struct mtpos mtpos;
Borislav Petkov54bb2072008-02-06 02:57:52 +01002358 int block_offset = 0, position = tape->first_frame;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002359 void __user *argp = (void __user *)arg;
2360
Borislav Petkov8004a8c2008-02-06 02:57:51 +01002361 debug_log(DBG_CHRDEV, "Enter %s, cmd=%u\n", __func__, cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002362
Borislav Petkov54abf372008-02-06 02:57:52 +01002363 if (tape->chrdev_dir == IDETAPE_DIR_WRITE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002364 idetape_empty_write_pipeline(drive);
2365 idetape_flush_tape_buffers(drive);
2366 }
2367 if (cmd == MTIOCGET || cmd == MTIOCPOS) {
Borislav Petkovb361acb2008-04-27 15:38:26 +02002368 block_offset = tape->merge_stage_size /
Borislav Petkov54bb2072008-02-06 02:57:52 +01002369 (tape->blk_size * tape->user_bs_factor);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002370 position = idetape_read_position(drive);
2371 if (position < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002372 return -EIO;
2373 }
2374 switch (cmd) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002375 case MTIOCTOP:
2376 if (copy_from_user(&mtop, argp, sizeof(struct mtop)))
2377 return -EFAULT;
2378 return idetape_mtioctop(drive, mtop.mt_op, mtop.mt_count);
2379 case MTIOCGET:
2380 memset(&mtget, 0, sizeof(struct mtget));
2381 mtget.mt_type = MT_ISSCSI2;
2382 mtget.mt_blkno = position / tape->user_bs_factor - block_offset;
2383 mtget.mt_dsreg =
2384 ((tape->blk_size * tape->user_bs_factor)
2385 << MT_ST_BLKSIZE_SHIFT) & MT_ST_BLKSIZE_MASK;
Borislav Petkov54bb2072008-02-06 02:57:52 +01002386
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002387 if (tape->drv_write_prot)
2388 mtget.mt_gstat |= GMT_WR_PROT(0xffffffff);
2389
2390 if (copy_to_user(argp, &mtget, sizeof(struct mtget)))
2391 return -EFAULT;
2392 return 0;
2393 case MTIOCPOS:
2394 mtpos.mt_blkno = position / tape->user_bs_factor - block_offset;
2395 if (copy_to_user(argp, &mtpos, sizeof(struct mtpos)))
2396 return -EFAULT;
2397 return 0;
2398 default:
2399 if (tape->chrdev_dir == IDETAPE_DIR_READ)
2400 idetape_discard_read_pipeline(drive, 1);
2401 return idetape_blkdev_ioctl(drive, cmd, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002402 }
2403}
2404
Borislav Petkov3cffb9c2008-02-02 19:56:50 +01002405/*
2406 * Do a mode sense page 0 with block descriptor and if it succeeds set the tape
2407 * block size with the reported value.
2408 */
2409static void ide_tape_get_bsize_from_bdesc(ide_drive_t *drive)
2410{
2411 idetape_tape_t *tape = drive->driver_data;
Borislav Petkovd236d742008-04-18 00:46:27 +02002412 struct ide_atapi_pc pc;
Borislav Petkov3cffb9c2008-02-02 19:56:50 +01002413
2414 idetape_create_mode_sense_cmd(&pc, IDETAPE_BLOCK_DESCRIPTOR);
2415 if (idetape_queue_pc_tail(drive, &pc)) {
2416 printk(KERN_ERR "ide-tape: Can't get block descriptor\n");
Borislav Petkov54bb2072008-02-06 02:57:52 +01002417 if (tape->blk_size == 0) {
Borislav Petkov3cffb9c2008-02-02 19:56:50 +01002418 printk(KERN_WARNING "ide-tape: Cannot deal with zero "
2419 "block size, assuming 32k\n");
Borislav Petkov54bb2072008-02-06 02:57:52 +01002420 tape->blk_size = 32768;
Borislav Petkov3cffb9c2008-02-02 19:56:50 +01002421 }
2422 return;
2423 }
Borislav Petkovd236d742008-04-18 00:46:27 +02002424 tape->blk_size = (pc.buf[4 + 5] << 16) +
2425 (pc.buf[4 + 6] << 8) +
2426 pc.buf[4 + 7];
2427 tape->drv_write_prot = (pc.buf[2] & 0x80) >> 7;
Borislav Petkov3cffb9c2008-02-02 19:56:50 +01002428}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002429
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002430static int idetape_chrdev_open(struct inode *inode, struct file *filp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002431{
2432 unsigned int minor = iminor(inode), i = minor & ~0xc0;
2433 ide_drive_t *drive;
2434 idetape_tape_t *tape;
Borislav Petkovd236d742008-04-18 00:46:27 +02002435 struct ide_atapi_pc pc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002436 int retval;
2437
Borislav Petkov8004a8c2008-02-06 02:57:51 +01002438 if (i >= MAX_HWIFS * MAX_DRIVES)
2439 return -ENXIO;
2440
2441 tape = ide_tape_chrdev_get(i);
2442 if (!tape)
2443 return -ENXIO;
2444
2445 debug_log(DBG_CHRDEV, "Enter %s\n", __func__);
2446
Linus Torvalds1da177e2005-04-16 15:20:36 -07002447 /*
2448 * We really want to do nonseekable_open(inode, filp); here, but some
2449 * versions of tar incorrectly call lseek on tapes and bail out if that
2450 * fails. So we disallow pread() and pwrite(), but permit lseeks.
2451 */
2452 filp->f_mode &= ~(FMODE_PREAD | FMODE_PWRITE);
2453
Linus Torvalds1da177e2005-04-16 15:20:36 -07002454 drive = tape->drive;
2455
2456 filp->private_data = tape;
2457
Borislav Petkov346331f2008-04-18 00:46:26 +02002458 if (test_and_set_bit(IDETAPE_FLAG_BUSY, &tape->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002459 retval = -EBUSY;
2460 goto out_put_tape;
2461 }
2462
2463 retval = idetape_wait_ready(drive, 60 * HZ);
2464 if (retval) {
Borislav Petkov346331f2008-04-18 00:46:26 +02002465 clear_bit(IDETAPE_FLAG_BUSY, &tape->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002466 printk(KERN_ERR "ide-tape: %s: drive not ready\n", tape->name);
2467 goto out_put_tape;
2468 }
2469
2470 idetape_read_position(drive);
Borislav Petkov346331f2008-04-18 00:46:26 +02002471 if (!test_bit(IDETAPE_FLAG_ADDRESS_VALID, &tape->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002472 (void)idetape_rewind_tape(drive);
2473
Linus Torvalds1da177e2005-04-16 15:20:36 -07002474 /* Read block size and write protect status from drive. */
Borislav Petkov3cffb9c2008-02-02 19:56:50 +01002475 ide_tape_get_bsize_from_bdesc(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002476
2477 /* Set write protect flag if device is opened as read-only. */
2478 if ((filp->f_flags & O_ACCMODE) == O_RDONLY)
2479 tape->write_prot = 1;
2480 else
2481 tape->write_prot = tape->drv_write_prot;
2482
2483 /* Make sure drive isn't write protected if user wants to write. */
2484 if (tape->write_prot) {
2485 if ((filp->f_flags & O_ACCMODE) == O_WRONLY ||
2486 (filp->f_flags & O_ACCMODE) == O_RDWR) {
Borislav Petkov346331f2008-04-18 00:46:26 +02002487 clear_bit(IDETAPE_FLAG_BUSY, &tape->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002488 retval = -EROFS;
2489 goto out_put_tape;
2490 }
2491 }
2492
Borislav Petkov3c98bf32008-02-06 02:57:53 +01002493 /* Lock the tape drive door so user can't eject. */
Borislav Petkov54abf372008-02-06 02:57:52 +01002494 if (tape->chrdev_dir == IDETAPE_DIR_NONE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002495 if (idetape_create_prevent_cmd(drive, &pc, 1)) {
2496 if (!idetape_queue_pc_tail(drive, &pc)) {
2497 if (tape->door_locked != DOOR_EXPLICITLY_LOCKED)
2498 tape->door_locked = DOOR_LOCKED;
2499 }
2500 }
2501 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002502 return 0;
2503
2504out_put_tape:
2505 ide_tape_put(tape);
2506 return retval;
2507}
2508
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002509static void idetape_write_release(ide_drive_t *drive, unsigned int minor)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002510{
2511 idetape_tape_t *tape = drive->driver_data;
2512
2513 idetape_empty_write_pipeline(drive);
2514 tape->merge_stage = __idetape_kmalloc_stage(tape, 1, 0);
2515 if (tape->merge_stage != NULL) {
Borislav Petkov54bb2072008-02-06 02:57:52 +01002516 idetape_pad_zeros(drive, tape->blk_size *
2517 (tape->user_bs_factor - 1));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002518 __idetape_kfree_stage(tape->merge_stage);
2519 tape->merge_stage = NULL;
2520 }
2521 idetape_write_filemark(drive);
2522 idetape_flush_tape_buffers(drive);
2523 idetape_flush_tape_buffers(drive);
2524}
2525
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002526static int idetape_chrdev_release(struct inode *inode, struct file *filp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002527{
2528 struct ide_tape_obj *tape = ide_tape_f(filp);
2529 ide_drive_t *drive = tape->drive;
Borislav Petkovd236d742008-04-18 00:46:27 +02002530 struct ide_atapi_pc pc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002531 unsigned int minor = iminor(inode);
2532
2533 lock_kernel();
2534 tape = drive->driver_data;
Borislav Petkov8004a8c2008-02-06 02:57:51 +01002535
2536 debug_log(DBG_CHRDEV, "Enter %s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002537
Borislav Petkov54abf372008-02-06 02:57:52 +01002538 if (tape->chrdev_dir == IDETAPE_DIR_WRITE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002539 idetape_write_release(drive, minor);
Borislav Petkov54abf372008-02-06 02:57:52 +01002540 if (tape->chrdev_dir == IDETAPE_DIR_READ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002541 if (minor < 128)
2542 idetape_discard_read_pipeline(drive, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002543 }
Borislav Petkovf64eee72008-04-27 15:38:25 +02002544
Borislav Petkov346331f2008-04-18 00:46:26 +02002545 if (minor < 128 && test_bit(IDETAPE_FLAG_MEDIUM_PRESENT, &tape->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002546 (void) idetape_rewind_tape(drive);
Borislav Petkov54abf372008-02-06 02:57:52 +01002547 if (tape->chrdev_dir == IDETAPE_DIR_NONE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002548 if (tape->door_locked == DOOR_LOCKED) {
2549 if (idetape_create_prevent_cmd(drive, &pc, 0)) {
2550 if (!idetape_queue_pc_tail(drive, &pc))
2551 tape->door_locked = DOOR_UNLOCKED;
2552 }
2553 }
2554 }
Borislav Petkov346331f2008-04-18 00:46:26 +02002555 clear_bit(IDETAPE_FLAG_BUSY, &tape->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002556 ide_tape_put(tape);
2557 unlock_kernel();
2558 return 0;
2559}
2560
2561/*
Borislav Petkov71071b82008-02-06 02:57:53 +01002562 * check the contents of the ATAPI IDENTIFY command results. We return:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002563 *
Borislav Petkov71071b82008-02-06 02:57:53 +01002564 * 1 - If the tape can be supported by us, based on the information we have so
2565 * far.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002566 *
Borislav Petkov71071b82008-02-06 02:57:53 +01002567 * 0 - If this tape driver is not currently supported by us.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002568 */
Borislav Petkov71071b82008-02-06 02:57:53 +01002569static int idetape_identify_device(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002570{
Borislav Petkov71071b82008-02-06 02:57:53 +01002571 u8 gcw[2], protocol, device_type, removable, packet_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002572
2573 if (drive->id_read == 0)
2574 return 1;
2575
Borislav Petkov71071b82008-02-06 02:57:53 +01002576 *((unsigned short *) &gcw) = drive->id->config;
2577
2578 protocol = (gcw[1] & 0xC0) >> 6;
2579 device_type = gcw[1] & 0x1F;
2580 removable = !!(gcw[0] & 0x80);
2581 packet_size = gcw[0] & 0x3;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002582
Linus Torvalds1da177e2005-04-16 15:20:36 -07002583 /* Check that we can support this device */
Borislav Petkov71071b82008-02-06 02:57:53 +01002584 if (protocol != 2)
Bartlomiej Zolnierkiewicz16422de32008-02-02 19:56:48 +01002585 printk(KERN_ERR "ide-tape: Protocol (0x%02x) is not ATAPI\n",
Borislav Petkov71071b82008-02-06 02:57:53 +01002586 protocol);
2587 else if (device_type != 1)
Bartlomiej Zolnierkiewicz16422de32008-02-02 19:56:48 +01002588 printk(KERN_ERR "ide-tape: Device type (0x%02x) is not set "
Borislav Petkov71071b82008-02-06 02:57:53 +01002589 "to tape\n", device_type);
2590 else if (!removable)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002591 printk(KERN_ERR "ide-tape: The removable flag is not set\n");
Borislav Petkov71071b82008-02-06 02:57:53 +01002592 else if (packet_size != 0) {
Borislav Petkov24d57f82008-02-06 02:57:54 +01002593 printk(KERN_ERR "ide-tape: Packet size (0x%02x) is not 12"
2594 " bytes\n", packet_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002595 } else
2596 return 1;
2597 return 0;
2598}
2599
Borislav Petkov6d29c8f2008-02-02 19:56:49 +01002600static void idetape_get_inquiry_results(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002601{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002602 idetape_tape_t *tape = drive->driver_data;
Borislav Petkovd236d742008-04-18 00:46:27 +02002603 struct ide_atapi_pc pc;
Borislav Petkov41f81d542008-02-06 02:57:52 +01002604 char fw_rev[6], vendor_id[10], product_id[18];
Borislav Petkov6d29c8f2008-02-02 19:56:49 +01002605
Linus Torvalds1da177e2005-04-16 15:20:36 -07002606 idetape_create_inquiry_cmd(&pc);
2607 if (idetape_queue_pc_tail(drive, &pc)) {
Borislav Petkov6d29c8f2008-02-02 19:56:49 +01002608 printk(KERN_ERR "ide-tape: %s: can't get INQUIRY results\n",
2609 tape->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002610 return;
2611 }
Borislav Petkovd236d742008-04-18 00:46:27 +02002612 memcpy(vendor_id, &pc.buf[8], 8);
2613 memcpy(product_id, &pc.buf[16], 16);
2614 memcpy(fw_rev, &pc.buf[32], 4);
Borislav Petkov6d29c8f2008-02-02 19:56:49 +01002615
Borislav Petkov41f81d542008-02-06 02:57:52 +01002616 ide_fixstring(vendor_id, 10, 0);
2617 ide_fixstring(product_id, 18, 0);
2618 ide_fixstring(fw_rev, 6, 0);
2619
Borislav Petkov6d29c8f2008-02-02 19:56:49 +01002620 printk(KERN_INFO "ide-tape: %s <-> %s: %s %s rev %s\n",
Borislav Petkov41f81d542008-02-06 02:57:52 +01002621 drive->name, tape->name, vendor_id, product_id, fw_rev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002622}
2623
2624/*
Borislav Petkovb6422012008-02-02 19:56:49 +01002625 * Ask the tape about its various parameters. In particular, we will adjust our
2626 * data transfer buffer size to the recommended value as returned by the tape.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002627 */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002628static void idetape_get_mode_sense_results(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002629{
2630 idetape_tape_t *tape = drive->driver_data;
Borislav Petkovd236d742008-04-18 00:46:27 +02002631 struct ide_atapi_pc pc;
Borislav Petkovb6422012008-02-02 19:56:49 +01002632 u8 *caps;
2633 u8 speed, max_speed;
Borislav Petkov47314fa2008-02-02 19:56:48 +01002634
Linus Torvalds1da177e2005-04-16 15:20:36 -07002635 idetape_create_mode_sense_cmd(&pc, IDETAPE_CAPABILITIES_PAGE);
2636 if (idetape_queue_pc_tail(drive, &pc)) {
Borislav Petkovb6422012008-02-02 19:56:49 +01002637 printk(KERN_ERR "ide-tape: Can't get tape parameters - assuming"
2638 " some default values\n");
Borislav Petkov54bb2072008-02-06 02:57:52 +01002639 tape->blk_size = 512;
Borislav Petkovb6422012008-02-02 19:56:49 +01002640 put_unaligned(52, (u16 *)&tape->caps[12]);
2641 put_unaligned(540, (u16 *)&tape->caps[14]);
2642 put_unaligned(6*52, (u16 *)&tape->caps[16]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002643 return;
2644 }
Borislav Petkovd236d742008-04-18 00:46:27 +02002645 caps = pc.buf + 4 + pc.buf[3];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002646
Borislav Petkovb6422012008-02-02 19:56:49 +01002647 /* convert to host order and save for later use */
2648 speed = be16_to_cpu(*(u16 *)&caps[14]);
2649 max_speed = be16_to_cpu(*(u16 *)&caps[8]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002650
Borislav Petkovb6422012008-02-02 19:56:49 +01002651 put_unaligned(max_speed, (u16 *)&caps[8]);
2652 put_unaligned(be16_to_cpu(*(u16 *)&caps[12]), (u16 *)&caps[12]);
2653 put_unaligned(speed, (u16 *)&caps[14]);
2654 put_unaligned(be16_to_cpu(*(u16 *)&caps[16]), (u16 *)&caps[16]);
2655
2656 if (!speed) {
2657 printk(KERN_INFO "ide-tape: %s: invalid tape speed "
2658 "(assuming 650KB/sec)\n", drive->name);
2659 put_unaligned(650, (u16 *)&caps[14]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002660 }
Borislav Petkovb6422012008-02-02 19:56:49 +01002661 if (!max_speed) {
2662 printk(KERN_INFO "ide-tape: %s: invalid max_speed "
2663 "(assuming 650KB/sec)\n", drive->name);
2664 put_unaligned(650, (u16 *)&caps[8]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002665 }
2666
Borislav Petkovb6422012008-02-02 19:56:49 +01002667 memcpy(&tape->caps, caps, 20);
2668 if (caps[7] & 0x02)
Borislav Petkov54bb2072008-02-06 02:57:52 +01002669 tape->blk_size = 512;
Borislav Petkovb6422012008-02-02 19:56:49 +01002670 else if (caps[7] & 0x04)
Borislav Petkov54bb2072008-02-06 02:57:52 +01002671 tape->blk_size = 1024;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002672}
2673
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +02002674#ifdef CONFIG_IDE_PROC_FS
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002675static void idetape_add_settings(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002676{
2677 idetape_tape_t *tape = drive->driver_data;
2678
Borislav Petkovb6422012008-02-02 19:56:49 +01002679 ide_add_setting(drive, "buffer", SETTING_READ, TYPE_SHORT, 0, 0xffff,
2680 1, 2, (u16 *)&tape->caps[16], NULL);
Borislav Petkovb6422012008-02-02 19:56:49 +01002681 ide_add_setting(drive, "speed", SETTING_READ, TYPE_SHORT, 0, 0xffff,
2682 1, 1, (u16 *)&tape->caps[14], NULL);
Borislav Petkov54bb2072008-02-06 02:57:52 +01002683 ide_add_setting(drive, "stage", SETTING_READ, TYPE_INT, 0, 0xffff, 1,
2684 1024, &tape->stage_size, NULL);
2685 ide_add_setting(drive, "tdsc", SETTING_RW, TYPE_INT, IDETAPE_DSC_RW_MIN,
2686 IDETAPE_DSC_RW_MAX, 1000, HZ, &tape->best_dsc_rw_freq,
2687 NULL);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002688 ide_add_setting(drive, "dsc_overlap", SETTING_RW, TYPE_BYTE, 0, 1, 1,
2689 1, &drive->dsc_overlap, NULL);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002690 ide_add_setting(drive, "avg_speed", SETTING_READ, TYPE_INT, 0, 0xffff,
2691 1, 1, &tape->avg_speed, NULL);
Borislav Petkov8004a8c2008-02-06 02:57:51 +01002692 ide_add_setting(drive, "debug_mask", SETTING_RW, TYPE_INT, 0, 0xffff, 1,
2693 1, &tape->debug_mask, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002694}
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +02002695#else
2696static inline void idetape_add_settings(ide_drive_t *drive) { ; }
2697#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002698
2699/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +01002700 * The function below is called to:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002701 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +01002702 * 1. Initialize our various state variables.
2703 * 2. Ask the tape for its capabilities.
2704 * 3. Allocate a buffer which will be used for data transfer. The buffer size
2705 * is chosen based on the recommendation which we received in step 2.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002706 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +01002707 * Note that at this point ide.c already assigned us an irq, so that we can
2708 * queue requests here and wait for their completion.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002709 */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002710static void idetape_setup(ide_drive_t *drive, idetape_tape_t *tape, int minor)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002711{
Borislav Petkov83042b22008-04-27 15:38:27 +02002712 unsigned long t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002713 int speed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002714 int stage_size;
Borislav Petkov71071b82008-02-06 02:57:53 +01002715 u8 gcw[2];
Borislav Petkovb6422012008-02-02 19:56:49 +01002716 u16 *ctl = (u16 *)&tape->caps[12];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002717
Borislav Petkov54bb2072008-02-06 02:57:52 +01002718 spin_lock_init(&tape->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002719 drive->dsc_overlap = 1;
Bartlomiej Zolnierkiewicz4166c192008-02-01 23:09:30 +01002720 if (drive->hwif->host_flags & IDE_HFLAG_NO_DSC) {
2721 printk(KERN_INFO "ide-tape: %s: disabling DSC overlap\n",
2722 tape->name);
2723 drive->dsc_overlap = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002724 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002725 /* Seagate Travan drives do not support DSC overlap. */
2726 if (strstr(drive->id->model, "Seagate STT3401"))
2727 drive->dsc_overlap = 0;
2728 tape->minor = minor;
2729 tape->name[0] = 'h';
2730 tape->name[1] = 't';
2731 tape->name[2] = '0' + minor;
Borislav Petkov54abf372008-02-06 02:57:52 +01002732 tape->chrdev_dir = IDETAPE_DIR_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002733 tape->pc = tape->pc_stack;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002734 *((unsigned short *) &gcw) = drive->id->config;
Borislav Petkov71071b82008-02-06 02:57:53 +01002735
2736 /* Command packet DRQ type */
2737 if (((gcw[0] & 0x60) >> 5) == 1)
Borislav Petkov346331f2008-04-18 00:46:26 +02002738 set_bit(IDETAPE_FLAG_DRQ_INTERRUPT, &tape->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002739
Linus Torvalds1da177e2005-04-16 15:20:36 -07002740 idetape_get_inquiry_results(drive);
2741 idetape_get_mode_sense_results(drive);
Borislav Petkov3cffb9c2008-02-02 19:56:50 +01002742 ide_tape_get_bsize_from_bdesc(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002743 tape->user_bs_factor = 1;
Borislav Petkov54bb2072008-02-06 02:57:52 +01002744 tape->stage_size = *ctl * tape->blk_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002745 while (tape->stage_size > 0xffff) {
2746 printk(KERN_NOTICE "ide-tape: decreasing stage size\n");
Borislav Petkovb6422012008-02-02 19:56:49 +01002747 *ctl /= 2;
Borislav Petkov54bb2072008-02-06 02:57:52 +01002748 tape->stage_size = *ctl * tape->blk_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002749 }
2750 stage_size = tape->stage_size;
2751 tape->pages_per_stage = stage_size / PAGE_SIZE;
2752 if (stage_size % PAGE_SIZE) {
2753 tape->pages_per_stage++;
2754 tape->excess_bh_size = PAGE_SIZE - stage_size % PAGE_SIZE;
2755 }
2756
Borislav Petkov83042b22008-04-27 15:38:27 +02002757 /* select the "best" DSC read/write polling freq */
Borislav Petkovb6422012008-02-02 19:56:49 +01002758 speed = max(*(u16 *)&tape->caps[14], *(u16 *)&tape->caps[8]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002759
Borislav Petkov83042b22008-04-27 15:38:27 +02002760 t = (IDETAPE_FIFO_THRESHOLD * tape->stage_size * HZ) / (speed * 1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002761
2762 /*
Borislav Petkov3c98bf32008-02-06 02:57:53 +01002763 * Ensure that the number we got makes sense; limit it within
2764 * IDETAPE_DSC_RW_MIN and IDETAPE_DSC_RW_MAX.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002765 */
Borislav Petkov54bb2072008-02-06 02:57:52 +01002766 tape->best_dsc_rw_freq = max_t(unsigned long,
2767 min_t(unsigned long, t, IDETAPE_DSC_RW_MAX),
2768 IDETAPE_DSC_RW_MIN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002769 printk(KERN_INFO "ide-tape: %s <-> %s: %dKBps, %d*%dkB buffer, "
Borislav Petkov83042b22008-04-27 15:38:27 +02002770 "%lums tDSC%s\n",
Borislav Petkovb6422012008-02-02 19:56:49 +01002771 drive->name, tape->name, *(u16 *)&tape->caps[14],
2772 (*(u16 *)&tape->caps[16] * 512) / tape->stage_size,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002773 tape->stage_size / 1024,
Borislav Petkov54bb2072008-02-06 02:57:52 +01002774 tape->best_dsc_rw_freq * 1000 / HZ,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002775 drive->using_dma ? ", DMA":"");
2776
2777 idetape_add_settings(drive);
2778}
2779
Russell King4031bbe2006-01-06 11:41:00 +00002780static void ide_tape_remove(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002781{
2782 idetape_tape_t *tape = drive->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002783
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +02002784 ide_proc_unregister_driver(drive, tape->driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002785
2786 ide_unregister_region(tape->disk);
2787
2788 ide_tape_put(tape);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002789}
2790
2791static void ide_tape_release(struct kref *kref)
2792{
2793 struct ide_tape_obj *tape = to_ide_tape(kref);
2794 ide_drive_t *drive = tape->drive;
2795 struct gendisk *g = tape->disk;
2796
Borislav Petkov83042b22008-04-27 15:38:27 +02002797 BUG_ON(tape->merge_stage_size);
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02002798
Linus Torvalds1da177e2005-04-16 15:20:36 -07002799 drive->dsc_overlap = 0;
2800 drive->driver_data = NULL;
Tony Jonesdbc12722007-09-25 02:03:03 +02002801 device_destroy(idetape_sysfs_class, MKDEV(IDETAPE_MAJOR, tape->minor));
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002802 device_destroy(idetape_sysfs_class,
2803 MKDEV(IDETAPE_MAJOR, tape->minor + 128));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002804 idetape_devs[tape->minor] = NULL;
2805 g->private_data = NULL;
2806 put_disk(g);
2807 kfree(tape);
2808}
2809
Bartlomiej Zolnierkiewiczecfd80e2007-05-10 00:01:09 +02002810#ifdef CONFIG_IDE_PROC_FS
Linus Torvalds1da177e2005-04-16 15:20:36 -07002811static int proc_idetape_read_name
2812 (char *page, char **start, off_t off, int count, int *eof, void *data)
2813{
2814 ide_drive_t *drive = (ide_drive_t *) data;
2815 idetape_tape_t *tape = drive->driver_data;
2816 char *out = page;
2817 int len;
2818
2819 len = sprintf(out, "%s\n", tape->name);
2820 PROC_IDE_READ_RETURN(page, start, off, count, eof, len);
2821}
2822
2823static ide_proc_entry_t idetape_proc[] = {
2824 { "capacity", S_IFREG|S_IRUGO, proc_ide_read_capacity, NULL },
2825 { "name", S_IFREG|S_IRUGO, proc_idetape_read_name, NULL },
2826 { NULL, 0, NULL, NULL }
2827};
Linus Torvalds1da177e2005-04-16 15:20:36 -07002828#endif
2829
Russell King4031bbe2006-01-06 11:41:00 +00002830static int ide_tape_probe(ide_drive_t *);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002831
Linus Torvalds1da177e2005-04-16 15:20:36 -07002832static ide_driver_t idetape_driver = {
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02002833 .gen_driver = {
Laurent Riffard4ef3b8f2005-11-18 22:15:40 +01002834 .owner = THIS_MODULE,
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02002835 .name = "ide-tape",
2836 .bus = &ide_bus_type,
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02002837 },
Russell King4031bbe2006-01-06 11:41:00 +00002838 .probe = ide_tape_probe,
2839 .remove = ide_tape_remove,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002840 .version = IDETAPE_VERSION,
2841 .media = ide_tape,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002842 .supports_dsc_overlap = 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002843 .do_request = idetape_do_request,
2844 .end_request = idetape_end_request,
2845 .error = __ide_error,
2846 .abort = __ide_abort,
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +02002847#ifdef CONFIG_IDE_PROC_FS
Linus Torvalds1da177e2005-04-16 15:20:36 -07002848 .proc = idetape_proc,
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +02002849#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002850};
2851
Borislav Petkov3c98bf32008-02-06 02:57:53 +01002852/* Our character device supporting functions, passed to register_chrdev. */
Arjan van de Ven2b8693c2007-02-12 00:55:32 -08002853static const struct file_operations idetape_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002854 .owner = THIS_MODULE,
2855 .read = idetape_chrdev_read,
2856 .write = idetape_chrdev_write,
2857 .ioctl = idetape_chrdev_ioctl,
2858 .open = idetape_chrdev_open,
2859 .release = idetape_chrdev_release,
2860};
2861
2862static int idetape_open(struct inode *inode, struct file *filp)
2863{
2864 struct gendisk *disk = inode->i_bdev->bd_disk;
2865 struct ide_tape_obj *tape;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002866
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002867 tape = ide_tape_get(disk);
2868 if (!tape)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002869 return -ENXIO;
2870
Linus Torvalds1da177e2005-04-16 15:20:36 -07002871 return 0;
2872}
2873
2874static int idetape_release(struct inode *inode, struct file *filp)
2875{
2876 struct gendisk *disk = inode->i_bdev->bd_disk;
2877 struct ide_tape_obj *tape = ide_tape_g(disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002878
2879 ide_tape_put(tape);
2880
2881 return 0;
2882}
2883
2884static int idetape_ioctl(struct inode *inode, struct file *file,
2885 unsigned int cmd, unsigned long arg)
2886{
2887 struct block_device *bdev = inode->i_bdev;
2888 struct ide_tape_obj *tape = ide_tape_g(bdev->bd_disk);
2889 ide_drive_t *drive = tape->drive;
2890 int err = generic_ide_ioctl(drive, file, bdev, cmd, arg);
2891 if (err == -EINVAL)
2892 err = idetape_blkdev_ioctl(drive, cmd, arg);
2893 return err;
2894}
2895
2896static struct block_device_operations idetape_block_ops = {
2897 .owner = THIS_MODULE,
2898 .open = idetape_open,
2899 .release = idetape_release,
2900 .ioctl = idetape_ioctl,
2901};
2902
Russell King4031bbe2006-01-06 11:41:00 +00002903static int ide_tape_probe(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002904{
2905 idetape_tape_t *tape;
2906 struct gendisk *g;
2907 int minor;
2908
2909 if (!strstr("ide-tape", drive->driver_req))
2910 goto failed;
2911 if (!drive->present)
2912 goto failed;
2913 if (drive->media != ide_tape)
2914 goto failed;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002915 if (!idetape_identify_device(drive)) {
2916 printk(KERN_ERR "ide-tape: %s: not supported by this version of"
2917 " the driver\n", drive->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002918 goto failed;
2919 }
2920 if (drive->scsi) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002921 printk(KERN_INFO "ide-tape: passing drive %s to ide-scsi"
2922 " emulation.\n", drive->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002923 goto failed;
2924 }
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002925 tape = kzalloc(sizeof(idetape_tape_t), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002926 if (tape == NULL) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002927 printk(KERN_ERR "ide-tape: %s: Can't allocate a tape struct\n",
2928 drive->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002929 goto failed;
2930 }
2931
2932 g = alloc_disk(1 << PARTN_BITS);
2933 if (!g)
2934 goto out_free_tape;
2935
2936 ide_init_disk(g, drive);
2937
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +02002938 ide_proc_register_driver(drive, &idetape_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002939
Linus Torvalds1da177e2005-04-16 15:20:36 -07002940 kref_init(&tape->kref);
2941
2942 tape->drive = drive;
2943 tape->driver = &idetape_driver;
2944 tape->disk = g;
2945
2946 g->private_data = &tape->driver;
2947
2948 drive->driver_data = tape;
2949
Arjan van de Vencf8b8972006-03-23 03:00:45 -08002950 mutex_lock(&idetape_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002951 for (minor = 0; idetape_devs[minor]; minor++)
2952 ;
2953 idetape_devs[minor] = tape;
Arjan van de Vencf8b8972006-03-23 03:00:45 -08002954 mutex_unlock(&idetape_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002955
2956 idetape_setup(drive, tape, minor);
2957
Tony Jonesdbc12722007-09-25 02:03:03 +02002958 device_create(idetape_sysfs_class, &drive->gendev,
2959 MKDEV(IDETAPE_MAJOR, minor), "%s", tape->name);
2960 device_create(idetape_sysfs_class, &drive->gendev,
2961 MKDEV(IDETAPE_MAJOR, minor + 128), "n%s", tape->name);
Will Dysond5dee802005-09-16 02:55:07 -07002962
Linus Torvalds1da177e2005-04-16 15:20:36 -07002963 g->fops = &idetape_block_ops;
2964 ide_register_region(g);
2965
2966 return 0;
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02002967
Linus Torvalds1da177e2005-04-16 15:20:36 -07002968out_free_tape:
2969 kfree(tape);
2970failed:
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02002971 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002972}
2973
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002974static void __exit idetape_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002975{
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02002976 driver_unregister(&idetape_driver.gen_driver);
Will Dysond5dee802005-09-16 02:55:07 -07002977 class_destroy(idetape_sysfs_class);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002978 unregister_chrdev(IDETAPE_MAJOR, "ht");
2979}
2980
Bartlomiej Zolnierkiewicz17514e82005-11-19 22:24:35 +01002981static int __init idetape_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002982{
Will Dysond5dee802005-09-16 02:55:07 -07002983 int error = 1;
2984 idetape_sysfs_class = class_create(THIS_MODULE, "ide_tape");
2985 if (IS_ERR(idetape_sysfs_class)) {
2986 idetape_sysfs_class = NULL;
2987 printk(KERN_ERR "Unable to create sysfs class for ide tapes\n");
2988 error = -EBUSY;
2989 goto out;
2990 }
2991
Linus Torvalds1da177e2005-04-16 15:20:36 -07002992 if (register_chrdev(IDETAPE_MAJOR, "ht", &idetape_fops)) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002993 printk(KERN_ERR "ide-tape: Failed to register chrdev"
2994 " interface\n");
Will Dysond5dee802005-09-16 02:55:07 -07002995 error = -EBUSY;
2996 goto out_free_class;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002997 }
Will Dysond5dee802005-09-16 02:55:07 -07002998
2999 error = driver_register(&idetape_driver.gen_driver);
3000 if (error)
3001 goto out_free_driver;
3002
3003 return 0;
3004
3005out_free_driver:
3006 driver_unregister(&idetape_driver.gen_driver);
3007out_free_class:
3008 class_destroy(idetape_sysfs_class);
3009out:
3010 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003011}
3012
Kay Sievers263756e2005-12-12 18:03:44 +01003013MODULE_ALIAS("ide:*m-tape*");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003014module_init(idetape_init);
3015module_exit(idetape_exit);
3016MODULE_ALIAS_CHARDEV_MAJOR(IDETAPE_MAJOR);
Borislav Petkov9c145762008-02-06 02:57:54 +01003017MODULE_DESCRIPTION("ATAPI Streaming TAPE Driver");
3018MODULE_LICENSE("GPL");