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.20"
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>
39 #include <scsi/scsi.h>
41 #include <asm/byteorder.h>
42 #include <linux/irq.h>
43 #include <linux/uaccess.h>
45 #include <asm/unaligned.h>
46 #include <linux/mtio.h>
49 /* output errors only */
51 /* output all sense key/asc */
53 /* info regarding all chrdev-related procedures */
54 DBG_CHRDEV = (1 << 2),
55 /* all remaining procedures */
57 /* buffer alloc info (pc_stack & rq_stack) */
58 DBG_PCRQ_STACK = (1 << 4),
61 /* define to see debug info */
62 #define IDETAPE_DEBUG_LOG 0
65 #define debug_log(lvl, fmt, args...) \
67 if (tape->debug_mask & lvl) \
68 printk(KERN_INFO "ide-tape: " fmt, ## args); \
71 #define debug_log(lvl, fmt, args...) do {} while (0)
74 /**************************** Tunable parameters *****************************/
78 * Pipelined mode parameters.
80 * We try to use the minimum number of stages which is enough to keep the tape
81 * constantly streaming. To accomplish that, we implement a feedback loop around
82 * the maximum number of stages:
84 * We start from MIN maximum stages (we will not even use MIN stages if we don't
85 * need them), increment it by RATE*(MAX-MIN) whenever we sense that the
86 * pipeline is empty, until we reach the optimum value or until we reach MAX.
88 * Setting the following parameter to 0 is illegal: the pipelined mode cannot be
89 * disabled (idetape_calculate_speeds() divides by tape->max_stages.)
91 #define IDETAPE_MIN_PIPELINE_STAGES 1
92 #define IDETAPE_MAX_PIPELINE_STAGES 400
93 #define IDETAPE_INCREASE_STAGES_RATE 20
96 * After each failed packet command we issue a request sense command and retry
97 * the packet command IDETAPE_MAX_PC_RETRIES times.
99 * Setting IDETAPE_MAX_PC_RETRIES to 0 will disable retries.
101 #define IDETAPE_MAX_PC_RETRIES 3
104 * With each packet command, we allocate a buffer of IDETAPE_PC_BUFFER_SIZE
105 * bytes. This is used for several packet commands (Not for READ/WRITE commands)
107 #define IDETAPE_PC_BUFFER_SIZE 256
110 * In various places in the driver, we need to allocate storage
111 * for packet commands and requests, which will remain valid while
112 * we leave the driver to wait for an interrupt or a timeout event.
114 #define IDETAPE_PC_STACK (10 + IDETAPE_MAX_PC_RETRIES)
117 * Some drives (for example, Seagate STT3401A Travan) require a very long
118 * timeout, because they don't return an interrupt or clear their busy bit
119 * until after the command completes (even retension commands).
121 #define IDETAPE_WAIT_CMD (900*HZ)
124 * The following parameter is used to select the point in the internal tape fifo
125 * in which we will start to refill the buffer. Decreasing the following
126 * parameter will improve the system's latency and interactive response, while
127 * using a high value might improve system throughput.
129 #define IDETAPE_FIFO_THRESHOLD 2
132 * DSC polling parameters.
134 * Polling for DSC (a single bit in the status register) is a very important
135 * function in ide-tape. There are two cases in which we poll for DSC:
137 * 1. Before a read/write packet command, to ensure that we can transfer data
138 * from/to the tape's data buffers, without causing an actual media access.
139 * In case the tape is not ready yet, we take out our request from the device
140 * request queue, so that ide.c could service requests from the other device
141 * on the same interface in the meantime.
143 * 2. After the successful initialization of a "media access packet command",
144 * which is a command that can take a long time to complete (the interval can
145 * range from several seconds to even an hour). Again, we postpone our request
146 * in the middle to free the bus for the other device. The polling frequency
147 * here should be lower than the read/write frequency since those media access
148 * commands are slow. We start from a "fast" frequency - IDETAPE_DSC_MA_FAST
149 * (1 second), and if we don't receive DSC after IDETAPE_DSC_MA_THRESHOLD
150 * (5 min), we switch it to a lower frequency - IDETAPE_DSC_MA_SLOW (1 min).
152 * We also set a timeout for the timer, in case something goes wrong. The
153 * timeout should be longer then the maximum execution time of a tape operation.
157 #define IDETAPE_DSC_RW_MIN 5*HZ/100 /* 50 msec */
158 #define IDETAPE_DSC_RW_MAX 40*HZ/100 /* 400 msec */
159 #define IDETAPE_DSC_RW_TIMEOUT 2*60*HZ /* 2 minutes */
160 #define IDETAPE_DSC_MA_FAST 2*HZ /* 2 seconds */
161 #define IDETAPE_DSC_MA_THRESHOLD 5*60*HZ /* 5 minutes */
162 #define IDETAPE_DSC_MA_SLOW 30*HZ /* 30 seconds */
163 #define IDETAPE_DSC_MA_TIMEOUT 2*60*60*HZ /* 2 hours */
165 /*************************** End of tunable parameters ***********************/
167 /* Read/Write error simulation */
168 #define SIMULATE_ERRORS 0
170 /* tape directions */
172 IDETAPE_DIR_NONE = (1 << 0),
173 IDETAPE_DIR_READ = (1 << 1),
174 IDETAPE_DIR_WRITE = (1 << 2),
180 struct idetape_bh *b_reqnext;
184 typedef struct idetape_packet_command_s {
185 /* Actual packet bytes */
187 /* On each retry, we increment retries */
191 /* Bytes to transfer */
192 int request_transfer;
193 /* Bytes actually transferred */
194 int actually_transferred;
195 /* Size of our data buffer */
197 struct idetape_bh *bh;
202 /* Pointer into the above buffer */
203 u8 *current_position;
204 /* Called when this packet command is completed */
205 ide_startstop_t (*callback) (ide_drive_t *);
206 /* Temporary buffer */
207 u8 pc_buffer[IDETAPE_PC_BUFFER_SIZE];
208 /* Status/Action bit flags: long for set_bit */
212 /* Packet command flag bits. */
214 /* Set when an error is considered normal - We won't retry */
215 PC_FLAG_ABORT = (1 << 0),
216 /* 1 When polling for DSC on a media access command */
217 PC_FLAG_WAIT_FOR_DSC = (1 << 1),
218 /* 1 when we prefer to use DMA if possible */
219 PC_FLAG_DMA_RECOMMENDED = (1 << 2),
220 /* 1 while DMA in progress */
221 PC_FLAG_DMA_IN_PROGRESS = (1 << 3),
222 /* 1 when encountered problem during DMA */
223 PC_FLAG_DMA_ERROR = (1 << 4),
225 PC_FLAG_WRITING = (1 << 5),
228 /* Tape door status */
229 #define DOOR_UNLOCKED 0
230 #define DOOR_LOCKED 1
231 #define DOOR_EXPLICITLY_LOCKED 2
233 /* Some defines for the SPACE command */
234 #define IDETAPE_SPACE_OVER_FILEMARK 1
235 #define IDETAPE_SPACE_TO_EOD 3
237 /* Some defines for the LOAD UNLOAD command */
238 #define IDETAPE_LU_LOAD_MASK 1
239 #define IDETAPE_LU_RETENSION_MASK 2
240 #define IDETAPE_LU_EOT_MASK 4
243 * Special requests for our block device strategy routine.
245 * In order to service a character device command, we add special requests to
246 * the tail of our block device request queue and wait for their completion.
250 REQ_IDETAPE_PC1 = (1 << 0), /* packet command (first stage) */
251 REQ_IDETAPE_PC2 = (1 << 1), /* packet command (second stage) */
252 REQ_IDETAPE_READ = (1 << 2),
253 REQ_IDETAPE_WRITE = (1 << 3),
256 /* Error codes returned in rq->errors to the higher part of the driver. */
257 #define IDETAPE_ERROR_GENERAL 101
258 #define IDETAPE_ERROR_FILEMARK 102
259 #define IDETAPE_ERROR_EOD 103
261 /* Structures related to the SELECT SENSE / MODE SENSE packet commands. */
262 #define IDETAPE_BLOCK_DESCRIPTOR 0
263 #define IDETAPE_CAPABILITIES_PAGE 0x2a
265 /* Tape flag bits values. */
267 IDETAPE_FLAG_IGNORE_DSC = (1 << 0),
268 /* 0 When the tape position is unknown */
269 IDETAPE_FLAG_ADDRESS_VALID = (1 << 1),
270 /* Device already opened */
271 IDETAPE_FLAG_BUSY = (1 << 2),
272 /* Error detected in a pipeline stage */
273 IDETAPE_FLAG_PIPELINE_ERR = (1 << 3),
274 /* Attempt to auto-detect the current user block size */
275 IDETAPE_FLAG_DETECT_BS = (1 << 4),
276 /* Currently on a filemark */
277 IDETAPE_FLAG_FILEMARK = (1 << 5),
278 /* DRQ interrupt device */
279 IDETAPE_FLAG_DRQ_INTERRUPT = (1 << 6),
280 /* pipeline active */
281 IDETAPE_FLAG_PIPELINE_ACTIVE = (1 << 7),
282 /* 0 = no tape is loaded, so we don't rewind after ejecting */
283 IDETAPE_FLAG_MEDIUM_PRESENT = (1 << 8),
286 /* A pipeline stage. */
287 typedef struct idetape_stage_s {
288 struct request rq; /* The corresponding request */
289 struct idetape_bh *bh; /* The data buffers */
290 struct idetape_stage_s *next; /* Pointer to the next stage */
294 * Most of our global data which we need to save even as we leave the driver due
295 * to an interrupt or a timer event is stored in the struct defined below.
297 typedef struct ide_tape_obj {
299 ide_driver_t *driver;
300 struct gendisk *disk;
304 * Since a typical character device operation requires more
305 * than one packet command, we provide here enough memory
306 * for the maximum of interconnected packet commands.
307 * The packet commands are stored in the circular array pc_stack.
308 * pc_stack_index points to the last used entry, and warps around
309 * to the start when we get to the last array entry.
311 * pc points to the current processed packet command.
313 * failed_pc points to the last failed packet command, or contains
314 * NULL if we do not need to retry any packet command. This is
315 * required since an additional packet command is needed before the
316 * retry, to get detailed information on what went wrong.
318 /* Current packet command */
320 /* Last failed packet command */
321 idetape_pc_t *failed_pc;
322 /* Packet command stack */
323 idetape_pc_t pc_stack[IDETAPE_PC_STACK];
324 /* Next free packet command storage space */
326 struct request rq_stack[IDETAPE_PC_STACK];
327 /* We implement a circular array */
331 * DSC polling variables.
333 * While polling for DSC we use postponed_rq to postpone the current
334 * request so that ide.c will be able to service pending requests on the
335 * other device. Note that at most we will have only one DSC (usually
336 * data transfer) request in the device request queue. Additional
337 * requests can be queued in our internal pipeline, but they will be
338 * visible to ide.c only one at a time.
340 struct request *postponed_rq;
341 /* The time in which we started polling for DSC */
342 unsigned long dsc_polling_start;
343 /* Timer used to poll for dsc */
344 struct timer_list dsc_timer;
345 /* Read/Write dsc polling frequency */
346 unsigned long best_dsc_rw_freq;
347 unsigned long dsc_poll_freq;
348 unsigned long dsc_timeout;
350 /* Read position information */
353 unsigned int first_frame;
355 /* Last error information */
356 u8 sense_key, asc, ascq;
358 /* Character device operation */
362 /* Current character device data transfer direction */
365 /* tape block size, usually 512 or 1024 bytes */
366 unsigned short blk_size;
369 /* Copy of the tape's Capabilities and Mechanical Page */
373 * Active data transfer request parameters.
375 * At most, there is only one ide-tape originated data transfer request
376 * in the device request queue. This allows ide.c to easily service
377 * requests from the other device when we postpone our active request.
378 * In the pipelined operation mode, we use our internal pipeline
379 * structure to hold more data requests. The data buffer size is chosen
380 * based on the tape's recommendation.
382 /* ptr to the request which is waiting in the device request queue */
383 struct request *active_data_rq;
384 /* Data buffer size chosen based on the tape's recommendation */
386 idetape_stage_t *merge_stage;
387 int merge_stage_size;
388 struct idetape_bh *bh;
393 * Pipeline parameters.
395 * To accomplish non-pipelined mode, we simply set the following
396 * variables to zero (or NULL, where appropriate).
398 /* Number of currently used stages */
400 /* Number of pending stages */
401 int nr_pending_stages;
402 /* We will not allocate more than this number of stages */
403 int max_stages, min_pipeline, max_pipeline;
404 /* The first stage which will be removed from the pipeline */
405 idetape_stage_t *first_stage;
406 /* The currently active stage */
407 idetape_stage_t *active_stage;
408 /* Will be serviced after the currently active request */
409 idetape_stage_t *next_stage;
410 /* New requests will be added to the pipeline here */
411 idetape_stage_t *last_stage;
412 /* Optional free stage which we can use */
413 idetape_stage_t *cache_stage;
415 /* Wasted space in each stage */
418 /* Status/Action flags: long for set_bit */
420 /* protects the ide-tape queue */
423 /* Measures average tape speed */
424 unsigned long avg_time;
428 /* the door is currently locked */
430 /* the tape hardware is write protected */
432 /* the tape is write protected (hardware or opened as read-only) */
436 * Limit the number of times a request can be postponed, to avoid an
437 * infinite postpone deadlock.
442 * Measures number of frames:
444 * 1. written/read to/from the driver pipeline (pipeline_head).
445 * 2. written/read to/from the tape buffers (idetape_bh).
446 * 3. written/read by the tape to/from the media (tape_head).
453 /* Speed control at the tape buffers input/output */
454 unsigned long insert_time;
457 int max_insert_speed;
458 int measure_insert_time;
460 /* Speed regulation negative feedback loop */
462 int pipeline_head_speed;
463 int controlled_pipeline_head_speed;
464 int uncontrolled_pipeline_head_speed;
465 int controlled_last_pipeline_head;
466 unsigned long uncontrolled_pipeline_head_time;
467 unsigned long controlled_pipeline_head_time;
468 int controlled_previous_pipeline_head;
469 int uncontrolled_previous_pipeline_head;
470 unsigned long controlled_previous_head_time;
471 unsigned long uncontrolled_previous_head_time;
472 int restart_speed_control_req;
477 static DEFINE_MUTEX(idetape_ref_mutex);
479 static struct class *idetape_sysfs_class;
481 #define to_ide_tape(obj) container_of(obj, struct ide_tape_obj, kref)
483 #define ide_tape_g(disk) \
484 container_of((disk)->private_data, struct ide_tape_obj, driver)
486 static struct ide_tape_obj *ide_tape_get(struct gendisk *disk)
488 struct ide_tape_obj *tape = NULL;
490 mutex_lock(&idetape_ref_mutex);
491 tape = ide_tape_g(disk);
493 kref_get(&tape->kref);
494 mutex_unlock(&idetape_ref_mutex);
498 static void ide_tape_release(struct kref *);
500 static void ide_tape_put(struct ide_tape_obj *tape)
502 mutex_lock(&idetape_ref_mutex);
503 kref_put(&tape->kref, ide_tape_release);
504 mutex_unlock(&idetape_ref_mutex);
508 * The variables below are used for the character device interface. Additional
509 * state variables are defined in our ide_drive_t structure.
511 static struct ide_tape_obj *idetape_devs[MAX_HWIFS * MAX_DRIVES];
513 #define ide_tape_f(file) ((file)->private_data)
515 static struct ide_tape_obj *ide_tape_chrdev_get(unsigned int i)
517 struct ide_tape_obj *tape = NULL;
519 mutex_lock(&idetape_ref_mutex);
520 tape = idetape_devs[i];
522 kref_get(&tape->kref);
523 mutex_unlock(&idetape_ref_mutex);
527 static void idetape_input_buffers(ide_drive_t *drive, idetape_pc_t *pc,
530 struct idetape_bh *bh = pc->bh;
535 printk(KERN_ERR "ide-tape: bh == NULL in "
536 "idetape_input_buffers\n");
537 ide_atapi_discard_data(drive, bcount);
541 (unsigned int)(bh->b_size - atomic_read(&bh->b_count)),
543 HWIF(drive)->atapi_input_bytes(drive, bh->b_data +
544 atomic_read(&bh->b_count), count);
546 atomic_add(count, &bh->b_count);
547 if (atomic_read(&bh->b_count) == bh->b_size) {
550 atomic_set(&bh->b_count, 0);
556 static void idetape_output_buffers(ide_drive_t *drive, idetape_pc_t *pc,
559 struct idetape_bh *bh = pc->bh;
564 printk(KERN_ERR "ide-tape: bh == NULL in %s\n",
568 count = min((unsigned int)pc->b_count, (unsigned int)bcount);
569 HWIF(drive)->atapi_output_bytes(drive, pc->b_data, count);
572 pc->b_count -= count;
577 pc->b_data = bh->b_data;
578 pc->b_count = atomic_read(&bh->b_count);
584 static void idetape_update_buffers(idetape_pc_t *pc)
586 struct idetape_bh *bh = pc->bh;
588 unsigned int bcount = pc->actually_transferred;
590 if (pc->flags & PC_FLAG_WRITING)
594 printk(KERN_ERR "ide-tape: bh == NULL in %s\n",
598 count = min((unsigned int)bh->b_size, (unsigned int)bcount);
599 atomic_set(&bh->b_count, count);
600 if (atomic_read(&bh->b_count) == bh->b_size)
608 * idetape_next_pc_storage returns a pointer to a place in which we can
609 * safely store a packet command, even though we intend to leave the
610 * driver. A storage space for a maximum of IDETAPE_PC_STACK packet
611 * commands is allocated at initialization time.
613 static idetape_pc_t *idetape_next_pc_storage(ide_drive_t *drive)
615 idetape_tape_t *tape = drive->driver_data;
617 debug_log(DBG_PCRQ_STACK, "pc_stack_index=%d\n", tape->pc_stack_index);
619 if (tape->pc_stack_index == IDETAPE_PC_STACK)
620 tape->pc_stack_index = 0;
621 return (&tape->pc_stack[tape->pc_stack_index++]);
625 * idetape_next_rq_storage is used along with idetape_next_pc_storage.
626 * Since we queue packet commands in the request queue, we need to
627 * allocate a request, along with the allocation of a packet command.
630 /**************************************************************
632 * This should get fixed to use kmalloc(.., GFP_ATOMIC) *
633 * followed later on by kfree(). -ml *
635 **************************************************************/
637 static struct request *idetape_next_rq_storage(ide_drive_t *drive)
639 idetape_tape_t *tape = drive->driver_data;
641 debug_log(DBG_PCRQ_STACK, "rq_stack_index=%d\n", tape->rq_stack_index);
643 if (tape->rq_stack_index == IDETAPE_PC_STACK)
644 tape->rq_stack_index = 0;
645 return (&tape->rq_stack[tape->rq_stack_index++]);
648 static void idetape_init_pc(idetape_pc_t *pc)
650 memset(pc->c, 0, 12);
653 pc->request_transfer = 0;
654 pc->buffer = pc->pc_buffer;
655 pc->buffer_size = IDETAPE_PC_BUFFER_SIZE;
661 * called on each failed packet command retry to analyze the request sense. We
662 * currently do not utilize this information.
664 static void idetape_analyze_error(ide_drive_t *drive, u8 *sense)
666 idetape_tape_t *tape = drive->driver_data;
667 idetape_pc_t *pc = tape->failed_pc;
669 tape->sense_key = sense[2] & 0xF;
670 tape->asc = sense[12];
671 tape->ascq = sense[13];
673 debug_log(DBG_ERR, "pc = %x, sense key = %x, asc = %x, ascq = %x\n",
674 pc->c[0], tape->sense_key, tape->asc, tape->ascq);
676 /* Correct pc->actually_transferred by asking the tape. */
677 if (pc->flags & PC_FLAG_DMA_ERROR) {
678 pc->actually_transferred = pc->request_transfer -
680 be32_to_cpu(get_unaligned((u32 *)&sense[3]));
681 idetape_update_buffers(pc);
685 * If error was the result of a zero-length read or write command,
686 * with sense key=5, asc=0x22, ascq=0, let it slide. Some drives
687 * (i.e. Seagate STT3401A Travan) don't support 0-length read/writes.
689 if ((pc->c[0] == READ_6 || pc->c[0] == WRITE_6)
691 && pc->c[4] == 0 && pc->c[3] == 0 && pc->c[2] == 0) {
692 if (tape->sense_key == 5) {
693 /* don't report an error, everything's ok */
695 /* don't retry read/write */
696 pc->flags |= PC_FLAG_ABORT;
699 if (pc->c[0] == READ_6 && (sense[2] & 0x80)) {
700 pc->error = IDETAPE_ERROR_FILEMARK;
701 pc->flags |= PC_FLAG_ABORT;
703 if (pc->c[0] == WRITE_6) {
704 if ((sense[2] & 0x40) || (tape->sense_key == 0xd
705 && tape->asc == 0x0 && tape->ascq == 0x2)) {
706 pc->error = IDETAPE_ERROR_EOD;
707 pc->flags |= PC_FLAG_ABORT;
710 if (pc->c[0] == READ_6 || pc->c[0] == WRITE_6) {
711 if (tape->sense_key == 8) {
712 pc->error = IDETAPE_ERROR_EOD;
713 pc->flags |= PC_FLAG_ABORT;
715 if (!(pc->flags & PC_FLAG_ABORT) &&
716 pc->actually_transferred)
717 pc->retries = IDETAPE_MAX_PC_RETRIES + 1;
721 static void idetape_activate_next_stage(ide_drive_t *drive)
723 idetape_tape_t *tape = drive->driver_data;
724 idetape_stage_t *stage = tape->next_stage;
725 struct request *rq = &stage->rq;
727 debug_log(DBG_PROCS, "Enter %s\n", __func__);
730 printk(KERN_ERR "ide-tape: bug: Trying to activate a non"
731 " existing stage\n");
735 rq->rq_disk = tape->disk;
737 rq->special = (void *)stage->bh;
738 tape->active_data_rq = rq;
739 tape->active_stage = stage;
740 tape->next_stage = stage->next;
743 /* Free a stage along with its related buffers completely. */
744 static void __idetape_kfree_stage(idetape_stage_t *stage)
746 struct idetape_bh *prev_bh, *bh = stage->bh;
750 if (bh->b_data != NULL) {
751 size = (int) bh->b_size;
753 free_page((unsigned long) bh->b_data);
755 bh->b_data += PAGE_SIZE;
765 static void idetape_kfree_stage(idetape_tape_t *tape, idetape_stage_t *stage)
767 __idetape_kfree_stage(stage);
771 * Remove tape->first_stage from the pipeline. The caller should avoid race
774 static void idetape_remove_stage_head(ide_drive_t *drive)
776 idetape_tape_t *tape = drive->driver_data;
777 idetape_stage_t *stage;
779 debug_log(DBG_PROCS, "Enter %s\n", __func__);
781 if (tape->first_stage == NULL) {
782 printk(KERN_ERR "ide-tape: bug: tape->first_stage is NULL\n");
785 if (tape->active_stage == tape->first_stage) {
786 printk(KERN_ERR "ide-tape: bug: Trying to free our active "
790 stage = tape->first_stage;
791 tape->first_stage = stage->next;
792 idetape_kfree_stage(tape, stage);
794 if (tape->first_stage == NULL) {
795 tape->last_stage = NULL;
796 if (tape->next_stage != NULL)
797 printk(KERN_ERR "ide-tape: bug: tape->next_stage !="
800 printk(KERN_ERR "ide-tape: bug: nr_stages should be 0 "
806 * This will free all the pipeline stages starting from new_last_stage->next
807 * to the end of the list, and point tape->last_stage to new_last_stage.
809 static void idetape_abort_pipeline(ide_drive_t *drive,
810 idetape_stage_t *new_last_stage)
812 idetape_tape_t *tape = drive->driver_data;
813 idetape_stage_t *stage = new_last_stage->next;
814 idetape_stage_t *nstage;
816 debug_log(DBG_PROCS, "%s: Enter %s\n", tape->name, __func__);
819 nstage = stage->next;
820 idetape_kfree_stage(tape, stage);
822 --tape->nr_pending_stages;
826 new_last_stage->next = NULL;
827 tape->last_stage = new_last_stage;
828 tape->next_stage = NULL;
832 * Finish servicing a request and insert a pending pipeline request into the
835 static int idetape_end_request(ide_drive_t *drive, int uptodate, int nr_sects)
837 struct request *rq = HWGROUP(drive)->rq;
838 idetape_tape_t *tape = drive->driver_data;
841 int remove_stage = 0;
842 idetape_stage_t *active_stage;
844 debug_log(DBG_PROCS, "Enter %s\n", __func__);
847 case 0: error = IDETAPE_ERROR_GENERAL; break;
848 case 1: error = 0; break;
849 default: error = uptodate;
853 tape->failed_pc = NULL;
855 if (!blk_special_request(rq)) {
856 ide_end_request(drive, uptodate, nr_sects);
860 spin_lock_irqsave(&tape->lock, flags);
862 /* The request was a pipelined data transfer request */
863 if (tape->active_data_rq == rq) {
864 active_stage = tape->active_stage;
865 tape->active_stage = NULL;
866 tape->active_data_rq = NULL;
867 tape->nr_pending_stages--;
868 if (rq->cmd[0] & REQ_IDETAPE_WRITE) {
871 set_bit(IDETAPE_FLAG_PIPELINE_ERR,
873 if (error == IDETAPE_ERROR_EOD)
874 idetape_abort_pipeline(drive,
877 } else if (rq->cmd[0] & REQ_IDETAPE_READ) {
878 if (error == IDETAPE_ERROR_EOD) {
879 set_bit(IDETAPE_FLAG_PIPELINE_ERR,
881 idetape_abort_pipeline(drive, active_stage);
884 if (tape->next_stage != NULL) {
885 idetape_activate_next_stage(drive);
887 /* Insert the next request into the request queue. */
888 (void)ide_do_drive_cmd(drive, tape->active_data_rq,
892 * This is a part of the feedback loop which tries to
893 * find the optimum number of stages. We are starting
894 * from a minimum maximum number of stages, and if we
895 * sense that the pipeline is empty, we try to increase
896 * it, until we reach the user compile time memory
899 int i = (tape->max_pipeline - tape->min_pipeline) / 10;
901 tape->max_stages += max(i, 1);
902 tape->max_stages = max(tape->max_stages,
904 tape->max_stages = min(tape->max_stages,
908 ide_end_drive_cmd(drive, 0, 0);
911 idetape_remove_stage_head(drive);
912 if (tape->active_data_rq == NULL)
913 clear_bit(IDETAPE_FLAG_PIPELINE_ACTIVE, &tape->flags);
914 spin_unlock_irqrestore(&tape->lock, flags);
918 static ide_startstop_t idetape_request_sense_callback(ide_drive_t *drive)
920 idetape_tape_t *tape = drive->driver_data;
922 debug_log(DBG_PROCS, "Enter %s\n", __func__);
924 if (!tape->pc->error) {
925 idetape_analyze_error(drive, tape->pc->buffer);
926 idetape_end_request(drive, 1, 0);
928 printk(KERN_ERR "ide-tape: Error in REQUEST SENSE itself - "
929 "Aborting request!\n");
930 idetape_end_request(drive, 0, 0);
935 static void idetape_create_request_sense_cmd(idetape_pc_t *pc)
938 pc->c[0] = REQUEST_SENSE;
940 pc->request_transfer = 20;
941 pc->callback = &idetape_request_sense_callback;
944 static void idetape_init_rq(struct request *rq, u8 cmd)
946 memset(rq, 0, sizeof(*rq));
947 rq->cmd_type = REQ_TYPE_SPECIAL;
952 * Generate a new packet command request in front of the request queue, before
953 * the current request, so that it will be processed immediately, on the next
954 * pass through the driver. The function below is called from the request
955 * handling part of the driver (the "bottom" part). Safe storage for the request
956 * should be allocated with ide_tape_next_{pc,rq}_storage() prior to that.
958 * Memory for those requests is pre-allocated at initialization time, and is
959 * limited to IDETAPE_PC_STACK requests. We assume that we have enough space for
960 * the maximum possible number of inter-dependent packet commands.
962 * The higher level of the driver - The ioctl handler and the character device
963 * handling functions should queue request to the lower level part and wait for
964 * their completion using idetape_queue_pc_tail or idetape_queue_rw_tail.
966 static void idetape_queue_pc_head(ide_drive_t *drive, idetape_pc_t *pc,
969 struct ide_tape_obj *tape = drive->driver_data;
971 idetape_init_rq(rq, REQ_IDETAPE_PC1);
972 rq->buffer = (char *) pc;
973 rq->rq_disk = tape->disk;
974 (void) ide_do_drive_cmd(drive, rq, ide_preempt);
978 * idetape_retry_pc is called when an error was detected during the
979 * last packet command. We queue a request sense packet command in
980 * the head of the request list.
982 static ide_startstop_t idetape_retry_pc (ide_drive_t *drive)
984 idetape_tape_t *tape = drive->driver_data;
988 (void)ide_read_error(drive);
989 pc = idetape_next_pc_storage(drive);
990 rq = idetape_next_rq_storage(drive);
991 idetape_create_request_sense_cmd(pc);
992 set_bit(IDETAPE_FLAG_IGNORE_DSC, &tape->flags);
993 idetape_queue_pc_head(drive, pc, rq);
998 * Postpone the current request so that ide.c will be able to service requests
999 * from another device on the same hwgroup while we are polling for DSC.
1001 static void idetape_postpone_request(ide_drive_t *drive)
1003 idetape_tape_t *tape = drive->driver_data;
1005 debug_log(DBG_PROCS, "Enter %s\n", __func__);
1007 tape->postponed_rq = HWGROUP(drive)->rq;
1008 ide_stall_queue(drive, tape->dsc_poll_freq);
1011 typedef void idetape_io_buf(ide_drive_t *, idetape_pc_t *, unsigned int);
1014 * This is the usual interrupt handler which will be called during a packet
1015 * command. We will transfer some of the data (as requested by the drive) and
1016 * will re-point interrupt handler to us. When data transfer is finished, we
1017 * will act according to the algorithm described before
1020 static ide_startstop_t idetape_pc_intr(ide_drive_t *drive)
1022 ide_hwif_t *hwif = drive->hwif;
1023 idetape_tape_t *tape = drive->driver_data;
1024 idetape_pc_t *pc = tape->pc;
1025 xfer_func_t *xferfunc;
1026 idetape_io_buf *iobuf;
1029 static int error_sim_count;
1034 debug_log(DBG_PROCS, "Enter %s - interrupt handler\n", __func__);
1036 /* Clear the interrupt */
1037 stat = ide_read_status(drive);
1039 if (pc->flags & PC_FLAG_DMA_IN_PROGRESS) {
1040 if (hwif->ide_dma_end(drive) || (stat & ERR_STAT)) {
1042 * A DMA error is sometimes expected. For example,
1043 * if the tape is crossing a filemark during a
1044 * READ command, it will issue an irq and position
1045 * itself before the filemark, so that only a partial
1046 * data transfer will occur (which causes the DMA
1047 * error). In that case, we will later ask the tape
1048 * how much bytes of the original request were
1049 * actually transferred (we can't receive that
1050 * information from the DMA engine on most chipsets).
1054 * On the contrary, a DMA error is never expected;
1055 * it usually indicates a hardware error or abort.
1056 * If the tape crosses a filemark during a READ
1057 * command, it will issue an irq and position itself
1058 * after the filemark (not before). Only a partial
1059 * data transfer will occur, but no DMA error.
1062 pc->flags |= PC_FLAG_DMA_ERROR;
1064 pc->actually_transferred = pc->request_transfer;
1065 idetape_update_buffers(pc);
1067 debug_log(DBG_PROCS, "DMA finished\n");
1071 /* No more interrupts */
1072 if ((stat & DRQ_STAT) == 0) {
1073 debug_log(DBG_SENSE, "Packet command completed, %d bytes"
1074 " transferred\n", pc->actually_transferred);
1076 pc->flags &= ~PC_FLAG_DMA_IN_PROGRESS;
1080 if ((pc->c[0] == WRITE_6 || pc->c[0] == READ_6) &&
1081 (++error_sim_count % 100) == 0) {
1082 printk(KERN_INFO "ide-tape: %s: simulating error\n",
1087 if ((stat & ERR_STAT) && pc->c[0] == REQUEST_SENSE)
1089 if ((stat & ERR_STAT) || (pc->flags & PC_FLAG_DMA_ERROR)) {
1090 /* Error detected */
1091 debug_log(DBG_ERR, "%s: I/O error\n", tape->name);
1093 if (pc->c[0] == REQUEST_SENSE) {
1094 printk(KERN_ERR "ide-tape: I/O error in request"
1095 " sense command\n");
1096 return ide_do_reset(drive);
1098 debug_log(DBG_ERR, "[cmd %x]: check condition\n",
1101 /* Retry operation */
1102 return idetape_retry_pc(drive);
1105 if ((pc->flags & PC_FLAG_WAIT_FOR_DSC) &&
1106 (stat & SEEK_STAT) == 0) {
1107 /* Media access command */
1108 tape->dsc_polling_start = jiffies;
1109 tape->dsc_poll_freq = IDETAPE_DSC_MA_FAST;
1110 tape->dsc_timeout = jiffies + IDETAPE_DSC_MA_TIMEOUT;
1111 /* Allow ide.c to handle other requests */
1112 idetape_postpone_request(drive);
1115 if (tape->failed_pc == pc)
1116 tape->failed_pc = NULL;
1117 /* Command finished - Call the callback function */
1118 return pc->callback(drive);
1121 if (pc->flags & PC_FLAG_DMA_IN_PROGRESS) {
1122 pc->flags &= ~PC_FLAG_DMA_IN_PROGRESS;
1123 printk(KERN_ERR "ide-tape: The tape wants to issue more "
1124 "interrupts in DMA mode\n");
1125 printk(KERN_ERR "ide-tape: DMA disabled, reverting to PIO\n");
1127 return ide_do_reset(drive);
1129 /* Get the number of bytes to transfer on this interrupt. */
1130 bcount = (hwif->INB(hwif->io_ports[IDE_BCOUNTH_OFFSET]) << 8) |
1131 hwif->INB(hwif->io_ports[IDE_BCOUNTL_OFFSET]);
1133 ireason = hwif->INB(hwif->io_ports[IDE_IREASON_OFFSET]);
1136 printk(KERN_ERR "ide-tape: CoD != 0 in %s\n", __func__);
1137 return ide_do_reset(drive);
1139 if (((ireason & IO) == IO) == !!(pc->flags & PC_FLAG_WRITING)) {
1140 /* Hopefully, we will never get here */
1141 printk(KERN_ERR "ide-tape: We wanted to %s, ",
1142 (ireason & IO) ? "Write" : "Read");
1143 printk(KERN_ERR "ide-tape: but the tape wants us to %s !\n",
1144 (ireason & IO) ? "Read" : "Write");
1145 return ide_do_reset(drive);
1147 if (!(pc->flags & PC_FLAG_WRITING)) {
1148 /* Reading - Check that we have enough space */
1149 temp = pc->actually_transferred + bcount;
1150 if (temp > pc->request_transfer) {
1151 if (temp > pc->buffer_size) {
1152 printk(KERN_ERR "ide-tape: The tape wants to "
1153 "send us more data than expected "
1154 "- discarding data\n");
1155 ide_atapi_discard_data(drive, bcount);
1156 ide_set_handler(drive, &idetape_pc_intr,
1157 IDETAPE_WAIT_CMD, NULL);
1160 debug_log(DBG_SENSE, "The tape wants to send us more "
1161 "data than expected - allowing transfer\n");
1163 iobuf = &idetape_input_buffers;
1164 xferfunc = hwif->atapi_input_bytes;
1166 iobuf = &idetape_output_buffers;
1167 xferfunc = hwif->atapi_output_bytes;
1171 iobuf(drive, pc, bcount);
1173 xferfunc(drive, pc->current_position, bcount);
1175 /* Update the current position */
1176 pc->actually_transferred += bcount;
1177 pc->current_position += bcount;
1179 debug_log(DBG_SENSE, "[cmd %x] transferred %d bytes on that intr.\n",
1182 /* And set the interrupt handler again */
1183 ide_set_handler(drive, &idetape_pc_intr, IDETAPE_WAIT_CMD, NULL);
1188 * Packet Command Interface
1190 * The current Packet Command is available in tape->pc, and will not change
1191 * until we finish handling it. Each packet command is associated with a
1192 * callback function that will be called when the command is finished.
1194 * The handling will be done in three stages:
1196 * 1. idetape_issue_pc will send the packet command to the drive, and will set
1197 * the interrupt handler to idetape_pc_intr.
1199 * 2. On each interrupt, idetape_pc_intr will be called. This step will be
1200 * repeated until the device signals us that no more interrupts will be issued.
1202 * 3. ATAPI Tape media access commands have immediate status with a delayed
1203 * process. In case of a successful initiation of a media access packet command,
1204 * the DSC bit will be set when the actual execution of the command is finished.
1205 * Since the tape drive will not issue an interrupt, we have to poll for this
1206 * event. In this case, we define the request as "low priority request" by
1207 * setting rq_status to IDETAPE_RQ_POSTPONED, set a timer to poll for DSC and
1210 * ide.c will then give higher priority to requests which originate from the
1211 * other device, until will change rq_status to RQ_ACTIVE.
1213 * 4. When the packet command is finished, it will be checked for errors.
1215 * 5. In case an error was found, we queue a request sense packet command in
1216 * front of the request queue and retry the operation up to
1217 * IDETAPE_MAX_PC_RETRIES times.
1219 * 6. In case no error was found, or we decided to give up and not to retry
1220 * again, the callback function will be called and then we will handle the next
1223 static ide_startstop_t idetape_transfer_pc(ide_drive_t *drive)
1225 ide_hwif_t *hwif = drive->hwif;
1226 idetape_tape_t *tape = drive->driver_data;
1227 idetape_pc_t *pc = tape->pc;
1229 ide_startstop_t startstop;
1232 if (ide_wait_stat(&startstop, drive, DRQ_STAT, BUSY_STAT, WAIT_READY)) {
1233 printk(KERN_ERR "ide-tape: Strange, packet command initiated "
1234 "yet DRQ isn't asserted\n");
1237 ireason = hwif->INB(hwif->io_ports[IDE_IREASON_OFFSET]);
1238 while (retries-- && ((ireason & CD) == 0 || (ireason & IO))) {
1239 printk(KERN_ERR "ide-tape: (IO,CoD != (0,1) while issuing "
1240 "a packet command, retrying\n");
1242 ireason = hwif->INB(hwif->io_ports[IDE_IREASON_OFFSET]);
1244 printk(KERN_ERR "ide-tape: (IO,CoD != (0,1) while "
1245 "issuing a packet command, ignoring\n");
1250 if ((ireason & CD) == 0 || (ireason & IO)) {
1251 printk(KERN_ERR "ide-tape: (IO,CoD) != (0,1) while issuing "
1252 "a packet command\n");
1253 return ide_do_reset(drive);
1255 /* Set the interrupt routine */
1256 ide_set_handler(drive, &idetape_pc_intr, IDETAPE_WAIT_CMD, NULL);
1257 #ifdef CONFIG_BLK_DEV_IDEDMA
1258 /* Begin DMA, if necessary */
1259 if (pc->flags & PC_FLAG_DMA_IN_PROGRESS)
1260 hwif->dma_start(drive);
1262 /* Send the actual packet */
1263 HWIF(drive)->atapi_output_bytes(drive, pc->c, 12);
1267 static ide_startstop_t idetape_issue_pc(ide_drive_t *drive, idetape_pc_t *pc)
1269 ide_hwif_t *hwif = drive->hwif;
1270 idetape_tape_t *tape = drive->driver_data;
1274 if (tape->pc->c[0] == REQUEST_SENSE &&
1275 pc->c[0] == REQUEST_SENSE) {
1276 printk(KERN_ERR "ide-tape: possible ide-tape.c bug - "
1277 "Two request sense in serial were issued\n");
1280 if (tape->failed_pc == NULL && pc->c[0] != REQUEST_SENSE)
1281 tape->failed_pc = pc;
1282 /* Set the current packet command */
1285 if (pc->retries > IDETAPE_MAX_PC_RETRIES ||
1286 (pc->flags & PC_FLAG_ABORT)) {
1288 * We will "abort" retrying a packet command in case legitimate
1289 * error code was received (crossing a filemark, or end of the
1290 * media, for example).
1292 if (!(pc->flags & PC_FLAG_ABORT)) {
1293 if (!(pc->c[0] == TEST_UNIT_READY &&
1294 tape->sense_key == 2 && tape->asc == 4 &&
1295 (tape->ascq == 1 || tape->ascq == 8))) {
1296 printk(KERN_ERR "ide-tape: %s: I/O error, "
1297 "pc = %2x, key = %2x, "
1298 "asc = %2x, ascq = %2x\n",
1299 tape->name, pc->c[0],
1300 tape->sense_key, tape->asc,
1304 pc->error = IDETAPE_ERROR_GENERAL;
1306 tape->failed_pc = NULL;
1307 return pc->callback(drive);
1309 debug_log(DBG_SENSE, "Retry #%d, cmd = %02X\n", pc->retries, pc->c[0]);
1312 /* We haven't transferred any data yet */
1313 pc->actually_transferred = 0;
1314 pc->current_position = pc->buffer;
1315 /* Request to transfer the entire buffer at once */
1316 bcount = pc->request_transfer;
1318 if (pc->flags & PC_FLAG_DMA_ERROR) {
1319 pc->flags &= ~PC_FLAG_DMA_ERROR;
1320 printk(KERN_WARNING "ide-tape: DMA disabled, "
1321 "reverting to PIO\n");
1324 if ((pc->flags & PC_FLAG_DMA_RECOMMENDED) && drive->using_dma)
1325 dma_ok = !hwif->dma_setup(drive);
1327 ide_pktcmd_tf_load(drive, IDE_TFLAG_NO_SELECT_MASK |
1328 IDE_TFLAG_OUT_DEVICE, bcount, dma_ok);
1331 /* Will begin DMA later */
1332 pc->flags |= PC_FLAG_DMA_IN_PROGRESS;
1333 if (test_bit(IDETAPE_FLAG_DRQ_INTERRUPT, &tape->flags)) {
1334 ide_execute_command(drive, WIN_PACKETCMD, &idetape_transfer_pc,
1335 IDETAPE_WAIT_CMD, NULL);
1338 hwif->OUTB(WIN_PACKETCMD, hwif->io_ports[IDE_COMMAND_OFFSET]);
1339 return idetape_transfer_pc(drive);
1343 static ide_startstop_t idetape_pc_callback(ide_drive_t *drive)
1345 idetape_tape_t *tape = drive->driver_data;
1347 debug_log(DBG_PROCS, "Enter %s\n", __func__);
1349 idetape_end_request(drive, tape->pc->error ? 0 : 1, 0);
1353 /* A mode sense command is used to "sense" tape parameters. */
1354 static void idetape_create_mode_sense_cmd(idetape_pc_t *pc, u8 page_code)
1356 idetape_init_pc(pc);
1357 pc->c[0] = MODE_SENSE;
1358 if (page_code != IDETAPE_BLOCK_DESCRIPTOR)
1359 /* DBD = 1 - Don't return block descriptors */
1361 pc->c[2] = page_code;
1363 * Changed pc->c[3] to 0 (255 will at best return unused info).
1365 * For SCSI this byte is defined as subpage instead of high byte
1366 * of length and some IDE drives seem to interpret it this way
1367 * and return an error when 255 is used.
1370 /* We will just discard data in that case */
1372 if (page_code == IDETAPE_BLOCK_DESCRIPTOR)
1373 pc->request_transfer = 12;
1374 else if (page_code == IDETAPE_CAPABILITIES_PAGE)
1375 pc->request_transfer = 24;
1377 pc->request_transfer = 50;
1378 pc->callback = &idetape_pc_callback;
1381 static void idetape_calculate_speeds(ide_drive_t *drive)
1383 idetape_tape_t *tape = drive->driver_data;
1385 if (time_after(jiffies,
1386 tape->controlled_pipeline_head_time + 120 * HZ)) {
1387 tape->controlled_previous_pipeline_head =
1388 tape->controlled_last_pipeline_head;
1389 tape->controlled_previous_head_time =
1390 tape->controlled_pipeline_head_time;
1391 tape->controlled_last_pipeline_head = tape->pipeline_head;
1392 tape->controlled_pipeline_head_time = jiffies;
1394 if (time_after(jiffies, tape->controlled_pipeline_head_time + 60 * HZ))
1395 tape->controlled_pipeline_head_speed = (tape->pipeline_head -
1396 tape->controlled_last_pipeline_head) * 32 * HZ /
1397 (jiffies - tape->controlled_pipeline_head_time);
1398 else if (time_after(jiffies, tape->controlled_previous_head_time))
1399 tape->controlled_pipeline_head_speed = (tape->pipeline_head -
1400 tape->controlled_previous_pipeline_head) * 32 *
1401 HZ / (jiffies - tape->controlled_previous_head_time);
1403 if (tape->nr_pending_stages < tape->max_stages/*- 1 */) {
1404 /* -1 for read mode error recovery */
1405 if (time_after(jiffies, tape->uncontrolled_previous_head_time +
1407 tape->uncontrolled_pipeline_head_time = jiffies;
1408 tape->uncontrolled_pipeline_head_speed =
1409 (tape->pipeline_head -
1410 tape->uncontrolled_previous_pipeline_head) *
1411 32 * HZ / (jiffies -
1412 tape->uncontrolled_previous_head_time);
1415 tape->uncontrolled_previous_head_time = jiffies;
1416 tape->uncontrolled_previous_pipeline_head = tape->pipeline_head;
1417 if (time_after(jiffies, tape->uncontrolled_pipeline_head_time +
1419 tape->uncontrolled_pipeline_head_time = jiffies;
1422 tape->pipeline_head_speed = max(tape->uncontrolled_pipeline_head_speed,
1423 tape->controlled_pipeline_head_speed);
1425 if (tape->speed_control == 1) {
1426 if (tape->nr_pending_stages >= tape->max_stages / 2)
1427 tape->max_insert_speed = tape->pipeline_head_speed +
1428 (1100 - tape->pipeline_head_speed) * 2 *
1429 (tape->nr_pending_stages - tape->max_stages / 2)
1432 tape->max_insert_speed = 500 +
1433 (tape->pipeline_head_speed - 500) * 2 *
1434 tape->nr_pending_stages / tape->max_stages;
1436 if (tape->nr_pending_stages >= tape->max_stages * 99 / 100)
1437 tape->max_insert_speed = 5000;
1439 tape->max_insert_speed = tape->speed_control;
1441 tape->max_insert_speed = max(tape->max_insert_speed, 500);
1444 static ide_startstop_t idetape_media_access_finished(ide_drive_t *drive)
1446 idetape_tape_t *tape = drive->driver_data;
1447 idetape_pc_t *pc = tape->pc;
1450 stat = ide_read_status(drive);
1452 if (stat & SEEK_STAT) {
1453 if (stat & ERR_STAT) {
1454 /* Error detected */
1455 if (pc->c[0] != TEST_UNIT_READY)
1456 printk(KERN_ERR "ide-tape: %s: I/O error, ",
1458 /* Retry operation */
1459 return idetape_retry_pc(drive);
1462 if (tape->failed_pc == pc)
1463 tape->failed_pc = NULL;
1465 pc->error = IDETAPE_ERROR_GENERAL;
1466 tape->failed_pc = NULL;
1468 return pc->callback(drive);
1471 static ide_startstop_t idetape_rw_callback(ide_drive_t *drive)
1473 idetape_tape_t *tape = drive->driver_data;
1474 struct request *rq = HWGROUP(drive)->rq;
1475 int blocks = tape->pc->actually_transferred / tape->blk_size;
1477 tape->avg_size += blocks * tape->blk_size;
1478 tape->insert_size += blocks * tape->blk_size;
1479 if (tape->insert_size > 1024 * 1024)
1480 tape->measure_insert_time = 1;
1481 if (tape->measure_insert_time) {
1482 tape->measure_insert_time = 0;
1483 tape->insert_time = jiffies;
1484 tape->insert_size = 0;
1486 if (time_after(jiffies, tape->insert_time))
1487 tape->insert_speed = tape->insert_size / 1024 * HZ /
1488 (jiffies - tape->insert_time);
1489 if (time_after_eq(jiffies, tape->avg_time + HZ)) {
1490 tape->avg_speed = tape->avg_size * HZ /
1491 (jiffies - tape->avg_time) / 1024;
1493 tape->avg_time = jiffies;
1495 debug_log(DBG_PROCS, "Enter %s\n", __func__);
1497 tape->first_frame += blocks;
1498 rq->current_nr_sectors -= blocks;
1500 if (!tape->pc->error)
1501 idetape_end_request(drive, 1, 0);
1503 idetape_end_request(drive, tape->pc->error, 0);
1507 static void idetape_create_read_cmd(idetape_tape_t *tape, idetape_pc_t *pc,
1508 unsigned int length, struct idetape_bh *bh)
1510 idetape_init_pc(pc);
1512 put_unaligned(cpu_to_be32(length), (unsigned int *) &pc->c[1]);
1514 pc->callback = &idetape_rw_callback;
1516 atomic_set(&bh->b_count, 0);
1518 pc->buffer_size = length * tape->blk_size;
1519 pc->request_transfer = pc->buffer_size;
1520 if (pc->request_transfer == tape->stage_size)
1521 pc->flags |= PC_FLAG_DMA_RECOMMENDED;
1524 static void idetape_create_write_cmd(idetape_tape_t *tape, idetape_pc_t *pc,
1525 unsigned int length, struct idetape_bh *bh)
1527 idetape_init_pc(pc);
1529 put_unaligned(cpu_to_be32(length), (unsigned int *) &pc->c[1]);
1531 pc->callback = &idetape_rw_callback;
1532 pc->flags |= PC_FLAG_WRITING;
1534 pc->b_data = bh->b_data;
1535 pc->b_count = atomic_read(&bh->b_count);
1537 pc->buffer_size = length * tape->blk_size;
1538 pc->request_transfer = pc->buffer_size;
1539 if (pc->request_transfer == tape->stage_size)
1540 pc->flags |= PC_FLAG_DMA_RECOMMENDED;
1543 static ide_startstop_t idetape_do_request(ide_drive_t *drive,
1544 struct request *rq, sector_t block)
1546 idetape_tape_t *tape = drive->driver_data;
1547 idetape_pc_t *pc = NULL;
1548 struct request *postponed_rq = tape->postponed_rq;
1551 debug_log(DBG_SENSE, "sector: %ld, nr_sectors: %ld,"
1552 " current_nr_sectors: %d\n",
1553 rq->sector, rq->nr_sectors, rq->current_nr_sectors);
1555 if (!blk_special_request(rq)) {
1556 /* We do not support buffer cache originated requests. */
1557 printk(KERN_NOTICE "ide-tape: %s: Unsupported request in "
1558 "request queue (%d)\n", drive->name, rq->cmd_type);
1559 ide_end_request(drive, 0, 0);
1563 /* Retry a failed packet command */
1564 if (tape->failed_pc && tape->pc->c[0] == REQUEST_SENSE)
1565 return idetape_issue_pc(drive, tape->failed_pc);
1567 if (postponed_rq != NULL)
1568 if (rq != postponed_rq) {
1569 printk(KERN_ERR "ide-tape: ide-tape.c bug - "
1570 "Two DSC requests were queued\n");
1571 idetape_end_request(drive, 0, 0);
1575 tape->postponed_rq = NULL;
1578 * If the tape is still busy, postpone our request and service
1579 * the other device meanwhile.
1581 stat = ide_read_status(drive);
1583 if (!drive->dsc_overlap && !(rq->cmd[0] & REQ_IDETAPE_PC2))
1584 set_bit(IDETAPE_FLAG_IGNORE_DSC, &tape->flags);
1586 if (drive->post_reset == 1) {
1587 set_bit(IDETAPE_FLAG_IGNORE_DSC, &tape->flags);
1588 drive->post_reset = 0;
1591 if (time_after(jiffies, tape->insert_time))
1592 tape->insert_speed = tape->insert_size / 1024 * HZ /
1593 (jiffies - tape->insert_time);
1594 idetape_calculate_speeds(drive);
1595 if (!test_and_clear_bit(IDETAPE_FLAG_IGNORE_DSC, &tape->flags) &&
1596 (stat & SEEK_STAT) == 0) {
1597 if (postponed_rq == NULL) {
1598 tape->dsc_polling_start = jiffies;
1599 tape->dsc_poll_freq = tape->best_dsc_rw_freq;
1600 tape->dsc_timeout = jiffies + IDETAPE_DSC_RW_TIMEOUT;
1601 } else if (time_after(jiffies, tape->dsc_timeout)) {
1602 printk(KERN_ERR "ide-tape: %s: DSC timeout\n",
1604 if (rq->cmd[0] & REQ_IDETAPE_PC2) {
1605 idetape_media_access_finished(drive);
1608 return ide_do_reset(drive);
1610 } else if (time_after(jiffies,
1611 tape->dsc_polling_start +
1612 IDETAPE_DSC_MA_THRESHOLD))
1613 tape->dsc_poll_freq = IDETAPE_DSC_MA_SLOW;
1614 idetape_postpone_request(drive);
1617 if (rq->cmd[0] & REQ_IDETAPE_READ) {
1618 tape->buffer_head++;
1619 tape->postpone_cnt = 0;
1620 pc = idetape_next_pc_storage(drive);
1621 idetape_create_read_cmd(tape, pc, rq->current_nr_sectors,
1622 (struct idetape_bh *)rq->special);
1625 if (rq->cmd[0] & REQ_IDETAPE_WRITE) {
1626 tape->buffer_head++;
1627 tape->postpone_cnt = 0;
1628 pc = idetape_next_pc_storage(drive);
1629 idetape_create_write_cmd(tape, pc, rq->current_nr_sectors,
1630 (struct idetape_bh *)rq->special);
1633 if (rq->cmd[0] & REQ_IDETAPE_PC1) {
1634 pc = (idetape_pc_t *) rq->buffer;
1635 rq->cmd[0] &= ~(REQ_IDETAPE_PC1);
1636 rq->cmd[0] |= REQ_IDETAPE_PC2;
1639 if (rq->cmd[0] & REQ_IDETAPE_PC2) {
1640 idetape_media_access_finished(drive);
1645 return idetape_issue_pc(drive, pc);
1648 /* Pipeline related functions */
1649 static inline int idetape_pipeline_active(idetape_tape_t *tape)
1653 rc1 = test_bit(IDETAPE_FLAG_PIPELINE_ACTIVE, &tape->flags);
1654 rc2 = (tape->active_data_rq != NULL);
1659 * The function below uses __get_free_page to allocate a pipeline stage, along
1660 * with all the necessary small buffers which together make a buffer of size
1661 * tape->stage_size (or a bit more). We attempt to combine sequential pages as
1664 * It returns a pointer to the new allocated stage, or NULL if we can't (or
1665 * don't want to) allocate a stage.
1667 * Pipeline stages are optional and are used to increase performance. If we
1668 * can't allocate them, we'll manage without them.
1670 static idetape_stage_t *__idetape_kmalloc_stage(idetape_tape_t *tape, int full,
1673 idetape_stage_t *stage;
1674 struct idetape_bh *prev_bh, *bh;
1675 int pages = tape->pages_per_stage;
1676 char *b_data = NULL;
1678 stage = kmalloc(sizeof(idetape_stage_t), GFP_KERNEL);
1683 stage->bh = kmalloc(sizeof(struct idetape_bh), GFP_KERNEL);
1687 bh->b_reqnext = NULL;
1688 bh->b_data = (char *) __get_free_page(GFP_KERNEL);
1692 memset(bh->b_data, 0, PAGE_SIZE);
1693 bh->b_size = PAGE_SIZE;
1694 atomic_set(&bh->b_count, full ? bh->b_size : 0);
1697 b_data = (char *) __get_free_page(GFP_KERNEL);
1701 memset(b_data, 0, PAGE_SIZE);
1702 if (bh->b_data == b_data + PAGE_SIZE) {
1703 bh->b_size += PAGE_SIZE;
1704 bh->b_data -= PAGE_SIZE;
1706 atomic_add(PAGE_SIZE, &bh->b_count);
1709 if (b_data == bh->b_data + bh->b_size) {
1710 bh->b_size += PAGE_SIZE;
1712 atomic_add(PAGE_SIZE, &bh->b_count);
1716 bh = kmalloc(sizeof(struct idetape_bh), GFP_KERNEL);
1718 free_page((unsigned long) b_data);
1721 bh->b_reqnext = NULL;
1722 bh->b_data = b_data;
1723 bh->b_size = PAGE_SIZE;
1724 atomic_set(&bh->b_count, full ? bh->b_size : 0);
1725 prev_bh->b_reqnext = bh;
1727 bh->b_size -= tape->excess_bh_size;
1729 atomic_sub(tape->excess_bh_size, &bh->b_count);
1732 __idetape_kfree_stage(stage);
1736 static idetape_stage_t *idetape_kmalloc_stage(idetape_tape_t *tape)
1738 idetape_stage_t *cache_stage = tape->cache_stage;
1740 debug_log(DBG_PROCS, "Enter %s\n", __func__);
1742 if (tape->nr_stages >= tape->max_stages)
1744 if (cache_stage != NULL) {
1745 tape->cache_stage = NULL;
1748 return __idetape_kmalloc_stage(tape, 0, 0);
1751 static int idetape_copy_stage_from_user(idetape_tape_t *tape,
1752 idetape_stage_t *stage, const char __user *buf, int n)
1754 struct idetape_bh *bh = tape->bh;
1760 printk(KERN_ERR "ide-tape: bh == NULL in %s\n",
1764 count = min((unsigned int)
1765 (bh->b_size - atomic_read(&bh->b_count)),
1767 if (copy_from_user(bh->b_data + atomic_read(&bh->b_count), buf,
1771 atomic_add(count, &bh->b_count);
1773 if (atomic_read(&bh->b_count) == bh->b_size) {
1776 atomic_set(&bh->b_count, 0);
1783 static int idetape_copy_stage_to_user(idetape_tape_t *tape, char __user *buf,
1784 idetape_stage_t *stage, int n)
1786 struct idetape_bh *bh = tape->bh;
1792 printk(KERN_ERR "ide-tape: bh == NULL in %s\n",
1796 count = min(tape->b_count, n);
1797 if (copy_to_user(buf, tape->b_data, count))
1800 tape->b_data += count;
1801 tape->b_count -= count;
1803 if (!tape->b_count) {
1807 tape->b_data = bh->b_data;
1808 tape->b_count = atomic_read(&bh->b_count);
1815 static void idetape_init_merge_stage(idetape_tape_t *tape)
1817 struct idetape_bh *bh = tape->merge_stage->bh;
1820 if (tape->chrdev_dir == IDETAPE_DIR_WRITE)
1821 atomic_set(&bh->b_count, 0);
1823 tape->b_data = bh->b_data;
1824 tape->b_count = atomic_read(&bh->b_count);
1828 static void idetape_switch_buffers(idetape_tape_t *tape, idetape_stage_t *stage)
1830 struct idetape_bh *tmp;
1833 stage->bh = tape->merge_stage->bh;
1834 tape->merge_stage->bh = tmp;
1835 idetape_init_merge_stage(tape);
1838 /* Add a new stage at the end of the pipeline. */
1839 static void idetape_add_stage_tail(ide_drive_t *drive, idetape_stage_t *stage)
1841 idetape_tape_t *tape = drive->driver_data;
1842 unsigned long flags;
1844 debug_log(DBG_PROCS, "Enter %s\n", __func__);
1846 spin_lock_irqsave(&tape->lock, flags);
1848 if (tape->last_stage != NULL)
1849 tape->last_stage->next = stage;
1851 tape->first_stage = stage;
1852 tape->next_stage = stage;
1853 tape->last_stage = stage;
1854 if (tape->next_stage == NULL)
1855 tape->next_stage = tape->last_stage;
1857 tape->nr_pending_stages++;
1858 spin_unlock_irqrestore(&tape->lock, flags);
1861 /* Install a completion in a pending request and sleep until it is serviced. The
1862 * caller should ensure that the request will not be serviced before we install
1863 * the completion (usually by disabling interrupts).
1865 static void idetape_wait_for_request(ide_drive_t *drive, struct request *rq)
1867 DECLARE_COMPLETION_ONSTACK(wait);
1868 idetape_tape_t *tape = drive->driver_data;
1870 if (rq == NULL || !blk_special_request(rq)) {
1871 printk(KERN_ERR "ide-tape: bug: Trying to sleep on non-valid"
1875 rq->end_io_data = &wait;
1876 rq->end_io = blk_end_sync_rq;
1877 spin_unlock_irq(&tape->lock);
1878 wait_for_completion(&wait);
1879 /* The stage and its struct request have been deallocated */
1880 spin_lock_irq(&tape->lock);
1883 static ide_startstop_t idetape_read_position_callback(ide_drive_t *drive)
1885 idetape_tape_t *tape = drive->driver_data;
1886 u8 *readpos = tape->pc->buffer;
1888 debug_log(DBG_PROCS, "Enter %s\n", __func__);
1890 if (!tape->pc->error) {
1891 debug_log(DBG_SENSE, "BOP - %s\n",
1892 (readpos[0] & 0x80) ? "Yes" : "No");
1893 debug_log(DBG_SENSE, "EOP - %s\n",
1894 (readpos[0] & 0x40) ? "Yes" : "No");
1896 if (readpos[0] & 0x4) {
1897 printk(KERN_INFO "ide-tape: Block location is unknown"
1899 clear_bit(IDETAPE_FLAG_ADDRESS_VALID, &tape->flags);
1900 idetape_end_request(drive, 0, 0);
1902 debug_log(DBG_SENSE, "Block Location - %u\n",
1903 be32_to_cpu(*(u32 *)&readpos[4]));
1905 tape->partition = readpos[1];
1907 be32_to_cpu(*(u32 *)&readpos[4]);
1908 set_bit(IDETAPE_FLAG_ADDRESS_VALID, &tape->flags);
1909 idetape_end_request(drive, 1, 0);
1912 idetape_end_request(drive, 0, 0);
1918 * Write a filemark if write_filemark=1. Flush the device buffers without
1919 * writing a filemark otherwise.
1921 static void idetape_create_write_filemark_cmd(ide_drive_t *drive,
1922 idetape_pc_t *pc, int write_filemark)
1924 idetape_init_pc(pc);
1925 pc->c[0] = WRITE_FILEMARKS;
1926 pc->c[4] = write_filemark;
1927 pc->flags |= PC_FLAG_WAIT_FOR_DSC;
1928 pc->callback = &idetape_pc_callback;
1931 static void idetape_create_test_unit_ready_cmd(idetape_pc_t *pc)
1933 idetape_init_pc(pc);
1934 pc->c[0] = TEST_UNIT_READY;
1935 pc->callback = &idetape_pc_callback;
1939 * We add a special packet command request to the tail of the request queue, and
1940 * wait for it to be serviced. This is not to be called from within the request
1941 * handling part of the driver! We allocate here data on the stack and it is
1942 * valid until the request is finished. This is not the case for the bottom part
1943 * of the driver, where we are always leaving the functions to wait for an
1944 * interrupt or a timer event.
1946 * From the bottom part of the driver, we should allocate safe memory using
1947 * idetape_next_pc_storage() and ide_tape_next_rq_storage(), and add the request
1948 * to the request list without waiting for it to be serviced! In that case, we
1949 * usually use idetape_queue_pc_head().
1951 static int __idetape_queue_pc_tail(ide_drive_t *drive, idetape_pc_t *pc)
1953 struct ide_tape_obj *tape = drive->driver_data;
1956 idetape_init_rq(&rq, REQ_IDETAPE_PC1);
1957 rq.buffer = (char *) pc;
1958 rq.rq_disk = tape->disk;
1959 return ide_do_drive_cmd(drive, &rq, ide_wait);
1962 static void idetape_create_load_unload_cmd(ide_drive_t *drive, idetape_pc_t *pc,
1965 idetape_init_pc(pc);
1966 pc->c[0] = START_STOP;
1968 pc->flags |= PC_FLAG_WAIT_FOR_DSC;
1969 pc->callback = &idetape_pc_callback;
1972 static int idetape_wait_ready(ide_drive_t *drive, unsigned long timeout)
1974 idetape_tape_t *tape = drive->driver_data;
1976 int load_attempted = 0;
1978 /* Wait for the tape to become ready */
1979 set_bit(IDETAPE_FLAG_MEDIUM_PRESENT, &tape->flags);
1981 while (time_before(jiffies, timeout)) {
1982 idetape_create_test_unit_ready_cmd(&pc);
1983 if (!__idetape_queue_pc_tail(drive, &pc))
1985 if ((tape->sense_key == 2 && tape->asc == 4 && tape->ascq == 2)
1986 || (tape->asc == 0x3A)) {
1990 idetape_create_load_unload_cmd(drive, &pc,
1991 IDETAPE_LU_LOAD_MASK);
1992 __idetape_queue_pc_tail(drive, &pc);
1994 /* not about to be ready */
1995 } else if (!(tape->sense_key == 2 && tape->asc == 4 &&
1996 (tape->ascq == 1 || tape->ascq == 8)))
2003 static int idetape_queue_pc_tail(ide_drive_t *drive, idetape_pc_t *pc)
2005 return __idetape_queue_pc_tail(drive, pc);
2008 static int idetape_flush_tape_buffers(ide_drive_t *drive)
2013 idetape_create_write_filemark_cmd(drive, &pc, 0);
2014 rc = idetape_queue_pc_tail(drive, &pc);
2017 idetape_wait_ready(drive, 60 * 5 * HZ);
2021 static void idetape_create_read_position_cmd(idetape_pc_t *pc)
2023 idetape_init_pc(pc);
2024 pc->c[0] = READ_POSITION;
2025 pc->request_transfer = 20;
2026 pc->callback = &idetape_read_position_callback;
2029 static int idetape_read_position(ide_drive_t *drive)
2031 idetape_tape_t *tape = drive->driver_data;
2035 debug_log(DBG_PROCS, "Enter %s\n", __func__);
2037 idetape_create_read_position_cmd(&pc);
2038 if (idetape_queue_pc_tail(drive, &pc))
2040 position = tape->first_frame;
2044 static void idetape_create_locate_cmd(ide_drive_t *drive, idetape_pc_t *pc,
2045 unsigned int block, u8 partition, int skip)
2047 idetape_init_pc(pc);
2048 pc->c[0] = POSITION_TO_ELEMENT;
2050 put_unaligned(cpu_to_be32(block), (unsigned int *) &pc->c[3]);
2051 pc->c[8] = partition;
2052 pc->flags |= PC_FLAG_WAIT_FOR_DSC;
2053 pc->callback = &idetape_pc_callback;
2056 static int idetape_create_prevent_cmd(ide_drive_t *drive, idetape_pc_t *pc,
2059 idetape_tape_t *tape = drive->driver_data;
2061 /* device supports locking according to capabilities page */
2062 if (!(tape->caps[6] & 0x01))
2065 idetape_init_pc(pc);
2066 pc->c[0] = ALLOW_MEDIUM_REMOVAL;
2068 pc->callback = &idetape_pc_callback;
2072 static int __idetape_discard_read_pipeline(ide_drive_t *drive)
2074 idetape_tape_t *tape = drive->driver_data;
2075 unsigned long flags;
2078 if (tape->chrdev_dir != IDETAPE_DIR_READ)
2081 /* Remove merge stage. */
2082 cnt = tape->merge_stage_size / tape->blk_size;
2083 if (test_and_clear_bit(IDETAPE_FLAG_FILEMARK, &tape->flags))
2084 ++cnt; /* Filemarks count as 1 sector */
2085 tape->merge_stage_size = 0;
2086 if (tape->merge_stage != NULL) {
2087 __idetape_kfree_stage(tape->merge_stage);
2088 tape->merge_stage = NULL;
2091 /* Clear pipeline flags. */
2092 clear_bit(IDETAPE_FLAG_PIPELINE_ERR, &tape->flags);
2093 tape->chrdev_dir = IDETAPE_DIR_NONE;
2095 /* Remove pipeline stages. */
2096 if (tape->first_stage == NULL)
2099 spin_lock_irqsave(&tape->lock, flags);
2100 tape->next_stage = NULL;
2101 if (idetape_pipeline_active(tape))
2102 idetape_wait_for_request(drive, tape->active_data_rq);
2103 spin_unlock_irqrestore(&tape->lock, flags);
2105 while (tape->first_stage != NULL) {
2106 struct request *rq_ptr = &tape->first_stage->rq;
2108 cnt += rq_ptr->nr_sectors - rq_ptr->current_nr_sectors;
2109 if (rq_ptr->errors == IDETAPE_ERROR_FILEMARK)
2111 idetape_remove_stage_head(drive);
2113 tape->nr_pending_stages = 0;
2114 tape->max_stages = tape->min_pipeline;
2119 * Position the tape to the requested block using the LOCATE packet command.
2120 * A READ POSITION command is then issued to check where we are positioned. Like
2121 * all higher level operations, we queue the commands at the tail of the request
2122 * queue and wait for their completion.
2124 static int idetape_position_tape(ide_drive_t *drive, unsigned int block,
2125 u8 partition, int skip)
2127 idetape_tape_t *tape = drive->driver_data;
2131 if (tape->chrdev_dir == IDETAPE_DIR_READ)
2132 __idetape_discard_read_pipeline(drive);
2133 idetape_wait_ready(drive, 60 * 5 * HZ);
2134 idetape_create_locate_cmd(drive, &pc, block, partition, skip);
2135 retval = idetape_queue_pc_tail(drive, &pc);
2139 idetape_create_read_position_cmd(&pc);
2140 return (idetape_queue_pc_tail(drive, &pc));
2143 static void idetape_discard_read_pipeline(ide_drive_t *drive,
2144 int restore_position)
2146 idetape_tape_t *tape = drive->driver_data;
2150 cnt = __idetape_discard_read_pipeline(drive);
2151 if (restore_position) {
2152 position = idetape_read_position(drive);
2153 seek = position > cnt ? position - cnt : 0;
2154 if (idetape_position_tape(drive, seek, 0, 0)) {
2155 printk(KERN_INFO "ide-tape: %s: position_tape failed in"
2156 " discard_pipeline()\n", tape->name);
2163 * Generate a read/write request for the block device interface and wait for it
2166 static int idetape_queue_rw_tail(ide_drive_t *drive, int cmd, int blocks,
2167 struct idetape_bh *bh)
2169 idetape_tape_t *tape = drive->driver_data;
2172 debug_log(DBG_SENSE, "%s: cmd=%d\n", __func__, cmd);
2174 if (idetape_pipeline_active(tape)) {
2175 printk(KERN_ERR "ide-tape: bug: the pipeline is active in %s\n",
2180 idetape_init_rq(&rq, cmd);
2181 rq.rq_disk = tape->disk;
2182 rq.special = (void *)bh;
2183 rq.sector = tape->first_frame;
2184 rq.nr_sectors = blocks;
2185 rq.current_nr_sectors = blocks;
2186 (void) ide_do_drive_cmd(drive, &rq, ide_wait);
2188 if ((cmd & (REQ_IDETAPE_READ | REQ_IDETAPE_WRITE)) == 0)
2191 if (tape->merge_stage)
2192 idetape_init_merge_stage(tape);
2193 if (rq.errors == IDETAPE_ERROR_GENERAL)
2195 return (tape->blk_size * (blocks-rq.current_nr_sectors));
2198 /* start servicing the pipeline stages, starting from tape->next_stage. */
2199 static void idetape_plug_pipeline(ide_drive_t *drive)
2201 idetape_tape_t *tape = drive->driver_data;
2203 if (tape->next_stage == NULL)
2205 if (!idetape_pipeline_active(tape)) {
2206 set_bit(IDETAPE_FLAG_PIPELINE_ACTIVE, &tape->flags);
2207 idetape_activate_next_stage(drive);
2208 (void) ide_do_drive_cmd(drive, tape->active_data_rq, ide_end);
2212 static void idetape_create_inquiry_cmd(idetape_pc_t *pc)
2214 idetape_init_pc(pc);
2217 pc->request_transfer = 254;
2218 pc->callback = &idetape_pc_callback;
2221 static void idetape_create_rewind_cmd(ide_drive_t *drive, idetape_pc_t *pc)
2223 idetape_init_pc(pc);
2224 pc->c[0] = REZERO_UNIT;
2225 pc->flags |= PC_FLAG_WAIT_FOR_DSC;
2226 pc->callback = &idetape_pc_callback;
2229 static void idetape_create_erase_cmd(idetape_pc_t *pc)
2231 idetape_init_pc(pc);
2234 pc->flags |= PC_FLAG_WAIT_FOR_DSC;
2235 pc->callback = &idetape_pc_callback;
2238 static void idetape_create_space_cmd(idetape_pc_t *pc, int count, u8 cmd)
2240 idetape_init_pc(pc);
2242 put_unaligned(cpu_to_be32(count), (unsigned int *) &pc->c[1]);
2244 pc->flags |= PC_FLAG_WAIT_FOR_DSC;
2245 pc->callback = &idetape_pc_callback;
2248 static void idetape_wait_first_stage(ide_drive_t *drive)
2250 idetape_tape_t *tape = drive->driver_data;
2251 unsigned long flags;
2253 if (tape->first_stage == NULL)
2255 spin_lock_irqsave(&tape->lock, flags);
2256 if (tape->active_stage == tape->first_stage)
2257 idetape_wait_for_request(drive, tape->active_data_rq);
2258 spin_unlock_irqrestore(&tape->lock, flags);
2262 * Try to add a character device originated write request to our pipeline. In
2263 * case we don't succeed, we revert to non-pipelined operation mode for this
2264 * request. In order to accomplish that, we
2266 * 1. Try to allocate a new pipeline stage.
2267 * 2. If we can't, wait for more and more requests to be serviced and try again
2269 * 3. If we still can't allocate a stage, fallback to non-pipelined operation
2270 * mode for this request.
2272 static int idetape_add_chrdev_write_request(ide_drive_t *drive, int blocks)
2274 idetape_tape_t *tape = drive->driver_data;
2275 idetape_stage_t *new_stage;
2276 unsigned long flags;
2279 debug_log(DBG_CHRDEV, "Enter %s\n", __func__);
2281 /* Attempt to allocate a new stage. Beware possible race conditions. */
2282 while ((new_stage = idetape_kmalloc_stage(tape)) == NULL) {
2283 spin_lock_irqsave(&tape->lock, flags);
2284 if (idetape_pipeline_active(tape)) {
2285 idetape_wait_for_request(drive, tape->active_data_rq);
2286 spin_unlock_irqrestore(&tape->lock, flags);
2288 spin_unlock_irqrestore(&tape->lock, flags);
2289 idetape_plug_pipeline(drive);
2290 if (idetape_pipeline_active(tape))
2293 * The machine is short on memory. Fallback to non-
2294 * pipelined operation mode for this request.
2296 return idetape_queue_rw_tail(drive, REQ_IDETAPE_WRITE,
2297 blocks, tape->merge_stage->bh);
2300 rq = &new_stage->rq;
2301 idetape_init_rq(rq, REQ_IDETAPE_WRITE);
2302 /* Doesn't actually matter - We always assume sequential access */
2303 rq->sector = tape->first_frame;
2304 rq->current_nr_sectors = blocks;
2305 rq->nr_sectors = blocks;
2307 idetape_switch_buffers(tape, new_stage);
2308 idetape_add_stage_tail(drive, new_stage);
2309 tape->pipeline_head++;
2310 idetape_calculate_speeds(drive);
2313 * Estimate whether the tape has stopped writing by checking if our
2314 * write pipeline is currently empty. If we are not writing anymore,
2315 * wait for the pipeline to be almost completely full (90%) before
2316 * starting to service requests, so that we will be able to keep up with
2317 * the higher speeds of the tape.
2319 if (!idetape_pipeline_active(tape)) {
2320 if (tape->nr_stages >= tape->max_stages * 9 / 10 ||
2321 tape->nr_stages >= tape->max_stages -
2322 tape->uncontrolled_pipeline_head_speed * 3 * 1024 /
2324 tape->measure_insert_time = 1;
2325 tape->insert_time = jiffies;
2326 tape->insert_size = 0;
2327 tape->insert_speed = 0;
2328 idetape_plug_pipeline(drive);
2331 if (test_and_clear_bit(IDETAPE_FLAG_PIPELINE_ERR, &tape->flags))
2332 /* Return a deferred error */
2338 * Wait until all pending pipeline requests are serviced. Typically called on
2341 static void idetape_wait_for_pipeline(ide_drive_t *drive)
2343 idetape_tape_t *tape = drive->driver_data;
2344 unsigned long flags;
2346 while (tape->next_stage || idetape_pipeline_active(tape)) {
2347 idetape_plug_pipeline(drive);
2348 spin_lock_irqsave(&tape->lock, flags);
2349 if (idetape_pipeline_active(tape))
2350 idetape_wait_for_request(drive, tape->active_data_rq);
2351 spin_unlock_irqrestore(&tape->lock, flags);
2355 static void idetape_empty_write_pipeline(ide_drive_t *drive)
2357 idetape_tape_t *tape = drive->driver_data;
2359 struct idetape_bh *bh;
2361 if (tape->chrdev_dir != IDETAPE_DIR_WRITE) {
2362 printk(KERN_ERR "ide-tape: bug: Trying to empty write pipeline,"
2363 " but we are not writing.\n");
2366 if (tape->merge_stage_size > tape->stage_size) {
2367 printk(KERN_ERR "ide-tape: bug: merge_buffer too big\n");
2368 tape->merge_stage_size = tape->stage_size;
2370 if (tape->merge_stage_size) {
2371 blocks = tape->merge_stage_size / tape->blk_size;
2372 if (tape->merge_stage_size % tape->blk_size) {
2376 i = tape->blk_size - tape->merge_stage_size %
2378 bh = tape->bh->b_reqnext;
2380 atomic_set(&bh->b_count, 0);
2386 printk(KERN_INFO "ide-tape: bug,"
2390 min = min(i, (unsigned int)(bh->b_size -
2391 atomic_read(&bh->b_count)));
2392 memset(bh->b_data + atomic_read(&bh->b_count),
2394 atomic_add(min, &bh->b_count);
2399 (void) idetape_add_chrdev_write_request(drive, blocks);
2400 tape->merge_stage_size = 0;
2402 idetape_wait_for_pipeline(drive);
2403 if (tape->merge_stage != NULL) {
2404 __idetape_kfree_stage(tape->merge_stage);
2405 tape->merge_stage = NULL;
2407 clear_bit(IDETAPE_FLAG_PIPELINE_ERR, &tape->flags);
2408 tape->chrdev_dir = IDETAPE_DIR_NONE;
2411 * On the next backup, perform the feedback loop again. (I don't want to
2412 * keep sense information between backups, as some systems are
2413 * constantly on, and the system load can be totally different on the
2416 tape->max_stages = tape->min_pipeline;
2417 if (tape->first_stage != NULL ||
2418 tape->next_stage != NULL ||
2419 tape->last_stage != NULL ||
2420 tape->nr_stages != 0) {
2421 printk(KERN_ERR "ide-tape: ide-tape pipeline bug, "
2422 "first_stage %p, next_stage %p, "
2423 "last_stage %p, nr_stages %d\n",
2424 tape->first_stage, tape->next_stage,
2425 tape->last_stage, tape->nr_stages);
2429 static void idetape_restart_speed_control(ide_drive_t *drive)
2431 idetape_tape_t *tape = drive->driver_data;
2433 tape->restart_speed_control_req = 0;
2434 tape->pipeline_head = 0;
2435 tape->controlled_last_pipeline_head = 0;
2436 tape->controlled_previous_pipeline_head = 0;
2437 tape->uncontrolled_previous_pipeline_head = 0;
2438 tape->controlled_pipeline_head_speed = 5000;
2439 tape->pipeline_head_speed = 5000;
2440 tape->uncontrolled_pipeline_head_speed = 0;
2441 tape->controlled_pipeline_head_time =
2442 tape->uncontrolled_pipeline_head_time = jiffies;
2443 tape->controlled_previous_head_time =
2444 tape->uncontrolled_previous_head_time = jiffies;
2447 static int idetape_init_read(ide_drive_t *drive, int max_stages)
2449 idetape_tape_t *tape = drive->driver_data;
2450 idetape_stage_t *new_stage;
2453 u16 blocks = *(u16 *)&tape->caps[12];
2455 /* Initialize read operation */
2456 if (tape->chrdev_dir != IDETAPE_DIR_READ) {
2457 if (tape->chrdev_dir == IDETAPE_DIR_WRITE) {
2458 idetape_empty_write_pipeline(drive);
2459 idetape_flush_tape_buffers(drive);
2461 if (tape->merge_stage || tape->merge_stage_size) {
2462 printk(KERN_ERR "ide-tape: merge_stage_size should be"
2464 tape->merge_stage_size = 0;
2466 tape->merge_stage = __idetape_kmalloc_stage(tape, 0, 0);
2467 if (!tape->merge_stage)
2469 tape->chrdev_dir = IDETAPE_DIR_READ;
2472 * Issue a read 0 command to ensure that DSC handshake is
2473 * switched from completion mode to buffer available mode.
2474 * No point in issuing this if DSC overlap isn't supported, some
2475 * drives (Seagate STT3401A) will return an error.
2477 if (drive->dsc_overlap) {
2478 bytes_read = idetape_queue_rw_tail(drive,
2479 REQ_IDETAPE_READ, 0,
2480 tape->merge_stage->bh);
2481 if (bytes_read < 0) {
2482 __idetape_kfree_stage(tape->merge_stage);
2483 tape->merge_stage = NULL;
2484 tape->chrdev_dir = IDETAPE_DIR_NONE;
2489 if (tape->restart_speed_control_req)
2490 idetape_restart_speed_control(drive);
2491 idetape_init_rq(&rq, REQ_IDETAPE_READ);
2492 rq.sector = tape->first_frame;
2493 rq.nr_sectors = blocks;
2494 rq.current_nr_sectors = blocks;
2495 if (!test_bit(IDETAPE_FLAG_PIPELINE_ERR, &tape->flags) &&
2496 tape->nr_stages < max_stages) {
2497 new_stage = idetape_kmalloc_stage(tape);
2498 while (new_stage != NULL) {
2500 idetape_add_stage_tail(drive, new_stage);
2501 if (tape->nr_stages >= max_stages)
2503 new_stage = idetape_kmalloc_stage(tape);
2506 if (!idetape_pipeline_active(tape)) {
2507 if (tape->nr_pending_stages >= 3 * max_stages / 4) {
2508 tape->measure_insert_time = 1;
2509 tape->insert_time = jiffies;
2510 tape->insert_size = 0;
2511 tape->insert_speed = 0;
2512 idetape_plug_pipeline(drive);
2519 * Called from idetape_chrdev_read() to service a character device read request
2520 * and add read-ahead requests to our pipeline.
2522 static int idetape_add_chrdev_read_request(ide_drive_t *drive, int blocks)
2524 idetape_tape_t *tape = drive->driver_data;
2525 unsigned long flags;
2526 struct request *rq_ptr;
2529 debug_log(DBG_PROCS, "Enter %s, %d blocks\n", __func__, blocks);
2531 /* If we are at a filemark, return a read length of 0 */
2532 if (test_bit(IDETAPE_FLAG_FILEMARK, &tape->flags))
2535 /* Wait for the next block to reach the head of the pipeline. */
2536 idetape_init_read(drive, tape->max_stages);
2537 if (tape->first_stage == NULL) {
2538 if (test_bit(IDETAPE_FLAG_PIPELINE_ERR, &tape->flags))
2540 return idetape_queue_rw_tail(drive, REQ_IDETAPE_READ, blocks,
2541 tape->merge_stage->bh);
2543 idetape_wait_first_stage(drive);
2544 rq_ptr = &tape->first_stage->rq;
2545 bytes_read = tape->blk_size * (rq_ptr->nr_sectors -
2546 rq_ptr->current_nr_sectors);
2547 rq_ptr->nr_sectors = 0;
2548 rq_ptr->current_nr_sectors = 0;
2550 if (rq_ptr->errors == IDETAPE_ERROR_EOD)
2553 idetape_switch_buffers(tape, tape->first_stage);
2554 if (rq_ptr->errors == IDETAPE_ERROR_FILEMARK)
2555 set_bit(IDETAPE_FLAG_FILEMARK, &tape->flags);
2556 spin_lock_irqsave(&tape->lock, flags);
2557 idetape_remove_stage_head(drive);
2558 spin_unlock_irqrestore(&tape->lock, flags);
2559 tape->pipeline_head++;
2560 idetape_calculate_speeds(drive);
2562 if (bytes_read > blocks * tape->blk_size) {
2563 printk(KERN_ERR "ide-tape: bug: trying to return more bytes"
2564 " than requested\n");
2565 bytes_read = blocks * tape->blk_size;
2567 return (bytes_read);
2570 static void idetape_pad_zeros(ide_drive_t *drive, int bcount)
2572 idetape_tape_t *tape = drive->driver_data;
2573 struct idetape_bh *bh;
2579 bh = tape->merge_stage->bh;
2580 count = min(tape->stage_size, bcount);
2582 blocks = count / tape->blk_size;
2584 atomic_set(&bh->b_count,
2585 min(count, (unsigned int)bh->b_size));
2586 memset(bh->b_data, 0, atomic_read(&bh->b_count));
2587 count -= atomic_read(&bh->b_count);
2590 idetape_queue_rw_tail(drive, REQ_IDETAPE_WRITE, blocks,
2591 tape->merge_stage->bh);
2595 static int idetape_pipeline_size(ide_drive_t *drive)
2597 idetape_tape_t *tape = drive->driver_data;
2598 idetape_stage_t *stage;
2602 idetape_wait_for_pipeline(drive);
2603 stage = tape->first_stage;
2604 while (stage != NULL) {
2606 size += tape->blk_size * (rq->nr_sectors -
2607 rq->current_nr_sectors);
2608 if (rq->errors == IDETAPE_ERROR_FILEMARK)
2609 size += tape->blk_size;
2610 stage = stage->next;
2612 size += tape->merge_stage_size;
2617 * Rewinds the tape to the Beginning Of the current Partition (BOP). We
2618 * currently support only one partition.
2620 static int idetape_rewind_tape(ide_drive_t *drive)
2624 idetape_tape_t *tape;
2625 tape = drive->driver_data;
2627 debug_log(DBG_SENSE, "Enter %s\n", __func__);
2629 idetape_create_rewind_cmd(drive, &pc);
2630 retval = idetape_queue_pc_tail(drive, &pc);
2634 idetape_create_read_position_cmd(&pc);
2635 retval = idetape_queue_pc_tail(drive, &pc);
2641 /* mtio.h compatible commands should be issued to the chrdev interface. */
2642 static int idetape_blkdev_ioctl(ide_drive_t *drive, unsigned int cmd,
2645 idetape_tape_t *tape = drive->driver_data;
2646 void __user *argp = (void __user *)arg;
2648 struct idetape_config {
2649 int dsc_rw_frequency;
2650 int dsc_media_access_frequency;
2654 debug_log(DBG_PROCS, "Enter %s\n", __func__);
2658 if (copy_from_user(&config, argp, sizeof(config)))
2660 tape->best_dsc_rw_freq = config.dsc_rw_frequency;
2661 tape->max_stages = config.nr_stages;
2664 config.dsc_rw_frequency = (int) tape->best_dsc_rw_freq;
2665 config.nr_stages = tape->max_stages;
2666 if (copy_to_user(argp, &config, sizeof(config)))
2676 * The function below is now a bit more complicated than just passing the
2677 * command to the tape since we may have crossed some filemarks during our
2678 * pipelined read-ahead mode. As a minor side effect, the pipeline enables us to
2679 * support MTFSFM when the filemark is in our internal pipeline even if the tape
2680 * doesn't support spacing over filemarks in the reverse direction.
2682 static int idetape_space_over_filemarks(ide_drive_t *drive, short mt_op,
2685 idetape_tape_t *tape = drive->driver_data;
2687 unsigned long flags;
2688 int retval, count = 0;
2689 int sprev = !!(tape->caps[4] & 0x20);
2693 if (MTBSF == mt_op || MTBSFM == mt_op) {
2696 mt_count = -mt_count;
2699 if (tape->chrdev_dir == IDETAPE_DIR_READ) {
2700 /* its a read-ahead buffer, scan it for crossed filemarks. */
2701 tape->merge_stage_size = 0;
2702 if (test_and_clear_bit(IDETAPE_FLAG_FILEMARK, &tape->flags))
2704 while (tape->first_stage != NULL) {
2705 if (count == mt_count) {
2706 if (mt_op == MTFSFM)
2707 set_bit(IDETAPE_FLAG_FILEMARK,
2711 spin_lock_irqsave(&tape->lock, flags);
2712 if (tape->first_stage == tape->active_stage) {
2714 * We have reached the active stage in the read
2715 * pipeline. There is no point in allowing the
2716 * drive to continue reading any farther, so we
2717 * stop the pipeline.
2719 * This section should be moved to a separate
2720 * subroutine because similar operations are
2721 * done in __idetape_discard_read_pipeline(),
2724 tape->next_stage = NULL;
2725 spin_unlock_irqrestore(&tape->lock, flags);
2726 idetape_wait_first_stage(drive);
2727 tape->next_stage = tape->first_stage->next;
2729 spin_unlock_irqrestore(&tape->lock, flags);
2730 if (tape->first_stage->rq.errors ==
2731 IDETAPE_ERROR_FILEMARK)
2733 idetape_remove_stage_head(drive);
2735 idetape_discard_read_pipeline(drive, 0);
2739 * The filemark was not found in our internal pipeline; now we can issue
2740 * the space command.
2745 idetape_create_space_cmd(&pc, mt_count - count,
2746 IDETAPE_SPACE_OVER_FILEMARK);
2747 return idetape_queue_pc_tail(drive, &pc);
2752 retval = idetape_space_over_filemarks(drive, MTFSF,
2756 count = (MTBSFM == mt_op ? 1 : -1);
2757 return idetape_space_over_filemarks(drive, MTFSF, count);
2759 printk(KERN_ERR "ide-tape: MTIO operation %d not supported\n",
2766 * Our character device read / write functions.
2768 * The tape is optimized to maximize throughput when it is transferring an
2769 * integral number of the "continuous transfer limit", which is a parameter of
2770 * the specific tape (26kB on my particular tape, 32kB for Onstream).
2772 * As of version 1.3 of the driver, the character device provides an abstract
2773 * continuous view of the media - any mix of block sizes (even 1 byte) on the
2774 * same backup/restore procedure is supported. The driver will internally
2775 * convert the requests to the recommended transfer unit, so that an unmatch
2776 * between the user's block size to the recommended size will only result in a
2777 * (slightly) increased driver overhead, but will no longer hit performance.
2778 * This is not applicable to Onstream.
2780 static ssize_t idetape_chrdev_read(struct file *file, char __user *buf,
2781 size_t count, loff_t *ppos)
2783 struct ide_tape_obj *tape = ide_tape_f(file);
2784 ide_drive_t *drive = tape->drive;
2785 ssize_t bytes_read, temp, actually_read = 0, rc;
2787 u16 ctl = *(u16 *)&tape->caps[12];
2789 debug_log(DBG_CHRDEV, "Enter %s, count %Zd\n", __func__, count);
2791 if (tape->chrdev_dir != IDETAPE_DIR_READ) {
2792 if (test_bit(IDETAPE_FLAG_DETECT_BS, &tape->flags))
2793 if (count > tape->blk_size &&
2794 (count % tape->blk_size) == 0)
2795 tape->user_bs_factor = count / tape->blk_size;
2797 rc = idetape_init_read(drive, tape->max_stages);
2802 if (tape->merge_stage_size) {
2803 actually_read = min((unsigned int)(tape->merge_stage_size),
2804 (unsigned int)count);
2805 if (idetape_copy_stage_to_user(tape, buf, tape->merge_stage,
2808 buf += actually_read;
2809 tape->merge_stage_size -= actually_read;
2810 count -= actually_read;
2812 while (count >= tape->stage_size) {
2813 bytes_read = idetape_add_chrdev_read_request(drive, ctl);
2814 if (bytes_read <= 0)
2816 if (idetape_copy_stage_to_user(tape, buf, tape->merge_stage,
2820 count -= bytes_read;
2821 actually_read += bytes_read;
2824 bytes_read = idetape_add_chrdev_read_request(drive, ctl);
2825 if (bytes_read <= 0)
2827 temp = min((unsigned long)count, (unsigned long)bytes_read);
2828 if (idetape_copy_stage_to_user(tape, buf, tape->merge_stage,
2831 actually_read += temp;
2832 tape->merge_stage_size = bytes_read-temp;
2835 if (!actually_read && test_bit(IDETAPE_FLAG_FILEMARK, &tape->flags)) {
2836 debug_log(DBG_SENSE, "%s: spacing over filemark\n", tape->name);
2838 idetape_space_over_filemarks(drive, MTFSF, 1);
2842 return ret ? ret : actually_read;
2845 static ssize_t idetape_chrdev_write(struct file *file, const char __user *buf,
2846 size_t count, loff_t *ppos)
2848 struct ide_tape_obj *tape = ide_tape_f(file);
2849 ide_drive_t *drive = tape->drive;
2850 ssize_t actually_written = 0;
2852 u16 ctl = *(u16 *)&tape->caps[12];
2854 /* The drive is write protected. */
2855 if (tape->write_prot)
2858 debug_log(DBG_CHRDEV, "Enter %s, count %Zd\n", __func__, count);
2860 /* Initialize write operation */
2861 if (tape->chrdev_dir != IDETAPE_DIR_WRITE) {
2862 if (tape->chrdev_dir == IDETAPE_DIR_READ)
2863 idetape_discard_read_pipeline(drive, 1);
2864 if (tape->merge_stage || tape->merge_stage_size) {
2865 printk(KERN_ERR "ide-tape: merge_stage_size "
2866 "should be 0 now\n");
2867 tape->merge_stage_size = 0;
2869 tape->merge_stage = __idetape_kmalloc_stage(tape, 0, 0);
2870 if (!tape->merge_stage)
2872 tape->chrdev_dir = IDETAPE_DIR_WRITE;
2873 idetape_init_merge_stage(tape);
2876 * Issue a write 0 command to ensure that DSC handshake is
2877 * switched from completion mode to buffer available mode. No
2878 * point in issuing this if DSC overlap isn't supported, some
2879 * drives (Seagate STT3401A) will return an error.
2881 if (drive->dsc_overlap) {
2882 ssize_t retval = idetape_queue_rw_tail(drive,
2883 REQ_IDETAPE_WRITE, 0,
2884 tape->merge_stage->bh);
2886 __idetape_kfree_stage(tape->merge_stage);
2887 tape->merge_stage = NULL;
2888 tape->chrdev_dir = IDETAPE_DIR_NONE;
2895 if (tape->restart_speed_control_req)
2896 idetape_restart_speed_control(drive);
2897 if (tape->merge_stage_size) {
2898 if (tape->merge_stage_size >= tape->stage_size) {
2899 printk(KERN_ERR "ide-tape: bug: merge buf too big\n");
2900 tape->merge_stage_size = 0;
2902 actually_written = min((unsigned int)
2903 (tape->stage_size - tape->merge_stage_size),
2904 (unsigned int)count);
2905 if (idetape_copy_stage_from_user(tape, tape->merge_stage, buf,
2908 buf += actually_written;
2909 tape->merge_stage_size += actually_written;
2910 count -= actually_written;
2912 if (tape->merge_stage_size == tape->stage_size) {
2914 tape->merge_stage_size = 0;
2915 retval = idetape_add_chrdev_write_request(drive, ctl);
2920 while (count >= tape->stage_size) {
2922 if (idetape_copy_stage_from_user(tape, tape->merge_stage, buf,
2925 buf += tape->stage_size;
2926 count -= tape->stage_size;
2927 retval = idetape_add_chrdev_write_request(drive, ctl);
2928 actually_written += tape->stage_size;
2933 actually_written += count;
2934 if (idetape_copy_stage_from_user(tape, tape->merge_stage, buf,
2937 tape->merge_stage_size += count;
2939 return ret ? ret : actually_written;
2942 static int idetape_write_filemark(ide_drive_t *drive)
2946 /* Write a filemark */
2947 idetape_create_write_filemark_cmd(drive, &pc, 1);
2948 if (idetape_queue_pc_tail(drive, &pc)) {
2949 printk(KERN_ERR "ide-tape: Couldn't write a filemark\n");
2956 * Called from idetape_chrdev_ioctl when the general mtio MTIOCTOP ioctl is
2959 * Note: MTBSF and MTBSFM are not supported when the tape doesn't support
2960 * spacing over filemarks in the reverse direction. In this case, MTFSFM is also
2961 * usually not supported (it is supported in the rare case in which we crossed
2962 * the filemark during our read-ahead pipelined operation mode).
2964 * The following commands are currently not supported:
2966 * MTFSS, MTBSS, MTWSM, MTSETDENSITY, MTSETDRVBUFFER, MT_ST_BOOLEANS,
2967 * MT_ST_WRITE_THRESHOLD.
2969 static int idetape_mtioctop(ide_drive_t *drive, short mt_op, int mt_count)
2971 idetape_tape_t *tape = drive->driver_data;
2975 debug_log(DBG_ERR, "Handling MTIOCTOP ioctl: mt_op=%d, mt_count=%d\n",
2978 /* Commands which need our pipelined read-ahead stages. */
2986 return idetape_space_over_filemarks(drive, mt_op, mt_count);
2993 if (tape->write_prot)
2995 idetape_discard_read_pipeline(drive, 1);
2996 for (i = 0; i < mt_count; i++) {
2997 retval = idetape_write_filemark(drive);
3003 idetape_discard_read_pipeline(drive, 0);
3004 if (idetape_rewind_tape(drive))
3008 idetape_discard_read_pipeline(drive, 0);
3009 idetape_create_load_unload_cmd(drive, &pc,
3010 IDETAPE_LU_LOAD_MASK);
3011 return idetape_queue_pc_tail(drive, &pc);
3015 * If door is locked, attempt to unlock before
3016 * attempting to eject.
3018 if (tape->door_locked) {
3019 if (idetape_create_prevent_cmd(drive, &pc, 0))
3020 if (!idetape_queue_pc_tail(drive, &pc))
3021 tape->door_locked = DOOR_UNLOCKED;
3023 idetape_discard_read_pipeline(drive, 0);
3024 idetape_create_load_unload_cmd(drive, &pc,
3025 !IDETAPE_LU_LOAD_MASK);
3026 retval = idetape_queue_pc_tail(drive, &pc);
3028 clear_bit(IDETAPE_FLAG_MEDIUM_PRESENT, &tape->flags);
3031 idetape_discard_read_pipeline(drive, 0);
3032 return idetape_flush_tape_buffers(drive);
3034 idetape_discard_read_pipeline(drive, 0);
3035 idetape_create_load_unload_cmd(drive, &pc,
3036 IDETAPE_LU_RETENSION_MASK | IDETAPE_LU_LOAD_MASK);
3037 return idetape_queue_pc_tail(drive, &pc);
3039 idetape_create_space_cmd(&pc, 0, IDETAPE_SPACE_TO_EOD);
3040 return idetape_queue_pc_tail(drive, &pc);
3042 (void)idetape_rewind_tape(drive);
3043 idetape_create_erase_cmd(&pc);
3044 return idetape_queue_pc_tail(drive, &pc);
3047 if (mt_count < tape->blk_size ||
3048 mt_count % tape->blk_size)
3050 tape->user_bs_factor = mt_count / tape->blk_size;
3051 clear_bit(IDETAPE_FLAG_DETECT_BS, &tape->flags);
3053 set_bit(IDETAPE_FLAG_DETECT_BS, &tape->flags);
3056 idetape_discard_read_pipeline(drive, 0);
3057 return idetape_position_tape(drive,
3058 mt_count * tape->user_bs_factor, tape->partition, 0);
3060 idetape_discard_read_pipeline(drive, 0);
3061 return idetape_position_tape(drive, 0, mt_count, 0);
3065 if (!idetape_create_prevent_cmd(drive, &pc, 1))
3067 retval = idetape_queue_pc_tail(drive, &pc);
3070 tape->door_locked = DOOR_EXPLICITLY_LOCKED;
3073 if (!idetape_create_prevent_cmd(drive, &pc, 0))
3075 retval = idetape_queue_pc_tail(drive, &pc);
3078 tape->door_locked = DOOR_UNLOCKED;
3081 printk(KERN_ERR "ide-tape: MTIO operation %d not supported\n",
3088 * Our character device ioctls. General mtio.h magnetic io commands are
3089 * supported here, and not in the corresponding block interface. Our own
3090 * ide-tape ioctls are supported on both interfaces.
3092 static int idetape_chrdev_ioctl(struct inode *inode, struct file *file,
3093 unsigned int cmd, unsigned long arg)
3095 struct ide_tape_obj *tape = ide_tape_f(file);
3096 ide_drive_t *drive = tape->drive;
3100 int block_offset = 0, position = tape->first_frame;
3101 void __user *argp = (void __user *)arg;
3103 debug_log(DBG_CHRDEV, "Enter %s, cmd=%u\n", __func__, cmd);
3105 tape->restart_speed_control_req = 1;
3106 if (tape->chrdev_dir == IDETAPE_DIR_WRITE) {
3107 idetape_empty_write_pipeline(drive);
3108 idetape_flush_tape_buffers(drive);
3110 if (cmd == MTIOCGET || cmd == MTIOCPOS) {
3111 block_offset = idetape_pipeline_size(drive) /
3112 (tape->blk_size * tape->user_bs_factor);
3113 position = idetape_read_position(drive);
3119 if (copy_from_user(&mtop, argp, sizeof(struct mtop)))
3121 return idetape_mtioctop(drive, mtop.mt_op, mtop.mt_count);
3123 memset(&mtget, 0, sizeof(struct mtget));
3124 mtget.mt_type = MT_ISSCSI2;
3125 mtget.mt_blkno = position / tape->user_bs_factor - block_offset;
3127 ((tape->blk_size * tape->user_bs_factor)
3128 << MT_ST_BLKSIZE_SHIFT) & MT_ST_BLKSIZE_MASK;
3130 if (tape->drv_write_prot)
3131 mtget.mt_gstat |= GMT_WR_PROT(0xffffffff);
3133 if (copy_to_user(argp, &mtget, sizeof(struct mtget)))
3137 mtpos.mt_blkno = position / tape->user_bs_factor - block_offset;
3138 if (copy_to_user(argp, &mtpos, sizeof(struct mtpos)))
3142 if (tape->chrdev_dir == IDETAPE_DIR_READ)
3143 idetape_discard_read_pipeline(drive, 1);
3144 return idetape_blkdev_ioctl(drive, cmd, arg);
3149 * Do a mode sense page 0 with block descriptor and if it succeeds set the tape
3150 * block size with the reported value.
3152 static void ide_tape_get_bsize_from_bdesc(ide_drive_t *drive)
3154 idetape_tape_t *tape = drive->driver_data;
3157 idetape_create_mode_sense_cmd(&pc, IDETAPE_BLOCK_DESCRIPTOR);
3158 if (idetape_queue_pc_tail(drive, &pc)) {
3159 printk(KERN_ERR "ide-tape: Can't get block descriptor\n");
3160 if (tape->blk_size == 0) {
3161 printk(KERN_WARNING "ide-tape: Cannot deal with zero "
3162 "block size, assuming 32k\n");
3163 tape->blk_size = 32768;
3167 tape->blk_size = (pc.buffer[4 + 5] << 16) +
3168 (pc.buffer[4 + 6] << 8) +
3170 tape->drv_write_prot = (pc.buffer[2] & 0x80) >> 7;
3173 static int idetape_chrdev_open(struct inode *inode, struct file *filp)
3175 unsigned int minor = iminor(inode), i = minor & ~0xc0;
3177 idetape_tape_t *tape;
3181 if (i >= MAX_HWIFS * MAX_DRIVES)
3184 tape = ide_tape_chrdev_get(i);
3188 debug_log(DBG_CHRDEV, "Enter %s\n", __func__);
3191 * We really want to do nonseekable_open(inode, filp); here, but some
3192 * versions of tar incorrectly call lseek on tapes and bail out if that
3193 * fails. So we disallow pread() and pwrite(), but permit lseeks.
3195 filp->f_mode &= ~(FMODE_PREAD | FMODE_PWRITE);
3197 drive = tape->drive;
3199 filp->private_data = tape;
3201 if (test_and_set_bit(IDETAPE_FLAG_BUSY, &tape->flags)) {
3206 retval = idetape_wait_ready(drive, 60 * HZ);
3208 clear_bit(IDETAPE_FLAG_BUSY, &tape->flags);
3209 printk(KERN_ERR "ide-tape: %s: drive not ready\n", tape->name);
3213 idetape_read_position(drive);
3214 if (!test_bit(IDETAPE_FLAG_ADDRESS_VALID, &tape->flags))
3215 (void)idetape_rewind_tape(drive);
3217 if (tape->chrdev_dir != IDETAPE_DIR_READ)
3218 clear_bit(IDETAPE_FLAG_PIPELINE_ERR, &tape->flags);
3220 /* Read block size and write protect status from drive. */
3221 ide_tape_get_bsize_from_bdesc(drive);
3223 /* Set write protect flag if device is opened as read-only. */
3224 if ((filp->f_flags & O_ACCMODE) == O_RDONLY)
3225 tape->write_prot = 1;
3227 tape->write_prot = tape->drv_write_prot;
3229 /* Make sure drive isn't write protected if user wants to write. */
3230 if (tape->write_prot) {
3231 if ((filp->f_flags & O_ACCMODE) == O_WRONLY ||
3232 (filp->f_flags & O_ACCMODE) == O_RDWR) {
3233 clear_bit(IDETAPE_FLAG_BUSY, &tape->flags);
3239 /* Lock the tape drive door so user can't eject. */
3240 if (tape->chrdev_dir == IDETAPE_DIR_NONE) {
3241 if (idetape_create_prevent_cmd(drive, &pc, 1)) {
3242 if (!idetape_queue_pc_tail(drive, &pc)) {
3243 if (tape->door_locked != DOOR_EXPLICITLY_LOCKED)
3244 tape->door_locked = DOOR_LOCKED;
3248 idetape_restart_speed_control(drive);
3249 tape->restart_speed_control_req = 0;
3257 static void idetape_write_release(ide_drive_t *drive, unsigned int minor)
3259 idetape_tape_t *tape = drive->driver_data;
3261 idetape_empty_write_pipeline(drive);
3262 tape->merge_stage = __idetape_kmalloc_stage(tape, 1, 0);
3263 if (tape->merge_stage != NULL) {
3264 idetape_pad_zeros(drive, tape->blk_size *
3265 (tape->user_bs_factor - 1));
3266 __idetape_kfree_stage(tape->merge_stage);
3267 tape->merge_stage = NULL;
3269 idetape_write_filemark(drive);
3270 idetape_flush_tape_buffers(drive);
3271 idetape_flush_tape_buffers(drive);
3274 static int idetape_chrdev_release(struct inode *inode, struct file *filp)
3276 struct ide_tape_obj *tape = ide_tape_f(filp);
3277 ide_drive_t *drive = tape->drive;
3279 unsigned int minor = iminor(inode);
3282 tape = drive->driver_data;
3284 debug_log(DBG_CHRDEV, "Enter %s\n", __func__);
3286 if (tape->chrdev_dir == IDETAPE_DIR_WRITE)
3287 idetape_write_release(drive, minor);
3288 if (tape->chrdev_dir == IDETAPE_DIR_READ) {
3290 idetape_discard_read_pipeline(drive, 1);
3292 idetape_wait_for_pipeline(drive);
3294 if (tape->cache_stage != NULL) {
3295 __idetape_kfree_stage(tape->cache_stage);
3296 tape->cache_stage = NULL;
3298 if (minor < 128 && test_bit(IDETAPE_FLAG_MEDIUM_PRESENT, &tape->flags))
3299 (void) idetape_rewind_tape(drive);
3300 if (tape->chrdev_dir == IDETAPE_DIR_NONE) {
3301 if (tape->door_locked == DOOR_LOCKED) {
3302 if (idetape_create_prevent_cmd(drive, &pc, 0)) {
3303 if (!idetape_queue_pc_tail(drive, &pc))
3304 tape->door_locked = DOOR_UNLOCKED;
3308 clear_bit(IDETAPE_FLAG_BUSY, &tape->flags);
3315 * check the contents of the ATAPI IDENTIFY command results. We return:
3317 * 1 - If the tape can be supported by us, based on the information we have so
3320 * 0 - If this tape driver is not currently supported by us.
3322 static int idetape_identify_device(ide_drive_t *drive)
3324 u8 gcw[2], protocol, device_type, removable, packet_size;
3326 if (drive->id_read == 0)
3329 *((unsigned short *) &gcw) = drive->id->config;
3331 protocol = (gcw[1] & 0xC0) >> 6;
3332 device_type = gcw[1] & 0x1F;
3333 removable = !!(gcw[0] & 0x80);
3334 packet_size = gcw[0] & 0x3;
3336 /* Check that we can support this device */
3338 printk(KERN_ERR "ide-tape: Protocol (0x%02x) is not ATAPI\n",
3340 else if (device_type != 1)
3341 printk(KERN_ERR "ide-tape: Device type (0x%02x) is not set "
3342 "to tape\n", device_type);
3343 else if (!removable)
3344 printk(KERN_ERR "ide-tape: The removable flag is not set\n");
3345 else if (packet_size != 0) {
3346 printk(KERN_ERR "ide-tape: Packet size (0x%02x) is not 12"
3347 " bytes\n", packet_size);
3353 static void idetape_get_inquiry_results(ide_drive_t *drive)
3355 idetape_tape_t *tape = drive->driver_data;
3357 char fw_rev[6], vendor_id[10], product_id[18];
3359 idetape_create_inquiry_cmd(&pc);
3360 if (idetape_queue_pc_tail(drive, &pc)) {
3361 printk(KERN_ERR "ide-tape: %s: can't get INQUIRY results\n",
3365 memcpy(vendor_id, &pc.buffer[8], 8);
3366 memcpy(product_id, &pc.buffer[16], 16);
3367 memcpy(fw_rev, &pc.buffer[32], 4);
3369 ide_fixstring(vendor_id, 10, 0);
3370 ide_fixstring(product_id, 18, 0);
3371 ide_fixstring(fw_rev, 6, 0);
3373 printk(KERN_INFO "ide-tape: %s <-> %s: %s %s rev %s\n",
3374 drive->name, tape->name, vendor_id, product_id, fw_rev);
3378 * Ask the tape about its various parameters. In particular, we will adjust our
3379 * data transfer buffer size to the recommended value as returned by the tape.
3381 static void idetape_get_mode_sense_results(ide_drive_t *drive)
3383 idetape_tape_t *tape = drive->driver_data;
3386 u8 speed, max_speed;
3388 idetape_create_mode_sense_cmd(&pc, IDETAPE_CAPABILITIES_PAGE);
3389 if (idetape_queue_pc_tail(drive, &pc)) {
3390 printk(KERN_ERR "ide-tape: Can't get tape parameters - assuming"
3391 " some default values\n");
3392 tape->blk_size = 512;
3393 put_unaligned(52, (u16 *)&tape->caps[12]);
3394 put_unaligned(540, (u16 *)&tape->caps[14]);
3395 put_unaligned(6*52, (u16 *)&tape->caps[16]);
3398 caps = pc.buffer + 4 + pc.buffer[3];
3400 /* convert to host order and save for later use */
3401 speed = be16_to_cpu(*(u16 *)&caps[14]);
3402 max_speed = be16_to_cpu(*(u16 *)&caps[8]);
3404 put_unaligned(max_speed, (u16 *)&caps[8]);
3405 put_unaligned(be16_to_cpu(*(u16 *)&caps[12]), (u16 *)&caps[12]);
3406 put_unaligned(speed, (u16 *)&caps[14]);
3407 put_unaligned(be16_to_cpu(*(u16 *)&caps[16]), (u16 *)&caps[16]);
3410 printk(KERN_INFO "ide-tape: %s: invalid tape speed "
3411 "(assuming 650KB/sec)\n", drive->name);
3412 put_unaligned(650, (u16 *)&caps[14]);
3415 printk(KERN_INFO "ide-tape: %s: invalid max_speed "
3416 "(assuming 650KB/sec)\n", drive->name);
3417 put_unaligned(650, (u16 *)&caps[8]);
3420 memcpy(&tape->caps, caps, 20);
3422 tape->blk_size = 512;
3423 else if (caps[7] & 0x04)
3424 tape->blk_size = 1024;
3427 #ifdef CONFIG_IDE_PROC_FS
3428 static void idetape_add_settings(ide_drive_t *drive)
3430 idetape_tape_t *tape = drive->driver_data;
3432 ide_add_setting(drive, "buffer", SETTING_READ, TYPE_SHORT, 0, 0xffff,
3433 1, 2, (u16 *)&tape->caps[16], NULL);
3434 ide_add_setting(drive, "pipeline_min", SETTING_RW, TYPE_INT, 1, 0xffff,
3435 tape->stage_size / 1024, 1, &tape->min_pipeline, NULL);
3436 ide_add_setting(drive, "pipeline", SETTING_RW, TYPE_INT, 1, 0xffff,
3437 tape->stage_size / 1024, 1, &tape->max_stages, NULL);
3438 ide_add_setting(drive, "pipeline_max", SETTING_RW, TYPE_INT, 1, 0xffff,
3439 tape->stage_size / 1024, 1, &tape->max_pipeline, NULL);
3440 ide_add_setting(drive, "pipeline_used", SETTING_READ, TYPE_INT, 0,
3441 0xffff, tape->stage_size / 1024, 1, &tape->nr_stages,
3443 ide_add_setting(drive, "pipeline_pending", SETTING_READ, TYPE_INT, 0,
3444 0xffff, tape->stage_size / 1024, 1,
3445 &tape->nr_pending_stages, NULL);
3446 ide_add_setting(drive, "speed", SETTING_READ, TYPE_SHORT, 0, 0xffff,
3447 1, 1, (u16 *)&tape->caps[14], NULL);
3448 ide_add_setting(drive, "stage", SETTING_READ, TYPE_INT, 0, 0xffff, 1,
3449 1024, &tape->stage_size, NULL);
3450 ide_add_setting(drive, "tdsc", SETTING_RW, TYPE_INT, IDETAPE_DSC_RW_MIN,
3451 IDETAPE_DSC_RW_MAX, 1000, HZ, &tape->best_dsc_rw_freq,
3453 ide_add_setting(drive, "dsc_overlap", SETTING_RW, TYPE_BYTE, 0, 1, 1,
3454 1, &drive->dsc_overlap, NULL);
3455 ide_add_setting(drive, "pipeline_head_speed_c", SETTING_READ, TYPE_INT,
3456 0, 0xffff, 1, 1, &tape->controlled_pipeline_head_speed,
3458 ide_add_setting(drive, "pipeline_head_speed_u", SETTING_READ, TYPE_INT,
3460 &tape->uncontrolled_pipeline_head_speed, NULL);
3461 ide_add_setting(drive, "avg_speed", SETTING_READ, TYPE_INT, 0, 0xffff,
3462 1, 1, &tape->avg_speed, NULL);
3463 ide_add_setting(drive, "debug_mask", SETTING_RW, TYPE_INT, 0, 0xffff, 1,
3464 1, &tape->debug_mask, NULL);
3467 static inline void idetape_add_settings(ide_drive_t *drive) { ; }
3471 * The function below is called to:
3473 * 1. Initialize our various state variables.
3474 * 2. Ask the tape for its capabilities.
3475 * 3. Allocate a buffer which will be used for data transfer. The buffer size
3476 * is chosen based on the recommendation which we received in step 2.
3478 * Note that at this point ide.c already assigned us an irq, so that we can
3479 * queue requests here and wait for their completion.
3481 static void idetape_setup(ide_drive_t *drive, idetape_tape_t *tape, int minor)
3483 unsigned long t1, tmid, tn, t;
3488 u16 *ctl = (u16 *)&tape->caps[12];
3490 spin_lock_init(&tape->lock);
3491 drive->dsc_overlap = 1;
3492 if (drive->hwif->host_flags & IDE_HFLAG_NO_DSC) {
3493 printk(KERN_INFO "ide-tape: %s: disabling DSC overlap\n",
3495 drive->dsc_overlap = 0;
3497 /* Seagate Travan drives do not support DSC overlap. */
3498 if (strstr(drive->id->model, "Seagate STT3401"))
3499 drive->dsc_overlap = 0;
3500 tape->minor = minor;
3501 tape->name[0] = 'h';
3502 tape->name[1] = 't';
3503 tape->name[2] = '0' + minor;
3504 tape->chrdev_dir = IDETAPE_DIR_NONE;
3505 tape->pc = tape->pc_stack;
3506 tape->max_insert_speed = 10000;
3507 tape->speed_control = 1;
3508 *((unsigned short *) &gcw) = drive->id->config;
3510 /* Command packet DRQ type */
3511 if (((gcw[0] & 0x60) >> 5) == 1)
3512 set_bit(IDETAPE_FLAG_DRQ_INTERRUPT, &tape->flags);
3514 tape->min_pipeline = 10;
3515 tape->max_pipeline = 10;
3516 tape->max_stages = 10;
3518 idetape_get_inquiry_results(drive);
3519 idetape_get_mode_sense_results(drive);
3520 ide_tape_get_bsize_from_bdesc(drive);
3521 tape->user_bs_factor = 1;
3522 tape->stage_size = *ctl * tape->blk_size;
3523 while (tape->stage_size > 0xffff) {
3524 printk(KERN_NOTICE "ide-tape: decreasing stage size\n");
3526 tape->stage_size = *ctl * tape->blk_size;
3528 stage_size = tape->stage_size;
3529 tape->pages_per_stage = stage_size / PAGE_SIZE;
3530 if (stage_size % PAGE_SIZE) {
3531 tape->pages_per_stage++;
3532 tape->excess_bh_size = PAGE_SIZE - stage_size % PAGE_SIZE;
3535 /* Select the "best" DSC read/write polling freq and pipeline size. */
3536 speed = max(*(u16 *)&tape->caps[14], *(u16 *)&tape->caps[8]);
3538 tape->max_stages = speed * 1000 * 10 / tape->stage_size;
3540 /* Limit memory use for pipeline to 10% of physical memory */
3542 if (tape->max_stages * tape->stage_size >
3543 si.totalram * si.mem_unit / 10)
3545 si.totalram * si.mem_unit / (10 * tape->stage_size);
3547 tape->max_stages = min(tape->max_stages, IDETAPE_MAX_PIPELINE_STAGES);
3548 tape->min_pipeline = min(tape->max_stages, IDETAPE_MIN_PIPELINE_STAGES);
3549 tape->max_pipeline =
3550 min(tape->max_stages * 2, IDETAPE_MAX_PIPELINE_STAGES);
3551 if (tape->max_stages == 0) {
3552 tape->max_stages = 1;
3553 tape->min_pipeline = 1;
3554 tape->max_pipeline = 1;
3557 t1 = (tape->stage_size * HZ) / (speed * 1000);
3558 tmid = (*(u16 *)&tape->caps[16] * 32 * HZ) / (speed * 125);
3559 tn = (IDETAPE_FIFO_THRESHOLD * tape->stage_size * HZ) / (speed * 1000);
3561 if (tape->max_stages)
3567 * Ensure that the number we got makes sense; limit it within
3568 * IDETAPE_DSC_RW_MIN and IDETAPE_DSC_RW_MAX.
3570 tape->best_dsc_rw_freq = max_t(unsigned long,
3571 min_t(unsigned long, t, IDETAPE_DSC_RW_MAX),
3572 IDETAPE_DSC_RW_MIN);
3573 printk(KERN_INFO "ide-tape: %s <-> %s: %dKBps, %d*%dkB buffer, "
3574 "%dkB pipeline, %lums tDSC%s\n",
3575 drive->name, tape->name, *(u16 *)&tape->caps[14],
3576 (*(u16 *)&tape->caps[16] * 512) / tape->stage_size,
3577 tape->stage_size / 1024,
3578 tape->max_stages * tape->stage_size / 1024,
3579 tape->best_dsc_rw_freq * 1000 / HZ,
3580 drive->using_dma ? ", DMA":"");
3582 idetape_add_settings(drive);
3585 static void ide_tape_remove(ide_drive_t *drive)
3587 idetape_tape_t *tape = drive->driver_data;
3589 ide_proc_unregister_driver(drive, tape->driver);
3591 ide_unregister_region(tape->disk);
3596 static void ide_tape_release(struct kref *kref)
3598 struct ide_tape_obj *tape = to_ide_tape(kref);
3599 ide_drive_t *drive = tape->drive;
3600 struct gendisk *g = tape->disk;
3602 BUG_ON(tape->first_stage != NULL || tape->merge_stage_size);
3604 drive->dsc_overlap = 0;
3605 drive->driver_data = NULL;
3606 device_destroy(idetape_sysfs_class, MKDEV(IDETAPE_MAJOR, tape->minor));
3607 device_destroy(idetape_sysfs_class,
3608 MKDEV(IDETAPE_MAJOR, tape->minor + 128));
3609 idetape_devs[tape->minor] = NULL;
3610 g->private_data = NULL;
3615 #ifdef CONFIG_IDE_PROC_FS
3616 static int proc_idetape_read_name
3617 (char *page, char **start, off_t off, int count, int *eof, void *data)
3619 ide_drive_t *drive = (ide_drive_t *) data;
3620 idetape_tape_t *tape = drive->driver_data;
3624 len = sprintf(out, "%s\n", tape->name);
3625 PROC_IDE_READ_RETURN(page, start, off, count, eof, len);
3628 static ide_proc_entry_t idetape_proc[] = {
3629 { "capacity", S_IFREG|S_IRUGO, proc_ide_read_capacity, NULL },
3630 { "name", S_IFREG|S_IRUGO, proc_idetape_read_name, NULL },
3631 { NULL, 0, NULL, NULL }
3635 static int ide_tape_probe(ide_drive_t *);
3637 static ide_driver_t idetape_driver = {
3639 .owner = THIS_MODULE,
3641 .bus = &ide_bus_type,
3643 .probe = ide_tape_probe,
3644 .remove = ide_tape_remove,
3645 .version = IDETAPE_VERSION,
3647 .supports_dsc_overlap = 1,
3648 .do_request = idetape_do_request,
3649 .end_request = idetape_end_request,
3650 .error = __ide_error,
3651 .abort = __ide_abort,
3652 #ifdef CONFIG_IDE_PROC_FS
3653 .proc = idetape_proc,
3657 /* Our character device supporting functions, passed to register_chrdev. */
3658 static const struct file_operations idetape_fops = {
3659 .owner = THIS_MODULE,
3660 .read = idetape_chrdev_read,
3661 .write = idetape_chrdev_write,
3662 .ioctl = idetape_chrdev_ioctl,
3663 .open = idetape_chrdev_open,
3664 .release = idetape_chrdev_release,
3667 static int idetape_open(struct inode *inode, struct file *filp)
3669 struct gendisk *disk = inode->i_bdev->bd_disk;
3670 struct ide_tape_obj *tape;
3672 tape = ide_tape_get(disk);
3679 static int idetape_release(struct inode *inode, struct file *filp)
3681 struct gendisk *disk = inode->i_bdev->bd_disk;
3682 struct ide_tape_obj *tape = ide_tape_g(disk);
3689 static int idetape_ioctl(struct inode *inode, struct file *file,
3690 unsigned int cmd, unsigned long arg)
3692 struct block_device *bdev = inode->i_bdev;
3693 struct ide_tape_obj *tape = ide_tape_g(bdev->bd_disk);
3694 ide_drive_t *drive = tape->drive;
3695 int err = generic_ide_ioctl(drive, file, bdev, cmd, arg);
3697 err = idetape_blkdev_ioctl(drive, cmd, arg);
3701 static struct block_device_operations idetape_block_ops = {
3702 .owner = THIS_MODULE,
3703 .open = idetape_open,
3704 .release = idetape_release,
3705 .ioctl = idetape_ioctl,
3708 static int ide_tape_probe(ide_drive_t *drive)
3710 idetape_tape_t *tape;
3714 if (!strstr("ide-tape", drive->driver_req))
3716 if (!drive->present)
3718 if (drive->media != ide_tape)
3720 if (!idetape_identify_device(drive)) {
3721 printk(KERN_ERR "ide-tape: %s: not supported by this version of"
3722 " the driver\n", drive->name);
3726 printk(KERN_INFO "ide-tape: passing drive %s to ide-scsi"
3727 " emulation.\n", drive->name);
3730 tape = kzalloc(sizeof(idetape_tape_t), GFP_KERNEL);
3732 printk(KERN_ERR "ide-tape: %s: Can't allocate a tape struct\n",
3737 g = alloc_disk(1 << PARTN_BITS);
3741 ide_init_disk(g, drive);
3743 ide_proc_register_driver(drive, &idetape_driver);
3745 kref_init(&tape->kref);
3747 tape->drive = drive;
3748 tape->driver = &idetape_driver;
3751 g->private_data = &tape->driver;
3753 drive->driver_data = tape;
3755 mutex_lock(&idetape_ref_mutex);
3756 for (minor = 0; idetape_devs[minor]; minor++)
3758 idetape_devs[minor] = tape;
3759 mutex_unlock(&idetape_ref_mutex);
3761 idetape_setup(drive, tape, minor);
3763 device_create(idetape_sysfs_class, &drive->gendev,
3764 MKDEV(IDETAPE_MAJOR, minor), "%s", tape->name);
3765 device_create(idetape_sysfs_class, &drive->gendev,
3766 MKDEV(IDETAPE_MAJOR, minor + 128), "n%s", tape->name);
3768 g->fops = &idetape_block_ops;
3769 ide_register_region(g);
3779 static void __exit idetape_exit(void)
3781 driver_unregister(&idetape_driver.gen_driver);
3782 class_destroy(idetape_sysfs_class);
3783 unregister_chrdev(IDETAPE_MAJOR, "ht");
3786 static int __init idetape_init(void)
3789 idetape_sysfs_class = class_create(THIS_MODULE, "ide_tape");
3790 if (IS_ERR(idetape_sysfs_class)) {
3791 idetape_sysfs_class = NULL;
3792 printk(KERN_ERR "Unable to create sysfs class for ide tapes\n");
3797 if (register_chrdev(IDETAPE_MAJOR, "ht", &idetape_fops)) {
3798 printk(KERN_ERR "ide-tape: Failed to register chrdev"
3801 goto out_free_class;
3804 error = driver_register(&idetape_driver.gen_driver);
3806 goto out_free_driver;
3811 driver_unregister(&idetape_driver.gen_driver);
3813 class_destroy(idetape_sysfs_class);
3818 MODULE_ALIAS("ide:*m-tape*");
3819 module_init(idetape_init);
3820 module_exit(idetape_exit);
3821 MODULE_ALIAS_CHARDEV_MAJOR(IDETAPE_MAJOR);
3822 MODULE_DESCRIPTION("ATAPI Streaming TAPE Driver");
3823 MODULE_LICENSE("GPL");