blob: 745e2fa549b9bff2d501f10dab2b52db57c453b2 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Borislav Petkov5ce78af2008-02-02 19:56:48 +01002 * IDE ATAPI streaming tape driver.
3 *
Bartlomiej Zolnierkiewicz59bca8c2008-02-01 23:09:33 +01004 * Copyright (C) 1995-1999 Gadi Oxman <gadio@netvision.net.il>
5 * Copyright (C) 2003-2005 Bartlomiej Zolnierkiewicz
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 * This driver was constructed as a student project in the software laboratory
8 * of the faculty of electrical engineering in the Technion - Israel's
9 * Institute Of Technology, with the guide of Avner Lottem and Dr. Ilana David.
10 *
11 * It is hereby placed under the terms of the GNU general public license.
12 * (See linux/COPYING).
Linus Torvalds1da177e2005-04-16 15:20:36 -070013 *
Borislav Petkov5ce78af2008-02-02 19:56:48 +010014 * For a historical changelog see
15 * Documentation/ide/ChangeLog.ide-tape.1995-2002
Linus Torvalds1da177e2005-04-16 15:20:36 -070016 */
17
Borislav Petkovdfe79932008-02-06 02:57:55 +010018#define IDETAPE_VERSION "1.20"
Linus Torvalds1da177e2005-04-16 15:20:36 -070019
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include <linux/module.h>
21#include <linux/types.h>
22#include <linux/string.h>
23#include <linux/kernel.h>
24#include <linux/delay.h>
25#include <linux/timer.h>
26#include <linux/mm.h>
27#include <linux/interrupt.h>
Marcelo Feitoza Parisi9bae1ff2006-03-28 01:56:46 -080028#include <linux/jiffies.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#include <linux/major.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include <linux/errno.h>
31#include <linux/genhd.h>
32#include <linux/slab.h>
33#include <linux/pci.h>
34#include <linux/ide.h>
35#include <linux/smp_lock.h>
36#include <linux/completion.h>
37#include <linux/bitops.h>
Arjan van de Vencf8b8972006-03-23 03:00:45 -080038#include <linux/mutex.h>
Borislav Petkov90699ce2008-02-02 19:56:50 +010039#include <scsi/scsi.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070040
41#include <asm/byteorder.h>
Borislav Petkovc837cfa2008-02-06 02:57:54 +010042#include <linux/irq.h>
43#include <linux/uaccess.h>
44#include <linux/io.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070045#include <asm/unaligned.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070046#include <linux/mtio.h>
47
Borislav Petkov8004a8c2008-02-06 02:57:51 +010048enum {
49 /* output errors only */
50 DBG_ERR = (1 << 0),
51 /* output all sense key/asc */
52 DBG_SENSE = (1 << 1),
53 /* info regarding all chrdev-related procedures */
54 DBG_CHRDEV = (1 << 2),
55 /* all remaining procedures */
56 DBG_PROCS = (1 << 3),
57 /* buffer alloc info (pc_stack & rq_stack) */
58 DBG_PCRQ_STACK = (1 << 4),
59};
60
61/* define to see debug info */
62#define IDETAPE_DEBUG_LOG 0
63
64#if IDETAPE_DEBUG_LOG
65#define debug_log(lvl, fmt, args...) \
66{ \
67 if (tape->debug_mask & lvl) \
68 printk(KERN_INFO "ide-tape: " fmt, ## args); \
69}
70#else
71#define debug_log(lvl, fmt, args...) do {} while (0)
72#endif
73
Linus Torvalds1da177e2005-04-16 15:20:36 -070074/**************************** Tunable parameters *****************************/
75
76
77/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +010078 * Pipelined mode parameters.
Linus Torvalds1da177e2005-04-16 15:20:36 -070079 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +010080 * We try to use the minimum number of stages which is enough to keep the tape
81 * constantly streaming. To accomplish that, we implement a feedback loop around
82 * the maximum number of stages:
Linus Torvalds1da177e2005-04-16 15:20:36 -070083 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +010084 * We start from MIN maximum stages (we will not even use MIN stages if we don't
85 * need them), increment it by RATE*(MAX-MIN) whenever we sense that the
86 * pipeline is empty, until we reach the optimum value or until we reach MAX.
Linus Torvalds1da177e2005-04-16 15:20:36 -070087 */
88#define IDETAPE_MIN_PIPELINE_STAGES 1
89#define IDETAPE_MAX_PIPELINE_STAGES 400
90#define IDETAPE_INCREASE_STAGES_RATE 20
91
92/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +010093 * After each failed packet command we issue a request sense command and retry
94 * the packet command IDETAPE_MAX_PC_RETRIES times.
Linus Torvalds1da177e2005-04-16 15:20:36 -070095 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +010096 * Setting IDETAPE_MAX_PC_RETRIES to 0 will disable retries.
Linus Torvalds1da177e2005-04-16 15:20:36 -070097 */
98#define IDETAPE_MAX_PC_RETRIES 3
99
100/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100101 * With each packet command, we allocate a buffer of IDETAPE_PC_BUFFER_SIZE
102 * bytes. This is used for several packet commands (Not for READ/WRITE commands)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 */
104#define IDETAPE_PC_BUFFER_SIZE 256
105
106/*
107 * In various places in the driver, we need to allocate storage
108 * for packet commands and requests, which will remain valid while
109 * we leave the driver to wait for an interrupt or a timeout event.
110 */
111#define IDETAPE_PC_STACK (10 + IDETAPE_MAX_PC_RETRIES)
112
113/*
114 * Some drives (for example, Seagate STT3401A Travan) require a very long
115 * timeout, because they don't return an interrupt or clear their busy bit
116 * until after the command completes (even retension commands).
117 */
118#define IDETAPE_WAIT_CMD (900*HZ)
119
120/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100121 * The following parameter is used to select the point in the internal tape fifo
122 * in which we will start to refill the buffer. Decreasing the following
123 * parameter will improve the system's latency and interactive response, while
124 * using a high value might improve system throughput.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 */
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100126#define IDETAPE_FIFO_THRESHOLD 2
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127
128/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100129 * DSC polling parameters.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100131 * Polling for DSC (a single bit in the status register) is a very important
132 * function in ide-tape. There are two cases in which we poll for DSC:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100134 * 1. Before a read/write packet command, to ensure that we can transfer data
135 * from/to the tape's data buffers, without causing an actual media access.
136 * In case the tape is not ready yet, we take out our request from the device
137 * request queue, so that ide.c could service requests from the other device
138 * on the same interface in the meantime.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100140 * 2. After the successful initialization of a "media access packet command",
141 * which is a command that can take a long time to complete (the interval can
142 * range from several seconds to even an hour). Again, we postpone our request
143 * in the middle to free the bus for the other device. The polling frequency
144 * here should be lower than the read/write frequency since those media access
145 * commands are slow. We start from a "fast" frequency - IDETAPE_DSC_MA_FAST
146 * (1 second), and if we don't receive DSC after IDETAPE_DSC_MA_THRESHOLD
147 * (5 min), we switch it to a lower frequency - IDETAPE_DSC_MA_SLOW (1 min).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100149 * We also set a timeout for the timer, in case something goes wrong. The
150 * timeout should be longer then the maximum execution time of a tape operation.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 */
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100152
153/* DSC timings. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154#define IDETAPE_DSC_RW_MIN 5*HZ/100 /* 50 msec */
155#define IDETAPE_DSC_RW_MAX 40*HZ/100 /* 400 msec */
156#define IDETAPE_DSC_RW_TIMEOUT 2*60*HZ /* 2 minutes */
157#define IDETAPE_DSC_MA_FAST 2*HZ /* 2 seconds */
158#define IDETAPE_DSC_MA_THRESHOLD 5*60*HZ /* 5 minutes */
159#define IDETAPE_DSC_MA_SLOW 30*HZ /* 30 seconds */
160#define IDETAPE_DSC_MA_TIMEOUT 2*60*60*HZ /* 2 hours */
161
162/*************************** End of tunable parameters ***********************/
163
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100164/* Read/Write error simulation */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165#define SIMULATE_ERRORS 0
166
Borislav Petkov54abf372008-02-06 02:57:52 +0100167/* tape directions */
168enum {
169 IDETAPE_DIR_NONE = (1 << 0),
170 IDETAPE_DIR_READ = (1 << 1),
171 IDETAPE_DIR_WRITE = (1 << 2),
172};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173
174struct idetape_bh {
Stephen Rothwellab057962007-08-01 23:46:44 +0200175 u32 b_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 atomic_t b_count;
177 struct idetape_bh *b_reqnext;
178 char *b_data;
179};
180
Borislav Petkov03056b92008-04-18 00:46:26 +0200181/* Tape door status */
182#define DOOR_UNLOCKED 0
183#define DOOR_LOCKED 1
184#define DOOR_EXPLICITLY_LOCKED 2
185
186/* Some defines for the SPACE command */
187#define IDETAPE_SPACE_OVER_FILEMARK 1
188#define IDETAPE_SPACE_TO_EOD 3
189
190/* Some defines for the LOAD UNLOAD command */
191#define IDETAPE_LU_LOAD_MASK 1
192#define IDETAPE_LU_RETENSION_MASK 2
193#define IDETAPE_LU_EOT_MASK 4
194
195/*
196 * Special requests for our block device strategy routine.
197 *
198 * In order to service a character device command, we add special requests to
199 * the tail of our block device request queue and wait for their completion.
200 */
201
202enum {
203 REQ_IDETAPE_PC1 = (1 << 0), /* packet command (first stage) */
204 REQ_IDETAPE_PC2 = (1 << 1), /* packet command (second stage) */
205 REQ_IDETAPE_READ = (1 << 2),
206 REQ_IDETAPE_WRITE = (1 << 3),
207};
208
209/* Error codes returned in rq->errors to the higher part of the driver. */
210#define IDETAPE_ERROR_GENERAL 101
211#define IDETAPE_ERROR_FILEMARK 102
212#define IDETAPE_ERROR_EOD 103
213
214/* Structures related to the SELECT SENSE / MODE SENSE packet commands. */
215#define IDETAPE_BLOCK_DESCRIPTOR 0
216#define IDETAPE_CAPABILITIES_PAGE 0x2a
217
Borislav Petkov346331f2008-04-18 00:46:26 +0200218/* Tape flag bits values. */
219enum {
220 IDETAPE_FLAG_IGNORE_DSC = (1 << 0),
221 /* 0 When the tape position is unknown */
222 IDETAPE_FLAG_ADDRESS_VALID = (1 << 1),
223 /* Device already opened */
224 IDETAPE_FLAG_BUSY = (1 << 2),
225 /* Error detected in a pipeline stage */
226 IDETAPE_FLAG_PIPELINE_ERR = (1 << 3),
227 /* Attempt to auto-detect the current user block size */
228 IDETAPE_FLAG_DETECT_BS = (1 << 4),
229 /* Currently on a filemark */
230 IDETAPE_FLAG_FILEMARK = (1 << 5),
231 /* DRQ interrupt device */
232 IDETAPE_FLAG_DRQ_INTERRUPT = (1 << 6),
233 /* pipeline active */
234 IDETAPE_FLAG_PIPELINE_ACTIVE = (1 << 7),
235 /* 0 = no tape is loaded, so we don't rewind after ejecting */
236 IDETAPE_FLAG_MEDIUM_PRESENT = (1 << 8),
237};
238
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100239/* A pipeline stage. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240typedef struct idetape_stage_s {
241 struct request rq; /* The corresponding request */
242 struct idetape_bh *bh; /* The data buffers */
243 struct idetape_stage_s *next; /* Pointer to the next stage */
244} idetape_stage_t;
245
246/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100247 * Most of our global data which we need to save even as we leave the driver due
248 * to an interrupt or a timer event is stored in the struct defined below.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 */
250typedef struct ide_tape_obj {
251 ide_drive_t *drive;
252 ide_driver_t *driver;
253 struct gendisk *disk;
254 struct kref kref;
255
256 /*
257 * Since a typical character device operation requires more
258 * than one packet command, we provide here enough memory
259 * for the maximum of interconnected packet commands.
260 * The packet commands are stored in the circular array pc_stack.
261 * pc_stack_index points to the last used entry, and warps around
262 * to the start when we get to the last array entry.
263 *
264 * pc points to the current processed packet command.
265 *
266 * failed_pc points to the last failed packet command, or contains
267 * NULL if we do not need to retry any packet command. This is
268 * required since an additional packet command is needed before the
269 * retry, to get detailed information on what went wrong.
270 */
271 /* Current packet command */
Borislav Petkovd236d742008-04-18 00:46:27 +0200272 struct ide_atapi_pc *pc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 /* Last failed packet command */
Borislav Petkovd236d742008-04-18 00:46:27 +0200274 struct ide_atapi_pc *failed_pc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 /* Packet command stack */
Borislav Petkovd236d742008-04-18 00:46:27 +0200276 struct ide_atapi_pc pc_stack[IDETAPE_PC_STACK];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 /* Next free packet command storage space */
278 int pc_stack_index;
279 struct request rq_stack[IDETAPE_PC_STACK];
280 /* We implement a circular array */
281 int rq_stack_index;
282
283 /*
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100284 * DSC polling variables.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100286 * While polling for DSC we use postponed_rq to postpone the current
287 * request so that ide.c will be able to service pending requests on the
288 * other device. Note that at most we will have only one DSC (usually
289 * data transfer) request in the device request queue. Additional
290 * requests can be queued in our internal pipeline, but they will be
291 * visible to ide.c only one at a time.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 */
293 struct request *postponed_rq;
294 /* The time in which we started polling for DSC */
295 unsigned long dsc_polling_start;
296 /* Timer used to poll for dsc */
297 struct timer_list dsc_timer;
298 /* Read/Write dsc polling frequency */
Borislav Petkov54bb2072008-02-06 02:57:52 +0100299 unsigned long best_dsc_rw_freq;
300 unsigned long dsc_poll_freq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 unsigned long dsc_timeout;
302
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100303 /* Read position information */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 u8 partition;
305 /* Current block */
Borislav Petkov54bb2072008-02-06 02:57:52 +0100306 unsigned int first_frame;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100308 /* Last error information */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 u8 sense_key, asc, ascq;
310
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100311 /* Character device operation */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 unsigned int minor;
313 /* device name */
314 char name[4];
315 /* Current character device data transfer direction */
Borislav Petkov54abf372008-02-06 02:57:52 +0100316 u8 chrdev_dir;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317
Borislav Petkov54bb2072008-02-06 02:57:52 +0100318 /* tape block size, usually 512 or 1024 bytes */
319 unsigned short blk_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 int user_bs_factor;
Borislav Petkovb6422012008-02-02 19:56:49 +0100321
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 /* Copy of the tape's Capabilities and Mechanical Page */
Borislav Petkovb6422012008-02-02 19:56:49 +0100323 u8 caps[20];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324
325 /*
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100326 * Active data transfer request parameters.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100328 * At most, there is only one ide-tape originated data transfer request
329 * in the device request queue. This allows ide.c to easily service
330 * requests from the other device when we postpone our active request.
331 * In the pipelined operation mode, we use our internal pipeline
332 * structure to hold more data requests. The data buffer size is chosen
333 * based on the tape's recommendation.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 */
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100335 /* ptr to the request which is waiting in the device request queue */
Borislav Petkov54bb2072008-02-06 02:57:52 +0100336 struct request *active_data_rq;
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100337 /* Data buffer size chosen based on the tape's recommendation */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 int stage_size;
339 idetape_stage_t *merge_stage;
340 int merge_stage_size;
341 struct idetape_bh *bh;
342 char *b_data;
343 int b_count;
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100344
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 /*
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100346 * Pipeline parameters.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100348 * To accomplish non-pipelined mode, we simply set the following
349 * variables to zero (or NULL, where appropriate).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 */
351 /* Number of currently used stages */
352 int nr_stages;
353 /* Number of pending stages */
354 int nr_pending_stages;
355 /* We will not allocate more than this number of stages */
356 int max_stages, min_pipeline, max_pipeline;
357 /* The first stage which will be removed from the pipeline */
358 idetape_stage_t *first_stage;
359 /* The currently active stage */
360 idetape_stage_t *active_stage;
361 /* Will be serviced after the currently active request */
362 idetape_stage_t *next_stage;
363 /* New requests will be added to the pipeline here */
364 idetape_stage_t *last_stage;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 int pages_per_stage;
366 /* Wasted space in each stage */
367 int excess_bh_size;
368
369 /* Status/Action flags: long for set_bit */
370 unsigned long flags;
371 /* protects the ide-tape queue */
Borislav Petkov54bb2072008-02-06 02:57:52 +0100372 spinlock_t lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100374 /* Measures average tape speed */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 unsigned long avg_time;
376 int avg_size;
377 int avg_speed;
378
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 /* the door is currently locked */
380 int door_locked;
381 /* the tape hardware is write protected */
382 char drv_write_prot;
383 /* the tape is write protected (hardware or opened as read-only) */
384 char write_prot;
385
386 /*
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100387 * Limit the number of times a request can be postponed, to avoid an
388 * infinite postpone deadlock.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 int postpone_cnt;
391
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100392 /* Speed control at the tape buffers input/output */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 unsigned long insert_time;
394 int insert_size;
395 int insert_speed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 int measure_insert_time;
397
Borislav Petkov8004a8c2008-02-06 02:57:51 +0100398 u32 debug_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399} idetape_tape_t;
400
Arjan van de Vencf8b8972006-03-23 03:00:45 -0800401static DEFINE_MUTEX(idetape_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402
Will Dysond5dee802005-09-16 02:55:07 -0700403static struct class *idetape_sysfs_class;
404
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405#define to_ide_tape(obj) container_of(obj, struct ide_tape_obj, kref)
406
407#define ide_tape_g(disk) \
408 container_of((disk)->private_data, struct ide_tape_obj, driver)
409
410static struct ide_tape_obj *ide_tape_get(struct gendisk *disk)
411{
412 struct ide_tape_obj *tape = NULL;
413
Arjan van de Vencf8b8972006-03-23 03:00:45 -0800414 mutex_lock(&idetape_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 tape = ide_tape_g(disk);
416 if (tape)
417 kref_get(&tape->kref);
Arjan van de Vencf8b8972006-03-23 03:00:45 -0800418 mutex_unlock(&idetape_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 return tape;
420}
421
422static void ide_tape_release(struct kref *);
423
424static void ide_tape_put(struct ide_tape_obj *tape)
425{
Arjan van de Vencf8b8972006-03-23 03:00:45 -0800426 mutex_lock(&idetape_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 kref_put(&tape->kref, ide_tape_release);
Arjan van de Vencf8b8972006-03-23 03:00:45 -0800428 mutex_unlock(&idetape_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429}
430
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100432 * The variables below are used for the character device interface. Additional
433 * state variables are defined in our ide_drive_t structure.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100435static struct ide_tape_obj *idetape_devs[MAX_HWIFS * MAX_DRIVES];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436
437#define ide_tape_f(file) ((file)->private_data)
438
439static struct ide_tape_obj *ide_tape_chrdev_get(unsigned int i)
440{
441 struct ide_tape_obj *tape = NULL;
442
Arjan van de Vencf8b8972006-03-23 03:00:45 -0800443 mutex_lock(&idetape_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 tape = idetape_devs[i];
445 if (tape)
446 kref_get(&tape->kref);
Arjan van de Vencf8b8972006-03-23 03:00:45 -0800447 mutex_unlock(&idetape_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 return tape;
449}
450
Borislav Petkovd236d742008-04-18 00:46:27 +0200451static void idetape_input_buffers(ide_drive_t *drive, struct ide_atapi_pc *pc,
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100452 unsigned int bcount)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453{
454 struct idetape_bh *bh = pc->bh;
455 int count;
456
457 while (bcount) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 if (bh == NULL) {
459 printk(KERN_ERR "ide-tape: bh == NULL in "
460 "idetape_input_buffers\n");
Bartlomiej Zolnierkiewicz7616c0a2008-04-18 00:46:26 +0200461 ide_atapi_discard_data(drive, bcount);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 return;
463 }
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100464 count = min(
465 (unsigned int)(bh->b_size - atomic_read(&bh->b_count)),
466 bcount);
467 HWIF(drive)->atapi_input_bytes(drive, bh->b_data +
468 atomic_read(&bh->b_count), count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 bcount -= count;
470 atomic_add(count, &bh->b_count);
471 if (atomic_read(&bh->b_count) == bh->b_size) {
472 bh = bh->b_reqnext;
473 if (bh)
474 atomic_set(&bh->b_count, 0);
475 }
476 }
477 pc->bh = bh;
478}
479
Borislav Petkovd236d742008-04-18 00:46:27 +0200480static void idetape_output_buffers(ide_drive_t *drive, struct ide_atapi_pc *pc,
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100481 unsigned int bcount)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482{
483 struct idetape_bh *bh = pc->bh;
484 int count;
485
486 while (bcount) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487 if (bh == NULL) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100488 printk(KERN_ERR "ide-tape: bh == NULL in %s\n",
489 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 return;
491 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 count = min((unsigned int)pc->b_count, (unsigned int)bcount);
493 HWIF(drive)->atapi_output_bytes(drive, pc->b_data, count);
494 bcount -= count;
495 pc->b_data += count;
496 pc->b_count -= count;
497 if (!pc->b_count) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100498 bh = bh->b_reqnext;
499 pc->bh = bh;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 if (bh) {
501 pc->b_data = bh->b_data;
502 pc->b_count = atomic_read(&bh->b_count);
503 }
504 }
505 }
506}
507
Borislav Petkovd236d742008-04-18 00:46:27 +0200508static void idetape_update_buffers(struct ide_atapi_pc *pc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509{
510 struct idetape_bh *bh = pc->bh;
511 int count;
Borislav Petkovd236d742008-04-18 00:46:27 +0200512 unsigned int bcount = pc->xferred;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513
Borislav Petkov346331f2008-04-18 00:46:26 +0200514 if (pc->flags & PC_FLAG_WRITING)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 return;
516 while (bcount) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 if (bh == NULL) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100518 printk(KERN_ERR "ide-tape: bh == NULL in %s\n",
519 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 return;
521 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 count = min((unsigned int)bh->b_size, (unsigned int)bcount);
523 atomic_set(&bh->b_count, count);
524 if (atomic_read(&bh->b_count) == bh->b_size)
525 bh = bh->b_reqnext;
526 bcount -= count;
527 }
528 pc->bh = bh;
529}
530
531/*
532 * idetape_next_pc_storage returns a pointer to a place in which we can
533 * safely store a packet command, even though we intend to leave the
534 * driver. A storage space for a maximum of IDETAPE_PC_STACK packet
535 * commands is allocated at initialization time.
536 */
Borislav Petkovd236d742008-04-18 00:46:27 +0200537static struct ide_atapi_pc *idetape_next_pc_storage(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538{
539 idetape_tape_t *tape = drive->driver_data;
540
Borislav Petkov8004a8c2008-02-06 02:57:51 +0100541 debug_log(DBG_PCRQ_STACK, "pc_stack_index=%d\n", tape->pc_stack_index);
542
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 if (tape->pc_stack_index == IDETAPE_PC_STACK)
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100544 tape->pc_stack_index = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545 return (&tape->pc_stack[tape->pc_stack_index++]);
546}
547
548/*
549 * idetape_next_rq_storage is used along with idetape_next_pc_storage.
550 * Since we queue packet commands in the request queue, we need to
551 * allocate a request, along with the allocation of a packet command.
552 */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100553
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554/**************************************************************
555 * *
556 * This should get fixed to use kmalloc(.., GFP_ATOMIC) *
557 * followed later on by kfree(). -ml *
558 * *
559 **************************************************************/
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100560
561static struct request *idetape_next_rq_storage(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562{
563 idetape_tape_t *tape = drive->driver_data;
564
Borislav Petkov8004a8c2008-02-06 02:57:51 +0100565 debug_log(DBG_PCRQ_STACK, "rq_stack_index=%d\n", tape->rq_stack_index);
566
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 if (tape->rq_stack_index == IDETAPE_PC_STACK)
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100568 tape->rq_stack_index = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 return (&tape->rq_stack[tape->rq_stack_index++]);
570}
571
Borislav Petkovd236d742008-04-18 00:46:27 +0200572static void idetape_init_pc(struct ide_atapi_pc *pc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573{
574 memset(pc->c, 0, 12);
575 pc->retries = 0;
576 pc->flags = 0;
Borislav Petkovd236d742008-04-18 00:46:27 +0200577 pc->req_xfer = 0;
578 pc->buf = pc->pc_buf;
579 pc->buf_size = IDETAPE_PC_BUFFER_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 pc->bh = NULL;
581 pc->b_data = NULL;
582}
583
584/*
Borislav Petkov1b5db432008-02-02 19:56:48 +0100585 * called on each failed packet command retry to analyze the request sense. We
586 * currently do not utilize this information.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 */
Borislav Petkov1b5db432008-02-02 19:56:48 +0100588static void idetape_analyze_error(ide_drive_t *drive, u8 *sense)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589{
590 idetape_tape_t *tape = drive->driver_data;
Borislav Petkovd236d742008-04-18 00:46:27 +0200591 struct ide_atapi_pc *pc = tape->failed_pc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592
Borislav Petkov1b5db432008-02-02 19:56:48 +0100593 tape->sense_key = sense[2] & 0xF;
594 tape->asc = sense[12];
595 tape->ascq = sense[13];
Borislav Petkov8004a8c2008-02-06 02:57:51 +0100596
597 debug_log(DBG_ERR, "pc = %x, sense key = %x, asc = %x, ascq = %x\n",
598 pc->c[0], tape->sense_key, tape->asc, tape->ascq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599
Borislav Petkovd236d742008-04-18 00:46:27 +0200600 /* Correct pc->xferred by asking the tape. */
Borislav Petkov346331f2008-04-18 00:46:26 +0200601 if (pc->flags & PC_FLAG_DMA_ERROR) {
Borislav Petkovd236d742008-04-18 00:46:27 +0200602 pc->xferred = pc->req_xfer -
Borislav Petkov54bb2072008-02-06 02:57:52 +0100603 tape->blk_size *
Borislav Petkov860ff5e2008-02-02 19:56:50 +0100604 be32_to_cpu(get_unaligned((u32 *)&sense[3]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 idetape_update_buffers(pc);
606 }
607
608 /*
609 * If error was the result of a zero-length read or write command,
610 * with sense key=5, asc=0x22, ascq=0, let it slide. Some drives
611 * (i.e. Seagate STT3401A Travan) don't support 0-length read/writes.
612 */
Borislav Petkov90699ce2008-02-02 19:56:50 +0100613 if ((pc->c[0] == READ_6 || pc->c[0] == WRITE_6)
Borislav Petkov1b5db432008-02-02 19:56:48 +0100614 /* length == 0 */
615 && pc->c[4] == 0 && pc->c[3] == 0 && pc->c[2] == 0) {
616 if (tape->sense_key == 5) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 /* don't report an error, everything's ok */
618 pc->error = 0;
619 /* don't retry read/write */
Borislav Petkov346331f2008-04-18 00:46:26 +0200620 pc->flags |= PC_FLAG_ABORT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621 }
622 }
Borislav Petkov90699ce2008-02-02 19:56:50 +0100623 if (pc->c[0] == READ_6 && (sense[2] & 0x80)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624 pc->error = IDETAPE_ERROR_FILEMARK;
Borislav Petkov346331f2008-04-18 00:46:26 +0200625 pc->flags |= PC_FLAG_ABORT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626 }
Borislav Petkov90699ce2008-02-02 19:56:50 +0100627 if (pc->c[0] == WRITE_6) {
Borislav Petkov1b5db432008-02-02 19:56:48 +0100628 if ((sense[2] & 0x40) || (tape->sense_key == 0xd
629 && tape->asc == 0x0 && tape->ascq == 0x2)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630 pc->error = IDETAPE_ERROR_EOD;
Borislav Petkov346331f2008-04-18 00:46:26 +0200631 pc->flags |= PC_FLAG_ABORT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 }
633 }
Borislav Petkov90699ce2008-02-02 19:56:50 +0100634 if (pc->c[0] == READ_6 || pc->c[0] == WRITE_6) {
Borislav Petkov1b5db432008-02-02 19:56:48 +0100635 if (tape->sense_key == 8) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636 pc->error = IDETAPE_ERROR_EOD;
Borislav Petkov346331f2008-04-18 00:46:26 +0200637 pc->flags |= PC_FLAG_ABORT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 }
Borislav Petkov346331f2008-04-18 00:46:26 +0200639 if (!(pc->flags & PC_FLAG_ABORT) &&
Borislav Petkovd236d742008-04-18 00:46:27 +0200640 pc->xferred)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 pc->retries = IDETAPE_MAX_PC_RETRIES + 1;
642 }
643}
644
Borislav Petkov419d4742008-02-02 19:56:51 +0100645static void idetape_activate_next_stage(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646{
647 idetape_tape_t *tape = drive->driver_data;
648 idetape_stage_t *stage = tape->next_stage;
649 struct request *rq = &stage->rq;
650
Borislav Petkov8004a8c2008-02-06 02:57:51 +0100651 debug_log(DBG_PROCS, "Enter %s\n", __func__);
652
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 if (stage == NULL) {
Borislav Petkov8004a8c2008-02-06 02:57:51 +0100654 printk(KERN_ERR "ide-tape: bug: Trying to activate a non"
655 " existing stage\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 return;
657 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658
659 rq->rq_disk = tape->disk;
660 rq->buffer = NULL;
661 rq->special = (void *)stage->bh;
Borislav Petkov54bb2072008-02-06 02:57:52 +0100662 tape->active_data_rq = rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 tape->active_stage = stage;
664 tape->next_stage = stage->next;
665}
666
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100667/* Free a stage along with its related buffers completely. */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100668static void __idetape_kfree_stage(idetape_stage_t *stage)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669{
670 struct idetape_bh *prev_bh, *bh = stage->bh;
671 int size;
672
673 while (bh != NULL) {
674 if (bh->b_data != NULL) {
675 size = (int) bh->b_size;
676 while (size > 0) {
677 free_page((unsigned long) bh->b_data);
678 size -= PAGE_SIZE;
679 bh->b_data += PAGE_SIZE;
680 }
681 }
682 prev_bh = bh;
683 bh = bh->b_reqnext;
684 kfree(prev_bh);
685 }
686 kfree(stage);
687}
688
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100689static void idetape_kfree_stage(idetape_tape_t *tape, idetape_stage_t *stage)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690{
691 __idetape_kfree_stage(stage);
692}
693
694/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100695 * Remove tape->first_stage from the pipeline. The caller should avoid race
696 * conditions.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100698static void idetape_remove_stage_head(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699{
700 idetape_tape_t *tape = drive->driver_data;
701 idetape_stage_t *stage;
Borislav Petkov8004a8c2008-02-06 02:57:51 +0100702
703 debug_log(DBG_PROCS, "Enter %s\n", __func__);
704
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705 if (tape->first_stage == NULL) {
706 printk(KERN_ERR "ide-tape: bug: tape->first_stage is NULL\n");
Borislav Petkov55a5d292008-02-02 19:56:49 +0100707 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708 }
709 if (tape->active_stage == tape->first_stage) {
Borislav Petkov8004a8c2008-02-06 02:57:51 +0100710 printk(KERN_ERR "ide-tape: bug: Trying to free our active "
711 "pipeline stage\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712 return;
713 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 stage = tape->first_stage;
715 tape->first_stage = stage->next;
716 idetape_kfree_stage(tape, stage);
717 tape->nr_stages--;
718 if (tape->first_stage == NULL) {
719 tape->last_stage = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 if (tape->next_stage != NULL)
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100721 printk(KERN_ERR "ide-tape: bug: tape->next_stage !="
722 " NULL\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 if (tape->nr_stages)
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100724 printk(KERN_ERR "ide-tape: bug: nr_stages should be 0 "
725 "now\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 }
727}
728
729/*
730 * This will free all the pipeline stages starting from new_last_stage->next
731 * to the end of the list, and point tape->last_stage to new_last_stage.
732 */
733static void idetape_abort_pipeline(ide_drive_t *drive,
734 idetape_stage_t *new_last_stage)
735{
736 idetape_tape_t *tape = drive->driver_data;
737 idetape_stage_t *stage = new_last_stage->next;
738 idetape_stage_t *nstage;
739
Borislav Petkov8004a8c2008-02-06 02:57:51 +0100740 debug_log(DBG_PROCS, "%s: Enter %s\n", tape->name, __func__);
741
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742 while (stage) {
743 nstage = stage->next;
744 idetape_kfree_stage(tape, stage);
745 --tape->nr_stages;
746 --tape->nr_pending_stages;
747 stage = nstage;
748 }
749 if (new_last_stage)
750 new_last_stage->next = NULL;
751 tape->last_stage = new_last_stage;
752 tape->next_stage = NULL;
753}
754
755/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100756 * Finish servicing a request and insert a pending pipeline request into the
757 * main device queue.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758 */
759static int idetape_end_request(ide_drive_t *drive, int uptodate, int nr_sects)
760{
761 struct request *rq = HWGROUP(drive)->rq;
762 idetape_tape_t *tape = drive->driver_data;
763 unsigned long flags;
764 int error;
765 int remove_stage = 0;
766 idetape_stage_t *active_stage;
767
Borislav Petkov8004a8c2008-02-06 02:57:51 +0100768 debug_log(DBG_PROCS, "Enter %s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769
770 switch (uptodate) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100771 case 0: error = IDETAPE_ERROR_GENERAL; break;
772 case 1: error = 0; break;
773 default: error = uptodate;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774 }
775 rq->errors = error;
776 if (error)
777 tape->failed_pc = NULL;
778
Bartlomiej Zolnierkiewicz36872212008-01-26 20:13:10 +0100779 if (!blk_special_request(rq)) {
780 ide_end_request(drive, uptodate, nr_sects);
781 return 0;
782 }
783
Borislav Petkov54bb2072008-02-06 02:57:52 +0100784 spin_lock_irqsave(&tape->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785
786 /* The request was a pipelined data transfer request */
Borislav Petkov54bb2072008-02-06 02:57:52 +0100787 if (tape->active_data_rq == rq) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788 active_stage = tape->active_stage;
789 tape->active_stage = NULL;
Borislav Petkov54bb2072008-02-06 02:57:52 +0100790 tape->active_data_rq = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791 tape->nr_pending_stages--;
792 if (rq->cmd[0] & REQ_IDETAPE_WRITE) {
793 remove_stage = 1;
794 if (error) {
Borislav Petkov346331f2008-04-18 00:46:26 +0200795 set_bit(IDETAPE_FLAG_PIPELINE_ERR,
796 &tape->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797 if (error == IDETAPE_ERROR_EOD)
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100798 idetape_abort_pipeline(drive,
799 active_stage);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800 }
801 } else if (rq->cmd[0] & REQ_IDETAPE_READ) {
802 if (error == IDETAPE_ERROR_EOD) {
Borislav Petkov346331f2008-04-18 00:46:26 +0200803 set_bit(IDETAPE_FLAG_PIPELINE_ERR,
804 &tape->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805 idetape_abort_pipeline(drive, active_stage);
806 }
807 }
808 if (tape->next_stage != NULL) {
Borislav Petkov419d4742008-02-02 19:56:51 +0100809 idetape_activate_next_stage(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100811 /* Insert the next request into the request queue. */
Borislav Petkov54bb2072008-02-06 02:57:52 +0100812 (void)ide_do_drive_cmd(drive, tape->active_data_rq,
813 ide_end);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814 } else if (!error) {
Borislav Petkov97219852008-02-06 02:57:52 +0100815 /*
816 * This is a part of the feedback loop which tries to
817 * find the optimum number of stages. We are starting
818 * from a minimum maximum number of stages, and if we
819 * sense that the pipeline is empty, we try to increase
820 * it, until we reach the user compile time memory
821 * limit.
822 */
823 int i = (tape->max_pipeline - tape->min_pipeline) / 10;
824
825 tape->max_stages += max(i, 1);
826 tape->max_stages = max(tape->max_stages,
827 tape->min_pipeline);
828 tape->max_stages = min(tape->max_stages,
829 tape->max_pipeline);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830 }
831 }
832 ide_end_drive_cmd(drive, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833
834 if (remove_stage)
835 idetape_remove_stage_head(drive);
Borislav Petkov54bb2072008-02-06 02:57:52 +0100836 if (tape->active_data_rq == NULL)
Borislav Petkov346331f2008-04-18 00:46:26 +0200837 clear_bit(IDETAPE_FLAG_PIPELINE_ACTIVE, &tape->flags);
Borislav Petkov54bb2072008-02-06 02:57:52 +0100838 spin_unlock_irqrestore(&tape->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839 return 0;
840}
841
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100842static ide_startstop_t idetape_request_sense_callback(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843{
844 idetape_tape_t *tape = drive->driver_data;
845
Borislav Petkov8004a8c2008-02-06 02:57:51 +0100846 debug_log(DBG_PROCS, "Enter %s\n", __func__);
847
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848 if (!tape->pc->error) {
Borislav Petkovd236d742008-04-18 00:46:27 +0200849 idetape_analyze_error(drive, tape->pc->buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850 idetape_end_request(drive, 1, 0);
851 } else {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100852 printk(KERN_ERR "ide-tape: Error in REQUEST SENSE itself - "
853 "Aborting request!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854 idetape_end_request(drive, 0, 0);
855 }
856 return ide_stopped;
857}
858
Borislav Petkovd236d742008-04-18 00:46:27 +0200859static void idetape_create_request_sense_cmd(struct ide_atapi_pc *pc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860{
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100861 idetape_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +0100862 pc->c[0] = REQUEST_SENSE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863 pc->c[4] = 20;
Borislav Petkovd236d742008-04-18 00:46:27 +0200864 pc->req_xfer = 20;
865 pc->idetape_callback = &idetape_request_sense_callback;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866}
867
868static void idetape_init_rq(struct request *rq, u8 cmd)
869{
870 memset(rq, 0, sizeof(*rq));
Jens Axboe4aff5e22006-08-10 08:44:47 +0200871 rq->cmd_type = REQ_TYPE_SPECIAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872 rq->cmd[0] = cmd;
873}
874
875/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100876 * Generate a new packet command request in front of the request queue, before
877 * the current request, so that it will be processed immediately, on the next
878 * pass through the driver. The function below is called from the request
879 * handling part of the driver (the "bottom" part). Safe storage for the request
880 * should be allocated with ide_tape_next_{pc,rq}_storage() prior to that.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100882 * Memory for those requests is pre-allocated at initialization time, and is
883 * limited to IDETAPE_PC_STACK requests. We assume that we have enough space for
884 * the maximum possible number of inter-dependent packet commands.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100886 * The higher level of the driver - The ioctl handler and the character device
887 * handling functions should queue request to the lower level part and wait for
888 * their completion using idetape_queue_pc_tail or idetape_queue_rw_tail.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889 */
Borislav Petkovd236d742008-04-18 00:46:27 +0200890static void idetape_queue_pc_head(ide_drive_t *drive, struct ide_atapi_pc *pc,
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100891 struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892{
893 struct ide_tape_obj *tape = drive->driver_data;
894
895 idetape_init_rq(rq, REQ_IDETAPE_PC1);
896 rq->buffer = (char *) pc;
897 rq->rq_disk = tape->disk;
898 (void) ide_do_drive_cmd(drive, rq, ide_preempt);
899}
900
901/*
902 * idetape_retry_pc is called when an error was detected during the
903 * last packet command. We queue a request sense packet command in
904 * the head of the request list.
905 */
906static ide_startstop_t idetape_retry_pc (ide_drive_t *drive)
907{
908 idetape_tape_t *tape = drive->driver_data;
Borislav Petkovd236d742008-04-18 00:46:27 +0200909 struct ide_atapi_pc *pc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910 struct request *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911
Bartlomiej Zolnierkiewicz64a57fe2008-02-06 02:57:51 +0100912 (void)ide_read_error(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913 pc = idetape_next_pc_storage(drive);
914 rq = idetape_next_rq_storage(drive);
915 idetape_create_request_sense_cmd(pc);
Borislav Petkov346331f2008-04-18 00:46:26 +0200916 set_bit(IDETAPE_FLAG_IGNORE_DSC, &tape->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917 idetape_queue_pc_head(drive, pc, rq);
918 return ide_stopped;
919}
920
921/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100922 * Postpone the current request so that ide.c will be able to service requests
923 * from another device on the same hwgroup while we are polling for DSC.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924 */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100925static void idetape_postpone_request(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926{
927 idetape_tape_t *tape = drive->driver_data;
928
Borislav Petkov8004a8c2008-02-06 02:57:51 +0100929 debug_log(DBG_PROCS, "Enter %s\n", __func__);
930
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931 tape->postponed_rq = HWGROUP(drive)->rq;
Borislav Petkov54bb2072008-02-06 02:57:52 +0100932 ide_stall_queue(drive, tape->dsc_poll_freq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933}
934
Borislav Petkovd236d742008-04-18 00:46:27 +0200935typedef void idetape_io_buf(ide_drive_t *, struct ide_atapi_pc *, unsigned int);
Borislav Petkova1efc852008-02-06 02:57:52 +0100936
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937/*
Borislav Petkova1efc852008-02-06 02:57:52 +0100938 * This is the usual interrupt handler which will be called during a packet
939 * command. We will transfer some of the data (as requested by the drive) and
940 * will re-point interrupt handler to us. When data transfer is finished, we
941 * will act according to the algorithm described before
Borislav Petkov8d06bfa2008-02-06 02:57:53 +0100942 * idetape_issue_pc.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943 */
Borislav Petkova1efc852008-02-06 02:57:52 +0100944static ide_startstop_t idetape_pc_intr(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945{
946 ide_hwif_t *hwif = drive->hwif;
947 idetape_tape_t *tape = drive->driver_data;
Borislav Petkovd236d742008-04-18 00:46:27 +0200948 struct ide_atapi_pc *pc = tape->pc;
Borislav Petkova1efc852008-02-06 02:57:52 +0100949 xfer_func_t *xferfunc;
950 idetape_io_buf *iobuf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951 unsigned int temp;
952#if SIMULATE_ERRORS
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100953 static int error_sim_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954#endif
Bartlomiej Zolnierkiewicz790d1232008-01-25 22:17:12 +0100955 u16 bcount;
Bartlomiej Zolnierkiewicz8e7657a2008-01-25 22:17:12 +0100956 u8 stat, ireason;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957
Borislav Petkov8004a8c2008-02-06 02:57:51 +0100958 debug_log(DBG_PROCS, "Enter %s - interrupt handler\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959
960 /* Clear the interrupt */
Bartlomiej Zolnierkiewiczc47137a2008-02-06 02:57:51 +0100961 stat = ide_read_status(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962
Borislav Petkov346331f2008-04-18 00:46:26 +0200963 if (pc->flags & PC_FLAG_DMA_IN_PROGRESS) {
Bartlomiej Zolnierkiewicz5e37bdc2008-04-26 22:25:24 +0200964 if (hwif->dma_ops->dma_end(drive) || (stat & ERR_STAT)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965 /*
966 * A DMA error is sometimes expected. For example,
967 * if the tape is crossing a filemark during a
968 * READ command, it will issue an irq and position
969 * itself before the filemark, so that only a partial
970 * data transfer will occur (which causes the DMA
971 * error). In that case, we will later ask the tape
972 * how much bytes of the original request were
973 * actually transferred (we can't receive that
974 * information from the DMA engine on most chipsets).
975 */
976
977 /*
978 * On the contrary, a DMA error is never expected;
979 * it usually indicates a hardware error or abort.
980 * If the tape crosses a filemark during a READ
981 * command, it will issue an irq and position itself
982 * after the filemark (not before). Only a partial
983 * data transfer will occur, but no DMA error.
984 * (AS, 19 Apr 2001)
985 */
Borislav Petkov346331f2008-04-18 00:46:26 +0200986 pc->flags |= PC_FLAG_DMA_ERROR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987 } else {
Borislav Petkovd236d742008-04-18 00:46:27 +0200988 pc->xferred = pc->req_xfer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989 idetape_update_buffers(pc);
990 }
Borislav Petkov8004a8c2008-02-06 02:57:51 +0100991 debug_log(DBG_PROCS, "DMA finished\n");
992
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993 }
994
995 /* No more interrupts */
Bartlomiej Zolnierkiewicz22c525b2008-01-25 22:17:11 +0100996 if ((stat & DRQ_STAT) == 0) {
Borislav Petkov8004a8c2008-02-06 02:57:51 +0100997 debug_log(DBG_SENSE, "Packet command completed, %d bytes"
Borislav Petkovd236d742008-04-18 00:46:27 +0200998 " transferred\n", pc->xferred);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999
Borislav Petkov346331f2008-04-18 00:46:26 +02001000 pc->flags &= ~PC_FLAG_DMA_IN_PROGRESS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001 local_irq_enable();
1002
1003#if SIMULATE_ERRORS
Borislav Petkov90699ce2008-02-02 19:56:50 +01001004 if ((pc->c[0] == WRITE_6 || pc->c[0] == READ_6) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005 (++error_sim_count % 100) == 0) {
1006 printk(KERN_INFO "ide-tape: %s: simulating error\n",
1007 tape->name);
Bartlomiej Zolnierkiewicz22c525b2008-01-25 22:17:11 +01001008 stat |= ERR_STAT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009 }
1010#endif
Borislav Petkov90699ce2008-02-02 19:56:50 +01001011 if ((stat & ERR_STAT) && pc->c[0] == REQUEST_SENSE)
Bartlomiej Zolnierkiewicz22c525b2008-01-25 22:17:11 +01001012 stat &= ~ERR_STAT;
Borislav Petkov346331f2008-04-18 00:46:26 +02001013 if ((stat & ERR_STAT) || (pc->flags & PC_FLAG_DMA_ERROR)) {
Bartlomiej Zolnierkiewicz22c525b2008-01-25 22:17:11 +01001014 /* Error detected */
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001015 debug_log(DBG_ERR, "%s: I/O error\n", tape->name);
1016
Borislav Petkov90699ce2008-02-02 19:56:50 +01001017 if (pc->c[0] == REQUEST_SENSE) {
Borislav Petkova1efc852008-02-06 02:57:52 +01001018 printk(KERN_ERR "ide-tape: I/O error in request"
1019 " sense command\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020 return ide_do_reset(drive);
1021 }
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001022 debug_log(DBG_ERR, "[cmd %x]: check condition\n",
1023 pc->c[0]);
1024
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025 /* Retry operation */
1026 return idetape_retry_pc(drive);
1027 }
1028 pc->error = 0;
Borislav Petkov346331f2008-04-18 00:46:26 +02001029 if ((pc->flags & PC_FLAG_WAIT_FOR_DSC) &&
Bartlomiej Zolnierkiewicz22c525b2008-01-25 22:17:11 +01001030 (stat & SEEK_STAT) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031 /* Media access command */
1032 tape->dsc_polling_start = jiffies;
Borislav Petkov54bb2072008-02-06 02:57:52 +01001033 tape->dsc_poll_freq = IDETAPE_DSC_MA_FAST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034 tape->dsc_timeout = jiffies + IDETAPE_DSC_MA_TIMEOUT;
1035 /* Allow ide.c to handle other requests */
1036 idetape_postpone_request(drive);
1037 return ide_stopped;
1038 }
1039 if (tape->failed_pc == pc)
1040 tape->failed_pc = NULL;
1041 /* Command finished - Call the callback function */
Borislav Petkovd236d742008-04-18 00:46:27 +02001042 return pc->idetape_callback(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043 }
Borislav Petkov346331f2008-04-18 00:46:26 +02001044
1045 if (pc->flags & PC_FLAG_DMA_IN_PROGRESS) {
1046 pc->flags &= ~PC_FLAG_DMA_IN_PROGRESS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047 printk(KERN_ERR "ide-tape: The tape wants to issue more "
1048 "interrupts in DMA mode\n");
1049 printk(KERN_ERR "ide-tape: DMA disabled, reverting to PIO\n");
Bartlomiej Zolnierkiewicz7469aaf2007-02-17 02:40:26 +01001050 ide_dma_off(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051 return ide_do_reset(drive);
1052 }
1053 /* Get the number of bytes to transfer on this interrupt. */
Bartlomiej Zolnierkiewicz23579a22008-04-18 00:46:26 +02001054 bcount = (hwif->INB(hwif->io_ports[IDE_BCOUNTH_OFFSET]) << 8) |
1055 hwif->INB(hwif->io_ports[IDE_BCOUNTL_OFFSET]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056
Bartlomiej Zolnierkiewicz23579a22008-04-18 00:46:26 +02001057 ireason = hwif->INB(hwif->io_ports[IDE_IREASON_OFFSET]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058
Bartlomiej Zolnierkiewicz8e7657a2008-01-25 22:17:12 +01001059 if (ireason & CD) {
Borislav Petkova1efc852008-02-06 02:57:52 +01001060 printk(KERN_ERR "ide-tape: CoD != 0 in %s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061 return ide_do_reset(drive);
1062 }
Borislav Petkov346331f2008-04-18 00:46:26 +02001063 if (((ireason & IO) == IO) == !!(pc->flags & PC_FLAG_WRITING)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064 /* Hopefully, we will never get here */
1065 printk(KERN_ERR "ide-tape: We wanted to %s, ",
Bartlomiej Zolnierkiewicz8e7657a2008-01-25 22:17:12 +01001066 (ireason & IO) ? "Write" : "Read");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067 printk(KERN_ERR "ide-tape: but the tape wants us to %s !\n",
Bartlomiej Zolnierkiewicz8e7657a2008-01-25 22:17:12 +01001068 (ireason & IO) ? "Read" : "Write");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069 return ide_do_reset(drive);
1070 }
Borislav Petkov346331f2008-04-18 00:46:26 +02001071 if (!(pc->flags & PC_FLAG_WRITING)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072 /* Reading - Check that we have enough space */
Borislav Petkovd236d742008-04-18 00:46:27 +02001073 temp = pc->xferred + bcount;
1074 if (temp > pc->req_xfer) {
1075 if (temp > pc->buf_size) {
Borislav Petkova1efc852008-02-06 02:57:52 +01001076 printk(KERN_ERR "ide-tape: The tape wants to "
1077 "send us more data than expected "
1078 "- discarding data\n");
Bartlomiej Zolnierkiewicz7616c0a2008-04-18 00:46:26 +02001079 ide_atapi_discard_data(drive, bcount);
Borislav Petkova1efc852008-02-06 02:57:52 +01001080 ide_set_handler(drive, &idetape_pc_intr,
1081 IDETAPE_WAIT_CMD, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082 return ide_started;
1083 }
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001084 debug_log(DBG_SENSE, "The tape wants to send us more "
1085 "data than expected - allowing transfer\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086 }
Borislav Petkova1efc852008-02-06 02:57:52 +01001087 iobuf = &idetape_input_buffers;
1088 xferfunc = hwif->atapi_input_bytes;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089 } else {
Borislav Petkova1efc852008-02-06 02:57:52 +01001090 iobuf = &idetape_output_buffers;
1091 xferfunc = hwif->atapi_output_bytes;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092 }
Borislav Petkova1efc852008-02-06 02:57:52 +01001093
1094 if (pc->bh)
1095 iobuf(drive, pc, bcount);
1096 else
Borislav Petkovd236d742008-04-18 00:46:27 +02001097 xferfunc(drive, pc->cur_pos, bcount);
Borislav Petkova1efc852008-02-06 02:57:52 +01001098
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099 /* Update the current position */
Borislav Petkovd236d742008-04-18 00:46:27 +02001100 pc->xferred += bcount;
1101 pc->cur_pos += bcount;
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001102
1103 debug_log(DBG_SENSE, "[cmd %x] transferred %d bytes on that intr.\n",
1104 pc->c[0], bcount);
1105
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106 /* And set the interrupt handler again */
1107 ide_set_handler(drive, &idetape_pc_intr, IDETAPE_WAIT_CMD, NULL);
1108 return ide_started;
1109}
1110
1111/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001112 * Packet Command Interface
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001114 * The current Packet Command is available in tape->pc, and will not change
1115 * until we finish handling it. Each packet command is associated with a
1116 * callback function that will be called when the command is finished.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001118 * The handling will be done in three stages:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001120 * 1. idetape_issue_pc will send the packet command to the drive, and will set
1121 * the interrupt handler to idetape_pc_intr.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001123 * 2. On each interrupt, idetape_pc_intr will be called. This step will be
1124 * repeated until the device signals us that no more interrupts will be issued.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001126 * 3. ATAPI Tape media access commands have immediate status with a delayed
1127 * process. In case of a successful initiation of a media access packet command,
1128 * the DSC bit will be set when the actual execution of the command is finished.
1129 * Since the tape drive will not issue an interrupt, we have to poll for this
1130 * event. In this case, we define the request as "low priority request" by
1131 * setting rq_status to IDETAPE_RQ_POSTPONED, set a timer to poll for DSC and
1132 * exit the driver.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001134 * ide.c will then give higher priority to requests which originate from the
1135 * other device, until will change rq_status to RQ_ACTIVE.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001137 * 4. When the packet command is finished, it will be checked for errors.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001139 * 5. In case an error was found, we queue a request sense packet command in
1140 * front of the request queue and retry the operation up to
1141 * IDETAPE_MAX_PC_RETRIES times.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001143 * 6. In case no error was found, or we decided to give up and not to retry
1144 * again, the callback function will be called and then we will handle the next
1145 * request.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146 */
1147static ide_startstop_t idetape_transfer_pc(ide_drive_t *drive)
1148{
1149 ide_hwif_t *hwif = drive->hwif;
1150 idetape_tape_t *tape = drive->driver_data;
Borislav Petkovd236d742008-04-18 00:46:27 +02001151 struct ide_atapi_pc *pc = tape->pc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152 int retries = 100;
1153 ide_startstop_t startstop;
Bartlomiej Zolnierkiewicz8e7657a2008-01-25 22:17:12 +01001154 u8 ireason;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001156 if (ide_wait_stat(&startstop, drive, DRQ_STAT, BUSY_STAT, WAIT_READY)) {
1157 printk(KERN_ERR "ide-tape: Strange, packet command initiated "
1158 "yet DRQ isn't asserted\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159 return startstop;
1160 }
Bartlomiej Zolnierkiewicz23579a22008-04-18 00:46:26 +02001161 ireason = hwif->INB(hwif->io_ports[IDE_IREASON_OFFSET]);
Bartlomiej Zolnierkiewicz8e7657a2008-01-25 22:17:12 +01001162 while (retries-- && ((ireason & CD) == 0 || (ireason & IO))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163 printk(KERN_ERR "ide-tape: (IO,CoD != (0,1) while issuing "
1164 "a packet command, retrying\n");
1165 udelay(100);
Bartlomiej Zolnierkiewicz23579a22008-04-18 00:46:26 +02001166 ireason = hwif->INB(hwif->io_ports[IDE_IREASON_OFFSET]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167 if (retries == 0) {
1168 printk(KERN_ERR "ide-tape: (IO,CoD != (0,1) while "
1169 "issuing a packet command, ignoring\n");
Bartlomiej Zolnierkiewicz8e7657a2008-01-25 22:17:12 +01001170 ireason |= CD;
1171 ireason &= ~IO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172 }
1173 }
Bartlomiej Zolnierkiewicz8e7657a2008-01-25 22:17:12 +01001174 if ((ireason & CD) == 0 || (ireason & IO)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175 printk(KERN_ERR "ide-tape: (IO,CoD) != (0,1) while issuing "
1176 "a packet command\n");
1177 return ide_do_reset(drive);
1178 }
1179 /* Set the interrupt routine */
1180 ide_set_handler(drive, &idetape_pc_intr, IDETAPE_WAIT_CMD, NULL);
1181#ifdef CONFIG_BLK_DEV_IDEDMA
1182 /* Begin DMA, if necessary */
Borislav Petkov346331f2008-04-18 00:46:26 +02001183 if (pc->flags & PC_FLAG_DMA_IN_PROGRESS)
Bartlomiej Zolnierkiewicz5e37bdc2008-04-26 22:25:24 +02001184 hwif->dma_ops->dma_start(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185#endif
1186 /* Send the actual packet */
1187 HWIF(drive)->atapi_output_bytes(drive, pc->c, 12);
1188 return ide_started;
1189}
1190
Borislav Petkovd236d742008-04-18 00:46:27 +02001191static ide_startstop_t idetape_issue_pc(ide_drive_t *drive,
1192 struct ide_atapi_pc *pc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193{
1194 ide_hwif_t *hwif = drive->hwif;
1195 idetape_tape_t *tape = drive->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196 int dma_ok = 0;
Bartlomiej Zolnierkiewicz790d1232008-01-25 22:17:12 +01001197 u16 bcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198
Borislav Petkov90699ce2008-02-02 19:56:50 +01001199 if (tape->pc->c[0] == REQUEST_SENSE &&
1200 pc->c[0] == REQUEST_SENSE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201 printk(KERN_ERR "ide-tape: possible ide-tape.c bug - "
1202 "Two request sense in serial were issued\n");
1203 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204
Borislav Petkov90699ce2008-02-02 19:56:50 +01001205 if (tape->failed_pc == NULL && pc->c[0] != REQUEST_SENSE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206 tape->failed_pc = pc;
1207 /* Set the current packet command */
1208 tape->pc = pc;
1209
1210 if (pc->retries > IDETAPE_MAX_PC_RETRIES ||
Borislav Petkov346331f2008-04-18 00:46:26 +02001211 (pc->flags & PC_FLAG_ABORT)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212 /*
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001213 * We will "abort" retrying a packet command in case legitimate
1214 * error code was received (crossing a filemark, or end of the
1215 * media, for example).
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216 */
Borislav Petkov346331f2008-04-18 00:46:26 +02001217 if (!(pc->flags & PC_FLAG_ABORT)) {
Borislav Petkov90699ce2008-02-02 19:56:50 +01001218 if (!(pc->c[0] == TEST_UNIT_READY &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219 tape->sense_key == 2 && tape->asc == 4 &&
1220 (tape->ascq == 1 || tape->ascq == 8))) {
1221 printk(KERN_ERR "ide-tape: %s: I/O error, "
1222 "pc = %2x, key = %2x, "
1223 "asc = %2x, ascq = %2x\n",
1224 tape->name, pc->c[0],
1225 tape->sense_key, tape->asc,
1226 tape->ascq);
1227 }
1228 /* Giving up */
1229 pc->error = IDETAPE_ERROR_GENERAL;
1230 }
1231 tape->failed_pc = NULL;
Borislav Petkovd236d742008-04-18 00:46:27 +02001232 return pc->idetape_callback(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233 }
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001234 debug_log(DBG_SENSE, "Retry #%d, cmd = %02X\n", pc->retries, pc->c[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235
1236 pc->retries++;
1237 /* We haven't transferred any data yet */
Borislav Petkovd236d742008-04-18 00:46:27 +02001238 pc->xferred = 0;
1239 pc->cur_pos = pc->buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240 /* Request to transfer the entire buffer at once */
Borislav Petkovd236d742008-04-18 00:46:27 +02001241 bcount = pc->req_xfer;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242
Borislav Petkov346331f2008-04-18 00:46:26 +02001243 if (pc->flags & PC_FLAG_DMA_ERROR) {
1244 pc->flags &= ~PC_FLAG_DMA_ERROR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245 printk(KERN_WARNING "ide-tape: DMA disabled, "
1246 "reverting to PIO\n");
Bartlomiej Zolnierkiewicz7469aaf2007-02-17 02:40:26 +01001247 ide_dma_off(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248 }
Borislav Petkov346331f2008-04-18 00:46:26 +02001249 if ((pc->flags & PC_FLAG_DMA_RECOMMENDED) && drive->using_dma)
Bartlomiej Zolnierkiewicz5e37bdc2008-04-26 22:25:24 +02001250 dma_ok = !hwif->dma_ops->dma_setup(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251
Bartlomiej Zolnierkiewicz2fc57382008-01-25 22:17:13 +01001252 ide_pktcmd_tf_load(drive, IDE_TFLAG_NO_SELECT_MASK |
1253 IDE_TFLAG_OUT_DEVICE, bcount, dma_ok);
1254
Borislav Petkov346331f2008-04-18 00:46:26 +02001255 if (dma_ok)
1256 /* Will begin DMA later */
1257 pc->flags |= PC_FLAG_DMA_IN_PROGRESS;
1258 if (test_bit(IDETAPE_FLAG_DRQ_INTERRUPT, &tape->flags)) {
Bartlomiej Zolnierkiewiczc1c9dbc2008-02-02 19:56:44 +01001259 ide_execute_command(drive, WIN_PACKETCMD, &idetape_transfer_pc,
1260 IDETAPE_WAIT_CMD, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261 return ide_started;
1262 } else {
Bartlomiej Zolnierkiewicz23579a22008-04-18 00:46:26 +02001263 hwif->OUTB(WIN_PACKETCMD, hwif->io_ports[IDE_COMMAND_OFFSET]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264 return idetape_transfer_pc(drive);
1265 }
1266}
1267
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001268static ide_startstop_t idetape_pc_callback(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269{
1270 idetape_tape_t *tape = drive->driver_data;
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001271
1272 debug_log(DBG_PROCS, "Enter %s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273
1274 idetape_end_request(drive, tape->pc->error ? 0 : 1, 0);
1275 return ide_stopped;
1276}
1277
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001278/* A mode sense command is used to "sense" tape parameters. */
Borislav Petkovd236d742008-04-18 00:46:27 +02001279static void idetape_create_mode_sense_cmd(struct ide_atapi_pc *pc, u8 page_code)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280{
1281 idetape_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +01001282 pc->c[0] = MODE_SENSE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283 if (page_code != IDETAPE_BLOCK_DESCRIPTOR)
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001284 /* DBD = 1 - Don't return block descriptors */
1285 pc->c[1] = 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286 pc->c[2] = page_code;
1287 /*
1288 * Changed pc->c[3] to 0 (255 will at best return unused info).
1289 *
1290 * For SCSI this byte is defined as subpage instead of high byte
1291 * of length and some IDE drives seem to interpret it this way
1292 * and return an error when 255 is used.
1293 */
1294 pc->c[3] = 0;
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001295 /* We will just discard data in that case */
1296 pc->c[4] = 255;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297 if (page_code == IDETAPE_BLOCK_DESCRIPTOR)
Borislav Petkovd236d742008-04-18 00:46:27 +02001298 pc->req_xfer = 12;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299 else if (page_code == IDETAPE_CAPABILITIES_PAGE)
Borislav Petkovd236d742008-04-18 00:46:27 +02001300 pc->req_xfer = 24;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301 else
Borislav Petkovd236d742008-04-18 00:46:27 +02001302 pc->req_xfer = 50;
1303 pc->idetape_callback = &idetape_pc_callback;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304}
1305
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001306static ide_startstop_t idetape_media_access_finished(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307{
1308 idetape_tape_t *tape = drive->driver_data;
Borislav Petkovd236d742008-04-18 00:46:27 +02001309 struct ide_atapi_pc *pc = tape->pc;
Bartlomiej Zolnierkiewicz22c525b2008-01-25 22:17:11 +01001310 u8 stat;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311
Bartlomiej Zolnierkiewiczc47137a2008-02-06 02:57:51 +01001312 stat = ide_read_status(drive);
1313
Bartlomiej Zolnierkiewicz22c525b2008-01-25 22:17:11 +01001314 if (stat & SEEK_STAT) {
1315 if (stat & ERR_STAT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316 /* Error detected */
Borislav Petkov90699ce2008-02-02 19:56:50 +01001317 if (pc->c[0] != TEST_UNIT_READY)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318 printk(KERN_ERR "ide-tape: %s: I/O error, ",
1319 tape->name);
1320 /* Retry operation */
1321 return idetape_retry_pc(drive);
1322 }
1323 pc->error = 0;
1324 if (tape->failed_pc == pc)
1325 tape->failed_pc = NULL;
1326 } else {
1327 pc->error = IDETAPE_ERROR_GENERAL;
1328 tape->failed_pc = NULL;
1329 }
Borislav Petkovd236d742008-04-18 00:46:27 +02001330 return pc->idetape_callback(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331}
1332
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001333static ide_startstop_t idetape_rw_callback(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334{
1335 idetape_tape_t *tape = drive->driver_data;
1336 struct request *rq = HWGROUP(drive)->rq;
Borislav Petkovd236d742008-04-18 00:46:27 +02001337 int blocks = tape->pc->xferred / tape->blk_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338
Borislav Petkov54bb2072008-02-06 02:57:52 +01001339 tape->avg_size += blocks * tape->blk_size;
1340 tape->insert_size += blocks * tape->blk_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341 if (tape->insert_size > 1024 * 1024)
1342 tape->measure_insert_time = 1;
1343 if (tape->measure_insert_time) {
1344 tape->measure_insert_time = 0;
1345 tape->insert_time = jiffies;
1346 tape->insert_size = 0;
1347 }
1348 if (time_after(jiffies, tape->insert_time))
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001349 tape->insert_speed = tape->insert_size / 1024 * HZ /
1350 (jiffies - tape->insert_time);
Marcelo Feitoza Parisi9bae1ff2006-03-28 01:56:46 -08001351 if (time_after_eq(jiffies, tape->avg_time + HZ)) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001352 tape->avg_speed = tape->avg_size * HZ /
1353 (jiffies - tape->avg_time) / 1024;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354 tape->avg_size = 0;
1355 tape->avg_time = jiffies;
1356 }
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001357 debug_log(DBG_PROCS, "Enter %s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358
Borislav Petkov54bb2072008-02-06 02:57:52 +01001359 tape->first_frame += blocks;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360 rq->current_nr_sectors -= blocks;
1361
1362 if (!tape->pc->error)
1363 idetape_end_request(drive, 1, 0);
1364 else
1365 idetape_end_request(drive, tape->pc->error, 0);
1366 return ide_stopped;
1367}
1368
Borislav Petkovd236d742008-04-18 00:46:27 +02001369static void idetape_create_read_cmd(idetape_tape_t *tape,
1370 struct ide_atapi_pc *pc,
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001371 unsigned int length, struct idetape_bh *bh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372{
1373 idetape_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +01001374 pc->c[0] = READ_6;
Borislav Petkov860ff5e2008-02-02 19:56:50 +01001375 put_unaligned(cpu_to_be32(length), (unsigned int *) &pc->c[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376 pc->c[1] = 1;
Borislav Petkovd236d742008-04-18 00:46:27 +02001377 pc->idetape_callback = &idetape_rw_callback;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378 pc->bh = bh;
1379 atomic_set(&bh->b_count, 0);
Borislav Petkovd236d742008-04-18 00:46:27 +02001380 pc->buf = NULL;
1381 pc->buf_size = length * tape->blk_size;
1382 pc->req_xfer = pc->buf_size;
1383 if (pc->req_xfer == tape->stage_size)
Borislav Petkov346331f2008-04-18 00:46:26 +02001384 pc->flags |= PC_FLAG_DMA_RECOMMENDED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385}
1386
Borislav Petkovd236d742008-04-18 00:46:27 +02001387static void idetape_create_write_cmd(idetape_tape_t *tape,
1388 struct ide_atapi_pc *pc,
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001389 unsigned int length, struct idetape_bh *bh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390{
1391 idetape_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +01001392 pc->c[0] = WRITE_6;
Borislav Petkov860ff5e2008-02-02 19:56:50 +01001393 put_unaligned(cpu_to_be32(length), (unsigned int *) &pc->c[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394 pc->c[1] = 1;
Borislav Petkovd236d742008-04-18 00:46:27 +02001395 pc->idetape_callback = &idetape_rw_callback;
Borislav Petkov346331f2008-04-18 00:46:26 +02001396 pc->flags |= PC_FLAG_WRITING;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397 pc->bh = bh;
1398 pc->b_data = bh->b_data;
1399 pc->b_count = atomic_read(&bh->b_count);
Borislav Petkovd236d742008-04-18 00:46:27 +02001400 pc->buf = NULL;
1401 pc->buf_size = length * tape->blk_size;
1402 pc->req_xfer = pc->buf_size;
1403 if (pc->req_xfer == tape->stage_size)
Borislav Petkov346331f2008-04-18 00:46:26 +02001404 pc->flags |= PC_FLAG_DMA_RECOMMENDED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405}
1406
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407static ide_startstop_t idetape_do_request(ide_drive_t *drive,
1408 struct request *rq, sector_t block)
1409{
1410 idetape_tape_t *tape = drive->driver_data;
Borislav Petkovd236d742008-04-18 00:46:27 +02001411 struct ide_atapi_pc *pc = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412 struct request *postponed_rq = tape->postponed_rq;
Bartlomiej Zolnierkiewicz22c525b2008-01-25 22:17:11 +01001413 u8 stat;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001415 debug_log(DBG_SENSE, "sector: %ld, nr_sectors: %ld,"
1416 " current_nr_sectors: %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417 rq->sector, rq->nr_sectors, rq->current_nr_sectors);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418
Jens Axboe4aff5e22006-08-10 08:44:47 +02001419 if (!blk_special_request(rq)) {
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001420 /* We do not support buffer cache originated requests. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421 printk(KERN_NOTICE "ide-tape: %s: Unsupported request in "
Jens Axboe4aff5e22006-08-10 08:44:47 +02001422 "request queue (%d)\n", drive->name, rq->cmd_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423 ide_end_request(drive, 0, 0);
1424 return ide_stopped;
1425 }
1426
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001427 /* Retry a failed packet command */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001428 if (tape->failed_pc && tape->pc->c[0] == REQUEST_SENSE)
Borislav Petkov8d06bfa2008-02-06 02:57:53 +01001429 return idetape_issue_pc(drive, tape->failed_pc);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001430
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431 if (postponed_rq != NULL)
1432 if (rq != postponed_rq) {
1433 printk(KERN_ERR "ide-tape: ide-tape.c bug - "
1434 "Two DSC requests were queued\n");
1435 idetape_end_request(drive, 0, 0);
1436 return ide_stopped;
1437 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438
1439 tape->postponed_rq = NULL;
1440
1441 /*
1442 * If the tape is still busy, postpone our request and service
1443 * the other device meanwhile.
1444 */
Bartlomiej Zolnierkiewiczc47137a2008-02-06 02:57:51 +01001445 stat = ide_read_status(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446
1447 if (!drive->dsc_overlap && !(rq->cmd[0] & REQ_IDETAPE_PC2))
Borislav Petkov346331f2008-04-18 00:46:26 +02001448 set_bit(IDETAPE_FLAG_IGNORE_DSC, &tape->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449
1450 if (drive->post_reset == 1) {
Borislav Petkov346331f2008-04-18 00:46:26 +02001451 set_bit(IDETAPE_FLAG_IGNORE_DSC, &tape->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452 drive->post_reset = 0;
1453 }
1454
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455 if (time_after(jiffies, tape->insert_time))
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001456 tape->insert_speed = tape->insert_size / 1024 * HZ /
1457 (jiffies - tape->insert_time);
Borislav Petkov346331f2008-04-18 00:46:26 +02001458 if (!test_and_clear_bit(IDETAPE_FLAG_IGNORE_DSC, &tape->flags) &&
Bartlomiej Zolnierkiewicz22c525b2008-01-25 22:17:11 +01001459 (stat & SEEK_STAT) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460 if (postponed_rq == NULL) {
1461 tape->dsc_polling_start = jiffies;
Borislav Petkov54bb2072008-02-06 02:57:52 +01001462 tape->dsc_poll_freq = tape->best_dsc_rw_freq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463 tape->dsc_timeout = jiffies + IDETAPE_DSC_RW_TIMEOUT;
1464 } else if (time_after(jiffies, tape->dsc_timeout)) {
1465 printk(KERN_ERR "ide-tape: %s: DSC timeout\n",
1466 tape->name);
1467 if (rq->cmd[0] & REQ_IDETAPE_PC2) {
1468 idetape_media_access_finished(drive);
1469 return ide_stopped;
1470 } else {
1471 return ide_do_reset(drive);
1472 }
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001473 } else if (time_after(jiffies,
1474 tape->dsc_polling_start +
1475 IDETAPE_DSC_MA_THRESHOLD))
Borislav Petkov54bb2072008-02-06 02:57:52 +01001476 tape->dsc_poll_freq = IDETAPE_DSC_MA_SLOW;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477 idetape_postpone_request(drive);
1478 return ide_stopped;
1479 }
1480 if (rq->cmd[0] & REQ_IDETAPE_READ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481 tape->postpone_cnt = 0;
1482 pc = idetape_next_pc_storage(drive);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001483 idetape_create_read_cmd(tape, pc, rq->current_nr_sectors,
1484 (struct idetape_bh *)rq->special);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485 goto out;
1486 }
1487 if (rq->cmd[0] & REQ_IDETAPE_WRITE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488 tape->postpone_cnt = 0;
1489 pc = idetape_next_pc_storage(drive);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001490 idetape_create_write_cmd(tape, pc, rq->current_nr_sectors,
1491 (struct idetape_bh *)rq->special);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492 goto out;
1493 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494 if (rq->cmd[0] & REQ_IDETAPE_PC1) {
Borislav Petkovd236d742008-04-18 00:46:27 +02001495 pc = (struct ide_atapi_pc *) rq->buffer;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496 rq->cmd[0] &= ~(REQ_IDETAPE_PC1);
1497 rq->cmd[0] |= REQ_IDETAPE_PC2;
1498 goto out;
1499 }
1500 if (rq->cmd[0] & REQ_IDETAPE_PC2) {
1501 idetape_media_access_finished(drive);
1502 return ide_stopped;
1503 }
1504 BUG();
1505out:
Borislav Petkov8d06bfa2008-02-06 02:57:53 +01001506 return idetape_issue_pc(drive, pc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507}
1508
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001509/* Pipeline related functions */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510
1511/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001512 * The function below uses __get_free_page to allocate a pipeline stage, along
1513 * with all the necessary small buffers which together make a buffer of size
1514 * tape->stage_size (or a bit more). We attempt to combine sequential pages as
1515 * much as possible.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001516 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001517 * It returns a pointer to the new allocated stage, or NULL if we can't (or
1518 * don't want to) allocate a stage.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001520 * Pipeline stages are optional and are used to increase performance. If we
1521 * can't allocate them, we'll manage without them.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522 */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001523static idetape_stage_t *__idetape_kmalloc_stage(idetape_tape_t *tape, int full,
1524 int clear)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001525{
1526 idetape_stage_t *stage;
1527 struct idetape_bh *prev_bh, *bh;
1528 int pages = tape->pages_per_stage;
1529 char *b_data = NULL;
1530
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001531 stage = kmalloc(sizeof(idetape_stage_t), GFP_KERNEL);
1532 if (!stage)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533 return NULL;
1534 stage->next = NULL;
1535
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001536 stage->bh = kmalloc(sizeof(struct idetape_bh), GFP_KERNEL);
1537 bh = stage->bh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001538 if (bh == NULL)
1539 goto abort;
1540 bh->b_reqnext = NULL;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001541 bh->b_data = (char *) __get_free_page(GFP_KERNEL);
1542 if (!bh->b_data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001543 goto abort;
1544 if (clear)
1545 memset(bh->b_data, 0, PAGE_SIZE);
1546 bh->b_size = PAGE_SIZE;
1547 atomic_set(&bh->b_count, full ? bh->b_size : 0);
1548
1549 while (--pages) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001550 b_data = (char *) __get_free_page(GFP_KERNEL);
1551 if (!b_data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001552 goto abort;
1553 if (clear)
1554 memset(b_data, 0, PAGE_SIZE);
1555 if (bh->b_data == b_data + PAGE_SIZE) {
1556 bh->b_size += PAGE_SIZE;
1557 bh->b_data -= PAGE_SIZE;
1558 if (full)
1559 atomic_add(PAGE_SIZE, &bh->b_count);
1560 continue;
1561 }
1562 if (b_data == bh->b_data + bh->b_size) {
1563 bh->b_size += PAGE_SIZE;
1564 if (full)
1565 atomic_add(PAGE_SIZE, &bh->b_count);
1566 continue;
1567 }
1568 prev_bh = bh;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001569 bh = kmalloc(sizeof(struct idetape_bh), GFP_KERNEL);
1570 if (!bh) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571 free_page((unsigned long) b_data);
1572 goto abort;
1573 }
1574 bh->b_reqnext = NULL;
1575 bh->b_data = b_data;
1576 bh->b_size = PAGE_SIZE;
1577 atomic_set(&bh->b_count, full ? bh->b_size : 0);
1578 prev_bh->b_reqnext = bh;
1579 }
1580 bh->b_size -= tape->excess_bh_size;
1581 if (full)
1582 atomic_sub(tape->excess_bh_size, &bh->b_count);
1583 return stage;
1584abort:
1585 __idetape_kfree_stage(stage);
1586 return NULL;
1587}
1588
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001589static int idetape_copy_stage_from_user(idetape_tape_t *tape,
Borislav Petkov8646c882008-04-27 15:38:26 +02001590 const char __user *buf, int n)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591{
1592 struct idetape_bh *bh = tape->bh;
1593 int count;
Daniel Walkerdcd96372006-06-25 05:47:37 -07001594 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001595
1596 while (n) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001597 if (bh == NULL) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001598 printk(KERN_ERR "ide-tape: bh == NULL in %s\n",
1599 __func__);
Daniel Walkerdcd96372006-06-25 05:47:37 -07001600 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001601 }
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001602 count = min((unsigned int)
1603 (bh->b_size - atomic_read(&bh->b_count)),
1604 (unsigned int)n);
1605 if (copy_from_user(bh->b_data + atomic_read(&bh->b_count), buf,
1606 count))
Daniel Walkerdcd96372006-06-25 05:47:37 -07001607 ret = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608 n -= count;
1609 atomic_add(count, &bh->b_count);
1610 buf += count;
1611 if (atomic_read(&bh->b_count) == bh->b_size) {
1612 bh = bh->b_reqnext;
1613 if (bh)
1614 atomic_set(&bh->b_count, 0);
1615 }
1616 }
1617 tape->bh = bh;
Daniel Walkerdcd96372006-06-25 05:47:37 -07001618 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001619}
1620
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001621static int idetape_copy_stage_to_user(idetape_tape_t *tape, char __user *buf,
Borislav Petkov99d74e62008-04-27 15:38:25 +02001622 int n)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623{
1624 struct idetape_bh *bh = tape->bh;
1625 int count;
Daniel Walkerdcd96372006-06-25 05:47:37 -07001626 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001627
1628 while (n) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001629 if (bh == NULL) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001630 printk(KERN_ERR "ide-tape: bh == NULL in %s\n",
1631 __func__);
Daniel Walkerdcd96372006-06-25 05:47:37 -07001632 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001633 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634 count = min(tape->b_count, n);
Daniel Walkerdcd96372006-06-25 05:47:37 -07001635 if (copy_to_user(buf, tape->b_data, count))
1636 ret = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001637 n -= count;
1638 tape->b_data += count;
1639 tape->b_count -= count;
1640 buf += count;
1641 if (!tape->b_count) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001642 bh = bh->b_reqnext;
1643 tape->bh = bh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644 if (bh) {
1645 tape->b_data = bh->b_data;
1646 tape->b_count = atomic_read(&bh->b_count);
1647 }
1648 }
1649 }
Daniel Walkerdcd96372006-06-25 05:47:37 -07001650 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001651}
1652
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001653static void idetape_init_merge_stage(idetape_tape_t *tape)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654{
1655 struct idetape_bh *bh = tape->merge_stage->bh;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001656
Linus Torvalds1da177e2005-04-16 15:20:36 -07001657 tape->bh = bh;
Borislav Petkov54abf372008-02-06 02:57:52 +01001658 if (tape->chrdev_dir == IDETAPE_DIR_WRITE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001659 atomic_set(&bh->b_count, 0);
1660 else {
1661 tape->b_data = bh->b_data;
1662 tape->b_count = atomic_read(&bh->b_count);
1663 }
1664}
1665
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001666/* Install a completion in a pending request and sleep until it is serviced. The
1667 * caller should ensure that the request will not be serviced before we install
1668 * the completion (usually by disabling interrupts).
Linus Torvalds1da177e2005-04-16 15:20:36 -07001669 */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001670static void idetape_wait_for_request(ide_drive_t *drive, struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001671{
Peter Zijlstra6e9a4732006-09-30 23:28:10 -07001672 DECLARE_COMPLETION_ONSTACK(wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001673 idetape_tape_t *tape = drive->driver_data;
1674
Jens Axboe4aff5e22006-08-10 08:44:47 +02001675 if (rq == NULL || !blk_special_request(rq)) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001676 printk(KERN_ERR "ide-tape: bug: Trying to sleep on non-valid"
1677 " request\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001678 return;
1679 }
Jens Axboec00895a2006-09-30 20:29:12 +02001680 rq->end_io_data = &wait;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681 rq->end_io = blk_end_sync_rq;
Borislav Petkov54bb2072008-02-06 02:57:52 +01001682 spin_unlock_irq(&tape->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001683 wait_for_completion(&wait);
1684 /* The stage and its struct request have been deallocated */
Borislav Petkov54bb2072008-02-06 02:57:52 +01001685 spin_lock_irq(&tape->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001686}
1687
Borislav Petkova2f5b7f2008-02-06 02:57:51 +01001688static ide_startstop_t idetape_read_position_callback(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001689{
1690 idetape_tape_t *tape = drive->driver_data;
Borislav Petkovd236d742008-04-18 00:46:27 +02001691 u8 *readpos = tape->pc->buf;
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001692
1693 debug_log(DBG_PROCS, "Enter %s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694
1695 if (!tape->pc->error) {
Borislav Petkova2f5b7f2008-02-06 02:57:51 +01001696 debug_log(DBG_SENSE, "BOP - %s\n",
1697 (readpos[0] & 0x80) ? "Yes" : "No");
1698 debug_log(DBG_SENSE, "EOP - %s\n",
1699 (readpos[0] & 0x40) ? "Yes" : "No");
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001700
Borislav Petkova2f5b7f2008-02-06 02:57:51 +01001701 if (readpos[0] & 0x4) {
1702 printk(KERN_INFO "ide-tape: Block location is unknown"
1703 "to the tape\n");
Borislav Petkov346331f2008-04-18 00:46:26 +02001704 clear_bit(IDETAPE_FLAG_ADDRESS_VALID, &tape->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001705 idetape_end_request(drive, 0, 0);
1706 } else {
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001707 debug_log(DBG_SENSE, "Block Location - %u\n",
Borislav Petkova2f5b7f2008-02-06 02:57:51 +01001708 be32_to_cpu(*(u32 *)&readpos[4]));
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001709
Borislav Petkova2f5b7f2008-02-06 02:57:51 +01001710 tape->partition = readpos[1];
Borislav Petkov54bb2072008-02-06 02:57:52 +01001711 tape->first_frame =
Borislav Petkova2f5b7f2008-02-06 02:57:51 +01001712 be32_to_cpu(*(u32 *)&readpos[4]);
Borislav Petkov346331f2008-04-18 00:46:26 +02001713 set_bit(IDETAPE_FLAG_ADDRESS_VALID, &tape->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001714 idetape_end_request(drive, 1, 0);
1715 }
1716 } else {
1717 idetape_end_request(drive, 0, 0);
1718 }
1719 return ide_stopped;
1720}
1721
1722/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001723 * Write a filemark if write_filemark=1. Flush the device buffers without
1724 * writing a filemark otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001725 */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001726static void idetape_create_write_filemark_cmd(ide_drive_t *drive,
Borislav Petkovd236d742008-04-18 00:46:27 +02001727 struct ide_atapi_pc *pc, int write_filemark)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001728{
1729 idetape_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +01001730 pc->c[0] = WRITE_FILEMARKS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001731 pc->c[4] = write_filemark;
Borislav Petkov346331f2008-04-18 00:46:26 +02001732 pc->flags |= PC_FLAG_WAIT_FOR_DSC;
Borislav Petkovd236d742008-04-18 00:46:27 +02001733 pc->idetape_callback = &idetape_pc_callback;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001734}
1735
Borislav Petkovd236d742008-04-18 00:46:27 +02001736static void idetape_create_test_unit_ready_cmd(struct ide_atapi_pc *pc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001737{
1738 idetape_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +01001739 pc->c[0] = TEST_UNIT_READY;
Borislav Petkovd236d742008-04-18 00:46:27 +02001740 pc->idetape_callback = &idetape_pc_callback;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001741}
1742
1743/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001744 * We add a special packet command request to the tail of the request queue, and
1745 * wait for it to be serviced. This is not to be called from within the request
1746 * handling part of the driver! We allocate here data on the stack and it is
1747 * valid until the request is finished. This is not the case for the bottom part
1748 * of the driver, where we are always leaving the functions to wait for an
1749 * interrupt or a timer event.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001750 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001751 * From the bottom part of the driver, we should allocate safe memory using
1752 * idetape_next_pc_storage() and ide_tape_next_rq_storage(), and add the request
1753 * to the request list without waiting for it to be serviced! In that case, we
1754 * usually use idetape_queue_pc_head().
Linus Torvalds1da177e2005-04-16 15:20:36 -07001755 */
Borislav Petkovd236d742008-04-18 00:46:27 +02001756static int __idetape_queue_pc_tail(ide_drive_t *drive, struct ide_atapi_pc *pc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001757{
1758 struct ide_tape_obj *tape = drive->driver_data;
1759 struct request rq;
1760
1761 idetape_init_rq(&rq, REQ_IDETAPE_PC1);
1762 rq.buffer = (char *) pc;
1763 rq.rq_disk = tape->disk;
1764 return ide_do_drive_cmd(drive, &rq, ide_wait);
1765}
1766
Borislav Petkovd236d742008-04-18 00:46:27 +02001767static void idetape_create_load_unload_cmd(ide_drive_t *drive,
1768 struct ide_atapi_pc *pc, int cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001769{
1770 idetape_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +01001771 pc->c[0] = START_STOP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001772 pc->c[4] = cmd;
Borislav Petkov346331f2008-04-18 00:46:26 +02001773 pc->flags |= PC_FLAG_WAIT_FOR_DSC;
Borislav Petkovd236d742008-04-18 00:46:27 +02001774 pc->idetape_callback = &idetape_pc_callback;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001775}
1776
1777static int idetape_wait_ready(ide_drive_t *drive, unsigned long timeout)
1778{
1779 idetape_tape_t *tape = drive->driver_data;
Borislav Petkovd236d742008-04-18 00:46:27 +02001780 struct ide_atapi_pc pc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001781 int load_attempted = 0;
1782
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001783 /* Wait for the tape to become ready */
Borislav Petkov346331f2008-04-18 00:46:26 +02001784 set_bit(IDETAPE_FLAG_MEDIUM_PRESENT, &tape->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001785 timeout += jiffies;
1786 while (time_before(jiffies, timeout)) {
1787 idetape_create_test_unit_ready_cmd(&pc);
1788 if (!__idetape_queue_pc_tail(drive, &pc))
1789 return 0;
1790 if ((tape->sense_key == 2 && tape->asc == 4 && tape->ascq == 2)
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001791 || (tape->asc == 0x3A)) {
1792 /* no media */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001793 if (load_attempted)
1794 return -ENOMEDIUM;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001795 idetape_create_load_unload_cmd(drive, &pc,
1796 IDETAPE_LU_LOAD_MASK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001797 __idetape_queue_pc_tail(drive, &pc);
1798 load_attempted = 1;
1799 /* not about to be ready */
1800 } else if (!(tape->sense_key == 2 && tape->asc == 4 &&
1801 (tape->ascq == 1 || tape->ascq == 8)))
1802 return -EIO;
Nishanth Aravamudan80ce45f2005-09-10 00:27:08 -07001803 msleep(100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001804 }
1805 return -EIO;
1806}
1807
Borislav Petkovd236d742008-04-18 00:46:27 +02001808static int idetape_queue_pc_tail(ide_drive_t *drive, struct ide_atapi_pc *pc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001809{
1810 return __idetape_queue_pc_tail(drive, pc);
1811}
1812
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001813static int idetape_flush_tape_buffers(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001814{
Borislav Petkovd236d742008-04-18 00:46:27 +02001815 struct ide_atapi_pc pc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001816 int rc;
1817
1818 idetape_create_write_filemark_cmd(drive, &pc, 0);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001819 rc = idetape_queue_pc_tail(drive, &pc);
1820 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001821 return rc;
1822 idetape_wait_ready(drive, 60 * 5 * HZ);
1823 return 0;
1824}
1825
Borislav Petkovd236d742008-04-18 00:46:27 +02001826static void idetape_create_read_position_cmd(struct ide_atapi_pc *pc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001827{
1828 idetape_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +01001829 pc->c[0] = READ_POSITION;
Borislav Petkovd236d742008-04-18 00:46:27 +02001830 pc->req_xfer = 20;
1831 pc->idetape_callback = &idetape_read_position_callback;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001832}
1833
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001834static int idetape_read_position(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001835{
1836 idetape_tape_t *tape = drive->driver_data;
Borislav Petkovd236d742008-04-18 00:46:27 +02001837 struct ide_atapi_pc pc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001838 int position;
1839
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001840 debug_log(DBG_PROCS, "Enter %s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001841
1842 idetape_create_read_position_cmd(&pc);
1843 if (idetape_queue_pc_tail(drive, &pc))
1844 return -1;
Borislav Petkov54bb2072008-02-06 02:57:52 +01001845 position = tape->first_frame;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001846 return position;
1847}
1848
Borislav Petkovd236d742008-04-18 00:46:27 +02001849static void idetape_create_locate_cmd(ide_drive_t *drive,
1850 struct ide_atapi_pc *pc,
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001851 unsigned int block, u8 partition, int skip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001852{
1853 idetape_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +01001854 pc->c[0] = POSITION_TO_ELEMENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001855 pc->c[1] = 2;
Borislav Petkov860ff5e2008-02-02 19:56:50 +01001856 put_unaligned(cpu_to_be32(block), (unsigned int *) &pc->c[3]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001857 pc->c[8] = partition;
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 int idetape_create_prevent_cmd(ide_drive_t *drive,
1863 struct ide_atapi_pc *pc, int prevent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001864{
1865 idetape_tape_t *tape = drive->driver_data;
1866
Borislav Petkovb6422012008-02-02 19:56:49 +01001867 /* device supports locking according to capabilities page */
1868 if (!(tape->caps[6] & 0x01))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001869 return 0;
1870
1871 idetape_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +01001872 pc->c[0] = ALLOW_MEDIUM_REMOVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001873 pc->c[4] = prevent;
Borislav Petkovd236d742008-04-18 00:46:27 +02001874 pc->idetape_callback = &idetape_pc_callback;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001875 return 1;
1876}
1877
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001878static int __idetape_discard_read_pipeline(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001879{
1880 idetape_tape_t *tape = drive->driver_data;
1881 unsigned long flags;
1882 int cnt;
1883
Borislav Petkov54abf372008-02-06 02:57:52 +01001884 if (tape->chrdev_dir != IDETAPE_DIR_READ)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001885 return 0;
1886
1887 /* Remove merge stage. */
Borislav Petkov54bb2072008-02-06 02:57:52 +01001888 cnt = tape->merge_stage_size / tape->blk_size;
Borislav Petkov346331f2008-04-18 00:46:26 +02001889 if (test_and_clear_bit(IDETAPE_FLAG_FILEMARK, &tape->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001890 ++cnt; /* Filemarks count as 1 sector */
1891 tape->merge_stage_size = 0;
1892 if (tape->merge_stage != NULL) {
1893 __idetape_kfree_stage(tape->merge_stage);
1894 tape->merge_stage = NULL;
1895 }
1896
1897 /* Clear pipeline flags. */
Borislav Petkov346331f2008-04-18 00:46:26 +02001898 clear_bit(IDETAPE_FLAG_PIPELINE_ERR, &tape->flags);
Borislav Petkov54abf372008-02-06 02:57:52 +01001899 tape->chrdev_dir = IDETAPE_DIR_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001900
1901 /* Remove pipeline stages. */
1902 if (tape->first_stage == NULL)
1903 return 0;
1904
Borislav Petkov54bb2072008-02-06 02:57:52 +01001905 spin_lock_irqsave(&tape->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001906 tape->next_stage = NULL;
Borislav Petkovc4b22f82008-04-26 22:25:20 +02001907 if (test_bit(IDETAPE_FLAG_PIPELINE_ACTIVE, &tape->flags))
Borislav Petkov54bb2072008-02-06 02:57:52 +01001908 idetape_wait_for_request(drive, tape->active_data_rq);
1909 spin_unlock_irqrestore(&tape->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001910
1911 while (tape->first_stage != NULL) {
1912 struct request *rq_ptr = &tape->first_stage->rq;
1913
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001914 cnt += rq_ptr->nr_sectors - rq_ptr->current_nr_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001915 if (rq_ptr->errors == IDETAPE_ERROR_FILEMARK)
1916 ++cnt;
1917 idetape_remove_stage_head(drive);
1918 }
1919 tape->nr_pending_stages = 0;
1920 tape->max_stages = tape->min_pipeline;
1921 return cnt;
1922}
1923
1924/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001925 * Position the tape to the requested block using the LOCATE packet command.
1926 * A READ POSITION command is then issued to check where we are positioned. Like
1927 * all higher level operations, we queue the commands at the tail of the request
1928 * queue and wait for their completion.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001929 */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001930static int idetape_position_tape(ide_drive_t *drive, unsigned int block,
1931 u8 partition, int skip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001932{
1933 idetape_tape_t *tape = drive->driver_data;
1934 int retval;
Borislav Petkovd236d742008-04-18 00:46:27 +02001935 struct ide_atapi_pc pc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001936
Borislav Petkov54abf372008-02-06 02:57:52 +01001937 if (tape->chrdev_dir == IDETAPE_DIR_READ)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001938 __idetape_discard_read_pipeline(drive);
1939 idetape_wait_ready(drive, 60 * 5 * HZ);
1940 idetape_create_locate_cmd(drive, &pc, block, partition, skip);
1941 retval = idetape_queue_pc_tail(drive, &pc);
1942 if (retval)
1943 return (retval);
1944
1945 idetape_create_read_position_cmd(&pc);
1946 return (idetape_queue_pc_tail(drive, &pc));
1947}
1948
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001949static void idetape_discard_read_pipeline(ide_drive_t *drive,
1950 int restore_position)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001951{
1952 idetape_tape_t *tape = drive->driver_data;
1953 int cnt;
1954 int seek, position;
1955
1956 cnt = __idetape_discard_read_pipeline(drive);
1957 if (restore_position) {
1958 position = idetape_read_position(drive);
1959 seek = position > cnt ? position - cnt : 0;
1960 if (idetape_position_tape(drive, seek, 0, 0)) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001961 printk(KERN_INFO "ide-tape: %s: position_tape failed in"
1962 " discard_pipeline()\n", tape->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001963 return;
1964 }
1965 }
1966}
1967
1968/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001969 * Generate a read/write request for the block device interface and wait for it
1970 * to be serviced.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001971 */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001972static int idetape_queue_rw_tail(ide_drive_t *drive, int cmd, int blocks,
1973 struct idetape_bh *bh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001974{
1975 idetape_tape_t *tape = drive->driver_data;
1976 struct request rq;
1977
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001978 debug_log(DBG_SENSE, "%s: cmd=%d\n", __func__, cmd);
1979
Borislav Petkovc4b22f82008-04-26 22:25:20 +02001980 if (test_bit(IDETAPE_FLAG_PIPELINE_ACTIVE, &tape->flags)) {
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001981 printk(KERN_ERR "ide-tape: bug: the pipeline is active in %s\n",
1982 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001983 return (0);
1984 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001985
1986 idetape_init_rq(&rq, cmd);
1987 rq.rq_disk = tape->disk;
1988 rq.special = (void *)bh;
Borislav Petkov54bb2072008-02-06 02:57:52 +01001989 rq.sector = tape->first_frame;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001990 rq.nr_sectors = blocks;
1991 rq.current_nr_sectors = blocks;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001992 (void) ide_do_drive_cmd(drive, &rq, ide_wait);
1993
1994 if ((cmd & (REQ_IDETAPE_READ | REQ_IDETAPE_WRITE)) == 0)
1995 return 0;
1996
1997 if (tape->merge_stage)
1998 idetape_init_merge_stage(tape);
1999 if (rq.errors == IDETAPE_ERROR_GENERAL)
2000 return -EIO;
Borislav Petkov54bb2072008-02-06 02:57:52 +01002001 return (tape->blk_size * (blocks-rq.current_nr_sectors));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002002}
2003
Borislav Petkov8d06bfa2008-02-06 02:57:53 +01002004/* start servicing the pipeline stages, starting from tape->next_stage. */
2005static void idetape_plug_pipeline(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002006{
2007 idetape_tape_t *tape = drive->driver_data;
2008
2009 if (tape->next_stage == NULL)
2010 return;
Borislav Petkovc4b22f82008-04-26 22:25:20 +02002011 if (!test_and_set_bit(IDETAPE_FLAG_PIPELINE_ACTIVE, &tape->flags)) {
Borislav Petkov419d4742008-02-02 19:56:51 +01002012 idetape_activate_next_stage(drive);
Borislav Petkov54bb2072008-02-06 02:57:52 +01002013 (void) ide_do_drive_cmd(drive, tape->active_data_rq, ide_end);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002014 }
2015}
2016
Borislav Petkovd236d742008-04-18 00:46:27 +02002017static void idetape_create_inquiry_cmd(struct ide_atapi_pc *pc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002018{
2019 idetape_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +01002020 pc->c[0] = INQUIRY;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002021 pc->c[4] = 254;
Borislav Petkovd236d742008-04-18 00:46:27 +02002022 pc->req_xfer = 254;
2023 pc->idetape_callback = &idetape_pc_callback;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002024}
2025
Borislav Petkovd236d742008-04-18 00:46:27 +02002026static void idetape_create_rewind_cmd(ide_drive_t *drive,
2027 struct ide_atapi_pc *pc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002028{
2029 idetape_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +01002030 pc->c[0] = REZERO_UNIT;
Borislav Petkov346331f2008-04-18 00:46:26 +02002031 pc->flags |= PC_FLAG_WAIT_FOR_DSC;
Borislav Petkovd236d742008-04-18 00:46:27 +02002032 pc->idetape_callback = &idetape_pc_callback;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002033}
2034
Borislav Petkovd236d742008-04-18 00:46:27 +02002035static void idetape_create_erase_cmd(struct ide_atapi_pc *pc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002036{
2037 idetape_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +01002038 pc->c[0] = ERASE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002039 pc->c[1] = 1;
Borislav Petkov346331f2008-04-18 00:46:26 +02002040 pc->flags |= PC_FLAG_WAIT_FOR_DSC;
Borislav Petkovd236d742008-04-18 00:46:27 +02002041 pc->idetape_callback = &idetape_pc_callback;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002042}
2043
Borislav Petkovd236d742008-04-18 00:46:27 +02002044static void idetape_create_space_cmd(struct ide_atapi_pc *pc, int count, u8 cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002045{
2046 idetape_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +01002047 pc->c[0] = SPACE;
Borislav Petkov860ff5e2008-02-02 19:56:50 +01002048 put_unaligned(cpu_to_be32(count), (unsigned int *) &pc->c[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002049 pc->c[1] = cmd;
Borislav Petkov346331f2008-04-18 00:46:26 +02002050 pc->flags |= PC_FLAG_WAIT_FOR_DSC;
Borislav Petkovd236d742008-04-18 00:46:27 +02002051 pc->idetape_callback = &idetape_pc_callback;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002052}
2053
Borislav Petkov97c566c2008-04-27 15:38:25 +02002054/* Queue up a character device originated write request. */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002055static int idetape_add_chrdev_write_request(ide_drive_t *drive, int blocks)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002056{
2057 idetape_tape_t *tape = drive->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002058 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002059
Borislav Petkov8004a8c2008-02-06 02:57:51 +01002060 debug_log(DBG_CHRDEV, "Enter %s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002061
Borislav Petkov3c98bf32008-02-06 02:57:53 +01002062 /* Attempt to allocate a new stage. Beware possible race conditions. */
Borislav Petkov97c566c2008-04-27 15:38:25 +02002063 while (1) {
Borislav Petkov54bb2072008-02-06 02:57:52 +01002064 spin_lock_irqsave(&tape->lock, flags);
Borislav Petkovc4b22f82008-04-26 22:25:20 +02002065 if (test_bit(IDETAPE_FLAG_PIPELINE_ACTIVE, &tape->flags)) {
Borislav Petkov54bb2072008-02-06 02:57:52 +01002066 idetape_wait_for_request(drive, tape->active_data_rq);
2067 spin_unlock_irqrestore(&tape->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002068 } else {
Borislav Petkov54bb2072008-02-06 02:57:52 +01002069 spin_unlock_irqrestore(&tape->lock, flags);
Borislav Petkov8d06bfa2008-02-06 02:57:53 +01002070 idetape_plug_pipeline(drive);
Borislav Petkovc4b22f82008-04-26 22:25:20 +02002071 if (test_bit(IDETAPE_FLAG_PIPELINE_ACTIVE,
2072 &tape->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002073 continue;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002074 return idetape_queue_rw_tail(drive, REQ_IDETAPE_WRITE,
2075 blocks, tape->merge_stage->bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002076 }
2077 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002078}
2079
2080/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +01002081 * Wait until all pending pipeline requests are serviced. Typically called on
2082 * device close.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002083 */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002084static void idetape_wait_for_pipeline(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002085{
2086 idetape_tape_t *tape = drive->driver_data;
2087 unsigned long flags;
2088
Borislav Petkovc4b22f82008-04-26 22:25:20 +02002089 while (tape->next_stage || test_bit(IDETAPE_FLAG_PIPELINE_ACTIVE,
2090 &tape->flags)) {
Borislav Petkov8d06bfa2008-02-06 02:57:53 +01002091 idetape_plug_pipeline(drive);
Borislav Petkov54bb2072008-02-06 02:57:52 +01002092 spin_lock_irqsave(&tape->lock, flags);
Borislav Petkovc4b22f82008-04-26 22:25:20 +02002093 if (test_bit(IDETAPE_FLAG_PIPELINE_ACTIVE, &tape->flags))
Borislav Petkov54bb2072008-02-06 02:57:52 +01002094 idetape_wait_for_request(drive, tape->active_data_rq);
2095 spin_unlock_irqrestore(&tape->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002096 }
2097}
2098
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002099static void idetape_empty_write_pipeline(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002100{
2101 idetape_tape_t *tape = drive->driver_data;
2102 int blocks, min;
2103 struct idetape_bh *bh;
Borislav Petkov55a5d292008-02-02 19:56:49 +01002104
Borislav Petkov54abf372008-02-06 02:57:52 +01002105 if (tape->chrdev_dir != IDETAPE_DIR_WRITE) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002106 printk(KERN_ERR "ide-tape: bug: Trying to empty write pipeline,"
2107 " but we are not writing.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002108 return;
2109 }
2110 if (tape->merge_stage_size > tape->stage_size) {
2111 printk(KERN_ERR "ide-tape: bug: merge_buffer too big\n");
2112 tape->merge_stage_size = tape->stage_size;
2113 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002114 if (tape->merge_stage_size) {
Borislav Petkov54bb2072008-02-06 02:57:52 +01002115 blocks = tape->merge_stage_size / tape->blk_size;
2116 if (tape->merge_stage_size % tape->blk_size) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002117 unsigned int i;
2118
2119 blocks++;
Borislav Petkov54bb2072008-02-06 02:57:52 +01002120 i = tape->blk_size - tape->merge_stage_size %
2121 tape->blk_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002122 bh = tape->bh->b_reqnext;
2123 while (bh) {
2124 atomic_set(&bh->b_count, 0);
2125 bh = bh->b_reqnext;
2126 }
2127 bh = tape->bh;
2128 while (i) {
2129 if (bh == NULL) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002130 printk(KERN_INFO "ide-tape: bug,"
2131 " bh NULL\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002132 break;
2133 }
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002134 min = min(i, (unsigned int)(bh->b_size -
2135 atomic_read(&bh->b_count)));
2136 memset(bh->b_data + atomic_read(&bh->b_count),
2137 0, min);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002138 atomic_add(min, &bh->b_count);
2139 i -= min;
2140 bh = bh->b_reqnext;
2141 }
2142 }
2143 (void) idetape_add_chrdev_write_request(drive, blocks);
2144 tape->merge_stage_size = 0;
2145 }
2146 idetape_wait_for_pipeline(drive);
2147 if (tape->merge_stage != NULL) {
2148 __idetape_kfree_stage(tape->merge_stage);
2149 tape->merge_stage = NULL;
2150 }
Borislav Petkov346331f2008-04-18 00:46:26 +02002151 clear_bit(IDETAPE_FLAG_PIPELINE_ERR, &tape->flags);
Borislav Petkov54abf372008-02-06 02:57:52 +01002152 tape->chrdev_dir = IDETAPE_DIR_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002153
2154 /*
Borislav Petkov3c98bf32008-02-06 02:57:53 +01002155 * On the next backup, perform the feedback loop again. (I don't want to
2156 * keep sense information between backups, as some systems are
2157 * constantly on, and the system load can be totally different on the
2158 * next backup).
Linus Torvalds1da177e2005-04-16 15:20:36 -07002159 */
2160 tape->max_stages = tape->min_pipeline;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002161 if (tape->first_stage != NULL ||
2162 tape->next_stage != NULL ||
2163 tape->last_stage != NULL ||
2164 tape->nr_stages != 0) {
2165 printk(KERN_ERR "ide-tape: ide-tape pipeline bug, "
2166 "first_stage %p, next_stage %p, "
2167 "last_stage %p, nr_stages %d\n",
2168 tape->first_stage, tape->next_stage,
2169 tape->last_stage, tape->nr_stages);
2170 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002171}
2172
Borislav Petkov8d06bfa2008-02-06 02:57:53 +01002173static int idetape_init_read(ide_drive_t *drive, int max_stages)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002174{
2175 idetape_tape_t *tape = drive->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002176 int bytes_read;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002177
2178 /* Initialize read operation */
Borislav Petkov54abf372008-02-06 02:57:52 +01002179 if (tape->chrdev_dir != IDETAPE_DIR_READ) {
2180 if (tape->chrdev_dir == IDETAPE_DIR_WRITE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002181 idetape_empty_write_pipeline(drive);
2182 idetape_flush_tape_buffers(drive);
2183 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002184 if (tape->merge_stage || tape->merge_stage_size) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002185 printk(KERN_ERR "ide-tape: merge_stage_size should be"
2186 " 0 now\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002187 tape->merge_stage_size = 0;
2188 }
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002189 tape->merge_stage = __idetape_kmalloc_stage(tape, 0, 0);
2190 if (!tape->merge_stage)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002191 return -ENOMEM;
Borislav Petkov54abf372008-02-06 02:57:52 +01002192 tape->chrdev_dir = IDETAPE_DIR_READ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002193
2194 /*
Borislav Petkov3c98bf32008-02-06 02:57:53 +01002195 * Issue a read 0 command to ensure that DSC handshake is
2196 * switched from completion mode to buffer available mode.
2197 * No point in issuing this if DSC overlap isn't supported, some
2198 * drives (Seagate STT3401A) will return an error.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002199 */
2200 if (drive->dsc_overlap) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002201 bytes_read = idetape_queue_rw_tail(drive,
2202 REQ_IDETAPE_READ, 0,
2203 tape->merge_stage->bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002204 if (bytes_read < 0) {
2205 __idetape_kfree_stage(tape->merge_stage);
2206 tape->merge_stage = NULL;
Borislav Petkov54abf372008-02-06 02:57:52 +01002207 tape->chrdev_dir = IDETAPE_DIR_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002208 return bytes_read;
2209 }
2210 }
2211 }
Borislav Petkov5e69bd92008-04-27 15:38:25 +02002212
Borislav Petkovc4b22f82008-04-26 22:25:20 +02002213 if (!test_bit(IDETAPE_FLAG_PIPELINE_ACTIVE, &tape->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002214 if (tape->nr_pending_stages >= 3 * max_stages / 4) {
2215 tape->measure_insert_time = 1;
2216 tape->insert_time = jiffies;
2217 tape->insert_size = 0;
2218 tape->insert_speed = 0;
Borislav Petkov8d06bfa2008-02-06 02:57:53 +01002219 idetape_plug_pipeline(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002220 }
2221 }
2222 return 0;
2223}
2224
2225/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +01002226 * Called from idetape_chrdev_read() to service a character device read request
2227 * and add read-ahead requests to our pipeline.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002228 */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002229static int idetape_add_chrdev_read_request(ide_drive_t *drive, int blocks)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002230{
2231 idetape_tape_t *tape = drive->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002232
Borislav Petkov8004a8c2008-02-06 02:57:51 +01002233 debug_log(DBG_PROCS, "Enter %s, %d blocks\n", __func__, blocks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002234
Borislav Petkov3c98bf32008-02-06 02:57:53 +01002235 /* If we are at a filemark, return a read length of 0 */
Borislav Petkov346331f2008-04-18 00:46:26 +02002236 if (test_bit(IDETAPE_FLAG_FILEMARK, &tape->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002237 return 0;
2238
Borislav Petkov8d06bfa2008-02-06 02:57:53 +01002239 idetape_init_read(drive, tape->max_stages);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002240
Borislav Petkov5e69bd92008-04-27 15:38:25 +02002241 if (test_bit(IDETAPE_FLAG_PIPELINE_ERR, &tape->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002242 return 0;
Borislav Petkov5e69bd92008-04-27 15:38:25 +02002243
2244 return idetape_queue_rw_tail(drive, REQ_IDETAPE_READ, blocks,
2245 tape->merge_stage->bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002246}
2247
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002248static void idetape_pad_zeros(ide_drive_t *drive, int bcount)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002249{
2250 idetape_tape_t *tape = drive->driver_data;
2251 struct idetape_bh *bh;
2252 int blocks;
Borislav Petkov3c98bf32008-02-06 02:57:53 +01002253
Linus Torvalds1da177e2005-04-16 15:20:36 -07002254 while (bcount) {
2255 unsigned int count;
2256
2257 bh = tape->merge_stage->bh;
2258 count = min(tape->stage_size, bcount);
2259 bcount -= count;
Borislav Petkov54bb2072008-02-06 02:57:52 +01002260 blocks = count / tape->blk_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002261 while (count) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002262 atomic_set(&bh->b_count,
2263 min(count, (unsigned int)bh->b_size));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002264 memset(bh->b_data, 0, atomic_read(&bh->b_count));
2265 count -= atomic_read(&bh->b_count);
2266 bh = bh->b_reqnext;
2267 }
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002268 idetape_queue_rw_tail(drive, REQ_IDETAPE_WRITE, blocks,
2269 tape->merge_stage->bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002270 }
2271}
2272
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002273static int idetape_pipeline_size(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002274{
2275 idetape_tape_t *tape = drive->driver_data;
2276 idetape_stage_t *stage;
2277 struct request *rq;
2278 int size = 0;
2279
2280 idetape_wait_for_pipeline(drive);
2281 stage = tape->first_stage;
2282 while (stage != NULL) {
2283 rq = &stage->rq;
Borislav Petkov54bb2072008-02-06 02:57:52 +01002284 size += tape->blk_size * (rq->nr_sectors -
2285 rq->current_nr_sectors);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002286 if (rq->errors == IDETAPE_ERROR_FILEMARK)
Borislav Petkov54bb2072008-02-06 02:57:52 +01002287 size += tape->blk_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002288 stage = stage->next;
2289 }
2290 size += tape->merge_stage_size;
2291 return size;
2292}
2293
2294/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +01002295 * Rewinds the tape to the Beginning Of the current Partition (BOP). We
2296 * currently support only one partition.
2297 */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002298static int idetape_rewind_tape(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002299{
2300 int retval;
Borislav Petkovd236d742008-04-18 00:46:27 +02002301 struct ide_atapi_pc pc;
Borislav Petkov8004a8c2008-02-06 02:57:51 +01002302 idetape_tape_t *tape;
2303 tape = drive->driver_data;
2304
2305 debug_log(DBG_SENSE, "Enter %s\n", __func__);
2306
Linus Torvalds1da177e2005-04-16 15:20:36 -07002307 idetape_create_rewind_cmd(drive, &pc);
2308 retval = idetape_queue_pc_tail(drive, &pc);
2309 if (retval)
2310 return retval;
2311
2312 idetape_create_read_position_cmd(&pc);
2313 retval = idetape_queue_pc_tail(drive, &pc);
2314 if (retval)
2315 return retval;
2316 return 0;
2317}
2318
Borislav Petkov3c98bf32008-02-06 02:57:53 +01002319/* mtio.h compatible commands should be issued to the chrdev interface. */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002320static int idetape_blkdev_ioctl(ide_drive_t *drive, unsigned int cmd,
2321 unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002322{
2323 idetape_tape_t *tape = drive->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002324 void __user *argp = (void __user *)arg;
2325
Borislav Petkovd59823f2008-02-02 19:56:51 +01002326 struct idetape_config {
2327 int dsc_rw_frequency;
2328 int dsc_media_access_frequency;
2329 int nr_stages;
2330 } config;
2331
Borislav Petkov8004a8c2008-02-06 02:57:51 +01002332 debug_log(DBG_PROCS, "Enter %s\n", __func__);
2333
Linus Torvalds1da177e2005-04-16 15:20:36 -07002334 switch (cmd) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002335 case 0x0340:
2336 if (copy_from_user(&config, argp, sizeof(config)))
2337 return -EFAULT;
2338 tape->best_dsc_rw_freq = config.dsc_rw_frequency;
2339 tape->max_stages = config.nr_stages;
2340 break;
2341 case 0x0350:
2342 config.dsc_rw_frequency = (int) tape->best_dsc_rw_freq;
2343 config.nr_stages = tape->max_stages;
2344 if (copy_to_user(argp, &config, sizeof(config)))
2345 return -EFAULT;
2346 break;
2347 default:
2348 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002349 }
2350 return 0;
2351}
2352
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002353static int idetape_space_over_filemarks(ide_drive_t *drive, short mt_op,
2354 int mt_count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002355{
2356 idetape_tape_t *tape = drive->driver_data;
Borislav Petkovd236d742008-04-18 00:46:27 +02002357 struct ide_atapi_pc pc;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002358 int retval, count = 0;
Borislav Petkovb6422012008-02-02 19:56:49 +01002359 int sprev = !!(tape->caps[4] & 0x20);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002360
2361 if (mt_count == 0)
2362 return 0;
2363 if (MTBSF == mt_op || MTBSFM == mt_op) {
Borislav Petkovb6422012008-02-02 19:56:49 +01002364 if (!sprev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002365 return -EIO;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002366 mt_count = -mt_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002367 }
2368
Borislav Petkov54abf372008-02-06 02:57:52 +01002369 if (tape->chrdev_dir == IDETAPE_DIR_READ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002370 tape->merge_stage_size = 0;
Borislav Petkov346331f2008-04-18 00:46:26 +02002371 if (test_and_clear_bit(IDETAPE_FLAG_FILEMARK, &tape->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002372 ++count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002373 idetape_discard_read_pipeline(drive, 0);
2374 }
2375
2376 /*
Borislav Petkov3c98bf32008-02-06 02:57:53 +01002377 * The filemark was not found in our internal pipeline; now we can issue
2378 * the space command.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002379 */
2380 switch (mt_op) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002381 case MTFSF:
2382 case MTBSF:
2383 idetape_create_space_cmd(&pc, mt_count - count,
2384 IDETAPE_SPACE_OVER_FILEMARK);
2385 return idetape_queue_pc_tail(drive, &pc);
2386 case MTFSFM:
2387 case MTBSFM:
2388 if (!sprev)
2389 return -EIO;
2390 retval = idetape_space_over_filemarks(drive, MTFSF,
2391 mt_count - count);
2392 if (retval)
2393 return retval;
2394 count = (MTBSFM == mt_op ? 1 : -1);
2395 return idetape_space_over_filemarks(drive, MTFSF, count);
2396 default:
2397 printk(KERN_ERR "ide-tape: MTIO operation %d not supported\n",
2398 mt_op);
2399 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002400 }
2401}
2402
Linus Torvalds1da177e2005-04-16 15:20:36 -07002403/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +01002404 * Our character device read / write functions.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002405 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +01002406 * The tape is optimized to maximize throughput when it is transferring an
2407 * integral number of the "continuous transfer limit", which is a parameter of
2408 * the specific tape (26kB on my particular tape, 32kB for Onstream).
Linus Torvalds1da177e2005-04-16 15:20:36 -07002409 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +01002410 * As of version 1.3 of the driver, the character device provides an abstract
2411 * continuous view of the media - any mix of block sizes (even 1 byte) on the
2412 * same backup/restore procedure is supported. The driver will internally
2413 * convert the requests to the recommended transfer unit, so that an unmatch
2414 * between the user's block size to the recommended size will only result in a
2415 * (slightly) increased driver overhead, but will no longer hit performance.
2416 * This is not applicable to Onstream.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002417 */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002418static ssize_t idetape_chrdev_read(struct file *file, char __user *buf,
2419 size_t count, loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002420{
2421 struct ide_tape_obj *tape = ide_tape_f(file);
2422 ide_drive_t *drive = tape->drive;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002423 ssize_t bytes_read, temp, actually_read = 0, rc;
Daniel Walkerdcd96372006-06-25 05:47:37 -07002424 ssize_t ret = 0;
Borislav Petkovb6422012008-02-02 19:56:49 +01002425 u16 ctl = *(u16 *)&tape->caps[12];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002426
Borislav Petkov8004a8c2008-02-06 02:57:51 +01002427 debug_log(DBG_CHRDEV, "Enter %s, count %Zd\n", __func__, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002428
Borislav Petkov54abf372008-02-06 02:57:52 +01002429 if (tape->chrdev_dir != IDETAPE_DIR_READ) {
Borislav Petkov346331f2008-04-18 00:46:26 +02002430 if (test_bit(IDETAPE_FLAG_DETECT_BS, &tape->flags))
Borislav Petkov54bb2072008-02-06 02:57:52 +01002431 if (count > tape->blk_size &&
2432 (count % tape->blk_size) == 0)
2433 tape->user_bs_factor = count / tape->blk_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002434 }
Borislav Petkov8d06bfa2008-02-06 02:57:53 +01002435 rc = idetape_init_read(drive, tape->max_stages);
2436 if (rc < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002437 return rc;
2438 if (count == 0)
2439 return (0);
2440 if (tape->merge_stage_size) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002441 actually_read = min((unsigned int)(tape->merge_stage_size),
2442 (unsigned int)count);
Borislav Petkov99d74e62008-04-27 15:38:25 +02002443 if (idetape_copy_stage_to_user(tape, buf, actually_read))
Daniel Walkerdcd96372006-06-25 05:47:37 -07002444 ret = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002445 buf += actually_read;
2446 tape->merge_stage_size -= actually_read;
2447 count -= actually_read;
2448 }
2449 while (count >= tape->stage_size) {
Borislav Petkovb6422012008-02-02 19:56:49 +01002450 bytes_read = idetape_add_chrdev_read_request(drive, ctl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002451 if (bytes_read <= 0)
2452 goto finish;
Borislav Petkov99d74e62008-04-27 15:38:25 +02002453 if (idetape_copy_stage_to_user(tape, buf, bytes_read))
Daniel Walkerdcd96372006-06-25 05:47:37 -07002454 ret = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002455 buf += bytes_read;
2456 count -= bytes_read;
2457 actually_read += bytes_read;
2458 }
2459 if (count) {
Borislav Petkovb6422012008-02-02 19:56:49 +01002460 bytes_read = idetape_add_chrdev_read_request(drive, ctl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002461 if (bytes_read <= 0)
2462 goto finish;
2463 temp = min((unsigned long)count, (unsigned long)bytes_read);
Borislav Petkov99d74e62008-04-27 15:38:25 +02002464 if (idetape_copy_stage_to_user(tape, buf, temp))
Daniel Walkerdcd96372006-06-25 05:47:37 -07002465 ret = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002466 actually_read += temp;
2467 tape->merge_stage_size = bytes_read-temp;
2468 }
2469finish:
Borislav Petkov346331f2008-04-18 00:46:26 +02002470 if (!actually_read && test_bit(IDETAPE_FLAG_FILEMARK, &tape->flags)) {
Borislav Petkov8004a8c2008-02-06 02:57:51 +01002471 debug_log(DBG_SENSE, "%s: spacing over filemark\n", tape->name);
2472
Linus Torvalds1da177e2005-04-16 15:20:36 -07002473 idetape_space_over_filemarks(drive, MTFSF, 1);
2474 return 0;
2475 }
Daniel Walkerdcd96372006-06-25 05:47:37 -07002476
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002477 return ret ? ret : actually_read;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002478}
2479
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002480static ssize_t idetape_chrdev_write(struct file *file, const char __user *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002481 size_t count, loff_t *ppos)
2482{
2483 struct ide_tape_obj *tape = ide_tape_f(file);
2484 ide_drive_t *drive = tape->drive;
Daniel Walkerdcd96372006-06-25 05:47:37 -07002485 ssize_t actually_written = 0;
2486 ssize_t ret = 0;
Borislav Petkovb6422012008-02-02 19:56:49 +01002487 u16 ctl = *(u16 *)&tape->caps[12];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002488
2489 /* The drive is write protected. */
2490 if (tape->write_prot)
2491 return -EACCES;
2492
Borislav Petkov8004a8c2008-02-06 02:57:51 +01002493 debug_log(DBG_CHRDEV, "Enter %s, count %Zd\n", __func__, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002494
2495 /* Initialize write operation */
Borislav Petkov54abf372008-02-06 02:57:52 +01002496 if (tape->chrdev_dir != IDETAPE_DIR_WRITE) {
2497 if (tape->chrdev_dir == IDETAPE_DIR_READ)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002498 idetape_discard_read_pipeline(drive, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002499 if (tape->merge_stage || tape->merge_stage_size) {
2500 printk(KERN_ERR "ide-tape: merge_stage_size "
2501 "should be 0 now\n");
2502 tape->merge_stage_size = 0;
2503 }
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002504 tape->merge_stage = __idetape_kmalloc_stage(tape, 0, 0);
2505 if (!tape->merge_stage)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002506 return -ENOMEM;
Borislav Petkov54abf372008-02-06 02:57:52 +01002507 tape->chrdev_dir = IDETAPE_DIR_WRITE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002508 idetape_init_merge_stage(tape);
2509
2510 /*
Borislav Petkov3c98bf32008-02-06 02:57:53 +01002511 * Issue a write 0 command to ensure that DSC handshake is
2512 * switched from completion mode to buffer available mode. No
2513 * point in issuing this if DSC overlap isn't supported, some
2514 * drives (Seagate STT3401A) will return an error.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002515 */
2516 if (drive->dsc_overlap) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002517 ssize_t retval = idetape_queue_rw_tail(drive,
2518 REQ_IDETAPE_WRITE, 0,
2519 tape->merge_stage->bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002520 if (retval < 0) {
2521 __idetape_kfree_stage(tape->merge_stage);
2522 tape->merge_stage = NULL;
Borislav Petkov54abf372008-02-06 02:57:52 +01002523 tape->chrdev_dir = IDETAPE_DIR_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002524 return retval;
2525 }
2526 }
2527 }
2528 if (count == 0)
2529 return (0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002530 if (tape->merge_stage_size) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002531 if (tape->merge_stage_size >= tape->stage_size) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002532 printk(KERN_ERR "ide-tape: bug: merge buf too big\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002533 tape->merge_stage_size = 0;
2534 }
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002535 actually_written = min((unsigned int)
2536 (tape->stage_size - tape->merge_stage_size),
2537 (unsigned int)count);
Borislav Petkov8646c882008-04-27 15:38:26 +02002538 if (idetape_copy_stage_from_user(tape, buf, actually_written))
Daniel Walkerdcd96372006-06-25 05:47:37 -07002539 ret = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002540 buf += actually_written;
2541 tape->merge_stage_size += actually_written;
2542 count -= actually_written;
2543
2544 if (tape->merge_stage_size == tape->stage_size) {
Daniel Walkerdcd96372006-06-25 05:47:37 -07002545 ssize_t retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002546 tape->merge_stage_size = 0;
Borislav Petkovb6422012008-02-02 19:56:49 +01002547 retval = idetape_add_chrdev_write_request(drive, ctl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002548 if (retval <= 0)
2549 return (retval);
2550 }
2551 }
2552 while (count >= tape->stage_size) {
Daniel Walkerdcd96372006-06-25 05:47:37 -07002553 ssize_t retval;
Borislav Petkov8646c882008-04-27 15:38:26 +02002554 if (idetape_copy_stage_from_user(tape, buf, tape->stage_size))
Daniel Walkerdcd96372006-06-25 05:47:37 -07002555 ret = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002556 buf += tape->stage_size;
2557 count -= tape->stage_size;
Borislav Petkovb6422012008-02-02 19:56:49 +01002558 retval = idetape_add_chrdev_write_request(drive, ctl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002559 actually_written += tape->stage_size;
2560 if (retval <= 0)
2561 return (retval);
2562 }
2563 if (count) {
2564 actually_written += count;
Borislav Petkov8646c882008-04-27 15:38:26 +02002565 if (idetape_copy_stage_from_user(tape, buf, count))
Daniel Walkerdcd96372006-06-25 05:47:37 -07002566 ret = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002567 tape->merge_stage_size += count;
2568 }
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002569 return ret ? ret : actually_written;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002570}
2571
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002572static int idetape_write_filemark(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002573{
Borislav Petkovd236d742008-04-18 00:46:27 +02002574 struct ide_atapi_pc pc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002575
2576 /* Write a filemark */
2577 idetape_create_write_filemark_cmd(drive, &pc, 1);
2578 if (idetape_queue_pc_tail(drive, &pc)) {
2579 printk(KERN_ERR "ide-tape: Couldn't write a filemark\n");
2580 return -EIO;
2581 }
2582 return 0;
2583}
2584
2585/*
Borislav Petkovd99c9da2008-02-02 19:56:51 +01002586 * Called from idetape_chrdev_ioctl when the general mtio MTIOCTOP ioctl is
2587 * requested.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002588 *
Borislav Petkovd99c9da2008-02-02 19:56:51 +01002589 * Note: MTBSF and MTBSFM are not supported when the tape doesn't support
2590 * spacing over filemarks in the reverse direction. In this case, MTFSFM is also
2591 * usually not supported (it is supported in the rare case in which we crossed
2592 * the filemark during our read-ahead pipelined operation mode).
Linus Torvalds1da177e2005-04-16 15:20:36 -07002593 *
Borislav Petkovd99c9da2008-02-02 19:56:51 +01002594 * The following commands are currently not supported:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002595 *
Borislav Petkovd99c9da2008-02-02 19:56:51 +01002596 * MTFSS, MTBSS, MTWSM, MTSETDENSITY, MTSETDRVBUFFER, MT_ST_BOOLEANS,
2597 * MT_ST_WRITE_THRESHOLD.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002598 */
Borislav Petkovd99c9da2008-02-02 19:56:51 +01002599static int idetape_mtioctop(ide_drive_t *drive, short mt_op, int mt_count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002600{
2601 idetape_tape_t *tape = drive->driver_data;
Borislav Petkovd236d742008-04-18 00:46:27 +02002602 struct ide_atapi_pc pc;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002603 int i, retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002604
Borislav Petkov8004a8c2008-02-06 02:57:51 +01002605 debug_log(DBG_ERR, "Handling MTIOCTOP ioctl: mt_op=%d, mt_count=%d\n",
2606 mt_op, mt_count);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002607
Borislav Petkov3c98bf32008-02-06 02:57:53 +01002608 /* Commands which need our pipelined read-ahead stages. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002609 switch (mt_op) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002610 case MTFSF:
2611 case MTFSFM:
2612 case MTBSF:
2613 case MTBSFM:
2614 if (!mt_count)
2615 return 0;
2616 return idetape_space_over_filemarks(drive, mt_op, mt_count);
2617 default:
2618 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002619 }
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002620
Linus Torvalds1da177e2005-04-16 15:20:36 -07002621 switch (mt_op) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002622 case MTWEOF:
2623 if (tape->write_prot)
2624 return -EACCES;
2625 idetape_discard_read_pipeline(drive, 1);
2626 for (i = 0; i < mt_count; i++) {
2627 retval = idetape_write_filemark(drive);
2628 if (retval)
2629 return retval;
2630 }
2631 return 0;
2632 case MTREW:
2633 idetape_discard_read_pipeline(drive, 0);
2634 if (idetape_rewind_tape(drive))
2635 return -EIO;
2636 return 0;
2637 case MTLOAD:
2638 idetape_discard_read_pipeline(drive, 0);
2639 idetape_create_load_unload_cmd(drive, &pc,
2640 IDETAPE_LU_LOAD_MASK);
2641 return idetape_queue_pc_tail(drive, &pc);
2642 case MTUNLOAD:
2643 case MTOFFL:
2644 /*
2645 * If door is locked, attempt to unlock before
2646 * attempting to eject.
2647 */
2648 if (tape->door_locked) {
2649 if (idetape_create_prevent_cmd(drive, &pc, 0))
2650 if (!idetape_queue_pc_tail(drive, &pc))
2651 tape->door_locked = DOOR_UNLOCKED;
2652 }
2653 idetape_discard_read_pipeline(drive, 0);
2654 idetape_create_load_unload_cmd(drive, &pc,
2655 !IDETAPE_LU_LOAD_MASK);
2656 retval = idetape_queue_pc_tail(drive, &pc);
2657 if (!retval)
Borislav Petkov346331f2008-04-18 00:46:26 +02002658 clear_bit(IDETAPE_FLAG_MEDIUM_PRESENT, &tape->flags);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002659 return retval;
2660 case MTNOP:
2661 idetape_discard_read_pipeline(drive, 0);
2662 return idetape_flush_tape_buffers(drive);
2663 case MTRETEN:
2664 idetape_discard_read_pipeline(drive, 0);
2665 idetape_create_load_unload_cmd(drive, &pc,
2666 IDETAPE_LU_RETENSION_MASK | IDETAPE_LU_LOAD_MASK);
2667 return idetape_queue_pc_tail(drive, &pc);
2668 case MTEOM:
2669 idetape_create_space_cmd(&pc, 0, IDETAPE_SPACE_TO_EOD);
2670 return idetape_queue_pc_tail(drive, &pc);
2671 case MTERASE:
2672 (void)idetape_rewind_tape(drive);
2673 idetape_create_erase_cmd(&pc);
2674 return idetape_queue_pc_tail(drive, &pc);
2675 case MTSETBLK:
2676 if (mt_count) {
2677 if (mt_count < tape->blk_size ||
2678 mt_count % tape->blk_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002679 return -EIO;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002680 tape->user_bs_factor = mt_count / tape->blk_size;
Borislav Petkov346331f2008-04-18 00:46:26 +02002681 clear_bit(IDETAPE_FLAG_DETECT_BS, &tape->flags);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002682 } else
Borislav Petkov346331f2008-04-18 00:46:26 +02002683 set_bit(IDETAPE_FLAG_DETECT_BS, &tape->flags);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002684 return 0;
2685 case MTSEEK:
2686 idetape_discard_read_pipeline(drive, 0);
2687 return idetape_position_tape(drive,
2688 mt_count * tape->user_bs_factor, tape->partition, 0);
2689 case MTSETPART:
2690 idetape_discard_read_pipeline(drive, 0);
2691 return idetape_position_tape(drive, 0, mt_count, 0);
2692 case MTFSR:
2693 case MTBSR:
2694 case MTLOCK:
2695 if (!idetape_create_prevent_cmd(drive, &pc, 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002696 return 0;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002697 retval = idetape_queue_pc_tail(drive, &pc);
2698 if (retval)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002699 return retval;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002700 tape->door_locked = DOOR_EXPLICITLY_LOCKED;
2701 return 0;
2702 case MTUNLOCK:
2703 if (!idetape_create_prevent_cmd(drive, &pc, 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002704 return 0;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002705 retval = idetape_queue_pc_tail(drive, &pc);
2706 if (retval)
2707 return retval;
2708 tape->door_locked = DOOR_UNLOCKED;
2709 return 0;
2710 default:
2711 printk(KERN_ERR "ide-tape: MTIO operation %d not supported\n",
2712 mt_op);
2713 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002714 }
2715}
2716
2717/*
Borislav Petkovd99c9da2008-02-02 19:56:51 +01002718 * Our character device ioctls. General mtio.h magnetic io commands are
2719 * supported here, and not in the corresponding block interface. Our own
2720 * ide-tape ioctls are supported on both interfaces.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002721 */
Borislav Petkovd99c9da2008-02-02 19:56:51 +01002722static int idetape_chrdev_ioctl(struct inode *inode, struct file *file,
2723 unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002724{
2725 struct ide_tape_obj *tape = ide_tape_f(file);
2726 ide_drive_t *drive = tape->drive;
2727 struct mtop mtop;
2728 struct mtget mtget;
2729 struct mtpos mtpos;
Borislav Petkov54bb2072008-02-06 02:57:52 +01002730 int block_offset = 0, position = tape->first_frame;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002731 void __user *argp = (void __user *)arg;
2732
Borislav Petkov8004a8c2008-02-06 02:57:51 +01002733 debug_log(DBG_CHRDEV, "Enter %s, cmd=%u\n", __func__, cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002734
Borislav Petkov54abf372008-02-06 02:57:52 +01002735 if (tape->chrdev_dir == IDETAPE_DIR_WRITE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002736 idetape_empty_write_pipeline(drive);
2737 idetape_flush_tape_buffers(drive);
2738 }
2739 if (cmd == MTIOCGET || cmd == MTIOCPOS) {
Borislav Petkov54bb2072008-02-06 02:57:52 +01002740 block_offset = idetape_pipeline_size(drive) /
2741 (tape->blk_size * tape->user_bs_factor);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002742 position = idetape_read_position(drive);
2743 if (position < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002744 return -EIO;
2745 }
2746 switch (cmd) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002747 case MTIOCTOP:
2748 if (copy_from_user(&mtop, argp, sizeof(struct mtop)))
2749 return -EFAULT;
2750 return idetape_mtioctop(drive, mtop.mt_op, mtop.mt_count);
2751 case MTIOCGET:
2752 memset(&mtget, 0, sizeof(struct mtget));
2753 mtget.mt_type = MT_ISSCSI2;
2754 mtget.mt_blkno = position / tape->user_bs_factor - block_offset;
2755 mtget.mt_dsreg =
2756 ((tape->blk_size * tape->user_bs_factor)
2757 << MT_ST_BLKSIZE_SHIFT) & MT_ST_BLKSIZE_MASK;
Borislav Petkov54bb2072008-02-06 02:57:52 +01002758
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002759 if (tape->drv_write_prot)
2760 mtget.mt_gstat |= GMT_WR_PROT(0xffffffff);
2761
2762 if (copy_to_user(argp, &mtget, sizeof(struct mtget)))
2763 return -EFAULT;
2764 return 0;
2765 case MTIOCPOS:
2766 mtpos.mt_blkno = position / tape->user_bs_factor - block_offset;
2767 if (copy_to_user(argp, &mtpos, sizeof(struct mtpos)))
2768 return -EFAULT;
2769 return 0;
2770 default:
2771 if (tape->chrdev_dir == IDETAPE_DIR_READ)
2772 idetape_discard_read_pipeline(drive, 1);
2773 return idetape_blkdev_ioctl(drive, cmd, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002774 }
2775}
2776
Borislav Petkov3cffb9c2008-02-02 19:56:50 +01002777/*
2778 * Do a mode sense page 0 with block descriptor and if it succeeds set the tape
2779 * block size with the reported value.
2780 */
2781static void ide_tape_get_bsize_from_bdesc(ide_drive_t *drive)
2782{
2783 idetape_tape_t *tape = drive->driver_data;
Borislav Petkovd236d742008-04-18 00:46:27 +02002784 struct ide_atapi_pc pc;
Borislav Petkov3cffb9c2008-02-02 19:56:50 +01002785
2786 idetape_create_mode_sense_cmd(&pc, IDETAPE_BLOCK_DESCRIPTOR);
2787 if (idetape_queue_pc_tail(drive, &pc)) {
2788 printk(KERN_ERR "ide-tape: Can't get block descriptor\n");
Borislav Petkov54bb2072008-02-06 02:57:52 +01002789 if (tape->blk_size == 0) {
Borislav Petkov3cffb9c2008-02-02 19:56:50 +01002790 printk(KERN_WARNING "ide-tape: Cannot deal with zero "
2791 "block size, assuming 32k\n");
Borislav Petkov54bb2072008-02-06 02:57:52 +01002792 tape->blk_size = 32768;
Borislav Petkov3cffb9c2008-02-02 19:56:50 +01002793 }
2794 return;
2795 }
Borislav Petkovd236d742008-04-18 00:46:27 +02002796 tape->blk_size = (pc.buf[4 + 5] << 16) +
2797 (pc.buf[4 + 6] << 8) +
2798 pc.buf[4 + 7];
2799 tape->drv_write_prot = (pc.buf[2] & 0x80) >> 7;
Borislav Petkov3cffb9c2008-02-02 19:56:50 +01002800}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002801
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002802static int idetape_chrdev_open(struct inode *inode, struct file *filp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002803{
2804 unsigned int minor = iminor(inode), i = minor & ~0xc0;
2805 ide_drive_t *drive;
2806 idetape_tape_t *tape;
Borislav Petkovd236d742008-04-18 00:46:27 +02002807 struct ide_atapi_pc pc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002808 int retval;
2809
Borislav Petkov8004a8c2008-02-06 02:57:51 +01002810 if (i >= MAX_HWIFS * MAX_DRIVES)
2811 return -ENXIO;
2812
2813 tape = ide_tape_chrdev_get(i);
2814 if (!tape)
2815 return -ENXIO;
2816
2817 debug_log(DBG_CHRDEV, "Enter %s\n", __func__);
2818
Linus Torvalds1da177e2005-04-16 15:20:36 -07002819 /*
2820 * We really want to do nonseekable_open(inode, filp); here, but some
2821 * versions of tar incorrectly call lseek on tapes and bail out if that
2822 * fails. So we disallow pread() and pwrite(), but permit lseeks.
2823 */
2824 filp->f_mode &= ~(FMODE_PREAD | FMODE_PWRITE);
2825
Linus Torvalds1da177e2005-04-16 15:20:36 -07002826 drive = tape->drive;
2827
2828 filp->private_data = tape;
2829
Borislav Petkov346331f2008-04-18 00:46:26 +02002830 if (test_and_set_bit(IDETAPE_FLAG_BUSY, &tape->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002831 retval = -EBUSY;
2832 goto out_put_tape;
2833 }
2834
2835 retval = idetape_wait_ready(drive, 60 * HZ);
2836 if (retval) {
Borislav Petkov346331f2008-04-18 00:46:26 +02002837 clear_bit(IDETAPE_FLAG_BUSY, &tape->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002838 printk(KERN_ERR "ide-tape: %s: drive not ready\n", tape->name);
2839 goto out_put_tape;
2840 }
2841
2842 idetape_read_position(drive);
Borislav Petkov346331f2008-04-18 00:46:26 +02002843 if (!test_bit(IDETAPE_FLAG_ADDRESS_VALID, &tape->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002844 (void)idetape_rewind_tape(drive);
2845
Borislav Petkov54abf372008-02-06 02:57:52 +01002846 if (tape->chrdev_dir != IDETAPE_DIR_READ)
Borislav Petkov346331f2008-04-18 00:46:26 +02002847 clear_bit(IDETAPE_FLAG_PIPELINE_ERR, &tape->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002848
2849 /* Read block size and write protect status from drive. */
Borislav Petkov3cffb9c2008-02-02 19:56:50 +01002850 ide_tape_get_bsize_from_bdesc(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002851
2852 /* Set write protect flag if device is opened as read-only. */
2853 if ((filp->f_flags & O_ACCMODE) == O_RDONLY)
2854 tape->write_prot = 1;
2855 else
2856 tape->write_prot = tape->drv_write_prot;
2857
2858 /* Make sure drive isn't write protected if user wants to write. */
2859 if (tape->write_prot) {
2860 if ((filp->f_flags & O_ACCMODE) == O_WRONLY ||
2861 (filp->f_flags & O_ACCMODE) == O_RDWR) {
Borislav Petkov346331f2008-04-18 00:46:26 +02002862 clear_bit(IDETAPE_FLAG_BUSY, &tape->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002863 retval = -EROFS;
2864 goto out_put_tape;
2865 }
2866 }
2867
Borislav Petkov3c98bf32008-02-06 02:57:53 +01002868 /* Lock the tape drive door so user can't eject. */
Borislav Petkov54abf372008-02-06 02:57:52 +01002869 if (tape->chrdev_dir == IDETAPE_DIR_NONE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002870 if (idetape_create_prevent_cmd(drive, &pc, 1)) {
2871 if (!idetape_queue_pc_tail(drive, &pc)) {
2872 if (tape->door_locked != DOOR_EXPLICITLY_LOCKED)
2873 tape->door_locked = DOOR_LOCKED;
2874 }
2875 }
2876 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002877 return 0;
2878
2879out_put_tape:
2880 ide_tape_put(tape);
2881 return retval;
2882}
2883
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002884static void idetape_write_release(ide_drive_t *drive, unsigned int minor)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002885{
2886 idetape_tape_t *tape = drive->driver_data;
2887
2888 idetape_empty_write_pipeline(drive);
2889 tape->merge_stage = __idetape_kmalloc_stage(tape, 1, 0);
2890 if (tape->merge_stage != NULL) {
Borislav Petkov54bb2072008-02-06 02:57:52 +01002891 idetape_pad_zeros(drive, tape->blk_size *
2892 (tape->user_bs_factor - 1));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002893 __idetape_kfree_stage(tape->merge_stage);
2894 tape->merge_stage = NULL;
2895 }
2896 idetape_write_filemark(drive);
2897 idetape_flush_tape_buffers(drive);
2898 idetape_flush_tape_buffers(drive);
2899}
2900
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002901static int idetape_chrdev_release(struct inode *inode, struct file *filp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002902{
2903 struct ide_tape_obj *tape = ide_tape_f(filp);
2904 ide_drive_t *drive = tape->drive;
Borislav Petkovd236d742008-04-18 00:46:27 +02002905 struct ide_atapi_pc pc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002906 unsigned int minor = iminor(inode);
2907
2908 lock_kernel();
2909 tape = drive->driver_data;
Borislav Petkov8004a8c2008-02-06 02:57:51 +01002910
2911 debug_log(DBG_CHRDEV, "Enter %s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002912
Borislav Petkov54abf372008-02-06 02:57:52 +01002913 if (tape->chrdev_dir == IDETAPE_DIR_WRITE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002914 idetape_write_release(drive, minor);
Borislav Petkov54abf372008-02-06 02:57:52 +01002915 if (tape->chrdev_dir == IDETAPE_DIR_READ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002916 if (minor < 128)
2917 idetape_discard_read_pipeline(drive, 1);
2918 else
2919 idetape_wait_for_pipeline(drive);
2920 }
Borislav Petkovf64eee72008-04-27 15:38:25 +02002921
Borislav Petkov346331f2008-04-18 00:46:26 +02002922 if (minor < 128 && test_bit(IDETAPE_FLAG_MEDIUM_PRESENT, &tape->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002923 (void) idetape_rewind_tape(drive);
Borislav Petkov54abf372008-02-06 02:57:52 +01002924 if (tape->chrdev_dir == IDETAPE_DIR_NONE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002925 if (tape->door_locked == DOOR_LOCKED) {
2926 if (idetape_create_prevent_cmd(drive, &pc, 0)) {
2927 if (!idetape_queue_pc_tail(drive, &pc))
2928 tape->door_locked = DOOR_UNLOCKED;
2929 }
2930 }
2931 }
Borislav Petkov346331f2008-04-18 00:46:26 +02002932 clear_bit(IDETAPE_FLAG_BUSY, &tape->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002933 ide_tape_put(tape);
2934 unlock_kernel();
2935 return 0;
2936}
2937
2938/*
Borislav Petkov71071b82008-02-06 02:57:53 +01002939 * check the contents of the ATAPI IDENTIFY command results. We return:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002940 *
Borislav Petkov71071b82008-02-06 02:57:53 +01002941 * 1 - If the tape can be supported by us, based on the information we have so
2942 * far.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002943 *
Borislav Petkov71071b82008-02-06 02:57:53 +01002944 * 0 - If this tape driver is not currently supported by us.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002945 */
Borislav Petkov71071b82008-02-06 02:57:53 +01002946static int idetape_identify_device(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002947{
Borislav Petkov71071b82008-02-06 02:57:53 +01002948 u8 gcw[2], protocol, device_type, removable, packet_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002949
2950 if (drive->id_read == 0)
2951 return 1;
2952
Borislav Petkov71071b82008-02-06 02:57:53 +01002953 *((unsigned short *) &gcw) = drive->id->config;
2954
2955 protocol = (gcw[1] & 0xC0) >> 6;
2956 device_type = gcw[1] & 0x1F;
2957 removable = !!(gcw[0] & 0x80);
2958 packet_size = gcw[0] & 0x3;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002959
Linus Torvalds1da177e2005-04-16 15:20:36 -07002960 /* Check that we can support this device */
Borislav Petkov71071b82008-02-06 02:57:53 +01002961 if (protocol != 2)
Bartlomiej Zolnierkiewicz16422de32008-02-02 19:56:48 +01002962 printk(KERN_ERR "ide-tape: Protocol (0x%02x) is not ATAPI\n",
Borislav Petkov71071b82008-02-06 02:57:53 +01002963 protocol);
2964 else if (device_type != 1)
Bartlomiej Zolnierkiewicz16422de32008-02-02 19:56:48 +01002965 printk(KERN_ERR "ide-tape: Device type (0x%02x) is not set "
Borislav Petkov71071b82008-02-06 02:57:53 +01002966 "to tape\n", device_type);
2967 else if (!removable)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002968 printk(KERN_ERR "ide-tape: The removable flag is not set\n");
Borislav Petkov71071b82008-02-06 02:57:53 +01002969 else if (packet_size != 0) {
Borislav Petkov24d57f82008-02-06 02:57:54 +01002970 printk(KERN_ERR "ide-tape: Packet size (0x%02x) is not 12"
2971 " bytes\n", packet_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002972 } else
2973 return 1;
2974 return 0;
2975}
2976
Borislav Petkov6d29c8f2008-02-02 19:56:49 +01002977static void idetape_get_inquiry_results(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002978{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002979 idetape_tape_t *tape = drive->driver_data;
Borislav Petkovd236d742008-04-18 00:46:27 +02002980 struct ide_atapi_pc pc;
Borislav Petkov41f81d542008-02-06 02:57:52 +01002981 char fw_rev[6], vendor_id[10], product_id[18];
Borislav Petkov6d29c8f2008-02-02 19:56:49 +01002982
Linus Torvalds1da177e2005-04-16 15:20:36 -07002983 idetape_create_inquiry_cmd(&pc);
2984 if (idetape_queue_pc_tail(drive, &pc)) {
Borislav Petkov6d29c8f2008-02-02 19:56:49 +01002985 printk(KERN_ERR "ide-tape: %s: can't get INQUIRY results\n",
2986 tape->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002987 return;
2988 }
Borislav Petkovd236d742008-04-18 00:46:27 +02002989 memcpy(vendor_id, &pc.buf[8], 8);
2990 memcpy(product_id, &pc.buf[16], 16);
2991 memcpy(fw_rev, &pc.buf[32], 4);
Borislav Petkov6d29c8f2008-02-02 19:56:49 +01002992
Borislav Petkov41f81d542008-02-06 02:57:52 +01002993 ide_fixstring(vendor_id, 10, 0);
2994 ide_fixstring(product_id, 18, 0);
2995 ide_fixstring(fw_rev, 6, 0);
2996
Borislav Petkov6d29c8f2008-02-02 19:56:49 +01002997 printk(KERN_INFO "ide-tape: %s <-> %s: %s %s rev %s\n",
Borislav Petkov41f81d542008-02-06 02:57:52 +01002998 drive->name, tape->name, vendor_id, product_id, fw_rev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002999}
3000
3001/*
Borislav Petkovb6422012008-02-02 19:56:49 +01003002 * Ask the tape about its various parameters. In particular, we will adjust our
3003 * data transfer buffer size to the recommended value as returned by the tape.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003004 */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01003005static void idetape_get_mode_sense_results(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003006{
3007 idetape_tape_t *tape = drive->driver_data;
Borislav Petkovd236d742008-04-18 00:46:27 +02003008 struct ide_atapi_pc pc;
Borislav Petkovb6422012008-02-02 19:56:49 +01003009 u8 *caps;
3010 u8 speed, max_speed;
Borislav Petkov47314fa2008-02-02 19:56:48 +01003011
Linus Torvalds1da177e2005-04-16 15:20:36 -07003012 idetape_create_mode_sense_cmd(&pc, IDETAPE_CAPABILITIES_PAGE);
3013 if (idetape_queue_pc_tail(drive, &pc)) {
Borislav Petkovb6422012008-02-02 19:56:49 +01003014 printk(KERN_ERR "ide-tape: Can't get tape parameters - assuming"
3015 " some default values\n");
Borislav Petkov54bb2072008-02-06 02:57:52 +01003016 tape->blk_size = 512;
Borislav Petkovb6422012008-02-02 19:56:49 +01003017 put_unaligned(52, (u16 *)&tape->caps[12]);
3018 put_unaligned(540, (u16 *)&tape->caps[14]);
3019 put_unaligned(6*52, (u16 *)&tape->caps[16]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003020 return;
3021 }
Borislav Petkovd236d742008-04-18 00:46:27 +02003022 caps = pc.buf + 4 + pc.buf[3];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003023
Borislav Petkovb6422012008-02-02 19:56:49 +01003024 /* convert to host order and save for later use */
3025 speed = be16_to_cpu(*(u16 *)&caps[14]);
3026 max_speed = be16_to_cpu(*(u16 *)&caps[8]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003027
Borislav Petkovb6422012008-02-02 19:56:49 +01003028 put_unaligned(max_speed, (u16 *)&caps[8]);
3029 put_unaligned(be16_to_cpu(*(u16 *)&caps[12]), (u16 *)&caps[12]);
3030 put_unaligned(speed, (u16 *)&caps[14]);
3031 put_unaligned(be16_to_cpu(*(u16 *)&caps[16]), (u16 *)&caps[16]);
3032
3033 if (!speed) {
3034 printk(KERN_INFO "ide-tape: %s: invalid tape speed "
3035 "(assuming 650KB/sec)\n", drive->name);
3036 put_unaligned(650, (u16 *)&caps[14]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003037 }
Borislav Petkovb6422012008-02-02 19:56:49 +01003038 if (!max_speed) {
3039 printk(KERN_INFO "ide-tape: %s: invalid max_speed "
3040 "(assuming 650KB/sec)\n", drive->name);
3041 put_unaligned(650, (u16 *)&caps[8]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003042 }
3043
Borislav Petkovb6422012008-02-02 19:56:49 +01003044 memcpy(&tape->caps, caps, 20);
3045 if (caps[7] & 0x02)
Borislav Petkov54bb2072008-02-06 02:57:52 +01003046 tape->blk_size = 512;
Borislav Petkovb6422012008-02-02 19:56:49 +01003047 else if (caps[7] & 0x04)
Borislav Petkov54bb2072008-02-06 02:57:52 +01003048 tape->blk_size = 1024;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003049}
3050
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +02003051#ifdef CONFIG_IDE_PROC_FS
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01003052static void idetape_add_settings(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003053{
3054 idetape_tape_t *tape = drive->driver_data;
3055
Borislav Petkovb6422012008-02-02 19:56:49 +01003056 ide_add_setting(drive, "buffer", SETTING_READ, TYPE_SHORT, 0, 0xffff,
3057 1, 2, (u16 *)&tape->caps[16], NULL);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01003058 ide_add_setting(drive, "pipeline_min", SETTING_RW, TYPE_INT, 1, 0xffff,
3059 tape->stage_size / 1024, 1, &tape->min_pipeline, NULL);
3060 ide_add_setting(drive, "pipeline", SETTING_RW, TYPE_INT, 1, 0xffff,
3061 tape->stage_size / 1024, 1, &tape->max_stages, NULL);
3062 ide_add_setting(drive, "pipeline_max", SETTING_RW, TYPE_INT, 1, 0xffff,
3063 tape->stage_size / 1024, 1, &tape->max_pipeline, NULL);
3064 ide_add_setting(drive, "pipeline_used", SETTING_READ, TYPE_INT, 0,
3065 0xffff, tape->stage_size / 1024, 1, &tape->nr_stages,
3066 NULL);
3067 ide_add_setting(drive, "pipeline_pending", SETTING_READ, TYPE_INT, 0,
3068 0xffff, tape->stage_size / 1024, 1,
3069 &tape->nr_pending_stages, NULL);
Borislav Petkovb6422012008-02-02 19:56:49 +01003070 ide_add_setting(drive, "speed", SETTING_READ, TYPE_SHORT, 0, 0xffff,
3071 1, 1, (u16 *)&tape->caps[14], NULL);
Borislav Petkov54bb2072008-02-06 02:57:52 +01003072 ide_add_setting(drive, "stage", SETTING_READ, TYPE_INT, 0, 0xffff, 1,
3073 1024, &tape->stage_size, NULL);
3074 ide_add_setting(drive, "tdsc", SETTING_RW, TYPE_INT, IDETAPE_DSC_RW_MIN,
3075 IDETAPE_DSC_RW_MAX, 1000, HZ, &tape->best_dsc_rw_freq,
3076 NULL);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01003077 ide_add_setting(drive, "dsc_overlap", SETTING_RW, TYPE_BYTE, 0, 1, 1,
3078 1, &drive->dsc_overlap, NULL);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01003079 ide_add_setting(drive, "avg_speed", SETTING_READ, TYPE_INT, 0, 0xffff,
3080 1, 1, &tape->avg_speed, NULL);
Borislav Petkov8004a8c2008-02-06 02:57:51 +01003081 ide_add_setting(drive, "debug_mask", SETTING_RW, TYPE_INT, 0, 0xffff, 1,
3082 1, &tape->debug_mask, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003083}
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +02003084#else
3085static inline void idetape_add_settings(ide_drive_t *drive) { ; }
3086#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003087
3088/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +01003089 * The function below is called to:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003090 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +01003091 * 1. Initialize our various state variables.
3092 * 2. Ask the tape for its capabilities.
3093 * 3. Allocate a buffer which will be used for data transfer. The buffer size
3094 * is chosen based on the recommendation which we received in step 2.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003095 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +01003096 * Note that at this point ide.c already assigned us an irq, so that we can
3097 * queue requests here and wait for their completion.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003098 */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01003099static void idetape_setup(ide_drive_t *drive, idetape_tape_t *tape, int minor)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003100{
3101 unsigned long t1, tmid, tn, t;
3102 int speed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003103 int stage_size;
Borislav Petkov71071b82008-02-06 02:57:53 +01003104 u8 gcw[2];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003105 struct sysinfo si;
Borislav Petkovb6422012008-02-02 19:56:49 +01003106 u16 *ctl = (u16 *)&tape->caps[12];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003107
Borislav Petkov54bb2072008-02-06 02:57:52 +01003108 spin_lock_init(&tape->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003109 drive->dsc_overlap = 1;
Bartlomiej Zolnierkiewicz4166c192008-02-01 23:09:30 +01003110 if (drive->hwif->host_flags & IDE_HFLAG_NO_DSC) {
3111 printk(KERN_INFO "ide-tape: %s: disabling DSC overlap\n",
3112 tape->name);
3113 drive->dsc_overlap = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003114 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003115 /* Seagate Travan drives do not support DSC overlap. */
3116 if (strstr(drive->id->model, "Seagate STT3401"))
3117 drive->dsc_overlap = 0;
3118 tape->minor = minor;
3119 tape->name[0] = 'h';
3120 tape->name[1] = 't';
3121 tape->name[2] = '0' + minor;
Borislav Petkov54abf372008-02-06 02:57:52 +01003122 tape->chrdev_dir = IDETAPE_DIR_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003123 tape->pc = tape->pc_stack;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003124 *((unsigned short *) &gcw) = drive->id->config;
Borislav Petkov71071b82008-02-06 02:57:53 +01003125
3126 /* Command packet DRQ type */
3127 if (((gcw[0] & 0x60) >> 5) == 1)
Borislav Petkov346331f2008-04-18 00:46:26 +02003128 set_bit(IDETAPE_FLAG_DRQ_INTERRUPT, &tape->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003129
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01003130 tape->min_pipeline = 10;
3131 tape->max_pipeline = 10;
3132 tape->max_stages = 10;
Borislav Petkov3c98bf32008-02-06 02:57:53 +01003133
Linus Torvalds1da177e2005-04-16 15:20:36 -07003134 idetape_get_inquiry_results(drive);
3135 idetape_get_mode_sense_results(drive);
Borislav Petkov3cffb9c2008-02-02 19:56:50 +01003136 ide_tape_get_bsize_from_bdesc(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003137 tape->user_bs_factor = 1;
Borislav Petkov54bb2072008-02-06 02:57:52 +01003138 tape->stage_size = *ctl * tape->blk_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003139 while (tape->stage_size > 0xffff) {
3140 printk(KERN_NOTICE "ide-tape: decreasing stage size\n");
Borislav Petkovb6422012008-02-02 19:56:49 +01003141 *ctl /= 2;
Borislav Petkov54bb2072008-02-06 02:57:52 +01003142 tape->stage_size = *ctl * tape->blk_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003143 }
3144 stage_size = tape->stage_size;
3145 tape->pages_per_stage = stage_size / PAGE_SIZE;
3146 if (stage_size % PAGE_SIZE) {
3147 tape->pages_per_stage++;
3148 tape->excess_bh_size = PAGE_SIZE - stage_size % PAGE_SIZE;
3149 }
3150
Borislav Petkovb6422012008-02-02 19:56:49 +01003151 /* Select the "best" DSC read/write polling freq and pipeline size. */
3152 speed = max(*(u16 *)&tape->caps[14], *(u16 *)&tape->caps[8]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003153
3154 tape->max_stages = speed * 1000 * 10 / tape->stage_size;
3155
Borislav Petkov3c98bf32008-02-06 02:57:53 +01003156 /* Limit memory use for pipeline to 10% of physical memory */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003157 si_meminfo(&si);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01003158 if (tape->max_stages * tape->stage_size >
3159 si.totalram * si.mem_unit / 10)
3160 tape->max_stages =
3161 si.totalram * si.mem_unit / (10 * tape->stage_size);
3162
Linus Torvalds1da177e2005-04-16 15:20:36 -07003163 tape->max_stages = min(tape->max_stages, IDETAPE_MAX_PIPELINE_STAGES);
3164 tape->min_pipeline = min(tape->max_stages, IDETAPE_MIN_PIPELINE_STAGES);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01003165 tape->max_pipeline =
3166 min(tape->max_stages * 2, IDETAPE_MAX_PIPELINE_STAGES);
3167 if (tape->max_stages == 0) {
3168 tape->max_stages = 1;
3169 tape->min_pipeline = 1;
3170 tape->max_pipeline = 1;
3171 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003172
3173 t1 = (tape->stage_size * HZ) / (speed * 1000);
Borislav Petkovb6422012008-02-02 19:56:49 +01003174 tmid = (*(u16 *)&tape->caps[16] * 32 * HZ) / (speed * 125);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003175 tn = (IDETAPE_FIFO_THRESHOLD * tape->stage_size * HZ) / (speed * 1000);
3176
3177 if (tape->max_stages)
3178 t = tn;
3179 else
3180 t = t1;
3181
3182 /*
Borislav Petkov3c98bf32008-02-06 02:57:53 +01003183 * Ensure that the number we got makes sense; limit it within
3184 * IDETAPE_DSC_RW_MIN and IDETAPE_DSC_RW_MAX.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003185 */
Borislav Petkov54bb2072008-02-06 02:57:52 +01003186 tape->best_dsc_rw_freq = max_t(unsigned long,
3187 min_t(unsigned long, t, IDETAPE_DSC_RW_MAX),
3188 IDETAPE_DSC_RW_MIN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003189 printk(KERN_INFO "ide-tape: %s <-> %s: %dKBps, %d*%dkB buffer, "
3190 "%dkB pipeline, %lums tDSC%s\n",
Borislav Petkovb6422012008-02-02 19:56:49 +01003191 drive->name, tape->name, *(u16 *)&tape->caps[14],
3192 (*(u16 *)&tape->caps[16] * 512) / tape->stage_size,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003193 tape->stage_size / 1024,
3194 tape->max_stages * tape->stage_size / 1024,
Borislav Petkov54bb2072008-02-06 02:57:52 +01003195 tape->best_dsc_rw_freq * 1000 / HZ,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003196 drive->using_dma ? ", DMA":"");
3197
3198 idetape_add_settings(drive);
3199}
3200
Russell King4031bbe2006-01-06 11:41:00 +00003201static void ide_tape_remove(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003202{
3203 idetape_tape_t *tape = drive->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003204
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +02003205 ide_proc_unregister_driver(drive, tape->driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003206
3207 ide_unregister_region(tape->disk);
3208
3209 ide_tape_put(tape);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003210}
3211
3212static void ide_tape_release(struct kref *kref)
3213{
3214 struct ide_tape_obj *tape = to_ide_tape(kref);
3215 ide_drive_t *drive = tape->drive;
3216 struct gendisk *g = tape->disk;
3217
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02003218 BUG_ON(tape->first_stage != NULL || tape->merge_stage_size);
3219
Linus Torvalds1da177e2005-04-16 15:20:36 -07003220 drive->dsc_overlap = 0;
3221 drive->driver_data = NULL;
Tony Jonesdbc12722007-09-25 02:03:03 +02003222 device_destroy(idetape_sysfs_class, MKDEV(IDETAPE_MAJOR, tape->minor));
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01003223 device_destroy(idetape_sysfs_class,
3224 MKDEV(IDETAPE_MAJOR, tape->minor + 128));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003225 idetape_devs[tape->minor] = NULL;
3226 g->private_data = NULL;
3227 put_disk(g);
3228 kfree(tape);
3229}
3230
Bartlomiej Zolnierkiewiczecfd80e2007-05-10 00:01:09 +02003231#ifdef CONFIG_IDE_PROC_FS
Linus Torvalds1da177e2005-04-16 15:20:36 -07003232static int proc_idetape_read_name
3233 (char *page, char **start, off_t off, int count, int *eof, void *data)
3234{
3235 ide_drive_t *drive = (ide_drive_t *) data;
3236 idetape_tape_t *tape = drive->driver_data;
3237 char *out = page;
3238 int len;
3239
3240 len = sprintf(out, "%s\n", tape->name);
3241 PROC_IDE_READ_RETURN(page, start, off, count, eof, len);
3242}
3243
3244static ide_proc_entry_t idetape_proc[] = {
3245 { "capacity", S_IFREG|S_IRUGO, proc_ide_read_capacity, NULL },
3246 { "name", S_IFREG|S_IRUGO, proc_idetape_read_name, NULL },
3247 { NULL, 0, NULL, NULL }
3248};
Linus Torvalds1da177e2005-04-16 15:20:36 -07003249#endif
3250
Russell King4031bbe2006-01-06 11:41:00 +00003251static int ide_tape_probe(ide_drive_t *);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003252
Linus Torvalds1da177e2005-04-16 15:20:36 -07003253static ide_driver_t idetape_driver = {
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02003254 .gen_driver = {
Laurent Riffard4ef3b8f2005-11-18 22:15:40 +01003255 .owner = THIS_MODULE,
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02003256 .name = "ide-tape",
3257 .bus = &ide_bus_type,
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02003258 },
Russell King4031bbe2006-01-06 11:41:00 +00003259 .probe = ide_tape_probe,
3260 .remove = ide_tape_remove,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003261 .version = IDETAPE_VERSION,
3262 .media = ide_tape,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003263 .supports_dsc_overlap = 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003264 .do_request = idetape_do_request,
3265 .end_request = idetape_end_request,
3266 .error = __ide_error,
3267 .abort = __ide_abort,
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +02003268#ifdef CONFIG_IDE_PROC_FS
Linus Torvalds1da177e2005-04-16 15:20:36 -07003269 .proc = idetape_proc,
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +02003270#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003271};
3272
Borislav Petkov3c98bf32008-02-06 02:57:53 +01003273/* Our character device supporting functions, passed to register_chrdev. */
Arjan van de Ven2b8693c2007-02-12 00:55:32 -08003274static const struct file_operations idetape_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003275 .owner = THIS_MODULE,
3276 .read = idetape_chrdev_read,
3277 .write = idetape_chrdev_write,
3278 .ioctl = idetape_chrdev_ioctl,
3279 .open = idetape_chrdev_open,
3280 .release = idetape_chrdev_release,
3281};
3282
3283static int idetape_open(struct inode *inode, struct file *filp)
3284{
3285 struct gendisk *disk = inode->i_bdev->bd_disk;
3286 struct ide_tape_obj *tape;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003287
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01003288 tape = ide_tape_get(disk);
3289 if (!tape)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003290 return -ENXIO;
3291
Linus Torvalds1da177e2005-04-16 15:20:36 -07003292 return 0;
3293}
3294
3295static int idetape_release(struct inode *inode, struct file *filp)
3296{
3297 struct gendisk *disk = inode->i_bdev->bd_disk;
3298 struct ide_tape_obj *tape = ide_tape_g(disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003299
3300 ide_tape_put(tape);
3301
3302 return 0;
3303}
3304
3305static int idetape_ioctl(struct inode *inode, struct file *file,
3306 unsigned int cmd, unsigned long arg)
3307{
3308 struct block_device *bdev = inode->i_bdev;
3309 struct ide_tape_obj *tape = ide_tape_g(bdev->bd_disk);
3310 ide_drive_t *drive = tape->drive;
3311 int err = generic_ide_ioctl(drive, file, bdev, cmd, arg);
3312 if (err == -EINVAL)
3313 err = idetape_blkdev_ioctl(drive, cmd, arg);
3314 return err;
3315}
3316
3317static struct block_device_operations idetape_block_ops = {
3318 .owner = THIS_MODULE,
3319 .open = idetape_open,
3320 .release = idetape_release,
3321 .ioctl = idetape_ioctl,
3322};
3323
Russell King4031bbe2006-01-06 11:41:00 +00003324static int ide_tape_probe(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003325{
3326 idetape_tape_t *tape;
3327 struct gendisk *g;
3328 int minor;
3329
3330 if (!strstr("ide-tape", drive->driver_req))
3331 goto failed;
3332 if (!drive->present)
3333 goto failed;
3334 if (drive->media != ide_tape)
3335 goto failed;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01003336 if (!idetape_identify_device(drive)) {
3337 printk(KERN_ERR "ide-tape: %s: not supported by this version of"
3338 " the driver\n", drive->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003339 goto failed;
3340 }
3341 if (drive->scsi) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01003342 printk(KERN_INFO "ide-tape: passing drive %s to ide-scsi"
3343 " emulation.\n", drive->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003344 goto failed;
3345 }
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01003346 tape = kzalloc(sizeof(idetape_tape_t), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003347 if (tape == NULL) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01003348 printk(KERN_ERR "ide-tape: %s: Can't allocate a tape struct\n",
3349 drive->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003350 goto failed;
3351 }
3352
3353 g = alloc_disk(1 << PARTN_BITS);
3354 if (!g)
3355 goto out_free_tape;
3356
3357 ide_init_disk(g, drive);
3358
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +02003359 ide_proc_register_driver(drive, &idetape_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003360
Linus Torvalds1da177e2005-04-16 15:20:36 -07003361 kref_init(&tape->kref);
3362
3363 tape->drive = drive;
3364 tape->driver = &idetape_driver;
3365 tape->disk = g;
3366
3367 g->private_data = &tape->driver;
3368
3369 drive->driver_data = tape;
3370
Arjan van de Vencf8b8972006-03-23 03:00:45 -08003371 mutex_lock(&idetape_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003372 for (minor = 0; idetape_devs[minor]; minor++)
3373 ;
3374 idetape_devs[minor] = tape;
Arjan van de Vencf8b8972006-03-23 03:00:45 -08003375 mutex_unlock(&idetape_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003376
3377 idetape_setup(drive, tape, minor);
3378
Tony Jonesdbc12722007-09-25 02:03:03 +02003379 device_create(idetape_sysfs_class, &drive->gendev,
3380 MKDEV(IDETAPE_MAJOR, minor), "%s", tape->name);
3381 device_create(idetape_sysfs_class, &drive->gendev,
3382 MKDEV(IDETAPE_MAJOR, minor + 128), "n%s", tape->name);
Will Dysond5dee802005-09-16 02:55:07 -07003383
Linus Torvalds1da177e2005-04-16 15:20:36 -07003384 g->fops = &idetape_block_ops;
3385 ide_register_region(g);
3386
3387 return 0;
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02003388
Linus Torvalds1da177e2005-04-16 15:20:36 -07003389out_free_tape:
3390 kfree(tape);
3391failed:
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02003392 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003393}
3394
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01003395static void __exit idetape_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003396{
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02003397 driver_unregister(&idetape_driver.gen_driver);
Will Dysond5dee802005-09-16 02:55:07 -07003398 class_destroy(idetape_sysfs_class);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003399 unregister_chrdev(IDETAPE_MAJOR, "ht");
3400}
3401
Bartlomiej Zolnierkiewicz17514e82005-11-19 22:24:35 +01003402static int __init idetape_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003403{
Will Dysond5dee802005-09-16 02:55:07 -07003404 int error = 1;
3405 idetape_sysfs_class = class_create(THIS_MODULE, "ide_tape");
3406 if (IS_ERR(idetape_sysfs_class)) {
3407 idetape_sysfs_class = NULL;
3408 printk(KERN_ERR "Unable to create sysfs class for ide tapes\n");
3409 error = -EBUSY;
3410 goto out;
3411 }
3412
Linus Torvalds1da177e2005-04-16 15:20:36 -07003413 if (register_chrdev(IDETAPE_MAJOR, "ht", &idetape_fops)) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01003414 printk(KERN_ERR "ide-tape: Failed to register chrdev"
3415 " interface\n");
Will Dysond5dee802005-09-16 02:55:07 -07003416 error = -EBUSY;
3417 goto out_free_class;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003418 }
Will Dysond5dee802005-09-16 02:55:07 -07003419
3420 error = driver_register(&idetape_driver.gen_driver);
3421 if (error)
3422 goto out_free_driver;
3423
3424 return 0;
3425
3426out_free_driver:
3427 driver_unregister(&idetape_driver.gen_driver);
3428out_free_class:
3429 class_destroy(idetape_sysfs_class);
3430out:
3431 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003432}
3433
Kay Sievers263756e2005-12-12 18:03:44 +01003434MODULE_ALIAS("ide:*m-tape*");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003435module_init(idetape_init);
3436module_exit(idetape_exit);
3437MODULE_ALIAS_CHARDEV_MAJOR(IDETAPE_MAJOR);
Borislav Petkov9c145762008-02-06 02:57:54 +01003438MODULE_DESCRIPTION("ATAPI Streaming TAPE Driver");
3439MODULE_LICENSE("GPL");