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/smp_lock.h>
36 #include <linux/slab.h>
37 #include <linux/pci.h>
38 #include <linux/ide.h>
39 #include <linux/smp_lock.h>
40 #include <linux/completion.h>
41 #include <linux/bitops.h>
42 #include <linux/mutex.h>
43 #include <scsi/scsi.h>
45 #include <asm/byteorder.h>
46 #include <linux/irq.h>
47 #include <linux/uaccess.h>
49 #include <asm/unaligned.h>
50 #include <linux/mtio.h>
52 /* define to see debug info */
53 #undef IDETAPE_DEBUG_LOG
55 #ifdef IDETAPE_DEBUG_LOG
56 #define ide_debug_log(lvl, fmt, args...) __ide_debug_log(lvl, fmt, ## args)
58 #define ide_debug_log(lvl, fmt, args...) do {} while (0)
61 /**************************** Tunable parameters *****************************/
63 * After each failed packet command we issue a request sense command and retry
64 * the packet command IDETAPE_MAX_PC_RETRIES times.
66 * Setting IDETAPE_MAX_PC_RETRIES to 0 will disable retries.
68 #define IDETAPE_MAX_PC_RETRIES 3
71 * The following parameter is used to select the point in the internal tape fifo
72 * in which we will start to refill the buffer. Decreasing the following
73 * parameter will improve the system's latency and interactive response, while
74 * using a high value might improve system throughput.
76 #define IDETAPE_FIFO_THRESHOLD 2
79 * DSC polling parameters.
81 * Polling for DSC (a single bit in the status register) is a very important
82 * function in ide-tape. There are two cases in which we poll for DSC:
84 * 1. Before a read/write packet command, to ensure that we can transfer data
85 * from/to the tape's data buffers, without causing an actual media access.
86 * In case the tape is not ready yet, we take out our request from the device
87 * request queue, so that ide.c could service requests from the other device
88 * on the same interface in the meantime.
90 * 2. After the successful initialization of a "media access packet command",
91 * which is a command that can take a long time to complete (the interval can
92 * range from several seconds to even an hour). Again, we postpone our request
93 * in the middle to free the bus for the other device. The polling frequency
94 * here should be lower than the read/write frequency since those media access
95 * commands are slow. We start from a "fast" frequency - IDETAPE_DSC_MA_FAST
96 * (1 second), and if we don't receive DSC after IDETAPE_DSC_MA_THRESHOLD
97 * (5 min), we switch it to a lower frequency - IDETAPE_DSC_MA_SLOW (1 min).
99 * We also set a timeout for the timer, in case something goes wrong. The
100 * timeout should be longer then the maximum execution time of a tape operation.
104 #define IDETAPE_DSC_RW_MIN 5*HZ/100 /* 50 msec */
105 #define IDETAPE_DSC_RW_MAX 40*HZ/100 /* 400 msec */
106 #define IDETAPE_DSC_RW_TIMEOUT 2*60*HZ /* 2 minutes */
107 #define IDETAPE_DSC_MA_FAST 2*HZ /* 2 seconds */
108 #define IDETAPE_DSC_MA_THRESHOLD 5*60*HZ /* 5 minutes */
109 #define IDETAPE_DSC_MA_SLOW 30*HZ /* 30 seconds */
110 #define IDETAPE_DSC_MA_TIMEOUT 2*60*60*HZ /* 2 hours */
112 /*************************** End of tunable parameters ***********************/
114 /* tape directions */
116 IDETAPE_DIR_NONE
= (1 << 0),
117 IDETAPE_DIR_READ
= (1 << 1),
118 IDETAPE_DIR_WRITE
= (1 << 2),
121 /* Tape door status */
122 #define DOOR_UNLOCKED 0
123 #define DOOR_LOCKED 1
124 #define DOOR_EXPLICITLY_LOCKED 2
126 /* Some defines for the SPACE command */
127 #define IDETAPE_SPACE_OVER_FILEMARK 1
128 #define IDETAPE_SPACE_TO_EOD 3
130 /* Some defines for the LOAD UNLOAD command */
131 #define IDETAPE_LU_LOAD_MASK 1
132 #define IDETAPE_LU_RETENSION_MASK 2
133 #define IDETAPE_LU_EOT_MASK 4
135 /* Structures related to the SELECT SENSE / MODE SENSE packet commands. */
136 #define IDETAPE_BLOCK_DESCRIPTOR 0
137 #define IDETAPE_CAPABILITIES_PAGE 0x2a
140 * Most of our global data which we need to save even as we leave the driver due
141 * to an interrupt or a timer event is stored in the struct defined below.
143 typedef struct ide_tape_obj
{
145 struct ide_driver
*driver
;
146 struct gendisk
*disk
;
149 /* used by REQ_IDETAPE_{READ,WRITE} requests */
150 struct ide_atapi_pc queued_pc
;
153 * DSC polling variables.
155 * While polling for DSC we use postponed_rq to postpone the current
156 * request so that ide.c will be able to service pending requests on the
157 * other device. Note that at most we will have only one DSC (usually
158 * data transfer) request in the device request queue.
162 /* The time in which we started polling for DSC */
163 unsigned long dsc_polling_start
;
164 /* Timer used to poll for dsc */
165 struct timer_list dsc_timer
;
166 /* Read/Write dsc polling frequency */
167 unsigned long best_dsc_rw_freq
;
168 unsigned long dsc_poll_freq
;
169 unsigned long dsc_timeout
;
171 /* Read position information */
174 unsigned int first_frame
;
176 /* Last error information */
177 u8 sense_key
, asc
, ascq
;
179 /* Character device operation */
183 /* Current character device data transfer direction */
186 /* tape block size, usually 512 or 1024 bytes */
187 unsigned short blk_size
;
190 /* Copy of the tape's Capabilities and Mechanical Page */
194 * Active data transfer request parameters.
196 * At most, there is only one ide-tape originated data transfer request
197 * in the device request queue. This allows ide.c to easily service
198 * requests from the other device when we postpone our active request.
201 /* Data buffer size chosen based on the tape's recommendation */
203 /* Staging buffer of buffer_size bytes */
205 /* The read/write cursor */
207 /* The number of valid bytes in buf */
210 /* Measures average tape speed */
211 unsigned long avg_time
;
215 /* the door is currently locked */
217 /* the tape hardware is write protected */
219 /* the tape is write protected (hardware or opened as read-only) */
223 static DEFINE_MUTEX(idetape_ref_mutex
);
225 static DEFINE_MUTEX(idetape_chrdev_mutex
);
227 static struct class *idetape_sysfs_class
;
229 static void ide_tape_release(struct device
*);
231 static struct ide_tape_obj
*idetape_devs
[MAX_HWIFS
* MAX_DRIVES
];
233 static struct ide_tape_obj
*ide_tape_get(struct gendisk
*disk
, bool cdev
,
236 struct ide_tape_obj
*tape
= NULL
;
238 mutex_lock(&idetape_ref_mutex
);
241 tape
= idetape_devs
[i
];
243 tape
= ide_drv_g(disk
, ide_tape_obj
);
246 if (ide_device_get(tape
->drive
))
249 get_device(&tape
->dev
);
252 mutex_unlock(&idetape_ref_mutex
);
256 static void ide_tape_put(struct ide_tape_obj
*tape
)
258 ide_drive_t
*drive
= tape
->drive
;
260 mutex_lock(&idetape_ref_mutex
);
261 put_device(&tape
->dev
);
262 ide_device_put(drive
);
263 mutex_unlock(&idetape_ref_mutex
);
267 * called on each failed packet command retry to analyze the request sense. We
268 * currently do not utilize this information.
270 static void idetape_analyze_error(ide_drive_t
*drive
)
272 idetape_tape_t
*tape
= drive
->driver_data
;
273 struct ide_atapi_pc
*pc
= drive
->failed_pc
;
274 struct request
*rq
= drive
->hwif
->rq
;
275 u8
*sense
= bio_data(rq
->bio
);
277 tape
->sense_key
= sense
[2] & 0xF;
278 tape
->asc
= sense
[12];
279 tape
->ascq
= sense
[13];
281 ide_debug_log(IDE_DBG_FUNC
,
282 "cmd: 0x%x, sense key = %x, asc = %x, ascq = %x",
283 rq
->cmd
[0], tape
->sense_key
, tape
->asc
, tape
->ascq
);
285 /* correct remaining bytes to transfer */
286 if (pc
->flags
& PC_FLAG_DMA_ERROR
)
287 rq
->resid_len
= tape
->blk_size
* get_unaligned_be32(&sense
[3]);
290 * If error was the result of a zero-length read or write command,
291 * with sense key=5, asc=0x22, ascq=0, let it slide. Some drives
292 * (i.e. Seagate STT3401A Travan) don't support 0-length read/writes.
294 if ((pc
->c
[0] == READ_6
|| pc
->c
[0] == WRITE_6
)
296 && pc
->c
[4] == 0 && pc
->c
[3] == 0 && pc
->c
[2] == 0) {
297 if (tape
->sense_key
== 5) {
298 /* don't report an error, everything's ok */
300 /* don't retry read/write */
301 pc
->flags
|= PC_FLAG_ABORT
;
304 if (pc
->c
[0] == READ_6
&& (sense
[2] & 0x80)) {
305 pc
->error
= IDE_DRV_ERROR_FILEMARK
;
306 pc
->flags
|= PC_FLAG_ABORT
;
308 if (pc
->c
[0] == WRITE_6
) {
309 if ((sense
[2] & 0x40) || (tape
->sense_key
== 0xd
310 && tape
->asc
== 0x0 && tape
->ascq
== 0x2)) {
311 pc
->error
= IDE_DRV_ERROR_EOD
;
312 pc
->flags
|= PC_FLAG_ABORT
;
315 if (pc
->c
[0] == READ_6
|| pc
->c
[0] == WRITE_6
) {
316 if (tape
->sense_key
== 8) {
317 pc
->error
= IDE_DRV_ERROR_EOD
;
318 pc
->flags
|= PC_FLAG_ABORT
;
320 if (!(pc
->flags
& PC_FLAG_ABORT
) &&
321 (blk_rq_bytes(rq
) - rq
->resid_len
))
322 pc
->retries
= IDETAPE_MAX_PC_RETRIES
+ 1;
326 static void ide_tape_handle_dsc(ide_drive_t
*);
328 static int ide_tape_callback(ide_drive_t
*drive
, int dsc
)
330 idetape_tape_t
*tape
= drive
->driver_data
;
331 struct ide_atapi_pc
*pc
= drive
->pc
;
332 struct request
*rq
= drive
->hwif
->rq
;
333 int uptodate
= pc
->error
? 0 : 1;
334 int err
= uptodate
? 0 : IDE_DRV_ERROR_GENERAL
;
336 ide_debug_log(IDE_DBG_FUNC
, "cmd: 0x%x, dsc: %d, err: %d", rq
->cmd
[0],
340 ide_tape_handle_dsc(drive
);
342 if (drive
->failed_pc
== pc
)
343 drive
->failed_pc
= NULL
;
345 if (pc
->c
[0] == REQUEST_SENSE
) {
347 idetape_analyze_error(drive
);
349 printk(KERN_ERR
"ide-tape: Error in REQUEST SENSE "
350 "itself - Aborting request!\n");
351 } else if (pc
->c
[0] == READ_6
|| pc
->c
[0] == WRITE_6
) {
352 unsigned int blocks
=
353 (blk_rq_bytes(rq
) - rq
->resid_len
) / tape
->blk_size
;
355 tape
->avg_size
+= blocks
* tape
->blk_size
;
357 if (time_after_eq(jiffies
, tape
->avg_time
+ HZ
)) {
358 tape
->avg_speed
= tape
->avg_size
* HZ
/
359 (jiffies
- tape
->avg_time
) / 1024;
361 tape
->avg_time
= jiffies
;
364 tape
->first_frame
+= blocks
;
377 * Postpone the current request so that ide.c will be able to service requests
378 * from another device on the same port while we are polling for DSC.
380 static void ide_tape_stall_queue(ide_drive_t
*drive
)
382 idetape_tape_t
*tape
= drive
->driver_data
;
384 ide_debug_log(IDE_DBG_FUNC
, "cmd: 0x%x, dsc_poll_freq: %lu",
385 drive
->hwif
->rq
->cmd
[0], tape
->dsc_poll_freq
);
387 tape
->postponed_rq
= true;
389 ide_stall_queue(drive
, tape
->dsc_poll_freq
);
392 static void ide_tape_handle_dsc(ide_drive_t
*drive
)
394 idetape_tape_t
*tape
= drive
->driver_data
;
396 /* Media access command */
397 tape
->dsc_polling_start
= jiffies
;
398 tape
->dsc_poll_freq
= IDETAPE_DSC_MA_FAST
;
399 tape
->dsc_timeout
= jiffies
+ IDETAPE_DSC_MA_TIMEOUT
;
400 /* Allow ide.c to handle other requests */
401 ide_tape_stall_queue(drive
);
405 * Packet Command Interface
407 * The current Packet Command is available in drive->pc, and will not change
408 * until we finish handling it. Each packet command is associated with a
409 * callback function that will be called when the command is finished.
411 * The handling will be done in three stages:
413 * 1. ide_tape_issue_pc will send the packet command to the drive, and will set
414 * the interrupt handler to ide_pc_intr.
416 * 2. On each interrupt, ide_pc_intr will be called. This step will be
417 * repeated until the device signals us that no more interrupts will be issued.
419 * 3. ATAPI Tape media access commands have immediate status with a delayed
420 * process. In case of a successful initiation of a media access packet command,
421 * the DSC bit will be set when the actual execution of the command is finished.
422 * Since the tape drive will not issue an interrupt, we have to poll for this
423 * event. In this case, we define the request as "low priority request" by
424 * setting rq_status to IDETAPE_RQ_POSTPONED, set a timer to poll for DSC and
427 * ide.c will then give higher priority to requests which originate from the
428 * other device, until will change rq_status to RQ_ACTIVE.
430 * 4. When the packet command is finished, it will be checked for errors.
432 * 5. In case an error was found, we queue a request sense packet command in
433 * front of the request queue and retry the operation up to
434 * IDETAPE_MAX_PC_RETRIES times.
436 * 6. In case no error was found, or we decided to give up and not to retry
437 * again, the callback function will be called and then we will handle the next
441 static ide_startstop_t
ide_tape_issue_pc(ide_drive_t
*drive
,
443 struct ide_atapi_pc
*pc
)
445 idetape_tape_t
*tape
= drive
->driver_data
;
446 struct request
*rq
= drive
->hwif
->rq
;
448 if (drive
->failed_pc
== NULL
&& pc
->c
[0] != REQUEST_SENSE
)
449 drive
->failed_pc
= pc
;
451 /* Set the current packet command */
454 if (pc
->retries
> IDETAPE_MAX_PC_RETRIES
||
455 (pc
->flags
& PC_FLAG_ABORT
)) {
458 * We will "abort" retrying a packet command in case legitimate
459 * error code was received (crossing a filemark, or end of the
460 * media, for example).
462 if (!(pc
->flags
& PC_FLAG_ABORT
)) {
463 if (!(pc
->c
[0] == TEST_UNIT_READY
&&
464 tape
->sense_key
== 2 && tape
->asc
== 4 &&
465 (tape
->ascq
== 1 || tape
->ascq
== 8))) {
466 printk(KERN_ERR
"ide-tape: %s: I/O error, "
467 "pc = %2x, key = %2x, "
468 "asc = %2x, ascq = %2x\n",
469 tape
->name
, pc
->c
[0],
470 tape
->sense_key
, tape
->asc
,
474 pc
->error
= IDE_DRV_ERROR_GENERAL
;
477 drive
->failed_pc
= NULL
;
478 drive
->pc_callback(drive
, 0);
479 ide_complete_rq(drive
, -EIO
, blk_rq_bytes(rq
));
482 ide_debug_log(IDE_DBG_SENSE
, "retry #%d, cmd: 0x%02x", pc
->retries
,
487 return ide_issue_pc(drive
, cmd
);
490 /* A mode sense command is used to "sense" tape parameters. */
491 static void idetape_create_mode_sense_cmd(struct ide_atapi_pc
*pc
, u8 page_code
)
494 pc
->c
[0] = MODE_SENSE
;
495 if (page_code
!= IDETAPE_BLOCK_DESCRIPTOR
)
496 /* DBD = 1 - Don't return block descriptors */
498 pc
->c
[2] = page_code
;
500 * Changed pc->c[3] to 0 (255 will at best return unused info).
502 * For SCSI this byte is defined as subpage instead of high byte
503 * of length and some IDE drives seem to interpret it this way
504 * and return an error when 255 is used.
507 /* We will just discard data in that case */
509 if (page_code
== IDETAPE_BLOCK_DESCRIPTOR
)
511 else if (page_code
== IDETAPE_CAPABILITIES_PAGE
)
517 static ide_startstop_t
idetape_media_access_finished(ide_drive_t
*drive
)
519 ide_hwif_t
*hwif
= drive
->hwif
;
520 idetape_tape_t
*tape
= drive
->driver_data
;
521 struct ide_atapi_pc
*pc
= drive
->pc
;
524 stat
= hwif
->tp_ops
->read_status(hwif
);
526 if (stat
& ATA_DSC
) {
527 if (stat
& ATA_ERR
) {
529 if (pc
->c
[0] != TEST_UNIT_READY
)
530 printk(KERN_ERR
"ide-tape: %s: I/O error, ",
532 /* Retry operation */
538 pc
->error
= IDE_DRV_ERROR_GENERAL
;
539 drive
->failed_pc
= NULL
;
541 drive
->pc_callback(drive
, 0);
545 static void ide_tape_create_rw_cmd(idetape_tape_t
*tape
,
546 struct ide_atapi_pc
*pc
, struct request
*rq
,
549 unsigned int length
= blk_rq_sectors(rq
) / (tape
->blk_size
>> 9);
552 put_unaligned(cpu_to_be32(length
), (unsigned int *) &pc
->c
[1]);
555 if (blk_rq_bytes(rq
) == tape
->buffer_size
)
556 pc
->flags
|= PC_FLAG_DMA_OK
;
558 if (opcode
== READ_6
)
560 else if (opcode
== WRITE_6
) {
562 pc
->flags
|= PC_FLAG_WRITING
;
565 memcpy(rq
->cmd
, pc
->c
, 12);
568 static ide_startstop_t
idetape_do_request(ide_drive_t
*drive
,
569 struct request
*rq
, sector_t block
)
571 ide_hwif_t
*hwif
= drive
->hwif
;
572 idetape_tape_t
*tape
= drive
->driver_data
;
573 struct ide_atapi_pc
*pc
= NULL
;
577 ide_debug_log(IDE_DBG_RQ
, "cmd: 0x%x, sector: %llu, nr_sectors: %u",
578 rq
->cmd
[0], (unsigned long long)blk_rq_pos(rq
),
581 BUG_ON(!(rq
->cmd_type
== REQ_TYPE_SPECIAL
||
582 rq
->cmd_type
== REQ_TYPE_SENSE
));
584 /* Retry a failed packet command */
585 if (drive
->failed_pc
&& drive
->pc
->c
[0] == REQUEST_SENSE
) {
586 pc
= drive
->failed_pc
;
591 * If the tape is still busy, postpone our request and service
592 * the other device meanwhile.
594 stat
= hwif
->tp_ops
->read_status(hwif
);
596 if ((drive
->dev_flags
& IDE_DFLAG_DSC_OVERLAP
) == 0 &&
597 (rq
->cmd
[13] & REQ_IDETAPE_PC2
) == 0)
598 drive
->atapi_flags
|= IDE_AFLAG_IGNORE_DSC
;
600 if (drive
->dev_flags
& IDE_DFLAG_POST_RESET
) {
601 drive
->atapi_flags
|= IDE_AFLAG_IGNORE_DSC
;
602 drive
->dev_flags
&= ~IDE_DFLAG_POST_RESET
;
605 if (!(drive
->atapi_flags
& IDE_AFLAG_IGNORE_DSC
) &&
607 if (!tape
->postponed_rq
) {
608 tape
->dsc_polling_start
= jiffies
;
609 tape
->dsc_poll_freq
= tape
->best_dsc_rw_freq
;
610 tape
->dsc_timeout
= jiffies
+ IDETAPE_DSC_RW_TIMEOUT
;
611 } else if (time_after(jiffies
, tape
->dsc_timeout
)) {
612 printk(KERN_ERR
"ide-tape: %s: DSC timeout\n",
614 if (rq
->cmd
[13] & REQ_IDETAPE_PC2
) {
615 idetape_media_access_finished(drive
);
618 return ide_do_reset(drive
);
620 } else if (time_after(jiffies
,
621 tape
->dsc_polling_start
+
622 IDETAPE_DSC_MA_THRESHOLD
))
623 tape
->dsc_poll_freq
= IDETAPE_DSC_MA_SLOW
;
624 ide_tape_stall_queue(drive
);
627 drive
->atapi_flags
&= ~IDE_AFLAG_IGNORE_DSC
;
628 tape
->postponed_rq
= false;
631 if (rq
->cmd
[13] & REQ_IDETAPE_READ
) {
632 pc
= &tape
->queued_pc
;
633 ide_tape_create_rw_cmd(tape
, pc
, rq
, READ_6
);
636 if (rq
->cmd
[13] & REQ_IDETAPE_WRITE
) {
637 pc
= &tape
->queued_pc
;
638 ide_tape_create_rw_cmd(tape
, pc
, rq
, WRITE_6
);
641 if (rq
->cmd
[13] & REQ_IDETAPE_PC1
) {
642 pc
= (struct ide_atapi_pc
*)rq
->special
;
643 rq
->cmd
[13] &= ~(REQ_IDETAPE_PC1
);
644 rq
->cmd
[13] |= REQ_IDETAPE_PC2
;
647 if (rq
->cmd
[13] & REQ_IDETAPE_PC2
) {
648 idetape_media_access_finished(drive
);
654 /* prepare sense request for this command */
655 ide_prep_sense(drive
, rq
);
657 memset(&cmd
, 0, sizeof(cmd
));
660 cmd
.tf_flags
|= IDE_TFLAG_WRITE
;
664 ide_init_sg_cmd(&cmd
, blk_rq_bytes(rq
));
665 ide_map_sg(drive
, &cmd
);
667 return ide_tape_issue_pc(drive
, &cmd
, pc
);
671 * Write a filemark if write_filemark=1. Flush the device buffers without
672 * writing a filemark otherwise.
674 static void idetape_create_write_filemark_cmd(ide_drive_t
*drive
,
675 struct ide_atapi_pc
*pc
, int write_filemark
)
678 pc
->c
[0] = WRITE_FILEMARKS
;
679 pc
->c
[4] = write_filemark
;
680 pc
->flags
|= PC_FLAG_WAIT_FOR_DSC
;
683 static int idetape_wait_ready(ide_drive_t
*drive
, unsigned long timeout
)
685 idetape_tape_t
*tape
= drive
->driver_data
;
686 struct gendisk
*disk
= tape
->disk
;
687 int load_attempted
= 0;
689 /* Wait for the tape to become ready */
690 set_bit(ilog2(IDE_AFLAG_MEDIUM_PRESENT
), &drive
->atapi_flags
);
692 while (time_before(jiffies
, timeout
)) {
693 if (ide_do_test_unit_ready(drive
, disk
) == 0)
695 if ((tape
->sense_key
== 2 && tape
->asc
== 4 && tape
->ascq
== 2)
696 || (tape
->asc
== 0x3A)) {
700 ide_do_start_stop(drive
, disk
, IDETAPE_LU_LOAD_MASK
);
702 /* not about to be ready */
703 } else if (!(tape
->sense_key
== 2 && tape
->asc
== 4 &&
704 (tape
->ascq
== 1 || tape
->ascq
== 8)))
711 static int idetape_flush_tape_buffers(ide_drive_t
*drive
)
713 struct ide_tape_obj
*tape
= drive
->driver_data
;
714 struct ide_atapi_pc pc
;
717 idetape_create_write_filemark_cmd(drive
, &pc
, 0);
718 rc
= ide_queue_pc_tail(drive
, tape
->disk
, &pc
, NULL
, 0);
721 idetape_wait_ready(drive
, 60 * 5 * HZ
);
725 static int ide_tape_read_position(ide_drive_t
*drive
)
727 idetape_tape_t
*tape
= drive
->driver_data
;
728 struct ide_atapi_pc pc
;
731 ide_debug_log(IDE_DBG_FUNC
, "enter");
735 pc
.c
[0] = READ_POSITION
;
738 if (ide_queue_pc_tail(drive
, tape
->disk
, &pc
, buf
, pc
.req_xfer
))
742 ide_debug_log(IDE_DBG_FUNC
, "BOP - %s",
743 (buf
[0] & 0x80) ? "Yes" : "No");
744 ide_debug_log(IDE_DBG_FUNC
, "EOP - %s",
745 (buf
[0] & 0x40) ? "Yes" : "No");
748 printk(KERN_INFO
"ide-tape: Block location is unknown"
750 clear_bit(ilog2(IDE_AFLAG_ADDRESS_VALID
),
751 &drive
->atapi_flags
);
754 ide_debug_log(IDE_DBG_FUNC
, "Block Location: %u",
755 be32_to_cpup((__be32
*)&buf
[4]));
757 tape
->partition
= buf
[1];
758 tape
->first_frame
= be32_to_cpup((__be32
*)&buf
[4]);
759 set_bit(ilog2(IDE_AFLAG_ADDRESS_VALID
),
760 &drive
->atapi_flags
);
764 return tape
->first_frame
;
767 static void idetape_create_locate_cmd(ide_drive_t
*drive
,
768 struct ide_atapi_pc
*pc
,
769 unsigned int block
, u8 partition
, int skip
)
772 pc
->c
[0] = POSITION_TO_ELEMENT
;
774 put_unaligned(cpu_to_be32(block
), (unsigned int *) &pc
->c
[3]);
775 pc
->c
[8] = partition
;
776 pc
->flags
|= PC_FLAG_WAIT_FOR_DSC
;
779 static void __ide_tape_discard_merge_buffer(ide_drive_t
*drive
)
781 idetape_tape_t
*tape
= drive
->driver_data
;
783 if (tape
->chrdev_dir
!= IDETAPE_DIR_READ
)
786 clear_bit(ilog2(IDE_AFLAG_FILEMARK
), &drive
->atapi_flags
);
788 if (tape
->buf
!= NULL
) {
793 tape
->chrdev_dir
= IDETAPE_DIR_NONE
;
797 * Position the tape to the requested block using the LOCATE packet command.
798 * A READ POSITION command is then issued to check where we are positioned. Like
799 * all higher level operations, we queue the commands at the tail of the request
800 * queue and wait for their completion.
802 static int idetape_position_tape(ide_drive_t
*drive
, unsigned int block
,
803 u8 partition
, int skip
)
805 idetape_tape_t
*tape
= drive
->driver_data
;
806 struct gendisk
*disk
= tape
->disk
;
808 struct ide_atapi_pc pc
;
810 if (tape
->chrdev_dir
== IDETAPE_DIR_READ
)
811 __ide_tape_discard_merge_buffer(drive
);
812 idetape_wait_ready(drive
, 60 * 5 * HZ
);
813 idetape_create_locate_cmd(drive
, &pc
, block
, partition
, skip
);
814 ret
= ide_queue_pc_tail(drive
, disk
, &pc
, NULL
, 0);
818 ret
= ide_tape_read_position(drive
);
824 static void ide_tape_discard_merge_buffer(ide_drive_t
*drive
,
825 int restore_position
)
827 idetape_tape_t
*tape
= drive
->driver_data
;
830 __ide_tape_discard_merge_buffer(drive
);
831 if (restore_position
) {
832 position
= ide_tape_read_position(drive
);
833 seek
= position
> 0 ? position
: 0;
834 if (idetape_position_tape(drive
, seek
, 0, 0)) {
835 printk(KERN_INFO
"ide-tape: %s: position_tape failed in"
836 " %s\n", tape
->name
, __func__
);
843 * Generate a read/write request for the block device interface and wait for it
846 static int idetape_queue_rw_tail(ide_drive_t
*drive
, int cmd
, int size
)
848 idetape_tape_t
*tape
= drive
->driver_data
;
852 ide_debug_log(IDE_DBG_FUNC
, "cmd: 0x%x, size: %d", cmd
, size
);
854 BUG_ON(cmd
!= REQ_IDETAPE_READ
&& cmd
!= REQ_IDETAPE_WRITE
);
855 BUG_ON(size
< 0 || size
% tape
->blk_size
);
857 rq
= blk_get_request(drive
->queue
, READ
, __GFP_WAIT
);
858 rq
->cmd_type
= REQ_TYPE_SPECIAL
;
860 rq
->rq_disk
= tape
->disk
;
861 rq
->__sector
= tape
->first_frame
;
864 ret
= blk_rq_map_kern(drive
->queue
, rq
, tape
->buf
, size
,
870 blk_execute_rq(drive
->queue
, tape
->disk
, rq
, 0);
872 /* calculate the number of transferred bytes and update buffer state */
873 size
-= rq
->resid_len
;
874 tape
->cur
= tape
->buf
;
875 if (cmd
== REQ_IDETAPE_READ
)
881 if (rq
->errors
== IDE_DRV_ERROR_GENERAL
)
888 static void idetape_create_inquiry_cmd(struct ide_atapi_pc
*pc
)
896 static void idetape_create_rewind_cmd(ide_drive_t
*drive
,
897 struct ide_atapi_pc
*pc
)
900 pc
->c
[0] = REZERO_UNIT
;
901 pc
->flags
|= PC_FLAG_WAIT_FOR_DSC
;
904 static void idetape_create_erase_cmd(struct ide_atapi_pc
*pc
)
909 pc
->flags
|= PC_FLAG_WAIT_FOR_DSC
;
912 static void idetape_create_space_cmd(struct ide_atapi_pc
*pc
, int count
, u8 cmd
)
916 put_unaligned(cpu_to_be32(count
), (unsigned int *) &pc
->c
[1]);
918 pc
->flags
|= PC_FLAG_WAIT_FOR_DSC
;
921 static void ide_tape_flush_merge_buffer(ide_drive_t
*drive
)
923 idetape_tape_t
*tape
= drive
->driver_data
;
925 if (tape
->chrdev_dir
!= IDETAPE_DIR_WRITE
) {
926 printk(KERN_ERR
"ide-tape: bug: Trying to empty merge buffer"
927 " but we are not writing.\n");
931 size_t aligned
= roundup(tape
->valid
, tape
->blk_size
);
933 memset(tape
->cur
, 0, aligned
- tape
->valid
);
934 idetape_queue_rw_tail(drive
, REQ_IDETAPE_WRITE
, aligned
);
938 tape
->chrdev_dir
= IDETAPE_DIR_NONE
;
941 static int idetape_init_rw(ide_drive_t
*drive
, int dir
)
943 idetape_tape_t
*tape
= drive
->driver_data
;
946 BUG_ON(dir
!= IDETAPE_DIR_READ
&& dir
!= IDETAPE_DIR_WRITE
);
948 if (tape
->chrdev_dir
== dir
)
951 if (tape
->chrdev_dir
== IDETAPE_DIR_READ
)
952 ide_tape_discard_merge_buffer(drive
, 1);
953 else if (tape
->chrdev_dir
== IDETAPE_DIR_WRITE
) {
954 ide_tape_flush_merge_buffer(drive
);
955 idetape_flush_tape_buffers(drive
);
958 if (tape
->buf
|| tape
->valid
) {
959 printk(KERN_ERR
"ide-tape: valid should be 0 now\n");
963 tape
->buf
= kmalloc(tape
->buffer_size
, GFP_KERNEL
);
966 tape
->chrdev_dir
= dir
;
967 tape
->cur
= tape
->buf
;
970 * Issue a 0 rw command to ensure that DSC handshake is
971 * switched from completion mode to buffer available mode. No
972 * point in issuing this if DSC overlap isn't supported, some
973 * drives (Seagate STT3401A) will return an error.
975 if (drive
->dev_flags
& IDE_DFLAG_DSC_OVERLAP
) {
976 int cmd
= dir
== IDETAPE_DIR_READ
? REQ_IDETAPE_READ
979 rc
= idetape_queue_rw_tail(drive
, cmd
, 0);
983 tape
->chrdev_dir
= IDETAPE_DIR_NONE
;
991 static void idetape_pad_zeros(ide_drive_t
*drive
, int bcount
)
993 idetape_tape_t
*tape
= drive
->driver_data
;
995 memset(tape
->buf
, 0, tape
->buffer_size
);
998 unsigned int count
= min(tape
->buffer_size
, bcount
);
1000 idetape_queue_rw_tail(drive
, REQ_IDETAPE_WRITE
, count
);
1006 * Rewinds the tape to the Beginning Of the current Partition (BOP). We
1007 * currently support only one partition.
1009 static int idetape_rewind_tape(ide_drive_t
*drive
)
1011 struct ide_tape_obj
*tape
= drive
->driver_data
;
1012 struct gendisk
*disk
= tape
->disk
;
1013 struct ide_atapi_pc pc
;
1016 ide_debug_log(IDE_DBG_FUNC
, "enter");
1018 idetape_create_rewind_cmd(drive
, &pc
);
1019 ret
= ide_queue_pc_tail(drive
, disk
, &pc
, NULL
, 0);
1023 ret
= ide_tape_read_position(drive
);
1029 /* mtio.h compatible commands should be issued to the chrdev interface. */
1030 static int idetape_blkdev_ioctl(ide_drive_t
*drive
, unsigned int cmd
,
1033 idetape_tape_t
*tape
= drive
->driver_data
;
1034 void __user
*argp
= (void __user
*)arg
;
1036 struct idetape_config
{
1037 int dsc_rw_frequency
;
1038 int dsc_media_access_frequency
;
1042 ide_debug_log(IDE_DBG_FUNC
, "cmd: 0x%04x", cmd
);
1046 if (copy_from_user(&config
, argp
, sizeof(config
)))
1048 tape
->best_dsc_rw_freq
= config
.dsc_rw_frequency
;
1051 memset(&config
, 0, sizeof(config
));
1052 config
.dsc_rw_frequency
= (int) tape
->best_dsc_rw_freq
;
1053 config
.nr_stages
= 1;
1054 if (copy_to_user(argp
, &config
, sizeof(config
)))
1063 static int idetape_space_over_filemarks(ide_drive_t
*drive
, short mt_op
,
1066 idetape_tape_t
*tape
= drive
->driver_data
;
1067 struct gendisk
*disk
= tape
->disk
;
1068 struct ide_atapi_pc pc
;
1069 int retval
, count
= 0;
1070 int sprev
= !!(tape
->caps
[4] & 0x20);
1073 ide_debug_log(IDE_DBG_FUNC
, "mt_op: %d, mt_count: %d", mt_op
, mt_count
);
1077 if (MTBSF
== mt_op
|| MTBSFM
== mt_op
) {
1080 mt_count
= -mt_count
;
1083 if (tape
->chrdev_dir
== IDETAPE_DIR_READ
) {
1085 if (test_and_clear_bit(ilog2(IDE_AFLAG_FILEMARK
),
1086 &drive
->atapi_flags
))
1088 ide_tape_discard_merge_buffer(drive
, 0);
1094 idetape_create_space_cmd(&pc
, mt_count
- count
,
1095 IDETAPE_SPACE_OVER_FILEMARK
);
1096 return ide_queue_pc_tail(drive
, disk
, &pc
, NULL
, 0);
1101 retval
= idetape_space_over_filemarks(drive
, MTFSF
,
1105 count
= (MTBSFM
== mt_op
? 1 : -1);
1106 return idetape_space_over_filemarks(drive
, MTFSF
, count
);
1108 printk(KERN_ERR
"ide-tape: MTIO operation %d not supported\n",
1115 * Our character device read / write functions.
1117 * The tape is optimized to maximize throughput when it is transferring an
1118 * integral number of the "continuous transfer limit", which is a parameter of
1119 * the specific tape (26kB on my particular tape, 32kB for Onstream).
1121 * As of version 1.3 of the driver, the character device provides an abstract
1122 * continuous view of the media - any mix of block sizes (even 1 byte) on the
1123 * same backup/restore procedure is supported. The driver will internally
1124 * convert the requests to the recommended transfer unit, so that an unmatch
1125 * between the user's block size to the recommended size will only result in a
1126 * (slightly) increased driver overhead, but will no longer hit performance.
1127 * This is not applicable to Onstream.
1129 static ssize_t
idetape_chrdev_read(struct file
*file
, char __user
*buf
,
1130 size_t count
, loff_t
*ppos
)
1132 struct ide_tape_obj
*tape
= file
->private_data
;
1133 ide_drive_t
*drive
= tape
->drive
;
1138 ide_debug_log(IDE_DBG_FUNC
, "count %Zd", count
);
1140 if (tape
->chrdev_dir
!= IDETAPE_DIR_READ
) {
1141 if (test_bit(ilog2(IDE_AFLAG_DETECT_BS
), &drive
->atapi_flags
))
1142 if (count
> tape
->blk_size
&&
1143 (count
% tape
->blk_size
) == 0)
1144 tape
->user_bs_factor
= count
/ tape
->blk_size
;
1147 rc
= idetape_init_rw(drive
, IDETAPE_DIR_READ
);
1151 while (done
< count
) {
1154 /* refill if staging buffer is empty */
1156 /* If we are at a filemark, nothing more to read */
1157 if (test_bit(ilog2(IDE_AFLAG_FILEMARK
),
1158 &drive
->atapi_flags
))
1161 if (idetape_queue_rw_tail(drive
, REQ_IDETAPE_READ
,
1162 tape
->buffer_size
) <= 0)
1167 todo
= min_t(size_t, count
- done
, tape
->valid
);
1168 if (copy_to_user(buf
+ done
, tape
->cur
, todo
))
1172 tape
->valid
-= todo
;
1176 if (!done
&& test_bit(ilog2(IDE_AFLAG_FILEMARK
), &drive
->atapi_flags
)) {
1177 idetape_space_over_filemarks(drive
, MTFSF
, 1);
1181 return ret
? ret
: done
;
1184 static ssize_t
idetape_chrdev_write(struct file
*file
, const char __user
*buf
,
1185 size_t count
, loff_t
*ppos
)
1187 struct ide_tape_obj
*tape
= file
->private_data
;
1188 ide_drive_t
*drive
= tape
->drive
;
1193 /* The drive is write protected. */
1194 if (tape
->write_prot
)
1197 ide_debug_log(IDE_DBG_FUNC
, "count %Zd", count
);
1199 /* Initialize write operation */
1200 rc
= idetape_init_rw(drive
, IDETAPE_DIR_WRITE
);
1204 while (done
< count
) {
1207 /* flush if staging buffer is full */
1208 if (tape
->valid
== tape
->buffer_size
&&
1209 idetape_queue_rw_tail(drive
, REQ_IDETAPE_WRITE
,
1210 tape
->buffer_size
) <= 0)
1214 todo
= min_t(size_t, count
- done
,
1215 tape
->buffer_size
- tape
->valid
);
1216 if (copy_from_user(tape
->cur
, buf
+ done
, todo
))
1220 tape
->valid
+= todo
;
1224 return ret
? ret
: done
;
1227 static int idetape_write_filemark(ide_drive_t
*drive
)
1229 struct ide_tape_obj
*tape
= drive
->driver_data
;
1230 struct ide_atapi_pc pc
;
1232 /* Write a filemark */
1233 idetape_create_write_filemark_cmd(drive
, &pc
, 1);
1234 if (ide_queue_pc_tail(drive
, tape
->disk
, &pc
, NULL
, 0)) {
1235 printk(KERN_ERR
"ide-tape: Couldn't write a filemark\n");
1242 * Called from idetape_chrdev_ioctl when the general mtio MTIOCTOP ioctl is
1245 * Note: MTBSF and MTBSFM are not supported when the tape doesn't support
1246 * spacing over filemarks in the reverse direction. In this case, MTFSFM is also
1247 * usually not supported.
1249 * The following commands are currently not supported:
1251 * MTFSS, MTBSS, MTWSM, MTSETDENSITY, MTSETDRVBUFFER, MT_ST_BOOLEANS,
1252 * MT_ST_WRITE_THRESHOLD.
1254 static int idetape_mtioctop(ide_drive_t
*drive
, short mt_op
, int mt_count
)
1256 idetape_tape_t
*tape
= drive
->driver_data
;
1257 struct gendisk
*disk
= tape
->disk
;
1258 struct ide_atapi_pc pc
;
1261 ide_debug_log(IDE_DBG_FUNC
, "MTIOCTOP ioctl: mt_op: %d, mt_count: %d",
1271 return idetape_space_over_filemarks(drive
, mt_op
, mt_count
);
1278 if (tape
->write_prot
)
1280 ide_tape_discard_merge_buffer(drive
, 1);
1281 for (i
= 0; i
< mt_count
; i
++) {
1282 retval
= idetape_write_filemark(drive
);
1288 ide_tape_discard_merge_buffer(drive
, 0);
1289 if (idetape_rewind_tape(drive
))
1293 ide_tape_discard_merge_buffer(drive
, 0);
1294 return ide_do_start_stop(drive
, disk
, IDETAPE_LU_LOAD_MASK
);
1298 * If door is locked, attempt to unlock before
1299 * attempting to eject.
1301 if (tape
->door_locked
) {
1302 if (!ide_set_media_lock(drive
, disk
, 0))
1303 tape
->door_locked
= DOOR_UNLOCKED
;
1305 ide_tape_discard_merge_buffer(drive
, 0);
1306 retval
= ide_do_start_stop(drive
, disk
, !IDETAPE_LU_LOAD_MASK
);
1308 clear_bit(ilog2(IDE_AFLAG_MEDIUM_PRESENT
),
1309 &drive
->atapi_flags
);
1312 ide_tape_discard_merge_buffer(drive
, 0);
1313 return idetape_flush_tape_buffers(drive
);
1315 ide_tape_discard_merge_buffer(drive
, 0);
1316 return ide_do_start_stop(drive
, disk
,
1317 IDETAPE_LU_RETENSION_MASK
| IDETAPE_LU_LOAD_MASK
);
1319 idetape_create_space_cmd(&pc
, 0, IDETAPE_SPACE_TO_EOD
);
1320 return ide_queue_pc_tail(drive
, disk
, &pc
, NULL
, 0);
1322 (void)idetape_rewind_tape(drive
);
1323 idetape_create_erase_cmd(&pc
);
1324 return ide_queue_pc_tail(drive
, disk
, &pc
, NULL
, 0);
1327 if (mt_count
< tape
->blk_size
||
1328 mt_count
% tape
->blk_size
)
1330 tape
->user_bs_factor
= mt_count
/ tape
->blk_size
;
1331 clear_bit(ilog2(IDE_AFLAG_DETECT_BS
),
1332 &drive
->atapi_flags
);
1334 set_bit(ilog2(IDE_AFLAG_DETECT_BS
),
1335 &drive
->atapi_flags
);
1338 ide_tape_discard_merge_buffer(drive
, 0);
1339 return idetape_position_tape(drive
,
1340 mt_count
* tape
->user_bs_factor
, tape
->partition
, 0);
1342 ide_tape_discard_merge_buffer(drive
, 0);
1343 return idetape_position_tape(drive
, 0, mt_count
, 0);
1347 retval
= ide_set_media_lock(drive
, disk
, 1);
1350 tape
->door_locked
= DOOR_EXPLICITLY_LOCKED
;
1353 retval
= ide_set_media_lock(drive
, disk
, 0);
1356 tape
->door_locked
= DOOR_UNLOCKED
;
1359 printk(KERN_ERR
"ide-tape: MTIO operation %d not supported\n",
1366 * Our character device ioctls. General mtio.h magnetic io commands are
1367 * supported here, and not in the corresponding block interface. Our own
1368 * ide-tape ioctls are supported on both interfaces.
1370 static long do_idetape_chrdev_ioctl(struct file
*file
,
1371 unsigned int cmd
, unsigned long arg
)
1373 struct ide_tape_obj
*tape
= file
->private_data
;
1374 ide_drive_t
*drive
= tape
->drive
;
1378 int block_offset
= 0, position
= tape
->first_frame
;
1379 void __user
*argp
= (void __user
*)arg
;
1381 ide_debug_log(IDE_DBG_FUNC
, "cmd: 0x%x", cmd
);
1383 if (tape
->chrdev_dir
== IDETAPE_DIR_WRITE
) {
1384 ide_tape_flush_merge_buffer(drive
);
1385 idetape_flush_tape_buffers(drive
);
1387 if (cmd
== MTIOCGET
|| cmd
== MTIOCPOS
) {
1388 block_offset
= tape
->valid
/
1389 (tape
->blk_size
* tape
->user_bs_factor
);
1390 position
= ide_tape_read_position(drive
);
1396 if (copy_from_user(&mtop
, argp
, sizeof(struct mtop
)))
1398 return idetape_mtioctop(drive
, mtop
.mt_op
, mtop
.mt_count
);
1400 memset(&mtget
, 0, sizeof(struct mtget
));
1401 mtget
.mt_type
= MT_ISSCSI2
;
1402 mtget
.mt_blkno
= position
/ tape
->user_bs_factor
- block_offset
;
1404 ((tape
->blk_size
* tape
->user_bs_factor
)
1405 << MT_ST_BLKSIZE_SHIFT
) & MT_ST_BLKSIZE_MASK
;
1407 if (tape
->drv_write_prot
)
1408 mtget
.mt_gstat
|= GMT_WR_PROT(0xffffffff);
1410 if (copy_to_user(argp
, &mtget
, sizeof(struct mtget
)))
1414 mtpos
.mt_blkno
= position
/ tape
->user_bs_factor
- block_offset
;
1415 if (copy_to_user(argp
, &mtpos
, sizeof(struct mtpos
)))
1419 if (tape
->chrdev_dir
== IDETAPE_DIR_READ
)
1420 ide_tape_discard_merge_buffer(drive
, 1);
1421 return idetape_blkdev_ioctl(drive
, cmd
, arg
);
1425 static long idetape_chrdev_ioctl(struct file
*file
,
1426 unsigned int cmd
, unsigned long arg
)
1430 ret
= do_idetape_chrdev_ioctl(file
, cmd
, arg
);
1436 * Do a mode sense page 0 with block descriptor and if it succeeds set the tape
1437 * block size with the reported value.
1439 static void ide_tape_get_bsize_from_bdesc(ide_drive_t
*drive
)
1441 idetape_tape_t
*tape
= drive
->driver_data
;
1442 struct ide_atapi_pc pc
;
1445 idetape_create_mode_sense_cmd(&pc
, IDETAPE_BLOCK_DESCRIPTOR
);
1446 if (ide_queue_pc_tail(drive
, tape
->disk
, &pc
, buf
, pc
.req_xfer
)) {
1447 printk(KERN_ERR
"ide-tape: Can't get block descriptor\n");
1448 if (tape
->blk_size
== 0) {
1449 printk(KERN_WARNING
"ide-tape: Cannot deal with zero "
1450 "block size, assuming 32k\n");
1451 tape
->blk_size
= 32768;
1455 tape
->blk_size
= (buf
[4 + 5] << 16) +
1458 tape
->drv_write_prot
= (buf
[2] & 0x80) >> 7;
1460 ide_debug_log(IDE_DBG_FUNC
, "blk_size: %d, write_prot: %d",
1461 tape
->blk_size
, tape
->drv_write_prot
);
1464 static int idetape_chrdev_open(struct inode
*inode
, struct file
*filp
)
1466 unsigned int minor
= iminor(inode
), i
= minor
& ~0xc0;
1468 idetape_tape_t
*tape
;
1471 if (i
>= MAX_HWIFS
* MAX_DRIVES
)
1474 mutex_lock(&idetape_chrdev_mutex
);
1476 tape
= ide_tape_get(NULL
, true, i
);
1478 mutex_unlock(&idetape_chrdev_mutex
);
1482 drive
= tape
->drive
;
1483 filp
->private_data
= tape
;
1485 ide_debug_log(IDE_DBG_FUNC
, "enter");
1488 * We really want to do nonseekable_open(inode, filp); here, but some
1489 * versions of tar incorrectly call lseek on tapes and bail out if that
1490 * fails. So we disallow pread() and pwrite(), but permit lseeks.
1492 filp
->f_mode
&= ~(FMODE_PREAD
| FMODE_PWRITE
);
1495 if (test_and_set_bit(ilog2(IDE_AFLAG_BUSY
), &drive
->atapi_flags
)) {
1500 retval
= idetape_wait_ready(drive
, 60 * HZ
);
1502 clear_bit(ilog2(IDE_AFLAG_BUSY
), &drive
->atapi_flags
);
1503 printk(KERN_ERR
"ide-tape: %s: drive not ready\n", tape
->name
);
1507 ide_tape_read_position(drive
);
1508 if (!test_bit(ilog2(IDE_AFLAG_ADDRESS_VALID
), &drive
->atapi_flags
))
1509 (void)idetape_rewind_tape(drive
);
1511 /* Read block size and write protect status from drive. */
1512 ide_tape_get_bsize_from_bdesc(drive
);
1514 /* Set write protect flag if device is opened as read-only. */
1515 if ((filp
->f_flags
& O_ACCMODE
) == O_RDONLY
)
1516 tape
->write_prot
= 1;
1518 tape
->write_prot
= tape
->drv_write_prot
;
1520 /* Make sure drive isn't write protected if user wants to write. */
1521 if (tape
->write_prot
) {
1522 if ((filp
->f_flags
& O_ACCMODE
) == O_WRONLY
||
1523 (filp
->f_flags
& O_ACCMODE
) == O_RDWR
) {
1524 clear_bit(ilog2(IDE_AFLAG_BUSY
), &drive
->atapi_flags
);
1530 /* Lock the tape drive door so user can't eject. */
1531 if (tape
->chrdev_dir
== IDETAPE_DIR_NONE
) {
1532 if (!ide_set_media_lock(drive
, tape
->disk
, 1)) {
1533 if (tape
->door_locked
!= DOOR_EXPLICITLY_LOCKED
)
1534 tape
->door_locked
= DOOR_LOCKED
;
1537 mutex_unlock(&idetape_chrdev_mutex
);
1544 mutex_unlock(&idetape_chrdev_mutex
);
1549 static void idetape_write_release(ide_drive_t
*drive
, unsigned int minor
)
1551 idetape_tape_t
*tape
= drive
->driver_data
;
1553 ide_tape_flush_merge_buffer(drive
);
1554 tape
->buf
= kmalloc(tape
->buffer_size
, GFP_KERNEL
);
1555 if (tape
->buf
!= NULL
) {
1556 idetape_pad_zeros(drive
, tape
->blk_size
*
1557 (tape
->user_bs_factor
- 1));
1561 idetape_write_filemark(drive
);
1562 idetape_flush_tape_buffers(drive
);
1563 idetape_flush_tape_buffers(drive
);
1566 static int idetape_chrdev_release(struct inode
*inode
, struct file
*filp
)
1568 struct ide_tape_obj
*tape
= filp
->private_data
;
1569 ide_drive_t
*drive
= tape
->drive
;
1570 unsigned int minor
= iminor(inode
);
1572 mutex_lock(&idetape_chrdev_mutex
);
1574 tape
= drive
->driver_data
;
1576 ide_debug_log(IDE_DBG_FUNC
, "enter");
1578 if (tape
->chrdev_dir
== IDETAPE_DIR_WRITE
)
1579 idetape_write_release(drive
, minor
);
1580 if (tape
->chrdev_dir
== IDETAPE_DIR_READ
) {
1582 ide_tape_discard_merge_buffer(drive
, 1);
1585 if (minor
< 128 && test_bit(ilog2(IDE_AFLAG_MEDIUM_PRESENT
),
1586 &drive
->atapi_flags
))
1587 (void) idetape_rewind_tape(drive
);
1589 if (tape
->chrdev_dir
== IDETAPE_DIR_NONE
) {
1590 if (tape
->door_locked
== DOOR_LOCKED
) {
1591 if (!ide_set_media_lock(drive
, tape
->disk
, 0))
1592 tape
->door_locked
= DOOR_UNLOCKED
;
1595 clear_bit(ilog2(IDE_AFLAG_BUSY
), &drive
->atapi_flags
);
1598 mutex_unlock(&idetape_chrdev_mutex
);
1603 static void idetape_get_inquiry_results(ide_drive_t
*drive
)
1605 idetape_tape_t
*tape
= drive
->driver_data
;
1606 struct ide_atapi_pc pc
;
1608 char fw_rev
[4], vendor_id
[8], product_id
[16];
1610 idetape_create_inquiry_cmd(&pc
);
1611 if (ide_queue_pc_tail(drive
, tape
->disk
, &pc
, pc_buf
, pc
.req_xfer
)) {
1612 printk(KERN_ERR
"ide-tape: %s: can't get INQUIRY results\n",
1616 memcpy(vendor_id
, &pc_buf
[8], 8);
1617 memcpy(product_id
, &pc_buf
[16], 16);
1618 memcpy(fw_rev
, &pc_buf
[32], 4);
1620 ide_fixstring(vendor_id
, 8, 0);
1621 ide_fixstring(product_id
, 16, 0);
1622 ide_fixstring(fw_rev
, 4, 0);
1624 printk(KERN_INFO
"ide-tape: %s <-> %s: %.8s %.16s rev %.4s\n",
1625 drive
->name
, tape
->name
, vendor_id
, product_id
, fw_rev
);
1629 * Ask the tape about its various parameters. In particular, we will adjust our
1630 * data transfer buffer size to the recommended value as returned by the tape.
1632 static void idetape_get_mode_sense_results(ide_drive_t
*drive
)
1634 idetape_tape_t
*tape
= drive
->driver_data
;
1635 struct ide_atapi_pc pc
;
1637 u8 speed
, max_speed
;
1639 idetape_create_mode_sense_cmd(&pc
, IDETAPE_CAPABILITIES_PAGE
);
1640 if (ide_queue_pc_tail(drive
, tape
->disk
, &pc
, buf
, pc
.req_xfer
)) {
1641 printk(KERN_ERR
"ide-tape: Can't get tape parameters - assuming"
1642 " some default values\n");
1643 tape
->blk_size
= 512;
1644 put_unaligned(52, (u16
*)&tape
->caps
[12]);
1645 put_unaligned(540, (u16
*)&tape
->caps
[14]);
1646 put_unaligned(6*52, (u16
*)&tape
->caps
[16]);
1649 caps
= buf
+ 4 + buf
[3];
1651 /* convert to host order and save for later use */
1652 speed
= be16_to_cpup((__be16
*)&caps
[14]);
1653 max_speed
= be16_to_cpup((__be16
*)&caps
[8]);
1655 *(u16
*)&caps
[8] = max_speed
;
1656 *(u16
*)&caps
[12] = be16_to_cpup((__be16
*)&caps
[12]);
1657 *(u16
*)&caps
[14] = speed
;
1658 *(u16
*)&caps
[16] = be16_to_cpup((__be16
*)&caps
[16]);
1661 printk(KERN_INFO
"ide-tape: %s: invalid tape speed "
1662 "(assuming 650KB/sec)\n", drive
->name
);
1663 *(u16
*)&caps
[14] = 650;
1666 printk(KERN_INFO
"ide-tape: %s: invalid max_speed "
1667 "(assuming 650KB/sec)\n", drive
->name
);
1668 *(u16
*)&caps
[8] = 650;
1671 memcpy(&tape
->caps
, caps
, 20);
1673 /* device lacks locking support according to capabilities page */
1674 if ((caps
[6] & 1) == 0)
1675 drive
->dev_flags
&= ~IDE_DFLAG_DOORLOCKING
;
1678 tape
->blk_size
= 512;
1679 else if (caps
[7] & 0x04)
1680 tape
->blk_size
= 1024;
1683 #ifdef CONFIG_IDE_PROC_FS
1684 #define ide_tape_devset_get(name, field) \
1685 static int get_##name(ide_drive_t *drive) \
1687 idetape_tape_t *tape = drive->driver_data; \
1688 return tape->field; \
1691 #define ide_tape_devset_set(name, field) \
1692 static int set_##name(ide_drive_t *drive, int arg) \
1694 idetape_tape_t *tape = drive->driver_data; \
1695 tape->field = arg; \
1699 #define ide_tape_devset_rw_field(_name, _field) \
1700 ide_tape_devset_get(_name, _field) \
1701 ide_tape_devset_set(_name, _field) \
1702 IDE_DEVSET(_name, DS_SYNC, get_##_name, set_##_name)
1704 #define ide_tape_devset_r_field(_name, _field) \
1705 ide_tape_devset_get(_name, _field) \
1706 IDE_DEVSET(_name, 0, get_##_name, NULL)
1708 static int mulf_tdsc(ide_drive_t
*drive
) { return 1000; }
1709 static int divf_tdsc(ide_drive_t
*drive
) { return HZ
; }
1710 static int divf_buffer(ide_drive_t
*drive
) { return 2; }
1711 static int divf_buffer_size(ide_drive_t
*drive
) { return 1024; }
1713 ide_devset_rw_flag(dsc_overlap
, IDE_DFLAG_DSC_OVERLAP
);
1715 ide_tape_devset_rw_field(tdsc
, best_dsc_rw_freq
);
1717 ide_tape_devset_r_field(avg_speed
, avg_speed
);
1718 ide_tape_devset_r_field(speed
, caps
[14]);
1719 ide_tape_devset_r_field(buffer
, caps
[16]);
1720 ide_tape_devset_r_field(buffer_size
, buffer_size
);
1722 static const struct ide_proc_devset idetape_settings
[] = {
1723 __IDE_PROC_DEVSET(avg_speed
, 0, 0xffff, NULL
, NULL
),
1724 __IDE_PROC_DEVSET(buffer
, 0, 0xffff, NULL
, divf_buffer
),
1725 __IDE_PROC_DEVSET(buffer_size
, 0, 0xffff, NULL
, divf_buffer_size
),
1726 __IDE_PROC_DEVSET(dsc_overlap
, 0, 1, NULL
, NULL
),
1727 __IDE_PROC_DEVSET(speed
, 0, 0xffff, NULL
, NULL
),
1728 __IDE_PROC_DEVSET(tdsc
, IDETAPE_DSC_RW_MIN
, IDETAPE_DSC_RW_MAX
,
1729 mulf_tdsc
, divf_tdsc
),
1735 * The function below is called to:
1737 * 1. Initialize our various state variables.
1738 * 2. Ask the tape for its capabilities.
1739 * 3. Allocate a buffer which will be used for data transfer. The buffer size
1740 * is chosen based on the recommendation which we received in step 2.
1742 * Note that at this point ide.c already assigned us an irq, so that we can
1743 * queue requests here and wait for their completion.
1745 static void idetape_setup(ide_drive_t
*drive
, idetape_tape_t
*tape
, int minor
)
1750 u16
*ctl
= (u16
*)&tape
->caps
[12];
1752 ide_debug_log(IDE_DBG_FUNC
, "minor: %d", minor
);
1754 drive
->pc_callback
= ide_tape_callback
;
1756 drive
->dev_flags
|= IDE_DFLAG_DSC_OVERLAP
;
1758 if (drive
->hwif
->host_flags
& IDE_HFLAG_NO_DSC
) {
1759 printk(KERN_INFO
"ide-tape: %s: disabling DSC overlap\n",
1761 drive
->dev_flags
&= ~IDE_DFLAG_DSC_OVERLAP
;
1764 /* Seagate Travan drives do not support DSC overlap. */
1765 if (strstr((char *)&drive
->id
[ATA_ID_PROD
], "Seagate STT3401"))
1766 drive
->dev_flags
&= ~IDE_DFLAG_DSC_OVERLAP
;
1768 tape
->minor
= minor
;
1769 tape
->name
[0] = 'h';
1770 tape
->name
[1] = 't';
1771 tape
->name
[2] = '0' + minor
;
1772 tape
->chrdev_dir
= IDETAPE_DIR_NONE
;
1774 idetape_get_inquiry_results(drive
);
1775 idetape_get_mode_sense_results(drive
);
1776 ide_tape_get_bsize_from_bdesc(drive
);
1777 tape
->user_bs_factor
= 1;
1778 tape
->buffer_size
= *ctl
* tape
->blk_size
;
1779 while (tape
->buffer_size
> 0xffff) {
1780 printk(KERN_NOTICE
"ide-tape: decreasing stage size\n");
1782 tape
->buffer_size
= *ctl
* tape
->blk_size
;
1784 buffer_size
= tape
->buffer_size
;
1786 /* select the "best" DSC read/write polling freq */
1787 speed
= max(*(u16
*)&tape
->caps
[14], *(u16
*)&tape
->caps
[8]);
1789 t
= (IDETAPE_FIFO_THRESHOLD
* tape
->buffer_size
* HZ
) / (speed
* 1000);
1792 * Ensure that the number we got makes sense; limit it within
1793 * IDETAPE_DSC_RW_MIN and IDETAPE_DSC_RW_MAX.
1795 tape
->best_dsc_rw_freq
= clamp_t(unsigned long, t
, IDETAPE_DSC_RW_MIN
,
1796 IDETAPE_DSC_RW_MAX
);
1797 printk(KERN_INFO
"ide-tape: %s <-> %s: %dKBps, %d*%dkB buffer, "
1799 drive
->name
, tape
->name
, *(u16
*)&tape
->caps
[14],
1800 (*(u16
*)&tape
->caps
[16] * 512) / tape
->buffer_size
,
1801 tape
->buffer_size
/ 1024,
1802 tape
->best_dsc_rw_freq
* 1000 / HZ
,
1803 (drive
->dev_flags
& IDE_DFLAG_USING_DMA
) ? ", DMA" : "");
1805 ide_proc_register_driver(drive
, tape
->driver
);
1808 static void ide_tape_remove(ide_drive_t
*drive
)
1810 idetape_tape_t
*tape
= drive
->driver_data
;
1812 ide_proc_unregister_driver(drive
, tape
->driver
);
1813 device_del(&tape
->dev
);
1814 ide_unregister_region(tape
->disk
);
1816 mutex_lock(&idetape_ref_mutex
);
1817 put_device(&tape
->dev
);
1818 mutex_unlock(&idetape_ref_mutex
);
1821 static void ide_tape_release(struct device
*dev
)
1823 struct ide_tape_obj
*tape
= to_ide_drv(dev
, ide_tape_obj
);
1824 ide_drive_t
*drive
= tape
->drive
;
1825 struct gendisk
*g
= tape
->disk
;
1827 BUG_ON(tape
->valid
);
1829 drive
->dev_flags
&= ~IDE_DFLAG_DSC_OVERLAP
;
1830 drive
->driver_data
= NULL
;
1831 device_destroy(idetape_sysfs_class
, MKDEV(IDETAPE_MAJOR
, tape
->minor
));
1832 device_destroy(idetape_sysfs_class
,
1833 MKDEV(IDETAPE_MAJOR
, tape
->minor
+ 128));
1834 idetape_devs
[tape
->minor
] = NULL
;
1835 g
->private_data
= NULL
;
1840 #ifdef CONFIG_IDE_PROC_FS
1841 static int idetape_name_proc_show(struct seq_file
*m
, void *v
)
1843 ide_drive_t
*drive
= (ide_drive_t
*) m
->private;
1844 idetape_tape_t
*tape
= drive
->driver_data
;
1846 seq_printf(m
, "%s\n", tape
->name
);
1850 static int idetape_name_proc_open(struct inode
*inode
, struct file
*file
)
1852 return single_open(file
, idetape_name_proc_show
, PDE(inode
)->data
);
1855 static const struct file_operations idetape_name_proc_fops
= {
1856 .owner
= THIS_MODULE
,
1857 .open
= idetape_name_proc_open
,
1859 .llseek
= seq_lseek
,
1860 .release
= single_release
,
1863 static ide_proc_entry_t idetape_proc
[] = {
1864 { "capacity", S_IFREG
|S_IRUGO
, &ide_capacity_proc_fops
},
1865 { "name", S_IFREG
|S_IRUGO
, &idetape_name_proc_fops
},
1869 static ide_proc_entry_t
*ide_tape_proc_entries(ide_drive_t
*drive
)
1871 return idetape_proc
;
1874 static const struct ide_proc_devset
*ide_tape_proc_devsets(ide_drive_t
*drive
)
1876 return idetape_settings
;
1880 static int ide_tape_probe(ide_drive_t
*);
1882 static struct ide_driver idetape_driver
= {
1884 .owner
= THIS_MODULE
,
1886 .bus
= &ide_bus_type
,
1888 .probe
= ide_tape_probe
,
1889 .remove
= ide_tape_remove
,
1890 .version
= IDETAPE_VERSION
,
1891 .do_request
= idetape_do_request
,
1892 #ifdef CONFIG_IDE_PROC_FS
1893 .proc_entries
= ide_tape_proc_entries
,
1894 .proc_devsets
= ide_tape_proc_devsets
,
1898 /* Our character device supporting functions, passed to register_chrdev. */
1899 static const struct file_operations idetape_fops
= {
1900 .owner
= THIS_MODULE
,
1901 .read
= idetape_chrdev_read
,
1902 .write
= idetape_chrdev_write
,
1903 .unlocked_ioctl
= idetape_chrdev_ioctl
,
1904 .open
= idetape_chrdev_open
,
1905 .release
= idetape_chrdev_release
,
1908 static int idetape_open(struct block_device
*bdev
, fmode_t mode
)
1910 struct ide_tape_obj
*tape
;
1913 tape
= ide_tape_get(bdev
->bd_disk
, false, 0);
1922 static int idetape_release(struct gendisk
*disk
, fmode_t mode
)
1924 struct ide_tape_obj
*tape
= ide_drv_g(disk
, ide_tape_obj
);
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
;
1941 err
= generic_ide_ioctl(drive
, bdev
, cmd
, arg
);
1943 err
= idetape_blkdev_ioctl(drive
, cmd
, arg
);
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");