blob: a81644b2473ee559251e9fc01d119605f62716ea [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 *****************************/
75
76
77/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +010078 * Pipelined mode parameters.
Linus Torvalds1da177e2005-04-16 15:20:36 -070079 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +010080 * We try to use the minimum number of stages which is enough to keep the tape
81 * constantly streaming. To accomplish that, we implement a feedback loop around
82 * the maximum number of stages:
Linus Torvalds1da177e2005-04-16 15:20:36 -070083 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +010084 * We start from MIN maximum stages (we will not even use MIN stages if we don't
85 * need them), increment it by RATE*(MAX-MIN) whenever we sense that the
86 * pipeline is empty, until we reach the optimum value or until we reach MAX.
Linus Torvalds1da177e2005-04-16 15:20:36 -070087 */
88#define IDETAPE_MIN_PIPELINE_STAGES 1
89#define IDETAPE_MAX_PIPELINE_STAGES 400
90#define IDETAPE_INCREASE_STAGES_RATE 20
91
92/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +010093 * After each failed packet command we issue a request sense command and retry
94 * the packet command IDETAPE_MAX_PC_RETRIES times.
Linus Torvalds1da177e2005-04-16 15:20:36 -070095 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +010096 * Setting IDETAPE_MAX_PC_RETRIES to 0 will disable retries.
Linus Torvalds1da177e2005-04-16 15:20:36 -070097 */
98#define IDETAPE_MAX_PC_RETRIES 3
99
100/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100101 * With each packet command, we allocate a buffer of IDETAPE_PC_BUFFER_SIZE
102 * bytes. This is used for several packet commands (Not for READ/WRITE commands)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 */
104#define IDETAPE_PC_BUFFER_SIZE 256
105
106/*
107 * In various places in the driver, we need to allocate storage
108 * for packet commands and requests, which will remain valid while
109 * we leave the driver to wait for an interrupt or a timeout event.
110 */
111#define IDETAPE_PC_STACK (10 + IDETAPE_MAX_PC_RETRIES)
112
113/*
114 * Some drives (for example, Seagate STT3401A Travan) require a very long
115 * timeout, because they don't return an interrupt or clear their busy bit
116 * until after the command completes (even retension commands).
117 */
118#define IDETAPE_WAIT_CMD (900*HZ)
119
120/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100121 * The following parameter is used to select the point in the internal tape fifo
122 * in which we will start to refill the buffer. Decreasing the following
123 * parameter will improve the system's latency and interactive response, while
124 * using a high value might improve system throughput.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 */
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100126#define IDETAPE_FIFO_THRESHOLD 2
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127
128/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100129 * DSC polling parameters.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100131 * Polling for DSC (a single bit in the status register) is a very important
132 * function in ide-tape. There are two cases in which we poll for DSC:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100134 * 1. Before a read/write packet command, to ensure that we can transfer data
135 * from/to the tape's data buffers, without causing an actual media access.
136 * In case the tape is not ready yet, we take out our request from the device
137 * request queue, so that ide.c could service requests from the other device
138 * on the same interface in the meantime.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100140 * 2. After the successful initialization of a "media access packet command",
141 * which is a command that can take a long time to complete (the interval can
142 * range from several seconds to even an hour). Again, we postpone our request
143 * in the middle to free the bus for the other device. The polling frequency
144 * here should be lower than the read/write frequency since those media access
145 * commands are slow. We start from a "fast" frequency - IDETAPE_DSC_MA_FAST
146 * (1 second), and if we don't receive DSC after IDETAPE_DSC_MA_THRESHOLD
147 * (5 min), we switch it to a lower frequency - IDETAPE_DSC_MA_SLOW (1 min).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100149 * We also set a timeout for the timer, in case something goes wrong. The
150 * timeout should be longer then the maximum execution time of a tape operation.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 */
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100152
153/* DSC timings. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154#define IDETAPE_DSC_RW_MIN 5*HZ/100 /* 50 msec */
155#define IDETAPE_DSC_RW_MAX 40*HZ/100 /* 400 msec */
156#define IDETAPE_DSC_RW_TIMEOUT 2*60*HZ /* 2 minutes */
157#define IDETAPE_DSC_MA_FAST 2*HZ /* 2 seconds */
158#define IDETAPE_DSC_MA_THRESHOLD 5*60*HZ /* 5 minutes */
159#define IDETAPE_DSC_MA_SLOW 30*HZ /* 30 seconds */
160#define IDETAPE_DSC_MA_TIMEOUT 2*60*60*HZ /* 2 hours */
161
162/*************************** End of tunable parameters ***********************/
163
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100164/* Read/Write error simulation */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165#define SIMULATE_ERRORS 0
166
Borislav Petkov54abf372008-02-06 02:57:52 +0100167/* tape directions */
168enum {
169 IDETAPE_DIR_NONE = (1 << 0),
170 IDETAPE_DIR_READ = (1 << 1),
171 IDETAPE_DIR_WRITE = (1 << 2),
172};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173
174struct idetape_bh {
Stephen Rothwellab057962007-08-01 23:46:44 +0200175 u32 b_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 atomic_t b_count;
177 struct idetape_bh *b_reqnext;
178 char *b_data;
179};
180
Borislav Petkov03056b92008-04-18 00:46:26 +0200181/* Tape door status */
182#define DOOR_UNLOCKED 0
183#define DOOR_LOCKED 1
184#define DOOR_EXPLICITLY_LOCKED 2
185
186/* Some defines for the SPACE command */
187#define IDETAPE_SPACE_OVER_FILEMARK 1
188#define IDETAPE_SPACE_TO_EOD 3
189
190/* Some defines for the LOAD UNLOAD command */
191#define IDETAPE_LU_LOAD_MASK 1
192#define IDETAPE_LU_RETENSION_MASK 2
193#define IDETAPE_LU_EOT_MASK 4
194
195/*
196 * Special requests for our block device strategy routine.
197 *
198 * In order to service a character device command, we add special requests to
199 * the tail of our block device request queue and wait for their completion.
200 */
201
202enum {
203 REQ_IDETAPE_PC1 = (1 << 0), /* packet command (first stage) */
204 REQ_IDETAPE_PC2 = (1 << 1), /* packet command (second stage) */
205 REQ_IDETAPE_READ = (1 << 2),
206 REQ_IDETAPE_WRITE = (1 << 3),
207};
208
209/* Error codes returned in rq->errors to the higher part of the driver. */
210#define IDETAPE_ERROR_GENERAL 101
211#define IDETAPE_ERROR_FILEMARK 102
212#define IDETAPE_ERROR_EOD 103
213
214/* Structures related to the SELECT SENSE / MODE SENSE packet commands. */
215#define IDETAPE_BLOCK_DESCRIPTOR 0
216#define IDETAPE_CAPABILITIES_PAGE 0x2a
217
Borislav Petkov346331f2008-04-18 00:46:26 +0200218/* Tape flag bits values. */
219enum {
220 IDETAPE_FLAG_IGNORE_DSC = (1 << 0),
221 /* 0 When the tape position is unknown */
222 IDETAPE_FLAG_ADDRESS_VALID = (1 << 1),
223 /* Device already opened */
Borislav Petkov42d54682008-04-27 15:38:27 +0200224 IDETAPE_FLAG_BUSY = (1 << 2),
Borislav Petkov346331f2008-04-18 00:46:26 +0200225 /* Attempt to auto-detect the current user block size */
Borislav Petkov42d54682008-04-27 15:38:27 +0200226 IDETAPE_FLAG_DETECT_BS = (1 << 3),
Borislav Petkov346331f2008-04-18 00:46:26 +0200227 /* Currently on a filemark */
Borislav Petkov42d54682008-04-27 15:38:27 +0200228 IDETAPE_FLAG_FILEMARK = (1 << 4),
Borislav Petkov346331f2008-04-18 00:46:26 +0200229 /* DRQ interrupt device */
Borislav Petkov42d54682008-04-27 15:38:27 +0200230 IDETAPE_FLAG_DRQ_INTERRUPT = (1 << 5),
Borislav Petkov346331f2008-04-18 00:46:26 +0200231 /* 0 = no tape is loaded, so we don't rewind after ejecting */
Borislav Petkov42d54682008-04-27 15:38:27 +0200232 IDETAPE_FLAG_MEDIUM_PRESENT = (1 << 6),
Borislav Petkov346331f2008-04-18 00:46:26 +0200233};
234
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100235/* A pipeline stage. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236typedef struct idetape_stage_s {
237 struct request rq; /* The corresponding request */
238 struct idetape_bh *bh; /* The data buffers */
239 struct idetape_stage_s *next; /* Pointer to the next stage */
240} idetape_stage_t;
241
242/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100243 * Most of our global data which we need to save even as we leave the driver due
244 * to an interrupt or a timer event is stored in the struct defined below.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 */
246typedef struct ide_tape_obj {
247 ide_drive_t *drive;
248 ide_driver_t *driver;
249 struct gendisk *disk;
250 struct kref kref;
251
252 /*
253 * Since a typical character device operation requires more
254 * than one packet command, we provide here enough memory
255 * for the maximum of interconnected packet commands.
256 * The packet commands are stored in the circular array pc_stack.
257 * pc_stack_index points to the last used entry, and warps around
258 * to the start when we get to the last array entry.
259 *
260 * pc points to the current processed packet command.
261 *
262 * failed_pc points to the last failed packet command, or contains
263 * NULL if we do not need to retry any packet command. This is
264 * required since an additional packet command is needed before the
265 * retry, to get detailed information on what went wrong.
266 */
267 /* Current packet command */
Borislav Petkovd236d742008-04-18 00:46:27 +0200268 struct ide_atapi_pc *pc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 /* Last failed packet command */
Borislav Petkovd236d742008-04-18 00:46:27 +0200270 struct ide_atapi_pc *failed_pc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 /* Packet command stack */
Borislav Petkovd236d742008-04-18 00:46:27 +0200272 struct ide_atapi_pc pc_stack[IDETAPE_PC_STACK];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 /* Next free packet command storage space */
274 int pc_stack_index;
275 struct request rq_stack[IDETAPE_PC_STACK];
276 /* We implement a circular array */
277 int rq_stack_index;
278
279 /*
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100280 * DSC polling variables.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100282 * While polling for DSC we use postponed_rq to postpone the current
283 * request so that ide.c will be able to service pending requests on the
284 * other device. Note that at most we will have only one DSC (usually
285 * data transfer) request in the device request queue. Additional
286 * requests can be queued in our internal pipeline, but they will be
287 * visible to ide.c only one at a time.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 */
289 struct request *postponed_rq;
290 /* The time in which we started polling for DSC */
291 unsigned long dsc_polling_start;
292 /* Timer used to poll for dsc */
293 struct timer_list dsc_timer;
294 /* Read/Write dsc polling frequency */
Borislav Petkov54bb2072008-02-06 02:57:52 +0100295 unsigned long best_dsc_rw_freq;
296 unsigned long dsc_poll_freq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 unsigned long dsc_timeout;
298
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100299 /* Read position information */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 u8 partition;
301 /* Current block */
Borislav Petkov54bb2072008-02-06 02:57:52 +0100302 unsigned int first_frame;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100304 /* Last error information */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 u8 sense_key, asc, ascq;
306
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100307 /* Character device operation */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 unsigned int minor;
309 /* device name */
310 char name[4];
311 /* Current character device data transfer direction */
Borislav Petkov54abf372008-02-06 02:57:52 +0100312 u8 chrdev_dir;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313
Borislav Petkov54bb2072008-02-06 02:57:52 +0100314 /* tape block size, usually 512 or 1024 bytes */
315 unsigned short blk_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 int user_bs_factor;
Borislav Petkovb6422012008-02-02 19:56:49 +0100317
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 /* Copy of the tape's Capabilities and Mechanical Page */
Borislav Petkovb6422012008-02-02 19:56:49 +0100319 u8 caps[20];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320
321 /*
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100322 * Active data transfer request parameters.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100324 * At most, there is only one ide-tape originated data transfer request
325 * in the device request queue. This allows ide.c to easily service
326 * requests from the other device when we postpone our active request.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 */
Borislav Petkov83042b22008-04-27 15:38:27 +0200328
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100329 /* Data buffer size chosen based on the tape's recommendation */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 int stage_size;
331 idetape_stage_t *merge_stage;
332 int merge_stage_size;
333 struct idetape_bh *bh;
334 char *b_data;
335 int b_count;
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100336
Borislav Petkov83042b22008-04-27 15:38:27 +0200337 /* Pipeline parameters. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 int pages_per_stage;
339 /* Wasted space in each stage */
340 int excess_bh_size;
341
342 /* Status/Action flags: long for set_bit */
343 unsigned long flags;
344 /* protects the ide-tape queue */
Borislav Petkov54bb2072008-02-06 02:57:52 +0100345 spinlock_t lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100347 /* Measures average tape speed */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 unsigned long avg_time;
349 int avg_size;
350 int avg_speed;
351
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 /* the door is currently locked */
353 int door_locked;
354 /* the tape hardware is write protected */
355 char drv_write_prot;
356 /* the tape is write protected (hardware or opened as read-only) */
357 char write_prot;
358
Borislav Petkov8004a8c2008-02-06 02:57:51 +0100359 u32 debug_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360} idetape_tape_t;
361
Arjan van de Vencf8b8972006-03-23 03:00:45 -0800362static DEFINE_MUTEX(idetape_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363
Will Dysond5dee802005-09-16 02:55:07 -0700364static struct class *idetape_sysfs_class;
365
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366#define to_ide_tape(obj) container_of(obj, struct ide_tape_obj, kref)
367
368#define ide_tape_g(disk) \
369 container_of((disk)->private_data, struct ide_tape_obj, driver)
370
371static struct ide_tape_obj *ide_tape_get(struct gendisk *disk)
372{
373 struct ide_tape_obj *tape = NULL;
374
Arjan van de Vencf8b8972006-03-23 03:00:45 -0800375 mutex_lock(&idetape_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 tape = ide_tape_g(disk);
377 if (tape)
378 kref_get(&tape->kref);
Arjan van de Vencf8b8972006-03-23 03:00:45 -0800379 mutex_unlock(&idetape_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 return tape;
381}
382
383static void ide_tape_release(struct kref *);
384
385static void ide_tape_put(struct ide_tape_obj *tape)
386{
Arjan van de Vencf8b8972006-03-23 03:00:45 -0800387 mutex_lock(&idetape_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 kref_put(&tape->kref, ide_tape_release);
Arjan van de Vencf8b8972006-03-23 03:00:45 -0800389 mutex_unlock(&idetape_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390}
391
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100393 * The variables below are used for the character device interface. Additional
394 * state variables are defined in our ide_drive_t structure.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100396static struct ide_tape_obj *idetape_devs[MAX_HWIFS * MAX_DRIVES];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397
398#define ide_tape_f(file) ((file)->private_data)
399
400static struct ide_tape_obj *ide_tape_chrdev_get(unsigned int i)
401{
402 struct ide_tape_obj *tape = NULL;
403
Arjan van de Vencf8b8972006-03-23 03:00:45 -0800404 mutex_lock(&idetape_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 tape = idetape_devs[i];
406 if (tape)
407 kref_get(&tape->kref);
Arjan van de Vencf8b8972006-03-23 03:00:45 -0800408 mutex_unlock(&idetape_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 return tape;
410}
411
Borislav Petkovd236d742008-04-18 00:46:27 +0200412static void idetape_input_buffers(ide_drive_t *drive, struct ide_atapi_pc *pc,
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100413 unsigned int bcount)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414{
415 struct idetape_bh *bh = pc->bh;
416 int count;
417
418 while (bcount) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 if (bh == NULL) {
420 printk(KERN_ERR "ide-tape: bh == NULL in "
421 "idetape_input_buffers\n");
Bartlomiej Zolnierkiewicz7616c0a2008-04-18 00:46:26 +0200422 ide_atapi_discard_data(drive, bcount);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 return;
424 }
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100425 count = min(
426 (unsigned int)(bh->b_size - atomic_read(&bh->b_count)),
427 bcount);
428 HWIF(drive)->atapi_input_bytes(drive, bh->b_data +
429 atomic_read(&bh->b_count), count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 bcount -= count;
431 atomic_add(count, &bh->b_count);
432 if (atomic_read(&bh->b_count) == bh->b_size) {
433 bh = bh->b_reqnext;
434 if (bh)
435 atomic_set(&bh->b_count, 0);
436 }
437 }
438 pc->bh = bh;
439}
440
Borislav Petkovd236d742008-04-18 00:46:27 +0200441static void idetape_output_buffers(ide_drive_t *drive, struct ide_atapi_pc *pc,
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100442 unsigned int bcount)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443{
444 struct idetape_bh *bh = pc->bh;
445 int count;
446
447 while (bcount) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 if (bh == NULL) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100449 printk(KERN_ERR "ide-tape: bh == NULL in %s\n",
450 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 return;
452 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 count = min((unsigned int)pc->b_count, (unsigned int)bcount);
454 HWIF(drive)->atapi_output_bytes(drive, pc->b_data, count);
455 bcount -= count;
456 pc->b_data += count;
457 pc->b_count -= count;
458 if (!pc->b_count) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100459 bh = bh->b_reqnext;
460 pc->bh = bh;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 if (bh) {
462 pc->b_data = bh->b_data;
463 pc->b_count = atomic_read(&bh->b_count);
464 }
465 }
466 }
467}
468
Borislav Petkovd236d742008-04-18 00:46:27 +0200469static void idetape_update_buffers(struct ide_atapi_pc *pc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470{
471 struct idetape_bh *bh = pc->bh;
472 int count;
Borislav Petkovd236d742008-04-18 00:46:27 +0200473 unsigned int bcount = pc->xferred;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474
Borislav Petkov346331f2008-04-18 00:46:26 +0200475 if (pc->flags & PC_FLAG_WRITING)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 return;
477 while (bcount) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 if (bh == NULL) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100479 printk(KERN_ERR "ide-tape: bh == NULL in %s\n",
480 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 return;
482 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 count = min((unsigned int)bh->b_size, (unsigned int)bcount);
484 atomic_set(&bh->b_count, count);
485 if (atomic_read(&bh->b_count) == bh->b_size)
486 bh = bh->b_reqnext;
487 bcount -= count;
488 }
489 pc->bh = bh;
490}
491
492/*
493 * idetape_next_pc_storage returns a pointer to a place in which we can
494 * safely store a packet command, even though we intend to leave the
495 * driver. A storage space for a maximum of IDETAPE_PC_STACK packet
496 * commands is allocated at initialization time.
497 */
Borislav Petkovd236d742008-04-18 00:46:27 +0200498static struct ide_atapi_pc *idetape_next_pc_storage(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499{
500 idetape_tape_t *tape = drive->driver_data;
501
Borislav Petkov8004a8c2008-02-06 02:57:51 +0100502 debug_log(DBG_PCRQ_STACK, "pc_stack_index=%d\n", tape->pc_stack_index);
503
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 if (tape->pc_stack_index == IDETAPE_PC_STACK)
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100505 tape->pc_stack_index = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 return (&tape->pc_stack[tape->pc_stack_index++]);
507}
508
509/*
510 * idetape_next_rq_storage is used along with idetape_next_pc_storage.
511 * Since we queue packet commands in the request queue, we need to
512 * allocate a request, along with the allocation of a packet command.
513 */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100514
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515/**************************************************************
516 * *
517 * This should get fixed to use kmalloc(.., GFP_ATOMIC) *
518 * followed later on by kfree(). -ml *
519 * *
520 **************************************************************/
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100521
522static struct request *idetape_next_rq_storage(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523{
524 idetape_tape_t *tape = drive->driver_data;
525
Borislav Petkov8004a8c2008-02-06 02:57:51 +0100526 debug_log(DBG_PCRQ_STACK, "rq_stack_index=%d\n", tape->rq_stack_index);
527
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 if (tape->rq_stack_index == IDETAPE_PC_STACK)
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100529 tape->rq_stack_index = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 return (&tape->rq_stack[tape->rq_stack_index++]);
531}
532
Borislav Petkovd236d742008-04-18 00:46:27 +0200533static void idetape_init_pc(struct ide_atapi_pc *pc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534{
535 memset(pc->c, 0, 12);
536 pc->retries = 0;
537 pc->flags = 0;
Borislav Petkovd236d742008-04-18 00:46:27 +0200538 pc->req_xfer = 0;
539 pc->buf = pc->pc_buf;
540 pc->buf_size = IDETAPE_PC_BUFFER_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 pc->bh = NULL;
542 pc->b_data = NULL;
543}
544
545/*
Borislav Petkov1b5db432008-02-02 19:56:48 +0100546 * called on each failed packet command retry to analyze the request sense. We
547 * currently do not utilize this information.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 */
Borislav Petkov1b5db432008-02-02 19:56:48 +0100549static void idetape_analyze_error(ide_drive_t *drive, u8 *sense)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550{
551 idetape_tape_t *tape = drive->driver_data;
Borislav Petkovd236d742008-04-18 00:46:27 +0200552 struct ide_atapi_pc *pc = tape->failed_pc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553
Borislav Petkov1b5db432008-02-02 19:56:48 +0100554 tape->sense_key = sense[2] & 0xF;
555 tape->asc = sense[12];
556 tape->ascq = sense[13];
Borislav Petkov8004a8c2008-02-06 02:57:51 +0100557
558 debug_log(DBG_ERR, "pc = %x, sense key = %x, asc = %x, ascq = %x\n",
559 pc->c[0], tape->sense_key, tape->asc, tape->ascq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560
Borislav Petkovd236d742008-04-18 00:46:27 +0200561 /* Correct pc->xferred by asking the tape. */
Borislav Petkov346331f2008-04-18 00:46:26 +0200562 if (pc->flags & PC_FLAG_DMA_ERROR) {
Borislav Petkovd236d742008-04-18 00:46:27 +0200563 pc->xferred = pc->req_xfer -
Borislav Petkov54bb2072008-02-06 02:57:52 +0100564 tape->blk_size *
Borislav Petkov860ff5e2008-02-02 19:56:50 +0100565 be32_to_cpu(get_unaligned((u32 *)&sense[3]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566 idetape_update_buffers(pc);
567 }
568
569 /*
570 * If error was the result of a zero-length read or write command,
571 * with sense key=5, asc=0x22, ascq=0, let it slide. Some drives
572 * (i.e. Seagate STT3401A Travan) don't support 0-length read/writes.
573 */
Borislav Petkov90699ce2008-02-02 19:56:50 +0100574 if ((pc->c[0] == READ_6 || pc->c[0] == WRITE_6)
Borislav Petkov1b5db432008-02-02 19:56:48 +0100575 /* length == 0 */
576 && pc->c[4] == 0 && pc->c[3] == 0 && pc->c[2] == 0) {
577 if (tape->sense_key == 5) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 /* don't report an error, everything's ok */
579 pc->error = 0;
580 /* don't retry read/write */
Borislav Petkov346331f2008-04-18 00:46:26 +0200581 pc->flags |= PC_FLAG_ABORT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 }
583 }
Borislav Petkov90699ce2008-02-02 19:56:50 +0100584 if (pc->c[0] == READ_6 && (sense[2] & 0x80)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 pc->error = IDETAPE_ERROR_FILEMARK;
Borislav Petkov346331f2008-04-18 00:46:26 +0200586 pc->flags |= PC_FLAG_ABORT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 }
Borislav Petkov90699ce2008-02-02 19:56:50 +0100588 if (pc->c[0] == WRITE_6) {
Borislav Petkov1b5db432008-02-02 19:56:48 +0100589 if ((sense[2] & 0x40) || (tape->sense_key == 0xd
590 && tape->asc == 0x0 && tape->ascq == 0x2)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 pc->error = IDETAPE_ERROR_EOD;
Borislav Petkov346331f2008-04-18 00:46:26 +0200592 pc->flags |= PC_FLAG_ABORT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 }
594 }
Borislav Petkov90699ce2008-02-02 19:56:50 +0100595 if (pc->c[0] == READ_6 || pc->c[0] == WRITE_6) {
Borislav Petkov1b5db432008-02-02 19:56:48 +0100596 if (tape->sense_key == 8) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 pc->error = IDETAPE_ERROR_EOD;
Borislav Petkov346331f2008-04-18 00:46:26 +0200598 pc->flags |= PC_FLAG_ABORT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 }
Borislav Petkov346331f2008-04-18 00:46:26 +0200600 if (!(pc->flags & PC_FLAG_ABORT) &&
Borislav Petkovd236d742008-04-18 00:46:27 +0200601 pc->xferred)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 pc->retries = IDETAPE_MAX_PC_RETRIES + 1;
603 }
604}
605
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100606/* Free a stage along with its related buffers completely. */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100607static void __idetape_kfree_stage(idetape_stage_t *stage)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608{
609 struct idetape_bh *prev_bh, *bh = stage->bh;
610 int size;
611
612 while (bh != NULL) {
613 if (bh->b_data != NULL) {
614 size = (int) bh->b_size;
615 while (size > 0) {
616 free_page((unsigned long) bh->b_data);
617 size -= PAGE_SIZE;
618 bh->b_data += PAGE_SIZE;
619 }
620 }
621 prev_bh = bh;
622 bh = bh->b_reqnext;
623 kfree(prev_bh);
624 }
625 kfree(stage);
626}
627
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100629 * Finish servicing a request and insert a pending pipeline request into the
630 * main device queue.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 */
632static int idetape_end_request(ide_drive_t *drive, int uptodate, int nr_sects)
633{
634 struct request *rq = HWGROUP(drive)->rq;
635 idetape_tape_t *tape = drive->driver_data;
636 unsigned long flags;
637 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638
Borislav Petkov8004a8c2008-02-06 02:57:51 +0100639 debug_log(DBG_PROCS, "Enter %s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640
641 switch (uptodate) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100642 case 0: error = IDETAPE_ERROR_GENERAL; break;
643 case 1: error = 0; break;
644 default: error = uptodate;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645 }
646 rq->errors = error;
647 if (error)
648 tape->failed_pc = NULL;
649
Bartlomiej Zolnierkiewicz36872212008-01-26 20:13:10 +0100650 if (!blk_special_request(rq)) {
651 ide_end_request(drive, uptodate, nr_sects);
652 return 0;
653 }
654
Borislav Petkov54bb2072008-02-06 02:57:52 +0100655 spin_lock_irqsave(&tape->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 ide_end_drive_cmd(drive, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658
Borislav Petkov54bb2072008-02-06 02:57:52 +0100659 spin_unlock_irqrestore(&tape->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 return 0;
661}
662
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100663static ide_startstop_t idetape_request_sense_callback(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664{
665 idetape_tape_t *tape = drive->driver_data;
666
Borislav Petkov8004a8c2008-02-06 02:57:51 +0100667 debug_log(DBG_PROCS, "Enter %s\n", __func__);
668
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669 if (!tape->pc->error) {
Borislav Petkovd236d742008-04-18 00:46:27 +0200670 idetape_analyze_error(drive, tape->pc->buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671 idetape_end_request(drive, 1, 0);
672 } else {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100673 printk(KERN_ERR "ide-tape: Error in REQUEST SENSE itself - "
674 "Aborting request!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675 idetape_end_request(drive, 0, 0);
676 }
677 return ide_stopped;
678}
679
Borislav Petkovd236d742008-04-18 00:46:27 +0200680static void idetape_create_request_sense_cmd(struct ide_atapi_pc *pc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681{
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100682 idetape_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +0100683 pc->c[0] = REQUEST_SENSE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684 pc->c[4] = 20;
Borislav Petkovd236d742008-04-18 00:46:27 +0200685 pc->req_xfer = 20;
686 pc->idetape_callback = &idetape_request_sense_callback;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687}
688
689static void idetape_init_rq(struct request *rq, u8 cmd)
690{
691 memset(rq, 0, sizeof(*rq));
Jens Axboe4aff5e22006-08-10 08:44:47 +0200692 rq->cmd_type = REQ_TYPE_SPECIAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 rq->cmd[0] = cmd;
694}
695
696/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100697 * Generate a new packet command request in front of the request queue, before
698 * the current request, so that it will be processed immediately, on the next
699 * pass through the driver. The function below is called from the request
700 * handling part of the driver (the "bottom" part). Safe storage for the request
701 * should be allocated with ide_tape_next_{pc,rq}_storage() prior to that.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100703 * Memory for those requests is pre-allocated at initialization time, and is
704 * limited to IDETAPE_PC_STACK requests. We assume that we have enough space for
705 * the maximum possible number of inter-dependent packet commands.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100707 * The higher level of the driver - The ioctl handler and the character device
708 * handling functions should queue request to the lower level part and wait for
709 * their completion using idetape_queue_pc_tail or idetape_queue_rw_tail.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710 */
Borislav Petkovd236d742008-04-18 00:46:27 +0200711static void idetape_queue_pc_head(ide_drive_t *drive, struct ide_atapi_pc *pc,
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100712 struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713{
714 struct ide_tape_obj *tape = drive->driver_data;
715
716 idetape_init_rq(rq, REQ_IDETAPE_PC1);
717 rq->buffer = (char *) pc;
718 rq->rq_disk = tape->disk;
719 (void) ide_do_drive_cmd(drive, rq, ide_preempt);
720}
721
722/*
723 * idetape_retry_pc is called when an error was detected during the
724 * last packet command. We queue a request sense packet command in
725 * the head of the request list.
726 */
727static ide_startstop_t idetape_retry_pc (ide_drive_t *drive)
728{
729 idetape_tape_t *tape = drive->driver_data;
Borislav Petkovd236d742008-04-18 00:46:27 +0200730 struct ide_atapi_pc *pc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 struct request *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732
Bartlomiej Zolnierkiewicz64a57fe2008-02-06 02:57:51 +0100733 (void)ide_read_error(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 pc = idetape_next_pc_storage(drive);
735 rq = idetape_next_rq_storage(drive);
736 idetape_create_request_sense_cmd(pc);
Borislav Petkov346331f2008-04-18 00:46:26 +0200737 set_bit(IDETAPE_FLAG_IGNORE_DSC, &tape->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738 idetape_queue_pc_head(drive, pc, rq);
739 return ide_stopped;
740}
741
742/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100743 * Postpone the current request so that ide.c will be able to service requests
744 * from another device on the same hwgroup while we are polling for DSC.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745 */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100746static void idetape_postpone_request(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747{
748 idetape_tape_t *tape = drive->driver_data;
749
Borislav Petkov8004a8c2008-02-06 02:57:51 +0100750 debug_log(DBG_PROCS, "Enter %s\n", __func__);
751
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752 tape->postponed_rq = HWGROUP(drive)->rq;
Borislav Petkov54bb2072008-02-06 02:57:52 +0100753 ide_stall_queue(drive, tape->dsc_poll_freq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754}
755
Borislav Petkovd236d742008-04-18 00:46:27 +0200756typedef void idetape_io_buf(ide_drive_t *, struct ide_atapi_pc *, unsigned int);
Borislav Petkova1efc852008-02-06 02:57:52 +0100757
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758/*
Borislav Petkova1efc852008-02-06 02:57:52 +0100759 * This is the usual interrupt handler which will be called during a packet
760 * command. We will transfer some of the data (as requested by the drive) and
761 * will re-point interrupt handler to us. When data transfer is finished, we
762 * will act according to the algorithm described before
Borislav Petkov8d06bfa2008-02-06 02:57:53 +0100763 * idetape_issue_pc.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764 */
Borislav Petkova1efc852008-02-06 02:57:52 +0100765static ide_startstop_t idetape_pc_intr(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766{
767 ide_hwif_t *hwif = drive->hwif;
768 idetape_tape_t *tape = drive->driver_data;
Borislav Petkovd236d742008-04-18 00:46:27 +0200769 struct ide_atapi_pc *pc = tape->pc;
Borislav Petkova1efc852008-02-06 02:57:52 +0100770 xfer_func_t *xferfunc;
771 idetape_io_buf *iobuf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772 unsigned int temp;
773#if SIMULATE_ERRORS
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100774 static int error_sim_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775#endif
Bartlomiej Zolnierkiewicz790d1232008-01-25 22:17:12 +0100776 u16 bcount;
Bartlomiej Zolnierkiewicz8e7657a2008-01-25 22:17:12 +0100777 u8 stat, ireason;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778
Borislav Petkov8004a8c2008-02-06 02:57:51 +0100779 debug_log(DBG_PROCS, "Enter %s - interrupt handler\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780
781 /* Clear the interrupt */
Bartlomiej Zolnierkiewiczc47137a2008-02-06 02:57:51 +0100782 stat = ide_read_status(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783
Borislav Petkov346331f2008-04-18 00:46:26 +0200784 if (pc->flags & PC_FLAG_DMA_IN_PROGRESS) {
Bartlomiej Zolnierkiewicz5e37bdc2008-04-26 22:25:24 +0200785 if (hwif->dma_ops->dma_end(drive) || (stat & ERR_STAT)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786 /*
787 * A DMA error is sometimes expected. For example,
788 * if the tape is crossing a filemark during a
789 * READ command, it will issue an irq and position
790 * itself before the filemark, so that only a partial
791 * data transfer will occur (which causes the DMA
792 * error). In that case, we will later ask the tape
793 * how much bytes of the original request were
794 * actually transferred (we can't receive that
795 * information from the DMA engine on most chipsets).
796 */
797
798 /*
799 * On the contrary, a DMA error is never expected;
800 * it usually indicates a hardware error or abort.
801 * If the tape crosses a filemark during a READ
802 * command, it will issue an irq and position itself
803 * after the filemark (not before). Only a partial
804 * data transfer will occur, but no DMA error.
805 * (AS, 19 Apr 2001)
806 */
Borislav Petkov346331f2008-04-18 00:46:26 +0200807 pc->flags |= PC_FLAG_DMA_ERROR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 } else {
Borislav Petkovd236d742008-04-18 00:46:27 +0200809 pc->xferred = pc->req_xfer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810 idetape_update_buffers(pc);
811 }
Borislav Petkov8004a8c2008-02-06 02:57:51 +0100812 debug_log(DBG_PROCS, "DMA finished\n");
813
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814 }
815
816 /* No more interrupts */
Bartlomiej Zolnierkiewicz22c525b2008-01-25 22:17:11 +0100817 if ((stat & DRQ_STAT) == 0) {
Borislav Petkov8004a8c2008-02-06 02:57:51 +0100818 debug_log(DBG_SENSE, "Packet command completed, %d bytes"
Borislav Petkovd236d742008-04-18 00:46:27 +0200819 " transferred\n", pc->xferred);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820
Borislav Petkov346331f2008-04-18 00:46:26 +0200821 pc->flags &= ~PC_FLAG_DMA_IN_PROGRESS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822 local_irq_enable();
823
824#if SIMULATE_ERRORS
Borislav Petkov90699ce2008-02-02 19:56:50 +0100825 if ((pc->c[0] == WRITE_6 || pc->c[0] == READ_6) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826 (++error_sim_count % 100) == 0) {
827 printk(KERN_INFO "ide-tape: %s: simulating error\n",
828 tape->name);
Bartlomiej Zolnierkiewicz22c525b2008-01-25 22:17:11 +0100829 stat |= ERR_STAT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830 }
831#endif
Borislav Petkov90699ce2008-02-02 19:56:50 +0100832 if ((stat & ERR_STAT) && pc->c[0] == REQUEST_SENSE)
Bartlomiej Zolnierkiewicz22c525b2008-01-25 22:17:11 +0100833 stat &= ~ERR_STAT;
Borislav Petkov346331f2008-04-18 00:46:26 +0200834 if ((stat & ERR_STAT) || (pc->flags & PC_FLAG_DMA_ERROR)) {
Bartlomiej Zolnierkiewicz22c525b2008-01-25 22:17:11 +0100835 /* Error detected */
Borislav Petkov8004a8c2008-02-06 02:57:51 +0100836 debug_log(DBG_ERR, "%s: I/O error\n", tape->name);
837
Borislav Petkov90699ce2008-02-02 19:56:50 +0100838 if (pc->c[0] == REQUEST_SENSE) {
Borislav Petkova1efc852008-02-06 02:57:52 +0100839 printk(KERN_ERR "ide-tape: I/O error in request"
840 " sense command\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841 return ide_do_reset(drive);
842 }
Borislav Petkov8004a8c2008-02-06 02:57:51 +0100843 debug_log(DBG_ERR, "[cmd %x]: check condition\n",
844 pc->c[0]);
845
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846 /* Retry operation */
847 return idetape_retry_pc(drive);
848 }
849 pc->error = 0;
Borislav Petkov346331f2008-04-18 00:46:26 +0200850 if ((pc->flags & PC_FLAG_WAIT_FOR_DSC) &&
Bartlomiej Zolnierkiewicz22c525b2008-01-25 22:17:11 +0100851 (stat & SEEK_STAT) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852 /* Media access command */
853 tape->dsc_polling_start = jiffies;
Borislav Petkov54bb2072008-02-06 02:57:52 +0100854 tape->dsc_poll_freq = IDETAPE_DSC_MA_FAST;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855 tape->dsc_timeout = jiffies + IDETAPE_DSC_MA_TIMEOUT;
856 /* Allow ide.c to handle other requests */
857 idetape_postpone_request(drive);
858 return ide_stopped;
859 }
860 if (tape->failed_pc == pc)
861 tape->failed_pc = NULL;
862 /* Command finished - Call the callback function */
Borislav Petkovd236d742008-04-18 00:46:27 +0200863 return pc->idetape_callback(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864 }
Borislav Petkov346331f2008-04-18 00:46:26 +0200865
866 if (pc->flags & PC_FLAG_DMA_IN_PROGRESS) {
867 pc->flags &= ~PC_FLAG_DMA_IN_PROGRESS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868 printk(KERN_ERR "ide-tape: The tape wants to issue more "
869 "interrupts in DMA mode\n");
870 printk(KERN_ERR "ide-tape: DMA disabled, reverting to PIO\n");
Bartlomiej Zolnierkiewicz7469aaf2007-02-17 02:40:26 +0100871 ide_dma_off(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872 return ide_do_reset(drive);
873 }
874 /* Get the number of bytes to transfer on this interrupt. */
Bartlomiej Zolnierkiewicz23579a22008-04-18 00:46:26 +0200875 bcount = (hwif->INB(hwif->io_ports[IDE_BCOUNTH_OFFSET]) << 8) |
876 hwif->INB(hwif->io_ports[IDE_BCOUNTL_OFFSET]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877
Bartlomiej Zolnierkiewicz23579a22008-04-18 00:46:26 +0200878 ireason = hwif->INB(hwif->io_ports[IDE_IREASON_OFFSET]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879
Bartlomiej Zolnierkiewicz8e7657a2008-01-25 22:17:12 +0100880 if (ireason & CD) {
Borislav Petkova1efc852008-02-06 02:57:52 +0100881 printk(KERN_ERR "ide-tape: CoD != 0 in %s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882 return ide_do_reset(drive);
883 }
Borislav Petkov346331f2008-04-18 00:46:26 +0200884 if (((ireason & IO) == IO) == !!(pc->flags & PC_FLAG_WRITING)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885 /* Hopefully, we will never get here */
886 printk(KERN_ERR "ide-tape: We wanted to %s, ",
Bartlomiej Zolnierkiewicz8e7657a2008-01-25 22:17:12 +0100887 (ireason & IO) ? "Write" : "Read");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888 printk(KERN_ERR "ide-tape: but the tape wants us to %s !\n",
Bartlomiej Zolnierkiewicz8e7657a2008-01-25 22:17:12 +0100889 (ireason & IO) ? "Read" : "Write");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890 return ide_do_reset(drive);
891 }
Borislav Petkov346331f2008-04-18 00:46:26 +0200892 if (!(pc->flags & PC_FLAG_WRITING)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893 /* Reading - Check that we have enough space */
Borislav Petkovd236d742008-04-18 00:46:27 +0200894 temp = pc->xferred + bcount;
895 if (temp > pc->req_xfer) {
896 if (temp > pc->buf_size) {
Borislav Petkova1efc852008-02-06 02:57:52 +0100897 printk(KERN_ERR "ide-tape: The tape wants to "
898 "send us more data than expected "
899 "- discarding data\n");
Bartlomiej Zolnierkiewicz7616c0a2008-04-18 00:46:26 +0200900 ide_atapi_discard_data(drive, bcount);
Borislav Petkova1efc852008-02-06 02:57:52 +0100901 ide_set_handler(drive, &idetape_pc_intr,
902 IDETAPE_WAIT_CMD, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903 return ide_started;
904 }
Borislav Petkov8004a8c2008-02-06 02:57:51 +0100905 debug_log(DBG_SENSE, "The tape wants to send us more "
906 "data than expected - allowing transfer\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907 }
Borislav Petkova1efc852008-02-06 02:57:52 +0100908 iobuf = &idetape_input_buffers;
909 xferfunc = hwif->atapi_input_bytes;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910 } else {
Borislav Petkova1efc852008-02-06 02:57:52 +0100911 iobuf = &idetape_output_buffers;
912 xferfunc = hwif->atapi_output_bytes;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913 }
Borislav Petkova1efc852008-02-06 02:57:52 +0100914
915 if (pc->bh)
916 iobuf(drive, pc, bcount);
917 else
Borislav Petkovd236d742008-04-18 00:46:27 +0200918 xferfunc(drive, pc->cur_pos, bcount);
Borislav Petkova1efc852008-02-06 02:57:52 +0100919
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920 /* Update the current position */
Borislav Petkovd236d742008-04-18 00:46:27 +0200921 pc->xferred += bcount;
922 pc->cur_pos += bcount;
Borislav Petkov8004a8c2008-02-06 02:57:51 +0100923
924 debug_log(DBG_SENSE, "[cmd %x] transferred %d bytes on that intr.\n",
925 pc->c[0], bcount);
926
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927 /* And set the interrupt handler again */
928 ide_set_handler(drive, &idetape_pc_intr, IDETAPE_WAIT_CMD, NULL);
929 return ide_started;
930}
931
932/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100933 * Packet Command Interface
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100935 * The current Packet Command is available in tape->pc, and will not change
936 * until we finish handling it. Each packet command is associated with a
937 * callback function that will be called when the command is finished.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100939 * The handling will be done in three stages:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100941 * 1. idetape_issue_pc will send the packet command to the drive, and will set
942 * the interrupt handler to idetape_pc_intr.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100944 * 2. On each interrupt, idetape_pc_intr will be called. This step will be
945 * repeated until the device signals us that no more interrupts will be issued.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100947 * 3. ATAPI Tape media access commands have immediate status with a delayed
948 * process. In case of a successful initiation of a media access packet command,
949 * the DSC bit will be set when the actual execution of the command is finished.
950 * Since the tape drive will not issue an interrupt, we have to poll for this
951 * event. In this case, we define the request as "low priority request" by
952 * setting rq_status to IDETAPE_RQ_POSTPONED, set a timer to poll for DSC and
953 * exit the driver.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100955 * ide.c will then give higher priority to requests which originate from the
956 * other device, until will change rq_status to RQ_ACTIVE.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100958 * 4. When the packet command is finished, it will be checked for errors.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100960 * 5. In case an error was found, we queue a request sense packet command in
961 * front of the request queue and retry the operation up to
962 * IDETAPE_MAX_PC_RETRIES times.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100964 * 6. In case no error was found, or we decided to give up and not to retry
965 * again, the callback function will be called and then we will handle the next
966 * request.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967 */
968static ide_startstop_t idetape_transfer_pc(ide_drive_t *drive)
969{
970 ide_hwif_t *hwif = drive->hwif;
971 idetape_tape_t *tape = drive->driver_data;
Borislav Petkovd236d742008-04-18 00:46:27 +0200972 struct ide_atapi_pc *pc = tape->pc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973 int retries = 100;
974 ide_startstop_t startstop;
Bartlomiej Zolnierkiewicz8e7657a2008-01-25 22:17:12 +0100975 u8 ireason;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100977 if (ide_wait_stat(&startstop, drive, DRQ_STAT, BUSY_STAT, WAIT_READY)) {
978 printk(KERN_ERR "ide-tape: Strange, packet command initiated "
979 "yet DRQ isn't asserted\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980 return startstop;
981 }
Bartlomiej Zolnierkiewicz23579a22008-04-18 00:46:26 +0200982 ireason = hwif->INB(hwif->io_ports[IDE_IREASON_OFFSET]);
Bartlomiej Zolnierkiewicz8e7657a2008-01-25 22:17:12 +0100983 while (retries-- && ((ireason & CD) == 0 || (ireason & IO))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984 printk(KERN_ERR "ide-tape: (IO,CoD != (0,1) while issuing "
985 "a packet command, retrying\n");
986 udelay(100);
Bartlomiej Zolnierkiewicz23579a22008-04-18 00:46:26 +0200987 ireason = hwif->INB(hwif->io_ports[IDE_IREASON_OFFSET]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988 if (retries == 0) {
989 printk(KERN_ERR "ide-tape: (IO,CoD != (0,1) while "
990 "issuing a packet command, ignoring\n");
Bartlomiej Zolnierkiewicz8e7657a2008-01-25 22:17:12 +0100991 ireason |= CD;
992 ireason &= ~IO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993 }
994 }
Bartlomiej Zolnierkiewicz8e7657a2008-01-25 22:17:12 +0100995 if ((ireason & CD) == 0 || (ireason & IO)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996 printk(KERN_ERR "ide-tape: (IO,CoD) != (0,1) while issuing "
997 "a packet command\n");
998 return ide_do_reset(drive);
999 }
1000 /* Set the interrupt routine */
1001 ide_set_handler(drive, &idetape_pc_intr, IDETAPE_WAIT_CMD, NULL);
1002#ifdef CONFIG_BLK_DEV_IDEDMA
1003 /* Begin DMA, if necessary */
Borislav Petkov346331f2008-04-18 00:46:26 +02001004 if (pc->flags & PC_FLAG_DMA_IN_PROGRESS)
Bartlomiej Zolnierkiewicz5e37bdc2008-04-26 22:25:24 +02001005 hwif->dma_ops->dma_start(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006#endif
1007 /* Send the actual packet */
1008 HWIF(drive)->atapi_output_bytes(drive, pc->c, 12);
1009 return ide_started;
1010}
1011
Borislav Petkovd236d742008-04-18 00:46:27 +02001012static ide_startstop_t idetape_issue_pc(ide_drive_t *drive,
1013 struct ide_atapi_pc *pc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014{
1015 ide_hwif_t *hwif = drive->hwif;
1016 idetape_tape_t *tape = drive->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017 int dma_ok = 0;
Bartlomiej Zolnierkiewicz790d1232008-01-25 22:17:12 +01001018 u16 bcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019
Borislav Petkov90699ce2008-02-02 19:56:50 +01001020 if (tape->pc->c[0] == REQUEST_SENSE &&
1021 pc->c[0] == REQUEST_SENSE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022 printk(KERN_ERR "ide-tape: possible ide-tape.c bug - "
1023 "Two request sense in serial were issued\n");
1024 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025
Borislav Petkov90699ce2008-02-02 19:56:50 +01001026 if (tape->failed_pc == NULL && pc->c[0] != REQUEST_SENSE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027 tape->failed_pc = pc;
1028 /* Set the current packet command */
1029 tape->pc = pc;
1030
1031 if (pc->retries > IDETAPE_MAX_PC_RETRIES ||
Borislav Petkov346331f2008-04-18 00:46:26 +02001032 (pc->flags & PC_FLAG_ABORT)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033 /*
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001034 * We will "abort" retrying a packet command in case legitimate
1035 * error code was received (crossing a filemark, or end of the
1036 * media, for example).
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037 */
Borislav Petkov346331f2008-04-18 00:46:26 +02001038 if (!(pc->flags & PC_FLAG_ABORT)) {
Borislav Petkov90699ce2008-02-02 19:56:50 +01001039 if (!(pc->c[0] == TEST_UNIT_READY &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040 tape->sense_key == 2 && tape->asc == 4 &&
1041 (tape->ascq == 1 || tape->ascq == 8))) {
1042 printk(KERN_ERR "ide-tape: %s: I/O error, "
1043 "pc = %2x, key = %2x, "
1044 "asc = %2x, ascq = %2x\n",
1045 tape->name, pc->c[0],
1046 tape->sense_key, tape->asc,
1047 tape->ascq);
1048 }
1049 /* Giving up */
1050 pc->error = IDETAPE_ERROR_GENERAL;
1051 }
1052 tape->failed_pc = NULL;
Borislav Petkovd236d742008-04-18 00:46:27 +02001053 return pc->idetape_callback(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054 }
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001055 debug_log(DBG_SENSE, "Retry #%d, cmd = %02X\n", pc->retries, pc->c[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056
1057 pc->retries++;
1058 /* We haven't transferred any data yet */
Borislav Petkovd236d742008-04-18 00:46:27 +02001059 pc->xferred = 0;
1060 pc->cur_pos = pc->buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061 /* Request to transfer the entire buffer at once */
Borislav Petkovd236d742008-04-18 00:46:27 +02001062 bcount = pc->req_xfer;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063
Borislav Petkov346331f2008-04-18 00:46:26 +02001064 if (pc->flags & PC_FLAG_DMA_ERROR) {
1065 pc->flags &= ~PC_FLAG_DMA_ERROR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066 printk(KERN_WARNING "ide-tape: DMA disabled, "
1067 "reverting to PIO\n");
Bartlomiej Zolnierkiewicz7469aaf2007-02-17 02:40:26 +01001068 ide_dma_off(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069 }
Borislav Petkov346331f2008-04-18 00:46:26 +02001070 if ((pc->flags & PC_FLAG_DMA_RECOMMENDED) && drive->using_dma)
Bartlomiej Zolnierkiewicz5e37bdc2008-04-26 22:25:24 +02001071 dma_ok = !hwif->dma_ops->dma_setup(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072
Bartlomiej Zolnierkiewicz2fc57382008-01-25 22:17:13 +01001073 ide_pktcmd_tf_load(drive, IDE_TFLAG_NO_SELECT_MASK |
1074 IDE_TFLAG_OUT_DEVICE, bcount, dma_ok);
1075
Borislav Petkov346331f2008-04-18 00:46:26 +02001076 if (dma_ok)
1077 /* Will begin DMA later */
1078 pc->flags |= PC_FLAG_DMA_IN_PROGRESS;
1079 if (test_bit(IDETAPE_FLAG_DRQ_INTERRUPT, &tape->flags)) {
Bartlomiej Zolnierkiewiczc1c9dbc2008-02-02 19:56:44 +01001080 ide_execute_command(drive, WIN_PACKETCMD, &idetape_transfer_pc,
1081 IDETAPE_WAIT_CMD, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082 return ide_started;
1083 } else {
Bartlomiej Zolnierkiewicz23579a22008-04-18 00:46:26 +02001084 hwif->OUTB(WIN_PACKETCMD, hwif->io_ports[IDE_COMMAND_OFFSET]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085 return idetape_transfer_pc(drive);
1086 }
1087}
1088
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001089static ide_startstop_t idetape_pc_callback(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090{
1091 idetape_tape_t *tape = drive->driver_data;
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001092
1093 debug_log(DBG_PROCS, "Enter %s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094
1095 idetape_end_request(drive, tape->pc->error ? 0 : 1, 0);
1096 return ide_stopped;
1097}
1098
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001099/* A mode sense command is used to "sense" tape parameters. */
Borislav Petkovd236d742008-04-18 00:46:27 +02001100static void idetape_create_mode_sense_cmd(struct ide_atapi_pc *pc, u8 page_code)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101{
1102 idetape_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +01001103 pc->c[0] = MODE_SENSE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104 if (page_code != IDETAPE_BLOCK_DESCRIPTOR)
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001105 /* DBD = 1 - Don't return block descriptors */
1106 pc->c[1] = 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107 pc->c[2] = page_code;
1108 /*
1109 * Changed pc->c[3] to 0 (255 will at best return unused info).
1110 *
1111 * For SCSI this byte is defined as subpage instead of high byte
1112 * of length and some IDE drives seem to interpret it this way
1113 * and return an error when 255 is used.
1114 */
1115 pc->c[3] = 0;
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001116 /* We will just discard data in that case */
1117 pc->c[4] = 255;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118 if (page_code == IDETAPE_BLOCK_DESCRIPTOR)
Borislav Petkovd236d742008-04-18 00:46:27 +02001119 pc->req_xfer = 12;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120 else if (page_code == IDETAPE_CAPABILITIES_PAGE)
Borislav Petkovd236d742008-04-18 00:46:27 +02001121 pc->req_xfer = 24;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122 else
Borislav Petkovd236d742008-04-18 00:46:27 +02001123 pc->req_xfer = 50;
1124 pc->idetape_callback = &idetape_pc_callback;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125}
1126
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001127static ide_startstop_t idetape_media_access_finished(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128{
1129 idetape_tape_t *tape = drive->driver_data;
Borislav Petkovd236d742008-04-18 00:46:27 +02001130 struct ide_atapi_pc *pc = tape->pc;
Bartlomiej Zolnierkiewicz22c525b2008-01-25 22:17:11 +01001131 u8 stat;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132
Bartlomiej Zolnierkiewiczc47137a2008-02-06 02:57:51 +01001133 stat = ide_read_status(drive);
1134
Bartlomiej Zolnierkiewicz22c525b2008-01-25 22:17:11 +01001135 if (stat & SEEK_STAT) {
1136 if (stat & ERR_STAT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137 /* Error detected */
Borislav Petkov90699ce2008-02-02 19:56:50 +01001138 if (pc->c[0] != TEST_UNIT_READY)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139 printk(KERN_ERR "ide-tape: %s: I/O error, ",
1140 tape->name);
1141 /* Retry operation */
1142 return idetape_retry_pc(drive);
1143 }
1144 pc->error = 0;
1145 if (tape->failed_pc == pc)
1146 tape->failed_pc = NULL;
1147 } else {
1148 pc->error = IDETAPE_ERROR_GENERAL;
1149 tape->failed_pc = NULL;
1150 }
Borislav Petkovd236d742008-04-18 00:46:27 +02001151 return pc->idetape_callback(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152}
1153
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001154static ide_startstop_t idetape_rw_callback(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155{
1156 idetape_tape_t *tape = drive->driver_data;
1157 struct request *rq = HWGROUP(drive)->rq;
Borislav Petkovd236d742008-04-18 00:46:27 +02001158 int blocks = tape->pc->xferred / tape->blk_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159
Borislav Petkov54bb2072008-02-06 02:57:52 +01001160 tape->avg_size += blocks * tape->blk_size;
Borislav Petkov83042b22008-04-27 15:38:27 +02001161
Marcelo Feitoza Parisi9bae1ff2006-03-28 01:56:46 -08001162 if (time_after_eq(jiffies, tape->avg_time + HZ)) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001163 tape->avg_speed = tape->avg_size * HZ /
1164 (jiffies - tape->avg_time) / 1024;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165 tape->avg_size = 0;
1166 tape->avg_time = jiffies;
1167 }
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001168 debug_log(DBG_PROCS, "Enter %s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169
Borislav Petkov54bb2072008-02-06 02:57:52 +01001170 tape->first_frame += blocks;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171 rq->current_nr_sectors -= blocks;
1172
1173 if (!tape->pc->error)
1174 idetape_end_request(drive, 1, 0);
1175 else
1176 idetape_end_request(drive, tape->pc->error, 0);
1177 return ide_stopped;
1178}
1179
Borislav Petkovd236d742008-04-18 00:46:27 +02001180static void idetape_create_read_cmd(idetape_tape_t *tape,
1181 struct ide_atapi_pc *pc,
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001182 unsigned int length, struct idetape_bh *bh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183{
1184 idetape_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +01001185 pc->c[0] = READ_6;
Borislav Petkov860ff5e2008-02-02 19:56:50 +01001186 put_unaligned(cpu_to_be32(length), (unsigned int *) &pc->c[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187 pc->c[1] = 1;
Borislav Petkovd236d742008-04-18 00:46:27 +02001188 pc->idetape_callback = &idetape_rw_callback;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189 pc->bh = bh;
1190 atomic_set(&bh->b_count, 0);
Borislav Petkovd236d742008-04-18 00:46:27 +02001191 pc->buf = NULL;
1192 pc->buf_size = length * tape->blk_size;
1193 pc->req_xfer = pc->buf_size;
1194 if (pc->req_xfer == tape->stage_size)
Borislav Petkov346331f2008-04-18 00:46:26 +02001195 pc->flags |= PC_FLAG_DMA_RECOMMENDED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196}
1197
Borislav Petkovd236d742008-04-18 00:46:27 +02001198static void idetape_create_write_cmd(idetape_tape_t *tape,
1199 struct ide_atapi_pc *pc,
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001200 unsigned int length, struct idetape_bh *bh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201{
1202 idetape_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +01001203 pc->c[0] = WRITE_6;
Borislav Petkov860ff5e2008-02-02 19:56:50 +01001204 put_unaligned(cpu_to_be32(length), (unsigned int *) &pc->c[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205 pc->c[1] = 1;
Borislav Petkovd236d742008-04-18 00:46:27 +02001206 pc->idetape_callback = &idetape_rw_callback;
Borislav Petkov346331f2008-04-18 00:46:26 +02001207 pc->flags |= PC_FLAG_WRITING;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208 pc->bh = bh;
1209 pc->b_data = bh->b_data;
1210 pc->b_count = atomic_read(&bh->b_count);
Borislav Petkovd236d742008-04-18 00:46:27 +02001211 pc->buf = NULL;
1212 pc->buf_size = length * tape->blk_size;
1213 pc->req_xfer = pc->buf_size;
1214 if (pc->req_xfer == tape->stage_size)
Borislav Petkov346331f2008-04-18 00:46:26 +02001215 pc->flags |= PC_FLAG_DMA_RECOMMENDED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216}
1217
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218static ide_startstop_t idetape_do_request(ide_drive_t *drive,
1219 struct request *rq, sector_t block)
1220{
1221 idetape_tape_t *tape = drive->driver_data;
Borislav Petkovd236d742008-04-18 00:46:27 +02001222 struct ide_atapi_pc *pc = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223 struct request *postponed_rq = tape->postponed_rq;
Bartlomiej Zolnierkiewicz22c525b2008-01-25 22:17:11 +01001224 u8 stat;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001226 debug_log(DBG_SENSE, "sector: %ld, nr_sectors: %ld,"
1227 " current_nr_sectors: %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228 rq->sector, rq->nr_sectors, rq->current_nr_sectors);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229
Jens Axboe4aff5e22006-08-10 08:44:47 +02001230 if (!blk_special_request(rq)) {
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001231 /* We do not support buffer cache originated requests. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232 printk(KERN_NOTICE "ide-tape: %s: Unsupported request in "
Jens Axboe4aff5e22006-08-10 08:44:47 +02001233 "request queue (%d)\n", drive->name, rq->cmd_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234 ide_end_request(drive, 0, 0);
1235 return ide_stopped;
1236 }
1237
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001238 /* Retry a failed packet command */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001239 if (tape->failed_pc && tape->pc->c[0] == REQUEST_SENSE)
Borislav Petkov8d06bfa2008-02-06 02:57:53 +01001240 return idetape_issue_pc(drive, tape->failed_pc);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001241
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242 if (postponed_rq != NULL)
1243 if (rq != postponed_rq) {
1244 printk(KERN_ERR "ide-tape: ide-tape.c bug - "
1245 "Two DSC requests were queued\n");
1246 idetape_end_request(drive, 0, 0);
1247 return ide_stopped;
1248 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249
1250 tape->postponed_rq = NULL;
1251
1252 /*
1253 * If the tape is still busy, postpone our request and service
1254 * the other device meanwhile.
1255 */
Bartlomiej Zolnierkiewiczc47137a2008-02-06 02:57:51 +01001256 stat = ide_read_status(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257
1258 if (!drive->dsc_overlap && !(rq->cmd[0] & REQ_IDETAPE_PC2))
Borislav Petkov346331f2008-04-18 00:46:26 +02001259 set_bit(IDETAPE_FLAG_IGNORE_DSC, &tape->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260
1261 if (drive->post_reset == 1) {
Borislav Petkov346331f2008-04-18 00:46:26 +02001262 set_bit(IDETAPE_FLAG_IGNORE_DSC, &tape->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263 drive->post_reset = 0;
1264 }
1265
Borislav Petkov346331f2008-04-18 00:46:26 +02001266 if (!test_and_clear_bit(IDETAPE_FLAG_IGNORE_DSC, &tape->flags) &&
Bartlomiej Zolnierkiewicz22c525b2008-01-25 22:17:11 +01001267 (stat & SEEK_STAT) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268 if (postponed_rq == NULL) {
1269 tape->dsc_polling_start = jiffies;
Borislav Petkov54bb2072008-02-06 02:57:52 +01001270 tape->dsc_poll_freq = tape->best_dsc_rw_freq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271 tape->dsc_timeout = jiffies + IDETAPE_DSC_RW_TIMEOUT;
1272 } else if (time_after(jiffies, tape->dsc_timeout)) {
1273 printk(KERN_ERR "ide-tape: %s: DSC timeout\n",
1274 tape->name);
1275 if (rq->cmd[0] & REQ_IDETAPE_PC2) {
1276 idetape_media_access_finished(drive);
1277 return ide_stopped;
1278 } else {
1279 return ide_do_reset(drive);
1280 }
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001281 } else if (time_after(jiffies,
1282 tape->dsc_polling_start +
1283 IDETAPE_DSC_MA_THRESHOLD))
Borislav Petkov54bb2072008-02-06 02:57:52 +01001284 tape->dsc_poll_freq = IDETAPE_DSC_MA_SLOW;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285 idetape_postpone_request(drive);
1286 return ide_stopped;
1287 }
1288 if (rq->cmd[0] & REQ_IDETAPE_READ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289 pc = idetape_next_pc_storage(drive);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001290 idetape_create_read_cmd(tape, pc, rq->current_nr_sectors,
1291 (struct idetape_bh *)rq->special);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292 goto out;
1293 }
1294 if (rq->cmd[0] & REQ_IDETAPE_WRITE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295 pc = idetape_next_pc_storage(drive);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001296 idetape_create_write_cmd(tape, pc, rq->current_nr_sectors,
1297 (struct idetape_bh *)rq->special);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298 goto out;
1299 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300 if (rq->cmd[0] & REQ_IDETAPE_PC1) {
Borislav Petkovd236d742008-04-18 00:46:27 +02001301 pc = (struct ide_atapi_pc *) rq->buffer;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302 rq->cmd[0] &= ~(REQ_IDETAPE_PC1);
1303 rq->cmd[0] |= REQ_IDETAPE_PC2;
1304 goto out;
1305 }
1306 if (rq->cmd[0] & REQ_IDETAPE_PC2) {
1307 idetape_media_access_finished(drive);
1308 return ide_stopped;
1309 }
1310 BUG();
1311out:
Borislav Petkov8d06bfa2008-02-06 02:57:53 +01001312 return idetape_issue_pc(drive, pc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313}
1314
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001315/* Pipeline related functions */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316
1317/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001318 * The function below uses __get_free_page to allocate a pipeline stage, along
1319 * with all the necessary small buffers which together make a buffer of size
1320 * tape->stage_size (or a bit more). We attempt to combine sequential pages as
1321 * much as possible.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001323 * It returns a pointer to the new allocated stage, or NULL if we can't (or
1324 * don't want to) allocate a stage.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001326 * Pipeline stages are optional and are used to increase performance. If we
1327 * can't allocate them, we'll manage without them.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328 */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001329static idetape_stage_t *__idetape_kmalloc_stage(idetape_tape_t *tape, int full,
1330 int clear)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331{
1332 idetape_stage_t *stage;
1333 struct idetape_bh *prev_bh, *bh;
1334 int pages = tape->pages_per_stage;
1335 char *b_data = NULL;
1336
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001337 stage = kmalloc(sizeof(idetape_stage_t), GFP_KERNEL);
1338 if (!stage)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339 return NULL;
1340 stage->next = NULL;
1341
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001342 stage->bh = kmalloc(sizeof(struct idetape_bh), GFP_KERNEL);
1343 bh = stage->bh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344 if (bh == NULL)
1345 goto abort;
1346 bh->b_reqnext = NULL;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001347 bh->b_data = (char *) __get_free_page(GFP_KERNEL);
1348 if (!bh->b_data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349 goto abort;
1350 if (clear)
1351 memset(bh->b_data, 0, PAGE_SIZE);
1352 bh->b_size = PAGE_SIZE;
1353 atomic_set(&bh->b_count, full ? bh->b_size : 0);
1354
1355 while (--pages) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001356 b_data = (char *) __get_free_page(GFP_KERNEL);
1357 if (!b_data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358 goto abort;
1359 if (clear)
1360 memset(b_data, 0, PAGE_SIZE);
1361 if (bh->b_data == b_data + PAGE_SIZE) {
1362 bh->b_size += PAGE_SIZE;
1363 bh->b_data -= PAGE_SIZE;
1364 if (full)
1365 atomic_add(PAGE_SIZE, &bh->b_count);
1366 continue;
1367 }
1368 if (b_data == bh->b_data + bh->b_size) {
1369 bh->b_size += PAGE_SIZE;
1370 if (full)
1371 atomic_add(PAGE_SIZE, &bh->b_count);
1372 continue;
1373 }
1374 prev_bh = bh;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001375 bh = kmalloc(sizeof(struct idetape_bh), GFP_KERNEL);
1376 if (!bh) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377 free_page((unsigned long) b_data);
1378 goto abort;
1379 }
1380 bh->b_reqnext = NULL;
1381 bh->b_data = b_data;
1382 bh->b_size = PAGE_SIZE;
1383 atomic_set(&bh->b_count, full ? bh->b_size : 0);
1384 prev_bh->b_reqnext = bh;
1385 }
1386 bh->b_size -= tape->excess_bh_size;
1387 if (full)
1388 atomic_sub(tape->excess_bh_size, &bh->b_count);
1389 return stage;
1390abort:
1391 __idetape_kfree_stage(stage);
1392 return NULL;
1393}
1394
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001395static int idetape_copy_stage_from_user(idetape_tape_t *tape,
Borislav Petkov8646c882008-04-27 15:38:26 +02001396 const char __user *buf, int n)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397{
1398 struct idetape_bh *bh = tape->bh;
1399 int count;
Daniel Walkerdcd96372006-06-25 05:47:37 -07001400 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401
1402 while (n) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403 if (bh == NULL) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001404 printk(KERN_ERR "ide-tape: bh == NULL in %s\n",
1405 __func__);
Daniel Walkerdcd96372006-06-25 05:47:37 -07001406 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407 }
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001408 count = min((unsigned int)
1409 (bh->b_size - atomic_read(&bh->b_count)),
1410 (unsigned int)n);
1411 if (copy_from_user(bh->b_data + atomic_read(&bh->b_count), buf,
1412 count))
Daniel Walkerdcd96372006-06-25 05:47:37 -07001413 ret = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414 n -= count;
1415 atomic_add(count, &bh->b_count);
1416 buf += count;
1417 if (atomic_read(&bh->b_count) == bh->b_size) {
1418 bh = bh->b_reqnext;
1419 if (bh)
1420 atomic_set(&bh->b_count, 0);
1421 }
1422 }
1423 tape->bh = bh;
Daniel Walkerdcd96372006-06-25 05:47:37 -07001424 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425}
1426
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001427static int idetape_copy_stage_to_user(idetape_tape_t *tape, char __user *buf,
Borislav Petkov99d74e62008-04-27 15:38:25 +02001428 int n)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429{
1430 struct idetape_bh *bh = tape->bh;
1431 int count;
Daniel Walkerdcd96372006-06-25 05:47:37 -07001432 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433
1434 while (n) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435 if (bh == NULL) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001436 printk(KERN_ERR "ide-tape: bh == NULL in %s\n",
1437 __func__);
Daniel Walkerdcd96372006-06-25 05:47:37 -07001438 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440 count = min(tape->b_count, n);
Daniel Walkerdcd96372006-06-25 05:47:37 -07001441 if (copy_to_user(buf, tape->b_data, count))
1442 ret = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443 n -= count;
1444 tape->b_data += count;
1445 tape->b_count -= count;
1446 buf += count;
1447 if (!tape->b_count) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001448 bh = bh->b_reqnext;
1449 tape->bh = bh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450 if (bh) {
1451 tape->b_data = bh->b_data;
1452 tape->b_count = atomic_read(&bh->b_count);
1453 }
1454 }
1455 }
Daniel Walkerdcd96372006-06-25 05:47:37 -07001456 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457}
1458
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001459static void idetape_init_merge_stage(idetape_tape_t *tape)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460{
1461 struct idetape_bh *bh = tape->merge_stage->bh;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001462
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463 tape->bh = bh;
Borislav Petkov54abf372008-02-06 02:57:52 +01001464 if (tape->chrdev_dir == IDETAPE_DIR_WRITE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465 atomic_set(&bh->b_count, 0);
1466 else {
1467 tape->b_data = bh->b_data;
1468 tape->b_count = atomic_read(&bh->b_count);
1469 }
1470}
1471
Borislav Petkova2f5b7f2008-02-06 02:57:51 +01001472static ide_startstop_t idetape_read_position_callback(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473{
1474 idetape_tape_t *tape = drive->driver_data;
Borislav Petkovd236d742008-04-18 00:46:27 +02001475 u8 *readpos = tape->pc->buf;
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001476
1477 debug_log(DBG_PROCS, "Enter %s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478
1479 if (!tape->pc->error) {
Borislav Petkova2f5b7f2008-02-06 02:57:51 +01001480 debug_log(DBG_SENSE, "BOP - %s\n",
1481 (readpos[0] & 0x80) ? "Yes" : "No");
1482 debug_log(DBG_SENSE, "EOP - %s\n",
1483 (readpos[0] & 0x40) ? "Yes" : "No");
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001484
Borislav Petkova2f5b7f2008-02-06 02:57:51 +01001485 if (readpos[0] & 0x4) {
1486 printk(KERN_INFO "ide-tape: Block location is unknown"
1487 "to the tape\n");
Borislav Petkov346331f2008-04-18 00:46:26 +02001488 clear_bit(IDETAPE_FLAG_ADDRESS_VALID, &tape->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489 idetape_end_request(drive, 0, 0);
1490 } else {
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001491 debug_log(DBG_SENSE, "Block Location - %u\n",
Borislav Petkova2f5b7f2008-02-06 02:57:51 +01001492 be32_to_cpu(*(u32 *)&readpos[4]));
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001493
Borislav Petkova2f5b7f2008-02-06 02:57:51 +01001494 tape->partition = readpos[1];
Borislav Petkov54bb2072008-02-06 02:57:52 +01001495 tape->first_frame =
Borislav Petkova2f5b7f2008-02-06 02:57:51 +01001496 be32_to_cpu(*(u32 *)&readpos[4]);
Borislav Petkov346331f2008-04-18 00:46:26 +02001497 set_bit(IDETAPE_FLAG_ADDRESS_VALID, &tape->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498 idetape_end_request(drive, 1, 0);
1499 }
1500 } else {
1501 idetape_end_request(drive, 0, 0);
1502 }
1503 return ide_stopped;
1504}
1505
1506/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001507 * Write a filemark if write_filemark=1. Flush the device buffers without
1508 * writing a filemark otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509 */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001510static void idetape_create_write_filemark_cmd(ide_drive_t *drive,
Borislav Petkovd236d742008-04-18 00:46:27 +02001511 struct ide_atapi_pc *pc, int write_filemark)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512{
1513 idetape_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +01001514 pc->c[0] = WRITE_FILEMARKS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515 pc->c[4] = write_filemark;
Borislav Petkov346331f2008-04-18 00:46:26 +02001516 pc->flags |= PC_FLAG_WAIT_FOR_DSC;
Borislav Petkovd236d742008-04-18 00:46:27 +02001517 pc->idetape_callback = &idetape_pc_callback;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518}
1519
Borislav Petkovd236d742008-04-18 00:46:27 +02001520static void idetape_create_test_unit_ready_cmd(struct ide_atapi_pc *pc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521{
1522 idetape_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +01001523 pc->c[0] = TEST_UNIT_READY;
Borislav Petkovd236d742008-04-18 00:46:27 +02001524 pc->idetape_callback = &idetape_pc_callback;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001525}
1526
1527/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001528 * We add a special packet command request to the tail of the request queue, and
1529 * wait for it to be serviced. This is not to be called from within the request
1530 * handling part of the driver! We allocate here data on the stack and it is
1531 * valid until the request is finished. This is not the case for the bottom part
1532 * of the driver, where we are always leaving the functions to wait for an
1533 * interrupt or a timer event.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001535 * From the bottom part of the driver, we should allocate safe memory using
1536 * idetape_next_pc_storage() and ide_tape_next_rq_storage(), and add the request
1537 * to the request list without waiting for it to be serviced! In that case, we
1538 * usually use idetape_queue_pc_head().
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539 */
Borislav Petkovea1ab3d2008-04-27 15:38:27 +02001540static int idetape_queue_pc_tail(ide_drive_t *drive, struct ide_atapi_pc *pc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541{
1542 struct ide_tape_obj *tape = drive->driver_data;
1543 struct request rq;
1544
1545 idetape_init_rq(&rq, REQ_IDETAPE_PC1);
1546 rq.buffer = (char *) pc;
1547 rq.rq_disk = tape->disk;
1548 return ide_do_drive_cmd(drive, &rq, ide_wait);
1549}
1550
Borislav Petkovd236d742008-04-18 00:46:27 +02001551static void idetape_create_load_unload_cmd(ide_drive_t *drive,
1552 struct ide_atapi_pc *pc, int cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553{
1554 idetape_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +01001555 pc->c[0] = START_STOP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556 pc->c[4] = cmd;
Borislav Petkov346331f2008-04-18 00:46:26 +02001557 pc->flags |= PC_FLAG_WAIT_FOR_DSC;
Borislav Petkovd236d742008-04-18 00:46:27 +02001558 pc->idetape_callback = &idetape_pc_callback;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559}
1560
1561static int idetape_wait_ready(ide_drive_t *drive, unsigned long timeout)
1562{
1563 idetape_tape_t *tape = drive->driver_data;
Borislav Petkovd236d742008-04-18 00:46:27 +02001564 struct ide_atapi_pc pc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565 int load_attempted = 0;
1566
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001567 /* Wait for the tape to become ready */
Borislav Petkov346331f2008-04-18 00:46:26 +02001568 set_bit(IDETAPE_FLAG_MEDIUM_PRESENT, &tape->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569 timeout += jiffies;
1570 while (time_before(jiffies, timeout)) {
1571 idetape_create_test_unit_ready_cmd(&pc);
Borislav Petkovea1ab3d2008-04-27 15:38:27 +02001572 if (!idetape_queue_pc_tail(drive, &pc))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001573 return 0;
1574 if ((tape->sense_key == 2 && tape->asc == 4 && tape->ascq == 2)
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001575 || (tape->asc == 0x3A)) {
1576 /* no media */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001577 if (load_attempted)
1578 return -ENOMEDIUM;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001579 idetape_create_load_unload_cmd(drive, &pc,
1580 IDETAPE_LU_LOAD_MASK);
Borislav Petkovea1ab3d2008-04-27 15:38:27 +02001581 idetape_queue_pc_tail(drive, &pc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582 load_attempted = 1;
1583 /* not about to be ready */
1584 } else if (!(tape->sense_key == 2 && tape->asc == 4 &&
1585 (tape->ascq == 1 || tape->ascq == 8)))
1586 return -EIO;
Nishanth Aravamudan80ce45f2005-09-10 00:27:08 -07001587 msleep(100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001588 }
1589 return -EIO;
1590}
1591
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001592static int idetape_flush_tape_buffers(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001593{
Borislav Petkovd236d742008-04-18 00:46:27 +02001594 struct ide_atapi_pc pc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001595 int rc;
1596
1597 idetape_create_write_filemark_cmd(drive, &pc, 0);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001598 rc = idetape_queue_pc_tail(drive, &pc);
1599 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001600 return rc;
1601 idetape_wait_ready(drive, 60 * 5 * HZ);
1602 return 0;
1603}
1604
Borislav Petkovd236d742008-04-18 00:46:27 +02001605static void idetape_create_read_position_cmd(struct ide_atapi_pc *pc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606{
1607 idetape_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +01001608 pc->c[0] = READ_POSITION;
Borislav Petkovd236d742008-04-18 00:46:27 +02001609 pc->req_xfer = 20;
1610 pc->idetape_callback = &idetape_read_position_callback;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001611}
1612
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001613static int idetape_read_position(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001614{
1615 idetape_tape_t *tape = drive->driver_data;
Borislav Petkovd236d742008-04-18 00:46:27 +02001616 struct ide_atapi_pc pc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617 int position;
1618
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001619 debug_log(DBG_PROCS, "Enter %s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001620
1621 idetape_create_read_position_cmd(&pc);
1622 if (idetape_queue_pc_tail(drive, &pc))
1623 return -1;
Borislav Petkov54bb2072008-02-06 02:57:52 +01001624 position = tape->first_frame;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001625 return position;
1626}
1627
Borislav Petkovd236d742008-04-18 00:46:27 +02001628static void idetape_create_locate_cmd(ide_drive_t *drive,
1629 struct ide_atapi_pc *pc,
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001630 unsigned int block, u8 partition, int skip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001631{
1632 idetape_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +01001633 pc->c[0] = POSITION_TO_ELEMENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634 pc->c[1] = 2;
Borislav Petkov860ff5e2008-02-02 19:56:50 +01001635 put_unaligned(cpu_to_be32(block), (unsigned int *) &pc->c[3]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636 pc->c[8] = partition;
Borislav Petkov346331f2008-04-18 00:46:26 +02001637 pc->flags |= PC_FLAG_WAIT_FOR_DSC;
Borislav Petkovd236d742008-04-18 00:46:27 +02001638 pc->idetape_callback = &idetape_pc_callback;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001639}
1640
Borislav Petkovd236d742008-04-18 00:46:27 +02001641static int idetape_create_prevent_cmd(ide_drive_t *drive,
1642 struct ide_atapi_pc *pc, int prevent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643{
1644 idetape_tape_t *tape = drive->driver_data;
1645
Borislav Petkovb6422012008-02-02 19:56:49 +01001646 /* device supports locking according to capabilities page */
1647 if (!(tape->caps[6] & 0x01))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001648 return 0;
1649
1650 idetape_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +01001651 pc->c[0] = ALLOW_MEDIUM_REMOVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001652 pc->c[4] = prevent;
Borislav Petkovd236d742008-04-18 00:46:27 +02001653 pc->idetape_callback = &idetape_pc_callback;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654 return 1;
1655}
1656
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001657static int __idetape_discard_read_pipeline(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001658{
1659 idetape_tape_t *tape = drive->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001660
Borislav Petkov54abf372008-02-06 02:57:52 +01001661 if (tape->chrdev_dir != IDETAPE_DIR_READ)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001662 return 0;
1663
Borislav Petkov83042b22008-04-27 15:38:27 +02001664 clear_bit(IDETAPE_FLAG_FILEMARK, &tape->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001665 tape->merge_stage_size = 0;
1666 if (tape->merge_stage != NULL) {
1667 __idetape_kfree_stage(tape->merge_stage);
1668 tape->merge_stage = NULL;
1669 }
1670
Borislav Petkov54abf372008-02-06 02:57:52 +01001671 tape->chrdev_dir = IDETAPE_DIR_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001672
Borislav Petkov83042b22008-04-27 15:38:27 +02001673 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001674}
1675
1676/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001677 * Position the tape to the requested block using the LOCATE packet command.
1678 * A READ POSITION command is then issued to check where we are positioned. Like
1679 * all higher level operations, we queue the commands at the tail of the request
1680 * queue and wait for their completion.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681 */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001682static int idetape_position_tape(ide_drive_t *drive, unsigned int block,
1683 u8 partition, int skip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001684{
1685 idetape_tape_t *tape = drive->driver_data;
1686 int retval;
Borislav Petkovd236d742008-04-18 00:46:27 +02001687 struct ide_atapi_pc pc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001688
Borislav Petkov54abf372008-02-06 02:57:52 +01001689 if (tape->chrdev_dir == IDETAPE_DIR_READ)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001690 __idetape_discard_read_pipeline(drive);
1691 idetape_wait_ready(drive, 60 * 5 * HZ);
1692 idetape_create_locate_cmd(drive, &pc, block, partition, skip);
1693 retval = idetape_queue_pc_tail(drive, &pc);
1694 if (retval)
1695 return (retval);
1696
1697 idetape_create_read_position_cmd(&pc);
1698 return (idetape_queue_pc_tail(drive, &pc));
1699}
1700
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001701static void idetape_discard_read_pipeline(ide_drive_t *drive,
1702 int restore_position)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001703{
1704 idetape_tape_t *tape = drive->driver_data;
1705 int cnt;
1706 int seek, position;
1707
1708 cnt = __idetape_discard_read_pipeline(drive);
1709 if (restore_position) {
1710 position = idetape_read_position(drive);
1711 seek = position > cnt ? position - cnt : 0;
1712 if (idetape_position_tape(drive, seek, 0, 0)) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001713 printk(KERN_INFO "ide-tape: %s: position_tape failed in"
1714 " discard_pipeline()\n", tape->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001715 return;
1716 }
1717 }
1718}
1719
1720/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001721 * Generate a read/write request for the block device interface and wait for it
1722 * to be serviced.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001723 */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001724static int idetape_queue_rw_tail(ide_drive_t *drive, int cmd, int blocks,
1725 struct idetape_bh *bh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001726{
1727 idetape_tape_t *tape = drive->driver_data;
1728 struct request rq;
1729
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001730 debug_log(DBG_SENSE, "%s: cmd=%d\n", __func__, cmd);
1731
Linus Torvalds1da177e2005-04-16 15:20:36 -07001732 idetape_init_rq(&rq, cmd);
1733 rq.rq_disk = tape->disk;
1734 rq.special = (void *)bh;
Borislav Petkov54bb2072008-02-06 02:57:52 +01001735 rq.sector = tape->first_frame;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001736 rq.nr_sectors = blocks;
1737 rq.current_nr_sectors = blocks;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001738 (void) ide_do_drive_cmd(drive, &rq, ide_wait);
1739
1740 if ((cmd & (REQ_IDETAPE_READ | REQ_IDETAPE_WRITE)) == 0)
1741 return 0;
1742
1743 if (tape->merge_stage)
1744 idetape_init_merge_stage(tape);
1745 if (rq.errors == IDETAPE_ERROR_GENERAL)
1746 return -EIO;
Borislav Petkov54bb2072008-02-06 02:57:52 +01001747 return (tape->blk_size * (blocks-rq.current_nr_sectors));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001748}
1749
Borislav Petkovd236d742008-04-18 00:46:27 +02001750static void idetape_create_inquiry_cmd(struct ide_atapi_pc *pc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001751{
1752 idetape_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +01001753 pc->c[0] = INQUIRY;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001754 pc->c[4] = 254;
Borislav Petkovd236d742008-04-18 00:46:27 +02001755 pc->req_xfer = 254;
1756 pc->idetape_callback = &idetape_pc_callback;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001757}
1758
Borislav Petkovd236d742008-04-18 00:46:27 +02001759static void idetape_create_rewind_cmd(ide_drive_t *drive,
1760 struct ide_atapi_pc *pc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001761{
1762 idetape_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +01001763 pc->c[0] = REZERO_UNIT;
Borislav Petkov346331f2008-04-18 00:46:26 +02001764 pc->flags |= PC_FLAG_WAIT_FOR_DSC;
Borislav Petkovd236d742008-04-18 00:46:27 +02001765 pc->idetape_callback = &idetape_pc_callback;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001766}
1767
Borislav Petkovd236d742008-04-18 00:46:27 +02001768static void idetape_create_erase_cmd(struct ide_atapi_pc *pc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001769{
1770 idetape_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +01001771 pc->c[0] = ERASE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001772 pc->c[1] = 1;
Borislav Petkov346331f2008-04-18 00:46:26 +02001773 pc->flags |= PC_FLAG_WAIT_FOR_DSC;
Borislav Petkovd236d742008-04-18 00:46:27 +02001774 pc->idetape_callback = &idetape_pc_callback;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001775}
1776
Borislav Petkovd236d742008-04-18 00:46:27 +02001777static void idetape_create_space_cmd(struct ide_atapi_pc *pc, int count, u8 cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001778{
1779 idetape_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +01001780 pc->c[0] = SPACE;
Borislav Petkov860ff5e2008-02-02 19:56:50 +01001781 put_unaligned(cpu_to_be32(count), (unsigned int *) &pc->c[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001782 pc->c[1] = cmd;
Borislav Petkov346331f2008-04-18 00:46:26 +02001783 pc->flags |= PC_FLAG_WAIT_FOR_DSC;
Borislav Petkovd236d742008-04-18 00:46:27 +02001784 pc->idetape_callback = &idetape_pc_callback;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001785}
1786
Borislav Petkov97c566c2008-04-27 15:38:25 +02001787/* Queue up a character device originated write request. */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001788static int idetape_add_chrdev_write_request(ide_drive_t *drive, int blocks)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001789{
1790 idetape_tape_t *tape = drive->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001791
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001792 debug_log(DBG_CHRDEV, "Enter %s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001793
Borislav Petkov0aa4b012008-04-27 15:38:27 +02001794 return idetape_queue_rw_tail(drive, REQ_IDETAPE_WRITE,
1795 blocks, tape->merge_stage->bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001796}
1797
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001798static void idetape_empty_write_pipeline(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001799{
1800 idetape_tape_t *tape = drive->driver_data;
1801 int blocks, min;
1802 struct idetape_bh *bh;
Borislav Petkov55a5d292008-02-02 19:56:49 +01001803
Borislav Petkov54abf372008-02-06 02:57:52 +01001804 if (tape->chrdev_dir != IDETAPE_DIR_WRITE) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001805 printk(KERN_ERR "ide-tape: bug: Trying to empty write pipeline,"
1806 " but we are not writing.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807 return;
1808 }
1809 if (tape->merge_stage_size > tape->stage_size) {
1810 printk(KERN_ERR "ide-tape: bug: merge_buffer too big\n");
1811 tape->merge_stage_size = tape->stage_size;
1812 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001813 if (tape->merge_stage_size) {
Borislav Petkov54bb2072008-02-06 02:57:52 +01001814 blocks = tape->merge_stage_size / tape->blk_size;
1815 if (tape->merge_stage_size % tape->blk_size) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001816 unsigned int i;
1817
1818 blocks++;
Borislav Petkov54bb2072008-02-06 02:57:52 +01001819 i = tape->blk_size - tape->merge_stage_size %
1820 tape->blk_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001821 bh = tape->bh->b_reqnext;
1822 while (bh) {
1823 atomic_set(&bh->b_count, 0);
1824 bh = bh->b_reqnext;
1825 }
1826 bh = tape->bh;
1827 while (i) {
1828 if (bh == NULL) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001829 printk(KERN_INFO "ide-tape: bug,"
1830 " bh NULL\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001831 break;
1832 }
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001833 min = min(i, (unsigned int)(bh->b_size -
1834 atomic_read(&bh->b_count)));
1835 memset(bh->b_data + atomic_read(&bh->b_count),
1836 0, min);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001837 atomic_add(min, &bh->b_count);
1838 i -= min;
1839 bh = bh->b_reqnext;
1840 }
1841 }
1842 (void) idetape_add_chrdev_write_request(drive, blocks);
1843 tape->merge_stage_size = 0;
1844 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001845 if (tape->merge_stage != NULL) {
1846 __idetape_kfree_stage(tape->merge_stage);
1847 tape->merge_stage = NULL;
1848 }
Borislav Petkov54abf372008-02-06 02:57:52 +01001849 tape->chrdev_dir = IDETAPE_DIR_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001850}
1851
Borislav Petkov83042b22008-04-27 15:38:27 +02001852static int idetape_init_read(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001853{
1854 idetape_tape_t *tape = drive->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001855 int bytes_read;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001856
1857 /* Initialize read operation */
Borislav Petkov54abf372008-02-06 02:57:52 +01001858 if (tape->chrdev_dir != IDETAPE_DIR_READ) {
1859 if (tape->chrdev_dir == IDETAPE_DIR_WRITE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001860 idetape_empty_write_pipeline(drive);
1861 idetape_flush_tape_buffers(drive);
1862 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001863 if (tape->merge_stage || tape->merge_stage_size) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001864 printk(KERN_ERR "ide-tape: merge_stage_size should be"
1865 " 0 now\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001866 tape->merge_stage_size = 0;
1867 }
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001868 tape->merge_stage = __idetape_kmalloc_stage(tape, 0, 0);
1869 if (!tape->merge_stage)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001870 return -ENOMEM;
Borislav Petkov54abf372008-02-06 02:57:52 +01001871 tape->chrdev_dir = IDETAPE_DIR_READ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001872
1873 /*
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001874 * Issue a read 0 command to ensure that DSC handshake is
1875 * switched from completion mode to buffer available mode.
1876 * No point in issuing this if DSC overlap isn't supported, some
1877 * drives (Seagate STT3401A) will return an error.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001878 */
1879 if (drive->dsc_overlap) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001880 bytes_read = idetape_queue_rw_tail(drive,
1881 REQ_IDETAPE_READ, 0,
1882 tape->merge_stage->bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001883 if (bytes_read < 0) {
1884 __idetape_kfree_stage(tape->merge_stage);
1885 tape->merge_stage = NULL;
Borislav Petkov54abf372008-02-06 02:57:52 +01001886 tape->chrdev_dir = IDETAPE_DIR_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001887 return bytes_read;
1888 }
1889 }
1890 }
Borislav Petkov5e69bd92008-04-27 15:38:25 +02001891
Linus Torvalds1da177e2005-04-16 15:20:36 -07001892 return 0;
1893}
1894
1895/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001896 * Called from idetape_chrdev_read() to service a character device read request
1897 * and add read-ahead requests to our pipeline.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001898 */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001899static int idetape_add_chrdev_read_request(ide_drive_t *drive, int blocks)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001900{
1901 idetape_tape_t *tape = drive->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001902
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001903 debug_log(DBG_PROCS, "Enter %s, %d blocks\n", __func__, blocks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001904
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001905 /* If we are at a filemark, return a read length of 0 */
Borislav Petkov346331f2008-04-18 00:46:26 +02001906 if (test_bit(IDETAPE_FLAG_FILEMARK, &tape->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001907 return 0;
1908
Borislav Petkov83042b22008-04-27 15:38:27 +02001909 idetape_init_read(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001910
Borislav Petkov5e69bd92008-04-27 15:38:25 +02001911 return idetape_queue_rw_tail(drive, REQ_IDETAPE_READ, blocks,
1912 tape->merge_stage->bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001913}
1914
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001915static void idetape_pad_zeros(ide_drive_t *drive, int bcount)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001916{
1917 idetape_tape_t *tape = drive->driver_data;
1918 struct idetape_bh *bh;
1919 int blocks;
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001920
Linus Torvalds1da177e2005-04-16 15:20:36 -07001921 while (bcount) {
1922 unsigned int count;
1923
1924 bh = tape->merge_stage->bh;
1925 count = min(tape->stage_size, bcount);
1926 bcount -= count;
Borislav Petkov54bb2072008-02-06 02:57:52 +01001927 blocks = count / tape->blk_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001928 while (count) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001929 atomic_set(&bh->b_count,
1930 min(count, (unsigned int)bh->b_size));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001931 memset(bh->b_data, 0, atomic_read(&bh->b_count));
1932 count -= atomic_read(&bh->b_count);
1933 bh = bh->b_reqnext;
1934 }
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001935 idetape_queue_rw_tail(drive, REQ_IDETAPE_WRITE, blocks,
1936 tape->merge_stage->bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001937 }
1938}
1939
Linus Torvalds1da177e2005-04-16 15:20:36 -07001940/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001941 * Rewinds the tape to the Beginning Of the current Partition (BOP). We
1942 * currently support only one partition.
1943 */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001944static int idetape_rewind_tape(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001945{
1946 int retval;
Borislav Petkovd236d742008-04-18 00:46:27 +02001947 struct ide_atapi_pc pc;
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001948 idetape_tape_t *tape;
1949 tape = drive->driver_data;
1950
1951 debug_log(DBG_SENSE, "Enter %s\n", __func__);
1952
Linus Torvalds1da177e2005-04-16 15:20:36 -07001953 idetape_create_rewind_cmd(drive, &pc);
1954 retval = idetape_queue_pc_tail(drive, &pc);
1955 if (retval)
1956 return retval;
1957
1958 idetape_create_read_position_cmd(&pc);
1959 retval = idetape_queue_pc_tail(drive, &pc);
1960 if (retval)
1961 return retval;
1962 return 0;
1963}
1964
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001965/* mtio.h compatible commands should be issued to the chrdev interface. */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001966static int idetape_blkdev_ioctl(ide_drive_t *drive, unsigned int cmd,
1967 unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001968{
1969 idetape_tape_t *tape = drive->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001970 void __user *argp = (void __user *)arg;
1971
Borislav Petkovd59823f2008-02-02 19:56:51 +01001972 struct idetape_config {
1973 int dsc_rw_frequency;
1974 int dsc_media_access_frequency;
1975 int nr_stages;
1976 } config;
1977
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001978 debug_log(DBG_PROCS, "Enter %s\n", __func__);
1979
Linus Torvalds1da177e2005-04-16 15:20:36 -07001980 switch (cmd) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001981 case 0x0340:
1982 if (copy_from_user(&config, argp, sizeof(config)))
1983 return -EFAULT;
1984 tape->best_dsc_rw_freq = config.dsc_rw_frequency;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001985 break;
1986 case 0x0350:
1987 config.dsc_rw_frequency = (int) tape->best_dsc_rw_freq;
Borislav Petkov83042b22008-04-27 15:38:27 +02001988 config.nr_stages = 1;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001989 if (copy_to_user(argp, &config, sizeof(config)))
1990 return -EFAULT;
1991 break;
1992 default:
1993 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001994 }
1995 return 0;
1996}
1997
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001998static int idetape_space_over_filemarks(ide_drive_t *drive, short mt_op,
1999 int mt_count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002000{
2001 idetape_tape_t *tape = drive->driver_data;
Borislav Petkovd236d742008-04-18 00:46:27 +02002002 struct ide_atapi_pc pc;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002003 int retval, count = 0;
Borislav Petkovb6422012008-02-02 19:56:49 +01002004 int sprev = !!(tape->caps[4] & 0x20);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002005
2006 if (mt_count == 0)
2007 return 0;
2008 if (MTBSF == mt_op || MTBSFM == mt_op) {
Borislav Petkovb6422012008-02-02 19:56:49 +01002009 if (!sprev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002010 return -EIO;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002011 mt_count = -mt_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002012 }
2013
Borislav Petkov54abf372008-02-06 02:57:52 +01002014 if (tape->chrdev_dir == IDETAPE_DIR_READ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002015 tape->merge_stage_size = 0;
Borislav Petkov346331f2008-04-18 00:46:26 +02002016 if (test_and_clear_bit(IDETAPE_FLAG_FILEMARK, &tape->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002017 ++count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002018 idetape_discard_read_pipeline(drive, 0);
2019 }
2020
2021 /*
Borislav Petkov3c98bf32008-02-06 02:57:53 +01002022 * The filemark was not found in our internal pipeline; now we can issue
2023 * the space command.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002024 */
2025 switch (mt_op) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002026 case MTFSF:
2027 case MTBSF:
2028 idetape_create_space_cmd(&pc, mt_count - count,
2029 IDETAPE_SPACE_OVER_FILEMARK);
2030 return idetape_queue_pc_tail(drive, &pc);
2031 case MTFSFM:
2032 case MTBSFM:
2033 if (!sprev)
2034 return -EIO;
2035 retval = idetape_space_over_filemarks(drive, MTFSF,
2036 mt_count - count);
2037 if (retval)
2038 return retval;
2039 count = (MTBSFM == mt_op ? 1 : -1);
2040 return idetape_space_over_filemarks(drive, MTFSF, count);
2041 default:
2042 printk(KERN_ERR "ide-tape: MTIO operation %d not supported\n",
2043 mt_op);
2044 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002045 }
2046}
2047
Linus Torvalds1da177e2005-04-16 15:20:36 -07002048/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +01002049 * Our character device read / write functions.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002050 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +01002051 * The tape is optimized to maximize throughput when it is transferring an
2052 * integral number of the "continuous transfer limit", which is a parameter of
2053 * the specific tape (26kB on my particular tape, 32kB for Onstream).
Linus Torvalds1da177e2005-04-16 15:20:36 -07002054 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +01002055 * As of version 1.3 of the driver, the character device provides an abstract
2056 * continuous view of the media - any mix of block sizes (even 1 byte) on the
2057 * same backup/restore procedure is supported. The driver will internally
2058 * convert the requests to the recommended transfer unit, so that an unmatch
2059 * between the user's block size to the recommended size will only result in a
2060 * (slightly) increased driver overhead, but will no longer hit performance.
2061 * This is not applicable to Onstream.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002062 */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002063static ssize_t idetape_chrdev_read(struct file *file, char __user *buf,
2064 size_t count, loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002065{
2066 struct ide_tape_obj *tape = ide_tape_f(file);
2067 ide_drive_t *drive = tape->drive;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002068 ssize_t bytes_read, temp, actually_read = 0, rc;
Daniel Walkerdcd96372006-06-25 05:47:37 -07002069 ssize_t ret = 0;
Borislav Petkovb6422012008-02-02 19:56:49 +01002070 u16 ctl = *(u16 *)&tape->caps[12];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002071
Borislav Petkov8004a8c2008-02-06 02:57:51 +01002072 debug_log(DBG_CHRDEV, "Enter %s, count %Zd\n", __func__, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002073
Borislav Petkov54abf372008-02-06 02:57:52 +01002074 if (tape->chrdev_dir != IDETAPE_DIR_READ) {
Borislav Petkov346331f2008-04-18 00:46:26 +02002075 if (test_bit(IDETAPE_FLAG_DETECT_BS, &tape->flags))
Borislav Petkov54bb2072008-02-06 02:57:52 +01002076 if (count > tape->blk_size &&
2077 (count % tape->blk_size) == 0)
2078 tape->user_bs_factor = count / tape->blk_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002079 }
Borislav Petkov83042b22008-04-27 15:38:27 +02002080 rc = idetape_init_read(drive);
Borislav Petkov8d06bfa2008-02-06 02:57:53 +01002081 if (rc < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002082 return rc;
2083 if (count == 0)
2084 return (0);
2085 if (tape->merge_stage_size) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002086 actually_read = min((unsigned int)(tape->merge_stage_size),
2087 (unsigned int)count);
Borislav Petkov99d74e62008-04-27 15:38:25 +02002088 if (idetape_copy_stage_to_user(tape, buf, actually_read))
Daniel Walkerdcd96372006-06-25 05:47:37 -07002089 ret = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002090 buf += actually_read;
2091 tape->merge_stage_size -= actually_read;
2092 count -= actually_read;
2093 }
2094 while (count >= tape->stage_size) {
Borislav Petkovb6422012008-02-02 19:56:49 +01002095 bytes_read = idetape_add_chrdev_read_request(drive, ctl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002096 if (bytes_read <= 0)
2097 goto finish;
Borislav Petkov99d74e62008-04-27 15:38:25 +02002098 if (idetape_copy_stage_to_user(tape, buf, bytes_read))
Daniel Walkerdcd96372006-06-25 05:47:37 -07002099 ret = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002100 buf += bytes_read;
2101 count -= bytes_read;
2102 actually_read += bytes_read;
2103 }
2104 if (count) {
Borislav Petkovb6422012008-02-02 19:56:49 +01002105 bytes_read = idetape_add_chrdev_read_request(drive, ctl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002106 if (bytes_read <= 0)
2107 goto finish;
2108 temp = min((unsigned long)count, (unsigned long)bytes_read);
Borislav Petkov99d74e62008-04-27 15:38:25 +02002109 if (idetape_copy_stage_to_user(tape, buf, temp))
Daniel Walkerdcd96372006-06-25 05:47:37 -07002110 ret = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002111 actually_read += temp;
2112 tape->merge_stage_size = bytes_read-temp;
2113 }
2114finish:
Borislav Petkov346331f2008-04-18 00:46:26 +02002115 if (!actually_read && test_bit(IDETAPE_FLAG_FILEMARK, &tape->flags)) {
Borislav Petkov8004a8c2008-02-06 02:57:51 +01002116 debug_log(DBG_SENSE, "%s: spacing over filemark\n", tape->name);
2117
Linus Torvalds1da177e2005-04-16 15:20:36 -07002118 idetape_space_over_filemarks(drive, MTFSF, 1);
2119 return 0;
2120 }
Daniel Walkerdcd96372006-06-25 05:47:37 -07002121
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002122 return ret ? ret : actually_read;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002123}
2124
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002125static ssize_t idetape_chrdev_write(struct file *file, const char __user *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002126 size_t count, loff_t *ppos)
2127{
2128 struct ide_tape_obj *tape = ide_tape_f(file);
2129 ide_drive_t *drive = tape->drive;
Daniel Walkerdcd96372006-06-25 05:47:37 -07002130 ssize_t actually_written = 0;
2131 ssize_t ret = 0;
Borislav Petkovb6422012008-02-02 19:56:49 +01002132 u16 ctl = *(u16 *)&tape->caps[12];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002133
2134 /* The drive is write protected. */
2135 if (tape->write_prot)
2136 return -EACCES;
2137
Borislav Petkov8004a8c2008-02-06 02:57:51 +01002138 debug_log(DBG_CHRDEV, "Enter %s, count %Zd\n", __func__, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002139
2140 /* Initialize write operation */
Borislav Petkov54abf372008-02-06 02:57:52 +01002141 if (tape->chrdev_dir != IDETAPE_DIR_WRITE) {
2142 if (tape->chrdev_dir == IDETAPE_DIR_READ)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002143 idetape_discard_read_pipeline(drive, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002144 if (tape->merge_stage || tape->merge_stage_size) {
2145 printk(KERN_ERR "ide-tape: merge_stage_size "
2146 "should be 0 now\n");
2147 tape->merge_stage_size = 0;
2148 }
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002149 tape->merge_stage = __idetape_kmalloc_stage(tape, 0, 0);
2150 if (!tape->merge_stage)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002151 return -ENOMEM;
Borislav Petkov54abf372008-02-06 02:57:52 +01002152 tape->chrdev_dir = IDETAPE_DIR_WRITE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002153 idetape_init_merge_stage(tape);
2154
2155 /*
Borislav Petkov3c98bf32008-02-06 02:57:53 +01002156 * Issue a write 0 command to ensure that DSC handshake is
2157 * switched from completion mode to buffer available mode. No
2158 * point in issuing this if DSC overlap isn't supported, some
2159 * drives (Seagate STT3401A) will return an error.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002160 */
2161 if (drive->dsc_overlap) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002162 ssize_t retval = idetape_queue_rw_tail(drive,
2163 REQ_IDETAPE_WRITE, 0,
2164 tape->merge_stage->bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002165 if (retval < 0) {
2166 __idetape_kfree_stage(tape->merge_stage);
2167 tape->merge_stage = NULL;
Borislav Petkov54abf372008-02-06 02:57:52 +01002168 tape->chrdev_dir = IDETAPE_DIR_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002169 return retval;
2170 }
2171 }
2172 }
2173 if (count == 0)
2174 return (0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002175 if (tape->merge_stage_size) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002176 if (tape->merge_stage_size >= tape->stage_size) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002177 printk(KERN_ERR "ide-tape: bug: merge buf too big\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002178 tape->merge_stage_size = 0;
2179 }
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002180 actually_written = min((unsigned int)
2181 (tape->stage_size - tape->merge_stage_size),
2182 (unsigned int)count);
Borislav Petkov8646c882008-04-27 15:38:26 +02002183 if (idetape_copy_stage_from_user(tape, buf, actually_written))
Daniel Walkerdcd96372006-06-25 05:47:37 -07002184 ret = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002185 buf += actually_written;
2186 tape->merge_stage_size += actually_written;
2187 count -= actually_written;
2188
2189 if (tape->merge_stage_size == tape->stage_size) {
Daniel Walkerdcd96372006-06-25 05:47:37 -07002190 ssize_t retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002191 tape->merge_stage_size = 0;
Borislav Petkovb6422012008-02-02 19:56:49 +01002192 retval = idetape_add_chrdev_write_request(drive, ctl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002193 if (retval <= 0)
2194 return (retval);
2195 }
2196 }
2197 while (count >= tape->stage_size) {
Daniel Walkerdcd96372006-06-25 05:47:37 -07002198 ssize_t retval;
Borislav Petkov8646c882008-04-27 15:38:26 +02002199 if (idetape_copy_stage_from_user(tape, buf, tape->stage_size))
Daniel Walkerdcd96372006-06-25 05:47:37 -07002200 ret = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002201 buf += tape->stage_size;
2202 count -= tape->stage_size;
Borislav Petkovb6422012008-02-02 19:56:49 +01002203 retval = idetape_add_chrdev_write_request(drive, ctl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002204 actually_written += tape->stage_size;
2205 if (retval <= 0)
2206 return (retval);
2207 }
2208 if (count) {
2209 actually_written += count;
Borislav Petkov8646c882008-04-27 15:38:26 +02002210 if (idetape_copy_stage_from_user(tape, buf, count))
Daniel Walkerdcd96372006-06-25 05:47:37 -07002211 ret = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002212 tape->merge_stage_size += count;
2213 }
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002214 return ret ? ret : actually_written;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002215}
2216
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002217static int idetape_write_filemark(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002218{
Borislav Petkovd236d742008-04-18 00:46:27 +02002219 struct ide_atapi_pc pc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002220
2221 /* Write a filemark */
2222 idetape_create_write_filemark_cmd(drive, &pc, 1);
2223 if (idetape_queue_pc_tail(drive, &pc)) {
2224 printk(KERN_ERR "ide-tape: Couldn't write a filemark\n");
2225 return -EIO;
2226 }
2227 return 0;
2228}
2229
2230/*
Borislav Petkovd99c9da2008-02-02 19:56:51 +01002231 * Called from idetape_chrdev_ioctl when the general mtio MTIOCTOP ioctl is
2232 * requested.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002233 *
Borislav Petkovd99c9da2008-02-02 19:56:51 +01002234 * Note: MTBSF and MTBSFM are not supported when the tape doesn't support
2235 * spacing over filemarks in the reverse direction. In this case, MTFSFM is also
2236 * usually not supported (it is supported in the rare case in which we crossed
2237 * the filemark during our read-ahead pipelined operation mode).
Linus Torvalds1da177e2005-04-16 15:20:36 -07002238 *
Borislav Petkovd99c9da2008-02-02 19:56:51 +01002239 * The following commands are currently not supported:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002240 *
Borislav Petkovd99c9da2008-02-02 19:56:51 +01002241 * MTFSS, MTBSS, MTWSM, MTSETDENSITY, MTSETDRVBUFFER, MT_ST_BOOLEANS,
2242 * MT_ST_WRITE_THRESHOLD.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002243 */
Borislav Petkovd99c9da2008-02-02 19:56:51 +01002244static int idetape_mtioctop(ide_drive_t *drive, short mt_op, int mt_count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002245{
2246 idetape_tape_t *tape = drive->driver_data;
Borislav Petkovd236d742008-04-18 00:46:27 +02002247 struct ide_atapi_pc pc;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002248 int i, retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002249
Borislav Petkov8004a8c2008-02-06 02:57:51 +01002250 debug_log(DBG_ERR, "Handling MTIOCTOP ioctl: mt_op=%d, mt_count=%d\n",
2251 mt_op, mt_count);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002252
Borislav Petkov3c98bf32008-02-06 02:57:53 +01002253 /* Commands which need our pipelined read-ahead stages. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002254 switch (mt_op) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002255 case MTFSF:
2256 case MTFSFM:
2257 case MTBSF:
2258 case MTBSFM:
2259 if (!mt_count)
2260 return 0;
2261 return idetape_space_over_filemarks(drive, mt_op, mt_count);
2262 default:
2263 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002264 }
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002265
Linus Torvalds1da177e2005-04-16 15:20:36 -07002266 switch (mt_op) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002267 case MTWEOF:
2268 if (tape->write_prot)
2269 return -EACCES;
2270 idetape_discard_read_pipeline(drive, 1);
2271 for (i = 0; i < mt_count; i++) {
2272 retval = idetape_write_filemark(drive);
2273 if (retval)
2274 return retval;
2275 }
2276 return 0;
2277 case MTREW:
2278 idetape_discard_read_pipeline(drive, 0);
2279 if (idetape_rewind_tape(drive))
2280 return -EIO;
2281 return 0;
2282 case MTLOAD:
2283 idetape_discard_read_pipeline(drive, 0);
2284 idetape_create_load_unload_cmd(drive, &pc,
2285 IDETAPE_LU_LOAD_MASK);
2286 return idetape_queue_pc_tail(drive, &pc);
2287 case MTUNLOAD:
2288 case MTOFFL:
2289 /*
2290 * If door is locked, attempt to unlock before
2291 * attempting to eject.
2292 */
2293 if (tape->door_locked) {
2294 if (idetape_create_prevent_cmd(drive, &pc, 0))
2295 if (!idetape_queue_pc_tail(drive, &pc))
2296 tape->door_locked = DOOR_UNLOCKED;
2297 }
2298 idetape_discard_read_pipeline(drive, 0);
2299 idetape_create_load_unload_cmd(drive, &pc,
2300 !IDETAPE_LU_LOAD_MASK);
2301 retval = idetape_queue_pc_tail(drive, &pc);
2302 if (!retval)
Borislav Petkov346331f2008-04-18 00:46:26 +02002303 clear_bit(IDETAPE_FLAG_MEDIUM_PRESENT, &tape->flags);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002304 return retval;
2305 case MTNOP:
2306 idetape_discard_read_pipeline(drive, 0);
2307 return idetape_flush_tape_buffers(drive);
2308 case MTRETEN:
2309 idetape_discard_read_pipeline(drive, 0);
2310 idetape_create_load_unload_cmd(drive, &pc,
2311 IDETAPE_LU_RETENSION_MASK | IDETAPE_LU_LOAD_MASK);
2312 return idetape_queue_pc_tail(drive, &pc);
2313 case MTEOM:
2314 idetape_create_space_cmd(&pc, 0, IDETAPE_SPACE_TO_EOD);
2315 return idetape_queue_pc_tail(drive, &pc);
2316 case MTERASE:
2317 (void)idetape_rewind_tape(drive);
2318 idetape_create_erase_cmd(&pc);
2319 return idetape_queue_pc_tail(drive, &pc);
2320 case MTSETBLK:
2321 if (mt_count) {
2322 if (mt_count < tape->blk_size ||
2323 mt_count % tape->blk_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002324 return -EIO;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002325 tape->user_bs_factor = mt_count / tape->blk_size;
Borislav Petkov346331f2008-04-18 00:46:26 +02002326 clear_bit(IDETAPE_FLAG_DETECT_BS, &tape->flags);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002327 } else
Borislav Petkov346331f2008-04-18 00:46:26 +02002328 set_bit(IDETAPE_FLAG_DETECT_BS, &tape->flags);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002329 return 0;
2330 case MTSEEK:
2331 idetape_discard_read_pipeline(drive, 0);
2332 return idetape_position_tape(drive,
2333 mt_count * tape->user_bs_factor, tape->partition, 0);
2334 case MTSETPART:
2335 idetape_discard_read_pipeline(drive, 0);
2336 return idetape_position_tape(drive, 0, mt_count, 0);
2337 case MTFSR:
2338 case MTBSR:
2339 case MTLOCK:
2340 if (!idetape_create_prevent_cmd(drive, &pc, 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002341 return 0;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002342 retval = idetape_queue_pc_tail(drive, &pc);
2343 if (retval)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002344 return retval;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002345 tape->door_locked = DOOR_EXPLICITLY_LOCKED;
2346 return 0;
2347 case MTUNLOCK:
2348 if (!idetape_create_prevent_cmd(drive, &pc, 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002349 return 0;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002350 retval = idetape_queue_pc_tail(drive, &pc);
2351 if (retval)
2352 return retval;
2353 tape->door_locked = DOOR_UNLOCKED;
2354 return 0;
2355 default:
2356 printk(KERN_ERR "ide-tape: MTIO operation %d not supported\n",
2357 mt_op);
2358 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002359 }
2360}
2361
2362/*
Borislav Petkovd99c9da2008-02-02 19:56:51 +01002363 * Our character device ioctls. General mtio.h magnetic io commands are
2364 * supported here, and not in the corresponding block interface. Our own
2365 * ide-tape ioctls are supported on both interfaces.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002366 */
Borislav Petkovd99c9da2008-02-02 19:56:51 +01002367static int idetape_chrdev_ioctl(struct inode *inode, struct file *file,
2368 unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002369{
2370 struct ide_tape_obj *tape = ide_tape_f(file);
2371 ide_drive_t *drive = tape->drive;
2372 struct mtop mtop;
2373 struct mtget mtget;
2374 struct mtpos mtpos;
Borislav Petkov54bb2072008-02-06 02:57:52 +01002375 int block_offset = 0, position = tape->first_frame;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002376 void __user *argp = (void __user *)arg;
2377
Borislav Petkov8004a8c2008-02-06 02:57:51 +01002378 debug_log(DBG_CHRDEV, "Enter %s, cmd=%u\n", __func__, cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002379
Borislav Petkov54abf372008-02-06 02:57:52 +01002380 if (tape->chrdev_dir == IDETAPE_DIR_WRITE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002381 idetape_empty_write_pipeline(drive);
2382 idetape_flush_tape_buffers(drive);
2383 }
2384 if (cmd == MTIOCGET || cmd == MTIOCPOS) {
Borislav Petkovb361acb2008-04-27 15:38:26 +02002385 block_offset = tape->merge_stage_size /
Borislav Petkov54bb2072008-02-06 02:57:52 +01002386 (tape->blk_size * tape->user_bs_factor);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002387 position = idetape_read_position(drive);
2388 if (position < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002389 return -EIO;
2390 }
2391 switch (cmd) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002392 case MTIOCTOP:
2393 if (copy_from_user(&mtop, argp, sizeof(struct mtop)))
2394 return -EFAULT;
2395 return idetape_mtioctop(drive, mtop.mt_op, mtop.mt_count);
2396 case MTIOCGET:
2397 memset(&mtget, 0, sizeof(struct mtget));
2398 mtget.mt_type = MT_ISSCSI2;
2399 mtget.mt_blkno = position / tape->user_bs_factor - block_offset;
2400 mtget.mt_dsreg =
2401 ((tape->blk_size * tape->user_bs_factor)
2402 << MT_ST_BLKSIZE_SHIFT) & MT_ST_BLKSIZE_MASK;
Borislav Petkov54bb2072008-02-06 02:57:52 +01002403
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002404 if (tape->drv_write_prot)
2405 mtget.mt_gstat |= GMT_WR_PROT(0xffffffff);
2406
2407 if (copy_to_user(argp, &mtget, sizeof(struct mtget)))
2408 return -EFAULT;
2409 return 0;
2410 case MTIOCPOS:
2411 mtpos.mt_blkno = position / tape->user_bs_factor - block_offset;
2412 if (copy_to_user(argp, &mtpos, sizeof(struct mtpos)))
2413 return -EFAULT;
2414 return 0;
2415 default:
2416 if (tape->chrdev_dir == IDETAPE_DIR_READ)
2417 idetape_discard_read_pipeline(drive, 1);
2418 return idetape_blkdev_ioctl(drive, cmd, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002419 }
2420}
2421
Borislav Petkov3cffb9c2008-02-02 19:56:50 +01002422/*
2423 * Do a mode sense page 0 with block descriptor and if it succeeds set the tape
2424 * block size with the reported value.
2425 */
2426static void ide_tape_get_bsize_from_bdesc(ide_drive_t *drive)
2427{
2428 idetape_tape_t *tape = drive->driver_data;
Borislav Petkovd236d742008-04-18 00:46:27 +02002429 struct ide_atapi_pc pc;
Borislav Petkov3cffb9c2008-02-02 19:56:50 +01002430
2431 idetape_create_mode_sense_cmd(&pc, IDETAPE_BLOCK_DESCRIPTOR);
2432 if (idetape_queue_pc_tail(drive, &pc)) {
2433 printk(KERN_ERR "ide-tape: Can't get block descriptor\n");
Borislav Petkov54bb2072008-02-06 02:57:52 +01002434 if (tape->blk_size == 0) {
Borislav Petkov3cffb9c2008-02-02 19:56:50 +01002435 printk(KERN_WARNING "ide-tape: Cannot deal with zero "
2436 "block size, assuming 32k\n");
Borislav Petkov54bb2072008-02-06 02:57:52 +01002437 tape->blk_size = 32768;
Borislav Petkov3cffb9c2008-02-02 19:56:50 +01002438 }
2439 return;
2440 }
Borislav Petkovd236d742008-04-18 00:46:27 +02002441 tape->blk_size = (pc.buf[4 + 5] << 16) +
2442 (pc.buf[4 + 6] << 8) +
2443 pc.buf[4 + 7];
2444 tape->drv_write_prot = (pc.buf[2] & 0x80) >> 7;
Borislav Petkov3cffb9c2008-02-02 19:56:50 +01002445}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002446
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002447static int idetape_chrdev_open(struct inode *inode, struct file *filp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002448{
2449 unsigned int minor = iminor(inode), i = minor & ~0xc0;
2450 ide_drive_t *drive;
2451 idetape_tape_t *tape;
Borislav Petkovd236d742008-04-18 00:46:27 +02002452 struct ide_atapi_pc pc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002453 int retval;
2454
Borislav Petkov8004a8c2008-02-06 02:57:51 +01002455 if (i >= MAX_HWIFS * MAX_DRIVES)
2456 return -ENXIO;
2457
2458 tape = ide_tape_chrdev_get(i);
2459 if (!tape)
2460 return -ENXIO;
2461
2462 debug_log(DBG_CHRDEV, "Enter %s\n", __func__);
2463
Linus Torvalds1da177e2005-04-16 15:20:36 -07002464 /*
2465 * We really want to do nonseekable_open(inode, filp); here, but some
2466 * versions of tar incorrectly call lseek on tapes and bail out if that
2467 * fails. So we disallow pread() and pwrite(), but permit lseeks.
2468 */
2469 filp->f_mode &= ~(FMODE_PREAD | FMODE_PWRITE);
2470
Linus Torvalds1da177e2005-04-16 15:20:36 -07002471 drive = tape->drive;
2472
2473 filp->private_data = tape;
2474
Borislav Petkov346331f2008-04-18 00:46:26 +02002475 if (test_and_set_bit(IDETAPE_FLAG_BUSY, &tape->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002476 retval = -EBUSY;
2477 goto out_put_tape;
2478 }
2479
2480 retval = idetape_wait_ready(drive, 60 * HZ);
2481 if (retval) {
Borislav Petkov346331f2008-04-18 00:46:26 +02002482 clear_bit(IDETAPE_FLAG_BUSY, &tape->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002483 printk(KERN_ERR "ide-tape: %s: drive not ready\n", tape->name);
2484 goto out_put_tape;
2485 }
2486
2487 idetape_read_position(drive);
Borislav Petkov346331f2008-04-18 00:46:26 +02002488 if (!test_bit(IDETAPE_FLAG_ADDRESS_VALID, &tape->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002489 (void)idetape_rewind_tape(drive);
2490
Linus Torvalds1da177e2005-04-16 15:20:36 -07002491 /* Read block size and write protect status from drive. */
Borislav Petkov3cffb9c2008-02-02 19:56:50 +01002492 ide_tape_get_bsize_from_bdesc(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002493
2494 /* Set write protect flag if device is opened as read-only. */
2495 if ((filp->f_flags & O_ACCMODE) == O_RDONLY)
2496 tape->write_prot = 1;
2497 else
2498 tape->write_prot = tape->drv_write_prot;
2499
2500 /* Make sure drive isn't write protected if user wants to write. */
2501 if (tape->write_prot) {
2502 if ((filp->f_flags & O_ACCMODE) == O_WRONLY ||
2503 (filp->f_flags & O_ACCMODE) == O_RDWR) {
Borislav Petkov346331f2008-04-18 00:46:26 +02002504 clear_bit(IDETAPE_FLAG_BUSY, &tape->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002505 retval = -EROFS;
2506 goto out_put_tape;
2507 }
2508 }
2509
Borislav Petkov3c98bf32008-02-06 02:57:53 +01002510 /* Lock the tape drive door so user can't eject. */
Borislav Petkov54abf372008-02-06 02:57:52 +01002511 if (tape->chrdev_dir == IDETAPE_DIR_NONE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002512 if (idetape_create_prevent_cmd(drive, &pc, 1)) {
2513 if (!idetape_queue_pc_tail(drive, &pc)) {
2514 if (tape->door_locked != DOOR_EXPLICITLY_LOCKED)
2515 tape->door_locked = DOOR_LOCKED;
2516 }
2517 }
2518 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002519 return 0;
2520
2521out_put_tape:
2522 ide_tape_put(tape);
2523 return retval;
2524}
2525
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002526static void idetape_write_release(ide_drive_t *drive, unsigned int minor)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002527{
2528 idetape_tape_t *tape = drive->driver_data;
2529
2530 idetape_empty_write_pipeline(drive);
2531 tape->merge_stage = __idetape_kmalloc_stage(tape, 1, 0);
2532 if (tape->merge_stage != NULL) {
Borislav Petkov54bb2072008-02-06 02:57:52 +01002533 idetape_pad_zeros(drive, tape->blk_size *
2534 (tape->user_bs_factor - 1));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002535 __idetape_kfree_stage(tape->merge_stage);
2536 tape->merge_stage = NULL;
2537 }
2538 idetape_write_filemark(drive);
2539 idetape_flush_tape_buffers(drive);
2540 idetape_flush_tape_buffers(drive);
2541}
2542
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002543static int idetape_chrdev_release(struct inode *inode, struct file *filp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002544{
2545 struct ide_tape_obj *tape = ide_tape_f(filp);
2546 ide_drive_t *drive = tape->drive;
Borislav Petkovd236d742008-04-18 00:46:27 +02002547 struct ide_atapi_pc pc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002548 unsigned int minor = iminor(inode);
2549
2550 lock_kernel();
2551 tape = drive->driver_data;
Borislav Petkov8004a8c2008-02-06 02:57:51 +01002552
2553 debug_log(DBG_CHRDEV, "Enter %s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002554
Borislav Petkov54abf372008-02-06 02:57:52 +01002555 if (tape->chrdev_dir == IDETAPE_DIR_WRITE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002556 idetape_write_release(drive, minor);
Borislav Petkov54abf372008-02-06 02:57:52 +01002557 if (tape->chrdev_dir == IDETAPE_DIR_READ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002558 if (minor < 128)
2559 idetape_discard_read_pipeline(drive, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002560 }
Borislav Petkovf64eee72008-04-27 15:38:25 +02002561
Borislav Petkov346331f2008-04-18 00:46:26 +02002562 if (minor < 128 && test_bit(IDETAPE_FLAG_MEDIUM_PRESENT, &tape->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002563 (void) idetape_rewind_tape(drive);
Borislav Petkov54abf372008-02-06 02:57:52 +01002564 if (tape->chrdev_dir == IDETAPE_DIR_NONE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002565 if (tape->door_locked == DOOR_LOCKED) {
2566 if (idetape_create_prevent_cmd(drive, &pc, 0)) {
2567 if (!idetape_queue_pc_tail(drive, &pc))
2568 tape->door_locked = DOOR_UNLOCKED;
2569 }
2570 }
2571 }
Borislav Petkov346331f2008-04-18 00:46:26 +02002572 clear_bit(IDETAPE_FLAG_BUSY, &tape->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002573 ide_tape_put(tape);
2574 unlock_kernel();
2575 return 0;
2576}
2577
2578/*
Borislav Petkov71071b82008-02-06 02:57:53 +01002579 * check the contents of the ATAPI IDENTIFY command results. We return:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002580 *
Borislav Petkov71071b82008-02-06 02:57:53 +01002581 * 1 - If the tape can be supported by us, based on the information we have so
2582 * far.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002583 *
Borislav Petkov71071b82008-02-06 02:57:53 +01002584 * 0 - If this tape driver is not currently supported by us.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002585 */
Borislav Petkov71071b82008-02-06 02:57:53 +01002586static int idetape_identify_device(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002587{
Borislav Petkov71071b82008-02-06 02:57:53 +01002588 u8 gcw[2], protocol, device_type, removable, packet_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002589
2590 if (drive->id_read == 0)
2591 return 1;
2592
Borislav Petkov71071b82008-02-06 02:57:53 +01002593 *((unsigned short *) &gcw) = drive->id->config;
2594
2595 protocol = (gcw[1] & 0xC0) >> 6;
2596 device_type = gcw[1] & 0x1F;
2597 removable = !!(gcw[0] & 0x80);
2598 packet_size = gcw[0] & 0x3;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002599
Linus Torvalds1da177e2005-04-16 15:20:36 -07002600 /* Check that we can support this device */
Borislav Petkov71071b82008-02-06 02:57:53 +01002601 if (protocol != 2)
Bartlomiej Zolnierkiewicz16422de32008-02-02 19:56:48 +01002602 printk(KERN_ERR "ide-tape: Protocol (0x%02x) is not ATAPI\n",
Borislav Petkov71071b82008-02-06 02:57:53 +01002603 protocol);
2604 else if (device_type != 1)
Bartlomiej Zolnierkiewicz16422de32008-02-02 19:56:48 +01002605 printk(KERN_ERR "ide-tape: Device type (0x%02x) is not set "
Borislav Petkov71071b82008-02-06 02:57:53 +01002606 "to tape\n", device_type);
2607 else if (!removable)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002608 printk(KERN_ERR "ide-tape: The removable flag is not set\n");
Borislav Petkov71071b82008-02-06 02:57:53 +01002609 else if (packet_size != 0) {
Borislav Petkov24d57f82008-02-06 02:57:54 +01002610 printk(KERN_ERR "ide-tape: Packet size (0x%02x) is not 12"
2611 " bytes\n", packet_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002612 } else
2613 return 1;
2614 return 0;
2615}
2616
Borislav Petkov6d29c8f2008-02-02 19:56:49 +01002617static void idetape_get_inquiry_results(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002618{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002619 idetape_tape_t *tape = drive->driver_data;
Borislav Petkovd236d742008-04-18 00:46:27 +02002620 struct ide_atapi_pc pc;
Borislav Petkov41f81d542008-02-06 02:57:52 +01002621 char fw_rev[6], vendor_id[10], product_id[18];
Borislav Petkov6d29c8f2008-02-02 19:56:49 +01002622
Linus Torvalds1da177e2005-04-16 15:20:36 -07002623 idetape_create_inquiry_cmd(&pc);
2624 if (idetape_queue_pc_tail(drive, &pc)) {
Borislav Petkov6d29c8f2008-02-02 19:56:49 +01002625 printk(KERN_ERR "ide-tape: %s: can't get INQUIRY results\n",
2626 tape->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002627 return;
2628 }
Borislav Petkovd236d742008-04-18 00:46:27 +02002629 memcpy(vendor_id, &pc.buf[8], 8);
2630 memcpy(product_id, &pc.buf[16], 16);
2631 memcpy(fw_rev, &pc.buf[32], 4);
Borislav Petkov6d29c8f2008-02-02 19:56:49 +01002632
Borislav Petkov41f81d542008-02-06 02:57:52 +01002633 ide_fixstring(vendor_id, 10, 0);
2634 ide_fixstring(product_id, 18, 0);
2635 ide_fixstring(fw_rev, 6, 0);
2636
Borislav Petkov6d29c8f2008-02-02 19:56:49 +01002637 printk(KERN_INFO "ide-tape: %s <-> %s: %s %s rev %s\n",
Borislav Petkov41f81d542008-02-06 02:57:52 +01002638 drive->name, tape->name, vendor_id, product_id, fw_rev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002639}
2640
2641/*
Borislav Petkovb6422012008-02-02 19:56:49 +01002642 * Ask the tape about its various parameters. In particular, we will adjust our
2643 * data transfer buffer size to the recommended value as returned by the tape.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002644 */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002645static void idetape_get_mode_sense_results(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002646{
2647 idetape_tape_t *tape = drive->driver_data;
Borislav Petkovd236d742008-04-18 00:46:27 +02002648 struct ide_atapi_pc pc;
Borislav Petkovb6422012008-02-02 19:56:49 +01002649 u8 *caps;
2650 u8 speed, max_speed;
Borislav Petkov47314fa2008-02-02 19:56:48 +01002651
Linus Torvalds1da177e2005-04-16 15:20:36 -07002652 idetape_create_mode_sense_cmd(&pc, IDETAPE_CAPABILITIES_PAGE);
2653 if (idetape_queue_pc_tail(drive, &pc)) {
Borislav Petkovb6422012008-02-02 19:56:49 +01002654 printk(KERN_ERR "ide-tape: Can't get tape parameters - assuming"
2655 " some default values\n");
Borislav Petkov54bb2072008-02-06 02:57:52 +01002656 tape->blk_size = 512;
Borislav Petkovb6422012008-02-02 19:56:49 +01002657 put_unaligned(52, (u16 *)&tape->caps[12]);
2658 put_unaligned(540, (u16 *)&tape->caps[14]);
2659 put_unaligned(6*52, (u16 *)&tape->caps[16]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002660 return;
2661 }
Borislav Petkovd236d742008-04-18 00:46:27 +02002662 caps = pc.buf + 4 + pc.buf[3];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002663
Borislav Petkovb6422012008-02-02 19:56:49 +01002664 /* convert to host order and save for later use */
2665 speed = be16_to_cpu(*(u16 *)&caps[14]);
2666 max_speed = be16_to_cpu(*(u16 *)&caps[8]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002667
Borislav Petkovb6422012008-02-02 19:56:49 +01002668 put_unaligned(max_speed, (u16 *)&caps[8]);
2669 put_unaligned(be16_to_cpu(*(u16 *)&caps[12]), (u16 *)&caps[12]);
2670 put_unaligned(speed, (u16 *)&caps[14]);
2671 put_unaligned(be16_to_cpu(*(u16 *)&caps[16]), (u16 *)&caps[16]);
2672
2673 if (!speed) {
2674 printk(KERN_INFO "ide-tape: %s: invalid tape speed "
2675 "(assuming 650KB/sec)\n", drive->name);
2676 put_unaligned(650, (u16 *)&caps[14]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002677 }
Borislav Petkovb6422012008-02-02 19:56:49 +01002678 if (!max_speed) {
2679 printk(KERN_INFO "ide-tape: %s: invalid max_speed "
2680 "(assuming 650KB/sec)\n", drive->name);
2681 put_unaligned(650, (u16 *)&caps[8]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002682 }
2683
Borislav Petkovb6422012008-02-02 19:56:49 +01002684 memcpy(&tape->caps, caps, 20);
2685 if (caps[7] & 0x02)
Borislav Petkov54bb2072008-02-06 02:57:52 +01002686 tape->blk_size = 512;
Borislav Petkovb6422012008-02-02 19:56:49 +01002687 else if (caps[7] & 0x04)
Borislav Petkov54bb2072008-02-06 02:57:52 +01002688 tape->blk_size = 1024;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002689}
2690
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +02002691#ifdef CONFIG_IDE_PROC_FS
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002692static void idetape_add_settings(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002693{
2694 idetape_tape_t *tape = drive->driver_data;
2695
Borislav Petkovb6422012008-02-02 19:56:49 +01002696 ide_add_setting(drive, "buffer", SETTING_READ, TYPE_SHORT, 0, 0xffff,
2697 1, 2, (u16 *)&tape->caps[16], NULL);
Borislav Petkovb6422012008-02-02 19:56:49 +01002698 ide_add_setting(drive, "speed", SETTING_READ, TYPE_SHORT, 0, 0xffff,
2699 1, 1, (u16 *)&tape->caps[14], NULL);
Borislav Petkov54bb2072008-02-06 02:57:52 +01002700 ide_add_setting(drive, "stage", SETTING_READ, TYPE_INT, 0, 0xffff, 1,
2701 1024, &tape->stage_size, NULL);
2702 ide_add_setting(drive, "tdsc", SETTING_RW, TYPE_INT, IDETAPE_DSC_RW_MIN,
2703 IDETAPE_DSC_RW_MAX, 1000, HZ, &tape->best_dsc_rw_freq,
2704 NULL);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002705 ide_add_setting(drive, "dsc_overlap", SETTING_RW, TYPE_BYTE, 0, 1, 1,
2706 1, &drive->dsc_overlap, NULL);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002707 ide_add_setting(drive, "avg_speed", SETTING_READ, TYPE_INT, 0, 0xffff,
2708 1, 1, &tape->avg_speed, NULL);
Borislav Petkov8004a8c2008-02-06 02:57:51 +01002709 ide_add_setting(drive, "debug_mask", SETTING_RW, TYPE_INT, 0, 0xffff, 1,
2710 1, &tape->debug_mask, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002711}
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +02002712#else
2713static inline void idetape_add_settings(ide_drive_t *drive) { ; }
2714#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002715
2716/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +01002717 * The function below is called to:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002718 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +01002719 * 1. Initialize our various state variables.
2720 * 2. Ask the tape for its capabilities.
2721 * 3. Allocate a buffer which will be used for data transfer. The buffer size
2722 * is chosen based on the recommendation which we received in step 2.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002723 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +01002724 * Note that at this point ide.c already assigned us an irq, so that we can
2725 * queue requests here and wait for their completion.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002726 */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002727static void idetape_setup(ide_drive_t *drive, idetape_tape_t *tape, int minor)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002728{
Borislav Petkov83042b22008-04-27 15:38:27 +02002729 unsigned long t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002730 int speed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002731 int stage_size;
Borislav Petkov71071b82008-02-06 02:57:53 +01002732 u8 gcw[2];
Borislav Petkovb6422012008-02-02 19:56:49 +01002733 u16 *ctl = (u16 *)&tape->caps[12];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002734
Borislav Petkov54bb2072008-02-06 02:57:52 +01002735 spin_lock_init(&tape->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002736 drive->dsc_overlap = 1;
Bartlomiej Zolnierkiewicz4166c192008-02-01 23:09:30 +01002737 if (drive->hwif->host_flags & IDE_HFLAG_NO_DSC) {
2738 printk(KERN_INFO "ide-tape: %s: disabling DSC overlap\n",
2739 tape->name);
2740 drive->dsc_overlap = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002741 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002742 /* Seagate Travan drives do not support DSC overlap. */
2743 if (strstr(drive->id->model, "Seagate STT3401"))
2744 drive->dsc_overlap = 0;
2745 tape->minor = minor;
2746 tape->name[0] = 'h';
2747 tape->name[1] = 't';
2748 tape->name[2] = '0' + minor;
Borislav Petkov54abf372008-02-06 02:57:52 +01002749 tape->chrdev_dir = IDETAPE_DIR_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002750 tape->pc = tape->pc_stack;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002751 *((unsigned short *) &gcw) = drive->id->config;
Borislav Petkov71071b82008-02-06 02:57:53 +01002752
2753 /* Command packet DRQ type */
2754 if (((gcw[0] & 0x60) >> 5) == 1)
Borislav Petkov346331f2008-04-18 00:46:26 +02002755 set_bit(IDETAPE_FLAG_DRQ_INTERRUPT, &tape->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002756
Linus Torvalds1da177e2005-04-16 15:20:36 -07002757 idetape_get_inquiry_results(drive);
2758 idetape_get_mode_sense_results(drive);
Borislav Petkov3cffb9c2008-02-02 19:56:50 +01002759 ide_tape_get_bsize_from_bdesc(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002760 tape->user_bs_factor = 1;
Borislav Petkov54bb2072008-02-06 02:57:52 +01002761 tape->stage_size = *ctl * tape->blk_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002762 while (tape->stage_size > 0xffff) {
2763 printk(KERN_NOTICE "ide-tape: decreasing stage size\n");
Borislav Petkovb6422012008-02-02 19:56:49 +01002764 *ctl /= 2;
Borislav Petkov54bb2072008-02-06 02:57:52 +01002765 tape->stage_size = *ctl * tape->blk_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002766 }
2767 stage_size = tape->stage_size;
2768 tape->pages_per_stage = stage_size / PAGE_SIZE;
2769 if (stage_size % PAGE_SIZE) {
2770 tape->pages_per_stage++;
2771 tape->excess_bh_size = PAGE_SIZE - stage_size % PAGE_SIZE;
2772 }
2773
Borislav Petkov83042b22008-04-27 15:38:27 +02002774 /* select the "best" DSC read/write polling freq */
Borislav Petkovb6422012008-02-02 19:56:49 +01002775 speed = max(*(u16 *)&tape->caps[14], *(u16 *)&tape->caps[8]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002776
Borislav Petkov83042b22008-04-27 15:38:27 +02002777 t = (IDETAPE_FIFO_THRESHOLD * tape->stage_size * HZ) / (speed * 1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002778
2779 /*
Borislav Petkov3c98bf32008-02-06 02:57:53 +01002780 * Ensure that the number we got makes sense; limit it within
2781 * IDETAPE_DSC_RW_MIN and IDETAPE_DSC_RW_MAX.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002782 */
Borislav Petkov54bb2072008-02-06 02:57:52 +01002783 tape->best_dsc_rw_freq = max_t(unsigned long,
2784 min_t(unsigned long, t, IDETAPE_DSC_RW_MAX),
2785 IDETAPE_DSC_RW_MIN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002786 printk(KERN_INFO "ide-tape: %s <-> %s: %dKBps, %d*%dkB buffer, "
Borislav Petkov83042b22008-04-27 15:38:27 +02002787 "%lums tDSC%s\n",
Borislav Petkovb6422012008-02-02 19:56:49 +01002788 drive->name, tape->name, *(u16 *)&tape->caps[14],
2789 (*(u16 *)&tape->caps[16] * 512) / tape->stage_size,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002790 tape->stage_size / 1024,
Borislav Petkov54bb2072008-02-06 02:57:52 +01002791 tape->best_dsc_rw_freq * 1000 / HZ,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002792 drive->using_dma ? ", DMA":"");
2793
2794 idetape_add_settings(drive);
2795}
2796
Russell King4031bbe2006-01-06 11:41:00 +00002797static void ide_tape_remove(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002798{
2799 idetape_tape_t *tape = drive->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002800
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +02002801 ide_proc_unregister_driver(drive, tape->driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002802
2803 ide_unregister_region(tape->disk);
2804
2805 ide_tape_put(tape);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002806}
2807
2808static void ide_tape_release(struct kref *kref)
2809{
2810 struct ide_tape_obj *tape = to_ide_tape(kref);
2811 ide_drive_t *drive = tape->drive;
2812 struct gendisk *g = tape->disk;
2813
Borislav Petkov83042b22008-04-27 15:38:27 +02002814 BUG_ON(tape->merge_stage_size);
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02002815
Linus Torvalds1da177e2005-04-16 15:20:36 -07002816 drive->dsc_overlap = 0;
2817 drive->driver_data = NULL;
Tony Jonesdbc12722007-09-25 02:03:03 +02002818 device_destroy(idetape_sysfs_class, MKDEV(IDETAPE_MAJOR, tape->minor));
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002819 device_destroy(idetape_sysfs_class,
2820 MKDEV(IDETAPE_MAJOR, tape->minor + 128));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002821 idetape_devs[tape->minor] = NULL;
2822 g->private_data = NULL;
2823 put_disk(g);
2824 kfree(tape);
2825}
2826
Bartlomiej Zolnierkiewiczecfd80e2007-05-10 00:01:09 +02002827#ifdef CONFIG_IDE_PROC_FS
Linus Torvalds1da177e2005-04-16 15:20:36 -07002828static int proc_idetape_read_name
2829 (char *page, char **start, off_t off, int count, int *eof, void *data)
2830{
2831 ide_drive_t *drive = (ide_drive_t *) data;
2832 idetape_tape_t *tape = drive->driver_data;
2833 char *out = page;
2834 int len;
2835
2836 len = sprintf(out, "%s\n", tape->name);
2837 PROC_IDE_READ_RETURN(page, start, off, count, eof, len);
2838}
2839
2840static ide_proc_entry_t idetape_proc[] = {
2841 { "capacity", S_IFREG|S_IRUGO, proc_ide_read_capacity, NULL },
2842 { "name", S_IFREG|S_IRUGO, proc_idetape_read_name, NULL },
2843 { NULL, 0, NULL, NULL }
2844};
Linus Torvalds1da177e2005-04-16 15:20:36 -07002845#endif
2846
Russell King4031bbe2006-01-06 11:41:00 +00002847static int ide_tape_probe(ide_drive_t *);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002848
Linus Torvalds1da177e2005-04-16 15:20:36 -07002849static ide_driver_t idetape_driver = {
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02002850 .gen_driver = {
Laurent Riffard4ef3b8f2005-11-18 22:15:40 +01002851 .owner = THIS_MODULE,
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02002852 .name = "ide-tape",
2853 .bus = &ide_bus_type,
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02002854 },
Russell King4031bbe2006-01-06 11:41:00 +00002855 .probe = ide_tape_probe,
2856 .remove = ide_tape_remove,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002857 .version = IDETAPE_VERSION,
2858 .media = ide_tape,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002859 .supports_dsc_overlap = 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002860 .do_request = idetape_do_request,
2861 .end_request = idetape_end_request,
2862 .error = __ide_error,
2863 .abort = __ide_abort,
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +02002864#ifdef CONFIG_IDE_PROC_FS
Linus Torvalds1da177e2005-04-16 15:20:36 -07002865 .proc = idetape_proc,
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +02002866#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002867};
2868
Borislav Petkov3c98bf32008-02-06 02:57:53 +01002869/* Our character device supporting functions, passed to register_chrdev. */
Arjan van de Ven2b8693c2007-02-12 00:55:32 -08002870static const struct file_operations idetape_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002871 .owner = THIS_MODULE,
2872 .read = idetape_chrdev_read,
2873 .write = idetape_chrdev_write,
2874 .ioctl = idetape_chrdev_ioctl,
2875 .open = idetape_chrdev_open,
2876 .release = idetape_chrdev_release,
2877};
2878
2879static int idetape_open(struct inode *inode, struct file *filp)
2880{
2881 struct gendisk *disk = inode->i_bdev->bd_disk;
2882 struct ide_tape_obj *tape;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002883
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002884 tape = ide_tape_get(disk);
2885 if (!tape)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002886 return -ENXIO;
2887
Linus Torvalds1da177e2005-04-16 15:20:36 -07002888 return 0;
2889}
2890
2891static int idetape_release(struct inode *inode, struct file *filp)
2892{
2893 struct gendisk *disk = inode->i_bdev->bd_disk;
2894 struct ide_tape_obj *tape = ide_tape_g(disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002895
2896 ide_tape_put(tape);
2897
2898 return 0;
2899}
2900
2901static int idetape_ioctl(struct inode *inode, struct file *file,
2902 unsigned int cmd, unsigned long arg)
2903{
2904 struct block_device *bdev = inode->i_bdev;
2905 struct ide_tape_obj *tape = ide_tape_g(bdev->bd_disk);
2906 ide_drive_t *drive = tape->drive;
2907 int err = generic_ide_ioctl(drive, file, bdev, cmd, arg);
2908 if (err == -EINVAL)
2909 err = idetape_blkdev_ioctl(drive, cmd, arg);
2910 return err;
2911}
2912
2913static struct block_device_operations idetape_block_ops = {
2914 .owner = THIS_MODULE,
2915 .open = idetape_open,
2916 .release = idetape_release,
2917 .ioctl = idetape_ioctl,
2918};
2919
Russell King4031bbe2006-01-06 11:41:00 +00002920static int ide_tape_probe(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002921{
2922 idetape_tape_t *tape;
2923 struct gendisk *g;
2924 int minor;
2925
2926 if (!strstr("ide-tape", drive->driver_req))
2927 goto failed;
2928 if (!drive->present)
2929 goto failed;
2930 if (drive->media != ide_tape)
2931 goto failed;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002932 if (!idetape_identify_device(drive)) {
2933 printk(KERN_ERR "ide-tape: %s: not supported by this version of"
2934 " the driver\n", drive->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002935 goto failed;
2936 }
2937 if (drive->scsi) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002938 printk(KERN_INFO "ide-tape: passing drive %s to ide-scsi"
2939 " emulation.\n", drive->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002940 goto failed;
2941 }
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002942 tape = kzalloc(sizeof(idetape_tape_t), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002943 if (tape == NULL) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002944 printk(KERN_ERR "ide-tape: %s: Can't allocate a tape struct\n",
2945 drive->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002946 goto failed;
2947 }
2948
2949 g = alloc_disk(1 << PARTN_BITS);
2950 if (!g)
2951 goto out_free_tape;
2952
2953 ide_init_disk(g, drive);
2954
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +02002955 ide_proc_register_driver(drive, &idetape_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002956
Linus Torvalds1da177e2005-04-16 15:20:36 -07002957 kref_init(&tape->kref);
2958
2959 tape->drive = drive;
2960 tape->driver = &idetape_driver;
2961 tape->disk = g;
2962
2963 g->private_data = &tape->driver;
2964
2965 drive->driver_data = tape;
2966
Arjan van de Vencf8b8972006-03-23 03:00:45 -08002967 mutex_lock(&idetape_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002968 for (minor = 0; idetape_devs[minor]; minor++)
2969 ;
2970 idetape_devs[minor] = tape;
Arjan van de Vencf8b8972006-03-23 03:00:45 -08002971 mutex_unlock(&idetape_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002972
2973 idetape_setup(drive, tape, minor);
2974
Tony Jonesdbc12722007-09-25 02:03:03 +02002975 device_create(idetape_sysfs_class, &drive->gendev,
2976 MKDEV(IDETAPE_MAJOR, minor), "%s", tape->name);
2977 device_create(idetape_sysfs_class, &drive->gendev,
2978 MKDEV(IDETAPE_MAJOR, minor + 128), "n%s", tape->name);
Will Dysond5dee802005-09-16 02:55:07 -07002979
Linus Torvalds1da177e2005-04-16 15:20:36 -07002980 g->fops = &idetape_block_ops;
2981 ide_register_region(g);
2982
2983 return 0;
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02002984
Linus Torvalds1da177e2005-04-16 15:20:36 -07002985out_free_tape:
2986 kfree(tape);
2987failed:
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02002988 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002989}
2990
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002991static void __exit idetape_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002992{
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02002993 driver_unregister(&idetape_driver.gen_driver);
Will Dysond5dee802005-09-16 02:55:07 -07002994 class_destroy(idetape_sysfs_class);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002995 unregister_chrdev(IDETAPE_MAJOR, "ht");
2996}
2997
Bartlomiej Zolnierkiewicz17514e82005-11-19 22:24:35 +01002998static int __init idetape_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002999{
Will Dysond5dee802005-09-16 02:55:07 -07003000 int error = 1;
3001 idetape_sysfs_class = class_create(THIS_MODULE, "ide_tape");
3002 if (IS_ERR(idetape_sysfs_class)) {
3003 idetape_sysfs_class = NULL;
3004 printk(KERN_ERR "Unable to create sysfs class for ide tapes\n");
3005 error = -EBUSY;
3006 goto out;
3007 }
3008
Linus Torvalds1da177e2005-04-16 15:20:36 -07003009 if (register_chrdev(IDETAPE_MAJOR, "ht", &idetape_fops)) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01003010 printk(KERN_ERR "ide-tape: Failed to register chrdev"
3011 " interface\n");
Will Dysond5dee802005-09-16 02:55:07 -07003012 error = -EBUSY;
3013 goto out_free_class;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003014 }
Will Dysond5dee802005-09-16 02:55:07 -07003015
3016 error = driver_register(&idetape_driver.gen_driver);
3017 if (error)
3018 goto out_free_driver;
3019
3020 return 0;
3021
3022out_free_driver:
3023 driver_unregister(&idetape_driver.gen_driver);
3024out_free_class:
3025 class_destroy(idetape_sysfs_class);
3026out:
3027 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003028}
3029
Kay Sievers263756e2005-12-12 18:03:44 +01003030MODULE_ALIAS("ide:*m-tape*");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003031module_init(idetape_init);
3032module_exit(idetape_exit);
3033MODULE_ALIAS_CHARDEV_MAJOR(IDETAPE_MAJOR);
Borislav Petkov9c145762008-02-06 02:57:54 +01003034MODULE_DESCRIPTION("ATAPI Streaming TAPE Driver");
3035MODULE_LICENSE("GPL");