blob: d907abaed9091a56e25bd96c186afe6888b5bb6d [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Borislav Petkov5ce78af2008-02-02 19:56:48 +01002 * IDE ATAPI streaming tape driver.
3 *
Bartlomiej Zolnierkiewicz59bca8c2008-02-01 23:09:33 +01004 * Copyright (C) 1995-1999 Gadi Oxman <gadio@netvision.net.il>
5 * Copyright (C) 2003-2005 Bartlomiej Zolnierkiewicz
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 * This driver was constructed as a student project in the software laboratory
8 * of the faculty of electrical engineering in the Technion - Israel's
9 * Institute Of Technology, with the guide of Avner Lottem and Dr. Ilana David.
10 *
11 * It is hereby placed under the terms of the GNU general public license.
12 * (See linux/COPYING).
Linus Torvalds1da177e2005-04-16 15:20:36 -070013 *
Borislav Petkov5ce78af2008-02-02 19:56:48 +010014 * For a historical changelog see
15 * Documentation/ide/ChangeLog.ide-tape.1995-2002
Linus Torvalds1da177e2005-04-16 15:20:36 -070016 */
17
Borislav Petkovdfe79932008-02-06 02:57:55 +010018#define IDETAPE_VERSION "1.20"
Linus Torvalds1da177e2005-04-16 15:20:36 -070019
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include <linux/module.h>
21#include <linux/types.h>
22#include <linux/string.h>
23#include <linux/kernel.h>
24#include <linux/delay.h>
25#include <linux/timer.h>
26#include <linux/mm.h>
27#include <linux/interrupt.h>
Marcelo Feitoza Parisi9bae1ff2006-03-28 01:56:46 -080028#include <linux/jiffies.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#include <linux/major.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include <linux/errno.h>
31#include <linux/genhd.h>
32#include <linux/slab.h>
33#include <linux/pci.h>
34#include <linux/ide.h>
35#include <linux/smp_lock.h>
36#include <linux/completion.h>
37#include <linux/bitops.h>
Arjan van de Vencf8b8972006-03-23 03:00:45 -080038#include <linux/mutex.h>
Borislav Petkov90699ce2008-02-02 19:56:50 +010039#include <scsi/scsi.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070040
41#include <asm/byteorder.h>
Borislav Petkovc837cfa2008-02-06 02:57:54 +010042#include <linux/irq.h>
43#include <linux/uaccess.h>
44#include <linux/io.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070045#include <asm/unaligned.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070046#include <linux/mtio.h>
47
Borislav Petkov8004a8c2008-02-06 02:57:51 +010048enum {
49 /* output errors only */
50 DBG_ERR = (1 << 0),
51 /* output all sense key/asc */
52 DBG_SENSE = (1 << 1),
53 /* info regarding all chrdev-related procedures */
54 DBG_CHRDEV = (1 << 2),
55 /* all remaining procedures */
56 DBG_PROCS = (1 << 3),
57 /* buffer alloc info (pc_stack & rq_stack) */
58 DBG_PCRQ_STACK = (1 << 4),
59};
60
61/* define to see debug info */
62#define IDETAPE_DEBUG_LOG 0
63
64#if IDETAPE_DEBUG_LOG
65#define debug_log(lvl, fmt, args...) \
66{ \
67 if (tape->debug_mask & lvl) \
68 printk(KERN_INFO "ide-tape: " fmt, ## args); \
69}
70#else
71#define debug_log(lvl, fmt, args...) do {} while (0)
72#endif
73
Linus Torvalds1da177e2005-04-16 15:20:36 -070074/**************************** Tunable parameters *****************************/
75
76
77/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +010078 * Pipelined mode parameters.
Linus Torvalds1da177e2005-04-16 15:20:36 -070079 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +010080 * We try to use the minimum number of stages which is enough to keep the tape
81 * constantly streaming. To accomplish that, we implement a feedback loop around
82 * the maximum number of stages:
Linus Torvalds1da177e2005-04-16 15:20:36 -070083 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +010084 * We start from MIN maximum stages (we will not even use MIN stages if we don't
85 * need them), increment it by RATE*(MAX-MIN) whenever we sense that the
86 * pipeline is empty, until we reach the optimum value or until we reach MAX.
Linus Torvalds1da177e2005-04-16 15:20:36 -070087 */
88#define IDETAPE_MIN_PIPELINE_STAGES 1
89#define IDETAPE_MAX_PIPELINE_STAGES 400
90#define IDETAPE_INCREASE_STAGES_RATE 20
91
92/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +010093 * After each failed packet command we issue a request sense command and retry
94 * the packet command IDETAPE_MAX_PC_RETRIES times.
Linus Torvalds1da177e2005-04-16 15:20:36 -070095 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +010096 * Setting IDETAPE_MAX_PC_RETRIES to 0 will disable retries.
Linus Torvalds1da177e2005-04-16 15:20:36 -070097 */
98#define IDETAPE_MAX_PC_RETRIES 3
99
100/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100101 * With each packet command, we allocate a buffer of IDETAPE_PC_BUFFER_SIZE
102 * bytes. This is used for several packet commands (Not for READ/WRITE commands)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 */
104#define IDETAPE_PC_BUFFER_SIZE 256
105
106/*
107 * In various places in the driver, we need to allocate storage
108 * for packet commands and requests, which will remain valid while
109 * we leave the driver to wait for an interrupt or a timeout event.
110 */
111#define IDETAPE_PC_STACK (10 + IDETAPE_MAX_PC_RETRIES)
112
113/*
114 * Some drives (for example, Seagate STT3401A Travan) require a very long
115 * timeout, because they don't return an interrupt or clear their busy bit
116 * until after the command completes (even retension commands).
117 */
118#define IDETAPE_WAIT_CMD (900*HZ)
119
120/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100121 * The following parameter is used to select the point in the internal tape fifo
122 * in which we will start to refill the buffer. Decreasing the following
123 * parameter will improve the system's latency and interactive response, while
124 * using a high value might improve system throughput.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 */
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100126#define IDETAPE_FIFO_THRESHOLD 2
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127
128/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100129 * DSC polling parameters.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100131 * Polling for DSC (a single bit in the status register) is a very important
132 * function in ide-tape. There are two cases in which we poll for DSC:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100134 * 1. Before a read/write packet command, to ensure that we can transfer data
135 * from/to the tape's data buffers, without causing an actual media access.
136 * In case the tape is not ready yet, we take out our request from the device
137 * request queue, so that ide.c could service requests from the other device
138 * on the same interface in the meantime.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100140 * 2. After the successful initialization of a "media access packet command",
141 * which is a command that can take a long time to complete (the interval can
142 * range from several seconds to even an hour). Again, we postpone our request
143 * in the middle to free the bus for the other device. The polling frequency
144 * here should be lower than the read/write frequency since those media access
145 * commands are slow. We start from a "fast" frequency - IDETAPE_DSC_MA_FAST
146 * (1 second), and if we don't receive DSC after IDETAPE_DSC_MA_THRESHOLD
147 * (5 min), we switch it to a lower frequency - IDETAPE_DSC_MA_SLOW (1 min).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100149 * We also set a timeout for the timer, in case something goes wrong. The
150 * timeout should be longer then the maximum execution time of a tape operation.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 */
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100152
153/* DSC timings. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154#define IDETAPE_DSC_RW_MIN 5*HZ/100 /* 50 msec */
155#define IDETAPE_DSC_RW_MAX 40*HZ/100 /* 400 msec */
156#define IDETAPE_DSC_RW_TIMEOUT 2*60*HZ /* 2 minutes */
157#define IDETAPE_DSC_MA_FAST 2*HZ /* 2 seconds */
158#define IDETAPE_DSC_MA_THRESHOLD 5*60*HZ /* 5 minutes */
159#define IDETAPE_DSC_MA_SLOW 30*HZ /* 30 seconds */
160#define IDETAPE_DSC_MA_TIMEOUT 2*60*60*HZ /* 2 hours */
161
162/*************************** End of tunable parameters ***********************/
163
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100164/* Read/Write error simulation */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165#define SIMULATE_ERRORS 0
166
Borislav Petkov54abf372008-02-06 02:57:52 +0100167/* tape directions */
168enum {
169 IDETAPE_DIR_NONE = (1 << 0),
170 IDETAPE_DIR_READ = (1 << 1),
171 IDETAPE_DIR_WRITE = (1 << 2),
172};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173
174struct idetape_bh {
Stephen Rothwellab057962007-08-01 23:46:44 +0200175 u32 b_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 atomic_t b_count;
177 struct idetape_bh *b_reqnext;
178 char *b_data;
179};
180
Borislav Petkov03056b92008-04-18 00:46:26 +0200181/* Tape door status */
182#define DOOR_UNLOCKED 0
183#define DOOR_LOCKED 1
184#define DOOR_EXPLICITLY_LOCKED 2
185
186/* Some defines for the SPACE command */
187#define IDETAPE_SPACE_OVER_FILEMARK 1
188#define IDETAPE_SPACE_TO_EOD 3
189
190/* Some defines for the LOAD UNLOAD command */
191#define IDETAPE_LU_LOAD_MASK 1
192#define IDETAPE_LU_RETENSION_MASK 2
193#define IDETAPE_LU_EOT_MASK 4
194
195/*
196 * Special requests for our block device strategy routine.
197 *
198 * In order to service a character device command, we add special requests to
199 * the tail of our block device request queue and wait for their completion.
200 */
201
202enum {
203 REQ_IDETAPE_PC1 = (1 << 0), /* packet command (first stage) */
204 REQ_IDETAPE_PC2 = (1 << 1), /* packet command (second stage) */
205 REQ_IDETAPE_READ = (1 << 2),
206 REQ_IDETAPE_WRITE = (1 << 3),
207};
208
209/* Error codes returned in rq->errors to the higher part of the driver. */
210#define IDETAPE_ERROR_GENERAL 101
211#define IDETAPE_ERROR_FILEMARK 102
212#define IDETAPE_ERROR_EOD 103
213
214/* Structures related to the SELECT SENSE / MODE SENSE packet commands. */
215#define IDETAPE_BLOCK_DESCRIPTOR 0
216#define IDETAPE_CAPABILITIES_PAGE 0x2a
217
Borislav Petkov346331f2008-04-18 00:46:26 +0200218/* Tape flag bits values. */
219enum {
220 IDETAPE_FLAG_IGNORE_DSC = (1 << 0),
221 /* 0 When the tape position is unknown */
222 IDETAPE_FLAG_ADDRESS_VALID = (1 << 1),
223 /* Device already opened */
224 IDETAPE_FLAG_BUSY = (1 << 2),
225 /* Error detected in a pipeline stage */
226 IDETAPE_FLAG_PIPELINE_ERR = (1 << 3),
227 /* Attempt to auto-detect the current user block size */
228 IDETAPE_FLAG_DETECT_BS = (1 << 4),
229 /* Currently on a filemark */
230 IDETAPE_FLAG_FILEMARK = (1 << 5),
231 /* DRQ interrupt device */
232 IDETAPE_FLAG_DRQ_INTERRUPT = (1 << 6),
233 /* pipeline active */
234 IDETAPE_FLAG_PIPELINE_ACTIVE = (1 << 7),
235 /* 0 = no tape is loaded, so we don't rewind after ejecting */
236 IDETAPE_FLAG_MEDIUM_PRESENT = (1 << 8),
237};
238
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100239/* A pipeline stage. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240typedef struct idetape_stage_s {
241 struct request rq; /* The corresponding request */
242 struct idetape_bh *bh; /* The data buffers */
243 struct idetape_stage_s *next; /* Pointer to the next stage */
244} idetape_stage_t;
245
246/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100247 * Most of our global data which we need to save even as we leave the driver due
248 * to an interrupt or a timer event is stored in the struct defined below.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 */
250typedef struct ide_tape_obj {
251 ide_drive_t *drive;
252 ide_driver_t *driver;
253 struct gendisk *disk;
254 struct kref kref;
255
256 /*
257 * Since a typical character device operation requires more
258 * than one packet command, we provide here enough memory
259 * for the maximum of interconnected packet commands.
260 * The packet commands are stored in the circular array pc_stack.
261 * pc_stack_index points to the last used entry, and warps around
262 * to the start when we get to the last array entry.
263 *
264 * pc points to the current processed packet command.
265 *
266 * failed_pc points to the last failed packet command, or contains
267 * NULL if we do not need to retry any packet command. This is
268 * required since an additional packet command is needed before the
269 * retry, to get detailed information on what went wrong.
270 */
271 /* Current packet command */
Borislav Petkovd236d742008-04-18 00:46:27 +0200272 struct ide_atapi_pc *pc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 /* Last failed packet command */
Borislav Petkovd236d742008-04-18 00:46:27 +0200274 struct ide_atapi_pc *failed_pc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 /* Packet command stack */
Borislav Petkovd236d742008-04-18 00:46:27 +0200276 struct ide_atapi_pc pc_stack[IDETAPE_PC_STACK];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 /* Next free packet command storage space */
278 int pc_stack_index;
279 struct request rq_stack[IDETAPE_PC_STACK];
280 /* We implement a circular array */
281 int rq_stack_index;
282
283 /*
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100284 * DSC polling variables.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100286 * While polling for DSC we use postponed_rq to postpone the current
287 * request so that ide.c will be able to service pending requests on the
288 * other device. Note that at most we will have only one DSC (usually
289 * data transfer) request in the device request queue. Additional
290 * requests can be queued in our internal pipeline, but they will be
291 * visible to ide.c only one at a time.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 */
293 struct request *postponed_rq;
294 /* The time in which we started polling for DSC */
295 unsigned long dsc_polling_start;
296 /* Timer used to poll for dsc */
297 struct timer_list dsc_timer;
298 /* Read/Write dsc polling frequency */
Borislav Petkov54bb2072008-02-06 02:57:52 +0100299 unsigned long best_dsc_rw_freq;
300 unsigned long dsc_poll_freq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 unsigned long dsc_timeout;
302
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100303 /* Read position information */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 u8 partition;
305 /* Current block */
Borislav Petkov54bb2072008-02-06 02:57:52 +0100306 unsigned int first_frame;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100308 /* Last error information */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 u8 sense_key, asc, ascq;
310
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100311 /* Character device operation */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 unsigned int minor;
313 /* device name */
314 char name[4];
315 /* Current character device data transfer direction */
Borislav Petkov54abf372008-02-06 02:57:52 +0100316 u8 chrdev_dir;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317
Borislav Petkov54bb2072008-02-06 02:57:52 +0100318 /* tape block size, usually 512 or 1024 bytes */
319 unsigned short blk_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 int user_bs_factor;
Borislav Petkovb6422012008-02-02 19:56:49 +0100321
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 /* Copy of the tape's Capabilities and Mechanical Page */
Borislav Petkovb6422012008-02-02 19:56:49 +0100323 u8 caps[20];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324
325 /*
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100326 * Active data transfer request parameters.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100328 * At most, there is only one ide-tape originated data transfer request
329 * in the device request queue. This allows ide.c to easily service
330 * requests from the other device when we postpone our active request.
331 * In the pipelined operation mode, we use our internal pipeline
332 * structure to hold more data requests. The data buffer size is chosen
333 * based on the tape's recommendation.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 */
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100335 /* ptr to the request which is waiting in the device request queue */
Borislav Petkov54bb2072008-02-06 02:57:52 +0100336 struct request *active_data_rq;
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100337 /* Data buffer size chosen based on the tape's recommendation */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 int stage_size;
339 idetape_stage_t *merge_stage;
340 int merge_stage_size;
341 struct idetape_bh *bh;
342 char *b_data;
343 int b_count;
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100344
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 /*
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100346 * Pipeline parameters.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100348 * To accomplish non-pipelined mode, we simply set the following
349 * variables to zero (or NULL, where appropriate).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 */
351 /* Number of currently used stages */
352 int nr_stages;
353 /* Number of pending stages */
354 int nr_pending_stages;
355 /* We will not allocate more than this number of stages */
356 int max_stages, min_pipeline, max_pipeline;
357 /* The first stage which will be removed from the pipeline */
358 idetape_stage_t *first_stage;
359 /* The currently active stage */
360 idetape_stage_t *active_stage;
361 /* Will be serviced after the currently active request */
362 idetape_stage_t *next_stage;
363 /* New requests will be added to the pipeline here */
364 idetape_stage_t *last_stage;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 int pages_per_stage;
366 /* Wasted space in each stage */
367 int excess_bh_size;
368
369 /* Status/Action flags: long for set_bit */
370 unsigned long flags;
371 /* protects the ide-tape queue */
Borislav Petkov54bb2072008-02-06 02:57:52 +0100372 spinlock_t lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100374 /* Measures average tape speed */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 unsigned long avg_time;
376 int avg_size;
377 int avg_speed;
378
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 /* the door is currently locked */
380 int door_locked;
381 /* the tape hardware is write protected */
382 char drv_write_prot;
383 /* the tape is write protected (hardware or opened as read-only) */
384 char write_prot;
385
386 /*
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100387 * Limit the number of times a request can be postponed, to avoid an
388 * infinite postpone deadlock.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 int postpone_cnt;
391
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100392 /* Speed control at the tape buffers input/output */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 unsigned long insert_time;
394 int insert_size;
395 int insert_speed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 int measure_insert_time;
397
Borislav Petkov8004a8c2008-02-06 02:57:51 +0100398 u32 debug_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399} idetape_tape_t;
400
Arjan van de Vencf8b8972006-03-23 03:00:45 -0800401static DEFINE_MUTEX(idetape_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402
Will Dysond5dee802005-09-16 02:55:07 -0700403static struct class *idetape_sysfs_class;
404
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405#define to_ide_tape(obj) container_of(obj, struct ide_tape_obj, kref)
406
407#define ide_tape_g(disk) \
408 container_of((disk)->private_data, struct ide_tape_obj, driver)
409
410static struct ide_tape_obj *ide_tape_get(struct gendisk *disk)
411{
412 struct ide_tape_obj *tape = NULL;
413
Arjan van de Vencf8b8972006-03-23 03:00:45 -0800414 mutex_lock(&idetape_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 tape = ide_tape_g(disk);
416 if (tape)
417 kref_get(&tape->kref);
Arjan van de Vencf8b8972006-03-23 03:00:45 -0800418 mutex_unlock(&idetape_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 return tape;
420}
421
422static void ide_tape_release(struct kref *);
423
424static void ide_tape_put(struct ide_tape_obj *tape)
425{
Arjan van de Vencf8b8972006-03-23 03:00:45 -0800426 mutex_lock(&idetape_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 kref_put(&tape->kref, ide_tape_release);
Arjan van de Vencf8b8972006-03-23 03:00:45 -0800428 mutex_unlock(&idetape_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429}
430
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100432 * The variables below are used for the character device interface. Additional
433 * state variables are defined in our ide_drive_t structure.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100435static struct ide_tape_obj *idetape_devs[MAX_HWIFS * MAX_DRIVES];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436
437#define ide_tape_f(file) ((file)->private_data)
438
439static struct ide_tape_obj *ide_tape_chrdev_get(unsigned int i)
440{
441 struct ide_tape_obj *tape = NULL;
442
Arjan van de Vencf8b8972006-03-23 03:00:45 -0800443 mutex_lock(&idetape_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 tape = idetape_devs[i];
445 if (tape)
446 kref_get(&tape->kref);
Arjan van de Vencf8b8972006-03-23 03:00:45 -0800447 mutex_unlock(&idetape_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 return tape;
449}
450
Borislav Petkovd236d742008-04-18 00:46:27 +0200451static void idetape_input_buffers(ide_drive_t *drive, struct ide_atapi_pc *pc,
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100452 unsigned int bcount)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453{
454 struct idetape_bh *bh = pc->bh;
455 int count;
456
457 while (bcount) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 if (bh == NULL) {
459 printk(KERN_ERR "ide-tape: bh == NULL in "
460 "idetape_input_buffers\n");
Bartlomiej Zolnierkiewicz7616c0a2008-04-18 00:46:26 +0200461 ide_atapi_discard_data(drive, bcount);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 return;
463 }
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100464 count = min(
465 (unsigned int)(bh->b_size - atomic_read(&bh->b_count)),
466 bcount);
467 HWIF(drive)->atapi_input_bytes(drive, bh->b_data +
468 atomic_read(&bh->b_count), count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 bcount -= count;
470 atomic_add(count, &bh->b_count);
471 if (atomic_read(&bh->b_count) == bh->b_size) {
472 bh = bh->b_reqnext;
473 if (bh)
474 atomic_set(&bh->b_count, 0);
475 }
476 }
477 pc->bh = bh;
478}
479
Borislav Petkovd236d742008-04-18 00:46:27 +0200480static void idetape_output_buffers(ide_drive_t *drive, struct ide_atapi_pc *pc,
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100481 unsigned int bcount)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482{
483 struct idetape_bh *bh = pc->bh;
484 int count;
485
486 while (bcount) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487 if (bh == NULL) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100488 printk(KERN_ERR "ide-tape: bh == NULL in %s\n",
489 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 return;
491 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 count = min((unsigned int)pc->b_count, (unsigned int)bcount);
493 HWIF(drive)->atapi_output_bytes(drive, pc->b_data, count);
494 bcount -= count;
495 pc->b_data += count;
496 pc->b_count -= count;
497 if (!pc->b_count) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100498 bh = bh->b_reqnext;
499 pc->bh = bh;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 if (bh) {
501 pc->b_data = bh->b_data;
502 pc->b_count = atomic_read(&bh->b_count);
503 }
504 }
505 }
506}
507
Borislav Petkovd236d742008-04-18 00:46:27 +0200508static void idetape_update_buffers(struct ide_atapi_pc *pc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509{
510 struct idetape_bh *bh = pc->bh;
511 int count;
Borislav Petkovd236d742008-04-18 00:46:27 +0200512 unsigned int bcount = pc->xferred;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513
Borislav Petkov346331f2008-04-18 00:46:26 +0200514 if (pc->flags & PC_FLAG_WRITING)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 return;
516 while (bcount) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 if (bh == NULL) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100518 printk(KERN_ERR "ide-tape: bh == NULL in %s\n",
519 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 return;
521 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 count = min((unsigned int)bh->b_size, (unsigned int)bcount);
523 atomic_set(&bh->b_count, count);
524 if (atomic_read(&bh->b_count) == bh->b_size)
525 bh = bh->b_reqnext;
526 bcount -= count;
527 }
528 pc->bh = bh;
529}
530
531/*
532 * idetape_next_pc_storage returns a pointer to a place in which we can
533 * safely store a packet command, even though we intend to leave the
534 * driver. A storage space for a maximum of IDETAPE_PC_STACK packet
535 * commands is allocated at initialization time.
536 */
Borislav Petkovd236d742008-04-18 00:46:27 +0200537static struct ide_atapi_pc *idetape_next_pc_storage(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538{
539 idetape_tape_t *tape = drive->driver_data;
540
Borislav Petkov8004a8c2008-02-06 02:57:51 +0100541 debug_log(DBG_PCRQ_STACK, "pc_stack_index=%d\n", tape->pc_stack_index);
542
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 if (tape->pc_stack_index == IDETAPE_PC_STACK)
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100544 tape->pc_stack_index = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545 return (&tape->pc_stack[tape->pc_stack_index++]);
546}
547
548/*
549 * idetape_next_rq_storage is used along with idetape_next_pc_storage.
550 * Since we queue packet commands in the request queue, we need to
551 * allocate a request, along with the allocation of a packet command.
552 */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100553
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554/**************************************************************
555 * *
556 * This should get fixed to use kmalloc(.., GFP_ATOMIC) *
557 * followed later on by kfree(). -ml *
558 * *
559 **************************************************************/
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100560
561static struct request *idetape_next_rq_storage(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562{
563 idetape_tape_t *tape = drive->driver_data;
564
Borislav Petkov8004a8c2008-02-06 02:57:51 +0100565 debug_log(DBG_PCRQ_STACK, "rq_stack_index=%d\n", tape->rq_stack_index);
566
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 if (tape->rq_stack_index == IDETAPE_PC_STACK)
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100568 tape->rq_stack_index = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 return (&tape->rq_stack[tape->rq_stack_index++]);
570}
571
Borislav Petkovd236d742008-04-18 00:46:27 +0200572static void idetape_init_pc(struct ide_atapi_pc *pc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573{
574 memset(pc->c, 0, 12);
575 pc->retries = 0;
576 pc->flags = 0;
Borislav Petkovd236d742008-04-18 00:46:27 +0200577 pc->req_xfer = 0;
578 pc->buf = pc->pc_buf;
579 pc->buf_size = IDETAPE_PC_BUFFER_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 pc->bh = NULL;
581 pc->b_data = NULL;
582}
583
584/*
Borislav Petkov1b5db432008-02-02 19:56:48 +0100585 * called on each failed packet command retry to analyze the request sense. We
586 * currently do not utilize this information.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 */
Borislav Petkov1b5db432008-02-02 19:56:48 +0100588static void idetape_analyze_error(ide_drive_t *drive, u8 *sense)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589{
590 idetape_tape_t *tape = drive->driver_data;
Borislav Petkovd236d742008-04-18 00:46:27 +0200591 struct ide_atapi_pc *pc = tape->failed_pc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592
Borislav Petkov1b5db432008-02-02 19:56:48 +0100593 tape->sense_key = sense[2] & 0xF;
594 tape->asc = sense[12];
595 tape->ascq = sense[13];
Borislav Petkov8004a8c2008-02-06 02:57:51 +0100596
597 debug_log(DBG_ERR, "pc = %x, sense key = %x, asc = %x, ascq = %x\n",
598 pc->c[0], tape->sense_key, tape->asc, tape->ascq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599
Borislav Petkovd236d742008-04-18 00:46:27 +0200600 /* Correct pc->xferred by asking the tape. */
Borislav Petkov346331f2008-04-18 00:46:26 +0200601 if (pc->flags & PC_FLAG_DMA_ERROR) {
Borislav Petkovd236d742008-04-18 00:46:27 +0200602 pc->xferred = pc->req_xfer -
Borislav Petkov54bb2072008-02-06 02:57:52 +0100603 tape->blk_size *
Borislav Petkov860ff5e2008-02-02 19:56:50 +0100604 be32_to_cpu(get_unaligned((u32 *)&sense[3]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 idetape_update_buffers(pc);
606 }
607
608 /*
609 * If error was the result of a zero-length read or write command,
610 * with sense key=5, asc=0x22, ascq=0, let it slide. Some drives
611 * (i.e. Seagate STT3401A Travan) don't support 0-length read/writes.
612 */
Borislav Petkov90699ce2008-02-02 19:56:50 +0100613 if ((pc->c[0] == READ_6 || pc->c[0] == WRITE_6)
Borislav Petkov1b5db432008-02-02 19:56:48 +0100614 /* length == 0 */
615 && pc->c[4] == 0 && pc->c[3] == 0 && pc->c[2] == 0) {
616 if (tape->sense_key == 5) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 /* don't report an error, everything's ok */
618 pc->error = 0;
619 /* don't retry read/write */
Borislav Petkov346331f2008-04-18 00:46:26 +0200620 pc->flags |= PC_FLAG_ABORT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621 }
622 }
Borislav Petkov90699ce2008-02-02 19:56:50 +0100623 if (pc->c[0] == READ_6 && (sense[2] & 0x80)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624 pc->error = IDETAPE_ERROR_FILEMARK;
Borislav Petkov346331f2008-04-18 00:46:26 +0200625 pc->flags |= PC_FLAG_ABORT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626 }
Borislav Petkov90699ce2008-02-02 19:56:50 +0100627 if (pc->c[0] == WRITE_6) {
Borislav Petkov1b5db432008-02-02 19:56:48 +0100628 if ((sense[2] & 0x40) || (tape->sense_key == 0xd
629 && tape->asc == 0x0 && tape->ascq == 0x2)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630 pc->error = IDETAPE_ERROR_EOD;
Borislav Petkov346331f2008-04-18 00:46:26 +0200631 pc->flags |= PC_FLAG_ABORT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 }
633 }
Borislav Petkov90699ce2008-02-02 19:56:50 +0100634 if (pc->c[0] == READ_6 || pc->c[0] == WRITE_6) {
Borislav Petkov1b5db432008-02-02 19:56:48 +0100635 if (tape->sense_key == 8) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636 pc->error = IDETAPE_ERROR_EOD;
Borislav Petkov346331f2008-04-18 00:46:26 +0200637 pc->flags |= PC_FLAG_ABORT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 }
Borislav Petkov346331f2008-04-18 00:46:26 +0200639 if (!(pc->flags & PC_FLAG_ABORT) &&
Borislav Petkovd236d742008-04-18 00:46:27 +0200640 pc->xferred)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 pc->retries = IDETAPE_MAX_PC_RETRIES + 1;
642 }
643}
644
Borislav Petkov419d4742008-02-02 19:56:51 +0100645static void idetape_activate_next_stage(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646{
647 idetape_tape_t *tape = drive->driver_data;
648 idetape_stage_t *stage = tape->next_stage;
649 struct request *rq = &stage->rq;
650
Borislav Petkov8004a8c2008-02-06 02:57:51 +0100651 debug_log(DBG_PROCS, "Enter %s\n", __func__);
652
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 if (stage == NULL) {
Borislav Petkov8004a8c2008-02-06 02:57:51 +0100654 printk(KERN_ERR "ide-tape: bug: Trying to activate a non"
655 " existing stage\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 return;
657 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658
659 rq->rq_disk = tape->disk;
660 rq->buffer = NULL;
661 rq->special = (void *)stage->bh;
Borislav Petkov54bb2072008-02-06 02:57:52 +0100662 tape->active_data_rq = rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 tape->active_stage = stage;
664 tape->next_stage = stage->next;
665}
666
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100667/* Free a stage along with its related buffers completely. */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100668static void __idetape_kfree_stage(idetape_stage_t *stage)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669{
670 struct idetape_bh *prev_bh, *bh = stage->bh;
671 int size;
672
673 while (bh != NULL) {
674 if (bh->b_data != NULL) {
675 size = (int) bh->b_size;
676 while (size > 0) {
677 free_page((unsigned long) bh->b_data);
678 size -= PAGE_SIZE;
679 bh->b_data += PAGE_SIZE;
680 }
681 }
682 prev_bh = bh;
683 bh = bh->b_reqnext;
684 kfree(prev_bh);
685 }
686 kfree(stage);
687}
688
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100690 * Finish servicing a request and insert a pending pipeline request into the
691 * main device queue.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692 */
693static int idetape_end_request(ide_drive_t *drive, int uptodate, int nr_sects)
694{
695 struct request *rq = HWGROUP(drive)->rq;
696 idetape_tape_t *tape = drive->driver_data;
697 unsigned long flags;
698 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699
Borislav Petkov8004a8c2008-02-06 02:57:51 +0100700 debug_log(DBG_PROCS, "Enter %s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701
702 switch (uptodate) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100703 case 0: error = IDETAPE_ERROR_GENERAL; break;
704 case 1: error = 0; break;
705 default: error = uptodate;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 }
707 rq->errors = error;
708 if (error)
709 tape->failed_pc = NULL;
710
Bartlomiej Zolnierkiewicz36872212008-01-26 20:13:10 +0100711 if (!blk_special_request(rq)) {
712 ide_end_request(drive, uptodate, nr_sects);
713 return 0;
714 }
715
Borislav Petkov54bb2072008-02-06 02:57:52 +0100716 spin_lock_irqsave(&tape->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718 ide_end_drive_cmd(drive, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719
Borislav Petkov189bb3b2008-04-27 15:38:27 +0200720 clear_bit(IDETAPE_FLAG_PIPELINE_ACTIVE, &tape->flags);
Borislav Petkov54bb2072008-02-06 02:57:52 +0100721 spin_unlock_irqrestore(&tape->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 return 0;
723}
724
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100725static ide_startstop_t idetape_request_sense_callback(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726{
727 idetape_tape_t *tape = drive->driver_data;
728
Borislav Petkov8004a8c2008-02-06 02:57:51 +0100729 debug_log(DBG_PROCS, "Enter %s\n", __func__);
730
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 if (!tape->pc->error) {
Borislav Petkovd236d742008-04-18 00:46:27 +0200732 idetape_analyze_error(drive, tape->pc->buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 idetape_end_request(drive, 1, 0);
734 } else {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100735 printk(KERN_ERR "ide-tape: Error in REQUEST SENSE itself - "
736 "Aborting request!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737 idetape_end_request(drive, 0, 0);
738 }
739 return ide_stopped;
740}
741
Borislav Petkovd236d742008-04-18 00:46:27 +0200742static void idetape_create_request_sense_cmd(struct ide_atapi_pc *pc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743{
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100744 idetape_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +0100745 pc->c[0] = REQUEST_SENSE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746 pc->c[4] = 20;
Borislav Petkovd236d742008-04-18 00:46:27 +0200747 pc->req_xfer = 20;
748 pc->idetape_callback = &idetape_request_sense_callback;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749}
750
751static void idetape_init_rq(struct request *rq, u8 cmd)
752{
753 memset(rq, 0, sizeof(*rq));
Jens Axboe4aff5e22006-08-10 08:44:47 +0200754 rq->cmd_type = REQ_TYPE_SPECIAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755 rq->cmd[0] = cmd;
756}
757
758/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100759 * Generate a new packet command request in front of the request queue, before
760 * the current request, so that it will be processed immediately, on the next
761 * pass through the driver. The function below is called from the request
762 * handling part of the driver (the "bottom" part). Safe storage for the request
763 * should be allocated with ide_tape_next_{pc,rq}_storage() prior to that.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100765 * Memory for those requests is pre-allocated at initialization time, and is
766 * limited to IDETAPE_PC_STACK requests. We assume that we have enough space for
767 * the maximum possible number of inter-dependent packet commands.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100769 * The higher level of the driver - The ioctl handler and the character device
770 * handling functions should queue request to the lower level part and wait for
771 * their completion using idetape_queue_pc_tail or idetape_queue_rw_tail.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772 */
Borislav Petkovd236d742008-04-18 00:46:27 +0200773static void idetape_queue_pc_head(ide_drive_t *drive, struct ide_atapi_pc *pc,
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100774 struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775{
776 struct ide_tape_obj *tape = drive->driver_data;
777
778 idetape_init_rq(rq, REQ_IDETAPE_PC1);
779 rq->buffer = (char *) pc;
780 rq->rq_disk = tape->disk;
781 (void) ide_do_drive_cmd(drive, rq, ide_preempt);
782}
783
784/*
785 * idetape_retry_pc is called when an error was detected during the
786 * last packet command. We queue a request sense packet command in
787 * the head of the request list.
788 */
789static ide_startstop_t idetape_retry_pc (ide_drive_t *drive)
790{
791 idetape_tape_t *tape = drive->driver_data;
Borislav Petkovd236d742008-04-18 00:46:27 +0200792 struct ide_atapi_pc *pc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793 struct request *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794
Bartlomiej Zolnierkiewicz64a57fe2008-02-06 02:57:51 +0100795 (void)ide_read_error(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796 pc = idetape_next_pc_storage(drive);
797 rq = idetape_next_rq_storage(drive);
798 idetape_create_request_sense_cmd(pc);
Borislav Petkov346331f2008-04-18 00:46:26 +0200799 set_bit(IDETAPE_FLAG_IGNORE_DSC, &tape->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800 idetape_queue_pc_head(drive, pc, rq);
801 return ide_stopped;
802}
803
804/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100805 * Postpone the current request so that ide.c will be able to service requests
806 * from another device on the same hwgroup while we are polling for DSC.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807 */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100808static void idetape_postpone_request(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809{
810 idetape_tape_t *tape = drive->driver_data;
811
Borislav Petkov8004a8c2008-02-06 02:57:51 +0100812 debug_log(DBG_PROCS, "Enter %s\n", __func__);
813
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814 tape->postponed_rq = HWGROUP(drive)->rq;
Borislav Petkov54bb2072008-02-06 02:57:52 +0100815 ide_stall_queue(drive, tape->dsc_poll_freq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816}
817
Borislav Petkovd236d742008-04-18 00:46:27 +0200818typedef void idetape_io_buf(ide_drive_t *, struct ide_atapi_pc *, unsigned int);
Borislav Petkova1efc852008-02-06 02:57:52 +0100819
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820/*
Borislav Petkova1efc852008-02-06 02:57:52 +0100821 * This is the usual interrupt handler which will be called during a packet
822 * command. We will transfer some of the data (as requested by the drive) and
823 * will re-point interrupt handler to us. When data transfer is finished, we
824 * will act according to the algorithm described before
Borislav Petkov8d06bfa2008-02-06 02:57:53 +0100825 * idetape_issue_pc.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826 */
Borislav Petkova1efc852008-02-06 02:57:52 +0100827static ide_startstop_t idetape_pc_intr(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828{
829 ide_hwif_t *hwif = drive->hwif;
830 idetape_tape_t *tape = drive->driver_data;
Borislav Petkovd236d742008-04-18 00:46:27 +0200831 struct ide_atapi_pc *pc = tape->pc;
Borislav Petkova1efc852008-02-06 02:57:52 +0100832 xfer_func_t *xferfunc;
833 idetape_io_buf *iobuf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834 unsigned int temp;
835#if SIMULATE_ERRORS
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100836 static int error_sim_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837#endif
Bartlomiej Zolnierkiewicz790d1232008-01-25 22:17:12 +0100838 u16 bcount;
Bartlomiej Zolnierkiewicz8e7657a2008-01-25 22:17:12 +0100839 u8 stat, ireason;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840
Borislav Petkov8004a8c2008-02-06 02:57:51 +0100841 debug_log(DBG_PROCS, "Enter %s - interrupt handler\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842
843 /* Clear the interrupt */
Bartlomiej Zolnierkiewiczc47137a2008-02-06 02:57:51 +0100844 stat = ide_read_status(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845
Borislav Petkov346331f2008-04-18 00:46:26 +0200846 if (pc->flags & PC_FLAG_DMA_IN_PROGRESS) {
Bartlomiej Zolnierkiewicz5e37bdc2008-04-26 22:25:24 +0200847 if (hwif->dma_ops->dma_end(drive) || (stat & ERR_STAT)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848 /*
849 * A DMA error is sometimes expected. For example,
850 * if the tape is crossing a filemark during a
851 * READ command, it will issue an irq and position
852 * itself before the filemark, so that only a partial
853 * data transfer will occur (which causes the DMA
854 * error). In that case, we will later ask the tape
855 * how much bytes of the original request were
856 * actually transferred (we can't receive that
857 * information from the DMA engine on most chipsets).
858 */
859
860 /*
861 * On the contrary, a DMA error is never expected;
862 * it usually indicates a hardware error or abort.
863 * If the tape crosses a filemark during a READ
864 * command, it will issue an irq and position itself
865 * after the filemark (not before). Only a partial
866 * data transfer will occur, but no DMA error.
867 * (AS, 19 Apr 2001)
868 */
Borislav Petkov346331f2008-04-18 00:46:26 +0200869 pc->flags |= PC_FLAG_DMA_ERROR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870 } else {
Borislav Petkovd236d742008-04-18 00:46:27 +0200871 pc->xferred = pc->req_xfer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872 idetape_update_buffers(pc);
873 }
Borislav Petkov8004a8c2008-02-06 02:57:51 +0100874 debug_log(DBG_PROCS, "DMA finished\n");
875
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876 }
877
878 /* No more interrupts */
Bartlomiej Zolnierkiewicz22c525b2008-01-25 22:17:11 +0100879 if ((stat & DRQ_STAT) == 0) {
Borislav Petkov8004a8c2008-02-06 02:57:51 +0100880 debug_log(DBG_SENSE, "Packet command completed, %d bytes"
Borislav Petkovd236d742008-04-18 00:46:27 +0200881 " transferred\n", pc->xferred);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882
Borislav Petkov346331f2008-04-18 00:46:26 +0200883 pc->flags &= ~PC_FLAG_DMA_IN_PROGRESS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884 local_irq_enable();
885
886#if SIMULATE_ERRORS
Borislav Petkov90699ce2008-02-02 19:56:50 +0100887 if ((pc->c[0] == WRITE_6 || pc->c[0] == READ_6) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888 (++error_sim_count % 100) == 0) {
889 printk(KERN_INFO "ide-tape: %s: simulating error\n",
890 tape->name);
Bartlomiej Zolnierkiewicz22c525b2008-01-25 22:17:11 +0100891 stat |= ERR_STAT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892 }
893#endif
Borislav Petkov90699ce2008-02-02 19:56:50 +0100894 if ((stat & ERR_STAT) && pc->c[0] == REQUEST_SENSE)
Bartlomiej Zolnierkiewicz22c525b2008-01-25 22:17:11 +0100895 stat &= ~ERR_STAT;
Borislav Petkov346331f2008-04-18 00:46:26 +0200896 if ((stat & ERR_STAT) || (pc->flags & PC_FLAG_DMA_ERROR)) {
Bartlomiej Zolnierkiewicz22c525b2008-01-25 22:17:11 +0100897 /* Error detected */
Borislav Petkov8004a8c2008-02-06 02:57:51 +0100898 debug_log(DBG_ERR, "%s: I/O error\n", tape->name);
899
Borislav Petkov90699ce2008-02-02 19:56:50 +0100900 if (pc->c[0] == REQUEST_SENSE) {
Borislav Petkova1efc852008-02-06 02:57:52 +0100901 printk(KERN_ERR "ide-tape: I/O error in request"
902 " sense command\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903 return ide_do_reset(drive);
904 }
Borislav Petkov8004a8c2008-02-06 02:57:51 +0100905 debug_log(DBG_ERR, "[cmd %x]: check condition\n",
906 pc->c[0]);
907
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908 /* Retry operation */
909 return idetape_retry_pc(drive);
910 }
911 pc->error = 0;
Borislav Petkov346331f2008-04-18 00:46:26 +0200912 if ((pc->flags & PC_FLAG_WAIT_FOR_DSC) &&
Bartlomiej Zolnierkiewicz22c525b2008-01-25 22:17:11 +0100913 (stat & SEEK_STAT) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914 /* Media access command */
915 tape->dsc_polling_start = jiffies;
Borislav Petkov54bb2072008-02-06 02:57:52 +0100916 tape->dsc_poll_freq = IDETAPE_DSC_MA_FAST;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917 tape->dsc_timeout = jiffies + IDETAPE_DSC_MA_TIMEOUT;
918 /* Allow ide.c to handle other requests */
919 idetape_postpone_request(drive);
920 return ide_stopped;
921 }
922 if (tape->failed_pc == pc)
923 tape->failed_pc = NULL;
924 /* Command finished - Call the callback function */
Borislav Petkovd236d742008-04-18 00:46:27 +0200925 return pc->idetape_callback(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926 }
Borislav Petkov346331f2008-04-18 00:46:26 +0200927
928 if (pc->flags & PC_FLAG_DMA_IN_PROGRESS) {
929 pc->flags &= ~PC_FLAG_DMA_IN_PROGRESS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930 printk(KERN_ERR "ide-tape: The tape wants to issue more "
931 "interrupts in DMA mode\n");
932 printk(KERN_ERR "ide-tape: DMA disabled, reverting to PIO\n");
Bartlomiej Zolnierkiewicz7469aaf2007-02-17 02:40:26 +0100933 ide_dma_off(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934 return ide_do_reset(drive);
935 }
936 /* Get the number of bytes to transfer on this interrupt. */
Bartlomiej Zolnierkiewicz23579a22008-04-18 00:46:26 +0200937 bcount = (hwif->INB(hwif->io_ports[IDE_BCOUNTH_OFFSET]) << 8) |
938 hwif->INB(hwif->io_ports[IDE_BCOUNTL_OFFSET]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939
Bartlomiej Zolnierkiewicz23579a22008-04-18 00:46:26 +0200940 ireason = hwif->INB(hwif->io_ports[IDE_IREASON_OFFSET]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941
Bartlomiej Zolnierkiewicz8e7657a2008-01-25 22:17:12 +0100942 if (ireason & CD) {
Borislav Petkova1efc852008-02-06 02:57:52 +0100943 printk(KERN_ERR "ide-tape: CoD != 0 in %s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944 return ide_do_reset(drive);
945 }
Borislav Petkov346331f2008-04-18 00:46:26 +0200946 if (((ireason & IO) == IO) == !!(pc->flags & PC_FLAG_WRITING)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947 /* Hopefully, we will never get here */
948 printk(KERN_ERR "ide-tape: We wanted to %s, ",
Bartlomiej Zolnierkiewicz8e7657a2008-01-25 22:17:12 +0100949 (ireason & IO) ? "Write" : "Read");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950 printk(KERN_ERR "ide-tape: but the tape wants us to %s !\n",
Bartlomiej Zolnierkiewicz8e7657a2008-01-25 22:17:12 +0100951 (ireason & IO) ? "Read" : "Write");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952 return ide_do_reset(drive);
953 }
Borislav Petkov346331f2008-04-18 00:46:26 +0200954 if (!(pc->flags & PC_FLAG_WRITING)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955 /* Reading - Check that we have enough space */
Borislav Petkovd236d742008-04-18 00:46:27 +0200956 temp = pc->xferred + bcount;
957 if (temp > pc->req_xfer) {
958 if (temp > pc->buf_size) {
Borislav Petkova1efc852008-02-06 02:57:52 +0100959 printk(KERN_ERR "ide-tape: The tape wants to "
960 "send us more data than expected "
961 "- discarding data\n");
Bartlomiej Zolnierkiewicz7616c0a2008-04-18 00:46:26 +0200962 ide_atapi_discard_data(drive, bcount);
Borislav Petkova1efc852008-02-06 02:57:52 +0100963 ide_set_handler(drive, &idetape_pc_intr,
964 IDETAPE_WAIT_CMD, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965 return ide_started;
966 }
Borislav Petkov8004a8c2008-02-06 02:57:51 +0100967 debug_log(DBG_SENSE, "The tape wants to send us more "
968 "data than expected - allowing transfer\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969 }
Borislav Petkova1efc852008-02-06 02:57:52 +0100970 iobuf = &idetape_input_buffers;
971 xferfunc = hwif->atapi_input_bytes;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972 } else {
Borislav Petkova1efc852008-02-06 02:57:52 +0100973 iobuf = &idetape_output_buffers;
974 xferfunc = hwif->atapi_output_bytes;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975 }
Borislav Petkova1efc852008-02-06 02:57:52 +0100976
977 if (pc->bh)
978 iobuf(drive, pc, bcount);
979 else
Borislav Petkovd236d742008-04-18 00:46:27 +0200980 xferfunc(drive, pc->cur_pos, bcount);
Borislav Petkova1efc852008-02-06 02:57:52 +0100981
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982 /* Update the current position */
Borislav Petkovd236d742008-04-18 00:46:27 +0200983 pc->xferred += bcount;
984 pc->cur_pos += bcount;
Borislav Petkov8004a8c2008-02-06 02:57:51 +0100985
986 debug_log(DBG_SENSE, "[cmd %x] transferred %d bytes on that intr.\n",
987 pc->c[0], bcount);
988
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989 /* And set the interrupt handler again */
990 ide_set_handler(drive, &idetape_pc_intr, IDETAPE_WAIT_CMD, NULL);
991 return ide_started;
992}
993
994/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100995 * Packet Command Interface
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100997 * The current Packet Command is available in tape->pc, and will not change
998 * until we finish handling it. Each packet command is associated with a
999 * callback function that will be called when the command is finished.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001001 * The handling will be done in three stages:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001003 * 1. idetape_issue_pc will send the packet command to the drive, and will set
1004 * the interrupt handler to idetape_pc_intr.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001006 * 2. On each interrupt, idetape_pc_intr will be called. This step will be
1007 * repeated until the device signals us that no more interrupts will be issued.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001009 * 3. ATAPI Tape media access commands have immediate status with a delayed
1010 * process. In case of a successful initiation of a media access packet command,
1011 * the DSC bit will be set when the actual execution of the command is finished.
1012 * Since the tape drive will not issue an interrupt, we have to poll for this
1013 * event. In this case, we define the request as "low priority request" by
1014 * setting rq_status to IDETAPE_RQ_POSTPONED, set a timer to poll for DSC and
1015 * exit the driver.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001017 * ide.c will then give higher priority to requests which originate from the
1018 * other device, until will change rq_status to RQ_ACTIVE.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001020 * 4. When the packet command is finished, it will be checked for errors.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001022 * 5. In case an error was found, we queue a request sense packet command in
1023 * front of the request queue and retry the operation up to
1024 * IDETAPE_MAX_PC_RETRIES times.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001026 * 6. In case no error was found, or we decided to give up and not to retry
1027 * again, the callback function will be called and then we will handle the next
1028 * request.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029 */
1030static ide_startstop_t idetape_transfer_pc(ide_drive_t *drive)
1031{
1032 ide_hwif_t *hwif = drive->hwif;
1033 idetape_tape_t *tape = drive->driver_data;
Borislav Petkovd236d742008-04-18 00:46:27 +02001034 struct ide_atapi_pc *pc = tape->pc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035 int retries = 100;
1036 ide_startstop_t startstop;
Bartlomiej Zolnierkiewicz8e7657a2008-01-25 22:17:12 +01001037 u8 ireason;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001039 if (ide_wait_stat(&startstop, drive, DRQ_STAT, BUSY_STAT, WAIT_READY)) {
1040 printk(KERN_ERR "ide-tape: Strange, packet command initiated "
1041 "yet DRQ isn't asserted\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042 return startstop;
1043 }
Bartlomiej Zolnierkiewicz23579a22008-04-18 00:46:26 +02001044 ireason = hwif->INB(hwif->io_ports[IDE_IREASON_OFFSET]);
Bartlomiej Zolnierkiewicz8e7657a2008-01-25 22:17:12 +01001045 while (retries-- && ((ireason & CD) == 0 || (ireason & IO))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046 printk(KERN_ERR "ide-tape: (IO,CoD != (0,1) while issuing "
1047 "a packet command, retrying\n");
1048 udelay(100);
Bartlomiej Zolnierkiewicz23579a22008-04-18 00:46:26 +02001049 ireason = hwif->INB(hwif->io_ports[IDE_IREASON_OFFSET]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050 if (retries == 0) {
1051 printk(KERN_ERR "ide-tape: (IO,CoD != (0,1) while "
1052 "issuing a packet command, ignoring\n");
Bartlomiej Zolnierkiewicz8e7657a2008-01-25 22:17:12 +01001053 ireason |= CD;
1054 ireason &= ~IO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055 }
1056 }
Bartlomiej Zolnierkiewicz8e7657a2008-01-25 22:17:12 +01001057 if ((ireason & CD) == 0 || (ireason & IO)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058 printk(KERN_ERR "ide-tape: (IO,CoD) != (0,1) while issuing "
1059 "a packet command\n");
1060 return ide_do_reset(drive);
1061 }
1062 /* Set the interrupt routine */
1063 ide_set_handler(drive, &idetape_pc_intr, IDETAPE_WAIT_CMD, NULL);
1064#ifdef CONFIG_BLK_DEV_IDEDMA
1065 /* Begin DMA, if necessary */
Borislav Petkov346331f2008-04-18 00:46:26 +02001066 if (pc->flags & PC_FLAG_DMA_IN_PROGRESS)
Bartlomiej Zolnierkiewicz5e37bdc2008-04-26 22:25:24 +02001067 hwif->dma_ops->dma_start(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068#endif
1069 /* Send the actual packet */
1070 HWIF(drive)->atapi_output_bytes(drive, pc->c, 12);
1071 return ide_started;
1072}
1073
Borislav Petkovd236d742008-04-18 00:46:27 +02001074static ide_startstop_t idetape_issue_pc(ide_drive_t *drive,
1075 struct ide_atapi_pc *pc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076{
1077 ide_hwif_t *hwif = drive->hwif;
1078 idetape_tape_t *tape = drive->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079 int dma_ok = 0;
Bartlomiej Zolnierkiewicz790d1232008-01-25 22:17:12 +01001080 u16 bcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081
Borislav Petkov90699ce2008-02-02 19:56:50 +01001082 if (tape->pc->c[0] == REQUEST_SENSE &&
1083 pc->c[0] == REQUEST_SENSE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084 printk(KERN_ERR "ide-tape: possible ide-tape.c bug - "
1085 "Two request sense in serial were issued\n");
1086 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087
Borislav Petkov90699ce2008-02-02 19:56:50 +01001088 if (tape->failed_pc == NULL && pc->c[0] != REQUEST_SENSE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089 tape->failed_pc = pc;
1090 /* Set the current packet command */
1091 tape->pc = pc;
1092
1093 if (pc->retries > IDETAPE_MAX_PC_RETRIES ||
Borislav Petkov346331f2008-04-18 00:46:26 +02001094 (pc->flags & PC_FLAG_ABORT)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095 /*
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001096 * We will "abort" retrying a packet command in case legitimate
1097 * error code was received (crossing a filemark, or end of the
1098 * media, for example).
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099 */
Borislav Petkov346331f2008-04-18 00:46:26 +02001100 if (!(pc->flags & PC_FLAG_ABORT)) {
Borislav Petkov90699ce2008-02-02 19:56:50 +01001101 if (!(pc->c[0] == TEST_UNIT_READY &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102 tape->sense_key == 2 && tape->asc == 4 &&
1103 (tape->ascq == 1 || tape->ascq == 8))) {
1104 printk(KERN_ERR "ide-tape: %s: I/O error, "
1105 "pc = %2x, key = %2x, "
1106 "asc = %2x, ascq = %2x\n",
1107 tape->name, pc->c[0],
1108 tape->sense_key, tape->asc,
1109 tape->ascq);
1110 }
1111 /* Giving up */
1112 pc->error = IDETAPE_ERROR_GENERAL;
1113 }
1114 tape->failed_pc = NULL;
Borislav Petkovd236d742008-04-18 00:46:27 +02001115 return pc->idetape_callback(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116 }
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001117 debug_log(DBG_SENSE, "Retry #%d, cmd = %02X\n", pc->retries, pc->c[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118
1119 pc->retries++;
1120 /* We haven't transferred any data yet */
Borislav Petkovd236d742008-04-18 00:46:27 +02001121 pc->xferred = 0;
1122 pc->cur_pos = pc->buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123 /* Request to transfer the entire buffer at once */
Borislav Petkovd236d742008-04-18 00:46:27 +02001124 bcount = pc->req_xfer;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125
Borislav Petkov346331f2008-04-18 00:46:26 +02001126 if (pc->flags & PC_FLAG_DMA_ERROR) {
1127 pc->flags &= ~PC_FLAG_DMA_ERROR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128 printk(KERN_WARNING "ide-tape: DMA disabled, "
1129 "reverting to PIO\n");
Bartlomiej Zolnierkiewicz7469aaf2007-02-17 02:40:26 +01001130 ide_dma_off(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131 }
Borislav Petkov346331f2008-04-18 00:46:26 +02001132 if ((pc->flags & PC_FLAG_DMA_RECOMMENDED) && drive->using_dma)
Bartlomiej Zolnierkiewicz5e37bdc2008-04-26 22:25:24 +02001133 dma_ok = !hwif->dma_ops->dma_setup(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134
Bartlomiej Zolnierkiewicz2fc57382008-01-25 22:17:13 +01001135 ide_pktcmd_tf_load(drive, IDE_TFLAG_NO_SELECT_MASK |
1136 IDE_TFLAG_OUT_DEVICE, bcount, dma_ok);
1137
Borislav Petkov346331f2008-04-18 00:46:26 +02001138 if (dma_ok)
1139 /* Will begin DMA later */
1140 pc->flags |= PC_FLAG_DMA_IN_PROGRESS;
1141 if (test_bit(IDETAPE_FLAG_DRQ_INTERRUPT, &tape->flags)) {
Bartlomiej Zolnierkiewiczc1c9dbc2008-02-02 19:56:44 +01001142 ide_execute_command(drive, WIN_PACKETCMD, &idetape_transfer_pc,
1143 IDETAPE_WAIT_CMD, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144 return ide_started;
1145 } else {
Bartlomiej Zolnierkiewicz23579a22008-04-18 00:46:26 +02001146 hwif->OUTB(WIN_PACKETCMD, hwif->io_ports[IDE_COMMAND_OFFSET]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147 return idetape_transfer_pc(drive);
1148 }
1149}
1150
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001151static ide_startstop_t idetape_pc_callback(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152{
1153 idetape_tape_t *tape = drive->driver_data;
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001154
1155 debug_log(DBG_PROCS, "Enter %s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156
1157 idetape_end_request(drive, tape->pc->error ? 0 : 1, 0);
1158 return ide_stopped;
1159}
1160
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001161/* A mode sense command is used to "sense" tape parameters. */
Borislav Petkovd236d742008-04-18 00:46:27 +02001162static void idetape_create_mode_sense_cmd(struct ide_atapi_pc *pc, u8 page_code)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163{
1164 idetape_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +01001165 pc->c[0] = MODE_SENSE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166 if (page_code != IDETAPE_BLOCK_DESCRIPTOR)
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001167 /* DBD = 1 - Don't return block descriptors */
1168 pc->c[1] = 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169 pc->c[2] = page_code;
1170 /*
1171 * Changed pc->c[3] to 0 (255 will at best return unused info).
1172 *
1173 * For SCSI this byte is defined as subpage instead of high byte
1174 * of length and some IDE drives seem to interpret it this way
1175 * and return an error when 255 is used.
1176 */
1177 pc->c[3] = 0;
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001178 /* We will just discard data in that case */
1179 pc->c[4] = 255;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180 if (page_code == IDETAPE_BLOCK_DESCRIPTOR)
Borislav Petkovd236d742008-04-18 00:46:27 +02001181 pc->req_xfer = 12;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182 else if (page_code == IDETAPE_CAPABILITIES_PAGE)
Borislav Petkovd236d742008-04-18 00:46:27 +02001183 pc->req_xfer = 24;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184 else
Borislav Petkovd236d742008-04-18 00:46:27 +02001185 pc->req_xfer = 50;
1186 pc->idetape_callback = &idetape_pc_callback;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187}
1188
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001189static ide_startstop_t idetape_media_access_finished(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190{
1191 idetape_tape_t *tape = drive->driver_data;
Borislav Petkovd236d742008-04-18 00:46:27 +02001192 struct ide_atapi_pc *pc = tape->pc;
Bartlomiej Zolnierkiewicz22c525b2008-01-25 22:17:11 +01001193 u8 stat;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194
Bartlomiej Zolnierkiewiczc47137a2008-02-06 02:57:51 +01001195 stat = ide_read_status(drive);
1196
Bartlomiej Zolnierkiewicz22c525b2008-01-25 22:17:11 +01001197 if (stat & SEEK_STAT) {
1198 if (stat & ERR_STAT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199 /* Error detected */
Borislav Petkov90699ce2008-02-02 19:56:50 +01001200 if (pc->c[0] != TEST_UNIT_READY)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201 printk(KERN_ERR "ide-tape: %s: I/O error, ",
1202 tape->name);
1203 /* Retry operation */
1204 return idetape_retry_pc(drive);
1205 }
1206 pc->error = 0;
1207 if (tape->failed_pc == pc)
1208 tape->failed_pc = NULL;
1209 } else {
1210 pc->error = IDETAPE_ERROR_GENERAL;
1211 tape->failed_pc = NULL;
1212 }
Borislav Petkovd236d742008-04-18 00:46:27 +02001213 return pc->idetape_callback(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214}
1215
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001216static ide_startstop_t idetape_rw_callback(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217{
1218 idetape_tape_t *tape = drive->driver_data;
1219 struct request *rq = HWGROUP(drive)->rq;
Borislav Petkovd236d742008-04-18 00:46:27 +02001220 int blocks = tape->pc->xferred / tape->blk_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221
Borislav Petkov54bb2072008-02-06 02:57:52 +01001222 tape->avg_size += blocks * tape->blk_size;
1223 tape->insert_size += blocks * tape->blk_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224 if (tape->insert_size > 1024 * 1024)
1225 tape->measure_insert_time = 1;
1226 if (tape->measure_insert_time) {
1227 tape->measure_insert_time = 0;
1228 tape->insert_time = jiffies;
1229 tape->insert_size = 0;
1230 }
1231 if (time_after(jiffies, tape->insert_time))
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001232 tape->insert_speed = tape->insert_size / 1024 * HZ /
1233 (jiffies - tape->insert_time);
Marcelo Feitoza Parisi9bae1ff2006-03-28 01:56:46 -08001234 if (time_after_eq(jiffies, tape->avg_time + HZ)) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001235 tape->avg_speed = tape->avg_size * HZ /
1236 (jiffies - tape->avg_time) / 1024;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237 tape->avg_size = 0;
1238 tape->avg_time = jiffies;
1239 }
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001240 debug_log(DBG_PROCS, "Enter %s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241
Borislav Petkov54bb2072008-02-06 02:57:52 +01001242 tape->first_frame += blocks;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243 rq->current_nr_sectors -= blocks;
1244
1245 if (!tape->pc->error)
1246 idetape_end_request(drive, 1, 0);
1247 else
1248 idetape_end_request(drive, tape->pc->error, 0);
1249 return ide_stopped;
1250}
1251
Borislav Petkovd236d742008-04-18 00:46:27 +02001252static void idetape_create_read_cmd(idetape_tape_t *tape,
1253 struct ide_atapi_pc *pc,
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001254 unsigned int length, struct idetape_bh *bh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255{
1256 idetape_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +01001257 pc->c[0] = READ_6;
Borislav Petkov860ff5e2008-02-02 19:56:50 +01001258 put_unaligned(cpu_to_be32(length), (unsigned int *) &pc->c[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259 pc->c[1] = 1;
Borislav Petkovd236d742008-04-18 00:46:27 +02001260 pc->idetape_callback = &idetape_rw_callback;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261 pc->bh = bh;
1262 atomic_set(&bh->b_count, 0);
Borislav Petkovd236d742008-04-18 00:46:27 +02001263 pc->buf = NULL;
1264 pc->buf_size = length * tape->blk_size;
1265 pc->req_xfer = pc->buf_size;
1266 if (pc->req_xfer == tape->stage_size)
Borislav Petkov346331f2008-04-18 00:46:26 +02001267 pc->flags |= PC_FLAG_DMA_RECOMMENDED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268}
1269
Borislav Petkovd236d742008-04-18 00:46:27 +02001270static void idetape_create_write_cmd(idetape_tape_t *tape,
1271 struct ide_atapi_pc *pc,
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001272 unsigned int length, struct idetape_bh *bh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273{
1274 idetape_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +01001275 pc->c[0] = WRITE_6;
Borislav Petkov860ff5e2008-02-02 19:56:50 +01001276 put_unaligned(cpu_to_be32(length), (unsigned int *) &pc->c[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277 pc->c[1] = 1;
Borislav Petkovd236d742008-04-18 00:46:27 +02001278 pc->idetape_callback = &idetape_rw_callback;
Borislav Petkov346331f2008-04-18 00:46:26 +02001279 pc->flags |= PC_FLAG_WRITING;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280 pc->bh = bh;
1281 pc->b_data = bh->b_data;
1282 pc->b_count = atomic_read(&bh->b_count);
Borislav Petkovd236d742008-04-18 00:46:27 +02001283 pc->buf = NULL;
1284 pc->buf_size = length * tape->blk_size;
1285 pc->req_xfer = pc->buf_size;
1286 if (pc->req_xfer == tape->stage_size)
Borislav Petkov346331f2008-04-18 00:46:26 +02001287 pc->flags |= PC_FLAG_DMA_RECOMMENDED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288}
1289
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290static ide_startstop_t idetape_do_request(ide_drive_t *drive,
1291 struct request *rq, sector_t block)
1292{
1293 idetape_tape_t *tape = drive->driver_data;
Borislav Petkovd236d742008-04-18 00:46:27 +02001294 struct ide_atapi_pc *pc = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295 struct request *postponed_rq = tape->postponed_rq;
Bartlomiej Zolnierkiewicz22c525b2008-01-25 22:17:11 +01001296 u8 stat;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001298 debug_log(DBG_SENSE, "sector: %ld, nr_sectors: %ld,"
1299 " current_nr_sectors: %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300 rq->sector, rq->nr_sectors, rq->current_nr_sectors);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301
Jens Axboe4aff5e22006-08-10 08:44:47 +02001302 if (!blk_special_request(rq)) {
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001303 /* We do not support buffer cache originated requests. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304 printk(KERN_NOTICE "ide-tape: %s: Unsupported request in "
Jens Axboe4aff5e22006-08-10 08:44:47 +02001305 "request queue (%d)\n", drive->name, rq->cmd_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306 ide_end_request(drive, 0, 0);
1307 return ide_stopped;
1308 }
1309
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001310 /* Retry a failed packet command */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001311 if (tape->failed_pc && tape->pc->c[0] == REQUEST_SENSE)
Borislav Petkov8d06bfa2008-02-06 02:57:53 +01001312 return idetape_issue_pc(drive, tape->failed_pc);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001313
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314 if (postponed_rq != NULL)
1315 if (rq != postponed_rq) {
1316 printk(KERN_ERR "ide-tape: ide-tape.c bug - "
1317 "Two DSC requests were queued\n");
1318 idetape_end_request(drive, 0, 0);
1319 return ide_stopped;
1320 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321
1322 tape->postponed_rq = NULL;
1323
1324 /*
1325 * If the tape is still busy, postpone our request and service
1326 * the other device meanwhile.
1327 */
Bartlomiej Zolnierkiewiczc47137a2008-02-06 02:57:51 +01001328 stat = ide_read_status(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329
1330 if (!drive->dsc_overlap && !(rq->cmd[0] & REQ_IDETAPE_PC2))
Borislav Petkov346331f2008-04-18 00:46:26 +02001331 set_bit(IDETAPE_FLAG_IGNORE_DSC, &tape->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332
1333 if (drive->post_reset == 1) {
Borislav Petkov346331f2008-04-18 00:46:26 +02001334 set_bit(IDETAPE_FLAG_IGNORE_DSC, &tape->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335 drive->post_reset = 0;
1336 }
1337
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338 if (time_after(jiffies, tape->insert_time))
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001339 tape->insert_speed = tape->insert_size / 1024 * HZ /
1340 (jiffies - tape->insert_time);
Borislav Petkov346331f2008-04-18 00:46:26 +02001341 if (!test_and_clear_bit(IDETAPE_FLAG_IGNORE_DSC, &tape->flags) &&
Bartlomiej Zolnierkiewicz22c525b2008-01-25 22:17:11 +01001342 (stat & SEEK_STAT) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343 if (postponed_rq == NULL) {
1344 tape->dsc_polling_start = jiffies;
Borislav Petkov54bb2072008-02-06 02:57:52 +01001345 tape->dsc_poll_freq = tape->best_dsc_rw_freq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346 tape->dsc_timeout = jiffies + IDETAPE_DSC_RW_TIMEOUT;
1347 } else if (time_after(jiffies, tape->dsc_timeout)) {
1348 printk(KERN_ERR "ide-tape: %s: DSC timeout\n",
1349 tape->name);
1350 if (rq->cmd[0] & REQ_IDETAPE_PC2) {
1351 idetape_media_access_finished(drive);
1352 return ide_stopped;
1353 } else {
1354 return ide_do_reset(drive);
1355 }
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001356 } else if (time_after(jiffies,
1357 tape->dsc_polling_start +
1358 IDETAPE_DSC_MA_THRESHOLD))
Borislav Petkov54bb2072008-02-06 02:57:52 +01001359 tape->dsc_poll_freq = IDETAPE_DSC_MA_SLOW;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360 idetape_postpone_request(drive);
1361 return ide_stopped;
1362 }
1363 if (rq->cmd[0] & REQ_IDETAPE_READ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364 tape->postpone_cnt = 0;
1365 pc = idetape_next_pc_storage(drive);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001366 idetape_create_read_cmd(tape, pc, rq->current_nr_sectors,
1367 (struct idetape_bh *)rq->special);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368 goto out;
1369 }
1370 if (rq->cmd[0] & REQ_IDETAPE_WRITE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371 tape->postpone_cnt = 0;
1372 pc = idetape_next_pc_storage(drive);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001373 idetape_create_write_cmd(tape, pc, rq->current_nr_sectors,
1374 (struct idetape_bh *)rq->special);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375 goto out;
1376 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377 if (rq->cmd[0] & REQ_IDETAPE_PC1) {
Borislav Petkovd236d742008-04-18 00:46:27 +02001378 pc = (struct ide_atapi_pc *) rq->buffer;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379 rq->cmd[0] &= ~(REQ_IDETAPE_PC1);
1380 rq->cmd[0] |= REQ_IDETAPE_PC2;
1381 goto out;
1382 }
1383 if (rq->cmd[0] & REQ_IDETAPE_PC2) {
1384 idetape_media_access_finished(drive);
1385 return ide_stopped;
1386 }
1387 BUG();
1388out:
Borislav Petkov8d06bfa2008-02-06 02:57:53 +01001389 return idetape_issue_pc(drive, pc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390}
1391
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001392/* Pipeline related functions */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393
1394/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001395 * The function below uses __get_free_page to allocate a pipeline stage, along
1396 * with all the necessary small buffers which together make a buffer of size
1397 * tape->stage_size (or a bit more). We attempt to combine sequential pages as
1398 * much as possible.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001400 * It returns a pointer to the new allocated stage, or NULL if we can't (or
1401 * don't want to) allocate a stage.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001403 * Pipeline stages are optional and are used to increase performance. If we
1404 * can't allocate them, we'll manage without them.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405 */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001406static idetape_stage_t *__idetape_kmalloc_stage(idetape_tape_t *tape, int full,
1407 int clear)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408{
1409 idetape_stage_t *stage;
1410 struct idetape_bh *prev_bh, *bh;
1411 int pages = tape->pages_per_stage;
1412 char *b_data = NULL;
1413
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001414 stage = kmalloc(sizeof(idetape_stage_t), GFP_KERNEL);
1415 if (!stage)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416 return NULL;
1417 stage->next = NULL;
1418
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001419 stage->bh = kmalloc(sizeof(struct idetape_bh), GFP_KERNEL);
1420 bh = stage->bh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421 if (bh == NULL)
1422 goto abort;
1423 bh->b_reqnext = NULL;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001424 bh->b_data = (char *) __get_free_page(GFP_KERNEL);
1425 if (!bh->b_data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426 goto abort;
1427 if (clear)
1428 memset(bh->b_data, 0, PAGE_SIZE);
1429 bh->b_size = PAGE_SIZE;
1430 atomic_set(&bh->b_count, full ? bh->b_size : 0);
1431
1432 while (--pages) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001433 b_data = (char *) __get_free_page(GFP_KERNEL);
1434 if (!b_data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435 goto abort;
1436 if (clear)
1437 memset(b_data, 0, PAGE_SIZE);
1438 if (bh->b_data == b_data + PAGE_SIZE) {
1439 bh->b_size += PAGE_SIZE;
1440 bh->b_data -= PAGE_SIZE;
1441 if (full)
1442 atomic_add(PAGE_SIZE, &bh->b_count);
1443 continue;
1444 }
1445 if (b_data == bh->b_data + bh->b_size) {
1446 bh->b_size += PAGE_SIZE;
1447 if (full)
1448 atomic_add(PAGE_SIZE, &bh->b_count);
1449 continue;
1450 }
1451 prev_bh = bh;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001452 bh = kmalloc(sizeof(struct idetape_bh), GFP_KERNEL);
1453 if (!bh) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454 free_page((unsigned long) b_data);
1455 goto abort;
1456 }
1457 bh->b_reqnext = NULL;
1458 bh->b_data = b_data;
1459 bh->b_size = PAGE_SIZE;
1460 atomic_set(&bh->b_count, full ? bh->b_size : 0);
1461 prev_bh->b_reqnext = bh;
1462 }
1463 bh->b_size -= tape->excess_bh_size;
1464 if (full)
1465 atomic_sub(tape->excess_bh_size, &bh->b_count);
1466 return stage;
1467abort:
1468 __idetape_kfree_stage(stage);
1469 return NULL;
1470}
1471
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001472static int idetape_copy_stage_from_user(idetape_tape_t *tape,
Borislav Petkov8646c882008-04-27 15:38:26 +02001473 const char __user *buf, int n)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474{
1475 struct idetape_bh *bh = tape->bh;
1476 int count;
Daniel Walkerdcd96372006-06-25 05:47:37 -07001477 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478
1479 while (n) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480 if (bh == NULL) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001481 printk(KERN_ERR "ide-tape: bh == NULL in %s\n",
1482 __func__);
Daniel Walkerdcd96372006-06-25 05:47:37 -07001483 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001484 }
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001485 count = min((unsigned int)
1486 (bh->b_size - atomic_read(&bh->b_count)),
1487 (unsigned int)n);
1488 if (copy_from_user(bh->b_data + atomic_read(&bh->b_count), buf,
1489 count))
Daniel Walkerdcd96372006-06-25 05:47:37 -07001490 ret = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491 n -= count;
1492 atomic_add(count, &bh->b_count);
1493 buf += count;
1494 if (atomic_read(&bh->b_count) == bh->b_size) {
1495 bh = bh->b_reqnext;
1496 if (bh)
1497 atomic_set(&bh->b_count, 0);
1498 }
1499 }
1500 tape->bh = bh;
Daniel Walkerdcd96372006-06-25 05:47:37 -07001501 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502}
1503
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001504static int idetape_copy_stage_to_user(idetape_tape_t *tape, char __user *buf,
Borislav Petkov99d74e62008-04-27 15:38:25 +02001505 int n)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506{
1507 struct idetape_bh *bh = tape->bh;
1508 int count;
Daniel Walkerdcd96372006-06-25 05:47:37 -07001509 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510
1511 while (n) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512 if (bh == NULL) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001513 printk(KERN_ERR "ide-tape: bh == NULL in %s\n",
1514 __func__);
Daniel Walkerdcd96372006-06-25 05:47:37 -07001515 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001516 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517 count = min(tape->b_count, n);
Daniel Walkerdcd96372006-06-25 05:47:37 -07001518 if (copy_to_user(buf, tape->b_data, count))
1519 ret = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520 n -= count;
1521 tape->b_data += count;
1522 tape->b_count -= count;
1523 buf += count;
1524 if (!tape->b_count) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001525 bh = bh->b_reqnext;
1526 tape->bh = bh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527 if (bh) {
1528 tape->b_data = bh->b_data;
1529 tape->b_count = atomic_read(&bh->b_count);
1530 }
1531 }
1532 }
Daniel Walkerdcd96372006-06-25 05:47:37 -07001533 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534}
1535
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001536static void idetape_init_merge_stage(idetape_tape_t *tape)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537{
1538 struct idetape_bh *bh = tape->merge_stage->bh;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001539
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540 tape->bh = bh;
Borislav Petkov54abf372008-02-06 02:57:52 +01001541 if (tape->chrdev_dir == IDETAPE_DIR_WRITE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542 atomic_set(&bh->b_count, 0);
1543 else {
1544 tape->b_data = bh->b_data;
1545 tape->b_count = atomic_read(&bh->b_count);
1546 }
1547}
1548
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001549/* Install a completion in a pending request and sleep until it is serviced. The
1550 * caller should ensure that the request will not be serviced before we install
1551 * the completion (usually by disabling interrupts).
Linus Torvalds1da177e2005-04-16 15:20:36 -07001552 */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001553static void idetape_wait_for_request(ide_drive_t *drive, struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554{
Peter Zijlstra6e9a4732006-09-30 23:28:10 -07001555 DECLARE_COMPLETION_ONSTACK(wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556 idetape_tape_t *tape = drive->driver_data;
1557
Jens Axboe4aff5e22006-08-10 08:44:47 +02001558 if (rq == NULL || !blk_special_request(rq)) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001559 printk(KERN_ERR "ide-tape: bug: Trying to sleep on non-valid"
1560 " request\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001561 return;
1562 }
Jens Axboec00895a2006-09-30 20:29:12 +02001563 rq->end_io_data = &wait;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001564 rq->end_io = blk_end_sync_rq;
Borislav Petkov54bb2072008-02-06 02:57:52 +01001565 spin_unlock_irq(&tape->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001566 wait_for_completion(&wait);
1567 /* The stage and its struct request have been deallocated */
Borislav Petkov54bb2072008-02-06 02:57:52 +01001568 spin_lock_irq(&tape->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569}
1570
Borislav Petkova2f5b7f2008-02-06 02:57:51 +01001571static ide_startstop_t idetape_read_position_callback(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572{
1573 idetape_tape_t *tape = drive->driver_data;
Borislav Petkovd236d742008-04-18 00:46:27 +02001574 u8 *readpos = tape->pc->buf;
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001575
1576 debug_log(DBG_PROCS, "Enter %s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001577
1578 if (!tape->pc->error) {
Borislav Petkova2f5b7f2008-02-06 02:57:51 +01001579 debug_log(DBG_SENSE, "BOP - %s\n",
1580 (readpos[0] & 0x80) ? "Yes" : "No");
1581 debug_log(DBG_SENSE, "EOP - %s\n",
1582 (readpos[0] & 0x40) ? "Yes" : "No");
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001583
Borislav Petkova2f5b7f2008-02-06 02:57:51 +01001584 if (readpos[0] & 0x4) {
1585 printk(KERN_INFO "ide-tape: Block location is unknown"
1586 "to the tape\n");
Borislav Petkov346331f2008-04-18 00:46:26 +02001587 clear_bit(IDETAPE_FLAG_ADDRESS_VALID, &tape->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001588 idetape_end_request(drive, 0, 0);
1589 } else {
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001590 debug_log(DBG_SENSE, "Block Location - %u\n",
Borislav Petkova2f5b7f2008-02-06 02:57:51 +01001591 be32_to_cpu(*(u32 *)&readpos[4]));
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001592
Borislav Petkova2f5b7f2008-02-06 02:57:51 +01001593 tape->partition = readpos[1];
Borislav Petkov54bb2072008-02-06 02:57:52 +01001594 tape->first_frame =
Borislav Petkova2f5b7f2008-02-06 02:57:51 +01001595 be32_to_cpu(*(u32 *)&readpos[4]);
Borislav Petkov346331f2008-04-18 00:46:26 +02001596 set_bit(IDETAPE_FLAG_ADDRESS_VALID, &tape->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001597 idetape_end_request(drive, 1, 0);
1598 }
1599 } else {
1600 idetape_end_request(drive, 0, 0);
1601 }
1602 return ide_stopped;
1603}
1604
1605/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001606 * Write a filemark if write_filemark=1. Flush the device buffers without
1607 * writing a filemark otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608 */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001609static void idetape_create_write_filemark_cmd(ide_drive_t *drive,
Borislav Petkovd236d742008-04-18 00:46:27 +02001610 struct ide_atapi_pc *pc, int write_filemark)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001611{
1612 idetape_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +01001613 pc->c[0] = WRITE_FILEMARKS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001614 pc->c[4] = write_filemark;
Borislav Petkov346331f2008-04-18 00:46:26 +02001615 pc->flags |= PC_FLAG_WAIT_FOR_DSC;
Borislav Petkovd236d742008-04-18 00:46:27 +02001616 pc->idetape_callback = &idetape_pc_callback;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617}
1618
Borislav Petkovd236d742008-04-18 00:46:27 +02001619static void idetape_create_test_unit_ready_cmd(struct ide_atapi_pc *pc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001620{
1621 idetape_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +01001622 pc->c[0] = TEST_UNIT_READY;
Borislav Petkovd236d742008-04-18 00:46:27 +02001623 pc->idetape_callback = &idetape_pc_callback;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001624}
1625
1626/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001627 * We add a special packet command request to the tail of the request queue, and
1628 * wait for it to be serviced. This is not to be called from within the request
1629 * handling part of the driver! We allocate here data on the stack and it is
1630 * valid until the request is finished. This is not the case for the bottom part
1631 * of the driver, where we are always leaving the functions to wait for an
1632 * interrupt or a timer event.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001633 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001634 * From the bottom part of the driver, we should allocate safe memory using
1635 * idetape_next_pc_storage() and ide_tape_next_rq_storage(), and add the request
1636 * to the request list without waiting for it to be serviced! In that case, we
1637 * usually use idetape_queue_pc_head().
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638 */
Borislav Petkovd236d742008-04-18 00:46:27 +02001639static int __idetape_queue_pc_tail(ide_drive_t *drive, struct ide_atapi_pc *pc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001640{
1641 struct ide_tape_obj *tape = drive->driver_data;
1642 struct request rq;
1643
1644 idetape_init_rq(&rq, REQ_IDETAPE_PC1);
1645 rq.buffer = (char *) pc;
1646 rq.rq_disk = tape->disk;
1647 return ide_do_drive_cmd(drive, &rq, ide_wait);
1648}
1649
Borislav Petkovd236d742008-04-18 00:46:27 +02001650static void idetape_create_load_unload_cmd(ide_drive_t *drive,
1651 struct ide_atapi_pc *pc, int cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001652{
1653 idetape_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +01001654 pc->c[0] = START_STOP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001655 pc->c[4] = cmd;
Borislav Petkov346331f2008-04-18 00:46:26 +02001656 pc->flags |= PC_FLAG_WAIT_FOR_DSC;
Borislav Petkovd236d742008-04-18 00:46:27 +02001657 pc->idetape_callback = &idetape_pc_callback;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001658}
1659
1660static int idetape_wait_ready(ide_drive_t *drive, unsigned long timeout)
1661{
1662 idetape_tape_t *tape = drive->driver_data;
Borislav Petkovd236d742008-04-18 00:46:27 +02001663 struct ide_atapi_pc pc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664 int load_attempted = 0;
1665
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001666 /* Wait for the tape to become ready */
Borislav Petkov346331f2008-04-18 00:46:26 +02001667 set_bit(IDETAPE_FLAG_MEDIUM_PRESENT, &tape->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001668 timeout += jiffies;
1669 while (time_before(jiffies, timeout)) {
1670 idetape_create_test_unit_ready_cmd(&pc);
1671 if (!__idetape_queue_pc_tail(drive, &pc))
1672 return 0;
1673 if ((tape->sense_key == 2 && tape->asc == 4 && tape->ascq == 2)
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001674 || (tape->asc == 0x3A)) {
1675 /* no media */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001676 if (load_attempted)
1677 return -ENOMEDIUM;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001678 idetape_create_load_unload_cmd(drive, &pc,
1679 IDETAPE_LU_LOAD_MASK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001680 __idetape_queue_pc_tail(drive, &pc);
1681 load_attempted = 1;
1682 /* not about to be ready */
1683 } else if (!(tape->sense_key == 2 && tape->asc == 4 &&
1684 (tape->ascq == 1 || tape->ascq == 8)))
1685 return -EIO;
Nishanth Aravamudan80ce45f2005-09-10 00:27:08 -07001686 msleep(100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001687 }
1688 return -EIO;
1689}
1690
Borislav Petkovd236d742008-04-18 00:46:27 +02001691static int idetape_queue_pc_tail(ide_drive_t *drive, struct ide_atapi_pc *pc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001692{
1693 return __idetape_queue_pc_tail(drive, pc);
1694}
1695
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001696static int idetape_flush_tape_buffers(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001697{
Borislav Petkovd236d742008-04-18 00:46:27 +02001698 struct ide_atapi_pc pc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001699 int rc;
1700
1701 idetape_create_write_filemark_cmd(drive, &pc, 0);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001702 rc = idetape_queue_pc_tail(drive, &pc);
1703 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001704 return rc;
1705 idetape_wait_ready(drive, 60 * 5 * HZ);
1706 return 0;
1707}
1708
Borislav Petkovd236d742008-04-18 00:46:27 +02001709static void idetape_create_read_position_cmd(struct ide_atapi_pc *pc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001710{
1711 idetape_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +01001712 pc->c[0] = READ_POSITION;
Borislav Petkovd236d742008-04-18 00:46:27 +02001713 pc->req_xfer = 20;
1714 pc->idetape_callback = &idetape_read_position_callback;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001715}
1716
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001717static int idetape_read_position(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001718{
1719 idetape_tape_t *tape = drive->driver_data;
Borislav Petkovd236d742008-04-18 00:46:27 +02001720 struct ide_atapi_pc pc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001721 int position;
1722
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001723 debug_log(DBG_PROCS, "Enter %s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001724
1725 idetape_create_read_position_cmd(&pc);
1726 if (idetape_queue_pc_tail(drive, &pc))
1727 return -1;
Borislav Petkov54bb2072008-02-06 02:57:52 +01001728 position = tape->first_frame;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001729 return position;
1730}
1731
Borislav Petkovd236d742008-04-18 00:46:27 +02001732static void idetape_create_locate_cmd(ide_drive_t *drive,
1733 struct ide_atapi_pc *pc,
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001734 unsigned int block, u8 partition, int skip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001735{
1736 idetape_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +01001737 pc->c[0] = POSITION_TO_ELEMENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001738 pc->c[1] = 2;
Borislav Petkov860ff5e2008-02-02 19:56:50 +01001739 put_unaligned(cpu_to_be32(block), (unsigned int *) &pc->c[3]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001740 pc->c[8] = partition;
Borislav Petkov346331f2008-04-18 00:46:26 +02001741 pc->flags |= PC_FLAG_WAIT_FOR_DSC;
Borislav Petkovd236d742008-04-18 00:46:27 +02001742 pc->idetape_callback = &idetape_pc_callback;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001743}
1744
Borislav Petkovd236d742008-04-18 00:46:27 +02001745static int idetape_create_prevent_cmd(ide_drive_t *drive,
1746 struct ide_atapi_pc *pc, int prevent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001747{
1748 idetape_tape_t *tape = drive->driver_data;
1749
Borislav Petkovb6422012008-02-02 19:56:49 +01001750 /* device supports locking according to capabilities page */
1751 if (!(tape->caps[6] & 0x01))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001752 return 0;
1753
1754 idetape_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +01001755 pc->c[0] = ALLOW_MEDIUM_REMOVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001756 pc->c[4] = prevent;
Borislav Petkovd236d742008-04-18 00:46:27 +02001757 pc->idetape_callback = &idetape_pc_callback;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001758 return 1;
1759}
1760
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001761static int __idetape_discard_read_pipeline(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001762{
1763 idetape_tape_t *tape = drive->driver_data;
1764 unsigned long flags;
1765 int cnt;
1766
Borislav Petkov54abf372008-02-06 02:57:52 +01001767 if (tape->chrdev_dir != IDETAPE_DIR_READ)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001768 return 0;
1769
1770 /* Remove merge stage. */
Borislav Petkov54bb2072008-02-06 02:57:52 +01001771 cnt = tape->merge_stage_size / tape->blk_size;
Borislav Petkov346331f2008-04-18 00:46:26 +02001772 if (test_and_clear_bit(IDETAPE_FLAG_FILEMARK, &tape->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001773 ++cnt; /* Filemarks count as 1 sector */
1774 tape->merge_stage_size = 0;
1775 if (tape->merge_stage != NULL) {
1776 __idetape_kfree_stage(tape->merge_stage);
1777 tape->merge_stage = NULL;
1778 }
1779
1780 /* Clear pipeline flags. */
Borislav Petkov346331f2008-04-18 00:46:26 +02001781 clear_bit(IDETAPE_FLAG_PIPELINE_ERR, &tape->flags);
Borislav Petkov54abf372008-02-06 02:57:52 +01001782 tape->chrdev_dir = IDETAPE_DIR_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001783
1784 /* Remove pipeline stages. */
1785 if (tape->first_stage == NULL)
1786 return 0;
1787
Borislav Petkov54bb2072008-02-06 02:57:52 +01001788 spin_lock_irqsave(&tape->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001789 tape->next_stage = NULL;
Borislav Petkovc4b22f82008-04-26 22:25:20 +02001790 if (test_bit(IDETAPE_FLAG_PIPELINE_ACTIVE, &tape->flags))
Borislav Petkov54bb2072008-02-06 02:57:52 +01001791 idetape_wait_for_request(drive, tape->active_data_rq);
1792 spin_unlock_irqrestore(&tape->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001793
1794 while (tape->first_stage != NULL) {
1795 struct request *rq_ptr = &tape->first_stage->rq;
1796
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001797 cnt += rq_ptr->nr_sectors - rq_ptr->current_nr_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001798 if (rq_ptr->errors == IDETAPE_ERROR_FILEMARK)
1799 ++cnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001800 }
1801 tape->nr_pending_stages = 0;
1802 tape->max_stages = tape->min_pipeline;
1803 return cnt;
1804}
1805
1806/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001807 * Position the tape to the requested block using the LOCATE packet command.
1808 * A READ POSITION command is then issued to check where we are positioned. Like
1809 * all higher level operations, we queue the commands at the tail of the request
1810 * queue and wait for their completion.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001811 */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001812static int idetape_position_tape(ide_drive_t *drive, unsigned int block,
1813 u8 partition, int skip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001814{
1815 idetape_tape_t *tape = drive->driver_data;
1816 int retval;
Borislav Petkovd236d742008-04-18 00:46:27 +02001817 struct ide_atapi_pc pc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001818
Borislav Petkov54abf372008-02-06 02:57:52 +01001819 if (tape->chrdev_dir == IDETAPE_DIR_READ)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001820 __idetape_discard_read_pipeline(drive);
1821 idetape_wait_ready(drive, 60 * 5 * HZ);
1822 idetape_create_locate_cmd(drive, &pc, block, partition, skip);
1823 retval = idetape_queue_pc_tail(drive, &pc);
1824 if (retval)
1825 return (retval);
1826
1827 idetape_create_read_position_cmd(&pc);
1828 return (idetape_queue_pc_tail(drive, &pc));
1829}
1830
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001831static void idetape_discard_read_pipeline(ide_drive_t *drive,
1832 int restore_position)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001833{
1834 idetape_tape_t *tape = drive->driver_data;
1835 int cnt;
1836 int seek, position;
1837
1838 cnt = __idetape_discard_read_pipeline(drive);
1839 if (restore_position) {
1840 position = idetape_read_position(drive);
1841 seek = position > cnt ? position - cnt : 0;
1842 if (idetape_position_tape(drive, seek, 0, 0)) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001843 printk(KERN_INFO "ide-tape: %s: position_tape failed in"
1844 " discard_pipeline()\n", tape->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001845 return;
1846 }
1847 }
1848}
1849
1850/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001851 * Generate a read/write request for the block device interface and wait for it
1852 * to be serviced.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001853 */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001854static int idetape_queue_rw_tail(ide_drive_t *drive, int cmd, int blocks,
1855 struct idetape_bh *bh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001856{
1857 idetape_tape_t *tape = drive->driver_data;
1858 struct request rq;
1859
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001860 debug_log(DBG_SENSE, "%s: cmd=%d\n", __func__, cmd);
1861
Borislav Petkovc4b22f82008-04-26 22:25:20 +02001862 if (test_bit(IDETAPE_FLAG_PIPELINE_ACTIVE, &tape->flags)) {
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001863 printk(KERN_ERR "ide-tape: bug: the pipeline is active in %s\n",
1864 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001865 return (0);
1866 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001867
1868 idetape_init_rq(&rq, cmd);
1869 rq.rq_disk = tape->disk;
1870 rq.special = (void *)bh;
Borislav Petkov54bb2072008-02-06 02:57:52 +01001871 rq.sector = tape->first_frame;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001872 rq.nr_sectors = blocks;
1873 rq.current_nr_sectors = blocks;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001874 (void) ide_do_drive_cmd(drive, &rq, ide_wait);
1875
1876 if ((cmd & (REQ_IDETAPE_READ | REQ_IDETAPE_WRITE)) == 0)
1877 return 0;
1878
1879 if (tape->merge_stage)
1880 idetape_init_merge_stage(tape);
1881 if (rq.errors == IDETAPE_ERROR_GENERAL)
1882 return -EIO;
Borislav Petkov54bb2072008-02-06 02:57:52 +01001883 return (tape->blk_size * (blocks-rq.current_nr_sectors));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001884}
1885
Borislav Petkov8d06bfa2008-02-06 02:57:53 +01001886/* start servicing the pipeline stages, starting from tape->next_stage. */
1887static void idetape_plug_pipeline(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001888{
1889 idetape_tape_t *tape = drive->driver_data;
1890
1891 if (tape->next_stage == NULL)
1892 return;
Borislav Petkovc4b22f82008-04-26 22:25:20 +02001893 if (!test_and_set_bit(IDETAPE_FLAG_PIPELINE_ACTIVE, &tape->flags)) {
Borislav Petkov419d4742008-02-02 19:56:51 +01001894 idetape_activate_next_stage(drive);
Borislav Petkov54bb2072008-02-06 02:57:52 +01001895 (void) ide_do_drive_cmd(drive, tape->active_data_rq, ide_end);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001896 }
1897}
1898
Borislav Petkovd236d742008-04-18 00:46:27 +02001899static void idetape_create_inquiry_cmd(struct ide_atapi_pc *pc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001900{
1901 idetape_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +01001902 pc->c[0] = INQUIRY;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001903 pc->c[4] = 254;
Borislav Petkovd236d742008-04-18 00:46:27 +02001904 pc->req_xfer = 254;
1905 pc->idetape_callback = &idetape_pc_callback;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001906}
1907
Borislav Petkovd236d742008-04-18 00:46:27 +02001908static void idetape_create_rewind_cmd(ide_drive_t *drive,
1909 struct ide_atapi_pc *pc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001910{
1911 idetape_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +01001912 pc->c[0] = REZERO_UNIT;
Borislav Petkov346331f2008-04-18 00:46:26 +02001913 pc->flags |= PC_FLAG_WAIT_FOR_DSC;
Borislav Petkovd236d742008-04-18 00:46:27 +02001914 pc->idetape_callback = &idetape_pc_callback;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001915}
1916
Borislav Petkovd236d742008-04-18 00:46:27 +02001917static void idetape_create_erase_cmd(struct ide_atapi_pc *pc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001918{
1919 idetape_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +01001920 pc->c[0] = ERASE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001921 pc->c[1] = 1;
Borislav Petkov346331f2008-04-18 00:46:26 +02001922 pc->flags |= PC_FLAG_WAIT_FOR_DSC;
Borislav Petkovd236d742008-04-18 00:46:27 +02001923 pc->idetape_callback = &idetape_pc_callback;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001924}
1925
Borislav Petkovd236d742008-04-18 00:46:27 +02001926static void idetape_create_space_cmd(struct ide_atapi_pc *pc, int count, u8 cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001927{
1928 idetape_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +01001929 pc->c[0] = SPACE;
Borislav Petkov860ff5e2008-02-02 19:56:50 +01001930 put_unaligned(cpu_to_be32(count), (unsigned int *) &pc->c[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001931 pc->c[1] = cmd;
Borislav Petkov346331f2008-04-18 00:46:26 +02001932 pc->flags |= PC_FLAG_WAIT_FOR_DSC;
Borislav Petkovd236d742008-04-18 00:46:27 +02001933 pc->idetape_callback = &idetape_pc_callback;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001934}
1935
Borislav Petkov97c566c2008-04-27 15:38:25 +02001936/* Queue up a character device originated write request. */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001937static int idetape_add_chrdev_write_request(ide_drive_t *drive, int blocks)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001938{
1939 idetape_tape_t *tape = drive->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001940 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001941
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001942 debug_log(DBG_CHRDEV, "Enter %s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001943
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001944 /* Attempt to allocate a new stage. Beware possible race conditions. */
Borislav Petkov97c566c2008-04-27 15:38:25 +02001945 while (1) {
Borislav Petkov54bb2072008-02-06 02:57:52 +01001946 spin_lock_irqsave(&tape->lock, flags);
Borislav Petkovc4b22f82008-04-26 22:25:20 +02001947 if (test_bit(IDETAPE_FLAG_PIPELINE_ACTIVE, &tape->flags)) {
Borislav Petkov54bb2072008-02-06 02:57:52 +01001948 idetape_wait_for_request(drive, tape->active_data_rq);
1949 spin_unlock_irqrestore(&tape->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001950 } else {
Borislav Petkov54bb2072008-02-06 02:57:52 +01001951 spin_unlock_irqrestore(&tape->lock, flags);
Borislav Petkov8d06bfa2008-02-06 02:57:53 +01001952 idetape_plug_pipeline(drive);
Borislav Petkovc4b22f82008-04-26 22:25:20 +02001953 if (test_bit(IDETAPE_FLAG_PIPELINE_ACTIVE,
1954 &tape->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001955 continue;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001956 return idetape_queue_rw_tail(drive, REQ_IDETAPE_WRITE,
1957 blocks, tape->merge_stage->bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001958 }
1959 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001960}
1961
1962/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001963 * Wait until all pending pipeline requests are serviced. Typically called on
1964 * device close.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001965 */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001966static void idetape_wait_for_pipeline(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001967{
1968 idetape_tape_t *tape = drive->driver_data;
1969 unsigned long flags;
1970
Borislav Petkovc4b22f82008-04-26 22:25:20 +02001971 while (tape->next_stage || test_bit(IDETAPE_FLAG_PIPELINE_ACTIVE,
1972 &tape->flags)) {
Borislav Petkov8d06bfa2008-02-06 02:57:53 +01001973 idetape_plug_pipeline(drive);
Borislav Petkov54bb2072008-02-06 02:57:52 +01001974 spin_lock_irqsave(&tape->lock, flags);
Borislav Petkovc4b22f82008-04-26 22:25:20 +02001975 if (test_bit(IDETAPE_FLAG_PIPELINE_ACTIVE, &tape->flags))
Borislav Petkov54bb2072008-02-06 02:57:52 +01001976 idetape_wait_for_request(drive, tape->active_data_rq);
1977 spin_unlock_irqrestore(&tape->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001978 }
1979}
1980
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001981static void idetape_empty_write_pipeline(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001982{
1983 idetape_tape_t *tape = drive->driver_data;
1984 int blocks, min;
1985 struct idetape_bh *bh;
Borislav Petkov55a5d292008-02-02 19:56:49 +01001986
Borislav Petkov54abf372008-02-06 02:57:52 +01001987 if (tape->chrdev_dir != IDETAPE_DIR_WRITE) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001988 printk(KERN_ERR "ide-tape: bug: Trying to empty write pipeline,"
1989 " but we are not writing.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001990 return;
1991 }
1992 if (tape->merge_stage_size > tape->stage_size) {
1993 printk(KERN_ERR "ide-tape: bug: merge_buffer too big\n");
1994 tape->merge_stage_size = tape->stage_size;
1995 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001996 if (tape->merge_stage_size) {
Borislav Petkov54bb2072008-02-06 02:57:52 +01001997 blocks = tape->merge_stage_size / tape->blk_size;
1998 if (tape->merge_stage_size % tape->blk_size) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001999 unsigned int i;
2000
2001 blocks++;
Borislav Petkov54bb2072008-02-06 02:57:52 +01002002 i = tape->blk_size - tape->merge_stage_size %
2003 tape->blk_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002004 bh = tape->bh->b_reqnext;
2005 while (bh) {
2006 atomic_set(&bh->b_count, 0);
2007 bh = bh->b_reqnext;
2008 }
2009 bh = tape->bh;
2010 while (i) {
2011 if (bh == NULL) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002012 printk(KERN_INFO "ide-tape: bug,"
2013 " bh NULL\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002014 break;
2015 }
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002016 min = min(i, (unsigned int)(bh->b_size -
2017 atomic_read(&bh->b_count)));
2018 memset(bh->b_data + atomic_read(&bh->b_count),
2019 0, min);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002020 atomic_add(min, &bh->b_count);
2021 i -= min;
2022 bh = bh->b_reqnext;
2023 }
2024 }
2025 (void) idetape_add_chrdev_write_request(drive, blocks);
2026 tape->merge_stage_size = 0;
2027 }
2028 idetape_wait_for_pipeline(drive);
2029 if (tape->merge_stage != NULL) {
2030 __idetape_kfree_stage(tape->merge_stage);
2031 tape->merge_stage = NULL;
2032 }
Borislav Petkov346331f2008-04-18 00:46:26 +02002033 clear_bit(IDETAPE_FLAG_PIPELINE_ERR, &tape->flags);
Borislav Petkov54abf372008-02-06 02:57:52 +01002034 tape->chrdev_dir = IDETAPE_DIR_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002035
2036 /*
Borislav Petkov3c98bf32008-02-06 02:57:53 +01002037 * On the next backup, perform the feedback loop again. (I don't want to
2038 * keep sense information between backups, as some systems are
2039 * constantly on, and the system load can be totally different on the
2040 * next backup).
Linus Torvalds1da177e2005-04-16 15:20:36 -07002041 */
2042 tape->max_stages = tape->min_pipeline;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002043 if (tape->first_stage != NULL ||
2044 tape->next_stage != NULL ||
2045 tape->last_stage != NULL ||
2046 tape->nr_stages != 0) {
2047 printk(KERN_ERR "ide-tape: ide-tape pipeline bug, "
2048 "first_stage %p, next_stage %p, "
2049 "last_stage %p, nr_stages %d\n",
2050 tape->first_stage, tape->next_stage,
2051 tape->last_stage, tape->nr_stages);
2052 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002053}
2054
Borislav Petkov8d06bfa2008-02-06 02:57:53 +01002055static int idetape_init_read(ide_drive_t *drive, int max_stages)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002056{
2057 idetape_tape_t *tape = drive->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002058 int bytes_read;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002059
2060 /* Initialize read operation */
Borislav Petkov54abf372008-02-06 02:57:52 +01002061 if (tape->chrdev_dir != IDETAPE_DIR_READ) {
2062 if (tape->chrdev_dir == IDETAPE_DIR_WRITE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002063 idetape_empty_write_pipeline(drive);
2064 idetape_flush_tape_buffers(drive);
2065 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002066 if (tape->merge_stage || tape->merge_stage_size) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002067 printk(KERN_ERR "ide-tape: merge_stage_size should be"
2068 " 0 now\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002069 tape->merge_stage_size = 0;
2070 }
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002071 tape->merge_stage = __idetape_kmalloc_stage(tape, 0, 0);
2072 if (!tape->merge_stage)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002073 return -ENOMEM;
Borislav Petkov54abf372008-02-06 02:57:52 +01002074 tape->chrdev_dir = IDETAPE_DIR_READ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002075
2076 /*
Borislav Petkov3c98bf32008-02-06 02:57:53 +01002077 * Issue a read 0 command to ensure that DSC handshake is
2078 * switched from completion mode to buffer available mode.
2079 * No point in issuing this if DSC overlap isn't supported, some
2080 * drives (Seagate STT3401A) will return an error.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002081 */
2082 if (drive->dsc_overlap) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002083 bytes_read = idetape_queue_rw_tail(drive,
2084 REQ_IDETAPE_READ, 0,
2085 tape->merge_stage->bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002086 if (bytes_read < 0) {
2087 __idetape_kfree_stage(tape->merge_stage);
2088 tape->merge_stage = NULL;
Borislav Petkov54abf372008-02-06 02:57:52 +01002089 tape->chrdev_dir = IDETAPE_DIR_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002090 return bytes_read;
2091 }
2092 }
2093 }
Borislav Petkov5e69bd92008-04-27 15:38:25 +02002094
Borislav Petkovc4b22f82008-04-26 22:25:20 +02002095 if (!test_bit(IDETAPE_FLAG_PIPELINE_ACTIVE, &tape->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002096 if (tape->nr_pending_stages >= 3 * max_stages / 4) {
2097 tape->measure_insert_time = 1;
2098 tape->insert_time = jiffies;
2099 tape->insert_size = 0;
2100 tape->insert_speed = 0;
Borislav Petkov8d06bfa2008-02-06 02:57:53 +01002101 idetape_plug_pipeline(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002102 }
2103 }
2104 return 0;
2105}
2106
2107/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +01002108 * Called from idetape_chrdev_read() to service a character device read request
2109 * and add read-ahead requests to our pipeline.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002110 */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002111static int idetape_add_chrdev_read_request(ide_drive_t *drive, int blocks)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002112{
2113 idetape_tape_t *tape = drive->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002114
Borislav Petkov8004a8c2008-02-06 02:57:51 +01002115 debug_log(DBG_PROCS, "Enter %s, %d blocks\n", __func__, blocks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002116
Borislav Petkov3c98bf32008-02-06 02:57:53 +01002117 /* If we are at a filemark, return a read length of 0 */
Borislav Petkov346331f2008-04-18 00:46:26 +02002118 if (test_bit(IDETAPE_FLAG_FILEMARK, &tape->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002119 return 0;
2120
Borislav Petkov8d06bfa2008-02-06 02:57:53 +01002121 idetape_init_read(drive, tape->max_stages);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002122
Borislav Petkov5e69bd92008-04-27 15:38:25 +02002123 if (test_bit(IDETAPE_FLAG_PIPELINE_ERR, &tape->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002124 return 0;
Borislav Petkov5e69bd92008-04-27 15:38:25 +02002125
2126 return idetape_queue_rw_tail(drive, REQ_IDETAPE_READ, blocks,
2127 tape->merge_stage->bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002128}
2129
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002130static void idetape_pad_zeros(ide_drive_t *drive, int bcount)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002131{
2132 idetape_tape_t *tape = drive->driver_data;
2133 struct idetape_bh *bh;
2134 int blocks;
Borislav Petkov3c98bf32008-02-06 02:57:53 +01002135
Linus Torvalds1da177e2005-04-16 15:20:36 -07002136 while (bcount) {
2137 unsigned int count;
2138
2139 bh = tape->merge_stage->bh;
2140 count = min(tape->stage_size, bcount);
2141 bcount -= count;
Borislav Petkov54bb2072008-02-06 02:57:52 +01002142 blocks = count / tape->blk_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002143 while (count) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002144 atomic_set(&bh->b_count,
2145 min(count, (unsigned int)bh->b_size));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002146 memset(bh->b_data, 0, atomic_read(&bh->b_count));
2147 count -= atomic_read(&bh->b_count);
2148 bh = bh->b_reqnext;
2149 }
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002150 idetape_queue_rw_tail(drive, REQ_IDETAPE_WRITE, blocks,
2151 tape->merge_stage->bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002152 }
2153}
2154
Linus Torvalds1da177e2005-04-16 15:20:36 -07002155/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +01002156 * Rewinds the tape to the Beginning Of the current Partition (BOP). We
2157 * currently support only one partition.
2158 */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002159static int idetape_rewind_tape(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002160{
2161 int retval;
Borislav Petkovd236d742008-04-18 00:46:27 +02002162 struct ide_atapi_pc pc;
Borislav Petkov8004a8c2008-02-06 02:57:51 +01002163 idetape_tape_t *tape;
2164 tape = drive->driver_data;
2165
2166 debug_log(DBG_SENSE, "Enter %s\n", __func__);
2167
Linus Torvalds1da177e2005-04-16 15:20:36 -07002168 idetape_create_rewind_cmd(drive, &pc);
2169 retval = idetape_queue_pc_tail(drive, &pc);
2170 if (retval)
2171 return retval;
2172
2173 idetape_create_read_position_cmd(&pc);
2174 retval = idetape_queue_pc_tail(drive, &pc);
2175 if (retval)
2176 return retval;
2177 return 0;
2178}
2179
Borislav Petkov3c98bf32008-02-06 02:57:53 +01002180/* mtio.h compatible commands should be issued to the chrdev interface. */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002181static int idetape_blkdev_ioctl(ide_drive_t *drive, unsigned int cmd,
2182 unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002183{
2184 idetape_tape_t *tape = drive->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002185 void __user *argp = (void __user *)arg;
2186
Borislav Petkovd59823f2008-02-02 19:56:51 +01002187 struct idetape_config {
2188 int dsc_rw_frequency;
2189 int dsc_media_access_frequency;
2190 int nr_stages;
2191 } config;
2192
Borislav Petkov8004a8c2008-02-06 02:57:51 +01002193 debug_log(DBG_PROCS, "Enter %s\n", __func__);
2194
Linus Torvalds1da177e2005-04-16 15:20:36 -07002195 switch (cmd) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002196 case 0x0340:
2197 if (copy_from_user(&config, argp, sizeof(config)))
2198 return -EFAULT;
2199 tape->best_dsc_rw_freq = config.dsc_rw_frequency;
2200 tape->max_stages = config.nr_stages;
2201 break;
2202 case 0x0350:
2203 config.dsc_rw_frequency = (int) tape->best_dsc_rw_freq;
2204 config.nr_stages = tape->max_stages;
2205 if (copy_to_user(argp, &config, sizeof(config)))
2206 return -EFAULT;
2207 break;
2208 default:
2209 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002210 }
2211 return 0;
2212}
2213
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002214static int idetape_space_over_filemarks(ide_drive_t *drive, short mt_op,
2215 int mt_count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002216{
2217 idetape_tape_t *tape = drive->driver_data;
Borislav Petkovd236d742008-04-18 00:46:27 +02002218 struct ide_atapi_pc pc;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002219 int retval, count = 0;
Borislav Petkovb6422012008-02-02 19:56:49 +01002220 int sprev = !!(tape->caps[4] & 0x20);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002221
2222 if (mt_count == 0)
2223 return 0;
2224 if (MTBSF == mt_op || MTBSFM == mt_op) {
Borislav Petkovb6422012008-02-02 19:56:49 +01002225 if (!sprev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002226 return -EIO;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002227 mt_count = -mt_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002228 }
2229
Borislav Petkov54abf372008-02-06 02:57:52 +01002230 if (tape->chrdev_dir == IDETAPE_DIR_READ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002231 tape->merge_stage_size = 0;
Borislav Petkov346331f2008-04-18 00:46:26 +02002232 if (test_and_clear_bit(IDETAPE_FLAG_FILEMARK, &tape->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002233 ++count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002234 idetape_discard_read_pipeline(drive, 0);
2235 }
2236
2237 /*
Borislav Petkov3c98bf32008-02-06 02:57:53 +01002238 * The filemark was not found in our internal pipeline; now we can issue
2239 * the space command.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002240 */
2241 switch (mt_op) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002242 case MTFSF:
2243 case MTBSF:
2244 idetape_create_space_cmd(&pc, mt_count - count,
2245 IDETAPE_SPACE_OVER_FILEMARK);
2246 return idetape_queue_pc_tail(drive, &pc);
2247 case MTFSFM:
2248 case MTBSFM:
2249 if (!sprev)
2250 return -EIO;
2251 retval = idetape_space_over_filemarks(drive, MTFSF,
2252 mt_count - count);
2253 if (retval)
2254 return retval;
2255 count = (MTBSFM == mt_op ? 1 : -1);
2256 return idetape_space_over_filemarks(drive, MTFSF, count);
2257 default:
2258 printk(KERN_ERR "ide-tape: MTIO operation %d not supported\n",
2259 mt_op);
2260 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002261 }
2262}
2263
Linus Torvalds1da177e2005-04-16 15:20:36 -07002264/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +01002265 * Our character device read / write functions.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002266 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +01002267 * The tape is optimized to maximize throughput when it is transferring an
2268 * integral number of the "continuous transfer limit", which is a parameter of
2269 * the specific tape (26kB on my particular tape, 32kB for Onstream).
Linus Torvalds1da177e2005-04-16 15:20:36 -07002270 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +01002271 * As of version 1.3 of the driver, the character device provides an abstract
2272 * continuous view of the media - any mix of block sizes (even 1 byte) on the
2273 * same backup/restore procedure is supported. The driver will internally
2274 * convert the requests to the recommended transfer unit, so that an unmatch
2275 * between the user's block size to the recommended size will only result in a
2276 * (slightly) increased driver overhead, but will no longer hit performance.
2277 * This is not applicable to Onstream.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002278 */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002279static ssize_t idetape_chrdev_read(struct file *file, char __user *buf,
2280 size_t count, loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002281{
2282 struct ide_tape_obj *tape = ide_tape_f(file);
2283 ide_drive_t *drive = tape->drive;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002284 ssize_t bytes_read, temp, actually_read = 0, rc;
Daniel Walkerdcd96372006-06-25 05:47:37 -07002285 ssize_t ret = 0;
Borislav Petkovb6422012008-02-02 19:56:49 +01002286 u16 ctl = *(u16 *)&tape->caps[12];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002287
Borislav Petkov8004a8c2008-02-06 02:57:51 +01002288 debug_log(DBG_CHRDEV, "Enter %s, count %Zd\n", __func__, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002289
Borislav Petkov54abf372008-02-06 02:57:52 +01002290 if (tape->chrdev_dir != IDETAPE_DIR_READ) {
Borislav Petkov346331f2008-04-18 00:46:26 +02002291 if (test_bit(IDETAPE_FLAG_DETECT_BS, &tape->flags))
Borislav Petkov54bb2072008-02-06 02:57:52 +01002292 if (count > tape->blk_size &&
2293 (count % tape->blk_size) == 0)
2294 tape->user_bs_factor = count / tape->blk_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002295 }
Borislav Petkov8d06bfa2008-02-06 02:57:53 +01002296 rc = idetape_init_read(drive, tape->max_stages);
2297 if (rc < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002298 return rc;
2299 if (count == 0)
2300 return (0);
2301 if (tape->merge_stage_size) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002302 actually_read = min((unsigned int)(tape->merge_stage_size),
2303 (unsigned int)count);
Borislav Petkov99d74e62008-04-27 15:38:25 +02002304 if (idetape_copy_stage_to_user(tape, buf, actually_read))
Daniel Walkerdcd96372006-06-25 05:47:37 -07002305 ret = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002306 buf += actually_read;
2307 tape->merge_stage_size -= actually_read;
2308 count -= actually_read;
2309 }
2310 while (count >= tape->stage_size) {
Borislav Petkovb6422012008-02-02 19:56:49 +01002311 bytes_read = idetape_add_chrdev_read_request(drive, ctl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002312 if (bytes_read <= 0)
2313 goto finish;
Borislav Petkov99d74e62008-04-27 15:38:25 +02002314 if (idetape_copy_stage_to_user(tape, buf, bytes_read))
Daniel Walkerdcd96372006-06-25 05:47:37 -07002315 ret = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002316 buf += bytes_read;
2317 count -= bytes_read;
2318 actually_read += bytes_read;
2319 }
2320 if (count) {
Borislav Petkovb6422012008-02-02 19:56:49 +01002321 bytes_read = idetape_add_chrdev_read_request(drive, ctl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002322 if (bytes_read <= 0)
2323 goto finish;
2324 temp = min((unsigned long)count, (unsigned long)bytes_read);
Borislav Petkov99d74e62008-04-27 15:38:25 +02002325 if (idetape_copy_stage_to_user(tape, buf, temp))
Daniel Walkerdcd96372006-06-25 05:47:37 -07002326 ret = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002327 actually_read += temp;
2328 tape->merge_stage_size = bytes_read-temp;
2329 }
2330finish:
Borislav Petkov346331f2008-04-18 00:46:26 +02002331 if (!actually_read && test_bit(IDETAPE_FLAG_FILEMARK, &tape->flags)) {
Borislav Petkov8004a8c2008-02-06 02:57:51 +01002332 debug_log(DBG_SENSE, "%s: spacing over filemark\n", tape->name);
2333
Linus Torvalds1da177e2005-04-16 15:20:36 -07002334 idetape_space_over_filemarks(drive, MTFSF, 1);
2335 return 0;
2336 }
Daniel Walkerdcd96372006-06-25 05:47:37 -07002337
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002338 return ret ? ret : actually_read;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002339}
2340
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002341static ssize_t idetape_chrdev_write(struct file *file, const char __user *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002342 size_t count, loff_t *ppos)
2343{
2344 struct ide_tape_obj *tape = ide_tape_f(file);
2345 ide_drive_t *drive = tape->drive;
Daniel Walkerdcd96372006-06-25 05:47:37 -07002346 ssize_t actually_written = 0;
2347 ssize_t ret = 0;
Borislav Petkovb6422012008-02-02 19:56:49 +01002348 u16 ctl = *(u16 *)&tape->caps[12];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002349
2350 /* The drive is write protected. */
2351 if (tape->write_prot)
2352 return -EACCES;
2353
Borislav Petkov8004a8c2008-02-06 02:57:51 +01002354 debug_log(DBG_CHRDEV, "Enter %s, count %Zd\n", __func__, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002355
2356 /* Initialize write operation */
Borislav Petkov54abf372008-02-06 02:57:52 +01002357 if (tape->chrdev_dir != IDETAPE_DIR_WRITE) {
2358 if (tape->chrdev_dir == IDETAPE_DIR_READ)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002359 idetape_discard_read_pipeline(drive, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002360 if (tape->merge_stage || tape->merge_stage_size) {
2361 printk(KERN_ERR "ide-tape: merge_stage_size "
2362 "should be 0 now\n");
2363 tape->merge_stage_size = 0;
2364 }
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002365 tape->merge_stage = __idetape_kmalloc_stage(tape, 0, 0);
2366 if (!tape->merge_stage)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002367 return -ENOMEM;
Borislav Petkov54abf372008-02-06 02:57:52 +01002368 tape->chrdev_dir = IDETAPE_DIR_WRITE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002369 idetape_init_merge_stage(tape);
2370
2371 /*
Borislav Petkov3c98bf32008-02-06 02:57:53 +01002372 * Issue a write 0 command to ensure that DSC handshake is
2373 * switched from completion mode to buffer available mode. No
2374 * point in issuing this if DSC overlap isn't supported, some
2375 * drives (Seagate STT3401A) will return an error.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002376 */
2377 if (drive->dsc_overlap) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002378 ssize_t retval = idetape_queue_rw_tail(drive,
2379 REQ_IDETAPE_WRITE, 0,
2380 tape->merge_stage->bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002381 if (retval < 0) {
2382 __idetape_kfree_stage(tape->merge_stage);
2383 tape->merge_stage = NULL;
Borislav Petkov54abf372008-02-06 02:57:52 +01002384 tape->chrdev_dir = IDETAPE_DIR_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002385 return retval;
2386 }
2387 }
2388 }
2389 if (count == 0)
2390 return (0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002391 if (tape->merge_stage_size) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002392 if (tape->merge_stage_size >= tape->stage_size) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002393 printk(KERN_ERR "ide-tape: bug: merge buf too big\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002394 tape->merge_stage_size = 0;
2395 }
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002396 actually_written = min((unsigned int)
2397 (tape->stage_size - tape->merge_stage_size),
2398 (unsigned int)count);
Borislav Petkov8646c882008-04-27 15:38:26 +02002399 if (idetape_copy_stage_from_user(tape, buf, actually_written))
Daniel Walkerdcd96372006-06-25 05:47:37 -07002400 ret = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002401 buf += actually_written;
2402 tape->merge_stage_size += actually_written;
2403 count -= actually_written;
2404
2405 if (tape->merge_stage_size == tape->stage_size) {
Daniel Walkerdcd96372006-06-25 05:47:37 -07002406 ssize_t retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002407 tape->merge_stage_size = 0;
Borislav Petkovb6422012008-02-02 19:56:49 +01002408 retval = idetape_add_chrdev_write_request(drive, ctl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002409 if (retval <= 0)
2410 return (retval);
2411 }
2412 }
2413 while (count >= tape->stage_size) {
Daniel Walkerdcd96372006-06-25 05:47:37 -07002414 ssize_t retval;
Borislav Petkov8646c882008-04-27 15:38:26 +02002415 if (idetape_copy_stage_from_user(tape, buf, tape->stage_size))
Daniel Walkerdcd96372006-06-25 05:47:37 -07002416 ret = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002417 buf += tape->stage_size;
2418 count -= tape->stage_size;
Borislav Petkovb6422012008-02-02 19:56:49 +01002419 retval = idetape_add_chrdev_write_request(drive, ctl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002420 actually_written += tape->stage_size;
2421 if (retval <= 0)
2422 return (retval);
2423 }
2424 if (count) {
2425 actually_written += count;
Borislav Petkov8646c882008-04-27 15:38:26 +02002426 if (idetape_copy_stage_from_user(tape, buf, count))
Daniel Walkerdcd96372006-06-25 05:47:37 -07002427 ret = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002428 tape->merge_stage_size += count;
2429 }
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002430 return ret ? ret : actually_written;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002431}
2432
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002433static int idetape_write_filemark(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002434{
Borislav Petkovd236d742008-04-18 00:46:27 +02002435 struct ide_atapi_pc pc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002436
2437 /* Write a filemark */
2438 idetape_create_write_filemark_cmd(drive, &pc, 1);
2439 if (idetape_queue_pc_tail(drive, &pc)) {
2440 printk(KERN_ERR "ide-tape: Couldn't write a filemark\n");
2441 return -EIO;
2442 }
2443 return 0;
2444}
2445
2446/*
Borislav Petkovd99c9da2008-02-02 19:56:51 +01002447 * Called from idetape_chrdev_ioctl when the general mtio MTIOCTOP ioctl is
2448 * requested.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002449 *
Borislav Petkovd99c9da2008-02-02 19:56:51 +01002450 * Note: MTBSF and MTBSFM are not supported when the tape doesn't support
2451 * spacing over filemarks in the reverse direction. In this case, MTFSFM is also
2452 * usually not supported (it is supported in the rare case in which we crossed
2453 * the filemark during our read-ahead pipelined operation mode).
Linus Torvalds1da177e2005-04-16 15:20:36 -07002454 *
Borislav Petkovd99c9da2008-02-02 19:56:51 +01002455 * The following commands are currently not supported:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002456 *
Borislav Petkovd99c9da2008-02-02 19:56:51 +01002457 * MTFSS, MTBSS, MTWSM, MTSETDENSITY, MTSETDRVBUFFER, MT_ST_BOOLEANS,
2458 * MT_ST_WRITE_THRESHOLD.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002459 */
Borislav Petkovd99c9da2008-02-02 19:56:51 +01002460static int idetape_mtioctop(ide_drive_t *drive, short mt_op, int mt_count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002461{
2462 idetape_tape_t *tape = drive->driver_data;
Borislav Petkovd236d742008-04-18 00:46:27 +02002463 struct ide_atapi_pc pc;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002464 int i, retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002465
Borislav Petkov8004a8c2008-02-06 02:57:51 +01002466 debug_log(DBG_ERR, "Handling MTIOCTOP ioctl: mt_op=%d, mt_count=%d\n",
2467 mt_op, mt_count);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002468
Borislav Petkov3c98bf32008-02-06 02:57:53 +01002469 /* Commands which need our pipelined read-ahead stages. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002470 switch (mt_op) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002471 case MTFSF:
2472 case MTFSFM:
2473 case MTBSF:
2474 case MTBSFM:
2475 if (!mt_count)
2476 return 0;
2477 return idetape_space_over_filemarks(drive, mt_op, mt_count);
2478 default:
2479 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002480 }
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002481
Linus Torvalds1da177e2005-04-16 15:20:36 -07002482 switch (mt_op) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002483 case MTWEOF:
2484 if (tape->write_prot)
2485 return -EACCES;
2486 idetape_discard_read_pipeline(drive, 1);
2487 for (i = 0; i < mt_count; i++) {
2488 retval = idetape_write_filemark(drive);
2489 if (retval)
2490 return retval;
2491 }
2492 return 0;
2493 case MTREW:
2494 idetape_discard_read_pipeline(drive, 0);
2495 if (idetape_rewind_tape(drive))
2496 return -EIO;
2497 return 0;
2498 case MTLOAD:
2499 idetape_discard_read_pipeline(drive, 0);
2500 idetape_create_load_unload_cmd(drive, &pc,
2501 IDETAPE_LU_LOAD_MASK);
2502 return idetape_queue_pc_tail(drive, &pc);
2503 case MTUNLOAD:
2504 case MTOFFL:
2505 /*
2506 * If door is locked, attempt to unlock before
2507 * attempting to eject.
2508 */
2509 if (tape->door_locked) {
2510 if (idetape_create_prevent_cmd(drive, &pc, 0))
2511 if (!idetape_queue_pc_tail(drive, &pc))
2512 tape->door_locked = DOOR_UNLOCKED;
2513 }
2514 idetape_discard_read_pipeline(drive, 0);
2515 idetape_create_load_unload_cmd(drive, &pc,
2516 !IDETAPE_LU_LOAD_MASK);
2517 retval = idetape_queue_pc_tail(drive, &pc);
2518 if (!retval)
Borislav Petkov346331f2008-04-18 00:46:26 +02002519 clear_bit(IDETAPE_FLAG_MEDIUM_PRESENT, &tape->flags);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002520 return retval;
2521 case MTNOP:
2522 idetape_discard_read_pipeline(drive, 0);
2523 return idetape_flush_tape_buffers(drive);
2524 case MTRETEN:
2525 idetape_discard_read_pipeline(drive, 0);
2526 idetape_create_load_unload_cmd(drive, &pc,
2527 IDETAPE_LU_RETENSION_MASK | IDETAPE_LU_LOAD_MASK);
2528 return idetape_queue_pc_tail(drive, &pc);
2529 case MTEOM:
2530 idetape_create_space_cmd(&pc, 0, IDETAPE_SPACE_TO_EOD);
2531 return idetape_queue_pc_tail(drive, &pc);
2532 case MTERASE:
2533 (void)idetape_rewind_tape(drive);
2534 idetape_create_erase_cmd(&pc);
2535 return idetape_queue_pc_tail(drive, &pc);
2536 case MTSETBLK:
2537 if (mt_count) {
2538 if (mt_count < tape->blk_size ||
2539 mt_count % tape->blk_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002540 return -EIO;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002541 tape->user_bs_factor = mt_count / tape->blk_size;
Borislav Petkov346331f2008-04-18 00:46:26 +02002542 clear_bit(IDETAPE_FLAG_DETECT_BS, &tape->flags);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002543 } else
Borislav Petkov346331f2008-04-18 00:46:26 +02002544 set_bit(IDETAPE_FLAG_DETECT_BS, &tape->flags);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002545 return 0;
2546 case MTSEEK:
2547 idetape_discard_read_pipeline(drive, 0);
2548 return idetape_position_tape(drive,
2549 mt_count * tape->user_bs_factor, tape->partition, 0);
2550 case MTSETPART:
2551 idetape_discard_read_pipeline(drive, 0);
2552 return idetape_position_tape(drive, 0, mt_count, 0);
2553 case MTFSR:
2554 case MTBSR:
2555 case MTLOCK:
2556 if (!idetape_create_prevent_cmd(drive, &pc, 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002557 return 0;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002558 retval = idetape_queue_pc_tail(drive, &pc);
2559 if (retval)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002560 return retval;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002561 tape->door_locked = DOOR_EXPLICITLY_LOCKED;
2562 return 0;
2563 case MTUNLOCK:
2564 if (!idetape_create_prevent_cmd(drive, &pc, 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002565 return 0;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002566 retval = idetape_queue_pc_tail(drive, &pc);
2567 if (retval)
2568 return retval;
2569 tape->door_locked = DOOR_UNLOCKED;
2570 return 0;
2571 default:
2572 printk(KERN_ERR "ide-tape: MTIO operation %d not supported\n",
2573 mt_op);
2574 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002575 }
2576}
2577
2578/*
Borislav Petkovd99c9da2008-02-02 19:56:51 +01002579 * Our character device ioctls. General mtio.h magnetic io commands are
2580 * supported here, and not in the corresponding block interface. Our own
2581 * ide-tape ioctls are supported on both interfaces.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002582 */
Borislav Petkovd99c9da2008-02-02 19:56:51 +01002583static int idetape_chrdev_ioctl(struct inode *inode, struct file *file,
2584 unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002585{
2586 struct ide_tape_obj *tape = ide_tape_f(file);
2587 ide_drive_t *drive = tape->drive;
2588 struct mtop mtop;
2589 struct mtget mtget;
2590 struct mtpos mtpos;
Borislav Petkov54bb2072008-02-06 02:57:52 +01002591 int block_offset = 0, position = tape->first_frame;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002592 void __user *argp = (void __user *)arg;
2593
Borislav Petkov8004a8c2008-02-06 02:57:51 +01002594 debug_log(DBG_CHRDEV, "Enter %s, cmd=%u\n", __func__, cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002595
Borislav Petkov54abf372008-02-06 02:57:52 +01002596 if (tape->chrdev_dir == IDETAPE_DIR_WRITE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002597 idetape_empty_write_pipeline(drive);
2598 idetape_flush_tape_buffers(drive);
2599 }
2600 if (cmd == MTIOCGET || cmd == MTIOCPOS) {
Borislav Petkovb361acb2008-04-27 15:38:26 +02002601 idetape_wait_for_pipeline(drive);
2602 block_offset = tape->merge_stage_size /
Borislav Petkov54bb2072008-02-06 02:57:52 +01002603 (tape->blk_size * tape->user_bs_factor);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002604 position = idetape_read_position(drive);
2605 if (position < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002606 return -EIO;
2607 }
2608 switch (cmd) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002609 case MTIOCTOP:
2610 if (copy_from_user(&mtop, argp, sizeof(struct mtop)))
2611 return -EFAULT;
2612 return idetape_mtioctop(drive, mtop.mt_op, mtop.mt_count);
2613 case MTIOCGET:
2614 memset(&mtget, 0, sizeof(struct mtget));
2615 mtget.mt_type = MT_ISSCSI2;
2616 mtget.mt_blkno = position / tape->user_bs_factor - block_offset;
2617 mtget.mt_dsreg =
2618 ((tape->blk_size * tape->user_bs_factor)
2619 << MT_ST_BLKSIZE_SHIFT) & MT_ST_BLKSIZE_MASK;
Borislav Petkov54bb2072008-02-06 02:57:52 +01002620
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002621 if (tape->drv_write_prot)
2622 mtget.mt_gstat |= GMT_WR_PROT(0xffffffff);
2623
2624 if (copy_to_user(argp, &mtget, sizeof(struct mtget)))
2625 return -EFAULT;
2626 return 0;
2627 case MTIOCPOS:
2628 mtpos.mt_blkno = position / tape->user_bs_factor - block_offset;
2629 if (copy_to_user(argp, &mtpos, sizeof(struct mtpos)))
2630 return -EFAULT;
2631 return 0;
2632 default:
2633 if (tape->chrdev_dir == IDETAPE_DIR_READ)
2634 idetape_discard_read_pipeline(drive, 1);
2635 return idetape_blkdev_ioctl(drive, cmd, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002636 }
2637}
2638
Borislav Petkov3cffb9c2008-02-02 19:56:50 +01002639/*
2640 * Do a mode sense page 0 with block descriptor and if it succeeds set the tape
2641 * block size with the reported value.
2642 */
2643static void ide_tape_get_bsize_from_bdesc(ide_drive_t *drive)
2644{
2645 idetape_tape_t *tape = drive->driver_data;
Borislav Petkovd236d742008-04-18 00:46:27 +02002646 struct ide_atapi_pc pc;
Borislav Petkov3cffb9c2008-02-02 19:56:50 +01002647
2648 idetape_create_mode_sense_cmd(&pc, IDETAPE_BLOCK_DESCRIPTOR);
2649 if (idetape_queue_pc_tail(drive, &pc)) {
2650 printk(KERN_ERR "ide-tape: Can't get block descriptor\n");
Borislav Petkov54bb2072008-02-06 02:57:52 +01002651 if (tape->blk_size == 0) {
Borislav Petkov3cffb9c2008-02-02 19:56:50 +01002652 printk(KERN_WARNING "ide-tape: Cannot deal with zero "
2653 "block size, assuming 32k\n");
Borislav Petkov54bb2072008-02-06 02:57:52 +01002654 tape->blk_size = 32768;
Borislav Petkov3cffb9c2008-02-02 19:56:50 +01002655 }
2656 return;
2657 }
Borislav Petkovd236d742008-04-18 00:46:27 +02002658 tape->blk_size = (pc.buf[4 + 5] << 16) +
2659 (pc.buf[4 + 6] << 8) +
2660 pc.buf[4 + 7];
2661 tape->drv_write_prot = (pc.buf[2] & 0x80) >> 7;
Borislav Petkov3cffb9c2008-02-02 19:56:50 +01002662}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002663
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002664static int idetape_chrdev_open(struct inode *inode, struct file *filp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002665{
2666 unsigned int minor = iminor(inode), i = minor & ~0xc0;
2667 ide_drive_t *drive;
2668 idetape_tape_t *tape;
Borislav Petkovd236d742008-04-18 00:46:27 +02002669 struct ide_atapi_pc pc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002670 int retval;
2671
Borislav Petkov8004a8c2008-02-06 02:57:51 +01002672 if (i >= MAX_HWIFS * MAX_DRIVES)
2673 return -ENXIO;
2674
2675 tape = ide_tape_chrdev_get(i);
2676 if (!tape)
2677 return -ENXIO;
2678
2679 debug_log(DBG_CHRDEV, "Enter %s\n", __func__);
2680
Linus Torvalds1da177e2005-04-16 15:20:36 -07002681 /*
2682 * We really want to do nonseekable_open(inode, filp); here, but some
2683 * versions of tar incorrectly call lseek on tapes and bail out if that
2684 * fails. So we disallow pread() and pwrite(), but permit lseeks.
2685 */
2686 filp->f_mode &= ~(FMODE_PREAD | FMODE_PWRITE);
2687
Linus Torvalds1da177e2005-04-16 15:20:36 -07002688 drive = tape->drive;
2689
2690 filp->private_data = tape;
2691
Borislav Petkov346331f2008-04-18 00:46:26 +02002692 if (test_and_set_bit(IDETAPE_FLAG_BUSY, &tape->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002693 retval = -EBUSY;
2694 goto out_put_tape;
2695 }
2696
2697 retval = idetape_wait_ready(drive, 60 * HZ);
2698 if (retval) {
Borislav Petkov346331f2008-04-18 00:46:26 +02002699 clear_bit(IDETAPE_FLAG_BUSY, &tape->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002700 printk(KERN_ERR "ide-tape: %s: drive not ready\n", tape->name);
2701 goto out_put_tape;
2702 }
2703
2704 idetape_read_position(drive);
Borislav Petkov346331f2008-04-18 00:46:26 +02002705 if (!test_bit(IDETAPE_FLAG_ADDRESS_VALID, &tape->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002706 (void)idetape_rewind_tape(drive);
2707
Borislav Petkov54abf372008-02-06 02:57:52 +01002708 if (tape->chrdev_dir != IDETAPE_DIR_READ)
Borislav Petkov346331f2008-04-18 00:46:26 +02002709 clear_bit(IDETAPE_FLAG_PIPELINE_ERR, &tape->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002710
2711 /* Read block size and write protect status from drive. */
Borislav Petkov3cffb9c2008-02-02 19:56:50 +01002712 ide_tape_get_bsize_from_bdesc(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002713
2714 /* Set write protect flag if device is opened as read-only. */
2715 if ((filp->f_flags & O_ACCMODE) == O_RDONLY)
2716 tape->write_prot = 1;
2717 else
2718 tape->write_prot = tape->drv_write_prot;
2719
2720 /* Make sure drive isn't write protected if user wants to write. */
2721 if (tape->write_prot) {
2722 if ((filp->f_flags & O_ACCMODE) == O_WRONLY ||
2723 (filp->f_flags & O_ACCMODE) == O_RDWR) {
Borislav Petkov346331f2008-04-18 00:46:26 +02002724 clear_bit(IDETAPE_FLAG_BUSY, &tape->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002725 retval = -EROFS;
2726 goto out_put_tape;
2727 }
2728 }
2729
Borislav Petkov3c98bf32008-02-06 02:57:53 +01002730 /* Lock the tape drive door so user can't eject. */
Borislav Petkov54abf372008-02-06 02:57:52 +01002731 if (tape->chrdev_dir == IDETAPE_DIR_NONE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002732 if (idetape_create_prevent_cmd(drive, &pc, 1)) {
2733 if (!idetape_queue_pc_tail(drive, &pc)) {
2734 if (tape->door_locked != DOOR_EXPLICITLY_LOCKED)
2735 tape->door_locked = DOOR_LOCKED;
2736 }
2737 }
2738 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002739 return 0;
2740
2741out_put_tape:
2742 ide_tape_put(tape);
2743 return retval;
2744}
2745
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002746static void idetape_write_release(ide_drive_t *drive, unsigned int minor)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002747{
2748 idetape_tape_t *tape = drive->driver_data;
2749
2750 idetape_empty_write_pipeline(drive);
2751 tape->merge_stage = __idetape_kmalloc_stage(tape, 1, 0);
2752 if (tape->merge_stage != NULL) {
Borislav Petkov54bb2072008-02-06 02:57:52 +01002753 idetape_pad_zeros(drive, tape->blk_size *
2754 (tape->user_bs_factor - 1));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002755 __idetape_kfree_stage(tape->merge_stage);
2756 tape->merge_stage = NULL;
2757 }
2758 idetape_write_filemark(drive);
2759 idetape_flush_tape_buffers(drive);
2760 idetape_flush_tape_buffers(drive);
2761}
2762
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002763static int idetape_chrdev_release(struct inode *inode, struct file *filp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002764{
2765 struct ide_tape_obj *tape = ide_tape_f(filp);
2766 ide_drive_t *drive = tape->drive;
Borislav Petkovd236d742008-04-18 00:46:27 +02002767 struct ide_atapi_pc pc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002768 unsigned int minor = iminor(inode);
2769
2770 lock_kernel();
2771 tape = drive->driver_data;
Borislav Petkov8004a8c2008-02-06 02:57:51 +01002772
2773 debug_log(DBG_CHRDEV, "Enter %s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002774
Borislav Petkov54abf372008-02-06 02:57:52 +01002775 if (tape->chrdev_dir == IDETAPE_DIR_WRITE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002776 idetape_write_release(drive, minor);
Borislav Petkov54abf372008-02-06 02:57:52 +01002777 if (tape->chrdev_dir == IDETAPE_DIR_READ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002778 if (minor < 128)
2779 idetape_discard_read_pipeline(drive, 1);
2780 else
2781 idetape_wait_for_pipeline(drive);
2782 }
Borislav Petkovf64eee72008-04-27 15:38:25 +02002783
Borislav Petkov346331f2008-04-18 00:46:26 +02002784 if (minor < 128 && test_bit(IDETAPE_FLAG_MEDIUM_PRESENT, &tape->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002785 (void) idetape_rewind_tape(drive);
Borislav Petkov54abf372008-02-06 02:57:52 +01002786 if (tape->chrdev_dir == IDETAPE_DIR_NONE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002787 if (tape->door_locked == DOOR_LOCKED) {
2788 if (idetape_create_prevent_cmd(drive, &pc, 0)) {
2789 if (!idetape_queue_pc_tail(drive, &pc))
2790 tape->door_locked = DOOR_UNLOCKED;
2791 }
2792 }
2793 }
Borislav Petkov346331f2008-04-18 00:46:26 +02002794 clear_bit(IDETAPE_FLAG_BUSY, &tape->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002795 ide_tape_put(tape);
2796 unlock_kernel();
2797 return 0;
2798}
2799
2800/*
Borislav Petkov71071b82008-02-06 02:57:53 +01002801 * check the contents of the ATAPI IDENTIFY command results. We return:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002802 *
Borislav Petkov71071b82008-02-06 02:57:53 +01002803 * 1 - If the tape can be supported by us, based on the information we have so
2804 * far.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002805 *
Borislav Petkov71071b82008-02-06 02:57:53 +01002806 * 0 - If this tape driver is not currently supported by us.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002807 */
Borislav Petkov71071b82008-02-06 02:57:53 +01002808static int idetape_identify_device(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002809{
Borislav Petkov71071b82008-02-06 02:57:53 +01002810 u8 gcw[2], protocol, device_type, removable, packet_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002811
2812 if (drive->id_read == 0)
2813 return 1;
2814
Borislav Petkov71071b82008-02-06 02:57:53 +01002815 *((unsigned short *) &gcw) = drive->id->config;
2816
2817 protocol = (gcw[1] & 0xC0) >> 6;
2818 device_type = gcw[1] & 0x1F;
2819 removable = !!(gcw[0] & 0x80);
2820 packet_size = gcw[0] & 0x3;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002821
Linus Torvalds1da177e2005-04-16 15:20:36 -07002822 /* Check that we can support this device */
Borislav Petkov71071b82008-02-06 02:57:53 +01002823 if (protocol != 2)
Bartlomiej Zolnierkiewicz16422de32008-02-02 19:56:48 +01002824 printk(KERN_ERR "ide-tape: Protocol (0x%02x) is not ATAPI\n",
Borislav Petkov71071b82008-02-06 02:57:53 +01002825 protocol);
2826 else if (device_type != 1)
Bartlomiej Zolnierkiewicz16422de32008-02-02 19:56:48 +01002827 printk(KERN_ERR "ide-tape: Device type (0x%02x) is not set "
Borislav Petkov71071b82008-02-06 02:57:53 +01002828 "to tape\n", device_type);
2829 else if (!removable)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002830 printk(KERN_ERR "ide-tape: The removable flag is not set\n");
Borislav Petkov71071b82008-02-06 02:57:53 +01002831 else if (packet_size != 0) {
Borislav Petkov24d57f82008-02-06 02:57:54 +01002832 printk(KERN_ERR "ide-tape: Packet size (0x%02x) is not 12"
2833 " bytes\n", packet_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002834 } else
2835 return 1;
2836 return 0;
2837}
2838
Borislav Petkov6d29c8f2008-02-02 19:56:49 +01002839static void idetape_get_inquiry_results(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002840{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002841 idetape_tape_t *tape = drive->driver_data;
Borislav Petkovd236d742008-04-18 00:46:27 +02002842 struct ide_atapi_pc pc;
Borislav Petkov41f81d542008-02-06 02:57:52 +01002843 char fw_rev[6], vendor_id[10], product_id[18];
Borislav Petkov6d29c8f2008-02-02 19:56:49 +01002844
Linus Torvalds1da177e2005-04-16 15:20:36 -07002845 idetape_create_inquiry_cmd(&pc);
2846 if (idetape_queue_pc_tail(drive, &pc)) {
Borislav Petkov6d29c8f2008-02-02 19:56:49 +01002847 printk(KERN_ERR "ide-tape: %s: can't get INQUIRY results\n",
2848 tape->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002849 return;
2850 }
Borislav Petkovd236d742008-04-18 00:46:27 +02002851 memcpy(vendor_id, &pc.buf[8], 8);
2852 memcpy(product_id, &pc.buf[16], 16);
2853 memcpy(fw_rev, &pc.buf[32], 4);
Borislav Petkov6d29c8f2008-02-02 19:56:49 +01002854
Borislav Petkov41f81d542008-02-06 02:57:52 +01002855 ide_fixstring(vendor_id, 10, 0);
2856 ide_fixstring(product_id, 18, 0);
2857 ide_fixstring(fw_rev, 6, 0);
2858
Borislav Petkov6d29c8f2008-02-02 19:56:49 +01002859 printk(KERN_INFO "ide-tape: %s <-> %s: %s %s rev %s\n",
Borislav Petkov41f81d542008-02-06 02:57:52 +01002860 drive->name, tape->name, vendor_id, product_id, fw_rev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002861}
2862
2863/*
Borislav Petkovb6422012008-02-02 19:56:49 +01002864 * Ask the tape about its various parameters. In particular, we will adjust our
2865 * data transfer buffer size to the recommended value as returned by the tape.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002866 */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002867static void idetape_get_mode_sense_results(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002868{
2869 idetape_tape_t *tape = drive->driver_data;
Borislav Petkovd236d742008-04-18 00:46:27 +02002870 struct ide_atapi_pc pc;
Borislav Petkovb6422012008-02-02 19:56:49 +01002871 u8 *caps;
2872 u8 speed, max_speed;
Borislav Petkov47314fa2008-02-02 19:56:48 +01002873
Linus Torvalds1da177e2005-04-16 15:20:36 -07002874 idetape_create_mode_sense_cmd(&pc, IDETAPE_CAPABILITIES_PAGE);
2875 if (idetape_queue_pc_tail(drive, &pc)) {
Borislav Petkovb6422012008-02-02 19:56:49 +01002876 printk(KERN_ERR "ide-tape: Can't get tape parameters - assuming"
2877 " some default values\n");
Borislav Petkov54bb2072008-02-06 02:57:52 +01002878 tape->blk_size = 512;
Borislav Petkovb6422012008-02-02 19:56:49 +01002879 put_unaligned(52, (u16 *)&tape->caps[12]);
2880 put_unaligned(540, (u16 *)&tape->caps[14]);
2881 put_unaligned(6*52, (u16 *)&tape->caps[16]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002882 return;
2883 }
Borislav Petkovd236d742008-04-18 00:46:27 +02002884 caps = pc.buf + 4 + pc.buf[3];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002885
Borislav Petkovb6422012008-02-02 19:56:49 +01002886 /* convert to host order and save for later use */
2887 speed = be16_to_cpu(*(u16 *)&caps[14]);
2888 max_speed = be16_to_cpu(*(u16 *)&caps[8]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002889
Borislav Petkovb6422012008-02-02 19:56:49 +01002890 put_unaligned(max_speed, (u16 *)&caps[8]);
2891 put_unaligned(be16_to_cpu(*(u16 *)&caps[12]), (u16 *)&caps[12]);
2892 put_unaligned(speed, (u16 *)&caps[14]);
2893 put_unaligned(be16_to_cpu(*(u16 *)&caps[16]), (u16 *)&caps[16]);
2894
2895 if (!speed) {
2896 printk(KERN_INFO "ide-tape: %s: invalid tape speed "
2897 "(assuming 650KB/sec)\n", drive->name);
2898 put_unaligned(650, (u16 *)&caps[14]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002899 }
Borislav Petkovb6422012008-02-02 19:56:49 +01002900 if (!max_speed) {
2901 printk(KERN_INFO "ide-tape: %s: invalid max_speed "
2902 "(assuming 650KB/sec)\n", drive->name);
2903 put_unaligned(650, (u16 *)&caps[8]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002904 }
2905
Borislav Petkovb6422012008-02-02 19:56:49 +01002906 memcpy(&tape->caps, caps, 20);
2907 if (caps[7] & 0x02)
Borislav Petkov54bb2072008-02-06 02:57:52 +01002908 tape->blk_size = 512;
Borislav Petkovb6422012008-02-02 19:56:49 +01002909 else if (caps[7] & 0x04)
Borislav Petkov54bb2072008-02-06 02:57:52 +01002910 tape->blk_size = 1024;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002911}
2912
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +02002913#ifdef CONFIG_IDE_PROC_FS
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002914static void idetape_add_settings(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002915{
2916 idetape_tape_t *tape = drive->driver_data;
2917
Borislav Petkovb6422012008-02-02 19:56:49 +01002918 ide_add_setting(drive, "buffer", SETTING_READ, TYPE_SHORT, 0, 0xffff,
2919 1, 2, (u16 *)&tape->caps[16], NULL);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002920 ide_add_setting(drive, "pipeline_min", SETTING_RW, TYPE_INT, 1, 0xffff,
2921 tape->stage_size / 1024, 1, &tape->min_pipeline, NULL);
2922 ide_add_setting(drive, "pipeline", SETTING_RW, TYPE_INT, 1, 0xffff,
2923 tape->stage_size / 1024, 1, &tape->max_stages, NULL);
2924 ide_add_setting(drive, "pipeline_max", SETTING_RW, TYPE_INT, 1, 0xffff,
2925 tape->stage_size / 1024, 1, &tape->max_pipeline, NULL);
2926 ide_add_setting(drive, "pipeline_used", SETTING_READ, TYPE_INT, 0,
2927 0xffff, tape->stage_size / 1024, 1, &tape->nr_stages,
2928 NULL);
2929 ide_add_setting(drive, "pipeline_pending", SETTING_READ, TYPE_INT, 0,
2930 0xffff, tape->stage_size / 1024, 1,
2931 &tape->nr_pending_stages, NULL);
Borislav Petkovb6422012008-02-02 19:56:49 +01002932 ide_add_setting(drive, "speed", SETTING_READ, TYPE_SHORT, 0, 0xffff,
2933 1, 1, (u16 *)&tape->caps[14], NULL);
Borislav Petkov54bb2072008-02-06 02:57:52 +01002934 ide_add_setting(drive, "stage", SETTING_READ, TYPE_INT, 0, 0xffff, 1,
2935 1024, &tape->stage_size, NULL);
2936 ide_add_setting(drive, "tdsc", SETTING_RW, TYPE_INT, IDETAPE_DSC_RW_MIN,
2937 IDETAPE_DSC_RW_MAX, 1000, HZ, &tape->best_dsc_rw_freq,
2938 NULL);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002939 ide_add_setting(drive, "dsc_overlap", SETTING_RW, TYPE_BYTE, 0, 1, 1,
2940 1, &drive->dsc_overlap, NULL);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002941 ide_add_setting(drive, "avg_speed", SETTING_READ, TYPE_INT, 0, 0xffff,
2942 1, 1, &tape->avg_speed, NULL);
Borislav Petkov8004a8c2008-02-06 02:57:51 +01002943 ide_add_setting(drive, "debug_mask", SETTING_RW, TYPE_INT, 0, 0xffff, 1,
2944 1, &tape->debug_mask, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002945}
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +02002946#else
2947static inline void idetape_add_settings(ide_drive_t *drive) { ; }
2948#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002949
2950/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +01002951 * The function below is called to:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002952 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +01002953 * 1. Initialize our various state variables.
2954 * 2. Ask the tape for its capabilities.
2955 * 3. Allocate a buffer which will be used for data transfer. The buffer size
2956 * is chosen based on the recommendation which we received in step 2.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002957 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +01002958 * Note that at this point ide.c already assigned us an irq, so that we can
2959 * queue requests here and wait for their completion.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002960 */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002961static void idetape_setup(ide_drive_t *drive, idetape_tape_t *tape, int minor)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002962{
2963 unsigned long t1, tmid, tn, t;
2964 int speed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002965 int stage_size;
Borislav Petkov71071b82008-02-06 02:57:53 +01002966 u8 gcw[2];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002967 struct sysinfo si;
Borislav Petkovb6422012008-02-02 19:56:49 +01002968 u16 *ctl = (u16 *)&tape->caps[12];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002969
Borislav Petkov54bb2072008-02-06 02:57:52 +01002970 spin_lock_init(&tape->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002971 drive->dsc_overlap = 1;
Bartlomiej Zolnierkiewicz4166c192008-02-01 23:09:30 +01002972 if (drive->hwif->host_flags & IDE_HFLAG_NO_DSC) {
2973 printk(KERN_INFO "ide-tape: %s: disabling DSC overlap\n",
2974 tape->name);
2975 drive->dsc_overlap = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002976 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002977 /* Seagate Travan drives do not support DSC overlap. */
2978 if (strstr(drive->id->model, "Seagate STT3401"))
2979 drive->dsc_overlap = 0;
2980 tape->minor = minor;
2981 tape->name[0] = 'h';
2982 tape->name[1] = 't';
2983 tape->name[2] = '0' + minor;
Borislav Petkov54abf372008-02-06 02:57:52 +01002984 tape->chrdev_dir = IDETAPE_DIR_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002985 tape->pc = tape->pc_stack;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002986 *((unsigned short *) &gcw) = drive->id->config;
Borislav Petkov71071b82008-02-06 02:57:53 +01002987
2988 /* Command packet DRQ type */
2989 if (((gcw[0] & 0x60) >> 5) == 1)
Borislav Petkov346331f2008-04-18 00:46:26 +02002990 set_bit(IDETAPE_FLAG_DRQ_INTERRUPT, &tape->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002991
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002992 tape->min_pipeline = 10;
2993 tape->max_pipeline = 10;
2994 tape->max_stages = 10;
Borislav Petkov3c98bf32008-02-06 02:57:53 +01002995
Linus Torvalds1da177e2005-04-16 15:20:36 -07002996 idetape_get_inquiry_results(drive);
2997 idetape_get_mode_sense_results(drive);
Borislav Petkov3cffb9c2008-02-02 19:56:50 +01002998 ide_tape_get_bsize_from_bdesc(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002999 tape->user_bs_factor = 1;
Borislav Petkov54bb2072008-02-06 02:57:52 +01003000 tape->stage_size = *ctl * tape->blk_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003001 while (tape->stage_size > 0xffff) {
3002 printk(KERN_NOTICE "ide-tape: decreasing stage size\n");
Borislav Petkovb6422012008-02-02 19:56:49 +01003003 *ctl /= 2;
Borislav Petkov54bb2072008-02-06 02:57:52 +01003004 tape->stage_size = *ctl * tape->blk_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003005 }
3006 stage_size = tape->stage_size;
3007 tape->pages_per_stage = stage_size / PAGE_SIZE;
3008 if (stage_size % PAGE_SIZE) {
3009 tape->pages_per_stage++;
3010 tape->excess_bh_size = PAGE_SIZE - stage_size % PAGE_SIZE;
3011 }
3012
Borislav Petkovb6422012008-02-02 19:56:49 +01003013 /* Select the "best" DSC read/write polling freq and pipeline size. */
3014 speed = max(*(u16 *)&tape->caps[14], *(u16 *)&tape->caps[8]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003015
3016 tape->max_stages = speed * 1000 * 10 / tape->stage_size;
3017
Borislav Petkov3c98bf32008-02-06 02:57:53 +01003018 /* Limit memory use for pipeline to 10% of physical memory */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003019 si_meminfo(&si);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01003020 if (tape->max_stages * tape->stage_size >
3021 si.totalram * si.mem_unit / 10)
3022 tape->max_stages =
3023 si.totalram * si.mem_unit / (10 * tape->stage_size);
3024
Linus Torvalds1da177e2005-04-16 15:20:36 -07003025 tape->max_stages = min(tape->max_stages, IDETAPE_MAX_PIPELINE_STAGES);
3026 tape->min_pipeline = min(tape->max_stages, IDETAPE_MIN_PIPELINE_STAGES);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01003027 tape->max_pipeline =
3028 min(tape->max_stages * 2, IDETAPE_MAX_PIPELINE_STAGES);
3029 if (tape->max_stages == 0) {
3030 tape->max_stages = 1;
3031 tape->min_pipeline = 1;
3032 tape->max_pipeline = 1;
3033 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003034
3035 t1 = (tape->stage_size * HZ) / (speed * 1000);
Borislav Petkovb6422012008-02-02 19:56:49 +01003036 tmid = (*(u16 *)&tape->caps[16] * 32 * HZ) / (speed * 125);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003037 tn = (IDETAPE_FIFO_THRESHOLD * tape->stage_size * HZ) / (speed * 1000);
3038
3039 if (tape->max_stages)
3040 t = tn;
3041 else
3042 t = t1;
3043
3044 /*
Borislav Petkov3c98bf32008-02-06 02:57:53 +01003045 * Ensure that the number we got makes sense; limit it within
3046 * IDETAPE_DSC_RW_MIN and IDETAPE_DSC_RW_MAX.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003047 */
Borislav Petkov54bb2072008-02-06 02:57:52 +01003048 tape->best_dsc_rw_freq = max_t(unsigned long,
3049 min_t(unsigned long, t, IDETAPE_DSC_RW_MAX),
3050 IDETAPE_DSC_RW_MIN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003051 printk(KERN_INFO "ide-tape: %s <-> %s: %dKBps, %d*%dkB buffer, "
3052 "%dkB pipeline, %lums tDSC%s\n",
Borislav Petkovb6422012008-02-02 19:56:49 +01003053 drive->name, tape->name, *(u16 *)&tape->caps[14],
3054 (*(u16 *)&tape->caps[16] * 512) / tape->stage_size,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003055 tape->stage_size / 1024,
3056 tape->max_stages * tape->stage_size / 1024,
Borislav Petkov54bb2072008-02-06 02:57:52 +01003057 tape->best_dsc_rw_freq * 1000 / HZ,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003058 drive->using_dma ? ", DMA":"");
3059
3060 idetape_add_settings(drive);
3061}
3062
Russell King4031bbe2006-01-06 11:41:00 +00003063static void ide_tape_remove(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003064{
3065 idetape_tape_t *tape = drive->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003066
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +02003067 ide_proc_unregister_driver(drive, tape->driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003068
3069 ide_unregister_region(tape->disk);
3070
3071 ide_tape_put(tape);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003072}
3073
3074static void ide_tape_release(struct kref *kref)
3075{
3076 struct ide_tape_obj *tape = to_ide_tape(kref);
3077 ide_drive_t *drive = tape->drive;
3078 struct gendisk *g = tape->disk;
3079
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02003080 BUG_ON(tape->first_stage != NULL || tape->merge_stage_size);
3081
Linus Torvalds1da177e2005-04-16 15:20:36 -07003082 drive->dsc_overlap = 0;
3083 drive->driver_data = NULL;
Tony Jonesdbc12722007-09-25 02:03:03 +02003084 device_destroy(idetape_sysfs_class, MKDEV(IDETAPE_MAJOR, tape->minor));
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01003085 device_destroy(idetape_sysfs_class,
3086 MKDEV(IDETAPE_MAJOR, tape->minor + 128));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003087 idetape_devs[tape->minor] = NULL;
3088 g->private_data = NULL;
3089 put_disk(g);
3090 kfree(tape);
3091}
3092
Bartlomiej Zolnierkiewiczecfd80e2007-05-10 00:01:09 +02003093#ifdef CONFIG_IDE_PROC_FS
Linus Torvalds1da177e2005-04-16 15:20:36 -07003094static int proc_idetape_read_name
3095 (char *page, char **start, off_t off, int count, int *eof, void *data)
3096{
3097 ide_drive_t *drive = (ide_drive_t *) data;
3098 idetape_tape_t *tape = drive->driver_data;
3099 char *out = page;
3100 int len;
3101
3102 len = sprintf(out, "%s\n", tape->name);
3103 PROC_IDE_READ_RETURN(page, start, off, count, eof, len);
3104}
3105
3106static ide_proc_entry_t idetape_proc[] = {
3107 { "capacity", S_IFREG|S_IRUGO, proc_ide_read_capacity, NULL },
3108 { "name", S_IFREG|S_IRUGO, proc_idetape_read_name, NULL },
3109 { NULL, 0, NULL, NULL }
3110};
Linus Torvalds1da177e2005-04-16 15:20:36 -07003111#endif
3112
Russell King4031bbe2006-01-06 11:41:00 +00003113static int ide_tape_probe(ide_drive_t *);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003114
Linus Torvalds1da177e2005-04-16 15:20:36 -07003115static ide_driver_t idetape_driver = {
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02003116 .gen_driver = {
Laurent Riffard4ef3b8f2005-11-18 22:15:40 +01003117 .owner = THIS_MODULE,
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02003118 .name = "ide-tape",
3119 .bus = &ide_bus_type,
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02003120 },
Russell King4031bbe2006-01-06 11:41:00 +00003121 .probe = ide_tape_probe,
3122 .remove = ide_tape_remove,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003123 .version = IDETAPE_VERSION,
3124 .media = ide_tape,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003125 .supports_dsc_overlap = 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003126 .do_request = idetape_do_request,
3127 .end_request = idetape_end_request,
3128 .error = __ide_error,
3129 .abort = __ide_abort,
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +02003130#ifdef CONFIG_IDE_PROC_FS
Linus Torvalds1da177e2005-04-16 15:20:36 -07003131 .proc = idetape_proc,
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +02003132#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003133};
3134
Borislav Petkov3c98bf32008-02-06 02:57:53 +01003135/* Our character device supporting functions, passed to register_chrdev. */
Arjan van de Ven2b8693c2007-02-12 00:55:32 -08003136static const struct file_operations idetape_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003137 .owner = THIS_MODULE,
3138 .read = idetape_chrdev_read,
3139 .write = idetape_chrdev_write,
3140 .ioctl = idetape_chrdev_ioctl,
3141 .open = idetape_chrdev_open,
3142 .release = idetape_chrdev_release,
3143};
3144
3145static int idetape_open(struct inode *inode, struct file *filp)
3146{
3147 struct gendisk *disk = inode->i_bdev->bd_disk;
3148 struct ide_tape_obj *tape;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003149
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01003150 tape = ide_tape_get(disk);
3151 if (!tape)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003152 return -ENXIO;
3153
Linus Torvalds1da177e2005-04-16 15:20:36 -07003154 return 0;
3155}
3156
3157static int idetape_release(struct inode *inode, struct file *filp)
3158{
3159 struct gendisk *disk = inode->i_bdev->bd_disk;
3160 struct ide_tape_obj *tape = ide_tape_g(disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003161
3162 ide_tape_put(tape);
3163
3164 return 0;
3165}
3166
3167static int idetape_ioctl(struct inode *inode, struct file *file,
3168 unsigned int cmd, unsigned long arg)
3169{
3170 struct block_device *bdev = inode->i_bdev;
3171 struct ide_tape_obj *tape = ide_tape_g(bdev->bd_disk);
3172 ide_drive_t *drive = tape->drive;
3173 int err = generic_ide_ioctl(drive, file, bdev, cmd, arg);
3174 if (err == -EINVAL)
3175 err = idetape_blkdev_ioctl(drive, cmd, arg);
3176 return err;
3177}
3178
3179static struct block_device_operations idetape_block_ops = {
3180 .owner = THIS_MODULE,
3181 .open = idetape_open,
3182 .release = idetape_release,
3183 .ioctl = idetape_ioctl,
3184};
3185
Russell King4031bbe2006-01-06 11:41:00 +00003186static int ide_tape_probe(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003187{
3188 idetape_tape_t *tape;
3189 struct gendisk *g;
3190 int minor;
3191
3192 if (!strstr("ide-tape", drive->driver_req))
3193 goto failed;
3194 if (!drive->present)
3195 goto failed;
3196 if (drive->media != ide_tape)
3197 goto failed;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01003198 if (!idetape_identify_device(drive)) {
3199 printk(KERN_ERR "ide-tape: %s: not supported by this version of"
3200 " the driver\n", drive->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003201 goto failed;
3202 }
3203 if (drive->scsi) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01003204 printk(KERN_INFO "ide-tape: passing drive %s to ide-scsi"
3205 " emulation.\n", drive->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003206 goto failed;
3207 }
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01003208 tape = kzalloc(sizeof(idetape_tape_t), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003209 if (tape == NULL) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01003210 printk(KERN_ERR "ide-tape: %s: Can't allocate a tape struct\n",
3211 drive->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003212 goto failed;
3213 }
3214
3215 g = alloc_disk(1 << PARTN_BITS);
3216 if (!g)
3217 goto out_free_tape;
3218
3219 ide_init_disk(g, drive);
3220
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +02003221 ide_proc_register_driver(drive, &idetape_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003222
Linus Torvalds1da177e2005-04-16 15:20:36 -07003223 kref_init(&tape->kref);
3224
3225 tape->drive = drive;
3226 tape->driver = &idetape_driver;
3227 tape->disk = g;
3228
3229 g->private_data = &tape->driver;
3230
3231 drive->driver_data = tape;
3232
Arjan van de Vencf8b8972006-03-23 03:00:45 -08003233 mutex_lock(&idetape_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003234 for (minor = 0; idetape_devs[minor]; minor++)
3235 ;
3236 idetape_devs[minor] = tape;
Arjan van de Vencf8b8972006-03-23 03:00:45 -08003237 mutex_unlock(&idetape_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003238
3239 idetape_setup(drive, tape, minor);
3240
Tony Jonesdbc12722007-09-25 02:03:03 +02003241 device_create(idetape_sysfs_class, &drive->gendev,
3242 MKDEV(IDETAPE_MAJOR, minor), "%s", tape->name);
3243 device_create(idetape_sysfs_class, &drive->gendev,
3244 MKDEV(IDETAPE_MAJOR, minor + 128), "n%s", tape->name);
Will Dysond5dee802005-09-16 02:55:07 -07003245
Linus Torvalds1da177e2005-04-16 15:20:36 -07003246 g->fops = &idetape_block_ops;
3247 ide_register_region(g);
3248
3249 return 0;
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02003250
Linus Torvalds1da177e2005-04-16 15:20:36 -07003251out_free_tape:
3252 kfree(tape);
3253failed:
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02003254 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003255}
3256
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01003257static void __exit idetape_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003258{
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02003259 driver_unregister(&idetape_driver.gen_driver);
Will Dysond5dee802005-09-16 02:55:07 -07003260 class_destroy(idetape_sysfs_class);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003261 unregister_chrdev(IDETAPE_MAJOR, "ht");
3262}
3263
Bartlomiej Zolnierkiewicz17514e82005-11-19 22:24:35 +01003264static int __init idetape_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003265{
Will Dysond5dee802005-09-16 02:55:07 -07003266 int error = 1;
3267 idetape_sysfs_class = class_create(THIS_MODULE, "ide_tape");
3268 if (IS_ERR(idetape_sysfs_class)) {
3269 idetape_sysfs_class = NULL;
3270 printk(KERN_ERR "Unable to create sysfs class for ide tapes\n");
3271 error = -EBUSY;
3272 goto out;
3273 }
3274
Linus Torvalds1da177e2005-04-16 15:20:36 -07003275 if (register_chrdev(IDETAPE_MAJOR, "ht", &idetape_fops)) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01003276 printk(KERN_ERR "ide-tape: Failed to register chrdev"
3277 " interface\n");
Will Dysond5dee802005-09-16 02:55:07 -07003278 error = -EBUSY;
3279 goto out_free_class;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003280 }
Will Dysond5dee802005-09-16 02:55:07 -07003281
3282 error = driver_register(&idetape_driver.gen_driver);
3283 if (error)
3284 goto out_free_driver;
3285
3286 return 0;
3287
3288out_free_driver:
3289 driver_unregister(&idetape_driver.gen_driver);
3290out_free_class:
3291 class_destroy(idetape_sysfs_class);
3292out:
3293 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003294}
3295
Kay Sievers263756e2005-12-12 18:03:44 +01003296MODULE_ALIAS("ide:*m-tape*");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003297module_init(idetape_init);
3298module_exit(idetape_exit);
3299MODULE_ALIAS_CHARDEV_MAJOR(IDETAPE_MAJOR);
Borislav Petkov9c145762008-02-06 02:57:54 +01003300MODULE_DESCRIPTION("ATAPI Streaming TAPE Driver");
3301MODULE_LICENSE("GPL");