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 * REQUEST SENSE packet command result - Data Format.
339 unsigned error_code :7; /* Current of deferred errors */
340 unsigned valid :1; /* The information field conforms to QIC-157C */
341 __u8 reserved1 :8; /* Segment Number - Reserved */
342 unsigned sense_key :4; /* Sense Key */
343 unsigned reserved2_4 :1; /* Reserved */
344 unsigned ili :1; /* Incorrect Length Indicator */
345 unsigned eom :1; /* End Of Medium */
346 unsigned filemark :1; /* Filemark */
347 __u32 information __attribute__ ((packed));
348 __u8 asl; /* Additional sense length (n-7) */
349 __u32 command_specific; /* Additional command specific information */
350 __u8 asc; /* Additional Sense Code */
351 __u8 ascq; /* Additional Sense Code Qualifier */
352 __u8 replaceable_unit_code; /* Field Replaceable Unit Code */
353 unsigned sk_specific1 :7; /* Sense Key Specific */
354 unsigned sksv :1; /* Sense Key Specific information is valid */
355 __u8 sk_specific2; /* Sense Key Specific */
356 __u8 sk_specific3; /* Sense Key Specific */
357 __u8 pad[2]; /* Padding to 20 bytes */
358 } idetape_request_sense_result_t;
362 * Most of our global data which we need to save even as we leave the
363 * driver due to an interrupt or a timer event is stored in a variable
364 * of type idetape_tape_t, defined below.
366 typedef struct ide_tape_obj {
368 ide_driver_t *driver;
369 struct gendisk *disk;
373 * Since a typical character device operation requires more
374 * than one packet command, we provide here enough memory
375 * for the maximum of interconnected packet commands.
376 * The packet commands are stored in the circular array pc_stack.
377 * pc_stack_index points to the last used entry, and warps around
378 * to the start when we get to the last array entry.
380 * pc points to the current processed packet command.
382 * failed_pc points to the last failed packet command, or contains
383 * NULL if we do not need to retry any packet command. This is
384 * required since an additional packet command is needed before the
385 * retry, to get detailed information on what went wrong.
387 /* Current packet command */
389 /* Last failed packet command */
390 idetape_pc_t *failed_pc;
391 /* Packet command stack */
392 idetape_pc_t pc_stack[IDETAPE_PC_STACK];
393 /* Next free packet command storage space */
395 struct request rq_stack[IDETAPE_PC_STACK];
396 /* We implement a circular array */
400 * DSC polling variables.
402 * While polling for DSC we use postponed_rq to postpone the
403 * current request so that ide.c will be able to service
404 * pending requests on the other device. Note that at most
405 * we will have only one DSC (usually data transfer) request
406 * in the device request queue. Additional requests can be
407 * queued in our internal pipeline, but they will be visible
408 * to ide.c only one at a time.
410 struct request *postponed_rq;
411 /* The time in which we started polling for DSC */
412 unsigned long dsc_polling_start;
413 /* Timer used to poll for dsc */
414 struct timer_list dsc_timer;
415 /* Read/Write dsc polling frequency */
416 unsigned long best_dsc_rw_frequency;
417 /* The current polling frequency */
418 unsigned long dsc_polling_frequency;
419 /* Maximum waiting time */
420 unsigned long dsc_timeout;
423 * Read position information
427 unsigned int first_frame_position;
428 unsigned int last_frame_position;
429 unsigned int blocks_in_buffer;
432 * Last error information
434 u8 sense_key, asc, ascq;
437 * Character device operation
442 /* Current character device data transfer direction */
443 idetape_chrdev_direction_t chrdev_direction;
448 /* Usually 512 or 1024 bytes */
449 unsigned short tape_block_size;
451 /* Copy of the tape's Capabilities and Mechanical Page */
452 idetape_capabilities_page_t capabilities;
455 * Active data transfer request parameters.
457 * At most, there is only one ide-tape originated data transfer
458 * request in the device request queue. This allows ide.c to
459 * easily service requests from the other device when we
460 * postpone our active request. In the pipelined operation
461 * mode, we use our internal pipeline structure to hold
462 * more data requests.
464 * The data buffer size is chosen based on the tape's
467 /* Pointer to the request which is waiting in the device request queue */
468 struct request *active_data_request;
469 /* Data buffer size (chosen based on the tape's recommendation */
471 idetape_stage_t *merge_stage;
472 int merge_stage_size;
473 struct idetape_bh *bh;
478 * Pipeline parameters.
480 * To accomplish non-pipelined mode, we simply set the following
481 * variables to zero (or NULL, where appropriate).
483 /* Number of currently used stages */
485 /* Number of pending stages */
486 int nr_pending_stages;
487 /* We will not allocate more than this number of stages */
488 int max_stages, min_pipeline, max_pipeline;
489 /* The first stage which will be removed from the pipeline */
490 idetape_stage_t *first_stage;
491 /* The currently active stage */
492 idetape_stage_t *active_stage;
493 /* Will be serviced after the currently active request */
494 idetape_stage_t *next_stage;
495 /* New requests will be added to the pipeline here */
496 idetape_stage_t *last_stage;
497 /* Optional free stage which we can use */
498 idetape_stage_t *cache_stage;
500 /* Wasted space in each stage */
503 /* Status/Action flags: long for set_bit */
505 /* protects the ide-tape queue */
509 * Measures average tape speed
511 unsigned long avg_time;
515 /* last sense information */
516 idetape_request_sense_result_t sense;
520 char firmware_revision[6];
521 int firmware_revision_num;
523 /* the door is currently locked */
525 /* the tape hardware is write protected */
527 /* the tape is write protected (hardware or opened as read-only) */
531 * Limit the number of times a request can
532 * be postponed, to avoid an infinite postpone
535 /* request postpone count limit */
539 * Measures number of frames:
541 * 1. written/read to/from the driver pipeline (pipeline_head).
542 * 2. written/read to/from the tape buffers (idetape_bh).
543 * 3. written/read by the tape to/from the media (tape_head).
551 * Speed control at the tape buffers input/output
553 unsigned long insert_time;
556 int max_insert_speed;
557 int measure_insert_time;
560 * Measure tape still time, in milliseconds
562 unsigned long tape_still_time_begin;
566 * Speed regulation negative feedback loop
569 int pipeline_head_speed;
570 int controlled_pipeline_head_speed;
571 int uncontrolled_pipeline_head_speed;
572 int controlled_last_pipeline_head;
573 int uncontrolled_last_pipeline_head;
574 unsigned long uncontrolled_pipeline_head_time;
575 unsigned long controlled_pipeline_head_time;
576 int controlled_previous_pipeline_head;
577 int uncontrolled_previous_pipeline_head;
578 unsigned long controlled_previous_head_time;
579 unsigned long uncontrolled_previous_head_time;
580 int restart_speed_control_req;
583 * Debug_level determines amount of debugging output;
584 * can be changed using /proc/ide/hdx/settings
585 * 0 : almost no debugging output
586 * 1 : 0+output errors only
587 * 2 : 1+output all sensekey/asc
588 * 3 : 2+follow all chrdev related procedures
589 * 4 : 3+follow all procedures
590 * 5 : 4+include pc_stack rq_stack info
591 * 6 : 5+USE_COUNT updates
596 static DEFINE_MUTEX(idetape_ref_mutex);
598 static struct class *idetape_sysfs_class;
600 #define to_ide_tape(obj) container_of(obj, struct ide_tape_obj, kref)
602 #define ide_tape_g(disk) \
603 container_of((disk)->private_data, struct ide_tape_obj, driver)
605 static struct ide_tape_obj *ide_tape_get(struct gendisk *disk)
607 struct ide_tape_obj *tape = NULL;
609 mutex_lock(&idetape_ref_mutex);
610 tape = ide_tape_g(disk);
612 kref_get(&tape->kref);
613 mutex_unlock(&idetape_ref_mutex);
617 static void ide_tape_release(struct kref *);
619 static void ide_tape_put(struct ide_tape_obj *tape)
621 mutex_lock(&idetape_ref_mutex);
622 kref_put(&tape->kref, ide_tape_release);
623 mutex_unlock(&idetape_ref_mutex);
629 #define DOOR_UNLOCKED 0
630 #define DOOR_LOCKED 1
631 #define DOOR_EXPLICITLY_LOCKED 2
634 * Tape flag bits values.
636 #define IDETAPE_IGNORE_DSC 0
637 #define IDETAPE_ADDRESS_VALID 1 /* 0 When the tape position is unknown */
638 #define IDETAPE_BUSY 2 /* Device already opened */
639 #define IDETAPE_PIPELINE_ERROR 3 /* Error detected in a pipeline stage */
640 #define IDETAPE_DETECT_BS 4 /* Attempt to auto-detect the current user block size */
641 #define IDETAPE_FILEMARK 5 /* Currently on a filemark */
642 #define IDETAPE_DRQ_INTERRUPT 6 /* DRQ interrupt device */
643 #define IDETAPE_READ_ERROR 7
644 #define IDETAPE_PIPELINE_ACTIVE 8 /* pipeline active */
645 /* 0 = no tape is loaded, so we don't rewind after ejecting */
646 #define IDETAPE_MEDIUM_PRESENT 9
649 * Supported ATAPI tape drives packet commands
651 #define IDETAPE_TEST_UNIT_READY_CMD 0x00
652 #define IDETAPE_REWIND_CMD 0x01
653 #define IDETAPE_REQUEST_SENSE_CMD 0x03
654 #define IDETAPE_READ_CMD 0x08
655 #define IDETAPE_WRITE_CMD 0x0a
656 #define IDETAPE_WRITE_FILEMARK_CMD 0x10
657 #define IDETAPE_SPACE_CMD 0x11
658 #define IDETAPE_INQUIRY_CMD 0x12
659 #define IDETAPE_ERASE_CMD 0x19
660 #define IDETAPE_MODE_SENSE_CMD 0x1a
661 #define IDETAPE_MODE_SELECT_CMD 0x15
662 #define IDETAPE_LOAD_UNLOAD_CMD 0x1b
663 #define IDETAPE_PREVENT_CMD 0x1e
664 #define IDETAPE_LOCATE_CMD 0x2b
665 #define IDETAPE_READ_POSITION_CMD 0x34
666 #define IDETAPE_READ_BUFFER_CMD 0x3c
667 #define IDETAPE_SET_SPEED_CMD 0xbb
670 * Some defines for the READ BUFFER command
672 #define IDETAPE_RETRIEVE_FAULTY_BLOCK 6
675 * Some defines for the SPACE command
677 #define IDETAPE_SPACE_OVER_FILEMARK 1
678 #define IDETAPE_SPACE_TO_EOD 3
681 * Some defines for the LOAD UNLOAD command
683 #define IDETAPE_LU_LOAD_MASK 1
684 #define IDETAPE_LU_RETENSION_MASK 2
685 #define IDETAPE_LU_EOT_MASK 4
688 * Special requests for our block device strategy routine.
690 * In order to service a character device command, we add special
691 * requests to the tail of our block device request queue and wait
692 * for their completion.
696 REQ_IDETAPE_PC1 = (1 << 0), /* packet command (first stage) */
697 REQ_IDETAPE_PC2 = (1 << 1), /* packet command (second stage) */
698 REQ_IDETAPE_READ = (1 << 2),
699 REQ_IDETAPE_WRITE = (1 << 3),
700 REQ_IDETAPE_READ_BUFFER = (1 << 4),
704 * Error codes which are returned in rq->errors to the higher part
707 #define IDETAPE_ERROR_GENERAL 101
708 #define IDETAPE_ERROR_FILEMARK 102
709 #define IDETAPE_ERROR_EOD 103
712 * The following is used to format the general configuration word of
713 * the ATAPI IDENTIFY DEVICE command.
715 struct idetape_id_gcw {
716 unsigned packet_size :2; /* Packet Size */
717 unsigned reserved234 :3; /* Reserved */
718 unsigned drq_type :2; /* Command packet DRQ type */
719 unsigned removable :1; /* Removable media */
720 unsigned device_type :5; /* Device type */
721 unsigned reserved13 :1; /* Reserved */
722 unsigned protocol :2; /* Protocol type */
726 * INQUIRY packet command - Data Format (From Table 6-8 of QIC-157C)
729 unsigned device_type :5; /* Peripheral Device Type */
730 unsigned reserved0_765 :3; /* Peripheral Qualifier - Reserved */
731 unsigned reserved1_6t0 :7; /* Reserved */
732 unsigned rmb :1; /* Removable Medium Bit */
733 unsigned ansi_version :3; /* ANSI Version */
734 unsigned ecma_version :3; /* ECMA Version */
735 unsigned iso_version :2; /* ISO Version */
736 unsigned response_format :4; /* Response Data Format */
737 unsigned reserved3_45 :2; /* Reserved */
738 unsigned reserved3_6 :1; /* TrmIOP - Reserved */
739 unsigned reserved3_7 :1; /* AENC - Reserved */
740 __u8 additional_length; /* Additional Length (total_length-4) */
741 __u8 rsv5, rsv6, rsv7; /* Reserved */
742 __u8 vendor_id[8]; /* Vendor Identification */
743 __u8 product_id[16]; /* Product Identification */
744 __u8 revision_level[4]; /* Revision Level */
745 __u8 vendor_specific[20]; /* Vendor Specific - Optional */
746 __u8 reserved56t95[40]; /* Reserved - Optional */
747 /* Additional information may be returned */
748 } idetape_inquiry_result_t;
751 * READ POSITION packet command - Data Format (From Table 6-57)
754 unsigned reserved0_10 :2; /* Reserved */
755 unsigned bpu :1; /* Block Position Unknown */
756 unsigned reserved0_543 :3; /* Reserved */
757 unsigned eop :1; /* End Of Partition */
758 unsigned bop :1; /* Beginning Of Partition */
759 u8 partition; /* Partition Number */
760 u8 reserved2, reserved3; /* Reserved */
761 u32 first_block; /* First Block Location */
762 u32 last_block; /* Last Block Location (Optional) */
763 u8 reserved12; /* Reserved */
764 u8 blocks_in_buffer[3]; /* Blocks In Buffer - (Optional) */
765 u32 bytes_in_buffer; /* Bytes In Buffer (Optional) */
766 } idetape_read_position_result_t;
769 * Follows structures which are related to the SELECT SENSE / MODE SENSE
770 * packet commands. Those packet commands are still not supported
773 #define IDETAPE_BLOCK_DESCRIPTOR 0
774 #define IDETAPE_CAPABILITIES_PAGE 0x2a
775 #define IDETAPE_PARAMTR_PAGE 0x2b /* Onstream DI-x0 only */
776 #define IDETAPE_BLOCK_SIZE_PAGE 0x30
777 #define IDETAPE_BUFFER_FILLING_PAGE 0x33
780 * Mode Parameter Header for the MODE SENSE packet command
783 __u8 mode_data_length; /* Length of the following data transfer */
784 __u8 medium_type; /* Medium Type */
785 __u8 dsp; /* Device Specific Parameter */
786 __u8 bdl; /* Block Descriptor Length */
788 /* data transfer page */
791 __u8 ps :1; /* parameters saveable */
792 __u8 page_length; /* page Length == 0x02 */
794 __u8 read32k :1; /* 32k blk size (data only) */
795 __u8 read32k5 :1; /* 32.5k blk size (data&AUX) */
796 __u8 reserved3_23 :2;
797 __u8 write32k :1; /* 32k blk size (data only) */
798 __u8 write32k5 :1; /* 32.5k blk size (data&AUX) */
800 __u8 streaming :1; /* streaming mode enable */
802 } idetape_mode_parameter_header_t;
805 * Mode Parameter Block Descriptor the MODE SENSE packet command
807 * Support for block descriptors is optional.
810 __u8 density_code; /* Medium density code */
811 __u8 blocks[3]; /* Number of blocks */
812 __u8 reserved4; /* Reserved */
813 __u8 length[3]; /* Block Length */
814 } idetape_parameter_block_descriptor_t;
817 * The Data Compression Page, as returned by the MODE SENSE packet command.
820 unsigned page_code :6; /* Page Code - Should be 0xf */
821 unsigned reserved0 :1; /* Reserved */
823 __u8 page_length; /* Page Length - Should be 14 */
824 unsigned reserved2 :6; /* Reserved */
825 unsigned dcc :1; /* Data Compression Capable */
826 unsigned dce :1; /* Data Compression Enable */
827 unsigned reserved3 :5; /* Reserved */
828 unsigned red :2; /* Report Exception on Decompression */
829 unsigned dde :1; /* Data Decompression Enable */
830 __u32 ca; /* Compression Algorithm */
831 __u32 da; /* Decompression Algorithm */
832 __u8 reserved[4]; /* Reserved */
833 } idetape_data_compression_page_t;
836 * The Medium Partition Page, as returned by the MODE SENSE packet command.
839 unsigned page_code :6; /* Page Code - Should be 0x11 */
840 unsigned reserved1_6 :1; /* Reserved */
842 __u8 page_length; /* Page Length - Should be 6 */
843 __u8 map; /* Maximum Additional Partitions - Should be 0 */
844 __u8 apd; /* Additional Partitions Defined - Should be 0 */
845 unsigned reserved4_012 :3; /* Reserved */
846 unsigned psum :2; /* Should be 0 */
847 unsigned idp :1; /* Should be 0 */
848 unsigned sdp :1; /* Should be 0 */
849 unsigned fdp :1; /* Fixed Data Partitions */
850 __u8 mfr; /* Medium Format Recognition */
851 __u8 reserved[2]; /* Reserved */
852 } idetape_medium_partition_page_t;
855 * Run time configurable parameters.
858 int dsc_rw_frequency;
859 int dsc_media_access_frequency;
864 * The variables below are used for the character device interface.
865 * Additional state variables are defined in our ide_drive_t structure.
867 static struct ide_tape_obj * idetape_devs[MAX_HWIFS * MAX_DRIVES];
869 #define ide_tape_f(file) ((file)->private_data)
871 static struct ide_tape_obj *ide_tape_chrdev_get(unsigned int i)
873 struct ide_tape_obj *tape = NULL;
875 mutex_lock(&idetape_ref_mutex);
876 tape = idetape_devs[i];
878 kref_get(&tape->kref);
879 mutex_unlock(&idetape_ref_mutex);
884 * Function declarations
887 static int idetape_chrdev_release (struct inode *inode, struct file *filp);
888 static void idetape_write_release (ide_drive_t *drive, unsigned int minor);
891 * Too bad. The drive wants to send us data which we are not ready to accept.
892 * Just throw it away.
894 static void idetape_discard_data (ide_drive_t *drive, unsigned int bcount)
897 (void) HWIF(drive)->INB(IDE_DATA_REG);
900 static void idetape_input_buffers (ide_drive_t *drive, idetape_pc_t *pc, unsigned int bcount)
902 struct idetape_bh *bh = pc->bh;
906 #if IDETAPE_DEBUG_BUGS
908 printk(KERN_ERR "ide-tape: bh == NULL in "
909 "idetape_input_buffers\n");
910 idetape_discard_data(drive, bcount);
913 #endif /* IDETAPE_DEBUG_BUGS */
914 count = min((unsigned int)(bh->b_size - atomic_read(&bh->b_count)), bcount);
915 HWIF(drive)->atapi_input_bytes(drive, bh->b_data + atomic_read(&bh->b_count), count);
917 atomic_add(count, &bh->b_count);
918 if (atomic_read(&bh->b_count) == bh->b_size) {
921 atomic_set(&bh->b_count, 0);
927 static void idetape_output_buffers (ide_drive_t *drive, idetape_pc_t *pc, unsigned int bcount)
929 struct idetape_bh *bh = pc->bh;
933 #if IDETAPE_DEBUG_BUGS
935 printk(KERN_ERR "ide-tape: bh == NULL in "
936 "idetape_output_buffers\n");
939 #endif /* IDETAPE_DEBUG_BUGS */
940 count = min((unsigned int)pc->b_count, (unsigned int)bcount);
941 HWIF(drive)->atapi_output_bytes(drive, pc->b_data, count);
944 pc->b_count -= count;
946 pc->bh = bh = bh->b_reqnext;
948 pc->b_data = bh->b_data;
949 pc->b_count = atomic_read(&bh->b_count);
955 static void idetape_update_buffers (idetape_pc_t *pc)
957 struct idetape_bh *bh = pc->bh;
959 unsigned int bcount = pc->actually_transferred;
961 if (test_bit(PC_WRITING, &pc->flags))
964 #if IDETAPE_DEBUG_BUGS
966 printk(KERN_ERR "ide-tape: bh == NULL in "
967 "idetape_update_buffers\n");
970 #endif /* IDETAPE_DEBUG_BUGS */
971 count = min((unsigned int)bh->b_size, (unsigned int)bcount);
972 atomic_set(&bh->b_count, count);
973 if (atomic_read(&bh->b_count) == bh->b_size)
981 * idetape_next_pc_storage returns a pointer to a place in which we can
982 * safely store a packet command, even though we intend to leave the
983 * driver. A storage space for a maximum of IDETAPE_PC_STACK packet
984 * commands is allocated at initialization time.
986 static idetape_pc_t *idetape_next_pc_storage (ide_drive_t *drive)
988 idetape_tape_t *tape = drive->driver_data;
990 #if IDETAPE_DEBUG_LOG
991 if (tape->debug_level >= 5)
992 printk(KERN_INFO "ide-tape: pc_stack_index=%d\n",
993 tape->pc_stack_index);
994 #endif /* IDETAPE_DEBUG_LOG */
995 if (tape->pc_stack_index == IDETAPE_PC_STACK)
996 tape->pc_stack_index=0;
997 return (&tape->pc_stack[tape->pc_stack_index++]);
1001 * idetape_next_rq_storage is used along with idetape_next_pc_storage.
1002 * Since we queue packet commands in the request queue, we need to
1003 * allocate a request, along with the allocation of a packet command.
1006 /**************************************************************
1008 * This should get fixed to use kmalloc(.., GFP_ATOMIC) *
1009 * followed later on by kfree(). -ml *
1011 **************************************************************/
1013 static struct request *idetape_next_rq_storage (ide_drive_t *drive)
1015 idetape_tape_t *tape = drive->driver_data;
1017 #if IDETAPE_DEBUG_LOG
1018 if (tape->debug_level >= 5)
1019 printk(KERN_INFO "ide-tape: rq_stack_index=%d\n",
1020 tape->rq_stack_index);
1021 #endif /* IDETAPE_DEBUG_LOG */
1022 if (tape->rq_stack_index == IDETAPE_PC_STACK)
1023 tape->rq_stack_index=0;
1024 return (&tape->rq_stack[tape->rq_stack_index++]);
1028 * idetape_init_pc initializes a packet command.
1030 static void idetape_init_pc (idetape_pc_t *pc)
1032 memset(pc->c, 0, 12);
1035 pc->request_transfer = 0;
1036 pc->buffer = pc->pc_buffer;
1037 pc->buffer_size = IDETAPE_PC_BUFFER_SIZE;
1043 * idetape_analyze_error is called on each failed packet command retry
1044 * to analyze the request sense. We currently do not utilize this
1047 static void idetape_analyze_error (ide_drive_t *drive, idetape_request_sense_result_t *result)
1049 idetape_tape_t *tape = drive->driver_data;
1050 idetape_pc_t *pc = tape->failed_pc;
1052 tape->sense = *result;
1053 tape->sense_key = result->sense_key;
1054 tape->asc = result->asc;
1055 tape->ascq = result->ascq;
1056 #if IDETAPE_DEBUG_LOG
1058 * Without debugging, we only log an error if we decided to
1061 if (tape->debug_level >= 1)
1062 printk(KERN_INFO "ide-tape: pc = %x, sense key = %x, "
1063 "asc = %x, ascq = %x\n",
1064 pc->c[0], result->sense_key,
1065 result->asc, result->ascq);
1066 #endif /* IDETAPE_DEBUG_LOG */
1069 * Correct pc->actually_transferred by asking the tape.
1071 if (test_bit(PC_DMA_ERROR, &pc->flags)) {
1072 pc->actually_transferred = pc->request_transfer - tape->tape_block_size * ntohl(get_unaligned(&result->information));
1073 idetape_update_buffers(pc);
1077 * If error was the result of a zero-length read or write command,
1078 * with sense key=5, asc=0x22, ascq=0, let it slide. Some drives
1079 * (i.e. Seagate STT3401A Travan) don't support 0-length read/writes.
1081 if ((pc->c[0] == IDETAPE_READ_CMD || pc->c[0] == IDETAPE_WRITE_CMD)
1082 && pc->c[4] == 0 && pc->c[3] == 0 && pc->c[2] == 0) { /* length==0 */
1083 if (result->sense_key == 5) {
1084 /* don't report an error, everything's ok */
1086 /* don't retry read/write */
1087 set_bit(PC_ABORT, &pc->flags);
1090 if (pc->c[0] == IDETAPE_READ_CMD && result->filemark) {
1091 pc->error = IDETAPE_ERROR_FILEMARK;
1092 set_bit(PC_ABORT, &pc->flags);
1094 if (pc->c[0] == IDETAPE_WRITE_CMD) {
1096 (result->sense_key == 0xd && result->asc == 0x0 &&
1097 result->ascq == 0x2)) {
1098 pc->error = IDETAPE_ERROR_EOD;
1099 set_bit(PC_ABORT, &pc->flags);
1102 if (pc->c[0] == IDETAPE_READ_CMD || pc->c[0] == IDETAPE_WRITE_CMD) {
1103 if (result->sense_key == 8) {
1104 pc->error = IDETAPE_ERROR_EOD;
1105 set_bit(PC_ABORT, &pc->flags);
1107 if (!test_bit(PC_ABORT, &pc->flags) &&
1108 pc->actually_transferred)
1109 pc->retries = IDETAPE_MAX_PC_RETRIES + 1;
1114 * idetape_active_next_stage will declare the next stage as "active".
1116 static void idetape_active_next_stage (ide_drive_t *drive)
1118 idetape_tape_t *tape = drive->driver_data;
1119 idetape_stage_t *stage = tape->next_stage;
1120 struct request *rq = &stage->rq;
1122 #if IDETAPE_DEBUG_LOG
1123 if (tape->debug_level >= 4)
1124 printk(KERN_INFO "ide-tape: Reached idetape_active_next_stage\n");
1125 #endif /* IDETAPE_DEBUG_LOG */
1126 #if IDETAPE_DEBUG_BUGS
1127 if (stage == NULL) {
1128 printk(KERN_ERR "ide-tape: bug: Trying to activate a non existing stage\n");
1131 #endif /* IDETAPE_DEBUG_BUGS */
1133 rq->rq_disk = tape->disk;
1135 rq->special = (void *)stage->bh;
1136 tape->active_data_request = rq;
1137 tape->active_stage = stage;
1138 tape->next_stage = stage->next;
1142 * idetape_increase_max_pipeline_stages is a part of the feedback
1143 * loop which tries to find the optimum number of stages. In the
1144 * feedback loop, we are starting from a minimum maximum number of
1145 * stages, and if we sense that the pipeline is empty, we try to
1146 * increase it, until we reach the user compile time memory limit.
1148 static void idetape_increase_max_pipeline_stages (ide_drive_t *drive)
1150 idetape_tape_t *tape = drive->driver_data;
1151 int increase = (tape->max_pipeline - tape->min_pipeline) / 10;
1153 #if IDETAPE_DEBUG_LOG
1154 if (tape->debug_level >= 4)
1155 printk (KERN_INFO "ide-tape: Reached idetape_increase_max_pipeline_stages\n");
1156 #endif /* IDETAPE_DEBUG_LOG */
1158 tape->max_stages += max(increase, 1);
1159 tape->max_stages = max(tape->max_stages, tape->min_pipeline);
1160 tape->max_stages = min(tape->max_stages, tape->max_pipeline);
1164 * idetape_kfree_stage calls kfree to completely free a stage, along with
1165 * its related buffers.
1167 static void __idetape_kfree_stage (idetape_stage_t *stage)
1169 struct idetape_bh *prev_bh, *bh = stage->bh;
1172 while (bh != NULL) {
1173 if (bh->b_data != NULL) {
1174 size = (int) bh->b_size;
1176 free_page((unsigned long) bh->b_data);
1178 bh->b_data += PAGE_SIZE;
1188 static void idetape_kfree_stage (idetape_tape_t *tape, idetape_stage_t *stage)
1190 __idetape_kfree_stage(stage);
1194 * idetape_remove_stage_head removes tape->first_stage from the pipeline.
1195 * The caller should avoid race conditions.
1197 static void idetape_remove_stage_head (ide_drive_t *drive)
1199 idetape_tape_t *tape = drive->driver_data;
1200 idetape_stage_t *stage;
1202 #if IDETAPE_DEBUG_LOG
1203 if (tape->debug_level >= 4)
1204 printk(KERN_INFO "ide-tape: Reached idetape_remove_stage_head\n");
1205 #endif /* IDETAPE_DEBUG_LOG */
1206 #if IDETAPE_DEBUG_BUGS
1207 if (tape->first_stage == NULL) {
1208 printk(KERN_ERR "ide-tape: bug: tape->first_stage is NULL\n");
1211 if (tape->active_stage == tape->first_stage) {
1212 printk(KERN_ERR "ide-tape: bug: Trying to free our active pipeline stage\n");
1215 #endif /* IDETAPE_DEBUG_BUGS */
1216 stage = tape->first_stage;
1217 tape->first_stage = stage->next;
1218 idetape_kfree_stage(tape, stage);
1220 if (tape->first_stage == NULL) {
1221 tape->last_stage = NULL;
1222 #if IDETAPE_DEBUG_BUGS
1223 if (tape->next_stage != NULL)
1224 printk(KERN_ERR "ide-tape: bug: tape->next_stage != NULL\n");
1225 if (tape->nr_stages)
1226 printk(KERN_ERR "ide-tape: bug: nr_stages should be 0 now\n");
1227 #endif /* IDETAPE_DEBUG_BUGS */
1232 * This will free all the pipeline stages starting from new_last_stage->next
1233 * to the end of the list, and point tape->last_stage to new_last_stage.
1235 static void idetape_abort_pipeline(ide_drive_t *drive,
1236 idetape_stage_t *new_last_stage)
1238 idetape_tape_t *tape = drive->driver_data;
1239 idetape_stage_t *stage = new_last_stage->next;
1240 idetape_stage_t *nstage;
1242 #if IDETAPE_DEBUG_LOG
1243 if (tape->debug_level >= 4)
1244 printk(KERN_INFO "ide-tape: %s: idetape_abort_pipeline called\n", tape->name);
1247 nstage = stage->next;
1248 idetape_kfree_stage(tape, stage);
1250 --tape->nr_pending_stages;
1254 new_last_stage->next = NULL;
1255 tape->last_stage = new_last_stage;
1256 tape->next_stage = NULL;
1260 * idetape_end_request is used to finish servicing a request, and to
1261 * insert a pending pipeline request into the main device queue.
1263 static int idetape_end_request(ide_drive_t *drive, int uptodate, int nr_sects)
1265 struct request *rq = HWGROUP(drive)->rq;
1266 idetape_tape_t *tape = drive->driver_data;
1267 unsigned long flags;
1269 int remove_stage = 0;
1270 idetape_stage_t *active_stage;
1272 #if IDETAPE_DEBUG_LOG
1273 if (tape->debug_level >= 4)
1274 printk(KERN_INFO "ide-tape: Reached idetape_end_request\n");
1275 #endif /* IDETAPE_DEBUG_LOG */
1278 case 0: error = IDETAPE_ERROR_GENERAL; break;
1279 case 1: error = 0; break;
1280 default: error = uptodate;
1284 tape->failed_pc = NULL;
1286 if (!blk_special_request(rq)) {
1287 ide_end_request(drive, uptodate, nr_sects);
1291 spin_lock_irqsave(&tape->spinlock, flags);
1293 /* The request was a pipelined data transfer request */
1294 if (tape->active_data_request == rq) {
1295 active_stage = tape->active_stage;
1296 tape->active_stage = NULL;
1297 tape->active_data_request = NULL;
1298 tape->nr_pending_stages--;
1299 if (rq->cmd[0] & REQ_IDETAPE_WRITE) {
1302 set_bit(IDETAPE_PIPELINE_ERROR, &tape->flags);
1303 if (error == IDETAPE_ERROR_EOD)
1304 idetape_abort_pipeline(drive, active_stage);
1306 } else if (rq->cmd[0] & REQ_IDETAPE_READ) {
1307 if (error == IDETAPE_ERROR_EOD) {
1308 set_bit(IDETAPE_PIPELINE_ERROR, &tape->flags);
1309 idetape_abort_pipeline(drive, active_stage);
1312 if (tape->next_stage != NULL) {
1313 idetape_active_next_stage(drive);
1316 * Insert the next request into the request queue.
1318 (void) ide_do_drive_cmd(drive, tape->active_data_request, ide_end);
1319 } else if (!error) {
1320 idetape_increase_max_pipeline_stages(drive);
1323 ide_end_drive_cmd(drive, 0, 0);
1324 // blkdev_dequeue_request(rq);
1325 // drive->rq = NULL;
1326 // end_that_request_last(rq);
1329 idetape_remove_stage_head(drive);
1330 if (tape->active_data_request == NULL)
1331 clear_bit(IDETAPE_PIPELINE_ACTIVE, &tape->flags);
1332 spin_unlock_irqrestore(&tape->spinlock, flags);
1336 static ide_startstop_t idetape_request_sense_callback (ide_drive_t *drive)
1338 idetape_tape_t *tape = drive->driver_data;
1340 #if IDETAPE_DEBUG_LOG
1341 if (tape->debug_level >= 4)
1342 printk(KERN_INFO "ide-tape: Reached idetape_request_sense_callback\n");
1343 #endif /* IDETAPE_DEBUG_LOG */
1344 if (!tape->pc->error) {
1345 idetape_analyze_error(drive, (idetape_request_sense_result_t *) tape->pc->buffer);
1346 idetape_end_request(drive, 1, 0);
1348 printk(KERN_ERR "ide-tape: Error in REQUEST SENSE itself - Aborting request!\n");
1349 idetape_end_request(drive, 0, 0);
1354 static void idetape_create_request_sense_cmd (idetape_pc_t *pc)
1356 idetape_init_pc(pc);
1357 pc->c[0] = IDETAPE_REQUEST_SENSE_CMD;
1359 pc->request_transfer = 20;
1360 pc->callback = &idetape_request_sense_callback;
1363 static void idetape_init_rq(struct request *rq, u8 cmd)
1365 memset(rq, 0, sizeof(*rq));
1366 rq->cmd_type = REQ_TYPE_SPECIAL;
1371 * idetape_queue_pc_head generates a new packet command request in front
1372 * of the request queue, before the current request, so that it will be
1373 * processed immediately, on the next pass through the driver.
1375 * idetape_queue_pc_head is called from the request handling part of
1376 * the driver (the "bottom" part). Safe storage for the request should
1377 * be allocated with idetape_next_pc_storage and idetape_next_rq_storage
1378 * before calling idetape_queue_pc_head.
1380 * Memory for those requests is pre-allocated at initialization time, and
1381 * is limited to IDETAPE_PC_STACK requests. We assume that we have enough
1382 * space for the maximum possible number of inter-dependent packet commands.
1384 * The higher level of the driver - The ioctl handler and the character
1385 * device handling functions should queue request to the lower level part
1386 * and wait for their completion using idetape_queue_pc_tail or
1387 * idetape_queue_rw_tail.
1389 static void idetape_queue_pc_head (ide_drive_t *drive, idetape_pc_t *pc,struct request *rq)
1391 struct ide_tape_obj *tape = drive->driver_data;
1393 idetape_init_rq(rq, REQ_IDETAPE_PC1);
1394 rq->buffer = (char *) pc;
1395 rq->rq_disk = tape->disk;
1396 (void) ide_do_drive_cmd(drive, rq, ide_preempt);
1400 * idetape_retry_pc is called when an error was detected during the
1401 * last packet command. We queue a request sense packet command in
1402 * the head of the request list.
1404 static ide_startstop_t idetape_retry_pc (ide_drive_t *drive)
1406 idetape_tape_t *tape = drive->driver_data;
1410 (void)drive->hwif->INB(IDE_ERROR_REG);
1411 pc = idetape_next_pc_storage(drive);
1412 rq = idetape_next_rq_storage(drive);
1413 idetape_create_request_sense_cmd(pc);
1414 set_bit(IDETAPE_IGNORE_DSC, &tape->flags);
1415 idetape_queue_pc_head(drive, pc, rq);
1420 * idetape_postpone_request postpones the current request so that
1421 * ide.c will be able to service requests from another device on
1422 * the same hwgroup while we are polling for DSC.
1424 static void idetape_postpone_request (ide_drive_t *drive)
1426 idetape_tape_t *tape = drive->driver_data;
1428 #if IDETAPE_DEBUG_LOG
1429 if (tape->debug_level >= 4)
1430 printk(KERN_INFO "ide-tape: idetape_postpone_request\n");
1432 tape->postponed_rq = HWGROUP(drive)->rq;
1433 ide_stall_queue(drive, tape->dsc_polling_frequency);
1437 * idetape_pc_intr is the usual interrupt handler which will be called
1438 * during a packet command. We will transfer some of the data (as
1439 * requested by the drive) and will re-point interrupt handler to us.
1440 * When data transfer is finished, we will act according to the
1441 * algorithm described before idetape_issue_packet_command.
1444 static ide_startstop_t idetape_pc_intr (ide_drive_t *drive)
1446 ide_hwif_t *hwif = drive->hwif;
1447 idetape_tape_t *tape = drive->driver_data;
1448 idetape_pc_t *pc = tape->pc;
1451 static int error_sim_count = 0;
1456 #if IDETAPE_DEBUG_LOG
1457 if (tape->debug_level >= 4)
1458 printk(KERN_INFO "ide-tape: Reached idetape_pc_intr "
1459 "interrupt handler\n");
1460 #endif /* IDETAPE_DEBUG_LOG */
1462 /* Clear the interrupt */
1463 stat = hwif->INB(IDE_STATUS_REG);
1465 if (test_bit(PC_DMA_IN_PROGRESS, &pc->flags)) {
1466 if (hwif->ide_dma_end(drive) || (stat & ERR_STAT)) {
1468 * A DMA error is sometimes expected. For example,
1469 * if the tape is crossing a filemark during a
1470 * READ command, it will issue an irq and position
1471 * itself before the filemark, so that only a partial
1472 * data transfer will occur (which causes the DMA
1473 * error). In that case, we will later ask the tape
1474 * how much bytes of the original request were
1475 * actually transferred (we can't receive that
1476 * information from the DMA engine on most chipsets).
1480 * On the contrary, a DMA error is never expected;
1481 * it usually indicates a hardware error or abort.
1482 * If the tape crosses a filemark during a READ
1483 * command, it will issue an irq and position itself
1484 * after the filemark (not before). Only a partial
1485 * data transfer will occur, but no DMA error.
1488 set_bit(PC_DMA_ERROR, &pc->flags);
1490 pc->actually_transferred = pc->request_transfer;
1491 idetape_update_buffers(pc);
1493 #if IDETAPE_DEBUG_LOG
1494 if (tape->debug_level >= 4)
1495 printk(KERN_INFO "ide-tape: DMA finished\n");
1496 #endif /* IDETAPE_DEBUG_LOG */
1499 /* No more interrupts */
1500 if ((stat & DRQ_STAT) == 0) {
1501 #if IDETAPE_DEBUG_LOG
1502 if (tape->debug_level >= 2)
1503 printk(KERN_INFO "ide-tape: Packet command completed, %d bytes transferred\n", pc->actually_transferred);
1504 #endif /* IDETAPE_DEBUG_LOG */
1505 clear_bit(PC_DMA_IN_PROGRESS, &pc->flags);
1510 if ((pc->c[0] == IDETAPE_WRITE_CMD ||
1511 pc->c[0] == IDETAPE_READ_CMD) &&
1512 (++error_sim_count % 100) == 0) {
1513 printk(KERN_INFO "ide-tape: %s: simulating error\n",
1518 if ((stat & ERR_STAT) && pc->c[0] == IDETAPE_REQUEST_SENSE_CMD)
1520 if ((stat & ERR_STAT) || test_bit(PC_DMA_ERROR, &pc->flags)) {
1521 /* Error detected */
1522 #if IDETAPE_DEBUG_LOG
1523 if (tape->debug_level >= 1)
1524 printk(KERN_INFO "ide-tape: %s: I/O error\n",
1526 #endif /* IDETAPE_DEBUG_LOG */
1527 if (pc->c[0] == IDETAPE_REQUEST_SENSE_CMD) {
1528 printk(KERN_ERR "ide-tape: I/O error in request sense command\n");
1529 return ide_do_reset(drive);
1531 #if IDETAPE_DEBUG_LOG
1532 if (tape->debug_level >= 1)
1533 printk(KERN_INFO "ide-tape: [cmd %x]: check condition\n", pc->c[0]);
1535 /* Retry operation */
1536 return idetape_retry_pc(drive);
1539 if (test_bit(PC_WAIT_FOR_DSC, &pc->flags) &&
1540 (stat & SEEK_STAT) == 0) {
1541 /* Media access command */
1542 tape->dsc_polling_start = jiffies;
1543 tape->dsc_polling_frequency = IDETAPE_DSC_MA_FAST;
1544 tape->dsc_timeout = jiffies + IDETAPE_DSC_MA_TIMEOUT;
1545 /* Allow ide.c to handle other requests */
1546 idetape_postpone_request(drive);
1549 if (tape->failed_pc == pc)
1550 tape->failed_pc = NULL;
1551 /* Command finished - Call the callback function */
1552 return pc->callback(drive);
1554 if (test_and_clear_bit(PC_DMA_IN_PROGRESS, &pc->flags)) {
1555 printk(KERN_ERR "ide-tape: The tape wants to issue more "
1556 "interrupts in DMA mode\n");
1557 printk(KERN_ERR "ide-tape: DMA disabled, reverting to PIO\n");
1559 return ide_do_reset(drive);
1561 /* Get the number of bytes to transfer on this interrupt. */
1562 bcount = (hwif->INB(IDE_BCOUNTH_REG) << 8) |
1563 hwif->INB(IDE_BCOUNTL_REG);
1565 ireason = hwif->INB(IDE_IREASON_REG);
1568 printk(KERN_ERR "ide-tape: CoD != 0 in idetape_pc_intr\n");
1569 return ide_do_reset(drive);
1571 if (((ireason & IO) == IO) == test_bit(PC_WRITING, &pc->flags)) {
1572 /* Hopefully, we will never get here */
1573 printk(KERN_ERR "ide-tape: We wanted to %s, ",
1574 (ireason & IO) ? "Write" : "Read");
1575 printk(KERN_ERR "ide-tape: but the tape wants us to %s !\n",
1576 (ireason & IO) ? "Read" : "Write");
1577 return ide_do_reset(drive);
1579 if (!test_bit(PC_WRITING, &pc->flags)) {
1580 /* Reading - Check that we have enough space */
1581 temp = pc->actually_transferred + bcount;
1582 if (temp > pc->request_transfer) {
1583 if (temp > pc->buffer_size) {
1584 printk(KERN_ERR "ide-tape: The tape wants to send us more data than expected - discarding data\n");
1585 idetape_discard_data(drive, bcount);
1586 ide_set_handler(drive, &idetape_pc_intr, IDETAPE_WAIT_CMD, NULL);
1589 #if IDETAPE_DEBUG_LOG
1590 if (tape->debug_level >= 2)
1591 printk(KERN_NOTICE "ide-tape: The tape wants to send us more data than expected - allowing transfer\n");
1592 #endif /* IDETAPE_DEBUG_LOG */
1595 if (test_bit(PC_WRITING, &pc->flags)) {
1597 idetape_output_buffers(drive, pc, bcount);
1599 /* Write the current buffer */
1600 hwif->atapi_output_bytes(drive, pc->current_position,
1604 idetape_input_buffers(drive, pc, bcount);
1606 /* Read the current buffer */
1607 hwif->atapi_input_bytes(drive, pc->current_position,
1610 /* Update the current position */
1611 pc->actually_transferred += bcount;
1612 pc->current_position += bcount;
1613 #if IDETAPE_DEBUG_LOG
1614 if (tape->debug_level >= 2)
1615 printk(KERN_INFO "ide-tape: [cmd %x] transferred %d bytes "
1616 "on that interrupt\n", pc->c[0], bcount);
1618 /* And set the interrupt handler again */
1619 ide_set_handler(drive, &idetape_pc_intr, IDETAPE_WAIT_CMD, NULL);
1624 * Packet Command Interface
1626 * The current Packet Command is available in tape->pc, and will not
1627 * change until we finish handling it. Each packet command is associated
1628 * with a callback function that will be called when the command is
1631 * The handling will be done in three stages:
1633 * 1. idetape_issue_packet_command will send the packet command to the
1634 * drive, and will set the interrupt handler to idetape_pc_intr.
1636 * 2. On each interrupt, idetape_pc_intr will be called. This step
1637 * will be repeated until the device signals us that no more
1638 * interrupts will be issued.
1640 * 3. ATAPI Tape media access commands have immediate status with a
1641 * delayed process. In case of a successful initiation of a
1642 * media access packet command, the DSC bit will be set when the
1643 * actual execution of the command is finished.
1644 * Since the tape drive will not issue an interrupt, we have to
1645 * poll for this event. In this case, we define the request as
1646 * "low priority request" by setting rq_status to
1647 * IDETAPE_RQ_POSTPONED, set a timer to poll for DSC and exit
1650 * ide.c will then give higher priority to requests which
1651 * originate from the other device, until will change rq_status
1654 * 4. When the packet command is finished, it will be checked for errors.
1656 * 5. In case an error was found, we queue a request sense packet
1657 * command in front of the request queue and retry the operation
1658 * up to IDETAPE_MAX_PC_RETRIES times.
1660 * 6. In case no error was found, or we decided to give up and not
1661 * to retry again, the callback function will be called and then
1662 * we will handle the next request.
1665 static ide_startstop_t idetape_transfer_pc(ide_drive_t *drive)
1667 ide_hwif_t *hwif = drive->hwif;
1668 idetape_tape_t *tape = drive->driver_data;
1669 idetape_pc_t *pc = tape->pc;
1671 ide_startstop_t startstop;
1674 if (ide_wait_stat(&startstop,drive,DRQ_STAT,BUSY_STAT,WAIT_READY)) {
1675 printk(KERN_ERR "ide-tape: Strange, packet command initiated yet DRQ isn't asserted\n");
1678 ireason = hwif->INB(IDE_IREASON_REG);
1679 while (retries-- && ((ireason & CD) == 0 || (ireason & IO))) {
1680 printk(KERN_ERR "ide-tape: (IO,CoD != (0,1) while issuing "
1681 "a packet command, retrying\n");
1683 ireason = hwif->INB(IDE_IREASON_REG);
1685 printk(KERN_ERR "ide-tape: (IO,CoD != (0,1) while "
1686 "issuing a packet command, ignoring\n");
1691 if ((ireason & CD) == 0 || (ireason & IO)) {
1692 printk(KERN_ERR "ide-tape: (IO,CoD) != (0,1) while issuing "
1693 "a packet command\n");
1694 return ide_do_reset(drive);
1696 /* Set the interrupt routine */
1697 ide_set_handler(drive, &idetape_pc_intr, IDETAPE_WAIT_CMD, NULL);
1698 #ifdef CONFIG_BLK_DEV_IDEDMA
1699 /* Begin DMA, if necessary */
1700 if (test_bit(PC_DMA_IN_PROGRESS, &pc->flags))
1701 hwif->dma_start(drive);
1703 /* Send the actual packet */
1704 HWIF(drive)->atapi_output_bytes(drive, pc->c, 12);
1708 static ide_startstop_t idetape_issue_packet_command (ide_drive_t *drive, idetape_pc_t *pc)
1710 ide_hwif_t *hwif = drive->hwif;
1711 idetape_tape_t *tape = drive->driver_data;
1715 #if IDETAPE_DEBUG_BUGS
1716 if (tape->pc->c[0] == IDETAPE_REQUEST_SENSE_CMD &&
1717 pc->c[0] == IDETAPE_REQUEST_SENSE_CMD) {
1718 printk(KERN_ERR "ide-tape: possible ide-tape.c bug - "
1719 "Two request sense in serial were issued\n");
1721 #endif /* IDETAPE_DEBUG_BUGS */
1723 if (tape->failed_pc == NULL && pc->c[0] != IDETAPE_REQUEST_SENSE_CMD)
1724 tape->failed_pc = pc;
1725 /* Set the current packet command */
1728 if (pc->retries > IDETAPE_MAX_PC_RETRIES ||
1729 test_bit(PC_ABORT, &pc->flags)) {
1731 * We will "abort" retrying a packet command in case
1732 * a legitimate error code was received (crossing a
1733 * filemark, or end of the media, for example).
1735 if (!test_bit(PC_ABORT, &pc->flags)) {
1736 if (!(pc->c[0] == IDETAPE_TEST_UNIT_READY_CMD &&
1737 tape->sense_key == 2 && tape->asc == 4 &&
1738 (tape->ascq == 1 || tape->ascq == 8))) {
1739 printk(KERN_ERR "ide-tape: %s: I/O error, "
1740 "pc = %2x, key = %2x, "
1741 "asc = %2x, ascq = %2x\n",
1742 tape->name, pc->c[0],
1743 tape->sense_key, tape->asc,
1747 pc->error = IDETAPE_ERROR_GENERAL;
1749 tape->failed_pc = NULL;
1750 return pc->callback(drive);
1752 #if IDETAPE_DEBUG_LOG
1753 if (tape->debug_level >= 2)
1754 printk(KERN_INFO "ide-tape: Retry number - %d, cmd = %02X\n", pc->retries, pc->c[0]);
1755 #endif /* IDETAPE_DEBUG_LOG */
1758 /* We haven't transferred any data yet */
1759 pc->actually_transferred = 0;
1760 pc->current_position = pc->buffer;
1761 /* Request to transfer the entire buffer at once */
1762 bcount = pc->request_transfer;
1764 if (test_and_clear_bit(PC_DMA_ERROR, &pc->flags)) {
1765 printk(KERN_WARNING "ide-tape: DMA disabled, "
1766 "reverting to PIO\n");
1769 if (test_bit(PC_DMA_RECOMMENDED, &pc->flags) && drive->using_dma)
1770 dma_ok = !hwif->dma_setup(drive);
1772 ide_pktcmd_tf_load(drive, IDE_TFLAG_NO_SELECT_MASK |
1773 IDE_TFLAG_OUT_DEVICE, bcount, dma_ok);
1775 if (dma_ok) /* Will begin DMA later */
1776 set_bit(PC_DMA_IN_PROGRESS, &pc->flags);
1777 if (test_bit(IDETAPE_DRQ_INTERRUPT, &tape->flags)) {
1778 ide_execute_command(drive, WIN_PACKETCMD, &idetape_transfer_pc,
1779 IDETAPE_WAIT_CMD, NULL);
1782 hwif->OUTB(WIN_PACKETCMD, IDE_COMMAND_REG);
1783 return idetape_transfer_pc(drive);
1788 * General packet command callback function.
1790 static ide_startstop_t idetape_pc_callback (ide_drive_t *drive)
1792 idetape_tape_t *tape = drive->driver_data;
1794 #if IDETAPE_DEBUG_LOG
1795 if (tape->debug_level >= 4)
1796 printk(KERN_INFO "ide-tape: Reached idetape_pc_callback\n");
1797 #endif /* IDETAPE_DEBUG_LOG */
1799 idetape_end_request(drive, tape->pc->error ? 0 : 1, 0);
1804 * A mode sense command is used to "sense" tape parameters.
1806 static void idetape_create_mode_sense_cmd (idetape_pc_t *pc, u8 page_code)
1808 idetape_init_pc(pc);
1809 pc->c[0] = IDETAPE_MODE_SENSE_CMD;
1810 if (page_code != IDETAPE_BLOCK_DESCRIPTOR)
1811 pc->c[1] = 8; /* DBD = 1 - Don't return block descriptors */
1812 pc->c[2] = page_code;
1814 * Changed pc->c[3] to 0 (255 will at best return unused info).
1816 * For SCSI this byte is defined as subpage instead of high byte
1817 * of length and some IDE drives seem to interpret it this way
1818 * and return an error when 255 is used.
1821 pc->c[4] = 255; /* (We will just discard data in that case) */
1822 if (page_code == IDETAPE_BLOCK_DESCRIPTOR)
1823 pc->request_transfer = 12;
1824 else if (page_code == IDETAPE_CAPABILITIES_PAGE)
1825 pc->request_transfer = 24;
1827 pc->request_transfer = 50;
1828 pc->callback = &idetape_pc_callback;
1831 static void calculate_speeds(ide_drive_t *drive)
1833 idetape_tape_t *tape = drive->driver_data;
1834 int full = 125, empty = 75;
1836 if (time_after(jiffies, tape->controlled_pipeline_head_time + 120 * HZ)) {
1837 tape->controlled_previous_pipeline_head = tape->controlled_last_pipeline_head;
1838 tape->controlled_previous_head_time = tape->controlled_pipeline_head_time;
1839 tape->controlled_last_pipeline_head = tape->pipeline_head;
1840 tape->controlled_pipeline_head_time = jiffies;
1842 if (time_after(jiffies, tape->controlled_pipeline_head_time + 60 * HZ))
1843 tape->controlled_pipeline_head_speed = (tape->pipeline_head - tape->controlled_last_pipeline_head) * 32 * HZ / (jiffies - tape->controlled_pipeline_head_time);
1844 else if (time_after(jiffies, tape->controlled_previous_head_time))
1845 tape->controlled_pipeline_head_speed = (tape->pipeline_head - tape->controlled_previous_pipeline_head) * 32 * HZ / (jiffies - tape->controlled_previous_head_time);
1847 if (tape->nr_pending_stages < tape->max_stages /*- 1 */) {
1848 /* -1 for read mode error recovery */
1849 if (time_after(jiffies, tape->uncontrolled_previous_head_time + 10 * HZ)) {
1850 tape->uncontrolled_pipeline_head_time = jiffies;
1851 tape->uncontrolled_pipeline_head_speed = (tape->pipeline_head - tape->uncontrolled_previous_pipeline_head) * 32 * HZ / (jiffies - tape->uncontrolled_previous_head_time);
1854 tape->uncontrolled_previous_head_time = jiffies;
1855 tape->uncontrolled_previous_pipeline_head = tape->pipeline_head;
1856 if (time_after(jiffies, tape->uncontrolled_pipeline_head_time + 30 * HZ)) {
1857 tape->uncontrolled_pipeline_head_time = jiffies;
1860 tape->pipeline_head_speed = max(tape->uncontrolled_pipeline_head_speed, tape->controlled_pipeline_head_speed);
1861 if (tape->speed_control == 0) {
1862 tape->max_insert_speed = 5000;
1863 } else if (tape->speed_control == 1) {
1864 if (tape->nr_pending_stages >= tape->max_stages / 2)
1865 tape->max_insert_speed = tape->pipeline_head_speed +
1866 (1100 - tape->pipeline_head_speed) * 2 * (tape->nr_pending_stages - tape->max_stages / 2) / tape->max_stages;
1868 tape->max_insert_speed = 500 +
1869 (tape->pipeline_head_speed - 500) * 2 * tape->nr_pending_stages / tape->max_stages;
1870 if (tape->nr_pending_stages >= tape->max_stages * 99 / 100)
1871 tape->max_insert_speed = 5000;
1872 } else if (tape->speed_control == 2) {
1873 tape->max_insert_speed = tape->pipeline_head_speed * empty / 100 +
1874 (tape->pipeline_head_speed * full / 100 - tape->pipeline_head_speed * empty / 100) * tape->nr_pending_stages / tape->max_stages;
1876 tape->max_insert_speed = tape->speed_control;
1877 tape->max_insert_speed = max(tape->max_insert_speed, 500);
1880 static ide_startstop_t idetape_media_access_finished (ide_drive_t *drive)
1882 idetape_tape_t *tape = drive->driver_data;
1883 idetape_pc_t *pc = tape->pc;
1886 stat = drive->hwif->INB(IDE_STATUS_REG);
1887 if (stat & SEEK_STAT) {
1888 if (stat & ERR_STAT) {
1889 /* Error detected */
1890 if (pc->c[0] != IDETAPE_TEST_UNIT_READY_CMD)
1891 printk(KERN_ERR "ide-tape: %s: I/O error, ",
1893 /* Retry operation */
1894 return idetape_retry_pc(drive);
1897 if (tape->failed_pc == pc)
1898 tape->failed_pc = NULL;
1900 pc->error = IDETAPE_ERROR_GENERAL;
1901 tape->failed_pc = NULL;
1903 return pc->callback(drive);
1906 static ide_startstop_t idetape_rw_callback (ide_drive_t *drive)
1908 idetape_tape_t *tape = drive->driver_data;
1909 struct request *rq = HWGROUP(drive)->rq;
1910 int blocks = tape->pc->actually_transferred / tape->tape_block_size;
1912 tape->avg_size += blocks * tape->tape_block_size;
1913 tape->insert_size += blocks * tape->tape_block_size;
1914 if (tape->insert_size > 1024 * 1024)
1915 tape->measure_insert_time = 1;
1916 if (tape->measure_insert_time) {
1917 tape->measure_insert_time = 0;
1918 tape->insert_time = jiffies;
1919 tape->insert_size = 0;
1921 if (time_after(jiffies, tape->insert_time))
1922 tape->insert_speed = tape->insert_size / 1024 * HZ / (jiffies - tape->insert_time);
1923 if (time_after_eq(jiffies, tape->avg_time + HZ)) {
1924 tape->avg_speed = tape->avg_size * HZ / (jiffies - tape->avg_time) / 1024;
1926 tape->avg_time = jiffies;
1929 #if IDETAPE_DEBUG_LOG
1930 if (tape->debug_level >= 4)
1931 printk(KERN_INFO "ide-tape: Reached idetape_rw_callback\n");
1932 #endif /* IDETAPE_DEBUG_LOG */
1934 tape->first_frame_position += blocks;
1935 rq->current_nr_sectors -= blocks;
1937 if (!tape->pc->error)
1938 idetape_end_request(drive, 1, 0);
1940 idetape_end_request(drive, tape->pc->error, 0);
1944 static void idetape_create_read_cmd(idetape_tape_t *tape, idetape_pc_t *pc, unsigned int length, struct idetape_bh *bh)
1946 idetape_init_pc(pc);
1947 pc->c[0] = IDETAPE_READ_CMD;
1948 put_unaligned(htonl(length), (unsigned int *) &pc->c[1]);
1950 pc->callback = &idetape_rw_callback;
1952 atomic_set(&bh->b_count, 0);
1954 pc->request_transfer = pc->buffer_size = length * tape->tape_block_size;
1955 if (pc->request_transfer == tape->stage_size)
1956 set_bit(PC_DMA_RECOMMENDED, &pc->flags);
1959 static void idetape_create_read_buffer_cmd(idetape_tape_t *tape, idetape_pc_t *pc, unsigned int length, struct idetape_bh *bh)
1962 struct idetape_bh *p = bh;
1964 idetape_init_pc(pc);
1965 pc->c[0] = IDETAPE_READ_BUFFER_CMD;
1966 pc->c[1] = IDETAPE_RETRIEVE_FAULTY_BLOCK;
1967 pc->c[7] = size >> 8;
1968 pc->c[8] = size & 0xff;
1969 pc->callback = &idetape_pc_callback;
1971 atomic_set(&bh->b_count, 0);
1974 atomic_set(&p->b_count, 0);
1977 pc->request_transfer = pc->buffer_size = size;
1980 static void idetape_create_write_cmd(idetape_tape_t *tape, idetape_pc_t *pc, unsigned int length, struct idetape_bh *bh)
1982 idetape_init_pc(pc);
1983 pc->c[0] = IDETAPE_WRITE_CMD;
1984 put_unaligned(htonl(length), (unsigned int *) &pc->c[1]);
1986 pc->callback = &idetape_rw_callback;
1987 set_bit(PC_WRITING, &pc->flags);
1989 pc->b_data = bh->b_data;
1990 pc->b_count = atomic_read(&bh->b_count);
1992 pc->request_transfer = pc->buffer_size = length * tape->tape_block_size;
1993 if (pc->request_transfer == tape->stage_size)
1994 set_bit(PC_DMA_RECOMMENDED, &pc->flags);
1998 * idetape_do_request is our request handling function.
2000 static ide_startstop_t idetape_do_request(ide_drive_t *drive,
2001 struct request *rq, sector_t block)
2003 idetape_tape_t *tape = drive->driver_data;
2004 idetape_pc_t *pc = NULL;
2005 struct request *postponed_rq = tape->postponed_rq;
2008 #if IDETAPE_DEBUG_LOG
2010 if (tape->debug_level >= 5)
2011 printk(KERN_INFO "ide-tape: %d, "
2012 "dev: %s, cmd: %ld, errors: %d\n",
2013 rq->rq_disk->disk_name, rq->cmd[0], rq->errors);
2015 if (tape->debug_level >= 2)
2016 printk(KERN_INFO "ide-tape: sector: %ld, "
2017 "nr_sectors: %ld, current_nr_sectors: %d\n",
2018 rq->sector, rq->nr_sectors, rq->current_nr_sectors);
2019 #endif /* IDETAPE_DEBUG_LOG */
2021 if (!blk_special_request(rq)) {
2023 * We do not support buffer cache originated requests.
2025 printk(KERN_NOTICE "ide-tape: %s: Unsupported request in "
2026 "request queue (%d)\n", drive->name, rq->cmd_type);
2027 ide_end_request(drive, 0, 0);
2032 * Retry a failed packet command
2034 if (tape->failed_pc != NULL &&
2035 tape->pc->c[0] == IDETAPE_REQUEST_SENSE_CMD) {
2036 return idetape_issue_packet_command(drive, tape->failed_pc);
2038 #if IDETAPE_DEBUG_BUGS
2039 if (postponed_rq != NULL)
2040 if (rq != postponed_rq) {
2041 printk(KERN_ERR "ide-tape: ide-tape.c bug - "
2042 "Two DSC requests were queued\n");
2043 idetape_end_request(drive, 0, 0);
2046 #endif /* IDETAPE_DEBUG_BUGS */
2048 tape->postponed_rq = NULL;
2051 * If the tape is still busy, postpone our request and service
2052 * the other device meanwhile.
2054 stat = drive->hwif->INB(IDE_STATUS_REG);
2056 if (!drive->dsc_overlap && !(rq->cmd[0] & REQ_IDETAPE_PC2))
2057 set_bit(IDETAPE_IGNORE_DSC, &tape->flags);
2059 if (drive->post_reset == 1) {
2060 set_bit(IDETAPE_IGNORE_DSC, &tape->flags);
2061 drive->post_reset = 0;
2064 if (tape->tape_still_time > 100 && tape->tape_still_time < 200)
2065 tape->measure_insert_time = 1;
2066 if (time_after(jiffies, tape->insert_time))
2067 tape->insert_speed = tape->insert_size / 1024 * HZ / (jiffies - tape->insert_time);
2068 calculate_speeds(drive);
2069 if (!test_and_clear_bit(IDETAPE_IGNORE_DSC, &tape->flags) &&
2070 (stat & SEEK_STAT) == 0) {
2071 if (postponed_rq == NULL) {
2072 tape->dsc_polling_start = jiffies;
2073 tape->dsc_polling_frequency = tape->best_dsc_rw_frequency;
2074 tape->dsc_timeout = jiffies + IDETAPE_DSC_RW_TIMEOUT;
2075 } else if (time_after(jiffies, tape->dsc_timeout)) {
2076 printk(KERN_ERR "ide-tape: %s: DSC timeout\n",
2078 if (rq->cmd[0] & REQ_IDETAPE_PC2) {
2079 idetape_media_access_finished(drive);
2082 return ide_do_reset(drive);
2084 } else if (time_after(jiffies, tape->dsc_polling_start + IDETAPE_DSC_MA_THRESHOLD))
2085 tape->dsc_polling_frequency = IDETAPE_DSC_MA_SLOW;
2086 idetape_postpone_request(drive);
2089 if (rq->cmd[0] & REQ_IDETAPE_READ) {
2090 tape->buffer_head++;
2091 tape->postpone_cnt = 0;
2092 pc = idetape_next_pc_storage(drive);
2093 idetape_create_read_cmd(tape, pc, rq->current_nr_sectors, (struct idetape_bh *)rq->special);
2096 if (rq->cmd[0] & REQ_IDETAPE_WRITE) {
2097 tape->buffer_head++;
2098 tape->postpone_cnt = 0;
2099 pc = idetape_next_pc_storage(drive);
2100 idetape_create_write_cmd(tape, pc, rq->current_nr_sectors, (struct idetape_bh *)rq->special);
2103 if (rq->cmd[0] & REQ_IDETAPE_READ_BUFFER) {
2104 tape->postpone_cnt = 0;
2105 pc = idetape_next_pc_storage(drive);
2106 idetape_create_read_buffer_cmd(tape, pc, rq->current_nr_sectors, (struct idetape_bh *)rq->special);
2109 if (rq->cmd[0] & REQ_IDETAPE_PC1) {
2110 pc = (idetape_pc_t *) rq->buffer;
2111 rq->cmd[0] &= ~(REQ_IDETAPE_PC1);
2112 rq->cmd[0] |= REQ_IDETAPE_PC2;
2115 if (rq->cmd[0] & REQ_IDETAPE_PC2) {
2116 idetape_media_access_finished(drive);
2121 return idetape_issue_packet_command(drive, pc);
2125 * Pipeline related functions
2127 static inline int idetape_pipeline_active (idetape_tape_t *tape)
2131 rc1 = test_bit(IDETAPE_PIPELINE_ACTIVE, &tape->flags);
2132 rc2 = (tape->active_data_request != NULL);
2137 * idetape_kmalloc_stage uses __get_free_page to allocate a pipeline
2138 * stage, along with all the necessary small buffers which together make
2139 * a buffer of size tape->stage_size (or a bit more). We attempt to
2140 * combine sequential pages as much as possible.
2142 * Returns a pointer to the new allocated stage, or NULL if we
2143 * can't (or don't want to) allocate a stage.
2145 * Pipeline stages are optional and are used to increase performance.
2146 * If we can't allocate them, we'll manage without them.
2148 static idetape_stage_t *__idetape_kmalloc_stage (idetape_tape_t *tape, int full, int clear)
2150 idetape_stage_t *stage;
2151 struct idetape_bh *prev_bh, *bh;
2152 int pages = tape->pages_per_stage;
2153 char *b_data = NULL;
2155 if ((stage = kmalloc(sizeof (idetape_stage_t),GFP_KERNEL)) == NULL)
2159 bh = stage->bh = kmalloc(sizeof(struct idetape_bh), GFP_KERNEL);
2162 bh->b_reqnext = NULL;
2163 if ((bh->b_data = (char *) __get_free_page (GFP_KERNEL)) == NULL)
2166 memset(bh->b_data, 0, PAGE_SIZE);
2167 bh->b_size = PAGE_SIZE;
2168 atomic_set(&bh->b_count, full ? bh->b_size : 0);
2171 if ((b_data = (char *) __get_free_page (GFP_KERNEL)) == NULL)
2174 memset(b_data, 0, PAGE_SIZE);
2175 if (bh->b_data == b_data + PAGE_SIZE) {
2176 bh->b_size += PAGE_SIZE;
2177 bh->b_data -= PAGE_SIZE;
2179 atomic_add(PAGE_SIZE, &bh->b_count);
2182 if (b_data == bh->b_data + bh->b_size) {
2183 bh->b_size += PAGE_SIZE;
2185 atomic_add(PAGE_SIZE, &bh->b_count);
2189 if ((bh = kmalloc(sizeof(struct idetape_bh), GFP_KERNEL)) == NULL) {
2190 free_page((unsigned long) b_data);
2193 bh->b_reqnext = NULL;
2194 bh->b_data = b_data;
2195 bh->b_size = PAGE_SIZE;
2196 atomic_set(&bh->b_count, full ? bh->b_size : 0);
2197 prev_bh->b_reqnext = bh;
2199 bh->b_size -= tape->excess_bh_size;
2201 atomic_sub(tape->excess_bh_size, &bh->b_count);
2204 __idetape_kfree_stage(stage);
2208 static idetape_stage_t *idetape_kmalloc_stage (idetape_tape_t *tape)
2210 idetape_stage_t *cache_stage = tape->cache_stage;
2212 #if IDETAPE_DEBUG_LOG
2213 if (tape->debug_level >= 4)
2214 printk(KERN_INFO "ide-tape: Reached idetape_kmalloc_stage\n");
2215 #endif /* IDETAPE_DEBUG_LOG */
2217 if (tape->nr_stages >= tape->max_stages)
2219 if (cache_stage != NULL) {
2220 tape->cache_stage = NULL;
2223 return __idetape_kmalloc_stage(tape, 0, 0);
2226 static int idetape_copy_stage_from_user (idetape_tape_t *tape, idetape_stage_t *stage, const char __user *buf, int n)
2228 struct idetape_bh *bh = tape->bh;
2233 #if IDETAPE_DEBUG_BUGS
2235 printk(KERN_ERR "ide-tape: bh == NULL in "
2236 "idetape_copy_stage_from_user\n");
2239 #endif /* IDETAPE_DEBUG_BUGS */
2240 count = min((unsigned int)(bh->b_size - atomic_read(&bh->b_count)), (unsigned int)n);
2241 if (copy_from_user(bh->b_data + atomic_read(&bh->b_count), buf, count))
2244 atomic_add(count, &bh->b_count);
2246 if (atomic_read(&bh->b_count) == bh->b_size) {
2249 atomic_set(&bh->b_count, 0);
2256 static int idetape_copy_stage_to_user (idetape_tape_t *tape, char __user *buf, idetape_stage_t *stage, int n)
2258 struct idetape_bh *bh = tape->bh;
2263 #if IDETAPE_DEBUG_BUGS
2265 printk(KERN_ERR "ide-tape: bh == NULL in "
2266 "idetape_copy_stage_to_user\n");
2269 #endif /* IDETAPE_DEBUG_BUGS */
2270 count = min(tape->b_count, n);
2271 if (copy_to_user(buf, tape->b_data, count))
2274 tape->b_data += count;
2275 tape->b_count -= count;
2277 if (!tape->b_count) {
2278 tape->bh = bh = bh->b_reqnext;
2280 tape->b_data = bh->b_data;
2281 tape->b_count = atomic_read(&bh->b_count);
2288 static void idetape_init_merge_stage (idetape_tape_t *tape)
2290 struct idetape_bh *bh = tape->merge_stage->bh;
2293 if (tape->chrdev_direction == idetape_direction_write)
2294 atomic_set(&bh->b_count, 0);
2296 tape->b_data = bh->b_data;
2297 tape->b_count = atomic_read(&bh->b_count);
2301 static void idetape_switch_buffers (idetape_tape_t *tape, idetape_stage_t *stage)
2303 struct idetape_bh *tmp;
2306 stage->bh = tape->merge_stage->bh;
2307 tape->merge_stage->bh = tmp;
2308 idetape_init_merge_stage(tape);
2312 * idetape_add_stage_tail adds a new stage at the end of the pipeline.
2314 static void idetape_add_stage_tail (ide_drive_t *drive,idetape_stage_t *stage)
2316 idetape_tape_t *tape = drive->driver_data;
2317 unsigned long flags;
2319 #if IDETAPE_DEBUG_LOG
2320 if (tape->debug_level >= 4)
2321 printk (KERN_INFO "ide-tape: Reached idetape_add_stage_tail\n");
2322 #endif /* IDETAPE_DEBUG_LOG */
2323 spin_lock_irqsave(&tape->spinlock, flags);
2325 if (tape->last_stage != NULL)
2326 tape->last_stage->next=stage;
2328 tape->first_stage = tape->next_stage=stage;
2329 tape->last_stage = stage;
2330 if (tape->next_stage == NULL)
2331 tape->next_stage = tape->last_stage;
2333 tape->nr_pending_stages++;
2334 spin_unlock_irqrestore(&tape->spinlock, flags);
2338 * idetape_wait_for_request installs a completion in a pending request
2339 * and sleeps until it is serviced.
2341 * The caller should ensure that the request will not be serviced
2342 * before we install the completion (usually by disabling interrupts).
2344 static void idetape_wait_for_request (ide_drive_t *drive, struct request *rq)
2346 DECLARE_COMPLETION_ONSTACK(wait);
2347 idetape_tape_t *tape = drive->driver_data;
2349 #if IDETAPE_DEBUG_BUGS
2350 if (rq == NULL || !blk_special_request(rq)) {
2351 printk (KERN_ERR "ide-tape: bug: Trying to sleep on non-valid request\n");
2354 #endif /* IDETAPE_DEBUG_BUGS */
2355 rq->end_io_data = &wait;
2356 rq->end_io = blk_end_sync_rq;
2357 spin_unlock_irq(&tape->spinlock);
2358 wait_for_completion(&wait);
2359 /* The stage and its struct request have been deallocated */
2360 spin_lock_irq(&tape->spinlock);
2363 static ide_startstop_t idetape_read_position_callback (ide_drive_t *drive)
2365 idetape_tape_t *tape = drive->driver_data;
2366 idetape_read_position_result_t *result;
2368 #if IDETAPE_DEBUG_LOG
2369 if (tape->debug_level >= 4)
2370 printk(KERN_INFO "ide-tape: Reached idetape_read_position_callback\n");
2371 #endif /* IDETAPE_DEBUG_LOG */
2373 if (!tape->pc->error) {
2374 result = (idetape_read_position_result_t *) tape->pc->buffer;
2375 #if IDETAPE_DEBUG_LOG
2376 if (tape->debug_level >= 2)
2377 printk(KERN_INFO "ide-tape: BOP - %s\n",result->bop ? "Yes":"No");
2378 if (tape->debug_level >= 2)
2379 printk(KERN_INFO "ide-tape: EOP - %s\n",result->eop ? "Yes":"No");
2380 #endif /* IDETAPE_DEBUG_LOG */
2382 printk(KERN_INFO "ide-tape: Block location is unknown to the tape\n");
2383 clear_bit(IDETAPE_ADDRESS_VALID, &tape->flags);
2384 idetape_end_request(drive, 0, 0);
2386 #if IDETAPE_DEBUG_LOG
2387 if (tape->debug_level >= 2)
2388 printk(KERN_INFO "ide-tape: Block Location - %u\n", ntohl(result->first_block));
2389 #endif /* IDETAPE_DEBUG_LOG */
2390 tape->partition = result->partition;
2391 tape->first_frame_position = ntohl(result->first_block);
2392 tape->last_frame_position = ntohl(result->last_block);
2393 tape->blocks_in_buffer = result->blocks_in_buffer[2];
2394 set_bit(IDETAPE_ADDRESS_VALID, &tape->flags);
2395 idetape_end_request(drive, 1, 0);
2398 idetape_end_request(drive, 0, 0);
2404 * idetape_create_write_filemark_cmd will:
2406 * 1. Write a filemark if write_filemark=1.
2407 * 2. Flush the device buffers without writing a filemark
2408 * if write_filemark=0.
2411 static void idetape_create_write_filemark_cmd (ide_drive_t *drive, idetape_pc_t *pc,int write_filemark)
2413 idetape_init_pc(pc);
2414 pc->c[0] = IDETAPE_WRITE_FILEMARK_CMD;
2415 pc->c[4] = write_filemark;
2416 set_bit(PC_WAIT_FOR_DSC, &pc->flags);
2417 pc->callback = &idetape_pc_callback;
2420 static void idetape_create_test_unit_ready_cmd(idetape_pc_t *pc)
2422 idetape_init_pc(pc);
2423 pc->c[0] = IDETAPE_TEST_UNIT_READY_CMD;
2424 pc->callback = &idetape_pc_callback;
2428 * idetape_queue_pc_tail is based on the following functions:
2430 * ide_do_drive_cmd from ide.c
2431 * cdrom_queue_request and cdrom_queue_packet_command from ide-cd.c
2433 * We add a special packet command request to the tail of the request
2434 * queue, and wait for it to be serviced.
2436 * This is not to be called from within the request handling part
2437 * of the driver ! We allocate here data in the stack, and it is valid
2438 * until the request is finished. This is not the case for the bottom
2439 * part of the driver, where we are always leaving the functions to wait
2440 * for an interrupt or a timer event.
2442 * From the bottom part of the driver, we should allocate safe memory
2443 * using idetape_next_pc_storage and idetape_next_rq_storage, and add
2444 * the request to the request list without waiting for it to be serviced !
2445 * In that case, we usually use idetape_queue_pc_head.
2447 static int __idetape_queue_pc_tail (ide_drive_t *drive, idetape_pc_t *pc)
2449 struct ide_tape_obj *tape = drive->driver_data;
2452 idetape_init_rq(&rq, REQ_IDETAPE_PC1);
2453 rq.buffer = (char *) pc;
2454 rq.rq_disk = tape->disk;
2455 return ide_do_drive_cmd(drive, &rq, ide_wait);
2458 static void idetape_create_load_unload_cmd (ide_drive_t *drive, idetape_pc_t *pc,int cmd)
2460 idetape_init_pc(pc);
2461 pc->c[0] = IDETAPE_LOAD_UNLOAD_CMD;
2463 set_bit(PC_WAIT_FOR_DSC, &pc->flags);
2464 pc->callback = &idetape_pc_callback;
2467 static int idetape_wait_ready(ide_drive_t *drive, unsigned long timeout)
2469 idetape_tape_t *tape = drive->driver_data;
2471 int load_attempted = 0;
2474 * Wait for the tape to become ready
2476 set_bit(IDETAPE_MEDIUM_PRESENT, &tape->flags);
2478 while (time_before(jiffies, timeout)) {
2479 idetape_create_test_unit_ready_cmd(&pc);
2480 if (!__idetape_queue_pc_tail(drive, &pc))
2482 if ((tape->sense_key == 2 && tape->asc == 4 && tape->ascq == 2)
2483 || (tape->asc == 0x3A)) { /* no media */
2486 idetape_create_load_unload_cmd(drive, &pc, IDETAPE_LU_LOAD_MASK);
2487 __idetape_queue_pc_tail(drive, &pc);
2489 /* not about to be ready */
2490 } else if (!(tape->sense_key == 2 && tape->asc == 4 &&
2491 (tape->ascq == 1 || tape->ascq == 8)))
2498 static int idetape_queue_pc_tail (ide_drive_t *drive,idetape_pc_t *pc)
2500 return __idetape_queue_pc_tail(drive, pc);
2503 static int idetape_flush_tape_buffers (ide_drive_t *drive)
2508 idetape_create_write_filemark_cmd(drive, &pc, 0);
2509 if ((rc = idetape_queue_pc_tail(drive, &pc)))
2511 idetape_wait_ready(drive, 60 * 5 * HZ);
2515 static void idetape_create_read_position_cmd (idetape_pc_t *pc)
2517 idetape_init_pc(pc);
2518 pc->c[0] = IDETAPE_READ_POSITION_CMD;
2519 pc->request_transfer = 20;
2520 pc->callback = &idetape_read_position_callback;
2523 static int idetape_read_position (ide_drive_t *drive)
2525 idetape_tape_t *tape = drive->driver_data;
2529 #if IDETAPE_DEBUG_LOG
2530 if (tape->debug_level >= 4)
2531 printk(KERN_INFO "ide-tape: Reached idetape_read_position\n");
2532 #endif /* IDETAPE_DEBUG_LOG */
2534 idetape_create_read_position_cmd(&pc);
2535 if (idetape_queue_pc_tail(drive, &pc))
2537 position = tape->first_frame_position;
2541 static void idetape_create_locate_cmd (ide_drive_t *drive, idetape_pc_t *pc, unsigned int block, u8 partition, int skip)
2543 idetape_init_pc(pc);
2544 pc->c[0] = IDETAPE_LOCATE_CMD;
2546 put_unaligned(htonl(block), (unsigned int *) &pc->c[3]);
2547 pc->c[8] = partition;
2548 set_bit(PC_WAIT_FOR_DSC, &pc->flags);
2549 pc->callback = &idetape_pc_callback;
2552 static int idetape_create_prevent_cmd (ide_drive_t *drive, idetape_pc_t *pc, int prevent)
2554 idetape_tape_t *tape = drive->driver_data;
2556 if (!tape->capabilities.lock)
2559 idetape_init_pc(pc);
2560 pc->c[0] = IDETAPE_PREVENT_CMD;
2562 pc->callback = &idetape_pc_callback;
2566 static int __idetape_discard_read_pipeline (ide_drive_t *drive)
2568 idetape_tape_t *tape = drive->driver_data;
2569 unsigned long flags;
2572 if (tape->chrdev_direction != idetape_direction_read)
2575 /* Remove merge stage. */
2576 cnt = tape->merge_stage_size / tape->tape_block_size;
2577 if (test_and_clear_bit(IDETAPE_FILEMARK, &tape->flags))
2578 ++cnt; /* Filemarks count as 1 sector */
2579 tape->merge_stage_size = 0;
2580 if (tape->merge_stage != NULL) {
2581 __idetape_kfree_stage(tape->merge_stage);
2582 tape->merge_stage = NULL;
2585 /* Clear pipeline flags. */
2586 clear_bit(IDETAPE_PIPELINE_ERROR, &tape->flags);
2587 tape->chrdev_direction = idetape_direction_none;
2589 /* Remove pipeline stages. */
2590 if (tape->first_stage == NULL)
2593 spin_lock_irqsave(&tape->spinlock, flags);
2594 tape->next_stage = NULL;
2595 if (idetape_pipeline_active(tape))
2596 idetape_wait_for_request(drive, tape->active_data_request);
2597 spin_unlock_irqrestore(&tape->spinlock, flags);
2599 while (tape->first_stage != NULL) {
2600 struct request *rq_ptr = &tape->first_stage->rq;
2602 cnt += rq_ptr->nr_sectors - rq_ptr->current_nr_sectors;
2603 if (rq_ptr->errors == IDETAPE_ERROR_FILEMARK)
2605 idetape_remove_stage_head(drive);
2607 tape->nr_pending_stages = 0;
2608 tape->max_stages = tape->min_pipeline;
2613 * idetape_position_tape positions the tape to the requested block
2614 * using the LOCATE packet command. A READ POSITION command is then
2615 * issued to check where we are positioned.
2617 * Like all higher level operations, we queue the commands at the tail
2618 * of the request queue and wait for their completion.
2621 static int idetape_position_tape (ide_drive_t *drive, unsigned int block, u8 partition, int skip)
2623 idetape_tape_t *tape = drive->driver_data;
2627 if (tape->chrdev_direction == idetape_direction_read)
2628 __idetape_discard_read_pipeline(drive);
2629 idetape_wait_ready(drive, 60 * 5 * HZ);
2630 idetape_create_locate_cmd(drive, &pc, block, partition, skip);
2631 retval = idetape_queue_pc_tail(drive, &pc);
2635 idetape_create_read_position_cmd(&pc);
2636 return (idetape_queue_pc_tail(drive, &pc));
2639 static void idetape_discard_read_pipeline (ide_drive_t *drive, int restore_position)
2641 idetape_tape_t *tape = drive->driver_data;
2645 cnt = __idetape_discard_read_pipeline(drive);
2646 if (restore_position) {
2647 position = idetape_read_position(drive);
2648 seek = position > cnt ? position - cnt : 0;
2649 if (idetape_position_tape(drive, seek, 0, 0)) {
2650 printk(KERN_INFO "ide-tape: %s: position_tape failed in discard_pipeline()\n", tape->name);
2657 * idetape_queue_rw_tail generates a read/write request for the block
2658 * device interface and wait for it to be serviced.
2660 static int idetape_queue_rw_tail(ide_drive_t *drive, int cmd, int blocks, struct idetape_bh *bh)
2662 idetape_tape_t *tape = drive->driver_data;
2665 #if IDETAPE_DEBUG_LOG
2666 if (tape->debug_level >= 2)
2667 printk(KERN_INFO "ide-tape: idetape_queue_rw_tail: cmd=%d\n",cmd);
2668 #endif /* IDETAPE_DEBUG_LOG */
2669 #if IDETAPE_DEBUG_BUGS
2670 if (idetape_pipeline_active(tape)) {
2671 printk(KERN_ERR "ide-tape: bug: the pipeline is active in idetape_queue_rw_tail\n");
2674 #endif /* IDETAPE_DEBUG_BUGS */
2676 idetape_init_rq(&rq, cmd);
2677 rq.rq_disk = tape->disk;
2678 rq.special = (void *)bh;
2679 rq.sector = tape->first_frame_position;
2680 rq.nr_sectors = rq.current_nr_sectors = blocks;
2681 (void) ide_do_drive_cmd(drive, &rq, ide_wait);
2683 if ((cmd & (REQ_IDETAPE_READ | REQ_IDETAPE_WRITE)) == 0)
2686 if (tape->merge_stage)
2687 idetape_init_merge_stage(tape);
2688 if (rq.errors == IDETAPE_ERROR_GENERAL)
2690 return (tape->tape_block_size * (blocks-rq.current_nr_sectors));
2694 * idetape_insert_pipeline_into_queue is used to start servicing the
2695 * pipeline stages, starting from tape->next_stage.
2697 static void idetape_insert_pipeline_into_queue (ide_drive_t *drive)
2699 idetape_tape_t *tape = drive->driver_data;
2701 if (tape->next_stage == NULL)
2703 if (!idetape_pipeline_active(tape)) {
2704 set_bit(IDETAPE_PIPELINE_ACTIVE, &tape->flags);
2705 idetape_active_next_stage(drive);
2706 (void) ide_do_drive_cmd(drive, tape->active_data_request, ide_end);
2710 static void idetape_create_inquiry_cmd (idetape_pc_t *pc)
2712 idetape_init_pc(pc);
2713 pc->c[0] = IDETAPE_INQUIRY_CMD;
2714 pc->c[4] = pc->request_transfer = 254;
2715 pc->callback = &idetape_pc_callback;
2718 static void idetape_create_rewind_cmd (ide_drive_t *drive, idetape_pc_t *pc)
2720 idetape_init_pc(pc);
2721 pc->c[0] = IDETAPE_REWIND_CMD;
2722 set_bit(PC_WAIT_FOR_DSC, &pc->flags);
2723 pc->callback = &idetape_pc_callback;
2727 static void idetape_create_mode_select_cmd (idetape_pc_t *pc, int length)
2729 idetape_init_pc(pc);
2730 set_bit(PC_WRITING, &pc->flags);
2731 pc->c[0] = IDETAPE_MODE_SELECT_CMD;
2733 put_unaligned(htons(length), (unsigned short *) &pc->c[3]);
2734 pc->request_transfer = 255;
2735 pc->callback = &idetape_pc_callback;
2739 static void idetape_create_erase_cmd (idetape_pc_t *pc)
2741 idetape_init_pc(pc);
2742 pc->c[0] = IDETAPE_ERASE_CMD;
2744 set_bit(PC_WAIT_FOR_DSC, &pc->flags);
2745 pc->callback = &idetape_pc_callback;
2748 static void idetape_create_space_cmd (idetape_pc_t *pc,int count, u8 cmd)
2750 idetape_init_pc(pc);
2751 pc->c[0] = IDETAPE_SPACE_CMD;
2752 put_unaligned(htonl(count), (unsigned int *) &pc->c[1]);
2754 set_bit(PC_WAIT_FOR_DSC, &pc->flags);
2755 pc->callback = &idetape_pc_callback;
2758 static void idetape_wait_first_stage (ide_drive_t *drive)
2760 idetape_tape_t *tape = drive->driver_data;
2761 unsigned long flags;
2763 if (tape->first_stage == NULL)
2765 spin_lock_irqsave(&tape->spinlock, flags);
2766 if (tape->active_stage == tape->first_stage)
2767 idetape_wait_for_request(drive, tape->active_data_request);
2768 spin_unlock_irqrestore(&tape->spinlock, flags);
2772 * idetape_add_chrdev_write_request tries to add a character device
2773 * originated write request to our pipeline. In case we don't succeed,
2774 * we revert to non-pipelined operation mode for this request.
2776 * 1. Try to allocate a new pipeline stage.
2777 * 2. If we can't, wait for more and more requests to be serviced
2778 * and try again each time.
2779 * 3. If we still can't allocate a stage, fallback to
2780 * non-pipelined operation mode for this request.
2782 static int idetape_add_chrdev_write_request (ide_drive_t *drive, int blocks)
2784 idetape_tape_t *tape = drive->driver_data;
2785 idetape_stage_t *new_stage;
2786 unsigned long flags;
2789 #if IDETAPE_DEBUG_LOG
2790 if (tape->debug_level >= 3)
2791 printk(KERN_INFO "ide-tape: Reached idetape_add_chrdev_write_request\n");
2792 #endif /* IDETAPE_DEBUG_LOG */
2795 * Attempt to allocate a new stage.
2796 * Pay special attention to possible race conditions.
2798 while ((new_stage = idetape_kmalloc_stage(tape)) == NULL) {
2799 spin_lock_irqsave(&tape->spinlock, flags);
2800 if (idetape_pipeline_active(tape)) {
2801 idetape_wait_for_request(drive, tape->active_data_request);
2802 spin_unlock_irqrestore(&tape->spinlock, flags);
2804 spin_unlock_irqrestore(&tape->spinlock, flags);
2805 idetape_insert_pipeline_into_queue(drive);
2806 if (idetape_pipeline_active(tape))
2809 * Linux is short on memory. Fallback to
2810 * non-pipelined operation mode for this request.
2812 return idetape_queue_rw_tail(drive, REQ_IDETAPE_WRITE, blocks, tape->merge_stage->bh);
2815 rq = &new_stage->rq;
2816 idetape_init_rq(rq, REQ_IDETAPE_WRITE);
2817 /* Doesn't actually matter - We always assume sequential access */
2818 rq->sector = tape->first_frame_position;
2819 rq->nr_sectors = rq->current_nr_sectors = blocks;
2821 idetape_switch_buffers(tape, new_stage);
2822 idetape_add_stage_tail(drive, new_stage);
2823 tape->pipeline_head++;
2824 calculate_speeds(drive);
2827 * Estimate whether the tape has stopped writing by checking
2828 * if our write pipeline is currently empty. If we are not
2829 * writing anymore, wait for the pipeline to be full enough
2830 * (90%) before starting to service requests, so that we will
2831 * be able to keep up with the higher speeds of the tape.
2833 if (!idetape_pipeline_active(tape)) {
2834 if (tape->nr_stages >= tape->max_stages * 9 / 10 ||
2835 tape->nr_stages >= tape->max_stages - tape->uncontrolled_pipeline_head_speed * 3 * 1024 / tape->tape_block_size) {
2836 tape->measure_insert_time = 1;
2837 tape->insert_time = jiffies;
2838 tape->insert_size = 0;
2839 tape->insert_speed = 0;
2840 idetape_insert_pipeline_into_queue(drive);
2843 if (test_and_clear_bit(IDETAPE_PIPELINE_ERROR, &tape->flags))
2844 /* Return a deferred error */
2850 * idetape_wait_for_pipeline will wait until all pending pipeline
2851 * requests are serviced. Typically called on device close.
2853 static void idetape_wait_for_pipeline (ide_drive_t *drive)
2855 idetape_tape_t *tape = drive->driver_data;
2856 unsigned long flags;
2858 while (tape->next_stage || idetape_pipeline_active(tape)) {
2859 idetape_insert_pipeline_into_queue(drive);
2860 spin_lock_irqsave(&tape->spinlock, flags);
2861 if (idetape_pipeline_active(tape))
2862 idetape_wait_for_request(drive, tape->active_data_request);
2863 spin_unlock_irqrestore(&tape->spinlock, flags);
2867 static void idetape_empty_write_pipeline (ide_drive_t *drive)
2869 idetape_tape_t *tape = drive->driver_data;
2871 struct idetape_bh *bh;
2873 #if IDETAPE_DEBUG_BUGS
2874 if (tape->chrdev_direction != idetape_direction_write) {
2875 printk(KERN_ERR "ide-tape: bug: Trying to empty write pipeline, but we are not writing.\n");
2878 if (tape->merge_stage_size > tape->stage_size) {
2879 printk(KERN_ERR "ide-tape: bug: merge_buffer too big\n");
2880 tape->merge_stage_size = tape->stage_size;
2882 #endif /* IDETAPE_DEBUG_BUGS */
2883 if (tape->merge_stage_size) {
2884 blocks = tape->merge_stage_size / tape->tape_block_size;
2885 if (tape->merge_stage_size % tape->tape_block_size) {
2889 i = tape->tape_block_size - tape->merge_stage_size % tape->tape_block_size;
2890 bh = tape->bh->b_reqnext;
2892 atomic_set(&bh->b_count, 0);
2899 printk(KERN_INFO "ide-tape: bug, bh NULL\n");
2902 min = min(i, (unsigned int)(bh->b_size - atomic_read(&bh->b_count)));
2903 memset(bh->b_data + atomic_read(&bh->b_count), 0, min);
2904 atomic_add(min, &bh->b_count);
2909 (void) idetape_add_chrdev_write_request(drive, blocks);
2910 tape->merge_stage_size = 0;
2912 idetape_wait_for_pipeline(drive);
2913 if (tape->merge_stage != NULL) {
2914 __idetape_kfree_stage(tape->merge_stage);
2915 tape->merge_stage = NULL;
2917 clear_bit(IDETAPE_PIPELINE_ERROR, &tape->flags);
2918 tape->chrdev_direction = idetape_direction_none;
2921 * On the next backup, perform the feedback loop again.
2922 * (I don't want to keep sense information between backups,
2923 * as some systems are constantly on, and the system load
2924 * can be totally different on the next backup).
2926 tape->max_stages = tape->min_pipeline;
2927 #if IDETAPE_DEBUG_BUGS
2928 if (tape->first_stage != NULL ||
2929 tape->next_stage != NULL ||
2930 tape->last_stage != NULL ||
2931 tape->nr_stages != 0) {
2932 printk(KERN_ERR "ide-tape: ide-tape pipeline bug, "
2933 "first_stage %p, next_stage %p, "
2934 "last_stage %p, nr_stages %d\n",
2935 tape->first_stage, tape->next_stage,
2936 tape->last_stage, tape->nr_stages);
2938 #endif /* IDETAPE_DEBUG_BUGS */
2941 static void idetape_restart_speed_control (ide_drive_t *drive)
2943 idetape_tape_t *tape = drive->driver_data;
2945 tape->restart_speed_control_req = 0;
2946 tape->pipeline_head = 0;
2947 tape->controlled_last_pipeline_head = tape->uncontrolled_last_pipeline_head = 0;
2948 tape->controlled_previous_pipeline_head = tape->uncontrolled_previous_pipeline_head = 0;
2949 tape->pipeline_head_speed = tape->controlled_pipeline_head_speed = 5000;
2950 tape->uncontrolled_pipeline_head_speed = 0;
2951 tape->controlled_pipeline_head_time = tape->uncontrolled_pipeline_head_time = jiffies;
2952 tape->controlled_previous_head_time = tape->uncontrolled_previous_head_time = jiffies;
2955 static int idetape_initiate_read (ide_drive_t *drive, int max_stages)
2957 idetape_tape_t *tape = drive->driver_data;
2958 idetape_stage_t *new_stage;
2961 int blocks = tape->capabilities.ctl;
2963 /* Initialize read operation */
2964 if (tape->chrdev_direction != idetape_direction_read) {
2965 if (tape->chrdev_direction == idetape_direction_write) {
2966 idetape_empty_write_pipeline(drive);
2967 idetape_flush_tape_buffers(drive);
2969 #if IDETAPE_DEBUG_BUGS
2970 if (tape->merge_stage || tape->merge_stage_size) {
2971 printk (KERN_ERR "ide-tape: merge_stage_size should be 0 now\n");
2972 tape->merge_stage_size = 0;
2974 #endif /* IDETAPE_DEBUG_BUGS */
2975 if ((tape->merge_stage = __idetape_kmalloc_stage(tape, 0, 0)) == NULL)
2977 tape->chrdev_direction = idetape_direction_read;
2980 * Issue a read 0 command to ensure that DSC handshake
2981 * is switched from completion mode to buffer available
2983 * No point in issuing this if DSC overlap isn't supported,
2984 * some drives (Seagate STT3401A) will return an error.
2986 if (drive->dsc_overlap) {
2987 bytes_read = idetape_queue_rw_tail(drive, REQ_IDETAPE_READ, 0, tape->merge_stage->bh);
2988 if (bytes_read < 0) {
2989 __idetape_kfree_stage(tape->merge_stage);
2990 tape->merge_stage = NULL;
2991 tape->chrdev_direction = idetape_direction_none;
2996 if (tape->restart_speed_control_req)
2997 idetape_restart_speed_control(drive);
2998 idetape_init_rq(&rq, REQ_IDETAPE_READ);
2999 rq.sector = tape->first_frame_position;
3000 rq.nr_sectors = rq.current_nr_sectors = blocks;
3001 if (!test_bit(IDETAPE_PIPELINE_ERROR, &tape->flags) &&
3002 tape->nr_stages < max_stages) {
3003 new_stage = idetape_kmalloc_stage(tape);
3004 while (new_stage != NULL) {
3006 idetape_add_stage_tail(drive, new_stage);
3007 if (tape->nr_stages >= max_stages)
3009 new_stage = idetape_kmalloc_stage(tape);
3012 if (!idetape_pipeline_active(tape)) {
3013 if (tape->nr_pending_stages >= 3 * max_stages / 4) {
3014 tape->measure_insert_time = 1;
3015 tape->insert_time = jiffies;
3016 tape->insert_size = 0;
3017 tape->insert_speed = 0;
3018 idetape_insert_pipeline_into_queue(drive);
3025 * idetape_add_chrdev_read_request is called from idetape_chrdev_read
3026 * to service a character device read request and add read-ahead
3027 * requests to our pipeline.
3029 static int idetape_add_chrdev_read_request (ide_drive_t *drive,int blocks)
3031 idetape_tape_t *tape = drive->driver_data;
3032 unsigned long flags;
3033 struct request *rq_ptr;
3036 #if IDETAPE_DEBUG_LOG
3037 if (tape->debug_level >= 4)
3038 printk(KERN_INFO "ide-tape: Reached idetape_add_chrdev_read_request, %d blocks\n", blocks);
3039 #endif /* IDETAPE_DEBUG_LOG */
3042 * If we are at a filemark, return a read length of 0
3044 if (test_bit(IDETAPE_FILEMARK, &tape->flags))
3048 * Wait for the next block to be available at the head
3051 idetape_initiate_read(drive, tape->max_stages);
3052 if (tape->first_stage == NULL) {
3053 if (test_bit(IDETAPE_PIPELINE_ERROR, &tape->flags))
3055 return idetape_queue_rw_tail(drive, REQ_IDETAPE_READ, blocks, tape->merge_stage->bh);
3057 idetape_wait_first_stage(drive);
3058 rq_ptr = &tape->first_stage->rq;
3059 bytes_read = tape->tape_block_size * (rq_ptr->nr_sectors - rq_ptr->current_nr_sectors);
3060 rq_ptr->nr_sectors = rq_ptr->current_nr_sectors = 0;
3063 if (rq_ptr->errors == IDETAPE_ERROR_EOD)
3066 idetape_switch_buffers(tape, tape->first_stage);
3067 if (rq_ptr->errors == IDETAPE_ERROR_FILEMARK)
3068 set_bit(IDETAPE_FILEMARK, &tape->flags);
3069 spin_lock_irqsave(&tape->spinlock, flags);
3070 idetape_remove_stage_head(drive);
3071 spin_unlock_irqrestore(&tape->spinlock, flags);
3072 tape->pipeline_head++;
3073 calculate_speeds(drive);
3075 #if IDETAPE_DEBUG_BUGS
3076 if (bytes_read > blocks * tape->tape_block_size) {
3077 printk(KERN_ERR "ide-tape: bug: trying to return more bytes than requested\n");
3078 bytes_read = blocks * tape->tape_block_size;
3080 #endif /* IDETAPE_DEBUG_BUGS */
3081 return (bytes_read);
3084 static void idetape_pad_zeros (ide_drive_t *drive, int bcount)
3086 idetape_tape_t *tape = drive->driver_data;
3087 struct idetape_bh *bh;
3093 bh = tape->merge_stage->bh;
3094 count = min(tape->stage_size, bcount);
3096 blocks = count / tape->tape_block_size;
3098 atomic_set(&bh->b_count, min(count, (unsigned int)bh->b_size));
3099 memset(bh->b_data, 0, atomic_read(&bh->b_count));
3100 count -= atomic_read(&bh->b_count);
3103 idetape_queue_rw_tail(drive, REQ_IDETAPE_WRITE, blocks, tape->merge_stage->bh);
3107 static int idetape_pipeline_size (ide_drive_t *drive)
3109 idetape_tape_t *tape = drive->driver_data;
3110 idetape_stage_t *stage;
3114 idetape_wait_for_pipeline(drive);
3115 stage = tape->first_stage;
3116 while (stage != NULL) {
3118 size += tape->tape_block_size * (rq->nr_sectors-rq->current_nr_sectors);
3119 if (rq->errors == IDETAPE_ERROR_FILEMARK)
3120 size += tape->tape_block_size;
3121 stage = stage->next;
3123 size += tape->merge_stage_size;
3128 * Rewinds the tape to the Beginning Of the current Partition (BOP).
3130 * We currently support only one partition.
3132 static int idetape_rewind_tape (ide_drive_t *drive)
3136 #if IDETAPE_DEBUG_LOG
3137 idetape_tape_t *tape = drive->driver_data;
3138 if (tape->debug_level >= 2)
3139 printk(KERN_INFO "ide-tape: Reached idetape_rewind_tape\n");
3140 #endif /* IDETAPE_DEBUG_LOG */
3142 idetape_create_rewind_cmd(drive, &pc);
3143 retval = idetape_queue_pc_tail(drive, &pc);
3147 idetape_create_read_position_cmd(&pc);
3148 retval = idetape_queue_pc_tail(drive, &pc);
3155 * Our special ide-tape ioctl's.
3157 * Currently there aren't any ioctl's.
3158 * mtio.h compatible commands should be issued to the character device
3161 static int idetape_blkdev_ioctl(ide_drive_t *drive, unsigned int cmd, unsigned long arg)
3163 idetape_tape_t *tape = drive->driver_data;
3164 idetape_config_t config;
3165 void __user *argp = (void __user *)arg;
3167 #if IDETAPE_DEBUG_LOG
3168 if (tape->debug_level >= 4)
3169 printk(KERN_INFO "ide-tape: Reached idetape_blkdev_ioctl\n");
3170 #endif /* IDETAPE_DEBUG_LOG */
3173 if (copy_from_user(&config, argp, sizeof (idetape_config_t)))
3175 tape->best_dsc_rw_frequency = config.dsc_rw_frequency;
3176 tape->max_stages = config.nr_stages;
3179 config.dsc_rw_frequency = (int) tape->best_dsc_rw_frequency;
3180 config.nr_stages = tape->max_stages;
3181 if (copy_to_user(argp, &config, sizeof (idetape_config_t)))
3191 * idetape_space_over_filemarks is now a bit more complicated than just
3192 * passing the command to the tape since we may have crossed some
3193 * filemarks during our pipelined read-ahead mode.
3195 * As a minor side effect, the pipeline enables us to support MTFSFM when
3196 * the filemark is in our internal pipeline even if the tape doesn't
3197 * support spacing over filemarks in the reverse direction.
3199 static int idetape_space_over_filemarks (ide_drive_t *drive,short mt_op,int mt_count)
3201 idetape_tape_t *tape = drive->driver_data;
3203 unsigned long flags;
3208 if (MTBSF == mt_op || MTBSFM == mt_op) {
3209 if (!tape->capabilities.sprev)
3211 mt_count = - mt_count;
3214 if (tape->chrdev_direction == idetape_direction_read) {
3216 * We have a read-ahead buffer. Scan it for crossed
3219 tape->merge_stage_size = 0;
3220 if (test_and_clear_bit(IDETAPE_FILEMARK, &tape->flags))
3222 while (tape->first_stage != NULL) {
3223 if (count == mt_count) {
3224 if (mt_op == MTFSFM)
3225 set_bit(IDETAPE_FILEMARK, &tape->flags);
3228 spin_lock_irqsave(&tape->spinlock, flags);
3229 if (tape->first_stage == tape->active_stage) {
3231 * We have reached the active stage in the read pipeline.
3232 * There is no point in allowing the drive to continue
3233 * reading any farther, so we stop the pipeline.
3235 * This section should be moved to a separate subroutine,
3236 * because a similar function is performed in
3237 * __idetape_discard_read_pipeline(), for example.
3239 tape->next_stage = NULL;
3240 spin_unlock_irqrestore(&tape->spinlock, flags);
3241 idetape_wait_first_stage(drive);
3242 tape->next_stage = tape->first_stage->next;
3244 spin_unlock_irqrestore(&tape->spinlock, flags);
3245 if (tape->first_stage->rq.errors == IDETAPE_ERROR_FILEMARK)
3247 idetape_remove_stage_head(drive);
3249 idetape_discard_read_pipeline(drive, 0);
3253 * The filemark was not found in our internal pipeline.
3254 * Now we can issue the space command.
3259 idetape_create_space_cmd(&pc,mt_count-count,IDETAPE_SPACE_OVER_FILEMARK);
3260 return (idetape_queue_pc_tail(drive, &pc));
3263 if (!tape->capabilities.sprev)
3265 retval = idetape_space_over_filemarks(drive, MTFSF, mt_count-count);
3266 if (retval) return (retval);
3267 count = (MTBSFM == mt_op ? 1 : -1);
3268 return (idetape_space_over_filemarks(drive, MTFSF, count));
3270 printk(KERN_ERR "ide-tape: MTIO operation %d not supported\n",mt_op);
3277 * Our character device read / write functions.
3279 * The tape is optimized to maximize throughput when it is transferring
3280 * an integral number of the "continuous transfer limit", which is
3281 * a parameter of the specific tape (26 KB on my particular tape).
3282 * (32 kB for Onstream)
3284 * As of version 1.3 of the driver, the character device provides an
3285 * abstract continuous view of the media - any mix of block sizes (even 1
3286 * byte) on the same backup/restore procedure is supported. The driver
3287 * will internally convert the requests to the recommended transfer unit,
3288 * so that an unmatch between the user's block size to the recommended
3289 * size will only result in a (slightly) increased driver overhead, but
3290 * will no longer hit performance.
3291 * This is not applicable to Onstream.
3293 static ssize_t idetape_chrdev_read (struct file *file, char __user *buf,
3294 size_t count, loff_t *ppos)
3296 struct ide_tape_obj *tape = ide_tape_f(file);
3297 ide_drive_t *drive = tape->drive;
3298 ssize_t bytes_read,temp, actually_read = 0, rc;
3301 #if IDETAPE_DEBUG_LOG
3302 if (tape->debug_level >= 3)
3303 printk(KERN_INFO "ide-tape: Reached idetape_chrdev_read, count %Zd\n", count);
3304 #endif /* IDETAPE_DEBUG_LOG */
3306 if (tape->chrdev_direction != idetape_direction_read) {
3307 if (test_bit(IDETAPE_DETECT_BS, &tape->flags))
3308 if (count > tape->tape_block_size &&
3309 (count % tape->tape_block_size) == 0)
3310 tape->user_bs_factor = count / tape->tape_block_size;
3312 if ((rc = idetape_initiate_read(drive, tape->max_stages)) < 0)
3316 if (tape->merge_stage_size) {
3317 actually_read = min((unsigned int)(tape->merge_stage_size), (unsigned int)count);
3318 if (idetape_copy_stage_to_user(tape, buf, tape->merge_stage, actually_read))
3320 buf += actually_read;
3321 tape->merge_stage_size -= actually_read;
3322 count -= actually_read;
3324 while (count >= tape->stage_size) {
3325 bytes_read = idetape_add_chrdev_read_request(drive, tape->capabilities.ctl);
3326 if (bytes_read <= 0)
3328 if (idetape_copy_stage_to_user(tape, buf, tape->merge_stage, bytes_read))
3331 count -= bytes_read;
3332 actually_read += bytes_read;
3335 bytes_read = idetape_add_chrdev_read_request(drive, tape->capabilities.ctl);
3336 if (bytes_read <= 0)
3338 temp = min((unsigned long)count, (unsigned long)bytes_read);
3339 if (idetape_copy_stage_to_user(tape, buf, tape->merge_stage, temp))
3341 actually_read += temp;
3342 tape->merge_stage_size = bytes_read-temp;
3345 if (!actually_read && test_bit(IDETAPE_FILEMARK, &tape->flags)) {
3346 #if IDETAPE_DEBUG_LOG
3347 if (tape->debug_level >= 2)
3348 printk(KERN_INFO "ide-tape: %s: spacing over filemark\n", tape->name);
3350 idetape_space_over_filemarks(drive, MTFSF, 1);
3354 return (ret) ? ret : actually_read;
3357 static ssize_t idetape_chrdev_write (struct file *file, const char __user *buf,
3358 size_t count, loff_t *ppos)
3360 struct ide_tape_obj *tape = ide_tape_f(file);
3361 ide_drive_t *drive = tape->drive;
3362 ssize_t actually_written = 0;
3365 /* The drive is write protected. */
3366 if (tape->write_prot)
3369 #if IDETAPE_DEBUG_LOG
3370 if (tape->debug_level >= 3)
3371 printk(KERN_INFO "ide-tape: Reached idetape_chrdev_write, "
3372 "count %Zd\n", count);
3373 #endif /* IDETAPE_DEBUG_LOG */
3375 /* Initialize write operation */
3376 if (tape->chrdev_direction != idetape_direction_write) {
3377 if (tape->chrdev_direction == idetape_direction_read)
3378 idetape_discard_read_pipeline(drive, 1);
3379 #if IDETAPE_DEBUG_BUGS
3380 if (tape->merge_stage || tape->merge_stage_size) {
3381 printk(KERN_ERR "ide-tape: merge_stage_size "
3382 "should be 0 now\n");
3383 tape->merge_stage_size = 0;
3385 #endif /* IDETAPE_DEBUG_BUGS */
3386 if ((tape->merge_stage = __idetape_kmalloc_stage(tape, 0, 0)) == NULL)
3388 tape->chrdev_direction = idetape_direction_write;
3389 idetape_init_merge_stage(tape);
3392 * Issue a write 0 command to ensure that DSC handshake
3393 * is switched from completion mode to buffer available
3395 * No point in issuing this if DSC overlap isn't supported,
3396 * some drives (Seagate STT3401A) will return an error.
3398 if (drive->dsc_overlap) {
3399 ssize_t retval = idetape_queue_rw_tail(drive, REQ_IDETAPE_WRITE, 0, tape->merge_stage->bh);
3401 __idetape_kfree_stage(tape->merge_stage);
3402 tape->merge_stage = NULL;
3403 tape->chrdev_direction = idetape_direction_none;
3410 if (tape->restart_speed_control_req)
3411 idetape_restart_speed_control(drive);
3412 if (tape->merge_stage_size) {
3413 #if IDETAPE_DEBUG_BUGS
3414 if (tape->merge_stage_size >= tape->stage_size) {
3415 printk(KERN_ERR "ide-tape: bug: merge buffer too big\n");
3416 tape->merge_stage_size = 0;
3418 #endif /* IDETAPE_DEBUG_BUGS */
3419 actually_written = min((unsigned int)(tape->stage_size - tape->merge_stage_size), (unsigned int)count);
3420 if (idetape_copy_stage_from_user(tape, tape->merge_stage, buf, actually_written))
3422 buf += actually_written;
3423 tape->merge_stage_size += actually_written;
3424 count -= actually_written;
3426 if (tape->merge_stage_size == tape->stage_size) {
3428 tape->merge_stage_size = 0;
3429 retval = idetape_add_chrdev_write_request(drive, tape->capabilities.ctl);
3434 while (count >= tape->stage_size) {
3436 if (idetape_copy_stage_from_user(tape, tape->merge_stage, buf, tape->stage_size))
3438 buf += tape->stage_size;
3439 count -= tape->stage_size;
3440 retval = idetape_add_chrdev_write_request(drive, tape->capabilities.ctl);
3441 actually_written += tape->stage_size;
3446 actually_written += count;
3447 if (idetape_copy_stage_from_user(tape, tape->merge_stage, buf, count))
3449 tape->merge_stage_size += count;
3451 return (ret) ? ret : actually_written;
3454 static int idetape_write_filemark (ide_drive_t *drive)
3458 /* Write a filemark */
3459 idetape_create_write_filemark_cmd(drive, &pc, 1);
3460 if (idetape_queue_pc_tail(drive, &pc)) {
3461 printk(KERN_ERR "ide-tape: Couldn't write a filemark\n");
3468 * idetape_mtioctop is called from idetape_chrdev_ioctl when
3469 * the general mtio MTIOCTOP ioctl is requested.
3471 * We currently support the following mtio.h operations:
3473 * MTFSF - Space over mt_count filemarks in the positive direction.
3474 * The tape is positioned after the last spaced filemark.
3476 * MTFSFM - Same as MTFSF, but the tape is positioned before the
3479 * MTBSF - Steps background over mt_count filemarks, tape is
3480 * positioned before the last filemark.
3482 * MTBSFM - Like MTBSF, only tape is positioned after the last filemark.
3486 * MTBSF and MTBSFM are not supported when the tape doesn't
3487 * support spacing over filemarks in the reverse direction.
3488 * In this case, MTFSFM is also usually not supported (it is
3489 * supported in the rare case in which we crossed the filemark
3490 * during our read-ahead pipelined operation mode).
3492 * MTWEOF - Writes mt_count filemarks. Tape is positioned after
3493 * the last written filemark.
3495 * MTREW - Rewinds tape.
3497 * MTLOAD - Loads the tape.
3499 * MTOFFL - Puts the tape drive "Offline": Rewinds the tape and
3500 * MTUNLOAD prevents further access until the media is replaced.
3502 * MTNOP - Flushes tape buffers.
3504 * MTRETEN - Retension media. This typically consists of one end
3505 * to end pass on the media.
3507 * MTEOM - Moves to the end of recorded data.
3509 * MTERASE - Erases tape.
3511 * MTSETBLK - Sets the user block size to mt_count bytes. If
3512 * mt_count is 0, we will attempt to autodetect
3515 * MTSEEK - Positions the tape in a specific block number, where
3516 * each block is assumed to contain which user_block_size
3519 * MTSETPART - Switches to another tape partition.
3521 * MTLOCK - Locks the tape door.
3523 * MTUNLOCK - Unlocks the tape door.
3525 * The following commands are currently not supported:
3527 * MTFSS, MTBSS, MTWSM, MTSETDENSITY,
3528 * MTSETDRVBUFFER, MT_ST_BOOLEANS, MT_ST_WRITE_THRESHOLD.
3530 static int idetape_mtioctop (ide_drive_t *drive,short mt_op,int mt_count)
3532 idetape_tape_t *tape = drive->driver_data;
3536 #if IDETAPE_DEBUG_LOG
3537 if (tape->debug_level >= 1)
3538 printk(KERN_INFO "ide-tape: Handling MTIOCTOP ioctl: "
3539 "mt_op=%d, mt_count=%d\n", mt_op, mt_count);
3540 #endif /* IDETAPE_DEBUG_LOG */
3542 * Commands which need our pipelined read-ahead stages.
3551 return (idetape_space_over_filemarks(drive,mt_op,mt_count));
3557 if (tape->write_prot)
3559 idetape_discard_read_pipeline(drive, 1);
3560 for (i = 0; i < mt_count; i++) {
3561 retval = idetape_write_filemark(drive);
3567 idetape_discard_read_pipeline(drive, 0);
3568 if (idetape_rewind_tape(drive))
3572 idetape_discard_read_pipeline(drive, 0);
3573 idetape_create_load_unload_cmd(drive, &pc, IDETAPE_LU_LOAD_MASK);
3574 return (idetape_queue_pc_tail(drive, &pc));
3578 * If door is locked, attempt to unlock before
3579 * attempting to eject.
3581 if (tape->door_locked) {
3582 if (idetape_create_prevent_cmd(drive, &pc, 0))
3583 if (!idetape_queue_pc_tail(drive, &pc))
3584 tape->door_locked = DOOR_UNLOCKED;
3586 idetape_discard_read_pipeline(drive, 0);
3587 idetape_create_load_unload_cmd(drive, &pc,!IDETAPE_LU_LOAD_MASK);
3588 retval = idetape_queue_pc_tail(drive, &pc);
3590 clear_bit(IDETAPE_MEDIUM_PRESENT, &tape->flags);
3593 idetape_discard_read_pipeline(drive, 0);
3594 return (idetape_flush_tape_buffers(drive));
3596 idetape_discard_read_pipeline(drive, 0);
3597 idetape_create_load_unload_cmd(drive, &pc,IDETAPE_LU_RETENSION_MASK | IDETAPE_LU_LOAD_MASK);
3598 return (idetape_queue_pc_tail(drive, &pc));
3600 idetape_create_space_cmd(&pc, 0, IDETAPE_SPACE_TO_EOD);
3601 return (idetape_queue_pc_tail(drive, &pc));
3603 (void) idetape_rewind_tape(drive);
3604 idetape_create_erase_cmd(&pc);
3605 return (idetape_queue_pc_tail(drive, &pc));
3608 if (mt_count < tape->tape_block_size || mt_count % tape->tape_block_size)
3610 tape->user_bs_factor = mt_count / tape->tape_block_size;
3611 clear_bit(IDETAPE_DETECT_BS, &tape->flags);
3613 set_bit(IDETAPE_DETECT_BS, &tape->flags);
3616 idetape_discard_read_pipeline(drive, 0);
3617 return idetape_position_tape(drive, mt_count * tape->user_bs_factor, tape->partition, 0);
3619 idetape_discard_read_pipeline(drive, 0);
3620 return (idetape_position_tape(drive, 0, mt_count, 0));
3624 if (!idetape_create_prevent_cmd(drive, &pc, 1))
3626 retval = idetape_queue_pc_tail(drive, &pc);
3627 if (retval) return retval;
3628 tape->door_locked = DOOR_EXPLICITLY_LOCKED;
3631 if (!idetape_create_prevent_cmd(drive, &pc, 0))
3633 retval = idetape_queue_pc_tail(drive, &pc);
3634 if (retval) return retval;
3635 tape->door_locked = DOOR_UNLOCKED;
3638 printk(KERN_ERR "ide-tape: MTIO operation %d not "
3639 "supported\n", mt_op);
3645 * Our character device ioctls.
3647 * General mtio.h magnetic io commands are supported here, and not in
3648 * the corresponding block interface.
3650 * The following ioctls are supported:
3652 * MTIOCTOP - Refer to idetape_mtioctop for detailed description.
3654 * MTIOCGET - The mt_dsreg field in the returned mtget structure
3655 * will be set to (user block size in bytes <<
3656 * MT_ST_BLKSIZE_SHIFT) & MT_ST_BLKSIZE_MASK.
3658 * The mt_blkno is set to the current user block number.
3659 * The other mtget fields are not supported.
3661 * MTIOCPOS - The current tape "block position" is returned. We
3662 * assume that each block contains user_block_size
3665 * Our own ide-tape ioctls are supported on both interfaces.
3667 static int idetape_chrdev_ioctl (struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg)
3669 struct ide_tape_obj *tape = ide_tape_f(file);
3670 ide_drive_t *drive = tape->drive;
3674 int block_offset = 0, position = tape->first_frame_position;
3675 void __user *argp = (void __user *)arg;
3677 #if IDETAPE_DEBUG_LOG
3678 if (tape->debug_level >= 3)
3679 printk(KERN_INFO "ide-tape: Reached idetape_chrdev_ioctl, "
3681 #endif /* IDETAPE_DEBUG_LOG */
3683 tape->restart_speed_control_req = 1;
3684 if (tape->chrdev_direction == idetape_direction_write) {
3685 idetape_empty_write_pipeline(drive);
3686 idetape_flush_tape_buffers(drive);
3688 if (cmd == MTIOCGET || cmd == MTIOCPOS) {
3689 block_offset = idetape_pipeline_size(drive) / (tape->tape_block_size * tape->user_bs_factor);
3690 if ((position = idetape_read_position(drive)) < 0)
3695 if (copy_from_user(&mtop, argp, sizeof (struct mtop)))
3697 return (idetape_mtioctop(drive,mtop.mt_op,mtop.mt_count));
3699 memset(&mtget, 0, sizeof (struct mtget));
3700 mtget.mt_type = MT_ISSCSI2;
3701 mtget.mt_blkno = position / tape->user_bs_factor - block_offset;
3702 mtget.mt_dsreg = ((tape->tape_block_size * tape->user_bs_factor) << MT_ST_BLKSIZE_SHIFT) & MT_ST_BLKSIZE_MASK;
3703 if (tape->drv_write_prot) {
3704 mtget.mt_gstat |= GMT_WR_PROT(0xffffffff);
3706 if (copy_to_user(argp, &mtget, sizeof(struct mtget)))
3710 mtpos.mt_blkno = position / tape->user_bs_factor - block_offset;
3711 if (copy_to_user(argp, &mtpos, sizeof(struct mtpos)))
3715 if (tape->chrdev_direction == idetape_direction_read)
3716 idetape_discard_read_pipeline(drive, 1);
3717 return idetape_blkdev_ioctl(drive, cmd, arg);
3721 static void idetape_get_blocksize_from_block_descriptor(ide_drive_t *drive);
3724 * Our character device open function.
3726 static int idetape_chrdev_open (struct inode *inode, struct file *filp)
3728 unsigned int minor = iminor(inode), i = minor & ~0xc0;
3730 idetape_tape_t *tape;
3735 * We really want to do nonseekable_open(inode, filp); here, but some
3736 * versions of tar incorrectly call lseek on tapes and bail out if that
3737 * fails. So we disallow pread() and pwrite(), but permit lseeks.
3739 filp->f_mode &= ~(FMODE_PREAD | FMODE_PWRITE);
3741 #if IDETAPE_DEBUG_LOG
3742 printk(KERN_INFO "ide-tape: Reached idetape_chrdev_open\n");
3743 #endif /* IDETAPE_DEBUG_LOG */
3745 if (i >= MAX_HWIFS * MAX_DRIVES)
3748 if (!(tape = ide_tape_chrdev_get(i)))
3751 drive = tape->drive;
3753 filp->private_data = tape;
3755 if (test_and_set_bit(IDETAPE_BUSY, &tape->flags)) {
3760 retval = idetape_wait_ready(drive, 60 * HZ);
3762 clear_bit(IDETAPE_BUSY, &tape->flags);
3763 printk(KERN_ERR "ide-tape: %s: drive not ready\n", tape->name);
3767 idetape_read_position(drive);
3768 if (!test_bit(IDETAPE_ADDRESS_VALID, &tape->flags))
3769 (void)idetape_rewind_tape(drive);
3771 if (tape->chrdev_direction != idetape_direction_read)
3772 clear_bit(IDETAPE_PIPELINE_ERROR, &tape->flags);
3774 /* Read block size and write protect status from drive. */
3775 idetape_get_blocksize_from_block_descriptor(drive);
3777 /* Set write protect flag if device is opened as read-only. */
3778 if ((filp->f_flags & O_ACCMODE) == O_RDONLY)
3779 tape->write_prot = 1;
3781 tape->write_prot = tape->drv_write_prot;
3783 /* Make sure drive isn't write protected if user wants to write. */
3784 if (tape->write_prot) {
3785 if ((filp->f_flags & O_ACCMODE) == O_WRONLY ||
3786 (filp->f_flags & O_ACCMODE) == O_RDWR) {
3787 clear_bit(IDETAPE_BUSY, &tape->flags);
3794 * Lock the tape drive door so user can't eject.
3796 if (tape->chrdev_direction == idetape_direction_none) {
3797 if (idetape_create_prevent_cmd(drive, &pc, 1)) {
3798 if (!idetape_queue_pc_tail(drive, &pc)) {
3799 if (tape->door_locked != DOOR_EXPLICITLY_LOCKED)
3800 tape->door_locked = DOOR_LOCKED;
3804 idetape_restart_speed_control(drive);
3805 tape->restart_speed_control_req = 0;
3813 static void idetape_write_release (ide_drive_t *drive, unsigned int minor)
3815 idetape_tape_t *tape = drive->driver_data;
3817 idetape_empty_write_pipeline(drive);
3818 tape->merge_stage = __idetape_kmalloc_stage(tape, 1, 0);
3819 if (tape->merge_stage != NULL) {
3820 idetape_pad_zeros(drive, tape->tape_block_size * (tape->user_bs_factor - 1));
3821 __idetape_kfree_stage(tape->merge_stage);
3822 tape->merge_stage = NULL;
3824 idetape_write_filemark(drive);
3825 idetape_flush_tape_buffers(drive);
3826 idetape_flush_tape_buffers(drive);
3830 * Our character device release function.
3832 static int idetape_chrdev_release (struct inode *inode, struct file *filp)
3834 struct ide_tape_obj *tape = ide_tape_f(filp);
3835 ide_drive_t *drive = tape->drive;
3837 unsigned int minor = iminor(inode);
3840 tape = drive->driver_data;
3841 #if IDETAPE_DEBUG_LOG
3842 if (tape->debug_level >= 3)
3843 printk(KERN_INFO "ide-tape: Reached idetape_chrdev_release\n");
3844 #endif /* IDETAPE_DEBUG_LOG */
3846 if (tape->chrdev_direction == idetape_direction_write)
3847 idetape_write_release(drive, minor);
3848 if (tape->chrdev_direction == idetape_direction_read) {
3850 idetape_discard_read_pipeline(drive, 1);
3852 idetape_wait_for_pipeline(drive);
3854 if (tape->cache_stage != NULL) {
3855 __idetape_kfree_stage(tape->cache_stage);
3856 tape->cache_stage = NULL;
3858 if (minor < 128 && test_bit(IDETAPE_MEDIUM_PRESENT, &tape->flags))
3859 (void) idetape_rewind_tape(drive);
3860 if (tape->chrdev_direction == idetape_direction_none) {
3861 if (tape->door_locked == DOOR_LOCKED) {
3862 if (idetape_create_prevent_cmd(drive, &pc, 0)) {
3863 if (!idetape_queue_pc_tail(drive, &pc))
3864 tape->door_locked = DOOR_UNLOCKED;
3868 clear_bit(IDETAPE_BUSY, &tape->flags);
3875 * idetape_identify_device is called to check the contents of the
3876 * ATAPI IDENTIFY command results. We return:
3878 * 1 If the tape can be supported by us, based on the information
3881 * 0 If this tape driver is not currently supported by us.
3883 static int idetape_identify_device (ide_drive_t *drive)
3885 struct idetape_id_gcw gcw;
3886 struct hd_driveid *id = drive->id;
3888 if (drive->id_read == 0)
3891 *((unsigned short *) &gcw) = id->config;
3893 #if IDETAPE_DEBUG_INFO
3894 printk(KERN_INFO "ide-tape: Dumping ATAPI Identify Device tape parameters\n");
3895 printk(KERN_INFO "ide-tape: Protocol Type: ");
3896 switch (gcw.protocol) {
3897 case 0: case 1: printk("ATA\n");break;
3898 case 2: printk("ATAPI\n");break;
3899 case 3: printk("Reserved (Unknown to ide-tape)\n");break;
3901 printk(KERN_INFO "ide-tape: Device Type: %x - ",gcw.device_type);
3902 switch (gcw.device_type) {
3903 case 0: printk("Direct-access Device\n");break;
3904 case 1: printk("Streaming Tape Device\n");break;
3905 case 2: case 3: case 4: printk("Reserved\n");break;
3906 case 5: printk("CD-ROM Device\n");break;
3907 case 6: printk("Reserved\n");
3908 case 7: printk("Optical memory Device\n");break;
3909 case 0x1f: printk("Unknown or no Device type\n");break;
3910 default: printk("Reserved\n");
3912 printk(KERN_INFO "ide-tape: Removable: %s",gcw.removable ? "Yes\n":"No\n");
3913 printk(KERN_INFO "ide-tape: Command Packet DRQ Type: ");
3914 switch (gcw.drq_type) {
3915 case 0: printk("Microprocessor DRQ\n");break;
3916 case 1: printk("Interrupt DRQ\n");break;
3917 case 2: printk("Accelerated DRQ\n");break;
3918 case 3: printk("Reserved\n");break;
3920 printk(KERN_INFO "ide-tape: Command Packet Size: ");
3921 switch (gcw.packet_size) {
3922 case 0: printk("12 bytes\n");break;
3923 case 1: printk("16 bytes\n");break;
3924 default: printk("Reserved\n");break;
3926 #endif /* IDETAPE_DEBUG_INFO */
3928 /* Check that we can support this device */
3930 if (gcw.protocol !=2 )
3931 printk(KERN_ERR "ide-tape: Protocol is not ATAPI\n");
3932 else if (gcw.device_type != 1)
3933 printk(KERN_ERR "ide-tape: Device type is not set to tape\n");
3934 else if (!gcw.removable)
3935 printk(KERN_ERR "ide-tape: The removable flag is not set\n");
3936 else if (gcw.packet_size != 0) {
3937 printk(KERN_ERR "ide-tape: Packet size is not 12 bytes long\n");
3938 if (gcw.packet_size == 1)
3939 printk(KERN_ERR "ide-tape: Sorry, padding to 16 bytes is still not supported\n");
3946 * Use INQUIRY to get the firmware revision
3948 static void idetape_get_inquiry_results (ide_drive_t *drive)
3951 idetape_tape_t *tape = drive->driver_data;
3953 idetape_inquiry_result_t *inquiry;
3955 idetape_create_inquiry_cmd(&pc);
3956 if (idetape_queue_pc_tail(drive, &pc)) {
3957 printk(KERN_ERR "ide-tape: %s: can't get INQUIRY results\n", tape->name);
3960 inquiry = (idetape_inquiry_result_t *) pc.buffer;
3961 memcpy(tape->vendor_id, inquiry->vendor_id, 8);
3962 memcpy(tape->product_id, inquiry->product_id, 16);
3963 memcpy(tape->firmware_revision, inquiry->revision_level, 4);
3964 ide_fixstring(tape->vendor_id, 10, 0);
3965 ide_fixstring(tape->product_id, 18, 0);
3966 ide_fixstring(tape->firmware_revision, 6, 0);
3967 r = tape->firmware_revision;
3968 if (*(r + 1) == '.')
3969 tape->firmware_revision_num = (*r - '0') * 100 + (*(r + 2) - '0') * 10 + *(r + 3) - '0';
3970 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);
3974 * idetape_get_mode_sense_results asks the tape about its various
3975 * parameters. In particular, we will adjust our data transfer buffer
3976 * size to the recommended value as returned by the tape.
3978 static void idetape_get_mode_sense_results (ide_drive_t *drive)
3980 idetape_tape_t *tape = drive->driver_data;
3982 idetape_mode_parameter_header_t *header;
3983 idetape_capabilities_page_t *capabilities;
3985 idetape_create_mode_sense_cmd(&pc, IDETAPE_CAPABILITIES_PAGE);
3986 if (idetape_queue_pc_tail(drive, &pc)) {
3987 printk(KERN_ERR "ide-tape: Can't get tape parameters - assuming some default values\n");
3988 tape->tape_block_size = 512;
3989 tape->capabilities.ctl = 52;
3990 tape->capabilities.speed = 450;
3991 tape->capabilities.buffer_size = 6 * 52;
3994 header = (idetape_mode_parameter_header_t *) pc.buffer;
3995 capabilities = (idetape_capabilities_page_t *) (pc.buffer + sizeof(idetape_mode_parameter_header_t) + header->bdl);
3997 capabilities->max_speed = ntohs(capabilities->max_speed);
3998 capabilities->ctl = ntohs(capabilities->ctl);
3999 capabilities->speed = ntohs(capabilities->speed);
4000 capabilities->buffer_size = ntohs(capabilities->buffer_size);
4002 if (!capabilities->speed) {
4003 printk(KERN_INFO "ide-tape: %s: overriding capabilities->speed (assuming 650KB/sec)\n", drive->name);
4004 capabilities->speed = 650;
4006 if (!capabilities->max_speed) {
4007 printk(KERN_INFO "ide-tape: %s: overriding capabilities->max_speed (assuming 650KB/sec)\n", drive->name);
4008 capabilities->max_speed = 650;
4011 tape->capabilities = *capabilities; /* Save us a copy */
4012 if (capabilities->blk512)
4013 tape->tape_block_size = 512;
4014 else if (capabilities->blk1024)
4015 tape->tape_block_size = 1024;
4017 #if IDETAPE_DEBUG_INFO
4018 printk(KERN_INFO "ide-tape: Dumping the results of the MODE SENSE packet command\n");
4019 printk(KERN_INFO "ide-tape: Mode Parameter Header:\n");
4020 printk(KERN_INFO "ide-tape: Mode Data Length - %d\n",header->mode_data_length);
4021 printk(KERN_INFO "ide-tape: Medium Type - %d\n",header->medium_type);
4022 printk(KERN_INFO "ide-tape: Device Specific Parameter - %d\n",header->dsp);
4023 printk(KERN_INFO "ide-tape: Block Descriptor Length - %d\n",header->bdl);
4025 printk(KERN_INFO "ide-tape: Capabilities and Mechanical Status Page:\n");
4026 printk(KERN_INFO "ide-tape: Page code - %d\n",capabilities->page_code);
4027 printk(KERN_INFO "ide-tape: Page length - %d\n",capabilities->page_length);
4028 printk(KERN_INFO "ide-tape: Read only - %s\n",capabilities->ro ? "Yes":"No");
4029 printk(KERN_INFO "ide-tape: Supports reverse space - %s\n",capabilities->sprev ? "Yes":"No");
4030 printk(KERN_INFO "ide-tape: Supports erase initiated formatting - %s\n",capabilities->efmt ? "Yes":"No");
4031 printk(KERN_INFO "ide-tape: Supports QFA two Partition format - %s\n",capabilities->qfa ? "Yes":"No");
4032 printk(KERN_INFO "ide-tape: Supports locking the medium - %s\n",capabilities->lock ? "Yes":"No");
4033 printk(KERN_INFO "ide-tape: The volume is currently locked - %s\n",capabilities->locked ? "Yes":"No");
4034 printk(KERN_INFO "ide-tape: The device defaults in the prevent state - %s\n",capabilities->prevent ? "Yes":"No");
4035 printk(KERN_INFO "ide-tape: Supports ejecting the medium - %s\n",capabilities->eject ? "Yes":"No");
4036 printk(KERN_INFO "ide-tape: Supports error correction - %s\n",capabilities->ecc ? "Yes":"No");
4037 printk(KERN_INFO "ide-tape: Supports data compression - %s\n",capabilities->cmprs ? "Yes":"No");
4038 printk(KERN_INFO "ide-tape: Supports 512 bytes block size - %s\n",capabilities->blk512 ? "Yes":"No");
4039 printk(KERN_INFO "ide-tape: Supports 1024 bytes block size - %s\n",capabilities->blk1024 ? "Yes":"No");
4040 printk(KERN_INFO "ide-tape: Supports 32768 bytes block size / Restricted byte count for PIO transfers - %s\n",capabilities->blk32768 ? "Yes":"No");
4041 printk(KERN_INFO "ide-tape: Maximum supported speed in KBps - %d\n",capabilities->max_speed);
4042 printk(KERN_INFO "ide-tape: Continuous transfer limits in blocks - %d\n",capabilities->ctl);
4043 printk(KERN_INFO "ide-tape: Current speed in KBps - %d\n",capabilities->speed);
4044 printk(KERN_INFO "ide-tape: Buffer size - %d\n",capabilities->buffer_size*512);
4045 #endif /* IDETAPE_DEBUG_INFO */
4049 * ide_get_blocksize_from_block_descriptor does a mode sense page 0 with block descriptor
4050 * and if it succeeds sets the tape block size with the reported value
4052 static void idetape_get_blocksize_from_block_descriptor(ide_drive_t *drive)
4055 idetape_tape_t *tape = drive->driver_data;
4057 idetape_mode_parameter_header_t *header;
4058 idetape_parameter_block_descriptor_t *block_descrp;
4060 idetape_create_mode_sense_cmd(&pc, IDETAPE_BLOCK_DESCRIPTOR);
4061 if (idetape_queue_pc_tail(drive, &pc)) {
4062 printk(KERN_ERR "ide-tape: Can't get block descriptor\n");
4063 if (tape->tape_block_size == 0) {
4064 printk(KERN_WARNING "ide-tape: Cannot deal with zero block size, assume 32k\n");
4065 tape->tape_block_size = 32768;
4069 header = (idetape_mode_parameter_header_t *) pc.buffer;
4070 block_descrp = (idetape_parameter_block_descriptor_t *) (pc.buffer + sizeof(idetape_mode_parameter_header_t));
4071 tape->tape_block_size =( block_descrp->length[0]<<16) + (block_descrp->length[1]<<8) + block_descrp->length[2];
4072 tape->drv_write_prot = (header->dsp & 0x80) >> 7;
4074 #if IDETAPE_DEBUG_INFO
4075 printk(KERN_INFO "ide-tape: Adjusted block size - %d\n", tape->tape_block_size);
4076 #endif /* IDETAPE_DEBUG_INFO */
4079 #ifdef CONFIG_IDE_PROC_FS
4080 static void idetape_add_settings (ide_drive_t *drive)
4082 idetape_tape_t *tape = drive->driver_data;
4085 * drive setting name read/write data type min max mul_factor div_factor data pointer set function
4087 ide_add_setting(drive, "buffer", SETTING_READ, TYPE_SHORT, 0, 0xffff, 1, 2, &tape->capabilities.buffer_size, NULL);
4088 ide_add_setting(drive, "pipeline_min", SETTING_RW, TYPE_INT, 1, 0xffff, tape->stage_size / 1024, 1, &tape->min_pipeline, NULL);
4089 ide_add_setting(drive, "pipeline", SETTING_RW, TYPE_INT, 1, 0xffff, tape->stage_size / 1024, 1, &tape->max_stages, NULL);
4090 ide_add_setting(drive, "pipeline_max", SETTING_RW, TYPE_INT, 1, 0xffff, tape->stage_size / 1024, 1, &tape->max_pipeline, NULL);
4091 ide_add_setting(drive, "pipeline_used", SETTING_READ, TYPE_INT, 0, 0xffff, tape->stage_size / 1024, 1, &tape->nr_stages, NULL);
4092 ide_add_setting(drive, "pipeline_pending", SETTING_READ, TYPE_INT, 0, 0xffff, tape->stage_size / 1024, 1, &tape->nr_pending_stages, NULL);
4093 ide_add_setting(drive, "speed", SETTING_READ, TYPE_SHORT, 0, 0xffff, 1, 1, &tape->capabilities.speed, NULL);
4094 ide_add_setting(drive, "stage", SETTING_READ, TYPE_INT, 0, 0xffff, 1, 1024, &tape->stage_size, NULL);
4095 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);
4096 ide_add_setting(drive, "dsc_overlap", SETTING_RW, TYPE_BYTE, 0, 1, 1, 1, &drive->dsc_overlap, NULL);
4097 ide_add_setting(drive, "pipeline_head_speed_c",SETTING_READ, TYPE_INT, 0, 0xffff, 1, 1, &tape->controlled_pipeline_head_speed, NULL);
4098 ide_add_setting(drive, "pipeline_head_speed_u",SETTING_READ, TYPE_INT, 0, 0xffff, 1, 1, &tape->uncontrolled_pipeline_head_speed,NULL);
4099 ide_add_setting(drive, "avg_speed", SETTING_READ, TYPE_INT, 0, 0xffff, 1, 1, &tape->avg_speed, NULL);
4100 ide_add_setting(drive, "debug_level", SETTING_RW, TYPE_INT, 0, 0xffff, 1, 1, &tape->debug_level, NULL);
4103 static inline void idetape_add_settings(ide_drive_t *drive) { ; }
4107 * ide_setup is called to:
4109 * 1. Initialize our various state variables.
4110 * 2. Ask the tape for its capabilities.
4111 * 3. Allocate a buffer which will be used for data
4112 * transfer. The buffer size is chosen based on
4113 * the recommendation which we received in step (2).
4115 * Note that at this point ide.c already assigned us an irq, so that
4116 * we can queue requests here and wait for their completion.
4118 static void idetape_setup (ide_drive_t *drive, idetape_tape_t *tape, int minor)
4120 unsigned long t1, tmid, tn, t;
4122 struct idetape_id_gcw gcw;
4126 spin_lock_init(&tape->spinlock);
4127 drive->dsc_overlap = 1;
4128 if (drive->hwif->host_flags & IDE_HFLAG_NO_DSC) {
4129 printk(KERN_INFO "ide-tape: %s: disabling DSC overlap\n",
4131 drive->dsc_overlap = 0;
4133 /* Seagate Travan drives do not support DSC overlap. */
4134 if (strstr(drive->id->model, "Seagate STT3401"))
4135 drive->dsc_overlap = 0;
4136 tape->minor = minor;
4137 tape->name[0] = 'h';
4138 tape->name[1] = 't';
4139 tape->name[2] = '0' + minor;
4140 tape->chrdev_direction = idetape_direction_none;
4141 tape->pc = tape->pc_stack;
4142 tape->max_insert_speed = 10000;
4143 tape->speed_control = 1;
4144 *((unsigned short *) &gcw) = drive->id->config;
4145 if (gcw.drq_type == 1)
4146 set_bit(IDETAPE_DRQ_INTERRUPT, &tape->flags);
4148 tape->min_pipeline = tape->max_pipeline = tape->max_stages = 10;
4150 idetape_get_inquiry_results(drive);
4151 idetape_get_mode_sense_results(drive);
4152 idetape_get_blocksize_from_block_descriptor(drive);
4153 tape->user_bs_factor = 1;
4154 tape->stage_size = tape->capabilities.ctl * tape->tape_block_size;
4155 while (tape->stage_size > 0xffff) {
4156 printk(KERN_NOTICE "ide-tape: decreasing stage size\n");
4157 tape->capabilities.ctl /= 2;
4158 tape->stage_size = tape->capabilities.ctl * tape->tape_block_size;
4160 stage_size = tape->stage_size;
4161 tape->pages_per_stage = stage_size / PAGE_SIZE;
4162 if (stage_size % PAGE_SIZE) {
4163 tape->pages_per_stage++;
4164 tape->excess_bh_size = PAGE_SIZE - stage_size % PAGE_SIZE;
4168 * Select the "best" DSC read/write polling frequency
4169 * and pipeline size.
4171 speed = max(tape->capabilities.speed, tape->capabilities.max_speed);
4173 tape->max_stages = speed * 1000 * 10 / tape->stage_size;
4176 * Limit memory use for pipeline to 10% of physical memory
4179 if (tape->max_stages * tape->stage_size > si.totalram * si.mem_unit / 10)
4180 tape->max_stages = si.totalram * si.mem_unit / (10 * tape->stage_size);
4181 tape->max_stages = min(tape->max_stages, IDETAPE_MAX_PIPELINE_STAGES);
4182 tape->min_pipeline = min(tape->max_stages, IDETAPE_MIN_PIPELINE_STAGES);
4183 tape->max_pipeline = min(tape->max_stages * 2, IDETAPE_MAX_PIPELINE_STAGES);
4184 if (tape->max_stages == 0)
4185 tape->max_stages = tape->min_pipeline = tape->max_pipeline = 1;
4187 t1 = (tape->stage_size * HZ) / (speed * 1000);
4188 tmid = (tape->capabilities.buffer_size * 32 * HZ) / (speed * 125);
4189 tn = (IDETAPE_FIFO_THRESHOLD * tape->stage_size * HZ) / (speed * 1000);
4191 if (tape->max_stages)
4197 * Ensure that the number we got makes sense; limit
4198 * it within IDETAPE_DSC_RW_MIN and IDETAPE_DSC_RW_MAX.
4200 tape->best_dsc_rw_frequency = max_t(unsigned long, min_t(unsigned long, t, IDETAPE_DSC_RW_MAX), IDETAPE_DSC_RW_MIN);
4201 printk(KERN_INFO "ide-tape: %s <-> %s: %dKBps, %d*%dkB buffer, "
4202 "%dkB pipeline, %lums tDSC%s\n",
4203 drive->name, tape->name, tape->capabilities.speed,
4204 (tape->capabilities.buffer_size * 512) / tape->stage_size,
4205 tape->stage_size / 1024,
4206 tape->max_stages * tape->stage_size / 1024,
4207 tape->best_dsc_rw_frequency * 1000 / HZ,
4208 drive->using_dma ? ", DMA":"");
4210 idetape_add_settings(drive);
4213 static void ide_tape_remove(ide_drive_t *drive)
4215 idetape_tape_t *tape = drive->driver_data;
4217 ide_proc_unregister_driver(drive, tape->driver);
4219 ide_unregister_region(tape->disk);
4224 static void ide_tape_release(struct kref *kref)
4226 struct ide_tape_obj *tape = to_ide_tape(kref);
4227 ide_drive_t *drive = tape->drive;
4228 struct gendisk *g = tape->disk;
4230 BUG_ON(tape->first_stage != NULL || tape->merge_stage_size);
4232 drive->dsc_overlap = 0;
4233 drive->driver_data = NULL;
4234 device_destroy(idetape_sysfs_class, MKDEV(IDETAPE_MAJOR, tape->minor));
4235 device_destroy(idetape_sysfs_class, MKDEV(IDETAPE_MAJOR, tape->minor + 128));
4236 idetape_devs[tape->minor] = NULL;
4237 g->private_data = NULL;
4242 #ifdef CONFIG_IDE_PROC_FS
4243 static int proc_idetape_read_name
4244 (char *page, char **start, off_t off, int count, int *eof, void *data)
4246 ide_drive_t *drive = (ide_drive_t *) data;
4247 idetape_tape_t *tape = drive->driver_data;
4251 len = sprintf(out, "%s\n", tape->name);
4252 PROC_IDE_READ_RETURN(page, start, off, count, eof, len);
4255 static ide_proc_entry_t idetape_proc[] = {
4256 { "capacity", S_IFREG|S_IRUGO, proc_ide_read_capacity, NULL },
4257 { "name", S_IFREG|S_IRUGO, proc_idetape_read_name, NULL },
4258 { NULL, 0, NULL, NULL }
4262 static int ide_tape_probe(ide_drive_t *);
4264 static ide_driver_t idetape_driver = {
4266 .owner = THIS_MODULE,
4268 .bus = &ide_bus_type,
4270 .probe = ide_tape_probe,
4271 .remove = ide_tape_remove,
4272 .version = IDETAPE_VERSION,
4274 .supports_dsc_overlap = 1,
4275 .do_request = idetape_do_request,
4276 .end_request = idetape_end_request,
4277 .error = __ide_error,
4278 .abort = __ide_abort,
4279 #ifdef CONFIG_IDE_PROC_FS
4280 .proc = idetape_proc,
4285 * Our character device supporting functions, passed to register_chrdev.
4287 static const struct file_operations idetape_fops = {
4288 .owner = THIS_MODULE,
4289 .read = idetape_chrdev_read,
4290 .write = idetape_chrdev_write,
4291 .ioctl = idetape_chrdev_ioctl,
4292 .open = idetape_chrdev_open,
4293 .release = idetape_chrdev_release,
4296 static int idetape_open(struct inode *inode, struct file *filp)
4298 struct gendisk *disk = inode->i_bdev->bd_disk;
4299 struct ide_tape_obj *tape;
4301 if (!(tape = ide_tape_get(disk)))
4307 static int idetape_release(struct inode *inode, struct file *filp)
4309 struct gendisk *disk = inode->i_bdev->bd_disk;
4310 struct ide_tape_obj *tape = ide_tape_g(disk);
4317 static int idetape_ioctl(struct inode *inode, struct file *file,
4318 unsigned int cmd, unsigned long arg)
4320 struct block_device *bdev = inode->i_bdev;
4321 struct ide_tape_obj *tape = ide_tape_g(bdev->bd_disk);
4322 ide_drive_t *drive = tape->drive;
4323 int err = generic_ide_ioctl(drive, file, bdev, cmd, arg);
4325 err = idetape_blkdev_ioctl(drive, cmd, arg);
4329 static struct block_device_operations idetape_block_ops = {
4330 .owner = THIS_MODULE,
4331 .open = idetape_open,
4332 .release = idetape_release,
4333 .ioctl = idetape_ioctl,
4336 static int ide_tape_probe(ide_drive_t *drive)
4338 idetape_tape_t *tape;
4342 if (!strstr("ide-tape", drive->driver_req))
4344 if (!drive->present)
4346 if (drive->media != ide_tape)
4348 if (!idetape_identify_device (drive)) {
4349 printk(KERN_ERR "ide-tape: %s: not supported by this version of ide-tape\n", drive->name);
4353 printk("ide-tape: passing drive %s to ide-scsi emulation.\n", drive->name);
4356 if (strstr(drive->id->model, "OnStream DI-")) {
4357 printk(KERN_WARNING "ide-tape: Use drive %s with ide-scsi emulation and osst.\n", drive->name);
4358 printk(KERN_WARNING "ide-tape: OnStream support will be removed soon from ide-tape!\n");
4360 tape = kzalloc(sizeof (idetape_tape_t), GFP_KERNEL);
4362 printk(KERN_ERR "ide-tape: %s: Can't allocate a tape structure\n", drive->name);
4366 g = alloc_disk(1 << PARTN_BITS);
4370 ide_init_disk(g, drive);
4372 ide_proc_register_driver(drive, &idetape_driver);
4374 kref_init(&tape->kref);
4376 tape->drive = drive;
4377 tape->driver = &idetape_driver;
4380 g->private_data = &tape->driver;
4382 drive->driver_data = tape;
4384 mutex_lock(&idetape_ref_mutex);
4385 for (minor = 0; idetape_devs[minor]; minor++)
4387 idetape_devs[minor] = tape;
4388 mutex_unlock(&idetape_ref_mutex);
4390 idetape_setup(drive, tape, minor);
4392 device_create(idetape_sysfs_class, &drive->gendev,
4393 MKDEV(IDETAPE_MAJOR, minor), "%s", tape->name);
4394 device_create(idetape_sysfs_class, &drive->gendev,
4395 MKDEV(IDETAPE_MAJOR, minor + 128), "n%s", tape->name);
4397 g->fops = &idetape_block_ops;
4398 ide_register_region(g);
4408 MODULE_DESCRIPTION("ATAPI Streaming TAPE Driver");
4409 MODULE_LICENSE("GPL");
4411 static void __exit idetape_exit (void)
4413 driver_unregister(&idetape_driver.gen_driver);
4414 class_destroy(idetape_sysfs_class);
4415 unregister_chrdev(IDETAPE_MAJOR, "ht");
4418 static int __init idetape_init(void)
4421 idetape_sysfs_class = class_create(THIS_MODULE, "ide_tape");
4422 if (IS_ERR(idetape_sysfs_class)) {
4423 idetape_sysfs_class = NULL;
4424 printk(KERN_ERR "Unable to create sysfs class for ide tapes\n");
4429 if (register_chrdev(IDETAPE_MAJOR, "ht", &idetape_fops)) {
4430 printk(KERN_ERR "ide-tape: Failed to register character device interface\n");
4432 goto out_free_class;
4435 error = driver_register(&idetape_driver.gen_driver);
4437 goto out_free_driver;
4442 driver_unregister(&idetape_driver.gen_driver);
4444 class_destroy(idetape_sysfs_class);
4449 MODULE_ALIAS("ide:*m-tape*");
4450 module_init(idetape_init);
4451 module_exit(idetape_exit);
4452 MODULE_ALIAS_CHARDEV_MAJOR(IDETAPE_MAJOR);