2 * IDE ATAPI streaming tape driver.
4 * Copyright (C) 1995-1999 Gadi Oxman <gadio@netvision.net.il>
5 * Copyright (C) 2003-2005 Bartlomiej Zolnierkiewicz
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.
11 * It is hereby placed under the terms of the GNU general public license.
12 * (See linux/COPYING).
14 * For a historical changelog see
15 * Documentation/ide/ChangeLog.ide-tape.1995-2002
18 #define IDETAPE_VERSION "1.19"
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>
27 #include <linux/interrupt.h>
28 #include <linux/jiffies.h>
29 #include <linux/major.h>
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>
38 #include <linux/mutex.h>
40 #include <asm/byteorder.h>
42 #include <asm/uaccess.h>
44 #include <asm/unaligned.h>
49 typedef struct os_partition_s {
53 __u32 first_frame_addr;
54 __u32 last_frame_addr;
61 typedef struct os_dat_entry_s {
71 #define OS_DAT_FLAGS_DATA (0xc)
72 #define OS_DAT_FLAGS_MARK (0x1)
74 typedef struct os_dat_s {
79 os_dat_entry_t dat_list[16];
82 #include <linux/mtio.h>
84 /**************************** Tunable parameters *****************************/
88 * Pipelined mode parameters.
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:
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.
99 * Setting the following parameter to 0 is illegal: the pipelined mode
100 * cannot be disabled (calculate_speeds() divides by tape->max_stages.)
102 #define IDETAPE_MIN_PIPELINE_STAGES 1
103 #define IDETAPE_MAX_PIPELINE_STAGES 400
104 #define IDETAPE_INCREASE_STAGES_RATE 20
107 * The following are used to debug the driver:
109 * Setting IDETAPE_DEBUG_LOG to 1 will log driver flow control.
110 * Setting IDETAPE_DEBUG_BUGS to 1 will enable self-sanity checks in
113 * Setting them to 0 will restore normal operation mode:
115 * 1. Disable logging normal successful operations.
116 * 2. Disable self-sanity checks.
117 * 3. Errors will still be logged, of course.
119 * All the #if DEBUG code will be removed some day, when the driver
120 * is verified to be stable enough. This will make it much more
123 #define IDETAPE_DEBUG_LOG 0
124 #define IDETAPE_DEBUG_BUGS 1
127 * After each failed packet command we issue a request sense command
128 * and retry the packet command IDETAPE_MAX_PC_RETRIES times.
130 * Setting IDETAPE_MAX_PC_RETRIES to 0 will disable retries.
132 #define IDETAPE_MAX_PC_RETRIES 3
135 * With each packet command, we allocate a buffer of
136 * IDETAPE_PC_BUFFER_SIZE bytes. This is used for several packet
137 * commands (Not for READ/WRITE commands).
139 #define IDETAPE_PC_BUFFER_SIZE 256
142 * In various places in the driver, we need to allocate storage
143 * for packet commands and requests, which will remain valid while
144 * we leave the driver to wait for an interrupt or a timeout event.
146 #define IDETAPE_PC_STACK (10 + IDETAPE_MAX_PC_RETRIES)
149 * Some drives (for example, Seagate STT3401A Travan) require a very long
150 * timeout, because they don't return an interrupt or clear their busy bit
151 * until after the command completes (even retension commands).
153 #define IDETAPE_WAIT_CMD (900*HZ)
156 * The following parameter is used to select the point in the internal
157 * tape fifo in which we will start to refill the buffer. Decreasing
158 * the following parameter will improve the system's latency and
159 * interactive response, while using a high value might improve system
162 #define IDETAPE_FIFO_THRESHOLD 2
165 * DSC polling parameters.
167 * Polling for DSC (a single bit in the status register) is a very
168 * important function in ide-tape. There are two cases in which we
171 * 1. Before a read/write packet command, to ensure that we
172 * can transfer data from/to the tape's data buffers, without
173 * causing an actual media access. In case the tape is not
174 * ready yet, we take out our request from the device
175 * request queue, so that ide.c will service requests from
176 * the other device on the same interface meanwhile.
178 * 2. After the successful initialization of a "media access
179 * packet command", which is a command which can take a long
180 * time to complete (it can be several seconds or even an hour).
182 * Again, we postpone our request in the middle to free the bus
183 * for the other device. The polling frequency here should be
184 * lower than the read/write frequency since those media access
185 * commands are slow. We start from a "fast" frequency -
186 * IDETAPE_DSC_MA_FAST (one second), and if we don't receive DSC
187 * after IDETAPE_DSC_MA_THRESHOLD (5 minutes), we switch it to a
188 * lower frequency - IDETAPE_DSC_MA_SLOW (1 minute).
190 * We also set a timeout for the timer, in case something goes wrong.
191 * The timeout should be longer then the maximum execution time of a
198 #define IDETAPE_DSC_RW_MIN 5*HZ/100 /* 50 msec */
199 #define IDETAPE_DSC_RW_MAX 40*HZ/100 /* 400 msec */
200 #define IDETAPE_DSC_RW_TIMEOUT 2*60*HZ /* 2 minutes */
201 #define IDETAPE_DSC_MA_FAST 2*HZ /* 2 seconds */
202 #define IDETAPE_DSC_MA_THRESHOLD 5*60*HZ /* 5 minutes */
203 #define IDETAPE_DSC_MA_SLOW 30*HZ /* 30 seconds */
204 #define IDETAPE_DSC_MA_TIMEOUT 2*60*60*HZ /* 2 hours */
206 /*************************** End of tunable parameters ***********************/
209 * Read/Write error simulation
211 #define SIMULATE_ERRORS 0
214 * For general magnetic tape device compatibility.
217 idetape_direction_none,
218 idetape_direction_read,
219 idetape_direction_write
220 } idetape_chrdev_direction_t;
225 struct idetape_bh *b_reqnext;
230 * Our view of a packet command.
232 typedef struct idetape_packet_command_s {
233 u8 c[12]; /* Actual packet bytes */
234 int retries; /* On each retry, we increment retries */
235 int error; /* Error code */
236 int request_transfer; /* Bytes to transfer */
237 int actually_transferred; /* Bytes actually transferred */
238 int buffer_size; /* Size of our data buffer */
239 struct idetape_bh *bh;
242 u8 *buffer; /* Data buffer */
243 u8 *current_position; /* Pointer into the above buffer */
244 ide_startstop_t (*callback) (ide_drive_t *); /* Called when this packet command is completed */
245 u8 pc_buffer[IDETAPE_PC_BUFFER_SIZE]; /* Temporary buffer */
246 unsigned long flags; /* Status/Action bit flags: long for set_bit */
250 * Packet command flag bits.
252 /* Set when an error is considered normal - We won't retry */
254 /* 1 When polling for DSC on a media access command */
255 #define PC_WAIT_FOR_DSC 1
256 /* 1 when we prefer to use DMA if possible */
257 #define PC_DMA_RECOMMENDED 2
258 /* 1 while DMA in progress */
259 #define PC_DMA_IN_PROGRESS 3
260 /* 1 when encountered problem during DMA */
261 #define PC_DMA_ERROR 4
266 * Capabilities and Mechanical Status Page
269 unsigned page_code :6; /* Page code - Should be 0x2a */
271 __u8 ps :1; /* parameters saveable */
272 __u8 page_length; /* Page Length - Should be 0x12 */
273 __u8 reserved2, reserved3;
274 unsigned ro :1; /* Read Only Mode */
275 unsigned reserved4_1234 :4;
276 unsigned sprev :1; /* Supports SPACE in the reverse direction */
277 unsigned reserved4_67 :2;
278 unsigned reserved5_012 :3;
279 unsigned efmt :1; /* Supports ERASE command initiated formatting */
280 unsigned reserved5_4 :1;
281 unsigned qfa :1; /* Supports the QFA two partition formats */
282 unsigned reserved5_67 :2;
283 unsigned lock :1; /* Supports locking the volume */
284 unsigned locked :1; /* The volume is locked */
285 unsigned prevent :1; /* The device defaults in the prevent state after power up */
286 unsigned eject :1; /* The device can eject the volume */
287 __u8 disconnect :1; /* The device can break request > ctl */
289 unsigned ecc :1; /* Supports error correction */
290 unsigned cmprs :1; /* Supports data compression */
291 unsigned reserved7_0 :1;
292 unsigned blk512 :1; /* Supports 512 bytes block size */
293 unsigned blk1024 :1; /* Supports 1024 bytes block size */
294 unsigned reserved7_3_6 :4;
295 unsigned blk32768 :1; /* slowb - the device restricts the byte count for PIO */
296 /* transfers for slow buffer memory ??? */
297 /* Also 32768 block size in some cases */
298 __u16 max_speed; /* Maximum speed supported in KBps */
299 __u8 reserved10, reserved11;
300 __u16 ctl; /* Continuous Transfer Limit in blocks */
301 __u16 speed; /* Current Speed, in KBps */
302 __u16 buffer_size; /* Buffer Size, in 512 bytes */
303 __u8 reserved18, reserved19;
304 } idetape_capabilities_page_t;
310 unsigned page_code :6; /* Page code - Should be 0x30 */
311 unsigned reserved1_6 :1;
313 __u8 page_length; /* Page Length - Should be 2 */
316 unsigned play32_5 :1;
317 unsigned reserved2_23 :2;
318 unsigned record32 :1;
319 unsigned record32_5 :1;
320 unsigned reserved2_6 :1;
322 } idetape_block_size_page_t;
327 typedef struct idetape_stage_s {
328 struct request rq; /* The corresponding request */
329 struct idetape_bh *bh; /* The data buffers */
330 struct idetape_stage_s *next; /* Pointer to the next stage */
334 * Most of our global data which we need to save even as we leave the
335 * driver due to an interrupt or a timer event is stored in a variable
336 * of type idetape_tape_t, defined below.
338 typedef struct ide_tape_obj {
340 ide_driver_t *driver;
341 struct gendisk *disk;
345 * Since a typical character device operation requires more
346 * than one packet command, we provide here enough memory
347 * for the maximum of interconnected packet commands.
348 * The packet commands are stored in the circular array pc_stack.
349 * pc_stack_index points to the last used entry, and warps around
350 * to the start when we get to the last array entry.
352 * pc points to the current processed packet command.
354 * failed_pc points to the last failed packet command, or contains
355 * NULL if we do not need to retry any packet command. This is
356 * required since an additional packet command is needed before the
357 * retry, to get detailed information on what went wrong.
359 /* Current packet command */
361 /* Last failed packet command */
362 idetape_pc_t *failed_pc;
363 /* Packet command stack */
364 idetape_pc_t pc_stack[IDETAPE_PC_STACK];
365 /* Next free packet command storage space */
367 struct request rq_stack[IDETAPE_PC_STACK];
368 /* We implement a circular array */
372 * DSC polling variables.
374 * While polling for DSC we use postponed_rq to postpone the
375 * current request so that ide.c will be able to service
376 * pending requests on the other device. Note that at most
377 * we will have only one DSC (usually data transfer) request
378 * in the device request queue. Additional requests can be
379 * queued in our internal pipeline, but they will be visible
380 * to ide.c only one at a time.
382 struct request *postponed_rq;
383 /* The time in which we started polling for DSC */
384 unsigned long dsc_polling_start;
385 /* Timer used to poll for dsc */
386 struct timer_list dsc_timer;
387 /* Read/Write dsc polling frequency */
388 unsigned long best_dsc_rw_frequency;
389 /* The current polling frequency */
390 unsigned long dsc_polling_frequency;
391 /* Maximum waiting time */
392 unsigned long dsc_timeout;
395 * Read position information
399 unsigned int first_frame_position;
400 unsigned int last_frame_position;
401 unsigned int blocks_in_buffer;
404 * Last error information
406 u8 sense_key, asc, ascq;
409 * Character device operation
414 /* Current character device data transfer direction */
415 idetape_chrdev_direction_t chrdev_direction;
420 /* Usually 512 or 1024 bytes */
421 unsigned short tape_block_size;
423 /* Copy of the tape's Capabilities and Mechanical Page */
424 idetape_capabilities_page_t capabilities;
427 * Active data transfer request parameters.
429 * At most, there is only one ide-tape originated data transfer
430 * request in the device request queue. This allows ide.c to
431 * easily service requests from the other device when we
432 * postpone our active request. In the pipelined operation
433 * mode, we use our internal pipeline structure to hold
434 * more data requests.
436 * The data buffer size is chosen based on the tape's
439 /* Pointer to the request which is waiting in the device request queue */
440 struct request *active_data_request;
441 /* Data buffer size (chosen based on the tape's recommendation */
443 idetape_stage_t *merge_stage;
444 int merge_stage_size;
445 struct idetape_bh *bh;
450 * Pipeline parameters.
452 * To accomplish non-pipelined mode, we simply set the following
453 * variables to zero (or NULL, where appropriate).
455 /* Number of currently used stages */
457 /* Number of pending stages */
458 int nr_pending_stages;
459 /* We will not allocate more than this number of stages */
460 int max_stages, min_pipeline, max_pipeline;
461 /* The first stage which will be removed from the pipeline */
462 idetape_stage_t *first_stage;
463 /* The currently active stage */
464 idetape_stage_t *active_stage;
465 /* Will be serviced after the currently active request */
466 idetape_stage_t *next_stage;
467 /* New requests will be added to the pipeline here */
468 idetape_stage_t *last_stage;
469 /* Optional free stage which we can use */
470 idetape_stage_t *cache_stage;
472 /* Wasted space in each stage */
475 /* Status/Action flags: long for set_bit */
477 /* protects the ide-tape queue */
481 * Measures average tape speed
483 unsigned long avg_time;
489 char firmware_revision[6];
490 int firmware_revision_num;
492 /* the door is currently locked */
494 /* the tape hardware is write protected */
496 /* the tape is write protected (hardware or opened as read-only) */
500 * Limit the number of times a request can
501 * be postponed, to avoid an infinite postpone
504 /* request postpone count limit */
508 * Measures number of frames:
510 * 1. written/read to/from the driver pipeline (pipeline_head).
511 * 2. written/read to/from the tape buffers (idetape_bh).
512 * 3. written/read by the tape to/from the media (tape_head).
520 * Speed control at the tape buffers input/output
522 unsigned long insert_time;
525 int max_insert_speed;
526 int measure_insert_time;
529 * Measure tape still time, in milliseconds
531 unsigned long tape_still_time_begin;
535 * Speed regulation negative feedback loop
538 int pipeline_head_speed;
539 int controlled_pipeline_head_speed;
540 int uncontrolled_pipeline_head_speed;
541 int controlled_last_pipeline_head;
542 int uncontrolled_last_pipeline_head;
543 unsigned long uncontrolled_pipeline_head_time;
544 unsigned long controlled_pipeline_head_time;
545 int controlled_previous_pipeline_head;
546 int uncontrolled_previous_pipeline_head;
547 unsigned long controlled_previous_head_time;
548 unsigned long uncontrolled_previous_head_time;
549 int restart_speed_control_req;
552 * Debug_level determines amount of debugging output;
553 * can be changed using /proc/ide/hdx/settings
554 * 0 : almost no debugging output
555 * 1 : 0+output errors only
556 * 2 : 1+output all sensekey/asc
557 * 3 : 2+follow all chrdev related procedures
558 * 4 : 3+follow all procedures
559 * 5 : 4+include pc_stack rq_stack info
560 * 6 : 5+USE_COUNT updates
565 static DEFINE_MUTEX(idetape_ref_mutex);
567 static struct class *idetape_sysfs_class;
569 #define to_ide_tape(obj) container_of(obj, struct ide_tape_obj, kref)
571 #define ide_tape_g(disk) \
572 container_of((disk)->private_data, struct ide_tape_obj, driver)
574 static struct ide_tape_obj *ide_tape_get(struct gendisk *disk)
576 struct ide_tape_obj *tape = NULL;
578 mutex_lock(&idetape_ref_mutex);
579 tape = ide_tape_g(disk);
581 kref_get(&tape->kref);
582 mutex_unlock(&idetape_ref_mutex);
586 static void ide_tape_release(struct kref *);
588 static void ide_tape_put(struct ide_tape_obj *tape)
590 mutex_lock(&idetape_ref_mutex);
591 kref_put(&tape->kref, ide_tape_release);
592 mutex_unlock(&idetape_ref_mutex);
598 #define DOOR_UNLOCKED 0
599 #define DOOR_LOCKED 1
600 #define DOOR_EXPLICITLY_LOCKED 2
603 * Tape flag bits values.
605 #define IDETAPE_IGNORE_DSC 0
606 #define IDETAPE_ADDRESS_VALID 1 /* 0 When the tape position is unknown */
607 #define IDETAPE_BUSY 2 /* Device already opened */
608 #define IDETAPE_PIPELINE_ERROR 3 /* Error detected in a pipeline stage */
609 #define IDETAPE_DETECT_BS 4 /* Attempt to auto-detect the current user block size */
610 #define IDETAPE_FILEMARK 5 /* Currently on a filemark */
611 #define IDETAPE_DRQ_INTERRUPT 6 /* DRQ interrupt device */
612 #define IDETAPE_READ_ERROR 7
613 #define IDETAPE_PIPELINE_ACTIVE 8 /* pipeline active */
614 /* 0 = no tape is loaded, so we don't rewind after ejecting */
615 #define IDETAPE_MEDIUM_PRESENT 9
618 * Supported ATAPI tape drives packet commands
620 #define IDETAPE_TEST_UNIT_READY_CMD 0x00
621 #define IDETAPE_REWIND_CMD 0x01
622 #define IDETAPE_REQUEST_SENSE_CMD 0x03
623 #define IDETAPE_READ_CMD 0x08
624 #define IDETAPE_WRITE_CMD 0x0a
625 #define IDETAPE_WRITE_FILEMARK_CMD 0x10
626 #define IDETAPE_SPACE_CMD 0x11
627 #define IDETAPE_INQUIRY_CMD 0x12
628 #define IDETAPE_ERASE_CMD 0x19
629 #define IDETAPE_MODE_SENSE_CMD 0x1a
630 #define IDETAPE_MODE_SELECT_CMD 0x15
631 #define IDETAPE_LOAD_UNLOAD_CMD 0x1b
632 #define IDETAPE_PREVENT_CMD 0x1e
633 #define IDETAPE_LOCATE_CMD 0x2b
634 #define IDETAPE_READ_POSITION_CMD 0x34
635 #define IDETAPE_READ_BUFFER_CMD 0x3c
636 #define IDETAPE_SET_SPEED_CMD 0xbb
639 * Some defines for the READ BUFFER command
641 #define IDETAPE_RETRIEVE_FAULTY_BLOCK 6
644 * Some defines for the SPACE command
646 #define IDETAPE_SPACE_OVER_FILEMARK 1
647 #define IDETAPE_SPACE_TO_EOD 3
650 * Some defines for the LOAD UNLOAD command
652 #define IDETAPE_LU_LOAD_MASK 1
653 #define IDETAPE_LU_RETENSION_MASK 2
654 #define IDETAPE_LU_EOT_MASK 4
657 * Special requests for our block device strategy routine.
659 * In order to service a character device command, we add special
660 * requests to the tail of our block device request queue and wait
661 * for their completion.
665 REQ_IDETAPE_PC1 = (1 << 0), /* packet command (first stage) */
666 REQ_IDETAPE_PC2 = (1 << 1), /* packet command (second stage) */
667 REQ_IDETAPE_READ = (1 << 2),
668 REQ_IDETAPE_WRITE = (1 << 3),
669 REQ_IDETAPE_READ_BUFFER = (1 << 4),
673 * Error codes which are returned in rq->errors to the higher part
676 #define IDETAPE_ERROR_GENERAL 101
677 #define IDETAPE_ERROR_FILEMARK 102
678 #define IDETAPE_ERROR_EOD 103
681 * The following is used to format the general configuration word of
682 * the ATAPI IDENTIFY DEVICE command.
684 struct idetape_id_gcw {
685 unsigned packet_size :2; /* Packet Size */
686 unsigned reserved234 :3; /* Reserved */
687 unsigned drq_type :2; /* Command packet DRQ type */
688 unsigned removable :1; /* Removable media */
689 unsigned device_type :5; /* Device type */
690 unsigned reserved13 :1; /* Reserved */
691 unsigned protocol :2; /* Protocol type */
695 * INQUIRY packet command - Data Format (From Table 6-8 of QIC-157C)
698 unsigned device_type :5; /* Peripheral Device Type */
699 unsigned reserved0_765 :3; /* Peripheral Qualifier - Reserved */
700 unsigned reserved1_6t0 :7; /* Reserved */
701 unsigned rmb :1; /* Removable Medium Bit */
702 unsigned ansi_version :3; /* ANSI Version */
703 unsigned ecma_version :3; /* ECMA Version */
704 unsigned iso_version :2; /* ISO Version */
705 unsigned response_format :4; /* Response Data Format */
706 unsigned reserved3_45 :2; /* Reserved */
707 unsigned reserved3_6 :1; /* TrmIOP - Reserved */
708 unsigned reserved3_7 :1; /* AENC - Reserved */
709 __u8 additional_length; /* Additional Length (total_length-4) */
710 __u8 rsv5, rsv6, rsv7; /* Reserved */
711 __u8 vendor_id[8]; /* Vendor Identification */
712 __u8 product_id[16]; /* Product Identification */
713 __u8 revision_level[4]; /* Revision Level */
714 __u8 vendor_specific[20]; /* Vendor Specific - Optional */
715 __u8 reserved56t95[40]; /* Reserved - Optional */
716 /* Additional information may be returned */
717 } idetape_inquiry_result_t;
720 * READ POSITION packet command - Data Format (From Table 6-57)
723 unsigned reserved0_10 :2; /* Reserved */
724 unsigned bpu :1; /* Block Position Unknown */
725 unsigned reserved0_543 :3; /* Reserved */
726 unsigned eop :1; /* End Of Partition */
727 unsigned bop :1; /* Beginning Of Partition */
728 u8 partition; /* Partition Number */
729 u8 reserved2, reserved3; /* Reserved */
730 u32 first_block; /* First Block Location */
731 u32 last_block; /* Last Block Location (Optional) */
732 u8 reserved12; /* Reserved */
733 u8 blocks_in_buffer[3]; /* Blocks In Buffer - (Optional) */
734 u32 bytes_in_buffer; /* Bytes In Buffer (Optional) */
735 } idetape_read_position_result_t;
738 * Follows structures which are related to the SELECT SENSE / MODE SENSE
739 * packet commands. Those packet commands are still not supported
742 #define IDETAPE_BLOCK_DESCRIPTOR 0
743 #define IDETAPE_CAPABILITIES_PAGE 0x2a
744 #define IDETAPE_PARAMTR_PAGE 0x2b /* Onstream DI-x0 only */
745 #define IDETAPE_BLOCK_SIZE_PAGE 0x30
746 #define IDETAPE_BUFFER_FILLING_PAGE 0x33
749 * Mode Parameter Block Descriptor the MODE SENSE packet command
751 * Support for block descriptors is optional.
754 __u8 density_code; /* Medium density code */
755 __u8 blocks[3]; /* Number of blocks */
756 __u8 reserved4; /* Reserved */
757 __u8 length[3]; /* Block Length */
758 } idetape_parameter_block_descriptor_t;
761 * The Data Compression Page, as returned by the MODE SENSE packet command.
764 unsigned page_code :6; /* Page Code - Should be 0xf */
765 unsigned reserved0 :1; /* Reserved */
767 __u8 page_length; /* Page Length - Should be 14 */
768 unsigned reserved2 :6; /* Reserved */
769 unsigned dcc :1; /* Data Compression Capable */
770 unsigned dce :1; /* Data Compression Enable */
771 unsigned reserved3 :5; /* Reserved */
772 unsigned red :2; /* Report Exception on Decompression */
773 unsigned dde :1; /* Data Decompression Enable */
774 __u32 ca; /* Compression Algorithm */
775 __u32 da; /* Decompression Algorithm */
776 __u8 reserved[4]; /* Reserved */
777 } idetape_data_compression_page_t;
780 * The Medium Partition Page, as returned by the MODE SENSE packet command.
783 unsigned page_code :6; /* Page Code - Should be 0x11 */
784 unsigned reserved1_6 :1; /* Reserved */
786 __u8 page_length; /* Page Length - Should be 6 */
787 __u8 map; /* Maximum Additional Partitions - Should be 0 */
788 __u8 apd; /* Additional Partitions Defined - Should be 0 */
789 unsigned reserved4_012 :3; /* Reserved */
790 unsigned psum :2; /* Should be 0 */
791 unsigned idp :1; /* Should be 0 */
792 unsigned sdp :1; /* Should be 0 */
793 unsigned fdp :1; /* Fixed Data Partitions */
794 __u8 mfr; /* Medium Format Recognition */
795 __u8 reserved[2]; /* Reserved */
796 } idetape_medium_partition_page_t;
799 * Run time configurable parameters.
802 int dsc_rw_frequency;
803 int dsc_media_access_frequency;
808 * The variables below are used for the character device interface.
809 * Additional state variables are defined in our ide_drive_t structure.
811 static struct ide_tape_obj * idetape_devs[MAX_HWIFS * MAX_DRIVES];
813 #define ide_tape_f(file) ((file)->private_data)
815 static struct ide_tape_obj *ide_tape_chrdev_get(unsigned int i)
817 struct ide_tape_obj *tape = NULL;
819 mutex_lock(&idetape_ref_mutex);
820 tape = idetape_devs[i];
822 kref_get(&tape->kref);
823 mutex_unlock(&idetape_ref_mutex);
828 * Function declarations
831 static int idetape_chrdev_release (struct inode *inode, struct file *filp);
832 static void idetape_write_release (ide_drive_t *drive, unsigned int minor);
835 * Too bad. The drive wants to send us data which we are not ready to accept.
836 * Just throw it away.
838 static void idetape_discard_data (ide_drive_t *drive, unsigned int bcount)
841 (void) HWIF(drive)->INB(IDE_DATA_REG);
844 static void idetape_input_buffers (ide_drive_t *drive, idetape_pc_t *pc, unsigned int bcount)
846 struct idetape_bh *bh = pc->bh;
850 #if IDETAPE_DEBUG_BUGS
852 printk(KERN_ERR "ide-tape: bh == NULL in "
853 "idetape_input_buffers\n");
854 idetape_discard_data(drive, bcount);
857 #endif /* IDETAPE_DEBUG_BUGS */
858 count = min((unsigned int)(bh->b_size - atomic_read(&bh->b_count)), bcount);
859 HWIF(drive)->atapi_input_bytes(drive, bh->b_data + atomic_read(&bh->b_count), count);
861 atomic_add(count, &bh->b_count);
862 if (atomic_read(&bh->b_count) == bh->b_size) {
865 atomic_set(&bh->b_count, 0);
871 static void idetape_output_buffers (ide_drive_t *drive, idetape_pc_t *pc, unsigned int bcount)
873 struct idetape_bh *bh = pc->bh;
877 #if IDETAPE_DEBUG_BUGS
879 printk(KERN_ERR "ide-tape: bh == NULL in "
880 "idetape_output_buffers\n");
883 #endif /* IDETAPE_DEBUG_BUGS */
884 count = min((unsigned int)pc->b_count, (unsigned int)bcount);
885 HWIF(drive)->atapi_output_bytes(drive, pc->b_data, count);
888 pc->b_count -= count;
890 pc->bh = bh = bh->b_reqnext;
892 pc->b_data = bh->b_data;
893 pc->b_count = atomic_read(&bh->b_count);
899 static void idetape_update_buffers (idetape_pc_t *pc)
901 struct idetape_bh *bh = pc->bh;
903 unsigned int bcount = pc->actually_transferred;
905 if (test_bit(PC_WRITING, &pc->flags))
908 #if IDETAPE_DEBUG_BUGS
910 printk(KERN_ERR "ide-tape: bh == NULL in "
911 "idetape_update_buffers\n");
914 #endif /* IDETAPE_DEBUG_BUGS */
915 count = min((unsigned int)bh->b_size, (unsigned int)bcount);
916 atomic_set(&bh->b_count, count);
917 if (atomic_read(&bh->b_count) == bh->b_size)
925 * idetape_next_pc_storage returns a pointer to a place in which we can
926 * safely store a packet command, even though we intend to leave the
927 * driver. A storage space for a maximum of IDETAPE_PC_STACK packet
928 * commands is allocated at initialization time.
930 static idetape_pc_t *idetape_next_pc_storage (ide_drive_t *drive)
932 idetape_tape_t *tape = drive->driver_data;
934 #if IDETAPE_DEBUG_LOG
935 if (tape->debug_level >= 5)
936 printk(KERN_INFO "ide-tape: pc_stack_index=%d\n",
937 tape->pc_stack_index);
938 #endif /* IDETAPE_DEBUG_LOG */
939 if (tape->pc_stack_index == IDETAPE_PC_STACK)
940 tape->pc_stack_index=0;
941 return (&tape->pc_stack[tape->pc_stack_index++]);
945 * idetape_next_rq_storage is used along with idetape_next_pc_storage.
946 * Since we queue packet commands in the request queue, we need to
947 * allocate a request, along with the allocation of a packet command.
950 /**************************************************************
952 * This should get fixed to use kmalloc(.., GFP_ATOMIC) *
953 * followed later on by kfree(). -ml *
955 **************************************************************/
957 static struct request *idetape_next_rq_storage (ide_drive_t *drive)
959 idetape_tape_t *tape = drive->driver_data;
961 #if IDETAPE_DEBUG_LOG
962 if (tape->debug_level >= 5)
963 printk(KERN_INFO "ide-tape: rq_stack_index=%d\n",
964 tape->rq_stack_index);
965 #endif /* IDETAPE_DEBUG_LOG */
966 if (tape->rq_stack_index == IDETAPE_PC_STACK)
967 tape->rq_stack_index=0;
968 return (&tape->rq_stack[tape->rq_stack_index++]);
972 * idetape_init_pc initializes a packet command.
974 static void idetape_init_pc (idetape_pc_t *pc)
976 memset(pc->c, 0, 12);
979 pc->request_transfer = 0;
980 pc->buffer = pc->pc_buffer;
981 pc->buffer_size = IDETAPE_PC_BUFFER_SIZE;
987 * called on each failed packet command retry to analyze the request sense. We
988 * currently do not utilize this information.
990 static void idetape_analyze_error(ide_drive_t *drive, u8 *sense)
992 idetape_tape_t *tape = drive->driver_data;
993 idetape_pc_t *pc = tape->failed_pc;
995 tape->sense_key = sense[2] & 0xF;
996 tape->asc = sense[12];
997 tape->ascq = sense[13];
998 #if IDETAPE_DEBUG_LOG
1000 * Without debugging, we only log an error if we decided to give up
1003 if (tape->debug_level >= 1)
1004 printk(KERN_INFO "ide-tape: pc = %x, sense key = %x, "
1005 "asc = %x, ascq = %x\n",
1006 pc->c[0], tape->sense_key,
1007 tape->asc, tape->ascq);
1008 #endif /* IDETAPE_DEBUG_LOG */
1010 /* Correct pc->actually_transferred by asking the tape. */
1011 if (test_bit(PC_DMA_ERROR, &pc->flags)) {
1012 pc->actually_transferred = pc->request_transfer -
1013 tape->tape_block_size *
1014 ntohl(get_unaligned((u32 *)&sense[3]));
1015 idetape_update_buffers(pc);
1019 * If error was the result of a zero-length read or write command,
1020 * with sense key=5, asc=0x22, ascq=0, let it slide. Some drives
1021 * (i.e. Seagate STT3401A Travan) don't support 0-length read/writes.
1023 if ((pc->c[0] == IDETAPE_READ_CMD || pc->c[0] == IDETAPE_WRITE_CMD)
1025 && pc->c[4] == 0 && pc->c[3] == 0 && pc->c[2] == 0) {
1026 if (tape->sense_key == 5) {
1027 /* don't report an error, everything's ok */
1029 /* don't retry read/write */
1030 set_bit(PC_ABORT, &pc->flags);
1033 if (pc->c[0] == IDETAPE_READ_CMD && (sense[2] & 0x80)) {
1034 pc->error = IDETAPE_ERROR_FILEMARK;
1035 set_bit(PC_ABORT, &pc->flags);
1037 if (pc->c[0] == IDETAPE_WRITE_CMD) {
1038 if ((sense[2] & 0x40) || (tape->sense_key == 0xd
1039 && tape->asc == 0x0 && tape->ascq == 0x2)) {
1040 pc->error = IDETAPE_ERROR_EOD;
1041 set_bit(PC_ABORT, &pc->flags);
1044 if (pc->c[0] == IDETAPE_READ_CMD || pc->c[0] == IDETAPE_WRITE_CMD) {
1045 if (tape->sense_key == 8) {
1046 pc->error = IDETAPE_ERROR_EOD;
1047 set_bit(PC_ABORT, &pc->flags);
1049 if (!test_bit(PC_ABORT, &pc->flags) &&
1050 pc->actually_transferred)
1051 pc->retries = IDETAPE_MAX_PC_RETRIES + 1;
1056 * idetape_active_next_stage will declare the next stage as "active".
1058 static void idetape_active_next_stage (ide_drive_t *drive)
1060 idetape_tape_t *tape = drive->driver_data;
1061 idetape_stage_t *stage = tape->next_stage;
1062 struct request *rq = &stage->rq;
1064 #if IDETAPE_DEBUG_LOG
1065 if (tape->debug_level >= 4)
1066 printk(KERN_INFO "ide-tape: Reached idetape_active_next_stage\n");
1067 #endif /* IDETAPE_DEBUG_LOG */
1068 #if IDETAPE_DEBUG_BUGS
1069 if (stage == NULL) {
1070 printk(KERN_ERR "ide-tape: bug: Trying to activate a non existing stage\n");
1073 #endif /* IDETAPE_DEBUG_BUGS */
1075 rq->rq_disk = tape->disk;
1077 rq->special = (void *)stage->bh;
1078 tape->active_data_request = rq;
1079 tape->active_stage = stage;
1080 tape->next_stage = stage->next;
1084 * idetape_increase_max_pipeline_stages is a part of the feedback
1085 * loop which tries to find the optimum number of stages. In the
1086 * feedback loop, we are starting from a minimum maximum number of
1087 * stages, and if we sense that the pipeline is empty, we try to
1088 * increase it, until we reach the user compile time memory limit.
1090 static void idetape_increase_max_pipeline_stages (ide_drive_t *drive)
1092 idetape_tape_t *tape = drive->driver_data;
1093 int increase = (tape->max_pipeline - tape->min_pipeline) / 10;
1095 #if IDETAPE_DEBUG_LOG
1096 if (tape->debug_level >= 4)
1097 printk (KERN_INFO "ide-tape: Reached idetape_increase_max_pipeline_stages\n");
1098 #endif /* IDETAPE_DEBUG_LOG */
1100 tape->max_stages += max(increase, 1);
1101 tape->max_stages = max(tape->max_stages, tape->min_pipeline);
1102 tape->max_stages = min(tape->max_stages, tape->max_pipeline);
1106 * idetape_kfree_stage calls kfree to completely free a stage, along with
1107 * its related buffers.
1109 static void __idetape_kfree_stage (idetape_stage_t *stage)
1111 struct idetape_bh *prev_bh, *bh = stage->bh;
1114 while (bh != NULL) {
1115 if (bh->b_data != NULL) {
1116 size = (int) bh->b_size;
1118 free_page((unsigned long) bh->b_data);
1120 bh->b_data += PAGE_SIZE;
1130 static void idetape_kfree_stage (idetape_tape_t *tape, idetape_stage_t *stage)
1132 __idetape_kfree_stage(stage);
1136 * idetape_remove_stage_head removes tape->first_stage from the pipeline.
1137 * The caller should avoid race conditions.
1139 static void idetape_remove_stage_head (ide_drive_t *drive)
1141 idetape_tape_t *tape = drive->driver_data;
1142 idetape_stage_t *stage;
1144 #if IDETAPE_DEBUG_LOG
1145 if (tape->debug_level >= 4)
1146 printk(KERN_INFO "ide-tape: Reached idetape_remove_stage_head\n");
1147 #endif /* IDETAPE_DEBUG_LOG */
1148 #if IDETAPE_DEBUG_BUGS
1149 if (tape->first_stage == NULL) {
1150 printk(KERN_ERR "ide-tape: bug: tape->first_stage is NULL\n");
1153 if (tape->active_stage == tape->first_stage) {
1154 printk(KERN_ERR "ide-tape: bug: Trying to free our active pipeline stage\n");
1157 #endif /* IDETAPE_DEBUG_BUGS */
1158 stage = tape->first_stage;
1159 tape->first_stage = stage->next;
1160 idetape_kfree_stage(tape, stage);
1162 if (tape->first_stage == NULL) {
1163 tape->last_stage = NULL;
1164 #if IDETAPE_DEBUG_BUGS
1165 if (tape->next_stage != NULL)
1166 printk(KERN_ERR "ide-tape: bug: tape->next_stage != NULL\n");
1167 if (tape->nr_stages)
1168 printk(KERN_ERR "ide-tape: bug: nr_stages should be 0 now\n");
1169 #endif /* IDETAPE_DEBUG_BUGS */
1174 * This will free all the pipeline stages starting from new_last_stage->next
1175 * to the end of the list, and point tape->last_stage to new_last_stage.
1177 static void idetape_abort_pipeline(ide_drive_t *drive,
1178 idetape_stage_t *new_last_stage)
1180 idetape_tape_t *tape = drive->driver_data;
1181 idetape_stage_t *stage = new_last_stage->next;
1182 idetape_stage_t *nstage;
1184 #if IDETAPE_DEBUG_LOG
1185 if (tape->debug_level >= 4)
1186 printk(KERN_INFO "ide-tape: %s: idetape_abort_pipeline called\n", tape->name);
1189 nstage = stage->next;
1190 idetape_kfree_stage(tape, stage);
1192 --tape->nr_pending_stages;
1196 new_last_stage->next = NULL;
1197 tape->last_stage = new_last_stage;
1198 tape->next_stage = NULL;
1202 * idetape_end_request is used to finish servicing a request, and to
1203 * insert a pending pipeline request into the main device queue.
1205 static int idetape_end_request(ide_drive_t *drive, int uptodate, int nr_sects)
1207 struct request *rq = HWGROUP(drive)->rq;
1208 idetape_tape_t *tape = drive->driver_data;
1209 unsigned long flags;
1211 int remove_stage = 0;
1212 idetape_stage_t *active_stage;
1214 #if IDETAPE_DEBUG_LOG
1215 if (tape->debug_level >= 4)
1216 printk(KERN_INFO "ide-tape: Reached idetape_end_request\n");
1217 #endif /* IDETAPE_DEBUG_LOG */
1220 case 0: error = IDETAPE_ERROR_GENERAL; break;
1221 case 1: error = 0; break;
1222 default: error = uptodate;
1226 tape->failed_pc = NULL;
1228 if (!blk_special_request(rq)) {
1229 ide_end_request(drive, uptodate, nr_sects);
1233 spin_lock_irqsave(&tape->spinlock, flags);
1235 /* The request was a pipelined data transfer request */
1236 if (tape->active_data_request == rq) {
1237 active_stage = tape->active_stage;
1238 tape->active_stage = NULL;
1239 tape->active_data_request = NULL;
1240 tape->nr_pending_stages--;
1241 if (rq->cmd[0] & REQ_IDETAPE_WRITE) {
1244 set_bit(IDETAPE_PIPELINE_ERROR, &tape->flags);
1245 if (error == IDETAPE_ERROR_EOD)
1246 idetape_abort_pipeline(drive, active_stage);
1248 } else if (rq->cmd[0] & REQ_IDETAPE_READ) {
1249 if (error == IDETAPE_ERROR_EOD) {
1250 set_bit(IDETAPE_PIPELINE_ERROR, &tape->flags);
1251 idetape_abort_pipeline(drive, active_stage);
1254 if (tape->next_stage != NULL) {
1255 idetape_active_next_stage(drive);
1258 * Insert the next request into the request queue.
1260 (void) ide_do_drive_cmd(drive, tape->active_data_request, ide_end);
1261 } else if (!error) {
1262 idetape_increase_max_pipeline_stages(drive);
1265 ide_end_drive_cmd(drive, 0, 0);
1266 // blkdev_dequeue_request(rq);
1267 // drive->rq = NULL;
1268 // end_that_request_last(rq);
1271 idetape_remove_stage_head(drive);
1272 if (tape->active_data_request == NULL)
1273 clear_bit(IDETAPE_PIPELINE_ACTIVE, &tape->flags);
1274 spin_unlock_irqrestore(&tape->spinlock, flags);
1278 static ide_startstop_t idetape_request_sense_callback (ide_drive_t *drive)
1280 idetape_tape_t *tape = drive->driver_data;
1282 #if IDETAPE_DEBUG_LOG
1283 if (tape->debug_level >= 4)
1284 printk(KERN_INFO "ide-tape: Reached idetape_request_sense_callback\n");
1285 #endif /* IDETAPE_DEBUG_LOG */
1286 if (!tape->pc->error) {
1287 idetape_analyze_error(drive, tape->pc->buffer);
1288 idetape_end_request(drive, 1, 0);
1290 printk(KERN_ERR "ide-tape: Error in REQUEST SENSE itself - Aborting request!\n");
1291 idetape_end_request(drive, 0, 0);
1296 static void idetape_create_request_sense_cmd (idetape_pc_t *pc)
1298 idetape_init_pc(pc);
1299 pc->c[0] = IDETAPE_REQUEST_SENSE_CMD;
1301 pc->request_transfer = 20;
1302 pc->callback = &idetape_request_sense_callback;
1305 static void idetape_init_rq(struct request *rq, u8 cmd)
1307 memset(rq, 0, sizeof(*rq));
1308 rq->cmd_type = REQ_TYPE_SPECIAL;
1313 * idetape_queue_pc_head generates a new packet command request in front
1314 * of the request queue, before the current request, so that it will be
1315 * processed immediately, on the next pass through the driver.
1317 * idetape_queue_pc_head is called from the request handling part of
1318 * the driver (the "bottom" part). Safe storage for the request should
1319 * be allocated with idetape_next_pc_storage and idetape_next_rq_storage
1320 * before calling idetape_queue_pc_head.
1322 * Memory for those requests is pre-allocated at initialization time, and
1323 * is limited to IDETAPE_PC_STACK requests. We assume that we have enough
1324 * space for the maximum possible number of inter-dependent packet commands.
1326 * The higher level of the driver - The ioctl handler and the character
1327 * device handling functions should queue request to the lower level part
1328 * and wait for their completion using idetape_queue_pc_tail or
1329 * idetape_queue_rw_tail.
1331 static void idetape_queue_pc_head (ide_drive_t *drive, idetape_pc_t *pc,struct request *rq)
1333 struct ide_tape_obj *tape = drive->driver_data;
1335 idetape_init_rq(rq, REQ_IDETAPE_PC1);
1336 rq->buffer = (char *) pc;
1337 rq->rq_disk = tape->disk;
1338 (void) ide_do_drive_cmd(drive, rq, ide_preempt);
1342 * idetape_retry_pc is called when an error was detected during the
1343 * last packet command. We queue a request sense packet command in
1344 * the head of the request list.
1346 static ide_startstop_t idetape_retry_pc (ide_drive_t *drive)
1348 idetape_tape_t *tape = drive->driver_data;
1352 (void)drive->hwif->INB(IDE_ERROR_REG);
1353 pc = idetape_next_pc_storage(drive);
1354 rq = idetape_next_rq_storage(drive);
1355 idetape_create_request_sense_cmd(pc);
1356 set_bit(IDETAPE_IGNORE_DSC, &tape->flags);
1357 idetape_queue_pc_head(drive, pc, rq);
1362 * idetape_postpone_request postpones the current request so that
1363 * ide.c will be able to service requests from another device on
1364 * the same hwgroup while we are polling for DSC.
1366 static void idetape_postpone_request (ide_drive_t *drive)
1368 idetape_tape_t *tape = drive->driver_data;
1370 #if IDETAPE_DEBUG_LOG
1371 if (tape->debug_level >= 4)
1372 printk(KERN_INFO "ide-tape: idetape_postpone_request\n");
1374 tape->postponed_rq = HWGROUP(drive)->rq;
1375 ide_stall_queue(drive, tape->dsc_polling_frequency);
1379 * idetape_pc_intr is the usual interrupt handler which will be called
1380 * during a packet command. We will transfer some of the data (as
1381 * requested by the drive) and will re-point interrupt handler to us.
1382 * When data transfer is finished, we will act according to the
1383 * algorithm described before idetape_issue_packet_command.
1386 static ide_startstop_t idetape_pc_intr (ide_drive_t *drive)
1388 ide_hwif_t *hwif = drive->hwif;
1389 idetape_tape_t *tape = drive->driver_data;
1390 idetape_pc_t *pc = tape->pc;
1393 static int error_sim_count = 0;
1398 #if IDETAPE_DEBUG_LOG
1399 if (tape->debug_level >= 4)
1400 printk(KERN_INFO "ide-tape: Reached idetape_pc_intr "
1401 "interrupt handler\n");
1402 #endif /* IDETAPE_DEBUG_LOG */
1404 /* Clear the interrupt */
1405 stat = hwif->INB(IDE_STATUS_REG);
1407 if (test_bit(PC_DMA_IN_PROGRESS, &pc->flags)) {
1408 if (hwif->ide_dma_end(drive) || (stat & ERR_STAT)) {
1410 * A DMA error is sometimes expected. For example,
1411 * if the tape is crossing a filemark during a
1412 * READ command, it will issue an irq and position
1413 * itself before the filemark, so that only a partial
1414 * data transfer will occur (which causes the DMA
1415 * error). In that case, we will later ask the tape
1416 * how much bytes of the original request were
1417 * actually transferred (we can't receive that
1418 * information from the DMA engine on most chipsets).
1422 * On the contrary, a DMA error is never expected;
1423 * it usually indicates a hardware error or abort.
1424 * If the tape crosses a filemark during a READ
1425 * command, it will issue an irq and position itself
1426 * after the filemark (not before). Only a partial
1427 * data transfer will occur, but no DMA error.
1430 set_bit(PC_DMA_ERROR, &pc->flags);
1432 pc->actually_transferred = pc->request_transfer;
1433 idetape_update_buffers(pc);
1435 #if IDETAPE_DEBUG_LOG
1436 if (tape->debug_level >= 4)
1437 printk(KERN_INFO "ide-tape: DMA finished\n");
1438 #endif /* IDETAPE_DEBUG_LOG */
1441 /* No more interrupts */
1442 if ((stat & DRQ_STAT) == 0) {
1443 #if IDETAPE_DEBUG_LOG
1444 if (tape->debug_level >= 2)
1445 printk(KERN_INFO "ide-tape: Packet command completed, %d bytes transferred\n", pc->actually_transferred);
1446 #endif /* IDETAPE_DEBUG_LOG */
1447 clear_bit(PC_DMA_IN_PROGRESS, &pc->flags);
1452 if ((pc->c[0] == IDETAPE_WRITE_CMD ||
1453 pc->c[0] == IDETAPE_READ_CMD) &&
1454 (++error_sim_count % 100) == 0) {
1455 printk(KERN_INFO "ide-tape: %s: simulating error\n",
1460 if ((stat & ERR_STAT) && pc->c[0] == IDETAPE_REQUEST_SENSE_CMD)
1462 if ((stat & ERR_STAT) || test_bit(PC_DMA_ERROR, &pc->flags)) {
1463 /* Error detected */
1464 #if IDETAPE_DEBUG_LOG
1465 if (tape->debug_level >= 1)
1466 printk(KERN_INFO "ide-tape: %s: I/O error\n",
1468 #endif /* IDETAPE_DEBUG_LOG */
1469 if (pc->c[0] == IDETAPE_REQUEST_SENSE_CMD) {
1470 printk(KERN_ERR "ide-tape: I/O error in request sense command\n");
1471 return ide_do_reset(drive);
1473 #if IDETAPE_DEBUG_LOG
1474 if (tape->debug_level >= 1)
1475 printk(KERN_INFO "ide-tape: [cmd %x]: check condition\n", pc->c[0]);
1477 /* Retry operation */
1478 return idetape_retry_pc(drive);
1481 if (test_bit(PC_WAIT_FOR_DSC, &pc->flags) &&
1482 (stat & SEEK_STAT) == 0) {
1483 /* Media access command */
1484 tape->dsc_polling_start = jiffies;
1485 tape->dsc_polling_frequency = IDETAPE_DSC_MA_FAST;
1486 tape->dsc_timeout = jiffies + IDETAPE_DSC_MA_TIMEOUT;
1487 /* Allow ide.c to handle other requests */
1488 idetape_postpone_request(drive);
1491 if (tape->failed_pc == pc)
1492 tape->failed_pc = NULL;
1493 /* Command finished - Call the callback function */
1494 return pc->callback(drive);
1496 if (test_and_clear_bit(PC_DMA_IN_PROGRESS, &pc->flags)) {
1497 printk(KERN_ERR "ide-tape: The tape wants to issue more "
1498 "interrupts in DMA mode\n");
1499 printk(KERN_ERR "ide-tape: DMA disabled, reverting to PIO\n");
1501 return ide_do_reset(drive);
1503 /* Get the number of bytes to transfer on this interrupt. */
1504 bcount = (hwif->INB(IDE_BCOUNTH_REG) << 8) |
1505 hwif->INB(IDE_BCOUNTL_REG);
1507 ireason = hwif->INB(IDE_IREASON_REG);
1510 printk(KERN_ERR "ide-tape: CoD != 0 in idetape_pc_intr\n");
1511 return ide_do_reset(drive);
1513 if (((ireason & IO) == IO) == test_bit(PC_WRITING, &pc->flags)) {
1514 /* Hopefully, we will never get here */
1515 printk(KERN_ERR "ide-tape: We wanted to %s, ",
1516 (ireason & IO) ? "Write" : "Read");
1517 printk(KERN_ERR "ide-tape: but the tape wants us to %s !\n",
1518 (ireason & IO) ? "Read" : "Write");
1519 return ide_do_reset(drive);
1521 if (!test_bit(PC_WRITING, &pc->flags)) {
1522 /* Reading - Check that we have enough space */
1523 temp = pc->actually_transferred + bcount;
1524 if (temp > pc->request_transfer) {
1525 if (temp > pc->buffer_size) {
1526 printk(KERN_ERR "ide-tape: The tape wants to send us more data than expected - discarding data\n");
1527 idetape_discard_data(drive, bcount);
1528 ide_set_handler(drive, &idetape_pc_intr, IDETAPE_WAIT_CMD, NULL);
1531 #if IDETAPE_DEBUG_LOG
1532 if (tape->debug_level >= 2)
1533 printk(KERN_NOTICE "ide-tape: The tape wants to send us more data than expected - allowing transfer\n");
1534 #endif /* IDETAPE_DEBUG_LOG */
1537 if (test_bit(PC_WRITING, &pc->flags)) {
1539 idetape_output_buffers(drive, pc, bcount);
1541 /* Write the current buffer */
1542 hwif->atapi_output_bytes(drive, pc->current_position,
1546 idetape_input_buffers(drive, pc, bcount);
1548 /* Read the current buffer */
1549 hwif->atapi_input_bytes(drive, pc->current_position,
1552 /* Update the current position */
1553 pc->actually_transferred += bcount;
1554 pc->current_position += bcount;
1555 #if IDETAPE_DEBUG_LOG
1556 if (tape->debug_level >= 2)
1557 printk(KERN_INFO "ide-tape: [cmd %x] transferred %d bytes "
1558 "on that interrupt\n", pc->c[0], bcount);
1560 /* And set the interrupt handler again */
1561 ide_set_handler(drive, &idetape_pc_intr, IDETAPE_WAIT_CMD, NULL);
1566 * Packet Command Interface
1568 * The current Packet Command is available in tape->pc, and will not
1569 * change until we finish handling it. Each packet command is associated
1570 * with a callback function that will be called when the command is
1573 * The handling will be done in three stages:
1575 * 1. idetape_issue_packet_command will send the packet command to the
1576 * drive, and will set the interrupt handler to idetape_pc_intr.
1578 * 2. On each interrupt, idetape_pc_intr will be called. This step
1579 * will be repeated until the device signals us that no more
1580 * interrupts will be issued.
1582 * 3. ATAPI Tape media access commands have immediate status with a
1583 * delayed process. In case of a successful initiation of a
1584 * media access packet command, the DSC bit will be set when the
1585 * actual execution of the command is finished.
1586 * Since the tape drive will not issue an interrupt, we have to
1587 * poll for this event. In this case, we define the request as
1588 * "low priority request" by setting rq_status to
1589 * IDETAPE_RQ_POSTPONED, set a timer to poll for DSC and exit
1592 * ide.c will then give higher priority to requests which
1593 * originate from the other device, until will change rq_status
1596 * 4. When the packet command is finished, it will be checked for errors.
1598 * 5. In case an error was found, we queue a request sense packet
1599 * command in front of the request queue and retry the operation
1600 * up to IDETAPE_MAX_PC_RETRIES times.
1602 * 6. In case no error was found, or we decided to give up and not
1603 * to retry again, the callback function will be called and then
1604 * we will handle the next request.
1607 static ide_startstop_t idetape_transfer_pc(ide_drive_t *drive)
1609 ide_hwif_t *hwif = drive->hwif;
1610 idetape_tape_t *tape = drive->driver_data;
1611 idetape_pc_t *pc = tape->pc;
1613 ide_startstop_t startstop;
1616 if (ide_wait_stat(&startstop,drive,DRQ_STAT,BUSY_STAT,WAIT_READY)) {
1617 printk(KERN_ERR "ide-tape: Strange, packet command initiated yet DRQ isn't asserted\n");
1620 ireason = hwif->INB(IDE_IREASON_REG);
1621 while (retries-- && ((ireason & CD) == 0 || (ireason & IO))) {
1622 printk(KERN_ERR "ide-tape: (IO,CoD != (0,1) while issuing "
1623 "a packet command, retrying\n");
1625 ireason = hwif->INB(IDE_IREASON_REG);
1627 printk(KERN_ERR "ide-tape: (IO,CoD != (0,1) while "
1628 "issuing a packet command, ignoring\n");
1633 if ((ireason & CD) == 0 || (ireason & IO)) {
1634 printk(KERN_ERR "ide-tape: (IO,CoD) != (0,1) while issuing "
1635 "a packet command\n");
1636 return ide_do_reset(drive);
1638 /* Set the interrupt routine */
1639 ide_set_handler(drive, &idetape_pc_intr, IDETAPE_WAIT_CMD, NULL);
1640 #ifdef CONFIG_BLK_DEV_IDEDMA
1641 /* Begin DMA, if necessary */
1642 if (test_bit(PC_DMA_IN_PROGRESS, &pc->flags))
1643 hwif->dma_start(drive);
1645 /* Send the actual packet */
1646 HWIF(drive)->atapi_output_bytes(drive, pc->c, 12);
1650 static ide_startstop_t idetape_issue_packet_command (ide_drive_t *drive, idetape_pc_t *pc)
1652 ide_hwif_t *hwif = drive->hwif;
1653 idetape_tape_t *tape = drive->driver_data;
1657 #if IDETAPE_DEBUG_BUGS
1658 if (tape->pc->c[0] == IDETAPE_REQUEST_SENSE_CMD &&
1659 pc->c[0] == IDETAPE_REQUEST_SENSE_CMD) {
1660 printk(KERN_ERR "ide-tape: possible ide-tape.c bug - "
1661 "Two request sense in serial were issued\n");
1663 #endif /* IDETAPE_DEBUG_BUGS */
1665 if (tape->failed_pc == NULL && pc->c[0] != IDETAPE_REQUEST_SENSE_CMD)
1666 tape->failed_pc = pc;
1667 /* Set the current packet command */
1670 if (pc->retries > IDETAPE_MAX_PC_RETRIES ||
1671 test_bit(PC_ABORT, &pc->flags)) {
1673 * We will "abort" retrying a packet command in case
1674 * a legitimate error code was received (crossing a
1675 * filemark, or end of the media, for example).
1677 if (!test_bit(PC_ABORT, &pc->flags)) {
1678 if (!(pc->c[0] == IDETAPE_TEST_UNIT_READY_CMD &&
1679 tape->sense_key == 2 && tape->asc == 4 &&
1680 (tape->ascq == 1 || tape->ascq == 8))) {
1681 printk(KERN_ERR "ide-tape: %s: I/O error, "
1682 "pc = %2x, key = %2x, "
1683 "asc = %2x, ascq = %2x\n",
1684 tape->name, pc->c[0],
1685 tape->sense_key, tape->asc,
1689 pc->error = IDETAPE_ERROR_GENERAL;
1691 tape->failed_pc = NULL;
1692 return pc->callback(drive);
1694 #if IDETAPE_DEBUG_LOG
1695 if (tape->debug_level >= 2)
1696 printk(KERN_INFO "ide-tape: Retry number - %d, cmd = %02X\n", pc->retries, pc->c[0]);
1697 #endif /* IDETAPE_DEBUG_LOG */
1700 /* We haven't transferred any data yet */
1701 pc->actually_transferred = 0;
1702 pc->current_position = pc->buffer;
1703 /* Request to transfer the entire buffer at once */
1704 bcount = pc->request_transfer;
1706 if (test_and_clear_bit(PC_DMA_ERROR, &pc->flags)) {
1707 printk(KERN_WARNING "ide-tape: DMA disabled, "
1708 "reverting to PIO\n");
1711 if (test_bit(PC_DMA_RECOMMENDED, &pc->flags) && drive->using_dma)
1712 dma_ok = !hwif->dma_setup(drive);
1714 ide_pktcmd_tf_load(drive, IDE_TFLAG_NO_SELECT_MASK |
1715 IDE_TFLAG_OUT_DEVICE, bcount, dma_ok);
1717 if (dma_ok) /* Will begin DMA later */
1718 set_bit(PC_DMA_IN_PROGRESS, &pc->flags);
1719 if (test_bit(IDETAPE_DRQ_INTERRUPT, &tape->flags)) {
1720 ide_execute_command(drive, WIN_PACKETCMD, &idetape_transfer_pc,
1721 IDETAPE_WAIT_CMD, NULL);
1724 hwif->OUTB(WIN_PACKETCMD, IDE_COMMAND_REG);
1725 return idetape_transfer_pc(drive);
1730 * General packet command callback function.
1732 static ide_startstop_t idetape_pc_callback (ide_drive_t *drive)
1734 idetape_tape_t *tape = drive->driver_data;
1736 #if IDETAPE_DEBUG_LOG
1737 if (tape->debug_level >= 4)
1738 printk(KERN_INFO "ide-tape: Reached idetape_pc_callback\n");
1739 #endif /* IDETAPE_DEBUG_LOG */
1741 idetape_end_request(drive, tape->pc->error ? 0 : 1, 0);
1746 * A mode sense command is used to "sense" tape parameters.
1748 static void idetape_create_mode_sense_cmd (idetape_pc_t *pc, u8 page_code)
1750 idetape_init_pc(pc);
1751 pc->c[0] = IDETAPE_MODE_SENSE_CMD;
1752 if (page_code != IDETAPE_BLOCK_DESCRIPTOR)
1753 pc->c[1] = 8; /* DBD = 1 - Don't return block descriptors */
1754 pc->c[2] = page_code;
1756 * Changed pc->c[3] to 0 (255 will at best return unused info).
1758 * For SCSI this byte is defined as subpage instead of high byte
1759 * of length and some IDE drives seem to interpret it this way
1760 * and return an error when 255 is used.
1763 pc->c[4] = 255; /* (We will just discard data in that case) */
1764 if (page_code == IDETAPE_BLOCK_DESCRIPTOR)
1765 pc->request_transfer = 12;
1766 else if (page_code == IDETAPE_CAPABILITIES_PAGE)
1767 pc->request_transfer = 24;
1769 pc->request_transfer = 50;
1770 pc->callback = &idetape_pc_callback;
1773 static void calculate_speeds(ide_drive_t *drive)
1775 idetape_tape_t *tape = drive->driver_data;
1776 int full = 125, empty = 75;
1778 if (time_after(jiffies, tape->controlled_pipeline_head_time + 120 * HZ)) {
1779 tape->controlled_previous_pipeline_head = tape->controlled_last_pipeline_head;
1780 tape->controlled_previous_head_time = tape->controlled_pipeline_head_time;
1781 tape->controlled_last_pipeline_head = tape->pipeline_head;
1782 tape->controlled_pipeline_head_time = jiffies;
1784 if (time_after(jiffies, tape->controlled_pipeline_head_time + 60 * HZ))
1785 tape->controlled_pipeline_head_speed = (tape->pipeline_head - tape->controlled_last_pipeline_head) * 32 * HZ / (jiffies - tape->controlled_pipeline_head_time);
1786 else if (time_after(jiffies, tape->controlled_previous_head_time))
1787 tape->controlled_pipeline_head_speed = (tape->pipeline_head - tape->controlled_previous_pipeline_head) * 32 * HZ / (jiffies - tape->controlled_previous_head_time);
1789 if (tape->nr_pending_stages < tape->max_stages /*- 1 */) {
1790 /* -1 for read mode error recovery */
1791 if (time_after(jiffies, tape->uncontrolled_previous_head_time + 10 * HZ)) {
1792 tape->uncontrolled_pipeline_head_time = jiffies;
1793 tape->uncontrolled_pipeline_head_speed = (tape->pipeline_head - tape->uncontrolled_previous_pipeline_head) * 32 * HZ / (jiffies - tape->uncontrolled_previous_head_time);
1796 tape->uncontrolled_previous_head_time = jiffies;
1797 tape->uncontrolled_previous_pipeline_head = tape->pipeline_head;
1798 if (time_after(jiffies, tape->uncontrolled_pipeline_head_time + 30 * HZ)) {
1799 tape->uncontrolled_pipeline_head_time = jiffies;
1802 tape->pipeline_head_speed = max(tape->uncontrolled_pipeline_head_speed, tape->controlled_pipeline_head_speed);
1803 if (tape->speed_control == 0) {
1804 tape->max_insert_speed = 5000;
1805 } else if (tape->speed_control == 1) {
1806 if (tape->nr_pending_stages >= tape->max_stages / 2)
1807 tape->max_insert_speed = tape->pipeline_head_speed +
1808 (1100 - tape->pipeline_head_speed) * 2 * (tape->nr_pending_stages - tape->max_stages / 2) / tape->max_stages;
1810 tape->max_insert_speed = 500 +
1811 (tape->pipeline_head_speed - 500) * 2 * tape->nr_pending_stages / tape->max_stages;
1812 if (tape->nr_pending_stages >= tape->max_stages * 99 / 100)
1813 tape->max_insert_speed = 5000;
1814 } else if (tape->speed_control == 2) {
1815 tape->max_insert_speed = tape->pipeline_head_speed * empty / 100 +
1816 (tape->pipeline_head_speed * full / 100 - tape->pipeline_head_speed * empty / 100) * tape->nr_pending_stages / tape->max_stages;
1818 tape->max_insert_speed = tape->speed_control;
1819 tape->max_insert_speed = max(tape->max_insert_speed, 500);
1822 static ide_startstop_t idetape_media_access_finished (ide_drive_t *drive)
1824 idetape_tape_t *tape = drive->driver_data;
1825 idetape_pc_t *pc = tape->pc;
1828 stat = drive->hwif->INB(IDE_STATUS_REG);
1829 if (stat & SEEK_STAT) {
1830 if (stat & ERR_STAT) {
1831 /* Error detected */
1832 if (pc->c[0] != IDETAPE_TEST_UNIT_READY_CMD)
1833 printk(KERN_ERR "ide-tape: %s: I/O error, ",
1835 /* Retry operation */
1836 return idetape_retry_pc(drive);
1839 if (tape->failed_pc == pc)
1840 tape->failed_pc = NULL;
1842 pc->error = IDETAPE_ERROR_GENERAL;
1843 tape->failed_pc = NULL;
1845 return pc->callback(drive);
1848 static ide_startstop_t idetape_rw_callback (ide_drive_t *drive)
1850 idetape_tape_t *tape = drive->driver_data;
1851 struct request *rq = HWGROUP(drive)->rq;
1852 int blocks = tape->pc->actually_transferred / tape->tape_block_size;
1854 tape->avg_size += blocks * tape->tape_block_size;
1855 tape->insert_size += blocks * tape->tape_block_size;
1856 if (tape->insert_size > 1024 * 1024)
1857 tape->measure_insert_time = 1;
1858 if (tape->measure_insert_time) {
1859 tape->measure_insert_time = 0;
1860 tape->insert_time = jiffies;
1861 tape->insert_size = 0;
1863 if (time_after(jiffies, tape->insert_time))
1864 tape->insert_speed = tape->insert_size / 1024 * HZ / (jiffies - tape->insert_time);
1865 if (time_after_eq(jiffies, tape->avg_time + HZ)) {
1866 tape->avg_speed = tape->avg_size * HZ / (jiffies - tape->avg_time) / 1024;
1868 tape->avg_time = jiffies;
1871 #if IDETAPE_DEBUG_LOG
1872 if (tape->debug_level >= 4)
1873 printk(KERN_INFO "ide-tape: Reached idetape_rw_callback\n");
1874 #endif /* IDETAPE_DEBUG_LOG */
1876 tape->first_frame_position += blocks;
1877 rq->current_nr_sectors -= blocks;
1879 if (!tape->pc->error)
1880 idetape_end_request(drive, 1, 0);
1882 idetape_end_request(drive, tape->pc->error, 0);
1886 static void idetape_create_read_cmd(idetape_tape_t *tape, idetape_pc_t *pc, unsigned int length, struct idetape_bh *bh)
1888 idetape_init_pc(pc);
1889 pc->c[0] = IDETAPE_READ_CMD;
1890 put_unaligned(htonl(length), (unsigned int *) &pc->c[1]);
1892 pc->callback = &idetape_rw_callback;
1894 atomic_set(&bh->b_count, 0);
1896 pc->request_transfer = pc->buffer_size = length * tape->tape_block_size;
1897 if (pc->request_transfer == tape->stage_size)
1898 set_bit(PC_DMA_RECOMMENDED, &pc->flags);
1901 static void idetape_create_read_buffer_cmd(idetape_tape_t *tape, idetape_pc_t *pc, unsigned int length, struct idetape_bh *bh)
1904 struct idetape_bh *p = bh;
1906 idetape_init_pc(pc);
1907 pc->c[0] = IDETAPE_READ_BUFFER_CMD;
1908 pc->c[1] = IDETAPE_RETRIEVE_FAULTY_BLOCK;
1909 pc->c[7] = size >> 8;
1910 pc->c[8] = size & 0xff;
1911 pc->callback = &idetape_pc_callback;
1913 atomic_set(&bh->b_count, 0);
1916 atomic_set(&p->b_count, 0);
1919 pc->request_transfer = pc->buffer_size = size;
1922 static void idetape_create_write_cmd(idetape_tape_t *tape, idetape_pc_t *pc, unsigned int length, struct idetape_bh *bh)
1924 idetape_init_pc(pc);
1925 pc->c[0] = IDETAPE_WRITE_CMD;
1926 put_unaligned(htonl(length), (unsigned int *) &pc->c[1]);
1928 pc->callback = &idetape_rw_callback;
1929 set_bit(PC_WRITING, &pc->flags);
1931 pc->b_data = bh->b_data;
1932 pc->b_count = atomic_read(&bh->b_count);
1934 pc->request_transfer = pc->buffer_size = length * tape->tape_block_size;
1935 if (pc->request_transfer == tape->stage_size)
1936 set_bit(PC_DMA_RECOMMENDED, &pc->flags);
1940 * idetape_do_request is our request handling function.
1942 static ide_startstop_t idetape_do_request(ide_drive_t *drive,
1943 struct request *rq, sector_t block)
1945 idetape_tape_t *tape = drive->driver_data;
1946 idetape_pc_t *pc = NULL;
1947 struct request *postponed_rq = tape->postponed_rq;
1950 #if IDETAPE_DEBUG_LOG
1951 if (tape->debug_level >= 2)
1952 printk(KERN_INFO "ide-tape: sector: %ld, "
1953 "nr_sectors: %ld, current_nr_sectors: %d\n",
1954 rq->sector, rq->nr_sectors, rq->current_nr_sectors);
1955 #endif /* IDETAPE_DEBUG_LOG */
1957 if (!blk_special_request(rq)) {
1959 * We do not support buffer cache originated requests.
1961 printk(KERN_NOTICE "ide-tape: %s: Unsupported request in "
1962 "request queue (%d)\n", drive->name, rq->cmd_type);
1963 ide_end_request(drive, 0, 0);
1968 * Retry a failed packet command
1970 if (tape->failed_pc != NULL &&
1971 tape->pc->c[0] == IDETAPE_REQUEST_SENSE_CMD) {
1972 return idetape_issue_packet_command(drive, tape->failed_pc);
1974 #if IDETAPE_DEBUG_BUGS
1975 if (postponed_rq != NULL)
1976 if (rq != postponed_rq) {
1977 printk(KERN_ERR "ide-tape: ide-tape.c bug - "
1978 "Two DSC requests were queued\n");
1979 idetape_end_request(drive, 0, 0);
1982 #endif /* IDETAPE_DEBUG_BUGS */
1984 tape->postponed_rq = NULL;
1987 * If the tape is still busy, postpone our request and service
1988 * the other device meanwhile.
1990 stat = drive->hwif->INB(IDE_STATUS_REG);
1992 if (!drive->dsc_overlap && !(rq->cmd[0] & REQ_IDETAPE_PC2))
1993 set_bit(IDETAPE_IGNORE_DSC, &tape->flags);
1995 if (drive->post_reset == 1) {
1996 set_bit(IDETAPE_IGNORE_DSC, &tape->flags);
1997 drive->post_reset = 0;
2000 if (tape->tape_still_time > 100 && tape->tape_still_time < 200)
2001 tape->measure_insert_time = 1;
2002 if (time_after(jiffies, tape->insert_time))
2003 tape->insert_speed = tape->insert_size / 1024 * HZ / (jiffies - tape->insert_time);
2004 calculate_speeds(drive);
2005 if (!test_and_clear_bit(IDETAPE_IGNORE_DSC, &tape->flags) &&
2006 (stat & SEEK_STAT) == 0) {
2007 if (postponed_rq == NULL) {
2008 tape->dsc_polling_start = jiffies;
2009 tape->dsc_polling_frequency = tape->best_dsc_rw_frequency;
2010 tape->dsc_timeout = jiffies + IDETAPE_DSC_RW_TIMEOUT;
2011 } else if (time_after(jiffies, tape->dsc_timeout)) {
2012 printk(KERN_ERR "ide-tape: %s: DSC timeout\n",
2014 if (rq->cmd[0] & REQ_IDETAPE_PC2) {
2015 idetape_media_access_finished(drive);
2018 return ide_do_reset(drive);
2020 } else if (time_after(jiffies, tape->dsc_polling_start + IDETAPE_DSC_MA_THRESHOLD))
2021 tape->dsc_polling_frequency = IDETAPE_DSC_MA_SLOW;
2022 idetape_postpone_request(drive);
2025 if (rq->cmd[0] & REQ_IDETAPE_READ) {
2026 tape->buffer_head++;
2027 tape->postpone_cnt = 0;
2028 pc = idetape_next_pc_storage(drive);
2029 idetape_create_read_cmd(tape, pc, rq->current_nr_sectors, (struct idetape_bh *)rq->special);
2032 if (rq->cmd[0] & REQ_IDETAPE_WRITE) {
2033 tape->buffer_head++;
2034 tape->postpone_cnt = 0;
2035 pc = idetape_next_pc_storage(drive);
2036 idetape_create_write_cmd(tape, pc, rq->current_nr_sectors, (struct idetape_bh *)rq->special);
2039 if (rq->cmd[0] & REQ_IDETAPE_READ_BUFFER) {
2040 tape->postpone_cnt = 0;
2041 pc = idetape_next_pc_storage(drive);
2042 idetape_create_read_buffer_cmd(tape, pc, rq->current_nr_sectors, (struct idetape_bh *)rq->special);
2045 if (rq->cmd[0] & REQ_IDETAPE_PC1) {
2046 pc = (idetape_pc_t *) rq->buffer;
2047 rq->cmd[0] &= ~(REQ_IDETAPE_PC1);
2048 rq->cmd[0] |= REQ_IDETAPE_PC2;
2051 if (rq->cmd[0] & REQ_IDETAPE_PC2) {
2052 idetape_media_access_finished(drive);
2057 return idetape_issue_packet_command(drive, pc);
2061 * Pipeline related functions
2063 static inline int idetape_pipeline_active (idetape_tape_t *tape)
2067 rc1 = test_bit(IDETAPE_PIPELINE_ACTIVE, &tape->flags);
2068 rc2 = (tape->active_data_request != NULL);
2073 * idetape_kmalloc_stage uses __get_free_page to allocate a pipeline
2074 * stage, along with all the necessary small buffers which together make
2075 * a buffer of size tape->stage_size (or a bit more). We attempt to
2076 * combine sequential pages as much as possible.
2078 * Returns a pointer to the new allocated stage, or NULL if we
2079 * can't (or don't want to) allocate a stage.
2081 * Pipeline stages are optional and are used to increase performance.
2082 * If we can't allocate them, we'll manage without them.
2084 static idetape_stage_t *__idetape_kmalloc_stage (idetape_tape_t *tape, int full, int clear)
2086 idetape_stage_t *stage;
2087 struct idetape_bh *prev_bh, *bh;
2088 int pages = tape->pages_per_stage;
2089 char *b_data = NULL;
2091 if ((stage = kmalloc(sizeof (idetape_stage_t),GFP_KERNEL)) == NULL)
2095 bh = stage->bh = kmalloc(sizeof(struct idetape_bh), GFP_KERNEL);
2098 bh->b_reqnext = NULL;
2099 if ((bh->b_data = (char *) __get_free_page (GFP_KERNEL)) == NULL)
2102 memset(bh->b_data, 0, PAGE_SIZE);
2103 bh->b_size = PAGE_SIZE;
2104 atomic_set(&bh->b_count, full ? bh->b_size : 0);
2107 if ((b_data = (char *) __get_free_page (GFP_KERNEL)) == NULL)
2110 memset(b_data, 0, PAGE_SIZE);
2111 if (bh->b_data == b_data + PAGE_SIZE) {
2112 bh->b_size += PAGE_SIZE;
2113 bh->b_data -= PAGE_SIZE;
2115 atomic_add(PAGE_SIZE, &bh->b_count);
2118 if (b_data == bh->b_data + bh->b_size) {
2119 bh->b_size += PAGE_SIZE;
2121 atomic_add(PAGE_SIZE, &bh->b_count);
2125 if ((bh = kmalloc(sizeof(struct idetape_bh), GFP_KERNEL)) == NULL) {
2126 free_page((unsigned long) b_data);
2129 bh->b_reqnext = NULL;
2130 bh->b_data = b_data;
2131 bh->b_size = PAGE_SIZE;
2132 atomic_set(&bh->b_count, full ? bh->b_size : 0);
2133 prev_bh->b_reqnext = bh;
2135 bh->b_size -= tape->excess_bh_size;
2137 atomic_sub(tape->excess_bh_size, &bh->b_count);
2140 __idetape_kfree_stage(stage);
2144 static idetape_stage_t *idetape_kmalloc_stage (idetape_tape_t *tape)
2146 idetape_stage_t *cache_stage = tape->cache_stage;
2148 #if IDETAPE_DEBUG_LOG
2149 if (tape->debug_level >= 4)
2150 printk(KERN_INFO "ide-tape: Reached idetape_kmalloc_stage\n");
2151 #endif /* IDETAPE_DEBUG_LOG */
2153 if (tape->nr_stages >= tape->max_stages)
2155 if (cache_stage != NULL) {
2156 tape->cache_stage = NULL;
2159 return __idetape_kmalloc_stage(tape, 0, 0);
2162 static int idetape_copy_stage_from_user (idetape_tape_t *tape, idetape_stage_t *stage, const char __user *buf, int n)
2164 struct idetape_bh *bh = tape->bh;
2169 #if IDETAPE_DEBUG_BUGS
2171 printk(KERN_ERR "ide-tape: bh == NULL in "
2172 "idetape_copy_stage_from_user\n");
2175 #endif /* IDETAPE_DEBUG_BUGS */
2176 count = min((unsigned int)(bh->b_size - atomic_read(&bh->b_count)), (unsigned int)n);
2177 if (copy_from_user(bh->b_data + atomic_read(&bh->b_count), buf, count))
2180 atomic_add(count, &bh->b_count);
2182 if (atomic_read(&bh->b_count) == bh->b_size) {
2185 atomic_set(&bh->b_count, 0);
2192 static int idetape_copy_stage_to_user (idetape_tape_t *tape, char __user *buf, idetape_stage_t *stage, int n)
2194 struct idetape_bh *bh = tape->bh;
2199 #if IDETAPE_DEBUG_BUGS
2201 printk(KERN_ERR "ide-tape: bh == NULL in "
2202 "idetape_copy_stage_to_user\n");
2205 #endif /* IDETAPE_DEBUG_BUGS */
2206 count = min(tape->b_count, n);
2207 if (copy_to_user(buf, tape->b_data, count))
2210 tape->b_data += count;
2211 tape->b_count -= count;
2213 if (!tape->b_count) {
2214 tape->bh = bh = bh->b_reqnext;
2216 tape->b_data = bh->b_data;
2217 tape->b_count = atomic_read(&bh->b_count);
2224 static void idetape_init_merge_stage (idetape_tape_t *tape)
2226 struct idetape_bh *bh = tape->merge_stage->bh;
2229 if (tape->chrdev_direction == idetape_direction_write)
2230 atomic_set(&bh->b_count, 0);
2232 tape->b_data = bh->b_data;
2233 tape->b_count = atomic_read(&bh->b_count);
2237 static void idetape_switch_buffers (idetape_tape_t *tape, idetape_stage_t *stage)
2239 struct idetape_bh *tmp;
2242 stage->bh = tape->merge_stage->bh;
2243 tape->merge_stage->bh = tmp;
2244 idetape_init_merge_stage(tape);
2248 * idetape_add_stage_tail adds a new stage at the end of the pipeline.
2250 static void idetape_add_stage_tail (ide_drive_t *drive,idetape_stage_t *stage)
2252 idetape_tape_t *tape = drive->driver_data;
2253 unsigned long flags;
2255 #if IDETAPE_DEBUG_LOG
2256 if (tape->debug_level >= 4)
2257 printk (KERN_INFO "ide-tape: Reached idetape_add_stage_tail\n");
2258 #endif /* IDETAPE_DEBUG_LOG */
2259 spin_lock_irqsave(&tape->spinlock, flags);
2261 if (tape->last_stage != NULL)
2262 tape->last_stage->next=stage;
2264 tape->first_stage = tape->next_stage=stage;
2265 tape->last_stage = stage;
2266 if (tape->next_stage == NULL)
2267 tape->next_stage = tape->last_stage;
2269 tape->nr_pending_stages++;
2270 spin_unlock_irqrestore(&tape->spinlock, flags);
2274 * idetape_wait_for_request installs a completion in a pending request
2275 * and sleeps until it is serviced.
2277 * The caller should ensure that the request will not be serviced
2278 * before we install the completion (usually by disabling interrupts).
2280 static void idetape_wait_for_request (ide_drive_t *drive, struct request *rq)
2282 DECLARE_COMPLETION_ONSTACK(wait);
2283 idetape_tape_t *tape = drive->driver_data;
2285 #if IDETAPE_DEBUG_BUGS
2286 if (rq == NULL || !blk_special_request(rq)) {
2287 printk (KERN_ERR "ide-tape: bug: Trying to sleep on non-valid request\n");
2290 #endif /* IDETAPE_DEBUG_BUGS */
2291 rq->end_io_data = &wait;
2292 rq->end_io = blk_end_sync_rq;
2293 spin_unlock_irq(&tape->spinlock);
2294 wait_for_completion(&wait);
2295 /* The stage and its struct request have been deallocated */
2296 spin_lock_irq(&tape->spinlock);
2299 static ide_startstop_t idetape_read_position_callback (ide_drive_t *drive)
2301 idetape_tape_t *tape = drive->driver_data;
2302 idetape_read_position_result_t *result;
2304 #if IDETAPE_DEBUG_LOG
2305 if (tape->debug_level >= 4)
2306 printk(KERN_INFO "ide-tape: Reached idetape_read_position_callback\n");
2307 #endif /* IDETAPE_DEBUG_LOG */
2309 if (!tape->pc->error) {
2310 result = (idetape_read_position_result_t *) tape->pc->buffer;
2311 #if IDETAPE_DEBUG_LOG
2312 if (tape->debug_level >= 2)
2313 printk(KERN_INFO "ide-tape: BOP - %s\n",result->bop ? "Yes":"No");
2314 if (tape->debug_level >= 2)
2315 printk(KERN_INFO "ide-tape: EOP - %s\n",result->eop ? "Yes":"No");
2316 #endif /* IDETAPE_DEBUG_LOG */
2318 printk(KERN_INFO "ide-tape: Block location is unknown to the tape\n");
2319 clear_bit(IDETAPE_ADDRESS_VALID, &tape->flags);
2320 idetape_end_request(drive, 0, 0);
2322 #if IDETAPE_DEBUG_LOG
2323 if (tape->debug_level >= 2)
2324 printk(KERN_INFO "ide-tape: Block Location - %u\n", ntohl(result->first_block));
2325 #endif /* IDETAPE_DEBUG_LOG */
2326 tape->partition = result->partition;
2327 tape->first_frame_position = ntohl(result->first_block);
2328 tape->last_frame_position = ntohl(result->last_block);
2329 tape->blocks_in_buffer = result->blocks_in_buffer[2];
2330 set_bit(IDETAPE_ADDRESS_VALID, &tape->flags);
2331 idetape_end_request(drive, 1, 0);
2334 idetape_end_request(drive, 0, 0);
2340 * idetape_create_write_filemark_cmd will:
2342 * 1. Write a filemark if write_filemark=1.
2343 * 2. Flush the device buffers without writing a filemark
2344 * if write_filemark=0.
2347 static void idetape_create_write_filemark_cmd (ide_drive_t *drive, idetape_pc_t *pc,int write_filemark)
2349 idetape_init_pc(pc);
2350 pc->c[0] = IDETAPE_WRITE_FILEMARK_CMD;
2351 pc->c[4] = write_filemark;
2352 set_bit(PC_WAIT_FOR_DSC, &pc->flags);
2353 pc->callback = &idetape_pc_callback;
2356 static void idetape_create_test_unit_ready_cmd(idetape_pc_t *pc)
2358 idetape_init_pc(pc);
2359 pc->c[0] = IDETAPE_TEST_UNIT_READY_CMD;
2360 pc->callback = &idetape_pc_callback;
2364 * idetape_queue_pc_tail is based on the following functions:
2366 * ide_do_drive_cmd from ide.c
2367 * cdrom_queue_request and cdrom_queue_packet_command from ide-cd.c
2369 * We add a special packet command request to the tail of the request
2370 * queue, and wait for it to be serviced.
2372 * This is not to be called from within the request handling part
2373 * of the driver ! We allocate here data in the stack, and it is valid
2374 * until the request is finished. This is not the case for the bottom
2375 * part of the driver, where we are always leaving the functions to wait
2376 * for an interrupt or a timer event.
2378 * From the bottom part of the driver, we should allocate safe memory
2379 * using idetape_next_pc_storage and idetape_next_rq_storage, and add
2380 * the request to the request list without waiting for it to be serviced !
2381 * In that case, we usually use idetape_queue_pc_head.
2383 static int __idetape_queue_pc_tail (ide_drive_t *drive, idetape_pc_t *pc)
2385 struct ide_tape_obj *tape = drive->driver_data;
2388 idetape_init_rq(&rq, REQ_IDETAPE_PC1);
2389 rq.buffer = (char *) pc;
2390 rq.rq_disk = tape->disk;
2391 return ide_do_drive_cmd(drive, &rq, ide_wait);
2394 static void idetape_create_load_unload_cmd (ide_drive_t *drive, idetape_pc_t *pc,int cmd)
2396 idetape_init_pc(pc);
2397 pc->c[0] = IDETAPE_LOAD_UNLOAD_CMD;
2399 set_bit(PC_WAIT_FOR_DSC, &pc->flags);
2400 pc->callback = &idetape_pc_callback;
2403 static int idetape_wait_ready(ide_drive_t *drive, unsigned long timeout)
2405 idetape_tape_t *tape = drive->driver_data;
2407 int load_attempted = 0;
2410 * Wait for the tape to become ready
2412 set_bit(IDETAPE_MEDIUM_PRESENT, &tape->flags);
2414 while (time_before(jiffies, timeout)) {
2415 idetape_create_test_unit_ready_cmd(&pc);
2416 if (!__idetape_queue_pc_tail(drive, &pc))
2418 if ((tape->sense_key == 2 && tape->asc == 4 && tape->ascq == 2)
2419 || (tape->asc == 0x3A)) { /* no media */
2422 idetape_create_load_unload_cmd(drive, &pc, IDETAPE_LU_LOAD_MASK);
2423 __idetape_queue_pc_tail(drive, &pc);
2425 /* not about to be ready */
2426 } else if (!(tape->sense_key == 2 && tape->asc == 4 &&
2427 (tape->ascq == 1 || tape->ascq == 8)))
2434 static int idetape_queue_pc_tail (ide_drive_t *drive,idetape_pc_t *pc)
2436 return __idetape_queue_pc_tail(drive, pc);
2439 static int idetape_flush_tape_buffers (ide_drive_t *drive)
2444 idetape_create_write_filemark_cmd(drive, &pc, 0);
2445 if ((rc = idetape_queue_pc_tail(drive, &pc)))
2447 idetape_wait_ready(drive, 60 * 5 * HZ);
2451 static void idetape_create_read_position_cmd (idetape_pc_t *pc)
2453 idetape_init_pc(pc);
2454 pc->c[0] = IDETAPE_READ_POSITION_CMD;
2455 pc->request_transfer = 20;
2456 pc->callback = &idetape_read_position_callback;
2459 static int idetape_read_position (ide_drive_t *drive)
2461 idetape_tape_t *tape = drive->driver_data;
2465 #if IDETAPE_DEBUG_LOG
2466 if (tape->debug_level >= 4)
2467 printk(KERN_INFO "ide-tape: Reached idetape_read_position\n");
2468 #endif /* IDETAPE_DEBUG_LOG */
2470 idetape_create_read_position_cmd(&pc);
2471 if (idetape_queue_pc_tail(drive, &pc))
2473 position = tape->first_frame_position;
2477 static void idetape_create_locate_cmd (ide_drive_t *drive, idetape_pc_t *pc, unsigned int block, u8 partition, int skip)
2479 idetape_init_pc(pc);
2480 pc->c[0] = IDETAPE_LOCATE_CMD;
2482 put_unaligned(htonl(block), (unsigned int *) &pc->c[3]);
2483 pc->c[8] = partition;
2484 set_bit(PC_WAIT_FOR_DSC, &pc->flags);
2485 pc->callback = &idetape_pc_callback;
2488 static int idetape_create_prevent_cmd (ide_drive_t *drive, idetape_pc_t *pc, int prevent)
2490 idetape_tape_t *tape = drive->driver_data;
2492 if (!tape->capabilities.lock)
2495 idetape_init_pc(pc);
2496 pc->c[0] = IDETAPE_PREVENT_CMD;
2498 pc->callback = &idetape_pc_callback;
2502 static int __idetape_discard_read_pipeline (ide_drive_t *drive)
2504 idetape_tape_t *tape = drive->driver_data;
2505 unsigned long flags;
2508 if (tape->chrdev_direction != idetape_direction_read)
2511 /* Remove merge stage. */
2512 cnt = tape->merge_stage_size / tape->tape_block_size;
2513 if (test_and_clear_bit(IDETAPE_FILEMARK, &tape->flags))
2514 ++cnt; /* Filemarks count as 1 sector */
2515 tape->merge_stage_size = 0;
2516 if (tape->merge_stage != NULL) {
2517 __idetape_kfree_stage(tape->merge_stage);
2518 tape->merge_stage = NULL;
2521 /* Clear pipeline flags. */
2522 clear_bit(IDETAPE_PIPELINE_ERROR, &tape->flags);
2523 tape->chrdev_direction = idetape_direction_none;
2525 /* Remove pipeline stages. */
2526 if (tape->first_stage == NULL)
2529 spin_lock_irqsave(&tape->spinlock, flags);
2530 tape->next_stage = NULL;
2531 if (idetape_pipeline_active(tape))
2532 idetape_wait_for_request(drive, tape->active_data_request);
2533 spin_unlock_irqrestore(&tape->spinlock, flags);
2535 while (tape->first_stage != NULL) {
2536 struct request *rq_ptr = &tape->first_stage->rq;
2538 cnt += rq_ptr->nr_sectors - rq_ptr->current_nr_sectors;
2539 if (rq_ptr->errors == IDETAPE_ERROR_FILEMARK)
2541 idetape_remove_stage_head(drive);
2543 tape->nr_pending_stages = 0;
2544 tape->max_stages = tape->min_pipeline;
2549 * idetape_position_tape positions the tape to the requested block
2550 * using the LOCATE packet command. A READ POSITION command is then
2551 * issued to check where we are positioned.
2553 * Like all higher level operations, we queue the commands at the tail
2554 * of the request queue and wait for their completion.
2557 static int idetape_position_tape (ide_drive_t *drive, unsigned int block, u8 partition, int skip)
2559 idetape_tape_t *tape = drive->driver_data;
2563 if (tape->chrdev_direction == idetape_direction_read)
2564 __idetape_discard_read_pipeline(drive);
2565 idetape_wait_ready(drive, 60 * 5 * HZ);
2566 idetape_create_locate_cmd(drive, &pc, block, partition, skip);
2567 retval = idetape_queue_pc_tail(drive, &pc);
2571 idetape_create_read_position_cmd(&pc);
2572 return (idetape_queue_pc_tail(drive, &pc));
2575 static void idetape_discard_read_pipeline (ide_drive_t *drive, int restore_position)
2577 idetape_tape_t *tape = drive->driver_data;
2581 cnt = __idetape_discard_read_pipeline(drive);
2582 if (restore_position) {
2583 position = idetape_read_position(drive);
2584 seek = position > cnt ? position - cnt : 0;
2585 if (idetape_position_tape(drive, seek, 0, 0)) {
2586 printk(KERN_INFO "ide-tape: %s: position_tape failed in discard_pipeline()\n", tape->name);
2593 * idetape_queue_rw_tail generates a read/write request for the block
2594 * device interface and wait for it to be serviced.
2596 static int idetape_queue_rw_tail(ide_drive_t *drive, int cmd, int blocks, struct idetape_bh *bh)
2598 idetape_tape_t *tape = drive->driver_data;
2601 #if IDETAPE_DEBUG_LOG
2602 if (tape->debug_level >= 2)
2603 printk(KERN_INFO "ide-tape: idetape_queue_rw_tail: cmd=%d\n",cmd);
2604 #endif /* IDETAPE_DEBUG_LOG */
2605 #if IDETAPE_DEBUG_BUGS
2606 if (idetape_pipeline_active(tape)) {
2607 printk(KERN_ERR "ide-tape: bug: the pipeline is active in idetape_queue_rw_tail\n");
2610 #endif /* IDETAPE_DEBUG_BUGS */
2612 idetape_init_rq(&rq, cmd);
2613 rq.rq_disk = tape->disk;
2614 rq.special = (void *)bh;
2615 rq.sector = tape->first_frame_position;
2616 rq.nr_sectors = rq.current_nr_sectors = blocks;
2617 (void) ide_do_drive_cmd(drive, &rq, ide_wait);
2619 if ((cmd & (REQ_IDETAPE_READ | REQ_IDETAPE_WRITE)) == 0)
2622 if (tape->merge_stage)
2623 idetape_init_merge_stage(tape);
2624 if (rq.errors == IDETAPE_ERROR_GENERAL)
2626 return (tape->tape_block_size * (blocks-rq.current_nr_sectors));
2630 * idetape_insert_pipeline_into_queue is used to start servicing the
2631 * pipeline stages, starting from tape->next_stage.
2633 static void idetape_insert_pipeline_into_queue (ide_drive_t *drive)
2635 idetape_tape_t *tape = drive->driver_data;
2637 if (tape->next_stage == NULL)
2639 if (!idetape_pipeline_active(tape)) {
2640 set_bit(IDETAPE_PIPELINE_ACTIVE, &tape->flags);
2641 idetape_active_next_stage(drive);
2642 (void) ide_do_drive_cmd(drive, tape->active_data_request, ide_end);
2646 static void idetape_create_inquiry_cmd (idetape_pc_t *pc)
2648 idetape_init_pc(pc);
2649 pc->c[0] = IDETAPE_INQUIRY_CMD;
2650 pc->c[4] = pc->request_transfer = 254;
2651 pc->callback = &idetape_pc_callback;
2654 static void idetape_create_rewind_cmd (ide_drive_t *drive, idetape_pc_t *pc)
2656 idetape_init_pc(pc);
2657 pc->c[0] = IDETAPE_REWIND_CMD;
2658 set_bit(PC_WAIT_FOR_DSC, &pc->flags);
2659 pc->callback = &idetape_pc_callback;
2662 static void idetape_create_erase_cmd (idetape_pc_t *pc)
2664 idetape_init_pc(pc);
2665 pc->c[0] = IDETAPE_ERASE_CMD;
2667 set_bit(PC_WAIT_FOR_DSC, &pc->flags);
2668 pc->callback = &idetape_pc_callback;
2671 static void idetape_create_space_cmd (idetape_pc_t *pc,int count, u8 cmd)
2673 idetape_init_pc(pc);
2674 pc->c[0] = IDETAPE_SPACE_CMD;
2675 put_unaligned(htonl(count), (unsigned int *) &pc->c[1]);
2677 set_bit(PC_WAIT_FOR_DSC, &pc->flags);
2678 pc->callback = &idetape_pc_callback;
2681 static void idetape_wait_first_stage (ide_drive_t *drive)
2683 idetape_tape_t *tape = drive->driver_data;
2684 unsigned long flags;
2686 if (tape->first_stage == NULL)
2688 spin_lock_irqsave(&tape->spinlock, flags);
2689 if (tape->active_stage == tape->first_stage)
2690 idetape_wait_for_request(drive, tape->active_data_request);
2691 spin_unlock_irqrestore(&tape->spinlock, flags);
2695 * idetape_add_chrdev_write_request tries to add a character device
2696 * originated write request to our pipeline. In case we don't succeed,
2697 * we revert to non-pipelined operation mode for this request.
2699 * 1. Try to allocate a new pipeline stage.
2700 * 2. If we can't, wait for more and more requests to be serviced
2701 * and try again each time.
2702 * 3. If we still can't allocate a stage, fallback to
2703 * non-pipelined operation mode for this request.
2705 static int idetape_add_chrdev_write_request (ide_drive_t *drive, int blocks)
2707 idetape_tape_t *tape = drive->driver_data;
2708 idetape_stage_t *new_stage;
2709 unsigned long flags;
2712 #if IDETAPE_DEBUG_LOG
2713 if (tape->debug_level >= 3)
2714 printk(KERN_INFO "ide-tape: Reached idetape_add_chrdev_write_request\n");
2715 #endif /* IDETAPE_DEBUG_LOG */
2718 * Attempt to allocate a new stage.
2719 * Pay special attention to possible race conditions.
2721 while ((new_stage = idetape_kmalloc_stage(tape)) == NULL) {
2722 spin_lock_irqsave(&tape->spinlock, flags);
2723 if (idetape_pipeline_active(tape)) {
2724 idetape_wait_for_request(drive, tape->active_data_request);
2725 spin_unlock_irqrestore(&tape->spinlock, flags);
2727 spin_unlock_irqrestore(&tape->spinlock, flags);
2728 idetape_insert_pipeline_into_queue(drive);
2729 if (idetape_pipeline_active(tape))
2732 * Linux is short on memory. Fallback to
2733 * non-pipelined operation mode for this request.
2735 return idetape_queue_rw_tail(drive, REQ_IDETAPE_WRITE, blocks, tape->merge_stage->bh);
2738 rq = &new_stage->rq;
2739 idetape_init_rq(rq, REQ_IDETAPE_WRITE);
2740 /* Doesn't actually matter - We always assume sequential access */
2741 rq->sector = tape->first_frame_position;
2742 rq->nr_sectors = rq->current_nr_sectors = blocks;
2744 idetape_switch_buffers(tape, new_stage);
2745 idetape_add_stage_tail(drive, new_stage);
2746 tape->pipeline_head++;
2747 calculate_speeds(drive);
2750 * Estimate whether the tape has stopped writing by checking
2751 * if our write pipeline is currently empty. If we are not
2752 * writing anymore, wait for the pipeline to be full enough
2753 * (90%) before starting to service requests, so that we will
2754 * be able to keep up with the higher speeds of the tape.
2756 if (!idetape_pipeline_active(tape)) {
2757 if (tape->nr_stages >= tape->max_stages * 9 / 10 ||
2758 tape->nr_stages >= tape->max_stages - tape->uncontrolled_pipeline_head_speed * 3 * 1024 / tape->tape_block_size) {
2759 tape->measure_insert_time = 1;
2760 tape->insert_time = jiffies;
2761 tape->insert_size = 0;
2762 tape->insert_speed = 0;
2763 idetape_insert_pipeline_into_queue(drive);
2766 if (test_and_clear_bit(IDETAPE_PIPELINE_ERROR, &tape->flags))
2767 /* Return a deferred error */
2773 * idetape_wait_for_pipeline will wait until all pending pipeline
2774 * requests are serviced. Typically called on device close.
2776 static void idetape_wait_for_pipeline (ide_drive_t *drive)
2778 idetape_tape_t *tape = drive->driver_data;
2779 unsigned long flags;
2781 while (tape->next_stage || idetape_pipeline_active(tape)) {
2782 idetape_insert_pipeline_into_queue(drive);
2783 spin_lock_irqsave(&tape->spinlock, flags);
2784 if (idetape_pipeline_active(tape))
2785 idetape_wait_for_request(drive, tape->active_data_request);
2786 spin_unlock_irqrestore(&tape->spinlock, flags);
2790 static void idetape_empty_write_pipeline (ide_drive_t *drive)
2792 idetape_tape_t *tape = drive->driver_data;
2794 struct idetape_bh *bh;
2796 #if IDETAPE_DEBUG_BUGS
2797 if (tape->chrdev_direction != idetape_direction_write) {
2798 printk(KERN_ERR "ide-tape: bug: Trying to empty write pipeline, but we are not writing.\n");
2801 if (tape->merge_stage_size > tape->stage_size) {
2802 printk(KERN_ERR "ide-tape: bug: merge_buffer too big\n");
2803 tape->merge_stage_size = tape->stage_size;
2805 #endif /* IDETAPE_DEBUG_BUGS */
2806 if (tape->merge_stage_size) {
2807 blocks = tape->merge_stage_size / tape->tape_block_size;
2808 if (tape->merge_stage_size % tape->tape_block_size) {
2812 i = tape->tape_block_size - tape->merge_stage_size % tape->tape_block_size;
2813 bh = tape->bh->b_reqnext;
2815 atomic_set(&bh->b_count, 0);
2822 printk(KERN_INFO "ide-tape: bug, bh NULL\n");
2825 min = min(i, (unsigned int)(bh->b_size - atomic_read(&bh->b_count)));
2826 memset(bh->b_data + atomic_read(&bh->b_count), 0, min);
2827 atomic_add(min, &bh->b_count);
2832 (void) idetape_add_chrdev_write_request(drive, blocks);
2833 tape->merge_stage_size = 0;
2835 idetape_wait_for_pipeline(drive);
2836 if (tape->merge_stage != NULL) {
2837 __idetape_kfree_stage(tape->merge_stage);
2838 tape->merge_stage = NULL;
2840 clear_bit(IDETAPE_PIPELINE_ERROR, &tape->flags);
2841 tape->chrdev_direction = idetape_direction_none;
2844 * On the next backup, perform the feedback loop again.
2845 * (I don't want to keep sense information between backups,
2846 * as some systems are constantly on, and the system load
2847 * can be totally different on the next backup).
2849 tape->max_stages = tape->min_pipeline;
2850 #if IDETAPE_DEBUG_BUGS
2851 if (tape->first_stage != NULL ||
2852 tape->next_stage != NULL ||
2853 tape->last_stage != NULL ||
2854 tape->nr_stages != 0) {
2855 printk(KERN_ERR "ide-tape: ide-tape pipeline bug, "
2856 "first_stage %p, next_stage %p, "
2857 "last_stage %p, nr_stages %d\n",
2858 tape->first_stage, tape->next_stage,
2859 tape->last_stage, tape->nr_stages);
2861 #endif /* IDETAPE_DEBUG_BUGS */
2864 static void idetape_restart_speed_control (ide_drive_t *drive)
2866 idetape_tape_t *tape = drive->driver_data;
2868 tape->restart_speed_control_req = 0;
2869 tape->pipeline_head = 0;
2870 tape->controlled_last_pipeline_head = tape->uncontrolled_last_pipeline_head = 0;
2871 tape->controlled_previous_pipeline_head = tape->uncontrolled_previous_pipeline_head = 0;
2872 tape->pipeline_head_speed = tape->controlled_pipeline_head_speed = 5000;
2873 tape->uncontrolled_pipeline_head_speed = 0;
2874 tape->controlled_pipeline_head_time = tape->uncontrolled_pipeline_head_time = jiffies;
2875 tape->controlled_previous_head_time = tape->uncontrolled_previous_head_time = jiffies;
2878 static int idetape_initiate_read (ide_drive_t *drive, int max_stages)
2880 idetape_tape_t *tape = drive->driver_data;
2881 idetape_stage_t *new_stage;
2884 int blocks = tape->capabilities.ctl;
2886 /* Initialize read operation */
2887 if (tape->chrdev_direction != idetape_direction_read) {
2888 if (tape->chrdev_direction == idetape_direction_write) {
2889 idetape_empty_write_pipeline(drive);
2890 idetape_flush_tape_buffers(drive);
2892 #if IDETAPE_DEBUG_BUGS
2893 if (tape->merge_stage || tape->merge_stage_size) {
2894 printk (KERN_ERR "ide-tape: merge_stage_size should be 0 now\n");
2895 tape->merge_stage_size = 0;
2897 #endif /* IDETAPE_DEBUG_BUGS */
2898 if ((tape->merge_stage = __idetape_kmalloc_stage(tape, 0, 0)) == NULL)
2900 tape->chrdev_direction = idetape_direction_read;
2903 * Issue a read 0 command to ensure that DSC handshake
2904 * is switched from completion mode to buffer available
2906 * No point in issuing this if DSC overlap isn't supported,
2907 * some drives (Seagate STT3401A) will return an error.
2909 if (drive->dsc_overlap) {
2910 bytes_read = idetape_queue_rw_tail(drive, REQ_IDETAPE_READ, 0, tape->merge_stage->bh);
2911 if (bytes_read < 0) {
2912 __idetape_kfree_stage(tape->merge_stage);
2913 tape->merge_stage = NULL;
2914 tape->chrdev_direction = idetape_direction_none;
2919 if (tape->restart_speed_control_req)
2920 idetape_restart_speed_control(drive);
2921 idetape_init_rq(&rq, REQ_IDETAPE_READ);
2922 rq.sector = tape->first_frame_position;
2923 rq.nr_sectors = rq.current_nr_sectors = blocks;
2924 if (!test_bit(IDETAPE_PIPELINE_ERROR, &tape->flags) &&
2925 tape->nr_stages < max_stages) {
2926 new_stage = idetape_kmalloc_stage(tape);
2927 while (new_stage != NULL) {
2929 idetape_add_stage_tail(drive, new_stage);
2930 if (tape->nr_stages >= max_stages)
2932 new_stage = idetape_kmalloc_stage(tape);
2935 if (!idetape_pipeline_active(tape)) {
2936 if (tape->nr_pending_stages >= 3 * max_stages / 4) {
2937 tape->measure_insert_time = 1;
2938 tape->insert_time = jiffies;
2939 tape->insert_size = 0;
2940 tape->insert_speed = 0;
2941 idetape_insert_pipeline_into_queue(drive);
2948 * idetape_add_chrdev_read_request is called from idetape_chrdev_read
2949 * to service a character device read request and add read-ahead
2950 * requests to our pipeline.
2952 static int idetape_add_chrdev_read_request (ide_drive_t *drive,int blocks)
2954 idetape_tape_t *tape = drive->driver_data;
2955 unsigned long flags;
2956 struct request *rq_ptr;
2959 #if IDETAPE_DEBUG_LOG
2960 if (tape->debug_level >= 4)
2961 printk(KERN_INFO "ide-tape: Reached idetape_add_chrdev_read_request, %d blocks\n", blocks);
2962 #endif /* IDETAPE_DEBUG_LOG */
2965 * If we are at a filemark, return a read length of 0
2967 if (test_bit(IDETAPE_FILEMARK, &tape->flags))
2971 * Wait for the next block to be available at the head
2974 idetape_initiate_read(drive, tape->max_stages);
2975 if (tape->first_stage == NULL) {
2976 if (test_bit(IDETAPE_PIPELINE_ERROR, &tape->flags))
2978 return idetape_queue_rw_tail(drive, REQ_IDETAPE_READ, blocks, tape->merge_stage->bh);
2980 idetape_wait_first_stage(drive);
2981 rq_ptr = &tape->first_stage->rq;
2982 bytes_read = tape->tape_block_size * (rq_ptr->nr_sectors - rq_ptr->current_nr_sectors);
2983 rq_ptr->nr_sectors = rq_ptr->current_nr_sectors = 0;
2986 if (rq_ptr->errors == IDETAPE_ERROR_EOD)
2989 idetape_switch_buffers(tape, tape->first_stage);
2990 if (rq_ptr->errors == IDETAPE_ERROR_FILEMARK)
2991 set_bit(IDETAPE_FILEMARK, &tape->flags);
2992 spin_lock_irqsave(&tape->spinlock, flags);
2993 idetape_remove_stage_head(drive);
2994 spin_unlock_irqrestore(&tape->spinlock, flags);
2995 tape->pipeline_head++;
2996 calculate_speeds(drive);
2998 #if IDETAPE_DEBUG_BUGS
2999 if (bytes_read > blocks * tape->tape_block_size) {
3000 printk(KERN_ERR "ide-tape: bug: trying to return more bytes than requested\n");
3001 bytes_read = blocks * tape->tape_block_size;
3003 #endif /* IDETAPE_DEBUG_BUGS */
3004 return (bytes_read);
3007 static void idetape_pad_zeros (ide_drive_t *drive, int bcount)
3009 idetape_tape_t *tape = drive->driver_data;
3010 struct idetape_bh *bh;
3016 bh = tape->merge_stage->bh;
3017 count = min(tape->stage_size, bcount);
3019 blocks = count / tape->tape_block_size;
3021 atomic_set(&bh->b_count, min(count, (unsigned int)bh->b_size));
3022 memset(bh->b_data, 0, atomic_read(&bh->b_count));
3023 count -= atomic_read(&bh->b_count);
3026 idetape_queue_rw_tail(drive, REQ_IDETAPE_WRITE, blocks, tape->merge_stage->bh);
3030 static int idetape_pipeline_size (ide_drive_t *drive)
3032 idetape_tape_t *tape = drive->driver_data;
3033 idetape_stage_t *stage;
3037 idetape_wait_for_pipeline(drive);
3038 stage = tape->first_stage;
3039 while (stage != NULL) {
3041 size += tape->tape_block_size * (rq->nr_sectors-rq->current_nr_sectors);
3042 if (rq->errors == IDETAPE_ERROR_FILEMARK)
3043 size += tape->tape_block_size;
3044 stage = stage->next;
3046 size += tape->merge_stage_size;
3051 * Rewinds the tape to the Beginning Of the current Partition (BOP).
3053 * We currently support only one partition.
3055 static int idetape_rewind_tape (ide_drive_t *drive)
3059 #if IDETAPE_DEBUG_LOG
3060 idetape_tape_t *tape = drive->driver_data;
3061 if (tape->debug_level >= 2)
3062 printk(KERN_INFO "ide-tape: Reached idetape_rewind_tape\n");
3063 #endif /* IDETAPE_DEBUG_LOG */
3065 idetape_create_rewind_cmd(drive, &pc);
3066 retval = idetape_queue_pc_tail(drive, &pc);
3070 idetape_create_read_position_cmd(&pc);
3071 retval = idetape_queue_pc_tail(drive, &pc);
3078 * Our special ide-tape ioctl's.
3080 * Currently there aren't any ioctl's.
3081 * mtio.h compatible commands should be issued to the character device
3084 static int idetape_blkdev_ioctl(ide_drive_t *drive, unsigned int cmd, unsigned long arg)
3086 idetape_tape_t *tape = drive->driver_data;
3087 idetape_config_t config;
3088 void __user *argp = (void __user *)arg;
3090 #if IDETAPE_DEBUG_LOG
3091 if (tape->debug_level >= 4)
3092 printk(KERN_INFO "ide-tape: Reached idetape_blkdev_ioctl\n");
3093 #endif /* IDETAPE_DEBUG_LOG */
3096 if (copy_from_user(&config, argp, sizeof (idetape_config_t)))
3098 tape->best_dsc_rw_frequency = config.dsc_rw_frequency;
3099 tape->max_stages = config.nr_stages;
3102 config.dsc_rw_frequency = (int) tape->best_dsc_rw_frequency;
3103 config.nr_stages = tape->max_stages;
3104 if (copy_to_user(argp, &config, sizeof (idetape_config_t)))
3114 * idetape_space_over_filemarks is now a bit more complicated than just
3115 * passing the command to the tape since we may have crossed some
3116 * filemarks during our pipelined read-ahead mode.
3118 * As a minor side effect, the pipeline enables us to support MTFSFM when
3119 * the filemark is in our internal pipeline even if the tape doesn't
3120 * support spacing over filemarks in the reverse direction.
3122 static int idetape_space_over_filemarks (ide_drive_t *drive,short mt_op,int mt_count)
3124 idetape_tape_t *tape = drive->driver_data;
3126 unsigned long flags;
3131 if (MTBSF == mt_op || MTBSFM == mt_op) {
3132 if (!tape->capabilities.sprev)
3134 mt_count = - mt_count;
3137 if (tape->chrdev_direction == idetape_direction_read) {
3139 * We have a read-ahead buffer. Scan it for crossed
3142 tape->merge_stage_size = 0;
3143 if (test_and_clear_bit(IDETAPE_FILEMARK, &tape->flags))
3145 while (tape->first_stage != NULL) {
3146 if (count == mt_count) {
3147 if (mt_op == MTFSFM)
3148 set_bit(IDETAPE_FILEMARK, &tape->flags);
3151 spin_lock_irqsave(&tape->spinlock, flags);
3152 if (tape->first_stage == tape->active_stage) {
3154 * We have reached the active stage in the read pipeline.
3155 * There is no point in allowing the drive to continue
3156 * reading any farther, so we stop the pipeline.
3158 * This section should be moved to a separate subroutine,
3159 * because a similar function is performed in
3160 * __idetape_discard_read_pipeline(), for example.
3162 tape->next_stage = NULL;
3163 spin_unlock_irqrestore(&tape->spinlock, flags);
3164 idetape_wait_first_stage(drive);
3165 tape->next_stage = tape->first_stage->next;
3167 spin_unlock_irqrestore(&tape->spinlock, flags);
3168 if (tape->first_stage->rq.errors == IDETAPE_ERROR_FILEMARK)
3170 idetape_remove_stage_head(drive);
3172 idetape_discard_read_pipeline(drive, 0);
3176 * The filemark was not found in our internal pipeline.
3177 * Now we can issue the space command.
3182 idetape_create_space_cmd(&pc,mt_count-count,IDETAPE_SPACE_OVER_FILEMARK);
3183 return (idetape_queue_pc_tail(drive, &pc));
3186 if (!tape->capabilities.sprev)
3188 retval = idetape_space_over_filemarks(drive, MTFSF, mt_count-count);
3189 if (retval) return (retval);
3190 count = (MTBSFM == mt_op ? 1 : -1);
3191 return (idetape_space_over_filemarks(drive, MTFSF, count));
3193 printk(KERN_ERR "ide-tape: MTIO operation %d not supported\n",mt_op);
3200 * Our character device read / write functions.
3202 * The tape is optimized to maximize throughput when it is transferring
3203 * an integral number of the "continuous transfer limit", which is
3204 * a parameter of the specific tape (26 KB on my particular tape).
3205 * (32 kB for Onstream)
3207 * As of version 1.3 of the driver, the character device provides an
3208 * abstract continuous view of the media - any mix of block sizes (even 1
3209 * byte) on the same backup/restore procedure is supported. The driver
3210 * will internally convert the requests to the recommended transfer unit,
3211 * so that an unmatch between the user's block size to the recommended
3212 * size will only result in a (slightly) increased driver overhead, but
3213 * will no longer hit performance.
3214 * This is not applicable to Onstream.
3216 static ssize_t idetape_chrdev_read (struct file *file, char __user *buf,
3217 size_t count, loff_t *ppos)
3219 struct ide_tape_obj *tape = ide_tape_f(file);
3220 ide_drive_t *drive = tape->drive;
3221 ssize_t bytes_read,temp, actually_read = 0, rc;
3224 #if IDETAPE_DEBUG_LOG
3225 if (tape->debug_level >= 3)
3226 printk(KERN_INFO "ide-tape: Reached idetape_chrdev_read, count %Zd\n", count);
3227 #endif /* IDETAPE_DEBUG_LOG */
3229 if (tape->chrdev_direction != idetape_direction_read) {
3230 if (test_bit(IDETAPE_DETECT_BS, &tape->flags))
3231 if (count > tape->tape_block_size &&
3232 (count % tape->tape_block_size) == 0)
3233 tape->user_bs_factor = count / tape->tape_block_size;
3235 if ((rc = idetape_initiate_read(drive, tape->max_stages)) < 0)
3239 if (tape->merge_stage_size) {
3240 actually_read = min((unsigned int)(tape->merge_stage_size), (unsigned int)count);
3241 if (idetape_copy_stage_to_user(tape, buf, tape->merge_stage, actually_read))
3243 buf += actually_read;
3244 tape->merge_stage_size -= actually_read;
3245 count -= actually_read;
3247 while (count >= tape->stage_size) {
3248 bytes_read = idetape_add_chrdev_read_request(drive, tape->capabilities.ctl);
3249 if (bytes_read <= 0)
3251 if (idetape_copy_stage_to_user(tape, buf, tape->merge_stage, bytes_read))
3254 count -= bytes_read;
3255 actually_read += bytes_read;
3258 bytes_read = idetape_add_chrdev_read_request(drive, tape->capabilities.ctl);
3259 if (bytes_read <= 0)
3261 temp = min((unsigned long)count, (unsigned long)bytes_read);
3262 if (idetape_copy_stage_to_user(tape, buf, tape->merge_stage, temp))
3264 actually_read += temp;
3265 tape->merge_stage_size = bytes_read-temp;
3268 if (!actually_read && test_bit(IDETAPE_FILEMARK, &tape->flags)) {
3269 #if IDETAPE_DEBUG_LOG
3270 if (tape->debug_level >= 2)
3271 printk(KERN_INFO "ide-tape: %s: spacing over filemark\n", tape->name);
3273 idetape_space_over_filemarks(drive, MTFSF, 1);
3277 return (ret) ? ret : actually_read;
3280 static ssize_t idetape_chrdev_write (struct file *file, const char __user *buf,
3281 size_t count, loff_t *ppos)
3283 struct ide_tape_obj *tape = ide_tape_f(file);
3284 ide_drive_t *drive = tape->drive;
3285 ssize_t actually_written = 0;
3288 /* The drive is write protected. */
3289 if (tape->write_prot)
3292 #if IDETAPE_DEBUG_LOG
3293 if (tape->debug_level >= 3)
3294 printk(KERN_INFO "ide-tape: Reached idetape_chrdev_write, "
3295 "count %Zd\n", count);
3296 #endif /* IDETAPE_DEBUG_LOG */
3298 /* Initialize write operation */
3299 if (tape->chrdev_direction != idetape_direction_write) {
3300 if (tape->chrdev_direction == idetape_direction_read)
3301 idetape_discard_read_pipeline(drive, 1);
3302 #if IDETAPE_DEBUG_BUGS
3303 if (tape->merge_stage || tape->merge_stage_size) {
3304 printk(KERN_ERR "ide-tape: merge_stage_size "
3305 "should be 0 now\n");
3306 tape->merge_stage_size = 0;
3308 #endif /* IDETAPE_DEBUG_BUGS */
3309 if ((tape->merge_stage = __idetape_kmalloc_stage(tape, 0, 0)) == NULL)
3311 tape->chrdev_direction = idetape_direction_write;
3312 idetape_init_merge_stage(tape);
3315 * Issue a write 0 command to ensure that DSC handshake
3316 * is switched from completion mode to buffer available
3318 * No point in issuing this if DSC overlap isn't supported,
3319 * some drives (Seagate STT3401A) will return an error.
3321 if (drive->dsc_overlap) {
3322 ssize_t retval = idetape_queue_rw_tail(drive, REQ_IDETAPE_WRITE, 0, tape->merge_stage->bh);
3324 __idetape_kfree_stage(tape->merge_stage);
3325 tape->merge_stage = NULL;
3326 tape->chrdev_direction = idetape_direction_none;
3333 if (tape->restart_speed_control_req)
3334 idetape_restart_speed_control(drive);
3335 if (tape->merge_stage_size) {
3336 #if IDETAPE_DEBUG_BUGS
3337 if (tape->merge_stage_size >= tape->stage_size) {
3338 printk(KERN_ERR "ide-tape: bug: merge buffer too big\n");
3339 tape->merge_stage_size = 0;
3341 #endif /* IDETAPE_DEBUG_BUGS */
3342 actually_written = min((unsigned int)(tape->stage_size - tape->merge_stage_size), (unsigned int)count);
3343 if (idetape_copy_stage_from_user(tape, tape->merge_stage, buf, actually_written))
3345 buf += actually_written;
3346 tape->merge_stage_size += actually_written;
3347 count -= actually_written;
3349 if (tape->merge_stage_size == tape->stage_size) {
3351 tape->merge_stage_size = 0;
3352 retval = idetape_add_chrdev_write_request(drive, tape->capabilities.ctl);
3357 while (count >= tape->stage_size) {
3359 if (idetape_copy_stage_from_user(tape, tape->merge_stage, buf, tape->stage_size))
3361 buf += tape->stage_size;
3362 count -= tape->stage_size;
3363 retval = idetape_add_chrdev_write_request(drive, tape->capabilities.ctl);
3364 actually_written += tape->stage_size;
3369 actually_written += count;
3370 if (idetape_copy_stage_from_user(tape, tape->merge_stage, buf, count))
3372 tape->merge_stage_size += count;
3374 return (ret) ? ret : actually_written;
3377 static int idetape_write_filemark (ide_drive_t *drive)
3381 /* Write a filemark */
3382 idetape_create_write_filemark_cmd(drive, &pc, 1);
3383 if (idetape_queue_pc_tail(drive, &pc)) {
3384 printk(KERN_ERR "ide-tape: Couldn't write a filemark\n");
3391 * idetape_mtioctop is called from idetape_chrdev_ioctl when
3392 * the general mtio MTIOCTOP ioctl is requested.
3394 * We currently support the following mtio.h operations:
3396 * MTFSF - Space over mt_count filemarks in the positive direction.
3397 * The tape is positioned after the last spaced filemark.
3399 * MTFSFM - Same as MTFSF, but the tape is positioned before the
3402 * MTBSF - Steps background over mt_count filemarks, tape is
3403 * positioned before the last filemark.
3405 * MTBSFM - Like MTBSF, only tape is positioned after the last filemark.
3409 * MTBSF and MTBSFM are not supported when the tape doesn't
3410 * support spacing over filemarks in the reverse direction.
3411 * In this case, MTFSFM is also usually not supported (it is
3412 * supported in the rare case in which we crossed the filemark
3413 * during our read-ahead pipelined operation mode).
3415 * MTWEOF - Writes mt_count filemarks. Tape is positioned after
3416 * the last written filemark.
3418 * MTREW - Rewinds tape.
3420 * MTLOAD - Loads the tape.
3422 * MTOFFL - Puts the tape drive "Offline": Rewinds the tape and
3423 * MTUNLOAD prevents further access until the media is replaced.
3425 * MTNOP - Flushes tape buffers.
3427 * MTRETEN - Retension media. This typically consists of one end
3428 * to end pass on the media.
3430 * MTEOM - Moves to the end of recorded data.
3432 * MTERASE - Erases tape.
3434 * MTSETBLK - Sets the user block size to mt_count bytes. If
3435 * mt_count is 0, we will attempt to autodetect
3438 * MTSEEK - Positions the tape in a specific block number, where
3439 * each block is assumed to contain which user_block_size
3442 * MTSETPART - Switches to another tape partition.
3444 * MTLOCK - Locks the tape door.
3446 * MTUNLOCK - Unlocks the tape door.
3448 * The following commands are currently not supported:
3450 * MTFSS, MTBSS, MTWSM, MTSETDENSITY,
3451 * MTSETDRVBUFFER, MT_ST_BOOLEANS, MT_ST_WRITE_THRESHOLD.
3453 static int idetape_mtioctop (ide_drive_t *drive,short mt_op,int mt_count)
3455 idetape_tape_t *tape = drive->driver_data;
3459 #if IDETAPE_DEBUG_LOG
3460 if (tape->debug_level >= 1)
3461 printk(KERN_INFO "ide-tape: Handling MTIOCTOP ioctl: "
3462 "mt_op=%d, mt_count=%d\n", mt_op, mt_count);
3463 #endif /* IDETAPE_DEBUG_LOG */
3465 * Commands which need our pipelined read-ahead stages.
3474 return (idetape_space_over_filemarks(drive,mt_op,mt_count));
3480 if (tape->write_prot)
3482 idetape_discard_read_pipeline(drive, 1);
3483 for (i = 0; i < mt_count; i++) {
3484 retval = idetape_write_filemark(drive);
3490 idetape_discard_read_pipeline(drive, 0);
3491 if (idetape_rewind_tape(drive))
3495 idetape_discard_read_pipeline(drive, 0);
3496 idetape_create_load_unload_cmd(drive, &pc, IDETAPE_LU_LOAD_MASK);
3497 return (idetape_queue_pc_tail(drive, &pc));
3501 * If door is locked, attempt to unlock before
3502 * attempting to eject.
3504 if (tape->door_locked) {
3505 if (idetape_create_prevent_cmd(drive, &pc, 0))
3506 if (!idetape_queue_pc_tail(drive, &pc))
3507 tape->door_locked = DOOR_UNLOCKED;
3509 idetape_discard_read_pipeline(drive, 0);
3510 idetape_create_load_unload_cmd(drive, &pc,!IDETAPE_LU_LOAD_MASK);
3511 retval = idetape_queue_pc_tail(drive, &pc);
3513 clear_bit(IDETAPE_MEDIUM_PRESENT, &tape->flags);
3516 idetape_discard_read_pipeline(drive, 0);
3517 return (idetape_flush_tape_buffers(drive));
3519 idetape_discard_read_pipeline(drive, 0);
3520 idetape_create_load_unload_cmd(drive, &pc,IDETAPE_LU_RETENSION_MASK | IDETAPE_LU_LOAD_MASK);
3521 return (idetape_queue_pc_tail(drive, &pc));
3523 idetape_create_space_cmd(&pc, 0, IDETAPE_SPACE_TO_EOD);
3524 return (idetape_queue_pc_tail(drive, &pc));
3526 (void) idetape_rewind_tape(drive);
3527 idetape_create_erase_cmd(&pc);
3528 return (idetape_queue_pc_tail(drive, &pc));
3531 if (mt_count < tape->tape_block_size || mt_count % tape->tape_block_size)
3533 tape->user_bs_factor = mt_count / tape->tape_block_size;
3534 clear_bit(IDETAPE_DETECT_BS, &tape->flags);
3536 set_bit(IDETAPE_DETECT_BS, &tape->flags);
3539 idetape_discard_read_pipeline(drive, 0);
3540 return idetape_position_tape(drive, mt_count * tape->user_bs_factor, tape->partition, 0);
3542 idetape_discard_read_pipeline(drive, 0);
3543 return (idetape_position_tape(drive, 0, mt_count, 0));
3547 if (!idetape_create_prevent_cmd(drive, &pc, 1))
3549 retval = idetape_queue_pc_tail(drive, &pc);
3550 if (retval) return retval;
3551 tape->door_locked = DOOR_EXPLICITLY_LOCKED;
3554 if (!idetape_create_prevent_cmd(drive, &pc, 0))
3556 retval = idetape_queue_pc_tail(drive, &pc);
3557 if (retval) return retval;
3558 tape->door_locked = DOOR_UNLOCKED;
3561 printk(KERN_ERR "ide-tape: MTIO operation %d not "
3562 "supported\n", mt_op);
3568 * Our character device ioctls.
3570 * General mtio.h magnetic io commands are supported here, and not in
3571 * the corresponding block interface.
3573 * The following ioctls are supported:
3575 * MTIOCTOP - Refer to idetape_mtioctop for detailed description.
3577 * MTIOCGET - The mt_dsreg field in the returned mtget structure
3578 * will be set to (user block size in bytes <<
3579 * MT_ST_BLKSIZE_SHIFT) & MT_ST_BLKSIZE_MASK.
3581 * The mt_blkno is set to the current user block number.
3582 * The other mtget fields are not supported.
3584 * MTIOCPOS - The current tape "block position" is returned. We
3585 * assume that each block contains user_block_size
3588 * Our own ide-tape ioctls are supported on both interfaces.
3590 static int idetape_chrdev_ioctl (struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg)
3592 struct ide_tape_obj *tape = ide_tape_f(file);
3593 ide_drive_t *drive = tape->drive;
3597 int block_offset = 0, position = tape->first_frame_position;
3598 void __user *argp = (void __user *)arg;
3600 #if IDETAPE_DEBUG_LOG
3601 if (tape->debug_level >= 3)
3602 printk(KERN_INFO "ide-tape: Reached idetape_chrdev_ioctl, "
3604 #endif /* IDETAPE_DEBUG_LOG */
3606 tape->restart_speed_control_req = 1;
3607 if (tape->chrdev_direction == idetape_direction_write) {
3608 idetape_empty_write_pipeline(drive);
3609 idetape_flush_tape_buffers(drive);
3611 if (cmd == MTIOCGET || cmd == MTIOCPOS) {
3612 block_offset = idetape_pipeline_size(drive) / (tape->tape_block_size * tape->user_bs_factor);
3613 if ((position = idetape_read_position(drive)) < 0)
3618 if (copy_from_user(&mtop, argp, sizeof (struct mtop)))
3620 return (idetape_mtioctop(drive,mtop.mt_op,mtop.mt_count));
3622 memset(&mtget, 0, sizeof (struct mtget));
3623 mtget.mt_type = MT_ISSCSI2;
3624 mtget.mt_blkno = position / tape->user_bs_factor - block_offset;
3625 mtget.mt_dsreg = ((tape->tape_block_size * tape->user_bs_factor) << MT_ST_BLKSIZE_SHIFT) & MT_ST_BLKSIZE_MASK;
3626 if (tape->drv_write_prot) {
3627 mtget.mt_gstat |= GMT_WR_PROT(0xffffffff);
3629 if (copy_to_user(argp, &mtget, sizeof(struct mtget)))
3633 mtpos.mt_blkno = position / tape->user_bs_factor - block_offset;
3634 if (copy_to_user(argp, &mtpos, sizeof(struct mtpos)))
3638 if (tape->chrdev_direction == idetape_direction_read)
3639 idetape_discard_read_pipeline(drive, 1);
3640 return idetape_blkdev_ioctl(drive, cmd, arg);
3644 static void idetape_get_blocksize_from_block_descriptor(ide_drive_t *drive);
3647 * Our character device open function.
3649 static int idetape_chrdev_open (struct inode *inode, struct file *filp)
3651 unsigned int minor = iminor(inode), i = minor & ~0xc0;
3653 idetape_tape_t *tape;
3658 * We really want to do nonseekable_open(inode, filp); here, but some
3659 * versions of tar incorrectly call lseek on tapes and bail out if that
3660 * fails. So we disallow pread() and pwrite(), but permit lseeks.
3662 filp->f_mode &= ~(FMODE_PREAD | FMODE_PWRITE);
3664 #if IDETAPE_DEBUG_LOG
3665 printk(KERN_INFO "ide-tape: Reached idetape_chrdev_open\n");
3666 #endif /* IDETAPE_DEBUG_LOG */
3668 if (i >= MAX_HWIFS * MAX_DRIVES)
3671 if (!(tape = ide_tape_chrdev_get(i)))
3674 drive = tape->drive;
3676 filp->private_data = tape;
3678 if (test_and_set_bit(IDETAPE_BUSY, &tape->flags)) {
3683 retval = idetape_wait_ready(drive, 60 * HZ);
3685 clear_bit(IDETAPE_BUSY, &tape->flags);
3686 printk(KERN_ERR "ide-tape: %s: drive not ready\n", tape->name);
3690 idetape_read_position(drive);
3691 if (!test_bit(IDETAPE_ADDRESS_VALID, &tape->flags))
3692 (void)idetape_rewind_tape(drive);
3694 if (tape->chrdev_direction != idetape_direction_read)
3695 clear_bit(IDETAPE_PIPELINE_ERROR, &tape->flags);
3697 /* Read block size and write protect status from drive. */
3698 idetape_get_blocksize_from_block_descriptor(drive);
3700 /* Set write protect flag if device is opened as read-only. */
3701 if ((filp->f_flags & O_ACCMODE) == O_RDONLY)
3702 tape->write_prot = 1;
3704 tape->write_prot = tape->drv_write_prot;
3706 /* Make sure drive isn't write protected if user wants to write. */
3707 if (tape->write_prot) {
3708 if ((filp->f_flags & O_ACCMODE) == O_WRONLY ||
3709 (filp->f_flags & O_ACCMODE) == O_RDWR) {
3710 clear_bit(IDETAPE_BUSY, &tape->flags);
3717 * Lock the tape drive door so user can't eject.
3719 if (tape->chrdev_direction == idetape_direction_none) {
3720 if (idetape_create_prevent_cmd(drive, &pc, 1)) {
3721 if (!idetape_queue_pc_tail(drive, &pc)) {
3722 if (tape->door_locked != DOOR_EXPLICITLY_LOCKED)
3723 tape->door_locked = DOOR_LOCKED;
3727 idetape_restart_speed_control(drive);
3728 tape->restart_speed_control_req = 0;
3736 static void idetape_write_release (ide_drive_t *drive, unsigned int minor)
3738 idetape_tape_t *tape = drive->driver_data;
3740 idetape_empty_write_pipeline(drive);
3741 tape->merge_stage = __idetape_kmalloc_stage(tape, 1, 0);
3742 if (tape->merge_stage != NULL) {
3743 idetape_pad_zeros(drive, tape->tape_block_size * (tape->user_bs_factor - 1));
3744 __idetape_kfree_stage(tape->merge_stage);
3745 tape->merge_stage = NULL;
3747 idetape_write_filemark(drive);
3748 idetape_flush_tape_buffers(drive);
3749 idetape_flush_tape_buffers(drive);
3753 * Our character device release function.
3755 static int idetape_chrdev_release (struct inode *inode, struct file *filp)
3757 struct ide_tape_obj *tape = ide_tape_f(filp);
3758 ide_drive_t *drive = tape->drive;
3760 unsigned int minor = iminor(inode);
3763 tape = drive->driver_data;
3764 #if IDETAPE_DEBUG_LOG
3765 if (tape->debug_level >= 3)
3766 printk(KERN_INFO "ide-tape: Reached idetape_chrdev_release\n");
3767 #endif /* IDETAPE_DEBUG_LOG */
3769 if (tape->chrdev_direction == idetape_direction_write)
3770 idetape_write_release(drive, minor);
3771 if (tape->chrdev_direction == idetape_direction_read) {
3773 idetape_discard_read_pipeline(drive, 1);
3775 idetape_wait_for_pipeline(drive);
3777 if (tape->cache_stage != NULL) {
3778 __idetape_kfree_stage(tape->cache_stage);
3779 tape->cache_stage = NULL;
3781 if (minor < 128 && test_bit(IDETAPE_MEDIUM_PRESENT, &tape->flags))
3782 (void) idetape_rewind_tape(drive);
3783 if (tape->chrdev_direction == idetape_direction_none) {
3784 if (tape->door_locked == DOOR_LOCKED) {
3785 if (idetape_create_prevent_cmd(drive, &pc, 0)) {
3786 if (!idetape_queue_pc_tail(drive, &pc))
3787 tape->door_locked = DOOR_UNLOCKED;
3791 clear_bit(IDETAPE_BUSY, &tape->flags);
3798 * idetape_identify_device is called to check the contents of the
3799 * ATAPI IDENTIFY command results. We return:
3801 * 1 If the tape can be supported by us, based on the information
3804 * 0 If this tape driver is not currently supported by us.
3806 static int idetape_identify_device (ide_drive_t *drive)
3808 struct idetape_id_gcw gcw;
3809 struct hd_driveid *id = drive->id;
3811 if (drive->id_read == 0)
3814 *((unsigned short *) &gcw) = id->config;
3816 /* Check that we can support this device */
3818 if (gcw.protocol != 2)
3819 printk(KERN_ERR "ide-tape: Protocol (0x%02x) is not ATAPI\n",
3821 else if (gcw.device_type != 1)
3822 printk(KERN_ERR "ide-tape: Device type (0x%02x) is not set "
3823 "to tape\n", gcw.device_type);
3824 else if (!gcw.removable)
3825 printk(KERN_ERR "ide-tape: The removable flag is not set\n");
3826 else if (gcw.packet_size != 0) {
3827 printk(KERN_ERR "ide-tape: Packet size (0x%02x) is not 12 "
3828 "bytes long\n", gcw.packet_size);
3835 * Use INQUIRY to get the firmware revision
3837 static void idetape_get_inquiry_results (ide_drive_t *drive)
3840 idetape_tape_t *tape = drive->driver_data;
3842 idetape_inquiry_result_t *inquiry;
3844 idetape_create_inquiry_cmd(&pc);
3845 if (idetape_queue_pc_tail(drive, &pc)) {
3846 printk(KERN_ERR "ide-tape: %s: can't get INQUIRY results\n", tape->name);
3849 inquiry = (idetape_inquiry_result_t *) pc.buffer;
3850 memcpy(tape->vendor_id, inquiry->vendor_id, 8);
3851 memcpy(tape->product_id, inquiry->product_id, 16);
3852 memcpy(tape->firmware_revision, inquiry->revision_level, 4);
3853 ide_fixstring(tape->vendor_id, 10, 0);
3854 ide_fixstring(tape->product_id, 18, 0);
3855 ide_fixstring(tape->firmware_revision, 6, 0);
3856 r = tape->firmware_revision;
3857 if (*(r + 1) == '.')
3858 tape->firmware_revision_num = (*r - '0') * 100 + (*(r + 2) - '0') * 10 + *(r + 3) - '0';
3859 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);
3863 * idetape_get_mode_sense_results asks the tape about its various
3864 * parameters. In particular, we will adjust our data transfer buffer
3865 * size to the recommended value as returned by the tape.
3867 static void idetape_get_mode_sense_results (ide_drive_t *drive)
3869 idetape_tape_t *tape = drive->driver_data;
3871 idetape_capabilities_page_t *capabilities;
3873 idetape_create_mode_sense_cmd(&pc, IDETAPE_CAPABILITIES_PAGE);
3874 if (idetape_queue_pc_tail(drive, &pc)) {
3875 printk(KERN_ERR "ide-tape: Can't get tape parameters - assuming some default values\n");
3876 tape->tape_block_size = 512;
3877 tape->capabilities.ctl = 52;
3878 tape->capabilities.speed = 450;
3879 tape->capabilities.buffer_size = 6 * 52;
3882 capabilities = (idetape_capabilities_page_t *)
3883 (pc.buffer + 4 + pc.buffer[3]);
3885 capabilities->max_speed = ntohs(capabilities->max_speed);
3886 capabilities->ctl = ntohs(capabilities->ctl);
3887 capabilities->speed = ntohs(capabilities->speed);
3888 capabilities->buffer_size = ntohs(capabilities->buffer_size);
3890 if (!capabilities->speed) {
3891 printk(KERN_INFO "ide-tape: %s: overriding capabilities->speed (assuming 650KB/sec)\n", drive->name);
3892 capabilities->speed = 650;
3894 if (!capabilities->max_speed) {
3895 printk(KERN_INFO "ide-tape: %s: overriding capabilities->max_speed (assuming 650KB/sec)\n", drive->name);
3896 capabilities->max_speed = 650;
3899 tape->capabilities = *capabilities; /* Save us a copy */
3900 if (capabilities->blk512)
3901 tape->tape_block_size = 512;
3902 else if (capabilities->blk1024)
3903 tape->tape_block_size = 1024;
3907 * ide_get_blocksize_from_block_descriptor does a mode sense page 0 with block descriptor
3908 * and if it succeeds sets the tape block size with the reported value
3910 static void idetape_get_blocksize_from_block_descriptor(ide_drive_t *drive)
3913 idetape_tape_t *tape = drive->driver_data;
3915 idetape_parameter_block_descriptor_t *block_descrp;
3917 idetape_create_mode_sense_cmd(&pc, IDETAPE_BLOCK_DESCRIPTOR);
3918 if (idetape_queue_pc_tail(drive, &pc)) {
3919 printk(KERN_ERR "ide-tape: Can't get block descriptor\n");
3920 if (tape->tape_block_size == 0) {
3921 printk(KERN_WARNING "ide-tape: Cannot deal with zero block size, assume 32k\n");
3922 tape->tape_block_size = 32768;
3926 block_descrp = (idetape_parameter_block_descriptor_t *)(pc.buffer + 4);
3927 tape->tape_block_size =( block_descrp->length[0]<<16) + (block_descrp->length[1]<<8) + block_descrp->length[2];
3928 tape->drv_write_prot = (pc.buffer[2] & 0x80) >> 7;
3931 #ifdef CONFIG_IDE_PROC_FS
3932 static void idetape_add_settings (ide_drive_t *drive)
3934 idetape_tape_t *tape = drive->driver_data;
3937 * drive setting name read/write data type min max mul_factor div_factor data pointer set function
3939 ide_add_setting(drive, "buffer", SETTING_READ, TYPE_SHORT, 0, 0xffff, 1, 2, &tape->capabilities.buffer_size, NULL);
3940 ide_add_setting(drive, "pipeline_min", SETTING_RW, TYPE_INT, 1, 0xffff, tape->stage_size / 1024, 1, &tape->min_pipeline, NULL);
3941 ide_add_setting(drive, "pipeline", SETTING_RW, TYPE_INT, 1, 0xffff, tape->stage_size / 1024, 1, &tape->max_stages, NULL);
3942 ide_add_setting(drive, "pipeline_max", SETTING_RW, TYPE_INT, 1, 0xffff, tape->stage_size / 1024, 1, &tape->max_pipeline, NULL);
3943 ide_add_setting(drive, "pipeline_used", SETTING_READ, TYPE_INT, 0, 0xffff, tape->stage_size / 1024, 1, &tape->nr_stages, NULL);
3944 ide_add_setting(drive, "pipeline_pending", SETTING_READ, TYPE_INT, 0, 0xffff, tape->stage_size / 1024, 1, &tape->nr_pending_stages, NULL);
3945 ide_add_setting(drive, "speed", SETTING_READ, TYPE_SHORT, 0, 0xffff, 1, 1, &tape->capabilities.speed, NULL);
3946 ide_add_setting(drive, "stage", SETTING_READ, TYPE_INT, 0, 0xffff, 1, 1024, &tape->stage_size, NULL);
3947 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);
3948 ide_add_setting(drive, "dsc_overlap", SETTING_RW, TYPE_BYTE, 0, 1, 1, 1, &drive->dsc_overlap, NULL);
3949 ide_add_setting(drive, "pipeline_head_speed_c",SETTING_READ, TYPE_INT, 0, 0xffff, 1, 1, &tape->controlled_pipeline_head_speed, NULL);
3950 ide_add_setting(drive, "pipeline_head_speed_u",SETTING_READ, TYPE_INT, 0, 0xffff, 1, 1, &tape->uncontrolled_pipeline_head_speed,NULL);
3951 ide_add_setting(drive, "avg_speed", SETTING_READ, TYPE_INT, 0, 0xffff, 1, 1, &tape->avg_speed, NULL);
3952 ide_add_setting(drive, "debug_level", SETTING_RW, TYPE_INT, 0, 0xffff, 1, 1, &tape->debug_level, NULL);
3955 static inline void idetape_add_settings(ide_drive_t *drive) { ; }
3959 * ide_setup is called to:
3961 * 1. Initialize our various state variables.
3962 * 2. Ask the tape for its capabilities.
3963 * 3. Allocate a buffer which will be used for data
3964 * transfer. The buffer size is chosen based on
3965 * the recommendation which we received in step (2).
3967 * Note that at this point ide.c already assigned us an irq, so that
3968 * we can queue requests here and wait for their completion.
3970 static void idetape_setup (ide_drive_t *drive, idetape_tape_t *tape, int minor)
3972 unsigned long t1, tmid, tn, t;
3974 struct idetape_id_gcw gcw;
3978 spin_lock_init(&tape->spinlock);
3979 drive->dsc_overlap = 1;
3980 if (drive->hwif->host_flags & IDE_HFLAG_NO_DSC) {
3981 printk(KERN_INFO "ide-tape: %s: disabling DSC overlap\n",
3983 drive->dsc_overlap = 0;
3985 /* Seagate Travan drives do not support DSC overlap. */
3986 if (strstr(drive->id->model, "Seagate STT3401"))
3987 drive->dsc_overlap = 0;
3988 tape->minor = minor;
3989 tape->name[0] = 'h';
3990 tape->name[1] = 't';
3991 tape->name[2] = '0' + minor;
3992 tape->chrdev_direction = idetape_direction_none;
3993 tape->pc = tape->pc_stack;
3994 tape->max_insert_speed = 10000;
3995 tape->speed_control = 1;
3996 *((unsigned short *) &gcw) = drive->id->config;
3997 if (gcw.drq_type == 1)
3998 set_bit(IDETAPE_DRQ_INTERRUPT, &tape->flags);
4000 tape->min_pipeline = tape->max_pipeline = tape->max_stages = 10;
4002 idetape_get_inquiry_results(drive);
4003 idetape_get_mode_sense_results(drive);
4004 idetape_get_blocksize_from_block_descriptor(drive);
4005 tape->user_bs_factor = 1;
4006 tape->stage_size = tape->capabilities.ctl * tape->tape_block_size;
4007 while (tape->stage_size > 0xffff) {
4008 printk(KERN_NOTICE "ide-tape: decreasing stage size\n");
4009 tape->capabilities.ctl /= 2;
4010 tape->stage_size = tape->capabilities.ctl * tape->tape_block_size;
4012 stage_size = tape->stage_size;
4013 tape->pages_per_stage = stage_size / PAGE_SIZE;
4014 if (stage_size % PAGE_SIZE) {
4015 tape->pages_per_stage++;
4016 tape->excess_bh_size = PAGE_SIZE - stage_size % PAGE_SIZE;
4020 * Select the "best" DSC read/write polling frequency
4021 * and pipeline size.
4023 speed = max(tape->capabilities.speed, tape->capabilities.max_speed);
4025 tape->max_stages = speed * 1000 * 10 / tape->stage_size;
4028 * Limit memory use for pipeline to 10% of physical memory
4031 if (tape->max_stages * tape->stage_size > si.totalram * si.mem_unit / 10)
4032 tape->max_stages = si.totalram * si.mem_unit / (10 * tape->stage_size);
4033 tape->max_stages = min(tape->max_stages, IDETAPE_MAX_PIPELINE_STAGES);
4034 tape->min_pipeline = min(tape->max_stages, IDETAPE_MIN_PIPELINE_STAGES);
4035 tape->max_pipeline = min(tape->max_stages * 2, IDETAPE_MAX_PIPELINE_STAGES);
4036 if (tape->max_stages == 0)
4037 tape->max_stages = tape->min_pipeline = tape->max_pipeline = 1;
4039 t1 = (tape->stage_size * HZ) / (speed * 1000);
4040 tmid = (tape->capabilities.buffer_size * 32 * HZ) / (speed * 125);
4041 tn = (IDETAPE_FIFO_THRESHOLD * tape->stage_size * HZ) / (speed * 1000);
4043 if (tape->max_stages)
4049 * Ensure that the number we got makes sense; limit
4050 * it within IDETAPE_DSC_RW_MIN and IDETAPE_DSC_RW_MAX.
4052 tape->best_dsc_rw_frequency = max_t(unsigned long, min_t(unsigned long, t, IDETAPE_DSC_RW_MAX), IDETAPE_DSC_RW_MIN);
4053 printk(KERN_INFO "ide-tape: %s <-> %s: %dKBps, %d*%dkB buffer, "
4054 "%dkB pipeline, %lums tDSC%s\n",
4055 drive->name, tape->name, tape->capabilities.speed,
4056 (tape->capabilities.buffer_size * 512) / tape->stage_size,
4057 tape->stage_size / 1024,
4058 tape->max_stages * tape->stage_size / 1024,
4059 tape->best_dsc_rw_frequency * 1000 / HZ,
4060 drive->using_dma ? ", DMA":"");
4062 idetape_add_settings(drive);
4065 static void ide_tape_remove(ide_drive_t *drive)
4067 idetape_tape_t *tape = drive->driver_data;
4069 ide_proc_unregister_driver(drive, tape->driver);
4071 ide_unregister_region(tape->disk);
4076 static void ide_tape_release(struct kref *kref)
4078 struct ide_tape_obj *tape = to_ide_tape(kref);
4079 ide_drive_t *drive = tape->drive;
4080 struct gendisk *g = tape->disk;
4082 BUG_ON(tape->first_stage != NULL || tape->merge_stage_size);
4084 drive->dsc_overlap = 0;
4085 drive->driver_data = NULL;
4086 device_destroy(idetape_sysfs_class, MKDEV(IDETAPE_MAJOR, tape->minor));
4087 device_destroy(idetape_sysfs_class, MKDEV(IDETAPE_MAJOR, tape->minor + 128));
4088 idetape_devs[tape->minor] = NULL;
4089 g->private_data = NULL;
4094 #ifdef CONFIG_IDE_PROC_FS
4095 static int proc_idetape_read_name
4096 (char *page, char **start, off_t off, int count, int *eof, void *data)
4098 ide_drive_t *drive = (ide_drive_t *) data;
4099 idetape_tape_t *tape = drive->driver_data;
4103 len = sprintf(out, "%s\n", tape->name);
4104 PROC_IDE_READ_RETURN(page, start, off, count, eof, len);
4107 static ide_proc_entry_t idetape_proc[] = {
4108 { "capacity", S_IFREG|S_IRUGO, proc_ide_read_capacity, NULL },
4109 { "name", S_IFREG|S_IRUGO, proc_idetape_read_name, NULL },
4110 { NULL, 0, NULL, NULL }
4114 static int ide_tape_probe(ide_drive_t *);
4116 static ide_driver_t idetape_driver = {
4118 .owner = THIS_MODULE,
4120 .bus = &ide_bus_type,
4122 .probe = ide_tape_probe,
4123 .remove = ide_tape_remove,
4124 .version = IDETAPE_VERSION,
4126 .supports_dsc_overlap = 1,
4127 .do_request = idetape_do_request,
4128 .end_request = idetape_end_request,
4129 .error = __ide_error,
4130 .abort = __ide_abort,
4131 #ifdef CONFIG_IDE_PROC_FS
4132 .proc = idetape_proc,
4137 * Our character device supporting functions, passed to register_chrdev.
4139 static const struct file_operations idetape_fops = {
4140 .owner = THIS_MODULE,
4141 .read = idetape_chrdev_read,
4142 .write = idetape_chrdev_write,
4143 .ioctl = idetape_chrdev_ioctl,
4144 .open = idetape_chrdev_open,
4145 .release = idetape_chrdev_release,
4148 static int idetape_open(struct inode *inode, struct file *filp)
4150 struct gendisk *disk = inode->i_bdev->bd_disk;
4151 struct ide_tape_obj *tape;
4153 if (!(tape = ide_tape_get(disk)))
4159 static int idetape_release(struct inode *inode, struct file *filp)
4161 struct gendisk *disk = inode->i_bdev->bd_disk;
4162 struct ide_tape_obj *tape = ide_tape_g(disk);
4169 static int idetape_ioctl(struct inode *inode, struct file *file,
4170 unsigned int cmd, unsigned long arg)
4172 struct block_device *bdev = inode->i_bdev;
4173 struct ide_tape_obj *tape = ide_tape_g(bdev->bd_disk);
4174 ide_drive_t *drive = tape->drive;
4175 int err = generic_ide_ioctl(drive, file, bdev, cmd, arg);
4177 err = idetape_blkdev_ioctl(drive, cmd, arg);
4181 static struct block_device_operations idetape_block_ops = {
4182 .owner = THIS_MODULE,
4183 .open = idetape_open,
4184 .release = idetape_release,
4185 .ioctl = idetape_ioctl,
4188 static int ide_tape_probe(ide_drive_t *drive)
4190 idetape_tape_t *tape;
4194 if (!strstr("ide-tape", drive->driver_req))
4196 if (!drive->present)
4198 if (drive->media != ide_tape)
4200 if (!idetape_identify_device (drive)) {
4201 printk(KERN_ERR "ide-tape: %s: not supported by this version of ide-tape\n", drive->name);
4205 printk("ide-tape: passing drive %s to ide-scsi emulation.\n", drive->name);
4208 if (strstr(drive->id->model, "OnStream DI-")) {
4209 printk(KERN_WARNING "ide-tape: Use drive %s with ide-scsi emulation and osst.\n", drive->name);
4210 printk(KERN_WARNING "ide-tape: OnStream support will be removed soon from ide-tape!\n");
4212 tape = kzalloc(sizeof (idetape_tape_t), GFP_KERNEL);
4214 printk(KERN_ERR "ide-tape: %s: Can't allocate a tape structure\n", drive->name);
4218 g = alloc_disk(1 << PARTN_BITS);
4222 ide_init_disk(g, drive);
4224 ide_proc_register_driver(drive, &idetape_driver);
4226 kref_init(&tape->kref);
4228 tape->drive = drive;
4229 tape->driver = &idetape_driver;
4232 g->private_data = &tape->driver;
4234 drive->driver_data = tape;
4236 mutex_lock(&idetape_ref_mutex);
4237 for (minor = 0; idetape_devs[minor]; minor++)
4239 idetape_devs[minor] = tape;
4240 mutex_unlock(&idetape_ref_mutex);
4242 idetape_setup(drive, tape, minor);
4244 device_create(idetape_sysfs_class, &drive->gendev,
4245 MKDEV(IDETAPE_MAJOR, minor), "%s", tape->name);
4246 device_create(idetape_sysfs_class, &drive->gendev,
4247 MKDEV(IDETAPE_MAJOR, minor + 128), "n%s", tape->name);
4249 g->fops = &idetape_block_ops;
4250 ide_register_region(g);
4260 MODULE_DESCRIPTION("ATAPI Streaming TAPE Driver");
4261 MODULE_LICENSE("GPL");
4263 static void __exit idetape_exit (void)
4265 driver_unregister(&idetape_driver.gen_driver);
4266 class_destroy(idetape_sysfs_class);
4267 unregister_chrdev(IDETAPE_MAJOR, "ht");
4270 static int __init idetape_init(void)
4273 idetape_sysfs_class = class_create(THIS_MODULE, "ide_tape");
4274 if (IS_ERR(idetape_sysfs_class)) {
4275 idetape_sysfs_class = NULL;
4276 printk(KERN_ERR "Unable to create sysfs class for ide tapes\n");
4281 if (register_chrdev(IDETAPE_MAJOR, "ht", &idetape_fops)) {
4282 printk(KERN_ERR "ide-tape: Failed to register character device interface\n");
4284 goto out_free_class;
4287 error = driver_register(&idetape_driver.gen_driver);
4289 goto out_free_driver;
4294 driver_unregister(&idetape_driver.gen_driver);
4296 class_destroy(idetape_sysfs_class);
4301 MODULE_ALIAS("ide:*m-tape*");
4302 module_init(idetape_init);
4303 module_exit(idetape_exit);
4304 MODULE_ALIAS_CHARDEV_MAJOR(IDETAPE_MAJOR);