]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - drivers/ide/ide-tape.c
ide-tape: remove struct idetape_request_sense_result_t
[mirror_ubuntu-bionic-kernel.git] / drivers / ide / ide-tape.c
CommitLineData
1da177e4 1/*
5ce78af4
BP
2 * IDE ATAPI streaming tape driver.
3 *
59bca8cc
BZ
4 * Copyright (C) 1995-1999 Gadi Oxman <gadio@netvision.net.il>
5 * Copyright (C) 2003-2005 Bartlomiej Zolnierkiewicz
1da177e4 6 *
1da177e4
LT
7 * 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).
1da177e4 13 *
5ce78af4
BP
14 * For a historical changelog see
15 * Documentation/ide/ChangeLog.ide-tape.1995-2002
1da177e4
LT
16 */
17
18#define IDETAPE_VERSION "1.19"
19
1da177e4
LT
20#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>
9bae1ff3 28#include <linux/jiffies.h>
1da177e4 29#include <linux/major.h>
1da177e4
LT
30#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>
cf8b8975 38#include <linux/mutex.h>
1da177e4
LT
39
40#include <asm/byteorder.h>
41#include <asm/irq.h>
42#include <asm/uaccess.h>
43#include <asm/io.h>
44#include <asm/unaligned.h>
45
46/*
47 * partition
48 */
49typedef struct os_partition_s {
50 __u8 partition_num;
51 __u8 par_desc_ver;
52 __u16 wrt_pass_cntr;
53 __u32 first_frame_addr;
54 __u32 last_frame_addr;
55 __u32 eod_frame_addr;
56} os_partition_t;
57
58/*
59 * DAT entry
60 */
61typedef struct os_dat_entry_s {
62 __u32 blk_sz;
63 __u16 blk_cnt;
64 __u8 flags;
65 __u8 reserved;
66} os_dat_entry_t;
67
68/*
69 * DAT
70 */
71#define OS_DAT_FLAGS_DATA (0xc)
72#define OS_DAT_FLAGS_MARK (0x1)
73
74typedef struct os_dat_s {
75 __u8 dat_sz;
76 __u8 reserved1;
77 __u8 entry_cnt;
78 __u8 reserved3;
79 os_dat_entry_t dat_list[16];
80} os_dat_t;
81
82#include <linux/mtio.h>
83
84/**************************** Tunable parameters *****************************/
85
86
87/*
88 * Pipelined mode parameters.
89 *
90 * We try to use the minimum number of stages which is enough to
91 * keep the tape constantly streaming. To accomplish that, we implement
92 * a feedback loop around the maximum number of stages:
93 *
94 * We start from MIN maximum stages (we will not even use MIN stages
95 * if we don't need them), increment it by RATE*(MAX-MIN)
96 * whenever we sense that the pipeline is empty, until we reach
97 * the optimum value or until we reach MAX.
98 *
99 * Setting the following parameter to 0 is illegal: the pipelined mode
100 * cannot be disabled (calculate_speeds() divides by tape->max_stages.)
101 */
102#define IDETAPE_MIN_PIPELINE_STAGES 1
103#define IDETAPE_MAX_PIPELINE_STAGES 400
104#define IDETAPE_INCREASE_STAGES_RATE 20
105
106/*
107 * The following are used to debug the driver:
108 *
109 * Setting IDETAPE_DEBUG_INFO to 1 will report device capabilities.
110 * Setting IDETAPE_DEBUG_LOG to 1 will log driver flow control.
111 * Setting IDETAPE_DEBUG_BUGS to 1 will enable self-sanity checks in
112 * some places.
113 *
114 * Setting them to 0 will restore normal operation mode:
115 *
116 * 1. Disable logging normal successful operations.
117 * 2. Disable self-sanity checks.
118 * 3. Errors will still be logged, of course.
119 *
120 * All the #if DEBUG code will be removed some day, when the driver
121 * is verified to be stable enough. This will make it much more
122 * esthetic.
123 */
124#define IDETAPE_DEBUG_INFO 0
125#define IDETAPE_DEBUG_LOG 0
126#define IDETAPE_DEBUG_BUGS 1
127
128/*
129 * After each failed packet command we issue a request sense command
130 * and retry the packet command IDETAPE_MAX_PC_RETRIES times.
131 *
132 * Setting IDETAPE_MAX_PC_RETRIES to 0 will disable retries.
133 */
134#define IDETAPE_MAX_PC_RETRIES 3
135
136/*
137 * With each packet command, we allocate a buffer of
138 * IDETAPE_PC_BUFFER_SIZE bytes. This is used for several packet
139 * commands (Not for READ/WRITE commands).
140 */
141#define IDETAPE_PC_BUFFER_SIZE 256
142
143/*
144 * In various places in the driver, we need to allocate storage
145 * for packet commands and requests, which will remain valid while
146 * we leave the driver to wait for an interrupt or a timeout event.
147 */
148#define IDETAPE_PC_STACK (10 + IDETAPE_MAX_PC_RETRIES)
149
150/*
151 * Some drives (for example, Seagate STT3401A Travan) require a very long
152 * timeout, because they don't return an interrupt or clear their busy bit
153 * until after the command completes (even retension commands).
154 */
155#define IDETAPE_WAIT_CMD (900*HZ)
156
157/*
158 * The following parameter is used to select the point in the internal
159 * tape fifo in which we will start to refill the buffer. Decreasing
160 * the following parameter will improve the system's latency and
3a4fa0a2 161 * interactive response, while using a high value might improve system
1da177e4
LT
162 * throughput.
163 */
164#define IDETAPE_FIFO_THRESHOLD 2
165
166/*
167 * DSC polling parameters.
168 *
169 * Polling for DSC (a single bit in the status register) is a very
170 * important function in ide-tape. There are two cases in which we
171 * poll for DSC:
172 *
173 * 1. Before a read/write packet command, to ensure that we
174 * can transfer data from/to the tape's data buffers, without
175 * causing an actual media access. In case the tape is not
176 * ready yet, we take out our request from the device
177 * request queue, so that ide.c will service requests from
178 * the other device on the same interface meanwhile.
179 *
180 * 2. After the successful initialization of a "media access
181 * packet command", which is a command which can take a long
182 * time to complete (it can be several seconds or even an hour).
183 *
184 * Again, we postpone our request in the middle to free the bus
185 * for the other device. The polling frequency here should be
186 * lower than the read/write frequency since those media access
187 * commands are slow. We start from a "fast" frequency -
188 * IDETAPE_DSC_MA_FAST (one second), and if we don't receive DSC
189 * after IDETAPE_DSC_MA_THRESHOLD (5 minutes), we switch it to a
190 * lower frequency - IDETAPE_DSC_MA_SLOW (1 minute).
191 *
192 * We also set a timeout for the timer, in case something goes wrong.
193 * The timeout should be longer then the maximum execution time of a
194 * tape operation.
195 */
196
197/*
198 * DSC timings.
199 */
200#define IDETAPE_DSC_RW_MIN 5*HZ/100 /* 50 msec */
201#define IDETAPE_DSC_RW_MAX 40*HZ/100 /* 400 msec */
202#define IDETAPE_DSC_RW_TIMEOUT 2*60*HZ /* 2 minutes */
203#define IDETAPE_DSC_MA_FAST 2*HZ /* 2 seconds */
204#define IDETAPE_DSC_MA_THRESHOLD 5*60*HZ /* 5 minutes */
205#define IDETAPE_DSC_MA_SLOW 30*HZ /* 30 seconds */
206#define IDETAPE_DSC_MA_TIMEOUT 2*60*60*HZ /* 2 hours */
207
208/*************************** End of tunable parameters ***********************/
209
1da177e4
LT
210/*
211 * Read/Write error simulation
212 */
213#define SIMULATE_ERRORS 0
214
215/*
216 * For general magnetic tape device compatibility.
217 */
218typedef enum {
219 idetape_direction_none,
220 idetape_direction_read,
221 idetape_direction_write
222} idetape_chrdev_direction_t;
223
224struct idetape_bh {
ab057968 225 u32 b_size;
1da177e4
LT
226 atomic_t b_count;
227 struct idetape_bh *b_reqnext;
228 char *b_data;
229};
230
231/*
232 * Our view of a packet command.
233 */
234typedef struct idetape_packet_command_s {
235 u8 c[12]; /* Actual packet bytes */
236 int retries; /* On each retry, we increment retries */
237 int error; /* Error code */
238 int request_transfer; /* Bytes to transfer */
239 int actually_transferred; /* Bytes actually transferred */
240 int buffer_size; /* Size of our data buffer */
241 struct idetape_bh *bh;
242 char *b_data;
243 int b_count;
244 u8 *buffer; /* Data buffer */
245 u8 *current_position; /* Pointer into the above buffer */
246 ide_startstop_t (*callback) (ide_drive_t *); /* Called when this packet command is completed */
247 u8 pc_buffer[IDETAPE_PC_BUFFER_SIZE]; /* Temporary buffer */
248 unsigned long flags; /* Status/Action bit flags: long for set_bit */
249} idetape_pc_t;
250
251/*
252 * Packet command flag bits.
253 */
254/* Set when an error is considered normal - We won't retry */
255#define PC_ABORT 0
256/* 1 When polling for DSC on a media access command */
257#define PC_WAIT_FOR_DSC 1
258/* 1 when we prefer to use DMA if possible */
259#define PC_DMA_RECOMMENDED 2
260/* 1 while DMA in progress */
261#define PC_DMA_IN_PROGRESS 3
262/* 1 when encountered problem during DMA */
263#define PC_DMA_ERROR 4
264/* Data direction */
265#define PC_WRITING 5
266
267/*
268 * Capabilities and Mechanical Status Page
269 */
270typedef struct {
271 unsigned page_code :6; /* Page code - Should be 0x2a */
272 __u8 reserved0_6 :1;
273 __u8 ps :1; /* parameters saveable */
274 __u8 page_length; /* Page Length - Should be 0x12 */
275 __u8 reserved2, reserved3;
276 unsigned ro :1; /* Read Only Mode */
277 unsigned reserved4_1234 :4;
278 unsigned sprev :1; /* Supports SPACE in the reverse direction */
279 unsigned reserved4_67 :2;
280 unsigned reserved5_012 :3;
281 unsigned efmt :1; /* Supports ERASE command initiated formatting */
282 unsigned reserved5_4 :1;
283 unsigned qfa :1; /* Supports the QFA two partition formats */
284 unsigned reserved5_67 :2;
285 unsigned lock :1; /* Supports locking the volume */
286 unsigned locked :1; /* The volume is locked */
287 unsigned prevent :1; /* The device defaults in the prevent state after power up */
288 unsigned eject :1; /* The device can eject the volume */
289 __u8 disconnect :1; /* The device can break request > ctl */
290 __u8 reserved6_5 :1;
291 unsigned ecc :1; /* Supports error correction */
292 unsigned cmprs :1; /* Supports data compression */
293 unsigned reserved7_0 :1;
294 unsigned blk512 :1; /* Supports 512 bytes block size */
295 unsigned blk1024 :1; /* Supports 1024 bytes block size */
296 unsigned reserved7_3_6 :4;
297 unsigned blk32768 :1; /* slowb - the device restricts the byte count for PIO */
298 /* transfers for slow buffer memory ??? */
299 /* Also 32768 block size in some cases */
300 __u16 max_speed; /* Maximum speed supported in KBps */
301 __u8 reserved10, reserved11;
302 __u16 ctl; /* Continuous Transfer Limit in blocks */
303 __u16 speed; /* Current Speed, in KBps */
304 __u16 buffer_size; /* Buffer Size, in 512 bytes */
305 __u8 reserved18, reserved19;
306} idetape_capabilities_page_t;
307
308/*
309 * Block Size Page
310 */
311typedef struct {
312 unsigned page_code :6; /* Page code - Should be 0x30 */
313 unsigned reserved1_6 :1;
314 unsigned ps :1;
315 __u8 page_length; /* Page Length - Should be 2 */
316 __u8 reserved2;
317 unsigned play32 :1;
318 unsigned play32_5 :1;
319 unsigned reserved2_23 :2;
320 unsigned record32 :1;
321 unsigned record32_5 :1;
322 unsigned reserved2_6 :1;
323 unsigned one :1;
324} idetape_block_size_page_t;
325
326/*
327 * A pipeline stage.
328 */
329typedef struct idetape_stage_s {
330 struct request rq; /* The corresponding request */
331 struct idetape_bh *bh; /* The data buffers */
332 struct idetape_stage_s *next; /* Pointer to the next stage */
333} idetape_stage_t;
334
1da177e4
LT
335/*
336 * Most of our global data which we need to save even as we leave the
337 * driver due to an interrupt or a timer event is stored in a variable
338 * of type idetape_tape_t, defined below.
339 */
340typedef struct ide_tape_obj {
341 ide_drive_t *drive;
342 ide_driver_t *driver;
343 struct gendisk *disk;
344 struct kref kref;
345
346 /*
347 * Since a typical character device operation requires more
348 * than one packet command, we provide here enough memory
349 * for the maximum of interconnected packet commands.
350 * The packet commands are stored in the circular array pc_stack.
351 * pc_stack_index points to the last used entry, and warps around
352 * to the start when we get to the last array entry.
353 *
354 * pc points to the current processed packet command.
355 *
356 * failed_pc points to the last failed packet command, or contains
357 * NULL if we do not need to retry any packet command. This is
358 * required since an additional packet command is needed before the
359 * retry, to get detailed information on what went wrong.
360 */
361 /* Current packet command */
362 idetape_pc_t *pc;
363 /* Last failed packet command */
364 idetape_pc_t *failed_pc;
365 /* Packet command stack */
366 idetape_pc_t pc_stack[IDETAPE_PC_STACK];
367 /* Next free packet command storage space */
368 int pc_stack_index;
369 struct request rq_stack[IDETAPE_PC_STACK];
370 /* We implement a circular array */
371 int rq_stack_index;
372
373 /*
374 * DSC polling variables.
375 *
376 * While polling for DSC we use postponed_rq to postpone the
377 * current request so that ide.c will be able to service
378 * pending requests on the other device. Note that at most
379 * we will have only one DSC (usually data transfer) request
380 * in the device request queue. Additional requests can be
381 * queued in our internal pipeline, but they will be visible
382 * to ide.c only one at a time.
383 */
384 struct request *postponed_rq;
385 /* The time in which we started polling for DSC */
386 unsigned long dsc_polling_start;
387 /* Timer used to poll for dsc */
388 struct timer_list dsc_timer;
389 /* Read/Write dsc polling frequency */
390 unsigned long best_dsc_rw_frequency;
391 /* The current polling frequency */
392 unsigned long dsc_polling_frequency;
393 /* Maximum waiting time */
394 unsigned long dsc_timeout;
395
396 /*
397 * Read position information
398 */
399 u8 partition;
400 /* Current block */
401 unsigned int first_frame_position;
402 unsigned int last_frame_position;
403 unsigned int blocks_in_buffer;
404
405 /*
406 * Last error information
407 */
408 u8 sense_key, asc, ascq;
409
410 /*
411 * Character device operation
412 */
413 unsigned int minor;
414 /* device name */
415 char name[4];
416 /* Current character device data transfer direction */
417 idetape_chrdev_direction_t chrdev_direction;
418
419 /*
420 * Device information
421 */
422 /* Usually 512 or 1024 bytes */
423 unsigned short tape_block_size;
424 int user_bs_factor;
425 /* Copy of the tape's Capabilities and Mechanical Page */
426 idetape_capabilities_page_t capabilities;
427
428 /*
429 * Active data transfer request parameters.
430 *
431 * At most, there is only one ide-tape originated data transfer
432 * request in the device request queue. This allows ide.c to
433 * easily service requests from the other device when we
434 * postpone our active request. In the pipelined operation
435 * mode, we use our internal pipeline structure to hold
436 * more data requests.
437 *
438 * The data buffer size is chosen based on the tape's
439 * recommendation.
440 */
441 /* Pointer to the request which is waiting in the device request queue */
442 struct request *active_data_request;
443 /* Data buffer size (chosen based on the tape's recommendation */
444 int stage_size;
445 idetape_stage_t *merge_stage;
446 int merge_stage_size;
447 struct idetape_bh *bh;
448 char *b_data;
449 int b_count;
450
451 /*
452 * Pipeline parameters.
453 *
454 * To accomplish non-pipelined mode, we simply set the following
455 * variables to zero (or NULL, where appropriate).
456 */
457 /* Number of currently used stages */
458 int nr_stages;
459 /* Number of pending stages */
460 int nr_pending_stages;
461 /* We will not allocate more than this number of stages */
462 int max_stages, min_pipeline, max_pipeline;
463 /* The first stage which will be removed from the pipeline */
464 idetape_stage_t *first_stage;
465 /* The currently active stage */
466 idetape_stage_t *active_stage;
467 /* Will be serviced after the currently active request */
468 idetape_stage_t *next_stage;
469 /* New requests will be added to the pipeline here */
470 idetape_stage_t *last_stage;
471 /* Optional free stage which we can use */
472 idetape_stage_t *cache_stage;
473 int pages_per_stage;
474 /* Wasted space in each stage */
475 int excess_bh_size;
476
477 /* Status/Action flags: long for set_bit */
478 unsigned long flags;
479 /* protects the ide-tape queue */
480 spinlock_t spinlock;
481
482 /*
483 * Measures average tape speed
484 */
485 unsigned long avg_time;
486 int avg_size;
487 int avg_speed;
488
1da177e4
LT
489 char vendor_id[10];
490 char product_id[18];
491 char firmware_revision[6];
492 int firmware_revision_num;
493
494 /* the door is currently locked */
495 int door_locked;
496 /* the tape hardware is write protected */
497 char drv_write_prot;
498 /* the tape is write protected (hardware or opened as read-only) */
499 char write_prot;
500
501 /*
502 * Limit the number of times a request can
503 * be postponed, to avoid an infinite postpone
504 * deadlock.
505 */
506 /* request postpone count limit */
507 int postpone_cnt;
508
509 /*
510 * Measures number of frames:
511 *
512 * 1. written/read to/from the driver pipeline (pipeline_head).
513 * 2. written/read to/from the tape buffers (idetape_bh).
514 * 3. written/read by the tape to/from the media (tape_head).
515 */
516 int pipeline_head;
517 int buffer_head;
518 int tape_head;
519 int last_tape_head;
520
521 /*
522 * Speed control at the tape buffers input/output
523 */
524 unsigned long insert_time;
525 int insert_size;
526 int insert_speed;
527 int max_insert_speed;
528 int measure_insert_time;
529
530 /*
531 * Measure tape still time, in milliseconds
532 */
533 unsigned long tape_still_time_begin;
534 int tape_still_time;
535
536 /*
537 * Speed regulation negative feedback loop
538 */
539 int speed_control;
540 int pipeline_head_speed;
541 int controlled_pipeline_head_speed;
542 int uncontrolled_pipeline_head_speed;
543 int controlled_last_pipeline_head;
544 int uncontrolled_last_pipeline_head;
545 unsigned long uncontrolled_pipeline_head_time;
546 unsigned long controlled_pipeline_head_time;
547 int controlled_previous_pipeline_head;
548 int uncontrolled_previous_pipeline_head;
549 unsigned long controlled_previous_head_time;
550 unsigned long uncontrolled_previous_head_time;
551 int restart_speed_control_req;
552
553 /*
554 * Debug_level determines amount of debugging output;
555 * can be changed using /proc/ide/hdx/settings
556 * 0 : almost no debugging output
557 * 1 : 0+output errors only
558 * 2 : 1+output all sensekey/asc
559 * 3 : 2+follow all chrdev related procedures
560 * 4 : 3+follow all procedures
561 * 5 : 4+include pc_stack rq_stack info
562 * 6 : 5+USE_COUNT updates
563 */
564 int debug_level;
565} idetape_tape_t;
566
cf8b8975 567static DEFINE_MUTEX(idetape_ref_mutex);
1da177e4 568
d5dee80a
WD
569static struct class *idetape_sysfs_class;
570
1da177e4
LT
571#define to_ide_tape(obj) container_of(obj, struct ide_tape_obj, kref)
572
573#define ide_tape_g(disk) \
574 container_of((disk)->private_data, struct ide_tape_obj, driver)
575
576static struct ide_tape_obj *ide_tape_get(struct gendisk *disk)
577{
578 struct ide_tape_obj *tape = NULL;
579
cf8b8975 580 mutex_lock(&idetape_ref_mutex);
1da177e4
LT
581 tape = ide_tape_g(disk);
582 if (tape)
583 kref_get(&tape->kref);
cf8b8975 584 mutex_unlock(&idetape_ref_mutex);
1da177e4
LT
585 return tape;
586}
587
588static void ide_tape_release(struct kref *);
589
590static void ide_tape_put(struct ide_tape_obj *tape)
591{
cf8b8975 592 mutex_lock(&idetape_ref_mutex);
1da177e4 593 kref_put(&tape->kref, ide_tape_release);
cf8b8975 594 mutex_unlock(&idetape_ref_mutex);
1da177e4
LT
595}
596
597/*
598 * Tape door status
599 */
600#define DOOR_UNLOCKED 0
601#define DOOR_LOCKED 1
602#define DOOR_EXPLICITLY_LOCKED 2
603
604/*
605 * Tape flag bits values.
606 */
607#define IDETAPE_IGNORE_DSC 0
608#define IDETAPE_ADDRESS_VALID 1 /* 0 When the tape position is unknown */
609#define IDETAPE_BUSY 2 /* Device already opened */
610#define IDETAPE_PIPELINE_ERROR 3 /* Error detected in a pipeline stage */
611#define IDETAPE_DETECT_BS 4 /* Attempt to auto-detect the current user block size */
612#define IDETAPE_FILEMARK 5 /* Currently on a filemark */
613#define IDETAPE_DRQ_INTERRUPT 6 /* DRQ interrupt device */
614#define IDETAPE_READ_ERROR 7
615#define IDETAPE_PIPELINE_ACTIVE 8 /* pipeline active */
616/* 0 = no tape is loaded, so we don't rewind after ejecting */
617#define IDETAPE_MEDIUM_PRESENT 9
618
619/*
620 * Supported ATAPI tape drives packet commands
621 */
622#define IDETAPE_TEST_UNIT_READY_CMD 0x00
623#define IDETAPE_REWIND_CMD 0x01
624#define IDETAPE_REQUEST_SENSE_CMD 0x03
625#define IDETAPE_READ_CMD 0x08
626#define IDETAPE_WRITE_CMD 0x0a
627#define IDETAPE_WRITE_FILEMARK_CMD 0x10
628#define IDETAPE_SPACE_CMD 0x11
629#define IDETAPE_INQUIRY_CMD 0x12
630#define IDETAPE_ERASE_CMD 0x19
631#define IDETAPE_MODE_SENSE_CMD 0x1a
632#define IDETAPE_MODE_SELECT_CMD 0x15
633#define IDETAPE_LOAD_UNLOAD_CMD 0x1b
634#define IDETAPE_PREVENT_CMD 0x1e
635#define IDETAPE_LOCATE_CMD 0x2b
636#define IDETAPE_READ_POSITION_CMD 0x34
637#define IDETAPE_READ_BUFFER_CMD 0x3c
638#define IDETAPE_SET_SPEED_CMD 0xbb
639
640/*
641 * Some defines for the READ BUFFER command
642 */
643#define IDETAPE_RETRIEVE_FAULTY_BLOCK 6
644
645/*
646 * Some defines for the SPACE command
647 */
648#define IDETAPE_SPACE_OVER_FILEMARK 1
649#define IDETAPE_SPACE_TO_EOD 3
650
651/*
652 * Some defines for the LOAD UNLOAD command
653 */
654#define IDETAPE_LU_LOAD_MASK 1
655#define IDETAPE_LU_RETENSION_MASK 2
656#define IDETAPE_LU_EOT_MASK 4
657
658/*
659 * Special requests for our block device strategy routine.
660 *
661 * In order to service a character device command, we add special
662 * requests to the tail of our block device request queue and wait
663 * for their completion.
664 */
665
666enum {
667 REQ_IDETAPE_PC1 = (1 << 0), /* packet command (first stage) */
668 REQ_IDETAPE_PC2 = (1 << 1), /* packet command (second stage) */
669 REQ_IDETAPE_READ = (1 << 2),
670 REQ_IDETAPE_WRITE = (1 << 3),
671 REQ_IDETAPE_READ_BUFFER = (1 << 4),
672};
673
674/*
675 * Error codes which are returned in rq->errors to the higher part
676 * of the driver.
677 */
678#define IDETAPE_ERROR_GENERAL 101
679#define IDETAPE_ERROR_FILEMARK 102
680#define IDETAPE_ERROR_EOD 103
681
682/*
683 * The following is used to format the general configuration word of
684 * the ATAPI IDENTIFY DEVICE command.
685 */
686struct idetape_id_gcw {
687 unsigned packet_size :2; /* Packet Size */
688 unsigned reserved234 :3; /* Reserved */
689 unsigned drq_type :2; /* Command packet DRQ type */
690 unsigned removable :1; /* Removable media */
691 unsigned device_type :5; /* Device type */
692 unsigned reserved13 :1; /* Reserved */
693 unsigned protocol :2; /* Protocol type */
694};
695
696/*
697 * INQUIRY packet command - Data Format (From Table 6-8 of QIC-157C)
698 */
699typedef struct {
700 unsigned device_type :5; /* Peripheral Device Type */
701 unsigned reserved0_765 :3; /* Peripheral Qualifier - Reserved */
702 unsigned reserved1_6t0 :7; /* Reserved */
703 unsigned rmb :1; /* Removable Medium Bit */
704 unsigned ansi_version :3; /* ANSI Version */
705 unsigned ecma_version :3; /* ECMA Version */
706 unsigned iso_version :2; /* ISO Version */
707 unsigned response_format :4; /* Response Data Format */
708 unsigned reserved3_45 :2; /* Reserved */
709 unsigned reserved3_6 :1; /* TrmIOP - Reserved */
710 unsigned reserved3_7 :1; /* AENC - Reserved */
711 __u8 additional_length; /* Additional Length (total_length-4) */
712 __u8 rsv5, rsv6, rsv7; /* Reserved */
713 __u8 vendor_id[8]; /* Vendor Identification */
714 __u8 product_id[16]; /* Product Identification */
715 __u8 revision_level[4]; /* Revision Level */
716 __u8 vendor_specific[20]; /* Vendor Specific - Optional */
717 __u8 reserved56t95[40]; /* Reserved - Optional */
718 /* Additional information may be returned */
719} idetape_inquiry_result_t;
720
721/*
722 * READ POSITION packet command - Data Format (From Table 6-57)
723 */
724typedef struct {
725 unsigned reserved0_10 :2; /* Reserved */
726 unsigned bpu :1; /* Block Position Unknown */
727 unsigned reserved0_543 :3; /* Reserved */
728 unsigned eop :1; /* End Of Partition */
729 unsigned bop :1; /* Beginning Of Partition */
730 u8 partition; /* Partition Number */
731 u8 reserved2, reserved3; /* Reserved */
732 u32 first_block; /* First Block Location */
733 u32 last_block; /* Last Block Location (Optional) */
734 u8 reserved12; /* Reserved */
735 u8 blocks_in_buffer[3]; /* Blocks In Buffer - (Optional) */
736 u32 bytes_in_buffer; /* Bytes In Buffer (Optional) */
737} idetape_read_position_result_t;
738
739/*
740 * Follows structures which are related to the SELECT SENSE / MODE SENSE
741 * packet commands. Those packet commands are still not supported
742 * by ide-tape.
743 */
744#define IDETAPE_BLOCK_DESCRIPTOR 0
745#define IDETAPE_CAPABILITIES_PAGE 0x2a
746#define IDETAPE_PARAMTR_PAGE 0x2b /* Onstream DI-x0 only */
747#define IDETAPE_BLOCK_SIZE_PAGE 0x30
748#define IDETAPE_BUFFER_FILLING_PAGE 0x33
749
750/*
751 * Mode Parameter Header for the MODE SENSE packet command
752 */
753typedef struct {
754 __u8 mode_data_length; /* Length of the following data transfer */
755 __u8 medium_type; /* Medium Type */
756 __u8 dsp; /* Device Specific Parameter */
757 __u8 bdl; /* Block Descriptor Length */
1da177e4
LT
758} idetape_mode_parameter_header_t;
759
760/*
761 * Mode Parameter Block Descriptor the MODE SENSE packet command
762 *
763 * Support for block descriptors is optional.
764 */
765typedef struct {
766 __u8 density_code; /* Medium density code */
767 __u8 blocks[3]; /* Number of blocks */
768 __u8 reserved4; /* Reserved */
769 __u8 length[3]; /* Block Length */
770} idetape_parameter_block_descriptor_t;
771
772/*
773 * The Data Compression Page, as returned by the MODE SENSE packet command.
774 */
775typedef struct {
776 unsigned page_code :6; /* Page Code - Should be 0xf */
777 unsigned reserved0 :1; /* Reserved */
778 unsigned ps :1;
779 __u8 page_length; /* Page Length - Should be 14 */
780 unsigned reserved2 :6; /* Reserved */
781 unsigned dcc :1; /* Data Compression Capable */
782 unsigned dce :1; /* Data Compression Enable */
783 unsigned reserved3 :5; /* Reserved */
784 unsigned red :2; /* Report Exception on Decompression */
785 unsigned dde :1; /* Data Decompression Enable */
786 __u32 ca; /* Compression Algorithm */
787 __u32 da; /* Decompression Algorithm */
788 __u8 reserved[4]; /* Reserved */
789} idetape_data_compression_page_t;
790
791/*
792 * The Medium Partition Page, as returned by the MODE SENSE packet command.
793 */
794typedef struct {
795 unsigned page_code :6; /* Page Code - Should be 0x11 */
796 unsigned reserved1_6 :1; /* Reserved */
797 unsigned ps :1;
798 __u8 page_length; /* Page Length - Should be 6 */
799 __u8 map; /* Maximum Additional Partitions - Should be 0 */
800 __u8 apd; /* Additional Partitions Defined - Should be 0 */
801 unsigned reserved4_012 :3; /* Reserved */
802 unsigned psum :2; /* Should be 0 */
803 unsigned idp :1; /* Should be 0 */
804 unsigned sdp :1; /* Should be 0 */
805 unsigned fdp :1; /* Fixed Data Partitions */
806 __u8 mfr; /* Medium Format Recognition */
807 __u8 reserved[2]; /* Reserved */
808} idetape_medium_partition_page_t;
809
810/*
811 * Run time configurable parameters.
812 */
813typedef struct {
814 int dsc_rw_frequency;
815 int dsc_media_access_frequency;
816 int nr_stages;
817} idetape_config_t;
818
819/*
820 * The variables below are used for the character device interface.
821 * Additional state variables are defined in our ide_drive_t structure.
822 */
823static struct ide_tape_obj * idetape_devs[MAX_HWIFS * MAX_DRIVES];
824
825#define ide_tape_f(file) ((file)->private_data)
826
827static struct ide_tape_obj *ide_tape_chrdev_get(unsigned int i)
828{
829 struct ide_tape_obj *tape = NULL;
830
cf8b8975 831 mutex_lock(&idetape_ref_mutex);
1da177e4
LT
832 tape = idetape_devs[i];
833 if (tape)
834 kref_get(&tape->kref);
cf8b8975 835 mutex_unlock(&idetape_ref_mutex);
1da177e4
LT
836 return tape;
837}
838
839/*
840 * Function declarations
841 *
842 */
843static int idetape_chrdev_release (struct inode *inode, struct file *filp);
844static void idetape_write_release (ide_drive_t *drive, unsigned int minor);
845
846/*
847 * Too bad. The drive wants to send us data which we are not ready to accept.
848 * Just throw it away.
849 */
850static void idetape_discard_data (ide_drive_t *drive, unsigned int bcount)
851{
852 while (bcount--)
853 (void) HWIF(drive)->INB(IDE_DATA_REG);
854}
855
856static void idetape_input_buffers (ide_drive_t *drive, idetape_pc_t *pc, unsigned int bcount)
857{
858 struct idetape_bh *bh = pc->bh;
859 int count;
860
861 while (bcount) {
862#if IDETAPE_DEBUG_BUGS
863 if (bh == NULL) {
864 printk(KERN_ERR "ide-tape: bh == NULL in "
865 "idetape_input_buffers\n");
866 idetape_discard_data(drive, bcount);
867 return;
868 }
869#endif /* IDETAPE_DEBUG_BUGS */
870 count = min((unsigned int)(bh->b_size - atomic_read(&bh->b_count)), bcount);
871 HWIF(drive)->atapi_input_bytes(drive, bh->b_data + atomic_read(&bh->b_count), count);
872 bcount -= count;
873 atomic_add(count, &bh->b_count);
874 if (atomic_read(&bh->b_count) == bh->b_size) {
875 bh = bh->b_reqnext;
876 if (bh)
877 atomic_set(&bh->b_count, 0);
878 }
879 }
880 pc->bh = bh;
881}
882
883static void idetape_output_buffers (ide_drive_t *drive, idetape_pc_t *pc, unsigned int bcount)
884{
885 struct idetape_bh *bh = pc->bh;
886 int count;
887
888 while (bcount) {
889#if IDETAPE_DEBUG_BUGS
890 if (bh == NULL) {
891 printk(KERN_ERR "ide-tape: bh == NULL in "
892 "idetape_output_buffers\n");
893 return;
894 }
895#endif /* IDETAPE_DEBUG_BUGS */
896 count = min((unsigned int)pc->b_count, (unsigned int)bcount);
897 HWIF(drive)->atapi_output_bytes(drive, pc->b_data, count);
898 bcount -= count;
899 pc->b_data += count;
900 pc->b_count -= count;
901 if (!pc->b_count) {
902 pc->bh = bh = bh->b_reqnext;
903 if (bh) {
904 pc->b_data = bh->b_data;
905 pc->b_count = atomic_read(&bh->b_count);
906 }
907 }
908 }
909}
910
911static void idetape_update_buffers (idetape_pc_t *pc)
912{
913 struct idetape_bh *bh = pc->bh;
914 int count;
915 unsigned int bcount = pc->actually_transferred;
916
917 if (test_bit(PC_WRITING, &pc->flags))
918 return;
919 while (bcount) {
920#if IDETAPE_DEBUG_BUGS
921 if (bh == NULL) {
922 printk(KERN_ERR "ide-tape: bh == NULL in "
923 "idetape_update_buffers\n");
924 return;
925 }
926#endif /* IDETAPE_DEBUG_BUGS */
927 count = min((unsigned int)bh->b_size, (unsigned int)bcount);
928 atomic_set(&bh->b_count, count);
929 if (atomic_read(&bh->b_count) == bh->b_size)
930 bh = bh->b_reqnext;
931 bcount -= count;
932 }
933 pc->bh = bh;
934}
935
936/*
937 * idetape_next_pc_storage returns a pointer to a place in which we can
938 * safely store a packet command, even though we intend to leave the
939 * driver. A storage space for a maximum of IDETAPE_PC_STACK packet
940 * commands is allocated at initialization time.
941 */
942static idetape_pc_t *idetape_next_pc_storage (ide_drive_t *drive)
943{
944 idetape_tape_t *tape = drive->driver_data;
945
946#if IDETAPE_DEBUG_LOG
947 if (tape->debug_level >= 5)
948 printk(KERN_INFO "ide-tape: pc_stack_index=%d\n",
949 tape->pc_stack_index);
950#endif /* IDETAPE_DEBUG_LOG */
951 if (tape->pc_stack_index == IDETAPE_PC_STACK)
952 tape->pc_stack_index=0;
953 return (&tape->pc_stack[tape->pc_stack_index++]);
954}
955
956/*
957 * idetape_next_rq_storage is used along with idetape_next_pc_storage.
958 * Since we queue packet commands in the request queue, we need to
959 * allocate a request, along with the allocation of a packet command.
960 */
961
962/**************************************************************
963 * *
964 * This should get fixed to use kmalloc(.., GFP_ATOMIC) *
965 * followed later on by kfree(). -ml *
966 * *
967 **************************************************************/
968
969static struct request *idetape_next_rq_storage (ide_drive_t *drive)
970{
971 idetape_tape_t *tape = drive->driver_data;
972
973#if IDETAPE_DEBUG_LOG
974 if (tape->debug_level >= 5)
975 printk(KERN_INFO "ide-tape: rq_stack_index=%d\n",
976 tape->rq_stack_index);
977#endif /* IDETAPE_DEBUG_LOG */
978 if (tape->rq_stack_index == IDETAPE_PC_STACK)
979 tape->rq_stack_index=0;
980 return (&tape->rq_stack[tape->rq_stack_index++]);
981}
982
983/*
984 * idetape_init_pc initializes a packet command.
985 */
986static void idetape_init_pc (idetape_pc_t *pc)
987{
988 memset(pc->c, 0, 12);
989 pc->retries = 0;
990 pc->flags = 0;
991 pc->request_transfer = 0;
992 pc->buffer = pc->pc_buffer;
993 pc->buffer_size = IDETAPE_PC_BUFFER_SIZE;
994 pc->bh = NULL;
995 pc->b_data = NULL;
996}
997
998/*
1b5db434
BP
999 * called on each failed packet command retry to analyze the request sense. We
1000 * currently do not utilize this information.
1da177e4 1001 */
1b5db434 1002static void idetape_analyze_error(ide_drive_t *drive, u8 *sense)
1da177e4
LT
1003{
1004 idetape_tape_t *tape = drive->driver_data;
1005 idetape_pc_t *pc = tape->failed_pc;
1006
1b5db434
BP
1007 tape->sense_key = sense[2] & 0xF;
1008 tape->asc = sense[12];
1009 tape->ascq = sense[13];
1da177e4
LT
1010#if IDETAPE_DEBUG_LOG
1011 /*
1b5db434
BP
1012 * Without debugging, we only log an error if we decided to give up
1013 * retrying.
1da177e4
LT
1014 */
1015 if (tape->debug_level >= 1)
1016 printk(KERN_INFO "ide-tape: pc = %x, sense key = %x, "
1017 "asc = %x, ascq = %x\n",
1b5db434
BP
1018 pc->c[0], tape->sense_key,
1019 tape->asc, tape->ascq);
1da177e4
LT
1020#endif /* IDETAPE_DEBUG_LOG */
1021
1b5db434 1022 /* Correct pc->actually_transferred by asking the tape. */
1da177e4 1023 if (test_bit(PC_DMA_ERROR, &pc->flags)) {
1b5db434
BP
1024 pc->actually_transferred = pc->request_transfer -
1025 tape->tape_block_size *
1026 ntohl(get_unaligned((u32 *)&sense[3]));
1da177e4
LT
1027 idetape_update_buffers(pc);
1028 }
1029
1030 /*
1031 * If error was the result of a zero-length read or write command,
1032 * with sense key=5, asc=0x22, ascq=0, let it slide. Some drives
1033 * (i.e. Seagate STT3401A Travan) don't support 0-length read/writes.
1034 */
1035 if ((pc->c[0] == IDETAPE_READ_CMD || pc->c[0] == IDETAPE_WRITE_CMD)
1b5db434
BP
1036 /* length == 0 */
1037 && pc->c[4] == 0 && pc->c[3] == 0 && pc->c[2] == 0) {
1038 if (tape->sense_key == 5) {
1da177e4
LT
1039 /* don't report an error, everything's ok */
1040 pc->error = 0;
1041 /* don't retry read/write */
1042 set_bit(PC_ABORT, &pc->flags);
1043 }
1044 }
1b5db434 1045 if (pc->c[0] == IDETAPE_READ_CMD && (sense[2] & 0x80)) {
1da177e4
LT
1046 pc->error = IDETAPE_ERROR_FILEMARK;
1047 set_bit(PC_ABORT, &pc->flags);
1048 }
1049 if (pc->c[0] == IDETAPE_WRITE_CMD) {
1b5db434
BP
1050 if ((sense[2] & 0x40) || (tape->sense_key == 0xd
1051 && tape->asc == 0x0 && tape->ascq == 0x2)) {
1da177e4
LT
1052 pc->error = IDETAPE_ERROR_EOD;
1053 set_bit(PC_ABORT, &pc->flags);
1054 }
1055 }
1056 if (pc->c[0] == IDETAPE_READ_CMD || pc->c[0] == IDETAPE_WRITE_CMD) {
1b5db434 1057 if (tape->sense_key == 8) {
1da177e4
LT
1058 pc->error = IDETAPE_ERROR_EOD;
1059 set_bit(PC_ABORT, &pc->flags);
1060 }
1061 if (!test_bit(PC_ABORT, &pc->flags) &&
1062 pc->actually_transferred)
1063 pc->retries = IDETAPE_MAX_PC_RETRIES + 1;
1064 }
1065}
1066
1067/*
1068 * idetape_active_next_stage will declare the next stage as "active".
1069 */
1070static void idetape_active_next_stage (ide_drive_t *drive)
1071{
1072 idetape_tape_t *tape = drive->driver_data;
1073 idetape_stage_t *stage = tape->next_stage;
1074 struct request *rq = &stage->rq;
1075
1076#if IDETAPE_DEBUG_LOG
1077 if (tape->debug_level >= 4)
1078 printk(KERN_INFO "ide-tape: Reached idetape_active_next_stage\n");
1079#endif /* IDETAPE_DEBUG_LOG */
1080#if IDETAPE_DEBUG_BUGS
1081 if (stage == NULL) {
1082 printk(KERN_ERR "ide-tape: bug: Trying to activate a non existing stage\n");
1083 return;
1084 }
1085#endif /* IDETAPE_DEBUG_BUGS */
1086
1087 rq->rq_disk = tape->disk;
1088 rq->buffer = NULL;
1089 rq->special = (void *)stage->bh;
1090 tape->active_data_request = rq;
1091 tape->active_stage = stage;
1092 tape->next_stage = stage->next;
1093}
1094
1095/*
1096 * idetape_increase_max_pipeline_stages is a part of the feedback
1097 * loop which tries to find the optimum number of stages. In the
1098 * feedback loop, we are starting from a minimum maximum number of
1099 * stages, and if we sense that the pipeline is empty, we try to
1100 * increase it, until we reach the user compile time memory limit.
1101 */
1102static void idetape_increase_max_pipeline_stages (ide_drive_t *drive)
1103{
1104 idetape_tape_t *tape = drive->driver_data;
1105 int increase = (tape->max_pipeline - tape->min_pipeline) / 10;
1106
1107#if IDETAPE_DEBUG_LOG
1108 if (tape->debug_level >= 4)
1109 printk (KERN_INFO "ide-tape: Reached idetape_increase_max_pipeline_stages\n");
1110#endif /* IDETAPE_DEBUG_LOG */
1111
1112 tape->max_stages += max(increase, 1);
1113 tape->max_stages = max(tape->max_stages, tape->min_pipeline);
1114 tape->max_stages = min(tape->max_stages, tape->max_pipeline);
1115}
1116
1117/*
1118 * idetape_kfree_stage calls kfree to completely free a stage, along with
1119 * its related buffers.
1120 */
1121static void __idetape_kfree_stage (idetape_stage_t *stage)
1122{
1123 struct idetape_bh *prev_bh, *bh = stage->bh;
1124 int size;
1125
1126 while (bh != NULL) {
1127 if (bh->b_data != NULL) {
1128 size = (int) bh->b_size;
1129 while (size > 0) {
1130 free_page((unsigned long) bh->b_data);
1131 size -= PAGE_SIZE;
1132 bh->b_data += PAGE_SIZE;
1133 }
1134 }
1135 prev_bh = bh;
1136 bh = bh->b_reqnext;
1137 kfree(prev_bh);
1138 }
1139 kfree(stage);
1140}
1141
1142static void idetape_kfree_stage (idetape_tape_t *tape, idetape_stage_t *stage)
1143{
1144 __idetape_kfree_stage(stage);
1145}
1146
1147/*
1148 * idetape_remove_stage_head removes tape->first_stage from the pipeline.
1149 * The caller should avoid race conditions.
1150 */
1151static void idetape_remove_stage_head (ide_drive_t *drive)
1152{
1153 idetape_tape_t *tape = drive->driver_data;
1154 idetape_stage_t *stage;
1155
1156#if IDETAPE_DEBUG_LOG
1157 if (tape->debug_level >= 4)
1158 printk(KERN_INFO "ide-tape: Reached idetape_remove_stage_head\n");
1159#endif /* IDETAPE_DEBUG_LOG */
1160#if IDETAPE_DEBUG_BUGS
1161 if (tape->first_stage == NULL) {
1162 printk(KERN_ERR "ide-tape: bug: tape->first_stage is NULL\n");
1163 return;
1164 }
1165 if (tape->active_stage == tape->first_stage) {
1166 printk(KERN_ERR "ide-tape: bug: Trying to free our active pipeline stage\n");
1167 return;
1168 }
1169#endif /* IDETAPE_DEBUG_BUGS */
1170 stage = tape->first_stage;
1171 tape->first_stage = stage->next;
1172 idetape_kfree_stage(tape, stage);
1173 tape->nr_stages--;
1174 if (tape->first_stage == NULL) {
1175 tape->last_stage = NULL;
1176#if IDETAPE_DEBUG_BUGS
1177 if (tape->next_stage != NULL)
1178 printk(KERN_ERR "ide-tape: bug: tape->next_stage != NULL\n");
1179 if (tape->nr_stages)
1180 printk(KERN_ERR "ide-tape: bug: nr_stages should be 0 now\n");
1181#endif /* IDETAPE_DEBUG_BUGS */
1182 }
1183}
1184
1185/*
1186 * This will free all the pipeline stages starting from new_last_stage->next
1187 * to the end of the list, and point tape->last_stage to new_last_stage.
1188 */
1189static void idetape_abort_pipeline(ide_drive_t *drive,
1190 idetape_stage_t *new_last_stage)
1191{
1192 idetape_tape_t *tape = drive->driver_data;
1193 idetape_stage_t *stage = new_last_stage->next;
1194 idetape_stage_t *nstage;
1195
1196#if IDETAPE_DEBUG_LOG
1197 if (tape->debug_level >= 4)
1198 printk(KERN_INFO "ide-tape: %s: idetape_abort_pipeline called\n", tape->name);
1199#endif
1200 while (stage) {
1201 nstage = stage->next;
1202 idetape_kfree_stage(tape, stage);
1203 --tape->nr_stages;
1204 --tape->nr_pending_stages;
1205 stage = nstage;
1206 }
1207 if (new_last_stage)
1208 new_last_stage->next = NULL;
1209 tape->last_stage = new_last_stage;
1210 tape->next_stage = NULL;
1211}
1212
1213/*
1214 * idetape_end_request is used to finish servicing a request, and to
1215 * insert a pending pipeline request into the main device queue.
1216 */
1217static int idetape_end_request(ide_drive_t *drive, int uptodate, int nr_sects)
1218{
1219 struct request *rq = HWGROUP(drive)->rq;
1220 idetape_tape_t *tape = drive->driver_data;
1221 unsigned long flags;
1222 int error;
1223 int remove_stage = 0;
1224 idetape_stage_t *active_stage;
1225
1226#if IDETAPE_DEBUG_LOG
1227 if (tape->debug_level >= 4)
1228 printk(KERN_INFO "ide-tape: Reached idetape_end_request\n");
1229#endif /* IDETAPE_DEBUG_LOG */
1230
1231 switch (uptodate) {
1232 case 0: error = IDETAPE_ERROR_GENERAL; break;
1233 case 1: error = 0; break;
1234 default: error = uptodate;
1235 }
1236 rq->errors = error;
1237 if (error)
1238 tape->failed_pc = NULL;
1239
3687221f
BZ
1240 if (!blk_special_request(rq)) {
1241 ide_end_request(drive, uptodate, nr_sects);
1242 return 0;
1243 }
1244
1da177e4
LT
1245 spin_lock_irqsave(&tape->spinlock, flags);
1246
1247 /* The request was a pipelined data transfer request */
1248 if (tape->active_data_request == rq) {
1249 active_stage = tape->active_stage;
1250 tape->active_stage = NULL;
1251 tape->active_data_request = NULL;
1252 tape->nr_pending_stages--;
1253 if (rq->cmd[0] & REQ_IDETAPE_WRITE) {
1254 remove_stage = 1;
1255 if (error) {
1256 set_bit(IDETAPE_PIPELINE_ERROR, &tape->flags);
1257 if (error == IDETAPE_ERROR_EOD)
1258 idetape_abort_pipeline(drive, active_stage);
1259 }
1260 } else if (rq->cmd[0] & REQ_IDETAPE_READ) {
1261 if (error == IDETAPE_ERROR_EOD) {
1262 set_bit(IDETAPE_PIPELINE_ERROR, &tape->flags);
1263 idetape_abort_pipeline(drive, active_stage);
1264 }
1265 }
1266 if (tape->next_stage != NULL) {
1267 idetape_active_next_stage(drive);
1268
1269 /*
1270 * Insert the next request into the request queue.
1271 */
1272 (void) ide_do_drive_cmd(drive, tape->active_data_request, ide_end);
1273 } else if (!error) {
1274 idetape_increase_max_pipeline_stages(drive);
1275 }
1276 }
1277 ide_end_drive_cmd(drive, 0, 0);
1278// blkdev_dequeue_request(rq);
1279// drive->rq = NULL;
1280// end_that_request_last(rq);
1281
1282 if (remove_stage)
1283 idetape_remove_stage_head(drive);
1284 if (tape->active_data_request == NULL)
1285 clear_bit(IDETAPE_PIPELINE_ACTIVE, &tape->flags);
1286 spin_unlock_irqrestore(&tape->spinlock, flags);
1287 return 0;
1288}
1289
1290static ide_startstop_t idetape_request_sense_callback (ide_drive_t *drive)
1291{
1292 idetape_tape_t *tape = drive->driver_data;
1293
1294#if IDETAPE_DEBUG_LOG
1295 if (tape->debug_level >= 4)
1296 printk(KERN_INFO "ide-tape: Reached idetape_request_sense_callback\n");
1297#endif /* IDETAPE_DEBUG_LOG */
1298 if (!tape->pc->error) {
1b5db434 1299 idetape_analyze_error(drive, tape->pc->buffer);
1da177e4
LT
1300 idetape_end_request(drive, 1, 0);
1301 } else {
1302 printk(KERN_ERR "ide-tape: Error in REQUEST SENSE itself - Aborting request!\n");
1303 idetape_end_request(drive, 0, 0);
1304 }
1305 return ide_stopped;
1306}
1307
1308static void idetape_create_request_sense_cmd (idetape_pc_t *pc)
1309{
1310 idetape_init_pc(pc);
1311 pc->c[0] = IDETAPE_REQUEST_SENSE_CMD;
1312 pc->c[4] = 20;
1313 pc->request_transfer = 20;
1314 pc->callback = &idetape_request_sense_callback;
1315}
1316
1317static void idetape_init_rq(struct request *rq, u8 cmd)
1318{
1319 memset(rq, 0, sizeof(*rq));
4aff5e23 1320 rq->cmd_type = REQ_TYPE_SPECIAL;
1da177e4
LT
1321 rq->cmd[0] = cmd;
1322}
1323
1324/*
1325 * idetape_queue_pc_head generates a new packet command request in front
1326 * of the request queue, before the current request, so that it will be
1327 * processed immediately, on the next pass through the driver.
1328 *
1329 * idetape_queue_pc_head is called from the request handling part of
1330 * the driver (the "bottom" part). Safe storage for the request should
1331 * be allocated with idetape_next_pc_storage and idetape_next_rq_storage
1332 * before calling idetape_queue_pc_head.
1333 *
1334 * Memory for those requests is pre-allocated at initialization time, and
1335 * is limited to IDETAPE_PC_STACK requests. We assume that we have enough
1336 * space for the maximum possible number of inter-dependent packet commands.
1337 *
1338 * The higher level of the driver - The ioctl handler and the character
1339 * device handling functions should queue request to the lower level part
1340 * and wait for their completion using idetape_queue_pc_tail or
1341 * idetape_queue_rw_tail.
1342 */
1343static void idetape_queue_pc_head (ide_drive_t *drive, idetape_pc_t *pc,struct request *rq)
1344{
1345 struct ide_tape_obj *tape = drive->driver_data;
1346
1347 idetape_init_rq(rq, REQ_IDETAPE_PC1);
1348 rq->buffer = (char *) pc;
1349 rq->rq_disk = tape->disk;
1350 (void) ide_do_drive_cmd(drive, rq, ide_preempt);
1351}
1352
1353/*
1354 * idetape_retry_pc is called when an error was detected during the
1355 * last packet command. We queue a request sense packet command in
1356 * the head of the request list.
1357 */
1358static ide_startstop_t idetape_retry_pc (ide_drive_t *drive)
1359{
1360 idetape_tape_t *tape = drive->driver_data;
1361 idetape_pc_t *pc;
1362 struct request *rq;
1da177e4 1363
0e38a66a 1364 (void)drive->hwif->INB(IDE_ERROR_REG);
1da177e4
LT
1365 pc = idetape_next_pc_storage(drive);
1366 rq = idetape_next_rq_storage(drive);
1367 idetape_create_request_sense_cmd(pc);
1368 set_bit(IDETAPE_IGNORE_DSC, &tape->flags);
1369 idetape_queue_pc_head(drive, pc, rq);
1370 return ide_stopped;
1371}
1372
1373/*
1374 * idetape_postpone_request postpones the current request so that
1375 * ide.c will be able to service requests from another device on
1376 * the same hwgroup while we are polling for DSC.
1377 */
1378static void idetape_postpone_request (ide_drive_t *drive)
1379{
1380 idetape_tape_t *tape = drive->driver_data;
1381
1382#if IDETAPE_DEBUG_LOG
1383 if (tape->debug_level >= 4)
1384 printk(KERN_INFO "ide-tape: idetape_postpone_request\n");
1385#endif
1386 tape->postponed_rq = HWGROUP(drive)->rq;
1387 ide_stall_queue(drive, tape->dsc_polling_frequency);
1388}
1389
1390/*
1391 * idetape_pc_intr is the usual interrupt handler which will be called
1392 * during a packet command. We will transfer some of the data (as
1393 * requested by the drive) and will re-point interrupt handler to us.
1394 * When data transfer is finished, we will act according to the
1395 * algorithm described before idetape_issue_packet_command.
1396 *
1397 */
1398static ide_startstop_t idetape_pc_intr (ide_drive_t *drive)
1399{
1400 ide_hwif_t *hwif = drive->hwif;
1401 idetape_tape_t *tape = drive->driver_data;
1da177e4 1402 idetape_pc_t *pc = tape->pc;
1da177e4
LT
1403 unsigned int temp;
1404#if SIMULATE_ERRORS
1405 static int error_sim_count = 0;
1406#endif
790d1239 1407 u16 bcount;
8e7657ae 1408 u8 stat, ireason;
1da177e4
LT
1409
1410#if IDETAPE_DEBUG_LOG
1411 if (tape->debug_level >= 4)
1412 printk(KERN_INFO "ide-tape: Reached idetape_pc_intr "
1413 "interrupt handler\n");
1414#endif /* IDETAPE_DEBUG_LOG */
1415
1416 /* Clear the interrupt */
22c525b9 1417 stat = hwif->INB(IDE_STATUS_REG);
1da177e4
LT
1418
1419 if (test_bit(PC_DMA_IN_PROGRESS, &pc->flags)) {
22c525b9 1420 if (hwif->ide_dma_end(drive) || (stat & ERR_STAT)) {
1da177e4
LT
1421 /*
1422 * A DMA error is sometimes expected. For example,
1423 * if the tape is crossing a filemark during a
1424 * READ command, it will issue an irq and position
1425 * itself before the filemark, so that only a partial
1426 * data transfer will occur (which causes the DMA
1427 * error). In that case, we will later ask the tape
1428 * how much bytes of the original request were
1429 * actually transferred (we can't receive that
1430 * information from the DMA engine on most chipsets).
1431 */
1432
1433 /*
1434 * On the contrary, a DMA error is never expected;
1435 * it usually indicates a hardware error or abort.
1436 * If the tape crosses a filemark during a READ
1437 * command, it will issue an irq and position itself
1438 * after the filemark (not before). Only a partial
1439 * data transfer will occur, but no DMA error.
1440 * (AS, 19 Apr 2001)
1441 */
1442 set_bit(PC_DMA_ERROR, &pc->flags);
1443 } else {
1444 pc->actually_transferred = pc->request_transfer;
1445 idetape_update_buffers(pc);
1446 }
1447#if IDETAPE_DEBUG_LOG
1448 if (tape->debug_level >= 4)
1449 printk(KERN_INFO "ide-tape: DMA finished\n");
1450#endif /* IDETAPE_DEBUG_LOG */
1451 }
1452
1453 /* No more interrupts */
22c525b9 1454 if ((stat & DRQ_STAT) == 0) {
1da177e4
LT
1455#if IDETAPE_DEBUG_LOG
1456 if (tape->debug_level >= 2)
1457 printk(KERN_INFO "ide-tape: Packet command completed, %d bytes transferred\n", pc->actually_transferred);
1458#endif /* IDETAPE_DEBUG_LOG */
1459 clear_bit(PC_DMA_IN_PROGRESS, &pc->flags);
1460
1461 local_irq_enable();
1462
1463#if SIMULATE_ERRORS
1464 if ((pc->c[0] == IDETAPE_WRITE_CMD ||
1465 pc->c[0] == IDETAPE_READ_CMD) &&
1466 (++error_sim_count % 100) == 0) {
1467 printk(KERN_INFO "ide-tape: %s: simulating error\n",
1468 tape->name);
22c525b9 1469 stat |= ERR_STAT;
1da177e4
LT
1470 }
1471#endif
22c525b9
BZ
1472 if ((stat & ERR_STAT) && pc->c[0] == IDETAPE_REQUEST_SENSE_CMD)
1473 stat &= ~ERR_STAT;
1474 if ((stat & ERR_STAT) || test_bit(PC_DMA_ERROR, &pc->flags)) {
1475 /* Error detected */
1da177e4
LT
1476#if IDETAPE_DEBUG_LOG
1477 if (tape->debug_level >= 1)
1478 printk(KERN_INFO "ide-tape: %s: I/O error\n",
1479 tape->name);
1480#endif /* IDETAPE_DEBUG_LOG */
1481 if (pc->c[0] == IDETAPE_REQUEST_SENSE_CMD) {
1482 printk(KERN_ERR "ide-tape: I/O error in request sense command\n");
1483 return ide_do_reset(drive);
1484 }
1485#if IDETAPE_DEBUG_LOG
1486 if (tape->debug_level >= 1)
1487 printk(KERN_INFO "ide-tape: [cmd %x]: check condition\n", pc->c[0]);
1488#endif
1489 /* Retry operation */
1490 return idetape_retry_pc(drive);
1491 }
1492 pc->error = 0;
1493 if (test_bit(PC_WAIT_FOR_DSC, &pc->flags) &&
22c525b9 1494 (stat & SEEK_STAT) == 0) {
1da177e4
LT
1495 /* Media access command */
1496 tape->dsc_polling_start = jiffies;
1497 tape->dsc_polling_frequency = IDETAPE_DSC_MA_FAST;
1498 tape->dsc_timeout = jiffies + IDETAPE_DSC_MA_TIMEOUT;
1499 /* Allow ide.c to handle other requests */
1500 idetape_postpone_request(drive);
1501 return ide_stopped;
1502 }
1503 if (tape->failed_pc == pc)
1504 tape->failed_pc = NULL;
1505 /* Command finished - Call the callback function */
1506 return pc->callback(drive);
1507 }
1508 if (test_and_clear_bit(PC_DMA_IN_PROGRESS, &pc->flags)) {
1509 printk(KERN_ERR "ide-tape: The tape wants to issue more "
1510 "interrupts in DMA mode\n");
1511 printk(KERN_ERR "ide-tape: DMA disabled, reverting to PIO\n");
7469aaf6 1512 ide_dma_off(drive);
1da177e4
LT
1513 return ide_do_reset(drive);
1514 }
1515 /* Get the number of bytes to transfer on this interrupt. */
790d1239
BZ
1516 bcount = (hwif->INB(IDE_BCOUNTH_REG) << 8) |
1517 hwif->INB(IDE_BCOUNTL_REG);
1da177e4 1518
8e7657ae 1519 ireason = hwif->INB(IDE_IREASON_REG);
1da177e4 1520
8e7657ae 1521 if (ireason & CD) {
1da177e4
LT
1522 printk(KERN_ERR "ide-tape: CoD != 0 in idetape_pc_intr\n");
1523 return ide_do_reset(drive);
1524 }
8e7657ae 1525 if (((ireason & IO) == IO) == test_bit(PC_WRITING, &pc->flags)) {
1da177e4
LT
1526 /* Hopefully, we will never get here */
1527 printk(KERN_ERR "ide-tape: We wanted to %s, ",
8e7657ae 1528 (ireason & IO) ? "Write" : "Read");
1da177e4 1529 printk(KERN_ERR "ide-tape: but the tape wants us to %s !\n",
8e7657ae 1530 (ireason & IO) ? "Read" : "Write");
1da177e4
LT
1531 return ide_do_reset(drive);
1532 }
1533 if (!test_bit(PC_WRITING, &pc->flags)) {
1534 /* Reading - Check that we have enough space */
790d1239 1535 temp = pc->actually_transferred + bcount;
1da177e4
LT
1536 if (temp > pc->request_transfer) {
1537 if (temp > pc->buffer_size) {
1538 printk(KERN_ERR "ide-tape: The tape wants to send us more data than expected - discarding data\n");
790d1239 1539 idetape_discard_data(drive, bcount);
1da177e4
LT
1540 ide_set_handler(drive, &idetape_pc_intr, IDETAPE_WAIT_CMD, NULL);
1541 return ide_started;
1542 }
1543#if IDETAPE_DEBUG_LOG
1544 if (tape->debug_level >= 2)
1545 printk(KERN_NOTICE "ide-tape: The tape wants to send us more data than expected - allowing transfer\n");
1546#endif /* IDETAPE_DEBUG_LOG */
1547 }
1548 }
1549 if (test_bit(PC_WRITING, &pc->flags)) {
1550 if (pc->bh != NULL)
790d1239 1551 idetape_output_buffers(drive, pc, bcount);
1da177e4
LT
1552 else
1553 /* Write the current buffer */
790d1239
BZ
1554 hwif->atapi_output_bytes(drive, pc->current_position,
1555 bcount);
1da177e4
LT
1556 } else {
1557 if (pc->bh != NULL)
790d1239 1558 idetape_input_buffers(drive, pc, bcount);
1da177e4
LT
1559 else
1560 /* Read the current buffer */
790d1239
BZ
1561 hwif->atapi_input_bytes(drive, pc->current_position,
1562 bcount);
1da177e4
LT
1563 }
1564 /* Update the current position */
790d1239
BZ
1565 pc->actually_transferred += bcount;
1566 pc->current_position += bcount;
1da177e4
LT
1567#if IDETAPE_DEBUG_LOG
1568 if (tape->debug_level >= 2)
790d1239
BZ
1569 printk(KERN_INFO "ide-tape: [cmd %x] transferred %d bytes "
1570 "on that interrupt\n", pc->c[0], bcount);
1da177e4
LT
1571#endif
1572 /* And set the interrupt handler again */
1573 ide_set_handler(drive, &idetape_pc_intr, IDETAPE_WAIT_CMD, NULL);
1574 return ide_started;
1575}
1576
1577/*
1578 * Packet Command Interface
1579 *
1580 * The current Packet Command is available in tape->pc, and will not
1581 * change until we finish handling it. Each packet command is associated
1582 * with a callback function that will be called when the command is
1583 * finished.
1584 *
1585 * The handling will be done in three stages:
1586 *
1587 * 1. idetape_issue_packet_command will send the packet command to the
1588 * drive, and will set the interrupt handler to idetape_pc_intr.
1589 *
1590 * 2. On each interrupt, idetape_pc_intr will be called. This step
1591 * will be repeated until the device signals us that no more
1592 * interrupts will be issued.
1593 *
1594 * 3. ATAPI Tape media access commands have immediate status with a
1595 * delayed process. In case of a successful initiation of a
1596 * media access packet command, the DSC bit will be set when the
1597 * actual execution of the command is finished.
1598 * Since the tape drive will not issue an interrupt, we have to
1599 * poll for this event. In this case, we define the request as
1600 * "low priority request" by setting rq_status to
1601 * IDETAPE_RQ_POSTPONED, set a timer to poll for DSC and exit
1602 * the driver.
1603 *
1604 * ide.c will then give higher priority to requests which
1605 * originate from the other device, until will change rq_status
1606 * to RQ_ACTIVE.
1607 *
1608 * 4. When the packet command is finished, it will be checked for errors.
1609 *
1610 * 5. In case an error was found, we queue a request sense packet
1611 * command in front of the request queue and retry the operation
1612 * up to IDETAPE_MAX_PC_RETRIES times.
1613 *
1614 * 6. In case no error was found, or we decided to give up and not
1615 * to retry again, the callback function will be called and then
1616 * we will handle the next request.
1617 *
1618 */
1619static ide_startstop_t idetape_transfer_pc(ide_drive_t *drive)
1620{
1621 ide_hwif_t *hwif = drive->hwif;
1622 idetape_tape_t *tape = drive->driver_data;
1623 idetape_pc_t *pc = tape->pc;
1da177e4
LT
1624 int retries = 100;
1625 ide_startstop_t startstop;
8e7657ae 1626 u8 ireason;
1da177e4
LT
1627
1628 if (ide_wait_stat(&startstop,drive,DRQ_STAT,BUSY_STAT,WAIT_READY)) {
1629 printk(KERN_ERR "ide-tape: Strange, packet command initiated yet DRQ isn't asserted\n");
1630 return startstop;
1631 }
8e7657ae
BZ
1632 ireason = hwif->INB(IDE_IREASON_REG);
1633 while (retries-- && ((ireason & CD) == 0 || (ireason & IO))) {
1da177e4
LT
1634 printk(KERN_ERR "ide-tape: (IO,CoD != (0,1) while issuing "
1635 "a packet command, retrying\n");
1636 udelay(100);
8e7657ae 1637 ireason = hwif->INB(IDE_IREASON_REG);
1da177e4
LT
1638 if (retries == 0) {
1639 printk(KERN_ERR "ide-tape: (IO,CoD != (0,1) while "
1640 "issuing a packet command, ignoring\n");
8e7657ae
BZ
1641 ireason |= CD;
1642 ireason &= ~IO;
1da177e4
LT
1643 }
1644 }
8e7657ae 1645 if ((ireason & CD) == 0 || (ireason & IO)) {
1da177e4
LT
1646 printk(KERN_ERR "ide-tape: (IO,CoD) != (0,1) while issuing "
1647 "a packet command\n");
1648 return ide_do_reset(drive);
1649 }
1650 /* Set the interrupt routine */
1651 ide_set_handler(drive, &idetape_pc_intr, IDETAPE_WAIT_CMD, NULL);
1652#ifdef CONFIG_BLK_DEV_IDEDMA
1653 /* Begin DMA, if necessary */
1654 if (test_bit(PC_DMA_IN_PROGRESS, &pc->flags))
1655 hwif->dma_start(drive);
1656#endif
1657 /* Send the actual packet */
1658 HWIF(drive)->atapi_output_bytes(drive, pc->c, 12);
1659 return ide_started;
1660}
1661
1662static ide_startstop_t idetape_issue_packet_command (ide_drive_t *drive, idetape_pc_t *pc)
1663{
1664 ide_hwif_t *hwif = drive->hwif;
1665 idetape_tape_t *tape = drive->driver_data;
1da177e4 1666 int dma_ok = 0;
790d1239 1667 u16 bcount;
1da177e4
LT
1668
1669#if IDETAPE_DEBUG_BUGS
1670 if (tape->pc->c[0] == IDETAPE_REQUEST_SENSE_CMD &&
1671 pc->c[0] == IDETAPE_REQUEST_SENSE_CMD) {
1672 printk(KERN_ERR "ide-tape: possible ide-tape.c bug - "
1673 "Two request sense in serial were issued\n");
1674 }
1675#endif /* IDETAPE_DEBUG_BUGS */
1676
1677 if (tape->failed_pc == NULL && pc->c[0] != IDETAPE_REQUEST_SENSE_CMD)
1678 tape->failed_pc = pc;
1679 /* Set the current packet command */
1680 tape->pc = pc;
1681
1682 if (pc->retries > IDETAPE_MAX_PC_RETRIES ||
1683 test_bit(PC_ABORT, &pc->flags)) {
1684 /*
1685 * We will "abort" retrying a packet command in case
1686 * a legitimate error code was received (crossing a
1687 * filemark, or end of the media, for example).
1688 */
1689 if (!test_bit(PC_ABORT, &pc->flags)) {
1690 if (!(pc->c[0] == IDETAPE_TEST_UNIT_READY_CMD &&
1691 tape->sense_key == 2 && tape->asc == 4 &&
1692 (tape->ascq == 1 || tape->ascq == 8))) {
1693 printk(KERN_ERR "ide-tape: %s: I/O error, "
1694 "pc = %2x, key = %2x, "
1695 "asc = %2x, ascq = %2x\n",
1696 tape->name, pc->c[0],
1697 tape->sense_key, tape->asc,
1698 tape->ascq);
1699 }
1700 /* Giving up */
1701 pc->error = IDETAPE_ERROR_GENERAL;
1702 }
1703 tape->failed_pc = NULL;
1704 return pc->callback(drive);
1705 }
1706#if IDETAPE_DEBUG_LOG
1707 if (tape->debug_level >= 2)
1708 printk(KERN_INFO "ide-tape: Retry number - %d, cmd = %02X\n", pc->retries, pc->c[0]);
1709#endif /* IDETAPE_DEBUG_LOG */
1710
1711 pc->retries++;
1712 /* We haven't transferred any data yet */
1713 pc->actually_transferred = 0;
1714 pc->current_position = pc->buffer;
1715 /* Request to transfer the entire buffer at once */
790d1239 1716 bcount = pc->request_transfer;
1da177e4
LT
1717
1718 if (test_and_clear_bit(PC_DMA_ERROR, &pc->flags)) {
1719 printk(KERN_WARNING "ide-tape: DMA disabled, "
1720 "reverting to PIO\n");
7469aaf6 1721 ide_dma_off(drive);
1da177e4
LT
1722 }
1723 if (test_bit(PC_DMA_RECOMMENDED, &pc->flags) && drive->using_dma)
1724 dma_ok = !hwif->dma_setup(drive);
1725
2fc57388
BZ
1726 ide_pktcmd_tf_load(drive, IDE_TFLAG_NO_SELECT_MASK |
1727 IDE_TFLAG_OUT_DEVICE, bcount, dma_ok);
1728
1da177e4
LT
1729 if (dma_ok) /* Will begin DMA later */
1730 set_bit(PC_DMA_IN_PROGRESS, &pc->flags);
1731 if (test_bit(IDETAPE_DRQ_INTERRUPT, &tape->flags)) {
c1c9dbc8
BZ
1732 ide_execute_command(drive, WIN_PACKETCMD, &idetape_transfer_pc,
1733 IDETAPE_WAIT_CMD, NULL);
1da177e4
LT
1734 return ide_started;
1735 } else {
1736 hwif->OUTB(WIN_PACKETCMD, IDE_COMMAND_REG);
1737 return idetape_transfer_pc(drive);
1738 }
1739}
1740
1741/*
1742 * General packet command callback function.
1743 */
1744static ide_startstop_t idetape_pc_callback (ide_drive_t *drive)
1745{
1746 idetape_tape_t *tape = drive->driver_data;
1747
1748#if IDETAPE_DEBUG_LOG
1749 if (tape->debug_level >= 4)
1750 printk(KERN_INFO "ide-tape: Reached idetape_pc_callback\n");
1751#endif /* IDETAPE_DEBUG_LOG */
1752
1753 idetape_end_request(drive, tape->pc->error ? 0 : 1, 0);
1754 return ide_stopped;
1755}
1756
1757/*
1758 * A mode sense command is used to "sense" tape parameters.
1759 */
1760static void idetape_create_mode_sense_cmd (idetape_pc_t *pc, u8 page_code)
1761{
1762 idetape_init_pc(pc);
1763 pc->c[0] = IDETAPE_MODE_SENSE_CMD;
1764 if (page_code != IDETAPE_BLOCK_DESCRIPTOR)
1765 pc->c[1] = 8; /* DBD = 1 - Don't return block descriptors */
1766 pc->c[2] = page_code;
1767 /*
1768 * Changed pc->c[3] to 0 (255 will at best return unused info).
1769 *
1770 * For SCSI this byte is defined as subpage instead of high byte
1771 * of length and some IDE drives seem to interpret it this way
1772 * and return an error when 255 is used.
1773 */
1774 pc->c[3] = 0;
1775 pc->c[4] = 255; /* (We will just discard data in that case) */
1776 if (page_code == IDETAPE_BLOCK_DESCRIPTOR)
1777 pc->request_transfer = 12;
1778 else if (page_code == IDETAPE_CAPABILITIES_PAGE)
1779 pc->request_transfer = 24;
1780 else
1781 pc->request_transfer = 50;
1782 pc->callback = &idetape_pc_callback;
1783}
1784
1785static void calculate_speeds(ide_drive_t *drive)
1786{
1787 idetape_tape_t *tape = drive->driver_data;
1788 int full = 125, empty = 75;
1789
1790 if (time_after(jiffies, tape->controlled_pipeline_head_time + 120 * HZ)) {
1791 tape->controlled_previous_pipeline_head = tape->controlled_last_pipeline_head;
1792 tape->controlled_previous_head_time = tape->controlled_pipeline_head_time;
1793 tape->controlled_last_pipeline_head = tape->pipeline_head;
1794 tape->controlled_pipeline_head_time = jiffies;
1795 }
1796 if (time_after(jiffies, tape->controlled_pipeline_head_time + 60 * HZ))
1797 tape->controlled_pipeline_head_speed = (tape->pipeline_head - tape->controlled_last_pipeline_head) * 32 * HZ / (jiffies - tape->controlled_pipeline_head_time);
1798 else if (time_after(jiffies, tape->controlled_previous_head_time))
1799 tape->controlled_pipeline_head_speed = (tape->pipeline_head - tape->controlled_previous_pipeline_head) * 32 * HZ / (jiffies - tape->controlled_previous_head_time);
1800
1801 if (tape->nr_pending_stages < tape->max_stages /*- 1 */) {
1802 /* -1 for read mode error recovery */
1803 if (time_after(jiffies, tape->uncontrolled_previous_head_time + 10 * HZ)) {
1804 tape->uncontrolled_pipeline_head_time = jiffies;
1805 tape->uncontrolled_pipeline_head_speed = (tape->pipeline_head - tape->uncontrolled_previous_pipeline_head) * 32 * HZ / (jiffies - tape->uncontrolled_previous_head_time);
1806 }
1807 } else {
1808 tape->uncontrolled_previous_head_time = jiffies;
1809 tape->uncontrolled_previous_pipeline_head = tape->pipeline_head;
1810 if (time_after(jiffies, tape->uncontrolled_pipeline_head_time + 30 * HZ)) {
1811 tape->uncontrolled_pipeline_head_time = jiffies;
1812 }
1813 }
1814 tape->pipeline_head_speed = max(tape->uncontrolled_pipeline_head_speed, tape->controlled_pipeline_head_speed);
1815 if (tape->speed_control == 0) {
1816 tape->max_insert_speed = 5000;
1817 } else if (tape->speed_control == 1) {
1818 if (tape->nr_pending_stages >= tape->max_stages / 2)
1819 tape->max_insert_speed = tape->pipeline_head_speed +
1820 (1100 - tape->pipeline_head_speed) * 2 * (tape->nr_pending_stages - tape->max_stages / 2) / tape->max_stages;
1821 else
1822 tape->max_insert_speed = 500 +
1823 (tape->pipeline_head_speed - 500) * 2 * tape->nr_pending_stages / tape->max_stages;
1824 if (tape->nr_pending_stages >= tape->max_stages * 99 / 100)
1825 tape->max_insert_speed = 5000;
1826 } else if (tape->speed_control == 2) {
1827 tape->max_insert_speed = tape->pipeline_head_speed * empty / 100 +
1828 (tape->pipeline_head_speed * full / 100 - tape->pipeline_head_speed * empty / 100) * tape->nr_pending_stages / tape->max_stages;
1829 } else
1830 tape->max_insert_speed = tape->speed_control;
1831 tape->max_insert_speed = max(tape->max_insert_speed, 500);
1832}
1833
1834static ide_startstop_t idetape_media_access_finished (ide_drive_t *drive)
1835{
1836 idetape_tape_t *tape = drive->driver_data;
1837 idetape_pc_t *pc = tape->pc;
22c525b9 1838 u8 stat;
1da177e4 1839
22c525b9
BZ
1840 stat = drive->hwif->INB(IDE_STATUS_REG);
1841 if (stat & SEEK_STAT) {
1842 if (stat & ERR_STAT) {
1da177e4
LT
1843 /* Error detected */
1844 if (pc->c[0] != IDETAPE_TEST_UNIT_READY_CMD)
1845 printk(KERN_ERR "ide-tape: %s: I/O error, ",
1846 tape->name);
1847 /* Retry operation */
1848 return idetape_retry_pc(drive);
1849 }
1850 pc->error = 0;
1851 if (tape->failed_pc == pc)
1852 tape->failed_pc = NULL;
1853 } else {
1854 pc->error = IDETAPE_ERROR_GENERAL;
1855 tape->failed_pc = NULL;
1856 }
1857 return pc->callback(drive);
1858}
1859
1860static ide_startstop_t idetape_rw_callback (ide_drive_t *drive)
1861{
1862 idetape_tape_t *tape = drive->driver_data;
1863 struct request *rq = HWGROUP(drive)->rq;
1864 int blocks = tape->pc->actually_transferred / tape->tape_block_size;
1865
1866 tape->avg_size += blocks * tape->tape_block_size;
1867 tape->insert_size += blocks * tape->tape_block_size;
1868 if (tape->insert_size > 1024 * 1024)
1869 tape->measure_insert_time = 1;
1870 if (tape->measure_insert_time) {
1871 tape->measure_insert_time = 0;
1872 tape->insert_time = jiffies;
1873 tape->insert_size = 0;
1874 }
1875 if (time_after(jiffies, tape->insert_time))
1876 tape->insert_speed = tape->insert_size / 1024 * HZ / (jiffies - tape->insert_time);
9bae1ff3 1877 if (time_after_eq(jiffies, tape->avg_time + HZ)) {
1da177e4
LT
1878 tape->avg_speed = tape->avg_size * HZ / (jiffies - tape->avg_time) / 1024;
1879 tape->avg_size = 0;
1880 tape->avg_time = jiffies;
1881 }
1882
1883#if IDETAPE_DEBUG_LOG
1884 if (tape->debug_level >= 4)
1885 printk(KERN_INFO "ide-tape: Reached idetape_rw_callback\n");
1886#endif /* IDETAPE_DEBUG_LOG */
1887
1888 tape->first_frame_position += blocks;
1889 rq->current_nr_sectors -= blocks;
1890
1891 if (!tape->pc->error)
1892 idetape_end_request(drive, 1, 0);
1893 else
1894 idetape_end_request(drive, tape->pc->error, 0);
1895 return ide_stopped;
1896}
1897
1898static void idetape_create_read_cmd(idetape_tape_t *tape, idetape_pc_t *pc, unsigned int length, struct idetape_bh *bh)
1899{
1900 idetape_init_pc(pc);
1901 pc->c[0] = IDETAPE_READ_CMD;
1902 put_unaligned(htonl(length), (unsigned int *) &pc->c[1]);
1903 pc->c[1] = 1;
1904 pc->callback = &idetape_rw_callback;
1905 pc->bh = bh;
1906 atomic_set(&bh->b_count, 0);
1907 pc->buffer = NULL;
1908 pc->request_transfer = pc->buffer_size = length * tape->tape_block_size;
1909 if (pc->request_transfer == tape->stage_size)
1910 set_bit(PC_DMA_RECOMMENDED, &pc->flags);
1911}
1912
1913static void idetape_create_read_buffer_cmd(idetape_tape_t *tape, idetape_pc_t *pc, unsigned int length, struct idetape_bh *bh)
1914{
1915 int size = 32768;
1916 struct idetape_bh *p = bh;
1917
1918 idetape_init_pc(pc);
1919 pc->c[0] = IDETAPE_READ_BUFFER_CMD;
1920 pc->c[1] = IDETAPE_RETRIEVE_FAULTY_BLOCK;
1921 pc->c[7] = size >> 8;
1922 pc->c[8] = size & 0xff;
1923 pc->callback = &idetape_pc_callback;
1924 pc->bh = bh;
1925 atomic_set(&bh->b_count, 0);
1926 pc->buffer = NULL;
1927 while (p) {
1928 atomic_set(&p->b_count, 0);
1929 p = p->b_reqnext;
1930 }
1931 pc->request_transfer = pc->buffer_size = size;
1932}
1933
1934static void idetape_create_write_cmd(idetape_tape_t *tape, idetape_pc_t *pc, unsigned int length, struct idetape_bh *bh)
1935{
1936 idetape_init_pc(pc);
1937 pc->c[0] = IDETAPE_WRITE_CMD;
1938 put_unaligned(htonl(length), (unsigned int *) &pc->c[1]);
1939 pc->c[1] = 1;
1940 pc->callback = &idetape_rw_callback;
1941 set_bit(PC_WRITING, &pc->flags);
1942 pc->bh = bh;
1943 pc->b_data = bh->b_data;
1944 pc->b_count = atomic_read(&bh->b_count);
1945 pc->buffer = NULL;
1946 pc->request_transfer = pc->buffer_size = length * tape->tape_block_size;
1947 if (pc->request_transfer == tape->stage_size)
1948 set_bit(PC_DMA_RECOMMENDED, &pc->flags);
1949}
1950
1951/*
1952 * idetape_do_request is our request handling function.
1953 */
1954static ide_startstop_t idetape_do_request(ide_drive_t *drive,
1955 struct request *rq, sector_t block)
1956{
1957 idetape_tape_t *tape = drive->driver_data;
1958 idetape_pc_t *pc = NULL;
1959 struct request *postponed_rq = tape->postponed_rq;
22c525b9 1960 u8 stat;
1da177e4
LT
1961
1962#if IDETAPE_DEBUG_LOG
1da177e4
LT
1963 if (tape->debug_level >= 2)
1964 printk(KERN_INFO "ide-tape: sector: %ld, "
1965 "nr_sectors: %ld, current_nr_sectors: %d\n",
1966 rq->sector, rq->nr_sectors, rq->current_nr_sectors);
1967#endif /* IDETAPE_DEBUG_LOG */
1968
4aff5e23 1969 if (!blk_special_request(rq)) {
1da177e4
LT
1970 /*
1971 * We do not support buffer cache originated requests.
1972 */
1973 printk(KERN_NOTICE "ide-tape: %s: Unsupported request in "
4aff5e23 1974 "request queue (%d)\n", drive->name, rq->cmd_type);
1da177e4
LT
1975 ide_end_request(drive, 0, 0);
1976 return ide_stopped;
1977 }
1978
1979 /*
1980 * Retry a failed packet command
1981 */
1982 if (tape->failed_pc != NULL &&
1983 tape->pc->c[0] == IDETAPE_REQUEST_SENSE_CMD) {
1984 return idetape_issue_packet_command(drive, tape->failed_pc);
1985 }
1986#if IDETAPE_DEBUG_BUGS
1987 if (postponed_rq != NULL)
1988 if (rq != postponed_rq) {
1989 printk(KERN_ERR "ide-tape: ide-tape.c bug - "
1990 "Two DSC requests were queued\n");
1991 idetape_end_request(drive, 0, 0);
1992 return ide_stopped;
1993 }
1994#endif /* IDETAPE_DEBUG_BUGS */
1995
1996 tape->postponed_rq = NULL;
1997
1998 /*
1999 * If the tape is still busy, postpone our request and service
2000 * the other device meanwhile.
2001 */
22c525b9 2002 stat = drive->hwif->INB(IDE_STATUS_REG);
1da177e4
LT
2003
2004 if (!drive->dsc_overlap && !(rq->cmd[0] & REQ_IDETAPE_PC2))
2005 set_bit(IDETAPE_IGNORE_DSC, &tape->flags);
2006
2007 if (drive->post_reset == 1) {
2008 set_bit(IDETAPE_IGNORE_DSC, &tape->flags);
2009 drive->post_reset = 0;
2010 }
2011
2012 if (tape->tape_still_time > 100 && tape->tape_still_time < 200)
2013 tape->measure_insert_time = 1;
2014 if (time_after(jiffies, tape->insert_time))
2015 tape->insert_speed = tape->insert_size / 1024 * HZ / (jiffies - tape->insert_time);
2016 calculate_speeds(drive);
2017 if (!test_and_clear_bit(IDETAPE_IGNORE_DSC, &tape->flags) &&
22c525b9 2018 (stat & SEEK_STAT) == 0) {
1da177e4
LT
2019 if (postponed_rq == NULL) {
2020 tape->dsc_polling_start = jiffies;
2021 tape->dsc_polling_frequency = tape->best_dsc_rw_frequency;
2022 tape->dsc_timeout = jiffies + IDETAPE_DSC_RW_TIMEOUT;
2023 } else if (time_after(jiffies, tape->dsc_timeout)) {
2024 printk(KERN_ERR "ide-tape: %s: DSC timeout\n",
2025 tape->name);
2026 if (rq->cmd[0] & REQ_IDETAPE_PC2) {
2027 idetape_media_access_finished(drive);
2028 return ide_stopped;
2029 } else {
2030 return ide_do_reset(drive);
2031 }
9bae1ff3 2032 } else if (time_after(jiffies, tape->dsc_polling_start + IDETAPE_DSC_MA_THRESHOLD))
1da177e4
LT
2033 tape->dsc_polling_frequency = IDETAPE_DSC_MA_SLOW;
2034 idetape_postpone_request(drive);
2035 return ide_stopped;
2036 }
2037 if (rq->cmd[0] & REQ_IDETAPE_READ) {
2038 tape->buffer_head++;
1da177e4
LT
2039 tape->postpone_cnt = 0;
2040 pc = idetape_next_pc_storage(drive);
2041 idetape_create_read_cmd(tape, pc, rq->current_nr_sectors, (struct idetape_bh *)rq->special);
2042 goto out;
2043 }
2044 if (rq->cmd[0] & REQ_IDETAPE_WRITE) {
2045 tape->buffer_head++;
1da177e4
LT
2046 tape->postpone_cnt = 0;
2047 pc = idetape_next_pc_storage(drive);
2048 idetape_create_write_cmd(tape, pc, rq->current_nr_sectors, (struct idetape_bh *)rq->special);
2049 goto out;
2050 }
2051 if (rq->cmd[0] & REQ_IDETAPE_READ_BUFFER) {
2052 tape->postpone_cnt = 0;
2053 pc = idetape_next_pc_storage(drive);
2054 idetape_create_read_buffer_cmd(tape, pc, rq->current_nr_sectors, (struct idetape_bh *)rq->special);
2055 goto out;
2056 }
2057 if (rq->cmd[0] & REQ_IDETAPE_PC1) {
2058 pc = (idetape_pc_t *) rq->buffer;
2059 rq->cmd[0] &= ~(REQ_IDETAPE_PC1);
2060 rq->cmd[0] |= REQ_IDETAPE_PC2;
2061 goto out;
2062 }
2063 if (rq->cmd[0] & REQ_IDETAPE_PC2) {
2064 idetape_media_access_finished(drive);
2065 return ide_stopped;
2066 }
2067 BUG();
2068out:
2069 return idetape_issue_packet_command(drive, pc);
2070}
2071
2072/*
2073 * Pipeline related functions
2074 */
2075static inline int idetape_pipeline_active (idetape_tape_t *tape)
2076{
2077 int rc1, rc2;
2078
2079 rc1 = test_bit(IDETAPE_PIPELINE_ACTIVE, &tape->flags);
2080 rc2 = (tape->active_data_request != NULL);
2081 return rc1;
2082}
2083
2084/*
2085 * idetape_kmalloc_stage uses __get_free_page to allocate a pipeline
2086 * stage, along with all the necessary small buffers which together make
2087 * a buffer of size tape->stage_size (or a bit more). We attempt to
2088 * combine sequential pages as much as possible.
2089 *
2090 * Returns a pointer to the new allocated stage, or NULL if we
2091 * can't (or don't want to) allocate a stage.
2092 *
2093 * Pipeline stages are optional and are used to increase performance.
2094 * If we can't allocate them, we'll manage without them.
2095 */
2096static idetape_stage_t *__idetape_kmalloc_stage (idetape_tape_t *tape, int full, int clear)
2097{
2098 idetape_stage_t *stage;
2099 struct idetape_bh *prev_bh, *bh;
2100 int pages = tape->pages_per_stage;
2101 char *b_data = NULL;
2102
5cbded58 2103 if ((stage = kmalloc(sizeof (idetape_stage_t),GFP_KERNEL)) == NULL)
1da177e4
LT
2104 return NULL;
2105 stage->next = NULL;
2106
5cbded58 2107 bh = stage->bh = kmalloc(sizeof(struct idetape_bh), GFP_KERNEL);
1da177e4
LT
2108 if (bh == NULL)
2109 goto abort;
2110 bh->b_reqnext = NULL;
2111 if ((bh->b_data = (char *) __get_free_page (GFP_KERNEL)) == NULL)
2112 goto abort;
2113 if (clear)
2114 memset(bh->b_data, 0, PAGE_SIZE);
2115 bh->b_size = PAGE_SIZE;
2116 atomic_set(&bh->b_count, full ? bh->b_size : 0);
2117
2118 while (--pages) {
2119 if ((b_data = (char *) __get_free_page (GFP_KERNEL)) == NULL)
2120 goto abort;
2121 if (clear)
2122 memset(b_data, 0, PAGE_SIZE);
2123 if (bh->b_data == b_data + PAGE_SIZE) {
2124 bh->b_size += PAGE_SIZE;
2125 bh->b_data -= PAGE_SIZE;
2126 if (full)
2127 atomic_add(PAGE_SIZE, &bh->b_count);
2128 continue;
2129 }
2130 if (b_data == bh->b_data + bh->b_size) {
2131 bh->b_size += PAGE_SIZE;
2132 if (full)
2133 atomic_add(PAGE_SIZE, &bh->b_count);
2134 continue;
2135 }
2136 prev_bh = bh;
5cbded58 2137 if ((bh = kmalloc(sizeof(struct idetape_bh), GFP_KERNEL)) == NULL) {
1da177e4
LT
2138 free_page((unsigned long) b_data);
2139 goto abort;
2140 }
2141 bh->b_reqnext = NULL;
2142 bh->b_data = b_data;
2143 bh->b_size = PAGE_SIZE;
2144 atomic_set(&bh->b_count, full ? bh->b_size : 0);
2145 prev_bh->b_reqnext = bh;
2146 }
2147 bh->b_size -= tape->excess_bh_size;
2148 if (full)
2149 atomic_sub(tape->excess_bh_size, &bh->b_count);
2150 return stage;
2151abort:
2152 __idetape_kfree_stage(stage);
2153 return NULL;
2154}
2155
2156static idetape_stage_t *idetape_kmalloc_stage (idetape_tape_t *tape)
2157{
2158 idetape_stage_t *cache_stage = tape->cache_stage;
2159
2160#if IDETAPE_DEBUG_LOG
2161 if (tape->debug_level >= 4)
2162 printk(KERN_INFO "ide-tape: Reached idetape_kmalloc_stage\n");
2163#endif /* IDETAPE_DEBUG_LOG */
2164
2165 if (tape->nr_stages >= tape->max_stages)
2166 return NULL;
2167 if (cache_stage != NULL) {
2168 tape->cache_stage = NULL;
2169 return cache_stage;
2170 }
2171 return __idetape_kmalloc_stage(tape, 0, 0);
2172}
2173
dcd96379 2174static int idetape_copy_stage_from_user (idetape_tape_t *tape, idetape_stage_t *stage, const char __user *buf, int n)
1da177e4
LT
2175{
2176 struct idetape_bh *bh = tape->bh;
2177 int count;
dcd96379 2178 int ret = 0;
1da177e4
LT
2179
2180 while (n) {
2181#if IDETAPE_DEBUG_BUGS
2182 if (bh == NULL) {
2183 printk(KERN_ERR "ide-tape: bh == NULL in "
2184 "idetape_copy_stage_from_user\n");
dcd96379 2185 return 1;
1da177e4
LT
2186 }
2187#endif /* IDETAPE_DEBUG_BUGS */
2188 count = min((unsigned int)(bh->b_size - atomic_read(&bh->b_count)), (unsigned int)n);
dcd96379
DW
2189 if (copy_from_user(bh->b_data + atomic_read(&bh->b_count), buf, count))
2190 ret = 1;
1da177e4
LT
2191 n -= count;
2192 atomic_add(count, &bh->b_count);
2193 buf += count;
2194 if (atomic_read(&bh->b_count) == bh->b_size) {
2195 bh = bh->b_reqnext;
2196 if (bh)
2197 atomic_set(&bh->b_count, 0);
2198 }
2199 }
2200 tape->bh = bh;
dcd96379 2201 return ret;
1da177e4
LT
2202}
2203
dcd96379 2204static int idetape_copy_stage_to_user (idetape_tape_t *tape, char __user *buf, idetape_stage_t *stage, int n)
1da177e4
LT
2205{
2206 struct idetape_bh *bh = tape->bh;
2207 int count;
dcd96379 2208 int ret = 0;
1da177e4
LT
2209
2210 while (n) {
2211#if IDETAPE_DEBUG_BUGS
2212 if (bh == NULL) {
2213 printk(KERN_ERR "ide-tape: bh == NULL in "
2214 "idetape_copy_stage_to_user\n");
dcd96379 2215 return 1;
1da177e4
LT
2216 }
2217#endif /* IDETAPE_DEBUG_BUGS */
2218 count = min(tape->b_count, n);
dcd96379
DW
2219 if (copy_to_user(buf, tape->b_data, count))
2220 ret = 1;
1da177e4
LT
2221 n -= count;
2222 tape->b_data += count;
2223 tape->b_count -= count;
2224 buf += count;
2225 if (!tape->b_count) {
2226 tape->bh = bh = bh->b_reqnext;
2227 if (bh) {
2228 tape->b_data = bh->b_data;
2229 tape->b_count = atomic_read(&bh->b_count);
2230 }
2231 }
2232 }
dcd96379 2233 return ret;
1da177e4
LT
2234}
2235
2236static void idetape_init_merge_stage (idetape_tape_t *tape)
2237{
2238 struct idetape_bh *bh = tape->merge_stage->bh;
2239
2240 tape->bh = bh;
2241 if (tape->chrdev_direction == idetape_direction_write)
2242 atomic_set(&bh->b_count, 0);
2243 else {
2244 tape->b_data = bh->b_data;
2245 tape->b_count = atomic_read(&bh->b_count);
2246 }
2247}
2248
2249static void idetape_switch_buffers (idetape_tape_t *tape, idetape_stage_t *stage)
2250{
2251 struct idetape_bh *tmp;
2252
2253 tmp = stage->bh;
2254 stage->bh = tape->merge_stage->bh;
2255 tape->merge_stage->bh = tmp;
2256 idetape_init_merge_stage(tape);
2257}
2258
2259/*
2260 * idetape_add_stage_tail adds a new stage at the end of the pipeline.
2261 */
2262static void idetape_add_stage_tail (ide_drive_t *drive,idetape_stage_t *stage)
2263{
2264 idetape_tape_t *tape = drive->driver_data;
2265 unsigned long flags;
2266
2267#if IDETAPE_DEBUG_LOG
2268 if (tape->debug_level >= 4)
2269 printk (KERN_INFO "ide-tape: Reached idetape_add_stage_tail\n");
2270#endif /* IDETAPE_DEBUG_LOG */
2271 spin_lock_irqsave(&tape->spinlock, flags);
2272 stage->next = NULL;
2273 if (tape->last_stage != NULL)
2274 tape->last_stage->next=stage;
2275 else
2276 tape->first_stage = tape->next_stage=stage;
2277 tape->last_stage = stage;
2278 if (tape->next_stage == NULL)
2279 tape->next_stage = tape->last_stage;
2280 tape->nr_stages++;
2281 tape->nr_pending_stages++;
2282 spin_unlock_irqrestore(&tape->spinlock, flags);
2283}
2284
2285/*
2286 * idetape_wait_for_request installs a completion in a pending request
2287 * and sleeps until it is serviced.
2288 *
2289 * The caller should ensure that the request will not be serviced
2290 * before we install the completion (usually by disabling interrupts).
2291 */
2292static void idetape_wait_for_request (ide_drive_t *drive, struct request *rq)
2293{
6e9a4738 2294 DECLARE_COMPLETION_ONSTACK(wait);
1da177e4
LT
2295 idetape_tape_t *tape = drive->driver_data;
2296
2297#if IDETAPE_DEBUG_BUGS
4aff5e23 2298 if (rq == NULL || !blk_special_request(rq)) {
1da177e4
LT
2299 printk (KERN_ERR "ide-tape: bug: Trying to sleep on non-valid request\n");
2300 return;
2301 }
2302#endif /* IDETAPE_DEBUG_BUGS */
c00895ab 2303 rq->end_io_data = &wait;
1da177e4
LT
2304 rq->end_io = blk_end_sync_rq;
2305 spin_unlock_irq(&tape->spinlock);
2306 wait_for_completion(&wait);
2307 /* The stage and its struct request have been deallocated */
2308 spin_lock_irq(&tape->spinlock);
2309}
2310
2311static ide_startstop_t idetape_read_position_callback (ide_drive_t *drive)
2312{
2313 idetape_tape_t *tape = drive->driver_data;
2314 idetape_read_position_result_t *result;
2315
2316#if IDETAPE_DEBUG_LOG
2317 if (tape->debug_level >= 4)
2318 printk(KERN_INFO "ide-tape: Reached idetape_read_position_callback\n");
2319#endif /* IDETAPE_DEBUG_LOG */
2320
2321 if (!tape->pc->error) {
2322 result = (idetape_read_position_result_t *) tape->pc->buffer;
2323#if IDETAPE_DEBUG_LOG
2324 if (tape->debug_level >= 2)
2325 printk(KERN_INFO "ide-tape: BOP - %s\n",result->bop ? "Yes":"No");
2326 if (tape->debug_level >= 2)
2327 printk(KERN_INFO "ide-tape: EOP - %s\n",result->eop ? "Yes":"No");
2328#endif /* IDETAPE_DEBUG_LOG */
2329 if (result->bpu) {
2330 printk(KERN_INFO "ide-tape: Block location is unknown to the tape\n");
2331 clear_bit(IDETAPE_ADDRESS_VALID, &tape->flags);
2332 idetape_end_request(drive, 0, 0);
2333 } else {
2334#if IDETAPE_DEBUG_LOG
2335 if (tape->debug_level >= 2)
2336 printk(KERN_INFO "ide-tape: Block Location - %u\n", ntohl(result->first_block));
2337#endif /* IDETAPE_DEBUG_LOG */
2338 tape->partition = result->partition;
2339 tape->first_frame_position = ntohl(result->first_block);
2340 tape->last_frame_position = ntohl(result->last_block);
2341 tape->blocks_in_buffer = result->blocks_in_buffer[2];
2342 set_bit(IDETAPE_ADDRESS_VALID, &tape->flags);
2343 idetape_end_request(drive, 1, 0);
2344 }
2345 } else {
2346 idetape_end_request(drive, 0, 0);
2347 }
2348 return ide_stopped;
2349}
2350
2351/*
2352 * idetape_create_write_filemark_cmd will:
2353 *
2354 * 1. Write a filemark if write_filemark=1.
2355 * 2. Flush the device buffers without writing a filemark
2356 * if write_filemark=0.
2357 *
2358 */
2359static void idetape_create_write_filemark_cmd (ide_drive_t *drive, idetape_pc_t *pc,int write_filemark)
2360{
2361 idetape_init_pc(pc);
2362 pc->c[0] = IDETAPE_WRITE_FILEMARK_CMD;
2363 pc->c[4] = write_filemark;
2364 set_bit(PC_WAIT_FOR_DSC, &pc->flags);
2365 pc->callback = &idetape_pc_callback;
2366}
2367
2368static void idetape_create_test_unit_ready_cmd(idetape_pc_t *pc)
2369{
2370 idetape_init_pc(pc);
2371 pc->c[0] = IDETAPE_TEST_UNIT_READY_CMD;
2372 pc->callback = &idetape_pc_callback;
2373}
2374
2375/*
2376 * idetape_queue_pc_tail is based on the following functions:
2377 *
2378 * ide_do_drive_cmd from ide.c
2379 * cdrom_queue_request and cdrom_queue_packet_command from ide-cd.c
2380 *
2381 * We add a special packet command request to the tail of the request
2382 * queue, and wait for it to be serviced.
2383 *
2384 * This is not to be called from within the request handling part
2385 * of the driver ! We allocate here data in the stack, and it is valid
2386 * until the request is finished. This is not the case for the bottom
2387 * part of the driver, where we are always leaving the functions to wait
2388 * for an interrupt or a timer event.
2389 *
2390 * From the bottom part of the driver, we should allocate safe memory
2391 * using idetape_next_pc_storage and idetape_next_rq_storage, and add
2392 * the request to the request list without waiting for it to be serviced !
2393 * In that case, we usually use idetape_queue_pc_head.
2394 */
2395static int __idetape_queue_pc_tail (ide_drive_t *drive, idetape_pc_t *pc)
2396{
2397 struct ide_tape_obj *tape = drive->driver_data;
2398 struct request rq;
2399
2400 idetape_init_rq(&rq, REQ_IDETAPE_PC1);
2401 rq.buffer = (char *) pc;
2402 rq.rq_disk = tape->disk;
2403 return ide_do_drive_cmd(drive, &rq, ide_wait);
2404}
2405
2406static void idetape_create_load_unload_cmd (ide_drive_t *drive, idetape_pc_t *pc,int cmd)
2407{
2408 idetape_init_pc(pc);
2409 pc->c[0] = IDETAPE_LOAD_UNLOAD_CMD;
2410 pc->c[4] = cmd;
2411 set_bit(PC_WAIT_FOR_DSC, &pc->flags);
2412 pc->callback = &idetape_pc_callback;
2413}
2414
2415static int idetape_wait_ready(ide_drive_t *drive, unsigned long timeout)
2416{
2417 idetape_tape_t *tape = drive->driver_data;
2418 idetape_pc_t pc;
2419 int load_attempted = 0;
2420
2421 /*
2422 * Wait for the tape to become ready
2423 */
2424 set_bit(IDETAPE_MEDIUM_PRESENT, &tape->flags);
2425 timeout += jiffies;
2426 while (time_before(jiffies, timeout)) {
2427 idetape_create_test_unit_ready_cmd(&pc);
2428 if (!__idetape_queue_pc_tail(drive, &pc))
2429 return 0;
2430 if ((tape->sense_key == 2 && tape->asc == 4 && tape->ascq == 2)
2431 || (tape->asc == 0x3A)) { /* no media */
2432 if (load_attempted)
2433 return -ENOMEDIUM;
2434 idetape_create_load_unload_cmd(drive, &pc, IDETAPE_LU_LOAD_MASK);
2435 __idetape_queue_pc_tail(drive, &pc);
2436 load_attempted = 1;
2437 /* not about to be ready */
2438 } else if (!(tape->sense_key == 2 && tape->asc == 4 &&
2439 (tape->ascq == 1 || tape->ascq == 8)))
2440 return -EIO;
80ce45fd 2441 msleep(100);
1da177e4
LT
2442 }
2443 return -EIO;
2444}
2445
2446static int idetape_queue_pc_tail (ide_drive_t *drive,idetape_pc_t *pc)
2447{
2448 return __idetape_queue_pc_tail(drive, pc);
2449}
2450
2451static int idetape_flush_tape_buffers (ide_drive_t *drive)
2452{
2453 idetape_pc_t pc;
2454 int rc;
2455
2456 idetape_create_write_filemark_cmd(drive, &pc, 0);
2457 if ((rc = idetape_queue_pc_tail(drive, &pc)))
2458 return rc;
2459 idetape_wait_ready(drive, 60 * 5 * HZ);
2460 return 0;
2461}
2462
2463static void idetape_create_read_position_cmd (idetape_pc_t *pc)
2464{
2465 idetape_init_pc(pc);
2466 pc->c[0] = IDETAPE_READ_POSITION_CMD;
2467 pc->request_transfer = 20;
2468 pc->callback = &idetape_read_position_callback;
2469}
2470
2471static int idetape_read_position (ide_drive_t *drive)
2472{
2473 idetape_tape_t *tape = drive->driver_data;
2474 idetape_pc_t pc;
2475 int position;
2476
2477#if IDETAPE_DEBUG_LOG
2478 if (tape->debug_level >= 4)
2479 printk(KERN_INFO "ide-tape: Reached idetape_read_position\n");
2480#endif /* IDETAPE_DEBUG_LOG */
2481
2482 idetape_create_read_position_cmd(&pc);
2483 if (idetape_queue_pc_tail(drive, &pc))
2484 return -1;
2485 position = tape->first_frame_position;
2486 return position;
2487}
2488
2489static void idetape_create_locate_cmd (ide_drive_t *drive, idetape_pc_t *pc, unsigned int block, u8 partition, int skip)
2490{
2491 idetape_init_pc(pc);
2492 pc->c[0] = IDETAPE_LOCATE_CMD;
2493 pc->c[1] = 2;
2494 put_unaligned(htonl(block), (unsigned int *) &pc->c[3]);
2495 pc->c[8] = partition;
2496 set_bit(PC_WAIT_FOR_DSC, &pc->flags);
2497 pc->callback = &idetape_pc_callback;
2498}
2499
2500static int idetape_create_prevent_cmd (ide_drive_t *drive, idetape_pc_t *pc, int prevent)
2501{
2502 idetape_tape_t *tape = drive->driver_data;
2503
2504 if (!tape->capabilities.lock)
2505 return 0;
2506
2507 idetape_init_pc(pc);
2508 pc->c[0] = IDETAPE_PREVENT_CMD;
2509 pc->c[4] = prevent;
2510 pc->callback = &idetape_pc_callback;
2511 return 1;
2512}
2513
2514static int __idetape_discard_read_pipeline (ide_drive_t *drive)
2515{
2516 idetape_tape_t *tape = drive->driver_data;
2517 unsigned long flags;
2518 int cnt;
2519
2520 if (tape->chrdev_direction != idetape_direction_read)
2521 return 0;
2522
2523 /* Remove merge stage. */
2524 cnt = tape->merge_stage_size / tape->tape_block_size;
2525 if (test_and_clear_bit(IDETAPE_FILEMARK, &tape->flags))
2526 ++cnt; /* Filemarks count as 1 sector */
2527 tape->merge_stage_size = 0;
2528 if (tape->merge_stage != NULL) {
2529 __idetape_kfree_stage(tape->merge_stage);
2530 tape->merge_stage = NULL;
2531 }
2532
2533 /* Clear pipeline flags. */
2534 clear_bit(IDETAPE_PIPELINE_ERROR, &tape->flags);
2535 tape->chrdev_direction = idetape_direction_none;
2536
2537 /* Remove pipeline stages. */
2538 if (tape->first_stage == NULL)
2539 return 0;
2540
2541 spin_lock_irqsave(&tape->spinlock, flags);
2542 tape->next_stage = NULL;
2543 if (idetape_pipeline_active(tape))
2544 idetape_wait_for_request(drive, tape->active_data_request);
2545 spin_unlock_irqrestore(&tape->spinlock, flags);
2546
2547 while (tape->first_stage != NULL) {
2548 struct request *rq_ptr = &tape->first_stage->rq;
2549
2550 cnt += rq_ptr->nr_sectors - rq_ptr->current_nr_sectors;
2551 if (rq_ptr->errors == IDETAPE_ERROR_FILEMARK)
2552 ++cnt;
2553 idetape_remove_stage_head(drive);
2554 }
2555 tape->nr_pending_stages = 0;
2556 tape->max_stages = tape->min_pipeline;
2557 return cnt;
2558}
2559
2560/*
2561 * idetape_position_tape positions the tape to the requested block
2562 * using the LOCATE packet command. A READ POSITION command is then
2563 * issued to check where we are positioned.
2564 *
2565 * Like all higher level operations, we queue the commands at the tail
2566 * of the request queue and wait for their completion.
2567 *
2568 */
2569static int idetape_position_tape (ide_drive_t *drive, unsigned int block, u8 partition, int skip)
2570{
2571 idetape_tape_t *tape = drive->driver_data;
2572 int retval;
2573 idetape_pc_t pc;
2574
2575 if (tape->chrdev_direction == idetape_direction_read)
2576 __idetape_discard_read_pipeline(drive);
2577 idetape_wait_ready(drive, 60 * 5 * HZ);
2578 idetape_create_locate_cmd(drive, &pc, block, partition, skip);
2579 retval = idetape_queue_pc_tail(drive, &pc);
2580 if (retval)
2581 return (retval);
2582
2583 idetape_create_read_position_cmd(&pc);
2584 return (idetape_queue_pc_tail(drive, &pc));
2585}
2586
2587static void idetape_discard_read_pipeline (ide_drive_t *drive, int restore_position)
2588{
2589 idetape_tape_t *tape = drive->driver_data;
2590 int cnt;
2591 int seek, position;
2592
2593 cnt = __idetape_discard_read_pipeline(drive);
2594 if (restore_position) {
2595 position = idetape_read_position(drive);
2596 seek = position > cnt ? position - cnt : 0;
2597 if (idetape_position_tape(drive, seek, 0, 0)) {
2598 printk(KERN_INFO "ide-tape: %s: position_tape failed in discard_pipeline()\n", tape->name);
2599 return;
2600 }
2601 }
2602}
2603
2604/*
2605 * idetape_queue_rw_tail generates a read/write request for the block
2606 * device interface and wait for it to be serviced.
2607 */
2608static int idetape_queue_rw_tail(ide_drive_t *drive, int cmd, int blocks, struct idetape_bh *bh)
2609{
2610 idetape_tape_t *tape = drive->driver_data;
2611 struct request rq;
2612
2613#if IDETAPE_DEBUG_LOG
2614 if (tape->debug_level >= 2)
2615 printk(KERN_INFO "ide-tape: idetape_queue_rw_tail: cmd=%d\n",cmd);
2616#endif /* IDETAPE_DEBUG_LOG */
2617#if IDETAPE_DEBUG_BUGS
2618 if (idetape_pipeline_active(tape)) {
2619 printk(KERN_ERR "ide-tape: bug: the pipeline is active in idetape_queue_rw_tail\n");
2620 return (0);
2621 }
2622#endif /* IDETAPE_DEBUG_BUGS */
2623
2624 idetape_init_rq(&rq, cmd);
2625 rq.rq_disk = tape->disk;
2626 rq.special = (void *)bh;
2627 rq.sector = tape->first_frame_position;
2628 rq.nr_sectors = rq.current_nr_sectors = blocks;
2629 (void) ide_do_drive_cmd(drive, &rq, ide_wait);
2630
2631 if ((cmd & (REQ_IDETAPE_READ | REQ_IDETAPE_WRITE)) == 0)
2632 return 0;
2633
2634 if (tape->merge_stage)
2635 idetape_init_merge_stage(tape);
2636 if (rq.errors == IDETAPE_ERROR_GENERAL)
2637 return -EIO;
2638 return (tape->tape_block_size * (blocks-rq.current_nr_sectors));
2639}
2640
2641/*
2642 * idetape_insert_pipeline_into_queue is used to start servicing the
2643 * pipeline stages, starting from tape->next_stage.
2644 */
2645static void idetape_insert_pipeline_into_queue (ide_drive_t *drive)
2646{
2647 idetape_tape_t *tape = drive->driver_data;
2648
2649 if (tape->next_stage == NULL)
2650 return;
2651 if (!idetape_pipeline_active(tape)) {
2652 set_bit(IDETAPE_PIPELINE_ACTIVE, &tape->flags);
2653 idetape_active_next_stage(drive);
2654 (void) ide_do_drive_cmd(drive, tape->active_data_request, ide_end);
2655 }
2656}
2657
2658static void idetape_create_inquiry_cmd (idetape_pc_t *pc)
2659{
2660 idetape_init_pc(pc);
2661 pc->c[0] = IDETAPE_INQUIRY_CMD;
2662 pc->c[4] = pc->request_transfer = 254;
2663 pc->callback = &idetape_pc_callback;
2664}
2665
2666static void idetape_create_rewind_cmd (ide_drive_t *drive, idetape_pc_t *pc)
2667{
2668 idetape_init_pc(pc);
2669 pc->c[0] = IDETAPE_REWIND_CMD;
2670 set_bit(PC_WAIT_FOR_DSC, &pc->flags);
2671 pc->callback = &idetape_pc_callback;
2672}
2673
1da177e4
LT
2674static void idetape_create_erase_cmd (idetape_pc_t *pc)
2675{
2676 idetape_init_pc(pc);
2677 pc->c[0] = IDETAPE_ERASE_CMD;
2678 pc->c[1] = 1;
2679 set_bit(PC_WAIT_FOR_DSC, &pc->flags);
2680 pc->callback = &idetape_pc_callback;
2681}
2682
2683static void idetape_create_space_cmd (idetape_pc_t *pc,int count, u8 cmd)
2684{
2685 idetape_init_pc(pc);
2686 pc->c[0] = IDETAPE_SPACE_CMD;
2687 put_unaligned(htonl(count), (unsigned int *) &pc->c[1]);
2688 pc->c[1] = cmd;
2689 set_bit(PC_WAIT_FOR_DSC, &pc->flags);
2690 pc->callback = &idetape_pc_callback;
2691}
2692
2693static void idetape_wait_first_stage (ide_drive_t *drive)
2694{
2695 idetape_tape_t *tape = drive->driver_data;
2696 unsigned long flags;
2697
2698 if (tape->first_stage == NULL)
2699 return;
2700 spin_lock_irqsave(&tape->spinlock, flags);
2701 if (tape->active_stage == tape->first_stage)
2702 idetape_wait_for_request(drive, tape->active_data_request);
2703 spin_unlock_irqrestore(&tape->spinlock, flags);
2704}
2705
2706/*
2707 * idetape_add_chrdev_write_request tries to add a character device
2708 * originated write request to our pipeline. In case we don't succeed,
2709 * we revert to non-pipelined operation mode for this request.
2710 *
2711 * 1. Try to allocate a new pipeline stage.
2712 * 2. If we can't, wait for more and more requests to be serviced
2713 * and try again each time.
2714 * 3. If we still can't allocate a stage, fallback to
2715 * non-pipelined operation mode for this request.
2716 */
2717static int idetape_add_chrdev_write_request (ide_drive_t *drive, int blocks)
2718{
2719 idetape_tape_t *tape = drive->driver_data;
2720 idetape_stage_t *new_stage;
2721 unsigned long flags;
2722 struct request *rq;
2723
2724#if IDETAPE_DEBUG_LOG
2725 if (tape->debug_level >= 3)
2726 printk(KERN_INFO "ide-tape: Reached idetape_add_chrdev_write_request\n");
2727#endif /* IDETAPE_DEBUG_LOG */
2728
2729 /*
2730 * Attempt to allocate a new stage.
2731 * Pay special attention to possible race conditions.
2732 */
2733 while ((new_stage = idetape_kmalloc_stage(tape)) == NULL) {
2734 spin_lock_irqsave(&tape->spinlock, flags);
2735 if (idetape_pipeline_active(tape)) {
2736 idetape_wait_for_request(drive, tape->active_data_request);
2737 spin_unlock_irqrestore(&tape->spinlock, flags);
2738 } else {
2739 spin_unlock_irqrestore(&tape->spinlock, flags);
2740 idetape_insert_pipeline_into_queue(drive);
2741 if (idetape_pipeline_active(tape))
2742 continue;
2743 /*
2744 * Linux is short on memory. Fallback to
2745 * non-pipelined operation mode for this request.
2746 */
2747 return idetape_queue_rw_tail(drive, REQ_IDETAPE_WRITE, blocks, tape->merge_stage->bh);
2748 }
2749 }
2750 rq = &new_stage->rq;
2751 idetape_init_rq(rq, REQ_IDETAPE_WRITE);
2752 /* Doesn't actually matter - We always assume sequential access */
2753 rq->sector = tape->first_frame_position;
2754 rq->nr_sectors = rq->current_nr_sectors = blocks;
2755
2756 idetape_switch_buffers(tape, new_stage);
2757 idetape_add_stage_tail(drive, new_stage);
2758 tape->pipeline_head++;
1da177e4
LT
2759 calculate_speeds(drive);
2760
2761 /*
2762 * Estimate whether the tape has stopped writing by checking
2763 * if our write pipeline is currently empty. If we are not
2764 * writing anymore, wait for the pipeline to be full enough
2765 * (90%) before starting to service requests, so that we will
2766 * be able to keep up with the higher speeds of the tape.
2767 */
2768 if (!idetape_pipeline_active(tape)) {
2769 if (tape->nr_stages >= tape->max_stages * 9 / 10 ||
2770 tape->nr_stages >= tape->max_stages - tape->uncontrolled_pipeline_head_speed * 3 * 1024 / tape->tape_block_size) {
2771 tape->measure_insert_time = 1;
2772 tape->insert_time = jiffies;
2773 tape->insert_size = 0;
2774 tape->insert_speed = 0;
2775 idetape_insert_pipeline_into_queue(drive);
2776 }
2777 }
2778 if (test_and_clear_bit(IDETAPE_PIPELINE_ERROR, &tape->flags))
2779 /* Return a deferred error */
2780 return -EIO;
2781 return blocks;
2782}
2783
2784/*
2785 * idetape_wait_for_pipeline will wait until all pending pipeline
2786 * requests are serviced. Typically called on device close.
2787 */
2788static void idetape_wait_for_pipeline (ide_drive_t *drive)
2789{
2790 idetape_tape_t *tape = drive->driver_data;
2791 unsigned long flags;
2792
2793 while (tape->next_stage || idetape_pipeline_active(tape)) {
2794 idetape_insert_pipeline_into_queue(drive);
2795 spin_lock_irqsave(&tape->spinlock, flags);
2796 if (idetape_pipeline_active(tape))
2797 idetape_wait_for_request(drive, tape->active_data_request);
2798 spin_unlock_irqrestore(&tape->spinlock, flags);
2799 }
2800}
2801
2802static void idetape_empty_write_pipeline (ide_drive_t *drive)
2803{
2804 idetape_tape_t *tape = drive->driver_data;
2805 int blocks, min;
2806 struct idetape_bh *bh;
2807
2808#if IDETAPE_DEBUG_BUGS
2809 if (tape->chrdev_direction != idetape_direction_write) {
2810 printk(KERN_ERR "ide-tape: bug: Trying to empty write pipeline, but we are not writing.\n");
2811 return;
2812 }
2813 if (tape->merge_stage_size > tape->stage_size) {
2814 printk(KERN_ERR "ide-tape: bug: merge_buffer too big\n");
2815 tape->merge_stage_size = tape->stage_size;
2816 }
2817#endif /* IDETAPE_DEBUG_BUGS */
2818 if (tape->merge_stage_size) {
2819 blocks = tape->merge_stage_size / tape->tape_block_size;
2820 if (tape->merge_stage_size % tape->tape_block_size) {
2821 unsigned int i;
2822
2823 blocks++;
2824 i = tape->tape_block_size - tape->merge_stage_size % tape->tape_block_size;
2825 bh = tape->bh->b_reqnext;
2826 while (bh) {
2827 atomic_set(&bh->b_count, 0);
2828 bh = bh->b_reqnext;
2829 }
2830 bh = tape->bh;
2831 while (i) {
2832 if (bh == NULL) {
2833
2834 printk(KERN_INFO "ide-tape: bug, bh NULL\n");
2835 break;
2836 }
2837 min = min(i, (unsigned int)(bh->b_size - atomic_read(&bh->b_count)));
2838 memset(bh->b_data + atomic_read(&bh->b_count), 0, min);
2839 atomic_add(min, &bh->b_count);
2840 i -= min;
2841 bh = bh->b_reqnext;
2842 }
2843 }
2844 (void) idetape_add_chrdev_write_request(drive, blocks);
2845 tape->merge_stage_size = 0;
2846 }
2847 idetape_wait_for_pipeline(drive);
2848 if (tape->merge_stage != NULL) {
2849 __idetape_kfree_stage(tape->merge_stage);
2850 tape->merge_stage = NULL;
2851 }
2852 clear_bit(IDETAPE_PIPELINE_ERROR, &tape->flags);
2853 tape->chrdev_direction = idetape_direction_none;
2854
2855 /*
2856 * On the next backup, perform the feedback loop again.
2857 * (I don't want to keep sense information between backups,
2858 * as some systems are constantly on, and the system load
2859 * can be totally different on the next backup).
2860 */
2861 tape->max_stages = tape->min_pipeline;
2862#if IDETAPE_DEBUG_BUGS
2863 if (tape->first_stage != NULL ||
2864 tape->next_stage != NULL ||
2865 tape->last_stage != NULL ||
2866 tape->nr_stages != 0) {
2867 printk(KERN_ERR "ide-tape: ide-tape pipeline bug, "
2868 "first_stage %p, next_stage %p, "
2869 "last_stage %p, nr_stages %d\n",
2870 tape->first_stage, tape->next_stage,
2871 tape->last_stage, tape->nr_stages);
2872 }
2873#endif /* IDETAPE_DEBUG_BUGS */
2874}
2875
2876static void idetape_restart_speed_control (ide_drive_t *drive)
2877{
2878 idetape_tape_t *tape = drive->driver_data;
2879
2880 tape->restart_speed_control_req = 0;
2881 tape->pipeline_head = 0;
2882 tape->controlled_last_pipeline_head = tape->uncontrolled_last_pipeline_head = 0;
2883 tape->controlled_previous_pipeline_head = tape->uncontrolled_previous_pipeline_head = 0;
2884 tape->pipeline_head_speed = tape->controlled_pipeline_head_speed = 5000;
2885 tape->uncontrolled_pipeline_head_speed = 0;
2886 tape->controlled_pipeline_head_time = tape->uncontrolled_pipeline_head_time = jiffies;
2887 tape->controlled_previous_head_time = tape->uncontrolled_previous_head_time = jiffies;
2888}
2889
2890static int idetape_initiate_read (ide_drive_t *drive, int max_stages)
2891{
2892 idetape_tape_t *tape = drive->driver_data;
2893 idetape_stage_t *new_stage;
2894 struct request rq;
2895 int bytes_read;
2896 int blocks = tape->capabilities.ctl;
2897
2898 /* Initialize read operation */
2899 if (tape->chrdev_direction != idetape_direction_read) {
2900 if (tape->chrdev_direction == idetape_direction_write) {
2901 idetape_empty_write_pipeline(drive);
2902 idetape_flush_tape_buffers(drive);
2903 }
2904#if IDETAPE_DEBUG_BUGS
2905 if (tape->merge_stage || tape->merge_stage_size) {
2906 printk (KERN_ERR "ide-tape: merge_stage_size should be 0 now\n");
2907 tape->merge_stage_size = 0;
2908 }
2909#endif /* IDETAPE_DEBUG_BUGS */
2910 if ((tape->merge_stage = __idetape_kmalloc_stage(tape, 0, 0)) == NULL)
2911 return -ENOMEM;
2912 tape->chrdev_direction = idetape_direction_read;
2913
2914 /*
2915 * Issue a read 0 command to ensure that DSC handshake
2916 * is switched from completion mode to buffer available
2917 * mode.
2918 * No point in issuing this if DSC overlap isn't supported,
2919 * some drives (Seagate STT3401A) will return an error.
2920 */
2921 if (drive->dsc_overlap) {
2922 bytes_read = idetape_queue_rw_tail(drive, REQ_IDETAPE_READ, 0, tape->merge_stage->bh);
2923 if (bytes_read < 0) {
2924 __idetape_kfree_stage(tape->merge_stage);
2925 tape->merge_stage = NULL;
2926 tape->chrdev_direction = idetape_direction_none;
2927 return bytes_read;
2928 }
2929 }
2930 }
2931 if (tape->restart_speed_control_req)
2932 idetape_restart_speed_control(drive);
2933 idetape_init_rq(&rq, REQ_IDETAPE_READ);
2934 rq.sector = tape->first_frame_position;
2935 rq.nr_sectors = rq.current_nr_sectors = blocks;
2936 if (!test_bit(IDETAPE_PIPELINE_ERROR, &tape->flags) &&
2937 tape->nr_stages < max_stages) {
2938 new_stage = idetape_kmalloc_stage(tape);
2939 while (new_stage != NULL) {
2940 new_stage->rq = rq;
2941 idetape_add_stage_tail(drive, new_stage);
2942 if (tape->nr_stages >= max_stages)
2943 break;
2944 new_stage = idetape_kmalloc_stage(tape);
2945 }
2946 }
2947 if (!idetape_pipeline_active(tape)) {
2948 if (tape->nr_pending_stages >= 3 * max_stages / 4) {
2949 tape->measure_insert_time = 1;
2950 tape->insert_time = jiffies;
2951 tape->insert_size = 0;
2952 tape->insert_speed = 0;
2953 idetape_insert_pipeline_into_queue(drive);
2954 }
2955 }
2956 return 0;
2957}
2958
2959/*
2960 * idetape_add_chrdev_read_request is called from idetape_chrdev_read
2961 * to service a character device read request and add read-ahead
2962 * requests to our pipeline.
2963 */
2964static int idetape_add_chrdev_read_request (ide_drive_t *drive,int blocks)
2965{
2966 idetape_tape_t *tape = drive->driver_data;
2967 unsigned long flags;
2968 struct request *rq_ptr;
2969 int bytes_read;
2970
2971#if IDETAPE_DEBUG_LOG
2972 if (tape->debug_level >= 4)
2973 printk(KERN_INFO "ide-tape: Reached idetape_add_chrdev_read_request, %d blocks\n", blocks);
2974#endif /* IDETAPE_DEBUG_LOG */
2975
2976 /*
2977 * If we are at a filemark, return a read length of 0
2978 */
2979 if (test_bit(IDETAPE_FILEMARK, &tape->flags))
2980 return 0;
2981
2982 /*
2983 * Wait for the next block to be available at the head
2984 * of the pipeline
2985 */
2986 idetape_initiate_read(drive, tape->max_stages);
2987 if (tape->first_stage == NULL) {
2988 if (test_bit(IDETAPE_PIPELINE_ERROR, &tape->flags))
2989 return 0;
2990 return idetape_queue_rw_tail(drive, REQ_IDETAPE_READ, blocks, tape->merge_stage->bh);
2991 }
2992 idetape_wait_first_stage(drive);
2993 rq_ptr = &tape->first_stage->rq;
2994 bytes_read = tape->tape_block_size * (rq_ptr->nr_sectors - rq_ptr->current_nr_sectors);
2995 rq_ptr->nr_sectors = rq_ptr->current_nr_sectors = 0;
2996
2997
2998 if (rq_ptr->errors == IDETAPE_ERROR_EOD)
2999 return 0;
3000 else {
3001 idetape_switch_buffers(tape, tape->first_stage);
3002 if (rq_ptr->errors == IDETAPE_ERROR_FILEMARK)
3003 set_bit(IDETAPE_FILEMARK, &tape->flags);
3004 spin_lock_irqsave(&tape->spinlock, flags);
3005 idetape_remove_stage_head(drive);
3006 spin_unlock_irqrestore(&tape->spinlock, flags);
3007 tape->pipeline_head++;
1da177e4
LT
3008 calculate_speeds(drive);
3009 }
3010#if IDETAPE_DEBUG_BUGS
3011 if (bytes_read > blocks * tape->tape_block_size) {
3012 printk(KERN_ERR "ide-tape: bug: trying to return more bytes than requested\n");
3013 bytes_read = blocks * tape->tape_block_size;
3014 }
3015#endif /* IDETAPE_DEBUG_BUGS */
3016 return (bytes_read);
3017}
3018
3019static void idetape_pad_zeros (ide_drive_t *drive, int bcount)
3020{
3021 idetape_tape_t *tape = drive->driver_data;
3022 struct idetape_bh *bh;
3023 int blocks;
3024
3025 while (bcount) {
3026 unsigned int count;
3027
3028 bh = tape->merge_stage->bh;
3029 count = min(tape->stage_size, bcount);
3030 bcount -= count;
3031 blocks = count / tape->tape_block_size;
3032 while (count) {
3033 atomic_set(&bh->b_count, min(count, (unsigned int)bh->b_size));
3034 memset(bh->b_data, 0, atomic_read(&bh->b_count));
3035 count -= atomic_read(&bh->b_count);
3036 bh = bh->b_reqnext;
3037 }
3038 idetape_queue_rw_tail(drive, REQ_IDETAPE_WRITE, blocks, tape->merge_stage->bh);
3039 }
3040}
3041
3042static int idetape_pipeline_size (ide_drive_t *drive)
3043{
3044 idetape_tape_t *tape = drive->driver_data;
3045 idetape_stage_t *stage;
3046 struct request *rq;
3047 int size = 0;
3048
3049 idetape_wait_for_pipeline(drive);
3050 stage = tape->first_stage;
3051 while (stage != NULL) {
3052 rq = &stage->rq;
3053 size += tape->tape_block_size * (rq->nr_sectors-rq->current_nr_sectors);
3054 if (rq->errors == IDETAPE_ERROR_FILEMARK)
3055 size += tape->tape_block_size;
3056 stage = stage->next;
3057 }
3058 size += tape->merge_stage_size;
3059 return size;
3060}
3061
3062/*
3063 * Rewinds the tape to the Beginning Of the current Partition (BOP).
3064 *
3065 * We currently support only one partition.
3066 */
3067static int idetape_rewind_tape (ide_drive_t *drive)
3068{
3069 int retval;
3070 idetape_pc_t pc;
3071#if IDETAPE_DEBUG_LOG
3072 idetape_tape_t *tape = drive->driver_data;
3073 if (tape->debug_level >= 2)
3074 printk(KERN_INFO "ide-tape: Reached idetape_rewind_tape\n");
3075#endif /* IDETAPE_DEBUG_LOG */
3076
3077 idetape_create_rewind_cmd(drive, &pc);
3078 retval = idetape_queue_pc_tail(drive, &pc);
3079 if (retval)
3080 return retval;
3081
3082 idetape_create_read_position_cmd(&pc);
3083 retval = idetape_queue_pc_tail(drive, &pc);
3084 if (retval)
3085 return retval;
3086 return 0;
3087}
3088
3089/*
3090 * Our special ide-tape ioctl's.
3091 *
3092 * Currently there aren't any ioctl's.
3093 * mtio.h compatible commands should be issued to the character device
3094 * interface.
3095 */
3096static int idetape_blkdev_ioctl(ide_drive_t *drive, unsigned int cmd, unsigned long arg)
3097{
3098 idetape_tape_t *tape = drive->driver_data;
3099 idetape_config_t config;
3100 void __user *argp = (void __user *)arg;
3101
3102#if IDETAPE_DEBUG_LOG
3103 if (tape->debug_level >= 4)
3104 printk(KERN_INFO "ide-tape: Reached idetape_blkdev_ioctl\n");
3105#endif /* IDETAPE_DEBUG_LOG */
3106 switch (cmd) {
3107 case 0x0340:
3108 if (copy_from_user(&config, argp, sizeof (idetape_config_t)))
3109 return -EFAULT;
3110 tape->best_dsc_rw_frequency = config.dsc_rw_frequency;
3111 tape->max_stages = config.nr_stages;
3112 break;
3113 case 0x0350:
3114 config.dsc_rw_frequency = (int) tape->best_dsc_rw_frequency;
3115 config.nr_stages = tape->max_stages;
3116 if (copy_to_user(argp, &config, sizeof (idetape_config_t)))
3117 return -EFAULT;
3118 break;
3119 default:
3120 return -EIO;
3121 }
3122 return 0;
3123}
3124
3125/*
3126 * idetape_space_over_filemarks is now a bit more complicated than just
3127 * passing the command to the tape since we may have crossed some
3128 * filemarks during our pipelined read-ahead mode.
3129 *
3130 * As a minor side effect, the pipeline enables us to support MTFSFM when
3131 * the filemark is in our internal pipeline even if the tape doesn't
3132 * support spacing over filemarks in the reverse direction.
3133 */
3134static int idetape_space_over_filemarks (ide_drive_t *drive,short mt_op,int mt_count)
3135{
3136 idetape_tape_t *tape = drive->driver_data;
3137 idetape_pc_t pc;
3138 unsigned long flags;
3139 int retval,count=0;
3140
3141 if (mt_count == 0)
3142 return 0;
3143 if (MTBSF == mt_op || MTBSFM == mt_op) {
3144 if (!tape->capabilities.sprev)
3145 return -EIO;
3146 mt_count = - mt_count;
3147 }
3148
3149 if (tape->chrdev_direction == idetape_direction_read) {
3150 /*
3151 * We have a read-ahead buffer. Scan it for crossed
3152 * filemarks.
3153 */
3154 tape->merge_stage_size = 0;
3155 if (test_and_clear_bit(IDETAPE_FILEMARK, &tape->flags))
3156 ++count;
3157 while (tape->first_stage != NULL) {
3158 if (count == mt_count) {
3159 if (mt_op == MTFSFM)
3160 set_bit(IDETAPE_FILEMARK, &tape->flags);
3161 return 0;
3162 }
3163 spin_lock_irqsave(&tape->spinlock, flags);
3164 if (tape->first_stage == tape->active_stage) {
3165 /*
3166 * We have reached the active stage in the read pipeline.
3167 * There is no point in allowing the drive to continue
3168 * reading any farther, so we stop the pipeline.
3169 *
3170 * This section should be moved to a separate subroutine,
3171 * because a similar function is performed in
3172 * __idetape_discard_read_pipeline(), for example.
3173 */
3174 tape->next_stage = NULL;
3175 spin_unlock_irqrestore(&tape->spinlock, flags);
3176 idetape_wait_first_stage(drive);
3177 tape->next_stage = tape->first_stage->next;
3178 } else
3179 spin_unlock_irqrestore(&tape->spinlock, flags);
3180 if (tape->first_stage->rq.errors == IDETAPE_ERROR_FILEMARK)
3181 ++count;
3182 idetape_remove_stage_head(drive);
3183 }
3184 idetape_discard_read_pipeline(drive, 0);
3185 }
3186
3187 /*
3188 * The filemark was not found in our internal pipeline.
3189 * Now we can issue the space command.
3190 */
3191 switch (mt_op) {
3192 case MTFSF:
3193 case MTBSF:
3194 idetape_create_space_cmd(&pc,mt_count-count,IDETAPE_SPACE_OVER_FILEMARK);
3195 return (idetape_queue_pc_tail(drive, &pc));
3196 case MTFSFM:
3197 case MTBSFM:
3198 if (!tape->capabilities.sprev)
3199 return (-EIO);
3200 retval = idetape_space_over_filemarks(drive, MTFSF, mt_count-count);
3201 if (retval) return (retval);
3202 count = (MTBSFM == mt_op ? 1 : -1);
3203 return (idetape_space_over_filemarks(drive, MTFSF, count));
3204 default:
3205 printk(KERN_ERR "ide-tape: MTIO operation %d not supported\n",mt_op);
3206 return (-EIO);
3207 }
3208}
3209
3210
3211/*
3212 * Our character device read / write functions.
3213 *
3214 * The tape is optimized to maximize throughput when it is transferring
3215 * an integral number of the "continuous transfer limit", which is
3216 * a parameter of the specific tape (26 KB on my particular tape).
3217 * (32 kB for Onstream)
3218 *
3219 * As of version 1.3 of the driver, the character device provides an
3220 * abstract continuous view of the media - any mix of block sizes (even 1
3221 * byte) on the same backup/restore procedure is supported. The driver
3222 * will internally convert the requests to the recommended transfer unit,
3223 * so that an unmatch between the user's block size to the recommended
3224 * size will only result in a (slightly) increased driver overhead, but
3225 * will no longer hit performance.
3226 * This is not applicable to Onstream.
3227 */
3228static ssize_t idetape_chrdev_read (struct file *file, char __user *buf,
3229 size_t count, loff_t *ppos)
3230{
3231 struct ide_tape_obj *tape = ide_tape_f(file);
3232 ide_drive_t *drive = tape->drive;
3233 ssize_t bytes_read,temp, actually_read = 0, rc;
dcd96379 3234 ssize_t ret = 0;
1da177e4
LT
3235
3236#if IDETAPE_DEBUG_LOG
3237 if (tape->debug_level >= 3)
3238 printk(KERN_INFO "ide-tape: Reached idetape_chrdev_read, count %Zd\n", count);
3239#endif /* IDETAPE_DEBUG_LOG */
3240
3241 if (tape->chrdev_direction != idetape_direction_read) {
3242 if (test_bit(IDETAPE_DETECT_BS, &tape->flags))
3243 if (count > tape->tape_block_size &&
3244 (count % tape->tape_block_size) == 0)
3245 tape->user_bs_factor = count / tape->tape_block_size;
3246 }
3247 if ((rc = idetape_initiate_read(drive, tape->max_stages)) < 0)
3248 return rc;
3249 if (count == 0)
3250 return (0);
3251 if (tape->merge_stage_size) {
3252 actually_read = min((unsigned int)(tape->merge_stage_size), (unsigned int)count);
dcd96379
DW
3253 if (idetape_copy_stage_to_user(tape, buf, tape->merge_stage, actually_read))
3254 ret = -EFAULT;
1da177e4
LT
3255 buf += actually_read;
3256 tape->merge_stage_size -= actually_read;
3257 count -= actually_read;
3258 }
3259 while (count >= tape->stage_size) {
3260 bytes_read = idetape_add_chrdev_read_request(drive, tape->capabilities.ctl);
3261 if (bytes_read <= 0)
3262 goto finish;
dcd96379
DW
3263 if (idetape_copy_stage_to_user(tape, buf, tape->merge_stage, bytes_read))
3264 ret = -EFAULT;
1da177e4
LT
3265 buf += bytes_read;
3266 count -= bytes_read;
3267 actually_read += bytes_read;
3268 }
3269 if (count) {
3270 bytes_read = idetape_add_chrdev_read_request(drive, tape->capabilities.ctl);
3271 if (bytes_read <= 0)
3272 goto finish;
3273 temp = min((unsigned long)count, (unsigned long)bytes_read);
dcd96379
DW
3274 if (idetape_copy_stage_to_user(tape, buf, tape->merge_stage, temp))
3275 ret = -EFAULT;
1da177e4
LT
3276 actually_read += temp;
3277 tape->merge_stage_size = bytes_read-temp;
3278 }
3279finish:
3280 if (!actually_read && test_bit(IDETAPE_FILEMARK, &tape->flags)) {
3281#if IDETAPE_DEBUG_LOG
3282 if (tape->debug_level >= 2)
3283 printk(KERN_INFO "ide-tape: %s: spacing over filemark\n", tape->name);
3284#endif
3285 idetape_space_over_filemarks(drive, MTFSF, 1);
3286 return 0;
3287 }
dcd96379
DW
3288
3289 return (ret) ? ret : actually_read;
1da177e4
LT
3290}
3291
3292static ssize_t idetape_chrdev_write (struct file *file, const char __user *buf,
3293 size_t count, loff_t *ppos)
3294{
3295 struct ide_tape_obj *tape = ide_tape_f(file);
3296 ide_drive_t *drive = tape->drive;
dcd96379
DW
3297 ssize_t actually_written = 0;
3298 ssize_t ret = 0;
1da177e4
LT
3299
3300 /* The drive is write protected. */
3301 if (tape->write_prot)
3302 return -EACCES;
3303
3304#if IDETAPE_DEBUG_LOG
3305 if (tape->debug_level >= 3)
3306 printk(KERN_INFO "ide-tape: Reached idetape_chrdev_write, "
3307 "count %Zd\n", count);
3308#endif /* IDETAPE_DEBUG_LOG */
3309
3310 /* Initialize write operation */
3311 if (tape->chrdev_direction != idetape_direction_write) {
3312 if (tape->chrdev_direction == idetape_direction_read)
3313 idetape_discard_read_pipeline(drive, 1);
3314#if IDETAPE_DEBUG_BUGS
3315 if (tape->merge_stage || tape->merge_stage_size) {
3316 printk(KERN_ERR "ide-tape: merge_stage_size "
3317 "should be 0 now\n");
3318 tape->merge_stage_size = 0;
3319 }
3320#endif /* IDETAPE_DEBUG_BUGS */
3321 if ((tape->merge_stage = __idetape_kmalloc_stage(tape, 0, 0)) == NULL)
3322 return -ENOMEM;
3323 tape->chrdev_direction = idetape_direction_write;
3324 idetape_init_merge_stage(tape);
3325
3326 /*
3327 * Issue a write 0 command to ensure that DSC handshake
3328 * is switched from completion mode to buffer available
3329 * mode.
3330 * No point in issuing this if DSC overlap isn't supported,
3331 * some drives (Seagate STT3401A) will return an error.
3332 */
3333 if (drive->dsc_overlap) {
dcd96379 3334 ssize_t retval = idetape_queue_rw_tail(drive, REQ_IDETAPE_WRITE, 0, tape->merge_stage->bh);
1da177e4
LT
3335 if (retval < 0) {
3336 __idetape_kfree_stage(tape->merge_stage);
3337 tape->merge_stage = NULL;
3338 tape->chrdev_direction = idetape_direction_none;
3339 return retval;
3340 }
3341 }
3342 }
3343 if (count == 0)
3344 return (0);
3345 if (tape->restart_speed_control_req)
3346 idetape_restart_speed_control(drive);
3347 if (tape->merge_stage_size) {
3348#if IDETAPE_DEBUG_BUGS
3349 if (tape->merge_stage_size >= tape->stage_size) {
3350 printk(KERN_ERR "ide-tape: bug: merge buffer too big\n");
3351 tape->merge_stage_size = 0;
3352 }
3353#endif /* IDETAPE_DEBUG_BUGS */
3354 actually_written = min((unsigned int)(tape->stage_size - tape->merge_stage_size), (unsigned int)count);
dcd96379
DW
3355 if (idetape_copy_stage_from_user(tape, tape->merge_stage, buf, actually_written))
3356 ret = -EFAULT;
1da177e4
LT
3357 buf += actually_written;
3358 tape->merge_stage_size += actually_written;
3359 count -= actually_written;
3360
3361 if (tape->merge_stage_size == tape->stage_size) {
dcd96379 3362 ssize_t retval;
1da177e4
LT
3363 tape->merge_stage_size = 0;
3364 retval = idetape_add_chrdev_write_request(drive, tape->capabilities.ctl);
3365 if (retval <= 0)
3366 return (retval);
3367 }
3368 }
3369 while (count >= tape->stage_size) {
dcd96379
DW
3370 ssize_t retval;
3371 if (idetape_copy_stage_from_user(tape, tape->merge_stage, buf, tape->stage_size))
3372 ret = -EFAULT;
1da177e4
LT
3373 buf += tape->stage_size;
3374 count -= tape->stage_size;
3375 retval = idetape_add_chrdev_write_request(drive, tape->capabilities.ctl);
3376 actually_written += tape->stage_size;
3377 if (retval <= 0)
3378 return (retval);
3379 }
3380 if (count) {
3381 actually_written += count;
dcd96379
DW
3382 if (idetape_copy_stage_from_user(tape, tape->merge_stage, buf, count))
3383 ret = -EFAULT;
1da177e4
LT
3384 tape->merge_stage_size += count;
3385 }
dcd96379 3386 return (ret) ? ret : actually_written;
1da177e4
LT
3387}
3388
3389static int idetape_write_filemark (ide_drive_t *drive)
3390{
3391 idetape_pc_t pc;
3392
3393 /* Write a filemark */
3394 idetape_create_write_filemark_cmd(drive, &pc, 1);
3395 if (idetape_queue_pc_tail(drive, &pc)) {
3396 printk(KERN_ERR "ide-tape: Couldn't write a filemark\n");
3397 return -EIO;
3398 }
3399 return 0;
3400}
3401
3402/*
3403 * idetape_mtioctop is called from idetape_chrdev_ioctl when
3404 * the general mtio MTIOCTOP ioctl is requested.
3405 *
3406 * We currently support the following mtio.h operations:
3407 *
3408 * MTFSF - Space over mt_count filemarks in the positive direction.
3409 * The tape is positioned after the last spaced filemark.
3410 *
3411 * MTFSFM - Same as MTFSF, but the tape is positioned before the
3412 * last filemark.
3413 *
3414 * MTBSF - Steps background over mt_count filemarks, tape is
3415 * positioned before the last filemark.
3416 *
3417 * MTBSFM - Like MTBSF, only tape is positioned after the last filemark.
3418 *
3419 * Note:
3420 *
3421 * MTBSF and MTBSFM are not supported when the tape doesn't
3422 * support spacing over filemarks in the reverse direction.
3423 * In this case, MTFSFM is also usually not supported (it is
3424 * supported in the rare case in which we crossed the filemark
3425 * during our read-ahead pipelined operation mode).
3426 *
3427 * MTWEOF - Writes mt_count filemarks. Tape is positioned after
3428 * the last written filemark.
3429 *
3430 * MTREW - Rewinds tape.
3431 *
3432 * MTLOAD - Loads the tape.
3433 *
3434 * MTOFFL - Puts the tape drive "Offline": Rewinds the tape and
3435 * MTUNLOAD prevents further access until the media is replaced.
3436 *
3437 * MTNOP - Flushes tape buffers.
3438 *
3439 * MTRETEN - Retension media. This typically consists of one end
3440 * to end pass on the media.
3441 *
3442 * MTEOM - Moves to the end of recorded data.
3443 *
3444 * MTERASE - Erases tape.
3445 *
3446 * MTSETBLK - Sets the user block size to mt_count bytes. If
3447 * mt_count is 0, we will attempt to autodetect
3448 * the block size.
3449 *
3450 * MTSEEK - Positions the tape in a specific block number, where
3451 * each block is assumed to contain which user_block_size
3452 * bytes.
3453 *
3454 * MTSETPART - Switches to another tape partition.
3455 *
3456 * MTLOCK - Locks the tape door.
3457 *
3458 * MTUNLOCK - Unlocks the tape door.
3459 *
3460 * The following commands are currently not supported:
3461 *
3462 * MTFSS, MTBSS, MTWSM, MTSETDENSITY,
3463 * MTSETDRVBUFFER, MT_ST_BOOLEANS, MT_ST_WRITE_THRESHOLD.
3464 */
3465static int idetape_mtioctop (ide_drive_t *drive,short mt_op,int mt_count)
3466{
3467 idetape_tape_t *tape = drive->driver_data;
3468 idetape_pc_t pc;
3469 int i,retval;
3470
3471#if IDETAPE_DEBUG_LOG
3472 if (tape->debug_level >= 1)
3473 printk(KERN_INFO "ide-tape: Handling MTIOCTOP ioctl: "
3474 "mt_op=%d, mt_count=%d\n", mt_op, mt_count);
3475#endif /* IDETAPE_DEBUG_LOG */
3476 /*
3477 * Commands which need our pipelined read-ahead stages.
3478 */
3479 switch (mt_op) {
3480 case MTFSF:
3481 case MTFSFM:
3482 case MTBSF:
3483 case MTBSFM:
3484 if (!mt_count)
3485 return (0);
3486 return (idetape_space_over_filemarks(drive,mt_op,mt_count));
3487 default:
3488 break;
3489 }
3490 switch (mt_op) {
3491 case MTWEOF:
3492 if (tape->write_prot)
3493 return -EACCES;
3494 idetape_discard_read_pipeline(drive, 1);
3495 for (i = 0; i < mt_count; i++) {
3496 retval = idetape_write_filemark(drive);
3497 if (retval)
3498 return retval;
3499 }
3500 return (0);
3501 case MTREW:
3502 idetape_discard_read_pipeline(drive, 0);
3503 if (idetape_rewind_tape(drive))
3504 return -EIO;
3505 return 0;
3506 case MTLOAD:
3507 idetape_discard_read_pipeline(drive, 0);
3508 idetape_create_load_unload_cmd(drive, &pc, IDETAPE_LU_LOAD_MASK);
3509 return (idetape_queue_pc_tail(drive, &pc));
3510 case MTUNLOAD:
3511 case MTOFFL:
3512 /*
3513 * If door is locked, attempt to unlock before
3514 * attempting to eject.
3515 */
3516 if (tape->door_locked) {
3517 if (idetape_create_prevent_cmd(drive, &pc, 0))
3518 if (!idetape_queue_pc_tail(drive, &pc))
3519 tape->door_locked = DOOR_UNLOCKED;
3520 }
3521 idetape_discard_read_pipeline(drive, 0);
3522 idetape_create_load_unload_cmd(drive, &pc,!IDETAPE_LU_LOAD_MASK);
3523 retval = idetape_queue_pc_tail(drive, &pc);
3524 if (!retval)
3525 clear_bit(IDETAPE_MEDIUM_PRESENT, &tape->flags);
3526 return retval;
3527 case MTNOP:
3528 idetape_discard_read_pipeline(drive, 0);
3529 return (idetape_flush_tape_buffers(drive));
3530 case MTRETEN:
3531 idetape_discard_read_pipeline(drive, 0);
3532 idetape_create_load_unload_cmd(drive, &pc,IDETAPE_LU_RETENSION_MASK | IDETAPE_LU_LOAD_MASK);
3533 return (idetape_queue_pc_tail(drive, &pc));
3534 case MTEOM:
3535 idetape_create_space_cmd(&pc, 0, IDETAPE_SPACE_TO_EOD);
3536 return (idetape_queue_pc_tail(drive, &pc));
3537 case MTERASE:
3538 (void) idetape_rewind_tape(drive);
3539 idetape_create_erase_cmd(&pc);
3540 return (idetape_queue_pc_tail(drive, &pc));
3541 case MTSETBLK:
3542 if (mt_count) {
3543 if (mt_count < tape->tape_block_size || mt_count % tape->tape_block_size)
3544 return -EIO;
3545 tape->user_bs_factor = mt_count / tape->tape_block_size;
3546 clear_bit(IDETAPE_DETECT_BS, &tape->flags);
3547 } else
3548 set_bit(IDETAPE_DETECT_BS, &tape->flags);
3549 return 0;
3550 case MTSEEK:
3551 idetape_discard_read_pipeline(drive, 0);
3552 return idetape_position_tape(drive, mt_count * tape->user_bs_factor, tape->partition, 0);
3553 case MTSETPART:
3554 idetape_discard_read_pipeline(drive, 0);
3555 return (idetape_position_tape(drive, 0, mt_count, 0));
3556 case MTFSR:
3557 case MTBSR:
3558 case MTLOCK:
3559 if (!idetape_create_prevent_cmd(drive, &pc, 1))
3560 return 0;
3561 retval = idetape_queue_pc_tail(drive, &pc);
3562 if (retval) return retval;
3563 tape->door_locked = DOOR_EXPLICITLY_LOCKED;
3564 return 0;
3565 case MTUNLOCK:
3566 if (!idetape_create_prevent_cmd(drive, &pc, 0))
3567 return 0;
3568 retval = idetape_queue_pc_tail(drive, &pc);
3569 if (retval) return retval;
3570 tape->door_locked = DOOR_UNLOCKED;
3571 return 0;
3572 default:
3573 printk(KERN_ERR "ide-tape: MTIO operation %d not "
3574 "supported\n", mt_op);
3575 return (-EIO);
3576 }
3577}
3578
3579/*
3580 * Our character device ioctls.
3581 *
3582 * General mtio.h magnetic io commands are supported here, and not in
3583 * the corresponding block interface.
3584 *
3585 * The following ioctls are supported:
3586 *
3587 * MTIOCTOP - Refer to idetape_mtioctop for detailed description.
3588 *
3589 * MTIOCGET - The mt_dsreg field in the returned mtget structure
3590 * will be set to (user block size in bytes <<
3591 * MT_ST_BLKSIZE_SHIFT) & MT_ST_BLKSIZE_MASK.
3592 *
3593 * The mt_blkno is set to the current user block number.
3594 * The other mtget fields are not supported.
3595 *
3596 * MTIOCPOS - The current tape "block position" is returned. We
3597 * assume that each block contains user_block_size
3598 * bytes.
3599 *
3600 * Our own ide-tape ioctls are supported on both interfaces.
3601 */
3602static int idetape_chrdev_ioctl (struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg)
3603{
3604 struct ide_tape_obj *tape = ide_tape_f(file);
3605 ide_drive_t *drive = tape->drive;
3606 struct mtop mtop;
3607 struct mtget mtget;
3608 struct mtpos mtpos;
3609 int block_offset = 0, position = tape->first_frame_position;
3610 void __user *argp = (void __user *)arg;
3611
3612#if IDETAPE_DEBUG_LOG
3613 if (tape->debug_level >= 3)
3614 printk(KERN_INFO "ide-tape: Reached idetape_chrdev_ioctl, "
3615 "cmd=%u\n", cmd);
3616#endif /* IDETAPE_DEBUG_LOG */
3617
3618 tape->restart_speed_control_req = 1;
3619 if (tape->chrdev_direction == idetape_direction_write) {
3620 idetape_empty_write_pipeline(drive);
3621 idetape_flush_tape_buffers(drive);
3622 }
3623 if (cmd == MTIOCGET || cmd == MTIOCPOS) {
3624 block_offset = idetape_pipeline_size(drive) / (tape->tape_block_size * tape->user_bs_factor);
3625 if ((position = idetape_read_position(drive)) < 0)
3626 return -EIO;
3627 }
3628 switch (cmd) {
3629 case MTIOCTOP:
3630 if (copy_from_user(&mtop, argp, sizeof (struct mtop)))
3631 return -EFAULT;
3632 return (idetape_mtioctop(drive,mtop.mt_op,mtop.mt_count));
3633 case MTIOCGET:
3634 memset(&mtget, 0, sizeof (struct mtget));
3635 mtget.mt_type = MT_ISSCSI2;
3636 mtget.mt_blkno = position / tape->user_bs_factor - block_offset;
3637 mtget.mt_dsreg = ((tape->tape_block_size * tape->user_bs_factor) << MT_ST_BLKSIZE_SHIFT) & MT_ST_BLKSIZE_MASK;
3638 if (tape->drv_write_prot) {
3639 mtget.mt_gstat |= GMT_WR_PROT(0xffffffff);
3640 }
3641 if (copy_to_user(argp, &mtget, sizeof(struct mtget)))
3642 return -EFAULT;
3643 return 0;
3644 case MTIOCPOS:
3645 mtpos.mt_blkno = position / tape->user_bs_factor - block_offset;
3646 if (copy_to_user(argp, &mtpos, sizeof(struct mtpos)))
3647 return -EFAULT;
3648 return 0;
3649 default:
3650 if (tape->chrdev_direction == idetape_direction_read)
3651 idetape_discard_read_pipeline(drive, 1);
3652 return idetape_blkdev_ioctl(drive, cmd, arg);
3653 }
3654}
3655
3656static void idetape_get_blocksize_from_block_descriptor(ide_drive_t *drive);
3657
3658/*
3659 * Our character device open function.
3660 */
3661static int idetape_chrdev_open (struct inode *inode, struct file *filp)
3662{
3663 unsigned int minor = iminor(inode), i = minor & ~0xc0;
3664 ide_drive_t *drive;
3665 idetape_tape_t *tape;
3666 idetape_pc_t pc;
3667 int retval;
3668
3669 /*
3670 * We really want to do nonseekable_open(inode, filp); here, but some
3671 * versions of tar incorrectly call lseek on tapes and bail out if that
3672 * fails. So we disallow pread() and pwrite(), but permit lseeks.
3673 */
3674 filp->f_mode &= ~(FMODE_PREAD | FMODE_PWRITE);
3675
3676#if IDETAPE_DEBUG_LOG
3677 printk(KERN_INFO "ide-tape: Reached idetape_chrdev_open\n");
3678#endif /* IDETAPE_DEBUG_LOG */
3679
3680 if (i >= MAX_HWIFS * MAX_DRIVES)
3681 return -ENXIO;
3682
3683 if (!(tape = ide_tape_chrdev_get(i)))
3684 return -ENXIO;
3685
3686 drive = tape->drive;
3687
3688 filp->private_data = tape;
3689
3690 if (test_and_set_bit(IDETAPE_BUSY, &tape->flags)) {
3691 retval = -EBUSY;
3692 goto out_put_tape;
3693 }
3694
3695 retval = idetape_wait_ready(drive, 60 * HZ);
3696 if (retval) {
3697 clear_bit(IDETAPE_BUSY, &tape->flags);
3698 printk(KERN_ERR "ide-tape: %s: drive not ready\n", tape->name);
3699 goto out_put_tape;
3700 }
3701
3702 idetape_read_position(drive);
3703 if (!test_bit(IDETAPE_ADDRESS_VALID, &tape->flags))
3704 (void)idetape_rewind_tape(drive);
3705
3706 if (tape->chrdev_direction != idetape_direction_read)
3707 clear_bit(IDETAPE_PIPELINE_ERROR, &tape->flags);
3708
3709 /* Read block size and write protect status from drive. */
3710 idetape_get_blocksize_from_block_descriptor(drive);
3711
3712 /* Set write protect flag if device is opened as read-only. */
3713 if ((filp->f_flags & O_ACCMODE) == O_RDONLY)
3714 tape->write_prot = 1;
3715 else
3716 tape->write_prot = tape->drv_write_prot;
3717
3718 /* Make sure drive isn't write protected if user wants to write. */
3719 if (tape->write_prot) {
3720 if ((filp->f_flags & O_ACCMODE) == O_WRONLY ||
3721 (filp->f_flags & O_ACCMODE) == O_RDWR) {
3722 clear_bit(IDETAPE_BUSY, &tape->flags);
3723 retval = -EROFS;
3724 goto out_put_tape;
3725 }
3726 }
3727
3728 /*
3729 * Lock the tape drive door so user can't eject.
3730 */
3731 if (tape->chrdev_direction == idetape_direction_none) {
3732 if (idetape_create_prevent_cmd(drive, &pc, 1)) {
3733 if (!idetape_queue_pc_tail(drive, &pc)) {
3734 if (tape->door_locked != DOOR_EXPLICITLY_LOCKED)
3735 tape->door_locked = DOOR_LOCKED;
3736 }
3737 }
3738 }
3739 idetape_restart_speed_control(drive);
3740 tape->restart_speed_control_req = 0;
3741 return 0;
3742
3743out_put_tape:
3744 ide_tape_put(tape);
3745 return retval;
3746}
3747
3748static void idetape_write_release (ide_drive_t *drive, unsigned int minor)
3749{
3750 idetape_tape_t *tape = drive->driver_data;
3751
3752 idetape_empty_write_pipeline(drive);
3753 tape->merge_stage = __idetape_kmalloc_stage(tape, 1, 0);
3754 if (tape->merge_stage != NULL) {
3755 idetape_pad_zeros(drive, tape->tape_block_size * (tape->user_bs_factor - 1));
3756 __idetape_kfree_stage(tape->merge_stage);
3757 tape->merge_stage = NULL;
3758 }
3759 idetape_write_filemark(drive);
3760 idetape_flush_tape_buffers(drive);
3761 idetape_flush_tape_buffers(drive);
3762}
3763
3764/*
3765 * Our character device release function.
3766 */
3767static int idetape_chrdev_release (struct inode *inode, struct file *filp)
3768{
3769 struct ide_tape_obj *tape = ide_tape_f(filp);
3770 ide_drive_t *drive = tape->drive;
3771 idetape_pc_t pc;
3772 unsigned int minor = iminor(inode);
3773
3774 lock_kernel();
3775 tape = drive->driver_data;
3776#if IDETAPE_DEBUG_LOG
3777 if (tape->debug_level >= 3)
3778 printk(KERN_INFO "ide-tape: Reached idetape_chrdev_release\n");
3779#endif /* IDETAPE_DEBUG_LOG */
3780
3781 if (tape->chrdev_direction == idetape_direction_write)
3782 idetape_write_release(drive, minor);
3783 if (tape->chrdev_direction == idetape_direction_read) {
3784 if (minor < 128)
3785 idetape_discard_read_pipeline(drive, 1);
3786 else
3787 idetape_wait_for_pipeline(drive);
3788 }
3789 if (tape->cache_stage != NULL) {
3790 __idetape_kfree_stage(tape->cache_stage);
3791 tape->cache_stage = NULL;
3792 }
3793 if (minor < 128 && test_bit(IDETAPE_MEDIUM_PRESENT, &tape->flags))
3794 (void) idetape_rewind_tape(drive);
3795 if (tape->chrdev_direction == idetape_direction_none) {
3796 if (tape->door_locked == DOOR_LOCKED) {
3797 if (idetape_create_prevent_cmd(drive, &pc, 0)) {
3798 if (!idetape_queue_pc_tail(drive, &pc))
3799 tape->door_locked = DOOR_UNLOCKED;
3800 }
3801 }
3802 }
3803 clear_bit(IDETAPE_BUSY, &tape->flags);
3804 ide_tape_put(tape);
3805 unlock_kernel();
3806 return 0;
3807}
3808
3809/*
3810 * idetape_identify_device is called to check the contents of the
3811 * ATAPI IDENTIFY command results. We return:
3812 *
3813 * 1 If the tape can be supported by us, based on the information
3814 * we have so far.
3815 *
3816 * 0 If this tape driver is not currently supported by us.
3817 */
3818static int idetape_identify_device (ide_drive_t *drive)
3819{
3820 struct idetape_id_gcw gcw;
3821 struct hd_driveid *id = drive->id;
1da177e4
LT
3822
3823 if (drive->id_read == 0)
3824 return 1;
3825
3826 *((unsigned short *) &gcw) = id->config;
3827
3828#if IDETAPE_DEBUG_INFO
3829 printk(KERN_INFO "ide-tape: Dumping ATAPI Identify Device tape parameters\n");
3830 printk(KERN_INFO "ide-tape: Protocol Type: ");
3831 switch (gcw.protocol) {
3832 case 0: case 1: printk("ATA\n");break;
3833 case 2: printk("ATAPI\n");break;
3834 case 3: printk("Reserved (Unknown to ide-tape)\n");break;
3835 }
3836 printk(KERN_INFO "ide-tape: Device Type: %x - ",gcw.device_type);
3837 switch (gcw.device_type) {
3838 case 0: printk("Direct-access Device\n");break;
3839 case 1: printk("Streaming Tape Device\n");break;
3840 case 2: case 3: case 4: printk("Reserved\n");break;
3841 case 5: printk("CD-ROM Device\n");break;
3842 case 6: printk("Reserved\n");
3843 case 7: printk("Optical memory Device\n");break;
3844 case 0x1f: printk("Unknown or no Device type\n");break;
3845 default: printk("Reserved\n");
3846 }
3847 printk(KERN_INFO "ide-tape: Removable: %s",gcw.removable ? "Yes\n":"No\n");
3848 printk(KERN_INFO "ide-tape: Command Packet DRQ Type: ");
3849 switch (gcw.drq_type) {
3850 case 0: printk("Microprocessor DRQ\n");break;
3851 case 1: printk("Interrupt DRQ\n");break;
3852 case 2: printk("Accelerated DRQ\n");break;
3853 case 3: printk("Reserved\n");break;
3854 }
3855 printk(KERN_INFO "ide-tape: Command Packet Size: ");
3856 switch (gcw.packet_size) {
3857 case 0: printk("12 bytes\n");break;
3858 case 1: printk("16 bytes\n");break;
3859 default: printk("Reserved\n");break;
3860 }
1da177e4
LT
3861#endif /* IDETAPE_DEBUG_INFO */
3862
3863 /* Check that we can support this device */
3864
3865 if (gcw.protocol !=2 )
3866 printk(KERN_ERR "ide-tape: Protocol is not ATAPI\n");
3867 else if (gcw.device_type != 1)
3868 printk(KERN_ERR "ide-tape: Device type is not set to tape\n");
3869 else if (!gcw.removable)
3870 printk(KERN_ERR "ide-tape: The removable flag is not set\n");
3871 else if (gcw.packet_size != 0) {
3872 printk(KERN_ERR "ide-tape: Packet size is not 12 bytes long\n");
3873 if (gcw.packet_size == 1)
3874 printk(KERN_ERR "ide-tape: Sorry, padding to 16 bytes is still not supported\n");
3875 } else
3876 return 1;
3877 return 0;
3878}
3879
3880/*
3881 * Use INQUIRY to get the firmware revision
3882 */
3883static void idetape_get_inquiry_results (ide_drive_t *drive)
3884{
3885 char *r;
3886 idetape_tape_t *tape = drive->driver_data;
3887 idetape_pc_t pc;
3888 idetape_inquiry_result_t *inquiry;
3889
3890 idetape_create_inquiry_cmd(&pc);
3891 if (idetape_queue_pc_tail(drive, &pc)) {
3892 printk(KERN_ERR "ide-tape: %s: can't get INQUIRY results\n", tape->name);
3893 return;
3894 }
3895 inquiry = (idetape_inquiry_result_t *) pc.buffer;
3896 memcpy(tape->vendor_id, inquiry->vendor_id, 8);
3897 memcpy(tape->product_id, inquiry->product_id, 16);
3898 memcpy(tape->firmware_revision, inquiry->revision_level, 4);
3899 ide_fixstring(tape->vendor_id, 10, 0);
3900 ide_fixstring(tape->product_id, 18, 0);
3901 ide_fixstring(tape->firmware_revision, 6, 0);
3902 r = tape->firmware_revision;
3903 if (*(r + 1) == '.')
3904 tape->firmware_revision_num = (*r - '0') * 100 + (*(r + 2) - '0') * 10 + *(r + 3) - '0';
3905 printk(KERN_INFO "ide-tape: %s <-> %s: %s %s rev %s\n", drive->name, tape->name, tape->vendor_id, tape->product_id, tape->firmware_revision);
3906}
3907
3908/*
3909 * idetape_get_mode_sense_results asks the tape about its various
3910 * parameters. In particular, we will adjust our data transfer buffer
3911 * size to the recommended value as returned by the tape.
3912 */
3913static void idetape_get_mode_sense_results (ide_drive_t *drive)
3914{
3915 idetape_tape_t *tape = drive->driver_data;
3916 idetape_pc_t pc;
3917 idetape_mode_parameter_header_t *header;
3918 idetape_capabilities_page_t *capabilities;
3919
3920 idetape_create_mode_sense_cmd(&pc, IDETAPE_CAPABILITIES_PAGE);
3921 if (idetape_queue_pc_tail(drive, &pc)) {
3922 printk(KERN_ERR "ide-tape: Can't get tape parameters - assuming some default values\n");
3923 tape->tape_block_size = 512;
3924 tape->capabilities.ctl = 52;
3925 tape->capabilities.speed = 450;
3926 tape->capabilities.buffer_size = 6 * 52;
3927 return;
3928 }
3929 header = (idetape_mode_parameter_header_t *) pc.buffer;
3930 capabilities = (idetape_capabilities_page_t *) (pc.buffer + sizeof(idetape_mode_parameter_header_t) + header->bdl);
3931
3932 capabilities->max_speed = ntohs(capabilities->max_speed);
3933 capabilities->ctl = ntohs(capabilities->ctl);
3934 capabilities->speed = ntohs(capabilities->speed);
3935 capabilities->buffer_size = ntohs(capabilities->buffer_size);
3936
3937 if (!capabilities->speed) {
3938 printk(KERN_INFO "ide-tape: %s: overriding capabilities->speed (assuming 650KB/sec)\n", drive->name);
3939 capabilities->speed = 650;
3940 }
3941 if (!capabilities->max_speed) {
3942 printk(KERN_INFO "ide-tape: %s: overriding capabilities->max_speed (assuming 650KB/sec)\n", drive->name);
3943 capabilities->max_speed = 650;
3944 }
3945
3946 tape->capabilities = *capabilities; /* Save us a copy */
3947 if (capabilities->blk512)
3948 tape->tape_block_size = 512;
3949 else if (capabilities->blk1024)
3950 tape->tape_block_size = 1024;
3951
3952#if IDETAPE_DEBUG_INFO
3953 printk(KERN_INFO "ide-tape: Dumping the results of the MODE SENSE packet command\n");
3954 printk(KERN_INFO "ide-tape: Mode Parameter Header:\n");
3955 printk(KERN_INFO "ide-tape: Mode Data Length - %d\n",header->mode_data_length);
3956 printk(KERN_INFO "ide-tape: Medium Type - %d\n",header->medium_type);
3957 printk(KERN_INFO "ide-tape: Device Specific Parameter - %d\n",header->dsp);
3958 printk(KERN_INFO "ide-tape: Block Descriptor Length - %d\n",header->bdl);
3959
3960 printk(KERN_INFO "ide-tape: Capabilities and Mechanical Status Page:\n");
3961 printk(KERN_INFO "ide-tape: Page code - %d\n",capabilities->page_code);
3962 printk(KERN_INFO "ide-tape: Page length - %d\n",capabilities->page_length);
3963 printk(KERN_INFO "ide-tape: Read only - %s\n",capabilities->ro ? "Yes":"No");
3964 printk(KERN_INFO "ide-tape: Supports reverse space - %s\n",capabilities->sprev ? "Yes":"No");
3965 printk(KERN_INFO "ide-tape: Supports erase initiated formatting - %s\n",capabilities->efmt ? "Yes":"No");
3966 printk(KERN_INFO "ide-tape: Supports QFA two Partition format - %s\n",capabilities->qfa ? "Yes":"No");
3967 printk(KERN_INFO "ide-tape: Supports locking the medium - %s\n",capabilities->lock ? "Yes":"No");
3968 printk(KERN_INFO "ide-tape: The volume is currently locked - %s\n",capabilities->locked ? "Yes":"No");
3969 printk(KERN_INFO "ide-tape: The device defaults in the prevent state - %s\n",capabilities->prevent ? "Yes":"No");
3970 printk(KERN_INFO "ide-tape: Supports ejecting the medium - %s\n",capabilities->eject ? "Yes":"No");
3971 printk(KERN_INFO "ide-tape: Supports error correction - %s\n",capabilities->ecc ? "Yes":"No");
3972 printk(KERN_INFO "ide-tape: Supports data compression - %s\n",capabilities->cmprs ? "Yes":"No");
3973 printk(KERN_INFO "ide-tape: Supports 512 bytes block size - %s\n",capabilities->blk512 ? "Yes":"No");
3974 printk(KERN_INFO "ide-tape: Supports 1024 bytes block size - %s\n",capabilities->blk1024 ? "Yes":"No");
3975 printk(KERN_INFO "ide-tape: Supports 32768 bytes block size / Restricted byte count for PIO transfers - %s\n",capabilities->blk32768 ? "Yes":"No");
3976 printk(KERN_INFO "ide-tape: Maximum supported speed in KBps - %d\n",capabilities->max_speed);
3977 printk(KERN_INFO "ide-tape: Continuous transfer limits in blocks - %d\n",capabilities->ctl);
3978 printk(KERN_INFO "ide-tape: Current speed in KBps - %d\n",capabilities->speed);
3979 printk(KERN_INFO "ide-tape: Buffer size - %d\n",capabilities->buffer_size*512);
3980#endif /* IDETAPE_DEBUG_INFO */
3981}
3982
3983/*
3984 * ide_get_blocksize_from_block_descriptor does a mode sense page 0 with block descriptor
3985 * and if it succeeds sets the tape block size with the reported value
3986 */
3987static void idetape_get_blocksize_from_block_descriptor(ide_drive_t *drive)
3988{
3989
3990 idetape_tape_t *tape = drive->driver_data;
3991 idetape_pc_t pc;
3992 idetape_mode_parameter_header_t *header;
3993 idetape_parameter_block_descriptor_t *block_descrp;
3994
3995 idetape_create_mode_sense_cmd(&pc, IDETAPE_BLOCK_DESCRIPTOR);
3996 if (idetape_queue_pc_tail(drive, &pc)) {
3997 printk(KERN_ERR "ide-tape: Can't get block descriptor\n");
3998 if (tape->tape_block_size == 0) {
3999 printk(KERN_WARNING "ide-tape: Cannot deal with zero block size, assume 32k\n");
4000 tape->tape_block_size = 32768;
4001 }
4002 return;
4003 }
4004 header = (idetape_mode_parameter_header_t *) pc.buffer;
4005 block_descrp = (idetape_parameter_block_descriptor_t *) (pc.buffer + sizeof(idetape_mode_parameter_header_t));
4006 tape->tape_block_size =( block_descrp->length[0]<<16) + (block_descrp->length[1]<<8) + block_descrp->length[2];
4007 tape->drv_write_prot = (header->dsp & 0x80) >> 7;
4008
4009#if IDETAPE_DEBUG_INFO
4010 printk(KERN_INFO "ide-tape: Adjusted block size - %d\n", tape->tape_block_size);
4011#endif /* IDETAPE_DEBUG_INFO */
4012}
7662d046
BZ
4013
4014#ifdef CONFIG_IDE_PROC_FS
1da177e4
LT
4015static void idetape_add_settings (ide_drive_t *drive)
4016{
4017 idetape_tape_t *tape = drive->driver_data;
4018
4019/*
1497943e 4020 * drive setting name read/write data type min max mul_factor div_factor data pointer set function
1da177e4 4021 */
1497943e
BZ
4022 ide_add_setting(drive, "buffer", SETTING_READ, TYPE_SHORT, 0, 0xffff, 1, 2, &tape->capabilities.buffer_size, NULL);
4023 ide_add_setting(drive, "pipeline_min", SETTING_RW, TYPE_INT, 1, 0xffff, tape->stage_size / 1024, 1, &tape->min_pipeline, NULL);
4024 ide_add_setting(drive, "pipeline", SETTING_RW, TYPE_INT, 1, 0xffff, tape->stage_size / 1024, 1, &tape->max_stages, NULL);
4025 ide_add_setting(drive, "pipeline_max", SETTING_RW, TYPE_INT, 1, 0xffff, tape->stage_size / 1024, 1, &tape->max_pipeline, NULL);
4026 ide_add_setting(drive, "pipeline_used", SETTING_READ, TYPE_INT, 0, 0xffff, tape->stage_size / 1024, 1, &tape->nr_stages, NULL);
4027 ide_add_setting(drive, "pipeline_pending", SETTING_READ, TYPE_INT, 0, 0xffff, tape->stage_size / 1024, 1, &tape->nr_pending_stages, NULL);
4028 ide_add_setting(drive, "speed", SETTING_READ, TYPE_SHORT, 0, 0xffff, 1, 1, &tape->capabilities.speed, NULL);
4029 ide_add_setting(drive, "stage", SETTING_READ, TYPE_INT, 0, 0xffff, 1, 1024, &tape->stage_size, NULL);
4030 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);
4031 ide_add_setting(drive, "dsc_overlap", SETTING_RW, TYPE_BYTE, 0, 1, 1, 1, &drive->dsc_overlap, NULL);
4032 ide_add_setting(drive, "pipeline_head_speed_c",SETTING_READ, TYPE_INT, 0, 0xffff, 1, 1, &tape->controlled_pipeline_head_speed, NULL);
4033 ide_add_setting(drive, "pipeline_head_speed_u",SETTING_READ, TYPE_INT, 0, 0xffff, 1, 1, &tape->uncontrolled_pipeline_head_speed,NULL);
4034 ide_add_setting(drive, "avg_speed", SETTING_READ, TYPE_INT, 0, 0xffff, 1, 1, &tape->avg_speed, NULL);
4035 ide_add_setting(drive, "debug_level", SETTING_RW, TYPE_INT, 0, 0xffff, 1, 1, &tape->debug_level, NULL);
1da177e4 4036}
7662d046
BZ
4037#else
4038static inline void idetape_add_settings(ide_drive_t *drive) { ; }
4039#endif
1da177e4
LT
4040
4041/*
4042 * ide_setup is called to:
4043 *
4044 * 1. Initialize our various state variables.
4045 * 2. Ask the tape for its capabilities.
4046 * 3. Allocate a buffer which will be used for data
4047 * transfer. The buffer size is chosen based on
4048 * the recommendation which we received in step (2).
4049 *
4050 * Note that at this point ide.c already assigned us an irq, so that
4051 * we can queue requests here and wait for their completion.
4052 */
4053static void idetape_setup (ide_drive_t *drive, idetape_tape_t *tape, int minor)
4054{
4055 unsigned long t1, tmid, tn, t;
4056 int speed;
4057 struct idetape_id_gcw gcw;
4058 int stage_size;
4059 struct sysinfo si;
4060
4061 spin_lock_init(&tape->spinlock);
4062 drive->dsc_overlap = 1;
4166c199
BZ
4063 if (drive->hwif->host_flags & IDE_HFLAG_NO_DSC) {
4064 printk(KERN_INFO "ide-tape: %s: disabling DSC overlap\n",
4065 tape->name);
4066 drive->dsc_overlap = 0;
1da177e4 4067 }
1da177e4
LT
4068 /* Seagate Travan drives do not support DSC overlap. */
4069 if (strstr(drive->id->model, "Seagate STT3401"))
4070 drive->dsc_overlap = 0;
4071 tape->minor = minor;
4072 tape->name[0] = 'h';
4073 tape->name[1] = 't';
4074 tape->name[2] = '0' + minor;
4075 tape->chrdev_direction = idetape_direction_none;
4076 tape->pc = tape->pc_stack;
4077 tape->max_insert_speed = 10000;
4078 tape->speed_control = 1;
4079 *((unsigned short *) &gcw) = drive->id->config;
4080 if (gcw.drq_type == 1)
4081 set_bit(IDETAPE_DRQ_INTERRUPT, &tape->flags);
4082
4083 tape->min_pipeline = tape->max_pipeline = tape->max_stages = 10;
4084
4085 idetape_get_inquiry_results(drive);
4086 idetape_get_mode_sense_results(drive);
4087 idetape_get_blocksize_from_block_descriptor(drive);
4088 tape->user_bs_factor = 1;
4089 tape->stage_size = tape->capabilities.ctl * tape->tape_block_size;
4090 while (tape->stage_size > 0xffff) {
4091 printk(KERN_NOTICE "ide-tape: decreasing stage size\n");
4092 tape->capabilities.ctl /= 2;
4093 tape->stage_size = tape->capabilities.ctl * tape->tape_block_size;
4094 }
4095 stage_size = tape->stage_size;
4096 tape->pages_per_stage = stage_size / PAGE_SIZE;
4097 if (stage_size % PAGE_SIZE) {
4098 tape->pages_per_stage++;
4099 tape->excess_bh_size = PAGE_SIZE - stage_size % PAGE_SIZE;
4100 }
4101
4102 /*
4103 * Select the "best" DSC read/write polling frequency
4104 * and pipeline size.
4105 */
4106 speed = max(tape->capabilities.speed, tape->capabilities.max_speed);
4107
4108 tape->max_stages = speed * 1000 * 10 / tape->stage_size;
4109
4110 /*
4111 * Limit memory use for pipeline to 10% of physical memory
4112 */
4113 si_meminfo(&si);
4114 if (tape->max_stages * tape->stage_size > si.totalram * si.mem_unit / 10)
4115 tape->max_stages = si.totalram * si.mem_unit / (10 * tape->stage_size);
4116 tape->max_stages = min(tape->max_stages, IDETAPE_MAX_PIPELINE_STAGES);
4117 tape->min_pipeline = min(tape->max_stages, IDETAPE_MIN_PIPELINE_STAGES);
4118 tape->max_pipeline = min(tape->max_stages * 2, IDETAPE_MAX_PIPELINE_STAGES);
4119 if (tape->max_stages == 0)
4120 tape->max_stages = tape->min_pipeline = tape->max_pipeline = 1;
4121
4122 t1 = (tape->stage_size * HZ) / (speed * 1000);
4123 tmid = (tape->capabilities.buffer_size * 32 * HZ) / (speed * 125);
4124 tn = (IDETAPE_FIFO_THRESHOLD * tape->stage_size * HZ) / (speed * 1000);
4125
4126 if (tape->max_stages)
4127 t = tn;
4128 else
4129 t = t1;
4130
4131 /*
4132 * Ensure that the number we got makes sense; limit
4133 * it within IDETAPE_DSC_RW_MIN and IDETAPE_DSC_RW_MAX.
4134 */
4135 tape->best_dsc_rw_frequency = max_t(unsigned long, min_t(unsigned long, t, IDETAPE_DSC_RW_MAX), IDETAPE_DSC_RW_MIN);
4136 printk(KERN_INFO "ide-tape: %s <-> %s: %dKBps, %d*%dkB buffer, "
4137 "%dkB pipeline, %lums tDSC%s\n",
4138 drive->name, tape->name, tape->capabilities.speed,
4139 (tape->capabilities.buffer_size * 512) / tape->stage_size,
4140 tape->stage_size / 1024,
4141 tape->max_stages * tape->stage_size / 1024,
4142 tape->best_dsc_rw_frequency * 1000 / HZ,
4143 drive->using_dma ? ", DMA":"");
4144
4145 idetape_add_settings(drive);
4146}
4147
4031bbe4 4148static void ide_tape_remove(ide_drive_t *drive)
1da177e4
LT
4149{
4150 idetape_tape_t *tape = drive->driver_data;
1da177e4 4151
7662d046 4152 ide_proc_unregister_driver(drive, tape->driver);
1da177e4
LT
4153
4154 ide_unregister_region(tape->disk);
4155
4156 ide_tape_put(tape);
1da177e4
LT
4157}
4158
4159static void ide_tape_release(struct kref *kref)
4160{
4161 struct ide_tape_obj *tape = to_ide_tape(kref);
4162 ide_drive_t *drive = tape->drive;
4163 struct gendisk *g = tape->disk;
4164
8604affd
BZ
4165 BUG_ON(tape->first_stage != NULL || tape->merge_stage_size);
4166
1da177e4
LT
4167 drive->dsc_overlap = 0;
4168 drive->driver_data = NULL;
dbc1272e
TJ
4169 device_destroy(idetape_sysfs_class, MKDEV(IDETAPE_MAJOR, tape->minor));
4170 device_destroy(idetape_sysfs_class, MKDEV(IDETAPE_MAJOR, tape->minor + 128));
1da177e4
LT
4171 idetape_devs[tape->minor] = NULL;
4172 g->private_data = NULL;
4173 put_disk(g);
4174 kfree(tape);
4175}
4176
ecfd80e4 4177#ifdef CONFIG_IDE_PROC_FS
1da177e4
LT
4178static int proc_idetape_read_name
4179 (char *page, char **start, off_t off, int count, int *eof, void *data)
4180{
4181 ide_drive_t *drive = (ide_drive_t *) data;
4182 idetape_tape_t *tape = drive->driver_data;
4183 char *out = page;
4184 int len;
4185
4186 len = sprintf(out, "%s\n", tape->name);
4187 PROC_IDE_READ_RETURN(page, start, off, count, eof, len);
4188}
4189
4190static ide_proc_entry_t idetape_proc[] = {
4191 { "capacity", S_IFREG|S_IRUGO, proc_ide_read_capacity, NULL },
4192 { "name", S_IFREG|S_IRUGO, proc_idetape_read_name, NULL },
4193 { NULL, 0, NULL, NULL }
4194};
1da177e4
LT
4195#endif
4196
4031bbe4 4197static int ide_tape_probe(ide_drive_t *);
1da177e4 4198
1da177e4 4199static ide_driver_t idetape_driver = {
8604affd 4200 .gen_driver = {
4ef3b8f4 4201 .owner = THIS_MODULE,
8604affd
BZ
4202 .name = "ide-tape",
4203 .bus = &ide_bus_type,
8604affd 4204 },
4031bbe4
RK
4205 .probe = ide_tape_probe,
4206 .remove = ide_tape_remove,
1da177e4
LT
4207 .version = IDETAPE_VERSION,
4208 .media = ide_tape,
1da177e4 4209 .supports_dsc_overlap = 1,
1da177e4
LT
4210 .do_request = idetape_do_request,
4211 .end_request = idetape_end_request,
4212 .error = __ide_error,
4213 .abort = __ide_abort,
7662d046 4214#ifdef CONFIG_IDE_PROC_FS
1da177e4 4215 .proc = idetape_proc,
7662d046 4216#endif
1da177e4
LT
4217};
4218
4219/*
4220 * Our character device supporting functions, passed to register_chrdev.
4221 */
2b8693c0 4222static const struct file_operations idetape_fops = {
1da177e4
LT
4223 .owner = THIS_MODULE,
4224 .read = idetape_chrdev_read,
4225 .write = idetape_chrdev_write,
4226 .ioctl = idetape_chrdev_ioctl,
4227 .open = idetape_chrdev_open,
4228 .release = idetape_chrdev_release,
4229};
4230
4231static int idetape_open(struct inode *inode, struct file *filp)
4232{
4233 struct gendisk *disk = inode->i_bdev->bd_disk;
4234 struct ide_tape_obj *tape;
1da177e4
LT
4235
4236 if (!(tape = ide_tape_get(disk)))
4237 return -ENXIO;
4238
1da177e4
LT
4239 return 0;
4240}
4241
4242static int idetape_release(struct inode *inode, struct file *filp)
4243{
4244 struct gendisk *disk = inode->i_bdev->bd_disk;
4245 struct ide_tape_obj *tape = ide_tape_g(disk);
1da177e4
LT
4246
4247 ide_tape_put(tape);
4248
4249 return 0;
4250}
4251
4252static int idetape_ioctl(struct inode *inode, struct file *file,
4253 unsigned int cmd, unsigned long arg)
4254{
4255 struct block_device *bdev = inode->i_bdev;
4256 struct ide_tape_obj *tape = ide_tape_g(bdev->bd_disk);
4257 ide_drive_t *drive = tape->drive;
4258 int err = generic_ide_ioctl(drive, file, bdev, cmd, arg);
4259 if (err == -EINVAL)
4260 err = idetape_blkdev_ioctl(drive, cmd, arg);
4261 return err;
4262}
4263
4264static struct block_device_operations idetape_block_ops = {
4265 .owner = THIS_MODULE,
4266 .open = idetape_open,
4267 .release = idetape_release,
4268 .ioctl = idetape_ioctl,
4269};
4270
4031bbe4 4271static int ide_tape_probe(ide_drive_t *drive)
1da177e4
LT
4272{
4273 idetape_tape_t *tape;
4274 struct gendisk *g;
4275 int minor;
4276
4277 if (!strstr("ide-tape", drive->driver_req))
4278 goto failed;
4279 if (!drive->present)
4280 goto failed;
4281 if (drive->media != ide_tape)
4282 goto failed;
4283 if (!idetape_identify_device (drive)) {
4284 printk(KERN_ERR "ide-tape: %s: not supported by this version of ide-tape\n", drive->name);
4285 goto failed;
4286 }
4287 if (drive->scsi) {
4288 printk("ide-tape: passing drive %s to ide-scsi emulation.\n", drive->name);
4289 goto failed;
4290 }
4291 if (strstr(drive->id->model, "OnStream DI-")) {
4292 printk(KERN_WARNING "ide-tape: Use drive %s with ide-scsi emulation and osst.\n", drive->name);
4293 printk(KERN_WARNING "ide-tape: OnStream support will be removed soon from ide-tape!\n");
4294 }
5cbded58 4295 tape = kzalloc(sizeof (idetape_tape_t), GFP_KERNEL);
1da177e4
LT
4296 if (tape == NULL) {
4297 printk(KERN_ERR "ide-tape: %s: Can't allocate a tape structure\n", drive->name);
4298 goto failed;
4299 }
4300
4301 g = alloc_disk(1 << PARTN_BITS);
4302 if (!g)
4303 goto out_free_tape;
4304
4305 ide_init_disk(g, drive);
4306
7662d046 4307 ide_proc_register_driver(drive, &idetape_driver);
1da177e4 4308
1da177e4
LT
4309 kref_init(&tape->kref);
4310
4311 tape->drive = drive;
4312 tape->driver = &idetape_driver;
4313 tape->disk = g;
4314
4315 g->private_data = &tape->driver;
4316
4317 drive->driver_data = tape;
4318
cf8b8975 4319 mutex_lock(&idetape_ref_mutex);
1da177e4
LT
4320 for (minor = 0; idetape_devs[minor]; minor++)
4321 ;
4322 idetape_devs[minor] = tape;
cf8b8975 4323 mutex_unlock(&idetape_ref_mutex);
1da177e4
LT
4324
4325 idetape_setup(drive, tape, minor);
4326
dbc1272e
TJ
4327 device_create(idetape_sysfs_class, &drive->gendev,
4328 MKDEV(IDETAPE_MAJOR, minor), "%s", tape->name);
4329 device_create(idetape_sysfs_class, &drive->gendev,
4330 MKDEV(IDETAPE_MAJOR, minor + 128), "n%s", tape->name);
d5dee80a 4331
1da177e4
LT
4332 g->fops = &idetape_block_ops;
4333 ide_register_region(g);
4334
4335 return 0;
8604affd 4336
1da177e4
LT
4337out_free_tape:
4338 kfree(tape);
4339failed:
8604affd 4340 return -ENODEV;
1da177e4
LT
4341}
4342
4343MODULE_DESCRIPTION("ATAPI Streaming TAPE Driver");
4344MODULE_LICENSE("GPL");
4345
4346static void __exit idetape_exit (void)
4347{
8604affd 4348 driver_unregister(&idetape_driver.gen_driver);
d5dee80a 4349 class_destroy(idetape_sysfs_class);
1da177e4
LT
4350 unregister_chrdev(IDETAPE_MAJOR, "ht");
4351}
4352
17514e8a 4353static int __init idetape_init(void)
1da177e4 4354{
d5dee80a
WD
4355 int error = 1;
4356 idetape_sysfs_class = class_create(THIS_MODULE, "ide_tape");
4357 if (IS_ERR(idetape_sysfs_class)) {
4358 idetape_sysfs_class = NULL;
4359 printk(KERN_ERR "Unable to create sysfs class for ide tapes\n");
4360 error = -EBUSY;
4361 goto out;
4362 }
4363
1da177e4
LT
4364 if (register_chrdev(IDETAPE_MAJOR, "ht", &idetape_fops)) {
4365 printk(KERN_ERR "ide-tape: Failed to register character device interface\n");
d5dee80a
WD
4366 error = -EBUSY;
4367 goto out_free_class;
1da177e4 4368 }
d5dee80a
WD
4369
4370 error = driver_register(&idetape_driver.gen_driver);
4371 if (error)
4372 goto out_free_driver;
4373
4374 return 0;
4375
4376out_free_driver:
4377 driver_unregister(&idetape_driver.gen_driver);
4378out_free_class:
4379 class_destroy(idetape_sysfs_class);
4380out:
4381 return error;
1da177e4
LT
4382}
4383
263756ec 4384MODULE_ALIAS("ide:*m-tape*");
1da177e4
LT
4385module_init(idetape_init);
4386module_exit(idetape_exit);
4387MODULE_ALIAS_CHARDEV_MAJOR(IDETAPE_MAJOR);