blob: 2fe4e8fdf3dada197ebd5c46477aa3b7c4877ffd [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
18#define IDETAPE_VERSION "1.19"
19
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>
42#include <asm/irq.h>
43#include <asm/uaccess.h>
44#include <asm/io.h>
45#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/*
78 * Pipelined mode parameters.
79 *
80 * We try to use the minimum number of stages which is enough to
81 * keep the tape constantly streaming. To accomplish that, we implement
82 * a feedback loop around the maximum number of stages:
83 *
84 * We start from MIN maximum stages (we will not even use MIN stages
85 * if we don't need them), increment it by RATE*(MAX-MIN)
86 * whenever we sense that the pipeline is empty, until we reach
87 * the optimum value or until we reach MAX.
88 *
89 * Setting the following parameter to 0 is illegal: the pipelined mode
Borislav Petkov37016ba2008-02-06 02:57:52 +010090 * cannot be disabled (idetape_calculate_speeds() divides by
91 * tape->max_stages.)
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 */
93#define IDETAPE_MIN_PIPELINE_STAGES 1
94#define IDETAPE_MAX_PIPELINE_STAGES 400
95#define IDETAPE_INCREASE_STAGES_RATE 20
96
97/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 * After each failed packet command we issue a request sense command
99 * and retry the packet command IDETAPE_MAX_PC_RETRIES times.
100 *
101 * Setting IDETAPE_MAX_PC_RETRIES to 0 will disable retries.
102 */
103#define IDETAPE_MAX_PC_RETRIES 3
104
105/*
106 * With each packet command, we allocate a buffer of
107 * IDETAPE_PC_BUFFER_SIZE bytes. This is used for several packet
108 * commands (Not for READ/WRITE commands).
109 */
110#define IDETAPE_PC_BUFFER_SIZE 256
111
112/*
113 * In various places in the driver, we need to allocate storage
114 * for packet commands and requests, which will remain valid while
115 * we leave the driver to wait for an interrupt or a timeout event.
116 */
117#define IDETAPE_PC_STACK (10 + IDETAPE_MAX_PC_RETRIES)
118
119/*
120 * Some drives (for example, Seagate STT3401A Travan) require a very long
121 * timeout, because they don't return an interrupt or clear their busy bit
122 * until after the command completes (even retension commands).
123 */
124#define IDETAPE_WAIT_CMD (900*HZ)
125
126/*
127 * The following parameter is used to select the point in the internal
128 * tape fifo in which we will start to refill the buffer. Decreasing
129 * the following parameter will improve the system's latency and
Robert P. J. Day3a4fa0a2007-10-19 23:10:43 +0200130 * interactive response, while using a high value might improve system
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131 * throughput.
132 */
133#define IDETAPE_FIFO_THRESHOLD 2
134
135/*
136 * DSC polling parameters.
137 *
138 * Polling for DSC (a single bit in the status register) is a very
139 * important function in ide-tape. There are two cases in which we
140 * poll for DSC:
141 *
142 * 1. Before a read/write packet command, to ensure that we
143 * can transfer data from/to the tape's data buffers, without
144 * causing an actual media access. In case the tape is not
145 * ready yet, we take out our request from the device
146 * request queue, so that ide.c will service requests from
147 * the other device on the same interface meanwhile.
148 *
149 * 2. After the successful initialization of a "media access
150 * packet command", which is a command which can take a long
151 * time to complete (it can be several seconds or even an hour).
152 *
153 * Again, we postpone our request in the middle to free the bus
154 * for the other device. The polling frequency here should be
155 * lower than the read/write frequency since those media access
156 * commands are slow. We start from a "fast" frequency -
157 * IDETAPE_DSC_MA_FAST (one second), and if we don't receive DSC
158 * after IDETAPE_DSC_MA_THRESHOLD (5 minutes), we switch it to a
159 * lower frequency - IDETAPE_DSC_MA_SLOW (1 minute).
160 *
161 * We also set a timeout for the timer, in case something goes wrong.
162 * The timeout should be longer then the maximum execution time of a
163 * tape operation.
164 */
165
166/*
167 * DSC timings.
168 */
169#define IDETAPE_DSC_RW_MIN 5*HZ/100 /* 50 msec */
170#define IDETAPE_DSC_RW_MAX 40*HZ/100 /* 400 msec */
171#define IDETAPE_DSC_RW_TIMEOUT 2*60*HZ /* 2 minutes */
172#define IDETAPE_DSC_MA_FAST 2*HZ /* 2 seconds */
173#define IDETAPE_DSC_MA_THRESHOLD 5*60*HZ /* 5 minutes */
174#define IDETAPE_DSC_MA_SLOW 30*HZ /* 30 seconds */
175#define IDETAPE_DSC_MA_TIMEOUT 2*60*60*HZ /* 2 hours */
176
177/*************************** End of tunable parameters ***********************/
178
179/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 * Read/Write error simulation
181 */
182#define SIMULATE_ERRORS 0
183
184/*
185 * For general magnetic tape device compatibility.
186 */
Borislav Petkov54abf372008-02-06 02:57:52 +0100187
188/* tape directions */
189enum {
190 IDETAPE_DIR_NONE = (1 << 0),
191 IDETAPE_DIR_READ = (1 << 1),
192 IDETAPE_DIR_WRITE = (1 << 2),
193};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194
195struct idetape_bh {
Stephen Rothwellab057962007-08-01 23:46:44 +0200196 u32 b_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 atomic_t b_count;
198 struct idetape_bh *b_reqnext;
199 char *b_data;
200};
201
202/*
203 * Our view of a packet command.
204 */
205typedef struct idetape_packet_command_s {
206 u8 c[12]; /* Actual packet bytes */
207 int retries; /* On each retry, we increment retries */
208 int error; /* Error code */
209 int request_transfer; /* Bytes to transfer */
210 int actually_transferred; /* Bytes actually transferred */
211 int buffer_size; /* Size of our data buffer */
212 struct idetape_bh *bh;
213 char *b_data;
214 int b_count;
215 u8 *buffer; /* Data buffer */
216 u8 *current_position; /* Pointer into the above buffer */
217 ide_startstop_t (*callback) (ide_drive_t *); /* Called when this packet command is completed */
218 u8 pc_buffer[IDETAPE_PC_BUFFER_SIZE]; /* Temporary buffer */
219 unsigned long flags; /* Status/Action bit flags: long for set_bit */
220} idetape_pc_t;
221
222/*
223 * Packet command flag bits.
224 */
225/* Set when an error is considered normal - We won't retry */
226#define PC_ABORT 0
227/* 1 When polling for DSC on a media access command */
228#define PC_WAIT_FOR_DSC 1
229/* 1 when we prefer to use DMA if possible */
230#define PC_DMA_RECOMMENDED 2
231/* 1 while DMA in progress */
232#define PC_DMA_IN_PROGRESS 3
233/* 1 when encountered problem during DMA */
234#define PC_DMA_ERROR 4
235/* Data direction */
236#define PC_WRITING 5
237
238/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 * A pipeline stage.
240 */
241typedef struct idetape_stage_s {
242 struct request rq; /* The corresponding request */
243 struct idetape_bh *bh; /* The data buffers */
244 struct idetape_stage_s *next; /* Pointer to the next stage */
245} idetape_stage_t;
246
247/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 * Most of our global data which we need to save even as we leave the
249 * driver due to an interrupt or a timer event is stored in a variable
250 * of type idetape_tape_t, defined below.
251 */
252typedef struct ide_tape_obj {
253 ide_drive_t *drive;
254 ide_driver_t *driver;
255 struct gendisk *disk;
256 struct kref kref;
257
258 /*
259 * Since a typical character device operation requires more
260 * than one packet command, we provide here enough memory
261 * for the maximum of interconnected packet commands.
262 * The packet commands are stored in the circular array pc_stack.
263 * pc_stack_index points to the last used entry, and warps around
264 * to the start when we get to the last array entry.
265 *
266 * pc points to the current processed packet command.
267 *
268 * failed_pc points to the last failed packet command, or contains
269 * NULL if we do not need to retry any packet command. This is
270 * required since an additional packet command is needed before the
271 * retry, to get detailed information on what went wrong.
272 */
273 /* Current packet command */
274 idetape_pc_t *pc;
275 /* Last failed packet command */
276 idetape_pc_t *failed_pc;
277 /* Packet command stack */
278 idetape_pc_t pc_stack[IDETAPE_PC_STACK];
279 /* Next free packet command storage space */
280 int pc_stack_index;
281 struct request rq_stack[IDETAPE_PC_STACK];
282 /* We implement a circular array */
283 int rq_stack_index;
284
285 /*
286 * DSC polling variables.
287 *
288 * While polling for DSC we use postponed_rq to postpone the
289 * current request so that ide.c will be able to service
290 * pending requests on the other device. Note that at most
291 * we will have only one DSC (usually data transfer) request
292 * in the device request queue. Additional requests can be
293 * queued in our internal pipeline, but they will be visible
294 * to ide.c only one at a time.
295 */
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
306 /*
307 * Read position information
308 */
309 u8 partition;
310 /* Current block */
Borislav Petkov54bb2072008-02-06 02:57:52 +0100311 unsigned int first_frame;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312
313 /*
314 * Last error information
315 */
316 u8 sense_key, asc, ascq;
317
318 /*
319 * Character device operation
320 */
321 unsigned int minor;
322 /* device name */
323 char name[4];
324 /* Current character device data transfer direction */
Borislav Petkov54abf372008-02-06 02:57:52 +0100325 u8 chrdev_dir;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326
Borislav Petkov54bb2072008-02-06 02:57:52 +0100327 /* tape block size, usually 512 or 1024 bytes */
328 unsigned short blk_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 int user_bs_factor;
Borislav Petkovb6422012008-02-02 19:56:49 +0100330
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 /* Copy of the tape's Capabilities and Mechanical Page */
Borislav Petkovb6422012008-02-02 19:56:49 +0100332 u8 caps[20];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333
334 /*
335 * Active data transfer request parameters.
336 *
337 * At most, there is only one ide-tape originated data transfer
338 * request in the device request queue. This allows ide.c to
339 * easily service requests from the other device when we
340 * postpone our active request. In the pipelined operation
341 * mode, we use our internal pipeline structure to hold
342 * more data requests.
343 *
344 * The data buffer size is chosen based on the tape's
345 * recommendation.
346 */
Borislav Petkov54bb2072008-02-06 02:57:52 +0100347 /* Ptr to the request which is waiting in the device request queue */
348 struct request *active_data_rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 /* Data buffer size (chosen based on the tape's recommendation */
350 int stage_size;
351 idetape_stage_t *merge_stage;
352 int merge_stage_size;
353 struct idetape_bh *bh;
354 char *b_data;
355 int b_count;
356
357 /*
358 * Pipeline parameters.
359 *
360 * To accomplish non-pipelined mode, we simply set the following
361 * variables to zero (or NULL, where appropriate).
362 */
363 /* Number of currently used stages */
364 int nr_stages;
365 /* Number of pending stages */
366 int nr_pending_stages;
367 /* We will not allocate more than this number of stages */
368 int max_stages, min_pipeline, max_pipeline;
369 /* The first stage which will be removed from the pipeline */
370 idetape_stage_t *first_stage;
371 /* The currently active stage */
372 idetape_stage_t *active_stage;
373 /* Will be serviced after the currently active request */
374 idetape_stage_t *next_stage;
375 /* New requests will be added to the pipeline here */
376 idetape_stage_t *last_stage;
377 /* Optional free stage which we can use */
378 idetape_stage_t *cache_stage;
379 int pages_per_stage;
380 /* Wasted space in each stage */
381 int excess_bh_size;
382
383 /* Status/Action flags: long for set_bit */
384 unsigned long flags;
385 /* protects the ide-tape queue */
Borislav Petkov54bb2072008-02-06 02:57:52 +0100386 spinlock_t lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387
388 /*
389 * Measures average tape speed
390 */
391 unsigned long avg_time;
392 int avg_size;
393 int avg_speed;
394
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 /* the door is currently locked */
396 int door_locked;
397 /* the tape hardware is write protected */
398 char drv_write_prot;
399 /* the tape is write protected (hardware or opened as read-only) */
400 char write_prot;
401
402 /*
403 * Limit the number of times a request can
404 * be postponed, to avoid an infinite postpone
405 * deadlock.
406 */
407 /* request postpone count limit */
408 int postpone_cnt;
409
410 /*
411 * Measures number of frames:
412 *
413 * 1. written/read to/from the driver pipeline (pipeline_head).
414 * 2. written/read to/from the tape buffers (idetape_bh).
415 * 3. written/read by the tape to/from the media (tape_head).
416 */
417 int pipeline_head;
418 int buffer_head;
419 int tape_head;
420 int last_tape_head;
421
422 /*
423 * Speed control at the tape buffers input/output
424 */
425 unsigned long insert_time;
426 int insert_size;
427 int insert_speed;
428 int max_insert_speed;
429 int measure_insert_time;
430
431 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 * Speed regulation negative feedback loop
433 */
434 int speed_control;
435 int pipeline_head_speed;
436 int controlled_pipeline_head_speed;
437 int uncontrolled_pipeline_head_speed;
438 int controlled_last_pipeline_head;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 unsigned long uncontrolled_pipeline_head_time;
440 unsigned long controlled_pipeline_head_time;
441 int controlled_previous_pipeline_head;
442 int uncontrolled_previous_pipeline_head;
443 unsigned long controlled_previous_head_time;
444 unsigned long uncontrolled_previous_head_time;
445 int restart_speed_control_req;
446
Borislav Petkov8004a8c2008-02-06 02:57:51 +0100447 u32 debug_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448} idetape_tape_t;
449
Arjan van de Vencf8b8972006-03-23 03:00:45 -0800450static DEFINE_MUTEX(idetape_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451
Will Dysond5dee802005-09-16 02:55:07 -0700452static struct class *idetape_sysfs_class;
453
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454#define to_ide_tape(obj) container_of(obj, struct ide_tape_obj, kref)
455
456#define ide_tape_g(disk) \
457 container_of((disk)->private_data, struct ide_tape_obj, driver)
458
459static struct ide_tape_obj *ide_tape_get(struct gendisk *disk)
460{
461 struct ide_tape_obj *tape = NULL;
462
Arjan van de Vencf8b8972006-03-23 03:00:45 -0800463 mutex_lock(&idetape_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 tape = ide_tape_g(disk);
465 if (tape)
466 kref_get(&tape->kref);
Arjan van de Vencf8b8972006-03-23 03:00:45 -0800467 mutex_unlock(&idetape_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 return tape;
469}
470
471static void ide_tape_release(struct kref *);
472
473static void ide_tape_put(struct ide_tape_obj *tape)
474{
Arjan van de Vencf8b8972006-03-23 03:00:45 -0800475 mutex_lock(&idetape_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 kref_put(&tape->kref, ide_tape_release);
Arjan van de Vencf8b8972006-03-23 03:00:45 -0800477 mutex_unlock(&idetape_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478}
479
480/*
481 * Tape door status
482 */
483#define DOOR_UNLOCKED 0
484#define DOOR_LOCKED 1
485#define DOOR_EXPLICITLY_LOCKED 2
486
487/*
488 * Tape flag bits values.
489 */
490#define IDETAPE_IGNORE_DSC 0
491#define IDETAPE_ADDRESS_VALID 1 /* 0 When the tape position is unknown */
492#define IDETAPE_BUSY 2 /* Device already opened */
493#define IDETAPE_PIPELINE_ERROR 3 /* Error detected in a pipeline stage */
494#define IDETAPE_DETECT_BS 4 /* Attempt to auto-detect the current user block size */
495#define IDETAPE_FILEMARK 5 /* Currently on a filemark */
496#define IDETAPE_DRQ_INTERRUPT 6 /* DRQ interrupt device */
497#define IDETAPE_READ_ERROR 7
498#define IDETAPE_PIPELINE_ACTIVE 8 /* pipeline active */
499/* 0 = no tape is loaded, so we don't rewind after ejecting */
500#define IDETAPE_MEDIUM_PRESENT 9
501
502/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 * Some defines for the READ BUFFER command
504 */
505#define IDETAPE_RETRIEVE_FAULTY_BLOCK 6
506
507/*
508 * Some defines for the SPACE command
509 */
510#define IDETAPE_SPACE_OVER_FILEMARK 1
511#define IDETAPE_SPACE_TO_EOD 3
512
513/*
514 * Some defines for the LOAD UNLOAD command
515 */
516#define IDETAPE_LU_LOAD_MASK 1
517#define IDETAPE_LU_RETENSION_MASK 2
518#define IDETAPE_LU_EOT_MASK 4
519
520/*
521 * Special requests for our block device strategy routine.
522 *
523 * In order to service a character device command, we add special
524 * requests to the tail of our block device request queue and wait
525 * for their completion.
526 */
527
528enum {
529 REQ_IDETAPE_PC1 = (1 << 0), /* packet command (first stage) */
530 REQ_IDETAPE_PC2 = (1 << 1), /* packet command (second stage) */
531 REQ_IDETAPE_READ = (1 << 2),
532 REQ_IDETAPE_WRITE = (1 << 3),
533 REQ_IDETAPE_READ_BUFFER = (1 << 4),
534};
535
536/*
537 * Error codes which are returned in rq->errors to the higher part
538 * of the driver.
539 */
540#define IDETAPE_ERROR_GENERAL 101
541#define IDETAPE_ERROR_FILEMARK 102
542#define IDETAPE_ERROR_EOD 103
543
544/*
545 * The following is used to format the general configuration word of
546 * the ATAPI IDENTIFY DEVICE command.
547 */
548struct idetape_id_gcw {
549 unsigned packet_size :2; /* Packet Size */
550 unsigned reserved234 :3; /* Reserved */
551 unsigned drq_type :2; /* Command packet DRQ type */
552 unsigned removable :1; /* Removable media */
553 unsigned device_type :5; /* Device type */
554 unsigned reserved13 :1; /* Reserved */
555 unsigned protocol :2; /* Protocol type */
556};
557
Borislav Petkovfa366252008-02-02 19:56:51 +0100558/* Structures related to the SELECT SENSE / MODE SENSE packet commands. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559#define IDETAPE_BLOCK_DESCRIPTOR 0
560#define IDETAPE_CAPABILITIES_PAGE 0x2a
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561
562/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 * The variables below are used for the character device interface.
564 * Additional state variables are defined in our ide_drive_t structure.
565 */
566static struct ide_tape_obj * idetape_devs[MAX_HWIFS * MAX_DRIVES];
567
568#define ide_tape_f(file) ((file)->private_data)
569
570static struct ide_tape_obj *ide_tape_chrdev_get(unsigned int i)
571{
572 struct ide_tape_obj *tape = NULL;
573
Arjan van de Vencf8b8972006-03-23 03:00:45 -0800574 mutex_lock(&idetape_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 tape = idetape_devs[i];
576 if (tape)
577 kref_get(&tape->kref);
Arjan van de Vencf8b8972006-03-23 03:00:45 -0800578 mutex_unlock(&idetape_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 return tape;
580}
581
582/*
583 * Function declarations
584 *
585 */
586static int idetape_chrdev_release (struct inode *inode, struct file *filp);
587static void idetape_write_release (ide_drive_t *drive, unsigned int minor);
588
589/*
590 * Too bad. The drive wants to send us data which we are not ready to accept.
591 * Just throw it away.
592 */
593static void idetape_discard_data (ide_drive_t *drive, unsigned int bcount)
594{
595 while (bcount--)
596 (void) HWIF(drive)->INB(IDE_DATA_REG);
597}
598
599static void idetape_input_buffers (ide_drive_t *drive, idetape_pc_t *pc, unsigned int bcount)
600{
601 struct idetape_bh *bh = pc->bh;
602 int count;
603
604 while (bcount) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 if (bh == NULL) {
606 printk(KERN_ERR "ide-tape: bh == NULL in "
607 "idetape_input_buffers\n");
608 idetape_discard_data(drive, bcount);
609 return;
610 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611 count = min((unsigned int)(bh->b_size - atomic_read(&bh->b_count)), bcount);
612 HWIF(drive)->atapi_input_bytes(drive, bh->b_data + atomic_read(&bh->b_count), count);
613 bcount -= count;
614 atomic_add(count, &bh->b_count);
615 if (atomic_read(&bh->b_count) == bh->b_size) {
616 bh = bh->b_reqnext;
617 if (bh)
618 atomic_set(&bh->b_count, 0);
619 }
620 }
621 pc->bh = bh;
622}
623
624static void idetape_output_buffers (ide_drive_t *drive, idetape_pc_t *pc, unsigned int bcount)
625{
626 struct idetape_bh *bh = pc->bh;
627 int count;
628
629 while (bcount) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630 if (bh == NULL) {
631 printk(KERN_ERR "ide-tape: bh == NULL in "
632 "idetape_output_buffers\n");
633 return;
634 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 count = min((unsigned int)pc->b_count, (unsigned int)bcount);
636 HWIF(drive)->atapi_output_bytes(drive, pc->b_data, count);
637 bcount -= count;
638 pc->b_data += count;
639 pc->b_count -= count;
640 if (!pc->b_count) {
641 pc->bh = bh = bh->b_reqnext;
642 if (bh) {
643 pc->b_data = bh->b_data;
644 pc->b_count = atomic_read(&bh->b_count);
645 }
646 }
647 }
648}
649
650static void idetape_update_buffers (idetape_pc_t *pc)
651{
652 struct idetape_bh *bh = pc->bh;
653 int count;
654 unsigned int bcount = pc->actually_transferred;
655
656 if (test_bit(PC_WRITING, &pc->flags))
657 return;
658 while (bcount) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659 if (bh == NULL) {
660 printk(KERN_ERR "ide-tape: bh == NULL in "
661 "idetape_update_buffers\n");
662 return;
663 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664 count = min((unsigned int)bh->b_size, (unsigned int)bcount);
665 atomic_set(&bh->b_count, count);
666 if (atomic_read(&bh->b_count) == bh->b_size)
667 bh = bh->b_reqnext;
668 bcount -= count;
669 }
670 pc->bh = bh;
671}
672
673/*
674 * idetape_next_pc_storage returns a pointer to a place in which we can
675 * safely store a packet command, even though we intend to leave the
676 * driver. A storage space for a maximum of IDETAPE_PC_STACK packet
677 * commands is allocated at initialization time.
678 */
679static idetape_pc_t *idetape_next_pc_storage (ide_drive_t *drive)
680{
681 idetape_tape_t *tape = drive->driver_data;
682
Borislav Petkov8004a8c2008-02-06 02:57:51 +0100683 debug_log(DBG_PCRQ_STACK, "pc_stack_index=%d\n", tape->pc_stack_index);
684
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 if (tape->pc_stack_index == IDETAPE_PC_STACK)
686 tape->pc_stack_index=0;
687 return (&tape->pc_stack[tape->pc_stack_index++]);
688}
689
690/*
691 * idetape_next_rq_storage is used along with idetape_next_pc_storage.
692 * Since we queue packet commands in the request queue, we need to
693 * allocate a request, along with the allocation of a packet command.
694 */
695
696/**************************************************************
697 * *
698 * This should get fixed to use kmalloc(.., GFP_ATOMIC) *
699 * followed later on by kfree(). -ml *
700 * *
701 **************************************************************/
702
703static struct request *idetape_next_rq_storage (ide_drive_t *drive)
704{
705 idetape_tape_t *tape = drive->driver_data;
706
Borislav Petkov8004a8c2008-02-06 02:57:51 +0100707 debug_log(DBG_PCRQ_STACK, "rq_stack_index=%d\n", tape->rq_stack_index);
708
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709 if (tape->rq_stack_index == IDETAPE_PC_STACK)
710 tape->rq_stack_index=0;
711 return (&tape->rq_stack[tape->rq_stack_index++]);
712}
713
714/*
715 * idetape_init_pc initializes a packet command.
716 */
717static void idetape_init_pc (idetape_pc_t *pc)
718{
719 memset(pc->c, 0, 12);
720 pc->retries = 0;
721 pc->flags = 0;
722 pc->request_transfer = 0;
723 pc->buffer = pc->pc_buffer;
724 pc->buffer_size = IDETAPE_PC_BUFFER_SIZE;
725 pc->bh = NULL;
726 pc->b_data = NULL;
727}
728
729/*
Borislav Petkov1b5db432008-02-02 19:56:48 +0100730 * called on each failed packet command retry to analyze the request sense. We
731 * currently do not utilize this information.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732 */
Borislav Petkov1b5db432008-02-02 19:56:48 +0100733static void idetape_analyze_error(ide_drive_t *drive, u8 *sense)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734{
735 idetape_tape_t *tape = drive->driver_data;
736 idetape_pc_t *pc = tape->failed_pc;
737
Borislav Petkov1b5db432008-02-02 19:56:48 +0100738 tape->sense_key = sense[2] & 0xF;
739 tape->asc = sense[12];
740 tape->ascq = sense[13];
Borislav Petkov8004a8c2008-02-06 02:57:51 +0100741
742 debug_log(DBG_ERR, "pc = %x, sense key = %x, asc = %x, ascq = %x\n",
743 pc->c[0], tape->sense_key, tape->asc, tape->ascq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744
Borislav Petkov1b5db432008-02-02 19:56:48 +0100745 /* Correct pc->actually_transferred by asking the tape. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746 if (test_bit(PC_DMA_ERROR, &pc->flags)) {
Borislav Petkov1b5db432008-02-02 19:56:48 +0100747 pc->actually_transferred = pc->request_transfer -
Borislav Petkov54bb2072008-02-06 02:57:52 +0100748 tape->blk_size *
Borislav Petkov860ff5e2008-02-02 19:56:50 +0100749 be32_to_cpu(get_unaligned((u32 *)&sense[3]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750 idetape_update_buffers(pc);
751 }
752
753 /*
754 * If error was the result of a zero-length read or write command,
755 * with sense key=5, asc=0x22, ascq=0, let it slide. Some drives
756 * (i.e. Seagate STT3401A Travan) don't support 0-length read/writes.
757 */
Borislav Petkov90699ce2008-02-02 19:56:50 +0100758 if ((pc->c[0] == READ_6 || pc->c[0] == WRITE_6)
Borislav Petkov1b5db432008-02-02 19:56:48 +0100759 /* length == 0 */
760 && pc->c[4] == 0 && pc->c[3] == 0 && pc->c[2] == 0) {
761 if (tape->sense_key == 5) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762 /* don't report an error, everything's ok */
763 pc->error = 0;
764 /* don't retry read/write */
765 set_bit(PC_ABORT, &pc->flags);
766 }
767 }
Borislav Petkov90699ce2008-02-02 19:56:50 +0100768 if (pc->c[0] == READ_6 && (sense[2] & 0x80)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769 pc->error = IDETAPE_ERROR_FILEMARK;
770 set_bit(PC_ABORT, &pc->flags);
771 }
Borislav Petkov90699ce2008-02-02 19:56:50 +0100772 if (pc->c[0] == WRITE_6) {
Borislav Petkov1b5db432008-02-02 19:56:48 +0100773 if ((sense[2] & 0x40) || (tape->sense_key == 0xd
774 && tape->asc == 0x0 && tape->ascq == 0x2)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775 pc->error = IDETAPE_ERROR_EOD;
776 set_bit(PC_ABORT, &pc->flags);
777 }
778 }
Borislav Petkov90699ce2008-02-02 19:56:50 +0100779 if (pc->c[0] == READ_6 || pc->c[0] == WRITE_6) {
Borislav Petkov1b5db432008-02-02 19:56:48 +0100780 if (tape->sense_key == 8) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781 pc->error = IDETAPE_ERROR_EOD;
782 set_bit(PC_ABORT, &pc->flags);
783 }
784 if (!test_bit(PC_ABORT, &pc->flags) &&
785 pc->actually_transferred)
786 pc->retries = IDETAPE_MAX_PC_RETRIES + 1;
787 }
788}
789
Borislav Petkov419d4742008-02-02 19:56:51 +0100790static void idetape_activate_next_stage(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791{
792 idetape_tape_t *tape = drive->driver_data;
793 idetape_stage_t *stage = tape->next_stage;
794 struct request *rq = &stage->rq;
795
Borislav Petkov8004a8c2008-02-06 02:57:51 +0100796 debug_log(DBG_PROCS, "Enter %s\n", __func__);
797
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 if (stage == NULL) {
Borislav Petkov8004a8c2008-02-06 02:57:51 +0100799 printk(KERN_ERR "ide-tape: bug: Trying to activate a non"
800 " existing stage\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801 return;
802 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803
804 rq->rq_disk = tape->disk;
805 rq->buffer = NULL;
806 rq->special = (void *)stage->bh;
Borislav Petkov54bb2072008-02-06 02:57:52 +0100807 tape->active_data_rq = rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 tape->active_stage = stage;
809 tape->next_stage = stage->next;
810}
811
812/*
813 * idetape_increase_max_pipeline_stages is a part of the feedback
814 * loop which tries to find the optimum number of stages. In the
815 * feedback loop, we are starting from a minimum maximum number of
816 * stages, and if we sense that the pipeline is empty, we try to
817 * increase it, until we reach the user compile time memory limit.
818 */
819static void idetape_increase_max_pipeline_stages (ide_drive_t *drive)
820{
821 idetape_tape_t *tape = drive->driver_data;
822 int increase = (tape->max_pipeline - tape->min_pipeline) / 10;
Borislav Petkov8004a8c2008-02-06 02:57:51 +0100823
824 debug_log(DBG_PROCS, "Enter %s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825
826 tape->max_stages += max(increase, 1);
827 tape->max_stages = max(tape->max_stages, tape->min_pipeline);
828 tape->max_stages = min(tape->max_stages, tape->max_pipeline);
829}
830
831/*
832 * idetape_kfree_stage calls kfree to completely free a stage, along with
833 * its related buffers.
834 */
835static void __idetape_kfree_stage (idetape_stage_t *stage)
836{
837 struct idetape_bh *prev_bh, *bh = stage->bh;
838 int size;
839
840 while (bh != NULL) {
841 if (bh->b_data != NULL) {
842 size = (int) bh->b_size;
843 while (size > 0) {
844 free_page((unsigned long) bh->b_data);
845 size -= PAGE_SIZE;
846 bh->b_data += PAGE_SIZE;
847 }
848 }
849 prev_bh = bh;
850 bh = bh->b_reqnext;
851 kfree(prev_bh);
852 }
853 kfree(stage);
854}
855
856static void idetape_kfree_stage (idetape_tape_t *tape, idetape_stage_t *stage)
857{
858 __idetape_kfree_stage(stage);
859}
860
861/*
862 * idetape_remove_stage_head removes tape->first_stage from the pipeline.
863 * The caller should avoid race conditions.
864 */
865static void idetape_remove_stage_head (ide_drive_t *drive)
866{
867 idetape_tape_t *tape = drive->driver_data;
868 idetape_stage_t *stage;
Borislav Petkov8004a8c2008-02-06 02:57:51 +0100869
870 debug_log(DBG_PROCS, "Enter %s\n", __func__);
871
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872 if (tape->first_stage == NULL) {
873 printk(KERN_ERR "ide-tape: bug: tape->first_stage is NULL\n");
Borislav Petkov55a5d292008-02-02 19:56:49 +0100874 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875 }
876 if (tape->active_stage == tape->first_stage) {
Borislav Petkov8004a8c2008-02-06 02:57:51 +0100877 printk(KERN_ERR "ide-tape: bug: Trying to free our active "
878 "pipeline stage\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879 return;
880 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881 stage = tape->first_stage;
882 tape->first_stage = stage->next;
883 idetape_kfree_stage(tape, stage);
884 tape->nr_stages--;
885 if (tape->first_stage == NULL) {
886 tape->last_stage = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887 if (tape->next_stage != NULL)
888 printk(KERN_ERR "ide-tape: bug: tape->next_stage != NULL\n");
889 if (tape->nr_stages)
890 printk(KERN_ERR "ide-tape: bug: nr_stages should be 0 now\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891 }
892}
893
894/*
895 * This will free all the pipeline stages starting from new_last_stage->next
896 * to the end of the list, and point tape->last_stage to new_last_stage.
897 */
898static void idetape_abort_pipeline(ide_drive_t *drive,
899 idetape_stage_t *new_last_stage)
900{
901 idetape_tape_t *tape = drive->driver_data;
902 idetape_stage_t *stage = new_last_stage->next;
903 idetape_stage_t *nstage;
904
Borislav Petkov8004a8c2008-02-06 02:57:51 +0100905 debug_log(DBG_PROCS, "%s: Enter %s\n", tape->name, __func__);
906
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907 while (stage) {
908 nstage = stage->next;
909 idetape_kfree_stage(tape, stage);
910 --tape->nr_stages;
911 --tape->nr_pending_stages;
912 stage = nstage;
913 }
914 if (new_last_stage)
915 new_last_stage->next = NULL;
916 tape->last_stage = new_last_stage;
917 tape->next_stage = NULL;
918}
919
920/*
921 * idetape_end_request is used to finish servicing a request, and to
922 * insert a pending pipeline request into the main device queue.
923 */
924static int idetape_end_request(ide_drive_t *drive, int uptodate, int nr_sects)
925{
926 struct request *rq = HWGROUP(drive)->rq;
927 idetape_tape_t *tape = drive->driver_data;
928 unsigned long flags;
929 int error;
930 int remove_stage = 0;
931 idetape_stage_t *active_stage;
932
Borislav Petkov8004a8c2008-02-06 02:57:51 +0100933 debug_log(DBG_PROCS, "Enter %s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934
935 switch (uptodate) {
936 case 0: error = IDETAPE_ERROR_GENERAL; break;
937 case 1: error = 0; break;
938 default: error = uptodate;
939 }
940 rq->errors = error;
941 if (error)
942 tape->failed_pc = NULL;
943
Bartlomiej Zolnierkiewicz36872212008-01-26 20:13:10 +0100944 if (!blk_special_request(rq)) {
945 ide_end_request(drive, uptodate, nr_sects);
946 return 0;
947 }
948
Borislav Petkov54bb2072008-02-06 02:57:52 +0100949 spin_lock_irqsave(&tape->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950
951 /* The request was a pipelined data transfer request */
Borislav Petkov54bb2072008-02-06 02:57:52 +0100952 if (tape->active_data_rq == rq) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953 active_stage = tape->active_stage;
954 tape->active_stage = NULL;
Borislav Petkov54bb2072008-02-06 02:57:52 +0100955 tape->active_data_rq = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956 tape->nr_pending_stages--;
957 if (rq->cmd[0] & REQ_IDETAPE_WRITE) {
958 remove_stage = 1;
959 if (error) {
960 set_bit(IDETAPE_PIPELINE_ERROR, &tape->flags);
961 if (error == IDETAPE_ERROR_EOD)
962 idetape_abort_pipeline(drive, active_stage);
963 }
964 } else if (rq->cmd[0] & REQ_IDETAPE_READ) {
965 if (error == IDETAPE_ERROR_EOD) {
966 set_bit(IDETAPE_PIPELINE_ERROR, &tape->flags);
967 idetape_abort_pipeline(drive, active_stage);
968 }
969 }
970 if (tape->next_stage != NULL) {
Borislav Petkov419d4742008-02-02 19:56:51 +0100971 idetape_activate_next_stage(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972
973 /*
974 * Insert the next request into the request queue.
975 */
Borislav Petkov54bb2072008-02-06 02:57:52 +0100976 (void)ide_do_drive_cmd(drive, tape->active_data_rq,
977 ide_end);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978 } else if (!error) {
979 idetape_increase_max_pipeline_stages(drive);
980 }
981 }
982 ide_end_drive_cmd(drive, 0, 0);
983// blkdev_dequeue_request(rq);
984// drive->rq = NULL;
985// end_that_request_last(rq);
986
987 if (remove_stage)
988 idetape_remove_stage_head(drive);
Borislav Petkov54bb2072008-02-06 02:57:52 +0100989 if (tape->active_data_rq == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990 clear_bit(IDETAPE_PIPELINE_ACTIVE, &tape->flags);
Borislav Petkov54bb2072008-02-06 02:57:52 +0100991 spin_unlock_irqrestore(&tape->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992 return 0;
993}
994
995static ide_startstop_t idetape_request_sense_callback (ide_drive_t *drive)
996{
997 idetape_tape_t *tape = drive->driver_data;
998
Borislav Petkov8004a8c2008-02-06 02:57:51 +0100999 debug_log(DBG_PROCS, "Enter %s\n", __func__);
1000
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001 if (!tape->pc->error) {
Borislav Petkov1b5db432008-02-02 19:56:48 +01001002 idetape_analyze_error(drive, tape->pc->buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003 idetape_end_request(drive, 1, 0);
1004 } else {
1005 printk(KERN_ERR "ide-tape: Error in REQUEST SENSE itself - Aborting request!\n");
1006 idetape_end_request(drive, 0, 0);
1007 }
1008 return ide_stopped;
1009}
1010
1011static void idetape_create_request_sense_cmd (idetape_pc_t *pc)
1012{
1013 idetape_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +01001014 pc->c[0] = REQUEST_SENSE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015 pc->c[4] = 20;
1016 pc->request_transfer = 20;
1017 pc->callback = &idetape_request_sense_callback;
1018}
1019
1020static void idetape_init_rq(struct request *rq, u8 cmd)
1021{
1022 memset(rq, 0, sizeof(*rq));
Jens Axboe4aff5e22006-08-10 08:44:47 +02001023 rq->cmd_type = REQ_TYPE_SPECIAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024 rq->cmd[0] = cmd;
1025}
1026
1027/*
1028 * idetape_queue_pc_head generates a new packet command request in front
1029 * of the request queue, before the current request, so that it will be
1030 * processed immediately, on the next pass through the driver.
1031 *
1032 * idetape_queue_pc_head is called from the request handling part of
1033 * the driver (the "bottom" part). Safe storage for the request should
1034 * be allocated with idetape_next_pc_storage and idetape_next_rq_storage
1035 * before calling idetape_queue_pc_head.
1036 *
1037 * Memory for those requests is pre-allocated at initialization time, and
1038 * is limited to IDETAPE_PC_STACK requests. We assume that we have enough
1039 * space for the maximum possible number of inter-dependent packet commands.
1040 *
1041 * The higher level of the driver - The ioctl handler and the character
1042 * device handling functions should queue request to the lower level part
1043 * and wait for their completion using idetape_queue_pc_tail or
1044 * idetape_queue_rw_tail.
1045 */
1046static void idetape_queue_pc_head (ide_drive_t *drive, idetape_pc_t *pc,struct request *rq)
1047{
1048 struct ide_tape_obj *tape = drive->driver_data;
1049
1050 idetape_init_rq(rq, REQ_IDETAPE_PC1);
1051 rq->buffer = (char *) pc;
1052 rq->rq_disk = tape->disk;
1053 (void) ide_do_drive_cmd(drive, rq, ide_preempt);
1054}
1055
1056/*
1057 * idetape_retry_pc is called when an error was detected during the
1058 * last packet command. We queue a request sense packet command in
1059 * the head of the request list.
1060 */
1061static ide_startstop_t idetape_retry_pc (ide_drive_t *drive)
1062{
1063 idetape_tape_t *tape = drive->driver_data;
1064 idetape_pc_t *pc;
1065 struct request *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066
Bartlomiej Zolnierkiewicz64a57fe2008-02-06 02:57:51 +01001067 (void)ide_read_error(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068 pc = idetape_next_pc_storage(drive);
1069 rq = idetape_next_rq_storage(drive);
1070 idetape_create_request_sense_cmd(pc);
1071 set_bit(IDETAPE_IGNORE_DSC, &tape->flags);
1072 idetape_queue_pc_head(drive, pc, rq);
1073 return ide_stopped;
1074}
1075
1076/*
1077 * idetape_postpone_request postpones the current request so that
1078 * ide.c will be able to service requests from another device on
1079 * the same hwgroup while we are polling for DSC.
1080 */
1081static void idetape_postpone_request (ide_drive_t *drive)
1082{
1083 idetape_tape_t *tape = drive->driver_data;
1084
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001085 debug_log(DBG_PROCS, "Enter %s\n", __func__);
1086
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087 tape->postponed_rq = HWGROUP(drive)->rq;
Borislav Petkov54bb2072008-02-06 02:57:52 +01001088 ide_stall_queue(drive, tape->dsc_poll_freq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089}
1090
Borislav Petkova1efc852008-02-06 02:57:52 +01001091typedef void idetape_io_buf(ide_drive_t *, idetape_pc_t *, unsigned int);
1092
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093/*
Borislav Petkova1efc852008-02-06 02:57:52 +01001094 * This is the usual interrupt handler which will be called during a packet
1095 * command. We will transfer some of the data (as requested by the drive) and
1096 * will re-point interrupt handler to us. When data transfer is finished, we
1097 * will act according to the algorithm described before
1098 * idetape_issue_packet_command.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099 */
Borislav Petkova1efc852008-02-06 02:57:52 +01001100static ide_startstop_t idetape_pc_intr(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101{
1102 ide_hwif_t *hwif = drive->hwif;
1103 idetape_tape_t *tape = drive->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104 idetape_pc_t *pc = tape->pc;
Borislav Petkova1efc852008-02-06 02:57:52 +01001105 xfer_func_t *xferfunc;
1106 idetape_io_buf *iobuf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107 unsigned int temp;
1108#if SIMULATE_ERRORS
1109 static int error_sim_count = 0;
1110#endif
Bartlomiej Zolnierkiewicz790d1232008-01-25 22:17:12 +01001111 u16 bcount;
Bartlomiej Zolnierkiewicz8e7657a2008-01-25 22:17:12 +01001112 u8 stat, ireason;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001114 debug_log(DBG_PROCS, "Enter %s - interrupt handler\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115
1116 /* Clear the interrupt */
Bartlomiej Zolnierkiewiczc47137a2008-02-06 02:57:51 +01001117 stat = ide_read_status(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118
1119 if (test_bit(PC_DMA_IN_PROGRESS, &pc->flags)) {
Bartlomiej Zolnierkiewicz22c525b2008-01-25 22:17:11 +01001120 if (hwif->ide_dma_end(drive) || (stat & ERR_STAT)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121 /*
1122 * A DMA error is sometimes expected. For example,
1123 * if the tape is crossing a filemark during a
1124 * READ command, it will issue an irq and position
1125 * itself before the filemark, so that only a partial
1126 * data transfer will occur (which causes the DMA
1127 * error). In that case, we will later ask the tape
1128 * how much bytes of the original request were
1129 * actually transferred (we can't receive that
1130 * information from the DMA engine on most chipsets).
1131 */
1132
1133 /*
1134 * On the contrary, a DMA error is never expected;
1135 * it usually indicates a hardware error or abort.
1136 * If the tape crosses a filemark during a READ
1137 * command, it will issue an irq and position itself
1138 * after the filemark (not before). Only a partial
1139 * data transfer will occur, but no DMA error.
1140 * (AS, 19 Apr 2001)
1141 */
1142 set_bit(PC_DMA_ERROR, &pc->flags);
1143 } else {
1144 pc->actually_transferred = pc->request_transfer;
1145 idetape_update_buffers(pc);
1146 }
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001147 debug_log(DBG_PROCS, "DMA finished\n");
1148
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149 }
1150
1151 /* No more interrupts */
Bartlomiej Zolnierkiewicz22c525b2008-01-25 22:17:11 +01001152 if ((stat & DRQ_STAT) == 0) {
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001153 debug_log(DBG_SENSE, "Packet command completed, %d bytes"
1154 " transferred\n", pc->actually_transferred);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001156 clear_bit(PC_DMA_IN_PROGRESS, &pc->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157 local_irq_enable();
1158
1159#if SIMULATE_ERRORS
Borislav Petkov90699ce2008-02-02 19:56:50 +01001160 if ((pc->c[0] == WRITE_6 || pc->c[0] == READ_6) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161 (++error_sim_count % 100) == 0) {
1162 printk(KERN_INFO "ide-tape: %s: simulating error\n",
1163 tape->name);
Bartlomiej Zolnierkiewicz22c525b2008-01-25 22:17:11 +01001164 stat |= ERR_STAT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165 }
1166#endif
Borislav Petkov90699ce2008-02-02 19:56:50 +01001167 if ((stat & ERR_STAT) && pc->c[0] == REQUEST_SENSE)
Bartlomiej Zolnierkiewicz22c525b2008-01-25 22:17:11 +01001168 stat &= ~ERR_STAT;
1169 if ((stat & ERR_STAT) || test_bit(PC_DMA_ERROR, &pc->flags)) {
1170 /* Error detected */
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001171 debug_log(DBG_ERR, "%s: I/O error\n", tape->name);
1172
Borislav Petkov90699ce2008-02-02 19:56:50 +01001173 if (pc->c[0] == REQUEST_SENSE) {
Borislav Petkova1efc852008-02-06 02:57:52 +01001174 printk(KERN_ERR "ide-tape: I/O error in request"
1175 " sense command\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176 return ide_do_reset(drive);
1177 }
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001178 debug_log(DBG_ERR, "[cmd %x]: check condition\n",
1179 pc->c[0]);
1180
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181 /* Retry operation */
1182 return idetape_retry_pc(drive);
1183 }
1184 pc->error = 0;
1185 if (test_bit(PC_WAIT_FOR_DSC, &pc->flags) &&
Bartlomiej Zolnierkiewicz22c525b2008-01-25 22:17:11 +01001186 (stat & SEEK_STAT) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187 /* Media access command */
1188 tape->dsc_polling_start = jiffies;
Borislav Petkov54bb2072008-02-06 02:57:52 +01001189 tape->dsc_poll_freq = IDETAPE_DSC_MA_FAST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190 tape->dsc_timeout = jiffies + IDETAPE_DSC_MA_TIMEOUT;
1191 /* Allow ide.c to handle other requests */
1192 idetape_postpone_request(drive);
1193 return ide_stopped;
1194 }
1195 if (tape->failed_pc == pc)
1196 tape->failed_pc = NULL;
1197 /* Command finished - Call the callback function */
1198 return pc->callback(drive);
1199 }
1200 if (test_and_clear_bit(PC_DMA_IN_PROGRESS, &pc->flags)) {
1201 printk(KERN_ERR "ide-tape: The tape wants to issue more "
1202 "interrupts in DMA mode\n");
1203 printk(KERN_ERR "ide-tape: DMA disabled, reverting to PIO\n");
Bartlomiej Zolnierkiewicz7469aaf2007-02-17 02:40:26 +01001204 ide_dma_off(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205 return ide_do_reset(drive);
1206 }
1207 /* Get the number of bytes to transfer on this interrupt. */
Bartlomiej Zolnierkiewicz790d1232008-01-25 22:17:12 +01001208 bcount = (hwif->INB(IDE_BCOUNTH_REG) << 8) |
1209 hwif->INB(IDE_BCOUNTL_REG);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210
Bartlomiej Zolnierkiewicz8e7657a2008-01-25 22:17:12 +01001211 ireason = hwif->INB(IDE_IREASON_REG);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212
Bartlomiej Zolnierkiewicz8e7657a2008-01-25 22:17:12 +01001213 if (ireason & CD) {
Borislav Petkova1efc852008-02-06 02:57:52 +01001214 printk(KERN_ERR "ide-tape: CoD != 0 in %s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215 return ide_do_reset(drive);
1216 }
Bartlomiej Zolnierkiewicz8e7657a2008-01-25 22:17:12 +01001217 if (((ireason & IO) == IO) == test_bit(PC_WRITING, &pc->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218 /* Hopefully, we will never get here */
1219 printk(KERN_ERR "ide-tape: We wanted to %s, ",
Bartlomiej Zolnierkiewicz8e7657a2008-01-25 22:17:12 +01001220 (ireason & IO) ? "Write" : "Read");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221 printk(KERN_ERR "ide-tape: but the tape wants us to %s !\n",
Bartlomiej Zolnierkiewicz8e7657a2008-01-25 22:17:12 +01001222 (ireason & IO) ? "Read" : "Write");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223 return ide_do_reset(drive);
1224 }
1225 if (!test_bit(PC_WRITING, &pc->flags)) {
1226 /* Reading - Check that we have enough space */
Bartlomiej Zolnierkiewicz790d1232008-01-25 22:17:12 +01001227 temp = pc->actually_transferred + bcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228 if (temp > pc->request_transfer) {
1229 if (temp > pc->buffer_size) {
Borislav Petkova1efc852008-02-06 02:57:52 +01001230 printk(KERN_ERR "ide-tape: The tape wants to "
1231 "send us more data than expected "
1232 "- discarding data\n");
Bartlomiej Zolnierkiewicz790d1232008-01-25 22:17:12 +01001233 idetape_discard_data(drive, bcount);
Borislav Petkova1efc852008-02-06 02:57:52 +01001234 ide_set_handler(drive, &idetape_pc_intr,
1235 IDETAPE_WAIT_CMD, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236 return ide_started;
1237 }
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001238 debug_log(DBG_SENSE, "The tape wants to send us more "
1239 "data than expected - allowing transfer\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240 }
Borislav Petkova1efc852008-02-06 02:57:52 +01001241 iobuf = &idetape_input_buffers;
1242 xferfunc = hwif->atapi_input_bytes;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243 } else {
Borislav Petkova1efc852008-02-06 02:57:52 +01001244 iobuf = &idetape_output_buffers;
1245 xferfunc = hwif->atapi_output_bytes;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246 }
Borislav Petkova1efc852008-02-06 02:57:52 +01001247
1248 if (pc->bh)
1249 iobuf(drive, pc, bcount);
1250 else
1251 xferfunc(drive, pc->current_position, bcount);
1252
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253 /* Update the current position */
Bartlomiej Zolnierkiewicz790d1232008-01-25 22:17:12 +01001254 pc->actually_transferred += bcount;
1255 pc->current_position += bcount;
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001256
1257 debug_log(DBG_SENSE, "[cmd %x] transferred %d bytes on that intr.\n",
1258 pc->c[0], bcount);
1259
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260 /* And set the interrupt handler again */
1261 ide_set_handler(drive, &idetape_pc_intr, IDETAPE_WAIT_CMD, NULL);
1262 return ide_started;
1263}
1264
1265/*
1266 * Packet Command Interface
1267 *
1268 * The current Packet Command is available in tape->pc, and will not
1269 * change until we finish handling it. Each packet command is associated
1270 * with a callback function that will be called when the command is
1271 * finished.
1272 *
1273 * The handling will be done in three stages:
1274 *
1275 * 1. idetape_issue_packet_command will send the packet command to the
1276 * drive, and will set the interrupt handler to idetape_pc_intr.
1277 *
1278 * 2. On each interrupt, idetape_pc_intr will be called. This step
1279 * will be repeated until the device signals us that no more
1280 * interrupts will be issued.
1281 *
1282 * 3. ATAPI Tape media access commands have immediate status with a
1283 * delayed process. In case of a successful initiation of a
1284 * media access packet command, the DSC bit will be set when the
1285 * actual execution of the command is finished.
1286 * Since the tape drive will not issue an interrupt, we have to
1287 * poll for this event. In this case, we define the request as
1288 * "low priority request" by setting rq_status to
1289 * IDETAPE_RQ_POSTPONED, set a timer to poll for DSC and exit
1290 * the driver.
1291 *
1292 * ide.c will then give higher priority to requests which
1293 * originate from the other device, until will change rq_status
1294 * to RQ_ACTIVE.
1295 *
1296 * 4. When the packet command is finished, it will be checked for errors.
1297 *
1298 * 5. In case an error was found, we queue a request sense packet
1299 * command in front of the request queue and retry the operation
1300 * up to IDETAPE_MAX_PC_RETRIES times.
1301 *
1302 * 6. In case no error was found, or we decided to give up and not
1303 * to retry again, the callback function will be called and then
1304 * we will handle the next request.
1305 *
1306 */
1307static ide_startstop_t idetape_transfer_pc(ide_drive_t *drive)
1308{
1309 ide_hwif_t *hwif = drive->hwif;
1310 idetape_tape_t *tape = drive->driver_data;
1311 idetape_pc_t *pc = tape->pc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312 int retries = 100;
1313 ide_startstop_t startstop;
Bartlomiej Zolnierkiewicz8e7657a2008-01-25 22:17:12 +01001314 u8 ireason;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315
1316 if (ide_wait_stat(&startstop,drive,DRQ_STAT,BUSY_STAT,WAIT_READY)) {
1317 printk(KERN_ERR "ide-tape: Strange, packet command initiated yet DRQ isn't asserted\n");
1318 return startstop;
1319 }
Bartlomiej Zolnierkiewicz8e7657a2008-01-25 22:17:12 +01001320 ireason = hwif->INB(IDE_IREASON_REG);
1321 while (retries-- && ((ireason & CD) == 0 || (ireason & IO))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322 printk(KERN_ERR "ide-tape: (IO,CoD != (0,1) while issuing "
1323 "a packet command, retrying\n");
1324 udelay(100);
Bartlomiej Zolnierkiewicz8e7657a2008-01-25 22:17:12 +01001325 ireason = hwif->INB(IDE_IREASON_REG);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326 if (retries == 0) {
1327 printk(KERN_ERR "ide-tape: (IO,CoD != (0,1) while "
1328 "issuing a packet command, ignoring\n");
Bartlomiej Zolnierkiewicz8e7657a2008-01-25 22:17:12 +01001329 ireason |= CD;
1330 ireason &= ~IO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331 }
1332 }
Bartlomiej Zolnierkiewicz8e7657a2008-01-25 22:17:12 +01001333 if ((ireason & CD) == 0 || (ireason & IO)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334 printk(KERN_ERR "ide-tape: (IO,CoD) != (0,1) while issuing "
1335 "a packet command\n");
1336 return ide_do_reset(drive);
1337 }
1338 /* Set the interrupt routine */
1339 ide_set_handler(drive, &idetape_pc_intr, IDETAPE_WAIT_CMD, NULL);
1340#ifdef CONFIG_BLK_DEV_IDEDMA
1341 /* Begin DMA, if necessary */
1342 if (test_bit(PC_DMA_IN_PROGRESS, &pc->flags))
1343 hwif->dma_start(drive);
1344#endif
1345 /* Send the actual packet */
1346 HWIF(drive)->atapi_output_bytes(drive, pc->c, 12);
1347 return ide_started;
1348}
1349
1350static ide_startstop_t idetape_issue_packet_command (ide_drive_t *drive, idetape_pc_t *pc)
1351{
1352 ide_hwif_t *hwif = drive->hwif;
1353 idetape_tape_t *tape = drive->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354 int dma_ok = 0;
Bartlomiej Zolnierkiewicz790d1232008-01-25 22:17:12 +01001355 u16 bcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356
Borislav Petkov90699ce2008-02-02 19:56:50 +01001357 if (tape->pc->c[0] == REQUEST_SENSE &&
1358 pc->c[0] == REQUEST_SENSE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359 printk(KERN_ERR "ide-tape: possible ide-tape.c bug - "
1360 "Two request sense in serial were issued\n");
1361 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362
Borislav Petkov90699ce2008-02-02 19:56:50 +01001363 if (tape->failed_pc == NULL && pc->c[0] != REQUEST_SENSE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364 tape->failed_pc = pc;
1365 /* Set the current packet command */
1366 tape->pc = pc;
1367
1368 if (pc->retries > IDETAPE_MAX_PC_RETRIES ||
1369 test_bit(PC_ABORT, &pc->flags)) {
1370 /*
1371 * We will "abort" retrying a packet command in case
1372 * a legitimate error code was received (crossing a
1373 * filemark, or end of the media, for example).
1374 */
1375 if (!test_bit(PC_ABORT, &pc->flags)) {
Borislav Petkov90699ce2008-02-02 19:56:50 +01001376 if (!(pc->c[0] == TEST_UNIT_READY &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377 tape->sense_key == 2 && tape->asc == 4 &&
1378 (tape->ascq == 1 || tape->ascq == 8))) {
1379 printk(KERN_ERR "ide-tape: %s: I/O error, "
1380 "pc = %2x, key = %2x, "
1381 "asc = %2x, ascq = %2x\n",
1382 tape->name, pc->c[0],
1383 tape->sense_key, tape->asc,
1384 tape->ascq);
1385 }
1386 /* Giving up */
1387 pc->error = IDETAPE_ERROR_GENERAL;
1388 }
1389 tape->failed_pc = NULL;
1390 return pc->callback(drive);
1391 }
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001392 debug_log(DBG_SENSE, "Retry #%d, cmd = %02X\n", pc->retries, pc->c[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393
1394 pc->retries++;
1395 /* We haven't transferred any data yet */
1396 pc->actually_transferred = 0;
1397 pc->current_position = pc->buffer;
1398 /* Request to transfer the entire buffer at once */
Bartlomiej Zolnierkiewicz790d1232008-01-25 22:17:12 +01001399 bcount = pc->request_transfer;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400
1401 if (test_and_clear_bit(PC_DMA_ERROR, &pc->flags)) {
1402 printk(KERN_WARNING "ide-tape: DMA disabled, "
1403 "reverting to PIO\n");
Bartlomiej Zolnierkiewicz7469aaf2007-02-17 02:40:26 +01001404 ide_dma_off(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405 }
1406 if (test_bit(PC_DMA_RECOMMENDED, &pc->flags) && drive->using_dma)
1407 dma_ok = !hwif->dma_setup(drive);
1408
Bartlomiej Zolnierkiewicz2fc57382008-01-25 22:17:13 +01001409 ide_pktcmd_tf_load(drive, IDE_TFLAG_NO_SELECT_MASK |
1410 IDE_TFLAG_OUT_DEVICE, bcount, dma_ok);
1411
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412 if (dma_ok) /* Will begin DMA later */
1413 set_bit(PC_DMA_IN_PROGRESS, &pc->flags);
1414 if (test_bit(IDETAPE_DRQ_INTERRUPT, &tape->flags)) {
Bartlomiej Zolnierkiewiczc1c9dbc2008-02-02 19:56:44 +01001415 ide_execute_command(drive, WIN_PACKETCMD, &idetape_transfer_pc,
1416 IDETAPE_WAIT_CMD, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417 return ide_started;
1418 } else {
1419 hwif->OUTB(WIN_PACKETCMD, IDE_COMMAND_REG);
1420 return idetape_transfer_pc(drive);
1421 }
1422}
1423
1424/*
1425 * General packet command callback function.
1426 */
1427static ide_startstop_t idetape_pc_callback (ide_drive_t *drive)
1428{
1429 idetape_tape_t *tape = drive->driver_data;
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001430
1431 debug_log(DBG_PROCS, "Enter %s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432
1433 idetape_end_request(drive, tape->pc->error ? 0 : 1, 0);
1434 return ide_stopped;
1435}
1436
1437/*
1438 * A mode sense command is used to "sense" tape parameters.
1439 */
1440static void idetape_create_mode_sense_cmd (idetape_pc_t *pc, u8 page_code)
1441{
1442 idetape_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +01001443 pc->c[0] = MODE_SENSE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444 if (page_code != IDETAPE_BLOCK_DESCRIPTOR)
1445 pc->c[1] = 8; /* DBD = 1 - Don't return block descriptors */
1446 pc->c[2] = page_code;
1447 /*
1448 * Changed pc->c[3] to 0 (255 will at best return unused info).
1449 *
1450 * For SCSI this byte is defined as subpage instead of high byte
1451 * of length and some IDE drives seem to interpret it this way
1452 * and return an error when 255 is used.
1453 */
1454 pc->c[3] = 0;
1455 pc->c[4] = 255; /* (We will just discard data in that case) */
1456 if (page_code == IDETAPE_BLOCK_DESCRIPTOR)
1457 pc->request_transfer = 12;
1458 else if (page_code == IDETAPE_CAPABILITIES_PAGE)
1459 pc->request_transfer = 24;
1460 else
1461 pc->request_transfer = 50;
1462 pc->callback = &idetape_pc_callback;
1463}
1464
Borislav Petkov37016ba2008-02-06 02:57:52 +01001465static void idetape_calculate_speeds(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466{
1467 idetape_tape_t *tape = drive->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468
1469 if (time_after(jiffies, tape->controlled_pipeline_head_time + 120 * HZ)) {
1470 tape->controlled_previous_pipeline_head = tape->controlled_last_pipeline_head;
1471 tape->controlled_previous_head_time = tape->controlled_pipeline_head_time;
1472 tape->controlled_last_pipeline_head = tape->pipeline_head;
1473 tape->controlled_pipeline_head_time = jiffies;
1474 }
1475 if (time_after(jiffies, tape->controlled_pipeline_head_time + 60 * HZ))
1476 tape->controlled_pipeline_head_speed = (tape->pipeline_head - tape->controlled_last_pipeline_head) * 32 * HZ / (jiffies - tape->controlled_pipeline_head_time);
1477 else if (time_after(jiffies, tape->controlled_previous_head_time))
1478 tape->controlled_pipeline_head_speed = (tape->pipeline_head - tape->controlled_previous_pipeline_head) * 32 * HZ / (jiffies - tape->controlled_previous_head_time);
1479
1480 if (tape->nr_pending_stages < tape->max_stages /*- 1 */) {
1481 /* -1 for read mode error recovery */
1482 if (time_after(jiffies, tape->uncontrolled_previous_head_time + 10 * HZ)) {
1483 tape->uncontrolled_pipeline_head_time = jiffies;
1484 tape->uncontrolled_pipeline_head_speed = (tape->pipeline_head - tape->uncontrolled_previous_pipeline_head) * 32 * HZ / (jiffies - tape->uncontrolled_previous_head_time);
1485 }
1486 } else {
1487 tape->uncontrolled_previous_head_time = jiffies;
1488 tape->uncontrolled_previous_pipeline_head = tape->pipeline_head;
1489 if (time_after(jiffies, tape->uncontrolled_pipeline_head_time + 30 * HZ)) {
1490 tape->uncontrolled_pipeline_head_time = jiffies;
1491 }
1492 }
1493 tape->pipeline_head_speed = max(tape->uncontrolled_pipeline_head_speed, tape->controlled_pipeline_head_speed);
Borislav Petkov37016ba2008-02-06 02:57:52 +01001494
1495 if (tape->speed_control == 1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496 if (tape->nr_pending_stages >= tape->max_stages / 2)
1497 tape->max_insert_speed = tape->pipeline_head_speed +
1498 (1100 - tape->pipeline_head_speed) * 2 * (tape->nr_pending_stages - tape->max_stages / 2) / tape->max_stages;
1499 else
1500 tape->max_insert_speed = 500 +
1501 (tape->pipeline_head_speed - 500) * 2 * tape->nr_pending_stages / tape->max_stages;
Borislav Petkov37016ba2008-02-06 02:57:52 +01001502
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503 if (tape->nr_pending_stages >= tape->max_stages * 99 / 100)
1504 tape->max_insert_speed = 5000;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505 } else
1506 tape->max_insert_speed = tape->speed_control;
Borislav Petkov37016ba2008-02-06 02:57:52 +01001507
Linus Torvalds1da177e2005-04-16 15:20:36 -07001508 tape->max_insert_speed = max(tape->max_insert_speed, 500);
1509}
1510
1511static ide_startstop_t idetape_media_access_finished (ide_drive_t *drive)
1512{
1513 idetape_tape_t *tape = drive->driver_data;
1514 idetape_pc_t *pc = tape->pc;
Bartlomiej Zolnierkiewicz22c525b2008-01-25 22:17:11 +01001515 u8 stat;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001516
Bartlomiej Zolnierkiewiczc47137a2008-02-06 02:57:51 +01001517 stat = ide_read_status(drive);
1518
Bartlomiej Zolnierkiewicz22c525b2008-01-25 22:17:11 +01001519 if (stat & SEEK_STAT) {
1520 if (stat & ERR_STAT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521 /* Error detected */
Borislav Petkov90699ce2008-02-02 19:56:50 +01001522 if (pc->c[0] != TEST_UNIT_READY)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523 printk(KERN_ERR "ide-tape: %s: I/O error, ",
1524 tape->name);
1525 /* Retry operation */
1526 return idetape_retry_pc(drive);
1527 }
1528 pc->error = 0;
1529 if (tape->failed_pc == pc)
1530 tape->failed_pc = NULL;
1531 } else {
1532 pc->error = IDETAPE_ERROR_GENERAL;
1533 tape->failed_pc = NULL;
1534 }
1535 return pc->callback(drive);
1536}
1537
1538static ide_startstop_t idetape_rw_callback (ide_drive_t *drive)
1539{
1540 idetape_tape_t *tape = drive->driver_data;
1541 struct request *rq = HWGROUP(drive)->rq;
Borislav Petkov54bb2072008-02-06 02:57:52 +01001542 int blocks = tape->pc->actually_transferred / tape->blk_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001543
Borislav Petkov54bb2072008-02-06 02:57:52 +01001544 tape->avg_size += blocks * tape->blk_size;
1545 tape->insert_size += blocks * tape->blk_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001546 if (tape->insert_size > 1024 * 1024)
1547 tape->measure_insert_time = 1;
1548 if (tape->measure_insert_time) {
1549 tape->measure_insert_time = 0;
1550 tape->insert_time = jiffies;
1551 tape->insert_size = 0;
1552 }
1553 if (time_after(jiffies, tape->insert_time))
1554 tape->insert_speed = tape->insert_size / 1024 * HZ / (jiffies - tape->insert_time);
Marcelo Feitoza Parisi9bae1ff2006-03-28 01:56:46 -08001555 if (time_after_eq(jiffies, tape->avg_time + HZ)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556 tape->avg_speed = tape->avg_size * HZ / (jiffies - tape->avg_time) / 1024;
1557 tape->avg_size = 0;
1558 tape->avg_time = jiffies;
1559 }
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001560 debug_log(DBG_PROCS, "Enter %s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001561
Borislav Petkov54bb2072008-02-06 02:57:52 +01001562 tape->first_frame += blocks;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563 rq->current_nr_sectors -= blocks;
1564
1565 if (!tape->pc->error)
1566 idetape_end_request(drive, 1, 0);
1567 else
1568 idetape_end_request(drive, tape->pc->error, 0);
1569 return ide_stopped;
1570}
1571
1572static void idetape_create_read_cmd(idetape_tape_t *tape, idetape_pc_t *pc, unsigned int length, struct idetape_bh *bh)
1573{
1574 idetape_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +01001575 pc->c[0] = READ_6;
Borislav Petkov860ff5e2008-02-02 19:56:50 +01001576 put_unaligned(cpu_to_be32(length), (unsigned int *) &pc->c[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001577 pc->c[1] = 1;
1578 pc->callback = &idetape_rw_callback;
1579 pc->bh = bh;
1580 atomic_set(&bh->b_count, 0);
1581 pc->buffer = NULL;
Borislav Petkov54bb2072008-02-06 02:57:52 +01001582 pc->buffer_size = length * tape->blk_size;
1583 pc->request_transfer = pc->buffer_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584 if (pc->request_transfer == tape->stage_size)
1585 set_bit(PC_DMA_RECOMMENDED, &pc->flags);
1586}
1587
1588static void idetape_create_read_buffer_cmd(idetape_tape_t *tape, idetape_pc_t *pc, unsigned int length, struct idetape_bh *bh)
1589{
1590 int size = 32768;
1591 struct idetape_bh *p = bh;
1592
1593 idetape_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +01001594 pc->c[0] = READ_BUFFER;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001595 pc->c[1] = IDETAPE_RETRIEVE_FAULTY_BLOCK;
1596 pc->c[7] = size >> 8;
1597 pc->c[8] = size & 0xff;
1598 pc->callback = &idetape_pc_callback;
1599 pc->bh = bh;
1600 atomic_set(&bh->b_count, 0);
1601 pc->buffer = NULL;
1602 while (p) {
1603 atomic_set(&p->b_count, 0);
1604 p = p->b_reqnext;
1605 }
1606 pc->request_transfer = pc->buffer_size = size;
1607}
1608
1609static void idetape_create_write_cmd(idetape_tape_t *tape, idetape_pc_t *pc, unsigned int length, struct idetape_bh *bh)
1610{
1611 idetape_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +01001612 pc->c[0] = WRITE_6;
Borislav Petkov860ff5e2008-02-02 19:56:50 +01001613 put_unaligned(cpu_to_be32(length), (unsigned int *) &pc->c[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001614 pc->c[1] = 1;
1615 pc->callback = &idetape_rw_callback;
1616 set_bit(PC_WRITING, &pc->flags);
1617 pc->bh = bh;
1618 pc->b_data = bh->b_data;
1619 pc->b_count = atomic_read(&bh->b_count);
1620 pc->buffer = NULL;
Borislav Petkov54bb2072008-02-06 02:57:52 +01001621 pc->buffer_size = length * tape->blk_size;
1622 pc->request_transfer = pc->buffer_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623 if (pc->request_transfer == tape->stage_size)
1624 set_bit(PC_DMA_RECOMMENDED, &pc->flags);
1625}
1626
1627/*
1628 * idetape_do_request is our request handling function.
1629 */
1630static ide_startstop_t idetape_do_request(ide_drive_t *drive,
1631 struct request *rq, sector_t block)
1632{
1633 idetape_tape_t *tape = drive->driver_data;
1634 idetape_pc_t *pc = NULL;
1635 struct request *postponed_rq = tape->postponed_rq;
Bartlomiej Zolnierkiewicz22c525b2008-01-25 22:17:11 +01001636 u8 stat;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001637
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001638 debug_log(DBG_SENSE, "sector: %ld, nr_sectors: %ld,"
1639 " current_nr_sectors: %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001640 rq->sector, rq->nr_sectors, rq->current_nr_sectors);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641
Jens Axboe4aff5e22006-08-10 08:44:47 +02001642 if (!blk_special_request(rq)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643 /*
1644 * We do not support buffer cache originated requests.
1645 */
1646 printk(KERN_NOTICE "ide-tape: %s: Unsupported request in "
Jens Axboe4aff5e22006-08-10 08:44:47 +02001647 "request queue (%d)\n", drive->name, rq->cmd_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001648 ide_end_request(drive, 0, 0);
1649 return ide_stopped;
1650 }
1651
1652 /*
1653 * Retry a failed packet command
1654 */
1655 if (tape->failed_pc != NULL &&
Borislav Petkov90699ce2008-02-02 19:56:50 +01001656 tape->pc->c[0] == REQUEST_SENSE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001657 return idetape_issue_packet_command(drive, tape->failed_pc);
1658 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001659 if (postponed_rq != NULL)
1660 if (rq != postponed_rq) {
1661 printk(KERN_ERR "ide-tape: ide-tape.c bug - "
1662 "Two DSC requests were queued\n");
1663 idetape_end_request(drive, 0, 0);
1664 return ide_stopped;
1665 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001666
1667 tape->postponed_rq = NULL;
1668
1669 /*
1670 * If the tape is still busy, postpone our request and service
1671 * the other device meanwhile.
1672 */
Bartlomiej Zolnierkiewiczc47137a2008-02-06 02:57:51 +01001673 stat = ide_read_status(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001674
1675 if (!drive->dsc_overlap && !(rq->cmd[0] & REQ_IDETAPE_PC2))
1676 set_bit(IDETAPE_IGNORE_DSC, &tape->flags);
1677
1678 if (drive->post_reset == 1) {
1679 set_bit(IDETAPE_IGNORE_DSC, &tape->flags);
1680 drive->post_reset = 0;
1681 }
1682
Linus Torvalds1da177e2005-04-16 15:20:36 -07001683 if (time_after(jiffies, tape->insert_time))
1684 tape->insert_speed = tape->insert_size / 1024 * HZ / (jiffies - tape->insert_time);
Borislav Petkov37016ba2008-02-06 02:57:52 +01001685 idetape_calculate_speeds(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001686 if (!test_and_clear_bit(IDETAPE_IGNORE_DSC, &tape->flags) &&
Bartlomiej Zolnierkiewicz22c525b2008-01-25 22:17:11 +01001687 (stat & SEEK_STAT) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001688 if (postponed_rq == NULL) {
1689 tape->dsc_polling_start = jiffies;
Borislav Petkov54bb2072008-02-06 02:57:52 +01001690 tape->dsc_poll_freq = tape->best_dsc_rw_freq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001691 tape->dsc_timeout = jiffies + IDETAPE_DSC_RW_TIMEOUT;
1692 } else if (time_after(jiffies, tape->dsc_timeout)) {
1693 printk(KERN_ERR "ide-tape: %s: DSC timeout\n",
1694 tape->name);
1695 if (rq->cmd[0] & REQ_IDETAPE_PC2) {
1696 idetape_media_access_finished(drive);
1697 return ide_stopped;
1698 } else {
1699 return ide_do_reset(drive);
1700 }
Marcelo Feitoza Parisi9bae1ff2006-03-28 01:56:46 -08001701 } else if (time_after(jiffies, tape->dsc_polling_start + IDETAPE_DSC_MA_THRESHOLD))
Borislav Petkov54bb2072008-02-06 02:57:52 +01001702 tape->dsc_poll_freq = IDETAPE_DSC_MA_SLOW;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001703 idetape_postpone_request(drive);
1704 return ide_stopped;
1705 }
1706 if (rq->cmd[0] & REQ_IDETAPE_READ) {
1707 tape->buffer_head++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001708 tape->postpone_cnt = 0;
1709 pc = idetape_next_pc_storage(drive);
1710 idetape_create_read_cmd(tape, pc, rq->current_nr_sectors, (struct idetape_bh *)rq->special);
1711 goto out;
1712 }
1713 if (rq->cmd[0] & REQ_IDETAPE_WRITE) {
1714 tape->buffer_head++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001715 tape->postpone_cnt = 0;
1716 pc = idetape_next_pc_storage(drive);
1717 idetape_create_write_cmd(tape, pc, rq->current_nr_sectors, (struct idetape_bh *)rq->special);
1718 goto out;
1719 }
1720 if (rq->cmd[0] & REQ_IDETAPE_READ_BUFFER) {
1721 tape->postpone_cnt = 0;
1722 pc = idetape_next_pc_storage(drive);
1723 idetape_create_read_buffer_cmd(tape, pc, rq->current_nr_sectors, (struct idetape_bh *)rq->special);
1724 goto out;
1725 }
1726 if (rq->cmd[0] & REQ_IDETAPE_PC1) {
1727 pc = (idetape_pc_t *) rq->buffer;
1728 rq->cmd[0] &= ~(REQ_IDETAPE_PC1);
1729 rq->cmd[0] |= REQ_IDETAPE_PC2;
1730 goto out;
1731 }
1732 if (rq->cmd[0] & REQ_IDETAPE_PC2) {
1733 idetape_media_access_finished(drive);
1734 return ide_stopped;
1735 }
1736 BUG();
1737out:
1738 return idetape_issue_packet_command(drive, pc);
1739}
1740
1741/*
1742 * Pipeline related functions
1743 */
1744static inline int idetape_pipeline_active (idetape_tape_t *tape)
1745{
1746 int rc1, rc2;
1747
1748 rc1 = test_bit(IDETAPE_PIPELINE_ACTIVE, &tape->flags);
Borislav Petkov54bb2072008-02-06 02:57:52 +01001749 rc2 = (tape->active_data_rq != NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001750 return rc1;
1751}
1752
1753/*
1754 * idetape_kmalloc_stage uses __get_free_page to allocate a pipeline
1755 * stage, along with all the necessary small buffers which together make
1756 * a buffer of size tape->stage_size (or a bit more). We attempt to
1757 * combine sequential pages as much as possible.
1758 *
1759 * Returns a pointer to the new allocated stage, or NULL if we
1760 * can't (or don't want to) allocate a stage.
1761 *
1762 * Pipeline stages are optional and are used to increase performance.
1763 * If we can't allocate them, we'll manage without them.
1764 */
1765static idetape_stage_t *__idetape_kmalloc_stage (idetape_tape_t *tape, int full, int clear)
1766{
1767 idetape_stage_t *stage;
1768 struct idetape_bh *prev_bh, *bh;
1769 int pages = tape->pages_per_stage;
1770 char *b_data = NULL;
1771
Robert P. J. Day5cbded52006-12-13 00:35:56 -08001772 if ((stage = kmalloc(sizeof (idetape_stage_t),GFP_KERNEL)) == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001773 return NULL;
1774 stage->next = NULL;
1775
Robert P. J. Day5cbded52006-12-13 00:35:56 -08001776 bh = stage->bh = kmalloc(sizeof(struct idetape_bh), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001777 if (bh == NULL)
1778 goto abort;
1779 bh->b_reqnext = NULL;
1780 if ((bh->b_data = (char *) __get_free_page (GFP_KERNEL)) == NULL)
1781 goto abort;
1782 if (clear)
1783 memset(bh->b_data, 0, PAGE_SIZE);
1784 bh->b_size = PAGE_SIZE;
1785 atomic_set(&bh->b_count, full ? bh->b_size : 0);
1786
1787 while (--pages) {
1788 if ((b_data = (char *) __get_free_page (GFP_KERNEL)) == NULL)
1789 goto abort;
1790 if (clear)
1791 memset(b_data, 0, PAGE_SIZE);
1792 if (bh->b_data == b_data + PAGE_SIZE) {
1793 bh->b_size += PAGE_SIZE;
1794 bh->b_data -= PAGE_SIZE;
1795 if (full)
1796 atomic_add(PAGE_SIZE, &bh->b_count);
1797 continue;
1798 }
1799 if (b_data == bh->b_data + bh->b_size) {
1800 bh->b_size += PAGE_SIZE;
1801 if (full)
1802 atomic_add(PAGE_SIZE, &bh->b_count);
1803 continue;
1804 }
1805 prev_bh = bh;
Robert P. J. Day5cbded52006-12-13 00:35:56 -08001806 if ((bh = kmalloc(sizeof(struct idetape_bh), GFP_KERNEL)) == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807 free_page((unsigned long) b_data);
1808 goto abort;
1809 }
1810 bh->b_reqnext = NULL;
1811 bh->b_data = b_data;
1812 bh->b_size = PAGE_SIZE;
1813 atomic_set(&bh->b_count, full ? bh->b_size : 0);
1814 prev_bh->b_reqnext = bh;
1815 }
1816 bh->b_size -= tape->excess_bh_size;
1817 if (full)
1818 atomic_sub(tape->excess_bh_size, &bh->b_count);
1819 return stage;
1820abort:
1821 __idetape_kfree_stage(stage);
1822 return NULL;
1823}
1824
1825static idetape_stage_t *idetape_kmalloc_stage (idetape_tape_t *tape)
1826{
1827 idetape_stage_t *cache_stage = tape->cache_stage;
1828
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001829 debug_log(DBG_PROCS, "Enter %s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001830
1831 if (tape->nr_stages >= tape->max_stages)
1832 return NULL;
1833 if (cache_stage != NULL) {
1834 tape->cache_stage = NULL;
1835 return cache_stage;
1836 }
1837 return __idetape_kmalloc_stage(tape, 0, 0);
1838}
1839
Daniel Walkerdcd96372006-06-25 05:47:37 -07001840static int idetape_copy_stage_from_user (idetape_tape_t *tape, idetape_stage_t *stage, const char __user *buf, int n)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001841{
1842 struct idetape_bh *bh = tape->bh;
1843 int count;
Daniel Walkerdcd96372006-06-25 05:47:37 -07001844 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001845
1846 while (n) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001847 if (bh == NULL) {
1848 printk(KERN_ERR "ide-tape: bh == NULL in "
1849 "idetape_copy_stage_from_user\n");
Daniel Walkerdcd96372006-06-25 05:47:37 -07001850 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001851 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001852 count = min((unsigned int)(bh->b_size - atomic_read(&bh->b_count)), (unsigned int)n);
Daniel Walkerdcd96372006-06-25 05:47:37 -07001853 if (copy_from_user(bh->b_data + atomic_read(&bh->b_count), buf, count))
1854 ret = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001855 n -= count;
1856 atomic_add(count, &bh->b_count);
1857 buf += count;
1858 if (atomic_read(&bh->b_count) == bh->b_size) {
1859 bh = bh->b_reqnext;
1860 if (bh)
1861 atomic_set(&bh->b_count, 0);
1862 }
1863 }
1864 tape->bh = bh;
Daniel Walkerdcd96372006-06-25 05:47:37 -07001865 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001866}
1867
Daniel Walkerdcd96372006-06-25 05:47:37 -07001868static int idetape_copy_stage_to_user (idetape_tape_t *tape, char __user *buf, idetape_stage_t *stage, int n)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001869{
1870 struct idetape_bh *bh = tape->bh;
1871 int count;
Daniel Walkerdcd96372006-06-25 05:47:37 -07001872 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001873
1874 while (n) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001875 if (bh == NULL) {
1876 printk(KERN_ERR "ide-tape: bh == NULL in "
1877 "idetape_copy_stage_to_user\n");
Daniel Walkerdcd96372006-06-25 05:47:37 -07001878 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001879 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001880 count = min(tape->b_count, n);
Daniel Walkerdcd96372006-06-25 05:47:37 -07001881 if (copy_to_user(buf, tape->b_data, count))
1882 ret = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001883 n -= count;
1884 tape->b_data += count;
1885 tape->b_count -= count;
1886 buf += count;
1887 if (!tape->b_count) {
1888 tape->bh = bh = bh->b_reqnext;
1889 if (bh) {
1890 tape->b_data = bh->b_data;
1891 tape->b_count = atomic_read(&bh->b_count);
1892 }
1893 }
1894 }
Daniel Walkerdcd96372006-06-25 05:47:37 -07001895 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001896}
1897
1898static void idetape_init_merge_stage (idetape_tape_t *tape)
1899{
1900 struct idetape_bh *bh = tape->merge_stage->bh;
1901
1902 tape->bh = bh;
Borislav Petkov54abf372008-02-06 02:57:52 +01001903 if (tape->chrdev_dir == IDETAPE_DIR_WRITE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001904 atomic_set(&bh->b_count, 0);
1905 else {
1906 tape->b_data = bh->b_data;
1907 tape->b_count = atomic_read(&bh->b_count);
1908 }
1909}
1910
1911static void idetape_switch_buffers (idetape_tape_t *tape, idetape_stage_t *stage)
1912{
1913 struct idetape_bh *tmp;
1914
1915 tmp = stage->bh;
1916 stage->bh = tape->merge_stage->bh;
1917 tape->merge_stage->bh = tmp;
1918 idetape_init_merge_stage(tape);
1919}
1920
1921/*
1922 * idetape_add_stage_tail adds a new stage at the end of the pipeline.
1923 */
1924static void idetape_add_stage_tail (ide_drive_t *drive,idetape_stage_t *stage)
1925{
1926 idetape_tape_t *tape = drive->driver_data;
1927 unsigned long flags;
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001928
1929 debug_log(DBG_PROCS, "Enter %s\n", __func__);
1930
Borislav Petkov54bb2072008-02-06 02:57:52 +01001931 spin_lock_irqsave(&tape->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001932 stage->next = NULL;
1933 if (tape->last_stage != NULL)
1934 tape->last_stage->next=stage;
1935 else
1936 tape->first_stage = tape->next_stage=stage;
1937 tape->last_stage = stage;
1938 if (tape->next_stage == NULL)
1939 tape->next_stage = tape->last_stage;
1940 tape->nr_stages++;
1941 tape->nr_pending_stages++;
Borislav Petkov54bb2072008-02-06 02:57:52 +01001942 spin_unlock_irqrestore(&tape->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001943}
1944
1945/*
1946 * idetape_wait_for_request installs a completion in a pending request
1947 * and sleeps until it is serviced.
1948 *
1949 * The caller should ensure that the request will not be serviced
1950 * before we install the completion (usually by disabling interrupts).
1951 */
1952static void idetape_wait_for_request (ide_drive_t *drive, struct request *rq)
1953{
Peter Zijlstra6e9a4732006-09-30 23:28:10 -07001954 DECLARE_COMPLETION_ONSTACK(wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001955 idetape_tape_t *tape = drive->driver_data;
1956
Jens Axboe4aff5e22006-08-10 08:44:47 +02001957 if (rq == NULL || !blk_special_request(rq)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001958 printk (KERN_ERR "ide-tape: bug: Trying to sleep on non-valid request\n");
1959 return;
1960 }
Jens Axboec00895a2006-09-30 20:29:12 +02001961 rq->end_io_data = &wait;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001962 rq->end_io = blk_end_sync_rq;
Borislav Petkov54bb2072008-02-06 02:57:52 +01001963 spin_unlock_irq(&tape->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001964 wait_for_completion(&wait);
1965 /* The stage and its struct request have been deallocated */
Borislav Petkov54bb2072008-02-06 02:57:52 +01001966 spin_lock_irq(&tape->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001967}
1968
Borislav Petkova2f5b7f2008-02-06 02:57:51 +01001969static ide_startstop_t idetape_read_position_callback(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001970{
1971 idetape_tape_t *tape = drive->driver_data;
Borislav Petkova2f5b7f2008-02-06 02:57:51 +01001972 u8 *readpos = tape->pc->buffer;
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001973
1974 debug_log(DBG_PROCS, "Enter %s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001975
1976 if (!tape->pc->error) {
Borislav Petkova2f5b7f2008-02-06 02:57:51 +01001977 debug_log(DBG_SENSE, "BOP - %s\n",
1978 (readpos[0] & 0x80) ? "Yes" : "No");
1979 debug_log(DBG_SENSE, "EOP - %s\n",
1980 (readpos[0] & 0x40) ? "Yes" : "No");
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001981
Borislav Petkova2f5b7f2008-02-06 02:57:51 +01001982 if (readpos[0] & 0x4) {
1983 printk(KERN_INFO "ide-tape: Block location is unknown"
1984 "to the tape\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001985 clear_bit(IDETAPE_ADDRESS_VALID, &tape->flags);
1986 idetape_end_request(drive, 0, 0);
1987 } else {
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001988 debug_log(DBG_SENSE, "Block Location - %u\n",
Borislav Petkova2f5b7f2008-02-06 02:57:51 +01001989 be32_to_cpu(*(u32 *)&readpos[4]));
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001990
Borislav Petkova2f5b7f2008-02-06 02:57:51 +01001991 tape->partition = readpos[1];
Borislav Petkov54bb2072008-02-06 02:57:52 +01001992 tape->first_frame =
Borislav Petkova2f5b7f2008-02-06 02:57:51 +01001993 be32_to_cpu(*(u32 *)&readpos[4]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001994 set_bit(IDETAPE_ADDRESS_VALID, &tape->flags);
1995 idetape_end_request(drive, 1, 0);
1996 }
1997 } else {
1998 idetape_end_request(drive, 0, 0);
1999 }
2000 return ide_stopped;
2001}
2002
2003/*
2004 * idetape_create_write_filemark_cmd will:
2005 *
2006 * 1. Write a filemark if write_filemark=1.
2007 * 2. Flush the device buffers without writing a filemark
2008 * if write_filemark=0.
2009 *
2010 */
2011static void idetape_create_write_filemark_cmd (ide_drive_t *drive, idetape_pc_t *pc,int write_filemark)
2012{
2013 idetape_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +01002014 pc->c[0] = WRITE_FILEMARKS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002015 pc->c[4] = write_filemark;
2016 set_bit(PC_WAIT_FOR_DSC, &pc->flags);
2017 pc->callback = &idetape_pc_callback;
2018}
2019
2020static void idetape_create_test_unit_ready_cmd(idetape_pc_t *pc)
2021{
2022 idetape_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +01002023 pc->c[0] = TEST_UNIT_READY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002024 pc->callback = &idetape_pc_callback;
2025}
2026
2027/*
2028 * idetape_queue_pc_tail is based on the following functions:
2029 *
2030 * ide_do_drive_cmd from ide.c
2031 * cdrom_queue_request and cdrom_queue_packet_command from ide-cd.c
2032 *
2033 * We add a special packet command request to the tail of the request
2034 * queue, and wait for it to be serviced.
2035 *
2036 * This is not to be called from within the request handling part
2037 * of the driver ! We allocate here data in the stack, and it is valid
2038 * until the request is finished. This is not the case for the bottom
2039 * part of the driver, where we are always leaving the functions to wait
2040 * for an interrupt or a timer event.
2041 *
2042 * From the bottom part of the driver, we should allocate safe memory
2043 * using idetape_next_pc_storage and idetape_next_rq_storage, and add
2044 * the request to the request list without waiting for it to be serviced !
2045 * In that case, we usually use idetape_queue_pc_head.
2046 */
2047static int __idetape_queue_pc_tail (ide_drive_t *drive, idetape_pc_t *pc)
2048{
2049 struct ide_tape_obj *tape = drive->driver_data;
2050 struct request rq;
2051
2052 idetape_init_rq(&rq, REQ_IDETAPE_PC1);
2053 rq.buffer = (char *) pc;
2054 rq.rq_disk = tape->disk;
2055 return ide_do_drive_cmd(drive, &rq, ide_wait);
2056}
2057
2058static void idetape_create_load_unload_cmd (ide_drive_t *drive, idetape_pc_t *pc,int cmd)
2059{
2060 idetape_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +01002061 pc->c[0] = START_STOP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002062 pc->c[4] = cmd;
2063 set_bit(PC_WAIT_FOR_DSC, &pc->flags);
2064 pc->callback = &idetape_pc_callback;
2065}
2066
2067static int idetape_wait_ready(ide_drive_t *drive, unsigned long timeout)
2068{
2069 idetape_tape_t *tape = drive->driver_data;
2070 idetape_pc_t pc;
2071 int load_attempted = 0;
2072
2073 /*
2074 * Wait for the tape to become ready
2075 */
2076 set_bit(IDETAPE_MEDIUM_PRESENT, &tape->flags);
2077 timeout += jiffies;
2078 while (time_before(jiffies, timeout)) {
2079 idetape_create_test_unit_ready_cmd(&pc);
2080 if (!__idetape_queue_pc_tail(drive, &pc))
2081 return 0;
2082 if ((tape->sense_key == 2 && tape->asc == 4 && tape->ascq == 2)
2083 || (tape->asc == 0x3A)) { /* no media */
2084 if (load_attempted)
2085 return -ENOMEDIUM;
2086 idetape_create_load_unload_cmd(drive, &pc, IDETAPE_LU_LOAD_MASK);
2087 __idetape_queue_pc_tail(drive, &pc);
2088 load_attempted = 1;
2089 /* not about to be ready */
2090 } else if (!(tape->sense_key == 2 && tape->asc == 4 &&
2091 (tape->ascq == 1 || tape->ascq == 8)))
2092 return -EIO;
Nishanth Aravamudan80ce45f2005-09-10 00:27:08 -07002093 msleep(100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002094 }
2095 return -EIO;
2096}
2097
2098static int idetape_queue_pc_tail (ide_drive_t *drive,idetape_pc_t *pc)
2099{
2100 return __idetape_queue_pc_tail(drive, pc);
2101}
2102
2103static int idetape_flush_tape_buffers (ide_drive_t *drive)
2104{
2105 idetape_pc_t pc;
2106 int rc;
2107
2108 idetape_create_write_filemark_cmd(drive, &pc, 0);
2109 if ((rc = idetape_queue_pc_tail(drive, &pc)))
2110 return rc;
2111 idetape_wait_ready(drive, 60 * 5 * HZ);
2112 return 0;
2113}
2114
2115static void idetape_create_read_position_cmd (idetape_pc_t *pc)
2116{
2117 idetape_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +01002118 pc->c[0] = READ_POSITION;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002119 pc->request_transfer = 20;
2120 pc->callback = &idetape_read_position_callback;
2121}
2122
2123static int idetape_read_position (ide_drive_t *drive)
2124{
2125 idetape_tape_t *tape = drive->driver_data;
2126 idetape_pc_t pc;
2127 int position;
2128
Borislav Petkov8004a8c2008-02-06 02:57:51 +01002129 debug_log(DBG_PROCS, "Enter %s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002130
2131 idetape_create_read_position_cmd(&pc);
2132 if (idetape_queue_pc_tail(drive, &pc))
2133 return -1;
Borislav Petkov54bb2072008-02-06 02:57:52 +01002134 position = tape->first_frame;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002135 return position;
2136}
2137
2138static void idetape_create_locate_cmd (ide_drive_t *drive, idetape_pc_t *pc, unsigned int block, u8 partition, int skip)
2139{
2140 idetape_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +01002141 pc->c[0] = POSITION_TO_ELEMENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002142 pc->c[1] = 2;
Borislav Petkov860ff5e2008-02-02 19:56:50 +01002143 put_unaligned(cpu_to_be32(block), (unsigned int *) &pc->c[3]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002144 pc->c[8] = partition;
2145 set_bit(PC_WAIT_FOR_DSC, &pc->flags);
2146 pc->callback = &idetape_pc_callback;
2147}
2148
2149static int idetape_create_prevent_cmd (ide_drive_t *drive, idetape_pc_t *pc, int prevent)
2150{
2151 idetape_tape_t *tape = drive->driver_data;
2152
Borislav Petkovb6422012008-02-02 19:56:49 +01002153 /* device supports locking according to capabilities page */
2154 if (!(tape->caps[6] & 0x01))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002155 return 0;
2156
2157 idetape_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +01002158 pc->c[0] = ALLOW_MEDIUM_REMOVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002159 pc->c[4] = prevent;
2160 pc->callback = &idetape_pc_callback;
2161 return 1;
2162}
2163
2164static int __idetape_discard_read_pipeline (ide_drive_t *drive)
2165{
2166 idetape_tape_t *tape = drive->driver_data;
2167 unsigned long flags;
2168 int cnt;
2169
Borislav Petkov54abf372008-02-06 02:57:52 +01002170 if (tape->chrdev_dir != IDETAPE_DIR_READ)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002171 return 0;
2172
2173 /* Remove merge stage. */
Borislav Petkov54bb2072008-02-06 02:57:52 +01002174 cnt = tape->merge_stage_size / tape->blk_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002175 if (test_and_clear_bit(IDETAPE_FILEMARK, &tape->flags))
2176 ++cnt; /* Filemarks count as 1 sector */
2177 tape->merge_stage_size = 0;
2178 if (tape->merge_stage != NULL) {
2179 __idetape_kfree_stage(tape->merge_stage);
2180 tape->merge_stage = NULL;
2181 }
2182
2183 /* Clear pipeline flags. */
2184 clear_bit(IDETAPE_PIPELINE_ERROR, &tape->flags);
Borislav Petkov54abf372008-02-06 02:57:52 +01002185 tape->chrdev_dir = IDETAPE_DIR_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002186
2187 /* Remove pipeline stages. */
2188 if (tape->first_stage == NULL)
2189 return 0;
2190
Borislav Petkov54bb2072008-02-06 02:57:52 +01002191 spin_lock_irqsave(&tape->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002192 tape->next_stage = NULL;
2193 if (idetape_pipeline_active(tape))
Borislav Petkov54bb2072008-02-06 02:57:52 +01002194 idetape_wait_for_request(drive, tape->active_data_rq);
2195 spin_unlock_irqrestore(&tape->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002196
2197 while (tape->first_stage != NULL) {
2198 struct request *rq_ptr = &tape->first_stage->rq;
2199
2200 cnt += rq_ptr->nr_sectors - rq_ptr->current_nr_sectors;
2201 if (rq_ptr->errors == IDETAPE_ERROR_FILEMARK)
2202 ++cnt;
2203 idetape_remove_stage_head(drive);
2204 }
2205 tape->nr_pending_stages = 0;
2206 tape->max_stages = tape->min_pipeline;
2207 return cnt;
2208}
2209
2210/*
2211 * idetape_position_tape positions the tape to the requested block
2212 * using the LOCATE packet command. A READ POSITION command is then
2213 * issued to check where we are positioned.
2214 *
2215 * Like all higher level operations, we queue the commands at the tail
2216 * of the request queue and wait for their completion.
2217 *
2218 */
2219static int idetape_position_tape (ide_drive_t *drive, unsigned int block, u8 partition, int skip)
2220{
2221 idetape_tape_t *tape = drive->driver_data;
2222 int retval;
2223 idetape_pc_t pc;
2224
Borislav Petkov54abf372008-02-06 02:57:52 +01002225 if (tape->chrdev_dir == IDETAPE_DIR_READ)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002226 __idetape_discard_read_pipeline(drive);
2227 idetape_wait_ready(drive, 60 * 5 * HZ);
2228 idetape_create_locate_cmd(drive, &pc, block, partition, skip);
2229 retval = idetape_queue_pc_tail(drive, &pc);
2230 if (retval)
2231 return (retval);
2232
2233 idetape_create_read_position_cmd(&pc);
2234 return (idetape_queue_pc_tail(drive, &pc));
2235}
2236
2237static void idetape_discard_read_pipeline (ide_drive_t *drive, int restore_position)
2238{
2239 idetape_tape_t *tape = drive->driver_data;
2240 int cnt;
2241 int seek, position;
2242
2243 cnt = __idetape_discard_read_pipeline(drive);
2244 if (restore_position) {
2245 position = idetape_read_position(drive);
2246 seek = position > cnt ? position - cnt : 0;
2247 if (idetape_position_tape(drive, seek, 0, 0)) {
2248 printk(KERN_INFO "ide-tape: %s: position_tape failed in discard_pipeline()\n", tape->name);
2249 return;
2250 }
2251 }
2252}
2253
2254/*
2255 * idetape_queue_rw_tail generates a read/write request for the block
2256 * device interface and wait for it to be serviced.
2257 */
2258static int idetape_queue_rw_tail(ide_drive_t *drive, int cmd, int blocks, struct idetape_bh *bh)
2259{
2260 idetape_tape_t *tape = drive->driver_data;
2261 struct request rq;
2262
Borislav Petkov8004a8c2008-02-06 02:57:51 +01002263 debug_log(DBG_SENSE, "%s: cmd=%d\n", __func__, cmd);
2264
Linus Torvalds1da177e2005-04-16 15:20:36 -07002265 if (idetape_pipeline_active(tape)) {
Borislav Petkov8004a8c2008-02-06 02:57:51 +01002266 printk(KERN_ERR "ide-tape: bug: the pipeline is active in %s\n",
2267 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002268 return (0);
2269 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002270
2271 idetape_init_rq(&rq, cmd);
2272 rq.rq_disk = tape->disk;
2273 rq.special = (void *)bh;
Borislav Petkov54bb2072008-02-06 02:57:52 +01002274 rq.sector = tape->first_frame;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002275 rq.nr_sectors = rq.current_nr_sectors = blocks;
2276 (void) ide_do_drive_cmd(drive, &rq, ide_wait);
2277
2278 if ((cmd & (REQ_IDETAPE_READ | REQ_IDETAPE_WRITE)) == 0)
2279 return 0;
2280
2281 if (tape->merge_stage)
2282 idetape_init_merge_stage(tape);
2283 if (rq.errors == IDETAPE_ERROR_GENERAL)
2284 return -EIO;
Borislav Petkov54bb2072008-02-06 02:57:52 +01002285 return (tape->blk_size * (blocks-rq.current_nr_sectors));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002286}
2287
2288/*
2289 * idetape_insert_pipeline_into_queue is used to start servicing the
2290 * pipeline stages, starting from tape->next_stage.
2291 */
2292static void idetape_insert_pipeline_into_queue (ide_drive_t *drive)
2293{
2294 idetape_tape_t *tape = drive->driver_data;
2295
2296 if (tape->next_stage == NULL)
2297 return;
2298 if (!idetape_pipeline_active(tape)) {
2299 set_bit(IDETAPE_PIPELINE_ACTIVE, &tape->flags);
Borislav Petkov419d4742008-02-02 19:56:51 +01002300 idetape_activate_next_stage(drive);
Borislav Petkov54bb2072008-02-06 02:57:52 +01002301 (void) ide_do_drive_cmd(drive, tape->active_data_rq, ide_end);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002302 }
2303}
2304
2305static void idetape_create_inquiry_cmd (idetape_pc_t *pc)
2306{
2307 idetape_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +01002308 pc->c[0] = INQUIRY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002309 pc->c[4] = pc->request_transfer = 254;
2310 pc->callback = &idetape_pc_callback;
2311}
2312
2313static void idetape_create_rewind_cmd (ide_drive_t *drive, idetape_pc_t *pc)
2314{
2315 idetape_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +01002316 pc->c[0] = REZERO_UNIT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002317 set_bit(PC_WAIT_FOR_DSC, &pc->flags);
2318 pc->callback = &idetape_pc_callback;
2319}
2320
Linus Torvalds1da177e2005-04-16 15:20:36 -07002321static void idetape_create_erase_cmd (idetape_pc_t *pc)
2322{
2323 idetape_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +01002324 pc->c[0] = ERASE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002325 pc->c[1] = 1;
2326 set_bit(PC_WAIT_FOR_DSC, &pc->flags);
2327 pc->callback = &idetape_pc_callback;
2328}
2329
2330static void idetape_create_space_cmd (idetape_pc_t *pc,int count, u8 cmd)
2331{
2332 idetape_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +01002333 pc->c[0] = SPACE;
Borislav Petkov860ff5e2008-02-02 19:56:50 +01002334 put_unaligned(cpu_to_be32(count), (unsigned int *) &pc->c[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002335 pc->c[1] = cmd;
2336 set_bit(PC_WAIT_FOR_DSC, &pc->flags);
2337 pc->callback = &idetape_pc_callback;
2338}
2339
2340static void idetape_wait_first_stage (ide_drive_t *drive)
2341{
2342 idetape_tape_t *tape = drive->driver_data;
2343 unsigned long flags;
2344
2345 if (tape->first_stage == NULL)
2346 return;
Borislav Petkov54bb2072008-02-06 02:57:52 +01002347 spin_lock_irqsave(&tape->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002348 if (tape->active_stage == tape->first_stage)
Borislav Petkov54bb2072008-02-06 02:57:52 +01002349 idetape_wait_for_request(drive, tape->active_data_rq);
2350 spin_unlock_irqrestore(&tape->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002351}
2352
2353/*
2354 * idetape_add_chrdev_write_request tries to add a character device
2355 * originated write request to our pipeline. In case we don't succeed,
2356 * we revert to non-pipelined operation mode for this request.
2357 *
2358 * 1. Try to allocate a new pipeline stage.
2359 * 2. If we can't, wait for more and more requests to be serviced
2360 * and try again each time.
2361 * 3. If we still can't allocate a stage, fallback to
2362 * non-pipelined operation mode for this request.
2363 */
2364static int idetape_add_chrdev_write_request (ide_drive_t *drive, int blocks)
2365{
2366 idetape_tape_t *tape = drive->driver_data;
2367 idetape_stage_t *new_stage;
2368 unsigned long flags;
2369 struct request *rq;
2370
Borislav Petkov8004a8c2008-02-06 02:57:51 +01002371 debug_log(DBG_CHRDEV, "Enter %s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002372
2373 /*
2374 * Attempt to allocate a new stage.
2375 * Pay special attention to possible race conditions.
2376 */
2377 while ((new_stage = idetape_kmalloc_stage(tape)) == NULL) {
Borislav Petkov54bb2072008-02-06 02:57:52 +01002378 spin_lock_irqsave(&tape->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002379 if (idetape_pipeline_active(tape)) {
Borislav Petkov54bb2072008-02-06 02:57:52 +01002380 idetape_wait_for_request(drive, tape->active_data_rq);
2381 spin_unlock_irqrestore(&tape->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002382 } else {
Borislav Petkov54bb2072008-02-06 02:57:52 +01002383 spin_unlock_irqrestore(&tape->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002384 idetape_insert_pipeline_into_queue(drive);
2385 if (idetape_pipeline_active(tape))
2386 continue;
2387 /*
2388 * Linux is short on memory. Fallback to
2389 * non-pipelined operation mode for this request.
2390 */
2391 return idetape_queue_rw_tail(drive, REQ_IDETAPE_WRITE, blocks, tape->merge_stage->bh);
2392 }
2393 }
2394 rq = &new_stage->rq;
2395 idetape_init_rq(rq, REQ_IDETAPE_WRITE);
2396 /* Doesn't actually matter - We always assume sequential access */
Borislav Petkov54bb2072008-02-06 02:57:52 +01002397 rq->sector = tape->first_frame;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002398 rq->nr_sectors = rq->current_nr_sectors = blocks;
2399
2400 idetape_switch_buffers(tape, new_stage);
2401 idetape_add_stage_tail(drive, new_stage);
2402 tape->pipeline_head++;
Borislav Petkov37016ba2008-02-06 02:57:52 +01002403 idetape_calculate_speeds(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002404
2405 /*
2406 * Estimate whether the tape has stopped writing by checking
2407 * if our write pipeline is currently empty. If we are not
2408 * writing anymore, wait for the pipeline to be full enough
2409 * (90%) before starting to service requests, so that we will
2410 * be able to keep up with the higher speeds of the tape.
2411 */
2412 if (!idetape_pipeline_active(tape)) {
2413 if (tape->nr_stages >= tape->max_stages * 9 / 10 ||
Borislav Petkov54bb2072008-02-06 02:57:52 +01002414 tape->nr_stages >= tape->max_stages -
2415 tape->uncontrolled_pipeline_head_speed * 3 * 1024 /
2416 tape->blk_size) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002417 tape->measure_insert_time = 1;
2418 tape->insert_time = jiffies;
2419 tape->insert_size = 0;
2420 tape->insert_speed = 0;
2421 idetape_insert_pipeline_into_queue(drive);
2422 }
2423 }
2424 if (test_and_clear_bit(IDETAPE_PIPELINE_ERROR, &tape->flags))
2425 /* Return a deferred error */
2426 return -EIO;
2427 return blocks;
2428}
2429
2430/*
2431 * idetape_wait_for_pipeline will wait until all pending pipeline
2432 * requests are serviced. Typically called on device close.
2433 */
2434static void idetape_wait_for_pipeline (ide_drive_t *drive)
2435{
2436 idetape_tape_t *tape = drive->driver_data;
2437 unsigned long flags;
2438
2439 while (tape->next_stage || idetape_pipeline_active(tape)) {
2440 idetape_insert_pipeline_into_queue(drive);
Borislav Petkov54bb2072008-02-06 02:57:52 +01002441 spin_lock_irqsave(&tape->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002442 if (idetape_pipeline_active(tape))
Borislav Petkov54bb2072008-02-06 02:57:52 +01002443 idetape_wait_for_request(drive, tape->active_data_rq);
2444 spin_unlock_irqrestore(&tape->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002445 }
2446}
2447
2448static void idetape_empty_write_pipeline (ide_drive_t *drive)
2449{
2450 idetape_tape_t *tape = drive->driver_data;
2451 int blocks, min;
2452 struct idetape_bh *bh;
Borislav Petkov55a5d292008-02-02 19:56:49 +01002453
Borislav Petkov54abf372008-02-06 02:57:52 +01002454 if (tape->chrdev_dir != IDETAPE_DIR_WRITE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002455 printk(KERN_ERR "ide-tape: bug: Trying to empty write pipeline, but we are not writing.\n");
2456 return;
2457 }
2458 if (tape->merge_stage_size > tape->stage_size) {
2459 printk(KERN_ERR "ide-tape: bug: merge_buffer too big\n");
2460 tape->merge_stage_size = tape->stage_size;
2461 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002462 if (tape->merge_stage_size) {
Borislav Petkov54bb2072008-02-06 02:57:52 +01002463 blocks = tape->merge_stage_size / tape->blk_size;
2464 if (tape->merge_stage_size % tape->blk_size) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002465 unsigned int i;
2466
2467 blocks++;
Borislav Petkov54bb2072008-02-06 02:57:52 +01002468 i = tape->blk_size - tape->merge_stage_size %
2469 tape->blk_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002470 bh = tape->bh->b_reqnext;
2471 while (bh) {
2472 atomic_set(&bh->b_count, 0);
2473 bh = bh->b_reqnext;
2474 }
2475 bh = tape->bh;
2476 while (i) {
2477 if (bh == NULL) {
2478
2479 printk(KERN_INFO "ide-tape: bug, bh NULL\n");
2480 break;
2481 }
2482 min = min(i, (unsigned int)(bh->b_size - atomic_read(&bh->b_count)));
2483 memset(bh->b_data + atomic_read(&bh->b_count), 0, min);
2484 atomic_add(min, &bh->b_count);
2485 i -= min;
2486 bh = bh->b_reqnext;
2487 }
2488 }
2489 (void) idetape_add_chrdev_write_request(drive, blocks);
2490 tape->merge_stage_size = 0;
2491 }
2492 idetape_wait_for_pipeline(drive);
2493 if (tape->merge_stage != NULL) {
2494 __idetape_kfree_stage(tape->merge_stage);
2495 tape->merge_stage = NULL;
2496 }
2497 clear_bit(IDETAPE_PIPELINE_ERROR, &tape->flags);
Borislav Petkov54abf372008-02-06 02:57:52 +01002498 tape->chrdev_dir = IDETAPE_DIR_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002499
2500 /*
2501 * On the next backup, perform the feedback loop again.
2502 * (I don't want to keep sense information between backups,
2503 * as some systems are constantly on, and the system load
2504 * can be totally different on the next backup).
2505 */
2506 tape->max_stages = tape->min_pipeline;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002507 if (tape->first_stage != NULL ||
2508 tape->next_stage != NULL ||
2509 tape->last_stage != NULL ||
2510 tape->nr_stages != 0) {
2511 printk(KERN_ERR "ide-tape: ide-tape pipeline bug, "
2512 "first_stage %p, next_stage %p, "
2513 "last_stage %p, nr_stages %d\n",
2514 tape->first_stage, tape->next_stage,
2515 tape->last_stage, tape->nr_stages);
2516 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002517}
2518
2519static void idetape_restart_speed_control (ide_drive_t *drive)
2520{
2521 idetape_tape_t *tape = drive->driver_data;
2522
2523 tape->restart_speed_control_req = 0;
2524 tape->pipeline_head = 0;
Borislav Petkov41f81d542008-02-06 02:57:52 +01002525 tape->controlled_last_pipeline_head = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002526 tape->controlled_previous_pipeline_head = tape->uncontrolled_previous_pipeline_head = 0;
2527 tape->pipeline_head_speed = tape->controlled_pipeline_head_speed = 5000;
2528 tape->uncontrolled_pipeline_head_speed = 0;
2529 tape->controlled_pipeline_head_time = tape->uncontrolled_pipeline_head_time = jiffies;
2530 tape->controlled_previous_head_time = tape->uncontrolled_previous_head_time = jiffies;
2531}
2532
2533static int idetape_initiate_read (ide_drive_t *drive, int max_stages)
2534{
2535 idetape_tape_t *tape = drive->driver_data;
2536 idetape_stage_t *new_stage;
2537 struct request rq;
2538 int bytes_read;
Borislav Petkovb6422012008-02-02 19:56:49 +01002539 u16 blocks = *(u16 *)&tape->caps[12];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002540
2541 /* Initialize read operation */
Borislav Petkov54abf372008-02-06 02:57:52 +01002542 if (tape->chrdev_dir != IDETAPE_DIR_READ) {
2543 if (tape->chrdev_dir == IDETAPE_DIR_WRITE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002544 idetape_empty_write_pipeline(drive);
2545 idetape_flush_tape_buffers(drive);
2546 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002547 if (tape->merge_stage || tape->merge_stage_size) {
2548 printk (KERN_ERR "ide-tape: merge_stage_size should be 0 now\n");
2549 tape->merge_stage_size = 0;
2550 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002551 if ((tape->merge_stage = __idetape_kmalloc_stage(tape, 0, 0)) == NULL)
2552 return -ENOMEM;
Borislav Petkov54abf372008-02-06 02:57:52 +01002553 tape->chrdev_dir = IDETAPE_DIR_READ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002554
2555 /*
2556 * Issue a read 0 command to ensure that DSC handshake
2557 * is switched from completion mode to buffer available
2558 * mode.
2559 * No point in issuing this if DSC overlap isn't supported,
2560 * some drives (Seagate STT3401A) will return an error.
2561 */
2562 if (drive->dsc_overlap) {
2563 bytes_read = idetape_queue_rw_tail(drive, REQ_IDETAPE_READ, 0, tape->merge_stage->bh);
2564 if (bytes_read < 0) {
2565 __idetape_kfree_stage(tape->merge_stage);
2566 tape->merge_stage = NULL;
Borislav Petkov54abf372008-02-06 02:57:52 +01002567 tape->chrdev_dir = IDETAPE_DIR_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002568 return bytes_read;
2569 }
2570 }
2571 }
2572 if (tape->restart_speed_control_req)
2573 idetape_restart_speed_control(drive);
2574 idetape_init_rq(&rq, REQ_IDETAPE_READ);
Borislav Petkov54bb2072008-02-06 02:57:52 +01002575 rq.sector = tape->first_frame;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002576 rq.nr_sectors = rq.current_nr_sectors = blocks;
2577 if (!test_bit(IDETAPE_PIPELINE_ERROR, &tape->flags) &&
2578 tape->nr_stages < max_stages) {
2579 new_stage = idetape_kmalloc_stage(tape);
2580 while (new_stage != NULL) {
2581 new_stage->rq = rq;
2582 idetape_add_stage_tail(drive, new_stage);
2583 if (tape->nr_stages >= max_stages)
2584 break;
2585 new_stage = idetape_kmalloc_stage(tape);
2586 }
2587 }
2588 if (!idetape_pipeline_active(tape)) {
2589 if (tape->nr_pending_stages >= 3 * max_stages / 4) {
2590 tape->measure_insert_time = 1;
2591 tape->insert_time = jiffies;
2592 tape->insert_size = 0;
2593 tape->insert_speed = 0;
2594 idetape_insert_pipeline_into_queue(drive);
2595 }
2596 }
2597 return 0;
2598}
2599
2600/*
2601 * idetape_add_chrdev_read_request is called from idetape_chrdev_read
2602 * to service a character device read request and add read-ahead
2603 * requests to our pipeline.
2604 */
2605static int idetape_add_chrdev_read_request (ide_drive_t *drive,int blocks)
2606{
2607 idetape_tape_t *tape = drive->driver_data;
2608 unsigned long flags;
2609 struct request *rq_ptr;
2610 int bytes_read;
2611
Borislav Petkov8004a8c2008-02-06 02:57:51 +01002612 debug_log(DBG_PROCS, "Enter %s, %d blocks\n", __func__, blocks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002613
2614 /*
2615 * If we are at a filemark, return a read length of 0
2616 */
2617 if (test_bit(IDETAPE_FILEMARK, &tape->flags))
2618 return 0;
2619
2620 /*
2621 * Wait for the next block to be available at the head
2622 * of the pipeline
2623 */
2624 idetape_initiate_read(drive, tape->max_stages);
2625 if (tape->first_stage == NULL) {
2626 if (test_bit(IDETAPE_PIPELINE_ERROR, &tape->flags))
2627 return 0;
Borislav Petkov54bb2072008-02-06 02:57:52 +01002628 return idetape_queue_rw_tail(drive, REQ_IDETAPE_READ, blocks,
2629 tape->merge_stage->bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002630 }
2631 idetape_wait_first_stage(drive);
2632 rq_ptr = &tape->first_stage->rq;
Borislav Petkov54bb2072008-02-06 02:57:52 +01002633 bytes_read = tape->blk_size * (rq_ptr->nr_sectors -
2634 rq_ptr->current_nr_sectors);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002635 rq_ptr->nr_sectors = rq_ptr->current_nr_sectors = 0;
2636
2637
2638 if (rq_ptr->errors == IDETAPE_ERROR_EOD)
2639 return 0;
2640 else {
2641 idetape_switch_buffers(tape, tape->first_stage);
2642 if (rq_ptr->errors == IDETAPE_ERROR_FILEMARK)
2643 set_bit(IDETAPE_FILEMARK, &tape->flags);
Borislav Petkov54bb2072008-02-06 02:57:52 +01002644 spin_lock_irqsave(&tape->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002645 idetape_remove_stage_head(drive);
Borislav Petkov54bb2072008-02-06 02:57:52 +01002646 spin_unlock_irqrestore(&tape->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002647 tape->pipeline_head++;
Borislav Petkov37016ba2008-02-06 02:57:52 +01002648 idetape_calculate_speeds(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002649 }
Borislav Petkov54bb2072008-02-06 02:57:52 +01002650 if (bytes_read > blocks * tape->blk_size) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002651 printk(KERN_ERR "ide-tape: bug: trying to return more bytes than requested\n");
Borislav Petkov54bb2072008-02-06 02:57:52 +01002652 bytes_read = blocks * tape->blk_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002653 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002654 return (bytes_read);
2655}
2656
2657static void idetape_pad_zeros (ide_drive_t *drive, int bcount)
2658{
2659 idetape_tape_t *tape = drive->driver_data;
2660 struct idetape_bh *bh;
2661 int blocks;
2662
2663 while (bcount) {
2664 unsigned int count;
2665
2666 bh = tape->merge_stage->bh;
2667 count = min(tape->stage_size, bcount);
2668 bcount -= count;
Borislav Petkov54bb2072008-02-06 02:57:52 +01002669 blocks = count / tape->blk_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002670 while (count) {
2671 atomic_set(&bh->b_count, min(count, (unsigned int)bh->b_size));
2672 memset(bh->b_data, 0, atomic_read(&bh->b_count));
2673 count -= atomic_read(&bh->b_count);
2674 bh = bh->b_reqnext;
2675 }
2676 idetape_queue_rw_tail(drive, REQ_IDETAPE_WRITE, blocks, tape->merge_stage->bh);
2677 }
2678}
2679
2680static int idetape_pipeline_size (ide_drive_t *drive)
2681{
2682 idetape_tape_t *tape = drive->driver_data;
2683 idetape_stage_t *stage;
2684 struct request *rq;
2685 int size = 0;
2686
2687 idetape_wait_for_pipeline(drive);
2688 stage = tape->first_stage;
2689 while (stage != NULL) {
2690 rq = &stage->rq;
Borislav Petkov54bb2072008-02-06 02:57:52 +01002691 size += tape->blk_size * (rq->nr_sectors -
2692 rq->current_nr_sectors);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002693 if (rq->errors == IDETAPE_ERROR_FILEMARK)
Borislav Petkov54bb2072008-02-06 02:57:52 +01002694 size += tape->blk_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002695 stage = stage->next;
2696 }
2697 size += tape->merge_stage_size;
2698 return size;
2699}
2700
2701/*
2702 * Rewinds the tape to the Beginning Of the current Partition (BOP).
2703 *
2704 * We currently support only one partition.
2705 */
2706static int idetape_rewind_tape (ide_drive_t *drive)
2707{
2708 int retval;
2709 idetape_pc_t pc;
Borislav Petkov8004a8c2008-02-06 02:57:51 +01002710 idetape_tape_t *tape;
2711 tape = drive->driver_data;
2712
2713 debug_log(DBG_SENSE, "Enter %s\n", __func__);
2714
Linus Torvalds1da177e2005-04-16 15:20:36 -07002715 idetape_create_rewind_cmd(drive, &pc);
2716 retval = idetape_queue_pc_tail(drive, &pc);
2717 if (retval)
2718 return retval;
2719
2720 idetape_create_read_position_cmd(&pc);
2721 retval = idetape_queue_pc_tail(drive, &pc);
2722 if (retval)
2723 return retval;
2724 return 0;
2725}
2726
2727/*
2728 * Our special ide-tape ioctl's.
2729 *
2730 * Currently there aren't any ioctl's.
2731 * mtio.h compatible commands should be issued to the character device
2732 * interface.
2733 */
2734static int idetape_blkdev_ioctl(ide_drive_t *drive, unsigned int cmd, unsigned long arg)
2735{
2736 idetape_tape_t *tape = drive->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002737 void __user *argp = (void __user *)arg;
2738
Borislav Petkovd59823f2008-02-02 19:56:51 +01002739 struct idetape_config {
2740 int dsc_rw_frequency;
2741 int dsc_media_access_frequency;
2742 int nr_stages;
2743 } config;
2744
Borislav Petkov8004a8c2008-02-06 02:57:51 +01002745 debug_log(DBG_PROCS, "Enter %s\n", __func__);
2746
Linus Torvalds1da177e2005-04-16 15:20:36 -07002747 switch (cmd) {
2748 case 0x0340:
Borislav Petkovd59823f2008-02-02 19:56:51 +01002749 if (copy_from_user(&config, argp, sizeof(config)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002750 return -EFAULT;
Borislav Petkov54bb2072008-02-06 02:57:52 +01002751 tape->best_dsc_rw_freq = config.dsc_rw_frequency;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002752 tape->max_stages = config.nr_stages;
2753 break;
2754 case 0x0350:
Borislav Petkov54bb2072008-02-06 02:57:52 +01002755 config.dsc_rw_frequency = (int) tape->best_dsc_rw_freq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002756 config.nr_stages = tape->max_stages;
Borislav Petkovd59823f2008-02-02 19:56:51 +01002757 if (copy_to_user(argp, &config, sizeof(config)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002758 return -EFAULT;
2759 break;
2760 default:
2761 return -EIO;
2762 }
2763 return 0;
2764}
2765
2766/*
2767 * idetape_space_over_filemarks is now a bit more complicated than just
2768 * passing the command to the tape since we may have crossed some
2769 * filemarks during our pipelined read-ahead mode.
2770 *
2771 * As a minor side effect, the pipeline enables us to support MTFSFM when
2772 * the filemark is in our internal pipeline even if the tape doesn't
2773 * support spacing over filemarks in the reverse direction.
2774 */
2775static int idetape_space_over_filemarks (ide_drive_t *drive,short mt_op,int mt_count)
2776{
2777 idetape_tape_t *tape = drive->driver_data;
2778 idetape_pc_t pc;
2779 unsigned long flags;
2780 int retval,count=0;
Borislav Petkovb6422012008-02-02 19:56:49 +01002781 int sprev = !!(tape->caps[4] & 0x20);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002782
2783 if (mt_count == 0)
2784 return 0;
2785 if (MTBSF == mt_op || MTBSFM == mt_op) {
Borislav Petkovb6422012008-02-02 19:56:49 +01002786 if (!sprev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002787 return -EIO;
2788 mt_count = - mt_count;
2789 }
2790
Borislav Petkov54abf372008-02-06 02:57:52 +01002791 if (tape->chrdev_dir == IDETAPE_DIR_READ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002792 /*
2793 * We have a read-ahead buffer. Scan it for crossed
2794 * filemarks.
2795 */
2796 tape->merge_stage_size = 0;
2797 if (test_and_clear_bit(IDETAPE_FILEMARK, &tape->flags))
2798 ++count;
2799 while (tape->first_stage != NULL) {
2800 if (count == mt_count) {
2801 if (mt_op == MTFSFM)
2802 set_bit(IDETAPE_FILEMARK, &tape->flags);
2803 return 0;
2804 }
Borislav Petkov54bb2072008-02-06 02:57:52 +01002805 spin_lock_irqsave(&tape->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002806 if (tape->first_stage == tape->active_stage) {
2807 /*
2808 * We have reached the active stage in the read pipeline.
2809 * There is no point in allowing the drive to continue
2810 * reading any farther, so we stop the pipeline.
2811 *
2812 * This section should be moved to a separate subroutine,
2813 * because a similar function is performed in
2814 * __idetape_discard_read_pipeline(), for example.
2815 */
2816 tape->next_stage = NULL;
Borislav Petkov54bb2072008-02-06 02:57:52 +01002817 spin_unlock_irqrestore(&tape->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002818 idetape_wait_first_stage(drive);
2819 tape->next_stage = tape->first_stage->next;
2820 } else
Borislav Petkov54bb2072008-02-06 02:57:52 +01002821 spin_unlock_irqrestore(&tape->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002822 if (tape->first_stage->rq.errors == IDETAPE_ERROR_FILEMARK)
2823 ++count;
2824 idetape_remove_stage_head(drive);
2825 }
2826 idetape_discard_read_pipeline(drive, 0);
2827 }
2828
2829 /*
2830 * The filemark was not found in our internal pipeline.
2831 * Now we can issue the space command.
2832 */
2833 switch (mt_op) {
2834 case MTFSF:
2835 case MTBSF:
2836 idetape_create_space_cmd(&pc,mt_count-count,IDETAPE_SPACE_OVER_FILEMARK);
2837 return (idetape_queue_pc_tail(drive, &pc));
2838 case MTFSFM:
2839 case MTBSFM:
Borislav Petkovb6422012008-02-02 19:56:49 +01002840 if (!sprev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002841 return (-EIO);
2842 retval = idetape_space_over_filemarks(drive, MTFSF, mt_count-count);
2843 if (retval) return (retval);
2844 count = (MTBSFM == mt_op ? 1 : -1);
2845 return (idetape_space_over_filemarks(drive, MTFSF, count));
2846 default:
2847 printk(KERN_ERR "ide-tape: MTIO operation %d not supported\n",mt_op);
2848 return (-EIO);
2849 }
2850}
2851
2852
2853/*
2854 * Our character device read / write functions.
2855 *
2856 * The tape is optimized to maximize throughput when it is transferring
2857 * an integral number of the "continuous transfer limit", which is
2858 * a parameter of the specific tape (26 KB on my particular tape).
2859 * (32 kB for Onstream)
2860 *
2861 * As of version 1.3 of the driver, the character device provides an
2862 * abstract continuous view of the media - any mix of block sizes (even 1
2863 * byte) on the same backup/restore procedure is supported. The driver
2864 * will internally convert the requests to the recommended transfer unit,
2865 * so that an unmatch between the user's block size to the recommended
2866 * size will only result in a (slightly) increased driver overhead, but
2867 * will no longer hit performance.
2868 * This is not applicable to Onstream.
2869 */
2870static ssize_t idetape_chrdev_read (struct file *file, char __user *buf,
2871 size_t count, loff_t *ppos)
2872{
2873 struct ide_tape_obj *tape = ide_tape_f(file);
2874 ide_drive_t *drive = tape->drive;
2875 ssize_t bytes_read,temp, actually_read = 0, rc;
Daniel Walkerdcd96372006-06-25 05:47:37 -07002876 ssize_t ret = 0;
Borislav Petkovb6422012008-02-02 19:56:49 +01002877 u16 ctl = *(u16 *)&tape->caps[12];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002878
Borislav Petkov8004a8c2008-02-06 02:57:51 +01002879 debug_log(DBG_CHRDEV, "Enter %s, count %Zd\n", __func__, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002880
Borislav Petkov54abf372008-02-06 02:57:52 +01002881 if (tape->chrdev_dir != IDETAPE_DIR_READ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002882 if (test_bit(IDETAPE_DETECT_BS, &tape->flags))
Borislav Petkov54bb2072008-02-06 02:57:52 +01002883 if (count > tape->blk_size &&
2884 (count % tape->blk_size) == 0)
2885 tape->user_bs_factor = count / tape->blk_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002886 }
2887 if ((rc = idetape_initiate_read(drive, tape->max_stages)) < 0)
2888 return rc;
2889 if (count == 0)
2890 return (0);
2891 if (tape->merge_stage_size) {
2892 actually_read = min((unsigned int)(tape->merge_stage_size), (unsigned int)count);
Daniel Walkerdcd96372006-06-25 05:47:37 -07002893 if (idetape_copy_stage_to_user(tape, buf, tape->merge_stage, actually_read))
2894 ret = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002895 buf += actually_read;
2896 tape->merge_stage_size -= actually_read;
2897 count -= actually_read;
2898 }
2899 while (count >= tape->stage_size) {
Borislav Petkovb6422012008-02-02 19:56:49 +01002900 bytes_read = idetape_add_chrdev_read_request(drive, ctl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002901 if (bytes_read <= 0)
2902 goto finish;
Daniel Walkerdcd96372006-06-25 05:47:37 -07002903 if (idetape_copy_stage_to_user(tape, buf, tape->merge_stage, bytes_read))
2904 ret = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002905 buf += bytes_read;
2906 count -= bytes_read;
2907 actually_read += bytes_read;
2908 }
2909 if (count) {
Borislav Petkovb6422012008-02-02 19:56:49 +01002910 bytes_read = idetape_add_chrdev_read_request(drive, ctl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002911 if (bytes_read <= 0)
2912 goto finish;
2913 temp = min((unsigned long)count, (unsigned long)bytes_read);
Daniel Walkerdcd96372006-06-25 05:47:37 -07002914 if (idetape_copy_stage_to_user(tape, buf, tape->merge_stage, temp))
2915 ret = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002916 actually_read += temp;
2917 tape->merge_stage_size = bytes_read-temp;
2918 }
2919finish:
2920 if (!actually_read && test_bit(IDETAPE_FILEMARK, &tape->flags)) {
Borislav Petkov8004a8c2008-02-06 02:57:51 +01002921 debug_log(DBG_SENSE, "%s: spacing over filemark\n", tape->name);
2922
Linus Torvalds1da177e2005-04-16 15:20:36 -07002923 idetape_space_over_filemarks(drive, MTFSF, 1);
2924 return 0;
2925 }
Daniel Walkerdcd96372006-06-25 05:47:37 -07002926
2927 return (ret) ? ret : actually_read;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002928}
2929
2930static ssize_t idetape_chrdev_write (struct file *file, const char __user *buf,
2931 size_t count, loff_t *ppos)
2932{
2933 struct ide_tape_obj *tape = ide_tape_f(file);
2934 ide_drive_t *drive = tape->drive;
Daniel Walkerdcd96372006-06-25 05:47:37 -07002935 ssize_t actually_written = 0;
2936 ssize_t ret = 0;
Borislav Petkovb6422012008-02-02 19:56:49 +01002937 u16 ctl = *(u16 *)&tape->caps[12];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002938
2939 /* The drive is write protected. */
2940 if (tape->write_prot)
2941 return -EACCES;
2942
Borislav Petkov8004a8c2008-02-06 02:57:51 +01002943 debug_log(DBG_CHRDEV, "Enter %s, count %Zd\n", __func__, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002944
2945 /* Initialize write operation */
Borislav Petkov54abf372008-02-06 02:57:52 +01002946 if (tape->chrdev_dir != IDETAPE_DIR_WRITE) {
2947 if (tape->chrdev_dir == IDETAPE_DIR_READ)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002948 idetape_discard_read_pipeline(drive, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002949 if (tape->merge_stage || tape->merge_stage_size) {
2950 printk(KERN_ERR "ide-tape: merge_stage_size "
2951 "should be 0 now\n");
2952 tape->merge_stage_size = 0;
2953 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002954 if ((tape->merge_stage = __idetape_kmalloc_stage(tape, 0, 0)) == NULL)
2955 return -ENOMEM;
Borislav Petkov54abf372008-02-06 02:57:52 +01002956 tape->chrdev_dir = IDETAPE_DIR_WRITE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002957 idetape_init_merge_stage(tape);
2958
2959 /*
2960 * Issue a write 0 command to ensure that DSC handshake
2961 * is switched from completion mode to buffer available
2962 * mode.
2963 * No point in issuing this if DSC overlap isn't supported,
2964 * some drives (Seagate STT3401A) will return an error.
2965 */
2966 if (drive->dsc_overlap) {
Daniel Walkerdcd96372006-06-25 05:47:37 -07002967 ssize_t retval = idetape_queue_rw_tail(drive, REQ_IDETAPE_WRITE, 0, tape->merge_stage->bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002968 if (retval < 0) {
2969 __idetape_kfree_stage(tape->merge_stage);
2970 tape->merge_stage = NULL;
Borislav Petkov54abf372008-02-06 02:57:52 +01002971 tape->chrdev_dir = IDETAPE_DIR_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002972 return retval;
2973 }
2974 }
2975 }
2976 if (count == 0)
2977 return (0);
2978 if (tape->restart_speed_control_req)
2979 idetape_restart_speed_control(drive);
2980 if (tape->merge_stage_size) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002981 if (tape->merge_stage_size >= tape->stage_size) {
2982 printk(KERN_ERR "ide-tape: bug: merge buffer too big\n");
2983 tape->merge_stage_size = 0;
2984 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002985 actually_written = min((unsigned int)(tape->stage_size - tape->merge_stage_size), (unsigned int)count);
Daniel Walkerdcd96372006-06-25 05:47:37 -07002986 if (idetape_copy_stage_from_user(tape, tape->merge_stage, buf, actually_written))
2987 ret = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002988 buf += actually_written;
2989 tape->merge_stage_size += actually_written;
2990 count -= actually_written;
2991
2992 if (tape->merge_stage_size == tape->stage_size) {
Daniel Walkerdcd96372006-06-25 05:47:37 -07002993 ssize_t retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002994 tape->merge_stage_size = 0;
Borislav Petkovb6422012008-02-02 19:56:49 +01002995 retval = idetape_add_chrdev_write_request(drive, ctl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002996 if (retval <= 0)
2997 return (retval);
2998 }
2999 }
3000 while (count >= tape->stage_size) {
Daniel Walkerdcd96372006-06-25 05:47:37 -07003001 ssize_t retval;
3002 if (idetape_copy_stage_from_user(tape, tape->merge_stage, buf, tape->stage_size))
3003 ret = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003004 buf += tape->stage_size;
3005 count -= tape->stage_size;
Borislav Petkovb6422012008-02-02 19:56:49 +01003006 retval = idetape_add_chrdev_write_request(drive, ctl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003007 actually_written += tape->stage_size;
3008 if (retval <= 0)
3009 return (retval);
3010 }
3011 if (count) {
3012 actually_written += count;
Daniel Walkerdcd96372006-06-25 05:47:37 -07003013 if (idetape_copy_stage_from_user(tape, tape->merge_stage, buf, count))
3014 ret = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003015 tape->merge_stage_size += count;
3016 }
Daniel Walkerdcd96372006-06-25 05:47:37 -07003017 return (ret) ? ret : actually_written;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003018}
3019
3020static int idetape_write_filemark (ide_drive_t *drive)
3021{
3022 idetape_pc_t pc;
3023
3024 /* Write a filemark */
3025 idetape_create_write_filemark_cmd(drive, &pc, 1);
3026 if (idetape_queue_pc_tail(drive, &pc)) {
3027 printk(KERN_ERR "ide-tape: Couldn't write a filemark\n");
3028 return -EIO;
3029 }
3030 return 0;
3031}
3032
3033/*
Borislav Petkovd99c9da2008-02-02 19:56:51 +01003034 * Called from idetape_chrdev_ioctl when the general mtio MTIOCTOP ioctl is
3035 * requested.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003036 *
Borislav Petkovd99c9da2008-02-02 19:56:51 +01003037 * Note: MTBSF and MTBSFM are not supported when the tape doesn't support
3038 * spacing over filemarks in the reverse direction. In this case, MTFSFM is also
3039 * usually not supported (it is supported in the rare case in which we crossed
3040 * the filemark during our read-ahead pipelined operation mode).
Linus Torvalds1da177e2005-04-16 15:20:36 -07003041 *
Borislav Petkovd99c9da2008-02-02 19:56:51 +01003042 * The following commands are currently not supported:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003043 *
Borislav Petkovd99c9da2008-02-02 19:56:51 +01003044 * MTFSS, MTBSS, MTWSM, MTSETDENSITY, MTSETDRVBUFFER, MT_ST_BOOLEANS,
3045 * MT_ST_WRITE_THRESHOLD.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003046 */
Borislav Petkovd99c9da2008-02-02 19:56:51 +01003047static int idetape_mtioctop(ide_drive_t *drive, short mt_op, int mt_count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003048{
3049 idetape_tape_t *tape = drive->driver_data;
3050 idetape_pc_t pc;
3051 int i,retval;
3052
Borislav Petkov8004a8c2008-02-06 02:57:51 +01003053 debug_log(DBG_ERR, "Handling MTIOCTOP ioctl: mt_op=%d, mt_count=%d\n",
3054 mt_op, mt_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003055 /*
3056 * Commands which need our pipelined read-ahead stages.
3057 */
3058 switch (mt_op) {
3059 case MTFSF:
3060 case MTFSFM:
3061 case MTBSF:
3062 case MTBSFM:
3063 if (!mt_count)
3064 return (0);
3065 return (idetape_space_over_filemarks(drive,mt_op,mt_count));
3066 default:
3067 break;
3068 }
3069 switch (mt_op) {
3070 case MTWEOF:
3071 if (tape->write_prot)
3072 return -EACCES;
3073 idetape_discard_read_pipeline(drive, 1);
3074 for (i = 0; i < mt_count; i++) {
3075 retval = idetape_write_filemark(drive);
3076 if (retval)
3077 return retval;
3078 }
3079 return (0);
3080 case MTREW:
3081 idetape_discard_read_pipeline(drive, 0);
3082 if (idetape_rewind_tape(drive))
3083 return -EIO;
3084 return 0;
3085 case MTLOAD:
3086 idetape_discard_read_pipeline(drive, 0);
3087 idetape_create_load_unload_cmd(drive, &pc, IDETAPE_LU_LOAD_MASK);
3088 return (idetape_queue_pc_tail(drive, &pc));
3089 case MTUNLOAD:
3090 case MTOFFL:
3091 /*
3092 * If door is locked, attempt to unlock before
3093 * attempting to eject.
3094 */
3095 if (tape->door_locked) {
3096 if (idetape_create_prevent_cmd(drive, &pc, 0))
3097 if (!idetape_queue_pc_tail(drive, &pc))
3098 tape->door_locked = DOOR_UNLOCKED;
3099 }
3100 idetape_discard_read_pipeline(drive, 0);
3101 idetape_create_load_unload_cmd(drive, &pc,!IDETAPE_LU_LOAD_MASK);
3102 retval = idetape_queue_pc_tail(drive, &pc);
3103 if (!retval)
3104 clear_bit(IDETAPE_MEDIUM_PRESENT, &tape->flags);
3105 return retval;
3106 case MTNOP:
3107 idetape_discard_read_pipeline(drive, 0);
3108 return (idetape_flush_tape_buffers(drive));
3109 case MTRETEN:
3110 idetape_discard_read_pipeline(drive, 0);
3111 idetape_create_load_unload_cmd(drive, &pc,IDETAPE_LU_RETENSION_MASK | IDETAPE_LU_LOAD_MASK);
3112 return (idetape_queue_pc_tail(drive, &pc));
3113 case MTEOM:
3114 idetape_create_space_cmd(&pc, 0, IDETAPE_SPACE_TO_EOD);
3115 return (idetape_queue_pc_tail(drive, &pc));
3116 case MTERASE:
3117 (void) idetape_rewind_tape(drive);
3118 idetape_create_erase_cmd(&pc);
3119 return (idetape_queue_pc_tail(drive, &pc));
3120 case MTSETBLK:
3121 if (mt_count) {
Borislav Petkov54bb2072008-02-06 02:57:52 +01003122 if (mt_count < tape->blk_size ||
3123 mt_count % tape->blk_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003124 return -EIO;
Borislav Petkov54bb2072008-02-06 02:57:52 +01003125 tape->user_bs_factor = mt_count /
3126 tape->blk_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003127 clear_bit(IDETAPE_DETECT_BS, &tape->flags);
3128 } else
3129 set_bit(IDETAPE_DETECT_BS, &tape->flags);
3130 return 0;
3131 case MTSEEK:
3132 idetape_discard_read_pipeline(drive, 0);
3133 return idetape_position_tape(drive, mt_count * tape->user_bs_factor, tape->partition, 0);
3134 case MTSETPART:
3135 idetape_discard_read_pipeline(drive, 0);
3136 return (idetape_position_tape(drive, 0, mt_count, 0));
3137 case MTFSR:
3138 case MTBSR:
3139 case MTLOCK:
3140 if (!idetape_create_prevent_cmd(drive, &pc, 1))
3141 return 0;
3142 retval = idetape_queue_pc_tail(drive, &pc);
3143 if (retval) return retval;
3144 tape->door_locked = DOOR_EXPLICITLY_LOCKED;
3145 return 0;
3146 case MTUNLOCK:
3147 if (!idetape_create_prevent_cmd(drive, &pc, 0))
3148 return 0;
3149 retval = idetape_queue_pc_tail(drive, &pc);
3150 if (retval) return retval;
3151 tape->door_locked = DOOR_UNLOCKED;
3152 return 0;
3153 default:
3154 printk(KERN_ERR "ide-tape: MTIO operation %d not "
3155 "supported\n", mt_op);
3156 return (-EIO);
3157 }
3158}
3159
3160/*
Borislav Petkovd99c9da2008-02-02 19:56:51 +01003161 * Our character device ioctls. General mtio.h magnetic io commands are
3162 * supported here, and not in the corresponding block interface. Our own
3163 * ide-tape ioctls are supported on both interfaces.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003164 */
Borislav Petkovd99c9da2008-02-02 19:56:51 +01003165static int idetape_chrdev_ioctl(struct inode *inode, struct file *file,
3166 unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003167{
3168 struct ide_tape_obj *tape = ide_tape_f(file);
3169 ide_drive_t *drive = tape->drive;
3170 struct mtop mtop;
3171 struct mtget mtget;
3172 struct mtpos mtpos;
Borislav Petkov54bb2072008-02-06 02:57:52 +01003173 int block_offset = 0, position = tape->first_frame;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003174 void __user *argp = (void __user *)arg;
3175
Borislav Petkov8004a8c2008-02-06 02:57:51 +01003176 debug_log(DBG_CHRDEV, "Enter %s, cmd=%u\n", __func__, cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003177
3178 tape->restart_speed_control_req = 1;
Borislav Petkov54abf372008-02-06 02:57:52 +01003179 if (tape->chrdev_dir == IDETAPE_DIR_WRITE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003180 idetape_empty_write_pipeline(drive);
3181 idetape_flush_tape_buffers(drive);
3182 }
3183 if (cmd == MTIOCGET || cmd == MTIOCPOS) {
Borislav Petkov54bb2072008-02-06 02:57:52 +01003184 block_offset = idetape_pipeline_size(drive) /
3185 (tape->blk_size * tape->user_bs_factor);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003186 if ((position = idetape_read_position(drive)) < 0)
3187 return -EIO;
3188 }
3189 switch (cmd) {
3190 case MTIOCTOP:
3191 if (copy_from_user(&mtop, argp, sizeof (struct mtop)))
3192 return -EFAULT;
3193 return (idetape_mtioctop(drive,mtop.mt_op,mtop.mt_count));
3194 case MTIOCGET:
3195 memset(&mtget, 0, sizeof (struct mtget));
3196 mtget.mt_type = MT_ISSCSI2;
3197 mtget.mt_blkno = position / tape->user_bs_factor - block_offset;
Borislav Petkov54bb2072008-02-06 02:57:52 +01003198 mtget.mt_dsreg =
3199 ((tape->blk_size * tape->user_bs_factor)
3200 << MT_ST_BLKSIZE_SHIFT) & MT_ST_BLKSIZE_MASK;
3201
Linus Torvalds1da177e2005-04-16 15:20:36 -07003202 if (tape->drv_write_prot) {
3203 mtget.mt_gstat |= GMT_WR_PROT(0xffffffff);
3204 }
3205 if (copy_to_user(argp, &mtget, sizeof(struct mtget)))
3206 return -EFAULT;
3207 return 0;
3208 case MTIOCPOS:
3209 mtpos.mt_blkno = position / tape->user_bs_factor - block_offset;
3210 if (copy_to_user(argp, &mtpos, sizeof(struct mtpos)))
3211 return -EFAULT;
3212 return 0;
3213 default:
Borislav Petkov54abf372008-02-06 02:57:52 +01003214 if (tape->chrdev_dir == IDETAPE_DIR_READ)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003215 idetape_discard_read_pipeline(drive, 1);
3216 return idetape_blkdev_ioctl(drive, cmd, arg);
3217 }
3218}
3219
Borislav Petkov3cffb9c2008-02-02 19:56:50 +01003220/*
3221 * Do a mode sense page 0 with block descriptor and if it succeeds set the tape
3222 * block size with the reported value.
3223 */
3224static void ide_tape_get_bsize_from_bdesc(ide_drive_t *drive)
3225{
3226 idetape_tape_t *tape = drive->driver_data;
3227 idetape_pc_t pc;
3228
3229 idetape_create_mode_sense_cmd(&pc, IDETAPE_BLOCK_DESCRIPTOR);
3230 if (idetape_queue_pc_tail(drive, &pc)) {
3231 printk(KERN_ERR "ide-tape: Can't get block descriptor\n");
Borislav Petkov54bb2072008-02-06 02:57:52 +01003232 if (tape->blk_size == 0) {
Borislav Petkov3cffb9c2008-02-02 19:56:50 +01003233 printk(KERN_WARNING "ide-tape: Cannot deal with zero "
3234 "block size, assuming 32k\n");
Borislav Petkov54bb2072008-02-06 02:57:52 +01003235 tape->blk_size = 32768;
Borislav Petkov3cffb9c2008-02-02 19:56:50 +01003236 }
3237 return;
3238 }
Borislav Petkov54bb2072008-02-06 02:57:52 +01003239 tape->blk_size = (pc.buffer[4 + 5] << 16) +
Borislav Petkov3cffb9c2008-02-02 19:56:50 +01003240 (pc.buffer[4 + 6] << 8) +
3241 pc.buffer[4 + 7];
3242 tape->drv_write_prot = (pc.buffer[2] & 0x80) >> 7;
3243}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003244
3245/*
3246 * Our character device open function.
3247 */
3248static int idetape_chrdev_open (struct inode *inode, struct file *filp)
3249{
3250 unsigned int minor = iminor(inode), i = minor & ~0xc0;
3251 ide_drive_t *drive;
3252 idetape_tape_t *tape;
3253 idetape_pc_t pc;
3254 int retval;
3255
Borislav Petkov8004a8c2008-02-06 02:57:51 +01003256 if (i >= MAX_HWIFS * MAX_DRIVES)
3257 return -ENXIO;
3258
3259 tape = ide_tape_chrdev_get(i);
3260 if (!tape)
3261 return -ENXIO;
3262
3263 debug_log(DBG_CHRDEV, "Enter %s\n", __func__);
3264
Linus Torvalds1da177e2005-04-16 15:20:36 -07003265 /*
3266 * We really want to do nonseekable_open(inode, filp); here, but some
3267 * versions of tar incorrectly call lseek on tapes and bail out if that
3268 * fails. So we disallow pread() and pwrite(), but permit lseeks.
3269 */
3270 filp->f_mode &= ~(FMODE_PREAD | FMODE_PWRITE);
3271
Linus Torvalds1da177e2005-04-16 15:20:36 -07003272 drive = tape->drive;
3273
3274 filp->private_data = tape;
3275
3276 if (test_and_set_bit(IDETAPE_BUSY, &tape->flags)) {
3277 retval = -EBUSY;
3278 goto out_put_tape;
3279 }
3280
3281 retval = idetape_wait_ready(drive, 60 * HZ);
3282 if (retval) {
3283 clear_bit(IDETAPE_BUSY, &tape->flags);
3284 printk(KERN_ERR "ide-tape: %s: drive not ready\n", tape->name);
3285 goto out_put_tape;
3286 }
3287
3288 idetape_read_position(drive);
3289 if (!test_bit(IDETAPE_ADDRESS_VALID, &tape->flags))
3290 (void)idetape_rewind_tape(drive);
3291
Borislav Petkov54abf372008-02-06 02:57:52 +01003292 if (tape->chrdev_dir != IDETAPE_DIR_READ)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003293 clear_bit(IDETAPE_PIPELINE_ERROR, &tape->flags);
3294
3295 /* Read block size and write protect status from drive. */
Borislav Petkov3cffb9c2008-02-02 19:56:50 +01003296 ide_tape_get_bsize_from_bdesc(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003297
3298 /* Set write protect flag if device is opened as read-only. */
3299 if ((filp->f_flags & O_ACCMODE) == O_RDONLY)
3300 tape->write_prot = 1;
3301 else
3302 tape->write_prot = tape->drv_write_prot;
3303
3304 /* Make sure drive isn't write protected if user wants to write. */
3305 if (tape->write_prot) {
3306 if ((filp->f_flags & O_ACCMODE) == O_WRONLY ||
3307 (filp->f_flags & O_ACCMODE) == O_RDWR) {
3308 clear_bit(IDETAPE_BUSY, &tape->flags);
3309 retval = -EROFS;
3310 goto out_put_tape;
3311 }
3312 }
3313
3314 /*
3315 * Lock the tape drive door so user can't eject.
3316 */
Borislav Petkov54abf372008-02-06 02:57:52 +01003317 if (tape->chrdev_dir == IDETAPE_DIR_NONE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003318 if (idetape_create_prevent_cmd(drive, &pc, 1)) {
3319 if (!idetape_queue_pc_tail(drive, &pc)) {
3320 if (tape->door_locked != DOOR_EXPLICITLY_LOCKED)
3321 tape->door_locked = DOOR_LOCKED;
3322 }
3323 }
3324 }
3325 idetape_restart_speed_control(drive);
3326 tape->restart_speed_control_req = 0;
3327 return 0;
3328
3329out_put_tape:
3330 ide_tape_put(tape);
3331 return retval;
3332}
3333
3334static void idetape_write_release (ide_drive_t *drive, unsigned int minor)
3335{
3336 idetape_tape_t *tape = drive->driver_data;
3337
3338 idetape_empty_write_pipeline(drive);
3339 tape->merge_stage = __idetape_kmalloc_stage(tape, 1, 0);
3340 if (tape->merge_stage != NULL) {
Borislav Petkov54bb2072008-02-06 02:57:52 +01003341 idetape_pad_zeros(drive, tape->blk_size *
3342 (tape->user_bs_factor - 1));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003343 __idetape_kfree_stage(tape->merge_stage);
3344 tape->merge_stage = NULL;
3345 }
3346 idetape_write_filemark(drive);
3347 idetape_flush_tape_buffers(drive);
3348 idetape_flush_tape_buffers(drive);
3349}
3350
3351/*
3352 * Our character device release function.
3353 */
3354static int idetape_chrdev_release (struct inode *inode, struct file *filp)
3355{
3356 struct ide_tape_obj *tape = ide_tape_f(filp);
3357 ide_drive_t *drive = tape->drive;
3358 idetape_pc_t pc;
3359 unsigned int minor = iminor(inode);
3360
3361 lock_kernel();
3362 tape = drive->driver_data;
Borislav Petkov8004a8c2008-02-06 02:57:51 +01003363
3364 debug_log(DBG_CHRDEV, "Enter %s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003365
Borislav Petkov54abf372008-02-06 02:57:52 +01003366 if (tape->chrdev_dir == IDETAPE_DIR_WRITE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003367 idetape_write_release(drive, minor);
Borislav Petkov54abf372008-02-06 02:57:52 +01003368 if (tape->chrdev_dir == IDETAPE_DIR_READ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003369 if (minor < 128)
3370 idetape_discard_read_pipeline(drive, 1);
3371 else
3372 idetape_wait_for_pipeline(drive);
3373 }
3374 if (tape->cache_stage != NULL) {
3375 __idetape_kfree_stage(tape->cache_stage);
3376 tape->cache_stage = NULL;
3377 }
3378 if (minor < 128 && test_bit(IDETAPE_MEDIUM_PRESENT, &tape->flags))
3379 (void) idetape_rewind_tape(drive);
Borislav Petkov54abf372008-02-06 02:57:52 +01003380 if (tape->chrdev_dir == IDETAPE_DIR_NONE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003381 if (tape->door_locked == DOOR_LOCKED) {
3382 if (idetape_create_prevent_cmd(drive, &pc, 0)) {
3383 if (!idetape_queue_pc_tail(drive, &pc))
3384 tape->door_locked = DOOR_UNLOCKED;
3385 }
3386 }
3387 }
3388 clear_bit(IDETAPE_BUSY, &tape->flags);
3389 ide_tape_put(tape);
3390 unlock_kernel();
3391 return 0;
3392}
3393
3394/*
3395 * idetape_identify_device is called to check the contents of the
3396 * ATAPI IDENTIFY command results. We return:
3397 *
3398 * 1 If the tape can be supported by us, based on the information
3399 * we have so far.
3400 *
3401 * 0 If this tape driver is not currently supported by us.
3402 */
3403static int idetape_identify_device (ide_drive_t *drive)
3404{
3405 struct idetape_id_gcw gcw;
3406 struct hd_driveid *id = drive->id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003407
3408 if (drive->id_read == 0)
3409 return 1;
3410
3411 *((unsigned short *) &gcw) = id->config;
3412
Linus Torvalds1da177e2005-04-16 15:20:36 -07003413 /* Check that we can support this device */
3414
Bartlomiej Zolnierkiewicz16422de32008-02-02 19:56:48 +01003415 if (gcw.protocol != 2)
3416 printk(KERN_ERR "ide-tape: Protocol (0x%02x) is not ATAPI\n",
3417 gcw.protocol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003418 else if (gcw.device_type != 1)
Bartlomiej Zolnierkiewicz16422de32008-02-02 19:56:48 +01003419 printk(KERN_ERR "ide-tape: Device type (0x%02x) is not set "
3420 "to tape\n", gcw.device_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003421 else if (!gcw.removable)
3422 printk(KERN_ERR "ide-tape: The removable flag is not set\n");
3423 else if (gcw.packet_size != 0) {
Bartlomiej Zolnierkiewicz16422de32008-02-02 19:56:48 +01003424 printk(KERN_ERR "ide-tape: Packet size (0x%02x) is not 12 "
3425 "bytes long\n", gcw.packet_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003426 } else
3427 return 1;
3428 return 0;
3429}
3430
Borislav Petkov6d29c8f2008-02-02 19:56:49 +01003431static void idetape_get_inquiry_results(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003432{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003433 idetape_tape_t *tape = drive->driver_data;
3434 idetape_pc_t pc;
Borislav Petkov41f81d542008-02-06 02:57:52 +01003435 char fw_rev[6], vendor_id[10], product_id[18];
Borislav Petkov6d29c8f2008-02-02 19:56:49 +01003436
Linus Torvalds1da177e2005-04-16 15:20:36 -07003437 idetape_create_inquiry_cmd(&pc);
3438 if (idetape_queue_pc_tail(drive, &pc)) {
Borislav Petkov6d29c8f2008-02-02 19:56:49 +01003439 printk(KERN_ERR "ide-tape: %s: can't get INQUIRY results\n",
3440 tape->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003441 return;
3442 }
Borislav Petkov41f81d542008-02-06 02:57:52 +01003443 memcpy(vendor_id, &pc.buffer[8], 8);
3444 memcpy(product_id, &pc.buffer[16], 16);
3445 memcpy(fw_rev, &pc.buffer[32], 4);
Borislav Petkov6d29c8f2008-02-02 19:56:49 +01003446
Borislav Petkov41f81d542008-02-06 02:57:52 +01003447 ide_fixstring(vendor_id, 10, 0);
3448 ide_fixstring(product_id, 18, 0);
3449 ide_fixstring(fw_rev, 6, 0);
3450
Borislav Petkov6d29c8f2008-02-02 19:56:49 +01003451 printk(KERN_INFO "ide-tape: %s <-> %s: %s %s rev %s\n",
Borislav Petkov41f81d542008-02-06 02:57:52 +01003452 drive->name, tape->name, vendor_id, product_id, fw_rev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003453}
3454
3455/*
Borislav Petkovb6422012008-02-02 19:56:49 +01003456 * Ask the tape about its various parameters. In particular, we will adjust our
3457 * data transfer buffer size to the recommended value as returned by the tape.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003458 */
3459static void idetape_get_mode_sense_results (ide_drive_t *drive)
3460{
3461 idetape_tape_t *tape = drive->driver_data;
3462 idetape_pc_t pc;
Borislav Petkovb6422012008-02-02 19:56:49 +01003463 u8 *caps;
3464 u8 speed, max_speed;
Borislav Petkov47314fa2008-02-02 19:56:48 +01003465
Linus Torvalds1da177e2005-04-16 15:20:36 -07003466 idetape_create_mode_sense_cmd(&pc, IDETAPE_CAPABILITIES_PAGE);
3467 if (idetape_queue_pc_tail(drive, &pc)) {
Borislav Petkovb6422012008-02-02 19:56:49 +01003468 printk(KERN_ERR "ide-tape: Can't get tape parameters - assuming"
3469 " some default values\n");
Borislav Petkov54bb2072008-02-06 02:57:52 +01003470 tape->blk_size = 512;
Borislav Petkovb6422012008-02-02 19:56:49 +01003471 put_unaligned(52, (u16 *)&tape->caps[12]);
3472 put_unaligned(540, (u16 *)&tape->caps[14]);
3473 put_unaligned(6*52, (u16 *)&tape->caps[16]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003474 return;
3475 }
Borislav Petkovb6422012008-02-02 19:56:49 +01003476 caps = pc.buffer + 4 + pc.buffer[3];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003477
Borislav Petkovb6422012008-02-02 19:56:49 +01003478 /* convert to host order and save for later use */
3479 speed = be16_to_cpu(*(u16 *)&caps[14]);
3480 max_speed = be16_to_cpu(*(u16 *)&caps[8]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003481
Borislav Petkovb6422012008-02-02 19:56:49 +01003482 put_unaligned(max_speed, (u16 *)&caps[8]);
3483 put_unaligned(be16_to_cpu(*(u16 *)&caps[12]), (u16 *)&caps[12]);
3484 put_unaligned(speed, (u16 *)&caps[14]);
3485 put_unaligned(be16_to_cpu(*(u16 *)&caps[16]), (u16 *)&caps[16]);
3486
3487 if (!speed) {
3488 printk(KERN_INFO "ide-tape: %s: invalid tape speed "
3489 "(assuming 650KB/sec)\n", drive->name);
3490 put_unaligned(650, (u16 *)&caps[14]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003491 }
Borislav Petkovb6422012008-02-02 19:56:49 +01003492 if (!max_speed) {
3493 printk(KERN_INFO "ide-tape: %s: invalid max_speed "
3494 "(assuming 650KB/sec)\n", drive->name);
3495 put_unaligned(650, (u16 *)&caps[8]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003496 }
3497
Borislav Petkovb6422012008-02-02 19:56:49 +01003498 memcpy(&tape->caps, caps, 20);
3499 if (caps[7] & 0x02)
Borislav Petkov54bb2072008-02-06 02:57:52 +01003500 tape->blk_size = 512;
Borislav Petkovb6422012008-02-02 19:56:49 +01003501 else if (caps[7] & 0x04)
Borislav Petkov54bb2072008-02-06 02:57:52 +01003502 tape->blk_size = 1024;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003503}
3504
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +02003505#ifdef CONFIG_IDE_PROC_FS
Linus Torvalds1da177e2005-04-16 15:20:36 -07003506static void idetape_add_settings (ide_drive_t *drive)
3507{
3508 idetape_tape_t *tape = drive->driver_data;
3509
3510/*
Bartlomiej Zolnierkiewicz14979432007-05-10 00:01:10 +02003511 * drive setting name read/write data type min max mul_factor div_factor data pointer set function
Linus Torvalds1da177e2005-04-16 15:20:36 -07003512 */
Borislav Petkovb6422012008-02-02 19:56:49 +01003513 ide_add_setting(drive, "buffer", SETTING_READ, TYPE_SHORT, 0, 0xffff,
3514 1, 2, (u16 *)&tape->caps[16], NULL);
Bartlomiej Zolnierkiewicz14979432007-05-10 00:01:10 +02003515 ide_add_setting(drive, "pipeline_min", SETTING_RW, TYPE_INT, 1, 0xffff, tape->stage_size / 1024, 1, &tape->min_pipeline, NULL);
3516 ide_add_setting(drive, "pipeline", SETTING_RW, TYPE_INT, 1, 0xffff, tape->stage_size / 1024, 1, &tape->max_stages, NULL);
3517 ide_add_setting(drive, "pipeline_max", SETTING_RW, TYPE_INT, 1, 0xffff, tape->stage_size / 1024, 1, &tape->max_pipeline, NULL);
3518 ide_add_setting(drive, "pipeline_used", SETTING_READ, TYPE_INT, 0, 0xffff, tape->stage_size / 1024, 1, &tape->nr_stages, NULL);
3519 ide_add_setting(drive, "pipeline_pending", SETTING_READ, TYPE_INT, 0, 0xffff, tape->stage_size / 1024, 1, &tape->nr_pending_stages, NULL);
Borislav Petkovb6422012008-02-02 19:56:49 +01003520 ide_add_setting(drive, "speed", SETTING_READ, TYPE_SHORT, 0, 0xffff,
3521 1, 1, (u16 *)&tape->caps[14], NULL);
Borislav Petkov54bb2072008-02-06 02:57:52 +01003522 ide_add_setting(drive, "stage", SETTING_READ, TYPE_INT, 0, 0xffff, 1,
3523 1024, &tape->stage_size, NULL);
3524 ide_add_setting(drive, "tdsc", SETTING_RW, TYPE_INT, IDETAPE_DSC_RW_MIN,
3525 IDETAPE_DSC_RW_MAX, 1000, HZ, &tape->best_dsc_rw_freq,
3526 NULL);
Bartlomiej Zolnierkiewicz14979432007-05-10 00:01:10 +02003527 ide_add_setting(drive, "dsc_overlap", SETTING_RW, TYPE_BYTE, 0, 1, 1, 1, &drive->dsc_overlap, NULL);
3528 ide_add_setting(drive, "pipeline_head_speed_c",SETTING_READ, TYPE_INT, 0, 0xffff, 1, 1, &tape->controlled_pipeline_head_speed, NULL);
3529 ide_add_setting(drive, "pipeline_head_speed_u",SETTING_READ, TYPE_INT, 0, 0xffff, 1, 1, &tape->uncontrolled_pipeline_head_speed,NULL);
3530 ide_add_setting(drive, "avg_speed", SETTING_READ, TYPE_INT, 0, 0xffff, 1, 1, &tape->avg_speed, NULL);
Borislav Petkov8004a8c2008-02-06 02:57:51 +01003531 ide_add_setting(drive, "debug_mask", SETTING_RW, TYPE_INT, 0, 0xffff, 1,
3532 1, &tape->debug_mask, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003533}
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +02003534#else
3535static inline void idetape_add_settings(ide_drive_t *drive) { ; }
3536#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003537
3538/*
3539 * ide_setup is called to:
3540 *
3541 * 1. Initialize our various state variables.
3542 * 2. Ask the tape for its capabilities.
3543 * 3. Allocate a buffer which will be used for data
3544 * transfer. The buffer size is chosen based on
3545 * the recommendation which we received in step (2).
3546 *
3547 * Note that at this point ide.c already assigned us an irq, so that
3548 * we can queue requests here and wait for their completion.
3549 */
3550static void idetape_setup (ide_drive_t *drive, idetape_tape_t *tape, int minor)
3551{
3552 unsigned long t1, tmid, tn, t;
3553 int speed;
3554 struct idetape_id_gcw gcw;
3555 int stage_size;
3556 struct sysinfo si;
Borislav Petkovb6422012008-02-02 19:56:49 +01003557 u16 *ctl = (u16 *)&tape->caps[12];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003558
Borislav Petkov54bb2072008-02-06 02:57:52 +01003559 spin_lock_init(&tape->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003560 drive->dsc_overlap = 1;
Bartlomiej Zolnierkiewicz4166c192008-02-01 23:09:30 +01003561 if (drive->hwif->host_flags & IDE_HFLAG_NO_DSC) {
3562 printk(KERN_INFO "ide-tape: %s: disabling DSC overlap\n",
3563 tape->name);
3564 drive->dsc_overlap = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003565 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003566 /* Seagate Travan drives do not support DSC overlap. */
3567 if (strstr(drive->id->model, "Seagate STT3401"))
3568 drive->dsc_overlap = 0;
3569 tape->minor = minor;
3570 tape->name[0] = 'h';
3571 tape->name[1] = 't';
3572 tape->name[2] = '0' + minor;
Borislav Petkov54abf372008-02-06 02:57:52 +01003573 tape->chrdev_dir = IDETAPE_DIR_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003574 tape->pc = tape->pc_stack;
3575 tape->max_insert_speed = 10000;
3576 tape->speed_control = 1;
3577 *((unsigned short *) &gcw) = drive->id->config;
3578 if (gcw.drq_type == 1)
3579 set_bit(IDETAPE_DRQ_INTERRUPT, &tape->flags);
3580
3581 tape->min_pipeline = tape->max_pipeline = tape->max_stages = 10;
3582
3583 idetape_get_inquiry_results(drive);
3584 idetape_get_mode_sense_results(drive);
Borislav Petkov3cffb9c2008-02-02 19:56:50 +01003585 ide_tape_get_bsize_from_bdesc(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003586 tape->user_bs_factor = 1;
Borislav Petkov54bb2072008-02-06 02:57:52 +01003587 tape->stage_size = *ctl * tape->blk_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003588 while (tape->stage_size > 0xffff) {
3589 printk(KERN_NOTICE "ide-tape: decreasing stage size\n");
Borislav Petkovb6422012008-02-02 19:56:49 +01003590 *ctl /= 2;
Borislav Petkov54bb2072008-02-06 02:57:52 +01003591 tape->stage_size = *ctl * tape->blk_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003592 }
3593 stage_size = tape->stage_size;
3594 tape->pages_per_stage = stage_size / PAGE_SIZE;
3595 if (stage_size % PAGE_SIZE) {
3596 tape->pages_per_stage++;
3597 tape->excess_bh_size = PAGE_SIZE - stage_size % PAGE_SIZE;
3598 }
3599
Borislav Petkovb6422012008-02-02 19:56:49 +01003600 /* Select the "best" DSC read/write polling freq and pipeline size. */
3601 speed = max(*(u16 *)&tape->caps[14], *(u16 *)&tape->caps[8]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003602
3603 tape->max_stages = speed * 1000 * 10 / tape->stage_size;
3604
3605 /*
3606 * Limit memory use for pipeline to 10% of physical memory
3607 */
3608 si_meminfo(&si);
3609 if (tape->max_stages * tape->stage_size > si.totalram * si.mem_unit / 10)
3610 tape->max_stages = si.totalram * si.mem_unit / (10 * tape->stage_size);
3611 tape->max_stages = min(tape->max_stages, IDETAPE_MAX_PIPELINE_STAGES);
3612 tape->min_pipeline = min(tape->max_stages, IDETAPE_MIN_PIPELINE_STAGES);
3613 tape->max_pipeline = min(tape->max_stages * 2, IDETAPE_MAX_PIPELINE_STAGES);
3614 if (tape->max_stages == 0)
3615 tape->max_stages = tape->min_pipeline = tape->max_pipeline = 1;
3616
3617 t1 = (tape->stage_size * HZ) / (speed * 1000);
Borislav Petkovb6422012008-02-02 19:56:49 +01003618 tmid = (*(u16 *)&tape->caps[16] * 32 * HZ) / (speed * 125);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003619 tn = (IDETAPE_FIFO_THRESHOLD * tape->stage_size * HZ) / (speed * 1000);
3620
3621 if (tape->max_stages)
3622 t = tn;
3623 else
3624 t = t1;
3625
3626 /*
3627 * Ensure that the number we got makes sense; limit
3628 * it within IDETAPE_DSC_RW_MIN and IDETAPE_DSC_RW_MAX.
3629 */
Borislav Petkov54bb2072008-02-06 02:57:52 +01003630 tape->best_dsc_rw_freq = max_t(unsigned long,
3631 min_t(unsigned long, t, IDETAPE_DSC_RW_MAX),
3632 IDETAPE_DSC_RW_MIN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003633 printk(KERN_INFO "ide-tape: %s <-> %s: %dKBps, %d*%dkB buffer, "
3634 "%dkB pipeline, %lums tDSC%s\n",
Borislav Petkovb6422012008-02-02 19:56:49 +01003635 drive->name, tape->name, *(u16 *)&tape->caps[14],
3636 (*(u16 *)&tape->caps[16] * 512) / tape->stage_size,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003637 tape->stage_size / 1024,
3638 tape->max_stages * tape->stage_size / 1024,
Borislav Petkov54bb2072008-02-06 02:57:52 +01003639 tape->best_dsc_rw_freq * 1000 / HZ,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003640 drive->using_dma ? ", DMA":"");
3641
3642 idetape_add_settings(drive);
3643}
3644
Russell King4031bbe2006-01-06 11:41:00 +00003645static void ide_tape_remove(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003646{
3647 idetape_tape_t *tape = drive->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003648
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +02003649 ide_proc_unregister_driver(drive, tape->driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003650
3651 ide_unregister_region(tape->disk);
3652
3653 ide_tape_put(tape);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003654}
3655
3656static void ide_tape_release(struct kref *kref)
3657{
3658 struct ide_tape_obj *tape = to_ide_tape(kref);
3659 ide_drive_t *drive = tape->drive;
3660 struct gendisk *g = tape->disk;
3661
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02003662 BUG_ON(tape->first_stage != NULL || tape->merge_stage_size);
3663
Linus Torvalds1da177e2005-04-16 15:20:36 -07003664 drive->dsc_overlap = 0;
3665 drive->driver_data = NULL;
Tony Jonesdbc12722007-09-25 02:03:03 +02003666 device_destroy(idetape_sysfs_class, MKDEV(IDETAPE_MAJOR, tape->minor));
3667 device_destroy(idetape_sysfs_class, MKDEV(IDETAPE_MAJOR, tape->minor + 128));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003668 idetape_devs[tape->minor] = NULL;
3669 g->private_data = NULL;
3670 put_disk(g);
3671 kfree(tape);
3672}
3673
Bartlomiej Zolnierkiewiczecfd80e2007-05-10 00:01:09 +02003674#ifdef CONFIG_IDE_PROC_FS
Linus Torvalds1da177e2005-04-16 15:20:36 -07003675static int proc_idetape_read_name
3676 (char *page, char **start, off_t off, int count, int *eof, void *data)
3677{
3678 ide_drive_t *drive = (ide_drive_t *) data;
3679 idetape_tape_t *tape = drive->driver_data;
3680 char *out = page;
3681 int len;
3682
3683 len = sprintf(out, "%s\n", tape->name);
3684 PROC_IDE_READ_RETURN(page, start, off, count, eof, len);
3685}
3686
3687static ide_proc_entry_t idetape_proc[] = {
3688 { "capacity", S_IFREG|S_IRUGO, proc_ide_read_capacity, NULL },
3689 { "name", S_IFREG|S_IRUGO, proc_idetape_read_name, NULL },
3690 { NULL, 0, NULL, NULL }
3691};
Linus Torvalds1da177e2005-04-16 15:20:36 -07003692#endif
3693
Russell King4031bbe2006-01-06 11:41:00 +00003694static int ide_tape_probe(ide_drive_t *);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003695
Linus Torvalds1da177e2005-04-16 15:20:36 -07003696static ide_driver_t idetape_driver = {
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02003697 .gen_driver = {
Laurent Riffard4ef3b8f2005-11-18 22:15:40 +01003698 .owner = THIS_MODULE,
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02003699 .name = "ide-tape",
3700 .bus = &ide_bus_type,
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02003701 },
Russell King4031bbe2006-01-06 11:41:00 +00003702 .probe = ide_tape_probe,
3703 .remove = ide_tape_remove,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003704 .version = IDETAPE_VERSION,
3705 .media = ide_tape,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003706 .supports_dsc_overlap = 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003707 .do_request = idetape_do_request,
3708 .end_request = idetape_end_request,
3709 .error = __ide_error,
3710 .abort = __ide_abort,
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +02003711#ifdef CONFIG_IDE_PROC_FS
Linus Torvalds1da177e2005-04-16 15:20:36 -07003712 .proc = idetape_proc,
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +02003713#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003714};
3715
3716/*
3717 * Our character device supporting functions, passed to register_chrdev.
3718 */
Arjan van de Ven2b8693c2007-02-12 00:55:32 -08003719static const struct file_operations idetape_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003720 .owner = THIS_MODULE,
3721 .read = idetape_chrdev_read,
3722 .write = idetape_chrdev_write,
3723 .ioctl = idetape_chrdev_ioctl,
3724 .open = idetape_chrdev_open,
3725 .release = idetape_chrdev_release,
3726};
3727
3728static int idetape_open(struct inode *inode, struct file *filp)
3729{
3730 struct gendisk *disk = inode->i_bdev->bd_disk;
3731 struct ide_tape_obj *tape;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003732
3733 if (!(tape = ide_tape_get(disk)))
3734 return -ENXIO;
3735
Linus Torvalds1da177e2005-04-16 15:20:36 -07003736 return 0;
3737}
3738
3739static int idetape_release(struct inode *inode, struct file *filp)
3740{
3741 struct gendisk *disk = inode->i_bdev->bd_disk;
3742 struct ide_tape_obj *tape = ide_tape_g(disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003743
3744 ide_tape_put(tape);
3745
3746 return 0;
3747}
3748
3749static int idetape_ioctl(struct inode *inode, struct file *file,
3750 unsigned int cmd, unsigned long arg)
3751{
3752 struct block_device *bdev = inode->i_bdev;
3753 struct ide_tape_obj *tape = ide_tape_g(bdev->bd_disk);
3754 ide_drive_t *drive = tape->drive;
3755 int err = generic_ide_ioctl(drive, file, bdev, cmd, arg);
3756 if (err == -EINVAL)
3757 err = idetape_blkdev_ioctl(drive, cmd, arg);
3758 return err;
3759}
3760
3761static struct block_device_operations idetape_block_ops = {
3762 .owner = THIS_MODULE,
3763 .open = idetape_open,
3764 .release = idetape_release,
3765 .ioctl = idetape_ioctl,
3766};
3767
Russell King4031bbe2006-01-06 11:41:00 +00003768static int ide_tape_probe(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003769{
3770 idetape_tape_t *tape;
3771 struct gendisk *g;
3772 int minor;
3773
3774 if (!strstr("ide-tape", drive->driver_req))
3775 goto failed;
3776 if (!drive->present)
3777 goto failed;
3778 if (drive->media != ide_tape)
3779 goto failed;
3780 if (!idetape_identify_device (drive)) {
3781 printk(KERN_ERR "ide-tape: %s: not supported by this version of ide-tape\n", drive->name);
3782 goto failed;
3783 }
3784 if (drive->scsi) {
3785 printk("ide-tape: passing drive %s to ide-scsi emulation.\n", drive->name);
3786 goto failed;
3787 }
3788 if (strstr(drive->id->model, "OnStream DI-")) {
3789 printk(KERN_WARNING "ide-tape: Use drive %s with ide-scsi emulation and osst.\n", drive->name);
3790 printk(KERN_WARNING "ide-tape: OnStream support will be removed soon from ide-tape!\n");
3791 }
Robert P. J. Day5cbded52006-12-13 00:35:56 -08003792 tape = kzalloc(sizeof (idetape_tape_t), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003793 if (tape == NULL) {
3794 printk(KERN_ERR "ide-tape: %s: Can't allocate a tape structure\n", drive->name);
3795 goto failed;
3796 }
3797
3798 g = alloc_disk(1 << PARTN_BITS);
3799 if (!g)
3800 goto out_free_tape;
3801
3802 ide_init_disk(g, drive);
3803
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +02003804 ide_proc_register_driver(drive, &idetape_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003805
Linus Torvalds1da177e2005-04-16 15:20:36 -07003806 kref_init(&tape->kref);
3807
3808 tape->drive = drive;
3809 tape->driver = &idetape_driver;
3810 tape->disk = g;
3811
3812 g->private_data = &tape->driver;
3813
3814 drive->driver_data = tape;
3815
Arjan van de Vencf8b8972006-03-23 03:00:45 -08003816 mutex_lock(&idetape_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003817 for (minor = 0; idetape_devs[minor]; minor++)
3818 ;
3819 idetape_devs[minor] = tape;
Arjan van de Vencf8b8972006-03-23 03:00:45 -08003820 mutex_unlock(&idetape_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003821
3822 idetape_setup(drive, tape, minor);
3823
Tony Jonesdbc12722007-09-25 02:03:03 +02003824 device_create(idetape_sysfs_class, &drive->gendev,
3825 MKDEV(IDETAPE_MAJOR, minor), "%s", tape->name);
3826 device_create(idetape_sysfs_class, &drive->gendev,
3827 MKDEV(IDETAPE_MAJOR, minor + 128), "n%s", tape->name);
Will Dysond5dee802005-09-16 02:55:07 -07003828
Linus Torvalds1da177e2005-04-16 15:20:36 -07003829 g->fops = &idetape_block_ops;
3830 ide_register_region(g);
3831
3832 return 0;
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02003833
Linus Torvalds1da177e2005-04-16 15:20:36 -07003834out_free_tape:
3835 kfree(tape);
3836failed:
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02003837 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003838}
3839
3840MODULE_DESCRIPTION("ATAPI Streaming TAPE Driver");
3841MODULE_LICENSE("GPL");
3842
3843static void __exit idetape_exit (void)
3844{
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02003845 driver_unregister(&idetape_driver.gen_driver);
Will Dysond5dee802005-09-16 02:55:07 -07003846 class_destroy(idetape_sysfs_class);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003847 unregister_chrdev(IDETAPE_MAJOR, "ht");
3848}
3849
Bartlomiej Zolnierkiewicz17514e82005-11-19 22:24:35 +01003850static int __init idetape_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003851{
Will Dysond5dee802005-09-16 02:55:07 -07003852 int error = 1;
3853 idetape_sysfs_class = class_create(THIS_MODULE, "ide_tape");
3854 if (IS_ERR(idetape_sysfs_class)) {
3855 idetape_sysfs_class = NULL;
3856 printk(KERN_ERR "Unable to create sysfs class for ide tapes\n");
3857 error = -EBUSY;
3858 goto out;
3859 }
3860
Linus Torvalds1da177e2005-04-16 15:20:36 -07003861 if (register_chrdev(IDETAPE_MAJOR, "ht", &idetape_fops)) {
3862 printk(KERN_ERR "ide-tape: Failed to register character device interface\n");
Will Dysond5dee802005-09-16 02:55:07 -07003863 error = -EBUSY;
3864 goto out_free_class;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003865 }
Will Dysond5dee802005-09-16 02:55:07 -07003866
3867 error = driver_register(&idetape_driver.gen_driver);
3868 if (error)
3869 goto out_free_driver;
3870
3871 return 0;
3872
3873out_free_driver:
3874 driver_unregister(&idetape_driver.gen_driver);
3875out_free_class:
3876 class_destroy(idetape_sysfs_class);
3877out:
3878 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003879}
3880
Kay Sievers263756e2005-12-12 18:03:44 +01003881MODULE_ALIAS("ide:*m-tape*");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003882module_init(idetape_init);
3883module_exit(idetape_exit);
3884MODULE_ALIAS_CHARDEV_MAJOR(IDETAPE_MAJOR);