blob: cbba475ebc5a3f88f89b506686e13c441ec5b550 [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/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 * This will free all the pipeline stages starting from new_last_stage->next
696 * to the end of the list, and point tape->last_stage to new_last_stage.
697 */
698static void idetape_abort_pipeline(ide_drive_t *drive,
699 idetape_stage_t *new_last_stage)
700{
701 idetape_tape_t *tape = drive->driver_data;
702 idetape_stage_t *stage = new_last_stage->next;
703 idetape_stage_t *nstage;
704
Borislav Petkov8004a8c2008-02-06 02:57:51 +0100705 debug_log(DBG_PROCS, "%s: Enter %s\n", tape->name, __func__);
706
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707 while (stage) {
708 nstage = stage->next;
709 idetape_kfree_stage(tape, stage);
710 --tape->nr_stages;
711 --tape->nr_pending_stages;
712 stage = nstage;
713 }
714 if (new_last_stage)
715 new_last_stage->next = NULL;
716 tape->last_stage = new_last_stage;
717 tape->next_stage = NULL;
718}
719
720/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100721 * Finish servicing a request and insert a pending pipeline request into the
722 * main device queue.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 */
724static int idetape_end_request(ide_drive_t *drive, int uptodate, int nr_sects)
725{
726 struct request *rq = HWGROUP(drive)->rq;
727 idetape_tape_t *tape = drive->driver_data;
728 unsigned long flags;
729 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 idetape_stage_t *active_stage;
731
Borislav Petkov8004a8c2008-02-06 02:57:51 +0100732 debug_log(DBG_PROCS, "Enter %s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733
734 switch (uptodate) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100735 case 0: error = IDETAPE_ERROR_GENERAL; break;
736 case 1: error = 0; break;
737 default: error = uptodate;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738 }
739 rq->errors = error;
740 if (error)
741 tape->failed_pc = NULL;
742
Bartlomiej Zolnierkiewicz36872212008-01-26 20:13:10 +0100743 if (!blk_special_request(rq)) {
744 ide_end_request(drive, uptodate, nr_sects);
745 return 0;
746 }
747
Borislav Petkov54bb2072008-02-06 02:57:52 +0100748 spin_lock_irqsave(&tape->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749
750 /* The request was a pipelined data transfer request */
Borislav Petkov54bb2072008-02-06 02:57:52 +0100751 if (tape->active_data_rq == rq) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752 active_stage = tape->active_stage;
753 tape->active_stage = NULL;
Borislav Petkov54bb2072008-02-06 02:57:52 +0100754 tape->active_data_rq = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755 tape->nr_pending_stages--;
756 if (rq->cmd[0] & REQ_IDETAPE_WRITE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 if (error) {
Borislav Petkov346331f2008-04-18 00:46:26 +0200758 set_bit(IDETAPE_FLAG_PIPELINE_ERR,
759 &tape->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760 if (error == IDETAPE_ERROR_EOD)
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100761 idetape_abort_pipeline(drive,
762 active_stage);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 }
764 } else if (rq->cmd[0] & REQ_IDETAPE_READ) {
765 if (error == IDETAPE_ERROR_EOD) {
Borislav Petkov346331f2008-04-18 00:46:26 +0200766 set_bit(IDETAPE_FLAG_PIPELINE_ERR,
767 &tape->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 idetape_abort_pipeline(drive, active_stage);
769 }
770 }
771 if (tape->next_stage != NULL) {
Borislav Petkov419d4742008-02-02 19:56:51 +0100772 idetape_activate_next_stage(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100774 /* Insert the next request into the request queue. */
Borislav Petkov54bb2072008-02-06 02:57:52 +0100775 (void)ide_do_drive_cmd(drive, tape->active_data_rq,
776 ide_end);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777 } else if (!error) {
Borislav Petkov97219852008-02-06 02:57:52 +0100778 /*
779 * This is a part of the feedback loop which tries to
780 * find the optimum number of stages. We are starting
781 * from a minimum maximum number of stages, and if we
782 * sense that the pipeline is empty, we try to increase
783 * it, until we reach the user compile time memory
784 * limit.
785 */
786 int i = (tape->max_pipeline - tape->min_pipeline) / 10;
787
788 tape->max_stages += max(i, 1);
789 tape->max_stages = max(tape->max_stages,
790 tape->min_pipeline);
791 tape->max_stages = min(tape->max_stages,
792 tape->max_pipeline);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793 }
794 }
795 ide_end_drive_cmd(drive, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796
Borislav Petkov54bb2072008-02-06 02:57:52 +0100797 if (tape->active_data_rq == NULL)
Borislav Petkov346331f2008-04-18 00:46:26 +0200798 clear_bit(IDETAPE_FLAG_PIPELINE_ACTIVE, &tape->flags);
Borislav Petkov54bb2072008-02-06 02:57:52 +0100799 spin_unlock_irqrestore(&tape->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800 return 0;
801}
802
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100803static ide_startstop_t idetape_request_sense_callback(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804{
805 idetape_tape_t *tape = drive->driver_data;
806
Borislav Petkov8004a8c2008-02-06 02:57:51 +0100807 debug_log(DBG_PROCS, "Enter %s\n", __func__);
808
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809 if (!tape->pc->error) {
Borislav Petkovd236d742008-04-18 00:46:27 +0200810 idetape_analyze_error(drive, tape->pc->buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811 idetape_end_request(drive, 1, 0);
812 } else {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100813 printk(KERN_ERR "ide-tape: Error in REQUEST SENSE itself - "
814 "Aborting request!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815 idetape_end_request(drive, 0, 0);
816 }
817 return ide_stopped;
818}
819
Borislav Petkovd236d742008-04-18 00:46:27 +0200820static void idetape_create_request_sense_cmd(struct ide_atapi_pc *pc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821{
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100822 idetape_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +0100823 pc->c[0] = REQUEST_SENSE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824 pc->c[4] = 20;
Borislav Petkovd236d742008-04-18 00:46:27 +0200825 pc->req_xfer = 20;
826 pc->idetape_callback = &idetape_request_sense_callback;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827}
828
829static void idetape_init_rq(struct request *rq, u8 cmd)
830{
831 memset(rq, 0, sizeof(*rq));
Jens Axboe4aff5e22006-08-10 08:44:47 +0200832 rq->cmd_type = REQ_TYPE_SPECIAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833 rq->cmd[0] = cmd;
834}
835
836/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100837 * Generate a new packet command request in front of the request queue, before
838 * the current request, so that it will be processed immediately, on the next
839 * pass through the driver. The function below is called from the request
840 * handling part of the driver (the "bottom" part). Safe storage for the request
841 * should be allocated with ide_tape_next_{pc,rq}_storage() prior to that.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100843 * Memory for those requests is pre-allocated at initialization time, and is
844 * limited to IDETAPE_PC_STACK requests. We assume that we have enough space for
845 * the maximum possible number of inter-dependent packet commands.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100847 * The higher level of the driver - The ioctl handler and the character device
848 * handling functions should queue request to the lower level part and wait for
849 * their completion using idetape_queue_pc_tail or idetape_queue_rw_tail.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850 */
Borislav Petkovd236d742008-04-18 00:46:27 +0200851static void idetape_queue_pc_head(ide_drive_t *drive, struct ide_atapi_pc *pc,
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100852 struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853{
854 struct ide_tape_obj *tape = drive->driver_data;
855
856 idetape_init_rq(rq, REQ_IDETAPE_PC1);
857 rq->buffer = (char *) pc;
858 rq->rq_disk = tape->disk;
859 (void) ide_do_drive_cmd(drive, rq, ide_preempt);
860}
861
862/*
863 * idetape_retry_pc is called when an error was detected during the
864 * last packet command. We queue a request sense packet command in
865 * the head of the request list.
866 */
867static ide_startstop_t idetape_retry_pc (ide_drive_t *drive)
868{
869 idetape_tape_t *tape = drive->driver_data;
Borislav Petkovd236d742008-04-18 00:46:27 +0200870 struct ide_atapi_pc *pc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871 struct request *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872
Bartlomiej Zolnierkiewicz64a57fe2008-02-06 02:57:51 +0100873 (void)ide_read_error(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874 pc = idetape_next_pc_storage(drive);
875 rq = idetape_next_rq_storage(drive);
876 idetape_create_request_sense_cmd(pc);
Borislav Petkov346331f2008-04-18 00:46:26 +0200877 set_bit(IDETAPE_FLAG_IGNORE_DSC, &tape->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878 idetape_queue_pc_head(drive, pc, rq);
879 return ide_stopped;
880}
881
882/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100883 * Postpone the current request so that ide.c will be able to service requests
884 * from another device on the same hwgroup while we are polling for DSC.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885 */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100886static void idetape_postpone_request(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887{
888 idetape_tape_t *tape = drive->driver_data;
889
Borislav Petkov8004a8c2008-02-06 02:57:51 +0100890 debug_log(DBG_PROCS, "Enter %s\n", __func__);
891
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892 tape->postponed_rq = HWGROUP(drive)->rq;
Borislav Petkov54bb2072008-02-06 02:57:52 +0100893 ide_stall_queue(drive, tape->dsc_poll_freq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894}
895
Borislav Petkovd236d742008-04-18 00:46:27 +0200896typedef void idetape_io_buf(ide_drive_t *, struct ide_atapi_pc *, unsigned int);
Borislav Petkova1efc852008-02-06 02:57:52 +0100897
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898/*
Borislav Petkova1efc852008-02-06 02:57:52 +0100899 * This is the usual interrupt handler which will be called during a packet
900 * command. We will transfer some of the data (as requested by the drive) and
901 * will re-point interrupt handler to us. When data transfer is finished, we
902 * will act according to the algorithm described before
Borislav Petkov8d06bfa2008-02-06 02:57:53 +0100903 * idetape_issue_pc.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904 */
Borislav Petkova1efc852008-02-06 02:57:52 +0100905static ide_startstop_t idetape_pc_intr(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906{
907 ide_hwif_t *hwif = drive->hwif;
908 idetape_tape_t *tape = drive->driver_data;
Borislav Petkovd236d742008-04-18 00:46:27 +0200909 struct ide_atapi_pc *pc = tape->pc;
Borislav Petkova1efc852008-02-06 02:57:52 +0100910 xfer_func_t *xferfunc;
911 idetape_io_buf *iobuf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912 unsigned int temp;
913#if SIMULATE_ERRORS
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100914 static int error_sim_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915#endif
Bartlomiej Zolnierkiewicz790d1232008-01-25 22:17:12 +0100916 u16 bcount;
Bartlomiej Zolnierkiewicz8e7657a2008-01-25 22:17:12 +0100917 u8 stat, ireason;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918
Borislav Petkov8004a8c2008-02-06 02:57:51 +0100919 debug_log(DBG_PROCS, "Enter %s - interrupt handler\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920
921 /* Clear the interrupt */
Bartlomiej Zolnierkiewiczc47137a2008-02-06 02:57:51 +0100922 stat = ide_read_status(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923
Borislav Petkov346331f2008-04-18 00:46:26 +0200924 if (pc->flags & PC_FLAG_DMA_IN_PROGRESS) {
Bartlomiej Zolnierkiewicz5e37bdc2008-04-26 22:25:24 +0200925 if (hwif->dma_ops->dma_end(drive) || (stat & ERR_STAT)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926 /*
927 * A DMA error is sometimes expected. For example,
928 * if the tape is crossing a filemark during a
929 * READ command, it will issue an irq and position
930 * itself before the filemark, so that only a partial
931 * data transfer will occur (which causes the DMA
932 * error). In that case, we will later ask the tape
933 * how much bytes of the original request were
934 * actually transferred (we can't receive that
935 * information from the DMA engine on most chipsets).
936 */
937
938 /*
939 * On the contrary, a DMA error is never expected;
940 * it usually indicates a hardware error or abort.
941 * If the tape crosses a filemark during a READ
942 * command, it will issue an irq and position itself
943 * after the filemark (not before). Only a partial
944 * data transfer will occur, but no DMA error.
945 * (AS, 19 Apr 2001)
946 */
Borislav Petkov346331f2008-04-18 00:46:26 +0200947 pc->flags |= PC_FLAG_DMA_ERROR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948 } else {
Borislav Petkovd236d742008-04-18 00:46:27 +0200949 pc->xferred = pc->req_xfer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950 idetape_update_buffers(pc);
951 }
Borislav Petkov8004a8c2008-02-06 02:57:51 +0100952 debug_log(DBG_PROCS, "DMA finished\n");
953
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954 }
955
956 /* No more interrupts */
Bartlomiej Zolnierkiewicz22c525b2008-01-25 22:17:11 +0100957 if ((stat & DRQ_STAT) == 0) {
Borislav Petkov8004a8c2008-02-06 02:57:51 +0100958 debug_log(DBG_SENSE, "Packet command completed, %d bytes"
Borislav Petkovd236d742008-04-18 00:46:27 +0200959 " transferred\n", pc->xferred);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960
Borislav Petkov346331f2008-04-18 00:46:26 +0200961 pc->flags &= ~PC_FLAG_DMA_IN_PROGRESS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962 local_irq_enable();
963
964#if SIMULATE_ERRORS
Borislav Petkov90699ce2008-02-02 19:56:50 +0100965 if ((pc->c[0] == WRITE_6 || pc->c[0] == READ_6) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966 (++error_sim_count % 100) == 0) {
967 printk(KERN_INFO "ide-tape: %s: simulating error\n",
968 tape->name);
Bartlomiej Zolnierkiewicz22c525b2008-01-25 22:17:11 +0100969 stat |= ERR_STAT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970 }
971#endif
Borislav Petkov90699ce2008-02-02 19:56:50 +0100972 if ((stat & ERR_STAT) && pc->c[0] == REQUEST_SENSE)
Bartlomiej Zolnierkiewicz22c525b2008-01-25 22:17:11 +0100973 stat &= ~ERR_STAT;
Borislav Petkov346331f2008-04-18 00:46:26 +0200974 if ((stat & ERR_STAT) || (pc->flags & PC_FLAG_DMA_ERROR)) {
Bartlomiej Zolnierkiewicz22c525b2008-01-25 22:17:11 +0100975 /* Error detected */
Borislav Petkov8004a8c2008-02-06 02:57:51 +0100976 debug_log(DBG_ERR, "%s: I/O error\n", tape->name);
977
Borislav Petkov90699ce2008-02-02 19:56:50 +0100978 if (pc->c[0] == REQUEST_SENSE) {
Borislav Petkova1efc852008-02-06 02:57:52 +0100979 printk(KERN_ERR "ide-tape: I/O error in request"
980 " sense command\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981 return ide_do_reset(drive);
982 }
Borislav Petkov8004a8c2008-02-06 02:57:51 +0100983 debug_log(DBG_ERR, "[cmd %x]: check condition\n",
984 pc->c[0]);
985
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986 /* Retry operation */
987 return idetape_retry_pc(drive);
988 }
989 pc->error = 0;
Borislav Petkov346331f2008-04-18 00:46:26 +0200990 if ((pc->flags & PC_FLAG_WAIT_FOR_DSC) &&
Bartlomiej Zolnierkiewicz22c525b2008-01-25 22:17:11 +0100991 (stat & SEEK_STAT) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992 /* Media access command */
993 tape->dsc_polling_start = jiffies;
Borislav Petkov54bb2072008-02-06 02:57:52 +0100994 tape->dsc_poll_freq = IDETAPE_DSC_MA_FAST;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995 tape->dsc_timeout = jiffies + IDETAPE_DSC_MA_TIMEOUT;
996 /* Allow ide.c to handle other requests */
997 idetape_postpone_request(drive);
998 return ide_stopped;
999 }
1000 if (tape->failed_pc == pc)
1001 tape->failed_pc = NULL;
1002 /* Command finished - Call the callback function */
Borislav Petkovd236d742008-04-18 00:46:27 +02001003 return pc->idetape_callback(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004 }
Borislav Petkov346331f2008-04-18 00:46:26 +02001005
1006 if (pc->flags & PC_FLAG_DMA_IN_PROGRESS) {
1007 pc->flags &= ~PC_FLAG_DMA_IN_PROGRESS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008 printk(KERN_ERR "ide-tape: The tape wants to issue more "
1009 "interrupts in DMA mode\n");
1010 printk(KERN_ERR "ide-tape: DMA disabled, reverting to PIO\n");
Bartlomiej Zolnierkiewicz7469aaf2007-02-17 02:40:26 +01001011 ide_dma_off(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012 return ide_do_reset(drive);
1013 }
1014 /* Get the number of bytes to transfer on this interrupt. */
Bartlomiej Zolnierkiewicz23579a22008-04-18 00:46:26 +02001015 bcount = (hwif->INB(hwif->io_ports[IDE_BCOUNTH_OFFSET]) << 8) |
1016 hwif->INB(hwif->io_ports[IDE_BCOUNTL_OFFSET]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017
Bartlomiej Zolnierkiewicz23579a22008-04-18 00:46:26 +02001018 ireason = hwif->INB(hwif->io_ports[IDE_IREASON_OFFSET]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019
Bartlomiej Zolnierkiewicz8e7657a2008-01-25 22:17:12 +01001020 if (ireason & CD) {
Borislav Petkova1efc852008-02-06 02:57:52 +01001021 printk(KERN_ERR "ide-tape: CoD != 0 in %s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022 return ide_do_reset(drive);
1023 }
Borislav Petkov346331f2008-04-18 00:46:26 +02001024 if (((ireason & IO) == IO) == !!(pc->flags & PC_FLAG_WRITING)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025 /* Hopefully, we will never get here */
1026 printk(KERN_ERR "ide-tape: We wanted to %s, ",
Bartlomiej Zolnierkiewicz8e7657a2008-01-25 22:17:12 +01001027 (ireason & IO) ? "Write" : "Read");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028 printk(KERN_ERR "ide-tape: but the tape wants us to %s !\n",
Bartlomiej Zolnierkiewicz8e7657a2008-01-25 22:17:12 +01001029 (ireason & IO) ? "Read" : "Write");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030 return ide_do_reset(drive);
1031 }
Borislav Petkov346331f2008-04-18 00:46:26 +02001032 if (!(pc->flags & PC_FLAG_WRITING)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033 /* Reading - Check that we have enough space */
Borislav Petkovd236d742008-04-18 00:46:27 +02001034 temp = pc->xferred + bcount;
1035 if (temp > pc->req_xfer) {
1036 if (temp > pc->buf_size) {
Borislav Petkova1efc852008-02-06 02:57:52 +01001037 printk(KERN_ERR "ide-tape: The tape wants to "
1038 "send us more data than expected "
1039 "- discarding data\n");
Bartlomiej Zolnierkiewicz7616c0a2008-04-18 00:46:26 +02001040 ide_atapi_discard_data(drive, bcount);
Borislav Petkova1efc852008-02-06 02:57:52 +01001041 ide_set_handler(drive, &idetape_pc_intr,
1042 IDETAPE_WAIT_CMD, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043 return ide_started;
1044 }
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001045 debug_log(DBG_SENSE, "The tape wants to send us more "
1046 "data than expected - allowing transfer\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047 }
Borislav Petkova1efc852008-02-06 02:57:52 +01001048 iobuf = &idetape_input_buffers;
1049 xferfunc = hwif->atapi_input_bytes;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050 } else {
Borislav Petkova1efc852008-02-06 02:57:52 +01001051 iobuf = &idetape_output_buffers;
1052 xferfunc = hwif->atapi_output_bytes;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053 }
Borislav Petkova1efc852008-02-06 02:57:52 +01001054
1055 if (pc->bh)
1056 iobuf(drive, pc, bcount);
1057 else
Borislav Petkovd236d742008-04-18 00:46:27 +02001058 xferfunc(drive, pc->cur_pos, bcount);
Borislav Petkova1efc852008-02-06 02:57:52 +01001059
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060 /* Update the current position */
Borislav Petkovd236d742008-04-18 00:46:27 +02001061 pc->xferred += bcount;
1062 pc->cur_pos += bcount;
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001063
1064 debug_log(DBG_SENSE, "[cmd %x] transferred %d bytes on that intr.\n",
1065 pc->c[0], bcount);
1066
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067 /* And set the interrupt handler again */
1068 ide_set_handler(drive, &idetape_pc_intr, IDETAPE_WAIT_CMD, NULL);
1069 return ide_started;
1070}
1071
1072/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001073 * Packet Command Interface
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001075 * The current Packet Command is available in tape->pc, and will not change
1076 * until we finish handling it. Each packet command is associated with a
1077 * callback function that will be called when the command is finished.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001079 * The handling will be done in three stages:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001081 * 1. idetape_issue_pc will send the packet command to the drive, and will set
1082 * the interrupt handler to idetape_pc_intr.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001084 * 2. On each interrupt, idetape_pc_intr will be called. This step will be
1085 * repeated until the device signals us that no more interrupts will be issued.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001087 * 3. ATAPI Tape media access commands have immediate status with a delayed
1088 * process. In case of a successful initiation of a media access packet command,
1089 * the DSC bit will be set when the actual execution of the command is finished.
1090 * Since the tape drive will not issue an interrupt, we have to poll for this
1091 * event. In this case, we define the request as "low priority request" by
1092 * setting rq_status to IDETAPE_RQ_POSTPONED, set a timer to poll for DSC and
1093 * exit the driver.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001095 * ide.c will then give higher priority to requests which originate from the
1096 * other device, until will change rq_status to RQ_ACTIVE.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001098 * 4. When the packet command is finished, it will be checked for errors.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001100 * 5. In case an error was found, we queue a request sense packet command in
1101 * front of the request queue and retry the operation up to
1102 * IDETAPE_MAX_PC_RETRIES times.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001104 * 6. In case no error was found, or we decided to give up and not to retry
1105 * again, the callback function will be called and then we will handle the next
1106 * request.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107 */
1108static ide_startstop_t idetape_transfer_pc(ide_drive_t *drive)
1109{
1110 ide_hwif_t *hwif = drive->hwif;
1111 idetape_tape_t *tape = drive->driver_data;
Borislav Petkovd236d742008-04-18 00:46:27 +02001112 struct ide_atapi_pc *pc = tape->pc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113 int retries = 100;
1114 ide_startstop_t startstop;
Bartlomiej Zolnierkiewicz8e7657a2008-01-25 22:17:12 +01001115 u8 ireason;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001117 if (ide_wait_stat(&startstop, drive, DRQ_STAT, BUSY_STAT, WAIT_READY)) {
1118 printk(KERN_ERR "ide-tape: Strange, packet command initiated "
1119 "yet DRQ isn't asserted\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120 return startstop;
1121 }
Bartlomiej Zolnierkiewicz23579a22008-04-18 00:46:26 +02001122 ireason = hwif->INB(hwif->io_ports[IDE_IREASON_OFFSET]);
Bartlomiej Zolnierkiewicz8e7657a2008-01-25 22:17:12 +01001123 while (retries-- && ((ireason & CD) == 0 || (ireason & IO))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124 printk(KERN_ERR "ide-tape: (IO,CoD != (0,1) while issuing "
1125 "a packet command, retrying\n");
1126 udelay(100);
Bartlomiej Zolnierkiewicz23579a22008-04-18 00:46:26 +02001127 ireason = hwif->INB(hwif->io_ports[IDE_IREASON_OFFSET]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128 if (retries == 0) {
1129 printk(KERN_ERR "ide-tape: (IO,CoD != (0,1) while "
1130 "issuing a packet command, ignoring\n");
Bartlomiej Zolnierkiewicz8e7657a2008-01-25 22:17:12 +01001131 ireason |= CD;
1132 ireason &= ~IO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133 }
1134 }
Bartlomiej Zolnierkiewicz8e7657a2008-01-25 22:17:12 +01001135 if ((ireason & CD) == 0 || (ireason & IO)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136 printk(KERN_ERR "ide-tape: (IO,CoD) != (0,1) while issuing "
1137 "a packet command\n");
1138 return ide_do_reset(drive);
1139 }
1140 /* Set the interrupt routine */
1141 ide_set_handler(drive, &idetape_pc_intr, IDETAPE_WAIT_CMD, NULL);
1142#ifdef CONFIG_BLK_DEV_IDEDMA
1143 /* Begin DMA, if necessary */
Borislav Petkov346331f2008-04-18 00:46:26 +02001144 if (pc->flags & PC_FLAG_DMA_IN_PROGRESS)
Bartlomiej Zolnierkiewicz5e37bdc2008-04-26 22:25:24 +02001145 hwif->dma_ops->dma_start(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146#endif
1147 /* Send the actual packet */
1148 HWIF(drive)->atapi_output_bytes(drive, pc->c, 12);
1149 return ide_started;
1150}
1151
Borislav Petkovd236d742008-04-18 00:46:27 +02001152static ide_startstop_t idetape_issue_pc(ide_drive_t *drive,
1153 struct ide_atapi_pc *pc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154{
1155 ide_hwif_t *hwif = drive->hwif;
1156 idetape_tape_t *tape = drive->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157 int dma_ok = 0;
Bartlomiej Zolnierkiewicz790d1232008-01-25 22:17:12 +01001158 u16 bcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159
Borislav Petkov90699ce2008-02-02 19:56:50 +01001160 if (tape->pc->c[0] == REQUEST_SENSE &&
1161 pc->c[0] == REQUEST_SENSE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162 printk(KERN_ERR "ide-tape: possible ide-tape.c bug - "
1163 "Two request sense in serial were issued\n");
1164 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165
Borislav Petkov90699ce2008-02-02 19:56:50 +01001166 if (tape->failed_pc == NULL && pc->c[0] != REQUEST_SENSE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167 tape->failed_pc = pc;
1168 /* Set the current packet command */
1169 tape->pc = pc;
1170
1171 if (pc->retries > IDETAPE_MAX_PC_RETRIES ||
Borislav Petkov346331f2008-04-18 00:46:26 +02001172 (pc->flags & PC_FLAG_ABORT)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173 /*
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001174 * We will "abort" retrying a packet command in case legitimate
1175 * error code was received (crossing a filemark, or end of the
1176 * media, for example).
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177 */
Borislav Petkov346331f2008-04-18 00:46:26 +02001178 if (!(pc->flags & PC_FLAG_ABORT)) {
Borislav Petkov90699ce2008-02-02 19:56:50 +01001179 if (!(pc->c[0] == TEST_UNIT_READY &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180 tape->sense_key == 2 && tape->asc == 4 &&
1181 (tape->ascq == 1 || tape->ascq == 8))) {
1182 printk(KERN_ERR "ide-tape: %s: I/O error, "
1183 "pc = %2x, key = %2x, "
1184 "asc = %2x, ascq = %2x\n",
1185 tape->name, pc->c[0],
1186 tape->sense_key, tape->asc,
1187 tape->ascq);
1188 }
1189 /* Giving up */
1190 pc->error = IDETAPE_ERROR_GENERAL;
1191 }
1192 tape->failed_pc = NULL;
Borislav Petkovd236d742008-04-18 00:46:27 +02001193 return pc->idetape_callback(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194 }
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001195 debug_log(DBG_SENSE, "Retry #%d, cmd = %02X\n", pc->retries, pc->c[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196
1197 pc->retries++;
1198 /* We haven't transferred any data yet */
Borislav Petkovd236d742008-04-18 00:46:27 +02001199 pc->xferred = 0;
1200 pc->cur_pos = pc->buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201 /* Request to transfer the entire buffer at once */
Borislav Petkovd236d742008-04-18 00:46:27 +02001202 bcount = pc->req_xfer;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203
Borislav Petkov346331f2008-04-18 00:46:26 +02001204 if (pc->flags & PC_FLAG_DMA_ERROR) {
1205 pc->flags &= ~PC_FLAG_DMA_ERROR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206 printk(KERN_WARNING "ide-tape: DMA disabled, "
1207 "reverting to PIO\n");
Bartlomiej Zolnierkiewicz7469aaf2007-02-17 02:40:26 +01001208 ide_dma_off(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209 }
Borislav Petkov346331f2008-04-18 00:46:26 +02001210 if ((pc->flags & PC_FLAG_DMA_RECOMMENDED) && drive->using_dma)
Bartlomiej Zolnierkiewicz5e37bdc2008-04-26 22:25:24 +02001211 dma_ok = !hwif->dma_ops->dma_setup(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212
Bartlomiej Zolnierkiewicz2fc57382008-01-25 22:17:13 +01001213 ide_pktcmd_tf_load(drive, IDE_TFLAG_NO_SELECT_MASK |
1214 IDE_TFLAG_OUT_DEVICE, bcount, dma_ok);
1215
Borislav Petkov346331f2008-04-18 00:46:26 +02001216 if (dma_ok)
1217 /* Will begin DMA later */
1218 pc->flags |= PC_FLAG_DMA_IN_PROGRESS;
1219 if (test_bit(IDETAPE_FLAG_DRQ_INTERRUPT, &tape->flags)) {
Bartlomiej Zolnierkiewiczc1c9dbc2008-02-02 19:56:44 +01001220 ide_execute_command(drive, WIN_PACKETCMD, &idetape_transfer_pc,
1221 IDETAPE_WAIT_CMD, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222 return ide_started;
1223 } else {
Bartlomiej Zolnierkiewicz23579a22008-04-18 00:46:26 +02001224 hwif->OUTB(WIN_PACKETCMD, hwif->io_ports[IDE_COMMAND_OFFSET]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225 return idetape_transfer_pc(drive);
1226 }
1227}
1228
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001229static ide_startstop_t idetape_pc_callback(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230{
1231 idetape_tape_t *tape = drive->driver_data;
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001232
1233 debug_log(DBG_PROCS, "Enter %s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234
1235 idetape_end_request(drive, tape->pc->error ? 0 : 1, 0);
1236 return ide_stopped;
1237}
1238
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001239/* A mode sense command is used to "sense" tape parameters. */
Borislav Petkovd236d742008-04-18 00:46:27 +02001240static void idetape_create_mode_sense_cmd(struct ide_atapi_pc *pc, u8 page_code)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241{
1242 idetape_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +01001243 pc->c[0] = MODE_SENSE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244 if (page_code != IDETAPE_BLOCK_DESCRIPTOR)
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001245 /* DBD = 1 - Don't return block descriptors */
1246 pc->c[1] = 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247 pc->c[2] = page_code;
1248 /*
1249 * Changed pc->c[3] to 0 (255 will at best return unused info).
1250 *
1251 * For SCSI this byte is defined as subpage instead of high byte
1252 * of length and some IDE drives seem to interpret it this way
1253 * and return an error when 255 is used.
1254 */
1255 pc->c[3] = 0;
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001256 /* We will just discard data in that case */
1257 pc->c[4] = 255;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258 if (page_code == IDETAPE_BLOCK_DESCRIPTOR)
Borislav Petkovd236d742008-04-18 00:46:27 +02001259 pc->req_xfer = 12;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260 else if (page_code == IDETAPE_CAPABILITIES_PAGE)
Borislav Petkovd236d742008-04-18 00:46:27 +02001261 pc->req_xfer = 24;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262 else
Borislav Petkovd236d742008-04-18 00:46:27 +02001263 pc->req_xfer = 50;
1264 pc->idetape_callback = &idetape_pc_callback;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265}
1266
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001267static ide_startstop_t idetape_media_access_finished(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268{
1269 idetape_tape_t *tape = drive->driver_data;
Borislav Petkovd236d742008-04-18 00:46:27 +02001270 struct ide_atapi_pc *pc = tape->pc;
Bartlomiej Zolnierkiewicz22c525b2008-01-25 22:17:11 +01001271 u8 stat;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272
Bartlomiej Zolnierkiewiczc47137a2008-02-06 02:57:51 +01001273 stat = ide_read_status(drive);
1274
Bartlomiej Zolnierkiewicz22c525b2008-01-25 22:17:11 +01001275 if (stat & SEEK_STAT) {
1276 if (stat & ERR_STAT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277 /* Error detected */
Borislav Petkov90699ce2008-02-02 19:56:50 +01001278 if (pc->c[0] != TEST_UNIT_READY)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279 printk(KERN_ERR "ide-tape: %s: I/O error, ",
1280 tape->name);
1281 /* Retry operation */
1282 return idetape_retry_pc(drive);
1283 }
1284 pc->error = 0;
1285 if (tape->failed_pc == pc)
1286 tape->failed_pc = NULL;
1287 } else {
1288 pc->error = IDETAPE_ERROR_GENERAL;
1289 tape->failed_pc = NULL;
1290 }
Borislav Petkovd236d742008-04-18 00:46:27 +02001291 return pc->idetape_callback(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292}
1293
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001294static ide_startstop_t idetape_rw_callback(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295{
1296 idetape_tape_t *tape = drive->driver_data;
1297 struct request *rq = HWGROUP(drive)->rq;
Borislav Petkovd236d742008-04-18 00:46:27 +02001298 int blocks = tape->pc->xferred / tape->blk_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299
Borislav Petkov54bb2072008-02-06 02:57:52 +01001300 tape->avg_size += blocks * tape->blk_size;
1301 tape->insert_size += blocks * tape->blk_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302 if (tape->insert_size > 1024 * 1024)
1303 tape->measure_insert_time = 1;
1304 if (tape->measure_insert_time) {
1305 tape->measure_insert_time = 0;
1306 tape->insert_time = jiffies;
1307 tape->insert_size = 0;
1308 }
1309 if (time_after(jiffies, tape->insert_time))
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001310 tape->insert_speed = tape->insert_size / 1024 * HZ /
1311 (jiffies - tape->insert_time);
Marcelo Feitoza Parisi9bae1ff2006-03-28 01:56:46 -08001312 if (time_after_eq(jiffies, tape->avg_time + HZ)) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001313 tape->avg_speed = tape->avg_size * HZ /
1314 (jiffies - tape->avg_time) / 1024;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315 tape->avg_size = 0;
1316 tape->avg_time = jiffies;
1317 }
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001318 debug_log(DBG_PROCS, "Enter %s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319
Borislav Petkov54bb2072008-02-06 02:57:52 +01001320 tape->first_frame += blocks;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321 rq->current_nr_sectors -= blocks;
1322
1323 if (!tape->pc->error)
1324 idetape_end_request(drive, 1, 0);
1325 else
1326 idetape_end_request(drive, tape->pc->error, 0);
1327 return ide_stopped;
1328}
1329
Borislav Petkovd236d742008-04-18 00:46:27 +02001330static void idetape_create_read_cmd(idetape_tape_t *tape,
1331 struct ide_atapi_pc *pc,
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001332 unsigned int length, struct idetape_bh *bh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333{
1334 idetape_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +01001335 pc->c[0] = READ_6;
Borislav Petkov860ff5e2008-02-02 19:56:50 +01001336 put_unaligned(cpu_to_be32(length), (unsigned int *) &pc->c[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337 pc->c[1] = 1;
Borislav Petkovd236d742008-04-18 00:46:27 +02001338 pc->idetape_callback = &idetape_rw_callback;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339 pc->bh = bh;
1340 atomic_set(&bh->b_count, 0);
Borislav Petkovd236d742008-04-18 00:46:27 +02001341 pc->buf = NULL;
1342 pc->buf_size = length * tape->blk_size;
1343 pc->req_xfer = pc->buf_size;
1344 if (pc->req_xfer == tape->stage_size)
Borislav Petkov346331f2008-04-18 00:46:26 +02001345 pc->flags |= PC_FLAG_DMA_RECOMMENDED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346}
1347
Borislav Petkovd236d742008-04-18 00:46:27 +02001348static void idetape_create_write_cmd(idetape_tape_t *tape,
1349 struct ide_atapi_pc *pc,
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001350 unsigned int length, struct idetape_bh *bh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351{
1352 idetape_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +01001353 pc->c[0] = WRITE_6;
Borislav Petkov860ff5e2008-02-02 19:56:50 +01001354 put_unaligned(cpu_to_be32(length), (unsigned int *) &pc->c[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355 pc->c[1] = 1;
Borislav Petkovd236d742008-04-18 00:46:27 +02001356 pc->idetape_callback = &idetape_rw_callback;
Borislav Petkov346331f2008-04-18 00:46:26 +02001357 pc->flags |= PC_FLAG_WRITING;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358 pc->bh = bh;
1359 pc->b_data = bh->b_data;
1360 pc->b_count = atomic_read(&bh->b_count);
Borislav Petkovd236d742008-04-18 00:46:27 +02001361 pc->buf = NULL;
1362 pc->buf_size = length * tape->blk_size;
1363 pc->req_xfer = pc->buf_size;
1364 if (pc->req_xfer == tape->stage_size)
Borislav Petkov346331f2008-04-18 00:46:26 +02001365 pc->flags |= PC_FLAG_DMA_RECOMMENDED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366}
1367
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368static ide_startstop_t idetape_do_request(ide_drive_t *drive,
1369 struct request *rq, sector_t block)
1370{
1371 idetape_tape_t *tape = drive->driver_data;
Borislav Petkovd236d742008-04-18 00:46:27 +02001372 struct ide_atapi_pc *pc = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373 struct request *postponed_rq = tape->postponed_rq;
Bartlomiej Zolnierkiewicz22c525b2008-01-25 22:17:11 +01001374 u8 stat;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001376 debug_log(DBG_SENSE, "sector: %ld, nr_sectors: %ld,"
1377 " current_nr_sectors: %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378 rq->sector, rq->nr_sectors, rq->current_nr_sectors);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379
Jens Axboe4aff5e22006-08-10 08:44:47 +02001380 if (!blk_special_request(rq)) {
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001381 /* We do not support buffer cache originated requests. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382 printk(KERN_NOTICE "ide-tape: %s: Unsupported request in "
Jens Axboe4aff5e22006-08-10 08:44:47 +02001383 "request queue (%d)\n", drive->name, rq->cmd_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384 ide_end_request(drive, 0, 0);
1385 return ide_stopped;
1386 }
1387
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001388 /* Retry a failed packet command */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001389 if (tape->failed_pc && tape->pc->c[0] == REQUEST_SENSE)
Borislav Petkov8d06bfa2008-02-06 02:57:53 +01001390 return idetape_issue_pc(drive, tape->failed_pc);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001391
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392 if (postponed_rq != NULL)
1393 if (rq != postponed_rq) {
1394 printk(KERN_ERR "ide-tape: ide-tape.c bug - "
1395 "Two DSC requests were queued\n");
1396 idetape_end_request(drive, 0, 0);
1397 return ide_stopped;
1398 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399
1400 tape->postponed_rq = NULL;
1401
1402 /*
1403 * If the tape is still busy, postpone our request and service
1404 * the other device meanwhile.
1405 */
Bartlomiej Zolnierkiewiczc47137a2008-02-06 02:57:51 +01001406 stat = ide_read_status(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407
1408 if (!drive->dsc_overlap && !(rq->cmd[0] & REQ_IDETAPE_PC2))
Borislav Petkov346331f2008-04-18 00:46:26 +02001409 set_bit(IDETAPE_FLAG_IGNORE_DSC, &tape->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410
1411 if (drive->post_reset == 1) {
Borislav Petkov346331f2008-04-18 00:46:26 +02001412 set_bit(IDETAPE_FLAG_IGNORE_DSC, &tape->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413 drive->post_reset = 0;
1414 }
1415
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416 if (time_after(jiffies, tape->insert_time))
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001417 tape->insert_speed = tape->insert_size / 1024 * HZ /
1418 (jiffies - tape->insert_time);
Borislav Petkov346331f2008-04-18 00:46:26 +02001419 if (!test_and_clear_bit(IDETAPE_FLAG_IGNORE_DSC, &tape->flags) &&
Bartlomiej Zolnierkiewicz22c525b2008-01-25 22:17:11 +01001420 (stat & SEEK_STAT) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421 if (postponed_rq == NULL) {
1422 tape->dsc_polling_start = jiffies;
Borislav Petkov54bb2072008-02-06 02:57:52 +01001423 tape->dsc_poll_freq = tape->best_dsc_rw_freq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424 tape->dsc_timeout = jiffies + IDETAPE_DSC_RW_TIMEOUT;
1425 } else if (time_after(jiffies, tape->dsc_timeout)) {
1426 printk(KERN_ERR "ide-tape: %s: DSC timeout\n",
1427 tape->name);
1428 if (rq->cmd[0] & REQ_IDETAPE_PC2) {
1429 idetape_media_access_finished(drive);
1430 return ide_stopped;
1431 } else {
1432 return ide_do_reset(drive);
1433 }
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001434 } else if (time_after(jiffies,
1435 tape->dsc_polling_start +
1436 IDETAPE_DSC_MA_THRESHOLD))
Borislav Petkov54bb2072008-02-06 02:57:52 +01001437 tape->dsc_poll_freq = IDETAPE_DSC_MA_SLOW;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438 idetape_postpone_request(drive);
1439 return ide_stopped;
1440 }
1441 if (rq->cmd[0] & REQ_IDETAPE_READ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442 tape->postpone_cnt = 0;
1443 pc = idetape_next_pc_storage(drive);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001444 idetape_create_read_cmd(tape, pc, rq->current_nr_sectors,
1445 (struct idetape_bh *)rq->special);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446 goto out;
1447 }
1448 if (rq->cmd[0] & REQ_IDETAPE_WRITE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449 tape->postpone_cnt = 0;
1450 pc = idetape_next_pc_storage(drive);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001451 idetape_create_write_cmd(tape, pc, rq->current_nr_sectors,
1452 (struct idetape_bh *)rq->special);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453 goto out;
1454 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455 if (rq->cmd[0] & REQ_IDETAPE_PC1) {
Borislav Petkovd236d742008-04-18 00:46:27 +02001456 pc = (struct ide_atapi_pc *) rq->buffer;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457 rq->cmd[0] &= ~(REQ_IDETAPE_PC1);
1458 rq->cmd[0] |= REQ_IDETAPE_PC2;
1459 goto out;
1460 }
1461 if (rq->cmd[0] & REQ_IDETAPE_PC2) {
1462 idetape_media_access_finished(drive);
1463 return ide_stopped;
1464 }
1465 BUG();
1466out:
Borislav Petkov8d06bfa2008-02-06 02:57:53 +01001467 return idetape_issue_pc(drive, pc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468}
1469
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001470/* Pipeline related functions */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001471
1472/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001473 * The function below uses __get_free_page to allocate a pipeline stage, along
1474 * with all the necessary small buffers which together make a buffer of size
1475 * tape->stage_size (or a bit more). We attempt to combine sequential pages as
1476 * much as possible.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001478 * It returns a pointer to the new allocated stage, or NULL if we can't (or
1479 * don't want to) allocate a stage.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001481 * Pipeline stages are optional and are used to increase performance. If we
1482 * can't allocate them, we'll manage without them.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483 */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001484static idetape_stage_t *__idetape_kmalloc_stage(idetape_tape_t *tape, int full,
1485 int clear)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486{
1487 idetape_stage_t *stage;
1488 struct idetape_bh *prev_bh, *bh;
1489 int pages = tape->pages_per_stage;
1490 char *b_data = NULL;
1491
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001492 stage = kmalloc(sizeof(idetape_stage_t), GFP_KERNEL);
1493 if (!stage)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494 return NULL;
1495 stage->next = NULL;
1496
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001497 stage->bh = kmalloc(sizeof(struct idetape_bh), GFP_KERNEL);
1498 bh = stage->bh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499 if (bh == NULL)
1500 goto abort;
1501 bh->b_reqnext = NULL;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001502 bh->b_data = (char *) __get_free_page(GFP_KERNEL);
1503 if (!bh->b_data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504 goto abort;
1505 if (clear)
1506 memset(bh->b_data, 0, PAGE_SIZE);
1507 bh->b_size = PAGE_SIZE;
1508 atomic_set(&bh->b_count, full ? bh->b_size : 0);
1509
1510 while (--pages) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001511 b_data = (char *) __get_free_page(GFP_KERNEL);
1512 if (!b_data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513 goto abort;
1514 if (clear)
1515 memset(b_data, 0, PAGE_SIZE);
1516 if (bh->b_data == b_data + PAGE_SIZE) {
1517 bh->b_size += PAGE_SIZE;
1518 bh->b_data -= PAGE_SIZE;
1519 if (full)
1520 atomic_add(PAGE_SIZE, &bh->b_count);
1521 continue;
1522 }
1523 if (b_data == bh->b_data + bh->b_size) {
1524 bh->b_size += PAGE_SIZE;
1525 if (full)
1526 atomic_add(PAGE_SIZE, &bh->b_count);
1527 continue;
1528 }
1529 prev_bh = bh;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001530 bh = kmalloc(sizeof(struct idetape_bh), GFP_KERNEL);
1531 if (!bh) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532 free_page((unsigned long) b_data);
1533 goto abort;
1534 }
1535 bh->b_reqnext = NULL;
1536 bh->b_data = b_data;
1537 bh->b_size = PAGE_SIZE;
1538 atomic_set(&bh->b_count, full ? bh->b_size : 0);
1539 prev_bh->b_reqnext = bh;
1540 }
1541 bh->b_size -= tape->excess_bh_size;
1542 if (full)
1543 atomic_sub(tape->excess_bh_size, &bh->b_count);
1544 return stage;
1545abort:
1546 __idetape_kfree_stage(stage);
1547 return NULL;
1548}
1549
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001550static int idetape_copy_stage_from_user(idetape_tape_t *tape,
Borislav Petkov8646c882008-04-27 15:38:26 +02001551 const char __user *buf, int n)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001552{
1553 struct idetape_bh *bh = tape->bh;
1554 int count;
Daniel Walkerdcd96372006-06-25 05:47:37 -07001555 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556
1557 while (n) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558 if (bh == NULL) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001559 printk(KERN_ERR "ide-tape: bh == NULL in %s\n",
1560 __func__);
Daniel Walkerdcd96372006-06-25 05:47:37 -07001561 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001562 }
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001563 count = min((unsigned int)
1564 (bh->b_size - atomic_read(&bh->b_count)),
1565 (unsigned int)n);
1566 if (copy_from_user(bh->b_data + atomic_read(&bh->b_count), buf,
1567 count))
Daniel Walkerdcd96372006-06-25 05:47:37 -07001568 ret = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569 n -= count;
1570 atomic_add(count, &bh->b_count);
1571 buf += count;
1572 if (atomic_read(&bh->b_count) == bh->b_size) {
1573 bh = bh->b_reqnext;
1574 if (bh)
1575 atomic_set(&bh->b_count, 0);
1576 }
1577 }
1578 tape->bh = bh;
Daniel Walkerdcd96372006-06-25 05:47:37 -07001579 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001580}
1581
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001582static int idetape_copy_stage_to_user(idetape_tape_t *tape, char __user *buf,
Borislav Petkov99d74e62008-04-27 15:38:25 +02001583 int n)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584{
1585 struct idetape_bh *bh = tape->bh;
1586 int count;
Daniel Walkerdcd96372006-06-25 05:47:37 -07001587 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001588
1589 while (n) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590 if (bh == NULL) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001591 printk(KERN_ERR "ide-tape: bh == NULL in %s\n",
1592 __func__);
Daniel Walkerdcd96372006-06-25 05:47:37 -07001593 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001595 count = min(tape->b_count, n);
Daniel Walkerdcd96372006-06-25 05:47:37 -07001596 if (copy_to_user(buf, tape->b_data, count))
1597 ret = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001598 n -= count;
1599 tape->b_data += count;
1600 tape->b_count -= count;
1601 buf += count;
1602 if (!tape->b_count) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001603 bh = bh->b_reqnext;
1604 tape->bh = bh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001605 if (bh) {
1606 tape->b_data = bh->b_data;
1607 tape->b_count = atomic_read(&bh->b_count);
1608 }
1609 }
1610 }
Daniel Walkerdcd96372006-06-25 05:47:37 -07001611 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001612}
1613
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001614static void idetape_init_merge_stage(idetape_tape_t *tape)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001615{
1616 struct idetape_bh *bh = tape->merge_stage->bh;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001617
Linus Torvalds1da177e2005-04-16 15:20:36 -07001618 tape->bh = bh;
Borislav Petkov54abf372008-02-06 02:57:52 +01001619 if (tape->chrdev_dir == IDETAPE_DIR_WRITE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001620 atomic_set(&bh->b_count, 0);
1621 else {
1622 tape->b_data = bh->b_data;
1623 tape->b_count = atomic_read(&bh->b_count);
1624 }
1625}
1626
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001627/* Install a completion in a pending request and sleep until it is serviced. The
1628 * caller should ensure that the request will not be serviced before we install
1629 * the completion (usually by disabling interrupts).
Linus Torvalds1da177e2005-04-16 15:20:36 -07001630 */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001631static void idetape_wait_for_request(ide_drive_t *drive, struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632{
Peter Zijlstra6e9a4732006-09-30 23:28:10 -07001633 DECLARE_COMPLETION_ONSTACK(wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634 idetape_tape_t *tape = drive->driver_data;
1635
Jens Axboe4aff5e22006-08-10 08:44:47 +02001636 if (rq == NULL || !blk_special_request(rq)) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001637 printk(KERN_ERR "ide-tape: bug: Trying to sleep on non-valid"
1638 " request\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001639 return;
1640 }
Jens Axboec00895a2006-09-30 20:29:12 +02001641 rq->end_io_data = &wait;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642 rq->end_io = blk_end_sync_rq;
Borislav Petkov54bb2072008-02-06 02:57:52 +01001643 spin_unlock_irq(&tape->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644 wait_for_completion(&wait);
1645 /* The stage and its struct request have been deallocated */
Borislav Petkov54bb2072008-02-06 02:57:52 +01001646 spin_lock_irq(&tape->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001647}
1648
Borislav Petkova2f5b7f2008-02-06 02:57:51 +01001649static ide_startstop_t idetape_read_position_callback(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001650{
1651 idetape_tape_t *tape = drive->driver_data;
Borislav Petkovd236d742008-04-18 00:46:27 +02001652 u8 *readpos = tape->pc->buf;
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001653
1654 debug_log(DBG_PROCS, "Enter %s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001655
1656 if (!tape->pc->error) {
Borislav Petkova2f5b7f2008-02-06 02:57:51 +01001657 debug_log(DBG_SENSE, "BOP - %s\n",
1658 (readpos[0] & 0x80) ? "Yes" : "No");
1659 debug_log(DBG_SENSE, "EOP - %s\n",
1660 (readpos[0] & 0x40) ? "Yes" : "No");
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001661
Borislav Petkova2f5b7f2008-02-06 02:57:51 +01001662 if (readpos[0] & 0x4) {
1663 printk(KERN_INFO "ide-tape: Block location is unknown"
1664 "to the tape\n");
Borislav Petkov346331f2008-04-18 00:46:26 +02001665 clear_bit(IDETAPE_FLAG_ADDRESS_VALID, &tape->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001666 idetape_end_request(drive, 0, 0);
1667 } else {
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001668 debug_log(DBG_SENSE, "Block Location - %u\n",
Borislav Petkova2f5b7f2008-02-06 02:57:51 +01001669 be32_to_cpu(*(u32 *)&readpos[4]));
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001670
Borislav Petkova2f5b7f2008-02-06 02:57:51 +01001671 tape->partition = readpos[1];
Borislav Petkov54bb2072008-02-06 02:57:52 +01001672 tape->first_frame =
Borislav Petkova2f5b7f2008-02-06 02:57:51 +01001673 be32_to_cpu(*(u32 *)&readpos[4]);
Borislav Petkov346331f2008-04-18 00:46:26 +02001674 set_bit(IDETAPE_FLAG_ADDRESS_VALID, &tape->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675 idetape_end_request(drive, 1, 0);
1676 }
1677 } else {
1678 idetape_end_request(drive, 0, 0);
1679 }
1680 return ide_stopped;
1681}
1682
1683/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001684 * Write a filemark if write_filemark=1. Flush the device buffers without
1685 * writing a filemark otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001686 */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001687static void idetape_create_write_filemark_cmd(ide_drive_t *drive,
Borislav Petkovd236d742008-04-18 00:46:27 +02001688 struct ide_atapi_pc *pc, int write_filemark)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001689{
1690 idetape_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +01001691 pc->c[0] = WRITE_FILEMARKS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001692 pc->c[4] = write_filemark;
Borislav Petkov346331f2008-04-18 00:46:26 +02001693 pc->flags |= PC_FLAG_WAIT_FOR_DSC;
Borislav Petkovd236d742008-04-18 00:46:27 +02001694 pc->idetape_callback = &idetape_pc_callback;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001695}
1696
Borislav Petkovd236d742008-04-18 00:46:27 +02001697static void idetape_create_test_unit_ready_cmd(struct ide_atapi_pc *pc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001698{
1699 idetape_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +01001700 pc->c[0] = TEST_UNIT_READY;
Borislav Petkovd236d742008-04-18 00:46:27 +02001701 pc->idetape_callback = &idetape_pc_callback;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001702}
1703
1704/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001705 * We add a special packet command request to the tail of the request queue, and
1706 * wait for it to be serviced. This is not to be called from within the request
1707 * handling part of the driver! We allocate here data on the stack and it is
1708 * valid until the request is finished. This is not the case for the bottom part
1709 * of the driver, where we are always leaving the functions to wait for an
1710 * interrupt or a timer event.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001711 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001712 * From the bottom part of the driver, we should allocate safe memory using
1713 * idetape_next_pc_storage() and ide_tape_next_rq_storage(), and add the request
1714 * to the request list without waiting for it to be serviced! In that case, we
1715 * usually use idetape_queue_pc_head().
Linus Torvalds1da177e2005-04-16 15:20:36 -07001716 */
Borislav Petkovd236d742008-04-18 00:46:27 +02001717static int __idetape_queue_pc_tail(ide_drive_t *drive, struct ide_atapi_pc *pc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001718{
1719 struct ide_tape_obj *tape = drive->driver_data;
1720 struct request rq;
1721
1722 idetape_init_rq(&rq, REQ_IDETAPE_PC1);
1723 rq.buffer = (char *) pc;
1724 rq.rq_disk = tape->disk;
1725 return ide_do_drive_cmd(drive, &rq, ide_wait);
1726}
1727
Borislav Petkovd236d742008-04-18 00:46:27 +02001728static void idetape_create_load_unload_cmd(ide_drive_t *drive,
1729 struct ide_atapi_pc *pc, int cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001730{
1731 idetape_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +01001732 pc->c[0] = START_STOP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001733 pc->c[4] = cmd;
Borislav Petkov346331f2008-04-18 00:46:26 +02001734 pc->flags |= PC_FLAG_WAIT_FOR_DSC;
Borislav Petkovd236d742008-04-18 00:46:27 +02001735 pc->idetape_callback = &idetape_pc_callback;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001736}
1737
1738static int idetape_wait_ready(ide_drive_t *drive, unsigned long timeout)
1739{
1740 idetape_tape_t *tape = drive->driver_data;
Borislav Petkovd236d742008-04-18 00:46:27 +02001741 struct ide_atapi_pc pc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001742 int load_attempted = 0;
1743
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001744 /* Wait for the tape to become ready */
Borislav Petkov346331f2008-04-18 00:46:26 +02001745 set_bit(IDETAPE_FLAG_MEDIUM_PRESENT, &tape->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001746 timeout += jiffies;
1747 while (time_before(jiffies, timeout)) {
1748 idetape_create_test_unit_ready_cmd(&pc);
1749 if (!__idetape_queue_pc_tail(drive, &pc))
1750 return 0;
1751 if ((tape->sense_key == 2 && tape->asc == 4 && tape->ascq == 2)
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001752 || (tape->asc == 0x3A)) {
1753 /* no media */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001754 if (load_attempted)
1755 return -ENOMEDIUM;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001756 idetape_create_load_unload_cmd(drive, &pc,
1757 IDETAPE_LU_LOAD_MASK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001758 __idetape_queue_pc_tail(drive, &pc);
1759 load_attempted = 1;
1760 /* not about to be ready */
1761 } else if (!(tape->sense_key == 2 && tape->asc == 4 &&
1762 (tape->ascq == 1 || tape->ascq == 8)))
1763 return -EIO;
Nishanth Aravamudan80ce45f2005-09-10 00:27:08 -07001764 msleep(100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001765 }
1766 return -EIO;
1767}
1768
Borislav Petkovd236d742008-04-18 00:46:27 +02001769static int idetape_queue_pc_tail(ide_drive_t *drive, struct ide_atapi_pc *pc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001770{
1771 return __idetape_queue_pc_tail(drive, pc);
1772}
1773
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001774static int idetape_flush_tape_buffers(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001775{
Borislav Petkovd236d742008-04-18 00:46:27 +02001776 struct ide_atapi_pc pc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001777 int rc;
1778
1779 idetape_create_write_filemark_cmd(drive, &pc, 0);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001780 rc = idetape_queue_pc_tail(drive, &pc);
1781 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001782 return rc;
1783 idetape_wait_ready(drive, 60 * 5 * HZ);
1784 return 0;
1785}
1786
Borislav Petkovd236d742008-04-18 00:46:27 +02001787static void idetape_create_read_position_cmd(struct ide_atapi_pc *pc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788{
1789 idetape_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +01001790 pc->c[0] = READ_POSITION;
Borislav Petkovd236d742008-04-18 00:46:27 +02001791 pc->req_xfer = 20;
1792 pc->idetape_callback = &idetape_read_position_callback;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001793}
1794
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001795static int idetape_read_position(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001796{
1797 idetape_tape_t *tape = drive->driver_data;
Borislav Petkovd236d742008-04-18 00:46:27 +02001798 struct ide_atapi_pc pc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001799 int position;
1800
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001801 debug_log(DBG_PROCS, "Enter %s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001802
1803 idetape_create_read_position_cmd(&pc);
1804 if (idetape_queue_pc_tail(drive, &pc))
1805 return -1;
Borislav Petkov54bb2072008-02-06 02:57:52 +01001806 position = tape->first_frame;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807 return position;
1808}
1809
Borislav Petkovd236d742008-04-18 00:46:27 +02001810static void idetape_create_locate_cmd(ide_drive_t *drive,
1811 struct ide_atapi_pc *pc,
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001812 unsigned int block, u8 partition, int skip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001813{
1814 idetape_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +01001815 pc->c[0] = POSITION_TO_ELEMENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001816 pc->c[1] = 2;
Borislav Petkov860ff5e2008-02-02 19:56:50 +01001817 put_unaligned(cpu_to_be32(block), (unsigned int *) &pc->c[3]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001818 pc->c[8] = partition;
Borislav Petkov346331f2008-04-18 00:46:26 +02001819 pc->flags |= PC_FLAG_WAIT_FOR_DSC;
Borislav Petkovd236d742008-04-18 00:46:27 +02001820 pc->idetape_callback = &idetape_pc_callback;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001821}
1822
Borislav Petkovd236d742008-04-18 00:46:27 +02001823static int idetape_create_prevent_cmd(ide_drive_t *drive,
1824 struct ide_atapi_pc *pc, int prevent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825{
1826 idetape_tape_t *tape = drive->driver_data;
1827
Borislav Petkovb6422012008-02-02 19:56:49 +01001828 /* device supports locking according to capabilities page */
1829 if (!(tape->caps[6] & 0x01))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001830 return 0;
1831
1832 idetape_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +01001833 pc->c[0] = ALLOW_MEDIUM_REMOVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001834 pc->c[4] = prevent;
Borislav Petkovd236d742008-04-18 00:46:27 +02001835 pc->idetape_callback = &idetape_pc_callback;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001836 return 1;
1837}
1838
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001839static int __idetape_discard_read_pipeline(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001840{
1841 idetape_tape_t *tape = drive->driver_data;
1842 unsigned long flags;
1843 int cnt;
1844
Borislav Petkov54abf372008-02-06 02:57:52 +01001845 if (tape->chrdev_dir != IDETAPE_DIR_READ)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001846 return 0;
1847
1848 /* Remove merge stage. */
Borislav Petkov54bb2072008-02-06 02:57:52 +01001849 cnt = tape->merge_stage_size / tape->blk_size;
Borislav Petkov346331f2008-04-18 00:46:26 +02001850 if (test_and_clear_bit(IDETAPE_FLAG_FILEMARK, &tape->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001851 ++cnt; /* Filemarks count as 1 sector */
1852 tape->merge_stage_size = 0;
1853 if (tape->merge_stage != NULL) {
1854 __idetape_kfree_stage(tape->merge_stage);
1855 tape->merge_stage = NULL;
1856 }
1857
1858 /* Clear pipeline flags. */
Borislav Petkov346331f2008-04-18 00:46:26 +02001859 clear_bit(IDETAPE_FLAG_PIPELINE_ERR, &tape->flags);
Borislav Petkov54abf372008-02-06 02:57:52 +01001860 tape->chrdev_dir = IDETAPE_DIR_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001861
1862 /* Remove pipeline stages. */
1863 if (tape->first_stage == NULL)
1864 return 0;
1865
Borislav Petkov54bb2072008-02-06 02:57:52 +01001866 spin_lock_irqsave(&tape->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001867 tape->next_stage = NULL;
Borislav Petkovc4b22f82008-04-26 22:25:20 +02001868 if (test_bit(IDETAPE_FLAG_PIPELINE_ACTIVE, &tape->flags))
Borislav Petkov54bb2072008-02-06 02:57:52 +01001869 idetape_wait_for_request(drive, tape->active_data_rq);
1870 spin_unlock_irqrestore(&tape->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001871
1872 while (tape->first_stage != NULL) {
1873 struct request *rq_ptr = &tape->first_stage->rq;
1874
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001875 cnt += rq_ptr->nr_sectors - rq_ptr->current_nr_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001876 if (rq_ptr->errors == IDETAPE_ERROR_FILEMARK)
1877 ++cnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001878 }
1879 tape->nr_pending_stages = 0;
1880 tape->max_stages = tape->min_pipeline;
1881 return cnt;
1882}
1883
1884/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001885 * Position the tape to the requested block using the LOCATE packet command.
1886 * A READ POSITION command is then issued to check where we are positioned. Like
1887 * all higher level operations, we queue the commands at the tail of the request
1888 * queue and wait for their completion.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001889 */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001890static int idetape_position_tape(ide_drive_t *drive, unsigned int block,
1891 u8 partition, int skip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001892{
1893 idetape_tape_t *tape = drive->driver_data;
1894 int retval;
Borislav Petkovd236d742008-04-18 00:46:27 +02001895 struct ide_atapi_pc pc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001896
Borislav Petkov54abf372008-02-06 02:57:52 +01001897 if (tape->chrdev_dir == IDETAPE_DIR_READ)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001898 __idetape_discard_read_pipeline(drive);
1899 idetape_wait_ready(drive, 60 * 5 * HZ);
1900 idetape_create_locate_cmd(drive, &pc, block, partition, skip);
1901 retval = idetape_queue_pc_tail(drive, &pc);
1902 if (retval)
1903 return (retval);
1904
1905 idetape_create_read_position_cmd(&pc);
1906 return (idetape_queue_pc_tail(drive, &pc));
1907}
1908
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001909static void idetape_discard_read_pipeline(ide_drive_t *drive,
1910 int restore_position)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001911{
1912 idetape_tape_t *tape = drive->driver_data;
1913 int cnt;
1914 int seek, position;
1915
1916 cnt = __idetape_discard_read_pipeline(drive);
1917 if (restore_position) {
1918 position = idetape_read_position(drive);
1919 seek = position > cnt ? position - cnt : 0;
1920 if (idetape_position_tape(drive, seek, 0, 0)) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001921 printk(KERN_INFO "ide-tape: %s: position_tape failed in"
1922 " discard_pipeline()\n", tape->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001923 return;
1924 }
1925 }
1926}
1927
1928/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001929 * Generate a read/write request for the block device interface and wait for it
1930 * to be serviced.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001931 */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001932static int idetape_queue_rw_tail(ide_drive_t *drive, int cmd, int blocks,
1933 struct idetape_bh *bh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001934{
1935 idetape_tape_t *tape = drive->driver_data;
1936 struct request rq;
1937
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001938 debug_log(DBG_SENSE, "%s: cmd=%d\n", __func__, cmd);
1939
Borislav Petkovc4b22f82008-04-26 22:25:20 +02001940 if (test_bit(IDETAPE_FLAG_PIPELINE_ACTIVE, &tape->flags)) {
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001941 printk(KERN_ERR "ide-tape: bug: the pipeline is active in %s\n",
1942 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001943 return (0);
1944 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001945
1946 idetape_init_rq(&rq, cmd);
1947 rq.rq_disk = tape->disk;
1948 rq.special = (void *)bh;
Borislav Petkov54bb2072008-02-06 02:57:52 +01001949 rq.sector = tape->first_frame;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001950 rq.nr_sectors = blocks;
1951 rq.current_nr_sectors = blocks;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001952 (void) ide_do_drive_cmd(drive, &rq, ide_wait);
1953
1954 if ((cmd & (REQ_IDETAPE_READ | REQ_IDETAPE_WRITE)) == 0)
1955 return 0;
1956
1957 if (tape->merge_stage)
1958 idetape_init_merge_stage(tape);
1959 if (rq.errors == IDETAPE_ERROR_GENERAL)
1960 return -EIO;
Borislav Petkov54bb2072008-02-06 02:57:52 +01001961 return (tape->blk_size * (blocks-rq.current_nr_sectors));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001962}
1963
Borislav Petkov8d06bfa2008-02-06 02:57:53 +01001964/* start servicing the pipeline stages, starting from tape->next_stage. */
1965static void idetape_plug_pipeline(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001966{
1967 idetape_tape_t *tape = drive->driver_data;
1968
1969 if (tape->next_stage == NULL)
1970 return;
Borislav Petkovc4b22f82008-04-26 22:25:20 +02001971 if (!test_and_set_bit(IDETAPE_FLAG_PIPELINE_ACTIVE, &tape->flags)) {
Borislav Petkov419d4742008-02-02 19:56:51 +01001972 idetape_activate_next_stage(drive);
Borislav Petkov54bb2072008-02-06 02:57:52 +01001973 (void) ide_do_drive_cmd(drive, tape->active_data_rq, ide_end);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001974 }
1975}
1976
Borislav Petkovd236d742008-04-18 00:46:27 +02001977static void idetape_create_inquiry_cmd(struct ide_atapi_pc *pc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001978{
1979 idetape_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +01001980 pc->c[0] = INQUIRY;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001981 pc->c[4] = 254;
Borislav Petkovd236d742008-04-18 00:46:27 +02001982 pc->req_xfer = 254;
1983 pc->idetape_callback = &idetape_pc_callback;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001984}
1985
Borislav Petkovd236d742008-04-18 00:46:27 +02001986static void idetape_create_rewind_cmd(ide_drive_t *drive,
1987 struct ide_atapi_pc *pc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001988{
1989 idetape_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +01001990 pc->c[0] = REZERO_UNIT;
Borislav Petkov346331f2008-04-18 00:46:26 +02001991 pc->flags |= PC_FLAG_WAIT_FOR_DSC;
Borislav Petkovd236d742008-04-18 00:46:27 +02001992 pc->idetape_callback = &idetape_pc_callback;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001993}
1994
Borislav Petkovd236d742008-04-18 00:46:27 +02001995static void idetape_create_erase_cmd(struct ide_atapi_pc *pc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001996{
1997 idetape_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +01001998 pc->c[0] = ERASE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001999 pc->c[1] = 1;
Borislav Petkov346331f2008-04-18 00:46:26 +02002000 pc->flags |= PC_FLAG_WAIT_FOR_DSC;
Borislav Petkovd236d742008-04-18 00:46:27 +02002001 pc->idetape_callback = &idetape_pc_callback;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002002}
2003
Borislav Petkovd236d742008-04-18 00:46:27 +02002004static void idetape_create_space_cmd(struct ide_atapi_pc *pc, int count, u8 cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002005{
2006 idetape_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +01002007 pc->c[0] = SPACE;
Borislav Petkov860ff5e2008-02-02 19:56:50 +01002008 put_unaligned(cpu_to_be32(count), (unsigned int *) &pc->c[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002009 pc->c[1] = cmd;
Borislav Petkov346331f2008-04-18 00:46:26 +02002010 pc->flags |= PC_FLAG_WAIT_FOR_DSC;
Borislav Petkovd236d742008-04-18 00:46:27 +02002011 pc->idetape_callback = &idetape_pc_callback;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002012}
2013
Borislav Petkov97c566c2008-04-27 15:38:25 +02002014/* Queue up a character device originated write request. */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002015static int idetape_add_chrdev_write_request(ide_drive_t *drive, int blocks)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002016{
2017 idetape_tape_t *tape = drive->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002018 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002019
Borislav Petkov8004a8c2008-02-06 02:57:51 +01002020 debug_log(DBG_CHRDEV, "Enter %s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002021
Borislav Petkov3c98bf32008-02-06 02:57:53 +01002022 /* Attempt to allocate a new stage. Beware possible race conditions. */
Borislav Petkov97c566c2008-04-27 15:38:25 +02002023 while (1) {
Borislav Petkov54bb2072008-02-06 02:57:52 +01002024 spin_lock_irqsave(&tape->lock, flags);
Borislav Petkovc4b22f82008-04-26 22:25:20 +02002025 if (test_bit(IDETAPE_FLAG_PIPELINE_ACTIVE, &tape->flags)) {
Borislav Petkov54bb2072008-02-06 02:57:52 +01002026 idetape_wait_for_request(drive, tape->active_data_rq);
2027 spin_unlock_irqrestore(&tape->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002028 } else {
Borislav Petkov54bb2072008-02-06 02:57:52 +01002029 spin_unlock_irqrestore(&tape->lock, flags);
Borislav Petkov8d06bfa2008-02-06 02:57:53 +01002030 idetape_plug_pipeline(drive);
Borislav Petkovc4b22f82008-04-26 22:25:20 +02002031 if (test_bit(IDETAPE_FLAG_PIPELINE_ACTIVE,
2032 &tape->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002033 continue;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002034 return idetape_queue_rw_tail(drive, REQ_IDETAPE_WRITE,
2035 blocks, tape->merge_stage->bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002036 }
2037 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002038}
2039
2040/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +01002041 * Wait until all pending pipeline requests are serviced. Typically called on
2042 * device close.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002043 */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002044static void idetape_wait_for_pipeline(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002045{
2046 idetape_tape_t *tape = drive->driver_data;
2047 unsigned long flags;
2048
Borislav Petkovc4b22f82008-04-26 22:25:20 +02002049 while (tape->next_stage || test_bit(IDETAPE_FLAG_PIPELINE_ACTIVE,
2050 &tape->flags)) {
Borislav Petkov8d06bfa2008-02-06 02:57:53 +01002051 idetape_plug_pipeline(drive);
Borislav Petkov54bb2072008-02-06 02:57:52 +01002052 spin_lock_irqsave(&tape->lock, flags);
Borislav Petkovc4b22f82008-04-26 22:25:20 +02002053 if (test_bit(IDETAPE_FLAG_PIPELINE_ACTIVE, &tape->flags))
Borislav Petkov54bb2072008-02-06 02:57:52 +01002054 idetape_wait_for_request(drive, tape->active_data_rq);
2055 spin_unlock_irqrestore(&tape->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002056 }
2057}
2058
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002059static void idetape_empty_write_pipeline(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002060{
2061 idetape_tape_t *tape = drive->driver_data;
2062 int blocks, min;
2063 struct idetape_bh *bh;
Borislav Petkov55a5d292008-02-02 19:56:49 +01002064
Borislav Petkov54abf372008-02-06 02:57:52 +01002065 if (tape->chrdev_dir != IDETAPE_DIR_WRITE) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002066 printk(KERN_ERR "ide-tape: bug: Trying to empty write pipeline,"
2067 " but we are not writing.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002068 return;
2069 }
2070 if (tape->merge_stage_size > tape->stage_size) {
2071 printk(KERN_ERR "ide-tape: bug: merge_buffer too big\n");
2072 tape->merge_stage_size = tape->stage_size;
2073 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002074 if (tape->merge_stage_size) {
Borislav Petkov54bb2072008-02-06 02:57:52 +01002075 blocks = tape->merge_stage_size / tape->blk_size;
2076 if (tape->merge_stage_size % tape->blk_size) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002077 unsigned int i;
2078
2079 blocks++;
Borislav Petkov54bb2072008-02-06 02:57:52 +01002080 i = tape->blk_size - tape->merge_stage_size %
2081 tape->blk_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002082 bh = tape->bh->b_reqnext;
2083 while (bh) {
2084 atomic_set(&bh->b_count, 0);
2085 bh = bh->b_reqnext;
2086 }
2087 bh = tape->bh;
2088 while (i) {
2089 if (bh == NULL) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002090 printk(KERN_INFO "ide-tape: bug,"
2091 " bh NULL\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002092 break;
2093 }
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002094 min = min(i, (unsigned int)(bh->b_size -
2095 atomic_read(&bh->b_count)));
2096 memset(bh->b_data + atomic_read(&bh->b_count),
2097 0, min);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002098 atomic_add(min, &bh->b_count);
2099 i -= min;
2100 bh = bh->b_reqnext;
2101 }
2102 }
2103 (void) idetape_add_chrdev_write_request(drive, blocks);
2104 tape->merge_stage_size = 0;
2105 }
2106 idetape_wait_for_pipeline(drive);
2107 if (tape->merge_stage != NULL) {
2108 __idetape_kfree_stage(tape->merge_stage);
2109 tape->merge_stage = NULL;
2110 }
Borislav Petkov346331f2008-04-18 00:46:26 +02002111 clear_bit(IDETAPE_FLAG_PIPELINE_ERR, &tape->flags);
Borislav Petkov54abf372008-02-06 02:57:52 +01002112 tape->chrdev_dir = IDETAPE_DIR_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002113
2114 /*
Borislav Petkov3c98bf32008-02-06 02:57:53 +01002115 * On the next backup, perform the feedback loop again. (I don't want to
2116 * keep sense information between backups, as some systems are
2117 * constantly on, and the system load can be totally different on the
2118 * next backup).
Linus Torvalds1da177e2005-04-16 15:20:36 -07002119 */
2120 tape->max_stages = tape->min_pipeline;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002121 if (tape->first_stage != NULL ||
2122 tape->next_stage != NULL ||
2123 tape->last_stage != NULL ||
2124 tape->nr_stages != 0) {
2125 printk(KERN_ERR "ide-tape: ide-tape pipeline bug, "
2126 "first_stage %p, next_stage %p, "
2127 "last_stage %p, nr_stages %d\n",
2128 tape->first_stage, tape->next_stage,
2129 tape->last_stage, tape->nr_stages);
2130 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002131}
2132
Borislav Petkov8d06bfa2008-02-06 02:57:53 +01002133static int idetape_init_read(ide_drive_t *drive, int max_stages)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002134{
2135 idetape_tape_t *tape = drive->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002136 int bytes_read;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002137
2138 /* Initialize read operation */
Borislav Petkov54abf372008-02-06 02:57:52 +01002139 if (tape->chrdev_dir != IDETAPE_DIR_READ) {
2140 if (tape->chrdev_dir == IDETAPE_DIR_WRITE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002141 idetape_empty_write_pipeline(drive);
2142 idetape_flush_tape_buffers(drive);
2143 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002144 if (tape->merge_stage || tape->merge_stage_size) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002145 printk(KERN_ERR "ide-tape: merge_stage_size should be"
2146 " 0 now\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002147 tape->merge_stage_size = 0;
2148 }
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002149 tape->merge_stage = __idetape_kmalloc_stage(tape, 0, 0);
2150 if (!tape->merge_stage)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002151 return -ENOMEM;
Borislav Petkov54abf372008-02-06 02:57:52 +01002152 tape->chrdev_dir = IDETAPE_DIR_READ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002153
2154 /*
Borislav Petkov3c98bf32008-02-06 02:57:53 +01002155 * Issue a read 0 command to ensure that DSC handshake is
2156 * switched from completion mode to buffer available mode.
2157 * No point in issuing this if DSC overlap isn't supported, some
2158 * drives (Seagate STT3401A) will return an error.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002159 */
2160 if (drive->dsc_overlap) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002161 bytes_read = idetape_queue_rw_tail(drive,
2162 REQ_IDETAPE_READ, 0,
2163 tape->merge_stage->bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002164 if (bytes_read < 0) {
2165 __idetape_kfree_stage(tape->merge_stage);
2166 tape->merge_stage = NULL;
Borislav Petkov54abf372008-02-06 02:57:52 +01002167 tape->chrdev_dir = IDETAPE_DIR_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002168 return bytes_read;
2169 }
2170 }
2171 }
Borislav Petkov5e69bd92008-04-27 15:38:25 +02002172
Borislav Petkovc4b22f82008-04-26 22:25:20 +02002173 if (!test_bit(IDETAPE_FLAG_PIPELINE_ACTIVE, &tape->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002174 if (tape->nr_pending_stages >= 3 * max_stages / 4) {
2175 tape->measure_insert_time = 1;
2176 tape->insert_time = jiffies;
2177 tape->insert_size = 0;
2178 tape->insert_speed = 0;
Borislav Petkov8d06bfa2008-02-06 02:57:53 +01002179 idetape_plug_pipeline(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002180 }
2181 }
2182 return 0;
2183}
2184
2185/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +01002186 * Called from idetape_chrdev_read() to service a character device read request
2187 * and add read-ahead requests to our pipeline.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002188 */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002189static int idetape_add_chrdev_read_request(ide_drive_t *drive, int blocks)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002190{
2191 idetape_tape_t *tape = drive->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002192
Borislav Petkov8004a8c2008-02-06 02:57:51 +01002193 debug_log(DBG_PROCS, "Enter %s, %d blocks\n", __func__, blocks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002194
Borislav Petkov3c98bf32008-02-06 02:57:53 +01002195 /* If we are at a filemark, return a read length of 0 */
Borislav Petkov346331f2008-04-18 00:46:26 +02002196 if (test_bit(IDETAPE_FLAG_FILEMARK, &tape->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002197 return 0;
2198
Borislav Petkov8d06bfa2008-02-06 02:57:53 +01002199 idetape_init_read(drive, tape->max_stages);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002200
Borislav Petkov5e69bd92008-04-27 15:38:25 +02002201 if (test_bit(IDETAPE_FLAG_PIPELINE_ERR, &tape->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002202 return 0;
Borislav Petkov5e69bd92008-04-27 15:38:25 +02002203
2204 return idetape_queue_rw_tail(drive, REQ_IDETAPE_READ, blocks,
2205 tape->merge_stage->bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002206}
2207
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002208static void idetape_pad_zeros(ide_drive_t *drive, int bcount)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002209{
2210 idetape_tape_t *tape = drive->driver_data;
2211 struct idetape_bh *bh;
2212 int blocks;
Borislav Petkov3c98bf32008-02-06 02:57:53 +01002213
Linus Torvalds1da177e2005-04-16 15:20:36 -07002214 while (bcount) {
2215 unsigned int count;
2216
2217 bh = tape->merge_stage->bh;
2218 count = min(tape->stage_size, bcount);
2219 bcount -= count;
Borislav Petkov54bb2072008-02-06 02:57:52 +01002220 blocks = count / tape->blk_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002221 while (count) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002222 atomic_set(&bh->b_count,
2223 min(count, (unsigned int)bh->b_size));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002224 memset(bh->b_data, 0, atomic_read(&bh->b_count));
2225 count -= atomic_read(&bh->b_count);
2226 bh = bh->b_reqnext;
2227 }
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002228 idetape_queue_rw_tail(drive, REQ_IDETAPE_WRITE, blocks,
2229 tape->merge_stage->bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002230 }
2231}
2232
Linus Torvalds1da177e2005-04-16 15:20:36 -07002233/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +01002234 * Rewinds the tape to the Beginning Of the current Partition (BOP). We
2235 * currently support only one partition.
2236 */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002237static int idetape_rewind_tape(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002238{
2239 int retval;
Borislav Petkovd236d742008-04-18 00:46:27 +02002240 struct ide_atapi_pc pc;
Borislav Petkov8004a8c2008-02-06 02:57:51 +01002241 idetape_tape_t *tape;
2242 tape = drive->driver_data;
2243
2244 debug_log(DBG_SENSE, "Enter %s\n", __func__);
2245
Linus Torvalds1da177e2005-04-16 15:20:36 -07002246 idetape_create_rewind_cmd(drive, &pc);
2247 retval = idetape_queue_pc_tail(drive, &pc);
2248 if (retval)
2249 return retval;
2250
2251 idetape_create_read_position_cmd(&pc);
2252 retval = idetape_queue_pc_tail(drive, &pc);
2253 if (retval)
2254 return retval;
2255 return 0;
2256}
2257
Borislav Petkov3c98bf32008-02-06 02:57:53 +01002258/* mtio.h compatible commands should be issued to the chrdev interface. */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002259static int idetape_blkdev_ioctl(ide_drive_t *drive, unsigned int cmd,
2260 unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002261{
2262 idetape_tape_t *tape = drive->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002263 void __user *argp = (void __user *)arg;
2264
Borislav Petkovd59823f2008-02-02 19:56:51 +01002265 struct idetape_config {
2266 int dsc_rw_frequency;
2267 int dsc_media_access_frequency;
2268 int nr_stages;
2269 } config;
2270
Borislav Petkov8004a8c2008-02-06 02:57:51 +01002271 debug_log(DBG_PROCS, "Enter %s\n", __func__);
2272
Linus Torvalds1da177e2005-04-16 15:20:36 -07002273 switch (cmd) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002274 case 0x0340:
2275 if (copy_from_user(&config, argp, sizeof(config)))
2276 return -EFAULT;
2277 tape->best_dsc_rw_freq = config.dsc_rw_frequency;
2278 tape->max_stages = config.nr_stages;
2279 break;
2280 case 0x0350:
2281 config.dsc_rw_frequency = (int) tape->best_dsc_rw_freq;
2282 config.nr_stages = tape->max_stages;
2283 if (copy_to_user(argp, &config, sizeof(config)))
2284 return -EFAULT;
2285 break;
2286 default:
2287 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002288 }
2289 return 0;
2290}
2291
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002292static int idetape_space_over_filemarks(ide_drive_t *drive, short mt_op,
2293 int mt_count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002294{
2295 idetape_tape_t *tape = drive->driver_data;
Borislav Petkovd236d742008-04-18 00:46:27 +02002296 struct ide_atapi_pc pc;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002297 int retval, count = 0;
Borislav Petkovb6422012008-02-02 19:56:49 +01002298 int sprev = !!(tape->caps[4] & 0x20);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002299
2300 if (mt_count == 0)
2301 return 0;
2302 if (MTBSF == mt_op || MTBSFM == mt_op) {
Borislav Petkovb6422012008-02-02 19:56:49 +01002303 if (!sprev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002304 return -EIO;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002305 mt_count = -mt_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002306 }
2307
Borislav Petkov54abf372008-02-06 02:57:52 +01002308 if (tape->chrdev_dir == IDETAPE_DIR_READ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002309 tape->merge_stage_size = 0;
Borislav Petkov346331f2008-04-18 00:46:26 +02002310 if (test_and_clear_bit(IDETAPE_FLAG_FILEMARK, &tape->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002311 ++count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002312 idetape_discard_read_pipeline(drive, 0);
2313 }
2314
2315 /*
Borislav Petkov3c98bf32008-02-06 02:57:53 +01002316 * The filemark was not found in our internal pipeline; now we can issue
2317 * the space command.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002318 */
2319 switch (mt_op) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002320 case MTFSF:
2321 case MTBSF:
2322 idetape_create_space_cmd(&pc, mt_count - count,
2323 IDETAPE_SPACE_OVER_FILEMARK);
2324 return idetape_queue_pc_tail(drive, &pc);
2325 case MTFSFM:
2326 case MTBSFM:
2327 if (!sprev)
2328 return -EIO;
2329 retval = idetape_space_over_filemarks(drive, MTFSF,
2330 mt_count - count);
2331 if (retval)
2332 return retval;
2333 count = (MTBSFM == mt_op ? 1 : -1);
2334 return idetape_space_over_filemarks(drive, MTFSF, count);
2335 default:
2336 printk(KERN_ERR "ide-tape: MTIO operation %d not supported\n",
2337 mt_op);
2338 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002339 }
2340}
2341
Linus Torvalds1da177e2005-04-16 15:20:36 -07002342/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +01002343 * Our character device read / write functions.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002344 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +01002345 * The tape is optimized to maximize throughput when it is transferring an
2346 * integral number of the "continuous transfer limit", which is a parameter of
2347 * the specific tape (26kB on my particular tape, 32kB for Onstream).
Linus Torvalds1da177e2005-04-16 15:20:36 -07002348 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +01002349 * As of version 1.3 of the driver, the character device provides an abstract
2350 * continuous view of the media - any mix of block sizes (even 1 byte) on the
2351 * same backup/restore procedure is supported. The driver will internally
2352 * convert the requests to the recommended transfer unit, so that an unmatch
2353 * between the user's block size to the recommended size will only result in a
2354 * (slightly) increased driver overhead, but will no longer hit performance.
2355 * This is not applicable to Onstream.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002356 */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002357static ssize_t idetape_chrdev_read(struct file *file, char __user *buf,
2358 size_t count, loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002359{
2360 struct ide_tape_obj *tape = ide_tape_f(file);
2361 ide_drive_t *drive = tape->drive;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002362 ssize_t bytes_read, temp, actually_read = 0, rc;
Daniel Walkerdcd96372006-06-25 05:47:37 -07002363 ssize_t ret = 0;
Borislav Petkovb6422012008-02-02 19:56:49 +01002364 u16 ctl = *(u16 *)&tape->caps[12];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002365
Borislav Petkov8004a8c2008-02-06 02:57:51 +01002366 debug_log(DBG_CHRDEV, "Enter %s, count %Zd\n", __func__, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002367
Borislav Petkov54abf372008-02-06 02:57:52 +01002368 if (tape->chrdev_dir != IDETAPE_DIR_READ) {
Borislav Petkov346331f2008-04-18 00:46:26 +02002369 if (test_bit(IDETAPE_FLAG_DETECT_BS, &tape->flags))
Borislav Petkov54bb2072008-02-06 02:57:52 +01002370 if (count > tape->blk_size &&
2371 (count % tape->blk_size) == 0)
2372 tape->user_bs_factor = count / tape->blk_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002373 }
Borislav Petkov8d06bfa2008-02-06 02:57:53 +01002374 rc = idetape_init_read(drive, tape->max_stages);
2375 if (rc < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002376 return rc;
2377 if (count == 0)
2378 return (0);
2379 if (tape->merge_stage_size) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002380 actually_read = min((unsigned int)(tape->merge_stage_size),
2381 (unsigned int)count);
Borislav Petkov99d74e62008-04-27 15:38:25 +02002382 if (idetape_copy_stage_to_user(tape, buf, actually_read))
Daniel Walkerdcd96372006-06-25 05:47:37 -07002383 ret = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002384 buf += actually_read;
2385 tape->merge_stage_size -= actually_read;
2386 count -= actually_read;
2387 }
2388 while (count >= tape->stage_size) {
Borislav Petkovb6422012008-02-02 19:56:49 +01002389 bytes_read = idetape_add_chrdev_read_request(drive, ctl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002390 if (bytes_read <= 0)
2391 goto finish;
Borislav Petkov99d74e62008-04-27 15:38:25 +02002392 if (idetape_copy_stage_to_user(tape, buf, bytes_read))
Daniel Walkerdcd96372006-06-25 05:47:37 -07002393 ret = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002394 buf += bytes_read;
2395 count -= bytes_read;
2396 actually_read += bytes_read;
2397 }
2398 if (count) {
Borislav Petkovb6422012008-02-02 19:56:49 +01002399 bytes_read = idetape_add_chrdev_read_request(drive, ctl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002400 if (bytes_read <= 0)
2401 goto finish;
2402 temp = min((unsigned long)count, (unsigned long)bytes_read);
Borislav Petkov99d74e62008-04-27 15:38:25 +02002403 if (idetape_copy_stage_to_user(tape, buf, temp))
Daniel Walkerdcd96372006-06-25 05:47:37 -07002404 ret = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002405 actually_read += temp;
2406 tape->merge_stage_size = bytes_read-temp;
2407 }
2408finish:
Borislav Petkov346331f2008-04-18 00:46:26 +02002409 if (!actually_read && test_bit(IDETAPE_FLAG_FILEMARK, &tape->flags)) {
Borislav Petkov8004a8c2008-02-06 02:57:51 +01002410 debug_log(DBG_SENSE, "%s: spacing over filemark\n", tape->name);
2411
Linus Torvalds1da177e2005-04-16 15:20:36 -07002412 idetape_space_over_filemarks(drive, MTFSF, 1);
2413 return 0;
2414 }
Daniel Walkerdcd96372006-06-25 05:47:37 -07002415
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002416 return ret ? ret : actually_read;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002417}
2418
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002419static ssize_t idetape_chrdev_write(struct file *file, const char __user *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002420 size_t count, loff_t *ppos)
2421{
2422 struct ide_tape_obj *tape = ide_tape_f(file);
2423 ide_drive_t *drive = tape->drive;
Daniel Walkerdcd96372006-06-25 05:47:37 -07002424 ssize_t actually_written = 0;
2425 ssize_t ret = 0;
Borislav Petkovb6422012008-02-02 19:56:49 +01002426 u16 ctl = *(u16 *)&tape->caps[12];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002427
2428 /* The drive is write protected. */
2429 if (tape->write_prot)
2430 return -EACCES;
2431
Borislav Petkov8004a8c2008-02-06 02:57:51 +01002432 debug_log(DBG_CHRDEV, "Enter %s, count %Zd\n", __func__, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002433
2434 /* Initialize write operation */
Borislav Petkov54abf372008-02-06 02:57:52 +01002435 if (tape->chrdev_dir != IDETAPE_DIR_WRITE) {
2436 if (tape->chrdev_dir == IDETAPE_DIR_READ)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002437 idetape_discard_read_pipeline(drive, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002438 if (tape->merge_stage || tape->merge_stage_size) {
2439 printk(KERN_ERR "ide-tape: merge_stage_size "
2440 "should be 0 now\n");
2441 tape->merge_stage_size = 0;
2442 }
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002443 tape->merge_stage = __idetape_kmalloc_stage(tape, 0, 0);
2444 if (!tape->merge_stage)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002445 return -ENOMEM;
Borislav Petkov54abf372008-02-06 02:57:52 +01002446 tape->chrdev_dir = IDETAPE_DIR_WRITE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002447 idetape_init_merge_stage(tape);
2448
2449 /*
Borislav Petkov3c98bf32008-02-06 02:57:53 +01002450 * Issue a write 0 command to ensure that DSC handshake is
2451 * switched from completion mode to buffer available mode. No
2452 * point in issuing this if DSC overlap isn't supported, some
2453 * drives (Seagate STT3401A) will return an error.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002454 */
2455 if (drive->dsc_overlap) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002456 ssize_t retval = idetape_queue_rw_tail(drive,
2457 REQ_IDETAPE_WRITE, 0,
2458 tape->merge_stage->bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002459 if (retval < 0) {
2460 __idetape_kfree_stage(tape->merge_stage);
2461 tape->merge_stage = NULL;
Borislav Petkov54abf372008-02-06 02:57:52 +01002462 tape->chrdev_dir = IDETAPE_DIR_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002463 return retval;
2464 }
2465 }
2466 }
2467 if (count == 0)
2468 return (0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002469 if (tape->merge_stage_size) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002470 if (tape->merge_stage_size >= tape->stage_size) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002471 printk(KERN_ERR "ide-tape: bug: merge buf too big\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002472 tape->merge_stage_size = 0;
2473 }
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002474 actually_written = min((unsigned int)
2475 (tape->stage_size - tape->merge_stage_size),
2476 (unsigned int)count);
Borislav Petkov8646c882008-04-27 15:38:26 +02002477 if (idetape_copy_stage_from_user(tape, buf, actually_written))
Daniel Walkerdcd96372006-06-25 05:47:37 -07002478 ret = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002479 buf += actually_written;
2480 tape->merge_stage_size += actually_written;
2481 count -= actually_written;
2482
2483 if (tape->merge_stage_size == tape->stage_size) {
Daniel Walkerdcd96372006-06-25 05:47:37 -07002484 ssize_t retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002485 tape->merge_stage_size = 0;
Borislav Petkovb6422012008-02-02 19:56:49 +01002486 retval = idetape_add_chrdev_write_request(drive, ctl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002487 if (retval <= 0)
2488 return (retval);
2489 }
2490 }
2491 while (count >= tape->stage_size) {
Daniel Walkerdcd96372006-06-25 05:47:37 -07002492 ssize_t retval;
Borislav Petkov8646c882008-04-27 15:38:26 +02002493 if (idetape_copy_stage_from_user(tape, buf, tape->stage_size))
Daniel Walkerdcd96372006-06-25 05:47:37 -07002494 ret = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002495 buf += tape->stage_size;
2496 count -= tape->stage_size;
Borislav Petkovb6422012008-02-02 19:56:49 +01002497 retval = idetape_add_chrdev_write_request(drive, ctl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002498 actually_written += tape->stage_size;
2499 if (retval <= 0)
2500 return (retval);
2501 }
2502 if (count) {
2503 actually_written += count;
Borislav Petkov8646c882008-04-27 15:38:26 +02002504 if (idetape_copy_stage_from_user(tape, buf, count))
Daniel Walkerdcd96372006-06-25 05:47:37 -07002505 ret = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002506 tape->merge_stage_size += count;
2507 }
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002508 return ret ? ret : actually_written;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002509}
2510
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002511static int idetape_write_filemark(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002512{
Borislav Petkovd236d742008-04-18 00:46:27 +02002513 struct ide_atapi_pc pc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002514
2515 /* Write a filemark */
2516 idetape_create_write_filemark_cmd(drive, &pc, 1);
2517 if (idetape_queue_pc_tail(drive, &pc)) {
2518 printk(KERN_ERR "ide-tape: Couldn't write a filemark\n");
2519 return -EIO;
2520 }
2521 return 0;
2522}
2523
2524/*
Borislav Petkovd99c9da2008-02-02 19:56:51 +01002525 * Called from idetape_chrdev_ioctl when the general mtio MTIOCTOP ioctl is
2526 * requested.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002527 *
Borislav Petkovd99c9da2008-02-02 19:56:51 +01002528 * Note: MTBSF and MTBSFM are not supported when the tape doesn't support
2529 * spacing over filemarks in the reverse direction. In this case, MTFSFM is also
2530 * usually not supported (it is supported in the rare case in which we crossed
2531 * the filemark during our read-ahead pipelined operation mode).
Linus Torvalds1da177e2005-04-16 15:20:36 -07002532 *
Borislav Petkovd99c9da2008-02-02 19:56:51 +01002533 * The following commands are currently not supported:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002534 *
Borislav Petkovd99c9da2008-02-02 19:56:51 +01002535 * MTFSS, MTBSS, MTWSM, MTSETDENSITY, MTSETDRVBUFFER, MT_ST_BOOLEANS,
2536 * MT_ST_WRITE_THRESHOLD.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002537 */
Borislav Petkovd99c9da2008-02-02 19:56:51 +01002538static int idetape_mtioctop(ide_drive_t *drive, short mt_op, int mt_count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002539{
2540 idetape_tape_t *tape = drive->driver_data;
Borislav Petkovd236d742008-04-18 00:46:27 +02002541 struct ide_atapi_pc pc;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002542 int i, retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002543
Borislav Petkov8004a8c2008-02-06 02:57:51 +01002544 debug_log(DBG_ERR, "Handling MTIOCTOP ioctl: mt_op=%d, mt_count=%d\n",
2545 mt_op, mt_count);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002546
Borislav Petkov3c98bf32008-02-06 02:57:53 +01002547 /* Commands which need our pipelined read-ahead stages. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002548 switch (mt_op) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002549 case MTFSF:
2550 case MTFSFM:
2551 case MTBSF:
2552 case MTBSFM:
2553 if (!mt_count)
2554 return 0;
2555 return idetape_space_over_filemarks(drive, mt_op, mt_count);
2556 default:
2557 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002558 }
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002559
Linus Torvalds1da177e2005-04-16 15:20:36 -07002560 switch (mt_op) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002561 case MTWEOF:
2562 if (tape->write_prot)
2563 return -EACCES;
2564 idetape_discard_read_pipeline(drive, 1);
2565 for (i = 0; i < mt_count; i++) {
2566 retval = idetape_write_filemark(drive);
2567 if (retval)
2568 return retval;
2569 }
2570 return 0;
2571 case MTREW:
2572 idetape_discard_read_pipeline(drive, 0);
2573 if (idetape_rewind_tape(drive))
2574 return -EIO;
2575 return 0;
2576 case MTLOAD:
2577 idetape_discard_read_pipeline(drive, 0);
2578 idetape_create_load_unload_cmd(drive, &pc,
2579 IDETAPE_LU_LOAD_MASK);
2580 return idetape_queue_pc_tail(drive, &pc);
2581 case MTUNLOAD:
2582 case MTOFFL:
2583 /*
2584 * If door is locked, attempt to unlock before
2585 * attempting to eject.
2586 */
2587 if (tape->door_locked) {
2588 if (idetape_create_prevent_cmd(drive, &pc, 0))
2589 if (!idetape_queue_pc_tail(drive, &pc))
2590 tape->door_locked = DOOR_UNLOCKED;
2591 }
2592 idetape_discard_read_pipeline(drive, 0);
2593 idetape_create_load_unload_cmd(drive, &pc,
2594 !IDETAPE_LU_LOAD_MASK);
2595 retval = idetape_queue_pc_tail(drive, &pc);
2596 if (!retval)
Borislav Petkov346331f2008-04-18 00:46:26 +02002597 clear_bit(IDETAPE_FLAG_MEDIUM_PRESENT, &tape->flags);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002598 return retval;
2599 case MTNOP:
2600 idetape_discard_read_pipeline(drive, 0);
2601 return idetape_flush_tape_buffers(drive);
2602 case MTRETEN:
2603 idetape_discard_read_pipeline(drive, 0);
2604 idetape_create_load_unload_cmd(drive, &pc,
2605 IDETAPE_LU_RETENSION_MASK | IDETAPE_LU_LOAD_MASK);
2606 return idetape_queue_pc_tail(drive, &pc);
2607 case MTEOM:
2608 idetape_create_space_cmd(&pc, 0, IDETAPE_SPACE_TO_EOD);
2609 return idetape_queue_pc_tail(drive, &pc);
2610 case MTERASE:
2611 (void)idetape_rewind_tape(drive);
2612 idetape_create_erase_cmd(&pc);
2613 return idetape_queue_pc_tail(drive, &pc);
2614 case MTSETBLK:
2615 if (mt_count) {
2616 if (mt_count < tape->blk_size ||
2617 mt_count % tape->blk_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002618 return -EIO;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002619 tape->user_bs_factor = mt_count / tape->blk_size;
Borislav Petkov346331f2008-04-18 00:46:26 +02002620 clear_bit(IDETAPE_FLAG_DETECT_BS, &tape->flags);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002621 } else
Borislav Petkov346331f2008-04-18 00:46:26 +02002622 set_bit(IDETAPE_FLAG_DETECT_BS, &tape->flags);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002623 return 0;
2624 case MTSEEK:
2625 idetape_discard_read_pipeline(drive, 0);
2626 return idetape_position_tape(drive,
2627 mt_count * tape->user_bs_factor, tape->partition, 0);
2628 case MTSETPART:
2629 idetape_discard_read_pipeline(drive, 0);
2630 return idetape_position_tape(drive, 0, mt_count, 0);
2631 case MTFSR:
2632 case MTBSR:
2633 case MTLOCK:
2634 if (!idetape_create_prevent_cmd(drive, &pc, 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002635 return 0;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002636 retval = idetape_queue_pc_tail(drive, &pc);
2637 if (retval)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002638 return retval;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002639 tape->door_locked = DOOR_EXPLICITLY_LOCKED;
2640 return 0;
2641 case MTUNLOCK:
2642 if (!idetape_create_prevent_cmd(drive, &pc, 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002643 return 0;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002644 retval = idetape_queue_pc_tail(drive, &pc);
2645 if (retval)
2646 return retval;
2647 tape->door_locked = DOOR_UNLOCKED;
2648 return 0;
2649 default:
2650 printk(KERN_ERR "ide-tape: MTIO operation %d not supported\n",
2651 mt_op);
2652 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002653 }
2654}
2655
2656/*
Borislav Petkovd99c9da2008-02-02 19:56:51 +01002657 * Our character device ioctls. General mtio.h magnetic io commands are
2658 * supported here, and not in the corresponding block interface. Our own
2659 * ide-tape ioctls are supported on both interfaces.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002660 */
Borislav Petkovd99c9da2008-02-02 19:56:51 +01002661static int idetape_chrdev_ioctl(struct inode *inode, struct file *file,
2662 unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002663{
2664 struct ide_tape_obj *tape = ide_tape_f(file);
2665 ide_drive_t *drive = tape->drive;
2666 struct mtop mtop;
2667 struct mtget mtget;
2668 struct mtpos mtpos;
Borislav Petkov54bb2072008-02-06 02:57:52 +01002669 int block_offset = 0, position = tape->first_frame;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002670 void __user *argp = (void __user *)arg;
2671
Borislav Petkov8004a8c2008-02-06 02:57:51 +01002672 debug_log(DBG_CHRDEV, "Enter %s, cmd=%u\n", __func__, cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002673
Borislav Petkov54abf372008-02-06 02:57:52 +01002674 if (tape->chrdev_dir == IDETAPE_DIR_WRITE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002675 idetape_empty_write_pipeline(drive);
2676 idetape_flush_tape_buffers(drive);
2677 }
2678 if (cmd == MTIOCGET || cmd == MTIOCPOS) {
Borislav Petkovb361acb2008-04-27 15:38:26 +02002679 idetape_wait_for_pipeline(drive);
2680 block_offset = tape->merge_stage_size /
Borislav Petkov54bb2072008-02-06 02:57:52 +01002681 (tape->blk_size * tape->user_bs_factor);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002682 position = idetape_read_position(drive);
2683 if (position < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002684 return -EIO;
2685 }
2686 switch (cmd) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002687 case MTIOCTOP:
2688 if (copy_from_user(&mtop, argp, sizeof(struct mtop)))
2689 return -EFAULT;
2690 return idetape_mtioctop(drive, mtop.mt_op, mtop.mt_count);
2691 case MTIOCGET:
2692 memset(&mtget, 0, sizeof(struct mtget));
2693 mtget.mt_type = MT_ISSCSI2;
2694 mtget.mt_blkno = position / tape->user_bs_factor - block_offset;
2695 mtget.mt_dsreg =
2696 ((tape->blk_size * tape->user_bs_factor)
2697 << MT_ST_BLKSIZE_SHIFT) & MT_ST_BLKSIZE_MASK;
Borislav Petkov54bb2072008-02-06 02:57:52 +01002698
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002699 if (tape->drv_write_prot)
2700 mtget.mt_gstat |= GMT_WR_PROT(0xffffffff);
2701
2702 if (copy_to_user(argp, &mtget, sizeof(struct mtget)))
2703 return -EFAULT;
2704 return 0;
2705 case MTIOCPOS:
2706 mtpos.mt_blkno = position / tape->user_bs_factor - block_offset;
2707 if (copy_to_user(argp, &mtpos, sizeof(struct mtpos)))
2708 return -EFAULT;
2709 return 0;
2710 default:
2711 if (tape->chrdev_dir == IDETAPE_DIR_READ)
2712 idetape_discard_read_pipeline(drive, 1);
2713 return idetape_blkdev_ioctl(drive, cmd, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002714 }
2715}
2716
Borislav Petkov3cffb9c2008-02-02 19:56:50 +01002717/*
2718 * Do a mode sense page 0 with block descriptor and if it succeeds set the tape
2719 * block size with the reported value.
2720 */
2721static void ide_tape_get_bsize_from_bdesc(ide_drive_t *drive)
2722{
2723 idetape_tape_t *tape = drive->driver_data;
Borislav Petkovd236d742008-04-18 00:46:27 +02002724 struct ide_atapi_pc pc;
Borislav Petkov3cffb9c2008-02-02 19:56:50 +01002725
2726 idetape_create_mode_sense_cmd(&pc, IDETAPE_BLOCK_DESCRIPTOR);
2727 if (idetape_queue_pc_tail(drive, &pc)) {
2728 printk(KERN_ERR "ide-tape: Can't get block descriptor\n");
Borislav Petkov54bb2072008-02-06 02:57:52 +01002729 if (tape->blk_size == 0) {
Borislav Petkov3cffb9c2008-02-02 19:56:50 +01002730 printk(KERN_WARNING "ide-tape: Cannot deal with zero "
2731 "block size, assuming 32k\n");
Borislav Petkov54bb2072008-02-06 02:57:52 +01002732 tape->blk_size = 32768;
Borislav Petkov3cffb9c2008-02-02 19:56:50 +01002733 }
2734 return;
2735 }
Borislav Petkovd236d742008-04-18 00:46:27 +02002736 tape->blk_size = (pc.buf[4 + 5] << 16) +
2737 (pc.buf[4 + 6] << 8) +
2738 pc.buf[4 + 7];
2739 tape->drv_write_prot = (pc.buf[2] & 0x80) >> 7;
Borislav Petkov3cffb9c2008-02-02 19:56:50 +01002740}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002741
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002742static int idetape_chrdev_open(struct inode *inode, struct file *filp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002743{
2744 unsigned int minor = iminor(inode), i = minor & ~0xc0;
2745 ide_drive_t *drive;
2746 idetape_tape_t *tape;
Borislav Petkovd236d742008-04-18 00:46:27 +02002747 struct ide_atapi_pc pc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002748 int retval;
2749
Borislav Petkov8004a8c2008-02-06 02:57:51 +01002750 if (i >= MAX_HWIFS * MAX_DRIVES)
2751 return -ENXIO;
2752
2753 tape = ide_tape_chrdev_get(i);
2754 if (!tape)
2755 return -ENXIO;
2756
2757 debug_log(DBG_CHRDEV, "Enter %s\n", __func__);
2758
Linus Torvalds1da177e2005-04-16 15:20:36 -07002759 /*
2760 * We really want to do nonseekable_open(inode, filp); here, but some
2761 * versions of tar incorrectly call lseek on tapes and bail out if that
2762 * fails. So we disallow pread() and pwrite(), but permit lseeks.
2763 */
2764 filp->f_mode &= ~(FMODE_PREAD | FMODE_PWRITE);
2765
Linus Torvalds1da177e2005-04-16 15:20:36 -07002766 drive = tape->drive;
2767
2768 filp->private_data = tape;
2769
Borislav Petkov346331f2008-04-18 00:46:26 +02002770 if (test_and_set_bit(IDETAPE_FLAG_BUSY, &tape->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002771 retval = -EBUSY;
2772 goto out_put_tape;
2773 }
2774
2775 retval = idetape_wait_ready(drive, 60 * HZ);
2776 if (retval) {
Borislav Petkov346331f2008-04-18 00:46:26 +02002777 clear_bit(IDETAPE_FLAG_BUSY, &tape->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002778 printk(KERN_ERR "ide-tape: %s: drive not ready\n", tape->name);
2779 goto out_put_tape;
2780 }
2781
2782 idetape_read_position(drive);
Borislav Petkov346331f2008-04-18 00:46:26 +02002783 if (!test_bit(IDETAPE_FLAG_ADDRESS_VALID, &tape->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002784 (void)idetape_rewind_tape(drive);
2785
Borislav Petkov54abf372008-02-06 02:57:52 +01002786 if (tape->chrdev_dir != IDETAPE_DIR_READ)
Borislav Petkov346331f2008-04-18 00:46:26 +02002787 clear_bit(IDETAPE_FLAG_PIPELINE_ERR, &tape->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002788
2789 /* Read block size and write protect status from drive. */
Borislav Petkov3cffb9c2008-02-02 19:56:50 +01002790 ide_tape_get_bsize_from_bdesc(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002791
2792 /* Set write protect flag if device is opened as read-only. */
2793 if ((filp->f_flags & O_ACCMODE) == O_RDONLY)
2794 tape->write_prot = 1;
2795 else
2796 tape->write_prot = tape->drv_write_prot;
2797
2798 /* Make sure drive isn't write protected if user wants to write. */
2799 if (tape->write_prot) {
2800 if ((filp->f_flags & O_ACCMODE) == O_WRONLY ||
2801 (filp->f_flags & O_ACCMODE) == O_RDWR) {
Borislav Petkov346331f2008-04-18 00:46:26 +02002802 clear_bit(IDETAPE_FLAG_BUSY, &tape->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002803 retval = -EROFS;
2804 goto out_put_tape;
2805 }
2806 }
2807
Borislav Petkov3c98bf32008-02-06 02:57:53 +01002808 /* Lock the tape drive door so user can't eject. */
Borislav Petkov54abf372008-02-06 02:57:52 +01002809 if (tape->chrdev_dir == IDETAPE_DIR_NONE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002810 if (idetape_create_prevent_cmd(drive, &pc, 1)) {
2811 if (!idetape_queue_pc_tail(drive, &pc)) {
2812 if (tape->door_locked != DOOR_EXPLICITLY_LOCKED)
2813 tape->door_locked = DOOR_LOCKED;
2814 }
2815 }
2816 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002817 return 0;
2818
2819out_put_tape:
2820 ide_tape_put(tape);
2821 return retval;
2822}
2823
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002824static void idetape_write_release(ide_drive_t *drive, unsigned int minor)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002825{
2826 idetape_tape_t *tape = drive->driver_data;
2827
2828 idetape_empty_write_pipeline(drive);
2829 tape->merge_stage = __idetape_kmalloc_stage(tape, 1, 0);
2830 if (tape->merge_stage != NULL) {
Borislav Petkov54bb2072008-02-06 02:57:52 +01002831 idetape_pad_zeros(drive, tape->blk_size *
2832 (tape->user_bs_factor - 1));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002833 __idetape_kfree_stage(tape->merge_stage);
2834 tape->merge_stage = NULL;
2835 }
2836 idetape_write_filemark(drive);
2837 idetape_flush_tape_buffers(drive);
2838 idetape_flush_tape_buffers(drive);
2839}
2840
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002841static int idetape_chrdev_release(struct inode *inode, struct file *filp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002842{
2843 struct ide_tape_obj *tape = ide_tape_f(filp);
2844 ide_drive_t *drive = tape->drive;
Borislav Petkovd236d742008-04-18 00:46:27 +02002845 struct ide_atapi_pc pc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002846 unsigned int minor = iminor(inode);
2847
2848 lock_kernel();
2849 tape = drive->driver_data;
Borislav Petkov8004a8c2008-02-06 02:57:51 +01002850
2851 debug_log(DBG_CHRDEV, "Enter %s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002852
Borislav Petkov54abf372008-02-06 02:57:52 +01002853 if (tape->chrdev_dir == IDETAPE_DIR_WRITE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002854 idetape_write_release(drive, minor);
Borislav Petkov54abf372008-02-06 02:57:52 +01002855 if (tape->chrdev_dir == IDETAPE_DIR_READ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002856 if (minor < 128)
2857 idetape_discard_read_pipeline(drive, 1);
2858 else
2859 idetape_wait_for_pipeline(drive);
2860 }
Borislav Petkovf64eee72008-04-27 15:38:25 +02002861
Borislav Petkov346331f2008-04-18 00:46:26 +02002862 if (minor < 128 && test_bit(IDETAPE_FLAG_MEDIUM_PRESENT, &tape->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002863 (void) idetape_rewind_tape(drive);
Borislav Petkov54abf372008-02-06 02:57:52 +01002864 if (tape->chrdev_dir == IDETAPE_DIR_NONE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002865 if (tape->door_locked == DOOR_LOCKED) {
2866 if (idetape_create_prevent_cmd(drive, &pc, 0)) {
2867 if (!idetape_queue_pc_tail(drive, &pc))
2868 tape->door_locked = DOOR_UNLOCKED;
2869 }
2870 }
2871 }
Borislav Petkov346331f2008-04-18 00:46:26 +02002872 clear_bit(IDETAPE_FLAG_BUSY, &tape->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002873 ide_tape_put(tape);
2874 unlock_kernel();
2875 return 0;
2876}
2877
2878/*
Borislav Petkov71071b82008-02-06 02:57:53 +01002879 * check the contents of the ATAPI IDENTIFY command results. We return:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002880 *
Borislav Petkov71071b82008-02-06 02:57:53 +01002881 * 1 - If the tape can be supported by us, based on the information we have so
2882 * far.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002883 *
Borislav Petkov71071b82008-02-06 02:57:53 +01002884 * 0 - If this tape driver is not currently supported by us.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002885 */
Borislav Petkov71071b82008-02-06 02:57:53 +01002886static int idetape_identify_device(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002887{
Borislav Petkov71071b82008-02-06 02:57:53 +01002888 u8 gcw[2], protocol, device_type, removable, packet_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002889
2890 if (drive->id_read == 0)
2891 return 1;
2892
Borislav Petkov71071b82008-02-06 02:57:53 +01002893 *((unsigned short *) &gcw) = drive->id->config;
2894
2895 protocol = (gcw[1] & 0xC0) >> 6;
2896 device_type = gcw[1] & 0x1F;
2897 removable = !!(gcw[0] & 0x80);
2898 packet_size = gcw[0] & 0x3;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002899
Linus Torvalds1da177e2005-04-16 15:20:36 -07002900 /* Check that we can support this device */
Borislav Petkov71071b82008-02-06 02:57:53 +01002901 if (protocol != 2)
Bartlomiej Zolnierkiewicz16422de32008-02-02 19:56:48 +01002902 printk(KERN_ERR "ide-tape: Protocol (0x%02x) is not ATAPI\n",
Borislav Petkov71071b82008-02-06 02:57:53 +01002903 protocol);
2904 else if (device_type != 1)
Bartlomiej Zolnierkiewicz16422de32008-02-02 19:56:48 +01002905 printk(KERN_ERR "ide-tape: Device type (0x%02x) is not set "
Borislav Petkov71071b82008-02-06 02:57:53 +01002906 "to tape\n", device_type);
2907 else if (!removable)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002908 printk(KERN_ERR "ide-tape: The removable flag is not set\n");
Borislav Petkov71071b82008-02-06 02:57:53 +01002909 else if (packet_size != 0) {
Borislav Petkov24d57f82008-02-06 02:57:54 +01002910 printk(KERN_ERR "ide-tape: Packet size (0x%02x) is not 12"
2911 " bytes\n", packet_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002912 } else
2913 return 1;
2914 return 0;
2915}
2916
Borislav Petkov6d29c8f2008-02-02 19:56:49 +01002917static void idetape_get_inquiry_results(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002918{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002919 idetape_tape_t *tape = drive->driver_data;
Borislav Petkovd236d742008-04-18 00:46:27 +02002920 struct ide_atapi_pc pc;
Borislav Petkov41f81d542008-02-06 02:57:52 +01002921 char fw_rev[6], vendor_id[10], product_id[18];
Borislav Petkov6d29c8f2008-02-02 19:56:49 +01002922
Linus Torvalds1da177e2005-04-16 15:20:36 -07002923 idetape_create_inquiry_cmd(&pc);
2924 if (idetape_queue_pc_tail(drive, &pc)) {
Borislav Petkov6d29c8f2008-02-02 19:56:49 +01002925 printk(KERN_ERR "ide-tape: %s: can't get INQUIRY results\n",
2926 tape->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002927 return;
2928 }
Borislav Petkovd236d742008-04-18 00:46:27 +02002929 memcpy(vendor_id, &pc.buf[8], 8);
2930 memcpy(product_id, &pc.buf[16], 16);
2931 memcpy(fw_rev, &pc.buf[32], 4);
Borislav Petkov6d29c8f2008-02-02 19:56:49 +01002932
Borislav Petkov41f81d542008-02-06 02:57:52 +01002933 ide_fixstring(vendor_id, 10, 0);
2934 ide_fixstring(product_id, 18, 0);
2935 ide_fixstring(fw_rev, 6, 0);
2936
Borislav Petkov6d29c8f2008-02-02 19:56:49 +01002937 printk(KERN_INFO "ide-tape: %s <-> %s: %s %s rev %s\n",
Borislav Petkov41f81d542008-02-06 02:57:52 +01002938 drive->name, tape->name, vendor_id, product_id, fw_rev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002939}
2940
2941/*
Borislav Petkovb6422012008-02-02 19:56:49 +01002942 * Ask the tape about its various parameters. In particular, we will adjust our
2943 * data transfer buffer size to the recommended value as returned by the tape.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002944 */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002945static void idetape_get_mode_sense_results(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002946{
2947 idetape_tape_t *tape = drive->driver_data;
Borislav Petkovd236d742008-04-18 00:46:27 +02002948 struct ide_atapi_pc pc;
Borislav Petkovb6422012008-02-02 19:56:49 +01002949 u8 *caps;
2950 u8 speed, max_speed;
Borislav Petkov47314fa2008-02-02 19:56:48 +01002951
Linus Torvalds1da177e2005-04-16 15:20:36 -07002952 idetape_create_mode_sense_cmd(&pc, IDETAPE_CAPABILITIES_PAGE);
2953 if (idetape_queue_pc_tail(drive, &pc)) {
Borislav Petkovb6422012008-02-02 19:56:49 +01002954 printk(KERN_ERR "ide-tape: Can't get tape parameters - assuming"
2955 " some default values\n");
Borislav Petkov54bb2072008-02-06 02:57:52 +01002956 tape->blk_size = 512;
Borislav Petkovb6422012008-02-02 19:56:49 +01002957 put_unaligned(52, (u16 *)&tape->caps[12]);
2958 put_unaligned(540, (u16 *)&tape->caps[14]);
2959 put_unaligned(6*52, (u16 *)&tape->caps[16]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002960 return;
2961 }
Borislav Petkovd236d742008-04-18 00:46:27 +02002962 caps = pc.buf + 4 + pc.buf[3];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002963
Borislav Petkovb6422012008-02-02 19:56:49 +01002964 /* convert to host order and save for later use */
2965 speed = be16_to_cpu(*(u16 *)&caps[14]);
2966 max_speed = be16_to_cpu(*(u16 *)&caps[8]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002967
Borislav Petkovb6422012008-02-02 19:56:49 +01002968 put_unaligned(max_speed, (u16 *)&caps[8]);
2969 put_unaligned(be16_to_cpu(*(u16 *)&caps[12]), (u16 *)&caps[12]);
2970 put_unaligned(speed, (u16 *)&caps[14]);
2971 put_unaligned(be16_to_cpu(*(u16 *)&caps[16]), (u16 *)&caps[16]);
2972
2973 if (!speed) {
2974 printk(KERN_INFO "ide-tape: %s: invalid tape speed "
2975 "(assuming 650KB/sec)\n", drive->name);
2976 put_unaligned(650, (u16 *)&caps[14]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002977 }
Borislav Petkovb6422012008-02-02 19:56:49 +01002978 if (!max_speed) {
2979 printk(KERN_INFO "ide-tape: %s: invalid max_speed "
2980 "(assuming 650KB/sec)\n", drive->name);
2981 put_unaligned(650, (u16 *)&caps[8]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002982 }
2983
Borislav Petkovb6422012008-02-02 19:56:49 +01002984 memcpy(&tape->caps, caps, 20);
2985 if (caps[7] & 0x02)
Borislav Petkov54bb2072008-02-06 02:57:52 +01002986 tape->blk_size = 512;
Borislav Petkovb6422012008-02-02 19:56:49 +01002987 else if (caps[7] & 0x04)
Borislav Petkov54bb2072008-02-06 02:57:52 +01002988 tape->blk_size = 1024;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002989}
2990
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +02002991#ifdef CONFIG_IDE_PROC_FS
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002992static void idetape_add_settings(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002993{
2994 idetape_tape_t *tape = drive->driver_data;
2995
Borislav Petkovb6422012008-02-02 19:56:49 +01002996 ide_add_setting(drive, "buffer", SETTING_READ, TYPE_SHORT, 0, 0xffff,
2997 1, 2, (u16 *)&tape->caps[16], NULL);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002998 ide_add_setting(drive, "pipeline_min", SETTING_RW, TYPE_INT, 1, 0xffff,
2999 tape->stage_size / 1024, 1, &tape->min_pipeline, NULL);
3000 ide_add_setting(drive, "pipeline", SETTING_RW, TYPE_INT, 1, 0xffff,
3001 tape->stage_size / 1024, 1, &tape->max_stages, NULL);
3002 ide_add_setting(drive, "pipeline_max", SETTING_RW, TYPE_INT, 1, 0xffff,
3003 tape->stage_size / 1024, 1, &tape->max_pipeline, NULL);
3004 ide_add_setting(drive, "pipeline_used", SETTING_READ, TYPE_INT, 0,
3005 0xffff, tape->stage_size / 1024, 1, &tape->nr_stages,
3006 NULL);
3007 ide_add_setting(drive, "pipeline_pending", SETTING_READ, TYPE_INT, 0,
3008 0xffff, tape->stage_size / 1024, 1,
3009 &tape->nr_pending_stages, NULL);
Borislav Petkovb6422012008-02-02 19:56:49 +01003010 ide_add_setting(drive, "speed", SETTING_READ, TYPE_SHORT, 0, 0xffff,
3011 1, 1, (u16 *)&tape->caps[14], NULL);
Borislav Petkov54bb2072008-02-06 02:57:52 +01003012 ide_add_setting(drive, "stage", SETTING_READ, TYPE_INT, 0, 0xffff, 1,
3013 1024, &tape->stage_size, NULL);
3014 ide_add_setting(drive, "tdsc", SETTING_RW, TYPE_INT, IDETAPE_DSC_RW_MIN,
3015 IDETAPE_DSC_RW_MAX, 1000, HZ, &tape->best_dsc_rw_freq,
3016 NULL);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01003017 ide_add_setting(drive, "dsc_overlap", SETTING_RW, TYPE_BYTE, 0, 1, 1,
3018 1, &drive->dsc_overlap, NULL);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01003019 ide_add_setting(drive, "avg_speed", SETTING_READ, TYPE_INT, 0, 0xffff,
3020 1, 1, &tape->avg_speed, NULL);
Borislav Petkov8004a8c2008-02-06 02:57:51 +01003021 ide_add_setting(drive, "debug_mask", SETTING_RW, TYPE_INT, 0, 0xffff, 1,
3022 1, &tape->debug_mask, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003023}
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +02003024#else
3025static inline void idetape_add_settings(ide_drive_t *drive) { ; }
3026#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003027
3028/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +01003029 * The function below is called to:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003030 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +01003031 * 1. Initialize our various state variables.
3032 * 2. Ask the tape for its capabilities.
3033 * 3. Allocate a buffer which will be used for data transfer. The buffer size
3034 * is chosen based on the recommendation which we received in step 2.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003035 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +01003036 * Note that at this point ide.c already assigned us an irq, so that we can
3037 * queue requests here and wait for their completion.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003038 */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01003039static void idetape_setup(ide_drive_t *drive, idetape_tape_t *tape, int minor)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003040{
3041 unsigned long t1, tmid, tn, t;
3042 int speed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003043 int stage_size;
Borislav Petkov71071b82008-02-06 02:57:53 +01003044 u8 gcw[2];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003045 struct sysinfo si;
Borislav Petkovb6422012008-02-02 19:56:49 +01003046 u16 *ctl = (u16 *)&tape->caps[12];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003047
Borislav Petkov54bb2072008-02-06 02:57:52 +01003048 spin_lock_init(&tape->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003049 drive->dsc_overlap = 1;
Bartlomiej Zolnierkiewicz4166c192008-02-01 23:09:30 +01003050 if (drive->hwif->host_flags & IDE_HFLAG_NO_DSC) {
3051 printk(KERN_INFO "ide-tape: %s: disabling DSC overlap\n",
3052 tape->name);
3053 drive->dsc_overlap = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003054 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003055 /* Seagate Travan drives do not support DSC overlap. */
3056 if (strstr(drive->id->model, "Seagate STT3401"))
3057 drive->dsc_overlap = 0;
3058 tape->minor = minor;
3059 tape->name[0] = 'h';
3060 tape->name[1] = 't';
3061 tape->name[2] = '0' + minor;
Borislav Petkov54abf372008-02-06 02:57:52 +01003062 tape->chrdev_dir = IDETAPE_DIR_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003063 tape->pc = tape->pc_stack;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003064 *((unsigned short *) &gcw) = drive->id->config;
Borislav Petkov71071b82008-02-06 02:57:53 +01003065
3066 /* Command packet DRQ type */
3067 if (((gcw[0] & 0x60) >> 5) == 1)
Borislav Petkov346331f2008-04-18 00:46:26 +02003068 set_bit(IDETAPE_FLAG_DRQ_INTERRUPT, &tape->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003069
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01003070 tape->min_pipeline = 10;
3071 tape->max_pipeline = 10;
3072 tape->max_stages = 10;
Borislav Petkov3c98bf32008-02-06 02:57:53 +01003073
Linus Torvalds1da177e2005-04-16 15:20:36 -07003074 idetape_get_inquiry_results(drive);
3075 idetape_get_mode_sense_results(drive);
Borislav Petkov3cffb9c2008-02-02 19:56:50 +01003076 ide_tape_get_bsize_from_bdesc(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003077 tape->user_bs_factor = 1;
Borislav Petkov54bb2072008-02-06 02:57:52 +01003078 tape->stage_size = *ctl * tape->blk_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003079 while (tape->stage_size > 0xffff) {
3080 printk(KERN_NOTICE "ide-tape: decreasing stage size\n");
Borislav Petkovb6422012008-02-02 19:56:49 +01003081 *ctl /= 2;
Borislav Petkov54bb2072008-02-06 02:57:52 +01003082 tape->stage_size = *ctl * tape->blk_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003083 }
3084 stage_size = tape->stage_size;
3085 tape->pages_per_stage = stage_size / PAGE_SIZE;
3086 if (stage_size % PAGE_SIZE) {
3087 tape->pages_per_stage++;
3088 tape->excess_bh_size = PAGE_SIZE - stage_size % PAGE_SIZE;
3089 }
3090
Borislav Petkovb6422012008-02-02 19:56:49 +01003091 /* Select the "best" DSC read/write polling freq and pipeline size. */
3092 speed = max(*(u16 *)&tape->caps[14], *(u16 *)&tape->caps[8]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003093
3094 tape->max_stages = speed * 1000 * 10 / tape->stage_size;
3095
Borislav Petkov3c98bf32008-02-06 02:57:53 +01003096 /* Limit memory use for pipeline to 10% of physical memory */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003097 si_meminfo(&si);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01003098 if (tape->max_stages * tape->stage_size >
3099 si.totalram * si.mem_unit / 10)
3100 tape->max_stages =
3101 si.totalram * si.mem_unit / (10 * tape->stage_size);
3102
Linus Torvalds1da177e2005-04-16 15:20:36 -07003103 tape->max_stages = min(tape->max_stages, IDETAPE_MAX_PIPELINE_STAGES);
3104 tape->min_pipeline = min(tape->max_stages, IDETAPE_MIN_PIPELINE_STAGES);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01003105 tape->max_pipeline =
3106 min(tape->max_stages * 2, IDETAPE_MAX_PIPELINE_STAGES);
3107 if (tape->max_stages == 0) {
3108 tape->max_stages = 1;
3109 tape->min_pipeline = 1;
3110 tape->max_pipeline = 1;
3111 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003112
3113 t1 = (tape->stage_size * HZ) / (speed * 1000);
Borislav Petkovb6422012008-02-02 19:56:49 +01003114 tmid = (*(u16 *)&tape->caps[16] * 32 * HZ) / (speed * 125);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003115 tn = (IDETAPE_FIFO_THRESHOLD * tape->stage_size * HZ) / (speed * 1000);
3116
3117 if (tape->max_stages)
3118 t = tn;
3119 else
3120 t = t1;
3121
3122 /*
Borislav Petkov3c98bf32008-02-06 02:57:53 +01003123 * Ensure that the number we got makes sense; limit it within
3124 * IDETAPE_DSC_RW_MIN and IDETAPE_DSC_RW_MAX.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003125 */
Borislav Petkov54bb2072008-02-06 02:57:52 +01003126 tape->best_dsc_rw_freq = max_t(unsigned long,
3127 min_t(unsigned long, t, IDETAPE_DSC_RW_MAX),
3128 IDETAPE_DSC_RW_MIN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003129 printk(KERN_INFO "ide-tape: %s <-> %s: %dKBps, %d*%dkB buffer, "
3130 "%dkB pipeline, %lums tDSC%s\n",
Borislav Petkovb6422012008-02-02 19:56:49 +01003131 drive->name, tape->name, *(u16 *)&tape->caps[14],
3132 (*(u16 *)&tape->caps[16] * 512) / tape->stage_size,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003133 tape->stage_size / 1024,
3134 tape->max_stages * tape->stage_size / 1024,
Borislav Petkov54bb2072008-02-06 02:57:52 +01003135 tape->best_dsc_rw_freq * 1000 / HZ,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003136 drive->using_dma ? ", DMA":"");
3137
3138 idetape_add_settings(drive);
3139}
3140
Russell King4031bbe2006-01-06 11:41:00 +00003141static void ide_tape_remove(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003142{
3143 idetape_tape_t *tape = drive->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003144
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +02003145 ide_proc_unregister_driver(drive, tape->driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003146
3147 ide_unregister_region(tape->disk);
3148
3149 ide_tape_put(tape);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003150}
3151
3152static void ide_tape_release(struct kref *kref)
3153{
3154 struct ide_tape_obj *tape = to_ide_tape(kref);
3155 ide_drive_t *drive = tape->drive;
3156 struct gendisk *g = tape->disk;
3157
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02003158 BUG_ON(tape->first_stage != NULL || tape->merge_stage_size);
3159
Linus Torvalds1da177e2005-04-16 15:20:36 -07003160 drive->dsc_overlap = 0;
3161 drive->driver_data = NULL;
Tony Jonesdbc12722007-09-25 02:03:03 +02003162 device_destroy(idetape_sysfs_class, MKDEV(IDETAPE_MAJOR, tape->minor));
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01003163 device_destroy(idetape_sysfs_class,
3164 MKDEV(IDETAPE_MAJOR, tape->minor + 128));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003165 idetape_devs[tape->minor] = NULL;
3166 g->private_data = NULL;
3167 put_disk(g);
3168 kfree(tape);
3169}
3170
Bartlomiej Zolnierkiewiczecfd80e2007-05-10 00:01:09 +02003171#ifdef CONFIG_IDE_PROC_FS
Linus Torvalds1da177e2005-04-16 15:20:36 -07003172static int proc_idetape_read_name
3173 (char *page, char **start, off_t off, int count, int *eof, void *data)
3174{
3175 ide_drive_t *drive = (ide_drive_t *) data;
3176 idetape_tape_t *tape = drive->driver_data;
3177 char *out = page;
3178 int len;
3179
3180 len = sprintf(out, "%s\n", tape->name);
3181 PROC_IDE_READ_RETURN(page, start, off, count, eof, len);
3182}
3183
3184static ide_proc_entry_t idetape_proc[] = {
3185 { "capacity", S_IFREG|S_IRUGO, proc_ide_read_capacity, NULL },
3186 { "name", S_IFREG|S_IRUGO, proc_idetape_read_name, NULL },
3187 { NULL, 0, NULL, NULL }
3188};
Linus Torvalds1da177e2005-04-16 15:20:36 -07003189#endif
3190
Russell King4031bbe2006-01-06 11:41:00 +00003191static int ide_tape_probe(ide_drive_t *);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003192
Linus Torvalds1da177e2005-04-16 15:20:36 -07003193static ide_driver_t idetape_driver = {
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02003194 .gen_driver = {
Laurent Riffard4ef3b8f2005-11-18 22:15:40 +01003195 .owner = THIS_MODULE,
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02003196 .name = "ide-tape",
3197 .bus = &ide_bus_type,
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02003198 },
Russell King4031bbe2006-01-06 11:41:00 +00003199 .probe = ide_tape_probe,
3200 .remove = ide_tape_remove,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003201 .version = IDETAPE_VERSION,
3202 .media = ide_tape,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003203 .supports_dsc_overlap = 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003204 .do_request = idetape_do_request,
3205 .end_request = idetape_end_request,
3206 .error = __ide_error,
3207 .abort = __ide_abort,
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +02003208#ifdef CONFIG_IDE_PROC_FS
Linus Torvalds1da177e2005-04-16 15:20:36 -07003209 .proc = idetape_proc,
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +02003210#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003211};
3212
Borislav Petkov3c98bf32008-02-06 02:57:53 +01003213/* Our character device supporting functions, passed to register_chrdev. */
Arjan van de Ven2b8693c2007-02-12 00:55:32 -08003214static const struct file_operations idetape_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003215 .owner = THIS_MODULE,
3216 .read = idetape_chrdev_read,
3217 .write = idetape_chrdev_write,
3218 .ioctl = idetape_chrdev_ioctl,
3219 .open = idetape_chrdev_open,
3220 .release = idetape_chrdev_release,
3221};
3222
3223static int idetape_open(struct inode *inode, struct file *filp)
3224{
3225 struct gendisk *disk = inode->i_bdev->bd_disk;
3226 struct ide_tape_obj *tape;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003227
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01003228 tape = ide_tape_get(disk);
3229 if (!tape)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003230 return -ENXIO;
3231
Linus Torvalds1da177e2005-04-16 15:20:36 -07003232 return 0;
3233}
3234
3235static int idetape_release(struct inode *inode, struct file *filp)
3236{
3237 struct gendisk *disk = inode->i_bdev->bd_disk;
3238 struct ide_tape_obj *tape = ide_tape_g(disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003239
3240 ide_tape_put(tape);
3241
3242 return 0;
3243}
3244
3245static int idetape_ioctl(struct inode *inode, struct file *file,
3246 unsigned int cmd, unsigned long arg)
3247{
3248 struct block_device *bdev = inode->i_bdev;
3249 struct ide_tape_obj *tape = ide_tape_g(bdev->bd_disk);
3250 ide_drive_t *drive = tape->drive;
3251 int err = generic_ide_ioctl(drive, file, bdev, cmd, arg);
3252 if (err == -EINVAL)
3253 err = idetape_blkdev_ioctl(drive, cmd, arg);
3254 return err;
3255}
3256
3257static struct block_device_operations idetape_block_ops = {
3258 .owner = THIS_MODULE,
3259 .open = idetape_open,
3260 .release = idetape_release,
3261 .ioctl = idetape_ioctl,
3262};
3263
Russell King4031bbe2006-01-06 11:41:00 +00003264static int ide_tape_probe(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003265{
3266 idetape_tape_t *tape;
3267 struct gendisk *g;
3268 int minor;
3269
3270 if (!strstr("ide-tape", drive->driver_req))
3271 goto failed;
3272 if (!drive->present)
3273 goto failed;
3274 if (drive->media != ide_tape)
3275 goto failed;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01003276 if (!idetape_identify_device(drive)) {
3277 printk(KERN_ERR "ide-tape: %s: not supported by this version of"
3278 " the driver\n", drive->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003279 goto failed;
3280 }
3281 if (drive->scsi) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01003282 printk(KERN_INFO "ide-tape: passing drive %s to ide-scsi"
3283 " emulation.\n", drive->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003284 goto failed;
3285 }
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01003286 tape = kzalloc(sizeof(idetape_tape_t), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003287 if (tape == NULL) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01003288 printk(KERN_ERR "ide-tape: %s: Can't allocate a tape struct\n",
3289 drive->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003290 goto failed;
3291 }
3292
3293 g = alloc_disk(1 << PARTN_BITS);
3294 if (!g)
3295 goto out_free_tape;
3296
3297 ide_init_disk(g, drive);
3298
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +02003299 ide_proc_register_driver(drive, &idetape_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003300
Linus Torvalds1da177e2005-04-16 15:20:36 -07003301 kref_init(&tape->kref);
3302
3303 tape->drive = drive;
3304 tape->driver = &idetape_driver;
3305 tape->disk = g;
3306
3307 g->private_data = &tape->driver;
3308
3309 drive->driver_data = tape;
3310
Arjan van de Vencf8b8972006-03-23 03:00:45 -08003311 mutex_lock(&idetape_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003312 for (minor = 0; idetape_devs[minor]; minor++)
3313 ;
3314 idetape_devs[minor] = tape;
Arjan van de Vencf8b8972006-03-23 03:00:45 -08003315 mutex_unlock(&idetape_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003316
3317 idetape_setup(drive, tape, minor);
3318
Tony Jonesdbc12722007-09-25 02:03:03 +02003319 device_create(idetape_sysfs_class, &drive->gendev,
3320 MKDEV(IDETAPE_MAJOR, minor), "%s", tape->name);
3321 device_create(idetape_sysfs_class, &drive->gendev,
3322 MKDEV(IDETAPE_MAJOR, minor + 128), "n%s", tape->name);
Will Dysond5dee802005-09-16 02:55:07 -07003323
Linus Torvalds1da177e2005-04-16 15:20:36 -07003324 g->fops = &idetape_block_ops;
3325 ide_register_region(g);
3326
3327 return 0;
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02003328
Linus Torvalds1da177e2005-04-16 15:20:36 -07003329out_free_tape:
3330 kfree(tape);
3331failed:
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02003332 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003333}
3334
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01003335static void __exit idetape_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003336{
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02003337 driver_unregister(&idetape_driver.gen_driver);
Will Dysond5dee802005-09-16 02:55:07 -07003338 class_destroy(idetape_sysfs_class);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003339 unregister_chrdev(IDETAPE_MAJOR, "ht");
3340}
3341
Bartlomiej Zolnierkiewicz17514e82005-11-19 22:24:35 +01003342static int __init idetape_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003343{
Will Dysond5dee802005-09-16 02:55:07 -07003344 int error = 1;
3345 idetape_sysfs_class = class_create(THIS_MODULE, "ide_tape");
3346 if (IS_ERR(idetape_sysfs_class)) {
3347 idetape_sysfs_class = NULL;
3348 printk(KERN_ERR "Unable to create sysfs class for ide tapes\n");
3349 error = -EBUSY;
3350 goto out;
3351 }
3352
Linus Torvalds1da177e2005-04-16 15:20:36 -07003353 if (register_chrdev(IDETAPE_MAJOR, "ht", &idetape_fops)) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01003354 printk(KERN_ERR "ide-tape: Failed to register chrdev"
3355 " interface\n");
Will Dysond5dee802005-09-16 02:55:07 -07003356 error = -EBUSY;
3357 goto out_free_class;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003358 }
Will Dysond5dee802005-09-16 02:55:07 -07003359
3360 error = driver_register(&idetape_driver.gen_driver);
3361 if (error)
3362 goto out_free_driver;
3363
3364 return 0;
3365
3366out_free_driver:
3367 driver_unregister(&idetape_driver.gen_driver);
3368out_free_class:
3369 class_destroy(idetape_sysfs_class);
3370out:
3371 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003372}
3373
Kay Sievers263756e2005-12-12 18:03:44 +01003374MODULE_ALIAS("ide:*m-tape*");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003375module_init(idetape_init);
3376module_exit(idetape_exit);
3377MODULE_ALIAS_CHARDEV_MAJOR(IDETAPE_MAJOR);
Borislav Petkov9c145762008-02-06 02:57:54 +01003378MODULE_DESCRIPTION("ATAPI Streaming TAPE Driver");
3379MODULE_LICENSE("GPL");