blob: aed25590d0580842b4c9651f36a90708492bd71e [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
18#define IDETAPE_VERSION "1.19"
19
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>
42#include <asm/irq.h>
43#include <asm/uaccess.h>
44#include <asm/io.h>
45#include <asm/unaligned.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070046#include <linux/mtio.h>
47
Borislav Petkov8004a8c2008-02-06 02:57:51 +010048enum {
49 /* output errors only */
50 DBG_ERR = (1 << 0),
51 /* output all sense key/asc */
52 DBG_SENSE = (1 << 1),
53 /* info regarding all chrdev-related procedures */
54 DBG_CHRDEV = (1 << 2),
55 /* all remaining procedures */
56 DBG_PROCS = (1 << 3),
57 /* buffer alloc info (pc_stack & rq_stack) */
58 DBG_PCRQ_STACK = (1 << 4),
59};
60
61/* define to see debug info */
62#define IDETAPE_DEBUG_LOG 0
63
64#if IDETAPE_DEBUG_LOG
65#define debug_log(lvl, fmt, args...) \
66{ \
67 if (tape->debug_mask & lvl) \
68 printk(KERN_INFO "ide-tape: " fmt, ## args); \
69}
70#else
71#define debug_log(lvl, fmt, args...) do {} while (0)
72#endif
73
Linus Torvalds1da177e2005-04-16 15:20:36 -070074/**************************** Tunable parameters *****************************/
75
76
77/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +010078 * Pipelined mode parameters.
Linus Torvalds1da177e2005-04-16 15:20:36 -070079 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +010080 * We try to use the minimum number of stages which is enough to keep the tape
81 * constantly streaming. To accomplish that, we implement a feedback loop around
82 * the maximum number of stages:
Linus Torvalds1da177e2005-04-16 15:20:36 -070083 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +010084 * We start from MIN maximum stages (we will not even use MIN stages if we don't
85 * need them), increment it by RATE*(MAX-MIN) whenever we sense that the
86 * pipeline is empty, until we reach the optimum value or until we reach MAX.
Linus Torvalds1da177e2005-04-16 15:20:36 -070087 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +010088 * Setting the following parameter to 0 is illegal: the pipelined mode cannot be
89 * disabled (idetape_calculate_speeds() divides by tape->max_stages.)
Linus Torvalds1da177e2005-04-16 15:20:36 -070090 */
91#define IDETAPE_MIN_PIPELINE_STAGES 1
92#define IDETAPE_MAX_PIPELINE_STAGES 400
93#define IDETAPE_INCREASE_STAGES_RATE 20
94
95/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +010096 * After each failed packet command we issue a request sense command and retry
97 * the packet command IDETAPE_MAX_PC_RETRIES times.
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +010099 * Setting IDETAPE_MAX_PC_RETRIES to 0 will disable retries.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100 */
101#define IDETAPE_MAX_PC_RETRIES 3
102
103/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100104 * With each packet command, we allocate a buffer of IDETAPE_PC_BUFFER_SIZE
105 * bytes. This is used for several packet commands (Not for READ/WRITE commands)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 */
107#define IDETAPE_PC_BUFFER_SIZE 256
108
109/*
110 * In various places in the driver, we need to allocate storage
111 * for packet commands and requests, which will remain valid while
112 * we leave the driver to wait for an interrupt or a timeout event.
113 */
114#define IDETAPE_PC_STACK (10 + IDETAPE_MAX_PC_RETRIES)
115
116/*
117 * Some drives (for example, Seagate STT3401A Travan) require a very long
118 * timeout, because they don't return an interrupt or clear their busy bit
119 * until after the command completes (even retension commands).
120 */
121#define IDETAPE_WAIT_CMD (900*HZ)
122
123/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100124 * The following parameter is used to select the point in the internal tape fifo
125 * in which we will start to refill the buffer. Decreasing the following
126 * parameter will improve the system's latency and interactive response, while
127 * using a high value might improve system throughput.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 */
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100129#define IDETAPE_FIFO_THRESHOLD 2
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130
131/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100132 * DSC polling parameters.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100134 * Polling for DSC (a single bit in the status register) is a very important
135 * function in ide-tape. There are two cases in which we poll for DSC:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100137 * 1. Before a read/write packet command, to ensure that we can transfer data
138 * from/to the tape's data buffers, without causing an actual media access.
139 * In case the tape is not ready yet, we take out our request from the device
140 * request queue, so that ide.c could service requests from the other device
141 * on the same interface in the meantime.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100143 * 2. After the successful initialization of a "media access packet command",
144 * which is a command that can take a long time to complete (the interval can
145 * range from several seconds to even an hour). Again, we postpone our request
146 * in the middle to free the bus for the other device. The polling frequency
147 * here should be lower than the read/write frequency since those media access
148 * commands are slow. We start from a "fast" frequency - IDETAPE_DSC_MA_FAST
149 * (1 second), and if we don't receive DSC after IDETAPE_DSC_MA_THRESHOLD
150 * (5 min), we switch it to a lower frequency - IDETAPE_DSC_MA_SLOW (1 min).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100152 * We also set a timeout for the timer, in case something goes wrong. The
153 * timeout should be longer then the maximum execution time of a tape operation.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 */
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100155
156/* DSC timings. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157#define IDETAPE_DSC_RW_MIN 5*HZ/100 /* 50 msec */
158#define IDETAPE_DSC_RW_MAX 40*HZ/100 /* 400 msec */
159#define IDETAPE_DSC_RW_TIMEOUT 2*60*HZ /* 2 minutes */
160#define IDETAPE_DSC_MA_FAST 2*HZ /* 2 seconds */
161#define IDETAPE_DSC_MA_THRESHOLD 5*60*HZ /* 5 minutes */
162#define IDETAPE_DSC_MA_SLOW 30*HZ /* 30 seconds */
163#define IDETAPE_DSC_MA_TIMEOUT 2*60*60*HZ /* 2 hours */
164
165/*************************** End of tunable parameters ***********************/
166
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100167/* Read/Write error simulation */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168#define SIMULATE_ERRORS 0
169
Borislav Petkov54abf372008-02-06 02:57:52 +0100170/* tape directions */
171enum {
172 IDETAPE_DIR_NONE = (1 << 0),
173 IDETAPE_DIR_READ = (1 << 1),
174 IDETAPE_DIR_WRITE = (1 << 2),
175};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176
177struct idetape_bh {
Stephen Rothwellab057962007-08-01 23:46:44 +0200178 u32 b_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 atomic_t b_count;
180 struct idetape_bh *b_reqnext;
181 char *b_data;
182};
183
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184typedef struct idetape_packet_command_s {
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100185 /* Actual packet bytes */
186 u8 c[12];
187 /* On each retry, we increment retries */
188 int retries;
189 /* Error code */
190 int error;
191 /* Bytes to transfer */
192 int request_transfer;
193 /* Bytes actually transferred */
194 int actually_transferred;
195 /* Size of our data buffer */
196 int buffer_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 struct idetape_bh *bh;
198 char *b_data;
199 int b_count;
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100200 /* Data buffer */
201 u8 *buffer;
202 /* Pointer into the above buffer */
203 u8 *current_position;
204 /* Called when this packet command is completed */
205 ide_startstop_t (*callback) (ide_drive_t *);
206 /* Temporary buffer */
207 u8 pc_buffer[IDETAPE_PC_BUFFER_SIZE];
208 /* Status/Action bit flags: long for set_bit */
209 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210} idetape_pc_t;
211
212/*
213 * Packet command flag bits.
214 */
215/* Set when an error is considered normal - We won't retry */
216#define PC_ABORT 0
217/* 1 When polling for DSC on a media access command */
218#define PC_WAIT_FOR_DSC 1
219/* 1 when we prefer to use DMA if possible */
220#define PC_DMA_RECOMMENDED 2
221/* 1 while DMA in progress */
222#define PC_DMA_IN_PROGRESS 3
223/* 1 when encountered problem during DMA */
224#define PC_DMA_ERROR 4
225/* Data direction */
226#define PC_WRITING 5
227
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100228/* A pipeline stage. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229typedef struct idetape_stage_s {
230 struct request rq; /* The corresponding request */
231 struct idetape_bh *bh; /* The data buffers */
232 struct idetape_stage_s *next; /* Pointer to the next stage */
233} idetape_stage_t;
234
235/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100236 * Most of our global data which we need to save even as we leave the driver due
237 * to an interrupt or a timer event is stored in the struct defined below.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 */
239typedef struct ide_tape_obj {
240 ide_drive_t *drive;
241 ide_driver_t *driver;
242 struct gendisk *disk;
243 struct kref kref;
244
245 /*
246 * Since a typical character device operation requires more
247 * than one packet command, we provide here enough memory
248 * for the maximum of interconnected packet commands.
249 * The packet commands are stored in the circular array pc_stack.
250 * pc_stack_index points to the last used entry, and warps around
251 * to the start when we get to the last array entry.
252 *
253 * pc points to the current processed packet command.
254 *
255 * failed_pc points to the last failed packet command, or contains
256 * NULL if we do not need to retry any packet command. This is
257 * required since an additional packet command is needed before the
258 * retry, to get detailed information on what went wrong.
259 */
260 /* Current packet command */
261 idetape_pc_t *pc;
262 /* Last failed packet command */
263 idetape_pc_t *failed_pc;
264 /* Packet command stack */
265 idetape_pc_t pc_stack[IDETAPE_PC_STACK];
266 /* Next free packet command storage space */
267 int pc_stack_index;
268 struct request rq_stack[IDETAPE_PC_STACK];
269 /* We implement a circular array */
270 int rq_stack_index;
271
272 /*
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100273 * DSC polling variables.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100275 * While polling for DSC we use postponed_rq to postpone the current
276 * request so that ide.c will be able to service pending requests on the
277 * other device. Note that at most we will have only one DSC (usually
278 * data transfer) request in the device request queue. Additional
279 * requests can be queued in our internal pipeline, but they will be
280 * visible to ide.c only one at a time.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 */
282 struct request *postponed_rq;
283 /* The time in which we started polling for DSC */
284 unsigned long dsc_polling_start;
285 /* Timer used to poll for dsc */
286 struct timer_list dsc_timer;
287 /* Read/Write dsc polling frequency */
Borislav Petkov54bb2072008-02-06 02:57:52 +0100288 unsigned long best_dsc_rw_freq;
289 unsigned long dsc_poll_freq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 unsigned long dsc_timeout;
291
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100292 /* Read position information */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 u8 partition;
294 /* Current block */
Borislav Petkov54bb2072008-02-06 02:57:52 +0100295 unsigned int first_frame;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100297 /* Last error information */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 u8 sense_key, asc, ascq;
299
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100300 /* Character device operation */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 unsigned int minor;
302 /* device name */
303 char name[4];
304 /* Current character device data transfer direction */
Borislav Petkov54abf372008-02-06 02:57:52 +0100305 u8 chrdev_dir;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306
Borislav Petkov54bb2072008-02-06 02:57:52 +0100307 /* tape block size, usually 512 or 1024 bytes */
308 unsigned short blk_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 int user_bs_factor;
Borislav Petkovb6422012008-02-02 19:56:49 +0100310
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 /* Copy of the tape's Capabilities and Mechanical Page */
Borislav Petkovb6422012008-02-02 19:56:49 +0100312 u8 caps[20];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313
314 /*
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100315 * Active data transfer request parameters.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100317 * At most, there is only one ide-tape originated data transfer request
318 * in the device request queue. This allows ide.c to easily service
319 * requests from the other device when we postpone our active request.
320 * In the pipelined operation mode, we use our internal pipeline
321 * structure to hold more data requests. The data buffer size is chosen
322 * based on the tape's recommendation.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 */
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100324 /* ptr to the request which is waiting in the device request queue */
Borislav Petkov54bb2072008-02-06 02:57:52 +0100325 struct request *active_data_rq;
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100326 /* Data buffer size chosen based on the tape's recommendation */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 int stage_size;
328 idetape_stage_t *merge_stage;
329 int merge_stage_size;
330 struct idetape_bh *bh;
331 char *b_data;
332 int b_count;
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100333
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 /*
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100335 * Pipeline parameters.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100337 * To accomplish non-pipelined mode, we simply set the following
338 * variables to zero (or NULL, where appropriate).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 */
340 /* Number of currently used stages */
341 int nr_stages;
342 /* Number of pending stages */
343 int nr_pending_stages;
344 /* We will not allocate more than this number of stages */
345 int max_stages, min_pipeline, max_pipeline;
346 /* The first stage which will be removed from the pipeline */
347 idetape_stage_t *first_stage;
348 /* The currently active stage */
349 idetape_stage_t *active_stage;
350 /* Will be serviced after the currently active request */
351 idetape_stage_t *next_stage;
352 /* New requests will be added to the pipeline here */
353 idetape_stage_t *last_stage;
354 /* Optional free stage which we can use */
355 idetape_stage_t *cache_stage;
356 int pages_per_stage;
357 /* Wasted space in each stage */
358 int excess_bh_size;
359
360 /* Status/Action flags: long for set_bit */
361 unsigned long flags;
362 /* protects the ide-tape queue */
Borislav Petkov54bb2072008-02-06 02:57:52 +0100363 spinlock_t lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100365 /* Measures average tape speed */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 unsigned long avg_time;
367 int avg_size;
368 int avg_speed;
369
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 /* the door is currently locked */
371 int door_locked;
372 /* the tape hardware is write protected */
373 char drv_write_prot;
374 /* the tape is write protected (hardware or opened as read-only) */
375 char write_prot;
376
377 /*
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100378 * Limit the number of times a request can be postponed, to avoid an
379 * infinite postpone deadlock.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 int postpone_cnt;
382
383 /*
384 * Measures number of frames:
385 *
386 * 1. written/read to/from the driver pipeline (pipeline_head).
387 * 2. written/read to/from the tape buffers (idetape_bh).
388 * 3. written/read by the tape to/from the media (tape_head).
389 */
390 int pipeline_head;
391 int buffer_head;
392 int tape_head;
393 int last_tape_head;
394
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100395 /* Speed control at the tape buffers input/output */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 unsigned long insert_time;
397 int insert_size;
398 int insert_speed;
399 int max_insert_speed;
400 int measure_insert_time;
401
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100402 /* Speed regulation negative feedback loop */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 int speed_control;
404 int pipeline_head_speed;
405 int controlled_pipeline_head_speed;
406 int uncontrolled_pipeline_head_speed;
407 int controlled_last_pipeline_head;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 unsigned long uncontrolled_pipeline_head_time;
409 unsigned long controlled_pipeline_head_time;
410 int controlled_previous_pipeline_head;
411 int uncontrolled_previous_pipeline_head;
412 unsigned long controlled_previous_head_time;
413 unsigned long uncontrolled_previous_head_time;
414 int restart_speed_control_req;
415
Borislav Petkov8004a8c2008-02-06 02:57:51 +0100416 u32 debug_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417} idetape_tape_t;
418
Arjan van de Vencf8b8972006-03-23 03:00:45 -0800419static DEFINE_MUTEX(idetape_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420
Will Dysond5dee802005-09-16 02:55:07 -0700421static struct class *idetape_sysfs_class;
422
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423#define to_ide_tape(obj) container_of(obj, struct ide_tape_obj, kref)
424
425#define ide_tape_g(disk) \
426 container_of((disk)->private_data, struct ide_tape_obj, driver)
427
428static struct ide_tape_obj *ide_tape_get(struct gendisk *disk)
429{
430 struct ide_tape_obj *tape = NULL;
431
Arjan van de Vencf8b8972006-03-23 03:00:45 -0800432 mutex_lock(&idetape_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 tape = ide_tape_g(disk);
434 if (tape)
435 kref_get(&tape->kref);
Arjan van de Vencf8b8972006-03-23 03:00:45 -0800436 mutex_unlock(&idetape_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 return tape;
438}
439
440static void ide_tape_release(struct kref *);
441
442static void ide_tape_put(struct ide_tape_obj *tape)
443{
Arjan van de Vencf8b8972006-03-23 03:00:45 -0800444 mutex_lock(&idetape_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 kref_put(&tape->kref, ide_tape_release);
Arjan van de Vencf8b8972006-03-23 03:00:45 -0800446 mutex_unlock(&idetape_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447}
448
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100449/* Tape door status */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450#define DOOR_UNLOCKED 0
451#define DOOR_LOCKED 1
452#define DOOR_EXPLICITLY_LOCKED 2
453
454/*
455 * Tape flag bits values.
456 */
457#define IDETAPE_IGNORE_DSC 0
458#define IDETAPE_ADDRESS_VALID 1 /* 0 When the tape position is unknown */
459#define IDETAPE_BUSY 2 /* Device already opened */
460#define IDETAPE_PIPELINE_ERROR 3 /* Error detected in a pipeline stage */
461#define IDETAPE_DETECT_BS 4 /* Attempt to auto-detect the current user block size */
462#define IDETAPE_FILEMARK 5 /* Currently on a filemark */
463#define IDETAPE_DRQ_INTERRUPT 6 /* DRQ interrupt device */
464#define IDETAPE_READ_ERROR 7
465#define IDETAPE_PIPELINE_ACTIVE 8 /* pipeline active */
466/* 0 = no tape is loaded, so we don't rewind after ejecting */
467#define IDETAPE_MEDIUM_PRESENT 9
468
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100469/* A define for the READ BUFFER command */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470#define IDETAPE_RETRIEVE_FAULTY_BLOCK 6
471
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100472/* Some defines for the SPACE command */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473#define IDETAPE_SPACE_OVER_FILEMARK 1
474#define IDETAPE_SPACE_TO_EOD 3
475
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100476/* Some defines for the LOAD UNLOAD command */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477#define IDETAPE_LU_LOAD_MASK 1
478#define IDETAPE_LU_RETENSION_MASK 2
479#define IDETAPE_LU_EOT_MASK 4
480
481/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100482 * Special requests for our block device strategy routine.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100484 * In order to service a character device command, we add special requests to
485 * the tail of our block device request queue and wait for their completion.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 */
487
488enum {
489 REQ_IDETAPE_PC1 = (1 << 0), /* packet command (first stage) */
490 REQ_IDETAPE_PC2 = (1 << 1), /* packet command (second stage) */
491 REQ_IDETAPE_READ = (1 << 2),
492 REQ_IDETAPE_WRITE = (1 << 3),
493 REQ_IDETAPE_READ_BUFFER = (1 << 4),
494};
495
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100496/* Error codes returned in rq->errors to the higher part of the driver. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497#define IDETAPE_ERROR_GENERAL 101
498#define IDETAPE_ERROR_FILEMARK 102
499#define IDETAPE_ERROR_EOD 103
500
Borislav Petkovfa366252008-02-02 19:56:51 +0100501/* Structures related to the SELECT SENSE / MODE SENSE packet commands. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502#define IDETAPE_BLOCK_DESCRIPTOR 0
503#define IDETAPE_CAPABILITIES_PAGE 0x2a
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504
505/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100506 * The variables below are used for the character device interface. Additional
507 * state variables are defined in our ide_drive_t structure.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 */
509static struct ide_tape_obj * idetape_devs[MAX_HWIFS * MAX_DRIVES];
510
511#define ide_tape_f(file) ((file)->private_data)
512
513static struct ide_tape_obj *ide_tape_chrdev_get(unsigned int i)
514{
515 struct ide_tape_obj *tape = NULL;
516
Arjan van de Vencf8b8972006-03-23 03:00:45 -0800517 mutex_lock(&idetape_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 tape = idetape_devs[i];
519 if (tape)
520 kref_get(&tape->kref);
Arjan van de Vencf8b8972006-03-23 03:00:45 -0800521 mutex_unlock(&idetape_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 return tape;
523}
524
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525static int idetape_chrdev_release (struct inode *inode, struct file *filp);
526static void idetape_write_release (ide_drive_t *drive, unsigned int minor);
527
528/*
529 * Too bad. The drive wants to send us data which we are not ready to accept.
530 * Just throw it away.
531 */
532static void idetape_discard_data (ide_drive_t *drive, unsigned int bcount)
533{
534 while (bcount--)
535 (void) HWIF(drive)->INB(IDE_DATA_REG);
536}
537
538static void idetape_input_buffers (ide_drive_t *drive, idetape_pc_t *pc, unsigned int bcount)
539{
540 struct idetape_bh *bh = pc->bh;
541 int count;
542
543 while (bcount) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 if (bh == NULL) {
545 printk(KERN_ERR "ide-tape: bh == NULL in "
546 "idetape_input_buffers\n");
547 idetape_discard_data(drive, bcount);
548 return;
549 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 count = min((unsigned int)(bh->b_size - atomic_read(&bh->b_count)), bcount);
551 HWIF(drive)->atapi_input_bytes(drive, bh->b_data + atomic_read(&bh->b_count), count);
552 bcount -= count;
553 atomic_add(count, &bh->b_count);
554 if (atomic_read(&bh->b_count) == bh->b_size) {
555 bh = bh->b_reqnext;
556 if (bh)
557 atomic_set(&bh->b_count, 0);
558 }
559 }
560 pc->bh = bh;
561}
562
563static void idetape_output_buffers (ide_drive_t *drive, idetape_pc_t *pc, unsigned int bcount)
564{
565 struct idetape_bh *bh = pc->bh;
566 int count;
567
568 while (bcount) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 if (bh == NULL) {
570 printk(KERN_ERR "ide-tape: bh == NULL in "
571 "idetape_output_buffers\n");
572 return;
573 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 count = min((unsigned int)pc->b_count, (unsigned int)bcount);
575 HWIF(drive)->atapi_output_bytes(drive, pc->b_data, count);
576 bcount -= count;
577 pc->b_data += count;
578 pc->b_count -= count;
579 if (!pc->b_count) {
580 pc->bh = bh = bh->b_reqnext;
581 if (bh) {
582 pc->b_data = bh->b_data;
583 pc->b_count = atomic_read(&bh->b_count);
584 }
585 }
586 }
587}
588
589static void idetape_update_buffers (idetape_pc_t *pc)
590{
591 struct idetape_bh *bh = pc->bh;
592 int count;
593 unsigned int bcount = pc->actually_transferred;
594
595 if (test_bit(PC_WRITING, &pc->flags))
596 return;
597 while (bcount) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 if (bh == NULL) {
599 printk(KERN_ERR "ide-tape: bh == NULL in "
600 "idetape_update_buffers\n");
601 return;
602 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 count = min((unsigned int)bh->b_size, (unsigned int)bcount);
604 atomic_set(&bh->b_count, count);
605 if (atomic_read(&bh->b_count) == bh->b_size)
606 bh = bh->b_reqnext;
607 bcount -= count;
608 }
609 pc->bh = bh;
610}
611
612/*
613 * idetape_next_pc_storage returns a pointer to a place in which we can
614 * safely store a packet command, even though we intend to leave the
615 * driver. A storage space for a maximum of IDETAPE_PC_STACK packet
616 * commands is allocated at initialization time.
617 */
618static idetape_pc_t *idetape_next_pc_storage (ide_drive_t *drive)
619{
620 idetape_tape_t *tape = drive->driver_data;
621
Borislav Petkov8004a8c2008-02-06 02:57:51 +0100622 debug_log(DBG_PCRQ_STACK, "pc_stack_index=%d\n", tape->pc_stack_index);
623
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624 if (tape->pc_stack_index == IDETAPE_PC_STACK)
625 tape->pc_stack_index=0;
626 return (&tape->pc_stack[tape->pc_stack_index++]);
627}
628
629/*
630 * idetape_next_rq_storage is used along with idetape_next_pc_storage.
631 * Since we queue packet commands in the request queue, we need to
632 * allocate a request, along with the allocation of a packet command.
633 */
634
635/**************************************************************
636 * *
637 * This should get fixed to use kmalloc(.., GFP_ATOMIC) *
638 * followed later on by kfree(). -ml *
639 * *
640 **************************************************************/
641
642static struct request *idetape_next_rq_storage (ide_drive_t *drive)
643{
644 idetape_tape_t *tape = drive->driver_data;
645
Borislav Petkov8004a8c2008-02-06 02:57:51 +0100646 debug_log(DBG_PCRQ_STACK, "rq_stack_index=%d\n", tape->rq_stack_index);
647
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648 if (tape->rq_stack_index == IDETAPE_PC_STACK)
649 tape->rq_stack_index=0;
650 return (&tape->rq_stack[tape->rq_stack_index++]);
651}
652
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653static void idetape_init_pc (idetape_pc_t *pc)
654{
655 memset(pc->c, 0, 12);
656 pc->retries = 0;
657 pc->flags = 0;
658 pc->request_transfer = 0;
659 pc->buffer = pc->pc_buffer;
660 pc->buffer_size = IDETAPE_PC_BUFFER_SIZE;
661 pc->bh = NULL;
662 pc->b_data = NULL;
663}
664
665/*
Borislav Petkov1b5db432008-02-02 19:56:48 +0100666 * called on each failed packet command retry to analyze the request sense. We
667 * currently do not utilize this information.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668 */
Borislav Petkov1b5db432008-02-02 19:56:48 +0100669static void idetape_analyze_error(ide_drive_t *drive, u8 *sense)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670{
671 idetape_tape_t *tape = drive->driver_data;
672 idetape_pc_t *pc = tape->failed_pc;
673
Borislav Petkov1b5db432008-02-02 19:56:48 +0100674 tape->sense_key = sense[2] & 0xF;
675 tape->asc = sense[12];
676 tape->ascq = sense[13];
Borislav Petkov8004a8c2008-02-06 02:57:51 +0100677
678 debug_log(DBG_ERR, "pc = %x, sense key = %x, asc = %x, ascq = %x\n",
679 pc->c[0], tape->sense_key, tape->asc, tape->ascq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680
Borislav Petkov1b5db432008-02-02 19:56:48 +0100681 /* Correct pc->actually_transferred by asking the tape. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 if (test_bit(PC_DMA_ERROR, &pc->flags)) {
Borislav Petkov1b5db432008-02-02 19:56:48 +0100683 pc->actually_transferred = pc->request_transfer -
Borislav Petkov54bb2072008-02-06 02:57:52 +0100684 tape->blk_size *
Borislav Petkov860ff5e2008-02-02 19:56:50 +0100685 be32_to_cpu(get_unaligned((u32 *)&sense[3]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686 idetape_update_buffers(pc);
687 }
688
689 /*
690 * If error was the result of a zero-length read or write command,
691 * with sense key=5, asc=0x22, ascq=0, let it slide. Some drives
692 * (i.e. Seagate STT3401A Travan) don't support 0-length read/writes.
693 */
Borislav Petkov90699ce2008-02-02 19:56:50 +0100694 if ((pc->c[0] == READ_6 || pc->c[0] == WRITE_6)
Borislav Petkov1b5db432008-02-02 19:56:48 +0100695 /* length == 0 */
696 && pc->c[4] == 0 && pc->c[3] == 0 && pc->c[2] == 0) {
697 if (tape->sense_key == 5) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 /* don't report an error, everything's ok */
699 pc->error = 0;
700 /* don't retry read/write */
701 set_bit(PC_ABORT, &pc->flags);
702 }
703 }
Borislav Petkov90699ce2008-02-02 19:56:50 +0100704 if (pc->c[0] == READ_6 && (sense[2] & 0x80)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705 pc->error = IDETAPE_ERROR_FILEMARK;
706 set_bit(PC_ABORT, &pc->flags);
707 }
Borislav Petkov90699ce2008-02-02 19:56:50 +0100708 if (pc->c[0] == WRITE_6) {
Borislav Petkov1b5db432008-02-02 19:56:48 +0100709 if ((sense[2] & 0x40) || (tape->sense_key == 0xd
710 && tape->asc == 0x0 && tape->ascq == 0x2)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711 pc->error = IDETAPE_ERROR_EOD;
712 set_bit(PC_ABORT, &pc->flags);
713 }
714 }
Borislav Petkov90699ce2008-02-02 19:56:50 +0100715 if (pc->c[0] == READ_6 || pc->c[0] == WRITE_6) {
Borislav Petkov1b5db432008-02-02 19:56:48 +0100716 if (tape->sense_key == 8) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 pc->error = IDETAPE_ERROR_EOD;
718 set_bit(PC_ABORT, &pc->flags);
719 }
720 if (!test_bit(PC_ABORT, &pc->flags) &&
721 pc->actually_transferred)
722 pc->retries = IDETAPE_MAX_PC_RETRIES + 1;
723 }
724}
725
Borislav Petkov419d4742008-02-02 19:56:51 +0100726static void idetape_activate_next_stage(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727{
728 idetape_tape_t *tape = drive->driver_data;
729 idetape_stage_t *stage = tape->next_stage;
730 struct request *rq = &stage->rq;
731
Borislav Petkov8004a8c2008-02-06 02:57:51 +0100732 debug_log(DBG_PROCS, "Enter %s\n", __func__);
733
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 if (stage == NULL) {
Borislav Petkov8004a8c2008-02-06 02:57:51 +0100735 printk(KERN_ERR "ide-tape: bug: Trying to activate a non"
736 " existing stage\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737 return;
738 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739
740 rq->rq_disk = tape->disk;
741 rq->buffer = NULL;
742 rq->special = (void *)stage->bh;
Borislav Petkov54bb2072008-02-06 02:57:52 +0100743 tape->active_data_rq = rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744 tape->active_stage = stage;
745 tape->next_stage = stage->next;
746}
747
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100748/* Free a stage along with its related buffers completely. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749static void __idetape_kfree_stage (idetape_stage_t *stage)
750{
751 struct idetape_bh *prev_bh, *bh = stage->bh;
752 int size;
753
754 while (bh != NULL) {
755 if (bh->b_data != NULL) {
756 size = (int) bh->b_size;
757 while (size > 0) {
758 free_page((unsigned long) bh->b_data);
759 size -= PAGE_SIZE;
760 bh->b_data += PAGE_SIZE;
761 }
762 }
763 prev_bh = bh;
764 bh = bh->b_reqnext;
765 kfree(prev_bh);
766 }
767 kfree(stage);
768}
769
770static void idetape_kfree_stage (idetape_tape_t *tape, idetape_stage_t *stage)
771{
772 __idetape_kfree_stage(stage);
773}
774
775/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100776 * Remove tape->first_stage from the pipeline. The caller should avoid race
777 * conditions.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778 */
779static void idetape_remove_stage_head (ide_drive_t *drive)
780{
781 idetape_tape_t *tape = drive->driver_data;
782 idetape_stage_t *stage;
Borislav Petkov8004a8c2008-02-06 02:57:51 +0100783
784 debug_log(DBG_PROCS, "Enter %s\n", __func__);
785
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786 if (tape->first_stage == NULL) {
787 printk(KERN_ERR "ide-tape: bug: tape->first_stage is NULL\n");
Borislav Petkov55a5d292008-02-02 19:56:49 +0100788 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789 }
790 if (tape->active_stage == tape->first_stage) {
Borislav Petkov8004a8c2008-02-06 02:57:51 +0100791 printk(KERN_ERR "ide-tape: bug: Trying to free our active "
792 "pipeline stage\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793 return;
794 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795 stage = tape->first_stage;
796 tape->first_stage = stage->next;
797 idetape_kfree_stage(tape, stage);
798 tape->nr_stages--;
799 if (tape->first_stage == NULL) {
800 tape->last_stage = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801 if (tape->next_stage != NULL)
802 printk(KERN_ERR "ide-tape: bug: tape->next_stage != NULL\n");
803 if (tape->nr_stages)
804 printk(KERN_ERR "ide-tape: bug: nr_stages should be 0 now\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805 }
806}
807
808/*
809 * This will free all the pipeline stages starting from new_last_stage->next
810 * to the end of the list, and point tape->last_stage to new_last_stage.
811 */
812static void idetape_abort_pipeline(ide_drive_t *drive,
813 idetape_stage_t *new_last_stage)
814{
815 idetape_tape_t *tape = drive->driver_data;
816 idetape_stage_t *stage = new_last_stage->next;
817 idetape_stage_t *nstage;
818
Borislav Petkov8004a8c2008-02-06 02:57:51 +0100819 debug_log(DBG_PROCS, "%s: Enter %s\n", tape->name, __func__);
820
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821 while (stage) {
822 nstage = stage->next;
823 idetape_kfree_stage(tape, stage);
824 --tape->nr_stages;
825 --tape->nr_pending_stages;
826 stage = nstage;
827 }
828 if (new_last_stage)
829 new_last_stage->next = NULL;
830 tape->last_stage = new_last_stage;
831 tape->next_stage = NULL;
832}
833
834/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100835 * Finish servicing a request and insert a pending pipeline request into the
836 * main device queue.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837 */
838static int idetape_end_request(ide_drive_t *drive, int uptodate, int nr_sects)
839{
840 struct request *rq = HWGROUP(drive)->rq;
841 idetape_tape_t *tape = drive->driver_data;
842 unsigned long flags;
843 int error;
844 int remove_stage = 0;
845 idetape_stage_t *active_stage;
846
Borislav Petkov8004a8c2008-02-06 02:57:51 +0100847 debug_log(DBG_PROCS, "Enter %s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848
849 switch (uptodate) {
850 case 0: error = IDETAPE_ERROR_GENERAL; break;
851 case 1: error = 0; break;
852 default: error = uptodate;
853 }
854 rq->errors = error;
855 if (error)
856 tape->failed_pc = NULL;
857
Bartlomiej Zolnierkiewicz36872212008-01-26 20:13:10 +0100858 if (!blk_special_request(rq)) {
859 ide_end_request(drive, uptodate, nr_sects);
860 return 0;
861 }
862
Borislav Petkov54bb2072008-02-06 02:57:52 +0100863 spin_lock_irqsave(&tape->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864
865 /* The request was a pipelined data transfer request */
Borislav Petkov54bb2072008-02-06 02:57:52 +0100866 if (tape->active_data_rq == rq) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867 active_stage = tape->active_stage;
868 tape->active_stage = NULL;
Borislav Petkov54bb2072008-02-06 02:57:52 +0100869 tape->active_data_rq = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870 tape->nr_pending_stages--;
871 if (rq->cmd[0] & REQ_IDETAPE_WRITE) {
872 remove_stage = 1;
873 if (error) {
874 set_bit(IDETAPE_PIPELINE_ERROR, &tape->flags);
875 if (error == IDETAPE_ERROR_EOD)
876 idetape_abort_pipeline(drive, active_stage);
877 }
878 } else if (rq->cmd[0] & REQ_IDETAPE_READ) {
879 if (error == IDETAPE_ERROR_EOD) {
880 set_bit(IDETAPE_PIPELINE_ERROR, &tape->flags);
881 idetape_abort_pipeline(drive, active_stage);
882 }
883 }
884 if (tape->next_stage != NULL) {
Borislav Petkov419d4742008-02-02 19:56:51 +0100885 idetape_activate_next_stage(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100887 /* Insert the next request into the request queue. */
Borislav Petkov54bb2072008-02-06 02:57:52 +0100888 (void)ide_do_drive_cmd(drive, tape->active_data_rq,
889 ide_end);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890 } else if (!error) {
Borislav Petkov97219852008-02-06 02:57:52 +0100891 /*
892 * This is a part of the feedback loop which tries to
893 * find the optimum number of stages. We are starting
894 * from a minimum maximum number of stages, and if we
895 * sense that the pipeline is empty, we try to increase
896 * it, until we reach the user compile time memory
897 * limit.
898 */
899 int i = (tape->max_pipeline - tape->min_pipeline) / 10;
900
901 tape->max_stages += max(i, 1);
902 tape->max_stages = max(tape->max_stages,
903 tape->min_pipeline);
904 tape->max_stages = min(tape->max_stages,
905 tape->max_pipeline);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906 }
907 }
908 ide_end_drive_cmd(drive, 0, 0);
909// blkdev_dequeue_request(rq);
910// drive->rq = NULL;
911// end_that_request_last(rq);
912
913 if (remove_stage)
914 idetape_remove_stage_head(drive);
Borislav Petkov54bb2072008-02-06 02:57:52 +0100915 if (tape->active_data_rq == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916 clear_bit(IDETAPE_PIPELINE_ACTIVE, &tape->flags);
Borislav Petkov54bb2072008-02-06 02:57:52 +0100917 spin_unlock_irqrestore(&tape->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918 return 0;
919}
920
921static ide_startstop_t idetape_request_sense_callback (ide_drive_t *drive)
922{
923 idetape_tape_t *tape = drive->driver_data;
924
Borislav Petkov8004a8c2008-02-06 02:57:51 +0100925 debug_log(DBG_PROCS, "Enter %s\n", __func__);
926
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927 if (!tape->pc->error) {
Borislav Petkov1b5db432008-02-02 19:56:48 +0100928 idetape_analyze_error(drive, tape->pc->buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929 idetape_end_request(drive, 1, 0);
930 } else {
931 printk(KERN_ERR "ide-tape: Error in REQUEST SENSE itself - Aborting request!\n");
932 idetape_end_request(drive, 0, 0);
933 }
934 return ide_stopped;
935}
936
937static void idetape_create_request_sense_cmd (idetape_pc_t *pc)
938{
939 idetape_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +0100940 pc->c[0] = REQUEST_SENSE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941 pc->c[4] = 20;
942 pc->request_transfer = 20;
943 pc->callback = &idetape_request_sense_callback;
944}
945
946static void idetape_init_rq(struct request *rq, u8 cmd)
947{
948 memset(rq, 0, sizeof(*rq));
Jens Axboe4aff5e22006-08-10 08:44:47 +0200949 rq->cmd_type = REQ_TYPE_SPECIAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950 rq->cmd[0] = cmd;
951}
952
953/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100954 * Generate a new packet command request in front of the request queue, before
955 * the current request, so that it will be processed immediately, on the next
956 * pass through the driver. The function below is called from the request
957 * handling part of the driver (the "bottom" part). Safe storage for the request
958 * should be allocated with ide_tape_next_{pc,rq}_storage() prior to that.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100960 * Memory for those requests is pre-allocated at initialization time, and is
961 * limited to IDETAPE_PC_STACK requests. We assume that we have enough space for
962 * the maximum possible number of inter-dependent packet commands.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100964 * The higher level of the driver - The ioctl handler and the character device
965 * handling functions should queue request to the lower level part and wait for
966 * their completion using idetape_queue_pc_tail or idetape_queue_rw_tail.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967 */
968static void idetape_queue_pc_head (ide_drive_t *drive, idetape_pc_t *pc,struct request *rq)
969{
970 struct ide_tape_obj *tape = drive->driver_data;
971
972 idetape_init_rq(rq, REQ_IDETAPE_PC1);
973 rq->buffer = (char *) pc;
974 rq->rq_disk = tape->disk;
975 (void) ide_do_drive_cmd(drive, rq, ide_preempt);
976}
977
978/*
979 * idetape_retry_pc is called when an error was detected during the
980 * last packet command. We queue a request sense packet command in
981 * the head of the request list.
982 */
983static ide_startstop_t idetape_retry_pc (ide_drive_t *drive)
984{
985 idetape_tape_t *tape = drive->driver_data;
986 idetape_pc_t *pc;
987 struct request *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988
Bartlomiej Zolnierkiewicz64a57fe2008-02-06 02:57:51 +0100989 (void)ide_read_error(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990 pc = idetape_next_pc_storage(drive);
991 rq = idetape_next_rq_storage(drive);
992 idetape_create_request_sense_cmd(pc);
993 set_bit(IDETAPE_IGNORE_DSC, &tape->flags);
994 idetape_queue_pc_head(drive, pc, rq);
995 return ide_stopped;
996}
997
998/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100999 * Postpone the current request so that ide.c will be able to service requests
1000 * from another device on the same hwgroup while we are polling for DSC.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001 */
1002static void idetape_postpone_request (ide_drive_t *drive)
1003{
1004 idetape_tape_t *tape = drive->driver_data;
1005
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001006 debug_log(DBG_PROCS, "Enter %s\n", __func__);
1007
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008 tape->postponed_rq = HWGROUP(drive)->rq;
Borislav Petkov54bb2072008-02-06 02:57:52 +01001009 ide_stall_queue(drive, tape->dsc_poll_freq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010}
1011
Borislav Petkova1efc852008-02-06 02:57:52 +01001012typedef void idetape_io_buf(ide_drive_t *, idetape_pc_t *, unsigned int);
1013
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014/*
Borislav Petkova1efc852008-02-06 02:57:52 +01001015 * This is the usual interrupt handler which will be called during a packet
1016 * command. We will transfer some of the data (as requested by the drive) and
1017 * will re-point interrupt handler to us. When data transfer is finished, we
1018 * will act according to the algorithm described before
Borislav Petkov8d06bfa2008-02-06 02:57:53 +01001019 * idetape_issue_pc.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020 */
Borislav Petkova1efc852008-02-06 02:57:52 +01001021static ide_startstop_t idetape_pc_intr(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022{
1023 ide_hwif_t *hwif = drive->hwif;
1024 idetape_tape_t *tape = drive->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025 idetape_pc_t *pc = tape->pc;
Borislav Petkova1efc852008-02-06 02:57:52 +01001026 xfer_func_t *xferfunc;
1027 idetape_io_buf *iobuf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028 unsigned int temp;
1029#if SIMULATE_ERRORS
1030 static int error_sim_count = 0;
1031#endif
Bartlomiej Zolnierkiewicz790d1232008-01-25 22:17:12 +01001032 u16 bcount;
Bartlomiej Zolnierkiewicz8e7657a2008-01-25 22:17:12 +01001033 u8 stat, ireason;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001035 debug_log(DBG_PROCS, "Enter %s - interrupt handler\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036
1037 /* Clear the interrupt */
Bartlomiej Zolnierkiewiczc47137a2008-02-06 02:57:51 +01001038 stat = ide_read_status(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039
1040 if (test_bit(PC_DMA_IN_PROGRESS, &pc->flags)) {
Bartlomiej Zolnierkiewicz22c525b2008-01-25 22:17:11 +01001041 if (hwif->ide_dma_end(drive) || (stat & ERR_STAT)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042 /*
1043 * A DMA error is sometimes expected. For example,
1044 * if the tape is crossing a filemark during a
1045 * READ command, it will issue an irq and position
1046 * itself before the filemark, so that only a partial
1047 * data transfer will occur (which causes the DMA
1048 * error). In that case, we will later ask the tape
1049 * how much bytes of the original request were
1050 * actually transferred (we can't receive that
1051 * information from the DMA engine on most chipsets).
1052 */
1053
1054 /*
1055 * On the contrary, a DMA error is never expected;
1056 * it usually indicates a hardware error or abort.
1057 * If the tape crosses a filemark during a READ
1058 * command, it will issue an irq and position itself
1059 * after the filemark (not before). Only a partial
1060 * data transfer will occur, but no DMA error.
1061 * (AS, 19 Apr 2001)
1062 */
1063 set_bit(PC_DMA_ERROR, &pc->flags);
1064 } else {
1065 pc->actually_transferred = pc->request_transfer;
1066 idetape_update_buffers(pc);
1067 }
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001068 debug_log(DBG_PROCS, "DMA finished\n");
1069
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070 }
1071
1072 /* No more interrupts */
Bartlomiej Zolnierkiewicz22c525b2008-01-25 22:17:11 +01001073 if ((stat & DRQ_STAT) == 0) {
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001074 debug_log(DBG_SENSE, "Packet command completed, %d bytes"
1075 " transferred\n", pc->actually_transferred);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001077 clear_bit(PC_DMA_IN_PROGRESS, &pc->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078 local_irq_enable();
1079
1080#if SIMULATE_ERRORS
Borislav Petkov90699ce2008-02-02 19:56:50 +01001081 if ((pc->c[0] == WRITE_6 || pc->c[0] == READ_6) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082 (++error_sim_count % 100) == 0) {
1083 printk(KERN_INFO "ide-tape: %s: simulating error\n",
1084 tape->name);
Bartlomiej Zolnierkiewicz22c525b2008-01-25 22:17:11 +01001085 stat |= ERR_STAT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086 }
1087#endif
Borislav Petkov90699ce2008-02-02 19:56:50 +01001088 if ((stat & ERR_STAT) && pc->c[0] == REQUEST_SENSE)
Bartlomiej Zolnierkiewicz22c525b2008-01-25 22:17:11 +01001089 stat &= ~ERR_STAT;
1090 if ((stat & ERR_STAT) || test_bit(PC_DMA_ERROR, &pc->flags)) {
1091 /* Error detected */
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001092 debug_log(DBG_ERR, "%s: I/O error\n", tape->name);
1093
Borislav Petkov90699ce2008-02-02 19:56:50 +01001094 if (pc->c[0] == REQUEST_SENSE) {
Borislav Petkova1efc852008-02-06 02:57:52 +01001095 printk(KERN_ERR "ide-tape: I/O error in request"
1096 " sense command\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097 return ide_do_reset(drive);
1098 }
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001099 debug_log(DBG_ERR, "[cmd %x]: check condition\n",
1100 pc->c[0]);
1101
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102 /* Retry operation */
1103 return idetape_retry_pc(drive);
1104 }
1105 pc->error = 0;
1106 if (test_bit(PC_WAIT_FOR_DSC, &pc->flags) &&
Bartlomiej Zolnierkiewicz22c525b2008-01-25 22:17:11 +01001107 (stat & SEEK_STAT) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108 /* Media access command */
1109 tape->dsc_polling_start = jiffies;
Borislav Petkov54bb2072008-02-06 02:57:52 +01001110 tape->dsc_poll_freq = IDETAPE_DSC_MA_FAST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111 tape->dsc_timeout = jiffies + IDETAPE_DSC_MA_TIMEOUT;
1112 /* Allow ide.c to handle other requests */
1113 idetape_postpone_request(drive);
1114 return ide_stopped;
1115 }
1116 if (tape->failed_pc == pc)
1117 tape->failed_pc = NULL;
1118 /* Command finished - Call the callback function */
1119 return pc->callback(drive);
1120 }
1121 if (test_and_clear_bit(PC_DMA_IN_PROGRESS, &pc->flags)) {
1122 printk(KERN_ERR "ide-tape: The tape wants to issue more "
1123 "interrupts in DMA mode\n");
1124 printk(KERN_ERR "ide-tape: DMA disabled, reverting to PIO\n");
Bartlomiej Zolnierkiewicz7469aaf2007-02-17 02:40:26 +01001125 ide_dma_off(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126 return ide_do_reset(drive);
1127 }
1128 /* Get the number of bytes to transfer on this interrupt. */
Bartlomiej Zolnierkiewicz790d1232008-01-25 22:17:12 +01001129 bcount = (hwif->INB(IDE_BCOUNTH_REG) << 8) |
1130 hwif->INB(IDE_BCOUNTL_REG);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131
Bartlomiej Zolnierkiewicz8e7657a2008-01-25 22:17:12 +01001132 ireason = hwif->INB(IDE_IREASON_REG);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133
Bartlomiej Zolnierkiewicz8e7657a2008-01-25 22:17:12 +01001134 if (ireason & CD) {
Borislav Petkova1efc852008-02-06 02:57:52 +01001135 printk(KERN_ERR "ide-tape: CoD != 0 in %s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136 return ide_do_reset(drive);
1137 }
Bartlomiej Zolnierkiewicz8e7657a2008-01-25 22:17:12 +01001138 if (((ireason & IO) == IO) == test_bit(PC_WRITING, &pc->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139 /* Hopefully, we will never get here */
1140 printk(KERN_ERR "ide-tape: We wanted to %s, ",
Bartlomiej Zolnierkiewicz8e7657a2008-01-25 22:17:12 +01001141 (ireason & IO) ? "Write" : "Read");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142 printk(KERN_ERR "ide-tape: but the tape wants us to %s !\n",
Bartlomiej Zolnierkiewicz8e7657a2008-01-25 22:17:12 +01001143 (ireason & IO) ? "Read" : "Write");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144 return ide_do_reset(drive);
1145 }
1146 if (!test_bit(PC_WRITING, &pc->flags)) {
1147 /* Reading - Check that we have enough space */
Bartlomiej Zolnierkiewicz790d1232008-01-25 22:17:12 +01001148 temp = pc->actually_transferred + bcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149 if (temp > pc->request_transfer) {
1150 if (temp > pc->buffer_size) {
Borislav Petkova1efc852008-02-06 02:57:52 +01001151 printk(KERN_ERR "ide-tape: The tape wants to "
1152 "send us more data than expected "
1153 "- discarding data\n");
Bartlomiej Zolnierkiewicz790d1232008-01-25 22:17:12 +01001154 idetape_discard_data(drive, bcount);
Borislav Petkova1efc852008-02-06 02:57:52 +01001155 ide_set_handler(drive, &idetape_pc_intr,
1156 IDETAPE_WAIT_CMD, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157 return ide_started;
1158 }
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001159 debug_log(DBG_SENSE, "The tape wants to send us more "
1160 "data than expected - allowing transfer\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161 }
Borislav Petkova1efc852008-02-06 02:57:52 +01001162 iobuf = &idetape_input_buffers;
1163 xferfunc = hwif->atapi_input_bytes;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164 } else {
Borislav Petkova1efc852008-02-06 02:57:52 +01001165 iobuf = &idetape_output_buffers;
1166 xferfunc = hwif->atapi_output_bytes;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167 }
Borislav Petkova1efc852008-02-06 02:57:52 +01001168
1169 if (pc->bh)
1170 iobuf(drive, pc, bcount);
1171 else
1172 xferfunc(drive, pc->current_position, bcount);
1173
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174 /* Update the current position */
Bartlomiej Zolnierkiewicz790d1232008-01-25 22:17:12 +01001175 pc->actually_transferred += bcount;
1176 pc->current_position += bcount;
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001177
1178 debug_log(DBG_SENSE, "[cmd %x] transferred %d bytes on that intr.\n",
1179 pc->c[0], bcount);
1180
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181 /* And set the interrupt handler again */
1182 ide_set_handler(drive, &idetape_pc_intr, IDETAPE_WAIT_CMD, NULL);
1183 return ide_started;
1184}
1185
1186/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001187 * Packet Command Interface
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001189 * The current Packet Command is available in tape->pc, and will not change
1190 * until we finish handling it. Each packet command is associated with a
1191 * callback function that will be called when the command is finished.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001193 * The handling will be done in three stages:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001195 * 1. idetape_issue_pc will send the packet command to the drive, and will set
1196 * the interrupt handler to idetape_pc_intr.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001198 * 2. On each interrupt, idetape_pc_intr will be called. This step will be
1199 * repeated until the device signals us that no more interrupts will be issued.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001201 * 3. ATAPI Tape media access commands have immediate status with a delayed
1202 * process. In case of a successful initiation of a media access packet command,
1203 * the DSC bit will be set when the actual execution of the command is finished.
1204 * Since the tape drive will not issue an interrupt, we have to poll for this
1205 * event. In this case, we define the request as "low priority request" by
1206 * setting rq_status to IDETAPE_RQ_POSTPONED, set a timer to poll for DSC and
1207 * exit the driver.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001209 * ide.c will then give higher priority to requests which originate from the
1210 * other device, until will change rq_status to RQ_ACTIVE.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001212 * 4. When the packet command is finished, it will be checked for errors.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001214 * 5. In case an error was found, we queue a request sense packet command in
1215 * front of the request queue and retry the operation up to
1216 * IDETAPE_MAX_PC_RETRIES times.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001218 * 6. In case no error was found, or we decided to give up and not to retry
1219 * again, the callback function will be called and then we will handle the next
1220 * request.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221 */
1222static ide_startstop_t idetape_transfer_pc(ide_drive_t *drive)
1223{
1224 ide_hwif_t *hwif = drive->hwif;
1225 idetape_tape_t *tape = drive->driver_data;
1226 idetape_pc_t *pc = tape->pc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227 int retries = 100;
1228 ide_startstop_t startstop;
Bartlomiej Zolnierkiewicz8e7657a2008-01-25 22:17:12 +01001229 u8 ireason;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230
1231 if (ide_wait_stat(&startstop,drive,DRQ_STAT,BUSY_STAT,WAIT_READY)) {
1232 printk(KERN_ERR "ide-tape: Strange, packet command initiated yet DRQ isn't asserted\n");
1233 return startstop;
1234 }
Bartlomiej Zolnierkiewicz8e7657a2008-01-25 22:17:12 +01001235 ireason = hwif->INB(IDE_IREASON_REG);
1236 while (retries-- && ((ireason & CD) == 0 || (ireason & IO))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237 printk(KERN_ERR "ide-tape: (IO,CoD != (0,1) while issuing "
1238 "a packet command, retrying\n");
1239 udelay(100);
Bartlomiej Zolnierkiewicz8e7657a2008-01-25 22:17:12 +01001240 ireason = hwif->INB(IDE_IREASON_REG);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241 if (retries == 0) {
1242 printk(KERN_ERR "ide-tape: (IO,CoD != (0,1) while "
1243 "issuing a packet command, ignoring\n");
Bartlomiej Zolnierkiewicz8e7657a2008-01-25 22:17:12 +01001244 ireason |= CD;
1245 ireason &= ~IO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246 }
1247 }
Bartlomiej Zolnierkiewicz8e7657a2008-01-25 22:17:12 +01001248 if ((ireason & CD) == 0 || (ireason & IO)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249 printk(KERN_ERR "ide-tape: (IO,CoD) != (0,1) while issuing "
1250 "a packet command\n");
1251 return ide_do_reset(drive);
1252 }
1253 /* Set the interrupt routine */
1254 ide_set_handler(drive, &idetape_pc_intr, IDETAPE_WAIT_CMD, NULL);
1255#ifdef CONFIG_BLK_DEV_IDEDMA
1256 /* Begin DMA, if necessary */
1257 if (test_bit(PC_DMA_IN_PROGRESS, &pc->flags))
1258 hwif->dma_start(drive);
1259#endif
1260 /* Send the actual packet */
1261 HWIF(drive)->atapi_output_bytes(drive, pc->c, 12);
1262 return ide_started;
1263}
1264
Borislav Petkov8d06bfa2008-02-06 02:57:53 +01001265static ide_startstop_t idetape_issue_pc(ide_drive_t *drive, idetape_pc_t *pc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266{
1267 ide_hwif_t *hwif = drive->hwif;
1268 idetape_tape_t *tape = drive->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269 int dma_ok = 0;
Bartlomiej Zolnierkiewicz790d1232008-01-25 22:17:12 +01001270 u16 bcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271
Borislav Petkov90699ce2008-02-02 19:56:50 +01001272 if (tape->pc->c[0] == REQUEST_SENSE &&
1273 pc->c[0] == REQUEST_SENSE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274 printk(KERN_ERR "ide-tape: possible ide-tape.c bug - "
1275 "Two request sense in serial were issued\n");
1276 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277
Borislav Petkov90699ce2008-02-02 19:56:50 +01001278 if (tape->failed_pc == NULL && pc->c[0] != REQUEST_SENSE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279 tape->failed_pc = pc;
1280 /* Set the current packet command */
1281 tape->pc = pc;
1282
1283 if (pc->retries > IDETAPE_MAX_PC_RETRIES ||
1284 test_bit(PC_ABORT, &pc->flags)) {
1285 /*
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001286 * We will "abort" retrying a packet command in case legitimate
1287 * error code was received (crossing a filemark, or end of the
1288 * media, for example).
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289 */
1290 if (!test_bit(PC_ABORT, &pc->flags)) {
Borislav Petkov90699ce2008-02-02 19:56:50 +01001291 if (!(pc->c[0] == TEST_UNIT_READY &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292 tape->sense_key == 2 && tape->asc == 4 &&
1293 (tape->ascq == 1 || tape->ascq == 8))) {
1294 printk(KERN_ERR "ide-tape: %s: I/O error, "
1295 "pc = %2x, key = %2x, "
1296 "asc = %2x, ascq = %2x\n",
1297 tape->name, pc->c[0],
1298 tape->sense_key, tape->asc,
1299 tape->ascq);
1300 }
1301 /* Giving up */
1302 pc->error = IDETAPE_ERROR_GENERAL;
1303 }
1304 tape->failed_pc = NULL;
1305 return pc->callback(drive);
1306 }
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001307 debug_log(DBG_SENSE, "Retry #%d, cmd = %02X\n", pc->retries, pc->c[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308
1309 pc->retries++;
1310 /* We haven't transferred any data yet */
1311 pc->actually_transferred = 0;
1312 pc->current_position = pc->buffer;
1313 /* Request to transfer the entire buffer at once */
Bartlomiej Zolnierkiewicz790d1232008-01-25 22:17:12 +01001314 bcount = pc->request_transfer;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315
1316 if (test_and_clear_bit(PC_DMA_ERROR, &pc->flags)) {
1317 printk(KERN_WARNING "ide-tape: DMA disabled, "
1318 "reverting to PIO\n");
Bartlomiej Zolnierkiewicz7469aaf2007-02-17 02:40:26 +01001319 ide_dma_off(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320 }
1321 if (test_bit(PC_DMA_RECOMMENDED, &pc->flags) && drive->using_dma)
1322 dma_ok = !hwif->dma_setup(drive);
1323
Bartlomiej Zolnierkiewicz2fc57382008-01-25 22:17:13 +01001324 ide_pktcmd_tf_load(drive, IDE_TFLAG_NO_SELECT_MASK |
1325 IDE_TFLAG_OUT_DEVICE, bcount, dma_ok);
1326
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327 if (dma_ok) /* Will begin DMA later */
1328 set_bit(PC_DMA_IN_PROGRESS, &pc->flags);
1329 if (test_bit(IDETAPE_DRQ_INTERRUPT, &tape->flags)) {
Bartlomiej Zolnierkiewiczc1c9dbc2008-02-02 19:56:44 +01001330 ide_execute_command(drive, WIN_PACKETCMD, &idetape_transfer_pc,
1331 IDETAPE_WAIT_CMD, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332 return ide_started;
1333 } else {
1334 hwif->OUTB(WIN_PACKETCMD, IDE_COMMAND_REG);
1335 return idetape_transfer_pc(drive);
1336 }
1337}
1338
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339static ide_startstop_t idetape_pc_callback (ide_drive_t *drive)
1340{
1341 idetape_tape_t *tape = drive->driver_data;
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001342
1343 debug_log(DBG_PROCS, "Enter %s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344
1345 idetape_end_request(drive, tape->pc->error ? 0 : 1, 0);
1346 return ide_stopped;
1347}
1348
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001349/* A mode sense command is used to "sense" tape parameters. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350static void idetape_create_mode_sense_cmd (idetape_pc_t *pc, u8 page_code)
1351{
1352 idetape_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +01001353 pc->c[0] = MODE_SENSE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354 if (page_code != IDETAPE_BLOCK_DESCRIPTOR)
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001355 /* DBD = 1 - Don't return block descriptors */
1356 pc->c[1] = 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357 pc->c[2] = page_code;
1358 /*
1359 * Changed pc->c[3] to 0 (255 will at best return unused info).
1360 *
1361 * For SCSI this byte is defined as subpage instead of high byte
1362 * of length and some IDE drives seem to interpret it this way
1363 * and return an error when 255 is used.
1364 */
1365 pc->c[3] = 0;
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001366 /* We will just discard data in that case */
1367 pc->c[4] = 255;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368 if (page_code == IDETAPE_BLOCK_DESCRIPTOR)
1369 pc->request_transfer = 12;
1370 else if (page_code == IDETAPE_CAPABILITIES_PAGE)
1371 pc->request_transfer = 24;
1372 else
1373 pc->request_transfer = 50;
1374 pc->callback = &idetape_pc_callback;
1375}
1376
Borislav Petkov37016ba2008-02-06 02:57:52 +01001377static void idetape_calculate_speeds(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378{
1379 idetape_tape_t *tape = drive->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380
1381 if (time_after(jiffies, tape->controlled_pipeline_head_time + 120 * HZ)) {
1382 tape->controlled_previous_pipeline_head = tape->controlled_last_pipeline_head;
1383 tape->controlled_previous_head_time = tape->controlled_pipeline_head_time;
1384 tape->controlled_last_pipeline_head = tape->pipeline_head;
1385 tape->controlled_pipeline_head_time = jiffies;
1386 }
1387 if (time_after(jiffies, tape->controlled_pipeline_head_time + 60 * HZ))
1388 tape->controlled_pipeline_head_speed = (tape->pipeline_head - tape->controlled_last_pipeline_head) * 32 * HZ / (jiffies - tape->controlled_pipeline_head_time);
1389 else if (time_after(jiffies, tape->controlled_previous_head_time))
1390 tape->controlled_pipeline_head_speed = (tape->pipeline_head - tape->controlled_previous_pipeline_head) * 32 * HZ / (jiffies - tape->controlled_previous_head_time);
1391
1392 if (tape->nr_pending_stages < tape->max_stages /*- 1 */) {
1393 /* -1 for read mode error recovery */
1394 if (time_after(jiffies, tape->uncontrolled_previous_head_time + 10 * HZ)) {
1395 tape->uncontrolled_pipeline_head_time = jiffies;
1396 tape->uncontrolled_pipeline_head_speed = (tape->pipeline_head - tape->uncontrolled_previous_pipeline_head) * 32 * HZ / (jiffies - tape->uncontrolled_previous_head_time);
1397 }
1398 } else {
1399 tape->uncontrolled_previous_head_time = jiffies;
1400 tape->uncontrolled_previous_pipeline_head = tape->pipeline_head;
1401 if (time_after(jiffies, tape->uncontrolled_pipeline_head_time + 30 * HZ)) {
1402 tape->uncontrolled_pipeline_head_time = jiffies;
1403 }
1404 }
1405 tape->pipeline_head_speed = max(tape->uncontrolled_pipeline_head_speed, tape->controlled_pipeline_head_speed);
Borislav Petkov37016ba2008-02-06 02:57:52 +01001406
1407 if (tape->speed_control == 1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408 if (tape->nr_pending_stages >= tape->max_stages / 2)
1409 tape->max_insert_speed = tape->pipeline_head_speed +
1410 (1100 - tape->pipeline_head_speed) * 2 * (tape->nr_pending_stages - tape->max_stages / 2) / tape->max_stages;
1411 else
1412 tape->max_insert_speed = 500 +
1413 (tape->pipeline_head_speed - 500) * 2 * tape->nr_pending_stages / tape->max_stages;
Borislav Petkov37016ba2008-02-06 02:57:52 +01001414
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415 if (tape->nr_pending_stages >= tape->max_stages * 99 / 100)
1416 tape->max_insert_speed = 5000;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417 } else
1418 tape->max_insert_speed = tape->speed_control;
Borislav Petkov37016ba2008-02-06 02:57:52 +01001419
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420 tape->max_insert_speed = max(tape->max_insert_speed, 500);
1421}
1422
1423static ide_startstop_t idetape_media_access_finished (ide_drive_t *drive)
1424{
1425 idetape_tape_t *tape = drive->driver_data;
1426 idetape_pc_t *pc = tape->pc;
Bartlomiej Zolnierkiewicz22c525b2008-01-25 22:17:11 +01001427 u8 stat;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428
Bartlomiej Zolnierkiewiczc47137a2008-02-06 02:57:51 +01001429 stat = ide_read_status(drive);
1430
Bartlomiej Zolnierkiewicz22c525b2008-01-25 22:17:11 +01001431 if (stat & SEEK_STAT) {
1432 if (stat & ERR_STAT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433 /* Error detected */
Borislav Petkov90699ce2008-02-02 19:56:50 +01001434 if (pc->c[0] != TEST_UNIT_READY)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435 printk(KERN_ERR "ide-tape: %s: I/O error, ",
1436 tape->name);
1437 /* Retry operation */
1438 return idetape_retry_pc(drive);
1439 }
1440 pc->error = 0;
1441 if (tape->failed_pc == pc)
1442 tape->failed_pc = NULL;
1443 } else {
1444 pc->error = IDETAPE_ERROR_GENERAL;
1445 tape->failed_pc = NULL;
1446 }
1447 return pc->callback(drive);
1448}
1449
1450static ide_startstop_t idetape_rw_callback (ide_drive_t *drive)
1451{
1452 idetape_tape_t *tape = drive->driver_data;
1453 struct request *rq = HWGROUP(drive)->rq;
Borislav Petkov54bb2072008-02-06 02:57:52 +01001454 int blocks = tape->pc->actually_transferred / tape->blk_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455
Borislav Petkov54bb2072008-02-06 02:57:52 +01001456 tape->avg_size += blocks * tape->blk_size;
1457 tape->insert_size += blocks * tape->blk_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458 if (tape->insert_size > 1024 * 1024)
1459 tape->measure_insert_time = 1;
1460 if (tape->measure_insert_time) {
1461 tape->measure_insert_time = 0;
1462 tape->insert_time = jiffies;
1463 tape->insert_size = 0;
1464 }
1465 if (time_after(jiffies, tape->insert_time))
1466 tape->insert_speed = tape->insert_size / 1024 * HZ / (jiffies - tape->insert_time);
Marcelo Feitoza Parisi9bae1ff2006-03-28 01:56:46 -08001467 if (time_after_eq(jiffies, tape->avg_time + HZ)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468 tape->avg_speed = tape->avg_size * HZ / (jiffies - tape->avg_time) / 1024;
1469 tape->avg_size = 0;
1470 tape->avg_time = jiffies;
1471 }
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001472 debug_log(DBG_PROCS, "Enter %s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473
Borislav Petkov54bb2072008-02-06 02:57:52 +01001474 tape->first_frame += blocks;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475 rq->current_nr_sectors -= blocks;
1476
1477 if (!tape->pc->error)
1478 idetape_end_request(drive, 1, 0);
1479 else
1480 idetape_end_request(drive, tape->pc->error, 0);
1481 return ide_stopped;
1482}
1483
1484static void idetape_create_read_cmd(idetape_tape_t *tape, idetape_pc_t *pc, unsigned int length, struct idetape_bh *bh)
1485{
1486 idetape_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +01001487 pc->c[0] = READ_6;
Borislav Petkov860ff5e2008-02-02 19:56:50 +01001488 put_unaligned(cpu_to_be32(length), (unsigned int *) &pc->c[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489 pc->c[1] = 1;
1490 pc->callback = &idetape_rw_callback;
1491 pc->bh = bh;
1492 atomic_set(&bh->b_count, 0);
1493 pc->buffer = NULL;
Borislav Petkov54bb2072008-02-06 02:57:52 +01001494 pc->buffer_size = length * tape->blk_size;
1495 pc->request_transfer = pc->buffer_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496 if (pc->request_transfer == tape->stage_size)
1497 set_bit(PC_DMA_RECOMMENDED, &pc->flags);
1498}
1499
1500static void idetape_create_read_buffer_cmd(idetape_tape_t *tape, idetape_pc_t *pc, unsigned int length, struct idetape_bh *bh)
1501{
1502 int size = 32768;
1503 struct idetape_bh *p = bh;
1504
1505 idetape_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +01001506 pc->c[0] = READ_BUFFER;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507 pc->c[1] = IDETAPE_RETRIEVE_FAULTY_BLOCK;
1508 pc->c[7] = size >> 8;
1509 pc->c[8] = size & 0xff;
1510 pc->callback = &idetape_pc_callback;
1511 pc->bh = bh;
1512 atomic_set(&bh->b_count, 0);
1513 pc->buffer = NULL;
1514 while (p) {
1515 atomic_set(&p->b_count, 0);
1516 p = p->b_reqnext;
1517 }
1518 pc->request_transfer = pc->buffer_size = size;
1519}
1520
1521static void idetape_create_write_cmd(idetape_tape_t *tape, idetape_pc_t *pc, unsigned int length, struct idetape_bh *bh)
1522{
1523 idetape_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +01001524 pc->c[0] = WRITE_6;
Borislav Petkov860ff5e2008-02-02 19:56:50 +01001525 put_unaligned(cpu_to_be32(length), (unsigned int *) &pc->c[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526 pc->c[1] = 1;
1527 pc->callback = &idetape_rw_callback;
1528 set_bit(PC_WRITING, &pc->flags);
1529 pc->bh = bh;
1530 pc->b_data = bh->b_data;
1531 pc->b_count = atomic_read(&bh->b_count);
1532 pc->buffer = NULL;
Borislav Petkov54bb2072008-02-06 02:57:52 +01001533 pc->buffer_size = length * tape->blk_size;
1534 pc->request_transfer = pc->buffer_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535 if (pc->request_transfer == tape->stage_size)
1536 set_bit(PC_DMA_RECOMMENDED, &pc->flags);
1537}
1538
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539static ide_startstop_t idetape_do_request(ide_drive_t *drive,
1540 struct request *rq, sector_t block)
1541{
1542 idetape_tape_t *tape = drive->driver_data;
1543 idetape_pc_t *pc = NULL;
1544 struct request *postponed_rq = tape->postponed_rq;
Bartlomiej Zolnierkiewicz22c525b2008-01-25 22:17:11 +01001545 u8 stat;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001546
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001547 debug_log(DBG_SENSE, "sector: %ld, nr_sectors: %ld,"
1548 " current_nr_sectors: %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549 rq->sector, rq->nr_sectors, rq->current_nr_sectors);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550
Jens Axboe4aff5e22006-08-10 08:44:47 +02001551 if (!blk_special_request(rq)) {
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001552 /* We do not support buffer cache originated requests. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553 printk(KERN_NOTICE "ide-tape: %s: Unsupported request in "
Jens Axboe4aff5e22006-08-10 08:44:47 +02001554 "request queue (%d)\n", drive->name, rq->cmd_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001555 ide_end_request(drive, 0, 0);
1556 return ide_stopped;
1557 }
1558
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001559 /* Retry a failed packet command */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560 if (tape->failed_pc != NULL &&
Borislav Petkov90699ce2008-02-02 19:56:50 +01001561 tape->pc->c[0] == REQUEST_SENSE) {
Borislav Petkov8d06bfa2008-02-06 02:57:53 +01001562 return idetape_issue_pc(drive, tape->failed_pc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001564 if (postponed_rq != NULL)
1565 if (rq != postponed_rq) {
1566 printk(KERN_ERR "ide-tape: ide-tape.c bug - "
1567 "Two DSC requests were queued\n");
1568 idetape_end_request(drive, 0, 0);
1569 return ide_stopped;
1570 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571
1572 tape->postponed_rq = NULL;
1573
1574 /*
1575 * If the tape is still busy, postpone our request and service
1576 * the other device meanwhile.
1577 */
Bartlomiej Zolnierkiewiczc47137a2008-02-06 02:57:51 +01001578 stat = ide_read_status(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579
1580 if (!drive->dsc_overlap && !(rq->cmd[0] & REQ_IDETAPE_PC2))
1581 set_bit(IDETAPE_IGNORE_DSC, &tape->flags);
1582
1583 if (drive->post_reset == 1) {
1584 set_bit(IDETAPE_IGNORE_DSC, &tape->flags);
1585 drive->post_reset = 0;
1586 }
1587
Linus Torvalds1da177e2005-04-16 15:20:36 -07001588 if (time_after(jiffies, tape->insert_time))
1589 tape->insert_speed = tape->insert_size / 1024 * HZ / (jiffies - tape->insert_time);
Borislav Petkov37016ba2008-02-06 02:57:52 +01001590 idetape_calculate_speeds(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591 if (!test_and_clear_bit(IDETAPE_IGNORE_DSC, &tape->flags) &&
Bartlomiej Zolnierkiewicz22c525b2008-01-25 22:17:11 +01001592 (stat & SEEK_STAT) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001593 if (postponed_rq == NULL) {
1594 tape->dsc_polling_start = jiffies;
Borislav Petkov54bb2072008-02-06 02:57:52 +01001595 tape->dsc_poll_freq = tape->best_dsc_rw_freq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596 tape->dsc_timeout = jiffies + IDETAPE_DSC_RW_TIMEOUT;
1597 } else if (time_after(jiffies, tape->dsc_timeout)) {
1598 printk(KERN_ERR "ide-tape: %s: DSC timeout\n",
1599 tape->name);
1600 if (rq->cmd[0] & REQ_IDETAPE_PC2) {
1601 idetape_media_access_finished(drive);
1602 return ide_stopped;
1603 } else {
1604 return ide_do_reset(drive);
1605 }
Marcelo Feitoza Parisi9bae1ff2006-03-28 01:56:46 -08001606 } else if (time_after(jiffies, tape->dsc_polling_start + IDETAPE_DSC_MA_THRESHOLD))
Borislav Petkov54bb2072008-02-06 02:57:52 +01001607 tape->dsc_poll_freq = IDETAPE_DSC_MA_SLOW;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608 idetape_postpone_request(drive);
1609 return ide_stopped;
1610 }
1611 if (rq->cmd[0] & REQ_IDETAPE_READ) {
1612 tape->buffer_head++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001613 tape->postpone_cnt = 0;
1614 pc = idetape_next_pc_storage(drive);
1615 idetape_create_read_cmd(tape, pc, rq->current_nr_sectors, (struct idetape_bh *)rq->special);
1616 goto out;
1617 }
1618 if (rq->cmd[0] & REQ_IDETAPE_WRITE) {
1619 tape->buffer_head++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001620 tape->postpone_cnt = 0;
1621 pc = idetape_next_pc_storage(drive);
1622 idetape_create_write_cmd(tape, pc, rq->current_nr_sectors, (struct idetape_bh *)rq->special);
1623 goto out;
1624 }
1625 if (rq->cmd[0] & REQ_IDETAPE_READ_BUFFER) {
1626 tape->postpone_cnt = 0;
1627 pc = idetape_next_pc_storage(drive);
1628 idetape_create_read_buffer_cmd(tape, pc, rq->current_nr_sectors, (struct idetape_bh *)rq->special);
1629 goto out;
1630 }
1631 if (rq->cmd[0] & REQ_IDETAPE_PC1) {
1632 pc = (idetape_pc_t *) rq->buffer;
1633 rq->cmd[0] &= ~(REQ_IDETAPE_PC1);
1634 rq->cmd[0] |= REQ_IDETAPE_PC2;
1635 goto out;
1636 }
1637 if (rq->cmd[0] & REQ_IDETAPE_PC2) {
1638 idetape_media_access_finished(drive);
1639 return ide_stopped;
1640 }
1641 BUG();
1642out:
Borislav Petkov8d06bfa2008-02-06 02:57:53 +01001643 return idetape_issue_pc(drive, pc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644}
1645
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001646/* Pipeline related functions */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001647static inline int idetape_pipeline_active (idetape_tape_t *tape)
1648{
1649 int rc1, rc2;
1650
1651 rc1 = test_bit(IDETAPE_PIPELINE_ACTIVE, &tape->flags);
Borislav Petkov54bb2072008-02-06 02:57:52 +01001652 rc2 = (tape->active_data_rq != NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001653 return rc1;
1654}
1655
1656/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001657 * The function below uses __get_free_page to allocate a pipeline stage, along
1658 * with all the necessary small buffers which together make a buffer of size
1659 * tape->stage_size (or a bit more). We attempt to combine sequential pages as
1660 * much as possible.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001661 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001662 * It returns a pointer to the new allocated stage, or NULL if we can't (or
1663 * don't want to) allocate a stage.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001665 * Pipeline stages are optional and are used to increase performance. If we
1666 * can't allocate them, we'll manage without them.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001667 */
1668static idetape_stage_t *__idetape_kmalloc_stage (idetape_tape_t *tape, int full, int clear)
1669{
1670 idetape_stage_t *stage;
1671 struct idetape_bh *prev_bh, *bh;
1672 int pages = tape->pages_per_stage;
1673 char *b_data = NULL;
1674
Robert P. J. Day5cbded52006-12-13 00:35:56 -08001675 if ((stage = kmalloc(sizeof (idetape_stage_t),GFP_KERNEL)) == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001676 return NULL;
1677 stage->next = NULL;
1678
Robert P. J. Day5cbded52006-12-13 00:35:56 -08001679 bh = stage->bh = kmalloc(sizeof(struct idetape_bh), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001680 if (bh == NULL)
1681 goto abort;
1682 bh->b_reqnext = NULL;
1683 if ((bh->b_data = (char *) __get_free_page (GFP_KERNEL)) == NULL)
1684 goto abort;
1685 if (clear)
1686 memset(bh->b_data, 0, PAGE_SIZE);
1687 bh->b_size = PAGE_SIZE;
1688 atomic_set(&bh->b_count, full ? bh->b_size : 0);
1689
1690 while (--pages) {
1691 if ((b_data = (char *) __get_free_page (GFP_KERNEL)) == NULL)
1692 goto abort;
1693 if (clear)
1694 memset(b_data, 0, PAGE_SIZE);
1695 if (bh->b_data == b_data + PAGE_SIZE) {
1696 bh->b_size += PAGE_SIZE;
1697 bh->b_data -= PAGE_SIZE;
1698 if (full)
1699 atomic_add(PAGE_SIZE, &bh->b_count);
1700 continue;
1701 }
1702 if (b_data == bh->b_data + bh->b_size) {
1703 bh->b_size += PAGE_SIZE;
1704 if (full)
1705 atomic_add(PAGE_SIZE, &bh->b_count);
1706 continue;
1707 }
1708 prev_bh = bh;
Robert P. J. Day5cbded52006-12-13 00:35:56 -08001709 if ((bh = kmalloc(sizeof(struct idetape_bh), GFP_KERNEL)) == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001710 free_page((unsigned long) b_data);
1711 goto abort;
1712 }
1713 bh->b_reqnext = NULL;
1714 bh->b_data = b_data;
1715 bh->b_size = PAGE_SIZE;
1716 atomic_set(&bh->b_count, full ? bh->b_size : 0);
1717 prev_bh->b_reqnext = bh;
1718 }
1719 bh->b_size -= tape->excess_bh_size;
1720 if (full)
1721 atomic_sub(tape->excess_bh_size, &bh->b_count);
1722 return stage;
1723abort:
1724 __idetape_kfree_stage(stage);
1725 return NULL;
1726}
1727
1728static idetape_stage_t *idetape_kmalloc_stage (idetape_tape_t *tape)
1729{
1730 idetape_stage_t *cache_stage = tape->cache_stage;
1731
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001732 debug_log(DBG_PROCS, "Enter %s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001733
1734 if (tape->nr_stages >= tape->max_stages)
1735 return NULL;
1736 if (cache_stage != NULL) {
1737 tape->cache_stage = NULL;
1738 return cache_stage;
1739 }
1740 return __idetape_kmalloc_stage(tape, 0, 0);
1741}
1742
Daniel Walkerdcd96372006-06-25 05:47:37 -07001743static int idetape_copy_stage_from_user (idetape_tape_t *tape, idetape_stage_t *stage, const char __user *buf, int n)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001744{
1745 struct idetape_bh *bh = tape->bh;
1746 int count;
Daniel Walkerdcd96372006-06-25 05:47:37 -07001747 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001748
1749 while (n) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001750 if (bh == NULL) {
1751 printk(KERN_ERR "ide-tape: bh == NULL in "
1752 "idetape_copy_stage_from_user\n");
Daniel Walkerdcd96372006-06-25 05:47:37 -07001753 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001754 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001755 count = min((unsigned int)(bh->b_size - atomic_read(&bh->b_count)), (unsigned int)n);
Daniel Walkerdcd96372006-06-25 05:47:37 -07001756 if (copy_from_user(bh->b_data + atomic_read(&bh->b_count), buf, count))
1757 ret = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001758 n -= count;
1759 atomic_add(count, &bh->b_count);
1760 buf += count;
1761 if (atomic_read(&bh->b_count) == bh->b_size) {
1762 bh = bh->b_reqnext;
1763 if (bh)
1764 atomic_set(&bh->b_count, 0);
1765 }
1766 }
1767 tape->bh = bh;
Daniel Walkerdcd96372006-06-25 05:47:37 -07001768 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001769}
1770
Daniel Walkerdcd96372006-06-25 05:47:37 -07001771static int idetape_copy_stage_to_user (idetape_tape_t *tape, char __user *buf, idetape_stage_t *stage, int n)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001772{
1773 struct idetape_bh *bh = tape->bh;
1774 int count;
Daniel Walkerdcd96372006-06-25 05:47:37 -07001775 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001776
1777 while (n) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001778 if (bh == NULL) {
1779 printk(KERN_ERR "ide-tape: bh == NULL in "
1780 "idetape_copy_stage_to_user\n");
Daniel Walkerdcd96372006-06-25 05:47:37 -07001781 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001782 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001783 count = min(tape->b_count, n);
Daniel Walkerdcd96372006-06-25 05:47:37 -07001784 if (copy_to_user(buf, tape->b_data, count))
1785 ret = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001786 n -= count;
1787 tape->b_data += count;
1788 tape->b_count -= count;
1789 buf += count;
1790 if (!tape->b_count) {
1791 tape->bh = bh = bh->b_reqnext;
1792 if (bh) {
1793 tape->b_data = bh->b_data;
1794 tape->b_count = atomic_read(&bh->b_count);
1795 }
1796 }
1797 }
Daniel Walkerdcd96372006-06-25 05:47:37 -07001798 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001799}
1800
1801static void idetape_init_merge_stage (idetape_tape_t *tape)
1802{
1803 struct idetape_bh *bh = tape->merge_stage->bh;
1804
1805 tape->bh = bh;
Borislav Petkov54abf372008-02-06 02:57:52 +01001806 if (tape->chrdev_dir == IDETAPE_DIR_WRITE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807 atomic_set(&bh->b_count, 0);
1808 else {
1809 tape->b_data = bh->b_data;
1810 tape->b_count = atomic_read(&bh->b_count);
1811 }
1812}
1813
1814static void idetape_switch_buffers (idetape_tape_t *tape, idetape_stage_t *stage)
1815{
1816 struct idetape_bh *tmp;
1817
1818 tmp = stage->bh;
1819 stage->bh = tape->merge_stage->bh;
1820 tape->merge_stage->bh = tmp;
1821 idetape_init_merge_stage(tape);
1822}
1823
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001824/* Add a new stage at the end of the pipeline. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825static void idetape_add_stage_tail (ide_drive_t *drive,idetape_stage_t *stage)
1826{
1827 idetape_tape_t *tape = drive->driver_data;
1828 unsigned long flags;
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001829
1830 debug_log(DBG_PROCS, "Enter %s\n", __func__);
1831
Borislav Petkov54bb2072008-02-06 02:57:52 +01001832 spin_lock_irqsave(&tape->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001833 stage->next = NULL;
1834 if (tape->last_stage != NULL)
1835 tape->last_stage->next=stage;
1836 else
1837 tape->first_stage = tape->next_stage=stage;
1838 tape->last_stage = stage;
1839 if (tape->next_stage == NULL)
1840 tape->next_stage = tape->last_stage;
1841 tape->nr_stages++;
1842 tape->nr_pending_stages++;
Borislav Petkov54bb2072008-02-06 02:57:52 +01001843 spin_unlock_irqrestore(&tape->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001844}
1845
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001846/* Install a completion in a pending request and sleep until it is serviced. The
1847 * caller should ensure that the request will not be serviced before we install
1848 * the completion (usually by disabling interrupts).
Linus Torvalds1da177e2005-04-16 15:20:36 -07001849 */
1850static void idetape_wait_for_request (ide_drive_t *drive, struct request *rq)
1851{
Peter Zijlstra6e9a4732006-09-30 23:28:10 -07001852 DECLARE_COMPLETION_ONSTACK(wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001853 idetape_tape_t *tape = drive->driver_data;
1854
Jens Axboe4aff5e22006-08-10 08:44:47 +02001855 if (rq == NULL || !blk_special_request(rq)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001856 printk (KERN_ERR "ide-tape: bug: Trying to sleep on non-valid request\n");
1857 return;
1858 }
Jens Axboec00895a2006-09-30 20:29:12 +02001859 rq->end_io_data = &wait;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001860 rq->end_io = blk_end_sync_rq;
Borislav Petkov54bb2072008-02-06 02:57:52 +01001861 spin_unlock_irq(&tape->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001862 wait_for_completion(&wait);
1863 /* The stage and its struct request have been deallocated */
Borislav Petkov54bb2072008-02-06 02:57:52 +01001864 spin_lock_irq(&tape->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001865}
1866
Borislav Petkova2f5b7f2008-02-06 02:57:51 +01001867static ide_startstop_t idetape_read_position_callback(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001868{
1869 idetape_tape_t *tape = drive->driver_data;
Borislav Petkova2f5b7f2008-02-06 02:57:51 +01001870 u8 *readpos = tape->pc->buffer;
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001871
1872 debug_log(DBG_PROCS, "Enter %s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001873
1874 if (!tape->pc->error) {
Borislav Petkova2f5b7f2008-02-06 02:57:51 +01001875 debug_log(DBG_SENSE, "BOP - %s\n",
1876 (readpos[0] & 0x80) ? "Yes" : "No");
1877 debug_log(DBG_SENSE, "EOP - %s\n",
1878 (readpos[0] & 0x40) ? "Yes" : "No");
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001879
Borislav Petkova2f5b7f2008-02-06 02:57:51 +01001880 if (readpos[0] & 0x4) {
1881 printk(KERN_INFO "ide-tape: Block location is unknown"
1882 "to the tape\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001883 clear_bit(IDETAPE_ADDRESS_VALID, &tape->flags);
1884 idetape_end_request(drive, 0, 0);
1885 } else {
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001886 debug_log(DBG_SENSE, "Block Location - %u\n",
Borislav Petkova2f5b7f2008-02-06 02:57:51 +01001887 be32_to_cpu(*(u32 *)&readpos[4]));
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001888
Borislav Petkova2f5b7f2008-02-06 02:57:51 +01001889 tape->partition = readpos[1];
Borislav Petkov54bb2072008-02-06 02:57:52 +01001890 tape->first_frame =
Borislav Petkova2f5b7f2008-02-06 02:57:51 +01001891 be32_to_cpu(*(u32 *)&readpos[4]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001892 set_bit(IDETAPE_ADDRESS_VALID, &tape->flags);
1893 idetape_end_request(drive, 1, 0);
1894 }
1895 } else {
1896 idetape_end_request(drive, 0, 0);
1897 }
1898 return ide_stopped;
1899}
1900
1901/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001902 * Write a filemark if write_filemark=1. Flush the device buffers without
1903 * writing a filemark otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001904 */
1905static void idetape_create_write_filemark_cmd (ide_drive_t *drive, idetape_pc_t *pc,int write_filemark)
1906{
1907 idetape_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +01001908 pc->c[0] = WRITE_FILEMARKS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001909 pc->c[4] = write_filemark;
1910 set_bit(PC_WAIT_FOR_DSC, &pc->flags);
1911 pc->callback = &idetape_pc_callback;
1912}
1913
1914static void idetape_create_test_unit_ready_cmd(idetape_pc_t *pc)
1915{
1916 idetape_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +01001917 pc->c[0] = TEST_UNIT_READY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001918 pc->callback = &idetape_pc_callback;
1919}
1920
1921/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001922 * We add a special packet command request to the tail of the request queue, and
1923 * wait for it to be serviced. This is not to be called from within the request
1924 * handling part of the driver! We allocate here data on the stack and it is
1925 * valid until the request is finished. This is not the case for the bottom part
1926 * of the driver, where we are always leaving the functions to wait for an
1927 * interrupt or a timer event.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001928 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001929 * From the bottom part of the driver, we should allocate safe memory using
1930 * idetape_next_pc_storage() and ide_tape_next_rq_storage(), and add the request
1931 * to the request list without waiting for it to be serviced! In that case, we
1932 * usually use idetape_queue_pc_head().
Linus Torvalds1da177e2005-04-16 15:20:36 -07001933 */
1934static int __idetape_queue_pc_tail (ide_drive_t *drive, idetape_pc_t *pc)
1935{
1936 struct ide_tape_obj *tape = drive->driver_data;
1937 struct request rq;
1938
1939 idetape_init_rq(&rq, REQ_IDETAPE_PC1);
1940 rq.buffer = (char *) pc;
1941 rq.rq_disk = tape->disk;
1942 return ide_do_drive_cmd(drive, &rq, ide_wait);
1943}
1944
1945static void idetape_create_load_unload_cmd (ide_drive_t *drive, idetape_pc_t *pc,int cmd)
1946{
1947 idetape_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +01001948 pc->c[0] = START_STOP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001949 pc->c[4] = cmd;
1950 set_bit(PC_WAIT_FOR_DSC, &pc->flags);
1951 pc->callback = &idetape_pc_callback;
1952}
1953
1954static int idetape_wait_ready(ide_drive_t *drive, unsigned long timeout)
1955{
1956 idetape_tape_t *tape = drive->driver_data;
1957 idetape_pc_t pc;
1958 int load_attempted = 0;
1959
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001960 /* Wait for the tape to become ready */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001961 set_bit(IDETAPE_MEDIUM_PRESENT, &tape->flags);
1962 timeout += jiffies;
1963 while (time_before(jiffies, timeout)) {
1964 idetape_create_test_unit_ready_cmd(&pc);
1965 if (!__idetape_queue_pc_tail(drive, &pc))
1966 return 0;
1967 if ((tape->sense_key == 2 && tape->asc == 4 && tape->ascq == 2)
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001968 || (tape->asc == 0x3A)) {
1969 /* no media */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001970 if (load_attempted)
1971 return -ENOMEDIUM;
1972 idetape_create_load_unload_cmd(drive, &pc, IDETAPE_LU_LOAD_MASK);
1973 __idetape_queue_pc_tail(drive, &pc);
1974 load_attempted = 1;
1975 /* not about to be ready */
1976 } else if (!(tape->sense_key == 2 && tape->asc == 4 &&
1977 (tape->ascq == 1 || tape->ascq == 8)))
1978 return -EIO;
Nishanth Aravamudan80ce45f2005-09-10 00:27:08 -07001979 msleep(100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001980 }
1981 return -EIO;
1982}
1983
1984static int idetape_queue_pc_tail (ide_drive_t *drive,idetape_pc_t *pc)
1985{
1986 return __idetape_queue_pc_tail(drive, pc);
1987}
1988
1989static int idetape_flush_tape_buffers (ide_drive_t *drive)
1990{
1991 idetape_pc_t pc;
1992 int rc;
1993
1994 idetape_create_write_filemark_cmd(drive, &pc, 0);
1995 if ((rc = idetape_queue_pc_tail(drive, &pc)))
1996 return rc;
1997 idetape_wait_ready(drive, 60 * 5 * HZ);
1998 return 0;
1999}
2000
2001static void idetape_create_read_position_cmd (idetape_pc_t *pc)
2002{
2003 idetape_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +01002004 pc->c[0] = READ_POSITION;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002005 pc->request_transfer = 20;
2006 pc->callback = &idetape_read_position_callback;
2007}
2008
2009static int idetape_read_position (ide_drive_t *drive)
2010{
2011 idetape_tape_t *tape = drive->driver_data;
2012 idetape_pc_t pc;
2013 int position;
2014
Borislav Petkov8004a8c2008-02-06 02:57:51 +01002015 debug_log(DBG_PROCS, "Enter %s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002016
2017 idetape_create_read_position_cmd(&pc);
2018 if (idetape_queue_pc_tail(drive, &pc))
2019 return -1;
Borislav Petkov54bb2072008-02-06 02:57:52 +01002020 position = tape->first_frame;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002021 return position;
2022}
2023
2024static void idetape_create_locate_cmd (ide_drive_t *drive, idetape_pc_t *pc, unsigned int block, u8 partition, int skip)
2025{
2026 idetape_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +01002027 pc->c[0] = POSITION_TO_ELEMENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002028 pc->c[1] = 2;
Borislav Petkov860ff5e2008-02-02 19:56:50 +01002029 put_unaligned(cpu_to_be32(block), (unsigned int *) &pc->c[3]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002030 pc->c[8] = partition;
2031 set_bit(PC_WAIT_FOR_DSC, &pc->flags);
2032 pc->callback = &idetape_pc_callback;
2033}
2034
2035static int idetape_create_prevent_cmd (ide_drive_t *drive, idetape_pc_t *pc, int prevent)
2036{
2037 idetape_tape_t *tape = drive->driver_data;
2038
Borislav Petkovb6422012008-02-02 19:56:49 +01002039 /* device supports locking according to capabilities page */
2040 if (!(tape->caps[6] & 0x01))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002041 return 0;
2042
2043 idetape_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +01002044 pc->c[0] = ALLOW_MEDIUM_REMOVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002045 pc->c[4] = prevent;
2046 pc->callback = &idetape_pc_callback;
2047 return 1;
2048}
2049
2050static int __idetape_discard_read_pipeline (ide_drive_t *drive)
2051{
2052 idetape_tape_t *tape = drive->driver_data;
2053 unsigned long flags;
2054 int cnt;
2055
Borislav Petkov54abf372008-02-06 02:57:52 +01002056 if (tape->chrdev_dir != IDETAPE_DIR_READ)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002057 return 0;
2058
2059 /* Remove merge stage. */
Borislav Petkov54bb2072008-02-06 02:57:52 +01002060 cnt = tape->merge_stage_size / tape->blk_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002061 if (test_and_clear_bit(IDETAPE_FILEMARK, &tape->flags))
2062 ++cnt; /* Filemarks count as 1 sector */
2063 tape->merge_stage_size = 0;
2064 if (tape->merge_stage != NULL) {
2065 __idetape_kfree_stage(tape->merge_stage);
2066 tape->merge_stage = NULL;
2067 }
2068
2069 /* Clear pipeline flags. */
2070 clear_bit(IDETAPE_PIPELINE_ERROR, &tape->flags);
Borislav Petkov54abf372008-02-06 02:57:52 +01002071 tape->chrdev_dir = IDETAPE_DIR_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002072
2073 /* Remove pipeline stages. */
2074 if (tape->first_stage == NULL)
2075 return 0;
2076
Borislav Petkov54bb2072008-02-06 02:57:52 +01002077 spin_lock_irqsave(&tape->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002078 tape->next_stage = NULL;
2079 if (idetape_pipeline_active(tape))
Borislav Petkov54bb2072008-02-06 02:57:52 +01002080 idetape_wait_for_request(drive, tape->active_data_rq);
2081 spin_unlock_irqrestore(&tape->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002082
2083 while (tape->first_stage != NULL) {
2084 struct request *rq_ptr = &tape->first_stage->rq;
2085
2086 cnt += rq_ptr->nr_sectors - rq_ptr->current_nr_sectors;
2087 if (rq_ptr->errors == IDETAPE_ERROR_FILEMARK)
2088 ++cnt;
2089 idetape_remove_stage_head(drive);
2090 }
2091 tape->nr_pending_stages = 0;
2092 tape->max_stages = tape->min_pipeline;
2093 return cnt;
2094}
2095
2096/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +01002097 * Position the tape to the requested block using the LOCATE packet command.
2098 * A READ POSITION command is then issued to check where we are positioned. Like
2099 * all higher level operations, we queue the commands at the tail of the request
2100 * queue and wait for their completion.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002101 */
2102static int idetape_position_tape (ide_drive_t *drive, unsigned int block, u8 partition, int skip)
2103{
2104 idetape_tape_t *tape = drive->driver_data;
2105 int retval;
2106 idetape_pc_t pc;
2107
Borislav Petkov54abf372008-02-06 02:57:52 +01002108 if (tape->chrdev_dir == IDETAPE_DIR_READ)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002109 __idetape_discard_read_pipeline(drive);
2110 idetape_wait_ready(drive, 60 * 5 * HZ);
2111 idetape_create_locate_cmd(drive, &pc, block, partition, skip);
2112 retval = idetape_queue_pc_tail(drive, &pc);
2113 if (retval)
2114 return (retval);
2115
2116 idetape_create_read_position_cmd(&pc);
2117 return (idetape_queue_pc_tail(drive, &pc));
2118}
2119
2120static void idetape_discard_read_pipeline (ide_drive_t *drive, int restore_position)
2121{
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)) {
2131 printk(KERN_INFO "ide-tape: %s: position_tape failed in discard_pipeline()\n", tape->name);
2132 return;
2133 }
2134 }
2135}
2136
2137/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +01002138 * Generate a read/write request for the block device interface and wait for it
2139 * to be serviced.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002140 */
2141static int idetape_queue_rw_tail(ide_drive_t *drive, int cmd, int blocks, struct idetape_bh *bh)
2142{
2143 idetape_tape_t *tape = drive->driver_data;
2144 struct request rq;
2145
Borislav Petkov8004a8c2008-02-06 02:57:51 +01002146 debug_log(DBG_SENSE, "%s: cmd=%d\n", __func__, cmd);
2147
Linus Torvalds1da177e2005-04-16 15:20:36 -07002148 if (idetape_pipeline_active(tape)) {
Borislav Petkov8004a8c2008-02-06 02:57:51 +01002149 printk(KERN_ERR "ide-tape: bug: the pipeline is active in %s\n",
2150 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002151 return (0);
2152 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002153
2154 idetape_init_rq(&rq, cmd);
2155 rq.rq_disk = tape->disk;
2156 rq.special = (void *)bh;
Borislav Petkov54bb2072008-02-06 02:57:52 +01002157 rq.sector = tape->first_frame;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002158 rq.nr_sectors = rq.current_nr_sectors = blocks;
2159 (void) ide_do_drive_cmd(drive, &rq, ide_wait);
2160
2161 if ((cmd & (REQ_IDETAPE_READ | REQ_IDETAPE_WRITE)) == 0)
2162 return 0;
2163
2164 if (tape->merge_stage)
2165 idetape_init_merge_stage(tape);
2166 if (rq.errors == IDETAPE_ERROR_GENERAL)
2167 return -EIO;
Borislav Petkov54bb2072008-02-06 02:57:52 +01002168 return (tape->blk_size * (blocks-rq.current_nr_sectors));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002169}
2170
Borislav Petkov8d06bfa2008-02-06 02:57:53 +01002171/* start servicing the pipeline stages, starting from tape->next_stage. */
2172static void idetape_plug_pipeline(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002173{
2174 idetape_tape_t *tape = drive->driver_data;
2175
2176 if (tape->next_stage == NULL)
2177 return;
2178 if (!idetape_pipeline_active(tape)) {
2179 set_bit(IDETAPE_PIPELINE_ACTIVE, &tape->flags);
Borislav Petkov419d4742008-02-02 19:56:51 +01002180 idetape_activate_next_stage(drive);
Borislav Petkov54bb2072008-02-06 02:57:52 +01002181 (void) ide_do_drive_cmd(drive, tape->active_data_rq, ide_end);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002182 }
2183}
2184
2185static void idetape_create_inquiry_cmd (idetape_pc_t *pc)
2186{
2187 idetape_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +01002188 pc->c[0] = INQUIRY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002189 pc->c[4] = pc->request_transfer = 254;
2190 pc->callback = &idetape_pc_callback;
2191}
2192
2193static void idetape_create_rewind_cmd (ide_drive_t *drive, idetape_pc_t *pc)
2194{
2195 idetape_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +01002196 pc->c[0] = REZERO_UNIT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002197 set_bit(PC_WAIT_FOR_DSC, &pc->flags);
2198 pc->callback = &idetape_pc_callback;
2199}
2200
Linus Torvalds1da177e2005-04-16 15:20:36 -07002201static void idetape_create_erase_cmd (idetape_pc_t *pc)
2202{
2203 idetape_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +01002204 pc->c[0] = ERASE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002205 pc->c[1] = 1;
2206 set_bit(PC_WAIT_FOR_DSC, &pc->flags);
2207 pc->callback = &idetape_pc_callback;
2208}
2209
2210static void idetape_create_space_cmd (idetape_pc_t *pc,int count, u8 cmd)
2211{
2212 idetape_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +01002213 pc->c[0] = SPACE;
Borislav Petkov860ff5e2008-02-02 19:56:50 +01002214 put_unaligned(cpu_to_be32(count), (unsigned int *) &pc->c[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002215 pc->c[1] = cmd;
2216 set_bit(PC_WAIT_FOR_DSC, &pc->flags);
2217 pc->callback = &idetape_pc_callback;
2218}
2219
2220static void idetape_wait_first_stage (ide_drive_t *drive)
2221{
2222 idetape_tape_t *tape = drive->driver_data;
2223 unsigned long flags;
2224
2225 if (tape->first_stage == NULL)
2226 return;
Borislav Petkov54bb2072008-02-06 02:57:52 +01002227 spin_lock_irqsave(&tape->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002228 if (tape->active_stage == tape->first_stage)
Borislav Petkov54bb2072008-02-06 02:57:52 +01002229 idetape_wait_for_request(drive, tape->active_data_rq);
2230 spin_unlock_irqrestore(&tape->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002231}
2232
2233/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +01002234 * Try to add a character device originated write request to our pipeline. In
2235 * case we don't succeed, we revert to non-pipelined operation mode for this
2236 * request. In order to accomplish that, we
Linus Torvalds1da177e2005-04-16 15:20:36 -07002237 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +01002238 * 1. Try to allocate a new pipeline stage.
2239 * 2. If we can't, wait for more and more requests to be serviced and try again
2240 * each time.
2241 * 3. If we still can't allocate a stage, fallback to non-pipelined operation
2242 * mode for this request.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002243 */
2244static int idetape_add_chrdev_write_request (ide_drive_t *drive, int blocks)
2245{
2246 idetape_tape_t *tape = drive->driver_data;
2247 idetape_stage_t *new_stage;
2248 unsigned long flags;
2249 struct request *rq;
2250
Borislav Petkov8004a8c2008-02-06 02:57:51 +01002251 debug_log(DBG_CHRDEV, "Enter %s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002252
Borislav Petkov3c98bf32008-02-06 02:57:53 +01002253 /* Attempt to allocate a new stage. Beware possible race conditions. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002254 while ((new_stage = idetape_kmalloc_stage(tape)) == NULL) {
Borislav Petkov54bb2072008-02-06 02:57:52 +01002255 spin_lock_irqsave(&tape->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002256 if (idetape_pipeline_active(tape)) {
Borislav Petkov54bb2072008-02-06 02:57:52 +01002257 idetape_wait_for_request(drive, tape->active_data_rq);
2258 spin_unlock_irqrestore(&tape->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002259 } else {
Borislav Petkov54bb2072008-02-06 02:57:52 +01002260 spin_unlock_irqrestore(&tape->lock, flags);
Borislav Petkov8d06bfa2008-02-06 02:57:53 +01002261 idetape_plug_pipeline(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002262 if (idetape_pipeline_active(tape))
2263 continue;
2264 /*
Borislav Petkov3c98bf32008-02-06 02:57:53 +01002265 * The machine is short on memory. Fallback to non-
2266 * pipelined operation mode for this request.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002267 */
2268 return idetape_queue_rw_tail(drive, REQ_IDETAPE_WRITE, blocks, tape->merge_stage->bh);
2269 }
2270 }
2271 rq = &new_stage->rq;
2272 idetape_init_rq(rq, REQ_IDETAPE_WRITE);
2273 /* Doesn't actually matter - We always assume sequential access */
Borislav Petkov54bb2072008-02-06 02:57:52 +01002274 rq->sector = tape->first_frame;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002275 rq->nr_sectors = rq->current_nr_sectors = blocks;
2276
2277 idetape_switch_buffers(tape, new_stage);
2278 idetape_add_stage_tail(drive, new_stage);
2279 tape->pipeline_head++;
Borislav Petkov37016ba2008-02-06 02:57:52 +01002280 idetape_calculate_speeds(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002281
2282 /*
Borislav Petkov3c98bf32008-02-06 02:57:53 +01002283 * Estimate whether the tape has stopped writing by checking if our
2284 * write pipeline is currently empty. If we are not writing anymore,
2285 * wait for the pipeline to be almost completely full (90%) before
2286 * starting to service requests, so that we will be able to keep up with
2287 * the higher speeds of the tape.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002288 */
2289 if (!idetape_pipeline_active(tape)) {
2290 if (tape->nr_stages >= tape->max_stages * 9 / 10 ||
Borislav Petkov54bb2072008-02-06 02:57:52 +01002291 tape->nr_stages >= tape->max_stages -
2292 tape->uncontrolled_pipeline_head_speed * 3 * 1024 /
2293 tape->blk_size) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002294 tape->measure_insert_time = 1;
2295 tape->insert_time = jiffies;
2296 tape->insert_size = 0;
2297 tape->insert_speed = 0;
Borislav Petkov8d06bfa2008-02-06 02:57:53 +01002298 idetape_plug_pipeline(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002299 }
2300 }
2301 if (test_and_clear_bit(IDETAPE_PIPELINE_ERROR, &tape->flags))
2302 /* Return a deferred error */
2303 return -EIO;
2304 return blocks;
2305}
2306
2307/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +01002308 * Wait until all pending pipeline requests are serviced. Typically called on
2309 * device close.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002310 */
2311static void idetape_wait_for_pipeline (ide_drive_t *drive)
2312{
2313 idetape_tape_t *tape = drive->driver_data;
2314 unsigned long flags;
2315
2316 while (tape->next_stage || idetape_pipeline_active(tape)) {
Borislav Petkov8d06bfa2008-02-06 02:57:53 +01002317 idetape_plug_pipeline(drive);
Borislav Petkov54bb2072008-02-06 02:57:52 +01002318 spin_lock_irqsave(&tape->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002319 if (idetape_pipeline_active(tape))
Borislav Petkov54bb2072008-02-06 02:57:52 +01002320 idetape_wait_for_request(drive, tape->active_data_rq);
2321 spin_unlock_irqrestore(&tape->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002322 }
2323}
2324
2325static void idetape_empty_write_pipeline (ide_drive_t *drive)
2326{
2327 idetape_tape_t *tape = drive->driver_data;
2328 int blocks, min;
2329 struct idetape_bh *bh;
Borislav Petkov55a5d292008-02-02 19:56:49 +01002330
Borislav Petkov54abf372008-02-06 02:57:52 +01002331 if (tape->chrdev_dir != IDETAPE_DIR_WRITE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002332 printk(KERN_ERR "ide-tape: bug: Trying to empty write pipeline, but we are not writing.\n");
2333 return;
2334 }
2335 if (tape->merge_stage_size > tape->stage_size) {
2336 printk(KERN_ERR "ide-tape: bug: merge_buffer too big\n");
2337 tape->merge_stage_size = tape->stage_size;
2338 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002339 if (tape->merge_stage_size) {
Borislav Petkov54bb2072008-02-06 02:57:52 +01002340 blocks = tape->merge_stage_size / tape->blk_size;
2341 if (tape->merge_stage_size % tape->blk_size) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002342 unsigned int i;
2343
2344 blocks++;
Borislav Petkov54bb2072008-02-06 02:57:52 +01002345 i = tape->blk_size - tape->merge_stage_size %
2346 tape->blk_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002347 bh = tape->bh->b_reqnext;
2348 while (bh) {
2349 atomic_set(&bh->b_count, 0);
2350 bh = bh->b_reqnext;
2351 }
2352 bh = tape->bh;
2353 while (i) {
2354 if (bh == NULL) {
2355
2356 printk(KERN_INFO "ide-tape: bug, bh NULL\n");
2357 break;
2358 }
2359 min = min(i, (unsigned int)(bh->b_size - atomic_read(&bh->b_count)));
2360 memset(bh->b_data + atomic_read(&bh->b_count), 0, min);
2361 atomic_add(min, &bh->b_count);
2362 i -= min;
2363 bh = bh->b_reqnext;
2364 }
2365 }
2366 (void) idetape_add_chrdev_write_request(drive, blocks);
2367 tape->merge_stage_size = 0;
2368 }
2369 idetape_wait_for_pipeline(drive);
2370 if (tape->merge_stage != NULL) {
2371 __idetape_kfree_stage(tape->merge_stage);
2372 tape->merge_stage = NULL;
2373 }
2374 clear_bit(IDETAPE_PIPELINE_ERROR, &tape->flags);
Borislav Petkov54abf372008-02-06 02:57:52 +01002375 tape->chrdev_dir = IDETAPE_DIR_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002376
2377 /*
Borislav Petkov3c98bf32008-02-06 02:57:53 +01002378 * On the next backup, perform the feedback loop again. (I don't want to
2379 * keep sense information between backups, as some systems are
2380 * constantly on, and the system load can be totally different on the
2381 * next backup).
Linus Torvalds1da177e2005-04-16 15:20:36 -07002382 */
2383 tape->max_stages = tape->min_pipeline;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002384 if (tape->first_stage != NULL ||
2385 tape->next_stage != NULL ||
2386 tape->last_stage != NULL ||
2387 tape->nr_stages != 0) {
2388 printk(KERN_ERR "ide-tape: ide-tape pipeline bug, "
2389 "first_stage %p, next_stage %p, "
2390 "last_stage %p, nr_stages %d\n",
2391 tape->first_stage, tape->next_stage,
2392 tape->last_stage, tape->nr_stages);
2393 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002394}
2395
2396static void idetape_restart_speed_control (ide_drive_t *drive)
2397{
2398 idetape_tape_t *tape = drive->driver_data;
2399
2400 tape->restart_speed_control_req = 0;
2401 tape->pipeline_head = 0;
Borislav Petkov41f81d542008-02-06 02:57:52 +01002402 tape->controlled_last_pipeline_head = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002403 tape->controlled_previous_pipeline_head = tape->uncontrolled_previous_pipeline_head = 0;
2404 tape->pipeline_head_speed = tape->controlled_pipeline_head_speed = 5000;
2405 tape->uncontrolled_pipeline_head_speed = 0;
2406 tape->controlled_pipeline_head_time = tape->uncontrolled_pipeline_head_time = jiffies;
2407 tape->controlled_previous_head_time = tape->uncontrolled_previous_head_time = jiffies;
2408}
2409
Borislav Petkov8d06bfa2008-02-06 02:57:53 +01002410static int idetape_init_read(ide_drive_t *drive, int max_stages)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002411{
2412 idetape_tape_t *tape = drive->driver_data;
2413 idetape_stage_t *new_stage;
2414 struct request rq;
2415 int bytes_read;
Borislav Petkovb6422012008-02-02 19:56:49 +01002416 u16 blocks = *(u16 *)&tape->caps[12];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002417
2418 /* Initialize read operation */
Borislav Petkov54abf372008-02-06 02:57:52 +01002419 if (tape->chrdev_dir != IDETAPE_DIR_READ) {
2420 if (tape->chrdev_dir == IDETAPE_DIR_WRITE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002421 idetape_empty_write_pipeline(drive);
2422 idetape_flush_tape_buffers(drive);
2423 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002424 if (tape->merge_stage || tape->merge_stage_size) {
2425 printk (KERN_ERR "ide-tape: merge_stage_size should be 0 now\n");
2426 tape->merge_stage_size = 0;
2427 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002428 if ((tape->merge_stage = __idetape_kmalloc_stage(tape, 0, 0)) == NULL)
2429 return -ENOMEM;
Borislav Petkov54abf372008-02-06 02:57:52 +01002430 tape->chrdev_dir = IDETAPE_DIR_READ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002431
2432 /*
Borislav Petkov3c98bf32008-02-06 02:57:53 +01002433 * Issue a read 0 command to ensure that DSC handshake is
2434 * switched from completion mode to buffer available mode.
2435 * No point in issuing this if DSC overlap isn't supported, some
2436 * drives (Seagate STT3401A) will return an error.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002437 */
2438 if (drive->dsc_overlap) {
2439 bytes_read = idetape_queue_rw_tail(drive, REQ_IDETAPE_READ, 0, tape->merge_stage->bh);
2440 if (bytes_read < 0) {
2441 __idetape_kfree_stage(tape->merge_stage);
2442 tape->merge_stage = NULL;
Borislav Petkov54abf372008-02-06 02:57:52 +01002443 tape->chrdev_dir = IDETAPE_DIR_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002444 return bytes_read;
2445 }
2446 }
2447 }
2448 if (tape->restart_speed_control_req)
2449 idetape_restart_speed_control(drive);
2450 idetape_init_rq(&rq, REQ_IDETAPE_READ);
Borislav Petkov54bb2072008-02-06 02:57:52 +01002451 rq.sector = tape->first_frame;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002452 rq.nr_sectors = rq.current_nr_sectors = blocks;
2453 if (!test_bit(IDETAPE_PIPELINE_ERROR, &tape->flags) &&
2454 tape->nr_stages < max_stages) {
2455 new_stage = idetape_kmalloc_stage(tape);
2456 while (new_stage != NULL) {
2457 new_stage->rq = rq;
2458 idetape_add_stage_tail(drive, new_stage);
2459 if (tape->nr_stages >= max_stages)
2460 break;
2461 new_stage = idetape_kmalloc_stage(tape);
2462 }
2463 }
2464 if (!idetape_pipeline_active(tape)) {
2465 if (tape->nr_pending_stages >= 3 * max_stages / 4) {
2466 tape->measure_insert_time = 1;
2467 tape->insert_time = jiffies;
2468 tape->insert_size = 0;
2469 tape->insert_speed = 0;
Borislav Petkov8d06bfa2008-02-06 02:57:53 +01002470 idetape_plug_pipeline(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002471 }
2472 }
2473 return 0;
2474}
2475
2476/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +01002477 * Called from idetape_chrdev_read() to service a character device read request
2478 * and add read-ahead requests to our pipeline.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002479 */
2480static int idetape_add_chrdev_read_request (ide_drive_t *drive,int blocks)
2481{
2482 idetape_tape_t *tape = drive->driver_data;
2483 unsigned long flags;
2484 struct request *rq_ptr;
2485 int bytes_read;
2486
Borislav Petkov8004a8c2008-02-06 02:57:51 +01002487 debug_log(DBG_PROCS, "Enter %s, %d blocks\n", __func__, blocks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002488
Borislav Petkov3c98bf32008-02-06 02:57:53 +01002489 /* If we are at a filemark, return a read length of 0 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002490 if (test_bit(IDETAPE_FILEMARK, &tape->flags))
2491 return 0;
2492
Borislav Petkov3c98bf32008-02-06 02:57:53 +01002493 /* Wait for the next block to reach the head of the pipeline. */
Borislav Petkov8d06bfa2008-02-06 02:57:53 +01002494 idetape_init_read(drive, tape->max_stages);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002495 if (tape->first_stage == NULL) {
2496 if (test_bit(IDETAPE_PIPELINE_ERROR, &tape->flags))
2497 return 0;
Borislav Petkov54bb2072008-02-06 02:57:52 +01002498 return idetape_queue_rw_tail(drive, REQ_IDETAPE_READ, blocks,
2499 tape->merge_stage->bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002500 }
2501 idetape_wait_first_stage(drive);
2502 rq_ptr = &tape->first_stage->rq;
Borislav Petkov54bb2072008-02-06 02:57:52 +01002503 bytes_read = tape->blk_size * (rq_ptr->nr_sectors -
2504 rq_ptr->current_nr_sectors);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002505 rq_ptr->nr_sectors = rq_ptr->current_nr_sectors = 0;
2506
2507
2508 if (rq_ptr->errors == IDETAPE_ERROR_EOD)
2509 return 0;
2510 else {
2511 idetape_switch_buffers(tape, tape->first_stage);
2512 if (rq_ptr->errors == IDETAPE_ERROR_FILEMARK)
2513 set_bit(IDETAPE_FILEMARK, &tape->flags);
Borislav Petkov54bb2072008-02-06 02:57:52 +01002514 spin_lock_irqsave(&tape->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002515 idetape_remove_stage_head(drive);
Borislav Petkov54bb2072008-02-06 02:57:52 +01002516 spin_unlock_irqrestore(&tape->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002517 tape->pipeline_head++;
Borislav Petkov37016ba2008-02-06 02:57:52 +01002518 idetape_calculate_speeds(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002519 }
Borislav Petkov54bb2072008-02-06 02:57:52 +01002520 if (bytes_read > blocks * tape->blk_size) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002521 printk(KERN_ERR "ide-tape: bug: trying to return more bytes than requested\n");
Borislav Petkov54bb2072008-02-06 02:57:52 +01002522 bytes_read = blocks * tape->blk_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002523 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002524 return (bytes_read);
2525}
2526
2527static void idetape_pad_zeros (ide_drive_t *drive, int bcount)
2528{
2529 idetape_tape_t *tape = drive->driver_data;
2530 struct idetape_bh *bh;
2531 int blocks;
Borislav Petkov3c98bf32008-02-06 02:57:53 +01002532
Linus Torvalds1da177e2005-04-16 15:20:36 -07002533 while (bcount) {
2534 unsigned int count;
2535
2536 bh = tape->merge_stage->bh;
2537 count = min(tape->stage_size, bcount);
2538 bcount -= count;
Borislav Petkov54bb2072008-02-06 02:57:52 +01002539 blocks = count / tape->blk_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002540 while (count) {
2541 atomic_set(&bh->b_count, min(count, (unsigned int)bh->b_size));
2542 memset(bh->b_data, 0, atomic_read(&bh->b_count));
2543 count -= atomic_read(&bh->b_count);
2544 bh = bh->b_reqnext;
2545 }
2546 idetape_queue_rw_tail(drive, REQ_IDETAPE_WRITE, blocks, tape->merge_stage->bh);
2547 }
2548}
2549
2550static int idetape_pipeline_size (ide_drive_t *drive)
2551{
2552 idetape_tape_t *tape = drive->driver_data;
2553 idetape_stage_t *stage;
2554 struct request *rq;
2555 int size = 0;
2556
2557 idetape_wait_for_pipeline(drive);
2558 stage = tape->first_stage;
2559 while (stage != NULL) {
2560 rq = &stage->rq;
Borislav Petkov54bb2072008-02-06 02:57:52 +01002561 size += tape->blk_size * (rq->nr_sectors -
2562 rq->current_nr_sectors);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002563 if (rq->errors == IDETAPE_ERROR_FILEMARK)
Borislav Petkov54bb2072008-02-06 02:57:52 +01002564 size += tape->blk_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002565 stage = stage->next;
2566 }
2567 size += tape->merge_stage_size;
2568 return size;
2569}
2570
2571/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +01002572 * Rewinds the tape to the Beginning Of the current Partition (BOP). We
2573 * currently support only one partition.
2574 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002575static int idetape_rewind_tape (ide_drive_t *drive)
2576{
2577 int retval;
2578 idetape_pc_t pc;
Borislav Petkov8004a8c2008-02-06 02:57:51 +01002579 idetape_tape_t *tape;
2580 tape = drive->driver_data;
2581
2582 debug_log(DBG_SENSE, "Enter %s\n", __func__);
2583
Linus Torvalds1da177e2005-04-16 15:20:36 -07002584 idetape_create_rewind_cmd(drive, &pc);
2585 retval = idetape_queue_pc_tail(drive, &pc);
2586 if (retval)
2587 return retval;
2588
2589 idetape_create_read_position_cmd(&pc);
2590 retval = idetape_queue_pc_tail(drive, &pc);
2591 if (retval)
2592 return retval;
2593 return 0;
2594}
2595
Borislav Petkov3c98bf32008-02-06 02:57:53 +01002596/* mtio.h compatible commands should be issued to the chrdev interface. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002597static int idetape_blkdev_ioctl(ide_drive_t *drive, unsigned int cmd, unsigned long arg)
2598{
2599 idetape_tape_t *tape = drive->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002600 void __user *argp = (void __user *)arg;
2601
Borislav Petkovd59823f2008-02-02 19:56:51 +01002602 struct idetape_config {
2603 int dsc_rw_frequency;
2604 int dsc_media_access_frequency;
2605 int nr_stages;
2606 } config;
2607
Borislav Petkov8004a8c2008-02-06 02:57:51 +01002608 debug_log(DBG_PROCS, "Enter %s\n", __func__);
2609
Linus Torvalds1da177e2005-04-16 15:20:36 -07002610 switch (cmd) {
2611 case 0x0340:
Borislav Petkovd59823f2008-02-02 19:56:51 +01002612 if (copy_from_user(&config, argp, sizeof(config)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002613 return -EFAULT;
Borislav Petkov54bb2072008-02-06 02:57:52 +01002614 tape->best_dsc_rw_freq = config.dsc_rw_frequency;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002615 tape->max_stages = config.nr_stages;
2616 break;
2617 case 0x0350:
Borislav Petkov54bb2072008-02-06 02:57:52 +01002618 config.dsc_rw_frequency = (int) tape->best_dsc_rw_freq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002619 config.nr_stages = tape->max_stages;
Borislav Petkovd59823f2008-02-02 19:56:51 +01002620 if (copy_to_user(argp, &config, sizeof(config)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002621 return -EFAULT;
2622 break;
2623 default:
2624 return -EIO;
2625 }
2626 return 0;
2627}
2628
2629/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +01002630 * The function below is now a bit more complicated than just passing the
2631 * command to the tape since we may have crossed some filemarks during our
2632 * pipelined read-ahead mode. As a minor side effect, the pipeline enables us to
2633 * support MTFSFM when the filemark is in our internal pipeline even if the tape
2634 * doesn't support spacing over filemarks in the reverse direction.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002635 */
2636static int idetape_space_over_filemarks (ide_drive_t *drive,short mt_op,int mt_count)
2637{
2638 idetape_tape_t *tape = drive->driver_data;
2639 idetape_pc_t pc;
2640 unsigned long flags;
2641 int retval,count=0;
Borislav Petkovb6422012008-02-02 19:56:49 +01002642 int sprev = !!(tape->caps[4] & 0x20);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002643
2644 if (mt_count == 0)
2645 return 0;
2646 if (MTBSF == mt_op || MTBSFM == mt_op) {
Borislav Petkovb6422012008-02-02 19:56:49 +01002647 if (!sprev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002648 return -EIO;
2649 mt_count = - mt_count;
2650 }
2651
Borislav Petkov54abf372008-02-06 02:57:52 +01002652 if (tape->chrdev_dir == IDETAPE_DIR_READ) {
Borislav Petkov3c98bf32008-02-06 02:57:53 +01002653 /* its a read-ahead buffer, scan it for crossed filemarks. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002654 tape->merge_stage_size = 0;
2655 if (test_and_clear_bit(IDETAPE_FILEMARK, &tape->flags))
2656 ++count;
2657 while (tape->first_stage != NULL) {
2658 if (count == mt_count) {
2659 if (mt_op == MTFSFM)
2660 set_bit(IDETAPE_FILEMARK, &tape->flags);
2661 return 0;
2662 }
Borislav Petkov54bb2072008-02-06 02:57:52 +01002663 spin_lock_irqsave(&tape->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002664 if (tape->first_stage == tape->active_stage) {
2665 /*
Borislav Petkov3c98bf32008-02-06 02:57:53 +01002666 * We have reached the active stage in the read
2667 * pipeline. There is no point in allowing the
2668 * drive to continue reading any farther, so we
2669 * stop the pipeline.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002670 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +01002671 * This section should be moved to a separate
2672 * subroutine because similar operations are
2673 * done in __idetape_discard_read_pipeline(),
2674 * for example.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002675 */
2676 tape->next_stage = NULL;
Borislav Petkov54bb2072008-02-06 02:57:52 +01002677 spin_unlock_irqrestore(&tape->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002678 idetape_wait_first_stage(drive);
2679 tape->next_stage = tape->first_stage->next;
2680 } else
Borislav Petkov54bb2072008-02-06 02:57:52 +01002681 spin_unlock_irqrestore(&tape->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002682 if (tape->first_stage->rq.errors == IDETAPE_ERROR_FILEMARK)
2683 ++count;
2684 idetape_remove_stage_head(drive);
2685 }
2686 idetape_discard_read_pipeline(drive, 0);
2687 }
2688
2689 /*
Borislav Petkov3c98bf32008-02-06 02:57:53 +01002690 * The filemark was not found in our internal pipeline; now we can issue
2691 * the space command.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002692 */
2693 switch (mt_op) {
2694 case MTFSF:
2695 case MTBSF:
2696 idetape_create_space_cmd(&pc,mt_count-count,IDETAPE_SPACE_OVER_FILEMARK);
2697 return (idetape_queue_pc_tail(drive, &pc));
2698 case MTFSFM:
2699 case MTBSFM:
Borislav Petkovb6422012008-02-02 19:56:49 +01002700 if (!sprev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002701 return (-EIO);
2702 retval = idetape_space_over_filemarks(drive, MTFSF, mt_count-count);
2703 if (retval) return (retval);
2704 count = (MTBSFM == mt_op ? 1 : -1);
2705 return (idetape_space_over_filemarks(drive, MTFSF, count));
2706 default:
2707 printk(KERN_ERR "ide-tape: MTIO operation %d not supported\n",mt_op);
2708 return (-EIO);
2709 }
2710}
2711
2712
2713/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +01002714 * Our character device read / write functions.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002715 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +01002716 * The tape is optimized to maximize throughput when it is transferring an
2717 * integral number of the "continuous transfer limit", which is a parameter of
2718 * the specific tape (26kB on my particular tape, 32kB for Onstream).
Linus Torvalds1da177e2005-04-16 15:20:36 -07002719 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +01002720 * As of version 1.3 of the driver, the character device provides an abstract
2721 * continuous view of the media - any mix of block sizes (even 1 byte) on the
2722 * same backup/restore procedure is supported. The driver will internally
2723 * convert the requests to the recommended transfer unit, so that an unmatch
2724 * between the user's block size to the recommended size will only result in a
2725 * (slightly) increased driver overhead, but will no longer hit performance.
2726 * This is not applicable to Onstream.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002727 */
2728static ssize_t idetape_chrdev_read (struct file *file, char __user *buf,
2729 size_t count, loff_t *ppos)
2730{
2731 struct ide_tape_obj *tape = ide_tape_f(file);
2732 ide_drive_t *drive = tape->drive;
2733 ssize_t bytes_read,temp, actually_read = 0, rc;
Daniel Walkerdcd96372006-06-25 05:47:37 -07002734 ssize_t ret = 0;
Borislav Petkovb6422012008-02-02 19:56:49 +01002735 u16 ctl = *(u16 *)&tape->caps[12];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002736
Borislav Petkov8004a8c2008-02-06 02:57:51 +01002737 debug_log(DBG_CHRDEV, "Enter %s, count %Zd\n", __func__, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002738
Borislav Petkov54abf372008-02-06 02:57:52 +01002739 if (tape->chrdev_dir != IDETAPE_DIR_READ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002740 if (test_bit(IDETAPE_DETECT_BS, &tape->flags))
Borislav Petkov54bb2072008-02-06 02:57:52 +01002741 if (count > tape->blk_size &&
2742 (count % tape->blk_size) == 0)
2743 tape->user_bs_factor = count / tape->blk_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002744 }
Borislav Petkov8d06bfa2008-02-06 02:57:53 +01002745 rc = idetape_init_read(drive, tape->max_stages);
2746 if (rc < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002747 return rc;
2748 if (count == 0)
2749 return (0);
2750 if (tape->merge_stage_size) {
2751 actually_read = min((unsigned int)(tape->merge_stage_size), (unsigned int)count);
Daniel Walkerdcd96372006-06-25 05:47:37 -07002752 if (idetape_copy_stage_to_user(tape, buf, tape->merge_stage, actually_read))
2753 ret = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002754 buf += actually_read;
2755 tape->merge_stage_size -= actually_read;
2756 count -= actually_read;
2757 }
2758 while (count >= tape->stage_size) {
Borislav Petkovb6422012008-02-02 19:56:49 +01002759 bytes_read = idetape_add_chrdev_read_request(drive, ctl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002760 if (bytes_read <= 0)
2761 goto finish;
Daniel Walkerdcd96372006-06-25 05:47:37 -07002762 if (idetape_copy_stage_to_user(tape, buf, tape->merge_stage, bytes_read))
2763 ret = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002764 buf += bytes_read;
2765 count -= bytes_read;
2766 actually_read += bytes_read;
2767 }
2768 if (count) {
Borislav Petkovb6422012008-02-02 19:56:49 +01002769 bytes_read = idetape_add_chrdev_read_request(drive, ctl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002770 if (bytes_read <= 0)
2771 goto finish;
2772 temp = min((unsigned long)count, (unsigned long)bytes_read);
Daniel Walkerdcd96372006-06-25 05:47:37 -07002773 if (idetape_copy_stage_to_user(tape, buf, tape->merge_stage, temp))
2774 ret = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002775 actually_read += temp;
2776 tape->merge_stage_size = bytes_read-temp;
2777 }
2778finish:
2779 if (!actually_read && test_bit(IDETAPE_FILEMARK, &tape->flags)) {
Borislav Petkov8004a8c2008-02-06 02:57:51 +01002780 debug_log(DBG_SENSE, "%s: spacing over filemark\n", tape->name);
2781
Linus Torvalds1da177e2005-04-16 15:20:36 -07002782 idetape_space_over_filemarks(drive, MTFSF, 1);
2783 return 0;
2784 }
Daniel Walkerdcd96372006-06-25 05:47:37 -07002785
2786 return (ret) ? ret : actually_read;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002787}
2788
2789static ssize_t idetape_chrdev_write (struct file *file, const char __user *buf,
2790 size_t count, loff_t *ppos)
2791{
2792 struct ide_tape_obj *tape = ide_tape_f(file);
2793 ide_drive_t *drive = tape->drive;
Daniel Walkerdcd96372006-06-25 05:47:37 -07002794 ssize_t actually_written = 0;
2795 ssize_t ret = 0;
Borislav Petkovb6422012008-02-02 19:56:49 +01002796 u16 ctl = *(u16 *)&tape->caps[12];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002797
2798 /* The drive is write protected. */
2799 if (tape->write_prot)
2800 return -EACCES;
2801
Borislav Petkov8004a8c2008-02-06 02:57:51 +01002802 debug_log(DBG_CHRDEV, "Enter %s, count %Zd\n", __func__, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002803
2804 /* Initialize write operation */
Borislav Petkov54abf372008-02-06 02:57:52 +01002805 if (tape->chrdev_dir != IDETAPE_DIR_WRITE) {
2806 if (tape->chrdev_dir == IDETAPE_DIR_READ)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002807 idetape_discard_read_pipeline(drive, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002808 if (tape->merge_stage || tape->merge_stage_size) {
2809 printk(KERN_ERR "ide-tape: merge_stage_size "
2810 "should be 0 now\n");
2811 tape->merge_stage_size = 0;
2812 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002813 if ((tape->merge_stage = __idetape_kmalloc_stage(tape, 0, 0)) == NULL)
2814 return -ENOMEM;
Borislav Petkov54abf372008-02-06 02:57:52 +01002815 tape->chrdev_dir = IDETAPE_DIR_WRITE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002816 idetape_init_merge_stage(tape);
2817
2818 /*
Borislav Petkov3c98bf32008-02-06 02:57:53 +01002819 * Issue a write 0 command to ensure that DSC handshake is
2820 * switched from completion mode to buffer available mode. No
2821 * point in issuing this if DSC overlap isn't supported, some
2822 * drives (Seagate STT3401A) will return an error.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002823 */
2824 if (drive->dsc_overlap) {
Daniel Walkerdcd96372006-06-25 05:47:37 -07002825 ssize_t retval = idetape_queue_rw_tail(drive, REQ_IDETAPE_WRITE, 0, tape->merge_stage->bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002826 if (retval < 0) {
2827 __idetape_kfree_stage(tape->merge_stage);
2828 tape->merge_stage = NULL;
Borislav Petkov54abf372008-02-06 02:57:52 +01002829 tape->chrdev_dir = IDETAPE_DIR_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002830 return retval;
2831 }
2832 }
2833 }
2834 if (count == 0)
2835 return (0);
2836 if (tape->restart_speed_control_req)
2837 idetape_restart_speed_control(drive);
2838 if (tape->merge_stage_size) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002839 if (tape->merge_stage_size >= tape->stage_size) {
2840 printk(KERN_ERR "ide-tape: bug: merge buffer too big\n");
2841 tape->merge_stage_size = 0;
2842 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002843 actually_written = min((unsigned int)(tape->stage_size - tape->merge_stage_size), (unsigned int)count);
Daniel Walkerdcd96372006-06-25 05:47:37 -07002844 if (idetape_copy_stage_from_user(tape, tape->merge_stage, buf, actually_written))
2845 ret = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002846 buf += actually_written;
2847 tape->merge_stage_size += actually_written;
2848 count -= actually_written;
2849
2850 if (tape->merge_stage_size == tape->stage_size) {
Daniel Walkerdcd96372006-06-25 05:47:37 -07002851 ssize_t retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002852 tape->merge_stage_size = 0;
Borislav Petkovb6422012008-02-02 19:56:49 +01002853 retval = idetape_add_chrdev_write_request(drive, ctl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002854 if (retval <= 0)
2855 return (retval);
2856 }
2857 }
2858 while (count >= tape->stage_size) {
Daniel Walkerdcd96372006-06-25 05:47:37 -07002859 ssize_t retval;
2860 if (idetape_copy_stage_from_user(tape, tape->merge_stage, buf, tape->stage_size))
2861 ret = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002862 buf += tape->stage_size;
2863 count -= tape->stage_size;
Borislav Petkovb6422012008-02-02 19:56:49 +01002864 retval = idetape_add_chrdev_write_request(drive, ctl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002865 actually_written += tape->stage_size;
2866 if (retval <= 0)
2867 return (retval);
2868 }
2869 if (count) {
2870 actually_written += count;
Daniel Walkerdcd96372006-06-25 05:47:37 -07002871 if (idetape_copy_stage_from_user(tape, tape->merge_stage, buf, count))
2872 ret = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002873 tape->merge_stage_size += count;
2874 }
Daniel Walkerdcd96372006-06-25 05:47:37 -07002875 return (ret) ? ret : actually_written;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002876}
2877
2878static int idetape_write_filemark (ide_drive_t *drive)
2879{
2880 idetape_pc_t pc;
2881
2882 /* Write a filemark */
2883 idetape_create_write_filemark_cmd(drive, &pc, 1);
2884 if (idetape_queue_pc_tail(drive, &pc)) {
2885 printk(KERN_ERR "ide-tape: Couldn't write a filemark\n");
2886 return -EIO;
2887 }
2888 return 0;
2889}
2890
2891/*
Borislav Petkovd99c9da2008-02-02 19:56:51 +01002892 * Called from idetape_chrdev_ioctl when the general mtio MTIOCTOP ioctl is
2893 * requested.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002894 *
Borislav Petkovd99c9da2008-02-02 19:56:51 +01002895 * Note: MTBSF and MTBSFM are not supported when the tape doesn't support
2896 * spacing over filemarks in the reverse direction. In this case, MTFSFM is also
2897 * usually not supported (it is supported in the rare case in which we crossed
2898 * the filemark during our read-ahead pipelined operation mode).
Linus Torvalds1da177e2005-04-16 15:20:36 -07002899 *
Borislav Petkovd99c9da2008-02-02 19:56:51 +01002900 * The following commands are currently not supported:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002901 *
Borislav Petkovd99c9da2008-02-02 19:56:51 +01002902 * MTFSS, MTBSS, MTWSM, MTSETDENSITY, MTSETDRVBUFFER, MT_ST_BOOLEANS,
2903 * MT_ST_WRITE_THRESHOLD.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002904 */
Borislav Petkovd99c9da2008-02-02 19:56:51 +01002905static int idetape_mtioctop(ide_drive_t *drive, short mt_op, int mt_count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002906{
2907 idetape_tape_t *tape = drive->driver_data;
2908 idetape_pc_t pc;
2909 int i,retval;
2910
Borislav Petkov8004a8c2008-02-06 02:57:51 +01002911 debug_log(DBG_ERR, "Handling MTIOCTOP ioctl: mt_op=%d, mt_count=%d\n",
2912 mt_op, mt_count);
Borislav Petkov3c98bf32008-02-06 02:57:53 +01002913 /* Commands which need our pipelined read-ahead stages. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002914 switch (mt_op) {
2915 case MTFSF:
2916 case MTFSFM:
2917 case MTBSF:
2918 case MTBSFM:
2919 if (!mt_count)
2920 return (0);
2921 return (idetape_space_over_filemarks(drive,mt_op,mt_count));
2922 default:
2923 break;
2924 }
2925 switch (mt_op) {
2926 case MTWEOF:
2927 if (tape->write_prot)
2928 return -EACCES;
2929 idetape_discard_read_pipeline(drive, 1);
2930 for (i = 0; i < mt_count; i++) {
2931 retval = idetape_write_filemark(drive);
2932 if (retval)
2933 return retval;
2934 }
2935 return (0);
2936 case MTREW:
2937 idetape_discard_read_pipeline(drive, 0);
2938 if (idetape_rewind_tape(drive))
2939 return -EIO;
2940 return 0;
2941 case MTLOAD:
2942 idetape_discard_read_pipeline(drive, 0);
2943 idetape_create_load_unload_cmd(drive, &pc, IDETAPE_LU_LOAD_MASK);
2944 return (idetape_queue_pc_tail(drive, &pc));
2945 case MTUNLOAD:
2946 case MTOFFL:
2947 /*
2948 * If door is locked, attempt to unlock before
2949 * attempting to eject.
2950 */
2951 if (tape->door_locked) {
2952 if (idetape_create_prevent_cmd(drive, &pc, 0))
2953 if (!idetape_queue_pc_tail(drive, &pc))
2954 tape->door_locked = DOOR_UNLOCKED;
2955 }
2956 idetape_discard_read_pipeline(drive, 0);
2957 idetape_create_load_unload_cmd(drive, &pc,!IDETAPE_LU_LOAD_MASK);
2958 retval = idetape_queue_pc_tail(drive, &pc);
2959 if (!retval)
2960 clear_bit(IDETAPE_MEDIUM_PRESENT, &tape->flags);
2961 return retval;
2962 case MTNOP:
2963 idetape_discard_read_pipeline(drive, 0);
2964 return (idetape_flush_tape_buffers(drive));
2965 case MTRETEN:
2966 idetape_discard_read_pipeline(drive, 0);
2967 idetape_create_load_unload_cmd(drive, &pc,IDETAPE_LU_RETENSION_MASK | IDETAPE_LU_LOAD_MASK);
2968 return (idetape_queue_pc_tail(drive, &pc));
2969 case MTEOM:
2970 idetape_create_space_cmd(&pc, 0, IDETAPE_SPACE_TO_EOD);
2971 return (idetape_queue_pc_tail(drive, &pc));
2972 case MTERASE:
2973 (void) idetape_rewind_tape(drive);
2974 idetape_create_erase_cmd(&pc);
2975 return (idetape_queue_pc_tail(drive, &pc));
2976 case MTSETBLK:
2977 if (mt_count) {
Borislav Petkov54bb2072008-02-06 02:57:52 +01002978 if (mt_count < tape->blk_size ||
2979 mt_count % tape->blk_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002980 return -EIO;
Borislav Petkov54bb2072008-02-06 02:57:52 +01002981 tape->user_bs_factor = mt_count /
2982 tape->blk_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002983 clear_bit(IDETAPE_DETECT_BS, &tape->flags);
2984 } else
2985 set_bit(IDETAPE_DETECT_BS, &tape->flags);
2986 return 0;
2987 case MTSEEK:
2988 idetape_discard_read_pipeline(drive, 0);
2989 return idetape_position_tape(drive, mt_count * tape->user_bs_factor, tape->partition, 0);
2990 case MTSETPART:
2991 idetape_discard_read_pipeline(drive, 0);
2992 return (idetape_position_tape(drive, 0, mt_count, 0));
2993 case MTFSR:
2994 case MTBSR:
2995 case MTLOCK:
2996 if (!idetape_create_prevent_cmd(drive, &pc, 1))
2997 return 0;
2998 retval = idetape_queue_pc_tail(drive, &pc);
2999 if (retval) return retval;
3000 tape->door_locked = DOOR_EXPLICITLY_LOCKED;
3001 return 0;
3002 case MTUNLOCK:
3003 if (!idetape_create_prevent_cmd(drive, &pc, 0))
3004 return 0;
3005 retval = idetape_queue_pc_tail(drive, &pc);
3006 if (retval) return retval;
3007 tape->door_locked = DOOR_UNLOCKED;
3008 return 0;
3009 default:
3010 printk(KERN_ERR "ide-tape: MTIO operation %d not "
3011 "supported\n", mt_op);
3012 return (-EIO);
3013 }
3014}
3015
3016/*
Borislav Petkovd99c9da2008-02-02 19:56:51 +01003017 * Our character device ioctls. General mtio.h magnetic io commands are
3018 * supported here, and not in the corresponding block interface. Our own
3019 * ide-tape ioctls are supported on both interfaces.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003020 */
Borislav Petkovd99c9da2008-02-02 19:56:51 +01003021static int idetape_chrdev_ioctl(struct inode *inode, struct file *file,
3022 unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003023{
3024 struct ide_tape_obj *tape = ide_tape_f(file);
3025 ide_drive_t *drive = tape->drive;
3026 struct mtop mtop;
3027 struct mtget mtget;
3028 struct mtpos mtpos;
Borislav Petkov54bb2072008-02-06 02:57:52 +01003029 int block_offset = 0, position = tape->first_frame;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003030 void __user *argp = (void __user *)arg;
3031
Borislav Petkov8004a8c2008-02-06 02:57:51 +01003032 debug_log(DBG_CHRDEV, "Enter %s, cmd=%u\n", __func__, cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003033
3034 tape->restart_speed_control_req = 1;
Borislav Petkov54abf372008-02-06 02:57:52 +01003035 if (tape->chrdev_dir == IDETAPE_DIR_WRITE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003036 idetape_empty_write_pipeline(drive);
3037 idetape_flush_tape_buffers(drive);
3038 }
3039 if (cmd == MTIOCGET || cmd == MTIOCPOS) {
Borislav Petkov54bb2072008-02-06 02:57:52 +01003040 block_offset = idetape_pipeline_size(drive) /
3041 (tape->blk_size * tape->user_bs_factor);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003042 if ((position = idetape_read_position(drive)) < 0)
3043 return -EIO;
3044 }
3045 switch (cmd) {
3046 case MTIOCTOP:
3047 if (copy_from_user(&mtop, argp, sizeof (struct mtop)))
3048 return -EFAULT;
3049 return (idetape_mtioctop(drive,mtop.mt_op,mtop.mt_count));
3050 case MTIOCGET:
3051 memset(&mtget, 0, sizeof (struct mtget));
3052 mtget.mt_type = MT_ISSCSI2;
3053 mtget.mt_blkno = position / tape->user_bs_factor - block_offset;
Borislav Petkov54bb2072008-02-06 02:57:52 +01003054 mtget.mt_dsreg =
3055 ((tape->blk_size * tape->user_bs_factor)
3056 << MT_ST_BLKSIZE_SHIFT) & MT_ST_BLKSIZE_MASK;
3057
Linus Torvalds1da177e2005-04-16 15:20:36 -07003058 if (tape->drv_write_prot) {
3059 mtget.mt_gstat |= GMT_WR_PROT(0xffffffff);
3060 }
3061 if (copy_to_user(argp, &mtget, sizeof(struct mtget)))
3062 return -EFAULT;
3063 return 0;
3064 case MTIOCPOS:
3065 mtpos.mt_blkno = position / tape->user_bs_factor - block_offset;
3066 if (copy_to_user(argp, &mtpos, sizeof(struct mtpos)))
3067 return -EFAULT;
3068 return 0;
3069 default:
Borislav Petkov54abf372008-02-06 02:57:52 +01003070 if (tape->chrdev_dir == IDETAPE_DIR_READ)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003071 idetape_discard_read_pipeline(drive, 1);
3072 return idetape_blkdev_ioctl(drive, cmd, arg);
3073 }
3074}
3075
Borislav Petkov3cffb9c2008-02-02 19:56:50 +01003076/*
3077 * Do a mode sense page 0 with block descriptor and if it succeeds set the tape
3078 * block size with the reported value.
3079 */
3080static void ide_tape_get_bsize_from_bdesc(ide_drive_t *drive)
3081{
3082 idetape_tape_t *tape = drive->driver_data;
3083 idetape_pc_t pc;
3084
3085 idetape_create_mode_sense_cmd(&pc, IDETAPE_BLOCK_DESCRIPTOR);
3086 if (idetape_queue_pc_tail(drive, &pc)) {
3087 printk(KERN_ERR "ide-tape: Can't get block descriptor\n");
Borislav Petkov54bb2072008-02-06 02:57:52 +01003088 if (tape->blk_size == 0) {
Borislav Petkov3cffb9c2008-02-02 19:56:50 +01003089 printk(KERN_WARNING "ide-tape: Cannot deal with zero "
3090 "block size, assuming 32k\n");
Borislav Petkov54bb2072008-02-06 02:57:52 +01003091 tape->blk_size = 32768;
Borislav Petkov3cffb9c2008-02-02 19:56:50 +01003092 }
3093 return;
3094 }
Borislav Petkov54bb2072008-02-06 02:57:52 +01003095 tape->blk_size = (pc.buffer[4 + 5] << 16) +
Borislav Petkov3cffb9c2008-02-02 19:56:50 +01003096 (pc.buffer[4 + 6] << 8) +
3097 pc.buffer[4 + 7];
3098 tape->drv_write_prot = (pc.buffer[2] & 0x80) >> 7;
3099}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003100
Linus Torvalds1da177e2005-04-16 15:20:36 -07003101static int idetape_chrdev_open (struct inode *inode, struct file *filp)
3102{
3103 unsigned int minor = iminor(inode), i = minor & ~0xc0;
3104 ide_drive_t *drive;
3105 idetape_tape_t *tape;
3106 idetape_pc_t pc;
3107 int retval;
3108
Borislav Petkov8004a8c2008-02-06 02:57:51 +01003109 if (i >= MAX_HWIFS * MAX_DRIVES)
3110 return -ENXIO;
3111
3112 tape = ide_tape_chrdev_get(i);
3113 if (!tape)
3114 return -ENXIO;
3115
3116 debug_log(DBG_CHRDEV, "Enter %s\n", __func__);
3117
Linus Torvalds1da177e2005-04-16 15:20:36 -07003118 /*
3119 * We really want to do nonseekable_open(inode, filp); here, but some
3120 * versions of tar incorrectly call lseek on tapes and bail out if that
3121 * fails. So we disallow pread() and pwrite(), but permit lseeks.
3122 */
3123 filp->f_mode &= ~(FMODE_PREAD | FMODE_PWRITE);
3124
Linus Torvalds1da177e2005-04-16 15:20:36 -07003125 drive = tape->drive;
3126
3127 filp->private_data = tape;
3128
3129 if (test_and_set_bit(IDETAPE_BUSY, &tape->flags)) {
3130 retval = -EBUSY;
3131 goto out_put_tape;
3132 }
3133
3134 retval = idetape_wait_ready(drive, 60 * HZ);
3135 if (retval) {
3136 clear_bit(IDETAPE_BUSY, &tape->flags);
3137 printk(KERN_ERR "ide-tape: %s: drive not ready\n", tape->name);
3138 goto out_put_tape;
3139 }
3140
3141 idetape_read_position(drive);
3142 if (!test_bit(IDETAPE_ADDRESS_VALID, &tape->flags))
3143 (void)idetape_rewind_tape(drive);
3144
Borislav Petkov54abf372008-02-06 02:57:52 +01003145 if (tape->chrdev_dir != IDETAPE_DIR_READ)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003146 clear_bit(IDETAPE_PIPELINE_ERROR, &tape->flags);
3147
3148 /* Read block size and write protect status from drive. */
Borislav Petkov3cffb9c2008-02-02 19:56:50 +01003149 ide_tape_get_bsize_from_bdesc(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003150
3151 /* Set write protect flag if device is opened as read-only. */
3152 if ((filp->f_flags & O_ACCMODE) == O_RDONLY)
3153 tape->write_prot = 1;
3154 else
3155 tape->write_prot = tape->drv_write_prot;
3156
3157 /* Make sure drive isn't write protected if user wants to write. */
3158 if (tape->write_prot) {
3159 if ((filp->f_flags & O_ACCMODE) == O_WRONLY ||
3160 (filp->f_flags & O_ACCMODE) == O_RDWR) {
3161 clear_bit(IDETAPE_BUSY, &tape->flags);
3162 retval = -EROFS;
3163 goto out_put_tape;
3164 }
3165 }
3166
Borislav Petkov3c98bf32008-02-06 02:57:53 +01003167 /* Lock the tape drive door so user can't eject. */
Borislav Petkov54abf372008-02-06 02:57:52 +01003168 if (tape->chrdev_dir == IDETAPE_DIR_NONE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003169 if (idetape_create_prevent_cmd(drive, &pc, 1)) {
3170 if (!idetape_queue_pc_tail(drive, &pc)) {
3171 if (tape->door_locked != DOOR_EXPLICITLY_LOCKED)
3172 tape->door_locked = DOOR_LOCKED;
3173 }
3174 }
3175 }
3176 idetape_restart_speed_control(drive);
3177 tape->restart_speed_control_req = 0;
3178 return 0;
3179
3180out_put_tape:
3181 ide_tape_put(tape);
3182 return retval;
3183}
3184
3185static void idetape_write_release (ide_drive_t *drive, unsigned int minor)
3186{
3187 idetape_tape_t *tape = drive->driver_data;
3188
3189 idetape_empty_write_pipeline(drive);
3190 tape->merge_stage = __idetape_kmalloc_stage(tape, 1, 0);
3191 if (tape->merge_stage != NULL) {
Borislav Petkov54bb2072008-02-06 02:57:52 +01003192 idetape_pad_zeros(drive, tape->blk_size *
3193 (tape->user_bs_factor - 1));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003194 __idetape_kfree_stage(tape->merge_stage);
3195 tape->merge_stage = NULL;
3196 }
3197 idetape_write_filemark(drive);
3198 idetape_flush_tape_buffers(drive);
3199 idetape_flush_tape_buffers(drive);
3200}
3201
Linus Torvalds1da177e2005-04-16 15:20:36 -07003202static int idetape_chrdev_release (struct inode *inode, struct file *filp)
3203{
3204 struct ide_tape_obj *tape = ide_tape_f(filp);
3205 ide_drive_t *drive = tape->drive;
3206 idetape_pc_t pc;
3207 unsigned int minor = iminor(inode);
3208
3209 lock_kernel();
3210 tape = drive->driver_data;
Borislav Petkov8004a8c2008-02-06 02:57:51 +01003211
3212 debug_log(DBG_CHRDEV, "Enter %s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003213
Borislav Petkov54abf372008-02-06 02:57:52 +01003214 if (tape->chrdev_dir == IDETAPE_DIR_WRITE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003215 idetape_write_release(drive, minor);
Borislav Petkov54abf372008-02-06 02:57:52 +01003216 if (tape->chrdev_dir == IDETAPE_DIR_READ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003217 if (minor < 128)
3218 idetape_discard_read_pipeline(drive, 1);
3219 else
3220 idetape_wait_for_pipeline(drive);
3221 }
3222 if (tape->cache_stage != NULL) {
3223 __idetape_kfree_stage(tape->cache_stage);
3224 tape->cache_stage = NULL;
3225 }
3226 if (minor < 128 && test_bit(IDETAPE_MEDIUM_PRESENT, &tape->flags))
3227 (void) idetape_rewind_tape(drive);
Borislav Petkov54abf372008-02-06 02:57:52 +01003228 if (tape->chrdev_dir == IDETAPE_DIR_NONE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003229 if (tape->door_locked == DOOR_LOCKED) {
3230 if (idetape_create_prevent_cmd(drive, &pc, 0)) {
3231 if (!idetape_queue_pc_tail(drive, &pc))
3232 tape->door_locked = DOOR_UNLOCKED;
3233 }
3234 }
3235 }
3236 clear_bit(IDETAPE_BUSY, &tape->flags);
3237 ide_tape_put(tape);
3238 unlock_kernel();
3239 return 0;
3240}
3241
3242/*
Borislav Petkov71071b82008-02-06 02:57:53 +01003243 * check the contents of the ATAPI IDENTIFY command results. We return:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003244 *
Borislav Petkov71071b82008-02-06 02:57:53 +01003245 * 1 - If the tape can be supported by us, based on the information we have so
3246 * far.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003247 *
Borislav Petkov71071b82008-02-06 02:57:53 +01003248 * 0 - If this tape driver is not currently supported by us.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003249 */
Borislav Petkov71071b82008-02-06 02:57:53 +01003250static int idetape_identify_device(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003251{
Borislav Petkov71071b82008-02-06 02:57:53 +01003252 u8 gcw[2], protocol, device_type, removable, packet_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003253
3254 if (drive->id_read == 0)
3255 return 1;
3256
Borislav Petkov71071b82008-02-06 02:57:53 +01003257 *((unsigned short *) &gcw) = drive->id->config;
3258
3259 protocol = (gcw[1] & 0xC0) >> 6;
3260 device_type = gcw[1] & 0x1F;
3261 removable = !!(gcw[0] & 0x80);
3262 packet_size = gcw[0] & 0x3;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003263
Linus Torvalds1da177e2005-04-16 15:20:36 -07003264 /* Check that we can support this device */
Borislav Petkov71071b82008-02-06 02:57:53 +01003265 if (protocol != 2)
Bartlomiej Zolnierkiewicz16422de32008-02-02 19:56:48 +01003266 printk(KERN_ERR "ide-tape: Protocol (0x%02x) is not ATAPI\n",
Borislav Petkov71071b82008-02-06 02:57:53 +01003267 protocol);
3268 else if (device_type != 1)
Bartlomiej Zolnierkiewicz16422de32008-02-02 19:56:48 +01003269 printk(KERN_ERR "ide-tape: Device type (0x%02x) is not set "
Borislav Petkov71071b82008-02-06 02:57:53 +01003270 "to tape\n", device_type);
3271 else if (!removable)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003272 printk(KERN_ERR "ide-tape: The removable flag is not set\n");
Borislav Petkov71071b82008-02-06 02:57:53 +01003273 else if (packet_size != 0) {
Bartlomiej Zolnierkiewicz16422de32008-02-02 19:56:48 +01003274 printk(KERN_ERR "ide-tape: Packet size (0x%02x) is not 12 "
Borislav Petkov71071b82008-02-06 02:57:53 +01003275 "bytes long\n", packet_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003276 } else
3277 return 1;
3278 return 0;
3279}
3280
Borislav Petkov6d29c8f2008-02-02 19:56:49 +01003281static void idetape_get_inquiry_results(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003282{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003283 idetape_tape_t *tape = drive->driver_data;
3284 idetape_pc_t pc;
Borislav Petkov41f81d542008-02-06 02:57:52 +01003285 char fw_rev[6], vendor_id[10], product_id[18];
Borislav Petkov6d29c8f2008-02-02 19:56:49 +01003286
Linus Torvalds1da177e2005-04-16 15:20:36 -07003287 idetape_create_inquiry_cmd(&pc);
3288 if (idetape_queue_pc_tail(drive, &pc)) {
Borislav Petkov6d29c8f2008-02-02 19:56:49 +01003289 printk(KERN_ERR "ide-tape: %s: can't get INQUIRY results\n",
3290 tape->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003291 return;
3292 }
Borislav Petkov41f81d542008-02-06 02:57:52 +01003293 memcpy(vendor_id, &pc.buffer[8], 8);
3294 memcpy(product_id, &pc.buffer[16], 16);
3295 memcpy(fw_rev, &pc.buffer[32], 4);
Borislav Petkov6d29c8f2008-02-02 19:56:49 +01003296
Borislav Petkov41f81d542008-02-06 02:57:52 +01003297 ide_fixstring(vendor_id, 10, 0);
3298 ide_fixstring(product_id, 18, 0);
3299 ide_fixstring(fw_rev, 6, 0);
3300
Borislav Petkov6d29c8f2008-02-02 19:56:49 +01003301 printk(KERN_INFO "ide-tape: %s <-> %s: %s %s rev %s\n",
Borislav Petkov41f81d542008-02-06 02:57:52 +01003302 drive->name, tape->name, vendor_id, product_id, fw_rev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003303}
3304
3305/*
Borislav Petkovb6422012008-02-02 19:56:49 +01003306 * Ask the tape about its various parameters. In particular, we will adjust our
3307 * data transfer buffer size to the recommended value as returned by the tape.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003308 */
3309static void idetape_get_mode_sense_results (ide_drive_t *drive)
3310{
3311 idetape_tape_t *tape = drive->driver_data;
3312 idetape_pc_t pc;
Borislav Petkovb6422012008-02-02 19:56:49 +01003313 u8 *caps;
3314 u8 speed, max_speed;
Borislav Petkov47314fa2008-02-02 19:56:48 +01003315
Linus Torvalds1da177e2005-04-16 15:20:36 -07003316 idetape_create_mode_sense_cmd(&pc, IDETAPE_CAPABILITIES_PAGE);
3317 if (idetape_queue_pc_tail(drive, &pc)) {
Borislav Petkovb6422012008-02-02 19:56:49 +01003318 printk(KERN_ERR "ide-tape: Can't get tape parameters - assuming"
3319 " some default values\n");
Borislav Petkov54bb2072008-02-06 02:57:52 +01003320 tape->blk_size = 512;
Borislav Petkovb6422012008-02-02 19:56:49 +01003321 put_unaligned(52, (u16 *)&tape->caps[12]);
3322 put_unaligned(540, (u16 *)&tape->caps[14]);
3323 put_unaligned(6*52, (u16 *)&tape->caps[16]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003324 return;
3325 }
Borislav Petkovb6422012008-02-02 19:56:49 +01003326 caps = pc.buffer + 4 + pc.buffer[3];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003327
Borislav Petkovb6422012008-02-02 19:56:49 +01003328 /* convert to host order and save for later use */
3329 speed = be16_to_cpu(*(u16 *)&caps[14]);
3330 max_speed = be16_to_cpu(*(u16 *)&caps[8]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003331
Borislav Petkovb6422012008-02-02 19:56:49 +01003332 put_unaligned(max_speed, (u16 *)&caps[8]);
3333 put_unaligned(be16_to_cpu(*(u16 *)&caps[12]), (u16 *)&caps[12]);
3334 put_unaligned(speed, (u16 *)&caps[14]);
3335 put_unaligned(be16_to_cpu(*(u16 *)&caps[16]), (u16 *)&caps[16]);
3336
3337 if (!speed) {
3338 printk(KERN_INFO "ide-tape: %s: invalid tape speed "
3339 "(assuming 650KB/sec)\n", drive->name);
3340 put_unaligned(650, (u16 *)&caps[14]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003341 }
Borislav Petkovb6422012008-02-02 19:56:49 +01003342 if (!max_speed) {
3343 printk(KERN_INFO "ide-tape: %s: invalid max_speed "
3344 "(assuming 650KB/sec)\n", drive->name);
3345 put_unaligned(650, (u16 *)&caps[8]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003346 }
3347
Borislav Petkovb6422012008-02-02 19:56:49 +01003348 memcpy(&tape->caps, caps, 20);
3349 if (caps[7] & 0x02)
Borislav Petkov54bb2072008-02-06 02:57:52 +01003350 tape->blk_size = 512;
Borislav Petkovb6422012008-02-02 19:56:49 +01003351 else if (caps[7] & 0x04)
Borislav Petkov54bb2072008-02-06 02:57:52 +01003352 tape->blk_size = 1024;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003353}
3354
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +02003355#ifdef CONFIG_IDE_PROC_FS
Linus Torvalds1da177e2005-04-16 15:20:36 -07003356static void idetape_add_settings (ide_drive_t *drive)
3357{
3358 idetape_tape_t *tape = drive->driver_data;
3359
Borislav Petkovb6422012008-02-02 19:56:49 +01003360 ide_add_setting(drive, "buffer", SETTING_READ, TYPE_SHORT, 0, 0xffff,
3361 1, 2, (u16 *)&tape->caps[16], NULL);
Bartlomiej Zolnierkiewicz14979432007-05-10 00:01:10 +02003362 ide_add_setting(drive, "pipeline_min", SETTING_RW, TYPE_INT, 1, 0xffff, tape->stage_size / 1024, 1, &tape->min_pipeline, NULL);
3363 ide_add_setting(drive, "pipeline", SETTING_RW, TYPE_INT, 1, 0xffff, tape->stage_size / 1024, 1, &tape->max_stages, NULL);
3364 ide_add_setting(drive, "pipeline_max", SETTING_RW, TYPE_INT, 1, 0xffff, tape->stage_size / 1024, 1, &tape->max_pipeline, NULL);
3365 ide_add_setting(drive, "pipeline_used", SETTING_READ, TYPE_INT, 0, 0xffff, tape->stage_size / 1024, 1, &tape->nr_stages, NULL);
3366 ide_add_setting(drive, "pipeline_pending", SETTING_READ, TYPE_INT, 0, 0xffff, tape->stage_size / 1024, 1, &tape->nr_pending_stages, NULL);
Borislav Petkovb6422012008-02-02 19:56:49 +01003367 ide_add_setting(drive, "speed", SETTING_READ, TYPE_SHORT, 0, 0xffff,
3368 1, 1, (u16 *)&tape->caps[14], NULL);
Borislav Petkov54bb2072008-02-06 02:57:52 +01003369 ide_add_setting(drive, "stage", SETTING_READ, TYPE_INT, 0, 0xffff, 1,
3370 1024, &tape->stage_size, NULL);
3371 ide_add_setting(drive, "tdsc", SETTING_RW, TYPE_INT, IDETAPE_DSC_RW_MIN,
3372 IDETAPE_DSC_RW_MAX, 1000, HZ, &tape->best_dsc_rw_freq,
3373 NULL);
Bartlomiej Zolnierkiewicz14979432007-05-10 00:01:10 +02003374 ide_add_setting(drive, "dsc_overlap", SETTING_RW, TYPE_BYTE, 0, 1, 1, 1, &drive->dsc_overlap, NULL);
3375 ide_add_setting(drive, "pipeline_head_speed_c",SETTING_READ, TYPE_INT, 0, 0xffff, 1, 1, &tape->controlled_pipeline_head_speed, NULL);
3376 ide_add_setting(drive, "pipeline_head_speed_u",SETTING_READ, TYPE_INT, 0, 0xffff, 1, 1, &tape->uncontrolled_pipeline_head_speed,NULL);
3377 ide_add_setting(drive, "avg_speed", SETTING_READ, TYPE_INT, 0, 0xffff, 1, 1, &tape->avg_speed, NULL);
Borislav Petkov8004a8c2008-02-06 02:57:51 +01003378 ide_add_setting(drive, "debug_mask", SETTING_RW, TYPE_INT, 0, 0xffff, 1,
3379 1, &tape->debug_mask, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003380}
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +02003381#else
3382static inline void idetape_add_settings(ide_drive_t *drive) { ; }
3383#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003384
3385/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +01003386 * The function below is called to:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003387 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +01003388 * 1. Initialize our various state variables.
3389 * 2. Ask the tape for its capabilities.
3390 * 3. Allocate a buffer which will be used for data transfer. The buffer size
3391 * is chosen based on the recommendation which we received in step 2.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003392 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +01003393 * Note that at this point ide.c already assigned us an irq, so that we can
3394 * queue requests here and wait for their completion.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003395 */
3396static void idetape_setup (ide_drive_t *drive, idetape_tape_t *tape, int minor)
3397{
3398 unsigned long t1, tmid, tn, t;
3399 int speed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003400 int stage_size;
Borislav Petkov71071b82008-02-06 02:57:53 +01003401 u8 gcw[2];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003402 struct sysinfo si;
Borislav Petkovb6422012008-02-02 19:56:49 +01003403 u16 *ctl = (u16 *)&tape->caps[12];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003404
Borislav Petkov54bb2072008-02-06 02:57:52 +01003405 spin_lock_init(&tape->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003406 drive->dsc_overlap = 1;
Bartlomiej Zolnierkiewicz4166c192008-02-01 23:09:30 +01003407 if (drive->hwif->host_flags & IDE_HFLAG_NO_DSC) {
3408 printk(KERN_INFO "ide-tape: %s: disabling DSC overlap\n",
3409 tape->name);
3410 drive->dsc_overlap = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003411 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003412 /* Seagate Travan drives do not support DSC overlap. */
3413 if (strstr(drive->id->model, "Seagate STT3401"))
3414 drive->dsc_overlap = 0;
3415 tape->minor = minor;
3416 tape->name[0] = 'h';
3417 tape->name[1] = 't';
3418 tape->name[2] = '0' + minor;
Borislav Petkov54abf372008-02-06 02:57:52 +01003419 tape->chrdev_dir = IDETAPE_DIR_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003420 tape->pc = tape->pc_stack;
3421 tape->max_insert_speed = 10000;
3422 tape->speed_control = 1;
3423 *((unsigned short *) &gcw) = drive->id->config;
Borislav Petkov71071b82008-02-06 02:57:53 +01003424
3425 /* Command packet DRQ type */
3426 if (((gcw[0] & 0x60) >> 5) == 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003427 set_bit(IDETAPE_DRQ_INTERRUPT, &tape->flags);
3428
3429 tape->min_pipeline = tape->max_pipeline = tape->max_stages = 10;
Borislav Petkov3c98bf32008-02-06 02:57:53 +01003430
Linus Torvalds1da177e2005-04-16 15:20:36 -07003431 idetape_get_inquiry_results(drive);
3432 idetape_get_mode_sense_results(drive);
Borislav Petkov3cffb9c2008-02-02 19:56:50 +01003433 ide_tape_get_bsize_from_bdesc(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003434 tape->user_bs_factor = 1;
Borislav Petkov54bb2072008-02-06 02:57:52 +01003435 tape->stage_size = *ctl * tape->blk_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003436 while (tape->stage_size > 0xffff) {
3437 printk(KERN_NOTICE "ide-tape: decreasing stage size\n");
Borislav Petkovb6422012008-02-02 19:56:49 +01003438 *ctl /= 2;
Borislav Petkov54bb2072008-02-06 02:57:52 +01003439 tape->stage_size = *ctl * tape->blk_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003440 }
3441 stage_size = tape->stage_size;
3442 tape->pages_per_stage = stage_size / PAGE_SIZE;
3443 if (stage_size % PAGE_SIZE) {
3444 tape->pages_per_stage++;
3445 tape->excess_bh_size = PAGE_SIZE - stage_size % PAGE_SIZE;
3446 }
3447
Borislav Petkovb6422012008-02-02 19:56:49 +01003448 /* Select the "best" DSC read/write polling freq and pipeline size. */
3449 speed = max(*(u16 *)&tape->caps[14], *(u16 *)&tape->caps[8]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003450
3451 tape->max_stages = speed * 1000 * 10 / tape->stage_size;
3452
Borislav Petkov3c98bf32008-02-06 02:57:53 +01003453 /* Limit memory use for pipeline to 10% of physical memory */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003454 si_meminfo(&si);
3455 if (tape->max_stages * tape->stage_size > si.totalram * si.mem_unit / 10)
3456 tape->max_stages = si.totalram * si.mem_unit / (10 * tape->stage_size);
3457 tape->max_stages = min(tape->max_stages, IDETAPE_MAX_PIPELINE_STAGES);
3458 tape->min_pipeline = min(tape->max_stages, IDETAPE_MIN_PIPELINE_STAGES);
3459 tape->max_pipeline = min(tape->max_stages * 2, IDETAPE_MAX_PIPELINE_STAGES);
3460 if (tape->max_stages == 0)
3461 tape->max_stages = tape->min_pipeline = tape->max_pipeline = 1;
3462
3463 t1 = (tape->stage_size * HZ) / (speed * 1000);
Borislav Petkovb6422012008-02-02 19:56:49 +01003464 tmid = (*(u16 *)&tape->caps[16] * 32 * HZ) / (speed * 125);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003465 tn = (IDETAPE_FIFO_THRESHOLD * tape->stage_size * HZ) / (speed * 1000);
3466
3467 if (tape->max_stages)
3468 t = tn;
3469 else
3470 t = t1;
3471
3472 /*
Borislav Petkov3c98bf32008-02-06 02:57:53 +01003473 * Ensure that the number we got makes sense; limit it within
3474 * IDETAPE_DSC_RW_MIN and IDETAPE_DSC_RW_MAX.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003475 */
Borislav Petkov54bb2072008-02-06 02:57:52 +01003476 tape->best_dsc_rw_freq = max_t(unsigned long,
3477 min_t(unsigned long, t, IDETAPE_DSC_RW_MAX),
3478 IDETAPE_DSC_RW_MIN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003479 printk(KERN_INFO "ide-tape: %s <-> %s: %dKBps, %d*%dkB buffer, "
3480 "%dkB pipeline, %lums tDSC%s\n",
Borislav Petkovb6422012008-02-02 19:56:49 +01003481 drive->name, tape->name, *(u16 *)&tape->caps[14],
3482 (*(u16 *)&tape->caps[16] * 512) / tape->stage_size,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003483 tape->stage_size / 1024,
3484 tape->max_stages * tape->stage_size / 1024,
Borislav Petkov54bb2072008-02-06 02:57:52 +01003485 tape->best_dsc_rw_freq * 1000 / HZ,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003486 drive->using_dma ? ", DMA":"");
3487
3488 idetape_add_settings(drive);
3489}
3490
Russell King4031bbe2006-01-06 11:41:00 +00003491static void ide_tape_remove(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003492{
3493 idetape_tape_t *tape = drive->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003494
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +02003495 ide_proc_unregister_driver(drive, tape->driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003496
3497 ide_unregister_region(tape->disk);
3498
3499 ide_tape_put(tape);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003500}
3501
3502static void ide_tape_release(struct kref *kref)
3503{
3504 struct ide_tape_obj *tape = to_ide_tape(kref);
3505 ide_drive_t *drive = tape->drive;
3506 struct gendisk *g = tape->disk;
3507
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02003508 BUG_ON(tape->first_stage != NULL || tape->merge_stage_size);
3509
Linus Torvalds1da177e2005-04-16 15:20:36 -07003510 drive->dsc_overlap = 0;
3511 drive->driver_data = NULL;
Tony Jonesdbc12722007-09-25 02:03:03 +02003512 device_destroy(idetape_sysfs_class, MKDEV(IDETAPE_MAJOR, tape->minor));
3513 device_destroy(idetape_sysfs_class, MKDEV(IDETAPE_MAJOR, tape->minor + 128));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003514 idetape_devs[tape->minor] = NULL;
3515 g->private_data = NULL;
3516 put_disk(g);
3517 kfree(tape);
3518}
3519
Bartlomiej Zolnierkiewiczecfd80e2007-05-10 00:01:09 +02003520#ifdef CONFIG_IDE_PROC_FS
Linus Torvalds1da177e2005-04-16 15:20:36 -07003521static int proc_idetape_read_name
3522 (char *page, char **start, off_t off, int count, int *eof, void *data)
3523{
3524 ide_drive_t *drive = (ide_drive_t *) data;
3525 idetape_tape_t *tape = drive->driver_data;
3526 char *out = page;
3527 int len;
3528
3529 len = sprintf(out, "%s\n", tape->name);
3530 PROC_IDE_READ_RETURN(page, start, off, count, eof, len);
3531}
3532
3533static ide_proc_entry_t idetape_proc[] = {
3534 { "capacity", S_IFREG|S_IRUGO, proc_ide_read_capacity, NULL },
3535 { "name", S_IFREG|S_IRUGO, proc_idetape_read_name, NULL },
3536 { NULL, 0, NULL, NULL }
3537};
Linus Torvalds1da177e2005-04-16 15:20:36 -07003538#endif
3539
Russell King4031bbe2006-01-06 11:41:00 +00003540static int ide_tape_probe(ide_drive_t *);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003541
Linus Torvalds1da177e2005-04-16 15:20:36 -07003542static ide_driver_t idetape_driver = {
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02003543 .gen_driver = {
Laurent Riffard4ef3b8f2005-11-18 22:15:40 +01003544 .owner = THIS_MODULE,
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02003545 .name = "ide-tape",
3546 .bus = &ide_bus_type,
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02003547 },
Russell King4031bbe2006-01-06 11:41:00 +00003548 .probe = ide_tape_probe,
3549 .remove = ide_tape_remove,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003550 .version = IDETAPE_VERSION,
3551 .media = ide_tape,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003552 .supports_dsc_overlap = 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003553 .do_request = idetape_do_request,
3554 .end_request = idetape_end_request,
3555 .error = __ide_error,
3556 .abort = __ide_abort,
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +02003557#ifdef CONFIG_IDE_PROC_FS
Linus Torvalds1da177e2005-04-16 15:20:36 -07003558 .proc = idetape_proc,
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +02003559#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003560};
3561
Borislav Petkov3c98bf32008-02-06 02:57:53 +01003562/* Our character device supporting functions, passed to register_chrdev. */
Arjan van de Ven2b8693c2007-02-12 00:55:32 -08003563static const struct file_operations idetape_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003564 .owner = THIS_MODULE,
3565 .read = idetape_chrdev_read,
3566 .write = idetape_chrdev_write,
3567 .ioctl = idetape_chrdev_ioctl,
3568 .open = idetape_chrdev_open,
3569 .release = idetape_chrdev_release,
3570};
3571
3572static int idetape_open(struct inode *inode, struct file *filp)
3573{
3574 struct gendisk *disk = inode->i_bdev->bd_disk;
3575 struct ide_tape_obj *tape;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003576
3577 if (!(tape = ide_tape_get(disk)))
3578 return -ENXIO;
3579
Linus Torvalds1da177e2005-04-16 15:20:36 -07003580 return 0;
3581}
3582
3583static int idetape_release(struct inode *inode, struct file *filp)
3584{
3585 struct gendisk *disk = inode->i_bdev->bd_disk;
3586 struct ide_tape_obj *tape = ide_tape_g(disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003587
3588 ide_tape_put(tape);
3589
3590 return 0;
3591}
3592
3593static int idetape_ioctl(struct inode *inode, struct file *file,
3594 unsigned int cmd, unsigned long arg)
3595{
3596 struct block_device *bdev = inode->i_bdev;
3597 struct ide_tape_obj *tape = ide_tape_g(bdev->bd_disk);
3598 ide_drive_t *drive = tape->drive;
3599 int err = generic_ide_ioctl(drive, file, bdev, cmd, arg);
3600 if (err == -EINVAL)
3601 err = idetape_blkdev_ioctl(drive, cmd, arg);
3602 return err;
3603}
3604
3605static struct block_device_operations idetape_block_ops = {
3606 .owner = THIS_MODULE,
3607 .open = idetape_open,
3608 .release = idetape_release,
3609 .ioctl = idetape_ioctl,
3610};
3611
Russell King4031bbe2006-01-06 11:41:00 +00003612static int ide_tape_probe(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003613{
3614 idetape_tape_t *tape;
3615 struct gendisk *g;
3616 int minor;
3617
3618 if (!strstr("ide-tape", drive->driver_req))
3619 goto failed;
3620 if (!drive->present)
3621 goto failed;
3622 if (drive->media != ide_tape)
3623 goto failed;
3624 if (!idetape_identify_device (drive)) {
3625 printk(KERN_ERR "ide-tape: %s: not supported by this version of ide-tape\n", drive->name);
3626 goto failed;
3627 }
3628 if (drive->scsi) {
3629 printk("ide-tape: passing drive %s to ide-scsi emulation.\n", drive->name);
3630 goto failed;
3631 }
3632 if (strstr(drive->id->model, "OnStream DI-")) {
3633 printk(KERN_WARNING "ide-tape: Use drive %s with ide-scsi emulation and osst.\n", drive->name);
3634 printk(KERN_WARNING "ide-tape: OnStream support will be removed soon from ide-tape!\n");
3635 }
Robert P. J. Day5cbded52006-12-13 00:35:56 -08003636 tape = kzalloc(sizeof (idetape_tape_t), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003637 if (tape == NULL) {
3638 printk(KERN_ERR "ide-tape: %s: Can't allocate a tape structure\n", drive->name);
3639 goto failed;
3640 }
3641
3642 g = alloc_disk(1 << PARTN_BITS);
3643 if (!g)
3644 goto out_free_tape;
3645
3646 ide_init_disk(g, drive);
3647
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +02003648 ide_proc_register_driver(drive, &idetape_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003649
Linus Torvalds1da177e2005-04-16 15:20:36 -07003650 kref_init(&tape->kref);
3651
3652 tape->drive = drive;
3653 tape->driver = &idetape_driver;
3654 tape->disk = g;
3655
3656 g->private_data = &tape->driver;
3657
3658 drive->driver_data = tape;
3659
Arjan van de Vencf8b8972006-03-23 03:00:45 -08003660 mutex_lock(&idetape_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003661 for (minor = 0; idetape_devs[minor]; minor++)
3662 ;
3663 idetape_devs[minor] = tape;
Arjan van de Vencf8b8972006-03-23 03:00:45 -08003664 mutex_unlock(&idetape_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003665
3666 idetape_setup(drive, tape, minor);
3667
Tony Jonesdbc12722007-09-25 02:03:03 +02003668 device_create(idetape_sysfs_class, &drive->gendev,
3669 MKDEV(IDETAPE_MAJOR, minor), "%s", tape->name);
3670 device_create(idetape_sysfs_class, &drive->gendev,
3671 MKDEV(IDETAPE_MAJOR, minor + 128), "n%s", tape->name);
Will Dysond5dee802005-09-16 02:55:07 -07003672
Linus Torvalds1da177e2005-04-16 15:20:36 -07003673 g->fops = &idetape_block_ops;
3674 ide_register_region(g);
3675
3676 return 0;
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02003677
Linus Torvalds1da177e2005-04-16 15:20:36 -07003678out_free_tape:
3679 kfree(tape);
3680failed:
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02003681 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003682}
3683
3684MODULE_DESCRIPTION("ATAPI Streaming TAPE Driver");
3685MODULE_LICENSE("GPL");
3686
3687static void __exit idetape_exit (void)
3688{
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02003689 driver_unregister(&idetape_driver.gen_driver);
Will Dysond5dee802005-09-16 02:55:07 -07003690 class_destroy(idetape_sysfs_class);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003691 unregister_chrdev(IDETAPE_MAJOR, "ht");
3692}
3693
Bartlomiej Zolnierkiewicz17514e82005-11-19 22:24:35 +01003694static int __init idetape_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003695{
Will Dysond5dee802005-09-16 02:55:07 -07003696 int error = 1;
3697 idetape_sysfs_class = class_create(THIS_MODULE, "ide_tape");
3698 if (IS_ERR(idetape_sysfs_class)) {
3699 idetape_sysfs_class = NULL;
3700 printk(KERN_ERR "Unable to create sysfs class for ide tapes\n");
3701 error = -EBUSY;
3702 goto out;
3703 }
3704
Linus Torvalds1da177e2005-04-16 15:20:36 -07003705 if (register_chrdev(IDETAPE_MAJOR, "ht", &idetape_fops)) {
3706 printk(KERN_ERR "ide-tape: Failed to register character device interface\n");
Will Dysond5dee802005-09-16 02:55:07 -07003707 error = -EBUSY;
3708 goto out_free_class;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003709 }
Will Dysond5dee802005-09-16 02:55:07 -07003710
3711 error = driver_register(&idetape_driver.gen_driver);
3712 if (error)
3713 goto out_free_driver;
3714
3715 return 0;
3716
3717out_free_driver:
3718 driver_unregister(&idetape_driver.gen_driver);
3719out_free_class:
3720 class_destroy(idetape_sysfs_class);
3721out:
3722 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003723}
3724
Kay Sievers263756e2005-12-12 18:03:44 +01003725MODULE_ALIAS("ide:*m-tape*");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003726module_init(idetape_init);
3727module_exit(idetape_exit);
3728MODULE_ALIAS_CHARDEV_MAJOR(IDETAPE_MAJOR);