blob: c4b470999203742a9d00e4e236758557388f0b7f [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.
327 * In the pipelined operation mode, we use our internal pipeline
328 * structure to hold more data requests. The data buffer size is chosen
329 * based on the tape's recommendation.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 */
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100331 /* ptr to the request which is waiting in the device request queue */
Borislav Petkov54bb2072008-02-06 02:57:52 +0100332 struct request *active_data_rq;
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100333 /* Data buffer size chosen based on the tape's recommendation */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 int stage_size;
335 idetape_stage_t *merge_stage;
336 int merge_stage_size;
337 struct idetape_bh *bh;
338 char *b_data;
339 int b_count;
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100340
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 /*
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100342 * Pipeline parameters.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100344 * To accomplish non-pipelined mode, we simply set the following
345 * variables to zero (or NULL, where appropriate).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 */
347 /* Number of currently used stages */
348 int nr_stages;
349 /* Number of pending stages */
350 int nr_pending_stages;
351 /* We will not allocate more than this number of stages */
352 int max_stages, min_pipeline, max_pipeline;
353 /* The first stage which will be removed from the pipeline */
354 idetape_stage_t *first_stage;
355 /* The currently active stage */
356 idetape_stage_t *active_stage;
357 /* Will be serviced after the currently active request */
358 idetape_stage_t *next_stage;
359 /* New requests will be added to the pipeline here */
360 idetape_stage_t *last_stage;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 int pages_per_stage;
362 /* Wasted space in each stage */
363 int excess_bh_size;
364
365 /* Status/Action flags: long for set_bit */
366 unsigned long flags;
367 /* protects the ide-tape queue */
Borislav Petkov54bb2072008-02-06 02:57:52 +0100368 spinlock_t lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100370 /* Measures average tape speed */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 unsigned long avg_time;
372 int avg_size;
373 int avg_speed;
374
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 /* the door is currently locked */
376 int door_locked;
377 /* the tape hardware is write protected */
378 char drv_write_prot;
379 /* the tape is write protected (hardware or opened as read-only) */
380 char write_prot;
381
382 /*
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100383 * Limit the number of times a request can be postponed, to avoid an
384 * infinite postpone deadlock.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 int postpone_cnt;
387
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100388 /* Speed control at the tape buffers input/output */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 unsigned long insert_time;
390 int insert_size;
391 int insert_speed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 int measure_insert_time;
393
Borislav Petkov8004a8c2008-02-06 02:57:51 +0100394 u32 debug_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395} idetape_tape_t;
396
Arjan van de Vencf8b8972006-03-23 03:00:45 -0800397static DEFINE_MUTEX(idetape_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398
Will Dysond5dee802005-09-16 02:55:07 -0700399static struct class *idetape_sysfs_class;
400
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401#define to_ide_tape(obj) container_of(obj, struct ide_tape_obj, kref)
402
403#define ide_tape_g(disk) \
404 container_of((disk)->private_data, struct ide_tape_obj, driver)
405
406static struct ide_tape_obj *ide_tape_get(struct gendisk *disk)
407{
408 struct ide_tape_obj *tape = NULL;
409
Arjan van de Vencf8b8972006-03-23 03:00:45 -0800410 mutex_lock(&idetape_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 tape = ide_tape_g(disk);
412 if (tape)
413 kref_get(&tape->kref);
Arjan van de Vencf8b8972006-03-23 03:00:45 -0800414 mutex_unlock(&idetape_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 return tape;
416}
417
418static void ide_tape_release(struct kref *);
419
420static void ide_tape_put(struct ide_tape_obj *tape)
421{
Arjan van de Vencf8b8972006-03-23 03:00:45 -0800422 mutex_lock(&idetape_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 kref_put(&tape->kref, ide_tape_release);
Arjan van de Vencf8b8972006-03-23 03:00:45 -0800424 mutex_unlock(&idetape_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425}
426
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100428 * The variables below are used for the character device interface. Additional
429 * state variables are defined in our ide_drive_t structure.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100431static struct ide_tape_obj *idetape_devs[MAX_HWIFS * MAX_DRIVES];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432
433#define ide_tape_f(file) ((file)->private_data)
434
435static struct ide_tape_obj *ide_tape_chrdev_get(unsigned int i)
436{
437 struct ide_tape_obj *tape = NULL;
438
Arjan van de Vencf8b8972006-03-23 03:00:45 -0800439 mutex_lock(&idetape_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 tape = idetape_devs[i];
441 if (tape)
442 kref_get(&tape->kref);
Arjan van de Vencf8b8972006-03-23 03:00:45 -0800443 mutex_unlock(&idetape_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 return tape;
445}
446
Borislav Petkovd236d742008-04-18 00:46:27 +0200447static void idetape_input_buffers(ide_drive_t *drive, struct ide_atapi_pc *pc,
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100448 unsigned int bcount)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449{
450 struct idetape_bh *bh = pc->bh;
451 int count;
452
453 while (bcount) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 if (bh == NULL) {
455 printk(KERN_ERR "ide-tape: bh == NULL in "
456 "idetape_input_buffers\n");
Bartlomiej Zolnierkiewicz7616c0a2008-04-18 00:46:26 +0200457 ide_atapi_discard_data(drive, bcount);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 return;
459 }
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100460 count = min(
461 (unsigned int)(bh->b_size - atomic_read(&bh->b_count)),
462 bcount);
463 HWIF(drive)->atapi_input_bytes(drive, bh->b_data +
464 atomic_read(&bh->b_count), count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 bcount -= count;
466 atomic_add(count, &bh->b_count);
467 if (atomic_read(&bh->b_count) == bh->b_size) {
468 bh = bh->b_reqnext;
469 if (bh)
470 atomic_set(&bh->b_count, 0);
471 }
472 }
473 pc->bh = bh;
474}
475
Borislav Petkovd236d742008-04-18 00:46:27 +0200476static void idetape_output_buffers(ide_drive_t *drive, struct ide_atapi_pc *pc,
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100477 unsigned int bcount)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478{
479 struct idetape_bh *bh = pc->bh;
480 int count;
481
482 while (bcount) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 if (bh == NULL) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100484 printk(KERN_ERR "ide-tape: bh == NULL in %s\n",
485 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 return;
487 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 count = min((unsigned int)pc->b_count, (unsigned int)bcount);
489 HWIF(drive)->atapi_output_bytes(drive, pc->b_data, count);
490 bcount -= count;
491 pc->b_data += count;
492 pc->b_count -= count;
493 if (!pc->b_count) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100494 bh = bh->b_reqnext;
495 pc->bh = bh;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 if (bh) {
497 pc->b_data = bh->b_data;
498 pc->b_count = atomic_read(&bh->b_count);
499 }
500 }
501 }
502}
503
Borislav Petkovd236d742008-04-18 00:46:27 +0200504static void idetape_update_buffers(struct ide_atapi_pc *pc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505{
506 struct idetape_bh *bh = pc->bh;
507 int count;
Borislav Petkovd236d742008-04-18 00:46:27 +0200508 unsigned int bcount = pc->xferred;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509
Borislav Petkov346331f2008-04-18 00:46:26 +0200510 if (pc->flags & PC_FLAG_WRITING)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 return;
512 while (bcount) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 if (bh == NULL) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100514 printk(KERN_ERR "ide-tape: bh == NULL in %s\n",
515 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 return;
517 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 count = min((unsigned int)bh->b_size, (unsigned int)bcount);
519 atomic_set(&bh->b_count, count);
520 if (atomic_read(&bh->b_count) == bh->b_size)
521 bh = bh->b_reqnext;
522 bcount -= count;
523 }
524 pc->bh = bh;
525}
526
527/*
528 * idetape_next_pc_storage returns a pointer to a place in which we can
529 * safely store a packet command, even though we intend to leave the
530 * driver. A storage space for a maximum of IDETAPE_PC_STACK packet
531 * commands is allocated at initialization time.
532 */
Borislav Petkovd236d742008-04-18 00:46:27 +0200533static struct ide_atapi_pc *idetape_next_pc_storage(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534{
535 idetape_tape_t *tape = drive->driver_data;
536
Borislav Petkov8004a8c2008-02-06 02:57:51 +0100537 debug_log(DBG_PCRQ_STACK, "pc_stack_index=%d\n", tape->pc_stack_index);
538
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 if (tape->pc_stack_index == IDETAPE_PC_STACK)
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100540 tape->pc_stack_index = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 return (&tape->pc_stack[tape->pc_stack_index++]);
542}
543
544/*
545 * idetape_next_rq_storage is used along with idetape_next_pc_storage.
546 * Since we queue packet commands in the request queue, we need to
547 * allocate a request, along with the allocation of a packet command.
548 */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100549
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550/**************************************************************
551 * *
552 * This should get fixed to use kmalloc(.., GFP_ATOMIC) *
553 * followed later on by kfree(). -ml *
554 * *
555 **************************************************************/
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100556
557static struct request *idetape_next_rq_storage(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558{
559 idetape_tape_t *tape = drive->driver_data;
560
Borislav Petkov8004a8c2008-02-06 02:57:51 +0100561 debug_log(DBG_PCRQ_STACK, "rq_stack_index=%d\n", tape->rq_stack_index);
562
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 if (tape->rq_stack_index == IDETAPE_PC_STACK)
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100564 tape->rq_stack_index = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 return (&tape->rq_stack[tape->rq_stack_index++]);
566}
567
Borislav Petkovd236d742008-04-18 00:46:27 +0200568static void idetape_init_pc(struct ide_atapi_pc *pc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569{
570 memset(pc->c, 0, 12);
571 pc->retries = 0;
572 pc->flags = 0;
Borislav Petkovd236d742008-04-18 00:46:27 +0200573 pc->req_xfer = 0;
574 pc->buf = pc->pc_buf;
575 pc->buf_size = IDETAPE_PC_BUFFER_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 pc->bh = NULL;
577 pc->b_data = NULL;
578}
579
580/*
Borislav Petkov1b5db432008-02-02 19:56:48 +0100581 * called on each failed packet command retry to analyze the request sense. We
582 * currently do not utilize this information.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 */
Borislav Petkov1b5db432008-02-02 19:56:48 +0100584static void idetape_analyze_error(ide_drive_t *drive, u8 *sense)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585{
586 idetape_tape_t *tape = drive->driver_data;
Borislav Petkovd236d742008-04-18 00:46:27 +0200587 struct ide_atapi_pc *pc = tape->failed_pc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588
Borislav Petkov1b5db432008-02-02 19:56:48 +0100589 tape->sense_key = sense[2] & 0xF;
590 tape->asc = sense[12];
591 tape->ascq = sense[13];
Borislav Petkov8004a8c2008-02-06 02:57:51 +0100592
593 debug_log(DBG_ERR, "pc = %x, sense key = %x, asc = %x, ascq = %x\n",
594 pc->c[0], tape->sense_key, tape->asc, tape->ascq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595
Borislav Petkovd236d742008-04-18 00:46:27 +0200596 /* Correct pc->xferred by asking the tape. */
Borislav Petkov346331f2008-04-18 00:46:26 +0200597 if (pc->flags & PC_FLAG_DMA_ERROR) {
Borislav Petkovd236d742008-04-18 00:46:27 +0200598 pc->xferred = pc->req_xfer -
Borislav Petkov54bb2072008-02-06 02:57:52 +0100599 tape->blk_size *
Borislav Petkov860ff5e2008-02-02 19:56:50 +0100600 be32_to_cpu(get_unaligned((u32 *)&sense[3]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 idetape_update_buffers(pc);
602 }
603
604 /*
605 * If error was the result of a zero-length read or write command,
606 * with sense key=5, asc=0x22, ascq=0, let it slide. Some drives
607 * (i.e. Seagate STT3401A Travan) don't support 0-length read/writes.
608 */
Borislav Petkov90699ce2008-02-02 19:56:50 +0100609 if ((pc->c[0] == READ_6 || pc->c[0] == WRITE_6)
Borislav Petkov1b5db432008-02-02 19:56:48 +0100610 /* length == 0 */
611 && pc->c[4] == 0 && pc->c[3] == 0 && pc->c[2] == 0) {
612 if (tape->sense_key == 5) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 /* don't report an error, everything's ok */
614 pc->error = 0;
615 /* don't retry read/write */
Borislav Petkov346331f2008-04-18 00:46:26 +0200616 pc->flags |= PC_FLAG_ABORT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 }
618 }
Borislav Petkov90699ce2008-02-02 19:56:50 +0100619 if (pc->c[0] == READ_6 && (sense[2] & 0x80)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620 pc->error = IDETAPE_ERROR_FILEMARK;
Borislav Petkov346331f2008-04-18 00:46:26 +0200621 pc->flags |= PC_FLAG_ABORT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 }
Borislav Petkov90699ce2008-02-02 19:56:50 +0100623 if (pc->c[0] == WRITE_6) {
Borislav Petkov1b5db432008-02-02 19:56:48 +0100624 if ((sense[2] & 0x40) || (tape->sense_key == 0xd
625 && tape->asc == 0x0 && tape->ascq == 0x2)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626 pc->error = IDETAPE_ERROR_EOD;
Borislav Petkov346331f2008-04-18 00:46:26 +0200627 pc->flags |= PC_FLAG_ABORT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628 }
629 }
Borislav Petkov90699ce2008-02-02 19:56:50 +0100630 if (pc->c[0] == READ_6 || pc->c[0] == WRITE_6) {
Borislav Petkov1b5db432008-02-02 19:56:48 +0100631 if (tape->sense_key == 8) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 pc->error = IDETAPE_ERROR_EOD;
Borislav Petkov346331f2008-04-18 00:46:26 +0200633 pc->flags |= PC_FLAG_ABORT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634 }
Borislav Petkov346331f2008-04-18 00:46:26 +0200635 if (!(pc->flags & PC_FLAG_ABORT) &&
Borislav Petkovd236d742008-04-18 00:46:27 +0200636 pc->xferred)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637 pc->retries = IDETAPE_MAX_PC_RETRIES + 1;
638 }
639}
640
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100641/* Free a stage along with its related buffers completely. */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100642static void __idetape_kfree_stage(idetape_stage_t *stage)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643{
644 struct idetape_bh *prev_bh, *bh = stage->bh;
645 int size;
646
647 while (bh != NULL) {
648 if (bh->b_data != NULL) {
649 size = (int) bh->b_size;
650 while (size > 0) {
651 free_page((unsigned long) bh->b_data);
652 size -= PAGE_SIZE;
653 bh->b_data += PAGE_SIZE;
654 }
655 }
656 prev_bh = bh;
657 bh = bh->b_reqnext;
658 kfree(prev_bh);
659 }
660 kfree(stage);
661}
662
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100664 * Finish servicing a request and insert a pending pipeline request into the
665 * main device queue.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666 */
667static int idetape_end_request(ide_drive_t *drive, int uptodate, int nr_sects)
668{
669 struct request *rq = HWGROUP(drive)->rq;
670 idetape_tape_t *tape = drive->driver_data;
671 unsigned long flags;
672 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673
Borislav Petkov8004a8c2008-02-06 02:57:51 +0100674 debug_log(DBG_PROCS, "Enter %s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675
676 switch (uptodate) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100677 case 0: error = IDETAPE_ERROR_GENERAL; break;
678 case 1: error = 0; break;
679 default: error = uptodate;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 }
681 rq->errors = error;
682 if (error)
683 tape->failed_pc = NULL;
684
Bartlomiej Zolnierkiewicz36872212008-01-26 20:13:10 +0100685 if (!blk_special_request(rq)) {
686 ide_end_request(drive, uptodate, nr_sects);
687 return 0;
688 }
689
Borislav Petkov54bb2072008-02-06 02:57:52 +0100690 spin_lock_irqsave(&tape->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692 ide_end_drive_cmd(drive, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693
Borislav Petkov54bb2072008-02-06 02:57:52 +0100694 spin_unlock_irqrestore(&tape->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 return 0;
696}
697
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100698static ide_startstop_t idetape_request_sense_callback(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699{
700 idetape_tape_t *tape = drive->driver_data;
701
Borislav Petkov8004a8c2008-02-06 02:57:51 +0100702 debug_log(DBG_PROCS, "Enter %s\n", __func__);
703
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 if (!tape->pc->error) {
Borislav Petkovd236d742008-04-18 00:46:27 +0200705 idetape_analyze_error(drive, tape->pc->buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 idetape_end_request(drive, 1, 0);
707 } else {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100708 printk(KERN_ERR "ide-tape: Error in REQUEST SENSE itself - "
709 "Aborting request!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710 idetape_end_request(drive, 0, 0);
711 }
712 return ide_stopped;
713}
714
Borislav Petkovd236d742008-04-18 00:46:27 +0200715static void idetape_create_request_sense_cmd(struct ide_atapi_pc *pc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716{
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100717 idetape_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +0100718 pc->c[0] = REQUEST_SENSE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719 pc->c[4] = 20;
Borislav Petkovd236d742008-04-18 00:46:27 +0200720 pc->req_xfer = 20;
721 pc->idetape_callback = &idetape_request_sense_callback;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722}
723
724static void idetape_init_rq(struct request *rq, u8 cmd)
725{
726 memset(rq, 0, sizeof(*rq));
Jens Axboe4aff5e22006-08-10 08:44:47 +0200727 rq->cmd_type = REQ_TYPE_SPECIAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 rq->cmd[0] = cmd;
729}
730
731/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100732 * Generate a new packet command request in front of the request queue, before
733 * the current request, so that it will be processed immediately, on the next
734 * pass through the driver. The function below is called from the request
735 * handling part of the driver (the "bottom" part). Safe storage for the request
736 * should be allocated with ide_tape_next_{pc,rq}_storage() prior to that.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100738 * Memory for those requests is pre-allocated at initialization time, and is
739 * limited to IDETAPE_PC_STACK requests. We assume that we have enough space for
740 * the maximum possible number of inter-dependent packet commands.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100742 * The higher level of the driver - The ioctl handler and the character device
743 * handling functions should queue request to the lower level part and wait for
744 * their completion using idetape_queue_pc_tail or idetape_queue_rw_tail.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745 */
Borislav Petkovd236d742008-04-18 00:46:27 +0200746static void idetape_queue_pc_head(ide_drive_t *drive, struct ide_atapi_pc *pc,
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100747 struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748{
749 struct ide_tape_obj *tape = drive->driver_data;
750
751 idetape_init_rq(rq, REQ_IDETAPE_PC1);
752 rq->buffer = (char *) pc;
753 rq->rq_disk = tape->disk;
754 (void) ide_do_drive_cmd(drive, rq, ide_preempt);
755}
756
757/*
758 * idetape_retry_pc is called when an error was detected during the
759 * last packet command. We queue a request sense packet command in
760 * the head of the request list.
761 */
762static ide_startstop_t idetape_retry_pc (ide_drive_t *drive)
763{
764 idetape_tape_t *tape = drive->driver_data;
Borislav Petkovd236d742008-04-18 00:46:27 +0200765 struct ide_atapi_pc *pc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766 struct request *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767
Bartlomiej Zolnierkiewicz64a57fe2008-02-06 02:57:51 +0100768 (void)ide_read_error(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769 pc = idetape_next_pc_storage(drive);
770 rq = idetape_next_rq_storage(drive);
771 idetape_create_request_sense_cmd(pc);
Borislav Petkov346331f2008-04-18 00:46:26 +0200772 set_bit(IDETAPE_FLAG_IGNORE_DSC, &tape->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773 idetape_queue_pc_head(drive, pc, rq);
774 return ide_stopped;
775}
776
777/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100778 * Postpone the current request so that ide.c will be able to service requests
779 * from another device on the same hwgroup while we are polling for DSC.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780 */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100781static void idetape_postpone_request(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782{
783 idetape_tape_t *tape = drive->driver_data;
784
Borislav Petkov8004a8c2008-02-06 02:57:51 +0100785 debug_log(DBG_PROCS, "Enter %s\n", __func__);
786
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787 tape->postponed_rq = HWGROUP(drive)->rq;
Borislav Petkov54bb2072008-02-06 02:57:52 +0100788 ide_stall_queue(drive, tape->dsc_poll_freq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789}
790
Borislav Petkovd236d742008-04-18 00:46:27 +0200791typedef void idetape_io_buf(ide_drive_t *, struct ide_atapi_pc *, unsigned int);
Borislav Petkova1efc852008-02-06 02:57:52 +0100792
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793/*
Borislav Petkova1efc852008-02-06 02:57:52 +0100794 * This is the usual interrupt handler which will be called during a packet
795 * command. We will transfer some of the data (as requested by the drive) and
796 * will re-point interrupt handler to us. When data transfer is finished, we
797 * will act according to the algorithm described before
Borislav Petkov8d06bfa2008-02-06 02:57:53 +0100798 * idetape_issue_pc.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799 */
Borislav Petkova1efc852008-02-06 02:57:52 +0100800static ide_startstop_t idetape_pc_intr(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801{
802 ide_hwif_t *hwif = drive->hwif;
803 idetape_tape_t *tape = drive->driver_data;
Borislav Petkovd236d742008-04-18 00:46:27 +0200804 struct ide_atapi_pc *pc = tape->pc;
Borislav Petkova1efc852008-02-06 02:57:52 +0100805 xfer_func_t *xferfunc;
806 idetape_io_buf *iobuf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807 unsigned int temp;
808#if SIMULATE_ERRORS
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100809 static int error_sim_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810#endif
Bartlomiej Zolnierkiewicz790d1232008-01-25 22:17:12 +0100811 u16 bcount;
Bartlomiej Zolnierkiewicz8e7657a2008-01-25 22:17:12 +0100812 u8 stat, ireason;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813
Borislav Petkov8004a8c2008-02-06 02:57:51 +0100814 debug_log(DBG_PROCS, "Enter %s - interrupt handler\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815
816 /* Clear the interrupt */
Bartlomiej Zolnierkiewiczc47137a2008-02-06 02:57:51 +0100817 stat = ide_read_status(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818
Borislav Petkov346331f2008-04-18 00:46:26 +0200819 if (pc->flags & PC_FLAG_DMA_IN_PROGRESS) {
Bartlomiej Zolnierkiewicz5e37bdc2008-04-26 22:25:24 +0200820 if (hwif->dma_ops->dma_end(drive) || (stat & ERR_STAT)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821 /*
822 * A DMA error is sometimes expected. For example,
823 * if the tape is crossing a filemark during a
824 * READ command, it will issue an irq and position
825 * itself before the filemark, so that only a partial
826 * data transfer will occur (which causes the DMA
827 * error). In that case, we will later ask the tape
828 * how much bytes of the original request were
829 * actually transferred (we can't receive that
830 * information from the DMA engine on most chipsets).
831 */
832
833 /*
834 * On the contrary, a DMA error is never expected;
835 * it usually indicates a hardware error or abort.
836 * If the tape crosses a filemark during a READ
837 * command, it will issue an irq and position itself
838 * after the filemark (not before). Only a partial
839 * data transfer will occur, but no DMA error.
840 * (AS, 19 Apr 2001)
841 */
Borislav Petkov346331f2008-04-18 00:46:26 +0200842 pc->flags |= PC_FLAG_DMA_ERROR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843 } else {
Borislav Petkovd236d742008-04-18 00:46:27 +0200844 pc->xferred = pc->req_xfer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845 idetape_update_buffers(pc);
846 }
Borislav Petkov8004a8c2008-02-06 02:57:51 +0100847 debug_log(DBG_PROCS, "DMA finished\n");
848
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849 }
850
851 /* No more interrupts */
Bartlomiej Zolnierkiewicz22c525b2008-01-25 22:17:11 +0100852 if ((stat & DRQ_STAT) == 0) {
Borislav Petkov8004a8c2008-02-06 02:57:51 +0100853 debug_log(DBG_SENSE, "Packet command completed, %d bytes"
Borislav Petkovd236d742008-04-18 00:46:27 +0200854 " transferred\n", pc->xferred);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855
Borislav Petkov346331f2008-04-18 00:46:26 +0200856 pc->flags &= ~PC_FLAG_DMA_IN_PROGRESS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857 local_irq_enable();
858
859#if SIMULATE_ERRORS
Borislav Petkov90699ce2008-02-02 19:56:50 +0100860 if ((pc->c[0] == WRITE_6 || pc->c[0] == READ_6) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861 (++error_sim_count % 100) == 0) {
862 printk(KERN_INFO "ide-tape: %s: simulating error\n",
863 tape->name);
Bartlomiej Zolnierkiewicz22c525b2008-01-25 22:17:11 +0100864 stat |= ERR_STAT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865 }
866#endif
Borislav Petkov90699ce2008-02-02 19:56:50 +0100867 if ((stat & ERR_STAT) && pc->c[0] == REQUEST_SENSE)
Bartlomiej Zolnierkiewicz22c525b2008-01-25 22:17:11 +0100868 stat &= ~ERR_STAT;
Borislav Petkov346331f2008-04-18 00:46:26 +0200869 if ((stat & ERR_STAT) || (pc->flags & PC_FLAG_DMA_ERROR)) {
Bartlomiej Zolnierkiewicz22c525b2008-01-25 22:17:11 +0100870 /* Error detected */
Borislav Petkov8004a8c2008-02-06 02:57:51 +0100871 debug_log(DBG_ERR, "%s: I/O error\n", tape->name);
872
Borislav Petkov90699ce2008-02-02 19:56:50 +0100873 if (pc->c[0] == REQUEST_SENSE) {
Borislav Petkova1efc852008-02-06 02:57:52 +0100874 printk(KERN_ERR "ide-tape: I/O error in request"
875 " sense command\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876 return ide_do_reset(drive);
877 }
Borislav Petkov8004a8c2008-02-06 02:57:51 +0100878 debug_log(DBG_ERR, "[cmd %x]: check condition\n",
879 pc->c[0]);
880
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881 /* Retry operation */
882 return idetape_retry_pc(drive);
883 }
884 pc->error = 0;
Borislav Petkov346331f2008-04-18 00:46:26 +0200885 if ((pc->flags & PC_FLAG_WAIT_FOR_DSC) &&
Bartlomiej Zolnierkiewicz22c525b2008-01-25 22:17:11 +0100886 (stat & SEEK_STAT) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887 /* Media access command */
888 tape->dsc_polling_start = jiffies;
Borislav Petkov54bb2072008-02-06 02:57:52 +0100889 tape->dsc_poll_freq = IDETAPE_DSC_MA_FAST;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890 tape->dsc_timeout = jiffies + IDETAPE_DSC_MA_TIMEOUT;
891 /* Allow ide.c to handle other requests */
892 idetape_postpone_request(drive);
893 return ide_stopped;
894 }
895 if (tape->failed_pc == pc)
896 tape->failed_pc = NULL;
897 /* Command finished - Call the callback function */
Borislav Petkovd236d742008-04-18 00:46:27 +0200898 return pc->idetape_callback(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899 }
Borislav Petkov346331f2008-04-18 00:46:26 +0200900
901 if (pc->flags & PC_FLAG_DMA_IN_PROGRESS) {
902 pc->flags &= ~PC_FLAG_DMA_IN_PROGRESS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903 printk(KERN_ERR "ide-tape: The tape wants to issue more "
904 "interrupts in DMA mode\n");
905 printk(KERN_ERR "ide-tape: DMA disabled, reverting to PIO\n");
Bartlomiej Zolnierkiewicz7469aaf2007-02-17 02:40:26 +0100906 ide_dma_off(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907 return ide_do_reset(drive);
908 }
909 /* Get the number of bytes to transfer on this interrupt. */
Bartlomiej Zolnierkiewicz23579a22008-04-18 00:46:26 +0200910 bcount = (hwif->INB(hwif->io_ports[IDE_BCOUNTH_OFFSET]) << 8) |
911 hwif->INB(hwif->io_ports[IDE_BCOUNTL_OFFSET]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912
Bartlomiej Zolnierkiewicz23579a22008-04-18 00:46:26 +0200913 ireason = hwif->INB(hwif->io_ports[IDE_IREASON_OFFSET]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914
Bartlomiej Zolnierkiewicz8e7657a2008-01-25 22:17:12 +0100915 if (ireason & CD) {
Borislav Petkova1efc852008-02-06 02:57:52 +0100916 printk(KERN_ERR "ide-tape: CoD != 0 in %s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917 return ide_do_reset(drive);
918 }
Borislav Petkov346331f2008-04-18 00:46:26 +0200919 if (((ireason & IO) == IO) == !!(pc->flags & PC_FLAG_WRITING)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920 /* Hopefully, we will never get here */
921 printk(KERN_ERR "ide-tape: We wanted to %s, ",
Bartlomiej Zolnierkiewicz8e7657a2008-01-25 22:17:12 +0100922 (ireason & IO) ? "Write" : "Read");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923 printk(KERN_ERR "ide-tape: but the tape wants us to %s !\n",
Bartlomiej Zolnierkiewicz8e7657a2008-01-25 22:17:12 +0100924 (ireason & IO) ? "Read" : "Write");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925 return ide_do_reset(drive);
926 }
Borislav Petkov346331f2008-04-18 00:46:26 +0200927 if (!(pc->flags & PC_FLAG_WRITING)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928 /* Reading - Check that we have enough space */
Borislav Petkovd236d742008-04-18 00:46:27 +0200929 temp = pc->xferred + bcount;
930 if (temp > pc->req_xfer) {
931 if (temp > pc->buf_size) {
Borislav Petkova1efc852008-02-06 02:57:52 +0100932 printk(KERN_ERR "ide-tape: The tape wants to "
933 "send us more data than expected "
934 "- discarding data\n");
Bartlomiej Zolnierkiewicz7616c0a2008-04-18 00:46:26 +0200935 ide_atapi_discard_data(drive, bcount);
Borislav Petkova1efc852008-02-06 02:57:52 +0100936 ide_set_handler(drive, &idetape_pc_intr,
937 IDETAPE_WAIT_CMD, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938 return ide_started;
939 }
Borislav Petkov8004a8c2008-02-06 02:57:51 +0100940 debug_log(DBG_SENSE, "The tape wants to send us more "
941 "data than expected - allowing transfer\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942 }
Borislav Petkova1efc852008-02-06 02:57:52 +0100943 iobuf = &idetape_input_buffers;
944 xferfunc = hwif->atapi_input_bytes;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945 } else {
Borislav Petkova1efc852008-02-06 02:57:52 +0100946 iobuf = &idetape_output_buffers;
947 xferfunc = hwif->atapi_output_bytes;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948 }
Borislav Petkova1efc852008-02-06 02:57:52 +0100949
950 if (pc->bh)
951 iobuf(drive, pc, bcount);
952 else
Borislav Petkovd236d742008-04-18 00:46:27 +0200953 xferfunc(drive, pc->cur_pos, bcount);
Borislav Petkova1efc852008-02-06 02:57:52 +0100954
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955 /* Update the current position */
Borislav Petkovd236d742008-04-18 00:46:27 +0200956 pc->xferred += bcount;
957 pc->cur_pos += bcount;
Borislav Petkov8004a8c2008-02-06 02:57:51 +0100958
959 debug_log(DBG_SENSE, "[cmd %x] transferred %d bytes on that intr.\n",
960 pc->c[0], bcount);
961
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962 /* And set the interrupt handler again */
963 ide_set_handler(drive, &idetape_pc_intr, IDETAPE_WAIT_CMD, NULL);
964 return ide_started;
965}
966
967/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100968 * Packet Command Interface
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100970 * The current Packet Command is available in tape->pc, and will not change
971 * until we finish handling it. Each packet command is associated with a
972 * callback function that will be called when the command is finished.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100974 * The handling will be done in three stages:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100976 * 1. idetape_issue_pc will send the packet command to the drive, and will set
977 * the interrupt handler to idetape_pc_intr.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100979 * 2. On each interrupt, idetape_pc_intr will be called. This step will be
980 * repeated until the device signals us that no more interrupts will be issued.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100982 * 3. ATAPI Tape media access commands have immediate status with a delayed
983 * process. In case of a successful initiation of a media access packet command,
984 * the DSC bit will be set when the actual execution of the command is finished.
985 * Since the tape drive will not issue an interrupt, we have to poll for this
986 * event. In this case, we define the request as "low priority request" by
987 * setting rq_status to IDETAPE_RQ_POSTPONED, set a timer to poll for DSC and
988 * exit the driver.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100990 * ide.c will then give higher priority to requests which originate from the
991 * other device, until will change rq_status to RQ_ACTIVE.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100993 * 4. When the packet command is finished, it will be checked for errors.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100995 * 5. In case an error was found, we queue a request sense packet command in
996 * front of the request queue and retry the operation up to
997 * IDETAPE_MAX_PC_RETRIES times.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100999 * 6. In case no error was found, or we decided to give up and not to retry
1000 * again, the callback function will be called and then we will handle the next
1001 * request.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002 */
1003static ide_startstop_t idetape_transfer_pc(ide_drive_t *drive)
1004{
1005 ide_hwif_t *hwif = drive->hwif;
1006 idetape_tape_t *tape = drive->driver_data;
Borislav Petkovd236d742008-04-18 00:46:27 +02001007 struct ide_atapi_pc *pc = tape->pc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008 int retries = 100;
1009 ide_startstop_t startstop;
Bartlomiej Zolnierkiewicz8e7657a2008-01-25 22:17:12 +01001010 u8 ireason;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001012 if (ide_wait_stat(&startstop, drive, DRQ_STAT, BUSY_STAT, WAIT_READY)) {
1013 printk(KERN_ERR "ide-tape: Strange, packet command initiated "
1014 "yet DRQ isn't asserted\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015 return startstop;
1016 }
Bartlomiej Zolnierkiewicz23579a22008-04-18 00:46:26 +02001017 ireason = hwif->INB(hwif->io_ports[IDE_IREASON_OFFSET]);
Bartlomiej Zolnierkiewicz8e7657a2008-01-25 22:17:12 +01001018 while (retries-- && ((ireason & CD) == 0 || (ireason & IO))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019 printk(KERN_ERR "ide-tape: (IO,CoD != (0,1) while issuing "
1020 "a packet command, retrying\n");
1021 udelay(100);
Bartlomiej Zolnierkiewicz23579a22008-04-18 00:46:26 +02001022 ireason = hwif->INB(hwif->io_ports[IDE_IREASON_OFFSET]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023 if (retries == 0) {
1024 printk(KERN_ERR "ide-tape: (IO,CoD != (0,1) while "
1025 "issuing a packet command, ignoring\n");
Bartlomiej Zolnierkiewicz8e7657a2008-01-25 22:17:12 +01001026 ireason |= CD;
1027 ireason &= ~IO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028 }
1029 }
Bartlomiej Zolnierkiewicz8e7657a2008-01-25 22:17:12 +01001030 if ((ireason & CD) == 0 || (ireason & IO)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031 printk(KERN_ERR "ide-tape: (IO,CoD) != (0,1) while issuing "
1032 "a packet command\n");
1033 return ide_do_reset(drive);
1034 }
1035 /* Set the interrupt routine */
1036 ide_set_handler(drive, &idetape_pc_intr, IDETAPE_WAIT_CMD, NULL);
1037#ifdef CONFIG_BLK_DEV_IDEDMA
1038 /* Begin DMA, if necessary */
Borislav Petkov346331f2008-04-18 00:46:26 +02001039 if (pc->flags & PC_FLAG_DMA_IN_PROGRESS)
Bartlomiej Zolnierkiewicz5e37bdc2008-04-26 22:25:24 +02001040 hwif->dma_ops->dma_start(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041#endif
1042 /* Send the actual packet */
1043 HWIF(drive)->atapi_output_bytes(drive, pc->c, 12);
1044 return ide_started;
1045}
1046
Borislav Petkovd236d742008-04-18 00:46:27 +02001047static ide_startstop_t idetape_issue_pc(ide_drive_t *drive,
1048 struct ide_atapi_pc *pc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049{
1050 ide_hwif_t *hwif = drive->hwif;
1051 idetape_tape_t *tape = drive->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052 int dma_ok = 0;
Bartlomiej Zolnierkiewicz790d1232008-01-25 22:17:12 +01001053 u16 bcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054
Borislav Petkov90699ce2008-02-02 19:56:50 +01001055 if (tape->pc->c[0] == REQUEST_SENSE &&
1056 pc->c[0] == REQUEST_SENSE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057 printk(KERN_ERR "ide-tape: possible ide-tape.c bug - "
1058 "Two request sense in serial were issued\n");
1059 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060
Borislav Petkov90699ce2008-02-02 19:56:50 +01001061 if (tape->failed_pc == NULL && pc->c[0] != REQUEST_SENSE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062 tape->failed_pc = pc;
1063 /* Set the current packet command */
1064 tape->pc = pc;
1065
1066 if (pc->retries > IDETAPE_MAX_PC_RETRIES ||
Borislav Petkov346331f2008-04-18 00:46:26 +02001067 (pc->flags & PC_FLAG_ABORT)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068 /*
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001069 * We will "abort" retrying a packet command in case legitimate
1070 * error code was received (crossing a filemark, or end of the
1071 * media, for example).
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072 */
Borislav Petkov346331f2008-04-18 00:46:26 +02001073 if (!(pc->flags & PC_FLAG_ABORT)) {
Borislav Petkov90699ce2008-02-02 19:56:50 +01001074 if (!(pc->c[0] == TEST_UNIT_READY &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075 tape->sense_key == 2 && tape->asc == 4 &&
1076 (tape->ascq == 1 || tape->ascq == 8))) {
1077 printk(KERN_ERR "ide-tape: %s: I/O error, "
1078 "pc = %2x, key = %2x, "
1079 "asc = %2x, ascq = %2x\n",
1080 tape->name, pc->c[0],
1081 tape->sense_key, tape->asc,
1082 tape->ascq);
1083 }
1084 /* Giving up */
1085 pc->error = IDETAPE_ERROR_GENERAL;
1086 }
1087 tape->failed_pc = NULL;
Borislav Petkovd236d742008-04-18 00:46:27 +02001088 return pc->idetape_callback(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089 }
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001090 debug_log(DBG_SENSE, "Retry #%d, cmd = %02X\n", pc->retries, pc->c[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091
1092 pc->retries++;
1093 /* We haven't transferred any data yet */
Borislav Petkovd236d742008-04-18 00:46:27 +02001094 pc->xferred = 0;
1095 pc->cur_pos = pc->buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096 /* Request to transfer the entire buffer at once */
Borislav Petkovd236d742008-04-18 00:46:27 +02001097 bcount = pc->req_xfer;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098
Borislav Petkov346331f2008-04-18 00:46:26 +02001099 if (pc->flags & PC_FLAG_DMA_ERROR) {
1100 pc->flags &= ~PC_FLAG_DMA_ERROR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101 printk(KERN_WARNING "ide-tape: DMA disabled, "
1102 "reverting to PIO\n");
Bartlomiej Zolnierkiewicz7469aaf2007-02-17 02:40:26 +01001103 ide_dma_off(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104 }
Borislav Petkov346331f2008-04-18 00:46:26 +02001105 if ((pc->flags & PC_FLAG_DMA_RECOMMENDED) && drive->using_dma)
Bartlomiej Zolnierkiewicz5e37bdc2008-04-26 22:25:24 +02001106 dma_ok = !hwif->dma_ops->dma_setup(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107
Bartlomiej Zolnierkiewicz2fc57382008-01-25 22:17:13 +01001108 ide_pktcmd_tf_load(drive, IDE_TFLAG_NO_SELECT_MASK |
1109 IDE_TFLAG_OUT_DEVICE, bcount, dma_ok);
1110
Borislav Petkov346331f2008-04-18 00:46:26 +02001111 if (dma_ok)
1112 /* Will begin DMA later */
1113 pc->flags |= PC_FLAG_DMA_IN_PROGRESS;
1114 if (test_bit(IDETAPE_FLAG_DRQ_INTERRUPT, &tape->flags)) {
Bartlomiej Zolnierkiewiczc1c9dbc2008-02-02 19:56:44 +01001115 ide_execute_command(drive, WIN_PACKETCMD, &idetape_transfer_pc,
1116 IDETAPE_WAIT_CMD, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117 return ide_started;
1118 } else {
Bartlomiej Zolnierkiewicz23579a22008-04-18 00:46:26 +02001119 hwif->OUTB(WIN_PACKETCMD, hwif->io_ports[IDE_COMMAND_OFFSET]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120 return idetape_transfer_pc(drive);
1121 }
1122}
1123
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001124static ide_startstop_t idetape_pc_callback(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125{
1126 idetape_tape_t *tape = drive->driver_data;
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001127
1128 debug_log(DBG_PROCS, "Enter %s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129
1130 idetape_end_request(drive, tape->pc->error ? 0 : 1, 0);
1131 return ide_stopped;
1132}
1133
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001134/* A mode sense command is used to "sense" tape parameters. */
Borislav Petkovd236d742008-04-18 00:46:27 +02001135static void idetape_create_mode_sense_cmd(struct ide_atapi_pc *pc, u8 page_code)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136{
1137 idetape_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +01001138 pc->c[0] = MODE_SENSE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139 if (page_code != IDETAPE_BLOCK_DESCRIPTOR)
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001140 /* DBD = 1 - Don't return block descriptors */
1141 pc->c[1] = 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142 pc->c[2] = page_code;
1143 /*
1144 * Changed pc->c[3] to 0 (255 will at best return unused info).
1145 *
1146 * For SCSI this byte is defined as subpage instead of high byte
1147 * of length and some IDE drives seem to interpret it this way
1148 * and return an error when 255 is used.
1149 */
1150 pc->c[3] = 0;
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001151 /* We will just discard data in that case */
1152 pc->c[4] = 255;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153 if (page_code == IDETAPE_BLOCK_DESCRIPTOR)
Borislav Petkovd236d742008-04-18 00:46:27 +02001154 pc->req_xfer = 12;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155 else if (page_code == IDETAPE_CAPABILITIES_PAGE)
Borislav Petkovd236d742008-04-18 00:46:27 +02001156 pc->req_xfer = 24;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157 else
Borislav Petkovd236d742008-04-18 00:46:27 +02001158 pc->req_xfer = 50;
1159 pc->idetape_callback = &idetape_pc_callback;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160}
1161
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001162static ide_startstop_t idetape_media_access_finished(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163{
1164 idetape_tape_t *tape = drive->driver_data;
Borislav Petkovd236d742008-04-18 00:46:27 +02001165 struct ide_atapi_pc *pc = tape->pc;
Bartlomiej Zolnierkiewicz22c525b2008-01-25 22:17:11 +01001166 u8 stat;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167
Bartlomiej Zolnierkiewiczc47137a2008-02-06 02:57:51 +01001168 stat = ide_read_status(drive);
1169
Bartlomiej Zolnierkiewicz22c525b2008-01-25 22:17:11 +01001170 if (stat & SEEK_STAT) {
1171 if (stat & ERR_STAT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172 /* Error detected */
Borislav Petkov90699ce2008-02-02 19:56:50 +01001173 if (pc->c[0] != TEST_UNIT_READY)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174 printk(KERN_ERR "ide-tape: %s: I/O error, ",
1175 tape->name);
1176 /* Retry operation */
1177 return idetape_retry_pc(drive);
1178 }
1179 pc->error = 0;
1180 if (tape->failed_pc == pc)
1181 tape->failed_pc = NULL;
1182 } else {
1183 pc->error = IDETAPE_ERROR_GENERAL;
1184 tape->failed_pc = NULL;
1185 }
Borislav Petkovd236d742008-04-18 00:46:27 +02001186 return pc->idetape_callback(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187}
1188
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001189static ide_startstop_t idetape_rw_callback(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190{
1191 idetape_tape_t *tape = drive->driver_data;
1192 struct request *rq = HWGROUP(drive)->rq;
Borislav Petkovd236d742008-04-18 00:46:27 +02001193 int blocks = tape->pc->xferred / tape->blk_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194
Borislav Petkov54bb2072008-02-06 02:57:52 +01001195 tape->avg_size += blocks * tape->blk_size;
1196 tape->insert_size += blocks * tape->blk_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197 if (tape->insert_size > 1024 * 1024)
1198 tape->measure_insert_time = 1;
1199 if (tape->measure_insert_time) {
1200 tape->measure_insert_time = 0;
1201 tape->insert_time = jiffies;
1202 tape->insert_size = 0;
1203 }
1204 if (time_after(jiffies, tape->insert_time))
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001205 tape->insert_speed = tape->insert_size / 1024 * HZ /
1206 (jiffies - tape->insert_time);
Marcelo Feitoza Parisi9bae1ff2006-03-28 01:56:46 -08001207 if (time_after_eq(jiffies, tape->avg_time + HZ)) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001208 tape->avg_speed = tape->avg_size * HZ /
1209 (jiffies - tape->avg_time) / 1024;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210 tape->avg_size = 0;
1211 tape->avg_time = jiffies;
1212 }
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001213 debug_log(DBG_PROCS, "Enter %s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214
Borislav Petkov54bb2072008-02-06 02:57:52 +01001215 tape->first_frame += blocks;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216 rq->current_nr_sectors -= blocks;
1217
1218 if (!tape->pc->error)
1219 idetape_end_request(drive, 1, 0);
1220 else
1221 idetape_end_request(drive, tape->pc->error, 0);
1222 return ide_stopped;
1223}
1224
Borislav Petkovd236d742008-04-18 00:46:27 +02001225static void idetape_create_read_cmd(idetape_tape_t *tape,
1226 struct ide_atapi_pc *pc,
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001227 unsigned int length, struct idetape_bh *bh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228{
1229 idetape_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +01001230 pc->c[0] = READ_6;
Borislav Petkov860ff5e2008-02-02 19:56:50 +01001231 put_unaligned(cpu_to_be32(length), (unsigned int *) &pc->c[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232 pc->c[1] = 1;
Borislav Petkovd236d742008-04-18 00:46:27 +02001233 pc->idetape_callback = &idetape_rw_callback;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234 pc->bh = bh;
1235 atomic_set(&bh->b_count, 0);
Borislav Petkovd236d742008-04-18 00:46:27 +02001236 pc->buf = NULL;
1237 pc->buf_size = length * tape->blk_size;
1238 pc->req_xfer = pc->buf_size;
1239 if (pc->req_xfer == tape->stage_size)
Borislav Petkov346331f2008-04-18 00:46:26 +02001240 pc->flags |= PC_FLAG_DMA_RECOMMENDED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241}
1242
Borislav Petkovd236d742008-04-18 00:46:27 +02001243static void idetape_create_write_cmd(idetape_tape_t *tape,
1244 struct ide_atapi_pc *pc,
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001245 unsigned int length, struct idetape_bh *bh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246{
1247 idetape_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +01001248 pc->c[0] = WRITE_6;
Borislav Petkov860ff5e2008-02-02 19:56:50 +01001249 put_unaligned(cpu_to_be32(length), (unsigned int *) &pc->c[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250 pc->c[1] = 1;
Borislav Petkovd236d742008-04-18 00:46:27 +02001251 pc->idetape_callback = &idetape_rw_callback;
Borislav Petkov346331f2008-04-18 00:46:26 +02001252 pc->flags |= PC_FLAG_WRITING;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253 pc->bh = bh;
1254 pc->b_data = bh->b_data;
1255 pc->b_count = atomic_read(&bh->b_count);
Borislav Petkovd236d742008-04-18 00:46:27 +02001256 pc->buf = NULL;
1257 pc->buf_size = length * tape->blk_size;
1258 pc->req_xfer = pc->buf_size;
1259 if (pc->req_xfer == tape->stage_size)
Borislav Petkov346331f2008-04-18 00:46:26 +02001260 pc->flags |= PC_FLAG_DMA_RECOMMENDED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261}
1262
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263static ide_startstop_t idetape_do_request(ide_drive_t *drive,
1264 struct request *rq, sector_t block)
1265{
1266 idetape_tape_t *tape = drive->driver_data;
Borislav Petkovd236d742008-04-18 00:46:27 +02001267 struct ide_atapi_pc *pc = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268 struct request *postponed_rq = tape->postponed_rq;
Bartlomiej Zolnierkiewicz22c525b2008-01-25 22:17:11 +01001269 u8 stat;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001271 debug_log(DBG_SENSE, "sector: %ld, nr_sectors: %ld,"
1272 " current_nr_sectors: %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273 rq->sector, rq->nr_sectors, rq->current_nr_sectors);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274
Jens Axboe4aff5e22006-08-10 08:44:47 +02001275 if (!blk_special_request(rq)) {
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001276 /* We do not support buffer cache originated requests. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277 printk(KERN_NOTICE "ide-tape: %s: Unsupported request in "
Jens Axboe4aff5e22006-08-10 08:44:47 +02001278 "request queue (%d)\n", drive->name, rq->cmd_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279 ide_end_request(drive, 0, 0);
1280 return ide_stopped;
1281 }
1282
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001283 /* Retry a failed packet command */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001284 if (tape->failed_pc && tape->pc->c[0] == REQUEST_SENSE)
Borislav Petkov8d06bfa2008-02-06 02:57:53 +01001285 return idetape_issue_pc(drive, tape->failed_pc);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001286
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287 if (postponed_rq != NULL)
1288 if (rq != postponed_rq) {
1289 printk(KERN_ERR "ide-tape: ide-tape.c bug - "
1290 "Two DSC requests were queued\n");
1291 idetape_end_request(drive, 0, 0);
1292 return ide_stopped;
1293 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294
1295 tape->postponed_rq = NULL;
1296
1297 /*
1298 * If the tape is still busy, postpone our request and service
1299 * the other device meanwhile.
1300 */
Bartlomiej Zolnierkiewiczc47137a2008-02-06 02:57:51 +01001301 stat = ide_read_status(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302
1303 if (!drive->dsc_overlap && !(rq->cmd[0] & REQ_IDETAPE_PC2))
Borislav Petkov346331f2008-04-18 00:46:26 +02001304 set_bit(IDETAPE_FLAG_IGNORE_DSC, &tape->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305
1306 if (drive->post_reset == 1) {
Borislav Petkov346331f2008-04-18 00:46:26 +02001307 set_bit(IDETAPE_FLAG_IGNORE_DSC, &tape->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308 drive->post_reset = 0;
1309 }
1310
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311 if (time_after(jiffies, tape->insert_time))
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001312 tape->insert_speed = tape->insert_size / 1024 * HZ /
1313 (jiffies - tape->insert_time);
Borislav Petkov346331f2008-04-18 00:46:26 +02001314 if (!test_and_clear_bit(IDETAPE_FLAG_IGNORE_DSC, &tape->flags) &&
Bartlomiej Zolnierkiewicz22c525b2008-01-25 22:17:11 +01001315 (stat & SEEK_STAT) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316 if (postponed_rq == NULL) {
1317 tape->dsc_polling_start = jiffies;
Borislav Petkov54bb2072008-02-06 02:57:52 +01001318 tape->dsc_poll_freq = tape->best_dsc_rw_freq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319 tape->dsc_timeout = jiffies + IDETAPE_DSC_RW_TIMEOUT;
1320 } else if (time_after(jiffies, tape->dsc_timeout)) {
1321 printk(KERN_ERR "ide-tape: %s: DSC timeout\n",
1322 tape->name);
1323 if (rq->cmd[0] & REQ_IDETAPE_PC2) {
1324 idetape_media_access_finished(drive);
1325 return ide_stopped;
1326 } else {
1327 return ide_do_reset(drive);
1328 }
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001329 } else if (time_after(jiffies,
1330 tape->dsc_polling_start +
1331 IDETAPE_DSC_MA_THRESHOLD))
Borislav Petkov54bb2072008-02-06 02:57:52 +01001332 tape->dsc_poll_freq = IDETAPE_DSC_MA_SLOW;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333 idetape_postpone_request(drive);
1334 return ide_stopped;
1335 }
1336 if (rq->cmd[0] & REQ_IDETAPE_READ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337 tape->postpone_cnt = 0;
1338 pc = idetape_next_pc_storage(drive);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001339 idetape_create_read_cmd(tape, pc, rq->current_nr_sectors,
1340 (struct idetape_bh *)rq->special);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341 goto out;
1342 }
1343 if (rq->cmd[0] & REQ_IDETAPE_WRITE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344 tape->postpone_cnt = 0;
1345 pc = idetape_next_pc_storage(drive);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001346 idetape_create_write_cmd(tape, pc, rq->current_nr_sectors,
1347 (struct idetape_bh *)rq->special);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348 goto out;
1349 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350 if (rq->cmd[0] & REQ_IDETAPE_PC1) {
Borislav Petkovd236d742008-04-18 00:46:27 +02001351 pc = (struct ide_atapi_pc *) rq->buffer;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352 rq->cmd[0] &= ~(REQ_IDETAPE_PC1);
1353 rq->cmd[0] |= REQ_IDETAPE_PC2;
1354 goto out;
1355 }
1356 if (rq->cmd[0] & REQ_IDETAPE_PC2) {
1357 idetape_media_access_finished(drive);
1358 return ide_stopped;
1359 }
1360 BUG();
1361out:
Borislav Petkov8d06bfa2008-02-06 02:57:53 +01001362 return idetape_issue_pc(drive, pc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363}
1364
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001365/* Pipeline related functions */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366
1367/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001368 * The function below uses __get_free_page to allocate a pipeline stage, along
1369 * with all the necessary small buffers which together make a buffer of size
1370 * tape->stage_size (or a bit more). We attempt to combine sequential pages as
1371 * much as possible.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001373 * It returns a pointer to the new allocated stage, or NULL if we can't (or
1374 * don't want to) allocate a stage.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001376 * Pipeline stages are optional and are used to increase performance. If we
1377 * can't allocate them, we'll manage without them.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378 */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001379static idetape_stage_t *__idetape_kmalloc_stage(idetape_tape_t *tape, int full,
1380 int clear)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381{
1382 idetape_stage_t *stage;
1383 struct idetape_bh *prev_bh, *bh;
1384 int pages = tape->pages_per_stage;
1385 char *b_data = NULL;
1386
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001387 stage = kmalloc(sizeof(idetape_stage_t), GFP_KERNEL);
1388 if (!stage)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389 return NULL;
1390 stage->next = NULL;
1391
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001392 stage->bh = kmalloc(sizeof(struct idetape_bh), GFP_KERNEL);
1393 bh = stage->bh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394 if (bh == NULL)
1395 goto abort;
1396 bh->b_reqnext = NULL;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001397 bh->b_data = (char *) __get_free_page(GFP_KERNEL);
1398 if (!bh->b_data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399 goto abort;
1400 if (clear)
1401 memset(bh->b_data, 0, PAGE_SIZE);
1402 bh->b_size = PAGE_SIZE;
1403 atomic_set(&bh->b_count, full ? bh->b_size : 0);
1404
1405 while (--pages) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001406 b_data = (char *) __get_free_page(GFP_KERNEL);
1407 if (!b_data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408 goto abort;
1409 if (clear)
1410 memset(b_data, 0, PAGE_SIZE);
1411 if (bh->b_data == b_data + PAGE_SIZE) {
1412 bh->b_size += PAGE_SIZE;
1413 bh->b_data -= PAGE_SIZE;
1414 if (full)
1415 atomic_add(PAGE_SIZE, &bh->b_count);
1416 continue;
1417 }
1418 if (b_data == bh->b_data + bh->b_size) {
1419 bh->b_size += PAGE_SIZE;
1420 if (full)
1421 atomic_add(PAGE_SIZE, &bh->b_count);
1422 continue;
1423 }
1424 prev_bh = bh;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001425 bh = kmalloc(sizeof(struct idetape_bh), GFP_KERNEL);
1426 if (!bh) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427 free_page((unsigned long) b_data);
1428 goto abort;
1429 }
1430 bh->b_reqnext = NULL;
1431 bh->b_data = b_data;
1432 bh->b_size = PAGE_SIZE;
1433 atomic_set(&bh->b_count, full ? bh->b_size : 0);
1434 prev_bh->b_reqnext = bh;
1435 }
1436 bh->b_size -= tape->excess_bh_size;
1437 if (full)
1438 atomic_sub(tape->excess_bh_size, &bh->b_count);
1439 return stage;
1440abort:
1441 __idetape_kfree_stage(stage);
1442 return NULL;
1443}
1444
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001445static int idetape_copy_stage_from_user(idetape_tape_t *tape,
Borislav Petkov8646c882008-04-27 15:38:26 +02001446 const char __user *buf, int n)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447{
1448 struct idetape_bh *bh = tape->bh;
1449 int count;
Daniel Walkerdcd96372006-06-25 05:47:37 -07001450 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451
1452 while (n) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453 if (bh == NULL) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001454 printk(KERN_ERR "ide-tape: bh == NULL in %s\n",
1455 __func__);
Daniel Walkerdcd96372006-06-25 05:47:37 -07001456 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457 }
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001458 count = min((unsigned int)
1459 (bh->b_size - atomic_read(&bh->b_count)),
1460 (unsigned int)n);
1461 if (copy_from_user(bh->b_data + atomic_read(&bh->b_count), buf,
1462 count))
Daniel Walkerdcd96372006-06-25 05:47:37 -07001463 ret = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464 n -= count;
1465 atomic_add(count, &bh->b_count);
1466 buf += count;
1467 if (atomic_read(&bh->b_count) == bh->b_size) {
1468 bh = bh->b_reqnext;
1469 if (bh)
1470 atomic_set(&bh->b_count, 0);
1471 }
1472 }
1473 tape->bh = bh;
Daniel Walkerdcd96372006-06-25 05:47:37 -07001474 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475}
1476
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001477static int idetape_copy_stage_to_user(idetape_tape_t *tape, char __user *buf,
Borislav Petkov99d74e62008-04-27 15:38:25 +02001478 int n)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479{
1480 struct idetape_bh *bh = tape->bh;
1481 int count;
Daniel Walkerdcd96372006-06-25 05:47:37 -07001482 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483
1484 while (n) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485 if (bh == NULL) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001486 printk(KERN_ERR "ide-tape: bh == NULL in %s\n",
1487 __func__);
Daniel Walkerdcd96372006-06-25 05:47:37 -07001488 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490 count = min(tape->b_count, n);
Daniel Walkerdcd96372006-06-25 05:47:37 -07001491 if (copy_to_user(buf, tape->b_data, count))
1492 ret = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493 n -= count;
1494 tape->b_data += count;
1495 tape->b_count -= count;
1496 buf += count;
1497 if (!tape->b_count) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001498 bh = bh->b_reqnext;
1499 tape->bh = bh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500 if (bh) {
1501 tape->b_data = bh->b_data;
1502 tape->b_count = atomic_read(&bh->b_count);
1503 }
1504 }
1505 }
Daniel Walkerdcd96372006-06-25 05:47:37 -07001506 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507}
1508
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001509static void idetape_init_merge_stage(idetape_tape_t *tape)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510{
1511 struct idetape_bh *bh = tape->merge_stage->bh;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001512
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513 tape->bh = bh;
Borislav Petkov54abf372008-02-06 02:57:52 +01001514 if (tape->chrdev_dir == IDETAPE_DIR_WRITE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515 atomic_set(&bh->b_count, 0);
1516 else {
1517 tape->b_data = bh->b_data;
1518 tape->b_count = atomic_read(&bh->b_count);
1519 }
1520}
1521
Borislav Petkova2f5b7f2008-02-06 02:57:51 +01001522static ide_startstop_t idetape_read_position_callback(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523{
1524 idetape_tape_t *tape = drive->driver_data;
Borislav Petkovd236d742008-04-18 00:46:27 +02001525 u8 *readpos = tape->pc->buf;
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001526
1527 debug_log(DBG_PROCS, "Enter %s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001528
1529 if (!tape->pc->error) {
Borislav Petkova2f5b7f2008-02-06 02:57:51 +01001530 debug_log(DBG_SENSE, "BOP - %s\n",
1531 (readpos[0] & 0x80) ? "Yes" : "No");
1532 debug_log(DBG_SENSE, "EOP - %s\n",
1533 (readpos[0] & 0x40) ? "Yes" : "No");
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001534
Borislav Petkova2f5b7f2008-02-06 02:57:51 +01001535 if (readpos[0] & 0x4) {
1536 printk(KERN_INFO "ide-tape: Block location is unknown"
1537 "to the tape\n");
Borislav Petkov346331f2008-04-18 00:46:26 +02001538 clear_bit(IDETAPE_FLAG_ADDRESS_VALID, &tape->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539 idetape_end_request(drive, 0, 0);
1540 } else {
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001541 debug_log(DBG_SENSE, "Block Location - %u\n",
Borislav Petkova2f5b7f2008-02-06 02:57:51 +01001542 be32_to_cpu(*(u32 *)&readpos[4]));
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001543
Borislav Petkova2f5b7f2008-02-06 02:57:51 +01001544 tape->partition = readpos[1];
Borislav Petkov54bb2072008-02-06 02:57:52 +01001545 tape->first_frame =
Borislav Petkova2f5b7f2008-02-06 02:57:51 +01001546 be32_to_cpu(*(u32 *)&readpos[4]);
Borislav Petkov346331f2008-04-18 00:46:26 +02001547 set_bit(IDETAPE_FLAG_ADDRESS_VALID, &tape->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548 idetape_end_request(drive, 1, 0);
1549 }
1550 } else {
1551 idetape_end_request(drive, 0, 0);
1552 }
1553 return ide_stopped;
1554}
1555
1556/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001557 * Write a filemark if write_filemark=1. Flush the device buffers without
1558 * writing a filemark otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559 */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001560static void idetape_create_write_filemark_cmd(ide_drive_t *drive,
Borislav Petkovd236d742008-04-18 00:46:27 +02001561 struct ide_atapi_pc *pc, int write_filemark)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001562{
1563 idetape_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +01001564 pc->c[0] = WRITE_FILEMARKS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565 pc->c[4] = write_filemark;
Borislav Petkov346331f2008-04-18 00:46:26 +02001566 pc->flags |= PC_FLAG_WAIT_FOR_DSC;
Borislav Petkovd236d742008-04-18 00:46:27 +02001567 pc->idetape_callback = &idetape_pc_callback;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001568}
1569
Borislav Petkovd236d742008-04-18 00:46:27 +02001570static void idetape_create_test_unit_ready_cmd(struct ide_atapi_pc *pc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571{
1572 idetape_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +01001573 pc->c[0] = TEST_UNIT_READY;
Borislav Petkovd236d742008-04-18 00:46:27 +02001574 pc->idetape_callback = &idetape_pc_callback;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001575}
1576
1577/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001578 * We add a special packet command request to the tail of the request queue, and
1579 * wait for it to be serviced. This is not to be called from within the request
1580 * handling part of the driver! We allocate here data on the stack and it is
1581 * valid until the request is finished. This is not the case for the bottom part
1582 * of the driver, where we are always leaving the functions to wait for an
1583 * interrupt or a timer event.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001585 * From the bottom part of the driver, we should allocate safe memory using
1586 * idetape_next_pc_storage() and ide_tape_next_rq_storage(), and add the request
1587 * to the request list without waiting for it to be serviced! In that case, we
1588 * usually use idetape_queue_pc_head().
Linus Torvalds1da177e2005-04-16 15:20:36 -07001589 */
Borislav Petkovea1ab3d2008-04-27 15:38:27 +02001590static int idetape_queue_pc_tail(ide_drive_t *drive, struct ide_atapi_pc *pc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591{
1592 struct ide_tape_obj *tape = drive->driver_data;
1593 struct request rq;
1594
1595 idetape_init_rq(&rq, REQ_IDETAPE_PC1);
1596 rq.buffer = (char *) pc;
1597 rq.rq_disk = tape->disk;
1598 return ide_do_drive_cmd(drive, &rq, ide_wait);
1599}
1600
Borislav Petkovd236d742008-04-18 00:46:27 +02001601static void idetape_create_load_unload_cmd(ide_drive_t *drive,
1602 struct ide_atapi_pc *pc, int cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001603{
1604 idetape_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +01001605 pc->c[0] = START_STOP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606 pc->c[4] = cmd;
Borislav Petkov346331f2008-04-18 00:46:26 +02001607 pc->flags |= PC_FLAG_WAIT_FOR_DSC;
Borislav Petkovd236d742008-04-18 00:46:27 +02001608 pc->idetape_callback = &idetape_pc_callback;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001609}
1610
1611static int idetape_wait_ready(ide_drive_t *drive, unsigned long timeout)
1612{
1613 idetape_tape_t *tape = drive->driver_data;
Borislav Petkovd236d742008-04-18 00:46:27 +02001614 struct ide_atapi_pc pc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001615 int load_attempted = 0;
1616
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001617 /* Wait for the tape to become ready */
Borislav Petkov346331f2008-04-18 00:46:26 +02001618 set_bit(IDETAPE_FLAG_MEDIUM_PRESENT, &tape->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001619 timeout += jiffies;
1620 while (time_before(jiffies, timeout)) {
1621 idetape_create_test_unit_ready_cmd(&pc);
Borislav Petkovea1ab3d2008-04-27 15:38:27 +02001622 if (!idetape_queue_pc_tail(drive, &pc))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623 return 0;
1624 if ((tape->sense_key == 2 && tape->asc == 4 && tape->ascq == 2)
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001625 || (tape->asc == 0x3A)) {
1626 /* no media */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001627 if (load_attempted)
1628 return -ENOMEDIUM;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001629 idetape_create_load_unload_cmd(drive, &pc,
1630 IDETAPE_LU_LOAD_MASK);
Borislav Petkovea1ab3d2008-04-27 15:38:27 +02001631 idetape_queue_pc_tail(drive, &pc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632 load_attempted = 1;
1633 /* not about to be ready */
1634 } else if (!(tape->sense_key == 2 && tape->asc == 4 &&
1635 (tape->ascq == 1 || tape->ascq == 8)))
1636 return -EIO;
Nishanth Aravamudan80ce45f2005-09-10 00:27:08 -07001637 msleep(100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638 }
1639 return -EIO;
1640}
1641
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001642static int idetape_flush_tape_buffers(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643{
Borislav Petkovd236d742008-04-18 00:46:27 +02001644 struct ide_atapi_pc pc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001645 int rc;
1646
1647 idetape_create_write_filemark_cmd(drive, &pc, 0);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001648 rc = idetape_queue_pc_tail(drive, &pc);
1649 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001650 return rc;
1651 idetape_wait_ready(drive, 60 * 5 * HZ);
1652 return 0;
1653}
1654
Borislav Petkovd236d742008-04-18 00:46:27 +02001655static void idetape_create_read_position_cmd(struct ide_atapi_pc *pc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001656{
1657 idetape_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +01001658 pc->c[0] = READ_POSITION;
Borislav Petkovd236d742008-04-18 00:46:27 +02001659 pc->req_xfer = 20;
1660 pc->idetape_callback = &idetape_read_position_callback;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001661}
1662
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001663static int idetape_read_position(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664{
1665 idetape_tape_t *tape = drive->driver_data;
Borislav Petkovd236d742008-04-18 00:46:27 +02001666 struct ide_atapi_pc pc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001667 int position;
1668
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001669 debug_log(DBG_PROCS, "Enter %s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001670
1671 idetape_create_read_position_cmd(&pc);
1672 if (idetape_queue_pc_tail(drive, &pc))
1673 return -1;
Borislav Petkov54bb2072008-02-06 02:57:52 +01001674 position = tape->first_frame;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675 return position;
1676}
1677
Borislav Petkovd236d742008-04-18 00:46:27 +02001678static void idetape_create_locate_cmd(ide_drive_t *drive,
1679 struct ide_atapi_pc *pc,
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001680 unsigned int block, u8 partition, int skip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681{
1682 idetape_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +01001683 pc->c[0] = POSITION_TO_ELEMENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001684 pc->c[1] = 2;
Borislav Petkov860ff5e2008-02-02 19:56:50 +01001685 put_unaligned(cpu_to_be32(block), (unsigned int *) &pc->c[3]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001686 pc->c[8] = partition;
Borislav Petkov346331f2008-04-18 00:46:26 +02001687 pc->flags |= PC_FLAG_WAIT_FOR_DSC;
Borislav Petkovd236d742008-04-18 00:46:27 +02001688 pc->idetape_callback = &idetape_pc_callback;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001689}
1690
Borislav Petkovd236d742008-04-18 00:46:27 +02001691static int idetape_create_prevent_cmd(ide_drive_t *drive,
1692 struct ide_atapi_pc *pc, int prevent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001693{
1694 idetape_tape_t *tape = drive->driver_data;
1695
Borislav Petkovb6422012008-02-02 19:56:49 +01001696 /* device supports locking according to capabilities page */
1697 if (!(tape->caps[6] & 0x01))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001698 return 0;
1699
1700 idetape_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +01001701 pc->c[0] = ALLOW_MEDIUM_REMOVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001702 pc->c[4] = prevent;
Borislav Petkovd236d742008-04-18 00:46:27 +02001703 pc->idetape_callback = &idetape_pc_callback;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001704 return 1;
1705}
1706
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001707static int __idetape_discard_read_pipeline(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001708{
1709 idetape_tape_t *tape = drive->driver_data;
1710 unsigned long flags;
1711 int cnt;
1712
Borislav Petkov54abf372008-02-06 02:57:52 +01001713 if (tape->chrdev_dir != IDETAPE_DIR_READ)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001714 return 0;
1715
1716 /* Remove merge stage. */
Borislav Petkov54bb2072008-02-06 02:57:52 +01001717 cnt = tape->merge_stage_size / tape->blk_size;
Borislav Petkov346331f2008-04-18 00:46:26 +02001718 if (test_and_clear_bit(IDETAPE_FLAG_FILEMARK, &tape->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001719 ++cnt; /* Filemarks count as 1 sector */
1720 tape->merge_stage_size = 0;
1721 if (tape->merge_stage != NULL) {
1722 __idetape_kfree_stage(tape->merge_stage);
1723 tape->merge_stage = NULL;
1724 }
1725
Borislav Petkov54abf372008-02-06 02:57:52 +01001726 tape->chrdev_dir = IDETAPE_DIR_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001727
1728 /* Remove pipeline stages. */
1729 if (tape->first_stage == NULL)
1730 return 0;
1731
Borislav Petkov54bb2072008-02-06 02:57:52 +01001732 spin_lock_irqsave(&tape->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001733 tape->next_stage = NULL;
Borislav Petkov54bb2072008-02-06 02:57:52 +01001734 spin_unlock_irqrestore(&tape->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001735
1736 while (tape->first_stage != NULL) {
1737 struct request *rq_ptr = &tape->first_stage->rq;
1738
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001739 cnt += rq_ptr->nr_sectors - rq_ptr->current_nr_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001740 if (rq_ptr->errors == IDETAPE_ERROR_FILEMARK)
1741 ++cnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001742 }
1743 tape->nr_pending_stages = 0;
1744 tape->max_stages = tape->min_pipeline;
1745 return cnt;
1746}
1747
1748/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001749 * Position the tape to the requested block using the LOCATE packet command.
1750 * A READ POSITION command is then issued to check where we are positioned. Like
1751 * all higher level operations, we queue the commands at the tail of the request
1752 * queue and wait for their completion.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001753 */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001754static int idetape_position_tape(ide_drive_t *drive, unsigned int block,
1755 u8 partition, int skip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001756{
1757 idetape_tape_t *tape = drive->driver_data;
1758 int retval;
Borislav Petkovd236d742008-04-18 00:46:27 +02001759 struct ide_atapi_pc pc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001760
Borislav Petkov54abf372008-02-06 02:57:52 +01001761 if (tape->chrdev_dir == IDETAPE_DIR_READ)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001762 __idetape_discard_read_pipeline(drive);
1763 idetape_wait_ready(drive, 60 * 5 * HZ);
1764 idetape_create_locate_cmd(drive, &pc, block, partition, skip);
1765 retval = idetape_queue_pc_tail(drive, &pc);
1766 if (retval)
1767 return (retval);
1768
1769 idetape_create_read_position_cmd(&pc);
1770 return (idetape_queue_pc_tail(drive, &pc));
1771}
1772
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001773static void idetape_discard_read_pipeline(ide_drive_t *drive,
1774 int restore_position)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001775{
1776 idetape_tape_t *tape = drive->driver_data;
1777 int cnt;
1778 int seek, position;
1779
1780 cnt = __idetape_discard_read_pipeline(drive);
1781 if (restore_position) {
1782 position = idetape_read_position(drive);
1783 seek = position > cnt ? position - cnt : 0;
1784 if (idetape_position_tape(drive, seek, 0, 0)) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001785 printk(KERN_INFO "ide-tape: %s: position_tape failed in"
1786 " discard_pipeline()\n", tape->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001787 return;
1788 }
1789 }
1790}
1791
1792/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001793 * Generate a read/write request for the block device interface and wait for it
1794 * to be serviced.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001795 */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001796static int idetape_queue_rw_tail(ide_drive_t *drive, int cmd, int blocks,
1797 struct idetape_bh *bh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001798{
1799 idetape_tape_t *tape = drive->driver_data;
1800 struct request rq;
1801
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001802 debug_log(DBG_SENSE, "%s: cmd=%d\n", __func__, cmd);
1803
Linus Torvalds1da177e2005-04-16 15:20:36 -07001804 idetape_init_rq(&rq, cmd);
1805 rq.rq_disk = tape->disk;
1806 rq.special = (void *)bh;
Borislav Petkov54bb2072008-02-06 02:57:52 +01001807 rq.sector = tape->first_frame;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001808 rq.nr_sectors = blocks;
1809 rq.current_nr_sectors = blocks;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001810 (void) ide_do_drive_cmd(drive, &rq, ide_wait);
1811
1812 if ((cmd & (REQ_IDETAPE_READ | REQ_IDETAPE_WRITE)) == 0)
1813 return 0;
1814
1815 if (tape->merge_stage)
1816 idetape_init_merge_stage(tape);
1817 if (rq.errors == IDETAPE_ERROR_GENERAL)
1818 return -EIO;
Borislav Petkov54bb2072008-02-06 02:57:52 +01001819 return (tape->blk_size * (blocks-rq.current_nr_sectors));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001820}
1821
Borislav Petkovd236d742008-04-18 00:46:27 +02001822static void idetape_create_inquiry_cmd(struct ide_atapi_pc *pc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001823{
1824 idetape_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +01001825 pc->c[0] = INQUIRY;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001826 pc->c[4] = 254;
Borislav Petkovd236d742008-04-18 00:46:27 +02001827 pc->req_xfer = 254;
1828 pc->idetape_callback = &idetape_pc_callback;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001829}
1830
Borislav Petkovd236d742008-04-18 00:46:27 +02001831static void idetape_create_rewind_cmd(ide_drive_t *drive,
1832 struct ide_atapi_pc *pc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001833{
1834 idetape_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +01001835 pc->c[0] = REZERO_UNIT;
Borislav Petkov346331f2008-04-18 00:46:26 +02001836 pc->flags |= PC_FLAG_WAIT_FOR_DSC;
Borislav Petkovd236d742008-04-18 00:46:27 +02001837 pc->idetape_callback = &idetape_pc_callback;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001838}
1839
Borislav Petkovd236d742008-04-18 00:46:27 +02001840static void idetape_create_erase_cmd(struct ide_atapi_pc *pc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001841{
1842 idetape_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +01001843 pc->c[0] = ERASE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001844 pc->c[1] = 1;
Borislav Petkov346331f2008-04-18 00:46:26 +02001845 pc->flags |= PC_FLAG_WAIT_FOR_DSC;
Borislav Petkovd236d742008-04-18 00:46:27 +02001846 pc->idetape_callback = &idetape_pc_callback;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001847}
1848
Borislav Petkovd236d742008-04-18 00:46:27 +02001849static void idetape_create_space_cmd(struct ide_atapi_pc *pc, int count, u8 cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001850{
1851 idetape_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +01001852 pc->c[0] = SPACE;
Borislav Petkov860ff5e2008-02-02 19:56:50 +01001853 put_unaligned(cpu_to_be32(count), (unsigned int *) &pc->c[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001854 pc->c[1] = cmd;
Borislav Petkov346331f2008-04-18 00:46:26 +02001855 pc->flags |= PC_FLAG_WAIT_FOR_DSC;
Borislav Petkovd236d742008-04-18 00:46:27 +02001856 pc->idetape_callback = &idetape_pc_callback;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001857}
1858
Borislav Petkov97c566c2008-04-27 15:38:25 +02001859/* Queue up a character device originated write request. */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001860static int idetape_add_chrdev_write_request(ide_drive_t *drive, int blocks)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001861{
1862 idetape_tape_t *tape = drive->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001863
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001864 debug_log(DBG_CHRDEV, "Enter %s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001865
Borislav Petkov0aa4b012008-04-27 15:38:27 +02001866 return idetape_queue_rw_tail(drive, REQ_IDETAPE_WRITE,
1867 blocks, tape->merge_stage->bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001868}
1869
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001870static void idetape_empty_write_pipeline(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001871{
1872 idetape_tape_t *tape = drive->driver_data;
1873 int blocks, min;
1874 struct idetape_bh *bh;
Borislav Petkov55a5d292008-02-02 19:56:49 +01001875
Borislav Petkov54abf372008-02-06 02:57:52 +01001876 if (tape->chrdev_dir != IDETAPE_DIR_WRITE) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001877 printk(KERN_ERR "ide-tape: bug: Trying to empty write pipeline,"
1878 " but we are not writing.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001879 return;
1880 }
1881 if (tape->merge_stage_size > tape->stage_size) {
1882 printk(KERN_ERR "ide-tape: bug: merge_buffer too big\n");
1883 tape->merge_stage_size = tape->stage_size;
1884 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001885 if (tape->merge_stage_size) {
Borislav Petkov54bb2072008-02-06 02:57:52 +01001886 blocks = tape->merge_stage_size / tape->blk_size;
1887 if (tape->merge_stage_size % tape->blk_size) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001888 unsigned int i;
1889
1890 blocks++;
Borislav Petkov54bb2072008-02-06 02:57:52 +01001891 i = tape->blk_size - tape->merge_stage_size %
1892 tape->blk_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001893 bh = tape->bh->b_reqnext;
1894 while (bh) {
1895 atomic_set(&bh->b_count, 0);
1896 bh = bh->b_reqnext;
1897 }
1898 bh = tape->bh;
1899 while (i) {
1900 if (bh == NULL) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001901 printk(KERN_INFO "ide-tape: bug,"
1902 " bh NULL\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001903 break;
1904 }
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001905 min = min(i, (unsigned int)(bh->b_size -
1906 atomic_read(&bh->b_count)));
1907 memset(bh->b_data + atomic_read(&bh->b_count),
1908 0, min);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001909 atomic_add(min, &bh->b_count);
1910 i -= min;
1911 bh = bh->b_reqnext;
1912 }
1913 }
1914 (void) idetape_add_chrdev_write_request(drive, blocks);
1915 tape->merge_stage_size = 0;
1916 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001917 if (tape->merge_stage != NULL) {
1918 __idetape_kfree_stage(tape->merge_stage);
1919 tape->merge_stage = NULL;
1920 }
Borislav Petkov54abf372008-02-06 02:57:52 +01001921 tape->chrdev_dir = IDETAPE_DIR_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001922
1923 /*
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001924 * On the next backup, perform the feedback loop again. (I don't want to
1925 * keep sense information between backups, as some systems are
1926 * constantly on, and the system load can be totally different on the
1927 * next backup).
Linus Torvalds1da177e2005-04-16 15:20:36 -07001928 */
1929 tape->max_stages = tape->min_pipeline;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001930 if (tape->first_stage != NULL ||
1931 tape->next_stage != NULL ||
1932 tape->last_stage != NULL ||
1933 tape->nr_stages != 0) {
1934 printk(KERN_ERR "ide-tape: ide-tape pipeline bug, "
1935 "first_stage %p, next_stage %p, "
1936 "last_stage %p, nr_stages %d\n",
1937 tape->first_stage, tape->next_stage,
1938 tape->last_stage, tape->nr_stages);
1939 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001940}
1941
Borislav Petkov8d06bfa2008-02-06 02:57:53 +01001942static int idetape_init_read(ide_drive_t *drive, int max_stages)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001943{
1944 idetape_tape_t *tape = drive->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001945 int bytes_read;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001946
1947 /* Initialize read operation */
Borislav Petkov54abf372008-02-06 02:57:52 +01001948 if (tape->chrdev_dir != IDETAPE_DIR_READ) {
1949 if (tape->chrdev_dir == IDETAPE_DIR_WRITE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001950 idetape_empty_write_pipeline(drive);
1951 idetape_flush_tape_buffers(drive);
1952 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001953 if (tape->merge_stage || tape->merge_stage_size) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001954 printk(KERN_ERR "ide-tape: merge_stage_size should be"
1955 " 0 now\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001956 tape->merge_stage_size = 0;
1957 }
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001958 tape->merge_stage = __idetape_kmalloc_stage(tape, 0, 0);
1959 if (!tape->merge_stage)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001960 return -ENOMEM;
Borislav Petkov54abf372008-02-06 02:57:52 +01001961 tape->chrdev_dir = IDETAPE_DIR_READ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001962
1963 /*
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001964 * Issue a read 0 command to ensure that DSC handshake is
1965 * switched from completion mode to buffer available mode.
1966 * No point in issuing this if DSC overlap isn't supported, some
1967 * drives (Seagate STT3401A) will return an error.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001968 */
1969 if (drive->dsc_overlap) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001970 bytes_read = idetape_queue_rw_tail(drive,
1971 REQ_IDETAPE_READ, 0,
1972 tape->merge_stage->bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001973 if (bytes_read < 0) {
1974 __idetape_kfree_stage(tape->merge_stage);
1975 tape->merge_stage = NULL;
Borislav Petkov54abf372008-02-06 02:57:52 +01001976 tape->chrdev_dir = IDETAPE_DIR_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001977 return bytes_read;
1978 }
1979 }
1980 }
Borislav Petkov5e69bd92008-04-27 15:38:25 +02001981
Borislav Petkov42d54682008-04-27 15:38:27 +02001982 if (tape->nr_pending_stages >= 3 * max_stages / 4) {
1983 tape->measure_insert_time = 1;
1984 tape->insert_time = jiffies;
1985 tape->insert_size = 0;
1986 tape->insert_speed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001987 }
Borislav Petkov42d54682008-04-27 15:38:27 +02001988
Linus Torvalds1da177e2005-04-16 15:20:36 -07001989 return 0;
1990}
1991
1992/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001993 * Called from idetape_chrdev_read() to service a character device read request
1994 * and add read-ahead requests to our pipeline.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001995 */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001996static int idetape_add_chrdev_read_request(ide_drive_t *drive, int blocks)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001997{
1998 idetape_tape_t *tape = drive->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001999
Borislav Petkov8004a8c2008-02-06 02:57:51 +01002000 debug_log(DBG_PROCS, "Enter %s, %d blocks\n", __func__, blocks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002001
Borislav Petkov3c98bf32008-02-06 02:57:53 +01002002 /* If we are at a filemark, return a read length of 0 */
Borislav Petkov346331f2008-04-18 00:46:26 +02002003 if (test_bit(IDETAPE_FLAG_FILEMARK, &tape->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002004 return 0;
2005
Borislav Petkov8d06bfa2008-02-06 02:57:53 +01002006 idetape_init_read(drive, tape->max_stages);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002007
Borislav Petkov5e69bd92008-04-27 15:38:25 +02002008 return idetape_queue_rw_tail(drive, REQ_IDETAPE_READ, blocks,
2009 tape->merge_stage->bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002010}
2011
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002012static void idetape_pad_zeros(ide_drive_t *drive, int bcount)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002013{
2014 idetape_tape_t *tape = drive->driver_data;
2015 struct idetape_bh *bh;
2016 int blocks;
Borislav Petkov3c98bf32008-02-06 02:57:53 +01002017
Linus Torvalds1da177e2005-04-16 15:20:36 -07002018 while (bcount) {
2019 unsigned int count;
2020
2021 bh = tape->merge_stage->bh;
2022 count = min(tape->stage_size, bcount);
2023 bcount -= count;
Borislav Petkov54bb2072008-02-06 02:57:52 +01002024 blocks = count / tape->blk_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002025 while (count) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002026 atomic_set(&bh->b_count,
2027 min(count, (unsigned int)bh->b_size));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002028 memset(bh->b_data, 0, atomic_read(&bh->b_count));
2029 count -= atomic_read(&bh->b_count);
2030 bh = bh->b_reqnext;
2031 }
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002032 idetape_queue_rw_tail(drive, REQ_IDETAPE_WRITE, blocks,
2033 tape->merge_stage->bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002034 }
2035}
2036
Linus Torvalds1da177e2005-04-16 15:20:36 -07002037/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +01002038 * Rewinds the tape to the Beginning Of the current Partition (BOP). We
2039 * currently support only one partition.
2040 */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002041static int idetape_rewind_tape(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002042{
2043 int retval;
Borislav Petkovd236d742008-04-18 00:46:27 +02002044 struct ide_atapi_pc pc;
Borislav Petkov8004a8c2008-02-06 02:57:51 +01002045 idetape_tape_t *tape;
2046 tape = drive->driver_data;
2047
2048 debug_log(DBG_SENSE, "Enter %s\n", __func__);
2049
Linus Torvalds1da177e2005-04-16 15:20:36 -07002050 idetape_create_rewind_cmd(drive, &pc);
2051 retval = idetape_queue_pc_tail(drive, &pc);
2052 if (retval)
2053 return retval;
2054
2055 idetape_create_read_position_cmd(&pc);
2056 retval = idetape_queue_pc_tail(drive, &pc);
2057 if (retval)
2058 return retval;
2059 return 0;
2060}
2061
Borislav Petkov3c98bf32008-02-06 02:57:53 +01002062/* mtio.h compatible commands should be issued to the chrdev interface. */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002063static int idetape_blkdev_ioctl(ide_drive_t *drive, unsigned int cmd,
2064 unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002065{
2066 idetape_tape_t *tape = drive->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002067 void __user *argp = (void __user *)arg;
2068
Borislav Petkovd59823f2008-02-02 19:56:51 +01002069 struct idetape_config {
2070 int dsc_rw_frequency;
2071 int dsc_media_access_frequency;
2072 int nr_stages;
2073 } config;
2074
Borislav Petkov8004a8c2008-02-06 02:57:51 +01002075 debug_log(DBG_PROCS, "Enter %s\n", __func__);
2076
Linus Torvalds1da177e2005-04-16 15:20:36 -07002077 switch (cmd) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002078 case 0x0340:
2079 if (copy_from_user(&config, argp, sizeof(config)))
2080 return -EFAULT;
2081 tape->best_dsc_rw_freq = config.dsc_rw_frequency;
2082 tape->max_stages = config.nr_stages;
2083 break;
2084 case 0x0350:
2085 config.dsc_rw_frequency = (int) tape->best_dsc_rw_freq;
2086 config.nr_stages = tape->max_stages;
2087 if (copy_to_user(argp, &config, sizeof(config)))
2088 return -EFAULT;
2089 break;
2090 default:
2091 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002092 }
2093 return 0;
2094}
2095
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002096static int idetape_space_over_filemarks(ide_drive_t *drive, short mt_op,
2097 int mt_count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002098{
2099 idetape_tape_t *tape = drive->driver_data;
Borislav Petkovd236d742008-04-18 00:46:27 +02002100 struct ide_atapi_pc pc;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002101 int retval, count = 0;
Borislav Petkovb6422012008-02-02 19:56:49 +01002102 int sprev = !!(tape->caps[4] & 0x20);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002103
2104 if (mt_count == 0)
2105 return 0;
2106 if (MTBSF == mt_op || MTBSFM == mt_op) {
Borislav Petkovb6422012008-02-02 19:56:49 +01002107 if (!sprev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002108 return -EIO;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002109 mt_count = -mt_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002110 }
2111
Borislav Petkov54abf372008-02-06 02:57:52 +01002112 if (tape->chrdev_dir == IDETAPE_DIR_READ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002113 tape->merge_stage_size = 0;
Borislav Petkov346331f2008-04-18 00:46:26 +02002114 if (test_and_clear_bit(IDETAPE_FLAG_FILEMARK, &tape->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002115 ++count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002116 idetape_discard_read_pipeline(drive, 0);
2117 }
2118
2119 /*
Borislav Petkov3c98bf32008-02-06 02:57:53 +01002120 * The filemark was not found in our internal pipeline; now we can issue
2121 * the space command.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002122 */
2123 switch (mt_op) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002124 case MTFSF:
2125 case MTBSF:
2126 idetape_create_space_cmd(&pc, mt_count - count,
2127 IDETAPE_SPACE_OVER_FILEMARK);
2128 return idetape_queue_pc_tail(drive, &pc);
2129 case MTFSFM:
2130 case MTBSFM:
2131 if (!sprev)
2132 return -EIO;
2133 retval = idetape_space_over_filemarks(drive, MTFSF,
2134 mt_count - count);
2135 if (retval)
2136 return retval;
2137 count = (MTBSFM == mt_op ? 1 : -1);
2138 return idetape_space_over_filemarks(drive, MTFSF, count);
2139 default:
2140 printk(KERN_ERR "ide-tape: MTIO operation %d not supported\n",
2141 mt_op);
2142 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002143 }
2144}
2145
Linus Torvalds1da177e2005-04-16 15:20:36 -07002146/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +01002147 * Our character device read / write functions.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002148 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +01002149 * The tape is optimized to maximize throughput when it is transferring an
2150 * integral number of the "continuous transfer limit", which is a parameter of
2151 * the specific tape (26kB on my particular tape, 32kB for Onstream).
Linus Torvalds1da177e2005-04-16 15:20:36 -07002152 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +01002153 * As of version 1.3 of the driver, the character device provides an abstract
2154 * continuous view of the media - any mix of block sizes (even 1 byte) on the
2155 * same backup/restore procedure is supported. The driver will internally
2156 * convert the requests to the recommended transfer unit, so that an unmatch
2157 * between the user's block size to the recommended size will only result in a
2158 * (slightly) increased driver overhead, but will no longer hit performance.
2159 * This is not applicable to Onstream.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002160 */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002161static ssize_t idetape_chrdev_read(struct file *file, char __user *buf,
2162 size_t count, loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002163{
2164 struct ide_tape_obj *tape = ide_tape_f(file);
2165 ide_drive_t *drive = tape->drive;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002166 ssize_t bytes_read, temp, actually_read = 0, rc;
Daniel Walkerdcd96372006-06-25 05:47:37 -07002167 ssize_t ret = 0;
Borislav Petkovb6422012008-02-02 19:56:49 +01002168 u16 ctl = *(u16 *)&tape->caps[12];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002169
Borislav Petkov8004a8c2008-02-06 02:57:51 +01002170 debug_log(DBG_CHRDEV, "Enter %s, count %Zd\n", __func__, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002171
Borislav Petkov54abf372008-02-06 02:57:52 +01002172 if (tape->chrdev_dir != IDETAPE_DIR_READ) {
Borislav Petkov346331f2008-04-18 00:46:26 +02002173 if (test_bit(IDETAPE_FLAG_DETECT_BS, &tape->flags))
Borislav Petkov54bb2072008-02-06 02:57:52 +01002174 if (count > tape->blk_size &&
2175 (count % tape->blk_size) == 0)
2176 tape->user_bs_factor = count / tape->blk_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002177 }
Borislav Petkov8d06bfa2008-02-06 02:57:53 +01002178 rc = idetape_init_read(drive, tape->max_stages);
2179 if (rc < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002180 return rc;
2181 if (count == 0)
2182 return (0);
2183 if (tape->merge_stage_size) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002184 actually_read = min((unsigned int)(tape->merge_stage_size),
2185 (unsigned int)count);
Borislav Petkov99d74e62008-04-27 15:38:25 +02002186 if (idetape_copy_stage_to_user(tape, buf, actually_read))
Daniel Walkerdcd96372006-06-25 05:47:37 -07002187 ret = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002188 buf += actually_read;
2189 tape->merge_stage_size -= actually_read;
2190 count -= actually_read;
2191 }
2192 while (count >= tape->stage_size) {
Borislav Petkovb6422012008-02-02 19:56:49 +01002193 bytes_read = idetape_add_chrdev_read_request(drive, ctl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002194 if (bytes_read <= 0)
2195 goto finish;
Borislav Petkov99d74e62008-04-27 15:38:25 +02002196 if (idetape_copy_stage_to_user(tape, buf, bytes_read))
Daniel Walkerdcd96372006-06-25 05:47:37 -07002197 ret = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002198 buf += bytes_read;
2199 count -= bytes_read;
2200 actually_read += bytes_read;
2201 }
2202 if (count) {
Borislav Petkovb6422012008-02-02 19:56:49 +01002203 bytes_read = idetape_add_chrdev_read_request(drive, ctl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002204 if (bytes_read <= 0)
2205 goto finish;
2206 temp = min((unsigned long)count, (unsigned long)bytes_read);
Borislav Petkov99d74e62008-04-27 15:38:25 +02002207 if (idetape_copy_stage_to_user(tape, buf, temp))
Daniel Walkerdcd96372006-06-25 05:47:37 -07002208 ret = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002209 actually_read += temp;
2210 tape->merge_stage_size = bytes_read-temp;
2211 }
2212finish:
Borislav Petkov346331f2008-04-18 00:46:26 +02002213 if (!actually_read && test_bit(IDETAPE_FLAG_FILEMARK, &tape->flags)) {
Borislav Petkov8004a8c2008-02-06 02:57:51 +01002214 debug_log(DBG_SENSE, "%s: spacing over filemark\n", tape->name);
2215
Linus Torvalds1da177e2005-04-16 15:20:36 -07002216 idetape_space_over_filemarks(drive, MTFSF, 1);
2217 return 0;
2218 }
Daniel Walkerdcd96372006-06-25 05:47:37 -07002219
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002220 return ret ? ret : actually_read;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002221}
2222
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002223static ssize_t idetape_chrdev_write(struct file *file, const char __user *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002224 size_t count, loff_t *ppos)
2225{
2226 struct ide_tape_obj *tape = ide_tape_f(file);
2227 ide_drive_t *drive = tape->drive;
Daniel Walkerdcd96372006-06-25 05:47:37 -07002228 ssize_t actually_written = 0;
2229 ssize_t ret = 0;
Borislav Petkovb6422012008-02-02 19:56:49 +01002230 u16 ctl = *(u16 *)&tape->caps[12];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002231
2232 /* The drive is write protected. */
2233 if (tape->write_prot)
2234 return -EACCES;
2235
Borislav Petkov8004a8c2008-02-06 02:57:51 +01002236 debug_log(DBG_CHRDEV, "Enter %s, count %Zd\n", __func__, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002237
2238 /* Initialize write operation */
Borislav Petkov54abf372008-02-06 02:57:52 +01002239 if (tape->chrdev_dir != IDETAPE_DIR_WRITE) {
2240 if (tape->chrdev_dir == IDETAPE_DIR_READ)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002241 idetape_discard_read_pipeline(drive, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002242 if (tape->merge_stage || tape->merge_stage_size) {
2243 printk(KERN_ERR "ide-tape: merge_stage_size "
2244 "should be 0 now\n");
2245 tape->merge_stage_size = 0;
2246 }
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002247 tape->merge_stage = __idetape_kmalloc_stage(tape, 0, 0);
2248 if (!tape->merge_stage)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002249 return -ENOMEM;
Borislav Petkov54abf372008-02-06 02:57:52 +01002250 tape->chrdev_dir = IDETAPE_DIR_WRITE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002251 idetape_init_merge_stage(tape);
2252
2253 /*
Borislav Petkov3c98bf32008-02-06 02:57:53 +01002254 * Issue a write 0 command to ensure that DSC handshake is
2255 * switched from completion mode to buffer available mode. No
2256 * point in issuing this if DSC overlap isn't supported, some
2257 * drives (Seagate STT3401A) will return an error.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002258 */
2259 if (drive->dsc_overlap) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002260 ssize_t retval = idetape_queue_rw_tail(drive,
2261 REQ_IDETAPE_WRITE, 0,
2262 tape->merge_stage->bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002263 if (retval < 0) {
2264 __idetape_kfree_stage(tape->merge_stage);
2265 tape->merge_stage = NULL;
Borislav Petkov54abf372008-02-06 02:57:52 +01002266 tape->chrdev_dir = IDETAPE_DIR_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002267 return retval;
2268 }
2269 }
2270 }
2271 if (count == 0)
2272 return (0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002273 if (tape->merge_stage_size) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002274 if (tape->merge_stage_size >= tape->stage_size) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002275 printk(KERN_ERR "ide-tape: bug: merge buf too big\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002276 tape->merge_stage_size = 0;
2277 }
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002278 actually_written = min((unsigned int)
2279 (tape->stage_size - tape->merge_stage_size),
2280 (unsigned int)count);
Borislav Petkov8646c882008-04-27 15:38:26 +02002281 if (idetape_copy_stage_from_user(tape, buf, actually_written))
Daniel Walkerdcd96372006-06-25 05:47:37 -07002282 ret = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002283 buf += actually_written;
2284 tape->merge_stage_size += actually_written;
2285 count -= actually_written;
2286
2287 if (tape->merge_stage_size == tape->stage_size) {
Daniel Walkerdcd96372006-06-25 05:47:37 -07002288 ssize_t retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002289 tape->merge_stage_size = 0;
Borislav Petkovb6422012008-02-02 19:56:49 +01002290 retval = idetape_add_chrdev_write_request(drive, ctl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002291 if (retval <= 0)
2292 return (retval);
2293 }
2294 }
2295 while (count >= tape->stage_size) {
Daniel Walkerdcd96372006-06-25 05:47:37 -07002296 ssize_t retval;
Borislav Petkov8646c882008-04-27 15:38:26 +02002297 if (idetape_copy_stage_from_user(tape, buf, tape->stage_size))
Daniel Walkerdcd96372006-06-25 05:47:37 -07002298 ret = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002299 buf += tape->stage_size;
2300 count -= tape->stage_size;
Borislav Petkovb6422012008-02-02 19:56:49 +01002301 retval = idetape_add_chrdev_write_request(drive, ctl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002302 actually_written += tape->stage_size;
2303 if (retval <= 0)
2304 return (retval);
2305 }
2306 if (count) {
2307 actually_written += count;
Borislav Petkov8646c882008-04-27 15:38:26 +02002308 if (idetape_copy_stage_from_user(tape, buf, count))
Daniel Walkerdcd96372006-06-25 05:47:37 -07002309 ret = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002310 tape->merge_stage_size += count;
2311 }
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002312 return ret ? ret : actually_written;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002313}
2314
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002315static int idetape_write_filemark(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002316{
Borislav Petkovd236d742008-04-18 00:46:27 +02002317 struct ide_atapi_pc pc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002318
2319 /* Write a filemark */
2320 idetape_create_write_filemark_cmd(drive, &pc, 1);
2321 if (idetape_queue_pc_tail(drive, &pc)) {
2322 printk(KERN_ERR "ide-tape: Couldn't write a filemark\n");
2323 return -EIO;
2324 }
2325 return 0;
2326}
2327
2328/*
Borislav Petkovd99c9da2008-02-02 19:56:51 +01002329 * Called from idetape_chrdev_ioctl when the general mtio MTIOCTOP ioctl is
2330 * requested.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002331 *
Borislav Petkovd99c9da2008-02-02 19:56:51 +01002332 * Note: MTBSF and MTBSFM are not supported when the tape doesn't support
2333 * spacing over filemarks in the reverse direction. In this case, MTFSFM is also
2334 * usually not supported (it is supported in the rare case in which we crossed
2335 * the filemark during our read-ahead pipelined operation mode).
Linus Torvalds1da177e2005-04-16 15:20:36 -07002336 *
Borislav Petkovd99c9da2008-02-02 19:56:51 +01002337 * The following commands are currently not supported:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002338 *
Borislav Petkovd99c9da2008-02-02 19:56:51 +01002339 * MTFSS, MTBSS, MTWSM, MTSETDENSITY, MTSETDRVBUFFER, MT_ST_BOOLEANS,
2340 * MT_ST_WRITE_THRESHOLD.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002341 */
Borislav Petkovd99c9da2008-02-02 19:56:51 +01002342static int idetape_mtioctop(ide_drive_t *drive, short mt_op, int mt_count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002343{
2344 idetape_tape_t *tape = drive->driver_data;
Borislav Petkovd236d742008-04-18 00:46:27 +02002345 struct ide_atapi_pc pc;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002346 int i, retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002347
Borislav Petkov8004a8c2008-02-06 02:57:51 +01002348 debug_log(DBG_ERR, "Handling MTIOCTOP ioctl: mt_op=%d, mt_count=%d\n",
2349 mt_op, mt_count);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002350
Borislav Petkov3c98bf32008-02-06 02:57:53 +01002351 /* Commands which need our pipelined read-ahead stages. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002352 switch (mt_op) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002353 case MTFSF:
2354 case MTFSFM:
2355 case MTBSF:
2356 case MTBSFM:
2357 if (!mt_count)
2358 return 0;
2359 return idetape_space_over_filemarks(drive, mt_op, mt_count);
2360 default:
2361 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002362 }
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002363
Linus Torvalds1da177e2005-04-16 15:20:36 -07002364 switch (mt_op) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002365 case MTWEOF:
2366 if (tape->write_prot)
2367 return -EACCES;
2368 idetape_discard_read_pipeline(drive, 1);
2369 for (i = 0; i < mt_count; i++) {
2370 retval = idetape_write_filemark(drive);
2371 if (retval)
2372 return retval;
2373 }
2374 return 0;
2375 case MTREW:
2376 idetape_discard_read_pipeline(drive, 0);
2377 if (idetape_rewind_tape(drive))
2378 return -EIO;
2379 return 0;
2380 case MTLOAD:
2381 idetape_discard_read_pipeline(drive, 0);
2382 idetape_create_load_unload_cmd(drive, &pc,
2383 IDETAPE_LU_LOAD_MASK);
2384 return idetape_queue_pc_tail(drive, &pc);
2385 case MTUNLOAD:
2386 case MTOFFL:
2387 /*
2388 * If door is locked, attempt to unlock before
2389 * attempting to eject.
2390 */
2391 if (tape->door_locked) {
2392 if (idetape_create_prevent_cmd(drive, &pc, 0))
2393 if (!idetape_queue_pc_tail(drive, &pc))
2394 tape->door_locked = DOOR_UNLOCKED;
2395 }
2396 idetape_discard_read_pipeline(drive, 0);
2397 idetape_create_load_unload_cmd(drive, &pc,
2398 !IDETAPE_LU_LOAD_MASK);
2399 retval = idetape_queue_pc_tail(drive, &pc);
2400 if (!retval)
Borislav Petkov346331f2008-04-18 00:46:26 +02002401 clear_bit(IDETAPE_FLAG_MEDIUM_PRESENT, &tape->flags);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002402 return retval;
2403 case MTNOP:
2404 idetape_discard_read_pipeline(drive, 0);
2405 return idetape_flush_tape_buffers(drive);
2406 case MTRETEN:
2407 idetape_discard_read_pipeline(drive, 0);
2408 idetape_create_load_unload_cmd(drive, &pc,
2409 IDETAPE_LU_RETENSION_MASK | IDETAPE_LU_LOAD_MASK);
2410 return idetape_queue_pc_tail(drive, &pc);
2411 case MTEOM:
2412 idetape_create_space_cmd(&pc, 0, IDETAPE_SPACE_TO_EOD);
2413 return idetape_queue_pc_tail(drive, &pc);
2414 case MTERASE:
2415 (void)idetape_rewind_tape(drive);
2416 idetape_create_erase_cmd(&pc);
2417 return idetape_queue_pc_tail(drive, &pc);
2418 case MTSETBLK:
2419 if (mt_count) {
2420 if (mt_count < tape->blk_size ||
2421 mt_count % tape->blk_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002422 return -EIO;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002423 tape->user_bs_factor = mt_count / tape->blk_size;
Borislav Petkov346331f2008-04-18 00:46:26 +02002424 clear_bit(IDETAPE_FLAG_DETECT_BS, &tape->flags);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002425 } else
Borislav Petkov346331f2008-04-18 00:46:26 +02002426 set_bit(IDETAPE_FLAG_DETECT_BS, &tape->flags);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002427 return 0;
2428 case MTSEEK:
2429 idetape_discard_read_pipeline(drive, 0);
2430 return idetape_position_tape(drive,
2431 mt_count * tape->user_bs_factor, tape->partition, 0);
2432 case MTSETPART:
2433 idetape_discard_read_pipeline(drive, 0);
2434 return idetape_position_tape(drive, 0, mt_count, 0);
2435 case MTFSR:
2436 case MTBSR:
2437 case MTLOCK:
2438 if (!idetape_create_prevent_cmd(drive, &pc, 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002439 return 0;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002440 retval = idetape_queue_pc_tail(drive, &pc);
2441 if (retval)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002442 return retval;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002443 tape->door_locked = DOOR_EXPLICITLY_LOCKED;
2444 return 0;
2445 case MTUNLOCK:
2446 if (!idetape_create_prevent_cmd(drive, &pc, 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002447 return 0;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002448 retval = idetape_queue_pc_tail(drive, &pc);
2449 if (retval)
2450 return retval;
2451 tape->door_locked = DOOR_UNLOCKED;
2452 return 0;
2453 default:
2454 printk(KERN_ERR "ide-tape: MTIO operation %d not supported\n",
2455 mt_op);
2456 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002457 }
2458}
2459
2460/*
Borislav Petkovd99c9da2008-02-02 19:56:51 +01002461 * Our character device ioctls. General mtio.h magnetic io commands are
2462 * supported here, and not in the corresponding block interface. Our own
2463 * ide-tape ioctls are supported on both interfaces.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002464 */
Borislav Petkovd99c9da2008-02-02 19:56:51 +01002465static int idetape_chrdev_ioctl(struct inode *inode, struct file *file,
2466 unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002467{
2468 struct ide_tape_obj *tape = ide_tape_f(file);
2469 ide_drive_t *drive = tape->drive;
2470 struct mtop mtop;
2471 struct mtget mtget;
2472 struct mtpos mtpos;
Borislav Petkov54bb2072008-02-06 02:57:52 +01002473 int block_offset = 0, position = tape->first_frame;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002474 void __user *argp = (void __user *)arg;
2475
Borislav Petkov8004a8c2008-02-06 02:57:51 +01002476 debug_log(DBG_CHRDEV, "Enter %s, cmd=%u\n", __func__, cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002477
Borislav Petkov54abf372008-02-06 02:57:52 +01002478 if (tape->chrdev_dir == IDETAPE_DIR_WRITE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002479 idetape_empty_write_pipeline(drive);
2480 idetape_flush_tape_buffers(drive);
2481 }
2482 if (cmd == MTIOCGET || cmd == MTIOCPOS) {
Borislav Petkovb361acb2008-04-27 15:38:26 +02002483 block_offset = tape->merge_stage_size /
Borislav Petkov54bb2072008-02-06 02:57:52 +01002484 (tape->blk_size * tape->user_bs_factor);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002485 position = idetape_read_position(drive);
2486 if (position < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002487 return -EIO;
2488 }
2489 switch (cmd) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002490 case MTIOCTOP:
2491 if (copy_from_user(&mtop, argp, sizeof(struct mtop)))
2492 return -EFAULT;
2493 return idetape_mtioctop(drive, mtop.mt_op, mtop.mt_count);
2494 case MTIOCGET:
2495 memset(&mtget, 0, sizeof(struct mtget));
2496 mtget.mt_type = MT_ISSCSI2;
2497 mtget.mt_blkno = position / tape->user_bs_factor - block_offset;
2498 mtget.mt_dsreg =
2499 ((tape->blk_size * tape->user_bs_factor)
2500 << MT_ST_BLKSIZE_SHIFT) & MT_ST_BLKSIZE_MASK;
Borislav Petkov54bb2072008-02-06 02:57:52 +01002501
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002502 if (tape->drv_write_prot)
2503 mtget.mt_gstat |= GMT_WR_PROT(0xffffffff);
2504
2505 if (copy_to_user(argp, &mtget, sizeof(struct mtget)))
2506 return -EFAULT;
2507 return 0;
2508 case MTIOCPOS:
2509 mtpos.mt_blkno = position / tape->user_bs_factor - block_offset;
2510 if (copy_to_user(argp, &mtpos, sizeof(struct mtpos)))
2511 return -EFAULT;
2512 return 0;
2513 default:
2514 if (tape->chrdev_dir == IDETAPE_DIR_READ)
2515 idetape_discard_read_pipeline(drive, 1);
2516 return idetape_blkdev_ioctl(drive, cmd, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002517 }
2518}
2519
Borislav Petkov3cffb9c2008-02-02 19:56:50 +01002520/*
2521 * Do a mode sense page 0 with block descriptor and if it succeeds set the tape
2522 * block size with the reported value.
2523 */
2524static void ide_tape_get_bsize_from_bdesc(ide_drive_t *drive)
2525{
2526 idetape_tape_t *tape = drive->driver_data;
Borislav Petkovd236d742008-04-18 00:46:27 +02002527 struct ide_atapi_pc pc;
Borislav Petkov3cffb9c2008-02-02 19:56:50 +01002528
2529 idetape_create_mode_sense_cmd(&pc, IDETAPE_BLOCK_DESCRIPTOR);
2530 if (idetape_queue_pc_tail(drive, &pc)) {
2531 printk(KERN_ERR "ide-tape: Can't get block descriptor\n");
Borislav Petkov54bb2072008-02-06 02:57:52 +01002532 if (tape->blk_size == 0) {
Borislav Petkov3cffb9c2008-02-02 19:56:50 +01002533 printk(KERN_WARNING "ide-tape: Cannot deal with zero "
2534 "block size, assuming 32k\n");
Borislav Petkov54bb2072008-02-06 02:57:52 +01002535 tape->blk_size = 32768;
Borislav Petkov3cffb9c2008-02-02 19:56:50 +01002536 }
2537 return;
2538 }
Borislav Petkovd236d742008-04-18 00:46:27 +02002539 tape->blk_size = (pc.buf[4 + 5] << 16) +
2540 (pc.buf[4 + 6] << 8) +
2541 pc.buf[4 + 7];
2542 tape->drv_write_prot = (pc.buf[2] & 0x80) >> 7;
Borislav Petkov3cffb9c2008-02-02 19:56:50 +01002543}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002544
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002545static int idetape_chrdev_open(struct inode *inode, struct file *filp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002546{
2547 unsigned int minor = iminor(inode), i = minor & ~0xc0;
2548 ide_drive_t *drive;
2549 idetape_tape_t *tape;
Borislav Petkovd236d742008-04-18 00:46:27 +02002550 struct ide_atapi_pc pc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002551 int retval;
2552
Borislav Petkov8004a8c2008-02-06 02:57:51 +01002553 if (i >= MAX_HWIFS * MAX_DRIVES)
2554 return -ENXIO;
2555
2556 tape = ide_tape_chrdev_get(i);
2557 if (!tape)
2558 return -ENXIO;
2559
2560 debug_log(DBG_CHRDEV, "Enter %s\n", __func__);
2561
Linus Torvalds1da177e2005-04-16 15:20:36 -07002562 /*
2563 * We really want to do nonseekable_open(inode, filp); here, but some
2564 * versions of tar incorrectly call lseek on tapes and bail out if that
2565 * fails. So we disallow pread() and pwrite(), but permit lseeks.
2566 */
2567 filp->f_mode &= ~(FMODE_PREAD | FMODE_PWRITE);
2568
Linus Torvalds1da177e2005-04-16 15:20:36 -07002569 drive = tape->drive;
2570
2571 filp->private_data = tape;
2572
Borislav Petkov346331f2008-04-18 00:46:26 +02002573 if (test_and_set_bit(IDETAPE_FLAG_BUSY, &tape->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002574 retval = -EBUSY;
2575 goto out_put_tape;
2576 }
2577
2578 retval = idetape_wait_ready(drive, 60 * HZ);
2579 if (retval) {
Borislav Petkov346331f2008-04-18 00:46:26 +02002580 clear_bit(IDETAPE_FLAG_BUSY, &tape->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002581 printk(KERN_ERR "ide-tape: %s: drive not ready\n", tape->name);
2582 goto out_put_tape;
2583 }
2584
2585 idetape_read_position(drive);
Borislav Petkov346331f2008-04-18 00:46:26 +02002586 if (!test_bit(IDETAPE_FLAG_ADDRESS_VALID, &tape->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002587 (void)idetape_rewind_tape(drive);
2588
Linus Torvalds1da177e2005-04-16 15:20:36 -07002589 /* Read block size and write protect status from drive. */
Borislav Petkov3cffb9c2008-02-02 19:56:50 +01002590 ide_tape_get_bsize_from_bdesc(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002591
2592 /* Set write protect flag if device is opened as read-only. */
2593 if ((filp->f_flags & O_ACCMODE) == O_RDONLY)
2594 tape->write_prot = 1;
2595 else
2596 tape->write_prot = tape->drv_write_prot;
2597
2598 /* Make sure drive isn't write protected if user wants to write. */
2599 if (tape->write_prot) {
2600 if ((filp->f_flags & O_ACCMODE) == O_WRONLY ||
2601 (filp->f_flags & O_ACCMODE) == O_RDWR) {
Borislav Petkov346331f2008-04-18 00:46:26 +02002602 clear_bit(IDETAPE_FLAG_BUSY, &tape->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002603 retval = -EROFS;
2604 goto out_put_tape;
2605 }
2606 }
2607
Borislav Petkov3c98bf32008-02-06 02:57:53 +01002608 /* Lock the tape drive door so user can't eject. */
Borislav Petkov54abf372008-02-06 02:57:52 +01002609 if (tape->chrdev_dir == IDETAPE_DIR_NONE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002610 if (idetape_create_prevent_cmd(drive, &pc, 1)) {
2611 if (!idetape_queue_pc_tail(drive, &pc)) {
2612 if (tape->door_locked != DOOR_EXPLICITLY_LOCKED)
2613 tape->door_locked = DOOR_LOCKED;
2614 }
2615 }
2616 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002617 return 0;
2618
2619out_put_tape:
2620 ide_tape_put(tape);
2621 return retval;
2622}
2623
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002624static void idetape_write_release(ide_drive_t *drive, unsigned int minor)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002625{
2626 idetape_tape_t *tape = drive->driver_data;
2627
2628 idetape_empty_write_pipeline(drive);
2629 tape->merge_stage = __idetape_kmalloc_stage(tape, 1, 0);
2630 if (tape->merge_stage != NULL) {
Borislav Petkov54bb2072008-02-06 02:57:52 +01002631 idetape_pad_zeros(drive, tape->blk_size *
2632 (tape->user_bs_factor - 1));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002633 __idetape_kfree_stage(tape->merge_stage);
2634 tape->merge_stage = NULL;
2635 }
2636 idetape_write_filemark(drive);
2637 idetape_flush_tape_buffers(drive);
2638 idetape_flush_tape_buffers(drive);
2639}
2640
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002641static int idetape_chrdev_release(struct inode *inode, struct file *filp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002642{
2643 struct ide_tape_obj *tape = ide_tape_f(filp);
2644 ide_drive_t *drive = tape->drive;
Borislav Petkovd236d742008-04-18 00:46:27 +02002645 struct ide_atapi_pc pc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002646 unsigned int minor = iminor(inode);
2647
2648 lock_kernel();
2649 tape = drive->driver_data;
Borislav Petkov8004a8c2008-02-06 02:57:51 +01002650
2651 debug_log(DBG_CHRDEV, "Enter %s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002652
Borislav Petkov54abf372008-02-06 02:57:52 +01002653 if (tape->chrdev_dir == IDETAPE_DIR_WRITE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002654 idetape_write_release(drive, minor);
Borislav Petkov54abf372008-02-06 02:57:52 +01002655 if (tape->chrdev_dir == IDETAPE_DIR_READ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002656 if (minor < 128)
2657 idetape_discard_read_pipeline(drive, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002658 }
Borislav Petkovf64eee72008-04-27 15:38:25 +02002659
Borislav Petkov346331f2008-04-18 00:46:26 +02002660 if (minor < 128 && test_bit(IDETAPE_FLAG_MEDIUM_PRESENT, &tape->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002661 (void) idetape_rewind_tape(drive);
Borislav Petkov54abf372008-02-06 02:57:52 +01002662 if (tape->chrdev_dir == IDETAPE_DIR_NONE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002663 if (tape->door_locked == DOOR_LOCKED) {
2664 if (idetape_create_prevent_cmd(drive, &pc, 0)) {
2665 if (!idetape_queue_pc_tail(drive, &pc))
2666 tape->door_locked = DOOR_UNLOCKED;
2667 }
2668 }
2669 }
Borislav Petkov346331f2008-04-18 00:46:26 +02002670 clear_bit(IDETAPE_FLAG_BUSY, &tape->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002671 ide_tape_put(tape);
2672 unlock_kernel();
2673 return 0;
2674}
2675
2676/*
Borislav Petkov71071b82008-02-06 02:57:53 +01002677 * check the contents of the ATAPI IDENTIFY command results. We return:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002678 *
Borislav Petkov71071b82008-02-06 02:57:53 +01002679 * 1 - If the tape can be supported by us, based on the information we have so
2680 * far.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002681 *
Borislav Petkov71071b82008-02-06 02:57:53 +01002682 * 0 - If this tape driver is not currently supported by us.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002683 */
Borislav Petkov71071b82008-02-06 02:57:53 +01002684static int idetape_identify_device(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002685{
Borislav Petkov71071b82008-02-06 02:57:53 +01002686 u8 gcw[2], protocol, device_type, removable, packet_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002687
2688 if (drive->id_read == 0)
2689 return 1;
2690
Borislav Petkov71071b82008-02-06 02:57:53 +01002691 *((unsigned short *) &gcw) = drive->id->config;
2692
2693 protocol = (gcw[1] & 0xC0) >> 6;
2694 device_type = gcw[1] & 0x1F;
2695 removable = !!(gcw[0] & 0x80);
2696 packet_size = gcw[0] & 0x3;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002697
Linus Torvalds1da177e2005-04-16 15:20:36 -07002698 /* Check that we can support this device */
Borislav Petkov71071b82008-02-06 02:57:53 +01002699 if (protocol != 2)
Bartlomiej Zolnierkiewicz16422de32008-02-02 19:56:48 +01002700 printk(KERN_ERR "ide-tape: Protocol (0x%02x) is not ATAPI\n",
Borislav Petkov71071b82008-02-06 02:57:53 +01002701 protocol);
2702 else if (device_type != 1)
Bartlomiej Zolnierkiewicz16422de32008-02-02 19:56:48 +01002703 printk(KERN_ERR "ide-tape: Device type (0x%02x) is not set "
Borislav Petkov71071b82008-02-06 02:57:53 +01002704 "to tape\n", device_type);
2705 else if (!removable)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002706 printk(KERN_ERR "ide-tape: The removable flag is not set\n");
Borislav Petkov71071b82008-02-06 02:57:53 +01002707 else if (packet_size != 0) {
Borislav Petkov24d57f82008-02-06 02:57:54 +01002708 printk(KERN_ERR "ide-tape: Packet size (0x%02x) is not 12"
2709 " bytes\n", packet_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002710 } else
2711 return 1;
2712 return 0;
2713}
2714
Borislav Petkov6d29c8f2008-02-02 19:56:49 +01002715static void idetape_get_inquiry_results(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002716{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002717 idetape_tape_t *tape = drive->driver_data;
Borislav Petkovd236d742008-04-18 00:46:27 +02002718 struct ide_atapi_pc pc;
Borislav Petkov41f81d542008-02-06 02:57:52 +01002719 char fw_rev[6], vendor_id[10], product_id[18];
Borislav Petkov6d29c8f2008-02-02 19:56:49 +01002720
Linus Torvalds1da177e2005-04-16 15:20:36 -07002721 idetape_create_inquiry_cmd(&pc);
2722 if (idetape_queue_pc_tail(drive, &pc)) {
Borislav Petkov6d29c8f2008-02-02 19:56:49 +01002723 printk(KERN_ERR "ide-tape: %s: can't get INQUIRY results\n",
2724 tape->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002725 return;
2726 }
Borislav Petkovd236d742008-04-18 00:46:27 +02002727 memcpy(vendor_id, &pc.buf[8], 8);
2728 memcpy(product_id, &pc.buf[16], 16);
2729 memcpy(fw_rev, &pc.buf[32], 4);
Borislav Petkov6d29c8f2008-02-02 19:56:49 +01002730
Borislav Petkov41f81d542008-02-06 02:57:52 +01002731 ide_fixstring(vendor_id, 10, 0);
2732 ide_fixstring(product_id, 18, 0);
2733 ide_fixstring(fw_rev, 6, 0);
2734
Borislav Petkov6d29c8f2008-02-02 19:56:49 +01002735 printk(KERN_INFO "ide-tape: %s <-> %s: %s %s rev %s\n",
Borislav Petkov41f81d542008-02-06 02:57:52 +01002736 drive->name, tape->name, vendor_id, product_id, fw_rev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002737}
2738
2739/*
Borislav Petkovb6422012008-02-02 19:56:49 +01002740 * Ask the tape about its various parameters. In particular, we will adjust our
2741 * data transfer buffer size to the recommended value as returned by the tape.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002742 */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002743static void idetape_get_mode_sense_results(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002744{
2745 idetape_tape_t *tape = drive->driver_data;
Borislav Petkovd236d742008-04-18 00:46:27 +02002746 struct ide_atapi_pc pc;
Borislav Petkovb6422012008-02-02 19:56:49 +01002747 u8 *caps;
2748 u8 speed, max_speed;
Borislav Petkov47314fa2008-02-02 19:56:48 +01002749
Linus Torvalds1da177e2005-04-16 15:20:36 -07002750 idetape_create_mode_sense_cmd(&pc, IDETAPE_CAPABILITIES_PAGE);
2751 if (idetape_queue_pc_tail(drive, &pc)) {
Borislav Petkovb6422012008-02-02 19:56:49 +01002752 printk(KERN_ERR "ide-tape: Can't get tape parameters - assuming"
2753 " some default values\n");
Borislav Petkov54bb2072008-02-06 02:57:52 +01002754 tape->blk_size = 512;
Borislav Petkovb6422012008-02-02 19:56:49 +01002755 put_unaligned(52, (u16 *)&tape->caps[12]);
2756 put_unaligned(540, (u16 *)&tape->caps[14]);
2757 put_unaligned(6*52, (u16 *)&tape->caps[16]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002758 return;
2759 }
Borislav Petkovd236d742008-04-18 00:46:27 +02002760 caps = pc.buf + 4 + pc.buf[3];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002761
Borislav Petkovb6422012008-02-02 19:56:49 +01002762 /* convert to host order and save for later use */
2763 speed = be16_to_cpu(*(u16 *)&caps[14]);
2764 max_speed = be16_to_cpu(*(u16 *)&caps[8]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002765
Borislav Petkovb6422012008-02-02 19:56:49 +01002766 put_unaligned(max_speed, (u16 *)&caps[8]);
2767 put_unaligned(be16_to_cpu(*(u16 *)&caps[12]), (u16 *)&caps[12]);
2768 put_unaligned(speed, (u16 *)&caps[14]);
2769 put_unaligned(be16_to_cpu(*(u16 *)&caps[16]), (u16 *)&caps[16]);
2770
2771 if (!speed) {
2772 printk(KERN_INFO "ide-tape: %s: invalid tape speed "
2773 "(assuming 650KB/sec)\n", drive->name);
2774 put_unaligned(650, (u16 *)&caps[14]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002775 }
Borislav Petkovb6422012008-02-02 19:56:49 +01002776 if (!max_speed) {
2777 printk(KERN_INFO "ide-tape: %s: invalid max_speed "
2778 "(assuming 650KB/sec)\n", drive->name);
2779 put_unaligned(650, (u16 *)&caps[8]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002780 }
2781
Borislav Petkovb6422012008-02-02 19:56:49 +01002782 memcpy(&tape->caps, caps, 20);
2783 if (caps[7] & 0x02)
Borislav Petkov54bb2072008-02-06 02:57:52 +01002784 tape->blk_size = 512;
Borislav Petkovb6422012008-02-02 19:56:49 +01002785 else if (caps[7] & 0x04)
Borislav Petkov54bb2072008-02-06 02:57:52 +01002786 tape->blk_size = 1024;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002787}
2788
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +02002789#ifdef CONFIG_IDE_PROC_FS
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002790static void idetape_add_settings(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002791{
2792 idetape_tape_t *tape = drive->driver_data;
2793
Borislav Petkovb6422012008-02-02 19:56:49 +01002794 ide_add_setting(drive, "buffer", SETTING_READ, TYPE_SHORT, 0, 0xffff,
2795 1, 2, (u16 *)&tape->caps[16], NULL);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002796 ide_add_setting(drive, "pipeline_min", SETTING_RW, TYPE_INT, 1, 0xffff,
2797 tape->stage_size / 1024, 1, &tape->min_pipeline, NULL);
2798 ide_add_setting(drive, "pipeline", SETTING_RW, TYPE_INT, 1, 0xffff,
2799 tape->stage_size / 1024, 1, &tape->max_stages, NULL);
2800 ide_add_setting(drive, "pipeline_max", SETTING_RW, TYPE_INT, 1, 0xffff,
2801 tape->stage_size / 1024, 1, &tape->max_pipeline, NULL);
2802 ide_add_setting(drive, "pipeline_used", SETTING_READ, TYPE_INT, 0,
2803 0xffff, tape->stage_size / 1024, 1, &tape->nr_stages,
2804 NULL);
2805 ide_add_setting(drive, "pipeline_pending", SETTING_READ, TYPE_INT, 0,
2806 0xffff, tape->stage_size / 1024, 1,
2807 &tape->nr_pending_stages, NULL);
Borislav Petkovb6422012008-02-02 19:56:49 +01002808 ide_add_setting(drive, "speed", SETTING_READ, TYPE_SHORT, 0, 0xffff,
2809 1, 1, (u16 *)&tape->caps[14], NULL);
Borislav Petkov54bb2072008-02-06 02:57:52 +01002810 ide_add_setting(drive, "stage", SETTING_READ, TYPE_INT, 0, 0xffff, 1,
2811 1024, &tape->stage_size, NULL);
2812 ide_add_setting(drive, "tdsc", SETTING_RW, TYPE_INT, IDETAPE_DSC_RW_MIN,
2813 IDETAPE_DSC_RW_MAX, 1000, HZ, &tape->best_dsc_rw_freq,
2814 NULL);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002815 ide_add_setting(drive, "dsc_overlap", SETTING_RW, TYPE_BYTE, 0, 1, 1,
2816 1, &drive->dsc_overlap, NULL);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002817 ide_add_setting(drive, "avg_speed", SETTING_READ, TYPE_INT, 0, 0xffff,
2818 1, 1, &tape->avg_speed, NULL);
Borislav Petkov8004a8c2008-02-06 02:57:51 +01002819 ide_add_setting(drive, "debug_mask", SETTING_RW, TYPE_INT, 0, 0xffff, 1,
2820 1, &tape->debug_mask, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002821}
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +02002822#else
2823static inline void idetape_add_settings(ide_drive_t *drive) { ; }
2824#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002825
2826/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +01002827 * The function below is called to:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002828 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +01002829 * 1. Initialize our various state variables.
2830 * 2. Ask the tape for its capabilities.
2831 * 3. Allocate a buffer which will be used for data transfer. The buffer size
2832 * is chosen based on the recommendation which we received in step 2.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002833 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +01002834 * Note that at this point ide.c already assigned us an irq, so that we can
2835 * queue requests here and wait for their completion.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002836 */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002837static void idetape_setup(ide_drive_t *drive, idetape_tape_t *tape, int minor)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002838{
2839 unsigned long t1, tmid, tn, t;
2840 int speed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002841 int stage_size;
Borislav Petkov71071b82008-02-06 02:57:53 +01002842 u8 gcw[2];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002843 struct sysinfo si;
Borislav Petkovb6422012008-02-02 19:56:49 +01002844 u16 *ctl = (u16 *)&tape->caps[12];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002845
Borislav Petkov54bb2072008-02-06 02:57:52 +01002846 spin_lock_init(&tape->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002847 drive->dsc_overlap = 1;
Bartlomiej Zolnierkiewicz4166c192008-02-01 23:09:30 +01002848 if (drive->hwif->host_flags & IDE_HFLAG_NO_DSC) {
2849 printk(KERN_INFO "ide-tape: %s: disabling DSC overlap\n",
2850 tape->name);
2851 drive->dsc_overlap = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002852 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002853 /* Seagate Travan drives do not support DSC overlap. */
2854 if (strstr(drive->id->model, "Seagate STT3401"))
2855 drive->dsc_overlap = 0;
2856 tape->minor = minor;
2857 tape->name[0] = 'h';
2858 tape->name[1] = 't';
2859 tape->name[2] = '0' + minor;
Borislav Petkov54abf372008-02-06 02:57:52 +01002860 tape->chrdev_dir = IDETAPE_DIR_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002861 tape->pc = tape->pc_stack;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002862 *((unsigned short *) &gcw) = drive->id->config;
Borislav Petkov71071b82008-02-06 02:57:53 +01002863
2864 /* Command packet DRQ type */
2865 if (((gcw[0] & 0x60) >> 5) == 1)
Borislav Petkov346331f2008-04-18 00:46:26 +02002866 set_bit(IDETAPE_FLAG_DRQ_INTERRUPT, &tape->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002867
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002868 tape->min_pipeline = 10;
2869 tape->max_pipeline = 10;
2870 tape->max_stages = 10;
Borislav Petkov3c98bf32008-02-06 02:57:53 +01002871
Linus Torvalds1da177e2005-04-16 15:20:36 -07002872 idetape_get_inquiry_results(drive);
2873 idetape_get_mode_sense_results(drive);
Borislav Petkov3cffb9c2008-02-02 19:56:50 +01002874 ide_tape_get_bsize_from_bdesc(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002875 tape->user_bs_factor = 1;
Borislav Petkov54bb2072008-02-06 02:57:52 +01002876 tape->stage_size = *ctl * tape->blk_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002877 while (tape->stage_size > 0xffff) {
2878 printk(KERN_NOTICE "ide-tape: decreasing stage size\n");
Borislav Petkovb6422012008-02-02 19:56:49 +01002879 *ctl /= 2;
Borislav Petkov54bb2072008-02-06 02:57:52 +01002880 tape->stage_size = *ctl * tape->blk_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002881 }
2882 stage_size = tape->stage_size;
2883 tape->pages_per_stage = stage_size / PAGE_SIZE;
2884 if (stage_size % PAGE_SIZE) {
2885 tape->pages_per_stage++;
2886 tape->excess_bh_size = PAGE_SIZE - stage_size % PAGE_SIZE;
2887 }
2888
Borislav Petkovb6422012008-02-02 19:56:49 +01002889 /* Select the "best" DSC read/write polling freq and pipeline size. */
2890 speed = max(*(u16 *)&tape->caps[14], *(u16 *)&tape->caps[8]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002891
2892 tape->max_stages = speed * 1000 * 10 / tape->stage_size;
2893
Borislav Petkov3c98bf32008-02-06 02:57:53 +01002894 /* Limit memory use for pipeline to 10% of physical memory */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002895 si_meminfo(&si);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002896 if (tape->max_stages * tape->stage_size >
2897 si.totalram * si.mem_unit / 10)
2898 tape->max_stages =
2899 si.totalram * si.mem_unit / (10 * tape->stage_size);
2900
Linus Torvalds1da177e2005-04-16 15:20:36 -07002901 tape->max_stages = min(tape->max_stages, IDETAPE_MAX_PIPELINE_STAGES);
2902 tape->min_pipeline = min(tape->max_stages, IDETAPE_MIN_PIPELINE_STAGES);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002903 tape->max_pipeline =
2904 min(tape->max_stages * 2, IDETAPE_MAX_PIPELINE_STAGES);
2905 if (tape->max_stages == 0) {
2906 tape->max_stages = 1;
2907 tape->min_pipeline = 1;
2908 tape->max_pipeline = 1;
2909 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002910
2911 t1 = (tape->stage_size * HZ) / (speed * 1000);
Borislav Petkovb6422012008-02-02 19:56:49 +01002912 tmid = (*(u16 *)&tape->caps[16] * 32 * HZ) / (speed * 125);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002913 tn = (IDETAPE_FIFO_THRESHOLD * tape->stage_size * HZ) / (speed * 1000);
2914
2915 if (tape->max_stages)
2916 t = tn;
2917 else
2918 t = t1;
2919
2920 /*
Borislav Petkov3c98bf32008-02-06 02:57:53 +01002921 * Ensure that the number we got makes sense; limit it within
2922 * IDETAPE_DSC_RW_MIN and IDETAPE_DSC_RW_MAX.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002923 */
Borislav Petkov54bb2072008-02-06 02:57:52 +01002924 tape->best_dsc_rw_freq = max_t(unsigned long,
2925 min_t(unsigned long, t, IDETAPE_DSC_RW_MAX),
2926 IDETAPE_DSC_RW_MIN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002927 printk(KERN_INFO "ide-tape: %s <-> %s: %dKBps, %d*%dkB buffer, "
2928 "%dkB pipeline, %lums tDSC%s\n",
Borislav Petkovb6422012008-02-02 19:56:49 +01002929 drive->name, tape->name, *(u16 *)&tape->caps[14],
2930 (*(u16 *)&tape->caps[16] * 512) / tape->stage_size,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002931 tape->stage_size / 1024,
2932 tape->max_stages * tape->stage_size / 1024,
Borislav Petkov54bb2072008-02-06 02:57:52 +01002933 tape->best_dsc_rw_freq * 1000 / HZ,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002934 drive->using_dma ? ", DMA":"");
2935
2936 idetape_add_settings(drive);
2937}
2938
Russell King4031bbe2006-01-06 11:41:00 +00002939static void ide_tape_remove(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002940{
2941 idetape_tape_t *tape = drive->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002942
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +02002943 ide_proc_unregister_driver(drive, tape->driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002944
2945 ide_unregister_region(tape->disk);
2946
2947 ide_tape_put(tape);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002948}
2949
2950static void ide_tape_release(struct kref *kref)
2951{
2952 struct ide_tape_obj *tape = to_ide_tape(kref);
2953 ide_drive_t *drive = tape->drive;
2954 struct gendisk *g = tape->disk;
2955
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02002956 BUG_ON(tape->first_stage != NULL || tape->merge_stage_size);
2957
Linus Torvalds1da177e2005-04-16 15:20:36 -07002958 drive->dsc_overlap = 0;
2959 drive->driver_data = NULL;
Tony Jonesdbc12722007-09-25 02:03:03 +02002960 device_destroy(idetape_sysfs_class, MKDEV(IDETAPE_MAJOR, tape->minor));
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002961 device_destroy(idetape_sysfs_class,
2962 MKDEV(IDETAPE_MAJOR, tape->minor + 128));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002963 idetape_devs[tape->minor] = NULL;
2964 g->private_data = NULL;
2965 put_disk(g);
2966 kfree(tape);
2967}
2968
Bartlomiej Zolnierkiewiczecfd80e2007-05-10 00:01:09 +02002969#ifdef CONFIG_IDE_PROC_FS
Linus Torvalds1da177e2005-04-16 15:20:36 -07002970static int proc_idetape_read_name
2971 (char *page, char **start, off_t off, int count, int *eof, void *data)
2972{
2973 ide_drive_t *drive = (ide_drive_t *) data;
2974 idetape_tape_t *tape = drive->driver_data;
2975 char *out = page;
2976 int len;
2977
2978 len = sprintf(out, "%s\n", tape->name);
2979 PROC_IDE_READ_RETURN(page, start, off, count, eof, len);
2980}
2981
2982static ide_proc_entry_t idetape_proc[] = {
2983 { "capacity", S_IFREG|S_IRUGO, proc_ide_read_capacity, NULL },
2984 { "name", S_IFREG|S_IRUGO, proc_idetape_read_name, NULL },
2985 { NULL, 0, NULL, NULL }
2986};
Linus Torvalds1da177e2005-04-16 15:20:36 -07002987#endif
2988
Russell King4031bbe2006-01-06 11:41:00 +00002989static int ide_tape_probe(ide_drive_t *);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002990
Linus Torvalds1da177e2005-04-16 15:20:36 -07002991static ide_driver_t idetape_driver = {
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02002992 .gen_driver = {
Laurent Riffard4ef3b8f2005-11-18 22:15:40 +01002993 .owner = THIS_MODULE,
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02002994 .name = "ide-tape",
2995 .bus = &ide_bus_type,
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02002996 },
Russell King4031bbe2006-01-06 11:41:00 +00002997 .probe = ide_tape_probe,
2998 .remove = ide_tape_remove,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002999 .version = IDETAPE_VERSION,
3000 .media = ide_tape,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003001 .supports_dsc_overlap = 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003002 .do_request = idetape_do_request,
3003 .end_request = idetape_end_request,
3004 .error = __ide_error,
3005 .abort = __ide_abort,
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +02003006#ifdef CONFIG_IDE_PROC_FS
Linus Torvalds1da177e2005-04-16 15:20:36 -07003007 .proc = idetape_proc,
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +02003008#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003009};
3010
Borislav Petkov3c98bf32008-02-06 02:57:53 +01003011/* Our character device supporting functions, passed to register_chrdev. */
Arjan van de Ven2b8693c2007-02-12 00:55:32 -08003012static const struct file_operations idetape_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003013 .owner = THIS_MODULE,
3014 .read = idetape_chrdev_read,
3015 .write = idetape_chrdev_write,
3016 .ioctl = idetape_chrdev_ioctl,
3017 .open = idetape_chrdev_open,
3018 .release = idetape_chrdev_release,
3019};
3020
3021static int idetape_open(struct inode *inode, struct file *filp)
3022{
3023 struct gendisk *disk = inode->i_bdev->bd_disk;
3024 struct ide_tape_obj *tape;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003025
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01003026 tape = ide_tape_get(disk);
3027 if (!tape)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003028 return -ENXIO;
3029
Linus Torvalds1da177e2005-04-16 15:20:36 -07003030 return 0;
3031}
3032
3033static int idetape_release(struct inode *inode, struct file *filp)
3034{
3035 struct gendisk *disk = inode->i_bdev->bd_disk;
3036 struct ide_tape_obj *tape = ide_tape_g(disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003037
3038 ide_tape_put(tape);
3039
3040 return 0;
3041}
3042
3043static int idetape_ioctl(struct inode *inode, struct file *file,
3044 unsigned int cmd, unsigned long arg)
3045{
3046 struct block_device *bdev = inode->i_bdev;
3047 struct ide_tape_obj *tape = ide_tape_g(bdev->bd_disk);
3048 ide_drive_t *drive = tape->drive;
3049 int err = generic_ide_ioctl(drive, file, bdev, cmd, arg);
3050 if (err == -EINVAL)
3051 err = idetape_blkdev_ioctl(drive, cmd, arg);
3052 return err;
3053}
3054
3055static struct block_device_operations idetape_block_ops = {
3056 .owner = THIS_MODULE,
3057 .open = idetape_open,
3058 .release = idetape_release,
3059 .ioctl = idetape_ioctl,
3060};
3061
Russell King4031bbe2006-01-06 11:41:00 +00003062static int ide_tape_probe(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003063{
3064 idetape_tape_t *tape;
3065 struct gendisk *g;
3066 int minor;
3067
3068 if (!strstr("ide-tape", drive->driver_req))
3069 goto failed;
3070 if (!drive->present)
3071 goto failed;
3072 if (drive->media != ide_tape)
3073 goto failed;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01003074 if (!idetape_identify_device(drive)) {
3075 printk(KERN_ERR "ide-tape: %s: not supported by this version of"
3076 " the driver\n", drive->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003077 goto failed;
3078 }
3079 if (drive->scsi) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01003080 printk(KERN_INFO "ide-tape: passing drive %s to ide-scsi"
3081 " emulation.\n", drive->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003082 goto failed;
3083 }
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01003084 tape = kzalloc(sizeof(idetape_tape_t), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003085 if (tape == NULL) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01003086 printk(KERN_ERR "ide-tape: %s: Can't allocate a tape struct\n",
3087 drive->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003088 goto failed;
3089 }
3090
3091 g = alloc_disk(1 << PARTN_BITS);
3092 if (!g)
3093 goto out_free_tape;
3094
3095 ide_init_disk(g, drive);
3096
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +02003097 ide_proc_register_driver(drive, &idetape_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003098
Linus Torvalds1da177e2005-04-16 15:20:36 -07003099 kref_init(&tape->kref);
3100
3101 tape->drive = drive;
3102 tape->driver = &idetape_driver;
3103 tape->disk = g;
3104
3105 g->private_data = &tape->driver;
3106
3107 drive->driver_data = tape;
3108
Arjan van de Vencf8b8972006-03-23 03:00:45 -08003109 mutex_lock(&idetape_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003110 for (minor = 0; idetape_devs[minor]; minor++)
3111 ;
3112 idetape_devs[minor] = tape;
Arjan van de Vencf8b8972006-03-23 03:00:45 -08003113 mutex_unlock(&idetape_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003114
3115 idetape_setup(drive, tape, minor);
3116
Tony Jonesdbc12722007-09-25 02:03:03 +02003117 device_create(idetape_sysfs_class, &drive->gendev,
3118 MKDEV(IDETAPE_MAJOR, minor), "%s", tape->name);
3119 device_create(idetape_sysfs_class, &drive->gendev,
3120 MKDEV(IDETAPE_MAJOR, minor + 128), "n%s", tape->name);
Will Dysond5dee802005-09-16 02:55:07 -07003121
Linus Torvalds1da177e2005-04-16 15:20:36 -07003122 g->fops = &idetape_block_ops;
3123 ide_register_region(g);
3124
3125 return 0;
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02003126
Linus Torvalds1da177e2005-04-16 15:20:36 -07003127out_free_tape:
3128 kfree(tape);
3129failed:
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02003130 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003131}
3132
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01003133static void __exit idetape_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003134{
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02003135 driver_unregister(&idetape_driver.gen_driver);
Will Dysond5dee802005-09-16 02:55:07 -07003136 class_destroy(idetape_sysfs_class);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003137 unregister_chrdev(IDETAPE_MAJOR, "ht");
3138}
3139
Bartlomiej Zolnierkiewicz17514e82005-11-19 22:24:35 +01003140static int __init idetape_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003141{
Will Dysond5dee802005-09-16 02:55:07 -07003142 int error = 1;
3143 idetape_sysfs_class = class_create(THIS_MODULE, "ide_tape");
3144 if (IS_ERR(idetape_sysfs_class)) {
3145 idetape_sysfs_class = NULL;
3146 printk(KERN_ERR "Unable to create sysfs class for ide tapes\n");
3147 error = -EBUSY;
3148 goto out;
3149 }
3150
Linus Torvalds1da177e2005-04-16 15:20:36 -07003151 if (register_chrdev(IDETAPE_MAJOR, "ht", &idetape_fops)) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01003152 printk(KERN_ERR "ide-tape: Failed to register chrdev"
3153 " interface\n");
Will Dysond5dee802005-09-16 02:55:07 -07003154 error = -EBUSY;
3155 goto out_free_class;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003156 }
Will Dysond5dee802005-09-16 02:55:07 -07003157
3158 error = driver_register(&idetape_driver.gen_driver);
3159 if (error)
3160 goto out_free_driver;
3161
3162 return 0;
3163
3164out_free_driver:
3165 driver_unregister(&idetape_driver.gen_driver);
3166out_free_class:
3167 class_destroy(idetape_sysfs_class);
3168out:
3169 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003170}
3171
Kay Sievers263756e2005-12-12 18:03:44 +01003172MODULE_ALIAS("ide:*m-tape*");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003173module_init(idetape_init);
3174module_exit(idetape_exit);
3175MODULE_ALIAS_CHARDEV_MAJOR(IDETAPE_MAJOR);
Borislav Petkov9c145762008-02-06 02:57:54 +01003176MODULE_DESCRIPTION("ATAPI Streaming TAPE Driver");
3177MODULE_LICENSE("GPL");