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/uaccess.h>
46 #include <asm/unaligned.h>
47 #include <linux/mtio.h>
49 /* define to see debug info */
50 #undef IDETAPE_DEBUG_LOG
52 #ifdef IDETAPE_DEBUG_LOG
53 #define ide_debug_log(lvl, fmt, args...) __ide_debug_log(lvl, fmt, ## args)
55 #define ide_debug_log(lvl, fmt, args...) do {} while (0)
58 /**************************** Tunable parameters *****************************/
60 * After each failed packet command we issue a request sense command and retry
61 * the packet command IDETAPE_MAX_PC_RETRIES times.
63 * Setting IDETAPE_MAX_PC_RETRIES to 0 will disable retries.
65 #define IDETAPE_MAX_PC_RETRIES 3
68 * The following parameter is used to select the point in the internal tape fifo
69 * in which we will start to refill the buffer. Decreasing the following
70 * parameter will improve the system's latency and interactive response, while
71 * using a high value might improve system throughput.
73 #define IDETAPE_FIFO_THRESHOLD 2
76 * DSC polling parameters.
78 * Polling for DSC (a single bit in the status register) is a very important
79 * function in ide-tape. There are two cases in which we poll for DSC:
81 * 1. Before a read/write packet command, to ensure that we can transfer data
82 * from/to the tape's data buffers, without causing an actual media access.
83 * In case the tape is not ready yet, we take out our request from the device
84 * request queue, so that ide.c could service requests from the other device
85 * on the same interface in the meantime.
87 * 2. After the successful initialization of a "media access packet command",
88 * which is a command that can take a long time to complete (the interval can
89 * range from several seconds to even an hour). Again, we postpone our request
90 * in the middle to free the bus for the other device. The polling frequency
91 * here should be lower than the read/write frequency since those media access
92 * commands are slow. We start from a "fast" frequency - IDETAPE_DSC_MA_FAST
93 * (1 second), and if we don't receive DSC after IDETAPE_DSC_MA_THRESHOLD
94 * (5 min), we switch it to a lower frequency - IDETAPE_DSC_MA_SLOW (1 min).
96 * We also set a timeout for the timer, in case something goes wrong. The
97 * timeout should be longer then the maximum execution time of a tape operation.
101 #define IDETAPE_DSC_RW_MIN 5*HZ/100 /* 50 msec */
102 #define IDETAPE_DSC_RW_MAX 40*HZ/100 /* 400 msec */
103 #define IDETAPE_DSC_RW_TIMEOUT 2*60*HZ /* 2 minutes */
104 #define IDETAPE_DSC_MA_FAST 2*HZ /* 2 seconds */
105 #define IDETAPE_DSC_MA_THRESHOLD 5*60*HZ /* 5 minutes */
106 #define IDETAPE_DSC_MA_SLOW 30*HZ /* 30 seconds */
107 #define IDETAPE_DSC_MA_TIMEOUT 2*60*60*HZ /* 2 hours */
109 /*************************** End of tunable parameters ***********************/
111 /* tape directions */
113 IDETAPE_DIR_NONE
= (1 << 0),
114 IDETAPE_DIR_READ
= (1 << 1),
115 IDETAPE_DIR_WRITE
= (1 << 2),
118 /* Tape door status */
119 #define DOOR_UNLOCKED 0
120 #define DOOR_LOCKED 1
121 #define DOOR_EXPLICITLY_LOCKED 2
123 /* Some defines for the SPACE command */
124 #define IDETAPE_SPACE_OVER_FILEMARK 1
125 #define IDETAPE_SPACE_TO_EOD 3
127 /* Some defines for the LOAD UNLOAD command */
128 #define IDETAPE_LU_LOAD_MASK 1
129 #define IDETAPE_LU_RETENSION_MASK 2
130 #define IDETAPE_LU_EOT_MASK 4
132 /* Structures related to the SELECT SENSE / MODE SENSE packet commands. */
133 #define IDETAPE_BLOCK_DESCRIPTOR 0
134 #define IDETAPE_CAPABILITIES_PAGE 0x2a
137 * Most of our global data which we need to save even as we leave the driver due
138 * to an interrupt or a timer event is stored in the struct defined below.
140 typedef struct ide_tape_obj
{
142 struct ide_driver
*driver
;
143 struct gendisk
*disk
;
146 /* used by REQ_IDETAPE_{READ,WRITE} requests */
147 struct ide_atapi_pc queued_pc
;
150 * DSC polling variables.
152 * While polling for DSC we use postponed_rq to postpone the current
153 * request so that ide.c will be able to service pending requests on the
154 * other device. Note that at most we will have only one DSC (usually
155 * data transfer) request in the device request queue.
159 /* The time in which we started polling for DSC */
160 unsigned long dsc_polling_start
;
161 /* Timer used to poll for dsc */
162 struct timer_list dsc_timer
;
163 /* Read/Write dsc polling frequency */
164 unsigned long best_dsc_rw_freq
;
165 unsigned long dsc_poll_freq
;
166 unsigned long dsc_timeout
;
168 /* Read position information */
171 unsigned int first_frame
;
173 /* Last error information */
174 u8 sense_key
, asc
, ascq
;
176 /* Character device operation */
180 /* Current character device data transfer direction */
183 /* tape block size, usually 512 or 1024 bytes */
184 unsigned short blk_size
;
187 /* Copy of the tape's Capabilities and Mechanical Page */
191 * Active data transfer request parameters.
193 * At most, there is only one ide-tape originated data transfer request
194 * in the device request queue. This allows ide.c to easily service
195 * requests from the other device when we postpone our active request.
198 /* Data buffer size chosen based on the tape's recommendation */
200 /* Staging buffer of buffer_size bytes */
202 /* The read/write cursor */
204 /* The number of valid bytes in buf */
207 /* Measures average tape speed */
208 unsigned long avg_time
;
212 /* the door is currently locked */
214 /* the tape hardware is write protected */
216 /* the tape is write protected (hardware or opened as read-only) */
220 static DEFINE_MUTEX(ide_tape_mutex
);
221 static DEFINE_MUTEX(idetape_ref_mutex
);
223 static DEFINE_MUTEX(idetape_chrdev_mutex
);
225 static struct class *idetape_sysfs_class
;
227 static void ide_tape_release(struct device
*);
229 static struct ide_tape_obj
*idetape_devs
[MAX_HWIFS
* MAX_DRIVES
];
231 static struct ide_tape_obj
*ide_tape_get(struct gendisk
*disk
, bool cdev
,
234 struct ide_tape_obj
*tape
= NULL
;
236 mutex_lock(&idetape_ref_mutex
);
239 tape
= idetape_devs
[i
];
241 tape
= ide_drv_g(disk
, ide_tape_obj
);
244 if (ide_device_get(tape
->drive
))
247 get_device(&tape
->dev
);
250 mutex_unlock(&idetape_ref_mutex
);
254 static void ide_tape_put(struct ide_tape_obj
*tape
)
256 ide_drive_t
*drive
= tape
->drive
;
258 mutex_lock(&idetape_ref_mutex
);
259 put_device(&tape
->dev
);
260 ide_device_put(drive
);
261 mutex_unlock(&idetape_ref_mutex
);
265 * called on each failed packet command retry to analyze the request sense. We
266 * currently do not utilize this information.
268 static void idetape_analyze_error(ide_drive_t
*drive
)
270 idetape_tape_t
*tape
= drive
->driver_data
;
271 struct ide_atapi_pc
*pc
= drive
->failed_pc
;
272 struct request
*rq
= drive
->hwif
->rq
;
273 u8
*sense
= bio_data(rq
->bio
);
275 tape
->sense_key
= sense
[2] & 0xF;
276 tape
->asc
= sense
[12];
277 tape
->ascq
= sense
[13];
279 ide_debug_log(IDE_DBG_FUNC
,
280 "cmd: 0x%x, sense key = %x, asc = %x, ascq = %x",
281 rq
->cmd
[0], tape
->sense_key
, tape
->asc
, tape
->ascq
);
283 /* correct remaining bytes to transfer */
284 if (pc
->flags
& PC_FLAG_DMA_ERROR
)
285 rq
->resid_len
= tape
->blk_size
* get_unaligned_be32(&sense
[3]);
288 * If error was the result of a zero-length read or write command,
289 * with sense key=5, asc=0x22, ascq=0, let it slide. Some drives
290 * (i.e. Seagate STT3401A Travan) don't support 0-length read/writes.
292 if ((pc
->c
[0] == READ_6
|| pc
->c
[0] == WRITE_6
)
294 && pc
->c
[4] == 0 && pc
->c
[3] == 0 && pc
->c
[2] == 0) {
295 if (tape
->sense_key
== 5) {
296 /* don't report an error, everything's ok */
298 /* don't retry read/write */
299 pc
->flags
|= PC_FLAG_ABORT
;
302 if (pc
->c
[0] == READ_6
&& (sense
[2] & 0x80)) {
303 pc
->error
= IDE_DRV_ERROR_FILEMARK
;
304 pc
->flags
|= PC_FLAG_ABORT
;
306 if (pc
->c
[0] == WRITE_6
) {
307 if ((sense
[2] & 0x40) || (tape
->sense_key
== 0xd
308 && tape
->asc
== 0x0 && tape
->ascq
== 0x2)) {
309 pc
->error
= IDE_DRV_ERROR_EOD
;
310 pc
->flags
|= PC_FLAG_ABORT
;
313 if (pc
->c
[0] == READ_6
|| pc
->c
[0] == WRITE_6
) {
314 if (tape
->sense_key
== 8) {
315 pc
->error
= IDE_DRV_ERROR_EOD
;
316 pc
->flags
|= PC_FLAG_ABORT
;
318 if (!(pc
->flags
& PC_FLAG_ABORT
) &&
319 (blk_rq_bytes(rq
) - rq
->resid_len
))
320 pc
->retries
= IDETAPE_MAX_PC_RETRIES
+ 1;
324 static void ide_tape_handle_dsc(ide_drive_t
*);
326 static int ide_tape_callback(ide_drive_t
*drive
, int dsc
)
328 idetape_tape_t
*tape
= drive
->driver_data
;
329 struct ide_atapi_pc
*pc
= drive
->pc
;
330 struct request
*rq
= drive
->hwif
->rq
;
331 int uptodate
= pc
->error
? 0 : 1;
332 int err
= uptodate
? 0 : IDE_DRV_ERROR_GENERAL
;
334 ide_debug_log(IDE_DBG_FUNC
, "cmd: 0x%x, dsc: %d, err: %d", rq
->cmd
[0],
338 ide_tape_handle_dsc(drive
);
340 if (drive
->failed_pc
== pc
)
341 drive
->failed_pc
= NULL
;
343 if (pc
->c
[0] == REQUEST_SENSE
) {
345 idetape_analyze_error(drive
);
347 printk(KERN_ERR
"ide-tape: Error in REQUEST SENSE "
348 "itself - Aborting request!\n");
349 } else if (pc
->c
[0] == READ_6
|| pc
->c
[0] == WRITE_6
) {
350 unsigned int blocks
=
351 (blk_rq_bytes(rq
) - rq
->resid_len
) / tape
->blk_size
;
353 tape
->avg_size
+= blocks
* tape
->blk_size
;
355 if (time_after_eq(jiffies
, tape
->avg_time
+ HZ
)) {
356 tape
->avg_speed
= tape
->avg_size
* HZ
/
357 (jiffies
- tape
->avg_time
) / 1024;
359 tape
->avg_time
= jiffies
;
362 tape
->first_frame
+= blocks
;
375 * Postpone the current request so that ide.c will be able to service requests
376 * from another device on the same port while we are polling for DSC.
378 static void ide_tape_stall_queue(ide_drive_t
*drive
)
380 idetape_tape_t
*tape
= drive
->driver_data
;
382 ide_debug_log(IDE_DBG_FUNC
, "cmd: 0x%x, dsc_poll_freq: %lu",
383 drive
->hwif
->rq
->cmd
[0], tape
->dsc_poll_freq
);
385 tape
->postponed_rq
= true;
387 ide_stall_queue(drive
, tape
->dsc_poll_freq
);
390 static void ide_tape_handle_dsc(ide_drive_t
*drive
)
392 idetape_tape_t
*tape
= drive
->driver_data
;
394 /* Media access command */
395 tape
->dsc_polling_start
= jiffies
;
396 tape
->dsc_poll_freq
= IDETAPE_DSC_MA_FAST
;
397 tape
->dsc_timeout
= jiffies
+ IDETAPE_DSC_MA_TIMEOUT
;
398 /* Allow ide.c to handle other requests */
399 ide_tape_stall_queue(drive
);
403 * Packet Command Interface
405 * The current Packet Command is available in drive->pc, and will not change
406 * until we finish handling it. Each packet command is associated with a
407 * callback function that will be called when the command is finished.
409 * The handling will be done in three stages:
411 * 1. ide_tape_issue_pc will send the packet command to the drive, and will set
412 * the interrupt handler to ide_pc_intr.
414 * 2. On each interrupt, ide_pc_intr will be called. This step will be
415 * repeated until the device signals us that no more interrupts will be issued.
417 * 3. ATAPI Tape media access commands have immediate status with a delayed
418 * process. In case of a successful initiation of a media access packet command,
419 * the DSC bit will be set when the actual execution of the command is finished.
420 * Since the tape drive will not issue an interrupt, we have to poll for this
421 * event. In this case, we define the request as "low priority request" by
422 * setting rq_status to IDETAPE_RQ_POSTPONED, set a timer to poll for DSC and
425 * ide.c will then give higher priority to requests which originate from the
426 * other device, until will change rq_status to RQ_ACTIVE.
428 * 4. When the packet command is finished, it will be checked for errors.
430 * 5. In case an error was found, we queue a request sense packet command in
431 * front of the request queue and retry the operation up to
432 * IDETAPE_MAX_PC_RETRIES times.
434 * 6. In case no error was found, or we decided to give up and not to retry
435 * again, the callback function will be called and then we will handle the next
439 static ide_startstop_t
ide_tape_issue_pc(ide_drive_t
*drive
,
441 struct ide_atapi_pc
*pc
)
443 idetape_tape_t
*tape
= drive
->driver_data
;
444 struct request
*rq
= drive
->hwif
->rq
;
446 if (drive
->failed_pc
== NULL
&& pc
->c
[0] != REQUEST_SENSE
)
447 drive
->failed_pc
= pc
;
449 /* Set the current packet command */
452 if (pc
->retries
> IDETAPE_MAX_PC_RETRIES
||
453 (pc
->flags
& PC_FLAG_ABORT
)) {
456 * We will "abort" retrying a packet command in case legitimate
457 * error code was received (crossing a filemark, or end of the
458 * media, for example).
460 if (!(pc
->flags
& PC_FLAG_ABORT
)) {
461 if (!(pc
->c
[0] == TEST_UNIT_READY
&&
462 tape
->sense_key
== 2 && tape
->asc
== 4 &&
463 (tape
->ascq
== 1 || tape
->ascq
== 8))) {
464 printk(KERN_ERR
"ide-tape: %s: I/O error, "
465 "pc = %2x, key = %2x, "
466 "asc = %2x, ascq = %2x\n",
467 tape
->name
, pc
->c
[0],
468 tape
->sense_key
, tape
->asc
,
472 pc
->error
= IDE_DRV_ERROR_GENERAL
;
475 drive
->failed_pc
= NULL
;
476 drive
->pc_callback(drive
, 0);
477 ide_complete_rq(drive
, -EIO
, blk_rq_bytes(rq
));
480 ide_debug_log(IDE_DBG_SENSE
, "retry #%d, cmd: 0x%02x", pc
->retries
,
485 return ide_issue_pc(drive
, cmd
);
488 /* A mode sense command is used to "sense" tape parameters. */
489 static void idetape_create_mode_sense_cmd(struct ide_atapi_pc
*pc
, u8 page_code
)
492 pc
->c
[0] = MODE_SENSE
;
493 if (page_code
!= IDETAPE_BLOCK_DESCRIPTOR
)
494 /* DBD = 1 - Don't return block descriptors */
496 pc
->c
[2] = page_code
;
498 * Changed pc->c[3] to 0 (255 will at best return unused info).
500 * For SCSI this byte is defined as subpage instead of high byte
501 * of length and some IDE drives seem to interpret it this way
502 * and return an error when 255 is used.
505 /* We will just discard data in that case */
507 if (page_code
== IDETAPE_BLOCK_DESCRIPTOR
)
509 else if (page_code
== IDETAPE_CAPABILITIES_PAGE
)
515 static ide_startstop_t
idetape_media_access_finished(ide_drive_t
*drive
)
517 ide_hwif_t
*hwif
= drive
->hwif
;
518 idetape_tape_t
*tape
= drive
->driver_data
;
519 struct ide_atapi_pc
*pc
= drive
->pc
;
522 stat
= hwif
->tp_ops
->read_status(hwif
);
524 if (stat
& ATA_DSC
) {
525 if (stat
& ATA_ERR
) {
527 if (pc
->c
[0] != TEST_UNIT_READY
)
528 printk(KERN_ERR
"ide-tape: %s: I/O error, ",
530 /* Retry operation */
536 pc
->error
= IDE_DRV_ERROR_GENERAL
;
537 drive
->failed_pc
= NULL
;
539 drive
->pc_callback(drive
, 0);
543 static void ide_tape_create_rw_cmd(idetape_tape_t
*tape
,
544 struct ide_atapi_pc
*pc
, struct request
*rq
,
547 unsigned int length
= blk_rq_sectors(rq
) / (tape
->blk_size
>> 9);
550 put_unaligned(cpu_to_be32(length
), (unsigned int *) &pc
->c
[1]);
553 if (blk_rq_bytes(rq
) == tape
->buffer_size
)
554 pc
->flags
|= PC_FLAG_DMA_OK
;
556 if (opcode
== READ_6
)
558 else if (opcode
== WRITE_6
) {
560 pc
->flags
|= PC_FLAG_WRITING
;
563 memcpy(rq
->cmd
, pc
->c
, 12);
566 static ide_startstop_t
idetape_do_request(ide_drive_t
*drive
,
567 struct request
*rq
, sector_t block
)
569 ide_hwif_t
*hwif
= drive
->hwif
;
570 idetape_tape_t
*tape
= drive
->driver_data
;
571 struct ide_atapi_pc
*pc
= NULL
;
575 ide_debug_log(IDE_DBG_RQ
, "cmd: 0x%x, sector: %llu, nr_sectors: %u",
576 rq
->cmd
[0], (unsigned long long)blk_rq_pos(rq
),
579 BUG_ON(!(rq
->cmd_type
== REQ_TYPE_DRV_PRIV
||
580 rq
->cmd_type
== REQ_TYPE_ATA_SENSE
));
582 /* Retry a failed packet command */
583 if (drive
->failed_pc
&& drive
->pc
->c
[0] == REQUEST_SENSE
) {
584 pc
= drive
->failed_pc
;
589 * If the tape is still busy, postpone our request and service
590 * the other device meanwhile.
592 stat
= hwif
->tp_ops
->read_status(hwif
);
594 if ((drive
->dev_flags
& IDE_DFLAG_DSC_OVERLAP
) == 0 &&
595 (rq
->cmd
[13] & REQ_IDETAPE_PC2
) == 0)
596 drive
->atapi_flags
|= IDE_AFLAG_IGNORE_DSC
;
598 if (drive
->dev_flags
& IDE_DFLAG_POST_RESET
) {
599 drive
->atapi_flags
|= IDE_AFLAG_IGNORE_DSC
;
600 drive
->dev_flags
&= ~IDE_DFLAG_POST_RESET
;
603 if (!(drive
->atapi_flags
& IDE_AFLAG_IGNORE_DSC
) &&
605 if (!tape
->postponed_rq
) {
606 tape
->dsc_polling_start
= jiffies
;
607 tape
->dsc_poll_freq
= tape
->best_dsc_rw_freq
;
608 tape
->dsc_timeout
= jiffies
+ IDETAPE_DSC_RW_TIMEOUT
;
609 } else if (time_after(jiffies
, tape
->dsc_timeout
)) {
610 printk(KERN_ERR
"ide-tape: %s: DSC timeout\n",
612 if (rq
->cmd
[13] & REQ_IDETAPE_PC2
) {
613 idetape_media_access_finished(drive
);
616 return ide_do_reset(drive
);
618 } else if (time_after(jiffies
,
619 tape
->dsc_polling_start
+
620 IDETAPE_DSC_MA_THRESHOLD
))
621 tape
->dsc_poll_freq
= IDETAPE_DSC_MA_SLOW
;
622 ide_tape_stall_queue(drive
);
625 drive
->atapi_flags
&= ~IDE_AFLAG_IGNORE_DSC
;
626 tape
->postponed_rq
= false;
629 if (rq
->cmd
[13] & REQ_IDETAPE_READ
) {
630 pc
= &tape
->queued_pc
;
631 ide_tape_create_rw_cmd(tape
, pc
, rq
, READ_6
);
634 if (rq
->cmd
[13] & REQ_IDETAPE_WRITE
) {
635 pc
= &tape
->queued_pc
;
636 ide_tape_create_rw_cmd(tape
, pc
, rq
, WRITE_6
);
639 if (rq
->cmd
[13] & REQ_IDETAPE_PC1
) {
640 pc
= (struct ide_atapi_pc
*)rq
->special
;
641 rq
->cmd
[13] &= ~(REQ_IDETAPE_PC1
);
642 rq
->cmd
[13] |= REQ_IDETAPE_PC2
;
645 if (rq
->cmd
[13] & REQ_IDETAPE_PC2
) {
646 idetape_media_access_finished(drive
);
652 /* prepare sense request for this command */
653 ide_prep_sense(drive
, rq
);
655 memset(&cmd
, 0, sizeof(cmd
));
658 cmd
.tf_flags
|= IDE_TFLAG_WRITE
;
662 ide_init_sg_cmd(&cmd
, blk_rq_bytes(rq
));
663 ide_map_sg(drive
, &cmd
);
665 return ide_tape_issue_pc(drive
, &cmd
, pc
);
669 * Write a filemark if write_filemark=1. Flush the device buffers without
670 * writing a filemark otherwise.
672 static void idetape_create_write_filemark_cmd(ide_drive_t
*drive
,
673 struct ide_atapi_pc
*pc
, int write_filemark
)
676 pc
->c
[0] = WRITE_FILEMARKS
;
677 pc
->c
[4] = write_filemark
;
678 pc
->flags
|= PC_FLAG_WAIT_FOR_DSC
;
681 static int idetape_wait_ready(ide_drive_t
*drive
, unsigned long timeout
)
683 idetape_tape_t
*tape
= drive
->driver_data
;
684 struct gendisk
*disk
= tape
->disk
;
685 int load_attempted
= 0;
687 /* Wait for the tape to become ready */
688 set_bit(ilog2(IDE_AFLAG_MEDIUM_PRESENT
), &drive
->atapi_flags
);
690 while (time_before(jiffies
, timeout
)) {
691 if (ide_do_test_unit_ready(drive
, disk
) == 0)
693 if ((tape
->sense_key
== 2 && tape
->asc
== 4 && tape
->ascq
== 2)
694 || (tape
->asc
== 0x3A)) {
698 ide_do_start_stop(drive
, disk
, IDETAPE_LU_LOAD_MASK
);
700 /* not about to be ready */
701 } else if (!(tape
->sense_key
== 2 && tape
->asc
== 4 &&
702 (tape
->ascq
== 1 || tape
->ascq
== 8)))
709 static int idetape_flush_tape_buffers(ide_drive_t
*drive
)
711 struct ide_tape_obj
*tape
= drive
->driver_data
;
712 struct ide_atapi_pc pc
;
715 idetape_create_write_filemark_cmd(drive
, &pc
, 0);
716 rc
= ide_queue_pc_tail(drive
, tape
->disk
, &pc
, NULL
, 0);
719 idetape_wait_ready(drive
, 60 * 5 * HZ
);
723 static int ide_tape_read_position(ide_drive_t
*drive
)
725 idetape_tape_t
*tape
= drive
->driver_data
;
726 struct ide_atapi_pc pc
;
729 ide_debug_log(IDE_DBG_FUNC
, "enter");
733 pc
.c
[0] = READ_POSITION
;
736 if (ide_queue_pc_tail(drive
, tape
->disk
, &pc
, buf
, pc
.req_xfer
))
740 ide_debug_log(IDE_DBG_FUNC
, "BOP - %s",
741 (buf
[0] & 0x80) ? "Yes" : "No");
742 ide_debug_log(IDE_DBG_FUNC
, "EOP - %s",
743 (buf
[0] & 0x40) ? "Yes" : "No");
746 printk(KERN_INFO
"ide-tape: Block location is unknown"
748 clear_bit(ilog2(IDE_AFLAG_ADDRESS_VALID
),
749 &drive
->atapi_flags
);
752 ide_debug_log(IDE_DBG_FUNC
, "Block Location: %u",
753 be32_to_cpup((__be32
*)&buf
[4]));
755 tape
->partition
= buf
[1];
756 tape
->first_frame
= be32_to_cpup((__be32
*)&buf
[4]);
757 set_bit(ilog2(IDE_AFLAG_ADDRESS_VALID
),
758 &drive
->atapi_flags
);
762 return tape
->first_frame
;
765 static void idetape_create_locate_cmd(ide_drive_t
*drive
,
766 struct ide_atapi_pc
*pc
,
767 unsigned int block
, u8 partition
, int skip
)
770 pc
->c
[0] = POSITION_TO_ELEMENT
;
772 put_unaligned(cpu_to_be32(block
), (unsigned int *) &pc
->c
[3]);
773 pc
->c
[8] = partition
;
774 pc
->flags
|= PC_FLAG_WAIT_FOR_DSC
;
777 static void __ide_tape_discard_merge_buffer(ide_drive_t
*drive
)
779 idetape_tape_t
*tape
= drive
->driver_data
;
781 if (tape
->chrdev_dir
!= IDETAPE_DIR_READ
)
784 clear_bit(ilog2(IDE_AFLAG_FILEMARK
), &drive
->atapi_flags
);
786 if (tape
->buf
!= NULL
) {
791 tape
->chrdev_dir
= IDETAPE_DIR_NONE
;
795 * Position the tape to the requested block using the LOCATE packet command.
796 * A READ POSITION command is then issued to check where we are positioned. Like
797 * all higher level operations, we queue the commands at the tail of the request
798 * queue and wait for their completion.
800 static int idetape_position_tape(ide_drive_t
*drive
, unsigned int block
,
801 u8 partition
, int skip
)
803 idetape_tape_t
*tape
= drive
->driver_data
;
804 struct gendisk
*disk
= tape
->disk
;
806 struct ide_atapi_pc pc
;
808 if (tape
->chrdev_dir
== IDETAPE_DIR_READ
)
809 __ide_tape_discard_merge_buffer(drive
);
810 idetape_wait_ready(drive
, 60 * 5 * HZ
);
811 idetape_create_locate_cmd(drive
, &pc
, block
, partition
, skip
);
812 ret
= ide_queue_pc_tail(drive
, disk
, &pc
, NULL
, 0);
816 ret
= ide_tape_read_position(drive
);
822 static void ide_tape_discard_merge_buffer(ide_drive_t
*drive
,
823 int restore_position
)
825 idetape_tape_t
*tape
= drive
->driver_data
;
828 __ide_tape_discard_merge_buffer(drive
);
829 if (restore_position
) {
830 position
= ide_tape_read_position(drive
);
831 seek
= position
> 0 ? position
: 0;
832 if (idetape_position_tape(drive
, seek
, 0, 0)) {
833 printk(KERN_INFO
"ide-tape: %s: position_tape failed in"
834 " %s\n", tape
->name
, __func__
);
841 * Generate a read/write request for the block device interface and wait for it
844 static int idetape_queue_rw_tail(ide_drive_t
*drive
, int cmd
, int size
)
846 idetape_tape_t
*tape
= drive
->driver_data
;
850 ide_debug_log(IDE_DBG_FUNC
, "cmd: 0x%x, size: %d", cmd
, size
);
852 BUG_ON(cmd
!= REQ_IDETAPE_READ
&& cmd
!= REQ_IDETAPE_WRITE
);
853 BUG_ON(size
< 0 || size
% tape
->blk_size
);
855 rq
= blk_get_request(drive
->queue
, READ
, __GFP_RECLAIM
);
856 rq
->cmd_type
= REQ_TYPE_DRV_PRIV
;
858 rq
->rq_disk
= tape
->disk
;
859 rq
->__sector
= tape
->first_frame
;
862 ret
= blk_rq_map_kern(drive
->queue
, rq
, tape
->buf
, size
,
868 blk_execute_rq(drive
->queue
, tape
->disk
, rq
, 0);
870 /* calculate the number of transferred bytes and update buffer state */
871 size
-= rq
->resid_len
;
872 tape
->cur
= tape
->buf
;
873 if (cmd
== REQ_IDETAPE_READ
)
879 if (rq
->errors
== IDE_DRV_ERROR_GENERAL
)
886 static void idetape_create_inquiry_cmd(struct ide_atapi_pc
*pc
)
894 static void idetape_create_rewind_cmd(ide_drive_t
*drive
,
895 struct ide_atapi_pc
*pc
)
898 pc
->c
[0] = REZERO_UNIT
;
899 pc
->flags
|= PC_FLAG_WAIT_FOR_DSC
;
902 static void idetape_create_erase_cmd(struct ide_atapi_pc
*pc
)
907 pc
->flags
|= PC_FLAG_WAIT_FOR_DSC
;
910 static void idetape_create_space_cmd(struct ide_atapi_pc
*pc
, int count
, u8 cmd
)
914 put_unaligned(cpu_to_be32(count
), (unsigned int *) &pc
->c
[1]);
916 pc
->flags
|= PC_FLAG_WAIT_FOR_DSC
;
919 static void ide_tape_flush_merge_buffer(ide_drive_t
*drive
)
921 idetape_tape_t
*tape
= drive
->driver_data
;
923 if (tape
->chrdev_dir
!= IDETAPE_DIR_WRITE
) {
924 printk(KERN_ERR
"ide-tape: bug: Trying to empty merge buffer"
925 " but we are not writing.\n");
929 size_t aligned
= roundup(tape
->valid
, tape
->blk_size
);
931 memset(tape
->cur
, 0, aligned
- tape
->valid
);
932 idetape_queue_rw_tail(drive
, REQ_IDETAPE_WRITE
, aligned
);
936 tape
->chrdev_dir
= IDETAPE_DIR_NONE
;
939 static int idetape_init_rw(ide_drive_t
*drive
, int dir
)
941 idetape_tape_t
*tape
= drive
->driver_data
;
944 BUG_ON(dir
!= IDETAPE_DIR_READ
&& dir
!= IDETAPE_DIR_WRITE
);
946 if (tape
->chrdev_dir
== dir
)
949 if (tape
->chrdev_dir
== IDETAPE_DIR_READ
)
950 ide_tape_discard_merge_buffer(drive
, 1);
951 else if (tape
->chrdev_dir
== IDETAPE_DIR_WRITE
) {
952 ide_tape_flush_merge_buffer(drive
);
953 idetape_flush_tape_buffers(drive
);
956 if (tape
->buf
|| tape
->valid
) {
957 printk(KERN_ERR
"ide-tape: valid should be 0 now\n");
961 tape
->buf
= kmalloc(tape
->buffer_size
, GFP_KERNEL
);
964 tape
->chrdev_dir
= dir
;
965 tape
->cur
= tape
->buf
;
968 * Issue a 0 rw command to ensure that DSC handshake is
969 * switched from completion mode to buffer available mode. No
970 * point in issuing this if DSC overlap isn't supported, some
971 * drives (Seagate STT3401A) will return an error.
973 if (drive
->dev_flags
& IDE_DFLAG_DSC_OVERLAP
) {
974 int cmd
= dir
== IDETAPE_DIR_READ
? REQ_IDETAPE_READ
977 rc
= idetape_queue_rw_tail(drive
, cmd
, 0);
981 tape
->chrdev_dir
= IDETAPE_DIR_NONE
;
989 static void idetape_pad_zeros(ide_drive_t
*drive
, int bcount
)
991 idetape_tape_t
*tape
= drive
->driver_data
;
993 memset(tape
->buf
, 0, tape
->buffer_size
);
996 unsigned int count
= min(tape
->buffer_size
, bcount
);
998 idetape_queue_rw_tail(drive
, REQ_IDETAPE_WRITE
, count
);
1004 * Rewinds the tape to the Beginning Of the current Partition (BOP). We
1005 * currently support only one partition.
1007 static int idetape_rewind_tape(ide_drive_t
*drive
)
1009 struct ide_tape_obj
*tape
= drive
->driver_data
;
1010 struct gendisk
*disk
= tape
->disk
;
1011 struct ide_atapi_pc pc
;
1014 ide_debug_log(IDE_DBG_FUNC
, "enter");
1016 idetape_create_rewind_cmd(drive
, &pc
);
1017 ret
= ide_queue_pc_tail(drive
, disk
, &pc
, NULL
, 0);
1021 ret
= ide_tape_read_position(drive
);
1027 /* mtio.h compatible commands should be issued to the chrdev interface. */
1028 static int idetape_blkdev_ioctl(ide_drive_t
*drive
, unsigned int cmd
,
1031 idetape_tape_t
*tape
= drive
->driver_data
;
1032 void __user
*argp
= (void __user
*)arg
;
1034 struct idetape_config
{
1035 int dsc_rw_frequency
;
1036 int dsc_media_access_frequency
;
1040 ide_debug_log(IDE_DBG_FUNC
, "cmd: 0x%04x", cmd
);
1044 if (copy_from_user(&config
, argp
, sizeof(config
)))
1046 tape
->best_dsc_rw_freq
= config
.dsc_rw_frequency
;
1049 memset(&config
, 0, sizeof(config
));
1050 config
.dsc_rw_frequency
= (int) tape
->best_dsc_rw_freq
;
1051 config
.nr_stages
= 1;
1052 if (copy_to_user(argp
, &config
, sizeof(config
)))
1061 static int idetape_space_over_filemarks(ide_drive_t
*drive
, short mt_op
,
1064 idetape_tape_t
*tape
= drive
->driver_data
;
1065 struct gendisk
*disk
= tape
->disk
;
1066 struct ide_atapi_pc pc
;
1067 int retval
, count
= 0;
1068 int sprev
= !!(tape
->caps
[4] & 0x20);
1071 ide_debug_log(IDE_DBG_FUNC
, "mt_op: %d, mt_count: %d", mt_op
, mt_count
);
1075 if (MTBSF
== mt_op
|| MTBSFM
== mt_op
) {
1078 mt_count
= -mt_count
;
1081 if (tape
->chrdev_dir
== IDETAPE_DIR_READ
) {
1083 if (test_and_clear_bit(ilog2(IDE_AFLAG_FILEMARK
),
1084 &drive
->atapi_flags
))
1086 ide_tape_discard_merge_buffer(drive
, 0);
1092 idetape_create_space_cmd(&pc
, mt_count
- count
,
1093 IDETAPE_SPACE_OVER_FILEMARK
);
1094 return ide_queue_pc_tail(drive
, disk
, &pc
, NULL
, 0);
1099 retval
= idetape_space_over_filemarks(drive
, MTFSF
,
1103 count
= (MTBSFM
== mt_op
? 1 : -1);
1104 return idetape_space_over_filemarks(drive
, MTFSF
, count
);
1106 printk(KERN_ERR
"ide-tape: MTIO operation %d not supported\n",
1113 * Our character device read / write functions.
1115 * The tape is optimized to maximize throughput when it is transferring an
1116 * integral number of the "continuous transfer limit", which is a parameter of
1117 * the specific tape (26kB on my particular tape, 32kB for Onstream).
1119 * As of version 1.3 of the driver, the character device provides an abstract
1120 * continuous view of the media - any mix of block sizes (even 1 byte) on the
1121 * same backup/restore procedure is supported. The driver will internally
1122 * convert the requests to the recommended transfer unit, so that an unmatch
1123 * between the user's block size to the recommended size will only result in a
1124 * (slightly) increased driver overhead, but will no longer hit performance.
1125 * This is not applicable to Onstream.
1127 static ssize_t
idetape_chrdev_read(struct file
*file
, char __user
*buf
,
1128 size_t count
, loff_t
*ppos
)
1130 struct ide_tape_obj
*tape
= file
->private_data
;
1131 ide_drive_t
*drive
= tape
->drive
;
1136 ide_debug_log(IDE_DBG_FUNC
, "count %Zd", count
);
1138 if (tape
->chrdev_dir
!= IDETAPE_DIR_READ
) {
1139 if (test_bit(ilog2(IDE_AFLAG_DETECT_BS
), &drive
->atapi_flags
))
1140 if (count
> tape
->blk_size
&&
1141 (count
% tape
->blk_size
) == 0)
1142 tape
->user_bs_factor
= count
/ tape
->blk_size
;
1145 rc
= idetape_init_rw(drive
, IDETAPE_DIR_READ
);
1149 while (done
< count
) {
1152 /* refill if staging buffer is empty */
1154 /* If we are at a filemark, nothing more to read */
1155 if (test_bit(ilog2(IDE_AFLAG_FILEMARK
),
1156 &drive
->atapi_flags
))
1159 if (idetape_queue_rw_tail(drive
, REQ_IDETAPE_READ
,
1160 tape
->buffer_size
) <= 0)
1165 todo
= min_t(size_t, count
- done
, tape
->valid
);
1166 if (copy_to_user(buf
+ done
, tape
->cur
, todo
))
1170 tape
->valid
-= todo
;
1174 if (!done
&& test_bit(ilog2(IDE_AFLAG_FILEMARK
), &drive
->atapi_flags
)) {
1175 idetape_space_over_filemarks(drive
, MTFSF
, 1);
1179 return ret
? ret
: done
;
1182 static ssize_t
idetape_chrdev_write(struct file
*file
, const char __user
*buf
,
1183 size_t count
, loff_t
*ppos
)
1185 struct ide_tape_obj
*tape
= file
->private_data
;
1186 ide_drive_t
*drive
= tape
->drive
;
1191 /* The drive is write protected. */
1192 if (tape
->write_prot
)
1195 ide_debug_log(IDE_DBG_FUNC
, "count %Zd", count
);
1197 /* Initialize write operation */
1198 rc
= idetape_init_rw(drive
, IDETAPE_DIR_WRITE
);
1202 while (done
< count
) {
1205 /* flush if staging buffer is full */
1206 if (tape
->valid
== tape
->buffer_size
&&
1207 idetape_queue_rw_tail(drive
, REQ_IDETAPE_WRITE
,
1208 tape
->buffer_size
) <= 0)
1212 todo
= min_t(size_t, count
- done
,
1213 tape
->buffer_size
- tape
->valid
);
1214 if (copy_from_user(tape
->cur
, buf
+ done
, todo
))
1218 tape
->valid
+= todo
;
1222 return ret
? ret
: done
;
1225 static int idetape_write_filemark(ide_drive_t
*drive
)
1227 struct ide_tape_obj
*tape
= drive
->driver_data
;
1228 struct ide_atapi_pc pc
;
1230 /* Write a filemark */
1231 idetape_create_write_filemark_cmd(drive
, &pc
, 1);
1232 if (ide_queue_pc_tail(drive
, tape
->disk
, &pc
, NULL
, 0)) {
1233 printk(KERN_ERR
"ide-tape: Couldn't write a filemark\n");
1240 * Called from idetape_chrdev_ioctl when the general mtio MTIOCTOP ioctl is
1243 * Note: MTBSF and MTBSFM are not supported when the tape doesn't support
1244 * spacing over filemarks in the reverse direction. In this case, MTFSFM is also
1245 * usually not supported.
1247 * The following commands are currently not supported:
1249 * MTFSS, MTBSS, MTWSM, MTSETDENSITY, MTSETDRVBUFFER, MT_ST_BOOLEANS,
1250 * MT_ST_WRITE_THRESHOLD.
1252 static int idetape_mtioctop(ide_drive_t
*drive
, short mt_op
, int mt_count
)
1254 idetape_tape_t
*tape
= drive
->driver_data
;
1255 struct gendisk
*disk
= tape
->disk
;
1256 struct ide_atapi_pc pc
;
1259 ide_debug_log(IDE_DBG_FUNC
, "MTIOCTOP ioctl: mt_op: %d, mt_count: %d",
1269 return idetape_space_over_filemarks(drive
, mt_op
, mt_count
);
1276 if (tape
->write_prot
)
1278 ide_tape_discard_merge_buffer(drive
, 1);
1279 for (i
= 0; i
< mt_count
; i
++) {
1280 retval
= idetape_write_filemark(drive
);
1286 ide_tape_discard_merge_buffer(drive
, 0);
1287 if (idetape_rewind_tape(drive
))
1291 ide_tape_discard_merge_buffer(drive
, 0);
1292 return ide_do_start_stop(drive
, disk
, IDETAPE_LU_LOAD_MASK
);
1296 * If door is locked, attempt to unlock before
1297 * attempting to eject.
1299 if (tape
->door_locked
) {
1300 if (!ide_set_media_lock(drive
, disk
, 0))
1301 tape
->door_locked
= DOOR_UNLOCKED
;
1303 ide_tape_discard_merge_buffer(drive
, 0);
1304 retval
= ide_do_start_stop(drive
, disk
, !IDETAPE_LU_LOAD_MASK
);
1306 clear_bit(ilog2(IDE_AFLAG_MEDIUM_PRESENT
),
1307 &drive
->atapi_flags
);
1310 ide_tape_discard_merge_buffer(drive
, 0);
1311 return idetape_flush_tape_buffers(drive
);
1313 ide_tape_discard_merge_buffer(drive
, 0);
1314 return ide_do_start_stop(drive
, disk
,
1315 IDETAPE_LU_RETENSION_MASK
| IDETAPE_LU_LOAD_MASK
);
1317 idetape_create_space_cmd(&pc
, 0, IDETAPE_SPACE_TO_EOD
);
1318 return ide_queue_pc_tail(drive
, disk
, &pc
, NULL
, 0);
1320 (void)idetape_rewind_tape(drive
);
1321 idetape_create_erase_cmd(&pc
);
1322 return ide_queue_pc_tail(drive
, disk
, &pc
, NULL
, 0);
1325 if (mt_count
< tape
->blk_size
||
1326 mt_count
% tape
->blk_size
)
1328 tape
->user_bs_factor
= mt_count
/ tape
->blk_size
;
1329 clear_bit(ilog2(IDE_AFLAG_DETECT_BS
),
1330 &drive
->atapi_flags
);
1332 set_bit(ilog2(IDE_AFLAG_DETECT_BS
),
1333 &drive
->atapi_flags
);
1336 ide_tape_discard_merge_buffer(drive
, 0);
1337 return idetape_position_tape(drive
,
1338 mt_count
* tape
->user_bs_factor
, tape
->partition
, 0);
1340 ide_tape_discard_merge_buffer(drive
, 0);
1341 return idetape_position_tape(drive
, 0, mt_count
, 0);
1345 retval
= ide_set_media_lock(drive
, disk
, 1);
1348 tape
->door_locked
= DOOR_EXPLICITLY_LOCKED
;
1351 retval
= ide_set_media_lock(drive
, disk
, 0);
1354 tape
->door_locked
= DOOR_UNLOCKED
;
1357 printk(KERN_ERR
"ide-tape: MTIO operation %d not supported\n",
1364 * Our character device ioctls. General mtio.h magnetic io commands are
1365 * supported here, and not in the corresponding block interface. Our own
1366 * ide-tape ioctls are supported on both interfaces.
1368 static long do_idetape_chrdev_ioctl(struct file
*file
,
1369 unsigned int cmd
, unsigned long arg
)
1371 struct ide_tape_obj
*tape
= file
->private_data
;
1372 ide_drive_t
*drive
= tape
->drive
;
1376 int block_offset
= 0, position
= tape
->first_frame
;
1377 void __user
*argp
= (void __user
*)arg
;
1379 ide_debug_log(IDE_DBG_FUNC
, "cmd: 0x%x", cmd
);
1381 if (tape
->chrdev_dir
== IDETAPE_DIR_WRITE
) {
1382 ide_tape_flush_merge_buffer(drive
);
1383 idetape_flush_tape_buffers(drive
);
1385 if (cmd
== MTIOCGET
|| cmd
== MTIOCPOS
) {
1386 block_offset
= tape
->valid
/
1387 (tape
->blk_size
* tape
->user_bs_factor
);
1388 position
= ide_tape_read_position(drive
);
1394 if (copy_from_user(&mtop
, argp
, sizeof(struct mtop
)))
1396 return idetape_mtioctop(drive
, mtop
.mt_op
, mtop
.mt_count
);
1398 memset(&mtget
, 0, sizeof(struct mtget
));
1399 mtget
.mt_type
= MT_ISSCSI2
;
1400 mtget
.mt_blkno
= position
/ tape
->user_bs_factor
- block_offset
;
1402 ((tape
->blk_size
* tape
->user_bs_factor
)
1403 << MT_ST_BLKSIZE_SHIFT
) & MT_ST_BLKSIZE_MASK
;
1405 if (tape
->drv_write_prot
)
1406 mtget
.mt_gstat
|= GMT_WR_PROT(0xffffffff);
1408 if (copy_to_user(argp
, &mtget
, sizeof(struct mtget
)))
1412 mtpos
.mt_blkno
= position
/ tape
->user_bs_factor
- block_offset
;
1413 if (copy_to_user(argp
, &mtpos
, sizeof(struct mtpos
)))
1417 if (tape
->chrdev_dir
== IDETAPE_DIR_READ
)
1418 ide_tape_discard_merge_buffer(drive
, 1);
1419 return idetape_blkdev_ioctl(drive
, cmd
, arg
);
1423 static long idetape_chrdev_ioctl(struct file
*file
,
1424 unsigned int cmd
, unsigned long arg
)
1427 mutex_lock(&ide_tape_mutex
);
1428 ret
= do_idetape_chrdev_ioctl(file
, cmd
, arg
);
1429 mutex_unlock(&ide_tape_mutex
);
1434 * Do a mode sense page 0 with block descriptor and if it succeeds set the tape
1435 * block size with the reported value.
1437 static void ide_tape_get_bsize_from_bdesc(ide_drive_t
*drive
)
1439 idetape_tape_t
*tape
= drive
->driver_data
;
1440 struct ide_atapi_pc pc
;
1443 idetape_create_mode_sense_cmd(&pc
, IDETAPE_BLOCK_DESCRIPTOR
);
1444 if (ide_queue_pc_tail(drive
, tape
->disk
, &pc
, buf
, pc
.req_xfer
)) {
1445 printk(KERN_ERR
"ide-tape: Can't get block descriptor\n");
1446 if (tape
->blk_size
== 0) {
1447 printk(KERN_WARNING
"ide-tape: Cannot deal with zero "
1448 "block size, assuming 32k\n");
1449 tape
->blk_size
= 32768;
1453 tape
->blk_size
= (buf
[4 + 5] << 16) +
1456 tape
->drv_write_prot
= (buf
[2] & 0x80) >> 7;
1458 ide_debug_log(IDE_DBG_FUNC
, "blk_size: %d, write_prot: %d",
1459 tape
->blk_size
, tape
->drv_write_prot
);
1462 static int idetape_chrdev_open(struct inode
*inode
, struct file
*filp
)
1464 unsigned int minor
= iminor(inode
), i
= minor
& ~0xc0;
1466 idetape_tape_t
*tape
;
1469 if (i
>= MAX_HWIFS
* MAX_DRIVES
)
1472 mutex_lock(&idetape_chrdev_mutex
);
1474 tape
= ide_tape_get(NULL
, true, i
);
1476 mutex_unlock(&idetape_chrdev_mutex
);
1480 drive
= tape
->drive
;
1481 filp
->private_data
= tape
;
1483 ide_debug_log(IDE_DBG_FUNC
, "enter");
1486 * We really want to do nonseekable_open(inode, filp); here, but some
1487 * versions of tar incorrectly call lseek on tapes and bail out if that
1488 * fails. So we disallow pread() and pwrite(), but permit lseeks.
1490 filp
->f_mode
&= ~(FMODE_PREAD
| FMODE_PWRITE
);
1493 if (test_and_set_bit(ilog2(IDE_AFLAG_BUSY
), &drive
->atapi_flags
)) {
1498 retval
= idetape_wait_ready(drive
, 60 * HZ
);
1500 clear_bit(ilog2(IDE_AFLAG_BUSY
), &drive
->atapi_flags
);
1501 printk(KERN_ERR
"ide-tape: %s: drive not ready\n", tape
->name
);
1505 ide_tape_read_position(drive
);
1506 if (!test_bit(ilog2(IDE_AFLAG_ADDRESS_VALID
), &drive
->atapi_flags
))
1507 (void)idetape_rewind_tape(drive
);
1509 /* Read block size and write protect status from drive. */
1510 ide_tape_get_bsize_from_bdesc(drive
);
1512 /* Set write protect flag if device is opened as read-only. */
1513 if ((filp
->f_flags
& O_ACCMODE
) == O_RDONLY
)
1514 tape
->write_prot
= 1;
1516 tape
->write_prot
= tape
->drv_write_prot
;
1518 /* Make sure drive isn't write protected if user wants to write. */
1519 if (tape
->write_prot
) {
1520 if ((filp
->f_flags
& O_ACCMODE
) == O_WRONLY
||
1521 (filp
->f_flags
& O_ACCMODE
) == O_RDWR
) {
1522 clear_bit(ilog2(IDE_AFLAG_BUSY
), &drive
->atapi_flags
);
1528 /* Lock the tape drive door so user can't eject. */
1529 if (tape
->chrdev_dir
== IDETAPE_DIR_NONE
) {
1530 if (!ide_set_media_lock(drive
, tape
->disk
, 1)) {
1531 if (tape
->door_locked
!= DOOR_EXPLICITLY_LOCKED
)
1532 tape
->door_locked
= DOOR_LOCKED
;
1535 mutex_unlock(&idetape_chrdev_mutex
);
1542 mutex_unlock(&idetape_chrdev_mutex
);
1547 static void idetape_write_release(ide_drive_t
*drive
, unsigned int minor
)
1549 idetape_tape_t
*tape
= drive
->driver_data
;
1551 ide_tape_flush_merge_buffer(drive
);
1552 tape
->buf
= kmalloc(tape
->buffer_size
, GFP_KERNEL
);
1553 if (tape
->buf
!= NULL
) {
1554 idetape_pad_zeros(drive
, tape
->blk_size
*
1555 (tape
->user_bs_factor
- 1));
1559 idetape_write_filemark(drive
);
1560 idetape_flush_tape_buffers(drive
);
1561 idetape_flush_tape_buffers(drive
);
1564 static int idetape_chrdev_release(struct inode
*inode
, struct file
*filp
)
1566 struct ide_tape_obj
*tape
= filp
->private_data
;
1567 ide_drive_t
*drive
= tape
->drive
;
1568 unsigned int minor
= iminor(inode
);
1570 mutex_lock(&idetape_chrdev_mutex
);
1572 tape
= drive
->driver_data
;
1574 ide_debug_log(IDE_DBG_FUNC
, "enter");
1576 if (tape
->chrdev_dir
== IDETAPE_DIR_WRITE
)
1577 idetape_write_release(drive
, minor
);
1578 if (tape
->chrdev_dir
== IDETAPE_DIR_READ
) {
1580 ide_tape_discard_merge_buffer(drive
, 1);
1583 if (minor
< 128 && test_bit(ilog2(IDE_AFLAG_MEDIUM_PRESENT
),
1584 &drive
->atapi_flags
))
1585 (void) idetape_rewind_tape(drive
);
1587 if (tape
->chrdev_dir
== IDETAPE_DIR_NONE
) {
1588 if (tape
->door_locked
== DOOR_LOCKED
) {
1589 if (!ide_set_media_lock(drive
, tape
->disk
, 0))
1590 tape
->door_locked
= DOOR_UNLOCKED
;
1593 clear_bit(ilog2(IDE_AFLAG_BUSY
), &drive
->atapi_flags
);
1596 mutex_unlock(&idetape_chrdev_mutex
);
1601 static void idetape_get_inquiry_results(ide_drive_t
*drive
)
1603 idetape_tape_t
*tape
= drive
->driver_data
;
1604 struct ide_atapi_pc pc
;
1606 char fw_rev
[4], vendor_id
[8], product_id
[16];
1608 idetape_create_inquiry_cmd(&pc
);
1609 if (ide_queue_pc_tail(drive
, tape
->disk
, &pc
, pc_buf
, pc
.req_xfer
)) {
1610 printk(KERN_ERR
"ide-tape: %s: can't get INQUIRY results\n",
1614 memcpy(vendor_id
, &pc_buf
[8], 8);
1615 memcpy(product_id
, &pc_buf
[16], 16);
1616 memcpy(fw_rev
, &pc_buf
[32], 4);
1618 ide_fixstring(vendor_id
, 8, 0);
1619 ide_fixstring(product_id
, 16, 0);
1620 ide_fixstring(fw_rev
, 4, 0);
1622 printk(KERN_INFO
"ide-tape: %s <-> %s: %.8s %.16s rev %.4s\n",
1623 drive
->name
, tape
->name
, vendor_id
, product_id
, fw_rev
);
1627 * Ask the tape about its various parameters. In particular, we will adjust our
1628 * data transfer buffer size to the recommended value as returned by the tape.
1630 static void idetape_get_mode_sense_results(ide_drive_t
*drive
)
1632 idetape_tape_t
*tape
= drive
->driver_data
;
1633 struct ide_atapi_pc pc
;
1635 u8 speed
, max_speed
;
1637 idetape_create_mode_sense_cmd(&pc
, IDETAPE_CAPABILITIES_PAGE
);
1638 if (ide_queue_pc_tail(drive
, tape
->disk
, &pc
, buf
, pc
.req_xfer
)) {
1639 printk(KERN_ERR
"ide-tape: Can't get tape parameters - assuming"
1640 " some default values\n");
1641 tape
->blk_size
= 512;
1642 put_unaligned(52, (u16
*)&tape
->caps
[12]);
1643 put_unaligned(540, (u16
*)&tape
->caps
[14]);
1644 put_unaligned(6*52, (u16
*)&tape
->caps
[16]);
1647 caps
= buf
+ 4 + buf
[3];
1649 /* convert to host order and save for later use */
1650 speed
= be16_to_cpup((__be16
*)&caps
[14]);
1651 max_speed
= be16_to_cpup((__be16
*)&caps
[8]);
1653 *(u16
*)&caps
[8] = max_speed
;
1654 *(u16
*)&caps
[12] = be16_to_cpup((__be16
*)&caps
[12]);
1655 *(u16
*)&caps
[14] = speed
;
1656 *(u16
*)&caps
[16] = be16_to_cpup((__be16
*)&caps
[16]);
1659 printk(KERN_INFO
"ide-tape: %s: invalid tape speed "
1660 "(assuming 650KB/sec)\n", drive
->name
);
1661 *(u16
*)&caps
[14] = 650;
1664 printk(KERN_INFO
"ide-tape: %s: invalid max_speed "
1665 "(assuming 650KB/sec)\n", drive
->name
);
1666 *(u16
*)&caps
[8] = 650;
1669 memcpy(&tape
->caps
, caps
, 20);
1671 /* device lacks locking support according to capabilities page */
1672 if ((caps
[6] & 1) == 0)
1673 drive
->dev_flags
&= ~IDE_DFLAG_DOORLOCKING
;
1676 tape
->blk_size
= 512;
1677 else if (caps
[7] & 0x04)
1678 tape
->blk_size
= 1024;
1681 #ifdef CONFIG_IDE_PROC_FS
1682 #define ide_tape_devset_get(name, field) \
1683 static int get_##name(ide_drive_t *drive) \
1685 idetape_tape_t *tape = drive->driver_data; \
1686 return tape->field; \
1689 #define ide_tape_devset_set(name, field) \
1690 static int set_##name(ide_drive_t *drive, int arg) \
1692 idetape_tape_t *tape = drive->driver_data; \
1693 tape->field = arg; \
1697 #define ide_tape_devset_rw_field(_name, _field) \
1698 ide_tape_devset_get(_name, _field) \
1699 ide_tape_devset_set(_name, _field) \
1700 IDE_DEVSET(_name, DS_SYNC, get_##_name, set_##_name)
1702 #define ide_tape_devset_r_field(_name, _field) \
1703 ide_tape_devset_get(_name, _field) \
1704 IDE_DEVSET(_name, 0, get_##_name, NULL)
1706 static int mulf_tdsc(ide_drive_t
*drive
) { return 1000; }
1707 static int divf_tdsc(ide_drive_t
*drive
) { return HZ
; }
1708 static int divf_buffer(ide_drive_t
*drive
) { return 2; }
1709 static int divf_buffer_size(ide_drive_t
*drive
) { return 1024; }
1711 ide_devset_rw_flag(dsc_overlap
, IDE_DFLAG_DSC_OVERLAP
);
1713 ide_tape_devset_rw_field(tdsc
, best_dsc_rw_freq
);
1715 ide_tape_devset_r_field(avg_speed
, avg_speed
);
1716 ide_tape_devset_r_field(speed
, caps
[14]);
1717 ide_tape_devset_r_field(buffer
, caps
[16]);
1718 ide_tape_devset_r_field(buffer_size
, buffer_size
);
1720 static const struct ide_proc_devset idetape_settings
[] = {
1721 __IDE_PROC_DEVSET(avg_speed
, 0, 0xffff, NULL
, NULL
),
1722 __IDE_PROC_DEVSET(buffer
, 0, 0xffff, NULL
, divf_buffer
),
1723 __IDE_PROC_DEVSET(buffer_size
, 0, 0xffff, NULL
, divf_buffer_size
),
1724 __IDE_PROC_DEVSET(dsc_overlap
, 0, 1, NULL
, NULL
),
1725 __IDE_PROC_DEVSET(speed
, 0, 0xffff, NULL
, NULL
),
1726 __IDE_PROC_DEVSET(tdsc
, IDETAPE_DSC_RW_MIN
, IDETAPE_DSC_RW_MAX
,
1727 mulf_tdsc
, divf_tdsc
),
1733 * The function below is called to:
1735 * 1. Initialize our various state variables.
1736 * 2. Ask the tape for its capabilities.
1737 * 3. Allocate a buffer which will be used for data transfer. The buffer size
1738 * is chosen based on the recommendation which we received in step 2.
1740 * Note that at this point ide.c already assigned us an irq, so that we can
1741 * queue requests here and wait for their completion.
1743 static void idetape_setup(ide_drive_t
*drive
, idetape_tape_t
*tape
, int minor
)
1748 u16
*ctl
= (u16
*)&tape
->caps
[12];
1750 ide_debug_log(IDE_DBG_FUNC
, "minor: %d", minor
);
1752 drive
->pc_callback
= ide_tape_callback
;
1754 drive
->dev_flags
|= IDE_DFLAG_DSC_OVERLAP
;
1756 if (drive
->hwif
->host_flags
& IDE_HFLAG_NO_DSC
) {
1757 printk(KERN_INFO
"ide-tape: %s: disabling DSC overlap\n",
1759 drive
->dev_flags
&= ~IDE_DFLAG_DSC_OVERLAP
;
1762 /* Seagate Travan drives do not support DSC overlap. */
1763 if (strstr((char *)&drive
->id
[ATA_ID_PROD
], "Seagate STT3401"))
1764 drive
->dev_flags
&= ~IDE_DFLAG_DSC_OVERLAP
;
1766 tape
->minor
= minor
;
1767 tape
->name
[0] = 'h';
1768 tape
->name
[1] = 't';
1769 tape
->name
[2] = '0' + minor
;
1770 tape
->chrdev_dir
= IDETAPE_DIR_NONE
;
1772 idetape_get_inquiry_results(drive
);
1773 idetape_get_mode_sense_results(drive
);
1774 ide_tape_get_bsize_from_bdesc(drive
);
1775 tape
->user_bs_factor
= 1;
1776 tape
->buffer_size
= *ctl
* tape
->blk_size
;
1777 while (tape
->buffer_size
> 0xffff) {
1778 printk(KERN_NOTICE
"ide-tape: decreasing stage size\n");
1780 tape
->buffer_size
= *ctl
* tape
->blk_size
;
1782 buffer_size
= tape
->buffer_size
;
1784 /* select the "best" DSC read/write polling freq */
1785 speed
= max(*(u16
*)&tape
->caps
[14], *(u16
*)&tape
->caps
[8]);
1787 t
= (IDETAPE_FIFO_THRESHOLD
* tape
->buffer_size
* HZ
) / (speed
* 1000);
1790 * Ensure that the number we got makes sense; limit it within
1791 * IDETAPE_DSC_RW_MIN and IDETAPE_DSC_RW_MAX.
1793 tape
->best_dsc_rw_freq
= clamp_t(unsigned long, t
, IDETAPE_DSC_RW_MIN
,
1794 IDETAPE_DSC_RW_MAX
);
1795 printk(KERN_INFO
"ide-tape: %s <-> %s: %dKBps, %d*%dkB buffer, "
1797 drive
->name
, tape
->name
, *(u16
*)&tape
->caps
[14],
1798 (*(u16
*)&tape
->caps
[16] * 512) / tape
->buffer_size
,
1799 tape
->buffer_size
/ 1024,
1800 jiffies_to_msecs(tape
->best_dsc_rw_freq
),
1801 (drive
->dev_flags
& IDE_DFLAG_USING_DMA
) ? ", DMA" : "");
1803 ide_proc_register_driver(drive
, tape
->driver
);
1806 static void ide_tape_remove(ide_drive_t
*drive
)
1808 idetape_tape_t
*tape
= drive
->driver_data
;
1810 ide_proc_unregister_driver(drive
, tape
->driver
);
1811 device_del(&tape
->dev
);
1812 ide_unregister_region(tape
->disk
);
1814 mutex_lock(&idetape_ref_mutex
);
1815 put_device(&tape
->dev
);
1816 mutex_unlock(&idetape_ref_mutex
);
1819 static void ide_tape_release(struct device
*dev
)
1821 struct ide_tape_obj
*tape
= to_ide_drv(dev
, ide_tape_obj
);
1822 ide_drive_t
*drive
= tape
->drive
;
1823 struct gendisk
*g
= tape
->disk
;
1825 BUG_ON(tape
->valid
);
1827 drive
->dev_flags
&= ~IDE_DFLAG_DSC_OVERLAP
;
1828 drive
->driver_data
= NULL
;
1829 device_destroy(idetape_sysfs_class
, MKDEV(IDETAPE_MAJOR
, tape
->minor
));
1830 device_destroy(idetape_sysfs_class
,
1831 MKDEV(IDETAPE_MAJOR
, tape
->minor
+ 128));
1832 idetape_devs
[tape
->minor
] = NULL
;
1833 g
->private_data
= NULL
;
1838 #ifdef CONFIG_IDE_PROC_FS
1839 static int idetape_name_proc_show(struct seq_file
*m
, void *v
)
1841 ide_drive_t
*drive
= (ide_drive_t
*) m
->private;
1842 idetape_tape_t
*tape
= drive
->driver_data
;
1844 seq_printf(m
, "%s\n", tape
->name
);
1848 static int idetape_name_proc_open(struct inode
*inode
, struct file
*file
)
1850 return single_open(file
, idetape_name_proc_show
, PDE_DATA(inode
));
1853 static const struct file_operations idetape_name_proc_fops
= {
1854 .owner
= THIS_MODULE
,
1855 .open
= idetape_name_proc_open
,
1857 .llseek
= seq_lseek
,
1858 .release
= single_release
,
1861 static ide_proc_entry_t idetape_proc
[] = {
1862 { "capacity", S_IFREG
|S_IRUGO
, &ide_capacity_proc_fops
},
1863 { "name", S_IFREG
|S_IRUGO
, &idetape_name_proc_fops
},
1867 static ide_proc_entry_t
*ide_tape_proc_entries(ide_drive_t
*drive
)
1869 return idetape_proc
;
1872 static const struct ide_proc_devset
*ide_tape_proc_devsets(ide_drive_t
*drive
)
1874 return idetape_settings
;
1878 static int ide_tape_probe(ide_drive_t
*);
1880 static struct ide_driver idetape_driver
= {
1882 .owner
= THIS_MODULE
,
1884 .bus
= &ide_bus_type
,
1886 .probe
= ide_tape_probe
,
1887 .remove
= ide_tape_remove
,
1888 .version
= IDETAPE_VERSION
,
1889 .do_request
= idetape_do_request
,
1890 #ifdef CONFIG_IDE_PROC_FS
1891 .proc_entries
= ide_tape_proc_entries
,
1892 .proc_devsets
= ide_tape_proc_devsets
,
1896 /* Our character device supporting functions, passed to register_chrdev. */
1897 static const struct file_operations idetape_fops
= {
1898 .owner
= THIS_MODULE
,
1899 .read
= idetape_chrdev_read
,
1900 .write
= idetape_chrdev_write
,
1901 .unlocked_ioctl
= idetape_chrdev_ioctl
,
1902 .open
= idetape_chrdev_open
,
1903 .release
= idetape_chrdev_release
,
1904 .llseek
= noop_llseek
,
1907 static int idetape_open(struct block_device
*bdev
, fmode_t mode
)
1909 struct ide_tape_obj
*tape
;
1911 mutex_lock(&ide_tape_mutex
);
1912 tape
= ide_tape_get(bdev
->bd_disk
, false, 0);
1913 mutex_unlock(&ide_tape_mutex
);
1921 static void idetape_release(struct gendisk
*disk
, fmode_t mode
)
1923 struct ide_tape_obj
*tape
= ide_drv_g(disk
, ide_tape_obj
);
1925 mutex_lock(&ide_tape_mutex
);
1927 mutex_unlock(&ide_tape_mutex
);
1930 static int idetape_ioctl(struct block_device
*bdev
, fmode_t mode
,
1931 unsigned int cmd
, unsigned long arg
)
1933 struct ide_tape_obj
*tape
= ide_drv_g(bdev
->bd_disk
, ide_tape_obj
);
1934 ide_drive_t
*drive
= tape
->drive
;
1937 mutex_lock(&ide_tape_mutex
);
1938 err
= generic_ide_ioctl(drive
, bdev
, cmd
, arg
);
1940 err
= idetape_blkdev_ioctl(drive
, cmd
, arg
);
1941 mutex_unlock(&ide_tape_mutex
);
1946 static const struct block_device_operations idetape_block_ops
= {
1947 .owner
= THIS_MODULE
,
1948 .open
= idetape_open
,
1949 .release
= idetape_release
,
1950 .ioctl
= idetape_ioctl
,
1953 static int ide_tape_probe(ide_drive_t
*drive
)
1955 idetape_tape_t
*tape
;
1959 ide_debug_log(IDE_DBG_FUNC
, "enter");
1961 if (!strstr(DRV_NAME
, drive
->driver_req
))
1964 if (drive
->media
!= ide_tape
)
1967 if ((drive
->dev_flags
& IDE_DFLAG_ID_READ
) &&
1968 ide_check_atapi_device(drive
, DRV_NAME
) == 0) {
1969 printk(KERN_ERR
"ide-tape: %s: not supported by this version of"
1970 " the driver\n", drive
->name
);
1973 tape
= kzalloc(sizeof(idetape_tape_t
), GFP_KERNEL
);
1975 printk(KERN_ERR
"ide-tape: %s: Can't allocate a tape struct\n",
1980 g
= alloc_disk(1 << PARTN_BITS
);
1984 ide_init_disk(g
, drive
);
1986 tape
->dev
.parent
= &drive
->gendev
;
1987 tape
->dev
.release
= ide_tape_release
;
1988 dev_set_name(&tape
->dev
, "%s", dev_name(&drive
->gendev
));
1990 if (device_register(&tape
->dev
))
1993 tape
->drive
= drive
;
1994 tape
->driver
= &idetape_driver
;
1997 g
->private_data
= &tape
->driver
;
1999 drive
->driver_data
= tape
;
2001 mutex_lock(&idetape_ref_mutex
);
2002 for (minor
= 0; idetape_devs
[minor
]; minor
++)
2004 idetape_devs
[minor
] = tape
;
2005 mutex_unlock(&idetape_ref_mutex
);
2007 idetape_setup(drive
, tape
, minor
);
2009 device_create(idetape_sysfs_class
, &drive
->gendev
,
2010 MKDEV(IDETAPE_MAJOR
, minor
), NULL
, "%s", tape
->name
);
2011 device_create(idetape_sysfs_class
, &drive
->gendev
,
2012 MKDEV(IDETAPE_MAJOR
, minor
+ 128), NULL
,
2015 g
->fops
= &idetape_block_ops
;
2016 ide_register_region(g
);
2028 static void __exit
idetape_exit(void)
2030 driver_unregister(&idetape_driver
.gen_driver
);
2031 class_destroy(idetape_sysfs_class
);
2032 unregister_chrdev(IDETAPE_MAJOR
, "ht");
2035 static int __init
idetape_init(void)
2038 idetape_sysfs_class
= class_create(THIS_MODULE
, "ide_tape");
2039 if (IS_ERR(idetape_sysfs_class
)) {
2040 idetape_sysfs_class
= NULL
;
2041 printk(KERN_ERR
"Unable to create sysfs class for ide tapes\n");
2046 if (register_chrdev(IDETAPE_MAJOR
, "ht", &idetape_fops
)) {
2047 printk(KERN_ERR
"ide-tape: Failed to register chrdev"
2050 goto out_free_class
;
2053 error
= driver_register(&idetape_driver
.gen_driver
);
2055 goto out_free_driver
;
2060 driver_unregister(&idetape_driver
.gen_driver
);
2062 class_destroy(idetape_sysfs_class
);
2067 MODULE_ALIAS("ide:*m-tape*");
2068 module_init(idetape_init
);
2069 module_exit(idetape_exit
);
2070 MODULE_ALIAS_CHARDEV_MAJOR(IDETAPE_MAJOR
);
2071 MODULE_DESCRIPTION("ATAPI Streaming TAPE Driver");
2072 MODULE_LICENSE("GPL");