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 #define IDETAPE_MIN_PIPELINE_STAGES 1
89 #define IDETAPE_MAX_PIPELINE_STAGES 400
90 #define IDETAPE_INCREASE_STAGES_RATE 20
93 * After each failed packet command we issue a request sense command and retry
94 * the packet command IDETAPE_MAX_PC_RETRIES times.
96 * Setting IDETAPE_MAX_PC_RETRIES to 0 will disable retries.
98 #define IDETAPE_MAX_PC_RETRIES 3
101 * With each packet command, we allocate a buffer of IDETAPE_PC_BUFFER_SIZE
102 * bytes. This is used for several packet commands (Not for READ/WRITE commands)
104 #define IDETAPE_PC_BUFFER_SIZE 256
107 * In various places in the driver, we need to allocate storage
108 * for packet commands and requests, which will remain valid while
109 * we leave the driver to wait for an interrupt or a timeout event.
111 #define IDETAPE_PC_STACK (10 + IDETAPE_MAX_PC_RETRIES)
114 * Some drives (for example, Seagate STT3401A Travan) require a very long
115 * timeout, because they don't return an interrupt or clear their busy bit
116 * until after the command completes (even retension commands).
118 #define IDETAPE_WAIT_CMD (900*HZ)
121 * The following parameter is used to select the point in the internal tape fifo
122 * in which we will start to refill the buffer. Decreasing the following
123 * parameter will improve the system's latency and interactive response, while
124 * using a high value might improve system throughput.
126 #define IDETAPE_FIFO_THRESHOLD 2
129 * DSC polling parameters.
131 * Polling for DSC (a single bit in the status register) is a very important
132 * function in ide-tape. There are two cases in which we poll for DSC:
134 * 1. Before a read/write packet command, to ensure that we can transfer data
135 * from/to the tape's data buffers, without causing an actual media access.
136 * In case the tape is not ready yet, we take out our request from the device
137 * request queue, so that ide.c could service requests from the other device
138 * on the same interface in the meantime.
140 * 2. After the successful initialization of a "media access packet command",
141 * which is a command that can take a long time to complete (the interval can
142 * range from several seconds to even an hour). Again, we postpone our request
143 * in the middle to free the bus for the other device. The polling frequency
144 * here should be lower than the read/write frequency since those media access
145 * commands are slow. We start from a "fast" frequency - IDETAPE_DSC_MA_FAST
146 * (1 second), and if we don't receive DSC after IDETAPE_DSC_MA_THRESHOLD
147 * (5 min), we switch it to a lower frequency - IDETAPE_DSC_MA_SLOW (1 min).
149 * We also set a timeout for the timer, in case something goes wrong. The
150 * timeout should be longer then the maximum execution time of a tape operation.
154 #define IDETAPE_DSC_RW_MIN 5*HZ/100 /* 50 msec */
155 #define IDETAPE_DSC_RW_MAX 40*HZ/100 /* 400 msec */
156 #define IDETAPE_DSC_RW_TIMEOUT 2*60*HZ /* 2 minutes */
157 #define IDETAPE_DSC_MA_FAST 2*HZ /* 2 seconds */
158 #define IDETAPE_DSC_MA_THRESHOLD 5*60*HZ /* 5 minutes */
159 #define IDETAPE_DSC_MA_SLOW 30*HZ /* 30 seconds */
160 #define IDETAPE_DSC_MA_TIMEOUT 2*60*60*HZ /* 2 hours */
162 /*************************** End of tunable parameters ***********************/
164 /* Read/Write error simulation */
165 #define SIMULATE_ERRORS 0
167 /* tape directions */
169 IDETAPE_DIR_NONE
= (1 << 0),
170 IDETAPE_DIR_READ
= (1 << 1),
171 IDETAPE_DIR_WRITE
= (1 << 2),
177 struct idetape_bh
*b_reqnext
;
181 /* Tape door status */
182 #define DOOR_UNLOCKED 0
183 #define DOOR_LOCKED 1
184 #define DOOR_EXPLICITLY_LOCKED 2
186 /* Some defines for the SPACE command */
187 #define IDETAPE_SPACE_OVER_FILEMARK 1
188 #define IDETAPE_SPACE_TO_EOD 3
190 /* Some defines for the LOAD UNLOAD command */
191 #define IDETAPE_LU_LOAD_MASK 1
192 #define IDETAPE_LU_RETENSION_MASK 2
193 #define IDETAPE_LU_EOT_MASK 4
196 * Special requests for our block device strategy routine.
198 * In order to service a character device command, we add special requests to
199 * the tail of our block device request queue and wait for their completion.
203 REQ_IDETAPE_PC1
= (1 << 0), /* packet command (first stage) */
204 REQ_IDETAPE_PC2
= (1 << 1), /* packet command (second stage) */
205 REQ_IDETAPE_READ
= (1 << 2),
206 REQ_IDETAPE_WRITE
= (1 << 3),
209 /* Error codes returned in rq->errors to the higher part of the driver. */
210 #define IDETAPE_ERROR_GENERAL 101
211 #define IDETAPE_ERROR_FILEMARK 102
212 #define IDETAPE_ERROR_EOD 103
214 /* Structures related to the SELECT SENSE / MODE SENSE packet commands. */
215 #define IDETAPE_BLOCK_DESCRIPTOR 0
216 #define IDETAPE_CAPABILITIES_PAGE 0x2a
218 /* Tape flag bits values. */
220 IDETAPE_FLAG_IGNORE_DSC
= (1 << 0),
221 /* 0 When the tape position is unknown */
222 IDETAPE_FLAG_ADDRESS_VALID
= (1 << 1),
223 /* Device already opened */
224 IDETAPE_FLAG_BUSY
= (1 << 2),
225 /* Attempt to auto-detect the current user block size */
226 IDETAPE_FLAG_DETECT_BS
= (1 << 3),
227 /* Currently on a filemark */
228 IDETAPE_FLAG_FILEMARK
= (1 << 4),
229 /* DRQ interrupt device */
230 IDETAPE_FLAG_DRQ_INTERRUPT
= (1 << 5),
231 /* 0 = no tape is loaded, so we don't rewind after ejecting */
232 IDETAPE_FLAG_MEDIUM_PRESENT
= (1 << 6),
235 /* A pipeline stage. */
236 typedef struct idetape_stage_s
{
237 struct request rq
; /* The corresponding request */
238 struct idetape_bh
*bh
; /* The data buffers */
239 struct idetape_stage_s
*next
; /* Pointer to the next stage */
243 * Most of our global data which we need to save even as we leave the driver due
244 * to an interrupt or a timer event is stored in the struct defined below.
246 typedef struct ide_tape_obj
{
248 ide_driver_t
*driver
;
249 struct gendisk
*disk
;
253 * Since a typical character device operation requires more
254 * than one packet command, we provide here enough memory
255 * for the maximum of interconnected packet commands.
256 * The packet commands are stored in the circular array pc_stack.
257 * pc_stack_index points to the last used entry, and warps around
258 * to the start when we get to the last array entry.
260 * pc points to the current processed packet command.
262 * failed_pc points to the last failed packet command, or contains
263 * NULL if we do not need to retry any packet command. This is
264 * required since an additional packet command is needed before the
265 * retry, to get detailed information on what went wrong.
267 /* Current packet command */
268 struct ide_atapi_pc
*pc
;
269 /* Last failed packet command */
270 struct ide_atapi_pc
*failed_pc
;
271 /* Packet command stack */
272 struct ide_atapi_pc pc_stack
[IDETAPE_PC_STACK
];
273 /* Next free packet command storage space */
275 struct request rq_stack
[IDETAPE_PC_STACK
];
276 /* We implement a circular array */
280 * DSC polling variables.
282 * While polling for DSC we use postponed_rq to postpone the current
283 * request so that ide.c will be able to service pending requests on the
284 * other device. Note that at most we will have only one DSC (usually
285 * data transfer) request in the device request queue. Additional
286 * requests can be queued in our internal pipeline, but they will be
287 * visible to ide.c only one at a time.
289 struct request
*postponed_rq
;
290 /* The time in which we started polling for DSC */
291 unsigned long dsc_polling_start
;
292 /* Timer used to poll for dsc */
293 struct timer_list dsc_timer
;
294 /* Read/Write dsc polling frequency */
295 unsigned long best_dsc_rw_freq
;
296 unsigned long dsc_poll_freq
;
297 unsigned long dsc_timeout
;
299 /* Read position information */
302 unsigned int first_frame
;
304 /* Last error information */
305 u8 sense_key
, asc
, ascq
;
307 /* Character device operation */
311 /* Current character device data transfer direction */
314 /* tape block size, usually 512 or 1024 bytes */
315 unsigned short blk_size
;
318 /* Copy of the tape's Capabilities and Mechanical Page */
322 * Active data transfer request parameters.
324 * At most, there is only one ide-tape originated data transfer request
325 * in the device request queue. This allows ide.c to easily service
326 * requests from the other device when we postpone our active request.
329 /* Data buffer size chosen based on the tape's recommendation */
331 idetape_stage_t
*merge_stage
;
332 int merge_stage_size
;
333 struct idetape_bh
*bh
;
337 /* Pipeline parameters. */
339 /* Wasted space in each stage */
342 /* Status/Action flags: long for set_bit */
344 /* protects the ide-tape queue */
347 /* Measures average tape speed */
348 unsigned long avg_time
;
352 /* the door is currently locked */
354 /* the tape hardware is write protected */
356 /* the tape is write protected (hardware or opened as read-only) */
362 static DEFINE_MUTEX(idetape_ref_mutex
);
364 static struct class *idetape_sysfs_class
;
366 #define to_ide_tape(obj) container_of(obj, struct ide_tape_obj, kref)
368 #define ide_tape_g(disk) \
369 container_of((disk)->private_data, struct ide_tape_obj, driver)
371 static struct ide_tape_obj
*ide_tape_get(struct gendisk
*disk
)
373 struct ide_tape_obj
*tape
= NULL
;
375 mutex_lock(&idetape_ref_mutex
);
376 tape
= ide_tape_g(disk
);
378 kref_get(&tape
->kref
);
379 mutex_unlock(&idetape_ref_mutex
);
383 static void ide_tape_release(struct kref
*);
385 static void ide_tape_put(struct ide_tape_obj
*tape
)
387 mutex_lock(&idetape_ref_mutex
);
388 kref_put(&tape
->kref
, ide_tape_release
);
389 mutex_unlock(&idetape_ref_mutex
);
393 * The variables below are used for the character device interface. Additional
394 * state variables are defined in our ide_drive_t structure.
396 static struct ide_tape_obj
*idetape_devs
[MAX_HWIFS
* MAX_DRIVES
];
398 #define ide_tape_f(file) ((file)->private_data)
400 static struct ide_tape_obj
*ide_tape_chrdev_get(unsigned int i
)
402 struct ide_tape_obj
*tape
= NULL
;
404 mutex_lock(&idetape_ref_mutex
);
405 tape
= idetape_devs
[i
];
407 kref_get(&tape
->kref
);
408 mutex_unlock(&idetape_ref_mutex
);
412 static void idetape_input_buffers(ide_drive_t
*drive
, struct ide_atapi_pc
*pc
,
415 struct idetape_bh
*bh
= pc
->bh
;
420 printk(KERN_ERR
"ide-tape: bh == NULL in "
421 "idetape_input_buffers\n");
422 ide_atapi_discard_data(drive
, bcount
);
426 (unsigned int)(bh
->b_size
- atomic_read(&bh
->b_count
)),
428 HWIF(drive
)->atapi_input_bytes(drive
, bh
->b_data
+
429 atomic_read(&bh
->b_count
), count
);
431 atomic_add(count
, &bh
->b_count
);
432 if (atomic_read(&bh
->b_count
) == bh
->b_size
) {
435 atomic_set(&bh
->b_count
, 0);
441 static void idetape_output_buffers(ide_drive_t
*drive
, struct ide_atapi_pc
*pc
,
444 struct idetape_bh
*bh
= pc
->bh
;
449 printk(KERN_ERR
"ide-tape: bh == NULL in %s\n",
453 count
= min((unsigned int)pc
->b_count
, (unsigned int)bcount
);
454 HWIF(drive
)->atapi_output_bytes(drive
, pc
->b_data
, count
);
457 pc
->b_count
-= count
;
462 pc
->b_data
= bh
->b_data
;
463 pc
->b_count
= atomic_read(&bh
->b_count
);
469 static void idetape_update_buffers(struct ide_atapi_pc
*pc
)
471 struct idetape_bh
*bh
= pc
->bh
;
473 unsigned int bcount
= pc
->xferred
;
475 if (pc
->flags
& PC_FLAG_WRITING
)
479 printk(KERN_ERR
"ide-tape: bh == NULL in %s\n",
483 count
= min((unsigned int)bh
->b_size
, (unsigned int)bcount
);
484 atomic_set(&bh
->b_count
, count
);
485 if (atomic_read(&bh
->b_count
) == bh
->b_size
)
493 * idetape_next_pc_storage returns a pointer to a place in which we can
494 * safely store a packet command, even though we intend to leave the
495 * driver. A storage space for a maximum of IDETAPE_PC_STACK packet
496 * commands is allocated at initialization time.
498 static struct ide_atapi_pc
*idetape_next_pc_storage(ide_drive_t
*drive
)
500 idetape_tape_t
*tape
= drive
->driver_data
;
502 debug_log(DBG_PCRQ_STACK
, "pc_stack_index=%d\n", tape
->pc_stack_index
);
504 if (tape
->pc_stack_index
== IDETAPE_PC_STACK
)
505 tape
->pc_stack_index
= 0;
506 return (&tape
->pc_stack
[tape
->pc_stack_index
++]);
510 * idetape_next_rq_storage is used along with idetape_next_pc_storage.
511 * Since we queue packet commands in the request queue, we need to
512 * allocate a request, along with the allocation of a packet command.
515 /**************************************************************
517 * This should get fixed to use kmalloc(.., GFP_ATOMIC) *
518 * followed later on by kfree(). -ml *
520 **************************************************************/
522 static struct request
*idetape_next_rq_storage(ide_drive_t
*drive
)
524 idetape_tape_t
*tape
= drive
->driver_data
;
526 debug_log(DBG_PCRQ_STACK
, "rq_stack_index=%d\n", tape
->rq_stack_index
);
528 if (tape
->rq_stack_index
== IDETAPE_PC_STACK
)
529 tape
->rq_stack_index
= 0;
530 return (&tape
->rq_stack
[tape
->rq_stack_index
++]);
533 static void idetape_init_pc(struct ide_atapi_pc
*pc
)
535 memset(pc
->c
, 0, 12);
539 pc
->buf
= pc
->pc_buf
;
540 pc
->buf_size
= IDETAPE_PC_BUFFER_SIZE
;
546 * called on each failed packet command retry to analyze the request sense. We
547 * currently do not utilize this information.
549 static void idetape_analyze_error(ide_drive_t
*drive
, u8
*sense
)
551 idetape_tape_t
*tape
= drive
->driver_data
;
552 struct ide_atapi_pc
*pc
= tape
->failed_pc
;
554 tape
->sense_key
= sense
[2] & 0xF;
555 tape
->asc
= sense
[12];
556 tape
->ascq
= sense
[13];
558 debug_log(DBG_ERR
, "pc = %x, sense key = %x, asc = %x, ascq = %x\n",
559 pc
->c
[0], tape
->sense_key
, tape
->asc
, tape
->ascq
);
561 /* Correct pc->xferred by asking the tape. */
562 if (pc
->flags
& PC_FLAG_DMA_ERROR
) {
563 pc
->xferred
= pc
->req_xfer
-
565 be32_to_cpu(get_unaligned((u32
*)&sense
[3]));
566 idetape_update_buffers(pc
);
570 * If error was the result of a zero-length read or write command,
571 * with sense key=5, asc=0x22, ascq=0, let it slide. Some drives
572 * (i.e. Seagate STT3401A Travan) don't support 0-length read/writes.
574 if ((pc
->c
[0] == READ_6
|| pc
->c
[0] == WRITE_6
)
576 && pc
->c
[4] == 0 && pc
->c
[3] == 0 && pc
->c
[2] == 0) {
577 if (tape
->sense_key
== 5) {
578 /* don't report an error, everything's ok */
580 /* don't retry read/write */
581 pc
->flags
|= PC_FLAG_ABORT
;
584 if (pc
->c
[0] == READ_6
&& (sense
[2] & 0x80)) {
585 pc
->error
= IDETAPE_ERROR_FILEMARK
;
586 pc
->flags
|= PC_FLAG_ABORT
;
588 if (pc
->c
[0] == WRITE_6
) {
589 if ((sense
[2] & 0x40) || (tape
->sense_key
== 0xd
590 && tape
->asc
== 0x0 && tape
->ascq
== 0x2)) {
591 pc
->error
= IDETAPE_ERROR_EOD
;
592 pc
->flags
|= PC_FLAG_ABORT
;
595 if (pc
->c
[0] == READ_6
|| pc
->c
[0] == WRITE_6
) {
596 if (tape
->sense_key
== 8) {
597 pc
->error
= IDETAPE_ERROR_EOD
;
598 pc
->flags
|= PC_FLAG_ABORT
;
600 if (!(pc
->flags
& PC_FLAG_ABORT
) &&
602 pc
->retries
= IDETAPE_MAX_PC_RETRIES
+ 1;
606 /* Free a stage along with its related buffers completely. */
607 static void __idetape_kfree_stage(idetape_stage_t
*stage
)
609 struct idetape_bh
*prev_bh
, *bh
= stage
->bh
;
613 if (bh
->b_data
!= NULL
) {
614 size
= (int) bh
->b_size
;
616 free_page((unsigned long) bh
->b_data
);
618 bh
->b_data
+= PAGE_SIZE
;
629 * Finish servicing a request and insert a pending pipeline request into the
632 static int idetape_end_request(ide_drive_t
*drive
, int uptodate
, int nr_sects
)
634 struct request
*rq
= HWGROUP(drive
)->rq
;
635 idetape_tape_t
*tape
= drive
->driver_data
;
639 debug_log(DBG_PROCS
, "Enter %s\n", __func__
);
642 case 0: error
= IDETAPE_ERROR_GENERAL
; break;
643 case 1: error
= 0; break;
644 default: error
= uptodate
;
648 tape
->failed_pc
= NULL
;
650 if (!blk_special_request(rq
)) {
651 ide_end_request(drive
, uptodate
, nr_sects
);
655 spin_lock_irqsave(&tape
->lock
, flags
);
657 ide_end_drive_cmd(drive
, 0, 0);
659 spin_unlock_irqrestore(&tape
->lock
, flags
);
663 static ide_startstop_t
idetape_request_sense_callback(ide_drive_t
*drive
)
665 idetape_tape_t
*tape
= drive
->driver_data
;
667 debug_log(DBG_PROCS
, "Enter %s\n", __func__
);
669 if (!tape
->pc
->error
) {
670 idetape_analyze_error(drive
, tape
->pc
->buf
);
671 idetape_end_request(drive
, 1, 0);
673 printk(KERN_ERR
"ide-tape: Error in REQUEST SENSE itself - "
674 "Aborting request!\n");
675 idetape_end_request(drive
, 0, 0);
680 static void idetape_create_request_sense_cmd(struct ide_atapi_pc
*pc
)
683 pc
->c
[0] = REQUEST_SENSE
;
686 pc
->idetape_callback
= &idetape_request_sense_callback
;
689 static void idetape_init_rq(struct request
*rq
, u8 cmd
)
691 memset(rq
, 0, sizeof(*rq
));
692 rq
->cmd_type
= REQ_TYPE_SPECIAL
;
697 * Generate a new packet command request in front of the request queue, before
698 * the current request, so that it will be processed immediately, on the next
699 * pass through the driver. The function below is called from the request
700 * handling part of the driver (the "bottom" part). Safe storage for the request
701 * should be allocated with ide_tape_next_{pc,rq}_storage() prior to that.
703 * Memory for those requests is pre-allocated at initialization time, and is
704 * limited to IDETAPE_PC_STACK requests. We assume that we have enough space for
705 * the maximum possible number of inter-dependent packet commands.
707 * The higher level of the driver - The ioctl handler and the character device
708 * handling functions should queue request to the lower level part and wait for
709 * their completion using idetape_queue_pc_tail or idetape_queue_rw_tail.
711 static void idetape_queue_pc_head(ide_drive_t
*drive
, struct ide_atapi_pc
*pc
,
714 struct ide_tape_obj
*tape
= drive
->driver_data
;
716 idetape_init_rq(rq
, REQ_IDETAPE_PC1
);
717 rq
->buffer
= (char *) pc
;
718 rq
->rq_disk
= tape
->disk
;
719 (void) ide_do_drive_cmd(drive
, rq
, ide_preempt
);
723 * idetape_retry_pc is called when an error was detected during the
724 * last packet command. We queue a request sense packet command in
725 * the head of the request list.
727 static ide_startstop_t
idetape_retry_pc (ide_drive_t
*drive
)
729 idetape_tape_t
*tape
= drive
->driver_data
;
730 struct ide_atapi_pc
*pc
;
733 (void)ide_read_error(drive
);
734 pc
= idetape_next_pc_storage(drive
);
735 rq
= idetape_next_rq_storage(drive
);
736 idetape_create_request_sense_cmd(pc
);
737 set_bit(IDETAPE_FLAG_IGNORE_DSC
, &tape
->flags
);
738 idetape_queue_pc_head(drive
, pc
, rq
);
743 * Postpone the current request so that ide.c will be able to service requests
744 * from another device on the same hwgroup while we are polling for DSC.
746 static void idetape_postpone_request(ide_drive_t
*drive
)
748 idetape_tape_t
*tape
= drive
->driver_data
;
750 debug_log(DBG_PROCS
, "Enter %s\n", __func__
);
752 tape
->postponed_rq
= HWGROUP(drive
)->rq
;
753 ide_stall_queue(drive
, tape
->dsc_poll_freq
);
756 typedef void idetape_io_buf(ide_drive_t
*, struct ide_atapi_pc
*, unsigned int);
759 * This is the usual interrupt handler which will be called during a packet
760 * command. We will transfer some of the data (as requested by the drive) and
761 * will re-point interrupt handler to us. When data transfer is finished, we
762 * will act according to the algorithm described before
765 static ide_startstop_t
idetape_pc_intr(ide_drive_t
*drive
)
767 ide_hwif_t
*hwif
= drive
->hwif
;
768 idetape_tape_t
*tape
= drive
->driver_data
;
769 struct ide_atapi_pc
*pc
= tape
->pc
;
770 xfer_func_t
*xferfunc
;
771 idetape_io_buf
*iobuf
;
774 static int error_sim_count
;
779 debug_log(DBG_PROCS
, "Enter %s - interrupt handler\n", __func__
);
781 /* Clear the interrupt */
782 stat
= ide_read_status(drive
);
784 if (pc
->flags
& PC_FLAG_DMA_IN_PROGRESS
) {
785 if (hwif
->dma_ops
->dma_end(drive
) || (stat
& ERR_STAT
)) {
787 * A DMA error is sometimes expected. For example,
788 * if the tape is crossing a filemark during a
789 * READ command, it will issue an irq and position
790 * itself before the filemark, so that only a partial
791 * data transfer will occur (which causes the DMA
792 * error). In that case, we will later ask the tape
793 * how much bytes of the original request were
794 * actually transferred (we can't receive that
795 * information from the DMA engine on most chipsets).
799 * On the contrary, a DMA error is never expected;
800 * it usually indicates a hardware error or abort.
801 * If the tape crosses a filemark during a READ
802 * command, it will issue an irq and position itself
803 * after the filemark (not before). Only a partial
804 * data transfer will occur, but no DMA error.
807 pc
->flags
|= PC_FLAG_DMA_ERROR
;
809 pc
->xferred
= pc
->req_xfer
;
810 idetape_update_buffers(pc
);
812 debug_log(DBG_PROCS
, "DMA finished\n");
816 /* No more interrupts */
817 if ((stat
& DRQ_STAT
) == 0) {
818 debug_log(DBG_SENSE
, "Packet command completed, %d bytes"
819 " transferred\n", pc
->xferred
);
821 pc
->flags
&= ~PC_FLAG_DMA_IN_PROGRESS
;
825 if ((pc
->c
[0] == WRITE_6
|| pc
->c
[0] == READ_6
) &&
826 (++error_sim_count
% 100) == 0) {
827 printk(KERN_INFO
"ide-tape: %s: simulating error\n",
832 if ((stat
& ERR_STAT
) && pc
->c
[0] == REQUEST_SENSE
)
834 if ((stat
& ERR_STAT
) || (pc
->flags
& PC_FLAG_DMA_ERROR
)) {
836 debug_log(DBG_ERR
, "%s: I/O error\n", tape
->name
);
838 if (pc
->c
[0] == REQUEST_SENSE
) {
839 printk(KERN_ERR
"ide-tape: I/O error in request"
841 return ide_do_reset(drive
);
843 debug_log(DBG_ERR
, "[cmd %x]: check condition\n",
846 /* Retry operation */
847 return idetape_retry_pc(drive
);
850 if ((pc
->flags
& PC_FLAG_WAIT_FOR_DSC
) &&
851 (stat
& SEEK_STAT
) == 0) {
852 /* Media access command */
853 tape
->dsc_polling_start
= jiffies
;
854 tape
->dsc_poll_freq
= IDETAPE_DSC_MA_FAST
;
855 tape
->dsc_timeout
= jiffies
+ IDETAPE_DSC_MA_TIMEOUT
;
856 /* Allow ide.c to handle other requests */
857 idetape_postpone_request(drive
);
860 if (tape
->failed_pc
== pc
)
861 tape
->failed_pc
= NULL
;
862 /* Command finished - Call the callback function */
863 return pc
->idetape_callback(drive
);
866 if (pc
->flags
& PC_FLAG_DMA_IN_PROGRESS
) {
867 pc
->flags
&= ~PC_FLAG_DMA_IN_PROGRESS
;
868 printk(KERN_ERR
"ide-tape: The tape wants to issue more "
869 "interrupts in DMA mode\n");
870 printk(KERN_ERR
"ide-tape: DMA disabled, reverting to PIO\n");
872 return ide_do_reset(drive
);
874 /* Get the number of bytes to transfer on this interrupt. */
875 bcount
= (hwif
->INB(hwif
->io_ports
[IDE_BCOUNTH_OFFSET
]) << 8) |
876 hwif
->INB(hwif
->io_ports
[IDE_BCOUNTL_OFFSET
]);
878 ireason
= hwif
->INB(hwif
->io_ports
[IDE_IREASON_OFFSET
]);
881 printk(KERN_ERR
"ide-tape: CoD != 0 in %s\n", __func__
);
882 return ide_do_reset(drive
);
884 if (((ireason
& IO
) == IO
) == !!(pc
->flags
& PC_FLAG_WRITING
)) {
885 /* Hopefully, we will never get here */
886 printk(KERN_ERR
"ide-tape: We wanted to %s, ",
887 (ireason
& IO
) ? "Write" : "Read");
888 printk(KERN_ERR
"ide-tape: but the tape wants us to %s !\n",
889 (ireason
& IO
) ? "Read" : "Write");
890 return ide_do_reset(drive
);
892 if (!(pc
->flags
& PC_FLAG_WRITING
)) {
893 /* Reading - Check that we have enough space */
894 temp
= pc
->xferred
+ bcount
;
895 if (temp
> pc
->req_xfer
) {
896 if (temp
> pc
->buf_size
) {
897 printk(KERN_ERR
"ide-tape: The tape wants to "
898 "send us more data than expected "
899 "- discarding data\n");
900 ide_atapi_discard_data(drive
, bcount
);
901 ide_set_handler(drive
, &idetape_pc_intr
,
902 IDETAPE_WAIT_CMD
, NULL
);
905 debug_log(DBG_SENSE
, "The tape wants to send us more "
906 "data than expected - allowing transfer\n");
908 iobuf
= &idetape_input_buffers
;
909 xferfunc
= hwif
->atapi_input_bytes
;
911 iobuf
= &idetape_output_buffers
;
912 xferfunc
= hwif
->atapi_output_bytes
;
916 iobuf(drive
, pc
, bcount
);
918 xferfunc(drive
, pc
->cur_pos
, bcount
);
920 /* Update the current position */
921 pc
->xferred
+= bcount
;
922 pc
->cur_pos
+= bcount
;
924 debug_log(DBG_SENSE
, "[cmd %x] transferred %d bytes on that intr.\n",
927 /* And set the interrupt handler again */
928 ide_set_handler(drive
, &idetape_pc_intr
, IDETAPE_WAIT_CMD
, NULL
);
933 * Packet Command Interface
935 * The current Packet Command is available in tape->pc, and will not change
936 * until we finish handling it. Each packet command is associated with a
937 * callback function that will be called when the command is finished.
939 * The handling will be done in three stages:
941 * 1. idetape_issue_pc will send the packet command to the drive, and will set
942 * the interrupt handler to idetape_pc_intr.
944 * 2. On each interrupt, idetape_pc_intr will be called. This step will be
945 * repeated until the device signals us that no more interrupts will be issued.
947 * 3. ATAPI Tape media access commands have immediate status with a delayed
948 * process. In case of a successful initiation of a media access packet command,
949 * the DSC bit will be set when the actual execution of the command is finished.
950 * Since the tape drive will not issue an interrupt, we have to poll for this
951 * event. In this case, we define the request as "low priority request" by
952 * setting rq_status to IDETAPE_RQ_POSTPONED, set a timer to poll for DSC and
955 * ide.c will then give higher priority to requests which originate from the
956 * other device, until will change rq_status to RQ_ACTIVE.
958 * 4. When the packet command is finished, it will be checked for errors.
960 * 5. In case an error was found, we queue a request sense packet command in
961 * front of the request queue and retry the operation up to
962 * IDETAPE_MAX_PC_RETRIES times.
964 * 6. In case no error was found, or we decided to give up and not to retry
965 * again, the callback function will be called and then we will handle the next
968 static ide_startstop_t
idetape_transfer_pc(ide_drive_t
*drive
)
970 ide_hwif_t
*hwif
= drive
->hwif
;
971 idetape_tape_t
*tape
= drive
->driver_data
;
972 struct ide_atapi_pc
*pc
= tape
->pc
;
974 ide_startstop_t startstop
;
977 if (ide_wait_stat(&startstop
, drive
, DRQ_STAT
, BUSY_STAT
, WAIT_READY
)) {
978 printk(KERN_ERR
"ide-tape: Strange, packet command initiated "
979 "yet DRQ isn't asserted\n");
982 ireason
= hwif
->INB(hwif
->io_ports
[IDE_IREASON_OFFSET
]);
983 while (retries
-- && ((ireason
& CD
) == 0 || (ireason
& IO
))) {
984 printk(KERN_ERR
"ide-tape: (IO,CoD != (0,1) while issuing "
985 "a packet command, retrying\n");
987 ireason
= hwif
->INB(hwif
->io_ports
[IDE_IREASON_OFFSET
]);
989 printk(KERN_ERR
"ide-tape: (IO,CoD != (0,1) while "
990 "issuing a packet command, ignoring\n");
995 if ((ireason
& CD
) == 0 || (ireason
& IO
)) {
996 printk(KERN_ERR
"ide-tape: (IO,CoD) != (0,1) while issuing "
997 "a packet command\n");
998 return ide_do_reset(drive
);
1000 /* Set the interrupt routine */
1001 ide_set_handler(drive
, &idetape_pc_intr
, IDETAPE_WAIT_CMD
, NULL
);
1002 #ifdef CONFIG_BLK_DEV_IDEDMA
1003 /* Begin DMA, if necessary */
1004 if (pc
->flags
& PC_FLAG_DMA_IN_PROGRESS
)
1005 hwif
->dma_ops
->dma_start(drive
);
1007 /* Send the actual packet */
1008 HWIF(drive
)->atapi_output_bytes(drive
, pc
->c
, 12);
1012 static ide_startstop_t
idetape_issue_pc(ide_drive_t
*drive
,
1013 struct ide_atapi_pc
*pc
)
1015 ide_hwif_t
*hwif
= drive
->hwif
;
1016 idetape_tape_t
*tape
= drive
->driver_data
;
1020 if (tape
->pc
->c
[0] == REQUEST_SENSE
&&
1021 pc
->c
[0] == REQUEST_SENSE
) {
1022 printk(KERN_ERR
"ide-tape: possible ide-tape.c bug - "
1023 "Two request sense in serial were issued\n");
1026 if (tape
->failed_pc
== NULL
&& pc
->c
[0] != REQUEST_SENSE
)
1027 tape
->failed_pc
= pc
;
1028 /* Set the current packet command */
1031 if (pc
->retries
> IDETAPE_MAX_PC_RETRIES
||
1032 (pc
->flags
& PC_FLAG_ABORT
)) {
1034 * We will "abort" retrying a packet command in case legitimate
1035 * error code was received (crossing a filemark, or end of the
1036 * media, for example).
1038 if (!(pc
->flags
& PC_FLAG_ABORT
)) {
1039 if (!(pc
->c
[0] == TEST_UNIT_READY
&&
1040 tape
->sense_key
== 2 && tape
->asc
== 4 &&
1041 (tape
->ascq
== 1 || tape
->ascq
== 8))) {
1042 printk(KERN_ERR
"ide-tape: %s: I/O error, "
1043 "pc = %2x, key = %2x, "
1044 "asc = %2x, ascq = %2x\n",
1045 tape
->name
, pc
->c
[0],
1046 tape
->sense_key
, tape
->asc
,
1050 pc
->error
= IDETAPE_ERROR_GENERAL
;
1052 tape
->failed_pc
= NULL
;
1053 return pc
->idetape_callback(drive
);
1055 debug_log(DBG_SENSE
, "Retry #%d, cmd = %02X\n", pc
->retries
, pc
->c
[0]);
1058 /* We haven't transferred any data yet */
1060 pc
->cur_pos
= pc
->buf
;
1061 /* Request to transfer the entire buffer at once */
1062 bcount
= pc
->req_xfer
;
1064 if (pc
->flags
& PC_FLAG_DMA_ERROR
) {
1065 pc
->flags
&= ~PC_FLAG_DMA_ERROR
;
1066 printk(KERN_WARNING
"ide-tape: DMA disabled, "
1067 "reverting to PIO\n");
1070 if ((pc
->flags
& PC_FLAG_DMA_RECOMMENDED
) && drive
->using_dma
)
1071 dma_ok
= !hwif
->dma_ops
->dma_setup(drive
);
1073 ide_pktcmd_tf_load(drive
, IDE_TFLAG_NO_SELECT_MASK
|
1074 IDE_TFLAG_OUT_DEVICE
, bcount
, dma_ok
);
1077 /* Will begin DMA later */
1078 pc
->flags
|= PC_FLAG_DMA_IN_PROGRESS
;
1079 if (test_bit(IDETAPE_FLAG_DRQ_INTERRUPT
, &tape
->flags
)) {
1080 ide_execute_command(drive
, WIN_PACKETCMD
, &idetape_transfer_pc
,
1081 IDETAPE_WAIT_CMD
, NULL
);
1084 hwif
->OUTB(WIN_PACKETCMD
, hwif
->io_ports
[IDE_COMMAND_OFFSET
]);
1085 return idetape_transfer_pc(drive
);
1089 static ide_startstop_t
idetape_pc_callback(ide_drive_t
*drive
)
1091 idetape_tape_t
*tape
= drive
->driver_data
;
1093 debug_log(DBG_PROCS
, "Enter %s\n", __func__
);
1095 idetape_end_request(drive
, tape
->pc
->error
? 0 : 1, 0);
1099 /* A mode sense command is used to "sense" tape parameters. */
1100 static void idetape_create_mode_sense_cmd(struct ide_atapi_pc
*pc
, u8 page_code
)
1102 idetape_init_pc(pc
);
1103 pc
->c
[0] = MODE_SENSE
;
1104 if (page_code
!= IDETAPE_BLOCK_DESCRIPTOR
)
1105 /* DBD = 1 - Don't return block descriptors */
1107 pc
->c
[2] = page_code
;
1109 * Changed pc->c[3] to 0 (255 will at best return unused info).
1111 * For SCSI this byte is defined as subpage instead of high byte
1112 * of length and some IDE drives seem to interpret it this way
1113 * and return an error when 255 is used.
1116 /* We will just discard data in that case */
1118 if (page_code
== IDETAPE_BLOCK_DESCRIPTOR
)
1120 else if (page_code
== IDETAPE_CAPABILITIES_PAGE
)
1124 pc
->idetape_callback
= &idetape_pc_callback
;
1127 static ide_startstop_t
idetape_media_access_finished(ide_drive_t
*drive
)
1129 idetape_tape_t
*tape
= drive
->driver_data
;
1130 struct ide_atapi_pc
*pc
= tape
->pc
;
1133 stat
= ide_read_status(drive
);
1135 if (stat
& SEEK_STAT
) {
1136 if (stat
& ERR_STAT
) {
1137 /* Error detected */
1138 if (pc
->c
[0] != TEST_UNIT_READY
)
1139 printk(KERN_ERR
"ide-tape: %s: I/O error, ",
1141 /* Retry operation */
1142 return idetape_retry_pc(drive
);
1145 if (tape
->failed_pc
== pc
)
1146 tape
->failed_pc
= NULL
;
1148 pc
->error
= IDETAPE_ERROR_GENERAL
;
1149 tape
->failed_pc
= NULL
;
1151 return pc
->idetape_callback(drive
);
1154 static ide_startstop_t
idetape_rw_callback(ide_drive_t
*drive
)
1156 idetape_tape_t
*tape
= drive
->driver_data
;
1157 struct request
*rq
= HWGROUP(drive
)->rq
;
1158 int blocks
= tape
->pc
->xferred
/ tape
->blk_size
;
1160 tape
->avg_size
+= blocks
* tape
->blk_size
;
1162 if (time_after_eq(jiffies
, tape
->avg_time
+ HZ
)) {
1163 tape
->avg_speed
= tape
->avg_size
* HZ
/
1164 (jiffies
- tape
->avg_time
) / 1024;
1166 tape
->avg_time
= jiffies
;
1168 debug_log(DBG_PROCS
, "Enter %s\n", __func__
);
1170 tape
->first_frame
+= blocks
;
1171 rq
->current_nr_sectors
-= blocks
;
1173 if (!tape
->pc
->error
)
1174 idetape_end_request(drive
, 1, 0);
1176 idetape_end_request(drive
, tape
->pc
->error
, 0);
1180 static void idetape_create_read_cmd(idetape_tape_t
*tape
,
1181 struct ide_atapi_pc
*pc
,
1182 unsigned int length
, struct idetape_bh
*bh
)
1184 idetape_init_pc(pc
);
1186 put_unaligned(cpu_to_be32(length
), (unsigned int *) &pc
->c
[1]);
1188 pc
->idetape_callback
= &idetape_rw_callback
;
1190 atomic_set(&bh
->b_count
, 0);
1192 pc
->buf_size
= length
* tape
->blk_size
;
1193 pc
->req_xfer
= pc
->buf_size
;
1194 if (pc
->req_xfer
== tape
->stage_size
)
1195 pc
->flags
|= PC_FLAG_DMA_RECOMMENDED
;
1198 static void idetape_create_write_cmd(idetape_tape_t
*tape
,
1199 struct ide_atapi_pc
*pc
,
1200 unsigned int length
, struct idetape_bh
*bh
)
1202 idetape_init_pc(pc
);
1204 put_unaligned(cpu_to_be32(length
), (unsigned int *) &pc
->c
[1]);
1206 pc
->idetape_callback
= &idetape_rw_callback
;
1207 pc
->flags
|= PC_FLAG_WRITING
;
1209 pc
->b_data
= bh
->b_data
;
1210 pc
->b_count
= atomic_read(&bh
->b_count
);
1212 pc
->buf_size
= length
* tape
->blk_size
;
1213 pc
->req_xfer
= pc
->buf_size
;
1214 if (pc
->req_xfer
== tape
->stage_size
)
1215 pc
->flags
|= PC_FLAG_DMA_RECOMMENDED
;
1218 static ide_startstop_t
idetape_do_request(ide_drive_t
*drive
,
1219 struct request
*rq
, sector_t block
)
1221 idetape_tape_t
*tape
= drive
->driver_data
;
1222 struct ide_atapi_pc
*pc
= NULL
;
1223 struct request
*postponed_rq
= tape
->postponed_rq
;
1226 debug_log(DBG_SENSE
, "sector: %ld, nr_sectors: %ld,"
1227 " current_nr_sectors: %d\n",
1228 rq
->sector
, rq
->nr_sectors
, rq
->current_nr_sectors
);
1230 if (!blk_special_request(rq
)) {
1231 /* We do not support buffer cache originated requests. */
1232 printk(KERN_NOTICE
"ide-tape: %s: Unsupported request in "
1233 "request queue (%d)\n", drive
->name
, rq
->cmd_type
);
1234 ide_end_request(drive
, 0, 0);
1238 /* Retry a failed packet command */
1239 if (tape
->failed_pc
&& tape
->pc
->c
[0] == REQUEST_SENSE
)
1240 return idetape_issue_pc(drive
, tape
->failed_pc
);
1242 if (postponed_rq
!= NULL
)
1243 if (rq
!= postponed_rq
) {
1244 printk(KERN_ERR
"ide-tape: ide-tape.c bug - "
1245 "Two DSC requests were queued\n");
1246 idetape_end_request(drive
, 0, 0);
1250 tape
->postponed_rq
= NULL
;
1253 * If the tape is still busy, postpone our request and service
1254 * the other device meanwhile.
1256 stat
= ide_read_status(drive
);
1258 if (!drive
->dsc_overlap
&& !(rq
->cmd
[0] & REQ_IDETAPE_PC2
))
1259 set_bit(IDETAPE_FLAG_IGNORE_DSC
, &tape
->flags
);
1261 if (drive
->post_reset
== 1) {
1262 set_bit(IDETAPE_FLAG_IGNORE_DSC
, &tape
->flags
);
1263 drive
->post_reset
= 0;
1266 if (!test_and_clear_bit(IDETAPE_FLAG_IGNORE_DSC
, &tape
->flags
) &&
1267 (stat
& SEEK_STAT
) == 0) {
1268 if (postponed_rq
== NULL
) {
1269 tape
->dsc_polling_start
= jiffies
;
1270 tape
->dsc_poll_freq
= tape
->best_dsc_rw_freq
;
1271 tape
->dsc_timeout
= jiffies
+ IDETAPE_DSC_RW_TIMEOUT
;
1272 } else if (time_after(jiffies
, tape
->dsc_timeout
)) {
1273 printk(KERN_ERR
"ide-tape: %s: DSC timeout\n",
1275 if (rq
->cmd
[0] & REQ_IDETAPE_PC2
) {
1276 idetape_media_access_finished(drive
);
1279 return ide_do_reset(drive
);
1281 } else if (time_after(jiffies
,
1282 tape
->dsc_polling_start
+
1283 IDETAPE_DSC_MA_THRESHOLD
))
1284 tape
->dsc_poll_freq
= IDETAPE_DSC_MA_SLOW
;
1285 idetape_postpone_request(drive
);
1288 if (rq
->cmd
[0] & REQ_IDETAPE_READ
) {
1289 pc
= idetape_next_pc_storage(drive
);
1290 idetape_create_read_cmd(tape
, pc
, rq
->current_nr_sectors
,
1291 (struct idetape_bh
*)rq
->special
);
1294 if (rq
->cmd
[0] & REQ_IDETAPE_WRITE
) {
1295 pc
= idetape_next_pc_storage(drive
);
1296 idetape_create_write_cmd(tape
, pc
, rq
->current_nr_sectors
,
1297 (struct idetape_bh
*)rq
->special
);
1300 if (rq
->cmd
[0] & REQ_IDETAPE_PC1
) {
1301 pc
= (struct ide_atapi_pc
*) rq
->buffer
;
1302 rq
->cmd
[0] &= ~(REQ_IDETAPE_PC1
);
1303 rq
->cmd
[0] |= REQ_IDETAPE_PC2
;
1306 if (rq
->cmd
[0] & REQ_IDETAPE_PC2
) {
1307 idetape_media_access_finished(drive
);
1312 return idetape_issue_pc(drive
, pc
);
1315 /* Pipeline related functions */
1318 * The function below uses __get_free_page to allocate a pipeline stage, along
1319 * with all the necessary small buffers which together make a buffer of size
1320 * tape->stage_size (or a bit more). We attempt to combine sequential pages as
1323 * It returns a pointer to the new allocated stage, or NULL if we can't (or
1324 * don't want to) allocate a stage.
1326 * Pipeline stages are optional and are used to increase performance. If we
1327 * can't allocate them, we'll manage without them.
1329 static idetape_stage_t
*__idetape_kmalloc_stage(idetape_tape_t
*tape
, int full
,
1332 idetape_stage_t
*stage
;
1333 struct idetape_bh
*prev_bh
, *bh
;
1334 int pages
= tape
->pages_per_stage
;
1335 char *b_data
= NULL
;
1337 stage
= kmalloc(sizeof(idetape_stage_t
), GFP_KERNEL
);
1342 stage
->bh
= kmalloc(sizeof(struct idetape_bh
), GFP_KERNEL
);
1346 bh
->b_reqnext
= NULL
;
1347 bh
->b_data
= (char *) __get_free_page(GFP_KERNEL
);
1351 memset(bh
->b_data
, 0, PAGE_SIZE
);
1352 bh
->b_size
= PAGE_SIZE
;
1353 atomic_set(&bh
->b_count
, full
? bh
->b_size
: 0);
1356 b_data
= (char *) __get_free_page(GFP_KERNEL
);
1360 memset(b_data
, 0, PAGE_SIZE
);
1361 if (bh
->b_data
== b_data
+ PAGE_SIZE
) {
1362 bh
->b_size
+= PAGE_SIZE
;
1363 bh
->b_data
-= PAGE_SIZE
;
1365 atomic_add(PAGE_SIZE
, &bh
->b_count
);
1368 if (b_data
== bh
->b_data
+ bh
->b_size
) {
1369 bh
->b_size
+= PAGE_SIZE
;
1371 atomic_add(PAGE_SIZE
, &bh
->b_count
);
1375 bh
= kmalloc(sizeof(struct idetape_bh
), GFP_KERNEL
);
1377 free_page((unsigned long) b_data
);
1380 bh
->b_reqnext
= NULL
;
1381 bh
->b_data
= b_data
;
1382 bh
->b_size
= PAGE_SIZE
;
1383 atomic_set(&bh
->b_count
, full
? bh
->b_size
: 0);
1384 prev_bh
->b_reqnext
= bh
;
1386 bh
->b_size
-= tape
->excess_bh_size
;
1388 atomic_sub(tape
->excess_bh_size
, &bh
->b_count
);
1391 __idetape_kfree_stage(stage
);
1395 static int idetape_copy_stage_from_user(idetape_tape_t
*tape
,
1396 const char __user
*buf
, int n
)
1398 struct idetape_bh
*bh
= tape
->bh
;
1404 printk(KERN_ERR
"ide-tape: bh == NULL in %s\n",
1408 count
= min((unsigned int)
1409 (bh
->b_size
- atomic_read(&bh
->b_count
)),
1411 if (copy_from_user(bh
->b_data
+ atomic_read(&bh
->b_count
), buf
,
1415 atomic_add(count
, &bh
->b_count
);
1417 if (atomic_read(&bh
->b_count
) == bh
->b_size
) {
1420 atomic_set(&bh
->b_count
, 0);
1427 static int idetape_copy_stage_to_user(idetape_tape_t
*tape
, char __user
*buf
,
1430 struct idetape_bh
*bh
= tape
->bh
;
1436 printk(KERN_ERR
"ide-tape: bh == NULL in %s\n",
1440 count
= min(tape
->b_count
, n
);
1441 if (copy_to_user(buf
, tape
->b_data
, count
))
1444 tape
->b_data
+= count
;
1445 tape
->b_count
-= count
;
1447 if (!tape
->b_count
) {
1451 tape
->b_data
= bh
->b_data
;
1452 tape
->b_count
= atomic_read(&bh
->b_count
);
1459 static void idetape_init_merge_stage(idetape_tape_t
*tape
)
1461 struct idetape_bh
*bh
= tape
->merge_stage
->bh
;
1464 if (tape
->chrdev_dir
== IDETAPE_DIR_WRITE
)
1465 atomic_set(&bh
->b_count
, 0);
1467 tape
->b_data
= bh
->b_data
;
1468 tape
->b_count
= atomic_read(&bh
->b_count
);
1472 static ide_startstop_t
idetape_read_position_callback(ide_drive_t
*drive
)
1474 idetape_tape_t
*tape
= drive
->driver_data
;
1475 u8
*readpos
= tape
->pc
->buf
;
1477 debug_log(DBG_PROCS
, "Enter %s\n", __func__
);
1479 if (!tape
->pc
->error
) {
1480 debug_log(DBG_SENSE
, "BOP - %s\n",
1481 (readpos
[0] & 0x80) ? "Yes" : "No");
1482 debug_log(DBG_SENSE
, "EOP - %s\n",
1483 (readpos
[0] & 0x40) ? "Yes" : "No");
1485 if (readpos
[0] & 0x4) {
1486 printk(KERN_INFO
"ide-tape: Block location is unknown"
1488 clear_bit(IDETAPE_FLAG_ADDRESS_VALID
, &tape
->flags
);
1489 idetape_end_request(drive
, 0, 0);
1491 debug_log(DBG_SENSE
, "Block Location - %u\n",
1492 be32_to_cpu(*(u32
*)&readpos
[4]));
1494 tape
->partition
= readpos
[1];
1496 be32_to_cpu(*(u32
*)&readpos
[4]);
1497 set_bit(IDETAPE_FLAG_ADDRESS_VALID
, &tape
->flags
);
1498 idetape_end_request(drive
, 1, 0);
1501 idetape_end_request(drive
, 0, 0);
1507 * Write a filemark if write_filemark=1. Flush the device buffers without
1508 * writing a filemark otherwise.
1510 static void idetape_create_write_filemark_cmd(ide_drive_t
*drive
,
1511 struct ide_atapi_pc
*pc
, int write_filemark
)
1513 idetape_init_pc(pc
);
1514 pc
->c
[0] = WRITE_FILEMARKS
;
1515 pc
->c
[4] = write_filemark
;
1516 pc
->flags
|= PC_FLAG_WAIT_FOR_DSC
;
1517 pc
->idetape_callback
= &idetape_pc_callback
;
1520 static void idetape_create_test_unit_ready_cmd(struct ide_atapi_pc
*pc
)
1522 idetape_init_pc(pc
);
1523 pc
->c
[0] = TEST_UNIT_READY
;
1524 pc
->idetape_callback
= &idetape_pc_callback
;
1528 * We add a special packet command request to the tail of the request queue, and
1529 * wait for it to be serviced. This is not to be called from within the request
1530 * handling part of the driver! We allocate here data on the stack and it is
1531 * valid until the request is finished. This is not the case for the bottom part
1532 * of the driver, where we are always leaving the functions to wait for an
1533 * interrupt or a timer event.
1535 * From the bottom part of the driver, we should allocate safe memory using
1536 * idetape_next_pc_storage() and ide_tape_next_rq_storage(), and add the request
1537 * to the request list without waiting for it to be serviced! In that case, we
1538 * usually use idetape_queue_pc_head().
1540 static int idetape_queue_pc_tail(ide_drive_t
*drive
, struct ide_atapi_pc
*pc
)
1542 struct ide_tape_obj
*tape
= drive
->driver_data
;
1545 idetape_init_rq(&rq
, REQ_IDETAPE_PC1
);
1546 rq
.buffer
= (char *) pc
;
1547 rq
.rq_disk
= tape
->disk
;
1548 return ide_do_drive_cmd(drive
, &rq
, ide_wait
);
1551 static void idetape_create_load_unload_cmd(ide_drive_t
*drive
,
1552 struct ide_atapi_pc
*pc
, int cmd
)
1554 idetape_init_pc(pc
);
1555 pc
->c
[0] = START_STOP
;
1557 pc
->flags
|= PC_FLAG_WAIT_FOR_DSC
;
1558 pc
->idetape_callback
= &idetape_pc_callback
;
1561 static int idetape_wait_ready(ide_drive_t
*drive
, unsigned long timeout
)
1563 idetape_tape_t
*tape
= drive
->driver_data
;
1564 struct ide_atapi_pc pc
;
1565 int load_attempted
= 0;
1567 /* Wait for the tape to become ready */
1568 set_bit(IDETAPE_FLAG_MEDIUM_PRESENT
, &tape
->flags
);
1570 while (time_before(jiffies
, timeout
)) {
1571 idetape_create_test_unit_ready_cmd(&pc
);
1572 if (!idetape_queue_pc_tail(drive
, &pc
))
1574 if ((tape
->sense_key
== 2 && tape
->asc
== 4 && tape
->ascq
== 2)
1575 || (tape
->asc
== 0x3A)) {
1579 idetape_create_load_unload_cmd(drive
, &pc
,
1580 IDETAPE_LU_LOAD_MASK
);
1581 idetape_queue_pc_tail(drive
, &pc
);
1583 /* not about to be ready */
1584 } else if (!(tape
->sense_key
== 2 && tape
->asc
== 4 &&
1585 (tape
->ascq
== 1 || tape
->ascq
== 8)))
1592 static int idetape_flush_tape_buffers(ide_drive_t
*drive
)
1594 struct ide_atapi_pc pc
;
1597 idetape_create_write_filemark_cmd(drive
, &pc
, 0);
1598 rc
= idetape_queue_pc_tail(drive
, &pc
);
1601 idetape_wait_ready(drive
, 60 * 5 * HZ
);
1605 static void idetape_create_read_position_cmd(struct ide_atapi_pc
*pc
)
1607 idetape_init_pc(pc
);
1608 pc
->c
[0] = READ_POSITION
;
1610 pc
->idetape_callback
= &idetape_read_position_callback
;
1613 static int idetape_read_position(ide_drive_t
*drive
)
1615 idetape_tape_t
*tape
= drive
->driver_data
;
1616 struct ide_atapi_pc pc
;
1619 debug_log(DBG_PROCS
, "Enter %s\n", __func__
);
1621 idetape_create_read_position_cmd(&pc
);
1622 if (idetape_queue_pc_tail(drive
, &pc
))
1624 position
= tape
->first_frame
;
1628 static void idetape_create_locate_cmd(ide_drive_t
*drive
,
1629 struct ide_atapi_pc
*pc
,
1630 unsigned int block
, u8 partition
, int skip
)
1632 idetape_init_pc(pc
);
1633 pc
->c
[0] = POSITION_TO_ELEMENT
;
1635 put_unaligned(cpu_to_be32(block
), (unsigned int *) &pc
->c
[3]);
1636 pc
->c
[8] = partition
;
1637 pc
->flags
|= PC_FLAG_WAIT_FOR_DSC
;
1638 pc
->idetape_callback
= &idetape_pc_callback
;
1641 static int idetape_create_prevent_cmd(ide_drive_t
*drive
,
1642 struct ide_atapi_pc
*pc
, int prevent
)
1644 idetape_tape_t
*tape
= drive
->driver_data
;
1646 /* device supports locking according to capabilities page */
1647 if (!(tape
->caps
[6] & 0x01))
1650 idetape_init_pc(pc
);
1651 pc
->c
[0] = ALLOW_MEDIUM_REMOVAL
;
1653 pc
->idetape_callback
= &idetape_pc_callback
;
1657 static int __idetape_discard_read_pipeline(ide_drive_t
*drive
)
1659 idetape_tape_t
*tape
= drive
->driver_data
;
1661 if (tape
->chrdev_dir
!= IDETAPE_DIR_READ
)
1664 clear_bit(IDETAPE_FLAG_FILEMARK
, &tape
->flags
);
1665 tape
->merge_stage_size
= 0;
1666 if (tape
->merge_stage
!= NULL
) {
1667 __idetape_kfree_stage(tape
->merge_stage
);
1668 tape
->merge_stage
= NULL
;
1671 tape
->chrdev_dir
= IDETAPE_DIR_NONE
;
1677 * Position the tape to the requested block using the LOCATE packet command.
1678 * A READ POSITION command is then issued to check where we are positioned. Like
1679 * all higher level operations, we queue the commands at the tail of the request
1680 * queue and wait for their completion.
1682 static int idetape_position_tape(ide_drive_t
*drive
, unsigned int block
,
1683 u8 partition
, int skip
)
1685 idetape_tape_t
*tape
= drive
->driver_data
;
1687 struct ide_atapi_pc pc
;
1689 if (tape
->chrdev_dir
== IDETAPE_DIR_READ
)
1690 __idetape_discard_read_pipeline(drive
);
1691 idetape_wait_ready(drive
, 60 * 5 * HZ
);
1692 idetape_create_locate_cmd(drive
, &pc
, block
, partition
, skip
);
1693 retval
= idetape_queue_pc_tail(drive
, &pc
);
1697 idetape_create_read_position_cmd(&pc
);
1698 return (idetape_queue_pc_tail(drive
, &pc
));
1701 static void idetape_discard_read_pipeline(ide_drive_t
*drive
,
1702 int restore_position
)
1704 idetape_tape_t
*tape
= drive
->driver_data
;
1708 cnt
= __idetape_discard_read_pipeline(drive
);
1709 if (restore_position
) {
1710 position
= idetape_read_position(drive
);
1711 seek
= position
> cnt
? position
- cnt
: 0;
1712 if (idetape_position_tape(drive
, seek
, 0, 0)) {
1713 printk(KERN_INFO
"ide-tape: %s: position_tape failed in"
1714 " discard_pipeline()\n", tape
->name
);
1721 * Generate a read/write request for the block device interface and wait for it
1724 static int idetape_queue_rw_tail(ide_drive_t
*drive
, int cmd
, int blocks
,
1725 struct idetape_bh
*bh
)
1727 idetape_tape_t
*tape
= drive
->driver_data
;
1730 debug_log(DBG_SENSE
, "%s: cmd=%d\n", __func__
, cmd
);
1732 idetape_init_rq(&rq
, cmd
);
1733 rq
.rq_disk
= tape
->disk
;
1734 rq
.special
= (void *)bh
;
1735 rq
.sector
= tape
->first_frame
;
1736 rq
.nr_sectors
= blocks
;
1737 rq
.current_nr_sectors
= blocks
;
1738 (void) ide_do_drive_cmd(drive
, &rq
, ide_wait
);
1740 if ((cmd
& (REQ_IDETAPE_READ
| REQ_IDETAPE_WRITE
)) == 0)
1743 if (tape
->merge_stage
)
1744 idetape_init_merge_stage(tape
);
1745 if (rq
.errors
== IDETAPE_ERROR_GENERAL
)
1747 return (tape
->blk_size
* (blocks
-rq
.current_nr_sectors
));
1750 static void idetape_create_inquiry_cmd(struct ide_atapi_pc
*pc
)
1752 idetape_init_pc(pc
);
1756 pc
->idetape_callback
= &idetape_pc_callback
;
1759 static void idetape_create_rewind_cmd(ide_drive_t
*drive
,
1760 struct ide_atapi_pc
*pc
)
1762 idetape_init_pc(pc
);
1763 pc
->c
[0] = REZERO_UNIT
;
1764 pc
->flags
|= PC_FLAG_WAIT_FOR_DSC
;
1765 pc
->idetape_callback
= &idetape_pc_callback
;
1768 static void idetape_create_erase_cmd(struct ide_atapi_pc
*pc
)
1770 idetape_init_pc(pc
);
1773 pc
->flags
|= PC_FLAG_WAIT_FOR_DSC
;
1774 pc
->idetape_callback
= &idetape_pc_callback
;
1777 static void idetape_create_space_cmd(struct ide_atapi_pc
*pc
, int count
, u8 cmd
)
1779 idetape_init_pc(pc
);
1781 put_unaligned(cpu_to_be32(count
), (unsigned int *) &pc
->c
[1]);
1783 pc
->flags
|= PC_FLAG_WAIT_FOR_DSC
;
1784 pc
->idetape_callback
= &idetape_pc_callback
;
1787 /* Queue up a character device originated write request. */
1788 static int idetape_add_chrdev_write_request(ide_drive_t
*drive
, int blocks
)
1790 idetape_tape_t
*tape
= drive
->driver_data
;
1792 debug_log(DBG_CHRDEV
, "Enter %s\n", __func__
);
1794 return idetape_queue_rw_tail(drive
, REQ_IDETAPE_WRITE
,
1795 blocks
, tape
->merge_stage
->bh
);
1798 static void idetape_empty_write_pipeline(ide_drive_t
*drive
)
1800 idetape_tape_t
*tape
= drive
->driver_data
;
1802 struct idetape_bh
*bh
;
1804 if (tape
->chrdev_dir
!= IDETAPE_DIR_WRITE
) {
1805 printk(KERN_ERR
"ide-tape: bug: Trying to empty write pipeline,"
1806 " but we are not writing.\n");
1809 if (tape
->merge_stage_size
> tape
->stage_size
) {
1810 printk(KERN_ERR
"ide-tape: bug: merge_buffer too big\n");
1811 tape
->merge_stage_size
= tape
->stage_size
;
1813 if (tape
->merge_stage_size
) {
1814 blocks
= tape
->merge_stage_size
/ tape
->blk_size
;
1815 if (tape
->merge_stage_size
% tape
->blk_size
) {
1819 i
= tape
->blk_size
- tape
->merge_stage_size
%
1821 bh
= tape
->bh
->b_reqnext
;
1823 atomic_set(&bh
->b_count
, 0);
1829 printk(KERN_INFO
"ide-tape: bug,"
1833 min
= min(i
, (unsigned int)(bh
->b_size
-
1834 atomic_read(&bh
->b_count
)));
1835 memset(bh
->b_data
+ atomic_read(&bh
->b_count
),
1837 atomic_add(min
, &bh
->b_count
);
1842 (void) idetape_add_chrdev_write_request(drive
, blocks
);
1843 tape
->merge_stage_size
= 0;
1845 if (tape
->merge_stage
!= NULL
) {
1846 __idetape_kfree_stage(tape
->merge_stage
);
1847 tape
->merge_stage
= NULL
;
1849 tape
->chrdev_dir
= IDETAPE_DIR_NONE
;
1852 static int idetape_init_read(ide_drive_t
*drive
)
1854 idetape_tape_t
*tape
= drive
->driver_data
;
1857 /* Initialize read operation */
1858 if (tape
->chrdev_dir
!= IDETAPE_DIR_READ
) {
1859 if (tape
->chrdev_dir
== IDETAPE_DIR_WRITE
) {
1860 idetape_empty_write_pipeline(drive
);
1861 idetape_flush_tape_buffers(drive
);
1863 if (tape
->merge_stage
|| tape
->merge_stage_size
) {
1864 printk(KERN_ERR
"ide-tape: merge_stage_size should be"
1866 tape
->merge_stage_size
= 0;
1868 tape
->merge_stage
= __idetape_kmalloc_stage(tape
, 0, 0);
1869 if (!tape
->merge_stage
)
1871 tape
->chrdev_dir
= IDETAPE_DIR_READ
;
1874 * Issue a read 0 command to ensure that DSC handshake is
1875 * switched from completion mode to buffer available mode.
1876 * No point in issuing this if DSC overlap isn't supported, some
1877 * drives (Seagate STT3401A) will return an error.
1879 if (drive
->dsc_overlap
) {
1880 bytes_read
= idetape_queue_rw_tail(drive
,
1881 REQ_IDETAPE_READ
, 0,
1882 tape
->merge_stage
->bh
);
1883 if (bytes_read
< 0) {
1884 __idetape_kfree_stage(tape
->merge_stage
);
1885 tape
->merge_stage
= NULL
;
1886 tape
->chrdev_dir
= IDETAPE_DIR_NONE
;
1896 * Called from idetape_chrdev_read() to service a character device read request
1897 * and add read-ahead requests to our pipeline.
1899 static int idetape_add_chrdev_read_request(ide_drive_t
*drive
, int blocks
)
1901 idetape_tape_t
*tape
= drive
->driver_data
;
1903 debug_log(DBG_PROCS
, "Enter %s, %d blocks\n", __func__
, blocks
);
1905 /* If we are at a filemark, return a read length of 0 */
1906 if (test_bit(IDETAPE_FLAG_FILEMARK
, &tape
->flags
))
1909 idetape_init_read(drive
);
1911 return idetape_queue_rw_tail(drive
, REQ_IDETAPE_READ
, blocks
,
1912 tape
->merge_stage
->bh
);
1915 static void idetape_pad_zeros(ide_drive_t
*drive
, int bcount
)
1917 idetape_tape_t
*tape
= drive
->driver_data
;
1918 struct idetape_bh
*bh
;
1924 bh
= tape
->merge_stage
->bh
;
1925 count
= min(tape
->stage_size
, bcount
);
1927 blocks
= count
/ tape
->blk_size
;
1929 atomic_set(&bh
->b_count
,
1930 min(count
, (unsigned int)bh
->b_size
));
1931 memset(bh
->b_data
, 0, atomic_read(&bh
->b_count
));
1932 count
-= atomic_read(&bh
->b_count
);
1935 idetape_queue_rw_tail(drive
, REQ_IDETAPE_WRITE
, blocks
,
1936 tape
->merge_stage
->bh
);
1941 * Rewinds the tape to the Beginning Of the current Partition (BOP). We
1942 * currently support only one partition.
1944 static int idetape_rewind_tape(ide_drive_t
*drive
)
1947 struct ide_atapi_pc pc
;
1948 idetape_tape_t
*tape
;
1949 tape
= drive
->driver_data
;
1951 debug_log(DBG_SENSE
, "Enter %s\n", __func__
);
1953 idetape_create_rewind_cmd(drive
, &pc
);
1954 retval
= idetape_queue_pc_tail(drive
, &pc
);
1958 idetape_create_read_position_cmd(&pc
);
1959 retval
= idetape_queue_pc_tail(drive
, &pc
);
1965 /* mtio.h compatible commands should be issued to the chrdev interface. */
1966 static int idetape_blkdev_ioctl(ide_drive_t
*drive
, unsigned int cmd
,
1969 idetape_tape_t
*tape
= drive
->driver_data
;
1970 void __user
*argp
= (void __user
*)arg
;
1972 struct idetape_config
{
1973 int dsc_rw_frequency
;
1974 int dsc_media_access_frequency
;
1978 debug_log(DBG_PROCS
, "Enter %s\n", __func__
);
1982 if (copy_from_user(&config
, argp
, sizeof(config
)))
1984 tape
->best_dsc_rw_freq
= config
.dsc_rw_frequency
;
1987 config
.dsc_rw_frequency
= (int) tape
->best_dsc_rw_freq
;
1988 config
.nr_stages
= 1;
1989 if (copy_to_user(argp
, &config
, sizeof(config
)))
1998 static int idetape_space_over_filemarks(ide_drive_t
*drive
, short mt_op
,
2001 idetape_tape_t
*tape
= drive
->driver_data
;
2002 struct ide_atapi_pc pc
;
2003 int retval
, count
= 0;
2004 int sprev
= !!(tape
->caps
[4] & 0x20);
2008 if (MTBSF
== mt_op
|| MTBSFM
== mt_op
) {
2011 mt_count
= -mt_count
;
2014 if (tape
->chrdev_dir
== IDETAPE_DIR_READ
) {
2015 tape
->merge_stage_size
= 0;
2016 if (test_and_clear_bit(IDETAPE_FLAG_FILEMARK
, &tape
->flags
))
2018 idetape_discard_read_pipeline(drive
, 0);
2022 * The filemark was not found in our internal pipeline; now we can issue
2023 * the space command.
2028 idetape_create_space_cmd(&pc
, mt_count
- count
,
2029 IDETAPE_SPACE_OVER_FILEMARK
);
2030 return idetape_queue_pc_tail(drive
, &pc
);
2035 retval
= idetape_space_over_filemarks(drive
, MTFSF
,
2039 count
= (MTBSFM
== mt_op
? 1 : -1);
2040 return idetape_space_over_filemarks(drive
, MTFSF
, count
);
2042 printk(KERN_ERR
"ide-tape: MTIO operation %d not supported\n",
2049 * Our character device read / write functions.
2051 * The tape is optimized to maximize throughput when it is transferring an
2052 * integral number of the "continuous transfer limit", which is a parameter of
2053 * the specific tape (26kB on my particular tape, 32kB for Onstream).
2055 * As of version 1.3 of the driver, the character device provides an abstract
2056 * continuous view of the media - any mix of block sizes (even 1 byte) on the
2057 * same backup/restore procedure is supported. The driver will internally
2058 * convert the requests to the recommended transfer unit, so that an unmatch
2059 * between the user's block size to the recommended size will only result in a
2060 * (slightly) increased driver overhead, but will no longer hit performance.
2061 * This is not applicable to Onstream.
2063 static ssize_t
idetape_chrdev_read(struct file
*file
, char __user
*buf
,
2064 size_t count
, loff_t
*ppos
)
2066 struct ide_tape_obj
*tape
= ide_tape_f(file
);
2067 ide_drive_t
*drive
= tape
->drive
;
2068 ssize_t bytes_read
, temp
, actually_read
= 0, rc
;
2070 u16 ctl
= *(u16
*)&tape
->caps
[12];
2072 debug_log(DBG_CHRDEV
, "Enter %s, count %Zd\n", __func__
, count
);
2074 if (tape
->chrdev_dir
!= IDETAPE_DIR_READ
) {
2075 if (test_bit(IDETAPE_FLAG_DETECT_BS
, &tape
->flags
))
2076 if (count
> tape
->blk_size
&&
2077 (count
% tape
->blk_size
) == 0)
2078 tape
->user_bs_factor
= count
/ tape
->blk_size
;
2080 rc
= idetape_init_read(drive
);
2085 if (tape
->merge_stage_size
) {
2086 actually_read
= min((unsigned int)(tape
->merge_stage_size
),
2087 (unsigned int)count
);
2088 if (idetape_copy_stage_to_user(tape
, buf
, actually_read
))
2090 buf
+= actually_read
;
2091 tape
->merge_stage_size
-= actually_read
;
2092 count
-= actually_read
;
2094 while (count
>= tape
->stage_size
) {
2095 bytes_read
= idetape_add_chrdev_read_request(drive
, ctl
);
2096 if (bytes_read
<= 0)
2098 if (idetape_copy_stage_to_user(tape
, buf
, bytes_read
))
2101 count
-= bytes_read
;
2102 actually_read
+= bytes_read
;
2105 bytes_read
= idetape_add_chrdev_read_request(drive
, ctl
);
2106 if (bytes_read
<= 0)
2108 temp
= min((unsigned long)count
, (unsigned long)bytes_read
);
2109 if (idetape_copy_stage_to_user(tape
, buf
, temp
))
2111 actually_read
+= temp
;
2112 tape
->merge_stage_size
= bytes_read
-temp
;
2115 if (!actually_read
&& test_bit(IDETAPE_FLAG_FILEMARK
, &tape
->flags
)) {
2116 debug_log(DBG_SENSE
, "%s: spacing over filemark\n", tape
->name
);
2118 idetape_space_over_filemarks(drive
, MTFSF
, 1);
2122 return ret
? ret
: actually_read
;
2125 static ssize_t
idetape_chrdev_write(struct file
*file
, const char __user
*buf
,
2126 size_t count
, loff_t
*ppos
)
2128 struct ide_tape_obj
*tape
= ide_tape_f(file
);
2129 ide_drive_t
*drive
= tape
->drive
;
2130 ssize_t actually_written
= 0;
2132 u16 ctl
= *(u16
*)&tape
->caps
[12];
2134 /* The drive is write protected. */
2135 if (tape
->write_prot
)
2138 debug_log(DBG_CHRDEV
, "Enter %s, count %Zd\n", __func__
, count
);
2140 /* Initialize write operation */
2141 if (tape
->chrdev_dir
!= IDETAPE_DIR_WRITE
) {
2142 if (tape
->chrdev_dir
== IDETAPE_DIR_READ
)
2143 idetape_discard_read_pipeline(drive
, 1);
2144 if (tape
->merge_stage
|| tape
->merge_stage_size
) {
2145 printk(KERN_ERR
"ide-tape: merge_stage_size "
2146 "should be 0 now\n");
2147 tape
->merge_stage_size
= 0;
2149 tape
->merge_stage
= __idetape_kmalloc_stage(tape
, 0, 0);
2150 if (!tape
->merge_stage
)
2152 tape
->chrdev_dir
= IDETAPE_DIR_WRITE
;
2153 idetape_init_merge_stage(tape
);
2156 * Issue a write 0 command to ensure that DSC handshake is
2157 * switched from completion mode to buffer available mode. No
2158 * point in issuing this if DSC overlap isn't supported, some
2159 * drives (Seagate STT3401A) will return an error.
2161 if (drive
->dsc_overlap
) {
2162 ssize_t retval
= idetape_queue_rw_tail(drive
,
2163 REQ_IDETAPE_WRITE
, 0,
2164 tape
->merge_stage
->bh
);
2166 __idetape_kfree_stage(tape
->merge_stage
);
2167 tape
->merge_stage
= NULL
;
2168 tape
->chrdev_dir
= IDETAPE_DIR_NONE
;
2175 if (tape
->merge_stage_size
) {
2176 if (tape
->merge_stage_size
>= tape
->stage_size
) {
2177 printk(KERN_ERR
"ide-tape: bug: merge buf too big\n");
2178 tape
->merge_stage_size
= 0;
2180 actually_written
= min((unsigned int)
2181 (tape
->stage_size
- tape
->merge_stage_size
),
2182 (unsigned int)count
);
2183 if (idetape_copy_stage_from_user(tape
, buf
, actually_written
))
2185 buf
+= actually_written
;
2186 tape
->merge_stage_size
+= actually_written
;
2187 count
-= actually_written
;
2189 if (tape
->merge_stage_size
== tape
->stage_size
) {
2191 tape
->merge_stage_size
= 0;
2192 retval
= idetape_add_chrdev_write_request(drive
, ctl
);
2197 while (count
>= tape
->stage_size
) {
2199 if (idetape_copy_stage_from_user(tape
, buf
, tape
->stage_size
))
2201 buf
+= tape
->stage_size
;
2202 count
-= tape
->stage_size
;
2203 retval
= idetape_add_chrdev_write_request(drive
, ctl
);
2204 actually_written
+= tape
->stage_size
;
2209 actually_written
+= count
;
2210 if (idetape_copy_stage_from_user(tape
, buf
, count
))
2212 tape
->merge_stage_size
+= count
;
2214 return ret
? ret
: actually_written
;
2217 static int idetape_write_filemark(ide_drive_t
*drive
)
2219 struct ide_atapi_pc pc
;
2221 /* Write a filemark */
2222 idetape_create_write_filemark_cmd(drive
, &pc
, 1);
2223 if (idetape_queue_pc_tail(drive
, &pc
)) {
2224 printk(KERN_ERR
"ide-tape: Couldn't write a filemark\n");
2231 * Called from idetape_chrdev_ioctl when the general mtio MTIOCTOP ioctl is
2234 * Note: MTBSF and MTBSFM are not supported when the tape doesn't support
2235 * spacing over filemarks in the reverse direction. In this case, MTFSFM is also
2236 * usually not supported (it is supported in the rare case in which we crossed
2237 * the filemark during our read-ahead pipelined operation mode).
2239 * The following commands are currently not supported:
2241 * MTFSS, MTBSS, MTWSM, MTSETDENSITY, MTSETDRVBUFFER, MT_ST_BOOLEANS,
2242 * MT_ST_WRITE_THRESHOLD.
2244 static int idetape_mtioctop(ide_drive_t
*drive
, short mt_op
, int mt_count
)
2246 idetape_tape_t
*tape
= drive
->driver_data
;
2247 struct ide_atapi_pc pc
;
2250 debug_log(DBG_ERR
, "Handling MTIOCTOP ioctl: mt_op=%d, mt_count=%d\n",
2253 /* Commands which need our pipelined read-ahead stages. */
2261 return idetape_space_over_filemarks(drive
, mt_op
, mt_count
);
2268 if (tape
->write_prot
)
2270 idetape_discard_read_pipeline(drive
, 1);
2271 for (i
= 0; i
< mt_count
; i
++) {
2272 retval
= idetape_write_filemark(drive
);
2278 idetape_discard_read_pipeline(drive
, 0);
2279 if (idetape_rewind_tape(drive
))
2283 idetape_discard_read_pipeline(drive
, 0);
2284 idetape_create_load_unload_cmd(drive
, &pc
,
2285 IDETAPE_LU_LOAD_MASK
);
2286 return idetape_queue_pc_tail(drive
, &pc
);
2290 * If door is locked, attempt to unlock before
2291 * attempting to eject.
2293 if (tape
->door_locked
) {
2294 if (idetape_create_prevent_cmd(drive
, &pc
, 0))
2295 if (!idetape_queue_pc_tail(drive
, &pc
))
2296 tape
->door_locked
= DOOR_UNLOCKED
;
2298 idetape_discard_read_pipeline(drive
, 0);
2299 idetape_create_load_unload_cmd(drive
, &pc
,
2300 !IDETAPE_LU_LOAD_MASK
);
2301 retval
= idetape_queue_pc_tail(drive
, &pc
);
2303 clear_bit(IDETAPE_FLAG_MEDIUM_PRESENT
, &tape
->flags
);
2306 idetape_discard_read_pipeline(drive
, 0);
2307 return idetape_flush_tape_buffers(drive
);
2309 idetape_discard_read_pipeline(drive
, 0);
2310 idetape_create_load_unload_cmd(drive
, &pc
,
2311 IDETAPE_LU_RETENSION_MASK
| IDETAPE_LU_LOAD_MASK
);
2312 return idetape_queue_pc_tail(drive
, &pc
);
2314 idetape_create_space_cmd(&pc
, 0, IDETAPE_SPACE_TO_EOD
);
2315 return idetape_queue_pc_tail(drive
, &pc
);
2317 (void)idetape_rewind_tape(drive
);
2318 idetape_create_erase_cmd(&pc
);
2319 return idetape_queue_pc_tail(drive
, &pc
);
2322 if (mt_count
< tape
->blk_size
||
2323 mt_count
% tape
->blk_size
)
2325 tape
->user_bs_factor
= mt_count
/ tape
->blk_size
;
2326 clear_bit(IDETAPE_FLAG_DETECT_BS
, &tape
->flags
);
2328 set_bit(IDETAPE_FLAG_DETECT_BS
, &tape
->flags
);
2331 idetape_discard_read_pipeline(drive
, 0);
2332 return idetape_position_tape(drive
,
2333 mt_count
* tape
->user_bs_factor
, tape
->partition
, 0);
2335 idetape_discard_read_pipeline(drive
, 0);
2336 return idetape_position_tape(drive
, 0, mt_count
, 0);
2340 if (!idetape_create_prevent_cmd(drive
, &pc
, 1))
2342 retval
= idetape_queue_pc_tail(drive
, &pc
);
2345 tape
->door_locked
= DOOR_EXPLICITLY_LOCKED
;
2348 if (!idetape_create_prevent_cmd(drive
, &pc
, 0))
2350 retval
= idetape_queue_pc_tail(drive
, &pc
);
2353 tape
->door_locked
= DOOR_UNLOCKED
;
2356 printk(KERN_ERR
"ide-tape: MTIO operation %d not supported\n",
2363 * Our character device ioctls. General mtio.h magnetic io commands are
2364 * supported here, and not in the corresponding block interface. Our own
2365 * ide-tape ioctls are supported on both interfaces.
2367 static int idetape_chrdev_ioctl(struct inode
*inode
, struct file
*file
,
2368 unsigned int cmd
, unsigned long arg
)
2370 struct ide_tape_obj
*tape
= ide_tape_f(file
);
2371 ide_drive_t
*drive
= tape
->drive
;
2375 int block_offset
= 0, position
= tape
->first_frame
;
2376 void __user
*argp
= (void __user
*)arg
;
2378 debug_log(DBG_CHRDEV
, "Enter %s, cmd=%u\n", __func__
, cmd
);
2380 if (tape
->chrdev_dir
== IDETAPE_DIR_WRITE
) {
2381 idetape_empty_write_pipeline(drive
);
2382 idetape_flush_tape_buffers(drive
);
2384 if (cmd
== MTIOCGET
|| cmd
== MTIOCPOS
) {
2385 block_offset
= tape
->merge_stage_size
/
2386 (tape
->blk_size
* tape
->user_bs_factor
);
2387 position
= idetape_read_position(drive
);
2393 if (copy_from_user(&mtop
, argp
, sizeof(struct mtop
)))
2395 return idetape_mtioctop(drive
, mtop
.mt_op
, mtop
.mt_count
);
2397 memset(&mtget
, 0, sizeof(struct mtget
));
2398 mtget
.mt_type
= MT_ISSCSI2
;
2399 mtget
.mt_blkno
= position
/ tape
->user_bs_factor
- block_offset
;
2401 ((tape
->blk_size
* tape
->user_bs_factor
)
2402 << MT_ST_BLKSIZE_SHIFT
) & MT_ST_BLKSIZE_MASK
;
2404 if (tape
->drv_write_prot
)
2405 mtget
.mt_gstat
|= GMT_WR_PROT(0xffffffff);
2407 if (copy_to_user(argp
, &mtget
, sizeof(struct mtget
)))
2411 mtpos
.mt_blkno
= position
/ tape
->user_bs_factor
- block_offset
;
2412 if (copy_to_user(argp
, &mtpos
, sizeof(struct mtpos
)))
2416 if (tape
->chrdev_dir
== IDETAPE_DIR_READ
)
2417 idetape_discard_read_pipeline(drive
, 1);
2418 return idetape_blkdev_ioctl(drive
, cmd
, arg
);
2423 * Do a mode sense page 0 with block descriptor and if it succeeds set the tape
2424 * block size with the reported value.
2426 static void ide_tape_get_bsize_from_bdesc(ide_drive_t
*drive
)
2428 idetape_tape_t
*tape
= drive
->driver_data
;
2429 struct ide_atapi_pc pc
;
2431 idetape_create_mode_sense_cmd(&pc
, IDETAPE_BLOCK_DESCRIPTOR
);
2432 if (idetape_queue_pc_tail(drive
, &pc
)) {
2433 printk(KERN_ERR
"ide-tape: Can't get block descriptor\n");
2434 if (tape
->blk_size
== 0) {
2435 printk(KERN_WARNING
"ide-tape: Cannot deal with zero "
2436 "block size, assuming 32k\n");
2437 tape
->blk_size
= 32768;
2441 tape
->blk_size
= (pc
.buf
[4 + 5] << 16) +
2442 (pc
.buf
[4 + 6] << 8) +
2444 tape
->drv_write_prot
= (pc
.buf
[2] & 0x80) >> 7;
2447 static int idetape_chrdev_open(struct inode
*inode
, struct file
*filp
)
2449 unsigned int minor
= iminor(inode
), i
= minor
& ~0xc0;
2451 idetape_tape_t
*tape
;
2452 struct ide_atapi_pc pc
;
2455 if (i
>= MAX_HWIFS
* MAX_DRIVES
)
2458 tape
= ide_tape_chrdev_get(i
);
2462 debug_log(DBG_CHRDEV
, "Enter %s\n", __func__
);
2465 * We really want to do nonseekable_open(inode, filp); here, but some
2466 * versions of tar incorrectly call lseek on tapes and bail out if that
2467 * fails. So we disallow pread() and pwrite(), but permit lseeks.
2469 filp
->f_mode
&= ~(FMODE_PREAD
| FMODE_PWRITE
);
2471 drive
= tape
->drive
;
2473 filp
->private_data
= tape
;
2475 if (test_and_set_bit(IDETAPE_FLAG_BUSY
, &tape
->flags
)) {
2480 retval
= idetape_wait_ready(drive
, 60 * HZ
);
2482 clear_bit(IDETAPE_FLAG_BUSY
, &tape
->flags
);
2483 printk(KERN_ERR
"ide-tape: %s: drive not ready\n", tape
->name
);
2487 idetape_read_position(drive
);
2488 if (!test_bit(IDETAPE_FLAG_ADDRESS_VALID
, &tape
->flags
))
2489 (void)idetape_rewind_tape(drive
);
2491 /* Read block size and write protect status from drive. */
2492 ide_tape_get_bsize_from_bdesc(drive
);
2494 /* Set write protect flag if device is opened as read-only. */
2495 if ((filp
->f_flags
& O_ACCMODE
) == O_RDONLY
)
2496 tape
->write_prot
= 1;
2498 tape
->write_prot
= tape
->drv_write_prot
;
2500 /* Make sure drive isn't write protected if user wants to write. */
2501 if (tape
->write_prot
) {
2502 if ((filp
->f_flags
& O_ACCMODE
) == O_WRONLY
||
2503 (filp
->f_flags
& O_ACCMODE
) == O_RDWR
) {
2504 clear_bit(IDETAPE_FLAG_BUSY
, &tape
->flags
);
2510 /* Lock the tape drive door so user can't eject. */
2511 if (tape
->chrdev_dir
== IDETAPE_DIR_NONE
) {
2512 if (idetape_create_prevent_cmd(drive
, &pc
, 1)) {
2513 if (!idetape_queue_pc_tail(drive
, &pc
)) {
2514 if (tape
->door_locked
!= DOOR_EXPLICITLY_LOCKED
)
2515 tape
->door_locked
= DOOR_LOCKED
;
2526 static void idetape_write_release(ide_drive_t
*drive
, unsigned int minor
)
2528 idetape_tape_t
*tape
= drive
->driver_data
;
2530 idetape_empty_write_pipeline(drive
);
2531 tape
->merge_stage
= __idetape_kmalloc_stage(tape
, 1, 0);
2532 if (tape
->merge_stage
!= NULL
) {
2533 idetape_pad_zeros(drive
, tape
->blk_size
*
2534 (tape
->user_bs_factor
- 1));
2535 __idetape_kfree_stage(tape
->merge_stage
);
2536 tape
->merge_stage
= NULL
;
2538 idetape_write_filemark(drive
);
2539 idetape_flush_tape_buffers(drive
);
2540 idetape_flush_tape_buffers(drive
);
2543 static int idetape_chrdev_release(struct inode
*inode
, struct file
*filp
)
2545 struct ide_tape_obj
*tape
= ide_tape_f(filp
);
2546 ide_drive_t
*drive
= tape
->drive
;
2547 struct ide_atapi_pc pc
;
2548 unsigned int minor
= iminor(inode
);
2551 tape
= drive
->driver_data
;
2553 debug_log(DBG_CHRDEV
, "Enter %s\n", __func__
);
2555 if (tape
->chrdev_dir
== IDETAPE_DIR_WRITE
)
2556 idetape_write_release(drive
, minor
);
2557 if (tape
->chrdev_dir
== IDETAPE_DIR_READ
) {
2559 idetape_discard_read_pipeline(drive
, 1);
2562 if (minor
< 128 && test_bit(IDETAPE_FLAG_MEDIUM_PRESENT
, &tape
->flags
))
2563 (void) idetape_rewind_tape(drive
);
2564 if (tape
->chrdev_dir
== IDETAPE_DIR_NONE
) {
2565 if (tape
->door_locked
== DOOR_LOCKED
) {
2566 if (idetape_create_prevent_cmd(drive
, &pc
, 0)) {
2567 if (!idetape_queue_pc_tail(drive
, &pc
))
2568 tape
->door_locked
= DOOR_UNLOCKED
;
2572 clear_bit(IDETAPE_FLAG_BUSY
, &tape
->flags
);
2579 * check the contents of the ATAPI IDENTIFY command results. We return:
2581 * 1 - If the tape can be supported by us, based on the information we have so
2584 * 0 - If this tape driver is not currently supported by us.
2586 static int idetape_identify_device(ide_drive_t
*drive
)
2588 u8 gcw
[2], protocol
, device_type
, removable
, packet_size
;
2590 if (drive
->id_read
== 0)
2593 *((unsigned short *) &gcw
) = drive
->id
->config
;
2595 protocol
= (gcw
[1] & 0xC0) >> 6;
2596 device_type
= gcw
[1] & 0x1F;
2597 removable
= !!(gcw
[0] & 0x80);
2598 packet_size
= gcw
[0] & 0x3;
2600 /* Check that we can support this device */
2602 printk(KERN_ERR
"ide-tape: Protocol (0x%02x) is not ATAPI\n",
2604 else if (device_type
!= 1)
2605 printk(KERN_ERR
"ide-tape: Device type (0x%02x) is not set "
2606 "to tape\n", device_type
);
2607 else if (!removable
)
2608 printk(KERN_ERR
"ide-tape: The removable flag is not set\n");
2609 else if (packet_size
!= 0) {
2610 printk(KERN_ERR
"ide-tape: Packet size (0x%02x) is not 12"
2611 " bytes\n", packet_size
);
2617 static void idetape_get_inquiry_results(ide_drive_t
*drive
)
2619 idetape_tape_t
*tape
= drive
->driver_data
;
2620 struct ide_atapi_pc pc
;
2621 char fw_rev
[6], vendor_id
[10], product_id
[18];
2623 idetape_create_inquiry_cmd(&pc
);
2624 if (idetape_queue_pc_tail(drive
, &pc
)) {
2625 printk(KERN_ERR
"ide-tape: %s: can't get INQUIRY results\n",
2629 memcpy(vendor_id
, &pc
.buf
[8], 8);
2630 memcpy(product_id
, &pc
.buf
[16], 16);
2631 memcpy(fw_rev
, &pc
.buf
[32], 4);
2633 ide_fixstring(vendor_id
, 10, 0);
2634 ide_fixstring(product_id
, 18, 0);
2635 ide_fixstring(fw_rev
, 6, 0);
2637 printk(KERN_INFO
"ide-tape: %s <-> %s: %s %s rev %s\n",
2638 drive
->name
, tape
->name
, vendor_id
, product_id
, fw_rev
);
2642 * Ask the tape about its various parameters. In particular, we will adjust our
2643 * data transfer buffer size to the recommended value as returned by the tape.
2645 static void idetape_get_mode_sense_results(ide_drive_t
*drive
)
2647 idetape_tape_t
*tape
= drive
->driver_data
;
2648 struct ide_atapi_pc pc
;
2650 u8 speed
, max_speed
;
2652 idetape_create_mode_sense_cmd(&pc
, IDETAPE_CAPABILITIES_PAGE
);
2653 if (idetape_queue_pc_tail(drive
, &pc
)) {
2654 printk(KERN_ERR
"ide-tape: Can't get tape parameters - assuming"
2655 " some default values\n");
2656 tape
->blk_size
= 512;
2657 put_unaligned(52, (u16
*)&tape
->caps
[12]);
2658 put_unaligned(540, (u16
*)&tape
->caps
[14]);
2659 put_unaligned(6*52, (u16
*)&tape
->caps
[16]);
2662 caps
= pc
.buf
+ 4 + pc
.buf
[3];
2664 /* convert to host order and save for later use */
2665 speed
= be16_to_cpu(*(u16
*)&caps
[14]);
2666 max_speed
= be16_to_cpu(*(u16
*)&caps
[8]);
2668 put_unaligned(max_speed
, (u16
*)&caps
[8]);
2669 put_unaligned(be16_to_cpu(*(u16
*)&caps
[12]), (u16
*)&caps
[12]);
2670 put_unaligned(speed
, (u16
*)&caps
[14]);
2671 put_unaligned(be16_to_cpu(*(u16
*)&caps
[16]), (u16
*)&caps
[16]);
2674 printk(KERN_INFO
"ide-tape: %s: invalid tape speed "
2675 "(assuming 650KB/sec)\n", drive
->name
);
2676 put_unaligned(650, (u16
*)&caps
[14]);
2679 printk(KERN_INFO
"ide-tape: %s: invalid max_speed "
2680 "(assuming 650KB/sec)\n", drive
->name
);
2681 put_unaligned(650, (u16
*)&caps
[8]);
2684 memcpy(&tape
->caps
, caps
, 20);
2686 tape
->blk_size
= 512;
2687 else if (caps
[7] & 0x04)
2688 tape
->blk_size
= 1024;
2691 #ifdef CONFIG_IDE_PROC_FS
2692 static void idetape_add_settings(ide_drive_t
*drive
)
2694 idetape_tape_t
*tape
= drive
->driver_data
;
2696 ide_add_setting(drive
, "buffer", SETTING_READ
, TYPE_SHORT
, 0, 0xffff,
2697 1, 2, (u16
*)&tape
->caps
[16], NULL
);
2698 ide_add_setting(drive
, "speed", SETTING_READ
, TYPE_SHORT
, 0, 0xffff,
2699 1, 1, (u16
*)&tape
->caps
[14], NULL
);
2700 ide_add_setting(drive
, "stage", SETTING_READ
, TYPE_INT
, 0, 0xffff, 1,
2701 1024, &tape
->stage_size
, NULL
);
2702 ide_add_setting(drive
, "tdsc", SETTING_RW
, TYPE_INT
, IDETAPE_DSC_RW_MIN
,
2703 IDETAPE_DSC_RW_MAX
, 1000, HZ
, &tape
->best_dsc_rw_freq
,
2705 ide_add_setting(drive
, "dsc_overlap", SETTING_RW
, TYPE_BYTE
, 0, 1, 1,
2706 1, &drive
->dsc_overlap
, NULL
);
2707 ide_add_setting(drive
, "avg_speed", SETTING_READ
, TYPE_INT
, 0, 0xffff,
2708 1, 1, &tape
->avg_speed
, NULL
);
2709 ide_add_setting(drive
, "debug_mask", SETTING_RW
, TYPE_INT
, 0, 0xffff, 1,
2710 1, &tape
->debug_mask
, NULL
);
2713 static inline void idetape_add_settings(ide_drive_t
*drive
) { ; }
2717 * The function below is called to:
2719 * 1. Initialize our various state variables.
2720 * 2. Ask the tape for its capabilities.
2721 * 3. Allocate a buffer which will be used for data transfer. The buffer size
2722 * is chosen based on the recommendation which we received in step 2.
2724 * Note that at this point ide.c already assigned us an irq, so that we can
2725 * queue requests here and wait for their completion.
2727 static void idetape_setup(ide_drive_t
*drive
, idetape_tape_t
*tape
, int minor
)
2733 u16
*ctl
= (u16
*)&tape
->caps
[12];
2735 spin_lock_init(&tape
->lock
);
2736 drive
->dsc_overlap
= 1;
2737 if (drive
->hwif
->host_flags
& IDE_HFLAG_NO_DSC
) {
2738 printk(KERN_INFO
"ide-tape: %s: disabling DSC overlap\n",
2740 drive
->dsc_overlap
= 0;
2742 /* Seagate Travan drives do not support DSC overlap. */
2743 if (strstr(drive
->id
->model
, "Seagate STT3401"))
2744 drive
->dsc_overlap
= 0;
2745 tape
->minor
= minor
;
2746 tape
->name
[0] = 'h';
2747 tape
->name
[1] = 't';
2748 tape
->name
[2] = '0' + minor
;
2749 tape
->chrdev_dir
= IDETAPE_DIR_NONE
;
2750 tape
->pc
= tape
->pc_stack
;
2751 *((unsigned short *) &gcw
) = drive
->id
->config
;
2753 /* Command packet DRQ type */
2754 if (((gcw
[0] & 0x60) >> 5) == 1)
2755 set_bit(IDETAPE_FLAG_DRQ_INTERRUPT
, &tape
->flags
);
2757 idetape_get_inquiry_results(drive
);
2758 idetape_get_mode_sense_results(drive
);
2759 ide_tape_get_bsize_from_bdesc(drive
);
2760 tape
->user_bs_factor
= 1;
2761 tape
->stage_size
= *ctl
* tape
->blk_size
;
2762 while (tape
->stage_size
> 0xffff) {
2763 printk(KERN_NOTICE
"ide-tape: decreasing stage size\n");
2765 tape
->stage_size
= *ctl
* tape
->blk_size
;
2767 stage_size
= tape
->stage_size
;
2768 tape
->pages_per_stage
= stage_size
/ PAGE_SIZE
;
2769 if (stage_size
% PAGE_SIZE
) {
2770 tape
->pages_per_stage
++;
2771 tape
->excess_bh_size
= PAGE_SIZE
- stage_size
% PAGE_SIZE
;
2774 /* select the "best" DSC read/write polling freq */
2775 speed
= max(*(u16
*)&tape
->caps
[14], *(u16
*)&tape
->caps
[8]);
2777 t
= (IDETAPE_FIFO_THRESHOLD
* tape
->stage_size
* HZ
) / (speed
* 1000);
2780 * Ensure that the number we got makes sense; limit it within
2781 * IDETAPE_DSC_RW_MIN and IDETAPE_DSC_RW_MAX.
2783 tape
->best_dsc_rw_freq
= max_t(unsigned long,
2784 min_t(unsigned long, t
, IDETAPE_DSC_RW_MAX
),
2785 IDETAPE_DSC_RW_MIN
);
2786 printk(KERN_INFO
"ide-tape: %s <-> %s: %dKBps, %d*%dkB buffer, "
2788 drive
->name
, tape
->name
, *(u16
*)&tape
->caps
[14],
2789 (*(u16
*)&tape
->caps
[16] * 512) / tape
->stage_size
,
2790 tape
->stage_size
/ 1024,
2791 tape
->best_dsc_rw_freq
* 1000 / HZ
,
2792 drive
->using_dma
? ", DMA":"");
2794 idetape_add_settings(drive
);
2797 static void ide_tape_remove(ide_drive_t
*drive
)
2799 idetape_tape_t
*tape
= drive
->driver_data
;
2801 ide_proc_unregister_driver(drive
, tape
->driver
);
2803 ide_unregister_region(tape
->disk
);
2808 static void ide_tape_release(struct kref
*kref
)
2810 struct ide_tape_obj
*tape
= to_ide_tape(kref
);
2811 ide_drive_t
*drive
= tape
->drive
;
2812 struct gendisk
*g
= tape
->disk
;
2814 BUG_ON(tape
->merge_stage_size
);
2816 drive
->dsc_overlap
= 0;
2817 drive
->driver_data
= NULL
;
2818 device_destroy(idetape_sysfs_class
, MKDEV(IDETAPE_MAJOR
, tape
->minor
));
2819 device_destroy(idetape_sysfs_class
,
2820 MKDEV(IDETAPE_MAJOR
, tape
->minor
+ 128));
2821 idetape_devs
[tape
->minor
] = NULL
;
2822 g
->private_data
= NULL
;
2827 #ifdef CONFIG_IDE_PROC_FS
2828 static int proc_idetape_read_name
2829 (char *page
, char **start
, off_t off
, int count
, int *eof
, void *data
)
2831 ide_drive_t
*drive
= (ide_drive_t
*) data
;
2832 idetape_tape_t
*tape
= drive
->driver_data
;
2836 len
= sprintf(out
, "%s\n", tape
->name
);
2837 PROC_IDE_READ_RETURN(page
, start
, off
, count
, eof
, len
);
2840 static ide_proc_entry_t idetape_proc
[] = {
2841 { "capacity", S_IFREG
|S_IRUGO
, proc_ide_read_capacity
, NULL
},
2842 { "name", S_IFREG
|S_IRUGO
, proc_idetape_read_name
, NULL
},
2843 { NULL
, 0, NULL
, NULL
}
2847 static int ide_tape_probe(ide_drive_t
*);
2849 static ide_driver_t idetape_driver
= {
2851 .owner
= THIS_MODULE
,
2853 .bus
= &ide_bus_type
,
2855 .probe
= ide_tape_probe
,
2856 .remove
= ide_tape_remove
,
2857 .version
= IDETAPE_VERSION
,
2859 .supports_dsc_overlap
= 1,
2860 .do_request
= idetape_do_request
,
2861 .end_request
= idetape_end_request
,
2862 .error
= __ide_error
,
2863 .abort
= __ide_abort
,
2864 #ifdef CONFIG_IDE_PROC_FS
2865 .proc
= idetape_proc
,
2869 /* Our character device supporting functions, passed to register_chrdev. */
2870 static const struct file_operations idetape_fops
= {
2871 .owner
= THIS_MODULE
,
2872 .read
= idetape_chrdev_read
,
2873 .write
= idetape_chrdev_write
,
2874 .ioctl
= idetape_chrdev_ioctl
,
2875 .open
= idetape_chrdev_open
,
2876 .release
= idetape_chrdev_release
,
2879 static int idetape_open(struct inode
*inode
, struct file
*filp
)
2881 struct gendisk
*disk
= inode
->i_bdev
->bd_disk
;
2882 struct ide_tape_obj
*tape
;
2884 tape
= ide_tape_get(disk
);
2891 static int idetape_release(struct inode
*inode
, struct file
*filp
)
2893 struct gendisk
*disk
= inode
->i_bdev
->bd_disk
;
2894 struct ide_tape_obj
*tape
= ide_tape_g(disk
);
2901 static int idetape_ioctl(struct inode
*inode
, struct file
*file
,
2902 unsigned int cmd
, unsigned long arg
)
2904 struct block_device
*bdev
= inode
->i_bdev
;
2905 struct ide_tape_obj
*tape
= ide_tape_g(bdev
->bd_disk
);
2906 ide_drive_t
*drive
= tape
->drive
;
2907 int err
= generic_ide_ioctl(drive
, file
, bdev
, cmd
, arg
);
2909 err
= idetape_blkdev_ioctl(drive
, cmd
, arg
);
2913 static struct block_device_operations idetape_block_ops
= {
2914 .owner
= THIS_MODULE
,
2915 .open
= idetape_open
,
2916 .release
= idetape_release
,
2917 .ioctl
= idetape_ioctl
,
2920 static int ide_tape_probe(ide_drive_t
*drive
)
2922 idetape_tape_t
*tape
;
2926 if (!strstr("ide-tape", drive
->driver_req
))
2928 if (!drive
->present
)
2930 if (drive
->media
!= ide_tape
)
2932 if (!idetape_identify_device(drive
)) {
2933 printk(KERN_ERR
"ide-tape: %s: not supported by this version of"
2934 " the driver\n", drive
->name
);
2938 printk(KERN_INFO
"ide-tape: passing drive %s to ide-scsi"
2939 " emulation.\n", drive
->name
);
2942 tape
= kzalloc(sizeof(idetape_tape_t
), GFP_KERNEL
);
2944 printk(KERN_ERR
"ide-tape: %s: Can't allocate a tape struct\n",
2949 g
= alloc_disk(1 << PARTN_BITS
);
2953 ide_init_disk(g
, drive
);
2955 ide_proc_register_driver(drive
, &idetape_driver
);
2957 kref_init(&tape
->kref
);
2959 tape
->drive
= drive
;
2960 tape
->driver
= &idetape_driver
;
2963 g
->private_data
= &tape
->driver
;
2965 drive
->driver_data
= tape
;
2967 mutex_lock(&idetape_ref_mutex
);
2968 for (minor
= 0; idetape_devs
[minor
]; minor
++)
2970 idetape_devs
[minor
] = tape
;
2971 mutex_unlock(&idetape_ref_mutex
);
2973 idetape_setup(drive
, tape
, minor
);
2975 device_create(idetape_sysfs_class
, &drive
->gendev
,
2976 MKDEV(IDETAPE_MAJOR
, minor
), "%s", tape
->name
);
2977 device_create(idetape_sysfs_class
, &drive
->gendev
,
2978 MKDEV(IDETAPE_MAJOR
, minor
+ 128), "n%s", tape
->name
);
2980 g
->fops
= &idetape_block_ops
;
2981 ide_register_region(g
);
2991 static void __exit
idetape_exit(void)
2993 driver_unregister(&idetape_driver
.gen_driver
);
2994 class_destroy(idetape_sysfs_class
);
2995 unregister_chrdev(IDETAPE_MAJOR
, "ht");
2998 static int __init
idetape_init(void)
3001 idetape_sysfs_class
= class_create(THIS_MODULE
, "ide_tape");
3002 if (IS_ERR(idetape_sysfs_class
)) {
3003 idetape_sysfs_class
= NULL
;
3004 printk(KERN_ERR
"Unable to create sysfs class for ide tapes\n");
3009 if (register_chrdev(IDETAPE_MAJOR
, "ht", &idetape_fops
)) {
3010 printk(KERN_ERR
"ide-tape: Failed to register chrdev"
3013 goto out_free_class
;
3016 error
= driver_register(&idetape_driver
.gen_driver
);
3018 goto out_free_driver
;
3023 driver_unregister(&idetape_driver
.gen_driver
);
3025 class_destroy(idetape_sysfs_class
);
3030 MODULE_ALIAS("ide:*m-tape*");
3031 module_init(idetape_init
);
3032 module_exit(idetape_exit
);
3033 MODULE_ALIAS_CHARDEV_MAJOR(IDETAPE_MAJOR
);
3034 MODULE_DESCRIPTION("ATAPI Streaming TAPE Driver");
3035 MODULE_LICENSE("GPL");