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 DRV_NAME "ide-tape"
20 #define IDETAPE_VERSION "1.20"
22 #include <linux/module.h>
23 #include <linux/types.h>
24 #include <linux/string.h>
25 #include <linux/kernel.h>
26 #include <linux/delay.h>
27 #include <linux/timer.h>
29 #include <linux/interrupt.h>
30 #include <linux/jiffies.h>
31 #include <linux/major.h>
32 #include <linux/errno.h>
33 #include <linux/genhd.h>
34 #include <linux/seq_file.h>
35 #include <linux/slab.h>
36 #include <linux/pci.h>
37 #include <linux/ide.h>
38 #include <linux/completion.h>
39 #include <linux/bitops.h>
40 #include <linux/mutex.h>
41 #include <scsi/scsi.h>
43 #include <asm/byteorder.h>
44 #include <linux/irq.h>
45 #include <linux/uaccess.h>
47 #include <asm/unaligned.h>
48 #include <linux/mtio.h>
50 /* define to see debug info */
51 #undef IDETAPE_DEBUG_LOG
53 #ifdef IDETAPE_DEBUG_LOG
54 #define ide_debug_log(lvl, fmt, args...) __ide_debug_log(lvl, fmt, ## args)
56 #define ide_debug_log(lvl, fmt, args...) do {} while (0)
59 /**************************** Tunable parameters *****************************/
61 * After each failed packet command we issue a request sense command and retry
62 * the packet command IDETAPE_MAX_PC_RETRIES times.
64 * Setting IDETAPE_MAX_PC_RETRIES to 0 will disable retries.
66 #define IDETAPE_MAX_PC_RETRIES 3
69 * The following parameter is used to select the point in the internal tape fifo
70 * in which we will start to refill the buffer. Decreasing the following
71 * parameter will improve the system's latency and interactive response, while
72 * using a high value might improve system throughput.
74 #define IDETAPE_FIFO_THRESHOLD 2
77 * DSC polling parameters.
79 * Polling for DSC (a single bit in the status register) is a very important
80 * function in ide-tape. There are two cases in which we poll for DSC:
82 * 1. Before a read/write packet command, to ensure that we can transfer data
83 * from/to the tape's data buffers, without causing an actual media access.
84 * In case the tape is not ready yet, we take out our request from the device
85 * request queue, so that ide.c could service requests from the other device
86 * on the same interface in the meantime.
88 * 2. After the successful initialization of a "media access packet command",
89 * which is a command that can take a long time to complete (the interval can
90 * range from several seconds to even an hour). Again, we postpone our request
91 * in the middle to free the bus for the other device. The polling frequency
92 * here should be lower than the read/write frequency since those media access
93 * commands are slow. We start from a "fast" frequency - IDETAPE_DSC_MA_FAST
94 * (1 second), and if we don't receive DSC after IDETAPE_DSC_MA_THRESHOLD
95 * (5 min), we switch it to a lower frequency - IDETAPE_DSC_MA_SLOW (1 min).
97 * We also set a timeout for the timer, in case something goes wrong. The
98 * timeout should be longer then the maximum execution time of a tape operation.
102 #define IDETAPE_DSC_RW_MIN 5*HZ/100 /* 50 msec */
103 #define IDETAPE_DSC_RW_MAX 40*HZ/100 /* 400 msec */
104 #define IDETAPE_DSC_RW_TIMEOUT 2*60*HZ /* 2 minutes */
105 #define IDETAPE_DSC_MA_FAST 2*HZ /* 2 seconds */
106 #define IDETAPE_DSC_MA_THRESHOLD 5*60*HZ /* 5 minutes */
107 #define IDETAPE_DSC_MA_SLOW 30*HZ /* 30 seconds */
108 #define IDETAPE_DSC_MA_TIMEOUT 2*60*60*HZ /* 2 hours */
110 /*************************** End of tunable parameters ***********************/
112 /* tape directions */
114 IDETAPE_DIR_NONE
= (1 << 0),
115 IDETAPE_DIR_READ
= (1 << 1),
116 IDETAPE_DIR_WRITE
= (1 << 2),
119 /* Tape door status */
120 #define DOOR_UNLOCKED 0
121 #define DOOR_LOCKED 1
122 #define DOOR_EXPLICITLY_LOCKED 2
124 /* Some defines for the SPACE command */
125 #define IDETAPE_SPACE_OVER_FILEMARK 1
126 #define IDETAPE_SPACE_TO_EOD 3
128 /* Some defines for the LOAD UNLOAD command */
129 #define IDETAPE_LU_LOAD_MASK 1
130 #define IDETAPE_LU_RETENSION_MASK 2
131 #define IDETAPE_LU_EOT_MASK 4
133 /* Structures related to the SELECT SENSE / MODE SENSE packet commands. */
134 #define IDETAPE_BLOCK_DESCRIPTOR 0
135 #define IDETAPE_CAPABILITIES_PAGE 0x2a
138 * Most of our global data which we need to save even as we leave the driver due
139 * to an interrupt or a timer event is stored in the struct defined below.
141 typedef struct ide_tape_obj
{
143 struct ide_driver
*driver
;
144 struct gendisk
*disk
;
147 /* used by REQ_IDETAPE_{READ,WRITE} requests */
148 struct ide_atapi_pc queued_pc
;
151 * DSC polling variables.
153 * While polling for DSC we use postponed_rq to postpone the current
154 * request so that ide.c will be able to service pending requests on the
155 * other device. Note that at most we will have only one DSC (usually
156 * data transfer) request in the device request queue.
160 /* The time in which we started polling for DSC */
161 unsigned long dsc_polling_start
;
162 /* Timer used to poll for dsc */
163 struct timer_list dsc_timer
;
164 /* Read/Write dsc polling frequency */
165 unsigned long best_dsc_rw_freq
;
166 unsigned long dsc_poll_freq
;
167 unsigned long dsc_timeout
;
169 /* Read position information */
172 unsigned int first_frame
;
174 /* Last error information */
175 u8 sense_key
, asc
, ascq
;
177 /* Character device operation */
181 /* Current character device data transfer direction */
184 /* tape block size, usually 512 or 1024 bytes */
185 unsigned short blk_size
;
188 /* Copy of the tape's Capabilities and Mechanical Page */
192 * Active data transfer request parameters.
194 * At most, there is only one ide-tape originated data transfer request
195 * in the device request queue. This allows ide.c to easily service
196 * requests from the other device when we postpone our active request.
199 /* Data buffer size chosen based on the tape's recommendation */
201 /* Staging buffer of buffer_size bytes */
203 /* The read/write cursor */
205 /* The number of valid bytes in buf */
208 /* Measures average tape speed */
209 unsigned long avg_time
;
213 /* the door is currently locked */
215 /* the tape hardware is write protected */
217 /* the tape is write protected (hardware or opened as read-only) */
221 static DEFINE_MUTEX(ide_tape_mutex
);
222 static DEFINE_MUTEX(idetape_ref_mutex
);
224 static DEFINE_MUTEX(idetape_chrdev_mutex
);
226 static struct class *idetape_sysfs_class
;
228 static void ide_tape_release(struct device
*);
230 static struct ide_tape_obj
*idetape_devs
[MAX_HWIFS
* MAX_DRIVES
];
232 static struct ide_tape_obj
*ide_tape_get(struct gendisk
*disk
, bool cdev
,
235 struct ide_tape_obj
*tape
= NULL
;
237 mutex_lock(&idetape_ref_mutex
);
240 tape
= idetape_devs
[i
];
242 tape
= ide_drv_g(disk
, ide_tape_obj
);
245 if (ide_device_get(tape
->drive
))
248 get_device(&tape
->dev
);
251 mutex_unlock(&idetape_ref_mutex
);
255 static void ide_tape_put(struct ide_tape_obj
*tape
)
257 ide_drive_t
*drive
= tape
->drive
;
259 mutex_lock(&idetape_ref_mutex
);
260 put_device(&tape
->dev
);
261 ide_device_put(drive
);
262 mutex_unlock(&idetape_ref_mutex
);
266 * called on each failed packet command retry to analyze the request sense. We
267 * currently do not utilize this information.
269 static void idetape_analyze_error(ide_drive_t
*drive
)
271 idetape_tape_t
*tape
= drive
->driver_data
;
272 struct ide_atapi_pc
*pc
= drive
->failed_pc
;
273 struct request
*rq
= drive
->hwif
->rq
;
274 u8
*sense
= bio_data(rq
->bio
);
276 tape
->sense_key
= sense
[2] & 0xF;
277 tape
->asc
= sense
[12];
278 tape
->ascq
= sense
[13];
280 ide_debug_log(IDE_DBG_FUNC
,
281 "cmd: 0x%x, sense key = %x, asc = %x, ascq = %x",
282 rq
->cmd
[0], tape
->sense_key
, tape
->asc
, tape
->ascq
);
284 /* correct remaining bytes to transfer */
285 if (pc
->flags
& PC_FLAG_DMA_ERROR
)
286 rq
->resid_len
= tape
->blk_size
* get_unaligned_be32(&sense
[3]);
289 * If error was the result of a zero-length read or write command,
290 * with sense key=5, asc=0x22, ascq=0, let it slide. Some drives
291 * (i.e. Seagate STT3401A Travan) don't support 0-length read/writes.
293 if ((pc
->c
[0] == READ_6
|| pc
->c
[0] == WRITE_6
)
295 && pc
->c
[4] == 0 && pc
->c
[3] == 0 && pc
->c
[2] == 0) {
296 if (tape
->sense_key
== 5) {
297 /* don't report an error, everything's ok */
299 /* don't retry read/write */
300 pc
->flags
|= PC_FLAG_ABORT
;
303 if (pc
->c
[0] == READ_6
&& (sense
[2] & 0x80)) {
304 pc
->error
= IDE_DRV_ERROR_FILEMARK
;
305 pc
->flags
|= PC_FLAG_ABORT
;
307 if (pc
->c
[0] == WRITE_6
) {
308 if ((sense
[2] & 0x40) || (tape
->sense_key
== 0xd
309 && tape
->asc
== 0x0 && tape
->ascq
== 0x2)) {
310 pc
->error
= IDE_DRV_ERROR_EOD
;
311 pc
->flags
|= PC_FLAG_ABORT
;
314 if (pc
->c
[0] == READ_6
|| pc
->c
[0] == WRITE_6
) {
315 if (tape
->sense_key
== 8) {
316 pc
->error
= IDE_DRV_ERROR_EOD
;
317 pc
->flags
|= PC_FLAG_ABORT
;
319 if (!(pc
->flags
& PC_FLAG_ABORT
) &&
320 (blk_rq_bytes(rq
) - rq
->resid_len
))
321 pc
->retries
= IDETAPE_MAX_PC_RETRIES
+ 1;
325 static void ide_tape_handle_dsc(ide_drive_t
*);
327 static int ide_tape_callback(ide_drive_t
*drive
, int dsc
)
329 idetape_tape_t
*tape
= drive
->driver_data
;
330 struct ide_atapi_pc
*pc
= drive
->pc
;
331 struct request
*rq
= drive
->hwif
->rq
;
332 int uptodate
= pc
->error
? 0 : 1;
333 int err
= uptodate
? 0 : IDE_DRV_ERROR_GENERAL
;
335 ide_debug_log(IDE_DBG_FUNC
, "cmd: 0x%x, dsc: %d, err: %d", rq
->cmd
[0],
339 ide_tape_handle_dsc(drive
);
341 if (drive
->failed_pc
== pc
)
342 drive
->failed_pc
= NULL
;
344 if (pc
->c
[0] == REQUEST_SENSE
) {
346 idetape_analyze_error(drive
);
348 printk(KERN_ERR
"ide-tape: Error in REQUEST SENSE "
349 "itself - Aborting request!\n");
350 } else if (pc
->c
[0] == READ_6
|| pc
->c
[0] == WRITE_6
) {
351 unsigned int blocks
=
352 (blk_rq_bytes(rq
) - rq
->resid_len
) / tape
->blk_size
;
354 tape
->avg_size
+= blocks
* tape
->blk_size
;
356 if (time_after_eq(jiffies
, tape
->avg_time
+ HZ
)) {
357 tape
->avg_speed
= tape
->avg_size
* HZ
/
358 (jiffies
- tape
->avg_time
) / 1024;
360 tape
->avg_time
= jiffies
;
363 tape
->first_frame
+= blocks
;
376 * Postpone the current request so that ide.c will be able to service requests
377 * from another device on the same port while we are polling for DSC.
379 static void ide_tape_stall_queue(ide_drive_t
*drive
)
381 idetape_tape_t
*tape
= drive
->driver_data
;
383 ide_debug_log(IDE_DBG_FUNC
, "cmd: 0x%x, dsc_poll_freq: %lu",
384 drive
->hwif
->rq
->cmd
[0], tape
->dsc_poll_freq
);
386 tape
->postponed_rq
= true;
388 ide_stall_queue(drive
, tape
->dsc_poll_freq
);
391 static void ide_tape_handle_dsc(ide_drive_t
*drive
)
393 idetape_tape_t
*tape
= drive
->driver_data
;
395 /* Media access command */
396 tape
->dsc_polling_start
= jiffies
;
397 tape
->dsc_poll_freq
= IDETAPE_DSC_MA_FAST
;
398 tape
->dsc_timeout
= jiffies
+ IDETAPE_DSC_MA_TIMEOUT
;
399 /* Allow ide.c to handle other requests */
400 ide_tape_stall_queue(drive
);
404 * Packet Command Interface
406 * The current Packet Command is available in drive->pc, and will not change
407 * until we finish handling it. Each packet command is associated with a
408 * callback function that will be called when the command is finished.
410 * The handling will be done in three stages:
412 * 1. ide_tape_issue_pc will send the packet command to the drive, and will set
413 * the interrupt handler to ide_pc_intr.
415 * 2. On each interrupt, ide_pc_intr will be called. This step will be
416 * repeated until the device signals us that no more interrupts will be issued.
418 * 3. ATAPI Tape media access commands have immediate status with a delayed
419 * process. In case of a successful initiation of a media access packet command,
420 * the DSC bit will be set when the actual execution of the command is finished.
421 * Since the tape drive will not issue an interrupt, we have to poll for this
422 * event. In this case, we define the request as "low priority request" by
423 * setting rq_status to IDETAPE_RQ_POSTPONED, set a timer to poll for DSC and
426 * ide.c will then give higher priority to requests which originate from the
427 * other device, until will change rq_status to RQ_ACTIVE.
429 * 4. When the packet command is finished, it will be checked for errors.
431 * 5. In case an error was found, we queue a request sense packet command in
432 * front of the request queue and retry the operation up to
433 * IDETAPE_MAX_PC_RETRIES times.
435 * 6. In case no error was found, or we decided to give up and not to retry
436 * again, the callback function will be called and then we will handle the next
440 static ide_startstop_t
ide_tape_issue_pc(ide_drive_t
*drive
,
442 struct ide_atapi_pc
*pc
)
444 idetape_tape_t
*tape
= drive
->driver_data
;
445 struct request
*rq
= drive
->hwif
->rq
;
447 if (drive
->failed_pc
== NULL
&& pc
->c
[0] != REQUEST_SENSE
)
448 drive
->failed_pc
= pc
;
450 /* Set the current packet command */
453 if (pc
->retries
> IDETAPE_MAX_PC_RETRIES
||
454 (pc
->flags
& PC_FLAG_ABORT
)) {
457 * We will "abort" retrying a packet command in case legitimate
458 * error code was received (crossing a filemark, or end of the
459 * media, for example).
461 if (!(pc
->flags
& PC_FLAG_ABORT
)) {
462 if (!(pc
->c
[0] == TEST_UNIT_READY
&&
463 tape
->sense_key
== 2 && tape
->asc
== 4 &&
464 (tape
->ascq
== 1 || tape
->ascq
== 8))) {
465 printk(KERN_ERR
"ide-tape: %s: I/O error, "
466 "pc = %2x, key = %2x, "
467 "asc = %2x, ascq = %2x\n",
468 tape
->name
, pc
->c
[0],
469 tape
->sense_key
, tape
->asc
,
473 pc
->error
= IDE_DRV_ERROR_GENERAL
;
476 drive
->failed_pc
= NULL
;
477 drive
->pc_callback(drive
, 0);
478 ide_complete_rq(drive
, -EIO
, blk_rq_bytes(rq
));
481 ide_debug_log(IDE_DBG_SENSE
, "retry #%d, cmd: 0x%02x", pc
->retries
,
486 return ide_issue_pc(drive
, cmd
);
489 /* A mode sense command is used to "sense" tape parameters. */
490 static void idetape_create_mode_sense_cmd(struct ide_atapi_pc
*pc
, u8 page_code
)
493 pc
->c
[0] = MODE_SENSE
;
494 if (page_code
!= IDETAPE_BLOCK_DESCRIPTOR
)
495 /* DBD = 1 - Don't return block descriptors */
497 pc
->c
[2] = page_code
;
499 * Changed pc->c[3] to 0 (255 will at best return unused info).
501 * For SCSI this byte is defined as subpage instead of high byte
502 * of length and some IDE drives seem to interpret it this way
503 * and return an error when 255 is used.
506 /* We will just discard data in that case */
508 if (page_code
== IDETAPE_BLOCK_DESCRIPTOR
)
510 else if (page_code
== IDETAPE_CAPABILITIES_PAGE
)
516 static ide_startstop_t
idetape_media_access_finished(ide_drive_t
*drive
)
518 ide_hwif_t
*hwif
= drive
->hwif
;
519 idetape_tape_t
*tape
= drive
->driver_data
;
520 struct ide_atapi_pc
*pc
= drive
->pc
;
523 stat
= hwif
->tp_ops
->read_status(hwif
);
525 if (stat
& ATA_DSC
) {
526 if (stat
& ATA_ERR
) {
528 if (pc
->c
[0] != TEST_UNIT_READY
)
529 printk(KERN_ERR
"ide-tape: %s: I/O error, ",
531 /* Retry operation */
537 pc
->error
= IDE_DRV_ERROR_GENERAL
;
538 drive
->failed_pc
= NULL
;
540 drive
->pc_callback(drive
, 0);
544 static void ide_tape_create_rw_cmd(idetape_tape_t
*tape
,
545 struct ide_atapi_pc
*pc
, struct request
*rq
,
548 unsigned int length
= blk_rq_sectors(rq
) / (tape
->blk_size
>> 9);
551 put_unaligned(cpu_to_be32(length
), (unsigned int *) &pc
->c
[1]);
554 if (blk_rq_bytes(rq
) == tape
->buffer_size
)
555 pc
->flags
|= PC_FLAG_DMA_OK
;
557 if (opcode
== READ_6
)
559 else if (opcode
== WRITE_6
) {
561 pc
->flags
|= PC_FLAG_WRITING
;
564 memcpy(rq
->cmd
, pc
->c
, 12);
567 static ide_startstop_t
idetape_do_request(ide_drive_t
*drive
,
568 struct request
*rq
, sector_t block
)
570 ide_hwif_t
*hwif
= drive
->hwif
;
571 idetape_tape_t
*tape
= drive
->driver_data
;
572 struct ide_atapi_pc
*pc
= NULL
;
576 ide_debug_log(IDE_DBG_RQ
, "cmd: 0x%x, sector: %llu, nr_sectors: %u",
577 rq
->cmd
[0], (unsigned long long)blk_rq_pos(rq
),
580 BUG_ON(!(rq
->cmd_type
== REQ_TYPE_SPECIAL
||
581 rq
->cmd_type
== REQ_TYPE_SENSE
));
583 /* Retry a failed packet command */
584 if (drive
->failed_pc
&& drive
->pc
->c
[0] == REQUEST_SENSE
) {
585 pc
= drive
->failed_pc
;
590 * If the tape is still busy, postpone our request and service
591 * the other device meanwhile.
593 stat
= hwif
->tp_ops
->read_status(hwif
);
595 if ((drive
->dev_flags
& IDE_DFLAG_DSC_OVERLAP
) == 0 &&
596 (rq
->cmd
[13] & REQ_IDETAPE_PC2
) == 0)
597 drive
->atapi_flags
|= IDE_AFLAG_IGNORE_DSC
;
599 if (drive
->dev_flags
& IDE_DFLAG_POST_RESET
) {
600 drive
->atapi_flags
|= IDE_AFLAG_IGNORE_DSC
;
601 drive
->dev_flags
&= ~IDE_DFLAG_POST_RESET
;
604 if (!(drive
->atapi_flags
& IDE_AFLAG_IGNORE_DSC
) &&
606 if (!tape
->postponed_rq
) {
607 tape
->dsc_polling_start
= jiffies
;
608 tape
->dsc_poll_freq
= tape
->best_dsc_rw_freq
;
609 tape
->dsc_timeout
= jiffies
+ IDETAPE_DSC_RW_TIMEOUT
;
610 } else if (time_after(jiffies
, tape
->dsc_timeout
)) {
611 printk(KERN_ERR
"ide-tape: %s: DSC timeout\n",
613 if (rq
->cmd
[13] & REQ_IDETAPE_PC2
) {
614 idetape_media_access_finished(drive
);
617 return ide_do_reset(drive
);
619 } else if (time_after(jiffies
,
620 tape
->dsc_polling_start
+
621 IDETAPE_DSC_MA_THRESHOLD
))
622 tape
->dsc_poll_freq
= IDETAPE_DSC_MA_SLOW
;
623 ide_tape_stall_queue(drive
);
626 drive
->atapi_flags
&= ~IDE_AFLAG_IGNORE_DSC
;
627 tape
->postponed_rq
= false;
630 if (rq
->cmd
[13] & REQ_IDETAPE_READ
) {
631 pc
= &tape
->queued_pc
;
632 ide_tape_create_rw_cmd(tape
, pc
, rq
, READ_6
);
635 if (rq
->cmd
[13] & REQ_IDETAPE_WRITE
) {
636 pc
= &tape
->queued_pc
;
637 ide_tape_create_rw_cmd(tape
, pc
, rq
, WRITE_6
);
640 if (rq
->cmd
[13] & REQ_IDETAPE_PC1
) {
641 pc
= (struct ide_atapi_pc
*)rq
->special
;
642 rq
->cmd
[13] &= ~(REQ_IDETAPE_PC1
);
643 rq
->cmd
[13] |= REQ_IDETAPE_PC2
;
646 if (rq
->cmd
[13] & REQ_IDETAPE_PC2
) {
647 idetape_media_access_finished(drive
);
653 /* prepare sense request for this command */
654 ide_prep_sense(drive
, rq
);
656 memset(&cmd
, 0, sizeof(cmd
));
659 cmd
.tf_flags
|= IDE_TFLAG_WRITE
;
663 ide_init_sg_cmd(&cmd
, blk_rq_bytes(rq
));
664 ide_map_sg(drive
, &cmd
);
666 return ide_tape_issue_pc(drive
, &cmd
, pc
);
670 * Write a filemark if write_filemark=1. Flush the device buffers without
671 * writing a filemark otherwise.
673 static void idetape_create_write_filemark_cmd(ide_drive_t
*drive
,
674 struct ide_atapi_pc
*pc
, int write_filemark
)
677 pc
->c
[0] = WRITE_FILEMARKS
;
678 pc
->c
[4] = write_filemark
;
679 pc
->flags
|= PC_FLAG_WAIT_FOR_DSC
;
682 static int idetape_wait_ready(ide_drive_t
*drive
, unsigned long timeout
)
684 idetape_tape_t
*tape
= drive
->driver_data
;
685 struct gendisk
*disk
= tape
->disk
;
686 int load_attempted
= 0;
688 /* Wait for the tape to become ready */
689 set_bit(ilog2(IDE_AFLAG_MEDIUM_PRESENT
), &drive
->atapi_flags
);
691 while (time_before(jiffies
, timeout
)) {
692 if (ide_do_test_unit_ready(drive
, disk
) == 0)
694 if ((tape
->sense_key
== 2 && tape
->asc
== 4 && tape
->ascq
== 2)
695 || (tape
->asc
== 0x3A)) {
699 ide_do_start_stop(drive
, disk
, IDETAPE_LU_LOAD_MASK
);
701 /* not about to be ready */
702 } else if (!(tape
->sense_key
== 2 && tape
->asc
== 4 &&
703 (tape
->ascq
== 1 || tape
->ascq
== 8)))
710 static int idetape_flush_tape_buffers(ide_drive_t
*drive
)
712 struct ide_tape_obj
*tape
= drive
->driver_data
;
713 struct ide_atapi_pc pc
;
716 idetape_create_write_filemark_cmd(drive
, &pc
, 0);
717 rc
= ide_queue_pc_tail(drive
, tape
->disk
, &pc
, NULL
, 0);
720 idetape_wait_ready(drive
, 60 * 5 * HZ
);
724 static int ide_tape_read_position(ide_drive_t
*drive
)
726 idetape_tape_t
*tape
= drive
->driver_data
;
727 struct ide_atapi_pc pc
;
730 ide_debug_log(IDE_DBG_FUNC
, "enter");
734 pc
.c
[0] = READ_POSITION
;
737 if (ide_queue_pc_tail(drive
, tape
->disk
, &pc
, buf
, pc
.req_xfer
))
741 ide_debug_log(IDE_DBG_FUNC
, "BOP - %s",
742 (buf
[0] & 0x80) ? "Yes" : "No");
743 ide_debug_log(IDE_DBG_FUNC
, "EOP - %s",
744 (buf
[0] & 0x40) ? "Yes" : "No");
747 printk(KERN_INFO
"ide-tape: Block location is unknown"
749 clear_bit(ilog2(IDE_AFLAG_ADDRESS_VALID
),
750 &drive
->atapi_flags
);
753 ide_debug_log(IDE_DBG_FUNC
, "Block Location: %u",
754 be32_to_cpup((__be32
*)&buf
[4]));
756 tape
->partition
= buf
[1];
757 tape
->first_frame
= be32_to_cpup((__be32
*)&buf
[4]);
758 set_bit(ilog2(IDE_AFLAG_ADDRESS_VALID
),
759 &drive
->atapi_flags
);
763 return tape
->first_frame
;
766 static void idetape_create_locate_cmd(ide_drive_t
*drive
,
767 struct ide_atapi_pc
*pc
,
768 unsigned int block
, u8 partition
, int skip
)
771 pc
->c
[0] = POSITION_TO_ELEMENT
;
773 put_unaligned(cpu_to_be32(block
), (unsigned int *) &pc
->c
[3]);
774 pc
->c
[8] = partition
;
775 pc
->flags
|= PC_FLAG_WAIT_FOR_DSC
;
778 static void __ide_tape_discard_merge_buffer(ide_drive_t
*drive
)
780 idetape_tape_t
*tape
= drive
->driver_data
;
782 if (tape
->chrdev_dir
!= IDETAPE_DIR_READ
)
785 clear_bit(ilog2(IDE_AFLAG_FILEMARK
), &drive
->atapi_flags
);
787 if (tape
->buf
!= NULL
) {
792 tape
->chrdev_dir
= IDETAPE_DIR_NONE
;
796 * Position the tape to the requested block using the LOCATE packet command.
797 * A READ POSITION command is then issued to check where we are positioned. Like
798 * all higher level operations, we queue the commands at the tail of the request
799 * queue and wait for their completion.
801 static int idetape_position_tape(ide_drive_t
*drive
, unsigned int block
,
802 u8 partition
, int skip
)
804 idetape_tape_t
*tape
= drive
->driver_data
;
805 struct gendisk
*disk
= tape
->disk
;
807 struct ide_atapi_pc pc
;
809 if (tape
->chrdev_dir
== IDETAPE_DIR_READ
)
810 __ide_tape_discard_merge_buffer(drive
);
811 idetape_wait_ready(drive
, 60 * 5 * HZ
);
812 idetape_create_locate_cmd(drive
, &pc
, block
, partition
, skip
);
813 ret
= ide_queue_pc_tail(drive
, disk
, &pc
, NULL
, 0);
817 ret
= ide_tape_read_position(drive
);
823 static void ide_tape_discard_merge_buffer(ide_drive_t
*drive
,
824 int restore_position
)
826 idetape_tape_t
*tape
= drive
->driver_data
;
829 __ide_tape_discard_merge_buffer(drive
);
830 if (restore_position
) {
831 position
= ide_tape_read_position(drive
);
832 seek
= position
> 0 ? position
: 0;
833 if (idetape_position_tape(drive
, seek
, 0, 0)) {
834 printk(KERN_INFO
"ide-tape: %s: position_tape failed in"
835 " %s\n", tape
->name
, __func__
);
842 * Generate a read/write request for the block device interface and wait for it
845 static int idetape_queue_rw_tail(ide_drive_t
*drive
, int cmd
, int size
)
847 idetape_tape_t
*tape
= drive
->driver_data
;
851 ide_debug_log(IDE_DBG_FUNC
, "cmd: 0x%x, size: %d", cmd
, size
);
853 BUG_ON(cmd
!= REQ_IDETAPE_READ
&& cmd
!= REQ_IDETAPE_WRITE
);
854 BUG_ON(size
< 0 || size
% tape
->blk_size
);
856 rq
= blk_get_request(drive
->queue
, READ
, __GFP_WAIT
);
857 rq
->cmd_type
= REQ_TYPE_SPECIAL
;
859 rq
->rq_disk
= tape
->disk
;
860 rq
->__sector
= tape
->first_frame
;
863 ret
= blk_rq_map_kern(drive
->queue
, rq
, tape
->buf
, size
,
869 blk_execute_rq(drive
->queue
, tape
->disk
, rq
, 0);
871 /* calculate the number of transferred bytes and update buffer state */
872 size
-= rq
->resid_len
;
873 tape
->cur
= tape
->buf
;
874 if (cmd
== REQ_IDETAPE_READ
)
880 if (rq
->errors
== IDE_DRV_ERROR_GENERAL
)
887 static void idetape_create_inquiry_cmd(struct ide_atapi_pc
*pc
)
895 static void idetape_create_rewind_cmd(ide_drive_t
*drive
,
896 struct ide_atapi_pc
*pc
)
899 pc
->c
[0] = REZERO_UNIT
;
900 pc
->flags
|= PC_FLAG_WAIT_FOR_DSC
;
903 static void idetape_create_erase_cmd(struct ide_atapi_pc
*pc
)
908 pc
->flags
|= PC_FLAG_WAIT_FOR_DSC
;
911 static void idetape_create_space_cmd(struct ide_atapi_pc
*pc
, int count
, u8 cmd
)
915 put_unaligned(cpu_to_be32(count
), (unsigned int *) &pc
->c
[1]);
917 pc
->flags
|= PC_FLAG_WAIT_FOR_DSC
;
920 static void ide_tape_flush_merge_buffer(ide_drive_t
*drive
)
922 idetape_tape_t
*tape
= drive
->driver_data
;
924 if (tape
->chrdev_dir
!= IDETAPE_DIR_WRITE
) {
925 printk(KERN_ERR
"ide-tape: bug: Trying to empty merge buffer"
926 " but we are not writing.\n");
930 size_t aligned
= roundup(tape
->valid
, tape
->blk_size
);
932 memset(tape
->cur
, 0, aligned
- tape
->valid
);
933 idetape_queue_rw_tail(drive
, REQ_IDETAPE_WRITE
, aligned
);
937 tape
->chrdev_dir
= IDETAPE_DIR_NONE
;
940 static int idetape_init_rw(ide_drive_t
*drive
, int dir
)
942 idetape_tape_t
*tape
= drive
->driver_data
;
945 BUG_ON(dir
!= IDETAPE_DIR_READ
&& dir
!= IDETAPE_DIR_WRITE
);
947 if (tape
->chrdev_dir
== dir
)
950 if (tape
->chrdev_dir
== IDETAPE_DIR_READ
)
951 ide_tape_discard_merge_buffer(drive
, 1);
952 else if (tape
->chrdev_dir
== IDETAPE_DIR_WRITE
) {
953 ide_tape_flush_merge_buffer(drive
);
954 idetape_flush_tape_buffers(drive
);
957 if (tape
->buf
|| tape
->valid
) {
958 printk(KERN_ERR
"ide-tape: valid should be 0 now\n");
962 tape
->buf
= kmalloc(tape
->buffer_size
, GFP_KERNEL
);
965 tape
->chrdev_dir
= dir
;
966 tape
->cur
= tape
->buf
;
969 * Issue a 0 rw command to ensure that DSC handshake is
970 * switched from completion mode to buffer available mode. No
971 * point in issuing this if DSC overlap isn't supported, some
972 * drives (Seagate STT3401A) will return an error.
974 if (drive
->dev_flags
& IDE_DFLAG_DSC_OVERLAP
) {
975 int cmd
= dir
== IDETAPE_DIR_READ
? REQ_IDETAPE_READ
978 rc
= idetape_queue_rw_tail(drive
, cmd
, 0);
982 tape
->chrdev_dir
= IDETAPE_DIR_NONE
;
990 static void idetape_pad_zeros(ide_drive_t
*drive
, int bcount
)
992 idetape_tape_t
*tape
= drive
->driver_data
;
994 memset(tape
->buf
, 0, tape
->buffer_size
);
997 unsigned int count
= min(tape
->buffer_size
, bcount
);
999 idetape_queue_rw_tail(drive
, REQ_IDETAPE_WRITE
, count
);
1005 * Rewinds the tape to the Beginning Of the current Partition (BOP). We
1006 * currently support only one partition.
1008 static int idetape_rewind_tape(ide_drive_t
*drive
)
1010 struct ide_tape_obj
*tape
= drive
->driver_data
;
1011 struct gendisk
*disk
= tape
->disk
;
1012 struct ide_atapi_pc pc
;
1015 ide_debug_log(IDE_DBG_FUNC
, "enter");
1017 idetape_create_rewind_cmd(drive
, &pc
);
1018 ret
= ide_queue_pc_tail(drive
, disk
, &pc
, NULL
, 0);
1022 ret
= ide_tape_read_position(drive
);
1028 /* mtio.h compatible commands should be issued to the chrdev interface. */
1029 static int idetape_blkdev_ioctl(ide_drive_t
*drive
, unsigned int cmd
,
1032 idetape_tape_t
*tape
= drive
->driver_data
;
1033 void __user
*argp
= (void __user
*)arg
;
1035 struct idetape_config
{
1036 int dsc_rw_frequency
;
1037 int dsc_media_access_frequency
;
1041 ide_debug_log(IDE_DBG_FUNC
, "cmd: 0x%04x", cmd
);
1045 if (copy_from_user(&config
, argp
, sizeof(config
)))
1047 tape
->best_dsc_rw_freq
= config
.dsc_rw_frequency
;
1050 memset(&config
, 0, sizeof(config
));
1051 config
.dsc_rw_frequency
= (int) tape
->best_dsc_rw_freq
;
1052 config
.nr_stages
= 1;
1053 if (copy_to_user(argp
, &config
, sizeof(config
)))
1062 static int idetape_space_over_filemarks(ide_drive_t
*drive
, short mt_op
,
1065 idetape_tape_t
*tape
= drive
->driver_data
;
1066 struct gendisk
*disk
= tape
->disk
;
1067 struct ide_atapi_pc pc
;
1068 int retval
, count
= 0;
1069 int sprev
= !!(tape
->caps
[4] & 0x20);
1072 ide_debug_log(IDE_DBG_FUNC
, "mt_op: %d, mt_count: %d", mt_op
, mt_count
);
1076 if (MTBSF
== mt_op
|| MTBSFM
== mt_op
) {
1079 mt_count
= -mt_count
;
1082 if (tape
->chrdev_dir
== IDETAPE_DIR_READ
) {
1084 if (test_and_clear_bit(ilog2(IDE_AFLAG_FILEMARK
),
1085 &drive
->atapi_flags
))
1087 ide_tape_discard_merge_buffer(drive
, 0);
1093 idetape_create_space_cmd(&pc
, mt_count
- count
,
1094 IDETAPE_SPACE_OVER_FILEMARK
);
1095 return ide_queue_pc_tail(drive
, disk
, &pc
, NULL
, 0);
1100 retval
= idetape_space_over_filemarks(drive
, MTFSF
,
1104 count
= (MTBSFM
== mt_op
? 1 : -1);
1105 return idetape_space_over_filemarks(drive
, MTFSF
, count
);
1107 printk(KERN_ERR
"ide-tape: MTIO operation %d not supported\n",
1114 * Our character device read / write functions.
1116 * The tape is optimized to maximize throughput when it is transferring an
1117 * integral number of the "continuous transfer limit", which is a parameter of
1118 * the specific tape (26kB on my particular tape, 32kB for Onstream).
1120 * As of version 1.3 of the driver, the character device provides an abstract
1121 * continuous view of the media - any mix of block sizes (even 1 byte) on the
1122 * same backup/restore procedure is supported. The driver will internally
1123 * convert the requests to the recommended transfer unit, so that an unmatch
1124 * between the user's block size to the recommended size will only result in a
1125 * (slightly) increased driver overhead, but will no longer hit performance.
1126 * This is not applicable to Onstream.
1128 static ssize_t
idetape_chrdev_read(struct file
*file
, char __user
*buf
,
1129 size_t count
, loff_t
*ppos
)
1131 struct ide_tape_obj
*tape
= file
->private_data
;
1132 ide_drive_t
*drive
= tape
->drive
;
1137 ide_debug_log(IDE_DBG_FUNC
, "count %Zd", count
);
1139 if (tape
->chrdev_dir
!= IDETAPE_DIR_READ
) {
1140 if (test_bit(ilog2(IDE_AFLAG_DETECT_BS
), &drive
->atapi_flags
))
1141 if (count
> tape
->blk_size
&&
1142 (count
% tape
->blk_size
) == 0)
1143 tape
->user_bs_factor
= count
/ tape
->blk_size
;
1146 rc
= idetape_init_rw(drive
, IDETAPE_DIR_READ
);
1150 while (done
< count
) {
1153 /* refill if staging buffer is empty */
1155 /* If we are at a filemark, nothing more to read */
1156 if (test_bit(ilog2(IDE_AFLAG_FILEMARK
),
1157 &drive
->atapi_flags
))
1160 if (idetape_queue_rw_tail(drive
, REQ_IDETAPE_READ
,
1161 tape
->buffer_size
) <= 0)
1166 todo
= min_t(size_t, count
- done
, tape
->valid
);
1167 if (copy_to_user(buf
+ done
, tape
->cur
, todo
))
1171 tape
->valid
-= todo
;
1175 if (!done
&& test_bit(ilog2(IDE_AFLAG_FILEMARK
), &drive
->atapi_flags
)) {
1176 idetape_space_over_filemarks(drive
, MTFSF
, 1);
1180 return ret
? ret
: done
;
1183 static ssize_t
idetape_chrdev_write(struct file
*file
, const char __user
*buf
,
1184 size_t count
, loff_t
*ppos
)
1186 struct ide_tape_obj
*tape
= file
->private_data
;
1187 ide_drive_t
*drive
= tape
->drive
;
1192 /* The drive is write protected. */
1193 if (tape
->write_prot
)
1196 ide_debug_log(IDE_DBG_FUNC
, "count %Zd", count
);
1198 /* Initialize write operation */
1199 rc
= idetape_init_rw(drive
, IDETAPE_DIR_WRITE
);
1203 while (done
< count
) {
1206 /* flush if staging buffer is full */
1207 if (tape
->valid
== tape
->buffer_size
&&
1208 idetape_queue_rw_tail(drive
, REQ_IDETAPE_WRITE
,
1209 tape
->buffer_size
) <= 0)
1213 todo
= min_t(size_t, count
- done
,
1214 tape
->buffer_size
- tape
->valid
);
1215 if (copy_from_user(tape
->cur
, buf
+ done
, todo
))
1219 tape
->valid
+= todo
;
1223 return ret
? ret
: done
;
1226 static int idetape_write_filemark(ide_drive_t
*drive
)
1228 struct ide_tape_obj
*tape
= drive
->driver_data
;
1229 struct ide_atapi_pc pc
;
1231 /* Write a filemark */
1232 idetape_create_write_filemark_cmd(drive
, &pc
, 1);
1233 if (ide_queue_pc_tail(drive
, tape
->disk
, &pc
, NULL
, 0)) {
1234 printk(KERN_ERR
"ide-tape: Couldn't write a filemark\n");
1241 * Called from idetape_chrdev_ioctl when the general mtio MTIOCTOP ioctl is
1244 * Note: MTBSF and MTBSFM are not supported when the tape doesn't support
1245 * spacing over filemarks in the reverse direction. In this case, MTFSFM is also
1246 * usually not supported.
1248 * The following commands are currently not supported:
1250 * MTFSS, MTBSS, MTWSM, MTSETDENSITY, MTSETDRVBUFFER, MT_ST_BOOLEANS,
1251 * MT_ST_WRITE_THRESHOLD.
1253 static int idetape_mtioctop(ide_drive_t
*drive
, short mt_op
, int mt_count
)
1255 idetape_tape_t
*tape
= drive
->driver_data
;
1256 struct gendisk
*disk
= tape
->disk
;
1257 struct ide_atapi_pc pc
;
1260 ide_debug_log(IDE_DBG_FUNC
, "MTIOCTOP ioctl: mt_op: %d, mt_count: %d",
1270 return idetape_space_over_filemarks(drive
, mt_op
, mt_count
);
1277 if (tape
->write_prot
)
1279 ide_tape_discard_merge_buffer(drive
, 1);
1280 for (i
= 0; i
< mt_count
; i
++) {
1281 retval
= idetape_write_filemark(drive
);
1287 ide_tape_discard_merge_buffer(drive
, 0);
1288 if (idetape_rewind_tape(drive
))
1292 ide_tape_discard_merge_buffer(drive
, 0);
1293 return ide_do_start_stop(drive
, disk
, IDETAPE_LU_LOAD_MASK
);
1297 * If door is locked, attempt to unlock before
1298 * attempting to eject.
1300 if (tape
->door_locked
) {
1301 if (!ide_set_media_lock(drive
, disk
, 0))
1302 tape
->door_locked
= DOOR_UNLOCKED
;
1304 ide_tape_discard_merge_buffer(drive
, 0);
1305 retval
= ide_do_start_stop(drive
, disk
, !IDETAPE_LU_LOAD_MASK
);
1307 clear_bit(ilog2(IDE_AFLAG_MEDIUM_PRESENT
),
1308 &drive
->atapi_flags
);
1311 ide_tape_discard_merge_buffer(drive
, 0);
1312 return idetape_flush_tape_buffers(drive
);
1314 ide_tape_discard_merge_buffer(drive
, 0);
1315 return ide_do_start_stop(drive
, disk
,
1316 IDETAPE_LU_RETENSION_MASK
| IDETAPE_LU_LOAD_MASK
);
1318 idetape_create_space_cmd(&pc
, 0, IDETAPE_SPACE_TO_EOD
);
1319 return ide_queue_pc_tail(drive
, disk
, &pc
, NULL
, 0);
1321 (void)idetape_rewind_tape(drive
);
1322 idetape_create_erase_cmd(&pc
);
1323 return ide_queue_pc_tail(drive
, disk
, &pc
, NULL
, 0);
1326 if (mt_count
< tape
->blk_size
||
1327 mt_count
% tape
->blk_size
)
1329 tape
->user_bs_factor
= mt_count
/ tape
->blk_size
;
1330 clear_bit(ilog2(IDE_AFLAG_DETECT_BS
),
1331 &drive
->atapi_flags
);
1333 set_bit(ilog2(IDE_AFLAG_DETECT_BS
),
1334 &drive
->atapi_flags
);
1337 ide_tape_discard_merge_buffer(drive
, 0);
1338 return idetape_position_tape(drive
,
1339 mt_count
* tape
->user_bs_factor
, tape
->partition
, 0);
1341 ide_tape_discard_merge_buffer(drive
, 0);
1342 return idetape_position_tape(drive
, 0, mt_count
, 0);
1346 retval
= ide_set_media_lock(drive
, disk
, 1);
1349 tape
->door_locked
= DOOR_EXPLICITLY_LOCKED
;
1352 retval
= ide_set_media_lock(drive
, disk
, 0);
1355 tape
->door_locked
= DOOR_UNLOCKED
;
1358 printk(KERN_ERR
"ide-tape: MTIO operation %d not supported\n",
1365 * Our character device ioctls. General mtio.h magnetic io commands are
1366 * supported here, and not in the corresponding block interface. Our own
1367 * ide-tape ioctls are supported on both interfaces.
1369 static long do_idetape_chrdev_ioctl(struct file
*file
,
1370 unsigned int cmd
, unsigned long arg
)
1372 struct ide_tape_obj
*tape
= file
->private_data
;
1373 ide_drive_t
*drive
= tape
->drive
;
1377 int block_offset
= 0, position
= tape
->first_frame
;
1378 void __user
*argp
= (void __user
*)arg
;
1380 ide_debug_log(IDE_DBG_FUNC
, "cmd: 0x%x", cmd
);
1382 if (tape
->chrdev_dir
== IDETAPE_DIR_WRITE
) {
1383 ide_tape_flush_merge_buffer(drive
);
1384 idetape_flush_tape_buffers(drive
);
1386 if (cmd
== MTIOCGET
|| cmd
== MTIOCPOS
) {
1387 block_offset
= tape
->valid
/
1388 (tape
->blk_size
* tape
->user_bs_factor
);
1389 position
= ide_tape_read_position(drive
);
1395 if (copy_from_user(&mtop
, argp
, sizeof(struct mtop
)))
1397 return idetape_mtioctop(drive
, mtop
.mt_op
, mtop
.mt_count
);
1399 memset(&mtget
, 0, sizeof(struct mtget
));
1400 mtget
.mt_type
= MT_ISSCSI2
;
1401 mtget
.mt_blkno
= position
/ tape
->user_bs_factor
- block_offset
;
1403 ((tape
->blk_size
* tape
->user_bs_factor
)
1404 << MT_ST_BLKSIZE_SHIFT
) & MT_ST_BLKSIZE_MASK
;
1406 if (tape
->drv_write_prot
)
1407 mtget
.mt_gstat
|= GMT_WR_PROT(0xffffffff);
1409 if (copy_to_user(argp
, &mtget
, sizeof(struct mtget
)))
1413 mtpos
.mt_blkno
= position
/ tape
->user_bs_factor
- block_offset
;
1414 if (copy_to_user(argp
, &mtpos
, sizeof(struct mtpos
)))
1418 if (tape
->chrdev_dir
== IDETAPE_DIR_READ
)
1419 ide_tape_discard_merge_buffer(drive
, 1);
1420 return idetape_blkdev_ioctl(drive
, cmd
, arg
);
1424 static long idetape_chrdev_ioctl(struct file
*file
,
1425 unsigned int cmd
, unsigned long arg
)
1428 mutex_lock(&ide_tape_mutex
);
1429 ret
= do_idetape_chrdev_ioctl(file
, cmd
, arg
);
1430 mutex_unlock(&ide_tape_mutex
);
1435 * Do a mode sense page 0 with block descriptor and if it succeeds set the tape
1436 * block size with the reported value.
1438 static void ide_tape_get_bsize_from_bdesc(ide_drive_t
*drive
)
1440 idetape_tape_t
*tape
= drive
->driver_data
;
1441 struct ide_atapi_pc pc
;
1444 idetape_create_mode_sense_cmd(&pc
, IDETAPE_BLOCK_DESCRIPTOR
);
1445 if (ide_queue_pc_tail(drive
, tape
->disk
, &pc
, buf
, pc
.req_xfer
)) {
1446 printk(KERN_ERR
"ide-tape: Can't get block descriptor\n");
1447 if (tape
->blk_size
== 0) {
1448 printk(KERN_WARNING
"ide-tape: Cannot deal with zero "
1449 "block size, assuming 32k\n");
1450 tape
->blk_size
= 32768;
1454 tape
->blk_size
= (buf
[4 + 5] << 16) +
1457 tape
->drv_write_prot
= (buf
[2] & 0x80) >> 7;
1459 ide_debug_log(IDE_DBG_FUNC
, "blk_size: %d, write_prot: %d",
1460 tape
->blk_size
, tape
->drv_write_prot
);
1463 static int idetape_chrdev_open(struct inode
*inode
, struct file
*filp
)
1465 unsigned int minor
= iminor(inode
), i
= minor
& ~0xc0;
1467 idetape_tape_t
*tape
;
1470 if (i
>= MAX_HWIFS
* MAX_DRIVES
)
1473 mutex_lock(&idetape_chrdev_mutex
);
1475 tape
= ide_tape_get(NULL
, true, i
);
1477 mutex_unlock(&idetape_chrdev_mutex
);
1481 drive
= tape
->drive
;
1482 filp
->private_data
= tape
;
1484 ide_debug_log(IDE_DBG_FUNC
, "enter");
1487 * We really want to do nonseekable_open(inode, filp); here, but some
1488 * versions of tar incorrectly call lseek on tapes and bail out if that
1489 * fails. So we disallow pread() and pwrite(), but permit lseeks.
1491 filp
->f_mode
&= ~(FMODE_PREAD
| FMODE_PWRITE
);
1494 if (test_and_set_bit(ilog2(IDE_AFLAG_BUSY
), &drive
->atapi_flags
)) {
1499 retval
= idetape_wait_ready(drive
, 60 * HZ
);
1501 clear_bit(ilog2(IDE_AFLAG_BUSY
), &drive
->atapi_flags
);
1502 printk(KERN_ERR
"ide-tape: %s: drive not ready\n", tape
->name
);
1506 ide_tape_read_position(drive
);
1507 if (!test_bit(ilog2(IDE_AFLAG_ADDRESS_VALID
), &drive
->atapi_flags
))
1508 (void)idetape_rewind_tape(drive
);
1510 /* Read block size and write protect status from drive. */
1511 ide_tape_get_bsize_from_bdesc(drive
);
1513 /* Set write protect flag if device is opened as read-only. */
1514 if ((filp
->f_flags
& O_ACCMODE
) == O_RDONLY
)
1515 tape
->write_prot
= 1;
1517 tape
->write_prot
= tape
->drv_write_prot
;
1519 /* Make sure drive isn't write protected if user wants to write. */
1520 if (tape
->write_prot
) {
1521 if ((filp
->f_flags
& O_ACCMODE
) == O_WRONLY
||
1522 (filp
->f_flags
& O_ACCMODE
) == O_RDWR
) {
1523 clear_bit(ilog2(IDE_AFLAG_BUSY
), &drive
->atapi_flags
);
1529 /* Lock the tape drive door so user can't eject. */
1530 if (tape
->chrdev_dir
== IDETAPE_DIR_NONE
) {
1531 if (!ide_set_media_lock(drive
, tape
->disk
, 1)) {
1532 if (tape
->door_locked
!= DOOR_EXPLICITLY_LOCKED
)
1533 tape
->door_locked
= DOOR_LOCKED
;
1536 mutex_unlock(&idetape_chrdev_mutex
);
1543 mutex_unlock(&idetape_chrdev_mutex
);
1548 static void idetape_write_release(ide_drive_t
*drive
, unsigned int minor
)
1550 idetape_tape_t
*tape
= drive
->driver_data
;
1552 ide_tape_flush_merge_buffer(drive
);
1553 tape
->buf
= kmalloc(tape
->buffer_size
, GFP_KERNEL
);
1554 if (tape
->buf
!= NULL
) {
1555 idetape_pad_zeros(drive
, tape
->blk_size
*
1556 (tape
->user_bs_factor
- 1));
1560 idetape_write_filemark(drive
);
1561 idetape_flush_tape_buffers(drive
);
1562 idetape_flush_tape_buffers(drive
);
1565 static int idetape_chrdev_release(struct inode
*inode
, struct file
*filp
)
1567 struct ide_tape_obj
*tape
= filp
->private_data
;
1568 ide_drive_t
*drive
= tape
->drive
;
1569 unsigned int minor
= iminor(inode
);
1571 mutex_lock(&idetape_chrdev_mutex
);
1573 tape
= drive
->driver_data
;
1575 ide_debug_log(IDE_DBG_FUNC
, "enter");
1577 if (tape
->chrdev_dir
== IDETAPE_DIR_WRITE
)
1578 idetape_write_release(drive
, minor
);
1579 if (tape
->chrdev_dir
== IDETAPE_DIR_READ
) {
1581 ide_tape_discard_merge_buffer(drive
, 1);
1584 if (minor
< 128 && test_bit(ilog2(IDE_AFLAG_MEDIUM_PRESENT
),
1585 &drive
->atapi_flags
))
1586 (void) idetape_rewind_tape(drive
);
1588 if (tape
->chrdev_dir
== IDETAPE_DIR_NONE
) {
1589 if (tape
->door_locked
== DOOR_LOCKED
) {
1590 if (!ide_set_media_lock(drive
, tape
->disk
, 0))
1591 tape
->door_locked
= DOOR_UNLOCKED
;
1594 clear_bit(ilog2(IDE_AFLAG_BUSY
), &drive
->atapi_flags
);
1597 mutex_unlock(&idetape_chrdev_mutex
);
1602 static void idetape_get_inquiry_results(ide_drive_t
*drive
)
1604 idetape_tape_t
*tape
= drive
->driver_data
;
1605 struct ide_atapi_pc pc
;
1607 char fw_rev
[4], vendor_id
[8], product_id
[16];
1609 idetape_create_inquiry_cmd(&pc
);
1610 if (ide_queue_pc_tail(drive
, tape
->disk
, &pc
, pc_buf
, pc
.req_xfer
)) {
1611 printk(KERN_ERR
"ide-tape: %s: can't get INQUIRY results\n",
1615 memcpy(vendor_id
, &pc_buf
[8], 8);
1616 memcpy(product_id
, &pc_buf
[16], 16);
1617 memcpy(fw_rev
, &pc_buf
[32], 4);
1619 ide_fixstring(vendor_id
, 8, 0);
1620 ide_fixstring(product_id
, 16, 0);
1621 ide_fixstring(fw_rev
, 4, 0);
1623 printk(KERN_INFO
"ide-tape: %s <-> %s: %.8s %.16s rev %.4s\n",
1624 drive
->name
, tape
->name
, vendor_id
, product_id
, fw_rev
);
1628 * Ask the tape about its various parameters. In particular, we will adjust our
1629 * data transfer buffer size to the recommended value as returned by the tape.
1631 static void idetape_get_mode_sense_results(ide_drive_t
*drive
)
1633 idetape_tape_t
*tape
= drive
->driver_data
;
1634 struct ide_atapi_pc pc
;
1636 u8 speed
, max_speed
;
1638 idetape_create_mode_sense_cmd(&pc
, IDETAPE_CAPABILITIES_PAGE
);
1639 if (ide_queue_pc_tail(drive
, tape
->disk
, &pc
, buf
, pc
.req_xfer
)) {
1640 printk(KERN_ERR
"ide-tape: Can't get tape parameters - assuming"
1641 " some default values\n");
1642 tape
->blk_size
= 512;
1643 put_unaligned(52, (u16
*)&tape
->caps
[12]);
1644 put_unaligned(540, (u16
*)&tape
->caps
[14]);
1645 put_unaligned(6*52, (u16
*)&tape
->caps
[16]);
1648 caps
= buf
+ 4 + buf
[3];
1650 /* convert to host order and save for later use */
1651 speed
= be16_to_cpup((__be16
*)&caps
[14]);
1652 max_speed
= be16_to_cpup((__be16
*)&caps
[8]);
1654 *(u16
*)&caps
[8] = max_speed
;
1655 *(u16
*)&caps
[12] = be16_to_cpup((__be16
*)&caps
[12]);
1656 *(u16
*)&caps
[14] = speed
;
1657 *(u16
*)&caps
[16] = be16_to_cpup((__be16
*)&caps
[16]);
1660 printk(KERN_INFO
"ide-tape: %s: invalid tape speed "
1661 "(assuming 650KB/sec)\n", drive
->name
);
1662 *(u16
*)&caps
[14] = 650;
1665 printk(KERN_INFO
"ide-tape: %s: invalid max_speed "
1666 "(assuming 650KB/sec)\n", drive
->name
);
1667 *(u16
*)&caps
[8] = 650;
1670 memcpy(&tape
->caps
, caps
, 20);
1672 /* device lacks locking support according to capabilities page */
1673 if ((caps
[6] & 1) == 0)
1674 drive
->dev_flags
&= ~IDE_DFLAG_DOORLOCKING
;
1677 tape
->blk_size
= 512;
1678 else if (caps
[7] & 0x04)
1679 tape
->blk_size
= 1024;
1682 #ifdef CONFIG_IDE_PROC_FS
1683 #define ide_tape_devset_get(name, field) \
1684 static int get_##name(ide_drive_t *drive) \
1686 idetape_tape_t *tape = drive->driver_data; \
1687 return tape->field; \
1690 #define ide_tape_devset_set(name, field) \
1691 static int set_##name(ide_drive_t *drive, int arg) \
1693 idetape_tape_t *tape = drive->driver_data; \
1694 tape->field = arg; \
1698 #define ide_tape_devset_rw_field(_name, _field) \
1699 ide_tape_devset_get(_name, _field) \
1700 ide_tape_devset_set(_name, _field) \
1701 IDE_DEVSET(_name, DS_SYNC, get_##_name, set_##_name)
1703 #define ide_tape_devset_r_field(_name, _field) \
1704 ide_tape_devset_get(_name, _field) \
1705 IDE_DEVSET(_name, 0, get_##_name, NULL)
1707 static int mulf_tdsc(ide_drive_t
*drive
) { return 1000; }
1708 static int divf_tdsc(ide_drive_t
*drive
) { return HZ
; }
1709 static int divf_buffer(ide_drive_t
*drive
) { return 2; }
1710 static int divf_buffer_size(ide_drive_t
*drive
) { return 1024; }
1712 ide_devset_rw_flag(dsc_overlap
, IDE_DFLAG_DSC_OVERLAP
);
1714 ide_tape_devset_rw_field(tdsc
, best_dsc_rw_freq
);
1716 ide_tape_devset_r_field(avg_speed
, avg_speed
);
1717 ide_tape_devset_r_field(speed
, caps
[14]);
1718 ide_tape_devset_r_field(buffer
, caps
[16]);
1719 ide_tape_devset_r_field(buffer_size
, buffer_size
);
1721 static const struct ide_proc_devset idetape_settings
[] = {
1722 __IDE_PROC_DEVSET(avg_speed
, 0, 0xffff, NULL
, NULL
),
1723 __IDE_PROC_DEVSET(buffer
, 0, 0xffff, NULL
, divf_buffer
),
1724 __IDE_PROC_DEVSET(buffer_size
, 0, 0xffff, NULL
, divf_buffer_size
),
1725 __IDE_PROC_DEVSET(dsc_overlap
, 0, 1, NULL
, NULL
),
1726 __IDE_PROC_DEVSET(speed
, 0, 0xffff, NULL
, NULL
),
1727 __IDE_PROC_DEVSET(tdsc
, IDETAPE_DSC_RW_MIN
, IDETAPE_DSC_RW_MAX
,
1728 mulf_tdsc
, divf_tdsc
),
1734 * The function below is called to:
1736 * 1. Initialize our various state variables.
1737 * 2. Ask the tape for its capabilities.
1738 * 3. Allocate a buffer which will be used for data transfer. The buffer size
1739 * is chosen based on the recommendation which we received in step 2.
1741 * Note that at this point ide.c already assigned us an irq, so that we can
1742 * queue requests here and wait for their completion.
1744 static void idetape_setup(ide_drive_t
*drive
, idetape_tape_t
*tape
, int minor
)
1749 u16
*ctl
= (u16
*)&tape
->caps
[12];
1751 ide_debug_log(IDE_DBG_FUNC
, "minor: %d", minor
);
1753 drive
->pc_callback
= ide_tape_callback
;
1755 drive
->dev_flags
|= IDE_DFLAG_DSC_OVERLAP
;
1757 if (drive
->hwif
->host_flags
& IDE_HFLAG_NO_DSC
) {
1758 printk(KERN_INFO
"ide-tape: %s: disabling DSC overlap\n",
1760 drive
->dev_flags
&= ~IDE_DFLAG_DSC_OVERLAP
;
1763 /* Seagate Travan drives do not support DSC overlap. */
1764 if (strstr((char *)&drive
->id
[ATA_ID_PROD
], "Seagate STT3401"))
1765 drive
->dev_flags
&= ~IDE_DFLAG_DSC_OVERLAP
;
1767 tape
->minor
= minor
;
1768 tape
->name
[0] = 'h';
1769 tape
->name
[1] = 't';
1770 tape
->name
[2] = '0' + minor
;
1771 tape
->chrdev_dir
= IDETAPE_DIR_NONE
;
1773 idetape_get_inquiry_results(drive
);
1774 idetape_get_mode_sense_results(drive
);
1775 ide_tape_get_bsize_from_bdesc(drive
);
1776 tape
->user_bs_factor
= 1;
1777 tape
->buffer_size
= *ctl
* tape
->blk_size
;
1778 while (tape
->buffer_size
> 0xffff) {
1779 printk(KERN_NOTICE
"ide-tape: decreasing stage size\n");
1781 tape
->buffer_size
= *ctl
* tape
->blk_size
;
1783 buffer_size
= tape
->buffer_size
;
1785 /* select the "best" DSC read/write polling freq */
1786 speed
= max(*(u16
*)&tape
->caps
[14], *(u16
*)&tape
->caps
[8]);
1788 t
= (IDETAPE_FIFO_THRESHOLD
* tape
->buffer_size
* HZ
) / (speed
* 1000);
1791 * Ensure that the number we got makes sense; limit it within
1792 * IDETAPE_DSC_RW_MIN and IDETAPE_DSC_RW_MAX.
1794 tape
->best_dsc_rw_freq
= clamp_t(unsigned long, t
, IDETAPE_DSC_RW_MIN
,
1795 IDETAPE_DSC_RW_MAX
);
1796 printk(KERN_INFO
"ide-tape: %s <-> %s: %dKBps, %d*%dkB buffer, "
1798 drive
->name
, tape
->name
, *(u16
*)&tape
->caps
[14],
1799 (*(u16
*)&tape
->caps
[16] * 512) / tape
->buffer_size
,
1800 tape
->buffer_size
/ 1024,
1801 tape
->best_dsc_rw_freq
* 1000 / HZ
,
1802 (drive
->dev_flags
& IDE_DFLAG_USING_DMA
) ? ", DMA" : "");
1804 ide_proc_register_driver(drive
, tape
->driver
);
1807 static void ide_tape_remove(ide_drive_t
*drive
)
1809 idetape_tape_t
*tape
= drive
->driver_data
;
1811 ide_proc_unregister_driver(drive
, tape
->driver
);
1812 device_del(&tape
->dev
);
1813 ide_unregister_region(tape
->disk
);
1815 mutex_lock(&idetape_ref_mutex
);
1816 put_device(&tape
->dev
);
1817 mutex_unlock(&idetape_ref_mutex
);
1820 static void ide_tape_release(struct device
*dev
)
1822 struct ide_tape_obj
*tape
= to_ide_drv(dev
, ide_tape_obj
);
1823 ide_drive_t
*drive
= tape
->drive
;
1824 struct gendisk
*g
= tape
->disk
;
1826 BUG_ON(tape
->valid
);
1828 drive
->dev_flags
&= ~IDE_DFLAG_DSC_OVERLAP
;
1829 drive
->driver_data
= NULL
;
1830 device_destroy(idetape_sysfs_class
, MKDEV(IDETAPE_MAJOR
, tape
->minor
));
1831 device_destroy(idetape_sysfs_class
,
1832 MKDEV(IDETAPE_MAJOR
, tape
->minor
+ 128));
1833 idetape_devs
[tape
->minor
] = NULL
;
1834 g
->private_data
= NULL
;
1839 #ifdef CONFIG_IDE_PROC_FS
1840 static int idetape_name_proc_show(struct seq_file
*m
, void *v
)
1842 ide_drive_t
*drive
= (ide_drive_t
*) m
->private;
1843 idetape_tape_t
*tape
= drive
->driver_data
;
1845 seq_printf(m
, "%s\n", tape
->name
);
1849 static int idetape_name_proc_open(struct inode
*inode
, struct file
*file
)
1851 return single_open(file
, idetape_name_proc_show
, PDE(inode
)->data
);
1854 static const struct file_operations idetape_name_proc_fops
= {
1855 .owner
= THIS_MODULE
,
1856 .open
= idetape_name_proc_open
,
1858 .llseek
= seq_lseek
,
1859 .release
= single_release
,
1862 static ide_proc_entry_t idetape_proc
[] = {
1863 { "capacity", S_IFREG
|S_IRUGO
, &ide_capacity_proc_fops
},
1864 { "name", S_IFREG
|S_IRUGO
, &idetape_name_proc_fops
},
1868 static ide_proc_entry_t
*ide_tape_proc_entries(ide_drive_t
*drive
)
1870 return idetape_proc
;
1873 static const struct ide_proc_devset
*ide_tape_proc_devsets(ide_drive_t
*drive
)
1875 return idetape_settings
;
1879 static int ide_tape_probe(ide_drive_t
*);
1881 static struct ide_driver idetape_driver
= {
1883 .owner
= THIS_MODULE
,
1885 .bus
= &ide_bus_type
,
1887 .probe
= ide_tape_probe
,
1888 .remove
= ide_tape_remove
,
1889 .version
= IDETAPE_VERSION
,
1890 .do_request
= idetape_do_request
,
1891 #ifdef CONFIG_IDE_PROC_FS
1892 .proc_entries
= ide_tape_proc_entries
,
1893 .proc_devsets
= ide_tape_proc_devsets
,
1897 /* Our character device supporting functions, passed to register_chrdev. */
1898 static const struct file_operations idetape_fops
= {
1899 .owner
= THIS_MODULE
,
1900 .read
= idetape_chrdev_read
,
1901 .write
= idetape_chrdev_write
,
1902 .unlocked_ioctl
= idetape_chrdev_ioctl
,
1903 .open
= idetape_chrdev_open
,
1904 .release
= idetape_chrdev_release
,
1905 .llseek
= noop_llseek
,
1908 static int idetape_open(struct block_device
*bdev
, fmode_t mode
)
1910 struct ide_tape_obj
*tape
;
1912 mutex_lock(&ide_tape_mutex
);
1913 tape
= ide_tape_get(bdev
->bd_disk
, false, 0);
1914 mutex_unlock(&ide_tape_mutex
);
1922 static int idetape_release(struct gendisk
*disk
, fmode_t mode
)
1924 struct ide_tape_obj
*tape
= ide_drv_g(disk
, ide_tape_obj
);
1926 mutex_lock(&ide_tape_mutex
);
1928 mutex_unlock(&ide_tape_mutex
);
1933 static int idetape_ioctl(struct block_device
*bdev
, fmode_t mode
,
1934 unsigned int cmd
, unsigned long arg
)
1936 struct ide_tape_obj
*tape
= ide_drv_g(bdev
->bd_disk
, ide_tape_obj
);
1937 ide_drive_t
*drive
= tape
->drive
;
1940 mutex_lock(&ide_tape_mutex
);
1941 err
= generic_ide_ioctl(drive
, bdev
, cmd
, arg
);
1943 err
= idetape_blkdev_ioctl(drive
, cmd
, arg
);
1944 mutex_unlock(&ide_tape_mutex
);
1949 static const struct block_device_operations idetape_block_ops
= {
1950 .owner
= THIS_MODULE
,
1951 .open
= idetape_open
,
1952 .release
= idetape_release
,
1953 .ioctl
= idetape_ioctl
,
1956 static int ide_tape_probe(ide_drive_t
*drive
)
1958 idetape_tape_t
*tape
;
1962 ide_debug_log(IDE_DBG_FUNC
, "enter");
1964 if (!strstr(DRV_NAME
, drive
->driver_req
))
1967 if (drive
->media
!= ide_tape
)
1970 if ((drive
->dev_flags
& IDE_DFLAG_ID_READ
) &&
1971 ide_check_atapi_device(drive
, DRV_NAME
) == 0) {
1972 printk(KERN_ERR
"ide-tape: %s: not supported by this version of"
1973 " the driver\n", drive
->name
);
1976 tape
= kzalloc(sizeof(idetape_tape_t
), GFP_KERNEL
);
1978 printk(KERN_ERR
"ide-tape: %s: Can't allocate a tape struct\n",
1983 g
= alloc_disk(1 << PARTN_BITS
);
1987 ide_init_disk(g
, drive
);
1989 tape
->dev
.parent
= &drive
->gendev
;
1990 tape
->dev
.release
= ide_tape_release
;
1991 dev_set_name(&tape
->dev
, dev_name(&drive
->gendev
));
1993 if (device_register(&tape
->dev
))
1996 tape
->drive
= drive
;
1997 tape
->driver
= &idetape_driver
;
2000 g
->private_data
= &tape
->driver
;
2002 drive
->driver_data
= tape
;
2004 mutex_lock(&idetape_ref_mutex
);
2005 for (minor
= 0; idetape_devs
[minor
]; minor
++)
2007 idetape_devs
[minor
] = tape
;
2008 mutex_unlock(&idetape_ref_mutex
);
2010 idetape_setup(drive
, tape
, minor
);
2012 device_create(idetape_sysfs_class
, &drive
->gendev
,
2013 MKDEV(IDETAPE_MAJOR
, minor
), NULL
, "%s", tape
->name
);
2014 device_create(idetape_sysfs_class
, &drive
->gendev
,
2015 MKDEV(IDETAPE_MAJOR
, minor
+ 128), NULL
,
2018 g
->fops
= &idetape_block_ops
;
2019 ide_register_region(g
);
2031 static void __exit
idetape_exit(void)
2033 driver_unregister(&idetape_driver
.gen_driver
);
2034 class_destroy(idetape_sysfs_class
);
2035 unregister_chrdev(IDETAPE_MAJOR
, "ht");
2038 static int __init
idetape_init(void)
2041 idetape_sysfs_class
= class_create(THIS_MODULE
, "ide_tape");
2042 if (IS_ERR(idetape_sysfs_class
)) {
2043 idetape_sysfs_class
= NULL
;
2044 printk(KERN_ERR
"Unable to create sysfs class for ide tapes\n");
2049 if (register_chrdev(IDETAPE_MAJOR
, "ht", &idetape_fops
)) {
2050 printk(KERN_ERR
"ide-tape: Failed to register chrdev"
2053 goto out_free_class
;
2056 error
= driver_register(&idetape_driver
.gen_driver
);
2058 goto out_free_driver
;
2063 driver_unregister(&idetape_driver
.gen_driver
);
2065 class_destroy(idetape_sysfs_class
);
2070 MODULE_ALIAS("ide:*m-tape*");
2071 module_init(idetape_init
);
2072 module_exit(idetape_exit
);
2073 MODULE_ALIAS_CHARDEV_MAJOR(IDETAPE_MAJOR
);
2074 MODULE_DESCRIPTION("ATAPI Streaming TAPE Driver");
2075 MODULE_LICENSE("GPL");