blob: 3f9dcca6f092680e3829332932a570ad64c8aaa5 [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
Borislav Petkov346331f2008-04-18 00:46:26 +0200184/* Packet command flag bits. */
185enum {
186 /* Set when an error is considered normal - We won't retry */
187 PC_FLAG_ABORT = (1 << 0),
188 /* 1 When polling for DSC on a media access command */
189 PC_FLAG_WAIT_FOR_DSC = (1 << 1),
190 /* 1 when we prefer to use DMA if possible */
191 PC_FLAG_DMA_RECOMMENDED = (1 << 2),
192 /* 1 while DMA in progress */
193 PC_FLAG_DMA_IN_PROGRESS = (1 << 3),
194 /* 1 when encountered problem during DMA */
195 PC_FLAG_DMA_ERROR = (1 << 4),
196 /* Data direction */
197 PC_FLAG_WRITING = (1 << 5),
198};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199
Borislav Petkov03056b92008-04-18 00:46:26 +0200200/* Tape door status */
201#define DOOR_UNLOCKED 0
202#define DOOR_LOCKED 1
203#define DOOR_EXPLICITLY_LOCKED 2
204
205/* Some defines for the SPACE command */
206#define IDETAPE_SPACE_OVER_FILEMARK 1
207#define IDETAPE_SPACE_TO_EOD 3
208
209/* Some defines for the LOAD UNLOAD command */
210#define IDETAPE_LU_LOAD_MASK 1
211#define IDETAPE_LU_RETENSION_MASK 2
212#define IDETAPE_LU_EOT_MASK 4
213
214/*
215 * Special requests for our block device strategy routine.
216 *
217 * In order to service a character device command, we add special requests to
218 * the tail of our block device request queue and wait for their completion.
219 */
220
221enum {
222 REQ_IDETAPE_PC1 = (1 << 0), /* packet command (first stage) */
223 REQ_IDETAPE_PC2 = (1 << 1), /* packet command (second stage) */
224 REQ_IDETAPE_READ = (1 << 2),
225 REQ_IDETAPE_WRITE = (1 << 3),
226};
227
228/* Error codes returned in rq->errors to the higher part of the driver. */
229#define IDETAPE_ERROR_GENERAL 101
230#define IDETAPE_ERROR_FILEMARK 102
231#define IDETAPE_ERROR_EOD 103
232
233/* Structures related to the SELECT SENSE / MODE SENSE packet commands. */
234#define IDETAPE_BLOCK_DESCRIPTOR 0
235#define IDETAPE_CAPABILITIES_PAGE 0x2a
236
Borislav Petkov346331f2008-04-18 00:46:26 +0200237/* Tape flag bits values. */
238enum {
239 IDETAPE_FLAG_IGNORE_DSC = (1 << 0),
240 /* 0 When the tape position is unknown */
241 IDETAPE_FLAG_ADDRESS_VALID = (1 << 1),
242 /* Device already opened */
243 IDETAPE_FLAG_BUSY = (1 << 2),
244 /* Error detected in a pipeline stage */
245 IDETAPE_FLAG_PIPELINE_ERR = (1 << 3),
246 /* Attempt to auto-detect the current user block size */
247 IDETAPE_FLAG_DETECT_BS = (1 << 4),
248 /* Currently on a filemark */
249 IDETAPE_FLAG_FILEMARK = (1 << 5),
250 /* DRQ interrupt device */
251 IDETAPE_FLAG_DRQ_INTERRUPT = (1 << 6),
252 /* pipeline active */
253 IDETAPE_FLAG_PIPELINE_ACTIVE = (1 << 7),
254 /* 0 = no tape is loaded, so we don't rewind after ejecting */
255 IDETAPE_FLAG_MEDIUM_PRESENT = (1 << 8),
256};
257
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100258/* A pipeline stage. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259typedef struct idetape_stage_s {
260 struct request rq; /* The corresponding request */
261 struct idetape_bh *bh; /* The data buffers */
262 struct idetape_stage_s *next; /* Pointer to the next stage */
263} idetape_stage_t;
264
265/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100266 * Most of our global data which we need to save even as we leave the driver due
267 * to an interrupt or a timer event is stored in the struct defined below.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 */
269typedef struct ide_tape_obj {
270 ide_drive_t *drive;
271 ide_driver_t *driver;
272 struct gendisk *disk;
273 struct kref kref;
274
275 /*
276 * Since a typical character device operation requires more
277 * than one packet command, we provide here enough memory
278 * for the maximum of interconnected packet commands.
279 * The packet commands are stored in the circular array pc_stack.
280 * pc_stack_index points to the last used entry, and warps around
281 * to the start when we get to the last array entry.
282 *
283 * pc points to the current processed packet command.
284 *
285 * failed_pc points to the last failed packet command, or contains
286 * NULL if we do not need to retry any packet command. This is
287 * required since an additional packet command is needed before the
288 * retry, to get detailed information on what went wrong.
289 */
290 /* Current packet command */
Borislav Petkovd236d742008-04-18 00:46:27 +0200291 struct ide_atapi_pc *pc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 /* Last failed packet command */
Borislav Petkovd236d742008-04-18 00:46:27 +0200293 struct ide_atapi_pc *failed_pc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 /* Packet command stack */
Borislav Petkovd236d742008-04-18 00:46:27 +0200295 struct ide_atapi_pc pc_stack[IDETAPE_PC_STACK];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 /* Next free packet command storage space */
297 int pc_stack_index;
298 struct request rq_stack[IDETAPE_PC_STACK];
299 /* We implement a circular array */
300 int rq_stack_index;
301
302 /*
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100303 * DSC polling variables.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100305 * While polling for DSC we use postponed_rq to postpone the current
306 * request so that ide.c will be able to service pending requests on the
307 * other device. Note that at most we will have only one DSC (usually
308 * data transfer) request in the device request queue. Additional
309 * requests can be queued in our internal pipeline, but they will be
310 * visible to ide.c only one at a time.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 */
312 struct request *postponed_rq;
313 /* The time in which we started polling for DSC */
314 unsigned long dsc_polling_start;
315 /* Timer used to poll for dsc */
316 struct timer_list dsc_timer;
317 /* Read/Write dsc polling frequency */
Borislav Petkov54bb2072008-02-06 02:57:52 +0100318 unsigned long best_dsc_rw_freq;
319 unsigned long dsc_poll_freq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 unsigned long dsc_timeout;
321
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100322 /* Read position information */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 u8 partition;
324 /* Current block */
Borislav Petkov54bb2072008-02-06 02:57:52 +0100325 unsigned int first_frame;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100327 /* Last error information */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328 u8 sense_key, asc, ascq;
329
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100330 /* Character device operation */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 unsigned int minor;
332 /* device name */
333 char name[4];
334 /* Current character device data transfer direction */
Borislav Petkov54abf372008-02-06 02:57:52 +0100335 u8 chrdev_dir;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336
Borislav Petkov54bb2072008-02-06 02:57:52 +0100337 /* tape block size, usually 512 or 1024 bytes */
338 unsigned short blk_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 int user_bs_factor;
Borislav Petkovb6422012008-02-02 19:56:49 +0100340
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 /* Copy of the tape's Capabilities and Mechanical Page */
Borislav Petkovb6422012008-02-02 19:56:49 +0100342 u8 caps[20];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343
344 /*
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100345 * Active data transfer request parameters.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100347 * At most, there is only one ide-tape originated data transfer request
348 * in the device request queue. This allows ide.c to easily service
349 * requests from the other device when we postpone our active request.
350 * In the pipelined operation mode, we use our internal pipeline
351 * structure to hold more data requests. The data buffer size is chosen
352 * based on the tape's recommendation.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 */
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100354 /* ptr to the request which is waiting in the device request queue */
Borislav Petkov54bb2072008-02-06 02:57:52 +0100355 struct request *active_data_rq;
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100356 /* Data buffer size chosen based on the tape's recommendation */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 int stage_size;
358 idetape_stage_t *merge_stage;
359 int merge_stage_size;
360 struct idetape_bh *bh;
361 char *b_data;
362 int b_count;
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100363
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 /*
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100365 * Pipeline parameters.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100367 * To accomplish non-pipelined mode, we simply set the following
368 * variables to zero (or NULL, where appropriate).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 */
370 /* Number of currently used stages */
371 int nr_stages;
372 /* Number of pending stages */
373 int nr_pending_stages;
374 /* We will not allocate more than this number of stages */
375 int max_stages, min_pipeline, max_pipeline;
376 /* The first stage which will be removed from the pipeline */
377 idetape_stage_t *first_stage;
378 /* The currently active stage */
379 idetape_stage_t *active_stage;
380 /* Will be serviced after the currently active request */
381 idetape_stage_t *next_stage;
382 /* New requests will be added to the pipeline here */
383 idetape_stage_t *last_stage;
384 /* Optional free stage which we can use */
385 idetape_stage_t *cache_stage;
386 int pages_per_stage;
387 /* Wasted space in each stage */
388 int excess_bh_size;
389
390 /* Status/Action flags: long for set_bit */
391 unsigned long flags;
392 /* protects the ide-tape queue */
Borislav Petkov54bb2072008-02-06 02:57:52 +0100393 spinlock_t lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100395 /* Measures average tape speed */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 unsigned long avg_time;
397 int avg_size;
398 int avg_speed;
399
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 /* the door is currently locked */
401 int door_locked;
402 /* the tape hardware is write protected */
403 char drv_write_prot;
404 /* the tape is write protected (hardware or opened as read-only) */
405 char write_prot;
406
407 /*
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100408 * Limit the number of times a request can be postponed, to avoid an
409 * infinite postpone deadlock.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 int postpone_cnt;
412
413 /*
414 * Measures number of frames:
415 *
416 * 1. written/read to/from the driver pipeline (pipeline_head).
417 * 2. written/read to/from the tape buffers (idetape_bh).
418 * 3. written/read by the tape to/from the media (tape_head).
419 */
420 int pipeline_head;
421 int buffer_head;
422 int tape_head;
423 int last_tape_head;
424
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100425 /* Speed control at the tape buffers input/output */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 unsigned long insert_time;
427 int insert_size;
428 int insert_speed;
429 int max_insert_speed;
430 int measure_insert_time;
431
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100432 /* Speed regulation negative feedback loop */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 int speed_control;
434 int pipeline_head_speed;
435 int controlled_pipeline_head_speed;
436 int uncontrolled_pipeline_head_speed;
437 int controlled_last_pipeline_head;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 unsigned long uncontrolled_pipeline_head_time;
439 unsigned long controlled_pipeline_head_time;
440 int controlled_previous_pipeline_head;
441 int uncontrolled_previous_pipeline_head;
442 unsigned long controlled_previous_head_time;
443 unsigned long uncontrolled_previous_head_time;
444 int restart_speed_control_req;
445
Borislav Petkov8004a8c2008-02-06 02:57:51 +0100446 u32 debug_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447} idetape_tape_t;
448
Arjan van de Vencf8b8972006-03-23 03:00:45 -0800449static DEFINE_MUTEX(idetape_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450
Will Dysond5dee802005-09-16 02:55:07 -0700451static struct class *idetape_sysfs_class;
452
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453#define to_ide_tape(obj) container_of(obj, struct ide_tape_obj, kref)
454
455#define ide_tape_g(disk) \
456 container_of((disk)->private_data, struct ide_tape_obj, driver)
457
458static struct ide_tape_obj *ide_tape_get(struct gendisk *disk)
459{
460 struct ide_tape_obj *tape = NULL;
461
Arjan van de Vencf8b8972006-03-23 03:00:45 -0800462 mutex_lock(&idetape_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 tape = ide_tape_g(disk);
464 if (tape)
465 kref_get(&tape->kref);
Arjan van de Vencf8b8972006-03-23 03:00:45 -0800466 mutex_unlock(&idetape_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 return tape;
468}
469
470static void ide_tape_release(struct kref *);
471
472static void ide_tape_put(struct ide_tape_obj *tape)
473{
Arjan van de Vencf8b8972006-03-23 03:00:45 -0800474 mutex_lock(&idetape_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 kref_put(&tape->kref, ide_tape_release);
Arjan van de Vencf8b8972006-03-23 03:00:45 -0800476 mutex_unlock(&idetape_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477}
478
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100480 * The variables below are used for the character device interface. Additional
481 * state variables are defined in our ide_drive_t structure.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100483static struct ide_tape_obj *idetape_devs[MAX_HWIFS * MAX_DRIVES];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484
485#define ide_tape_f(file) ((file)->private_data)
486
487static struct ide_tape_obj *ide_tape_chrdev_get(unsigned int i)
488{
489 struct ide_tape_obj *tape = NULL;
490
Arjan van de Vencf8b8972006-03-23 03:00:45 -0800491 mutex_lock(&idetape_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 tape = idetape_devs[i];
493 if (tape)
494 kref_get(&tape->kref);
Arjan van de Vencf8b8972006-03-23 03:00:45 -0800495 mutex_unlock(&idetape_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 return tape;
497}
498
Borislav Petkovd236d742008-04-18 00:46:27 +0200499static void idetape_input_buffers(ide_drive_t *drive, struct ide_atapi_pc *pc,
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100500 unsigned int bcount)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501{
502 struct idetape_bh *bh = pc->bh;
503 int count;
504
505 while (bcount) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 if (bh == NULL) {
507 printk(KERN_ERR "ide-tape: bh == NULL in "
508 "idetape_input_buffers\n");
Bartlomiej Zolnierkiewicz7616c0a2008-04-18 00:46:26 +0200509 ide_atapi_discard_data(drive, bcount);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 return;
511 }
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100512 count = min(
513 (unsigned int)(bh->b_size - atomic_read(&bh->b_count)),
514 bcount);
515 HWIF(drive)->atapi_input_bytes(drive, bh->b_data +
516 atomic_read(&bh->b_count), count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 bcount -= count;
518 atomic_add(count, &bh->b_count);
519 if (atomic_read(&bh->b_count) == bh->b_size) {
520 bh = bh->b_reqnext;
521 if (bh)
522 atomic_set(&bh->b_count, 0);
523 }
524 }
525 pc->bh = bh;
526}
527
Borislav Petkovd236d742008-04-18 00:46:27 +0200528static void idetape_output_buffers(ide_drive_t *drive, struct ide_atapi_pc *pc,
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100529 unsigned int bcount)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530{
531 struct idetape_bh *bh = pc->bh;
532 int count;
533
534 while (bcount) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 if (bh == NULL) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100536 printk(KERN_ERR "ide-tape: bh == NULL in %s\n",
537 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 return;
539 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 count = min((unsigned int)pc->b_count, (unsigned int)bcount);
541 HWIF(drive)->atapi_output_bytes(drive, pc->b_data, count);
542 bcount -= count;
543 pc->b_data += count;
544 pc->b_count -= count;
545 if (!pc->b_count) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100546 bh = bh->b_reqnext;
547 pc->bh = bh;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 if (bh) {
549 pc->b_data = bh->b_data;
550 pc->b_count = atomic_read(&bh->b_count);
551 }
552 }
553 }
554}
555
Borislav Petkovd236d742008-04-18 00:46:27 +0200556static void idetape_update_buffers(struct ide_atapi_pc *pc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557{
558 struct idetape_bh *bh = pc->bh;
559 int count;
Borislav Petkovd236d742008-04-18 00:46:27 +0200560 unsigned int bcount = pc->xferred;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561
Borislav Petkov346331f2008-04-18 00:46:26 +0200562 if (pc->flags & PC_FLAG_WRITING)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 return;
564 while (bcount) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 if (bh == NULL) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100566 printk(KERN_ERR "ide-tape: bh == NULL in %s\n",
567 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 return;
569 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 count = min((unsigned int)bh->b_size, (unsigned int)bcount);
571 atomic_set(&bh->b_count, count);
572 if (atomic_read(&bh->b_count) == bh->b_size)
573 bh = bh->b_reqnext;
574 bcount -= count;
575 }
576 pc->bh = bh;
577}
578
579/*
580 * idetape_next_pc_storage returns a pointer to a place in which we can
581 * safely store a packet command, even though we intend to leave the
582 * driver. A storage space for a maximum of IDETAPE_PC_STACK packet
583 * commands is allocated at initialization time.
584 */
Borislav Petkovd236d742008-04-18 00:46:27 +0200585static struct ide_atapi_pc *idetape_next_pc_storage(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586{
587 idetape_tape_t *tape = drive->driver_data;
588
Borislav Petkov8004a8c2008-02-06 02:57:51 +0100589 debug_log(DBG_PCRQ_STACK, "pc_stack_index=%d\n", tape->pc_stack_index);
590
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 if (tape->pc_stack_index == IDETAPE_PC_STACK)
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100592 tape->pc_stack_index = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 return (&tape->pc_stack[tape->pc_stack_index++]);
594}
595
596/*
597 * idetape_next_rq_storage is used along with idetape_next_pc_storage.
598 * Since we queue packet commands in the request queue, we need to
599 * allocate a request, along with the allocation of a packet command.
600 */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100601
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602/**************************************************************
603 * *
604 * This should get fixed to use kmalloc(.., GFP_ATOMIC) *
605 * followed later on by kfree(). -ml *
606 * *
607 **************************************************************/
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100608
609static struct request *idetape_next_rq_storage(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610{
611 idetape_tape_t *tape = drive->driver_data;
612
Borislav Petkov8004a8c2008-02-06 02:57:51 +0100613 debug_log(DBG_PCRQ_STACK, "rq_stack_index=%d\n", tape->rq_stack_index);
614
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 if (tape->rq_stack_index == IDETAPE_PC_STACK)
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100616 tape->rq_stack_index = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 return (&tape->rq_stack[tape->rq_stack_index++]);
618}
619
Borislav Petkovd236d742008-04-18 00:46:27 +0200620static void idetape_init_pc(struct ide_atapi_pc *pc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621{
622 memset(pc->c, 0, 12);
623 pc->retries = 0;
624 pc->flags = 0;
Borislav Petkovd236d742008-04-18 00:46:27 +0200625 pc->req_xfer = 0;
626 pc->buf = pc->pc_buf;
627 pc->buf_size = IDETAPE_PC_BUFFER_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628 pc->bh = NULL;
629 pc->b_data = NULL;
630}
631
632/*
Borislav Petkov1b5db432008-02-02 19:56:48 +0100633 * called on each failed packet command retry to analyze the request sense. We
634 * currently do not utilize this information.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 */
Borislav Petkov1b5db432008-02-02 19:56:48 +0100636static void idetape_analyze_error(ide_drive_t *drive, u8 *sense)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637{
638 idetape_tape_t *tape = drive->driver_data;
Borislav Petkovd236d742008-04-18 00:46:27 +0200639 struct ide_atapi_pc *pc = tape->failed_pc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640
Borislav Petkov1b5db432008-02-02 19:56:48 +0100641 tape->sense_key = sense[2] & 0xF;
642 tape->asc = sense[12];
643 tape->ascq = sense[13];
Borislav Petkov8004a8c2008-02-06 02:57:51 +0100644
645 debug_log(DBG_ERR, "pc = %x, sense key = %x, asc = %x, ascq = %x\n",
646 pc->c[0], tape->sense_key, tape->asc, tape->ascq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647
Borislav Petkovd236d742008-04-18 00:46:27 +0200648 /* Correct pc->xferred by asking the tape. */
Borislav Petkov346331f2008-04-18 00:46:26 +0200649 if (pc->flags & PC_FLAG_DMA_ERROR) {
Borislav Petkovd236d742008-04-18 00:46:27 +0200650 pc->xferred = pc->req_xfer -
Borislav Petkov54bb2072008-02-06 02:57:52 +0100651 tape->blk_size *
Borislav Petkov860ff5e2008-02-02 19:56:50 +0100652 be32_to_cpu(get_unaligned((u32 *)&sense[3]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 idetape_update_buffers(pc);
654 }
655
656 /*
657 * If error was the result of a zero-length read or write command,
658 * with sense key=5, asc=0x22, ascq=0, let it slide. Some drives
659 * (i.e. Seagate STT3401A Travan) don't support 0-length read/writes.
660 */
Borislav Petkov90699ce2008-02-02 19:56:50 +0100661 if ((pc->c[0] == READ_6 || pc->c[0] == WRITE_6)
Borislav Petkov1b5db432008-02-02 19:56:48 +0100662 /* length == 0 */
663 && pc->c[4] == 0 && pc->c[3] == 0 && pc->c[2] == 0) {
664 if (tape->sense_key == 5) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665 /* don't report an error, everything's ok */
666 pc->error = 0;
667 /* don't retry read/write */
Borislav Petkov346331f2008-04-18 00:46:26 +0200668 pc->flags |= PC_FLAG_ABORT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669 }
670 }
Borislav Petkov90699ce2008-02-02 19:56:50 +0100671 if (pc->c[0] == READ_6 && (sense[2] & 0x80)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672 pc->error = IDETAPE_ERROR_FILEMARK;
Borislav Petkov346331f2008-04-18 00:46:26 +0200673 pc->flags |= PC_FLAG_ABORT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 }
Borislav Petkov90699ce2008-02-02 19:56:50 +0100675 if (pc->c[0] == WRITE_6) {
Borislav Petkov1b5db432008-02-02 19:56:48 +0100676 if ((sense[2] & 0x40) || (tape->sense_key == 0xd
677 && tape->asc == 0x0 && tape->ascq == 0x2)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 pc->error = IDETAPE_ERROR_EOD;
Borislav Petkov346331f2008-04-18 00:46:26 +0200679 pc->flags |= PC_FLAG_ABORT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 }
681 }
Borislav Petkov90699ce2008-02-02 19:56:50 +0100682 if (pc->c[0] == READ_6 || pc->c[0] == WRITE_6) {
Borislav Petkov1b5db432008-02-02 19:56:48 +0100683 if (tape->sense_key == 8) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684 pc->error = IDETAPE_ERROR_EOD;
Borislav Petkov346331f2008-04-18 00:46:26 +0200685 pc->flags |= PC_FLAG_ABORT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686 }
Borislav Petkov346331f2008-04-18 00:46:26 +0200687 if (!(pc->flags & PC_FLAG_ABORT) &&
Borislav Petkovd236d742008-04-18 00:46:27 +0200688 pc->xferred)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689 pc->retries = IDETAPE_MAX_PC_RETRIES + 1;
690 }
691}
692
Borislav Petkov419d4742008-02-02 19:56:51 +0100693static void idetape_activate_next_stage(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694{
695 idetape_tape_t *tape = drive->driver_data;
696 idetape_stage_t *stage = tape->next_stage;
697 struct request *rq = &stage->rq;
698
Borislav Petkov8004a8c2008-02-06 02:57:51 +0100699 debug_log(DBG_PROCS, "Enter %s\n", __func__);
700
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 if (stage == NULL) {
Borislav Petkov8004a8c2008-02-06 02:57:51 +0100702 printk(KERN_ERR "ide-tape: bug: Trying to activate a non"
703 " existing stage\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 return;
705 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706
707 rq->rq_disk = tape->disk;
708 rq->buffer = NULL;
709 rq->special = (void *)stage->bh;
Borislav Petkov54bb2072008-02-06 02:57:52 +0100710 tape->active_data_rq = rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711 tape->active_stage = stage;
712 tape->next_stage = stage->next;
713}
714
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100715/* Free a stage along with its related buffers completely. */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100716static void __idetape_kfree_stage(idetape_stage_t *stage)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717{
718 struct idetape_bh *prev_bh, *bh = stage->bh;
719 int size;
720
721 while (bh != NULL) {
722 if (bh->b_data != NULL) {
723 size = (int) bh->b_size;
724 while (size > 0) {
725 free_page((unsigned long) bh->b_data);
726 size -= PAGE_SIZE;
727 bh->b_data += PAGE_SIZE;
728 }
729 }
730 prev_bh = bh;
731 bh = bh->b_reqnext;
732 kfree(prev_bh);
733 }
734 kfree(stage);
735}
736
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100737static void idetape_kfree_stage(idetape_tape_t *tape, idetape_stage_t *stage)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738{
739 __idetape_kfree_stage(stage);
740}
741
742/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100743 * Remove tape->first_stage from the pipeline. The caller should avoid race
744 * conditions.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745 */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100746static void idetape_remove_stage_head(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747{
748 idetape_tape_t *tape = drive->driver_data;
749 idetape_stage_t *stage;
Borislav Petkov8004a8c2008-02-06 02:57:51 +0100750
751 debug_log(DBG_PROCS, "Enter %s\n", __func__);
752
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753 if (tape->first_stage == NULL) {
754 printk(KERN_ERR "ide-tape: bug: tape->first_stage is NULL\n");
Borislav Petkov55a5d292008-02-02 19:56:49 +0100755 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756 }
757 if (tape->active_stage == tape->first_stage) {
Borislav Petkov8004a8c2008-02-06 02:57:51 +0100758 printk(KERN_ERR "ide-tape: bug: Trying to free our active "
759 "pipeline stage\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760 return;
761 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762 stage = tape->first_stage;
763 tape->first_stage = stage->next;
764 idetape_kfree_stage(tape, stage);
765 tape->nr_stages--;
766 if (tape->first_stage == NULL) {
767 tape->last_stage = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 if (tape->next_stage != NULL)
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100769 printk(KERN_ERR "ide-tape: bug: tape->next_stage !="
770 " NULL\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771 if (tape->nr_stages)
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100772 printk(KERN_ERR "ide-tape: bug: nr_stages should be 0 "
773 "now\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774 }
775}
776
777/*
778 * This will free all the pipeline stages starting from new_last_stage->next
779 * to the end of the list, and point tape->last_stage to new_last_stage.
780 */
781static void idetape_abort_pipeline(ide_drive_t *drive,
782 idetape_stage_t *new_last_stage)
783{
784 idetape_tape_t *tape = drive->driver_data;
785 idetape_stage_t *stage = new_last_stage->next;
786 idetape_stage_t *nstage;
787
Borislav Petkov8004a8c2008-02-06 02:57:51 +0100788 debug_log(DBG_PROCS, "%s: Enter %s\n", tape->name, __func__);
789
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790 while (stage) {
791 nstage = stage->next;
792 idetape_kfree_stage(tape, stage);
793 --tape->nr_stages;
794 --tape->nr_pending_stages;
795 stage = nstage;
796 }
797 if (new_last_stage)
798 new_last_stage->next = NULL;
799 tape->last_stage = new_last_stage;
800 tape->next_stage = NULL;
801}
802
803/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100804 * Finish servicing a request and insert a pending pipeline request into the
805 * main device queue.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806 */
807static int idetape_end_request(ide_drive_t *drive, int uptodate, int nr_sects)
808{
809 struct request *rq = HWGROUP(drive)->rq;
810 idetape_tape_t *tape = drive->driver_data;
811 unsigned long flags;
812 int error;
813 int remove_stage = 0;
814 idetape_stage_t *active_stage;
815
Borislav Petkov8004a8c2008-02-06 02:57:51 +0100816 debug_log(DBG_PROCS, "Enter %s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817
818 switch (uptodate) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100819 case 0: error = IDETAPE_ERROR_GENERAL; break;
820 case 1: error = 0; break;
821 default: error = uptodate;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822 }
823 rq->errors = error;
824 if (error)
825 tape->failed_pc = NULL;
826
Bartlomiej Zolnierkiewicz36872212008-01-26 20:13:10 +0100827 if (!blk_special_request(rq)) {
828 ide_end_request(drive, uptodate, nr_sects);
829 return 0;
830 }
831
Borislav Petkov54bb2072008-02-06 02:57:52 +0100832 spin_lock_irqsave(&tape->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833
834 /* The request was a pipelined data transfer request */
Borislav Petkov54bb2072008-02-06 02:57:52 +0100835 if (tape->active_data_rq == rq) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836 active_stage = tape->active_stage;
837 tape->active_stage = NULL;
Borislav Petkov54bb2072008-02-06 02:57:52 +0100838 tape->active_data_rq = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839 tape->nr_pending_stages--;
840 if (rq->cmd[0] & REQ_IDETAPE_WRITE) {
841 remove_stage = 1;
842 if (error) {
Borislav Petkov346331f2008-04-18 00:46:26 +0200843 set_bit(IDETAPE_FLAG_PIPELINE_ERR,
844 &tape->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845 if (error == IDETAPE_ERROR_EOD)
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100846 idetape_abort_pipeline(drive,
847 active_stage);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848 }
849 } else if (rq->cmd[0] & REQ_IDETAPE_READ) {
850 if (error == IDETAPE_ERROR_EOD) {
Borislav Petkov346331f2008-04-18 00:46:26 +0200851 set_bit(IDETAPE_FLAG_PIPELINE_ERR,
852 &tape->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853 idetape_abort_pipeline(drive, active_stage);
854 }
855 }
856 if (tape->next_stage != NULL) {
Borislav Petkov419d4742008-02-02 19:56:51 +0100857 idetape_activate_next_stage(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100859 /* Insert the next request into the request queue. */
Borislav Petkov54bb2072008-02-06 02:57:52 +0100860 (void)ide_do_drive_cmd(drive, tape->active_data_rq,
861 ide_end);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862 } else if (!error) {
Borislav Petkov97219852008-02-06 02:57:52 +0100863 /*
864 * This is a part of the feedback loop which tries to
865 * find the optimum number of stages. We are starting
866 * from a minimum maximum number of stages, and if we
867 * sense that the pipeline is empty, we try to increase
868 * it, until we reach the user compile time memory
869 * limit.
870 */
871 int i = (tape->max_pipeline - tape->min_pipeline) / 10;
872
873 tape->max_stages += max(i, 1);
874 tape->max_stages = max(tape->max_stages,
875 tape->min_pipeline);
876 tape->max_stages = min(tape->max_stages,
877 tape->max_pipeline);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878 }
879 }
880 ide_end_drive_cmd(drive, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881
882 if (remove_stage)
883 idetape_remove_stage_head(drive);
Borislav Petkov54bb2072008-02-06 02:57:52 +0100884 if (tape->active_data_rq == NULL)
Borislav Petkov346331f2008-04-18 00:46:26 +0200885 clear_bit(IDETAPE_FLAG_PIPELINE_ACTIVE, &tape->flags);
Borislav Petkov54bb2072008-02-06 02:57:52 +0100886 spin_unlock_irqrestore(&tape->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887 return 0;
888}
889
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100890static ide_startstop_t idetape_request_sense_callback(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891{
892 idetape_tape_t *tape = drive->driver_data;
893
Borislav Petkov8004a8c2008-02-06 02:57:51 +0100894 debug_log(DBG_PROCS, "Enter %s\n", __func__);
895
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896 if (!tape->pc->error) {
Borislav Petkovd236d742008-04-18 00:46:27 +0200897 idetape_analyze_error(drive, tape->pc->buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898 idetape_end_request(drive, 1, 0);
899 } else {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100900 printk(KERN_ERR "ide-tape: Error in REQUEST SENSE itself - "
901 "Aborting request!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902 idetape_end_request(drive, 0, 0);
903 }
904 return ide_stopped;
905}
906
Borislav Petkovd236d742008-04-18 00:46:27 +0200907static void idetape_create_request_sense_cmd(struct ide_atapi_pc *pc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908{
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100909 idetape_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +0100910 pc->c[0] = REQUEST_SENSE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911 pc->c[4] = 20;
Borislav Petkovd236d742008-04-18 00:46:27 +0200912 pc->req_xfer = 20;
913 pc->idetape_callback = &idetape_request_sense_callback;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914}
915
916static void idetape_init_rq(struct request *rq, u8 cmd)
917{
918 memset(rq, 0, sizeof(*rq));
Jens Axboe4aff5e22006-08-10 08:44:47 +0200919 rq->cmd_type = REQ_TYPE_SPECIAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920 rq->cmd[0] = cmd;
921}
922
923/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100924 * Generate a new packet command request in front of the request queue, before
925 * the current request, so that it will be processed immediately, on the next
926 * pass through the driver. The function below is called from the request
927 * handling part of the driver (the "bottom" part). Safe storage for the request
928 * should be allocated with ide_tape_next_{pc,rq}_storage() prior to that.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100930 * Memory for those requests is pre-allocated at initialization time, and is
931 * limited to IDETAPE_PC_STACK requests. We assume that we have enough space for
932 * the maximum possible number of inter-dependent packet commands.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100934 * The higher level of the driver - The ioctl handler and the character device
935 * handling functions should queue request to the lower level part and wait for
936 * their completion using idetape_queue_pc_tail or idetape_queue_rw_tail.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937 */
Borislav Petkovd236d742008-04-18 00:46:27 +0200938static void idetape_queue_pc_head(ide_drive_t *drive, struct ide_atapi_pc *pc,
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100939 struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940{
941 struct ide_tape_obj *tape = drive->driver_data;
942
943 idetape_init_rq(rq, REQ_IDETAPE_PC1);
944 rq->buffer = (char *) pc;
945 rq->rq_disk = tape->disk;
946 (void) ide_do_drive_cmd(drive, rq, ide_preempt);
947}
948
949/*
950 * idetape_retry_pc is called when an error was detected during the
951 * last packet command. We queue a request sense packet command in
952 * the head of the request list.
953 */
954static ide_startstop_t idetape_retry_pc (ide_drive_t *drive)
955{
956 idetape_tape_t *tape = drive->driver_data;
Borislav Petkovd236d742008-04-18 00:46:27 +0200957 struct ide_atapi_pc *pc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958 struct request *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959
Bartlomiej Zolnierkiewicz64a57fe2008-02-06 02:57:51 +0100960 (void)ide_read_error(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961 pc = idetape_next_pc_storage(drive);
962 rq = idetape_next_rq_storage(drive);
963 idetape_create_request_sense_cmd(pc);
Borislav Petkov346331f2008-04-18 00:46:26 +0200964 set_bit(IDETAPE_FLAG_IGNORE_DSC, &tape->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965 idetape_queue_pc_head(drive, pc, rq);
966 return ide_stopped;
967}
968
969/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100970 * Postpone the current request so that ide.c will be able to service requests
971 * from another device on the same hwgroup while we are polling for DSC.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972 */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100973static void idetape_postpone_request(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974{
975 idetape_tape_t *tape = drive->driver_data;
976
Borislav Petkov8004a8c2008-02-06 02:57:51 +0100977 debug_log(DBG_PROCS, "Enter %s\n", __func__);
978
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979 tape->postponed_rq = HWGROUP(drive)->rq;
Borislav Petkov54bb2072008-02-06 02:57:52 +0100980 ide_stall_queue(drive, tape->dsc_poll_freq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981}
982
Borislav Petkovd236d742008-04-18 00:46:27 +0200983typedef void idetape_io_buf(ide_drive_t *, struct ide_atapi_pc *, unsigned int);
Borislav Petkova1efc852008-02-06 02:57:52 +0100984
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985/*
Borislav Petkova1efc852008-02-06 02:57:52 +0100986 * This is the usual interrupt handler which will be called during a packet
987 * command. We will transfer some of the data (as requested by the drive) and
988 * will re-point interrupt handler to us. When data transfer is finished, we
989 * will act according to the algorithm described before
Borislav Petkov8d06bfa2008-02-06 02:57:53 +0100990 * idetape_issue_pc.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991 */
Borislav Petkova1efc852008-02-06 02:57:52 +0100992static ide_startstop_t idetape_pc_intr(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993{
994 ide_hwif_t *hwif = drive->hwif;
995 idetape_tape_t *tape = drive->driver_data;
Borislav Petkovd236d742008-04-18 00:46:27 +0200996 struct ide_atapi_pc *pc = tape->pc;
Borislav Petkova1efc852008-02-06 02:57:52 +0100997 xfer_func_t *xferfunc;
998 idetape_io_buf *iobuf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999 unsigned int temp;
1000#if SIMULATE_ERRORS
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001001 static int error_sim_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002#endif
Bartlomiej Zolnierkiewicz790d1232008-01-25 22:17:12 +01001003 u16 bcount;
Bartlomiej Zolnierkiewicz8e7657a2008-01-25 22:17:12 +01001004 u8 stat, ireason;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001006 debug_log(DBG_PROCS, "Enter %s - interrupt handler\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007
1008 /* Clear the interrupt */
Bartlomiej Zolnierkiewiczc47137a2008-02-06 02:57:51 +01001009 stat = ide_read_status(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010
Borislav Petkov346331f2008-04-18 00:46:26 +02001011 if (pc->flags & PC_FLAG_DMA_IN_PROGRESS) {
Bartlomiej Zolnierkiewicz22c525b2008-01-25 22:17:11 +01001012 if (hwif->ide_dma_end(drive) || (stat & ERR_STAT)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013 /*
1014 * A DMA error is sometimes expected. For example,
1015 * if the tape is crossing a filemark during a
1016 * READ command, it will issue an irq and position
1017 * itself before the filemark, so that only a partial
1018 * data transfer will occur (which causes the DMA
1019 * error). In that case, we will later ask the tape
1020 * how much bytes of the original request were
1021 * actually transferred (we can't receive that
1022 * information from the DMA engine on most chipsets).
1023 */
1024
1025 /*
1026 * On the contrary, a DMA error is never expected;
1027 * it usually indicates a hardware error or abort.
1028 * If the tape crosses a filemark during a READ
1029 * command, it will issue an irq and position itself
1030 * after the filemark (not before). Only a partial
1031 * data transfer will occur, but no DMA error.
1032 * (AS, 19 Apr 2001)
1033 */
Borislav Petkov346331f2008-04-18 00:46:26 +02001034 pc->flags |= PC_FLAG_DMA_ERROR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035 } else {
Borislav Petkovd236d742008-04-18 00:46:27 +02001036 pc->xferred = pc->req_xfer;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037 idetape_update_buffers(pc);
1038 }
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001039 debug_log(DBG_PROCS, "DMA finished\n");
1040
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041 }
1042
1043 /* No more interrupts */
Bartlomiej Zolnierkiewicz22c525b2008-01-25 22:17:11 +01001044 if ((stat & DRQ_STAT) == 0) {
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001045 debug_log(DBG_SENSE, "Packet command completed, %d bytes"
Borislav Petkovd236d742008-04-18 00:46:27 +02001046 " transferred\n", pc->xferred);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047
Borislav Petkov346331f2008-04-18 00:46:26 +02001048 pc->flags &= ~PC_FLAG_DMA_IN_PROGRESS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049 local_irq_enable();
1050
1051#if SIMULATE_ERRORS
Borislav Petkov90699ce2008-02-02 19:56:50 +01001052 if ((pc->c[0] == WRITE_6 || pc->c[0] == READ_6) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053 (++error_sim_count % 100) == 0) {
1054 printk(KERN_INFO "ide-tape: %s: simulating error\n",
1055 tape->name);
Bartlomiej Zolnierkiewicz22c525b2008-01-25 22:17:11 +01001056 stat |= ERR_STAT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057 }
1058#endif
Borislav Petkov90699ce2008-02-02 19:56:50 +01001059 if ((stat & ERR_STAT) && pc->c[0] == REQUEST_SENSE)
Bartlomiej Zolnierkiewicz22c525b2008-01-25 22:17:11 +01001060 stat &= ~ERR_STAT;
Borislav Petkov346331f2008-04-18 00:46:26 +02001061 if ((stat & ERR_STAT) || (pc->flags & PC_FLAG_DMA_ERROR)) {
Bartlomiej Zolnierkiewicz22c525b2008-01-25 22:17:11 +01001062 /* Error detected */
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001063 debug_log(DBG_ERR, "%s: I/O error\n", tape->name);
1064
Borislav Petkov90699ce2008-02-02 19:56:50 +01001065 if (pc->c[0] == REQUEST_SENSE) {
Borislav Petkova1efc852008-02-06 02:57:52 +01001066 printk(KERN_ERR "ide-tape: I/O error in request"
1067 " sense command\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068 return ide_do_reset(drive);
1069 }
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001070 debug_log(DBG_ERR, "[cmd %x]: check condition\n",
1071 pc->c[0]);
1072
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073 /* Retry operation */
1074 return idetape_retry_pc(drive);
1075 }
1076 pc->error = 0;
Borislav Petkov346331f2008-04-18 00:46:26 +02001077 if ((pc->flags & PC_FLAG_WAIT_FOR_DSC) &&
Bartlomiej Zolnierkiewicz22c525b2008-01-25 22:17:11 +01001078 (stat & SEEK_STAT) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079 /* Media access command */
1080 tape->dsc_polling_start = jiffies;
Borislav Petkov54bb2072008-02-06 02:57:52 +01001081 tape->dsc_poll_freq = IDETAPE_DSC_MA_FAST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082 tape->dsc_timeout = jiffies + IDETAPE_DSC_MA_TIMEOUT;
1083 /* Allow ide.c to handle other requests */
1084 idetape_postpone_request(drive);
1085 return ide_stopped;
1086 }
1087 if (tape->failed_pc == pc)
1088 tape->failed_pc = NULL;
1089 /* Command finished - Call the callback function */
Borislav Petkovd236d742008-04-18 00:46:27 +02001090 return pc->idetape_callback(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091 }
Borislav Petkov346331f2008-04-18 00:46:26 +02001092
1093 if (pc->flags & PC_FLAG_DMA_IN_PROGRESS) {
1094 pc->flags &= ~PC_FLAG_DMA_IN_PROGRESS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095 printk(KERN_ERR "ide-tape: The tape wants to issue more "
1096 "interrupts in DMA mode\n");
1097 printk(KERN_ERR "ide-tape: DMA disabled, reverting to PIO\n");
Bartlomiej Zolnierkiewicz7469aaf2007-02-17 02:40:26 +01001098 ide_dma_off(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099 return ide_do_reset(drive);
1100 }
1101 /* Get the number of bytes to transfer on this interrupt. */
Bartlomiej Zolnierkiewicz23579a22008-04-18 00:46:26 +02001102 bcount = (hwif->INB(hwif->io_ports[IDE_BCOUNTH_OFFSET]) << 8) |
1103 hwif->INB(hwif->io_ports[IDE_BCOUNTL_OFFSET]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104
Bartlomiej Zolnierkiewicz23579a22008-04-18 00:46:26 +02001105 ireason = hwif->INB(hwif->io_ports[IDE_IREASON_OFFSET]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106
Bartlomiej Zolnierkiewicz8e7657a2008-01-25 22:17:12 +01001107 if (ireason & CD) {
Borislav Petkova1efc852008-02-06 02:57:52 +01001108 printk(KERN_ERR "ide-tape: CoD != 0 in %s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109 return ide_do_reset(drive);
1110 }
Borislav Petkov346331f2008-04-18 00:46:26 +02001111 if (((ireason & IO) == IO) == !!(pc->flags & PC_FLAG_WRITING)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112 /* Hopefully, we will never get here */
1113 printk(KERN_ERR "ide-tape: We wanted to %s, ",
Bartlomiej Zolnierkiewicz8e7657a2008-01-25 22:17:12 +01001114 (ireason & IO) ? "Write" : "Read");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115 printk(KERN_ERR "ide-tape: but the tape wants us to %s !\n",
Bartlomiej Zolnierkiewicz8e7657a2008-01-25 22:17:12 +01001116 (ireason & IO) ? "Read" : "Write");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117 return ide_do_reset(drive);
1118 }
Borislav Petkov346331f2008-04-18 00:46:26 +02001119 if (!(pc->flags & PC_FLAG_WRITING)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120 /* Reading - Check that we have enough space */
Borislav Petkovd236d742008-04-18 00:46:27 +02001121 temp = pc->xferred + bcount;
1122 if (temp > pc->req_xfer) {
1123 if (temp > pc->buf_size) {
Borislav Petkova1efc852008-02-06 02:57:52 +01001124 printk(KERN_ERR "ide-tape: The tape wants to "
1125 "send us more data than expected "
1126 "- discarding data\n");
Bartlomiej Zolnierkiewicz7616c0a2008-04-18 00:46:26 +02001127 ide_atapi_discard_data(drive, bcount);
Borislav Petkova1efc852008-02-06 02:57:52 +01001128 ide_set_handler(drive, &idetape_pc_intr,
1129 IDETAPE_WAIT_CMD, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130 return ide_started;
1131 }
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001132 debug_log(DBG_SENSE, "The tape wants to send us more "
1133 "data than expected - allowing transfer\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134 }
Borislav Petkova1efc852008-02-06 02:57:52 +01001135 iobuf = &idetape_input_buffers;
1136 xferfunc = hwif->atapi_input_bytes;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137 } else {
Borislav Petkova1efc852008-02-06 02:57:52 +01001138 iobuf = &idetape_output_buffers;
1139 xferfunc = hwif->atapi_output_bytes;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140 }
Borislav Petkova1efc852008-02-06 02:57:52 +01001141
1142 if (pc->bh)
1143 iobuf(drive, pc, bcount);
1144 else
Borislav Petkovd236d742008-04-18 00:46:27 +02001145 xferfunc(drive, pc->cur_pos, bcount);
Borislav Petkova1efc852008-02-06 02:57:52 +01001146
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147 /* Update the current position */
Borislav Petkovd236d742008-04-18 00:46:27 +02001148 pc->xferred += bcount;
1149 pc->cur_pos += bcount;
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001150
1151 debug_log(DBG_SENSE, "[cmd %x] transferred %d bytes on that intr.\n",
1152 pc->c[0], bcount);
1153
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154 /* And set the interrupt handler again */
1155 ide_set_handler(drive, &idetape_pc_intr, IDETAPE_WAIT_CMD, NULL);
1156 return ide_started;
1157}
1158
1159/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001160 * Packet Command Interface
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001162 * The current Packet Command is available in tape->pc, and will not change
1163 * until we finish handling it. Each packet command is associated with a
1164 * callback function that will be called when the command is finished.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001166 * The handling will be done in three stages:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001168 * 1. idetape_issue_pc will send the packet command to the drive, and will set
1169 * the interrupt handler to idetape_pc_intr.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001171 * 2. On each interrupt, idetape_pc_intr will be called. This step will be
1172 * repeated until the device signals us that no more interrupts will be issued.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001174 * 3. ATAPI Tape media access commands have immediate status with a delayed
1175 * process. In case of a successful initiation of a media access packet command,
1176 * the DSC bit will be set when the actual execution of the command is finished.
1177 * Since the tape drive will not issue an interrupt, we have to poll for this
1178 * event. In this case, we define the request as "low priority request" by
1179 * setting rq_status to IDETAPE_RQ_POSTPONED, set a timer to poll for DSC and
1180 * exit the driver.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001182 * ide.c will then give higher priority to requests which originate from the
1183 * other device, until will change rq_status to RQ_ACTIVE.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001185 * 4. When the packet command is finished, it will be checked for errors.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001187 * 5. In case an error was found, we queue a request sense packet command in
1188 * front of the request queue and retry the operation up to
1189 * IDETAPE_MAX_PC_RETRIES times.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001191 * 6. In case no error was found, or we decided to give up and not to retry
1192 * again, the callback function will be called and then we will handle the next
1193 * request.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194 */
1195static ide_startstop_t idetape_transfer_pc(ide_drive_t *drive)
1196{
1197 ide_hwif_t *hwif = drive->hwif;
1198 idetape_tape_t *tape = drive->driver_data;
Borislav Petkovd236d742008-04-18 00:46:27 +02001199 struct ide_atapi_pc *pc = tape->pc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200 int retries = 100;
1201 ide_startstop_t startstop;
Bartlomiej Zolnierkiewicz8e7657a2008-01-25 22:17:12 +01001202 u8 ireason;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001204 if (ide_wait_stat(&startstop, drive, DRQ_STAT, BUSY_STAT, WAIT_READY)) {
1205 printk(KERN_ERR "ide-tape: Strange, packet command initiated "
1206 "yet DRQ isn't asserted\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207 return startstop;
1208 }
Bartlomiej Zolnierkiewicz23579a22008-04-18 00:46:26 +02001209 ireason = hwif->INB(hwif->io_ports[IDE_IREASON_OFFSET]);
Bartlomiej Zolnierkiewicz8e7657a2008-01-25 22:17:12 +01001210 while (retries-- && ((ireason & CD) == 0 || (ireason & IO))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211 printk(KERN_ERR "ide-tape: (IO,CoD != (0,1) while issuing "
1212 "a packet command, retrying\n");
1213 udelay(100);
Bartlomiej Zolnierkiewicz23579a22008-04-18 00:46:26 +02001214 ireason = hwif->INB(hwif->io_ports[IDE_IREASON_OFFSET]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215 if (retries == 0) {
1216 printk(KERN_ERR "ide-tape: (IO,CoD != (0,1) while "
1217 "issuing a packet command, ignoring\n");
Bartlomiej Zolnierkiewicz8e7657a2008-01-25 22:17:12 +01001218 ireason |= CD;
1219 ireason &= ~IO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220 }
1221 }
Bartlomiej Zolnierkiewicz8e7657a2008-01-25 22:17:12 +01001222 if ((ireason & CD) == 0 || (ireason & IO)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223 printk(KERN_ERR "ide-tape: (IO,CoD) != (0,1) while issuing "
1224 "a packet command\n");
1225 return ide_do_reset(drive);
1226 }
1227 /* Set the interrupt routine */
1228 ide_set_handler(drive, &idetape_pc_intr, IDETAPE_WAIT_CMD, NULL);
1229#ifdef CONFIG_BLK_DEV_IDEDMA
1230 /* Begin DMA, if necessary */
Borislav Petkov346331f2008-04-18 00:46:26 +02001231 if (pc->flags & PC_FLAG_DMA_IN_PROGRESS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232 hwif->dma_start(drive);
1233#endif
1234 /* Send the actual packet */
1235 HWIF(drive)->atapi_output_bytes(drive, pc->c, 12);
1236 return ide_started;
1237}
1238
Borislav Petkovd236d742008-04-18 00:46:27 +02001239static ide_startstop_t idetape_issue_pc(ide_drive_t *drive,
1240 struct ide_atapi_pc *pc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241{
1242 ide_hwif_t *hwif = drive->hwif;
1243 idetape_tape_t *tape = drive->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244 int dma_ok = 0;
Bartlomiej Zolnierkiewicz790d1232008-01-25 22:17:12 +01001245 u16 bcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246
Borislav Petkov90699ce2008-02-02 19:56:50 +01001247 if (tape->pc->c[0] == REQUEST_SENSE &&
1248 pc->c[0] == REQUEST_SENSE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249 printk(KERN_ERR "ide-tape: possible ide-tape.c bug - "
1250 "Two request sense in serial were issued\n");
1251 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252
Borislav Petkov90699ce2008-02-02 19:56:50 +01001253 if (tape->failed_pc == NULL && pc->c[0] != REQUEST_SENSE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254 tape->failed_pc = pc;
1255 /* Set the current packet command */
1256 tape->pc = pc;
1257
1258 if (pc->retries > IDETAPE_MAX_PC_RETRIES ||
Borislav Petkov346331f2008-04-18 00:46:26 +02001259 (pc->flags & PC_FLAG_ABORT)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260 /*
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001261 * We will "abort" retrying a packet command in case legitimate
1262 * error code was received (crossing a filemark, or end of the
1263 * media, for example).
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264 */
Borislav Petkov346331f2008-04-18 00:46:26 +02001265 if (!(pc->flags & PC_FLAG_ABORT)) {
Borislav Petkov90699ce2008-02-02 19:56:50 +01001266 if (!(pc->c[0] == TEST_UNIT_READY &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267 tape->sense_key == 2 && tape->asc == 4 &&
1268 (tape->ascq == 1 || tape->ascq == 8))) {
1269 printk(KERN_ERR "ide-tape: %s: I/O error, "
1270 "pc = %2x, key = %2x, "
1271 "asc = %2x, ascq = %2x\n",
1272 tape->name, pc->c[0],
1273 tape->sense_key, tape->asc,
1274 tape->ascq);
1275 }
1276 /* Giving up */
1277 pc->error = IDETAPE_ERROR_GENERAL;
1278 }
1279 tape->failed_pc = NULL;
Borislav Petkovd236d742008-04-18 00:46:27 +02001280 return pc->idetape_callback(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281 }
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001282 debug_log(DBG_SENSE, "Retry #%d, cmd = %02X\n", pc->retries, pc->c[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283
1284 pc->retries++;
1285 /* We haven't transferred any data yet */
Borislav Petkovd236d742008-04-18 00:46:27 +02001286 pc->xferred = 0;
1287 pc->cur_pos = pc->buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288 /* Request to transfer the entire buffer at once */
Borislav Petkovd236d742008-04-18 00:46:27 +02001289 bcount = pc->req_xfer;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290
Borislav Petkov346331f2008-04-18 00:46:26 +02001291 if (pc->flags & PC_FLAG_DMA_ERROR) {
1292 pc->flags &= ~PC_FLAG_DMA_ERROR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293 printk(KERN_WARNING "ide-tape: DMA disabled, "
1294 "reverting to PIO\n");
Bartlomiej Zolnierkiewicz7469aaf2007-02-17 02:40:26 +01001295 ide_dma_off(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296 }
Borislav Petkov346331f2008-04-18 00:46:26 +02001297 if ((pc->flags & PC_FLAG_DMA_RECOMMENDED) && drive->using_dma)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298 dma_ok = !hwif->dma_setup(drive);
1299
Bartlomiej Zolnierkiewicz2fc57382008-01-25 22:17:13 +01001300 ide_pktcmd_tf_load(drive, IDE_TFLAG_NO_SELECT_MASK |
1301 IDE_TFLAG_OUT_DEVICE, bcount, dma_ok);
1302
Borislav Petkov346331f2008-04-18 00:46:26 +02001303 if (dma_ok)
1304 /* Will begin DMA later */
1305 pc->flags |= PC_FLAG_DMA_IN_PROGRESS;
1306 if (test_bit(IDETAPE_FLAG_DRQ_INTERRUPT, &tape->flags)) {
Bartlomiej Zolnierkiewiczc1c9dbc2008-02-02 19:56:44 +01001307 ide_execute_command(drive, WIN_PACKETCMD, &idetape_transfer_pc,
1308 IDETAPE_WAIT_CMD, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309 return ide_started;
1310 } else {
Bartlomiej Zolnierkiewicz23579a22008-04-18 00:46:26 +02001311 hwif->OUTB(WIN_PACKETCMD, hwif->io_ports[IDE_COMMAND_OFFSET]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312 return idetape_transfer_pc(drive);
1313 }
1314}
1315
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001316static ide_startstop_t idetape_pc_callback(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317{
1318 idetape_tape_t *tape = drive->driver_data;
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001319
1320 debug_log(DBG_PROCS, "Enter %s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321
1322 idetape_end_request(drive, tape->pc->error ? 0 : 1, 0);
1323 return ide_stopped;
1324}
1325
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001326/* A mode sense command is used to "sense" tape parameters. */
Borislav Petkovd236d742008-04-18 00:46:27 +02001327static void idetape_create_mode_sense_cmd(struct ide_atapi_pc *pc, u8 page_code)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328{
1329 idetape_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +01001330 pc->c[0] = MODE_SENSE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331 if (page_code != IDETAPE_BLOCK_DESCRIPTOR)
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001332 /* DBD = 1 - Don't return block descriptors */
1333 pc->c[1] = 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334 pc->c[2] = page_code;
1335 /*
1336 * Changed pc->c[3] to 0 (255 will at best return unused info).
1337 *
1338 * For SCSI this byte is defined as subpage instead of high byte
1339 * of length and some IDE drives seem to interpret it this way
1340 * and return an error when 255 is used.
1341 */
1342 pc->c[3] = 0;
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001343 /* We will just discard data in that case */
1344 pc->c[4] = 255;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345 if (page_code == IDETAPE_BLOCK_DESCRIPTOR)
Borislav Petkovd236d742008-04-18 00:46:27 +02001346 pc->req_xfer = 12;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347 else if (page_code == IDETAPE_CAPABILITIES_PAGE)
Borislav Petkovd236d742008-04-18 00:46:27 +02001348 pc->req_xfer = 24;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349 else
Borislav Petkovd236d742008-04-18 00:46:27 +02001350 pc->req_xfer = 50;
1351 pc->idetape_callback = &idetape_pc_callback;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352}
1353
Borislav Petkov37016ba2008-02-06 02:57:52 +01001354static void idetape_calculate_speeds(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355{
1356 idetape_tape_t *tape = drive->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001358 if (time_after(jiffies,
1359 tape->controlled_pipeline_head_time + 120 * HZ)) {
1360 tape->controlled_previous_pipeline_head =
1361 tape->controlled_last_pipeline_head;
1362 tape->controlled_previous_head_time =
1363 tape->controlled_pipeline_head_time;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364 tape->controlled_last_pipeline_head = tape->pipeline_head;
1365 tape->controlled_pipeline_head_time = jiffies;
1366 }
1367 if (time_after(jiffies, tape->controlled_pipeline_head_time + 60 * HZ))
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001368 tape->controlled_pipeline_head_speed = (tape->pipeline_head -
1369 tape->controlled_last_pipeline_head) * 32 * HZ /
1370 (jiffies - tape->controlled_pipeline_head_time);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371 else if (time_after(jiffies, tape->controlled_previous_head_time))
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001372 tape->controlled_pipeline_head_speed = (tape->pipeline_head -
1373 tape->controlled_previous_pipeline_head) * 32 *
1374 HZ / (jiffies - tape->controlled_previous_head_time);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001376 if (tape->nr_pending_stages < tape->max_stages/*- 1 */) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377 /* -1 for read mode error recovery */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001378 if (time_after(jiffies, tape->uncontrolled_previous_head_time +
1379 10 * HZ)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380 tape->uncontrolled_pipeline_head_time = jiffies;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001381 tape->uncontrolled_pipeline_head_speed =
1382 (tape->pipeline_head -
1383 tape->uncontrolled_previous_pipeline_head) *
1384 32 * HZ / (jiffies -
1385 tape->uncontrolled_previous_head_time);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386 }
1387 } else {
1388 tape->uncontrolled_previous_head_time = jiffies;
1389 tape->uncontrolled_previous_pipeline_head = tape->pipeline_head;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001390 if (time_after(jiffies, tape->uncontrolled_pipeline_head_time +
1391 30 * HZ))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392 tape->uncontrolled_pipeline_head_time = jiffies;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001393
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394 }
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001395 tape->pipeline_head_speed = max(tape->uncontrolled_pipeline_head_speed,
1396 tape->controlled_pipeline_head_speed);
Borislav Petkov37016ba2008-02-06 02:57:52 +01001397
1398 if (tape->speed_control == 1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399 if (tape->nr_pending_stages >= tape->max_stages / 2)
1400 tape->max_insert_speed = tape->pipeline_head_speed +
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001401 (1100 - tape->pipeline_head_speed) * 2 *
1402 (tape->nr_pending_stages - tape->max_stages / 2)
1403 / tape->max_stages;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404 else
1405 tape->max_insert_speed = 500 +
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001406 (tape->pipeline_head_speed - 500) * 2 *
1407 tape->nr_pending_stages / tape->max_stages;
Borislav Petkov37016ba2008-02-06 02:57:52 +01001408
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409 if (tape->nr_pending_stages >= tape->max_stages * 99 / 100)
1410 tape->max_insert_speed = 5000;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411 } else
1412 tape->max_insert_speed = tape->speed_control;
Borislav Petkov37016ba2008-02-06 02:57:52 +01001413
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414 tape->max_insert_speed = max(tape->max_insert_speed, 500);
1415}
1416
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001417static ide_startstop_t idetape_media_access_finished(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418{
1419 idetape_tape_t *tape = drive->driver_data;
Borislav Petkovd236d742008-04-18 00:46:27 +02001420 struct ide_atapi_pc *pc = tape->pc;
Bartlomiej Zolnierkiewicz22c525b2008-01-25 22:17:11 +01001421 u8 stat;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422
Bartlomiej Zolnierkiewiczc47137a2008-02-06 02:57:51 +01001423 stat = ide_read_status(drive);
1424
Bartlomiej Zolnierkiewicz22c525b2008-01-25 22:17:11 +01001425 if (stat & SEEK_STAT) {
1426 if (stat & ERR_STAT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427 /* Error detected */
Borislav Petkov90699ce2008-02-02 19:56:50 +01001428 if (pc->c[0] != TEST_UNIT_READY)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429 printk(KERN_ERR "ide-tape: %s: I/O error, ",
1430 tape->name);
1431 /* Retry operation */
1432 return idetape_retry_pc(drive);
1433 }
1434 pc->error = 0;
1435 if (tape->failed_pc == pc)
1436 tape->failed_pc = NULL;
1437 } else {
1438 pc->error = IDETAPE_ERROR_GENERAL;
1439 tape->failed_pc = NULL;
1440 }
Borislav Petkovd236d742008-04-18 00:46:27 +02001441 return pc->idetape_callback(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442}
1443
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001444static ide_startstop_t idetape_rw_callback(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445{
1446 idetape_tape_t *tape = drive->driver_data;
1447 struct request *rq = HWGROUP(drive)->rq;
Borislav Petkovd236d742008-04-18 00:46:27 +02001448 int blocks = tape->pc->xferred / tape->blk_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449
Borislav Petkov54bb2072008-02-06 02:57:52 +01001450 tape->avg_size += blocks * tape->blk_size;
1451 tape->insert_size += blocks * tape->blk_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452 if (tape->insert_size > 1024 * 1024)
1453 tape->measure_insert_time = 1;
1454 if (tape->measure_insert_time) {
1455 tape->measure_insert_time = 0;
1456 tape->insert_time = jiffies;
1457 tape->insert_size = 0;
1458 }
1459 if (time_after(jiffies, tape->insert_time))
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001460 tape->insert_speed = tape->insert_size / 1024 * HZ /
1461 (jiffies - tape->insert_time);
Marcelo Feitoza Parisi9bae1ff2006-03-28 01:56:46 -08001462 if (time_after_eq(jiffies, tape->avg_time + HZ)) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001463 tape->avg_speed = tape->avg_size * HZ /
1464 (jiffies - tape->avg_time) / 1024;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465 tape->avg_size = 0;
1466 tape->avg_time = jiffies;
1467 }
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001468 debug_log(DBG_PROCS, "Enter %s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469
Borislav Petkov54bb2072008-02-06 02:57:52 +01001470 tape->first_frame += blocks;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001471 rq->current_nr_sectors -= blocks;
1472
1473 if (!tape->pc->error)
1474 idetape_end_request(drive, 1, 0);
1475 else
1476 idetape_end_request(drive, tape->pc->error, 0);
1477 return ide_stopped;
1478}
1479
Borislav Petkovd236d742008-04-18 00:46:27 +02001480static void idetape_create_read_cmd(idetape_tape_t *tape,
1481 struct ide_atapi_pc *pc,
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001482 unsigned int length, struct idetape_bh *bh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483{
1484 idetape_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +01001485 pc->c[0] = READ_6;
Borislav Petkov860ff5e2008-02-02 19:56:50 +01001486 put_unaligned(cpu_to_be32(length), (unsigned int *) &pc->c[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487 pc->c[1] = 1;
Borislav Petkovd236d742008-04-18 00:46:27 +02001488 pc->idetape_callback = &idetape_rw_callback;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489 pc->bh = bh;
1490 atomic_set(&bh->b_count, 0);
Borislav Petkovd236d742008-04-18 00:46:27 +02001491 pc->buf = NULL;
1492 pc->buf_size = length * tape->blk_size;
1493 pc->req_xfer = pc->buf_size;
1494 if (pc->req_xfer == tape->stage_size)
Borislav Petkov346331f2008-04-18 00:46:26 +02001495 pc->flags |= PC_FLAG_DMA_RECOMMENDED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496}
1497
Borislav Petkovd236d742008-04-18 00:46:27 +02001498static void idetape_create_write_cmd(idetape_tape_t *tape,
1499 struct ide_atapi_pc *pc,
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001500 unsigned int length, struct idetape_bh *bh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501{
1502 idetape_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +01001503 pc->c[0] = WRITE_6;
Borislav Petkov860ff5e2008-02-02 19:56:50 +01001504 put_unaligned(cpu_to_be32(length), (unsigned int *) &pc->c[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505 pc->c[1] = 1;
Borislav Petkovd236d742008-04-18 00:46:27 +02001506 pc->idetape_callback = &idetape_rw_callback;
Borislav Petkov346331f2008-04-18 00:46:26 +02001507 pc->flags |= PC_FLAG_WRITING;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001508 pc->bh = bh;
1509 pc->b_data = bh->b_data;
1510 pc->b_count = atomic_read(&bh->b_count);
Borislav Petkovd236d742008-04-18 00:46:27 +02001511 pc->buf = NULL;
1512 pc->buf_size = length * tape->blk_size;
1513 pc->req_xfer = pc->buf_size;
1514 if (pc->req_xfer == tape->stage_size)
Borislav Petkov346331f2008-04-18 00:46:26 +02001515 pc->flags |= PC_FLAG_DMA_RECOMMENDED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001516}
1517
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518static ide_startstop_t idetape_do_request(ide_drive_t *drive,
1519 struct request *rq, sector_t block)
1520{
1521 idetape_tape_t *tape = drive->driver_data;
Borislav Petkovd236d742008-04-18 00:46:27 +02001522 struct ide_atapi_pc *pc = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523 struct request *postponed_rq = tape->postponed_rq;
Bartlomiej Zolnierkiewicz22c525b2008-01-25 22:17:11 +01001524 u8 stat;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001525
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001526 debug_log(DBG_SENSE, "sector: %ld, nr_sectors: %ld,"
1527 " current_nr_sectors: %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001528 rq->sector, rq->nr_sectors, rq->current_nr_sectors);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529
Jens Axboe4aff5e22006-08-10 08:44:47 +02001530 if (!blk_special_request(rq)) {
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001531 /* We do not support buffer cache originated requests. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532 printk(KERN_NOTICE "ide-tape: %s: Unsupported request in "
Jens Axboe4aff5e22006-08-10 08:44:47 +02001533 "request queue (%d)\n", drive->name, rq->cmd_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534 ide_end_request(drive, 0, 0);
1535 return ide_stopped;
1536 }
1537
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001538 /* Retry a failed packet command */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001539 if (tape->failed_pc && tape->pc->c[0] == REQUEST_SENSE)
Borislav Petkov8d06bfa2008-02-06 02:57:53 +01001540 return idetape_issue_pc(drive, tape->failed_pc);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001541
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542 if (postponed_rq != NULL)
1543 if (rq != postponed_rq) {
1544 printk(KERN_ERR "ide-tape: ide-tape.c bug - "
1545 "Two DSC requests were queued\n");
1546 idetape_end_request(drive, 0, 0);
1547 return ide_stopped;
1548 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549
1550 tape->postponed_rq = NULL;
1551
1552 /*
1553 * If the tape is still busy, postpone our request and service
1554 * the other device meanwhile.
1555 */
Bartlomiej Zolnierkiewiczc47137a2008-02-06 02:57:51 +01001556 stat = ide_read_status(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557
1558 if (!drive->dsc_overlap && !(rq->cmd[0] & REQ_IDETAPE_PC2))
Borislav Petkov346331f2008-04-18 00:46:26 +02001559 set_bit(IDETAPE_FLAG_IGNORE_DSC, &tape->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560
1561 if (drive->post_reset == 1) {
Borislav Petkov346331f2008-04-18 00:46:26 +02001562 set_bit(IDETAPE_FLAG_IGNORE_DSC, &tape->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563 drive->post_reset = 0;
1564 }
1565
Linus Torvalds1da177e2005-04-16 15:20:36 -07001566 if (time_after(jiffies, tape->insert_time))
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001567 tape->insert_speed = tape->insert_size / 1024 * HZ /
1568 (jiffies - tape->insert_time);
Borislav Petkov37016ba2008-02-06 02:57:52 +01001569 idetape_calculate_speeds(drive);
Borislav Petkov346331f2008-04-18 00:46:26 +02001570 if (!test_and_clear_bit(IDETAPE_FLAG_IGNORE_DSC, &tape->flags) &&
Bartlomiej Zolnierkiewicz22c525b2008-01-25 22:17:11 +01001571 (stat & SEEK_STAT) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572 if (postponed_rq == NULL) {
1573 tape->dsc_polling_start = jiffies;
Borislav Petkov54bb2072008-02-06 02:57:52 +01001574 tape->dsc_poll_freq = tape->best_dsc_rw_freq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001575 tape->dsc_timeout = jiffies + IDETAPE_DSC_RW_TIMEOUT;
1576 } else if (time_after(jiffies, tape->dsc_timeout)) {
1577 printk(KERN_ERR "ide-tape: %s: DSC timeout\n",
1578 tape->name);
1579 if (rq->cmd[0] & REQ_IDETAPE_PC2) {
1580 idetape_media_access_finished(drive);
1581 return ide_stopped;
1582 } else {
1583 return ide_do_reset(drive);
1584 }
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001585 } else if (time_after(jiffies,
1586 tape->dsc_polling_start +
1587 IDETAPE_DSC_MA_THRESHOLD))
Borislav Petkov54bb2072008-02-06 02:57:52 +01001588 tape->dsc_poll_freq = IDETAPE_DSC_MA_SLOW;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001589 idetape_postpone_request(drive);
1590 return ide_stopped;
1591 }
1592 if (rq->cmd[0] & REQ_IDETAPE_READ) {
1593 tape->buffer_head++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594 tape->postpone_cnt = 0;
1595 pc = idetape_next_pc_storage(drive);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001596 idetape_create_read_cmd(tape, pc, rq->current_nr_sectors,
1597 (struct idetape_bh *)rq->special);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001598 goto out;
1599 }
1600 if (rq->cmd[0] & REQ_IDETAPE_WRITE) {
1601 tape->buffer_head++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001602 tape->postpone_cnt = 0;
1603 pc = idetape_next_pc_storage(drive);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001604 idetape_create_write_cmd(tape, pc, rq->current_nr_sectors,
1605 (struct idetape_bh *)rq->special);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606 goto out;
1607 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608 if (rq->cmd[0] & REQ_IDETAPE_PC1) {
Borislav Petkovd236d742008-04-18 00:46:27 +02001609 pc = (struct ide_atapi_pc *) rq->buffer;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610 rq->cmd[0] &= ~(REQ_IDETAPE_PC1);
1611 rq->cmd[0] |= REQ_IDETAPE_PC2;
1612 goto out;
1613 }
1614 if (rq->cmd[0] & REQ_IDETAPE_PC2) {
1615 idetape_media_access_finished(drive);
1616 return ide_stopped;
1617 }
1618 BUG();
1619out:
Borislav Petkov8d06bfa2008-02-06 02:57:53 +01001620 return idetape_issue_pc(drive, pc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001621}
1622
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001623/* Pipeline related functions */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001624static inline int idetape_pipeline_active(idetape_tape_t *tape)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001625{
1626 int rc1, rc2;
1627
Borislav Petkov346331f2008-04-18 00:46:26 +02001628 rc1 = test_bit(IDETAPE_FLAG_PIPELINE_ACTIVE, &tape->flags);
Borislav Petkov54bb2072008-02-06 02:57:52 +01001629 rc2 = (tape->active_data_rq != NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001630 return rc1;
1631}
1632
1633/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001634 * The function below uses __get_free_page to allocate a pipeline stage, along
1635 * with all the necessary small buffers which together make a buffer of size
1636 * tape->stage_size (or a bit more). We attempt to combine sequential pages as
1637 * much as possible.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001639 * It returns a pointer to the new allocated stage, or NULL if we can't (or
1640 * don't want to) allocate a stage.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001642 * Pipeline stages are optional and are used to increase performance. If we
1643 * can't allocate them, we'll manage without them.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644 */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001645static idetape_stage_t *__idetape_kmalloc_stage(idetape_tape_t *tape, int full,
1646 int clear)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001647{
1648 idetape_stage_t *stage;
1649 struct idetape_bh *prev_bh, *bh;
1650 int pages = tape->pages_per_stage;
1651 char *b_data = NULL;
1652
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001653 stage = kmalloc(sizeof(idetape_stage_t), GFP_KERNEL);
1654 if (!stage)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001655 return NULL;
1656 stage->next = NULL;
1657
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001658 stage->bh = kmalloc(sizeof(struct idetape_bh), GFP_KERNEL);
1659 bh = stage->bh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001660 if (bh == NULL)
1661 goto abort;
1662 bh->b_reqnext = NULL;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001663 bh->b_data = (char *) __get_free_page(GFP_KERNEL);
1664 if (!bh->b_data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001665 goto abort;
1666 if (clear)
1667 memset(bh->b_data, 0, PAGE_SIZE);
1668 bh->b_size = PAGE_SIZE;
1669 atomic_set(&bh->b_count, full ? bh->b_size : 0);
1670
1671 while (--pages) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001672 b_data = (char *) __get_free_page(GFP_KERNEL);
1673 if (!b_data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001674 goto abort;
1675 if (clear)
1676 memset(b_data, 0, PAGE_SIZE);
1677 if (bh->b_data == b_data + PAGE_SIZE) {
1678 bh->b_size += PAGE_SIZE;
1679 bh->b_data -= PAGE_SIZE;
1680 if (full)
1681 atomic_add(PAGE_SIZE, &bh->b_count);
1682 continue;
1683 }
1684 if (b_data == bh->b_data + bh->b_size) {
1685 bh->b_size += PAGE_SIZE;
1686 if (full)
1687 atomic_add(PAGE_SIZE, &bh->b_count);
1688 continue;
1689 }
1690 prev_bh = bh;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001691 bh = kmalloc(sizeof(struct idetape_bh), GFP_KERNEL);
1692 if (!bh) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001693 free_page((unsigned long) b_data);
1694 goto abort;
1695 }
1696 bh->b_reqnext = NULL;
1697 bh->b_data = b_data;
1698 bh->b_size = PAGE_SIZE;
1699 atomic_set(&bh->b_count, full ? bh->b_size : 0);
1700 prev_bh->b_reqnext = bh;
1701 }
1702 bh->b_size -= tape->excess_bh_size;
1703 if (full)
1704 atomic_sub(tape->excess_bh_size, &bh->b_count);
1705 return stage;
1706abort:
1707 __idetape_kfree_stage(stage);
1708 return NULL;
1709}
1710
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001711static idetape_stage_t *idetape_kmalloc_stage(idetape_tape_t *tape)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001712{
1713 idetape_stage_t *cache_stage = tape->cache_stage;
1714
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001715 debug_log(DBG_PROCS, "Enter %s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001716
1717 if (tape->nr_stages >= tape->max_stages)
1718 return NULL;
1719 if (cache_stage != NULL) {
1720 tape->cache_stage = NULL;
1721 return cache_stage;
1722 }
1723 return __idetape_kmalloc_stage(tape, 0, 0);
1724}
1725
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001726static int idetape_copy_stage_from_user(idetape_tape_t *tape,
1727 idetape_stage_t *stage, const char __user *buf, int n)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001728{
1729 struct idetape_bh *bh = tape->bh;
1730 int count;
Daniel Walkerdcd96372006-06-25 05:47:37 -07001731 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001732
1733 while (n) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001734 if (bh == NULL) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001735 printk(KERN_ERR "ide-tape: bh == NULL in %s\n",
1736 __func__);
Daniel Walkerdcd96372006-06-25 05:47:37 -07001737 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001738 }
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001739 count = min((unsigned int)
1740 (bh->b_size - atomic_read(&bh->b_count)),
1741 (unsigned int)n);
1742 if (copy_from_user(bh->b_data + atomic_read(&bh->b_count), buf,
1743 count))
Daniel Walkerdcd96372006-06-25 05:47:37 -07001744 ret = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001745 n -= count;
1746 atomic_add(count, &bh->b_count);
1747 buf += count;
1748 if (atomic_read(&bh->b_count) == bh->b_size) {
1749 bh = bh->b_reqnext;
1750 if (bh)
1751 atomic_set(&bh->b_count, 0);
1752 }
1753 }
1754 tape->bh = bh;
Daniel Walkerdcd96372006-06-25 05:47:37 -07001755 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001756}
1757
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001758static int idetape_copy_stage_to_user(idetape_tape_t *tape, char __user *buf,
1759 idetape_stage_t *stage, int n)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001760{
1761 struct idetape_bh *bh = tape->bh;
1762 int count;
Daniel Walkerdcd96372006-06-25 05:47:37 -07001763 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001764
1765 while (n) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001766 if (bh == NULL) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001767 printk(KERN_ERR "ide-tape: bh == NULL in %s\n",
1768 __func__);
Daniel Walkerdcd96372006-06-25 05:47:37 -07001769 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001770 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001771 count = min(tape->b_count, n);
Daniel Walkerdcd96372006-06-25 05:47:37 -07001772 if (copy_to_user(buf, tape->b_data, count))
1773 ret = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001774 n -= count;
1775 tape->b_data += count;
1776 tape->b_count -= count;
1777 buf += count;
1778 if (!tape->b_count) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001779 bh = bh->b_reqnext;
1780 tape->bh = bh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001781 if (bh) {
1782 tape->b_data = bh->b_data;
1783 tape->b_count = atomic_read(&bh->b_count);
1784 }
1785 }
1786 }
Daniel Walkerdcd96372006-06-25 05:47:37 -07001787 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788}
1789
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001790static void idetape_init_merge_stage(idetape_tape_t *tape)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001791{
1792 struct idetape_bh *bh = tape->merge_stage->bh;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001793
Linus Torvalds1da177e2005-04-16 15:20:36 -07001794 tape->bh = bh;
Borislav Petkov54abf372008-02-06 02:57:52 +01001795 if (tape->chrdev_dir == IDETAPE_DIR_WRITE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001796 atomic_set(&bh->b_count, 0);
1797 else {
1798 tape->b_data = bh->b_data;
1799 tape->b_count = atomic_read(&bh->b_count);
1800 }
1801}
1802
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001803static void idetape_switch_buffers(idetape_tape_t *tape, idetape_stage_t *stage)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001804{
1805 struct idetape_bh *tmp;
1806
1807 tmp = stage->bh;
1808 stage->bh = tape->merge_stage->bh;
1809 tape->merge_stage->bh = tmp;
1810 idetape_init_merge_stage(tape);
1811}
1812
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001813/* Add a new stage at the end of the pipeline. */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001814static void idetape_add_stage_tail(ide_drive_t *drive, idetape_stage_t *stage)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001815{
1816 idetape_tape_t *tape = drive->driver_data;
1817 unsigned long flags;
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001818
1819 debug_log(DBG_PROCS, "Enter %s\n", __func__);
1820
Borislav Petkov54bb2072008-02-06 02:57:52 +01001821 spin_lock_irqsave(&tape->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001822 stage->next = NULL;
1823 if (tape->last_stage != NULL)
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001824 tape->last_stage->next = stage;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825 else
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001826 tape->first_stage = stage;
1827 tape->next_stage = stage;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001828 tape->last_stage = stage;
1829 if (tape->next_stage == NULL)
1830 tape->next_stage = tape->last_stage;
1831 tape->nr_stages++;
1832 tape->nr_pending_stages++;
Borislav Petkov54bb2072008-02-06 02:57:52 +01001833 spin_unlock_irqrestore(&tape->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001834}
1835
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001836/* Install a completion in a pending request and sleep until it is serviced. The
1837 * caller should ensure that the request will not be serviced before we install
1838 * the completion (usually by disabling interrupts).
Linus Torvalds1da177e2005-04-16 15:20:36 -07001839 */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001840static void idetape_wait_for_request(ide_drive_t *drive, struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001841{
Peter Zijlstra6e9a4732006-09-30 23:28:10 -07001842 DECLARE_COMPLETION_ONSTACK(wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001843 idetape_tape_t *tape = drive->driver_data;
1844
Jens Axboe4aff5e22006-08-10 08:44:47 +02001845 if (rq == NULL || !blk_special_request(rq)) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001846 printk(KERN_ERR "ide-tape: bug: Trying to sleep on non-valid"
1847 " request\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001848 return;
1849 }
Jens Axboec00895a2006-09-30 20:29:12 +02001850 rq->end_io_data = &wait;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001851 rq->end_io = blk_end_sync_rq;
Borislav Petkov54bb2072008-02-06 02:57:52 +01001852 spin_unlock_irq(&tape->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001853 wait_for_completion(&wait);
1854 /* The stage and its struct request have been deallocated */
Borislav Petkov54bb2072008-02-06 02:57:52 +01001855 spin_lock_irq(&tape->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001856}
1857
Borislav Petkova2f5b7f2008-02-06 02:57:51 +01001858static ide_startstop_t idetape_read_position_callback(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001859{
1860 idetape_tape_t *tape = drive->driver_data;
Borislav Petkovd236d742008-04-18 00:46:27 +02001861 u8 *readpos = tape->pc->buf;
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001862
1863 debug_log(DBG_PROCS, "Enter %s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001864
1865 if (!tape->pc->error) {
Borislav Petkova2f5b7f2008-02-06 02:57:51 +01001866 debug_log(DBG_SENSE, "BOP - %s\n",
1867 (readpos[0] & 0x80) ? "Yes" : "No");
1868 debug_log(DBG_SENSE, "EOP - %s\n",
1869 (readpos[0] & 0x40) ? "Yes" : "No");
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001870
Borislav Petkova2f5b7f2008-02-06 02:57:51 +01001871 if (readpos[0] & 0x4) {
1872 printk(KERN_INFO "ide-tape: Block location is unknown"
1873 "to the tape\n");
Borislav Petkov346331f2008-04-18 00:46:26 +02001874 clear_bit(IDETAPE_FLAG_ADDRESS_VALID, &tape->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001875 idetape_end_request(drive, 0, 0);
1876 } else {
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001877 debug_log(DBG_SENSE, "Block Location - %u\n",
Borislav Petkova2f5b7f2008-02-06 02:57:51 +01001878 be32_to_cpu(*(u32 *)&readpos[4]));
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001879
Borislav Petkova2f5b7f2008-02-06 02:57:51 +01001880 tape->partition = readpos[1];
Borislav Petkov54bb2072008-02-06 02:57:52 +01001881 tape->first_frame =
Borislav Petkova2f5b7f2008-02-06 02:57:51 +01001882 be32_to_cpu(*(u32 *)&readpos[4]);
Borislav Petkov346331f2008-04-18 00:46:26 +02001883 set_bit(IDETAPE_FLAG_ADDRESS_VALID, &tape->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001884 idetape_end_request(drive, 1, 0);
1885 }
1886 } else {
1887 idetape_end_request(drive, 0, 0);
1888 }
1889 return ide_stopped;
1890}
1891
1892/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001893 * Write a filemark if write_filemark=1. Flush the device buffers without
1894 * writing a filemark otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001895 */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001896static void idetape_create_write_filemark_cmd(ide_drive_t *drive,
Borislav Petkovd236d742008-04-18 00:46:27 +02001897 struct ide_atapi_pc *pc, int write_filemark)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001898{
1899 idetape_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +01001900 pc->c[0] = WRITE_FILEMARKS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001901 pc->c[4] = write_filemark;
Borislav Petkov346331f2008-04-18 00:46:26 +02001902 pc->flags |= PC_FLAG_WAIT_FOR_DSC;
Borislav Petkovd236d742008-04-18 00:46:27 +02001903 pc->idetape_callback = &idetape_pc_callback;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001904}
1905
Borislav Petkovd236d742008-04-18 00:46:27 +02001906static void idetape_create_test_unit_ready_cmd(struct ide_atapi_pc *pc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001907{
1908 idetape_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +01001909 pc->c[0] = TEST_UNIT_READY;
Borislav Petkovd236d742008-04-18 00:46:27 +02001910 pc->idetape_callback = &idetape_pc_callback;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001911}
1912
1913/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001914 * We add a special packet command request to the tail of the request queue, and
1915 * wait for it to be serviced. This is not to be called from within the request
1916 * handling part of the driver! We allocate here data on the stack and it is
1917 * valid until the request is finished. This is not the case for the bottom part
1918 * of the driver, where we are always leaving the functions to wait for an
1919 * interrupt or a timer event.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001920 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001921 * From the bottom part of the driver, we should allocate safe memory using
1922 * idetape_next_pc_storage() and ide_tape_next_rq_storage(), and add the request
1923 * to the request list without waiting for it to be serviced! In that case, we
1924 * usually use idetape_queue_pc_head().
Linus Torvalds1da177e2005-04-16 15:20:36 -07001925 */
Borislav Petkovd236d742008-04-18 00:46:27 +02001926static int __idetape_queue_pc_tail(ide_drive_t *drive, struct ide_atapi_pc *pc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001927{
1928 struct ide_tape_obj *tape = drive->driver_data;
1929 struct request rq;
1930
1931 idetape_init_rq(&rq, REQ_IDETAPE_PC1);
1932 rq.buffer = (char *) pc;
1933 rq.rq_disk = tape->disk;
1934 return ide_do_drive_cmd(drive, &rq, ide_wait);
1935}
1936
Borislav Petkovd236d742008-04-18 00:46:27 +02001937static void idetape_create_load_unload_cmd(ide_drive_t *drive,
1938 struct ide_atapi_pc *pc, int cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001939{
1940 idetape_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +01001941 pc->c[0] = START_STOP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001942 pc->c[4] = cmd;
Borislav Petkov346331f2008-04-18 00:46:26 +02001943 pc->flags |= PC_FLAG_WAIT_FOR_DSC;
Borislav Petkovd236d742008-04-18 00:46:27 +02001944 pc->idetape_callback = &idetape_pc_callback;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001945}
1946
1947static int idetape_wait_ready(ide_drive_t *drive, unsigned long timeout)
1948{
1949 idetape_tape_t *tape = drive->driver_data;
Borislav Petkovd236d742008-04-18 00:46:27 +02001950 struct ide_atapi_pc pc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001951 int load_attempted = 0;
1952
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001953 /* Wait for the tape to become ready */
Borislav Petkov346331f2008-04-18 00:46:26 +02001954 set_bit(IDETAPE_FLAG_MEDIUM_PRESENT, &tape->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001955 timeout += jiffies;
1956 while (time_before(jiffies, timeout)) {
1957 idetape_create_test_unit_ready_cmd(&pc);
1958 if (!__idetape_queue_pc_tail(drive, &pc))
1959 return 0;
1960 if ((tape->sense_key == 2 && tape->asc == 4 && tape->ascq == 2)
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001961 || (tape->asc == 0x3A)) {
1962 /* no media */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001963 if (load_attempted)
1964 return -ENOMEDIUM;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001965 idetape_create_load_unload_cmd(drive, &pc,
1966 IDETAPE_LU_LOAD_MASK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001967 __idetape_queue_pc_tail(drive, &pc);
1968 load_attempted = 1;
1969 /* not about to be ready */
1970 } else if (!(tape->sense_key == 2 && tape->asc == 4 &&
1971 (tape->ascq == 1 || tape->ascq == 8)))
1972 return -EIO;
Nishanth Aravamudan80ce45f2005-09-10 00:27:08 -07001973 msleep(100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001974 }
1975 return -EIO;
1976}
1977
Borislav Petkovd236d742008-04-18 00:46:27 +02001978static int idetape_queue_pc_tail(ide_drive_t *drive, struct ide_atapi_pc *pc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001979{
1980 return __idetape_queue_pc_tail(drive, pc);
1981}
1982
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001983static int idetape_flush_tape_buffers(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001984{
Borislav Petkovd236d742008-04-18 00:46:27 +02001985 struct ide_atapi_pc pc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001986 int rc;
1987
1988 idetape_create_write_filemark_cmd(drive, &pc, 0);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001989 rc = idetape_queue_pc_tail(drive, &pc);
1990 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001991 return rc;
1992 idetape_wait_ready(drive, 60 * 5 * HZ);
1993 return 0;
1994}
1995
Borislav Petkovd236d742008-04-18 00:46:27 +02001996static void idetape_create_read_position_cmd(struct ide_atapi_pc *pc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001997{
1998 idetape_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +01001999 pc->c[0] = READ_POSITION;
Borislav Petkovd236d742008-04-18 00:46:27 +02002000 pc->req_xfer = 20;
2001 pc->idetape_callback = &idetape_read_position_callback;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002002}
2003
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002004static int idetape_read_position(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002005{
2006 idetape_tape_t *tape = drive->driver_data;
Borislav Petkovd236d742008-04-18 00:46:27 +02002007 struct ide_atapi_pc pc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002008 int position;
2009
Borislav Petkov8004a8c2008-02-06 02:57:51 +01002010 debug_log(DBG_PROCS, "Enter %s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002011
2012 idetape_create_read_position_cmd(&pc);
2013 if (idetape_queue_pc_tail(drive, &pc))
2014 return -1;
Borislav Petkov54bb2072008-02-06 02:57:52 +01002015 position = tape->first_frame;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002016 return position;
2017}
2018
Borislav Petkovd236d742008-04-18 00:46:27 +02002019static void idetape_create_locate_cmd(ide_drive_t *drive,
2020 struct ide_atapi_pc *pc,
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002021 unsigned int block, u8 partition, int skip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002022{
2023 idetape_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +01002024 pc->c[0] = POSITION_TO_ELEMENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002025 pc->c[1] = 2;
Borislav Petkov860ff5e2008-02-02 19:56:50 +01002026 put_unaligned(cpu_to_be32(block), (unsigned int *) &pc->c[3]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002027 pc->c[8] = partition;
Borislav Petkov346331f2008-04-18 00:46:26 +02002028 pc->flags |= PC_FLAG_WAIT_FOR_DSC;
Borislav Petkovd236d742008-04-18 00:46:27 +02002029 pc->idetape_callback = &idetape_pc_callback;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002030}
2031
Borislav Petkovd236d742008-04-18 00:46:27 +02002032static int idetape_create_prevent_cmd(ide_drive_t *drive,
2033 struct ide_atapi_pc *pc, int prevent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002034{
2035 idetape_tape_t *tape = drive->driver_data;
2036
Borislav Petkovb6422012008-02-02 19:56:49 +01002037 /* device supports locking according to capabilities page */
2038 if (!(tape->caps[6] & 0x01))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002039 return 0;
2040
2041 idetape_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +01002042 pc->c[0] = ALLOW_MEDIUM_REMOVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002043 pc->c[4] = prevent;
Borislav Petkovd236d742008-04-18 00:46:27 +02002044 pc->idetape_callback = &idetape_pc_callback;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002045 return 1;
2046}
2047
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002048static int __idetape_discard_read_pipeline(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002049{
2050 idetape_tape_t *tape = drive->driver_data;
2051 unsigned long flags;
2052 int cnt;
2053
Borislav Petkov54abf372008-02-06 02:57:52 +01002054 if (tape->chrdev_dir != IDETAPE_DIR_READ)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002055 return 0;
2056
2057 /* Remove merge stage. */
Borislav Petkov54bb2072008-02-06 02:57:52 +01002058 cnt = tape->merge_stage_size / tape->blk_size;
Borislav Petkov346331f2008-04-18 00:46:26 +02002059 if (test_and_clear_bit(IDETAPE_FLAG_FILEMARK, &tape->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002060 ++cnt; /* Filemarks count as 1 sector */
2061 tape->merge_stage_size = 0;
2062 if (tape->merge_stage != NULL) {
2063 __idetape_kfree_stage(tape->merge_stage);
2064 tape->merge_stage = NULL;
2065 }
2066
2067 /* Clear pipeline flags. */
Borislav Petkov346331f2008-04-18 00:46:26 +02002068 clear_bit(IDETAPE_FLAG_PIPELINE_ERR, &tape->flags);
Borislav Petkov54abf372008-02-06 02:57:52 +01002069 tape->chrdev_dir = IDETAPE_DIR_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002070
2071 /* Remove pipeline stages. */
2072 if (tape->first_stage == NULL)
2073 return 0;
2074
Borislav Petkov54bb2072008-02-06 02:57:52 +01002075 spin_lock_irqsave(&tape->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002076 tape->next_stage = NULL;
2077 if (idetape_pipeline_active(tape))
Borislav Petkov54bb2072008-02-06 02:57:52 +01002078 idetape_wait_for_request(drive, tape->active_data_rq);
2079 spin_unlock_irqrestore(&tape->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002080
2081 while (tape->first_stage != NULL) {
2082 struct request *rq_ptr = &tape->first_stage->rq;
2083
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002084 cnt += rq_ptr->nr_sectors - rq_ptr->current_nr_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002085 if (rq_ptr->errors == IDETAPE_ERROR_FILEMARK)
2086 ++cnt;
2087 idetape_remove_stage_head(drive);
2088 }
2089 tape->nr_pending_stages = 0;
2090 tape->max_stages = tape->min_pipeline;
2091 return cnt;
2092}
2093
2094/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +01002095 * Position the tape to the requested block using the LOCATE packet command.
2096 * A READ POSITION command is then issued to check where we are positioned. Like
2097 * all higher level operations, we queue the commands at the tail of the request
2098 * queue and wait for their completion.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002099 */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002100static int idetape_position_tape(ide_drive_t *drive, unsigned int block,
2101 u8 partition, int skip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002102{
2103 idetape_tape_t *tape = drive->driver_data;
2104 int retval;
Borislav Petkovd236d742008-04-18 00:46:27 +02002105 struct ide_atapi_pc pc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002106
Borislav Petkov54abf372008-02-06 02:57:52 +01002107 if (tape->chrdev_dir == IDETAPE_DIR_READ)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002108 __idetape_discard_read_pipeline(drive);
2109 idetape_wait_ready(drive, 60 * 5 * HZ);
2110 idetape_create_locate_cmd(drive, &pc, block, partition, skip);
2111 retval = idetape_queue_pc_tail(drive, &pc);
2112 if (retval)
2113 return (retval);
2114
2115 idetape_create_read_position_cmd(&pc);
2116 return (idetape_queue_pc_tail(drive, &pc));
2117}
2118
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002119static void idetape_discard_read_pipeline(ide_drive_t *drive,
2120 int restore_position)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002121{
2122 idetape_tape_t *tape = drive->driver_data;
2123 int cnt;
2124 int seek, position;
2125
2126 cnt = __idetape_discard_read_pipeline(drive);
2127 if (restore_position) {
2128 position = idetape_read_position(drive);
2129 seek = position > cnt ? position - cnt : 0;
2130 if (idetape_position_tape(drive, seek, 0, 0)) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002131 printk(KERN_INFO "ide-tape: %s: position_tape failed in"
2132 " discard_pipeline()\n", tape->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002133 return;
2134 }
2135 }
2136}
2137
2138/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +01002139 * Generate a read/write request for the block device interface and wait for it
2140 * to be serviced.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002141 */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002142static int idetape_queue_rw_tail(ide_drive_t *drive, int cmd, int blocks,
2143 struct idetape_bh *bh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002144{
2145 idetape_tape_t *tape = drive->driver_data;
2146 struct request rq;
2147
Borislav Petkov8004a8c2008-02-06 02:57:51 +01002148 debug_log(DBG_SENSE, "%s: cmd=%d\n", __func__, cmd);
2149
Linus Torvalds1da177e2005-04-16 15:20:36 -07002150 if (idetape_pipeline_active(tape)) {
Borislav Petkov8004a8c2008-02-06 02:57:51 +01002151 printk(KERN_ERR "ide-tape: bug: the pipeline is active in %s\n",
2152 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002153 return (0);
2154 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002155
2156 idetape_init_rq(&rq, cmd);
2157 rq.rq_disk = tape->disk;
2158 rq.special = (void *)bh;
Borislav Petkov54bb2072008-02-06 02:57:52 +01002159 rq.sector = tape->first_frame;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002160 rq.nr_sectors = blocks;
2161 rq.current_nr_sectors = blocks;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002162 (void) ide_do_drive_cmd(drive, &rq, ide_wait);
2163
2164 if ((cmd & (REQ_IDETAPE_READ | REQ_IDETAPE_WRITE)) == 0)
2165 return 0;
2166
2167 if (tape->merge_stage)
2168 idetape_init_merge_stage(tape);
2169 if (rq.errors == IDETAPE_ERROR_GENERAL)
2170 return -EIO;
Borislav Petkov54bb2072008-02-06 02:57:52 +01002171 return (tape->blk_size * (blocks-rq.current_nr_sectors));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002172}
2173
Borislav Petkov8d06bfa2008-02-06 02:57:53 +01002174/* start servicing the pipeline stages, starting from tape->next_stage. */
2175static void idetape_plug_pipeline(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002176{
2177 idetape_tape_t *tape = drive->driver_data;
2178
2179 if (tape->next_stage == NULL)
2180 return;
2181 if (!idetape_pipeline_active(tape)) {
Borislav Petkov346331f2008-04-18 00:46:26 +02002182 set_bit(IDETAPE_FLAG_PIPELINE_ACTIVE, &tape->flags);
Borislav Petkov419d4742008-02-02 19:56:51 +01002183 idetape_activate_next_stage(drive);
Borislav Petkov54bb2072008-02-06 02:57:52 +01002184 (void) ide_do_drive_cmd(drive, tape->active_data_rq, ide_end);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002185 }
2186}
2187
Borislav Petkovd236d742008-04-18 00:46:27 +02002188static void idetape_create_inquiry_cmd(struct ide_atapi_pc *pc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002189{
2190 idetape_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +01002191 pc->c[0] = INQUIRY;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002192 pc->c[4] = 254;
Borislav Petkovd236d742008-04-18 00:46:27 +02002193 pc->req_xfer = 254;
2194 pc->idetape_callback = &idetape_pc_callback;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002195}
2196
Borislav Petkovd236d742008-04-18 00:46:27 +02002197static void idetape_create_rewind_cmd(ide_drive_t *drive,
2198 struct ide_atapi_pc *pc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002199{
2200 idetape_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +01002201 pc->c[0] = REZERO_UNIT;
Borislav Petkov346331f2008-04-18 00:46:26 +02002202 pc->flags |= PC_FLAG_WAIT_FOR_DSC;
Borislav Petkovd236d742008-04-18 00:46:27 +02002203 pc->idetape_callback = &idetape_pc_callback;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002204}
2205
Borislav Petkovd236d742008-04-18 00:46:27 +02002206static void idetape_create_erase_cmd(struct ide_atapi_pc *pc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002207{
2208 idetape_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +01002209 pc->c[0] = ERASE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002210 pc->c[1] = 1;
Borislav Petkov346331f2008-04-18 00:46:26 +02002211 pc->flags |= PC_FLAG_WAIT_FOR_DSC;
Borislav Petkovd236d742008-04-18 00:46:27 +02002212 pc->idetape_callback = &idetape_pc_callback;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002213}
2214
Borislav Petkovd236d742008-04-18 00:46:27 +02002215static void idetape_create_space_cmd(struct ide_atapi_pc *pc, int count, u8 cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002216{
2217 idetape_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +01002218 pc->c[0] = SPACE;
Borislav Petkov860ff5e2008-02-02 19:56:50 +01002219 put_unaligned(cpu_to_be32(count), (unsigned int *) &pc->c[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002220 pc->c[1] = cmd;
Borislav Petkov346331f2008-04-18 00:46:26 +02002221 pc->flags |= PC_FLAG_WAIT_FOR_DSC;
Borislav Petkovd236d742008-04-18 00:46:27 +02002222 pc->idetape_callback = &idetape_pc_callback;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002223}
2224
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002225static void idetape_wait_first_stage(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002226{
2227 idetape_tape_t *tape = drive->driver_data;
2228 unsigned long flags;
2229
2230 if (tape->first_stage == NULL)
2231 return;
Borislav Petkov54bb2072008-02-06 02:57:52 +01002232 spin_lock_irqsave(&tape->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002233 if (tape->active_stage == tape->first_stage)
Borislav Petkov54bb2072008-02-06 02:57:52 +01002234 idetape_wait_for_request(drive, tape->active_data_rq);
2235 spin_unlock_irqrestore(&tape->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002236}
2237
2238/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +01002239 * Try to add a character device originated write request to our pipeline. In
2240 * case we don't succeed, we revert to non-pipelined operation mode for this
2241 * request. In order to accomplish that, we
Linus Torvalds1da177e2005-04-16 15:20:36 -07002242 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +01002243 * 1. Try to allocate a new pipeline stage.
2244 * 2. If we can't, wait for more and more requests to be serviced and try again
2245 * each time.
2246 * 3. If we still can't allocate a stage, fallback to non-pipelined operation
2247 * mode for this request.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002248 */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002249static int idetape_add_chrdev_write_request(ide_drive_t *drive, int blocks)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002250{
2251 idetape_tape_t *tape = drive->driver_data;
2252 idetape_stage_t *new_stage;
2253 unsigned long flags;
2254 struct request *rq;
2255
Borislav Petkov8004a8c2008-02-06 02:57:51 +01002256 debug_log(DBG_CHRDEV, "Enter %s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002257
Borislav Petkov3c98bf32008-02-06 02:57:53 +01002258 /* Attempt to allocate a new stage. Beware possible race conditions. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002259 while ((new_stage = idetape_kmalloc_stage(tape)) == NULL) {
Borislav Petkov54bb2072008-02-06 02:57:52 +01002260 spin_lock_irqsave(&tape->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002261 if (idetape_pipeline_active(tape)) {
Borislav Petkov54bb2072008-02-06 02:57:52 +01002262 idetape_wait_for_request(drive, tape->active_data_rq);
2263 spin_unlock_irqrestore(&tape->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002264 } else {
Borislav Petkov54bb2072008-02-06 02:57:52 +01002265 spin_unlock_irqrestore(&tape->lock, flags);
Borislav Petkov8d06bfa2008-02-06 02:57:53 +01002266 idetape_plug_pipeline(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002267 if (idetape_pipeline_active(tape))
2268 continue;
2269 /*
Borislav Petkov3c98bf32008-02-06 02:57:53 +01002270 * The machine is short on memory. Fallback to non-
2271 * pipelined operation mode for this request.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002272 */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002273 return idetape_queue_rw_tail(drive, REQ_IDETAPE_WRITE,
2274 blocks, tape->merge_stage->bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002275 }
2276 }
2277 rq = &new_stage->rq;
2278 idetape_init_rq(rq, REQ_IDETAPE_WRITE);
2279 /* Doesn't actually matter - We always assume sequential access */
Borislav Petkov54bb2072008-02-06 02:57:52 +01002280 rq->sector = tape->first_frame;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002281 rq->current_nr_sectors = blocks;
2282 rq->nr_sectors = blocks;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002283
2284 idetape_switch_buffers(tape, new_stage);
2285 idetape_add_stage_tail(drive, new_stage);
2286 tape->pipeline_head++;
Borislav Petkov37016ba2008-02-06 02:57:52 +01002287 idetape_calculate_speeds(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002288
2289 /*
Borislav Petkov3c98bf32008-02-06 02:57:53 +01002290 * Estimate whether the tape has stopped writing by checking if our
2291 * write pipeline is currently empty. If we are not writing anymore,
2292 * wait for the pipeline to be almost completely full (90%) before
2293 * starting to service requests, so that we will be able to keep up with
2294 * the higher speeds of the tape.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002295 */
2296 if (!idetape_pipeline_active(tape)) {
2297 if (tape->nr_stages >= tape->max_stages * 9 / 10 ||
Borislav Petkov54bb2072008-02-06 02:57:52 +01002298 tape->nr_stages >= tape->max_stages -
2299 tape->uncontrolled_pipeline_head_speed * 3 * 1024 /
2300 tape->blk_size) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002301 tape->measure_insert_time = 1;
2302 tape->insert_time = jiffies;
2303 tape->insert_size = 0;
2304 tape->insert_speed = 0;
Borislav Petkov8d06bfa2008-02-06 02:57:53 +01002305 idetape_plug_pipeline(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002306 }
2307 }
Borislav Petkov346331f2008-04-18 00:46:26 +02002308 if (test_and_clear_bit(IDETAPE_FLAG_PIPELINE_ERR, &tape->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002309 /* Return a deferred error */
2310 return -EIO;
2311 return blocks;
2312}
2313
2314/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +01002315 * Wait until all pending pipeline requests are serviced. Typically called on
2316 * device close.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002317 */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002318static void idetape_wait_for_pipeline(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002319{
2320 idetape_tape_t *tape = drive->driver_data;
2321 unsigned long flags;
2322
2323 while (tape->next_stage || idetape_pipeline_active(tape)) {
Borislav Petkov8d06bfa2008-02-06 02:57:53 +01002324 idetape_plug_pipeline(drive);
Borislav Petkov54bb2072008-02-06 02:57:52 +01002325 spin_lock_irqsave(&tape->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002326 if (idetape_pipeline_active(tape))
Borislav Petkov54bb2072008-02-06 02:57:52 +01002327 idetape_wait_for_request(drive, tape->active_data_rq);
2328 spin_unlock_irqrestore(&tape->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002329 }
2330}
2331
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002332static void idetape_empty_write_pipeline(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002333{
2334 idetape_tape_t *tape = drive->driver_data;
2335 int blocks, min;
2336 struct idetape_bh *bh;
Borislav Petkov55a5d292008-02-02 19:56:49 +01002337
Borislav Petkov54abf372008-02-06 02:57:52 +01002338 if (tape->chrdev_dir != IDETAPE_DIR_WRITE) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002339 printk(KERN_ERR "ide-tape: bug: Trying to empty write pipeline,"
2340 " but we are not writing.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002341 return;
2342 }
2343 if (tape->merge_stage_size > tape->stage_size) {
2344 printk(KERN_ERR "ide-tape: bug: merge_buffer too big\n");
2345 tape->merge_stage_size = tape->stage_size;
2346 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002347 if (tape->merge_stage_size) {
Borislav Petkov54bb2072008-02-06 02:57:52 +01002348 blocks = tape->merge_stage_size / tape->blk_size;
2349 if (tape->merge_stage_size % tape->blk_size) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002350 unsigned int i;
2351
2352 blocks++;
Borislav Petkov54bb2072008-02-06 02:57:52 +01002353 i = tape->blk_size - tape->merge_stage_size %
2354 tape->blk_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002355 bh = tape->bh->b_reqnext;
2356 while (bh) {
2357 atomic_set(&bh->b_count, 0);
2358 bh = bh->b_reqnext;
2359 }
2360 bh = tape->bh;
2361 while (i) {
2362 if (bh == NULL) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002363 printk(KERN_INFO "ide-tape: bug,"
2364 " bh NULL\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002365 break;
2366 }
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002367 min = min(i, (unsigned int)(bh->b_size -
2368 atomic_read(&bh->b_count)));
2369 memset(bh->b_data + atomic_read(&bh->b_count),
2370 0, min);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002371 atomic_add(min, &bh->b_count);
2372 i -= min;
2373 bh = bh->b_reqnext;
2374 }
2375 }
2376 (void) idetape_add_chrdev_write_request(drive, blocks);
2377 tape->merge_stage_size = 0;
2378 }
2379 idetape_wait_for_pipeline(drive);
2380 if (tape->merge_stage != NULL) {
2381 __idetape_kfree_stage(tape->merge_stage);
2382 tape->merge_stage = NULL;
2383 }
Borislav Petkov346331f2008-04-18 00:46:26 +02002384 clear_bit(IDETAPE_FLAG_PIPELINE_ERR, &tape->flags);
Borislav Petkov54abf372008-02-06 02:57:52 +01002385 tape->chrdev_dir = IDETAPE_DIR_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002386
2387 /*
Borislav Petkov3c98bf32008-02-06 02:57:53 +01002388 * On the next backup, perform the feedback loop again. (I don't want to
2389 * keep sense information between backups, as some systems are
2390 * constantly on, and the system load can be totally different on the
2391 * next backup).
Linus Torvalds1da177e2005-04-16 15:20:36 -07002392 */
2393 tape->max_stages = tape->min_pipeline;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002394 if (tape->first_stage != NULL ||
2395 tape->next_stage != NULL ||
2396 tape->last_stage != NULL ||
2397 tape->nr_stages != 0) {
2398 printk(KERN_ERR "ide-tape: ide-tape pipeline bug, "
2399 "first_stage %p, next_stage %p, "
2400 "last_stage %p, nr_stages %d\n",
2401 tape->first_stage, tape->next_stage,
2402 tape->last_stage, tape->nr_stages);
2403 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002404}
2405
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002406static void idetape_restart_speed_control(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002407{
2408 idetape_tape_t *tape = drive->driver_data;
2409
2410 tape->restart_speed_control_req = 0;
2411 tape->pipeline_head = 0;
Borislav Petkov41f81d542008-02-06 02:57:52 +01002412 tape->controlled_last_pipeline_head = 0;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002413 tape->controlled_previous_pipeline_head = 0;
2414 tape->uncontrolled_previous_pipeline_head = 0;
2415 tape->controlled_pipeline_head_speed = 5000;
2416 tape->pipeline_head_speed = 5000;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002417 tape->uncontrolled_pipeline_head_speed = 0;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002418 tape->controlled_pipeline_head_time =
2419 tape->uncontrolled_pipeline_head_time = jiffies;
2420 tape->controlled_previous_head_time =
2421 tape->uncontrolled_previous_head_time = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002422}
2423
Borislav Petkov8d06bfa2008-02-06 02:57:53 +01002424static int idetape_init_read(ide_drive_t *drive, int max_stages)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002425{
2426 idetape_tape_t *tape = drive->driver_data;
2427 idetape_stage_t *new_stage;
2428 struct request rq;
2429 int bytes_read;
Borislav Petkovb6422012008-02-02 19:56:49 +01002430 u16 blocks = *(u16 *)&tape->caps[12];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002431
2432 /* Initialize read operation */
Borislav Petkov54abf372008-02-06 02:57:52 +01002433 if (tape->chrdev_dir != IDETAPE_DIR_READ) {
2434 if (tape->chrdev_dir == IDETAPE_DIR_WRITE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002435 idetape_empty_write_pipeline(drive);
2436 idetape_flush_tape_buffers(drive);
2437 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002438 if (tape->merge_stage || tape->merge_stage_size) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002439 printk(KERN_ERR "ide-tape: merge_stage_size should be"
2440 " 0 now\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002441 tape->merge_stage_size = 0;
2442 }
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002443 tape->merge_stage = __idetape_kmalloc_stage(tape, 0, 0);
2444 if (!tape->merge_stage)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002445 return -ENOMEM;
Borislav Petkov54abf372008-02-06 02:57:52 +01002446 tape->chrdev_dir = IDETAPE_DIR_READ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002447
2448 /*
Borislav Petkov3c98bf32008-02-06 02:57:53 +01002449 * Issue a read 0 command to ensure that DSC handshake is
2450 * switched from completion mode to buffer available mode.
2451 * No point in issuing this if DSC overlap isn't supported, some
2452 * drives (Seagate STT3401A) will return an error.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002453 */
2454 if (drive->dsc_overlap) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002455 bytes_read = idetape_queue_rw_tail(drive,
2456 REQ_IDETAPE_READ, 0,
2457 tape->merge_stage->bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002458 if (bytes_read < 0) {
2459 __idetape_kfree_stage(tape->merge_stage);
2460 tape->merge_stage = NULL;
Borislav Petkov54abf372008-02-06 02:57:52 +01002461 tape->chrdev_dir = IDETAPE_DIR_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002462 return bytes_read;
2463 }
2464 }
2465 }
2466 if (tape->restart_speed_control_req)
2467 idetape_restart_speed_control(drive);
2468 idetape_init_rq(&rq, REQ_IDETAPE_READ);
Borislav Petkov54bb2072008-02-06 02:57:52 +01002469 rq.sector = tape->first_frame;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002470 rq.nr_sectors = blocks;
2471 rq.current_nr_sectors = blocks;
Borislav Petkov346331f2008-04-18 00:46:26 +02002472 if (!test_bit(IDETAPE_FLAG_PIPELINE_ERR, &tape->flags) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07002473 tape->nr_stages < max_stages) {
2474 new_stage = idetape_kmalloc_stage(tape);
2475 while (new_stage != NULL) {
2476 new_stage->rq = rq;
2477 idetape_add_stage_tail(drive, new_stage);
2478 if (tape->nr_stages >= max_stages)
2479 break;
2480 new_stage = idetape_kmalloc_stage(tape);
2481 }
2482 }
2483 if (!idetape_pipeline_active(tape)) {
2484 if (tape->nr_pending_stages >= 3 * max_stages / 4) {
2485 tape->measure_insert_time = 1;
2486 tape->insert_time = jiffies;
2487 tape->insert_size = 0;
2488 tape->insert_speed = 0;
Borislav Petkov8d06bfa2008-02-06 02:57:53 +01002489 idetape_plug_pipeline(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002490 }
2491 }
2492 return 0;
2493}
2494
2495/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +01002496 * Called from idetape_chrdev_read() to service a character device read request
2497 * and add read-ahead requests to our pipeline.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002498 */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002499static int idetape_add_chrdev_read_request(ide_drive_t *drive, int blocks)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002500{
2501 idetape_tape_t *tape = drive->driver_data;
2502 unsigned long flags;
2503 struct request *rq_ptr;
2504 int bytes_read;
2505
Borislav Petkov8004a8c2008-02-06 02:57:51 +01002506 debug_log(DBG_PROCS, "Enter %s, %d blocks\n", __func__, blocks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002507
Borislav Petkov3c98bf32008-02-06 02:57:53 +01002508 /* If we are at a filemark, return a read length of 0 */
Borislav Petkov346331f2008-04-18 00:46:26 +02002509 if (test_bit(IDETAPE_FLAG_FILEMARK, &tape->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002510 return 0;
2511
Borislav Petkov3c98bf32008-02-06 02:57:53 +01002512 /* Wait for the next block to reach the head of the pipeline. */
Borislav Petkov8d06bfa2008-02-06 02:57:53 +01002513 idetape_init_read(drive, tape->max_stages);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002514 if (tape->first_stage == NULL) {
Borislav Petkov346331f2008-04-18 00:46:26 +02002515 if (test_bit(IDETAPE_FLAG_PIPELINE_ERR, &tape->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002516 return 0;
Borislav Petkov54bb2072008-02-06 02:57:52 +01002517 return idetape_queue_rw_tail(drive, REQ_IDETAPE_READ, blocks,
2518 tape->merge_stage->bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002519 }
2520 idetape_wait_first_stage(drive);
2521 rq_ptr = &tape->first_stage->rq;
Borislav Petkov54bb2072008-02-06 02:57:52 +01002522 bytes_read = tape->blk_size * (rq_ptr->nr_sectors -
2523 rq_ptr->current_nr_sectors);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002524 rq_ptr->nr_sectors = 0;
2525 rq_ptr->current_nr_sectors = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002526
2527 if (rq_ptr->errors == IDETAPE_ERROR_EOD)
2528 return 0;
2529 else {
2530 idetape_switch_buffers(tape, tape->first_stage);
2531 if (rq_ptr->errors == IDETAPE_ERROR_FILEMARK)
Borislav Petkov346331f2008-04-18 00:46:26 +02002532 set_bit(IDETAPE_FLAG_FILEMARK, &tape->flags);
Borislav Petkov54bb2072008-02-06 02:57:52 +01002533 spin_lock_irqsave(&tape->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002534 idetape_remove_stage_head(drive);
Borislav Petkov54bb2072008-02-06 02:57:52 +01002535 spin_unlock_irqrestore(&tape->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002536 tape->pipeline_head++;
Borislav Petkov37016ba2008-02-06 02:57:52 +01002537 idetape_calculate_speeds(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002538 }
Borislav Petkov54bb2072008-02-06 02:57:52 +01002539 if (bytes_read > blocks * tape->blk_size) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002540 printk(KERN_ERR "ide-tape: bug: trying to return more bytes"
2541 " than requested\n");
Borislav Petkov54bb2072008-02-06 02:57:52 +01002542 bytes_read = blocks * tape->blk_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002543 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002544 return (bytes_read);
2545}
2546
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002547static void idetape_pad_zeros(ide_drive_t *drive, int bcount)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002548{
2549 idetape_tape_t *tape = drive->driver_data;
2550 struct idetape_bh *bh;
2551 int blocks;
Borislav Petkov3c98bf32008-02-06 02:57:53 +01002552
Linus Torvalds1da177e2005-04-16 15:20:36 -07002553 while (bcount) {
2554 unsigned int count;
2555
2556 bh = tape->merge_stage->bh;
2557 count = min(tape->stage_size, bcount);
2558 bcount -= count;
Borislav Petkov54bb2072008-02-06 02:57:52 +01002559 blocks = count / tape->blk_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002560 while (count) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002561 atomic_set(&bh->b_count,
2562 min(count, (unsigned int)bh->b_size));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002563 memset(bh->b_data, 0, atomic_read(&bh->b_count));
2564 count -= atomic_read(&bh->b_count);
2565 bh = bh->b_reqnext;
2566 }
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002567 idetape_queue_rw_tail(drive, REQ_IDETAPE_WRITE, blocks,
2568 tape->merge_stage->bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002569 }
2570}
2571
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002572static int idetape_pipeline_size(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002573{
2574 idetape_tape_t *tape = drive->driver_data;
2575 idetape_stage_t *stage;
2576 struct request *rq;
2577 int size = 0;
2578
2579 idetape_wait_for_pipeline(drive);
2580 stage = tape->first_stage;
2581 while (stage != NULL) {
2582 rq = &stage->rq;
Borislav Petkov54bb2072008-02-06 02:57:52 +01002583 size += tape->blk_size * (rq->nr_sectors -
2584 rq->current_nr_sectors);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002585 if (rq->errors == IDETAPE_ERROR_FILEMARK)
Borislav Petkov54bb2072008-02-06 02:57:52 +01002586 size += tape->blk_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002587 stage = stage->next;
2588 }
2589 size += tape->merge_stage_size;
2590 return size;
2591}
2592
2593/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +01002594 * Rewinds the tape to the Beginning Of the current Partition (BOP). We
2595 * currently support only one partition.
2596 */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002597static int idetape_rewind_tape(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002598{
2599 int retval;
Borislav Petkovd236d742008-04-18 00:46:27 +02002600 struct ide_atapi_pc pc;
Borislav Petkov8004a8c2008-02-06 02:57:51 +01002601 idetape_tape_t *tape;
2602 tape = drive->driver_data;
2603
2604 debug_log(DBG_SENSE, "Enter %s\n", __func__);
2605
Linus Torvalds1da177e2005-04-16 15:20:36 -07002606 idetape_create_rewind_cmd(drive, &pc);
2607 retval = idetape_queue_pc_tail(drive, &pc);
2608 if (retval)
2609 return retval;
2610
2611 idetape_create_read_position_cmd(&pc);
2612 retval = idetape_queue_pc_tail(drive, &pc);
2613 if (retval)
2614 return retval;
2615 return 0;
2616}
2617
Borislav Petkov3c98bf32008-02-06 02:57:53 +01002618/* mtio.h compatible commands should be issued to the chrdev interface. */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002619static int idetape_blkdev_ioctl(ide_drive_t *drive, unsigned int cmd,
2620 unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002621{
2622 idetape_tape_t *tape = drive->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002623 void __user *argp = (void __user *)arg;
2624
Borislav Petkovd59823f2008-02-02 19:56:51 +01002625 struct idetape_config {
2626 int dsc_rw_frequency;
2627 int dsc_media_access_frequency;
2628 int nr_stages;
2629 } config;
2630
Borislav Petkov8004a8c2008-02-06 02:57:51 +01002631 debug_log(DBG_PROCS, "Enter %s\n", __func__);
2632
Linus Torvalds1da177e2005-04-16 15:20:36 -07002633 switch (cmd) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002634 case 0x0340:
2635 if (copy_from_user(&config, argp, sizeof(config)))
2636 return -EFAULT;
2637 tape->best_dsc_rw_freq = config.dsc_rw_frequency;
2638 tape->max_stages = config.nr_stages;
2639 break;
2640 case 0x0350:
2641 config.dsc_rw_frequency = (int) tape->best_dsc_rw_freq;
2642 config.nr_stages = tape->max_stages;
2643 if (copy_to_user(argp, &config, sizeof(config)))
2644 return -EFAULT;
2645 break;
2646 default:
2647 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002648 }
2649 return 0;
2650}
2651
2652/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +01002653 * The function below is now a bit more complicated than just passing the
2654 * command to the tape since we may have crossed some filemarks during our
2655 * pipelined read-ahead mode. As a minor side effect, the pipeline enables us to
2656 * support MTFSFM when the filemark is in our internal pipeline even if the tape
2657 * doesn't support spacing over filemarks in the reverse direction.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002658 */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002659static int idetape_space_over_filemarks(ide_drive_t *drive, short mt_op,
2660 int mt_count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002661{
2662 idetape_tape_t *tape = drive->driver_data;
Borislav Petkovd236d742008-04-18 00:46:27 +02002663 struct ide_atapi_pc pc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002664 unsigned long flags;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002665 int retval, count = 0;
Borislav Petkovb6422012008-02-02 19:56:49 +01002666 int sprev = !!(tape->caps[4] & 0x20);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002667
2668 if (mt_count == 0)
2669 return 0;
2670 if (MTBSF == mt_op || MTBSFM == mt_op) {
Borislav Petkovb6422012008-02-02 19:56:49 +01002671 if (!sprev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002672 return -EIO;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002673 mt_count = -mt_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002674 }
2675
Borislav Petkov54abf372008-02-06 02:57:52 +01002676 if (tape->chrdev_dir == IDETAPE_DIR_READ) {
Borislav Petkov3c98bf32008-02-06 02:57:53 +01002677 /* its a read-ahead buffer, scan it for crossed filemarks. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002678 tape->merge_stage_size = 0;
Borislav Petkov346331f2008-04-18 00:46:26 +02002679 if (test_and_clear_bit(IDETAPE_FLAG_FILEMARK, &tape->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002680 ++count;
2681 while (tape->first_stage != NULL) {
2682 if (count == mt_count) {
2683 if (mt_op == MTFSFM)
Borislav Petkov346331f2008-04-18 00:46:26 +02002684 set_bit(IDETAPE_FLAG_FILEMARK,
2685 &tape->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002686 return 0;
2687 }
Borislav Petkov54bb2072008-02-06 02:57:52 +01002688 spin_lock_irqsave(&tape->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002689 if (tape->first_stage == tape->active_stage) {
2690 /*
Borislav Petkov3c98bf32008-02-06 02:57:53 +01002691 * We have reached the active stage in the read
2692 * pipeline. There is no point in allowing the
2693 * drive to continue reading any farther, so we
2694 * stop the pipeline.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002695 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +01002696 * This section should be moved to a separate
2697 * subroutine because similar operations are
2698 * done in __idetape_discard_read_pipeline(),
2699 * for example.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002700 */
2701 tape->next_stage = NULL;
Borislav Petkov54bb2072008-02-06 02:57:52 +01002702 spin_unlock_irqrestore(&tape->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002703 idetape_wait_first_stage(drive);
2704 tape->next_stage = tape->first_stage->next;
2705 } else
Borislav Petkov54bb2072008-02-06 02:57:52 +01002706 spin_unlock_irqrestore(&tape->lock, flags);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002707 if (tape->first_stage->rq.errors ==
2708 IDETAPE_ERROR_FILEMARK)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002709 ++count;
2710 idetape_remove_stage_head(drive);
2711 }
2712 idetape_discard_read_pipeline(drive, 0);
2713 }
2714
2715 /*
Borislav Petkov3c98bf32008-02-06 02:57:53 +01002716 * The filemark was not found in our internal pipeline; now we can issue
2717 * the space command.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002718 */
2719 switch (mt_op) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002720 case MTFSF:
2721 case MTBSF:
2722 idetape_create_space_cmd(&pc, mt_count - count,
2723 IDETAPE_SPACE_OVER_FILEMARK);
2724 return idetape_queue_pc_tail(drive, &pc);
2725 case MTFSFM:
2726 case MTBSFM:
2727 if (!sprev)
2728 return -EIO;
2729 retval = idetape_space_over_filemarks(drive, MTFSF,
2730 mt_count - count);
2731 if (retval)
2732 return retval;
2733 count = (MTBSFM == mt_op ? 1 : -1);
2734 return idetape_space_over_filemarks(drive, MTFSF, count);
2735 default:
2736 printk(KERN_ERR "ide-tape: MTIO operation %d not supported\n",
2737 mt_op);
2738 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002739 }
2740}
2741
Linus Torvalds1da177e2005-04-16 15:20:36 -07002742/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +01002743 * Our character device read / write functions.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002744 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +01002745 * The tape is optimized to maximize throughput when it is transferring an
2746 * integral number of the "continuous transfer limit", which is a parameter of
2747 * the specific tape (26kB on my particular tape, 32kB for Onstream).
Linus Torvalds1da177e2005-04-16 15:20:36 -07002748 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +01002749 * As of version 1.3 of the driver, the character device provides an abstract
2750 * continuous view of the media - any mix of block sizes (even 1 byte) on the
2751 * same backup/restore procedure is supported. The driver will internally
2752 * convert the requests to the recommended transfer unit, so that an unmatch
2753 * between the user's block size to the recommended size will only result in a
2754 * (slightly) increased driver overhead, but will no longer hit performance.
2755 * This is not applicable to Onstream.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002756 */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002757static ssize_t idetape_chrdev_read(struct file *file, char __user *buf,
2758 size_t count, loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002759{
2760 struct ide_tape_obj *tape = ide_tape_f(file);
2761 ide_drive_t *drive = tape->drive;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002762 ssize_t bytes_read, temp, actually_read = 0, rc;
Daniel Walkerdcd96372006-06-25 05:47:37 -07002763 ssize_t ret = 0;
Borislav Petkovb6422012008-02-02 19:56:49 +01002764 u16 ctl = *(u16 *)&tape->caps[12];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002765
Borislav Petkov8004a8c2008-02-06 02:57:51 +01002766 debug_log(DBG_CHRDEV, "Enter %s, count %Zd\n", __func__, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002767
Borislav Petkov54abf372008-02-06 02:57:52 +01002768 if (tape->chrdev_dir != IDETAPE_DIR_READ) {
Borislav Petkov346331f2008-04-18 00:46:26 +02002769 if (test_bit(IDETAPE_FLAG_DETECT_BS, &tape->flags))
Borislav Petkov54bb2072008-02-06 02:57:52 +01002770 if (count > tape->blk_size &&
2771 (count % tape->blk_size) == 0)
2772 tape->user_bs_factor = count / tape->blk_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002773 }
Borislav Petkov8d06bfa2008-02-06 02:57:53 +01002774 rc = idetape_init_read(drive, tape->max_stages);
2775 if (rc < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002776 return rc;
2777 if (count == 0)
2778 return (0);
2779 if (tape->merge_stage_size) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002780 actually_read = min((unsigned int)(tape->merge_stage_size),
2781 (unsigned int)count);
2782 if (idetape_copy_stage_to_user(tape, buf, tape->merge_stage,
2783 actually_read))
Daniel Walkerdcd96372006-06-25 05:47:37 -07002784 ret = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002785 buf += actually_read;
2786 tape->merge_stage_size -= actually_read;
2787 count -= actually_read;
2788 }
2789 while (count >= tape->stage_size) {
Borislav Petkovb6422012008-02-02 19:56:49 +01002790 bytes_read = idetape_add_chrdev_read_request(drive, ctl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002791 if (bytes_read <= 0)
2792 goto finish;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002793 if (idetape_copy_stage_to_user(tape, buf, tape->merge_stage,
2794 bytes_read))
Daniel Walkerdcd96372006-06-25 05:47:37 -07002795 ret = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002796 buf += bytes_read;
2797 count -= bytes_read;
2798 actually_read += bytes_read;
2799 }
2800 if (count) {
Borislav Petkovb6422012008-02-02 19:56:49 +01002801 bytes_read = idetape_add_chrdev_read_request(drive, ctl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002802 if (bytes_read <= 0)
2803 goto finish;
2804 temp = min((unsigned long)count, (unsigned long)bytes_read);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002805 if (idetape_copy_stage_to_user(tape, buf, tape->merge_stage,
2806 temp))
Daniel Walkerdcd96372006-06-25 05:47:37 -07002807 ret = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002808 actually_read += temp;
2809 tape->merge_stage_size = bytes_read-temp;
2810 }
2811finish:
Borislav Petkov346331f2008-04-18 00:46:26 +02002812 if (!actually_read && test_bit(IDETAPE_FLAG_FILEMARK, &tape->flags)) {
Borislav Petkov8004a8c2008-02-06 02:57:51 +01002813 debug_log(DBG_SENSE, "%s: spacing over filemark\n", tape->name);
2814
Linus Torvalds1da177e2005-04-16 15:20:36 -07002815 idetape_space_over_filemarks(drive, MTFSF, 1);
2816 return 0;
2817 }
Daniel Walkerdcd96372006-06-25 05:47:37 -07002818
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002819 return ret ? ret : actually_read;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002820}
2821
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002822static ssize_t idetape_chrdev_write(struct file *file, const char __user *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002823 size_t count, loff_t *ppos)
2824{
2825 struct ide_tape_obj *tape = ide_tape_f(file);
2826 ide_drive_t *drive = tape->drive;
Daniel Walkerdcd96372006-06-25 05:47:37 -07002827 ssize_t actually_written = 0;
2828 ssize_t ret = 0;
Borislav Petkovb6422012008-02-02 19:56:49 +01002829 u16 ctl = *(u16 *)&tape->caps[12];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002830
2831 /* The drive is write protected. */
2832 if (tape->write_prot)
2833 return -EACCES;
2834
Borislav Petkov8004a8c2008-02-06 02:57:51 +01002835 debug_log(DBG_CHRDEV, "Enter %s, count %Zd\n", __func__, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002836
2837 /* Initialize write operation */
Borislav Petkov54abf372008-02-06 02:57:52 +01002838 if (tape->chrdev_dir != IDETAPE_DIR_WRITE) {
2839 if (tape->chrdev_dir == IDETAPE_DIR_READ)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002840 idetape_discard_read_pipeline(drive, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002841 if (tape->merge_stage || tape->merge_stage_size) {
2842 printk(KERN_ERR "ide-tape: merge_stage_size "
2843 "should be 0 now\n");
2844 tape->merge_stage_size = 0;
2845 }
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002846 tape->merge_stage = __idetape_kmalloc_stage(tape, 0, 0);
2847 if (!tape->merge_stage)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002848 return -ENOMEM;
Borislav Petkov54abf372008-02-06 02:57:52 +01002849 tape->chrdev_dir = IDETAPE_DIR_WRITE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002850 idetape_init_merge_stage(tape);
2851
2852 /*
Borislav Petkov3c98bf32008-02-06 02:57:53 +01002853 * Issue a write 0 command to ensure that DSC handshake is
2854 * switched from completion mode to buffer available mode. No
2855 * point in issuing this if DSC overlap isn't supported, some
2856 * drives (Seagate STT3401A) will return an error.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002857 */
2858 if (drive->dsc_overlap) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002859 ssize_t retval = idetape_queue_rw_tail(drive,
2860 REQ_IDETAPE_WRITE, 0,
2861 tape->merge_stage->bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002862 if (retval < 0) {
2863 __idetape_kfree_stage(tape->merge_stage);
2864 tape->merge_stage = NULL;
Borislav Petkov54abf372008-02-06 02:57:52 +01002865 tape->chrdev_dir = IDETAPE_DIR_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002866 return retval;
2867 }
2868 }
2869 }
2870 if (count == 0)
2871 return (0);
2872 if (tape->restart_speed_control_req)
2873 idetape_restart_speed_control(drive);
2874 if (tape->merge_stage_size) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002875 if (tape->merge_stage_size >= tape->stage_size) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002876 printk(KERN_ERR "ide-tape: bug: merge buf too big\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002877 tape->merge_stage_size = 0;
2878 }
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002879 actually_written = min((unsigned int)
2880 (tape->stage_size - tape->merge_stage_size),
2881 (unsigned int)count);
2882 if (idetape_copy_stage_from_user(tape, tape->merge_stage, buf,
2883 actually_written))
Daniel Walkerdcd96372006-06-25 05:47:37 -07002884 ret = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002885 buf += actually_written;
2886 tape->merge_stage_size += actually_written;
2887 count -= actually_written;
2888
2889 if (tape->merge_stage_size == tape->stage_size) {
Daniel Walkerdcd96372006-06-25 05:47:37 -07002890 ssize_t retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002891 tape->merge_stage_size = 0;
Borislav Petkovb6422012008-02-02 19:56:49 +01002892 retval = idetape_add_chrdev_write_request(drive, ctl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002893 if (retval <= 0)
2894 return (retval);
2895 }
2896 }
2897 while (count >= tape->stage_size) {
Daniel Walkerdcd96372006-06-25 05:47:37 -07002898 ssize_t retval;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002899 if (idetape_copy_stage_from_user(tape, tape->merge_stage, buf,
2900 tape->stage_size))
Daniel Walkerdcd96372006-06-25 05:47:37 -07002901 ret = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002902 buf += tape->stage_size;
2903 count -= tape->stage_size;
Borislav Petkovb6422012008-02-02 19:56:49 +01002904 retval = idetape_add_chrdev_write_request(drive, ctl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002905 actually_written += tape->stage_size;
2906 if (retval <= 0)
2907 return (retval);
2908 }
2909 if (count) {
2910 actually_written += count;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002911 if (idetape_copy_stage_from_user(tape, tape->merge_stage, buf,
2912 count))
Daniel Walkerdcd96372006-06-25 05:47:37 -07002913 ret = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002914 tape->merge_stage_size += count;
2915 }
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002916 return ret ? ret : actually_written;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002917}
2918
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002919static int idetape_write_filemark(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002920{
Borislav Petkovd236d742008-04-18 00:46:27 +02002921 struct ide_atapi_pc pc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002922
2923 /* Write a filemark */
2924 idetape_create_write_filemark_cmd(drive, &pc, 1);
2925 if (idetape_queue_pc_tail(drive, &pc)) {
2926 printk(KERN_ERR "ide-tape: Couldn't write a filemark\n");
2927 return -EIO;
2928 }
2929 return 0;
2930}
2931
2932/*
Borislav Petkovd99c9da2008-02-02 19:56:51 +01002933 * Called from idetape_chrdev_ioctl when the general mtio MTIOCTOP ioctl is
2934 * requested.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002935 *
Borislav Petkovd99c9da2008-02-02 19:56:51 +01002936 * Note: MTBSF and MTBSFM are not supported when the tape doesn't support
2937 * spacing over filemarks in the reverse direction. In this case, MTFSFM is also
2938 * usually not supported (it is supported in the rare case in which we crossed
2939 * the filemark during our read-ahead pipelined operation mode).
Linus Torvalds1da177e2005-04-16 15:20:36 -07002940 *
Borislav Petkovd99c9da2008-02-02 19:56:51 +01002941 * The following commands are currently not supported:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002942 *
Borislav Petkovd99c9da2008-02-02 19:56:51 +01002943 * MTFSS, MTBSS, MTWSM, MTSETDENSITY, MTSETDRVBUFFER, MT_ST_BOOLEANS,
2944 * MT_ST_WRITE_THRESHOLD.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002945 */
Borislav Petkovd99c9da2008-02-02 19:56:51 +01002946static int idetape_mtioctop(ide_drive_t *drive, short mt_op, int mt_count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002947{
2948 idetape_tape_t *tape = drive->driver_data;
Borislav Petkovd236d742008-04-18 00:46:27 +02002949 struct ide_atapi_pc pc;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002950 int i, retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002951
Borislav Petkov8004a8c2008-02-06 02:57:51 +01002952 debug_log(DBG_ERR, "Handling MTIOCTOP ioctl: mt_op=%d, mt_count=%d\n",
2953 mt_op, mt_count);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002954
Borislav Petkov3c98bf32008-02-06 02:57:53 +01002955 /* Commands which need our pipelined read-ahead stages. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002956 switch (mt_op) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002957 case MTFSF:
2958 case MTFSFM:
2959 case MTBSF:
2960 case MTBSFM:
2961 if (!mt_count)
2962 return 0;
2963 return idetape_space_over_filemarks(drive, mt_op, mt_count);
2964 default:
2965 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002966 }
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002967
Linus Torvalds1da177e2005-04-16 15:20:36 -07002968 switch (mt_op) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002969 case MTWEOF:
2970 if (tape->write_prot)
2971 return -EACCES;
2972 idetape_discard_read_pipeline(drive, 1);
2973 for (i = 0; i < mt_count; i++) {
2974 retval = idetape_write_filemark(drive);
2975 if (retval)
2976 return retval;
2977 }
2978 return 0;
2979 case MTREW:
2980 idetape_discard_read_pipeline(drive, 0);
2981 if (idetape_rewind_tape(drive))
2982 return -EIO;
2983 return 0;
2984 case MTLOAD:
2985 idetape_discard_read_pipeline(drive, 0);
2986 idetape_create_load_unload_cmd(drive, &pc,
2987 IDETAPE_LU_LOAD_MASK);
2988 return idetape_queue_pc_tail(drive, &pc);
2989 case MTUNLOAD:
2990 case MTOFFL:
2991 /*
2992 * If door is locked, attempt to unlock before
2993 * attempting to eject.
2994 */
2995 if (tape->door_locked) {
2996 if (idetape_create_prevent_cmd(drive, &pc, 0))
2997 if (!idetape_queue_pc_tail(drive, &pc))
2998 tape->door_locked = DOOR_UNLOCKED;
2999 }
3000 idetape_discard_read_pipeline(drive, 0);
3001 idetape_create_load_unload_cmd(drive, &pc,
3002 !IDETAPE_LU_LOAD_MASK);
3003 retval = idetape_queue_pc_tail(drive, &pc);
3004 if (!retval)
Borislav Petkov346331f2008-04-18 00:46:26 +02003005 clear_bit(IDETAPE_FLAG_MEDIUM_PRESENT, &tape->flags);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01003006 return retval;
3007 case MTNOP:
3008 idetape_discard_read_pipeline(drive, 0);
3009 return idetape_flush_tape_buffers(drive);
3010 case MTRETEN:
3011 idetape_discard_read_pipeline(drive, 0);
3012 idetape_create_load_unload_cmd(drive, &pc,
3013 IDETAPE_LU_RETENSION_MASK | IDETAPE_LU_LOAD_MASK);
3014 return idetape_queue_pc_tail(drive, &pc);
3015 case MTEOM:
3016 idetape_create_space_cmd(&pc, 0, IDETAPE_SPACE_TO_EOD);
3017 return idetape_queue_pc_tail(drive, &pc);
3018 case MTERASE:
3019 (void)idetape_rewind_tape(drive);
3020 idetape_create_erase_cmd(&pc);
3021 return idetape_queue_pc_tail(drive, &pc);
3022 case MTSETBLK:
3023 if (mt_count) {
3024 if (mt_count < tape->blk_size ||
3025 mt_count % tape->blk_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003026 return -EIO;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01003027 tape->user_bs_factor = mt_count / tape->blk_size;
Borislav Petkov346331f2008-04-18 00:46:26 +02003028 clear_bit(IDETAPE_FLAG_DETECT_BS, &tape->flags);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01003029 } else
Borislav Petkov346331f2008-04-18 00:46:26 +02003030 set_bit(IDETAPE_FLAG_DETECT_BS, &tape->flags);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01003031 return 0;
3032 case MTSEEK:
3033 idetape_discard_read_pipeline(drive, 0);
3034 return idetape_position_tape(drive,
3035 mt_count * tape->user_bs_factor, tape->partition, 0);
3036 case MTSETPART:
3037 idetape_discard_read_pipeline(drive, 0);
3038 return idetape_position_tape(drive, 0, mt_count, 0);
3039 case MTFSR:
3040 case MTBSR:
3041 case MTLOCK:
3042 if (!idetape_create_prevent_cmd(drive, &pc, 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003043 return 0;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01003044 retval = idetape_queue_pc_tail(drive, &pc);
3045 if (retval)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003046 return retval;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01003047 tape->door_locked = DOOR_EXPLICITLY_LOCKED;
3048 return 0;
3049 case MTUNLOCK:
3050 if (!idetape_create_prevent_cmd(drive, &pc, 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003051 return 0;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01003052 retval = idetape_queue_pc_tail(drive, &pc);
3053 if (retval)
3054 return retval;
3055 tape->door_locked = DOOR_UNLOCKED;
3056 return 0;
3057 default:
3058 printk(KERN_ERR "ide-tape: MTIO operation %d not supported\n",
3059 mt_op);
3060 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003061 }
3062}
3063
3064/*
Borislav Petkovd99c9da2008-02-02 19:56:51 +01003065 * Our character device ioctls. General mtio.h magnetic io commands are
3066 * supported here, and not in the corresponding block interface. Our own
3067 * ide-tape ioctls are supported on both interfaces.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003068 */
Borislav Petkovd99c9da2008-02-02 19:56:51 +01003069static int idetape_chrdev_ioctl(struct inode *inode, struct file *file,
3070 unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003071{
3072 struct ide_tape_obj *tape = ide_tape_f(file);
3073 ide_drive_t *drive = tape->drive;
3074 struct mtop mtop;
3075 struct mtget mtget;
3076 struct mtpos mtpos;
Borislav Petkov54bb2072008-02-06 02:57:52 +01003077 int block_offset = 0, position = tape->first_frame;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003078 void __user *argp = (void __user *)arg;
3079
Borislav Petkov8004a8c2008-02-06 02:57:51 +01003080 debug_log(DBG_CHRDEV, "Enter %s, cmd=%u\n", __func__, cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003081
3082 tape->restart_speed_control_req = 1;
Borislav Petkov54abf372008-02-06 02:57:52 +01003083 if (tape->chrdev_dir == IDETAPE_DIR_WRITE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003084 idetape_empty_write_pipeline(drive);
3085 idetape_flush_tape_buffers(drive);
3086 }
3087 if (cmd == MTIOCGET || cmd == MTIOCPOS) {
Borislav Petkov54bb2072008-02-06 02:57:52 +01003088 block_offset = idetape_pipeline_size(drive) /
3089 (tape->blk_size * tape->user_bs_factor);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01003090 position = idetape_read_position(drive);
3091 if (position < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003092 return -EIO;
3093 }
3094 switch (cmd) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01003095 case MTIOCTOP:
3096 if (copy_from_user(&mtop, argp, sizeof(struct mtop)))
3097 return -EFAULT;
3098 return idetape_mtioctop(drive, mtop.mt_op, mtop.mt_count);
3099 case MTIOCGET:
3100 memset(&mtget, 0, sizeof(struct mtget));
3101 mtget.mt_type = MT_ISSCSI2;
3102 mtget.mt_blkno = position / tape->user_bs_factor - block_offset;
3103 mtget.mt_dsreg =
3104 ((tape->blk_size * tape->user_bs_factor)
3105 << MT_ST_BLKSIZE_SHIFT) & MT_ST_BLKSIZE_MASK;
Borislav Petkov54bb2072008-02-06 02:57:52 +01003106
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01003107 if (tape->drv_write_prot)
3108 mtget.mt_gstat |= GMT_WR_PROT(0xffffffff);
3109
3110 if (copy_to_user(argp, &mtget, sizeof(struct mtget)))
3111 return -EFAULT;
3112 return 0;
3113 case MTIOCPOS:
3114 mtpos.mt_blkno = position / tape->user_bs_factor - block_offset;
3115 if (copy_to_user(argp, &mtpos, sizeof(struct mtpos)))
3116 return -EFAULT;
3117 return 0;
3118 default:
3119 if (tape->chrdev_dir == IDETAPE_DIR_READ)
3120 idetape_discard_read_pipeline(drive, 1);
3121 return idetape_blkdev_ioctl(drive, cmd, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003122 }
3123}
3124
Borislav Petkov3cffb9c2008-02-02 19:56:50 +01003125/*
3126 * Do a mode sense page 0 with block descriptor and if it succeeds set the tape
3127 * block size with the reported value.
3128 */
3129static void ide_tape_get_bsize_from_bdesc(ide_drive_t *drive)
3130{
3131 idetape_tape_t *tape = drive->driver_data;
Borislav Petkovd236d742008-04-18 00:46:27 +02003132 struct ide_atapi_pc pc;
Borislav Petkov3cffb9c2008-02-02 19:56:50 +01003133
3134 idetape_create_mode_sense_cmd(&pc, IDETAPE_BLOCK_DESCRIPTOR);
3135 if (idetape_queue_pc_tail(drive, &pc)) {
3136 printk(KERN_ERR "ide-tape: Can't get block descriptor\n");
Borislav Petkov54bb2072008-02-06 02:57:52 +01003137 if (tape->blk_size == 0) {
Borislav Petkov3cffb9c2008-02-02 19:56:50 +01003138 printk(KERN_WARNING "ide-tape: Cannot deal with zero "
3139 "block size, assuming 32k\n");
Borislav Petkov54bb2072008-02-06 02:57:52 +01003140 tape->blk_size = 32768;
Borislav Petkov3cffb9c2008-02-02 19:56:50 +01003141 }
3142 return;
3143 }
Borislav Petkovd236d742008-04-18 00:46:27 +02003144 tape->blk_size = (pc.buf[4 + 5] << 16) +
3145 (pc.buf[4 + 6] << 8) +
3146 pc.buf[4 + 7];
3147 tape->drv_write_prot = (pc.buf[2] & 0x80) >> 7;
Borislav Petkov3cffb9c2008-02-02 19:56:50 +01003148}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003149
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01003150static int idetape_chrdev_open(struct inode *inode, struct file *filp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003151{
3152 unsigned int minor = iminor(inode), i = minor & ~0xc0;
3153 ide_drive_t *drive;
3154 idetape_tape_t *tape;
Borislav Petkovd236d742008-04-18 00:46:27 +02003155 struct ide_atapi_pc pc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003156 int retval;
3157
Borislav Petkov8004a8c2008-02-06 02:57:51 +01003158 if (i >= MAX_HWIFS * MAX_DRIVES)
3159 return -ENXIO;
3160
3161 tape = ide_tape_chrdev_get(i);
3162 if (!tape)
3163 return -ENXIO;
3164
3165 debug_log(DBG_CHRDEV, "Enter %s\n", __func__);
3166
Linus Torvalds1da177e2005-04-16 15:20:36 -07003167 /*
3168 * We really want to do nonseekable_open(inode, filp); here, but some
3169 * versions of tar incorrectly call lseek on tapes and bail out if that
3170 * fails. So we disallow pread() and pwrite(), but permit lseeks.
3171 */
3172 filp->f_mode &= ~(FMODE_PREAD | FMODE_PWRITE);
3173
Linus Torvalds1da177e2005-04-16 15:20:36 -07003174 drive = tape->drive;
3175
3176 filp->private_data = tape;
3177
Borislav Petkov346331f2008-04-18 00:46:26 +02003178 if (test_and_set_bit(IDETAPE_FLAG_BUSY, &tape->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003179 retval = -EBUSY;
3180 goto out_put_tape;
3181 }
3182
3183 retval = idetape_wait_ready(drive, 60 * HZ);
3184 if (retval) {
Borislav Petkov346331f2008-04-18 00:46:26 +02003185 clear_bit(IDETAPE_FLAG_BUSY, &tape->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003186 printk(KERN_ERR "ide-tape: %s: drive not ready\n", tape->name);
3187 goto out_put_tape;
3188 }
3189
3190 idetape_read_position(drive);
Borislav Petkov346331f2008-04-18 00:46:26 +02003191 if (!test_bit(IDETAPE_FLAG_ADDRESS_VALID, &tape->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003192 (void)idetape_rewind_tape(drive);
3193
Borislav Petkov54abf372008-02-06 02:57:52 +01003194 if (tape->chrdev_dir != IDETAPE_DIR_READ)
Borislav Petkov346331f2008-04-18 00:46:26 +02003195 clear_bit(IDETAPE_FLAG_PIPELINE_ERR, &tape->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003196
3197 /* Read block size and write protect status from drive. */
Borislav Petkov3cffb9c2008-02-02 19:56:50 +01003198 ide_tape_get_bsize_from_bdesc(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003199
3200 /* Set write protect flag if device is opened as read-only. */
3201 if ((filp->f_flags & O_ACCMODE) == O_RDONLY)
3202 tape->write_prot = 1;
3203 else
3204 tape->write_prot = tape->drv_write_prot;
3205
3206 /* Make sure drive isn't write protected if user wants to write. */
3207 if (tape->write_prot) {
3208 if ((filp->f_flags & O_ACCMODE) == O_WRONLY ||
3209 (filp->f_flags & O_ACCMODE) == O_RDWR) {
Borislav Petkov346331f2008-04-18 00:46:26 +02003210 clear_bit(IDETAPE_FLAG_BUSY, &tape->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003211 retval = -EROFS;
3212 goto out_put_tape;
3213 }
3214 }
3215
Borislav Petkov3c98bf32008-02-06 02:57:53 +01003216 /* Lock the tape drive door so user can't eject. */
Borislav Petkov54abf372008-02-06 02:57:52 +01003217 if (tape->chrdev_dir == IDETAPE_DIR_NONE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003218 if (idetape_create_prevent_cmd(drive, &pc, 1)) {
3219 if (!idetape_queue_pc_tail(drive, &pc)) {
3220 if (tape->door_locked != DOOR_EXPLICITLY_LOCKED)
3221 tape->door_locked = DOOR_LOCKED;
3222 }
3223 }
3224 }
3225 idetape_restart_speed_control(drive);
3226 tape->restart_speed_control_req = 0;
3227 return 0;
3228
3229out_put_tape:
3230 ide_tape_put(tape);
3231 return retval;
3232}
3233
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01003234static void idetape_write_release(ide_drive_t *drive, unsigned int minor)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003235{
3236 idetape_tape_t *tape = drive->driver_data;
3237
3238 idetape_empty_write_pipeline(drive);
3239 tape->merge_stage = __idetape_kmalloc_stage(tape, 1, 0);
3240 if (tape->merge_stage != NULL) {
Borislav Petkov54bb2072008-02-06 02:57:52 +01003241 idetape_pad_zeros(drive, tape->blk_size *
3242 (tape->user_bs_factor - 1));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003243 __idetape_kfree_stage(tape->merge_stage);
3244 tape->merge_stage = NULL;
3245 }
3246 idetape_write_filemark(drive);
3247 idetape_flush_tape_buffers(drive);
3248 idetape_flush_tape_buffers(drive);
3249}
3250
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01003251static int idetape_chrdev_release(struct inode *inode, struct file *filp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003252{
3253 struct ide_tape_obj *tape = ide_tape_f(filp);
3254 ide_drive_t *drive = tape->drive;
Borislav Petkovd236d742008-04-18 00:46:27 +02003255 struct ide_atapi_pc pc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003256 unsigned int minor = iminor(inode);
3257
3258 lock_kernel();
3259 tape = drive->driver_data;
Borislav Petkov8004a8c2008-02-06 02:57:51 +01003260
3261 debug_log(DBG_CHRDEV, "Enter %s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003262
Borislav Petkov54abf372008-02-06 02:57:52 +01003263 if (tape->chrdev_dir == IDETAPE_DIR_WRITE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003264 idetape_write_release(drive, minor);
Borislav Petkov54abf372008-02-06 02:57:52 +01003265 if (tape->chrdev_dir == IDETAPE_DIR_READ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003266 if (minor < 128)
3267 idetape_discard_read_pipeline(drive, 1);
3268 else
3269 idetape_wait_for_pipeline(drive);
3270 }
3271 if (tape->cache_stage != NULL) {
3272 __idetape_kfree_stage(tape->cache_stage);
3273 tape->cache_stage = NULL;
3274 }
Borislav Petkov346331f2008-04-18 00:46:26 +02003275 if (minor < 128 && test_bit(IDETAPE_FLAG_MEDIUM_PRESENT, &tape->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003276 (void) idetape_rewind_tape(drive);
Borislav Petkov54abf372008-02-06 02:57:52 +01003277 if (tape->chrdev_dir == IDETAPE_DIR_NONE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003278 if (tape->door_locked == DOOR_LOCKED) {
3279 if (idetape_create_prevent_cmd(drive, &pc, 0)) {
3280 if (!idetape_queue_pc_tail(drive, &pc))
3281 tape->door_locked = DOOR_UNLOCKED;
3282 }
3283 }
3284 }
Borislav Petkov346331f2008-04-18 00:46:26 +02003285 clear_bit(IDETAPE_FLAG_BUSY, &tape->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003286 ide_tape_put(tape);
3287 unlock_kernel();
3288 return 0;
3289}
3290
3291/*
Borislav Petkov71071b82008-02-06 02:57:53 +01003292 * check the contents of the ATAPI IDENTIFY command results. We return:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003293 *
Borislav Petkov71071b82008-02-06 02:57:53 +01003294 * 1 - If the tape can be supported by us, based on the information we have so
3295 * far.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003296 *
Borislav Petkov71071b82008-02-06 02:57:53 +01003297 * 0 - If this tape driver is not currently supported by us.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003298 */
Borislav Petkov71071b82008-02-06 02:57:53 +01003299static int idetape_identify_device(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003300{
Borislav Petkov71071b82008-02-06 02:57:53 +01003301 u8 gcw[2], protocol, device_type, removable, packet_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003302
3303 if (drive->id_read == 0)
3304 return 1;
3305
Borislav Petkov71071b82008-02-06 02:57:53 +01003306 *((unsigned short *) &gcw) = drive->id->config;
3307
3308 protocol = (gcw[1] & 0xC0) >> 6;
3309 device_type = gcw[1] & 0x1F;
3310 removable = !!(gcw[0] & 0x80);
3311 packet_size = gcw[0] & 0x3;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003312
Linus Torvalds1da177e2005-04-16 15:20:36 -07003313 /* Check that we can support this device */
Borislav Petkov71071b82008-02-06 02:57:53 +01003314 if (protocol != 2)
Bartlomiej Zolnierkiewicz16422de32008-02-02 19:56:48 +01003315 printk(KERN_ERR "ide-tape: Protocol (0x%02x) is not ATAPI\n",
Borislav Petkov71071b82008-02-06 02:57:53 +01003316 protocol);
3317 else if (device_type != 1)
Bartlomiej Zolnierkiewicz16422de32008-02-02 19:56:48 +01003318 printk(KERN_ERR "ide-tape: Device type (0x%02x) is not set "
Borislav Petkov71071b82008-02-06 02:57:53 +01003319 "to tape\n", device_type);
3320 else if (!removable)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003321 printk(KERN_ERR "ide-tape: The removable flag is not set\n");
Borislav Petkov71071b82008-02-06 02:57:53 +01003322 else if (packet_size != 0) {
Borislav Petkov24d57f82008-02-06 02:57:54 +01003323 printk(KERN_ERR "ide-tape: Packet size (0x%02x) is not 12"
3324 " bytes\n", packet_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003325 } else
3326 return 1;
3327 return 0;
3328}
3329
Borislav Petkov6d29c8f2008-02-02 19:56:49 +01003330static void idetape_get_inquiry_results(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003331{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003332 idetape_tape_t *tape = drive->driver_data;
Borislav Petkovd236d742008-04-18 00:46:27 +02003333 struct ide_atapi_pc pc;
Borislav Petkov41f81d542008-02-06 02:57:52 +01003334 char fw_rev[6], vendor_id[10], product_id[18];
Borislav Petkov6d29c8f2008-02-02 19:56:49 +01003335
Linus Torvalds1da177e2005-04-16 15:20:36 -07003336 idetape_create_inquiry_cmd(&pc);
3337 if (idetape_queue_pc_tail(drive, &pc)) {
Borislav Petkov6d29c8f2008-02-02 19:56:49 +01003338 printk(KERN_ERR "ide-tape: %s: can't get INQUIRY results\n",
3339 tape->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003340 return;
3341 }
Borislav Petkovd236d742008-04-18 00:46:27 +02003342 memcpy(vendor_id, &pc.buf[8], 8);
3343 memcpy(product_id, &pc.buf[16], 16);
3344 memcpy(fw_rev, &pc.buf[32], 4);
Borislav Petkov6d29c8f2008-02-02 19:56:49 +01003345
Borislav Petkov41f81d542008-02-06 02:57:52 +01003346 ide_fixstring(vendor_id, 10, 0);
3347 ide_fixstring(product_id, 18, 0);
3348 ide_fixstring(fw_rev, 6, 0);
3349
Borislav Petkov6d29c8f2008-02-02 19:56:49 +01003350 printk(KERN_INFO "ide-tape: %s <-> %s: %s %s rev %s\n",
Borislav Petkov41f81d542008-02-06 02:57:52 +01003351 drive->name, tape->name, vendor_id, product_id, fw_rev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003352}
3353
3354/*
Borislav Petkovb6422012008-02-02 19:56:49 +01003355 * Ask the tape about its various parameters. In particular, we will adjust our
3356 * data transfer buffer size to the recommended value as returned by the tape.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003357 */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01003358static void idetape_get_mode_sense_results(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003359{
3360 idetape_tape_t *tape = drive->driver_data;
Borislav Petkovd236d742008-04-18 00:46:27 +02003361 struct ide_atapi_pc pc;
Borislav Petkovb6422012008-02-02 19:56:49 +01003362 u8 *caps;
3363 u8 speed, max_speed;
Borislav Petkov47314fa2008-02-02 19:56:48 +01003364
Linus Torvalds1da177e2005-04-16 15:20:36 -07003365 idetape_create_mode_sense_cmd(&pc, IDETAPE_CAPABILITIES_PAGE);
3366 if (idetape_queue_pc_tail(drive, &pc)) {
Borislav Petkovb6422012008-02-02 19:56:49 +01003367 printk(KERN_ERR "ide-tape: Can't get tape parameters - assuming"
3368 " some default values\n");
Borislav Petkov54bb2072008-02-06 02:57:52 +01003369 tape->blk_size = 512;
Borislav Petkovb6422012008-02-02 19:56:49 +01003370 put_unaligned(52, (u16 *)&tape->caps[12]);
3371 put_unaligned(540, (u16 *)&tape->caps[14]);
3372 put_unaligned(6*52, (u16 *)&tape->caps[16]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003373 return;
3374 }
Borislav Petkovd236d742008-04-18 00:46:27 +02003375 caps = pc.buf + 4 + pc.buf[3];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003376
Borislav Petkovb6422012008-02-02 19:56:49 +01003377 /* convert to host order and save for later use */
3378 speed = be16_to_cpu(*(u16 *)&caps[14]);
3379 max_speed = be16_to_cpu(*(u16 *)&caps[8]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003380
Borislav Petkovb6422012008-02-02 19:56:49 +01003381 put_unaligned(max_speed, (u16 *)&caps[8]);
3382 put_unaligned(be16_to_cpu(*(u16 *)&caps[12]), (u16 *)&caps[12]);
3383 put_unaligned(speed, (u16 *)&caps[14]);
3384 put_unaligned(be16_to_cpu(*(u16 *)&caps[16]), (u16 *)&caps[16]);
3385
3386 if (!speed) {
3387 printk(KERN_INFO "ide-tape: %s: invalid tape speed "
3388 "(assuming 650KB/sec)\n", drive->name);
3389 put_unaligned(650, (u16 *)&caps[14]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003390 }
Borislav Petkovb6422012008-02-02 19:56:49 +01003391 if (!max_speed) {
3392 printk(KERN_INFO "ide-tape: %s: invalid max_speed "
3393 "(assuming 650KB/sec)\n", drive->name);
3394 put_unaligned(650, (u16 *)&caps[8]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003395 }
3396
Borislav Petkovb6422012008-02-02 19:56:49 +01003397 memcpy(&tape->caps, caps, 20);
3398 if (caps[7] & 0x02)
Borislav Petkov54bb2072008-02-06 02:57:52 +01003399 tape->blk_size = 512;
Borislav Petkovb6422012008-02-02 19:56:49 +01003400 else if (caps[7] & 0x04)
Borislav Petkov54bb2072008-02-06 02:57:52 +01003401 tape->blk_size = 1024;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003402}
3403
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +02003404#ifdef CONFIG_IDE_PROC_FS
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01003405static void idetape_add_settings(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003406{
3407 idetape_tape_t *tape = drive->driver_data;
3408
Borislav Petkovb6422012008-02-02 19:56:49 +01003409 ide_add_setting(drive, "buffer", SETTING_READ, TYPE_SHORT, 0, 0xffff,
3410 1, 2, (u16 *)&tape->caps[16], NULL);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01003411 ide_add_setting(drive, "pipeline_min", SETTING_RW, TYPE_INT, 1, 0xffff,
3412 tape->stage_size / 1024, 1, &tape->min_pipeline, NULL);
3413 ide_add_setting(drive, "pipeline", SETTING_RW, TYPE_INT, 1, 0xffff,
3414 tape->stage_size / 1024, 1, &tape->max_stages, NULL);
3415 ide_add_setting(drive, "pipeline_max", SETTING_RW, TYPE_INT, 1, 0xffff,
3416 tape->stage_size / 1024, 1, &tape->max_pipeline, NULL);
3417 ide_add_setting(drive, "pipeline_used", SETTING_READ, TYPE_INT, 0,
3418 0xffff, tape->stage_size / 1024, 1, &tape->nr_stages,
3419 NULL);
3420 ide_add_setting(drive, "pipeline_pending", SETTING_READ, TYPE_INT, 0,
3421 0xffff, tape->stage_size / 1024, 1,
3422 &tape->nr_pending_stages, NULL);
Borislav Petkovb6422012008-02-02 19:56:49 +01003423 ide_add_setting(drive, "speed", SETTING_READ, TYPE_SHORT, 0, 0xffff,
3424 1, 1, (u16 *)&tape->caps[14], NULL);
Borislav Petkov54bb2072008-02-06 02:57:52 +01003425 ide_add_setting(drive, "stage", SETTING_READ, TYPE_INT, 0, 0xffff, 1,
3426 1024, &tape->stage_size, NULL);
3427 ide_add_setting(drive, "tdsc", SETTING_RW, TYPE_INT, IDETAPE_DSC_RW_MIN,
3428 IDETAPE_DSC_RW_MAX, 1000, HZ, &tape->best_dsc_rw_freq,
3429 NULL);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01003430 ide_add_setting(drive, "dsc_overlap", SETTING_RW, TYPE_BYTE, 0, 1, 1,
3431 1, &drive->dsc_overlap, NULL);
3432 ide_add_setting(drive, "pipeline_head_speed_c", SETTING_READ, TYPE_INT,
3433 0, 0xffff, 1, 1, &tape->controlled_pipeline_head_speed,
3434 NULL);
3435 ide_add_setting(drive, "pipeline_head_speed_u", SETTING_READ, TYPE_INT,
3436 0, 0xffff, 1, 1,
3437 &tape->uncontrolled_pipeline_head_speed, NULL);
3438 ide_add_setting(drive, "avg_speed", SETTING_READ, TYPE_INT, 0, 0xffff,
3439 1, 1, &tape->avg_speed, NULL);
Borislav Petkov8004a8c2008-02-06 02:57:51 +01003440 ide_add_setting(drive, "debug_mask", SETTING_RW, TYPE_INT, 0, 0xffff, 1,
3441 1, &tape->debug_mask, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003442}
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +02003443#else
3444static inline void idetape_add_settings(ide_drive_t *drive) { ; }
3445#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003446
3447/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +01003448 * The function below is called to:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003449 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +01003450 * 1. Initialize our various state variables.
3451 * 2. Ask the tape for its capabilities.
3452 * 3. Allocate a buffer which will be used for data transfer. The buffer size
3453 * is chosen based on the recommendation which we received in step 2.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003454 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +01003455 * Note that at this point ide.c already assigned us an irq, so that we can
3456 * queue requests here and wait for their completion.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003457 */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01003458static void idetape_setup(ide_drive_t *drive, idetape_tape_t *tape, int minor)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003459{
3460 unsigned long t1, tmid, tn, t;
3461 int speed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003462 int stage_size;
Borislav Petkov71071b82008-02-06 02:57:53 +01003463 u8 gcw[2];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003464 struct sysinfo si;
Borislav Petkovb6422012008-02-02 19:56:49 +01003465 u16 *ctl = (u16 *)&tape->caps[12];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003466
Borislav Petkov54bb2072008-02-06 02:57:52 +01003467 spin_lock_init(&tape->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003468 drive->dsc_overlap = 1;
Bartlomiej Zolnierkiewicz4166c192008-02-01 23:09:30 +01003469 if (drive->hwif->host_flags & IDE_HFLAG_NO_DSC) {
3470 printk(KERN_INFO "ide-tape: %s: disabling DSC overlap\n",
3471 tape->name);
3472 drive->dsc_overlap = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003473 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003474 /* Seagate Travan drives do not support DSC overlap. */
3475 if (strstr(drive->id->model, "Seagate STT3401"))
3476 drive->dsc_overlap = 0;
3477 tape->minor = minor;
3478 tape->name[0] = 'h';
3479 tape->name[1] = 't';
3480 tape->name[2] = '0' + minor;
Borislav Petkov54abf372008-02-06 02:57:52 +01003481 tape->chrdev_dir = IDETAPE_DIR_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003482 tape->pc = tape->pc_stack;
3483 tape->max_insert_speed = 10000;
3484 tape->speed_control = 1;
3485 *((unsigned short *) &gcw) = drive->id->config;
Borislav Petkov71071b82008-02-06 02:57:53 +01003486
3487 /* Command packet DRQ type */
3488 if (((gcw[0] & 0x60) >> 5) == 1)
Borislav Petkov346331f2008-04-18 00:46:26 +02003489 set_bit(IDETAPE_FLAG_DRQ_INTERRUPT, &tape->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003490
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01003491 tape->min_pipeline = 10;
3492 tape->max_pipeline = 10;
3493 tape->max_stages = 10;
Borislav Petkov3c98bf32008-02-06 02:57:53 +01003494
Linus Torvalds1da177e2005-04-16 15:20:36 -07003495 idetape_get_inquiry_results(drive);
3496 idetape_get_mode_sense_results(drive);
Borislav Petkov3cffb9c2008-02-02 19:56:50 +01003497 ide_tape_get_bsize_from_bdesc(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003498 tape->user_bs_factor = 1;
Borislav Petkov54bb2072008-02-06 02:57:52 +01003499 tape->stage_size = *ctl * tape->blk_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003500 while (tape->stage_size > 0xffff) {
3501 printk(KERN_NOTICE "ide-tape: decreasing stage size\n");
Borislav Petkovb6422012008-02-02 19:56:49 +01003502 *ctl /= 2;
Borislav Petkov54bb2072008-02-06 02:57:52 +01003503 tape->stage_size = *ctl * tape->blk_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003504 }
3505 stage_size = tape->stage_size;
3506 tape->pages_per_stage = stage_size / PAGE_SIZE;
3507 if (stage_size % PAGE_SIZE) {
3508 tape->pages_per_stage++;
3509 tape->excess_bh_size = PAGE_SIZE - stage_size % PAGE_SIZE;
3510 }
3511
Borislav Petkovb6422012008-02-02 19:56:49 +01003512 /* Select the "best" DSC read/write polling freq and pipeline size. */
3513 speed = max(*(u16 *)&tape->caps[14], *(u16 *)&tape->caps[8]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003514
3515 tape->max_stages = speed * 1000 * 10 / tape->stage_size;
3516
Borislav Petkov3c98bf32008-02-06 02:57:53 +01003517 /* Limit memory use for pipeline to 10% of physical memory */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003518 si_meminfo(&si);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01003519 if (tape->max_stages * tape->stage_size >
3520 si.totalram * si.mem_unit / 10)
3521 tape->max_stages =
3522 si.totalram * si.mem_unit / (10 * tape->stage_size);
3523
Linus Torvalds1da177e2005-04-16 15:20:36 -07003524 tape->max_stages = min(tape->max_stages, IDETAPE_MAX_PIPELINE_STAGES);
3525 tape->min_pipeline = min(tape->max_stages, IDETAPE_MIN_PIPELINE_STAGES);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01003526 tape->max_pipeline =
3527 min(tape->max_stages * 2, IDETAPE_MAX_PIPELINE_STAGES);
3528 if (tape->max_stages == 0) {
3529 tape->max_stages = 1;
3530 tape->min_pipeline = 1;
3531 tape->max_pipeline = 1;
3532 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003533
3534 t1 = (tape->stage_size * HZ) / (speed * 1000);
Borislav Petkovb6422012008-02-02 19:56:49 +01003535 tmid = (*(u16 *)&tape->caps[16] * 32 * HZ) / (speed * 125);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003536 tn = (IDETAPE_FIFO_THRESHOLD * tape->stage_size * HZ) / (speed * 1000);
3537
3538 if (tape->max_stages)
3539 t = tn;
3540 else
3541 t = t1;
3542
3543 /*
Borislav Petkov3c98bf32008-02-06 02:57:53 +01003544 * Ensure that the number we got makes sense; limit it within
3545 * IDETAPE_DSC_RW_MIN and IDETAPE_DSC_RW_MAX.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003546 */
Borislav Petkov54bb2072008-02-06 02:57:52 +01003547 tape->best_dsc_rw_freq = max_t(unsigned long,
3548 min_t(unsigned long, t, IDETAPE_DSC_RW_MAX),
3549 IDETAPE_DSC_RW_MIN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003550 printk(KERN_INFO "ide-tape: %s <-> %s: %dKBps, %d*%dkB buffer, "
3551 "%dkB pipeline, %lums tDSC%s\n",
Borislav Petkovb6422012008-02-02 19:56:49 +01003552 drive->name, tape->name, *(u16 *)&tape->caps[14],
3553 (*(u16 *)&tape->caps[16] * 512) / tape->stage_size,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003554 tape->stage_size / 1024,
3555 tape->max_stages * tape->stage_size / 1024,
Borislav Petkov54bb2072008-02-06 02:57:52 +01003556 tape->best_dsc_rw_freq * 1000 / HZ,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003557 drive->using_dma ? ", DMA":"");
3558
3559 idetape_add_settings(drive);
3560}
3561
Russell King4031bbe2006-01-06 11:41:00 +00003562static void ide_tape_remove(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003563{
3564 idetape_tape_t *tape = drive->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003565
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +02003566 ide_proc_unregister_driver(drive, tape->driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003567
3568 ide_unregister_region(tape->disk);
3569
3570 ide_tape_put(tape);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003571}
3572
3573static void ide_tape_release(struct kref *kref)
3574{
3575 struct ide_tape_obj *tape = to_ide_tape(kref);
3576 ide_drive_t *drive = tape->drive;
3577 struct gendisk *g = tape->disk;
3578
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02003579 BUG_ON(tape->first_stage != NULL || tape->merge_stage_size);
3580
Linus Torvalds1da177e2005-04-16 15:20:36 -07003581 drive->dsc_overlap = 0;
3582 drive->driver_data = NULL;
Tony Jonesdbc12722007-09-25 02:03:03 +02003583 device_destroy(idetape_sysfs_class, MKDEV(IDETAPE_MAJOR, tape->minor));
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01003584 device_destroy(idetape_sysfs_class,
3585 MKDEV(IDETAPE_MAJOR, tape->minor + 128));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003586 idetape_devs[tape->minor] = NULL;
3587 g->private_data = NULL;
3588 put_disk(g);
3589 kfree(tape);
3590}
3591
Bartlomiej Zolnierkiewiczecfd80e2007-05-10 00:01:09 +02003592#ifdef CONFIG_IDE_PROC_FS
Linus Torvalds1da177e2005-04-16 15:20:36 -07003593static int proc_idetape_read_name
3594 (char *page, char **start, off_t off, int count, int *eof, void *data)
3595{
3596 ide_drive_t *drive = (ide_drive_t *) data;
3597 idetape_tape_t *tape = drive->driver_data;
3598 char *out = page;
3599 int len;
3600
3601 len = sprintf(out, "%s\n", tape->name);
3602 PROC_IDE_READ_RETURN(page, start, off, count, eof, len);
3603}
3604
3605static ide_proc_entry_t idetape_proc[] = {
3606 { "capacity", S_IFREG|S_IRUGO, proc_ide_read_capacity, NULL },
3607 { "name", S_IFREG|S_IRUGO, proc_idetape_read_name, NULL },
3608 { NULL, 0, NULL, NULL }
3609};
Linus Torvalds1da177e2005-04-16 15:20:36 -07003610#endif
3611
Russell King4031bbe2006-01-06 11:41:00 +00003612static int ide_tape_probe(ide_drive_t *);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003613
Linus Torvalds1da177e2005-04-16 15:20:36 -07003614static ide_driver_t idetape_driver = {
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02003615 .gen_driver = {
Laurent Riffard4ef3b8f2005-11-18 22:15:40 +01003616 .owner = THIS_MODULE,
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02003617 .name = "ide-tape",
3618 .bus = &ide_bus_type,
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02003619 },
Russell King4031bbe2006-01-06 11:41:00 +00003620 .probe = ide_tape_probe,
3621 .remove = ide_tape_remove,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003622 .version = IDETAPE_VERSION,
3623 .media = ide_tape,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003624 .supports_dsc_overlap = 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003625 .do_request = idetape_do_request,
3626 .end_request = idetape_end_request,
3627 .error = __ide_error,
3628 .abort = __ide_abort,
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +02003629#ifdef CONFIG_IDE_PROC_FS
Linus Torvalds1da177e2005-04-16 15:20:36 -07003630 .proc = idetape_proc,
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +02003631#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003632};
3633
Borislav Petkov3c98bf32008-02-06 02:57:53 +01003634/* Our character device supporting functions, passed to register_chrdev. */
Arjan van de Ven2b8693c2007-02-12 00:55:32 -08003635static const struct file_operations idetape_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003636 .owner = THIS_MODULE,
3637 .read = idetape_chrdev_read,
3638 .write = idetape_chrdev_write,
3639 .ioctl = idetape_chrdev_ioctl,
3640 .open = idetape_chrdev_open,
3641 .release = idetape_chrdev_release,
3642};
3643
3644static int idetape_open(struct inode *inode, struct file *filp)
3645{
3646 struct gendisk *disk = inode->i_bdev->bd_disk;
3647 struct ide_tape_obj *tape;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003648
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01003649 tape = ide_tape_get(disk);
3650 if (!tape)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003651 return -ENXIO;
3652
Linus Torvalds1da177e2005-04-16 15:20:36 -07003653 return 0;
3654}
3655
3656static int idetape_release(struct inode *inode, struct file *filp)
3657{
3658 struct gendisk *disk = inode->i_bdev->bd_disk;
3659 struct ide_tape_obj *tape = ide_tape_g(disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003660
3661 ide_tape_put(tape);
3662
3663 return 0;
3664}
3665
3666static int idetape_ioctl(struct inode *inode, struct file *file,
3667 unsigned int cmd, unsigned long arg)
3668{
3669 struct block_device *bdev = inode->i_bdev;
3670 struct ide_tape_obj *tape = ide_tape_g(bdev->bd_disk);
3671 ide_drive_t *drive = tape->drive;
3672 int err = generic_ide_ioctl(drive, file, bdev, cmd, arg);
3673 if (err == -EINVAL)
3674 err = idetape_blkdev_ioctl(drive, cmd, arg);
3675 return err;
3676}
3677
3678static struct block_device_operations idetape_block_ops = {
3679 .owner = THIS_MODULE,
3680 .open = idetape_open,
3681 .release = idetape_release,
3682 .ioctl = idetape_ioctl,
3683};
3684
Russell King4031bbe2006-01-06 11:41:00 +00003685static int ide_tape_probe(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003686{
3687 idetape_tape_t *tape;
3688 struct gendisk *g;
3689 int minor;
3690
3691 if (!strstr("ide-tape", drive->driver_req))
3692 goto failed;
3693 if (!drive->present)
3694 goto failed;
3695 if (drive->media != ide_tape)
3696 goto failed;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01003697 if (!idetape_identify_device(drive)) {
3698 printk(KERN_ERR "ide-tape: %s: not supported by this version of"
3699 " the driver\n", drive->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003700 goto failed;
3701 }
3702 if (drive->scsi) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01003703 printk(KERN_INFO "ide-tape: passing drive %s to ide-scsi"
3704 " emulation.\n", drive->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003705 goto failed;
3706 }
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01003707 tape = kzalloc(sizeof(idetape_tape_t), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003708 if (tape == NULL) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01003709 printk(KERN_ERR "ide-tape: %s: Can't allocate a tape struct\n",
3710 drive->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003711 goto failed;
3712 }
3713
3714 g = alloc_disk(1 << PARTN_BITS);
3715 if (!g)
3716 goto out_free_tape;
3717
3718 ide_init_disk(g, drive);
3719
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +02003720 ide_proc_register_driver(drive, &idetape_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003721
Linus Torvalds1da177e2005-04-16 15:20:36 -07003722 kref_init(&tape->kref);
3723
3724 tape->drive = drive;
3725 tape->driver = &idetape_driver;
3726 tape->disk = g;
3727
3728 g->private_data = &tape->driver;
3729
3730 drive->driver_data = tape;
3731
Arjan van de Vencf8b8972006-03-23 03:00:45 -08003732 mutex_lock(&idetape_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003733 for (minor = 0; idetape_devs[minor]; minor++)
3734 ;
3735 idetape_devs[minor] = tape;
Arjan van de Vencf8b8972006-03-23 03:00:45 -08003736 mutex_unlock(&idetape_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003737
3738 idetape_setup(drive, tape, minor);
3739
Tony Jonesdbc12722007-09-25 02:03:03 +02003740 device_create(idetape_sysfs_class, &drive->gendev,
3741 MKDEV(IDETAPE_MAJOR, minor), "%s", tape->name);
3742 device_create(idetape_sysfs_class, &drive->gendev,
3743 MKDEV(IDETAPE_MAJOR, minor + 128), "n%s", tape->name);
Will Dysond5dee802005-09-16 02:55:07 -07003744
Linus Torvalds1da177e2005-04-16 15:20:36 -07003745 g->fops = &idetape_block_ops;
3746 ide_register_region(g);
3747
3748 return 0;
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02003749
Linus Torvalds1da177e2005-04-16 15:20:36 -07003750out_free_tape:
3751 kfree(tape);
3752failed:
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02003753 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003754}
3755
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01003756static void __exit idetape_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003757{
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02003758 driver_unregister(&idetape_driver.gen_driver);
Will Dysond5dee802005-09-16 02:55:07 -07003759 class_destroy(idetape_sysfs_class);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003760 unregister_chrdev(IDETAPE_MAJOR, "ht");
3761}
3762
Bartlomiej Zolnierkiewicz17514e82005-11-19 22:24:35 +01003763static int __init idetape_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003764{
Will Dysond5dee802005-09-16 02:55:07 -07003765 int error = 1;
3766 idetape_sysfs_class = class_create(THIS_MODULE, "ide_tape");
3767 if (IS_ERR(idetape_sysfs_class)) {
3768 idetape_sysfs_class = NULL;
3769 printk(KERN_ERR "Unable to create sysfs class for ide tapes\n");
3770 error = -EBUSY;
3771 goto out;
3772 }
3773
Linus Torvalds1da177e2005-04-16 15:20:36 -07003774 if (register_chrdev(IDETAPE_MAJOR, "ht", &idetape_fops)) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01003775 printk(KERN_ERR "ide-tape: Failed to register chrdev"
3776 " interface\n");
Will Dysond5dee802005-09-16 02:55:07 -07003777 error = -EBUSY;
3778 goto out_free_class;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003779 }
Will Dysond5dee802005-09-16 02:55:07 -07003780
3781 error = driver_register(&idetape_driver.gen_driver);
3782 if (error)
3783 goto out_free_driver;
3784
3785 return 0;
3786
3787out_free_driver:
3788 driver_unregister(&idetape_driver.gen_driver);
3789out_free_class:
3790 class_destroy(idetape_sysfs_class);
3791out:
3792 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003793}
3794
Kay Sievers263756e2005-12-12 18:03:44 +01003795MODULE_ALIAS("ide:*m-tape*");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003796module_init(idetape_init);
3797module_exit(idetape_exit);
3798MODULE_ALIAS_CHARDEV_MAJOR(IDETAPE_MAJOR);
Borislav Petkov9c145762008-02-06 02:57:54 +01003799MODULE_DESCRIPTION("ATAPI Streaming TAPE Driver");
3800MODULE_LICENSE("GPL");