blob: 73f06c859301d332085d5c2663672fab98924fb7 [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 */
302 unsigned long best_dsc_rw_frequency;
303 /* The current polling frequency */
304 unsigned long dsc_polling_frequency;
305 /* Maximum waiting time */
306 unsigned long dsc_timeout;
307
308 /*
309 * Read position information
310 */
311 u8 partition;
312 /* Current block */
313 unsigned int first_frame_position;
314 unsigned int last_frame_position;
315 unsigned int blocks_in_buffer;
316
317 /*
318 * Last error information
319 */
320 u8 sense_key, asc, ascq;
321
322 /*
323 * Character device operation
324 */
325 unsigned int minor;
326 /* device name */
327 char name[4];
328 /* Current character device data transfer direction */
Borislav Petkov54abf372008-02-06 02:57:52 +0100329 u8 chrdev_dir;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330
331 /*
332 * Device information
333 */
334 /* Usually 512 or 1024 bytes */
335 unsigned short tape_block_size;
336 int user_bs_factor;
Borislav Petkovb6422012008-02-02 19:56:49 +0100337
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 /* Copy of the tape's Capabilities and Mechanical Page */
Borislav Petkovb6422012008-02-02 19:56:49 +0100339 u8 caps[20];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340
341 /*
342 * Active data transfer request parameters.
343 *
344 * At most, there is only one ide-tape originated data transfer
345 * request in the device request queue. This allows ide.c to
346 * easily service requests from the other device when we
347 * postpone our active request. In the pipelined operation
348 * mode, we use our internal pipeline structure to hold
349 * more data requests.
350 *
351 * The data buffer size is chosen based on the tape's
352 * recommendation.
353 */
354 /* Pointer to the request which is waiting in the device request queue */
355 struct request *active_data_request;
356 /* Data buffer size (chosen based on the tape's recommendation */
357 int stage_size;
358 idetape_stage_t *merge_stage;
359 int merge_stage_size;
360 struct idetape_bh *bh;
361 char *b_data;
362 int b_count;
363
364 /*
365 * Pipeline parameters.
366 *
367 * To accomplish non-pipelined mode, we simply set the following
368 * variables to zero (or NULL, where appropriate).
369 */
370 /* Number of currently used stages */
371 int nr_stages;
372 /* Number of pending stages */
373 int nr_pending_stages;
374 /* We will not allocate more than this number of stages */
375 int max_stages, min_pipeline, max_pipeline;
376 /* The first stage which will be removed from the pipeline */
377 idetape_stage_t *first_stage;
378 /* The currently active stage */
379 idetape_stage_t *active_stage;
380 /* Will be serviced after the currently active request */
381 idetape_stage_t *next_stage;
382 /* New requests will be added to the pipeline here */
383 idetape_stage_t *last_stage;
384 /* Optional free stage which we can use */
385 idetape_stage_t *cache_stage;
386 int pages_per_stage;
387 /* Wasted space in each stage */
388 int excess_bh_size;
389
390 /* Status/Action flags: long for set_bit */
391 unsigned long flags;
392 /* protects the ide-tape queue */
393 spinlock_t spinlock;
394
395 /*
396 * Measures average tape speed
397 */
398 unsigned long avg_time;
399 int avg_size;
400 int avg_speed;
401
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 char vendor_id[10];
403 char product_id[18];
404 char firmware_revision[6];
405 int firmware_revision_num;
406
407 /* the door is currently locked */
408 int door_locked;
409 /* the tape hardware is write protected */
410 char drv_write_prot;
411 /* the tape is write protected (hardware or opened as read-only) */
412 char write_prot;
413
414 /*
415 * Limit the number of times a request can
416 * be postponed, to avoid an infinite postpone
417 * deadlock.
418 */
419 /* request postpone count limit */
420 int postpone_cnt;
421
422 /*
423 * Measures number of frames:
424 *
425 * 1. written/read to/from the driver pipeline (pipeline_head).
426 * 2. written/read to/from the tape buffers (idetape_bh).
427 * 3. written/read by the tape to/from the media (tape_head).
428 */
429 int pipeline_head;
430 int buffer_head;
431 int tape_head;
432 int last_tape_head;
433
434 /*
435 * Speed control at the tape buffers input/output
436 */
437 unsigned long insert_time;
438 int insert_size;
439 int insert_speed;
440 int max_insert_speed;
441 int measure_insert_time;
442
443 /*
444 * Measure tape still time, in milliseconds
445 */
446 unsigned long tape_still_time_begin;
447 int tape_still_time;
448
449 /*
450 * Speed regulation negative feedback loop
451 */
452 int speed_control;
453 int pipeline_head_speed;
454 int controlled_pipeline_head_speed;
455 int uncontrolled_pipeline_head_speed;
456 int controlled_last_pipeline_head;
457 int uncontrolled_last_pipeline_head;
458 unsigned long uncontrolled_pipeline_head_time;
459 unsigned long controlled_pipeline_head_time;
460 int controlled_previous_pipeline_head;
461 int uncontrolled_previous_pipeline_head;
462 unsigned long controlled_previous_head_time;
463 unsigned long uncontrolled_previous_head_time;
464 int restart_speed_control_req;
465
Borislav Petkov8004a8c2008-02-06 02:57:51 +0100466 u32 debug_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467} idetape_tape_t;
468
Arjan van de Vencf8b8972006-03-23 03:00:45 -0800469static DEFINE_MUTEX(idetape_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470
Will Dysond5dee802005-09-16 02:55:07 -0700471static struct class *idetape_sysfs_class;
472
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473#define to_ide_tape(obj) container_of(obj, struct ide_tape_obj, kref)
474
475#define ide_tape_g(disk) \
476 container_of((disk)->private_data, struct ide_tape_obj, driver)
477
478static struct ide_tape_obj *ide_tape_get(struct gendisk *disk)
479{
480 struct ide_tape_obj *tape = NULL;
481
Arjan van de Vencf8b8972006-03-23 03:00:45 -0800482 mutex_lock(&idetape_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 tape = ide_tape_g(disk);
484 if (tape)
485 kref_get(&tape->kref);
Arjan van de Vencf8b8972006-03-23 03:00:45 -0800486 mutex_unlock(&idetape_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487 return tape;
488}
489
490static void ide_tape_release(struct kref *);
491
492static void ide_tape_put(struct ide_tape_obj *tape)
493{
Arjan van de Vencf8b8972006-03-23 03:00:45 -0800494 mutex_lock(&idetape_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 kref_put(&tape->kref, ide_tape_release);
Arjan van de Vencf8b8972006-03-23 03:00:45 -0800496 mutex_unlock(&idetape_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497}
498
499/*
500 * Tape door status
501 */
502#define DOOR_UNLOCKED 0
503#define DOOR_LOCKED 1
504#define DOOR_EXPLICITLY_LOCKED 2
505
506/*
507 * Tape flag bits values.
508 */
509#define IDETAPE_IGNORE_DSC 0
510#define IDETAPE_ADDRESS_VALID 1 /* 0 When the tape position is unknown */
511#define IDETAPE_BUSY 2 /* Device already opened */
512#define IDETAPE_PIPELINE_ERROR 3 /* Error detected in a pipeline stage */
513#define IDETAPE_DETECT_BS 4 /* Attempt to auto-detect the current user block size */
514#define IDETAPE_FILEMARK 5 /* Currently on a filemark */
515#define IDETAPE_DRQ_INTERRUPT 6 /* DRQ interrupt device */
516#define IDETAPE_READ_ERROR 7
517#define IDETAPE_PIPELINE_ACTIVE 8 /* pipeline active */
518/* 0 = no tape is loaded, so we don't rewind after ejecting */
519#define IDETAPE_MEDIUM_PRESENT 9
520
521/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 * Some defines for the READ BUFFER command
523 */
524#define IDETAPE_RETRIEVE_FAULTY_BLOCK 6
525
526/*
527 * Some defines for the SPACE command
528 */
529#define IDETAPE_SPACE_OVER_FILEMARK 1
530#define IDETAPE_SPACE_TO_EOD 3
531
532/*
533 * Some defines for the LOAD UNLOAD command
534 */
535#define IDETAPE_LU_LOAD_MASK 1
536#define IDETAPE_LU_RETENSION_MASK 2
537#define IDETAPE_LU_EOT_MASK 4
538
539/*
540 * Special requests for our block device strategy routine.
541 *
542 * In order to service a character device command, we add special
543 * requests to the tail of our block device request queue and wait
544 * for their completion.
545 */
546
547enum {
548 REQ_IDETAPE_PC1 = (1 << 0), /* packet command (first stage) */
549 REQ_IDETAPE_PC2 = (1 << 1), /* packet command (second stage) */
550 REQ_IDETAPE_READ = (1 << 2),
551 REQ_IDETAPE_WRITE = (1 << 3),
552 REQ_IDETAPE_READ_BUFFER = (1 << 4),
553};
554
555/*
556 * Error codes which are returned in rq->errors to the higher part
557 * of the driver.
558 */
559#define IDETAPE_ERROR_GENERAL 101
560#define IDETAPE_ERROR_FILEMARK 102
561#define IDETAPE_ERROR_EOD 103
562
563/*
564 * The following is used to format the general configuration word of
565 * the ATAPI IDENTIFY DEVICE command.
566 */
567struct idetape_id_gcw {
568 unsigned packet_size :2; /* Packet Size */
569 unsigned reserved234 :3; /* Reserved */
570 unsigned drq_type :2; /* Command packet DRQ type */
571 unsigned removable :1; /* Removable media */
572 unsigned device_type :5; /* Device type */
573 unsigned reserved13 :1; /* Reserved */
574 unsigned protocol :2; /* Protocol type */
575};
576
Borislav Petkovfa366252008-02-02 19:56:51 +0100577/* Structures related to the SELECT SENSE / MODE SENSE packet commands. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578#define IDETAPE_BLOCK_DESCRIPTOR 0
579#define IDETAPE_CAPABILITIES_PAGE 0x2a
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580
581/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 * The variables below are used for the character device interface.
583 * Additional state variables are defined in our ide_drive_t structure.
584 */
585static struct ide_tape_obj * idetape_devs[MAX_HWIFS * MAX_DRIVES];
586
587#define ide_tape_f(file) ((file)->private_data)
588
589static struct ide_tape_obj *ide_tape_chrdev_get(unsigned int i)
590{
591 struct ide_tape_obj *tape = NULL;
592
Arjan van de Vencf8b8972006-03-23 03:00:45 -0800593 mutex_lock(&idetape_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 tape = idetape_devs[i];
595 if (tape)
596 kref_get(&tape->kref);
Arjan van de Vencf8b8972006-03-23 03:00:45 -0800597 mutex_unlock(&idetape_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 return tape;
599}
600
601/*
602 * Function declarations
603 *
604 */
605static int idetape_chrdev_release (struct inode *inode, struct file *filp);
606static void idetape_write_release (ide_drive_t *drive, unsigned int minor);
607
608/*
609 * Too bad. The drive wants to send us data which we are not ready to accept.
610 * Just throw it away.
611 */
612static void idetape_discard_data (ide_drive_t *drive, unsigned int bcount)
613{
614 while (bcount--)
615 (void) HWIF(drive)->INB(IDE_DATA_REG);
616}
617
618static void idetape_input_buffers (ide_drive_t *drive, idetape_pc_t *pc, unsigned int bcount)
619{
620 struct idetape_bh *bh = pc->bh;
621 int count;
622
623 while (bcount) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624 if (bh == NULL) {
625 printk(KERN_ERR "ide-tape: bh == NULL in "
626 "idetape_input_buffers\n");
627 idetape_discard_data(drive, bcount);
628 return;
629 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630 count = min((unsigned int)(bh->b_size - atomic_read(&bh->b_count)), bcount);
631 HWIF(drive)->atapi_input_bytes(drive, bh->b_data + atomic_read(&bh->b_count), count);
632 bcount -= count;
633 atomic_add(count, &bh->b_count);
634 if (atomic_read(&bh->b_count) == bh->b_size) {
635 bh = bh->b_reqnext;
636 if (bh)
637 atomic_set(&bh->b_count, 0);
638 }
639 }
640 pc->bh = bh;
641}
642
643static void idetape_output_buffers (ide_drive_t *drive, idetape_pc_t *pc, unsigned int bcount)
644{
645 struct idetape_bh *bh = pc->bh;
646 int count;
647
648 while (bcount) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 if (bh == NULL) {
650 printk(KERN_ERR "ide-tape: bh == NULL in "
651 "idetape_output_buffers\n");
652 return;
653 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 count = min((unsigned int)pc->b_count, (unsigned int)bcount);
655 HWIF(drive)->atapi_output_bytes(drive, pc->b_data, count);
656 bcount -= count;
657 pc->b_data += count;
658 pc->b_count -= count;
659 if (!pc->b_count) {
660 pc->bh = bh = bh->b_reqnext;
661 if (bh) {
662 pc->b_data = bh->b_data;
663 pc->b_count = atomic_read(&bh->b_count);
664 }
665 }
666 }
667}
668
669static void idetape_update_buffers (idetape_pc_t *pc)
670{
671 struct idetape_bh *bh = pc->bh;
672 int count;
673 unsigned int bcount = pc->actually_transferred;
674
675 if (test_bit(PC_WRITING, &pc->flags))
676 return;
677 while (bcount) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 if (bh == NULL) {
679 printk(KERN_ERR "ide-tape: bh == NULL in "
680 "idetape_update_buffers\n");
681 return;
682 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 count = min((unsigned int)bh->b_size, (unsigned int)bcount);
684 atomic_set(&bh->b_count, count);
685 if (atomic_read(&bh->b_count) == bh->b_size)
686 bh = bh->b_reqnext;
687 bcount -= count;
688 }
689 pc->bh = bh;
690}
691
692/*
693 * idetape_next_pc_storage returns a pointer to a place in which we can
694 * safely store a packet command, even though we intend to leave the
695 * driver. A storage space for a maximum of IDETAPE_PC_STACK packet
696 * commands is allocated at initialization time.
697 */
698static idetape_pc_t *idetape_next_pc_storage (ide_drive_t *drive)
699{
700 idetape_tape_t *tape = drive->driver_data;
701
Borislav Petkov8004a8c2008-02-06 02:57:51 +0100702 debug_log(DBG_PCRQ_STACK, "pc_stack_index=%d\n", tape->pc_stack_index);
703
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 if (tape->pc_stack_index == IDETAPE_PC_STACK)
705 tape->pc_stack_index=0;
706 return (&tape->pc_stack[tape->pc_stack_index++]);
707}
708
709/*
710 * idetape_next_rq_storage is used along with idetape_next_pc_storage.
711 * Since we queue packet commands in the request queue, we need to
712 * allocate a request, along with the allocation of a packet command.
713 */
714
715/**************************************************************
716 * *
717 * This should get fixed to use kmalloc(.., GFP_ATOMIC) *
718 * followed later on by kfree(). -ml *
719 * *
720 **************************************************************/
721
722static struct request *idetape_next_rq_storage (ide_drive_t *drive)
723{
724 idetape_tape_t *tape = drive->driver_data;
725
Borislav Petkov8004a8c2008-02-06 02:57:51 +0100726 debug_log(DBG_PCRQ_STACK, "rq_stack_index=%d\n", tape->rq_stack_index);
727
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 if (tape->rq_stack_index == IDETAPE_PC_STACK)
729 tape->rq_stack_index=0;
730 return (&tape->rq_stack[tape->rq_stack_index++]);
731}
732
733/*
734 * idetape_init_pc initializes a packet command.
735 */
736static void idetape_init_pc (idetape_pc_t *pc)
737{
738 memset(pc->c, 0, 12);
739 pc->retries = 0;
740 pc->flags = 0;
741 pc->request_transfer = 0;
742 pc->buffer = pc->pc_buffer;
743 pc->buffer_size = IDETAPE_PC_BUFFER_SIZE;
744 pc->bh = NULL;
745 pc->b_data = NULL;
746}
747
748/*
Borislav Petkov1b5db432008-02-02 19:56:48 +0100749 * called on each failed packet command retry to analyze the request sense. We
750 * currently do not utilize this information.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751 */
Borislav Petkov1b5db432008-02-02 19:56:48 +0100752static void idetape_analyze_error(ide_drive_t *drive, u8 *sense)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753{
754 idetape_tape_t *tape = drive->driver_data;
755 idetape_pc_t *pc = tape->failed_pc;
756
Borislav Petkov1b5db432008-02-02 19:56:48 +0100757 tape->sense_key = sense[2] & 0xF;
758 tape->asc = sense[12];
759 tape->ascq = sense[13];
Borislav Petkov8004a8c2008-02-06 02:57:51 +0100760
761 debug_log(DBG_ERR, "pc = %x, sense key = %x, asc = %x, ascq = %x\n",
762 pc->c[0], tape->sense_key, tape->asc, tape->ascq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763
Borislav Petkov1b5db432008-02-02 19:56:48 +0100764 /* Correct pc->actually_transferred by asking the tape. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765 if (test_bit(PC_DMA_ERROR, &pc->flags)) {
Borislav Petkov1b5db432008-02-02 19:56:48 +0100766 pc->actually_transferred = pc->request_transfer -
767 tape->tape_block_size *
Borislav Petkov860ff5e2008-02-02 19:56:50 +0100768 be32_to_cpu(get_unaligned((u32 *)&sense[3]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769 idetape_update_buffers(pc);
770 }
771
772 /*
773 * If error was the result of a zero-length read or write command,
774 * with sense key=5, asc=0x22, ascq=0, let it slide. Some drives
775 * (i.e. Seagate STT3401A Travan) don't support 0-length read/writes.
776 */
Borislav Petkov90699ce2008-02-02 19:56:50 +0100777 if ((pc->c[0] == READ_6 || pc->c[0] == WRITE_6)
Borislav Petkov1b5db432008-02-02 19:56:48 +0100778 /* length == 0 */
779 && pc->c[4] == 0 && pc->c[3] == 0 && pc->c[2] == 0) {
780 if (tape->sense_key == 5) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781 /* don't report an error, everything's ok */
782 pc->error = 0;
783 /* don't retry read/write */
784 set_bit(PC_ABORT, &pc->flags);
785 }
786 }
Borislav Petkov90699ce2008-02-02 19:56:50 +0100787 if (pc->c[0] == READ_6 && (sense[2] & 0x80)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788 pc->error = IDETAPE_ERROR_FILEMARK;
789 set_bit(PC_ABORT, &pc->flags);
790 }
Borislav Petkov90699ce2008-02-02 19:56:50 +0100791 if (pc->c[0] == WRITE_6) {
Borislav Petkov1b5db432008-02-02 19:56:48 +0100792 if ((sense[2] & 0x40) || (tape->sense_key == 0xd
793 && tape->asc == 0x0 && tape->ascq == 0x2)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794 pc->error = IDETAPE_ERROR_EOD;
795 set_bit(PC_ABORT, &pc->flags);
796 }
797 }
Borislav Petkov90699ce2008-02-02 19:56:50 +0100798 if (pc->c[0] == READ_6 || pc->c[0] == WRITE_6) {
Borislav Petkov1b5db432008-02-02 19:56:48 +0100799 if (tape->sense_key == 8) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800 pc->error = IDETAPE_ERROR_EOD;
801 set_bit(PC_ABORT, &pc->flags);
802 }
803 if (!test_bit(PC_ABORT, &pc->flags) &&
804 pc->actually_transferred)
805 pc->retries = IDETAPE_MAX_PC_RETRIES + 1;
806 }
807}
808
Borislav Petkov419d4742008-02-02 19:56:51 +0100809static void idetape_activate_next_stage(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810{
811 idetape_tape_t *tape = drive->driver_data;
812 idetape_stage_t *stage = tape->next_stage;
813 struct request *rq = &stage->rq;
814
Borislav Petkov8004a8c2008-02-06 02:57:51 +0100815 debug_log(DBG_PROCS, "Enter %s\n", __func__);
816
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817 if (stage == NULL) {
Borislav Petkov8004a8c2008-02-06 02:57:51 +0100818 printk(KERN_ERR "ide-tape: bug: Trying to activate a non"
819 " existing stage\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820 return;
821 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822
823 rq->rq_disk = tape->disk;
824 rq->buffer = NULL;
825 rq->special = (void *)stage->bh;
826 tape->active_data_request = rq;
827 tape->active_stage = stage;
828 tape->next_stage = stage->next;
829}
830
831/*
832 * idetape_increase_max_pipeline_stages is a part of the feedback
833 * loop which tries to find the optimum number of stages. In the
834 * feedback loop, we are starting from a minimum maximum number of
835 * stages, and if we sense that the pipeline is empty, we try to
836 * increase it, until we reach the user compile time memory limit.
837 */
838static void idetape_increase_max_pipeline_stages (ide_drive_t *drive)
839{
840 idetape_tape_t *tape = drive->driver_data;
841 int increase = (tape->max_pipeline - tape->min_pipeline) / 10;
Borislav Petkov8004a8c2008-02-06 02:57:51 +0100842
843 debug_log(DBG_PROCS, "Enter %s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844
845 tape->max_stages += max(increase, 1);
846 tape->max_stages = max(tape->max_stages, tape->min_pipeline);
847 tape->max_stages = min(tape->max_stages, tape->max_pipeline);
848}
849
850/*
851 * idetape_kfree_stage calls kfree to completely free a stage, along with
852 * its related buffers.
853 */
854static void __idetape_kfree_stage (idetape_stage_t *stage)
855{
856 struct idetape_bh *prev_bh, *bh = stage->bh;
857 int size;
858
859 while (bh != NULL) {
860 if (bh->b_data != NULL) {
861 size = (int) bh->b_size;
862 while (size > 0) {
863 free_page((unsigned long) bh->b_data);
864 size -= PAGE_SIZE;
865 bh->b_data += PAGE_SIZE;
866 }
867 }
868 prev_bh = bh;
869 bh = bh->b_reqnext;
870 kfree(prev_bh);
871 }
872 kfree(stage);
873}
874
875static void idetape_kfree_stage (idetape_tape_t *tape, idetape_stage_t *stage)
876{
877 __idetape_kfree_stage(stage);
878}
879
880/*
881 * idetape_remove_stage_head removes tape->first_stage from the pipeline.
882 * The caller should avoid race conditions.
883 */
884static void idetape_remove_stage_head (ide_drive_t *drive)
885{
886 idetape_tape_t *tape = drive->driver_data;
887 idetape_stage_t *stage;
Borislav Petkov8004a8c2008-02-06 02:57:51 +0100888
889 debug_log(DBG_PROCS, "Enter %s\n", __func__);
890
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891 if (tape->first_stage == NULL) {
892 printk(KERN_ERR "ide-tape: bug: tape->first_stage is NULL\n");
Borislav Petkov55a5d292008-02-02 19:56:49 +0100893 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894 }
895 if (tape->active_stage == tape->first_stage) {
Borislav Petkov8004a8c2008-02-06 02:57:51 +0100896 printk(KERN_ERR "ide-tape: bug: Trying to free our active "
897 "pipeline stage\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898 return;
899 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900 stage = tape->first_stage;
901 tape->first_stage = stage->next;
902 idetape_kfree_stage(tape, stage);
903 tape->nr_stages--;
904 if (tape->first_stage == NULL) {
905 tape->last_stage = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906 if (tape->next_stage != NULL)
907 printk(KERN_ERR "ide-tape: bug: tape->next_stage != NULL\n");
908 if (tape->nr_stages)
909 printk(KERN_ERR "ide-tape: bug: nr_stages should be 0 now\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910 }
911}
912
913/*
914 * This will free all the pipeline stages starting from new_last_stage->next
915 * to the end of the list, and point tape->last_stage to new_last_stage.
916 */
917static void idetape_abort_pipeline(ide_drive_t *drive,
918 idetape_stage_t *new_last_stage)
919{
920 idetape_tape_t *tape = drive->driver_data;
921 idetape_stage_t *stage = new_last_stage->next;
922 idetape_stage_t *nstage;
923
Borislav Petkov8004a8c2008-02-06 02:57:51 +0100924 debug_log(DBG_PROCS, "%s: Enter %s\n", tape->name, __func__);
925
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926 while (stage) {
927 nstage = stage->next;
928 idetape_kfree_stage(tape, stage);
929 --tape->nr_stages;
930 --tape->nr_pending_stages;
931 stage = nstage;
932 }
933 if (new_last_stage)
934 new_last_stage->next = NULL;
935 tape->last_stage = new_last_stage;
936 tape->next_stage = NULL;
937}
938
939/*
940 * idetape_end_request is used to finish servicing a request, and to
941 * insert a pending pipeline request into the main device queue.
942 */
943static int idetape_end_request(ide_drive_t *drive, int uptodate, int nr_sects)
944{
945 struct request *rq = HWGROUP(drive)->rq;
946 idetape_tape_t *tape = drive->driver_data;
947 unsigned long flags;
948 int error;
949 int remove_stage = 0;
950 idetape_stage_t *active_stage;
951
Borislav Petkov8004a8c2008-02-06 02:57:51 +0100952 debug_log(DBG_PROCS, "Enter %s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953
954 switch (uptodate) {
955 case 0: error = IDETAPE_ERROR_GENERAL; break;
956 case 1: error = 0; break;
957 default: error = uptodate;
958 }
959 rq->errors = error;
960 if (error)
961 tape->failed_pc = NULL;
962
Bartlomiej Zolnierkiewicz36872212008-01-26 20:13:10 +0100963 if (!blk_special_request(rq)) {
964 ide_end_request(drive, uptodate, nr_sects);
965 return 0;
966 }
967
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968 spin_lock_irqsave(&tape->spinlock, flags);
969
970 /* The request was a pipelined data transfer request */
971 if (tape->active_data_request == rq) {
972 active_stage = tape->active_stage;
973 tape->active_stage = NULL;
974 tape->active_data_request = NULL;
975 tape->nr_pending_stages--;
976 if (rq->cmd[0] & REQ_IDETAPE_WRITE) {
977 remove_stage = 1;
978 if (error) {
979 set_bit(IDETAPE_PIPELINE_ERROR, &tape->flags);
980 if (error == IDETAPE_ERROR_EOD)
981 idetape_abort_pipeline(drive, active_stage);
982 }
983 } else if (rq->cmd[0] & REQ_IDETAPE_READ) {
984 if (error == IDETAPE_ERROR_EOD) {
985 set_bit(IDETAPE_PIPELINE_ERROR, &tape->flags);
986 idetape_abort_pipeline(drive, active_stage);
987 }
988 }
989 if (tape->next_stage != NULL) {
Borislav Petkov419d4742008-02-02 19:56:51 +0100990 idetape_activate_next_stage(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991
992 /*
993 * Insert the next request into the request queue.
994 */
995 (void) ide_do_drive_cmd(drive, tape->active_data_request, ide_end);
996 } else if (!error) {
997 idetape_increase_max_pipeline_stages(drive);
998 }
999 }
1000 ide_end_drive_cmd(drive, 0, 0);
1001// blkdev_dequeue_request(rq);
1002// drive->rq = NULL;
1003// end_that_request_last(rq);
1004
1005 if (remove_stage)
1006 idetape_remove_stage_head(drive);
1007 if (tape->active_data_request == NULL)
1008 clear_bit(IDETAPE_PIPELINE_ACTIVE, &tape->flags);
1009 spin_unlock_irqrestore(&tape->spinlock, flags);
1010 return 0;
1011}
1012
1013static ide_startstop_t idetape_request_sense_callback (ide_drive_t *drive)
1014{
1015 idetape_tape_t *tape = drive->driver_data;
1016
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001017 debug_log(DBG_PROCS, "Enter %s\n", __func__);
1018
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019 if (!tape->pc->error) {
Borislav Petkov1b5db432008-02-02 19:56:48 +01001020 idetape_analyze_error(drive, tape->pc->buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021 idetape_end_request(drive, 1, 0);
1022 } else {
1023 printk(KERN_ERR "ide-tape: Error in REQUEST SENSE itself - Aborting request!\n");
1024 idetape_end_request(drive, 0, 0);
1025 }
1026 return ide_stopped;
1027}
1028
1029static void idetape_create_request_sense_cmd (idetape_pc_t *pc)
1030{
1031 idetape_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +01001032 pc->c[0] = REQUEST_SENSE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033 pc->c[4] = 20;
1034 pc->request_transfer = 20;
1035 pc->callback = &idetape_request_sense_callback;
1036}
1037
1038static void idetape_init_rq(struct request *rq, u8 cmd)
1039{
1040 memset(rq, 0, sizeof(*rq));
Jens Axboe4aff5e22006-08-10 08:44:47 +02001041 rq->cmd_type = REQ_TYPE_SPECIAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042 rq->cmd[0] = cmd;
1043}
1044
1045/*
1046 * idetape_queue_pc_head generates a new packet command request in front
1047 * of the request queue, before the current request, so that it will be
1048 * processed immediately, on the next pass through the driver.
1049 *
1050 * idetape_queue_pc_head is called from the request handling part of
1051 * the driver (the "bottom" part). Safe storage for the request should
1052 * be allocated with idetape_next_pc_storage and idetape_next_rq_storage
1053 * before calling idetape_queue_pc_head.
1054 *
1055 * Memory for those requests is pre-allocated at initialization time, and
1056 * is limited to IDETAPE_PC_STACK requests. We assume that we have enough
1057 * space for the maximum possible number of inter-dependent packet commands.
1058 *
1059 * The higher level of the driver - The ioctl handler and the character
1060 * device handling functions should queue request to the lower level part
1061 * and wait for their completion using idetape_queue_pc_tail or
1062 * idetape_queue_rw_tail.
1063 */
1064static void idetape_queue_pc_head (ide_drive_t *drive, idetape_pc_t *pc,struct request *rq)
1065{
1066 struct ide_tape_obj *tape = drive->driver_data;
1067
1068 idetape_init_rq(rq, REQ_IDETAPE_PC1);
1069 rq->buffer = (char *) pc;
1070 rq->rq_disk = tape->disk;
1071 (void) ide_do_drive_cmd(drive, rq, ide_preempt);
1072}
1073
1074/*
1075 * idetape_retry_pc is called when an error was detected during the
1076 * last packet command. We queue a request sense packet command in
1077 * the head of the request list.
1078 */
1079static ide_startstop_t idetape_retry_pc (ide_drive_t *drive)
1080{
1081 idetape_tape_t *tape = drive->driver_data;
1082 idetape_pc_t *pc;
1083 struct request *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084
Bartlomiej Zolnierkiewicz64a57fe2008-02-06 02:57:51 +01001085 (void)ide_read_error(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086 pc = idetape_next_pc_storage(drive);
1087 rq = idetape_next_rq_storage(drive);
1088 idetape_create_request_sense_cmd(pc);
1089 set_bit(IDETAPE_IGNORE_DSC, &tape->flags);
1090 idetape_queue_pc_head(drive, pc, rq);
1091 return ide_stopped;
1092}
1093
1094/*
1095 * idetape_postpone_request postpones the current request so that
1096 * ide.c will be able to service requests from another device on
1097 * the same hwgroup while we are polling for DSC.
1098 */
1099static void idetape_postpone_request (ide_drive_t *drive)
1100{
1101 idetape_tape_t *tape = drive->driver_data;
1102
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001103 debug_log(DBG_PROCS, "Enter %s\n", __func__);
1104
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105 tape->postponed_rq = HWGROUP(drive)->rq;
1106 ide_stall_queue(drive, tape->dsc_polling_frequency);
1107}
1108
Borislav Petkova1efc852008-02-06 02:57:52 +01001109typedef void idetape_io_buf(ide_drive_t *, idetape_pc_t *, unsigned int);
1110
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111/*
Borislav Petkova1efc852008-02-06 02:57:52 +01001112 * This is the usual interrupt handler which will be called during a packet
1113 * command. We will transfer some of the data (as requested by the drive) and
1114 * will re-point interrupt handler to us. When data transfer is finished, we
1115 * will act according to the algorithm described before
1116 * idetape_issue_packet_command.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117 */
Borislav Petkova1efc852008-02-06 02:57:52 +01001118static ide_startstop_t idetape_pc_intr(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119{
1120 ide_hwif_t *hwif = drive->hwif;
1121 idetape_tape_t *tape = drive->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122 idetape_pc_t *pc = tape->pc;
Borislav Petkova1efc852008-02-06 02:57:52 +01001123 xfer_func_t *xferfunc;
1124 idetape_io_buf *iobuf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125 unsigned int temp;
1126#if SIMULATE_ERRORS
1127 static int error_sim_count = 0;
1128#endif
Bartlomiej Zolnierkiewicz790d1232008-01-25 22:17:12 +01001129 u16 bcount;
Bartlomiej Zolnierkiewicz8e7657a2008-01-25 22:17:12 +01001130 u8 stat, ireason;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001132 debug_log(DBG_PROCS, "Enter %s - interrupt handler\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133
1134 /* Clear the interrupt */
Bartlomiej Zolnierkiewiczc47137a2008-02-06 02:57:51 +01001135 stat = ide_read_status(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136
1137 if (test_bit(PC_DMA_IN_PROGRESS, &pc->flags)) {
Bartlomiej Zolnierkiewicz22c525b2008-01-25 22:17:11 +01001138 if (hwif->ide_dma_end(drive) || (stat & ERR_STAT)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139 /*
1140 * A DMA error is sometimes expected. For example,
1141 * if the tape is crossing a filemark during a
1142 * READ command, it will issue an irq and position
1143 * itself before the filemark, so that only a partial
1144 * data transfer will occur (which causes the DMA
1145 * error). In that case, we will later ask the tape
1146 * how much bytes of the original request were
1147 * actually transferred (we can't receive that
1148 * information from the DMA engine on most chipsets).
1149 */
1150
1151 /*
1152 * On the contrary, a DMA error is never expected;
1153 * it usually indicates a hardware error or abort.
1154 * If the tape crosses a filemark during a READ
1155 * command, it will issue an irq and position itself
1156 * after the filemark (not before). Only a partial
1157 * data transfer will occur, but no DMA error.
1158 * (AS, 19 Apr 2001)
1159 */
1160 set_bit(PC_DMA_ERROR, &pc->flags);
1161 } else {
1162 pc->actually_transferred = pc->request_transfer;
1163 idetape_update_buffers(pc);
1164 }
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001165 debug_log(DBG_PROCS, "DMA finished\n");
1166
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167 }
1168
1169 /* No more interrupts */
Bartlomiej Zolnierkiewicz22c525b2008-01-25 22:17:11 +01001170 if ((stat & DRQ_STAT) == 0) {
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001171 debug_log(DBG_SENSE, "Packet command completed, %d bytes"
1172 " transferred\n", pc->actually_transferred);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001174 clear_bit(PC_DMA_IN_PROGRESS, &pc->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175 local_irq_enable();
1176
1177#if SIMULATE_ERRORS
Borislav Petkov90699ce2008-02-02 19:56:50 +01001178 if ((pc->c[0] == WRITE_6 || pc->c[0] == READ_6) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179 (++error_sim_count % 100) == 0) {
1180 printk(KERN_INFO "ide-tape: %s: simulating error\n",
1181 tape->name);
Bartlomiej Zolnierkiewicz22c525b2008-01-25 22:17:11 +01001182 stat |= ERR_STAT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183 }
1184#endif
Borislav Petkov90699ce2008-02-02 19:56:50 +01001185 if ((stat & ERR_STAT) && pc->c[0] == REQUEST_SENSE)
Bartlomiej Zolnierkiewicz22c525b2008-01-25 22:17:11 +01001186 stat &= ~ERR_STAT;
1187 if ((stat & ERR_STAT) || test_bit(PC_DMA_ERROR, &pc->flags)) {
1188 /* Error detected */
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001189 debug_log(DBG_ERR, "%s: I/O error\n", tape->name);
1190
Borislav Petkov90699ce2008-02-02 19:56:50 +01001191 if (pc->c[0] == REQUEST_SENSE) {
Borislav Petkova1efc852008-02-06 02:57:52 +01001192 printk(KERN_ERR "ide-tape: I/O error in request"
1193 " sense command\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194 return ide_do_reset(drive);
1195 }
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001196 debug_log(DBG_ERR, "[cmd %x]: check condition\n",
1197 pc->c[0]);
1198
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199 /* Retry operation */
1200 return idetape_retry_pc(drive);
1201 }
1202 pc->error = 0;
1203 if (test_bit(PC_WAIT_FOR_DSC, &pc->flags) &&
Bartlomiej Zolnierkiewicz22c525b2008-01-25 22:17:11 +01001204 (stat & SEEK_STAT) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205 /* Media access command */
1206 tape->dsc_polling_start = jiffies;
1207 tape->dsc_polling_frequency = IDETAPE_DSC_MA_FAST;
1208 tape->dsc_timeout = jiffies + IDETAPE_DSC_MA_TIMEOUT;
1209 /* Allow ide.c to handle other requests */
1210 idetape_postpone_request(drive);
1211 return ide_stopped;
1212 }
1213 if (tape->failed_pc == pc)
1214 tape->failed_pc = NULL;
1215 /* Command finished - Call the callback function */
1216 return pc->callback(drive);
1217 }
1218 if (test_and_clear_bit(PC_DMA_IN_PROGRESS, &pc->flags)) {
1219 printk(KERN_ERR "ide-tape: The tape wants to issue more "
1220 "interrupts in DMA mode\n");
1221 printk(KERN_ERR "ide-tape: DMA disabled, reverting to PIO\n");
Bartlomiej Zolnierkiewicz7469aaf2007-02-17 02:40:26 +01001222 ide_dma_off(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223 return ide_do_reset(drive);
1224 }
1225 /* Get the number of bytes to transfer on this interrupt. */
Bartlomiej Zolnierkiewicz790d1232008-01-25 22:17:12 +01001226 bcount = (hwif->INB(IDE_BCOUNTH_REG) << 8) |
1227 hwif->INB(IDE_BCOUNTL_REG);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228
Bartlomiej Zolnierkiewicz8e7657a2008-01-25 22:17:12 +01001229 ireason = hwif->INB(IDE_IREASON_REG);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230
Bartlomiej Zolnierkiewicz8e7657a2008-01-25 22:17:12 +01001231 if (ireason & CD) {
Borislav Petkova1efc852008-02-06 02:57:52 +01001232 printk(KERN_ERR "ide-tape: CoD != 0 in %s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233 return ide_do_reset(drive);
1234 }
Bartlomiej Zolnierkiewicz8e7657a2008-01-25 22:17:12 +01001235 if (((ireason & IO) == IO) == test_bit(PC_WRITING, &pc->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236 /* Hopefully, we will never get here */
1237 printk(KERN_ERR "ide-tape: We wanted to %s, ",
Bartlomiej Zolnierkiewicz8e7657a2008-01-25 22:17:12 +01001238 (ireason & IO) ? "Write" : "Read");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239 printk(KERN_ERR "ide-tape: but the tape wants us to %s !\n",
Bartlomiej Zolnierkiewicz8e7657a2008-01-25 22:17:12 +01001240 (ireason & IO) ? "Read" : "Write");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241 return ide_do_reset(drive);
1242 }
1243 if (!test_bit(PC_WRITING, &pc->flags)) {
1244 /* Reading - Check that we have enough space */
Bartlomiej Zolnierkiewicz790d1232008-01-25 22:17:12 +01001245 temp = pc->actually_transferred + bcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246 if (temp > pc->request_transfer) {
1247 if (temp > pc->buffer_size) {
Borislav Petkova1efc852008-02-06 02:57:52 +01001248 printk(KERN_ERR "ide-tape: The tape wants to "
1249 "send us more data than expected "
1250 "- discarding data\n");
Bartlomiej Zolnierkiewicz790d1232008-01-25 22:17:12 +01001251 idetape_discard_data(drive, bcount);
Borislav Petkova1efc852008-02-06 02:57:52 +01001252 ide_set_handler(drive, &idetape_pc_intr,
1253 IDETAPE_WAIT_CMD, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254 return ide_started;
1255 }
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001256 debug_log(DBG_SENSE, "The tape wants to send us more "
1257 "data than expected - allowing transfer\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258 }
Borislav Petkova1efc852008-02-06 02:57:52 +01001259 iobuf = &idetape_input_buffers;
1260 xferfunc = hwif->atapi_input_bytes;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261 } else {
Borislav Petkova1efc852008-02-06 02:57:52 +01001262 iobuf = &idetape_output_buffers;
1263 xferfunc = hwif->atapi_output_bytes;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264 }
Borislav Petkova1efc852008-02-06 02:57:52 +01001265
1266 if (pc->bh)
1267 iobuf(drive, pc, bcount);
1268 else
1269 xferfunc(drive, pc->current_position, bcount);
1270
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271 /* Update the current position */
Bartlomiej Zolnierkiewicz790d1232008-01-25 22:17:12 +01001272 pc->actually_transferred += bcount;
1273 pc->current_position += bcount;
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001274
1275 debug_log(DBG_SENSE, "[cmd %x] transferred %d bytes on that intr.\n",
1276 pc->c[0], bcount);
1277
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278 /* And set the interrupt handler again */
1279 ide_set_handler(drive, &idetape_pc_intr, IDETAPE_WAIT_CMD, NULL);
1280 return ide_started;
1281}
1282
1283/*
1284 * Packet Command Interface
1285 *
1286 * The current Packet Command is available in tape->pc, and will not
1287 * change until we finish handling it. Each packet command is associated
1288 * with a callback function that will be called when the command is
1289 * finished.
1290 *
1291 * The handling will be done in three stages:
1292 *
1293 * 1. idetape_issue_packet_command will send the packet command to the
1294 * drive, and will set the interrupt handler to idetape_pc_intr.
1295 *
1296 * 2. On each interrupt, idetape_pc_intr will be called. This step
1297 * will be repeated until the device signals us that no more
1298 * interrupts will be issued.
1299 *
1300 * 3. ATAPI Tape media access commands have immediate status with a
1301 * delayed process. In case of a successful initiation of a
1302 * media access packet command, the DSC bit will be set when the
1303 * actual execution of the command is finished.
1304 * Since the tape drive will not issue an interrupt, we have to
1305 * poll for this event. In this case, we define the request as
1306 * "low priority request" by setting rq_status to
1307 * IDETAPE_RQ_POSTPONED, set a timer to poll for DSC and exit
1308 * the driver.
1309 *
1310 * ide.c will then give higher priority to requests which
1311 * originate from the other device, until will change rq_status
1312 * to RQ_ACTIVE.
1313 *
1314 * 4. When the packet command is finished, it will be checked for errors.
1315 *
1316 * 5. In case an error was found, we queue a request sense packet
1317 * command in front of the request queue and retry the operation
1318 * up to IDETAPE_MAX_PC_RETRIES times.
1319 *
1320 * 6. In case no error was found, or we decided to give up and not
1321 * to retry again, the callback function will be called and then
1322 * we will handle the next request.
1323 *
1324 */
1325static ide_startstop_t idetape_transfer_pc(ide_drive_t *drive)
1326{
1327 ide_hwif_t *hwif = drive->hwif;
1328 idetape_tape_t *tape = drive->driver_data;
1329 idetape_pc_t *pc = tape->pc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330 int retries = 100;
1331 ide_startstop_t startstop;
Bartlomiej Zolnierkiewicz8e7657a2008-01-25 22:17:12 +01001332 u8 ireason;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333
1334 if (ide_wait_stat(&startstop,drive,DRQ_STAT,BUSY_STAT,WAIT_READY)) {
1335 printk(KERN_ERR "ide-tape: Strange, packet command initiated yet DRQ isn't asserted\n");
1336 return startstop;
1337 }
Bartlomiej Zolnierkiewicz8e7657a2008-01-25 22:17:12 +01001338 ireason = hwif->INB(IDE_IREASON_REG);
1339 while (retries-- && ((ireason & CD) == 0 || (ireason & IO))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340 printk(KERN_ERR "ide-tape: (IO,CoD != (0,1) while issuing "
1341 "a packet command, retrying\n");
1342 udelay(100);
Bartlomiej Zolnierkiewicz8e7657a2008-01-25 22:17:12 +01001343 ireason = hwif->INB(IDE_IREASON_REG);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344 if (retries == 0) {
1345 printk(KERN_ERR "ide-tape: (IO,CoD != (0,1) while "
1346 "issuing a packet command, ignoring\n");
Bartlomiej Zolnierkiewicz8e7657a2008-01-25 22:17:12 +01001347 ireason |= CD;
1348 ireason &= ~IO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349 }
1350 }
Bartlomiej Zolnierkiewicz8e7657a2008-01-25 22:17:12 +01001351 if ((ireason & CD) == 0 || (ireason & IO)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352 printk(KERN_ERR "ide-tape: (IO,CoD) != (0,1) while issuing "
1353 "a packet command\n");
1354 return ide_do_reset(drive);
1355 }
1356 /* Set the interrupt routine */
1357 ide_set_handler(drive, &idetape_pc_intr, IDETAPE_WAIT_CMD, NULL);
1358#ifdef CONFIG_BLK_DEV_IDEDMA
1359 /* Begin DMA, if necessary */
1360 if (test_bit(PC_DMA_IN_PROGRESS, &pc->flags))
1361 hwif->dma_start(drive);
1362#endif
1363 /* Send the actual packet */
1364 HWIF(drive)->atapi_output_bytes(drive, pc->c, 12);
1365 return ide_started;
1366}
1367
1368static ide_startstop_t idetape_issue_packet_command (ide_drive_t *drive, idetape_pc_t *pc)
1369{
1370 ide_hwif_t *hwif = drive->hwif;
1371 idetape_tape_t *tape = drive->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372 int dma_ok = 0;
Bartlomiej Zolnierkiewicz790d1232008-01-25 22:17:12 +01001373 u16 bcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374
Borislav Petkov90699ce2008-02-02 19:56:50 +01001375 if (tape->pc->c[0] == REQUEST_SENSE &&
1376 pc->c[0] == REQUEST_SENSE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377 printk(KERN_ERR "ide-tape: possible ide-tape.c bug - "
1378 "Two request sense in serial were issued\n");
1379 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380
Borislav Petkov90699ce2008-02-02 19:56:50 +01001381 if (tape->failed_pc == NULL && pc->c[0] != REQUEST_SENSE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382 tape->failed_pc = pc;
1383 /* Set the current packet command */
1384 tape->pc = pc;
1385
1386 if (pc->retries > IDETAPE_MAX_PC_RETRIES ||
1387 test_bit(PC_ABORT, &pc->flags)) {
1388 /*
1389 * We will "abort" retrying a packet command in case
1390 * a legitimate error code was received (crossing a
1391 * filemark, or end of the media, for example).
1392 */
1393 if (!test_bit(PC_ABORT, &pc->flags)) {
Borislav Petkov90699ce2008-02-02 19:56:50 +01001394 if (!(pc->c[0] == TEST_UNIT_READY &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395 tape->sense_key == 2 && tape->asc == 4 &&
1396 (tape->ascq == 1 || tape->ascq == 8))) {
1397 printk(KERN_ERR "ide-tape: %s: I/O error, "
1398 "pc = %2x, key = %2x, "
1399 "asc = %2x, ascq = %2x\n",
1400 tape->name, pc->c[0],
1401 tape->sense_key, tape->asc,
1402 tape->ascq);
1403 }
1404 /* Giving up */
1405 pc->error = IDETAPE_ERROR_GENERAL;
1406 }
1407 tape->failed_pc = NULL;
1408 return pc->callback(drive);
1409 }
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001410 debug_log(DBG_SENSE, "Retry #%d, cmd = %02X\n", pc->retries, pc->c[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411
1412 pc->retries++;
1413 /* We haven't transferred any data yet */
1414 pc->actually_transferred = 0;
1415 pc->current_position = pc->buffer;
1416 /* Request to transfer the entire buffer at once */
Bartlomiej Zolnierkiewicz790d1232008-01-25 22:17:12 +01001417 bcount = pc->request_transfer;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418
1419 if (test_and_clear_bit(PC_DMA_ERROR, &pc->flags)) {
1420 printk(KERN_WARNING "ide-tape: DMA disabled, "
1421 "reverting to PIO\n");
Bartlomiej Zolnierkiewicz7469aaf2007-02-17 02:40:26 +01001422 ide_dma_off(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423 }
1424 if (test_bit(PC_DMA_RECOMMENDED, &pc->flags) && drive->using_dma)
1425 dma_ok = !hwif->dma_setup(drive);
1426
Bartlomiej Zolnierkiewicz2fc57382008-01-25 22:17:13 +01001427 ide_pktcmd_tf_load(drive, IDE_TFLAG_NO_SELECT_MASK |
1428 IDE_TFLAG_OUT_DEVICE, bcount, dma_ok);
1429
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430 if (dma_ok) /* Will begin DMA later */
1431 set_bit(PC_DMA_IN_PROGRESS, &pc->flags);
1432 if (test_bit(IDETAPE_DRQ_INTERRUPT, &tape->flags)) {
Bartlomiej Zolnierkiewiczc1c9dbc2008-02-02 19:56:44 +01001433 ide_execute_command(drive, WIN_PACKETCMD, &idetape_transfer_pc,
1434 IDETAPE_WAIT_CMD, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435 return ide_started;
1436 } else {
1437 hwif->OUTB(WIN_PACKETCMD, IDE_COMMAND_REG);
1438 return idetape_transfer_pc(drive);
1439 }
1440}
1441
1442/*
1443 * General packet command callback function.
1444 */
1445static ide_startstop_t idetape_pc_callback (ide_drive_t *drive)
1446{
1447 idetape_tape_t *tape = drive->driver_data;
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001448
1449 debug_log(DBG_PROCS, "Enter %s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450
1451 idetape_end_request(drive, tape->pc->error ? 0 : 1, 0);
1452 return ide_stopped;
1453}
1454
1455/*
1456 * A mode sense command is used to "sense" tape parameters.
1457 */
1458static void idetape_create_mode_sense_cmd (idetape_pc_t *pc, u8 page_code)
1459{
1460 idetape_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +01001461 pc->c[0] = MODE_SENSE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462 if (page_code != IDETAPE_BLOCK_DESCRIPTOR)
1463 pc->c[1] = 8; /* DBD = 1 - Don't return block descriptors */
1464 pc->c[2] = page_code;
1465 /*
1466 * Changed pc->c[3] to 0 (255 will at best return unused info).
1467 *
1468 * For SCSI this byte is defined as subpage instead of high byte
1469 * of length and some IDE drives seem to interpret it this way
1470 * and return an error when 255 is used.
1471 */
1472 pc->c[3] = 0;
1473 pc->c[4] = 255; /* (We will just discard data in that case) */
1474 if (page_code == IDETAPE_BLOCK_DESCRIPTOR)
1475 pc->request_transfer = 12;
1476 else if (page_code == IDETAPE_CAPABILITIES_PAGE)
1477 pc->request_transfer = 24;
1478 else
1479 pc->request_transfer = 50;
1480 pc->callback = &idetape_pc_callback;
1481}
1482
Borislav Petkov37016ba2008-02-06 02:57:52 +01001483static void idetape_calculate_speeds(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001484{
1485 idetape_tape_t *tape = drive->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486
1487 if (time_after(jiffies, tape->controlled_pipeline_head_time + 120 * HZ)) {
1488 tape->controlled_previous_pipeline_head = tape->controlled_last_pipeline_head;
1489 tape->controlled_previous_head_time = tape->controlled_pipeline_head_time;
1490 tape->controlled_last_pipeline_head = tape->pipeline_head;
1491 tape->controlled_pipeline_head_time = jiffies;
1492 }
1493 if (time_after(jiffies, tape->controlled_pipeline_head_time + 60 * HZ))
1494 tape->controlled_pipeline_head_speed = (tape->pipeline_head - tape->controlled_last_pipeline_head) * 32 * HZ / (jiffies - tape->controlled_pipeline_head_time);
1495 else if (time_after(jiffies, tape->controlled_previous_head_time))
1496 tape->controlled_pipeline_head_speed = (tape->pipeline_head - tape->controlled_previous_pipeline_head) * 32 * HZ / (jiffies - tape->controlled_previous_head_time);
1497
1498 if (tape->nr_pending_stages < tape->max_stages /*- 1 */) {
1499 /* -1 for read mode error recovery */
1500 if (time_after(jiffies, tape->uncontrolled_previous_head_time + 10 * HZ)) {
1501 tape->uncontrolled_pipeline_head_time = jiffies;
1502 tape->uncontrolled_pipeline_head_speed = (tape->pipeline_head - tape->uncontrolled_previous_pipeline_head) * 32 * HZ / (jiffies - tape->uncontrolled_previous_head_time);
1503 }
1504 } else {
1505 tape->uncontrolled_previous_head_time = jiffies;
1506 tape->uncontrolled_previous_pipeline_head = tape->pipeline_head;
1507 if (time_after(jiffies, tape->uncontrolled_pipeline_head_time + 30 * HZ)) {
1508 tape->uncontrolled_pipeline_head_time = jiffies;
1509 }
1510 }
1511 tape->pipeline_head_speed = max(tape->uncontrolled_pipeline_head_speed, tape->controlled_pipeline_head_speed);
Borislav Petkov37016ba2008-02-06 02:57:52 +01001512
1513 if (tape->speed_control == 1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514 if (tape->nr_pending_stages >= tape->max_stages / 2)
1515 tape->max_insert_speed = tape->pipeline_head_speed +
1516 (1100 - tape->pipeline_head_speed) * 2 * (tape->nr_pending_stages - tape->max_stages / 2) / tape->max_stages;
1517 else
1518 tape->max_insert_speed = 500 +
1519 (tape->pipeline_head_speed - 500) * 2 * tape->nr_pending_stages / tape->max_stages;
Borislav Petkov37016ba2008-02-06 02:57:52 +01001520
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521 if (tape->nr_pending_stages >= tape->max_stages * 99 / 100)
1522 tape->max_insert_speed = 5000;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523 } else
1524 tape->max_insert_speed = tape->speed_control;
Borislav Petkov37016ba2008-02-06 02:57:52 +01001525
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526 tape->max_insert_speed = max(tape->max_insert_speed, 500);
1527}
1528
1529static ide_startstop_t idetape_media_access_finished (ide_drive_t *drive)
1530{
1531 idetape_tape_t *tape = drive->driver_data;
1532 idetape_pc_t *pc = tape->pc;
Bartlomiej Zolnierkiewicz22c525b2008-01-25 22:17:11 +01001533 u8 stat;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534
Bartlomiej Zolnierkiewiczc47137a2008-02-06 02:57:51 +01001535 stat = ide_read_status(drive);
1536
Bartlomiej Zolnierkiewicz22c525b2008-01-25 22:17:11 +01001537 if (stat & SEEK_STAT) {
1538 if (stat & ERR_STAT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539 /* Error detected */
Borislav Petkov90699ce2008-02-02 19:56:50 +01001540 if (pc->c[0] != TEST_UNIT_READY)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541 printk(KERN_ERR "ide-tape: %s: I/O error, ",
1542 tape->name);
1543 /* Retry operation */
1544 return idetape_retry_pc(drive);
1545 }
1546 pc->error = 0;
1547 if (tape->failed_pc == pc)
1548 tape->failed_pc = NULL;
1549 } else {
1550 pc->error = IDETAPE_ERROR_GENERAL;
1551 tape->failed_pc = NULL;
1552 }
1553 return pc->callback(drive);
1554}
1555
1556static ide_startstop_t idetape_rw_callback (ide_drive_t *drive)
1557{
1558 idetape_tape_t *tape = drive->driver_data;
1559 struct request *rq = HWGROUP(drive)->rq;
1560 int blocks = tape->pc->actually_transferred / tape->tape_block_size;
1561
1562 tape->avg_size += blocks * tape->tape_block_size;
1563 tape->insert_size += blocks * tape->tape_block_size;
1564 if (tape->insert_size > 1024 * 1024)
1565 tape->measure_insert_time = 1;
1566 if (tape->measure_insert_time) {
1567 tape->measure_insert_time = 0;
1568 tape->insert_time = jiffies;
1569 tape->insert_size = 0;
1570 }
1571 if (time_after(jiffies, tape->insert_time))
1572 tape->insert_speed = tape->insert_size / 1024 * HZ / (jiffies - tape->insert_time);
Marcelo Feitoza Parisi9bae1ff2006-03-28 01:56:46 -08001573 if (time_after_eq(jiffies, tape->avg_time + HZ)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001574 tape->avg_speed = tape->avg_size * HZ / (jiffies - tape->avg_time) / 1024;
1575 tape->avg_size = 0;
1576 tape->avg_time = jiffies;
1577 }
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001578 debug_log(DBG_PROCS, "Enter %s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579
1580 tape->first_frame_position += blocks;
1581 rq->current_nr_sectors -= blocks;
1582
1583 if (!tape->pc->error)
1584 idetape_end_request(drive, 1, 0);
1585 else
1586 idetape_end_request(drive, tape->pc->error, 0);
1587 return ide_stopped;
1588}
1589
1590static void idetape_create_read_cmd(idetape_tape_t *tape, idetape_pc_t *pc, unsigned int length, struct idetape_bh *bh)
1591{
1592 idetape_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +01001593 pc->c[0] = READ_6;
Borislav Petkov860ff5e2008-02-02 19:56:50 +01001594 put_unaligned(cpu_to_be32(length), (unsigned int *) &pc->c[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001595 pc->c[1] = 1;
1596 pc->callback = &idetape_rw_callback;
1597 pc->bh = bh;
1598 atomic_set(&bh->b_count, 0);
1599 pc->buffer = NULL;
1600 pc->request_transfer = pc->buffer_size = length * tape->tape_block_size;
1601 if (pc->request_transfer == tape->stage_size)
1602 set_bit(PC_DMA_RECOMMENDED, &pc->flags);
1603}
1604
1605static void idetape_create_read_buffer_cmd(idetape_tape_t *tape, idetape_pc_t *pc, unsigned int length, struct idetape_bh *bh)
1606{
1607 int size = 32768;
1608 struct idetape_bh *p = bh;
1609
1610 idetape_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +01001611 pc->c[0] = READ_BUFFER;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001612 pc->c[1] = IDETAPE_RETRIEVE_FAULTY_BLOCK;
1613 pc->c[7] = size >> 8;
1614 pc->c[8] = size & 0xff;
1615 pc->callback = &idetape_pc_callback;
1616 pc->bh = bh;
1617 atomic_set(&bh->b_count, 0);
1618 pc->buffer = NULL;
1619 while (p) {
1620 atomic_set(&p->b_count, 0);
1621 p = p->b_reqnext;
1622 }
1623 pc->request_transfer = pc->buffer_size = size;
1624}
1625
1626static void idetape_create_write_cmd(idetape_tape_t *tape, idetape_pc_t *pc, unsigned int length, struct idetape_bh *bh)
1627{
1628 idetape_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +01001629 pc->c[0] = WRITE_6;
Borislav Petkov860ff5e2008-02-02 19:56:50 +01001630 put_unaligned(cpu_to_be32(length), (unsigned int *) &pc->c[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001631 pc->c[1] = 1;
1632 pc->callback = &idetape_rw_callback;
1633 set_bit(PC_WRITING, &pc->flags);
1634 pc->bh = bh;
1635 pc->b_data = bh->b_data;
1636 pc->b_count = atomic_read(&bh->b_count);
1637 pc->buffer = NULL;
1638 pc->request_transfer = pc->buffer_size = length * tape->tape_block_size;
1639 if (pc->request_transfer == tape->stage_size)
1640 set_bit(PC_DMA_RECOMMENDED, &pc->flags);
1641}
1642
1643/*
1644 * idetape_do_request is our request handling function.
1645 */
1646static ide_startstop_t idetape_do_request(ide_drive_t *drive,
1647 struct request *rq, sector_t block)
1648{
1649 idetape_tape_t *tape = drive->driver_data;
1650 idetape_pc_t *pc = NULL;
1651 struct request *postponed_rq = tape->postponed_rq;
Bartlomiej Zolnierkiewicz22c525b2008-01-25 22:17:11 +01001652 u8 stat;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001653
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001654 debug_log(DBG_SENSE, "sector: %ld, nr_sectors: %ld,"
1655 " current_nr_sectors: %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001656 rq->sector, rq->nr_sectors, rq->current_nr_sectors);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001657
Jens Axboe4aff5e22006-08-10 08:44:47 +02001658 if (!blk_special_request(rq)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001659 /*
1660 * We do not support buffer cache originated requests.
1661 */
1662 printk(KERN_NOTICE "ide-tape: %s: Unsupported request in "
Jens Axboe4aff5e22006-08-10 08:44:47 +02001663 "request queue (%d)\n", drive->name, rq->cmd_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664 ide_end_request(drive, 0, 0);
1665 return ide_stopped;
1666 }
1667
1668 /*
1669 * Retry a failed packet command
1670 */
1671 if (tape->failed_pc != NULL &&
Borislav Petkov90699ce2008-02-02 19:56:50 +01001672 tape->pc->c[0] == REQUEST_SENSE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001673 return idetape_issue_packet_command(drive, tape->failed_pc);
1674 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675 if (postponed_rq != NULL)
1676 if (rq != postponed_rq) {
1677 printk(KERN_ERR "ide-tape: ide-tape.c bug - "
1678 "Two DSC requests were queued\n");
1679 idetape_end_request(drive, 0, 0);
1680 return ide_stopped;
1681 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001682
1683 tape->postponed_rq = NULL;
1684
1685 /*
1686 * If the tape is still busy, postpone our request and service
1687 * the other device meanwhile.
1688 */
Bartlomiej Zolnierkiewiczc47137a2008-02-06 02:57:51 +01001689 stat = ide_read_status(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001690
1691 if (!drive->dsc_overlap && !(rq->cmd[0] & REQ_IDETAPE_PC2))
1692 set_bit(IDETAPE_IGNORE_DSC, &tape->flags);
1693
1694 if (drive->post_reset == 1) {
1695 set_bit(IDETAPE_IGNORE_DSC, &tape->flags);
1696 drive->post_reset = 0;
1697 }
1698
1699 if (tape->tape_still_time > 100 && tape->tape_still_time < 200)
1700 tape->measure_insert_time = 1;
1701 if (time_after(jiffies, tape->insert_time))
1702 tape->insert_speed = tape->insert_size / 1024 * HZ / (jiffies - tape->insert_time);
Borislav Petkov37016ba2008-02-06 02:57:52 +01001703 idetape_calculate_speeds(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001704 if (!test_and_clear_bit(IDETAPE_IGNORE_DSC, &tape->flags) &&
Bartlomiej Zolnierkiewicz22c525b2008-01-25 22:17:11 +01001705 (stat & SEEK_STAT) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001706 if (postponed_rq == NULL) {
1707 tape->dsc_polling_start = jiffies;
1708 tape->dsc_polling_frequency = tape->best_dsc_rw_frequency;
1709 tape->dsc_timeout = jiffies + IDETAPE_DSC_RW_TIMEOUT;
1710 } else if (time_after(jiffies, tape->dsc_timeout)) {
1711 printk(KERN_ERR "ide-tape: %s: DSC timeout\n",
1712 tape->name);
1713 if (rq->cmd[0] & REQ_IDETAPE_PC2) {
1714 idetape_media_access_finished(drive);
1715 return ide_stopped;
1716 } else {
1717 return ide_do_reset(drive);
1718 }
Marcelo Feitoza Parisi9bae1ff2006-03-28 01:56:46 -08001719 } else if (time_after(jiffies, tape->dsc_polling_start + IDETAPE_DSC_MA_THRESHOLD))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001720 tape->dsc_polling_frequency = IDETAPE_DSC_MA_SLOW;
1721 idetape_postpone_request(drive);
1722 return ide_stopped;
1723 }
1724 if (rq->cmd[0] & REQ_IDETAPE_READ) {
1725 tape->buffer_head++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001726 tape->postpone_cnt = 0;
1727 pc = idetape_next_pc_storage(drive);
1728 idetape_create_read_cmd(tape, pc, rq->current_nr_sectors, (struct idetape_bh *)rq->special);
1729 goto out;
1730 }
1731 if (rq->cmd[0] & REQ_IDETAPE_WRITE) {
1732 tape->buffer_head++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001733 tape->postpone_cnt = 0;
1734 pc = idetape_next_pc_storage(drive);
1735 idetape_create_write_cmd(tape, pc, rq->current_nr_sectors, (struct idetape_bh *)rq->special);
1736 goto out;
1737 }
1738 if (rq->cmd[0] & REQ_IDETAPE_READ_BUFFER) {
1739 tape->postpone_cnt = 0;
1740 pc = idetape_next_pc_storage(drive);
1741 idetape_create_read_buffer_cmd(tape, pc, rq->current_nr_sectors, (struct idetape_bh *)rq->special);
1742 goto out;
1743 }
1744 if (rq->cmd[0] & REQ_IDETAPE_PC1) {
1745 pc = (idetape_pc_t *) rq->buffer;
1746 rq->cmd[0] &= ~(REQ_IDETAPE_PC1);
1747 rq->cmd[0] |= REQ_IDETAPE_PC2;
1748 goto out;
1749 }
1750 if (rq->cmd[0] & REQ_IDETAPE_PC2) {
1751 idetape_media_access_finished(drive);
1752 return ide_stopped;
1753 }
1754 BUG();
1755out:
1756 return idetape_issue_packet_command(drive, pc);
1757}
1758
1759/*
1760 * Pipeline related functions
1761 */
1762static inline int idetape_pipeline_active (idetape_tape_t *tape)
1763{
1764 int rc1, rc2;
1765
1766 rc1 = test_bit(IDETAPE_PIPELINE_ACTIVE, &tape->flags);
1767 rc2 = (tape->active_data_request != NULL);
1768 return rc1;
1769}
1770
1771/*
1772 * idetape_kmalloc_stage uses __get_free_page to allocate a pipeline
1773 * stage, along with all the necessary small buffers which together make
1774 * a buffer of size tape->stage_size (or a bit more). We attempt to
1775 * combine sequential pages as much as possible.
1776 *
1777 * Returns a pointer to the new allocated stage, or NULL if we
1778 * can't (or don't want to) allocate a stage.
1779 *
1780 * Pipeline stages are optional and are used to increase performance.
1781 * If we can't allocate them, we'll manage without them.
1782 */
1783static idetape_stage_t *__idetape_kmalloc_stage (idetape_tape_t *tape, int full, int clear)
1784{
1785 idetape_stage_t *stage;
1786 struct idetape_bh *prev_bh, *bh;
1787 int pages = tape->pages_per_stage;
1788 char *b_data = NULL;
1789
Robert P. J. Day5cbded52006-12-13 00:35:56 -08001790 if ((stage = kmalloc(sizeof (idetape_stage_t),GFP_KERNEL)) == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001791 return NULL;
1792 stage->next = NULL;
1793
Robert P. J. Day5cbded52006-12-13 00:35:56 -08001794 bh = stage->bh = kmalloc(sizeof(struct idetape_bh), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001795 if (bh == NULL)
1796 goto abort;
1797 bh->b_reqnext = NULL;
1798 if ((bh->b_data = (char *) __get_free_page (GFP_KERNEL)) == NULL)
1799 goto abort;
1800 if (clear)
1801 memset(bh->b_data, 0, PAGE_SIZE);
1802 bh->b_size = PAGE_SIZE;
1803 atomic_set(&bh->b_count, full ? bh->b_size : 0);
1804
1805 while (--pages) {
1806 if ((b_data = (char *) __get_free_page (GFP_KERNEL)) == NULL)
1807 goto abort;
1808 if (clear)
1809 memset(b_data, 0, PAGE_SIZE);
1810 if (bh->b_data == b_data + PAGE_SIZE) {
1811 bh->b_size += PAGE_SIZE;
1812 bh->b_data -= PAGE_SIZE;
1813 if (full)
1814 atomic_add(PAGE_SIZE, &bh->b_count);
1815 continue;
1816 }
1817 if (b_data == bh->b_data + bh->b_size) {
1818 bh->b_size += PAGE_SIZE;
1819 if (full)
1820 atomic_add(PAGE_SIZE, &bh->b_count);
1821 continue;
1822 }
1823 prev_bh = bh;
Robert P. J. Day5cbded52006-12-13 00:35:56 -08001824 if ((bh = kmalloc(sizeof(struct idetape_bh), GFP_KERNEL)) == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825 free_page((unsigned long) b_data);
1826 goto abort;
1827 }
1828 bh->b_reqnext = NULL;
1829 bh->b_data = b_data;
1830 bh->b_size = PAGE_SIZE;
1831 atomic_set(&bh->b_count, full ? bh->b_size : 0);
1832 prev_bh->b_reqnext = bh;
1833 }
1834 bh->b_size -= tape->excess_bh_size;
1835 if (full)
1836 atomic_sub(tape->excess_bh_size, &bh->b_count);
1837 return stage;
1838abort:
1839 __idetape_kfree_stage(stage);
1840 return NULL;
1841}
1842
1843static idetape_stage_t *idetape_kmalloc_stage (idetape_tape_t *tape)
1844{
1845 idetape_stage_t *cache_stage = tape->cache_stage;
1846
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001847 debug_log(DBG_PROCS, "Enter %s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001848
1849 if (tape->nr_stages >= tape->max_stages)
1850 return NULL;
1851 if (cache_stage != NULL) {
1852 tape->cache_stage = NULL;
1853 return cache_stage;
1854 }
1855 return __idetape_kmalloc_stage(tape, 0, 0);
1856}
1857
Daniel Walkerdcd96372006-06-25 05:47:37 -07001858static 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 -07001859{
1860 struct idetape_bh *bh = tape->bh;
1861 int count;
Daniel Walkerdcd96372006-06-25 05:47:37 -07001862 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001863
1864 while (n) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001865 if (bh == NULL) {
1866 printk(KERN_ERR "ide-tape: bh == NULL in "
1867 "idetape_copy_stage_from_user\n");
Daniel Walkerdcd96372006-06-25 05:47:37 -07001868 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001869 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001870 count = min((unsigned int)(bh->b_size - atomic_read(&bh->b_count)), (unsigned int)n);
Daniel Walkerdcd96372006-06-25 05:47:37 -07001871 if (copy_from_user(bh->b_data + atomic_read(&bh->b_count), buf, count))
1872 ret = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001873 n -= count;
1874 atomic_add(count, &bh->b_count);
1875 buf += count;
1876 if (atomic_read(&bh->b_count) == bh->b_size) {
1877 bh = bh->b_reqnext;
1878 if (bh)
1879 atomic_set(&bh->b_count, 0);
1880 }
1881 }
1882 tape->bh = bh;
Daniel Walkerdcd96372006-06-25 05:47:37 -07001883 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001884}
1885
Daniel Walkerdcd96372006-06-25 05:47:37 -07001886static 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 -07001887{
1888 struct idetape_bh *bh = tape->bh;
1889 int count;
Daniel Walkerdcd96372006-06-25 05:47:37 -07001890 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001891
1892 while (n) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001893 if (bh == NULL) {
1894 printk(KERN_ERR "ide-tape: bh == NULL in "
1895 "idetape_copy_stage_to_user\n");
Daniel Walkerdcd96372006-06-25 05:47:37 -07001896 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001897 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001898 count = min(tape->b_count, n);
Daniel Walkerdcd96372006-06-25 05:47:37 -07001899 if (copy_to_user(buf, tape->b_data, count))
1900 ret = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001901 n -= count;
1902 tape->b_data += count;
1903 tape->b_count -= count;
1904 buf += count;
1905 if (!tape->b_count) {
1906 tape->bh = bh = bh->b_reqnext;
1907 if (bh) {
1908 tape->b_data = bh->b_data;
1909 tape->b_count = atomic_read(&bh->b_count);
1910 }
1911 }
1912 }
Daniel Walkerdcd96372006-06-25 05:47:37 -07001913 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001914}
1915
1916static void idetape_init_merge_stage (idetape_tape_t *tape)
1917{
1918 struct idetape_bh *bh = tape->merge_stage->bh;
1919
1920 tape->bh = bh;
Borislav Petkov54abf372008-02-06 02:57:52 +01001921 if (tape->chrdev_dir == IDETAPE_DIR_WRITE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001922 atomic_set(&bh->b_count, 0);
1923 else {
1924 tape->b_data = bh->b_data;
1925 tape->b_count = atomic_read(&bh->b_count);
1926 }
1927}
1928
1929static void idetape_switch_buffers (idetape_tape_t *tape, idetape_stage_t *stage)
1930{
1931 struct idetape_bh *tmp;
1932
1933 tmp = stage->bh;
1934 stage->bh = tape->merge_stage->bh;
1935 tape->merge_stage->bh = tmp;
1936 idetape_init_merge_stage(tape);
1937}
1938
1939/*
1940 * idetape_add_stage_tail adds a new stage at the end of the pipeline.
1941 */
1942static void idetape_add_stage_tail (ide_drive_t *drive,idetape_stage_t *stage)
1943{
1944 idetape_tape_t *tape = drive->driver_data;
1945 unsigned long flags;
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001946
1947 debug_log(DBG_PROCS, "Enter %s\n", __func__);
1948
Linus Torvalds1da177e2005-04-16 15:20:36 -07001949 spin_lock_irqsave(&tape->spinlock, flags);
1950 stage->next = NULL;
1951 if (tape->last_stage != NULL)
1952 tape->last_stage->next=stage;
1953 else
1954 tape->first_stage = tape->next_stage=stage;
1955 tape->last_stage = stage;
1956 if (tape->next_stage == NULL)
1957 tape->next_stage = tape->last_stage;
1958 tape->nr_stages++;
1959 tape->nr_pending_stages++;
1960 spin_unlock_irqrestore(&tape->spinlock, flags);
1961}
1962
1963/*
1964 * idetape_wait_for_request installs a completion in a pending request
1965 * and sleeps until it is serviced.
1966 *
1967 * The caller should ensure that the request will not be serviced
1968 * before we install the completion (usually by disabling interrupts).
1969 */
1970static void idetape_wait_for_request (ide_drive_t *drive, struct request *rq)
1971{
Peter Zijlstra6e9a4732006-09-30 23:28:10 -07001972 DECLARE_COMPLETION_ONSTACK(wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001973 idetape_tape_t *tape = drive->driver_data;
1974
Jens Axboe4aff5e22006-08-10 08:44:47 +02001975 if (rq == NULL || !blk_special_request(rq)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001976 printk (KERN_ERR "ide-tape: bug: Trying to sleep on non-valid request\n");
1977 return;
1978 }
Jens Axboec00895a2006-09-30 20:29:12 +02001979 rq->end_io_data = &wait;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001980 rq->end_io = blk_end_sync_rq;
1981 spin_unlock_irq(&tape->spinlock);
1982 wait_for_completion(&wait);
1983 /* The stage and its struct request have been deallocated */
1984 spin_lock_irq(&tape->spinlock);
1985}
1986
Borislav Petkova2f5b7f2008-02-06 02:57:51 +01001987static ide_startstop_t idetape_read_position_callback(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001988{
1989 idetape_tape_t *tape = drive->driver_data;
Borislav Petkova2f5b7f2008-02-06 02:57:51 +01001990 u8 *readpos = tape->pc->buffer;
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001991
1992 debug_log(DBG_PROCS, "Enter %s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001993
1994 if (!tape->pc->error) {
Borislav Petkova2f5b7f2008-02-06 02:57:51 +01001995 debug_log(DBG_SENSE, "BOP - %s\n",
1996 (readpos[0] & 0x80) ? "Yes" : "No");
1997 debug_log(DBG_SENSE, "EOP - %s\n",
1998 (readpos[0] & 0x40) ? "Yes" : "No");
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001999
Borislav Petkova2f5b7f2008-02-06 02:57:51 +01002000 if (readpos[0] & 0x4) {
2001 printk(KERN_INFO "ide-tape: Block location is unknown"
2002 "to the tape\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002003 clear_bit(IDETAPE_ADDRESS_VALID, &tape->flags);
2004 idetape_end_request(drive, 0, 0);
2005 } else {
Borislav Petkov8004a8c2008-02-06 02:57:51 +01002006 debug_log(DBG_SENSE, "Block Location - %u\n",
Borislav Petkova2f5b7f2008-02-06 02:57:51 +01002007 be32_to_cpu(*(u32 *)&readpos[4]));
Borislav Petkov8004a8c2008-02-06 02:57:51 +01002008
Borislav Petkova2f5b7f2008-02-06 02:57:51 +01002009 tape->partition = readpos[1];
2010 tape->first_frame_position =
2011 be32_to_cpu(*(u32 *)&readpos[4]);
2012 tape->last_frame_position =
2013 be32_to_cpu(*(u32 *)&readpos[8]);
2014 tape->blocks_in_buffer = readpos[15];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002015 set_bit(IDETAPE_ADDRESS_VALID, &tape->flags);
2016 idetape_end_request(drive, 1, 0);
2017 }
2018 } else {
2019 idetape_end_request(drive, 0, 0);
2020 }
2021 return ide_stopped;
2022}
2023
2024/*
2025 * idetape_create_write_filemark_cmd will:
2026 *
2027 * 1. Write a filemark if write_filemark=1.
2028 * 2. Flush the device buffers without writing a filemark
2029 * if write_filemark=0.
2030 *
2031 */
2032static void idetape_create_write_filemark_cmd (ide_drive_t *drive, idetape_pc_t *pc,int write_filemark)
2033{
2034 idetape_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +01002035 pc->c[0] = WRITE_FILEMARKS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002036 pc->c[4] = write_filemark;
2037 set_bit(PC_WAIT_FOR_DSC, &pc->flags);
2038 pc->callback = &idetape_pc_callback;
2039}
2040
2041static void idetape_create_test_unit_ready_cmd(idetape_pc_t *pc)
2042{
2043 idetape_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +01002044 pc->c[0] = TEST_UNIT_READY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002045 pc->callback = &idetape_pc_callback;
2046}
2047
2048/*
2049 * idetape_queue_pc_tail is based on the following functions:
2050 *
2051 * ide_do_drive_cmd from ide.c
2052 * cdrom_queue_request and cdrom_queue_packet_command from ide-cd.c
2053 *
2054 * We add a special packet command request to the tail of the request
2055 * queue, and wait for it to be serviced.
2056 *
2057 * This is not to be called from within the request handling part
2058 * of the driver ! We allocate here data in the stack, and it is valid
2059 * until the request is finished. This is not the case for the bottom
2060 * part of the driver, where we are always leaving the functions to wait
2061 * for an interrupt or a timer event.
2062 *
2063 * From the bottom part of the driver, we should allocate safe memory
2064 * using idetape_next_pc_storage and idetape_next_rq_storage, and add
2065 * the request to the request list without waiting for it to be serviced !
2066 * In that case, we usually use idetape_queue_pc_head.
2067 */
2068static int __idetape_queue_pc_tail (ide_drive_t *drive, idetape_pc_t *pc)
2069{
2070 struct ide_tape_obj *tape = drive->driver_data;
2071 struct request rq;
2072
2073 idetape_init_rq(&rq, REQ_IDETAPE_PC1);
2074 rq.buffer = (char *) pc;
2075 rq.rq_disk = tape->disk;
2076 return ide_do_drive_cmd(drive, &rq, ide_wait);
2077}
2078
2079static void idetape_create_load_unload_cmd (ide_drive_t *drive, idetape_pc_t *pc,int cmd)
2080{
2081 idetape_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +01002082 pc->c[0] = START_STOP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002083 pc->c[4] = cmd;
2084 set_bit(PC_WAIT_FOR_DSC, &pc->flags);
2085 pc->callback = &idetape_pc_callback;
2086}
2087
2088static int idetape_wait_ready(ide_drive_t *drive, unsigned long timeout)
2089{
2090 idetape_tape_t *tape = drive->driver_data;
2091 idetape_pc_t pc;
2092 int load_attempted = 0;
2093
2094 /*
2095 * Wait for the tape to become ready
2096 */
2097 set_bit(IDETAPE_MEDIUM_PRESENT, &tape->flags);
2098 timeout += jiffies;
2099 while (time_before(jiffies, timeout)) {
2100 idetape_create_test_unit_ready_cmd(&pc);
2101 if (!__idetape_queue_pc_tail(drive, &pc))
2102 return 0;
2103 if ((tape->sense_key == 2 && tape->asc == 4 && tape->ascq == 2)
2104 || (tape->asc == 0x3A)) { /* no media */
2105 if (load_attempted)
2106 return -ENOMEDIUM;
2107 idetape_create_load_unload_cmd(drive, &pc, IDETAPE_LU_LOAD_MASK);
2108 __idetape_queue_pc_tail(drive, &pc);
2109 load_attempted = 1;
2110 /* not about to be ready */
2111 } else if (!(tape->sense_key == 2 && tape->asc == 4 &&
2112 (tape->ascq == 1 || tape->ascq == 8)))
2113 return -EIO;
Nishanth Aravamudan80ce45f2005-09-10 00:27:08 -07002114 msleep(100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002115 }
2116 return -EIO;
2117}
2118
2119static int idetape_queue_pc_tail (ide_drive_t *drive,idetape_pc_t *pc)
2120{
2121 return __idetape_queue_pc_tail(drive, pc);
2122}
2123
2124static int idetape_flush_tape_buffers (ide_drive_t *drive)
2125{
2126 idetape_pc_t pc;
2127 int rc;
2128
2129 idetape_create_write_filemark_cmd(drive, &pc, 0);
2130 if ((rc = idetape_queue_pc_tail(drive, &pc)))
2131 return rc;
2132 idetape_wait_ready(drive, 60 * 5 * HZ);
2133 return 0;
2134}
2135
2136static void idetape_create_read_position_cmd (idetape_pc_t *pc)
2137{
2138 idetape_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +01002139 pc->c[0] = READ_POSITION;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002140 pc->request_transfer = 20;
2141 pc->callback = &idetape_read_position_callback;
2142}
2143
2144static int idetape_read_position (ide_drive_t *drive)
2145{
2146 idetape_tape_t *tape = drive->driver_data;
2147 idetape_pc_t pc;
2148 int position;
2149
Borislav Petkov8004a8c2008-02-06 02:57:51 +01002150 debug_log(DBG_PROCS, "Enter %s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002151
2152 idetape_create_read_position_cmd(&pc);
2153 if (idetape_queue_pc_tail(drive, &pc))
2154 return -1;
2155 position = tape->first_frame_position;
2156 return position;
2157}
2158
2159static void idetape_create_locate_cmd (ide_drive_t *drive, idetape_pc_t *pc, unsigned int block, u8 partition, int skip)
2160{
2161 idetape_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +01002162 pc->c[0] = POSITION_TO_ELEMENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002163 pc->c[1] = 2;
Borislav Petkov860ff5e2008-02-02 19:56:50 +01002164 put_unaligned(cpu_to_be32(block), (unsigned int *) &pc->c[3]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002165 pc->c[8] = partition;
2166 set_bit(PC_WAIT_FOR_DSC, &pc->flags);
2167 pc->callback = &idetape_pc_callback;
2168}
2169
2170static int idetape_create_prevent_cmd (ide_drive_t *drive, idetape_pc_t *pc, int prevent)
2171{
2172 idetape_tape_t *tape = drive->driver_data;
2173
Borislav Petkovb6422012008-02-02 19:56:49 +01002174 /* device supports locking according to capabilities page */
2175 if (!(tape->caps[6] & 0x01))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002176 return 0;
2177
2178 idetape_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +01002179 pc->c[0] = ALLOW_MEDIUM_REMOVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002180 pc->c[4] = prevent;
2181 pc->callback = &idetape_pc_callback;
2182 return 1;
2183}
2184
2185static int __idetape_discard_read_pipeline (ide_drive_t *drive)
2186{
2187 idetape_tape_t *tape = drive->driver_data;
2188 unsigned long flags;
2189 int cnt;
2190
Borislav Petkov54abf372008-02-06 02:57:52 +01002191 if (tape->chrdev_dir != IDETAPE_DIR_READ)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002192 return 0;
2193
2194 /* Remove merge stage. */
2195 cnt = tape->merge_stage_size / tape->tape_block_size;
2196 if (test_and_clear_bit(IDETAPE_FILEMARK, &tape->flags))
2197 ++cnt; /* Filemarks count as 1 sector */
2198 tape->merge_stage_size = 0;
2199 if (tape->merge_stage != NULL) {
2200 __idetape_kfree_stage(tape->merge_stage);
2201 tape->merge_stage = NULL;
2202 }
2203
2204 /* Clear pipeline flags. */
2205 clear_bit(IDETAPE_PIPELINE_ERROR, &tape->flags);
Borislav Petkov54abf372008-02-06 02:57:52 +01002206 tape->chrdev_dir = IDETAPE_DIR_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002207
2208 /* Remove pipeline stages. */
2209 if (tape->first_stage == NULL)
2210 return 0;
2211
2212 spin_lock_irqsave(&tape->spinlock, flags);
2213 tape->next_stage = NULL;
2214 if (idetape_pipeline_active(tape))
2215 idetape_wait_for_request(drive, tape->active_data_request);
2216 spin_unlock_irqrestore(&tape->spinlock, flags);
2217
2218 while (tape->first_stage != NULL) {
2219 struct request *rq_ptr = &tape->first_stage->rq;
2220
2221 cnt += rq_ptr->nr_sectors - rq_ptr->current_nr_sectors;
2222 if (rq_ptr->errors == IDETAPE_ERROR_FILEMARK)
2223 ++cnt;
2224 idetape_remove_stage_head(drive);
2225 }
2226 tape->nr_pending_stages = 0;
2227 tape->max_stages = tape->min_pipeline;
2228 return cnt;
2229}
2230
2231/*
2232 * idetape_position_tape positions the tape to the requested block
2233 * using the LOCATE packet command. A READ POSITION command is then
2234 * issued to check where we are positioned.
2235 *
2236 * Like all higher level operations, we queue the commands at the tail
2237 * of the request queue and wait for their completion.
2238 *
2239 */
2240static int idetape_position_tape (ide_drive_t *drive, unsigned int block, u8 partition, int skip)
2241{
2242 idetape_tape_t *tape = drive->driver_data;
2243 int retval;
2244 idetape_pc_t pc;
2245
Borislav Petkov54abf372008-02-06 02:57:52 +01002246 if (tape->chrdev_dir == IDETAPE_DIR_READ)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002247 __idetape_discard_read_pipeline(drive);
2248 idetape_wait_ready(drive, 60 * 5 * HZ);
2249 idetape_create_locate_cmd(drive, &pc, block, partition, skip);
2250 retval = idetape_queue_pc_tail(drive, &pc);
2251 if (retval)
2252 return (retval);
2253
2254 idetape_create_read_position_cmd(&pc);
2255 return (idetape_queue_pc_tail(drive, &pc));
2256}
2257
2258static void idetape_discard_read_pipeline (ide_drive_t *drive, int restore_position)
2259{
2260 idetape_tape_t *tape = drive->driver_data;
2261 int cnt;
2262 int seek, position;
2263
2264 cnt = __idetape_discard_read_pipeline(drive);
2265 if (restore_position) {
2266 position = idetape_read_position(drive);
2267 seek = position > cnt ? position - cnt : 0;
2268 if (idetape_position_tape(drive, seek, 0, 0)) {
2269 printk(KERN_INFO "ide-tape: %s: position_tape failed in discard_pipeline()\n", tape->name);
2270 return;
2271 }
2272 }
2273}
2274
2275/*
2276 * idetape_queue_rw_tail generates a read/write request for the block
2277 * device interface and wait for it to be serviced.
2278 */
2279static int idetape_queue_rw_tail(ide_drive_t *drive, int cmd, int blocks, struct idetape_bh *bh)
2280{
2281 idetape_tape_t *tape = drive->driver_data;
2282 struct request rq;
2283
Borislav Petkov8004a8c2008-02-06 02:57:51 +01002284 debug_log(DBG_SENSE, "%s: cmd=%d\n", __func__, cmd);
2285
Linus Torvalds1da177e2005-04-16 15:20:36 -07002286 if (idetape_pipeline_active(tape)) {
Borislav Petkov8004a8c2008-02-06 02:57:51 +01002287 printk(KERN_ERR "ide-tape: bug: the pipeline is active in %s\n",
2288 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002289 return (0);
2290 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002291
2292 idetape_init_rq(&rq, cmd);
2293 rq.rq_disk = tape->disk;
2294 rq.special = (void *)bh;
2295 rq.sector = tape->first_frame_position;
2296 rq.nr_sectors = rq.current_nr_sectors = blocks;
2297 (void) ide_do_drive_cmd(drive, &rq, ide_wait);
2298
2299 if ((cmd & (REQ_IDETAPE_READ | REQ_IDETAPE_WRITE)) == 0)
2300 return 0;
2301
2302 if (tape->merge_stage)
2303 idetape_init_merge_stage(tape);
2304 if (rq.errors == IDETAPE_ERROR_GENERAL)
2305 return -EIO;
2306 return (tape->tape_block_size * (blocks-rq.current_nr_sectors));
2307}
2308
2309/*
2310 * idetape_insert_pipeline_into_queue is used to start servicing the
2311 * pipeline stages, starting from tape->next_stage.
2312 */
2313static void idetape_insert_pipeline_into_queue (ide_drive_t *drive)
2314{
2315 idetape_tape_t *tape = drive->driver_data;
2316
2317 if (tape->next_stage == NULL)
2318 return;
2319 if (!idetape_pipeline_active(tape)) {
2320 set_bit(IDETAPE_PIPELINE_ACTIVE, &tape->flags);
Borislav Petkov419d4742008-02-02 19:56:51 +01002321 idetape_activate_next_stage(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002322 (void) ide_do_drive_cmd(drive, tape->active_data_request, ide_end);
2323 }
2324}
2325
2326static void idetape_create_inquiry_cmd (idetape_pc_t *pc)
2327{
2328 idetape_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +01002329 pc->c[0] = INQUIRY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002330 pc->c[4] = pc->request_transfer = 254;
2331 pc->callback = &idetape_pc_callback;
2332}
2333
2334static void idetape_create_rewind_cmd (ide_drive_t *drive, idetape_pc_t *pc)
2335{
2336 idetape_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +01002337 pc->c[0] = REZERO_UNIT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002338 set_bit(PC_WAIT_FOR_DSC, &pc->flags);
2339 pc->callback = &idetape_pc_callback;
2340}
2341
Linus Torvalds1da177e2005-04-16 15:20:36 -07002342static void idetape_create_erase_cmd (idetape_pc_t *pc)
2343{
2344 idetape_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +01002345 pc->c[0] = ERASE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002346 pc->c[1] = 1;
2347 set_bit(PC_WAIT_FOR_DSC, &pc->flags);
2348 pc->callback = &idetape_pc_callback;
2349}
2350
2351static void idetape_create_space_cmd (idetape_pc_t *pc,int count, u8 cmd)
2352{
2353 idetape_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +01002354 pc->c[0] = SPACE;
Borislav Petkov860ff5e2008-02-02 19:56:50 +01002355 put_unaligned(cpu_to_be32(count), (unsigned int *) &pc->c[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002356 pc->c[1] = cmd;
2357 set_bit(PC_WAIT_FOR_DSC, &pc->flags);
2358 pc->callback = &idetape_pc_callback;
2359}
2360
2361static void idetape_wait_first_stage (ide_drive_t *drive)
2362{
2363 idetape_tape_t *tape = drive->driver_data;
2364 unsigned long flags;
2365
2366 if (tape->first_stage == NULL)
2367 return;
2368 spin_lock_irqsave(&tape->spinlock, flags);
2369 if (tape->active_stage == tape->first_stage)
2370 idetape_wait_for_request(drive, tape->active_data_request);
2371 spin_unlock_irqrestore(&tape->spinlock, flags);
2372}
2373
2374/*
2375 * idetape_add_chrdev_write_request tries to add a character device
2376 * originated write request to our pipeline. In case we don't succeed,
2377 * we revert to non-pipelined operation mode for this request.
2378 *
2379 * 1. Try to allocate a new pipeline stage.
2380 * 2. If we can't, wait for more and more requests to be serviced
2381 * and try again each time.
2382 * 3. If we still can't allocate a stage, fallback to
2383 * non-pipelined operation mode for this request.
2384 */
2385static int idetape_add_chrdev_write_request (ide_drive_t *drive, int blocks)
2386{
2387 idetape_tape_t *tape = drive->driver_data;
2388 idetape_stage_t *new_stage;
2389 unsigned long flags;
2390 struct request *rq;
2391
Borislav Petkov8004a8c2008-02-06 02:57:51 +01002392 debug_log(DBG_CHRDEV, "Enter %s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002393
2394 /*
2395 * Attempt to allocate a new stage.
2396 * Pay special attention to possible race conditions.
2397 */
2398 while ((new_stage = idetape_kmalloc_stage(tape)) == NULL) {
2399 spin_lock_irqsave(&tape->spinlock, flags);
2400 if (idetape_pipeline_active(tape)) {
2401 idetape_wait_for_request(drive, tape->active_data_request);
2402 spin_unlock_irqrestore(&tape->spinlock, flags);
2403 } else {
2404 spin_unlock_irqrestore(&tape->spinlock, flags);
2405 idetape_insert_pipeline_into_queue(drive);
2406 if (idetape_pipeline_active(tape))
2407 continue;
2408 /*
2409 * Linux is short on memory. Fallback to
2410 * non-pipelined operation mode for this request.
2411 */
2412 return idetape_queue_rw_tail(drive, REQ_IDETAPE_WRITE, blocks, tape->merge_stage->bh);
2413 }
2414 }
2415 rq = &new_stage->rq;
2416 idetape_init_rq(rq, REQ_IDETAPE_WRITE);
2417 /* Doesn't actually matter - We always assume sequential access */
2418 rq->sector = tape->first_frame_position;
2419 rq->nr_sectors = rq->current_nr_sectors = blocks;
2420
2421 idetape_switch_buffers(tape, new_stage);
2422 idetape_add_stage_tail(drive, new_stage);
2423 tape->pipeline_head++;
Borislav Petkov37016ba2008-02-06 02:57:52 +01002424 idetape_calculate_speeds(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002425
2426 /*
2427 * Estimate whether the tape has stopped writing by checking
2428 * if our write pipeline is currently empty. If we are not
2429 * writing anymore, wait for the pipeline to be full enough
2430 * (90%) before starting to service requests, so that we will
2431 * be able to keep up with the higher speeds of the tape.
2432 */
2433 if (!idetape_pipeline_active(tape)) {
2434 if (tape->nr_stages >= tape->max_stages * 9 / 10 ||
2435 tape->nr_stages >= tape->max_stages - tape->uncontrolled_pipeline_head_speed * 3 * 1024 / tape->tape_block_size) {
2436 tape->measure_insert_time = 1;
2437 tape->insert_time = jiffies;
2438 tape->insert_size = 0;
2439 tape->insert_speed = 0;
2440 idetape_insert_pipeline_into_queue(drive);
2441 }
2442 }
2443 if (test_and_clear_bit(IDETAPE_PIPELINE_ERROR, &tape->flags))
2444 /* Return a deferred error */
2445 return -EIO;
2446 return blocks;
2447}
2448
2449/*
2450 * idetape_wait_for_pipeline will wait until all pending pipeline
2451 * requests are serviced. Typically called on device close.
2452 */
2453static void idetape_wait_for_pipeline (ide_drive_t *drive)
2454{
2455 idetape_tape_t *tape = drive->driver_data;
2456 unsigned long flags;
2457
2458 while (tape->next_stage || idetape_pipeline_active(tape)) {
2459 idetape_insert_pipeline_into_queue(drive);
2460 spin_lock_irqsave(&tape->spinlock, flags);
2461 if (idetape_pipeline_active(tape))
2462 idetape_wait_for_request(drive, tape->active_data_request);
2463 spin_unlock_irqrestore(&tape->spinlock, flags);
2464 }
2465}
2466
2467static void idetape_empty_write_pipeline (ide_drive_t *drive)
2468{
2469 idetape_tape_t *tape = drive->driver_data;
2470 int blocks, min;
2471 struct idetape_bh *bh;
Borislav Petkov55a5d292008-02-02 19:56:49 +01002472
Borislav Petkov54abf372008-02-06 02:57:52 +01002473 if (tape->chrdev_dir != IDETAPE_DIR_WRITE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002474 printk(KERN_ERR "ide-tape: bug: Trying to empty write pipeline, but we are not writing.\n");
2475 return;
2476 }
2477 if (tape->merge_stage_size > tape->stage_size) {
2478 printk(KERN_ERR "ide-tape: bug: merge_buffer too big\n");
2479 tape->merge_stage_size = tape->stage_size;
2480 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002481 if (tape->merge_stage_size) {
2482 blocks = tape->merge_stage_size / tape->tape_block_size;
2483 if (tape->merge_stage_size % tape->tape_block_size) {
2484 unsigned int i;
2485
2486 blocks++;
2487 i = tape->tape_block_size - tape->merge_stage_size % tape->tape_block_size;
2488 bh = tape->bh->b_reqnext;
2489 while (bh) {
2490 atomic_set(&bh->b_count, 0);
2491 bh = bh->b_reqnext;
2492 }
2493 bh = tape->bh;
2494 while (i) {
2495 if (bh == NULL) {
2496
2497 printk(KERN_INFO "ide-tape: bug, bh NULL\n");
2498 break;
2499 }
2500 min = min(i, (unsigned int)(bh->b_size - atomic_read(&bh->b_count)));
2501 memset(bh->b_data + atomic_read(&bh->b_count), 0, min);
2502 atomic_add(min, &bh->b_count);
2503 i -= min;
2504 bh = bh->b_reqnext;
2505 }
2506 }
2507 (void) idetape_add_chrdev_write_request(drive, blocks);
2508 tape->merge_stage_size = 0;
2509 }
2510 idetape_wait_for_pipeline(drive);
2511 if (tape->merge_stage != NULL) {
2512 __idetape_kfree_stage(tape->merge_stage);
2513 tape->merge_stage = NULL;
2514 }
2515 clear_bit(IDETAPE_PIPELINE_ERROR, &tape->flags);
Borislav Petkov54abf372008-02-06 02:57:52 +01002516 tape->chrdev_dir = IDETAPE_DIR_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002517
2518 /*
2519 * On the next backup, perform the feedback loop again.
2520 * (I don't want to keep sense information between backups,
2521 * as some systems are constantly on, and the system load
2522 * can be totally different on the next backup).
2523 */
2524 tape->max_stages = tape->min_pipeline;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002525 if (tape->first_stage != NULL ||
2526 tape->next_stage != NULL ||
2527 tape->last_stage != NULL ||
2528 tape->nr_stages != 0) {
2529 printk(KERN_ERR "ide-tape: ide-tape pipeline bug, "
2530 "first_stage %p, next_stage %p, "
2531 "last_stage %p, nr_stages %d\n",
2532 tape->first_stage, tape->next_stage,
2533 tape->last_stage, tape->nr_stages);
2534 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002535}
2536
2537static void idetape_restart_speed_control (ide_drive_t *drive)
2538{
2539 idetape_tape_t *tape = drive->driver_data;
2540
2541 tape->restart_speed_control_req = 0;
2542 tape->pipeline_head = 0;
2543 tape->controlled_last_pipeline_head = tape->uncontrolled_last_pipeline_head = 0;
2544 tape->controlled_previous_pipeline_head = tape->uncontrolled_previous_pipeline_head = 0;
2545 tape->pipeline_head_speed = tape->controlled_pipeline_head_speed = 5000;
2546 tape->uncontrolled_pipeline_head_speed = 0;
2547 tape->controlled_pipeline_head_time = tape->uncontrolled_pipeline_head_time = jiffies;
2548 tape->controlled_previous_head_time = tape->uncontrolled_previous_head_time = jiffies;
2549}
2550
2551static int idetape_initiate_read (ide_drive_t *drive, int max_stages)
2552{
2553 idetape_tape_t *tape = drive->driver_data;
2554 idetape_stage_t *new_stage;
2555 struct request rq;
2556 int bytes_read;
Borislav Petkovb6422012008-02-02 19:56:49 +01002557 u16 blocks = *(u16 *)&tape->caps[12];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002558
2559 /* Initialize read operation */
Borislav Petkov54abf372008-02-06 02:57:52 +01002560 if (tape->chrdev_dir != IDETAPE_DIR_READ) {
2561 if (tape->chrdev_dir == IDETAPE_DIR_WRITE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002562 idetape_empty_write_pipeline(drive);
2563 idetape_flush_tape_buffers(drive);
2564 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002565 if (tape->merge_stage || tape->merge_stage_size) {
2566 printk (KERN_ERR "ide-tape: merge_stage_size should be 0 now\n");
2567 tape->merge_stage_size = 0;
2568 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002569 if ((tape->merge_stage = __idetape_kmalloc_stage(tape, 0, 0)) == NULL)
2570 return -ENOMEM;
Borislav Petkov54abf372008-02-06 02:57:52 +01002571 tape->chrdev_dir = IDETAPE_DIR_READ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002572
2573 /*
2574 * Issue a read 0 command to ensure that DSC handshake
2575 * is switched from completion mode to buffer available
2576 * mode.
2577 * No point in issuing this if DSC overlap isn't supported,
2578 * some drives (Seagate STT3401A) will return an error.
2579 */
2580 if (drive->dsc_overlap) {
2581 bytes_read = idetape_queue_rw_tail(drive, REQ_IDETAPE_READ, 0, tape->merge_stage->bh);
2582 if (bytes_read < 0) {
2583 __idetape_kfree_stage(tape->merge_stage);
2584 tape->merge_stage = NULL;
Borislav Petkov54abf372008-02-06 02:57:52 +01002585 tape->chrdev_dir = IDETAPE_DIR_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002586 return bytes_read;
2587 }
2588 }
2589 }
2590 if (tape->restart_speed_control_req)
2591 idetape_restart_speed_control(drive);
2592 idetape_init_rq(&rq, REQ_IDETAPE_READ);
2593 rq.sector = tape->first_frame_position;
2594 rq.nr_sectors = rq.current_nr_sectors = blocks;
2595 if (!test_bit(IDETAPE_PIPELINE_ERROR, &tape->flags) &&
2596 tape->nr_stages < max_stages) {
2597 new_stage = idetape_kmalloc_stage(tape);
2598 while (new_stage != NULL) {
2599 new_stage->rq = rq;
2600 idetape_add_stage_tail(drive, new_stage);
2601 if (tape->nr_stages >= max_stages)
2602 break;
2603 new_stage = idetape_kmalloc_stage(tape);
2604 }
2605 }
2606 if (!idetape_pipeline_active(tape)) {
2607 if (tape->nr_pending_stages >= 3 * max_stages / 4) {
2608 tape->measure_insert_time = 1;
2609 tape->insert_time = jiffies;
2610 tape->insert_size = 0;
2611 tape->insert_speed = 0;
2612 idetape_insert_pipeline_into_queue(drive);
2613 }
2614 }
2615 return 0;
2616}
2617
2618/*
2619 * idetape_add_chrdev_read_request is called from idetape_chrdev_read
2620 * to service a character device read request and add read-ahead
2621 * requests to our pipeline.
2622 */
2623static int idetape_add_chrdev_read_request (ide_drive_t *drive,int blocks)
2624{
2625 idetape_tape_t *tape = drive->driver_data;
2626 unsigned long flags;
2627 struct request *rq_ptr;
2628 int bytes_read;
2629
Borislav Petkov8004a8c2008-02-06 02:57:51 +01002630 debug_log(DBG_PROCS, "Enter %s, %d blocks\n", __func__, blocks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002631
2632 /*
2633 * If we are at a filemark, return a read length of 0
2634 */
2635 if (test_bit(IDETAPE_FILEMARK, &tape->flags))
2636 return 0;
2637
2638 /*
2639 * Wait for the next block to be available at the head
2640 * of the pipeline
2641 */
2642 idetape_initiate_read(drive, tape->max_stages);
2643 if (tape->first_stage == NULL) {
2644 if (test_bit(IDETAPE_PIPELINE_ERROR, &tape->flags))
2645 return 0;
2646 return idetape_queue_rw_tail(drive, REQ_IDETAPE_READ, blocks, tape->merge_stage->bh);
2647 }
2648 idetape_wait_first_stage(drive);
2649 rq_ptr = &tape->first_stage->rq;
2650 bytes_read = tape->tape_block_size * (rq_ptr->nr_sectors - rq_ptr->current_nr_sectors);
2651 rq_ptr->nr_sectors = rq_ptr->current_nr_sectors = 0;
2652
2653
2654 if (rq_ptr->errors == IDETAPE_ERROR_EOD)
2655 return 0;
2656 else {
2657 idetape_switch_buffers(tape, tape->first_stage);
2658 if (rq_ptr->errors == IDETAPE_ERROR_FILEMARK)
2659 set_bit(IDETAPE_FILEMARK, &tape->flags);
2660 spin_lock_irqsave(&tape->spinlock, flags);
2661 idetape_remove_stage_head(drive);
2662 spin_unlock_irqrestore(&tape->spinlock, flags);
2663 tape->pipeline_head++;
Borislav Petkov37016ba2008-02-06 02:57:52 +01002664 idetape_calculate_speeds(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002665 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002666 if (bytes_read > blocks * tape->tape_block_size) {
2667 printk(KERN_ERR "ide-tape: bug: trying to return more bytes than requested\n");
2668 bytes_read = blocks * tape->tape_block_size;
2669 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002670 return (bytes_read);
2671}
2672
2673static void idetape_pad_zeros (ide_drive_t *drive, int bcount)
2674{
2675 idetape_tape_t *tape = drive->driver_data;
2676 struct idetape_bh *bh;
2677 int blocks;
2678
2679 while (bcount) {
2680 unsigned int count;
2681
2682 bh = tape->merge_stage->bh;
2683 count = min(tape->stage_size, bcount);
2684 bcount -= count;
2685 blocks = count / tape->tape_block_size;
2686 while (count) {
2687 atomic_set(&bh->b_count, min(count, (unsigned int)bh->b_size));
2688 memset(bh->b_data, 0, atomic_read(&bh->b_count));
2689 count -= atomic_read(&bh->b_count);
2690 bh = bh->b_reqnext;
2691 }
2692 idetape_queue_rw_tail(drive, REQ_IDETAPE_WRITE, blocks, tape->merge_stage->bh);
2693 }
2694}
2695
2696static int idetape_pipeline_size (ide_drive_t *drive)
2697{
2698 idetape_tape_t *tape = drive->driver_data;
2699 idetape_stage_t *stage;
2700 struct request *rq;
2701 int size = 0;
2702
2703 idetape_wait_for_pipeline(drive);
2704 stage = tape->first_stage;
2705 while (stage != NULL) {
2706 rq = &stage->rq;
2707 size += tape->tape_block_size * (rq->nr_sectors-rq->current_nr_sectors);
2708 if (rq->errors == IDETAPE_ERROR_FILEMARK)
2709 size += tape->tape_block_size;
2710 stage = stage->next;
2711 }
2712 size += tape->merge_stage_size;
2713 return size;
2714}
2715
2716/*
2717 * Rewinds the tape to the Beginning Of the current Partition (BOP).
2718 *
2719 * We currently support only one partition.
2720 */
2721static int idetape_rewind_tape (ide_drive_t *drive)
2722{
2723 int retval;
2724 idetape_pc_t pc;
Borislav Petkov8004a8c2008-02-06 02:57:51 +01002725 idetape_tape_t *tape;
2726 tape = drive->driver_data;
2727
2728 debug_log(DBG_SENSE, "Enter %s\n", __func__);
2729
Linus Torvalds1da177e2005-04-16 15:20:36 -07002730 idetape_create_rewind_cmd(drive, &pc);
2731 retval = idetape_queue_pc_tail(drive, &pc);
2732 if (retval)
2733 return retval;
2734
2735 idetape_create_read_position_cmd(&pc);
2736 retval = idetape_queue_pc_tail(drive, &pc);
2737 if (retval)
2738 return retval;
2739 return 0;
2740}
2741
2742/*
2743 * Our special ide-tape ioctl's.
2744 *
2745 * Currently there aren't any ioctl's.
2746 * mtio.h compatible commands should be issued to the character device
2747 * interface.
2748 */
2749static int idetape_blkdev_ioctl(ide_drive_t *drive, unsigned int cmd, unsigned long arg)
2750{
2751 idetape_tape_t *tape = drive->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002752 void __user *argp = (void __user *)arg;
2753
Borislav Petkovd59823f2008-02-02 19:56:51 +01002754 struct idetape_config {
2755 int dsc_rw_frequency;
2756 int dsc_media_access_frequency;
2757 int nr_stages;
2758 } config;
2759
Borislav Petkov8004a8c2008-02-06 02:57:51 +01002760 debug_log(DBG_PROCS, "Enter %s\n", __func__);
2761
Linus Torvalds1da177e2005-04-16 15:20:36 -07002762 switch (cmd) {
2763 case 0x0340:
Borislav Petkovd59823f2008-02-02 19:56:51 +01002764 if (copy_from_user(&config, argp, sizeof(config)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002765 return -EFAULT;
2766 tape->best_dsc_rw_frequency = config.dsc_rw_frequency;
2767 tape->max_stages = config.nr_stages;
2768 break;
2769 case 0x0350:
2770 config.dsc_rw_frequency = (int) tape->best_dsc_rw_frequency;
2771 config.nr_stages = tape->max_stages;
Borislav Petkovd59823f2008-02-02 19:56:51 +01002772 if (copy_to_user(argp, &config, sizeof(config)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002773 return -EFAULT;
2774 break;
2775 default:
2776 return -EIO;
2777 }
2778 return 0;
2779}
2780
2781/*
2782 * idetape_space_over_filemarks is now a bit more complicated than just
2783 * passing the command to the tape since we may have crossed some
2784 * filemarks during our pipelined read-ahead mode.
2785 *
2786 * As a minor side effect, the pipeline enables us to support MTFSFM when
2787 * the filemark is in our internal pipeline even if the tape doesn't
2788 * support spacing over filemarks in the reverse direction.
2789 */
2790static int idetape_space_over_filemarks (ide_drive_t *drive,short mt_op,int mt_count)
2791{
2792 idetape_tape_t *tape = drive->driver_data;
2793 idetape_pc_t pc;
2794 unsigned long flags;
2795 int retval,count=0;
Borislav Petkovb6422012008-02-02 19:56:49 +01002796 int sprev = !!(tape->caps[4] & 0x20);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002797
2798 if (mt_count == 0)
2799 return 0;
2800 if (MTBSF == mt_op || MTBSFM == mt_op) {
Borislav Petkovb6422012008-02-02 19:56:49 +01002801 if (!sprev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002802 return -EIO;
2803 mt_count = - mt_count;
2804 }
2805
Borislav Petkov54abf372008-02-06 02:57:52 +01002806 if (tape->chrdev_dir == IDETAPE_DIR_READ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002807 /*
2808 * We have a read-ahead buffer. Scan it for crossed
2809 * filemarks.
2810 */
2811 tape->merge_stage_size = 0;
2812 if (test_and_clear_bit(IDETAPE_FILEMARK, &tape->flags))
2813 ++count;
2814 while (tape->first_stage != NULL) {
2815 if (count == mt_count) {
2816 if (mt_op == MTFSFM)
2817 set_bit(IDETAPE_FILEMARK, &tape->flags);
2818 return 0;
2819 }
2820 spin_lock_irqsave(&tape->spinlock, flags);
2821 if (tape->first_stage == tape->active_stage) {
2822 /*
2823 * We have reached the active stage in the read pipeline.
2824 * There is no point in allowing the drive to continue
2825 * reading any farther, so we stop the pipeline.
2826 *
2827 * This section should be moved to a separate subroutine,
2828 * because a similar function is performed in
2829 * __idetape_discard_read_pipeline(), for example.
2830 */
2831 tape->next_stage = NULL;
2832 spin_unlock_irqrestore(&tape->spinlock, flags);
2833 idetape_wait_first_stage(drive);
2834 tape->next_stage = tape->first_stage->next;
2835 } else
2836 spin_unlock_irqrestore(&tape->spinlock, flags);
2837 if (tape->first_stage->rq.errors == IDETAPE_ERROR_FILEMARK)
2838 ++count;
2839 idetape_remove_stage_head(drive);
2840 }
2841 idetape_discard_read_pipeline(drive, 0);
2842 }
2843
2844 /*
2845 * The filemark was not found in our internal pipeline.
2846 * Now we can issue the space command.
2847 */
2848 switch (mt_op) {
2849 case MTFSF:
2850 case MTBSF:
2851 idetape_create_space_cmd(&pc,mt_count-count,IDETAPE_SPACE_OVER_FILEMARK);
2852 return (idetape_queue_pc_tail(drive, &pc));
2853 case MTFSFM:
2854 case MTBSFM:
Borislav Petkovb6422012008-02-02 19:56:49 +01002855 if (!sprev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002856 return (-EIO);
2857 retval = idetape_space_over_filemarks(drive, MTFSF, mt_count-count);
2858 if (retval) return (retval);
2859 count = (MTBSFM == mt_op ? 1 : -1);
2860 return (idetape_space_over_filemarks(drive, MTFSF, count));
2861 default:
2862 printk(KERN_ERR "ide-tape: MTIO operation %d not supported\n",mt_op);
2863 return (-EIO);
2864 }
2865}
2866
2867
2868/*
2869 * Our character device read / write functions.
2870 *
2871 * The tape is optimized to maximize throughput when it is transferring
2872 * an integral number of the "continuous transfer limit", which is
2873 * a parameter of the specific tape (26 KB on my particular tape).
2874 * (32 kB for Onstream)
2875 *
2876 * As of version 1.3 of the driver, the character device provides an
2877 * abstract continuous view of the media - any mix of block sizes (even 1
2878 * byte) on the same backup/restore procedure is supported. The driver
2879 * will internally convert the requests to the recommended transfer unit,
2880 * so that an unmatch between the user's block size to the recommended
2881 * size will only result in a (slightly) increased driver overhead, but
2882 * will no longer hit performance.
2883 * This is not applicable to Onstream.
2884 */
2885static ssize_t idetape_chrdev_read (struct file *file, char __user *buf,
2886 size_t count, loff_t *ppos)
2887{
2888 struct ide_tape_obj *tape = ide_tape_f(file);
2889 ide_drive_t *drive = tape->drive;
2890 ssize_t bytes_read,temp, actually_read = 0, rc;
Daniel Walkerdcd96372006-06-25 05:47:37 -07002891 ssize_t ret = 0;
Borislav Petkovb6422012008-02-02 19:56:49 +01002892 u16 ctl = *(u16 *)&tape->caps[12];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002893
Borislav Petkov8004a8c2008-02-06 02:57:51 +01002894 debug_log(DBG_CHRDEV, "Enter %s, count %Zd\n", __func__, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002895
Borislav Petkov54abf372008-02-06 02:57:52 +01002896 if (tape->chrdev_dir != IDETAPE_DIR_READ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002897 if (test_bit(IDETAPE_DETECT_BS, &tape->flags))
2898 if (count > tape->tape_block_size &&
2899 (count % tape->tape_block_size) == 0)
2900 tape->user_bs_factor = count / tape->tape_block_size;
2901 }
2902 if ((rc = idetape_initiate_read(drive, tape->max_stages)) < 0)
2903 return rc;
2904 if (count == 0)
2905 return (0);
2906 if (tape->merge_stage_size) {
2907 actually_read = min((unsigned int)(tape->merge_stage_size), (unsigned int)count);
Daniel Walkerdcd96372006-06-25 05:47:37 -07002908 if (idetape_copy_stage_to_user(tape, buf, tape->merge_stage, actually_read))
2909 ret = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002910 buf += actually_read;
2911 tape->merge_stage_size -= actually_read;
2912 count -= actually_read;
2913 }
2914 while (count >= tape->stage_size) {
Borislav Petkovb6422012008-02-02 19:56:49 +01002915 bytes_read = idetape_add_chrdev_read_request(drive, ctl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002916 if (bytes_read <= 0)
2917 goto finish;
Daniel Walkerdcd96372006-06-25 05:47:37 -07002918 if (idetape_copy_stage_to_user(tape, buf, tape->merge_stage, bytes_read))
2919 ret = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002920 buf += bytes_read;
2921 count -= bytes_read;
2922 actually_read += bytes_read;
2923 }
2924 if (count) {
Borislav Petkovb6422012008-02-02 19:56:49 +01002925 bytes_read = idetape_add_chrdev_read_request(drive, ctl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002926 if (bytes_read <= 0)
2927 goto finish;
2928 temp = min((unsigned long)count, (unsigned long)bytes_read);
Daniel Walkerdcd96372006-06-25 05:47:37 -07002929 if (idetape_copy_stage_to_user(tape, buf, tape->merge_stage, temp))
2930 ret = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002931 actually_read += temp;
2932 tape->merge_stage_size = bytes_read-temp;
2933 }
2934finish:
2935 if (!actually_read && test_bit(IDETAPE_FILEMARK, &tape->flags)) {
Borislav Petkov8004a8c2008-02-06 02:57:51 +01002936 debug_log(DBG_SENSE, "%s: spacing over filemark\n", tape->name);
2937
Linus Torvalds1da177e2005-04-16 15:20:36 -07002938 idetape_space_over_filemarks(drive, MTFSF, 1);
2939 return 0;
2940 }
Daniel Walkerdcd96372006-06-25 05:47:37 -07002941
2942 return (ret) ? ret : actually_read;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002943}
2944
2945static ssize_t idetape_chrdev_write (struct file *file, const char __user *buf,
2946 size_t count, loff_t *ppos)
2947{
2948 struct ide_tape_obj *tape = ide_tape_f(file);
2949 ide_drive_t *drive = tape->drive;
Daniel Walkerdcd96372006-06-25 05:47:37 -07002950 ssize_t actually_written = 0;
2951 ssize_t ret = 0;
Borislav Petkovb6422012008-02-02 19:56:49 +01002952 u16 ctl = *(u16 *)&tape->caps[12];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002953
2954 /* The drive is write protected. */
2955 if (tape->write_prot)
2956 return -EACCES;
2957
Borislav Petkov8004a8c2008-02-06 02:57:51 +01002958 debug_log(DBG_CHRDEV, "Enter %s, count %Zd\n", __func__, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002959
2960 /* Initialize write operation */
Borislav Petkov54abf372008-02-06 02:57:52 +01002961 if (tape->chrdev_dir != IDETAPE_DIR_WRITE) {
2962 if (tape->chrdev_dir == IDETAPE_DIR_READ)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002963 idetape_discard_read_pipeline(drive, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002964 if (tape->merge_stage || tape->merge_stage_size) {
2965 printk(KERN_ERR "ide-tape: merge_stage_size "
2966 "should be 0 now\n");
2967 tape->merge_stage_size = 0;
2968 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002969 if ((tape->merge_stage = __idetape_kmalloc_stage(tape, 0, 0)) == NULL)
2970 return -ENOMEM;
Borislav Petkov54abf372008-02-06 02:57:52 +01002971 tape->chrdev_dir = IDETAPE_DIR_WRITE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002972 idetape_init_merge_stage(tape);
2973
2974 /*
2975 * Issue a write 0 command to ensure that DSC handshake
2976 * is switched from completion mode to buffer available
2977 * mode.
2978 * No point in issuing this if DSC overlap isn't supported,
2979 * some drives (Seagate STT3401A) will return an error.
2980 */
2981 if (drive->dsc_overlap) {
Daniel Walkerdcd96372006-06-25 05:47:37 -07002982 ssize_t retval = idetape_queue_rw_tail(drive, REQ_IDETAPE_WRITE, 0, tape->merge_stage->bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002983 if (retval < 0) {
2984 __idetape_kfree_stage(tape->merge_stage);
2985 tape->merge_stage = NULL;
Borislav Petkov54abf372008-02-06 02:57:52 +01002986 tape->chrdev_dir = IDETAPE_DIR_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002987 return retval;
2988 }
2989 }
2990 }
2991 if (count == 0)
2992 return (0);
2993 if (tape->restart_speed_control_req)
2994 idetape_restart_speed_control(drive);
2995 if (tape->merge_stage_size) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002996 if (tape->merge_stage_size >= tape->stage_size) {
2997 printk(KERN_ERR "ide-tape: bug: merge buffer too big\n");
2998 tape->merge_stage_size = 0;
2999 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003000 actually_written = min((unsigned int)(tape->stage_size - tape->merge_stage_size), (unsigned int)count);
Daniel Walkerdcd96372006-06-25 05:47:37 -07003001 if (idetape_copy_stage_from_user(tape, tape->merge_stage, buf, actually_written))
3002 ret = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003003 buf += actually_written;
3004 tape->merge_stage_size += actually_written;
3005 count -= actually_written;
3006
3007 if (tape->merge_stage_size == tape->stage_size) {
Daniel Walkerdcd96372006-06-25 05:47:37 -07003008 ssize_t retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003009 tape->merge_stage_size = 0;
Borislav Petkovb6422012008-02-02 19:56:49 +01003010 retval = idetape_add_chrdev_write_request(drive, ctl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003011 if (retval <= 0)
3012 return (retval);
3013 }
3014 }
3015 while (count >= tape->stage_size) {
Daniel Walkerdcd96372006-06-25 05:47:37 -07003016 ssize_t retval;
3017 if (idetape_copy_stage_from_user(tape, tape->merge_stage, buf, tape->stage_size))
3018 ret = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003019 buf += tape->stage_size;
3020 count -= tape->stage_size;
Borislav Petkovb6422012008-02-02 19:56:49 +01003021 retval = idetape_add_chrdev_write_request(drive, ctl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003022 actually_written += tape->stage_size;
3023 if (retval <= 0)
3024 return (retval);
3025 }
3026 if (count) {
3027 actually_written += count;
Daniel Walkerdcd96372006-06-25 05:47:37 -07003028 if (idetape_copy_stage_from_user(tape, tape->merge_stage, buf, count))
3029 ret = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003030 tape->merge_stage_size += count;
3031 }
Daniel Walkerdcd96372006-06-25 05:47:37 -07003032 return (ret) ? ret : actually_written;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003033}
3034
3035static int idetape_write_filemark (ide_drive_t *drive)
3036{
3037 idetape_pc_t pc;
3038
3039 /* Write a filemark */
3040 idetape_create_write_filemark_cmd(drive, &pc, 1);
3041 if (idetape_queue_pc_tail(drive, &pc)) {
3042 printk(KERN_ERR "ide-tape: Couldn't write a filemark\n");
3043 return -EIO;
3044 }
3045 return 0;
3046}
3047
3048/*
Borislav Petkovd99c9da2008-02-02 19:56:51 +01003049 * Called from idetape_chrdev_ioctl when the general mtio MTIOCTOP ioctl is
3050 * requested.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003051 *
Borislav Petkovd99c9da2008-02-02 19:56:51 +01003052 * Note: MTBSF and MTBSFM are not supported when the tape doesn't support
3053 * spacing over filemarks in the reverse direction. In this case, MTFSFM is also
3054 * usually not supported (it is supported in the rare case in which we crossed
3055 * the filemark during our read-ahead pipelined operation mode).
Linus Torvalds1da177e2005-04-16 15:20:36 -07003056 *
Borislav Petkovd99c9da2008-02-02 19:56:51 +01003057 * The following commands are currently not supported:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003058 *
Borislav Petkovd99c9da2008-02-02 19:56:51 +01003059 * MTFSS, MTBSS, MTWSM, MTSETDENSITY, MTSETDRVBUFFER, MT_ST_BOOLEANS,
3060 * MT_ST_WRITE_THRESHOLD.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003061 */
Borislav Petkovd99c9da2008-02-02 19:56:51 +01003062static int idetape_mtioctop(ide_drive_t *drive, short mt_op, int mt_count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003063{
3064 idetape_tape_t *tape = drive->driver_data;
3065 idetape_pc_t pc;
3066 int i,retval;
3067
Borislav Petkov8004a8c2008-02-06 02:57:51 +01003068 debug_log(DBG_ERR, "Handling MTIOCTOP ioctl: mt_op=%d, mt_count=%d\n",
3069 mt_op, mt_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003070 /*
3071 * Commands which need our pipelined read-ahead stages.
3072 */
3073 switch (mt_op) {
3074 case MTFSF:
3075 case MTFSFM:
3076 case MTBSF:
3077 case MTBSFM:
3078 if (!mt_count)
3079 return (0);
3080 return (idetape_space_over_filemarks(drive,mt_op,mt_count));
3081 default:
3082 break;
3083 }
3084 switch (mt_op) {
3085 case MTWEOF:
3086 if (tape->write_prot)
3087 return -EACCES;
3088 idetape_discard_read_pipeline(drive, 1);
3089 for (i = 0; i < mt_count; i++) {
3090 retval = idetape_write_filemark(drive);
3091 if (retval)
3092 return retval;
3093 }
3094 return (0);
3095 case MTREW:
3096 idetape_discard_read_pipeline(drive, 0);
3097 if (idetape_rewind_tape(drive))
3098 return -EIO;
3099 return 0;
3100 case MTLOAD:
3101 idetape_discard_read_pipeline(drive, 0);
3102 idetape_create_load_unload_cmd(drive, &pc, IDETAPE_LU_LOAD_MASK);
3103 return (idetape_queue_pc_tail(drive, &pc));
3104 case MTUNLOAD:
3105 case MTOFFL:
3106 /*
3107 * If door is locked, attempt to unlock before
3108 * attempting to eject.
3109 */
3110 if (tape->door_locked) {
3111 if (idetape_create_prevent_cmd(drive, &pc, 0))
3112 if (!idetape_queue_pc_tail(drive, &pc))
3113 tape->door_locked = DOOR_UNLOCKED;
3114 }
3115 idetape_discard_read_pipeline(drive, 0);
3116 idetape_create_load_unload_cmd(drive, &pc,!IDETAPE_LU_LOAD_MASK);
3117 retval = idetape_queue_pc_tail(drive, &pc);
3118 if (!retval)
3119 clear_bit(IDETAPE_MEDIUM_PRESENT, &tape->flags);
3120 return retval;
3121 case MTNOP:
3122 idetape_discard_read_pipeline(drive, 0);
3123 return (idetape_flush_tape_buffers(drive));
3124 case MTRETEN:
3125 idetape_discard_read_pipeline(drive, 0);
3126 idetape_create_load_unload_cmd(drive, &pc,IDETAPE_LU_RETENSION_MASK | IDETAPE_LU_LOAD_MASK);
3127 return (idetape_queue_pc_tail(drive, &pc));
3128 case MTEOM:
3129 idetape_create_space_cmd(&pc, 0, IDETAPE_SPACE_TO_EOD);
3130 return (idetape_queue_pc_tail(drive, &pc));
3131 case MTERASE:
3132 (void) idetape_rewind_tape(drive);
3133 idetape_create_erase_cmd(&pc);
3134 return (idetape_queue_pc_tail(drive, &pc));
3135 case MTSETBLK:
3136 if (mt_count) {
3137 if (mt_count < tape->tape_block_size || mt_count % tape->tape_block_size)
3138 return -EIO;
3139 tape->user_bs_factor = mt_count / tape->tape_block_size;
3140 clear_bit(IDETAPE_DETECT_BS, &tape->flags);
3141 } else
3142 set_bit(IDETAPE_DETECT_BS, &tape->flags);
3143 return 0;
3144 case MTSEEK:
3145 idetape_discard_read_pipeline(drive, 0);
3146 return idetape_position_tape(drive, mt_count * tape->user_bs_factor, tape->partition, 0);
3147 case MTSETPART:
3148 idetape_discard_read_pipeline(drive, 0);
3149 return (idetape_position_tape(drive, 0, mt_count, 0));
3150 case MTFSR:
3151 case MTBSR:
3152 case MTLOCK:
3153 if (!idetape_create_prevent_cmd(drive, &pc, 1))
3154 return 0;
3155 retval = idetape_queue_pc_tail(drive, &pc);
3156 if (retval) return retval;
3157 tape->door_locked = DOOR_EXPLICITLY_LOCKED;
3158 return 0;
3159 case MTUNLOCK:
3160 if (!idetape_create_prevent_cmd(drive, &pc, 0))
3161 return 0;
3162 retval = idetape_queue_pc_tail(drive, &pc);
3163 if (retval) return retval;
3164 tape->door_locked = DOOR_UNLOCKED;
3165 return 0;
3166 default:
3167 printk(KERN_ERR "ide-tape: MTIO operation %d not "
3168 "supported\n", mt_op);
3169 return (-EIO);
3170 }
3171}
3172
3173/*
Borislav Petkovd99c9da2008-02-02 19:56:51 +01003174 * Our character device ioctls. General mtio.h magnetic io commands are
3175 * supported here, and not in the corresponding block interface. Our own
3176 * ide-tape ioctls are supported on both interfaces.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003177 */
Borislav Petkovd99c9da2008-02-02 19:56:51 +01003178static int idetape_chrdev_ioctl(struct inode *inode, struct file *file,
3179 unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003180{
3181 struct ide_tape_obj *tape = ide_tape_f(file);
3182 ide_drive_t *drive = tape->drive;
3183 struct mtop mtop;
3184 struct mtget mtget;
3185 struct mtpos mtpos;
3186 int block_offset = 0, position = tape->first_frame_position;
3187 void __user *argp = (void __user *)arg;
3188
Borislav Petkov8004a8c2008-02-06 02:57:51 +01003189 debug_log(DBG_CHRDEV, "Enter %s, cmd=%u\n", __func__, cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003190
3191 tape->restart_speed_control_req = 1;
Borislav Petkov54abf372008-02-06 02:57:52 +01003192 if (tape->chrdev_dir == IDETAPE_DIR_WRITE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003193 idetape_empty_write_pipeline(drive);
3194 idetape_flush_tape_buffers(drive);
3195 }
3196 if (cmd == MTIOCGET || cmd == MTIOCPOS) {
3197 block_offset = idetape_pipeline_size(drive) / (tape->tape_block_size * tape->user_bs_factor);
3198 if ((position = idetape_read_position(drive)) < 0)
3199 return -EIO;
3200 }
3201 switch (cmd) {
3202 case MTIOCTOP:
3203 if (copy_from_user(&mtop, argp, sizeof (struct mtop)))
3204 return -EFAULT;
3205 return (idetape_mtioctop(drive,mtop.mt_op,mtop.mt_count));
3206 case MTIOCGET:
3207 memset(&mtget, 0, sizeof (struct mtget));
3208 mtget.mt_type = MT_ISSCSI2;
3209 mtget.mt_blkno = position / tape->user_bs_factor - block_offset;
3210 mtget.mt_dsreg = ((tape->tape_block_size * tape->user_bs_factor) << MT_ST_BLKSIZE_SHIFT) & MT_ST_BLKSIZE_MASK;
3211 if (tape->drv_write_prot) {
3212 mtget.mt_gstat |= GMT_WR_PROT(0xffffffff);
3213 }
3214 if (copy_to_user(argp, &mtget, sizeof(struct mtget)))
3215 return -EFAULT;
3216 return 0;
3217 case MTIOCPOS:
3218 mtpos.mt_blkno = position / tape->user_bs_factor - block_offset;
3219 if (copy_to_user(argp, &mtpos, sizeof(struct mtpos)))
3220 return -EFAULT;
3221 return 0;
3222 default:
Borislav Petkov54abf372008-02-06 02:57:52 +01003223 if (tape->chrdev_dir == IDETAPE_DIR_READ)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003224 idetape_discard_read_pipeline(drive, 1);
3225 return idetape_blkdev_ioctl(drive, cmd, arg);
3226 }
3227}
3228
Borislav Petkov3cffb9c2008-02-02 19:56:50 +01003229/*
3230 * Do a mode sense page 0 with block descriptor and if it succeeds set the tape
3231 * block size with the reported value.
3232 */
3233static void ide_tape_get_bsize_from_bdesc(ide_drive_t *drive)
3234{
3235 idetape_tape_t *tape = drive->driver_data;
3236 idetape_pc_t pc;
3237
3238 idetape_create_mode_sense_cmd(&pc, IDETAPE_BLOCK_DESCRIPTOR);
3239 if (idetape_queue_pc_tail(drive, &pc)) {
3240 printk(KERN_ERR "ide-tape: Can't get block descriptor\n");
3241 if (tape->tape_block_size == 0) {
3242 printk(KERN_WARNING "ide-tape: Cannot deal with zero "
3243 "block size, assuming 32k\n");
3244 tape->tape_block_size = 32768;
3245 }
3246 return;
3247 }
3248 tape->tape_block_size = (pc.buffer[4 + 5] << 16) +
3249 (pc.buffer[4 + 6] << 8) +
3250 pc.buffer[4 + 7];
3251 tape->drv_write_prot = (pc.buffer[2] & 0x80) >> 7;
3252}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003253
3254/*
3255 * Our character device open function.
3256 */
3257static int idetape_chrdev_open (struct inode *inode, struct file *filp)
3258{
3259 unsigned int minor = iminor(inode), i = minor & ~0xc0;
3260 ide_drive_t *drive;
3261 idetape_tape_t *tape;
3262 idetape_pc_t pc;
3263 int retval;
3264
Borislav Petkov8004a8c2008-02-06 02:57:51 +01003265 if (i >= MAX_HWIFS * MAX_DRIVES)
3266 return -ENXIO;
3267
3268 tape = ide_tape_chrdev_get(i);
3269 if (!tape)
3270 return -ENXIO;
3271
3272 debug_log(DBG_CHRDEV, "Enter %s\n", __func__);
3273
Linus Torvalds1da177e2005-04-16 15:20:36 -07003274 /*
3275 * We really want to do nonseekable_open(inode, filp); here, but some
3276 * versions of tar incorrectly call lseek on tapes and bail out if that
3277 * fails. So we disallow pread() and pwrite(), but permit lseeks.
3278 */
3279 filp->f_mode &= ~(FMODE_PREAD | FMODE_PWRITE);
3280
Linus Torvalds1da177e2005-04-16 15:20:36 -07003281 drive = tape->drive;
3282
3283 filp->private_data = tape;
3284
3285 if (test_and_set_bit(IDETAPE_BUSY, &tape->flags)) {
3286 retval = -EBUSY;
3287 goto out_put_tape;
3288 }
3289
3290 retval = idetape_wait_ready(drive, 60 * HZ);
3291 if (retval) {
3292 clear_bit(IDETAPE_BUSY, &tape->flags);
3293 printk(KERN_ERR "ide-tape: %s: drive not ready\n", tape->name);
3294 goto out_put_tape;
3295 }
3296
3297 idetape_read_position(drive);
3298 if (!test_bit(IDETAPE_ADDRESS_VALID, &tape->flags))
3299 (void)idetape_rewind_tape(drive);
3300
Borislav Petkov54abf372008-02-06 02:57:52 +01003301 if (tape->chrdev_dir != IDETAPE_DIR_READ)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003302 clear_bit(IDETAPE_PIPELINE_ERROR, &tape->flags);
3303
3304 /* Read block size and write protect status from drive. */
Borislav Petkov3cffb9c2008-02-02 19:56:50 +01003305 ide_tape_get_bsize_from_bdesc(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003306
3307 /* Set write protect flag if device is opened as read-only. */
3308 if ((filp->f_flags & O_ACCMODE) == O_RDONLY)
3309 tape->write_prot = 1;
3310 else
3311 tape->write_prot = tape->drv_write_prot;
3312
3313 /* Make sure drive isn't write protected if user wants to write. */
3314 if (tape->write_prot) {
3315 if ((filp->f_flags & O_ACCMODE) == O_WRONLY ||
3316 (filp->f_flags & O_ACCMODE) == O_RDWR) {
3317 clear_bit(IDETAPE_BUSY, &tape->flags);
3318 retval = -EROFS;
3319 goto out_put_tape;
3320 }
3321 }
3322
3323 /*
3324 * Lock the tape drive door so user can't eject.
3325 */
Borislav Petkov54abf372008-02-06 02:57:52 +01003326 if (tape->chrdev_dir == IDETAPE_DIR_NONE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003327 if (idetape_create_prevent_cmd(drive, &pc, 1)) {
3328 if (!idetape_queue_pc_tail(drive, &pc)) {
3329 if (tape->door_locked != DOOR_EXPLICITLY_LOCKED)
3330 tape->door_locked = DOOR_LOCKED;
3331 }
3332 }
3333 }
3334 idetape_restart_speed_control(drive);
3335 tape->restart_speed_control_req = 0;
3336 return 0;
3337
3338out_put_tape:
3339 ide_tape_put(tape);
3340 return retval;
3341}
3342
3343static void idetape_write_release (ide_drive_t *drive, unsigned int minor)
3344{
3345 idetape_tape_t *tape = drive->driver_data;
3346
3347 idetape_empty_write_pipeline(drive);
3348 tape->merge_stage = __idetape_kmalloc_stage(tape, 1, 0);
3349 if (tape->merge_stage != NULL) {
3350 idetape_pad_zeros(drive, tape->tape_block_size * (tape->user_bs_factor - 1));
3351 __idetape_kfree_stage(tape->merge_stage);
3352 tape->merge_stage = NULL;
3353 }
3354 idetape_write_filemark(drive);
3355 idetape_flush_tape_buffers(drive);
3356 idetape_flush_tape_buffers(drive);
3357}
3358
3359/*
3360 * Our character device release function.
3361 */
3362static int idetape_chrdev_release (struct inode *inode, struct file *filp)
3363{
3364 struct ide_tape_obj *tape = ide_tape_f(filp);
3365 ide_drive_t *drive = tape->drive;
3366 idetape_pc_t pc;
3367 unsigned int minor = iminor(inode);
3368
3369 lock_kernel();
3370 tape = drive->driver_data;
Borislav Petkov8004a8c2008-02-06 02:57:51 +01003371
3372 debug_log(DBG_CHRDEV, "Enter %s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003373
Borislav Petkov54abf372008-02-06 02:57:52 +01003374 if (tape->chrdev_dir == IDETAPE_DIR_WRITE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003375 idetape_write_release(drive, minor);
Borislav Petkov54abf372008-02-06 02:57:52 +01003376 if (tape->chrdev_dir == IDETAPE_DIR_READ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003377 if (minor < 128)
3378 idetape_discard_read_pipeline(drive, 1);
3379 else
3380 idetape_wait_for_pipeline(drive);
3381 }
3382 if (tape->cache_stage != NULL) {
3383 __idetape_kfree_stage(tape->cache_stage);
3384 tape->cache_stage = NULL;
3385 }
3386 if (minor < 128 && test_bit(IDETAPE_MEDIUM_PRESENT, &tape->flags))
3387 (void) idetape_rewind_tape(drive);
Borislav Petkov54abf372008-02-06 02:57:52 +01003388 if (tape->chrdev_dir == IDETAPE_DIR_NONE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003389 if (tape->door_locked == DOOR_LOCKED) {
3390 if (idetape_create_prevent_cmd(drive, &pc, 0)) {
3391 if (!idetape_queue_pc_tail(drive, &pc))
3392 tape->door_locked = DOOR_UNLOCKED;
3393 }
3394 }
3395 }
3396 clear_bit(IDETAPE_BUSY, &tape->flags);
3397 ide_tape_put(tape);
3398 unlock_kernel();
3399 return 0;
3400}
3401
3402/*
3403 * idetape_identify_device is called to check the contents of the
3404 * ATAPI IDENTIFY command results. We return:
3405 *
3406 * 1 If the tape can be supported by us, based on the information
3407 * we have so far.
3408 *
3409 * 0 If this tape driver is not currently supported by us.
3410 */
3411static int idetape_identify_device (ide_drive_t *drive)
3412{
3413 struct idetape_id_gcw gcw;
3414 struct hd_driveid *id = drive->id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003415
3416 if (drive->id_read == 0)
3417 return 1;
3418
3419 *((unsigned short *) &gcw) = id->config;
3420
Linus Torvalds1da177e2005-04-16 15:20:36 -07003421 /* Check that we can support this device */
3422
Bartlomiej Zolnierkiewicz16422de32008-02-02 19:56:48 +01003423 if (gcw.protocol != 2)
3424 printk(KERN_ERR "ide-tape: Protocol (0x%02x) is not ATAPI\n",
3425 gcw.protocol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003426 else if (gcw.device_type != 1)
Bartlomiej Zolnierkiewicz16422de32008-02-02 19:56:48 +01003427 printk(KERN_ERR "ide-tape: Device type (0x%02x) is not set "
3428 "to tape\n", gcw.device_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003429 else if (!gcw.removable)
3430 printk(KERN_ERR "ide-tape: The removable flag is not set\n");
3431 else if (gcw.packet_size != 0) {
Bartlomiej Zolnierkiewicz16422de32008-02-02 19:56:48 +01003432 printk(KERN_ERR "ide-tape: Packet size (0x%02x) is not 12 "
3433 "bytes long\n", gcw.packet_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003434 } else
3435 return 1;
3436 return 0;
3437}
3438
Borislav Petkov6d29c8f2008-02-02 19:56:49 +01003439static void idetape_get_inquiry_results(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003440{
3441 char *r;
3442 idetape_tape_t *tape = drive->driver_data;
3443 idetape_pc_t pc;
Borislav Petkov6d29c8f2008-02-02 19:56:49 +01003444
Linus Torvalds1da177e2005-04-16 15:20:36 -07003445 idetape_create_inquiry_cmd(&pc);
3446 if (idetape_queue_pc_tail(drive, &pc)) {
Borislav Petkov6d29c8f2008-02-02 19:56:49 +01003447 printk(KERN_ERR "ide-tape: %s: can't get INQUIRY results\n",
3448 tape->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003449 return;
3450 }
Borislav Petkov6d29c8f2008-02-02 19:56:49 +01003451 memcpy(tape->vendor_id, &pc.buffer[8], 8);
3452 memcpy(tape->product_id, &pc.buffer[16], 16);
3453 memcpy(tape->firmware_revision, &pc.buffer[32], 4);
3454
Linus Torvalds1da177e2005-04-16 15:20:36 -07003455 ide_fixstring(tape->vendor_id, 10, 0);
3456 ide_fixstring(tape->product_id, 18, 0);
3457 ide_fixstring(tape->firmware_revision, 6, 0);
3458 r = tape->firmware_revision;
3459 if (*(r + 1) == '.')
Borislav Petkov6d29c8f2008-02-02 19:56:49 +01003460 tape->firmware_revision_num = (*r - '0') * 100 +
3461 (*(r + 2) - '0') * 10 + *(r + 3) - '0';
3462 printk(KERN_INFO "ide-tape: %s <-> %s: %s %s rev %s\n",
3463 drive->name, tape->name, tape->vendor_id,
3464 tape->product_id, tape->firmware_revision);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003465}
3466
3467/*
Borislav Petkovb6422012008-02-02 19:56:49 +01003468 * Ask the tape about its various parameters. In particular, we will adjust our
3469 * data transfer buffer size to the recommended value as returned by the tape.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003470 */
3471static void idetape_get_mode_sense_results (ide_drive_t *drive)
3472{
3473 idetape_tape_t *tape = drive->driver_data;
3474 idetape_pc_t pc;
Borislav Petkovb6422012008-02-02 19:56:49 +01003475 u8 *caps;
3476 u8 speed, max_speed;
Borislav Petkov47314fa2008-02-02 19:56:48 +01003477
Linus Torvalds1da177e2005-04-16 15:20:36 -07003478 idetape_create_mode_sense_cmd(&pc, IDETAPE_CAPABILITIES_PAGE);
3479 if (idetape_queue_pc_tail(drive, &pc)) {
Borislav Petkovb6422012008-02-02 19:56:49 +01003480 printk(KERN_ERR "ide-tape: Can't get tape parameters - assuming"
3481 " some default values\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003482 tape->tape_block_size = 512;
Borislav Petkovb6422012008-02-02 19:56:49 +01003483 put_unaligned(52, (u16 *)&tape->caps[12]);
3484 put_unaligned(540, (u16 *)&tape->caps[14]);
3485 put_unaligned(6*52, (u16 *)&tape->caps[16]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003486 return;
3487 }
Borislav Petkovb6422012008-02-02 19:56:49 +01003488 caps = pc.buffer + 4 + pc.buffer[3];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003489
Borislav Petkovb6422012008-02-02 19:56:49 +01003490 /* convert to host order and save for later use */
3491 speed = be16_to_cpu(*(u16 *)&caps[14]);
3492 max_speed = be16_to_cpu(*(u16 *)&caps[8]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003493
Borislav Petkovb6422012008-02-02 19:56:49 +01003494 put_unaligned(max_speed, (u16 *)&caps[8]);
3495 put_unaligned(be16_to_cpu(*(u16 *)&caps[12]), (u16 *)&caps[12]);
3496 put_unaligned(speed, (u16 *)&caps[14]);
3497 put_unaligned(be16_to_cpu(*(u16 *)&caps[16]), (u16 *)&caps[16]);
3498
3499 if (!speed) {
3500 printk(KERN_INFO "ide-tape: %s: invalid tape speed "
3501 "(assuming 650KB/sec)\n", drive->name);
3502 put_unaligned(650, (u16 *)&caps[14]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003503 }
Borislav Petkovb6422012008-02-02 19:56:49 +01003504 if (!max_speed) {
3505 printk(KERN_INFO "ide-tape: %s: invalid max_speed "
3506 "(assuming 650KB/sec)\n", drive->name);
3507 put_unaligned(650, (u16 *)&caps[8]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003508 }
3509
Borislav Petkovb6422012008-02-02 19:56:49 +01003510 memcpy(&tape->caps, caps, 20);
3511 if (caps[7] & 0x02)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003512 tape->tape_block_size = 512;
Borislav Petkovb6422012008-02-02 19:56:49 +01003513 else if (caps[7] & 0x04)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003514 tape->tape_block_size = 1024;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003515}
3516
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +02003517#ifdef CONFIG_IDE_PROC_FS
Linus Torvalds1da177e2005-04-16 15:20:36 -07003518static void idetape_add_settings (ide_drive_t *drive)
3519{
3520 idetape_tape_t *tape = drive->driver_data;
3521
3522/*
Bartlomiej Zolnierkiewicz14979432007-05-10 00:01:10 +02003523 * drive setting name read/write data type min max mul_factor div_factor data pointer set function
Linus Torvalds1da177e2005-04-16 15:20:36 -07003524 */
Borislav Petkovb6422012008-02-02 19:56:49 +01003525 ide_add_setting(drive, "buffer", SETTING_READ, TYPE_SHORT, 0, 0xffff,
3526 1, 2, (u16 *)&tape->caps[16], NULL);
Bartlomiej Zolnierkiewicz14979432007-05-10 00:01:10 +02003527 ide_add_setting(drive, "pipeline_min", SETTING_RW, TYPE_INT, 1, 0xffff, tape->stage_size / 1024, 1, &tape->min_pipeline, NULL);
3528 ide_add_setting(drive, "pipeline", SETTING_RW, TYPE_INT, 1, 0xffff, tape->stage_size / 1024, 1, &tape->max_stages, NULL);
3529 ide_add_setting(drive, "pipeline_max", SETTING_RW, TYPE_INT, 1, 0xffff, tape->stage_size / 1024, 1, &tape->max_pipeline, NULL);
3530 ide_add_setting(drive, "pipeline_used", SETTING_READ, TYPE_INT, 0, 0xffff, tape->stage_size / 1024, 1, &tape->nr_stages, NULL);
3531 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 +01003532 ide_add_setting(drive, "speed", SETTING_READ, TYPE_SHORT, 0, 0xffff,
3533 1, 1, (u16 *)&tape->caps[14], NULL);
Bartlomiej Zolnierkiewicz14979432007-05-10 00:01:10 +02003534 ide_add_setting(drive, "stage", SETTING_READ, TYPE_INT, 0, 0xffff, 1, 1024, &tape->stage_size, NULL);
3535 ide_add_setting(drive, "tdsc", SETTING_RW, TYPE_INT, IDETAPE_DSC_RW_MIN, IDETAPE_DSC_RW_MAX, 1000, HZ, &tape->best_dsc_rw_frequency, NULL);
3536 ide_add_setting(drive, "dsc_overlap", SETTING_RW, TYPE_BYTE, 0, 1, 1, 1, &drive->dsc_overlap, NULL);
3537 ide_add_setting(drive, "pipeline_head_speed_c",SETTING_READ, TYPE_INT, 0, 0xffff, 1, 1, &tape->controlled_pipeline_head_speed, NULL);
3538 ide_add_setting(drive, "pipeline_head_speed_u",SETTING_READ, TYPE_INT, 0, 0xffff, 1, 1, &tape->uncontrolled_pipeline_head_speed,NULL);
3539 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 +01003540 ide_add_setting(drive, "debug_mask", SETTING_RW, TYPE_INT, 0, 0xffff, 1,
3541 1, &tape->debug_mask, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003542}
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +02003543#else
3544static inline void idetape_add_settings(ide_drive_t *drive) { ; }
3545#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003546
3547/*
3548 * ide_setup is called to:
3549 *
3550 * 1. Initialize our various state variables.
3551 * 2. Ask the tape for its capabilities.
3552 * 3. Allocate a buffer which will be used for data
3553 * transfer. The buffer size is chosen based on
3554 * the recommendation which we received in step (2).
3555 *
3556 * Note that at this point ide.c already assigned us an irq, so that
3557 * we can queue requests here and wait for their completion.
3558 */
3559static void idetape_setup (ide_drive_t *drive, idetape_tape_t *tape, int minor)
3560{
3561 unsigned long t1, tmid, tn, t;
3562 int speed;
3563 struct idetape_id_gcw gcw;
3564 int stage_size;
3565 struct sysinfo si;
Borislav Petkovb6422012008-02-02 19:56:49 +01003566 u16 *ctl = (u16 *)&tape->caps[12];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003567
3568 spin_lock_init(&tape->spinlock);
3569 drive->dsc_overlap = 1;
Bartlomiej Zolnierkiewicz4166c192008-02-01 23:09:30 +01003570 if (drive->hwif->host_flags & IDE_HFLAG_NO_DSC) {
3571 printk(KERN_INFO "ide-tape: %s: disabling DSC overlap\n",
3572 tape->name);
3573 drive->dsc_overlap = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003574 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003575 /* Seagate Travan drives do not support DSC overlap. */
3576 if (strstr(drive->id->model, "Seagate STT3401"))
3577 drive->dsc_overlap = 0;
3578 tape->minor = minor;
3579 tape->name[0] = 'h';
3580 tape->name[1] = 't';
3581 tape->name[2] = '0' + minor;
Borislav Petkov54abf372008-02-06 02:57:52 +01003582 tape->chrdev_dir = IDETAPE_DIR_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003583 tape->pc = tape->pc_stack;
3584 tape->max_insert_speed = 10000;
3585 tape->speed_control = 1;
3586 *((unsigned short *) &gcw) = drive->id->config;
3587 if (gcw.drq_type == 1)
3588 set_bit(IDETAPE_DRQ_INTERRUPT, &tape->flags);
3589
3590 tape->min_pipeline = tape->max_pipeline = tape->max_stages = 10;
3591
3592 idetape_get_inquiry_results(drive);
3593 idetape_get_mode_sense_results(drive);
Borislav Petkov3cffb9c2008-02-02 19:56:50 +01003594 ide_tape_get_bsize_from_bdesc(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003595 tape->user_bs_factor = 1;
Borislav Petkovb6422012008-02-02 19:56:49 +01003596 tape->stage_size = *ctl * tape->tape_block_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003597 while (tape->stage_size > 0xffff) {
3598 printk(KERN_NOTICE "ide-tape: decreasing stage size\n");
Borislav Petkovb6422012008-02-02 19:56:49 +01003599 *ctl /= 2;
3600 tape->stage_size = *ctl * tape->tape_block_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003601 }
3602 stage_size = tape->stage_size;
3603 tape->pages_per_stage = stage_size / PAGE_SIZE;
3604 if (stage_size % PAGE_SIZE) {
3605 tape->pages_per_stage++;
3606 tape->excess_bh_size = PAGE_SIZE - stage_size % PAGE_SIZE;
3607 }
3608
Borislav Petkovb6422012008-02-02 19:56:49 +01003609 /* Select the "best" DSC read/write polling freq and pipeline size. */
3610 speed = max(*(u16 *)&tape->caps[14], *(u16 *)&tape->caps[8]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003611
3612 tape->max_stages = speed * 1000 * 10 / tape->stage_size;
3613
3614 /*
3615 * Limit memory use for pipeline to 10% of physical memory
3616 */
3617 si_meminfo(&si);
3618 if (tape->max_stages * tape->stage_size > si.totalram * si.mem_unit / 10)
3619 tape->max_stages = si.totalram * si.mem_unit / (10 * tape->stage_size);
3620 tape->max_stages = min(tape->max_stages, IDETAPE_MAX_PIPELINE_STAGES);
3621 tape->min_pipeline = min(tape->max_stages, IDETAPE_MIN_PIPELINE_STAGES);
3622 tape->max_pipeline = min(tape->max_stages * 2, IDETAPE_MAX_PIPELINE_STAGES);
3623 if (tape->max_stages == 0)
3624 tape->max_stages = tape->min_pipeline = tape->max_pipeline = 1;
3625
3626 t1 = (tape->stage_size * HZ) / (speed * 1000);
Borislav Petkovb6422012008-02-02 19:56:49 +01003627 tmid = (*(u16 *)&tape->caps[16] * 32 * HZ) / (speed * 125);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003628 tn = (IDETAPE_FIFO_THRESHOLD * tape->stage_size * HZ) / (speed * 1000);
3629
3630 if (tape->max_stages)
3631 t = tn;
3632 else
3633 t = t1;
3634
3635 /*
3636 * Ensure that the number we got makes sense; limit
3637 * it within IDETAPE_DSC_RW_MIN and IDETAPE_DSC_RW_MAX.
3638 */
3639 tape->best_dsc_rw_frequency = max_t(unsigned long, min_t(unsigned long, t, IDETAPE_DSC_RW_MAX), IDETAPE_DSC_RW_MIN);
3640 printk(KERN_INFO "ide-tape: %s <-> %s: %dKBps, %d*%dkB buffer, "
3641 "%dkB pipeline, %lums tDSC%s\n",
Borislav Petkovb6422012008-02-02 19:56:49 +01003642 drive->name, tape->name, *(u16 *)&tape->caps[14],
3643 (*(u16 *)&tape->caps[16] * 512) / tape->stage_size,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003644 tape->stage_size / 1024,
3645 tape->max_stages * tape->stage_size / 1024,
3646 tape->best_dsc_rw_frequency * 1000 / HZ,
3647 drive->using_dma ? ", DMA":"");
3648
3649 idetape_add_settings(drive);
3650}
3651
Russell King4031bbe2006-01-06 11:41:00 +00003652static void ide_tape_remove(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003653{
3654 idetape_tape_t *tape = drive->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003655
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +02003656 ide_proc_unregister_driver(drive, tape->driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003657
3658 ide_unregister_region(tape->disk);
3659
3660 ide_tape_put(tape);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003661}
3662
3663static void ide_tape_release(struct kref *kref)
3664{
3665 struct ide_tape_obj *tape = to_ide_tape(kref);
3666 ide_drive_t *drive = tape->drive;
3667 struct gendisk *g = tape->disk;
3668
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02003669 BUG_ON(tape->first_stage != NULL || tape->merge_stage_size);
3670
Linus Torvalds1da177e2005-04-16 15:20:36 -07003671 drive->dsc_overlap = 0;
3672 drive->driver_data = NULL;
Tony Jonesdbc12722007-09-25 02:03:03 +02003673 device_destroy(idetape_sysfs_class, MKDEV(IDETAPE_MAJOR, tape->minor));
3674 device_destroy(idetape_sysfs_class, MKDEV(IDETAPE_MAJOR, tape->minor + 128));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003675 idetape_devs[tape->minor] = NULL;
3676 g->private_data = NULL;
3677 put_disk(g);
3678 kfree(tape);
3679}
3680
Bartlomiej Zolnierkiewiczecfd80e2007-05-10 00:01:09 +02003681#ifdef CONFIG_IDE_PROC_FS
Linus Torvalds1da177e2005-04-16 15:20:36 -07003682static int proc_idetape_read_name
3683 (char *page, char **start, off_t off, int count, int *eof, void *data)
3684{
3685 ide_drive_t *drive = (ide_drive_t *) data;
3686 idetape_tape_t *tape = drive->driver_data;
3687 char *out = page;
3688 int len;
3689
3690 len = sprintf(out, "%s\n", tape->name);
3691 PROC_IDE_READ_RETURN(page, start, off, count, eof, len);
3692}
3693
3694static ide_proc_entry_t idetape_proc[] = {
3695 { "capacity", S_IFREG|S_IRUGO, proc_ide_read_capacity, NULL },
3696 { "name", S_IFREG|S_IRUGO, proc_idetape_read_name, NULL },
3697 { NULL, 0, NULL, NULL }
3698};
Linus Torvalds1da177e2005-04-16 15:20:36 -07003699#endif
3700
Russell King4031bbe2006-01-06 11:41:00 +00003701static int ide_tape_probe(ide_drive_t *);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003702
Linus Torvalds1da177e2005-04-16 15:20:36 -07003703static ide_driver_t idetape_driver = {
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02003704 .gen_driver = {
Laurent Riffard4ef3b8f2005-11-18 22:15:40 +01003705 .owner = THIS_MODULE,
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02003706 .name = "ide-tape",
3707 .bus = &ide_bus_type,
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02003708 },
Russell King4031bbe2006-01-06 11:41:00 +00003709 .probe = ide_tape_probe,
3710 .remove = ide_tape_remove,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003711 .version = IDETAPE_VERSION,
3712 .media = ide_tape,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003713 .supports_dsc_overlap = 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003714 .do_request = idetape_do_request,
3715 .end_request = idetape_end_request,
3716 .error = __ide_error,
3717 .abort = __ide_abort,
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +02003718#ifdef CONFIG_IDE_PROC_FS
Linus Torvalds1da177e2005-04-16 15:20:36 -07003719 .proc = idetape_proc,
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +02003720#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003721};
3722
3723/*
3724 * Our character device supporting functions, passed to register_chrdev.
3725 */
Arjan van de Ven2b8693c2007-02-12 00:55:32 -08003726static const struct file_operations idetape_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003727 .owner = THIS_MODULE,
3728 .read = idetape_chrdev_read,
3729 .write = idetape_chrdev_write,
3730 .ioctl = idetape_chrdev_ioctl,
3731 .open = idetape_chrdev_open,
3732 .release = idetape_chrdev_release,
3733};
3734
3735static int idetape_open(struct inode *inode, struct file *filp)
3736{
3737 struct gendisk *disk = inode->i_bdev->bd_disk;
3738 struct ide_tape_obj *tape;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003739
3740 if (!(tape = ide_tape_get(disk)))
3741 return -ENXIO;
3742
Linus Torvalds1da177e2005-04-16 15:20:36 -07003743 return 0;
3744}
3745
3746static int idetape_release(struct inode *inode, struct file *filp)
3747{
3748 struct gendisk *disk = inode->i_bdev->bd_disk;
3749 struct ide_tape_obj *tape = ide_tape_g(disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003750
3751 ide_tape_put(tape);
3752
3753 return 0;
3754}
3755
3756static int idetape_ioctl(struct inode *inode, struct file *file,
3757 unsigned int cmd, unsigned long arg)
3758{
3759 struct block_device *bdev = inode->i_bdev;
3760 struct ide_tape_obj *tape = ide_tape_g(bdev->bd_disk);
3761 ide_drive_t *drive = tape->drive;
3762 int err = generic_ide_ioctl(drive, file, bdev, cmd, arg);
3763 if (err == -EINVAL)
3764 err = idetape_blkdev_ioctl(drive, cmd, arg);
3765 return err;
3766}
3767
3768static struct block_device_operations idetape_block_ops = {
3769 .owner = THIS_MODULE,
3770 .open = idetape_open,
3771 .release = idetape_release,
3772 .ioctl = idetape_ioctl,
3773};
3774
Russell King4031bbe2006-01-06 11:41:00 +00003775static int ide_tape_probe(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003776{
3777 idetape_tape_t *tape;
3778 struct gendisk *g;
3779 int minor;
3780
3781 if (!strstr("ide-tape", drive->driver_req))
3782 goto failed;
3783 if (!drive->present)
3784 goto failed;
3785 if (drive->media != ide_tape)
3786 goto failed;
3787 if (!idetape_identify_device (drive)) {
3788 printk(KERN_ERR "ide-tape: %s: not supported by this version of ide-tape\n", drive->name);
3789 goto failed;
3790 }
3791 if (drive->scsi) {
3792 printk("ide-tape: passing drive %s to ide-scsi emulation.\n", drive->name);
3793 goto failed;
3794 }
3795 if (strstr(drive->id->model, "OnStream DI-")) {
3796 printk(KERN_WARNING "ide-tape: Use drive %s with ide-scsi emulation and osst.\n", drive->name);
3797 printk(KERN_WARNING "ide-tape: OnStream support will be removed soon from ide-tape!\n");
3798 }
Robert P. J. Day5cbded52006-12-13 00:35:56 -08003799 tape = kzalloc(sizeof (idetape_tape_t), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003800 if (tape == NULL) {
3801 printk(KERN_ERR "ide-tape: %s: Can't allocate a tape structure\n", drive->name);
3802 goto failed;
3803 }
3804
3805 g = alloc_disk(1 << PARTN_BITS);
3806 if (!g)
3807 goto out_free_tape;
3808
3809 ide_init_disk(g, drive);
3810
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +02003811 ide_proc_register_driver(drive, &idetape_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003812
Linus Torvalds1da177e2005-04-16 15:20:36 -07003813 kref_init(&tape->kref);
3814
3815 tape->drive = drive;
3816 tape->driver = &idetape_driver;
3817 tape->disk = g;
3818
3819 g->private_data = &tape->driver;
3820
3821 drive->driver_data = tape;
3822
Arjan van de Vencf8b8972006-03-23 03:00:45 -08003823 mutex_lock(&idetape_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003824 for (minor = 0; idetape_devs[minor]; minor++)
3825 ;
3826 idetape_devs[minor] = tape;
Arjan van de Vencf8b8972006-03-23 03:00:45 -08003827 mutex_unlock(&idetape_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003828
3829 idetape_setup(drive, tape, minor);
3830
Tony Jonesdbc12722007-09-25 02:03:03 +02003831 device_create(idetape_sysfs_class, &drive->gendev,
3832 MKDEV(IDETAPE_MAJOR, minor), "%s", tape->name);
3833 device_create(idetape_sysfs_class, &drive->gendev,
3834 MKDEV(IDETAPE_MAJOR, minor + 128), "n%s", tape->name);
Will Dysond5dee802005-09-16 02:55:07 -07003835
Linus Torvalds1da177e2005-04-16 15:20:36 -07003836 g->fops = &idetape_block_ops;
3837 ide_register_region(g);
3838
3839 return 0;
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02003840
Linus Torvalds1da177e2005-04-16 15:20:36 -07003841out_free_tape:
3842 kfree(tape);
3843failed:
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02003844 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003845}
3846
3847MODULE_DESCRIPTION("ATAPI Streaming TAPE Driver");
3848MODULE_LICENSE("GPL");
3849
3850static void __exit idetape_exit (void)
3851{
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02003852 driver_unregister(&idetape_driver.gen_driver);
Will Dysond5dee802005-09-16 02:55:07 -07003853 class_destroy(idetape_sysfs_class);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003854 unregister_chrdev(IDETAPE_MAJOR, "ht");
3855}
3856
Bartlomiej Zolnierkiewicz17514e82005-11-19 22:24:35 +01003857static int __init idetape_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003858{
Will Dysond5dee802005-09-16 02:55:07 -07003859 int error = 1;
3860 idetape_sysfs_class = class_create(THIS_MODULE, "ide_tape");
3861 if (IS_ERR(idetape_sysfs_class)) {
3862 idetape_sysfs_class = NULL;
3863 printk(KERN_ERR "Unable to create sysfs class for ide tapes\n");
3864 error = -EBUSY;
3865 goto out;
3866 }
3867
Linus Torvalds1da177e2005-04-16 15:20:36 -07003868 if (register_chrdev(IDETAPE_MAJOR, "ht", &idetape_fops)) {
3869 printk(KERN_ERR "ide-tape: Failed to register character device interface\n");
Will Dysond5dee802005-09-16 02:55:07 -07003870 error = -EBUSY;
3871 goto out_free_class;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003872 }
Will Dysond5dee802005-09-16 02:55:07 -07003873
3874 error = driver_register(&idetape_driver.gen_driver);
3875 if (error)
3876 goto out_free_driver;
3877
3878 return 0;
3879
3880out_free_driver:
3881 driver_unregister(&idetape_driver.gen_driver);
3882out_free_class:
3883 class_destroy(idetape_sysfs_class);
3884out:
3885 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003886}
3887
Kay Sievers263756e2005-12-12 18:03:44 +01003888MODULE_ALIAS("ide:*m-tape*");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003889module_init(idetape_init);
3890module_exit(idetape_exit);
3891MODULE_ALIAS_CHARDEV_MAJOR(IDETAPE_MAJOR);