blob: 8883eea4658f4411b21fb49d8fe3b184e670cb12 [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 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +010088 * Setting the following parameter to 0 is illegal: the pipelined mode cannot be
89 * disabled (idetape_calculate_speeds() divides by tape->max_stages.)
Linus Torvalds1da177e2005-04-16 15:20:36 -070090 */
91#define IDETAPE_MIN_PIPELINE_STAGES 1
92#define IDETAPE_MAX_PIPELINE_STAGES 400
93#define IDETAPE_INCREASE_STAGES_RATE 20
94
95/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +010096 * After each failed packet command we issue a request sense command and retry
97 * the packet command IDETAPE_MAX_PC_RETRIES times.
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +010099 * Setting IDETAPE_MAX_PC_RETRIES to 0 will disable retries.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100 */
101#define IDETAPE_MAX_PC_RETRIES 3
102
103/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100104 * With each packet command, we allocate a buffer of IDETAPE_PC_BUFFER_SIZE
105 * bytes. This is used for several packet commands (Not for READ/WRITE commands)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 */
107#define IDETAPE_PC_BUFFER_SIZE 256
108
109/*
110 * In various places in the driver, we need to allocate storage
111 * for packet commands and requests, which will remain valid while
112 * we leave the driver to wait for an interrupt or a timeout event.
113 */
114#define IDETAPE_PC_STACK (10 + IDETAPE_MAX_PC_RETRIES)
115
116/*
117 * Some drives (for example, Seagate STT3401A Travan) require a very long
118 * timeout, because they don't return an interrupt or clear their busy bit
119 * until after the command completes (even retension commands).
120 */
121#define IDETAPE_WAIT_CMD (900*HZ)
122
123/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100124 * The following parameter is used to select the point in the internal tape fifo
125 * in which we will start to refill the buffer. Decreasing the following
126 * parameter will improve the system's latency and interactive response, while
127 * using a high value might improve system throughput.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 */
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100129#define IDETAPE_FIFO_THRESHOLD 2
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130
131/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100132 * DSC polling parameters.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100134 * Polling for DSC (a single bit in the status register) is a very important
135 * function in ide-tape. There are two cases in which we poll for DSC:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100137 * 1. Before a read/write packet command, to ensure that we can transfer data
138 * from/to the tape's data buffers, without causing an actual media access.
139 * In case the tape is not ready yet, we take out our request from the device
140 * request queue, so that ide.c could service requests from the other device
141 * on the same interface in the meantime.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100143 * 2. After the successful initialization of a "media access packet command",
144 * which is a command that can take a long time to complete (the interval can
145 * range from several seconds to even an hour). Again, we postpone our request
146 * in the middle to free the bus for the other device. The polling frequency
147 * here should be lower than the read/write frequency since those media access
148 * commands are slow. We start from a "fast" frequency - IDETAPE_DSC_MA_FAST
149 * (1 second), and if we don't receive DSC after IDETAPE_DSC_MA_THRESHOLD
150 * (5 min), we switch it to a lower frequency - IDETAPE_DSC_MA_SLOW (1 min).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100152 * We also set a timeout for the timer, in case something goes wrong. The
153 * timeout should be longer then the maximum execution time of a tape operation.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 */
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100155
156/* DSC timings. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157#define IDETAPE_DSC_RW_MIN 5*HZ/100 /* 50 msec */
158#define IDETAPE_DSC_RW_MAX 40*HZ/100 /* 400 msec */
159#define IDETAPE_DSC_RW_TIMEOUT 2*60*HZ /* 2 minutes */
160#define IDETAPE_DSC_MA_FAST 2*HZ /* 2 seconds */
161#define IDETAPE_DSC_MA_THRESHOLD 5*60*HZ /* 5 minutes */
162#define IDETAPE_DSC_MA_SLOW 30*HZ /* 30 seconds */
163#define IDETAPE_DSC_MA_TIMEOUT 2*60*60*HZ /* 2 hours */
164
165/*************************** End of tunable parameters ***********************/
166
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100167/* Read/Write error simulation */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168#define SIMULATE_ERRORS 0
169
Borislav Petkov54abf372008-02-06 02:57:52 +0100170/* tape directions */
171enum {
172 IDETAPE_DIR_NONE = (1 << 0),
173 IDETAPE_DIR_READ = (1 << 1),
174 IDETAPE_DIR_WRITE = (1 << 2),
175};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176
177struct idetape_bh {
Stephen Rothwellab057962007-08-01 23:46:44 +0200178 u32 b_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 atomic_t b_count;
180 struct idetape_bh *b_reqnext;
181 char *b_data;
182};
183
Borislav Petkov03056b92008-04-18 00:46:26 +0200184/* Tape door status */
185#define DOOR_UNLOCKED 0
186#define DOOR_LOCKED 1
187#define DOOR_EXPLICITLY_LOCKED 2
188
189/* Some defines for the SPACE command */
190#define IDETAPE_SPACE_OVER_FILEMARK 1
191#define IDETAPE_SPACE_TO_EOD 3
192
193/* Some defines for the LOAD UNLOAD command */
194#define IDETAPE_LU_LOAD_MASK 1
195#define IDETAPE_LU_RETENSION_MASK 2
196#define IDETAPE_LU_EOT_MASK 4
197
198/*
199 * Special requests for our block device strategy routine.
200 *
201 * In order to service a character device command, we add special requests to
202 * the tail of our block device request queue and wait for their completion.
203 */
204
205enum {
206 REQ_IDETAPE_PC1 = (1 << 0), /* packet command (first stage) */
207 REQ_IDETAPE_PC2 = (1 << 1), /* packet command (second stage) */
208 REQ_IDETAPE_READ = (1 << 2),
209 REQ_IDETAPE_WRITE = (1 << 3),
210};
211
212/* Error codes returned in rq->errors to the higher part of the driver. */
213#define IDETAPE_ERROR_GENERAL 101
214#define IDETAPE_ERROR_FILEMARK 102
215#define IDETAPE_ERROR_EOD 103
216
217/* Structures related to the SELECT SENSE / MODE SENSE packet commands. */
218#define IDETAPE_BLOCK_DESCRIPTOR 0
219#define IDETAPE_CAPABILITIES_PAGE 0x2a
220
Borislav Petkov346331f2008-04-18 00:46:26 +0200221/* Tape flag bits values. */
222enum {
223 IDETAPE_FLAG_IGNORE_DSC = (1 << 0),
224 /* 0 When the tape position is unknown */
225 IDETAPE_FLAG_ADDRESS_VALID = (1 << 1),
226 /* Device already opened */
227 IDETAPE_FLAG_BUSY = (1 << 2),
228 /* Error detected in a pipeline stage */
229 IDETAPE_FLAG_PIPELINE_ERR = (1 << 3),
230 /* Attempt to auto-detect the current user block size */
231 IDETAPE_FLAG_DETECT_BS = (1 << 4),
232 /* Currently on a filemark */
233 IDETAPE_FLAG_FILEMARK = (1 << 5),
234 /* DRQ interrupt device */
235 IDETAPE_FLAG_DRQ_INTERRUPT = (1 << 6),
236 /* pipeline active */
237 IDETAPE_FLAG_PIPELINE_ACTIVE = (1 << 7),
238 /* 0 = no tape is loaded, so we don't rewind after ejecting */
239 IDETAPE_FLAG_MEDIUM_PRESENT = (1 << 8),
240};
241
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100242/* A pipeline stage. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243typedef struct idetape_stage_s {
244 struct request rq; /* The corresponding request */
245 struct idetape_bh *bh; /* The data buffers */
246 struct idetape_stage_s *next; /* Pointer to the next stage */
247} idetape_stage_t;
248
249/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100250 * Most of our global data which we need to save even as we leave the driver due
251 * to an interrupt or a timer event is stored in the struct defined below.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 */
253typedef struct ide_tape_obj {
254 ide_drive_t *drive;
255 ide_driver_t *driver;
256 struct gendisk *disk;
257 struct kref kref;
258
259 /*
260 * Since a typical character device operation requires more
261 * than one packet command, we provide here enough memory
262 * for the maximum of interconnected packet commands.
263 * The packet commands are stored in the circular array pc_stack.
264 * pc_stack_index points to the last used entry, and warps around
265 * to the start when we get to the last array entry.
266 *
267 * pc points to the current processed packet command.
268 *
269 * failed_pc points to the last failed packet command, or contains
270 * NULL if we do not need to retry any packet command. This is
271 * required since an additional packet command is needed before the
272 * retry, to get detailed information on what went wrong.
273 */
274 /* Current packet command */
Borislav Petkovd236d742008-04-18 00:46:27 +0200275 struct ide_atapi_pc *pc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 /* Last failed packet command */
Borislav Petkovd236d742008-04-18 00:46:27 +0200277 struct ide_atapi_pc *failed_pc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 /* Packet command stack */
Borislav Petkovd236d742008-04-18 00:46:27 +0200279 struct ide_atapi_pc pc_stack[IDETAPE_PC_STACK];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 /* Next free packet command storage space */
281 int pc_stack_index;
282 struct request rq_stack[IDETAPE_PC_STACK];
283 /* We implement a circular array */
284 int rq_stack_index;
285
286 /*
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100287 * DSC polling variables.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100289 * While polling for DSC we use postponed_rq to postpone the current
290 * request so that ide.c will be able to service pending requests on the
291 * other device. Note that at most we will have only one DSC (usually
292 * data transfer) request in the device request queue. Additional
293 * requests can be queued in our internal pipeline, but they will be
294 * visible to ide.c only one at a time.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 */
296 struct request *postponed_rq;
297 /* The time in which we started polling for DSC */
298 unsigned long dsc_polling_start;
299 /* Timer used to poll for dsc */
300 struct timer_list dsc_timer;
301 /* Read/Write dsc polling frequency */
Borislav Petkov54bb2072008-02-06 02:57:52 +0100302 unsigned long best_dsc_rw_freq;
303 unsigned long dsc_poll_freq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 unsigned long dsc_timeout;
305
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100306 /* Read position information */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 u8 partition;
308 /* Current block */
Borislav Petkov54bb2072008-02-06 02:57:52 +0100309 unsigned int first_frame;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100311 /* Last error information */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 u8 sense_key, asc, ascq;
313
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100314 /* Character device operation */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 unsigned int minor;
316 /* device name */
317 char name[4];
318 /* Current character device data transfer direction */
Borislav Petkov54abf372008-02-06 02:57:52 +0100319 u8 chrdev_dir;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320
Borislav Petkov54bb2072008-02-06 02:57:52 +0100321 /* tape block size, usually 512 or 1024 bytes */
322 unsigned short blk_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 int user_bs_factor;
Borislav Petkovb6422012008-02-02 19:56:49 +0100324
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 /* Copy of the tape's Capabilities and Mechanical Page */
Borislav Petkovb6422012008-02-02 19:56:49 +0100326 u8 caps[20];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327
328 /*
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100329 * Active data transfer request parameters.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100331 * At most, there is only one ide-tape originated data transfer request
332 * in the device request queue. This allows ide.c to easily service
333 * requests from the other device when we postpone our active request.
334 * In the pipelined operation mode, we use our internal pipeline
335 * structure to hold more data requests. The data buffer size is chosen
336 * based on the tape's recommendation.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 */
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100338 /* ptr to the request which is waiting in the device request queue */
Borislav Petkov54bb2072008-02-06 02:57:52 +0100339 struct request *active_data_rq;
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100340 /* Data buffer size chosen based on the tape's recommendation */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 int stage_size;
342 idetape_stage_t *merge_stage;
343 int merge_stage_size;
344 struct idetape_bh *bh;
345 char *b_data;
346 int b_count;
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100347
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 /*
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100349 * Pipeline parameters.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100351 * To accomplish non-pipelined mode, we simply set the following
352 * variables to zero (or NULL, where appropriate).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 */
354 /* Number of currently used stages */
355 int nr_stages;
356 /* Number of pending stages */
357 int nr_pending_stages;
358 /* We will not allocate more than this number of stages */
359 int max_stages, min_pipeline, max_pipeline;
360 /* The first stage which will be removed from the pipeline */
361 idetape_stage_t *first_stage;
362 /* The currently active stage */
363 idetape_stage_t *active_stage;
364 /* Will be serviced after the currently active request */
365 idetape_stage_t *next_stage;
366 /* New requests will be added to the pipeline here */
367 idetape_stage_t *last_stage;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 int pages_per_stage;
369 /* Wasted space in each stage */
370 int excess_bh_size;
371
372 /* Status/Action flags: long for set_bit */
373 unsigned long flags;
374 /* protects the ide-tape queue */
Borislav Petkov54bb2072008-02-06 02:57:52 +0100375 spinlock_t lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100377 /* Measures average tape speed */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 unsigned long avg_time;
379 int avg_size;
380 int avg_speed;
381
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 /* the door is currently locked */
383 int door_locked;
384 /* the tape hardware is write protected */
385 char drv_write_prot;
386 /* the tape is write protected (hardware or opened as read-only) */
387 char write_prot;
388
389 /*
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100390 * Limit the number of times a request can be postponed, to avoid an
391 * infinite postpone deadlock.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 int postpone_cnt;
394
395 /*
396 * Measures number of frames:
397 *
398 * 1. written/read to/from the driver pipeline (pipeline_head).
399 * 2. written/read to/from the tape buffers (idetape_bh).
400 * 3. written/read by the tape to/from the media (tape_head).
401 */
402 int pipeline_head;
403 int buffer_head;
404 int tape_head;
405 int last_tape_head;
406
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100407 /* Speed control at the tape buffers input/output */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 unsigned long insert_time;
409 int insert_size;
410 int insert_speed;
411 int max_insert_speed;
412 int measure_insert_time;
413
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100414 /* Speed regulation negative feedback loop */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 int speed_control;
416 int pipeline_head_speed;
417 int controlled_pipeline_head_speed;
418 int uncontrolled_pipeline_head_speed;
419 int controlled_last_pipeline_head;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 unsigned long uncontrolled_pipeline_head_time;
421 unsigned long controlled_pipeline_head_time;
422 int controlled_previous_pipeline_head;
423 int uncontrolled_previous_pipeline_head;
424 unsigned long controlled_previous_head_time;
425 unsigned long uncontrolled_previous_head_time;
426 int restart_speed_control_req;
427
Borislav Petkov8004a8c2008-02-06 02:57:51 +0100428 u32 debug_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429} idetape_tape_t;
430
Arjan van de Vencf8b8972006-03-23 03:00:45 -0800431static DEFINE_MUTEX(idetape_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432
Will Dysond5dee802005-09-16 02:55:07 -0700433static struct class *idetape_sysfs_class;
434
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435#define to_ide_tape(obj) container_of(obj, struct ide_tape_obj, kref)
436
437#define ide_tape_g(disk) \
438 container_of((disk)->private_data, struct ide_tape_obj, driver)
439
440static struct ide_tape_obj *ide_tape_get(struct gendisk *disk)
441{
442 struct ide_tape_obj *tape = NULL;
443
Arjan van de Vencf8b8972006-03-23 03:00:45 -0800444 mutex_lock(&idetape_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 tape = ide_tape_g(disk);
446 if (tape)
447 kref_get(&tape->kref);
Arjan van de Vencf8b8972006-03-23 03:00:45 -0800448 mutex_unlock(&idetape_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 return tape;
450}
451
452static void ide_tape_release(struct kref *);
453
454static void ide_tape_put(struct ide_tape_obj *tape)
455{
Arjan van de Vencf8b8972006-03-23 03:00:45 -0800456 mutex_lock(&idetape_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 kref_put(&tape->kref, ide_tape_release);
Arjan van de Vencf8b8972006-03-23 03:00:45 -0800458 mutex_unlock(&idetape_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459}
460
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100462 * The variables below are used for the character device interface. Additional
463 * state variables are defined in our ide_drive_t structure.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100465static struct ide_tape_obj *idetape_devs[MAX_HWIFS * MAX_DRIVES];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466
467#define ide_tape_f(file) ((file)->private_data)
468
469static struct ide_tape_obj *ide_tape_chrdev_get(unsigned int i)
470{
471 struct ide_tape_obj *tape = NULL;
472
Arjan van de Vencf8b8972006-03-23 03:00:45 -0800473 mutex_lock(&idetape_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 tape = idetape_devs[i];
475 if (tape)
476 kref_get(&tape->kref);
Arjan van de Vencf8b8972006-03-23 03:00:45 -0800477 mutex_unlock(&idetape_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 return tape;
479}
480
Borislav Petkovd236d742008-04-18 00:46:27 +0200481static void idetape_input_buffers(ide_drive_t *drive, struct ide_atapi_pc *pc,
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100482 unsigned int bcount)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483{
484 struct idetape_bh *bh = pc->bh;
485 int count;
486
487 while (bcount) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 if (bh == NULL) {
489 printk(KERN_ERR "ide-tape: bh == NULL in "
490 "idetape_input_buffers\n");
Bartlomiej Zolnierkiewicz7616c0a2008-04-18 00:46:26 +0200491 ide_atapi_discard_data(drive, bcount);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 return;
493 }
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100494 count = min(
495 (unsigned int)(bh->b_size - atomic_read(&bh->b_count)),
496 bcount);
497 HWIF(drive)->atapi_input_bytes(drive, bh->b_data +
498 atomic_read(&bh->b_count), count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 bcount -= count;
500 atomic_add(count, &bh->b_count);
501 if (atomic_read(&bh->b_count) == bh->b_size) {
502 bh = bh->b_reqnext;
503 if (bh)
504 atomic_set(&bh->b_count, 0);
505 }
506 }
507 pc->bh = bh;
508}
509
Borislav Petkovd236d742008-04-18 00:46:27 +0200510static void idetape_output_buffers(ide_drive_t *drive, struct ide_atapi_pc *pc,
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100511 unsigned int bcount)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512{
513 struct idetape_bh *bh = pc->bh;
514 int count;
515
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)pc->b_count, (unsigned int)bcount);
523 HWIF(drive)->atapi_output_bytes(drive, pc->b_data, count);
524 bcount -= count;
525 pc->b_data += count;
526 pc->b_count -= count;
527 if (!pc->b_count) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100528 bh = bh->b_reqnext;
529 pc->bh = bh;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 if (bh) {
531 pc->b_data = bh->b_data;
532 pc->b_count = atomic_read(&bh->b_count);
533 }
534 }
535 }
536}
537
Borislav Petkovd236d742008-04-18 00:46:27 +0200538static void idetape_update_buffers(struct ide_atapi_pc *pc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539{
540 struct idetape_bh *bh = pc->bh;
541 int count;
Borislav Petkovd236d742008-04-18 00:46:27 +0200542 unsigned int bcount = pc->xferred;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543
Borislav Petkov346331f2008-04-18 00:46:26 +0200544 if (pc->flags & PC_FLAG_WRITING)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545 return;
546 while (bcount) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 if (bh == NULL) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100548 printk(KERN_ERR "ide-tape: bh == NULL in %s\n",
549 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 return;
551 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 count = min((unsigned int)bh->b_size, (unsigned int)bcount);
553 atomic_set(&bh->b_count, count);
554 if (atomic_read(&bh->b_count) == bh->b_size)
555 bh = bh->b_reqnext;
556 bcount -= count;
557 }
558 pc->bh = bh;
559}
560
561/*
562 * idetape_next_pc_storage returns a pointer to a place in which we can
563 * safely store a packet command, even though we intend to leave the
564 * driver. A storage space for a maximum of IDETAPE_PC_STACK packet
565 * commands is allocated at initialization time.
566 */
Borislav Petkovd236d742008-04-18 00:46:27 +0200567static struct ide_atapi_pc *idetape_next_pc_storage(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568{
569 idetape_tape_t *tape = drive->driver_data;
570
Borislav Petkov8004a8c2008-02-06 02:57:51 +0100571 debug_log(DBG_PCRQ_STACK, "pc_stack_index=%d\n", tape->pc_stack_index);
572
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 if (tape->pc_stack_index == IDETAPE_PC_STACK)
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100574 tape->pc_stack_index = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 return (&tape->pc_stack[tape->pc_stack_index++]);
576}
577
578/*
579 * idetape_next_rq_storage is used along with idetape_next_pc_storage.
580 * Since we queue packet commands in the request queue, we need to
581 * allocate a request, along with the allocation of a packet command.
582 */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100583
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584/**************************************************************
585 * *
586 * This should get fixed to use kmalloc(.., GFP_ATOMIC) *
587 * followed later on by kfree(). -ml *
588 * *
589 **************************************************************/
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100590
591static struct request *idetape_next_rq_storage(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592{
593 idetape_tape_t *tape = drive->driver_data;
594
Borislav Petkov8004a8c2008-02-06 02:57:51 +0100595 debug_log(DBG_PCRQ_STACK, "rq_stack_index=%d\n", tape->rq_stack_index);
596
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 if (tape->rq_stack_index == IDETAPE_PC_STACK)
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100598 tape->rq_stack_index = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 return (&tape->rq_stack[tape->rq_stack_index++]);
600}
601
Borislav Petkovd236d742008-04-18 00:46:27 +0200602static void idetape_init_pc(struct ide_atapi_pc *pc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603{
604 memset(pc->c, 0, 12);
605 pc->retries = 0;
606 pc->flags = 0;
Borislav Petkovd236d742008-04-18 00:46:27 +0200607 pc->req_xfer = 0;
608 pc->buf = pc->pc_buf;
609 pc->buf_size = IDETAPE_PC_BUFFER_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 pc->bh = NULL;
611 pc->b_data = NULL;
612}
613
614/*
Borislav Petkov1b5db432008-02-02 19:56:48 +0100615 * called on each failed packet command retry to analyze the request sense. We
616 * currently do not utilize this information.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 */
Borislav Petkov1b5db432008-02-02 19:56:48 +0100618static void idetape_analyze_error(ide_drive_t *drive, u8 *sense)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619{
620 idetape_tape_t *tape = drive->driver_data;
Borislav Petkovd236d742008-04-18 00:46:27 +0200621 struct ide_atapi_pc *pc = tape->failed_pc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622
Borislav Petkov1b5db432008-02-02 19:56:48 +0100623 tape->sense_key = sense[2] & 0xF;
624 tape->asc = sense[12];
625 tape->ascq = sense[13];
Borislav Petkov8004a8c2008-02-06 02:57:51 +0100626
627 debug_log(DBG_ERR, "pc = %x, sense key = %x, asc = %x, ascq = %x\n",
628 pc->c[0], tape->sense_key, tape->asc, tape->ascq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629
Borislav Petkovd236d742008-04-18 00:46:27 +0200630 /* Correct pc->xferred by asking the tape. */
Borislav Petkov346331f2008-04-18 00:46:26 +0200631 if (pc->flags & PC_FLAG_DMA_ERROR) {
Borislav Petkovd236d742008-04-18 00:46:27 +0200632 pc->xferred = pc->req_xfer -
Borislav Petkov54bb2072008-02-06 02:57:52 +0100633 tape->blk_size *
Borislav Petkov860ff5e2008-02-02 19:56:50 +0100634 be32_to_cpu(get_unaligned((u32 *)&sense[3]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 idetape_update_buffers(pc);
636 }
637
638 /*
639 * If error was the result of a zero-length read or write command,
640 * with sense key=5, asc=0x22, ascq=0, let it slide. Some drives
641 * (i.e. Seagate STT3401A Travan) don't support 0-length read/writes.
642 */
Borislav Petkov90699ce2008-02-02 19:56:50 +0100643 if ((pc->c[0] == READ_6 || pc->c[0] == WRITE_6)
Borislav Petkov1b5db432008-02-02 19:56:48 +0100644 /* length == 0 */
645 && pc->c[4] == 0 && pc->c[3] == 0 && pc->c[2] == 0) {
646 if (tape->sense_key == 5) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647 /* don't report an error, everything's ok */
648 pc->error = 0;
649 /* don't retry read/write */
Borislav Petkov346331f2008-04-18 00:46:26 +0200650 pc->flags |= PC_FLAG_ABORT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 }
652 }
Borislav Petkov90699ce2008-02-02 19:56:50 +0100653 if (pc->c[0] == READ_6 && (sense[2] & 0x80)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 pc->error = IDETAPE_ERROR_FILEMARK;
Borislav Petkov346331f2008-04-18 00:46:26 +0200655 pc->flags |= PC_FLAG_ABORT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 }
Borislav Petkov90699ce2008-02-02 19:56:50 +0100657 if (pc->c[0] == WRITE_6) {
Borislav Petkov1b5db432008-02-02 19:56:48 +0100658 if ((sense[2] & 0x40) || (tape->sense_key == 0xd
659 && tape->asc == 0x0 && tape->ascq == 0x2)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 pc->error = IDETAPE_ERROR_EOD;
Borislav Petkov346331f2008-04-18 00:46:26 +0200661 pc->flags |= PC_FLAG_ABORT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662 }
663 }
Borislav Petkov90699ce2008-02-02 19:56:50 +0100664 if (pc->c[0] == READ_6 || pc->c[0] == WRITE_6) {
Borislav Petkov1b5db432008-02-02 19:56:48 +0100665 if (tape->sense_key == 8) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666 pc->error = IDETAPE_ERROR_EOD;
Borislav Petkov346331f2008-04-18 00:46:26 +0200667 pc->flags |= PC_FLAG_ABORT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668 }
Borislav Petkov346331f2008-04-18 00:46:26 +0200669 if (!(pc->flags & PC_FLAG_ABORT) &&
Borislav Petkovd236d742008-04-18 00:46:27 +0200670 pc->xferred)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671 pc->retries = IDETAPE_MAX_PC_RETRIES + 1;
672 }
673}
674
Borislav Petkov419d4742008-02-02 19:56:51 +0100675static void idetape_activate_next_stage(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676{
677 idetape_tape_t *tape = drive->driver_data;
678 idetape_stage_t *stage = tape->next_stage;
679 struct request *rq = &stage->rq;
680
Borislav Petkov8004a8c2008-02-06 02:57:51 +0100681 debug_log(DBG_PROCS, "Enter %s\n", __func__);
682
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 if (stage == NULL) {
Borislav Petkov8004a8c2008-02-06 02:57:51 +0100684 printk(KERN_ERR "ide-tape: bug: Trying to activate a non"
685 " existing stage\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686 return;
687 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688
689 rq->rq_disk = tape->disk;
690 rq->buffer = NULL;
691 rq->special = (void *)stage->bh;
Borislav Petkov54bb2072008-02-06 02:57:52 +0100692 tape->active_data_rq = rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 tape->active_stage = stage;
694 tape->next_stage = stage->next;
695}
696
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100697/* Free a stage along with its related buffers completely. */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100698static void __idetape_kfree_stage(idetape_stage_t *stage)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699{
700 struct idetape_bh *prev_bh, *bh = stage->bh;
701 int size;
702
703 while (bh != NULL) {
704 if (bh->b_data != NULL) {
705 size = (int) bh->b_size;
706 while (size > 0) {
707 free_page((unsigned long) bh->b_data);
708 size -= PAGE_SIZE;
709 bh->b_data += PAGE_SIZE;
710 }
711 }
712 prev_bh = bh;
713 bh = bh->b_reqnext;
714 kfree(prev_bh);
715 }
716 kfree(stage);
717}
718
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100719static void idetape_kfree_stage(idetape_tape_t *tape, idetape_stage_t *stage)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720{
721 __idetape_kfree_stage(stage);
722}
723
724/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100725 * Remove tape->first_stage from the pipeline. The caller should avoid race
726 * conditions.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727 */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100728static void idetape_remove_stage_head(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729{
730 idetape_tape_t *tape = drive->driver_data;
731 idetape_stage_t *stage;
Borislav Petkov8004a8c2008-02-06 02:57:51 +0100732
733 debug_log(DBG_PROCS, "Enter %s\n", __func__);
734
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735 if (tape->first_stage == NULL) {
736 printk(KERN_ERR "ide-tape: bug: tape->first_stage is NULL\n");
Borislav Petkov55a5d292008-02-02 19:56:49 +0100737 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738 }
739 if (tape->active_stage == tape->first_stage) {
Borislav Petkov8004a8c2008-02-06 02:57:51 +0100740 printk(KERN_ERR "ide-tape: bug: Trying to free our active "
741 "pipeline stage\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742 return;
743 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744 stage = tape->first_stage;
745 tape->first_stage = stage->next;
746 idetape_kfree_stage(tape, stage);
747 tape->nr_stages--;
748 if (tape->first_stage == NULL) {
749 tape->last_stage = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750 if (tape->next_stage != NULL)
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100751 printk(KERN_ERR "ide-tape: bug: tape->next_stage !="
752 " NULL\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753 if (tape->nr_stages)
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100754 printk(KERN_ERR "ide-tape: bug: nr_stages should be 0 "
755 "now\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756 }
757}
758
759/*
760 * This will free all the pipeline stages starting from new_last_stage->next
761 * to the end of the list, and point tape->last_stage to new_last_stage.
762 */
763static void idetape_abort_pipeline(ide_drive_t *drive,
764 idetape_stage_t *new_last_stage)
765{
766 idetape_tape_t *tape = drive->driver_data;
767 idetape_stage_t *stage = new_last_stage->next;
768 idetape_stage_t *nstage;
769
Borislav Petkov8004a8c2008-02-06 02:57:51 +0100770 debug_log(DBG_PROCS, "%s: Enter %s\n", tape->name, __func__);
771
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772 while (stage) {
773 nstage = stage->next;
774 idetape_kfree_stage(tape, stage);
775 --tape->nr_stages;
776 --tape->nr_pending_stages;
777 stage = nstage;
778 }
779 if (new_last_stage)
780 new_last_stage->next = NULL;
781 tape->last_stage = new_last_stage;
782 tape->next_stage = NULL;
783}
784
785/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100786 * Finish servicing a request and insert a pending pipeline request into the
787 * main device queue.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788 */
789static int idetape_end_request(ide_drive_t *drive, int uptodate, int nr_sects)
790{
791 struct request *rq = HWGROUP(drive)->rq;
792 idetape_tape_t *tape = drive->driver_data;
793 unsigned long flags;
794 int error;
795 int remove_stage = 0;
796 idetape_stage_t *active_stage;
797
Borislav Petkov8004a8c2008-02-06 02:57:51 +0100798 debug_log(DBG_PROCS, "Enter %s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799
800 switch (uptodate) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100801 case 0: error = IDETAPE_ERROR_GENERAL; break;
802 case 1: error = 0; break;
803 default: error = uptodate;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 }
805 rq->errors = error;
806 if (error)
807 tape->failed_pc = NULL;
808
Bartlomiej Zolnierkiewicz36872212008-01-26 20:13:10 +0100809 if (!blk_special_request(rq)) {
810 ide_end_request(drive, uptodate, nr_sects);
811 return 0;
812 }
813
Borislav Petkov54bb2072008-02-06 02:57:52 +0100814 spin_lock_irqsave(&tape->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815
816 /* The request was a pipelined data transfer request */
Borislav Petkov54bb2072008-02-06 02:57:52 +0100817 if (tape->active_data_rq == rq) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818 active_stage = tape->active_stage;
819 tape->active_stage = NULL;
Borislav Petkov54bb2072008-02-06 02:57:52 +0100820 tape->active_data_rq = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821 tape->nr_pending_stages--;
822 if (rq->cmd[0] & REQ_IDETAPE_WRITE) {
823 remove_stage = 1;
824 if (error) {
Borislav Petkov346331f2008-04-18 00:46:26 +0200825 set_bit(IDETAPE_FLAG_PIPELINE_ERR,
826 &tape->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827 if (error == IDETAPE_ERROR_EOD)
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100828 idetape_abort_pipeline(drive,
829 active_stage);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830 }
831 } else if (rq->cmd[0] & REQ_IDETAPE_READ) {
832 if (error == IDETAPE_ERROR_EOD) {
Borislav Petkov346331f2008-04-18 00:46:26 +0200833 set_bit(IDETAPE_FLAG_PIPELINE_ERR,
834 &tape->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835 idetape_abort_pipeline(drive, active_stage);
836 }
837 }
838 if (tape->next_stage != NULL) {
Borislav Petkov419d4742008-02-02 19:56:51 +0100839 idetape_activate_next_stage(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100841 /* Insert the next request into the request queue. */
Borislav Petkov54bb2072008-02-06 02:57:52 +0100842 (void)ide_do_drive_cmd(drive, tape->active_data_rq,
843 ide_end);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844 } else if (!error) {
Borislav Petkov97219852008-02-06 02:57:52 +0100845 /*
846 * This is a part of the feedback loop which tries to
847 * find the optimum number of stages. We are starting
848 * from a minimum maximum number of stages, and if we
849 * sense that the pipeline is empty, we try to increase
850 * it, until we reach the user compile time memory
851 * limit.
852 */
853 int i = (tape->max_pipeline - tape->min_pipeline) / 10;
854
855 tape->max_stages += max(i, 1);
856 tape->max_stages = max(tape->max_stages,
857 tape->min_pipeline);
858 tape->max_stages = min(tape->max_stages,
859 tape->max_pipeline);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860 }
861 }
862 ide_end_drive_cmd(drive, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863
864 if (remove_stage)
865 idetape_remove_stage_head(drive);
Borislav Petkov54bb2072008-02-06 02:57:52 +0100866 if (tape->active_data_rq == NULL)
Borislav Petkov346331f2008-04-18 00:46:26 +0200867 clear_bit(IDETAPE_FLAG_PIPELINE_ACTIVE, &tape->flags);
Borislav Petkov54bb2072008-02-06 02:57:52 +0100868 spin_unlock_irqrestore(&tape->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869 return 0;
870}
871
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100872static ide_startstop_t idetape_request_sense_callback(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873{
874 idetape_tape_t *tape = drive->driver_data;
875
Borislav Petkov8004a8c2008-02-06 02:57:51 +0100876 debug_log(DBG_PROCS, "Enter %s\n", __func__);
877
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878 if (!tape->pc->error) {
Borislav Petkovd236d742008-04-18 00:46:27 +0200879 idetape_analyze_error(drive, tape->pc->buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880 idetape_end_request(drive, 1, 0);
881 } else {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100882 printk(KERN_ERR "ide-tape: Error in REQUEST SENSE itself - "
883 "Aborting request!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884 idetape_end_request(drive, 0, 0);
885 }
886 return ide_stopped;
887}
888
Borislav Petkovd236d742008-04-18 00:46:27 +0200889static void idetape_create_request_sense_cmd(struct ide_atapi_pc *pc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890{
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100891 idetape_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +0100892 pc->c[0] = REQUEST_SENSE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893 pc->c[4] = 20;
Borislav Petkovd236d742008-04-18 00:46:27 +0200894 pc->req_xfer = 20;
895 pc->idetape_callback = &idetape_request_sense_callback;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896}
897
898static void idetape_init_rq(struct request *rq, u8 cmd)
899{
900 memset(rq, 0, sizeof(*rq));
Jens Axboe4aff5e22006-08-10 08:44:47 +0200901 rq->cmd_type = REQ_TYPE_SPECIAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902 rq->cmd[0] = cmd;
903}
904
905/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100906 * Generate a new packet command request in front of the request queue, before
907 * the current request, so that it will be processed immediately, on the next
908 * pass through the driver. The function below is called from the request
909 * handling part of the driver (the "bottom" part). Safe storage for the request
910 * should be allocated with ide_tape_next_{pc,rq}_storage() prior to that.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100912 * Memory for those requests is pre-allocated at initialization time, and is
913 * limited to IDETAPE_PC_STACK requests. We assume that we have enough space for
914 * the maximum possible number of inter-dependent packet commands.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100916 * The higher level of the driver - The ioctl handler and the character device
917 * handling functions should queue request to the lower level part and wait for
918 * their completion using idetape_queue_pc_tail or idetape_queue_rw_tail.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919 */
Borislav Petkovd236d742008-04-18 00:46:27 +0200920static void idetape_queue_pc_head(ide_drive_t *drive, struct ide_atapi_pc *pc,
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100921 struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922{
923 struct ide_tape_obj *tape = drive->driver_data;
924
925 idetape_init_rq(rq, REQ_IDETAPE_PC1);
926 rq->buffer = (char *) pc;
927 rq->rq_disk = tape->disk;
928 (void) ide_do_drive_cmd(drive, rq, ide_preempt);
929}
930
931/*
932 * idetape_retry_pc is called when an error was detected during the
933 * last packet command. We queue a request sense packet command in
934 * the head of the request list.
935 */
936static ide_startstop_t idetape_retry_pc (ide_drive_t *drive)
937{
938 idetape_tape_t *tape = drive->driver_data;
Borislav Petkovd236d742008-04-18 00:46:27 +0200939 struct ide_atapi_pc *pc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940 struct request *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941
Bartlomiej Zolnierkiewicz64a57fe2008-02-06 02:57:51 +0100942 (void)ide_read_error(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943 pc = idetape_next_pc_storage(drive);
944 rq = idetape_next_rq_storage(drive);
945 idetape_create_request_sense_cmd(pc);
Borislav Petkov346331f2008-04-18 00:46:26 +0200946 set_bit(IDETAPE_FLAG_IGNORE_DSC, &tape->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947 idetape_queue_pc_head(drive, pc, rq);
948 return ide_stopped;
949}
950
951/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100952 * Postpone the current request so that ide.c will be able to service requests
953 * from another device on the same hwgroup while we are polling for DSC.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954 */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100955static void idetape_postpone_request(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956{
957 idetape_tape_t *tape = drive->driver_data;
958
Borislav Petkov8004a8c2008-02-06 02:57:51 +0100959 debug_log(DBG_PROCS, "Enter %s\n", __func__);
960
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961 tape->postponed_rq = HWGROUP(drive)->rq;
Borislav Petkov54bb2072008-02-06 02:57:52 +0100962 ide_stall_queue(drive, tape->dsc_poll_freq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963}
964
Borislav Petkovd236d742008-04-18 00:46:27 +0200965typedef void idetape_io_buf(ide_drive_t *, struct ide_atapi_pc *, unsigned int);
Borislav Petkova1efc852008-02-06 02:57:52 +0100966
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967/*
Borislav Petkova1efc852008-02-06 02:57:52 +0100968 * This is the usual interrupt handler which will be called during a packet
969 * command. We will transfer some of the data (as requested by the drive) and
970 * will re-point interrupt handler to us. When data transfer is finished, we
971 * will act according to the algorithm described before
Borislav Petkov8d06bfa2008-02-06 02:57:53 +0100972 * idetape_issue_pc.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973 */
Borislav Petkova1efc852008-02-06 02:57:52 +0100974static ide_startstop_t idetape_pc_intr(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975{
976 ide_hwif_t *hwif = drive->hwif;
977 idetape_tape_t *tape = drive->driver_data;
Borislav Petkovd236d742008-04-18 00:46:27 +0200978 struct ide_atapi_pc *pc = tape->pc;
Borislav Petkova1efc852008-02-06 02:57:52 +0100979 xfer_func_t *xferfunc;
980 idetape_io_buf *iobuf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981 unsigned int temp;
982#if SIMULATE_ERRORS
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100983 static int error_sim_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984#endif
Bartlomiej Zolnierkiewicz790d1232008-01-25 22:17:12 +0100985 u16 bcount;
Bartlomiej Zolnierkiewicz8e7657a2008-01-25 22:17:12 +0100986 u8 stat, ireason;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987
Borislav Petkov8004a8c2008-02-06 02:57:51 +0100988 debug_log(DBG_PROCS, "Enter %s - interrupt handler\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989
990 /* Clear the interrupt */
Bartlomiej Zolnierkiewiczc47137a2008-02-06 02:57:51 +0100991 stat = ide_read_status(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992
Borislav Petkov346331f2008-04-18 00:46:26 +0200993 if (pc->flags & PC_FLAG_DMA_IN_PROGRESS) {
Bartlomiej Zolnierkiewicz5e37bdc2008-04-26 22:25:24 +0200994 if (hwif->dma_ops->dma_end(drive) || (stat & ERR_STAT)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995 /*
996 * A DMA error is sometimes expected. For example,
997 * if the tape is crossing a filemark during a
998 * READ command, it will issue an irq and position
999 * itself before the filemark, so that only a partial
1000 * data transfer will occur (which causes the DMA
1001 * error). In that case, we will later ask the tape
1002 * how much bytes of the original request were
1003 * actually transferred (we can't receive that
1004 * information from the DMA engine on most chipsets).
1005 */
1006
1007 /*
1008 * On the contrary, a DMA error is never expected;
1009 * it usually indicates a hardware error or abort.
1010 * If the tape crosses a filemark during a READ
1011 * command, it will issue an irq and position itself
1012 * after the filemark (not before). Only a partial
1013 * data transfer will occur, but no DMA error.
1014 * (AS, 19 Apr 2001)
1015 */
Borislav Petkov346331f2008-04-18 00:46:26 +02001016 pc->flags |= PC_FLAG_DMA_ERROR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017 } else {
Borislav Petkovd236d742008-04-18 00:46:27 +02001018 pc->xferred = pc->req_xfer;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019 idetape_update_buffers(pc);
1020 }
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001021 debug_log(DBG_PROCS, "DMA finished\n");
1022
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023 }
1024
1025 /* No more interrupts */
Bartlomiej Zolnierkiewicz22c525b2008-01-25 22:17:11 +01001026 if ((stat & DRQ_STAT) == 0) {
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001027 debug_log(DBG_SENSE, "Packet command completed, %d bytes"
Borislav Petkovd236d742008-04-18 00:46:27 +02001028 " transferred\n", pc->xferred);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029
Borislav Petkov346331f2008-04-18 00:46:26 +02001030 pc->flags &= ~PC_FLAG_DMA_IN_PROGRESS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031 local_irq_enable();
1032
1033#if SIMULATE_ERRORS
Borislav Petkov90699ce2008-02-02 19:56:50 +01001034 if ((pc->c[0] == WRITE_6 || pc->c[0] == READ_6) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035 (++error_sim_count % 100) == 0) {
1036 printk(KERN_INFO "ide-tape: %s: simulating error\n",
1037 tape->name);
Bartlomiej Zolnierkiewicz22c525b2008-01-25 22:17:11 +01001038 stat |= ERR_STAT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039 }
1040#endif
Borislav Petkov90699ce2008-02-02 19:56:50 +01001041 if ((stat & ERR_STAT) && pc->c[0] == REQUEST_SENSE)
Bartlomiej Zolnierkiewicz22c525b2008-01-25 22:17:11 +01001042 stat &= ~ERR_STAT;
Borislav Petkov346331f2008-04-18 00:46:26 +02001043 if ((stat & ERR_STAT) || (pc->flags & PC_FLAG_DMA_ERROR)) {
Bartlomiej Zolnierkiewicz22c525b2008-01-25 22:17:11 +01001044 /* Error detected */
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001045 debug_log(DBG_ERR, "%s: I/O error\n", tape->name);
1046
Borislav Petkov90699ce2008-02-02 19:56:50 +01001047 if (pc->c[0] == REQUEST_SENSE) {
Borislav Petkova1efc852008-02-06 02:57:52 +01001048 printk(KERN_ERR "ide-tape: I/O error in request"
1049 " sense command\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050 return ide_do_reset(drive);
1051 }
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001052 debug_log(DBG_ERR, "[cmd %x]: check condition\n",
1053 pc->c[0]);
1054
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055 /* Retry operation */
1056 return idetape_retry_pc(drive);
1057 }
1058 pc->error = 0;
Borislav Petkov346331f2008-04-18 00:46:26 +02001059 if ((pc->flags & PC_FLAG_WAIT_FOR_DSC) &&
Bartlomiej Zolnierkiewicz22c525b2008-01-25 22:17:11 +01001060 (stat & SEEK_STAT) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061 /* Media access command */
1062 tape->dsc_polling_start = jiffies;
Borislav Petkov54bb2072008-02-06 02:57:52 +01001063 tape->dsc_poll_freq = IDETAPE_DSC_MA_FAST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064 tape->dsc_timeout = jiffies + IDETAPE_DSC_MA_TIMEOUT;
1065 /* Allow ide.c to handle other requests */
1066 idetape_postpone_request(drive);
1067 return ide_stopped;
1068 }
1069 if (tape->failed_pc == pc)
1070 tape->failed_pc = NULL;
1071 /* Command finished - Call the callback function */
Borislav Petkovd236d742008-04-18 00:46:27 +02001072 return pc->idetape_callback(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073 }
Borislav Petkov346331f2008-04-18 00:46:26 +02001074
1075 if (pc->flags & PC_FLAG_DMA_IN_PROGRESS) {
1076 pc->flags &= ~PC_FLAG_DMA_IN_PROGRESS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077 printk(KERN_ERR "ide-tape: The tape wants to issue more "
1078 "interrupts in DMA mode\n");
1079 printk(KERN_ERR "ide-tape: DMA disabled, reverting to PIO\n");
Bartlomiej Zolnierkiewicz7469aaf2007-02-17 02:40:26 +01001080 ide_dma_off(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081 return ide_do_reset(drive);
1082 }
1083 /* Get the number of bytes to transfer on this interrupt. */
Bartlomiej Zolnierkiewicz23579a22008-04-18 00:46:26 +02001084 bcount = (hwif->INB(hwif->io_ports[IDE_BCOUNTH_OFFSET]) << 8) |
1085 hwif->INB(hwif->io_ports[IDE_BCOUNTL_OFFSET]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086
Bartlomiej Zolnierkiewicz23579a22008-04-18 00:46:26 +02001087 ireason = hwif->INB(hwif->io_ports[IDE_IREASON_OFFSET]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088
Bartlomiej Zolnierkiewicz8e7657a2008-01-25 22:17:12 +01001089 if (ireason & CD) {
Borislav Petkova1efc852008-02-06 02:57:52 +01001090 printk(KERN_ERR "ide-tape: CoD != 0 in %s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091 return ide_do_reset(drive);
1092 }
Borislav Petkov346331f2008-04-18 00:46:26 +02001093 if (((ireason & IO) == IO) == !!(pc->flags & PC_FLAG_WRITING)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094 /* Hopefully, we will never get here */
1095 printk(KERN_ERR "ide-tape: We wanted to %s, ",
Bartlomiej Zolnierkiewicz8e7657a2008-01-25 22:17:12 +01001096 (ireason & IO) ? "Write" : "Read");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097 printk(KERN_ERR "ide-tape: but the tape wants us to %s !\n",
Bartlomiej Zolnierkiewicz8e7657a2008-01-25 22:17:12 +01001098 (ireason & IO) ? "Read" : "Write");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099 return ide_do_reset(drive);
1100 }
Borislav Petkov346331f2008-04-18 00:46:26 +02001101 if (!(pc->flags & PC_FLAG_WRITING)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102 /* Reading - Check that we have enough space */
Borislav Petkovd236d742008-04-18 00:46:27 +02001103 temp = pc->xferred + bcount;
1104 if (temp > pc->req_xfer) {
1105 if (temp > pc->buf_size) {
Borislav Petkova1efc852008-02-06 02:57:52 +01001106 printk(KERN_ERR "ide-tape: The tape wants to "
1107 "send us more data than expected "
1108 "- discarding data\n");
Bartlomiej Zolnierkiewicz7616c0a2008-04-18 00:46:26 +02001109 ide_atapi_discard_data(drive, bcount);
Borislav Petkova1efc852008-02-06 02:57:52 +01001110 ide_set_handler(drive, &idetape_pc_intr,
1111 IDETAPE_WAIT_CMD, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112 return ide_started;
1113 }
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001114 debug_log(DBG_SENSE, "The tape wants to send us more "
1115 "data than expected - allowing transfer\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116 }
Borislav Petkova1efc852008-02-06 02:57:52 +01001117 iobuf = &idetape_input_buffers;
1118 xferfunc = hwif->atapi_input_bytes;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119 } else {
Borislav Petkova1efc852008-02-06 02:57:52 +01001120 iobuf = &idetape_output_buffers;
1121 xferfunc = hwif->atapi_output_bytes;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122 }
Borislav Petkova1efc852008-02-06 02:57:52 +01001123
1124 if (pc->bh)
1125 iobuf(drive, pc, bcount);
1126 else
Borislav Petkovd236d742008-04-18 00:46:27 +02001127 xferfunc(drive, pc->cur_pos, bcount);
Borislav Petkova1efc852008-02-06 02:57:52 +01001128
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129 /* Update the current position */
Borislav Petkovd236d742008-04-18 00:46:27 +02001130 pc->xferred += bcount;
1131 pc->cur_pos += bcount;
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001132
1133 debug_log(DBG_SENSE, "[cmd %x] transferred %d bytes on that intr.\n",
1134 pc->c[0], bcount);
1135
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136 /* And set the interrupt handler again */
1137 ide_set_handler(drive, &idetape_pc_intr, IDETAPE_WAIT_CMD, NULL);
1138 return ide_started;
1139}
1140
1141/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001142 * Packet Command Interface
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001144 * The current Packet Command is available in tape->pc, and will not change
1145 * until we finish handling it. Each packet command is associated with a
1146 * callback function that will be called when the command is finished.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001148 * The handling will be done in three stages:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001150 * 1. idetape_issue_pc will send the packet command to the drive, and will set
1151 * the interrupt handler to idetape_pc_intr.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001153 * 2. On each interrupt, idetape_pc_intr will be called. This step will be
1154 * repeated until the device signals us that no more interrupts will be issued.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001156 * 3. ATAPI Tape media access commands have immediate status with a delayed
1157 * process. In case of a successful initiation of a media access packet command,
1158 * the DSC bit will be set when the actual execution of the command is finished.
1159 * Since the tape drive will not issue an interrupt, we have to poll for this
1160 * event. In this case, we define the request as "low priority request" by
1161 * setting rq_status to IDETAPE_RQ_POSTPONED, set a timer to poll for DSC and
1162 * exit the driver.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001164 * ide.c will then give higher priority to requests which originate from the
1165 * other device, until will change rq_status to RQ_ACTIVE.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001167 * 4. When the packet command is finished, it will be checked for errors.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001169 * 5. In case an error was found, we queue a request sense packet command in
1170 * front of the request queue and retry the operation up to
1171 * IDETAPE_MAX_PC_RETRIES times.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001173 * 6. In case no error was found, or we decided to give up and not to retry
1174 * again, the callback function will be called and then we will handle the next
1175 * request.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176 */
1177static ide_startstop_t idetape_transfer_pc(ide_drive_t *drive)
1178{
1179 ide_hwif_t *hwif = drive->hwif;
1180 idetape_tape_t *tape = drive->driver_data;
Borislav Petkovd236d742008-04-18 00:46:27 +02001181 struct ide_atapi_pc *pc = tape->pc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182 int retries = 100;
1183 ide_startstop_t startstop;
Bartlomiej Zolnierkiewicz8e7657a2008-01-25 22:17:12 +01001184 u8 ireason;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001186 if (ide_wait_stat(&startstop, drive, DRQ_STAT, BUSY_STAT, WAIT_READY)) {
1187 printk(KERN_ERR "ide-tape: Strange, packet command initiated "
1188 "yet DRQ isn't asserted\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189 return startstop;
1190 }
Bartlomiej Zolnierkiewicz23579a22008-04-18 00:46:26 +02001191 ireason = hwif->INB(hwif->io_ports[IDE_IREASON_OFFSET]);
Bartlomiej Zolnierkiewicz8e7657a2008-01-25 22:17:12 +01001192 while (retries-- && ((ireason & CD) == 0 || (ireason & IO))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193 printk(KERN_ERR "ide-tape: (IO,CoD != (0,1) while issuing "
1194 "a packet command, retrying\n");
1195 udelay(100);
Bartlomiej Zolnierkiewicz23579a22008-04-18 00:46:26 +02001196 ireason = hwif->INB(hwif->io_ports[IDE_IREASON_OFFSET]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197 if (retries == 0) {
1198 printk(KERN_ERR "ide-tape: (IO,CoD != (0,1) while "
1199 "issuing a packet command, ignoring\n");
Bartlomiej Zolnierkiewicz8e7657a2008-01-25 22:17:12 +01001200 ireason |= CD;
1201 ireason &= ~IO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202 }
1203 }
Bartlomiej Zolnierkiewicz8e7657a2008-01-25 22:17:12 +01001204 if ((ireason & CD) == 0 || (ireason & IO)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205 printk(KERN_ERR "ide-tape: (IO,CoD) != (0,1) while issuing "
1206 "a packet command\n");
1207 return ide_do_reset(drive);
1208 }
1209 /* Set the interrupt routine */
1210 ide_set_handler(drive, &idetape_pc_intr, IDETAPE_WAIT_CMD, NULL);
1211#ifdef CONFIG_BLK_DEV_IDEDMA
1212 /* Begin DMA, if necessary */
Borislav Petkov346331f2008-04-18 00:46:26 +02001213 if (pc->flags & PC_FLAG_DMA_IN_PROGRESS)
Bartlomiej Zolnierkiewicz5e37bdc2008-04-26 22:25:24 +02001214 hwif->dma_ops->dma_start(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215#endif
1216 /* Send the actual packet */
1217 HWIF(drive)->atapi_output_bytes(drive, pc->c, 12);
1218 return ide_started;
1219}
1220
Borislav Petkovd236d742008-04-18 00:46:27 +02001221static ide_startstop_t idetape_issue_pc(ide_drive_t *drive,
1222 struct ide_atapi_pc *pc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223{
1224 ide_hwif_t *hwif = drive->hwif;
1225 idetape_tape_t *tape = drive->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226 int dma_ok = 0;
Bartlomiej Zolnierkiewicz790d1232008-01-25 22:17:12 +01001227 u16 bcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228
Borislav Petkov90699ce2008-02-02 19:56:50 +01001229 if (tape->pc->c[0] == REQUEST_SENSE &&
1230 pc->c[0] == REQUEST_SENSE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231 printk(KERN_ERR "ide-tape: possible ide-tape.c bug - "
1232 "Two request sense in serial were issued\n");
1233 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234
Borislav Petkov90699ce2008-02-02 19:56:50 +01001235 if (tape->failed_pc == NULL && pc->c[0] != REQUEST_SENSE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236 tape->failed_pc = pc;
1237 /* Set the current packet command */
1238 tape->pc = pc;
1239
1240 if (pc->retries > IDETAPE_MAX_PC_RETRIES ||
Borislav Petkov346331f2008-04-18 00:46:26 +02001241 (pc->flags & PC_FLAG_ABORT)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242 /*
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001243 * We will "abort" retrying a packet command in case legitimate
1244 * error code was received (crossing a filemark, or end of the
1245 * media, for example).
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246 */
Borislav Petkov346331f2008-04-18 00:46:26 +02001247 if (!(pc->flags & PC_FLAG_ABORT)) {
Borislav Petkov90699ce2008-02-02 19:56:50 +01001248 if (!(pc->c[0] == TEST_UNIT_READY &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249 tape->sense_key == 2 && tape->asc == 4 &&
1250 (tape->ascq == 1 || tape->ascq == 8))) {
1251 printk(KERN_ERR "ide-tape: %s: I/O error, "
1252 "pc = %2x, key = %2x, "
1253 "asc = %2x, ascq = %2x\n",
1254 tape->name, pc->c[0],
1255 tape->sense_key, tape->asc,
1256 tape->ascq);
1257 }
1258 /* Giving up */
1259 pc->error = IDETAPE_ERROR_GENERAL;
1260 }
1261 tape->failed_pc = NULL;
Borislav Petkovd236d742008-04-18 00:46:27 +02001262 return pc->idetape_callback(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263 }
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001264 debug_log(DBG_SENSE, "Retry #%d, cmd = %02X\n", pc->retries, pc->c[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265
1266 pc->retries++;
1267 /* We haven't transferred any data yet */
Borislav Petkovd236d742008-04-18 00:46:27 +02001268 pc->xferred = 0;
1269 pc->cur_pos = pc->buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270 /* Request to transfer the entire buffer at once */
Borislav Petkovd236d742008-04-18 00:46:27 +02001271 bcount = pc->req_xfer;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272
Borislav Petkov346331f2008-04-18 00:46:26 +02001273 if (pc->flags & PC_FLAG_DMA_ERROR) {
1274 pc->flags &= ~PC_FLAG_DMA_ERROR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275 printk(KERN_WARNING "ide-tape: DMA disabled, "
1276 "reverting to PIO\n");
Bartlomiej Zolnierkiewicz7469aaf2007-02-17 02:40:26 +01001277 ide_dma_off(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278 }
Borislav Petkov346331f2008-04-18 00:46:26 +02001279 if ((pc->flags & PC_FLAG_DMA_RECOMMENDED) && drive->using_dma)
Bartlomiej Zolnierkiewicz5e37bdc2008-04-26 22:25:24 +02001280 dma_ok = !hwif->dma_ops->dma_setup(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281
Bartlomiej Zolnierkiewicz2fc57382008-01-25 22:17:13 +01001282 ide_pktcmd_tf_load(drive, IDE_TFLAG_NO_SELECT_MASK |
1283 IDE_TFLAG_OUT_DEVICE, bcount, dma_ok);
1284
Borislav Petkov346331f2008-04-18 00:46:26 +02001285 if (dma_ok)
1286 /* Will begin DMA later */
1287 pc->flags |= PC_FLAG_DMA_IN_PROGRESS;
1288 if (test_bit(IDETAPE_FLAG_DRQ_INTERRUPT, &tape->flags)) {
Bartlomiej Zolnierkiewiczc1c9dbc2008-02-02 19:56:44 +01001289 ide_execute_command(drive, WIN_PACKETCMD, &idetape_transfer_pc,
1290 IDETAPE_WAIT_CMD, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291 return ide_started;
1292 } else {
Bartlomiej Zolnierkiewicz23579a22008-04-18 00:46:26 +02001293 hwif->OUTB(WIN_PACKETCMD, hwif->io_ports[IDE_COMMAND_OFFSET]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294 return idetape_transfer_pc(drive);
1295 }
1296}
1297
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001298static ide_startstop_t idetape_pc_callback(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299{
1300 idetape_tape_t *tape = drive->driver_data;
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001301
1302 debug_log(DBG_PROCS, "Enter %s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303
1304 idetape_end_request(drive, tape->pc->error ? 0 : 1, 0);
1305 return ide_stopped;
1306}
1307
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001308/* A mode sense command is used to "sense" tape parameters. */
Borislav Petkovd236d742008-04-18 00:46:27 +02001309static void idetape_create_mode_sense_cmd(struct ide_atapi_pc *pc, u8 page_code)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310{
1311 idetape_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +01001312 pc->c[0] = MODE_SENSE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313 if (page_code != IDETAPE_BLOCK_DESCRIPTOR)
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001314 /* DBD = 1 - Don't return block descriptors */
1315 pc->c[1] = 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316 pc->c[2] = page_code;
1317 /*
1318 * Changed pc->c[3] to 0 (255 will at best return unused info).
1319 *
1320 * For SCSI this byte is defined as subpage instead of high byte
1321 * of length and some IDE drives seem to interpret it this way
1322 * and return an error when 255 is used.
1323 */
1324 pc->c[3] = 0;
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001325 /* We will just discard data in that case */
1326 pc->c[4] = 255;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327 if (page_code == IDETAPE_BLOCK_DESCRIPTOR)
Borislav Petkovd236d742008-04-18 00:46:27 +02001328 pc->req_xfer = 12;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329 else if (page_code == IDETAPE_CAPABILITIES_PAGE)
Borislav Petkovd236d742008-04-18 00:46:27 +02001330 pc->req_xfer = 24;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331 else
Borislav Petkovd236d742008-04-18 00:46:27 +02001332 pc->req_xfer = 50;
1333 pc->idetape_callback = &idetape_pc_callback;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334}
1335
Borislav Petkov37016ba2008-02-06 02:57:52 +01001336static void idetape_calculate_speeds(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337{
1338 idetape_tape_t *tape = drive->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001340 if (time_after(jiffies,
1341 tape->controlled_pipeline_head_time + 120 * HZ)) {
1342 tape->controlled_previous_pipeline_head =
1343 tape->controlled_last_pipeline_head;
1344 tape->controlled_previous_head_time =
1345 tape->controlled_pipeline_head_time;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346 tape->controlled_last_pipeline_head = tape->pipeline_head;
1347 tape->controlled_pipeline_head_time = jiffies;
1348 }
1349 if (time_after(jiffies, tape->controlled_pipeline_head_time + 60 * HZ))
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001350 tape->controlled_pipeline_head_speed = (tape->pipeline_head -
1351 tape->controlled_last_pipeline_head) * 32 * HZ /
1352 (jiffies - tape->controlled_pipeline_head_time);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353 else if (time_after(jiffies, tape->controlled_previous_head_time))
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001354 tape->controlled_pipeline_head_speed = (tape->pipeline_head -
1355 tape->controlled_previous_pipeline_head) * 32 *
1356 HZ / (jiffies - tape->controlled_previous_head_time);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001358 if (tape->nr_pending_stages < tape->max_stages/*- 1 */) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359 /* -1 for read mode error recovery */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001360 if (time_after(jiffies, tape->uncontrolled_previous_head_time +
1361 10 * HZ)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362 tape->uncontrolled_pipeline_head_time = jiffies;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001363 tape->uncontrolled_pipeline_head_speed =
1364 (tape->pipeline_head -
1365 tape->uncontrolled_previous_pipeline_head) *
1366 32 * HZ / (jiffies -
1367 tape->uncontrolled_previous_head_time);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368 }
1369 } else {
1370 tape->uncontrolled_previous_head_time = jiffies;
1371 tape->uncontrolled_previous_pipeline_head = tape->pipeline_head;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001372 if (time_after(jiffies, tape->uncontrolled_pipeline_head_time +
1373 30 * HZ))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374 tape->uncontrolled_pipeline_head_time = jiffies;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001375
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376 }
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001377 tape->pipeline_head_speed = max(tape->uncontrolled_pipeline_head_speed,
1378 tape->controlled_pipeline_head_speed);
Borislav Petkov37016ba2008-02-06 02:57:52 +01001379
1380 if (tape->speed_control == 1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381 if (tape->nr_pending_stages >= tape->max_stages / 2)
1382 tape->max_insert_speed = tape->pipeline_head_speed +
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001383 (1100 - tape->pipeline_head_speed) * 2 *
1384 (tape->nr_pending_stages - tape->max_stages / 2)
1385 / tape->max_stages;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386 else
1387 tape->max_insert_speed = 500 +
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001388 (tape->pipeline_head_speed - 500) * 2 *
1389 tape->nr_pending_stages / tape->max_stages;
Borislav Petkov37016ba2008-02-06 02:57:52 +01001390
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391 if (tape->nr_pending_stages >= tape->max_stages * 99 / 100)
1392 tape->max_insert_speed = 5000;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393 } else
1394 tape->max_insert_speed = tape->speed_control;
Borislav Petkov37016ba2008-02-06 02:57:52 +01001395
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396 tape->max_insert_speed = max(tape->max_insert_speed, 500);
1397}
1398
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001399static ide_startstop_t idetape_media_access_finished(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400{
1401 idetape_tape_t *tape = drive->driver_data;
Borislav Petkovd236d742008-04-18 00:46:27 +02001402 struct ide_atapi_pc *pc = tape->pc;
Bartlomiej Zolnierkiewicz22c525b2008-01-25 22:17:11 +01001403 u8 stat;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404
Bartlomiej Zolnierkiewiczc47137a2008-02-06 02:57:51 +01001405 stat = ide_read_status(drive);
1406
Bartlomiej Zolnierkiewicz22c525b2008-01-25 22:17:11 +01001407 if (stat & SEEK_STAT) {
1408 if (stat & ERR_STAT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409 /* Error detected */
Borislav Petkov90699ce2008-02-02 19:56:50 +01001410 if (pc->c[0] != TEST_UNIT_READY)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411 printk(KERN_ERR "ide-tape: %s: I/O error, ",
1412 tape->name);
1413 /* Retry operation */
1414 return idetape_retry_pc(drive);
1415 }
1416 pc->error = 0;
1417 if (tape->failed_pc == pc)
1418 tape->failed_pc = NULL;
1419 } else {
1420 pc->error = IDETAPE_ERROR_GENERAL;
1421 tape->failed_pc = NULL;
1422 }
Borislav Petkovd236d742008-04-18 00:46:27 +02001423 return pc->idetape_callback(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424}
1425
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001426static ide_startstop_t idetape_rw_callback(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427{
1428 idetape_tape_t *tape = drive->driver_data;
1429 struct request *rq = HWGROUP(drive)->rq;
Borislav Petkovd236d742008-04-18 00:46:27 +02001430 int blocks = tape->pc->xferred / tape->blk_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431
Borislav Petkov54bb2072008-02-06 02:57:52 +01001432 tape->avg_size += blocks * tape->blk_size;
1433 tape->insert_size += blocks * tape->blk_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434 if (tape->insert_size > 1024 * 1024)
1435 tape->measure_insert_time = 1;
1436 if (tape->measure_insert_time) {
1437 tape->measure_insert_time = 0;
1438 tape->insert_time = jiffies;
1439 tape->insert_size = 0;
1440 }
1441 if (time_after(jiffies, tape->insert_time))
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001442 tape->insert_speed = tape->insert_size / 1024 * HZ /
1443 (jiffies - tape->insert_time);
Marcelo Feitoza Parisi9bae1ff2006-03-28 01:56:46 -08001444 if (time_after_eq(jiffies, tape->avg_time + HZ)) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001445 tape->avg_speed = tape->avg_size * HZ /
1446 (jiffies - tape->avg_time) / 1024;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447 tape->avg_size = 0;
1448 tape->avg_time = jiffies;
1449 }
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001450 debug_log(DBG_PROCS, "Enter %s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451
Borislav Petkov54bb2072008-02-06 02:57:52 +01001452 tape->first_frame += blocks;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453 rq->current_nr_sectors -= blocks;
1454
1455 if (!tape->pc->error)
1456 idetape_end_request(drive, 1, 0);
1457 else
1458 idetape_end_request(drive, tape->pc->error, 0);
1459 return ide_stopped;
1460}
1461
Borislav Petkovd236d742008-04-18 00:46:27 +02001462static void idetape_create_read_cmd(idetape_tape_t *tape,
1463 struct ide_atapi_pc *pc,
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001464 unsigned int length, struct idetape_bh *bh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465{
1466 idetape_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +01001467 pc->c[0] = READ_6;
Borislav Petkov860ff5e2008-02-02 19:56:50 +01001468 put_unaligned(cpu_to_be32(length), (unsigned int *) &pc->c[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469 pc->c[1] = 1;
Borislav Petkovd236d742008-04-18 00:46:27 +02001470 pc->idetape_callback = &idetape_rw_callback;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001471 pc->bh = bh;
1472 atomic_set(&bh->b_count, 0);
Borislav Petkovd236d742008-04-18 00:46:27 +02001473 pc->buf = NULL;
1474 pc->buf_size = length * tape->blk_size;
1475 pc->req_xfer = pc->buf_size;
1476 if (pc->req_xfer == tape->stage_size)
Borislav Petkov346331f2008-04-18 00:46:26 +02001477 pc->flags |= PC_FLAG_DMA_RECOMMENDED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478}
1479
Borislav Petkovd236d742008-04-18 00:46:27 +02001480static void idetape_create_write_cmd(idetape_tape_t *tape,
1481 struct ide_atapi_pc *pc,
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001482 unsigned int length, struct idetape_bh *bh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483{
1484 idetape_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +01001485 pc->c[0] = WRITE_6;
Borislav Petkov860ff5e2008-02-02 19:56:50 +01001486 put_unaligned(cpu_to_be32(length), (unsigned int *) &pc->c[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487 pc->c[1] = 1;
Borislav Petkovd236d742008-04-18 00:46:27 +02001488 pc->idetape_callback = &idetape_rw_callback;
Borislav Petkov346331f2008-04-18 00:46:26 +02001489 pc->flags |= PC_FLAG_WRITING;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490 pc->bh = bh;
1491 pc->b_data = bh->b_data;
1492 pc->b_count = atomic_read(&bh->b_count);
Borislav Petkovd236d742008-04-18 00:46:27 +02001493 pc->buf = NULL;
1494 pc->buf_size = length * tape->blk_size;
1495 pc->req_xfer = pc->buf_size;
1496 if (pc->req_xfer == tape->stage_size)
Borislav Petkov346331f2008-04-18 00:46:26 +02001497 pc->flags |= PC_FLAG_DMA_RECOMMENDED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498}
1499
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500static ide_startstop_t idetape_do_request(ide_drive_t *drive,
1501 struct request *rq, sector_t block)
1502{
1503 idetape_tape_t *tape = drive->driver_data;
Borislav Petkovd236d742008-04-18 00:46:27 +02001504 struct ide_atapi_pc *pc = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505 struct request *postponed_rq = tape->postponed_rq;
Bartlomiej Zolnierkiewicz22c525b2008-01-25 22:17:11 +01001506 u8 stat;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001508 debug_log(DBG_SENSE, "sector: %ld, nr_sectors: %ld,"
1509 " current_nr_sectors: %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510 rq->sector, rq->nr_sectors, rq->current_nr_sectors);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511
Jens Axboe4aff5e22006-08-10 08:44:47 +02001512 if (!blk_special_request(rq)) {
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001513 /* We do not support buffer cache originated requests. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514 printk(KERN_NOTICE "ide-tape: %s: Unsupported request in "
Jens Axboe4aff5e22006-08-10 08:44:47 +02001515 "request queue (%d)\n", drive->name, rq->cmd_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001516 ide_end_request(drive, 0, 0);
1517 return ide_stopped;
1518 }
1519
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001520 /* Retry a failed packet command */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001521 if (tape->failed_pc && tape->pc->c[0] == REQUEST_SENSE)
Borislav Petkov8d06bfa2008-02-06 02:57:53 +01001522 return idetape_issue_pc(drive, tape->failed_pc);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001523
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524 if (postponed_rq != NULL)
1525 if (rq != postponed_rq) {
1526 printk(KERN_ERR "ide-tape: ide-tape.c bug - "
1527 "Two DSC requests were queued\n");
1528 idetape_end_request(drive, 0, 0);
1529 return ide_stopped;
1530 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531
1532 tape->postponed_rq = NULL;
1533
1534 /*
1535 * If the tape is still busy, postpone our request and service
1536 * the other device meanwhile.
1537 */
Bartlomiej Zolnierkiewiczc47137a2008-02-06 02:57:51 +01001538 stat = ide_read_status(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539
1540 if (!drive->dsc_overlap && !(rq->cmd[0] & REQ_IDETAPE_PC2))
Borislav Petkov346331f2008-04-18 00:46:26 +02001541 set_bit(IDETAPE_FLAG_IGNORE_DSC, &tape->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542
1543 if (drive->post_reset == 1) {
Borislav Petkov346331f2008-04-18 00:46:26 +02001544 set_bit(IDETAPE_FLAG_IGNORE_DSC, &tape->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545 drive->post_reset = 0;
1546 }
1547
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548 if (time_after(jiffies, tape->insert_time))
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001549 tape->insert_speed = tape->insert_size / 1024 * HZ /
1550 (jiffies - tape->insert_time);
Borislav Petkov37016ba2008-02-06 02:57:52 +01001551 idetape_calculate_speeds(drive);
Borislav Petkov346331f2008-04-18 00:46:26 +02001552 if (!test_and_clear_bit(IDETAPE_FLAG_IGNORE_DSC, &tape->flags) &&
Bartlomiej Zolnierkiewicz22c525b2008-01-25 22:17:11 +01001553 (stat & SEEK_STAT) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554 if (postponed_rq == NULL) {
1555 tape->dsc_polling_start = jiffies;
Borislav Petkov54bb2072008-02-06 02:57:52 +01001556 tape->dsc_poll_freq = tape->best_dsc_rw_freq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557 tape->dsc_timeout = jiffies + IDETAPE_DSC_RW_TIMEOUT;
1558 } else if (time_after(jiffies, tape->dsc_timeout)) {
1559 printk(KERN_ERR "ide-tape: %s: DSC timeout\n",
1560 tape->name);
1561 if (rq->cmd[0] & REQ_IDETAPE_PC2) {
1562 idetape_media_access_finished(drive);
1563 return ide_stopped;
1564 } else {
1565 return ide_do_reset(drive);
1566 }
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001567 } else if (time_after(jiffies,
1568 tape->dsc_polling_start +
1569 IDETAPE_DSC_MA_THRESHOLD))
Borislav Petkov54bb2072008-02-06 02:57:52 +01001570 tape->dsc_poll_freq = IDETAPE_DSC_MA_SLOW;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571 idetape_postpone_request(drive);
1572 return ide_stopped;
1573 }
1574 if (rq->cmd[0] & REQ_IDETAPE_READ) {
1575 tape->buffer_head++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001576 tape->postpone_cnt = 0;
1577 pc = idetape_next_pc_storage(drive);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001578 idetape_create_read_cmd(tape, pc, rq->current_nr_sectors,
1579 (struct idetape_bh *)rq->special);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001580 goto out;
1581 }
1582 if (rq->cmd[0] & REQ_IDETAPE_WRITE) {
1583 tape->buffer_head++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584 tape->postpone_cnt = 0;
1585 pc = idetape_next_pc_storage(drive);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001586 idetape_create_write_cmd(tape, pc, rq->current_nr_sectors,
1587 (struct idetape_bh *)rq->special);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001588 goto out;
1589 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590 if (rq->cmd[0] & REQ_IDETAPE_PC1) {
Borislav Petkovd236d742008-04-18 00:46:27 +02001591 pc = (struct ide_atapi_pc *) rq->buffer;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592 rq->cmd[0] &= ~(REQ_IDETAPE_PC1);
1593 rq->cmd[0] |= REQ_IDETAPE_PC2;
1594 goto out;
1595 }
1596 if (rq->cmd[0] & REQ_IDETAPE_PC2) {
1597 idetape_media_access_finished(drive);
1598 return ide_stopped;
1599 }
1600 BUG();
1601out:
Borislav Petkov8d06bfa2008-02-06 02:57:53 +01001602 return idetape_issue_pc(drive, pc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001603}
1604
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001605/* Pipeline related functions */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606
1607/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001608 * The function below uses __get_free_page to allocate a pipeline stage, along
1609 * with all the necessary small buffers which together make a buffer of size
1610 * tape->stage_size (or a bit more). We attempt to combine sequential pages as
1611 * much as possible.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001612 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001613 * It returns a pointer to the new allocated stage, or NULL if we can't (or
1614 * don't want to) allocate a stage.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001615 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001616 * Pipeline stages are optional and are used to increase performance. If we
1617 * can't allocate them, we'll manage without them.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001618 */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001619static idetape_stage_t *__idetape_kmalloc_stage(idetape_tape_t *tape, int full,
1620 int clear)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001621{
1622 idetape_stage_t *stage;
1623 struct idetape_bh *prev_bh, *bh;
1624 int pages = tape->pages_per_stage;
1625 char *b_data = NULL;
1626
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001627 stage = kmalloc(sizeof(idetape_stage_t), GFP_KERNEL);
1628 if (!stage)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001629 return NULL;
1630 stage->next = NULL;
1631
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001632 stage->bh = kmalloc(sizeof(struct idetape_bh), GFP_KERNEL);
1633 bh = stage->bh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634 if (bh == NULL)
1635 goto abort;
1636 bh->b_reqnext = NULL;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001637 bh->b_data = (char *) __get_free_page(GFP_KERNEL);
1638 if (!bh->b_data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001639 goto abort;
1640 if (clear)
1641 memset(bh->b_data, 0, PAGE_SIZE);
1642 bh->b_size = PAGE_SIZE;
1643 atomic_set(&bh->b_count, full ? bh->b_size : 0);
1644
1645 while (--pages) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001646 b_data = (char *) __get_free_page(GFP_KERNEL);
1647 if (!b_data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001648 goto abort;
1649 if (clear)
1650 memset(b_data, 0, PAGE_SIZE);
1651 if (bh->b_data == b_data + PAGE_SIZE) {
1652 bh->b_size += PAGE_SIZE;
1653 bh->b_data -= PAGE_SIZE;
1654 if (full)
1655 atomic_add(PAGE_SIZE, &bh->b_count);
1656 continue;
1657 }
1658 if (b_data == bh->b_data + bh->b_size) {
1659 bh->b_size += PAGE_SIZE;
1660 if (full)
1661 atomic_add(PAGE_SIZE, &bh->b_count);
1662 continue;
1663 }
1664 prev_bh = bh;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001665 bh = kmalloc(sizeof(struct idetape_bh), GFP_KERNEL);
1666 if (!bh) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001667 free_page((unsigned long) b_data);
1668 goto abort;
1669 }
1670 bh->b_reqnext = NULL;
1671 bh->b_data = b_data;
1672 bh->b_size = PAGE_SIZE;
1673 atomic_set(&bh->b_count, full ? bh->b_size : 0);
1674 prev_bh->b_reqnext = bh;
1675 }
1676 bh->b_size -= tape->excess_bh_size;
1677 if (full)
1678 atomic_sub(tape->excess_bh_size, &bh->b_count);
1679 return stage;
1680abort:
1681 __idetape_kfree_stage(stage);
1682 return NULL;
1683}
1684
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001685static idetape_stage_t *idetape_kmalloc_stage(idetape_tape_t *tape)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001686{
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001687 debug_log(DBG_PROCS, "Enter %s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001688
1689 if (tape->nr_stages >= tape->max_stages)
1690 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001691 return __idetape_kmalloc_stage(tape, 0, 0);
1692}
1693
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001694static int idetape_copy_stage_from_user(idetape_tape_t *tape,
1695 idetape_stage_t *stage, const char __user *buf, int n)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001696{
1697 struct idetape_bh *bh = tape->bh;
1698 int count;
Daniel Walkerdcd96372006-06-25 05:47:37 -07001699 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001700
1701 while (n) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001702 if (bh == NULL) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001703 printk(KERN_ERR "ide-tape: bh == NULL in %s\n",
1704 __func__);
Daniel Walkerdcd96372006-06-25 05:47:37 -07001705 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001706 }
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001707 count = min((unsigned int)
1708 (bh->b_size - atomic_read(&bh->b_count)),
1709 (unsigned int)n);
1710 if (copy_from_user(bh->b_data + atomic_read(&bh->b_count), buf,
1711 count))
Daniel Walkerdcd96372006-06-25 05:47:37 -07001712 ret = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713 n -= count;
1714 atomic_add(count, &bh->b_count);
1715 buf += count;
1716 if (atomic_read(&bh->b_count) == bh->b_size) {
1717 bh = bh->b_reqnext;
1718 if (bh)
1719 atomic_set(&bh->b_count, 0);
1720 }
1721 }
1722 tape->bh = bh;
Daniel Walkerdcd96372006-06-25 05:47:37 -07001723 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001724}
1725
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001726static int idetape_copy_stage_to_user(idetape_tape_t *tape, char __user *buf,
1727 idetape_stage_t *stage, int n)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001728{
1729 struct idetape_bh *bh = tape->bh;
1730 int count;
Daniel Walkerdcd96372006-06-25 05:47:37 -07001731 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001732
1733 while (n) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001734 if (bh == NULL) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001735 printk(KERN_ERR "ide-tape: bh == NULL in %s\n",
1736 __func__);
Daniel Walkerdcd96372006-06-25 05:47:37 -07001737 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001738 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001739 count = min(tape->b_count, n);
Daniel Walkerdcd96372006-06-25 05:47:37 -07001740 if (copy_to_user(buf, tape->b_data, count))
1741 ret = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001742 n -= count;
1743 tape->b_data += count;
1744 tape->b_count -= count;
1745 buf += count;
1746 if (!tape->b_count) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001747 bh = bh->b_reqnext;
1748 tape->bh = bh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001749 if (bh) {
1750 tape->b_data = bh->b_data;
1751 tape->b_count = atomic_read(&bh->b_count);
1752 }
1753 }
1754 }
Daniel Walkerdcd96372006-06-25 05:47:37 -07001755 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001756}
1757
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001758static void idetape_init_merge_stage(idetape_tape_t *tape)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001759{
1760 struct idetape_bh *bh = tape->merge_stage->bh;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001761
Linus Torvalds1da177e2005-04-16 15:20:36 -07001762 tape->bh = bh;
Borislav Petkov54abf372008-02-06 02:57:52 +01001763 if (tape->chrdev_dir == IDETAPE_DIR_WRITE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001764 atomic_set(&bh->b_count, 0);
1765 else {
1766 tape->b_data = bh->b_data;
1767 tape->b_count = atomic_read(&bh->b_count);
1768 }
1769}
1770
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001771static void idetape_switch_buffers(idetape_tape_t *tape, idetape_stage_t *stage)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001772{
1773 struct idetape_bh *tmp;
1774
1775 tmp = stage->bh;
1776 stage->bh = tape->merge_stage->bh;
1777 tape->merge_stage->bh = tmp;
1778 idetape_init_merge_stage(tape);
1779}
1780
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001781/* Add a new stage at the end of the pipeline. */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001782static void idetape_add_stage_tail(ide_drive_t *drive, idetape_stage_t *stage)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001783{
1784 idetape_tape_t *tape = drive->driver_data;
1785 unsigned long flags;
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001786
1787 debug_log(DBG_PROCS, "Enter %s\n", __func__);
1788
Borislav Petkov54bb2072008-02-06 02:57:52 +01001789 spin_lock_irqsave(&tape->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790 stage->next = NULL;
1791 if (tape->last_stage != NULL)
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001792 tape->last_stage->next = stage;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001793 else
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001794 tape->first_stage = stage;
1795 tape->next_stage = stage;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001796 tape->last_stage = stage;
1797 if (tape->next_stage == NULL)
1798 tape->next_stage = tape->last_stage;
1799 tape->nr_stages++;
1800 tape->nr_pending_stages++;
Borislav Petkov54bb2072008-02-06 02:57:52 +01001801 spin_unlock_irqrestore(&tape->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001802}
1803
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001804/* Install a completion in a pending request and sleep until it is serviced. The
1805 * caller should ensure that the request will not be serviced before we install
1806 * the completion (usually by disabling interrupts).
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807 */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001808static void idetape_wait_for_request(ide_drive_t *drive, struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001809{
Peter Zijlstra6e9a4732006-09-30 23:28:10 -07001810 DECLARE_COMPLETION_ONSTACK(wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001811 idetape_tape_t *tape = drive->driver_data;
1812
Jens Axboe4aff5e22006-08-10 08:44:47 +02001813 if (rq == NULL || !blk_special_request(rq)) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001814 printk(KERN_ERR "ide-tape: bug: Trying to sleep on non-valid"
1815 " request\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001816 return;
1817 }
Jens Axboec00895a2006-09-30 20:29:12 +02001818 rq->end_io_data = &wait;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001819 rq->end_io = blk_end_sync_rq;
Borislav Petkov54bb2072008-02-06 02:57:52 +01001820 spin_unlock_irq(&tape->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001821 wait_for_completion(&wait);
1822 /* The stage and its struct request have been deallocated */
Borislav Petkov54bb2072008-02-06 02:57:52 +01001823 spin_lock_irq(&tape->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001824}
1825
Borislav Petkova2f5b7f2008-02-06 02:57:51 +01001826static ide_startstop_t idetape_read_position_callback(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001827{
1828 idetape_tape_t *tape = drive->driver_data;
Borislav Petkovd236d742008-04-18 00:46:27 +02001829 u8 *readpos = tape->pc->buf;
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001830
1831 debug_log(DBG_PROCS, "Enter %s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001832
1833 if (!tape->pc->error) {
Borislav Petkova2f5b7f2008-02-06 02:57:51 +01001834 debug_log(DBG_SENSE, "BOP - %s\n",
1835 (readpos[0] & 0x80) ? "Yes" : "No");
1836 debug_log(DBG_SENSE, "EOP - %s\n",
1837 (readpos[0] & 0x40) ? "Yes" : "No");
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001838
Borislav Petkova2f5b7f2008-02-06 02:57:51 +01001839 if (readpos[0] & 0x4) {
1840 printk(KERN_INFO "ide-tape: Block location is unknown"
1841 "to the tape\n");
Borislav Petkov346331f2008-04-18 00:46:26 +02001842 clear_bit(IDETAPE_FLAG_ADDRESS_VALID, &tape->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001843 idetape_end_request(drive, 0, 0);
1844 } else {
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001845 debug_log(DBG_SENSE, "Block Location - %u\n",
Borislav Petkova2f5b7f2008-02-06 02:57:51 +01001846 be32_to_cpu(*(u32 *)&readpos[4]));
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001847
Borislav Petkova2f5b7f2008-02-06 02:57:51 +01001848 tape->partition = readpos[1];
Borislav Petkov54bb2072008-02-06 02:57:52 +01001849 tape->first_frame =
Borislav Petkova2f5b7f2008-02-06 02:57:51 +01001850 be32_to_cpu(*(u32 *)&readpos[4]);
Borislav Petkov346331f2008-04-18 00:46:26 +02001851 set_bit(IDETAPE_FLAG_ADDRESS_VALID, &tape->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001852 idetape_end_request(drive, 1, 0);
1853 }
1854 } else {
1855 idetape_end_request(drive, 0, 0);
1856 }
1857 return ide_stopped;
1858}
1859
1860/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001861 * Write a filemark if write_filemark=1. Flush the device buffers without
1862 * writing a filemark otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001863 */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001864static void idetape_create_write_filemark_cmd(ide_drive_t *drive,
Borislav Petkovd236d742008-04-18 00:46:27 +02001865 struct ide_atapi_pc *pc, int write_filemark)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001866{
1867 idetape_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +01001868 pc->c[0] = WRITE_FILEMARKS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001869 pc->c[4] = write_filemark;
Borislav Petkov346331f2008-04-18 00:46:26 +02001870 pc->flags |= PC_FLAG_WAIT_FOR_DSC;
Borislav Petkovd236d742008-04-18 00:46:27 +02001871 pc->idetape_callback = &idetape_pc_callback;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001872}
1873
Borislav Petkovd236d742008-04-18 00:46:27 +02001874static void idetape_create_test_unit_ready_cmd(struct ide_atapi_pc *pc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001875{
1876 idetape_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +01001877 pc->c[0] = TEST_UNIT_READY;
Borislav Petkovd236d742008-04-18 00:46:27 +02001878 pc->idetape_callback = &idetape_pc_callback;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001879}
1880
1881/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001882 * We add a special packet command request to the tail of the request queue, and
1883 * wait for it to be serviced. This is not to be called from within the request
1884 * handling part of the driver! We allocate here data on the stack and it is
1885 * valid until the request is finished. This is not the case for the bottom part
1886 * of the driver, where we are always leaving the functions to wait for an
1887 * interrupt or a timer event.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001888 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001889 * From the bottom part of the driver, we should allocate safe memory using
1890 * idetape_next_pc_storage() and ide_tape_next_rq_storage(), and add the request
1891 * to the request list without waiting for it to be serviced! In that case, we
1892 * usually use idetape_queue_pc_head().
Linus Torvalds1da177e2005-04-16 15:20:36 -07001893 */
Borislav Petkovd236d742008-04-18 00:46:27 +02001894static int __idetape_queue_pc_tail(ide_drive_t *drive, struct ide_atapi_pc *pc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001895{
1896 struct ide_tape_obj *tape = drive->driver_data;
1897 struct request rq;
1898
1899 idetape_init_rq(&rq, REQ_IDETAPE_PC1);
1900 rq.buffer = (char *) pc;
1901 rq.rq_disk = tape->disk;
1902 return ide_do_drive_cmd(drive, &rq, ide_wait);
1903}
1904
Borislav Petkovd236d742008-04-18 00:46:27 +02001905static void idetape_create_load_unload_cmd(ide_drive_t *drive,
1906 struct ide_atapi_pc *pc, int cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001907{
1908 idetape_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +01001909 pc->c[0] = START_STOP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001910 pc->c[4] = cmd;
Borislav Petkov346331f2008-04-18 00:46:26 +02001911 pc->flags |= PC_FLAG_WAIT_FOR_DSC;
Borislav Petkovd236d742008-04-18 00:46:27 +02001912 pc->idetape_callback = &idetape_pc_callback;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001913}
1914
1915static int idetape_wait_ready(ide_drive_t *drive, unsigned long timeout)
1916{
1917 idetape_tape_t *tape = drive->driver_data;
Borislav Petkovd236d742008-04-18 00:46:27 +02001918 struct ide_atapi_pc pc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001919 int load_attempted = 0;
1920
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001921 /* Wait for the tape to become ready */
Borislav Petkov346331f2008-04-18 00:46:26 +02001922 set_bit(IDETAPE_FLAG_MEDIUM_PRESENT, &tape->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001923 timeout += jiffies;
1924 while (time_before(jiffies, timeout)) {
1925 idetape_create_test_unit_ready_cmd(&pc);
1926 if (!__idetape_queue_pc_tail(drive, &pc))
1927 return 0;
1928 if ((tape->sense_key == 2 && tape->asc == 4 && tape->ascq == 2)
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001929 || (tape->asc == 0x3A)) {
1930 /* no media */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001931 if (load_attempted)
1932 return -ENOMEDIUM;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001933 idetape_create_load_unload_cmd(drive, &pc,
1934 IDETAPE_LU_LOAD_MASK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001935 __idetape_queue_pc_tail(drive, &pc);
1936 load_attempted = 1;
1937 /* not about to be ready */
1938 } else if (!(tape->sense_key == 2 && tape->asc == 4 &&
1939 (tape->ascq == 1 || tape->ascq == 8)))
1940 return -EIO;
Nishanth Aravamudan80ce45f2005-09-10 00:27:08 -07001941 msleep(100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001942 }
1943 return -EIO;
1944}
1945
Borislav Petkovd236d742008-04-18 00:46:27 +02001946static int idetape_queue_pc_tail(ide_drive_t *drive, struct ide_atapi_pc *pc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001947{
1948 return __idetape_queue_pc_tail(drive, pc);
1949}
1950
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001951static int idetape_flush_tape_buffers(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001952{
Borislav Petkovd236d742008-04-18 00:46:27 +02001953 struct ide_atapi_pc pc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001954 int rc;
1955
1956 idetape_create_write_filemark_cmd(drive, &pc, 0);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001957 rc = idetape_queue_pc_tail(drive, &pc);
1958 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001959 return rc;
1960 idetape_wait_ready(drive, 60 * 5 * HZ);
1961 return 0;
1962}
1963
Borislav Petkovd236d742008-04-18 00:46:27 +02001964static void idetape_create_read_position_cmd(struct ide_atapi_pc *pc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001965{
1966 idetape_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +01001967 pc->c[0] = READ_POSITION;
Borislav Petkovd236d742008-04-18 00:46:27 +02001968 pc->req_xfer = 20;
1969 pc->idetape_callback = &idetape_read_position_callback;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001970}
1971
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001972static int idetape_read_position(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001973{
1974 idetape_tape_t *tape = drive->driver_data;
Borislav Petkovd236d742008-04-18 00:46:27 +02001975 struct ide_atapi_pc pc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001976 int position;
1977
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001978 debug_log(DBG_PROCS, "Enter %s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001979
1980 idetape_create_read_position_cmd(&pc);
1981 if (idetape_queue_pc_tail(drive, &pc))
1982 return -1;
Borislav Petkov54bb2072008-02-06 02:57:52 +01001983 position = tape->first_frame;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001984 return position;
1985}
1986
Borislav Petkovd236d742008-04-18 00:46:27 +02001987static void idetape_create_locate_cmd(ide_drive_t *drive,
1988 struct ide_atapi_pc *pc,
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001989 unsigned int block, u8 partition, int skip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001990{
1991 idetape_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +01001992 pc->c[0] = POSITION_TO_ELEMENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001993 pc->c[1] = 2;
Borislav Petkov860ff5e2008-02-02 19:56:50 +01001994 put_unaligned(cpu_to_be32(block), (unsigned int *) &pc->c[3]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001995 pc->c[8] = partition;
Borislav Petkov346331f2008-04-18 00:46:26 +02001996 pc->flags |= PC_FLAG_WAIT_FOR_DSC;
Borislav Petkovd236d742008-04-18 00:46:27 +02001997 pc->idetape_callback = &idetape_pc_callback;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001998}
1999
Borislav Petkovd236d742008-04-18 00:46:27 +02002000static int idetape_create_prevent_cmd(ide_drive_t *drive,
2001 struct ide_atapi_pc *pc, int prevent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002002{
2003 idetape_tape_t *tape = drive->driver_data;
2004
Borislav Petkovb6422012008-02-02 19:56:49 +01002005 /* device supports locking according to capabilities page */
2006 if (!(tape->caps[6] & 0x01))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002007 return 0;
2008
2009 idetape_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +01002010 pc->c[0] = ALLOW_MEDIUM_REMOVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002011 pc->c[4] = prevent;
Borislav Petkovd236d742008-04-18 00:46:27 +02002012 pc->idetape_callback = &idetape_pc_callback;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002013 return 1;
2014}
2015
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002016static int __idetape_discard_read_pipeline(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002017{
2018 idetape_tape_t *tape = drive->driver_data;
2019 unsigned long flags;
2020 int cnt;
2021
Borislav Petkov54abf372008-02-06 02:57:52 +01002022 if (tape->chrdev_dir != IDETAPE_DIR_READ)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002023 return 0;
2024
2025 /* Remove merge stage. */
Borislav Petkov54bb2072008-02-06 02:57:52 +01002026 cnt = tape->merge_stage_size / tape->blk_size;
Borislav Petkov346331f2008-04-18 00:46:26 +02002027 if (test_and_clear_bit(IDETAPE_FLAG_FILEMARK, &tape->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002028 ++cnt; /* Filemarks count as 1 sector */
2029 tape->merge_stage_size = 0;
2030 if (tape->merge_stage != NULL) {
2031 __idetape_kfree_stage(tape->merge_stage);
2032 tape->merge_stage = NULL;
2033 }
2034
2035 /* Clear pipeline flags. */
Borislav Petkov346331f2008-04-18 00:46:26 +02002036 clear_bit(IDETAPE_FLAG_PIPELINE_ERR, &tape->flags);
Borislav Petkov54abf372008-02-06 02:57:52 +01002037 tape->chrdev_dir = IDETAPE_DIR_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002038
2039 /* Remove pipeline stages. */
2040 if (tape->first_stage == NULL)
2041 return 0;
2042
Borislav Petkov54bb2072008-02-06 02:57:52 +01002043 spin_lock_irqsave(&tape->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002044 tape->next_stage = NULL;
Borislav Petkovc4b22f82008-04-26 22:25:20 +02002045 if (test_bit(IDETAPE_FLAG_PIPELINE_ACTIVE, &tape->flags))
Borislav Petkov54bb2072008-02-06 02:57:52 +01002046 idetape_wait_for_request(drive, tape->active_data_rq);
2047 spin_unlock_irqrestore(&tape->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002048
2049 while (tape->first_stage != NULL) {
2050 struct request *rq_ptr = &tape->first_stage->rq;
2051
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002052 cnt += rq_ptr->nr_sectors - rq_ptr->current_nr_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002053 if (rq_ptr->errors == IDETAPE_ERROR_FILEMARK)
2054 ++cnt;
2055 idetape_remove_stage_head(drive);
2056 }
2057 tape->nr_pending_stages = 0;
2058 tape->max_stages = tape->min_pipeline;
2059 return cnt;
2060}
2061
2062/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +01002063 * Position the tape to the requested block using the LOCATE packet command.
2064 * A READ POSITION command is then issued to check where we are positioned. Like
2065 * all higher level operations, we queue the commands at the tail of the request
2066 * queue and wait for their completion.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002067 */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002068static int idetape_position_tape(ide_drive_t *drive, unsigned int block,
2069 u8 partition, int skip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002070{
2071 idetape_tape_t *tape = drive->driver_data;
2072 int retval;
Borislav Petkovd236d742008-04-18 00:46:27 +02002073 struct ide_atapi_pc pc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002074
Borislav Petkov54abf372008-02-06 02:57:52 +01002075 if (tape->chrdev_dir == IDETAPE_DIR_READ)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002076 __idetape_discard_read_pipeline(drive);
2077 idetape_wait_ready(drive, 60 * 5 * HZ);
2078 idetape_create_locate_cmd(drive, &pc, block, partition, skip);
2079 retval = idetape_queue_pc_tail(drive, &pc);
2080 if (retval)
2081 return (retval);
2082
2083 idetape_create_read_position_cmd(&pc);
2084 return (idetape_queue_pc_tail(drive, &pc));
2085}
2086
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002087static void idetape_discard_read_pipeline(ide_drive_t *drive,
2088 int restore_position)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002089{
2090 idetape_tape_t *tape = drive->driver_data;
2091 int cnt;
2092 int seek, position;
2093
2094 cnt = __idetape_discard_read_pipeline(drive);
2095 if (restore_position) {
2096 position = idetape_read_position(drive);
2097 seek = position > cnt ? position - cnt : 0;
2098 if (idetape_position_tape(drive, seek, 0, 0)) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002099 printk(KERN_INFO "ide-tape: %s: position_tape failed in"
2100 " discard_pipeline()\n", tape->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002101 return;
2102 }
2103 }
2104}
2105
2106/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +01002107 * Generate a read/write request for the block device interface and wait for it
2108 * to be serviced.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002109 */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002110static int idetape_queue_rw_tail(ide_drive_t *drive, int cmd, int blocks,
2111 struct idetape_bh *bh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002112{
2113 idetape_tape_t *tape = drive->driver_data;
2114 struct request rq;
2115
Borislav Petkov8004a8c2008-02-06 02:57:51 +01002116 debug_log(DBG_SENSE, "%s: cmd=%d\n", __func__, cmd);
2117
Borislav Petkovc4b22f82008-04-26 22:25:20 +02002118 if (test_bit(IDETAPE_FLAG_PIPELINE_ACTIVE, &tape->flags)) {
Borislav Petkov8004a8c2008-02-06 02:57:51 +01002119 printk(KERN_ERR "ide-tape: bug: the pipeline is active in %s\n",
2120 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002121 return (0);
2122 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002123
2124 idetape_init_rq(&rq, cmd);
2125 rq.rq_disk = tape->disk;
2126 rq.special = (void *)bh;
Borislav Petkov54bb2072008-02-06 02:57:52 +01002127 rq.sector = tape->first_frame;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002128 rq.nr_sectors = blocks;
2129 rq.current_nr_sectors = blocks;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002130 (void) ide_do_drive_cmd(drive, &rq, ide_wait);
2131
2132 if ((cmd & (REQ_IDETAPE_READ | REQ_IDETAPE_WRITE)) == 0)
2133 return 0;
2134
2135 if (tape->merge_stage)
2136 idetape_init_merge_stage(tape);
2137 if (rq.errors == IDETAPE_ERROR_GENERAL)
2138 return -EIO;
Borislav Petkov54bb2072008-02-06 02:57:52 +01002139 return (tape->blk_size * (blocks-rq.current_nr_sectors));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002140}
2141
Borislav Petkov8d06bfa2008-02-06 02:57:53 +01002142/* start servicing the pipeline stages, starting from tape->next_stage. */
2143static void idetape_plug_pipeline(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002144{
2145 idetape_tape_t *tape = drive->driver_data;
2146
2147 if (tape->next_stage == NULL)
2148 return;
Borislav Petkovc4b22f82008-04-26 22:25:20 +02002149 if (!test_and_set_bit(IDETAPE_FLAG_PIPELINE_ACTIVE, &tape->flags)) {
Borislav Petkov419d4742008-02-02 19:56:51 +01002150 idetape_activate_next_stage(drive);
Borislav Petkov54bb2072008-02-06 02:57:52 +01002151 (void) ide_do_drive_cmd(drive, tape->active_data_rq, ide_end);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002152 }
2153}
2154
Borislav Petkovd236d742008-04-18 00:46:27 +02002155static void idetape_create_inquiry_cmd(struct ide_atapi_pc *pc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002156{
2157 idetape_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +01002158 pc->c[0] = INQUIRY;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002159 pc->c[4] = 254;
Borislav Petkovd236d742008-04-18 00:46:27 +02002160 pc->req_xfer = 254;
2161 pc->idetape_callback = &idetape_pc_callback;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002162}
2163
Borislav Petkovd236d742008-04-18 00:46:27 +02002164static void idetape_create_rewind_cmd(ide_drive_t *drive,
2165 struct ide_atapi_pc *pc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002166{
2167 idetape_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +01002168 pc->c[0] = REZERO_UNIT;
Borislav Petkov346331f2008-04-18 00:46:26 +02002169 pc->flags |= PC_FLAG_WAIT_FOR_DSC;
Borislav Petkovd236d742008-04-18 00:46:27 +02002170 pc->idetape_callback = &idetape_pc_callback;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002171}
2172
Borislav Petkovd236d742008-04-18 00:46:27 +02002173static void idetape_create_erase_cmd(struct ide_atapi_pc *pc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002174{
2175 idetape_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +01002176 pc->c[0] = ERASE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002177 pc->c[1] = 1;
Borislav Petkov346331f2008-04-18 00:46:26 +02002178 pc->flags |= PC_FLAG_WAIT_FOR_DSC;
Borislav Petkovd236d742008-04-18 00:46:27 +02002179 pc->idetape_callback = &idetape_pc_callback;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002180}
2181
Borislav Petkovd236d742008-04-18 00:46:27 +02002182static void idetape_create_space_cmd(struct ide_atapi_pc *pc, int count, u8 cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002183{
2184 idetape_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +01002185 pc->c[0] = SPACE;
Borislav Petkov860ff5e2008-02-02 19:56:50 +01002186 put_unaligned(cpu_to_be32(count), (unsigned int *) &pc->c[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002187 pc->c[1] = cmd;
Borislav Petkov346331f2008-04-18 00:46:26 +02002188 pc->flags |= PC_FLAG_WAIT_FOR_DSC;
Borislav Petkovd236d742008-04-18 00:46:27 +02002189 pc->idetape_callback = &idetape_pc_callback;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002190}
2191
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002192static void idetape_wait_first_stage(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002193{
2194 idetape_tape_t *tape = drive->driver_data;
2195 unsigned long flags;
2196
2197 if (tape->first_stage == NULL)
2198 return;
Borislav Petkov54bb2072008-02-06 02:57:52 +01002199 spin_lock_irqsave(&tape->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002200 if (tape->active_stage == tape->first_stage)
Borislav Petkov54bb2072008-02-06 02:57:52 +01002201 idetape_wait_for_request(drive, tape->active_data_rq);
2202 spin_unlock_irqrestore(&tape->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002203}
2204
2205/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +01002206 * Try to add a character device originated write request to our pipeline. In
2207 * case we don't succeed, we revert to non-pipelined operation mode for this
2208 * request. In order to accomplish that, we
Linus Torvalds1da177e2005-04-16 15:20:36 -07002209 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +01002210 * 1. Try to allocate a new pipeline stage.
2211 * 2. If we can't, wait for more and more requests to be serviced and try again
2212 * each time.
2213 * 3. If we still can't allocate a stage, fallback to non-pipelined operation
2214 * mode for this request.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002215 */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002216static int idetape_add_chrdev_write_request(ide_drive_t *drive, int blocks)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002217{
2218 idetape_tape_t *tape = drive->driver_data;
2219 idetape_stage_t *new_stage;
2220 unsigned long flags;
2221 struct request *rq;
2222
Borislav Petkov8004a8c2008-02-06 02:57:51 +01002223 debug_log(DBG_CHRDEV, "Enter %s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002224
Borislav Petkov3c98bf32008-02-06 02:57:53 +01002225 /* Attempt to allocate a new stage. Beware possible race conditions. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002226 while ((new_stage = idetape_kmalloc_stage(tape)) == NULL) {
Borislav Petkov54bb2072008-02-06 02:57:52 +01002227 spin_lock_irqsave(&tape->lock, flags);
Borislav Petkovc4b22f82008-04-26 22:25:20 +02002228 if (test_bit(IDETAPE_FLAG_PIPELINE_ACTIVE, &tape->flags)) {
Borislav Petkov54bb2072008-02-06 02:57:52 +01002229 idetape_wait_for_request(drive, tape->active_data_rq);
2230 spin_unlock_irqrestore(&tape->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002231 } else {
Borislav Petkov54bb2072008-02-06 02:57:52 +01002232 spin_unlock_irqrestore(&tape->lock, flags);
Borislav Petkov8d06bfa2008-02-06 02:57:53 +01002233 idetape_plug_pipeline(drive);
Borislav Petkovc4b22f82008-04-26 22:25:20 +02002234 if (test_bit(IDETAPE_FLAG_PIPELINE_ACTIVE,
2235 &tape->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002236 continue;
2237 /*
Borislav Petkov3c98bf32008-02-06 02:57:53 +01002238 * The machine is short on memory. Fallback to non-
2239 * pipelined operation mode for this request.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002240 */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002241 return idetape_queue_rw_tail(drive, REQ_IDETAPE_WRITE,
2242 blocks, tape->merge_stage->bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002243 }
2244 }
2245 rq = &new_stage->rq;
2246 idetape_init_rq(rq, REQ_IDETAPE_WRITE);
2247 /* Doesn't actually matter - We always assume sequential access */
Borislav Petkov54bb2072008-02-06 02:57:52 +01002248 rq->sector = tape->first_frame;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002249 rq->current_nr_sectors = blocks;
2250 rq->nr_sectors = blocks;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002251
2252 idetape_switch_buffers(tape, new_stage);
2253 idetape_add_stage_tail(drive, new_stage);
2254 tape->pipeline_head++;
Borislav Petkov37016ba2008-02-06 02:57:52 +01002255 idetape_calculate_speeds(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002256
2257 /*
Borislav Petkov3c98bf32008-02-06 02:57:53 +01002258 * Estimate whether the tape has stopped writing by checking if our
2259 * write pipeline is currently empty. If we are not writing anymore,
2260 * wait for the pipeline to be almost completely full (90%) before
2261 * starting to service requests, so that we will be able to keep up with
2262 * the higher speeds of the tape.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002263 */
Borislav Petkovc4b22f82008-04-26 22:25:20 +02002264 if (!test_bit(IDETAPE_FLAG_PIPELINE_ACTIVE, &tape->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002265 if (tape->nr_stages >= tape->max_stages * 9 / 10 ||
Borislav Petkov54bb2072008-02-06 02:57:52 +01002266 tape->nr_stages >= tape->max_stages -
2267 tape->uncontrolled_pipeline_head_speed * 3 * 1024 /
2268 tape->blk_size) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002269 tape->measure_insert_time = 1;
2270 tape->insert_time = jiffies;
2271 tape->insert_size = 0;
2272 tape->insert_speed = 0;
Borislav Petkov8d06bfa2008-02-06 02:57:53 +01002273 idetape_plug_pipeline(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002274 }
2275 }
Borislav Petkov346331f2008-04-18 00:46:26 +02002276 if (test_and_clear_bit(IDETAPE_FLAG_PIPELINE_ERR, &tape->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002277 /* Return a deferred error */
2278 return -EIO;
2279 return blocks;
2280}
2281
2282/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +01002283 * Wait until all pending pipeline requests are serviced. Typically called on
2284 * device close.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002285 */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002286static void idetape_wait_for_pipeline(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002287{
2288 idetape_tape_t *tape = drive->driver_data;
2289 unsigned long flags;
2290
Borislav Petkovc4b22f82008-04-26 22:25:20 +02002291 while (tape->next_stage || test_bit(IDETAPE_FLAG_PIPELINE_ACTIVE,
2292 &tape->flags)) {
Borislav Petkov8d06bfa2008-02-06 02:57:53 +01002293 idetape_plug_pipeline(drive);
Borislav Petkov54bb2072008-02-06 02:57:52 +01002294 spin_lock_irqsave(&tape->lock, flags);
Borislav Petkovc4b22f82008-04-26 22:25:20 +02002295 if (test_bit(IDETAPE_FLAG_PIPELINE_ACTIVE, &tape->flags))
Borislav Petkov54bb2072008-02-06 02:57:52 +01002296 idetape_wait_for_request(drive, tape->active_data_rq);
2297 spin_unlock_irqrestore(&tape->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002298 }
2299}
2300
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002301static void idetape_empty_write_pipeline(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002302{
2303 idetape_tape_t *tape = drive->driver_data;
2304 int blocks, min;
2305 struct idetape_bh *bh;
Borislav Petkov55a5d292008-02-02 19:56:49 +01002306
Borislav Petkov54abf372008-02-06 02:57:52 +01002307 if (tape->chrdev_dir != IDETAPE_DIR_WRITE) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002308 printk(KERN_ERR "ide-tape: bug: Trying to empty write pipeline,"
2309 " but we are not writing.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002310 return;
2311 }
2312 if (tape->merge_stage_size > tape->stage_size) {
2313 printk(KERN_ERR "ide-tape: bug: merge_buffer too big\n");
2314 tape->merge_stage_size = tape->stage_size;
2315 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002316 if (tape->merge_stage_size) {
Borislav Petkov54bb2072008-02-06 02:57:52 +01002317 blocks = tape->merge_stage_size / tape->blk_size;
2318 if (tape->merge_stage_size % tape->blk_size) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002319 unsigned int i;
2320
2321 blocks++;
Borislav Petkov54bb2072008-02-06 02:57:52 +01002322 i = tape->blk_size - tape->merge_stage_size %
2323 tape->blk_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002324 bh = tape->bh->b_reqnext;
2325 while (bh) {
2326 atomic_set(&bh->b_count, 0);
2327 bh = bh->b_reqnext;
2328 }
2329 bh = tape->bh;
2330 while (i) {
2331 if (bh == NULL) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002332 printk(KERN_INFO "ide-tape: bug,"
2333 " bh NULL\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002334 break;
2335 }
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002336 min = min(i, (unsigned int)(bh->b_size -
2337 atomic_read(&bh->b_count)));
2338 memset(bh->b_data + atomic_read(&bh->b_count),
2339 0, min);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002340 atomic_add(min, &bh->b_count);
2341 i -= min;
2342 bh = bh->b_reqnext;
2343 }
2344 }
2345 (void) idetape_add_chrdev_write_request(drive, blocks);
2346 tape->merge_stage_size = 0;
2347 }
2348 idetape_wait_for_pipeline(drive);
2349 if (tape->merge_stage != NULL) {
2350 __idetape_kfree_stage(tape->merge_stage);
2351 tape->merge_stage = NULL;
2352 }
Borislav Petkov346331f2008-04-18 00:46:26 +02002353 clear_bit(IDETAPE_FLAG_PIPELINE_ERR, &tape->flags);
Borislav Petkov54abf372008-02-06 02:57:52 +01002354 tape->chrdev_dir = IDETAPE_DIR_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002355
2356 /*
Borislav Petkov3c98bf32008-02-06 02:57:53 +01002357 * On the next backup, perform the feedback loop again. (I don't want to
2358 * keep sense information between backups, as some systems are
2359 * constantly on, and the system load can be totally different on the
2360 * next backup).
Linus Torvalds1da177e2005-04-16 15:20:36 -07002361 */
2362 tape->max_stages = tape->min_pipeline;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002363 if (tape->first_stage != NULL ||
2364 tape->next_stage != NULL ||
2365 tape->last_stage != NULL ||
2366 tape->nr_stages != 0) {
2367 printk(KERN_ERR "ide-tape: ide-tape pipeline bug, "
2368 "first_stage %p, next_stage %p, "
2369 "last_stage %p, nr_stages %d\n",
2370 tape->first_stage, tape->next_stage,
2371 tape->last_stage, tape->nr_stages);
2372 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002373}
2374
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002375static void idetape_restart_speed_control(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002376{
2377 idetape_tape_t *tape = drive->driver_data;
2378
2379 tape->restart_speed_control_req = 0;
2380 tape->pipeline_head = 0;
Borislav Petkov41f81d542008-02-06 02:57:52 +01002381 tape->controlled_last_pipeline_head = 0;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002382 tape->controlled_previous_pipeline_head = 0;
2383 tape->uncontrolled_previous_pipeline_head = 0;
2384 tape->controlled_pipeline_head_speed = 5000;
2385 tape->pipeline_head_speed = 5000;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002386 tape->uncontrolled_pipeline_head_speed = 0;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002387 tape->controlled_pipeline_head_time =
2388 tape->uncontrolled_pipeline_head_time = jiffies;
2389 tape->controlled_previous_head_time =
2390 tape->uncontrolled_previous_head_time = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002391}
2392
Borislav Petkov8d06bfa2008-02-06 02:57:53 +01002393static int idetape_init_read(ide_drive_t *drive, int max_stages)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002394{
2395 idetape_tape_t *tape = drive->driver_data;
2396 idetape_stage_t *new_stage;
2397 struct request rq;
2398 int bytes_read;
Borislav Petkovb6422012008-02-02 19:56:49 +01002399 u16 blocks = *(u16 *)&tape->caps[12];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002400
2401 /* Initialize read operation */
Borislav Petkov54abf372008-02-06 02:57:52 +01002402 if (tape->chrdev_dir != IDETAPE_DIR_READ) {
2403 if (tape->chrdev_dir == IDETAPE_DIR_WRITE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002404 idetape_empty_write_pipeline(drive);
2405 idetape_flush_tape_buffers(drive);
2406 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002407 if (tape->merge_stage || tape->merge_stage_size) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002408 printk(KERN_ERR "ide-tape: merge_stage_size should be"
2409 " 0 now\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002410 tape->merge_stage_size = 0;
2411 }
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002412 tape->merge_stage = __idetape_kmalloc_stage(tape, 0, 0);
2413 if (!tape->merge_stage)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002414 return -ENOMEM;
Borislav Petkov54abf372008-02-06 02:57:52 +01002415 tape->chrdev_dir = IDETAPE_DIR_READ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002416
2417 /*
Borislav Petkov3c98bf32008-02-06 02:57:53 +01002418 * Issue a read 0 command to ensure that DSC handshake is
2419 * switched from completion mode to buffer available mode.
2420 * No point in issuing this if DSC overlap isn't supported, some
2421 * drives (Seagate STT3401A) will return an error.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002422 */
2423 if (drive->dsc_overlap) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002424 bytes_read = idetape_queue_rw_tail(drive,
2425 REQ_IDETAPE_READ, 0,
2426 tape->merge_stage->bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002427 if (bytes_read < 0) {
2428 __idetape_kfree_stage(tape->merge_stage);
2429 tape->merge_stage = NULL;
Borislav Petkov54abf372008-02-06 02:57:52 +01002430 tape->chrdev_dir = IDETAPE_DIR_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002431 return bytes_read;
2432 }
2433 }
2434 }
2435 if (tape->restart_speed_control_req)
2436 idetape_restart_speed_control(drive);
2437 idetape_init_rq(&rq, REQ_IDETAPE_READ);
Borislav Petkov54bb2072008-02-06 02:57:52 +01002438 rq.sector = tape->first_frame;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002439 rq.nr_sectors = blocks;
2440 rq.current_nr_sectors = blocks;
Borislav Petkov346331f2008-04-18 00:46:26 +02002441 if (!test_bit(IDETAPE_FLAG_PIPELINE_ERR, &tape->flags) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07002442 tape->nr_stages < max_stages) {
2443 new_stage = idetape_kmalloc_stage(tape);
2444 while (new_stage != NULL) {
2445 new_stage->rq = rq;
2446 idetape_add_stage_tail(drive, new_stage);
2447 if (tape->nr_stages >= max_stages)
2448 break;
2449 new_stage = idetape_kmalloc_stage(tape);
2450 }
2451 }
Borislav Petkovc4b22f82008-04-26 22:25:20 +02002452 if (!test_bit(IDETAPE_FLAG_PIPELINE_ACTIVE, &tape->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002453 if (tape->nr_pending_stages >= 3 * max_stages / 4) {
2454 tape->measure_insert_time = 1;
2455 tape->insert_time = jiffies;
2456 tape->insert_size = 0;
2457 tape->insert_speed = 0;
Borislav Petkov8d06bfa2008-02-06 02:57:53 +01002458 idetape_plug_pipeline(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002459 }
2460 }
2461 return 0;
2462}
2463
2464/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +01002465 * Called from idetape_chrdev_read() to service a character device read request
2466 * and add read-ahead requests to our pipeline.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002467 */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002468static int idetape_add_chrdev_read_request(ide_drive_t *drive, int blocks)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002469{
2470 idetape_tape_t *tape = drive->driver_data;
2471 unsigned long flags;
2472 struct request *rq_ptr;
2473 int bytes_read;
2474
Borislav Petkov8004a8c2008-02-06 02:57:51 +01002475 debug_log(DBG_PROCS, "Enter %s, %d blocks\n", __func__, blocks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002476
Borislav Petkov3c98bf32008-02-06 02:57:53 +01002477 /* If we are at a filemark, return a read length of 0 */
Borislav Petkov346331f2008-04-18 00:46:26 +02002478 if (test_bit(IDETAPE_FLAG_FILEMARK, &tape->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002479 return 0;
2480
Borislav Petkov3c98bf32008-02-06 02:57:53 +01002481 /* Wait for the next block to reach the head of the pipeline. */
Borislav Petkov8d06bfa2008-02-06 02:57:53 +01002482 idetape_init_read(drive, tape->max_stages);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002483 if (tape->first_stage == NULL) {
Borislav Petkov346331f2008-04-18 00:46:26 +02002484 if (test_bit(IDETAPE_FLAG_PIPELINE_ERR, &tape->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002485 return 0;
Borislav Petkov54bb2072008-02-06 02:57:52 +01002486 return idetape_queue_rw_tail(drive, REQ_IDETAPE_READ, blocks,
2487 tape->merge_stage->bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002488 }
2489 idetape_wait_first_stage(drive);
2490 rq_ptr = &tape->first_stage->rq;
Borislav Petkov54bb2072008-02-06 02:57:52 +01002491 bytes_read = tape->blk_size * (rq_ptr->nr_sectors -
2492 rq_ptr->current_nr_sectors);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002493 rq_ptr->nr_sectors = 0;
2494 rq_ptr->current_nr_sectors = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002495
2496 if (rq_ptr->errors == IDETAPE_ERROR_EOD)
2497 return 0;
2498 else {
2499 idetape_switch_buffers(tape, tape->first_stage);
2500 if (rq_ptr->errors == IDETAPE_ERROR_FILEMARK)
Borislav Petkov346331f2008-04-18 00:46:26 +02002501 set_bit(IDETAPE_FLAG_FILEMARK, &tape->flags);
Borislav Petkov54bb2072008-02-06 02:57:52 +01002502 spin_lock_irqsave(&tape->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002503 idetape_remove_stage_head(drive);
Borislav Petkov54bb2072008-02-06 02:57:52 +01002504 spin_unlock_irqrestore(&tape->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002505 tape->pipeline_head++;
Borislav Petkov37016ba2008-02-06 02:57:52 +01002506 idetape_calculate_speeds(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002507 }
Borislav Petkov54bb2072008-02-06 02:57:52 +01002508 if (bytes_read > blocks * tape->blk_size) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002509 printk(KERN_ERR "ide-tape: bug: trying to return more bytes"
2510 " than requested\n");
Borislav Petkov54bb2072008-02-06 02:57:52 +01002511 bytes_read = blocks * tape->blk_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002512 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002513 return (bytes_read);
2514}
2515
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002516static void idetape_pad_zeros(ide_drive_t *drive, int bcount)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002517{
2518 idetape_tape_t *tape = drive->driver_data;
2519 struct idetape_bh *bh;
2520 int blocks;
Borislav Petkov3c98bf32008-02-06 02:57:53 +01002521
Linus Torvalds1da177e2005-04-16 15:20:36 -07002522 while (bcount) {
2523 unsigned int count;
2524
2525 bh = tape->merge_stage->bh;
2526 count = min(tape->stage_size, bcount);
2527 bcount -= count;
Borislav Petkov54bb2072008-02-06 02:57:52 +01002528 blocks = count / tape->blk_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002529 while (count) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002530 atomic_set(&bh->b_count,
2531 min(count, (unsigned int)bh->b_size));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002532 memset(bh->b_data, 0, atomic_read(&bh->b_count));
2533 count -= atomic_read(&bh->b_count);
2534 bh = bh->b_reqnext;
2535 }
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002536 idetape_queue_rw_tail(drive, REQ_IDETAPE_WRITE, blocks,
2537 tape->merge_stage->bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002538 }
2539}
2540
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002541static int idetape_pipeline_size(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002542{
2543 idetape_tape_t *tape = drive->driver_data;
2544 idetape_stage_t *stage;
2545 struct request *rq;
2546 int size = 0;
2547
2548 idetape_wait_for_pipeline(drive);
2549 stage = tape->first_stage;
2550 while (stage != NULL) {
2551 rq = &stage->rq;
Borislav Petkov54bb2072008-02-06 02:57:52 +01002552 size += tape->blk_size * (rq->nr_sectors -
2553 rq->current_nr_sectors);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002554 if (rq->errors == IDETAPE_ERROR_FILEMARK)
Borislav Petkov54bb2072008-02-06 02:57:52 +01002555 size += tape->blk_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002556 stage = stage->next;
2557 }
2558 size += tape->merge_stage_size;
2559 return size;
2560}
2561
2562/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +01002563 * Rewinds the tape to the Beginning Of the current Partition (BOP). We
2564 * currently support only one partition.
2565 */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002566static int idetape_rewind_tape(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002567{
2568 int retval;
Borislav Petkovd236d742008-04-18 00:46:27 +02002569 struct ide_atapi_pc pc;
Borislav Petkov8004a8c2008-02-06 02:57:51 +01002570 idetape_tape_t *tape;
2571 tape = drive->driver_data;
2572
2573 debug_log(DBG_SENSE, "Enter %s\n", __func__);
2574
Linus Torvalds1da177e2005-04-16 15:20:36 -07002575 idetape_create_rewind_cmd(drive, &pc);
2576 retval = idetape_queue_pc_tail(drive, &pc);
2577 if (retval)
2578 return retval;
2579
2580 idetape_create_read_position_cmd(&pc);
2581 retval = idetape_queue_pc_tail(drive, &pc);
2582 if (retval)
2583 return retval;
2584 return 0;
2585}
2586
Borislav Petkov3c98bf32008-02-06 02:57:53 +01002587/* mtio.h compatible commands should be issued to the chrdev interface. */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002588static int idetape_blkdev_ioctl(ide_drive_t *drive, unsigned int cmd,
2589 unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002590{
2591 idetape_tape_t *tape = drive->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002592 void __user *argp = (void __user *)arg;
2593
Borislav Petkovd59823f2008-02-02 19:56:51 +01002594 struct idetape_config {
2595 int dsc_rw_frequency;
2596 int dsc_media_access_frequency;
2597 int nr_stages;
2598 } config;
2599
Borislav Petkov8004a8c2008-02-06 02:57:51 +01002600 debug_log(DBG_PROCS, "Enter %s\n", __func__);
2601
Linus Torvalds1da177e2005-04-16 15:20:36 -07002602 switch (cmd) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002603 case 0x0340:
2604 if (copy_from_user(&config, argp, sizeof(config)))
2605 return -EFAULT;
2606 tape->best_dsc_rw_freq = config.dsc_rw_frequency;
2607 tape->max_stages = config.nr_stages;
2608 break;
2609 case 0x0350:
2610 config.dsc_rw_frequency = (int) tape->best_dsc_rw_freq;
2611 config.nr_stages = tape->max_stages;
2612 if (copy_to_user(argp, &config, sizeof(config)))
2613 return -EFAULT;
2614 break;
2615 default:
2616 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002617 }
2618 return 0;
2619}
2620
2621/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +01002622 * The function below is now a bit more complicated than just passing the
2623 * command to the tape since we may have crossed some filemarks during our
2624 * pipelined read-ahead mode. As a minor side effect, the pipeline enables us to
2625 * support MTFSFM when the filemark is in our internal pipeline even if the tape
2626 * doesn't support spacing over filemarks in the reverse direction.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002627 */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002628static int idetape_space_over_filemarks(ide_drive_t *drive, short mt_op,
2629 int mt_count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002630{
2631 idetape_tape_t *tape = drive->driver_data;
Borislav Petkovd236d742008-04-18 00:46:27 +02002632 struct ide_atapi_pc pc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002633 unsigned long flags;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002634 int retval, count = 0;
Borislav Petkovb6422012008-02-02 19:56:49 +01002635 int sprev = !!(tape->caps[4] & 0x20);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002636
2637 if (mt_count == 0)
2638 return 0;
2639 if (MTBSF == mt_op || MTBSFM == mt_op) {
Borislav Petkovb6422012008-02-02 19:56:49 +01002640 if (!sprev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002641 return -EIO;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002642 mt_count = -mt_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002643 }
2644
Borislav Petkov54abf372008-02-06 02:57:52 +01002645 if (tape->chrdev_dir == IDETAPE_DIR_READ) {
Borislav Petkov3c98bf32008-02-06 02:57:53 +01002646 /* its a read-ahead buffer, scan it for crossed filemarks. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002647 tape->merge_stage_size = 0;
Borislav Petkov346331f2008-04-18 00:46:26 +02002648 if (test_and_clear_bit(IDETAPE_FLAG_FILEMARK, &tape->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002649 ++count;
2650 while (tape->first_stage != NULL) {
2651 if (count == mt_count) {
2652 if (mt_op == MTFSFM)
Borislav Petkov346331f2008-04-18 00:46:26 +02002653 set_bit(IDETAPE_FLAG_FILEMARK,
2654 &tape->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002655 return 0;
2656 }
Borislav Petkov54bb2072008-02-06 02:57:52 +01002657 spin_lock_irqsave(&tape->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002658 if (tape->first_stage == tape->active_stage) {
2659 /*
Borislav Petkov3c98bf32008-02-06 02:57:53 +01002660 * We have reached the active stage in the read
2661 * pipeline. There is no point in allowing the
2662 * drive to continue reading any farther, so we
2663 * stop the pipeline.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002664 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +01002665 * This section should be moved to a separate
2666 * subroutine because similar operations are
2667 * done in __idetape_discard_read_pipeline(),
2668 * for example.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002669 */
2670 tape->next_stage = NULL;
Borislav Petkov54bb2072008-02-06 02:57:52 +01002671 spin_unlock_irqrestore(&tape->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002672 idetape_wait_first_stage(drive);
2673 tape->next_stage = tape->first_stage->next;
2674 } else
Borislav Petkov54bb2072008-02-06 02:57:52 +01002675 spin_unlock_irqrestore(&tape->lock, flags);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002676 if (tape->first_stage->rq.errors ==
2677 IDETAPE_ERROR_FILEMARK)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002678 ++count;
2679 idetape_remove_stage_head(drive);
2680 }
2681 idetape_discard_read_pipeline(drive, 0);
2682 }
2683
2684 /*
Borislav Petkov3c98bf32008-02-06 02:57:53 +01002685 * The filemark was not found in our internal pipeline; now we can issue
2686 * the space command.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002687 */
2688 switch (mt_op) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002689 case MTFSF:
2690 case MTBSF:
2691 idetape_create_space_cmd(&pc, mt_count - count,
2692 IDETAPE_SPACE_OVER_FILEMARK);
2693 return idetape_queue_pc_tail(drive, &pc);
2694 case MTFSFM:
2695 case MTBSFM:
2696 if (!sprev)
2697 return -EIO;
2698 retval = idetape_space_over_filemarks(drive, MTFSF,
2699 mt_count - count);
2700 if (retval)
2701 return retval;
2702 count = (MTBSFM == mt_op ? 1 : -1);
2703 return idetape_space_over_filemarks(drive, MTFSF, count);
2704 default:
2705 printk(KERN_ERR "ide-tape: MTIO operation %d not supported\n",
2706 mt_op);
2707 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002708 }
2709}
2710
Linus Torvalds1da177e2005-04-16 15:20:36 -07002711/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +01002712 * Our character device read / write functions.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002713 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +01002714 * The tape is optimized to maximize throughput when it is transferring an
2715 * integral number of the "continuous transfer limit", which is a parameter of
2716 * the specific tape (26kB on my particular tape, 32kB for Onstream).
Linus Torvalds1da177e2005-04-16 15:20:36 -07002717 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +01002718 * As of version 1.3 of the driver, the character device provides an abstract
2719 * continuous view of the media - any mix of block sizes (even 1 byte) on the
2720 * same backup/restore procedure is supported. The driver will internally
2721 * convert the requests to the recommended transfer unit, so that an unmatch
2722 * between the user's block size to the recommended size will only result in a
2723 * (slightly) increased driver overhead, but will no longer hit performance.
2724 * This is not applicable to Onstream.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002725 */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002726static ssize_t idetape_chrdev_read(struct file *file, char __user *buf,
2727 size_t count, loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002728{
2729 struct ide_tape_obj *tape = ide_tape_f(file);
2730 ide_drive_t *drive = tape->drive;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002731 ssize_t bytes_read, temp, actually_read = 0, rc;
Daniel Walkerdcd96372006-06-25 05:47:37 -07002732 ssize_t ret = 0;
Borislav Petkovb6422012008-02-02 19:56:49 +01002733 u16 ctl = *(u16 *)&tape->caps[12];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002734
Borislav Petkov8004a8c2008-02-06 02:57:51 +01002735 debug_log(DBG_CHRDEV, "Enter %s, count %Zd\n", __func__, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002736
Borislav Petkov54abf372008-02-06 02:57:52 +01002737 if (tape->chrdev_dir != IDETAPE_DIR_READ) {
Borislav Petkov346331f2008-04-18 00:46:26 +02002738 if (test_bit(IDETAPE_FLAG_DETECT_BS, &tape->flags))
Borislav Petkov54bb2072008-02-06 02:57:52 +01002739 if (count > tape->blk_size &&
2740 (count % tape->blk_size) == 0)
2741 tape->user_bs_factor = count / tape->blk_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002742 }
Borislav Petkov8d06bfa2008-02-06 02:57:53 +01002743 rc = idetape_init_read(drive, tape->max_stages);
2744 if (rc < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002745 return rc;
2746 if (count == 0)
2747 return (0);
2748 if (tape->merge_stage_size) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002749 actually_read = min((unsigned int)(tape->merge_stage_size),
2750 (unsigned int)count);
2751 if (idetape_copy_stage_to_user(tape, buf, tape->merge_stage,
2752 actually_read))
Daniel Walkerdcd96372006-06-25 05:47:37 -07002753 ret = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002754 buf += actually_read;
2755 tape->merge_stage_size -= actually_read;
2756 count -= actually_read;
2757 }
2758 while (count >= tape->stage_size) {
Borislav Petkovb6422012008-02-02 19:56:49 +01002759 bytes_read = idetape_add_chrdev_read_request(drive, ctl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002760 if (bytes_read <= 0)
2761 goto finish;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002762 if (idetape_copy_stage_to_user(tape, buf, tape->merge_stage,
2763 bytes_read))
Daniel Walkerdcd96372006-06-25 05:47:37 -07002764 ret = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002765 buf += bytes_read;
2766 count -= bytes_read;
2767 actually_read += bytes_read;
2768 }
2769 if (count) {
Borislav Petkovb6422012008-02-02 19:56:49 +01002770 bytes_read = idetape_add_chrdev_read_request(drive, ctl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002771 if (bytes_read <= 0)
2772 goto finish;
2773 temp = min((unsigned long)count, (unsigned long)bytes_read);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002774 if (idetape_copy_stage_to_user(tape, buf, tape->merge_stage,
2775 temp))
Daniel Walkerdcd96372006-06-25 05:47:37 -07002776 ret = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002777 actually_read += temp;
2778 tape->merge_stage_size = bytes_read-temp;
2779 }
2780finish:
Borislav Petkov346331f2008-04-18 00:46:26 +02002781 if (!actually_read && test_bit(IDETAPE_FLAG_FILEMARK, &tape->flags)) {
Borislav Petkov8004a8c2008-02-06 02:57:51 +01002782 debug_log(DBG_SENSE, "%s: spacing over filemark\n", tape->name);
2783
Linus Torvalds1da177e2005-04-16 15:20:36 -07002784 idetape_space_over_filemarks(drive, MTFSF, 1);
2785 return 0;
2786 }
Daniel Walkerdcd96372006-06-25 05:47:37 -07002787
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002788 return ret ? ret : actually_read;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002789}
2790
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002791static ssize_t idetape_chrdev_write(struct file *file, const char __user *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002792 size_t count, loff_t *ppos)
2793{
2794 struct ide_tape_obj *tape = ide_tape_f(file);
2795 ide_drive_t *drive = tape->drive;
Daniel Walkerdcd96372006-06-25 05:47:37 -07002796 ssize_t actually_written = 0;
2797 ssize_t ret = 0;
Borislav Petkovb6422012008-02-02 19:56:49 +01002798 u16 ctl = *(u16 *)&tape->caps[12];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002799
2800 /* The drive is write protected. */
2801 if (tape->write_prot)
2802 return -EACCES;
2803
Borislav Petkov8004a8c2008-02-06 02:57:51 +01002804 debug_log(DBG_CHRDEV, "Enter %s, count %Zd\n", __func__, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002805
2806 /* Initialize write operation */
Borislav Petkov54abf372008-02-06 02:57:52 +01002807 if (tape->chrdev_dir != IDETAPE_DIR_WRITE) {
2808 if (tape->chrdev_dir == IDETAPE_DIR_READ)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002809 idetape_discard_read_pipeline(drive, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002810 if (tape->merge_stage || tape->merge_stage_size) {
2811 printk(KERN_ERR "ide-tape: merge_stage_size "
2812 "should be 0 now\n");
2813 tape->merge_stage_size = 0;
2814 }
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002815 tape->merge_stage = __idetape_kmalloc_stage(tape, 0, 0);
2816 if (!tape->merge_stage)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002817 return -ENOMEM;
Borislav Petkov54abf372008-02-06 02:57:52 +01002818 tape->chrdev_dir = IDETAPE_DIR_WRITE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002819 idetape_init_merge_stage(tape);
2820
2821 /*
Borislav Petkov3c98bf32008-02-06 02:57:53 +01002822 * Issue a write 0 command to ensure that DSC handshake is
2823 * switched from completion mode to buffer available mode. No
2824 * point in issuing this if DSC overlap isn't supported, some
2825 * drives (Seagate STT3401A) will return an error.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002826 */
2827 if (drive->dsc_overlap) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002828 ssize_t retval = idetape_queue_rw_tail(drive,
2829 REQ_IDETAPE_WRITE, 0,
2830 tape->merge_stage->bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002831 if (retval < 0) {
2832 __idetape_kfree_stage(tape->merge_stage);
2833 tape->merge_stage = NULL;
Borislav Petkov54abf372008-02-06 02:57:52 +01002834 tape->chrdev_dir = IDETAPE_DIR_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002835 return retval;
2836 }
2837 }
2838 }
2839 if (count == 0)
2840 return (0);
2841 if (tape->restart_speed_control_req)
2842 idetape_restart_speed_control(drive);
2843 if (tape->merge_stage_size) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002844 if (tape->merge_stage_size >= tape->stage_size) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002845 printk(KERN_ERR "ide-tape: bug: merge buf too big\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002846 tape->merge_stage_size = 0;
2847 }
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002848 actually_written = min((unsigned int)
2849 (tape->stage_size - tape->merge_stage_size),
2850 (unsigned int)count);
2851 if (idetape_copy_stage_from_user(tape, tape->merge_stage, buf,
2852 actually_written))
Daniel Walkerdcd96372006-06-25 05:47:37 -07002853 ret = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002854 buf += actually_written;
2855 tape->merge_stage_size += actually_written;
2856 count -= actually_written;
2857
2858 if (tape->merge_stage_size == tape->stage_size) {
Daniel Walkerdcd96372006-06-25 05:47:37 -07002859 ssize_t retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002860 tape->merge_stage_size = 0;
Borislav Petkovb6422012008-02-02 19:56:49 +01002861 retval = idetape_add_chrdev_write_request(drive, ctl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002862 if (retval <= 0)
2863 return (retval);
2864 }
2865 }
2866 while (count >= tape->stage_size) {
Daniel Walkerdcd96372006-06-25 05:47:37 -07002867 ssize_t retval;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002868 if (idetape_copy_stage_from_user(tape, tape->merge_stage, buf,
2869 tape->stage_size))
Daniel Walkerdcd96372006-06-25 05:47:37 -07002870 ret = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002871 buf += tape->stage_size;
2872 count -= tape->stage_size;
Borislav Petkovb6422012008-02-02 19:56:49 +01002873 retval = idetape_add_chrdev_write_request(drive, ctl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002874 actually_written += tape->stage_size;
2875 if (retval <= 0)
2876 return (retval);
2877 }
2878 if (count) {
2879 actually_written += count;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002880 if (idetape_copy_stage_from_user(tape, tape->merge_stage, buf,
2881 count))
Daniel Walkerdcd96372006-06-25 05:47:37 -07002882 ret = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002883 tape->merge_stage_size += count;
2884 }
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002885 return ret ? ret : actually_written;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002886}
2887
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002888static int idetape_write_filemark(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002889{
Borislav Petkovd236d742008-04-18 00:46:27 +02002890 struct ide_atapi_pc pc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002891
2892 /* Write a filemark */
2893 idetape_create_write_filemark_cmd(drive, &pc, 1);
2894 if (idetape_queue_pc_tail(drive, &pc)) {
2895 printk(KERN_ERR "ide-tape: Couldn't write a filemark\n");
2896 return -EIO;
2897 }
2898 return 0;
2899}
2900
2901/*
Borislav Petkovd99c9da2008-02-02 19:56:51 +01002902 * Called from idetape_chrdev_ioctl when the general mtio MTIOCTOP ioctl is
2903 * requested.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002904 *
Borislav Petkovd99c9da2008-02-02 19:56:51 +01002905 * Note: MTBSF and MTBSFM are not supported when the tape doesn't support
2906 * spacing over filemarks in the reverse direction. In this case, MTFSFM is also
2907 * usually not supported (it is supported in the rare case in which we crossed
2908 * the filemark during our read-ahead pipelined operation mode).
Linus Torvalds1da177e2005-04-16 15:20:36 -07002909 *
Borislav Petkovd99c9da2008-02-02 19:56:51 +01002910 * The following commands are currently not supported:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002911 *
Borislav Petkovd99c9da2008-02-02 19:56:51 +01002912 * MTFSS, MTBSS, MTWSM, MTSETDENSITY, MTSETDRVBUFFER, MT_ST_BOOLEANS,
2913 * MT_ST_WRITE_THRESHOLD.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002914 */
Borislav Petkovd99c9da2008-02-02 19:56:51 +01002915static int idetape_mtioctop(ide_drive_t *drive, short mt_op, int mt_count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002916{
2917 idetape_tape_t *tape = drive->driver_data;
Borislav Petkovd236d742008-04-18 00:46:27 +02002918 struct ide_atapi_pc pc;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002919 int i, retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002920
Borislav Petkov8004a8c2008-02-06 02:57:51 +01002921 debug_log(DBG_ERR, "Handling MTIOCTOP ioctl: mt_op=%d, mt_count=%d\n",
2922 mt_op, mt_count);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002923
Borislav Petkov3c98bf32008-02-06 02:57:53 +01002924 /* Commands which need our pipelined read-ahead stages. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002925 switch (mt_op) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002926 case MTFSF:
2927 case MTFSFM:
2928 case MTBSF:
2929 case MTBSFM:
2930 if (!mt_count)
2931 return 0;
2932 return idetape_space_over_filemarks(drive, mt_op, mt_count);
2933 default:
2934 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002935 }
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002936
Linus Torvalds1da177e2005-04-16 15:20:36 -07002937 switch (mt_op) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002938 case MTWEOF:
2939 if (tape->write_prot)
2940 return -EACCES;
2941 idetape_discard_read_pipeline(drive, 1);
2942 for (i = 0; i < mt_count; i++) {
2943 retval = idetape_write_filemark(drive);
2944 if (retval)
2945 return retval;
2946 }
2947 return 0;
2948 case MTREW:
2949 idetape_discard_read_pipeline(drive, 0);
2950 if (idetape_rewind_tape(drive))
2951 return -EIO;
2952 return 0;
2953 case MTLOAD:
2954 idetape_discard_read_pipeline(drive, 0);
2955 idetape_create_load_unload_cmd(drive, &pc,
2956 IDETAPE_LU_LOAD_MASK);
2957 return idetape_queue_pc_tail(drive, &pc);
2958 case MTUNLOAD:
2959 case MTOFFL:
2960 /*
2961 * If door is locked, attempt to unlock before
2962 * attempting to eject.
2963 */
2964 if (tape->door_locked) {
2965 if (idetape_create_prevent_cmd(drive, &pc, 0))
2966 if (!idetape_queue_pc_tail(drive, &pc))
2967 tape->door_locked = DOOR_UNLOCKED;
2968 }
2969 idetape_discard_read_pipeline(drive, 0);
2970 idetape_create_load_unload_cmd(drive, &pc,
2971 !IDETAPE_LU_LOAD_MASK);
2972 retval = idetape_queue_pc_tail(drive, &pc);
2973 if (!retval)
Borislav Petkov346331f2008-04-18 00:46:26 +02002974 clear_bit(IDETAPE_FLAG_MEDIUM_PRESENT, &tape->flags);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002975 return retval;
2976 case MTNOP:
2977 idetape_discard_read_pipeline(drive, 0);
2978 return idetape_flush_tape_buffers(drive);
2979 case MTRETEN:
2980 idetape_discard_read_pipeline(drive, 0);
2981 idetape_create_load_unload_cmd(drive, &pc,
2982 IDETAPE_LU_RETENSION_MASK | IDETAPE_LU_LOAD_MASK);
2983 return idetape_queue_pc_tail(drive, &pc);
2984 case MTEOM:
2985 idetape_create_space_cmd(&pc, 0, IDETAPE_SPACE_TO_EOD);
2986 return idetape_queue_pc_tail(drive, &pc);
2987 case MTERASE:
2988 (void)idetape_rewind_tape(drive);
2989 idetape_create_erase_cmd(&pc);
2990 return idetape_queue_pc_tail(drive, &pc);
2991 case MTSETBLK:
2992 if (mt_count) {
2993 if (mt_count < tape->blk_size ||
2994 mt_count % tape->blk_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002995 return -EIO;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002996 tape->user_bs_factor = mt_count / tape->blk_size;
Borislav Petkov346331f2008-04-18 00:46:26 +02002997 clear_bit(IDETAPE_FLAG_DETECT_BS, &tape->flags);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002998 } else
Borislav Petkov346331f2008-04-18 00:46:26 +02002999 set_bit(IDETAPE_FLAG_DETECT_BS, &tape->flags);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01003000 return 0;
3001 case MTSEEK:
3002 idetape_discard_read_pipeline(drive, 0);
3003 return idetape_position_tape(drive,
3004 mt_count * tape->user_bs_factor, tape->partition, 0);
3005 case MTSETPART:
3006 idetape_discard_read_pipeline(drive, 0);
3007 return idetape_position_tape(drive, 0, mt_count, 0);
3008 case MTFSR:
3009 case MTBSR:
3010 case MTLOCK:
3011 if (!idetape_create_prevent_cmd(drive, &pc, 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003012 return 0;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01003013 retval = idetape_queue_pc_tail(drive, &pc);
3014 if (retval)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003015 return retval;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01003016 tape->door_locked = DOOR_EXPLICITLY_LOCKED;
3017 return 0;
3018 case MTUNLOCK:
3019 if (!idetape_create_prevent_cmd(drive, &pc, 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003020 return 0;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01003021 retval = idetape_queue_pc_tail(drive, &pc);
3022 if (retval)
3023 return retval;
3024 tape->door_locked = DOOR_UNLOCKED;
3025 return 0;
3026 default:
3027 printk(KERN_ERR "ide-tape: MTIO operation %d not supported\n",
3028 mt_op);
3029 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003030 }
3031}
3032
3033/*
Borislav Petkovd99c9da2008-02-02 19:56:51 +01003034 * Our character device ioctls. General mtio.h magnetic io commands are
3035 * supported here, and not in the corresponding block interface. Our own
3036 * ide-tape ioctls are supported on both interfaces.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003037 */
Borislav Petkovd99c9da2008-02-02 19:56:51 +01003038static int idetape_chrdev_ioctl(struct inode *inode, struct file *file,
3039 unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003040{
3041 struct ide_tape_obj *tape = ide_tape_f(file);
3042 ide_drive_t *drive = tape->drive;
3043 struct mtop mtop;
3044 struct mtget mtget;
3045 struct mtpos mtpos;
Borislav Petkov54bb2072008-02-06 02:57:52 +01003046 int block_offset = 0, position = tape->first_frame;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003047 void __user *argp = (void __user *)arg;
3048
Borislav Petkov8004a8c2008-02-06 02:57:51 +01003049 debug_log(DBG_CHRDEV, "Enter %s, cmd=%u\n", __func__, cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003050
3051 tape->restart_speed_control_req = 1;
Borislav Petkov54abf372008-02-06 02:57:52 +01003052 if (tape->chrdev_dir == IDETAPE_DIR_WRITE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003053 idetape_empty_write_pipeline(drive);
3054 idetape_flush_tape_buffers(drive);
3055 }
3056 if (cmd == MTIOCGET || cmd == MTIOCPOS) {
Borislav Petkov54bb2072008-02-06 02:57:52 +01003057 block_offset = idetape_pipeline_size(drive) /
3058 (tape->blk_size * tape->user_bs_factor);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01003059 position = idetape_read_position(drive);
3060 if (position < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003061 return -EIO;
3062 }
3063 switch (cmd) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01003064 case MTIOCTOP:
3065 if (copy_from_user(&mtop, argp, sizeof(struct mtop)))
3066 return -EFAULT;
3067 return idetape_mtioctop(drive, mtop.mt_op, mtop.mt_count);
3068 case MTIOCGET:
3069 memset(&mtget, 0, sizeof(struct mtget));
3070 mtget.mt_type = MT_ISSCSI2;
3071 mtget.mt_blkno = position / tape->user_bs_factor - block_offset;
3072 mtget.mt_dsreg =
3073 ((tape->blk_size * tape->user_bs_factor)
3074 << MT_ST_BLKSIZE_SHIFT) & MT_ST_BLKSIZE_MASK;
Borislav Petkov54bb2072008-02-06 02:57:52 +01003075
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01003076 if (tape->drv_write_prot)
3077 mtget.mt_gstat |= GMT_WR_PROT(0xffffffff);
3078
3079 if (copy_to_user(argp, &mtget, sizeof(struct mtget)))
3080 return -EFAULT;
3081 return 0;
3082 case MTIOCPOS:
3083 mtpos.mt_blkno = position / tape->user_bs_factor - block_offset;
3084 if (copy_to_user(argp, &mtpos, sizeof(struct mtpos)))
3085 return -EFAULT;
3086 return 0;
3087 default:
3088 if (tape->chrdev_dir == IDETAPE_DIR_READ)
3089 idetape_discard_read_pipeline(drive, 1);
3090 return idetape_blkdev_ioctl(drive, cmd, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003091 }
3092}
3093
Borislav Petkov3cffb9c2008-02-02 19:56:50 +01003094/*
3095 * Do a mode sense page 0 with block descriptor and if it succeeds set the tape
3096 * block size with the reported value.
3097 */
3098static void ide_tape_get_bsize_from_bdesc(ide_drive_t *drive)
3099{
3100 idetape_tape_t *tape = drive->driver_data;
Borislav Petkovd236d742008-04-18 00:46:27 +02003101 struct ide_atapi_pc pc;
Borislav Petkov3cffb9c2008-02-02 19:56:50 +01003102
3103 idetape_create_mode_sense_cmd(&pc, IDETAPE_BLOCK_DESCRIPTOR);
3104 if (idetape_queue_pc_tail(drive, &pc)) {
3105 printk(KERN_ERR "ide-tape: Can't get block descriptor\n");
Borislav Petkov54bb2072008-02-06 02:57:52 +01003106 if (tape->blk_size == 0) {
Borislav Petkov3cffb9c2008-02-02 19:56:50 +01003107 printk(KERN_WARNING "ide-tape: Cannot deal with zero "
3108 "block size, assuming 32k\n");
Borislav Petkov54bb2072008-02-06 02:57:52 +01003109 tape->blk_size = 32768;
Borislav Petkov3cffb9c2008-02-02 19:56:50 +01003110 }
3111 return;
3112 }
Borislav Petkovd236d742008-04-18 00:46:27 +02003113 tape->blk_size = (pc.buf[4 + 5] << 16) +
3114 (pc.buf[4 + 6] << 8) +
3115 pc.buf[4 + 7];
3116 tape->drv_write_prot = (pc.buf[2] & 0x80) >> 7;
Borislav Petkov3cffb9c2008-02-02 19:56:50 +01003117}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003118
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01003119static int idetape_chrdev_open(struct inode *inode, struct file *filp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003120{
3121 unsigned int minor = iminor(inode), i = minor & ~0xc0;
3122 ide_drive_t *drive;
3123 idetape_tape_t *tape;
Borislav Petkovd236d742008-04-18 00:46:27 +02003124 struct ide_atapi_pc pc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003125 int retval;
3126
Borislav Petkov8004a8c2008-02-06 02:57:51 +01003127 if (i >= MAX_HWIFS * MAX_DRIVES)
3128 return -ENXIO;
3129
3130 tape = ide_tape_chrdev_get(i);
3131 if (!tape)
3132 return -ENXIO;
3133
3134 debug_log(DBG_CHRDEV, "Enter %s\n", __func__);
3135
Linus Torvalds1da177e2005-04-16 15:20:36 -07003136 /*
3137 * We really want to do nonseekable_open(inode, filp); here, but some
3138 * versions of tar incorrectly call lseek on tapes and bail out if that
3139 * fails. So we disallow pread() and pwrite(), but permit lseeks.
3140 */
3141 filp->f_mode &= ~(FMODE_PREAD | FMODE_PWRITE);
3142
Linus Torvalds1da177e2005-04-16 15:20:36 -07003143 drive = tape->drive;
3144
3145 filp->private_data = tape;
3146
Borislav Petkov346331f2008-04-18 00:46:26 +02003147 if (test_and_set_bit(IDETAPE_FLAG_BUSY, &tape->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003148 retval = -EBUSY;
3149 goto out_put_tape;
3150 }
3151
3152 retval = idetape_wait_ready(drive, 60 * HZ);
3153 if (retval) {
Borislav Petkov346331f2008-04-18 00:46:26 +02003154 clear_bit(IDETAPE_FLAG_BUSY, &tape->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003155 printk(KERN_ERR "ide-tape: %s: drive not ready\n", tape->name);
3156 goto out_put_tape;
3157 }
3158
3159 idetape_read_position(drive);
Borislav Petkov346331f2008-04-18 00:46:26 +02003160 if (!test_bit(IDETAPE_FLAG_ADDRESS_VALID, &tape->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003161 (void)idetape_rewind_tape(drive);
3162
Borislav Petkov54abf372008-02-06 02:57:52 +01003163 if (tape->chrdev_dir != IDETAPE_DIR_READ)
Borislav Petkov346331f2008-04-18 00:46:26 +02003164 clear_bit(IDETAPE_FLAG_PIPELINE_ERR, &tape->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003165
3166 /* Read block size and write protect status from drive. */
Borislav Petkov3cffb9c2008-02-02 19:56:50 +01003167 ide_tape_get_bsize_from_bdesc(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003168
3169 /* Set write protect flag if device is opened as read-only. */
3170 if ((filp->f_flags & O_ACCMODE) == O_RDONLY)
3171 tape->write_prot = 1;
3172 else
3173 tape->write_prot = tape->drv_write_prot;
3174
3175 /* Make sure drive isn't write protected if user wants to write. */
3176 if (tape->write_prot) {
3177 if ((filp->f_flags & O_ACCMODE) == O_WRONLY ||
3178 (filp->f_flags & O_ACCMODE) == O_RDWR) {
Borislav Petkov346331f2008-04-18 00:46:26 +02003179 clear_bit(IDETAPE_FLAG_BUSY, &tape->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003180 retval = -EROFS;
3181 goto out_put_tape;
3182 }
3183 }
3184
Borislav Petkov3c98bf32008-02-06 02:57:53 +01003185 /* Lock the tape drive door so user can't eject. */
Borislav Petkov54abf372008-02-06 02:57:52 +01003186 if (tape->chrdev_dir == IDETAPE_DIR_NONE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003187 if (idetape_create_prevent_cmd(drive, &pc, 1)) {
3188 if (!idetape_queue_pc_tail(drive, &pc)) {
3189 if (tape->door_locked != DOOR_EXPLICITLY_LOCKED)
3190 tape->door_locked = DOOR_LOCKED;
3191 }
3192 }
3193 }
3194 idetape_restart_speed_control(drive);
3195 tape->restart_speed_control_req = 0;
3196 return 0;
3197
3198out_put_tape:
3199 ide_tape_put(tape);
3200 return retval;
3201}
3202
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01003203static void idetape_write_release(ide_drive_t *drive, unsigned int minor)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003204{
3205 idetape_tape_t *tape = drive->driver_data;
3206
3207 idetape_empty_write_pipeline(drive);
3208 tape->merge_stage = __idetape_kmalloc_stage(tape, 1, 0);
3209 if (tape->merge_stage != NULL) {
Borislav Petkov54bb2072008-02-06 02:57:52 +01003210 idetape_pad_zeros(drive, tape->blk_size *
3211 (tape->user_bs_factor - 1));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003212 __idetape_kfree_stage(tape->merge_stage);
3213 tape->merge_stage = NULL;
3214 }
3215 idetape_write_filemark(drive);
3216 idetape_flush_tape_buffers(drive);
3217 idetape_flush_tape_buffers(drive);
3218}
3219
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01003220static int idetape_chrdev_release(struct inode *inode, struct file *filp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003221{
3222 struct ide_tape_obj *tape = ide_tape_f(filp);
3223 ide_drive_t *drive = tape->drive;
Borislav Petkovd236d742008-04-18 00:46:27 +02003224 struct ide_atapi_pc pc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003225 unsigned int minor = iminor(inode);
3226
3227 lock_kernel();
3228 tape = drive->driver_data;
Borislav Petkov8004a8c2008-02-06 02:57:51 +01003229
3230 debug_log(DBG_CHRDEV, "Enter %s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003231
Borislav Petkov54abf372008-02-06 02:57:52 +01003232 if (tape->chrdev_dir == IDETAPE_DIR_WRITE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003233 idetape_write_release(drive, minor);
Borislav Petkov54abf372008-02-06 02:57:52 +01003234 if (tape->chrdev_dir == IDETAPE_DIR_READ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003235 if (minor < 128)
3236 idetape_discard_read_pipeline(drive, 1);
3237 else
3238 idetape_wait_for_pipeline(drive);
3239 }
Borislav Petkovf64eee72008-04-27 15:38:25 +02003240
Borislav Petkov346331f2008-04-18 00:46:26 +02003241 if (minor < 128 && test_bit(IDETAPE_FLAG_MEDIUM_PRESENT, &tape->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003242 (void) idetape_rewind_tape(drive);
Borislav Petkov54abf372008-02-06 02:57:52 +01003243 if (tape->chrdev_dir == IDETAPE_DIR_NONE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003244 if (tape->door_locked == DOOR_LOCKED) {
3245 if (idetape_create_prevent_cmd(drive, &pc, 0)) {
3246 if (!idetape_queue_pc_tail(drive, &pc))
3247 tape->door_locked = DOOR_UNLOCKED;
3248 }
3249 }
3250 }
Borislav Petkov346331f2008-04-18 00:46:26 +02003251 clear_bit(IDETAPE_FLAG_BUSY, &tape->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003252 ide_tape_put(tape);
3253 unlock_kernel();
3254 return 0;
3255}
3256
3257/*
Borislav Petkov71071b82008-02-06 02:57:53 +01003258 * check the contents of the ATAPI IDENTIFY command results. We return:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003259 *
Borislav Petkov71071b82008-02-06 02:57:53 +01003260 * 1 - If the tape can be supported by us, based on the information we have so
3261 * far.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003262 *
Borislav Petkov71071b82008-02-06 02:57:53 +01003263 * 0 - If this tape driver is not currently supported by us.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003264 */
Borislav Petkov71071b82008-02-06 02:57:53 +01003265static int idetape_identify_device(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003266{
Borislav Petkov71071b82008-02-06 02:57:53 +01003267 u8 gcw[2], protocol, device_type, removable, packet_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003268
3269 if (drive->id_read == 0)
3270 return 1;
3271
Borislav Petkov71071b82008-02-06 02:57:53 +01003272 *((unsigned short *) &gcw) = drive->id->config;
3273
3274 protocol = (gcw[1] & 0xC0) >> 6;
3275 device_type = gcw[1] & 0x1F;
3276 removable = !!(gcw[0] & 0x80);
3277 packet_size = gcw[0] & 0x3;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003278
Linus Torvalds1da177e2005-04-16 15:20:36 -07003279 /* Check that we can support this device */
Borislav Petkov71071b82008-02-06 02:57:53 +01003280 if (protocol != 2)
Bartlomiej Zolnierkiewicz16422de32008-02-02 19:56:48 +01003281 printk(KERN_ERR "ide-tape: Protocol (0x%02x) is not ATAPI\n",
Borislav Petkov71071b82008-02-06 02:57:53 +01003282 protocol);
3283 else if (device_type != 1)
Bartlomiej Zolnierkiewicz16422de32008-02-02 19:56:48 +01003284 printk(KERN_ERR "ide-tape: Device type (0x%02x) is not set "
Borislav Petkov71071b82008-02-06 02:57:53 +01003285 "to tape\n", device_type);
3286 else if (!removable)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003287 printk(KERN_ERR "ide-tape: The removable flag is not set\n");
Borislav Petkov71071b82008-02-06 02:57:53 +01003288 else if (packet_size != 0) {
Borislav Petkov24d57f82008-02-06 02:57:54 +01003289 printk(KERN_ERR "ide-tape: Packet size (0x%02x) is not 12"
3290 " bytes\n", packet_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003291 } else
3292 return 1;
3293 return 0;
3294}
3295
Borislav Petkov6d29c8f2008-02-02 19:56:49 +01003296static void idetape_get_inquiry_results(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003297{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003298 idetape_tape_t *tape = drive->driver_data;
Borislav Petkovd236d742008-04-18 00:46:27 +02003299 struct ide_atapi_pc pc;
Borislav Petkov41f81d542008-02-06 02:57:52 +01003300 char fw_rev[6], vendor_id[10], product_id[18];
Borislav Petkov6d29c8f2008-02-02 19:56:49 +01003301
Linus Torvalds1da177e2005-04-16 15:20:36 -07003302 idetape_create_inquiry_cmd(&pc);
3303 if (idetape_queue_pc_tail(drive, &pc)) {
Borislav Petkov6d29c8f2008-02-02 19:56:49 +01003304 printk(KERN_ERR "ide-tape: %s: can't get INQUIRY results\n",
3305 tape->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003306 return;
3307 }
Borislav Petkovd236d742008-04-18 00:46:27 +02003308 memcpy(vendor_id, &pc.buf[8], 8);
3309 memcpy(product_id, &pc.buf[16], 16);
3310 memcpy(fw_rev, &pc.buf[32], 4);
Borislav Petkov6d29c8f2008-02-02 19:56:49 +01003311
Borislav Petkov41f81d542008-02-06 02:57:52 +01003312 ide_fixstring(vendor_id, 10, 0);
3313 ide_fixstring(product_id, 18, 0);
3314 ide_fixstring(fw_rev, 6, 0);
3315
Borislav Petkov6d29c8f2008-02-02 19:56:49 +01003316 printk(KERN_INFO "ide-tape: %s <-> %s: %s %s rev %s\n",
Borislav Petkov41f81d542008-02-06 02:57:52 +01003317 drive->name, tape->name, vendor_id, product_id, fw_rev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003318}
3319
3320/*
Borislav Petkovb6422012008-02-02 19:56:49 +01003321 * Ask the tape about its various parameters. In particular, we will adjust our
3322 * data transfer buffer size to the recommended value as returned by the tape.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003323 */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01003324static void idetape_get_mode_sense_results(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003325{
3326 idetape_tape_t *tape = drive->driver_data;
Borislav Petkovd236d742008-04-18 00:46:27 +02003327 struct ide_atapi_pc pc;
Borislav Petkovb6422012008-02-02 19:56:49 +01003328 u8 *caps;
3329 u8 speed, max_speed;
Borislav Petkov47314fa2008-02-02 19:56:48 +01003330
Linus Torvalds1da177e2005-04-16 15:20:36 -07003331 idetape_create_mode_sense_cmd(&pc, IDETAPE_CAPABILITIES_PAGE);
3332 if (idetape_queue_pc_tail(drive, &pc)) {
Borislav Petkovb6422012008-02-02 19:56:49 +01003333 printk(KERN_ERR "ide-tape: Can't get tape parameters - assuming"
3334 " some default values\n");
Borislav Petkov54bb2072008-02-06 02:57:52 +01003335 tape->blk_size = 512;
Borislav Petkovb6422012008-02-02 19:56:49 +01003336 put_unaligned(52, (u16 *)&tape->caps[12]);
3337 put_unaligned(540, (u16 *)&tape->caps[14]);
3338 put_unaligned(6*52, (u16 *)&tape->caps[16]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003339 return;
3340 }
Borislav Petkovd236d742008-04-18 00:46:27 +02003341 caps = pc.buf + 4 + pc.buf[3];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003342
Borislav Petkovb6422012008-02-02 19:56:49 +01003343 /* convert to host order and save for later use */
3344 speed = be16_to_cpu(*(u16 *)&caps[14]);
3345 max_speed = be16_to_cpu(*(u16 *)&caps[8]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003346
Borislav Petkovb6422012008-02-02 19:56:49 +01003347 put_unaligned(max_speed, (u16 *)&caps[8]);
3348 put_unaligned(be16_to_cpu(*(u16 *)&caps[12]), (u16 *)&caps[12]);
3349 put_unaligned(speed, (u16 *)&caps[14]);
3350 put_unaligned(be16_to_cpu(*(u16 *)&caps[16]), (u16 *)&caps[16]);
3351
3352 if (!speed) {
3353 printk(KERN_INFO "ide-tape: %s: invalid tape speed "
3354 "(assuming 650KB/sec)\n", drive->name);
3355 put_unaligned(650, (u16 *)&caps[14]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003356 }
Borislav Petkovb6422012008-02-02 19:56:49 +01003357 if (!max_speed) {
3358 printk(KERN_INFO "ide-tape: %s: invalid max_speed "
3359 "(assuming 650KB/sec)\n", drive->name);
3360 put_unaligned(650, (u16 *)&caps[8]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003361 }
3362
Borislav Petkovb6422012008-02-02 19:56:49 +01003363 memcpy(&tape->caps, caps, 20);
3364 if (caps[7] & 0x02)
Borislav Petkov54bb2072008-02-06 02:57:52 +01003365 tape->blk_size = 512;
Borislav Petkovb6422012008-02-02 19:56:49 +01003366 else if (caps[7] & 0x04)
Borislav Petkov54bb2072008-02-06 02:57:52 +01003367 tape->blk_size = 1024;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003368}
3369
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +02003370#ifdef CONFIG_IDE_PROC_FS
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01003371static void idetape_add_settings(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003372{
3373 idetape_tape_t *tape = drive->driver_data;
3374
Borislav Petkovb6422012008-02-02 19:56:49 +01003375 ide_add_setting(drive, "buffer", SETTING_READ, TYPE_SHORT, 0, 0xffff,
3376 1, 2, (u16 *)&tape->caps[16], NULL);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01003377 ide_add_setting(drive, "pipeline_min", SETTING_RW, TYPE_INT, 1, 0xffff,
3378 tape->stage_size / 1024, 1, &tape->min_pipeline, NULL);
3379 ide_add_setting(drive, "pipeline", SETTING_RW, TYPE_INT, 1, 0xffff,
3380 tape->stage_size / 1024, 1, &tape->max_stages, NULL);
3381 ide_add_setting(drive, "pipeline_max", SETTING_RW, TYPE_INT, 1, 0xffff,
3382 tape->stage_size / 1024, 1, &tape->max_pipeline, NULL);
3383 ide_add_setting(drive, "pipeline_used", SETTING_READ, TYPE_INT, 0,
3384 0xffff, tape->stage_size / 1024, 1, &tape->nr_stages,
3385 NULL);
3386 ide_add_setting(drive, "pipeline_pending", SETTING_READ, TYPE_INT, 0,
3387 0xffff, tape->stage_size / 1024, 1,
3388 &tape->nr_pending_stages, NULL);
Borislav Petkovb6422012008-02-02 19:56:49 +01003389 ide_add_setting(drive, "speed", SETTING_READ, TYPE_SHORT, 0, 0xffff,
3390 1, 1, (u16 *)&tape->caps[14], NULL);
Borislav Petkov54bb2072008-02-06 02:57:52 +01003391 ide_add_setting(drive, "stage", SETTING_READ, TYPE_INT, 0, 0xffff, 1,
3392 1024, &tape->stage_size, NULL);
3393 ide_add_setting(drive, "tdsc", SETTING_RW, TYPE_INT, IDETAPE_DSC_RW_MIN,
3394 IDETAPE_DSC_RW_MAX, 1000, HZ, &tape->best_dsc_rw_freq,
3395 NULL);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01003396 ide_add_setting(drive, "dsc_overlap", SETTING_RW, TYPE_BYTE, 0, 1, 1,
3397 1, &drive->dsc_overlap, NULL);
3398 ide_add_setting(drive, "pipeline_head_speed_c", SETTING_READ, TYPE_INT,
3399 0, 0xffff, 1, 1, &tape->controlled_pipeline_head_speed,
3400 NULL);
3401 ide_add_setting(drive, "pipeline_head_speed_u", SETTING_READ, TYPE_INT,
3402 0, 0xffff, 1, 1,
3403 &tape->uncontrolled_pipeline_head_speed, NULL);
3404 ide_add_setting(drive, "avg_speed", SETTING_READ, TYPE_INT, 0, 0xffff,
3405 1, 1, &tape->avg_speed, NULL);
Borislav Petkov8004a8c2008-02-06 02:57:51 +01003406 ide_add_setting(drive, "debug_mask", SETTING_RW, TYPE_INT, 0, 0xffff, 1,
3407 1, &tape->debug_mask, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003408}
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +02003409#else
3410static inline void idetape_add_settings(ide_drive_t *drive) { ; }
3411#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003412
3413/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +01003414 * The function below is called to:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003415 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +01003416 * 1. Initialize our various state variables.
3417 * 2. Ask the tape for its capabilities.
3418 * 3. Allocate a buffer which will be used for data transfer. The buffer size
3419 * is chosen based on the recommendation which we received in step 2.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003420 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +01003421 * Note that at this point ide.c already assigned us an irq, so that we can
3422 * queue requests here and wait for their completion.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003423 */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01003424static void idetape_setup(ide_drive_t *drive, idetape_tape_t *tape, int minor)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003425{
3426 unsigned long t1, tmid, tn, t;
3427 int speed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003428 int stage_size;
Borislav Petkov71071b82008-02-06 02:57:53 +01003429 u8 gcw[2];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003430 struct sysinfo si;
Borislav Petkovb6422012008-02-02 19:56:49 +01003431 u16 *ctl = (u16 *)&tape->caps[12];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003432
Borislav Petkov54bb2072008-02-06 02:57:52 +01003433 spin_lock_init(&tape->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003434 drive->dsc_overlap = 1;
Bartlomiej Zolnierkiewicz4166c192008-02-01 23:09:30 +01003435 if (drive->hwif->host_flags & IDE_HFLAG_NO_DSC) {
3436 printk(KERN_INFO "ide-tape: %s: disabling DSC overlap\n",
3437 tape->name);
3438 drive->dsc_overlap = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003439 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003440 /* Seagate Travan drives do not support DSC overlap. */
3441 if (strstr(drive->id->model, "Seagate STT3401"))
3442 drive->dsc_overlap = 0;
3443 tape->minor = minor;
3444 tape->name[0] = 'h';
3445 tape->name[1] = 't';
3446 tape->name[2] = '0' + minor;
Borislav Petkov54abf372008-02-06 02:57:52 +01003447 tape->chrdev_dir = IDETAPE_DIR_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003448 tape->pc = tape->pc_stack;
3449 tape->max_insert_speed = 10000;
3450 tape->speed_control = 1;
3451 *((unsigned short *) &gcw) = drive->id->config;
Borislav Petkov71071b82008-02-06 02:57:53 +01003452
3453 /* Command packet DRQ type */
3454 if (((gcw[0] & 0x60) >> 5) == 1)
Borislav Petkov346331f2008-04-18 00:46:26 +02003455 set_bit(IDETAPE_FLAG_DRQ_INTERRUPT, &tape->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003456
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01003457 tape->min_pipeline = 10;
3458 tape->max_pipeline = 10;
3459 tape->max_stages = 10;
Borislav Petkov3c98bf32008-02-06 02:57:53 +01003460
Linus Torvalds1da177e2005-04-16 15:20:36 -07003461 idetape_get_inquiry_results(drive);
3462 idetape_get_mode_sense_results(drive);
Borislav Petkov3cffb9c2008-02-02 19:56:50 +01003463 ide_tape_get_bsize_from_bdesc(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003464 tape->user_bs_factor = 1;
Borislav Petkov54bb2072008-02-06 02:57:52 +01003465 tape->stage_size = *ctl * tape->blk_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003466 while (tape->stage_size > 0xffff) {
3467 printk(KERN_NOTICE "ide-tape: decreasing stage size\n");
Borislav Petkovb6422012008-02-02 19:56:49 +01003468 *ctl /= 2;
Borislav Petkov54bb2072008-02-06 02:57:52 +01003469 tape->stage_size = *ctl * tape->blk_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003470 }
3471 stage_size = tape->stage_size;
3472 tape->pages_per_stage = stage_size / PAGE_SIZE;
3473 if (stage_size % PAGE_SIZE) {
3474 tape->pages_per_stage++;
3475 tape->excess_bh_size = PAGE_SIZE - stage_size % PAGE_SIZE;
3476 }
3477
Borislav Petkovb6422012008-02-02 19:56:49 +01003478 /* Select the "best" DSC read/write polling freq and pipeline size. */
3479 speed = max(*(u16 *)&tape->caps[14], *(u16 *)&tape->caps[8]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003480
3481 tape->max_stages = speed * 1000 * 10 / tape->stage_size;
3482
Borislav Petkov3c98bf32008-02-06 02:57:53 +01003483 /* Limit memory use for pipeline to 10% of physical memory */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003484 si_meminfo(&si);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01003485 if (tape->max_stages * tape->stage_size >
3486 si.totalram * si.mem_unit / 10)
3487 tape->max_stages =
3488 si.totalram * si.mem_unit / (10 * tape->stage_size);
3489
Linus Torvalds1da177e2005-04-16 15:20:36 -07003490 tape->max_stages = min(tape->max_stages, IDETAPE_MAX_PIPELINE_STAGES);
3491 tape->min_pipeline = min(tape->max_stages, IDETAPE_MIN_PIPELINE_STAGES);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01003492 tape->max_pipeline =
3493 min(tape->max_stages * 2, IDETAPE_MAX_PIPELINE_STAGES);
3494 if (tape->max_stages == 0) {
3495 tape->max_stages = 1;
3496 tape->min_pipeline = 1;
3497 tape->max_pipeline = 1;
3498 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003499
3500 t1 = (tape->stage_size * HZ) / (speed * 1000);
Borislav Petkovb6422012008-02-02 19:56:49 +01003501 tmid = (*(u16 *)&tape->caps[16] * 32 * HZ) / (speed * 125);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003502 tn = (IDETAPE_FIFO_THRESHOLD * tape->stage_size * HZ) / (speed * 1000);
3503
3504 if (tape->max_stages)
3505 t = tn;
3506 else
3507 t = t1;
3508
3509 /*
Borislav Petkov3c98bf32008-02-06 02:57:53 +01003510 * Ensure that the number we got makes sense; limit it within
3511 * IDETAPE_DSC_RW_MIN and IDETAPE_DSC_RW_MAX.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003512 */
Borislav Petkov54bb2072008-02-06 02:57:52 +01003513 tape->best_dsc_rw_freq = max_t(unsigned long,
3514 min_t(unsigned long, t, IDETAPE_DSC_RW_MAX),
3515 IDETAPE_DSC_RW_MIN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003516 printk(KERN_INFO "ide-tape: %s <-> %s: %dKBps, %d*%dkB buffer, "
3517 "%dkB pipeline, %lums tDSC%s\n",
Borislav Petkovb6422012008-02-02 19:56:49 +01003518 drive->name, tape->name, *(u16 *)&tape->caps[14],
3519 (*(u16 *)&tape->caps[16] * 512) / tape->stage_size,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003520 tape->stage_size / 1024,
3521 tape->max_stages * tape->stage_size / 1024,
Borislav Petkov54bb2072008-02-06 02:57:52 +01003522 tape->best_dsc_rw_freq * 1000 / HZ,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003523 drive->using_dma ? ", DMA":"");
3524
3525 idetape_add_settings(drive);
3526}
3527
Russell King4031bbe2006-01-06 11:41:00 +00003528static void ide_tape_remove(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003529{
3530 idetape_tape_t *tape = drive->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003531
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +02003532 ide_proc_unregister_driver(drive, tape->driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003533
3534 ide_unregister_region(tape->disk);
3535
3536 ide_tape_put(tape);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003537}
3538
3539static void ide_tape_release(struct kref *kref)
3540{
3541 struct ide_tape_obj *tape = to_ide_tape(kref);
3542 ide_drive_t *drive = tape->drive;
3543 struct gendisk *g = tape->disk;
3544
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02003545 BUG_ON(tape->first_stage != NULL || tape->merge_stage_size);
3546
Linus Torvalds1da177e2005-04-16 15:20:36 -07003547 drive->dsc_overlap = 0;
3548 drive->driver_data = NULL;
Tony Jonesdbc12722007-09-25 02:03:03 +02003549 device_destroy(idetape_sysfs_class, MKDEV(IDETAPE_MAJOR, tape->minor));
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01003550 device_destroy(idetape_sysfs_class,
3551 MKDEV(IDETAPE_MAJOR, tape->minor + 128));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003552 idetape_devs[tape->minor] = NULL;
3553 g->private_data = NULL;
3554 put_disk(g);
3555 kfree(tape);
3556}
3557
Bartlomiej Zolnierkiewiczecfd80e2007-05-10 00:01:09 +02003558#ifdef CONFIG_IDE_PROC_FS
Linus Torvalds1da177e2005-04-16 15:20:36 -07003559static int proc_idetape_read_name
3560 (char *page, char **start, off_t off, int count, int *eof, void *data)
3561{
3562 ide_drive_t *drive = (ide_drive_t *) data;
3563 idetape_tape_t *tape = drive->driver_data;
3564 char *out = page;
3565 int len;
3566
3567 len = sprintf(out, "%s\n", tape->name);
3568 PROC_IDE_READ_RETURN(page, start, off, count, eof, len);
3569}
3570
3571static ide_proc_entry_t idetape_proc[] = {
3572 { "capacity", S_IFREG|S_IRUGO, proc_ide_read_capacity, NULL },
3573 { "name", S_IFREG|S_IRUGO, proc_idetape_read_name, NULL },
3574 { NULL, 0, NULL, NULL }
3575};
Linus Torvalds1da177e2005-04-16 15:20:36 -07003576#endif
3577
Russell King4031bbe2006-01-06 11:41:00 +00003578static int ide_tape_probe(ide_drive_t *);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003579
Linus Torvalds1da177e2005-04-16 15:20:36 -07003580static ide_driver_t idetape_driver = {
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02003581 .gen_driver = {
Laurent Riffard4ef3b8f2005-11-18 22:15:40 +01003582 .owner = THIS_MODULE,
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02003583 .name = "ide-tape",
3584 .bus = &ide_bus_type,
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02003585 },
Russell King4031bbe2006-01-06 11:41:00 +00003586 .probe = ide_tape_probe,
3587 .remove = ide_tape_remove,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003588 .version = IDETAPE_VERSION,
3589 .media = ide_tape,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003590 .supports_dsc_overlap = 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003591 .do_request = idetape_do_request,
3592 .end_request = idetape_end_request,
3593 .error = __ide_error,
3594 .abort = __ide_abort,
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +02003595#ifdef CONFIG_IDE_PROC_FS
Linus Torvalds1da177e2005-04-16 15:20:36 -07003596 .proc = idetape_proc,
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +02003597#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003598};
3599
Borislav Petkov3c98bf32008-02-06 02:57:53 +01003600/* Our character device supporting functions, passed to register_chrdev. */
Arjan van de Ven2b8693c2007-02-12 00:55:32 -08003601static const struct file_operations idetape_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003602 .owner = THIS_MODULE,
3603 .read = idetape_chrdev_read,
3604 .write = idetape_chrdev_write,
3605 .ioctl = idetape_chrdev_ioctl,
3606 .open = idetape_chrdev_open,
3607 .release = idetape_chrdev_release,
3608};
3609
3610static int idetape_open(struct inode *inode, struct file *filp)
3611{
3612 struct gendisk *disk = inode->i_bdev->bd_disk;
3613 struct ide_tape_obj *tape;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003614
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01003615 tape = ide_tape_get(disk);
3616 if (!tape)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003617 return -ENXIO;
3618
Linus Torvalds1da177e2005-04-16 15:20:36 -07003619 return 0;
3620}
3621
3622static int idetape_release(struct inode *inode, struct file *filp)
3623{
3624 struct gendisk *disk = inode->i_bdev->bd_disk;
3625 struct ide_tape_obj *tape = ide_tape_g(disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003626
3627 ide_tape_put(tape);
3628
3629 return 0;
3630}
3631
3632static int idetape_ioctl(struct inode *inode, struct file *file,
3633 unsigned int cmd, unsigned long arg)
3634{
3635 struct block_device *bdev = inode->i_bdev;
3636 struct ide_tape_obj *tape = ide_tape_g(bdev->bd_disk);
3637 ide_drive_t *drive = tape->drive;
3638 int err = generic_ide_ioctl(drive, file, bdev, cmd, arg);
3639 if (err == -EINVAL)
3640 err = idetape_blkdev_ioctl(drive, cmd, arg);
3641 return err;
3642}
3643
3644static struct block_device_operations idetape_block_ops = {
3645 .owner = THIS_MODULE,
3646 .open = idetape_open,
3647 .release = idetape_release,
3648 .ioctl = idetape_ioctl,
3649};
3650
Russell King4031bbe2006-01-06 11:41:00 +00003651static int ide_tape_probe(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003652{
3653 idetape_tape_t *tape;
3654 struct gendisk *g;
3655 int minor;
3656
3657 if (!strstr("ide-tape", drive->driver_req))
3658 goto failed;
3659 if (!drive->present)
3660 goto failed;
3661 if (drive->media != ide_tape)
3662 goto failed;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01003663 if (!idetape_identify_device(drive)) {
3664 printk(KERN_ERR "ide-tape: %s: not supported by this version of"
3665 " the driver\n", drive->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003666 goto failed;
3667 }
3668 if (drive->scsi) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01003669 printk(KERN_INFO "ide-tape: passing drive %s to ide-scsi"
3670 " emulation.\n", drive->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003671 goto failed;
3672 }
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01003673 tape = kzalloc(sizeof(idetape_tape_t), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003674 if (tape == NULL) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01003675 printk(KERN_ERR "ide-tape: %s: Can't allocate a tape struct\n",
3676 drive->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003677 goto failed;
3678 }
3679
3680 g = alloc_disk(1 << PARTN_BITS);
3681 if (!g)
3682 goto out_free_tape;
3683
3684 ide_init_disk(g, drive);
3685
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +02003686 ide_proc_register_driver(drive, &idetape_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003687
Linus Torvalds1da177e2005-04-16 15:20:36 -07003688 kref_init(&tape->kref);
3689
3690 tape->drive = drive;
3691 tape->driver = &idetape_driver;
3692 tape->disk = g;
3693
3694 g->private_data = &tape->driver;
3695
3696 drive->driver_data = tape;
3697
Arjan van de Vencf8b8972006-03-23 03:00:45 -08003698 mutex_lock(&idetape_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003699 for (minor = 0; idetape_devs[minor]; minor++)
3700 ;
3701 idetape_devs[minor] = tape;
Arjan van de Vencf8b8972006-03-23 03:00:45 -08003702 mutex_unlock(&idetape_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003703
3704 idetape_setup(drive, tape, minor);
3705
Tony Jonesdbc12722007-09-25 02:03:03 +02003706 device_create(idetape_sysfs_class, &drive->gendev,
3707 MKDEV(IDETAPE_MAJOR, minor), "%s", tape->name);
3708 device_create(idetape_sysfs_class, &drive->gendev,
3709 MKDEV(IDETAPE_MAJOR, minor + 128), "n%s", tape->name);
Will Dysond5dee802005-09-16 02:55:07 -07003710
Linus Torvalds1da177e2005-04-16 15:20:36 -07003711 g->fops = &idetape_block_ops;
3712 ide_register_region(g);
3713
3714 return 0;
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02003715
Linus Torvalds1da177e2005-04-16 15:20:36 -07003716out_free_tape:
3717 kfree(tape);
3718failed:
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02003719 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003720}
3721
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01003722static void __exit idetape_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003723{
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02003724 driver_unregister(&idetape_driver.gen_driver);
Will Dysond5dee802005-09-16 02:55:07 -07003725 class_destroy(idetape_sysfs_class);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003726 unregister_chrdev(IDETAPE_MAJOR, "ht");
3727}
3728
Bartlomiej Zolnierkiewicz17514e82005-11-19 22:24:35 +01003729static int __init idetape_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003730{
Will Dysond5dee802005-09-16 02:55:07 -07003731 int error = 1;
3732 idetape_sysfs_class = class_create(THIS_MODULE, "ide_tape");
3733 if (IS_ERR(idetape_sysfs_class)) {
3734 idetape_sysfs_class = NULL;
3735 printk(KERN_ERR "Unable to create sysfs class for ide tapes\n");
3736 error = -EBUSY;
3737 goto out;
3738 }
3739
Linus Torvalds1da177e2005-04-16 15:20:36 -07003740 if (register_chrdev(IDETAPE_MAJOR, "ht", &idetape_fops)) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01003741 printk(KERN_ERR "ide-tape: Failed to register chrdev"
3742 " interface\n");
Will Dysond5dee802005-09-16 02:55:07 -07003743 error = -EBUSY;
3744 goto out_free_class;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003745 }
Will Dysond5dee802005-09-16 02:55:07 -07003746
3747 error = driver_register(&idetape_driver.gen_driver);
3748 if (error)
3749 goto out_free_driver;
3750
3751 return 0;
3752
3753out_free_driver:
3754 driver_unregister(&idetape_driver.gen_driver);
3755out_free_class:
3756 class_destroy(idetape_sysfs_class);
3757out:
3758 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003759}
3760
Kay Sievers263756e2005-12-12 18:03:44 +01003761MODULE_ALIAS("ide:*m-tape*");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003762module_init(idetape_init);
3763module_exit(idetape_exit);
3764MODULE_ALIAS_CHARDEV_MAJOR(IDETAPE_MAJOR);
Borislav Petkov9c145762008-02-06 02:57:54 +01003765MODULE_DESCRIPTION("ATAPI Streaming TAPE Driver");
3766MODULE_LICENSE("GPL");