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 Block Descriptor the MODE SENSE packet command
753 * Support for block descriptors is optional.
756 __u8 density_code; /* Medium density code */
757 __u8 blocks[3]; /* Number of blocks */
758 __u8 reserved4; /* Reserved */
759 __u8 length[3]; /* Block Length */
760 } idetape_parameter_block_descriptor_t;
763 * The Data Compression Page, as returned by the MODE SENSE packet command.
766 unsigned page_code :6; /* Page Code - Should be 0xf */
767 unsigned reserved0 :1; /* Reserved */
769 __u8 page_length; /* Page Length - Should be 14 */
770 unsigned reserved2 :6; /* Reserved */
771 unsigned dcc :1; /* Data Compression Capable */
772 unsigned dce :1; /* Data Compression Enable */
773 unsigned reserved3 :5; /* Reserved */
774 unsigned red :2; /* Report Exception on Decompression */
775 unsigned dde :1; /* Data Decompression Enable */
776 __u32 ca; /* Compression Algorithm */
777 __u32 da; /* Decompression Algorithm */
778 __u8 reserved[4]; /* Reserved */
779 } idetape_data_compression_page_t;
782 * The Medium Partition Page, as returned by the MODE SENSE packet command.
785 unsigned page_code :6; /* Page Code - Should be 0x11 */
786 unsigned reserved1_6 :1; /* Reserved */
788 __u8 page_length; /* Page Length - Should be 6 */
789 __u8 map; /* Maximum Additional Partitions - Should be 0 */
790 __u8 apd; /* Additional Partitions Defined - Should be 0 */
791 unsigned reserved4_012 :3; /* Reserved */
792 unsigned psum :2; /* Should be 0 */
793 unsigned idp :1; /* Should be 0 */
794 unsigned sdp :1; /* Should be 0 */
795 unsigned fdp :1; /* Fixed Data Partitions */
796 __u8 mfr; /* Medium Format Recognition */
797 __u8 reserved[2]; /* Reserved */
798 } idetape_medium_partition_page_t;
801 * Run time configurable parameters.
804 int dsc_rw_frequency;
805 int dsc_media_access_frequency;
810 * The variables below are used for the character device interface.
811 * Additional state variables are defined in our ide_drive_t structure.
813 static struct ide_tape_obj * idetape_devs[MAX_HWIFS * MAX_DRIVES];
815 #define ide_tape_f(file) ((file)->private_data)
817 static struct ide_tape_obj *ide_tape_chrdev_get(unsigned int i)
819 struct ide_tape_obj *tape = NULL;
821 mutex_lock(&idetape_ref_mutex);
822 tape = idetape_devs[i];
824 kref_get(&tape->kref);
825 mutex_unlock(&idetape_ref_mutex);
830 * Function declarations
833 static int idetape_chrdev_release (struct inode *inode, struct file *filp);
834 static void idetape_write_release (ide_drive_t *drive, unsigned int minor);
837 * Too bad. The drive wants to send us data which we are not ready to accept.
838 * Just throw it away.
840 static void idetape_discard_data (ide_drive_t *drive, unsigned int bcount)
843 (void) HWIF(drive)->INB(IDE_DATA_REG);
846 static void idetape_input_buffers (ide_drive_t *drive, idetape_pc_t *pc, unsigned int bcount)
848 struct idetape_bh *bh = pc->bh;
852 #if IDETAPE_DEBUG_BUGS
854 printk(KERN_ERR "ide-tape: bh == NULL in "
855 "idetape_input_buffers\n");
856 idetape_discard_data(drive, bcount);
859 #endif /* IDETAPE_DEBUG_BUGS */
860 count = min((unsigned int)(bh->b_size - atomic_read(&bh->b_count)), bcount);
861 HWIF(drive)->atapi_input_bytes(drive, bh->b_data + atomic_read(&bh->b_count), count);
863 atomic_add(count, &bh->b_count);
864 if (atomic_read(&bh->b_count) == bh->b_size) {
867 atomic_set(&bh->b_count, 0);
873 static void idetape_output_buffers (ide_drive_t *drive, idetape_pc_t *pc, unsigned int bcount)
875 struct idetape_bh *bh = pc->bh;
879 #if IDETAPE_DEBUG_BUGS
881 printk(KERN_ERR "ide-tape: bh == NULL in "
882 "idetape_output_buffers\n");
885 #endif /* IDETAPE_DEBUG_BUGS */
886 count = min((unsigned int)pc->b_count, (unsigned int)bcount);
887 HWIF(drive)->atapi_output_bytes(drive, pc->b_data, count);
890 pc->b_count -= count;
892 pc->bh = bh = bh->b_reqnext;
894 pc->b_data = bh->b_data;
895 pc->b_count = atomic_read(&bh->b_count);
901 static void idetape_update_buffers (idetape_pc_t *pc)
903 struct idetape_bh *bh = pc->bh;
905 unsigned int bcount = pc->actually_transferred;
907 if (test_bit(PC_WRITING, &pc->flags))
910 #if IDETAPE_DEBUG_BUGS
912 printk(KERN_ERR "ide-tape: bh == NULL in "
913 "idetape_update_buffers\n");
916 #endif /* IDETAPE_DEBUG_BUGS */
917 count = min((unsigned int)bh->b_size, (unsigned int)bcount);
918 atomic_set(&bh->b_count, count);
919 if (atomic_read(&bh->b_count) == bh->b_size)
927 * idetape_next_pc_storage returns a pointer to a place in which we can
928 * safely store a packet command, even though we intend to leave the
929 * driver. A storage space for a maximum of IDETAPE_PC_STACK packet
930 * commands is allocated at initialization time.
932 static idetape_pc_t *idetape_next_pc_storage (ide_drive_t *drive)
934 idetape_tape_t *tape = drive->driver_data;
936 #if IDETAPE_DEBUG_LOG
937 if (tape->debug_level >= 5)
938 printk(KERN_INFO "ide-tape: pc_stack_index=%d\n",
939 tape->pc_stack_index);
940 #endif /* IDETAPE_DEBUG_LOG */
941 if (tape->pc_stack_index == IDETAPE_PC_STACK)
942 tape->pc_stack_index=0;
943 return (&tape->pc_stack[tape->pc_stack_index++]);
947 * idetape_next_rq_storage is used along with idetape_next_pc_storage.
948 * Since we queue packet commands in the request queue, we need to
949 * allocate a request, along with the allocation of a packet command.
952 /**************************************************************
954 * This should get fixed to use kmalloc(.., GFP_ATOMIC) *
955 * followed later on by kfree(). -ml *
957 **************************************************************/
959 static struct request *idetape_next_rq_storage (ide_drive_t *drive)
961 idetape_tape_t *tape = drive->driver_data;
963 #if IDETAPE_DEBUG_LOG
964 if (tape->debug_level >= 5)
965 printk(KERN_INFO "ide-tape: rq_stack_index=%d\n",
966 tape->rq_stack_index);
967 #endif /* IDETAPE_DEBUG_LOG */
968 if (tape->rq_stack_index == IDETAPE_PC_STACK)
969 tape->rq_stack_index=0;
970 return (&tape->rq_stack[tape->rq_stack_index++]);
974 * idetape_init_pc initializes a packet command.
976 static void idetape_init_pc (idetape_pc_t *pc)
978 memset(pc->c, 0, 12);
981 pc->request_transfer = 0;
982 pc->buffer = pc->pc_buffer;
983 pc->buffer_size = IDETAPE_PC_BUFFER_SIZE;
989 * called on each failed packet command retry to analyze the request sense. We
990 * currently do not utilize this information.
992 static void idetape_analyze_error(ide_drive_t *drive, u8 *sense)
994 idetape_tape_t *tape = drive->driver_data;
995 idetape_pc_t *pc = tape->failed_pc;
997 tape->sense_key = sense[2] & 0xF;
998 tape->asc = sense[12];
999 tape->ascq = sense[13];
1000 #if IDETAPE_DEBUG_LOG
1002 * Without debugging, we only log an error if we decided to give up
1005 if (tape->debug_level >= 1)
1006 printk(KERN_INFO "ide-tape: pc = %x, sense key = %x, "
1007 "asc = %x, ascq = %x\n",
1008 pc->c[0], tape->sense_key,
1009 tape->asc, tape->ascq);
1010 #endif /* IDETAPE_DEBUG_LOG */
1012 /* Correct pc->actually_transferred by asking the tape. */
1013 if (test_bit(PC_DMA_ERROR, &pc->flags)) {
1014 pc->actually_transferred = pc->request_transfer -
1015 tape->tape_block_size *
1016 ntohl(get_unaligned((u32 *)&sense[3]));
1017 idetape_update_buffers(pc);
1021 * If error was the result of a zero-length read or write command,
1022 * with sense key=5, asc=0x22, ascq=0, let it slide. Some drives
1023 * (i.e. Seagate STT3401A Travan) don't support 0-length read/writes.
1025 if ((pc->c[0] == IDETAPE_READ_CMD || pc->c[0] == IDETAPE_WRITE_CMD)
1027 && pc->c[4] == 0 && pc->c[3] == 0 && pc->c[2] == 0) {
1028 if (tape->sense_key == 5) {
1029 /* don't report an error, everything's ok */
1031 /* don't retry read/write */
1032 set_bit(PC_ABORT, &pc->flags);
1035 if (pc->c[0] == IDETAPE_READ_CMD && (sense[2] & 0x80)) {
1036 pc->error = IDETAPE_ERROR_FILEMARK;
1037 set_bit(PC_ABORT, &pc->flags);
1039 if (pc->c[0] == IDETAPE_WRITE_CMD) {
1040 if ((sense[2] & 0x40) || (tape->sense_key == 0xd
1041 && tape->asc == 0x0 && tape->ascq == 0x2)) {
1042 pc->error = IDETAPE_ERROR_EOD;
1043 set_bit(PC_ABORT, &pc->flags);
1046 if (pc->c[0] == IDETAPE_READ_CMD || pc->c[0] == IDETAPE_WRITE_CMD) {
1047 if (tape->sense_key == 8) {
1048 pc->error = IDETAPE_ERROR_EOD;
1049 set_bit(PC_ABORT, &pc->flags);
1051 if (!test_bit(PC_ABORT, &pc->flags) &&
1052 pc->actually_transferred)
1053 pc->retries = IDETAPE_MAX_PC_RETRIES + 1;
1058 * idetape_active_next_stage will declare the next stage as "active".
1060 static void idetape_active_next_stage (ide_drive_t *drive)
1062 idetape_tape_t *tape = drive->driver_data;
1063 idetape_stage_t *stage = tape->next_stage;
1064 struct request *rq = &stage->rq;
1066 #if IDETAPE_DEBUG_LOG
1067 if (tape->debug_level >= 4)
1068 printk(KERN_INFO "ide-tape: Reached idetape_active_next_stage\n");
1069 #endif /* IDETAPE_DEBUG_LOG */
1070 #if IDETAPE_DEBUG_BUGS
1071 if (stage == NULL) {
1072 printk(KERN_ERR "ide-tape: bug: Trying to activate a non existing stage\n");
1075 #endif /* IDETAPE_DEBUG_BUGS */
1077 rq->rq_disk = tape->disk;
1079 rq->special = (void *)stage->bh;
1080 tape->active_data_request = rq;
1081 tape->active_stage = stage;
1082 tape->next_stage = stage->next;
1086 * idetape_increase_max_pipeline_stages is a part of the feedback
1087 * loop which tries to find the optimum number of stages. In the
1088 * feedback loop, we are starting from a minimum maximum number of
1089 * stages, and if we sense that the pipeline is empty, we try to
1090 * increase it, until we reach the user compile time memory limit.
1092 static void idetape_increase_max_pipeline_stages (ide_drive_t *drive)
1094 idetape_tape_t *tape = drive->driver_data;
1095 int increase = (tape->max_pipeline - tape->min_pipeline) / 10;
1097 #if IDETAPE_DEBUG_LOG
1098 if (tape->debug_level >= 4)
1099 printk (KERN_INFO "ide-tape: Reached idetape_increase_max_pipeline_stages\n");
1100 #endif /* IDETAPE_DEBUG_LOG */
1102 tape->max_stages += max(increase, 1);
1103 tape->max_stages = max(tape->max_stages, tape->min_pipeline);
1104 tape->max_stages = min(tape->max_stages, tape->max_pipeline);
1108 * idetape_kfree_stage calls kfree to completely free a stage, along with
1109 * its related buffers.
1111 static void __idetape_kfree_stage (idetape_stage_t *stage)
1113 struct idetape_bh *prev_bh, *bh = stage->bh;
1116 while (bh != NULL) {
1117 if (bh->b_data != NULL) {
1118 size = (int) bh->b_size;
1120 free_page((unsigned long) bh->b_data);
1122 bh->b_data += PAGE_SIZE;
1132 static void idetape_kfree_stage (idetape_tape_t *tape, idetape_stage_t *stage)
1134 __idetape_kfree_stage(stage);
1138 * idetape_remove_stage_head removes tape->first_stage from the pipeline.
1139 * The caller should avoid race conditions.
1141 static void idetape_remove_stage_head (ide_drive_t *drive)
1143 idetape_tape_t *tape = drive->driver_data;
1144 idetape_stage_t *stage;
1146 #if IDETAPE_DEBUG_LOG
1147 if (tape->debug_level >= 4)
1148 printk(KERN_INFO "ide-tape: Reached idetape_remove_stage_head\n");
1149 #endif /* IDETAPE_DEBUG_LOG */
1150 #if IDETAPE_DEBUG_BUGS
1151 if (tape->first_stage == NULL) {
1152 printk(KERN_ERR "ide-tape: bug: tape->first_stage is NULL\n");
1155 if (tape->active_stage == tape->first_stage) {
1156 printk(KERN_ERR "ide-tape: bug: Trying to free our active pipeline stage\n");
1159 #endif /* IDETAPE_DEBUG_BUGS */
1160 stage = tape->first_stage;
1161 tape->first_stage = stage->next;
1162 idetape_kfree_stage(tape, stage);
1164 if (tape->first_stage == NULL) {
1165 tape->last_stage = NULL;
1166 #if IDETAPE_DEBUG_BUGS
1167 if (tape->next_stage != NULL)
1168 printk(KERN_ERR "ide-tape: bug: tape->next_stage != NULL\n");
1169 if (tape->nr_stages)
1170 printk(KERN_ERR "ide-tape: bug: nr_stages should be 0 now\n");
1171 #endif /* IDETAPE_DEBUG_BUGS */
1176 * This will free all the pipeline stages starting from new_last_stage->next
1177 * to the end of the list, and point tape->last_stage to new_last_stage.
1179 static void idetape_abort_pipeline(ide_drive_t *drive,
1180 idetape_stage_t *new_last_stage)
1182 idetape_tape_t *tape = drive->driver_data;
1183 idetape_stage_t *stage = new_last_stage->next;
1184 idetape_stage_t *nstage;
1186 #if IDETAPE_DEBUG_LOG
1187 if (tape->debug_level >= 4)
1188 printk(KERN_INFO "ide-tape: %s: idetape_abort_pipeline called\n", tape->name);
1191 nstage = stage->next;
1192 idetape_kfree_stage(tape, stage);
1194 --tape->nr_pending_stages;
1198 new_last_stage->next = NULL;
1199 tape->last_stage = new_last_stage;
1200 tape->next_stage = NULL;
1204 * idetape_end_request is used to finish servicing a request, and to
1205 * insert a pending pipeline request into the main device queue.
1207 static int idetape_end_request(ide_drive_t *drive, int uptodate, int nr_sects)
1209 struct request *rq = HWGROUP(drive)->rq;
1210 idetape_tape_t *tape = drive->driver_data;
1211 unsigned long flags;
1213 int remove_stage = 0;
1214 idetape_stage_t *active_stage;
1216 #if IDETAPE_DEBUG_LOG
1217 if (tape->debug_level >= 4)
1218 printk(KERN_INFO "ide-tape: Reached idetape_end_request\n");
1219 #endif /* IDETAPE_DEBUG_LOG */
1222 case 0: error = IDETAPE_ERROR_GENERAL; break;
1223 case 1: error = 0; break;
1224 default: error = uptodate;
1228 tape->failed_pc = NULL;
1230 if (!blk_special_request(rq)) {
1231 ide_end_request(drive, uptodate, nr_sects);
1235 spin_lock_irqsave(&tape->spinlock, flags);
1237 /* The request was a pipelined data transfer request */
1238 if (tape->active_data_request == rq) {
1239 active_stage = tape->active_stage;
1240 tape->active_stage = NULL;
1241 tape->active_data_request = NULL;
1242 tape->nr_pending_stages--;
1243 if (rq->cmd[0] & REQ_IDETAPE_WRITE) {
1246 set_bit(IDETAPE_PIPELINE_ERROR, &tape->flags);
1247 if (error == IDETAPE_ERROR_EOD)
1248 idetape_abort_pipeline(drive, active_stage);
1250 } else if (rq->cmd[0] & REQ_IDETAPE_READ) {
1251 if (error == IDETAPE_ERROR_EOD) {
1252 set_bit(IDETAPE_PIPELINE_ERROR, &tape->flags);
1253 idetape_abort_pipeline(drive, active_stage);
1256 if (tape->next_stage != NULL) {
1257 idetape_active_next_stage(drive);
1260 * Insert the next request into the request queue.
1262 (void) ide_do_drive_cmd(drive, tape->active_data_request, ide_end);
1263 } else if (!error) {
1264 idetape_increase_max_pipeline_stages(drive);
1267 ide_end_drive_cmd(drive, 0, 0);
1268 // blkdev_dequeue_request(rq);
1269 // drive->rq = NULL;
1270 // end_that_request_last(rq);
1273 idetape_remove_stage_head(drive);
1274 if (tape->active_data_request == NULL)
1275 clear_bit(IDETAPE_PIPELINE_ACTIVE, &tape->flags);
1276 spin_unlock_irqrestore(&tape->spinlock, flags);
1280 static ide_startstop_t idetape_request_sense_callback (ide_drive_t *drive)
1282 idetape_tape_t *tape = drive->driver_data;
1284 #if IDETAPE_DEBUG_LOG
1285 if (tape->debug_level >= 4)
1286 printk(KERN_INFO "ide-tape: Reached idetape_request_sense_callback\n");
1287 #endif /* IDETAPE_DEBUG_LOG */
1288 if (!tape->pc->error) {
1289 idetape_analyze_error(drive, tape->pc->buffer);
1290 idetape_end_request(drive, 1, 0);
1292 printk(KERN_ERR "ide-tape: Error in REQUEST SENSE itself - Aborting request!\n");
1293 idetape_end_request(drive, 0, 0);
1298 static void idetape_create_request_sense_cmd (idetape_pc_t *pc)
1300 idetape_init_pc(pc);
1301 pc->c[0] = IDETAPE_REQUEST_SENSE_CMD;
1303 pc->request_transfer = 20;
1304 pc->callback = &idetape_request_sense_callback;
1307 static void idetape_init_rq(struct request *rq, u8 cmd)
1309 memset(rq, 0, sizeof(*rq));
1310 rq->cmd_type = REQ_TYPE_SPECIAL;
1315 * idetape_queue_pc_head generates a new packet command request in front
1316 * of the request queue, before the current request, so that it will be
1317 * processed immediately, on the next pass through the driver.
1319 * idetape_queue_pc_head is called from the request handling part of
1320 * the driver (the "bottom" part). Safe storage for the request should
1321 * be allocated with idetape_next_pc_storage and idetape_next_rq_storage
1322 * before calling idetape_queue_pc_head.
1324 * Memory for those requests is pre-allocated at initialization time, and
1325 * is limited to IDETAPE_PC_STACK requests. We assume that we have enough
1326 * space for the maximum possible number of inter-dependent packet commands.
1328 * The higher level of the driver - The ioctl handler and the character
1329 * device handling functions should queue request to the lower level part
1330 * and wait for their completion using idetape_queue_pc_tail or
1331 * idetape_queue_rw_tail.
1333 static void idetape_queue_pc_head (ide_drive_t *drive, idetape_pc_t *pc,struct request *rq)
1335 struct ide_tape_obj *tape = drive->driver_data;
1337 idetape_init_rq(rq, REQ_IDETAPE_PC1);
1338 rq->buffer = (char *) pc;
1339 rq->rq_disk = tape->disk;
1340 (void) ide_do_drive_cmd(drive, rq, ide_preempt);
1344 * idetape_retry_pc is called when an error was detected during the
1345 * last packet command. We queue a request sense packet command in
1346 * the head of the request list.
1348 static ide_startstop_t idetape_retry_pc (ide_drive_t *drive)
1350 idetape_tape_t *tape = drive->driver_data;
1354 (void)drive->hwif->INB(IDE_ERROR_REG);
1355 pc = idetape_next_pc_storage(drive);
1356 rq = idetape_next_rq_storage(drive);
1357 idetape_create_request_sense_cmd(pc);
1358 set_bit(IDETAPE_IGNORE_DSC, &tape->flags);
1359 idetape_queue_pc_head(drive, pc, rq);
1364 * idetape_postpone_request postpones the current request so that
1365 * ide.c will be able to service requests from another device on
1366 * the same hwgroup while we are polling for DSC.
1368 static void idetape_postpone_request (ide_drive_t *drive)
1370 idetape_tape_t *tape = drive->driver_data;
1372 #if IDETAPE_DEBUG_LOG
1373 if (tape->debug_level >= 4)
1374 printk(KERN_INFO "ide-tape: idetape_postpone_request\n");
1376 tape->postponed_rq = HWGROUP(drive)->rq;
1377 ide_stall_queue(drive, tape->dsc_polling_frequency);
1381 * idetape_pc_intr is the usual interrupt handler which will be called
1382 * during a packet command. We will transfer some of the data (as
1383 * requested by the drive) and will re-point interrupt handler to us.
1384 * When data transfer is finished, we will act according to the
1385 * algorithm described before idetape_issue_packet_command.
1388 static ide_startstop_t idetape_pc_intr (ide_drive_t *drive)
1390 ide_hwif_t *hwif = drive->hwif;
1391 idetape_tape_t *tape = drive->driver_data;
1392 idetape_pc_t *pc = tape->pc;
1395 static int error_sim_count = 0;
1400 #if IDETAPE_DEBUG_LOG
1401 if (tape->debug_level >= 4)
1402 printk(KERN_INFO "ide-tape: Reached idetape_pc_intr "
1403 "interrupt handler\n");
1404 #endif /* IDETAPE_DEBUG_LOG */
1406 /* Clear the interrupt */
1407 stat = hwif->INB(IDE_STATUS_REG);
1409 if (test_bit(PC_DMA_IN_PROGRESS, &pc->flags)) {
1410 if (hwif->ide_dma_end(drive) || (stat & ERR_STAT)) {
1412 * A DMA error is sometimes expected. For example,
1413 * if the tape is crossing a filemark during a
1414 * READ command, it will issue an irq and position
1415 * itself before the filemark, so that only a partial
1416 * data transfer will occur (which causes the DMA
1417 * error). In that case, we will later ask the tape
1418 * how much bytes of the original request were
1419 * actually transferred (we can't receive that
1420 * information from the DMA engine on most chipsets).
1424 * On the contrary, a DMA error is never expected;
1425 * it usually indicates a hardware error or abort.
1426 * If the tape crosses a filemark during a READ
1427 * command, it will issue an irq and position itself
1428 * after the filemark (not before). Only a partial
1429 * data transfer will occur, but no DMA error.
1432 set_bit(PC_DMA_ERROR, &pc->flags);
1434 pc->actually_transferred = pc->request_transfer;
1435 idetape_update_buffers(pc);
1437 #if IDETAPE_DEBUG_LOG
1438 if (tape->debug_level >= 4)
1439 printk(KERN_INFO "ide-tape: DMA finished\n");
1440 #endif /* IDETAPE_DEBUG_LOG */
1443 /* No more interrupts */
1444 if ((stat & DRQ_STAT) == 0) {
1445 #if IDETAPE_DEBUG_LOG
1446 if (tape->debug_level >= 2)
1447 printk(KERN_INFO "ide-tape: Packet command completed, %d bytes transferred\n", pc->actually_transferred);
1448 #endif /* IDETAPE_DEBUG_LOG */
1449 clear_bit(PC_DMA_IN_PROGRESS, &pc->flags);
1454 if ((pc->c[0] == IDETAPE_WRITE_CMD ||
1455 pc->c[0] == IDETAPE_READ_CMD) &&
1456 (++error_sim_count % 100) == 0) {
1457 printk(KERN_INFO "ide-tape: %s: simulating error\n",
1462 if ((stat & ERR_STAT) && pc->c[0] == IDETAPE_REQUEST_SENSE_CMD)
1464 if ((stat & ERR_STAT) || test_bit(PC_DMA_ERROR, &pc->flags)) {
1465 /* Error detected */
1466 #if IDETAPE_DEBUG_LOG
1467 if (tape->debug_level >= 1)
1468 printk(KERN_INFO "ide-tape: %s: I/O error\n",
1470 #endif /* IDETAPE_DEBUG_LOG */
1471 if (pc->c[0] == IDETAPE_REQUEST_SENSE_CMD) {
1472 printk(KERN_ERR "ide-tape: I/O error in request sense command\n");
1473 return ide_do_reset(drive);
1475 #if IDETAPE_DEBUG_LOG
1476 if (tape->debug_level >= 1)
1477 printk(KERN_INFO "ide-tape: [cmd %x]: check condition\n", pc->c[0]);
1479 /* Retry operation */
1480 return idetape_retry_pc(drive);
1483 if (test_bit(PC_WAIT_FOR_DSC, &pc->flags) &&
1484 (stat & SEEK_STAT) == 0) {
1485 /* Media access command */
1486 tape->dsc_polling_start = jiffies;
1487 tape->dsc_polling_frequency = IDETAPE_DSC_MA_FAST;
1488 tape->dsc_timeout = jiffies + IDETAPE_DSC_MA_TIMEOUT;
1489 /* Allow ide.c to handle other requests */
1490 idetape_postpone_request(drive);
1493 if (tape->failed_pc == pc)
1494 tape->failed_pc = NULL;
1495 /* Command finished - Call the callback function */
1496 return pc->callback(drive);
1498 if (test_and_clear_bit(PC_DMA_IN_PROGRESS, &pc->flags)) {
1499 printk(KERN_ERR "ide-tape: The tape wants to issue more "
1500 "interrupts in DMA mode\n");
1501 printk(KERN_ERR "ide-tape: DMA disabled, reverting to PIO\n");
1503 return ide_do_reset(drive);
1505 /* Get the number of bytes to transfer on this interrupt. */
1506 bcount = (hwif->INB(IDE_BCOUNTH_REG) << 8) |
1507 hwif->INB(IDE_BCOUNTL_REG);
1509 ireason = hwif->INB(IDE_IREASON_REG);
1512 printk(KERN_ERR "ide-tape: CoD != 0 in idetape_pc_intr\n");
1513 return ide_do_reset(drive);
1515 if (((ireason & IO) == IO) == test_bit(PC_WRITING, &pc->flags)) {
1516 /* Hopefully, we will never get here */
1517 printk(KERN_ERR "ide-tape: We wanted to %s, ",
1518 (ireason & IO) ? "Write" : "Read");
1519 printk(KERN_ERR "ide-tape: but the tape wants us to %s !\n",
1520 (ireason & IO) ? "Read" : "Write");
1521 return ide_do_reset(drive);
1523 if (!test_bit(PC_WRITING, &pc->flags)) {
1524 /* Reading - Check that we have enough space */
1525 temp = pc->actually_transferred + bcount;
1526 if (temp > pc->request_transfer) {
1527 if (temp > pc->buffer_size) {
1528 printk(KERN_ERR "ide-tape: The tape wants to send us more data than expected - discarding data\n");
1529 idetape_discard_data(drive, bcount);
1530 ide_set_handler(drive, &idetape_pc_intr, IDETAPE_WAIT_CMD, NULL);
1533 #if IDETAPE_DEBUG_LOG
1534 if (tape->debug_level >= 2)
1535 printk(KERN_NOTICE "ide-tape: The tape wants to send us more data than expected - allowing transfer\n");
1536 #endif /* IDETAPE_DEBUG_LOG */
1539 if (test_bit(PC_WRITING, &pc->flags)) {
1541 idetape_output_buffers(drive, pc, bcount);
1543 /* Write the current buffer */
1544 hwif->atapi_output_bytes(drive, pc->current_position,
1548 idetape_input_buffers(drive, pc, bcount);
1550 /* Read the current buffer */
1551 hwif->atapi_input_bytes(drive, pc->current_position,
1554 /* Update the current position */
1555 pc->actually_transferred += bcount;
1556 pc->current_position += bcount;
1557 #if IDETAPE_DEBUG_LOG
1558 if (tape->debug_level >= 2)
1559 printk(KERN_INFO "ide-tape: [cmd %x] transferred %d bytes "
1560 "on that interrupt\n", pc->c[0], bcount);
1562 /* And set the interrupt handler again */
1563 ide_set_handler(drive, &idetape_pc_intr, IDETAPE_WAIT_CMD, NULL);
1568 * Packet Command Interface
1570 * The current Packet Command is available in tape->pc, and will not
1571 * change until we finish handling it. Each packet command is associated
1572 * with a callback function that will be called when the command is
1575 * The handling will be done in three stages:
1577 * 1. idetape_issue_packet_command will send the packet command to the
1578 * drive, and will set the interrupt handler to idetape_pc_intr.
1580 * 2. On each interrupt, idetape_pc_intr will be called. This step
1581 * will be repeated until the device signals us that no more
1582 * interrupts will be issued.
1584 * 3. ATAPI Tape media access commands have immediate status with a
1585 * delayed process. In case of a successful initiation of a
1586 * media access packet command, the DSC bit will be set when the
1587 * actual execution of the command is finished.
1588 * Since the tape drive will not issue an interrupt, we have to
1589 * poll for this event. In this case, we define the request as
1590 * "low priority request" by setting rq_status to
1591 * IDETAPE_RQ_POSTPONED, set a timer to poll for DSC and exit
1594 * ide.c will then give higher priority to requests which
1595 * originate from the other device, until will change rq_status
1598 * 4. When the packet command is finished, it will be checked for errors.
1600 * 5. In case an error was found, we queue a request sense packet
1601 * command in front of the request queue and retry the operation
1602 * up to IDETAPE_MAX_PC_RETRIES times.
1604 * 6. In case no error was found, or we decided to give up and not
1605 * to retry again, the callback function will be called and then
1606 * we will handle the next request.
1609 static ide_startstop_t idetape_transfer_pc(ide_drive_t *drive)
1611 ide_hwif_t *hwif = drive->hwif;
1612 idetape_tape_t *tape = drive->driver_data;
1613 idetape_pc_t *pc = tape->pc;
1615 ide_startstop_t startstop;
1618 if (ide_wait_stat(&startstop,drive,DRQ_STAT,BUSY_STAT,WAIT_READY)) {
1619 printk(KERN_ERR "ide-tape: Strange, packet command initiated yet DRQ isn't asserted\n");
1622 ireason = hwif->INB(IDE_IREASON_REG);
1623 while (retries-- && ((ireason & CD) == 0 || (ireason & IO))) {
1624 printk(KERN_ERR "ide-tape: (IO,CoD != (0,1) while issuing "
1625 "a packet command, retrying\n");
1627 ireason = hwif->INB(IDE_IREASON_REG);
1629 printk(KERN_ERR "ide-tape: (IO,CoD != (0,1) while "
1630 "issuing a packet command, ignoring\n");
1635 if ((ireason & CD) == 0 || (ireason & IO)) {
1636 printk(KERN_ERR "ide-tape: (IO,CoD) != (0,1) while issuing "
1637 "a packet command\n");
1638 return ide_do_reset(drive);
1640 /* Set the interrupt routine */
1641 ide_set_handler(drive, &idetape_pc_intr, IDETAPE_WAIT_CMD, NULL);
1642 #ifdef CONFIG_BLK_DEV_IDEDMA
1643 /* Begin DMA, if necessary */
1644 if (test_bit(PC_DMA_IN_PROGRESS, &pc->flags))
1645 hwif->dma_start(drive);
1647 /* Send the actual packet */
1648 HWIF(drive)->atapi_output_bytes(drive, pc->c, 12);
1652 static ide_startstop_t idetape_issue_packet_command (ide_drive_t *drive, idetape_pc_t *pc)
1654 ide_hwif_t *hwif = drive->hwif;
1655 idetape_tape_t *tape = drive->driver_data;
1659 #if IDETAPE_DEBUG_BUGS
1660 if (tape->pc->c[0] == IDETAPE_REQUEST_SENSE_CMD &&
1661 pc->c[0] == IDETAPE_REQUEST_SENSE_CMD) {
1662 printk(KERN_ERR "ide-tape: possible ide-tape.c bug - "
1663 "Two request sense in serial were issued\n");
1665 #endif /* IDETAPE_DEBUG_BUGS */
1667 if (tape->failed_pc == NULL && pc->c[0] != IDETAPE_REQUEST_SENSE_CMD)
1668 tape->failed_pc = pc;
1669 /* Set the current packet command */
1672 if (pc->retries > IDETAPE_MAX_PC_RETRIES ||
1673 test_bit(PC_ABORT, &pc->flags)) {
1675 * We will "abort" retrying a packet command in case
1676 * a legitimate error code was received (crossing a
1677 * filemark, or end of the media, for example).
1679 if (!test_bit(PC_ABORT, &pc->flags)) {
1680 if (!(pc->c[0] == IDETAPE_TEST_UNIT_READY_CMD &&
1681 tape->sense_key == 2 && tape->asc == 4 &&
1682 (tape->ascq == 1 || tape->ascq == 8))) {
1683 printk(KERN_ERR "ide-tape: %s: I/O error, "
1684 "pc = %2x, key = %2x, "
1685 "asc = %2x, ascq = %2x\n",
1686 tape->name, pc->c[0],
1687 tape->sense_key, tape->asc,
1691 pc->error = IDETAPE_ERROR_GENERAL;
1693 tape->failed_pc = NULL;
1694 return pc->callback(drive);
1696 #if IDETAPE_DEBUG_LOG
1697 if (tape->debug_level >= 2)
1698 printk(KERN_INFO "ide-tape: Retry number - %d, cmd = %02X\n", pc->retries, pc->c[0]);
1699 #endif /* IDETAPE_DEBUG_LOG */
1702 /* We haven't transferred any data yet */
1703 pc->actually_transferred = 0;
1704 pc->current_position = pc->buffer;
1705 /* Request to transfer the entire buffer at once */
1706 bcount = pc->request_transfer;
1708 if (test_and_clear_bit(PC_DMA_ERROR, &pc->flags)) {
1709 printk(KERN_WARNING "ide-tape: DMA disabled, "
1710 "reverting to PIO\n");
1713 if (test_bit(PC_DMA_RECOMMENDED, &pc->flags) && drive->using_dma)
1714 dma_ok = !hwif->dma_setup(drive);
1716 ide_pktcmd_tf_load(drive, IDE_TFLAG_NO_SELECT_MASK |
1717 IDE_TFLAG_OUT_DEVICE, bcount, dma_ok);
1719 if (dma_ok) /* Will begin DMA later */
1720 set_bit(PC_DMA_IN_PROGRESS, &pc->flags);
1721 if (test_bit(IDETAPE_DRQ_INTERRUPT, &tape->flags)) {
1722 ide_execute_command(drive, WIN_PACKETCMD, &idetape_transfer_pc,
1723 IDETAPE_WAIT_CMD, NULL);
1726 hwif->OUTB(WIN_PACKETCMD, IDE_COMMAND_REG);
1727 return idetape_transfer_pc(drive);
1732 * General packet command callback function.
1734 static ide_startstop_t idetape_pc_callback (ide_drive_t *drive)
1736 idetape_tape_t *tape = drive->driver_data;
1738 #if IDETAPE_DEBUG_LOG
1739 if (tape->debug_level >= 4)
1740 printk(KERN_INFO "ide-tape: Reached idetape_pc_callback\n");
1741 #endif /* IDETAPE_DEBUG_LOG */
1743 idetape_end_request(drive, tape->pc->error ? 0 : 1, 0);
1748 * A mode sense command is used to "sense" tape parameters.
1750 static void idetape_create_mode_sense_cmd (idetape_pc_t *pc, u8 page_code)
1752 idetape_init_pc(pc);
1753 pc->c[0] = IDETAPE_MODE_SENSE_CMD;
1754 if (page_code != IDETAPE_BLOCK_DESCRIPTOR)
1755 pc->c[1] = 8; /* DBD = 1 - Don't return block descriptors */
1756 pc->c[2] = page_code;
1758 * Changed pc->c[3] to 0 (255 will at best return unused info).
1760 * For SCSI this byte is defined as subpage instead of high byte
1761 * of length and some IDE drives seem to interpret it this way
1762 * and return an error when 255 is used.
1765 pc->c[4] = 255; /* (We will just discard data in that case) */
1766 if (page_code == IDETAPE_BLOCK_DESCRIPTOR)
1767 pc->request_transfer = 12;
1768 else if (page_code == IDETAPE_CAPABILITIES_PAGE)
1769 pc->request_transfer = 24;
1771 pc->request_transfer = 50;
1772 pc->callback = &idetape_pc_callback;
1775 static void calculate_speeds(ide_drive_t *drive)
1777 idetape_tape_t *tape = drive->driver_data;
1778 int full = 125, empty = 75;
1780 if (time_after(jiffies, tape->controlled_pipeline_head_time + 120 * HZ)) {
1781 tape->controlled_previous_pipeline_head = tape->controlled_last_pipeline_head;
1782 tape->controlled_previous_head_time = tape->controlled_pipeline_head_time;
1783 tape->controlled_last_pipeline_head = tape->pipeline_head;
1784 tape->controlled_pipeline_head_time = jiffies;
1786 if (time_after(jiffies, tape->controlled_pipeline_head_time + 60 * HZ))
1787 tape->controlled_pipeline_head_speed = (tape->pipeline_head - tape->controlled_last_pipeline_head) * 32 * HZ / (jiffies - tape->controlled_pipeline_head_time);
1788 else if (time_after(jiffies, tape->controlled_previous_head_time))
1789 tape->controlled_pipeline_head_speed = (tape->pipeline_head - tape->controlled_previous_pipeline_head) * 32 * HZ / (jiffies - tape->controlled_previous_head_time);
1791 if (tape->nr_pending_stages < tape->max_stages /*- 1 */) {
1792 /* -1 for read mode error recovery */
1793 if (time_after(jiffies, tape->uncontrolled_previous_head_time + 10 * HZ)) {
1794 tape->uncontrolled_pipeline_head_time = jiffies;
1795 tape->uncontrolled_pipeline_head_speed = (tape->pipeline_head - tape->uncontrolled_previous_pipeline_head) * 32 * HZ / (jiffies - tape->uncontrolled_previous_head_time);
1798 tape->uncontrolled_previous_head_time = jiffies;
1799 tape->uncontrolled_previous_pipeline_head = tape->pipeline_head;
1800 if (time_after(jiffies, tape->uncontrolled_pipeline_head_time + 30 * HZ)) {
1801 tape->uncontrolled_pipeline_head_time = jiffies;
1804 tape->pipeline_head_speed = max(tape->uncontrolled_pipeline_head_speed, tape->controlled_pipeline_head_speed);
1805 if (tape->speed_control == 0) {
1806 tape->max_insert_speed = 5000;
1807 } else if (tape->speed_control == 1) {
1808 if (tape->nr_pending_stages >= tape->max_stages / 2)
1809 tape->max_insert_speed = tape->pipeline_head_speed +
1810 (1100 - tape->pipeline_head_speed) * 2 * (tape->nr_pending_stages - tape->max_stages / 2) / tape->max_stages;
1812 tape->max_insert_speed = 500 +
1813 (tape->pipeline_head_speed - 500) * 2 * tape->nr_pending_stages / tape->max_stages;
1814 if (tape->nr_pending_stages >= tape->max_stages * 99 / 100)
1815 tape->max_insert_speed = 5000;
1816 } else if (tape->speed_control == 2) {
1817 tape->max_insert_speed = tape->pipeline_head_speed * empty / 100 +
1818 (tape->pipeline_head_speed * full / 100 - tape->pipeline_head_speed * empty / 100) * tape->nr_pending_stages / tape->max_stages;
1820 tape->max_insert_speed = tape->speed_control;
1821 tape->max_insert_speed = max(tape->max_insert_speed, 500);
1824 static ide_startstop_t idetape_media_access_finished (ide_drive_t *drive)
1826 idetape_tape_t *tape = drive->driver_data;
1827 idetape_pc_t *pc = tape->pc;
1830 stat = drive->hwif->INB(IDE_STATUS_REG);
1831 if (stat & SEEK_STAT) {
1832 if (stat & ERR_STAT) {
1833 /* Error detected */
1834 if (pc->c[0] != IDETAPE_TEST_UNIT_READY_CMD)
1835 printk(KERN_ERR "ide-tape: %s: I/O error, ",
1837 /* Retry operation */
1838 return idetape_retry_pc(drive);
1841 if (tape->failed_pc == pc)
1842 tape->failed_pc = NULL;
1844 pc->error = IDETAPE_ERROR_GENERAL;
1845 tape->failed_pc = NULL;
1847 return pc->callback(drive);
1850 static ide_startstop_t idetape_rw_callback (ide_drive_t *drive)
1852 idetape_tape_t *tape = drive->driver_data;
1853 struct request *rq = HWGROUP(drive)->rq;
1854 int blocks = tape->pc->actually_transferred / tape->tape_block_size;
1856 tape->avg_size += blocks * tape->tape_block_size;
1857 tape->insert_size += blocks * tape->tape_block_size;
1858 if (tape->insert_size > 1024 * 1024)
1859 tape->measure_insert_time = 1;
1860 if (tape->measure_insert_time) {
1861 tape->measure_insert_time = 0;
1862 tape->insert_time = jiffies;
1863 tape->insert_size = 0;
1865 if (time_after(jiffies, tape->insert_time))
1866 tape->insert_speed = tape->insert_size / 1024 * HZ / (jiffies - tape->insert_time);
1867 if (time_after_eq(jiffies, tape->avg_time + HZ)) {
1868 tape->avg_speed = tape->avg_size * HZ / (jiffies - tape->avg_time) / 1024;
1870 tape->avg_time = jiffies;
1873 #if IDETAPE_DEBUG_LOG
1874 if (tape->debug_level >= 4)
1875 printk(KERN_INFO "ide-tape: Reached idetape_rw_callback\n");
1876 #endif /* IDETAPE_DEBUG_LOG */
1878 tape->first_frame_position += blocks;
1879 rq->current_nr_sectors -= blocks;
1881 if (!tape->pc->error)
1882 idetape_end_request(drive, 1, 0);
1884 idetape_end_request(drive, tape->pc->error, 0);
1888 static void idetape_create_read_cmd(idetape_tape_t *tape, idetape_pc_t *pc, unsigned int length, struct idetape_bh *bh)
1890 idetape_init_pc(pc);
1891 pc->c[0] = IDETAPE_READ_CMD;
1892 put_unaligned(htonl(length), (unsigned int *) &pc->c[1]);
1894 pc->callback = &idetape_rw_callback;
1896 atomic_set(&bh->b_count, 0);
1898 pc->request_transfer = pc->buffer_size = length * tape->tape_block_size;
1899 if (pc->request_transfer == tape->stage_size)
1900 set_bit(PC_DMA_RECOMMENDED, &pc->flags);
1903 static void idetape_create_read_buffer_cmd(idetape_tape_t *tape, idetape_pc_t *pc, unsigned int length, struct idetape_bh *bh)
1906 struct idetape_bh *p = bh;
1908 idetape_init_pc(pc);
1909 pc->c[0] = IDETAPE_READ_BUFFER_CMD;
1910 pc->c[1] = IDETAPE_RETRIEVE_FAULTY_BLOCK;
1911 pc->c[7] = size >> 8;
1912 pc->c[8] = size & 0xff;
1913 pc->callback = &idetape_pc_callback;
1915 atomic_set(&bh->b_count, 0);
1918 atomic_set(&p->b_count, 0);
1921 pc->request_transfer = pc->buffer_size = size;
1924 static void idetape_create_write_cmd(idetape_tape_t *tape, idetape_pc_t *pc, unsigned int length, struct idetape_bh *bh)
1926 idetape_init_pc(pc);
1927 pc->c[0] = IDETAPE_WRITE_CMD;
1928 put_unaligned(htonl(length), (unsigned int *) &pc->c[1]);
1930 pc->callback = &idetape_rw_callback;
1931 set_bit(PC_WRITING, &pc->flags);
1933 pc->b_data = bh->b_data;
1934 pc->b_count = atomic_read(&bh->b_count);
1936 pc->request_transfer = pc->buffer_size = length * tape->tape_block_size;
1937 if (pc->request_transfer == tape->stage_size)
1938 set_bit(PC_DMA_RECOMMENDED, &pc->flags);
1942 * idetape_do_request is our request handling function.
1944 static ide_startstop_t idetape_do_request(ide_drive_t *drive,
1945 struct request *rq, sector_t block)
1947 idetape_tape_t *tape = drive->driver_data;
1948 idetape_pc_t *pc = NULL;
1949 struct request *postponed_rq = tape->postponed_rq;
1952 #if IDETAPE_DEBUG_LOG
1953 if (tape->debug_level >= 2)
1954 printk(KERN_INFO "ide-tape: sector: %ld, "
1955 "nr_sectors: %ld, current_nr_sectors: %d\n",
1956 rq->sector, rq->nr_sectors, rq->current_nr_sectors);
1957 #endif /* IDETAPE_DEBUG_LOG */
1959 if (!blk_special_request(rq)) {
1961 * We do not support buffer cache originated requests.
1963 printk(KERN_NOTICE "ide-tape: %s: Unsupported request in "
1964 "request queue (%d)\n", drive->name, rq->cmd_type);
1965 ide_end_request(drive, 0, 0);
1970 * Retry a failed packet command
1972 if (tape->failed_pc != NULL &&
1973 tape->pc->c[0] == IDETAPE_REQUEST_SENSE_CMD) {
1974 return idetape_issue_packet_command(drive, tape->failed_pc);
1976 #if IDETAPE_DEBUG_BUGS
1977 if (postponed_rq != NULL)
1978 if (rq != postponed_rq) {
1979 printk(KERN_ERR "ide-tape: ide-tape.c bug - "
1980 "Two DSC requests were queued\n");
1981 idetape_end_request(drive, 0, 0);
1984 #endif /* IDETAPE_DEBUG_BUGS */
1986 tape->postponed_rq = NULL;
1989 * If the tape is still busy, postpone our request and service
1990 * the other device meanwhile.
1992 stat = drive->hwif->INB(IDE_STATUS_REG);
1994 if (!drive->dsc_overlap && !(rq->cmd[0] & REQ_IDETAPE_PC2))
1995 set_bit(IDETAPE_IGNORE_DSC, &tape->flags);
1997 if (drive->post_reset == 1) {
1998 set_bit(IDETAPE_IGNORE_DSC, &tape->flags);
1999 drive->post_reset = 0;
2002 if (tape->tape_still_time > 100 && tape->tape_still_time < 200)
2003 tape->measure_insert_time = 1;
2004 if (time_after(jiffies, tape->insert_time))
2005 tape->insert_speed = tape->insert_size / 1024 * HZ / (jiffies - tape->insert_time);
2006 calculate_speeds(drive);
2007 if (!test_and_clear_bit(IDETAPE_IGNORE_DSC, &tape->flags) &&
2008 (stat & SEEK_STAT) == 0) {
2009 if (postponed_rq == NULL) {
2010 tape->dsc_polling_start = jiffies;
2011 tape->dsc_polling_frequency = tape->best_dsc_rw_frequency;
2012 tape->dsc_timeout = jiffies + IDETAPE_DSC_RW_TIMEOUT;
2013 } else if (time_after(jiffies, tape->dsc_timeout)) {
2014 printk(KERN_ERR "ide-tape: %s: DSC timeout\n",
2016 if (rq->cmd[0] & REQ_IDETAPE_PC2) {
2017 idetape_media_access_finished(drive);
2020 return ide_do_reset(drive);
2022 } else if (time_after(jiffies, tape->dsc_polling_start + IDETAPE_DSC_MA_THRESHOLD))
2023 tape->dsc_polling_frequency = IDETAPE_DSC_MA_SLOW;
2024 idetape_postpone_request(drive);
2027 if (rq->cmd[0] & REQ_IDETAPE_READ) {
2028 tape->buffer_head++;
2029 tape->postpone_cnt = 0;
2030 pc = idetape_next_pc_storage(drive);
2031 idetape_create_read_cmd(tape, pc, rq->current_nr_sectors, (struct idetape_bh *)rq->special);
2034 if (rq->cmd[0] & REQ_IDETAPE_WRITE) {
2035 tape->buffer_head++;
2036 tape->postpone_cnt = 0;
2037 pc = idetape_next_pc_storage(drive);
2038 idetape_create_write_cmd(tape, pc, rq->current_nr_sectors, (struct idetape_bh *)rq->special);
2041 if (rq->cmd[0] & REQ_IDETAPE_READ_BUFFER) {
2042 tape->postpone_cnt = 0;
2043 pc = idetape_next_pc_storage(drive);
2044 idetape_create_read_buffer_cmd(tape, pc, rq->current_nr_sectors, (struct idetape_bh *)rq->special);
2047 if (rq->cmd[0] & REQ_IDETAPE_PC1) {
2048 pc = (idetape_pc_t *) rq->buffer;
2049 rq->cmd[0] &= ~(REQ_IDETAPE_PC1);
2050 rq->cmd[0] |= REQ_IDETAPE_PC2;
2053 if (rq->cmd[0] & REQ_IDETAPE_PC2) {
2054 idetape_media_access_finished(drive);
2059 return idetape_issue_packet_command(drive, pc);
2063 * Pipeline related functions
2065 static inline int idetape_pipeline_active (idetape_tape_t *tape)
2069 rc1 = test_bit(IDETAPE_PIPELINE_ACTIVE, &tape->flags);
2070 rc2 = (tape->active_data_request != NULL);
2075 * idetape_kmalloc_stage uses __get_free_page to allocate a pipeline
2076 * stage, along with all the necessary small buffers which together make
2077 * a buffer of size tape->stage_size (or a bit more). We attempt to
2078 * combine sequential pages as much as possible.
2080 * Returns a pointer to the new allocated stage, or NULL if we
2081 * can't (or don't want to) allocate a stage.
2083 * Pipeline stages are optional and are used to increase performance.
2084 * If we can't allocate them, we'll manage without them.
2086 static idetape_stage_t *__idetape_kmalloc_stage (idetape_tape_t *tape, int full, int clear)
2088 idetape_stage_t *stage;
2089 struct idetape_bh *prev_bh, *bh;
2090 int pages = tape->pages_per_stage;
2091 char *b_data = NULL;
2093 if ((stage = kmalloc(sizeof (idetape_stage_t),GFP_KERNEL)) == NULL)
2097 bh = stage->bh = kmalloc(sizeof(struct idetape_bh), GFP_KERNEL);
2100 bh->b_reqnext = NULL;
2101 if ((bh->b_data = (char *) __get_free_page (GFP_KERNEL)) == NULL)
2104 memset(bh->b_data, 0, PAGE_SIZE);
2105 bh->b_size = PAGE_SIZE;
2106 atomic_set(&bh->b_count, full ? bh->b_size : 0);
2109 if ((b_data = (char *) __get_free_page (GFP_KERNEL)) == NULL)
2112 memset(b_data, 0, PAGE_SIZE);
2113 if (bh->b_data == b_data + PAGE_SIZE) {
2114 bh->b_size += PAGE_SIZE;
2115 bh->b_data -= PAGE_SIZE;
2117 atomic_add(PAGE_SIZE, &bh->b_count);
2120 if (b_data == bh->b_data + bh->b_size) {
2121 bh->b_size += PAGE_SIZE;
2123 atomic_add(PAGE_SIZE, &bh->b_count);
2127 if ((bh = kmalloc(sizeof(struct idetape_bh), GFP_KERNEL)) == NULL) {
2128 free_page((unsigned long) b_data);
2131 bh->b_reqnext = NULL;
2132 bh->b_data = b_data;
2133 bh->b_size = PAGE_SIZE;
2134 atomic_set(&bh->b_count, full ? bh->b_size : 0);
2135 prev_bh->b_reqnext = bh;
2137 bh->b_size -= tape->excess_bh_size;
2139 atomic_sub(tape->excess_bh_size, &bh->b_count);
2142 __idetape_kfree_stage(stage);
2146 static idetape_stage_t *idetape_kmalloc_stage (idetape_tape_t *tape)
2148 idetape_stage_t *cache_stage = tape->cache_stage;
2150 #if IDETAPE_DEBUG_LOG
2151 if (tape->debug_level >= 4)
2152 printk(KERN_INFO "ide-tape: Reached idetape_kmalloc_stage\n");
2153 #endif /* IDETAPE_DEBUG_LOG */
2155 if (tape->nr_stages >= tape->max_stages)
2157 if (cache_stage != NULL) {
2158 tape->cache_stage = NULL;
2161 return __idetape_kmalloc_stage(tape, 0, 0);
2164 static int idetape_copy_stage_from_user (idetape_tape_t *tape, idetape_stage_t *stage, const char __user *buf, int n)
2166 struct idetape_bh *bh = tape->bh;
2171 #if IDETAPE_DEBUG_BUGS
2173 printk(KERN_ERR "ide-tape: bh == NULL in "
2174 "idetape_copy_stage_from_user\n");
2177 #endif /* IDETAPE_DEBUG_BUGS */
2178 count = min((unsigned int)(bh->b_size - atomic_read(&bh->b_count)), (unsigned int)n);
2179 if (copy_from_user(bh->b_data + atomic_read(&bh->b_count), buf, count))
2182 atomic_add(count, &bh->b_count);
2184 if (atomic_read(&bh->b_count) == bh->b_size) {
2187 atomic_set(&bh->b_count, 0);
2194 static int idetape_copy_stage_to_user (idetape_tape_t *tape, char __user *buf, idetape_stage_t *stage, int n)
2196 struct idetape_bh *bh = tape->bh;
2201 #if IDETAPE_DEBUG_BUGS
2203 printk(KERN_ERR "ide-tape: bh == NULL in "
2204 "idetape_copy_stage_to_user\n");
2207 #endif /* IDETAPE_DEBUG_BUGS */
2208 count = min(tape->b_count, n);
2209 if (copy_to_user(buf, tape->b_data, count))
2212 tape->b_data += count;
2213 tape->b_count -= count;
2215 if (!tape->b_count) {
2216 tape->bh = bh = bh->b_reqnext;
2218 tape->b_data = bh->b_data;
2219 tape->b_count = atomic_read(&bh->b_count);
2226 static void idetape_init_merge_stage (idetape_tape_t *tape)
2228 struct idetape_bh *bh = tape->merge_stage->bh;
2231 if (tape->chrdev_direction == idetape_direction_write)
2232 atomic_set(&bh->b_count, 0);
2234 tape->b_data = bh->b_data;
2235 tape->b_count = atomic_read(&bh->b_count);
2239 static void idetape_switch_buffers (idetape_tape_t *tape, idetape_stage_t *stage)
2241 struct idetape_bh *tmp;
2244 stage->bh = tape->merge_stage->bh;
2245 tape->merge_stage->bh = tmp;
2246 idetape_init_merge_stage(tape);
2250 * idetape_add_stage_tail adds a new stage at the end of the pipeline.
2252 static void idetape_add_stage_tail (ide_drive_t *drive,idetape_stage_t *stage)
2254 idetape_tape_t *tape = drive->driver_data;
2255 unsigned long flags;
2257 #if IDETAPE_DEBUG_LOG
2258 if (tape->debug_level >= 4)
2259 printk (KERN_INFO "ide-tape: Reached idetape_add_stage_tail\n");
2260 #endif /* IDETAPE_DEBUG_LOG */
2261 spin_lock_irqsave(&tape->spinlock, flags);
2263 if (tape->last_stage != NULL)
2264 tape->last_stage->next=stage;
2266 tape->first_stage = tape->next_stage=stage;
2267 tape->last_stage = stage;
2268 if (tape->next_stage == NULL)
2269 tape->next_stage = tape->last_stage;
2271 tape->nr_pending_stages++;
2272 spin_unlock_irqrestore(&tape->spinlock, flags);
2276 * idetape_wait_for_request installs a completion in a pending request
2277 * and sleeps until it is serviced.
2279 * The caller should ensure that the request will not be serviced
2280 * before we install the completion (usually by disabling interrupts).
2282 static void idetape_wait_for_request (ide_drive_t *drive, struct request *rq)
2284 DECLARE_COMPLETION_ONSTACK(wait);
2285 idetape_tape_t *tape = drive->driver_data;
2287 #if IDETAPE_DEBUG_BUGS
2288 if (rq == NULL || !blk_special_request(rq)) {
2289 printk (KERN_ERR "ide-tape: bug: Trying to sleep on non-valid request\n");
2292 #endif /* IDETAPE_DEBUG_BUGS */
2293 rq->end_io_data = &wait;
2294 rq->end_io = blk_end_sync_rq;
2295 spin_unlock_irq(&tape->spinlock);
2296 wait_for_completion(&wait);
2297 /* The stage and its struct request have been deallocated */
2298 spin_lock_irq(&tape->spinlock);
2301 static ide_startstop_t idetape_read_position_callback (ide_drive_t *drive)
2303 idetape_tape_t *tape = drive->driver_data;
2304 idetape_read_position_result_t *result;
2306 #if IDETAPE_DEBUG_LOG
2307 if (tape->debug_level >= 4)
2308 printk(KERN_INFO "ide-tape: Reached idetape_read_position_callback\n");
2309 #endif /* IDETAPE_DEBUG_LOG */
2311 if (!tape->pc->error) {
2312 result = (idetape_read_position_result_t *) tape->pc->buffer;
2313 #if IDETAPE_DEBUG_LOG
2314 if (tape->debug_level >= 2)
2315 printk(KERN_INFO "ide-tape: BOP - %s\n",result->bop ? "Yes":"No");
2316 if (tape->debug_level >= 2)
2317 printk(KERN_INFO "ide-tape: EOP - %s\n",result->eop ? "Yes":"No");
2318 #endif /* IDETAPE_DEBUG_LOG */
2320 printk(KERN_INFO "ide-tape: Block location is unknown to the tape\n");
2321 clear_bit(IDETAPE_ADDRESS_VALID, &tape->flags);
2322 idetape_end_request(drive, 0, 0);
2324 #if IDETAPE_DEBUG_LOG
2325 if (tape->debug_level >= 2)
2326 printk(KERN_INFO "ide-tape: Block Location - %u\n", ntohl(result->first_block));
2327 #endif /* IDETAPE_DEBUG_LOG */
2328 tape->partition = result->partition;
2329 tape->first_frame_position = ntohl(result->first_block);
2330 tape->last_frame_position = ntohl(result->last_block);
2331 tape->blocks_in_buffer = result->blocks_in_buffer[2];
2332 set_bit(IDETAPE_ADDRESS_VALID, &tape->flags);
2333 idetape_end_request(drive, 1, 0);
2336 idetape_end_request(drive, 0, 0);
2342 * idetape_create_write_filemark_cmd will:
2344 * 1. Write a filemark if write_filemark=1.
2345 * 2. Flush the device buffers without writing a filemark
2346 * if write_filemark=0.
2349 static void idetape_create_write_filemark_cmd (ide_drive_t *drive, idetape_pc_t *pc,int write_filemark)
2351 idetape_init_pc(pc);
2352 pc->c[0] = IDETAPE_WRITE_FILEMARK_CMD;
2353 pc->c[4] = write_filemark;
2354 set_bit(PC_WAIT_FOR_DSC, &pc->flags);
2355 pc->callback = &idetape_pc_callback;
2358 static void idetape_create_test_unit_ready_cmd(idetape_pc_t *pc)
2360 idetape_init_pc(pc);
2361 pc->c[0] = IDETAPE_TEST_UNIT_READY_CMD;
2362 pc->callback = &idetape_pc_callback;
2366 * idetape_queue_pc_tail is based on the following functions:
2368 * ide_do_drive_cmd from ide.c
2369 * cdrom_queue_request and cdrom_queue_packet_command from ide-cd.c
2371 * We add a special packet command request to the tail of the request
2372 * queue, and wait for it to be serviced.
2374 * This is not to be called from within the request handling part
2375 * of the driver ! We allocate here data in the stack, and it is valid
2376 * until the request is finished. This is not the case for the bottom
2377 * part of the driver, where we are always leaving the functions to wait
2378 * for an interrupt or a timer event.
2380 * From the bottom part of the driver, we should allocate safe memory
2381 * using idetape_next_pc_storage and idetape_next_rq_storage, and add
2382 * the request to the request list without waiting for it to be serviced !
2383 * In that case, we usually use idetape_queue_pc_head.
2385 static int __idetape_queue_pc_tail (ide_drive_t *drive, idetape_pc_t *pc)
2387 struct ide_tape_obj *tape = drive->driver_data;
2390 idetape_init_rq(&rq, REQ_IDETAPE_PC1);
2391 rq.buffer = (char *) pc;
2392 rq.rq_disk = tape->disk;
2393 return ide_do_drive_cmd(drive, &rq, ide_wait);
2396 static void idetape_create_load_unload_cmd (ide_drive_t *drive, idetape_pc_t *pc,int cmd)
2398 idetape_init_pc(pc);
2399 pc->c[0] = IDETAPE_LOAD_UNLOAD_CMD;
2401 set_bit(PC_WAIT_FOR_DSC, &pc->flags);
2402 pc->callback = &idetape_pc_callback;
2405 static int idetape_wait_ready(ide_drive_t *drive, unsigned long timeout)
2407 idetape_tape_t *tape = drive->driver_data;
2409 int load_attempted = 0;
2412 * Wait for the tape to become ready
2414 set_bit(IDETAPE_MEDIUM_PRESENT, &tape->flags);
2416 while (time_before(jiffies, timeout)) {
2417 idetape_create_test_unit_ready_cmd(&pc);
2418 if (!__idetape_queue_pc_tail(drive, &pc))
2420 if ((tape->sense_key == 2 && tape->asc == 4 && tape->ascq == 2)
2421 || (tape->asc == 0x3A)) { /* no media */
2424 idetape_create_load_unload_cmd(drive, &pc, IDETAPE_LU_LOAD_MASK);
2425 __idetape_queue_pc_tail(drive, &pc);
2427 /* not about to be ready */
2428 } else if (!(tape->sense_key == 2 && tape->asc == 4 &&
2429 (tape->ascq == 1 || tape->ascq == 8)))
2436 static int idetape_queue_pc_tail (ide_drive_t *drive,idetape_pc_t *pc)
2438 return __idetape_queue_pc_tail(drive, pc);
2441 static int idetape_flush_tape_buffers (ide_drive_t *drive)
2446 idetape_create_write_filemark_cmd(drive, &pc, 0);
2447 if ((rc = idetape_queue_pc_tail(drive, &pc)))
2449 idetape_wait_ready(drive, 60 * 5 * HZ);
2453 static void idetape_create_read_position_cmd (idetape_pc_t *pc)
2455 idetape_init_pc(pc);
2456 pc->c[0] = IDETAPE_READ_POSITION_CMD;
2457 pc->request_transfer = 20;
2458 pc->callback = &idetape_read_position_callback;
2461 static int idetape_read_position (ide_drive_t *drive)
2463 idetape_tape_t *tape = drive->driver_data;
2467 #if IDETAPE_DEBUG_LOG
2468 if (tape->debug_level >= 4)
2469 printk(KERN_INFO "ide-tape: Reached idetape_read_position\n");
2470 #endif /* IDETAPE_DEBUG_LOG */
2472 idetape_create_read_position_cmd(&pc);
2473 if (idetape_queue_pc_tail(drive, &pc))
2475 position = tape->first_frame_position;
2479 static void idetape_create_locate_cmd (ide_drive_t *drive, idetape_pc_t *pc, unsigned int block, u8 partition, int skip)
2481 idetape_init_pc(pc);
2482 pc->c[0] = IDETAPE_LOCATE_CMD;
2484 put_unaligned(htonl(block), (unsigned int *) &pc->c[3]);
2485 pc->c[8] = partition;
2486 set_bit(PC_WAIT_FOR_DSC, &pc->flags);
2487 pc->callback = &idetape_pc_callback;
2490 static int idetape_create_prevent_cmd (ide_drive_t *drive, idetape_pc_t *pc, int prevent)
2492 idetape_tape_t *tape = drive->driver_data;
2494 if (!tape->capabilities.lock)
2497 idetape_init_pc(pc);
2498 pc->c[0] = IDETAPE_PREVENT_CMD;
2500 pc->callback = &idetape_pc_callback;
2504 static int __idetape_discard_read_pipeline (ide_drive_t *drive)
2506 idetape_tape_t *tape = drive->driver_data;
2507 unsigned long flags;
2510 if (tape->chrdev_direction != idetape_direction_read)
2513 /* Remove merge stage. */
2514 cnt = tape->merge_stage_size / tape->tape_block_size;
2515 if (test_and_clear_bit(IDETAPE_FILEMARK, &tape->flags))
2516 ++cnt; /* Filemarks count as 1 sector */
2517 tape->merge_stage_size = 0;
2518 if (tape->merge_stage != NULL) {
2519 __idetape_kfree_stage(tape->merge_stage);
2520 tape->merge_stage = NULL;
2523 /* Clear pipeline flags. */
2524 clear_bit(IDETAPE_PIPELINE_ERROR, &tape->flags);
2525 tape->chrdev_direction = idetape_direction_none;
2527 /* Remove pipeline stages. */
2528 if (tape->first_stage == NULL)
2531 spin_lock_irqsave(&tape->spinlock, flags);
2532 tape->next_stage = NULL;
2533 if (idetape_pipeline_active(tape))
2534 idetape_wait_for_request(drive, tape->active_data_request);
2535 spin_unlock_irqrestore(&tape->spinlock, flags);
2537 while (tape->first_stage != NULL) {
2538 struct request *rq_ptr = &tape->first_stage->rq;
2540 cnt += rq_ptr->nr_sectors - rq_ptr->current_nr_sectors;
2541 if (rq_ptr->errors == IDETAPE_ERROR_FILEMARK)
2543 idetape_remove_stage_head(drive);
2545 tape->nr_pending_stages = 0;
2546 tape->max_stages = tape->min_pipeline;
2551 * idetape_position_tape positions the tape to the requested block
2552 * using the LOCATE packet command. A READ POSITION command is then
2553 * issued to check where we are positioned.
2555 * Like all higher level operations, we queue the commands at the tail
2556 * of the request queue and wait for their completion.
2559 static int idetape_position_tape (ide_drive_t *drive, unsigned int block, u8 partition, int skip)
2561 idetape_tape_t *tape = drive->driver_data;
2565 if (tape->chrdev_direction == idetape_direction_read)
2566 __idetape_discard_read_pipeline(drive);
2567 idetape_wait_ready(drive, 60 * 5 * HZ);
2568 idetape_create_locate_cmd(drive, &pc, block, partition, skip);
2569 retval = idetape_queue_pc_tail(drive, &pc);
2573 idetape_create_read_position_cmd(&pc);
2574 return (idetape_queue_pc_tail(drive, &pc));
2577 static void idetape_discard_read_pipeline (ide_drive_t *drive, int restore_position)
2579 idetape_tape_t *tape = drive->driver_data;
2583 cnt = __idetape_discard_read_pipeline(drive);
2584 if (restore_position) {
2585 position = idetape_read_position(drive);
2586 seek = position > cnt ? position - cnt : 0;
2587 if (idetape_position_tape(drive, seek, 0, 0)) {
2588 printk(KERN_INFO "ide-tape: %s: position_tape failed in discard_pipeline()\n", tape->name);
2595 * idetape_queue_rw_tail generates a read/write request for the block
2596 * device interface and wait for it to be serviced.
2598 static int idetape_queue_rw_tail(ide_drive_t *drive, int cmd, int blocks, struct idetape_bh *bh)
2600 idetape_tape_t *tape = drive->driver_data;
2603 #if IDETAPE_DEBUG_LOG
2604 if (tape->debug_level >= 2)
2605 printk(KERN_INFO "ide-tape: idetape_queue_rw_tail: cmd=%d\n",cmd);
2606 #endif /* IDETAPE_DEBUG_LOG */
2607 #if IDETAPE_DEBUG_BUGS
2608 if (idetape_pipeline_active(tape)) {
2609 printk(KERN_ERR "ide-tape: bug: the pipeline is active in idetape_queue_rw_tail\n");
2612 #endif /* IDETAPE_DEBUG_BUGS */
2614 idetape_init_rq(&rq, cmd);
2615 rq.rq_disk = tape->disk;
2616 rq.special = (void *)bh;
2617 rq.sector = tape->first_frame_position;
2618 rq.nr_sectors = rq.current_nr_sectors = blocks;
2619 (void) ide_do_drive_cmd(drive, &rq, ide_wait);
2621 if ((cmd & (REQ_IDETAPE_READ | REQ_IDETAPE_WRITE)) == 0)
2624 if (tape->merge_stage)
2625 idetape_init_merge_stage(tape);
2626 if (rq.errors == IDETAPE_ERROR_GENERAL)
2628 return (tape->tape_block_size * (blocks-rq.current_nr_sectors));
2632 * idetape_insert_pipeline_into_queue is used to start servicing the
2633 * pipeline stages, starting from tape->next_stage.
2635 static void idetape_insert_pipeline_into_queue (ide_drive_t *drive)
2637 idetape_tape_t *tape = drive->driver_data;
2639 if (tape->next_stage == NULL)
2641 if (!idetape_pipeline_active(tape)) {
2642 set_bit(IDETAPE_PIPELINE_ACTIVE, &tape->flags);
2643 idetape_active_next_stage(drive);
2644 (void) ide_do_drive_cmd(drive, tape->active_data_request, ide_end);
2648 static void idetape_create_inquiry_cmd (idetape_pc_t *pc)
2650 idetape_init_pc(pc);
2651 pc->c[0] = IDETAPE_INQUIRY_CMD;
2652 pc->c[4] = pc->request_transfer = 254;
2653 pc->callback = &idetape_pc_callback;
2656 static void idetape_create_rewind_cmd (ide_drive_t *drive, idetape_pc_t *pc)
2658 idetape_init_pc(pc);
2659 pc->c[0] = IDETAPE_REWIND_CMD;
2660 set_bit(PC_WAIT_FOR_DSC, &pc->flags);
2661 pc->callback = &idetape_pc_callback;
2664 static void idetape_create_erase_cmd (idetape_pc_t *pc)
2666 idetape_init_pc(pc);
2667 pc->c[0] = IDETAPE_ERASE_CMD;
2669 set_bit(PC_WAIT_FOR_DSC, &pc->flags);
2670 pc->callback = &idetape_pc_callback;
2673 static void idetape_create_space_cmd (idetape_pc_t *pc,int count, u8 cmd)
2675 idetape_init_pc(pc);
2676 pc->c[0] = IDETAPE_SPACE_CMD;
2677 put_unaligned(htonl(count), (unsigned int *) &pc->c[1]);
2679 set_bit(PC_WAIT_FOR_DSC, &pc->flags);
2680 pc->callback = &idetape_pc_callback;
2683 static void idetape_wait_first_stage (ide_drive_t *drive)
2685 idetape_tape_t *tape = drive->driver_data;
2686 unsigned long flags;
2688 if (tape->first_stage == NULL)
2690 spin_lock_irqsave(&tape->spinlock, flags);
2691 if (tape->active_stage == tape->first_stage)
2692 idetape_wait_for_request(drive, tape->active_data_request);
2693 spin_unlock_irqrestore(&tape->spinlock, flags);
2697 * idetape_add_chrdev_write_request tries to add a character device
2698 * originated write request to our pipeline. In case we don't succeed,
2699 * we revert to non-pipelined operation mode for this request.
2701 * 1. Try to allocate a new pipeline stage.
2702 * 2. If we can't, wait for more and more requests to be serviced
2703 * and try again each time.
2704 * 3. If we still can't allocate a stage, fallback to
2705 * non-pipelined operation mode for this request.
2707 static int idetape_add_chrdev_write_request (ide_drive_t *drive, int blocks)
2709 idetape_tape_t *tape = drive->driver_data;
2710 idetape_stage_t *new_stage;
2711 unsigned long flags;
2714 #if IDETAPE_DEBUG_LOG
2715 if (tape->debug_level >= 3)
2716 printk(KERN_INFO "ide-tape: Reached idetape_add_chrdev_write_request\n");
2717 #endif /* IDETAPE_DEBUG_LOG */
2720 * Attempt to allocate a new stage.
2721 * Pay special attention to possible race conditions.
2723 while ((new_stage = idetape_kmalloc_stage(tape)) == NULL) {
2724 spin_lock_irqsave(&tape->spinlock, flags);
2725 if (idetape_pipeline_active(tape)) {
2726 idetape_wait_for_request(drive, tape->active_data_request);
2727 spin_unlock_irqrestore(&tape->spinlock, flags);
2729 spin_unlock_irqrestore(&tape->spinlock, flags);
2730 idetape_insert_pipeline_into_queue(drive);
2731 if (idetape_pipeline_active(tape))
2734 * Linux is short on memory. Fallback to
2735 * non-pipelined operation mode for this request.
2737 return idetape_queue_rw_tail(drive, REQ_IDETAPE_WRITE, blocks, tape->merge_stage->bh);
2740 rq = &new_stage->rq;
2741 idetape_init_rq(rq, REQ_IDETAPE_WRITE);
2742 /* Doesn't actually matter - We always assume sequential access */
2743 rq->sector = tape->first_frame_position;
2744 rq->nr_sectors = rq->current_nr_sectors = blocks;
2746 idetape_switch_buffers(tape, new_stage);
2747 idetape_add_stage_tail(drive, new_stage);
2748 tape->pipeline_head++;
2749 calculate_speeds(drive);
2752 * Estimate whether the tape has stopped writing by checking
2753 * if our write pipeline is currently empty. If we are not
2754 * writing anymore, wait for the pipeline to be full enough
2755 * (90%) before starting to service requests, so that we will
2756 * be able to keep up with the higher speeds of the tape.
2758 if (!idetape_pipeline_active(tape)) {
2759 if (tape->nr_stages >= tape->max_stages * 9 / 10 ||
2760 tape->nr_stages >= tape->max_stages - tape->uncontrolled_pipeline_head_speed * 3 * 1024 / tape->tape_block_size) {
2761 tape->measure_insert_time = 1;
2762 tape->insert_time = jiffies;
2763 tape->insert_size = 0;
2764 tape->insert_speed = 0;
2765 idetape_insert_pipeline_into_queue(drive);
2768 if (test_and_clear_bit(IDETAPE_PIPELINE_ERROR, &tape->flags))
2769 /* Return a deferred error */
2775 * idetape_wait_for_pipeline will wait until all pending pipeline
2776 * requests are serviced. Typically called on device close.
2778 static void idetape_wait_for_pipeline (ide_drive_t *drive)
2780 idetape_tape_t *tape = drive->driver_data;
2781 unsigned long flags;
2783 while (tape->next_stage || idetape_pipeline_active(tape)) {
2784 idetape_insert_pipeline_into_queue(drive);
2785 spin_lock_irqsave(&tape->spinlock, flags);
2786 if (idetape_pipeline_active(tape))
2787 idetape_wait_for_request(drive, tape->active_data_request);
2788 spin_unlock_irqrestore(&tape->spinlock, flags);
2792 static void idetape_empty_write_pipeline (ide_drive_t *drive)
2794 idetape_tape_t *tape = drive->driver_data;
2796 struct idetape_bh *bh;
2798 #if IDETAPE_DEBUG_BUGS
2799 if (tape->chrdev_direction != idetape_direction_write) {
2800 printk(KERN_ERR "ide-tape: bug: Trying to empty write pipeline, but we are not writing.\n");
2803 if (tape->merge_stage_size > tape->stage_size) {
2804 printk(KERN_ERR "ide-tape: bug: merge_buffer too big\n");
2805 tape->merge_stage_size = tape->stage_size;
2807 #endif /* IDETAPE_DEBUG_BUGS */
2808 if (tape->merge_stage_size) {
2809 blocks = tape->merge_stage_size / tape->tape_block_size;
2810 if (tape->merge_stage_size % tape->tape_block_size) {
2814 i = tape->tape_block_size - tape->merge_stage_size % tape->tape_block_size;
2815 bh = tape->bh->b_reqnext;
2817 atomic_set(&bh->b_count, 0);
2824 printk(KERN_INFO "ide-tape: bug, bh NULL\n");
2827 min = min(i, (unsigned int)(bh->b_size - atomic_read(&bh->b_count)));
2828 memset(bh->b_data + atomic_read(&bh->b_count), 0, min);
2829 atomic_add(min, &bh->b_count);
2834 (void) idetape_add_chrdev_write_request(drive, blocks);
2835 tape->merge_stage_size = 0;
2837 idetape_wait_for_pipeline(drive);
2838 if (tape->merge_stage != NULL) {
2839 __idetape_kfree_stage(tape->merge_stage);
2840 tape->merge_stage = NULL;
2842 clear_bit(IDETAPE_PIPELINE_ERROR, &tape->flags);
2843 tape->chrdev_direction = idetape_direction_none;
2846 * On the next backup, perform the feedback loop again.
2847 * (I don't want to keep sense information between backups,
2848 * as some systems are constantly on, and the system load
2849 * can be totally different on the next backup).
2851 tape->max_stages = tape->min_pipeline;
2852 #if IDETAPE_DEBUG_BUGS
2853 if (tape->first_stage != NULL ||
2854 tape->next_stage != NULL ||
2855 tape->last_stage != NULL ||
2856 tape->nr_stages != 0) {
2857 printk(KERN_ERR "ide-tape: ide-tape pipeline bug, "
2858 "first_stage %p, next_stage %p, "
2859 "last_stage %p, nr_stages %d\n",
2860 tape->first_stage, tape->next_stage,
2861 tape->last_stage, tape->nr_stages);
2863 #endif /* IDETAPE_DEBUG_BUGS */
2866 static void idetape_restart_speed_control (ide_drive_t *drive)
2868 idetape_tape_t *tape = drive->driver_data;
2870 tape->restart_speed_control_req = 0;
2871 tape->pipeline_head = 0;
2872 tape->controlled_last_pipeline_head = tape->uncontrolled_last_pipeline_head = 0;
2873 tape->controlled_previous_pipeline_head = tape->uncontrolled_previous_pipeline_head = 0;
2874 tape->pipeline_head_speed = tape->controlled_pipeline_head_speed = 5000;
2875 tape->uncontrolled_pipeline_head_speed = 0;
2876 tape->controlled_pipeline_head_time = tape->uncontrolled_pipeline_head_time = jiffies;
2877 tape->controlled_previous_head_time = tape->uncontrolled_previous_head_time = jiffies;
2880 static int idetape_initiate_read (ide_drive_t *drive, int max_stages)
2882 idetape_tape_t *tape = drive->driver_data;
2883 idetape_stage_t *new_stage;
2886 int blocks = tape->capabilities.ctl;
2888 /* Initialize read operation */
2889 if (tape->chrdev_direction != idetape_direction_read) {
2890 if (tape->chrdev_direction == idetape_direction_write) {
2891 idetape_empty_write_pipeline(drive);
2892 idetape_flush_tape_buffers(drive);
2894 #if IDETAPE_DEBUG_BUGS
2895 if (tape->merge_stage || tape->merge_stage_size) {
2896 printk (KERN_ERR "ide-tape: merge_stage_size should be 0 now\n");
2897 tape->merge_stage_size = 0;
2899 #endif /* IDETAPE_DEBUG_BUGS */
2900 if ((tape->merge_stage = __idetape_kmalloc_stage(tape, 0, 0)) == NULL)
2902 tape->chrdev_direction = idetape_direction_read;
2905 * Issue a read 0 command to ensure that DSC handshake
2906 * is switched from completion mode to buffer available
2908 * No point in issuing this if DSC overlap isn't supported,
2909 * some drives (Seagate STT3401A) will return an error.
2911 if (drive->dsc_overlap) {
2912 bytes_read = idetape_queue_rw_tail(drive, REQ_IDETAPE_READ, 0, tape->merge_stage->bh);
2913 if (bytes_read < 0) {
2914 __idetape_kfree_stage(tape->merge_stage);
2915 tape->merge_stage = NULL;
2916 tape->chrdev_direction = idetape_direction_none;
2921 if (tape->restart_speed_control_req)
2922 idetape_restart_speed_control(drive);
2923 idetape_init_rq(&rq, REQ_IDETAPE_READ);
2924 rq.sector = tape->first_frame_position;
2925 rq.nr_sectors = rq.current_nr_sectors = blocks;
2926 if (!test_bit(IDETAPE_PIPELINE_ERROR, &tape->flags) &&
2927 tape->nr_stages < max_stages) {
2928 new_stage = idetape_kmalloc_stage(tape);
2929 while (new_stage != NULL) {
2931 idetape_add_stage_tail(drive, new_stage);
2932 if (tape->nr_stages >= max_stages)
2934 new_stage = idetape_kmalloc_stage(tape);
2937 if (!idetape_pipeline_active(tape)) {
2938 if (tape->nr_pending_stages >= 3 * max_stages / 4) {
2939 tape->measure_insert_time = 1;
2940 tape->insert_time = jiffies;
2941 tape->insert_size = 0;
2942 tape->insert_speed = 0;
2943 idetape_insert_pipeline_into_queue(drive);
2950 * idetape_add_chrdev_read_request is called from idetape_chrdev_read
2951 * to service a character device read request and add read-ahead
2952 * requests to our pipeline.
2954 static int idetape_add_chrdev_read_request (ide_drive_t *drive,int blocks)
2956 idetape_tape_t *tape = drive->driver_data;
2957 unsigned long flags;
2958 struct request *rq_ptr;
2961 #if IDETAPE_DEBUG_LOG
2962 if (tape->debug_level >= 4)
2963 printk(KERN_INFO "ide-tape: Reached idetape_add_chrdev_read_request, %d blocks\n", blocks);
2964 #endif /* IDETAPE_DEBUG_LOG */
2967 * If we are at a filemark, return a read length of 0
2969 if (test_bit(IDETAPE_FILEMARK, &tape->flags))
2973 * Wait for the next block to be available at the head
2976 idetape_initiate_read(drive, tape->max_stages);
2977 if (tape->first_stage == NULL) {
2978 if (test_bit(IDETAPE_PIPELINE_ERROR, &tape->flags))
2980 return idetape_queue_rw_tail(drive, REQ_IDETAPE_READ, blocks, tape->merge_stage->bh);
2982 idetape_wait_first_stage(drive);
2983 rq_ptr = &tape->first_stage->rq;
2984 bytes_read = tape->tape_block_size * (rq_ptr->nr_sectors - rq_ptr->current_nr_sectors);
2985 rq_ptr->nr_sectors = rq_ptr->current_nr_sectors = 0;
2988 if (rq_ptr->errors == IDETAPE_ERROR_EOD)
2991 idetape_switch_buffers(tape, tape->first_stage);
2992 if (rq_ptr->errors == IDETAPE_ERROR_FILEMARK)
2993 set_bit(IDETAPE_FILEMARK, &tape->flags);
2994 spin_lock_irqsave(&tape->spinlock, flags);
2995 idetape_remove_stage_head(drive);
2996 spin_unlock_irqrestore(&tape->spinlock, flags);
2997 tape->pipeline_head++;
2998 calculate_speeds(drive);
3000 #if IDETAPE_DEBUG_BUGS
3001 if (bytes_read > blocks * tape->tape_block_size) {
3002 printk(KERN_ERR "ide-tape: bug: trying to return more bytes than requested\n");
3003 bytes_read = blocks * tape->tape_block_size;
3005 #endif /* IDETAPE_DEBUG_BUGS */
3006 return (bytes_read);
3009 static void idetape_pad_zeros (ide_drive_t *drive, int bcount)
3011 idetape_tape_t *tape = drive->driver_data;
3012 struct idetape_bh *bh;
3018 bh = tape->merge_stage->bh;
3019 count = min(tape->stage_size, bcount);
3021 blocks = count / tape->tape_block_size;
3023 atomic_set(&bh->b_count, min(count, (unsigned int)bh->b_size));
3024 memset(bh->b_data, 0, atomic_read(&bh->b_count));
3025 count -= atomic_read(&bh->b_count);
3028 idetape_queue_rw_tail(drive, REQ_IDETAPE_WRITE, blocks, tape->merge_stage->bh);
3032 static int idetape_pipeline_size (ide_drive_t *drive)
3034 idetape_tape_t *tape = drive->driver_data;
3035 idetape_stage_t *stage;
3039 idetape_wait_for_pipeline(drive);
3040 stage = tape->first_stage;
3041 while (stage != NULL) {
3043 size += tape->tape_block_size * (rq->nr_sectors-rq->current_nr_sectors);
3044 if (rq->errors == IDETAPE_ERROR_FILEMARK)
3045 size += tape->tape_block_size;
3046 stage = stage->next;
3048 size += tape->merge_stage_size;
3053 * Rewinds the tape to the Beginning Of the current Partition (BOP).
3055 * We currently support only one partition.
3057 static int idetape_rewind_tape (ide_drive_t *drive)
3061 #if IDETAPE_DEBUG_LOG
3062 idetape_tape_t *tape = drive->driver_data;
3063 if (tape->debug_level >= 2)
3064 printk(KERN_INFO "ide-tape: Reached idetape_rewind_tape\n");
3065 #endif /* IDETAPE_DEBUG_LOG */
3067 idetape_create_rewind_cmd(drive, &pc);
3068 retval = idetape_queue_pc_tail(drive, &pc);
3072 idetape_create_read_position_cmd(&pc);
3073 retval = idetape_queue_pc_tail(drive, &pc);
3080 * Our special ide-tape ioctl's.
3082 * Currently there aren't any ioctl's.
3083 * mtio.h compatible commands should be issued to the character device
3086 static int idetape_blkdev_ioctl(ide_drive_t *drive, unsigned int cmd, unsigned long arg)
3088 idetape_tape_t *tape = drive->driver_data;
3089 idetape_config_t config;
3090 void __user *argp = (void __user *)arg;
3092 #if IDETAPE_DEBUG_LOG
3093 if (tape->debug_level >= 4)
3094 printk(KERN_INFO "ide-tape: Reached idetape_blkdev_ioctl\n");
3095 #endif /* IDETAPE_DEBUG_LOG */
3098 if (copy_from_user(&config, argp, sizeof (idetape_config_t)))
3100 tape->best_dsc_rw_frequency = config.dsc_rw_frequency;
3101 tape->max_stages = config.nr_stages;
3104 config.dsc_rw_frequency = (int) tape->best_dsc_rw_frequency;
3105 config.nr_stages = tape->max_stages;
3106 if (copy_to_user(argp, &config, sizeof (idetape_config_t)))
3116 * idetape_space_over_filemarks is now a bit more complicated than just
3117 * passing the command to the tape since we may have crossed some
3118 * filemarks during our pipelined read-ahead mode.
3120 * As a minor side effect, the pipeline enables us to support MTFSFM when
3121 * the filemark is in our internal pipeline even if the tape doesn't
3122 * support spacing over filemarks in the reverse direction.
3124 static int idetape_space_over_filemarks (ide_drive_t *drive,short mt_op,int mt_count)
3126 idetape_tape_t *tape = drive->driver_data;
3128 unsigned long flags;
3133 if (MTBSF == mt_op || MTBSFM == mt_op) {
3134 if (!tape->capabilities.sprev)
3136 mt_count = - mt_count;
3139 if (tape->chrdev_direction == idetape_direction_read) {
3141 * We have a read-ahead buffer. Scan it for crossed
3144 tape->merge_stage_size = 0;
3145 if (test_and_clear_bit(IDETAPE_FILEMARK, &tape->flags))
3147 while (tape->first_stage != NULL) {
3148 if (count == mt_count) {
3149 if (mt_op == MTFSFM)
3150 set_bit(IDETAPE_FILEMARK, &tape->flags);
3153 spin_lock_irqsave(&tape->spinlock, flags);
3154 if (tape->first_stage == tape->active_stage) {
3156 * We have reached the active stage in the read pipeline.
3157 * There is no point in allowing the drive to continue
3158 * reading any farther, so we stop the pipeline.
3160 * This section should be moved to a separate subroutine,
3161 * because a similar function is performed in
3162 * __idetape_discard_read_pipeline(), for example.
3164 tape->next_stage = NULL;
3165 spin_unlock_irqrestore(&tape->spinlock, flags);
3166 idetape_wait_first_stage(drive);
3167 tape->next_stage = tape->first_stage->next;
3169 spin_unlock_irqrestore(&tape->spinlock, flags);
3170 if (tape->first_stage->rq.errors == IDETAPE_ERROR_FILEMARK)
3172 idetape_remove_stage_head(drive);
3174 idetape_discard_read_pipeline(drive, 0);
3178 * The filemark was not found in our internal pipeline.
3179 * Now we can issue the space command.
3184 idetape_create_space_cmd(&pc,mt_count-count,IDETAPE_SPACE_OVER_FILEMARK);
3185 return (idetape_queue_pc_tail(drive, &pc));
3188 if (!tape->capabilities.sprev)
3190 retval = idetape_space_over_filemarks(drive, MTFSF, mt_count-count);
3191 if (retval) return (retval);
3192 count = (MTBSFM == mt_op ? 1 : -1);
3193 return (idetape_space_over_filemarks(drive, MTFSF, count));
3195 printk(KERN_ERR "ide-tape: MTIO operation %d not supported\n",mt_op);
3202 * Our character device read / write functions.
3204 * The tape is optimized to maximize throughput when it is transferring
3205 * an integral number of the "continuous transfer limit", which is
3206 * a parameter of the specific tape (26 KB on my particular tape).
3207 * (32 kB for Onstream)
3209 * As of version 1.3 of the driver, the character device provides an
3210 * abstract continuous view of the media - any mix of block sizes (even 1
3211 * byte) on the same backup/restore procedure is supported. The driver
3212 * will internally convert the requests to the recommended transfer unit,
3213 * so that an unmatch between the user's block size to the recommended
3214 * size will only result in a (slightly) increased driver overhead, but
3215 * will no longer hit performance.
3216 * This is not applicable to Onstream.
3218 static ssize_t idetape_chrdev_read (struct file *file, char __user *buf,
3219 size_t count, loff_t *ppos)
3221 struct ide_tape_obj *tape = ide_tape_f(file);
3222 ide_drive_t *drive = tape->drive;
3223 ssize_t bytes_read,temp, actually_read = 0, rc;
3226 #if IDETAPE_DEBUG_LOG
3227 if (tape->debug_level >= 3)
3228 printk(KERN_INFO "ide-tape: Reached idetape_chrdev_read, count %Zd\n", count);
3229 #endif /* IDETAPE_DEBUG_LOG */
3231 if (tape->chrdev_direction != idetape_direction_read) {
3232 if (test_bit(IDETAPE_DETECT_BS, &tape->flags))
3233 if (count > tape->tape_block_size &&
3234 (count % tape->tape_block_size) == 0)
3235 tape->user_bs_factor = count / tape->tape_block_size;
3237 if ((rc = idetape_initiate_read(drive, tape->max_stages)) < 0)
3241 if (tape->merge_stage_size) {
3242 actually_read = min((unsigned int)(tape->merge_stage_size), (unsigned int)count);
3243 if (idetape_copy_stage_to_user(tape, buf, tape->merge_stage, actually_read))
3245 buf += actually_read;
3246 tape->merge_stage_size -= actually_read;
3247 count -= actually_read;
3249 while (count >= tape->stage_size) {
3250 bytes_read = idetape_add_chrdev_read_request(drive, tape->capabilities.ctl);
3251 if (bytes_read <= 0)
3253 if (idetape_copy_stage_to_user(tape, buf, tape->merge_stage, bytes_read))
3256 count -= bytes_read;
3257 actually_read += bytes_read;
3260 bytes_read = idetape_add_chrdev_read_request(drive, tape->capabilities.ctl);
3261 if (bytes_read <= 0)
3263 temp = min((unsigned long)count, (unsigned long)bytes_read);
3264 if (idetape_copy_stage_to_user(tape, buf, tape->merge_stage, temp))
3266 actually_read += temp;
3267 tape->merge_stage_size = bytes_read-temp;
3270 if (!actually_read && test_bit(IDETAPE_FILEMARK, &tape->flags)) {
3271 #if IDETAPE_DEBUG_LOG
3272 if (tape->debug_level >= 2)
3273 printk(KERN_INFO "ide-tape: %s: spacing over filemark\n", tape->name);
3275 idetape_space_over_filemarks(drive, MTFSF, 1);
3279 return (ret) ? ret : actually_read;
3282 static ssize_t idetape_chrdev_write (struct file *file, const char __user *buf,
3283 size_t count, loff_t *ppos)
3285 struct ide_tape_obj *tape = ide_tape_f(file);
3286 ide_drive_t *drive = tape->drive;
3287 ssize_t actually_written = 0;
3290 /* The drive is write protected. */
3291 if (tape->write_prot)
3294 #if IDETAPE_DEBUG_LOG
3295 if (tape->debug_level >= 3)
3296 printk(KERN_INFO "ide-tape: Reached idetape_chrdev_write, "
3297 "count %Zd\n", count);
3298 #endif /* IDETAPE_DEBUG_LOG */
3300 /* Initialize write operation */
3301 if (tape->chrdev_direction != idetape_direction_write) {
3302 if (tape->chrdev_direction == idetape_direction_read)
3303 idetape_discard_read_pipeline(drive, 1);
3304 #if IDETAPE_DEBUG_BUGS
3305 if (tape->merge_stage || tape->merge_stage_size) {
3306 printk(KERN_ERR "ide-tape: merge_stage_size "
3307 "should be 0 now\n");
3308 tape->merge_stage_size = 0;
3310 #endif /* IDETAPE_DEBUG_BUGS */
3311 if ((tape->merge_stage = __idetape_kmalloc_stage(tape, 0, 0)) == NULL)
3313 tape->chrdev_direction = idetape_direction_write;
3314 idetape_init_merge_stage(tape);
3317 * Issue a write 0 command to ensure that DSC handshake
3318 * is switched from completion mode to buffer available
3320 * No point in issuing this if DSC overlap isn't supported,
3321 * some drives (Seagate STT3401A) will return an error.
3323 if (drive->dsc_overlap) {
3324 ssize_t retval = idetape_queue_rw_tail(drive, REQ_IDETAPE_WRITE, 0, tape->merge_stage->bh);
3326 __idetape_kfree_stage(tape->merge_stage);
3327 tape->merge_stage = NULL;
3328 tape->chrdev_direction = idetape_direction_none;
3335 if (tape->restart_speed_control_req)
3336 idetape_restart_speed_control(drive);
3337 if (tape->merge_stage_size) {
3338 #if IDETAPE_DEBUG_BUGS
3339 if (tape->merge_stage_size >= tape->stage_size) {
3340 printk(KERN_ERR "ide-tape: bug: merge buffer too big\n");
3341 tape->merge_stage_size = 0;
3343 #endif /* IDETAPE_DEBUG_BUGS */
3344 actually_written = min((unsigned int)(tape->stage_size - tape->merge_stage_size), (unsigned int)count);
3345 if (idetape_copy_stage_from_user(tape, tape->merge_stage, buf, actually_written))
3347 buf += actually_written;
3348 tape->merge_stage_size += actually_written;
3349 count -= actually_written;
3351 if (tape->merge_stage_size == tape->stage_size) {
3353 tape->merge_stage_size = 0;
3354 retval = idetape_add_chrdev_write_request(drive, tape->capabilities.ctl);
3359 while (count >= tape->stage_size) {
3361 if (idetape_copy_stage_from_user(tape, tape->merge_stage, buf, tape->stage_size))
3363 buf += tape->stage_size;
3364 count -= tape->stage_size;
3365 retval = idetape_add_chrdev_write_request(drive, tape->capabilities.ctl);
3366 actually_written += tape->stage_size;
3371 actually_written += count;
3372 if (idetape_copy_stage_from_user(tape, tape->merge_stage, buf, count))
3374 tape->merge_stage_size += count;
3376 return (ret) ? ret : actually_written;
3379 static int idetape_write_filemark (ide_drive_t *drive)
3383 /* Write a filemark */
3384 idetape_create_write_filemark_cmd(drive, &pc, 1);
3385 if (idetape_queue_pc_tail(drive, &pc)) {
3386 printk(KERN_ERR "ide-tape: Couldn't write a filemark\n");
3393 * idetape_mtioctop is called from idetape_chrdev_ioctl when
3394 * the general mtio MTIOCTOP ioctl is requested.
3396 * We currently support the following mtio.h operations:
3398 * MTFSF - Space over mt_count filemarks in the positive direction.
3399 * The tape is positioned after the last spaced filemark.
3401 * MTFSFM - Same as MTFSF, but the tape is positioned before the
3404 * MTBSF - Steps background over mt_count filemarks, tape is
3405 * positioned before the last filemark.
3407 * MTBSFM - Like MTBSF, only tape is positioned after the last filemark.
3411 * MTBSF and MTBSFM are not supported when the tape doesn't
3412 * support spacing over filemarks in the reverse direction.
3413 * In this case, MTFSFM is also usually not supported (it is
3414 * supported in the rare case in which we crossed the filemark
3415 * during our read-ahead pipelined operation mode).
3417 * MTWEOF - Writes mt_count filemarks. Tape is positioned after
3418 * the last written filemark.
3420 * MTREW - Rewinds tape.
3422 * MTLOAD - Loads the tape.
3424 * MTOFFL - Puts the tape drive "Offline": Rewinds the tape and
3425 * MTUNLOAD prevents further access until the media is replaced.
3427 * MTNOP - Flushes tape buffers.
3429 * MTRETEN - Retension media. This typically consists of one end
3430 * to end pass on the media.
3432 * MTEOM - Moves to the end of recorded data.
3434 * MTERASE - Erases tape.
3436 * MTSETBLK - Sets the user block size to mt_count bytes. If
3437 * mt_count is 0, we will attempt to autodetect
3440 * MTSEEK - Positions the tape in a specific block number, where
3441 * each block is assumed to contain which user_block_size
3444 * MTSETPART - Switches to another tape partition.
3446 * MTLOCK - Locks the tape door.
3448 * MTUNLOCK - Unlocks the tape door.
3450 * The following commands are currently not supported:
3452 * MTFSS, MTBSS, MTWSM, MTSETDENSITY,
3453 * MTSETDRVBUFFER, MT_ST_BOOLEANS, MT_ST_WRITE_THRESHOLD.
3455 static int idetape_mtioctop (ide_drive_t *drive,short mt_op,int mt_count)
3457 idetape_tape_t *tape = drive->driver_data;
3461 #if IDETAPE_DEBUG_LOG
3462 if (tape->debug_level >= 1)
3463 printk(KERN_INFO "ide-tape: Handling MTIOCTOP ioctl: "
3464 "mt_op=%d, mt_count=%d\n", mt_op, mt_count);
3465 #endif /* IDETAPE_DEBUG_LOG */
3467 * Commands which need our pipelined read-ahead stages.
3476 return (idetape_space_over_filemarks(drive,mt_op,mt_count));
3482 if (tape->write_prot)
3484 idetape_discard_read_pipeline(drive, 1);
3485 for (i = 0; i < mt_count; i++) {
3486 retval = idetape_write_filemark(drive);
3492 idetape_discard_read_pipeline(drive, 0);
3493 if (idetape_rewind_tape(drive))
3497 idetape_discard_read_pipeline(drive, 0);
3498 idetape_create_load_unload_cmd(drive, &pc, IDETAPE_LU_LOAD_MASK);
3499 return (idetape_queue_pc_tail(drive, &pc));
3503 * If door is locked, attempt to unlock before
3504 * attempting to eject.
3506 if (tape->door_locked) {
3507 if (idetape_create_prevent_cmd(drive, &pc, 0))
3508 if (!idetape_queue_pc_tail(drive, &pc))
3509 tape->door_locked = DOOR_UNLOCKED;
3511 idetape_discard_read_pipeline(drive, 0);
3512 idetape_create_load_unload_cmd(drive, &pc,!IDETAPE_LU_LOAD_MASK);
3513 retval = idetape_queue_pc_tail(drive, &pc);
3515 clear_bit(IDETAPE_MEDIUM_PRESENT, &tape->flags);
3518 idetape_discard_read_pipeline(drive, 0);
3519 return (idetape_flush_tape_buffers(drive));
3521 idetape_discard_read_pipeline(drive, 0);
3522 idetape_create_load_unload_cmd(drive, &pc,IDETAPE_LU_RETENSION_MASK | IDETAPE_LU_LOAD_MASK);
3523 return (idetape_queue_pc_tail(drive, &pc));
3525 idetape_create_space_cmd(&pc, 0, IDETAPE_SPACE_TO_EOD);
3526 return (idetape_queue_pc_tail(drive, &pc));
3528 (void) idetape_rewind_tape(drive);
3529 idetape_create_erase_cmd(&pc);
3530 return (idetape_queue_pc_tail(drive, &pc));
3533 if (mt_count < tape->tape_block_size || mt_count % tape->tape_block_size)
3535 tape->user_bs_factor = mt_count / tape->tape_block_size;
3536 clear_bit(IDETAPE_DETECT_BS, &tape->flags);
3538 set_bit(IDETAPE_DETECT_BS, &tape->flags);
3541 idetape_discard_read_pipeline(drive, 0);
3542 return idetape_position_tape(drive, mt_count * tape->user_bs_factor, tape->partition, 0);
3544 idetape_discard_read_pipeline(drive, 0);
3545 return (idetape_position_tape(drive, 0, mt_count, 0));
3549 if (!idetape_create_prevent_cmd(drive, &pc, 1))
3551 retval = idetape_queue_pc_tail(drive, &pc);
3552 if (retval) return retval;
3553 tape->door_locked = DOOR_EXPLICITLY_LOCKED;
3556 if (!idetape_create_prevent_cmd(drive, &pc, 0))
3558 retval = idetape_queue_pc_tail(drive, &pc);
3559 if (retval) return retval;
3560 tape->door_locked = DOOR_UNLOCKED;
3563 printk(KERN_ERR "ide-tape: MTIO operation %d not "
3564 "supported\n", mt_op);
3570 * Our character device ioctls.
3572 * General mtio.h magnetic io commands are supported here, and not in
3573 * the corresponding block interface.
3575 * The following ioctls are supported:
3577 * MTIOCTOP - Refer to idetape_mtioctop for detailed description.
3579 * MTIOCGET - The mt_dsreg field in the returned mtget structure
3580 * will be set to (user block size in bytes <<
3581 * MT_ST_BLKSIZE_SHIFT) & MT_ST_BLKSIZE_MASK.
3583 * The mt_blkno is set to the current user block number.
3584 * The other mtget fields are not supported.
3586 * MTIOCPOS - The current tape "block position" is returned. We
3587 * assume that each block contains user_block_size
3590 * Our own ide-tape ioctls are supported on both interfaces.
3592 static int idetape_chrdev_ioctl (struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg)
3594 struct ide_tape_obj *tape = ide_tape_f(file);
3595 ide_drive_t *drive = tape->drive;
3599 int block_offset = 0, position = tape->first_frame_position;
3600 void __user *argp = (void __user *)arg;
3602 #if IDETAPE_DEBUG_LOG
3603 if (tape->debug_level >= 3)
3604 printk(KERN_INFO "ide-tape: Reached idetape_chrdev_ioctl, "
3606 #endif /* IDETAPE_DEBUG_LOG */
3608 tape->restart_speed_control_req = 1;
3609 if (tape->chrdev_direction == idetape_direction_write) {
3610 idetape_empty_write_pipeline(drive);
3611 idetape_flush_tape_buffers(drive);
3613 if (cmd == MTIOCGET || cmd == MTIOCPOS) {
3614 block_offset = idetape_pipeline_size(drive) / (tape->tape_block_size * tape->user_bs_factor);
3615 if ((position = idetape_read_position(drive)) < 0)
3620 if (copy_from_user(&mtop, argp, sizeof (struct mtop)))
3622 return (idetape_mtioctop(drive,mtop.mt_op,mtop.mt_count));
3624 memset(&mtget, 0, sizeof (struct mtget));
3625 mtget.mt_type = MT_ISSCSI2;
3626 mtget.mt_blkno = position / tape->user_bs_factor - block_offset;
3627 mtget.mt_dsreg = ((tape->tape_block_size * tape->user_bs_factor) << MT_ST_BLKSIZE_SHIFT) & MT_ST_BLKSIZE_MASK;
3628 if (tape->drv_write_prot) {
3629 mtget.mt_gstat |= GMT_WR_PROT(0xffffffff);
3631 if (copy_to_user(argp, &mtget, sizeof(struct mtget)))
3635 mtpos.mt_blkno = position / tape->user_bs_factor - block_offset;
3636 if (copy_to_user(argp, &mtpos, sizeof(struct mtpos)))
3640 if (tape->chrdev_direction == idetape_direction_read)
3641 idetape_discard_read_pipeline(drive, 1);
3642 return idetape_blkdev_ioctl(drive, cmd, arg);
3646 static void idetape_get_blocksize_from_block_descriptor(ide_drive_t *drive);
3649 * Our character device open function.
3651 static int idetape_chrdev_open (struct inode *inode, struct file *filp)
3653 unsigned int minor = iminor(inode), i = minor & ~0xc0;
3655 idetape_tape_t *tape;
3660 * We really want to do nonseekable_open(inode, filp); here, but some
3661 * versions of tar incorrectly call lseek on tapes and bail out if that
3662 * fails. So we disallow pread() and pwrite(), but permit lseeks.
3664 filp->f_mode &= ~(FMODE_PREAD | FMODE_PWRITE);
3666 #if IDETAPE_DEBUG_LOG
3667 printk(KERN_INFO "ide-tape: Reached idetape_chrdev_open\n");
3668 #endif /* IDETAPE_DEBUG_LOG */
3670 if (i >= MAX_HWIFS * MAX_DRIVES)
3673 if (!(tape = ide_tape_chrdev_get(i)))
3676 drive = tape->drive;
3678 filp->private_data = tape;
3680 if (test_and_set_bit(IDETAPE_BUSY, &tape->flags)) {
3685 retval = idetape_wait_ready(drive, 60 * HZ);
3687 clear_bit(IDETAPE_BUSY, &tape->flags);
3688 printk(KERN_ERR "ide-tape: %s: drive not ready\n", tape->name);
3692 idetape_read_position(drive);
3693 if (!test_bit(IDETAPE_ADDRESS_VALID, &tape->flags))
3694 (void)idetape_rewind_tape(drive);
3696 if (tape->chrdev_direction != idetape_direction_read)
3697 clear_bit(IDETAPE_PIPELINE_ERROR, &tape->flags);
3699 /* Read block size and write protect status from drive. */
3700 idetape_get_blocksize_from_block_descriptor(drive);
3702 /* Set write protect flag if device is opened as read-only. */
3703 if ((filp->f_flags & O_ACCMODE) == O_RDONLY)
3704 tape->write_prot = 1;
3706 tape->write_prot = tape->drv_write_prot;
3708 /* Make sure drive isn't write protected if user wants to write. */
3709 if (tape->write_prot) {
3710 if ((filp->f_flags & O_ACCMODE) == O_WRONLY ||
3711 (filp->f_flags & O_ACCMODE) == O_RDWR) {
3712 clear_bit(IDETAPE_BUSY, &tape->flags);
3719 * Lock the tape drive door so user can't eject.
3721 if (tape->chrdev_direction == idetape_direction_none) {
3722 if (idetape_create_prevent_cmd(drive, &pc, 1)) {
3723 if (!idetape_queue_pc_tail(drive, &pc)) {
3724 if (tape->door_locked != DOOR_EXPLICITLY_LOCKED)
3725 tape->door_locked = DOOR_LOCKED;
3729 idetape_restart_speed_control(drive);
3730 tape->restart_speed_control_req = 0;
3738 static void idetape_write_release (ide_drive_t *drive, unsigned int minor)
3740 idetape_tape_t *tape = drive->driver_data;
3742 idetape_empty_write_pipeline(drive);
3743 tape->merge_stage = __idetape_kmalloc_stage(tape, 1, 0);
3744 if (tape->merge_stage != NULL) {
3745 idetape_pad_zeros(drive, tape->tape_block_size * (tape->user_bs_factor - 1));
3746 __idetape_kfree_stage(tape->merge_stage);
3747 tape->merge_stage = NULL;
3749 idetape_write_filemark(drive);
3750 idetape_flush_tape_buffers(drive);
3751 idetape_flush_tape_buffers(drive);
3755 * Our character device release function.
3757 static int idetape_chrdev_release (struct inode *inode, struct file *filp)
3759 struct ide_tape_obj *tape = ide_tape_f(filp);
3760 ide_drive_t *drive = tape->drive;
3762 unsigned int minor = iminor(inode);
3765 tape = drive->driver_data;
3766 #if IDETAPE_DEBUG_LOG
3767 if (tape->debug_level >= 3)
3768 printk(KERN_INFO "ide-tape: Reached idetape_chrdev_release\n");
3769 #endif /* IDETAPE_DEBUG_LOG */
3771 if (tape->chrdev_direction == idetape_direction_write)
3772 idetape_write_release(drive, minor);
3773 if (tape->chrdev_direction == idetape_direction_read) {
3775 idetape_discard_read_pipeline(drive, 1);
3777 idetape_wait_for_pipeline(drive);
3779 if (tape->cache_stage != NULL) {
3780 __idetape_kfree_stage(tape->cache_stage);
3781 tape->cache_stage = NULL;
3783 if (minor < 128 && test_bit(IDETAPE_MEDIUM_PRESENT, &tape->flags))
3784 (void) idetape_rewind_tape(drive);
3785 if (tape->chrdev_direction == idetape_direction_none) {
3786 if (tape->door_locked == DOOR_LOCKED) {
3787 if (idetape_create_prevent_cmd(drive, &pc, 0)) {
3788 if (!idetape_queue_pc_tail(drive, &pc))
3789 tape->door_locked = DOOR_UNLOCKED;
3793 clear_bit(IDETAPE_BUSY, &tape->flags);
3800 * idetape_identify_device is called to check the contents of the
3801 * ATAPI IDENTIFY command results. We return:
3803 * 1 If the tape can be supported by us, based on the information
3806 * 0 If this tape driver is not currently supported by us.
3808 static int idetape_identify_device (ide_drive_t *drive)
3810 struct idetape_id_gcw gcw;
3811 struct hd_driveid *id = drive->id;
3813 if (drive->id_read == 0)
3816 *((unsigned short *) &gcw) = id->config;
3818 #if IDETAPE_DEBUG_INFO
3819 printk(KERN_INFO "ide-tape: Dumping ATAPI Identify Device tape parameters\n");
3820 printk(KERN_INFO "ide-tape: Protocol Type: ");
3821 switch (gcw.protocol) {
3822 case 0: case 1: printk("ATA\n");break;
3823 case 2: printk("ATAPI\n");break;
3824 case 3: printk("Reserved (Unknown to ide-tape)\n");break;
3826 printk(KERN_INFO "ide-tape: Device Type: %x - ",gcw.device_type);
3827 switch (gcw.device_type) {
3828 case 0: printk("Direct-access Device\n");break;
3829 case 1: printk("Streaming Tape Device\n");break;
3830 case 2: case 3: case 4: printk("Reserved\n");break;
3831 case 5: printk("CD-ROM Device\n");break;
3832 case 6: printk("Reserved\n");
3833 case 7: printk("Optical memory Device\n");break;
3834 case 0x1f: printk("Unknown or no Device type\n");break;
3835 default: printk("Reserved\n");
3837 printk(KERN_INFO "ide-tape: Removable: %s",gcw.removable ? "Yes\n":"No\n");
3838 printk(KERN_INFO "ide-tape: Command Packet DRQ Type: ");
3839 switch (gcw.drq_type) {
3840 case 0: printk("Microprocessor DRQ\n");break;
3841 case 1: printk("Interrupt DRQ\n");break;
3842 case 2: printk("Accelerated DRQ\n");break;
3843 case 3: printk("Reserved\n");break;
3845 printk(KERN_INFO "ide-tape: Command Packet Size: ");
3846 switch (gcw.packet_size) {
3847 case 0: printk("12 bytes\n");break;
3848 case 1: printk("16 bytes\n");break;
3849 default: printk("Reserved\n");break;
3851 #endif /* IDETAPE_DEBUG_INFO */
3853 /* Check that we can support this device */
3855 if (gcw.protocol != 2)
3856 printk(KERN_ERR "ide-tape: Protocol (0x%02x) is not ATAPI\n",
3858 else if (gcw.device_type != 1)
3859 printk(KERN_ERR "ide-tape: Device type (0x%02x) is not set "
3860 "to tape\n", gcw.device_type);
3861 else if (!gcw.removable)
3862 printk(KERN_ERR "ide-tape: The removable flag is not set\n");
3863 else if (gcw.packet_size != 0) {
3864 printk(KERN_ERR "ide-tape: Packet size (0x%02x) is not 12 "
3865 "bytes long\n", gcw.packet_size);
3872 * Use INQUIRY to get the firmware revision
3874 static void idetape_get_inquiry_results (ide_drive_t *drive)
3877 idetape_tape_t *tape = drive->driver_data;
3879 idetape_inquiry_result_t *inquiry;
3881 idetape_create_inquiry_cmd(&pc);
3882 if (idetape_queue_pc_tail(drive, &pc)) {
3883 printk(KERN_ERR "ide-tape: %s: can't get INQUIRY results\n", tape->name);
3886 inquiry = (idetape_inquiry_result_t *) pc.buffer;
3887 memcpy(tape->vendor_id, inquiry->vendor_id, 8);
3888 memcpy(tape->product_id, inquiry->product_id, 16);
3889 memcpy(tape->firmware_revision, inquiry->revision_level, 4);
3890 ide_fixstring(tape->vendor_id, 10, 0);
3891 ide_fixstring(tape->product_id, 18, 0);
3892 ide_fixstring(tape->firmware_revision, 6, 0);
3893 r = tape->firmware_revision;
3894 if (*(r + 1) == '.')
3895 tape->firmware_revision_num = (*r - '0') * 100 + (*(r + 2) - '0') * 10 + *(r + 3) - '0';
3896 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);
3900 * idetape_get_mode_sense_results asks the tape about its various
3901 * parameters. In particular, we will adjust our data transfer buffer
3902 * size to the recommended value as returned by the tape.
3904 static void idetape_get_mode_sense_results (ide_drive_t *drive)
3906 idetape_tape_t *tape = drive->driver_data;
3908 idetape_capabilities_page_t *capabilities;
3910 idetape_create_mode_sense_cmd(&pc, IDETAPE_CAPABILITIES_PAGE);
3911 if (idetape_queue_pc_tail(drive, &pc)) {
3912 printk(KERN_ERR "ide-tape: Can't get tape parameters - assuming some default values\n");
3913 tape->tape_block_size = 512;
3914 tape->capabilities.ctl = 52;
3915 tape->capabilities.speed = 450;
3916 tape->capabilities.buffer_size = 6 * 52;
3919 capabilities = (idetape_capabilities_page_t *)
3920 (pc.buffer + 4 + pc.buffer[3]);
3922 capabilities->max_speed = ntohs(capabilities->max_speed);
3923 capabilities->ctl = ntohs(capabilities->ctl);
3924 capabilities->speed = ntohs(capabilities->speed);
3925 capabilities->buffer_size = ntohs(capabilities->buffer_size);
3927 if (!capabilities->speed) {
3928 printk(KERN_INFO "ide-tape: %s: overriding capabilities->speed (assuming 650KB/sec)\n", drive->name);
3929 capabilities->speed = 650;
3931 if (!capabilities->max_speed) {
3932 printk(KERN_INFO "ide-tape: %s: overriding capabilities->max_speed (assuming 650KB/sec)\n", drive->name);
3933 capabilities->max_speed = 650;
3936 tape->capabilities = *capabilities; /* Save us a copy */
3937 if (capabilities->blk512)
3938 tape->tape_block_size = 512;
3939 else if (capabilities->blk1024)
3940 tape->tape_block_size = 1024;
3942 #if IDETAPE_DEBUG_INFO
3943 printk(KERN_INFO "ide-tape: Dumping the results of the MODE SENSE packet command\n");
3944 printk(KERN_INFO "ide-tape: Mode Parameter Header:\n");
3945 printk(KERN_INFO "ide-tape: Mode Data Length - %d\n", pc.buffer[0]);
3946 printk(KERN_INFO "ide-tape: Medium Type - %d\n", pc.buffer[1]);
3947 printk(KERN_INFO "ide-tape: Device Specific Parameter - %d\n",
3949 printk(KERN_INFO "ide-tape: Block Descriptor Length - %d\n",
3952 printk(KERN_INFO "ide-tape: Capabilities and Mechanical Status Page:\n");
3953 printk(KERN_INFO "ide-tape: Page code - %d\n",capabilities->page_code);
3954 printk(KERN_INFO "ide-tape: Page length - %d\n",capabilities->page_length);
3955 printk(KERN_INFO "ide-tape: Read only - %s\n",capabilities->ro ? "Yes":"No");
3956 printk(KERN_INFO "ide-tape: Supports reverse space - %s\n",capabilities->sprev ? "Yes":"No");
3957 printk(KERN_INFO "ide-tape: Supports erase initiated formatting - %s\n",capabilities->efmt ? "Yes":"No");
3958 printk(KERN_INFO "ide-tape: Supports QFA two Partition format - %s\n",capabilities->qfa ? "Yes":"No");
3959 printk(KERN_INFO "ide-tape: Supports locking the medium - %s\n",capabilities->lock ? "Yes":"No");
3960 printk(KERN_INFO "ide-tape: The volume is currently locked - %s\n",capabilities->locked ? "Yes":"No");
3961 printk(KERN_INFO "ide-tape: The device defaults in the prevent state - %s\n",capabilities->prevent ? "Yes":"No");
3962 printk(KERN_INFO "ide-tape: Supports ejecting the medium - %s\n",capabilities->eject ? "Yes":"No");
3963 printk(KERN_INFO "ide-tape: Supports error correction - %s\n",capabilities->ecc ? "Yes":"No");
3964 printk(KERN_INFO "ide-tape: Supports data compression - %s\n",capabilities->cmprs ? "Yes":"No");
3965 printk(KERN_INFO "ide-tape: Supports 512 bytes block size - %s\n",capabilities->blk512 ? "Yes":"No");
3966 printk(KERN_INFO "ide-tape: Supports 1024 bytes block size - %s\n",capabilities->blk1024 ? "Yes":"No");
3967 printk(KERN_INFO "ide-tape: Supports 32768 bytes block size / Restricted byte count for PIO transfers - %s\n",capabilities->blk32768 ? "Yes":"No");
3968 printk(KERN_INFO "ide-tape: Maximum supported speed in KBps - %d\n",capabilities->max_speed);
3969 printk(KERN_INFO "ide-tape: Continuous transfer limits in blocks - %d\n",capabilities->ctl);
3970 printk(KERN_INFO "ide-tape: Current speed in KBps - %d\n",capabilities->speed);
3971 printk(KERN_INFO "ide-tape: Buffer size - %d\n",capabilities->buffer_size*512);
3972 #endif /* IDETAPE_DEBUG_INFO */
3976 * ide_get_blocksize_from_block_descriptor does a mode sense page 0 with block descriptor
3977 * and if it succeeds sets the tape block size with the reported value
3979 static void idetape_get_blocksize_from_block_descriptor(ide_drive_t *drive)
3982 idetape_tape_t *tape = drive->driver_data;
3984 idetape_parameter_block_descriptor_t *block_descrp;
3986 idetape_create_mode_sense_cmd(&pc, IDETAPE_BLOCK_DESCRIPTOR);
3987 if (idetape_queue_pc_tail(drive, &pc)) {
3988 printk(KERN_ERR "ide-tape: Can't get block descriptor\n");
3989 if (tape->tape_block_size == 0) {
3990 printk(KERN_WARNING "ide-tape: Cannot deal with zero block size, assume 32k\n");
3991 tape->tape_block_size = 32768;
3995 block_descrp = (idetape_parameter_block_descriptor_t *)(pc.buffer + 4);
3996 tape->tape_block_size =( block_descrp->length[0]<<16) + (block_descrp->length[1]<<8) + block_descrp->length[2];
3997 tape->drv_write_prot = (pc.buffer[2] & 0x80) >> 7;
3999 #if IDETAPE_DEBUG_INFO
4000 printk(KERN_INFO "ide-tape: Adjusted block size - %d\n", tape->tape_block_size);
4001 #endif /* IDETAPE_DEBUG_INFO */
4004 #ifdef CONFIG_IDE_PROC_FS
4005 static void idetape_add_settings (ide_drive_t *drive)
4007 idetape_tape_t *tape = drive->driver_data;
4010 * drive setting name read/write data type min max mul_factor div_factor data pointer set function
4012 ide_add_setting(drive, "buffer", SETTING_READ, TYPE_SHORT, 0, 0xffff, 1, 2, &tape->capabilities.buffer_size, NULL);
4013 ide_add_setting(drive, "pipeline_min", SETTING_RW, TYPE_INT, 1, 0xffff, tape->stage_size / 1024, 1, &tape->min_pipeline, NULL);
4014 ide_add_setting(drive, "pipeline", SETTING_RW, TYPE_INT, 1, 0xffff, tape->stage_size / 1024, 1, &tape->max_stages, NULL);
4015 ide_add_setting(drive, "pipeline_max", SETTING_RW, TYPE_INT, 1, 0xffff, tape->stage_size / 1024, 1, &tape->max_pipeline, NULL);
4016 ide_add_setting(drive, "pipeline_used", SETTING_READ, TYPE_INT, 0, 0xffff, tape->stage_size / 1024, 1, &tape->nr_stages, NULL);
4017 ide_add_setting(drive, "pipeline_pending", SETTING_READ, TYPE_INT, 0, 0xffff, tape->stage_size / 1024, 1, &tape->nr_pending_stages, NULL);
4018 ide_add_setting(drive, "speed", SETTING_READ, TYPE_SHORT, 0, 0xffff, 1, 1, &tape->capabilities.speed, NULL);
4019 ide_add_setting(drive, "stage", SETTING_READ, TYPE_INT, 0, 0xffff, 1, 1024, &tape->stage_size, NULL);
4020 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);
4021 ide_add_setting(drive, "dsc_overlap", SETTING_RW, TYPE_BYTE, 0, 1, 1, 1, &drive->dsc_overlap, NULL);
4022 ide_add_setting(drive, "pipeline_head_speed_c",SETTING_READ, TYPE_INT, 0, 0xffff, 1, 1, &tape->controlled_pipeline_head_speed, NULL);
4023 ide_add_setting(drive, "pipeline_head_speed_u",SETTING_READ, TYPE_INT, 0, 0xffff, 1, 1, &tape->uncontrolled_pipeline_head_speed,NULL);
4024 ide_add_setting(drive, "avg_speed", SETTING_READ, TYPE_INT, 0, 0xffff, 1, 1, &tape->avg_speed, NULL);
4025 ide_add_setting(drive, "debug_level", SETTING_RW, TYPE_INT, 0, 0xffff, 1, 1, &tape->debug_level, NULL);
4028 static inline void idetape_add_settings(ide_drive_t *drive) { ; }
4032 * ide_setup is called to:
4034 * 1. Initialize our various state variables.
4035 * 2. Ask the tape for its capabilities.
4036 * 3. Allocate a buffer which will be used for data
4037 * transfer. The buffer size is chosen based on
4038 * the recommendation which we received in step (2).
4040 * Note that at this point ide.c already assigned us an irq, so that
4041 * we can queue requests here and wait for their completion.
4043 static void idetape_setup (ide_drive_t *drive, idetape_tape_t *tape, int minor)
4045 unsigned long t1, tmid, tn, t;
4047 struct idetape_id_gcw gcw;
4051 spin_lock_init(&tape->spinlock);
4052 drive->dsc_overlap = 1;
4053 if (drive->hwif->host_flags & IDE_HFLAG_NO_DSC) {
4054 printk(KERN_INFO "ide-tape: %s: disabling DSC overlap\n",
4056 drive->dsc_overlap = 0;
4058 /* Seagate Travan drives do not support DSC overlap. */
4059 if (strstr(drive->id->model, "Seagate STT3401"))
4060 drive->dsc_overlap = 0;
4061 tape->minor = minor;
4062 tape->name[0] = 'h';
4063 tape->name[1] = 't';
4064 tape->name[2] = '0' + minor;
4065 tape->chrdev_direction = idetape_direction_none;
4066 tape->pc = tape->pc_stack;
4067 tape->max_insert_speed = 10000;
4068 tape->speed_control = 1;
4069 *((unsigned short *) &gcw) = drive->id->config;
4070 if (gcw.drq_type == 1)
4071 set_bit(IDETAPE_DRQ_INTERRUPT, &tape->flags);
4073 tape->min_pipeline = tape->max_pipeline = tape->max_stages = 10;
4075 idetape_get_inquiry_results(drive);
4076 idetape_get_mode_sense_results(drive);
4077 idetape_get_blocksize_from_block_descriptor(drive);
4078 tape->user_bs_factor = 1;
4079 tape->stage_size = tape->capabilities.ctl * tape->tape_block_size;
4080 while (tape->stage_size > 0xffff) {
4081 printk(KERN_NOTICE "ide-tape: decreasing stage size\n");
4082 tape->capabilities.ctl /= 2;
4083 tape->stage_size = tape->capabilities.ctl * tape->tape_block_size;
4085 stage_size = tape->stage_size;
4086 tape->pages_per_stage = stage_size / PAGE_SIZE;
4087 if (stage_size % PAGE_SIZE) {
4088 tape->pages_per_stage++;
4089 tape->excess_bh_size = PAGE_SIZE - stage_size % PAGE_SIZE;
4093 * Select the "best" DSC read/write polling frequency
4094 * and pipeline size.
4096 speed = max(tape->capabilities.speed, tape->capabilities.max_speed);
4098 tape->max_stages = speed * 1000 * 10 / tape->stage_size;
4101 * Limit memory use for pipeline to 10% of physical memory
4104 if (tape->max_stages * tape->stage_size > si.totalram * si.mem_unit / 10)
4105 tape->max_stages = si.totalram * si.mem_unit / (10 * tape->stage_size);
4106 tape->max_stages = min(tape->max_stages, IDETAPE_MAX_PIPELINE_STAGES);
4107 tape->min_pipeline = min(tape->max_stages, IDETAPE_MIN_PIPELINE_STAGES);
4108 tape->max_pipeline = min(tape->max_stages * 2, IDETAPE_MAX_PIPELINE_STAGES);
4109 if (tape->max_stages == 0)
4110 tape->max_stages = tape->min_pipeline = tape->max_pipeline = 1;
4112 t1 = (tape->stage_size * HZ) / (speed * 1000);
4113 tmid = (tape->capabilities.buffer_size * 32 * HZ) / (speed * 125);
4114 tn = (IDETAPE_FIFO_THRESHOLD * tape->stage_size * HZ) / (speed * 1000);
4116 if (tape->max_stages)
4122 * Ensure that the number we got makes sense; limit
4123 * it within IDETAPE_DSC_RW_MIN and IDETAPE_DSC_RW_MAX.
4125 tape->best_dsc_rw_frequency = max_t(unsigned long, min_t(unsigned long, t, IDETAPE_DSC_RW_MAX), IDETAPE_DSC_RW_MIN);
4126 printk(KERN_INFO "ide-tape: %s <-> %s: %dKBps, %d*%dkB buffer, "
4127 "%dkB pipeline, %lums tDSC%s\n",
4128 drive->name, tape->name, tape->capabilities.speed,
4129 (tape->capabilities.buffer_size * 512) / tape->stage_size,
4130 tape->stage_size / 1024,
4131 tape->max_stages * tape->stage_size / 1024,
4132 tape->best_dsc_rw_frequency * 1000 / HZ,
4133 drive->using_dma ? ", DMA":"");
4135 idetape_add_settings(drive);
4138 static void ide_tape_remove(ide_drive_t *drive)
4140 idetape_tape_t *tape = drive->driver_data;
4142 ide_proc_unregister_driver(drive, tape->driver);
4144 ide_unregister_region(tape->disk);
4149 static void ide_tape_release(struct kref *kref)
4151 struct ide_tape_obj *tape = to_ide_tape(kref);
4152 ide_drive_t *drive = tape->drive;
4153 struct gendisk *g = tape->disk;
4155 BUG_ON(tape->first_stage != NULL || tape->merge_stage_size);
4157 drive->dsc_overlap = 0;
4158 drive->driver_data = NULL;
4159 device_destroy(idetape_sysfs_class, MKDEV(IDETAPE_MAJOR, tape->minor));
4160 device_destroy(idetape_sysfs_class, MKDEV(IDETAPE_MAJOR, tape->minor + 128));
4161 idetape_devs[tape->minor] = NULL;
4162 g->private_data = NULL;
4167 #ifdef CONFIG_IDE_PROC_FS
4168 static int proc_idetape_read_name
4169 (char *page, char **start, off_t off, int count, int *eof, void *data)
4171 ide_drive_t *drive = (ide_drive_t *) data;
4172 idetape_tape_t *tape = drive->driver_data;
4176 len = sprintf(out, "%s\n", tape->name);
4177 PROC_IDE_READ_RETURN(page, start, off, count, eof, len);
4180 static ide_proc_entry_t idetape_proc[] = {
4181 { "capacity", S_IFREG|S_IRUGO, proc_ide_read_capacity, NULL },
4182 { "name", S_IFREG|S_IRUGO, proc_idetape_read_name, NULL },
4183 { NULL, 0, NULL, NULL }
4187 static int ide_tape_probe(ide_drive_t *);
4189 static ide_driver_t idetape_driver = {
4191 .owner = THIS_MODULE,
4193 .bus = &ide_bus_type,
4195 .probe = ide_tape_probe,
4196 .remove = ide_tape_remove,
4197 .version = IDETAPE_VERSION,
4199 .supports_dsc_overlap = 1,
4200 .do_request = idetape_do_request,
4201 .end_request = idetape_end_request,
4202 .error = __ide_error,
4203 .abort = __ide_abort,
4204 #ifdef CONFIG_IDE_PROC_FS
4205 .proc = idetape_proc,
4210 * Our character device supporting functions, passed to register_chrdev.
4212 static const struct file_operations idetape_fops = {
4213 .owner = THIS_MODULE,
4214 .read = idetape_chrdev_read,
4215 .write = idetape_chrdev_write,
4216 .ioctl = idetape_chrdev_ioctl,
4217 .open = idetape_chrdev_open,
4218 .release = idetape_chrdev_release,
4221 static int idetape_open(struct inode *inode, struct file *filp)
4223 struct gendisk *disk = inode->i_bdev->bd_disk;
4224 struct ide_tape_obj *tape;
4226 if (!(tape = ide_tape_get(disk)))
4232 static int idetape_release(struct inode *inode, struct file *filp)
4234 struct gendisk *disk = inode->i_bdev->bd_disk;
4235 struct ide_tape_obj *tape = ide_tape_g(disk);
4242 static int idetape_ioctl(struct inode *inode, struct file *file,
4243 unsigned int cmd, unsigned long arg)
4245 struct block_device *bdev = inode->i_bdev;
4246 struct ide_tape_obj *tape = ide_tape_g(bdev->bd_disk);
4247 ide_drive_t *drive = tape->drive;
4248 int err = generic_ide_ioctl(drive, file, bdev, cmd, arg);
4250 err = idetape_blkdev_ioctl(drive, cmd, arg);
4254 static struct block_device_operations idetape_block_ops = {
4255 .owner = THIS_MODULE,
4256 .open = idetape_open,
4257 .release = idetape_release,
4258 .ioctl = idetape_ioctl,
4261 static int ide_tape_probe(ide_drive_t *drive)
4263 idetape_tape_t *tape;
4267 if (!strstr("ide-tape", drive->driver_req))
4269 if (!drive->present)
4271 if (drive->media != ide_tape)
4273 if (!idetape_identify_device (drive)) {
4274 printk(KERN_ERR "ide-tape: %s: not supported by this version of ide-tape\n", drive->name);
4278 printk("ide-tape: passing drive %s to ide-scsi emulation.\n", drive->name);
4281 if (strstr(drive->id->model, "OnStream DI-")) {
4282 printk(KERN_WARNING "ide-tape: Use drive %s with ide-scsi emulation and osst.\n", drive->name);
4283 printk(KERN_WARNING "ide-tape: OnStream support will be removed soon from ide-tape!\n");
4285 tape = kzalloc(sizeof (idetape_tape_t), GFP_KERNEL);
4287 printk(KERN_ERR "ide-tape: %s: Can't allocate a tape structure\n", drive->name);
4291 g = alloc_disk(1 << PARTN_BITS);
4295 ide_init_disk(g, drive);
4297 ide_proc_register_driver(drive, &idetape_driver);
4299 kref_init(&tape->kref);
4301 tape->drive = drive;
4302 tape->driver = &idetape_driver;
4305 g->private_data = &tape->driver;
4307 drive->driver_data = tape;
4309 mutex_lock(&idetape_ref_mutex);
4310 for (minor = 0; idetape_devs[minor]; minor++)
4312 idetape_devs[minor] = tape;
4313 mutex_unlock(&idetape_ref_mutex);
4315 idetape_setup(drive, tape, minor);
4317 device_create(idetape_sysfs_class, &drive->gendev,
4318 MKDEV(IDETAPE_MAJOR, minor), "%s", tape->name);
4319 device_create(idetape_sysfs_class, &drive->gendev,
4320 MKDEV(IDETAPE_MAJOR, minor + 128), "n%s", tape->name);
4322 g->fops = &idetape_block_ops;
4323 ide_register_region(g);
4333 MODULE_DESCRIPTION("ATAPI Streaming TAPE Driver");
4334 MODULE_LICENSE("GPL");
4336 static void __exit idetape_exit (void)
4338 driver_unregister(&idetape_driver.gen_driver);
4339 class_destroy(idetape_sysfs_class);
4340 unregister_chrdev(IDETAPE_MAJOR, "ht");
4343 static int __init idetape_init(void)
4346 idetape_sysfs_class = class_create(THIS_MODULE, "ide_tape");
4347 if (IS_ERR(idetape_sysfs_class)) {
4348 idetape_sysfs_class = NULL;
4349 printk(KERN_ERR "Unable to create sysfs class for ide tapes\n");
4354 if (register_chrdev(IDETAPE_MAJOR, "ht", &idetape_fops)) {
4355 printk(KERN_ERR "ide-tape: Failed to register character device interface\n");
4357 goto out_free_class;
4360 error = driver_register(&idetape_driver.gen_driver);
4362 goto out_free_driver;
4367 driver_unregister(&idetape_driver.gen_driver);
4369 class_destroy(idetape_sysfs_class);
4374 MODULE_ALIAS("ide:*m-tape*");
4375 module_init(idetape_init);
4376 module_exit(idetape_exit);
4377 MODULE_ALIAS_CHARDEV_MAJOR(IDETAPE_MAJOR);