blob: d8b02fb0284cc65e9b66bd5564365b97c8fa8163 [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
501/*
502 * The following is used to format the general configuration word of
503 * the ATAPI IDENTIFY DEVICE command.
504 */
505struct idetape_id_gcw {
506 unsigned packet_size :2; /* Packet Size */
507 unsigned reserved234 :3; /* Reserved */
508 unsigned drq_type :2; /* Command packet DRQ type */
509 unsigned removable :1; /* Removable media */
510 unsigned device_type :5; /* Device type */
511 unsigned reserved13 :1; /* Reserved */
512 unsigned protocol :2; /* Protocol type */
513};
514
Borislav Petkovfa366252008-02-02 19:56:51 +0100515/* Structures related to the SELECT SENSE / MODE SENSE packet commands. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516#define IDETAPE_BLOCK_DESCRIPTOR 0
517#define IDETAPE_CAPABILITIES_PAGE 0x2a
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518
519/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100520 * The variables below are used for the character device interface. Additional
521 * state variables are defined in our ide_drive_t structure.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 */
523static struct ide_tape_obj * idetape_devs[MAX_HWIFS * MAX_DRIVES];
524
525#define ide_tape_f(file) ((file)->private_data)
526
527static struct ide_tape_obj *ide_tape_chrdev_get(unsigned int i)
528{
529 struct ide_tape_obj *tape = NULL;
530
Arjan van de Vencf8b8972006-03-23 03:00:45 -0800531 mutex_lock(&idetape_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 tape = idetape_devs[i];
533 if (tape)
534 kref_get(&tape->kref);
Arjan van de Vencf8b8972006-03-23 03:00:45 -0800535 mutex_unlock(&idetape_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 return tape;
537}
538
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539static int idetape_chrdev_release (struct inode *inode, struct file *filp);
540static void idetape_write_release (ide_drive_t *drive, unsigned int minor);
541
542/*
543 * Too bad. The drive wants to send us data which we are not ready to accept.
544 * Just throw it away.
545 */
546static void idetape_discard_data (ide_drive_t *drive, unsigned int bcount)
547{
548 while (bcount--)
549 (void) HWIF(drive)->INB(IDE_DATA_REG);
550}
551
552static void idetape_input_buffers (ide_drive_t *drive, idetape_pc_t *pc, unsigned int bcount)
553{
554 struct idetape_bh *bh = pc->bh;
555 int count;
556
557 while (bcount) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 if (bh == NULL) {
559 printk(KERN_ERR "ide-tape: bh == NULL in "
560 "idetape_input_buffers\n");
561 idetape_discard_data(drive, bcount);
562 return;
563 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 count = min((unsigned int)(bh->b_size - atomic_read(&bh->b_count)), bcount);
565 HWIF(drive)->atapi_input_bytes(drive, bh->b_data + atomic_read(&bh->b_count), count);
566 bcount -= count;
567 atomic_add(count, &bh->b_count);
568 if (atomic_read(&bh->b_count) == bh->b_size) {
569 bh = bh->b_reqnext;
570 if (bh)
571 atomic_set(&bh->b_count, 0);
572 }
573 }
574 pc->bh = bh;
575}
576
577static void idetape_output_buffers (ide_drive_t *drive, idetape_pc_t *pc, unsigned int bcount)
578{
579 struct idetape_bh *bh = pc->bh;
580 int count;
581
582 while (bcount) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 if (bh == NULL) {
584 printk(KERN_ERR "ide-tape: bh == NULL in "
585 "idetape_output_buffers\n");
586 return;
587 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588 count = min((unsigned int)pc->b_count, (unsigned int)bcount);
589 HWIF(drive)->atapi_output_bytes(drive, pc->b_data, count);
590 bcount -= count;
591 pc->b_data += count;
592 pc->b_count -= count;
593 if (!pc->b_count) {
594 pc->bh = bh = bh->b_reqnext;
595 if (bh) {
596 pc->b_data = bh->b_data;
597 pc->b_count = atomic_read(&bh->b_count);
598 }
599 }
600 }
601}
602
603static void idetape_update_buffers (idetape_pc_t *pc)
604{
605 struct idetape_bh *bh = pc->bh;
606 int count;
607 unsigned int bcount = pc->actually_transferred;
608
609 if (test_bit(PC_WRITING, &pc->flags))
610 return;
611 while (bcount) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 if (bh == NULL) {
613 printk(KERN_ERR "ide-tape: bh == NULL in "
614 "idetape_update_buffers\n");
615 return;
616 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 count = min((unsigned int)bh->b_size, (unsigned int)bcount);
618 atomic_set(&bh->b_count, count);
619 if (atomic_read(&bh->b_count) == bh->b_size)
620 bh = bh->b_reqnext;
621 bcount -= count;
622 }
623 pc->bh = bh;
624}
625
626/*
627 * idetape_next_pc_storage returns a pointer to a place in which we can
628 * safely store a packet command, even though we intend to leave the
629 * driver. A storage space for a maximum of IDETAPE_PC_STACK packet
630 * commands is allocated at initialization time.
631 */
632static idetape_pc_t *idetape_next_pc_storage (ide_drive_t *drive)
633{
634 idetape_tape_t *tape = drive->driver_data;
635
Borislav Petkov8004a8c2008-02-06 02:57:51 +0100636 debug_log(DBG_PCRQ_STACK, "pc_stack_index=%d\n", tape->pc_stack_index);
637
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 if (tape->pc_stack_index == IDETAPE_PC_STACK)
639 tape->pc_stack_index=0;
640 return (&tape->pc_stack[tape->pc_stack_index++]);
641}
642
643/*
644 * idetape_next_rq_storage is used along with idetape_next_pc_storage.
645 * Since we queue packet commands in the request queue, we need to
646 * allocate a request, along with the allocation of a packet command.
647 */
648
649/**************************************************************
650 * *
651 * This should get fixed to use kmalloc(.., GFP_ATOMIC) *
652 * followed later on by kfree(). -ml *
653 * *
654 **************************************************************/
655
656static struct request *idetape_next_rq_storage (ide_drive_t *drive)
657{
658 idetape_tape_t *tape = drive->driver_data;
659
Borislav Petkov8004a8c2008-02-06 02:57:51 +0100660 debug_log(DBG_PCRQ_STACK, "rq_stack_index=%d\n", tape->rq_stack_index);
661
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662 if (tape->rq_stack_index == IDETAPE_PC_STACK)
663 tape->rq_stack_index=0;
664 return (&tape->rq_stack[tape->rq_stack_index++]);
665}
666
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667static void idetape_init_pc (idetape_pc_t *pc)
668{
669 memset(pc->c, 0, 12);
670 pc->retries = 0;
671 pc->flags = 0;
672 pc->request_transfer = 0;
673 pc->buffer = pc->pc_buffer;
674 pc->buffer_size = IDETAPE_PC_BUFFER_SIZE;
675 pc->bh = NULL;
676 pc->b_data = NULL;
677}
678
679/*
Borislav Petkov1b5db432008-02-02 19:56:48 +0100680 * called on each failed packet command retry to analyze the request sense. We
681 * currently do not utilize this information.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 */
Borislav Petkov1b5db432008-02-02 19:56:48 +0100683static void idetape_analyze_error(ide_drive_t *drive, u8 *sense)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684{
685 idetape_tape_t *tape = drive->driver_data;
686 idetape_pc_t *pc = tape->failed_pc;
687
Borislav Petkov1b5db432008-02-02 19:56:48 +0100688 tape->sense_key = sense[2] & 0xF;
689 tape->asc = sense[12];
690 tape->ascq = sense[13];
Borislav Petkov8004a8c2008-02-06 02:57:51 +0100691
692 debug_log(DBG_ERR, "pc = %x, sense key = %x, asc = %x, ascq = %x\n",
693 pc->c[0], tape->sense_key, tape->asc, tape->ascq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694
Borislav Petkov1b5db432008-02-02 19:56:48 +0100695 /* Correct pc->actually_transferred by asking the tape. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 if (test_bit(PC_DMA_ERROR, &pc->flags)) {
Borislav Petkov1b5db432008-02-02 19:56:48 +0100697 pc->actually_transferred = pc->request_transfer -
Borislav Petkov54bb2072008-02-06 02:57:52 +0100698 tape->blk_size *
Borislav Petkov860ff5e2008-02-02 19:56:50 +0100699 be32_to_cpu(get_unaligned((u32 *)&sense[3]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 idetape_update_buffers(pc);
701 }
702
703 /*
704 * If error was the result of a zero-length read or write command,
705 * with sense key=5, asc=0x22, ascq=0, let it slide. Some drives
706 * (i.e. Seagate STT3401A Travan) don't support 0-length read/writes.
707 */
Borislav Petkov90699ce2008-02-02 19:56:50 +0100708 if ((pc->c[0] == READ_6 || pc->c[0] == WRITE_6)
Borislav Petkov1b5db432008-02-02 19:56:48 +0100709 /* length == 0 */
710 && pc->c[4] == 0 && pc->c[3] == 0 && pc->c[2] == 0) {
711 if (tape->sense_key == 5) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712 /* don't report an error, everything's ok */
713 pc->error = 0;
714 /* don't retry read/write */
715 set_bit(PC_ABORT, &pc->flags);
716 }
717 }
Borislav Petkov90699ce2008-02-02 19:56:50 +0100718 if (pc->c[0] == READ_6 && (sense[2] & 0x80)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719 pc->error = IDETAPE_ERROR_FILEMARK;
720 set_bit(PC_ABORT, &pc->flags);
721 }
Borislav Petkov90699ce2008-02-02 19:56:50 +0100722 if (pc->c[0] == WRITE_6) {
Borislav Petkov1b5db432008-02-02 19:56:48 +0100723 if ((sense[2] & 0x40) || (tape->sense_key == 0xd
724 && tape->asc == 0x0 && tape->ascq == 0x2)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 pc->error = IDETAPE_ERROR_EOD;
726 set_bit(PC_ABORT, &pc->flags);
727 }
728 }
Borislav Petkov90699ce2008-02-02 19:56:50 +0100729 if (pc->c[0] == READ_6 || pc->c[0] == WRITE_6) {
Borislav Petkov1b5db432008-02-02 19:56:48 +0100730 if (tape->sense_key == 8) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 pc->error = IDETAPE_ERROR_EOD;
732 set_bit(PC_ABORT, &pc->flags);
733 }
734 if (!test_bit(PC_ABORT, &pc->flags) &&
735 pc->actually_transferred)
736 pc->retries = IDETAPE_MAX_PC_RETRIES + 1;
737 }
738}
739
Borislav Petkov419d4742008-02-02 19:56:51 +0100740static void idetape_activate_next_stage(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741{
742 idetape_tape_t *tape = drive->driver_data;
743 idetape_stage_t *stage = tape->next_stage;
744 struct request *rq = &stage->rq;
745
Borislav Petkov8004a8c2008-02-06 02:57:51 +0100746 debug_log(DBG_PROCS, "Enter %s\n", __func__);
747
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748 if (stage == NULL) {
Borislav Petkov8004a8c2008-02-06 02:57:51 +0100749 printk(KERN_ERR "ide-tape: bug: Trying to activate a non"
750 " existing stage\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751 return;
752 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753
754 rq->rq_disk = tape->disk;
755 rq->buffer = NULL;
756 rq->special = (void *)stage->bh;
Borislav Petkov54bb2072008-02-06 02:57:52 +0100757 tape->active_data_rq = rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758 tape->active_stage = stage;
759 tape->next_stage = stage->next;
760}
761
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100762/* Free a stage along with its related buffers completely. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763static void __idetape_kfree_stage (idetape_stage_t *stage)
764{
765 struct idetape_bh *prev_bh, *bh = stage->bh;
766 int size;
767
768 while (bh != NULL) {
769 if (bh->b_data != NULL) {
770 size = (int) bh->b_size;
771 while (size > 0) {
772 free_page((unsigned long) bh->b_data);
773 size -= PAGE_SIZE;
774 bh->b_data += PAGE_SIZE;
775 }
776 }
777 prev_bh = bh;
778 bh = bh->b_reqnext;
779 kfree(prev_bh);
780 }
781 kfree(stage);
782}
783
784static void idetape_kfree_stage (idetape_tape_t *tape, idetape_stage_t *stage)
785{
786 __idetape_kfree_stage(stage);
787}
788
789/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100790 * Remove tape->first_stage from the pipeline. The caller should avoid race
791 * conditions.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792 */
793static void idetape_remove_stage_head (ide_drive_t *drive)
794{
795 idetape_tape_t *tape = drive->driver_data;
796 idetape_stage_t *stage;
Borislav Petkov8004a8c2008-02-06 02:57:51 +0100797
798 debug_log(DBG_PROCS, "Enter %s\n", __func__);
799
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800 if (tape->first_stage == NULL) {
801 printk(KERN_ERR "ide-tape: bug: tape->first_stage is NULL\n");
Borislav Petkov55a5d292008-02-02 19:56:49 +0100802 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803 }
804 if (tape->active_stage == tape->first_stage) {
Borislav Petkov8004a8c2008-02-06 02:57:51 +0100805 printk(KERN_ERR "ide-tape: bug: Trying to free our active "
806 "pipeline stage\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807 return;
808 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809 stage = tape->first_stage;
810 tape->first_stage = stage->next;
811 idetape_kfree_stage(tape, stage);
812 tape->nr_stages--;
813 if (tape->first_stage == NULL) {
814 tape->last_stage = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815 if (tape->next_stage != NULL)
816 printk(KERN_ERR "ide-tape: bug: tape->next_stage != NULL\n");
817 if (tape->nr_stages)
818 printk(KERN_ERR "ide-tape: bug: nr_stages should be 0 now\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819 }
820}
821
822/*
823 * This will free all the pipeline stages starting from new_last_stage->next
824 * to the end of the list, and point tape->last_stage to new_last_stage.
825 */
826static void idetape_abort_pipeline(ide_drive_t *drive,
827 idetape_stage_t *new_last_stage)
828{
829 idetape_tape_t *tape = drive->driver_data;
830 idetape_stage_t *stage = new_last_stage->next;
831 idetape_stage_t *nstage;
832
Borislav Petkov8004a8c2008-02-06 02:57:51 +0100833 debug_log(DBG_PROCS, "%s: Enter %s\n", tape->name, __func__);
834
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835 while (stage) {
836 nstage = stage->next;
837 idetape_kfree_stage(tape, stage);
838 --tape->nr_stages;
839 --tape->nr_pending_stages;
840 stage = nstage;
841 }
842 if (new_last_stage)
843 new_last_stage->next = NULL;
844 tape->last_stage = new_last_stage;
845 tape->next_stage = NULL;
846}
847
848/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100849 * Finish servicing a request and insert a pending pipeline request into the
850 * main device queue.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851 */
852static int idetape_end_request(ide_drive_t *drive, int uptodate, int nr_sects)
853{
854 struct request *rq = HWGROUP(drive)->rq;
855 idetape_tape_t *tape = drive->driver_data;
856 unsigned long flags;
857 int error;
858 int remove_stage = 0;
859 idetape_stage_t *active_stage;
860
Borislav Petkov8004a8c2008-02-06 02:57:51 +0100861 debug_log(DBG_PROCS, "Enter %s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862
863 switch (uptodate) {
864 case 0: error = IDETAPE_ERROR_GENERAL; break;
865 case 1: error = 0; break;
866 default: error = uptodate;
867 }
868 rq->errors = error;
869 if (error)
870 tape->failed_pc = NULL;
871
Bartlomiej Zolnierkiewicz36872212008-01-26 20:13:10 +0100872 if (!blk_special_request(rq)) {
873 ide_end_request(drive, uptodate, nr_sects);
874 return 0;
875 }
876
Borislav Petkov54bb2072008-02-06 02:57:52 +0100877 spin_lock_irqsave(&tape->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878
879 /* The request was a pipelined data transfer request */
Borislav Petkov54bb2072008-02-06 02:57:52 +0100880 if (tape->active_data_rq == rq) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881 active_stage = tape->active_stage;
882 tape->active_stage = NULL;
Borislav Petkov54bb2072008-02-06 02:57:52 +0100883 tape->active_data_rq = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884 tape->nr_pending_stages--;
885 if (rq->cmd[0] & REQ_IDETAPE_WRITE) {
886 remove_stage = 1;
887 if (error) {
888 set_bit(IDETAPE_PIPELINE_ERROR, &tape->flags);
889 if (error == IDETAPE_ERROR_EOD)
890 idetape_abort_pipeline(drive, active_stage);
891 }
892 } else if (rq->cmd[0] & REQ_IDETAPE_READ) {
893 if (error == IDETAPE_ERROR_EOD) {
894 set_bit(IDETAPE_PIPELINE_ERROR, &tape->flags);
895 idetape_abort_pipeline(drive, active_stage);
896 }
897 }
898 if (tape->next_stage != NULL) {
Borislav Petkov419d4742008-02-02 19:56:51 +0100899 idetape_activate_next_stage(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100901 /* Insert the next request into the request queue. */
Borislav Petkov54bb2072008-02-06 02:57:52 +0100902 (void)ide_do_drive_cmd(drive, tape->active_data_rq,
903 ide_end);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904 } else if (!error) {
Borislav Petkov97219852008-02-06 02:57:52 +0100905 /*
906 * This is a part of the feedback loop which tries to
907 * find the optimum number of stages. We are starting
908 * from a minimum maximum number of stages, and if we
909 * sense that the pipeline is empty, we try to increase
910 * it, until we reach the user compile time memory
911 * limit.
912 */
913 int i = (tape->max_pipeline - tape->min_pipeline) / 10;
914
915 tape->max_stages += max(i, 1);
916 tape->max_stages = max(tape->max_stages,
917 tape->min_pipeline);
918 tape->max_stages = min(tape->max_stages,
919 tape->max_pipeline);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920 }
921 }
922 ide_end_drive_cmd(drive, 0, 0);
923// blkdev_dequeue_request(rq);
924// drive->rq = NULL;
925// end_that_request_last(rq);
926
927 if (remove_stage)
928 idetape_remove_stage_head(drive);
Borislav Petkov54bb2072008-02-06 02:57:52 +0100929 if (tape->active_data_rq == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930 clear_bit(IDETAPE_PIPELINE_ACTIVE, &tape->flags);
Borislav Petkov54bb2072008-02-06 02:57:52 +0100931 spin_unlock_irqrestore(&tape->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932 return 0;
933}
934
935static ide_startstop_t idetape_request_sense_callback (ide_drive_t *drive)
936{
937 idetape_tape_t *tape = drive->driver_data;
938
Borislav Petkov8004a8c2008-02-06 02:57:51 +0100939 debug_log(DBG_PROCS, "Enter %s\n", __func__);
940
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941 if (!tape->pc->error) {
Borislav Petkov1b5db432008-02-02 19:56:48 +0100942 idetape_analyze_error(drive, tape->pc->buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943 idetape_end_request(drive, 1, 0);
944 } else {
945 printk(KERN_ERR "ide-tape: Error in REQUEST SENSE itself - Aborting request!\n");
946 idetape_end_request(drive, 0, 0);
947 }
948 return ide_stopped;
949}
950
951static void idetape_create_request_sense_cmd (idetape_pc_t *pc)
952{
953 idetape_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +0100954 pc->c[0] = REQUEST_SENSE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955 pc->c[4] = 20;
956 pc->request_transfer = 20;
957 pc->callback = &idetape_request_sense_callback;
958}
959
960static void idetape_init_rq(struct request *rq, u8 cmd)
961{
962 memset(rq, 0, sizeof(*rq));
Jens Axboe4aff5e22006-08-10 08:44:47 +0200963 rq->cmd_type = REQ_TYPE_SPECIAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964 rq->cmd[0] = cmd;
965}
966
967/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100968 * Generate a new packet command request in front of the request queue, before
969 * the current request, so that it will be processed immediately, on the next
970 * pass through the driver. The function below is called from the request
971 * handling part of the driver (the "bottom" part). Safe storage for the request
972 * should be allocated with ide_tape_next_{pc,rq}_storage() prior to that.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100974 * Memory for those requests is pre-allocated at initialization time, and is
975 * limited to IDETAPE_PC_STACK requests. We assume that we have enough space for
976 * the maximum possible number of inter-dependent packet commands.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100978 * The higher level of the driver - The ioctl handler and the character device
979 * handling functions should queue request to the lower level part and wait for
980 * their completion using idetape_queue_pc_tail or idetape_queue_rw_tail.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981 */
982static void idetape_queue_pc_head (ide_drive_t *drive, idetape_pc_t *pc,struct request *rq)
983{
984 struct ide_tape_obj *tape = drive->driver_data;
985
986 idetape_init_rq(rq, REQ_IDETAPE_PC1);
987 rq->buffer = (char *) pc;
988 rq->rq_disk = tape->disk;
989 (void) ide_do_drive_cmd(drive, rq, ide_preempt);
990}
991
992/*
993 * idetape_retry_pc is called when an error was detected during the
994 * last packet command. We queue a request sense packet command in
995 * the head of the request list.
996 */
997static ide_startstop_t idetape_retry_pc (ide_drive_t *drive)
998{
999 idetape_tape_t *tape = drive->driver_data;
1000 idetape_pc_t *pc;
1001 struct request *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002
Bartlomiej Zolnierkiewicz64a57fe2008-02-06 02:57:51 +01001003 (void)ide_read_error(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004 pc = idetape_next_pc_storage(drive);
1005 rq = idetape_next_rq_storage(drive);
1006 idetape_create_request_sense_cmd(pc);
1007 set_bit(IDETAPE_IGNORE_DSC, &tape->flags);
1008 idetape_queue_pc_head(drive, pc, rq);
1009 return ide_stopped;
1010}
1011
1012/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001013 * Postpone the current request so that ide.c will be able to service requests
1014 * from another device on the same hwgroup while we are polling for DSC.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015 */
1016static void idetape_postpone_request (ide_drive_t *drive)
1017{
1018 idetape_tape_t *tape = drive->driver_data;
1019
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001020 debug_log(DBG_PROCS, "Enter %s\n", __func__);
1021
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022 tape->postponed_rq = HWGROUP(drive)->rq;
Borislav Petkov54bb2072008-02-06 02:57:52 +01001023 ide_stall_queue(drive, tape->dsc_poll_freq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024}
1025
Borislav Petkova1efc852008-02-06 02:57:52 +01001026typedef void idetape_io_buf(ide_drive_t *, idetape_pc_t *, unsigned int);
1027
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028/*
Borislav Petkova1efc852008-02-06 02:57:52 +01001029 * This is the usual interrupt handler which will be called during a packet
1030 * command. We will transfer some of the data (as requested by the drive) and
1031 * will re-point interrupt handler to us. When data transfer is finished, we
1032 * will act according to the algorithm described before
Borislav Petkov8d06bfa2008-02-06 02:57:53 +01001033 * idetape_issue_pc.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034 */
Borislav Petkova1efc852008-02-06 02:57:52 +01001035static ide_startstop_t idetape_pc_intr(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036{
1037 ide_hwif_t *hwif = drive->hwif;
1038 idetape_tape_t *tape = drive->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039 idetape_pc_t *pc = tape->pc;
Borislav Petkova1efc852008-02-06 02:57:52 +01001040 xfer_func_t *xferfunc;
1041 idetape_io_buf *iobuf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042 unsigned int temp;
1043#if SIMULATE_ERRORS
1044 static int error_sim_count = 0;
1045#endif
Bartlomiej Zolnierkiewicz790d1232008-01-25 22:17:12 +01001046 u16 bcount;
Bartlomiej Zolnierkiewicz8e7657a2008-01-25 22:17:12 +01001047 u8 stat, ireason;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001049 debug_log(DBG_PROCS, "Enter %s - interrupt handler\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050
1051 /* Clear the interrupt */
Bartlomiej Zolnierkiewiczc47137a2008-02-06 02:57:51 +01001052 stat = ide_read_status(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053
1054 if (test_bit(PC_DMA_IN_PROGRESS, &pc->flags)) {
Bartlomiej Zolnierkiewicz22c525b2008-01-25 22:17:11 +01001055 if (hwif->ide_dma_end(drive) || (stat & ERR_STAT)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056 /*
1057 * A DMA error is sometimes expected. For example,
1058 * if the tape is crossing a filemark during a
1059 * READ command, it will issue an irq and position
1060 * itself before the filemark, so that only a partial
1061 * data transfer will occur (which causes the DMA
1062 * error). In that case, we will later ask the tape
1063 * how much bytes of the original request were
1064 * actually transferred (we can't receive that
1065 * information from the DMA engine on most chipsets).
1066 */
1067
1068 /*
1069 * On the contrary, a DMA error is never expected;
1070 * it usually indicates a hardware error or abort.
1071 * If the tape crosses a filemark during a READ
1072 * command, it will issue an irq and position itself
1073 * after the filemark (not before). Only a partial
1074 * data transfer will occur, but no DMA error.
1075 * (AS, 19 Apr 2001)
1076 */
1077 set_bit(PC_DMA_ERROR, &pc->flags);
1078 } else {
1079 pc->actually_transferred = pc->request_transfer;
1080 idetape_update_buffers(pc);
1081 }
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001082 debug_log(DBG_PROCS, "DMA finished\n");
1083
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084 }
1085
1086 /* No more interrupts */
Bartlomiej Zolnierkiewicz22c525b2008-01-25 22:17:11 +01001087 if ((stat & DRQ_STAT) == 0) {
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001088 debug_log(DBG_SENSE, "Packet command completed, %d bytes"
1089 " transferred\n", pc->actually_transferred);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001091 clear_bit(PC_DMA_IN_PROGRESS, &pc->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092 local_irq_enable();
1093
1094#if SIMULATE_ERRORS
Borislav Petkov90699ce2008-02-02 19:56:50 +01001095 if ((pc->c[0] == WRITE_6 || pc->c[0] == READ_6) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096 (++error_sim_count % 100) == 0) {
1097 printk(KERN_INFO "ide-tape: %s: simulating error\n",
1098 tape->name);
Bartlomiej Zolnierkiewicz22c525b2008-01-25 22:17:11 +01001099 stat |= ERR_STAT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100 }
1101#endif
Borislav Petkov90699ce2008-02-02 19:56:50 +01001102 if ((stat & ERR_STAT) && pc->c[0] == REQUEST_SENSE)
Bartlomiej Zolnierkiewicz22c525b2008-01-25 22:17:11 +01001103 stat &= ~ERR_STAT;
1104 if ((stat & ERR_STAT) || test_bit(PC_DMA_ERROR, &pc->flags)) {
1105 /* Error detected */
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001106 debug_log(DBG_ERR, "%s: I/O error\n", tape->name);
1107
Borislav Petkov90699ce2008-02-02 19:56:50 +01001108 if (pc->c[0] == REQUEST_SENSE) {
Borislav Petkova1efc852008-02-06 02:57:52 +01001109 printk(KERN_ERR "ide-tape: I/O error in request"
1110 " sense command\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111 return ide_do_reset(drive);
1112 }
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001113 debug_log(DBG_ERR, "[cmd %x]: check condition\n",
1114 pc->c[0]);
1115
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116 /* Retry operation */
1117 return idetape_retry_pc(drive);
1118 }
1119 pc->error = 0;
1120 if (test_bit(PC_WAIT_FOR_DSC, &pc->flags) &&
Bartlomiej Zolnierkiewicz22c525b2008-01-25 22:17:11 +01001121 (stat & SEEK_STAT) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122 /* Media access command */
1123 tape->dsc_polling_start = jiffies;
Borislav Petkov54bb2072008-02-06 02:57:52 +01001124 tape->dsc_poll_freq = IDETAPE_DSC_MA_FAST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125 tape->dsc_timeout = jiffies + IDETAPE_DSC_MA_TIMEOUT;
1126 /* Allow ide.c to handle other requests */
1127 idetape_postpone_request(drive);
1128 return ide_stopped;
1129 }
1130 if (tape->failed_pc == pc)
1131 tape->failed_pc = NULL;
1132 /* Command finished - Call the callback function */
1133 return pc->callback(drive);
1134 }
1135 if (test_and_clear_bit(PC_DMA_IN_PROGRESS, &pc->flags)) {
1136 printk(KERN_ERR "ide-tape: The tape wants to issue more "
1137 "interrupts in DMA mode\n");
1138 printk(KERN_ERR "ide-tape: DMA disabled, reverting to PIO\n");
Bartlomiej Zolnierkiewicz7469aaf2007-02-17 02:40:26 +01001139 ide_dma_off(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140 return ide_do_reset(drive);
1141 }
1142 /* Get the number of bytes to transfer on this interrupt. */
Bartlomiej Zolnierkiewicz790d1232008-01-25 22:17:12 +01001143 bcount = (hwif->INB(IDE_BCOUNTH_REG) << 8) |
1144 hwif->INB(IDE_BCOUNTL_REG);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145
Bartlomiej Zolnierkiewicz8e7657a2008-01-25 22:17:12 +01001146 ireason = hwif->INB(IDE_IREASON_REG);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147
Bartlomiej Zolnierkiewicz8e7657a2008-01-25 22:17:12 +01001148 if (ireason & CD) {
Borislav Petkova1efc852008-02-06 02:57:52 +01001149 printk(KERN_ERR "ide-tape: CoD != 0 in %s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150 return ide_do_reset(drive);
1151 }
Bartlomiej Zolnierkiewicz8e7657a2008-01-25 22:17:12 +01001152 if (((ireason & IO) == IO) == test_bit(PC_WRITING, &pc->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153 /* Hopefully, we will never get here */
1154 printk(KERN_ERR "ide-tape: We wanted to %s, ",
Bartlomiej Zolnierkiewicz8e7657a2008-01-25 22:17:12 +01001155 (ireason & IO) ? "Write" : "Read");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156 printk(KERN_ERR "ide-tape: but the tape wants us to %s !\n",
Bartlomiej Zolnierkiewicz8e7657a2008-01-25 22:17:12 +01001157 (ireason & IO) ? "Read" : "Write");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158 return ide_do_reset(drive);
1159 }
1160 if (!test_bit(PC_WRITING, &pc->flags)) {
1161 /* Reading - Check that we have enough space */
Bartlomiej Zolnierkiewicz790d1232008-01-25 22:17:12 +01001162 temp = pc->actually_transferred + bcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163 if (temp > pc->request_transfer) {
1164 if (temp > pc->buffer_size) {
Borislav Petkova1efc852008-02-06 02:57:52 +01001165 printk(KERN_ERR "ide-tape: The tape wants to "
1166 "send us more data than expected "
1167 "- discarding data\n");
Bartlomiej Zolnierkiewicz790d1232008-01-25 22:17:12 +01001168 idetape_discard_data(drive, bcount);
Borislav Petkova1efc852008-02-06 02:57:52 +01001169 ide_set_handler(drive, &idetape_pc_intr,
1170 IDETAPE_WAIT_CMD, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171 return ide_started;
1172 }
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001173 debug_log(DBG_SENSE, "The tape wants to send us more "
1174 "data than expected - allowing transfer\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175 }
Borislav Petkova1efc852008-02-06 02:57:52 +01001176 iobuf = &idetape_input_buffers;
1177 xferfunc = hwif->atapi_input_bytes;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178 } else {
Borislav Petkova1efc852008-02-06 02:57:52 +01001179 iobuf = &idetape_output_buffers;
1180 xferfunc = hwif->atapi_output_bytes;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181 }
Borislav Petkova1efc852008-02-06 02:57:52 +01001182
1183 if (pc->bh)
1184 iobuf(drive, pc, bcount);
1185 else
1186 xferfunc(drive, pc->current_position, bcount);
1187
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188 /* Update the current position */
Bartlomiej Zolnierkiewicz790d1232008-01-25 22:17:12 +01001189 pc->actually_transferred += bcount;
1190 pc->current_position += bcount;
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001191
1192 debug_log(DBG_SENSE, "[cmd %x] transferred %d bytes on that intr.\n",
1193 pc->c[0], bcount);
1194
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195 /* And set the interrupt handler again */
1196 ide_set_handler(drive, &idetape_pc_intr, IDETAPE_WAIT_CMD, NULL);
1197 return ide_started;
1198}
1199
1200/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001201 * Packet Command Interface
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001203 * The current Packet Command is available in tape->pc, and will not change
1204 * until we finish handling it. Each packet command is associated with a
1205 * callback function that will be called when the command is finished.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001207 * The handling will be done in three stages:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001209 * 1. idetape_issue_pc will send the packet command to the drive, and will set
1210 * the interrupt handler to idetape_pc_intr.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001212 * 2. On each interrupt, idetape_pc_intr will be called. This step will be
1213 * repeated until the device signals us that no more interrupts will be issued.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001215 * 3. ATAPI Tape media access commands have immediate status with a delayed
1216 * process. In case of a successful initiation of a media access packet command,
1217 * the DSC bit will be set when the actual execution of the command is finished.
1218 * Since the tape drive will not issue an interrupt, we have to poll for this
1219 * event. In this case, we define the request as "low priority request" by
1220 * setting rq_status to IDETAPE_RQ_POSTPONED, set a timer to poll for DSC and
1221 * exit the driver.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001223 * ide.c will then give higher priority to requests which originate from the
1224 * other device, until will change rq_status to RQ_ACTIVE.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001226 * 4. When the packet command is finished, it will be checked for errors.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001228 * 5. In case an error was found, we queue a request sense packet command in
1229 * front of the request queue and retry the operation up to
1230 * IDETAPE_MAX_PC_RETRIES times.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001232 * 6. In case no error was found, or we decided to give up and not to retry
1233 * again, the callback function will be called and then we will handle the next
1234 * request.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235 */
1236static ide_startstop_t idetape_transfer_pc(ide_drive_t *drive)
1237{
1238 ide_hwif_t *hwif = drive->hwif;
1239 idetape_tape_t *tape = drive->driver_data;
1240 idetape_pc_t *pc = tape->pc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241 int retries = 100;
1242 ide_startstop_t startstop;
Bartlomiej Zolnierkiewicz8e7657a2008-01-25 22:17:12 +01001243 u8 ireason;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244
1245 if (ide_wait_stat(&startstop,drive,DRQ_STAT,BUSY_STAT,WAIT_READY)) {
1246 printk(KERN_ERR "ide-tape: Strange, packet command initiated yet DRQ isn't asserted\n");
1247 return startstop;
1248 }
Bartlomiej Zolnierkiewicz8e7657a2008-01-25 22:17:12 +01001249 ireason = hwif->INB(IDE_IREASON_REG);
1250 while (retries-- && ((ireason & CD) == 0 || (ireason & IO))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251 printk(KERN_ERR "ide-tape: (IO,CoD != (0,1) while issuing "
1252 "a packet command, retrying\n");
1253 udelay(100);
Bartlomiej Zolnierkiewicz8e7657a2008-01-25 22:17:12 +01001254 ireason = hwif->INB(IDE_IREASON_REG);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255 if (retries == 0) {
1256 printk(KERN_ERR "ide-tape: (IO,CoD != (0,1) while "
1257 "issuing a packet command, ignoring\n");
Bartlomiej Zolnierkiewicz8e7657a2008-01-25 22:17:12 +01001258 ireason |= CD;
1259 ireason &= ~IO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260 }
1261 }
Bartlomiej Zolnierkiewicz8e7657a2008-01-25 22:17:12 +01001262 if ((ireason & CD) == 0 || (ireason & IO)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263 printk(KERN_ERR "ide-tape: (IO,CoD) != (0,1) while issuing "
1264 "a packet command\n");
1265 return ide_do_reset(drive);
1266 }
1267 /* Set the interrupt routine */
1268 ide_set_handler(drive, &idetape_pc_intr, IDETAPE_WAIT_CMD, NULL);
1269#ifdef CONFIG_BLK_DEV_IDEDMA
1270 /* Begin DMA, if necessary */
1271 if (test_bit(PC_DMA_IN_PROGRESS, &pc->flags))
1272 hwif->dma_start(drive);
1273#endif
1274 /* Send the actual packet */
1275 HWIF(drive)->atapi_output_bytes(drive, pc->c, 12);
1276 return ide_started;
1277}
1278
Borislav Petkov8d06bfa2008-02-06 02:57:53 +01001279static ide_startstop_t idetape_issue_pc(ide_drive_t *drive, idetape_pc_t *pc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280{
1281 ide_hwif_t *hwif = drive->hwif;
1282 idetape_tape_t *tape = drive->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283 int dma_ok = 0;
Bartlomiej Zolnierkiewicz790d1232008-01-25 22:17:12 +01001284 u16 bcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285
Borislav Petkov90699ce2008-02-02 19:56:50 +01001286 if (tape->pc->c[0] == REQUEST_SENSE &&
1287 pc->c[0] == REQUEST_SENSE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288 printk(KERN_ERR "ide-tape: possible ide-tape.c bug - "
1289 "Two request sense in serial were issued\n");
1290 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291
Borislav Petkov90699ce2008-02-02 19:56:50 +01001292 if (tape->failed_pc == NULL && pc->c[0] != REQUEST_SENSE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293 tape->failed_pc = pc;
1294 /* Set the current packet command */
1295 tape->pc = pc;
1296
1297 if (pc->retries > IDETAPE_MAX_PC_RETRIES ||
1298 test_bit(PC_ABORT, &pc->flags)) {
1299 /*
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001300 * We will "abort" retrying a packet command in case legitimate
1301 * error code was received (crossing a filemark, or end of the
1302 * media, for example).
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303 */
1304 if (!test_bit(PC_ABORT, &pc->flags)) {
Borislav Petkov90699ce2008-02-02 19:56:50 +01001305 if (!(pc->c[0] == TEST_UNIT_READY &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306 tape->sense_key == 2 && tape->asc == 4 &&
1307 (tape->ascq == 1 || tape->ascq == 8))) {
1308 printk(KERN_ERR "ide-tape: %s: I/O error, "
1309 "pc = %2x, key = %2x, "
1310 "asc = %2x, ascq = %2x\n",
1311 tape->name, pc->c[0],
1312 tape->sense_key, tape->asc,
1313 tape->ascq);
1314 }
1315 /* Giving up */
1316 pc->error = IDETAPE_ERROR_GENERAL;
1317 }
1318 tape->failed_pc = NULL;
1319 return pc->callback(drive);
1320 }
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001321 debug_log(DBG_SENSE, "Retry #%d, cmd = %02X\n", pc->retries, pc->c[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322
1323 pc->retries++;
1324 /* We haven't transferred any data yet */
1325 pc->actually_transferred = 0;
1326 pc->current_position = pc->buffer;
1327 /* Request to transfer the entire buffer at once */
Bartlomiej Zolnierkiewicz790d1232008-01-25 22:17:12 +01001328 bcount = pc->request_transfer;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329
1330 if (test_and_clear_bit(PC_DMA_ERROR, &pc->flags)) {
1331 printk(KERN_WARNING "ide-tape: DMA disabled, "
1332 "reverting to PIO\n");
Bartlomiej Zolnierkiewicz7469aaf2007-02-17 02:40:26 +01001333 ide_dma_off(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334 }
1335 if (test_bit(PC_DMA_RECOMMENDED, &pc->flags) && drive->using_dma)
1336 dma_ok = !hwif->dma_setup(drive);
1337
Bartlomiej Zolnierkiewicz2fc57382008-01-25 22:17:13 +01001338 ide_pktcmd_tf_load(drive, IDE_TFLAG_NO_SELECT_MASK |
1339 IDE_TFLAG_OUT_DEVICE, bcount, dma_ok);
1340
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341 if (dma_ok) /* Will begin DMA later */
1342 set_bit(PC_DMA_IN_PROGRESS, &pc->flags);
1343 if (test_bit(IDETAPE_DRQ_INTERRUPT, &tape->flags)) {
Bartlomiej Zolnierkiewiczc1c9dbc2008-02-02 19:56:44 +01001344 ide_execute_command(drive, WIN_PACKETCMD, &idetape_transfer_pc,
1345 IDETAPE_WAIT_CMD, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346 return ide_started;
1347 } else {
1348 hwif->OUTB(WIN_PACKETCMD, IDE_COMMAND_REG);
1349 return idetape_transfer_pc(drive);
1350 }
1351}
1352
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353static ide_startstop_t idetape_pc_callback (ide_drive_t *drive)
1354{
1355 idetape_tape_t *tape = drive->driver_data;
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001356
1357 debug_log(DBG_PROCS, "Enter %s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358
1359 idetape_end_request(drive, tape->pc->error ? 0 : 1, 0);
1360 return ide_stopped;
1361}
1362
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001363/* A mode sense command is used to "sense" tape parameters. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364static void idetape_create_mode_sense_cmd (idetape_pc_t *pc, u8 page_code)
1365{
1366 idetape_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +01001367 pc->c[0] = MODE_SENSE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368 if (page_code != IDETAPE_BLOCK_DESCRIPTOR)
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001369 /* DBD = 1 - Don't return block descriptors */
1370 pc->c[1] = 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371 pc->c[2] = page_code;
1372 /*
1373 * Changed pc->c[3] to 0 (255 will at best return unused info).
1374 *
1375 * For SCSI this byte is defined as subpage instead of high byte
1376 * of length and some IDE drives seem to interpret it this way
1377 * and return an error when 255 is used.
1378 */
1379 pc->c[3] = 0;
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001380 /* We will just discard data in that case */
1381 pc->c[4] = 255;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382 if (page_code == IDETAPE_BLOCK_DESCRIPTOR)
1383 pc->request_transfer = 12;
1384 else if (page_code == IDETAPE_CAPABILITIES_PAGE)
1385 pc->request_transfer = 24;
1386 else
1387 pc->request_transfer = 50;
1388 pc->callback = &idetape_pc_callback;
1389}
1390
Borislav Petkov37016ba2008-02-06 02:57:52 +01001391static void idetape_calculate_speeds(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392{
1393 idetape_tape_t *tape = drive->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394
1395 if (time_after(jiffies, tape->controlled_pipeline_head_time + 120 * HZ)) {
1396 tape->controlled_previous_pipeline_head = tape->controlled_last_pipeline_head;
1397 tape->controlled_previous_head_time = tape->controlled_pipeline_head_time;
1398 tape->controlled_last_pipeline_head = tape->pipeline_head;
1399 tape->controlled_pipeline_head_time = jiffies;
1400 }
1401 if (time_after(jiffies, tape->controlled_pipeline_head_time + 60 * HZ))
1402 tape->controlled_pipeline_head_speed = (tape->pipeline_head - tape->controlled_last_pipeline_head) * 32 * HZ / (jiffies - tape->controlled_pipeline_head_time);
1403 else if (time_after(jiffies, tape->controlled_previous_head_time))
1404 tape->controlled_pipeline_head_speed = (tape->pipeline_head - tape->controlled_previous_pipeline_head) * 32 * HZ / (jiffies - tape->controlled_previous_head_time);
1405
1406 if (tape->nr_pending_stages < tape->max_stages /*- 1 */) {
1407 /* -1 for read mode error recovery */
1408 if (time_after(jiffies, tape->uncontrolled_previous_head_time + 10 * HZ)) {
1409 tape->uncontrolled_pipeline_head_time = jiffies;
1410 tape->uncontrolled_pipeline_head_speed = (tape->pipeline_head - tape->uncontrolled_previous_pipeline_head) * 32 * HZ / (jiffies - tape->uncontrolled_previous_head_time);
1411 }
1412 } else {
1413 tape->uncontrolled_previous_head_time = jiffies;
1414 tape->uncontrolled_previous_pipeline_head = tape->pipeline_head;
1415 if (time_after(jiffies, tape->uncontrolled_pipeline_head_time + 30 * HZ)) {
1416 tape->uncontrolled_pipeline_head_time = jiffies;
1417 }
1418 }
1419 tape->pipeline_head_speed = max(tape->uncontrolled_pipeline_head_speed, tape->controlled_pipeline_head_speed);
Borislav Petkov37016ba2008-02-06 02:57:52 +01001420
1421 if (tape->speed_control == 1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422 if (tape->nr_pending_stages >= tape->max_stages / 2)
1423 tape->max_insert_speed = tape->pipeline_head_speed +
1424 (1100 - tape->pipeline_head_speed) * 2 * (tape->nr_pending_stages - tape->max_stages / 2) / tape->max_stages;
1425 else
1426 tape->max_insert_speed = 500 +
1427 (tape->pipeline_head_speed - 500) * 2 * tape->nr_pending_stages / tape->max_stages;
Borislav Petkov37016ba2008-02-06 02:57:52 +01001428
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429 if (tape->nr_pending_stages >= tape->max_stages * 99 / 100)
1430 tape->max_insert_speed = 5000;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431 } else
1432 tape->max_insert_speed = tape->speed_control;
Borislav Petkov37016ba2008-02-06 02:57:52 +01001433
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434 tape->max_insert_speed = max(tape->max_insert_speed, 500);
1435}
1436
1437static ide_startstop_t idetape_media_access_finished (ide_drive_t *drive)
1438{
1439 idetape_tape_t *tape = drive->driver_data;
1440 idetape_pc_t *pc = tape->pc;
Bartlomiej Zolnierkiewicz22c525b2008-01-25 22:17:11 +01001441 u8 stat;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442
Bartlomiej Zolnierkiewiczc47137a2008-02-06 02:57:51 +01001443 stat = ide_read_status(drive);
1444
Bartlomiej Zolnierkiewicz22c525b2008-01-25 22:17:11 +01001445 if (stat & SEEK_STAT) {
1446 if (stat & ERR_STAT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447 /* Error detected */
Borislav Petkov90699ce2008-02-02 19:56:50 +01001448 if (pc->c[0] != TEST_UNIT_READY)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449 printk(KERN_ERR "ide-tape: %s: I/O error, ",
1450 tape->name);
1451 /* Retry operation */
1452 return idetape_retry_pc(drive);
1453 }
1454 pc->error = 0;
1455 if (tape->failed_pc == pc)
1456 tape->failed_pc = NULL;
1457 } else {
1458 pc->error = IDETAPE_ERROR_GENERAL;
1459 tape->failed_pc = NULL;
1460 }
1461 return pc->callback(drive);
1462}
1463
1464static ide_startstop_t idetape_rw_callback (ide_drive_t *drive)
1465{
1466 idetape_tape_t *tape = drive->driver_data;
1467 struct request *rq = HWGROUP(drive)->rq;
Borislav Petkov54bb2072008-02-06 02:57:52 +01001468 int blocks = tape->pc->actually_transferred / tape->blk_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469
Borislav Petkov54bb2072008-02-06 02:57:52 +01001470 tape->avg_size += blocks * tape->blk_size;
1471 tape->insert_size += blocks * tape->blk_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472 if (tape->insert_size > 1024 * 1024)
1473 tape->measure_insert_time = 1;
1474 if (tape->measure_insert_time) {
1475 tape->measure_insert_time = 0;
1476 tape->insert_time = jiffies;
1477 tape->insert_size = 0;
1478 }
1479 if (time_after(jiffies, tape->insert_time))
1480 tape->insert_speed = tape->insert_size / 1024 * HZ / (jiffies - tape->insert_time);
Marcelo Feitoza Parisi9bae1ff2006-03-28 01:56:46 -08001481 if (time_after_eq(jiffies, tape->avg_time + HZ)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482 tape->avg_speed = tape->avg_size * HZ / (jiffies - tape->avg_time) / 1024;
1483 tape->avg_size = 0;
1484 tape->avg_time = jiffies;
1485 }
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001486 debug_log(DBG_PROCS, "Enter %s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487
Borislav Petkov54bb2072008-02-06 02:57:52 +01001488 tape->first_frame += blocks;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489 rq->current_nr_sectors -= blocks;
1490
1491 if (!tape->pc->error)
1492 idetape_end_request(drive, 1, 0);
1493 else
1494 idetape_end_request(drive, tape->pc->error, 0);
1495 return ide_stopped;
1496}
1497
1498static void idetape_create_read_cmd(idetape_tape_t *tape, idetape_pc_t *pc, unsigned int length, struct idetape_bh *bh)
1499{
1500 idetape_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +01001501 pc->c[0] = READ_6;
Borislav Petkov860ff5e2008-02-02 19:56:50 +01001502 put_unaligned(cpu_to_be32(length), (unsigned int *) &pc->c[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503 pc->c[1] = 1;
1504 pc->callback = &idetape_rw_callback;
1505 pc->bh = bh;
1506 atomic_set(&bh->b_count, 0);
1507 pc->buffer = NULL;
Borislav Petkov54bb2072008-02-06 02:57:52 +01001508 pc->buffer_size = length * tape->blk_size;
1509 pc->request_transfer = pc->buffer_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510 if (pc->request_transfer == tape->stage_size)
1511 set_bit(PC_DMA_RECOMMENDED, &pc->flags);
1512}
1513
1514static void idetape_create_read_buffer_cmd(idetape_tape_t *tape, idetape_pc_t *pc, unsigned int length, struct idetape_bh *bh)
1515{
1516 int size = 32768;
1517 struct idetape_bh *p = bh;
1518
1519 idetape_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +01001520 pc->c[0] = READ_BUFFER;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521 pc->c[1] = IDETAPE_RETRIEVE_FAULTY_BLOCK;
1522 pc->c[7] = size >> 8;
1523 pc->c[8] = size & 0xff;
1524 pc->callback = &idetape_pc_callback;
1525 pc->bh = bh;
1526 atomic_set(&bh->b_count, 0);
1527 pc->buffer = NULL;
1528 while (p) {
1529 atomic_set(&p->b_count, 0);
1530 p = p->b_reqnext;
1531 }
1532 pc->request_transfer = pc->buffer_size = size;
1533}
1534
1535static void idetape_create_write_cmd(idetape_tape_t *tape, idetape_pc_t *pc, unsigned int length, struct idetape_bh *bh)
1536{
1537 idetape_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +01001538 pc->c[0] = WRITE_6;
Borislav Petkov860ff5e2008-02-02 19:56:50 +01001539 put_unaligned(cpu_to_be32(length), (unsigned int *) &pc->c[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540 pc->c[1] = 1;
1541 pc->callback = &idetape_rw_callback;
1542 set_bit(PC_WRITING, &pc->flags);
1543 pc->bh = bh;
1544 pc->b_data = bh->b_data;
1545 pc->b_count = atomic_read(&bh->b_count);
1546 pc->buffer = NULL;
Borislav Petkov54bb2072008-02-06 02:57:52 +01001547 pc->buffer_size = length * tape->blk_size;
1548 pc->request_transfer = pc->buffer_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549 if (pc->request_transfer == tape->stage_size)
1550 set_bit(PC_DMA_RECOMMENDED, &pc->flags);
1551}
1552
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553static ide_startstop_t idetape_do_request(ide_drive_t *drive,
1554 struct request *rq, sector_t block)
1555{
1556 idetape_tape_t *tape = drive->driver_data;
1557 idetape_pc_t *pc = NULL;
1558 struct request *postponed_rq = tape->postponed_rq;
Bartlomiej Zolnierkiewicz22c525b2008-01-25 22:17:11 +01001559 u8 stat;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001561 debug_log(DBG_SENSE, "sector: %ld, nr_sectors: %ld,"
1562 " current_nr_sectors: %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563 rq->sector, rq->nr_sectors, rq->current_nr_sectors);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001564
Jens Axboe4aff5e22006-08-10 08:44:47 +02001565 if (!blk_special_request(rq)) {
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001566 /* We do not support buffer cache originated requests. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567 printk(KERN_NOTICE "ide-tape: %s: Unsupported request in "
Jens Axboe4aff5e22006-08-10 08:44:47 +02001568 "request queue (%d)\n", drive->name, rq->cmd_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569 ide_end_request(drive, 0, 0);
1570 return ide_stopped;
1571 }
1572
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001573 /* Retry a failed packet command */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001574 if (tape->failed_pc != NULL &&
Borislav Petkov90699ce2008-02-02 19:56:50 +01001575 tape->pc->c[0] == REQUEST_SENSE) {
Borislav Petkov8d06bfa2008-02-06 02:57:53 +01001576 return idetape_issue_pc(drive, tape->failed_pc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001577 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001578 if (postponed_rq != NULL)
1579 if (rq != postponed_rq) {
1580 printk(KERN_ERR "ide-tape: ide-tape.c bug - "
1581 "Two DSC requests were queued\n");
1582 idetape_end_request(drive, 0, 0);
1583 return ide_stopped;
1584 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585
1586 tape->postponed_rq = NULL;
1587
1588 /*
1589 * If the tape is still busy, postpone our request and service
1590 * the other device meanwhile.
1591 */
Bartlomiej Zolnierkiewiczc47137a2008-02-06 02:57:51 +01001592 stat = ide_read_status(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001593
1594 if (!drive->dsc_overlap && !(rq->cmd[0] & REQ_IDETAPE_PC2))
1595 set_bit(IDETAPE_IGNORE_DSC, &tape->flags);
1596
1597 if (drive->post_reset == 1) {
1598 set_bit(IDETAPE_IGNORE_DSC, &tape->flags);
1599 drive->post_reset = 0;
1600 }
1601
Linus Torvalds1da177e2005-04-16 15:20:36 -07001602 if (time_after(jiffies, tape->insert_time))
1603 tape->insert_speed = tape->insert_size / 1024 * HZ / (jiffies - tape->insert_time);
Borislav Petkov37016ba2008-02-06 02:57:52 +01001604 idetape_calculate_speeds(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001605 if (!test_and_clear_bit(IDETAPE_IGNORE_DSC, &tape->flags) &&
Bartlomiej Zolnierkiewicz22c525b2008-01-25 22:17:11 +01001606 (stat & SEEK_STAT) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001607 if (postponed_rq == NULL) {
1608 tape->dsc_polling_start = jiffies;
Borislav Petkov54bb2072008-02-06 02:57:52 +01001609 tape->dsc_poll_freq = tape->best_dsc_rw_freq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610 tape->dsc_timeout = jiffies + IDETAPE_DSC_RW_TIMEOUT;
1611 } else if (time_after(jiffies, tape->dsc_timeout)) {
1612 printk(KERN_ERR "ide-tape: %s: DSC timeout\n",
1613 tape->name);
1614 if (rq->cmd[0] & REQ_IDETAPE_PC2) {
1615 idetape_media_access_finished(drive);
1616 return ide_stopped;
1617 } else {
1618 return ide_do_reset(drive);
1619 }
Marcelo Feitoza Parisi9bae1ff2006-03-28 01:56:46 -08001620 } else if (time_after(jiffies, tape->dsc_polling_start + IDETAPE_DSC_MA_THRESHOLD))
Borislav Petkov54bb2072008-02-06 02:57:52 +01001621 tape->dsc_poll_freq = IDETAPE_DSC_MA_SLOW;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001622 idetape_postpone_request(drive);
1623 return ide_stopped;
1624 }
1625 if (rq->cmd[0] & REQ_IDETAPE_READ) {
1626 tape->buffer_head++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001627 tape->postpone_cnt = 0;
1628 pc = idetape_next_pc_storage(drive);
1629 idetape_create_read_cmd(tape, pc, rq->current_nr_sectors, (struct idetape_bh *)rq->special);
1630 goto out;
1631 }
1632 if (rq->cmd[0] & REQ_IDETAPE_WRITE) {
1633 tape->buffer_head++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634 tape->postpone_cnt = 0;
1635 pc = idetape_next_pc_storage(drive);
1636 idetape_create_write_cmd(tape, pc, rq->current_nr_sectors, (struct idetape_bh *)rq->special);
1637 goto out;
1638 }
1639 if (rq->cmd[0] & REQ_IDETAPE_READ_BUFFER) {
1640 tape->postpone_cnt = 0;
1641 pc = idetape_next_pc_storage(drive);
1642 idetape_create_read_buffer_cmd(tape, pc, rq->current_nr_sectors, (struct idetape_bh *)rq->special);
1643 goto out;
1644 }
1645 if (rq->cmd[0] & REQ_IDETAPE_PC1) {
1646 pc = (idetape_pc_t *) rq->buffer;
1647 rq->cmd[0] &= ~(REQ_IDETAPE_PC1);
1648 rq->cmd[0] |= REQ_IDETAPE_PC2;
1649 goto out;
1650 }
1651 if (rq->cmd[0] & REQ_IDETAPE_PC2) {
1652 idetape_media_access_finished(drive);
1653 return ide_stopped;
1654 }
1655 BUG();
1656out:
Borislav Petkov8d06bfa2008-02-06 02:57:53 +01001657 return idetape_issue_pc(drive, pc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001658}
1659
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001660/* Pipeline related functions */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001661static inline int idetape_pipeline_active (idetape_tape_t *tape)
1662{
1663 int rc1, rc2;
1664
1665 rc1 = test_bit(IDETAPE_PIPELINE_ACTIVE, &tape->flags);
Borislav Petkov54bb2072008-02-06 02:57:52 +01001666 rc2 = (tape->active_data_rq != NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001667 return rc1;
1668}
1669
1670/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001671 * The function below uses __get_free_page to allocate a pipeline stage, along
1672 * with all the necessary small buffers which together make a buffer of size
1673 * tape->stage_size (or a bit more). We attempt to combine sequential pages as
1674 * much as possible.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001676 * It returns a pointer to the new allocated stage, or NULL if we can't (or
1677 * don't want to) allocate a stage.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001678 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001679 * Pipeline stages are optional and are used to increase performance. If we
1680 * can't allocate them, we'll manage without them.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681 */
1682static idetape_stage_t *__idetape_kmalloc_stage (idetape_tape_t *tape, int full, int clear)
1683{
1684 idetape_stage_t *stage;
1685 struct idetape_bh *prev_bh, *bh;
1686 int pages = tape->pages_per_stage;
1687 char *b_data = NULL;
1688
Robert P. J. Day5cbded52006-12-13 00:35:56 -08001689 if ((stage = kmalloc(sizeof (idetape_stage_t),GFP_KERNEL)) == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001690 return NULL;
1691 stage->next = NULL;
1692
Robert P. J. Day5cbded52006-12-13 00:35:56 -08001693 bh = stage->bh = kmalloc(sizeof(struct idetape_bh), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694 if (bh == NULL)
1695 goto abort;
1696 bh->b_reqnext = NULL;
1697 if ((bh->b_data = (char *) __get_free_page (GFP_KERNEL)) == NULL)
1698 goto abort;
1699 if (clear)
1700 memset(bh->b_data, 0, PAGE_SIZE);
1701 bh->b_size = PAGE_SIZE;
1702 atomic_set(&bh->b_count, full ? bh->b_size : 0);
1703
1704 while (--pages) {
1705 if ((b_data = (char *) __get_free_page (GFP_KERNEL)) == NULL)
1706 goto abort;
1707 if (clear)
1708 memset(b_data, 0, PAGE_SIZE);
1709 if (bh->b_data == b_data + PAGE_SIZE) {
1710 bh->b_size += PAGE_SIZE;
1711 bh->b_data -= PAGE_SIZE;
1712 if (full)
1713 atomic_add(PAGE_SIZE, &bh->b_count);
1714 continue;
1715 }
1716 if (b_data == bh->b_data + bh->b_size) {
1717 bh->b_size += PAGE_SIZE;
1718 if (full)
1719 atomic_add(PAGE_SIZE, &bh->b_count);
1720 continue;
1721 }
1722 prev_bh = bh;
Robert P. J. Day5cbded52006-12-13 00:35:56 -08001723 if ((bh = kmalloc(sizeof(struct idetape_bh), GFP_KERNEL)) == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001724 free_page((unsigned long) b_data);
1725 goto abort;
1726 }
1727 bh->b_reqnext = NULL;
1728 bh->b_data = b_data;
1729 bh->b_size = PAGE_SIZE;
1730 atomic_set(&bh->b_count, full ? bh->b_size : 0);
1731 prev_bh->b_reqnext = bh;
1732 }
1733 bh->b_size -= tape->excess_bh_size;
1734 if (full)
1735 atomic_sub(tape->excess_bh_size, &bh->b_count);
1736 return stage;
1737abort:
1738 __idetape_kfree_stage(stage);
1739 return NULL;
1740}
1741
1742static idetape_stage_t *idetape_kmalloc_stage (idetape_tape_t *tape)
1743{
1744 idetape_stage_t *cache_stage = tape->cache_stage;
1745
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001746 debug_log(DBG_PROCS, "Enter %s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001747
1748 if (tape->nr_stages >= tape->max_stages)
1749 return NULL;
1750 if (cache_stage != NULL) {
1751 tape->cache_stage = NULL;
1752 return cache_stage;
1753 }
1754 return __idetape_kmalloc_stage(tape, 0, 0);
1755}
1756
Daniel Walkerdcd96372006-06-25 05:47:37 -07001757static 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 -07001758{
1759 struct idetape_bh *bh = tape->bh;
1760 int count;
Daniel Walkerdcd96372006-06-25 05:47:37 -07001761 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001762
1763 while (n) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001764 if (bh == NULL) {
1765 printk(KERN_ERR "ide-tape: bh == NULL in "
1766 "idetape_copy_stage_from_user\n");
Daniel Walkerdcd96372006-06-25 05:47:37 -07001767 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001768 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001769 count = min((unsigned int)(bh->b_size - atomic_read(&bh->b_count)), (unsigned int)n);
Daniel Walkerdcd96372006-06-25 05:47:37 -07001770 if (copy_from_user(bh->b_data + atomic_read(&bh->b_count), buf, count))
1771 ret = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001772 n -= count;
1773 atomic_add(count, &bh->b_count);
1774 buf += count;
1775 if (atomic_read(&bh->b_count) == bh->b_size) {
1776 bh = bh->b_reqnext;
1777 if (bh)
1778 atomic_set(&bh->b_count, 0);
1779 }
1780 }
1781 tape->bh = bh;
Daniel Walkerdcd96372006-06-25 05:47:37 -07001782 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001783}
1784
Daniel Walkerdcd96372006-06-25 05:47:37 -07001785static 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 -07001786{
1787 struct idetape_bh *bh = tape->bh;
1788 int count;
Daniel Walkerdcd96372006-06-25 05:47:37 -07001789 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790
1791 while (n) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001792 if (bh == NULL) {
1793 printk(KERN_ERR "ide-tape: bh == NULL in "
1794 "idetape_copy_stage_to_user\n");
Daniel Walkerdcd96372006-06-25 05:47:37 -07001795 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001796 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001797 count = min(tape->b_count, n);
Daniel Walkerdcd96372006-06-25 05:47:37 -07001798 if (copy_to_user(buf, tape->b_data, count))
1799 ret = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001800 n -= count;
1801 tape->b_data += count;
1802 tape->b_count -= count;
1803 buf += count;
1804 if (!tape->b_count) {
1805 tape->bh = bh = bh->b_reqnext;
1806 if (bh) {
1807 tape->b_data = bh->b_data;
1808 tape->b_count = atomic_read(&bh->b_count);
1809 }
1810 }
1811 }
Daniel Walkerdcd96372006-06-25 05:47:37 -07001812 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001813}
1814
1815static void idetape_init_merge_stage (idetape_tape_t *tape)
1816{
1817 struct idetape_bh *bh = tape->merge_stage->bh;
1818
1819 tape->bh = bh;
Borislav Petkov54abf372008-02-06 02:57:52 +01001820 if (tape->chrdev_dir == IDETAPE_DIR_WRITE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001821 atomic_set(&bh->b_count, 0);
1822 else {
1823 tape->b_data = bh->b_data;
1824 tape->b_count = atomic_read(&bh->b_count);
1825 }
1826}
1827
1828static void idetape_switch_buffers (idetape_tape_t *tape, idetape_stage_t *stage)
1829{
1830 struct idetape_bh *tmp;
1831
1832 tmp = stage->bh;
1833 stage->bh = tape->merge_stage->bh;
1834 tape->merge_stage->bh = tmp;
1835 idetape_init_merge_stage(tape);
1836}
1837
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001838/* Add a new stage at the end of the pipeline. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001839static void idetape_add_stage_tail (ide_drive_t *drive,idetape_stage_t *stage)
1840{
1841 idetape_tape_t *tape = drive->driver_data;
1842 unsigned long flags;
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001843
1844 debug_log(DBG_PROCS, "Enter %s\n", __func__);
1845
Borislav Petkov54bb2072008-02-06 02:57:52 +01001846 spin_lock_irqsave(&tape->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001847 stage->next = NULL;
1848 if (tape->last_stage != NULL)
1849 tape->last_stage->next=stage;
1850 else
1851 tape->first_stage = tape->next_stage=stage;
1852 tape->last_stage = stage;
1853 if (tape->next_stage == NULL)
1854 tape->next_stage = tape->last_stage;
1855 tape->nr_stages++;
1856 tape->nr_pending_stages++;
Borislav Petkov54bb2072008-02-06 02:57:52 +01001857 spin_unlock_irqrestore(&tape->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001858}
1859
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001860/* Install a completion in a pending request and sleep until it is serviced. The
1861 * caller should ensure that the request will not be serviced before we install
1862 * the completion (usually by disabling interrupts).
Linus Torvalds1da177e2005-04-16 15:20:36 -07001863 */
1864static void idetape_wait_for_request (ide_drive_t *drive, struct request *rq)
1865{
Peter Zijlstra6e9a4732006-09-30 23:28:10 -07001866 DECLARE_COMPLETION_ONSTACK(wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001867 idetape_tape_t *tape = drive->driver_data;
1868
Jens Axboe4aff5e22006-08-10 08:44:47 +02001869 if (rq == NULL || !blk_special_request(rq)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001870 printk (KERN_ERR "ide-tape: bug: Trying to sleep on non-valid request\n");
1871 return;
1872 }
Jens Axboec00895a2006-09-30 20:29:12 +02001873 rq->end_io_data = &wait;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001874 rq->end_io = blk_end_sync_rq;
Borislav Petkov54bb2072008-02-06 02:57:52 +01001875 spin_unlock_irq(&tape->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001876 wait_for_completion(&wait);
1877 /* The stage and its struct request have been deallocated */
Borislav Petkov54bb2072008-02-06 02:57:52 +01001878 spin_lock_irq(&tape->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001879}
1880
Borislav Petkova2f5b7f2008-02-06 02:57:51 +01001881static ide_startstop_t idetape_read_position_callback(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001882{
1883 idetape_tape_t *tape = drive->driver_data;
Borislav Petkova2f5b7f2008-02-06 02:57:51 +01001884 u8 *readpos = tape->pc->buffer;
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001885
1886 debug_log(DBG_PROCS, "Enter %s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001887
1888 if (!tape->pc->error) {
Borislav Petkova2f5b7f2008-02-06 02:57:51 +01001889 debug_log(DBG_SENSE, "BOP - %s\n",
1890 (readpos[0] & 0x80) ? "Yes" : "No");
1891 debug_log(DBG_SENSE, "EOP - %s\n",
1892 (readpos[0] & 0x40) ? "Yes" : "No");
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001893
Borislav Petkova2f5b7f2008-02-06 02:57:51 +01001894 if (readpos[0] & 0x4) {
1895 printk(KERN_INFO "ide-tape: Block location is unknown"
1896 "to the tape\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001897 clear_bit(IDETAPE_ADDRESS_VALID, &tape->flags);
1898 idetape_end_request(drive, 0, 0);
1899 } else {
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001900 debug_log(DBG_SENSE, "Block Location - %u\n",
Borislav Petkova2f5b7f2008-02-06 02:57:51 +01001901 be32_to_cpu(*(u32 *)&readpos[4]));
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001902
Borislav Petkova2f5b7f2008-02-06 02:57:51 +01001903 tape->partition = readpos[1];
Borislav Petkov54bb2072008-02-06 02:57:52 +01001904 tape->first_frame =
Borislav Petkova2f5b7f2008-02-06 02:57:51 +01001905 be32_to_cpu(*(u32 *)&readpos[4]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001906 set_bit(IDETAPE_ADDRESS_VALID, &tape->flags);
1907 idetape_end_request(drive, 1, 0);
1908 }
1909 } else {
1910 idetape_end_request(drive, 0, 0);
1911 }
1912 return ide_stopped;
1913}
1914
1915/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001916 * Write a filemark if write_filemark=1. Flush the device buffers without
1917 * writing a filemark otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001918 */
1919static void idetape_create_write_filemark_cmd (ide_drive_t *drive, idetape_pc_t *pc,int write_filemark)
1920{
1921 idetape_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +01001922 pc->c[0] = WRITE_FILEMARKS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001923 pc->c[4] = write_filemark;
1924 set_bit(PC_WAIT_FOR_DSC, &pc->flags);
1925 pc->callback = &idetape_pc_callback;
1926}
1927
1928static void idetape_create_test_unit_ready_cmd(idetape_pc_t *pc)
1929{
1930 idetape_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +01001931 pc->c[0] = TEST_UNIT_READY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001932 pc->callback = &idetape_pc_callback;
1933}
1934
1935/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001936 * We add a special packet command request to the tail of the request queue, and
1937 * wait for it to be serviced. This is not to be called from within the request
1938 * handling part of the driver! We allocate here data on the stack and it is
1939 * valid until the request is finished. This is not the case for the bottom part
1940 * of the driver, where we are always leaving the functions to wait for an
1941 * interrupt or a timer event.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001942 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001943 * From the bottom part of the driver, we should allocate safe memory using
1944 * idetape_next_pc_storage() and ide_tape_next_rq_storage(), and add the request
1945 * to the request list without waiting for it to be serviced! In that case, we
1946 * usually use idetape_queue_pc_head().
Linus Torvalds1da177e2005-04-16 15:20:36 -07001947 */
1948static int __idetape_queue_pc_tail (ide_drive_t *drive, idetape_pc_t *pc)
1949{
1950 struct ide_tape_obj *tape = drive->driver_data;
1951 struct request rq;
1952
1953 idetape_init_rq(&rq, REQ_IDETAPE_PC1);
1954 rq.buffer = (char *) pc;
1955 rq.rq_disk = tape->disk;
1956 return ide_do_drive_cmd(drive, &rq, ide_wait);
1957}
1958
1959static void idetape_create_load_unload_cmd (ide_drive_t *drive, idetape_pc_t *pc,int cmd)
1960{
1961 idetape_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +01001962 pc->c[0] = START_STOP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001963 pc->c[4] = cmd;
1964 set_bit(PC_WAIT_FOR_DSC, &pc->flags);
1965 pc->callback = &idetape_pc_callback;
1966}
1967
1968static int idetape_wait_ready(ide_drive_t *drive, unsigned long timeout)
1969{
1970 idetape_tape_t *tape = drive->driver_data;
1971 idetape_pc_t pc;
1972 int load_attempted = 0;
1973
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001974 /* Wait for the tape to become ready */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001975 set_bit(IDETAPE_MEDIUM_PRESENT, &tape->flags);
1976 timeout += jiffies;
1977 while (time_before(jiffies, timeout)) {
1978 idetape_create_test_unit_ready_cmd(&pc);
1979 if (!__idetape_queue_pc_tail(drive, &pc))
1980 return 0;
1981 if ((tape->sense_key == 2 && tape->asc == 4 && tape->ascq == 2)
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001982 || (tape->asc == 0x3A)) {
1983 /* no media */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001984 if (load_attempted)
1985 return -ENOMEDIUM;
1986 idetape_create_load_unload_cmd(drive, &pc, IDETAPE_LU_LOAD_MASK);
1987 __idetape_queue_pc_tail(drive, &pc);
1988 load_attempted = 1;
1989 /* not about to be ready */
1990 } else if (!(tape->sense_key == 2 && tape->asc == 4 &&
1991 (tape->ascq == 1 || tape->ascq == 8)))
1992 return -EIO;
Nishanth Aravamudan80ce45f2005-09-10 00:27:08 -07001993 msleep(100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001994 }
1995 return -EIO;
1996}
1997
1998static int idetape_queue_pc_tail (ide_drive_t *drive,idetape_pc_t *pc)
1999{
2000 return __idetape_queue_pc_tail(drive, pc);
2001}
2002
2003static int idetape_flush_tape_buffers (ide_drive_t *drive)
2004{
2005 idetape_pc_t pc;
2006 int rc;
2007
2008 idetape_create_write_filemark_cmd(drive, &pc, 0);
2009 if ((rc = idetape_queue_pc_tail(drive, &pc)))
2010 return rc;
2011 idetape_wait_ready(drive, 60 * 5 * HZ);
2012 return 0;
2013}
2014
2015static void idetape_create_read_position_cmd (idetape_pc_t *pc)
2016{
2017 idetape_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +01002018 pc->c[0] = READ_POSITION;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002019 pc->request_transfer = 20;
2020 pc->callback = &idetape_read_position_callback;
2021}
2022
2023static int idetape_read_position (ide_drive_t *drive)
2024{
2025 idetape_tape_t *tape = drive->driver_data;
2026 idetape_pc_t pc;
2027 int position;
2028
Borislav Petkov8004a8c2008-02-06 02:57:51 +01002029 debug_log(DBG_PROCS, "Enter %s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002030
2031 idetape_create_read_position_cmd(&pc);
2032 if (idetape_queue_pc_tail(drive, &pc))
2033 return -1;
Borislav Petkov54bb2072008-02-06 02:57:52 +01002034 position = tape->first_frame;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002035 return position;
2036}
2037
2038static void idetape_create_locate_cmd (ide_drive_t *drive, idetape_pc_t *pc, unsigned int block, u8 partition, int skip)
2039{
2040 idetape_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +01002041 pc->c[0] = POSITION_TO_ELEMENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002042 pc->c[1] = 2;
Borislav Petkov860ff5e2008-02-02 19:56:50 +01002043 put_unaligned(cpu_to_be32(block), (unsigned int *) &pc->c[3]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002044 pc->c[8] = partition;
2045 set_bit(PC_WAIT_FOR_DSC, &pc->flags);
2046 pc->callback = &idetape_pc_callback;
2047}
2048
2049static int idetape_create_prevent_cmd (ide_drive_t *drive, idetape_pc_t *pc, int prevent)
2050{
2051 idetape_tape_t *tape = drive->driver_data;
2052
Borislav Petkovb6422012008-02-02 19:56:49 +01002053 /* device supports locking according to capabilities page */
2054 if (!(tape->caps[6] & 0x01))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002055 return 0;
2056
2057 idetape_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +01002058 pc->c[0] = ALLOW_MEDIUM_REMOVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002059 pc->c[4] = prevent;
2060 pc->callback = &idetape_pc_callback;
2061 return 1;
2062}
2063
2064static int __idetape_discard_read_pipeline (ide_drive_t *drive)
2065{
2066 idetape_tape_t *tape = drive->driver_data;
2067 unsigned long flags;
2068 int cnt;
2069
Borislav Petkov54abf372008-02-06 02:57:52 +01002070 if (tape->chrdev_dir != IDETAPE_DIR_READ)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002071 return 0;
2072
2073 /* Remove merge stage. */
Borislav Petkov54bb2072008-02-06 02:57:52 +01002074 cnt = tape->merge_stage_size / tape->blk_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002075 if (test_and_clear_bit(IDETAPE_FILEMARK, &tape->flags))
2076 ++cnt; /* Filemarks count as 1 sector */
2077 tape->merge_stage_size = 0;
2078 if (tape->merge_stage != NULL) {
2079 __idetape_kfree_stage(tape->merge_stage);
2080 tape->merge_stage = NULL;
2081 }
2082
2083 /* Clear pipeline flags. */
2084 clear_bit(IDETAPE_PIPELINE_ERROR, &tape->flags);
Borislav Petkov54abf372008-02-06 02:57:52 +01002085 tape->chrdev_dir = IDETAPE_DIR_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002086
2087 /* Remove pipeline stages. */
2088 if (tape->first_stage == NULL)
2089 return 0;
2090
Borislav Petkov54bb2072008-02-06 02:57:52 +01002091 spin_lock_irqsave(&tape->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002092 tape->next_stage = NULL;
2093 if (idetape_pipeline_active(tape))
Borislav Petkov54bb2072008-02-06 02:57:52 +01002094 idetape_wait_for_request(drive, tape->active_data_rq);
2095 spin_unlock_irqrestore(&tape->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002096
2097 while (tape->first_stage != NULL) {
2098 struct request *rq_ptr = &tape->first_stage->rq;
2099
2100 cnt += rq_ptr->nr_sectors - rq_ptr->current_nr_sectors;
2101 if (rq_ptr->errors == IDETAPE_ERROR_FILEMARK)
2102 ++cnt;
2103 idetape_remove_stage_head(drive);
2104 }
2105 tape->nr_pending_stages = 0;
2106 tape->max_stages = tape->min_pipeline;
2107 return cnt;
2108}
2109
2110/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +01002111 * Position the tape to the requested block using the LOCATE packet command.
2112 * A READ POSITION command is then issued to check where we are positioned. Like
2113 * all higher level operations, we queue the commands at the tail of the request
2114 * queue and wait for their completion.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002115 */
2116static int idetape_position_tape (ide_drive_t *drive, unsigned int block, u8 partition, int skip)
2117{
2118 idetape_tape_t *tape = drive->driver_data;
2119 int retval;
2120 idetape_pc_t pc;
2121
Borislav Petkov54abf372008-02-06 02:57:52 +01002122 if (tape->chrdev_dir == IDETAPE_DIR_READ)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002123 __idetape_discard_read_pipeline(drive);
2124 idetape_wait_ready(drive, 60 * 5 * HZ);
2125 idetape_create_locate_cmd(drive, &pc, block, partition, skip);
2126 retval = idetape_queue_pc_tail(drive, &pc);
2127 if (retval)
2128 return (retval);
2129
2130 idetape_create_read_position_cmd(&pc);
2131 return (idetape_queue_pc_tail(drive, &pc));
2132}
2133
2134static void idetape_discard_read_pipeline (ide_drive_t *drive, int restore_position)
2135{
2136 idetape_tape_t *tape = drive->driver_data;
2137 int cnt;
2138 int seek, position;
2139
2140 cnt = __idetape_discard_read_pipeline(drive);
2141 if (restore_position) {
2142 position = idetape_read_position(drive);
2143 seek = position > cnt ? position - cnt : 0;
2144 if (idetape_position_tape(drive, seek, 0, 0)) {
2145 printk(KERN_INFO "ide-tape: %s: position_tape failed in discard_pipeline()\n", tape->name);
2146 return;
2147 }
2148 }
2149}
2150
2151/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +01002152 * Generate a read/write request for the block device interface and wait for it
2153 * to be serviced.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002154 */
2155static int idetape_queue_rw_tail(ide_drive_t *drive, int cmd, int blocks, struct idetape_bh *bh)
2156{
2157 idetape_tape_t *tape = drive->driver_data;
2158 struct request rq;
2159
Borislav Petkov8004a8c2008-02-06 02:57:51 +01002160 debug_log(DBG_SENSE, "%s: cmd=%d\n", __func__, cmd);
2161
Linus Torvalds1da177e2005-04-16 15:20:36 -07002162 if (idetape_pipeline_active(tape)) {
Borislav Petkov8004a8c2008-02-06 02:57:51 +01002163 printk(KERN_ERR "ide-tape: bug: the pipeline is active in %s\n",
2164 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002165 return (0);
2166 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002167
2168 idetape_init_rq(&rq, cmd);
2169 rq.rq_disk = tape->disk;
2170 rq.special = (void *)bh;
Borislav Petkov54bb2072008-02-06 02:57:52 +01002171 rq.sector = tape->first_frame;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002172 rq.nr_sectors = rq.current_nr_sectors = blocks;
2173 (void) ide_do_drive_cmd(drive, &rq, ide_wait);
2174
2175 if ((cmd & (REQ_IDETAPE_READ | REQ_IDETAPE_WRITE)) == 0)
2176 return 0;
2177
2178 if (tape->merge_stage)
2179 idetape_init_merge_stage(tape);
2180 if (rq.errors == IDETAPE_ERROR_GENERAL)
2181 return -EIO;
Borislav Petkov54bb2072008-02-06 02:57:52 +01002182 return (tape->blk_size * (blocks-rq.current_nr_sectors));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002183}
2184
Borislav Petkov8d06bfa2008-02-06 02:57:53 +01002185/* start servicing the pipeline stages, starting from tape->next_stage. */
2186static void idetape_plug_pipeline(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002187{
2188 idetape_tape_t *tape = drive->driver_data;
2189
2190 if (tape->next_stage == NULL)
2191 return;
2192 if (!idetape_pipeline_active(tape)) {
2193 set_bit(IDETAPE_PIPELINE_ACTIVE, &tape->flags);
Borislav Petkov419d4742008-02-02 19:56:51 +01002194 idetape_activate_next_stage(drive);
Borislav Petkov54bb2072008-02-06 02:57:52 +01002195 (void) ide_do_drive_cmd(drive, tape->active_data_rq, ide_end);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002196 }
2197}
2198
2199static void idetape_create_inquiry_cmd (idetape_pc_t *pc)
2200{
2201 idetape_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +01002202 pc->c[0] = INQUIRY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002203 pc->c[4] = pc->request_transfer = 254;
2204 pc->callback = &idetape_pc_callback;
2205}
2206
2207static void idetape_create_rewind_cmd (ide_drive_t *drive, idetape_pc_t *pc)
2208{
2209 idetape_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +01002210 pc->c[0] = REZERO_UNIT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002211 set_bit(PC_WAIT_FOR_DSC, &pc->flags);
2212 pc->callback = &idetape_pc_callback;
2213}
2214
Linus Torvalds1da177e2005-04-16 15:20:36 -07002215static void idetape_create_erase_cmd (idetape_pc_t *pc)
2216{
2217 idetape_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +01002218 pc->c[0] = ERASE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002219 pc->c[1] = 1;
2220 set_bit(PC_WAIT_FOR_DSC, &pc->flags);
2221 pc->callback = &idetape_pc_callback;
2222}
2223
2224static void idetape_create_space_cmd (idetape_pc_t *pc,int count, u8 cmd)
2225{
2226 idetape_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +01002227 pc->c[0] = SPACE;
Borislav Petkov860ff5e2008-02-02 19:56:50 +01002228 put_unaligned(cpu_to_be32(count), (unsigned int *) &pc->c[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002229 pc->c[1] = cmd;
2230 set_bit(PC_WAIT_FOR_DSC, &pc->flags);
2231 pc->callback = &idetape_pc_callback;
2232}
2233
2234static void idetape_wait_first_stage (ide_drive_t *drive)
2235{
2236 idetape_tape_t *tape = drive->driver_data;
2237 unsigned long flags;
2238
2239 if (tape->first_stage == NULL)
2240 return;
Borislav Petkov54bb2072008-02-06 02:57:52 +01002241 spin_lock_irqsave(&tape->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002242 if (tape->active_stage == tape->first_stage)
Borislav Petkov54bb2072008-02-06 02:57:52 +01002243 idetape_wait_for_request(drive, tape->active_data_rq);
2244 spin_unlock_irqrestore(&tape->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002245}
2246
2247/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +01002248 * Try to add a character device originated write request to our pipeline. In
2249 * case we don't succeed, we revert to non-pipelined operation mode for this
2250 * request. In order to accomplish that, we
Linus Torvalds1da177e2005-04-16 15:20:36 -07002251 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +01002252 * 1. Try to allocate a new pipeline stage.
2253 * 2. If we can't, wait for more and more requests to be serviced and try again
2254 * each time.
2255 * 3. If we still can't allocate a stage, fallback to non-pipelined operation
2256 * mode for this request.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002257 */
2258static int idetape_add_chrdev_write_request (ide_drive_t *drive, int blocks)
2259{
2260 idetape_tape_t *tape = drive->driver_data;
2261 idetape_stage_t *new_stage;
2262 unsigned long flags;
2263 struct request *rq;
2264
Borislav Petkov8004a8c2008-02-06 02:57:51 +01002265 debug_log(DBG_CHRDEV, "Enter %s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002266
Borislav Petkov3c98bf32008-02-06 02:57:53 +01002267 /* Attempt to allocate a new stage. Beware possible race conditions. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002268 while ((new_stage = idetape_kmalloc_stage(tape)) == NULL) {
Borislav Petkov54bb2072008-02-06 02:57:52 +01002269 spin_lock_irqsave(&tape->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002270 if (idetape_pipeline_active(tape)) {
Borislav Petkov54bb2072008-02-06 02:57:52 +01002271 idetape_wait_for_request(drive, tape->active_data_rq);
2272 spin_unlock_irqrestore(&tape->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002273 } else {
Borislav Petkov54bb2072008-02-06 02:57:52 +01002274 spin_unlock_irqrestore(&tape->lock, flags);
Borislav Petkov8d06bfa2008-02-06 02:57:53 +01002275 idetape_plug_pipeline(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002276 if (idetape_pipeline_active(tape))
2277 continue;
2278 /*
Borislav Petkov3c98bf32008-02-06 02:57:53 +01002279 * The machine is short on memory. Fallback to non-
2280 * pipelined operation mode for this request.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002281 */
2282 return idetape_queue_rw_tail(drive, REQ_IDETAPE_WRITE, blocks, tape->merge_stage->bh);
2283 }
2284 }
2285 rq = &new_stage->rq;
2286 idetape_init_rq(rq, REQ_IDETAPE_WRITE);
2287 /* Doesn't actually matter - We always assume sequential access */
Borislav Petkov54bb2072008-02-06 02:57:52 +01002288 rq->sector = tape->first_frame;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002289 rq->nr_sectors = rq->current_nr_sectors = blocks;
2290
2291 idetape_switch_buffers(tape, new_stage);
2292 idetape_add_stage_tail(drive, new_stage);
2293 tape->pipeline_head++;
Borislav Petkov37016ba2008-02-06 02:57:52 +01002294 idetape_calculate_speeds(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002295
2296 /*
Borislav Petkov3c98bf32008-02-06 02:57:53 +01002297 * Estimate whether the tape has stopped writing by checking if our
2298 * write pipeline is currently empty. If we are not writing anymore,
2299 * wait for the pipeline to be almost completely full (90%) before
2300 * starting to service requests, so that we will be able to keep up with
2301 * the higher speeds of the tape.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002302 */
2303 if (!idetape_pipeline_active(tape)) {
2304 if (tape->nr_stages >= tape->max_stages * 9 / 10 ||
Borislav Petkov54bb2072008-02-06 02:57:52 +01002305 tape->nr_stages >= tape->max_stages -
2306 tape->uncontrolled_pipeline_head_speed * 3 * 1024 /
2307 tape->blk_size) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002308 tape->measure_insert_time = 1;
2309 tape->insert_time = jiffies;
2310 tape->insert_size = 0;
2311 tape->insert_speed = 0;
Borislav Petkov8d06bfa2008-02-06 02:57:53 +01002312 idetape_plug_pipeline(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002313 }
2314 }
2315 if (test_and_clear_bit(IDETAPE_PIPELINE_ERROR, &tape->flags))
2316 /* Return a deferred error */
2317 return -EIO;
2318 return blocks;
2319}
2320
2321/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +01002322 * Wait until all pending pipeline requests are serviced. Typically called on
2323 * device close.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002324 */
2325static void idetape_wait_for_pipeline (ide_drive_t *drive)
2326{
2327 idetape_tape_t *tape = drive->driver_data;
2328 unsigned long flags;
2329
2330 while (tape->next_stage || idetape_pipeline_active(tape)) {
Borislav Petkov8d06bfa2008-02-06 02:57:53 +01002331 idetape_plug_pipeline(drive);
Borislav Petkov54bb2072008-02-06 02:57:52 +01002332 spin_lock_irqsave(&tape->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002333 if (idetape_pipeline_active(tape))
Borislav Petkov54bb2072008-02-06 02:57:52 +01002334 idetape_wait_for_request(drive, tape->active_data_rq);
2335 spin_unlock_irqrestore(&tape->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002336 }
2337}
2338
2339static void idetape_empty_write_pipeline (ide_drive_t *drive)
2340{
2341 idetape_tape_t *tape = drive->driver_data;
2342 int blocks, min;
2343 struct idetape_bh *bh;
Borislav Petkov55a5d292008-02-02 19:56:49 +01002344
Borislav Petkov54abf372008-02-06 02:57:52 +01002345 if (tape->chrdev_dir != IDETAPE_DIR_WRITE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002346 printk(KERN_ERR "ide-tape: bug: Trying to empty write pipeline, but we are not writing.\n");
2347 return;
2348 }
2349 if (tape->merge_stage_size > tape->stage_size) {
2350 printk(KERN_ERR "ide-tape: bug: merge_buffer too big\n");
2351 tape->merge_stage_size = tape->stage_size;
2352 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002353 if (tape->merge_stage_size) {
Borislav Petkov54bb2072008-02-06 02:57:52 +01002354 blocks = tape->merge_stage_size / tape->blk_size;
2355 if (tape->merge_stage_size % tape->blk_size) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002356 unsigned int i;
2357
2358 blocks++;
Borislav Petkov54bb2072008-02-06 02:57:52 +01002359 i = tape->blk_size - tape->merge_stage_size %
2360 tape->blk_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002361 bh = tape->bh->b_reqnext;
2362 while (bh) {
2363 atomic_set(&bh->b_count, 0);
2364 bh = bh->b_reqnext;
2365 }
2366 bh = tape->bh;
2367 while (i) {
2368 if (bh == NULL) {
2369
2370 printk(KERN_INFO "ide-tape: bug, bh NULL\n");
2371 break;
2372 }
2373 min = min(i, (unsigned int)(bh->b_size - atomic_read(&bh->b_count)));
2374 memset(bh->b_data + atomic_read(&bh->b_count), 0, min);
2375 atomic_add(min, &bh->b_count);
2376 i -= min;
2377 bh = bh->b_reqnext;
2378 }
2379 }
2380 (void) idetape_add_chrdev_write_request(drive, blocks);
2381 tape->merge_stage_size = 0;
2382 }
2383 idetape_wait_for_pipeline(drive);
2384 if (tape->merge_stage != NULL) {
2385 __idetape_kfree_stage(tape->merge_stage);
2386 tape->merge_stage = NULL;
2387 }
2388 clear_bit(IDETAPE_PIPELINE_ERROR, &tape->flags);
Borislav Petkov54abf372008-02-06 02:57:52 +01002389 tape->chrdev_dir = IDETAPE_DIR_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002390
2391 /*
Borislav Petkov3c98bf32008-02-06 02:57:53 +01002392 * On the next backup, perform the feedback loop again. (I don't want to
2393 * keep sense information between backups, as some systems are
2394 * constantly on, and the system load can be totally different on the
2395 * next backup).
Linus Torvalds1da177e2005-04-16 15:20:36 -07002396 */
2397 tape->max_stages = tape->min_pipeline;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002398 if (tape->first_stage != NULL ||
2399 tape->next_stage != NULL ||
2400 tape->last_stage != NULL ||
2401 tape->nr_stages != 0) {
2402 printk(KERN_ERR "ide-tape: ide-tape pipeline bug, "
2403 "first_stage %p, next_stage %p, "
2404 "last_stage %p, nr_stages %d\n",
2405 tape->first_stage, tape->next_stage,
2406 tape->last_stage, tape->nr_stages);
2407 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002408}
2409
2410static void idetape_restart_speed_control (ide_drive_t *drive)
2411{
2412 idetape_tape_t *tape = drive->driver_data;
2413
2414 tape->restart_speed_control_req = 0;
2415 tape->pipeline_head = 0;
Borislav Petkov41f81d542008-02-06 02:57:52 +01002416 tape->controlled_last_pipeline_head = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002417 tape->controlled_previous_pipeline_head = tape->uncontrolled_previous_pipeline_head = 0;
2418 tape->pipeline_head_speed = tape->controlled_pipeline_head_speed = 5000;
2419 tape->uncontrolled_pipeline_head_speed = 0;
2420 tape->controlled_pipeline_head_time = tape->uncontrolled_pipeline_head_time = jiffies;
2421 tape->controlled_previous_head_time = tape->uncontrolled_previous_head_time = jiffies;
2422}
2423
Borislav Petkov8d06bfa2008-02-06 02:57:53 +01002424static int idetape_init_read(ide_drive_t *drive, int max_stages)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002425{
2426 idetape_tape_t *tape = drive->driver_data;
2427 idetape_stage_t *new_stage;
2428 struct request rq;
2429 int bytes_read;
Borislav Petkovb6422012008-02-02 19:56:49 +01002430 u16 blocks = *(u16 *)&tape->caps[12];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002431
2432 /* Initialize read operation */
Borislav Petkov54abf372008-02-06 02:57:52 +01002433 if (tape->chrdev_dir != IDETAPE_DIR_READ) {
2434 if (tape->chrdev_dir == IDETAPE_DIR_WRITE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002435 idetape_empty_write_pipeline(drive);
2436 idetape_flush_tape_buffers(drive);
2437 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002438 if (tape->merge_stage || tape->merge_stage_size) {
2439 printk (KERN_ERR "ide-tape: merge_stage_size should be 0 now\n");
2440 tape->merge_stage_size = 0;
2441 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002442 if ((tape->merge_stage = __idetape_kmalloc_stage(tape, 0, 0)) == NULL)
2443 return -ENOMEM;
Borislav Petkov54abf372008-02-06 02:57:52 +01002444 tape->chrdev_dir = IDETAPE_DIR_READ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002445
2446 /*
Borislav Petkov3c98bf32008-02-06 02:57:53 +01002447 * Issue a read 0 command to ensure that DSC handshake is
2448 * switched from completion mode to buffer available mode.
2449 * No point in issuing this if DSC overlap isn't supported, some
2450 * drives (Seagate STT3401A) will return an error.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002451 */
2452 if (drive->dsc_overlap) {
2453 bytes_read = idetape_queue_rw_tail(drive, REQ_IDETAPE_READ, 0, tape->merge_stage->bh);
2454 if (bytes_read < 0) {
2455 __idetape_kfree_stage(tape->merge_stage);
2456 tape->merge_stage = NULL;
Borislav Petkov54abf372008-02-06 02:57:52 +01002457 tape->chrdev_dir = IDETAPE_DIR_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002458 return bytes_read;
2459 }
2460 }
2461 }
2462 if (tape->restart_speed_control_req)
2463 idetape_restart_speed_control(drive);
2464 idetape_init_rq(&rq, REQ_IDETAPE_READ);
Borislav Petkov54bb2072008-02-06 02:57:52 +01002465 rq.sector = tape->first_frame;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002466 rq.nr_sectors = rq.current_nr_sectors = blocks;
2467 if (!test_bit(IDETAPE_PIPELINE_ERROR, &tape->flags) &&
2468 tape->nr_stages < max_stages) {
2469 new_stage = idetape_kmalloc_stage(tape);
2470 while (new_stage != NULL) {
2471 new_stage->rq = rq;
2472 idetape_add_stage_tail(drive, new_stage);
2473 if (tape->nr_stages >= max_stages)
2474 break;
2475 new_stage = idetape_kmalloc_stage(tape);
2476 }
2477 }
2478 if (!idetape_pipeline_active(tape)) {
2479 if (tape->nr_pending_stages >= 3 * max_stages / 4) {
2480 tape->measure_insert_time = 1;
2481 tape->insert_time = jiffies;
2482 tape->insert_size = 0;
2483 tape->insert_speed = 0;
Borislav Petkov8d06bfa2008-02-06 02:57:53 +01002484 idetape_plug_pipeline(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002485 }
2486 }
2487 return 0;
2488}
2489
2490/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +01002491 * Called from idetape_chrdev_read() to service a character device read request
2492 * and add read-ahead requests to our pipeline.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002493 */
2494static int idetape_add_chrdev_read_request (ide_drive_t *drive,int blocks)
2495{
2496 idetape_tape_t *tape = drive->driver_data;
2497 unsigned long flags;
2498 struct request *rq_ptr;
2499 int bytes_read;
2500
Borislav Petkov8004a8c2008-02-06 02:57:51 +01002501 debug_log(DBG_PROCS, "Enter %s, %d blocks\n", __func__, blocks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002502
Borislav Petkov3c98bf32008-02-06 02:57:53 +01002503 /* If we are at a filemark, return a read length of 0 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002504 if (test_bit(IDETAPE_FILEMARK, &tape->flags))
2505 return 0;
2506
Borislav Petkov3c98bf32008-02-06 02:57:53 +01002507 /* Wait for the next block to reach the head of the pipeline. */
Borislav Petkov8d06bfa2008-02-06 02:57:53 +01002508 idetape_init_read(drive, tape->max_stages);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002509 if (tape->first_stage == NULL) {
2510 if (test_bit(IDETAPE_PIPELINE_ERROR, &tape->flags))
2511 return 0;
Borislav Petkov54bb2072008-02-06 02:57:52 +01002512 return idetape_queue_rw_tail(drive, REQ_IDETAPE_READ, blocks,
2513 tape->merge_stage->bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002514 }
2515 idetape_wait_first_stage(drive);
2516 rq_ptr = &tape->first_stage->rq;
Borislav Petkov54bb2072008-02-06 02:57:52 +01002517 bytes_read = tape->blk_size * (rq_ptr->nr_sectors -
2518 rq_ptr->current_nr_sectors);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002519 rq_ptr->nr_sectors = rq_ptr->current_nr_sectors = 0;
2520
2521
2522 if (rq_ptr->errors == IDETAPE_ERROR_EOD)
2523 return 0;
2524 else {
2525 idetape_switch_buffers(tape, tape->first_stage);
2526 if (rq_ptr->errors == IDETAPE_ERROR_FILEMARK)
2527 set_bit(IDETAPE_FILEMARK, &tape->flags);
Borislav Petkov54bb2072008-02-06 02:57:52 +01002528 spin_lock_irqsave(&tape->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002529 idetape_remove_stage_head(drive);
Borislav Petkov54bb2072008-02-06 02:57:52 +01002530 spin_unlock_irqrestore(&tape->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002531 tape->pipeline_head++;
Borislav Petkov37016ba2008-02-06 02:57:52 +01002532 idetape_calculate_speeds(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002533 }
Borislav Petkov54bb2072008-02-06 02:57:52 +01002534 if (bytes_read > blocks * tape->blk_size) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002535 printk(KERN_ERR "ide-tape: bug: trying to return more bytes than requested\n");
Borislav Petkov54bb2072008-02-06 02:57:52 +01002536 bytes_read = blocks * tape->blk_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002537 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002538 return (bytes_read);
2539}
2540
2541static void idetape_pad_zeros (ide_drive_t *drive, int bcount)
2542{
2543 idetape_tape_t *tape = drive->driver_data;
2544 struct idetape_bh *bh;
2545 int blocks;
Borislav Petkov3c98bf32008-02-06 02:57:53 +01002546
Linus Torvalds1da177e2005-04-16 15:20:36 -07002547 while (bcount) {
2548 unsigned int count;
2549
2550 bh = tape->merge_stage->bh;
2551 count = min(tape->stage_size, bcount);
2552 bcount -= count;
Borislav Petkov54bb2072008-02-06 02:57:52 +01002553 blocks = count / tape->blk_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002554 while (count) {
2555 atomic_set(&bh->b_count, min(count, (unsigned int)bh->b_size));
2556 memset(bh->b_data, 0, atomic_read(&bh->b_count));
2557 count -= atomic_read(&bh->b_count);
2558 bh = bh->b_reqnext;
2559 }
2560 idetape_queue_rw_tail(drive, REQ_IDETAPE_WRITE, blocks, tape->merge_stage->bh);
2561 }
2562}
2563
2564static int idetape_pipeline_size (ide_drive_t *drive)
2565{
2566 idetape_tape_t *tape = drive->driver_data;
2567 idetape_stage_t *stage;
2568 struct request *rq;
2569 int size = 0;
2570
2571 idetape_wait_for_pipeline(drive);
2572 stage = tape->first_stage;
2573 while (stage != NULL) {
2574 rq = &stage->rq;
Borislav Petkov54bb2072008-02-06 02:57:52 +01002575 size += tape->blk_size * (rq->nr_sectors -
2576 rq->current_nr_sectors);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002577 if (rq->errors == IDETAPE_ERROR_FILEMARK)
Borislav Petkov54bb2072008-02-06 02:57:52 +01002578 size += tape->blk_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002579 stage = stage->next;
2580 }
2581 size += tape->merge_stage_size;
2582 return size;
2583}
2584
2585/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +01002586 * Rewinds the tape to the Beginning Of the current Partition (BOP). We
2587 * currently support only one partition.
2588 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002589static int idetape_rewind_tape (ide_drive_t *drive)
2590{
2591 int retval;
2592 idetape_pc_t pc;
Borislav Petkov8004a8c2008-02-06 02:57:51 +01002593 idetape_tape_t *tape;
2594 tape = drive->driver_data;
2595
2596 debug_log(DBG_SENSE, "Enter %s\n", __func__);
2597
Linus Torvalds1da177e2005-04-16 15:20:36 -07002598 idetape_create_rewind_cmd(drive, &pc);
2599 retval = idetape_queue_pc_tail(drive, &pc);
2600 if (retval)
2601 return retval;
2602
2603 idetape_create_read_position_cmd(&pc);
2604 retval = idetape_queue_pc_tail(drive, &pc);
2605 if (retval)
2606 return retval;
2607 return 0;
2608}
2609
Borislav Petkov3c98bf32008-02-06 02:57:53 +01002610/* mtio.h compatible commands should be issued to the chrdev interface. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002611static int idetape_blkdev_ioctl(ide_drive_t *drive, unsigned int cmd, unsigned long arg)
2612{
2613 idetape_tape_t *tape = drive->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002614 void __user *argp = (void __user *)arg;
2615
Borislav Petkovd59823f2008-02-02 19:56:51 +01002616 struct idetape_config {
2617 int dsc_rw_frequency;
2618 int dsc_media_access_frequency;
2619 int nr_stages;
2620 } config;
2621
Borislav Petkov8004a8c2008-02-06 02:57:51 +01002622 debug_log(DBG_PROCS, "Enter %s\n", __func__);
2623
Linus Torvalds1da177e2005-04-16 15:20:36 -07002624 switch (cmd) {
2625 case 0x0340:
Borislav Petkovd59823f2008-02-02 19:56:51 +01002626 if (copy_from_user(&config, argp, sizeof(config)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002627 return -EFAULT;
Borislav Petkov54bb2072008-02-06 02:57:52 +01002628 tape->best_dsc_rw_freq = config.dsc_rw_frequency;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002629 tape->max_stages = config.nr_stages;
2630 break;
2631 case 0x0350:
Borislav Petkov54bb2072008-02-06 02:57:52 +01002632 config.dsc_rw_frequency = (int) tape->best_dsc_rw_freq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002633 config.nr_stages = tape->max_stages;
Borislav Petkovd59823f2008-02-02 19:56:51 +01002634 if (copy_to_user(argp, &config, sizeof(config)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002635 return -EFAULT;
2636 break;
2637 default:
2638 return -EIO;
2639 }
2640 return 0;
2641}
2642
2643/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +01002644 * The function below is now a bit more complicated than just passing the
2645 * command to the tape since we may have crossed some filemarks during our
2646 * pipelined read-ahead mode. As a minor side effect, the pipeline enables us to
2647 * support MTFSFM when the filemark is in our internal pipeline even if the tape
2648 * doesn't support spacing over filemarks in the reverse direction.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002649 */
2650static int idetape_space_over_filemarks (ide_drive_t *drive,short mt_op,int mt_count)
2651{
2652 idetape_tape_t *tape = drive->driver_data;
2653 idetape_pc_t pc;
2654 unsigned long flags;
2655 int retval,count=0;
Borislav Petkovb6422012008-02-02 19:56:49 +01002656 int sprev = !!(tape->caps[4] & 0x20);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002657
2658 if (mt_count == 0)
2659 return 0;
2660 if (MTBSF == mt_op || MTBSFM == mt_op) {
Borislav Petkovb6422012008-02-02 19:56:49 +01002661 if (!sprev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002662 return -EIO;
2663 mt_count = - mt_count;
2664 }
2665
Borislav Petkov54abf372008-02-06 02:57:52 +01002666 if (tape->chrdev_dir == IDETAPE_DIR_READ) {
Borislav Petkov3c98bf32008-02-06 02:57:53 +01002667 /* its a read-ahead buffer, scan it for crossed filemarks. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002668 tape->merge_stage_size = 0;
2669 if (test_and_clear_bit(IDETAPE_FILEMARK, &tape->flags))
2670 ++count;
2671 while (tape->first_stage != NULL) {
2672 if (count == mt_count) {
2673 if (mt_op == MTFSFM)
2674 set_bit(IDETAPE_FILEMARK, &tape->flags);
2675 return 0;
2676 }
Borislav Petkov54bb2072008-02-06 02:57:52 +01002677 spin_lock_irqsave(&tape->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002678 if (tape->first_stage == tape->active_stage) {
2679 /*
Borislav Petkov3c98bf32008-02-06 02:57:53 +01002680 * We have reached the active stage in the read
2681 * pipeline. There is no point in allowing the
2682 * drive to continue reading any farther, so we
2683 * stop the pipeline.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002684 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +01002685 * This section should be moved to a separate
2686 * subroutine because similar operations are
2687 * done in __idetape_discard_read_pipeline(),
2688 * for example.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002689 */
2690 tape->next_stage = NULL;
Borislav Petkov54bb2072008-02-06 02:57:52 +01002691 spin_unlock_irqrestore(&tape->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002692 idetape_wait_first_stage(drive);
2693 tape->next_stage = tape->first_stage->next;
2694 } else
Borislav Petkov54bb2072008-02-06 02:57:52 +01002695 spin_unlock_irqrestore(&tape->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002696 if (tape->first_stage->rq.errors == IDETAPE_ERROR_FILEMARK)
2697 ++count;
2698 idetape_remove_stage_head(drive);
2699 }
2700 idetape_discard_read_pipeline(drive, 0);
2701 }
2702
2703 /*
Borislav Petkov3c98bf32008-02-06 02:57:53 +01002704 * The filemark was not found in our internal pipeline; now we can issue
2705 * the space command.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002706 */
2707 switch (mt_op) {
2708 case MTFSF:
2709 case MTBSF:
2710 idetape_create_space_cmd(&pc,mt_count-count,IDETAPE_SPACE_OVER_FILEMARK);
2711 return (idetape_queue_pc_tail(drive, &pc));
2712 case MTFSFM:
2713 case MTBSFM:
Borislav Petkovb6422012008-02-02 19:56:49 +01002714 if (!sprev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002715 return (-EIO);
2716 retval = idetape_space_over_filemarks(drive, MTFSF, mt_count-count);
2717 if (retval) return (retval);
2718 count = (MTBSFM == mt_op ? 1 : -1);
2719 return (idetape_space_over_filemarks(drive, MTFSF, count));
2720 default:
2721 printk(KERN_ERR "ide-tape: MTIO operation %d not supported\n",mt_op);
2722 return (-EIO);
2723 }
2724}
2725
2726
2727/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +01002728 * Our character device read / write functions.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002729 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +01002730 * The tape is optimized to maximize throughput when it is transferring an
2731 * integral number of the "continuous transfer limit", which is a parameter of
2732 * the specific tape (26kB on my particular tape, 32kB for Onstream).
Linus Torvalds1da177e2005-04-16 15:20:36 -07002733 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +01002734 * As of version 1.3 of the driver, the character device provides an abstract
2735 * continuous view of the media - any mix of block sizes (even 1 byte) on the
2736 * same backup/restore procedure is supported. The driver will internally
2737 * convert the requests to the recommended transfer unit, so that an unmatch
2738 * between the user's block size to the recommended size will only result in a
2739 * (slightly) increased driver overhead, but will no longer hit performance.
2740 * This is not applicable to Onstream.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002741 */
2742static ssize_t idetape_chrdev_read (struct file *file, char __user *buf,
2743 size_t count, loff_t *ppos)
2744{
2745 struct ide_tape_obj *tape = ide_tape_f(file);
2746 ide_drive_t *drive = tape->drive;
2747 ssize_t bytes_read,temp, actually_read = 0, rc;
Daniel Walkerdcd96372006-06-25 05:47:37 -07002748 ssize_t ret = 0;
Borislav Petkovb6422012008-02-02 19:56:49 +01002749 u16 ctl = *(u16 *)&tape->caps[12];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002750
Borislav Petkov8004a8c2008-02-06 02:57:51 +01002751 debug_log(DBG_CHRDEV, "Enter %s, count %Zd\n", __func__, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002752
Borislav Petkov54abf372008-02-06 02:57:52 +01002753 if (tape->chrdev_dir != IDETAPE_DIR_READ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002754 if (test_bit(IDETAPE_DETECT_BS, &tape->flags))
Borislav Petkov54bb2072008-02-06 02:57:52 +01002755 if (count > tape->blk_size &&
2756 (count % tape->blk_size) == 0)
2757 tape->user_bs_factor = count / tape->blk_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002758 }
Borislav Petkov8d06bfa2008-02-06 02:57:53 +01002759 rc = idetape_init_read(drive, tape->max_stages);
2760 if (rc < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002761 return rc;
2762 if (count == 0)
2763 return (0);
2764 if (tape->merge_stage_size) {
2765 actually_read = min((unsigned int)(tape->merge_stage_size), (unsigned int)count);
Daniel Walkerdcd96372006-06-25 05:47:37 -07002766 if (idetape_copy_stage_to_user(tape, buf, tape->merge_stage, actually_read))
2767 ret = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002768 buf += actually_read;
2769 tape->merge_stage_size -= actually_read;
2770 count -= actually_read;
2771 }
2772 while (count >= tape->stage_size) {
Borislav Petkovb6422012008-02-02 19:56:49 +01002773 bytes_read = idetape_add_chrdev_read_request(drive, ctl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002774 if (bytes_read <= 0)
2775 goto finish;
Daniel Walkerdcd96372006-06-25 05:47:37 -07002776 if (idetape_copy_stage_to_user(tape, buf, tape->merge_stage, bytes_read))
2777 ret = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002778 buf += bytes_read;
2779 count -= bytes_read;
2780 actually_read += bytes_read;
2781 }
2782 if (count) {
Borislav Petkovb6422012008-02-02 19:56:49 +01002783 bytes_read = idetape_add_chrdev_read_request(drive, ctl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002784 if (bytes_read <= 0)
2785 goto finish;
2786 temp = min((unsigned long)count, (unsigned long)bytes_read);
Daniel Walkerdcd96372006-06-25 05:47:37 -07002787 if (idetape_copy_stage_to_user(tape, buf, tape->merge_stage, temp))
2788 ret = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002789 actually_read += temp;
2790 tape->merge_stage_size = bytes_read-temp;
2791 }
2792finish:
2793 if (!actually_read && test_bit(IDETAPE_FILEMARK, &tape->flags)) {
Borislav Petkov8004a8c2008-02-06 02:57:51 +01002794 debug_log(DBG_SENSE, "%s: spacing over filemark\n", tape->name);
2795
Linus Torvalds1da177e2005-04-16 15:20:36 -07002796 idetape_space_over_filemarks(drive, MTFSF, 1);
2797 return 0;
2798 }
Daniel Walkerdcd96372006-06-25 05:47:37 -07002799
2800 return (ret) ? ret : actually_read;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002801}
2802
2803static ssize_t idetape_chrdev_write (struct file *file, const char __user *buf,
2804 size_t count, loff_t *ppos)
2805{
2806 struct ide_tape_obj *tape = ide_tape_f(file);
2807 ide_drive_t *drive = tape->drive;
Daniel Walkerdcd96372006-06-25 05:47:37 -07002808 ssize_t actually_written = 0;
2809 ssize_t ret = 0;
Borislav Petkovb6422012008-02-02 19:56:49 +01002810 u16 ctl = *(u16 *)&tape->caps[12];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002811
2812 /* The drive is write protected. */
2813 if (tape->write_prot)
2814 return -EACCES;
2815
Borislav Petkov8004a8c2008-02-06 02:57:51 +01002816 debug_log(DBG_CHRDEV, "Enter %s, count %Zd\n", __func__, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002817
2818 /* Initialize write operation */
Borislav Petkov54abf372008-02-06 02:57:52 +01002819 if (tape->chrdev_dir != IDETAPE_DIR_WRITE) {
2820 if (tape->chrdev_dir == IDETAPE_DIR_READ)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002821 idetape_discard_read_pipeline(drive, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002822 if (tape->merge_stage || tape->merge_stage_size) {
2823 printk(KERN_ERR "ide-tape: merge_stage_size "
2824 "should be 0 now\n");
2825 tape->merge_stage_size = 0;
2826 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002827 if ((tape->merge_stage = __idetape_kmalloc_stage(tape, 0, 0)) == NULL)
2828 return -ENOMEM;
Borislav Petkov54abf372008-02-06 02:57:52 +01002829 tape->chrdev_dir = IDETAPE_DIR_WRITE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002830 idetape_init_merge_stage(tape);
2831
2832 /*
Borislav Petkov3c98bf32008-02-06 02:57:53 +01002833 * Issue a write 0 command to ensure that DSC handshake is
2834 * switched from completion mode to buffer available mode. No
2835 * point in issuing this if DSC overlap isn't supported, some
2836 * drives (Seagate STT3401A) will return an error.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002837 */
2838 if (drive->dsc_overlap) {
Daniel Walkerdcd96372006-06-25 05:47:37 -07002839 ssize_t retval = idetape_queue_rw_tail(drive, REQ_IDETAPE_WRITE, 0, tape->merge_stage->bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002840 if (retval < 0) {
2841 __idetape_kfree_stage(tape->merge_stage);
2842 tape->merge_stage = NULL;
Borislav Petkov54abf372008-02-06 02:57:52 +01002843 tape->chrdev_dir = IDETAPE_DIR_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002844 return retval;
2845 }
2846 }
2847 }
2848 if (count == 0)
2849 return (0);
2850 if (tape->restart_speed_control_req)
2851 idetape_restart_speed_control(drive);
2852 if (tape->merge_stage_size) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002853 if (tape->merge_stage_size >= tape->stage_size) {
2854 printk(KERN_ERR "ide-tape: bug: merge buffer too big\n");
2855 tape->merge_stage_size = 0;
2856 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002857 actually_written = min((unsigned int)(tape->stage_size - tape->merge_stage_size), (unsigned int)count);
Daniel Walkerdcd96372006-06-25 05:47:37 -07002858 if (idetape_copy_stage_from_user(tape, tape->merge_stage, buf, actually_written))
2859 ret = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002860 buf += actually_written;
2861 tape->merge_stage_size += actually_written;
2862 count -= actually_written;
2863
2864 if (tape->merge_stage_size == tape->stage_size) {
Daniel Walkerdcd96372006-06-25 05:47:37 -07002865 ssize_t retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002866 tape->merge_stage_size = 0;
Borislav Petkovb6422012008-02-02 19:56:49 +01002867 retval = idetape_add_chrdev_write_request(drive, ctl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002868 if (retval <= 0)
2869 return (retval);
2870 }
2871 }
2872 while (count >= tape->stage_size) {
Daniel Walkerdcd96372006-06-25 05:47:37 -07002873 ssize_t retval;
2874 if (idetape_copy_stage_from_user(tape, tape->merge_stage, buf, tape->stage_size))
2875 ret = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002876 buf += tape->stage_size;
2877 count -= tape->stage_size;
Borislav Petkovb6422012008-02-02 19:56:49 +01002878 retval = idetape_add_chrdev_write_request(drive, ctl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002879 actually_written += tape->stage_size;
2880 if (retval <= 0)
2881 return (retval);
2882 }
2883 if (count) {
2884 actually_written += count;
Daniel Walkerdcd96372006-06-25 05:47:37 -07002885 if (idetape_copy_stage_from_user(tape, tape->merge_stage, buf, count))
2886 ret = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002887 tape->merge_stage_size += count;
2888 }
Daniel Walkerdcd96372006-06-25 05:47:37 -07002889 return (ret) ? ret : actually_written;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002890}
2891
2892static int idetape_write_filemark (ide_drive_t *drive)
2893{
2894 idetape_pc_t pc;
2895
2896 /* Write a filemark */
2897 idetape_create_write_filemark_cmd(drive, &pc, 1);
2898 if (idetape_queue_pc_tail(drive, &pc)) {
2899 printk(KERN_ERR "ide-tape: Couldn't write a filemark\n");
2900 return -EIO;
2901 }
2902 return 0;
2903}
2904
2905/*
Borislav Petkovd99c9da2008-02-02 19:56:51 +01002906 * Called from idetape_chrdev_ioctl when the general mtio MTIOCTOP ioctl is
2907 * requested.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002908 *
Borislav Petkovd99c9da2008-02-02 19:56:51 +01002909 * Note: MTBSF and MTBSFM are not supported when the tape doesn't support
2910 * spacing over filemarks in the reverse direction. In this case, MTFSFM is also
2911 * usually not supported (it is supported in the rare case in which we crossed
2912 * the filemark during our read-ahead pipelined operation mode).
Linus Torvalds1da177e2005-04-16 15:20:36 -07002913 *
Borislav Petkovd99c9da2008-02-02 19:56:51 +01002914 * The following commands are currently not supported:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002915 *
Borislav Petkovd99c9da2008-02-02 19:56:51 +01002916 * MTFSS, MTBSS, MTWSM, MTSETDENSITY, MTSETDRVBUFFER, MT_ST_BOOLEANS,
2917 * MT_ST_WRITE_THRESHOLD.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002918 */
Borislav Petkovd99c9da2008-02-02 19:56:51 +01002919static int idetape_mtioctop(ide_drive_t *drive, short mt_op, int mt_count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002920{
2921 idetape_tape_t *tape = drive->driver_data;
2922 idetape_pc_t pc;
2923 int i,retval;
2924
Borislav Petkov8004a8c2008-02-06 02:57:51 +01002925 debug_log(DBG_ERR, "Handling MTIOCTOP ioctl: mt_op=%d, mt_count=%d\n",
2926 mt_op, mt_count);
Borislav Petkov3c98bf32008-02-06 02:57:53 +01002927 /* Commands which need our pipelined read-ahead stages. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002928 switch (mt_op) {
2929 case MTFSF:
2930 case MTFSFM:
2931 case MTBSF:
2932 case MTBSFM:
2933 if (!mt_count)
2934 return (0);
2935 return (idetape_space_over_filemarks(drive,mt_op,mt_count));
2936 default:
2937 break;
2938 }
2939 switch (mt_op) {
2940 case MTWEOF:
2941 if (tape->write_prot)
2942 return -EACCES;
2943 idetape_discard_read_pipeline(drive, 1);
2944 for (i = 0; i < mt_count; i++) {
2945 retval = idetape_write_filemark(drive);
2946 if (retval)
2947 return retval;
2948 }
2949 return (0);
2950 case MTREW:
2951 idetape_discard_read_pipeline(drive, 0);
2952 if (idetape_rewind_tape(drive))
2953 return -EIO;
2954 return 0;
2955 case MTLOAD:
2956 idetape_discard_read_pipeline(drive, 0);
2957 idetape_create_load_unload_cmd(drive, &pc, IDETAPE_LU_LOAD_MASK);
2958 return (idetape_queue_pc_tail(drive, &pc));
2959 case MTUNLOAD:
2960 case MTOFFL:
2961 /*
2962 * If door is locked, attempt to unlock before
2963 * attempting to eject.
2964 */
2965 if (tape->door_locked) {
2966 if (idetape_create_prevent_cmd(drive, &pc, 0))
2967 if (!idetape_queue_pc_tail(drive, &pc))
2968 tape->door_locked = DOOR_UNLOCKED;
2969 }
2970 idetape_discard_read_pipeline(drive, 0);
2971 idetape_create_load_unload_cmd(drive, &pc,!IDETAPE_LU_LOAD_MASK);
2972 retval = idetape_queue_pc_tail(drive, &pc);
2973 if (!retval)
2974 clear_bit(IDETAPE_MEDIUM_PRESENT, &tape->flags);
2975 return retval;
2976 case MTNOP:
2977 idetape_discard_read_pipeline(drive, 0);
2978 return (idetape_flush_tape_buffers(drive));
2979 case MTRETEN:
2980 idetape_discard_read_pipeline(drive, 0);
2981 idetape_create_load_unload_cmd(drive, &pc,IDETAPE_LU_RETENSION_MASK | IDETAPE_LU_LOAD_MASK);
2982 return (idetape_queue_pc_tail(drive, &pc));
2983 case MTEOM:
2984 idetape_create_space_cmd(&pc, 0, IDETAPE_SPACE_TO_EOD);
2985 return (idetape_queue_pc_tail(drive, &pc));
2986 case MTERASE:
2987 (void) idetape_rewind_tape(drive);
2988 idetape_create_erase_cmd(&pc);
2989 return (idetape_queue_pc_tail(drive, &pc));
2990 case MTSETBLK:
2991 if (mt_count) {
Borislav Petkov54bb2072008-02-06 02:57:52 +01002992 if (mt_count < tape->blk_size ||
2993 mt_count % tape->blk_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002994 return -EIO;
Borislav Petkov54bb2072008-02-06 02:57:52 +01002995 tape->user_bs_factor = mt_count /
2996 tape->blk_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002997 clear_bit(IDETAPE_DETECT_BS, &tape->flags);
2998 } else
2999 set_bit(IDETAPE_DETECT_BS, &tape->flags);
3000 return 0;
3001 case MTSEEK:
3002 idetape_discard_read_pipeline(drive, 0);
3003 return idetape_position_tape(drive, mt_count * tape->user_bs_factor, tape->partition, 0);
3004 case MTSETPART:
3005 idetape_discard_read_pipeline(drive, 0);
3006 return (idetape_position_tape(drive, 0, mt_count, 0));
3007 case MTFSR:
3008 case MTBSR:
3009 case MTLOCK:
3010 if (!idetape_create_prevent_cmd(drive, &pc, 1))
3011 return 0;
3012 retval = idetape_queue_pc_tail(drive, &pc);
3013 if (retval) return retval;
3014 tape->door_locked = DOOR_EXPLICITLY_LOCKED;
3015 return 0;
3016 case MTUNLOCK:
3017 if (!idetape_create_prevent_cmd(drive, &pc, 0))
3018 return 0;
3019 retval = idetape_queue_pc_tail(drive, &pc);
3020 if (retval) return retval;
3021 tape->door_locked = DOOR_UNLOCKED;
3022 return 0;
3023 default:
3024 printk(KERN_ERR "ide-tape: MTIO operation %d not "
3025 "supported\n", mt_op);
3026 return (-EIO);
3027 }
3028}
3029
3030/*
Borislav Petkovd99c9da2008-02-02 19:56:51 +01003031 * Our character device ioctls. General mtio.h magnetic io commands are
3032 * supported here, and not in the corresponding block interface. Our own
3033 * ide-tape ioctls are supported on both interfaces.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003034 */
Borislav Petkovd99c9da2008-02-02 19:56:51 +01003035static int idetape_chrdev_ioctl(struct inode *inode, struct file *file,
3036 unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003037{
3038 struct ide_tape_obj *tape = ide_tape_f(file);
3039 ide_drive_t *drive = tape->drive;
3040 struct mtop mtop;
3041 struct mtget mtget;
3042 struct mtpos mtpos;
Borislav Petkov54bb2072008-02-06 02:57:52 +01003043 int block_offset = 0, position = tape->first_frame;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003044 void __user *argp = (void __user *)arg;
3045
Borislav Petkov8004a8c2008-02-06 02:57:51 +01003046 debug_log(DBG_CHRDEV, "Enter %s, cmd=%u\n", __func__, cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003047
3048 tape->restart_speed_control_req = 1;
Borislav Petkov54abf372008-02-06 02:57:52 +01003049 if (tape->chrdev_dir == IDETAPE_DIR_WRITE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003050 idetape_empty_write_pipeline(drive);
3051 idetape_flush_tape_buffers(drive);
3052 }
3053 if (cmd == MTIOCGET || cmd == MTIOCPOS) {
Borislav Petkov54bb2072008-02-06 02:57:52 +01003054 block_offset = idetape_pipeline_size(drive) /
3055 (tape->blk_size * tape->user_bs_factor);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003056 if ((position = idetape_read_position(drive)) < 0)
3057 return -EIO;
3058 }
3059 switch (cmd) {
3060 case MTIOCTOP:
3061 if (copy_from_user(&mtop, argp, sizeof (struct mtop)))
3062 return -EFAULT;
3063 return (idetape_mtioctop(drive,mtop.mt_op,mtop.mt_count));
3064 case MTIOCGET:
3065 memset(&mtget, 0, sizeof (struct mtget));
3066 mtget.mt_type = MT_ISSCSI2;
3067 mtget.mt_blkno = position / tape->user_bs_factor - block_offset;
Borislav Petkov54bb2072008-02-06 02:57:52 +01003068 mtget.mt_dsreg =
3069 ((tape->blk_size * tape->user_bs_factor)
3070 << MT_ST_BLKSIZE_SHIFT) & MT_ST_BLKSIZE_MASK;
3071
Linus Torvalds1da177e2005-04-16 15:20:36 -07003072 if (tape->drv_write_prot) {
3073 mtget.mt_gstat |= GMT_WR_PROT(0xffffffff);
3074 }
3075 if (copy_to_user(argp, &mtget, sizeof(struct mtget)))
3076 return -EFAULT;
3077 return 0;
3078 case MTIOCPOS:
3079 mtpos.mt_blkno = position / tape->user_bs_factor - block_offset;
3080 if (copy_to_user(argp, &mtpos, sizeof(struct mtpos)))
3081 return -EFAULT;
3082 return 0;
3083 default:
Borislav Petkov54abf372008-02-06 02:57:52 +01003084 if (tape->chrdev_dir == IDETAPE_DIR_READ)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003085 idetape_discard_read_pipeline(drive, 1);
3086 return idetape_blkdev_ioctl(drive, cmd, arg);
3087 }
3088}
3089
Borislav Petkov3cffb9c2008-02-02 19:56:50 +01003090/*
3091 * Do a mode sense page 0 with block descriptor and if it succeeds set the tape
3092 * block size with the reported value.
3093 */
3094static void ide_tape_get_bsize_from_bdesc(ide_drive_t *drive)
3095{
3096 idetape_tape_t *tape = drive->driver_data;
3097 idetape_pc_t pc;
3098
3099 idetape_create_mode_sense_cmd(&pc, IDETAPE_BLOCK_DESCRIPTOR);
3100 if (idetape_queue_pc_tail(drive, &pc)) {
3101 printk(KERN_ERR "ide-tape: Can't get block descriptor\n");
Borislav Petkov54bb2072008-02-06 02:57:52 +01003102 if (tape->blk_size == 0) {
Borislav Petkov3cffb9c2008-02-02 19:56:50 +01003103 printk(KERN_WARNING "ide-tape: Cannot deal with zero "
3104 "block size, assuming 32k\n");
Borislav Petkov54bb2072008-02-06 02:57:52 +01003105 tape->blk_size = 32768;
Borislav Petkov3cffb9c2008-02-02 19:56:50 +01003106 }
3107 return;
3108 }
Borislav Petkov54bb2072008-02-06 02:57:52 +01003109 tape->blk_size = (pc.buffer[4 + 5] << 16) +
Borislav Petkov3cffb9c2008-02-02 19:56:50 +01003110 (pc.buffer[4 + 6] << 8) +
3111 pc.buffer[4 + 7];
3112 tape->drv_write_prot = (pc.buffer[2] & 0x80) >> 7;
3113}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003114
Linus Torvalds1da177e2005-04-16 15:20:36 -07003115static int idetape_chrdev_open (struct inode *inode, struct file *filp)
3116{
3117 unsigned int minor = iminor(inode), i = minor & ~0xc0;
3118 ide_drive_t *drive;
3119 idetape_tape_t *tape;
3120 idetape_pc_t pc;
3121 int retval;
3122
Borislav Petkov8004a8c2008-02-06 02:57:51 +01003123 if (i >= MAX_HWIFS * MAX_DRIVES)
3124 return -ENXIO;
3125
3126 tape = ide_tape_chrdev_get(i);
3127 if (!tape)
3128 return -ENXIO;
3129
3130 debug_log(DBG_CHRDEV, "Enter %s\n", __func__);
3131
Linus Torvalds1da177e2005-04-16 15:20:36 -07003132 /*
3133 * We really want to do nonseekable_open(inode, filp); here, but some
3134 * versions of tar incorrectly call lseek on tapes and bail out if that
3135 * fails. So we disallow pread() and pwrite(), but permit lseeks.
3136 */
3137 filp->f_mode &= ~(FMODE_PREAD | FMODE_PWRITE);
3138
Linus Torvalds1da177e2005-04-16 15:20:36 -07003139 drive = tape->drive;
3140
3141 filp->private_data = tape;
3142
3143 if (test_and_set_bit(IDETAPE_BUSY, &tape->flags)) {
3144 retval = -EBUSY;
3145 goto out_put_tape;
3146 }
3147
3148 retval = idetape_wait_ready(drive, 60 * HZ);
3149 if (retval) {
3150 clear_bit(IDETAPE_BUSY, &tape->flags);
3151 printk(KERN_ERR "ide-tape: %s: drive not ready\n", tape->name);
3152 goto out_put_tape;
3153 }
3154
3155 idetape_read_position(drive);
3156 if (!test_bit(IDETAPE_ADDRESS_VALID, &tape->flags))
3157 (void)idetape_rewind_tape(drive);
3158
Borislav Petkov54abf372008-02-06 02:57:52 +01003159 if (tape->chrdev_dir != IDETAPE_DIR_READ)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003160 clear_bit(IDETAPE_PIPELINE_ERROR, &tape->flags);
3161
3162 /* Read block size and write protect status from drive. */
Borislav Petkov3cffb9c2008-02-02 19:56:50 +01003163 ide_tape_get_bsize_from_bdesc(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003164
3165 /* Set write protect flag if device is opened as read-only. */
3166 if ((filp->f_flags & O_ACCMODE) == O_RDONLY)
3167 tape->write_prot = 1;
3168 else
3169 tape->write_prot = tape->drv_write_prot;
3170
3171 /* Make sure drive isn't write protected if user wants to write. */
3172 if (tape->write_prot) {
3173 if ((filp->f_flags & O_ACCMODE) == O_WRONLY ||
3174 (filp->f_flags & O_ACCMODE) == O_RDWR) {
3175 clear_bit(IDETAPE_BUSY, &tape->flags);
3176 retval = -EROFS;
3177 goto out_put_tape;
3178 }
3179 }
3180
Borislav Petkov3c98bf32008-02-06 02:57:53 +01003181 /* Lock the tape drive door so user can't eject. */
Borislav Petkov54abf372008-02-06 02:57:52 +01003182 if (tape->chrdev_dir == IDETAPE_DIR_NONE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003183 if (idetape_create_prevent_cmd(drive, &pc, 1)) {
3184 if (!idetape_queue_pc_tail(drive, &pc)) {
3185 if (tape->door_locked != DOOR_EXPLICITLY_LOCKED)
3186 tape->door_locked = DOOR_LOCKED;
3187 }
3188 }
3189 }
3190 idetape_restart_speed_control(drive);
3191 tape->restart_speed_control_req = 0;
3192 return 0;
3193
3194out_put_tape:
3195 ide_tape_put(tape);
3196 return retval;
3197}
3198
3199static void idetape_write_release (ide_drive_t *drive, unsigned int minor)
3200{
3201 idetape_tape_t *tape = drive->driver_data;
3202
3203 idetape_empty_write_pipeline(drive);
3204 tape->merge_stage = __idetape_kmalloc_stage(tape, 1, 0);
3205 if (tape->merge_stage != NULL) {
Borislav Petkov54bb2072008-02-06 02:57:52 +01003206 idetape_pad_zeros(drive, tape->blk_size *
3207 (tape->user_bs_factor - 1));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003208 __idetape_kfree_stage(tape->merge_stage);
3209 tape->merge_stage = NULL;
3210 }
3211 idetape_write_filemark(drive);
3212 idetape_flush_tape_buffers(drive);
3213 idetape_flush_tape_buffers(drive);
3214}
3215
Linus Torvalds1da177e2005-04-16 15:20:36 -07003216static int idetape_chrdev_release (struct inode *inode, struct file *filp)
3217{
3218 struct ide_tape_obj *tape = ide_tape_f(filp);
3219 ide_drive_t *drive = tape->drive;
3220 idetape_pc_t pc;
3221 unsigned int minor = iminor(inode);
3222
3223 lock_kernel();
3224 tape = drive->driver_data;
Borislav Petkov8004a8c2008-02-06 02:57:51 +01003225
3226 debug_log(DBG_CHRDEV, "Enter %s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003227
Borislav Petkov54abf372008-02-06 02:57:52 +01003228 if (tape->chrdev_dir == IDETAPE_DIR_WRITE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003229 idetape_write_release(drive, minor);
Borislav Petkov54abf372008-02-06 02:57:52 +01003230 if (tape->chrdev_dir == IDETAPE_DIR_READ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003231 if (minor < 128)
3232 idetape_discard_read_pipeline(drive, 1);
3233 else
3234 idetape_wait_for_pipeline(drive);
3235 }
3236 if (tape->cache_stage != NULL) {
3237 __idetape_kfree_stage(tape->cache_stage);
3238 tape->cache_stage = NULL;
3239 }
3240 if (minor < 128 && test_bit(IDETAPE_MEDIUM_PRESENT, &tape->flags))
3241 (void) idetape_rewind_tape(drive);
Borislav Petkov54abf372008-02-06 02:57:52 +01003242 if (tape->chrdev_dir == IDETAPE_DIR_NONE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003243 if (tape->door_locked == DOOR_LOCKED) {
3244 if (idetape_create_prevent_cmd(drive, &pc, 0)) {
3245 if (!idetape_queue_pc_tail(drive, &pc))
3246 tape->door_locked = DOOR_UNLOCKED;
3247 }
3248 }
3249 }
3250 clear_bit(IDETAPE_BUSY, &tape->flags);
3251 ide_tape_put(tape);
3252 unlock_kernel();
3253 return 0;
3254}
3255
3256/*
3257 * idetape_identify_device is called to check the contents of the
3258 * ATAPI IDENTIFY command results. We return:
3259 *
3260 * 1 If the tape can be supported by us, based on the information
3261 * we have so far.
3262 *
3263 * 0 If this tape driver is not currently supported by us.
3264 */
3265static int idetape_identify_device (ide_drive_t *drive)
3266{
3267 struct idetape_id_gcw gcw;
3268 struct hd_driveid *id = drive->id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003269
3270 if (drive->id_read == 0)
3271 return 1;
3272
3273 *((unsigned short *) &gcw) = id->config;
3274
Linus Torvalds1da177e2005-04-16 15:20:36 -07003275 /* Check that we can support this device */
3276
Bartlomiej Zolnierkiewicz16422de32008-02-02 19:56:48 +01003277 if (gcw.protocol != 2)
3278 printk(KERN_ERR "ide-tape: Protocol (0x%02x) is not ATAPI\n",
3279 gcw.protocol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003280 else if (gcw.device_type != 1)
Bartlomiej Zolnierkiewicz16422de32008-02-02 19:56:48 +01003281 printk(KERN_ERR "ide-tape: Device type (0x%02x) is not set "
3282 "to tape\n", gcw.device_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003283 else if (!gcw.removable)
3284 printk(KERN_ERR "ide-tape: The removable flag is not set\n");
3285 else if (gcw.packet_size != 0) {
Bartlomiej Zolnierkiewicz16422de32008-02-02 19:56:48 +01003286 printk(KERN_ERR "ide-tape: Packet size (0x%02x) is not 12 "
3287 "bytes long\n", gcw.packet_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003288 } else
3289 return 1;
3290 return 0;
3291}
3292
Borislav Petkov6d29c8f2008-02-02 19:56:49 +01003293static void idetape_get_inquiry_results(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003294{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003295 idetape_tape_t *tape = drive->driver_data;
3296 idetape_pc_t pc;
Borislav Petkov41f81d542008-02-06 02:57:52 +01003297 char fw_rev[6], vendor_id[10], product_id[18];
Borislav Petkov6d29c8f2008-02-02 19:56:49 +01003298
Linus Torvalds1da177e2005-04-16 15:20:36 -07003299 idetape_create_inquiry_cmd(&pc);
3300 if (idetape_queue_pc_tail(drive, &pc)) {
Borislav Petkov6d29c8f2008-02-02 19:56:49 +01003301 printk(KERN_ERR "ide-tape: %s: can't get INQUIRY results\n",
3302 tape->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003303 return;
3304 }
Borislav Petkov41f81d542008-02-06 02:57:52 +01003305 memcpy(vendor_id, &pc.buffer[8], 8);
3306 memcpy(product_id, &pc.buffer[16], 16);
3307 memcpy(fw_rev, &pc.buffer[32], 4);
Borislav Petkov6d29c8f2008-02-02 19:56:49 +01003308
Borislav Petkov41f81d542008-02-06 02:57:52 +01003309 ide_fixstring(vendor_id, 10, 0);
3310 ide_fixstring(product_id, 18, 0);
3311 ide_fixstring(fw_rev, 6, 0);
3312
Borislav Petkov6d29c8f2008-02-02 19:56:49 +01003313 printk(KERN_INFO "ide-tape: %s <-> %s: %s %s rev %s\n",
Borislav Petkov41f81d542008-02-06 02:57:52 +01003314 drive->name, tape->name, vendor_id, product_id, fw_rev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003315}
3316
3317/*
Borislav Petkovb6422012008-02-02 19:56:49 +01003318 * Ask the tape about its various parameters. In particular, we will adjust our
3319 * data transfer buffer size to the recommended value as returned by the tape.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003320 */
3321static void idetape_get_mode_sense_results (ide_drive_t *drive)
3322{
3323 idetape_tape_t *tape = drive->driver_data;
3324 idetape_pc_t pc;
Borislav Petkovb6422012008-02-02 19:56:49 +01003325 u8 *caps;
3326 u8 speed, max_speed;
Borislav Petkov47314fa2008-02-02 19:56:48 +01003327
Linus Torvalds1da177e2005-04-16 15:20:36 -07003328 idetape_create_mode_sense_cmd(&pc, IDETAPE_CAPABILITIES_PAGE);
3329 if (idetape_queue_pc_tail(drive, &pc)) {
Borislav Petkovb6422012008-02-02 19:56:49 +01003330 printk(KERN_ERR "ide-tape: Can't get tape parameters - assuming"
3331 " some default values\n");
Borislav Petkov54bb2072008-02-06 02:57:52 +01003332 tape->blk_size = 512;
Borislav Petkovb6422012008-02-02 19:56:49 +01003333 put_unaligned(52, (u16 *)&tape->caps[12]);
3334 put_unaligned(540, (u16 *)&tape->caps[14]);
3335 put_unaligned(6*52, (u16 *)&tape->caps[16]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003336 return;
3337 }
Borislav Petkovb6422012008-02-02 19:56:49 +01003338 caps = pc.buffer + 4 + pc.buffer[3];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003339
Borislav Petkovb6422012008-02-02 19:56:49 +01003340 /* convert to host order and save for later use */
3341 speed = be16_to_cpu(*(u16 *)&caps[14]);
3342 max_speed = be16_to_cpu(*(u16 *)&caps[8]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003343
Borislav Petkovb6422012008-02-02 19:56:49 +01003344 put_unaligned(max_speed, (u16 *)&caps[8]);
3345 put_unaligned(be16_to_cpu(*(u16 *)&caps[12]), (u16 *)&caps[12]);
3346 put_unaligned(speed, (u16 *)&caps[14]);
3347 put_unaligned(be16_to_cpu(*(u16 *)&caps[16]), (u16 *)&caps[16]);
3348
3349 if (!speed) {
3350 printk(KERN_INFO "ide-tape: %s: invalid tape speed "
3351 "(assuming 650KB/sec)\n", drive->name);
3352 put_unaligned(650, (u16 *)&caps[14]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003353 }
Borislav Petkovb6422012008-02-02 19:56:49 +01003354 if (!max_speed) {
3355 printk(KERN_INFO "ide-tape: %s: invalid max_speed "
3356 "(assuming 650KB/sec)\n", drive->name);
3357 put_unaligned(650, (u16 *)&caps[8]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003358 }
3359
Borislav Petkovb6422012008-02-02 19:56:49 +01003360 memcpy(&tape->caps, caps, 20);
3361 if (caps[7] & 0x02)
Borislav Petkov54bb2072008-02-06 02:57:52 +01003362 tape->blk_size = 512;
Borislav Petkovb6422012008-02-02 19:56:49 +01003363 else if (caps[7] & 0x04)
Borislav Petkov54bb2072008-02-06 02:57:52 +01003364 tape->blk_size = 1024;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003365}
3366
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +02003367#ifdef CONFIG_IDE_PROC_FS
Linus Torvalds1da177e2005-04-16 15:20:36 -07003368static void idetape_add_settings (ide_drive_t *drive)
3369{
3370 idetape_tape_t *tape = drive->driver_data;
3371
Borislav Petkovb6422012008-02-02 19:56:49 +01003372 ide_add_setting(drive, "buffer", SETTING_READ, TYPE_SHORT, 0, 0xffff,
3373 1, 2, (u16 *)&tape->caps[16], NULL);
Bartlomiej Zolnierkiewicz14979432007-05-10 00:01:10 +02003374 ide_add_setting(drive, "pipeline_min", SETTING_RW, TYPE_INT, 1, 0xffff, tape->stage_size / 1024, 1, &tape->min_pipeline, NULL);
3375 ide_add_setting(drive, "pipeline", SETTING_RW, TYPE_INT, 1, 0xffff, tape->stage_size / 1024, 1, &tape->max_stages, NULL);
3376 ide_add_setting(drive, "pipeline_max", SETTING_RW, TYPE_INT, 1, 0xffff, tape->stage_size / 1024, 1, &tape->max_pipeline, NULL);
3377 ide_add_setting(drive, "pipeline_used", SETTING_READ, TYPE_INT, 0, 0xffff, tape->stage_size / 1024, 1, &tape->nr_stages, NULL);
3378 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 +01003379 ide_add_setting(drive, "speed", SETTING_READ, TYPE_SHORT, 0, 0xffff,
3380 1, 1, (u16 *)&tape->caps[14], NULL);
Borislav Petkov54bb2072008-02-06 02:57:52 +01003381 ide_add_setting(drive, "stage", SETTING_READ, TYPE_INT, 0, 0xffff, 1,
3382 1024, &tape->stage_size, NULL);
3383 ide_add_setting(drive, "tdsc", SETTING_RW, TYPE_INT, IDETAPE_DSC_RW_MIN,
3384 IDETAPE_DSC_RW_MAX, 1000, HZ, &tape->best_dsc_rw_freq,
3385 NULL);
Bartlomiej Zolnierkiewicz14979432007-05-10 00:01:10 +02003386 ide_add_setting(drive, "dsc_overlap", SETTING_RW, TYPE_BYTE, 0, 1, 1, 1, &drive->dsc_overlap, NULL);
3387 ide_add_setting(drive, "pipeline_head_speed_c",SETTING_READ, TYPE_INT, 0, 0xffff, 1, 1, &tape->controlled_pipeline_head_speed, NULL);
3388 ide_add_setting(drive, "pipeline_head_speed_u",SETTING_READ, TYPE_INT, 0, 0xffff, 1, 1, &tape->uncontrolled_pipeline_head_speed,NULL);
3389 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 +01003390 ide_add_setting(drive, "debug_mask", SETTING_RW, TYPE_INT, 0, 0xffff, 1,
3391 1, &tape->debug_mask, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003392}
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +02003393#else
3394static inline void idetape_add_settings(ide_drive_t *drive) { ; }
3395#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003396
3397/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +01003398 * The function below is called to:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003399 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +01003400 * 1. Initialize our various state variables.
3401 * 2. Ask the tape for its capabilities.
3402 * 3. Allocate a buffer which will be used for data transfer. The buffer size
3403 * is chosen based on the recommendation which we received in step 2.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003404 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +01003405 * Note that at this point ide.c already assigned us an irq, so that we can
3406 * queue requests here and wait for their completion.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003407 */
3408static void idetape_setup (ide_drive_t *drive, idetape_tape_t *tape, int minor)
3409{
3410 unsigned long t1, tmid, tn, t;
3411 int speed;
3412 struct idetape_id_gcw gcw;
3413 int stage_size;
3414 struct sysinfo si;
Borislav Petkovb6422012008-02-02 19:56:49 +01003415 u16 *ctl = (u16 *)&tape->caps[12];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003416
Borislav Petkov54bb2072008-02-06 02:57:52 +01003417 spin_lock_init(&tape->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003418 drive->dsc_overlap = 1;
Bartlomiej Zolnierkiewicz4166c192008-02-01 23:09:30 +01003419 if (drive->hwif->host_flags & IDE_HFLAG_NO_DSC) {
3420 printk(KERN_INFO "ide-tape: %s: disabling DSC overlap\n",
3421 tape->name);
3422 drive->dsc_overlap = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003423 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003424 /* Seagate Travan drives do not support DSC overlap. */
3425 if (strstr(drive->id->model, "Seagate STT3401"))
3426 drive->dsc_overlap = 0;
3427 tape->minor = minor;
3428 tape->name[0] = 'h';
3429 tape->name[1] = 't';
3430 tape->name[2] = '0' + minor;
Borislav Petkov54abf372008-02-06 02:57:52 +01003431 tape->chrdev_dir = IDETAPE_DIR_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003432 tape->pc = tape->pc_stack;
3433 tape->max_insert_speed = 10000;
3434 tape->speed_control = 1;
3435 *((unsigned short *) &gcw) = drive->id->config;
3436 if (gcw.drq_type == 1)
3437 set_bit(IDETAPE_DRQ_INTERRUPT, &tape->flags);
3438
3439 tape->min_pipeline = tape->max_pipeline = tape->max_stages = 10;
Borislav Petkov3c98bf32008-02-06 02:57:53 +01003440
Linus Torvalds1da177e2005-04-16 15:20:36 -07003441 idetape_get_inquiry_results(drive);
3442 idetape_get_mode_sense_results(drive);
Borislav Petkov3cffb9c2008-02-02 19:56:50 +01003443 ide_tape_get_bsize_from_bdesc(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003444 tape->user_bs_factor = 1;
Borislav Petkov54bb2072008-02-06 02:57:52 +01003445 tape->stage_size = *ctl * tape->blk_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003446 while (tape->stage_size > 0xffff) {
3447 printk(KERN_NOTICE "ide-tape: decreasing stage size\n");
Borislav Petkovb6422012008-02-02 19:56:49 +01003448 *ctl /= 2;
Borislav Petkov54bb2072008-02-06 02:57:52 +01003449 tape->stage_size = *ctl * tape->blk_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003450 }
3451 stage_size = tape->stage_size;
3452 tape->pages_per_stage = stage_size / PAGE_SIZE;
3453 if (stage_size % PAGE_SIZE) {
3454 tape->pages_per_stage++;
3455 tape->excess_bh_size = PAGE_SIZE - stage_size % PAGE_SIZE;
3456 }
3457
Borislav Petkovb6422012008-02-02 19:56:49 +01003458 /* Select the "best" DSC read/write polling freq and pipeline size. */
3459 speed = max(*(u16 *)&tape->caps[14], *(u16 *)&tape->caps[8]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003460
3461 tape->max_stages = speed * 1000 * 10 / tape->stage_size;
3462
Borislav Petkov3c98bf32008-02-06 02:57:53 +01003463 /* Limit memory use for pipeline to 10% of physical memory */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003464 si_meminfo(&si);
3465 if (tape->max_stages * tape->stage_size > si.totalram * si.mem_unit / 10)
3466 tape->max_stages = si.totalram * si.mem_unit / (10 * tape->stage_size);
3467 tape->max_stages = min(tape->max_stages, IDETAPE_MAX_PIPELINE_STAGES);
3468 tape->min_pipeline = min(tape->max_stages, IDETAPE_MIN_PIPELINE_STAGES);
3469 tape->max_pipeline = min(tape->max_stages * 2, IDETAPE_MAX_PIPELINE_STAGES);
3470 if (tape->max_stages == 0)
3471 tape->max_stages = tape->min_pipeline = tape->max_pipeline = 1;
3472
3473 t1 = (tape->stage_size * HZ) / (speed * 1000);
Borislav Petkovb6422012008-02-02 19:56:49 +01003474 tmid = (*(u16 *)&tape->caps[16] * 32 * HZ) / (speed * 125);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003475 tn = (IDETAPE_FIFO_THRESHOLD * tape->stage_size * HZ) / (speed * 1000);
3476
3477 if (tape->max_stages)
3478 t = tn;
3479 else
3480 t = t1;
3481
3482 /*
Borislav Petkov3c98bf32008-02-06 02:57:53 +01003483 * Ensure that the number we got makes sense; limit it within
3484 * IDETAPE_DSC_RW_MIN and IDETAPE_DSC_RW_MAX.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003485 */
Borislav Petkov54bb2072008-02-06 02:57:52 +01003486 tape->best_dsc_rw_freq = max_t(unsigned long,
3487 min_t(unsigned long, t, IDETAPE_DSC_RW_MAX),
3488 IDETAPE_DSC_RW_MIN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003489 printk(KERN_INFO "ide-tape: %s <-> %s: %dKBps, %d*%dkB buffer, "
3490 "%dkB pipeline, %lums tDSC%s\n",
Borislav Petkovb6422012008-02-02 19:56:49 +01003491 drive->name, tape->name, *(u16 *)&tape->caps[14],
3492 (*(u16 *)&tape->caps[16] * 512) / tape->stage_size,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003493 tape->stage_size / 1024,
3494 tape->max_stages * tape->stage_size / 1024,
Borislav Petkov54bb2072008-02-06 02:57:52 +01003495 tape->best_dsc_rw_freq * 1000 / HZ,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003496 drive->using_dma ? ", DMA":"");
3497
3498 idetape_add_settings(drive);
3499}
3500
Russell King4031bbe2006-01-06 11:41:00 +00003501static void ide_tape_remove(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003502{
3503 idetape_tape_t *tape = drive->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003504
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +02003505 ide_proc_unregister_driver(drive, tape->driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003506
3507 ide_unregister_region(tape->disk);
3508
3509 ide_tape_put(tape);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003510}
3511
3512static void ide_tape_release(struct kref *kref)
3513{
3514 struct ide_tape_obj *tape = to_ide_tape(kref);
3515 ide_drive_t *drive = tape->drive;
3516 struct gendisk *g = tape->disk;
3517
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02003518 BUG_ON(tape->first_stage != NULL || tape->merge_stage_size);
3519
Linus Torvalds1da177e2005-04-16 15:20:36 -07003520 drive->dsc_overlap = 0;
3521 drive->driver_data = NULL;
Tony Jonesdbc12722007-09-25 02:03:03 +02003522 device_destroy(idetape_sysfs_class, MKDEV(IDETAPE_MAJOR, tape->minor));
3523 device_destroy(idetape_sysfs_class, MKDEV(IDETAPE_MAJOR, tape->minor + 128));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003524 idetape_devs[tape->minor] = NULL;
3525 g->private_data = NULL;
3526 put_disk(g);
3527 kfree(tape);
3528}
3529
Bartlomiej Zolnierkiewiczecfd80e2007-05-10 00:01:09 +02003530#ifdef CONFIG_IDE_PROC_FS
Linus Torvalds1da177e2005-04-16 15:20:36 -07003531static int proc_idetape_read_name
3532 (char *page, char **start, off_t off, int count, int *eof, void *data)
3533{
3534 ide_drive_t *drive = (ide_drive_t *) data;
3535 idetape_tape_t *tape = drive->driver_data;
3536 char *out = page;
3537 int len;
3538
3539 len = sprintf(out, "%s\n", tape->name);
3540 PROC_IDE_READ_RETURN(page, start, off, count, eof, len);
3541}
3542
3543static ide_proc_entry_t idetape_proc[] = {
3544 { "capacity", S_IFREG|S_IRUGO, proc_ide_read_capacity, NULL },
3545 { "name", S_IFREG|S_IRUGO, proc_idetape_read_name, NULL },
3546 { NULL, 0, NULL, NULL }
3547};
Linus Torvalds1da177e2005-04-16 15:20:36 -07003548#endif
3549
Russell King4031bbe2006-01-06 11:41:00 +00003550static int ide_tape_probe(ide_drive_t *);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003551
Linus Torvalds1da177e2005-04-16 15:20:36 -07003552static ide_driver_t idetape_driver = {
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02003553 .gen_driver = {
Laurent Riffard4ef3b8f2005-11-18 22:15:40 +01003554 .owner = THIS_MODULE,
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02003555 .name = "ide-tape",
3556 .bus = &ide_bus_type,
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02003557 },
Russell King4031bbe2006-01-06 11:41:00 +00003558 .probe = ide_tape_probe,
3559 .remove = ide_tape_remove,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003560 .version = IDETAPE_VERSION,
3561 .media = ide_tape,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003562 .supports_dsc_overlap = 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003563 .do_request = idetape_do_request,
3564 .end_request = idetape_end_request,
3565 .error = __ide_error,
3566 .abort = __ide_abort,
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +02003567#ifdef CONFIG_IDE_PROC_FS
Linus Torvalds1da177e2005-04-16 15:20:36 -07003568 .proc = idetape_proc,
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +02003569#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003570};
3571
Borislav Petkov3c98bf32008-02-06 02:57:53 +01003572/* Our character device supporting functions, passed to register_chrdev. */
Arjan van de Ven2b8693c2007-02-12 00:55:32 -08003573static const struct file_operations idetape_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003574 .owner = THIS_MODULE,
3575 .read = idetape_chrdev_read,
3576 .write = idetape_chrdev_write,
3577 .ioctl = idetape_chrdev_ioctl,
3578 .open = idetape_chrdev_open,
3579 .release = idetape_chrdev_release,
3580};
3581
3582static int idetape_open(struct inode *inode, struct file *filp)
3583{
3584 struct gendisk *disk = inode->i_bdev->bd_disk;
3585 struct ide_tape_obj *tape;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003586
3587 if (!(tape = ide_tape_get(disk)))
3588 return -ENXIO;
3589
Linus Torvalds1da177e2005-04-16 15:20:36 -07003590 return 0;
3591}
3592
3593static int idetape_release(struct inode *inode, struct file *filp)
3594{
3595 struct gendisk *disk = inode->i_bdev->bd_disk;
3596 struct ide_tape_obj *tape = ide_tape_g(disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003597
3598 ide_tape_put(tape);
3599
3600 return 0;
3601}
3602
3603static int idetape_ioctl(struct inode *inode, struct file *file,
3604 unsigned int cmd, unsigned long arg)
3605{
3606 struct block_device *bdev = inode->i_bdev;
3607 struct ide_tape_obj *tape = ide_tape_g(bdev->bd_disk);
3608 ide_drive_t *drive = tape->drive;
3609 int err = generic_ide_ioctl(drive, file, bdev, cmd, arg);
3610 if (err == -EINVAL)
3611 err = idetape_blkdev_ioctl(drive, cmd, arg);
3612 return err;
3613}
3614
3615static struct block_device_operations idetape_block_ops = {
3616 .owner = THIS_MODULE,
3617 .open = idetape_open,
3618 .release = idetape_release,
3619 .ioctl = idetape_ioctl,
3620};
3621
Russell King4031bbe2006-01-06 11:41:00 +00003622static int ide_tape_probe(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003623{
3624 idetape_tape_t *tape;
3625 struct gendisk *g;
3626 int minor;
3627
3628 if (!strstr("ide-tape", drive->driver_req))
3629 goto failed;
3630 if (!drive->present)
3631 goto failed;
3632 if (drive->media != ide_tape)
3633 goto failed;
3634 if (!idetape_identify_device (drive)) {
3635 printk(KERN_ERR "ide-tape: %s: not supported by this version of ide-tape\n", drive->name);
3636 goto failed;
3637 }
3638 if (drive->scsi) {
3639 printk("ide-tape: passing drive %s to ide-scsi emulation.\n", drive->name);
3640 goto failed;
3641 }
3642 if (strstr(drive->id->model, "OnStream DI-")) {
3643 printk(KERN_WARNING "ide-tape: Use drive %s with ide-scsi emulation and osst.\n", drive->name);
3644 printk(KERN_WARNING "ide-tape: OnStream support will be removed soon from ide-tape!\n");
3645 }
Robert P. J. Day5cbded52006-12-13 00:35:56 -08003646 tape = kzalloc(sizeof (idetape_tape_t), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003647 if (tape == NULL) {
3648 printk(KERN_ERR "ide-tape: %s: Can't allocate a tape structure\n", drive->name);
3649 goto failed;
3650 }
3651
3652 g = alloc_disk(1 << PARTN_BITS);
3653 if (!g)
3654 goto out_free_tape;
3655
3656 ide_init_disk(g, drive);
3657
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +02003658 ide_proc_register_driver(drive, &idetape_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003659
Linus Torvalds1da177e2005-04-16 15:20:36 -07003660 kref_init(&tape->kref);
3661
3662 tape->drive = drive;
3663 tape->driver = &idetape_driver;
3664 tape->disk = g;
3665
3666 g->private_data = &tape->driver;
3667
3668 drive->driver_data = tape;
3669
Arjan van de Vencf8b8972006-03-23 03:00:45 -08003670 mutex_lock(&idetape_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003671 for (minor = 0; idetape_devs[minor]; minor++)
3672 ;
3673 idetape_devs[minor] = tape;
Arjan van de Vencf8b8972006-03-23 03:00:45 -08003674 mutex_unlock(&idetape_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003675
3676 idetape_setup(drive, tape, minor);
3677
Tony Jonesdbc12722007-09-25 02:03:03 +02003678 device_create(idetape_sysfs_class, &drive->gendev,
3679 MKDEV(IDETAPE_MAJOR, minor), "%s", tape->name);
3680 device_create(idetape_sysfs_class, &drive->gendev,
3681 MKDEV(IDETAPE_MAJOR, minor + 128), "n%s", tape->name);
Will Dysond5dee802005-09-16 02:55:07 -07003682
Linus Torvalds1da177e2005-04-16 15:20:36 -07003683 g->fops = &idetape_block_ops;
3684 ide_register_region(g);
3685
3686 return 0;
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02003687
Linus Torvalds1da177e2005-04-16 15:20:36 -07003688out_free_tape:
3689 kfree(tape);
3690failed:
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02003691 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003692}
3693
3694MODULE_DESCRIPTION("ATAPI Streaming TAPE Driver");
3695MODULE_LICENSE("GPL");
3696
3697static void __exit idetape_exit (void)
3698{
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02003699 driver_unregister(&idetape_driver.gen_driver);
Will Dysond5dee802005-09-16 02:55:07 -07003700 class_destroy(idetape_sysfs_class);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003701 unregister_chrdev(IDETAPE_MAJOR, "ht");
3702}
3703
Bartlomiej Zolnierkiewicz17514e82005-11-19 22:24:35 +01003704static int __init idetape_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003705{
Will Dysond5dee802005-09-16 02:55:07 -07003706 int error = 1;
3707 idetape_sysfs_class = class_create(THIS_MODULE, "ide_tape");
3708 if (IS_ERR(idetape_sysfs_class)) {
3709 idetape_sysfs_class = NULL;
3710 printk(KERN_ERR "Unable to create sysfs class for ide tapes\n");
3711 error = -EBUSY;
3712 goto out;
3713 }
3714
Linus Torvalds1da177e2005-04-16 15:20:36 -07003715 if (register_chrdev(IDETAPE_MAJOR, "ht", &idetape_fops)) {
3716 printk(KERN_ERR "ide-tape: Failed to register character device interface\n");
Will Dysond5dee802005-09-16 02:55:07 -07003717 error = -EBUSY;
3718 goto out_free_class;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003719 }
Will Dysond5dee802005-09-16 02:55:07 -07003720
3721 error = driver_register(&idetape_driver.gen_driver);
3722 if (error)
3723 goto out_free_driver;
3724
3725 return 0;
3726
3727out_free_driver:
3728 driver_unregister(&idetape_driver.gen_driver);
3729out_free_class:
3730 class_destroy(idetape_sysfs_class);
3731out:
3732 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003733}
3734
Kay Sievers263756e2005-12-12 18:03:44 +01003735MODULE_ALIAS("ide:*m-tape*");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003736module_init(idetape_init);
3737module_exit(idetape_exit);
3738MODULE_ALIAS_CHARDEV_MAJOR(IDETAPE_MAJOR);