blob: 8eb117519ba4f9692be6ec32411e46530885bc7a [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 Petkov3c98bf32008-02-06 02:57:53 +0100645/* Free a stage along with its related buffers completely. */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100646static void __idetape_kfree_stage(idetape_stage_t *stage)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647{
648 struct idetape_bh *prev_bh, *bh = stage->bh;
649 int size;
650
651 while (bh != NULL) {
652 if (bh->b_data != NULL) {
653 size = (int) bh->b_size;
654 while (size > 0) {
655 free_page((unsigned long) bh->b_data);
656 size -= PAGE_SIZE;
657 bh->b_data += PAGE_SIZE;
658 }
659 }
660 prev_bh = bh;
661 bh = bh->b_reqnext;
662 kfree(prev_bh);
663 }
664 kfree(stage);
665}
666
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100668 * Finish servicing a request and insert a pending pipeline request into the
669 * main device queue.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670 */
671static int idetape_end_request(ide_drive_t *drive, int uptodate, int nr_sects)
672{
673 struct request *rq = HWGROUP(drive)->rq;
674 idetape_tape_t *tape = drive->driver_data;
675 unsigned long flags;
676 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677
Borislav Petkov8004a8c2008-02-06 02:57:51 +0100678 debug_log(DBG_PROCS, "Enter %s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679
680 switch (uptodate) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100681 case 0: error = IDETAPE_ERROR_GENERAL; break;
682 case 1: error = 0; break;
683 default: error = uptodate;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684 }
685 rq->errors = error;
686 if (error)
687 tape->failed_pc = NULL;
688
Bartlomiej Zolnierkiewicz36872212008-01-26 20:13:10 +0100689 if (!blk_special_request(rq)) {
690 ide_end_request(drive, uptodate, nr_sects);
691 return 0;
692 }
693
Borislav Petkov54bb2072008-02-06 02:57:52 +0100694 spin_lock_irqsave(&tape->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 ide_end_drive_cmd(drive, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697
Borislav Petkov189bb3b2008-04-27 15:38:27 +0200698 clear_bit(IDETAPE_FLAG_PIPELINE_ACTIVE, &tape->flags);
Borislav Petkov54bb2072008-02-06 02:57:52 +0100699 spin_unlock_irqrestore(&tape->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 return 0;
701}
702
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100703static ide_startstop_t idetape_request_sense_callback(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704{
705 idetape_tape_t *tape = drive->driver_data;
706
Borislav Petkov8004a8c2008-02-06 02:57:51 +0100707 debug_log(DBG_PROCS, "Enter %s\n", __func__);
708
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709 if (!tape->pc->error) {
Borislav Petkovd236d742008-04-18 00:46:27 +0200710 idetape_analyze_error(drive, tape->pc->buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711 idetape_end_request(drive, 1, 0);
712 } else {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100713 printk(KERN_ERR "ide-tape: Error in REQUEST SENSE itself - "
714 "Aborting request!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 idetape_end_request(drive, 0, 0);
716 }
717 return ide_stopped;
718}
719
Borislav Petkovd236d742008-04-18 00:46:27 +0200720static void idetape_create_request_sense_cmd(struct ide_atapi_pc *pc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721{
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100722 idetape_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +0100723 pc->c[0] = REQUEST_SENSE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724 pc->c[4] = 20;
Borislav Petkovd236d742008-04-18 00:46:27 +0200725 pc->req_xfer = 20;
726 pc->idetape_callback = &idetape_request_sense_callback;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727}
728
729static void idetape_init_rq(struct request *rq, u8 cmd)
730{
731 memset(rq, 0, sizeof(*rq));
Jens Axboe4aff5e22006-08-10 08:44:47 +0200732 rq->cmd_type = REQ_TYPE_SPECIAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 rq->cmd[0] = cmd;
734}
735
736/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100737 * Generate a new packet command request in front of the request queue, before
738 * the current request, so that it will be processed immediately, on the next
739 * pass through the driver. The function below is called from the request
740 * handling part of the driver (the "bottom" part). Safe storage for the request
741 * should be allocated with ide_tape_next_{pc,rq}_storage() prior to that.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100743 * Memory for those requests is pre-allocated at initialization time, and is
744 * limited to IDETAPE_PC_STACK requests. We assume that we have enough space for
745 * the maximum possible number of inter-dependent packet commands.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100747 * The higher level of the driver - The ioctl handler and the character device
748 * handling functions should queue request to the lower level part and wait for
749 * their completion using idetape_queue_pc_tail or idetape_queue_rw_tail.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750 */
Borislav Petkovd236d742008-04-18 00:46:27 +0200751static void idetape_queue_pc_head(ide_drive_t *drive, struct ide_atapi_pc *pc,
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100752 struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753{
754 struct ide_tape_obj *tape = drive->driver_data;
755
756 idetape_init_rq(rq, REQ_IDETAPE_PC1);
757 rq->buffer = (char *) pc;
758 rq->rq_disk = tape->disk;
759 (void) ide_do_drive_cmd(drive, rq, ide_preempt);
760}
761
762/*
763 * idetape_retry_pc is called when an error was detected during the
764 * last packet command. We queue a request sense packet command in
765 * the head of the request list.
766 */
767static ide_startstop_t idetape_retry_pc (ide_drive_t *drive)
768{
769 idetape_tape_t *tape = drive->driver_data;
Borislav Petkovd236d742008-04-18 00:46:27 +0200770 struct ide_atapi_pc *pc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771 struct request *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772
Bartlomiej Zolnierkiewicz64a57fe2008-02-06 02:57:51 +0100773 (void)ide_read_error(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774 pc = idetape_next_pc_storage(drive);
775 rq = idetape_next_rq_storage(drive);
776 idetape_create_request_sense_cmd(pc);
Borislav Petkov346331f2008-04-18 00:46:26 +0200777 set_bit(IDETAPE_FLAG_IGNORE_DSC, &tape->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778 idetape_queue_pc_head(drive, pc, rq);
779 return ide_stopped;
780}
781
782/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100783 * Postpone the current request so that ide.c will be able to service requests
784 * from another device on the same hwgroup while we are polling for DSC.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785 */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100786static void idetape_postpone_request(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787{
788 idetape_tape_t *tape = drive->driver_data;
789
Borislav Petkov8004a8c2008-02-06 02:57:51 +0100790 debug_log(DBG_PROCS, "Enter %s\n", __func__);
791
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792 tape->postponed_rq = HWGROUP(drive)->rq;
Borislav Petkov54bb2072008-02-06 02:57:52 +0100793 ide_stall_queue(drive, tape->dsc_poll_freq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794}
795
Borislav Petkovd236d742008-04-18 00:46:27 +0200796typedef void idetape_io_buf(ide_drive_t *, struct ide_atapi_pc *, unsigned int);
Borislav Petkova1efc852008-02-06 02:57:52 +0100797
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798/*
Borislav Petkova1efc852008-02-06 02:57:52 +0100799 * This is the usual interrupt handler which will be called during a packet
800 * command. We will transfer some of the data (as requested by the drive) and
801 * will re-point interrupt handler to us. When data transfer is finished, we
802 * will act according to the algorithm described before
Borislav Petkov8d06bfa2008-02-06 02:57:53 +0100803 * idetape_issue_pc.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 */
Borislav Petkova1efc852008-02-06 02:57:52 +0100805static ide_startstop_t idetape_pc_intr(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806{
807 ide_hwif_t *hwif = drive->hwif;
808 idetape_tape_t *tape = drive->driver_data;
Borislav Petkovd236d742008-04-18 00:46:27 +0200809 struct ide_atapi_pc *pc = tape->pc;
Borislav Petkova1efc852008-02-06 02:57:52 +0100810 xfer_func_t *xferfunc;
811 idetape_io_buf *iobuf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812 unsigned int temp;
813#if SIMULATE_ERRORS
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100814 static int error_sim_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815#endif
Bartlomiej Zolnierkiewicz790d1232008-01-25 22:17:12 +0100816 u16 bcount;
Bartlomiej Zolnierkiewicz8e7657a2008-01-25 22:17:12 +0100817 u8 stat, ireason;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818
Borislav Petkov8004a8c2008-02-06 02:57:51 +0100819 debug_log(DBG_PROCS, "Enter %s - interrupt handler\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820
821 /* Clear the interrupt */
Bartlomiej Zolnierkiewiczc47137a2008-02-06 02:57:51 +0100822 stat = ide_read_status(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823
Borislav Petkov346331f2008-04-18 00:46:26 +0200824 if (pc->flags & PC_FLAG_DMA_IN_PROGRESS) {
Bartlomiej Zolnierkiewicz5e37bdc2008-04-26 22:25:24 +0200825 if (hwif->dma_ops->dma_end(drive) || (stat & ERR_STAT)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826 /*
827 * A DMA error is sometimes expected. For example,
828 * if the tape is crossing a filemark during a
829 * READ command, it will issue an irq and position
830 * itself before the filemark, so that only a partial
831 * data transfer will occur (which causes the DMA
832 * error). In that case, we will later ask the tape
833 * how much bytes of the original request were
834 * actually transferred (we can't receive that
835 * information from the DMA engine on most chipsets).
836 */
837
838 /*
839 * On the contrary, a DMA error is never expected;
840 * it usually indicates a hardware error or abort.
841 * If the tape crosses a filemark during a READ
842 * command, it will issue an irq and position itself
843 * after the filemark (not before). Only a partial
844 * data transfer will occur, but no DMA error.
845 * (AS, 19 Apr 2001)
846 */
Borislav Petkov346331f2008-04-18 00:46:26 +0200847 pc->flags |= PC_FLAG_DMA_ERROR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848 } else {
Borislav Petkovd236d742008-04-18 00:46:27 +0200849 pc->xferred = pc->req_xfer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850 idetape_update_buffers(pc);
851 }
Borislav Petkov8004a8c2008-02-06 02:57:51 +0100852 debug_log(DBG_PROCS, "DMA finished\n");
853
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854 }
855
856 /* No more interrupts */
Bartlomiej Zolnierkiewicz22c525b2008-01-25 22:17:11 +0100857 if ((stat & DRQ_STAT) == 0) {
Borislav Petkov8004a8c2008-02-06 02:57:51 +0100858 debug_log(DBG_SENSE, "Packet command completed, %d bytes"
Borislav Petkovd236d742008-04-18 00:46:27 +0200859 " transferred\n", pc->xferred);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860
Borislav Petkov346331f2008-04-18 00:46:26 +0200861 pc->flags &= ~PC_FLAG_DMA_IN_PROGRESS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862 local_irq_enable();
863
864#if SIMULATE_ERRORS
Borislav Petkov90699ce2008-02-02 19:56:50 +0100865 if ((pc->c[0] == WRITE_6 || pc->c[0] == READ_6) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866 (++error_sim_count % 100) == 0) {
867 printk(KERN_INFO "ide-tape: %s: simulating error\n",
868 tape->name);
Bartlomiej Zolnierkiewicz22c525b2008-01-25 22:17:11 +0100869 stat |= ERR_STAT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870 }
871#endif
Borislav Petkov90699ce2008-02-02 19:56:50 +0100872 if ((stat & ERR_STAT) && pc->c[0] == REQUEST_SENSE)
Bartlomiej Zolnierkiewicz22c525b2008-01-25 22:17:11 +0100873 stat &= ~ERR_STAT;
Borislav Petkov346331f2008-04-18 00:46:26 +0200874 if ((stat & ERR_STAT) || (pc->flags & PC_FLAG_DMA_ERROR)) {
Bartlomiej Zolnierkiewicz22c525b2008-01-25 22:17:11 +0100875 /* Error detected */
Borislav Petkov8004a8c2008-02-06 02:57:51 +0100876 debug_log(DBG_ERR, "%s: I/O error\n", tape->name);
877
Borislav Petkov90699ce2008-02-02 19:56:50 +0100878 if (pc->c[0] == REQUEST_SENSE) {
Borislav Petkova1efc852008-02-06 02:57:52 +0100879 printk(KERN_ERR "ide-tape: I/O error in request"
880 " sense command\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881 return ide_do_reset(drive);
882 }
Borislav Petkov8004a8c2008-02-06 02:57:51 +0100883 debug_log(DBG_ERR, "[cmd %x]: check condition\n",
884 pc->c[0]);
885
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886 /* Retry operation */
887 return idetape_retry_pc(drive);
888 }
889 pc->error = 0;
Borislav Petkov346331f2008-04-18 00:46:26 +0200890 if ((pc->flags & PC_FLAG_WAIT_FOR_DSC) &&
Bartlomiej Zolnierkiewicz22c525b2008-01-25 22:17:11 +0100891 (stat & SEEK_STAT) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892 /* Media access command */
893 tape->dsc_polling_start = jiffies;
Borislav Petkov54bb2072008-02-06 02:57:52 +0100894 tape->dsc_poll_freq = IDETAPE_DSC_MA_FAST;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895 tape->dsc_timeout = jiffies + IDETAPE_DSC_MA_TIMEOUT;
896 /* Allow ide.c to handle other requests */
897 idetape_postpone_request(drive);
898 return ide_stopped;
899 }
900 if (tape->failed_pc == pc)
901 tape->failed_pc = NULL;
902 /* Command finished - Call the callback function */
Borislav Petkovd236d742008-04-18 00:46:27 +0200903 return pc->idetape_callback(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904 }
Borislav Petkov346331f2008-04-18 00:46:26 +0200905
906 if (pc->flags & PC_FLAG_DMA_IN_PROGRESS) {
907 pc->flags &= ~PC_FLAG_DMA_IN_PROGRESS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908 printk(KERN_ERR "ide-tape: The tape wants to issue more "
909 "interrupts in DMA mode\n");
910 printk(KERN_ERR "ide-tape: DMA disabled, reverting to PIO\n");
Bartlomiej Zolnierkiewicz7469aaf2007-02-17 02:40:26 +0100911 ide_dma_off(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912 return ide_do_reset(drive);
913 }
914 /* Get the number of bytes to transfer on this interrupt. */
Bartlomiej Zolnierkiewicz23579a22008-04-18 00:46:26 +0200915 bcount = (hwif->INB(hwif->io_ports[IDE_BCOUNTH_OFFSET]) << 8) |
916 hwif->INB(hwif->io_ports[IDE_BCOUNTL_OFFSET]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917
Bartlomiej Zolnierkiewicz23579a22008-04-18 00:46:26 +0200918 ireason = hwif->INB(hwif->io_ports[IDE_IREASON_OFFSET]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919
Bartlomiej Zolnierkiewicz8e7657a2008-01-25 22:17:12 +0100920 if (ireason & CD) {
Borislav Petkova1efc852008-02-06 02:57:52 +0100921 printk(KERN_ERR "ide-tape: CoD != 0 in %s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922 return ide_do_reset(drive);
923 }
Borislav Petkov346331f2008-04-18 00:46:26 +0200924 if (((ireason & IO) == IO) == !!(pc->flags & PC_FLAG_WRITING)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925 /* Hopefully, we will never get here */
926 printk(KERN_ERR "ide-tape: We wanted to %s, ",
Bartlomiej Zolnierkiewicz8e7657a2008-01-25 22:17:12 +0100927 (ireason & IO) ? "Write" : "Read");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928 printk(KERN_ERR "ide-tape: but the tape wants us to %s !\n",
Bartlomiej Zolnierkiewicz8e7657a2008-01-25 22:17:12 +0100929 (ireason & IO) ? "Read" : "Write");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930 return ide_do_reset(drive);
931 }
Borislav Petkov346331f2008-04-18 00:46:26 +0200932 if (!(pc->flags & PC_FLAG_WRITING)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933 /* Reading - Check that we have enough space */
Borislav Petkovd236d742008-04-18 00:46:27 +0200934 temp = pc->xferred + bcount;
935 if (temp > pc->req_xfer) {
936 if (temp > pc->buf_size) {
Borislav Petkova1efc852008-02-06 02:57:52 +0100937 printk(KERN_ERR "ide-tape: The tape wants to "
938 "send us more data than expected "
939 "- discarding data\n");
Bartlomiej Zolnierkiewicz7616c0a2008-04-18 00:46:26 +0200940 ide_atapi_discard_data(drive, bcount);
Borislav Petkova1efc852008-02-06 02:57:52 +0100941 ide_set_handler(drive, &idetape_pc_intr,
942 IDETAPE_WAIT_CMD, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943 return ide_started;
944 }
Borislav Petkov8004a8c2008-02-06 02:57:51 +0100945 debug_log(DBG_SENSE, "The tape wants to send us more "
946 "data than expected - allowing transfer\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947 }
Borislav Petkova1efc852008-02-06 02:57:52 +0100948 iobuf = &idetape_input_buffers;
949 xferfunc = hwif->atapi_input_bytes;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950 } else {
Borislav Petkova1efc852008-02-06 02:57:52 +0100951 iobuf = &idetape_output_buffers;
952 xferfunc = hwif->atapi_output_bytes;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953 }
Borislav Petkova1efc852008-02-06 02:57:52 +0100954
955 if (pc->bh)
956 iobuf(drive, pc, bcount);
957 else
Borislav Petkovd236d742008-04-18 00:46:27 +0200958 xferfunc(drive, pc->cur_pos, bcount);
Borislav Petkova1efc852008-02-06 02:57:52 +0100959
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960 /* Update the current position */
Borislav Petkovd236d742008-04-18 00:46:27 +0200961 pc->xferred += bcount;
962 pc->cur_pos += bcount;
Borislav Petkov8004a8c2008-02-06 02:57:51 +0100963
964 debug_log(DBG_SENSE, "[cmd %x] transferred %d bytes on that intr.\n",
965 pc->c[0], bcount);
966
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967 /* And set the interrupt handler again */
968 ide_set_handler(drive, &idetape_pc_intr, IDETAPE_WAIT_CMD, NULL);
969 return ide_started;
970}
971
972/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100973 * Packet Command Interface
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100975 * The current Packet Command is available in tape->pc, and will not change
976 * until we finish handling it. Each packet command is associated with a
977 * callback function that will be called when the command is finished.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100979 * The handling will be done in three stages:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100981 * 1. idetape_issue_pc will send the packet command to the drive, and will set
982 * the interrupt handler to idetape_pc_intr.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100984 * 2. On each interrupt, idetape_pc_intr will be called. This step will be
985 * repeated until the device signals us that no more interrupts will be issued.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100987 * 3. ATAPI Tape media access commands have immediate status with a delayed
988 * process. In case of a successful initiation of a media access packet command,
989 * the DSC bit will be set when the actual execution of the command is finished.
990 * Since the tape drive will not issue an interrupt, we have to poll for this
991 * event. In this case, we define the request as "low priority request" by
992 * setting rq_status to IDETAPE_RQ_POSTPONED, set a timer to poll for DSC and
993 * exit the driver.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100995 * ide.c will then give higher priority to requests which originate from the
996 * other device, until will change rq_status to RQ_ACTIVE.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100998 * 4. When the packet command is finished, it will be checked for errors.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001000 * 5. In case an error was found, we queue a request sense packet command in
1001 * front of the request queue and retry the operation up to
1002 * IDETAPE_MAX_PC_RETRIES times.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001004 * 6. In case no error was found, or we decided to give up and not to retry
1005 * again, the callback function will be called and then we will handle the next
1006 * request.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007 */
1008static ide_startstop_t idetape_transfer_pc(ide_drive_t *drive)
1009{
1010 ide_hwif_t *hwif = drive->hwif;
1011 idetape_tape_t *tape = drive->driver_data;
Borislav Petkovd236d742008-04-18 00:46:27 +02001012 struct ide_atapi_pc *pc = tape->pc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013 int retries = 100;
1014 ide_startstop_t startstop;
Bartlomiej Zolnierkiewicz8e7657a2008-01-25 22:17:12 +01001015 u8 ireason;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001017 if (ide_wait_stat(&startstop, drive, DRQ_STAT, BUSY_STAT, WAIT_READY)) {
1018 printk(KERN_ERR "ide-tape: Strange, packet command initiated "
1019 "yet DRQ isn't asserted\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020 return startstop;
1021 }
Bartlomiej Zolnierkiewicz23579a22008-04-18 00:46:26 +02001022 ireason = hwif->INB(hwif->io_ports[IDE_IREASON_OFFSET]);
Bartlomiej Zolnierkiewicz8e7657a2008-01-25 22:17:12 +01001023 while (retries-- && ((ireason & CD) == 0 || (ireason & IO))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024 printk(KERN_ERR "ide-tape: (IO,CoD != (0,1) while issuing "
1025 "a packet command, retrying\n");
1026 udelay(100);
Bartlomiej Zolnierkiewicz23579a22008-04-18 00:46:26 +02001027 ireason = hwif->INB(hwif->io_ports[IDE_IREASON_OFFSET]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028 if (retries == 0) {
1029 printk(KERN_ERR "ide-tape: (IO,CoD != (0,1) while "
1030 "issuing a packet command, ignoring\n");
Bartlomiej Zolnierkiewicz8e7657a2008-01-25 22:17:12 +01001031 ireason |= CD;
1032 ireason &= ~IO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033 }
1034 }
Bartlomiej Zolnierkiewicz8e7657a2008-01-25 22:17:12 +01001035 if ((ireason & CD) == 0 || (ireason & IO)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036 printk(KERN_ERR "ide-tape: (IO,CoD) != (0,1) while issuing "
1037 "a packet command\n");
1038 return ide_do_reset(drive);
1039 }
1040 /* Set the interrupt routine */
1041 ide_set_handler(drive, &idetape_pc_intr, IDETAPE_WAIT_CMD, NULL);
1042#ifdef CONFIG_BLK_DEV_IDEDMA
1043 /* Begin DMA, if necessary */
Borislav Petkov346331f2008-04-18 00:46:26 +02001044 if (pc->flags & PC_FLAG_DMA_IN_PROGRESS)
Bartlomiej Zolnierkiewicz5e37bdc2008-04-26 22:25:24 +02001045 hwif->dma_ops->dma_start(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046#endif
1047 /* Send the actual packet */
1048 HWIF(drive)->atapi_output_bytes(drive, pc->c, 12);
1049 return ide_started;
1050}
1051
Borislav Petkovd236d742008-04-18 00:46:27 +02001052static ide_startstop_t idetape_issue_pc(ide_drive_t *drive,
1053 struct ide_atapi_pc *pc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054{
1055 ide_hwif_t *hwif = drive->hwif;
1056 idetape_tape_t *tape = drive->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057 int dma_ok = 0;
Bartlomiej Zolnierkiewicz790d1232008-01-25 22:17:12 +01001058 u16 bcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059
Borislav Petkov90699ce2008-02-02 19:56:50 +01001060 if (tape->pc->c[0] == REQUEST_SENSE &&
1061 pc->c[0] == REQUEST_SENSE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062 printk(KERN_ERR "ide-tape: possible ide-tape.c bug - "
1063 "Two request sense in serial were issued\n");
1064 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065
Borislav Petkov90699ce2008-02-02 19:56:50 +01001066 if (tape->failed_pc == NULL && pc->c[0] != REQUEST_SENSE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067 tape->failed_pc = pc;
1068 /* Set the current packet command */
1069 tape->pc = pc;
1070
1071 if (pc->retries > IDETAPE_MAX_PC_RETRIES ||
Borislav Petkov346331f2008-04-18 00:46:26 +02001072 (pc->flags & PC_FLAG_ABORT)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073 /*
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001074 * We will "abort" retrying a packet command in case legitimate
1075 * error code was received (crossing a filemark, or end of the
1076 * media, for example).
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077 */
Borislav Petkov346331f2008-04-18 00:46:26 +02001078 if (!(pc->flags & PC_FLAG_ABORT)) {
Borislav Petkov90699ce2008-02-02 19:56:50 +01001079 if (!(pc->c[0] == TEST_UNIT_READY &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080 tape->sense_key == 2 && tape->asc == 4 &&
1081 (tape->ascq == 1 || tape->ascq == 8))) {
1082 printk(KERN_ERR "ide-tape: %s: I/O error, "
1083 "pc = %2x, key = %2x, "
1084 "asc = %2x, ascq = %2x\n",
1085 tape->name, pc->c[0],
1086 tape->sense_key, tape->asc,
1087 tape->ascq);
1088 }
1089 /* Giving up */
1090 pc->error = IDETAPE_ERROR_GENERAL;
1091 }
1092 tape->failed_pc = NULL;
Borislav Petkovd236d742008-04-18 00:46:27 +02001093 return pc->idetape_callback(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094 }
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001095 debug_log(DBG_SENSE, "Retry #%d, cmd = %02X\n", pc->retries, pc->c[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096
1097 pc->retries++;
1098 /* We haven't transferred any data yet */
Borislav Petkovd236d742008-04-18 00:46:27 +02001099 pc->xferred = 0;
1100 pc->cur_pos = pc->buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101 /* Request to transfer the entire buffer at once */
Borislav Petkovd236d742008-04-18 00:46:27 +02001102 bcount = pc->req_xfer;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103
Borislav Petkov346331f2008-04-18 00:46:26 +02001104 if (pc->flags & PC_FLAG_DMA_ERROR) {
1105 pc->flags &= ~PC_FLAG_DMA_ERROR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106 printk(KERN_WARNING "ide-tape: DMA disabled, "
1107 "reverting to PIO\n");
Bartlomiej Zolnierkiewicz7469aaf2007-02-17 02:40:26 +01001108 ide_dma_off(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109 }
Borislav Petkov346331f2008-04-18 00:46:26 +02001110 if ((pc->flags & PC_FLAG_DMA_RECOMMENDED) && drive->using_dma)
Bartlomiej Zolnierkiewicz5e37bdc2008-04-26 22:25:24 +02001111 dma_ok = !hwif->dma_ops->dma_setup(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112
Bartlomiej Zolnierkiewicz2fc57382008-01-25 22:17:13 +01001113 ide_pktcmd_tf_load(drive, IDE_TFLAG_NO_SELECT_MASK |
1114 IDE_TFLAG_OUT_DEVICE, bcount, dma_ok);
1115
Borislav Petkov346331f2008-04-18 00:46:26 +02001116 if (dma_ok)
1117 /* Will begin DMA later */
1118 pc->flags |= PC_FLAG_DMA_IN_PROGRESS;
1119 if (test_bit(IDETAPE_FLAG_DRQ_INTERRUPT, &tape->flags)) {
Bartlomiej Zolnierkiewiczc1c9dbc2008-02-02 19:56:44 +01001120 ide_execute_command(drive, WIN_PACKETCMD, &idetape_transfer_pc,
1121 IDETAPE_WAIT_CMD, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122 return ide_started;
1123 } else {
Bartlomiej Zolnierkiewicz23579a22008-04-18 00:46:26 +02001124 hwif->OUTB(WIN_PACKETCMD, hwif->io_ports[IDE_COMMAND_OFFSET]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125 return idetape_transfer_pc(drive);
1126 }
1127}
1128
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001129static ide_startstop_t idetape_pc_callback(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130{
1131 idetape_tape_t *tape = drive->driver_data;
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001132
1133 debug_log(DBG_PROCS, "Enter %s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134
1135 idetape_end_request(drive, tape->pc->error ? 0 : 1, 0);
1136 return ide_stopped;
1137}
1138
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001139/* A mode sense command is used to "sense" tape parameters. */
Borislav Petkovd236d742008-04-18 00:46:27 +02001140static void idetape_create_mode_sense_cmd(struct ide_atapi_pc *pc, u8 page_code)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141{
1142 idetape_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +01001143 pc->c[0] = MODE_SENSE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144 if (page_code != IDETAPE_BLOCK_DESCRIPTOR)
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001145 /* DBD = 1 - Don't return block descriptors */
1146 pc->c[1] = 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147 pc->c[2] = page_code;
1148 /*
1149 * Changed pc->c[3] to 0 (255 will at best return unused info).
1150 *
1151 * For SCSI this byte is defined as subpage instead of high byte
1152 * of length and some IDE drives seem to interpret it this way
1153 * and return an error when 255 is used.
1154 */
1155 pc->c[3] = 0;
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001156 /* We will just discard data in that case */
1157 pc->c[4] = 255;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158 if (page_code == IDETAPE_BLOCK_DESCRIPTOR)
Borislav Petkovd236d742008-04-18 00:46:27 +02001159 pc->req_xfer = 12;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160 else if (page_code == IDETAPE_CAPABILITIES_PAGE)
Borislav Petkovd236d742008-04-18 00:46:27 +02001161 pc->req_xfer = 24;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162 else
Borislav Petkovd236d742008-04-18 00:46:27 +02001163 pc->req_xfer = 50;
1164 pc->idetape_callback = &idetape_pc_callback;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165}
1166
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001167static ide_startstop_t idetape_media_access_finished(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168{
1169 idetape_tape_t *tape = drive->driver_data;
Borislav Petkovd236d742008-04-18 00:46:27 +02001170 struct ide_atapi_pc *pc = tape->pc;
Bartlomiej Zolnierkiewicz22c525b2008-01-25 22:17:11 +01001171 u8 stat;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172
Bartlomiej Zolnierkiewiczc47137a2008-02-06 02:57:51 +01001173 stat = ide_read_status(drive);
1174
Bartlomiej Zolnierkiewicz22c525b2008-01-25 22:17:11 +01001175 if (stat & SEEK_STAT) {
1176 if (stat & ERR_STAT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177 /* Error detected */
Borislav Petkov90699ce2008-02-02 19:56:50 +01001178 if (pc->c[0] != TEST_UNIT_READY)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179 printk(KERN_ERR "ide-tape: %s: I/O error, ",
1180 tape->name);
1181 /* Retry operation */
1182 return idetape_retry_pc(drive);
1183 }
1184 pc->error = 0;
1185 if (tape->failed_pc == pc)
1186 tape->failed_pc = NULL;
1187 } else {
1188 pc->error = IDETAPE_ERROR_GENERAL;
1189 tape->failed_pc = NULL;
1190 }
Borislav Petkovd236d742008-04-18 00:46:27 +02001191 return pc->idetape_callback(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192}
1193
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001194static ide_startstop_t idetape_rw_callback(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195{
1196 idetape_tape_t *tape = drive->driver_data;
1197 struct request *rq = HWGROUP(drive)->rq;
Borislav Petkovd236d742008-04-18 00:46:27 +02001198 int blocks = tape->pc->xferred / tape->blk_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199
Borislav Petkov54bb2072008-02-06 02:57:52 +01001200 tape->avg_size += blocks * tape->blk_size;
1201 tape->insert_size += blocks * tape->blk_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202 if (tape->insert_size > 1024 * 1024)
1203 tape->measure_insert_time = 1;
1204 if (tape->measure_insert_time) {
1205 tape->measure_insert_time = 0;
1206 tape->insert_time = jiffies;
1207 tape->insert_size = 0;
1208 }
1209 if (time_after(jiffies, tape->insert_time))
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001210 tape->insert_speed = tape->insert_size / 1024 * HZ /
1211 (jiffies - tape->insert_time);
Marcelo Feitoza Parisi9bae1ff2006-03-28 01:56:46 -08001212 if (time_after_eq(jiffies, tape->avg_time + HZ)) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001213 tape->avg_speed = tape->avg_size * HZ /
1214 (jiffies - tape->avg_time) / 1024;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215 tape->avg_size = 0;
1216 tape->avg_time = jiffies;
1217 }
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001218 debug_log(DBG_PROCS, "Enter %s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219
Borislav Petkov54bb2072008-02-06 02:57:52 +01001220 tape->first_frame += blocks;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221 rq->current_nr_sectors -= blocks;
1222
1223 if (!tape->pc->error)
1224 idetape_end_request(drive, 1, 0);
1225 else
1226 idetape_end_request(drive, tape->pc->error, 0);
1227 return ide_stopped;
1228}
1229
Borislav Petkovd236d742008-04-18 00:46:27 +02001230static void idetape_create_read_cmd(idetape_tape_t *tape,
1231 struct ide_atapi_pc *pc,
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001232 unsigned int length, struct idetape_bh *bh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233{
1234 idetape_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +01001235 pc->c[0] = READ_6;
Borislav Petkov860ff5e2008-02-02 19:56:50 +01001236 put_unaligned(cpu_to_be32(length), (unsigned int *) &pc->c[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237 pc->c[1] = 1;
Borislav Petkovd236d742008-04-18 00:46:27 +02001238 pc->idetape_callback = &idetape_rw_callback;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239 pc->bh = bh;
1240 atomic_set(&bh->b_count, 0);
Borislav Petkovd236d742008-04-18 00:46:27 +02001241 pc->buf = NULL;
1242 pc->buf_size = length * tape->blk_size;
1243 pc->req_xfer = pc->buf_size;
1244 if (pc->req_xfer == tape->stage_size)
Borislav Petkov346331f2008-04-18 00:46:26 +02001245 pc->flags |= PC_FLAG_DMA_RECOMMENDED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246}
1247
Borislav Petkovd236d742008-04-18 00:46:27 +02001248static void idetape_create_write_cmd(idetape_tape_t *tape,
1249 struct ide_atapi_pc *pc,
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001250 unsigned int length, struct idetape_bh *bh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251{
1252 idetape_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +01001253 pc->c[0] = WRITE_6;
Borislav Petkov860ff5e2008-02-02 19:56:50 +01001254 put_unaligned(cpu_to_be32(length), (unsigned int *) &pc->c[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255 pc->c[1] = 1;
Borislav Petkovd236d742008-04-18 00:46:27 +02001256 pc->idetape_callback = &idetape_rw_callback;
Borislav Petkov346331f2008-04-18 00:46:26 +02001257 pc->flags |= PC_FLAG_WRITING;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258 pc->bh = bh;
1259 pc->b_data = bh->b_data;
1260 pc->b_count = atomic_read(&bh->b_count);
Borislav Petkovd236d742008-04-18 00:46:27 +02001261 pc->buf = NULL;
1262 pc->buf_size = length * tape->blk_size;
1263 pc->req_xfer = pc->buf_size;
1264 if (pc->req_xfer == tape->stage_size)
Borislav Petkov346331f2008-04-18 00:46:26 +02001265 pc->flags |= PC_FLAG_DMA_RECOMMENDED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266}
1267
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268static ide_startstop_t idetape_do_request(ide_drive_t *drive,
1269 struct request *rq, sector_t block)
1270{
1271 idetape_tape_t *tape = drive->driver_data;
Borislav Petkovd236d742008-04-18 00:46:27 +02001272 struct ide_atapi_pc *pc = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273 struct request *postponed_rq = tape->postponed_rq;
Bartlomiej Zolnierkiewicz22c525b2008-01-25 22:17:11 +01001274 u8 stat;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001276 debug_log(DBG_SENSE, "sector: %ld, nr_sectors: %ld,"
1277 " current_nr_sectors: %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278 rq->sector, rq->nr_sectors, rq->current_nr_sectors);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279
Jens Axboe4aff5e22006-08-10 08:44:47 +02001280 if (!blk_special_request(rq)) {
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001281 /* We do not support buffer cache originated requests. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282 printk(KERN_NOTICE "ide-tape: %s: Unsupported request in "
Jens Axboe4aff5e22006-08-10 08:44:47 +02001283 "request queue (%d)\n", drive->name, rq->cmd_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284 ide_end_request(drive, 0, 0);
1285 return ide_stopped;
1286 }
1287
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001288 /* Retry a failed packet command */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001289 if (tape->failed_pc && tape->pc->c[0] == REQUEST_SENSE)
Borislav Petkov8d06bfa2008-02-06 02:57:53 +01001290 return idetape_issue_pc(drive, tape->failed_pc);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001291
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292 if (postponed_rq != NULL)
1293 if (rq != postponed_rq) {
1294 printk(KERN_ERR "ide-tape: ide-tape.c bug - "
1295 "Two DSC requests were queued\n");
1296 idetape_end_request(drive, 0, 0);
1297 return ide_stopped;
1298 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299
1300 tape->postponed_rq = NULL;
1301
1302 /*
1303 * If the tape is still busy, postpone our request and service
1304 * the other device meanwhile.
1305 */
Bartlomiej Zolnierkiewiczc47137a2008-02-06 02:57:51 +01001306 stat = ide_read_status(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307
1308 if (!drive->dsc_overlap && !(rq->cmd[0] & REQ_IDETAPE_PC2))
Borislav Petkov346331f2008-04-18 00:46:26 +02001309 set_bit(IDETAPE_FLAG_IGNORE_DSC, &tape->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310
1311 if (drive->post_reset == 1) {
Borislav Petkov346331f2008-04-18 00:46:26 +02001312 set_bit(IDETAPE_FLAG_IGNORE_DSC, &tape->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313 drive->post_reset = 0;
1314 }
1315
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316 if (time_after(jiffies, tape->insert_time))
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001317 tape->insert_speed = tape->insert_size / 1024 * HZ /
1318 (jiffies - tape->insert_time);
Borislav Petkov346331f2008-04-18 00:46:26 +02001319 if (!test_and_clear_bit(IDETAPE_FLAG_IGNORE_DSC, &tape->flags) &&
Bartlomiej Zolnierkiewicz22c525b2008-01-25 22:17:11 +01001320 (stat & SEEK_STAT) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321 if (postponed_rq == NULL) {
1322 tape->dsc_polling_start = jiffies;
Borislav Petkov54bb2072008-02-06 02:57:52 +01001323 tape->dsc_poll_freq = tape->best_dsc_rw_freq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324 tape->dsc_timeout = jiffies + IDETAPE_DSC_RW_TIMEOUT;
1325 } else if (time_after(jiffies, tape->dsc_timeout)) {
1326 printk(KERN_ERR "ide-tape: %s: DSC timeout\n",
1327 tape->name);
1328 if (rq->cmd[0] & REQ_IDETAPE_PC2) {
1329 idetape_media_access_finished(drive);
1330 return ide_stopped;
1331 } else {
1332 return ide_do_reset(drive);
1333 }
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001334 } else if (time_after(jiffies,
1335 tape->dsc_polling_start +
1336 IDETAPE_DSC_MA_THRESHOLD))
Borislav Petkov54bb2072008-02-06 02:57:52 +01001337 tape->dsc_poll_freq = IDETAPE_DSC_MA_SLOW;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338 idetape_postpone_request(drive);
1339 return ide_stopped;
1340 }
1341 if (rq->cmd[0] & REQ_IDETAPE_READ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342 tape->postpone_cnt = 0;
1343 pc = idetape_next_pc_storage(drive);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001344 idetape_create_read_cmd(tape, pc, rq->current_nr_sectors,
1345 (struct idetape_bh *)rq->special);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346 goto out;
1347 }
1348 if (rq->cmd[0] & REQ_IDETAPE_WRITE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349 tape->postpone_cnt = 0;
1350 pc = idetape_next_pc_storage(drive);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001351 idetape_create_write_cmd(tape, pc, rq->current_nr_sectors,
1352 (struct idetape_bh *)rq->special);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353 goto out;
1354 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355 if (rq->cmd[0] & REQ_IDETAPE_PC1) {
Borislav Petkovd236d742008-04-18 00:46:27 +02001356 pc = (struct ide_atapi_pc *) rq->buffer;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357 rq->cmd[0] &= ~(REQ_IDETAPE_PC1);
1358 rq->cmd[0] |= REQ_IDETAPE_PC2;
1359 goto out;
1360 }
1361 if (rq->cmd[0] & REQ_IDETAPE_PC2) {
1362 idetape_media_access_finished(drive);
1363 return ide_stopped;
1364 }
1365 BUG();
1366out:
Borislav Petkov8d06bfa2008-02-06 02:57:53 +01001367 return idetape_issue_pc(drive, pc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368}
1369
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001370/* Pipeline related functions */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371
1372/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001373 * The function below uses __get_free_page to allocate a pipeline stage, along
1374 * with all the necessary small buffers which together make a buffer of size
1375 * tape->stage_size (or a bit more). We attempt to combine sequential pages as
1376 * much as possible.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001378 * It returns a pointer to the new allocated stage, or NULL if we can't (or
1379 * don't want to) allocate a stage.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001381 * Pipeline stages are optional and are used to increase performance. If we
1382 * can't allocate them, we'll manage without them.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383 */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001384static idetape_stage_t *__idetape_kmalloc_stage(idetape_tape_t *tape, int full,
1385 int clear)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386{
1387 idetape_stage_t *stage;
1388 struct idetape_bh *prev_bh, *bh;
1389 int pages = tape->pages_per_stage;
1390 char *b_data = NULL;
1391
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001392 stage = kmalloc(sizeof(idetape_stage_t), GFP_KERNEL);
1393 if (!stage)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394 return NULL;
1395 stage->next = NULL;
1396
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001397 stage->bh = kmalloc(sizeof(struct idetape_bh), GFP_KERNEL);
1398 bh = stage->bh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399 if (bh == NULL)
1400 goto abort;
1401 bh->b_reqnext = NULL;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001402 bh->b_data = (char *) __get_free_page(GFP_KERNEL);
1403 if (!bh->b_data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404 goto abort;
1405 if (clear)
1406 memset(bh->b_data, 0, PAGE_SIZE);
1407 bh->b_size = PAGE_SIZE;
1408 atomic_set(&bh->b_count, full ? bh->b_size : 0);
1409
1410 while (--pages) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001411 b_data = (char *) __get_free_page(GFP_KERNEL);
1412 if (!b_data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413 goto abort;
1414 if (clear)
1415 memset(b_data, 0, PAGE_SIZE);
1416 if (bh->b_data == b_data + PAGE_SIZE) {
1417 bh->b_size += PAGE_SIZE;
1418 bh->b_data -= PAGE_SIZE;
1419 if (full)
1420 atomic_add(PAGE_SIZE, &bh->b_count);
1421 continue;
1422 }
1423 if (b_data == bh->b_data + bh->b_size) {
1424 bh->b_size += PAGE_SIZE;
1425 if (full)
1426 atomic_add(PAGE_SIZE, &bh->b_count);
1427 continue;
1428 }
1429 prev_bh = bh;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001430 bh = kmalloc(sizeof(struct idetape_bh), GFP_KERNEL);
1431 if (!bh) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432 free_page((unsigned long) b_data);
1433 goto abort;
1434 }
1435 bh->b_reqnext = NULL;
1436 bh->b_data = b_data;
1437 bh->b_size = PAGE_SIZE;
1438 atomic_set(&bh->b_count, full ? bh->b_size : 0);
1439 prev_bh->b_reqnext = bh;
1440 }
1441 bh->b_size -= tape->excess_bh_size;
1442 if (full)
1443 atomic_sub(tape->excess_bh_size, &bh->b_count);
1444 return stage;
1445abort:
1446 __idetape_kfree_stage(stage);
1447 return NULL;
1448}
1449
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001450static int idetape_copy_stage_from_user(idetape_tape_t *tape,
Borislav Petkov8646c882008-04-27 15:38:26 +02001451 const char __user *buf, int n)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452{
1453 struct idetape_bh *bh = tape->bh;
1454 int count;
Daniel Walkerdcd96372006-06-25 05:47:37 -07001455 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456
1457 while (n) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458 if (bh == NULL) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001459 printk(KERN_ERR "ide-tape: bh == NULL in %s\n",
1460 __func__);
Daniel Walkerdcd96372006-06-25 05:47:37 -07001461 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462 }
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001463 count = min((unsigned int)
1464 (bh->b_size - atomic_read(&bh->b_count)),
1465 (unsigned int)n);
1466 if (copy_from_user(bh->b_data + atomic_read(&bh->b_count), buf,
1467 count))
Daniel Walkerdcd96372006-06-25 05:47:37 -07001468 ret = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469 n -= count;
1470 atomic_add(count, &bh->b_count);
1471 buf += count;
1472 if (atomic_read(&bh->b_count) == bh->b_size) {
1473 bh = bh->b_reqnext;
1474 if (bh)
1475 atomic_set(&bh->b_count, 0);
1476 }
1477 }
1478 tape->bh = bh;
Daniel Walkerdcd96372006-06-25 05:47:37 -07001479 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480}
1481
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001482static int idetape_copy_stage_to_user(idetape_tape_t *tape, char __user *buf,
Borislav Petkov99d74e62008-04-27 15:38:25 +02001483 int n)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001484{
1485 struct idetape_bh *bh = tape->bh;
1486 int count;
Daniel Walkerdcd96372006-06-25 05:47:37 -07001487 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488
1489 while (n) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490 if (bh == NULL) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001491 printk(KERN_ERR "ide-tape: bh == NULL in %s\n",
1492 __func__);
Daniel Walkerdcd96372006-06-25 05:47:37 -07001493 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495 count = min(tape->b_count, n);
Daniel Walkerdcd96372006-06-25 05:47:37 -07001496 if (copy_to_user(buf, tape->b_data, count))
1497 ret = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498 n -= count;
1499 tape->b_data += count;
1500 tape->b_count -= count;
1501 buf += count;
1502 if (!tape->b_count) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001503 bh = bh->b_reqnext;
1504 tape->bh = bh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505 if (bh) {
1506 tape->b_data = bh->b_data;
1507 tape->b_count = atomic_read(&bh->b_count);
1508 }
1509 }
1510 }
Daniel Walkerdcd96372006-06-25 05:47:37 -07001511 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512}
1513
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001514static void idetape_init_merge_stage(idetape_tape_t *tape)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515{
1516 struct idetape_bh *bh = tape->merge_stage->bh;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001517
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518 tape->bh = bh;
Borislav Petkov54abf372008-02-06 02:57:52 +01001519 if (tape->chrdev_dir == IDETAPE_DIR_WRITE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520 atomic_set(&bh->b_count, 0);
1521 else {
1522 tape->b_data = bh->b_data;
1523 tape->b_count = atomic_read(&bh->b_count);
1524 }
1525}
1526
Borislav Petkova2f5b7f2008-02-06 02:57:51 +01001527static ide_startstop_t idetape_read_position_callback(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001528{
1529 idetape_tape_t *tape = drive->driver_data;
Borislav Petkovd236d742008-04-18 00:46:27 +02001530 u8 *readpos = tape->pc->buf;
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001531
1532 debug_log(DBG_PROCS, "Enter %s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533
1534 if (!tape->pc->error) {
Borislav Petkova2f5b7f2008-02-06 02:57:51 +01001535 debug_log(DBG_SENSE, "BOP - %s\n",
1536 (readpos[0] & 0x80) ? "Yes" : "No");
1537 debug_log(DBG_SENSE, "EOP - %s\n",
1538 (readpos[0] & 0x40) ? "Yes" : "No");
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001539
Borislav Petkova2f5b7f2008-02-06 02:57:51 +01001540 if (readpos[0] & 0x4) {
1541 printk(KERN_INFO "ide-tape: Block location is unknown"
1542 "to the tape\n");
Borislav Petkov346331f2008-04-18 00:46:26 +02001543 clear_bit(IDETAPE_FLAG_ADDRESS_VALID, &tape->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544 idetape_end_request(drive, 0, 0);
1545 } else {
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001546 debug_log(DBG_SENSE, "Block Location - %u\n",
Borislav Petkova2f5b7f2008-02-06 02:57:51 +01001547 be32_to_cpu(*(u32 *)&readpos[4]));
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001548
Borislav Petkova2f5b7f2008-02-06 02:57:51 +01001549 tape->partition = readpos[1];
Borislav Petkov54bb2072008-02-06 02:57:52 +01001550 tape->first_frame =
Borislav Petkova2f5b7f2008-02-06 02:57:51 +01001551 be32_to_cpu(*(u32 *)&readpos[4]);
Borislav Petkov346331f2008-04-18 00:46:26 +02001552 set_bit(IDETAPE_FLAG_ADDRESS_VALID, &tape->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553 idetape_end_request(drive, 1, 0);
1554 }
1555 } else {
1556 idetape_end_request(drive, 0, 0);
1557 }
1558 return ide_stopped;
1559}
1560
1561/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001562 * Write a filemark if write_filemark=1. Flush the device buffers without
1563 * writing a filemark otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001564 */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001565static void idetape_create_write_filemark_cmd(ide_drive_t *drive,
Borislav Petkovd236d742008-04-18 00:46:27 +02001566 struct ide_atapi_pc *pc, int write_filemark)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567{
1568 idetape_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +01001569 pc->c[0] = WRITE_FILEMARKS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570 pc->c[4] = write_filemark;
Borislav Petkov346331f2008-04-18 00:46:26 +02001571 pc->flags |= PC_FLAG_WAIT_FOR_DSC;
Borislav Petkovd236d742008-04-18 00:46:27 +02001572 pc->idetape_callback = &idetape_pc_callback;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001573}
1574
Borislav Petkovd236d742008-04-18 00:46:27 +02001575static void idetape_create_test_unit_ready_cmd(struct ide_atapi_pc *pc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001576{
1577 idetape_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +01001578 pc->c[0] = TEST_UNIT_READY;
Borislav Petkovd236d742008-04-18 00:46:27 +02001579 pc->idetape_callback = &idetape_pc_callback;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001580}
1581
1582/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001583 * We add a special packet command request to the tail of the request queue, and
1584 * wait for it to be serviced. This is not to be called from within the request
1585 * handling part of the driver! We allocate here data on the stack and it is
1586 * valid until the request is finished. This is not the case for the bottom part
1587 * of the driver, where we are always leaving the functions to wait for an
1588 * interrupt or a timer event.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001589 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001590 * From the bottom part of the driver, we should allocate safe memory using
1591 * idetape_next_pc_storage() and ide_tape_next_rq_storage(), and add the request
1592 * to the request list without waiting for it to be serviced! In that case, we
1593 * usually use idetape_queue_pc_head().
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594 */
Borislav Petkovea1ab3d2008-04-27 15:38:27 +02001595static int idetape_queue_pc_tail(ide_drive_t *drive, struct ide_atapi_pc *pc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596{
1597 struct ide_tape_obj *tape = drive->driver_data;
1598 struct request rq;
1599
1600 idetape_init_rq(&rq, REQ_IDETAPE_PC1);
1601 rq.buffer = (char *) pc;
1602 rq.rq_disk = tape->disk;
1603 return ide_do_drive_cmd(drive, &rq, ide_wait);
1604}
1605
Borislav Petkovd236d742008-04-18 00:46:27 +02001606static void idetape_create_load_unload_cmd(ide_drive_t *drive,
1607 struct ide_atapi_pc *pc, int cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608{
1609 idetape_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +01001610 pc->c[0] = START_STOP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001611 pc->c[4] = cmd;
Borislav Petkov346331f2008-04-18 00:46:26 +02001612 pc->flags |= PC_FLAG_WAIT_FOR_DSC;
Borislav Petkovd236d742008-04-18 00:46:27 +02001613 pc->idetape_callback = &idetape_pc_callback;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001614}
1615
1616static int idetape_wait_ready(ide_drive_t *drive, unsigned long timeout)
1617{
1618 idetape_tape_t *tape = drive->driver_data;
Borislav Petkovd236d742008-04-18 00:46:27 +02001619 struct ide_atapi_pc pc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001620 int load_attempted = 0;
1621
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001622 /* Wait for the tape to become ready */
Borislav Petkov346331f2008-04-18 00:46:26 +02001623 set_bit(IDETAPE_FLAG_MEDIUM_PRESENT, &tape->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001624 timeout += jiffies;
1625 while (time_before(jiffies, timeout)) {
1626 idetape_create_test_unit_ready_cmd(&pc);
Borislav Petkovea1ab3d2008-04-27 15:38:27 +02001627 if (!idetape_queue_pc_tail(drive, &pc))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001628 return 0;
1629 if ((tape->sense_key == 2 && tape->asc == 4 && tape->ascq == 2)
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001630 || (tape->asc == 0x3A)) {
1631 /* no media */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632 if (load_attempted)
1633 return -ENOMEDIUM;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001634 idetape_create_load_unload_cmd(drive, &pc,
1635 IDETAPE_LU_LOAD_MASK);
Borislav Petkovea1ab3d2008-04-27 15:38:27 +02001636 idetape_queue_pc_tail(drive, &pc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001637 load_attempted = 1;
1638 /* not about to be ready */
1639 } else if (!(tape->sense_key == 2 && tape->asc == 4 &&
1640 (tape->ascq == 1 || tape->ascq == 8)))
1641 return -EIO;
Nishanth Aravamudan80ce45f2005-09-10 00:27:08 -07001642 msleep(100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643 }
1644 return -EIO;
1645}
1646
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001647static int idetape_flush_tape_buffers(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001648{
Borislav Petkovd236d742008-04-18 00:46:27 +02001649 struct ide_atapi_pc pc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001650 int rc;
1651
1652 idetape_create_write_filemark_cmd(drive, &pc, 0);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001653 rc = idetape_queue_pc_tail(drive, &pc);
1654 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001655 return rc;
1656 idetape_wait_ready(drive, 60 * 5 * HZ);
1657 return 0;
1658}
1659
Borislav Petkovd236d742008-04-18 00:46:27 +02001660static void idetape_create_read_position_cmd(struct ide_atapi_pc *pc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001661{
1662 idetape_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +01001663 pc->c[0] = READ_POSITION;
Borislav Petkovd236d742008-04-18 00:46:27 +02001664 pc->req_xfer = 20;
1665 pc->idetape_callback = &idetape_read_position_callback;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001666}
1667
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001668static int idetape_read_position(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001669{
1670 idetape_tape_t *tape = drive->driver_data;
Borislav Petkovd236d742008-04-18 00:46:27 +02001671 struct ide_atapi_pc pc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001672 int position;
1673
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001674 debug_log(DBG_PROCS, "Enter %s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675
1676 idetape_create_read_position_cmd(&pc);
1677 if (idetape_queue_pc_tail(drive, &pc))
1678 return -1;
Borislav Petkov54bb2072008-02-06 02:57:52 +01001679 position = tape->first_frame;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001680 return position;
1681}
1682
Borislav Petkovd236d742008-04-18 00:46:27 +02001683static void idetape_create_locate_cmd(ide_drive_t *drive,
1684 struct ide_atapi_pc *pc,
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001685 unsigned int block, u8 partition, int skip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001686{
1687 idetape_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +01001688 pc->c[0] = POSITION_TO_ELEMENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001689 pc->c[1] = 2;
Borislav Petkov860ff5e2008-02-02 19:56:50 +01001690 put_unaligned(cpu_to_be32(block), (unsigned int *) &pc->c[3]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001691 pc->c[8] = partition;
Borislav Petkov346331f2008-04-18 00:46:26 +02001692 pc->flags |= PC_FLAG_WAIT_FOR_DSC;
Borislav Petkovd236d742008-04-18 00:46:27 +02001693 pc->idetape_callback = &idetape_pc_callback;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694}
1695
Borislav Petkovd236d742008-04-18 00:46:27 +02001696static int idetape_create_prevent_cmd(ide_drive_t *drive,
1697 struct ide_atapi_pc *pc, int prevent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001698{
1699 idetape_tape_t *tape = drive->driver_data;
1700
Borislav Petkovb6422012008-02-02 19:56:49 +01001701 /* device supports locking according to capabilities page */
1702 if (!(tape->caps[6] & 0x01))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001703 return 0;
1704
1705 idetape_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +01001706 pc->c[0] = ALLOW_MEDIUM_REMOVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001707 pc->c[4] = prevent;
Borislav Petkovd236d742008-04-18 00:46:27 +02001708 pc->idetape_callback = &idetape_pc_callback;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001709 return 1;
1710}
1711
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001712static int __idetape_discard_read_pipeline(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713{
1714 idetape_tape_t *tape = drive->driver_data;
1715 unsigned long flags;
1716 int cnt;
1717
Borislav Petkov54abf372008-02-06 02:57:52 +01001718 if (tape->chrdev_dir != IDETAPE_DIR_READ)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001719 return 0;
1720
1721 /* Remove merge stage. */
Borislav Petkov54bb2072008-02-06 02:57:52 +01001722 cnt = tape->merge_stage_size / tape->blk_size;
Borislav Petkov346331f2008-04-18 00:46:26 +02001723 if (test_and_clear_bit(IDETAPE_FLAG_FILEMARK, &tape->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001724 ++cnt; /* Filemarks count as 1 sector */
1725 tape->merge_stage_size = 0;
1726 if (tape->merge_stage != NULL) {
1727 __idetape_kfree_stage(tape->merge_stage);
1728 tape->merge_stage = NULL;
1729 }
1730
1731 /* Clear pipeline flags. */
Borislav Petkov346331f2008-04-18 00:46:26 +02001732 clear_bit(IDETAPE_FLAG_PIPELINE_ERR, &tape->flags);
Borislav Petkov54abf372008-02-06 02:57:52 +01001733 tape->chrdev_dir = IDETAPE_DIR_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001734
1735 /* Remove pipeline stages. */
1736 if (tape->first_stage == NULL)
1737 return 0;
1738
Borislav Petkov54bb2072008-02-06 02:57:52 +01001739 spin_lock_irqsave(&tape->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001740 tape->next_stage = NULL;
Borislav Petkov54bb2072008-02-06 02:57:52 +01001741 spin_unlock_irqrestore(&tape->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001742
1743 while (tape->first_stage != NULL) {
1744 struct request *rq_ptr = &tape->first_stage->rq;
1745
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001746 cnt += rq_ptr->nr_sectors - rq_ptr->current_nr_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001747 if (rq_ptr->errors == IDETAPE_ERROR_FILEMARK)
1748 ++cnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001749 }
1750 tape->nr_pending_stages = 0;
1751 tape->max_stages = tape->min_pipeline;
1752 return cnt;
1753}
1754
1755/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001756 * Position the tape to the requested block using the LOCATE packet command.
1757 * A READ POSITION command is then issued to check where we are positioned. Like
1758 * all higher level operations, we queue the commands at the tail of the request
1759 * queue and wait for their completion.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001760 */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001761static int idetape_position_tape(ide_drive_t *drive, unsigned int block,
1762 u8 partition, int skip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001763{
1764 idetape_tape_t *tape = drive->driver_data;
1765 int retval;
Borislav Petkovd236d742008-04-18 00:46:27 +02001766 struct ide_atapi_pc pc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767
Borislav Petkov54abf372008-02-06 02:57:52 +01001768 if (tape->chrdev_dir == IDETAPE_DIR_READ)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001769 __idetape_discard_read_pipeline(drive);
1770 idetape_wait_ready(drive, 60 * 5 * HZ);
1771 idetape_create_locate_cmd(drive, &pc, block, partition, skip);
1772 retval = idetape_queue_pc_tail(drive, &pc);
1773 if (retval)
1774 return (retval);
1775
1776 idetape_create_read_position_cmd(&pc);
1777 return (idetape_queue_pc_tail(drive, &pc));
1778}
1779
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001780static void idetape_discard_read_pipeline(ide_drive_t *drive,
1781 int restore_position)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001782{
1783 idetape_tape_t *tape = drive->driver_data;
1784 int cnt;
1785 int seek, position;
1786
1787 cnt = __idetape_discard_read_pipeline(drive);
1788 if (restore_position) {
1789 position = idetape_read_position(drive);
1790 seek = position > cnt ? position - cnt : 0;
1791 if (idetape_position_tape(drive, seek, 0, 0)) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001792 printk(KERN_INFO "ide-tape: %s: position_tape failed in"
1793 " discard_pipeline()\n", tape->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001794 return;
1795 }
1796 }
1797}
1798
1799/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001800 * Generate a read/write request for the block device interface and wait for it
1801 * to be serviced.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001802 */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001803static int idetape_queue_rw_tail(ide_drive_t *drive, int cmd, int blocks,
1804 struct idetape_bh *bh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805{
1806 idetape_tape_t *tape = drive->driver_data;
1807 struct request rq;
1808
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001809 debug_log(DBG_SENSE, "%s: cmd=%d\n", __func__, cmd);
1810
Borislav Petkovc4b22f82008-04-26 22:25:20 +02001811 if (test_bit(IDETAPE_FLAG_PIPELINE_ACTIVE, &tape->flags)) {
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001812 printk(KERN_ERR "ide-tape: bug: the pipeline is active in %s\n",
1813 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001814 return (0);
1815 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001816
1817 idetape_init_rq(&rq, cmd);
1818 rq.rq_disk = tape->disk;
1819 rq.special = (void *)bh;
Borislav Petkov54bb2072008-02-06 02:57:52 +01001820 rq.sector = tape->first_frame;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001821 rq.nr_sectors = blocks;
1822 rq.current_nr_sectors = blocks;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001823 (void) ide_do_drive_cmd(drive, &rq, ide_wait);
1824
1825 if ((cmd & (REQ_IDETAPE_READ | REQ_IDETAPE_WRITE)) == 0)
1826 return 0;
1827
1828 if (tape->merge_stage)
1829 idetape_init_merge_stage(tape);
1830 if (rq.errors == IDETAPE_ERROR_GENERAL)
1831 return -EIO;
Borislav Petkov54bb2072008-02-06 02:57:52 +01001832 return (tape->blk_size * (blocks-rq.current_nr_sectors));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001833}
1834
Borislav Petkovd236d742008-04-18 00:46:27 +02001835static void idetape_create_inquiry_cmd(struct ide_atapi_pc *pc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001836{
1837 idetape_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +01001838 pc->c[0] = INQUIRY;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001839 pc->c[4] = 254;
Borislav Petkovd236d742008-04-18 00:46:27 +02001840 pc->req_xfer = 254;
1841 pc->idetape_callback = &idetape_pc_callback;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001842}
1843
Borislav Petkovd236d742008-04-18 00:46:27 +02001844static void idetape_create_rewind_cmd(ide_drive_t *drive,
1845 struct ide_atapi_pc *pc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001846{
1847 idetape_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +01001848 pc->c[0] = REZERO_UNIT;
Borislav Petkov346331f2008-04-18 00:46:26 +02001849 pc->flags |= PC_FLAG_WAIT_FOR_DSC;
Borislav Petkovd236d742008-04-18 00:46:27 +02001850 pc->idetape_callback = &idetape_pc_callback;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001851}
1852
Borislav Petkovd236d742008-04-18 00:46:27 +02001853static void idetape_create_erase_cmd(struct ide_atapi_pc *pc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001854{
1855 idetape_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +01001856 pc->c[0] = ERASE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001857 pc->c[1] = 1;
Borislav Petkov346331f2008-04-18 00:46:26 +02001858 pc->flags |= PC_FLAG_WAIT_FOR_DSC;
Borislav Petkovd236d742008-04-18 00:46:27 +02001859 pc->idetape_callback = &idetape_pc_callback;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001860}
1861
Borislav Petkovd236d742008-04-18 00:46:27 +02001862static void idetape_create_space_cmd(struct ide_atapi_pc *pc, int count, u8 cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001863{
1864 idetape_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +01001865 pc->c[0] = SPACE;
Borislav Petkov860ff5e2008-02-02 19:56:50 +01001866 put_unaligned(cpu_to_be32(count), (unsigned int *) &pc->c[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001867 pc->c[1] = cmd;
Borislav Petkov346331f2008-04-18 00:46:26 +02001868 pc->flags |= PC_FLAG_WAIT_FOR_DSC;
Borislav Petkovd236d742008-04-18 00:46:27 +02001869 pc->idetape_callback = &idetape_pc_callback;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001870}
1871
Borislav Petkov97c566c2008-04-27 15:38:25 +02001872/* Queue up a character device originated write request. */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001873static int idetape_add_chrdev_write_request(ide_drive_t *drive, int blocks)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001874{
1875 idetape_tape_t *tape = drive->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001876
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001877 debug_log(DBG_CHRDEV, "Enter %s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001878
Borislav Petkov0aa4b012008-04-27 15:38:27 +02001879 return idetape_queue_rw_tail(drive, REQ_IDETAPE_WRITE,
1880 blocks, tape->merge_stage->bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001881}
1882
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001883static void idetape_empty_write_pipeline(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001884{
1885 idetape_tape_t *tape = drive->driver_data;
1886 int blocks, min;
1887 struct idetape_bh *bh;
Borislav Petkov55a5d292008-02-02 19:56:49 +01001888
Borislav Petkov54abf372008-02-06 02:57:52 +01001889 if (tape->chrdev_dir != IDETAPE_DIR_WRITE) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001890 printk(KERN_ERR "ide-tape: bug: Trying to empty write pipeline,"
1891 " but we are not writing.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001892 return;
1893 }
1894 if (tape->merge_stage_size > tape->stage_size) {
1895 printk(KERN_ERR "ide-tape: bug: merge_buffer too big\n");
1896 tape->merge_stage_size = tape->stage_size;
1897 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001898 if (tape->merge_stage_size) {
Borislav Petkov54bb2072008-02-06 02:57:52 +01001899 blocks = tape->merge_stage_size / tape->blk_size;
1900 if (tape->merge_stage_size % tape->blk_size) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001901 unsigned int i;
1902
1903 blocks++;
Borislav Petkov54bb2072008-02-06 02:57:52 +01001904 i = tape->blk_size - tape->merge_stage_size %
1905 tape->blk_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001906 bh = tape->bh->b_reqnext;
1907 while (bh) {
1908 atomic_set(&bh->b_count, 0);
1909 bh = bh->b_reqnext;
1910 }
1911 bh = tape->bh;
1912 while (i) {
1913 if (bh == NULL) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001914 printk(KERN_INFO "ide-tape: bug,"
1915 " bh NULL\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001916 break;
1917 }
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001918 min = min(i, (unsigned int)(bh->b_size -
1919 atomic_read(&bh->b_count)));
1920 memset(bh->b_data + atomic_read(&bh->b_count),
1921 0, min);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001922 atomic_add(min, &bh->b_count);
1923 i -= min;
1924 bh = bh->b_reqnext;
1925 }
1926 }
1927 (void) idetape_add_chrdev_write_request(drive, blocks);
1928 tape->merge_stage_size = 0;
1929 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001930 if (tape->merge_stage != NULL) {
1931 __idetape_kfree_stage(tape->merge_stage);
1932 tape->merge_stage = NULL;
1933 }
Borislav Petkov346331f2008-04-18 00:46:26 +02001934 clear_bit(IDETAPE_FLAG_PIPELINE_ERR, &tape->flags);
Borislav Petkov54abf372008-02-06 02:57:52 +01001935 tape->chrdev_dir = IDETAPE_DIR_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001936
1937 /*
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001938 * On the next backup, perform the feedback loop again. (I don't want to
1939 * keep sense information between backups, as some systems are
1940 * constantly on, and the system load can be totally different on the
1941 * next backup).
Linus Torvalds1da177e2005-04-16 15:20:36 -07001942 */
1943 tape->max_stages = tape->min_pipeline;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001944 if (tape->first_stage != NULL ||
1945 tape->next_stage != NULL ||
1946 tape->last_stage != NULL ||
1947 tape->nr_stages != 0) {
1948 printk(KERN_ERR "ide-tape: ide-tape pipeline bug, "
1949 "first_stage %p, next_stage %p, "
1950 "last_stage %p, nr_stages %d\n",
1951 tape->first_stage, tape->next_stage,
1952 tape->last_stage, tape->nr_stages);
1953 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001954}
1955
Borislav Petkov8d06bfa2008-02-06 02:57:53 +01001956static int idetape_init_read(ide_drive_t *drive, int max_stages)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001957{
1958 idetape_tape_t *tape = drive->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001959 int bytes_read;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001960
1961 /* Initialize read operation */
Borislav Petkov54abf372008-02-06 02:57:52 +01001962 if (tape->chrdev_dir != IDETAPE_DIR_READ) {
1963 if (tape->chrdev_dir == IDETAPE_DIR_WRITE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001964 idetape_empty_write_pipeline(drive);
1965 idetape_flush_tape_buffers(drive);
1966 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001967 if (tape->merge_stage || tape->merge_stage_size) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001968 printk(KERN_ERR "ide-tape: merge_stage_size should be"
1969 " 0 now\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001970 tape->merge_stage_size = 0;
1971 }
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001972 tape->merge_stage = __idetape_kmalloc_stage(tape, 0, 0);
1973 if (!tape->merge_stage)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001974 return -ENOMEM;
Borislav Petkov54abf372008-02-06 02:57:52 +01001975 tape->chrdev_dir = IDETAPE_DIR_READ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001976
1977 /*
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001978 * Issue a read 0 command to ensure that DSC handshake is
1979 * switched from completion mode to buffer available mode.
1980 * No point in issuing this if DSC overlap isn't supported, some
1981 * drives (Seagate STT3401A) will return an error.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001982 */
1983 if (drive->dsc_overlap) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001984 bytes_read = idetape_queue_rw_tail(drive,
1985 REQ_IDETAPE_READ, 0,
1986 tape->merge_stage->bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001987 if (bytes_read < 0) {
1988 __idetape_kfree_stage(tape->merge_stage);
1989 tape->merge_stage = NULL;
Borislav Petkov54abf372008-02-06 02:57:52 +01001990 tape->chrdev_dir = IDETAPE_DIR_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001991 return bytes_read;
1992 }
1993 }
1994 }
Borislav Petkov5e69bd92008-04-27 15:38:25 +02001995
Borislav Petkovc4b22f82008-04-26 22:25:20 +02001996 if (!test_bit(IDETAPE_FLAG_PIPELINE_ACTIVE, &tape->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001997 if (tape->nr_pending_stages >= 3 * max_stages / 4) {
1998 tape->measure_insert_time = 1;
1999 tape->insert_time = jiffies;
2000 tape->insert_size = 0;
2001 tape->insert_speed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002002 }
2003 }
2004 return 0;
2005}
2006
2007/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +01002008 * Called from idetape_chrdev_read() to service a character device read request
2009 * and add read-ahead requests to our pipeline.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002010 */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002011static int idetape_add_chrdev_read_request(ide_drive_t *drive, int blocks)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002012{
2013 idetape_tape_t *tape = drive->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002014
Borislav Petkov8004a8c2008-02-06 02:57:51 +01002015 debug_log(DBG_PROCS, "Enter %s, %d blocks\n", __func__, blocks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002016
Borislav Petkov3c98bf32008-02-06 02:57:53 +01002017 /* If we are at a filemark, return a read length of 0 */
Borislav Petkov346331f2008-04-18 00:46:26 +02002018 if (test_bit(IDETAPE_FLAG_FILEMARK, &tape->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002019 return 0;
2020
Borislav Petkov8d06bfa2008-02-06 02:57:53 +01002021 idetape_init_read(drive, tape->max_stages);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002022
Borislav Petkov5e69bd92008-04-27 15:38:25 +02002023 if (test_bit(IDETAPE_FLAG_PIPELINE_ERR, &tape->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002024 return 0;
Borislav Petkov5e69bd92008-04-27 15:38:25 +02002025
2026 return idetape_queue_rw_tail(drive, REQ_IDETAPE_READ, blocks,
2027 tape->merge_stage->bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002028}
2029
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002030static void idetape_pad_zeros(ide_drive_t *drive, int bcount)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002031{
2032 idetape_tape_t *tape = drive->driver_data;
2033 struct idetape_bh *bh;
2034 int blocks;
Borislav Petkov3c98bf32008-02-06 02:57:53 +01002035
Linus Torvalds1da177e2005-04-16 15:20:36 -07002036 while (bcount) {
2037 unsigned int count;
2038
2039 bh = tape->merge_stage->bh;
2040 count = min(tape->stage_size, bcount);
2041 bcount -= count;
Borislav Petkov54bb2072008-02-06 02:57:52 +01002042 blocks = count / tape->blk_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002043 while (count) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002044 atomic_set(&bh->b_count,
2045 min(count, (unsigned int)bh->b_size));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002046 memset(bh->b_data, 0, atomic_read(&bh->b_count));
2047 count -= atomic_read(&bh->b_count);
2048 bh = bh->b_reqnext;
2049 }
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002050 idetape_queue_rw_tail(drive, REQ_IDETAPE_WRITE, blocks,
2051 tape->merge_stage->bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002052 }
2053}
2054
Linus Torvalds1da177e2005-04-16 15:20:36 -07002055/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +01002056 * Rewinds the tape to the Beginning Of the current Partition (BOP). We
2057 * currently support only one partition.
2058 */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002059static int idetape_rewind_tape(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002060{
2061 int retval;
Borislav Petkovd236d742008-04-18 00:46:27 +02002062 struct ide_atapi_pc pc;
Borislav Petkov8004a8c2008-02-06 02:57:51 +01002063 idetape_tape_t *tape;
2064 tape = drive->driver_data;
2065
2066 debug_log(DBG_SENSE, "Enter %s\n", __func__);
2067
Linus Torvalds1da177e2005-04-16 15:20:36 -07002068 idetape_create_rewind_cmd(drive, &pc);
2069 retval = idetape_queue_pc_tail(drive, &pc);
2070 if (retval)
2071 return retval;
2072
2073 idetape_create_read_position_cmd(&pc);
2074 retval = idetape_queue_pc_tail(drive, &pc);
2075 if (retval)
2076 return retval;
2077 return 0;
2078}
2079
Borislav Petkov3c98bf32008-02-06 02:57:53 +01002080/* mtio.h compatible commands should be issued to the chrdev interface. */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002081static int idetape_blkdev_ioctl(ide_drive_t *drive, unsigned int cmd,
2082 unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002083{
2084 idetape_tape_t *tape = drive->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002085 void __user *argp = (void __user *)arg;
2086
Borislav Petkovd59823f2008-02-02 19:56:51 +01002087 struct idetape_config {
2088 int dsc_rw_frequency;
2089 int dsc_media_access_frequency;
2090 int nr_stages;
2091 } config;
2092
Borislav Petkov8004a8c2008-02-06 02:57:51 +01002093 debug_log(DBG_PROCS, "Enter %s\n", __func__);
2094
Linus Torvalds1da177e2005-04-16 15:20:36 -07002095 switch (cmd) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002096 case 0x0340:
2097 if (copy_from_user(&config, argp, sizeof(config)))
2098 return -EFAULT;
2099 tape->best_dsc_rw_freq = config.dsc_rw_frequency;
2100 tape->max_stages = config.nr_stages;
2101 break;
2102 case 0x0350:
2103 config.dsc_rw_frequency = (int) tape->best_dsc_rw_freq;
2104 config.nr_stages = tape->max_stages;
2105 if (copy_to_user(argp, &config, sizeof(config)))
2106 return -EFAULT;
2107 break;
2108 default:
2109 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002110 }
2111 return 0;
2112}
2113
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002114static int idetape_space_over_filemarks(ide_drive_t *drive, short mt_op,
2115 int mt_count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002116{
2117 idetape_tape_t *tape = drive->driver_data;
Borislav Petkovd236d742008-04-18 00:46:27 +02002118 struct ide_atapi_pc pc;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002119 int retval, count = 0;
Borislav Petkovb6422012008-02-02 19:56:49 +01002120 int sprev = !!(tape->caps[4] & 0x20);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002121
2122 if (mt_count == 0)
2123 return 0;
2124 if (MTBSF == mt_op || MTBSFM == mt_op) {
Borislav Petkovb6422012008-02-02 19:56:49 +01002125 if (!sprev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002126 return -EIO;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002127 mt_count = -mt_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002128 }
2129
Borislav Petkov54abf372008-02-06 02:57:52 +01002130 if (tape->chrdev_dir == IDETAPE_DIR_READ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002131 tape->merge_stage_size = 0;
Borislav Petkov346331f2008-04-18 00:46:26 +02002132 if (test_and_clear_bit(IDETAPE_FLAG_FILEMARK, &tape->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002133 ++count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002134 idetape_discard_read_pipeline(drive, 0);
2135 }
2136
2137 /*
Borislav Petkov3c98bf32008-02-06 02:57:53 +01002138 * The filemark was not found in our internal pipeline; now we can issue
2139 * the space command.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002140 */
2141 switch (mt_op) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002142 case MTFSF:
2143 case MTBSF:
2144 idetape_create_space_cmd(&pc, mt_count - count,
2145 IDETAPE_SPACE_OVER_FILEMARK);
2146 return idetape_queue_pc_tail(drive, &pc);
2147 case MTFSFM:
2148 case MTBSFM:
2149 if (!sprev)
2150 return -EIO;
2151 retval = idetape_space_over_filemarks(drive, MTFSF,
2152 mt_count - count);
2153 if (retval)
2154 return retval;
2155 count = (MTBSFM == mt_op ? 1 : -1);
2156 return idetape_space_over_filemarks(drive, MTFSF, count);
2157 default:
2158 printk(KERN_ERR "ide-tape: MTIO operation %d not supported\n",
2159 mt_op);
2160 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002161 }
2162}
2163
Linus Torvalds1da177e2005-04-16 15:20:36 -07002164/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +01002165 * Our character device read / write functions.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002166 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +01002167 * The tape is optimized to maximize throughput when it is transferring an
2168 * integral number of the "continuous transfer limit", which is a parameter of
2169 * the specific tape (26kB on my particular tape, 32kB for Onstream).
Linus Torvalds1da177e2005-04-16 15:20:36 -07002170 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +01002171 * As of version 1.3 of the driver, the character device provides an abstract
2172 * continuous view of the media - any mix of block sizes (even 1 byte) on the
2173 * same backup/restore procedure is supported. The driver will internally
2174 * convert the requests to the recommended transfer unit, so that an unmatch
2175 * between the user's block size to the recommended size will only result in a
2176 * (slightly) increased driver overhead, but will no longer hit performance.
2177 * This is not applicable to Onstream.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002178 */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002179static ssize_t idetape_chrdev_read(struct file *file, char __user *buf,
2180 size_t count, loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002181{
2182 struct ide_tape_obj *tape = ide_tape_f(file);
2183 ide_drive_t *drive = tape->drive;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002184 ssize_t bytes_read, temp, actually_read = 0, rc;
Daniel Walkerdcd96372006-06-25 05:47:37 -07002185 ssize_t ret = 0;
Borislav Petkovb6422012008-02-02 19:56:49 +01002186 u16 ctl = *(u16 *)&tape->caps[12];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002187
Borislav Petkov8004a8c2008-02-06 02:57:51 +01002188 debug_log(DBG_CHRDEV, "Enter %s, count %Zd\n", __func__, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002189
Borislav Petkov54abf372008-02-06 02:57:52 +01002190 if (tape->chrdev_dir != IDETAPE_DIR_READ) {
Borislav Petkov346331f2008-04-18 00:46:26 +02002191 if (test_bit(IDETAPE_FLAG_DETECT_BS, &tape->flags))
Borislav Petkov54bb2072008-02-06 02:57:52 +01002192 if (count > tape->blk_size &&
2193 (count % tape->blk_size) == 0)
2194 tape->user_bs_factor = count / tape->blk_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002195 }
Borislav Petkov8d06bfa2008-02-06 02:57:53 +01002196 rc = idetape_init_read(drive, tape->max_stages);
2197 if (rc < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002198 return rc;
2199 if (count == 0)
2200 return (0);
2201 if (tape->merge_stage_size) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002202 actually_read = min((unsigned int)(tape->merge_stage_size),
2203 (unsigned int)count);
Borislav Petkov99d74e62008-04-27 15:38:25 +02002204 if (idetape_copy_stage_to_user(tape, buf, actually_read))
Daniel Walkerdcd96372006-06-25 05:47:37 -07002205 ret = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002206 buf += actually_read;
2207 tape->merge_stage_size -= actually_read;
2208 count -= actually_read;
2209 }
2210 while (count >= tape->stage_size) {
Borislav Petkovb6422012008-02-02 19:56:49 +01002211 bytes_read = idetape_add_chrdev_read_request(drive, ctl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002212 if (bytes_read <= 0)
2213 goto finish;
Borislav Petkov99d74e62008-04-27 15:38:25 +02002214 if (idetape_copy_stage_to_user(tape, buf, bytes_read))
Daniel Walkerdcd96372006-06-25 05:47:37 -07002215 ret = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002216 buf += bytes_read;
2217 count -= bytes_read;
2218 actually_read += bytes_read;
2219 }
2220 if (count) {
Borislav Petkovb6422012008-02-02 19:56:49 +01002221 bytes_read = idetape_add_chrdev_read_request(drive, ctl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002222 if (bytes_read <= 0)
2223 goto finish;
2224 temp = min((unsigned long)count, (unsigned long)bytes_read);
Borislav Petkov99d74e62008-04-27 15:38:25 +02002225 if (idetape_copy_stage_to_user(tape, buf, temp))
Daniel Walkerdcd96372006-06-25 05:47:37 -07002226 ret = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002227 actually_read += temp;
2228 tape->merge_stage_size = bytes_read-temp;
2229 }
2230finish:
Borislav Petkov346331f2008-04-18 00:46:26 +02002231 if (!actually_read && test_bit(IDETAPE_FLAG_FILEMARK, &tape->flags)) {
Borislav Petkov8004a8c2008-02-06 02:57:51 +01002232 debug_log(DBG_SENSE, "%s: spacing over filemark\n", tape->name);
2233
Linus Torvalds1da177e2005-04-16 15:20:36 -07002234 idetape_space_over_filemarks(drive, MTFSF, 1);
2235 return 0;
2236 }
Daniel Walkerdcd96372006-06-25 05:47:37 -07002237
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002238 return ret ? ret : actually_read;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002239}
2240
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002241static ssize_t idetape_chrdev_write(struct file *file, const char __user *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002242 size_t count, loff_t *ppos)
2243{
2244 struct ide_tape_obj *tape = ide_tape_f(file);
2245 ide_drive_t *drive = tape->drive;
Daniel Walkerdcd96372006-06-25 05:47:37 -07002246 ssize_t actually_written = 0;
2247 ssize_t ret = 0;
Borislav Petkovb6422012008-02-02 19:56:49 +01002248 u16 ctl = *(u16 *)&tape->caps[12];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002249
2250 /* The drive is write protected. */
2251 if (tape->write_prot)
2252 return -EACCES;
2253
Borislav Petkov8004a8c2008-02-06 02:57:51 +01002254 debug_log(DBG_CHRDEV, "Enter %s, count %Zd\n", __func__, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002255
2256 /* Initialize write operation */
Borislav Petkov54abf372008-02-06 02:57:52 +01002257 if (tape->chrdev_dir != IDETAPE_DIR_WRITE) {
2258 if (tape->chrdev_dir == IDETAPE_DIR_READ)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002259 idetape_discard_read_pipeline(drive, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002260 if (tape->merge_stage || tape->merge_stage_size) {
2261 printk(KERN_ERR "ide-tape: merge_stage_size "
2262 "should be 0 now\n");
2263 tape->merge_stage_size = 0;
2264 }
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002265 tape->merge_stage = __idetape_kmalloc_stage(tape, 0, 0);
2266 if (!tape->merge_stage)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002267 return -ENOMEM;
Borislav Petkov54abf372008-02-06 02:57:52 +01002268 tape->chrdev_dir = IDETAPE_DIR_WRITE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002269 idetape_init_merge_stage(tape);
2270
2271 /*
Borislav Petkov3c98bf32008-02-06 02:57:53 +01002272 * Issue a write 0 command to ensure that DSC handshake is
2273 * switched from completion mode to buffer available mode. No
2274 * point in issuing this if DSC overlap isn't supported, some
2275 * drives (Seagate STT3401A) will return an error.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002276 */
2277 if (drive->dsc_overlap) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002278 ssize_t retval = idetape_queue_rw_tail(drive,
2279 REQ_IDETAPE_WRITE, 0,
2280 tape->merge_stage->bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002281 if (retval < 0) {
2282 __idetape_kfree_stage(tape->merge_stage);
2283 tape->merge_stage = NULL;
Borislav Petkov54abf372008-02-06 02:57:52 +01002284 tape->chrdev_dir = IDETAPE_DIR_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002285 return retval;
2286 }
2287 }
2288 }
2289 if (count == 0)
2290 return (0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002291 if (tape->merge_stage_size) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002292 if (tape->merge_stage_size >= tape->stage_size) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002293 printk(KERN_ERR "ide-tape: bug: merge buf too big\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002294 tape->merge_stage_size = 0;
2295 }
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002296 actually_written = min((unsigned int)
2297 (tape->stage_size - tape->merge_stage_size),
2298 (unsigned int)count);
Borislav Petkov8646c882008-04-27 15:38:26 +02002299 if (idetape_copy_stage_from_user(tape, buf, actually_written))
Daniel Walkerdcd96372006-06-25 05:47:37 -07002300 ret = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002301 buf += actually_written;
2302 tape->merge_stage_size += actually_written;
2303 count -= actually_written;
2304
2305 if (tape->merge_stage_size == tape->stage_size) {
Daniel Walkerdcd96372006-06-25 05:47:37 -07002306 ssize_t retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002307 tape->merge_stage_size = 0;
Borislav Petkovb6422012008-02-02 19:56:49 +01002308 retval = idetape_add_chrdev_write_request(drive, ctl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002309 if (retval <= 0)
2310 return (retval);
2311 }
2312 }
2313 while (count >= tape->stage_size) {
Daniel Walkerdcd96372006-06-25 05:47:37 -07002314 ssize_t retval;
Borislav Petkov8646c882008-04-27 15:38:26 +02002315 if (idetape_copy_stage_from_user(tape, buf, tape->stage_size))
Daniel Walkerdcd96372006-06-25 05:47:37 -07002316 ret = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002317 buf += tape->stage_size;
2318 count -= tape->stage_size;
Borislav Petkovb6422012008-02-02 19:56:49 +01002319 retval = idetape_add_chrdev_write_request(drive, ctl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002320 actually_written += tape->stage_size;
2321 if (retval <= 0)
2322 return (retval);
2323 }
2324 if (count) {
2325 actually_written += count;
Borislav Petkov8646c882008-04-27 15:38:26 +02002326 if (idetape_copy_stage_from_user(tape, buf, count))
Daniel Walkerdcd96372006-06-25 05:47:37 -07002327 ret = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002328 tape->merge_stage_size += count;
2329 }
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002330 return ret ? ret : actually_written;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002331}
2332
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002333static int idetape_write_filemark(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002334{
Borislav Petkovd236d742008-04-18 00:46:27 +02002335 struct ide_atapi_pc pc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002336
2337 /* Write a filemark */
2338 idetape_create_write_filemark_cmd(drive, &pc, 1);
2339 if (idetape_queue_pc_tail(drive, &pc)) {
2340 printk(KERN_ERR "ide-tape: Couldn't write a filemark\n");
2341 return -EIO;
2342 }
2343 return 0;
2344}
2345
2346/*
Borislav Petkovd99c9da2008-02-02 19:56:51 +01002347 * Called from idetape_chrdev_ioctl when the general mtio MTIOCTOP ioctl is
2348 * requested.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002349 *
Borislav Petkovd99c9da2008-02-02 19:56:51 +01002350 * Note: MTBSF and MTBSFM are not supported when the tape doesn't support
2351 * spacing over filemarks in the reverse direction. In this case, MTFSFM is also
2352 * usually not supported (it is supported in the rare case in which we crossed
2353 * the filemark during our read-ahead pipelined operation mode).
Linus Torvalds1da177e2005-04-16 15:20:36 -07002354 *
Borislav Petkovd99c9da2008-02-02 19:56:51 +01002355 * The following commands are currently not supported:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002356 *
Borislav Petkovd99c9da2008-02-02 19:56:51 +01002357 * MTFSS, MTBSS, MTWSM, MTSETDENSITY, MTSETDRVBUFFER, MT_ST_BOOLEANS,
2358 * MT_ST_WRITE_THRESHOLD.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002359 */
Borislav Petkovd99c9da2008-02-02 19:56:51 +01002360static int idetape_mtioctop(ide_drive_t *drive, short mt_op, int mt_count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002361{
2362 idetape_tape_t *tape = drive->driver_data;
Borislav Petkovd236d742008-04-18 00:46:27 +02002363 struct ide_atapi_pc pc;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002364 int i, retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002365
Borislav Petkov8004a8c2008-02-06 02:57:51 +01002366 debug_log(DBG_ERR, "Handling MTIOCTOP ioctl: mt_op=%d, mt_count=%d\n",
2367 mt_op, mt_count);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002368
Borislav Petkov3c98bf32008-02-06 02:57:53 +01002369 /* Commands which need our pipelined read-ahead stages. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002370 switch (mt_op) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002371 case MTFSF:
2372 case MTFSFM:
2373 case MTBSF:
2374 case MTBSFM:
2375 if (!mt_count)
2376 return 0;
2377 return idetape_space_over_filemarks(drive, mt_op, mt_count);
2378 default:
2379 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002380 }
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002381
Linus Torvalds1da177e2005-04-16 15:20:36 -07002382 switch (mt_op) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002383 case MTWEOF:
2384 if (tape->write_prot)
2385 return -EACCES;
2386 idetape_discard_read_pipeline(drive, 1);
2387 for (i = 0; i < mt_count; i++) {
2388 retval = idetape_write_filemark(drive);
2389 if (retval)
2390 return retval;
2391 }
2392 return 0;
2393 case MTREW:
2394 idetape_discard_read_pipeline(drive, 0);
2395 if (idetape_rewind_tape(drive))
2396 return -EIO;
2397 return 0;
2398 case MTLOAD:
2399 idetape_discard_read_pipeline(drive, 0);
2400 idetape_create_load_unload_cmd(drive, &pc,
2401 IDETAPE_LU_LOAD_MASK);
2402 return idetape_queue_pc_tail(drive, &pc);
2403 case MTUNLOAD:
2404 case MTOFFL:
2405 /*
2406 * If door is locked, attempt to unlock before
2407 * attempting to eject.
2408 */
2409 if (tape->door_locked) {
2410 if (idetape_create_prevent_cmd(drive, &pc, 0))
2411 if (!idetape_queue_pc_tail(drive, &pc))
2412 tape->door_locked = DOOR_UNLOCKED;
2413 }
2414 idetape_discard_read_pipeline(drive, 0);
2415 idetape_create_load_unload_cmd(drive, &pc,
2416 !IDETAPE_LU_LOAD_MASK);
2417 retval = idetape_queue_pc_tail(drive, &pc);
2418 if (!retval)
Borislav Petkov346331f2008-04-18 00:46:26 +02002419 clear_bit(IDETAPE_FLAG_MEDIUM_PRESENT, &tape->flags);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002420 return retval;
2421 case MTNOP:
2422 idetape_discard_read_pipeline(drive, 0);
2423 return idetape_flush_tape_buffers(drive);
2424 case MTRETEN:
2425 idetape_discard_read_pipeline(drive, 0);
2426 idetape_create_load_unload_cmd(drive, &pc,
2427 IDETAPE_LU_RETENSION_MASK | IDETAPE_LU_LOAD_MASK);
2428 return idetape_queue_pc_tail(drive, &pc);
2429 case MTEOM:
2430 idetape_create_space_cmd(&pc, 0, IDETAPE_SPACE_TO_EOD);
2431 return idetape_queue_pc_tail(drive, &pc);
2432 case MTERASE:
2433 (void)idetape_rewind_tape(drive);
2434 idetape_create_erase_cmd(&pc);
2435 return idetape_queue_pc_tail(drive, &pc);
2436 case MTSETBLK:
2437 if (mt_count) {
2438 if (mt_count < tape->blk_size ||
2439 mt_count % tape->blk_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002440 return -EIO;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002441 tape->user_bs_factor = mt_count / tape->blk_size;
Borislav Petkov346331f2008-04-18 00:46:26 +02002442 clear_bit(IDETAPE_FLAG_DETECT_BS, &tape->flags);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002443 } else
Borislav Petkov346331f2008-04-18 00:46:26 +02002444 set_bit(IDETAPE_FLAG_DETECT_BS, &tape->flags);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002445 return 0;
2446 case MTSEEK:
2447 idetape_discard_read_pipeline(drive, 0);
2448 return idetape_position_tape(drive,
2449 mt_count * tape->user_bs_factor, tape->partition, 0);
2450 case MTSETPART:
2451 idetape_discard_read_pipeline(drive, 0);
2452 return idetape_position_tape(drive, 0, mt_count, 0);
2453 case MTFSR:
2454 case MTBSR:
2455 case MTLOCK:
2456 if (!idetape_create_prevent_cmd(drive, &pc, 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002457 return 0;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002458 retval = idetape_queue_pc_tail(drive, &pc);
2459 if (retval)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002460 return retval;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002461 tape->door_locked = DOOR_EXPLICITLY_LOCKED;
2462 return 0;
2463 case MTUNLOCK:
2464 if (!idetape_create_prevent_cmd(drive, &pc, 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002465 return 0;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002466 retval = idetape_queue_pc_tail(drive, &pc);
2467 if (retval)
2468 return retval;
2469 tape->door_locked = DOOR_UNLOCKED;
2470 return 0;
2471 default:
2472 printk(KERN_ERR "ide-tape: MTIO operation %d not supported\n",
2473 mt_op);
2474 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002475 }
2476}
2477
2478/*
Borislav Petkovd99c9da2008-02-02 19:56:51 +01002479 * Our character device ioctls. General mtio.h magnetic io commands are
2480 * supported here, and not in the corresponding block interface. Our own
2481 * ide-tape ioctls are supported on both interfaces.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002482 */
Borislav Petkovd99c9da2008-02-02 19:56:51 +01002483static int idetape_chrdev_ioctl(struct inode *inode, struct file *file,
2484 unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002485{
2486 struct ide_tape_obj *tape = ide_tape_f(file);
2487 ide_drive_t *drive = tape->drive;
2488 struct mtop mtop;
2489 struct mtget mtget;
2490 struct mtpos mtpos;
Borislav Petkov54bb2072008-02-06 02:57:52 +01002491 int block_offset = 0, position = tape->first_frame;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002492 void __user *argp = (void __user *)arg;
2493
Borislav Petkov8004a8c2008-02-06 02:57:51 +01002494 debug_log(DBG_CHRDEV, "Enter %s, cmd=%u\n", __func__, cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002495
Borislav Petkov54abf372008-02-06 02:57:52 +01002496 if (tape->chrdev_dir == IDETAPE_DIR_WRITE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002497 idetape_empty_write_pipeline(drive);
2498 idetape_flush_tape_buffers(drive);
2499 }
2500 if (cmd == MTIOCGET || cmd == MTIOCPOS) {
Borislav Petkovb361acb2008-04-27 15:38:26 +02002501 block_offset = tape->merge_stage_size /
Borislav Petkov54bb2072008-02-06 02:57:52 +01002502 (tape->blk_size * tape->user_bs_factor);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002503 position = idetape_read_position(drive);
2504 if (position < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002505 return -EIO;
2506 }
2507 switch (cmd) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002508 case MTIOCTOP:
2509 if (copy_from_user(&mtop, argp, sizeof(struct mtop)))
2510 return -EFAULT;
2511 return idetape_mtioctop(drive, mtop.mt_op, mtop.mt_count);
2512 case MTIOCGET:
2513 memset(&mtget, 0, sizeof(struct mtget));
2514 mtget.mt_type = MT_ISSCSI2;
2515 mtget.mt_blkno = position / tape->user_bs_factor - block_offset;
2516 mtget.mt_dsreg =
2517 ((tape->blk_size * tape->user_bs_factor)
2518 << MT_ST_BLKSIZE_SHIFT) & MT_ST_BLKSIZE_MASK;
Borislav Petkov54bb2072008-02-06 02:57:52 +01002519
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002520 if (tape->drv_write_prot)
2521 mtget.mt_gstat |= GMT_WR_PROT(0xffffffff);
2522
2523 if (copy_to_user(argp, &mtget, sizeof(struct mtget)))
2524 return -EFAULT;
2525 return 0;
2526 case MTIOCPOS:
2527 mtpos.mt_blkno = position / tape->user_bs_factor - block_offset;
2528 if (copy_to_user(argp, &mtpos, sizeof(struct mtpos)))
2529 return -EFAULT;
2530 return 0;
2531 default:
2532 if (tape->chrdev_dir == IDETAPE_DIR_READ)
2533 idetape_discard_read_pipeline(drive, 1);
2534 return idetape_blkdev_ioctl(drive, cmd, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002535 }
2536}
2537
Borislav Petkov3cffb9c2008-02-02 19:56:50 +01002538/*
2539 * Do a mode sense page 0 with block descriptor and if it succeeds set the tape
2540 * block size with the reported value.
2541 */
2542static void ide_tape_get_bsize_from_bdesc(ide_drive_t *drive)
2543{
2544 idetape_tape_t *tape = drive->driver_data;
Borislav Petkovd236d742008-04-18 00:46:27 +02002545 struct ide_atapi_pc pc;
Borislav Petkov3cffb9c2008-02-02 19:56:50 +01002546
2547 idetape_create_mode_sense_cmd(&pc, IDETAPE_BLOCK_DESCRIPTOR);
2548 if (idetape_queue_pc_tail(drive, &pc)) {
2549 printk(KERN_ERR "ide-tape: Can't get block descriptor\n");
Borislav Petkov54bb2072008-02-06 02:57:52 +01002550 if (tape->blk_size == 0) {
Borislav Petkov3cffb9c2008-02-02 19:56:50 +01002551 printk(KERN_WARNING "ide-tape: Cannot deal with zero "
2552 "block size, assuming 32k\n");
Borislav Petkov54bb2072008-02-06 02:57:52 +01002553 tape->blk_size = 32768;
Borislav Petkov3cffb9c2008-02-02 19:56:50 +01002554 }
2555 return;
2556 }
Borislav Petkovd236d742008-04-18 00:46:27 +02002557 tape->blk_size = (pc.buf[4 + 5] << 16) +
2558 (pc.buf[4 + 6] << 8) +
2559 pc.buf[4 + 7];
2560 tape->drv_write_prot = (pc.buf[2] & 0x80) >> 7;
Borislav Petkov3cffb9c2008-02-02 19:56:50 +01002561}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002562
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002563static int idetape_chrdev_open(struct inode *inode, struct file *filp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002564{
2565 unsigned int minor = iminor(inode), i = minor & ~0xc0;
2566 ide_drive_t *drive;
2567 idetape_tape_t *tape;
Borislav Petkovd236d742008-04-18 00:46:27 +02002568 struct ide_atapi_pc pc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002569 int retval;
2570
Borislav Petkov8004a8c2008-02-06 02:57:51 +01002571 if (i >= MAX_HWIFS * MAX_DRIVES)
2572 return -ENXIO;
2573
2574 tape = ide_tape_chrdev_get(i);
2575 if (!tape)
2576 return -ENXIO;
2577
2578 debug_log(DBG_CHRDEV, "Enter %s\n", __func__);
2579
Linus Torvalds1da177e2005-04-16 15:20:36 -07002580 /*
2581 * We really want to do nonseekable_open(inode, filp); here, but some
2582 * versions of tar incorrectly call lseek on tapes and bail out if that
2583 * fails. So we disallow pread() and pwrite(), but permit lseeks.
2584 */
2585 filp->f_mode &= ~(FMODE_PREAD | FMODE_PWRITE);
2586
Linus Torvalds1da177e2005-04-16 15:20:36 -07002587 drive = tape->drive;
2588
2589 filp->private_data = tape;
2590
Borislav Petkov346331f2008-04-18 00:46:26 +02002591 if (test_and_set_bit(IDETAPE_FLAG_BUSY, &tape->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002592 retval = -EBUSY;
2593 goto out_put_tape;
2594 }
2595
2596 retval = idetape_wait_ready(drive, 60 * HZ);
2597 if (retval) {
Borislav Petkov346331f2008-04-18 00:46:26 +02002598 clear_bit(IDETAPE_FLAG_BUSY, &tape->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002599 printk(KERN_ERR "ide-tape: %s: drive not ready\n", tape->name);
2600 goto out_put_tape;
2601 }
2602
2603 idetape_read_position(drive);
Borislav Petkov346331f2008-04-18 00:46:26 +02002604 if (!test_bit(IDETAPE_FLAG_ADDRESS_VALID, &tape->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002605 (void)idetape_rewind_tape(drive);
2606
Borislav Petkov54abf372008-02-06 02:57:52 +01002607 if (tape->chrdev_dir != IDETAPE_DIR_READ)
Borislav Petkov346331f2008-04-18 00:46:26 +02002608 clear_bit(IDETAPE_FLAG_PIPELINE_ERR, &tape->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002609
2610 /* Read block size and write protect status from drive. */
Borislav Petkov3cffb9c2008-02-02 19:56:50 +01002611 ide_tape_get_bsize_from_bdesc(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002612
2613 /* Set write protect flag if device is opened as read-only. */
2614 if ((filp->f_flags & O_ACCMODE) == O_RDONLY)
2615 tape->write_prot = 1;
2616 else
2617 tape->write_prot = tape->drv_write_prot;
2618
2619 /* Make sure drive isn't write protected if user wants to write. */
2620 if (tape->write_prot) {
2621 if ((filp->f_flags & O_ACCMODE) == O_WRONLY ||
2622 (filp->f_flags & O_ACCMODE) == O_RDWR) {
Borislav Petkov346331f2008-04-18 00:46:26 +02002623 clear_bit(IDETAPE_FLAG_BUSY, &tape->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002624 retval = -EROFS;
2625 goto out_put_tape;
2626 }
2627 }
2628
Borislav Petkov3c98bf32008-02-06 02:57:53 +01002629 /* Lock the tape drive door so user can't eject. */
Borislav Petkov54abf372008-02-06 02:57:52 +01002630 if (tape->chrdev_dir == IDETAPE_DIR_NONE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002631 if (idetape_create_prevent_cmd(drive, &pc, 1)) {
2632 if (!idetape_queue_pc_tail(drive, &pc)) {
2633 if (tape->door_locked != DOOR_EXPLICITLY_LOCKED)
2634 tape->door_locked = DOOR_LOCKED;
2635 }
2636 }
2637 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002638 return 0;
2639
2640out_put_tape:
2641 ide_tape_put(tape);
2642 return retval;
2643}
2644
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002645static void idetape_write_release(ide_drive_t *drive, unsigned int minor)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002646{
2647 idetape_tape_t *tape = drive->driver_data;
2648
2649 idetape_empty_write_pipeline(drive);
2650 tape->merge_stage = __idetape_kmalloc_stage(tape, 1, 0);
2651 if (tape->merge_stage != NULL) {
Borislav Petkov54bb2072008-02-06 02:57:52 +01002652 idetape_pad_zeros(drive, tape->blk_size *
2653 (tape->user_bs_factor - 1));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002654 __idetape_kfree_stage(tape->merge_stage);
2655 tape->merge_stage = NULL;
2656 }
2657 idetape_write_filemark(drive);
2658 idetape_flush_tape_buffers(drive);
2659 idetape_flush_tape_buffers(drive);
2660}
2661
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002662static int idetape_chrdev_release(struct inode *inode, struct file *filp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002663{
2664 struct ide_tape_obj *tape = ide_tape_f(filp);
2665 ide_drive_t *drive = tape->drive;
Borislav Petkovd236d742008-04-18 00:46:27 +02002666 struct ide_atapi_pc pc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002667 unsigned int minor = iminor(inode);
2668
2669 lock_kernel();
2670 tape = drive->driver_data;
Borislav Petkov8004a8c2008-02-06 02:57:51 +01002671
2672 debug_log(DBG_CHRDEV, "Enter %s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002673
Borislav Petkov54abf372008-02-06 02:57:52 +01002674 if (tape->chrdev_dir == IDETAPE_DIR_WRITE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002675 idetape_write_release(drive, minor);
Borislav Petkov54abf372008-02-06 02:57:52 +01002676 if (tape->chrdev_dir == IDETAPE_DIR_READ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002677 if (minor < 128)
2678 idetape_discard_read_pipeline(drive, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002679 }
Borislav Petkovf64eee72008-04-27 15:38:25 +02002680
Borislav Petkov346331f2008-04-18 00:46:26 +02002681 if (minor < 128 && test_bit(IDETAPE_FLAG_MEDIUM_PRESENT, &tape->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002682 (void) idetape_rewind_tape(drive);
Borislav Petkov54abf372008-02-06 02:57:52 +01002683 if (tape->chrdev_dir == IDETAPE_DIR_NONE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002684 if (tape->door_locked == DOOR_LOCKED) {
2685 if (idetape_create_prevent_cmd(drive, &pc, 0)) {
2686 if (!idetape_queue_pc_tail(drive, &pc))
2687 tape->door_locked = DOOR_UNLOCKED;
2688 }
2689 }
2690 }
Borislav Petkov346331f2008-04-18 00:46:26 +02002691 clear_bit(IDETAPE_FLAG_BUSY, &tape->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002692 ide_tape_put(tape);
2693 unlock_kernel();
2694 return 0;
2695}
2696
2697/*
Borislav Petkov71071b82008-02-06 02:57:53 +01002698 * check the contents of the ATAPI IDENTIFY command results. We return:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002699 *
Borislav Petkov71071b82008-02-06 02:57:53 +01002700 * 1 - If the tape can be supported by us, based on the information we have so
2701 * far.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002702 *
Borislav Petkov71071b82008-02-06 02:57:53 +01002703 * 0 - If this tape driver is not currently supported by us.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002704 */
Borislav Petkov71071b82008-02-06 02:57:53 +01002705static int idetape_identify_device(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002706{
Borislav Petkov71071b82008-02-06 02:57:53 +01002707 u8 gcw[2], protocol, device_type, removable, packet_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002708
2709 if (drive->id_read == 0)
2710 return 1;
2711
Borislav Petkov71071b82008-02-06 02:57:53 +01002712 *((unsigned short *) &gcw) = drive->id->config;
2713
2714 protocol = (gcw[1] & 0xC0) >> 6;
2715 device_type = gcw[1] & 0x1F;
2716 removable = !!(gcw[0] & 0x80);
2717 packet_size = gcw[0] & 0x3;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002718
Linus Torvalds1da177e2005-04-16 15:20:36 -07002719 /* Check that we can support this device */
Borislav Petkov71071b82008-02-06 02:57:53 +01002720 if (protocol != 2)
Bartlomiej Zolnierkiewicz16422de32008-02-02 19:56:48 +01002721 printk(KERN_ERR "ide-tape: Protocol (0x%02x) is not ATAPI\n",
Borislav Petkov71071b82008-02-06 02:57:53 +01002722 protocol);
2723 else if (device_type != 1)
Bartlomiej Zolnierkiewicz16422de32008-02-02 19:56:48 +01002724 printk(KERN_ERR "ide-tape: Device type (0x%02x) is not set "
Borislav Petkov71071b82008-02-06 02:57:53 +01002725 "to tape\n", device_type);
2726 else if (!removable)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002727 printk(KERN_ERR "ide-tape: The removable flag is not set\n");
Borislav Petkov71071b82008-02-06 02:57:53 +01002728 else if (packet_size != 0) {
Borislav Petkov24d57f82008-02-06 02:57:54 +01002729 printk(KERN_ERR "ide-tape: Packet size (0x%02x) is not 12"
2730 " bytes\n", packet_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002731 } else
2732 return 1;
2733 return 0;
2734}
2735
Borislav Petkov6d29c8f2008-02-02 19:56:49 +01002736static void idetape_get_inquiry_results(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002737{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002738 idetape_tape_t *tape = drive->driver_data;
Borislav Petkovd236d742008-04-18 00:46:27 +02002739 struct ide_atapi_pc pc;
Borislav Petkov41f81d542008-02-06 02:57:52 +01002740 char fw_rev[6], vendor_id[10], product_id[18];
Borislav Petkov6d29c8f2008-02-02 19:56:49 +01002741
Linus Torvalds1da177e2005-04-16 15:20:36 -07002742 idetape_create_inquiry_cmd(&pc);
2743 if (idetape_queue_pc_tail(drive, &pc)) {
Borislav Petkov6d29c8f2008-02-02 19:56:49 +01002744 printk(KERN_ERR "ide-tape: %s: can't get INQUIRY results\n",
2745 tape->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002746 return;
2747 }
Borislav Petkovd236d742008-04-18 00:46:27 +02002748 memcpy(vendor_id, &pc.buf[8], 8);
2749 memcpy(product_id, &pc.buf[16], 16);
2750 memcpy(fw_rev, &pc.buf[32], 4);
Borislav Petkov6d29c8f2008-02-02 19:56:49 +01002751
Borislav Petkov41f81d542008-02-06 02:57:52 +01002752 ide_fixstring(vendor_id, 10, 0);
2753 ide_fixstring(product_id, 18, 0);
2754 ide_fixstring(fw_rev, 6, 0);
2755
Borislav Petkov6d29c8f2008-02-02 19:56:49 +01002756 printk(KERN_INFO "ide-tape: %s <-> %s: %s %s rev %s\n",
Borislav Petkov41f81d542008-02-06 02:57:52 +01002757 drive->name, tape->name, vendor_id, product_id, fw_rev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002758}
2759
2760/*
Borislav Petkovb6422012008-02-02 19:56:49 +01002761 * Ask the tape about its various parameters. In particular, we will adjust our
2762 * data transfer buffer size to the recommended value as returned by the tape.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002763 */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002764static void idetape_get_mode_sense_results(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002765{
2766 idetape_tape_t *tape = drive->driver_data;
Borislav Petkovd236d742008-04-18 00:46:27 +02002767 struct ide_atapi_pc pc;
Borislav Petkovb6422012008-02-02 19:56:49 +01002768 u8 *caps;
2769 u8 speed, max_speed;
Borislav Petkov47314fa2008-02-02 19:56:48 +01002770
Linus Torvalds1da177e2005-04-16 15:20:36 -07002771 idetape_create_mode_sense_cmd(&pc, IDETAPE_CAPABILITIES_PAGE);
2772 if (idetape_queue_pc_tail(drive, &pc)) {
Borislav Petkovb6422012008-02-02 19:56:49 +01002773 printk(KERN_ERR "ide-tape: Can't get tape parameters - assuming"
2774 " some default values\n");
Borislav Petkov54bb2072008-02-06 02:57:52 +01002775 tape->blk_size = 512;
Borislav Petkovb6422012008-02-02 19:56:49 +01002776 put_unaligned(52, (u16 *)&tape->caps[12]);
2777 put_unaligned(540, (u16 *)&tape->caps[14]);
2778 put_unaligned(6*52, (u16 *)&tape->caps[16]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002779 return;
2780 }
Borislav Petkovd236d742008-04-18 00:46:27 +02002781 caps = pc.buf + 4 + pc.buf[3];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002782
Borislav Petkovb6422012008-02-02 19:56:49 +01002783 /* convert to host order and save for later use */
2784 speed = be16_to_cpu(*(u16 *)&caps[14]);
2785 max_speed = be16_to_cpu(*(u16 *)&caps[8]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002786
Borislav Petkovb6422012008-02-02 19:56:49 +01002787 put_unaligned(max_speed, (u16 *)&caps[8]);
2788 put_unaligned(be16_to_cpu(*(u16 *)&caps[12]), (u16 *)&caps[12]);
2789 put_unaligned(speed, (u16 *)&caps[14]);
2790 put_unaligned(be16_to_cpu(*(u16 *)&caps[16]), (u16 *)&caps[16]);
2791
2792 if (!speed) {
2793 printk(KERN_INFO "ide-tape: %s: invalid tape speed "
2794 "(assuming 650KB/sec)\n", drive->name);
2795 put_unaligned(650, (u16 *)&caps[14]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002796 }
Borislav Petkovb6422012008-02-02 19:56:49 +01002797 if (!max_speed) {
2798 printk(KERN_INFO "ide-tape: %s: invalid max_speed "
2799 "(assuming 650KB/sec)\n", drive->name);
2800 put_unaligned(650, (u16 *)&caps[8]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002801 }
2802
Borislav Petkovb6422012008-02-02 19:56:49 +01002803 memcpy(&tape->caps, caps, 20);
2804 if (caps[7] & 0x02)
Borislav Petkov54bb2072008-02-06 02:57:52 +01002805 tape->blk_size = 512;
Borislav Petkovb6422012008-02-02 19:56:49 +01002806 else if (caps[7] & 0x04)
Borislav Petkov54bb2072008-02-06 02:57:52 +01002807 tape->blk_size = 1024;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002808}
2809
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +02002810#ifdef CONFIG_IDE_PROC_FS
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002811static void idetape_add_settings(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002812{
2813 idetape_tape_t *tape = drive->driver_data;
2814
Borislav Petkovb6422012008-02-02 19:56:49 +01002815 ide_add_setting(drive, "buffer", SETTING_READ, TYPE_SHORT, 0, 0xffff,
2816 1, 2, (u16 *)&tape->caps[16], NULL);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002817 ide_add_setting(drive, "pipeline_min", SETTING_RW, TYPE_INT, 1, 0xffff,
2818 tape->stage_size / 1024, 1, &tape->min_pipeline, NULL);
2819 ide_add_setting(drive, "pipeline", SETTING_RW, TYPE_INT, 1, 0xffff,
2820 tape->stage_size / 1024, 1, &tape->max_stages, NULL);
2821 ide_add_setting(drive, "pipeline_max", SETTING_RW, TYPE_INT, 1, 0xffff,
2822 tape->stage_size / 1024, 1, &tape->max_pipeline, NULL);
2823 ide_add_setting(drive, "pipeline_used", SETTING_READ, TYPE_INT, 0,
2824 0xffff, tape->stage_size / 1024, 1, &tape->nr_stages,
2825 NULL);
2826 ide_add_setting(drive, "pipeline_pending", SETTING_READ, TYPE_INT, 0,
2827 0xffff, tape->stage_size / 1024, 1,
2828 &tape->nr_pending_stages, NULL);
Borislav Petkovb6422012008-02-02 19:56:49 +01002829 ide_add_setting(drive, "speed", SETTING_READ, TYPE_SHORT, 0, 0xffff,
2830 1, 1, (u16 *)&tape->caps[14], NULL);
Borislav Petkov54bb2072008-02-06 02:57:52 +01002831 ide_add_setting(drive, "stage", SETTING_READ, TYPE_INT, 0, 0xffff, 1,
2832 1024, &tape->stage_size, NULL);
2833 ide_add_setting(drive, "tdsc", SETTING_RW, TYPE_INT, IDETAPE_DSC_RW_MIN,
2834 IDETAPE_DSC_RW_MAX, 1000, HZ, &tape->best_dsc_rw_freq,
2835 NULL);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002836 ide_add_setting(drive, "dsc_overlap", SETTING_RW, TYPE_BYTE, 0, 1, 1,
2837 1, &drive->dsc_overlap, NULL);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002838 ide_add_setting(drive, "avg_speed", SETTING_READ, TYPE_INT, 0, 0xffff,
2839 1, 1, &tape->avg_speed, NULL);
Borislav Petkov8004a8c2008-02-06 02:57:51 +01002840 ide_add_setting(drive, "debug_mask", SETTING_RW, TYPE_INT, 0, 0xffff, 1,
2841 1, &tape->debug_mask, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002842}
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +02002843#else
2844static inline void idetape_add_settings(ide_drive_t *drive) { ; }
2845#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002846
2847/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +01002848 * The function below is called to:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002849 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +01002850 * 1. Initialize our various state variables.
2851 * 2. Ask the tape for its capabilities.
2852 * 3. Allocate a buffer which will be used for data transfer. The buffer size
2853 * is chosen based on the recommendation which we received in step 2.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002854 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +01002855 * Note that at this point ide.c already assigned us an irq, so that we can
2856 * queue requests here and wait for their completion.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002857 */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002858static void idetape_setup(ide_drive_t *drive, idetape_tape_t *tape, int minor)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002859{
2860 unsigned long t1, tmid, tn, t;
2861 int speed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002862 int stage_size;
Borislav Petkov71071b82008-02-06 02:57:53 +01002863 u8 gcw[2];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002864 struct sysinfo si;
Borislav Petkovb6422012008-02-02 19:56:49 +01002865 u16 *ctl = (u16 *)&tape->caps[12];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002866
Borislav Petkov54bb2072008-02-06 02:57:52 +01002867 spin_lock_init(&tape->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002868 drive->dsc_overlap = 1;
Bartlomiej Zolnierkiewicz4166c192008-02-01 23:09:30 +01002869 if (drive->hwif->host_flags & IDE_HFLAG_NO_DSC) {
2870 printk(KERN_INFO "ide-tape: %s: disabling DSC overlap\n",
2871 tape->name);
2872 drive->dsc_overlap = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002873 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002874 /* Seagate Travan drives do not support DSC overlap. */
2875 if (strstr(drive->id->model, "Seagate STT3401"))
2876 drive->dsc_overlap = 0;
2877 tape->minor = minor;
2878 tape->name[0] = 'h';
2879 tape->name[1] = 't';
2880 tape->name[2] = '0' + minor;
Borislav Petkov54abf372008-02-06 02:57:52 +01002881 tape->chrdev_dir = IDETAPE_DIR_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002882 tape->pc = tape->pc_stack;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002883 *((unsigned short *) &gcw) = drive->id->config;
Borislav Petkov71071b82008-02-06 02:57:53 +01002884
2885 /* Command packet DRQ type */
2886 if (((gcw[0] & 0x60) >> 5) == 1)
Borislav Petkov346331f2008-04-18 00:46:26 +02002887 set_bit(IDETAPE_FLAG_DRQ_INTERRUPT, &tape->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002888
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002889 tape->min_pipeline = 10;
2890 tape->max_pipeline = 10;
2891 tape->max_stages = 10;
Borislav Petkov3c98bf32008-02-06 02:57:53 +01002892
Linus Torvalds1da177e2005-04-16 15:20:36 -07002893 idetape_get_inquiry_results(drive);
2894 idetape_get_mode_sense_results(drive);
Borislav Petkov3cffb9c2008-02-02 19:56:50 +01002895 ide_tape_get_bsize_from_bdesc(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002896 tape->user_bs_factor = 1;
Borislav Petkov54bb2072008-02-06 02:57:52 +01002897 tape->stage_size = *ctl * tape->blk_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002898 while (tape->stage_size > 0xffff) {
2899 printk(KERN_NOTICE "ide-tape: decreasing stage size\n");
Borislav Petkovb6422012008-02-02 19:56:49 +01002900 *ctl /= 2;
Borislav Petkov54bb2072008-02-06 02:57:52 +01002901 tape->stage_size = *ctl * tape->blk_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002902 }
2903 stage_size = tape->stage_size;
2904 tape->pages_per_stage = stage_size / PAGE_SIZE;
2905 if (stage_size % PAGE_SIZE) {
2906 tape->pages_per_stage++;
2907 tape->excess_bh_size = PAGE_SIZE - stage_size % PAGE_SIZE;
2908 }
2909
Borislav Petkovb6422012008-02-02 19:56:49 +01002910 /* Select the "best" DSC read/write polling freq and pipeline size. */
2911 speed = max(*(u16 *)&tape->caps[14], *(u16 *)&tape->caps[8]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002912
2913 tape->max_stages = speed * 1000 * 10 / tape->stage_size;
2914
Borislav Petkov3c98bf32008-02-06 02:57:53 +01002915 /* Limit memory use for pipeline to 10% of physical memory */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002916 si_meminfo(&si);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002917 if (tape->max_stages * tape->stage_size >
2918 si.totalram * si.mem_unit / 10)
2919 tape->max_stages =
2920 si.totalram * si.mem_unit / (10 * tape->stage_size);
2921
Linus Torvalds1da177e2005-04-16 15:20:36 -07002922 tape->max_stages = min(tape->max_stages, IDETAPE_MAX_PIPELINE_STAGES);
2923 tape->min_pipeline = min(tape->max_stages, IDETAPE_MIN_PIPELINE_STAGES);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002924 tape->max_pipeline =
2925 min(tape->max_stages * 2, IDETAPE_MAX_PIPELINE_STAGES);
2926 if (tape->max_stages == 0) {
2927 tape->max_stages = 1;
2928 tape->min_pipeline = 1;
2929 tape->max_pipeline = 1;
2930 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002931
2932 t1 = (tape->stage_size * HZ) / (speed * 1000);
Borislav Petkovb6422012008-02-02 19:56:49 +01002933 tmid = (*(u16 *)&tape->caps[16] * 32 * HZ) / (speed * 125);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002934 tn = (IDETAPE_FIFO_THRESHOLD * tape->stage_size * HZ) / (speed * 1000);
2935
2936 if (tape->max_stages)
2937 t = tn;
2938 else
2939 t = t1;
2940
2941 /*
Borislav Petkov3c98bf32008-02-06 02:57:53 +01002942 * Ensure that the number we got makes sense; limit it within
2943 * IDETAPE_DSC_RW_MIN and IDETAPE_DSC_RW_MAX.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002944 */
Borislav Petkov54bb2072008-02-06 02:57:52 +01002945 tape->best_dsc_rw_freq = max_t(unsigned long,
2946 min_t(unsigned long, t, IDETAPE_DSC_RW_MAX),
2947 IDETAPE_DSC_RW_MIN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002948 printk(KERN_INFO "ide-tape: %s <-> %s: %dKBps, %d*%dkB buffer, "
2949 "%dkB pipeline, %lums tDSC%s\n",
Borislav Petkovb6422012008-02-02 19:56:49 +01002950 drive->name, tape->name, *(u16 *)&tape->caps[14],
2951 (*(u16 *)&tape->caps[16] * 512) / tape->stage_size,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002952 tape->stage_size / 1024,
2953 tape->max_stages * tape->stage_size / 1024,
Borislav Petkov54bb2072008-02-06 02:57:52 +01002954 tape->best_dsc_rw_freq * 1000 / HZ,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002955 drive->using_dma ? ", DMA":"");
2956
2957 idetape_add_settings(drive);
2958}
2959
Russell King4031bbe2006-01-06 11:41:00 +00002960static void ide_tape_remove(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002961{
2962 idetape_tape_t *tape = drive->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002963
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +02002964 ide_proc_unregister_driver(drive, tape->driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002965
2966 ide_unregister_region(tape->disk);
2967
2968 ide_tape_put(tape);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002969}
2970
2971static void ide_tape_release(struct kref *kref)
2972{
2973 struct ide_tape_obj *tape = to_ide_tape(kref);
2974 ide_drive_t *drive = tape->drive;
2975 struct gendisk *g = tape->disk;
2976
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02002977 BUG_ON(tape->first_stage != NULL || tape->merge_stage_size);
2978
Linus Torvalds1da177e2005-04-16 15:20:36 -07002979 drive->dsc_overlap = 0;
2980 drive->driver_data = NULL;
Tony Jonesdbc12722007-09-25 02:03:03 +02002981 device_destroy(idetape_sysfs_class, MKDEV(IDETAPE_MAJOR, tape->minor));
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002982 device_destroy(idetape_sysfs_class,
2983 MKDEV(IDETAPE_MAJOR, tape->minor + 128));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002984 idetape_devs[tape->minor] = NULL;
2985 g->private_data = NULL;
2986 put_disk(g);
2987 kfree(tape);
2988}
2989
Bartlomiej Zolnierkiewiczecfd80e2007-05-10 00:01:09 +02002990#ifdef CONFIG_IDE_PROC_FS
Linus Torvalds1da177e2005-04-16 15:20:36 -07002991static int proc_idetape_read_name
2992 (char *page, char **start, off_t off, int count, int *eof, void *data)
2993{
2994 ide_drive_t *drive = (ide_drive_t *) data;
2995 idetape_tape_t *tape = drive->driver_data;
2996 char *out = page;
2997 int len;
2998
2999 len = sprintf(out, "%s\n", tape->name);
3000 PROC_IDE_READ_RETURN(page, start, off, count, eof, len);
3001}
3002
3003static ide_proc_entry_t idetape_proc[] = {
3004 { "capacity", S_IFREG|S_IRUGO, proc_ide_read_capacity, NULL },
3005 { "name", S_IFREG|S_IRUGO, proc_idetape_read_name, NULL },
3006 { NULL, 0, NULL, NULL }
3007};
Linus Torvalds1da177e2005-04-16 15:20:36 -07003008#endif
3009
Russell King4031bbe2006-01-06 11:41:00 +00003010static int ide_tape_probe(ide_drive_t *);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003011
Linus Torvalds1da177e2005-04-16 15:20:36 -07003012static ide_driver_t idetape_driver = {
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02003013 .gen_driver = {
Laurent Riffard4ef3b8f2005-11-18 22:15:40 +01003014 .owner = THIS_MODULE,
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02003015 .name = "ide-tape",
3016 .bus = &ide_bus_type,
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02003017 },
Russell King4031bbe2006-01-06 11:41:00 +00003018 .probe = ide_tape_probe,
3019 .remove = ide_tape_remove,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003020 .version = IDETAPE_VERSION,
3021 .media = ide_tape,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003022 .supports_dsc_overlap = 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003023 .do_request = idetape_do_request,
3024 .end_request = idetape_end_request,
3025 .error = __ide_error,
3026 .abort = __ide_abort,
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +02003027#ifdef CONFIG_IDE_PROC_FS
Linus Torvalds1da177e2005-04-16 15:20:36 -07003028 .proc = idetape_proc,
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +02003029#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003030};
3031
Borislav Petkov3c98bf32008-02-06 02:57:53 +01003032/* Our character device supporting functions, passed to register_chrdev. */
Arjan van de Ven2b8693c2007-02-12 00:55:32 -08003033static const struct file_operations idetape_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003034 .owner = THIS_MODULE,
3035 .read = idetape_chrdev_read,
3036 .write = idetape_chrdev_write,
3037 .ioctl = idetape_chrdev_ioctl,
3038 .open = idetape_chrdev_open,
3039 .release = idetape_chrdev_release,
3040};
3041
3042static int idetape_open(struct inode *inode, struct file *filp)
3043{
3044 struct gendisk *disk = inode->i_bdev->bd_disk;
3045 struct ide_tape_obj *tape;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003046
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01003047 tape = ide_tape_get(disk);
3048 if (!tape)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003049 return -ENXIO;
3050
Linus Torvalds1da177e2005-04-16 15:20:36 -07003051 return 0;
3052}
3053
3054static int idetape_release(struct inode *inode, struct file *filp)
3055{
3056 struct gendisk *disk = inode->i_bdev->bd_disk;
3057 struct ide_tape_obj *tape = ide_tape_g(disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003058
3059 ide_tape_put(tape);
3060
3061 return 0;
3062}
3063
3064static int idetape_ioctl(struct inode *inode, struct file *file,
3065 unsigned int cmd, unsigned long arg)
3066{
3067 struct block_device *bdev = inode->i_bdev;
3068 struct ide_tape_obj *tape = ide_tape_g(bdev->bd_disk);
3069 ide_drive_t *drive = tape->drive;
3070 int err = generic_ide_ioctl(drive, file, bdev, cmd, arg);
3071 if (err == -EINVAL)
3072 err = idetape_blkdev_ioctl(drive, cmd, arg);
3073 return err;
3074}
3075
3076static struct block_device_operations idetape_block_ops = {
3077 .owner = THIS_MODULE,
3078 .open = idetape_open,
3079 .release = idetape_release,
3080 .ioctl = idetape_ioctl,
3081};
3082
Russell King4031bbe2006-01-06 11:41:00 +00003083static int ide_tape_probe(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003084{
3085 idetape_tape_t *tape;
3086 struct gendisk *g;
3087 int minor;
3088
3089 if (!strstr("ide-tape", drive->driver_req))
3090 goto failed;
3091 if (!drive->present)
3092 goto failed;
3093 if (drive->media != ide_tape)
3094 goto failed;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01003095 if (!idetape_identify_device(drive)) {
3096 printk(KERN_ERR "ide-tape: %s: not supported by this version of"
3097 " the driver\n", drive->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003098 goto failed;
3099 }
3100 if (drive->scsi) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01003101 printk(KERN_INFO "ide-tape: passing drive %s to ide-scsi"
3102 " emulation.\n", drive->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003103 goto failed;
3104 }
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01003105 tape = kzalloc(sizeof(idetape_tape_t), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003106 if (tape == NULL) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01003107 printk(KERN_ERR "ide-tape: %s: Can't allocate a tape struct\n",
3108 drive->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003109 goto failed;
3110 }
3111
3112 g = alloc_disk(1 << PARTN_BITS);
3113 if (!g)
3114 goto out_free_tape;
3115
3116 ide_init_disk(g, drive);
3117
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +02003118 ide_proc_register_driver(drive, &idetape_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003119
Linus Torvalds1da177e2005-04-16 15:20:36 -07003120 kref_init(&tape->kref);
3121
3122 tape->drive = drive;
3123 tape->driver = &idetape_driver;
3124 tape->disk = g;
3125
3126 g->private_data = &tape->driver;
3127
3128 drive->driver_data = tape;
3129
Arjan van de Vencf8b8972006-03-23 03:00:45 -08003130 mutex_lock(&idetape_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003131 for (minor = 0; idetape_devs[minor]; minor++)
3132 ;
3133 idetape_devs[minor] = tape;
Arjan van de Vencf8b8972006-03-23 03:00:45 -08003134 mutex_unlock(&idetape_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003135
3136 idetape_setup(drive, tape, minor);
3137
Tony Jonesdbc12722007-09-25 02:03:03 +02003138 device_create(idetape_sysfs_class, &drive->gendev,
3139 MKDEV(IDETAPE_MAJOR, minor), "%s", tape->name);
3140 device_create(idetape_sysfs_class, &drive->gendev,
3141 MKDEV(IDETAPE_MAJOR, minor + 128), "n%s", tape->name);
Will Dysond5dee802005-09-16 02:55:07 -07003142
Linus Torvalds1da177e2005-04-16 15:20:36 -07003143 g->fops = &idetape_block_ops;
3144 ide_register_region(g);
3145
3146 return 0;
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02003147
Linus Torvalds1da177e2005-04-16 15:20:36 -07003148out_free_tape:
3149 kfree(tape);
3150failed:
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02003151 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003152}
3153
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01003154static void __exit idetape_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003155{
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02003156 driver_unregister(&idetape_driver.gen_driver);
Will Dysond5dee802005-09-16 02:55:07 -07003157 class_destroy(idetape_sysfs_class);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003158 unregister_chrdev(IDETAPE_MAJOR, "ht");
3159}
3160
Bartlomiej Zolnierkiewicz17514e82005-11-19 22:24:35 +01003161static int __init idetape_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003162{
Will Dysond5dee802005-09-16 02:55:07 -07003163 int error = 1;
3164 idetape_sysfs_class = class_create(THIS_MODULE, "ide_tape");
3165 if (IS_ERR(idetape_sysfs_class)) {
3166 idetape_sysfs_class = NULL;
3167 printk(KERN_ERR "Unable to create sysfs class for ide tapes\n");
3168 error = -EBUSY;
3169 goto out;
3170 }
3171
Linus Torvalds1da177e2005-04-16 15:20:36 -07003172 if (register_chrdev(IDETAPE_MAJOR, "ht", &idetape_fops)) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01003173 printk(KERN_ERR "ide-tape: Failed to register chrdev"
3174 " interface\n");
Will Dysond5dee802005-09-16 02:55:07 -07003175 error = -EBUSY;
3176 goto out_free_class;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003177 }
Will Dysond5dee802005-09-16 02:55:07 -07003178
3179 error = driver_register(&idetape_driver.gen_driver);
3180 if (error)
3181 goto out_free_driver;
3182
3183 return 0;
3184
3185out_free_driver:
3186 driver_unregister(&idetape_driver.gen_driver);
3187out_free_class:
3188 class_destroy(idetape_sysfs_class);
3189out:
3190 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003191}
3192
Kay Sievers263756e2005-12-12 18:03:44 +01003193MODULE_ALIAS("ide:*m-tape*");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003194module_init(idetape_init);
3195module_exit(idetape_exit);
3196MODULE_ALIAS_CHARDEV_MAJOR(IDETAPE_MAJOR);
Borislav Petkov9c145762008-02-06 02:57:54 +01003197MODULE_DESCRIPTION("ATAPI Streaming TAPE Driver");
3198MODULE_LICENSE("GPL");