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_INFO to 1 will report device capabilities.
110 * Setting IDETAPE_DEBUG_LOG to 1 will log driver flow control.
111 * Setting IDETAPE_DEBUG_BUGS to 1 will enable self-sanity checks in
114 * Setting them to 0 will restore normal operation mode:
116 * 1. Disable logging normal successful operations.
117 * 2. Disable self-sanity checks.
118 * 3. Errors will still be logged, of course.
120 * All the #if DEBUG code will be removed some day, when the driver
121 * is verified to be stable enough. This will make it much more
124 #define IDETAPE_DEBUG_INFO 0
125 #define IDETAPE_DEBUG_LOG 0
126 #define IDETAPE_DEBUG_BUGS 1
129 * After each failed packet command we issue a request sense command
130 * and retry the packet command IDETAPE_MAX_PC_RETRIES times.
132 * Setting IDETAPE_MAX_PC_RETRIES to 0 will disable retries.
134 #define IDETAPE_MAX_PC_RETRIES 3
137 * With each packet command, we allocate a buffer of
138 * IDETAPE_PC_BUFFER_SIZE bytes. This is used for several packet
139 * commands (Not for READ/WRITE commands).
141 #define IDETAPE_PC_BUFFER_SIZE 256
144 * In various places in the driver, we need to allocate storage
145 * for packet commands and requests, which will remain valid while
146 * we leave the driver to wait for an interrupt or a timeout event.
148 #define IDETAPE_PC_STACK (10 + IDETAPE_MAX_PC_RETRIES)
151 * Some drives (for example, Seagate STT3401A Travan) require a very long
152 * timeout, because they don't return an interrupt or clear their busy bit
153 * until after the command completes (even retension commands).
155 #define IDETAPE_WAIT_CMD (900*HZ)
158 * The following parameter is used to select the point in the internal
159 * tape fifo in which we will start to refill the buffer. Decreasing
160 * the following parameter will improve the system's latency and
161 * interactive response, while using a high value might improve system
164 #define IDETAPE_FIFO_THRESHOLD 2
167 * DSC polling parameters.
169 * Polling for DSC (a single bit in the status register) is a very
170 * important function in ide-tape. There are two cases in which we
173 * 1. Before a read/write packet command, to ensure that we
174 * can transfer data from/to the tape's data buffers, without
175 * causing an actual media access. In case the tape is not
176 * ready yet, we take out our request from the device
177 * request queue, so that ide.c will service requests from
178 * the other device on the same interface meanwhile.
180 * 2. After the successful initialization of a "media access
181 * packet command", which is a command which can take a long
182 * time to complete (it can be several seconds or even an hour).
184 * Again, we postpone our request in the middle to free the bus
185 * for the other device. The polling frequency here should be
186 * lower than the read/write frequency since those media access
187 * commands are slow. We start from a "fast" frequency -
188 * IDETAPE_DSC_MA_FAST (one second), and if we don't receive DSC
189 * after IDETAPE_DSC_MA_THRESHOLD (5 minutes), we switch it to a
190 * lower frequency - IDETAPE_DSC_MA_SLOW (1 minute).
192 * We also set a timeout for the timer, in case something goes wrong.
193 * The timeout should be longer then the maximum execution time of a
200 #define IDETAPE_DSC_RW_MIN 5*HZ/100 /* 50 msec */
201 #define IDETAPE_DSC_RW_MAX 40*HZ/100 /* 400 msec */
202 #define IDETAPE_DSC_RW_TIMEOUT 2*60*HZ /* 2 minutes */
203 #define IDETAPE_DSC_MA_FAST 2*HZ /* 2 seconds */
204 #define IDETAPE_DSC_MA_THRESHOLD 5*60*HZ /* 5 minutes */
205 #define IDETAPE_DSC_MA_SLOW 30*HZ /* 30 seconds */
206 #define IDETAPE_DSC_MA_TIMEOUT 2*60*60*HZ /* 2 hours */
208 /*************************** End of tunable parameters ***********************/
211 * Read/Write error simulation
213 #define SIMULATE_ERRORS 0
216 * For general magnetic tape device compatibility.
219 idetape_direction_none
,
220 idetape_direction_read
,
221 idetape_direction_write
222 } idetape_chrdev_direction_t
;
227 struct idetape_bh
*b_reqnext
;
232 * Our view of a packet command.
234 typedef struct idetape_packet_command_s
{
235 u8 c
[12]; /* Actual packet bytes */
236 int retries
; /* On each retry, we increment retries */
237 int error
; /* Error code */
238 int request_transfer
; /* Bytes to transfer */
239 int actually_transferred
; /* Bytes actually transferred */
240 int buffer_size
; /* Size of our data buffer */
241 struct idetape_bh
*bh
;
244 u8
*buffer
; /* Data buffer */
245 u8
*current_position
; /* Pointer into the above buffer */
246 ide_startstop_t (*callback
) (ide_drive_t
*); /* Called when this packet command is completed */
247 u8 pc_buffer
[IDETAPE_PC_BUFFER_SIZE
]; /* Temporary buffer */
248 unsigned long flags
; /* Status/Action bit flags: long for set_bit */
252 * Packet command flag bits.
254 /* Set when an error is considered normal - We won't retry */
256 /* 1 When polling for DSC on a media access command */
257 #define PC_WAIT_FOR_DSC 1
258 /* 1 when we prefer to use DMA if possible */
259 #define PC_DMA_RECOMMENDED 2
260 /* 1 while DMA in progress */
261 #define PC_DMA_IN_PROGRESS 3
262 /* 1 when encountered problem during DMA */
263 #define PC_DMA_ERROR 4
268 * Capabilities and Mechanical Status Page
271 unsigned page_code
:6; /* Page code - Should be 0x2a */
273 __u8 ps
:1; /* parameters saveable */
274 __u8 page_length
; /* Page Length - Should be 0x12 */
275 __u8 reserved2
, reserved3
;
276 unsigned ro
:1; /* Read Only Mode */
277 unsigned reserved4_1234
:4;
278 unsigned sprev
:1; /* Supports SPACE in the reverse direction */
279 unsigned reserved4_67
:2;
280 unsigned reserved5_012
:3;
281 unsigned efmt
:1; /* Supports ERASE command initiated formatting */
282 unsigned reserved5_4
:1;
283 unsigned qfa
:1; /* Supports the QFA two partition formats */
284 unsigned reserved5_67
:2;
285 unsigned lock
:1; /* Supports locking the volume */
286 unsigned locked
:1; /* The volume is locked */
287 unsigned prevent
:1; /* The device defaults in the prevent state after power up */
288 unsigned eject
:1; /* The device can eject the volume */
289 __u8 disconnect
:1; /* The device can break request > ctl */
291 unsigned ecc
:1; /* Supports error correction */
292 unsigned cmprs
:1; /* Supports data compression */
293 unsigned reserved7_0
:1;
294 unsigned blk512
:1; /* Supports 512 bytes block size */
295 unsigned blk1024
:1; /* Supports 1024 bytes block size */
296 unsigned reserved7_3_6
:4;
297 unsigned blk32768
:1; /* slowb - the device restricts the byte count for PIO */
298 /* transfers for slow buffer memory ??? */
299 /* Also 32768 block size in some cases */
300 __u16 max_speed
; /* Maximum speed supported in KBps */
301 __u8 reserved10
, reserved11
;
302 __u16 ctl
; /* Continuous Transfer Limit in blocks */
303 __u16 speed
; /* Current Speed, in KBps */
304 __u16 buffer_size
; /* Buffer Size, in 512 bytes */
305 __u8 reserved18
, reserved19
;
306 } idetape_capabilities_page_t
;
312 unsigned page_code
:6; /* Page code - Should be 0x30 */
313 unsigned reserved1_6
:1;
315 __u8 page_length
; /* Page Length - Should be 2 */
318 unsigned play32_5
:1;
319 unsigned reserved2_23
:2;
320 unsigned record32
:1;
321 unsigned record32_5
:1;
322 unsigned reserved2_6
:1;
324 } idetape_block_size_page_t
;
329 typedef struct idetape_stage_s
{
330 struct request rq
; /* The corresponding request */
331 struct idetape_bh
*bh
; /* The data buffers */
332 struct idetape_stage_s
*next
; /* Pointer to the next stage */
336 * Most of our global data which we need to save even as we leave the
337 * driver due to an interrupt or a timer event is stored in a variable
338 * of type idetape_tape_t, defined below.
340 typedef struct ide_tape_obj
{
342 ide_driver_t
*driver
;
343 struct gendisk
*disk
;
347 * Since a typical character device operation requires more
348 * than one packet command, we provide here enough memory
349 * for the maximum of interconnected packet commands.
350 * The packet commands are stored in the circular array pc_stack.
351 * pc_stack_index points to the last used entry, and warps around
352 * to the start when we get to the last array entry.
354 * pc points to the current processed packet command.
356 * failed_pc points to the last failed packet command, or contains
357 * NULL if we do not need to retry any packet command. This is
358 * required since an additional packet command is needed before the
359 * retry, to get detailed information on what went wrong.
361 /* Current packet command */
363 /* Last failed packet command */
364 idetape_pc_t
*failed_pc
;
365 /* Packet command stack */
366 idetape_pc_t pc_stack
[IDETAPE_PC_STACK
];
367 /* Next free packet command storage space */
369 struct request rq_stack
[IDETAPE_PC_STACK
];
370 /* We implement a circular array */
374 * DSC polling variables.
376 * While polling for DSC we use postponed_rq to postpone the
377 * current request so that ide.c will be able to service
378 * pending requests on the other device. Note that at most
379 * we will have only one DSC (usually data transfer) request
380 * in the device request queue. Additional requests can be
381 * queued in our internal pipeline, but they will be visible
382 * to ide.c only one at a time.
384 struct request
*postponed_rq
;
385 /* The time in which we started polling for DSC */
386 unsigned long dsc_polling_start
;
387 /* Timer used to poll for dsc */
388 struct timer_list dsc_timer
;
389 /* Read/Write dsc polling frequency */
390 unsigned long best_dsc_rw_frequency
;
391 /* The current polling frequency */
392 unsigned long dsc_polling_frequency
;
393 /* Maximum waiting time */
394 unsigned long dsc_timeout
;
397 * Read position information
401 unsigned int first_frame_position
;
402 unsigned int last_frame_position
;
403 unsigned int blocks_in_buffer
;
406 * Last error information
408 u8 sense_key
, asc
, ascq
;
411 * Character device operation
416 /* Current character device data transfer direction */
417 idetape_chrdev_direction_t chrdev_direction
;
422 /* Usually 512 or 1024 bytes */
423 unsigned short tape_block_size
;
425 /* Copy of the tape's Capabilities and Mechanical Page */
426 idetape_capabilities_page_t capabilities
;
429 * Active data transfer request parameters.
431 * At most, there is only one ide-tape originated data transfer
432 * request in the device request queue. This allows ide.c to
433 * easily service requests from the other device when we
434 * postpone our active request. In the pipelined operation
435 * mode, we use our internal pipeline structure to hold
436 * more data requests.
438 * The data buffer size is chosen based on the tape's
441 /* Pointer to the request which is waiting in the device request queue */
442 struct request
*active_data_request
;
443 /* Data buffer size (chosen based on the tape's recommendation */
445 idetape_stage_t
*merge_stage
;
446 int merge_stage_size
;
447 struct idetape_bh
*bh
;
452 * Pipeline parameters.
454 * To accomplish non-pipelined mode, we simply set the following
455 * variables to zero (or NULL, where appropriate).
457 /* Number of currently used stages */
459 /* Number of pending stages */
460 int nr_pending_stages
;
461 /* We will not allocate more than this number of stages */
462 int max_stages
, min_pipeline
, max_pipeline
;
463 /* The first stage which will be removed from the pipeline */
464 idetape_stage_t
*first_stage
;
465 /* The currently active stage */
466 idetape_stage_t
*active_stage
;
467 /* Will be serviced after the currently active request */
468 idetape_stage_t
*next_stage
;
469 /* New requests will be added to the pipeline here */
470 idetape_stage_t
*last_stage
;
471 /* Optional free stage which we can use */
472 idetape_stage_t
*cache_stage
;
474 /* Wasted space in each stage */
477 /* Status/Action flags: long for set_bit */
479 /* protects the ide-tape queue */
483 * Measures average tape speed
485 unsigned long avg_time
;
491 char firmware_revision
[6];
492 int firmware_revision_num
;
494 /* the door is currently locked */
496 /* the tape hardware is write protected */
498 /* the tape is write protected (hardware or opened as read-only) */
502 * Limit the number of times a request can
503 * be postponed, to avoid an infinite postpone
506 /* request postpone count limit */
510 * Measures number of frames:
512 * 1. written/read to/from the driver pipeline (pipeline_head).
513 * 2. written/read to/from the tape buffers (idetape_bh).
514 * 3. written/read by the tape to/from the media (tape_head).
522 * Speed control at the tape buffers input/output
524 unsigned long insert_time
;
527 int max_insert_speed
;
528 int measure_insert_time
;
531 * Measure tape still time, in milliseconds
533 unsigned long tape_still_time_begin
;
537 * Speed regulation negative feedback loop
540 int pipeline_head_speed
;
541 int controlled_pipeline_head_speed
;
542 int uncontrolled_pipeline_head_speed
;
543 int controlled_last_pipeline_head
;
544 int uncontrolled_last_pipeline_head
;
545 unsigned long uncontrolled_pipeline_head_time
;
546 unsigned long controlled_pipeline_head_time
;
547 int controlled_previous_pipeline_head
;
548 int uncontrolled_previous_pipeline_head
;
549 unsigned long controlled_previous_head_time
;
550 unsigned long uncontrolled_previous_head_time
;
551 int restart_speed_control_req
;
554 * Debug_level determines amount of debugging output;
555 * can be changed using /proc/ide/hdx/settings
556 * 0 : almost no debugging output
557 * 1 : 0+output errors only
558 * 2 : 1+output all sensekey/asc
559 * 3 : 2+follow all chrdev related procedures
560 * 4 : 3+follow all procedures
561 * 5 : 4+include pc_stack rq_stack info
562 * 6 : 5+USE_COUNT updates
567 static DEFINE_MUTEX(idetape_ref_mutex
);
569 static struct class *idetape_sysfs_class
;
571 #define to_ide_tape(obj) container_of(obj, struct ide_tape_obj, kref)
573 #define ide_tape_g(disk) \
574 container_of((disk)->private_data, struct ide_tape_obj, driver)
576 static struct ide_tape_obj
*ide_tape_get(struct gendisk
*disk
)
578 struct ide_tape_obj
*tape
= NULL
;
580 mutex_lock(&idetape_ref_mutex
);
581 tape
= ide_tape_g(disk
);
583 kref_get(&tape
->kref
);
584 mutex_unlock(&idetape_ref_mutex
);
588 static void ide_tape_release(struct kref
*);
590 static void ide_tape_put(struct ide_tape_obj
*tape
)
592 mutex_lock(&idetape_ref_mutex
);
593 kref_put(&tape
->kref
, ide_tape_release
);
594 mutex_unlock(&idetape_ref_mutex
);
600 #define DOOR_UNLOCKED 0
601 #define DOOR_LOCKED 1
602 #define DOOR_EXPLICITLY_LOCKED 2
605 * Tape flag bits values.
607 #define IDETAPE_IGNORE_DSC 0
608 #define IDETAPE_ADDRESS_VALID 1 /* 0 When the tape position is unknown */
609 #define IDETAPE_BUSY 2 /* Device already opened */
610 #define IDETAPE_PIPELINE_ERROR 3 /* Error detected in a pipeline stage */
611 #define IDETAPE_DETECT_BS 4 /* Attempt to auto-detect the current user block size */
612 #define IDETAPE_FILEMARK 5 /* Currently on a filemark */
613 #define IDETAPE_DRQ_INTERRUPT 6 /* DRQ interrupt device */
614 #define IDETAPE_READ_ERROR 7
615 #define IDETAPE_PIPELINE_ACTIVE 8 /* pipeline active */
616 /* 0 = no tape is loaded, so we don't rewind after ejecting */
617 #define IDETAPE_MEDIUM_PRESENT 9
620 * Supported ATAPI tape drives packet commands
622 #define IDETAPE_TEST_UNIT_READY_CMD 0x00
623 #define IDETAPE_REWIND_CMD 0x01
624 #define IDETAPE_REQUEST_SENSE_CMD 0x03
625 #define IDETAPE_READ_CMD 0x08
626 #define IDETAPE_WRITE_CMD 0x0a
627 #define IDETAPE_WRITE_FILEMARK_CMD 0x10
628 #define IDETAPE_SPACE_CMD 0x11
629 #define IDETAPE_INQUIRY_CMD 0x12
630 #define IDETAPE_ERASE_CMD 0x19
631 #define IDETAPE_MODE_SENSE_CMD 0x1a
632 #define IDETAPE_MODE_SELECT_CMD 0x15
633 #define IDETAPE_LOAD_UNLOAD_CMD 0x1b
634 #define IDETAPE_PREVENT_CMD 0x1e
635 #define IDETAPE_LOCATE_CMD 0x2b
636 #define IDETAPE_READ_POSITION_CMD 0x34
637 #define IDETAPE_READ_BUFFER_CMD 0x3c
638 #define IDETAPE_SET_SPEED_CMD 0xbb
641 * Some defines for the READ BUFFER command
643 #define IDETAPE_RETRIEVE_FAULTY_BLOCK 6
646 * Some defines for the SPACE command
648 #define IDETAPE_SPACE_OVER_FILEMARK 1
649 #define IDETAPE_SPACE_TO_EOD 3
652 * Some defines for the LOAD UNLOAD command
654 #define IDETAPE_LU_LOAD_MASK 1
655 #define IDETAPE_LU_RETENSION_MASK 2
656 #define IDETAPE_LU_EOT_MASK 4
659 * Special requests for our block device strategy routine.
661 * In order to service a character device command, we add special
662 * requests to the tail of our block device request queue and wait
663 * for their completion.
667 REQ_IDETAPE_PC1
= (1 << 0), /* packet command (first stage) */
668 REQ_IDETAPE_PC2
= (1 << 1), /* packet command (second stage) */
669 REQ_IDETAPE_READ
= (1 << 2),
670 REQ_IDETAPE_WRITE
= (1 << 3),
671 REQ_IDETAPE_READ_BUFFER
= (1 << 4),
675 * Error codes which are returned in rq->errors to the higher part
678 #define IDETAPE_ERROR_GENERAL 101
679 #define IDETAPE_ERROR_FILEMARK 102
680 #define IDETAPE_ERROR_EOD 103
683 * The following is used to format the general configuration word of
684 * the ATAPI IDENTIFY DEVICE command.
686 struct idetape_id_gcw
{
687 unsigned packet_size
:2; /* Packet Size */
688 unsigned reserved234
:3; /* Reserved */
689 unsigned drq_type
:2; /* Command packet DRQ type */
690 unsigned removable
:1; /* Removable media */
691 unsigned device_type
:5; /* Device type */
692 unsigned reserved13
:1; /* Reserved */
693 unsigned protocol
:2; /* Protocol type */
697 * INQUIRY packet command - Data Format (From Table 6-8 of QIC-157C)
700 unsigned device_type
:5; /* Peripheral Device Type */
701 unsigned reserved0_765
:3; /* Peripheral Qualifier - Reserved */
702 unsigned reserved1_6t0
:7; /* Reserved */
703 unsigned rmb
:1; /* Removable Medium Bit */
704 unsigned ansi_version
:3; /* ANSI Version */
705 unsigned ecma_version
:3; /* ECMA Version */
706 unsigned iso_version
:2; /* ISO Version */
707 unsigned response_format
:4; /* Response Data Format */
708 unsigned reserved3_45
:2; /* Reserved */
709 unsigned reserved3_6
:1; /* TrmIOP - Reserved */
710 unsigned reserved3_7
:1; /* AENC - Reserved */
711 __u8 additional_length
; /* Additional Length (total_length-4) */
712 __u8 rsv5
, rsv6
, rsv7
; /* Reserved */
713 __u8 vendor_id
[8]; /* Vendor Identification */
714 __u8 product_id
[16]; /* Product Identification */
715 __u8 revision_level
[4]; /* Revision Level */
716 __u8 vendor_specific
[20]; /* Vendor Specific - Optional */
717 __u8 reserved56t95
[40]; /* Reserved - Optional */
718 /* Additional information may be returned */
719 } idetape_inquiry_result_t
;
722 * READ POSITION packet command - Data Format (From Table 6-57)
725 unsigned reserved0_10
:2; /* Reserved */
726 unsigned bpu
:1; /* Block Position Unknown */
727 unsigned reserved0_543
:3; /* Reserved */
728 unsigned eop
:1; /* End Of Partition */
729 unsigned bop
:1; /* Beginning Of Partition */
730 u8 partition
; /* Partition Number */
731 u8 reserved2
, reserved3
; /* Reserved */
732 u32 first_block
; /* First Block Location */
733 u32 last_block
; /* Last Block Location (Optional) */
734 u8 reserved12
; /* Reserved */
735 u8 blocks_in_buffer
[3]; /* Blocks In Buffer - (Optional) */
736 u32 bytes_in_buffer
; /* Bytes In Buffer (Optional) */
737 } idetape_read_position_result_t
;
740 * Follows structures which are related to the SELECT SENSE / MODE SENSE
741 * packet commands. Those packet commands are still not supported
744 #define IDETAPE_BLOCK_DESCRIPTOR 0
745 #define IDETAPE_CAPABILITIES_PAGE 0x2a
746 #define IDETAPE_PARAMTR_PAGE 0x2b /* Onstream DI-x0 only */
747 #define IDETAPE_BLOCK_SIZE_PAGE 0x30
748 #define IDETAPE_BUFFER_FILLING_PAGE 0x33
751 * Mode Parameter Header for the MODE SENSE packet command
754 __u8 mode_data_length
; /* Length of the following data transfer */
755 __u8 medium_type
; /* Medium Type */
756 __u8 dsp
; /* Device Specific Parameter */
757 __u8 bdl
; /* Block Descriptor Length */
758 } idetape_mode_parameter_header_t
;
761 * Mode Parameter Block Descriptor the MODE SENSE packet command
763 * Support for block descriptors is optional.
766 __u8 density_code
; /* Medium density code */
767 __u8 blocks
[3]; /* Number of blocks */
768 __u8 reserved4
; /* Reserved */
769 __u8 length
[3]; /* Block Length */
770 } idetape_parameter_block_descriptor_t
;
773 * The Data Compression Page, as returned by the MODE SENSE packet command.
776 unsigned page_code
:6; /* Page Code - Should be 0xf */
777 unsigned reserved0
:1; /* Reserved */
779 __u8 page_length
; /* Page Length - Should be 14 */
780 unsigned reserved2
:6; /* Reserved */
781 unsigned dcc
:1; /* Data Compression Capable */
782 unsigned dce
:1; /* Data Compression Enable */
783 unsigned reserved3
:5; /* Reserved */
784 unsigned red
:2; /* Report Exception on Decompression */
785 unsigned dde
:1; /* Data Decompression Enable */
786 __u32 ca
; /* Compression Algorithm */
787 __u32 da
; /* Decompression Algorithm */
788 __u8 reserved
[4]; /* Reserved */
789 } idetape_data_compression_page_t
;
792 * The Medium Partition Page, as returned by the MODE SENSE packet command.
795 unsigned page_code
:6; /* Page Code - Should be 0x11 */
796 unsigned reserved1_6
:1; /* Reserved */
798 __u8 page_length
; /* Page Length - Should be 6 */
799 __u8 map
; /* Maximum Additional Partitions - Should be 0 */
800 __u8 apd
; /* Additional Partitions Defined - Should be 0 */
801 unsigned reserved4_012
:3; /* Reserved */
802 unsigned psum
:2; /* Should be 0 */
803 unsigned idp
:1; /* Should be 0 */
804 unsigned sdp
:1; /* Should be 0 */
805 unsigned fdp
:1; /* Fixed Data Partitions */
806 __u8 mfr
; /* Medium Format Recognition */
807 __u8 reserved
[2]; /* Reserved */
808 } idetape_medium_partition_page_t
;
811 * Run time configurable parameters.
814 int dsc_rw_frequency
;
815 int dsc_media_access_frequency
;
820 * The variables below are used for the character device interface.
821 * Additional state variables are defined in our ide_drive_t structure.
823 static struct ide_tape_obj
* idetape_devs
[MAX_HWIFS
* MAX_DRIVES
];
825 #define ide_tape_f(file) ((file)->private_data)
827 static struct ide_tape_obj
*ide_tape_chrdev_get(unsigned int i
)
829 struct ide_tape_obj
*tape
= NULL
;
831 mutex_lock(&idetape_ref_mutex
);
832 tape
= idetape_devs
[i
];
834 kref_get(&tape
->kref
);
835 mutex_unlock(&idetape_ref_mutex
);
840 * Function declarations
843 static int idetape_chrdev_release (struct inode
*inode
, struct file
*filp
);
844 static void idetape_write_release (ide_drive_t
*drive
, unsigned int minor
);
847 * Too bad. The drive wants to send us data which we are not ready to accept.
848 * Just throw it away.
850 static void idetape_discard_data (ide_drive_t
*drive
, unsigned int bcount
)
853 (void) HWIF(drive
)->INB(IDE_DATA_REG
);
856 static void idetape_input_buffers (ide_drive_t
*drive
, idetape_pc_t
*pc
, unsigned int bcount
)
858 struct idetape_bh
*bh
= pc
->bh
;
862 #if IDETAPE_DEBUG_BUGS
864 printk(KERN_ERR
"ide-tape: bh == NULL in "
865 "idetape_input_buffers\n");
866 idetape_discard_data(drive
, bcount
);
869 #endif /* IDETAPE_DEBUG_BUGS */
870 count
= min((unsigned int)(bh
->b_size
- atomic_read(&bh
->b_count
)), bcount
);
871 HWIF(drive
)->atapi_input_bytes(drive
, bh
->b_data
+ atomic_read(&bh
->b_count
), count
);
873 atomic_add(count
, &bh
->b_count
);
874 if (atomic_read(&bh
->b_count
) == bh
->b_size
) {
877 atomic_set(&bh
->b_count
, 0);
883 static void idetape_output_buffers (ide_drive_t
*drive
, idetape_pc_t
*pc
, unsigned int bcount
)
885 struct idetape_bh
*bh
= pc
->bh
;
889 #if IDETAPE_DEBUG_BUGS
891 printk(KERN_ERR
"ide-tape: bh == NULL in "
892 "idetape_output_buffers\n");
895 #endif /* IDETAPE_DEBUG_BUGS */
896 count
= min((unsigned int)pc
->b_count
, (unsigned int)bcount
);
897 HWIF(drive
)->atapi_output_bytes(drive
, pc
->b_data
, count
);
900 pc
->b_count
-= count
;
902 pc
->bh
= bh
= bh
->b_reqnext
;
904 pc
->b_data
= bh
->b_data
;
905 pc
->b_count
= atomic_read(&bh
->b_count
);
911 static void idetape_update_buffers (idetape_pc_t
*pc
)
913 struct idetape_bh
*bh
= pc
->bh
;
915 unsigned int bcount
= pc
->actually_transferred
;
917 if (test_bit(PC_WRITING
, &pc
->flags
))
920 #if IDETAPE_DEBUG_BUGS
922 printk(KERN_ERR
"ide-tape: bh == NULL in "
923 "idetape_update_buffers\n");
926 #endif /* IDETAPE_DEBUG_BUGS */
927 count
= min((unsigned int)bh
->b_size
, (unsigned int)bcount
);
928 atomic_set(&bh
->b_count
, count
);
929 if (atomic_read(&bh
->b_count
) == bh
->b_size
)
937 * idetape_next_pc_storage returns a pointer to a place in which we can
938 * safely store a packet command, even though we intend to leave the
939 * driver. A storage space for a maximum of IDETAPE_PC_STACK packet
940 * commands is allocated at initialization time.
942 static idetape_pc_t
*idetape_next_pc_storage (ide_drive_t
*drive
)
944 idetape_tape_t
*tape
= drive
->driver_data
;
946 #if IDETAPE_DEBUG_LOG
947 if (tape
->debug_level
>= 5)
948 printk(KERN_INFO
"ide-tape: pc_stack_index=%d\n",
949 tape
->pc_stack_index
);
950 #endif /* IDETAPE_DEBUG_LOG */
951 if (tape
->pc_stack_index
== IDETAPE_PC_STACK
)
952 tape
->pc_stack_index
=0;
953 return (&tape
->pc_stack
[tape
->pc_stack_index
++]);
957 * idetape_next_rq_storage is used along with idetape_next_pc_storage.
958 * Since we queue packet commands in the request queue, we need to
959 * allocate a request, along with the allocation of a packet command.
962 /**************************************************************
964 * This should get fixed to use kmalloc(.., GFP_ATOMIC) *
965 * followed later on by kfree(). -ml *
967 **************************************************************/
969 static struct request
*idetape_next_rq_storage (ide_drive_t
*drive
)
971 idetape_tape_t
*tape
= drive
->driver_data
;
973 #if IDETAPE_DEBUG_LOG
974 if (tape
->debug_level
>= 5)
975 printk(KERN_INFO
"ide-tape: rq_stack_index=%d\n",
976 tape
->rq_stack_index
);
977 #endif /* IDETAPE_DEBUG_LOG */
978 if (tape
->rq_stack_index
== IDETAPE_PC_STACK
)
979 tape
->rq_stack_index
=0;
980 return (&tape
->rq_stack
[tape
->rq_stack_index
++]);
984 * idetape_init_pc initializes a packet command.
986 static void idetape_init_pc (idetape_pc_t
*pc
)
988 memset(pc
->c
, 0, 12);
991 pc
->request_transfer
= 0;
992 pc
->buffer
= pc
->pc_buffer
;
993 pc
->buffer_size
= IDETAPE_PC_BUFFER_SIZE
;
999 * called on each failed packet command retry to analyze the request sense. We
1000 * currently do not utilize this information.
1002 static void idetape_analyze_error(ide_drive_t
*drive
, u8
*sense
)
1004 idetape_tape_t
*tape
= drive
->driver_data
;
1005 idetape_pc_t
*pc
= tape
->failed_pc
;
1007 tape
->sense_key
= sense
[2] & 0xF;
1008 tape
->asc
= sense
[12];
1009 tape
->ascq
= sense
[13];
1010 #if IDETAPE_DEBUG_LOG
1012 * Without debugging, we only log an error if we decided to give up
1015 if (tape
->debug_level
>= 1)
1016 printk(KERN_INFO
"ide-tape: pc = %x, sense key = %x, "
1017 "asc = %x, ascq = %x\n",
1018 pc
->c
[0], tape
->sense_key
,
1019 tape
->asc
, tape
->ascq
);
1020 #endif /* IDETAPE_DEBUG_LOG */
1022 /* Correct pc->actually_transferred by asking the tape. */
1023 if (test_bit(PC_DMA_ERROR
, &pc
->flags
)) {
1024 pc
->actually_transferred
= pc
->request_transfer
-
1025 tape
->tape_block_size
*
1026 ntohl(get_unaligned((u32
*)&sense
[3]));
1027 idetape_update_buffers(pc
);
1031 * If error was the result of a zero-length read or write command,
1032 * with sense key=5, asc=0x22, ascq=0, let it slide. Some drives
1033 * (i.e. Seagate STT3401A Travan) don't support 0-length read/writes.
1035 if ((pc
->c
[0] == IDETAPE_READ_CMD
|| pc
->c
[0] == IDETAPE_WRITE_CMD
)
1037 && pc
->c
[4] == 0 && pc
->c
[3] == 0 && pc
->c
[2] == 0) {
1038 if (tape
->sense_key
== 5) {
1039 /* don't report an error, everything's ok */
1041 /* don't retry read/write */
1042 set_bit(PC_ABORT
, &pc
->flags
);
1045 if (pc
->c
[0] == IDETAPE_READ_CMD
&& (sense
[2] & 0x80)) {
1046 pc
->error
= IDETAPE_ERROR_FILEMARK
;
1047 set_bit(PC_ABORT
, &pc
->flags
);
1049 if (pc
->c
[0] == IDETAPE_WRITE_CMD
) {
1050 if ((sense
[2] & 0x40) || (tape
->sense_key
== 0xd
1051 && tape
->asc
== 0x0 && tape
->ascq
== 0x2)) {
1052 pc
->error
= IDETAPE_ERROR_EOD
;
1053 set_bit(PC_ABORT
, &pc
->flags
);
1056 if (pc
->c
[0] == IDETAPE_READ_CMD
|| pc
->c
[0] == IDETAPE_WRITE_CMD
) {
1057 if (tape
->sense_key
== 8) {
1058 pc
->error
= IDETAPE_ERROR_EOD
;
1059 set_bit(PC_ABORT
, &pc
->flags
);
1061 if (!test_bit(PC_ABORT
, &pc
->flags
) &&
1062 pc
->actually_transferred
)
1063 pc
->retries
= IDETAPE_MAX_PC_RETRIES
+ 1;
1068 * idetape_active_next_stage will declare the next stage as "active".
1070 static void idetape_active_next_stage (ide_drive_t
*drive
)
1072 idetape_tape_t
*tape
= drive
->driver_data
;
1073 idetape_stage_t
*stage
= tape
->next_stage
;
1074 struct request
*rq
= &stage
->rq
;
1076 #if IDETAPE_DEBUG_LOG
1077 if (tape
->debug_level
>= 4)
1078 printk(KERN_INFO
"ide-tape: Reached idetape_active_next_stage\n");
1079 #endif /* IDETAPE_DEBUG_LOG */
1080 #if IDETAPE_DEBUG_BUGS
1081 if (stage
== NULL
) {
1082 printk(KERN_ERR
"ide-tape: bug: Trying to activate a non existing stage\n");
1085 #endif /* IDETAPE_DEBUG_BUGS */
1087 rq
->rq_disk
= tape
->disk
;
1089 rq
->special
= (void *)stage
->bh
;
1090 tape
->active_data_request
= rq
;
1091 tape
->active_stage
= stage
;
1092 tape
->next_stage
= stage
->next
;
1096 * idetape_increase_max_pipeline_stages is a part of the feedback
1097 * loop which tries to find the optimum number of stages. In the
1098 * feedback loop, we are starting from a minimum maximum number of
1099 * stages, and if we sense that the pipeline is empty, we try to
1100 * increase it, until we reach the user compile time memory limit.
1102 static void idetape_increase_max_pipeline_stages (ide_drive_t
*drive
)
1104 idetape_tape_t
*tape
= drive
->driver_data
;
1105 int increase
= (tape
->max_pipeline
- tape
->min_pipeline
) / 10;
1107 #if IDETAPE_DEBUG_LOG
1108 if (tape
->debug_level
>= 4)
1109 printk (KERN_INFO
"ide-tape: Reached idetape_increase_max_pipeline_stages\n");
1110 #endif /* IDETAPE_DEBUG_LOG */
1112 tape
->max_stages
+= max(increase
, 1);
1113 tape
->max_stages
= max(tape
->max_stages
, tape
->min_pipeline
);
1114 tape
->max_stages
= min(tape
->max_stages
, tape
->max_pipeline
);
1118 * idetape_kfree_stage calls kfree to completely free a stage, along with
1119 * its related buffers.
1121 static void __idetape_kfree_stage (idetape_stage_t
*stage
)
1123 struct idetape_bh
*prev_bh
, *bh
= stage
->bh
;
1126 while (bh
!= NULL
) {
1127 if (bh
->b_data
!= NULL
) {
1128 size
= (int) bh
->b_size
;
1130 free_page((unsigned long) bh
->b_data
);
1132 bh
->b_data
+= PAGE_SIZE
;
1142 static void idetape_kfree_stage (idetape_tape_t
*tape
, idetape_stage_t
*stage
)
1144 __idetape_kfree_stage(stage
);
1148 * idetape_remove_stage_head removes tape->first_stage from the pipeline.
1149 * The caller should avoid race conditions.
1151 static void idetape_remove_stage_head (ide_drive_t
*drive
)
1153 idetape_tape_t
*tape
= drive
->driver_data
;
1154 idetape_stage_t
*stage
;
1156 #if IDETAPE_DEBUG_LOG
1157 if (tape
->debug_level
>= 4)
1158 printk(KERN_INFO
"ide-tape: Reached idetape_remove_stage_head\n");
1159 #endif /* IDETAPE_DEBUG_LOG */
1160 #if IDETAPE_DEBUG_BUGS
1161 if (tape
->first_stage
== NULL
) {
1162 printk(KERN_ERR
"ide-tape: bug: tape->first_stage is NULL\n");
1165 if (tape
->active_stage
== tape
->first_stage
) {
1166 printk(KERN_ERR
"ide-tape: bug: Trying to free our active pipeline stage\n");
1169 #endif /* IDETAPE_DEBUG_BUGS */
1170 stage
= tape
->first_stage
;
1171 tape
->first_stage
= stage
->next
;
1172 idetape_kfree_stage(tape
, stage
);
1174 if (tape
->first_stage
== NULL
) {
1175 tape
->last_stage
= NULL
;
1176 #if IDETAPE_DEBUG_BUGS
1177 if (tape
->next_stage
!= NULL
)
1178 printk(KERN_ERR
"ide-tape: bug: tape->next_stage != NULL\n");
1179 if (tape
->nr_stages
)
1180 printk(KERN_ERR
"ide-tape: bug: nr_stages should be 0 now\n");
1181 #endif /* IDETAPE_DEBUG_BUGS */
1186 * This will free all the pipeline stages starting from new_last_stage->next
1187 * to the end of the list, and point tape->last_stage to new_last_stage.
1189 static void idetape_abort_pipeline(ide_drive_t
*drive
,
1190 idetape_stage_t
*new_last_stage
)
1192 idetape_tape_t
*tape
= drive
->driver_data
;
1193 idetape_stage_t
*stage
= new_last_stage
->next
;
1194 idetape_stage_t
*nstage
;
1196 #if IDETAPE_DEBUG_LOG
1197 if (tape
->debug_level
>= 4)
1198 printk(KERN_INFO
"ide-tape: %s: idetape_abort_pipeline called\n", tape
->name
);
1201 nstage
= stage
->next
;
1202 idetape_kfree_stage(tape
, stage
);
1204 --tape
->nr_pending_stages
;
1208 new_last_stage
->next
= NULL
;
1209 tape
->last_stage
= new_last_stage
;
1210 tape
->next_stage
= NULL
;
1214 * idetape_end_request is used to finish servicing a request, and to
1215 * insert a pending pipeline request into the main device queue.
1217 static int idetape_end_request(ide_drive_t
*drive
, int uptodate
, int nr_sects
)
1219 struct request
*rq
= HWGROUP(drive
)->rq
;
1220 idetape_tape_t
*tape
= drive
->driver_data
;
1221 unsigned long flags
;
1223 int remove_stage
= 0;
1224 idetape_stage_t
*active_stage
;
1226 #if IDETAPE_DEBUG_LOG
1227 if (tape
->debug_level
>= 4)
1228 printk(KERN_INFO
"ide-tape: Reached idetape_end_request\n");
1229 #endif /* IDETAPE_DEBUG_LOG */
1232 case 0: error
= IDETAPE_ERROR_GENERAL
; break;
1233 case 1: error
= 0; break;
1234 default: error
= uptodate
;
1238 tape
->failed_pc
= NULL
;
1240 if (!blk_special_request(rq
)) {
1241 ide_end_request(drive
, uptodate
, nr_sects
);
1245 spin_lock_irqsave(&tape
->spinlock
, flags
);
1247 /* The request was a pipelined data transfer request */
1248 if (tape
->active_data_request
== rq
) {
1249 active_stage
= tape
->active_stage
;
1250 tape
->active_stage
= NULL
;
1251 tape
->active_data_request
= NULL
;
1252 tape
->nr_pending_stages
--;
1253 if (rq
->cmd
[0] & REQ_IDETAPE_WRITE
) {
1256 set_bit(IDETAPE_PIPELINE_ERROR
, &tape
->flags
);
1257 if (error
== IDETAPE_ERROR_EOD
)
1258 idetape_abort_pipeline(drive
, active_stage
);
1260 } else if (rq
->cmd
[0] & REQ_IDETAPE_READ
) {
1261 if (error
== IDETAPE_ERROR_EOD
) {
1262 set_bit(IDETAPE_PIPELINE_ERROR
, &tape
->flags
);
1263 idetape_abort_pipeline(drive
, active_stage
);
1266 if (tape
->next_stage
!= NULL
) {
1267 idetape_active_next_stage(drive
);
1270 * Insert the next request into the request queue.
1272 (void) ide_do_drive_cmd(drive
, tape
->active_data_request
, ide_end
);
1273 } else if (!error
) {
1274 idetape_increase_max_pipeline_stages(drive
);
1277 ide_end_drive_cmd(drive
, 0, 0);
1278 // blkdev_dequeue_request(rq);
1279 // drive->rq = NULL;
1280 // end_that_request_last(rq);
1283 idetape_remove_stage_head(drive
);
1284 if (tape
->active_data_request
== NULL
)
1285 clear_bit(IDETAPE_PIPELINE_ACTIVE
, &tape
->flags
);
1286 spin_unlock_irqrestore(&tape
->spinlock
, flags
);
1290 static ide_startstop_t
idetape_request_sense_callback (ide_drive_t
*drive
)
1292 idetape_tape_t
*tape
= drive
->driver_data
;
1294 #if IDETAPE_DEBUG_LOG
1295 if (tape
->debug_level
>= 4)
1296 printk(KERN_INFO
"ide-tape: Reached idetape_request_sense_callback\n");
1297 #endif /* IDETAPE_DEBUG_LOG */
1298 if (!tape
->pc
->error
) {
1299 idetape_analyze_error(drive
, tape
->pc
->buffer
);
1300 idetape_end_request(drive
, 1, 0);
1302 printk(KERN_ERR
"ide-tape: Error in REQUEST SENSE itself - Aborting request!\n");
1303 idetape_end_request(drive
, 0, 0);
1308 static void idetape_create_request_sense_cmd (idetape_pc_t
*pc
)
1310 idetape_init_pc(pc
);
1311 pc
->c
[0] = IDETAPE_REQUEST_SENSE_CMD
;
1313 pc
->request_transfer
= 20;
1314 pc
->callback
= &idetape_request_sense_callback
;
1317 static void idetape_init_rq(struct request
*rq
, u8 cmd
)
1319 memset(rq
, 0, sizeof(*rq
));
1320 rq
->cmd_type
= REQ_TYPE_SPECIAL
;
1325 * idetape_queue_pc_head generates a new packet command request in front
1326 * of the request queue, before the current request, so that it will be
1327 * processed immediately, on the next pass through the driver.
1329 * idetape_queue_pc_head is called from the request handling part of
1330 * the driver (the "bottom" part). Safe storage for the request should
1331 * be allocated with idetape_next_pc_storage and idetape_next_rq_storage
1332 * before calling idetape_queue_pc_head.
1334 * Memory for those requests is pre-allocated at initialization time, and
1335 * is limited to IDETAPE_PC_STACK requests. We assume that we have enough
1336 * space for the maximum possible number of inter-dependent packet commands.
1338 * The higher level of the driver - The ioctl handler and the character
1339 * device handling functions should queue request to the lower level part
1340 * and wait for their completion using idetape_queue_pc_tail or
1341 * idetape_queue_rw_tail.
1343 static void idetape_queue_pc_head (ide_drive_t
*drive
, idetape_pc_t
*pc
,struct request
*rq
)
1345 struct ide_tape_obj
*tape
= drive
->driver_data
;
1347 idetape_init_rq(rq
, REQ_IDETAPE_PC1
);
1348 rq
->buffer
= (char *) pc
;
1349 rq
->rq_disk
= tape
->disk
;
1350 (void) ide_do_drive_cmd(drive
, rq
, ide_preempt
);
1354 * idetape_retry_pc is called when an error was detected during the
1355 * last packet command. We queue a request sense packet command in
1356 * the head of the request list.
1358 static ide_startstop_t
idetape_retry_pc (ide_drive_t
*drive
)
1360 idetape_tape_t
*tape
= drive
->driver_data
;
1364 (void)drive
->hwif
->INB(IDE_ERROR_REG
);
1365 pc
= idetape_next_pc_storage(drive
);
1366 rq
= idetape_next_rq_storage(drive
);
1367 idetape_create_request_sense_cmd(pc
);
1368 set_bit(IDETAPE_IGNORE_DSC
, &tape
->flags
);
1369 idetape_queue_pc_head(drive
, pc
, rq
);
1374 * idetape_postpone_request postpones the current request so that
1375 * ide.c will be able to service requests from another device on
1376 * the same hwgroup while we are polling for DSC.
1378 static void idetape_postpone_request (ide_drive_t
*drive
)
1380 idetape_tape_t
*tape
= drive
->driver_data
;
1382 #if IDETAPE_DEBUG_LOG
1383 if (tape
->debug_level
>= 4)
1384 printk(KERN_INFO
"ide-tape: idetape_postpone_request\n");
1386 tape
->postponed_rq
= HWGROUP(drive
)->rq
;
1387 ide_stall_queue(drive
, tape
->dsc_polling_frequency
);
1391 * idetape_pc_intr is the usual interrupt handler which will be called
1392 * during a packet command. We will transfer some of the data (as
1393 * requested by the drive) and will re-point interrupt handler to us.
1394 * When data transfer is finished, we will act according to the
1395 * algorithm described before idetape_issue_packet_command.
1398 static ide_startstop_t
idetape_pc_intr (ide_drive_t
*drive
)
1400 ide_hwif_t
*hwif
= drive
->hwif
;
1401 idetape_tape_t
*tape
= drive
->driver_data
;
1402 idetape_pc_t
*pc
= tape
->pc
;
1405 static int error_sim_count
= 0;
1410 #if IDETAPE_DEBUG_LOG
1411 if (tape
->debug_level
>= 4)
1412 printk(KERN_INFO
"ide-tape: Reached idetape_pc_intr "
1413 "interrupt handler\n");
1414 #endif /* IDETAPE_DEBUG_LOG */
1416 /* Clear the interrupt */
1417 stat
= hwif
->INB(IDE_STATUS_REG
);
1419 if (test_bit(PC_DMA_IN_PROGRESS
, &pc
->flags
)) {
1420 if (hwif
->ide_dma_end(drive
) || (stat
& ERR_STAT
)) {
1422 * A DMA error is sometimes expected. For example,
1423 * if the tape is crossing a filemark during a
1424 * READ command, it will issue an irq and position
1425 * itself before the filemark, so that only a partial
1426 * data transfer will occur (which causes the DMA
1427 * error). In that case, we will later ask the tape
1428 * how much bytes of the original request were
1429 * actually transferred (we can't receive that
1430 * information from the DMA engine on most chipsets).
1434 * On the contrary, a DMA error is never expected;
1435 * it usually indicates a hardware error or abort.
1436 * If the tape crosses a filemark during a READ
1437 * command, it will issue an irq and position itself
1438 * after the filemark (not before). Only a partial
1439 * data transfer will occur, but no DMA error.
1442 set_bit(PC_DMA_ERROR
, &pc
->flags
);
1444 pc
->actually_transferred
= pc
->request_transfer
;
1445 idetape_update_buffers(pc
);
1447 #if IDETAPE_DEBUG_LOG
1448 if (tape
->debug_level
>= 4)
1449 printk(KERN_INFO
"ide-tape: DMA finished\n");
1450 #endif /* IDETAPE_DEBUG_LOG */
1453 /* No more interrupts */
1454 if ((stat
& DRQ_STAT
) == 0) {
1455 #if IDETAPE_DEBUG_LOG
1456 if (tape
->debug_level
>= 2)
1457 printk(KERN_INFO
"ide-tape: Packet command completed, %d bytes transferred\n", pc
->actually_transferred
);
1458 #endif /* IDETAPE_DEBUG_LOG */
1459 clear_bit(PC_DMA_IN_PROGRESS
, &pc
->flags
);
1464 if ((pc
->c
[0] == IDETAPE_WRITE_CMD
||
1465 pc
->c
[0] == IDETAPE_READ_CMD
) &&
1466 (++error_sim_count
% 100) == 0) {
1467 printk(KERN_INFO
"ide-tape: %s: simulating error\n",
1472 if ((stat
& ERR_STAT
) && pc
->c
[0] == IDETAPE_REQUEST_SENSE_CMD
)
1474 if ((stat
& ERR_STAT
) || test_bit(PC_DMA_ERROR
, &pc
->flags
)) {
1475 /* Error detected */
1476 #if IDETAPE_DEBUG_LOG
1477 if (tape
->debug_level
>= 1)
1478 printk(KERN_INFO
"ide-tape: %s: I/O error\n",
1480 #endif /* IDETAPE_DEBUG_LOG */
1481 if (pc
->c
[0] == IDETAPE_REQUEST_SENSE_CMD
) {
1482 printk(KERN_ERR
"ide-tape: I/O error in request sense command\n");
1483 return ide_do_reset(drive
);
1485 #if IDETAPE_DEBUG_LOG
1486 if (tape
->debug_level
>= 1)
1487 printk(KERN_INFO
"ide-tape: [cmd %x]: check condition\n", pc
->c
[0]);
1489 /* Retry operation */
1490 return idetape_retry_pc(drive
);
1493 if (test_bit(PC_WAIT_FOR_DSC
, &pc
->flags
) &&
1494 (stat
& SEEK_STAT
) == 0) {
1495 /* Media access command */
1496 tape
->dsc_polling_start
= jiffies
;
1497 tape
->dsc_polling_frequency
= IDETAPE_DSC_MA_FAST
;
1498 tape
->dsc_timeout
= jiffies
+ IDETAPE_DSC_MA_TIMEOUT
;
1499 /* Allow ide.c to handle other requests */
1500 idetape_postpone_request(drive
);
1503 if (tape
->failed_pc
== pc
)
1504 tape
->failed_pc
= NULL
;
1505 /* Command finished - Call the callback function */
1506 return pc
->callback(drive
);
1508 if (test_and_clear_bit(PC_DMA_IN_PROGRESS
, &pc
->flags
)) {
1509 printk(KERN_ERR
"ide-tape: The tape wants to issue more "
1510 "interrupts in DMA mode\n");
1511 printk(KERN_ERR
"ide-tape: DMA disabled, reverting to PIO\n");
1513 return ide_do_reset(drive
);
1515 /* Get the number of bytes to transfer on this interrupt. */
1516 bcount
= (hwif
->INB(IDE_BCOUNTH_REG
) << 8) |
1517 hwif
->INB(IDE_BCOUNTL_REG
);
1519 ireason
= hwif
->INB(IDE_IREASON_REG
);
1522 printk(KERN_ERR
"ide-tape: CoD != 0 in idetape_pc_intr\n");
1523 return ide_do_reset(drive
);
1525 if (((ireason
& IO
) == IO
) == test_bit(PC_WRITING
, &pc
->flags
)) {
1526 /* Hopefully, we will never get here */
1527 printk(KERN_ERR
"ide-tape: We wanted to %s, ",
1528 (ireason
& IO
) ? "Write" : "Read");
1529 printk(KERN_ERR
"ide-tape: but the tape wants us to %s !\n",
1530 (ireason
& IO
) ? "Read" : "Write");
1531 return ide_do_reset(drive
);
1533 if (!test_bit(PC_WRITING
, &pc
->flags
)) {
1534 /* Reading - Check that we have enough space */
1535 temp
= pc
->actually_transferred
+ bcount
;
1536 if (temp
> pc
->request_transfer
) {
1537 if (temp
> pc
->buffer_size
) {
1538 printk(KERN_ERR
"ide-tape: The tape wants to send us more data than expected - discarding data\n");
1539 idetape_discard_data(drive
, bcount
);
1540 ide_set_handler(drive
, &idetape_pc_intr
, IDETAPE_WAIT_CMD
, NULL
);
1543 #if IDETAPE_DEBUG_LOG
1544 if (tape
->debug_level
>= 2)
1545 printk(KERN_NOTICE
"ide-tape: The tape wants to send us more data than expected - allowing transfer\n");
1546 #endif /* IDETAPE_DEBUG_LOG */
1549 if (test_bit(PC_WRITING
, &pc
->flags
)) {
1551 idetape_output_buffers(drive
, pc
, bcount
);
1553 /* Write the current buffer */
1554 hwif
->atapi_output_bytes(drive
, pc
->current_position
,
1558 idetape_input_buffers(drive
, pc
, bcount
);
1560 /* Read the current buffer */
1561 hwif
->atapi_input_bytes(drive
, pc
->current_position
,
1564 /* Update the current position */
1565 pc
->actually_transferred
+= bcount
;
1566 pc
->current_position
+= bcount
;
1567 #if IDETAPE_DEBUG_LOG
1568 if (tape
->debug_level
>= 2)
1569 printk(KERN_INFO
"ide-tape: [cmd %x] transferred %d bytes "
1570 "on that interrupt\n", pc
->c
[0], bcount
);
1572 /* And set the interrupt handler again */
1573 ide_set_handler(drive
, &idetape_pc_intr
, IDETAPE_WAIT_CMD
, NULL
);
1578 * Packet Command Interface
1580 * The current Packet Command is available in tape->pc, and will not
1581 * change until we finish handling it. Each packet command is associated
1582 * with a callback function that will be called when the command is
1585 * The handling will be done in three stages:
1587 * 1. idetape_issue_packet_command will send the packet command to the
1588 * drive, and will set the interrupt handler to idetape_pc_intr.
1590 * 2. On each interrupt, idetape_pc_intr will be called. This step
1591 * will be repeated until the device signals us that no more
1592 * interrupts will be issued.
1594 * 3. ATAPI Tape media access commands have immediate status with a
1595 * delayed process. In case of a successful initiation of a
1596 * media access packet command, the DSC bit will be set when the
1597 * actual execution of the command is finished.
1598 * Since the tape drive will not issue an interrupt, we have to
1599 * poll for this event. In this case, we define the request as
1600 * "low priority request" by setting rq_status to
1601 * IDETAPE_RQ_POSTPONED, set a timer to poll for DSC and exit
1604 * ide.c will then give higher priority to requests which
1605 * originate from the other device, until will change rq_status
1608 * 4. When the packet command is finished, it will be checked for errors.
1610 * 5. In case an error was found, we queue a request sense packet
1611 * command in front of the request queue and retry the operation
1612 * up to IDETAPE_MAX_PC_RETRIES times.
1614 * 6. In case no error was found, or we decided to give up and not
1615 * to retry again, the callback function will be called and then
1616 * we will handle the next request.
1619 static ide_startstop_t
idetape_transfer_pc(ide_drive_t
*drive
)
1621 ide_hwif_t
*hwif
= drive
->hwif
;
1622 idetape_tape_t
*tape
= drive
->driver_data
;
1623 idetape_pc_t
*pc
= tape
->pc
;
1625 ide_startstop_t startstop
;
1628 if (ide_wait_stat(&startstop
,drive
,DRQ_STAT
,BUSY_STAT
,WAIT_READY
)) {
1629 printk(KERN_ERR
"ide-tape: Strange, packet command initiated yet DRQ isn't asserted\n");
1632 ireason
= hwif
->INB(IDE_IREASON_REG
);
1633 while (retries
-- && ((ireason
& CD
) == 0 || (ireason
& IO
))) {
1634 printk(KERN_ERR
"ide-tape: (IO,CoD != (0,1) while issuing "
1635 "a packet command, retrying\n");
1637 ireason
= hwif
->INB(IDE_IREASON_REG
);
1639 printk(KERN_ERR
"ide-tape: (IO,CoD != (0,1) while "
1640 "issuing a packet command, ignoring\n");
1645 if ((ireason
& CD
) == 0 || (ireason
& IO
)) {
1646 printk(KERN_ERR
"ide-tape: (IO,CoD) != (0,1) while issuing "
1647 "a packet command\n");
1648 return ide_do_reset(drive
);
1650 /* Set the interrupt routine */
1651 ide_set_handler(drive
, &idetape_pc_intr
, IDETAPE_WAIT_CMD
, NULL
);
1652 #ifdef CONFIG_BLK_DEV_IDEDMA
1653 /* Begin DMA, if necessary */
1654 if (test_bit(PC_DMA_IN_PROGRESS
, &pc
->flags
))
1655 hwif
->dma_start(drive
);
1657 /* Send the actual packet */
1658 HWIF(drive
)->atapi_output_bytes(drive
, pc
->c
, 12);
1662 static ide_startstop_t
idetape_issue_packet_command (ide_drive_t
*drive
, idetape_pc_t
*pc
)
1664 ide_hwif_t
*hwif
= drive
->hwif
;
1665 idetape_tape_t
*tape
= drive
->driver_data
;
1669 #if IDETAPE_DEBUG_BUGS
1670 if (tape
->pc
->c
[0] == IDETAPE_REQUEST_SENSE_CMD
&&
1671 pc
->c
[0] == IDETAPE_REQUEST_SENSE_CMD
) {
1672 printk(KERN_ERR
"ide-tape: possible ide-tape.c bug - "
1673 "Two request sense in serial were issued\n");
1675 #endif /* IDETAPE_DEBUG_BUGS */
1677 if (tape
->failed_pc
== NULL
&& pc
->c
[0] != IDETAPE_REQUEST_SENSE_CMD
)
1678 tape
->failed_pc
= pc
;
1679 /* Set the current packet command */
1682 if (pc
->retries
> IDETAPE_MAX_PC_RETRIES
||
1683 test_bit(PC_ABORT
, &pc
->flags
)) {
1685 * We will "abort" retrying a packet command in case
1686 * a legitimate error code was received (crossing a
1687 * filemark, or end of the media, for example).
1689 if (!test_bit(PC_ABORT
, &pc
->flags
)) {
1690 if (!(pc
->c
[0] == IDETAPE_TEST_UNIT_READY_CMD
&&
1691 tape
->sense_key
== 2 && tape
->asc
== 4 &&
1692 (tape
->ascq
== 1 || tape
->ascq
== 8))) {
1693 printk(KERN_ERR
"ide-tape: %s: I/O error, "
1694 "pc = %2x, key = %2x, "
1695 "asc = %2x, ascq = %2x\n",
1696 tape
->name
, pc
->c
[0],
1697 tape
->sense_key
, tape
->asc
,
1701 pc
->error
= IDETAPE_ERROR_GENERAL
;
1703 tape
->failed_pc
= NULL
;
1704 return pc
->callback(drive
);
1706 #if IDETAPE_DEBUG_LOG
1707 if (tape
->debug_level
>= 2)
1708 printk(KERN_INFO
"ide-tape: Retry number - %d, cmd = %02X\n", pc
->retries
, pc
->c
[0]);
1709 #endif /* IDETAPE_DEBUG_LOG */
1712 /* We haven't transferred any data yet */
1713 pc
->actually_transferred
= 0;
1714 pc
->current_position
= pc
->buffer
;
1715 /* Request to transfer the entire buffer at once */
1716 bcount
= pc
->request_transfer
;
1718 if (test_and_clear_bit(PC_DMA_ERROR
, &pc
->flags
)) {
1719 printk(KERN_WARNING
"ide-tape: DMA disabled, "
1720 "reverting to PIO\n");
1723 if (test_bit(PC_DMA_RECOMMENDED
, &pc
->flags
) && drive
->using_dma
)
1724 dma_ok
= !hwif
->dma_setup(drive
);
1726 ide_pktcmd_tf_load(drive
, IDE_TFLAG_NO_SELECT_MASK
|
1727 IDE_TFLAG_OUT_DEVICE
, bcount
, dma_ok
);
1729 if (dma_ok
) /* Will begin DMA later */
1730 set_bit(PC_DMA_IN_PROGRESS
, &pc
->flags
);
1731 if (test_bit(IDETAPE_DRQ_INTERRUPT
, &tape
->flags
)) {
1732 ide_execute_command(drive
, WIN_PACKETCMD
, &idetape_transfer_pc
,
1733 IDETAPE_WAIT_CMD
, NULL
);
1736 hwif
->OUTB(WIN_PACKETCMD
, IDE_COMMAND_REG
);
1737 return idetape_transfer_pc(drive
);
1742 * General packet command callback function.
1744 static ide_startstop_t
idetape_pc_callback (ide_drive_t
*drive
)
1746 idetape_tape_t
*tape
= drive
->driver_data
;
1748 #if IDETAPE_DEBUG_LOG
1749 if (tape
->debug_level
>= 4)
1750 printk(KERN_INFO
"ide-tape: Reached idetape_pc_callback\n");
1751 #endif /* IDETAPE_DEBUG_LOG */
1753 idetape_end_request(drive
, tape
->pc
->error
? 0 : 1, 0);
1758 * A mode sense command is used to "sense" tape parameters.
1760 static void idetape_create_mode_sense_cmd (idetape_pc_t
*pc
, u8 page_code
)
1762 idetape_init_pc(pc
);
1763 pc
->c
[0] = IDETAPE_MODE_SENSE_CMD
;
1764 if (page_code
!= IDETAPE_BLOCK_DESCRIPTOR
)
1765 pc
->c
[1] = 8; /* DBD = 1 - Don't return block descriptors */
1766 pc
->c
[2] = page_code
;
1768 * Changed pc->c[3] to 0 (255 will at best return unused info).
1770 * For SCSI this byte is defined as subpage instead of high byte
1771 * of length and some IDE drives seem to interpret it this way
1772 * and return an error when 255 is used.
1775 pc
->c
[4] = 255; /* (We will just discard data in that case) */
1776 if (page_code
== IDETAPE_BLOCK_DESCRIPTOR
)
1777 pc
->request_transfer
= 12;
1778 else if (page_code
== IDETAPE_CAPABILITIES_PAGE
)
1779 pc
->request_transfer
= 24;
1781 pc
->request_transfer
= 50;
1782 pc
->callback
= &idetape_pc_callback
;
1785 static void calculate_speeds(ide_drive_t
*drive
)
1787 idetape_tape_t
*tape
= drive
->driver_data
;
1788 int full
= 125, empty
= 75;
1790 if (time_after(jiffies
, tape
->controlled_pipeline_head_time
+ 120 * HZ
)) {
1791 tape
->controlled_previous_pipeline_head
= tape
->controlled_last_pipeline_head
;
1792 tape
->controlled_previous_head_time
= tape
->controlled_pipeline_head_time
;
1793 tape
->controlled_last_pipeline_head
= tape
->pipeline_head
;
1794 tape
->controlled_pipeline_head_time
= jiffies
;
1796 if (time_after(jiffies
, tape
->controlled_pipeline_head_time
+ 60 * HZ
))
1797 tape
->controlled_pipeline_head_speed
= (tape
->pipeline_head
- tape
->controlled_last_pipeline_head
) * 32 * HZ
/ (jiffies
- tape
->controlled_pipeline_head_time
);
1798 else if (time_after(jiffies
, tape
->controlled_previous_head_time
))
1799 tape
->controlled_pipeline_head_speed
= (tape
->pipeline_head
- tape
->controlled_previous_pipeline_head
) * 32 * HZ
/ (jiffies
- tape
->controlled_previous_head_time
);
1801 if (tape
->nr_pending_stages
< tape
->max_stages
/*- 1 */) {
1802 /* -1 for read mode error recovery */
1803 if (time_after(jiffies
, tape
->uncontrolled_previous_head_time
+ 10 * HZ
)) {
1804 tape
->uncontrolled_pipeline_head_time
= jiffies
;
1805 tape
->uncontrolled_pipeline_head_speed
= (tape
->pipeline_head
- tape
->uncontrolled_previous_pipeline_head
) * 32 * HZ
/ (jiffies
- tape
->uncontrolled_previous_head_time
);
1808 tape
->uncontrolled_previous_head_time
= jiffies
;
1809 tape
->uncontrolled_previous_pipeline_head
= tape
->pipeline_head
;
1810 if (time_after(jiffies
, tape
->uncontrolled_pipeline_head_time
+ 30 * HZ
)) {
1811 tape
->uncontrolled_pipeline_head_time
= jiffies
;
1814 tape
->pipeline_head_speed
= max(tape
->uncontrolled_pipeline_head_speed
, tape
->controlled_pipeline_head_speed
);
1815 if (tape
->speed_control
== 0) {
1816 tape
->max_insert_speed
= 5000;
1817 } else if (tape
->speed_control
== 1) {
1818 if (tape
->nr_pending_stages
>= tape
->max_stages
/ 2)
1819 tape
->max_insert_speed
= tape
->pipeline_head_speed
+
1820 (1100 - tape
->pipeline_head_speed
) * 2 * (tape
->nr_pending_stages
- tape
->max_stages
/ 2) / tape
->max_stages
;
1822 tape
->max_insert_speed
= 500 +
1823 (tape
->pipeline_head_speed
- 500) * 2 * tape
->nr_pending_stages
/ tape
->max_stages
;
1824 if (tape
->nr_pending_stages
>= tape
->max_stages
* 99 / 100)
1825 tape
->max_insert_speed
= 5000;
1826 } else if (tape
->speed_control
== 2) {
1827 tape
->max_insert_speed
= tape
->pipeline_head_speed
* empty
/ 100 +
1828 (tape
->pipeline_head_speed
* full
/ 100 - tape
->pipeline_head_speed
* empty
/ 100) * tape
->nr_pending_stages
/ tape
->max_stages
;
1830 tape
->max_insert_speed
= tape
->speed_control
;
1831 tape
->max_insert_speed
= max(tape
->max_insert_speed
, 500);
1834 static ide_startstop_t
idetape_media_access_finished (ide_drive_t
*drive
)
1836 idetape_tape_t
*tape
= drive
->driver_data
;
1837 idetape_pc_t
*pc
= tape
->pc
;
1840 stat
= drive
->hwif
->INB(IDE_STATUS_REG
);
1841 if (stat
& SEEK_STAT
) {
1842 if (stat
& ERR_STAT
) {
1843 /* Error detected */
1844 if (pc
->c
[0] != IDETAPE_TEST_UNIT_READY_CMD
)
1845 printk(KERN_ERR
"ide-tape: %s: I/O error, ",
1847 /* Retry operation */
1848 return idetape_retry_pc(drive
);
1851 if (tape
->failed_pc
== pc
)
1852 tape
->failed_pc
= NULL
;
1854 pc
->error
= IDETAPE_ERROR_GENERAL
;
1855 tape
->failed_pc
= NULL
;
1857 return pc
->callback(drive
);
1860 static ide_startstop_t
idetape_rw_callback (ide_drive_t
*drive
)
1862 idetape_tape_t
*tape
= drive
->driver_data
;
1863 struct request
*rq
= HWGROUP(drive
)->rq
;
1864 int blocks
= tape
->pc
->actually_transferred
/ tape
->tape_block_size
;
1866 tape
->avg_size
+= blocks
* tape
->tape_block_size
;
1867 tape
->insert_size
+= blocks
* tape
->tape_block_size
;
1868 if (tape
->insert_size
> 1024 * 1024)
1869 tape
->measure_insert_time
= 1;
1870 if (tape
->measure_insert_time
) {
1871 tape
->measure_insert_time
= 0;
1872 tape
->insert_time
= jiffies
;
1873 tape
->insert_size
= 0;
1875 if (time_after(jiffies
, tape
->insert_time
))
1876 tape
->insert_speed
= tape
->insert_size
/ 1024 * HZ
/ (jiffies
- tape
->insert_time
);
1877 if (time_after_eq(jiffies
, tape
->avg_time
+ HZ
)) {
1878 tape
->avg_speed
= tape
->avg_size
* HZ
/ (jiffies
- tape
->avg_time
) / 1024;
1880 tape
->avg_time
= jiffies
;
1883 #if IDETAPE_DEBUG_LOG
1884 if (tape
->debug_level
>= 4)
1885 printk(KERN_INFO
"ide-tape: Reached idetape_rw_callback\n");
1886 #endif /* IDETAPE_DEBUG_LOG */
1888 tape
->first_frame_position
+= blocks
;
1889 rq
->current_nr_sectors
-= blocks
;
1891 if (!tape
->pc
->error
)
1892 idetape_end_request(drive
, 1, 0);
1894 idetape_end_request(drive
, tape
->pc
->error
, 0);
1898 static void idetape_create_read_cmd(idetape_tape_t
*tape
, idetape_pc_t
*pc
, unsigned int length
, struct idetape_bh
*bh
)
1900 idetape_init_pc(pc
);
1901 pc
->c
[0] = IDETAPE_READ_CMD
;
1902 put_unaligned(htonl(length
), (unsigned int *) &pc
->c
[1]);
1904 pc
->callback
= &idetape_rw_callback
;
1906 atomic_set(&bh
->b_count
, 0);
1908 pc
->request_transfer
= pc
->buffer_size
= length
* tape
->tape_block_size
;
1909 if (pc
->request_transfer
== tape
->stage_size
)
1910 set_bit(PC_DMA_RECOMMENDED
, &pc
->flags
);
1913 static void idetape_create_read_buffer_cmd(idetape_tape_t
*tape
, idetape_pc_t
*pc
, unsigned int length
, struct idetape_bh
*bh
)
1916 struct idetape_bh
*p
= bh
;
1918 idetape_init_pc(pc
);
1919 pc
->c
[0] = IDETAPE_READ_BUFFER_CMD
;
1920 pc
->c
[1] = IDETAPE_RETRIEVE_FAULTY_BLOCK
;
1921 pc
->c
[7] = size
>> 8;
1922 pc
->c
[8] = size
& 0xff;
1923 pc
->callback
= &idetape_pc_callback
;
1925 atomic_set(&bh
->b_count
, 0);
1928 atomic_set(&p
->b_count
, 0);
1931 pc
->request_transfer
= pc
->buffer_size
= size
;
1934 static void idetape_create_write_cmd(idetape_tape_t
*tape
, idetape_pc_t
*pc
, unsigned int length
, struct idetape_bh
*bh
)
1936 idetape_init_pc(pc
);
1937 pc
->c
[0] = IDETAPE_WRITE_CMD
;
1938 put_unaligned(htonl(length
), (unsigned int *) &pc
->c
[1]);
1940 pc
->callback
= &idetape_rw_callback
;
1941 set_bit(PC_WRITING
, &pc
->flags
);
1943 pc
->b_data
= bh
->b_data
;
1944 pc
->b_count
= atomic_read(&bh
->b_count
);
1946 pc
->request_transfer
= pc
->buffer_size
= length
* tape
->tape_block_size
;
1947 if (pc
->request_transfer
== tape
->stage_size
)
1948 set_bit(PC_DMA_RECOMMENDED
, &pc
->flags
);
1952 * idetape_do_request is our request handling function.
1954 static ide_startstop_t
idetape_do_request(ide_drive_t
*drive
,
1955 struct request
*rq
, sector_t block
)
1957 idetape_tape_t
*tape
= drive
->driver_data
;
1958 idetape_pc_t
*pc
= NULL
;
1959 struct request
*postponed_rq
= tape
->postponed_rq
;
1962 #if IDETAPE_DEBUG_LOG
1963 if (tape
->debug_level
>= 2)
1964 printk(KERN_INFO
"ide-tape: sector: %ld, "
1965 "nr_sectors: %ld, current_nr_sectors: %d\n",
1966 rq
->sector
, rq
->nr_sectors
, rq
->current_nr_sectors
);
1967 #endif /* IDETAPE_DEBUG_LOG */
1969 if (!blk_special_request(rq
)) {
1971 * We do not support buffer cache originated requests.
1973 printk(KERN_NOTICE
"ide-tape: %s: Unsupported request in "
1974 "request queue (%d)\n", drive
->name
, rq
->cmd_type
);
1975 ide_end_request(drive
, 0, 0);
1980 * Retry a failed packet command
1982 if (tape
->failed_pc
!= NULL
&&
1983 tape
->pc
->c
[0] == IDETAPE_REQUEST_SENSE_CMD
) {
1984 return idetape_issue_packet_command(drive
, tape
->failed_pc
);
1986 #if IDETAPE_DEBUG_BUGS
1987 if (postponed_rq
!= NULL
)
1988 if (rq
!= postponed_rq
) {
1989 printk(KERN_ERR
"ide-tape: ide-tape.c bug - "
1990 "Two DSC requests were queued\n");
1991 idetape_end_request(drive
, 0, 0);
1994 #endif /* IDETAPE_DEBUG_BUGS */
1996 tape
->postponed_rq
= NULL
;
1999 * If the tape is still busy, postpone our request and service
2000 * the other device meanwhile.
2002 stat
= drive
->hwif
->INB(IDE_STATUS_REG
);
2004 if (!drive
->dsc_overlap
&& !(rq
->cmd
[0] & REQ_IDETAPE_PC2
))
2005 set_bit(IDETAPE_IGNORE_DSC
, &tape
->flags
);
2007 if (drive
->post_reset
== 1) {
2008 set_bit(IDETAPE_IGNORE_DSC
, &tape
->flags
);
2009 drive
->post_reset
= 0;
2012 if (tape
->tape_still_time
> 100 && tape
->tape_still_time
< 200)
2013 tape
->measure_insert_time
= 1;
2014 if (time_after(jiffies
, tape
->insert_time
))
2015 tape
->insert_speed
= tape
->insert_size
/ 1024 * HZ
/ (jiffies
- tape
->insert_time
);
2016 calculate_speeds(drive
);
2017 if (!test_and_clear_bit(IDETAPE_IGNORE_DSC
, &tape
->flags
) &&
2018 (stat
& SEEK_STAT
) == 0) {
2019 if (postponed_rq
== NULL
) {
2020 tape
->dsc_polling_start
= jiffies
;
2021 tape
->dsc_polling_frequency
= tape
->best_dsc_rw_frequency
;
2022 tape
->dsc_timeout
= jiffies
+ IDETAPE_DSC_RW_TIMEOUT
;
2023 } else if (time_after(jiffies
, tape
->dsc_timeout
)) {
2024 printk(KERN_ERR
"ide-tape: %s: DSC timeout\n",
2026 if (rq
->cmd
[0] & REQ_IDETAPE_PC2
) {
2027 idetape_media_access_finished(drive
);
2030 return ide_do_reset(drive
);
2032 } else if (time_after(jiffies
, tape
->dsc_polling_start
+ IDETAPE_DSC_MA_THRESHOLD
))
2033 tape
->dsc_polling_frequency
= IDETAPE_DSC_MA_SLOW
;
2034 idetape_postpone_request(drive
);
2037 if (rq
->cmd
[0] & REQ_IDETAPE_READ
) {
2038 tape
->buffer_head
++;
2039 tape
->postpone_cnt
= 0;
2040 pc
= idetape_next_pc_storage(drive
);
2041 idetape_create_read_cmd(tape
, pc
, rq
->current_nr_sectors
, (struct idetape_bh
*)rq
->special
);
2044 if (rq
->cmd
[0] & REQ_IDETAPE_WRITE
) {
2045 tape
->buffer_head
++;
2046 tape
->postpone_cnt
= 0;
2047 pc
= idetape_next_pc_storage(drive
);
2048 idetape_create_write_cmd(tape
, pc
, rq
->current_nr_sectors
, (struct idetape_bh
*)rq
->special
);
2051 if (rq
->cmd
[0] & REQ_IDETAPE_READ_BUFFER
) {
2052 tape
->postpone_cnt
= 0;
2053 pc
= idetape_next_pc_storage(drive
);
2054 idetape_create_read_buffer_cmd(tape
, pc
, rq
->current_nr_sectors
, (struct idetape_bh
*)rq
->special
);
2057 if (rq
->cmd
[0] & REQ_IDETAPE_PC1
) {
2058 pc
= (idetape_pc_t
*) rq
->buffer
;
2059 rq
->cmd
[0] &= ~(REQ_IDETAPE_PC1
);
2060 rq
->cmd
[0] |= REQ_IDETAPE_PC2
;
2063 if (rq
->cmd
[0] & REQ_IDETAPE_PC2
) {
2064 idetape_media_access_finished(drive
);
2069 return idetape_issue_packet_command(drive
, pc
);
2073 * Pipeline related functions
2075 static inline int idetape_pipeline_active (idetape_tape_t
*tape
)
2079 rc1
= test_bit(IDETAPE_PIPELINE_ACTIVE
, &tape
->flags
);
2080 rc2
= (tape
->active_data_request
!= NULL
);
2085 * idetape_kmalloc_stage uses __get_free_page to allocate a pipeline
2086 * stage, along with all the necessary small buffers which together make
2087 * a buffer of size tape->stage_size (or a bit more). We attempt to
2088 * combine sequential pages as much as possible.
2090 * Returns a pointer to the new allocated stage, or NULL if we
2091 * can't (or don't want to) allocate a stage.
2093 * Pipeline stages are optional and are used to increase performance.
2094 * If we can't allocate them, we'll manage without them.
2096 static idetape_stage_t
*__idetape_kmalloc_stage (idetape_tape_t
*tape
, int full
, int clear
)
2098 idetape_stage_t
*stage
;
2099 struct idetape_bh
*prev_bh
, *bh
;
2100 int pages
= tape
->pages_per_stage
;
2101 char *b_data
= NULL
;
2103 if ((stage
= kmalloc(sizeof (idetape_stage_t
),GFP_KERNEL
)) == NULL
)
2107 bh
= stage
->bh
= kmalloc(sizeof(struct idetape_bh
), GFP_KERNEL
);
2110 bh
->b_reqnext
= NULL
;
2111 if ((bh
->b_data
= (char *) __get_free_page (GFP_KERNEL
)) == NULL
)
2114 memset(bh
->b_data
, 0, PAGE_SIZE
);
2115 bh
->b_size
= PAGE_SIZE
;
2116 atomic_set(&bh
->b_count
, full
? bh
->b_size
: 0);
2119 if ((b_data
= (char *) __get_free_page (GFP_KERNEL
)) == NULL
)
2122 memset(b_data
, 0, PAGE_SIZE
);
2123 if (bh
->b_data
== b_data
+ PAGE_SIZE
) {
2124 bh
->b_size
+= PAGE_SIZE
;
2125 bh
->b_data
-= PAGE_SIZE
;
2127 atomic_add(PAGE_SIZE
, &bh
->b_count
);
2130 if (b_data
== bh
->b_data
+ bh
->b_size
) {
2131 bh
->b_size
+= PAGE_SIZE
;
2133 atomic_add(PAGE_SIZE
, &bh
->b_count
);
2137 if ((bh
= kmalloc(sizeof(struct idetape_bh
), GFP_KERNEL
)) == NULL
) {
2138 free_page((unsigned long) b_data
);
2141 bh
->b_reqnext
= NULL
;
2142 bh
->b_data
= b_data
;
2143 bh
->b_size
= PAGE_SIZE
;
2144 atomic_set(&bh
->b_count
, full
? bh
->b_size
: 0);
2145 prev_bh
->b_reqnext
= bh
;
2147 bh
->b_size
-= tape
->excess_bh_size
;
2149 atomic_sub(tape
->excess_bh_size
, &bh
->b_count
);
2152 __idetape_kfree_stage(stage
);
2156 static idetape_stage_t
*idetape_kmalloc_stage (idetape_tape_t
*tape
)
2158 idetape_stage_t
*cache_stage
= tape
->cache_stage
;
2160 #if IDETAPE_DEBUG_LOG
2161 if (tape
->debug_level
>= 4)
2162 printk(KERN_INFO
"ide-tape: Reached idetape_kmalloc_stage\n");
2163 #endif /* IDETAPE_DEBUG_LOG */
2165 if (tape
->nr_stages
>= tape
->max_stages
)
2167 if (cache_stage
!= NULL
) {
2168 tape
->cache_stage
= NULL
;
2171 return __idetape_kmalloc_stage(tape
, 0, 0);
2174 static int idetape_copy_stage_from_user (idetape_tape_t
*tape
, idetape_stage_t
*stage
, const char __user
*buf
, int n
)
2176 struct idetape_bh
*bh
= tape
->bh
;
2181 #if IDETAPE_DEBUG_BUGS
2183 printk(KERN_ERR
"ide-tape: bh == NULL in "
2184 "idetape_copy_stage_from_user\n");
2187 #endif /* IDETAPE_DEBUG_BUGS */
2188 count
= min((unsigned int)(bh
->b_size
- atomic_read(&bh
->b_count
)), (unsigned int)n
);
2189 if (copy_from_user(bh
->b_data
+ atomic_read(&bh
->b_count
), buf
, count
))
2192 atomic_add(count
, &bh
->b_count
);
2194 if (atomic_read(&bh
->b_count
) == bh
->b_size
) {
2197 atomic_set(&bh
->b_count
, 0);
2204 static int idetape_copy_stage_to_user (idetape_tape_t
*tape
, char __user
*buf
, idetape_stage_t
*stage
, int n
)
2206 struct idetape_bh
*bh
= tape
->bh
;
2211 #if IDETAPE_DEBUG_BUGS
2213 printk(KERN_ERR
"ide-tape: bh == NULL in "
2214 "idetape_copy_stage_to_user\n");
2217 #endif /* IDETAPE_DEBUG_BUGS */
2218 count
= min(tape
->b_count
, n
);
2219 if (copy_to_user(buf
, tape
->b_data
, count
))
2222 tape
->b_data
+= count
;
2223 tape
->b_count
-= count
;
2225 if (!tape
->b_count
) {
2226 tape
->bh
= bh
= bh
->b_reqnext
;
2228 tape
->b_data
= bh
->b_data
;
2229 tape
->b_count
= atomic_read(&bh
->b_count
);
2236 static void idetape_init_merge_stage (idetape_tape_t
*tape
)
2238 struct idetape_bh
*bh
= tape
->merge_stage
->bh
;
2241 if (tape
->chrdev_direction
== idetape_direction_write
)
2242 atomic_set(&bh
->b_count
, 0);
2244 tape
->b_data
= bh
->b_data
;
2245 tape
->b_count
= atomic_read(&bh
->b_count
);
2249 static void idetape_switch_buffers (idetape_tape_t
*tape
, idetape_stage_t
*stage
)
2251 struct idetape_bh
*tmp
;
2254 stage
->bh
= tape
->merge_stage
->bh
;
2255 tape
->merge_stage
->bh
= tmp
;
2256 idetape_init_merge_stage(tape
);
2260 * idetape_add_stage_tail adds a new stage at the end of the pipeline.
2262 static void idetape_add_stage_tail (ide_drive_t
*drive
,idetape_stage_t
*stage
)
2264 idetape_tape_t
*tape
= drive
->driver_data
;
2265 unsigned long flags
;
2267 #if IDETAPE_DEBUG_LOG
2268 if (tape
->debug_level
>= 4)
2269 printk (KERN_INFO
"ide-tape: Reached idetape_add_stage_tail\n");
2270 #endif /* IDETAPE_DEBUG_LOG */
2271 spin_lock_irqsave(&tape
->spinlock
, flags
);
2273 if (tape
->last_stage
!= NULL
)
2274 tape
->last_stage
->next
=stage
;
2276 tape
->first_stage
= tape
->next_stage
=stage
;
2277 tape
->last_stage
= stage
;
2278 if (tape
->next_stage
== NULL
)
2279 tape
->next_stage
= tape
->last_stage
;
2281 tape
->nr_pending_stages
++;
2282 spin_unlock_irqrestore(&tape
->spinlock
, flags
);
2286 * idetape_wait_for_request installs a completion in a pending request
2287 * and sleeps until it is serviced.
2289 * The caller should ensure that the request will not be serviced
2290 * before we install the completion (usually by disabling interrupts).
2292 static void idetape_wait_for_request (ide_drive_t
*drive
, struct request
*rq
)
2294 DECLARE_COMPLETION_ONSTACK(wait
);
2295 idetape_tape_t
*tape
= drive
->driver_data
;
2297 #if IDETAPE_DEBUG_BUGS
2298 if (rq
== NULL
|| !blk_special_request(rq
)) {
2299 printk (KERN_ERR
"ide-tape: bug: Trying to sleep on non-valid request\n");
2302 #endif /* IDETAPE_DEBUG_BUGS */
2303 rq
->end_io_data
= &wait
;
2304 rq
->end_io
= blk_end_sync_rq
;
2305 spin_unlock_irq(&tape
->spinlock
);
2306 wait_for_completion(&wait
);
2307 /* The stage and its struct request have been deallocated */
2308 spin_lock_irq(&tape
->spinlock
);
2311 static ide_startstop_t
idetape_read_position_callback (ide_drive_t
*drive
)
2313 idetape_tape_t
*tape
= drive
->driver_data
;
2314 idetape_read_position_result_t
*result
;
2316 #if IDETAPE_DEBUG_LOG
2317 if (tape
->debug_level
>= 4)
2318 printk(KERN_INFO
"ide-tape: Reached idetape_read_position_callback\n");
2319 #endif /* IDETAPE_DEBUG_LOG */
2321 if (!tape
->pc
->error
) {
2322 result
= (idetape_read_position_result_t
*) tape
->pc
->buffer
;
2323 #if IDETAPE_DEBUG_LOG
2324 if (tape
->debug_level
>= 2)
2325 printk(KERN_INFO
"ide-tape: BOP - %s\n",result
->bop
? "Yes":"No");
2326 if (tape
->debug_level
>= 2)
2327 printk(KERN_INFO
"ide-tape: EOP - %s\n",result
->eop
? "Yes":"No");
2328 #endif /* IDETAPE_DEBUG_LOG */
2330 printk(KERN_INFO
"ide-tape: Block location is unknown to the tape\n");
2331 clear_bit(IDETAPE_ADDRESS_VALID
, &tape
->flags
);
2332 idetape_end_request(drive
, 0, 0);
2334 #if IDETAPE_DEBUG_LOG
2335 if (tape
->debug_level
>= 2)
2336 printk(KERN_INFO
"ide-tape: Block Location - %u\n", ntohl(result
->first_block
));
2337 #endif /* IDETAPE_DEBUG_LOG */
2338 tape
->partition
= result
->partition
;
2339 tape
->first_frame_position
= ntohl(result
->first_block
);
2340 tape
->last_frame_position
= ntohl(result
->last_block
);
2341 tape
->blocks_in_buffer
= result
->blocks_in_buffer
[2];
2342 set_bit(IDETAPE_ADDRESS_VALID
, &tape
->flags
);
2343 idetape_end_request(drive
, 1, 0);
2346 idetape_end_request(drive
, 0, 0);
2352 * idetape_create_write_filemark_cmd will:
2354 * 1. Write a filemark if write_filemark=1.
2355 * 2. Flush the device buffers without writing a filemark
2356 * if write_filemark=0.
2359 static void idetape_create_write_filemark_cmd (ide_drive_t
*drive
, idetape_pc_t
*pc
,int write_filemark
)
2361 idetape_init_pc(pc
);
2362 pc
->c
[0] = IDETAPE_WRITE_FILEMARK_CMD
;
2363 pc
->c
[4] = write_filemark
;
2364 set_bit(PC_WAIT_FOR_DSC
, &pc
->flags
);
2365 pc
->callback
= &idetape_pc_callback
;
2368 static void idetape_create_test_unit_ready_cmd(idetape_pc_t
*pc
)
2370 idetape_init_pc(pc
);
2371 pc
->c
[0] = IDETAPE_TEST_UNIT_READY_CMD
;
2372 pc
->callback
= &idetape_pc_callback
;
2376 * idetape_queue_pc_tail is based on the following functions:
2378 * ide_do_drive_cmd from ide.c
2379 * cdrom_queue_request and cdrom_queue_packet_command from ide-cd.c
2381 * We add a special packet command request to the tail of the request
2382 * queue, and wait for it to be serviced.
2384 * This is not to be called from within the request handling part
2385 * of the driver ! We allocate here data in the stack, and it is valid
2386 * until the request is finished. This is not the case for the bottom
2387 * part of the driver, where we are always leaving the functions to wait
2388 * for an interrupt or a timer event.
2390 * From the bottom part of the driver, we should allocate safe memory
2391 * using idetape_next_pc_storage and idetape_next_rq_storage, and add
2392 * the request to the request list without waiting for it to be serviced !
2393 * In that case, we usually use idetape_queue_pc_head.
2395 static int __idetape_queue_pc_tail (ide_drive_t
*drive
, idetape_pc_t
*pc
)
2397 struct ide_tape_obj
*tape
= drive
->driver_data
;
2400 idetape_init_rq(&rq
, REQ_IDETAPE_PC1
);
2401 rq
.buffer
= (char *) pc
;
2402 rq
.rq_disk
= tape
->disk
;
2403 return ide_do_drive_cmd(drive
, &rq
, ide_wait
);
2406 static void idetape_create_load_unload_cmd (ide_drive_t
*drive
, idetape_pc_t
*pc
,int cmd
)
2408 idetape_init_pc(pc
);
2409 pc
->c
[0] = IDETAPE_LOAD_UNLOAD_CMD
;
2411 set_bit(PC_WAIT_FOR_DSC
, &pc
->flags
);
2412 pc
->callback
= &idetape_pc_callback
;
2415 static int idetape_wait_ready(ide_drive_t
*drive
, unsigned long timeout
)
2417 idetape_tape_t
*tape
= drive
->driver_data
;
2419 int load_attempted
= 0;
2422 * Wait for the tape to become ready
2424 set_bit(IDETAPE_MEDIUM_PRESENT
, &tape
->flags
);
2426 while (time_before(jiffies
, timeout
)) {
2427 idetape_create_test_unit_ready_cmd(&pc
);
2428 if (!__idetape_queue_pc_tail(drive
, &pc
))
2430 if ((tape
->sense_key
== 2 && tape
->asc
== 4 && tape
->ascq
== 2)
2431 || (tape
->asc
== 0x3A)) { /* no media */
2434 idetape_create_load_unload_cmd(drive
, &pc
, IDETAPE_LU_LOAD_MASK
);
2435 __idetape_queue_pc_tail(drive
, &pc
);
2437 /* not about to be ready */
2438 } else if (!(tape
->sense_key
== 2 && tape
->asc
== 4 &&
2439 (tape
->ascq
== 1 || tape
->ascq
== 8)))
2446 static int idetape_queue_pc_tail (ide_drive_t
*drive
,idetape_pc_t
*pc
)
2448 return __idetape_queue_pc_tail(drive
, pc
);
2451 static int idetape_flush_tape_buffers (ide_drive_t
*drive
)
2456 idetape_create_write_filemark_cmd(drive
, &pc
, 0);
2457 if ((rc
= idetape_queue_pc_tail(drive
, &pc
)))
2459 idetape_wait_ready(drive
, 60 * 5 * HZ
);
2463 static void idetape_create_read_position_cmd (idetape_pc_t
*pc
)
2465 idetape_init_pc(pc
);
2466 pc
->c
[0] = IDETAPE_READ_POSITION_CMD
;
2467 pc
->request_transfer
= 20;
2468 pc
->callback
= &idetape_read_position_callback
;
2471 static int idetape_read_position (ide_drive_t
*drive
)
2473 idetape_tape_t
*tape
= drive
->driver_data
;
2477 #if IDETAPE_DEBUG_LOG
2478 if (tape
->debug_level
>= 4)
2479 printk(KERN_INFO
"ide-tape: Reached idetape_read_position\n");
2480 #endif /* IDETAPE_DEBUG_LOG */
2482 idetape_create_read_position_cmd(&pc
);
2483 if (idetape_queue_pc_tail(drive
, &pc
))
2485 position
= tape
->first_frame_position
;
2489 static void idetape_create_locate_cmd (ide_drive_t
*drive
, idetape_pc_t
*pc
, unsigned int block
, u8 partition
, int skip
)
2491 idetape_init_pc(pc
);
2492 pc
->c
[0] = IDETAPE_LOCATE_CMD
;
2494 put_unaligned(htonl(block
), (unsigned int *) &pc
->c
[3]);
2495 pc
->c
[8] = partition
;
2496 set_bit(PC_WAIT_FOR_DSC
, &pc
->flags
);
2497 pc
->callback
= &idetape_pc_callback
;
2500 static int idetape_create_prevent_cmd (ide_drive_t
*drive
, idetape_pc_t
*pc
, int prevent
)
2502 idetape_tape_t
*tape
= drive
->driver_data
;
2504 if (!tape
->capabilities
.lock
)
2507 idetape_init_pc(pc
);
2508 pc
->c
[0] = IDETAPE_PREVENT_CMD
;
2510 pc
->callback
= &idetape_pc_callback
;
2514 static int __idetape_discard_read_pipeline (ide_drive_t
*drive
)
2516 idetape_tape_t
*tape
= drive
->driver_data
;
2517 unsigned long flags
;
2520 if (tape
->chrdev_direction
!= idetape_direction_read
)
2523 /* Remove merge stage. */
2524 cnt
= tape
->merge_stage_size
/ tape
->tape_block_size
;
2525 if (test_and_clear_bit(IDETAPE_FILEMARK
, &tape
->flags
))
2526 ++cnt
; /* Filemarks count as 1 sector */
2527 tape
->merge_stage_size
= 0;
2528 if (tape
->merge_stage
!= NULL
) {
2529 __idetape_kfree_stage(tape
->merge_stage
);
2530 tape
->merge_stage
= NULL
;
2533 /* Clear pipeline flags. */
2534 clear_bit(IDETAPE_PIPELINE_ERROR
, &tape
->flags
);
2535 tape
->chrdev_direction
= idetape_direction_none
;
2537 /* Remove pipeline stages. */
2538 if (tape
->first_stage
== NULL
)
2541 spin_lock_irqsave(&tape
->spinlock
, flags
);
2542 tape
->next_stage
= NULL
;
2543 if (idetape_pipeline_active(tape
))
2544 idetape_wait_for_request(drive
, tape
->active_data_request
);
2545 spin_unlock_irqrestore(&tape
->spinlock
, flags
);
2547 while (tape
->first_stage
!= NULL
) {
2548 struct request
*rq_ptr
= &tape
->first_stage
->rq
;
2550 cnt
+= rq_ptr
->nr_sectors
- rq_ptr
->current_nr_sectors
;
2551 if (rq_ptr
->errors
== IDETAPE_ERROR_FILEMARK
)
2553 idetape_remove_stage_head(drive
);
2555 tape
->nr_pending_stages
= 0;
2556 tape
->max_stages
= tape
->min_pipeline
;
2561 * idetape_position_tape positions the tape to the requested block
2562 * using the LOCATE packet command. A READ POSITION command is then
2563 * issued to check where we are positioned.
2565 * Like all higher level operations, we queue the commands at the tail
2566 * of the request queue and wait for their completion.
2569 static int idetape_position_tape (ide_drive_t
*drive
, unsigned int block
, u8 partition
, int skip
)
2571 idetape_tape_t
*tape
= drive
->driver_data
;
2575 if (tape
->chrdev_direction
== idetape_direction_read
)
2576 __idetape_discard_read_pipeline(drive
);
2577 idetape_wait_ready(drive
, 60 * 5 * HZ
);
2578 idetape_create_locate_cmd(drive
, &pc
, block
, partition
, skip
);
2579 retval
= idetape_queue_pc_tail(drive
, &pc
);
2583 idetape_create_read_position_cmd(&pc
);
2584 return (idetape_queue_pc_tail(drive
, &pc
));
2587 static void idetape_discard_read_pipeline (ide_drive_t
*drive
, int restore_position
)
2589 idetape_tape_t
*tape
= drive
->driver_data
;
2593 cnt
= __idetape_discard_read_pipeline(drive
);
2594 if (restore_position
) {
2595 position
= idetape_read_position(drive
);
2596 seek
= position
> cnt
? position
- cnt
: 0;
2597 if (idetape_position_tape(drive
, seek
, 0, 0)) {
2598 printk(KERN_INFO
"ide-tape: %s: position_tape failed in discard_pipeline()\n", tape
->name
);
2605 * idetape_queue_rw_tail generates a read/write request for the block
2606 * device interface and wait for it to be serviced.
2608 static int idetape_queue_rw_tail(ide_drive_t
*drive
, int cmd
, int blocks
, struct idetape_bh
*bh
)
2610 idetape_tape_t
*tape
= drive
->driver_data
;
2613 #if IDETAPE_DEBUG_LOG
2614 if (tape
->debug_level
>= 2)
2615 printk(KERN_INFO
"ide-tape: idetape_queue_rw_tail: cmd=%d\n",cmd
);
2616 #endif /* IDETAPE_DEBUG_LOG */
2617 #if IDETAPE_DEBUG_BUGS
2618 if (idetape_pipeline_active(tape
)) {
2619 printk(KERN_ERR
"ide-tape: bug: the pipeline is active in idetape_queue_rw_tail\n");
2622 #endif /* IDETAPE_DEBUG_BUGS */
2624 idetape_init_rq(&rq
, cmd
);
2625 rq
.rq_disk
= tape
->disk
;
2626 rq
.special
= (void *)bh
;
2627 rq
.sector
= tape
->first_frame_position
;
2628 rq
.nr_sectors
= rq
.current_nr_sectors
= blocks
;
2629 (void) ide_do_drive_cmd(drive
, &rq
, ide_wait
);
2631 if ((cmd
& (REQ_IDETAPE_READ
| REQ_IDETAPE_WRITE
)) == 0)
2634 if (tape
->merge_stage
)
2635 idetape_init_merge_stage(tape
);
2636 if (rq
.errors
== IDETAPE_ERROR_GENERAL
)
2638 return (tape
->tape_block_size
* (blocks
-rq
.current_nr_sectors
));
2642 * idetape_insert_pipeline_into_queue is used to start servicing the
2643 * pipeline stages, starting from tape->next_stage.
2645 static void idetape_insert_pipeline_into_queue (ide_drive_t
*drive
)
2647 idetape_tape_t
*tape
= drive
->driver_data
;
2649 if (tape
->next_stage
== NULL
)
2651 if (!idetape_pipeline_active(tape
)) {
2652 set_bit(IDETAPE_PIPELINE_ACTIVE
, &tape
->flags
);
2653 idetape_active_next_stage(drive
);
2654 (void) ide_do_drive_cmd(drive
, tape
->active_data_request
, ide_end
);
2658 static void idetape_create_inquiry_cmd (idetape_pc_t
*pc
)
2660 idetape_init_pc(pc
);
2661 pc
->c
[0] = IDETAPE_INQUIRY_CMD
;
2662 pc
->c
[4] = pc
->request_transfer
= 254;
2663 pc
->callback
= &idetape_pc_callback
;
2666 static void idetape_create_rewind_cmd (ide_drive_t
*drive
, idetape_pc_t
*pc
)
2668 idetape_init_pc(pc
);
2669 pc
->c
[0] = IDETAPE_REWIND_CMD
;
2670 set_bit(PC_WAIT_FOR_DSC
, &pc
->flags
);
2671 pc
->callback
= &idetape_pc_callback
;
2674 static void idetape_create_erase_cmd (idetape_pc_t
*pc
)
2676 idetape_init_pc(pc
);
2677 pc
->c
[0] = IDETAPE_ERASE_CMD
;
2679 set_bit(PC_WAIT_FOR_DSC
, &pc
->flags
);
2680 pc
->callback
= &idetape_pc_callback
;
2683 static void idetape_create_space_cmd (idetape_pc_t
*pc
,int count
, u8 cmd
)
2685 idetape_init_pc(pc
);
2686 pc
->c
[0] = IDETAPE_SPACE_CMD
;
2687 put_unaligned(htonl(count
), (unsigned int *) &pc
->c
[1]);
2689 set_bit(PC_WAIT_FOR_DSC
, &pc
->flags
);
2690 pc
->callback
= &idetape_pc_callback
;
2693 static void idetape_wait_first_stage (ide_drive_t
*drive
)
2695 idetape_tape_t
*tape
= drive
->driver_data
;
2696 unsigned long flags
;
2698 if (tape
->first_stage
== NULL
)
2700 spin_lock_irqsave(&tape
->spinlock
, flags
);
2701 if (tape
->active_stage
== tape
->first_stage
)
2702 idetape_wait_for_request(drive
, tape
->active_data_request
);
2703 spin_unlock_irqrestore(&tape
->spinlock
, flags
);
2707 * idetape_add_chrdev_write_request tries to add a character device
2708 * originated write request to our pipeline. In case we don't succeed,
2709 * we revert to non-pipelined operation mode for this request.
2711 * 1. Try to allocate a new pipeline stage.
2712 * 2. If we can't, wait for more and more requests to be serviced
2713 * and try again each time.
2714 * 3. If we still can't allocate a stage, fallback to
2715 * non-pipelined operation mode for this request.
2717 static int idetape_add_chrdev_write_request (ide_drive_t
*drive
, int blocks
)
2719 idetape_tape_t
*tape
= drive
->driver_data
;
2720 idetape_stage_t
*new_stage
;
2721 unsigned long flags
;
2724 #if IDETAPE_DEBUG_LOG
2725 if (tape
->debug_level
>= 3)
2726 printk(KERN_INFO
"ide-tape: Reached idetape_add_chrdev_write_request\n");
2727 #endif /* IDETAPE_DEBUG_LOG */
2730 * Attempt to allocate a new stage.
2731 * Pay special attention to possible race conditions.
2733 while ((new_stage
= idetape_kmalloc_stage(tape
)) == NULL
) {
2734 spin_lock_irqsave(&tape
->spinlock
, flags
);
2735 if (idetape_pipeline_active(tape
)) {
2736 idetape_wait_for_request(drive
, tape
->active_data_request
);
2737 spin_unlock_irqrestore(&tape
->spinlock
, flags
);
2739 spin_unlock_irqrestore(&tape
->spinlock
, flags
);
2740 idetape_insert_pipeline_into_queue(drive
);
2741 if (idetape_pipeline_active(tape
))
2744 * Linux is short on memory. Fallback to
2745 * non-pipelined operation mode for this request.
2747 return idetape_queue_rw_tail(drive
, REQ_IDETAPE_WRITE
, blocks
, tape
->merge_stage
->bh
);
2750 rq
= &new_stage
->rq
;
2751 idetape_init_rq(rq
, REQ_IDETAPE_WRITE
);
2752 /* Doesn't actually matter - We always assume sequential access */
2753 rq
->sector
= tape
->first_frame_position
;
2754 rq
->nr_sectors
= rq
->current_nr_sectors
= blocks
;
2756 idetape_switch_buffers(tape
, new_stage
);
2757 idetape_add_stage_tail(drive
, new_stage
);
2758 tape
->pipeline_head
++;
2759 calculate_speeds(drive
);
2762 * Estimate whether the tape has stopped writing by checking
2763 * if our write pipeline is currently empty. If we are not
2764 * writing anymore, wait for the pipeline to be full enough
2765 * (90%) before starting to service requests, so that we will
2766 * be able to keep up with the higher speeds of the tape.
2768 if (!idetape_pipeline_active(tape
)) {
2769 if (tape
->nr_stages
>= tape
->max_stages
* 9 / 10 ||
2770 tape
->nr_stages
>= tape
->max_stages
- tape
->uncontrolled_pipeline_head_speed
* 3 * 1024 / tape
->tape_block_size
) {
2771 tape
->measure_insert_time
= 1;
2772 tape
->insert_time
= jiffies
;
2773 tape
->insert_size
= 0;
2774 tape
->insert_speed
= 0;
2775 idetape_insert_pipeline_into_queue(drive
);
2778 if (test_and_clear_bit(IDETAPE_PIPELINE_ERROR
, &tape
->flags
))
2779 /* Return a deferred error */
2785 * idetape_wait_for_pipeline will wait until all pending pipeline
2786 * requests are serviced. Typically called on device close.
2788 static void idetape_wait_for_pipeline (ide_drive_t
*drive
)
2790 idetape_tape_t
*tape
= drive
->driver_data
;
2791 unsigned long flags
;
2793 while (tape
->next_stage
|| idetape_pipeline_active(tape
)) {
2794 idetape_insert_pipeline_into_queue(drive
);
2795 spin_lock_irqsave(&tape
->spinlock
, flags
);
2796 if (idetape_pipeline_active(tape
))
2797 idetape_wait_for_request(drive
, tape
->active_data_request
);
2798 spin_unlock_irqrestore(&tape
->spinlock
, flags
);
2802 static void idetape_empty_write_pipeline (ide_drive_t
*drive
)
2804 idetape_tape_t
*tape
= drive
->driver_data
;
2806 struct idetape_bh
*bh
;
2808 #if IDETAPE_DEBUG_BUGS
2809 if (tape
->chrdev_direction
!= idetape_direction_write
) {
2810 printk(KERN_ERR
"ide-tape: bug: Trying to empty write pipeline, but we are not writing.\n");
2813 if (tape
->merge_stage_size
> tape
->stage_size
) {
2814 printk(KERN_ERR
"ide-tape: bug: merge_buffer too big\n");
2815 tape
->merge_stage_size
= tape
->stage_size
;
2817 #endif /* IDETAPE_DEBUG_BUGS */
2818 if (tape
->merge_stage_size
) {
2819 blocks
= tape
->merge_stage_size
/ tape
->tape_block_size
;
2820 if (tape
->merge_stage_size
% tape
->tape_block_size
) {
2824 i
= tape
->tape_block_size
- tape
->merge_stage_size
% tape
->tape_block_size
;
2825 bh
= tape
->bh
->b_reqnext
;
2827 atomic_set(&bh
->b_count
, 0);
2834 printk(KERN_INFO
"ide-tape: bug, bh NULL\n");
2837 min
= min(i
, (unsigned int)(bh
->b_size
- atomic_read(&bh
->b_count
)));
2838 memset(bh
->b_data
+ atomic_read(&bh
->b_count
), 0, min
);
2839 atomic_add(min
, &bh
->b_count
);
2844 (void) idetape_add_chrdev_write_request(drive
, blocks
);
2845 tape
->merge_stage_size
= 0;
2847 idetape_wait_for_pipeline(drive
);
2848 if (tape
->merge_stage
!= NULL
) {
2849 __idetape_kfree_stage(tape
->merge_stage
);
2850 tape
->merge_stage
= NULL
;
2852 clear_bit(IDETAPE_PIPELINE_ERROR
, &tape
->flags
);
2853 tape
->chrdev_direction
= idetape_direction_none
;
2856 * On the next backup, perform the feedback loop again.
2857 * (I don't want to keep sense information between backups,
2858 * as some systems are constantly on, and the system load
2859 * can be totally different on the next backup).
2861 tape
->max_stages
= tape
->min_pipeline
;
2862 #if IDETAPE_DEBUG_BUGS
2863 if (tape
->first_stage
!= NULL
||
2864 tape
->next_stage
!= NULL
||
2865 tape
->last_stage
!= NULL
||
2866 tape
->nr_stages
!= 0) {
2867 printk(KERN_ERR
"ide-tape: ide-tape pipeline bug, "
2868 "first_stage %p, next_stage %p, "
2869 "last_stage %p, nr_stages %d\n",
2870 tape
->first_stage
, tape
->next_stage
,
2871 tape
->last_stage
, tape
->nr_stages
);
2873 #endif /* IDETAPE_DEBUG_BUGS */
2876 static void idetape_restart_speed_control (ide_drive_t
*drive
)
2878 idetape_tape_t
*tape
= drive
->driver_data
;
2880 tape
->restart_speed_control_req
= 0;
2881 tape
->pipeline_head
= 0;
2882 tape
->controlled_last_pipeline_head
= tape
->uncontrolled_last_pipeline_head
= 0;
2883 tape
->controlled_previous_pipeline_head
= tape
->uncontrolled_previous_pipeline_head
= 0;
2884 tape
->pipeline_head_speed
= tape
->controlled_pipeline_head_speed
= 5000;
2885 tape
->uncontrolled_pipeline_head_speed
= 0;
2886 tape
->controlled_pipeline_head_time
= tape
->uncontrolled_pipeline_head_time
= jiffies
;
2887 tape
->controlled_previous_head_time
= tape
->uncontrolled_previous_head_time
= jiffies
;
2890 static int idetape_initiate_read (ide_drive_t
*drive
, int max_stages
)
2892 idetape_tape_t
*tape
= drive
->driver_data
;
2893 idetape_stage_t
*new_stage
;
2896 int blocks
= tape
->capabilities
.ctl
;
2898 /* Initialize read operation */
2899 if (tape
->chrdev_direction
!= idetape_direction_read
) {
2900 if (tape
->chrdev_direction
== idetape_direction_write
) {
2901 idetape_empty_write_pipeline(drive
);
2902 idetape_flush_tape_buffers(drive
);
2904 #if IDETAPE_DEBUG_BUGS
2905 if (tape
->merge_stage
|| tape
->merge_stage_size
) {
2906 printk (KERN_ERR
"ide-tape: merge_stage_size should be 0 now\n");
2907 tape
->merge_stage_size
= 0;
2909 #endif /* IDETAPE_DEBUG_BUGS */
2910 if ((tape
->merge_stage
= __idetape_kmalloc_stage(tape
, 0, 0)) == NULL
)
2912 tape
->chrdev_direction
= idetape_direction_read
;
2915 * Issue a read 0 command to ensure that DSC handshake
2916 * is switched from completion mode to buffer available
2918 * No point in issuing this if DSC overlap isn't supported,
2919 * some drives (Seagate STT3401A) will return an error.
2921 if (drive
->dsc_overlap
) {
2922 bytes_read
= idetape_queue_rw_tail(drive
, REQ_IDETAPE_READ
, 0, tape
->merge_stage
->bh
);
2923 if (bytes_read
< 0) {
2924 __idetape_kfree_stage(tape
->merge_stage
);
2925 tape
->merge_stage
= NULL
;
2926 tape
->chrdev_direction
= idetape_direction_none
;
2931 if (tape
->restart_speed_control_req
)
2932 idetape_restart_speed_control(drive
);
2933 idetape_init_rq(&rq
, REQ_IDETAPE_READ
);
2934 rq
.sector
= tape
->first_frame_position
;
2935 rq
.nr_sectors
= rq
.current_nr_sectors
= blocks
;
2936 if (!test_bit(IDETAPE_PIPELINE_ERROR
, &tape
->flags
) &&
2937 tape
->nr_stages
< max_stages
) {
2938 new_stage
= idetape_kmalloc_stage(tape
);
2939 while (new_stage
!= NULL
) {
2941 idetape_add_stage_tail(drive
, new_stage
);
2942 if (tape
->nr_stages
>= max_stages
)
2944 new_stage
= idetape_kmalloc_stage(tape
);
2947 if (!idetape_pipeline_active(tape
)) {
2948 if (tape
->nr_pending_stages
>= 3 * max_stages
/ 4) {
2949 tape
->measure_insert_time
= 1;
2950 tape
->insert_time
= jiffies
;
2951 tape
->insert_size
= 0;
2952 tape
->insert_speed
= 0;
2953 idetape_insert_pipeline_into_queue(drive
);
2960 * idetape_add_chrdev_read_request is called from idetape_chrdev_read
2961 * to service a character device read request and add read-ahead
2962 * requests to our pipeline.
2964 static int idetape_add_chrdev_read_request (ide_drive_t
*drive
,int blocks
)
2966 idetape_tape_t
*tape
= drive
->driver_data
;
2967 unsigned long flags
;
2968 struct request
*rq_ptr
;
2971 #if IDETAPE_DEBUG_LOG
2972 if (tape
->debug_level
>= 4)
2973 printk(KERN_INFO
"ide-tape: Reached idetape_add_chrdev_read_request, %d blocks\n", blocks
);
2974 #endif /* IDETAPE_DEBUG_LOG */
2977 * If we are at a filemark, return a read length of 0
2979 if (test_bit(IDETAPE_FILEMARK
, &tape
->flags
))
2983 * Wait for the next block to be available at the head
2986 idetape_initiate_read(drive
, tape
->max_stages
);
2987 if (tape
->first_stage
== NULL
) {
2988 if (test_bit(IDETAPE_PIPELINE_ERROR
, &tape
->flags
))
2990 return idetape_queue_rw_tail(drive
, REQ_IDETAPE_READ
, blocks
, tape
->merge_stage
->bh
);
2992 idetape_wait_first_stage(drive
);
2993 rq_ptr
= &tape
->first_stage
->rq
;
2994 bytes_read
= tape
->tape_block_size
* (rq_ptr
->nr_sectors
- rq_ptr
->current_nr_sectors
);
2995 rq_ptr
->nr_sectors
= rq_ptr
->current_nr_sectors
= 0;
2998 if (rq_ptr
->errors
== IDETAPE_ERROR_EOD
)
3001 idetape_switch_buffers(tape
, tape
->first_stage
);
3002 if (rq_ptr
->errors
== IDETAPE_ERROR_FILEMARK
)
3003 set_bit(IDETAPE_FILEMARK
, &tape
->flags
);
3004 spin_lock_irqsave(&tape
->spinlock
, flags
);
3005 idetape_remove_stage_head(drive
);
3006 spin_unlock_irqrestore(&tape
->spinlock
, flags
);
3007 tape
->pipeline_head
++;
3008 calculate_speeds(drive
);
3010 #if IDETAPE_DEBUG_BUGS
3011 if (bytes_read
> blocks
* tape
->tape_block_size
) {
3012 printk(KERN_ERR
"ide-tape: bug: trying to return more bytes than requested\n");
3013 bytes_read
= blocks
* tape
->tape_block_size
;
3015 #endif /* IDETAPE_DEBUG_BUGS */
3016 return (bytes_read
);
3019 static void idetape_pad_zeros (ide_drive_t
*drive
, int bcount
)
3021 idetape_tape_t
*tape
= drive
->driver_data
;
3022 struct idetape_bh
*bh
;
3028 bh
= tape
->merge_stage
->bh
;
3029 count
= min(tape
->stage_size
, bcount
);
3031 blocks
= count
/ tape
->tape_block_size
;
3033 atomic_set(&bh
->b_count
, min(count
, (unsigned int)bh
->b_size
));
3034 memset(bh
->b_data
, 0, atomic_read(&bh
->b_count
));
3035 count
-= atomic_read(&bh
->b_count
);
3038 idetape_queue_rw_tail(drive
, REQ_IDETAPE_WRITE
, blocks
, tape
->merge_stage
->bh
);
3042 static int idetape_pipeline_size (ide_drive_t
*drive
)
3044 idetape_tape_t
*tape
= drive
->driver_data
;
3045 idetape_stage_t
*stage
;
3049 idetape_wait_for_pipeline(drive
);
3050 stage
= tape
->first_stage
;
3051 while (stage
!= NULL
) {
3053 size
+= tape
->tape_block_size
* (rq
->nr_sectors
-rq
->current_nr_sectors
);
3054 if (rq
->errors
== IDETAPE_ERROR_FILEMARK
)
3055 size
+= tape
->tape_block_size
;
3056 stage
= stage
->next
;
3058 size
+= tape
->merge_stage_size
;
3063 * Rewinds the tape to the Beginning Of the current Partition (BOP).
3065 * We currently support only one partition.
3067 static int idetape_rewind_tape (ide_drive_t
*drive
)
3071 #if IDETAPE_DEBUG_LOG
3072 idetape_tape_t
*tape
= drive
->driver_data
;
3073 if (tape
->debug_level
>= 2)
3074 printk(KERN_INFO
"ide-tape: Reached idetape_rewind_tape\n");
3075 #endif /* IDETAPE_DEBUG_LOG */
3077 idetape_create_rewind_cmd(drive
, &pc
);
3078 retval
= idetape_queue_pc_tail(drive
, &pc
);
3082 idetape_create_read_position_cmd(&pc
);
3083 retval
= idetape_queue_pc_tail(drive
, &pc
);
3090 * Our special ide-tape ioctl's.
3092 * Currently there aren't any ioctl's.
3093 * mtio.h compatible commands should be issued to the character device
3096 static int idetape_blkdev_ioctl(ide_drive_t
*drive
, unsigned int cmd
, unsigned long arg
)
3098 idetape_tape_t
*tape
= drive
->driver_data
;
3099 idetape_config_t config
;
3100 void __user
*argp
= (void __user
*)arg
;
3102 #if IDETAPE_DEBUG_LOG
3103 if (tape
->debug_level
>= 4)
3104 printk(KERN_INFO
"ide-tape: Reached idetape_blkdev_ioctl\n");
3105 #endif /* IDETAPE_DEBUG_LOG */
3108 if (copy_from_user(&config
, argp
, sizeof (idetape_config_t
)))
3110 tape
->best_dsc_rw_frequency
= config
.dsc_rw_frequency
;
3111 tape
->max_stages
= config
.nr_stages
;
3114 config
.dsc_rw_frequency
= (int) tape
->best_dsc_rw_frequency
;
3115 config
.nr_stages
= tape
->max_stages
;
3116 if (copy_to_user(argp
, &config
, sizeof (idetape_config_t
)))
3126 * idetape_space_over_filemarks is now a bit more complicated than just
3127 * passing the command to the tape since we may have crossed some
3128 * filemarks during our pipelined read-ahead mode.
3130 * As a minor side effect, the pipeline enables us to support MTFSFM when
3131 * the filemark is in our internal pipeline even if the tape doesn't
3132 * support spacing over filemarks in the reverse direction.
3134 static int idetape_space_over_filemarks (ide_drive_t
*drive
,short mt_op
,int mt_count
)
3136 idetape_tape_t
*tape
= drive
->driver_data
;
3138 unsigned long flags
;
3143 if (MTBSF
== mt_op
|| MTBSFM
== mt_op
) {
3144 if (!tape
->capabilities
.sprev
)
3146 mt_count
= - mt_count
;
3149 if (tape
->chrdev_direction
== idetape_direction_read
) {
3151 * We have a read-ahead buffer. Scan it for crossed
3154 tape
->merge_stage_size
= 0;
3155 if (test_and_clear_bit(IDETAPE_FILEMARK
, &tape
->flags
))
3157 while (tape
->first_stage
!= NULL
) {
3158 if (count
== mt_count
) {
3159 if (mt_op
== MTFSFM
)
3160 set_bit(IDETAPE_FILEMARK
, &tape
->flags
);
3163 spin_lock_irqsave(&tape
->spinlock
, flags
);
3164 if (tape
->first_stage
== tape
->active_stage
) {
3166 * We have reached the active stage in the read pipeline.
3167 * There is no point in allowing the drive to continue
3168 * reading any farther, so we stop the pipeline.
3170 * This section should be moved to a separate subroutine,
3171 * because a similar function is performed in
3172 * __idetape_discard_read_pipeline(), for example.
3174 tape
->next_stage
= NULL
;
3175 spin_unlock_irqrestore(&tape
->spinlock
, flags
);
3176 idetape_wait_first_stage(drive
);
3177 tape
->next_stage
= tape
->first_stage
->next
;
3179 spin_unlock_irqrestore(&tape
->spinlock
, flags
);
3180 if (tape
->first_stage
->rq
.errors
== IDETAPE_ERROR_FILEMARK
)
3182 idetape_remove_stage_head(drive
);
3184 idetape_discard_read_pipeline(drive
, 0);
3188 * The filemark was not found in our internal pipeline.
3189 * Now we can issue the space command.
3194 idetape_create_space_cmd(&pc
,mt_count
-count
,IDETAPE_SPACE_OVER_FILEMARK
);
3195 return (idetape_queue_pc_tail(drive
, &pc
));
3198 if (!tape
->capabilities
.sprev
)
3200 retval
= idetape_space_over_filemarks(drive
, MTFSF
, mt_count
-count
);
3201 if (retval
) return (retval
);
3202 count
= (MTBSFM
== mt_op
? 1 : -1);
3203 return (idetape_space_over_filemarks(drive
, MTFSF
, count
));
3205 printk(KERN_ERR
"ide-tape: MTIO operation %d not supported\n",mt_op
);
3212 * Our character device read / write functions.
3214 * The tape is optimized to maximize throughput when it is transferring
3215 * an integral number of the "continuous transfer limit", which is
3216 * a parameter of the specific tape (26 KB on my particular tape).
3217 * (32 kB for Onstream)
3219 * As of version 1.3 of the driver, the character device provides an
3220 * abstract continuous view of the media - any mix of block sizes (even 1
3221 * byte) on the same backup/restore procedure is supported. The driver
3222 * will internally convert the requests to the recommended transfer unit,
3223 * so that an unmatch between the user's block size to the recommended
3224 * size will only result in a (slightly) increased driver overhead, but
3225 * will no longer hit performance.
3226 * This is not applicable to Onstream.
3228 static ssize_t
idetape_chrdev_read (struct file
*file
, char __user
*buf
,
3229 size_t count
, loff_t
*ppos
)
3231 struct ide_tape_obj
*tape
= ide_tape_f(file
);
3232 ide_drive_t
*drive
= tape
->drive
;
3233 ssize_t bytes_read
,temp
, actually_read
= 0, rc
;
3236 #if IDETAPE_DEBUG_LOG
3237 if (tape
->debug_level
>= 3)
3238 printk(KERN_INFO
"ide-tape: Reached idetape_chrdev_read, count %Zd\n", count
);
3239 #endif /* IDETAPE_DEBUG_LOG */
3241 if (tape
->chrdev_direction
!= idetape_direction_read
) {
3242 if (test_bit(IDETAPE_DETECT_BS
, &tape
->flags
))
3243 if (count
> tape
->tape_block_size
&&
3244 (count
% tape
->tape_block_size
) == 0)
3245 tape
->user_bs_factor
= count
/ tape
->tape_block_size
;
3247 if ((rc
= idetape_initiate_read(drive
, tape
->max_stages
)) < 0)
3251 if (tape
->merge_stage_size
) {
3252 actually_read
= min((unsigned int)(tape
->merge_stage_size
), (unsigned int)count
);
3253 if (idetape_copy_stage_to_user(tape
, buf
, tape
->merge_stage
, actually_read
))
3255 buf
+= actually_read
;
3256 tape
->merge_stage_size
-= actually_read
;
3257 count
-= actually_read
;
3259 while (count
>= tape
->stage_size
) {
3260 bytes_read
= idetape_add_chrdev_read_request(drive
, tape
->capabilities
.ctl
);
3261 if (bytes_read
<= 0)
3263 if (idetape_copy_stage_to_user(tape
, buf
, tape
->merge_stage
, bytes_read
))
3266 count
-= bytes_read
;
3267 actually_read
+= bytes_read
;
3270 bytes_read
= idetape_add_chrdev_read_request(drive
, tape
->capabilities
.ctl
);
3271 if (bytes_read
<= 0)
3273 temp
= min((unsigned long)count
, (unsigned long)bytes_read
);
3274 if (idetape_copy_stage_to_user(tape
, buf
, tape
->merge_stage
, temp
))
3276 actually_read
+= temp
;
3277 tape
->merge_stage_size
= bytes_read
-temp
;
3280 if (!actually_read
&& test_bit(IDETAPE_FILEMARK
, &tape
->flags
)) {
3281 #if IDETAPE_DEBUG_LOG
3282 if (tape
->debug_level
>= 2)
3283 printk(KERN_INFO
"ide-tape: %s: spacing over filemark\n", tape
->name
);
3285 idetape_space_over_filemarks(drive
, MTFSF
, 1);
3289 return (ret
) ? ret
: actually_read
;
3292 static ssize_t
idetape_chrdev_write (struct file
*file
, const char __user
*buf
,
3293 size_t count
, loff_t
*ppos
)
3295 struct ide_tape_obj
*tape
= ide_tape_f(file
);
3296 ide_drive_t
*drive
= tape
->drive
;
3297 ssize_t actually_written
= 0;
3300 /* The drive is write protected. */
3301 if (tape
->write_prot
)
3304 #if IDETAPE_DEBUG_LOG
3305 if (tape
->debug_level
>= 3)
3306 printk(KERN_INFO
"ide-tape: Reached idetape_chrdev_write, "
3307 "count %Zd\n", count
);
3308 #endif /* IDETAPE_DEBUG_LOG */
3310 /* Initialize write operation */
3311 if (tape
->chrdev_direction
!= idetape_direction_write
) {
3312 if (tape
->chrdev_direction
== idetape_direction_read
)
3313 idetape_discard_read_pipeline(drive
, 1);
3314 #if IDETAPE_DEBUG_BUGS
3315 if (tape
->merge_stage
|| tape
->merge_stage_size
) {
3316 printk(KERN_ERR
"ide-tape: merge_stage_size "
3317 "should be 0 now\n");
3318 tape
->merge_stage_size
= 0;
3320 #endif /* IDETAPE_DEBUG_BUGS */
3321 if ((tape
->merge_stage
= __idetape_kmalloc_stage(tape
, 0, 0)) == NULL
)
3323 tape
->chrdev_direction
= idetape_direction_write
;
3324 idetape_init_merge_stage(tape
);
3327 * Issue a write 0 command to ensure that DSC handshake
3328 * is switched from completion mode to buffer available
3330 * No point in issuing this if DSC overlap isn't supported,
3331 * some drives (Seagate STT3401A) will return an error.
3333 if (drive
->dsc_overlap
) {
3334 ssize_t retval
= idetape_queue_rw_tail(drive
, REQ_IDETAPE_WRITE
, 0, tape
->merge_stage
->bh
);
3336 __idetape_kfree_stage(tape
->merge_stage
);
3337 tape
->merge_stage
= NULL
;
3338 tape
->chrdev_direction
= idetape_direction_none
;
3345 if (tape
->restart_speed_control_req
)
3346 idetape_restart_speed_control(drive
);
3347 if (tape
->merge_stage_size
) {
3348 #if IDETAPE_DEBUG_BUGS
3349 if (tape
->merge_stage_size
>= tape
->stage_size
) {
3350 printk(KERN_ERR
"ide-tape: bug: merge buffer too big\n");
3351 tape
->merge_stage_size
= 0;
3353 #endif /* IDETAPE_DEBUG_BUGS */
3354 actually_written
= min((unsigned int)(tape
->stage_size
- tape
->merge_stage_size
), (unsigned int)count
);
3355 if (idetape_copy_stage_from_user(tape
, tape
->merge_stage
, buf
, actually_written
))
3357 buf
+= actually_written
;
3358 tape
->merge_stage_size
+= actually_written
;
3359 count
-= actually_written
;
3361 if (tape
->merge_stage_size
== tape
->stage_size
) {
3363 tape
->merge_stage_size
= 0;
3364 retval
= idetape_add_chrdev_write_request(drive
, tape
->capabilities
.ctl
);
3369 while (count
>= tape
->stage_size
) {
3371 if (idetape_copy_stage_from_user(tape
, tape
->merge_stage
, buf
, tape
->stage_size
))
3373 buf
+= tape
->stage_size
;
3374 count
-= tape
->stage_size
;
3375 retval
= idetape_add_chrdev_write_request(drive
, tape
->capabilities
.ctl
);
3376 actually_written
+= tape
->stage_size
;
3381 actually_written
+= count
;
3382 if (idetape_copy_stage_from_user(tape
, tape
->merge_stage
, buf
, count
))
3384 tape
->merge_stage_size
+= count
;
3386 return (ret
) ? ret
: actually_written
;
3389 static int idetape_write_filemark (ide_drive_t
*drive
)
3393 /* Write a filemark */
3394 idetape_create_write_filemark_cmd(drive
, &pc
, 1);
3395 if (idetape_queue_pc_tail(drive
, &pc
)) {
3396 printk(KERN_ERR
"ide-tape: Couldn't write a filemark\n");
3403 * idetape_mtioctop is called from idetape_chrdev_ioctl when
3404 * the general mtio MTIOCTOP ioctl is requested.
3406 * We currently support the following mtio.h operations:
3408 * MTFSF - Space over mt_count filemarks in the positive direction.
3409 * The tape is positioned after the last spaced filemark.
3411 * MTFSFM - Same as MTFSF, but the tape is positioned before the
3414 * MTBSF - Steps background over mt_count filemarks, tape is
3415 * positioned before the last filemark.
3417 * MTBSFM - Like MTBSF, only tape is positioned after the last filemark.
3421 * MTBSF and MTBSFM are not supported when the tape doesn't
3422 * support spacing over filemarks in the reverse direction.
3423 * In this case, MTFSFM is also usually not supported (it is
3424 * supported in the rare case in which we crossed the filemark
3425 * during our read-ahead pipelined operation mode).
3427 * MTWEOF - Writes mt_count filemarks. Tape is positioned after
3428 * the last written filemark.
3430 * MTREW - Rewinds tape.
3432 * MTLOAD - Loads the tape.
3434 * MTOFFL - Puts the tape drive "Offline": Rewinds the tape and
3435 * MTUNLOAD prevents further access until the media is replaced.
3437 * MTNOP - Flushes tape buffers.
3439 * MTRETEN - Retension media. This typically consists of one end
3440 * to end pass on the media.
3442 * MTEOM - Moves to the end of recorded data.
3444 * MTERASE - Erases tape.
3446 * MTSETBLK - Sets the user block size to mt_count bytes. If
3447 * mt_count is 0, we will attempt to autodetect
3450 * MTSEEK - Positions the tape in a specific block number, where
3451 * each block is assumed to contain which user_block_size
3454 * MTSETPART - Switches to another tape partition.
3456 * MTLOCK - Locks the tape door.
3458 * MTUNLOCK - Unlocks the tape door.
3460 * The following commands are currently not supported:
3462 * MTFSS, MTBSS, MTWSM, MTSETDENSITY,
3463 * MTSETDRVBUFFER, MT_ST_BOOLEANS, MT_ST_WRITE_THRESHOLD.
3465 static int idetape_mtioctop (ide_drive_t
*drive
,short mt_op
,int mt_count
)
3467 idetape_tape_t
*tape
= drive
->driver_data
;
3471 #if IDETAPE_DEBUG_LOG
3472 if (tape
->debug_level
>= 1)
3473 printk(KERN_INFO
"ide-tape: Handling MTIOCTOP ioctl: "
3474 "mt_op=%d, mt_count=%d\n", mt_op
, mt_count
);
3475 #endif /* IDETAPE_DEBUG_LOG */
3477 * Commands which need our pipelined read-ahead stages.
3486 return (idetape_space_over_filemarks(drive
,mt_op
,mt_count
));
3492 if (tape
->write_prot
)
3494 idetape_discard_read_pipeline(drive
, 1);
3495 for (i
= 0; i
< mt_count
; i
++) {
3496 retval
= idetape_write_filemark(drive
);
3502 idetape_discard_read_pipeline(drive
, 0);
3503 if (idetape_rewind_tape(drive
))
3507 idetape_discard_read_pipeline(drive
, 0);
3508 idetape_create_load_unload_cmd(drive
, &pc
, IDETAPE_LU_LOAD_MASK
);
3509 return (idetape_queue_pc_tail(drive
, &pc
));
3513 * If door is locked, attempt to unlock before
3514 * attempting to eject.
3516 if (tape
->door_locked
) {
3517 if (idetape_create_prevent_cmd(drive
, &pc
, 0))
3518 if (!idetape_queue_pc_tail(drive
, &pc
))
3519 tape
->door_locked
= DOOR_UNLOCKED
;
3521 idetape_discard_read_pipeline(drive
, 0);
3522 idetape_create_load_unload_cmd(drive
, &pc
,!IDETAPE_LU_LOAD_MASK
);
3523 retval
= idetape_queue_pc_tail(drive
, &pc
);
3525 clear_bit(IDETAPE_MEDIUM_PRESENT
, &tape
->flags
);
3528 idetape_discard_read_pipeline(drive
, 0);
3529 return (idetape_flush_tape_buffers(drive
));
3531 idetape_discard_read_pipeline(drive
, 0);
3532 idetape_create_load_unload_cmd(drive
, &pc
,IDETAPE_LU_RETENSION_MASK
| IDETAPE_LU_LOAD_MASK
);
3533 return (idetape_queue_pc_tail(drive
, &pc
));
3535 idetape_create_space_cmd(&pc
, 0, IDETAPE_SPACE_TO_EOD
);
3536 return (idetape_queue_pc_tail(drive
, &pc
));
3538 (void) idetape_rewind_tape(drive
);
3539 idetape_create_erase_cmd(&pc
);
3540 return (idetape_queue_pc_tail(drive
, &pc
));
3543 if (mt_count
< tape
->tape_block_size
|| mt_count
% tape
->tape_block_size
)
3545 tape
->user_bs_factor
= mt_count
/ tape
->tape_block_size
;
3546 clear_bit(IDETAPE_DETECT_BS
, &tape
->flags
);
3548 set_bit(IDETAPE_DETECT_BS
, &tape
->flags
);
3551 idetape_discard_read_pipeline(drive
, 0);
3552 return idetape_position_tape(drive
, mt_count
* tape
->user_bs_factor
, tape
->partition
, 0);
3554 idetape_discard_read_pipeline(drive
, 0);
3555 return (idetape_position_tape(drive
, 0, mt_count
, 0));
3559 if (!idetape_create_prevent_cmd(drive
, &pc
, 1))
3561 retval
= idetape_queue_pc_tail(drive
, &pc
);
3562 if (retval
) return retval
;
3563 tape
->door_locked
= DOOR_EXPLICITLY_LOCKED
;
3566 if (!idetape_create_prevent_cmd(drive
, &pc
, 0))
3568 retval
= idetape_queue_pc_tail(drive
, &pc
);
3569 if (retval
) return retval
;
3570 tape
->door_locked
= DOOR_UNLOCKED
;
3573 printk(KERN_ERR
"ide-tape: MTIO operation %d not "
3574 "supported\n", mt_op
);
3580 * Our character device ioctls.
3582 * General mtio.h magnetic io commands are supported here, and not in
3583 * the corresponding block interface.
3585 * The following ioctls are supported:
3587 * MTIOCTOP - Refer to idetape_mtioctop for detailed description.
3589 * MTIOCGET - The mt_dsreg field in the returned mtget structure
3590 * will be set to (user block size in bytes <<
3591 * MT_ST_BLKSIZE_SHIFT) & MT_ST_BLKSIZE_MASK.
3593 * The mt_blkno is set to the current user block number.
3594 * The other mtget fields are not supported.
3596 * MTIOCPOS - The current tape "block position" is returned. We
3597 * assume that each block contains user_block_size
3600 * Our own ide-tape ioctls are supported on both interfaces.
3602 static int idetape_chrdev_ioctl (struct inode
*inode
, struct file
*file
, unsigned int cmd
, unsigned long arg
)
3604 struct ide_tape_obj
*tape
= ide_tape_f(file
);
3605 ide_drive_t
*drive
= tape
->drive
;
3609 int block_offset
= 0, position
= tape
->first_frame_position
;
3610 void __user
*argp
= (void __user
*)arg
;
3612 #if IDETAPE_DEBUG_LOG
3613 if (tape
->debug_level
>= 3)
3614 printk(KERN_INFO
"ide-tape: Reached idetape_chrdev_ioctl, "
3616 #endif /* IDETAPE_DEBUG_LOG */
3618 tape
->restart_speed_control_req
= 1;
3619 if (tape
->chrdev_direction
== idetape_direction_write
) {
3620 idetape_empty_write_pipeline(drive
);
3621 idetape_flush_tape_buffers(drive
);
3623 if (cmd
== MTIOCGET
|| cmd
== MTIOCPOS
) {
3624 block_offset
= idetape_pipeline_size(drive
) / (tape
->tape_block_size
* tape
->user_bs_factor
);
3625 if ((position
= idetape_read_position(drive
)) < 0)
3630 if (copy_from_user(&mtop
, argp
, sizeof (struct mtop
)))
3632 return (idetape_mtioctop(drive
,mtop
.mt_op
,mtop
.mt_count
));
3634 memset(&mtget
, 0, sizeof (struct mtget
));
3635 mtget
.mt_type
= MT_ISSCSI2
;
3636 mtget
.mt_blkno
= position
/ tape
->user_bs_factor
- block_offset
;
3637 mtget
.mt_dsreg
= ((tape
->tape_block_size
* tape
->user_bs_factor
) << MT_ST_BLKSIZE_SHIFT
) & MT_ST_BLKSIZE_MASK
;
3638 if (tape
->drv_write_prot
) {
3639 mtget
.mt_gstat
|= GMT_WR_PROT(0xffffffff);
3641 if (copy_to_user(argp
, &mtget
, sizeof(struct mtget
)))
3645 mtpos
.mt_blkno
= position
/ tape
->user_bs_factor
- block_offset
;
3646 if (copy_to_user(argp
, &mtpos
, sizeof(struct mtpos
)))
3650 if (tape
->chrdev_direction
== idetape_direction_read
)
3651 idetape_discard_read_pipeline(drive
, 1);
3652 return idetape_blkdev_ioctl(drive
, cmd
, arg
);
3656 static void idetape_get_blocksize_from_block_descriptor(ide_drive_t
*drive
);
3659 * Our character device open function.
3661 static int idetape_chrdev_open (struct inode
*inode
, struct file
*filp
)
3663 unsigned int minor
= iminor(inode
), i
= minor
& ~0xc0;
3665 idetape_tape_t
*tape
;
3670 * We really want to do nonseekable_open(inode, filp); here, but some
3671 * versions of tar incorrectly call lseek on tapes and bail out if that
3672 * fails. So we disallow pread() and pwrite(), but permit lseeks.
3674 filp
->f_mode
&= ~(FMODE_PREAD
| FMODE_PWRITE
);
3676 #if IDETAPE_DEBUG_LOG
3677 printk(KERN_INFO
"ide-tape: Reached idetape_chrdev_open\n");
3678 #endif /* IDETAPE_DEBUG_LOG */
3680 if (i
>= MAX_HWIFS
* MAX_DRIVES
)
3683 if (!(tape
= ide_tape_chrdev_get(i
)))
3686 drive
= tape
->drive
;
3688 filp
->private_data
= tape
;
3690 if (test_and_set_bit(IDETAPE_BUSY
, &tape
->flags
)) {
3695 retval
= idetape_wait_ready(drive
, 60 * HZ
);
3697 clear_bit(IDETAPE_BUSY
, &tape
->flags
);
3698 printk(KERN_ERR
"ide-tape: %s: drive not ready\n", tape
->name
);
3702 idetape_read_position(drive
);
3703 if (!test_bit(IDETAPE_ADDRESS_VALID
, &tape
->flags
))
3704 (void)idetape_rewind_tape(drive
);
3706 if (tape
->chrdev_direction
!= idetape_direction_read
)
3707 clear_bit(IDETAPE_PIPELINE_ERROR
, &tape
->flags
);
3709 /* Read block size and write protect status from drive. */
3710 idetape_get_blocksize_from_block_descriptor(drive
);
3712 /* Set write protect flag if device is opened as read-only. */
3713 if ((filp
->f_flags
& O_ACCMODE
) == O_RDONLY
)
3714 tape
->write_prot
= 1;
3716 tape
->write_prot
= tape
->drv_write_prot
;
3718 /* Make sure drive isn't write protected if user wants to write. */
3719 if (tape
->write_prot
) {
3720 if ((filp
->f_flags
& O_ACCMODE
) == O_WRONLY
||
3721 (filp
->f_flags
& O_ACCMODE
) == O_RDWR
) {
3722 clear_bit(IDETAPE_BUSY
, &tape
->flags
);
3729 * Lock the tape drive door so user can't eject.
3731 if (tape
->chrdev_direction
== idetape_direction_none
) {
3732 if (idetape_create_prevent_cmd(drive
, &pc
, 1)) {
3733 if (!idetape_queue_pc_tail(drive
, &pc
)) {
3734 if (tape
->door_locked
!= DOOR_EXPLICITLY_LOCKED
)
3735 tape
->door_locked
= DOOR_LOCKED
;
3739 idetape_restart_speed_control(drive
);
3740 tape
->restart_speed_control_req
= 0;
3748 static void idetape_write_release (ide_drive_t
*drive
, unsigned int minor
)
3750 idetape_tape_t
*tape
= drive
->driver_data
;
3752 idetape_empty_write_pipeline(drive
);
3753 tape
->merge_stage
= __idetape_kmalloc_stage(tape
, 1, 0);
3754 if (tape
->merge_stage
!= NULL
) {
3755 idetape_pad_zeros(drive
, tape
->tape_block_size
* (tape
->user_bs_factor
- 1));
3756 __idetape_kfree_stage(tape
->merge_stage
);
3757 tape
->merge_stage
= NULL
;
3759 idetape_write_filemark(drive
);
3760 idetape_flush_tape_buffers(drive
);
3761 idetape_flush_tape_buffers(drive
);
3765 * Our character device release function.
3767 static int idetape_chrdev_release (struct inode
*inode
, struct file
*filp
)
3769 struct ide_tape_obj
*tape
= ide_tape_f(filp
);
3770 ide_drive_t
*drive
= tape
->drive
;
3772 unsigned int minor
= iminor(inode
);
3775 tape
= drive
->driver_data
;
3776 #if IDETAPE_DEBUG_LOG
3777 if (tape
->debug_level
>= 3)
3778 printk(KERN_INFO
"ide-tape: Reached idetape_chrdev_release\n");
3779 #endif /* IDETAPE_DEBUG_LOG */
3781 if (tape
->chrdev_direction
== idetape_direction_write
)
3782 idetape_write_release(drive
, minor
);
3783 if (tape
->chrdev_direction
== idetape_direction_read
) {
3785 idetape_discard_read_pipeline(drive
, 1);
3787 idetape_wait_for_pipeline(drive
);
3789 if (tape
->cache_stage
!= NULL
) {
3790 __idetape_kfree_stage(tape
->cache_stage
);
3791 tape
->cache_stage
= NULL
;
3793 if (minor
< 128 && test_bit(IDETAPE_MEDIUM_PRESENT
, &tape
->flags
))
3794 (void) idetape_rewind_tape(drive
);
3795 if (tape
->chrdev_direction
== idetape_direction_none
) {
3796 if (tape
->door_locked
== DOOR_LOCKED
) {
3797 if (idetape_create_prevent_cmd(drive
, &pc
, 0)) {
3798 if (!idetape_queue_pc_tail(drive
, &pc
))
3799 tape
->door_locked
= DOOR_UNLOCKED
;
3803 clear_bit(IDETAPE_BUSY
, &tape
->flags
);
3810 * idetape_identify_device is called to check the contents of the
3811 * ATAPI IDENTIFY command results. We return:
3813 * 1 If the tape can be supported by us, based on the information
3816 * 0 If this tape driver is not currently supported by us.
3818 static int idetape_identify_device (ide_drive_t
*drive
)
3820 struct idetape_id_gcw gcw
;
3821 struct hd_driveid
*id
= drive
->id
;
3823 if (drive
->id_read
== 0)
3826 *((unsigned short *) &gcw
) = id
->config
;
3828 #if IDETAPE_DEBUG_INFO
3829 printk(KERN_INFO
"ide-tape: Dumping ATAPI Identify Device tape parameters\n");
3830 printk(KERN_INFO
"ide-tape: Protocol Type: ");
3831 switch (gcw
.protocol
) {
3832 case 0: case 1: printk("ATA\n");break;
3833 case 2: printk("ATAPI\n");break;
3834 case 3: printk("Reserved (Unknown to ide-tape)\n");break;
3836 printk(KERN_INFO
"ide-tape: Device Type: %x - ",gcw
.device_type
);
3837 switch (gcw
.device_type
) {
3838 case 0: printk("Direct-access Device\n");break;
3839 case 1: printk("Streaming Tape Device\n");break;
3840 case 2: case 3: case 4: printk("Reserved\n");break;
3841 case 5: printk("CD-ROM Device\n");break;
3842 case 6: printk("Reserved\n");
3843 case 7: printk("Optical memory Device\n");break;
3844 case 0x1f: printk("Unknown or no Device type\n");break;
3845 default: printk("Reserved\n");
3847 printk(KERN_INFO
"ide-tape: Removable: %s",gcw
.removable
? "Yes\n":"No\n");
3848 printk(KERN_INFO
"ide-tape: Command Packet DRQ Type: ");
3849 switch (gcw
.drq_type
) {
3850 case 0: printk("Microprocessor DRQ\n");break;
3851 case 1: printk("Interrupt DRQ\n");break;
3852 case 2: printk("Accelerated DRQ\n");break;
3853 case 3: printk("Reserved\n");break;
3855 printk(KERN_INFO
"ide-tape: Command Packet Size: ");
3856 switch (gcw
.packet_size
) {
3857 case 0: printk("12 bytes\n");break;
3858 case 1: printk("16 bytes\n");break;
3859 default: printk("Reserved\n");break;
3861 #endif /* IDETAPE_DEBUG_INFO */
3863 /* Check that we can support this device */
3865 if (gcw
.protocol
!=2 )
3866 printk(KERN_ERR
"ide-tape: Protocol is not ATAPI\n");
3867 else if (gcw
.device_type
!= 1)
3868 printk(KERN_ERR
"ide-tape: Device type is not set to tape\n");
3869 else if (!gcw
.removable
)
3870 printk(KERN_ERR
"ide-tape: The removable flag is not set\n");
3871 else if (gcw
.packet_size
!= 0) {
3872 printk(KERN_ERR
"ide-tape: Packet size is not 12 bytes long\n");
3873 if (gcw
.packet_size
== 1)
3874 printk(KERN_ERR
"ide-tape: Sorry, padding to 16 bytes is still not supported\n");
3881 * Use INQUIRY to get the firmware revision
3883 static void idetape_get_inquiry_results (ide_drive_t
*drive
)
3886 idetape_tape_t
*tape
= drive
->driver_data
;
3888 idetape_inquiry_result_t
*inquiry
;
3890 idetape_create_inquiry_cmd(&pc
);
3891 if (idetape_queue_pc_tail(drive
, &pc
)) {
3892 printk(KERN_ERR
"ide-tape: %s: can't get INQUIRY results\n", tape
->name
);
3895 inquiry
= (idetape_inquiry_result_t
*) pc
.buffer
;
3896 memcpy(tape
->vendor_id
, inquiry
->vendor_id
, 8);
3897 memcpy(tape
->product_id
, inquiry
->product_id
, 16);
3898 memcpy(tape
->firmware_revision
, inquiry
->revision_level
, 4);
3899 ide_fixstring(tape
->vendor_id
, 10, 0);
3900 ide_fixstring(tape
->product_id
, 18, 0);
3901 ide_fixstring(tape
->firmware_revision
, 6, 0);
3902 r
= tape
->firmware_revision
;
3903 if (*(r
+ 1) == '.')
3904 tape
->firmware_revision_num
= (*r
- '0') * 100 + (*(r
+ 2) - '0') * 10 + *(r
+ 3) - '0';
3905 printk(KERN_INFO
"ide-tape: %s <-> %s: %s %s rev %s\n", drive
->name
, tape
->name
, tape
->vendor_id
, tape
->product_id
, tape
->firmware_revision
);
3909 * idetape_get_mode_sense_results asks the tape about its various
3910 * parameters. In particular, we will adjust our data transfer buffer
3911 * size to the recommended value as returned by the tape.
3913 static void idetape_get_mode_sense_results (ide_drive_t
*drive
)
3915 idetape_tape_t
*tape
= drive
->driver_data
;
3917 idetape_mode_parameter_header_t
*header
;
3918 idetape_capabilities_page_t
*capabilities
;
3920 idetape_create_mode_sense_cmd(&pc
, IDETAPE_CAPABILITIES_PAGE
);
3921 if (idetape_queue_pc_tail(drive
, &pc
)) {
3922 printk(KERN_ERR
"ide-tape: Can't get tape parameters - assuming some default values\n");
3923 tape
->tape_block_size
= 512;
3924 tape
->capabilities
.ctl
= 52;
3925 tape
->capabilities
.speed
= 450;
3926 tape
->capabilities
.buffer_size
= 6 * 52;
3929 header
= (idetape_mode_parameter_header_t
*) pc
.buffer
;
3930 capabilities
= (idetape_capabilities_page_t
*) (pc
.buffer
+ sizeof(idetape_mode_parameter_header_t
) + header
->bdl
);
3932 capabilities
->max_speed
= ntohs(capabilities
->max_speed
);
3933 capabilities
->ctl
= ntohs(capabilities
->ctl
);
3934 capabilities
->speed
= ntohs(capabilities
->speed
);
3935 capabilities
->buffer_size
= ntohs(capabilities
->buffer_size
);
3937 if (!capabilities
->speed
) {
3938 printk(KERN_INFO
"ide-tape: %s: overriding capabilities->speed (assuming 650KB/sec)\n", drive
->name
);
3939 capabilities
->speed
= 650;
3941 if (!capabilities
->max_speed
) {
3942 printk(KERN_INFO
"ide-tape: %s: overriding capabilities->max_speed (assuming 650KB/sec)\n", drive
->name
);
3943 capabilities
->max_speed
= 650;
3946 tape
->capabilities
= *capabilities
; /* Save us a copy */
3947 if (capabilities
->blk512
)
3948 tape
->tape_block_size
= 512;
3949 else if (capabilities
->blk1024
)
3950 tape
->tape_block_size
= 1024;
3952 #if IDETAPE_DEBUG_INFO
3953 printk(KERN_INFO
"ide-tape: Dumping the results of the MODE SENSE packet command\n");
3954 printk(KERN_INFO
"ide-tape: Mode Parameter Header:\n");
3955 printk(KERN_INFO
"ide-tape: Mode Data Length - %d\n",header
->mode_data_length
);
3956 printk(KERN_INFO
"ide-tape: Medium Type - %d\n",header
->medium_type
);
3957 printk(KERN_INFO
"ide-tape: Device Specific Parameter - %d\n",header
->dsp
);
3958 printk(KERN_INFO
"ide-tape: Block Descriptor Length - %d\n",header
->bdl
);
3960 printk(KERN_INFO
"ide-tape: Capabilities and Mechanical Status Page:\n");
3961 printk(KERN_INFO
"ide-tape: Page code - %d\n",capabilities
->page_code
);
3962 printk(KERN_INFO
"ide-tape: Page length - %d\n",capabilities
->page_length
);
3963 printk(KERN_INFO
"ide-tape: Read only - %s\n",capabilities
->ro
? "Yes":"No");
3964 printk(KERN_INFO
"ide-tape: Supports reverse space - %s\n",capabilities
->sprev
? "Yes":"No");
3965 printk(KERN_INFO
"ide-tape: Supports erase initiated formatting - %s\n",capabilities
->efmt
? "Yes":"No");
3966 printk(KERN_INFO
"ide-tape: Supports QFA two Partition format - %s\n",capabilities
->qfa
? "Yes":"No");
3967 printk(KERN_INFO
"ide-tape: Supports locking the medium - %s\n",capabilities
->lock
? "Yes":"No");
3968 printk(KERN_INFO
"ide-tape: The volume is currently locked - %s\n",capabilities
->locked
? "Yes":"No");
3969 printk(KERN_INFO
"ide-tape: The device defaults in the prevent state - %s\n",capabilities
->prevent
? "Yes":"No");
3970 printk(KERN_INFO
"ide-tape: Supports ejecting the medium - %s\n",capabilities
->eject
? "Yes":"No");
3971 printk(KERN_INFO
"ide-tape: Supports error correction - %s\n",capabilities
->ecc
? "Yes":"No");
3972 printk(KERN_INFO
"ide-tape: Supports data compression - %s\n",capabilities
->cmprs
? "Yes":"No");
3973 printk(KERN_INFO
"ide-tape: Supports 512 bytes block size - %s\n",capabilities
->blk512
? "Yes":"No");
3974 printk(KERN_INFO
"ide-tape: Supports 1024 bytes block size - %s\n",capabilities
->blk1024
? "Yes":"No");
3975 printk(KERN_INFO
"ide-tape: Supports 32768 bytes block size / Restricted byte count for PIO transfers - %s\n",capabilities
->blk32768
? "Yes":"No");
3976 printk(KERN_INFO
"ide-tape: Maximum supported speed in KBps - %d\n",capabilities
->max_speed
);
3977 printk(KERN_INFO
"ide-tape: Continuous transfer limits in blocks - %d\n",capabilities
->ctl
);
3978 printk(KERN_INFO
"ide-tape: Current speed in KBps - %d\n",capabilities
->speed
);
3979 printk(KERN_INFO
"ide-tape: Buffer size - %d\n",capabilities
->buffer_size
*512);
3980 #endif /* IDETAPE_DEBUG_INFO */
3984 * ide_get_blocksize_from_block_descriptor does a mode sense page 0 with block descriptor
3985 * and if it succeeds sets the tape block size with the reported value
3987 static void idetape_get_blocksize_from_block_descriptor(ide_drive_t
*drive
)
3990 idetape_tape_t
*tape
= drive
->driver_data
;
3992 idetape_mode_parameter_header_t
*header
;
3993 idetape_parameter_block_descriptor_t
*block_descrp
;
3995 idetape_create_mode_sense_cmd(&pc
, IDETAPE_BLOCK_DESCRIPTOR
);
3996 if (idetape_queue_pc_tail(drive
, &pc
)) {
3997 printk(KERN_ERR
"ide-tape: Can't get block descriptor\n");
3998 if (tape
->tape_block_size
== 0) {
3999 printk(KERN_WARNING
"ide-tape: Cannot deal with zero block size, assume 32k\n");
4000 tape
->tape_block_size
= 32768;
4004 header
= (idetape_mode_parameter_header_t
*) pc
.buffer
;
4005 block_descrp
= (idetape_parameter_block_descriptor_t
*) (pc
.buffer
+ sizeof(idetape_mode_parameter_header_t
));
4006 tape
->tape_block_size
=( block_descrp
->length
[0]<<16) + (block_descrp
->length
[1]<<8) + block_descrp
->length
[2];
4007 tape
->drv_write_prot
= (header
->dsp
& 0x80) >> 7;
4009 #if IDETAPE_DEBUG_INFO
4010 printk(KERN_INFO
"ide-tape: Adjusted block size - %d\n", tape
->tape_block_size
);
4011 #endif /* IDETAPE_DEBUG_INFO */
4014 #ifdef CONFIG_IDE_PROC_FS
4015 static void idetape_add_settings (ide_drive_t
*drive
)
4017 idetape_tape_t
*tape
= drive
->driver_data
;
4020 * drive setting name read/write data type min max mul_factor div_factor data pointer set function
4022 ide_add_setting(drive
, "buffer", SETTING_READ
, TYPE_SHORT
, 0, 0xffff, 1, 2, &tape
->capabilities
.buffer_size
, NULL
);
4023 ide_add_setting(drive
, "pipeline_min", SETTING_RW
, TYPE_INT
, 1, 0xffff, tape
->stage_size
/ 1024, 1, &tape
->min_pipeline
, NULL
);
4024 ide_add_setting(drive
, "pipeline", SETTING_RW
, TYPE_INT
, 1, 0xffff, tape
->stage_size
/ 1024, 1, &tape
->max_stages
, NULL
);
4025 ide_add_setting(drive
, "pipeline_max", SETTING_RW
, TYPE_INT
, 1, 0xffff, tape
->stage_size
/ 1024, 1, &tape
->max_pipeline
, NULL
);
4026 ide_add_setting(drive
, "pipeline_used", SETTING_READ
, TYPE_INT
, 0, 0xffff, tape
->stage_size
/ 1024, 1, &tape
->nr_stages
, NULL
);
4027 ide_add_setting(drive
, "pipeline_pending", SETTING_READ
, TYPE_INT
, 0, 0xffff, tape
->stage_size
/ 1024, 1, &tape
->nr_pending_stages
, NULL
);
4028 ide_add_setting(drive
, "speed", SETTING_READ
, TYPE_SHORT
, 0, 0xffff, 1, 1, &tape
->capabilities
.speed
, NULL
);
4029 ide_add_setting(drive
, "stage", SETTING_READ
, TYPE_INT
, 0, 0xffff, 1, 1024, &tape
->stage_size
, NULL
);
4030 ide_add_setting(drive
, "tdsc", SETTING_RW
, TYPE_INT
, IDETAPE_DSC_RW_MIN
, IDETAPE_DSC_RW_MAX
, 1000, HZ
, &tape
->best_dsc_rw_frequency
, NULL
);
4031 ide_add_setting(drive
, "dsc_overlap", SETTING_RW
, TYPE_BYTE
, 0, 1, 1, 1, &drive
->dsc_overlap
, NULL
);
4032 ide_add_setting(drive
, "pipeline_head_speed_c",SETTING_READ
, TYPE_INT
, 0, 0xffff, 1, 1, &tape
->controlled_pipeline_head_speed
, NULL
);
4033 ide_add_setting(drive
, "pipeline_head_speed_u",SETTING_READ
, TYPE_INT
, 0, 0xffff, 1, 1, &tape
->uncontrolled_pipeline_head_speed
,NULL
);
4034 ide_add_setting(drive
, "avg_speed", SETTING_READ
, TYPE_INT
, 0, 0xffff, 1, 1, &tape
->avg_speed
, NULL
);
4035 ide_add_setting(drive
, "debug_level", SETTING_RW
, TYPE_INT
, 0, 0xffff, 1, 1, &tape
->debug_level
, NULL
);
4038 static inline void idetape_add_settings(ide_drive_t
*drive
) { ; }
4042 * ide_setup is called to:
4044 * 1. Initialize our various state variables.
4045 * 2. Ask the tape for its capabilities.
4046 * 3. Allocate a buffer which will be used for data
4047 * transfer. The buffer size is chosen based on
4048 * the recommendation which we received in step (2).
4050 * Note that at this point ide.c already assigned us an irq, so that
4051 * we can queue requests here and wait for their completion.
4053 static void idetape_setup (ide_drive_t
*drive
, idetape_tape_t
*tape
, int minor
)
4055 unsigned long t1
, tmid
, tn
, t
;
4057 struct idetape_id_gcw gcw
;
4061 spin_lock_init(&tape
->spinlock
);
4062 drive
->dsc_overlap
= 1;
4063 if (drive
->hwif
->host_flags
& IDE_HFLAG_NO_DSC
) {
4064 printk(KERN_INFO
"ide-tape: %s: disabling DSC overlap\n",
4066 drive
->dsc_overlap
= 0;
4068 /* Seagate Travan drives do not support DSC overlap. */
4069 if (strstr(drive
->id
->model
, "Seagate STT3401"))
4070 drive
->dsc_overlap
= 0;
4071 tape
->minor
= minor
;
4072 tape
->name
[0] = 'h';
4073 tape
->name
[1] = 't';
4074 tape
->name
[2] = '0' + minor
;
4075 tape
->chrdev_direction
= idetape_direction_none
;
4076 tape
->pc
= tape
->pc_stack
;
4077 tape
->max_insert_speed
= 10000;
4078 tape
->speed_control
= 1;
4079 *((unsigned short *) &gcw
) = drive
->id
->config
;
4080 if (gcw
.drq_type
== 1)
4081 set_bit(IDETAPE_DRQ_INTERRUPT
, &tape
->flags
);
4083 tape
->min_pipeline
= tape
->max_pipeline
= tape
->max_stages
= 10;
4085 idetape_get_inquiry_results(drive
);
4086 idetape_get_mode_sense_results(drive
);
4087 idetape_get_blocksize_from_block_descriptor(drive
);
4088 tape
->user_bs_factor
= 1;
4089 tape
->stage_size
= tape
->capabilities
.ctl
* tape
->tape_block_size
;
4090 while (tape
->stage_size
> 0xffff) {
4091 printk(KERN_NOTICE
"ide-tape: decreasing stage size\n");
4092 tape
->capabilities
.ctl
/= 2;
4093 tape
->stage_size
= tape
->capabilities
.ctl
* tape
->tape_block_size
;
4095 stage_size
= tape
->stage_size
;
4096 tape
->pages_per_stage
= stage_size
/ PAGE_SIZE
;
4097 if (stage_size
% PAGE_SIZE
) {
4098 tape
->pages_per_stage
++;
4099 tape
->excess_bh_size
= PAGE_SIZE
- stage_size
% PAGE_SIZE
;
4103 * Select the "best" DSC read/write polling frequency
4104 * and pipeline size.
4106 speed
= max(tape
->capabilities
.speed
, tape
->capabilities
.max_speed
);
4108 tape
->max_stages
= speed
* 1000 * 10 / tape
->stage_size
;
4111 * Limit memory use for pipeline to 10% of physical memory
4114 if (tape
->max_stages
* tape
->stage_size
> si
.totalram
* si
.mem_unit
/ 10)
4115 tape
->max_stages
= si
.totalram
* si
.mem_unit
/ (10 * tape
->stage_size
);
4116 tape
->max_stages
= min(tape
->max_stages
, IDETAPE_MAX_PIPELINE_STAGES
);
4117 tape
->min_pipeline
= min(tape
->max_stages
, IDETAPE_MIN_PIPELINE_STAGES
);
4118 tape
->max_pipeline
= min(tape
->max_stages
* 2, IDETAPE_MAX_PIPELINE_STAGES
);
4119 if (tape
->max_stages
== 0)
4120 tape
->max_stages
= tape
->min_pipeline
= tape
->max_pipeline
= 1;
4122 t1
= (tape
->stage_size
* HZ
) / (speed
* 1000);
4123 tmid
= (tape
->capabilities
.buffer_size
* 32 * HZ
) / (speed
* 125);
4124 tn
= (IDETAPE_FIFO_THRESHOLD
* tape
->stage_size
* HZ
) / (speed
* 1000);
4126 if (tape
->max_stages
)
4132 * Ensure that the number we got makes sense; limit
4133 * it within IDETAPE_DSC_RW_MIN and IDETAPE_DSC_RW_MAX.
4135 tape
->best_dsc_rw_frequency
= max_t(unsigned long, min_t(unsigned long, t
, IDETAPE_DSC_RW_MAX
), IDETAPE_DSC_RW_MIN
);
4136 printk(KERN_INFO
"ide-tape: %s <-> %s: %dKBps, %d*%dkB buffer, "
4137 "%dkB pipeline, %lums tDSC%s\n",
4138 drive
->name
, tape
->name
, tape
->capabilities
.speed
,
4139 (tape
->capabilities
.buffer_size
* 512) / tape
->stage_size
,
4140 tape
->stage_size
/ 1024,
4141 tape
->max_stages
* tape
->stage_size
/ 1024,
4142 tape
->best_dsc_rw_frequency
* 1000 / HZ
,
4143 drive
->using_dma
? ", DMA":"");
4145 idetape_add_settings(drive
);
4148 static void ide_tape_remove(ide_drive_t
*drive
)
4150 idetape_tape_t
*tape
= drive
->driver_data
;
4152 ide_proc_unregister_driver(drive
, tape
->driver
);
4154 ide_unregister_region(tape
->disk
);
4159 static void ide_tape_release(struct kref
*kref
)
4161 struct ide_tape_obj
*tape
= to_ide_tape(kref
);
4162 ide_drive_t
*drive
= tape
->drive
;
4163 struct gendisk
*g
= tape
->disk
;
4165 BUG_ON(tape
->first_stage
!= NULL
|| tape
->merge_stage_size
);
4167 drive
->dsc_overlap
= 0;
4168 drive
->driver_data
= NULL
;
4169 device_destroy(idetape_sysfs_class
, MKDEV(IDETAPE_MAJOR
, tape
->minor
));
4170 device_destroy(idetape_sysfs_class
, MKDEV(IDETAPE_MAJOR
, tape
->minor
+ 128));
4171 idetape_devs
[tape
->minor
] = NULL
;
4172 g
->private_data
= NULL
;
4177 #ifdef CONFIG_IDE_PROC_FS
4178 static int proc_idetape_read_name
4179 (char *page
, char **start
, off_t off
, int count
, int *eof
, void *data
)
4181 ide_drive_t
*drive
= (ide_drive_t
*) data
;
4182 idetape_tape_t
*tape
= drive
->driver_data
;
4186 len
= sprintf(out
, "%s\n", tape
->name
);
4187 PROC_IDE_READ_RETURN(page
, start
, off
, count
, eof
, len
);
4190 static ide_proc_entry_t idetape_proc
[] = {
4191 { "capacity", S_IFREG
|S_IRUGO
, proc_ide_read_capacity
, NULL
},
4192 { "name", S_IFREG
|S_IRUGO
, proc_idetape_read_name
, NULL
},
4193 { NULL
, 0, NULL
, NULL
}
4197 static int ide_tape_probe(ide_drive_t
*);
4199 static ide_driver_t idetape_driver
= {
4201 .owner
= THIS_MODULE
,
4203 .bus
= &ide_bus_type
,
4205 .probe
= ide_tape_probe
,
4206 .remove
= ide_tape_remove
,
4207 .version
= IDETAPE_VERSION
,
4209 .supports_dsc_overlap
= 1,
4210 .do_request
= idetape_do_request
,
4211 .end_request
= idetape_end_request
,
4212 .error
= __ide_error
,
4213 .abort
= __ide_abort
,
4214 #ifdef CONFIG_IDE_PROC_FS
4215 .proc
= idetape_proc
,
4220 * Our character device supporting functions, passed to register_chrdev.
4222 static const struct file_operations idetape_fops
= {
4223 .owner
= THIS_MODULE
,
4224 .read
= idetape_chrdev_read
,
4225 .write
= idetape_chrdev_write
,
4226 .ioctl
= idetape_chrdev_ioctl
,
4227 .open
= idetape_chrdev_open
,
4228 .release
= idetape_chrdev_release
,
4231 static int idetape_open(struct inode
*inode
, struct file
*filp
)
4233 struct gendisk
*disk
= inode
->i_bdev
->bd_disk
;
4234 struct ide_tape_obj
*tape
;
4236 if (!(tape
= ide_tape_get(disk
)))
4242 static int idetape_release(struct inode
*inode
, struct file
*filp
)
4244 struct gendisk
*disk
= inode
->i_bdev
->bd_disk
;
4245 struct ide_tape_obj
*tape
= ide_tape_g(disk
);
4252 static int idetape_ioctl(struct inode
*inode
, struct file
*file
,
4253 unsigned int cmd
, unsigned long arg
)
4255 struct block_device
*bdev
= inode
->i_bdev
;
4256 struct ide_tape_obj
*tape
= ide_tape_g(bdev
->bd_disk
);
4257 ide_drive_t
*drive
= tape
->drive
;
4258 int err
= generic_ide_ioctl(drive
, file
, bdev
, cmd
, arg
);
4260 err
= idetape_blkdev_ioctl(drive
, cmd
, arg
);
4264 static struct block_device_operations idetape_block_ops
= {
4265 .owner
= THIS_MODULE
,
4266 .open
= idetape_open
,
4267 .release
= idetape_release
,
4268 .ioctl
= idetape_ioctl
,
4271 static int ide_tape_probe(ide_drive_t
*drive
)
4273 idetape_tape_t
*tape
;
4277 if (!strstr("ide-tape", drive
->driver_req
))
4279 if (!drive
->present
)
4281 if (drive
->media
!= ide_tape
)
4283 if (!idetape_identify_device (drive
)) {
4284 printk(KERN_ERR
"ide-tape: %s: not supported by this version of ide-tape\n", drive
->name
);
4288 printk("ide-tape: passing drive %s to ide-scsi emulation.\n", drive
->name
);
4291 if (strstr(drive
->id
->model
, "OnStream DI-")) {
4292 printk(KERN_WARNING
"ide-tape: Use drive %s with ide-scsi emulation and osst.\n", drive
->name
);
4293 printk(KERN_WARNING
"ide-tape: OnStream support will be removed soon from ide-tape!\n");
4295 tape
= kzalloc(sizeof (idetape_tape_t
), GFP_KERNEL
);
4297 printk(KERN_ERR
"ide-tape: %s: Can't allocate a tape structure\n", drive
->name
);
4301 g
= alloc_disk(1 << PARTN_BITS
);
4305 ide_init_disk(g
, drive
);
4307 ide_proc_register_driver(drive
, &idetape_driver
);
4309 kref_init(&tape
->kref
);
4311 tape
->drive
= drive
;
4312 tape
->driver
= &idetape_driver
;
4315 g
->private_data
= &tape
->driver
;
4317 drive
->driver_data
= tape
;
4319 mutex_lock(&idetape_ref_mutex
);
4320 for (minor
= 0; idetape_devs
[minor
]; minor
++)
4322 idetape_devs
[minor
] = tape
;
4323 mutex_unlock(&idetape_ref_mutex
);
4325 idetape_setup(drive
, tape
, minor
);
4327 device_create(idetape_sysfs_class
, &drive
->gendev
,
4328 MKDEV(IDETAPE_MAJOR
, minor
), "%s", tape
->name
);
4329 device_create(idetape_sysfs_class
, &drive
->gendev
,
4330 MKDEV(IDETAPE_MAJOR
, minor
+ 128), "n%s", tape
->name
);
4332 g
->fops
= &idetape_block_ops
;
4333 ide_register_region(g
);
4343 MODULE_DESCRIPTION("ATAPI Streaming TAPE Driver");
4344 MODULE_LICENSE("GPL");
4346 static void __exit
idetape_exit (void)
4348 driver_unregister(&idetape_driver
.gen_driver
);
4349 class_destroy(idetape_sysfs_class
);
4350 unregister_chrdev(IDETAPE_MAJOR
, "ht");
4353 static int __init
idetape_init(void)
4356 idetape_sysfs_class
= class_create(THIS_MODULE
, "ide_tape");
4357 if (IS_ERR(idetape_sysfs_class
)) {
4358 idetape_sysfs_class
= NULL
;
4359 printk(KERN_ERR
"Unable to create sysfs class for ide tapes\n");
4364 if (register_chrdev(IDETAPE_MAJOR
, "ht", &idetape_fops
)) {
4365 printk(KERN_ERR
"ide-tape: Failed to register character device interface\n");
4367 goto out_free_class
;
4370 error
= driver_register(&idetape_driver
.gen_driver
);
4372 goto out_free_driver
;
4377 driver_unregister(&idetape_driver
.gen_driver
);
4379 class_destroy(idetape_sysfs_class
);
4384 MODULE_ALIAS("ide:*m-tape*");
4385 module_init(idetape_init
);
4386 module_exit(idetape_exit
);
4387 MODULE_ALIAS_CHARDEV_MAJOR(IDETAPE_MAJOR
);