2 * IDE ATAPI streaming tape driver.
4 * Copyright (C) 1995-1999 Gadi Oxman <gadio@netvision.net.il>
5 * Copyright (C) 2003-2005 Bartlomiej Zolnierkiewicz
7 * This driver was constructed as a student project in the software laboratory
8 * of the faculty of electrical engineering in the Technion - Israel's
9 * Institute Of Technology, with the guide of Avner Lottem and Dr. Ilana David.
11 * It is hereby placed under the terms of the GNU general public license.
12 * (See linux/COPYING).
14 * For a historical changelog see
15 * Documentation/ide/ChangeLog.ide-tape.1995-2002
18 #define IDETAPE_VERSION "1.19"
20 #include <linux/module.h>
21 #include <linux/types.h>
22 #include <linux/string.h>
23 #include <linux/kernel.h>
24 #include <linux/delay.h>
25 #include <linux/timer.h>
27 #include <linux/interrupt.h>
28 #include <linux/jiffies.h>
29 #include <linux/major.h>
30 #include <linux/errno.h>
31 #include <linux/genhd.h>
32 #include <linux/slab.h>
33 #include <linux/pci.h>
34 #include <linux/ide.h>
35 #include <linux/smp_lock.h>
36 #include <linux/completion.h>
37 #include <linux/bitops.h>
38 #include <linux/mutex.h>
40 #include <asm/byteorder.h>
42 #include <asm/uaccess.h>
44 #include <asm/unaligned.h>
49 typedef struct os_partition_s {
53 __u32 first_frame_addr;
54 __u32 last_frame_addr;
61 typedef struct os_dat_entry_s {
71 #define OS_DAT_FLAGS_DATA (0xc)
72 #define OS_DAT_FLAGS_MARK (0x1)
74 typedef struct os_dat_s {
79 os_dat_entry_t dat_list[16];
82 #include <linux/mtio.h>
84 /**************************** Tunable parameters *****************************/
88 * Pipelined mode parameters.
90 * We try to use the minimum number of stages which is enough to
91 * keep the tape constantly streaming. To accomplish that, we implement
92 * a feedback loop around the maximum number of stages:
94 * We start from MIN maximum stages (we will not even use MIN stages
95 * if we don't need them), increment it by RATE*(MAX-MIN)
96 * whenever we sense that the pipeline is empty, until we reach
97 * the optimum value or until we reach MAX.
99 * Setting the following parameter to 0 is illegal: the pipelined mode
100 * cannot be disabled (calculate_speeds() divides by tape->max_stages.)
102 #define IDETAPE_MIN_PIPELINE_STAGES 1
103 #define IDETAPE_MAX_PIPELINE_STAGES 400
104 #define IDETAPE_INCREASE_STAGES_RATE 20
107 * The following are used to debug the driver:
109 * Setting IDETAPE_DEBUG_LOG to 1 will log driver flow control.
111 * Setting them to 0 will restore normal operation mode:
113 * 1. Disable logging normal successful operations.
114 * 2. Disable self-sanity checks.
115 * 3. Errors will still be logged, of course.
117 * All the #if DEBUG code will be removed some day, when the driver
118 * is verified to be stable enough. This will make it much more
121 #define IDETAPE_DEBUG_LOG 0
124 * After each failed packet command we issue a request sense command
125 * and retry the packet command IDETAPE_MAX_PC_RETRIES times.
127 * Setting IDETAPE_MAX_PC_RETRIES to 0 will disable retries.
129 #define IDETAPE_MAX_PC_RETRIES 3
132 * With each packet command, we allocate a buffer of
133 * IDETAPE_PC_BUFFER_SIZE bytes. This is used for several packet
134 * commands (Not for READ/WRITE commands).
136 #define IDETAPE_PC_BUFFER_SIZE 256
139 * In various places in the driver, we need to allocate storage
140 * for packet commands and requests, which will remain valid while
141 * we leave the driver to wait for an interrupt or a timeout event.
143 #define IDETAPE_PC_STACK (10 + IDETAPE_MAX_PC_RETRIES)
146 * Some drives (for example, Seagate STT3401A Travan) require a very long
147 * timeout, because they don't return an interrupt or clear their busy bit
148 * until after the command completes (even retension commands).
150 #define IDETAPE_WAIT_CMD (900*HZ)
153 * The following parameter is used to select the point in the internal
154 * tape fifo in which we will start to refill the buffer. Decreasing
155 * the following parameter will improve the system's latency and
156 * interactive response, while using a high value might improve system
159 #define IDETAPE_FIFO_THRESHOLD 2
162 * DSC polling parameters.
164 * Polling for DSC (a single bit in the status register) is a very
165 * important function in ide-tape. There are two cases in which we
168 * 1. Before a read/write packet command, to ensure that we
169 * can transfer data from/to the tape's data buffers, without
170 * causing an actual media access. In case the tape is not
171 * ready yet, we take out our request from the device
172 * request queue, so that ide.c will service requests from
173 * the other device on the same interface meanwhile.
175 * 2. After the successful initialization of a "media access
176 * packet command", which is a command which can take a long
177 * time to complete (it can be several seconds or even an hour).
179 * Again, we postpone our request in the middle to free the bus
180 * for the other device. The polling frequency here should be
181 * lower than the read/write frequency since those media access
182 * commands are slow. We start from a "fast" frequency -
183 * IDETAPE_DSC_MA_FAST (one second), and if we don't receive DSC
184 * after IDETAPE_DSC_MA_THRESHOLD (5 minutes), we switch it to a
185 * lower frequency - IDETAPE_DSC_MA_SLOW (1 minute).
187 * We also set a timeout for the timer, in case something goes wrong.
188 * The timeout should be longer then the maximum execution time of a
195 #define IDETAPE_DSC_RW_MIN 5*HZ/100 /* 50 msec */
196 #define IDETAPE_DSC_RW_MAX 40*HZ/100 /* 400 msec */
197 #define IDETAPE_DSC_RW_TIMEOUT 2*60*HZ /* 2 minutes */
198 #define IDETAPE_DSC_MA_FAST 2*HZ /* 2 seconds */
199 #define IDETAPE_DSC_MA_THRESHOLD 5*60*HZ /* 5 minutes */
200 #define IDETAPE_DSC_MA_SLOW 30*HZ /* 30 seconds */
201 #define IDETAPE_DSC_MA_TIMEOUT 2*60*60*HZ /* 2 hours */
203 /*************************** End of tunable parameters ***********************/
206 * Read/Write error simulation
208 #define SIMULATE_ERRORS 0
211 * For general magnetic tape device compatibility.
214 idetape_direction_none,
215 idetape_direction_read,
216 idetape_direction_write
217 } idetape_chrdev_direction_t;
222 struct idetape_bh *b_reqnext;
227 * Our view of a packet command.
229 typedef struct idetape_packet_command_s {
230 u8 c[12]; /* Actual packet bytes */
231 int retries; /* On each retry, we increment retries */
232 int error; /* Error code */
233 int request_transfer; /* Bytes to transfer */
234 int actually_transferred; /* Bytes actually transferred */
235 int buffer_size; /* Size of our data buffer */
236 struct idetape_bh *bh;
239 u8 *buffer; /* Data buffer */
240 u8 *current_position; /* Pointer into the above buffer */
241 ide_startstop_t (*callback) (ide_drive_t *); /* Called when this packet command is completed */
242 u8 pc_buffer[IDETAPE_PC_BUFFER_SIZE]; /* Temporary buffer */
243 unsigned long flags; /* Status/Action bit flags: long for set_bit */
247 * Packet command flag bits.
249 /* Set when an error is considered normal - We won't retry */
251 /* 1 When polling for DSC on a media access command */
252 #define PC_WAIT_FOR_DSC 1
253 /* 1 when we prefer to use DMA if possible */
254 #define PC_DMA_RECOMMENDED 2
255 /* 1 while DMA in progress */
256 #define PC_DMA_IN_PROGRESS 3
257 /* 1 when encountered problem during DMA */
258 #define PC_DMA_ERROR 4
266 unsigned page_code :6; /* Page code - Should be 0x30 */
267 unsigned reserved1_6 :1;
269 __u8 page_length; /* Page Length - Should be 2 */
272 unsigned play32_5 :1;
273 unsigned reserved2_23 :2;
274 unsigned record32 :1;
275 unsigned record32_5 :1;
276 unsigned reserved2_6 :1;
278 } idetape_block_size_page_t;
283 typedef struct idetape_stage_s {
284 struct request rq; /* The corresponding request */
285 struct idetape_bh *bh; /* The data buffers */
286 struct idetape_stage_s *next; /* Pointer to the next stage */
290 * Most of our global data which we need to save even as we leave the
291 * driver due to an interrupt or a timer event is stored in a variable
292 * of type idetape_tape_t, defined below.
294 typedef struct ide_tape_obj {
296 ide_driver_t *driver;
297 struct gendisk *disk;
301 * Since a typical character device operation requires more
302 * than one packet command, we provide here enough memory
303 * for the maximum of interconnected packet commands.
304 * The packet commands are stored in the circular array pc_stack.
305 * pc_stack_index points to the last used entry, and warps around
306 * to the start when we get to the last array entry.
308 * pc points to the current processed packet command.
310 * failed_pc points to the last failed packet command, or contains
311 * NULL if we do not need to retry any packet command. This is
312 * required since an additional packet command is needed before the
313 * retry, to get detailed information on what went wrong.
315 /* Current packet command */
317 /* Last failed packet command */
318 idetape_pc_t *failed_pc;
319 /* Packet command stack */
320 idetape_pc_t pc_stack[IDETAPE_PC_STACK];
321 /* Next free packet command storage space */
323 struct request rq_stack[IDETAPE_PC_STACK];
324 /* We implement a circular array */
328 * DSC polling variables.
330 * While polling for DSC we use postponed_rq to postpone the
331 * current request so that ide.c will be able to service
332 * pending requests on the other device. Note that at most
333 * we will have only one DSC (usually data transfer) request
334 * in the device request queue. Additional requests can be
335 * queued in our internal pipeline, but they will be visible
336 * to ide.c only one at a time.
338 struct request *postponed_rq;
339 /* The time in which we started polling for DSC */
340 unsigned long dsc_polling_start;
341 /* Timer used to poll for dsc */
342 struct timer_list dsc_timer;
343 /* Read/Write dsc polling frequency */
344 unsigned long best_dsc_rw_frequency;
345 /* The current polling frequency */
346 unsigned long dsc_polling_frequency;
347 /* Maximum waiting time */
348 unsigned long dsc_timeout;
351 * Read position information
355 unsigned int first_frame_position;
356 unsigned int last_frame_position;
357 unsigned int blocks_in_buffer;
360 * Last error information
362 u8 sense_key, asc, ascq;
365 * Character device operation
370 /* Current character device data transfer direction */
371 idetape_chrdev_direction_t chrdev_direction;
376 /* Usually 512 or 1024 bytes */
377 unsigned short tape_block_size;
380 /* Copy of the tape's Capabilities and Mechanical Page */
384 * Active data transfer request parameters.
386 * At most, there is only one ide-tape originated data transfer
387 * request in the device request queue. This allows ide.c to
388 * easily service requests from the other device when we
389 * postpone our active request. In the pipelined operation
390 * mode, we use our internal pipeline structure to hold
391 * more data requests.
393 * The data buffer size is chosen based on the tape's
396 /* Pointer to the request which is waiting in the device request queue */
397 struct request *active_data_request;
398 /* Data buffer size (chosen based on the tape's recommendation */
400 idetape_stage_t *merge_stage;
401 int merge_stage_size;
402 struct idetape_bh *bh;
407 * Pipeline parameters.
409 * To accomplish non-pipelined mode, we simply set the following
410 * variables to zero (or NULL, where appropriate).
412 /* Number of currently used stages */
414 /* Number of pending stages */
415 int nr_pending_stages;
416 /* We will not allocate more than this number of stages */
417 int max_stages, min_pipeline, max_pipeline;
418 /* The first stage which will be removed from the pipeline */
419 idetape_stage_t *first_stage;
420 /* The currently active stage */
421 idetape_stage_t *active_stage;
422 /* Will be serviced after the currently active request */
423 idetape_stage_t *next_stage;
424 /* New requests will be added to the pipeline here */
425 idetape_stage_t *last_stage;
426 /* Optional free stage which we can use */
427 idetape_stage_t *cache_stage;
429 /* Wasted space in each stage */
432 /* Status/Action flags: long for set_bit */
434 /* protects the ide-tape queue */
438 * Measures average tape speed
440 unsigned long avg_time;
446 char firmware_revision[6];
447 int firmware_revision_num;
449 /* the door is currently locked */
451 /* the tape hardware is write protected */
453 /* the tape is write protected (hardware or opened as read-only) */
457 * Limit the number of times a request can
458 * be postponed, to avoid an infinite postpone
461 /* request postpone count limit */
465 * Measures number of frames:
467 * 1. written/read to/from the driver pipeline (pipeline_head).
468 * 2. written/read to/from the tape buffers (idetape_bh).
469 * 3. written/read by the tape to/from the media (tape_head).
477 * Speed control at the tape buffers input/output
479 unsigned long insert_time;
482 int max_insert_speed;
483 int measure_insert_time;
486 * Measure tape still time, in milliseconds
488 unsigned long tape_still_time_begin;
492 * Speed regulation negative feedback loop
495 int pipeline_head_speed;
496 int controlled_pipeline_head_speed;
497 int uncontrolled_pipeline_head_speed;
498 int controlled_last_pipeline_head;
499 int uncontrolled_last_pipeline_head;
500 unsigned long uncontrolled_pipeline_head_time;
501 unsigned long controlled_pipeline_head_time;
502 int controlled_previous_pipeline_head;
503 int uncontrolled_previous_pipeline_head;
504 unsigned long controlled_previous_head_time;
505 unsigned long uncontrolled_previous_head_time;
506 int restart_speed_control_req;
509 * Debug_level determines amount of debugging output;
510 * can be changed using /proc/ide/hdx/settings
511 * 0 : almost no debugging output
512 * 1 : 0+output errors only
513 * 2 : 1+output all sensekey/asc
514 * 3 : 2+follow all chrdev related procedures
515 * 4 : 3+follow all procedures
516 * 5 : 4+include pc_stack rq_stack info
517 * 6 : 5+USE_COUNT updates
522 static DEFINE_MUTEX(idetape_ref_mutex);
524 static struct class *idetape_sysfs_class;
526 #define to_ide_tape(obj) container_of(obj, struct ide_tape_obj, kref)
528 #define ide_tape_g(disk) \
529 container_of((disk)->private_data, struct ide_tape_obj, driver)
531 static struct ide_tape_obj *ide_tape_get(struct gendisk *disk)
533 struct ide_tape_obj *tape = NULL;
535 mutex_lock(&idetape_ref_mutex);
536 tape = ide_tape_g(disk);
538 kref_get(&tape->kref);
539 mutex_unlock(&idetape_ref_mutex);
543 static void ide_tape_release(struct kref *);
545 static void ide_tape_put(struct ide_tape_obj *tape)
547 mutex_lock(&idetape_ref_mutex);
548 kref_put(&tape->kref, ide_tape_release);
549 mutex_unlock(&idetape_ref_mutex);
555 #define DOOR_UNLOCKED 0
556 #define DOOR_LOCKED 1
557 #define DOOR_EXPLICITLY_LOCKED 2
560 * Tape flag bits values.
562 #define IDETAPE_IGNORE_DSC 0
563 #define IDETAPE_ADDRESS_VALID 1 /* 0 When the tape position is unknown */
564 #define IDETAPE_BUSY 2 /* Device already opened */
565 #define IDETAPE_PIPELINE_ERROR 3 /* Error detected in a pipeline stage */
566 #define IDETAPE_DETECT_BS 4 /* Attempt to auto-detect the current user block size */
567 #define IDETAPE_FILEMARK 5 /* Currently on a filemark */
568 #define IDETAPE_DRQ_INTERRUPT 6 /* DRQ interrupt device */
569 #define IDETAPE_READ_ERROR 7
570 #define IDETAPE_PIPELINE_ACTIVE 8 /* pipeline active */
571 /* 0 = no tape is loaded, so we don't rewind after ejecting */
572 #define IDETAPE_MEDIUM_PRESENT 9
575 * Supported ATAPI tape drives packet commands
577 #define IDETAPE_TEST_UNIT_READY_CMD 0x00
578 #define IDETAPE_REWIND_CMD 0x01
579 #define IDETAPE_REQUEST_SENSE_CMD 0x03
580 #define IDETAPE_READ_CMD 0x08
581 #define IDETAPE_WRITE_CMD 0x0a
582 #define IDETAPE_WRITE_FILEMARK_CMD 0x10
583 #define IDETAPE_SPACE_CMD 0x11
584 #define IDETAPE_INQUIRY_CMD 0x12
585 #define IDETAPE_ERASE_CMD 0x19
586 #define IDETAPE_MODE_SENSE_CMD 0x1a
587 #define IDETAPE_MODE_SELECT_CMD 0x15
588 #define IDETAPE_LOAD_UNLOAD_CMD 0x1b
589 #define IDETAPE_PREVENT_CMD 0x1e
590 #define IDETAPE_LOCATE_CMD 0x2b
591 #define IDETAPE_READ_POSITION_CMD 0x34
592 #define IDETAPE_READ_BUFFER_CMD 0x3c
593 #define IDETAPE_SET_SPEED_CMD 0xbb
596 * Some defines for the READ BUFFER command
598 #define IDETAPE_RETRIEVE_FAULTY_BLOCK 6
601 * Some defines for the SPACE command
603 #define IDETAPE_SPACE_OVER_FILEMARK 1
604 #define IDETAPE_SPACE_TO_EOD 3
607 * Some defines for the LOAD UNLOAD command
609 #define IDETAPE_LU_LOAD_MASK 1
610 #define IDETAPE_LU_RETENSION_MASK 2
611 #define IDETAPE_LU_EOT_MASK 4
614 * Special requests for our block device strategy routine.
616 * In order to service a character device command, we add special
617 * requests to the tail of our block device request queue and wait
618 * for their completion.
622 REQ_IDETAPE_PC1 = (1 << 0), /* packet command (first stage) */
623 REQ_IDETAPE_PC2 = (1 << 1), /* packet command (second stage) */
624 REQ_IDETAPE_READ = (1 << 2),
625 REQ_IDETAPE_WRITE = (1 << 3),
626 REQ_IDETAPE_READ_BUFFER = (1 << 4),
630 * Error codes which are returned in rq->errors to the higher part
633 #define IDETAPE_ERROR_GENERAL 101
634 #define IDETAPE_ERROR_FILEMARK 102
635 #define IDETAPE_ERROR_EOD 103
638 * The following is used to format the general configuration word of
639 * the ATAPI IDENTIFY DEVICE command.
641 struct idetape_id_gcw {
642 unsigned packet_size :2; /* Packet Size */
643 unsigned reserved234 :3; /* Reserved */
644 unsigned drq_type :2; /* Command packet DRQ type */
645 unsigned removable :1; /* Removable media */
646 unsigned device_type :5; /* Device type */
647 unsigned reserved13 :1; /* Reserved */
648 unsigned protocol :2; /* Protocol type */
652 * READ POSITION packet command - Data Format (From Table 6-57)
655 unsigned reserved0_10 :2; /* Reserved */
656 unsigned bpu :1; /* Block Position Unknown */
657 unsigned reserved0_543 :3; /* Reserved */
658 unsigned eop :1; /* End Of Partition */
659 unsigned bop :1; /* Beginning Of Partition */
660 u8 partition; /* Partition Number */
661 u8 reserved2, reserved3; /* Reserved */
662 u32 first_block; /* First Block Location */
663 u32 last_block; /* Last Block Location (Optional) */
664 u8 reserved12; /* Reserved */
665 u8 blocks_in_buffer[3]; /* Blocks In Buffer - (Optional) */
666 u32 bytes_in_buffer; /* Bytes In Buffer (Optional) */
667 } idetape_read_position_result_t;
670 * Follows structures which are related to the SELECT SENSE / MODE SENSE
671 * packet commands. Those packet commands are still not supported
674 #define IDETAPE_BLOCK_DESCRIPTOR 0
675 #define IDETAPE_CAPABILITIES_PAGE 0x2a
676 #define IDETAPE_PARAMTR_PAGE 0x2b /* Onstream DI-x0 only */
677 #define IDETAPE_BLOCK_SIZE_PAGE 0x30
678 #define IDETAPE_BUFFER_FILLING_PAGE 0x33
681 * Run time configurable parameters.
684 int dsc_rw_frequency;
685 int dsc_media_access_frequency;
690 * The variables below are used for the character device interface.
691 * Additional state variables are defined in our ide_drive_t structure.
693 static struct ide_tape_obj * idetape_devs[MAX_HWIFS * MAX_DRIVES];
695 #define ide_tape_f(file) ((file)->private_data)
697 static struct ide_tape_obj *ide_tape_chrdev_get(unsigned int i)
699 struct ide_tape_obj *tape = NULL;
701 mutex_lock(&idetape_ref_mutex);
702 tape = idetape_devs[i];
704 kref_get(&tape->kref);
705 mutex_unlock(&idetape_ref_mutex);
710 * Function declarations
713 static int idetape_chrdev_release (struct inode *inode, struct file *filp);
714 static void idetape_write_release (ide_drive_t *drive, unsigned int minor);
717 * Too bad. The drive wants to send us data which we are not ready to accept.
718 * Just throw it away.
720 static void idetape_discard_data (ide_drive_t *drive, unsigned int bcount)
723 (void) HWIF(drive)->INB(IDE_DATA_REG);
726 static void idetape_input_buffers (ide_drive_t *drive, idetape_pc_t *pc, unsigned int bcount)
728 struct idetape_bh *bh = pc->bh;
733 printk(KERN_ERR "ide-tape: bh == NULL in "
734 "idetape_input_buffers\n");
735 idetape_discard_data(drive, bcount);
738 count = min((unsigned int)(bh->b_size - atomic_read(&bh->b_count)), bcount);
739 HWIF(drive)->atapi_input_bytes(drive, bh->b_data + atomic_read(&bh->b_count), count);
741 atomic_add(count, &bh->b_count);
742 if (atomic_read(&bh->b_count) == bh->b_size) {
745 atomic_set(&bh->b_count, 0);
751 static void idetape_output_buffers (ide_drive_t *drive, idetape_pc_t *pc, unsigned int bcount)
753 struct idetape_bh *bh = pc->bh;
758 printk(KERN_ERR "ide-tape: bh == NULL in "
759 "idetape_output_buffers\n");
762 count = min((unsigned int)pc->b_count, (unsigned int)bcount);
763 HWIF(drive)->atapi_output_bytes(drive, pc->b_data, count);
766 pc->b_count -= count;
768 pc->bh = bh = bh->b_reqnext;
770 pc->b_data = bh->b_data;
771 pc->b_count = atomic_read(&bh->b_count);
777 static void idetape_update_buffers (idetape_pc_t *pc)
779 struct idetape_bh *bh = pc->bh;
781 unsigned int bcount = pc->actually_transferred;
783 if (test_bit(PC_WRITING, &pc->flags))
787 printk(KERN_ERR "ide-tape: bh == NULL in "
788 "idetape_update_buffers\n");
791 count = min((unsigned int)bh->b_size, (unsigned int)bcount);
792 atomic_set(&bh->b_count, count);
793 if (atomic_read(&bh->b_count) == bh->b_size)
801 * idetape_next_pc_storage returns a pointer to a place in which we can
802 * safely store a packet command, even though we intend to leave the
803 * driver. A storage space for a maximum of IDETAPE_PC_STACK packet
804 * commands is allocated at initialization time.
806 static idetape_pc_t *idetape_next_pc_storage (ide_drive_t *drive)
808 idetape_tape_t *tape = drive->driver_data;
810 #if IDETAPE_DEBUG_LOG
811 if (tape->debug_level >= 5)
812 printk(KERN_INFO "ide-tape: pc_stack_index=%d\n",
813 tape->pc_stack_index);
814 #endif /* IDETAPE_DEBUG_LOG */
815 if (tape->pc_stack_index == IDETAPE_PC_STACK)
816 tape->pc_stack_index=0;
817 return (&tape->pc_stack[tape->pc_stack_index++]);
821 * idetape_next_rq_storage is used along with idetape_next_pc_storage.
822 * Since we queue packet commands in the request queue, we need to
823 * allocate a request, along with the allocation of a packet command.
826 /**************************************************************
828 * This should get fixed to use kmalloc(.., GFP_ATOMIC) *
829 * followed later on by kfree(). -ml *
831 **************************************************************/
833 static struct request *idetape_next_rq_storage (ide_drive_t *drive)
835 idetape_tape_t *tape = drive->driver_data;
837 #if IDETAPE_DEBUG_LOG
838 if (tape->debug_level >= 5)
839 printk(KERN_INFO "ide-tape: rq_stack_index=%d\n",
840 tape->rq_stack_index);
841 #endif /* IDETAPE_DEBUG_LOG */
842 if (tape->rq_stack_index == IDETAPE_PC_STACK)
843 tape->rq_stack_index=0;
844 return (&tape->rq_stack[tape->rq_stack_index++]);
848 * idetape_init_pc initializes a packet command.
850 static void idetape_init_pc (idetape_pc_t *pc)
852 memset(pc->c, 0, 12);
855 pc->request_transfer = 0;
856 pc->buffer = pc->pc_buffer;
857 pc->buffer_size = IDETAPE_PC_BUFFER_SIZE;
863 * called on each failed packet command retry to analyze the request sense. We
864 * currently do not utilize this information.
866 static void idetape_analyze_error(ide_drive_t *drive, u8 *sense)
868 idetape_tape_t *tape = drive->driver_data;
869 idetape_pc_t *pc = tape->failed_pc;
871 tape->sense_key = sense[2] & 0xF;
872 tape->asc = sense[12];
873 tape->ascq = sense[13];
874 #if IDETAPE_DEBUG_LOG
876 * Without debugging, we only log an error if we decided to give up
879 if (tape->debug_level >= 1)
880 printk(KERN_INFO "ide-tape: pc = %x, sense key = %x, "
881 "asc = %x, ascq = %x\n",
882 pc->c[0], tape->sense_key,
883 tape->asc, tape->ascq);
884 #endif /* IDETAPE_DEBUG_LOG */
886 /* Correct pc->actually_transferred by asking the tape. */
887 if (test_bit(PC_DMA_ERROR, &pc->flags)) {
888 pc->actually_transferred = pc->request_transfer -
889 tape->tape_block_size *
890 ntohl(get_unaligned((u32 *)&sense[3]));
891 idetape_update_buffers(pc);
895 * If error was the result of a zero-length read or write command,
896 * with sense key=5, asc=0x22, ascq=0, let it slide. Some drives
897 * (i.e. Seagate STT3401A Travan) don't support 0-length read/writes.
899 if ((pc->c[0] == IDETAPE_READ_CMD || pc->c[0] == IDETAPE_WRITE_CMD)
901 && pc->c[4] == 0 && pc->c[3] == 0 && pc->c[2] == 0) {
902 if (tape->sense_key == 5) {
903 /* don't report an error, everything's ok */
905 /* don't retry read/write */
906 set_bit(PC_ABORT, &pc->flags);
909 if (pc->c[0] == IDETAPE_READ_CMD && (sense[2] & 0x80)) {
910 pc->error = IDETAPE_ERROR_FILEMARK;
911 set_bit(PC_ABORT, &pc->flags);
913 if (pc->c[0] == IDETAPE_WRITE_CMD) {
914 if ((sense[2] & 0x40) || (tape->sense_key == 0xd
915 && tape->asc == 0x0 && tape->ascq == 0x2)) {
916 pc->error = IDETAPE_ERROR_EOD;
917 set_bit(PC_ABORT, &pc->flags);
920 if (pc->c[0] == IDETAPE_READ_CMD || pc->c[0] == IDETAPE_WRITE_CMD) {
921 if (tape->sense_key == 8) {
922 pc->error = IDETAPE_ERROR_EOD;
923 set_bit(PC_ABORT, &pc->flags);
925 if (!test_bit(PC_ABORT, &pc->flags) &&
926 pc->actually_transferred)
927 pc->retries = IDETAPE_MAX_PC_RETRIES + 1;
932 * idetape_active_next_stage will declare the next stage as "active".
934 static void idetape_active_next_stage (ide_drive_t *drive)
936 idetape_tape_t *tape = drive->driver_data;
937 idetape_stage_t *stage = tape->next_stage;
938 struct request *rq = &stage->rq;
940 #if IDETAPE_DEBUG_LOG
941 if (tape->debug_level >= 4)
942 printk(KERN_INFO "ide-tape: Reached idetape_active_next_stage\n");
943 #endif /* IDETAPE_DEBUG_LOG */
945 printk(KERN_ERR "ide-tape: bug: Trying to activate a non existing stage\n");
949 rq->rq_disk = tape->disk;
951 rq->special = (void *)stage->bh;
952 tape->active_data_request = rq;
953 tape->active_stage = stage;
954 tape->next_stage = stage->next;
958 * idetape_increase_max_pipeline_stages is a part of the feedback
959 * loop which tries to find the optimum number of stages. In the
960 * feedback loop, we are starting from a minimum maximum number of
961 * stages, and if we sense that the pipeline is empty, we try to
962 * increase it, until we reach the user compile time memory limit.
964 static void idetape_increase_max_pipeline_stages (ide_drive_t *drive)
966 idetape_tape_t *tape = drive->driver_data;
967 int increase = (tape->max_pipeline - tape->min_pipeline) / 10;
969 #if IDETAPE_DEBUG_LOG
970 if (tape->debug_level >= 4)
971 printk (KERN_INFO "ide-tape: Reached idetape_increase_max_pipeline_stages\n");
972 #endif /* IDETAPE_DEBUG_LOG */
974 tape->max_stages += max(increase, 1);
975 tape->max_stages = max(tape->max_stages, tape->min_pipeline);
976 tape->max_stages = min(tape->max_stages, tape->max_pipeline);
980 * idetape_kfree_stage calls kfree to completely free a stage, along with
981 * its related buffers.
983 static void __idetape_kfree_stage (idetape_stage_t *stage)
985 struct idetape_bh *prev_bh, *bh = stage->bh;
989 if (bh->b_data != NULL) {
990 size = (int) bh->b_size;
992 free_page((unsigned long) bh->b_data);
994 bh->b_data += PAGE_SIZE;
1004 static void idetape_kfree_stage (idetape_tape_t *tape, idetape_stage_t *stage)
1006 __idetape_kfree_stage(stage);
1010 * idetape_remove_stage_head removes tape->first_stage from the pipeline.
1011 * The caller should avoid race conditions.
1013 static void idetape_remove_stage_head (ide_drive_t *drive)
1015 idetape_tape_t *tape = drive->driver_data;
1016 idetape_stage_t *stage;
1018 #if IDETAPE_DEBUG_LOG
1019 if (tape->debug_level >= 4)
1020 printk(KERN_INFO "ide-tape: Reached idetape_remove_stage_head\n");
1021 #endif /* IDETAPE_DEBUG_LOG */
1022 if (tape->first_stage == NULL) {
1023 printk(KERN_ERR "ide-tape: bug: tape->first_stage is NULL\n");
1026 if (tape->active_stage == tape->first_stage) {
1027 printk(KERN_ERR "ide-tape: bug: Trying to free our active pipeline stage\n");
1030 stage = tape->first_stage;
1031 tape->first_stage = stage->next;
1032 idetape_kfree_stage(tape, stage);
1034 if (tape->first_stage == NULL) {
1035 tape->last_stage = NULL;
1036 if (tape->next_stage != NULL)
1037 printk(KERN_ERR "ide-tape: bug: tape->next_stage != NULL\n");
1038 if (tape->nr_stages)
1039 printk(KERN_ERR "ide-tape: bug: nr_stages should be 0 now\n");
1044 * This will free all the pipeline stages starting from new_last_stage->next
1045 * to the end of the list, and point tape->last_stage to new_last_stage.
1047 static void idetape_abort_pipeline(ide_drive_t *drive,
1048 idetape_stage_t *new_last_stage)
1050 idetape_tape_t *tape = drive->driver_data;
1051 idetape_stage_t *stage = new_last_stage->next;
1052 idetape_stage_t *nstage;
1054 #if IDETAPE_DEBUG_LOG
1055 if (tape->debug_level >= 4)
1056 printk(KERN_INFO "ide-tape: %s: idetape_abort_pipeline called\n", tape->name);
1059 nstage = stage->next;
1060 idetape_kfree_stage(tape, stage);
1062 --tape->nr_pending_stages;
1066 new_last_stage->next = NULL;
1067 tape->last_stage = new_last_stage;
1068 tape->next_stage = NULL;
1072 * idetape_end_request is used to finish servicing a request, and to
1073 * insert a pending pipeline request into the main device queue.
1075 static int idetape_end_request(ide_drive_t *drive, int uptodate, int nr_sects)
1077 struct request *rq = HWGROUP(drive)->rq;
1078 idetape_tape_t *tape = drive->driver_data;
1079 unsigned long flags;
1081 int remove_stage = 0;
1082 idetape_stage_t *active_stage;
1084 #if IDETAPE_DEBUG_LOG
1085 if (tape->debug_level >= 4)
1086 printk(KERN_INFO "ide-tape: Reached idetape_end_request\n");
1087 #endif /* IDETAPE_DEBUG_LOG */
1090 case 0: error = IDETAPE_ERROR_GENERAL; break;
1091 case 1: error = 0; break;
1092 default: error = uptodate;
1096 tape->failed_pc = NULL;
1098 if (!blk_special_request(rq)) {
1099 ide_end_request(drive, uptodate, nr_sects);
1103 spin_lock_irqsave(&tape->spinlock, flags);
1105 /* The request was a pipelined data transfer request */
1106 if (tape->active_data_request == rq) {
1107 active_stage = tape->active_stage;
1108 tape->active_stage = NULL;
1109 tape->active_data_request = NULL;
1110 tape->nr_pending_stages--;
1111 if (rq->cmd[0] & REQ_IDETAPE_WRITE) {
1114 set_bit(IDETAPE_PIPELINE_ERROR, &tape->flags);
1115 if (error == IDETAPE_ERROR_EOD)
1116 idetape_abort_pipeline(drive, active_stage);
1118 } else if (rq->cmd[0] & REQ_IDETAPE_READ) {
1119 if (error == IDETAPE_ERROR_EOD) {
1120 set_bit(IDETAPE_PIPELINE_ERROR, &tape->flags);
1121 idetape_abort_pipeline(drive, active_stage);
1124 if (tape->next_stage != NULL) {
1125 idetape_active_next_stage(drive);
1128 * Insert the next request into the request queue.
1130 (void) ide_do_drive_cmd(drive, tape->active_data_request, ide_end);
1131 } else if (!error) {
1132 idetape_increase_max_pipeline_stages(drive);
1135 ide_end_drive_cmd(drive, 0, 0);
1136 // blkdev_dequeue_request(rq);
1137 // drive->rq = NULL;
1138 // end_that_request_last(rq);
1141 idetape_remove_stage_head(drive);
1142 if (tape->active_data_request == NULL)
1143 clear_bit(IDETAPE_PIPELINE_ACTIVE, &tape->flags);
1144 spin_unlock_irqrestore(&tape->spinlock, flags);
1148 static ide_startstop_t idetape_request_sense_callback (ide_drive_t *drive)
1150 idetape_tape_t *tape = drive->driver_data;
1152 #if IDETAPE_DEBUG_LOG
1153 if (tape->debug_level >= 4)
1154 printk(KERN_INFO "ide-tape: Reached idetape_request_sense_callback\n");
1155 #endif /* IDETAPE_DEBUG_LOG */
1156 if (!tape->pc->error) {
1157 idetape_analyze_error(drive, tape->pc->buffer);
1158 idetape_end_request(drive, 1, 0);
1160 printk(KERN_ERR "ide-tape: Error in REQUEST SENSE itself - Aborting request!\n");
1161 idetape_end_request(drive, 0, 0);
1166 static void idetape_create_request_sense_cmd (idetape_pc_t *pc)
1168 idetape_init_pc(pc);
1169 pc->c[0] = IDETAPE_REQUEST_SENSE_CMD;
1171 pc->request_transfer = 20;
1172 pc->callback = &idetape_request_sense_callback;
1175 static void idetape_init_rq(struct request *rq, u8 cmd)
1177 memset(rq, 0, sizeof(*rq));
1178 rq->cmd_type = REQ_TYPE_SPECIAL;
1183 * idetape_queue_pc_head generates a new packet command request in front
1184 * of the request queue, before the current request, so that it will be
1185 * processed immediately, on the next pass through the driver.
1187 * idetape_queue_pc_head is called from the request handling part of
1188 * the driver (the "bottom" part). Safe storage for the request should
1189 * be allocated with idetape_next_pc_storage and idetape_next_rq_storage
1190 * before calling idetape_queue_pc_head.
1192 * Memory for those requests is pre-allocated at initialization time, and
1193 * is limited to IDETAPE_PC_STACK requests. We assume that we have enough
1194 * space for the maximum possible number of inter-dependent packet commands.
1196 * The higher level of the driver - The ioctl handler and the character
1197 * device handling functions should queue request to the lower level part
1198 * and wait for their completion using idetape_queue_pc_tail or
1199 * idetape_queue_rw_tail.
1201 static void idetape_queue_pc_head (ide_drive_t *drive, idetape_pc_t *pc,struct request *rq)
1203 struct ide_tape_obj *tape = drive->driver_data;
1205 idetape_init_rq(rq, REQ_IDETAPE_PC1);
1206 rq->buffer = (char *) pc;
1207 rq->rq_disk = tape->disk;
1208 (void) ide_do_drive_cmd(drive, rq, ide_preempt);
1212 * idetape_retry_pc is called when an error was detected during the
1213 * last packet command. We queue a request sense packet command in
1214 * the head of the request list.
1216 static ide_startstop_t idetape_retry_pc (ide_drive_t *drive)
1218 idetape_tape_t *tape = drive->driver_data;
1222 (void)drive->hwif->INB(IDE_ERROR_REG);
1223 pc = idetape_next_pc_storage(drive);
1224 rq = idetape_next_rq_storage(drive);
1225 idetape_create_request_sense_cmd(pc);
1226 set_bit(IDETAPE_IGNORE_DSC, &tape->flags);
1227 idetape_queue_pc_head(drive, pc, rq);
1232 * idetape_postpone_request postpones the current request so that
1233 * ide.c will be able to service requests from another device on
1234 * the same hwgroup while we are polling for DSC.
1236 static void idetape_postpone_request (ide_drive_t *drive)
1238 idetape_tape_t *tape = drive->driver_data;
1240 #if IDETAPE_DEBUG_LOG
1241 if (tape->debug_level >= 4)
1242 printk(KERN_INFO "ide-tape: idetape_postpone_request\n");
1244 tape->postponed_rq = HWGROUP(drive)->rq;
1245 ide_stall_queue(drive, tape->dsc_polling_frequency);
1249 * idetape_pc_intr is the usual interrupt handler which will be called
1250 * during a packet command. We will transfer some of the data (as
1251 * requested by the drive) and will re-point interrupt handler to us.
1252 * When data transfer is finished, we will act according to the
1253 * algorithm described before idetape_issue_packet_command.
1256 static ide_startstop_t idetape_pc_intr (ide_drive_t *drive)
1258 ide_hwif_t *hwif = drive->hwif;
1259 idetape_tape_t *tape = drive->driver_data;
1260 idetape_pc_t *pc = tape->pc;
1263 static int error_sim_count = 0;
1268 #if IDETAPE_DEBUG_LOG
1269 if (tape->debug_level >= 4)
1270 printk(KERN_INFO "ide-tape: Reached idetape_pc_intr "
1271 "interrupt handler\n");
1272 #endif /* IDETAPE_DEBUG_LOG */
1274 /* Clear the interrupt */
1275 stat = hwif->INB(IDE_STATUS_REG);
1277 if (test_bit(PC_DMA_IN_PROGRESS, &pc->flags)) {
1278 if (hwif->ide_dma_end(drive) || (stat & ERR_STAT)) {
1280 * A DMA error is sometimes expected. For example,
1281 * if the tape is crossing a filemark during a
1282 * READ command, it will issue an irq and position
1283 * itself before the filemark, so that only a partial
1284 * data transfer will occur (which causes the DMA
1285 * error). In that case, we will later ask the tape
1286 * how much bytes of the original request were
1287 * actually transferred (we can't receive that
1288 * information from the DMA engine on most chipsets).
1292 * On the contrary, a DMA error is never expected;
1293 * it usually indicates a hardware error or abort.
1294 * If the tape crosses a filemark during a READ
1295 * command, it will issue an irq and position itself
1296 * after the filemark (not before). Only a partial
1297 * data transfer will occur, but no DMA error.
1300 set_bit(PC_DMA_ERROR, &pc->flags);
1302 pc->actually_transferred = pc->request_transfer;
1303 idetape_update_buffers(pc);
1305 #if IDETAPE_DEBUG_LOG
1306 if (tape->debug_level >= 4)
1307 printk(KERN_INFO "ide-tape: DMA finished\n");
1308 #endif /* IDETAPE_DEBUG_LOG */
1311 /* No more interrupts */
1312 if ((stat & DRQ_STAT) == 0) {
1313 #if IDETAPE_DEBUG_LOG
1314 if (tape->debug_level >= 2)
1315 printk(KERN_INFO "ide-tape: Packet command completed, %d bytes transferred\n", pc->actually_transferred);
1316 #endif /* IDETAPE_DEBUG_LOG */
1317 clear_bit(PC_DMA_IN_PROGRESS, &pc->flags);
1322 if ((pc->c[0] == IDETAPE_WRITE_CMD ||
1323 pc->c[0] == IDETAPE_READ_CMD) &&
1324 (++error_sim_count % 100) == 0) {
1325 printk(KERN_INFO "ide-tape: %s: simulating error\n",
1330 if ((stat & ERR_STAT) && pc->c[0] == IDETAPE_REQUEST_SENSE_CMD)
1332 if ((stat & ERR_STAT) || test_bit(PC_DMA_ERROR, &pc->flags)) {
1333 /* Error detected */
1334 #if IDETAPE_DEBUG_LOG
1335 if (tape->debug_level >= 1)
1336 printk(KERN_INFO "ide-tape: %s: I/O error\n",
1338 #endif /* IDETAPE_DEBUG_LOG */
1339 if (pc->c[0] == IDETAPE_REQUEST_SENSE_CMD) {
1340 printk(KERN_ERR "ide-tape: I/O error in request sense command\n");
1341 return ide_do_reset(drive);
1343 #if IDETAPE_DEBUG_LOG
1344 if (tape->debug_level >= 1)
1345 printk(KERN_INFO "ide-tape: [cmd %x]: check condition\n", pc->c[0]);
1347 /* Retry operation */
1348 return idetape_retry_pc(drive);
1351 if (test_bit(PC_WAIT_FOR_DSC, &pc->flags) &&
1352 (stat & SEEK_STAT) == 0) {
1353 /* Media access command */
1354 tape->dsc_polling_start = jiffies;
1355 tape->dsc_polling_frequency = IDETAPE_DSC_MA_FAST;
1356 tape->dsc_timeout = jiffies + IDETAPE_DSC_MA_TIMEOUT;
1357 /* Allow ide.c to handle other requests */
1358 idetape_postpone_request(drive);
1361 if (tape->failed_pc == pc)
1362 tape->failed_pc = NULL;
1363 /* Command finished - Call the callback function */
1364 return pc->callback(drive);
1366 if (test_and_clear_bit(PC_DMA_IN_PROGRESS, &pc->flags)) {
1367 printk(KERN_ERR "ide-tape: The tape wants to issue more "
1368 "interrupts in DMA mode\n");
1369 printk(KERN_ERR "ide-tape: DMA disabled, reverting to PIO\n");
1371 return ide_do_reset(drive);
1373 /* Get the number of bytes to transfer on this interrupt. */
1374 bcount = (hwif->INB(IDE_BCOUNTH_REG) << 8) |
1375 hwif->INB(IDE_BCOUNTL_REG);
1377 ireason = hwif->INB(IDE_IREASON_REG);
1380 printk(KERN_ERR "ide-tape: CoD != 0 in idetape_pc_intr\n");
1381 return ide_do_reset(drive);
1383 if (((ireason & IO) == IO) == test_bit(PC_WRITING, &pc->flags)) {
1384 /* Hopefully, we will never get here */
1385 printk(KERN_ERR "ide-tape: We wanted to %s, ",
1386 (ireason & IO) ? "Write" : "Read");
1387 printk(KERN_ERR "ide-tape: but the tape wants us to %s !\n",
1388 (ireason & IO) ? "Read" : "Write");
1389 return ide_do_reset(drive);
1391 if (!test_bit(PC_WRITING, &pc->flags)) {
1392 /* Reading - Check that we have enough space */
1393 temp = pc->actually_transferred + bcount;
1394 if (temp > pc->request_transfer) {
1395 if (temp > pc->buffer_size) {
1396 printk(KERN_ERR "ide-tape: The tape wants to send us more data than expected - discarding data\n");
1397 idetape_discard_data(drive, bcount);
1398 ide_set_handler(drive, &idetape_pc_intr, IDETAPE_WAIT_CMD, NULL);
1401 #if IDETAPE_DEBUG_LOG
1402 if (tape->debug_level >= 2)
1403 printk(KERN_NOTICE "ide-tape: The tape wants to send us more data than expected - allowing transfer\n");
1404 #endif /* IDETAPE_DEBUG_LOG */
1407 if (test_bit(PC_WRITING, &pc->flags)) {
1409 idetape_output_buffers(drive, pc, bcount);
1411 /* Write the current buffer */
1412 hwif->atapi_output_bytes(drive, pc->current_position,
1416 idetape_input_buffers(drive, pc, bcount);
1418 /* Read the current buffer */
1419 hwif->atapi_input_bytes(drive, pc->current_position,
1422 /* Update the current position */
1423 pc->actually_transferred += bcount;
1424 pc->current_position += bcount;
1425 #if IDETAPE_DEBUG_LOG
1426 if (tape->debug_level >= 2)
1427 printk(KERN_INFO "ide-tape: [cmd %x] transferred %d bytes "
1428 "on that interrupt\n", pc->c[0], bcount);
1430 /* And set the interrupt handler again */
1431 ide_set_handler(drive, &idetape_pc_intr, IDETAPE_WAIT_CMD, NULL);
1436 * Packet Command Interface
1438 * The current Packet Command is available in tape->pc, and will not
1439 * change until we finish handling it. Each packet command is associated
1440 * with a callback function that will be called when the command is
1443 * The handling will be done in three stages:
1445 * 1. idetape_issue_packet_command will send the packet command to the
1446 * drive, and will set the interrupt handler to idetape_pc_intr.
1448 * 2. On each interrupt, idetape_pc_intr will be called. This step
1449 * will be repeated until the device signals us that no more
1450 * interrupts will be issued.
1452 * 3. ATAPI Tape media access commands have immediate status with a
1453 * delayed process. In case of a successful initiation of a
1454 * media access packet command, the DSC bit will be set when the
1455 * actual execution of the command is finished.
1456 * Since the tape drive will not issue an interrupt, we have to
1457 * poll for this event. In this case, we define the request as
1458 * "low priority request" by setting rq_status to
1459 * IDETAPE_RQ_POSTPONED, set a timer to poll for DSC and exit
1462 * ide.c will then give higher priority to requests which
1463 * originate from the other device, until will change rq_status
1466 * 4. When the packet command is finished, it will be checked for errors.
1468 * 5. In case an error was found, we queue a request sense packet
1469 * command in front of the request queue and retry the operation
1470 * up to IDETAPE_MAX_PC_RETRIES times.
1472 * 6. In case no error was found, or we decided to give up and not
1473 * to retry again, the callback function will be called and then
1474 * we will handle the next request.
1477 static ide_startstop_t idetape_transfer_pc(ide_drive_t *drive)
1479 ide_hwif_t *hwif = drive->hwif;
1480 idetape_tape_t *tape = drive->driver_data;
1481 idetape_pc_t *pc = tape->pc;
1483 ide_startstop_t startstop;
1486 if (ide_wait_stat(&startstop,drive,DRQ_STAT,BUSY_STAT,WAIT_READY)) {
1487 printk(KERN_ERR "ide-tape: Strange, packet command initiated yet DRQ isn't asserted\n");
1490 ireason = hwif->INB(IDE_IREASON_REG);
1491 while (retries-- && ((ireason & CD) == 0 || (ireason & IO))) {
1492 printk(KERN_ERR "ide-tape: (IO,CoD != (0,1) while issuing "
1493 "a packet command, retrying\n");
1495 ireason = hwif->INB(IDE_IREASON_REG);
1497 printk(KERN_ERR "ide-tape: (IO,CoD != (0,1) while "
1498 "issuing a packet command, ignoring\n");
1503 if ((ireason & CD) == 0 || (ireason & IO)) {
1504 printk(KERN_ERR "ide-tape: (IO,CoD) != (0,1) while issuing "
1505 "a packet command\n");
1506 return ide_do_reset(drive);
1508 /* Set the interrupt routine */
1509 ide_set_handler(drive, &idetape_pc_intr, IDETAPE_WAIT_CMD, NULL);
1510 #ifdef CONFIG_BLK_DEV_IDEDMA
1511 /* Begin DMA, if necessary */
1512 if (test_bit(PC_DMA_IN_PROGRESS, &pc->flags))
1513 hwif->dma_start(drive);
1515 /* Send the actual packet */
1516 HWIF(drive)->atapi_output_bytes(drive, pc->c, 12);
1520 static ide_startstop_t idetape_issue_packet_command (ide_drive_t *drive, idetape_pc_t *pc)
1522 ide_hwif_t *hwif = drive->hwif;
1523 idetape_tape_t *tape = drive->driver_data;
1527 if (tape->pc->c[0] == IDETAPE_REQUEST_SENSE_CMD &&
1528 pc->c[0] == IDETAPE_REQUEST_SENSE_CMD) {
1529 printk(KERN_ERR "ide-tape: possible ide-tape.c bug - "
1530 "Two request sense in serial were issued\n");
1533 if (tape->failed_pc == NULL && pc->c[0] != IDETAPE_REQUEST_SENSE_CMD)
1534 tape->failed_pc = pc;
1535 /* Set the current packet command */
1538 if (pc->retries > IDETAPE_MAX_PC_RETRIES ||
1539 test_bit(PC_ABORT, &pc->flags)) {
1541 * We will "abort" retrying a packet command in case
1542 * a legitimate error code was received (crossing a
1543 * filemark, or end of the media, for example).
1545 if (!test_bit(PC_ABORT, &pc->flags)) {
1546 if (!(pc->c[0] == IDETAPE_TEST_UNIT_READY_CMD &&
1547 tape->sense_key == 2 && tape->asc == 4 &&
1548 (tape->ascq == 1 || tape->ascq == 8))) {
1549 printk(KERN_ERR "ide-tape: %s: I/O error, "
1550 "pc = %2x, key = %2x, "
1551 "asc = %2x, ascq = %2x\n",
1552 tape->name, pc->c[0],
1553 tape->sense_key, tape->asc,
1557 pc->error = IDETAPE_ERROR_GENERAL;
1559 tape->failed_pc = NULL;
1560 return pc->callback(drive);
1562 #if IDETAPE_DEBUG_LOG
1563 if (tape->debug_level >= 2)
1564 printk(KERN_INFO "ide-tape: Retry number - %d, cmd = %02X\n", pc->retries, pc->c[0]);
1565 #endif /* IDETAPE_DEBUG_LOG */
1568 /* We haven't transferred any data yet */
1569 pc->actually_transferred = 0;
1570 pc->current_position = pc->buffer;
1571 /* Request to transfer the entire buffer at once */
1572 bcount = pc->request_transfer;
1574 if (test_and_clear_bit(PC_DMA_ERROR, &pc->flags)) {
1575 printk(KERN_WARNING "ide-tape: DMA disabled, "
1576 "reverting to PIO\n");
1579 if (test_bit(PC_DMA_RECOMMENDED, &pc->flags) && drive->using_dma)
1580 dma_ok = !hwif->dma_setup(drive);
1582 ide_pktcmd_tf_load(drive, IDE_TFLAG_NO_SELECT_MASK |
1583 IDE_TFLAG_OUT_DEVICE, bcount, dma_ok);
1585 if (dma_ok) /* Will begin DMA later */
1586 set_bit(PC_DMA_IN_PROGRESS, &pc->flags);
1587 if (test_bit(IDETAPE_DRQ_INTERRUPT, &tape->flags)) {
1588 ide_execute_command(drive, WIN_PACKETCMD, &idetape_transfer_pc,
1589 IDETAPE_WAIT_CMD, NULL);
1592 hwif->OUTB(WIN_PACKETCMD, IDE_COMMAND_REG);
1593 return idetape_transfer_pc(drive);
1598 * General packet command callback function.
1600 static ide_startstop_t idetape_pc_callback (ide_drive_t *drive)
1602 idetape_tape_t *tape = drive->driver_data;
1604 #if IDETAPE_DEBUG_LOG
1605 if (tape->debug_level >= 4)
1606 printk(KERN_INFO "ide-tape: Reached idetape_pc_callback\n");
1607 #endif /* IDETAPE_DEBUG_LOG */
1609 idetape_end_request(drive, tape->pc->error ? 0 : 1, 0);
1614 * A mode sense command is used to "sense" tape parameters.
1616 static void idetape_create_mode_sense_cmd (idetape_pc_t *pc, u8 page_code)
1618 idetape_init_pc(pc);
1619 pc->c[0] = IDETAPE_MODE_SENSE_CMD;
1620 if (page_code != IDETAPE_BLOCK_DESCRIPTOR)
1621 pc->c[1] = 8; /* DBD = 1 - Don't return block descriptors */
1622 pc->c[2] = page_code;
1624 * Changed pc->c[3] to 0 (255 will at best return unused info).
1626 * For SCSI this byte is defined as subpage instead of high byte
1627 * of length and some IDE drives seem to interpret it this way
1628 * and return an error when 255 is used.
1631 pc->c[4] = 255; /* (We will just discard data in that case) */
1632 if (page_code == IDETAPE_BLOCK_DESCRIPTOR)
1633 pc->request_transfer = 12;
1634 else if (page_code == IDETAPE_CAPABILITIES_PAGE)
1635 pc->request_transfer = 24;
1637 pc->request_transfer = 50;
1638 pc->callback = &idetape_pc_callback;
1641 static void calculate_speeds(ide_drive_t *drive)
1643 idetape_tape_t *tape = drive->driver_data;
1644 int full = 125, empty = 75;
1646 if (time_after(jiffies, tape->controlled_pipeline_head_time + 120 * HZ)) {
1647 tape->controlled_previous_pipeline_head = tape->controlled_last_pipeline_head;
1648 tape->controlled_previous_head_time = tape->controlled_pipeline_head_time;
1649 tape->controlled_last_pipeline_head = tape->pipeline_head;
1650 tape->controlled_pipeline_head_time = jiffies;
1652 if (time_after(jiffies, tape->controlled_pipeline_head_time + 60 * HZ))
1653 tape->controlled_pipeline_head_speed = (tape->pipeline_head - tape->controlled_last_pipeline_head) * 32 * HZ / (jiffies - tape->controlled_pipeline_head_time);
1654 else if (time_after(jiffies, tape->controlled_previous_head_time))
1655 tape->controlled_pipeline_head_speed = (tape->pipeline_head - tape->controlled_previous_pipeline_head) * 32 * HZ / (jiffies - tape->controlled_previous_head_time);
1657 if (tape->nr_pending_stages < tape->max_stages /*- 1 */) {
1658 /* -1 for read mode error recovery */
1659 if (time_after(jiffies, tape->uncontrolled_previous_head_time + 10 * HZ)) {
1660 tape->uncontrolled_pipeline_head_time = jiffies;
1661 tape->uncontrolled_pipeline_head_speed = (tape->pipeline_head - tape->uncontrolled_previous_pipeline_head) * 32 * HZ / (jiffies - tape->uncontrolled_previous_head_time);
1664 tape->uncontrolled_previous_head_time = jiffies;
1665 tape->uncontrolled_previous_pipeline_head = tape->pipeline_head;
1666 if (time_after(jiffies, tape->uncontrolled_pipeline_head_time + 30 * HZ)) {
1667 tape->uncontrolled_pipeline_head_time = jiffies;
1670 tape->pipeline_head_speed = max(tape->uncontrolled_pipeline_head_speed, tape->controlled_pipeline_head_speed);
1671 if (tape->speed_control == 0) {
1672 tape->max_insert_speed = 5000;
1673 } else if (tape->speed_control == 1) {
1674 if (tape->nr_pending_stages >= tape->max_stages / 2)
1675 tape->max_insert_speed = tape->pipeline_head_speed +
1676 (1100 - tape->pipeline_head_speed) * 2 * (tape->nr_pending_stages - tape->max_stages / 2) / tape->max_stages;
1678 tape->max_insert_speed = 500 +
1679 (tape->pipeline_head_speed - 500) * 2 * tape->nr_pending_stages / tape->max_stages;
1680 if (tape->nr_pending_stages >= tape->max_stages * 99 / 100)
1681 tape->max_insert_speed = 5000;
1682 } else if (tape->speed_control == 2) {
1683 tape->max_insert_speed = tape->pipeline_head_speed * empty / 100 +
1684 (tape->pipeline_head_speed * full / 100 - tape->pipeline_head_speed * empty / 100) * tape->nr_pending_stages / tape->max_stages;
1686 tape->max_insert_speed = tape->speed_control;
1687 tape->max_insert_speed = max(tape->max_insert_speed, 500);
1690 static ide_startstop_t idetape_media_access_finished (ide_drive_t *drive)
1692 idetape_tape_t *tape = drive->driver_data;
1693 idetape_pc_t *pc = tape->pc;
1696 stat = drive->hwif->INB(IDE_STATUS_REG);
1697 if (stat & SEEK_STAT) {
1698 if (stat & ERR_STAT) {
1699 /* Error detected */
1700 if (pc->c[0] != IDETAPE_TEST_UNIT_READY_CMD)
1701 printk(KERN_ERR "ide-tape: %s: I/O error, ",
1703 /* Retry operation */
1704 return idetape_retry_pc(drive);
1707 if (tape->failed_pc == pc)
1708 tape->failed_pc = NULL;
1710 pc->error = IDETAPE_ERROR_GENERAL;
1711 tape->failed_pc = NULL;
1713 return pc->callback(drive);
1716 static ide_startstop_t idetape_rw_callback (ide_drive_t *drive)
1718 idetape_tape_t *tape = drive->driver_data;
1719 struct request *rq = HWGROUP(drive)->rq;
1720 int blocks = tape->pc->actually_transferred / tape->tape_block_size;
1722 tape->avg_size += blocks * tape->tape_block_size;
1723 tape->insert_size += blocks * tape->tape_block_size;
1724 if (tape->insert_size > 1024 * 1024)
1725 tape->measure_insert_time = 1;
1726 if (tape->measure_insert_time) {
1727 tape->measure_insert_time = 0;
1728 tape->insert_time = jiffies;
1729 tape->insert_size = 0;
1731 if (time_after(jiffies, tape->insert_time))
1732 tape->insert_speed = tape->insert_size / 1024 * HZ / (jiffies - tape->insert_time);
1733 if (time_after_eq(jiffies, tape->avg_time + HZ)) {
1734 tape->avg_speed = tape->avg_size * HZ / (jiffies - tape->avg_time) / 1024;
1736 tape->avg_time = jiffies;
1739 #if IDETAPE_DEBUG_LOG
1740 if (tape->debug_level >= 4)
1741 printk(KERN_INFO "ide-tape: Reached idetape_rw_callback\n");
1742 #endif /* IDETAPE_DEBUG_LOG */
1744 tape->first_frame_position += blocks;
1745 rq->current_nr_sectors -= blocks;
1747 if (!tape->pc->error)
1748 idetape_end_request(drive, 1, 0);
1750 idetape_end_request(drive, tape->pc->error, 0);
1754 static void idetape_create_read_cmd(idetape_tape_t *tape, idetape_pc_t *pc, unsigned int length, struct idetape_bh *bh)
1756 idetape_init_pc(pc);
1757 pc->c[0] = IDETAPE_READ_CMD;
1758 put_unaligned(htonl(length), (unsigned int *) &pc->c[1]);
1760 pc->callback = &idetape_rw_callback;
1762 atomic_set(&bh->b_count, 0);
1764 pc->request_transfer = pc->buffer_size = length * tape->tape_block_size;
1765 if (pc->request_transfer == tape->stage_size)
1766 set_bit(PC_DMA_RECOMMENDED, &pc->flags);
1769 static void idetape_create_read_buffer_cmd(idetape_tape_t *tape, idetape_pc_t *pc, unsigned int length, struct idetape_bh *bh)
1772 struct idetape_bh *p = bh;
1774 idetape_init_pc(pc);
1775 pc->c[0] = IDETAPE_READ_BUFFER_CMD;
1776 pc->c[1] = IDETAPE_RETRIEVE_FAULTY_BLOCK;
1777 pc->c[7] = size >> 8;
1778 pc->c[8] = size & 0xff;
1779 pc->callback = &idetape_pc_callback;
1781 atomic_set(&bh->b_count, 0);
1784 atomic_set(&p->b_count, 0);
1787 pc->request_transfer = pc->buffer_size = size;
1790 static void idetape_create_write_cmd(idetape_tape_t *tape, idetape_pc_t *pc, unsigned int length, struct idetape_bh *bh)
1792 idetape_init_pc(pc);
1793 pc->c[0] = IDETAPE_WRITE_CMD;
1794 put_unaligned(htonl(length), (unsigned int *) &pc->c[1]);
1796 pc->callback = &idetape_rw_callback;
1797 set_bit(PC_WRITING, &pc->flags);
1799 pc->b_data = bh->b_data;
1800 pc->b_count = atomic_read(&bh->b_count);
1802 pc->request_transfer = pc->buffer_size = length * tape->tape_block_size;
1803 if (pc->request_transfer == tape->stage_size)
1804 set_bit(PC_DMA_RECOMMENDED, &pc->flags);
1808 * idetape_do_request is our request handling function.
1810 static ide_startstop_t idetape_do_request(ide_drive_t *drive,
1811 struct request *rq, sector_t block)
1813 idetape_tape_t *tape = drive->driver_data;
1814 idetape_pc_t *pc = NULL;
1815 struct request *postponed_rq = tape->postponed_rq;
1818 #if IDETAPE_DEBUG_LOG
1819 if (tape->debug_level >= 2)
1820 printk(KERN_INFO "ide-tape: sector: %ld, "
1821 "nr_sectors: %ld, current_nr_sectors: %d\n",
1822 rq->sector, rq->nr_sectors, rq->current_nr_sectors);
1823 #endif /* IDETAPE_DEBUG_LOG */
1825 if (!blk_special_request(rq)) {
1827 * We do not support buffer cache originated requests.
1829 printk(KERN_NOTICE "ide-tape: %s: Unsupported request in "
1830 "request queue (%d)\n", drive->name, rq->cmd_type);
1831 ide_end_request(drive, 0, 0);
1836 * Retry a failed packet command
1838 if (tape->failed_pc != NULL &&
1839 tape->pc->c[0] == IDETAPE_REQUEST_SENSE_CMD) {
1840 return idetape_issue_packet_command(drive, tape->failed_pc);
1842 if (postponed_rq != NULL)
1843 if (rq != postponed_rq) {
1844 printk(KERN_ERR "ide-tape: ide-tape.c bug - "
1845 "Two DSC requests were queued\n");
1846 idetape_end_request(drive, 0, 0);
1850 tape->postponed_rq = NULL;
1853 * If the tape is still busy, postpone our request and service
1854 * the other device meanwhile.
1856 stat = drive->hwif->INB(IDE_STATUS_REG);
1858 if (!drive->dsc_overlap && !(rq->cmd[0] & REQ_IDETAPE_PC2))
1859 set_bit(IDETAPE_IGNORE_DSC, &tape->flags);
1861 if (drive->post_reset == 1) {
1862 set_bit(IDETAPE_IGNORE_DSC, &tape->flags);
1863 drive->post_reset = 0;
1866 if (tape->tape_still_time > 100 && tape->tape_still_time < 200)
1867 tape->measure_insert_time = 1;
1868 if (time_after(jiffies, tape->insert_time))
1869 tape->insert_speed = tape->insert_size / 1024 * HZ / (jiffies - tape->insert_time);
1870 calculate_speeds(drive);
1871 if (!test_and_clear_bit(IDETAPE_IGNORE_DSC, &tape->flags) &&
1872 (stat & SEEK_STAT) == 0) {
1873 if (postponed_rq == NULL) {
1874 tape->dsc_polling_start = jiffies;
1875 tape->dsc_polling_frequency = tape->best_dsc_rw_frequency;
1876 tape->dsc_timeout = jiffies + IDETAPE_DSC_RW_TIMEOUT;
1877 } else if (time_after(jiffies, tape->dsc_timeout)) {
1878 printk(KERN_ERR "ide-tape: %s: DSC timeout\n",
1880 if (rq->cmd[0] & REQ_IDETAPE_PC2) {
1881 idetape_media_access_finished(drive);
1884 return ide_do_reset(drive);
1886 } else if (time_after(jiffies, tape->dsc_polling_start + IDETAPE_DSC_MA_THRESHOLD))
1887 tape->dsc_polling_frequency = IDETAPE_DSC_MA_SLOW;
1888 idetape_postpone_request(drive);
1891 if (rq->cmd[0] & REQ_IDETAPE_READ) {
1892 tape->buffer_head++;
1893 tape->postpone_cnt = 0;
1894 pc = idetape_next_pc_storage(drive);
1895 idetape_create_read_cmd(tape, pc, rq->current_nr_sectors, (struct idetape_bh *)rq->special);
1898 if (rq->cmd[0] & REQ_IDETAPE_WRITE) {
1899 tape->buffer_head++;
1900 tape->postpone_cnt = 0;
1901 pc = idetape_next_pc_storage(drive);
1902 idetape_create_write_cmd(tape, pc, rq->current_nr_sectors, (struct idetape_bh *)rq->special);
1905 if (rq->cmd[0] & REQ_IDETAPE_READ_BUFFER) {
1906 tape->postpone_cnt = 0;
1907 pc = idetape_next_pc_storage(drive);
1908 idetape_create_read_buffer_cmd(tape, pc, rq->current_nr_sectors, (struct idetape_bh *)rq->special);
1911 if (rq->cmd[0] & REQ_IDETAPE_PC1) {
1912 pc = (idetape_pc_t *) rq->buffer;
1913 rq->cmd[0] &= ~(REQ_IDETAPE_PC1);
1914 rq->cmd[0] |= REQ_IDETAPE_PC2;
1917 if (rq->cmd[0] & REQ_IDETAPE_PC2) {
1918 idetape_media_access_finished(drive);
1923 return idetape_issue_packet_command(drive, pc);
1927 * Pipeline related functions
1929 static inline int idetape_pipeline_active (idetape_tape_t *tape)
1933 rc1 = test_bit(IDETAPE_PIPELINE_ACTIVE, &tape->flags);
1934 rc2 = (tape->active_data_request != NULL);
1939 * idetape_kmalloc_stage uses __get_free_page to allocate a pipeline
1940 * stage, along with all the necessary small buffers which together make
1941 * a buffer of size tape->stage_size (or a bit more). We attempt to
1942 * combine sequential pages as much as possible.
1944 * Returns a pointer to the new allocated stage, or NULL if we
1945 * can't (or don't want to) allocate a stage.
1947 * Pipeline stages are optional and are used to increase performance.
1948 * If we can't allocate them, we'll manage without them.
1950 static idetape_stage_t *__idetape_kmalloc_stage (idetape_tape_t *tape, int full, int clear)
1952 idetape_stage_t *stage;
1953 struct idetape_bh *prev_bh, *bh;
1954 int pages = tape->pages_per_stage;
1955 char *b_data = NULL;
1957 if ((stage = kmalloc(sizeof (idetape_stage_t),GFP_KERNEL)) == NULL)
1961 bh = stage->bh = kmalloc(sizeof(struct idetape_bh), GFP_KERNEL);
1964 bh->b_reqnext = NULL;
1965 if ((bh->b_data = (char *) __get_free_page (GFP_KERNEL)) == NULL)
1968 memset(bh->b_data, 0, PAGE_SIZE);
1969 bh->b_size = PAGE_SIZE;
1970 atomic_set(&bh->b_count, full ? bh->b_size : 0);
1973 if ((b_data = (char *) __get_free_page (GFP_KERNEL)) == NULL)
1976 memset(b_data, 0, PAGE_SIZE);
1977 if (bh->b_data == b_data + PAGE_SIZE) {
1978 bh->b_size += PAGE_SIZE;
1979 bh->b_data -= PAGE_SIZE;
1981 atomic_add(PAGE_SIZE, &bh->b_count);
1984 if (b_data == bh->b_data + bh->b_size) {
1985 bh->b_size += PAGE_SIZE;
1987 atomic_add(PAGE_SIZE, &bh->b_count);
1991 if ((bh = kmalloc(sizeof(struct idetape_bh), GFP_KERNEL)) == NULL) {
1992 free_page((unsigned long) b_data);
1995 bh->b_reqnext = NULL;
1996 bh->b_data = b_data;
1997 bh->b_size = PAGE_SIZE;
1998 atomic_set(&bh->b_count, full ? bh->b_size : 0);
1999 prev_bh->b_reqnext = bh;
2001 bh->b_size -= tape->excess_bh_size;
2003 atomic_sub(tape->excess_bh_size, &bh->b_count);
2006 __idetape_kfree_stage(stage);
2010 static idetape_stage_t *idetape_kmalloc_stage (idetape_tape_t *tape)
2012 idetape_stage_t *cache_stage = tape->cache_stage;
2014 #if IDETAPE_DEBUG_LOG
2015 if (tape->debug_level >= 4)
2016 printk(KERN_INFO "ide-tape: Reached idetape_kmalloc_stage\n");
2017 #endif /* IDETAPE_DEBUG_LOG */
2019 if (tape->nr_stages >= tape->max_stages)
2021 if (cache_stage != NULL) {
2022 tape->cache_stage = NULL;
2025 return __idetape_kmalloc_stage(tape, 0, 0);
2028 static int idetape_copy_stage_from_user (idetape_tape_t *tape, idetape_stage_t *stage, const char __user *buf, int n)
2030 struct idetape_bh *bh = tape->bh;
2036 printk(KERN_ERR "ide-tape: bh == NULL in "
2037 "idetape_copy_stage_from_user\n");
2040 count = min((unsigned int)(bh->b_size - atomic_read(&bh->b_count)), (unsigned int)n);
2041 if (copy_from_user(bh->b_data + atomic_read(&bh->b_count), buf, count))
2044 atomic_add(count, &bh->b_count);
2046 if (atomic_read(&bh->b_count) == bh->b_size) {
2049 atomic_set(&bh->b_count, 0);
2056 static int idetape_copy_stage_to_user (idetape_tape_t *tape, char __user *buf, idetape_stage_t *stage, int n)
2058 struct idetape_bh *bh = tape->bh;
2064 printk(KERN_ERR "ide-tape: bh == NULL in "
2065 "idetape_copy_stage_to_user\n");
2068 count = min(tape->b_count, n);
2069 if (copy_to_user(buf, tape->b_data, count))
2072 tape->b_data += count;
2073 tape->b_count -= count;
2075 if (!tape->b_count) {
2076 tape->bh = bh = bh->b_reqnext;
2078 tape->b_data = bh->b_data;
2079 tape->b_count = atomic_read(&bh->b_count);
2086 static void idetape_init_merge_stage (idetape_tape_t *tape)
2088 struct idetape_bh *bh = tape->merge_stage->bh;
2091 if (tape->chrdev_direction == idetape_direction_write)
2092 atomic_set(&bh->b_count, 0);
2094 tape->b_data = bh->b_data;
2095 tape->b_count = atomic_read(&bh->b_count);
2099 static void idetape_switch_buffers (idetape_tape_t *tape, idetape_stage_t *stage)
2101 struct idetape_bh *tmp;
2104 stage->bh = tape->merge_stage->bh;
2105 tape->merge_stage->bh = tmp;
2106 idetape_init_merge_stage(tape);
2110 * idetape_add_stage_tail adds a new stage at the end of the pipeline.
2112 static void idetape_add_stage_tail (ide_drive_t *drive,idetape_stage_t *stage)
2114 idetape_tape_t *tape = drive->driver_data;
2115 unsigned long flags;
2117 #if IDETAPE_DEBUG_LOG
2118 if (tape->debug_level >= 4)
2119 printk (KERN_INFO "ide-tape: Reached idetape_add_stage_tail\n");
2120 #endif /* IDETAPE_DEBUG_LOG */
2121 spin_lock_irqsave(&tape->spinlock, flags);
2123 if (tape->last_stage != NULL)
2124 tape->last_stage->next=stage;
2126 tape->first_stage = tape->next_stage=stage;
2127 tape->last_stage = stage;
2128 if (tape->next_stage == NULL)
2129 tape->next_stage = tape->last_stage;
2131 tape->nr_pending_stages++;
2132 spin_unlock_irqrestore(&tape->spinlock, flags);
2136 * idetape_wait_for_request installs a completion in a pending request
2137 * and sleeps until it is serviced.
2139 * The caller should ensure that the request will not be serviced
2140 * before we install the completion (usually by disabling interrupts).
2142 static void idetape_wait_for_request (ide_drive_t *drive, struct request *rq)
2144 DECLARE_COMPLETION_ONSTACK(wait);
2145 idetape_tape_t *tape = drive->driver_data;
2147 if (rq == NULL || !blk_special_request(rq)) {
2148 printk (KERN_ERR "ide-tape: bug: Trying to sleep on non-valid request\n");
2151 rq->end_io_data = &wait;
2152 rq->end_io = blk_end_sync_rq;
2153 spin_unlock_irq(&tape->spinlock);
2154 wait_for_completion(&wait);
2155 /* The stage and its struct request have been deallocated */
2156 spin_lock_irq(&tape->spinlock);
2159 static ide_startstop_t idetape_read_position_callback (ide_drive_t *drive)
2161 idetape_tape_t *tape = drive->driver_data;
2162 idetape_read_position_result_t *result;
2164 #if IDETAPE_DEBUG_LOG
2165 if (tape->debug_level >= 4)
2166 printk(KERN_INFO "ide-tape: Reached idetape_read_position_callback\n");
2167 #endif /* IDETAPE_DEBUG_LOG */
2169 if (!tape->pc->error) {
2170 result = (idetape_read_position_result_t *) tape->pc->buffer;
2171 #if IDETAPE_DEBUG_LOG
2172 if (tape->debug_level >= 2)
2173 printk(KERN_INFO "ide-tape: BOP - %s\n",result->bop ? "Yes":"No");
2174 if (tape->debug_level >= 2)
2175 printk(KERN_INFO "ide-tape: EOP - %s\n",result->eop ? "Yes":"No");
2176 #endif /* IDETAPE_DEBUG_LOG */
2178 printk(KERN_INFO "ide-tape: Block location is unknown to the tape\n");
2179 clear_bit(IDETAPE_ADDRESS_VALID, &tape->flags);
2180 idetape_end_request(drive, 0, 0);
2182 #if IDETAPE_DEBUG_LOG
2183 if (tape->debug_level >= 2)
2184 printk(KERN_INFO "ide-tape: Block Location - %u\n", ntohl(result->first_block));
2185 #endif /* IDETAPE_DEBUG_LOG */
2186 tape->partition = result->partition;
2187 tape->first_frame_position = ntohl(result->first_block);
2188 tape->last_frame_position = ntohl(result->last_block);
2189 tape->blocks_in_buffer = result->blocks_in_buffer[2];
2190 set_bit(IDETAPE_ADDRESS_VALID, &tape->flags);
2191 idetape_end_request(drive, 1, 0);
2194 idetape_end_request(drive, 0, 0);
2200 * idetape_create_write_filemark_cmd will:
2202 * 1. Write a filemark if write_filemark=1.
2203 * 2. Flush the device buffers without writing a filemark
2204 * if write_filemark=0.
2207 static void idetape_create_write_filemark_cmd (ide_drive_t *drive, idetape_pc_t *pc,int write_filemark)
2209 idetape_init_pc(pc);
2210 pc->c[0] = IDETAPE_WRITE_FILEMARK_CMD;
2211 pc->c[4] = write_filemark;
2212 set_bit(PC_WAIT_FOR_DSC, &pc->flags);
2213 pc->callback = &idetape_pc_callback;
2216 static void idetape_create_test_unit_ready_cmd(idetape_pc_t *pc)
2218 idetape_init_pc(pc);
2219 pc->c[0] = IDETAPE_TEST_UNIT_READY_CMD;
2220 pc->callback = &idetape_pc_callback;
2224 * idetape_queue_pc_tail is based on the following functions:
2226 * ide_do_drive_cmd from ide.c
2227 * cdrom_queue_request and cdrom_queue_packet_command from ide-cd.c
2229 * We add a special packet command request to the tail of the request
2230 * queue, and wait for it to be serviced.
2232 * This is not to be called from within the request handling part
2233 * of the driver ! We allocate here data in the stack, and it is valid
2234 * until the request is finished. This is not the case for the bottom
2235 * part of the driver, where we are always leaving the functions to wait
2236 * for an interrupt or a timer event.
2238 * From the bottom part of the driver, we should allocate safe memory
2239 * using idetape_next_pc_storage and idetape_next_rq_storage, and add
2240 * the request to the request list without waiting for it to be serviced !
2241 * In that case, we usually use idetape_queue_pc_head.
2243 static int __idetape_queue_pc_tail (ide_drive_t *drive, idetape_pc_t *pc)
2245 struct ide_tape_obj *tape = drive->driver_data;
2248 idetape_init_rq(&rq, REQ_IDETAPE_PC1);
2249 rq.buffer = (char *) pc;
2250 rq.rq_disk = tape->disk;
2251 return ide_do_drive_cmd(drive, &rq, ide_wait);
2254 static void idetape_create_load_unload_cmd (ide_drive_t *drive, idetape_pc_t *pc,int cmd)
2256 idetape_init_pc(pc);
2257 pc->c[0] = IDETAPE_LOAD_UNLOAD_CMD;
2259 set_bit(PC_WAIT_FOR_DSC, &pc->flags);
2260 pc->callback = &idetape_pc_callback;
2263 static int idetape_wait_ready(ide_drive_t *drive, unsigned long timeout)
2265 idetape_tape_t *tape = drive->driver_data;
2267 int load_attempted = 0;
2270 * Wait for the tape to become ready
2272 set_bit(IDETAPE_MEDIUM_PRESENT, &tape->flags);
2274 while (time_before(jiffies, timeout)) {
2275 idetape_create_test_unit_ready_cmd(&pc);
2276 if (!__idetape_queue_pc_tail(drive, &pc))
2278 if ((tape->sense_key == 2 && tape->asc == 4 && tape->ascq == 2)
2279 || (tape->asc == 0x3A)) { /* no media */
2282 idetape_create_load_unload_cmd(drive, &pc, IDETAPE_LU_LOAD_MASK);
2283 __idetape_queue_pc_tail(drive, &pc);
2285 /* not about to be ready */
2286 } else if (!(tape->sense_key == 2 && tape->asc == 4 &&
2287 (tape->ascq == 1 || tape->ascq == 8)))
2294 static int idetape_queue_pc_tail (ide_drive_t *drive,idetape_pc_t *pc)
2296 return __idetape_queue_pc_tail(drive, pc);
2299 static int idetape_flush_tape_buffers (ide_drive_t *drive)
2304 idetape_create_write_filemark_cmd(drive, &pc, 0);
2305 if ((rc = idetape_queue_pc_tail(drive, &pc)))
2307 idetape_wait_ready(drive, 60 * 5 * HZ);
2311 static void idetape_create_read_position_cmd (idetape_pc_t *pc)
2313 idetape_init_pc(pc);
2314 pc->c[0] = IDETAPE_READ_POSITION_CMD;
2315 pc->request_transfer = 20;
2316 pc->callback = &idetape_read_position_callback;
2319 static int idetape_read_position (ide_drive_t *drive)
2321 idetape_tape_t *tape = drive->driver_data;
2325 #if IDETAPE_DEBUG_LOG
2326 if (tape->debug_level >= 4)
2327 printk(KERN_INFO "ide-tape: Reached idetape_read_position\n");
2328 #endif /* IDETAPE_DEBUG_LOG */
2330 idetape_create_read_position_cmd(&pc);
2331 if (idetape_queue_pc_tail(drive, &pc))
2333 position = tape->first_frame_position;
2337 static void idetape_create_locate_cmd (ide_drive_t *drive, idetape_pc_t *pc, unsigned int block, u8 partition, int skip)
2339 idetape_init_pc(pc);
2340 pc->c[0] = IDETAPE_LOCATE_CMD;
2342 put_unaligned(htonl(block), (unsigned int *) &pc->c[3]);
2343 pc->c[8] = partition;
2344 set_bit(PC_WAIT_FOR_DSC, &pc->flags);
2345 pc->callback = &idetape_pc_callback;
2348 static int idetape_create_prevent_cmd (ide_drive_t *drive, idetape_pc_t *pc, int prevent)
2350 idetape_tape_t *tape = drive->driver_data;
2352 /* device supports locking according to capabilities page */
2353 if (!(tape->caps[6] & 0x01))
2356 idetape_init_pc(pc);
2357 pc->c[0] = IDETAPE_PREVENT_CMD;
2359 pc->callback = &idetape_pc_callback;
2363 static int __idetape_discard_read_pipeline (ide_drive_t *drive)
2365 idetape_tape_t *tape = drive->driver_data;
2366 unsigned long flags;
2369 if (tape->chrdev_direction != idetape_direction_read)
2372 /* Remove merge stage. */
2373 cnt = tape->merge_stage_size / tape->tape_block_size;
2374 if (test_and_clear_bit(IDETAPE_FILEMARK, &tape->flags))
2375 ++cnt; /* Filemarks count as 1 sector */
2376 tape->merge_stage_size = 0;
2377 if (tape->merge_stage != NULL) {
2378 __idetape_kfree_stage(tape->merge_stage);
2379 tape->merge_stage = NULL;
2382 /* Clear pipeline flags. */
2383 clear_bit(IDETAPE_PIPELINE_ERROR, &tape->flags);
2384 tape->chrdev_direction = idetape_direction_none;
2386 /* Remove pipeline stages. */
2387 if (tape->first_stage == NULL)
2390 spin_lock_irqsave(&tape->spinlock, flags);
2391 tape->next_stage = NULL;
2392 if (idetape_pipeline_active(tape))
2393 idetape_wait_for_request(drive, tape->active_data_request);
2394 spin_unlock_irqrestore(&tape->spinlock, flags);
2396 while (tape->first_stage != NULL) {
2397 struct request *rq_ptr = &tape->first_stage->rq;
2399 cnt += rq_ptr->nr_sectors - rq_ptr->current_nr_sectors;
2400 if (rq_ptr->errors == IDETAPE_ERROR_FILEMARK)
2402 idetape_remove_stage_head(drive);
2404 tape->nr_pending_stages = 0;
2405 tape->max_stages = tape->min_pipeline;
2410 * idetape_position_tape positions the tape to the requested block
2411 * using the LOCATE packet command. A READ POSITION command is then
2412 * issued to check where we are positioned.
2414 * Like all higher level operations, we queue the commands at the tail
2415 * of the request queue and wait for their completion.
2418 static int idetape_position_tape (ide_drive_t *drive, unsigned int block, u8 partition, int skip)
2420 idetape_tape_t *tape = drive->driver_data;
2424 if (tape->chrdev_direction == idetape_direction_read)
2425 __idetape_discard_read_pipeline(drive);
2426 idetape_wait_ready(drive, 60 * 5 * HZ);
2427 idetape_create_locate_cmd(drive, &pc, block, partition, skip);
2428 retval = idetape_queue_pc_tail(drive, &pc);
2432 idetape_create_read_position_cmd(&pc);
2433 return (idetape_queue_pc_tail(drive, &pc));
2436 static void idetape_discard_read_pipeline (ide_drive_t *drive, int restore_position)
2438 idetape_tape_t *tape = drive->driver_data;
2442 cnt = __idetape_discard_read_pipeline(drive);
2443 if (restore_position) {
2444 position = idetape_read_position(drive);
2445 seek = position > cnt ? position - cnt : 0;
2446 if (idetape_position_tape(drive, seek, 0, 0)) {
2447 printk(KERN_INFO "ide-tape: %s: position_tape failed in discard_pipeline()\n", tape->name);
2454 * idetape_queue_rw_tail generates a read/write request for the block
2455 * device interface and wait for it to be serviced.
2457 static int idetape_queue_rw_tail(ide_drive_t *drive, int cmd, int blocks, struct idetape_bh *bh)
2459 idetape_tape_t *tape = drive->driver_data;
2462 #if IDETAPE_DEBUG_LOG
2463 if (tape->debug_level >= 2)
2464 printk(KERN_INFO "ide-tape: idetape_queue_rw_tail: cmd=%d\n",cmd);
2465 #endif /* IDETAPE_DEBUG_LOG */
2466 if (idetape_pipeline_active(tape)) {
2467 printk(KERN_ERR "ide-tape: bug: the pipeline is active in idetape_queue_rw_tail\n");
2471 idetape_init_rq(&rq, cmd);
2472 rq.rq_disk = tape->disk;
2473 rq.special = (void *)bh;
2474 rq.sector = tape->first_frame_position;
2475 rq.nr_sectors = rq.current_nr_sectors = blocks;
2476 (void) ide_do_drive_cmd(drive, &rq, ide_wait);
2478 if ((cmd & (REQ_IDETAPE_READ | REQ_IDETAPE_WRITE)) == 0)
2481 if (tape->merge_stage)
2482 idetape_init_merge_stage(tape);
2483 if (rq.errors == IDETAPE_ERROR_GENERAL)
2485 return (tape->tape_block_size * (blocks-rq.current_nr_sectors));
2489 * idetape_insert_pipeline_into_queue is used to start servicing the
2490 * pipeline stages, starting from tape->next_stage.
2492 static void idetape_insert_pipeline_into_queue (ide_drive_t *drive)
2494 idetape_tape_t *tape = drive->driver_data;
2496 if (tape->next_stage == NULL)
2498 if (!idetape_pipeline_active(tape)) {
2499 set_bit(IDETAPE_PIPELINE_ACTIVE, &tape->flags);
2500 idetape_active_next_stage(drive);
2501 (void) ide_do_drive_cmd(drive, tape->active_data_request, ide_end);
2505 static void idetape_create_inquiry_cmd (idetape_pc_t *pc)
2507 idetape_init_pc(pc);
2508 pc->c[0] = IDETAPE_INQUIRY_CMD;
2509 pc->c[4] = pc->request_transfer = 254;
2510 pc->callback = &idetape_pc_callback;
2513 static void idetape_create_rewind_cmd (ide_drive_t *drive, idetape_pc_t *pc)
2515 idetape_init_pc(pc);
2516 pc->c[0] = IDETAPE_REWIND_CMD;
2517 set_bit(PC_WAIT_FOR_DSC, &pc->flags);
2518 pc->callback = &idetape_pc_callback;
2521 static void idetape_create_erase_cmd (idetape_pc_t *pc)
2523 idetape_init_pc(pc);
2524 pc->c[0] = IDETAPE_ERASE_CMD;
2526 set_bit(PC_WAIT_FOR_DSC, &pc->flags);
2527 pc->callback = &idetape_pc_callback;
2530 static void idetape_create_space_cmd (idetape_pc_t *pc,int count, u8 cmd)
2532 idetape_init_pc(pc);
2533 pc->c[0] = IDETAPE_SPACE_CMD;
2534 put_unaligned(htonl(count), (unsigned int *) &pc->c[1]);
2536 set_bit(PC_WAIT_FOR_DSC, &pc->flags);
2537 pc->callback = &idetape_pc_callback;
2540 static void idetape_wait_first_stage (ide_drive_t *drive)
2542 idetape_tape_t *tape = drive->driver_data;
2543 unsigned long flags;
2545 if (tape->first_stage == NULL)
2547 spin_lock_irqsave(&tape->spinlock, flags);
2548 if (tape->active_stage == tape->first_stage)
2549 idetape_wait_for_request(drive, tape->active_data_request);
2550 spin_unlock_irqrestore(&tape->spinlock, flags);
2554 * idetape_add_chrdev_write_request tries to add a character device
2555 * originated write request to our pipeline. In case we don't succeed,
2556 * we revert to non-pipelined operation mode for this request.
2558 * 1. Try to allocate a new pipeline stage.
2559 * 2. If we can't, wait for more and more requests to be serviced
2560 * and try again each time.
2561 * 3. If we still can't allocate a stage, fallback to
2562 * non-pipelined operation mode for this request.
2564 static int idetape_add_chrdev_write_request (ide_drive_t *drive, int blocks)
2566 idetape_tape_t *tape = drive->driver_data;
2567 idetape_stage_t *new_stage;
2568 unsigned long flags;
2571 #if IDETAPE_DEBUG_LOG
2572 if (tape->debug_level >= 3)
2573 printk(KERN_INFO "ide-tape: Reached idetape_add_chrdev_write_request\n");
2574 #endif /* IDETAPE_DEBUG_LOG */
2577 * Attempt to allocate a new stage.
2578 * Pay special attention to possible race conditions.
2580 while ((new_stage = idetape_kmalloc_stage(tape)) == NULL) {
2581 spin_lock_irqsave(&tape->spinlock, flags);
2582 if (idetape_pipeline_active(tape)) {
2583 idetape_wait_for_request(drive, tape->active_data_request);
2584 spin_unlock_irqrestore(&tape->spinlock, flags);
2586 spin_unlock_irqrestore(&tape->spinlock, flags);
2587 idetape_insert_pipeline_into_queue(drive);
2588 if (idetape_pipeline_active(tape))
2591 * Linux is short on memory. Fallback to
2592 * non-pipelined operation mode for this request.
2594 return idetape_queue_rw_tail(drive, REQ_IDETAPE_WRITE, blocks, tape->merge_stage->bh);
2597 rq = &new_stage->rq;
2598 idetape_init_rq(rq, REQ_IDETAPE_WRITE);
2599 /* Doesn't actually matter - We always assume sequential access */
2600 rq->sector = tape->first_frame_position;
2601 rq->nr_sectors = rq->current_nr_sectors = blocks;
2603 idetape_switch_buffers(tape, new_stage);
2604 idetape_add_stage_tail(drive, new_stage);
2605 tape->pipeline_head++;
2606 calculate_speeds(drive);
2609 * Estimate whether the tape has stopped writing by checking
2610 * if our write pipeline is currently empty. If we are not
2611 * writing anymore, wait for the pipeline to be full enough
2612 * (90%) before starting to service requests, so that we will
2613 * be able to keep up with the higher speeds of the tape.
2615 if (!idetape_pipeline_active(tape)) {
2616 if (tape->nr_stages >= tape->max_stages * 9 / 10 ||
2617 tape->nr_stages >= tape->max_stages - tape->uncontrolled_pipeline_head_speed * 3 * 1024 / tape->tape_block_size) {
2618 tape->measure_insert_time = 1;
2619 tape->insert_time = jiffies;
2620 tape->insert_size = 0;
2621 tape->insert_speed = 0;
2622 idetape_insert_pipeline_into_queue(drive);
2625 if (test_and_clear_bit(IDETAPE_PIPELINE_ERROR, &tape->flags))
2626 /* Return a deferred error */
2632 * idetape_wait_for_pipeline will wait until all pending pipeline
2633 * requests are serviced. Typically called on device close.
2635 static void idetape_wait_for_pipeline (ide_drive_t *drive)
2637 idetape_tape_t *tape = drive->driver_data;
2638 unsigned long flags;
2640 while (tape->next_stage || idetape_pipeline_active(tape)) {
2641 idetape_insert_pipeline_into_queue(drive);
2642 spin_lock_irqsave(&tape->spinlock, flags);
2643 if (idetape_pipeline_active(tape))
2644 idetape_wait_for_request(drive, tape->active_data_request);
2645 spin_unlock_irqrestore(&tape->spinlock, flags);
2649 static void idetape_empty_write_pipeline (ide_drive_t *drive)
2651 idetape_tape_t *tape = drive->driver_data;
2653 struct idetape_bh *bh;
2655 if (tape->chrdev_direction != idetape_direction_write) {
2656 printk(KERN_ERR "ide-tape: bug: Trying to empty write pipeline, but we are not writing.\n");
2659 if (tape->merge_stage_size > tape->stage_size) {
2660 printk(KERN_ERR "ide-tape: bug: merge_buffer too big\n");
2661 tape->merge_stage_size = tape->stage_size;
2663 if (tape->merge_stage_size) {
2664 blocks = tape->merge_stage_size / tape->tape_block_size;
2665 if (tape->merge_stage_size % tape->tape_block_size) {
2669 i = tape->tape_block_size - tape->merge_stage_size % tape->tape_block_size;
2670 bh = tape->bh->b_reqnext;
2672 atomic_set(&bh->b_count, 0);
2679 printk(KERN_INFO "ide-tape: bug, bh NULL\n");
2682 min = min(i, (unsigned int)(bh->b_size - atomic_read(&bh->b_count)));
2683 memset(bh->b_data + atomic_read(&bh->b_count), 0, min);
2684 atomic_add(min, &bh->b_count);
2689 (void) idetape_add_chrdev_write_request(drive, blocks);
2690 tape->merge_stage_size = 0;
2692 idetape_wait_for_pipeline(drive);
2693 if (tape->merge_stage != NULL) {
2694 __idetape_kfree_stage(tape->merge_stage);
2695 tape->merge_stage = NULL;
2697 clear_bit(IDETAPE_PIPELINE_ERROR, &tape->flags);
2698 tape->chrdev_direction = idetape_direction_none;
2701 * On the next backup, perform the feedback loop again.
2702 * (I don't want to keep sense information between backups,
2703 * as some systems are constantly on, and the system load
2704 * can be totally different on the next backup).
2706 tape->max_stages = tape->min_pipeline;
2707 if (tape->first_stage != NULL ||
2708 tape->next_stage != NULL ||
2709 tape->last_stage != NULL ||
2710 tape->nr_stages != 0) {
2711 printk(KERN_ERR "ide-tape: ide-tape pipeline bug, "
2712 "first_stage %p, next_stage %p, "
2713 "last_stage %p, nr_stages %d\n",
2714 tape->first_stage, tape->next_stage,
2715 tape->last_stage, tape->nr_stages);
2719 static void idetape_restart_speed_control (ide_drive_t *drive)
2721 idetape_tape_t *tape = drive->driver_data;
2723 tape->restart_speed_control_req = 0;
2724 tape->pipeline_head = 0;
2725 tape->controlled_last_pipeline_head = tape->uncontrolled_last_pipeline_head = 0;
2726 tape->controlled_previous_pipeline_head = tape->uncontrolled_previous_pipeline_head = 0;
2727 tape->pipeline_head_speed = tape->controlled_pipeline_head_speed = 5000;
2728 tape->uncontrolled_pipeline_head_speed = 0;
2729 tape->controlled_pipeline_head_time = tape->uncontrolled_pipeline_head_time = jiffies;
2730 tape->controlled_previous_head_time = tape->uncontrolled_previous_head_time = jiffies;
2733 static int idetape_initiate_read (ide_drive_t *drive, int max_stages)
2735 idetape_tape_t *tape = drive->driver_data;
2736 idetape_stage_t *new_stage;
2739 u16 blocks = *(u16 *)&tape->caps[12];
2741 /* Initialize read operation */
2742 if (tape->chrdev_direction != idetape_direction_read) {
2743 if (tape->chrdev_direction == idetape_direction_write) {
2744 idetape_empty_write_pipeline(drive);
2745 idetape_flush_tape_buffers(drive);
2747 if (tape->merge_stage || tape->merge_stage_size) {
2748 printk (KERN_ERR "ide-tape: merge_stage_size should be 0 now\n");
2749 tape->merge_stage_size = 0;
2751 if ((tape->merge_stage = __idetape_kmalloc_stage(tape, 0, 0)) == NULL)
2753 tape->chrdev_direction = idetape_direction_read;
2756 * Issue a read 0 command to ensure that DSC handshake
2757 * is switched from completion mode to buffer available
2759 * No point in issuing this if DSC overlap isn't supported,
2760 * some drives (Seagate STT3401A) will return an error.
2762 if (drive->dsc_overlap) {
2763 bytes_read = idetape_queue_rw_tail(drive, REQ_IDETAPE_READ, 0, tape->merge_stage->bh);
2764 if (bytes_read < 0) {
2765 __idetape_kfree_stage(tape->merge_stage);
2766 tape->merge_stage = NULL;
2767 tape->chrdev_direction = idetape_direction_none;
2772 if (tape->restart_speed_control_req)
2773 idetape_restart_speed_control(drive);
2774 idetape_init_rq(&rq, REQ_IDETAPE_READ);
2775 rq.sector = tape->first_frame_position;
2776 rq.nr_sectors = rq.current_nr_sectors = blocks;
2777 if (!test_bit(IDETAPE_PIPELINE_ERROR, &tape->flags) &&
2778 tape->nr_stages < max_stages) {
2779 new_stage = idetape_kmalloc_stage(tape);
2780 while (new_stage != NULL) {
2782 idetape_add_stage_tail(drive, new_stage);
2783 if (tape->nr_stages >= max_stages)
2785 new_stage = idetape_kmalloc_stage(tape);
2788 if (!idetape_pipeline_active(tape)) {
2789 if (tape->nr_pending_stages >= 3 * max_stages / 4) {
2790 tape->measure_insert_time = 1;
2791 tape->insert_time = jiffies;
2792 tape->insert_size = 0;
2793 tape->insert_speed = 0;
2794 idetape_insert_pipeline_into_queue(drive);
2801 * idetape_add_chrdev_read_request is called from idetape_chrdev_read
2802 * to service a character device read request and add read-ahead
2803 * requests to our pipeline.
2805 static int idetape_add_chrdev_read_request (ide_drive_t *drive,int blocks)
2807 idetape_tape_t *tape = drive->driver_data;
2808 unsigned long flags;
2809 struct request *rq_ptr;
2812 #if IDETAPE_DEBUG_LOG
2813 if (tape->debug_level >= 4)
2814 printk(KERN_INFO "ide-tape: Reached idetape_add_chrdev_read_request, %d blocks\n", blocks);
2815 #endif /* IDETAPE_DEBUG_LOG */
2818 * If we are at a filemark, return a read length of 0
2820 if (test_bit(IDETAPE_FILEMARK, &tape->flags))
2824 * Wait for the next block to be available at the head
2827 idetape_initiate_read(drive, tape->max_stages);
2828 if (tape->first_stage == NULL) {
2829 if (test_bit(IDETAPE_PIPELINE_ERROR, &tape->flags))
2831 return idetape_queue_rw_tail(drive, REQ_IDETAPE_READ, blocks, tape->merge_stage->bh);
2833 idetape_wait_first_stage(drive);
2834 rq_ptr = &tape->first_stage->rq;
2835 bytes_read = tape->tape_block_size * (rq_ptr->nr_sectors - rq_ptr->current_nr_sectors);
2836 rq_ptr->nr_sectors = rq_ptr->current_nr_sectors = 0;
2839 if (rq_ptr->errors == IDETAPE_ERROR_EOD)
2842 idetape_switch_buffers(tape, tape->first_stage);
2843 if (rq_ptr->errors == IDETAPE_ERROR_FILEMARK)
2844 set_bit(IDETAPE_FILEMARK, &tape->flags);
2845 spin_lock_irqsave(&tape->spinlock, flags);
2846 idetape_remove_stage_head(drive);
2847 spin_unlock_irqrestore(&tape->spinlock, flags);
2848 tape->pipeline_head++;
2849 calculate_speeds(drive);
2851 if (bytes_read > blocks * tape->tape_block_size) {
2852 printk(KERN_ERR "ide-tape: bug: trying to return more bytes than requested\n");
2853 bytes_read = blocks * tape->tape_block_size;
2855 return (bytes_read);
2858 static void idetape_pad_zeros (ide_drive_t *drive, int bcount)
2860 idetape_tape_t *tape = drive->driver_data;
2861 struct idetape_bh *bh;
2867 bh = tape->merge_stage->bh;
2868 count = min(tape->stage_size, bcount);
2870 blocks = count / tape->tape_block_size;
2872 atomic_set(&bh->b_count, min(count, (unsigned int)bh->b_size));
2873 memset(bh->b_data, 0, atomic_read(&bh->b_count));
2874 count -= atomic_read(&bh->b_count);
2877 idetape_queue_rw_tail(drive, REQ_IDETAPE_WRITE, blocks, tape->merge_stage->bh);
2881 static int idetape_pipeline_size (ide_drive_t *drive)
2883 idetape_tape_t *tape = drive->driver_data;
2884 idetape_stage_t *stage;
2888 idetape_wait_for_pipeline(drive);
2889 stage = tape->first_stage;
2890 while (stage != NULL) {
2892 size += tape->tape_block_size * (rq->nr_sectors-rq->current_nr_sectors);
2893 if (rq->errors == IDETAPE_ERROR_FILEMARK)
2894 size += tape->tape_block_size;
2895 stage = stage->next;
2897 size += tape->merge_stage_size;
2902 * Rewinds the tape to the Beginning Of the current Partition (BOP).
2904 * We currently support only one partition.
2906 static int idetape_rewind_tape (ide_drive_t *drive)
2910 #if IDETAPE_DEBUG_LOG
2911 idetape_tape_t *tape = drive->driver_data;
2912 if (tape->debug_level >= 2)
2913 printk(KERN_INFO "ide-tape: Reached idetape_rewind_tape\n");
2914 #endif /* IDETAPE_DEBUG_LOG */
2916 idetape_create_rewind_cmd(drive, &pc);
2917 retval = idetape_queue_pc_tail(drive, &pc);
2921 idetape_create_read_position_cmd(&pc);
2922 retval = idetape_queue_pc_tail(drive, &pc);
2929 * Our special ide-tape ioctl's.
2931 * Currently there aren't any ioctl's.
2932 * mtio.h compatible commands should be issued to the character device
2935 static int idetape_blkdev_ioctl(ide_drive_t *drive, unsigned int cmd, unsigned long arg)
2937 idetape_tape_t *tape = drive->driver_data;
2938 idetape_config_t config;
2939 void __user *argp = (void __user *)arg;
2941 #if IDETAPE_DEBUG_LOG
2942 if (tape->debug_level >= 4)
2943 printk(KERN_INFO "ide-tape: Reached idetape_blkdev_ioctl\n");
2944 #endif /* IDETAPE_DEBUG_LOG */
2947 if (copy_from_user(&config, argp, sizeof (idetape_config_t)))
2949 tape->best_dsc_rw_frequency = config.dsc_rw_frequency;
2950 tape->max_stages = config.nr_stages;
2953 config.dsc_rw_frequency = (int) tape->best_dsc_rw_frequency;
2954 config.nr_stages = tape->max_stages;
2955 if (copy_to_user(argp, &config, sizeof (idetape_config_t)))
2965 * idetape_space_over_filemarks is now a bit more complicated than just
2966 * passing the command to the tape since we may have crossed some
2967 * filemarks during our pipelined read-ahead mode.
2969 * As a minor side effect, the pipeline enables us to support MTFSFM when
2970 * the filemark is in our internal pipeline even if the tape doesn't
2971 * support spacing over filemarks in the reverse direction.
2973 static int idetape_space_over_filemarks (ide_drive_t *drive,short mt_op,int mt_count)
2975 idetape_tape_t *tape = drive->driver_data;
2977 unsigned long flags;
2979 int sprev = !!(tape->caps[4] & 0x20);
2983 if (MTBSF == mt_op || MTBSFM == mt_op) {
2986 mt_count = - mt_count;
2989 if (tape->chrdev_direction == idetape_direction_read) {
2991 * We have a read-ahead buffer. Scan it for crossed
2994 tape->merge_stage_size = 0;
2995 if (test_and_clear_bit(IDETAPE_FILEMARK, &tape->flags))
2997 while (tape->first_stage != NULL) {
2998 if (count == mt_count) {
2999 if (mt_op == MTFSFM)
3000 set_bit(IDETAPE_FILEMARK, &tape->flags);
3003 spin_lock_irqsave(&tape->spinlock, flags);
3004 if (tape->first_stage == tape->active_stage) {
3006 * We have reached the active stage in the read pipeline.
3007 * There is no point in allowing the drive to continue
3008 * reading any farther, so we stop the pipeline.
3010 * This section should be moved to a separate subroutine,
3011 * because a similar function is performed in
3012 * __idetape_discard_read_pipeline(), for example.
3014 tape->next_stage = NULL;
3015 spin_unlock_irqrestore(&tape->spinlock, flags);
3016 idetape_wait_first_stage(drive);
3017 tape->next_stage = tape->first_stage->next;
3019 spin_unlock_irqrestore(&tape->spinlock, flags);
3020 if (tape->first_stage->rq.errors == IDETAPE_ERROR_FILEMARK)
3022 idetape_remove_stage_head(drive);
3024 idetape_discard_read_pipeline(drive, 0);
3028 * The filemark was not found in our internal pipeline.
3029 * Now we can issue the space command.
3034 idetape_create_space_cmd(&pc,mt_count-count,IDETAPE_SPACE_OVER_FILEMARK);
3035 return (idetape_queue_pc_tail(drive, &pc));
3040 retval = idetape_space_over_filemarks(drive, MTFSF, mt_count-count);
3041 if (retval) return (retval);
3042 count = (MTBSFM == mt_op ? 1 : -1);
3043 return (idetape_space_over_filemarks(drive, MTFSF, count));
3045 printk(KERN_ERR "ide-tape: MTIO operation %d not supported\n",mt_op);
3052 * Our character device read / write functions.
3054 * The tape is optimized to maximize throughput when it is transferring
3055 * an integral number of the "continuous transfer limit", which is
3056 * a parameter of the specific tape (26 KB on my particular tape).
3057 * (32 kB for Onstream)
3059 * As of version 1.3 of the driver, the character device provides an
3060 * abstract continuous view of the media - any mix of block sizes (even 1
3061 * byte) on the same backup/restore procedure is supported. The driver
3062 * will internally convert the requests to the recommended transfer unit,
3063 * so that an unmatch between the user's block size to the recommended
3064 * size will only result in a (slightly) increased driver overhead, but
3065 * will no longer hit performance.
3066 * This is not applicable to Onstream.
3068 static ssize_t idetape_chrdev_read (struct file *file, char __user *buf,
3069 size_t count, loff_t *ppos)
3071 struct ide_tape_obj *tape = ide_tape_f(file);
3072 ide_drive_t *drive = tape->drive;
3073 ssize_t bytes_read,temp, actually_read = 0, rc;
3075 u16 ctl = *(u16 *)&tape->caps[12];
3077 #if IDETAPE_DEBUG_LOG
3078 if (tape->debug_level >= 3)
3079 printk(KERN_INFO "ide-tape: Reached idetape_chrdev_read, count %Zd\n", count);
3080 #endif /* IDETAPE_DEBUG_LOG */
3082 if (tape->chrdev_direction != idetape_direction_read) {
3083 if (test_bit(IDETAPE_DETECT_BS, &tape->flags))
3084 if (count > tape->tape_block_size &&
3085 (count % tape->tape_block_size) == 0)
3086 tape->user_bs_factor = count / tape->tape_block_size;
3088 if ((rc = idetape_initiate_read(drive, tape->max_stages)) < 0)
3092 if (tape->merge_stage_size) {
3093 actually_read = min((unsigned int)(tape->merge_stage_size), (unsigned int)count);
3094 if (idetape_copy_stage_to_user(tape, buf, tape->merge_stage, actually_read))
3096 buf += actually_read;
3097 tape->merge_stage_size -= actually_read;
3098 count -= actually_read;
3100 while (count >= tape->stage_size) {
3101 bytes_read = idetape_add_chrdev_read_request(drive, ctl);
3102 if (bytes_read <= 0)
3104 if (idetape_copy_stage_to_user(tape, buf, tape->merge_stage, bytes_read))
3107 count -= bytes_read;
3108 actually_read += bytes_read;
3111 bytes_read = idetape_add_chrdev_read_request(drive, ctl);
3112 if (bytes_read <= 0)
3114 temp = min((unsigned long)count, (unsigned long)bytes_read);
3115 if (idetape_copy_stage_to_user(tape, buf, tape->merge_stage, temp))
3117 actually_read += temp;
3118 tape->merge_stage_size = bytes_read-temp;
3121 if (!actually_read && test_bit(IDETAPE_FILEMARK, &tape->flags)) {
3122 #if IDETAPE_DEBUG_LOG
3123 if (tape->debug_level >= 2)
3124 printk(KERN_INFO "ide-tape: %s: spacing over filemark\n", tape->name);
3126 idetape_space_over_filemarks(drive, MTFSF, 1);
3130 return (ret) ? ret : actually_read;
3133 static ssize_t idetape_chrdev_write (struct file *file, const char __user *buf,
3134 size_t count, loff_t *ppos)
3136 struct ide_tape_obj *tape = ide_tape_f(file);
3137 ide_drive_t *drive = tape->drive;
3138 ssize_t actually_written = 0;
3140 u16 ctl = *(u16 *)&tape->caps[12];
3142 /* The drive is write protected. */
3143 if (tape->write_prot)
3146 #if IDETAPE_DEBUG_LOG
3147 if (tape->debug_level >= 3)
3148 printk(KERN_INFO "ide-tape: Reached idetape_chrdev_write, "
3149 "count %Zd\n", count);
3150 #endif /* IDETAPE_DEBUG_LOG */
3152 /* Initialize write operation */
3153 if (tape->chrdev_direction != idetape_direction_write) {
3154 if (tape->chrdev_direction == idetape_direction_read)
3155 idetape_discard_read_pipeline(drive, 1);
3156 if (tape->merge_stage || tape->merge_stage_size) {
3157 printk(KERN_ERR "ide-tape: merge_stage_size "
3158 "should be 0 now\n");
3159 tape->merge_stage_size = 0;
3161 if ((tape->merge_stage = __idetape_kmalloc_stage(tape, 0, 0)) == NULL)
3163 tape->chrdev_direction = idetape_direction_write;
3164 idetape_init_merge_stage(tape);
3167 * Issue a write 0 command to ensure that DSC handshake
3168 * is switched from completion mode to buffer available
3170 * No point in issuing this if DSC overlap isn't supported,
3171 * some drives (Seagate STT3401A) will return an error.
3173 if (drive->dsc_overlap) {
3174 ssize_t retval = idetape_queue_rw_tail(drive, REQ_IDETAPE_WRITE, 0, tape->merge_stage->bh);
3176 __idetape_kfree_stage(tape->merge_stage);
3177 tape->merge_stage = NULL;
3178 tape->chrdev_direction = idetape_direction_none;
3185 if (tape->restart_speed_control_req)
3186 idetape_restart_speed_control(drive);
3187 if (tape->merge_stage_size) {
3188 if (tape->merge_stage_size >= tape->stage_size) {
3189 printk(KERN_ERR "ide-tape: bug: merge buffer too big\n");
3190 tape->merge_stage_size = 0;
3192 actually_written = min((unsigned int)(tape->stage_size - tape->merge_stage_size), (unsigned int)count);
3193 if (idetape_copy_stage_from_user(tape, tape->merge_stage, buf, actually_written))
3195 buf += actually_written;
3196 tape->merge_stage_size += actually_written;
3197 count -= actually_written;
3199 if (tape->merge_stage_size == tape->stage_size) {
3201 tape->merge_stage_size = 0;
3202 retval = idetape_add_chrdev_write_request(drive, ctl);
3207 while (count >= tape->stage_size) {
3209 if (idetape_copy_stage_from_user(tape, tape->merge_stage, buf, tape->stage_size))
3211 buf += tape->stage_size;
3212 count -= tape->stage_size;
3213 retval = idetape_add_chrdev_write_request(drive, ctl);
3214 actually_written += tape->stage_size;
3219 actually_written += count;
3220 if (idetape_copy_stage_from_user(tape, tape->merge_stage, buf, count))
3222 tape->merge_stage_size += count;
3224 return (ret) ? ret : actually_written;
3227 static int idetape_write_filemark (ide_drive_t *drive)
3231 /* Write a filemark */
3232 idetape_create_write_filemark_cmd(drive, &pc, 1);
3233 if (idetape_queue_pc_tail(drive, &pc)) {
3234 printk(KERN_ERR "ide-tape: Couldn't write a filemark\n");
3241 * idetape_mtioctop is called from idetape_chrdev_ioctl when
3242 * the general mtio MTIOCTOP ioctl is requested.
3244 * We currently support the following mtio.h operations:
3246 * MTFSF - Space over mt_count filemarks in the positive direction.
3247 * The tape is positioned after the last spaced filemark.
3249 * MTFSFM - Same as MTFSF, but the tape is positioned before the
3252 * MTBSF - Steps background over mt_count filemarks, tape is
3253 * positioned before the last filemark.
3255 * MTBSFM - Like MTBSF, only tape is positioned after the last filemark.
3259 * MTBSF and MTBSFM are not supported when the tape doesn't
3260 * support spacing over filemarks in the reverse direction.
3261 * In this case, MTFSFM is also usually not supported (it is
3262 * supported in the rare case in which we crossed the filemark
3263 * during our read-ahead pipelined operation mode).
3265 * MTWEOF - Writes mt_count filemarks. Tape is positioned after
3266 * the last written filemark.
3268 * MTREW - Rewinds tape.
3270 * MTLOAD - Loads the tape.
3272 * MTOFFL - Puts the tape drive "Offline": Rewinds the tape and
3273 * MTUNLOAD prevents further access until the media is replaced.
3275 * MTNOP - Flushes tape buffers.
3277 * MTRETEN - Retension media. This typically consists of one end
3278 * to end pass on the media.
3280 * MTEOM - Moves to the end of recorded data.
3282 * MTERASE - Erases tape.
3284 * MTSETBLK - Sets the user block size to mt_count bytes. If
3285 * mt_count is 0, we will attempt to autodetect
3288 * MTSEEK - Positions the tape in a specific block number, where
3289 * each block is assumed to contain which user_block_size
3292 * MTSETPART - Switches to another tape partition.
3294 * MTLOCK - Locks the tape door.
3296 * MTUNLOCK - Unlocks the tape door.
3298 * The following commands are currently not supported:
3300 * MTFSS, MTBSS, MTWSM, MTSETDENSITY,
3301 * MTSETDRVBUFFER, MT_ST_BOOLEANS, MT_ST_WRITE_THRESHOLD.
3303 static int idetape_mtioctop (ide_drive_t *drive,short mt_op,int mt_count)
3305 idetape_tape_t *tape = drive->driver_data;
3309 #if IDETAPE_DEBUG_LOG
3310 if (tape->debug_level >= 1)
3311 printk(KERN_INFO "ide-tape: Handling MTIOCTOP ioctl: "
3312 "mt_op=%d, mt_count=%d\n", mt_op, mt_count);
3313 #endif /* IDETAPE_DEBUG_LOG */
3315 * Commands which need our pipelined read-ahead stages.
3324 return (idetape_space_over_filemarks(drive,mt_op,mt_count));
3330 if (tape->write_prot)
3332 idetape_discard_read_pipeline(drive, 1);
3333 for (i = 0; i < mt_count; i++) {
3334 retval = idetape_write_filemark(drive);
3340 idetape_discard_read_pipeline(drive, 0);
3341 if (idetape_rewind_tape(drive))
3345 idetape_discard_read_pipeline(drive, 0);
3346 idetape_create_load_unload_cmd(drive, &pc, IDETAPE_LU_LOAD_MASK);
3347 return (idetape_queue_pc_tail(drive, &pc));
3351 * If door is locked, attempt to unlock before
3352 * attempting to eject.
3354 if (tape->door_locked) {
3355 if (idetape_create_prevent_cmd(drive, &pc, 0))
3356 if (!idetape_queue_pc_tail(drive, &pc))
3357 tape->door_locked = DOOR_UNLOCKED;
3359 idetape_discard_read_pipeline(drive, 0);
3360 idetape_create_load_unload_cmd(drive, &pc,!IDETAPE_LU_LOAD_MASK);
3361 retval = idetape_queue_pc_tail(drive, &pc);
3363 clear_bit(IDETAPE_MEDIUM_PRESENT, &tape->flags);
3366 idetape_discard_read_pipeline(drive, 0);
3367 return (idetape_flush_tape_buffers(drive));
3369 idetape_discard_read_pipeline(drive, 0);
3370 idetape_create_load_unload_cmd(drive, &pc,IDETAPE_LU_RETENSION_MASK | IDETAPE_LU_LOAD_MASK);
3371 return (idetape_queue_pc_tail(drive, &pc));
3373 idetape_create_space_cmd(&pc, 0, IDETAPE_SPACE_TO_EOD);
3374 return (idetape_queue_pc_tail(drive, &pc));
3376 (void) idetape_rewind_tape(drive);
3377 idetape_create_erase_cmd(&pc);
3378 return (idetape_queue_pc_tail(drive, &pc));
3381 if (mt_count < tape->tape_block_size || mt_count % tape->tape_block_size)
3383 tape->user_bs_factor = mt_count / tape->tape_block_size;
3384 clear_bit(IDETAPE_DETECT_BS, &tape->flags);
3386 set_bit(IDETAPE_DETECT_BS, &tape->flags);
3389 idetape_discard_read_pipeline(drive, 0);
3390 return idetape_position_tape(drive, mt_count * tape->user_bs_factor, tape->partition, 0);
3392 idetape_discard_read_pipeline(drive, 0);
3393 return (idetape_position_tape(drive, 0, mt_count, 0));
3397 if (!idetape_create_prevent_cmd(drive, &pc, 1))
3399 retval = idetape_queue_pc_tail(drive, &pc);
3400 if (retval) return retval;
3401 tape->door_locked = DOOR_EXPLICITLY_LOCKED;
3404 if (!idetape_create_prevent_cmd(drive, &pc, 0))
3406 retval = idetape_queue_pc_tail(drive, &pc);
3407 if (retval) return retval;
3408 tape->door_locked = DOOR_UNLOCKED;
3411 printk(KERN_ERR "ide-tape: MTIO operation %d not "
3412 "supported\n", mt_op);
3418 * Our character device ioctls.
3420 * General mtio.h magnetic io commands are supported here, and not in
3421 * the corresponding block interface.
3423 * The following ioctls are supported:
3425 * MTIOCTOP - Refer to idetape_mtioctop for detailed description.
3427 * MTIOCGET - The mt_dsreg field in the returned mtget structure
3428 * will be set to (user block size in bytes <<
3429 * MT_ST_BLKSIZE_SHIFT) & MT_ST_BLKSIZE_MASK.
3431 * The mt_blkno is set to the current user block number.
3432 * The other mtget fields are not supported.
3434 * MTIOCPOS - The current tape "block position" is returned. We
3435 * assume that each block contains user_block_size
3438 * Our own ide-tape ioctls are supported on both interfaces.
3440 static int idetape_chrdev_ioctl (struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg)
3442 struct ide_tape_obj *tape = ide_tape_f(file);
3443 ide_drive_t *drive = tape->drive;
3447 int block_offset = 0, position = tape->first_frame_position;
3448 void __user *argp = (void __user *)arg;
3450 #if IDETAPE_DEBUG_LOG
3451 if (tape->debug_level >= 3)
3452 printk(KERN_INFO "ide-tape: Reached idetape_chrdev_ioctl, "
3454 #endif /* IDETAPE_DEBUG_LOG */
3456 tape->restart_speed_control_req = 1;
3457 if (tape->chrdev_direction == idetape_direction_write) {
3458 idetape_empty_write_pipeline(drive);
3459 idetape_flush_tape_buffers(drive);
3461 if (cmd == MTIOCGET || cmd == MTIOCPOS) {
3462 block_offset = idetape_pipeline_size(drive) / (tape->tape_block_size * tape->user_bs_factor);
3463 if ((position = idetape_read_position(drive)) < 0)
3468 if (copy_from_user(&mtop, argp, sizeof (struct mtop)))
3470 return (idetape_mtioctop(drive,mtop.mt_op,mtop.mt_count));
3472 memset(&mtget, 0, sizeof (struct mtget));
3473 mtget.mt_type = MT_ISSCSI2;
3474 mtget.mt_blkno = position / tape->user_bs_factor - block_offset;
3475 mtget.mt_dsreg = ((tape->tape_block_size * tape->user_bs_factor) << MT_ST_BLKSIZE_SHIFT) & MT_ST_BLKSIZE_MASK;
3476 if (tape->drv_write_prot) {
3477 mtget.mt_gstat |= GMT_WR_PROT(0xffffffff);
3479 if (copy_to_user(argp, &mtget, sizeof(struct mtget)))
3483 mtpos.mt_blkno = position / tape->user_bs_factor - block_offset;
3484 if (copy_to_user(argp, &mtpos, sizeof(struct mtpos)))
3488 if (tape->chrdev_direction == idetape_direction_read)
3489 idetape_discard_read_pipeline(drive, 1);
3490 return idetape_blkdev_ioctl(drive, cmd, arg);
3495 * Do a mode sense page 0 with block descriptor and if it succeeds set the tape
3496 * block size with the reported value.
3498 static void ide_tape_get_bsize_from_bdesc(ide_drive_t *drive)
3500 idetape_tape_t *tape = drive->driver_data;
3503 idetape_create_mode_sense_cmd(&pc, IDETAPE_BLOCK_DESCRIPTOR);
3504 if (idetape_queue_pc_tail(drive, &pc)) {
3505 printk(KERN_ERR "ide-tape: Can't get block descriptor\n");
3506 if (tape->tape_block_size == 0) {
3507 printk(KERN_WARNING "ide-tape: Cannot deal with zero "
3508 "block size, assuming 32k\n");
3509 tape->tape_block_size = 32768;
3513 tape->tape_block_size = (pc.buffer[4 + 5] << 16) +
3514 (pc.buffer[4 + 6] << 8) +
3516 tape->drv_write_prot = (pc.buffer[2] & 0x80) >> 7;
3520 * Our character device open function.
3522 static int idetape_chrdev_open (struct inode *inode, struct file *filp)
3524 unsigned int minor = iminor(inode), i = minor & ~0xc0;
3526 idetape_tape_t *tape;
3531 * We really want to do nonseekable_open(inode, filp); here, but some
3532 * versions of tar incorrectly call lseek on tapes and bail out if that
3533 * fails. So we disallow pread() and pwrite(), but permit lseeks.
3535 filp->f_mode &= ~(FMODE_PREAD | FMODE_PWRITE);
3537 #if IDETAPE_DEBUG_LOG
3538 printk(KERN_INFO "ide-tape: Reached idetape_chrdev_open\n");
3539 #endif /* IDETAPE_DEBUG_LOG */
3541 if (i >= MAX_HWIFS * MAX_DRIVES)
3544 if (!(tape = ide_tape_chrdev_get(i)))
3547 drive = tape->drive;
3549 filp->private_data = tape;
3551 if (test_and_set_bit(IDETAPE_BUSY, &tape->flags)) {
3556 retval = idetape_wait_ready(drive, 60 * HZ);
3558 clear_bit(IDETAPE_BUSY, &tape->flags);
3559 printk(KERN_ERR "ide-tape: %s: drive not ready\n", tape->name);
3563 idetape_read_position(drive);
3564 if (!test_bit(IDETAPE_ADDRESS_VALID, &tape->flags))
3565 (void)idetape_rewind_tape(drive);
3567 if (tape->chrdev_direction != idetape_direction_read)
3568 clear_bit(IDETAPE_PIPELINE_ERROR, &tape->flags);
3570 /* Read block size and write protect status from drive. */
3571 ide_tape_get_bsize_from_bdesc(drive);
3573 /* Set write protect flag if device is opened as read-only. */
3574 if ((filp->f_flags & O_ACCMODE) == O_RDONLY)
3575 tape->write_prot = 1;
3577 tape->write_prot = tape->drv_write_prot;
3579 /* Make sure drive isn't write protected if user wants to write. */
3580 if (tape->write_prot) {
3581 if ((filp->f_flags & O_ACCMODE) == O_WRONLY ||
3582 (filp->f_flags & O_ACCMODE) == O_RDWR) {
3583 clear_bit(IDETAPE_BUSY, &tape->flags);
3590 * Lock the tape drive door so user can't eject.
3592 if (tape->chrdev_direction == idetape_direction_none) {
3593 if (idetape_create_prevent_cmd(drive, &pc, 1)) {
3594 if (!idetape_queue_pc_tail(drive, &pc)) {
3595 if (tape->door_locked != DOOR_EXPLICITLY_LOCKED)
3596 tape->door_locked = DOOR_LOCKED;
3600 idetape_restart_speed_control(drive);
3601 tape->restart_speed_control_req = 0;
3609 static void idetape_write_release (ide_drive_t *drive, unsigned int minor)
3611 idetape_tape_t *tape = drive->driver_data;
3613 idetape_empty_write_pipeline(drive);
3614 tape->merge_stage = __idetape_kmalloc_stage(tape, 1, 0);
3615 if (tape->merge_stage != NULL) {
3616 idetape_pad_zeros(drive, tape->tape_block_size * (tape->user_bs_factor - 1));
3617 __idetape_kfree_stage(tape->merge_stage);
3618 tape->merge_stage = NULL;
3620 idetape_write_filemark(drive);
3621 idetape_flush_tape_buffers(drive);
3622 idetape_flush_tape_buffers(drive);
3626 * Our character device release function.
3628 static int idetape_chrdev_release (struct inode *inode, struct file *filp)
3630 struct ide_tape_obj *tape = ide_tape_f(filp);
3631 ide_drive_t *drive = tape->drive;
3633 unsigned int minor = iminor(inode);
3636 tape = drive->driver_data;
3637 #if IDETAPE_DEBUG_LOG
3638 if (tape->debug_level >= 3)
3639 printk(KERN_INFO "ide-tape: Reached idetape_chrdev_release\n");
3640 #endif /* IDETAPE_DEBUG_LOG */
3642 if (tape->chrdev_direction == idetape_direction_write)
3643 idetape_write_release(drive, minor);
3644 if (tape->chrdev_direction == idetape_direction_read) {
3646 idetape_discard_read_pipeline(drive, 1);
3648 idetape_wait_for_pipeline(drive);
3650 if (tape->cache_stage != NULL) {
3651 __idetape_kfree_stage(tape->cache_stage);
3652 tape->cache_stage = NULL;
3654 if (minor < 128 && test_bit(IDETAPE_MEDIUM_PRESENT, &tape->flags))
3655 (void) idetape_rewind_tape(drive);
3656 if (tape->chrdev_direction == idetape_direction_none) {
3657 if (tape->door_locked == DOOR_LOCKED) {
3658 if (idetape_create_prevent_cmd(drive, &pc, 0)) {
3659 if (!idetape_queue_pc_tail(drive, &pc))
3660 tape->door_locked = DOOR_UNLOCKED;
3664 clear_bit(IDETAPE_BUSY, &tape->flags);
3671 * idetape_identify_device is called to check the contents of the
3672 * ATAPI IDENTIFY command results. We return:
3674 * 1 If the tape can be supported by us, based on the information
3677 * 0 If this tape driver is not currently supported by us.
3679 static int idetape_identify_device (ide_drive_t *drive)
3681 struct idetape_id_gcw gcw;
3682 struct hd_driveid *id = drive->id;
3684 if (drive->id_read == 0)
3687 *((unsigned short *) &gcw) = id->config;
3689 /* Check that we can support this device */
3691 if (gcw.protocol != 2)
3692 printk(KERN_ERR "ide-tape: Protocol (0x%02x) is not ATAPI\n",
3694 else if (gcw.device_type != 1)
3695 printk(KERN_ERR "ide-tape: Device type (0x%02x) is not set "
3696 "to tape\n", gcw.device_type);
3697 else if (!gcw.removable)
3698 printk(KERN_ERR "ide-tape: The removable flag is not set\n");
3699 else if (gcw.packet_size != 0) {
3700 printk(KERN_ERR "ide-tape: Packet size (0x%02x) is not 12 "
3701 "bytes long\n", gcw.packet_size);
3707 static void idetape_get_inquiry_results(ide_drive_t *drive)
3710 idetape_tape_t *tape = drive->driver_data;
3713 idetape_create_inquiry_cmd(&pc);
3714 if (idetape_queue_pc_tail(drive, &pc)) {
3715 printk(KERN_ERR "ide-tape: %s: can't get INQUIRY results\n",
3719 memcpy(tape->vendor_id, &pc.buffer[8], 8);
3720 memcpy(tape->product_id, &pc.buffer[16], 16);
3721 memcpy(tape->firmware_revision, &pc.buffer[32], 4);
3723 ide_fixstring(tape->vendor_id, 10, 0);
3724 ide_fixstring(tape->product_id, 18, 0);
3725 ide_fixstring(tape->firmware_revision, 6, 0);
3726 r = tape->firmware_revision;
3727 if (*(r + 1) == '.')
3728 tape->firmware_revision_num = (*r - '0') * 100 +
3729 (*(r + 2) - '0') * 10 + *(r + 3) - '0';
3730 printk(KERN_INFO "ide-tape: %s <-> %s: %s %s rev %s\n",
3731 drive->name, tape->name, tape->vendor_id,
3732 tape->product_id, tape->firmware_revision);
3736 * Ask the tape about its various parameters. In particular, we will adjust our
3737 * data transfer buffer size to the recommended value as returned by the tape.
3739 static void idetape_get_mode_sense_results (ide_drive_t *drive)
3741 idetape_tape_t *tape = drive->driver_data;
3744 u8 speed, max_speed;
3746 idetape_create_mode_sense_cmd(&pc, IDETAPE_CAPABILITIES_PAGE);
3747 if (idetape_queue_pc_tail(drive, &pc)) {
3748 printk(KERN_ERR "ide-tape: Can't get tape parameters - assuming"
3749 " some default values\n");
3750 tape->tape_block_size = 512;
3751 put_unaligned(52, (u16 *)&tape->caps[12]);
3752 put_unaligned(540, (u16 *)&tape->caps[14]);
3753 put_unaligned(6*52, (u16 *)&tape->caps[16]);
3756 caps = pc.buffer + 4 + pc.buffer[3];
3758 /* convert to host order and save for later use */
3759 speed = be16_to_cpu(*(u16 *)&caps[14]);
3760 max_speed = be16_to_cpu(*(u16 *)&caps[8]);
3762 put_unaligned(max_speed, (u16 *)&caps[8]);
3763 put_unaligned(be16_to_cpu(*(u16 *)&caps[12]), (u16 *)&caps[12]);
3764 put_unaligned(speed, (u16 *)&caps[14]);
3765 put_unaligned(be16_to_cpu(*(u16 *)&caps[16]), (u16 *)&caps[16]);
3768 printk(KERN_INFO "ide-tape: %s: invalid tape speed "
3769 "(assuming 650KB/sec)\n", drive->name);
3770 put_unaligned(650, (u16 *)&caps[14]);
3773 printk(KERN_INFO "ide-tape: %s: invalid max_speed "
3774 "(assuming 650KB/sec)\n", drive->name);
3775 put_unaligned(650, (u16 *)&caps[8]);
3778 memcpy(&tape->caps, caps, 20);
3780 tape->tape_block_size = 512;
3781 else if (caps[7] & 0x04)
3782 tape->tape_block_size = 1024;
3785 #ifdef CONFIG_IDE_PROC_FS
3786 static void idetape_add_settings (ide_drive_t *drive)
3788 idetape_tape_t *tape = drive->driver_data;
3791 * drive setting name read/write data type min max mul_factor div_factor data pointer set function
3793 ide_add_setting(drive, "buffer", SETTING_READ, TYPE_SHORT, 0, 0xffff,
3794 1, 2, (u16 *)&tape->caps[16], NULL);
3795 ide_add_setting(drive, "pipeline_min", SETTING_RW, TYPE_INT, 1, 0xffff, tape->stage_size / 1024, 1, &tape->min_pipeline, NULL);
3796 ide_add_setting(drive, "pipeline", SETTING_RW, TYPE_INT, 1, 0xffff, tape->stage_size / 1024, 1, &tape->max_stages, NULL);
3797 ide_add_setting(drive, "pipeline_max", SETTING_RW, TYPE_INT, 1, 0xffff, tape->stage_size / 1024, 1, &tape->max_pipeline, NULL);
3798 ide_add_setting(drive, "pipeline_used", SETTING_READ, TYPE_INT, 0, 0xffff, tape->stage_size / 1024, 1, &tape->nr_stages, NULL);
3799 ide_add_setting(drive, "pipeline_pending", SETTING_READ, TYPE_INT, 0, 0xffff, tape->stage_size / 1024, 1, &tape->nr_pending_stages, NULL);
3800 ide_add_setting(drive, "speed", SETTING_READ, TYPE_SHORT, 0, 0xffff,
3801 1, 1, (u16 *)&tape->caps[14], NULL);
3802 ide_add_setting(drive, "stage", SETTING_READ, TYPE_INT, 0, 0xffff, 1, 1024, &tape->stage_size, NULL);
3803 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);
3804 ide_add_setting(drive, "dsc_overlap", SETTING_RW, TYPE_BYTE, 0, 1, 1, 1, &drive->dsc_overlap, NULL);
3805 ide_add_setting(drive, "pipeline_head_speed_c",SETTING_READ, TYPE_INT, 0, 0xffff, 1, 1, &tape->controlled_pipeline_head_speed, NULL);
3806 ide_add_setting(drive, "pipeline_head_speed_u",SETTING_READ, TYPE_INT, 0, 0xffff, 1, 1, &tape->uncontrolled_pipeline_head_speed,NULL);
3807 ide_add_setting(drive, "avg_speed", SETTING_READ, TYPE_INT, 0, 0xffff, 1, 1, &tape->avg_speed, NULL);
3808 ide_add_setting(drive, "debug_level", SETTING_RW, TYPE_INT, 0, 0xffff, 1, 1, &tape->debug_level, NULL);
3811 static inline void idetape_add_settings(ide_drive_t *drive) { ; }
3815 * ide_setup is called to:
3817 * 1. Initialize our various state variables.
3818 * 2. Ask the tape for its capabilities.
3819 * 3. Allocate a buffer which will be used for data
3820 * transfer. The buffer size is chosen based on
3821 * the recommendation which we received in step (2).
3823 * Note that at this point ide.c already assigned us an irq, so that
3824 * we can queue requests here and wait for their completion.
3826 static void idetape_setup (ide_drive_t *drive, idetape_tape_t *tape, int minor)
3828 unsigned long t1, tmid, tn, t;
3830 struct idetape_id_gcw gcw;
3833 u16 *ctl = (u16 *)&tape->caps[12];
3835 spin_lock_init(&tape->spinlock);
3836 drive->dsc_overlap = 1;
3837 if (drive->hwif->host_flags & IDE_HFLAG_NO_DSC) {
3838 printk(KERN_INFO "ide-tape: %s: disabling DSC overlap\n",
3840 drive->dsc_overlap = 0;
3842 /* Seagate Travan drives do not support DSC overlap. */
3843 if (strstr(drive->id->model, "Seagate STT3401"))
3844 drive->dsc_overlap = 0;
3845 tape->minor = minor;
3846 tape->name[0] = 'h';
3847 tape->name[1] = 't';
3848 tape->name[2] = '0' + minor;
3849 tape->chrdev_direction = idetape_direction_none;
3850 tape->pc = tape->pc_stack;
3851 tape->max_insert_speed = 10000;
3852 tape->speed_control = 1;
3853 *((unsigned short *) &gcw) = drive->id->config;
3854 if (gcw.drq_type == 1)
3855 set_bit(IDETAPE_DRQ_INTERRUPT, &tape->flags);
3857 tape->min_pipeline = tape->max_pipeline = tape->max_stages = 10;
3859 idetape_get_inquiry_results(drive);
3860 idetape_get_mode_sense_results(drive);
3861 ide_tape_get_bsize_from_bdesc(drive);
3862 tape->user_bs_factor = 1;
3863 tape->stage_size = *ctl * tape->tape_block_size;
3864 while (tape->stage_size > 0xffff) {
3865 printk(KERN_NOTICE "ide-tape: decreasing stage size\n");
3867 tape->stage_size = *ctl * tape->tape_block_size;
3869 stage_size = tape->stage_size;
3870 tape->pages_per_stage = stage_size / PAGE_SIZE;
3871 if (stage_size % PAGE_SIZE) {
3872 tape->pages_per_stage++;
3873 tape->excess_bh_size = PAGE_SIZE - stage_size % PAGE_SIZE;
3876 /* Select the "best" DSC read/write polling freq and pipeline size. */
3877 speed = max(*(u16 *)&tape->caps[14], *(u16 *)&tape->caps[8]);
3879 tape->max_stages = speed * 1000 * 10 / tape->stage_size;
3882 * Limit memory use for pipeline to 10% of physical memory
3885 if (tape->max_stages * tape->stage_size > si.totalram * si.mem_unit / 10)
3886 tape->max_stages = si.totalram * si.mem_unit / (10 * tape->stage_size);
3887 tape->max_stages = min(tape->max_stages, IDETAPE_MAX_PIPELINE_STAGES);
3888 tape->min_pipeline = min(tape->max_stages, IDETAPE_MIN_PIPELINE_STAGES);
3889 tape->max_pipeline = min(tape->max_stages * 2, IDETAPE_MAX_PIPELINE_STAGES);
3890 if (tape->max_stages == 0)
3891 tape->max_stages = tape->min_pipeline = tape->max_pipeline = 1;
3893 t1 = (tape->stage_size * HZ) / (speed * 1000);
3894 tmid = (*(u16 *)&tape->caps[16] * 32 * HZ) / (speed * 125);
3895 tn = (IDETAPE_FIFO_THRESHOLD * tape->stage_size * HZ) / (speed * 1000);
3897 if (tape->max_stages)
3903 * Ensure that the number we got makes sense; limit
3904 * it within IDETAPE_DSC_RW_MIN and IDETAPE_DSC_RW_MAX.
3906 tape->best_dsc_rw_frequency = max_t(unsigned long, min_t(unsigned long, t, IDETAPE_DSC_RW_MAX), IDETAPE_DSC_RW_MIN);
3907 printk(KERN_INFO "ide-tape: %s <-> %s: %dKBps, %d*%dkB buffer, "
3908 "%dkB pipeline, %lums tDSC%s\n",
3909 drive->name, tape->name, *(u16 *)&tape->caps[14],
3910 (*(u16 *)&tape->caps[16] * 512) / tape->stage_size,
3911 tape->stage_size / 1024,
3912 tape->max_stages * tape->stage_size / 1024,
3913 tape->best_dsc_rw_frequency * 1000 / HZ,
3914 drive->using_dma ? ", DMA":"");
3916 idetape_add_settings(drive);
3919 static void ide_tape_remove(ide_drive_t *drive)
3921 idetape_tape_t *tape = drive->driver_data;
3923 ide_proc_unregister_driver(drive, tape->driver);
3925 ide_unregister_region(tape->disk);
3930 static void ide_tape_release(struct kref *kref)
3932 struct ide_tape_obj *tape = to_ide_tape(kref);
3933 ide_drive_t *drive = tape->drive;
3934 struct gendisk *g = tape->disk;
3936 BUG_ON(tape->first_stage != NULL || tape->merge_stage_size);
3938 drive->dsc_overlap = 0;
3939 drive->driver_data = NULL;
3940 device_destroy(idetape_sysfs_class, MKDEV(IDETAPE_MAJOR, tape->minor));
3941 device_destroy(idetape_sysfs_class, MKDEV(IDETAPE_MAJOR, tape->minor + 128));
3942 idetape_devs[tape->minor] = NULL;
3943 g->private_data = NULL;
3948 #ifdef CONFIG_IDE_PROC_FS
3949 static int proc_idetape_read_name
3950 (char *page, char **start, off_t off, int count, int *eof, void *data)
3952 ide_drive_t *drive = (ide_drive_t *) data;
3953 idetape_tape_t *tape = drive->driver_data;
3957 len = sprintf(out, "%s\n", tape->name);
3958 PROC_IDE_READ_RETURN(page, start, off, count, eof, len);
3961 static ide_proc_entry_t idetape_proc[] = {
3962 { "capacity", S_IFREG|S_IRUGO, proc_ide_read_capacity, NULL },
3963 { "name", S_IFREG|S_IRUGO, proc_idetape_read_name, NULL },
3964 { NULL, 0, NULL, NULL }
3968 static int ide_tape_probe(ide_drive_t *);
3970 static ide_driver_t idetape_driver = {
3972 .owner = THIS_MODULE,
3974 .bus = &ide_bus_type,
3976 .probe = ide_tape_probe,
3977 .remove = ide_tape_remove,
3978 .version = IDETAPE_VERSION,
3980 .supports_dsc_overlap = 1,
3981 .do_request = idetape_do_request,
3982 .end_request = idetape_end_request,
3983 .error = __ide_error,
3984 .abort = __ide_abort,
3985 #ifdef CONFIG_IDE_PROC_FS
3986 .proc = idetape_proc,
3991 * Our character device supporting functions, passed to register_chrdev.
3993 static const struct file_operations idetape_fops = {
3994 .owner = THIS_MODULE,
3995 .read = idetape_chrdev_read,
3996 .write = idetape_chrdev_write,
3997 .ioctl = idetape_chrdev_ioctl,
3998 .open = idetape_chrdev_open,
3999 .release = idetape_chrdev_release,
4002 static int idetape_open(struct inode *inode, struct file *filp)
4004 struct gendisk *disk = inode->i_bdev->bd_disk;
4005 struct ide_tape_obj *tape;
4007 if (!(tape = ide_tape_get(disk)))
4013 static int idetape_release(struct inode *inode, struct file *filp)
4015 struct gendisk *disk = inode->i_bdev->bd_disk;
4016 struct ide_tape_obj *tape = ide_tape_g(disk);
4023 static int idetape_ioctl(struct inode *inode, struct file *file,
4024 unsigned int cmd, unsigned long arg)
4026 struct block_device *bdev = inode->i_bdev;
4027 struct ide_tape_obj *tape = ide_tape_g(bdev->bd_disk);
4028 ide_drive_t *drive = tape->drive;
4029 int err = generic_ide_ioctl(drive, file, bdev, cmd, arg);
4031 err = idetape_blkdev_ioctl(drive, cmd, arg);
4035 static struct block_device_operations idetape_block_ops = {
4036 .owner = THIS_MODULE,
4037 .open = idetape_open,
4038 .release = idetape_release,
4039 .ioctl = idetape_ioctl,
4042 static int ide_tape_probe(ide_drive_t *drive)
4044 idetape_tape_t *tape;
4048 if (!strstr("ide-tape", drive->driver_req))
4050 if (!drive->present)
4052 if (drive->media != ide_tape)
4054 if (!idetape_identify_device (drive)) {
4055 printk(KERN_ERR "ide-tape: %s: not supported by this version of ide-tape\n", drive->name);
4059 printk("ide-tape: passing drive %s to ide-scsi emulation.\n", drive->name);
4062 if (strstr(drive->id->model, "OnStream DI-")) {
4063 printk(KERN_WARNING "ide-tape: Use drive %s with ide-scsi emulation and osst.\n", drive->name);
4064 printk(KERN_WARNING "ide-tape: OnStream support will be removed soon from ide-tape!\n");
4066 tape = kzalloc(sizeof (idetape_tape_t), GFP_KERNEL);
4068 printk(KERN_ERR "ide-tape: %s: Can't allocate a tape structure\n", drive->name);
4072 g = alloc_disk(1 << PARTN_BITS);
4076 ide_init_disk(g, drive);
4078 ide_proc_register_driver(drive, &idetape_driver);
4080 kref_init(&tape->kref);
4082 tape->drive = drive;
4083 tape->driver = &idetape_driver;
4086 g->private_data = &tape->driver;
4088 drive->driver_data = tape;
4090 mutex_lock(&idetape_ref_mutex);
4091 for (minor = 0; idetape_devs[minor]; minor++)
4093 idetape_devs[minor] = tape;
4094 mutex_unlock(&idetape_ref_mutex);
4096 idetape_setup(drive, tape, minor);
4098 device_create(idetape_sysfs_class, &drive->gendev,
4099 MKDEV(IDETAPE_MAJOR, minor), "%s", tape->name);
4100 device_create(idetape_sysfs_class, &drive->gendev,
4101 MKDEV(IDETAPE_MAJOR, minor + 128), "n%s", tape->name);
4103 g->fops = &idetape_block_ops;
4104 ide_register_region(g);
4114 MODULE_DESCRIPTION("ATAPI Streaming TAPE Driver");
4115 MODULE_LICENSE("GPL");
4117 static void __exit idetape_exit (void)
4119 driver_unregister(&idetape_driver.gen_driver);
4120 class_destroy(idetape_sysfs_class);
4121 unregister_chrdev(IDETAPE_MAJOR, "ht");
4124 static int __init idetape_init(void)
4127 idetape_sysfs_class = class_create(THIS_MODULE, "ide_tape");
4128 if (IS_ERR(idetape_sysfs_class)) {
4129 idetape_sysfs_class = NULL;
4130 printk(KERN_ERR "Unable to create sysfs class for ide tapes\n");
4135 if (register_chrdev(IDETAPE_MAJOR, "ht", &idetape_fops)) {
4136 printk(KERN_ERR "ide-tape: Failed to register character device interface\n");
4138 goto out_free_class;
4141 error = driver_register(&idetape_driver.gen_driver);
4143 goto out_free_driver;
4148 driver_unregister(&idetape_driver.gen_driver);
4150 class_destroy(idetape_sysfs_class);
4155 MODULE_ALIAS("ide:*m-tape*");
4156 module_init(idetape_init);
4157 module_exit(idetape_exit);
4158 MODULE_ALIAS_CHARDEV_MAJOR(IDETAPE_MAJOR);