blob: ce3a23e18ce91b6e794c43205905b3265ac933f1 [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 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +010088 * Setting the following parameter to 0 is illegal: the pipelined mode cannot be
89 * disabled (idetape_calculate_speeds() divides by tape->max_stages.)
Linus Torvalds1da177e2005-04-16 15:20:36 -070090 */
91#define IDETAPE_MIN_PIPELINE_STAGES 1
92#define IDETAPE_MAX_PIPELINE_STAGES 400
93#define IDETAPE_INCREASE_STAGES_RATE 20
94
95/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +010096 * After each failed packet command we issue a request sense command and retry
97 * the packet command IDETAPE_MAX_PC_RETRIES times.
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +010099 * Setting IDETAPE_MAX_PC_RETRIES to 0 will disable retries.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100 */
101#define IDETAPE_MAX_PC_RETRIES 3
102
103/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100104 * With each packet command, we allocate a buffer of IDETAPE_PC_BUFFER_SIZE
105 * bytes. This is used for several packet commands (Not for READ/WRITE commands)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 */
107#define IDETAPE_PC_BUFFER_SIZE 256
108
109/*
110 * In various places in the driver, we need to allocate storage
111 * for packet commands and requests, which will remain valid while
112 * we leave the driver to wait for an interrupt or a timeout event.
113 */
114#define IDETAPE_PC_STACK (10 + IDETAPE_MAX_PC_RETRIES)
115
116/*
117 * Some drives (for example, Seagate STT3401A Travan) require a very long
118 * timeout, because they don't return an interrupt or clear their busy bit
119 * until after the command completes (even retension commands).
120 */
121#define IDETAPE_WAIT_CMD (900*HZ)
122
123/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100124 * The following parameter is used to select the point in the internal tape fifo
125 * in which we will start to refill the buffer. Decreasing the following
126 * parameter will improve the system's latency and interactive response, while
127 * using a high value might improve system throughput.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 */
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100129#define IDETAPE_FIFO_THRESHOLD 2
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130
131/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100132 * DSC polling parameters.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100134 * Polling for DSC (a single bit in the status register) is a very important
135 * function in ide-tape. There are two cases in which we poll for DSC:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100137 * 1. Before a read/write packet command, to ensure that we can transfer data
138 * from/to the tape's data buffers, without causing an actual media access.
139 * In case the tape is not ready yet, we take out our request from the device
140 * request queue, so that ide.c could service requests from the other device
141 * on the same interface in the meantime.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100143 * 2. After the successful initialization of a "media access packet command",
144 * which is a command that can take a long time to complete (the interval can
145 * range from several seconds to even an hour). Again, we postpone our request
146 * in the middle to free the bus for the other device. The polling frequency
147 * here should be lower than the read/write frequency since those media access
148 * commands are slow. We start from a "fast" frequency - IDETAPE_DSC_MA_FAST
149 * (1 second), and if we don't receive DSC after IDETAPE_DSC_MA_THRESHOLD
150 * (5 min), we switch it to a lower frequency - IDETAPE_DSC_MA_SLOW (1 min).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100152 * We also set a timeout for the timer, in case something goes wrong. The
153 * timeout should be longer then the maximum execution time of a tape operation.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 */
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100155
156/* DSC timings. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157#define IDETAPE_DSC_RW_MIN 5*HZ/100 /* 50 msec */
158#define IDETAPE_DSC_RW_MAX 40*HZ/100 /* 400 msec */
159#define IDETAPE_DSC_RW_TIMEOUT 2*60*HZ /* 2 minutes */
160#define IDETAPE_DSC_MA_FAST 2*HZ /* 2 seconds */
161#define IDETAPE_DSC_MA_THRESHOLD 5*60*HZ /* 5 minutes */
162#define IDETAPE_DSC_MA_SLOW 30*HZ /* 30 seconds */
163#define IDETAPE_DSC_MA_TIMEOUT 2*60*60*HZ /* 2 hours */
164
165/*************************** End of tunable parameters ***********************/
166
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100167/* Read/Write error simulation */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168#define SIMULATE_ERRORS 0
169
Borislav Petkov54abf372008-02-06 02:57:52 +0100170/* tape directions */
171enum {
172 IDETAPE_DIR_NONE = (1 << 0),
173 IDETAPE_DIR_READ = (1 << 1),
174 IDETAPE_DIR_WRITE = (1 << 2),
175};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176
177struct idetape_bh {
Stephen Rothwellab057962007-08-01 23:46:44 +0200178 u32 b_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 atomic_t b_count;
180 struct idetape_bh *b_reqnext;
181 char *b_data;
182};
183
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184typedef struct idetape_packet_command_s {
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100185 /* Actual packet bytes */
186 u8 c[12];
187 /* On each retry, we increment retries */
188 int retries;
189 /* Error code */
190 int error;
191 /* Bytes to transfer */
192 int request_transfer;
193 /* Bytes actually transferred */
194 int actually_transferred;
195 /* Size of our data buffer */
196 int buffer_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 struct idetape_bh *bh;
198 char *b_data;
199 int b_count;
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100200 /* Data buffer */
201 u8 *buffer;
202 /* Pointer into the above buffer */
203 u8 *current_position;
204 /* Called when this packet command is completed */
205 ide_startstop_t (*callback) (ide_drive_t *);
206 /* Temporary buffer */
207 u8 pc_buffer[IDETAPE_PC_BUFFER_SIZE];
208 /* Status/Action bit flags: long for set_bit */
209 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210} idetape_pc_t;
211
Borislav Petkov346331f2008-04-18 00:46:26 +0200212/* Packet command flag bits. */
213enum {
214 /* Set when an error is considered normal - We won't retry */
215 PC_FLAG_ABORT = (1 << 0),
216 /* 1 When polling for DSC on a media access command */
217 PC_FLAG_WAIT_FOR_DSC = (1 << 1),
218 /* 1 when we prefer to use DMA if possible */
219 PC_FLAG_DMA_RECOMMENDED = (1 << 2),
220 /* 1 while DMA in progress */
221 PC_FLAG_DMA_IN_PROGRESS = (1 << 3),
222 /* 1 when encountered problem during DMA */
223 PC_FLAG_DMA_ERROR = (1 << 4),
224 /* Data direction */
225 PC_FLAG_WRITING = (1 << 5),
226};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227
Borislav Petkov03056b92008-04-18 00:46:26 +0200228/* Tape door status */
229#define DOOR_UNLOCKED 0
230#define DOOR_LOCKED 1
231#define DOOR_EXPLICITLY_LOCKED 2
232
233/* Some defines for the SPACE command */
234#define IDETAPE_SPACE_OVER_FILEMARK 1
235#define IDETAPE_SPACE_TO_EOD 3
236
237/* Some defines for the LOAD UNLOAD command */
238#define IDETAPE_LU_LOAD_MASK 1
239#define IDETAPE_LU_RETENSION_MASK 2
240#define IDETAPE_LU_EOT_MASK 4
241
242/*
243 * Special requests for our block device strategy routine.
244 *
245 * In order to service a character device command, we add special requests to
246 * the tail of our block device request queue and wait for their completion.
247 */
248
249enum {
250 REQ_IDETAPE_PC1 = (1 << 0), /* packet command (first stage) */
251 REQ_IDETAPE_PC2 = (1 << 1), /* packet command (second stage) */
252 REQ_IDETAPE_READ = (1 << 2),
253 REQ_IDETAPE_WRITE = (1 << 3),
254};
255
256/* Error codes returned in rq->errors to the higher part of the driver. */
257#define IDETAPE_ERROR_GENERAL 101
258#define IDETAPE_ERROR_FILEMARK 102
259#define IDETAPE_ERROR_EOD 103
260
261/* Structures related to the SELECT SENSE / MODE SENSE packet commands. */
262#define IDETAPE_BLOCK_DESCRIPTOR 0
263#define IDETAPE_CAPABILITIES_PAGE 0x2a
264
Borislav Petkov346331f2008-04-18 00:46:26 +0200265/* Tape flag bits values. */
266enum {
267 IDETAPE_FLAG_IGNORE_DSC = (1 << 0),
268 /* 0 When the tape position is unknown */
269 IDETAPE_FLAG_ADDRESS_VALID = (1 << 1),
270 /* Device already opened */
271 IDETAPE_FLAG_BUSY = (1 << 2),
272 /* Error detected in a pipeline stage */
273 IDETAPE_FLAG_PIPELINE_ERR = (1 << 3),
274 /* Attempt to auto-detect the current user block size */
275 IDETAPE_FLAG_DETECT_BS = (1 << 4),
276 /* Currently on a filemark */
277 IDETAPE_FLAG_FILEMARK = (1 << 5),
278 /* DRQ interrupt device */
279 IDETAPE_FLAG_DRQ_INTERRUPT = (1 << 6),
280 /* pipeline active */
281 IDETAPE_FLAG_PIPELINE_ACTIVE = (1 << 7),
282 /* 0 = no tape is loaded, so we don't rewind after ejecting */
283 IDETAPE_FLAG_MEDIUM_PRESENT = (1 << 8),
284};
285
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100286/* A pipeline stage. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287typedef struct idetape_stage_s {
288 struct request rq; /* The corresponding request */
289 struct idetape_bh *bh; /* The data buffers */
290 struct idetape_stage_s *next; /* Pointer to the next stage */
291} idetape_stage_t;
292
293/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100294 * Most of our global data which we need to save even as we leave the driver due
295 * to an interrupt or a timer event is stored in the struct defined below.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 */
297typedef struct ide_tape_obj {
298 ide_drive_t *drive;
299 ide_driver_t *driver;
300 struct gendisk *disk;
301 struct kref kref;
302
303 /*
304 * Since a typical character device operation requires more
305 * than one packet command, we provide here enough memory
306 * for the maximum of interconnected packet commands.
307 * The packet commands are stored in the circular array pc_stack.
308 * pc_stack_index points to the last used entry, and warps around
309 * to the start when we get to the last array entry.
310 *
311 * pc points to the current processed packet command.
312 *
313 * failed_pc points to the last failed packet command, or contains
314 * NULL if we do not need to retry any packet command. This is
315 * required since an additional packet command is needed before the
316 * retry, to get detailed information on what went wrong.
317 */
318 /* Current packet command */
319 idetape_pc_t *pc;
320 /* Last failed packet command */
321 idetape_pc_t *failed_pc;
322 /* Packet command stack */
323 idetape_pc_t pc_stack[IDETAPE_PC_STACK];
324 /* Next free packet command storage space */
325 int pc_stack_index;
326 struct request rq_stack[IDETAPE_PC_STACK];
327 /* We implement a circular array */
328 int rq_stack_index;
329
330 /*
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100331 * DSC polling variables.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100333 * While polling for DSC we use postponed_rq to postpone the current
334 * request so that ide.c will be able to service pending requests on the
335 * other device. Note that at most we will have only one DSC (usually
336 * data transfer) request in the device request queue. Additional
337 * requests can be queued in our internal pipeline, but they will be
338 * visible to ide.c only one at a time.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 */
340 struct request *postponed_rq;
341 /* The time in which we started polling for DSC */
342 unsigned long dsc_polling_start;
343 /* Timer used to poll for dsc */
344 struct timer_list dsc_timer;
345 /* Read/Write dsc polling frequency */
Borislav Petkov54bb2072008-02-06 02:57:52 +0100346 unsigned long best_dsc_rw_freq;
347 unsigned long dsc_poll_freq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 unsigned long dsc_timeout;
349
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100350 /* Read position information */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 u8 partition;
352 /* Current block */
Borislav Petkov54bb2072008-02-06 02:57:52 +0100353 unsigned int first_frame;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100355 /* Last error information */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 u8 sense_key, asc, ascq;
357
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100358 /* Character device operation */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 unsigned int minor;
360 /* device name */
361 char name[4];
362 /* Current character device data transfer direction */
Borislav Petkov54abf372008-02-06 02:57:52 +0100363 u8 chrdev_dir;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364
Borislav Petkov54bb2072008-02-06 02:57:52 +0100365 /* tape block size, usually 512 or 1024 bytes */
366 unsigned short blk_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 int user_bs_factor;
Borislav Petkovb6422012008-02-02 19:56:49 +0100368
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 /* Copy of the tape's Capabilities and Mechanical Page */
Borislav Petkovb6422012008-02-02 19:56:49 +0100370 u8 caps[20];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371
372 /*
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100373 * Active data transfer request parameters.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100375 * At most, there is only one ide-tape originated data transfer request
376 * in the device request queue. This allows ide.c to easily service
377 * requests from the other device when we postpone our active request.
378 * In the pipelined operation mode, we use our internal pipeline
379 * structure to hold more data requests. The data buffer size is chosen
380 * based on the tape's recommendation.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 */
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100382 /* ptr to the request which is waiting in the device request queue */
Borislav Petkov54bb2072008-02-06 02:57:52 +0100383 struct request *active_data_rq;
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100384 /* Data buffer size chosen based on the tape's recommendation */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 int stage_size;
386 idetape_stage_t *merge_stage;
387 int merge_stage_size;
388 struct idetape_bh *bh;
389 char *b_data;
390 int b_count;
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100391
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 /*
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100393 * Pipeline parameters.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100395 * To accomplish non-pipelined mode, we simply set the following
396 * variables to zero (or NULL, where appropriate).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 */
398 /* Number of currently used stages */
399 int nr_stages;
400 /* Number of pending stages */
401 int nr_pending_stages;
402 /* We will not allocate more than this number of stages */
403 int max_stages, min_pipeline, max_pipeline;
404 /* The first stage which will be removed from the pipeline */
405 idetape_stage_t *first_stage;
406 /* The currently active stage */
407 idetape_stage_t *active_stage;
408 /* Will be serviced after the currently active request */
409 idetape_stage_t *next_stage;
410 /* New requests will be added to the pipeline here */
411 idetape_stage_t *last_stage;
412 /* Optional free stage which we can use */
413 idetape_stage_t *cache_stage;
414 int pages_per_stage;
415 /* Wasted space in each stage */
416 int excess_bh_size;
417
418 /* Status/Action flags: long for set_bit */
419 unsigned long flags;
420 /* protects the ide-tape queue */
Borislav Petkov54bb2072008-02-06 02:57:52 +0100421 spinlock_t lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100423 /* Measures average tape speed */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 unsigned long avg_time;
425 int avg_size;
426 int avg_speed;
427
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 /* the door is currently locked */
429 int door_locked;
430 /* the tape hardware is write protected */
431 char drv_write_prot;
432 /* the tape is write protected (hardware or opened as read-only) */
433 char write_prot;
434
435 /*
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100436 * Limit the number of times a request can be postponed, to avoid an
437 * infinite postpone deadlock.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 int postpone_cnt;
440
441 /*
442 * Measures number of frames:
443 *
444 * 1. written/read to/from the driver pipeline (pipeline_head).
445 * 2. written/read to/from the tape buffers (idetape_bh).
446 * 3. written/read by the tape to/from the media (tape_head).
447 */
448 int pipeline_head;
449 int buffer_head;
450 int tape_head;
451 int last_tape_head;
452
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100453 /* Speed control at the tape buffers input/output */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 unsigned long insert_time;
455 int insert_size;
456 int insert_speed;
457 int max_insert_speed;
458 int measure_insert_time;
459
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100460 /* Speed regulation negative feedback loop */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 int speed_control;
462 int pipeline_head_speed;
463 int controlled_pipeline_head_speed;
464 int uncontrolled_pipeline_head_speed;
465 int controlled_last_pipeline_head;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 unsigned long uncontrolled_pipeline_head_time;
467 unsigned long controlled_pipeline_head_time;
468 int controlled_previous_pipeline_head;
469 int uncontrolled_previous_pipeline_head;
470 unsigned long controlled_previous_head_time;
471 unsigned long uncontrolled_previous_head_time;
472 int restart_speed_control_req;
473
Borislav Petkov8004a8c2008-02-06 02:57:51 +0100474 u32 debug_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475} idetape_tape_t;
476
Arjan van de Vencf8b8972006-03-23 03:00:45 -0800477static DEFINE_MUTEX(idetape_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478
Will Dysond5dee802005-09-16 02:55:07 -0700479static struct class *idetape_sysfs_class;
480
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481#define to_ide_tape(obj) container_of(obj, struct ide_tape_obj, kref)
482
483#define ide_tape_g(disk) \
484 container_of((disk)->private_data, struct ide_tape_obj, driver)
485
486static struct ide_tape_obj *ide_tape_get(struct gendisk *disk)
487{
488 struct ide_tape_obj *tape = NULL;
489
Arjan van de Vencf8b8972006-03-23 03:00:45 -0800490 mutex_lock(&idetape_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 tape = ide_tape_g(disk);
492 if (tape)
493 kref_get(&tape->kref);
Arjan van de Vencf8b8972006-03-23 03:00:45 -0800494 mutex_unlock(&idetape_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 return tape;
496}
497
498static void ide_tape_release(struct kref *);
499
500static void ide_tape_put(struct ide_tape_obj *tape)
501{
Arjan van de Vencf8b8972006-03-23 03:00:45 -0800502 mutex_lock(&idetape_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 kref_put(&tape->kref, ide_tape_release);
Arjan van de Vencf8b8972006-03-23 03:00:45 -0800504 mutex_unlock(&idetape_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505}
506
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100508 * The variables below are used for the character device interface. Additional
509 * state variables are defined in our ide_drive_t structure.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100511static struct ide_tape_obj *idetape_devs[MAX_HWIFS * MAX_DRIVES];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512
513#define ide_tape_f(file) ((file)->private_data)
514
515static struct ide_tape_obj *ide_tape_chrdev_get(unsigned int i)
516{
517 struct ide_tape_obj *tape = NULL;
518
Arjan van de Vencf8b8972006-03-23 03:00:45 -0800519 mutex_lock(&idetape_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 tape = idetape_devs[i];
521 if (tape)
522 kref_get(&tape->kref);
Arjan van de Vencf8b8972006-03-23 03:00:45 -0800523 mutex_unlock(&idetape_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 return tape;
525}
526
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100527static void idetape_input_buffers(ide_drive_t *drive, idetape_pc_t *pc,
528 unsigned int bcount)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529{
530 struct idetape_bh *bh = pc->bh;
531 int count;
532
533 while (bcount) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 if (bh == NULL) {
535 printk(KERN_ERR "ide-tape: bh == NULL in "
536 "idetape_input_buffers\n");
Bartlomiej Zolnierkiewicz7616c0a2008-04-18 00:46:26 +0200537 ide_atapi_discard_data(drive, bcount);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 return;
539 }
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100540 count = min(
541 (unsigned int)(bh->b_size - atomic_read(&bh->b_count)),
542 bcount);
543 HWIF(drive)->atapi_input_bytes(drive, bh->b_data +
544 atomic_read(&bh->b_count), count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545 bcount -= count;
546 atomic_add(count, &bh->b_count);
547 if (atomic_read(&bh->b_count) == bh->b_size) {
548 bh = bh->b_reqnext;
549 if (bh)
550 atomic_set(&bh->b_count, 0);
551 }
552 }
553 pc->bh = bh;
554}
555
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100556static void idetape_output_buffers(ide_drive_t *drive, idetape_pc_t *pc,
557 unsigned int bcount)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558{
559 struct idetape_bh *bh = pc->bh;
560 int count;
561
562 while (bcount) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 if (bh == NULL) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100564 printk(KERN_ERR "ide-tape: bh == NULL in %s\n",
565 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566 return;
567 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 count = min((unsigned int)pc->b_count, (unsigned int)bcount);
569 HWIF(drive)->atapi_output_bytes(drive, pc->b_data, count);
570 bcount -= count;
571 pc->b_data += count;
572 pc->b_count -= count;
573 if (!pc->b_count) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100574 bh = bh->b_reqnext;
575 pc->bh = bh;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 if (bh) {
577 pc->b_data = bh->b_data;
578 pc->b_count = atomic_read(&bh->b_count);
579 }
580 }
581 }
582}
583
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100584static void idetape_update_buffers(idetape_pc_t *pc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585{
586 struct idetape_bh *bh = pc->bh;
587 int count;
588 unsigned int bcount = pc->actually_transferred;
589
Borislav Petkov346331f2008-04-18 00:46:26 +0200590 if (pc->flags & PC_FLAG_WRITING)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 return;
592 while (bcount) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 if (bh == NULL) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100594 printk(KERN_ERR "ide-tape: bh == NULL in %s\n",
595 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 return;
597 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 count = min((unsigned int)bh->b_size, (unsigned int)bcount);
599 atomic_set(&bh->b_count, count);
600 if (atomic_read(&bh->b_count) == bh->b_size)
601 bh = bh->b_reqnext;
602 bcount -= count;
603 }
604 pc->bh = bh;
605}
606
607/*
608 * idetape_next_pc_storage returns a pointer to a place in which we can
609 * safely store a packet command, even though we intend to leave the
610 * driver. A storage space for a maximum of IDETAPE_PC_STACK packet
611 * commands is allocated at initialization time.
612 */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100613static idetape_pc_t *idetape_next_pc_storage(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614{
615 idetape_tape_t *tape = drive->driver_data;
616
Borislav Petkov8004a8c2008-02-06 02:57:51 +0100617 debug_log(DBG_PCRQ_STACK, "pc_stack_index=%d\n", tape->pc_stack_index);
618
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 if (tape->pc_stack_index == IDETAPE_PC_STACK)
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100620 tape->pc_stack_index = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621 return (&tape->pc_stack[tape->pc_stack_index++]);
622}
623
624/*
625 * idetape_next_rq_storage is used along with idetape_next_pc_storage.
626 * Since we queue packet commands in the request queue, we need to
627 * allocate a request, along with the allocation of a packet command.
628 */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100629
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630/**************************************************************
631 * *
632 * This should get fixed to use kmalloc(.., GFP_ATOMIC) *
633 * followed later on by kfree(). -ml *
634 * *
635 **************************************************************/
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100636
637static struct request *idetape_next_rq_storage(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638{
639 idetape_tape_t *tape = drive->driver_data;
640
Borislav Petkov8004a8c2008-02-06 02:57:51 +0100641 debug_log(DBG_PCRQ_STACK, "rq_stack_index=%d\n", tape->rq_stack_index);
642
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 if (tape->rq_stack_index == IDETAPE_PC_STACK)
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100644 tape->rq_stack_index = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645 return (&tape->rq_stack[tape->rq_stack_index++]);
646}
647
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100648static void idetape_init_pc(idetape_pc_t *pc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649{
650 memset(pc->c, 0, 12);
651 pc->retries = 0;
652 pc->flags = 0;
653 pc->request_transfer = 0;
654 pc->buffer = pc->pc_buffer;
655 pc->buffer_size = IDETAPE_PC_BUFFER_SIZE;
656 pc->bh = NULL;
657 pc->b_data = NULL;
658}
659
660/*
Borislav Petkov1b5db432008-02-02 19:56:48 +0100661 * called on each failed packet command retry to analyze the request sense. We
662 * currently do not utilize this information.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 */
Borislav Petkov1b5db432008-02-02 19:56:48 +0100664static void idetape_analyze_error(ide_drive_t *drive, u8 *sense)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665{
666 idetape_tape_t *tape = drive->driver_data;
667 idetape_pc_t *pc = tape->failed_pc;
668
Borislav Petkov1b5db432008-02-02 19:56:48 +0100669 tape->sense_key = sense[2] & 0xF;
670 tape->asc = sense[12];
671 tape->ascq = sense[13];
Borislav Petkov8004a8c2008-02-06 02:57:51 +0100672
673 debug_log(DBG_ERR, "pc = %x, sense key = %x, asc = %x, ascq = %x\n",
674 pc->c[0], tape->sense_key, tape->asc, tape->ascq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675
Borislav Petkov1b5db432008-02-02 19:56:48 +0100676 /* Correct pc->actually_transferred by asking the tape. */
Borislav Petkov346331f2008-04-18 00:46:26 +0200677 if (pc->flags & PC_FLAG_DMA_ERROR) {
Borislav Petkov1b5db432008-02-02 19:56:48 +0100678 pc->actually_transferred = pc->request_transfer -
Borislav Petkov54bb2072008-02-06 02:57:52 +0100679 tape->blk_size *
Borislav Petkov860ff5e2008-02-02 19:56:50 +0100680 be32_to_cpu(get_unaligned((u32 *)&sense[3]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 idetape_update_buffers(pc);
682 }
683
684 /*
685 * If error was the result of a zero-length read or write command,
686 * with sense key=5, asc=0x22, ascq=0, let it slide. Some drives
687 * (i.e. Seagate STT3401A Travan) don't support 0-length read/writes.
688 */
Borislav Petkov90699ce2008-02-02 19:56:50 +0100689 if ((pc->c[0] == READ_6 || pc->c[0] == WRITE_6)
Borislav Petkov1b5db432008-02-02 19:56:48 +0100690 /* length == 0 */
691 && pc->c[4] == 0 && pc->c[3] == 0 && pc->c[2] == 0) {
692 if (tape->sense_key == 5) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 /* don't report an error, everything's ok */
694 pc->error = 0;
695 /* don't retry read/write */
Borislav Petkov346331f2008-04-18 00:46:26 +0200696 pc->flags |= PC_FLAG_ABORT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 }
698 }
Borislav Petkov90699ce2008-02-02 19:56:50 +0100699 if (pc->c[0] == READ_6 && (sense[2] & 0x80)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 pc->error = IDETAPE_ERROR_FILEMARK;
Borislav Petkov346331f2008-04-18 00:46:26 +0200701 pc->flags |= PC_FLAG_ABORT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 }
Borislav Petkov90699ce2008-02-02 19:56:50 +0100703 if (pc->c[0] == WRITE_6) {
Borislav Petkov1b5db432008-02-02 19:56:48 +0100704 if ((sense[2] & 0x40) || (tape->sense_key == 0xd
705 && tape->asc == 0x0 && tape->ascq == 0x2)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 pc->error = IDETAPE_ERROR_EOD;
Borislav Petkov346331f2008-04-18 00:46:26 +0200707 pc->flags |= PC_FLAG_ABORT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708 }
709 }
Borislav Petkov90699ce2008-02-02 19:56:50 +0100710 if (pc->c[0] == READ_6 || pc->c[0] == WRITE_6) {
Borislav Petkov1b5db432008-02-02 19:56:48 +0100711 if (tape->sense_key == 8) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712 pc->error = IDETAPE_ERROR_EOD;
Borislav Petkov346331f2008-04-18 00:46:26 +0200713 pc->flags |= PC_FLAG_ABORT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 }
Borislav Petkov346331f2008-04-18 00:46:26 +0200715 if (!(pc->flags & PC_FLAG_ABORT) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 pc->actually_transferred)
717 pc->retries = IDETAPE_MAX_PC_RETRIES + 1;
718 }
719}
720
Borislav Petkov419d4742008-02-02 19:56:51 +0100721static void idetape_activate_next_stage(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722{
723 idetape_tape_t *tape = drive->driver_data;
724 idetape_stage_t *stage = tape->next_stage;
725 struct request *rq = &stage->rq;
726
Borislav Petkov8004a8c2008-02-06 02:57:51 +0100727 debug_log(DBG_PROCS, "Enter %s\n", __func__);
728
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729 if (stage == NULL) {
Borislav Petkov8004a8c2008-02-06 02:57:51 +0100730 printk(KERN_ERR "ide-tape: bug: Trying to activate a non"
731 " existing stage\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732 return;
733 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734
735 rq->rq_disk = tape->disk;
736 rq->buffer = NULL;
737 rq->special = (void *)stage->bh;
Borislav Petkov54bb2072008-02-06 02:57:52 +0100738 tape->active_data_rq = rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 tape->active_stage = stage;
740 tape->next_stage = stage->next;
741}
742
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100743/* Free a stage along with its related buffers completely. */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100744static void __idetape_kfree_stage(idetape_stage_t *stage)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745{
746 struct idetape_bh *prev_bh, *bh = stage->bh;
747 int size;
748
749 while (bh != NULL) {
750 if (bh->b_data != NULL) {
751 size = (int) bh->b_size;
752 while (size > 0) {
753 free_page((unsigned long) bh->b_data);
754 size -= PAGE_SIZE;
755 bh->b_data += PAGE_SIZE;
756 }
757 }
758 prev_bh = bh;
759 bh = bh->b_reqnext;
760 kfree(prev_bh);
761 }
762 kfree(stage);
763}
764
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100765static void idetape_kfree_stage(idetape_tape_t *tape, idetape_stage_t *stage)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766{
767 __idetape_kfree_stage(stage);
768}
769
770/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100771 * Remove tape->first_stage from the pipeline. The caller should avoid race
772 * conditions.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773 */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100774static void idetape_remove_stage_head(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775{
776 idetape_tape_t *tape = drive->driver_data;
777 idetape_stage_t *stage;
Borislav Petkov8004a8c2008-02-06 02:57:51 +0100778
779 debug_log(DBG_PROCS, "Enter %s\n", __func__);
780
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781 if (tape->first_stage == NULL) {
782 printk(KERN_ERR "ide-tape: bug: tape->first_stage is NULL\n");
Borislav Petkov55a5d292008-02-02 19:56:49 +0100783 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784 }
785 if (tape->active_stage == tape->first_stage) {
Borislav Petkov8004a8c2008-02-06 02:57:51 +0100786 printk(KERN_ERR "ide-tape: bug: Trying to free our active "
787 "pipeline stage\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788 return;
789 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790 stage = tape->first_stage;
791 tape->first_stage = stage->next;
792 idetape_kfree_stage(tape, stage);
793 tape->nr_stages--;
794 if (tape->first_stage == NULL) {
795 tape->last_stage = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796 if (tape->next_stage != NULL)
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100797 printk(KERN_ERR "ide-tape: bug: tape->next_stage !="
798 " NULL\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799 if (tape->nr_stages)
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100800 printk(KERN_ERR "ide-tape: bug: nr_stages should be 0 "
801 "now\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802 }
803}
804
805/*
806 * This will free all the pipeline stages starting from new_last_stage->next
807 * to the end of the list, and point tape->last_stage to new_last_stage.
808 */
809static void idetape_abort_pipeline(ide_drive_t *drive,
810 idetape_stage_t *new_last_stage)
811{
812 idetape_tape_t *tape = drive->driver_data;
813 idetape_stage_t *stage = new_last_stage->next;
814 idetape_stage_t *nstage;
815
Borislav Petkov8004a8c2008-02-06 02:57:51 +0100816 debug_log(DBG_PROCS, "%s: Enter %s\n", tape->name, __func__);
817
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818 while (stage) {
819 nstage = stage->next;
820 idetape_kfree_stage(tape, stage);
821 --tape->nr_stages;
822 --tape->nr_pending_stages;
823 stage = nstage;
824 }
825 if (new_last_stage)
826 new_last_stage->next = NULL;
827 tape->last_stage = new_last_stage;
828 tape->next_stage = NULL;
829}
830
831/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100832 * Finish servicing a request and insert a pending pipeline request into the
833 * main device queue.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834 */
835static int idetape_end_request(ide_drive_t *drive, int uptodate, int nr_sects)
836{
837 struct request *rq = HWGROUP(drive)->rq;
838 idetape_tape_t *tape = drive->driver_data;
839 unsigned long flags;
840 int error;
841 int remove_stage = 0;
842 idetape_stage_t *active_stage;
843
Borislav Petkov8004a8c2008-02-06 02:57:51 +0100844 debug_log(DBG_PROCS, "Enter %s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845
846 switch (uptodate) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100847 case 0: error = IDETAPE_ERROR_GENERAL; break;
848 case 1: error = 0; break;
849 default: error = uptodate;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850 }
851 rq->errors = error;
852 if (error)
853 tape->failed_pc = NULL;
854
Bartlomiej Zolnierkiewicz36872212008-01-26 20:13:10 +0100855 if (!blk_special_request(rq)) {
856 ide_end_request(drive, uptodate, nr_sects);
857 return 0;
858 }
859
Borislav Petkov54bb2072008-02-06 02:57:52 +0100860 spin_lock_irqsave(&tape->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861
862 /* The request was a pipelined data transfer request */
Borislav Petkov54bb2072008-02-06 02:57:52 +0100863 if (tape->active_data_rq == rq) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864 active_stage = tape->active_stage;
865 tape->active_stage = NULL;
Borislav Petkov54bb2072008-02-06 02:57:52 +0100866 tape->active_data_rq = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867 tape->nr_pending_stages--;
868 if (rq->cmd[0] & REQ_IDETAPE_WRITE) {
869 remove_stage = 1;
870 if (error) {
Borislav Petkov346331f2008-04-18 00:46:26 +0200871 set_bit(IDETAPE_FLAG_PIPELINE_ERR,
872 &tape->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873 if (error == IDETAPE_ERROR_EOD)
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100874 idetape_abort_pipeline(drive,
875 active_stage);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876 }
877 } else if (rq->cmd[0] & REQ_IDETAPE_READ) {
878 if (error == IDETAPE_ERROR_EOD) {
Borislav Petkov346331f2008-04-18 00:46:26 +0200879 set_bit(IDETAPE_FLAG_PIPELINE_ERR,
880 &tape->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881 idetape_abort_pipeline(drive, active_stage);
882 }
883 }
884 if (tape->next_stage != NULL) {
Borislav Petkov419d4742008-02-02 19:56:51 +0100885 idetape_activate_next_stage(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100887 /* Insert the next request into the request queue. */
Borislav Petkov54bb2072008-02-06 02:57:52 +0100888 (void)ide_do_drive_cmd(drive, tape->active_data_rq,
889 ide_end);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890 } else if (!error) {
Borislav Petkov97219852008-02-06 02:57:52 +0100891 /*
892 * This is a part of the feedback loop which tries to
893 * find the optimum number of stages. We are starting
894 * from a minimum maximum number of stages, and if we
895 * sense that the pipeline is empty, we try to increase
896 * it, until we reach the user compile time memory
897 * limit.
898 */
899 int i = (tape->max_pipeline - tape->min_pipeline) / 10;
900
901 tape->max_stages += max(i, 1);
902 tape->max_stages = max(tape->max_stages,
903 tape->min_pipeline);
904 tape->max_stages = min(tape->max_stages,
905 tape->max_pipeline);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906 }
907 }
908 ide_end_drive_cmd(drive, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909
910 if (remove_stage)
911 idetape_remove_stage_head(drive);
Borislav Petkov54bb2072008-02-06 02:57:52 +0100912 if (tape->active_data_rq == NULL)
Borislav Petkov346331f2008-04-18 00:46:26 +0200913 clear_bit(IDETAPE_FLAG_PIPELINE_ACTIVE, &tape->flags);
Borislav Petkov54bb2072008-02-06 02:57:52 +0100914 spin_unlock_irqrestore(&tape->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915 return 0;
916}
917
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100918static ide_startstop_t idetape_request_sense_callback(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919{
920 idetape_tape_t *tape = drive->driver_data;
921
Borislav Petkov8004a8c2008-02-06 02:57:51 +0100922 debug_log(DBG_PROCS, "Enter %s\n", __func__);
923
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924 if (!tape->pc->error) {
Borislav Petkov1b5db432008-02-02 19:56:48 +0100925 idetape_analyze_error(drive, tape->pc->buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926 idetape_end_request(drive, 1, 0);
927 } else {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100928 printk(KERN_ERR "ide-tape: Error in REQUEST SENSE itself - "
929 "Aborting request!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930 idetape_end_request(drive, 0, 0);
931 }
932 return ide_stopped;
933}
934
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100935static void idetape_create_request_sense_cmd(idetape_pc_t *pc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936{
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100937 idetape_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +0100938 pc->c[0] = REQUEST_SENSE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939 pc->c[4] = 20;
940 pc->request_transfer = 20;
941 pc->callback = &idetape_request_sense_callback;
942}
943
944static void idetape_init_rq(struct request *rq, u8 cmd)
945{
946 memset(rq, 0, sizeof(*rq));
Jens Axboe4aff5e22006-08-10 08:44:47 +0200947 rq->cmd_type = REQ_TYPE_SPECIAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948 rq->cmd[0] = cmd;
949}
950
951/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100952 * Generate a new packet command request in front of the request queue, before
953 * the current request, so that it will be processed immediately, on the next
954 * pass through the driver. The function below is called from the request
955 * handling part of the driver (the "bottom" part). Safe storage for the request
956 * should be allocated with ide_tape_next_{pc,rq}_storage() prior to that.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100958 * Memory for those requests is pre-allocated at initialization time, and is
959 * limited to IDETAPE_PC_STACK requests. We assume that we have enough space for
960 * the maximum possible number of inter-dependent packet commands.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100962 * The higher level of the driver - The ioctl handler and the character device
963 * handling functions should queue request to the lower level part and wait for
964 * their completion using idetape_queue_pc_tail or idetape_queue_rw_tail.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965 */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100966static void idetape_queue_pc_head(ide_drive_t *drive, idetape_pc_t *pc,
967 struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968{
969 struct ide_tape_obj *tape = drive->driver_data;
970
971 idetape_init_rq(rq, REQ_IDETAPE_PC1);
972 rq->buffer = (char *) pc;
973 rq->rq_disk = tape->disk;
974 (void) ide_do_drive_cmd(drive, rq, ide_preempt);
975}
976
977/*
978 * idetape_retry_pc is called when an error was detected during the
979 * last packet command. We queue a request sense packet command in
980 * the head of the request list.
981 */
982static ide_startstop_t idetape_retry_pc (ide_drive_t *drive)
983{
984 idetape_tape_t *tape = drive->driver_data;
985 idetape_pc_t *pc;
986 struct request *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987
Bartlomiej Zolnierkiewicz64a57fe2008-02-06 02:57:51 +0100988 (void)ide_read_error(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989 pc = idetape_next_pc_storage(drive);
990 rq = idetape_next_rq_storage(drive);
991 idetape_create_request_sense_cmd(pc);
Borislav Petkov346331f2008-04-18 00:46:26 +0200992 set_bit(IDETAPE_FLAG_IGNORE_DSC, &tape->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993 idetape_queue_pc_head(drive, pc, rq);
994 return ide_stopped;
995}
996
997/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100998 * Postpone the current request so that ide.c will be able to service requests
999 * from another device on the same hwgroup while we are polling for DSC.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000 */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001001static void idetape_postpone_request(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002{
1003 idetape_tape_t *tape = drive->driver_data;
1004
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001005 debug_log(DBG_PROCS, "Enter %s\n", __func__);
1006
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007 tape->postponed_rq = HWGROUP(drive)->rq;
Borislav Petkov54bb2072008-02-06 02:57:52 +01001008 ide_stall_queue(drive, tape->dsc_poll_freq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009}
1010
Borislav Petkova1efc852008-02-06 02:57:52 +01001011typedef void idetape_io_buf(ide_drive_t *, idetape_pc_t *, unsigned int);
1012
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013/*
Borislav Petkova1efc852008-02-06 02:57:52 +01001014 * This is the usual interrupt handler which will be called during a packet
1015 * command. We will transfer some of the data (as requested by the drive) and
1016 * will re-point interrupt handler to us. When data transfer is finished, we
1017 * will act according to the algorithm described before
Borislav Petkov8d06bfa2008-02-06 02:57:53 +01001018 * idetape_issue_pc.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019 */
Borislav Petkova1efc852008-02-06 02:57:52 +01001020static ide_startstop_t idetape_pc_intr(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021{
1022 ide_hwif_t *hwif = drive->hwif;
1023 idetape_tape_t *tape = drive->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024 idetape_pc_t *pc = tape->pc;
Borislav Petkova1efc852008-02-06 02:57:52 +01001025 xfer_func_t *xferfunc;
1026 idetape_io_buf *iobuf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027 unsigned int temp;
1028#if SIMULATE_ERRORS
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001029 static int error_sim_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030#endif
Bartlomiej Zolnierkiewicz790d1232008-01-25 22:17:12 +01001031 u16 bcount;
Bartlomiej Zolnierkiewicz8e7657a2008-01-25 22:17:12 +01001032 u8 stat, ireason;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001034 debug_log(DBG_PROCS, "Enter %s - interrupt handler\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035
1036 /* Clear the interrupt */
Bartlomiej Zolnierkiewiczc47137a2008-02-06 02:57:51 +01001037 stat = ide_read_status(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038
Borislav Petkov346331f2008-04-18 00:46:26 +02001039 if (pc->flags & PC_FLAG_DMA_IN_PROGRESS) {
Bartlomiej Zolnierkiewicz22c525b2008-01-25 22:17:11 +01001040 if (hwif->ide_dma_end(drive) || (stat & ERR_STAT)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041 /*
1042 * A DMA error is sometimes expected. For example,
1043 * if the tape is crossing a filemark during a
1044 * READ command, it will issue an irq and position
1045 * itself before the filemark, so that only a partial
1046 * data transfer will occur (which causes the DMA
1047 * error). In that case, we will later ask the tape
1048 * how much bytes of the original request were
1049 * actually transferred (we can't receive that
1050 * information from the DMA engine on most chipsets).
1051 */
1052
1053 /*
1054 * On the contrary, a DMA error is never expected;
1055 * it usually indicates a hardware error or abort.
1056 * If the tape crosses a filemark during a READ
1057 * command, it will issue an irq and position itself
1058 * after the filemark (not before). Only a partial
1059 * data transfer will occur, but no DMA error.
1060 * (AS, 19 Apr 2001)
1061 */
Borislav Petkov346331f2008-04-18 00:46:26 +02001062 pc->flags |= PC_FLAG_DMA_ERROR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063 } else {
1064 pc->actually_transferred = pc->request_transfer;
1065 idetape_update_buffers(pc);
1066 }
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001067 debug_log(DBG_PROCS, "DMA finished\n");
1068
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069 }
1070
1071 /* No more interrupts */
Bartlomiej Zolnierkiewicz22c525b2008-01-25 22:17:11 +01001072 if ((stat & DRQ_STAT) == 0) {
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001073 debug_log(DBG_SENSE, "Packet command completed, %d bytes"
1074 " transferred\n", pc->actually_transferred);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075
Borislav Petkov346331f2008-04-18 00:46:26 +02001076 pc->flags &= ~PC_FLAG_DMA_IN_PROGRESS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077 local_irq_enable();
1078
1079#if SIMULATE_ERRORS
Borislav Petkov90699ce2008-02-02 19:56:50 +01001080 if ((pc->c[0] == WRITE_6 || pc->c[0] == READ_6) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081 (++error_sim_count % 100) == 0) {
1082 printk(KERN_INFO "ide-tape: %s: simulating error\n",
1083 tape->name);
Bartlomiej Zolnierkiewicz22c525b2008-01-25 22:17:11 +01001084 stat |= ERR_STAT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085 }
1086#endif
Borislav Petkov90699ce2008-02-02 19:56:50 +01001087 if ((stat & ERR_STAT) && pc->c[0] == REQUEST_SENSE)
Bartlomiej Zolnierkiewicz22c525b2008-01-25 22:17:11 +01001088 stat &= ~ERR_STAT;
Borislav Petkov346331f2008-04-18 00:46:26 +02001089 if ((stat & ERR_STAT) || (pc->flags & PC_FLAG_DMA_ERROR)) {
Bartlomiej Zolnierkiewicz22c525b2008-01-25 22:17:11 +01001090 /* Error detected */
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001091 debug_log(DBG_ERR, "%s: I/O error\n", tape->name);
1092
Borislav Petkov90699ce2008-02-02 19:56:50 +01001093 if (pc->c[0] == REQUEST_SENSE) {
Borislav Petkova1efc852008-02-06 02:57:52 +01001094 printk(KERN_ERR "ide-tape: I/O error in request"
1095 " sense command\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096 return ide_do_reset(drive);
1097 }
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001098 debug_log(DBG_ERR, "[cmd %x]: check condition\n",
1099 pc->c[0]);
1100
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101 /* Retry operation */
1102 return idetape_retry_pc(drive);
1103 }
1104 pc->error = 0;
Borislav Petkov346331f2008-04-18 00:46:26 +02001105 if ((pc->flags & PC_FLAG_WAIT_FOR_DSC) &&
Bartlomiej Zolnierkiewicz22c525b2008-01-25 22:17:11 +01001106 (stat & SEEK_STAT) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107 /* Media access command */
1108 tape->dsc_polling_start = jiffies;
Borislav Petkov54bb2072008-02-06 02:57:52 +01001109 tape->dsc_poll_freq = IDETAPE_DSC_MA_FAST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110 tape->dsc_timeout = jiffies + IDETAPE_DSC_MA_TIMEOUT;
1111 /* Allow ide.c to handle other requests */
1112 idetape_postpone_request(drive);
1113 return ide_stopped;
1114 }
1115 if (tape->failed_pc == pc)
1116 tape->failed_pc = NULL;
1117 /* Command finished - Call the callback function */
1118 return pc->callback(drive);
1119 }
Borislav Petkov346331f2008-04-18 00:46:26 +02001120
1121 if (pc->flags & PC_FLAG_DMA_IN_PROGRESS) {
1122 pc->flags &= ~PC_FLAG_DMA_IN_PROGRESS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123 printk(KERN_ERR "ide-tape: The tape wants to issue more "
1124 "interrupts in DMA mode\n");
1125 printk(KERN_ERR "ide-tape: DMA disabled, reverting to PIO\n");
Bartlomiej Zolnierkiewicz7469aaf2007-02-17 02:40:26 +01001126 ide_dma_off(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127 return ide_do_reset(drive);
1128 }
1129 /* Get the number of bytes to transfer on this interrupt. */
Bartlomiej Zolnierkiewicz23579a22008-04-18 00:46:26 +02001130 bcount = (hwif->INB(hwif->io_ports[IDE_BCOUNTH_OFFSET]) << 8) |
1131 hwif->INB(hwif->io_ports[IDE_BCOUNTL_OFFSET]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132
Bartlomiej Zolnierkiewicz23579a22008-04-18 00:46:26 +02001133 ireason = hwif->INB(hwif->io_ports[IDE_IREASON_OFFSET]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134
Bartlomiej Zolnierkiewicz8e7657a2008-01-25 22:17:12 +01001135 if (ireason & CD) {
Borislav Petkova1efc852008-02-06 02:57:52 +01001136 printk(KERN_ERR "ide-tape: CoD != 0 in %s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137 return ide_do_reset(drive);
1138 }
Borislav Petkov346331f2008-04-18 00:46:26 +02001139 if (((ireason & IO) == IO) == !!(pc->flags & PC_FLAG_WRITING)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140 /* Hopefully, we will never get here */
1141 printk(KERN_ERR "ide-tape: We wanted to %s, ",
Bartlomiej Zolnierkiewicz8e7657a2008-01-25 22:17:12 +01001142 (ireason & IO) ? "Write" : "Read");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143 printk(KERN_ERR "ide-tape: but the tape wants us to %s !\n",
Bartlomiej Zolnierkiewicz8e7657a2008-01-25 22:17:12 +01001144 (ireason & IO) ? "Read" : "Write");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145 return ide_do_reset(drive);
1146 }
Borislav Petkov346331f2008-04-18 00:46:26 +02001147 if (!(pc->flags & PC_FLAG_WRITING)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148 /* Reading - Check that we have enough space */
Bartlomiej Zolnierkiewicz790d1232008-01-25 22:17:12 +01001149 temp = pc->actually_transferred + bcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150 if (temp > pc->request_transfer) {
1151 if (temp > pc->buffer_size) {
Borislav Petkova1efc852008-02-06 02:57:52 +01001152 printk(KERN_ERR "ide-tape: The tape wants to "
1153 "send us more data than expected "
1154 "- discarding data\n");
Bartlomiej Zolnierkiewicz7616c0a2008-04-18 00:46:26 +02001155 ide_atapi_discard_data(drive, bcount);
Borislav Petkova1efc852008-02-06 02:57:52 +01001156 ide_set_handler(drive, &idetape_pc_intr,
1157 IDETAPE_WAIT_CMD, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158 return ide_started;
1159 }
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001160 debug_log(DBG_SENSE, "The tape wants to send us more "
1161 "data than expected - allowing transfer\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162 }
Borislav Petkova1efc852008-02-06 02:57:52 +01001163 iobuf = &idetape_input_buffers;
1164 xferfunc = hwif->atapi_input_bytes;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165 } else {
Borislav Petkova1efc852008-02-06 02:57:52 +01001166 iobuf = &idetape_output_buffers;
1167 xferfunc = hwif->atapi_output_bytes;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168 }
Borislav Petkova1efc852008-02-06 02:57:52 +01001169
1170 if (pc->bh)
1171 iobuf(drive, pc, bcount);
1172 else
1173 xferfunc(drive, pc->current_position, bcount);
1174
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175 /* Update the current position */
Bartlomiej Zolnierkiewicz790d1232008-01-25 22:17:12 +01001176 pc->actually_transferred += bcount;
1177 pc->current_position += bcount;
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001178
1179 debug_log(DBG_SENSE, "[cmd %x] transferred %d bytes on that intr.\n",
1180 pc->c[0], bcount);
1181
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182 /* And set the interrupt handler again */
1183 ide_set_handler(drive, &idetape_pc_intr, IDETAPE_WAIT_CMD, NULL);
1184 return ide_started;
1185}
1186
1187/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001188 * Packet Command Interface
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001190 * The current Packet Command is available in tape->pc, and will not change
1191 * until we finish handling it. Each packet command is associated with a
1192 * callback function that will be called when the command is finished.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001194 * The handling will be done in three stages:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001196 * 1. idetape_issue_pc will send the packet command to the drive, and will set
1197 * the interrupt handler to idetape_pc_intr.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001199 * 2. On each interrupt, idetape_pc_intr will be called. This step will be
1200 * repeated until the device signals us that no more interrupts will be issued.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001202 * 3. ATAPI Tape media access commands have immediate status with a delayed
1203 * process. In case of a successful initiation of a media access packet command,
1204 * the DSC bit will be set when the actual execution of the command is finished.
1205 * Since the tape drive will not issue an interrupt, we have to poll for this
1206 * event. In this case, we define the request as "low priority request" by
1207 * setting rq_status to IDETAPE_RQ_POSTPONED, set a timer to poll for DSC and
1208 * exit the driver.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001210 * ide.c will then give higher priority to requests which originate from the
1211 * other device, until will change rq_status to RQ_ACTIVE.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001213 * 4. When the packet command is finished, it will be checked for errors.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001215 * 5. In case an error was found, we queue a request sense packet command in
1216 * front of the request queue and retry the operation up to
1217 * IDETAPE_MAX_PC_RETRIES times.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001219 * 6. In case no error was found, or we decided to give up and not to retry
1220 * again, the callback function will be called and then we will handle the next
1221 * request.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222 */
1223static ide_startstop_t idetape_transfer_pc(ide_drive_t *drive)
1224{
1225 ide_hwif_t *hwif = drive->hwif;
1226 idetape_tape_t *tape = drive->driver_data;
1227 idetape_pc_t *pc = tape->pc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228 int retries = 100;
1229 ide_startstop_t startstop;
Bartlomiej Zolnierkiewicz8e7657a2008-01-25 22:17:12 +01001230 u8 ireason;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001232 if (ide_wait_stat(&startstop, drive, DRQ_STAT, BUSY_STAT, WAIT_READY)) {
1233 printk(KERN_ERR "ide-tape: Strange, packet command initiated "
1234 "yet DRQ isn't asserted\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235 return startstop;
1236 }
Bartlomiej Zolnierkiewicz23579a22008-04-18 00:46:26 +02001237 ireason = hwif->INB(hwif->io_ports[IDE_IREASON_OFFSET]);
Bartlomiej Zolnierkiewicz8e7657a2008-01-25 22:17:12 +01001238 while (retries-- && ((ireason & CD) == 0 || (ireason & IO))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239 printk(KERN_ERR "ide-tape: (IO,CoD != (0,1) while issuing "
1240 "a packet command, retrying\n");
1241 udelay(100);
Bartlomiej Zolnierkiewicz23579a22008-04-18 00:46:26 +02001242 ireason = hwif->INB(hwif->io_ports[IDE_IREASON_OFFSET]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243 if (retries == 0) {
1244 printk(KERN_ERR "ide-tape: (IO,CoD != (0,1) while "
1245 "issuing a packet command, ignoring\n");
Bartlomiej Zolnierkiewicz8e7657a2008-01-25 22:17:12 +01001246 ireason |= CD;
1247 ireason &= ~IO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248 }
1249 }
Bartlomiej Zolnierkiewicz8e7657a2008-01-25 22:17:12 +01001250 if ((ireason & CD) == 0 || (ireason & IO)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251 printk(KERN_ERR "ide-tape: (IO,CoD) != (0,1) while issuing "
1252 "a packet command\n");
1253 return ide_do_reset(drive);
1254 }
1255 /* Set the interrupt routine */
1256 ide_set_handler(drive, &idetape_pc_intr, IDETAPE_WAIT_CMD, NULL);
1257#ifdef CONFIG_BLK_DEV_IDEDMA
1258 /* Begin DMA, if necessary */
Borislav Petkov346331f2008-04-18 00:46:26 +02001259 if (pc->flags & PC_FLAG_DMA_IN_PROGRESS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260 hwif->dma_start(drive);
1261#endif
1262 /* Send the actual packet */
1263 HWIF(drive)->atapi_output_bytes(drive, pc->c, 12);
1264 return ide_started;
1265}
1266
Borislav Petkov8d06bfa2008-02-06 02:57:53 +01001267static ide_startstop_t idetape_issue_pc(ide_drive_t *drive, idetape_pc_t *pc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268{
1269 ide_hwif_t *hwif = drive->hwif;
1270 idetape_tape_t *tape = drive->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271 int dma_ok = 0;
Bartlomiej Zolnierkiewicz790d1232008-01-25 22:17:12 +01001272 u16 bcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273
Borislav Petkov90699ce2008-02-02 19:56:50 +01001274 if (tape->pc->c[0] == REQUEST_SENSE &&
1275 pc->c[0] == REQUEST_SENSE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276 printk(KERN_ERR "ide-tape: possible ide-tape.c bug - "
1277 "Two request sense in serial were issued\n");
1278 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279
Borislav Petkov90699ce2008-02-02 19:56:50 +01001280 if (tape->failed_pc == NULL && pc->c[0] != REQUEST_SENSE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281 tape->failed_pc = pc;
1282 /* Set the current packet command */
1283 tape->pc = pc;
1284
1285 if (pc->retries > IDETAPE_MAX_PC_RETRIES ||
Borislav Petkov346331f2008-04-18 00:46:26 +02001286 (pc->flags & PC_FLAG_ABORT)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287 /*
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001288 * We will "abort" retrying a packet command in case legitimate
1289 * error code was received (crossing a filemark, or end of the
1290 * media, for example).
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291 */
Borislav Petkov346331f2008-04-18 00:46:26 +02001292 if (!(pc->flags & PC_FLAG_ABORT)) {
Borislav Petkov90699ce2008-02-02 19:56:50 +01001293 if (!(pc->c[0] == TEST_UNIT_READY &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294 tape->sense_key == 2 && tape->asc == 4 &&
1295 (tape->ascq == 1 || tape->ascq == 8))) {
1296 printk(KERN_ERR "ide-tape: %s: I/O error, "
1297 "pc = %2x, key = %2x, "
1298 "asc = %2x, ascq = %2x\n",
1299 tape->name, pc->c[0],
1300 tape->sense_key, tape->asc,
1301 tape->ascq);
1302 }
1303 /* Giving up */
1304 pc->error = IDETAPE_ERROR_GENERAL;
1305 }
1306 tape->failed_pc = NULL;
1307 return pc->callback(drive);
1308 }
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001309 debug_log(DBG_SENSE, "Retry #%d, cmd = %02X\n", pc->retries, pc->c[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310
1311 pc->retries++;
1312 /* We haven't transferred any data yet */
1313 pc->actually_transferred = 0;
1314 pc->current_position = pc->buffer;
1315 /* Request to transfer the entire buffer at once */
Bartlomiej Zolnierkiewicz790d1232008-01-25 22:17:12 +01001316 bcount = pc->request_transfer;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317
Borislav Petkov346331f2008-04-18 00:46:26 +02001318 if (pc->flags & PC_FLAG_DMA_ERROR) {
1319 pc->flags &= ~PC_FLAG_DMA_ERROR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320 printk(KERN_WARNING "ide-tape: DMA disabled, "
1321 "reverting to PIO\n");
Bartlomiej Zolnierkiewicz7469aaf2007-02-17 02:40:26 +01001322 ide_dma_off(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323 }
Borislav Petkov346331f2008-04-18 00:46:26 +02001324 if ((pc->flags & PC_FLAG_DMA_RECOMMENDED) && drive->using_dma)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325 dma_ok = !hwif->dma_setup(drive);
1326
Bartlomiej Zolnierkiewicz2fc57382008-01-25 22:17:13 +01001327 ide_pktcmd_tf_load(drive, IDE_TFLAG_NO_SELECT_MASK |
1328 IDE_TFLAG_OUT_DEVICE, bcount, dma_ok);
1329
Borislav Petkov346331f2008-04-18 00:46:26 +02001330 if (dma_ok)
1331 /* Will begin DMA later */
1332 pc->flags |= PC_FLAG_DMA_IN_PROGRESS;
1333 if (test_bit(IDETAPE_FLAG_DRQ_INTERRUPT, &tape->flags)) {
Bartlomiej Zolnierkiewiczc1c9dbc2008-02-02 19:56:44 +01001334 ide_execute_command(drive, WIN_PACKETCMD, &idetape_transfer_pc,
1335 IDETAPE_WAIT_CMD, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336 return ide_started;
1337 } else {
Bartlomiej Zolnierkiewicz23579a22008-04-18 00:46:26 +02001338 hwif->OUTB(WIN_PACKETCMD, hwif->io_ports[IDE_COMMAND_OFFSET]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339 return idetape_transfer_pc(drive);
1340 }
1341}
1342
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001343static ide_startstop_t idetape_pc_callback(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344{
1345 idetape_tape_t *tape = drive->driver_data;
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001346
1347 debug_log(DBG_PROCS, "Enter %s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348
1349 idetape_end_request(drive, tape->pc->error ? 0 : 1, 0);
1350 return ide_stopped;
1351}
1352
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001353/* A mode sense command is used to "sense" tape parameters. */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001354static void idetape_create_mode_sense_cmd(idetape_pc_t *pc, u8 page_code)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355{
1356 idetape_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +01001357 pc->c[0] = MODE_SENSE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358 if (page_code != IDETAPE_BLOCK_DESCRIPTOR)
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001359 /* DBD = 1 - Don't return block descriptors */
1360 pc->c[1] = 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361 pc->c[2] = page_code;
1362 /*
1363 * Changed pc->c[3] to 0 (255 will at best return unused info).
1364 *
1365 * For SCSI this byte is defined as subpage instead of high byte
1366 * of length and some IDE drives seem to interpret it this way
1367 * and return an error when 255 is used.
1368 */
1369 pc->c[3] = 0;
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001370 /* We will just discard data in that case */
1371 pc->c[4] = 255;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372 if (page_code == IDETAPE_BLOCK_DESCRIPTOR)
1373 pc->request_transfer = 12;
1374 else if (page_code == IDETAPE_CAPABILITIES_PAGE)
1375 pc->request_transfer = 24;
1376 else
1377 pc->request_transfer = 50;
1378 pc->callback = &idetape_pc_callback;
1379}
1380
Borislav Petkov37016ba2008-02-06 02:57:52 +01001381static void idetape_calculate_speeds(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382{
1383 idetape_tape_t *tape = drive->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001385 if (time_after(jiffies,
1386 tape->controlled_pipeline_head_time + 120 * HZ)) {
1387 tape->controlled_previous_pipeline_head =
1388 tape->controlled_last_pipeline_head;
1389 tape->controlled_previous_head_time =
1390 tape->controlled_pipeline_head_time;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391 tape->controlled_last_pipeline_head = tape->pipeline_head;
1392 tape->controlled_pipeline_head_time = jiffies;
1393 }
1394 if (time_after(jiffies, tape->controlled_pipeline_head_time + 60 * HZ))
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001395 tape->controlled_pipeline_head_speed = (tape->pipeline_head -
1396 tape->controlled_last_pipeline_head) * 32 * HZ /
1397 (jiffies - tape->controlled_pipeline_head_time);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398 else if (time_after(jiffies, tape->controlled_previous_head_time))
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001399 tape->controlled_pipeline_head_speed = (tape->pipeline_head -
1400 tape->controlled_previous_pipeline_head) * 32 *
1401 HZ / (jiffies - tape->controlled_previous_head_time);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001403 if (tape->nr_pending_stages < tape->max_stages/*- 1 */) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404 /* -1 for read mode error recovery */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001405 if (time_after(jiffies, tape->uncontrolled_previous_head_time +
1406 10 * HZ)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407 tape->uncontrolled_pipeline_head_time = jiffies;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001408 tape->uncontrolled_pipeline_head_speed =
1409 (tape->pipeline_head -
1410 tape->uncontrolled_previous_pipeline_head) *
1411 32 * HZ / (jiffies -
1412 tape->uncontrolled_previous_head_time);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413 }
1414 } else {
1415 tape->uncontrolled_previous_head_time = jiffies;
1416 tape->uncontrolled_previous_pipeline_head = tape->pipeline_head;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001417 if (time_after(jiffies, tape->uncontrolled_pipeline_head_time +
1418 30 * HZ))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419 tape->uncontrolled_pipeline_head_time = jiffies;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001420
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421 }
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001422 tape->pipeline_head_speed = max(tape->uncontrolled_pipeline_head_speed,
1423 tape->controlled_pipeline_head_speed);
Borislav Petkov37016ba2008-02-06 02:57:52 +01001424
1425 if (tape->speed_control == 1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426 if (tape->nr_pending_stages >= tape->max_stages / 2)
1427 tape->max_insert_speed = tape->pipeline_head_speed +
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001428 (1100 - tape->pipeline_head_speed) * 2 *
1429 (tape->nr_pending_stages - tape->max_stages / 2)
1430 / tape->max_stages;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431 else
1432 tape->max_insert_speed = 500 +
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001433 (tape->pipeline_head_speed - 500) * 2 *
1434 tape->nr_pending_stages / tape->max_stages;
Borislav Petkov37016ba2008-02-06 02:57:52 +01001435
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436 if (tape->nr_pending_stages >= tape->max_stages * 99 / 100)
1437 tape->max_insert_speed = 5000;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438 } else
1439 tape->max_insert_speed = tape->speed_control;
Borislav Petkov37016ba2008-02-06 02:57:52 +01001440
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441 tape->max_insert_speed = max(tape->max_insert_speed, 500);
1442}
1443
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001444static ide_startstop_t idetape_media_access_finished(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445{
1446 idetape_tape_t *tape = drive->driver_data;
1447 idetape_pc_t *pc = tape->pc;
Bartlomiej Zolnierkiewicz22c525b2008-01-25 22:17:11 +01001448 u8 stat;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449
Bartlomiej Zolnierkiewiczc47137a2008-02-06 02:57:51 +01001450 stat = ide_read_status(drive);
1451
Bartlomiej Zolnierkiewicz22c525b2008-01-25 22:17:11 +01001452 if (stat & SEEK_STAT) {
1453 if (stat & ERR_STAT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454 /* Error detected */
Borislav Petkov90699ce2008-02-02 19:56:50 +01001455 if (pc->c[0] != TEST_UNIT_READY)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456 printk(KERN_ERR "ide-tape: %s: I/O error, ",
1457 tape->name);
1458 /* Retry operation */
1459 return idetape_retry_pc(drive);
1460 }
1461 pc->error = 0;
1462 if (tape->failed_pc == pc)
1463 tape->failed_pc = NULL;
1464 } else {
1465 pc->error = IDETAPE_ERROR_GENERAL;
1466 tape->failed_pc = NULL;
1467 }
1468 return pc->callback(drive);
1469}
1470
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001471static ide_startstop_t idetape_rw_callback(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472{
1473 idetape_tape_t *tape = drive->driver_data;
1474 struct request *rq = HWGROUP(drive)->rq;
Borislav Petkov54bb2072008-02-06 02:57:52 +01001475 int blocks = tape->pc->actually_transferred / tape->blk_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476
Borislav Petkov54bb2072008-02-06 02:57:52 +01001477 tape->avg_size += blocks * tape->blk_size;
1478 tape->insert_size += blocks * tape->blk_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479 if (tape->insert_size > 1024 * 1024)
1480 tape->measure_insert_time = 1;
1481 if (tape->measure_insert_time) {
1482 tape->measure_insert_time = 0;
1483 tape->insert_time = jiffies;
1484 tape->insert_size = 0;
1485 }
1486 if (time_after(jiffies, tape->insert_time))
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001487 tape->insert_speed = tape->insert_size / 1024 * HZ /
1488 (jiffies - tape->insert_time);
Marcelo Feitoza Parisi9bae1ff2006-03-28 01:56:46 -08001489 if (time_after_eq(jiffies, tape->avg_time + HZ)) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001490 tape->avg_speed = tape->avg_size * HZ /
1491 (jiffies - tape->avg_time) / 1024;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492 tape->avg_size = 0;
1493 tape->avg_time = jiffies;
1494 }
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001495 debug_log(DBG_PROCS, "Enter %s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496
Borislav Petkov54bb2072008-02-06 02:57:52 +01001497 tape->first_frame += blocks;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498 rq->current_nr_sectors -= blocks;
1499
1500 if (!tape->pc->error)
1501 idetape_end_request(drive, 1, 0);
1502 else
1503 idetape_end_request(drive, tape->pc->error, 0);
1504 return ide_stopped;
1505}
1506
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001507static void idetape_create_read_cmd(idetape_tape_t *tape, idetape_pc_t *pc,
1508 unsigned int length, struct idetape_bh *bh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509{
1510 idetape_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +01001511 pc->c[0] = READ_6;
Borislav Petkov860ff5e2008-02-02 19:56:50 +01001512 put_unaligned(cpu_to_be32(length), (unsigned int *) &pc->c[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513 pc->c[1] = 1;
1514 pc->callback = &idetape_rw_callback;
1515 pc->bh = bh;
1516 atomic_set(&bh->b_count, 0);
1517 pc->buffer = NULL;
Borislav Petkov54bb2072008-02-06 02:57:52 +01001518 pc->buffer_size = length * tape->blk_size;
1519 pc->request_transfer = pc->buffer_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520 if (pc->request_transfer == tape->stage_size)
Borislav Petkov346331f2008-04-18 00:46:26 +02001521 pc->flags |= PC_FLAG_DMA_RECOMMENDED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522}
1523
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001524static void idetape_create_write_cmd(idetape_tape_t *tape, idetape_pc_t *pc,
1525 unsigned int length, struct idetape_bh *bh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526{
1527 idetape_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +01001528 pc->c[0] = WRITE_6;
Borislav Petkov860ff5e2008-02-02 19:56:50 +01001529 put_unaligned(cpu_to_be32(length), (unsigned int *) &pc->c[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001530 pc->c[1] = 1;
1531 pc->callback = &idetape_rw_callback;
Borislav Petkov346331f2008-04-18 00:46:26 +02001532 pc->flags |= PC_FLAG_WRITING;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533 pc->bh = bh;
1534 pc->b_data = bh->b_data;
1535 pc->b_count = atomic_read(&bh->b_count);
1536 pc->buffer = NULL;
Borislav Petkov54bb2072008-02-06 02:57:52 +01001537 pc->buffer_size = length * tape->blk_size;
1538 pc->request_transfer = pc->buffer_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539 if (pc->request_transfer == tape->stage_size)
Borislav Petkov346331f2008-04-18 00:46:26 +02001540 pc->flags |= PC_FLAG_DMA_RECOMMENDED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541}
1542
Linus Torvalds1da177e2005-04-16 15:20:36 -07001543static ide_startstop_t idetape_do_request(ide_drive_t *drive,
1544 struct request *rq, sector_t block)
1545{
1546 idetape_tape_t *tape = drive->driver_data;
1547 idetape_pc_t *pc = NULL;
1548 struct request *postponed_rq = tape->postponed_rq;
Bartlomiej Zolnierkiewicz22c525b2008-01-25 22:17:11 +01001549 u8 stat;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001551 debug_log(DBG_SENSE, "sector: %ld, nr_sectors: %ld,"
1552 " current_nr_sectors: %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553 rq->sector, rq->nr_sectors, rq->current_nr_sectors);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554
Jens Axboe4aff5e22006-08-10 08:44:47 +02001555 if (!blk_special_request(rq)) {
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001556 /* We do not support buffer cache originated requests. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557 printk(KERN_NOTICE "ide-tape: %s: Unsupported request in "
Jens Axboe4aff5e22006-08-10 08:44:47 +02001558 "request queue (%d)\n", drive->name, rq->cmd_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559 ide_end_request(drive, 0, 0);
1560 return ide_stopped;
1561 }
1562
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001563 /* Retry a failed packet command */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001564 if (tape->failed_pc && tape->pc->c[0] == REQUEST_SENSE)
Borislav Petkov8d06bfa2008-02-06 02:57:53 +01001565 return idetape_issue_pc(drive, tape->failed_pc);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001566
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567 if (postponed_rq != NULL)
1568 if (rq != postponed_rq) {
1569 printk(KERN_ERR "ide-tape: ide-tape.c bug - "
1570 "Two DSC requests were queued\n");
1571 idetape_end_request(drive, 0, 0);
1572 return ide_stopped;
1573 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001574
1575 tape->postponed_rq = NULL;
1576
1577 /*
1578 * If the tape is still busy, postpone our request and service
1579 * the other device meanwhile.
1580 */
Bartlomiej Zolnierkiewiczc47137a2008-02-06 02:57:51 +01001581 stat = ide_read_status(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582
1583 if (!drive->dsc_overlap && !(rq->cmd[0] & REQ_IDETAPE_PC2))
Borislav Petkov346331f2008-04-18 00:46:26 +02001584 set_bit(IDETAPE_FLAG_IGNORE_DSC, &tape->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585
1586 if (drive->post_reset == 1) {
Borislav Petkov346331f2008-04-18 00:46:26 +02001587 set_bit(IDETAPE_FLAG_IGNORE_DSC, &tape->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001588 drive->post_reset = 0;
1589 }
1590
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591 if (time_after(jiffies, tape->insert_time))
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001592 tape->insert_speed = tape->insert_size / 1024 * HZ /
1593 (jiffies - tape->insert_time);
Borislav Petkov37016ba2008-02-06 02:57:52 +01001594 idetape_calculate_speeds(drive);
Borislav Petkov346331f2008-04-18 00:46:26 +02001595 if (!test_and_clear_bit(IDETAPE_FLAG_IGNORE_DSC, &tape->flags) &&
Bartlomiej Zolnierkiewicz22c525b2008-01-25 22:17:11 +01001596 (stat & SEEK_STAT) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001597 if (postponed_rq == NULL) {
1598 tape->dsc_polling_start = jiffies;
Borislav Petkov54bb2072008-02-06 02:57:52 +01001599 tape->dsc_poll_freq = tape->best_dsc_rw_freq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001600 tape->dsc_timeout = jiffies + IDETAPE_DSC_RW_TIMEOUT;
1601 } else if (time_after(jiffies, tape->dsc_timeout)) {
1602 printk(KERN_ERR "ide-tape: %s: DSC timeout\n",
1603 tape->name);
1604 if (rq->cmd[0] & REQ_IDETAPE_PC2) {
1605 idetape_media_access_finished(drive);
1606 return ide_stopped;
1607 } else {
1608 return ide_do_reset(drive);
1609 }
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001610 } else if (time_after(jiffies,
1611 tape->dsc_polling_start +
1612 IDETAPE_DSC_MA_THRESHOLD))
Borislav Petkov54bb2072008-02-06 02:57:52 +01001613 tape->dsc_poll_freq = IDETAPE_DSC_MA_SLOW;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001614 idetape_postpone_request(drive);
1615 return ide_stopped;
1616 }
1617 if (rq->cmd[0] & REQ_IDETAPE_READ) {
1618 tape->buffer_head++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001619 tape->postpone_cnt = 0;
1620 pc = idetape_next_pc_storage(drive);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001621 idetape_create_read_cmd(tape, pc, rq->current_nr_sectors,
1622 (struct idetape_bh *)rq->special);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623 goto out;
1624 }
1625 if (rq->cmd[0] & REQ_IDETAPE_WRITE) {
1626 tape->buffer_head++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001627 tape->postpone_cnt = 0;
1628 pc = idetape_next_pc_storage(drive);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001629 idetape_create_write_cmd(tape, pc, rq->current_nr_sectors,
1630 (struct idetape_bh *)rq->special);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001631 goto out;
1632 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001633 if (rq->cmd[0] & REQ_IDETAPE_PC1) {
1634 pc = (idetape_pc_t *) rq->buffer;
1635 rq->cmd[0] &= ~(REQ_IDETAPE_PC1);
1636 rq->cmd[0] |= REQ_IDETAPE_PC2;
1637 goto out;
1638 }
1639 if (rq->cmd[0] & REQ_IDETAPE_PC2) {
1640 idetape_media_access_finished(drive);
1641 return ide_stopped;
1642 }
1643 BUG();
1644out:
Borislav Petkov8d06bfa2008-02-06 02:57:53 +01001645 return idetape_issue_pc(drive, pc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001646}
1647
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001648/* Pipeline related functions */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001649static inline int idetape_pipeline_active(idetape_tape_t *tape)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001650{
1651 int rc1, rc2;
1652
Borislav Petkov346331f2008-04-18 00:46:26 +02001653 rc1 = test_bit(IDETAPE_FLAG_PIPELINE_ACTIVE, &tape->flags);
Borislav Petkov54bb2072008-02-06 02:57:52 +01001654 rc2 = (tape->active_data_rq != NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001655 return rc1;
1656}
1657
1658/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001659 * The function below uses __get_free_page to allocate a pipeline stage, along
1660 * with all the necessary small buffers which together make a buffer of size
1661 * tape->stage_size (or a bit more). We attempt to combine sequential pages as
1662 * much as possible.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001663 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001664 * It returns a pointer to the new allocated stage, or NULL if we can't (or
1665 * don't want to) allocate a stage.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001666 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001667 * Pipeline stages are optional and are used to increase performance. If we
1668 * can't allocate them, we'll manage without them.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001669 */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001670static idetape_stage_t *__idetape_kmalloc_stage(idetape_tape_t *tape, int full,
1671 int clear)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001672{
1673 idetape_stage_t *stage;
1674 struct idetape_bh *prev_bh, *bh;
1675 int pages = tape->pages_per_stage;
1676 char *b_data = NULL;
1677
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001678 stage = kmalloc(sizeof(idetape_stage_t), GFP_KERNEL);
1679 if (!stage)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001680 return NULL;
1681 stage->next = NULL;
1682
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001683 stage->bh = kmalloc(sizeof(struct idetape_bh), GFP_KERNEL);
1684 bh = stage->bh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001685 if (bh == NULL)
1686 goto abort;
1687 bh->b_reqnext = NULL;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001688 bh->b_data = (char *) __get_free_page(GFP_KERNEL);
1689 if (!bh->b_data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001690 goto abort;
1691 if (clear)
1692 memset(bh->b_data, 0, PAGE_SIZE);
1693 bh->b_size = PAGE_SIZE;
1694 atomic_set(&bh->b_count, full ? bh->b_size : 0);
1695
1696 while (--pages) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001697 b_data = (char *) __get_free_page(GFP_KERNEL);
1698 if (!b_data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001699 goto abort;
1700 if (clear)
1701 memset(b_data, 0, PAGE_SIZE);
1702 if (bh->b_data == b_data + PAGE_SIZE) {
1703 bh->b_size += PAGE_SIZE;
1704 bh->b_data -= PAGE_SIZE;
1705 if (full)
1706 atomic_add(PAGE_SIZE, &bh->b_count);
1707 continue;
1708 }
1709 if (b_data == bh->b_data + bh->b_size) {
1710 bh->b_size += PAGE_SIZE;
1711 if (full)
1712 atomic_add(PAGE_SIZE, &bh->b_count);
1713 continue;
1714 }
1715 prev_bh = bh;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001716 bh = kmalloc(sizeof(struct idetape_bh), GFP_KERNEL);
1717 if (!bh) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001718 free_page((unsigned long) b_data);
1719 goto abort;
1720 }
1721 bh->b_reqnext = NULL;
1722 bh->b_data = b_data;
1723 bh->b_size = PAGE_SIZE;
1724 atomic_set(&bh->b_count, full ? bh->b_size : 0);
1725 prev_bh->b_reqnext = bh;
1726 }
1727 bh->b_size -= tape->excess_bh_size;
1728 if (full)
1729 atomic_sub(tape->excess_bh_size, &bh->b_count);
1730 return stage;
1731abort:
1732 __idetape_kfree_stage(stage);
1733 return NULL;
1734}
1735
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001736static idetape_stage_t *idetape_kmalloc_stage(idetape_tape_t *tape)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001737{
1738 idetape_stage_t *cache_stage = tape->cache_stage;
1739
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001740 debug_log(DBG_PROCS, "Enter %s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001741
1742 if (tape->nr_stages >= tape->max_stages)
1743 return NULL;
1744 if (cache_stage != NULL) {
1745 tape->cache_stage = NULL;
1746 return cache_stage;
1747 }
1748 return __idetape_kmalloc_stage(tape, 0, 0);
1749}
1750
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001751static int idetape_copy_stage_from_user(idetape_tape_t *tape,
1752 idetape_stage_t *stage, const char __user *buf, int n)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001753{
1754 struct idetape_bh *bh = tape->bh;
1755 int count;
Daniel Walkerdcd96372006-06-25 05:47:37 -07001756 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001757
1758 while (n) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001759 if (bh == NULL) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001760 printk(KERN_ERR "ide-tape: bh == NULL in %s\n",
1761 __func__);
Daniel Walkerdcd96372006-06-25 05:47:37 -07001762 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001763 }
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001764 count = min((unsigned int)
1765 (bh->b_size - atomic_read(&bh->b_count)),
1766 (unsigned int)n);
1767 if (copy_from_user(bh->b_data + atomic_read(&bh->b_count), buf,
1768 count))
Daniel Walkerdcd96372006-06-25 05:47:37 -07001769 ret = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001770 n -= count;
1771 atomic_add(count, &bh->b_count);
1772 buf += count;
1773 if (atomic_read(&bh->b_count) == bh->b_size) {
1774 bh = bh->b_reqnext;
1775 if (bh)
1776 atomic_set(&bh->b_count, 0);
1777 }
1778 }
1779 tape->bh = bh;
Daniel Walkerdcd96372006-06-25 05:47:37 -07001780 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001781}
1782
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001783static int idetape_copy_stage_to_user(idetape_tape_t *tape, char __user *buf,
1784 idetape_stage_t *stage, int n)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001785{
1786 struct idetape_bh *bh = tape->bh;
1787 int count;
Daniel Walkerdcd96372006-06-25 05:47:37 -07001788 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001789
1790 while (n) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001791 if (bh == NULL) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001792 printk(KERN_ERR "ide-tape: bh == NULL in %s\n",
1793 __func__);
Daniel Walkerdcd96372006-06-25 05:47:37 -07001794 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001795 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001796 count = min(tape->b_count, n);
Daniel Walkerdcd96372006-06-25 05:47:37 -07001797 if (copy_to_user(buf, tape->b_data, count))
1798 ret = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001799 n -= count;
1800 tape->b_data += count;
1801 tape->b_count -= count;
1802 buf += count;
1803 if (!tape->b_count) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001804 bh = bh->b_reqnext;
1805 tape->bh = bh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001806 if (bh) {
1807 tape->b_data = bh->b_data;
1808 tape->b_count = atomic_read(&bh->b_count);
1809 }
1810 }
1811 }
Daniel Walkerdcd96372006-06-25 05:47:37 -07001812 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001813}
1814
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001815static void idetape_init_merge_stage(idetape_tape_t *tape)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001816{
1817 struct idetape_bh *bh = tape->merge_stage->bh;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001818
Linus Torvalds1da177e2005-04-16 15:20:36 -07001819 tape->bh = bh;
Borislav Petkov54abf372008-02-06 02:57:52 +01001820 if (tape->chrdev_dir == IDETAPE_DIR_WRITE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001821 atomic_set(&bh->b_count, 0);
1822 else {
1823 tape->b_data = bh->b_data;
1824 tape->b_count = atomic_read(&bh->b_count);
1825 }
1826}
1827
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001828static void idetape_switch_buffers(idetape_tape_t *tape, idetape_stage_t *stage)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001829{
1830 struct idetape_bh *tmp;
1831
1832 tmp = stage->bh;
1833 stage->bh = tape->merge_stage->bh;
1834 tape->merge_stage->bh = tmp;
1835 idetape_init_merge_stage(tape);
1836}
1837
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001838/* Add a new stage at the end of the pipeline. */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001839static void idetape_add_stage_tail(ide_drive_t *drive, idetape_stage_t *stage)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001840{
1841 idetape_tape_t *tape = drive->driver_data;
1842 unsigned long flags;
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001843
1844 debug_log(DBG_PROCS, "Enter %s\n", __func__);
1845
Borislav Petkov54bb2072008-02-06 02:57:52 +01001846 spin_lock_irqsave(&tape->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001847 stage->next = NULL;
1848 if (tape->last_stage != NULL)
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001849 tape->last_stage->next = stage;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001850 else
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001851 tape->first_stage = stage;
1852 tape->next_stage = stage;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001853 tape->last_stage = stage;
1854 if (tape->next_stage == NULL)
1855 tape->next_stage = tape->last_stage;
1856 tape->nr_stages++;
1857 tape->nr_pending_stages++;
Borislav Petkov54bb2072008-02-06 02:57:52 +01001858 spin_unlock_irqrestore(&tape->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001859}
1860
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001861/* Install a completion in a pending request and sleep until it is serviced. The
1862 * caller should ensure that the request will not be serviced before we install
1863 * the completion (usually by disabling interrupts).
Linus Torvalds1da177e2005-04-16 15:20:36 -07001864 */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001865static void idetape_wait_for_request(ide_drive_t *drive, struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001866{
Peter Zijlstra6e9a4732006-09-30 23:28:10 -07001867 DECLARE_COMPLETION_ONSTACK(wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001868 idetape_tape_t *tape = drive->driver_data;
1869
Jens Axboe4aff5e22006-08-10 08:44:47 +02001870 if (rq == NULL || !blk_special_request(rq)) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001871 printk(KERN_ERR "ide-tape: bug: Trying to sleep on non-valid"
1872 " request\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001873 return;
1874 }
Jens Axboec00895a2006-09-30 20:29:12 +02001875 rq->end_io_data = &wait;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001876 rq->end_io = blk_end_sync_rq;
Borislav Petkov54bb2072008-02-06 02:57:52 +01001877 spin_unlock_irq(&tape->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001878 wait_for_completion(&wait);
1879 /* The stage and its struct request have been deallocated */
Borislav Petkov54bb2072008-02-06 02:57:52 +01001880 spin_lock_irq(&tape->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001881}
1882
Borislav Petkova2f5b7f2008-02-06 02:57:51 +01001883static ide_startstop_t idetape_read_position_callback(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001884{
1885 idetape_tape_t *tape = drive->driver_data;
Borislav Petkova2f5b7f2008-02-06 02:57:51 +01001886 u8 *readpos = tape->pc->buffer;
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001887
1888 debug_log(DBG_PROCS, "Enter %s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001889
1890 if (!tape->pc->error) {
Borislav Petkova2f5b7f2008-02-06 02:57:51 +01001891 debug_log(DBG_SENSE, "BOP - %s\n",
1892 (readpos[0] & 0x80) ? "Yes" : "No");
1893 debug_log(DBG_SENSE, "EOP - %s\n",
1894 (readpos[0] & 0x40) ? "Yes" : "No");
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001895
Borislav Petkova2f5b7f2008-02-06 02:57:51 +01001896 if (readpos[0] & 0x4) {
1897 printk(KERN_INFO "ide-tape: Block location is unknown"
1898 "to the tape\n");
Borislav Petkov346331f2008-04-18 00:46:26 +02001899 clear_bit(IDETAPE_FLAG_ADDRESS_VALID, &tape->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001900 idetape_end_request(drive, 0, 0);
1901 } else {
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001902 debug_log(DBG_SENSE, "Block Location - %u\n",
Borislav Petkova2f5b7f2008-02-06 02:57:51 +01001903 be32_to_cpu(*(u32 *)&readpos[4]));
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001904
Borislav Petkova2f5b7f2008-02-06 02:57:51 +01001905 tape->partition = readpos[1];
Borislav Petkov54bb2072008-02-06 02:57:52 +01001906 tape->first_frame =
Borislav Petkova2f5b7f2008-02-06 02:57:51 +01001907 be32_to_cpu(*(u32 *)&readpos[4]);
Borislav Petkov346331f2008-04-18 00:46:26 +02001908 set_bit(IDETAPE_FLAG_ADDRESS_VALID, &tape->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001909 idetape_end_request(drive, 1, 0);
1910 }
1911 } else {
1912 idetape_end_request(drive, 0, 0);
1913 }
1914 return ide_stopped;
1915}
1916
1917/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001918 * Write a filemark if write_filemark=1. Flush the device buffers without
1919 * writing a filemark otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001920 */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001921static void idetape_create_write_filemark_cmd(ide_drive_t *drive,
1922 idetape_pc_t *pc, int write_filemark)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001923{
1924 idetape_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +01001925 pc->c[0] = WRITE_FILEMARKS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001926 pc->c[4] = write_filemark;
Borislav Petkov346331f2008-04-18 00:46:26 +02001927 pc->flags |= PC_FLAG_WAIT_FOR_DSC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001928 pc->callback = &idetape_pc_callback;
1929}
1930
1931static void idetape_create_test_unit_ready_cmd(idetape_pc_t *pc)
1932{
1933 idetape_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +01001934 pc->c[0] = TEST_UNIT_READY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001935 pc->callback = &idetape_pc_callback;
1936}
1937
1938/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001939 * We add a special packet command request to the tail of the request queue, and
1940 * wait for it to be serviced. This is not to be called from within the request
1941 * handling part of the driver! We allocate here data on the stack and it is
1942 * valid until the request is finished. This is not the case for the bottom part
1943 * of the driver, where we are always leaving the functions to wait for an
1944 * interrupt or a timer event.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001945 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001946 * From the bottom part of the driver, we should allocate safe memory using
1947 * idetape_next_pc_storage() and ide_tape_next_rq_storage(), and add the request
1948 * to the request list without waiting for it to be serviced! In that case, we
1949 * usually use idetape_queue_pc_head().
Linus Torvalds1da177e2005-04-16 15:20:36 -07001950 */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001951static int __idetape_queue_pc_tail(ide_drive_t *drive, idetape_pc_t *pc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001952{
1953 struct ide_tape_obj *tape = drive->driver_data;
1954 struct request rq;
1955
1956 idetape_init_rq(&rq, REQ_IDETAPE_PC1);
1957 rq.buffer = (char *) pc;
1958 rq.rq_disk = tape->disk;
1959 return ide_do_drive_cmd(drive, &rq, ide_wait);
1960}
1961
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001962static void idetape_create_load_unload_cmd(ide_drive_t *drive, idetape_pc_t *pc,
1963 int cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001964{
1965 idetape_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +01001966 pc->c[0] = START_STOP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001967 pc->c[4] = cmd;
Borislav Petkov346331f2008-04-18 00:46:26 +02001968 pc->flags |= PC_FLAG_WAIT_FOR_DSC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001969 pc->callback = &idetape_pc_callback;
1970}
1971
1972static int idetape_wait_ready(ide_drive_t *drive, unsigned long timeout)
1973{
1974 idetape_tape_t *tape = drive->driver_data;
1975 idetape_pc_t pc;
1976 int load_attempted = 0;
1977
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001978 /* Wait for the tape to become ready */
Borislav Petkov346331f2008-04-18 00:46:26 +02001979 set_bit(IDETAPE_FLAG_MEDIUM_PRESENT, &tape->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001980 timeout += jiffies;
1981 while (time_before(jiffies, timeout)) {
1982 idetape_create_test_unit_ready_cmd(&pc);
1983 if (!__idetape_queue_pc_tail(drive, &pc))
1984 return 0;
1985 if ((tape->sense_key == 2 && tape->asc == 4 && tape->ascq == 2)
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001986 || (tape->asc == 0x3A)) {
1987 /* no media */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001988 if (load_attempted)
1989 return -ENOMEDIUM;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001990 idetape_create_load_unload_cmd(drive, &pc,
1991 IDETAPE_LU_LOAD_MASK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001992 __idetape_queue_pc_tail(drive, &pc);
1993 load_attempted = 1;
1994 /* not about to be ready */
1995 } else if (!(tape->sense_key == 2 && tape->asc == 4 &&
1996 (tape->ascq == 1 || tape->ascq == 8)))
1997 return -EIO;
Nishanth Aravamudan80ce45f2005-09-10 00:27:08 -07001998 msleep(100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001999 }
2000 return -EIO;
2001}
2002
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002003static int idetape_queue_pc_tail(ide_drive_t *drive, idetape_pc_t *pc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002004{
2005 return __idetape_queue_pc_tail(drive, pc);
2006}
2007
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002008static int idetape_flush_tape_buffers(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002009{
2010 idetape_pc_t pc;
2011 int rc;
2012
2013 idetape_create_write_filemark_cmd(drive, &pc, 0);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002014 rc = idetape_queue_pc_tail(drive, &pc);
2015 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002016 return rc;
2017 idetape_wait_ready(drive, 60 * 5 * HZ);
2018 return 0;
2019}
2020
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002021static void idetape_create_read_position_cmd(idetape_pc_t *pc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002022{
2023 idetape_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +01002024 pc->c[0] = READ_POSITION;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002025 pc->request_transfer = 20;
2026 pc->callback = &idetape_read_position_callback;
2027}
2028
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002029static int idetape_read_position(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002030{
2031 idetape_tape_t *tape = drive->driver_data;
2032 idetape_pc_t pc;
2033 int position;
2034
Borislav Petkov8004a8c2008-02-06 02:57:51 +01002035 debug_log(DBG_PROCS, "Enter %s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002036
2037 idetape_create_read_position_cmd(&pc);
2038 if (idetape_queue_pc_tail(drive, &pc))
2039 return -1;
Borislav Petkov54bb2072008-02-06 02:57:52 +01002040 position = tape->first_frame;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002041 return position;
2042}
2043
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002044static void idetape_create_locate_cmd(ide_drive_t *drive, idetape_pc_t *pc,
2045 unsigned int block, u8 partition, int skip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002046{
2047 idetape_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +01002048 pc->c[0] = POSITION_TO_ELEMENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002049 pc->c[1] = 2;
Borislav Petkov860ff5e2008-02-02 19:56:50 +01002050 put_unaligned(cpu_to_be32(block), (unsigned int *) &pc->c[3]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002051 pc->c[8] = partition;
Borislav Petkov346331f2008-04-18 00:46:26 +02002052 pc->flags |= PC_FLAG_WAIT_FOR_DSC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002053 pc->callback = &idetape_pc_callback;
2054}
2055
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002056static int idetape_create_prevent_cmd(ide_drive_t *drive, idetape_pc_t *pc,
2057 int prevent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002058{
2059 idetape_tape_t *tape = drive->driver_data;
2060
Borislav Petkovb6422012008-02-02 19:56:49 +01002061 /* device supports locking according to capabilities page */
2062 if (!(tape->caps[6] & 0x01))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002063 return 0;
2064
2065 idetape_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +01002066 pc->c[0] = ALLOW_MEDIUM_REMOVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002067 pc->c[4] = prevent;
2068 pc->callback = &idetape_pc_callback;
2069 return 1;
2070}
2071
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002072static int __idetape_discard_read_pipeline(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002073{
2074 idetape_tape_t *tape = drive->driver_data;
2075 unsigned long flags;
2076 int cnt;
2077
Borislav Petkov54abf372008-02-06 02:57:52 +01002078 if (tape->chrdev_dir != IDETAPE_DIR_READ)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002079 return 0;
2080
2081 /* Remove merge stage. */
Borislav Petkov54bb2072008-02-06 02:57:52 +01002082 cnt = tape->merge_stage_size / tape->blk_size;
Borislav Petkov346331f2008-04-18 00:46:26 +02002083 if (test_and_clear_bit(IDETAPE_FLAG_FILEMARK, &tape->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002084 ++cnt; /* Filemarks count as 1 sector */
2085 tape->merge_stage_size = 0;
2086 if (tape->merge_stage != NULL) {
2087 __idetape_kfree_stage(tape->merge_stage);
2088 tape->merge_stage = NULL;
2089 }
2090
2091 /* Clear pipeline flags. */
Borislav Petkov346331f2008-04-18 00:46:26 +02002092 clear_bit(IDETAPE_FLAG_PIPELINE_ERR, &tape->flags);
Borislav Petkov54abf372008-02-06 02:57:52 +01002093 tape->chrdev_dir = IDETAPE_DIR_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002094
2095 /* Remove pipeline stages. */
2096 if (tape->first_stage == NULL)
2097 return 0;
2098
Borislav Petkov54bb2072008-02-06 02:57:52 +01002099 spin_lock_irqsave(&tape->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002100 tape->next_stage = NULL;
2101 if (idetape_pipeline_active(tape))
Borislav Petkov54bb2072008-02-06 02:57:52 +01002102 idetape_wait_for_request(drive, tape->active_data_rq);
2103 spin_unlock_irqrestore(&tape->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002104
2105 while (tape->first_stage != NULL) {
2106 struct request *rq_ptr = &tape->first_stage->rq;
2107
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002108 cnt += rq_ptr->nr_sectors - rq_ptr->current_nr_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002109 if (rq_ptr->errors == IDETAPE_ERROR_FILEMARK)
2110 ++cnt;
2111 idetape_remove_stage_head(drive);
2112 }
2113 tape->nr_pending_stages = 0;
2114 tape->max_stages = tape->min_pipeline;
2115 return cnt;
2116}
2117
2118/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +01002119 * Position the tape to the requested block using the LOCATE packet command.
2120 * A READ POSITION command is then issued to check where we are positioned. Like
2121 * all higher level operations, we queue the commands at the tail of the request
2122 * queue and wait for their completion.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002123 */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002124static int idetape_position_tape(ide_drive_t *drive, unsigned int block,
2125 u8 partition, int skip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002126{
2127 idetape_tape_t *tape = drive->driver_data;
2128 int retval;
2129 idetape_pc_t pc;
2130
Borislav Petkov54abf372008-02-06 02:57:52 +01002131 if (tape->chrdev_dir == IDETAPE_DIR_READ)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002132 __idetape_discard_read_pipeline(drive);
2133 idetape_wait_ready(drive, 60 * 5 * HZ);
2134 idetape_create_locate_cmd(drive, &pc, block, partition, skip);
2135 retval = idetape_queue_pc_tail(drive, &pc);
2136 if (retval)
2137 return (retval);
2138
2139 idetape_create_read_position_cmd(&pc);
2140 return (idetape_queue_pc_tail(drive, &pc));
2141}
2142
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002143static void idetape_discard_read_pipeline(ide_drive_t *drive,
2144 int restore_position)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002145{
2146 idetape_tape_t *tape = drive->driver_data;
2147 int cnt;
2148 int seek, position;
2149
2150 cnt = __idetape_discard_read_pipeline(drive);
2151 if (restore_position) {
2152 position = idetape_read_position(drive);
2153 seek = position > cnt ? position - cnt : 0;
2154 if (idetape_position_tape(drive, seek, 0, 0)) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002155 printk(KERN_INFO "ide-tape: %s: position_tape failed in"
2156 " discard_pipeline()\n", tape->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002157 return;
2158 }
2159 }
2160}
2161
2162/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +01002163 * Generate a read/write request for the block device interface and wait for it
2164 * to be serviced.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002165 */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002166static int idetape_queue_rw_tail(ide_drive_t *drive, int cmd, int blocks,
2167 struct idetape_bh *bh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002168{
2169 idetape_tape_t *tape = drive->driver_data;
2170 struct request rq;
2171
Borislav Petkov8004a8c2008-02-06 02:57:51 +01002172 debug_log(DBG_SENSE, "%s: cmd=%d\n", __func__, cmd);
2173
Linus Torvalds1da177e2005-04-16 15:20:36 -07002174 if (idetape_pipeline_active(tape)) {
Borislav Petkov8004a8c2008-02-06 02:57:51 +01002175 printk(KERN_ERR "ide-tape: bug: the pipeline is active in %s\n",
2176 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002177 return (0);
2178 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002179
2180 idetape_init_rq(&rq, cmd);
2181 rq.rq_disk = tape->disk;
2182 rq.special = (void *)bh;
Borislav Petkov54bb2072008-02-06 02:57:52 +01002183 rq.sector = tape->first_frame;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002184 rq.nr_sectors = blocks;
2185 rq.current_nr_sectors = blocks;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002186 (void) ide_do_drive_cmd(drive, &rq, ide_wait);
2187
2188 if ((cmd & (REQ_IDETAPE_READ | REQ_IDETAPE_WRITE)) == 0)
2189 return 0;
2190
2191 if (tape->merge_stage)
2192 idetape_init_merge_stage(tape);
2193 if (rq.errors == IDETAPE_ERROR_GENERAL)
2194 return -EIO;
Borislav Petkov54bb2072008-02-06 02:57:52 +01002195 return (tape->blk_size * (blocks-rq.current_nr_sectors));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002196}
2197
Borislav Petkov8d06bfa2008-02-06 02:57:53 +01002198/* start servicing the pipeline stages, starting from tape->next_stage. */
2199static void idetape_plug_pipeline(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002200{
2201 idetape_tape_t *tape = drive->driver_data;
2202
2203 if (tape->next_stage == NULL)
2204 return;
2205 if (!idetape_pipeline_active(tape)) {
Borislav Petkov346331f2008-04-18 00:46:26 +02002206 set_bit(IDETAPE_FLAG_PIPELINE_ACTIVE, &tape->flags);
Borislav Petkov419d4742008-02-02 19:56:51 +01002207 idetape_activate_next_stage(drive);
Borislav Petkov54bb2072008-02-06 02:57:52 +01002208 (void) ide_do_drive_cmd(drive, tape->active_data_rq, ide_end);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002209 }
2210}
2211
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002212static void idetape_create_inquiry_cmd(idetape_pc_t *pc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002213{
2214 idetape_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +01002215 pc->c[0] = INQUIRY;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002216 pc->c[4] = 254;
2217 pc->request_transfer = 254;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002218 pc->callback = &idetape_pc_callback;
2219}
2220
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002221static void idetape_create_rewind_cmd(ide_drive_t *drive, idetape_pc_t *pc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002222{
2223 idetape_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +01002224 pc->c[0] = REZERO_UNIT;
Borislav Petkov346331f2008-04-18 00:46:26 +02002225 pc->flags |= PC_FLAG_WAIT_FOR_DSC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002226 pc->callback = &idetape_pc_callback;
2227}
2228
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002229static void idetape_create_erase_cmd(idetape_pc_t *pc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002230{
2231 idetape_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +01002232 pc->c[0] = ERASE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002233 pc->c[1] = 1;
Borislav Petkov346331f2008-04-18 00:46:26 +02002234 pc->flags |= PC_FLAG_WAIT_FOR_DSC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002235 pc->callback = &idetape_pc_callback;
2236}
2237
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002238static void idetape_create_space_cmd(idetape_pc_t *pc, int count, u8 cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002239{
2240 idetape_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +01002241 pc->c[0] = SPACE;
Borislav Petkov860ff5e2008-02-02 19:56:50 +01002242 put_unaligned(cpu_to_be32(count), (unsigned int *) &pc->c[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002243 pc->c[1] = cmd;
Borislav Petkov346331f2008-04-18 00:46:26 +02002244 pc->flags |= PC_FLAG_WAIT_FOR_DSC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002245 pc->callback = &idetape_pc_callback;
2246}
2247
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002248static void idetape_wait_first_stage(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002249{
2250 idetape_tape_t *tape = drive->driver_data;
2251 unsigned long flags;
2252
2253 if (tape->first_stage == NULL)
2254 return;
Borislav Petkov54bb2072008-02-06 02:57:52 +01002255 spin_lock_irqsave(&tape->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002256 if (tape->active_stage == tape->first_stage)
Borislav Petkov54bb2072008-02-06 02:57:52 +01002257 idetape_wait_for_request(drive, tape->active_data_rq);
2258 spin_unlock_irqrestore(&tape->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002259}
2260
2261/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +01002262 * Try to add a character device originated write request to our pipeline. In
2263 * case we don't succeed, we revert to non-pipelined operation mode for this
2264 * request. In order to accomplish that, we
Linus Torvalds1da177e2005-04-16 15:20:36 -07002265 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +01002266 * 1. Try to allocate a new pipeline stage.
2267 * 2. If we can't, wait for more and more requests to be serviced and try again
2268 * each time.
2269 * 3. If we still can't allocate a stage, fallback to non-pipelined operation
2270 * mode for this request.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002271 */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002272static int idetape_add_chrdev_write_request(ide_drive_t *drive, int blocks)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002273{
2274 idetape_tape_t *tape = drive->driver_data;
2275 idetape_stage_t *new_stage;
2276 unsigned long flags;
2277 struct request *rq;
2278
Borislav Petkov8004a8c2008-02-06 02:57:51 +01002279 debug_log(DBG_CHRDEV, "Enter %s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002280
Borislav Petkov3c98bf32008-02-06 02:57:53 +01002281 /* Attempt to allocate a new stage. Beware possible race conditions. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002282 while ((new_stage = idetape_kmalloc_stage(tape)) == NULL) {
Borislav Petkov54bb2072008-02-06 02:57:52 +01002283 spin_lock_irqsave(&tape->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002284 if (idetape_pipeline_active(tape)) {
Borislav Petkov54bb2072008-02-06 02:57:52 +01002285 idetape_wait_for_request(drive, tape->active_data_rq);
2286 spin_unlock_irqrestore(&tape->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002287 } else {
Borislav Petkov54bb2072008-02-06 02:57:52 +01002288 spin_unlock_irqrestore(&tape->lock, flags);
Borislav Petkov8d06bfa2008-02-06 02:57:53 +01002289 idetape_plug_pipeline(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002290 if (idetape_pipeline_active(tape))
2291 continue;
2292 /*
Borislav Petkov3c98bf32008-02-06 02:57:53 +01002293 * The machine is short on memory. Fallback to non-
2294 * pipelined operation mode for this request.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002295 */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002296 return idetape_queue_rw_tail(drive, REQ_IDETAPE_WRITE,
2297 blocks, tape->merge_stage->bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002298 }
2299 }
2300 rq = &new_stage->rq;
2301 idetape_init_rq(rq, REQ_IDETAPE_WRITE);
2302 /* Doesn't actually matter - We always assume sequential access */
Borislav Petkov54bb2072008-02-06 02:57:52 +01002303 rq->sector = tape->first_frame;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002304 rq->current_nr_sectors = blocks;
2305 rq->nr_sectors = blocks;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002306
2307 idetape_switch_buffers(tape, new_stage);
2308 idetape_add_stage_tail(drive, new_stage);
2309 tape->pipeline_head++;
Borislav Petkov37016ba2008-02-06 02:57:52 +01002310 idetape_calculate_speeds(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002311
2312 /*
Borislav Petkov3c98bf32008-02-06 02:57:53 +01002313 * Estimate whether the tape has stopped writing by checking if our
2314 * write pipeline is currently empty. If we are not writing anymore,
2315 * wait for the pipeline to be almost completely full (90%) before
2316 * starting to service requests, so that we will be able to keep up with
2317 * the higher speeds of the tape.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002318 */
2319 if (!idetape_pipeline_active(tape)) {
2320 if (tape->nr_stages >= tape->max_stages * 9 / 10 ||
Borislav Petkov54bb2072008-02-06 02:57:52 +01002321 tape->nr_stages >= tape->max_stages -
2322 tape->uncontrolled_pipeline_head_speed * 3 * 1024 /
2323 tape->blk_size) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002324 tape->measure_insert_time = 1;
2325 tape->insert_time = jiffies;
2326 tape->insert_size = 0;
2327 tape->insert_speed = 0;
Borislav Petkov8d06bfa2008-02-06 02:57:53 +01002328 idetape_plug_pipeline(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002329 }
2330 }
Borislav Petkov346331f2008-04-18 00:46:26 +02002331 if (test_and_clear_bit(IDETAPE_FLAG_PIPELINE_ERR, &tape->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002332 /* Return a deferred error */
2333 return -EIO;
2334 return blocks;
2335}
2336
2337/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +01002338 * Wait until all pending pipeline requests are serviced. Typically called on
2339 * device close.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002340 */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002341static void idetape_wait_for_pipeline(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002342{
2343 idetape_tape_t *tape = drive->driver_data;
2344 unsigned long flags;
2345
2346 while (tape->next_stage || idetape_pipeline_active(tape)) {
Borislav Petkov8d06bfa2008-02-06 02:57:53 +01002347 idetape_plug_pipeline(drive);
Borislav Petkov54bb2072008-02-06 02:57:52 +01002348 spin_lock_irqsave(&tape->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002349 if (idetape_pipeline_active(tape))
Borislav Petkov54bb2072008-02-06 02:57:52 +01002350 idetape_wait_for_request(drive, tape->active_data_rq);
2351 spin_unlock_irqrestore(&tape->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002352 }
2353}
2354
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002355static void idetape_empty_write_pipeline(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002356{
2357 idetape_tape_t *tape = drive->driver_data;
2358 int blocks, min;
2359 struct idetape_bh *bh;
Borislav Petkov55a5d292008-02-02 19:56:49 +01002360
Borislav Petkov54abf372008-02-06 02:57:52 +01002361 if (tape->chrdev_dir != IDETAPE_DIR_WRITE) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002362 printk(KERN_ERR "ide-tape: bug: Trying to empty write pipeline,"
2363 " but we are not writing.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002364 return;
2365 }
2366 if (tape->merge_stage_size > tape->stage_size) {
2367 printk(KERN_ERR "ide-tape: bug: merge_buffer too big\n");
2368 tape->merge_stage_size = tape->stage_size;
2369 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002370 if (tape->merge_stage_size) {
Borislav Petkov54bb2072008-02-06 02:57:52 +01002371 blocks = tape->merge_stage_size / tape->blk_size;
2372 if (tape->merge_stage_size % tape->blk_size) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002373 unsigned int i;
2374
2375 blocks++;
Borislav Petkov54bb2072008-02-06 02:57:52 +01002376 i = tape->blk_size - tape->merge_stage_size %
2377 tape->blk_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002378 bh = tape->bh->b_reqnext;
2379 while (bh) {
2380 atomic_set(&bh->b_count, 0);
2381 bh = bh->b_reqnext;
2382 }
2383 bh = tape->bh;
2384 while (i) {
2385 if (bh == NULL) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002386 printk(KERN_INFO "ide-tape: bug,"
2387 " bh NULL\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002388 break;
2389 }
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002390 min = min(i, (unsigned int)(bh->b_size -
2391 atomic_read(&bh->b_count)));
2392 memset(bh->b_data + atomic_read(&bh->b_count),
2393 0, min);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002394 atomic_add(min, &bh->b_count);
2395 i -= min;
2396 bh = bh->b_reqnext;
2397 }
2398 }
2399 (void) idetape_add_chrdev_write_request(drive, blocks);
2400 tape->merge_stage_size = 0;
2401 }
2402 idetape_wait_for_pipeline(drive);
2403 if (tape->merge_stage != NULL) {
2404 __idetape_kfree_stage(tape->merge_stage);
2405 tape->merge_stage = NULL;
2406 }
Borislav Petkov346331f2008-04-18 00:46:26 +02002407 clear_bit(IDETAPE_FLAG_PIPELINE_ERR, &tape->flags);
Borislav Petkov54abf372008-02-06 02:57:52 +01002408 tape->chrdev_dir = IDETAPE_DIR_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002409
2410 /*
Borislav Petkov3c98bf32008-02-06 02:57:53 +01002411 * On the next backup, perform the feedback loop again. (I don't want to
2412 * keep sense information between backups, as some systems are
2413 * constantly on, and the system load can be totally different on the
2414 * next backup).
Linus Torvalds1da177e2005-04-16 15:20:36 -07002415 */
2416 tape->max_stages = tape->min_pipeline;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002417 if (tape->first_stage != NULL ||
2418 tape->next_stage != NULL ||
2419 tape->last_stage != NULL ||
2420 tape->nr_stages != 0) {
2421 printk(KERN_ERR "ide-tape: ide-tape pipeline bug, "
2422 "first_stage %p, next_stage %p, "
2423 "last_stage %p, nr_stages %d\n",
2424 tape->first_stage, tape->next_stage,
2425 tape->last_stage, tape->nr_stages);
2426 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002427}
2428
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002429static void idetape_restart_speed_control(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002430{
2431 idetape_tape_t *tape = drive->driver_data;
2432
2433 tape->restart_speed_control_req = 0;
2434 tape->pipeline_head = 0;
Borislav Petkov41f81d542008-02-06 02:57:52 +01002435 tape->controlled_last_pipeline_head = 0;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002436 tape->controlled_previous_pipeline_head = 0;
2437 tape->uncontrolled_previous_pipeline_head = 0;
2438 tape->controlled_pipeline_head_speed = 5000;
2439 tape->pipeline_head_speed = 5000;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002440 tape->uncontrolled_pipeline_head_speed = 0;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002441 tape->controlled_pipeline_head_time =
2442 tape->uncontrolled_pipeline_head_time = jiffies;
2443 tape->controlled_previous_head_time =
2444 tape->uncontrolled_previous_head_time = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002445}
2446
Borislav Petkov8d06bfa2008-02-06 02:57:53 +01002447static int idetape_init_read(ide_drive_t *drive, int max_stages)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002448{
2449 idetape_tape_t *tape = drive->driver_data;
2450 idetape_stage_t *new_stage;
2451 struct request rq;
2452 int bytes_read;
Borislav Petkovb6422012008-02-02 19:56:49 +01002453 u16 blocks = *(u16 *)&tape->caps[12];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002454
2455 /* Initialize read operation */
Borislav Petkov54abf372008-02-06 02:57:52 +01002456 if (tape->chrdev_dir != IDETAPE_DIR_READ) {
2457 if (tape->chrdev_dir == IDETAPE_DIR_WRITE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002458 idetape_empty_write_pipeline(drive);
2459 idetape_flush_tape_buffers(drive);
2460 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002461 if (tape->merge_stage || tape->merge_stage_size) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002462 printk(KERN_ERR "ide-tape: merge_stage_size should be"
2463 " 0 now\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002464 tape->merge_stage_size = 0;
2465 }
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002466 tape->merge_stage = __idetape_kmalloc_stage(tape, 0, 0);
2467 if (!tape->merge_stage)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002468 return -ENOMEM;
Borislav Petkov54abf372008-02-06 02:57:52 +01002469 tape->chrdev_dir = IDETAPE_DIR_READ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002470
2471 /*
Borislav Petkov3c98bf32008-02-06 02:57:53 +01002472 * Issue a read 0 command to ensure that DSC handshake is
2473 * switched from completion mode to buffer available mode.
2474 * No point in issuing this if DSC overlap isn't supported, some
2475 * drives (Seagate STT3401A) will return an error.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002476 */
2477 if (drive->dsc_overlap) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002478 bytes_read = idetape_queue_rw_tail(drive,
2479 REQ_IDETAPE_READ, 0,
2480 tape->merge_stage->bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002481 if (bytes_read < 0) {
2482 __idetape_kfree_stage(tape->merge_stage);
2483 tape->merge_stage = NULL;
Borislav Petkov54abf372008-02-06 02:57:52 +01002484 tape->chrdev_dir = IDETAPE_DIR_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002485 return bytes_read;
2486 }
2487 }
2488 }
2489 if (tape->restart_speed_control_req)
2490 idetape_restart_speed_control(drive);
2491 idetape_init_rq(&rq, REQ_IDETAPE_READ);
Borislav Petkov54bb2072008-02-06 02:57:52 +01002492 rq.sector = tape->first_frame;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002493 rq.nr_sectors = blocks;
2494 rq.current_nr_sectors = blocks;
Borislav Petkov346331f2008-04-18 00:46:26 +02002495 if (!test_bit(IDETAPE_FLAG_PIPELINE_ERR, &tape->flags) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07002496 tape->nr_stages < max_stages) {
2497 new_stage = idetape_kmalloc_stage(tape);
2498 while (new_stage != NULL) {
2499 new_stage->rq = rq;
2500 idetape_add_stage_tail(drive, new_stage);
2501 if (tape->nr_stages >= max_stages)
2502 break;
2503 new_stage = idetape_kmalloc_stage(tape);
2504 }
2505 }
2506 if (!idetape_pipeline_active(tape)) {
2507 if (tape->nr_pending_stages >= 3 * max_stages / 4) {
2508 tape->measure_insert_time = 1;
2509 tape->insert_time = jiffies;
2510 tape->insert_size = 0;
2511 tape->insert_speed = 0;
Borislav Petkov8d06bfa2008-02-06 02:57:53 +01002512 idetape_plug_pipeline(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002513 }
2514 }
2515 return 0;
2516}
2517
2518/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +01002519 * Called from idetape_chrdev_read() to service a character device read request
2520 * and add read-ahead requests to our pipeline.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002521 */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002522static int idetape_add_chrdev_read_request(ide_drive_t *drive, int blocks)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002523{
2524 idetape_tape_t *tape = drive->driver_data;
2525 unsigned long flags;
2526 struct request *rq_ptr;
2527 int bytes_read;
2528
Borislav Petkov8004a8c2008-02-06 02:57:51 +01002529 debug_log(DBG_PROCS, "Enter %s, %d blocks\n", __func__, blocks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002530
Borislav Petkov3c98bf32008-02-06 02:57:53 +01002531 /* If we are at a filemark, return a read length of 0 */
Borislav Petkov346331f2008-04-18 00:46:26 +02002532 if (test_bit(IDETAPE_FLAG_FILEMARK, &tape->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002533 return 0;
2534
Borislav Petkov3c98bf32008-02-06 02:57:53 +01002535 /* Wait for the next block to reach the head of the pipeline. */
Borislav Petkov8d06bfa2008-02-06 02:57:53 +01002536 idetape_init_read(drive, tape->max_stages);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002537 if (tape->first_stage == NULL) {
Borislav Petkov346331f2008-04-18 00:46:26 +02002538 if (test_bit(IDETAPE_FLAG_PIPELINE_ERR, &tape->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002539 return 0;
Borislav Petkov54bb2072008-02-06 02:57:52 +01002540 return idetape_queue_rw_tail(drive, REQ_IDETAPE_READ, blocks,
2541 tape->merge_stage->bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002542 }
2543 idetape_wait_first_stage(drive);
2544 rq_ptr = &tape->first_stage->rq;
Borislav Petkov54bb2072008-02-06 02:57:52 +01002545 bytes_read = tape->blk_size * (rq_ptr->nr_sectors -
2546 rq_ptr->current_nr_sectors);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002547 rq_ptr->nr_sectors = 0;
2548 rq_ptr->current_nr_sectors = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002549
2550 if (rq_ptr->errors == IDETAPE_ERROR_EOD)
2551 return 0;
2552 else {
2553 idetape_switch_buffers(tape, tape->first_stage);
2554 if (rq_ptr->errors == IDETAPE_ERROR_FILEMARK)
Borislav Petkov346331f2008-04-18 00:46:26 +02002555 set_bit(IDETAPE_FLAG_FILEMARK, &tape->flags);
Borislav Petkov54bb2072008-02-06 02:57:52 +01002556 spin_lock_irqsave(&tape->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002557 idetape_remove_stage_head(drive);
Borislav Petkov54bb2072008-02-06 02:57:52 +01002558 spin_unlock_irqrestore(&tape->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002559 tape->pipeline_head++;
Borislav Petkov37016ba2008-02-06 02:57:52 +01002560 idetape_calculate_speeds(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002561 }
Borislav Petkov54bb2072008-02-06 02:57:52 +01002562 if (bytes_read > blocks * tape->blk_size) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002563 printk(KERN_ERR "ide-tape: bug: trying to return more bytes"
2564 " than requested\n");
Borislav Petkov54bb2072008-02-06 02:57:52 +01002565 bytes_read = blocks * tape->blk_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002566 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002567 return (bytes_read);
2568}
2569
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002570static void idetape_pad_zeros(ide_drive_t *drive, int bcount)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002571{
2572 idetape_tape_t *tape = drive->driver_data;
2573 struct idetape_bh *bh;
2574 int blocks;
Borislav Petkov3c98bf32008-02-06 02:57:53 +01002575
Linus Torvalds1da177e2005-04-16 15:20:36 -07002576 while (bcount) {
2577 unsigned int count;
2578
2579 bh = tape->merge_stage->bh;
2580 count = min(tape->stage_size, bcount);
2581 bcount -= count;
Borislav Petkov54bb2072008-02-06 02:57:52 +01002582 blocks = count / tape->blk_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002583 while (count) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002584 atomic_set(&bh->b_count,
2585 min(count, (unsigned int)bh->b_size));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002586 memset(bh->b_data, 0, atomic_read(&bh->b_count));
2587 count -= atomic_read(&bh->b_count);
2588 bh = bh->b_reqnext;
2589 }
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002590 idetape_queue_rw_tail(drive, REQ_IDETAPE_WRITE, blocks,
2591 tape->merge_stage->bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002592 }
2593}
2594
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002595static int idetape_pipeline_size(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002596{
2597 idetape_tape_t *tape = drive->driver_data;
2598 idetape_stage_t *stage;
2599 struct request *rq;
2600 int size = 0;
2601
2602 idetape_wait_for_pipeline(drive);
2603 stage = tape->first_stage;
2604 while (stage != NULL) {
2605 rq = &stage->rq;
Borislav Petkov54bb2072008-02-06 02:57:52 +01002606 size += tape->blk_size * (rq->nr_sectors -
2607 rq->current_nr_sectors);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002608 if (rq->errors == IDETAPE_ERROR_FILEMARK)
Borislav Petkov54bb2072008-02-06 02:57:52 +01002609 size += tape->blk_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002610 stage = stage->next;
2611 }
2612 size += tape->merge_stage_size;
2613 return size;
2614}
2615
2616/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +01002617 * Rewinds the tape to the Beginning Of the current Partition (BOP). We
2618 * currently support only one partition.
2619 */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002620static int idetape_rewind_tape(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002621{
2622 int retval;
2623 idetape_pc_t pc;
Borislav Petkov8004a8c2008-02-06 02:57:51 +01002624 idetape_tape_t *tape;
2625 tape = drive->driver_data;
2626
2627 debug_log(DBG_SENSE, "Enter %s\n", __func__);
2628
Linus Torvalds1da177e2005-04-16 15:20:36 -07002629 idetape_create_rewind_cmd(drive, &pc);
2630 retval = idetape_queue_pc_tail(drive, &pc);
2631 if (retval)
2632 return retval;
2633
2634 idetape_create_read_position_cmd(&pc);
2635 retval = idetape_queue_pc_tail(drive, &pc);
2636 if (retval)
2637 return retval;
2638 return 0;
2639}
2640
Borislav Petkov3c98bf32008-02-06 02:57:53 +01002641/* mtio.h compatible commands should be issued to the chrdev interface. */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002642static int idetape_blkdev_ioctl(ide_drive_t *drive, unsigned int cmd,
2643 unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002644{
2645 idetape_tape_t *tape = drive->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002646 void __user *argp = (void __user *)arg;
2647
Borislav Petkovd59823f2008-02-02 19:56:51 +01002648 struct idetape_config {
2649 int dsc_rw_frequency;
2650 int dsc_media_access_frequency;
2651 int nr_stages;
2652 } config;
2653
Borislav Petkov8004a8c2008-02-06 02:57:51 +01002654 debug_log(DBG_PROCS, "Enter %s\n", __func__);
2655
Linus Torvalds1da177e2005-04-16 15:20:36 -07002656 switch (cmd) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002657 case 0x0340:
2658 if (copy_from_user(&config, argp, sizeof(config)))
2659 return -EFAULT;
2660 tape->best_dsc_rw_freq = config.dsc_rw_frequency;
2661 tape->max_stages = config.nr_stages;
2662 break;
2663 case 0x0350:
2664 config.dsc_rw_frequency = (int) tape->best_dsc_rw_freq;
2665 config.nr_stages = tape->max_stages;
2666 if (copy_to_user(argp, &config, sizeof(config)))
2667 return -EFAULT;
2668 break;
2669 default:
2670 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002671 }
2672 return 0;
2673}
2674
2675/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +01002676 * The function below is now a bit more complicated than just passing the
2677 * command to the tape since we may have crossed some filemarks during our
2678 * pipelined read-ahead mode. As a minor side effect, the pipeline enables us to
2679 * support MTFSFM when the filemark is in our internal pipeline even if the tape
2680 * doesn't support spacing over filemarks in the reverse direction.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002681 */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002682static int idetape_space_over_filemarks(ide_drive_t *drive, short mt_op,
2683 int mt_count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002684{
2685 idetape_tape_t *tape = drive->driver_data;
2686 idetape_pc_t pc;
2687 unsigned long flags;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002688 int retval, count = 0;
Borislav Petkovb6422012008-02-02 19:56:49 +01002689 int sprev = !!(tape->caps[4] & 0x20);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002690
2691 if (mt_count == 0)
2692 return 0;
2693 if (MTBSF == mt_op || MTBSFM == mt_op) {
Borislav Petkovb6422012008-02-02 19:56:49 +01002694 if (!sprev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002695 return -EIO;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002696 mt_count = -mt_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002697 }
2698
Borislav Petkov54abf372008-02-06 02:57:52 +01002699 if (tape->chrdev_dir == IDETAPE_DIR_READ) {
Borislav Petkov3c98bf32008-02-06 02:57:53 +01002700 /* its a read-ahead buffer, scan it for crossed filemarks. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002701 tape->merge_stage_size = 0;
Borislav Petkov346331f2008-04-18 00:46:26 +02002702 if (test_and_clear_bit(IDETAPE_FLAG_FILEMARK, &tape->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002703 ++count;
2704 while (tape->first_stage != NULL) {
2705 if (count == mt_count) {
2706 if (mt_op == MTFSFM)
Borislav Petkov346331f2008-04-18 00:46:26 +02002707 set_bit(IDETAPE_FLAG_FILEMARK,
2708 &tape->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002709 return 0;
2710 }
Borislav Petkov54bb2072008-02-06 02:57:52 +01002711 spin_lock_irqsave(&tape->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002712 if (tape->first_stage == tape->active_stage) {
2713 /*
Borislav Petkov3c98bf32008-02-06 02:57:53 +01002714 * We have reached the active stage in the read
2715 * pipeline. There is no point in allowing the
2716 * drive to continue reading any farther, so we
2717 * stop the pipeline.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002718 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +01002719 * This section should be moved to a separate
2720 * subroutine because similar operations are
2721 * done in __idetape_discard_read_pipeline(),
2722 * for example.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002723 */
2724 tape->next_stage = NULL;
Borislav Petkov54bb2072008-02-06 02:57:52 +01002725 spin_unlock_irqrestore(&tape->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002726 idetape_wait_first_stage(drive);
2727 tape->next_stage = tape->first_stage->next;
2728 } else
Borislav Petkov54bb2072008-02-06 02:57:52 +01002729 spin_unlock_irqrestore(&tape->lock, flags);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002730 if (tape->first_stage->rq.errors ==
2731 IDETAPE_ERROR_FILEMARK)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002732 ++count;
2733 idetape_remove_stage_head(drive);
2734 }
2735 idetape_discard_read_pipeline(drive, 0);
2736 }
2737
2738 /*
Borislav Petkov3c98bf32008-02-06 02:57:53 +01002739 * The filemark was not found in our internal pipeline; now we can issue
2740 * the space command.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002741 */
2742 switch (mt_op) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002743 case MTFSF:
2744 case MTBSF:
2745 idetape_create_space_cmd(&pc, mt_count - count,
2746 IDETAPE_SPACE_OVER_FILEMARK);
2747 return idetape_queue_pc_tail(drive, &pc);
2748 case MTFSFM:
2749 case MTBSFM:
2750 if (!sprev)
2751 return -EIO;
2752 retval = idetape_space_over_filemarks(drive, MTFSF,
2753 mt_count - count);
2754 if (retval)
2755 return retval;
2756 count = (MTBSFM == mt_op ? 1 : -1);
2757 return idetape_space_over_filemarks(drive, MTFSF, count);
2758 default:
2759 printk(KERN_ERR "ide-tape: MTIO operation %d not supported\n",
2760 mt_op);
2761 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002762 }
2763}
2764
Linus Torvalds1da177e2005-04-16 15:20:36 -07002765/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +01002766 * Our character device read / write functions.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002767 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +01002768 * The tape is optimized to maximize throughput when it is transferring an
2769 * integral number of the "continuous transfer limit", which is a parameter of
2770 * the specific tape (26kB on my particular tape, 32kB for Onstream).
Linus Torvalds1da177e2005-04-16 15:20:36 -07002771 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +01002772 * As of version 1.3 of the driver, the character device provides an abstract
2773 * continuous view of the media - any mix of block sizes (even 1 byte) on the
2774 * same backup/restore procedure is supported. The driver will internally
2775 * convert the requests to the recommended transfer unit, so that an unmatch
2776 * between the user's block size to the recommended size will only result in a
2777 * (slightly) increased driver overhead, but will no longer hit performance.
2778 * This is not applicable to Onstream.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002779 */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002780static ssize_t idetape_chrdev_read(struct file *file, char __user *buf,
2781 size_t count, loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002782{
2783 struct ide_tape_obj *tape = ide_tape_f(file);
2784 ide_drive_t *drive = tape->drive;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002785 ssize_t bytes_read, temp, actually_read = 0, rc;
Daniel Walkerdcd96372006-06-25 05:47:37 -07002786 ssize_t ret = 0;
Borislav Petkovb6422012008-02-02 19:56:49 +01002787 u16 ctl = *(u16 *)&tape->caps[12];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002788
Borislav Petkov8004a8c2008-02-06 02:57:51 +01002789 debug_log(DBG_CHRDEV, "Enter %s, count %Zd\n", __func__, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002790
Borislav Petkov54abf372008-02-06 02:57:52 +01002791 if (tape->chrdev_dir != IDETAPE_DIR_READ) {
Borislav Petkov346331f2008-04-18 00:46:26 +02002792 if (test_bit(IDETAPE_FLAG_DETECT_BS, &tape->flags))
Borislav Petkov54bb2072008-02-06 02:57:52 +01002793 if (count > tape->blk_size &&
2794 (count % tape->blk_size) == 0)
2795 tape->user_bs_factor = count / tape->blk_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002796 }
Borislav Petkov8d06bfa2008-02-06 02:57:53 +01002797 rc = idetape_init_read(drive, tape->max_stages);
2798 if (rc < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002799 return rc;
2800 if (count == 0)
2801 return (0);
2802 if (tape->merge_stage_size) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002803 actually_read = min((unsigned int)(tape->merge_stage_size),
2804 (unsigned int)count);
2805 if (idetape_copy_stage_to_user(tape, buf, tape->merge_stage,
2806 actually_read))
Daniel Walkerdcd96372006-06-25 05:47:37 -07002807 ret = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002808 buf += actually_read;
2809 tape->merge_stage_size -= actually_read;
2810 count -= actually_read;
2811 }
2812 while (count >= tape->stage_size) {
Borislav Petkovb6422012008-02-02 19:56:49 +01002813 bytes_read = idetape_add_chrdev_read_request(drive, ctl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002814 if (bytes_read <= 0)
2815 goto finish;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002816 if (idetape_copy_stage_to_user(tape, buf, tape->merge_stage,
2817 bytes_read))
Daniel Walkerdcd96372006-06-25 05:47:37 -07002818 ret = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002819 buf += bytes_read;
2820 count -= bytes_read;
2821 actually_read += bytes_read;
2822 }
2823 if (count) {
Borislav Petkovb6422012008-02-02 19:56:49 +01002824 bytes_read = idetape_add_chrdev_read_request(drive, ctl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002825 if (bytes_read <= 0)
2826 goto finish;
2827 temp = min((unsigned long)count, (unsigned long)bytes_read);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002828 if (idetape_copy_stage_to_user(tape, buf, tape->merge_stage,
2829 temp))
Daniel Walkerdcd96372006-06-25 05:47:37 -07002830 ret = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002831 actually_read += temp;
2832 tape->merge_stage_size = bytes_read-temp;
2833 }
2834finish:
Borislav Petkov346331f2008-04-18 00:46:26 +02002835 if (!actually_read && test_bit(IDETAPE_FLAG_FILEMARK, &tape->flags)) {
Borislav Petkov8004a8c2008-02-06 02:57:51 +01002836 debug_log(DBG_SENSE, "%s: spacing over filemark\n", tape->name);
2837
Linus Torvalds1da177e2005-04-16 15:20:36 -07002838 idetape_space_over_filemarks(drive, MTFSF, 1);
2839 return 0;
2840 }
Daniel Walkerdcd96372006-06-25 05:47:37 -07002841
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002842 return ret ? ret : actually_read;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002843}
2844
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002845static ssize_t idetape_chrdev_write(struct file *file, const char __user *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002846 size_t count, loff_t *ppos)
2847{
2848 struct ide_tape_obj *tape = ide_tape_f(file);
2849 ide_drive_t *drive = tape->drive;
Daniel Walkerdcd96372006-06-25 05:47:37 -07002850 ssize_t actually_written = 0;
2851 ssize_t ret = 0;
Borislav Petkovb6422012008-02-02 19:56:49 +01002852 u16 ctl = *(u16 *)&tape->caps[12];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002853
2854 /* The drive is write protected. */
2855 if (tape->write_prot)
2856 return -EACCES;
2857
Borislav Petkov8004a8c2008-02-06 02:57:51 +01002858 debug_log(DBG_CHRDEV, "Enter %s, count %Zd\n", __func__, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002859
2860 /* Initialize write operation */
Borislav Petkov54abf372008-02-06 02:57:52 +01002861 if (tape->chrdev_dir != IDETAPE_DIR_WRITE) {
2862 if (tape->chrdev_dir == IDETAPE_DIR_READ)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002863 idetape_discard_read_pipeline(drive, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002864 if (tape->merge_stage || tape->merge_stage_size) {
2865 printk(KERN_ERR "ide-tape: merge_stage_size "
2866 "should be 0 now\n");
2867 tape->merge_stage_size = 0;
2868 }
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002869 tape->merge_stage = __idetape_kmalloc_stage(tape, 0, 0);
2870 if (!tape->merge_stage)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002871 return -ENOMEM;
Borislav Petkov54abf372008-02-06 02:57:52 +01002872 tape->chrdev_dir = IDETAPE_DIR_WRITE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002873 idetape_init_merge_stage(tape);
2874
2875 /*
Borislav Petkov3c98bf32008-02-06 02:57:53 +01002876 * Issue a write 0 command to ensure that DSC handshake is
2877 * switched from completion mode to buffer available mode. No
2878 * point in issuing this if DSC overlap isn't supported, some
2879 * drives (Seagate STT3401A) will return an error.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002880 */
2881 if (drive->dsc_overlap) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002882 ssize_t retval = idetape_queue_rw_tail(drive,
2883 REQ_IDETAPE_WRITE, 0,
2884 tape->merge_stage->bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002885 if (retval < 0) {
2886 __idetape_kfree_stage(tape->merge_stage);
2887 tape->merge_stage = NULL;
Borislav Petkov54abf372008-02-06 02:57:52 +01002888 tape->chrdev_dir = IDETAPE_DIR_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002889 return retval;
2890 }
2891 }
2892 }
2893 if (count == 0)
2894 return (0);
2895 if (tape->restart_speed_control_req)
2896 idetape_restart_speed_control(drive);
2897 if (tape->merge_stage_size) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002898 if (tape->merge_stage_size >= tape->stage_size) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002899 printk(KERN_ERR "ide-tape: bug: merge buf too big\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002900 tape->merge_stage_size = 0;
2901 }
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002902 actually_written = min((unsigned int)
2903 (tape->stage_size - tape->merge_stage_size),
2904 (unsigned int)count);
2905 if (idetape_copy_stage_from_user(tape, tape->merge_stage, buf,
2906 actually_written))
Daniel Walkerdcd96372006-06-25 05:47:37 -07002907 ret = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002908 buf += actually_written;
2909 tape->merge_stage_size += actually_written;
2910 count -= actually_written;
2911
2912 if (tape->merge_stage_size == tape->stage_size) {
Daniel Walkerdcd96372006-06-25 05:47:37 -07002913 ssize_t retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002914 tape->merge_stage_size = 0;
Borislav Petkovb6422012008-02-02 19:56:49 +01002915 retval = idetape_add_chrdev_write_request(drive, ctl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002916 if (retval <= 0)
2917 return (retval);
2918 }
2919 }
2920 while (count >= tape->stage_size) {
Daniel Walkerdcd96372006-06-25 05:47:37 -07002921 ssize_t retval;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002922 if (idetape_copy_stage_from_user(tape, tape->merge_stage, buf,
2923 tape->stage_size))
Daniel Walkerdcd96372006-06-25 05:47:37 -07002924 ret = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002925 buf += tape->stage_size;
2926 count -= tape->stage_size;
Borislav Petkovb6422012008-02-02 19:56:49 +01002927 retval = idetape_add_chrdev_write_request(drive, ctl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002928 actually_written += tape->stage_size;
2929 if (retval <= 0)
2930 return (retval);
2931 }
2932 if (count) {
2933 actually_written += count;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002934 if (idetape_copy_stage_from_user(tape, tape->merge_stage, buf,
2935 count))
Daniel Walkerdcd96372006-06-25 05:47:37 -07002936 ret = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002937 tape->merge_stage_size += count;
2938 }
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002939 return ret ? ret : actually_written;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002940}
2941
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002942static int idetape_write_filemark(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002943{
2944 idetape_pc_t pc;
2945
2946 /* Write a filemark */
2947 idetape_create_write_filemark_cmd(drive, &pc, 1);
2948 if (idetape_queue_pc_tail(drive, &pc)) {
2949 printk(KERN_ERR "ide-tape: Couldn't write a filemark\n");
2950 return -EIO;
2951 }
2952 return 0;
2953}
2954
2955/*
Borislav Petkovd99c9da2008-02-02 19:56:51 +01002956 * Called from idetape_chrdev_ioctl when the general mtio MTIOCTOP ioctl is
2957 * requested.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002958 *
Borislav Petkovd99c9da2008-02-02 19:56:51 +01002959 * Note: MTBSF and MTBSFM are not supported when the tape doesn't support
2960 * spacing over filemarks in the reverse direction. In this case, MTFSFM is also
2961 * usually not supported (it is supported in the rare case in which we crossed
2962 * the filemark during our read-ahead pipelined operation mode).
Linus Torvalds1da177e2005-04-16 15:20:36 -07002963 *
Borislav Petkovd99c9da2008-02-02 19:56:51 +01002964 * The following commands are currently not supported:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002965 *
Borislav Petkovd99c9da2008-02-02 19:56:51 +01002966 * MTFSS, MTBSS, MTWSM, MTSETDENSITY, MTSETDRVBUFFER, MT_ST_BOOLEANS,
2967 * MT_ST_WRITE_THRESHOLD.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002968 */
Borislav Petkovd99c9da2008-02-02 19:56:51 +01002969static int idetape_mtioctop(ide_drive_t *drive, short mt_op, int mt_count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002970{
2971 idetape_tape_t *tape = drive->driver_data;
2972 idetape_pc_t pc;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002973 int i, retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002974
Borislav Petkov8004a8c2008-02-06 02:57:51 +01002975 debug_log(DBG_ERR, "Handling MTIOCTOP ioctl: mt_op=%d, mt_count=%d\n",
2976 mt_op, mt_count);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002977
Borislav Petkov3c98bf32008-02-06 02:57:53 +01002978 /* Commands which need our pipelined read-ahead stages. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002979 switch (mt_op) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002980 case MTFSF:
2981 case MTFSFM:
2982 case MTBSF:
2983 case MTBSFM:
2984 if (!mt_count)
2985 return 0;
2986 return idetape_space_over_filemarks(drive, mt_op, mt_count);
2987 default:
2988 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002989 }
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002990
Linus Torvalds1da177e2005-04-16 15:20:36 -07002991 switch (mt_op) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002992 case MTWEOF:
2993 if (tape->write_prot)
2994 return -EACCES;
2995 idetape_discard_read_pipeline(drive, 1);
2996 for (i = 0; i < mt_count; i++) {
2997 retval = idetape_write_filemark(drive);
2998 if (retval)
2999 return retval;
3000 }
3001 return 0;
3002 case MTREW:
3003 idetape_discard_read_pipeline(drive, 0);
3004 if (idetape_rewind_tape(drive))
3005 return -EIO;
3006 return 0;
3007 case MTLOAD:
3008 idetape_discard_read_pipeline(drive, 0);
3009 idetape_create_load_unload_cmd(drive, &pc,
3010 IDETAPE_LU_LOAD_MASK);
3011 return idetape_queue_pc_tail(drive, &pc);
3012 case MTUNLOAD:
3013 case MTOFFL:
3014 /*
3015 * If door is locked, attempt to unlock before
3016 * attempting to eject.
3017 */
3018 if (tape->door_locked) {
3019 if (idetape_create_prevent_cmd(drive, &pc, 0))
3020 if (!idetape_queue_pc_tail(drive, &pc))
3021 tape->door_locked = DOOR_UNLOCKED;
3022 }
3023 idetape_discard_read_pipeline(drive, 0);
3024 idetape_create_load_unload_cmd(drive, &pc,
3025 !IDETAPE_LU_LOAD_MASK);
3026 retval = idetape_queue_pc_tail(drive, &pc);
3027 if (!retval)
Borislav Petkov346331f2008-04-18 00:46:26 +02003028 clear_bit(IDETAPE_FLAG_MEDIUM_PRESENT, &tape->flags);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01003029 return retval;
3030 case MTNOP:
3031 idetape_discard_read_pipeline(drive, 0);
3032 return idetape_flush_tape_buffers(drive);
3033 case MTRETEN:
3034 idetape_discard_read_pipeline(drive, 0);
3035 idetape_create_load_unload_cmd(drive, &pc,
3036 IDETAPE_LU_RETENSION_MASK | IDETAPE_LU_LOAD_MASK);
3037 return idetape_queue_pc_tail(drive, &pc);
3038 case MTEOM:
3039 idetape_create_space_cmd(&pc, 0, IDETAPE_SPACE_TO_EOD);
3040 return idetape_queue_pc_tail(drive, &pc);
3041 case MTERASE:
3042 (void)idetape_rewind_tape(drive);
3043 idetape_create_erase_cmd(&pc);
3044 return idetape_queue_pc_tail(drive, &pc);
3045 case MTSETBLK:
3046 if (mt_count) {
3047 if (mt_count < tape->blk_size ||
3048 mt_count % tape->blk_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003049 return -EIO;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01003050 tape->user_bs_factor = mt_count / tape->blk_size;
Borislav Petkov346331f2008-04-18 00:46:26 +02003051 clear_bit(IDETAPE_FLAG_DETECT_BS, &tape->flags);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01003052 } else
Borislav Petkov346331f2008-04-18 00:46:26 +02003053 set_bit(IDETAPE_FLAG_DETECT_BS, &tape->flags);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01003054 return 0;
3055 case MTSEEK:
3056 idetape_discard_read_pipeline(drive, 0);
3057 return idetape_position_tape(drive,
3058 mt_count * tape->user_bs_factor, tape->partition, 0);
3059 case MTSETPART:
3060 idetape_discard_read_pipeline(drive, 0);
3061 return idetape_position_tape(drive, 0, mt_count, 0);
3062 case MTFSR:
3063 case MTBSR:
3064 case MTLOCK:
3065 if (!idetape_create_prevent_cmd(drive, &pc, 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003066 return 0;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01003067 retval = idetape_queue_pc_tail(drive, &pc);
3068 if (retval)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003069 return retval;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01003070 tape->door_locked = DOOR_EXPLICITLY_LOCKED;
3071 return 0;
3072 case MTUNLOCK:
3073 if (!idetape_create_prevent_cmd(drive, &pc, 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003074 return 0;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01003075 retval = idetape_queue_pc_tail(drive, &pc);
3076 if (retval)
3077 return retval;
3078 tape->door_locked = DOOR_UNLOCKED;
3079 return 0;
3080 default:
3081 printk(KERN_ERR "ide-tape: MTIO operation %d not supported\n",
3082 mt_op);
3083 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003084 }
3085}
3086
3087/*
Borislav Petkovd99c9da2008-02-02 19:56:51 +01003088 * Our character device ioctls. General mtio.h magnetic io commands are
3089 * supported here, and not in the corresponding block interface. Our own
3090 * ide-tape ioctls are supported on both interfaces.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003091 */
Borislav Petkovd99c9da2008-02-02 19:56:51 +01003092static int idetape_chrdev_ioctl(struct inode *inode, struct file *file,
3093 unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003094{
3095 struct ide_tape_obj *tape = ide_tape_f(file);
3096 ide_drive_t *drive = tape->drive;
3097 struct mtop mtop;
3098 struct mtget mtget;
3099 struct mtpos mtpos;
Borislav Petkov54bb2072008-02-06 02:57:52 +01003100 int block_offset = 0, position = tape->first_frame;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003101 void __user *argp = (void __user *)arg;
3102
Borislav Petkov8004a8c2008-02-06 02:57:51 +01003103 debug_log(DBG_CHRDEV, "Enter %s, cmd=%u\n", __func__, cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003104
3105 tape->restart_speed_control_req = 1;
Borislav Petkov54abf372008-02-06 02:57:52 +01003106 if (tape->chrdev_dir == IDETAPE_DIR_WRITE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003107 idetape_empty_write_pipeline(drive);
3108 idetape_flush_tape_buffers(drive);
3109 }
3110 if (cmd == MTIOCGET || cmd == MTIOCPOS) {
Borislav Petkov54bb2072008-02-06 02:57:52 +01003111 block_offset = idetape_pipeline_size(drive) /
3112 (tape->blk_size * tape->user_bs_factor);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01003113 position = idetape_read_position(drive);
3114 if (position < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003115 return -EIO;
3116 }
3117 switch (cmd) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01003118 case MTIOCTOP:
3119 if (copy_from_user(&mtop, argp, sizeof(struct mtop)))
3120 return -EFAULT;
3121 return idetape_mtioctop(drive, mtop.mt_op, mtop.mt_count);
3122 case MTIOCGET:
3123 memset(&mtget, 0, sizeof(struct mtget));
3124 mtget.mt_type = MT_ISSCSI2;
3125 mtget.mt_blkno = position / tape->user_bs_factor - block_offset;
3126 mtget.mt_dsreg =
3127 ((tape->blk_size * tape->user_bs_factor)
3128 << MT_ST_BLKSIZE_SHIFT) & MT_ST_BLKSIZE_MASK;
Borislav Petkov54bb2072008-02-06 02:57:52 +01003129
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01003130 if (tape->drv_write_prot)
3131 mtget.mt_gstat |= GMT_WR_PROT(0xffffffff);
3132
3133 if (copy_to_user(argp, &mtget, sizeof(struct mtget)))
3134 return -EFAULT;
3135 return 0;
3136 case MTIOCPOS:
3137 mtpos.mt_blkno = position / tape->user_bs_factor - block_offset;
3138 if (copy_to_user(argp, &mtpos, sizeof(struct mtpos)))
3139 return -EFAULT;
3140 return 0;
3141 default:
3142 if (tape->chrdev_dir == IDETAPE_DIR_READ)
3143 idetape_discard_read_pipeline(drive, 1);
3144 return idetape_blkdev_ioctl(drive, cmd, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003145 }
3146}
3147
Borislav Petkov3cffb9c2008-02-02 19:56:50 +01003148/*
3149 * Do a mode sense page 0 with block descriptor and if it succeeds set the tape
3150 * block size with the reported value.
3151 */
3152static void ide_tape_get_bsize_from_bdesc(ide_drive_t *drive)
3153{
3154 idetape_tape_t *tape = drive->driver_data;
3155 idetape_pc_t pc;
3156
3157 idetape_create_mode_sense_cmd(&pc, IDETAPE_BLOCK_DESCRIPTOR);
3158 if (idetape_queue_pc_tail(drive, &pc)) {
3159 printk(KERN_ERR "ide-tape: Can't get block descriptor\n");
Borislav Petkov54bb2072008-02-06 02:57:52 +01003160 if (tape->blk_size == 0) {
Borislav Petkov3cffb9c2008-02-02 19:56:50 +01003161 printk(KERN_WARNING "ide-tape: Cannot deal with zero "
3162 "block size, assuming 32k\n");
Borislav Petkov54bb2072008-02-06 02:57:52 +01003163 tape->blk_size = 32768;
Borislav Petkov3cffb9c2008-02-02 19:56:50 +01003164 }
3165 return;
3166 }
Borislav Petkov54bb2072008-02-06 02:57:52 +01003167 tape->blk_size = (pc.buffer[4 + 5] << 16) +
Borislav Petkov3cffb9c2008-02-02 19:56:50 +01003168 (pc.buffer[4 + 6] << 8) +
3169 pc.buffer[4 + 7];
3170 tape->drv_write_prot = (pc.buffer[2] & 0x80) >> 7;
3171}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003172
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01003173static int idetape_chrdev_open(struct inode *inode, struct file *filp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003174{
3175 unsigned int minor = iminor(inode), i = minor & ~0xc0;
3176 ide_drive_t *drive;
3177 idetape_tape_t *tape;
3178 idetape_pc_t pc;
3179 int retval;
3180
Borislav Petkov8004a8c2008-02-06 02:57:51 +01003181 if (i >= MAX_HWIFS * MAX_DRIVES)
3182 return -ENXIO;
3183
3184 tape = ide_tape_chrdev_get(i);
3185 if (!tape)
3186 return -ENXIO;
3187
3188 debug_log(DBG_CHRDEV, "Enter %s\n", __func__);
3189
Linus Torvalds1da177e2005-04-16 15:20:36 -07003190 /*
3191 * We really want to do nonseekable_open(inode, filp); here, but some
3192 * versions of tar incorrectly call lseek on tapes and bail out if that
3193 * fails. So we disallow pread() and pwrite(), but permit lseeks.
3194 */
3195 filp->f_mode &= ~(FMODE_PREAD | FMODE_PWRITE);
3196
Linus Torvalds1da177e2005-04-16 15:20:36 -07003197 drive = tape->drive;
3198
3199 filp->private_data = tape;
3200
Borislav Petkov346331f2008-04-18 00:46:26 +02003201 if (test_and_set_bit(IDETAPE_FLAG_BUSY, &tape->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003202 retval = -EBUSY;
3203 goto out_put_tape;
3204 }
3205
3206 retval = idetape_wait_ready(drive, 60 * HZ);
3207 if (retval) {
Borislav Petkov346331f2008-04-18 00:46:26 +02003208 clear_bit(IDETAPE_FLAG_BUSY, &tape->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003209 printk(KERN_ERR "ide-tape: %s: drive not ready\n", tape->name);
3210 goto out_put_tape;
3211 }
3212
3213 idetape_read_position(drive);
Borislav Petkov346331f2008-04-18 00:46:26 +02003214 if (!test_bit(IDETAPE_FLAG_ADDRESS_VALID, &tape->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003215 (void)idetape_rewind_tape(drive);
3216
Borislav Petkov54abf372008-02-06 02:57:52 +01003217 if (tape->chrdev_dir != IDETAPE_DIR_READ)
Borislav Petkov346331f2008-04-18 00:46:26 +02003218 clear_bit(IDETAPE_FLAG_PIPELINE_ERR, &tape->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003219
3220 /* Read block size and write protect status from drive. */
Borislav Petkov3cffb9c2008-02-02 19:56:50 +01003221 ide_tape_get_bsize_from_bdesc(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003222
3223 /* Set write protect flag if device is opened as read-only. */
3224 if ((filp->f_flags & O_ACCMODE) == O_RDONLY)
3225 tape->write_prot = 1;
3226 else
3227 tape->write_prot = tape->drv_write_prot;
3228
3229 /* Make sure drive isn't write protected if user wants to write. */
3230 if (tape->write_prot) {
3231 if ((filp->f_flags & O_ACCMODE) == O_WRONLY ||
3232 (filp->f_flags & O_ACCMODE) == O_RDWR) {
Borislav Petkov346331f2008-04-18 00:46:26 +02003233 clear_bit(IDETAPE_FLAG_BUSY, &tape->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003234 retval = -EROFS;
3235 goto out_put_tape;
3236 }
3237 }
3238
Borislav Petkov3c98bf32008-02-06 02:57:53 +01003239 /* Lock the tape drive door so user can't eject. */
Borislav Petkov54abf372008-02-06 02:57:52 +01003240 if (tape->chrdev_dir == IDETAPE_DIR_NONE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003241 if (idetape_create_prevent_cmd(drive, &pc, 1)) {
3242 if (!idetape_queue_pc_tail(drive, &pc)) {
3243 if (tape->door_locked != DOOR_EXPLICITLY_LOCKED)
3244 tape->door_locked = DOOR_LOCKED;
3245 }
3246 }
3247 }
3248 idetape_restart_speed_control(drive);
3249 tape->restart_speed_control_req = 0;
3250 return 0;
3251
3252out_put_tape:
3253 ide_tape_put(tape);
3254 return retval;
3255}
3256
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01003257static void idetape_write_release(ide_drive_t *drive, unsigned int minor)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003258{
3259 idetape_tape_t *tape = drive->driver_data;
3260
3261 idetape_empty_write_pipeline(drive);
3262 tape->merge_stage = __idetape_kmalloc_stage(tape, 1, 0);
3263 if (tape->merge_stage != NULL) {
Borislav Petkov54bb2072008-02-06 02:57:52 +01003264 idetape_pad_zeros(drive, tape->blk_size *
3265 (tape->user_bs_factor - 1));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003266 __idetape_kfree_stage(tape->merge_stage);
3267 tape->merge_stage = NULL;
3268 }
3269 idetape_write_filemark(drive);
3270 idetape_flush_tape_buffers(drive);
3271 idetape_flush_tape_buffers(drive);
3272}
3273
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01003274static int idetape_chrdev_release(struct inode *inode, struct file *filp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003275{
3276 struct ide_tape_obj *tape = ide_tape_f(filp);
3277 ide_drive_t *drive = tape->drive;
3278 idetape_pc_t pc;
3279 unsigned int minor = iminor(inode);
3280
3281 lock_kernel();
3282 tape = drive->driver_data;
Borislav Petkov8004a8c2008-02-06 02:57:51 +01003283
3284 debug_log(DBG_CHRDEV, "Enter %s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003285
Borislav Petkov54abf372008-02-06 02:57:52 +01003286 if (tape->chrdev_dir == IDETAPE_DIR_WRITE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003287 idetape_write_release(drive, minor);
Borislav Petkov54abf372008-02-06 02:57:52 +01003288 if (tape->chrdev_dir == IDETAPE_DIR_READ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003289 if (minor < 128)
3290 idetape_discard_read_pipeline(drive, 1);
3291 else
3292 idetape_wait_for_pipeline(drive);
3293 }
3294 if (tape->cache_stage != NULL) {
3295 __idetape_kfree_stage(tape->cache_stage);
3296 tape->cache_stage = NULL;
3297 }
Borislav Petkov346331f2008-04-18 00:46:26 +02003298 if (minor < 128 && test_bit(IDETAPE_FLAG_MEDIUM_PRESENT, &tape->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003299 (void) idetape_rewind_tape(drive);
Borislav Petkov54abf372008-02-06 02:57:52 +01003300 if (tape->chrdev_dir == IDETAPE_DIR_NONE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003301 if (tape->door_locked == DOOR_LOCKED) {
3302 if (idetape_create_prevent_cmd(drive, &pc, 0)) {
3303 if (!idetape_queue_pc_tail(drive, &pc))
3304 tape->door_locked = DOOR_UNLOCKED;
3305 }
3306 }
3307 }
Borislav Petkov346331f2008-04-18 00:46:26 +02003308 clear_bit(IDETAPE_FLAG_BUSY, &tape->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003309 ide_tape_put(tape);
3310 unlock_kernel();
3311 return 0;
3312}
3313
3314/*
Borislav Petkov71071b82008-02-06 02:57:53 +01003315 * check the contents of the ATAPI IDENTIFY command results. We return:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003316 *
Borislav Petkov71071b82008-02-06 02:57:53 +01003317 * 1 - If the tape can be supported by us, based on the information we have so
3318 * far.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003319 *
Borislav Petkov71071b82008-02-06 02:57:53 +01003320 * 0 - If this tape driver is not currently supported by us.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003321 */
Borislav Petkov71071b82008-02-06 02:57:53 +01003322static int idetape_identify_device(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003323{
Borislav Petkov71071b82008-02-06 02:57:53 +01003324 u8 gcw[2], protocol, device_type, removable, packet_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003325
3326 if (drive->id_read == 0)
3327 return 1;
3328
Borislav Petkov71071b82008-02-06 02:57:53 +01003329 *((unsigned short *) &gcw) = drive->id->config;
3330
3331 protocol = (gcw[1] & 0xC0) >> 6;
3332 device_type = gcw[1] & 0x1F;
3333 removable = !!(gcw[0] & 0x80);
3334 packet_size = gcw[0] & 0x3;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003335
Linus Torvalds1da177e2005-04-16 15:20:36 -07003336 /* Check that we can support this device */
Borislav Petkov71071b82008-02-06 02:57:53 +01003337 if (protocol != 2)
Bartlomiej Zolnierkiewicz16422de32008-02-02 19:56:48 +01003338 printk(KERN_ERR "ide-tape: Protocol (0x%02x) is not ATAPI\n",
Borislav Petkov71071b82008-02-06 02:57:53 +01003339 protocol);
3340 else if (device_type != 1)
Bartlomiej Zolnierkiewicz16422de32008-02-02 19:56:48 +01003341 printk(KERN_ERR "ide-tape: Device type (0x%02x) is not set "
Borislav Petkov71071b82008-02-06 02:57:53 +01003342 "to tape\n", device_type);
3343 else if (!removable)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003344 printk(KERN_ERR "ide-tape: The removable flag is not set\n");
Borislav Petkov71071b82008-02-06 02:57:53 +01003345 else if (packet_size != 0) {
Borislav Petkov24d57f82008-02-06 02:57:54 +01003346 printk(KERN_ERR "ide-tape: Packet size (0x%02x) is not 12"
3347 " bytes\n", packet_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003348 } else
3349 return 1;
3350 return 0;
3351}
3352
Borislav Petkov6d29c8f2008-02-02 19:56:49 +01003353static void idetape_get_inquiry_results(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003354{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003355 idetape_tape_t *tape = drive->driver_data;
3356 idetape_pc_t pc;
Borislav Petkov41f81d542008-02-06 02:57:52 +01003357 char fw_rev[6], vendor_id[10], product_id[18];
Borislav Petkov6d29c8f2008-02-02 19:56:49 +01003358
Linus Torvalds1da177e2005-04-16 15:20:36 -07003359 idetape_create_inquiry_cmd(&pc);
3360 if (idetape_queue_pc_tail(drive, &pc)) {
Borislav Petkov6d29c8f2008-02-02 19:56:49 +01003361 printk(KERN_ERR "ide-tape: %s: can't get INQUIRY results\n",
3362 tape->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003363 return;
3364 }
Borislav Petkov41f81d542008-02-06 02:57:52 +01003365 memcpy(vendor_id, &pc.buffer[8], 8);
3366 memcpy(product_id, &pc.buffer[16], 16);
3367 memcpy(fw_rev, &pc.buffer[32], 4);
Borislav Petkov6d29c8f2008-02-02 19:56:49 +01003368
Borislav Petkov41f81d542008-02-06 02:57:52 +01003369 ide_fixstring(vendor_id, 10, 0);
3370 ide_fixstring(product_id, 18, 0);
3371 ide_fixstring(fw_rev, 6, 0);
3372
Borislav Petkov6d29c8f2008-02-02 19:56:49 +01003373 printk(KERN_INFO "ide-tape: %s <-> %s: %s %s rev %s\n",
Borislav Petkov41f81d542008-02-06 02:57:52 +01003374 drive->name, tape->name, vendor_id, product_id, fw_rev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003375}
3376
3377/*
Borislav Petkovb6422012008-02-02 19:56:49 +01003378 * Ask the tape about its various parameters. In particular, we will adjust our
3379 * data transfer buffer size to the recommended value as returned by the tape.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003380 */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01003381static void idetape_get_mode_sense_results(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003382{
3383 idetape_tape_t *tape = drive->driver_data;
3384 idetape_pc_t pc;
Borislav Petkovb6422012008-02-02 19:56:49 +01003385 u8 *caps;
3386 u8 speed, max_speed;
Borislav Petkov47314fa2008-02-02 19:56:48 +01003387
Linus Torvalds1da177e2005-04-16 15:20:36 -07003388 idetape_create_mode_sense_cmd(&pc, IDETAPE_CAPABILITIES_PAGE);
3389 if (idetape_queue_pc_tail(drive, &pc)) {
Borislav Petkovb6422012008-02-02 19:56:49 +01003390 printk(KERN_ERR "ide-tape: Can't get tape parameters - assuming"
3391 " some default values\n");
Borislav Petkov54bb2072008-02-06 02:57:52 +01003392 tape->blk_size = 512;
Borislav Petkovb6422012008-02-02 19:56:49 +01003393 put_unaligned(52, (u16 *)&tape->caps[12]);
3394 put_unaligned(540, (u16 *)&tape->caps[14]);
3395 put_unaligned(6*52, (u16 *)&tape->caps[16]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003396 return;
3397 }
Borislav Petkovb6422012008-02-02 19:56:49 +01003398 caps = pc.buffer + 4 + pc.buffer[3];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003399
Borislav Petkovb6422012008-02-02 19:56:49 +01003400 /* convert to host order and save for later use */
3401 speed = be16_to_cpu(*(u16 *)&caps[14]);
3402 max_speed = be16_to_cpu(*(u16 *)&caps[8]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003403
Borislav Petkovb6422012008-02-02 19:56:49 +01003404 put_unaligned(max_speed, (u16 *)&caps[8]);
3405 put_unaligned(be16_to_cpu(*(u16 *)&caps[12]), (u16 *)&caps[12]);
3406 put_unaligned(speed, (u16 *)&caps[14]);
3407 put_unaligned(be16_to_cpu(*(u16 *)&caps[16]), (u16 *)&caps[16]);
3408
3409 if (!speed) {
3410 printk(KERN_INFO "ide-tape: %s: invalid tape speed "
3411 "(assuming 650KB/sec)\n", drive->name);
3412 put_unaligned(650, (u16 *)&caps[14]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003413 }
Borislav Petkovb6422012008-02-02 19:56:49 +01003414 if (!max_speed) {
3415 printk(KERN_INFO "ide-tape: %s: invalid max_speed "
3416 "(assuming 650KB/sec)\n", drive->name);
3417 put_unaligned(650, (u16 *)&caps[8]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003418 }
3419
Borislav Petkovb6422012008-02-02 19:56:49 +01003420 memcpy(&tape->caps, caps, 20);
3421 if (caps[7] & 0x02)
Borislav Petkov54bb2072008-02-06 02:57:52 +01003422 tape->blk_size = 512;
Borislav Petkovb6422012008-02-02 19:56:49 +01003423 else if (caps[7] & 0x04)
Borislav Petkov54bb2072008-02-06 02:57:52 +01003424 tape->blk_size = 1024;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003425}
3426
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +02003427#ifdef CONFIG_IDE_PROC_FS
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01003428static void idetape_add_settings(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003429{
3430 idetape_tape_t *tape = drive->driver_data;
3431
Borislav Petkovb6422012008-02-02 19:56:49 +01003432 ide_add_setting(drive, "buffer", SETTING_READ, TYPE_SHORT, 0, 0xffff,
3433 1, 2, (u16 *)&tape->caps[16], NULL);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01003434 ide_add_setting(drive, "pipeline_min", SETTING_RW, TYPE_INT, 1, 0xffff,
3435 tape->stage_size / 1024, 1, &tape->min_pipeline, NULL);
3436 ide_add_setting(drive, "pipeline", SETTING_RW, TYPE_INT, 1, 0xffff,
3437 tape->stage_size / 1024, 1, &tape->max_stages, NULL);
3438 ide_add_setting(drive, "pipeline_max", SETTING_RW, TYPE_INT, 1, 0xffff,
3439 tape->stage_size / 1024, 1, &tape->max_pipeline, NULL);
3440 ide_add_setting(drive, "pipeline_used", SETTING_READ, TYPE_INT, 0,
3441 0xffff, tape->stage_size / 1024, 1, &tape->nr_stages,
3442 NULL);
3443 ide_add_setting(drive, "pipeline_pending", SETTING_READ, TYPE_INT, 0,
3444 0xffff, tape->stage_size / 1024, 1,
3445 &tape->nr_pending_stages, NULL);
Borislav Petkovb6422012008-02-02 19:56:49 +01003446 ide_add_setting(drive, "speed", SETTING_READ, TYPE_SHORT, 0, 0xffff,
3447 1, 1, (u16 *)&tape->caps[14], NULL);
Borislav Petkov54bb2072008-02-06 02:57:52 +01003448 ide_add_setting(drive, "stage", SETTING_READ, TYPE_INT, 0, 0xffff, 1,
3449 1024, &tape->stage_size, NULL);
3450 ide_add_setting(drive, "tdsc", SETTING_RW, TYPE_INT, IDETAPE_DSC_RW_MIN,
3451 IDETAPE_DSC_RW_MAX, 1000, HZ, &tape->best_dsc_rw_freq,
3452 NULL);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01003453 ide_add_setting(drive, "dsc_overlap", SETTING_RW, TYPE_BYTE, 0, 1, 1,
3454 1, &drive->dsc_overlap, NULL);
3455 ide_add_setting(drive, "pipeline_head_speed_c", SETTING_READ, TYPE_INT,
3456 0, 0xffff, 1, 1, &tape->controlled_pipeline_head_speed,
3457 NULL);
3458 ide_add_setting(drive, "pipeline_head_speed_u", SETTING_READ, TYPE_INT,
3459 0, 0xffff, 1, 1,
3460 &tape->uncontrolled_pipeline_head_speed, NULL);
3461 ide_add_setting(drive, "avg_speed", SETTING_READ, TYPE_INT, 0, 0xffff,
3462 1, 1, &tape->avg_speed, NULL);
Borislav Petkov8004a8c2008-02-06 02:57:51 +01003463 ide_add_setting(drive, "debug_mask", SETTING_RW, TYPE_INT, 0, 0xffff, 1,
3464 1, &tape->debug_mask, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003465}
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +02003466#else
3467static inline void idetape_add_settings(ide_drive_t *drive) { ; }
3468#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003469
3470/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +01003471 * The function below is called to:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003472 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +01003473 * 1. Initialize our various state variables.
3474 * 2. Ask the tape for its capabilities.
3475 * 3. Allocate a buffer which will be used for data transfer. The buffer size
3476 * is chosen based on the recommendation which we received in step 2.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003477 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +01003478 * Note that at this point ide.c already assigned us an irq, so that we can
3479 * queue requests here and wait for their completion.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003480 */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01003481static void idetape_setup(ide_drive_t *drive, idetape_tape_t *tape, int minor)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003482{
3483 unsigned long t1, tmid, tn, t;
3484 int speed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003485 int stage_size;
Borislav Petkov71071b82008-02-06 02:57:53 +01003486 u8 gcw[2];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003487 struct sysinfo si;
Borislav Petkovb6422012008-02-02 19:56:49 +01003488 u16 *ctl = (u16 *)&tape->caps[12];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003489
Borislav Petkov54bb2072008-02-06 02:57:52 +01003490 spin_lock_init(&tape->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003491 drive->dsc_overlap = 1;
Bartlomiej Zolnierkiewicz4166c192008-02-01 23:09:30 +01003492 if (drive->hwif->host_flags & IDE_HFLAG_NO_DSC) {
3493 printk(KERN_INFO "ide-tape: %s: disabling DSC overlap\n",
3494 tape->name);
3495 drive->dsc_overlap = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003496 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003497 /* Seagate Travan drives do not support DSC overlap. */
3498 if (strstr(drive->id->model, "Seagate STT3401"))
3499 drive->dsc_overlap = 0;
3500 tape->minor = minor;
3501 tape->name[0] = 'h';
3502 tape->name[1] = 't';
3503 tape->name[2] = '0' + minor;
Borislav Petkov54abf372008-02-06 02:57:52 +01003504 tape->chrdev_dir = IDETAPE_DIR_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003505 tape->pc = tape->pc_stack;
3506 tape->max_insert_speed = 10000;
3507 tape->speed_control = 1;
3508 *((unsigned short *) &gcw) = drive->id->config;
Borislav Petkov71071b82008-02-06 02:57:53 +01003509
3510 /* Command packet DRQ type */
3511 if (((gcw[0] & 0x60) >> 5) == 1)
Borislav Petkov346331f2008-04-18 00:46:26 +02003512 set_bit(IDETAPE_FLAG_DRQ_INTERRUPT, &tape->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003513
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01003514 tape->min_pipeline = 10;
3515 tape->max_pipeline = 10;
3516 tape->max_stages = 10;
Borislav Petkov3c98bf32008-02-06 02:57:53 +01003517
Linus Torvalds1da177e2005-04-16 15:20:36 -07003518 idetape_get_inquiry_results(drive);
3519 idetape_get_mode_sense_results(drive);
Borislav Petkov3cffb9c2008-02-02 19:56:50 +01003520 ide_tape_get_bsize_from_bdesc(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003521 tape->user_bs_factor = 1;
Borislav Petkov54bb2072008-02-06 02:57:52 +01003522 tape->stage_size = *ctl * tape->blk_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003523 while (tape->stage_size > 0xffff) {
3524 printk(KERN_NOTICE "ide-tape: decreasing stage size\n");
Borislav Petkovb6422012008-02-02 19:56:49 +01003525 *ctl /= 2;
Borislav Petkov54bb2072008-02-06 02:57:52 +01003526 tape->stage_size = *ctl * tape->blk_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003527 }
3528 stage_size = tape->stage_size;
3529 tape->pages_per_stage = stage_size / PAGE_SIZE;
3530 if (stage_size % PAGE_SIZE) {
3531 tape->pages_per_stage++;
3532 tape->excess_bh_size = PAGE_SIZE - stage_size % PAGE_SIZE;
3533 }
3534
Borislav Petkovb6422012008-02-02 19:56:49 +01003535 /* Select the "best" DSC read/write polling freq and pipeline size. */
3536 speed = max(*(u16 *)&tape->caps[14], *(u16 *)&tape->caps[8]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003537
3538 tape->max_stages = speed * 1000 * 10 / tape->stage_size;
3539
Borislav Petkov3c98bf32008-02-06 02:57:53 +01003540 /* Limit memory use for pipeline to 10% of physical memory */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003541 si_meminfo(&si);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01003542 if (tape->max_stages * tape->stage_size >
3543 si.totalram * si.mem_unit / 10)
3544 tape->max_stages =
3545 si.totalram * si.mem_unit / (10 * tape->stage_size);
3546
Linus Torvalds1da177e2005-04-16 15:20:36 -07003547 tape->max_stages = min(tape->max_stages, IDETAPE_MAX_PIPELINE_STAGES);
3548 tape->min_pipeline = min(tape->max_stages, IDETAPE_MIN_PIPELINE_STAGES);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01003549 tape->max_pipeline =
3550 min(tape->max_stages * 2, IDETAPE_MAX_PIPELINE_STAGES);
3551 if (tape->max_stages == 0) {
3552 tape->max_stages = 1;
3553 tape->min_pipeline = 1;
3554 tape->max_pipeline = 1;
3555 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003556
3557 t1 = (tape->stage_size * HZ) / (speed * 1000);
Borislav Petkovb6422012008-02-02 19:56:49 +01003558 tmid = (*(u16 *)&tape->caps[16] * 32 * HZ) / (speed * 125);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003559 tn = (IDETAPE_FIFO_THRESHOLD * tape->stage_size * HZ) / (speed * 1000);
3560
3561 if (tape->max_stages)
3562 t = tn;
3563 else
3564 t = t1;
3565
3566 /*
Borislav Petkov3c98bf32008-02-06 02:57:53 +01003567 * Ensure that the number we got makes sense; limit it within
3568 * IDETAPE_DSC_RW_MIN and IDETAPE_DSC_RW_MAX.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003569 */
Borislav Petkov54bb2072008-02-06 02:57:52 +01003570 tape->best_dsc_rw_freq = max_t(unsigned long,
3571 min_t(unsigned long, t, IDETAPE_DSC_RW_MAX),
3572 IDETAPE_DSC_RW_MIN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003573 printk(KERN_INFO "ide-tape: %s <-> %s: %dKBps, %d*%dkB buffer, "
3574 "%dkB pipeline, %lums tDSC%s\n",
Borislav Petkovb6422012008-02-02 19:56:49 +01003575 drive->name, tape->name, *(u16 *)&tape->caps[14],
3576 (*(u16 *)&tape->caps[16] * 512) / tape->stage_size,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003577 tape->stage_size / 1024,
3578 tape->max_stages * tape->stage_size / 1024,
Borislav Petkov54bb2072008-02-06 02:57:52 +01003579 tape->best_dsc_rw_freq * 1000 / HZ,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003580 drive->using_dma ? ", DMA":"");
3581
3582 idetape_add_settings(drive);
3583}
3584
Russell King4031bbe2006-01-06 11:41:00 +00003585static void ide_tape_remove(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003586{
3587 idetape_tape_t *tape = drive->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003588
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +02003589 ide_proc_unregister_driver(drive, tape->driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003590
3591 ide_unregister_region(tape->disk);
3592
3593 ide_tape_put(tape);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003594}
3595
3596static void ide_tape_release(struct kref *kref)
3597{
3598 struct ide_tape_obj *tape = to_ide_tape(kref);
3599 ide_drive_t *drive = tape->drive;
3600 struct gendisk *g = tape->disk;
3601
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02003602 BUG_ON(tape->first_stage != NULL || tape->merge_stage_size);
3603
Linus Torvalds1da177e2005-04-16 15:20:36 -07003604 drive->dsc_overlap = 0;
3605 drive->driver_data = NULL;
Tony Jonesdbc12722007-09-25 02:03:03 +02003606 device_destroy(idetape_sysfs_class, MKDEV(IDETAPE_MAJOR, tape->minor));
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01003607 device_destroy(idetape_sysfs_class,
3608 MKDEV(IDETAPE_MAJOR, tape->minor + 128));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003609 idetape_devs[tape->minor] = NULL;
3610 g->private_data = NULL;
3611 put_disk(g);
3612 kfree(tape);
3613}
3614
Bartlomiej Zolnierkiewiczecfd80e2007-05-10 00:01:09 +02003615#ifdef CONFIG_IDE_PROC_FS
Linus Torvalds1da177e2005-04-16 15:20:36 -07003616static int proc_idetape_read_name
3617 (char *page, char **start, off_t off, int count, int *eof, void *data)
3618{
3619 ide_drive_t *drive = (ide_drive_t *) data;
3620 idetape_tape_t *tape = drive->driver_data;
3621 char *out = page;
3622 int len;
3623
3624 len = sprintf(out, "%s\n", tape->name);
3625 PROC_IDE_READ_RETURN(page, start, off, count, eof, len);
3626}
3627
3628static ide_proc_entry_t idetape_proc[] = {
3629 { "capacity", S_IFREG|S_IRUGO, proc_ide_read_capacity, NULL },
3630 { "name", S_IFREG|S_IRUGO, proc_idetape_read_name, NULL },
3631 { NULL, 0, NULL, NULL }
3632};
Linus Torvalds1da177e2005-04-16 15:20:36 -07003633#endif
3634
Russell King4031bbe2006-01-06 11:41:00 +00003635static int ide_tape_probe(ide_drive_t *);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003636
Linus Torvalds1da177e2005-04-16 15:20:36 -07003637static ide_driver_t idetape_driver = {
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02003638 .gen_driver = {
Laurent Riffard4ef3b8f2005-11-18 22:15:40 +01003639 .owner = THIS_MODULE,
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02003640 .name = "ide-tape",
3641 .bus = &ide_bus_type,
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02003642 },
Russell King4031bbe2006-01-06 11:41:00 +00003643 .probe = ide_tape_probe,
3644 .remove = ide_tape_remove,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003645 .version = IDETAPE_VERSION,
3646 .media = ide_tape,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003647 .supports_dsc_overlap = 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003648 .do_request = idetape_do_request,
3649 .end_request = idetape_end_request,
3650 .error = __ide_error,
3651 .abort = __ide_abort,
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +02003652#ifdef CONFIG_IDE_PROC_FS
Linus Torvalds1da177e2005-04-16 15:20:36 -07003653 .proc = idetape_proc,
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +02003654#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003655};
3656
Borislav Petkov3c98bf32008-02-06 02:57:53 +01003657/* Our character device supporting functions, passed to register_chrdev. */
Arjan van de Ven2b8693c2007-02-12 00:55:32 -08003658static const struct file_operations idetape_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003659 .owner = THIS_MODULE,
3660 .read = idetape_chrdev_read,
3661 .write = idetape_chrdev_write,
3662 .ioctl = idetape_chrdev_ioctl,
3663 .open = idetape_chrdev_open,
3664 .release = idetape_chrdev_release,
3665};
3666
3667static int idetape_open(struct inode *inode, struct file *filp)
3668{
3669 struct gendisk *disk = inode->i_bdev->bd_disk;
3670 struct ide_tape_obj *tape;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003671
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01003672 tape = ide_tape_get(disk);
3673 if (!tape)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003674 return -ENXIO;
3675
Linus Torvalds1da177e2005-04-16 15:20:36 -07003676 return 0;
3677}
3678
3679static int idetape_release(struct inode *inode, struct file *filp)
3680{
3681 struct gendisk *disk = inode->i_bdev->bd_disk;
3682 struct ide_tape_obj *tape = ide_tape_g(disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003683
3684 ide_tape_put(tape);
3685
3686 return 0;
3687}
3688
3689static int idetape_ioctl(struct inode *inode, struct file *file,
3690 unsigned int cmd, unsigned long arg)
3691{
3692 struct block_device *bdev = inode->i_bdev;
3693 struct ide_tape_obj *tape = ide_tape_g(bdev->bd_disk);
3694 ide_drive_t *drive = tape->drive;
3695 int err = generic_ide_ioctl(drive, file, bdev, cmd, arg);
3696 if (err == -EINVAL)
3697 err = idetape_blkdev_ioctl(drive, cmd, arg);
3698 return err;
3699}
3700
3701static struct block_device_operations idetape_block_ops = {
3702 .owner = THIS_MODULE,
3703 .open = idetape_open,
3704 .release = idetape_release,
3705 .ioctl = idetape_ioctl,
3706};
3707
Russell King4031bbe2006-01-06 11:41:00 +00003708static int ide_tape_probe(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003709{
3710 idetape_tape_t *tape;
3711 struct gendisk *g;
3712 int minor;
3713
3714 if (!strstr("ide-tape", drive->driver_req))
3715 goto failed;
3716 if (!drive->present)
3717 goto failed;
3718 if (drive->media != ide_tape)
3719 goto failed;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01003720 if (!idetape_identify_device(drive)) {
3721 printk(KERN_ERR "ide-tape: %s: not supported by this version of"
3722 " the driver\n", drive->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003723 goto failed;
3724 }
3725 if (drive->scsi) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01003726 printk(KERN_INFO "ide-tape: passing drive %s to ide-scsi"
3727 " emulation.\n", drive->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003728 goto failed;
3729 }
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01003730 tape = kzalloc(sizeof(idetape_tape_t), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003731 if (tape == NULL) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01003732 printk(KERN_ERR "ide-tape: %s: Can't allocate a tape struct\n",
3733 drive->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003734 goto failed;
3735 }
3736
3737 g = alloc_disk(1 << PARTN_BITS);
3738 if (!g)
3739 goto out_free_tape;
3740
3741 ide_init_disk(g, drive);
3742
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +02003743 ide_proc_register_driver(drive, &idetape_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003744
Linus Torvalds1da177e2005-04-16 15:20:36 -07003745 kref_init(&tape->kref);
3746
3747 tape->drive = drive;
3748 tape->driver = &idetape_driver;
3749 tape->disk = g;
3750
3751 g->private_data = &tape->driver;
3752
3753 drive->driver_data = tape;
3754
Arjan van de Vencf8b8972006-03-23 03:00:45 -08003755 mutex_lock(&idetape_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003756 for (minor = 0; idetape_devs[minor]; minor++)
3757 ;
3758 idetape_devs[minor] = tape;
Arjan van de Vencf8b8972006-03-23 03:00:45 -08003759 mutex_unlock(&idetape_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003760
3761 idetape_setup(drive, tape, minor);
3762
Tony Jonesdbc12722007-09-25 02:03:03 +02003763 device_create(idetape_sysfs_class, &drive->gendev,
3764 MKDEV(IDETAPE_MAJOR, minor), "%s", tape->name);
3765 device_create(idetape_sysfs_class, &drive->gendev,
3766 MKDEV(IDETAPE_MAJOR, minor + 128), "n%s", tape->name);
Will Dysond5dee802005-09-16 02:55:07 -07003767
Linus Torvalds1da177e2005-04-16 15:20:36 -07003768 g->fops = &idetape_block_ops;
3769 ide_register_region(g);
3770
3771 return 0;
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02003772
Linus Torvalds1da177e2005-04-16 15:20:36 -07003773out_free_tape:
3774 kfree(tape);
3775failed:
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02003776 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003777}
3778
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01003779static void __exit idetape_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003780{
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02003781 driver_unregister(&idetape_driver.gen_driver);
Will Dysond5dee802005-09-16 02:55:07 -07003782 class_destroy(idetape_sysfs_class);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003783 unregister_chrdev(IDETAPE_MAJOR, "ht");
3784}
3785
Bartlomiej Zolnierkiewicz17514e82005-11-19 22:24:35 +01003786static int __init idetape_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003787{
Will Dysond5dee802005-09-16 02:55:07 -07003788 int error = 1;
3789 idetape_sysfs_class = class_create(THIS_MODULE, "ide_tape");
3790 if (IS_ERR(idetape_sysfs_class)) {
3791 idetape_sysfs_class = NULL;
3792 printk(KERN_ERR "Unable to create sysfs class for ide tapes\n");
3793 error = -EBUSY;
3794 goto out;
3795 }
3796
Linus Torvalds1da177e2005-04-16 15:20:36 -07003797 if (register_chrdev(IDETAPE_MAJOR, "ht", &idetape_fops)) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01003798 printk(KERN_ERR "ide-tape: Failed to register chrdev"
3799 " interface\n");
Will Dysond5dee802005-09-16 02:55:07 -07003800 error = -EBUSY;
3801 goto out_free_class;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003802 }
Will Dysond5dee802005-09-16 02:55:07 -07003803
3804 error = driver_register(&idetape_driver.gen_driver);
3805 if (error)
3806 goto out_free_driver;
3807
3808 return 0;
3809
3810out_free_driver:
3811 driver_unregister(&idetape_driver.gen_driver);
3812out_free_class:
3813 class_destroy(idetape_sysfs_class);
3814out:
3815 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003816}
3817
Kay Sievers263756e2005-12-12 18:03:44 +01003818MODULE_ALIAS("ide:*m-tape*");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003819module_init(idetape_init);
3820module_exit(idetape_exit);
3821MODULE_ALIAS_CHARDEV_MAJOR(IDETAPE_MAJOR);
Borislav Petkov9c145762008-02-06 02:57:54 +01003822MODULE_DESCRIPTION("ATAPI Streaming TAPE Driver");
3823MODULE_LICENSE("GPL");