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/smp_lock.h>
39 #include <linux/completion.h>
40 #include <linux/bitops.h>
41 #include <linux/mutex.h>
42 #include <scsi/scsi.h>
44 #include <asm/byteorder.h>
45 #include <linux/irq.h>
46 #include <linux/uaccess.h>
48 #include <asm/unaligned.h>
49 #include <linux/mtio.h>
51 /* define to see debug info */
52 #undef IDETAPE_DEBUG_LOG
54 #ifdef IDETAPE_DEBUG_LOG
55 #define ide_debug_log(lvl, fmt, args...) __ide_debug_log(lvl, fmt, ## args)
57 #define ide_debug_log(lvl, fmt, args...) do {} while (0)
60 /**************************** Tunable parameters *****************************/
62 * After each failed packet command we issue a request sense command and retry
63 * the packet command IDETAPE_MAX_PC_RETRIES times.
65 * Setting IDETAPE_MAX_PC_RETRIES to 0 will disable retries.
67 #define IDETAPE_MAX_PC_RETRIES 3
70 * The following parameter is used to select the point in the internal tape fifo
71 * in which we will start to refill the buffer. Decreasing the following
72 * parameter will improve the system's latency and interactive response, while
73 * using a high value might improve system throughput.
75 #define IDETAPE_FIFO_THRESHOLD 2
78 * DSC polling parameters.
80 * Polling for DSC (a single bit in the status register) is a very important
81 * function in ide-tape. There are two cases in which we poll for DSC:
83 * 1. Before a read/write packet command, to ensure that we can transfer data
84 * from/to the tape's data buffers, without causing an actual media access.
85 * In case the tape is not ready yet, we take out our request from the device
86 * request queue, so that ide.c could service requests from the other device
87 * on the same interface in the meantime.
89 * 2. After the successful initialization of a "media access packet command",
90 * which is a command that can take a long time to complete (the interval can
91 * range from several seconds to even an hour). Again, we postpone our request
92 * in the middle to free the bus for the other device. The polling frequency
93 * here should be lower than the read/write frequency since those media access
94 * commands are slow. We start from a "fast" frequency - IDETAPE_DSC_MA_FAST
95 * (1 second), and if we don't receive DSC after IDETAPE_DSC_MA_THRESHOLD
96 * (5 min), we switch it to a lower frequency - IDETAPE_DSC_MA_SLOW (1 min).
98 * We also set a timeout for the timer, in case something goes wrong. The
99 * timeout should be longer then the maximum execution time of a tape operation.
103 #define IDETAPE_DSC_RW_MIN 5*HZ/100 /* 50 msec */
104 #define IDETAPE_DSC_RW_MAX 40*HZ/100 /* 400 msec */
105 #define IDETAPE_DSC_RW_TIMEOUT 2*60*HZ /* 2 minutes */
106 #define IDETAPE_DSC_MA_FAST 2*HZ /* 2 seconds */
107 #define IDETAPE_DSC_MA_THRESHOLD 5*60*HZ /* 5 minutes */
108 #define IDETAPE_DSC_MA_SLOW 30*HZ /* 30 seconds */
109 #define IDETAPE_DSC_MA_TIMEOUT 2*60*60*HZ /* 2 hours */
111 /*************************** End of tunable parameters ***********************/
113 /* tape directions */
115 IDETAPE_DIR_NONE
= (1 << 0),
116 IDETAPE_DIR_READ
= (1 << 1),
117 IDETAPE_DIR_WRITE
= (1 << 2),
120 /* Tape door status */
121 #define DOOR_UNLOCKED 0
122 #define DOOR_LOCKED 1
123 #define DOOR_EXPLICITLY_LOCKED 2
125 /* Some defines for the SPACE command */
126 #define IDETAPE_SPACE_OVER_FILEMARK 1
127 #define IDETAPE_SPACE_TO_EOD 3
129 /* Some defines for the LOAD UNLOAD command */
130 #define IDETAPE_LU_LOAD_MASK 1
131 #define IDETAPE_LU_RETENSION_MASK 2
132 #define IDETAPE_LU_EOT_MASK 4
134 /* Structures related to the SELECT SENSE / MODE SENSE packet commands. */
135 #define IDETAPE_BLOCK_DESCRIPTOR 0
136 #define IDETAPE_CAPABILITIES_PAGE 0x2a
139 * Most of our global data which we need to save even as we leave the driver due
140 * to an interrupt or a timer event is stored in the struct defined below.
142 typedef struct ide_tape_obj
{
144 struct ide_driver
*driver
;
145 struct gendisk
*disk
;
148 /* used by REQ_IDETAPE_{READ,WRITE} requests */
149 struct ide_atapi_pc queued_pc
;
152 * DSC polling variables.
154 * While polling for DSC we use postponed_rq to postpone the current
155 * request so that ide.c will be able to service pending requests on the
156 * other device. Note that at most we will have only one DSC (usually
157 * data transfer) request in the device request queue.
161 /* The time in which we started polling for DSC */
162 unsigned long dsc_polling_start
;
163 /* Timer used to poll for dsc */
164 struct timer_list dsc_timer
;
165 /* Read/Write dsc polling frequency */
166 unsigned long best_dsc_rw_freq
;
167 unsigned long dsc_poll_freq
;
168 unsigned long dsc_timeout
;
170 /* Read position information */
173 unsigned int first_frame
;
175 /* Last error information */
176 u8 sense_key
, asc
, ascq
;
178 /* Character device operation */
182 /* Current character device data transfer direction */
185 /* tape block size, usually 512 or 1024 bytes */
186 unsigned short blk_size
;
189 /* Copy of the tape's Capabilities and Mechanical Page */
193 * Active data transfer request parameters.
195 * At most, there is only one ide-tape originated data transfer request
196 * in the device request queue. This allows ide.c to easily service
197 * requests from the other device when we postpone our active request.
200 /* Data buffer size chosen based on the tape's recommendation */
202 /* Staging buffer of buffer_size bytes */
204 /* The read/write cursor */
206 /* The number of valid bytes in buf */
209 /* Measures average tape speed */
210 unsigned long avg_time
;
214 /* the door is currently locked */
216 /* the tape hardware is write protected */
218 /* the tape is write protected (hardware or opened as read-only) */
222 static DEFINE_MUTEX(idetape_ref_mutex
);
224 static struct class *idetape_sysfs_class
;
226 static void ide_tape_release(struct device
*);
228 static struct ide_tape_obj
*idetape_devs
[MAX_HWIFS
* MAX_DRIVES
];
230 static struct ide_tape_obj
*ide_tape_get(struct gendisk
*disk
, bool cdev
,
233 struct ide_tape_obj
*tape
= NULL
;
235 mutex_lock(&idetape_ref_mutex
);
238 tape
= idetape_devs
[i
];
240 tape
= ide_drv_g(disk
, ide_tape_obj
);
243 if (ide_device_get(tape
->drive
))
246 get_device(&tape
->dev
);
249 mutex_unlock(&idetape_ref_mutex
);
253 static void ide_tape_put(struct ide_tape_obj
*tape
)
255 ide_drive_t
*drive
= tape
->drive
;
257 mutex_lock(&idetape_ref_mutex
);
258 put_device(&tape
->dev
);
259 ide_device_put(drive
);
260 mutex_unlock(&idetape_ref_mutex
);
264 * called on each failed packet command retry to analyze the request sense. We
265 * currently do not utilize this information.
267 static void idetape_analyze_error(ide_drive_t
*drive
)
269 idetape_tape_t
*tape
= drive
->driver_data
;
270 struct ide_atapi_pc
*pc
= drive
->failed_pc
;
271 struct request
*rq
= drive
->hwif
->rq
;
272 u8
*sense
= bio_data(rq
->bio
);
274 tape
->sense_key
= sense
[2] & 0xF;
275 tape
->asc
= sense
[12];
276 tape
->ascq
= sense
[13];
278 ide_debug_log(IDE_DBG_FUNC
,
279 "cmd: 0x%x, sense key = %x, asc = %x, ascq = %x",
280 rq
->cmd
[0], tape
->sense_key
, tape
->asc
, tape
->ascq
);
282 /* correct remaining bytes to transfer */
283 if (pc
->flags
& PC_FLAG_DMA_ERROR
)
284 rq
->resid_len
= tape
->blk_size
* get_unaligned_be32(&sense
[3]);
287 * If error was the result of a zero-length read or write command,
288 * with sense key=5, asc=0x22, ascq=0, let it slide. Some drives
289 * (i.e. Seagate STT3401A Travan) don't support 0-length read/writes.
291 if ((pc
->c
[0] == READ_6
|| pc
->c
[0] == WRITE_6
)
293 && pc
->c
[4] == 0 && pc
->c
[3] == 0 && pc
->c
[2] == 0) {
294 if (tape
->sense_key
== 5) {
295 /* don't report an error, everything's ok */
297 /* don't retry read/write */
298 pc
->flags
|= PC_FLAG_ABORT
;
301 if (pc
->c
[0] == READ_6
&& (sense
[2] & 0x80)) {
302 pc
->error
= IDE_DRV_ERROR_FILEMARK
;
303 pc
->flags
|= PC_FLAG_ABORT
;
305 if (pc
->c
[0] == WRITE_6
) {
306 if ((sense
[2] & 0x40) || (tape
->sense_key
== 0xd
307 && tape
->asc
== 0x0 && tape
->ascq
== 0x2)) {
308 pc
->error
= IDE_DRV_ERROR_EOD
;
309 pc
->flags
|= PC_FLAG_ABORT
;
312 if (pc
->c
[0] == READ_6
|| pc
->c
[0] == WRITE_6
) {
313 if (tape
->sense_key
== 8) {
314 pc
->error
= IDE_DRV_ERROR_EOD
;
315 pc
->flags
|= PC_FLAG_ABORT
;
317 if (!(pc
->flags
& PC_FLAG_ABORT
) &&
318 (blk_rq_bytes(rq
) - rq
->resid_len
))
319 pc
->retries
= IDETAPE_MAX_PC_RETRIES
+ 1;
323 static void ide_tape_handle_dsc(ide_drive_t
*);
325 static int ide_tape_callback(ide_drive_t
*drive
, int dsc
)
327 idetape_tape_t
*tape
= drive
->driver_data
;
328 struct ide_atapi_pc
*pc
= drive
->pc
;
329 struct request
*rq
= drive
->hwif
->rq
;
330 int uptodate
= pc
->error
? 0 : 1;
331 int err
= uptodate
? 0 : IDE_DRV_ERROR_GENERAL
;
333 ide_debug_log(IDE_DBG_FUNC
, "cmd: 0x%x, dsc: %d, err: %d", rq
->cmd
[0],
337 ide_tape_handle_dsc(drive
);
339 if (drive
->failed_pc
== pc
)
340 drive
->failed_pc
= NULL
;
342 if (pc
->c
[0] == REQUEST_SENSE
) {
344 idetape_analyze_error(drive
);
346 printk(KERN_ERR
"ide-tape: Error in REQUEST SENSE "
347 "itself - Aborting request!\n");
348 } else if (pc
->c
[0] == READ_6
|| pc
->c
[0] == WRITE_6
) {
349 unsigned int blocks
=
350 (blk_rq_bytes(rq
) - rq
->resid_len
) / tape
->blk_size
;
352 tape
->avg_size
+= blocks
* tape
->blk_size
;
354 if (time_after_eq(jiffies
, tape
->avg_time
+ HZ
)) {
355 tape
->avg_speed
= tape
->avg_size
* HZ
/
356 (jiffies
- tape
->avg_time
) / 1024;
358 tape
->avg_time
= jiffies
;
361 tape
->first_frame
+= blocks
;
374 * Postpone the current request so that ide.c will be able to service requests
375 * from another device on the same port while we are polling for DSC.
377 static void ide_tape_stall_queue(ide_drive_t
*drive
)
379 idetape_tape_t
*tape
= drive
->driver_data
;
381 ide_debug_log(IDE_DBG_FUNC
, "cmd: 0x%x, dsc_poll_freq: %lu",
382 drive
->hwif
->rq
->cmd
[0], tape
->dsc_poll_freq
);
384 tape
->postponed_rq
= true;
386 ide_stall_queue(drive
, tape
->dsc_poll_freq
);
389 static void ide_tape_handle_dsc(ide_drive_t
*drive
)
391 idetape_tape_t
*tape
= drive
->driver_data
;
393 /* Media access command */
394 tape
->dsc_polling_start
= jiffies
;
395 tape
->dsc_poll_freq
= IDETAPE_DSC_MA_FAST
;
396 tape
->dsc_timeout
= jiffies
+ IDETAPE_DSC_MA_TIMEOUT
;
397 /* Allow ide.c to handle other requests */
398 ide_tape_stall_queue(drive
);
402 * Packet Command Interface
404 * The current Packet Command is available in drive->pc, and will not change
405 * until we finish handling it. Each packet command is associated with a
406 * callback function that will be called when the command is finished.
408 * The handling will be done in three stages:
410 * 1. ide_tape_issue_pc will send the packet command to the drive, and will set
411 * the interrupt handler to ide_pc_intr.
413 * 2. On each interrupt, ide_pc_intr will be called. This step will be
414 * repeated until the device signals us that no more interrupts will be issued.
416 * 3. ATAPI Tape media access commands have immediate status with a delayed
417 * process. In case of a successful initiation of a media access packet command,
418 * the DSC bit will be set when the actual execution of the command is finished.
419 * Since the tape drive will not issue an interrupt, we have to poll for this
420 * event. In this case, we define the request as "low priority request" by
421 * setting rq_status to IDETAPE_RQ_POSTPONED, set a timer to poll for DSC and
424 * ide.c will then give higher priority to requests which originate from the
425 * other device, until will change rq_status to RQ_ACTIVE.
427 * 4. When the packet command is finished, it will be checked for errors.
429 * 5. In case an error was found, we queue a request sense packet command in
430 * front of the request queue and retry the operation up to
431 * IDETAPE_MAX_PC_RETRIES times.
433 * 6. In case no error was found, or we decided to give up and not to retry
434 * again, the callback function will be called and then we will handle the next
438 static ide_startstop_t
ide_tape_issue_pc(ide_drive_t
*drive
,
440 struct ide_atapi_pc
*pc
)
442 idetape_tape_t
*tape
= drive
->driver_data
;
443 struct request
*rq
= drive
->hwif
->rq
;
445 if (drive
->failed_pc
== NULL
&& pc
->c
[0] != REQUEST_SENSE
)
446 drive
->failed_pc
= pc
;
448 /* Set the current packet command */
451 if (pc
->retries
> IDETAPE_MAX_PC_RETRIES
||
452 (pc
->flags
& PC_FLAG_ABORT
)) {
455 * We will "abort" retrying a packet command in case legitimate
456 * error code was received (crossing a filemark, or end of the
457 * media, for example).
459 if (!(pc
->flags
& PC_FLAG_ABORT
)) {
460 if (!(pc
->c
[0] == TEST_UNIT_READY
&&
461 tape
->sense_key
== 2 && tape
->asc
== 4 &&
462 (tape
->ascq
== 1 || tape
->ascq
== 8))) {
463 printk(KERN_ERR
"ide-tape: %s: I/O error, "
464 "pc = %2x, key = %2x, "
465 "asc = %2x, ascq = %2x\n",
466 tape
->name
, pc
->c
[0],
467 tape
->sense_key
, tape
->asc
,
471 pc
->error
= IDE_DRV_ERROR_GENERAL
;
474 drive
->failed_pc
= NULL
;
475 drive
->pc_callback(drive
, 0);
476 ide_complete_rq(drive
, -EIO
, blk_rq_bytes(rq
));
479 ide_debug_log(IDE_DBG_SENSE
, "retry #%d, cmd: 0x%02x", pc
->retries
,
484 return ide_issue_pc(drive
, cmd
);
487 /* A mode sense command is used to "sense" tape parameters. */
488 static void idetape_create_mode_sense_cmd(struct ide_atapi_pc
*pc
, u8 page_code
)
491 pc
->c
[0] = MODE_SENSE
;
492 if (page_code
!= IDETAPE_BLOCK_DESCRIPTOR
)
493 /* DBD = 1 - Don't return block descriptors */
495 pc
->c
[2] = page_code
;
497 * Changed pc->c[3] to 0 (255 will at best return unused info).
499 * For SCSI this byte is defined as subpage instead of high byte
500 * of length and some IDE drives seem to interpret it this way
501 * and return an error when 255 is used.
504 /* We will just discard data in that case */
506 if (page_code
== IDETAPE_BLOCK_DESCRIPTOR
)
508 else if (page_code
== IDETAPE_CAPABILITIES_PAGE
)
514 static ide_startstop_t
idetape_media_access_finished(ide_drive_t
*drive
)
516 ide_hwif_t
*hwif
= drive
->hwif
;
517 idetape_tape_t
*tape
= drive
->driver_data
;
518 struct ide_atapi_pc
*pc
= drive
->pc
;
521 stat
= hwif
->tp_ops
->read_status(hwif
);
523 if (stat
& ATA_DSC
) {
524 if (stat
& ATA_ERR
) {
526 if (pc
->c
[0] != TEST_UNIT_READY
)
527 printk(KERN_ERR
"ide-tape: %s: I/O error, ",
529 /* Retry operation */
535 pc
->error
= IDE_DRV_ERROR_GENERAL
;
536 drive
->failed_pc
= NULL
;
538 drive
->pc_callback(drive
, 0);
542 static void ide_tape_create_rw_cmd(idetape_tape_t
*tape
,
543 struct ide_atapi_pc
*pc
, struct request
*rq
,
546 unsigned int length
= blk_rq_sectors(rq
) / (tape
->blk_size
>> 9);
549 put_unaligned(cpu_to_be32(length
), (unsigned int *) &pc
->c
[1]);
552 if (blk_rq_bytes(rq
) == tape
->buffer_size
)
553 pc
->flags
|= PC_FLAG_DMA_OK
;
555 if (opcode
== READ_6
)
557 else if (opcode
== WRITE_6
) {
559 pc
->flags
|= PC_FLAG_WRITING
;
562 memcpy(rq
->cmd
, pc
->c
, 12);
565 static ide_startstop_t
idetape_do_request(ide_drive_t
*drive
,
566 struct request
*rq
, sector_t block
)
568 ide_hwif_t
*hwif
= drive
->hwif
;
569 idetape_tape_t
*tape
= drive
->driver_data
;
570 struct ide_atapi_pc
*pc
= NULL
;
574 ide_debug_log(IDE_DBG_RQ
, "cmd: 0x%x, sector: %llu, nr_sectors: %u",
575 rq
->cmd
[0], (unsigned long long)blk_rq_pos(rq
),
578 BUG_ON(!(blk_special_request(rq
) || blk_sense_request(rq
)));
580 /* Retry a failed packet command */
581 if (drive
->failed_pc
&& drive
->pc
->c
[0] == REQUEST_SENSE
) {
582 pc
= drive
->failed_pc
;
587 * If the tape is still busy, postpone our request and service
588 * the other device meanwhile.
590 stat
= hwif
->tp_ops
->read_status(hwif
);
592 if ((drive
->dev_flags
& IDE_DFLAG_DSC_OVERLAP
) == 0 &&
593 (rq
->cmd
[13] & REQ_IDETAPE_PC2
) == 0)
594 drive
->atapi_flags
|= IDE_AFLAG_IGNORE_DSC
;
596 if (drive
->dev_flags
& IDE_DFLAG_POST_RESET
) {
597 drive
->atapi_flags
|= IDE_AFLAG_IGNORE_DSC
;
598 drive
->dev_flags
&= ~IDE_DFLAG_POST_RESET
;
601 if (!(drive
->atapi_flags
& IDE_AFLAG_IGNORE_DSC
) &&
603 if (!tape
->postponed_rq
) {
604 tape
->dsc_polling_start
= jiffies
;
605 tape
->dsc_poll_freq
= tape
->best_dsc_rw_freq
;
606 tape
->dsc_timeout
= jiffies
+ IDETAPE_DSC_RW_TIMEOUT
;
607 } else if (time_after(jiffies
, tape
->dsc_timeout
)) {
608 printk(KERN_ERR
"ide-tape: %s: DSC timeout\n",
610 if (rq
->cmd
[13] & REQ_IDETAPE_PC2
) {
611 idetape_media_access_finished(drive
);
614 return ide_do_reset(drive
);
616 } else if (time_after(jiffies
,
617 tape
->dsc_polling_start
+
618 IDETAPE_DSC_MA_THRESHOLD
))
619 tape
->dsc_poll_freq
= IDETAPE_DSC_MA_SLOW
;
620 ide_tape_stall_queue(drive
);
623 drive
->atapi_flags
&= ~IDE_AFLAG_IGNORE_DSC
;
624 tape
->postponed_rq
= false;
627 if (rq
->cmd
[13] & REQ_IDETAPE_READ
) {
628 pc
= &tape
->queued_pc
;
629 ide_tape_create_rw_cmd(tape
, pc
, rq
, READ_6
);
632 if (rq
->cmd
[13] & REQ_IDETAPE_WRITE
) {
633 pc
= &tape
->queued_pc
;
634 ide_tape_create_rw_cmd(tape
, pc
, rq
, WRITE_6
);
637 if (rq
->cmd
[13] & REQ_IDETAPE_PC1
) {
638 pc
= (struct ide_atapi_pc
*)rq
->special
;
639 rq
->cmd
[13] &= ~(REQ_IDETAPE_PC1
);
640 rq
->cmd
[13] |= REQ_IDETAPE_PC2
;
643 if (rq
->cmd
[13] & REQ_IDETAPE_PC2
) {
644 idetape_media_access_finished(drive
);
650 /* prepare sense request for this command */
651 ide_prep_sense(drive
, rq
);
653 memset(&cmd
, 0, sizeof(cmd
));
656 cmd
.tf_flags
|= IDE_TFLAG_WRITE
;
660 ide_init_sg_cmd(&cmd
, blk_rq_bytes(rq
));
661 ide_map_sg(drive
, &cmd
);
663 return ide_tape_issue_pc(drive
, &cmd
, pc
);
667 * Write a filemark if write_filemark=1. Flush the device buffers without
668 * writing a filemark otherwise.
670 static void idetape_create_write_filemark_cmd(ide_drive_t
*drive
,
671 struct ide_atapi_pc
*pc
, int write_filemark
)
674 pc
->c
[0] = WRITE_FILEMARKS
;
675 pc
->c
[4] = write_filemark
;
676 pc
->flags
|= PC_FLAG_WAIT_FOR_DSC
;
679 static int idetape_wait_ready(ide_drive_t
*drive
, unsigned long timeout
)
681 idetape_tape_t
*tape
= drive
->driver_data
;
682 struct gendisk
*disk
= tape
->disk
;
683 int load_attempted
= 0;
685 /* Wait for the tape to become ready */
686 set_bit(ilog2(IDE_AFLAG_MEDIUM_PRESENT
), &drive
->atapi_flags
);
688 while (time_before(jiffies
, timeout
)) {
689 if (ide_do_test_unit_ready(drive
, disk
) == 0)
691 if ((tape
->sense_key
== 2 && tape
->asc
== 4 && tape
->ascq
== 2)
692 || (tape
->asc
== 0x3A)) {
696 ide_do_start_stop(drive
, disk
, IDETAPE_LU_LOAD_MASK
);
698 /* not about to be ready */
699 } else if (!(tape
->sense_key
== 2 && tape
->asc
== 4 &&
700 (tape
->ascq
== 1 || tape
->ascq
== 8)))
707 static int idetape_flush_tape_buffers(ide_drive_t
*drive
)
709 struct ide_tape_obj
*tape
= drive
->driver_data
;
710 struct ide_atapi_pc pc
;
713 idetape_create_write_filemark_cmd(drive
, &pc
, 0);
714 rc
= ide_queue_pc_tail(drive
, tape
->disk
, &pc
, NULL
, 0);
717 idetape_wait_ready(drive
, 60 * 5 * HZ
);
721 static int ide_tape_read_position(ide_drive_t
*drive
)
723 idetape_tape_t
*tape
= drive
->driver_data
;
724 struct ide_atapi_pc pc
;
727 ide_debug_log(IDE_DBG_FUNC
, "enter");
731 pc
.c
[0] = READ_POSITION
;
734 if (ide_queue_pc_tail(drive
, tape
->disk
, &pc
, buf
, pc
.req_xfer
))
738 ide_debug_log(IDE_DBG_FUNC
, "BOP - %s",
739 (buf
[0] & 0x80) ? "Yes" : "No");
740 ide_debug_log(IDE_DBG_FUNC
, "EOP - %s",
741 (buf
[0] & 0x40) ? "Yes" : "No");
744 printk(KERN_INFO
"ide-tape: Block location is unknown"
746 clear_bit(ilog2(IDE_AFLAG_ADDRESS_VALID
),
747 &drive
->atapi_flags
);
750 ide_debug_log(IDE_DBG_FUNC
, "Block Location: %u",
751 be32_to_cpup((__be32
*)&buf
[4]));
753 tape
->partition
= buf
[1];
754 tape
->first_frame
= be32_to_cpup((__be32
*)&buf
[4]);
755 set_bit(ilog2(IDE_AFLAG_ADDRESS_VALID
),
756 &drive
->atapi_flags
);
760 return tape
->first_frame
;
763 static void idetape_create_locate_cmd(ide_drive_t
*drive
,
764 struct ide_atapi_pc
*pc
,
765 unsigned int block
, u8 partition
, int skip
)
768 pc
->c
[0] = POSITION_TO_ELEMENT
;
770 put_unaligned(cpu_to_be32(block
), (unsigned int *) &pc
->c
[3]);
771 pc
->c
[8] = partition
;
772 pc
->flags
|= PC_FLAG_WAIT_FOR_DSC
;
775 static void __ide_tape_discard_merge_buffer(ide_drive_t
*drive
)
777 idetape_tape_t
*tape
= drive
->driver_data
;
779 if (tape
->chrdev_dir
!= IDETAPE_DIR_READ
)
782 clear_bit(ilog2(IDE_AFLAG_FILEMARK
), &drive
->atapi_flags
);
784 if (tape
->buf
!= NULL
) {
789 tape
->chrdev_dir
= IDETAPE_DIR_NONE
;
793 * Position the tape to the requested block using the LOCATE packet command.
794 * A READ POSITION command is then issued to check where we are positioned. Like
795 * all higher level operations, we queue the commands at the tail of the request
796 * queue and wait for their completion.
798 static int idetape_position_tape(ide_drive_t
*drive
, unsigned int block
,
799 u8 partition
, int skip
)
801 idetape_tape_t
*tape
= drive
->driver_data
;
802 struct gendisk
*disk
= tape
->disk
;
804 struct ide_atapi_pc pc
;
806 if (tape
->chrdev_dir
== IDETAPE_DIR_READ
)
807 __ide_tape_discard_merge_buffer(drive
);
808 idetape_wait_ready(drive
, 60 * 5 * HZ
);
809 idetape_create_locate_cmd(drive
, &pc
, block
, partition
, skip
);
810 ret
= ide_queue_pc_tail(drive
, disk
, &pc
, NULL
, 0);
814 ret
= ide_tape_read_position(drive
);
820 static void ide_tape_discard_merge_buffer(ide_drive_t
*drive
,
821 int restore_position
)
823 idetape_tape_t
*tape
= drive
->driver_data
;
826 __ide_tape_discard_merge_buffer(drive
);
827 if (restore_position
) {
828 position
= ide_tape_read_position(drive
);
829 seek
= position
> 0 ? position
: 0;
830 if (idetape_position_tape(drive
, seek
, 0, 0)) {
831 printk(KERN_INFO
"ide-tape: %s: position_tape failed in"
832 " %s\n", tape
->name
, __func__
);
839 * Generate a read/write request for the block device interface and wait for it
842 static int idetape_queue_rw_tail(ide_drive_t
*drive
, int cmd
, int size
)
844 idetape_tape_t
*tape
= drive
->driver_data
;
848 ide_debug_log(IDE_DBG_FUNC
, "cmd: 0x%x, size: %d", cmd
, size
);
850 BUG_ON(cmd
!= REQ_IDETAPE_READ
&& cmd
!= REQ_IDETAPE_WRITE
);
851 BUG_ON(size
< 0 || size
% tape
->blk_size
);
853 rq
= blk_get_request(drive
->queue
, READ
, __GFP_WAIT
);
854 rq
->cmd_type
= REQ_TYPE_SPECIAL
;
856 rq
->rq_disk
= tape
->disk
;
857 rq
->__sector
= tape
->first_frame
;
860 ret
= blk_rq_map_kern(drive
->queue
, rq
, tape
->buf
, size
,
866 blk_execute_rq(drive
->queue
, tape
->disk
, rq
, 0);
868 /* calculate the number of transferred bytes and update buffer state */
869 size
-= rq
->resid_len
;
870 tape
->cur
= tape
->buf
;
871 if (cmd
== REQ_IDETAPE_READ
)
877 if (rq
->errors
== IDE_DRV_ERROR_GENERAL
)
884 static void idetape_create_inquiry_cmd(struct ide_atapi_pc
*pc
)
892 static void idetape_create_rewind_cmd(ide_drive_t
*drive
,
893 struct ide_atapi_pc
*pc
)
896 pc
->c
[0] = REZERO_UNIT
;
897 pc
->flags
|= PC_FLAG_WAIT_FOR_DSC
;
900 static void idetape_create_erase_cmd(struct ide_atapi_pc
*pc
)
905 pc
->flags
|= PC_FLAG_WAIT_FOR_DSC
;
908 static void idetape_create_space_cmd(struct ide_atapi_pc
*pc
, int count
, u8 cmd
)
912 put_unaligned(cpu_to_be32(count
), (unsigned int *) &pc
->c
[1]);
914 pc
->flags
|= PC_FLAG_WAIT_FOR_DSC
;
917 static void ide_tape_flush_merge_buffer(ide_drive_t
*drive
)
919 idetape_tape_t
*tape
= drive
->driver_data
;
921 if (tape
->chrdev_dir
!= IDETAPE_DIR_WRITE
) {
922 printk(KERN_ERR
"ide-tape: bug: Trying to empty merge buffer"
923 " but we are not writing.\n");
927 size_t aligned
= roundup(tape
->valid
, tape
->blk_size
);
929 memset(tape
->cur
, 0, aligned
- tape
->valid
);
930 idetape_queue_rw_tail(drive
, REQ_IDETAPE_WRITE
, aligned
);
934 tape
->chrdev_dir
= IDETAPE_DIR_NONE
;
937 static int idetape_init_rw(ide_drive_t
*drive
, int dir
)
939 idetape_tape_t
*tape
= drive
->driver_data
;
942 BUG_ON(dir
!= IDETAPE_DIR_READ
&& dir
!= IDETAPE_DIR_WRITE
);
944 if (tape
->chrdev_dir
== dir
)
947 if (tape
->chrdev_dir
== IDETAPE_DIR_READ
)
948 ide_tape_discard_merge_buffer(drive
, 1);
949 else if (tape
->chrdev_dir
== IDETAPE_DIR_WRITE
) {
950 ide_tape_flush_merge_buffer(drive
);
951 idetape_flush_tape_buffers(drive
);
954 if (tape
->buf
|| tape
->valid
) {
955 printk(KERN_ERR
"ide-tape: valid should be 0 now\n");
959 tape
->buf
= kmalloc(tape
->buffer_size
, GFP_KERNEL
);
962 tape
->chrdev_dir
= dir
;
963 tape
->cur
= tape
->buf
;
966 * Issue a 0 rw command to ensure that DSC handshake is
967 * switched from completion mode to buffer available mode. No
968 * point in issuing this if DSC overlap isn't supported, some
969 * drives (Seagate STT3401A) will return an error.
971 if (drive
->dev_flags
& IDE_DFLAG_DSC_OVERLAP
) {
972 int cmd
= dir
== IDETAPE_DIR_READ
? REQ_IDETAPE_READ
975 rc
= idetape_queue_rw_tail(drive
, cmd
, 0);
979 tape
->chrdev_dir
= IDETAPE_DIR_NONE
;
987 static void idetape_pad_zeros(ide_drive_t
*drive
, int bcount
)
989 idetape_tape_t
*tape
= drive
->driver_data
;
991 memset(tape
->buf
, 0, tape
->buffer_size
);
994 unsigned int count
= min(tape
->buffer_size
, bcount
);
996 idetape_queue_rw_tail(drive
, REQ_IDETAPE_WRITE
, count
);
1002 * Rewinds the tape to the Beginning Of the current Partition (BOP). We
1003 * currently support only one partition.
1005 static int idetape_rewind_tape(ide_drive_t
*drive
)
1007 struct ide_tape_obj
*tape
= drive
->driver_data
;
1008 struct gendisk
*disk
= tape
->disk
;
1009 struct ide_atapi_pc pc
;
1012 ide_debug_log(IDE_DBG_FUNC
, "enter");
1014 idetape_create_rewind_cmd(drive
, &pc
);
1015 ret
= ide_queue_pc_tail(drive
, disk
, &pc
, NULL
, 0);
1019 ret
= ide_tape_read_position(drive
);
1025 /* mtio.h compatible commands should be issued to the chrdev interface. */
1026 static int idetape_blkdev_ioctl(ide_drive_t
*drive
, unsigned int cmd
,
1029 idetape_tape_t
*tape
= drive
->driver_data
;
1030 void __user
*argp
= (void __user
*)arg
;
1032 struct idetape_config
{
1033 int dsc_rw_frequency
;
1034 int dsc_media_access_frequency
;
1038 ide_debug_log(IDE_DBG_FUNC
, "cmd: 0x%04x", cmd
);
1042 if (copy_from_user(&config
, argp
, sizeof(config
)))
1044 tape
->best_dsc_rw_freq
= config
.dsc_rw_frequency
;
1047 memset(&config
, 0, sizeof(config
));
1048 config
.dsc_rw_frequency
= (int) tape
->best_dsc_rw_freq
;
1049 config
.nr_stages
= 1;
1050 if (copy_to_user(argp
, &config
, sizeof(config
)))
1059 static int idetape_space_over_filemarks(ide_drive_t
*drive
, short mt_op
,
1062 idetape_tape_t
*tape
= drive
->driver_data
;
1063 struct gendisk
*disk
= tape
->disk
;
1064 struct ide_atapi_pc pc
;
1065 int retval
, count
= 0;
1066 int sprev
= !!(tape
->caps
[4] & 0x20);
1069 ide_debug_log(IDE_DBG_FUNC
, "mt_op: %d, mt_count: %d", mt_op
, mt_count
);
1073 if (MTBSF
== mt_op
|| MTBSFM
== mt_op
) {
1076 mt_count
= -mt_count
;
1079 if (tape
->chrdev_dir
== IDETAPE_DIR_READ
) {
1081 if (test_and_clear_bit(ilog2(IDE_AFLAG_FILEMARK
),
1082 &drive
->atapi_flags
))
1084 ide_tape_discard_merge_buffer(drive
, 0);
1090 idetape_create_space_cmd(&pc
, mt_count
- count
,
1091 IDETAPE_SPACE_OVER_FILEMARK
);
1092 return ide_queue_pc_tail(drive
, disk
, &pc
, NULL
, 0);
1097 retval
= idetape_space_over_filemarks(drive
, MTFSF
,
1101 count
= (MTBSFM
== mt_op
? 1 : -1);
1102 return idetape_space_over_filemarks(drive
, MTFSF
, count
);
1104 printk(KERN_ERR
"ide-tape: MTIO operation %d not supported\n",
1111 * Our character device read / write functions.
1113 * The tape is optimized to maximize throughput when it is transferring an
1114 * integral number of the "continuous transfer limit", which is a parameter of
1115 * the specific tape (26kB on my particular tape, 32kB for Onstream).
1117 * As of version 1.3 of the driver, the character device provides an abstract
1118 * continuous view of the media - any mix of block sizes (even 1 byte) on the
1119 * same backup/restore procedure is supported. The driver will internally
1120 * convert the requests to the recommended transfer unit, so that an unmatch
1121 * between the user's block size to the recommended size will only result in a
1122 * (slightly) increased driver overhead, but will no longer hit performance.
1123 * This is not applicable to Onstream.
1125 static ssize_t
idetape_chrdev_read(struct file
*file
, char __user
*buf
,
1126 size_t count
, loff_t
*ppos
)
1128 struct ide_tape_obj
*tape
= file
->private_data
;
1129 ide_drive_t
*drive
= tape
->drive
;
1134 ide_debug_log(IDE_DBG_FUNC
, "count %Zd", count
);
1136 if (tape
->chrdev_dir
!= IDETAPE_DIR_READ
) {
1137 if (test_bit(ilog2(IDE_AFLAG_DETECT_BS
), &drive
->atapi_flags
))
1138 if (count
> tape
->blk_size
&&
1139 (count
% tape
->blk_size
) == 0)
1140 tape
->user_bs_factor
= count
/ tape
->blk_size
;
1143 rc
= idetape_init_rw(drive
, IDETAPE_DIR_READ
);
1147 while (done
< count
) {
1150 /* refill if staging buffer is empty */
1152 /* If we are at a filemark, nothing more to read */
1153 if (test_bit(ilog2(IDE_AFLAG_FILEMARK
),
1154 &drive
->atapi_flags
))
1157 if (idetape_queue_rw_tail(drive
, REQ_IDETAPE_READ
,
1158 tape
->buffer_size
) <= 0)
1163 todo
= min_t(size_t, count
- done
, tape
->valid
);
1164 if (copy_to_user(buf
+ done
, tape
->cur
, todo
))
1168 tape
->valid
-= todo
;
1172 if (!done
&& test_bit(ilog2(IDE_AFLAG_FILEMARK
), &drive
->atapi_flags
)) {
1173 idetape_space_over_filemarks(drive
, MTFSF
, 1);
1177 return ret
? ret
: done
;
1180 static ssize_t
idetape_chrdev_write(struct file
*file
, const char __user
*buf
,
1181 size_t count
, loff_t
*ppos
)
1183 struct ide_tape_obj
*tape
= file
->private_data
;
1184 ide_drive_t
*drive
= tape
->drive
;
1189 /* The drive is write protected. */
1190 if (tape
->write_prot
)
1193 ide_debug_log(IDE_DBG_FUNC
, "count %Zd", count
);
1195 /* Initialize write operation */
1196 rc
= idetape_init_rw(drive
, IDETAPE_DIR_WRITE
);
1200 while (done
< count
) {
1203 /* flush if staging buffer is full */
1204 if (tape
->valid
== tape
->buffer_size
&&
1205 idetape_queue_rw_tail(drive
, REQ_IDETAPE_WRITE
,
1206 tape
->buffer_size
) <= 0)
1210 todo
= min_t(size_t, count
- done
,
1211 tape
->buffer_size
- tape
->valid
);
1212 if (copy_from_user(tape
->cur
, buf
+ done
, todo
))
1216 tape
->valid
+= todo
;
1220 return ret
? ret
: done
;
1223 static int idetape_write_filemark(ide_drive_t
*drive
)
1225 struct ide_tape_obj
*tape
= drive
->driver_data
;
1226 struct ide_atapi_pc pc
;
1228 /* Write a filemark */
1229 idetape_create_write_filemark_cmd(drive
, &pc
, 1);
1230 if (ide_queue_pc_tail(drive
, tape
->disk
, &pc
, NULL
, 0)) {
1231 printk(KERN_ERR
"ide-tape: Couldn't write a filemark\n");
1238 * Called from idetape_chrdev_ioctl when the general mtio MTIOCTOP ioctl is
1241 * Note: MTBSF and MTBSFM are not supported when the tape doesn't support
1242 * spacing over filemarks in the reverse direction. In this case, MTFSFM is also
1243 * usually not supported.
1245 * The following commands are currently not supported:
1247 * MTFSS, MTBSS, MTWSM, MTSETDENSITY, MTSETDRVBUFFER, MT_ST_BOOLEANS,
1248 * MT_ST_WRITE_THRESHOLD.
1250 static int idetape_mtioctop(ide_drive_t
*drive
, short mt_op
, int mt_count
)
1252 idetape_tape_t
*tape
= drive
->driver_data
;
1253 struct gendisk
*disk
= tape
->disk
;
1254 struct ide_atapi_pc pc
;
1257 ide_debug_log(IDE_DBG_FUNC
, "MTIOCTOP ioctl: mt_op: %d, mt_count: %d",
1267 return idetape_space_over_filemarks(drive
, mt_op
, mt_count
);
1274 if (tape
->write_prot
)
1276 ide_tape_discard_merge_buffer(drive
, 1);
1277 for (i
= 0; i
< mt_count
; i
++) {
1278 retval
= idetape_write_filemark(drive
);
1284 ide_tape_discard_merge_buffer(drive
, 0);
1285 if (idetape_rewind_tape(drive
))
1289 ide_tape_discard_merge_buffer(drive
, 0);
1290 return ide_do_start_stop(drive
, disk
, IDETAPE_LU_LOAD_MASK
);
1294 * If door is locked, attempt to unlock before
1295 * attempting to eject.
1297 if (tape
->door_locked
) {
1298 if (!ide_set_media_lock(drive
, disk
, 0))
1299 tape
->door_locked
= DOOR_UNLOCKED
;
1301 ide_tape_discard_merge_buffer(drive
, 0);
1302 retval
= ide_do_start_stop(drive
, disk
, !IDETAPE_LU_LOAD_MASK
);
1304 clear_bit(ilog2(IDE_AFLAG_MEDIUM_PRESENT
),
1305 &drive
->atapi_flags
);
1308 ide_tape_discard_merge_buffer(drive
, 0);
1309 return idetape_flush_tape_buffers(drive
);
1311 ide_tape_discard_merge_buffer(drive
, 0);
1312 return ide_do_start_stop(drive
, disk
,
1313 IDETAPE_LU_RETENSION_MASK
| IDETAPE_LU_LOAD_MASK
);
1315 idetape_create_space_cmd(&pc
, 0, IDETAPE_SPACE_TO_EOD
);
1316 return ide_queue_pc_tail(drive
, disk
, &pc
, NULL
, 0);
1318 (void)idetape_rewind_tape(drive
);
1319 idetape_create_erase_cmd(&pc
);
1320 return ide_queue_pc_tail(drive
, disk
, &pc
, NULL
, 0);
1323 if (mt_count
< tape
->blk_size
||
1324 mt_count
% tape
->blk_size
)
1326 tape
->user_bs_factor
= mt_count
/ tape
->blk_size
;
1327 clear_bit(ilog2(IDE_AFLAG_DETECT_BS
),
1328 &drive
->atapi_flags
);
1330 set_bit(ilog2(IDE_AFLAG_DETECT_BS
),
1331 &drive
->atapi_flags
);
1334 ide_tape_discard_merge_buffer(drive
, 0);
1335 return idetape_position_tape(drive
,
1336 mt_count
* tape
->user_bs_factor
, tape
->partition
, 0);
1338 ide_tape_discard_merge_buffer(drive
, 0);
1339 return idetape_position_tape(drive
, 0, mt_count
, 0);
1343 retval
= ide_set_media_lock(drive
, disk
, 1);
1346 tape
->door_locked
= DOOR_EXPLICITLY_LOCKED
;
1349 retval
= ide_set_media_lock(drive
, disk
, 0);
1352 tape
->door_locked
= DOOR_UNLOCKED
;
1355 printk(KERN_ERR
"ide-tape: MTIO operation %d not supported\n",
1362 * Our character device ioctls. General mtio.h magnetic io commands are
1363 * supported here, and not in the corresponding block interface. Our own
1364 * ide-tape ioctls are supported on both interfaces.
1366 static int idetape_chrdev_ioctl(struct inode
*inode
, struct file
*file
,
1367 unsigned int cmd
, unsigned long arg
)
1369 struct ide_tape_obj
*tape
= file
->private_data
;
1370 ide_drive_t
*drive
= tape
->drive
;
1374 int block_offset
= 0, position
= tape
->first_frame
;
1375 void __user
*argp
= (void __user
*)arg
;
1377 ide_debug_log(IDE_DBG_FUNC
, "cmd: 0x%x", cmd
);
1379 if (tape
->chrdev_dir
== IDETAPE_DIR_WRITE
) {
1380 ide_tape_flush_merge_buffer(drive
);
1381 idetape_flush_tape_buffers(drive
);
1383 if (cmd
== MTIOCGET
|| cmd
== MTIOCPOS
) {
1384 block_offset
= tape
->valid
/
1385 (tape
->blk_size
* tape
->user_bs_factor
);
1386 position
= ide_tape_read_position(drive
);
1392 if (copy_from_user(&mtop
, argp
, sizeof(struct mtop
)))
1394 return idetape_mtioctop(drive
, mtop
.mt_op
, mtop
.mt_count
);
1396 memset(&mtget
, 0, sizeof(struct mtget
));
1397 mtget
.mt_type
= MT_ISSCSI2
;
1398 mtget
.mt_blkno
= position
/ tape
->user_bs_factor
- block_offset
;
1400 ((tape
->blk_size
* tape
->user_bs_factor
)
1401 << MT_ST_BLKSIZE_SHIFT
) & MT_ST_BLKSIZE_MASK
;
1403 if (tape
->drv_write_prot
)
1404 mtget
.mt_gstat
|= GMT_WR_PROT(0xffffffff);
1406 if (copy_to_user(argp
, &mtget
, sizeof(struct mtget
)))
1410 mtpos
.mt_blkno
= position
/ tape
->user_bs_factor
- block_offset
;
1411 if (copy_to_user(argp
, &mtpos
, sizeof(struct mtpos
)))
1415 if (tape
->chrdev_dir
== IDETAPE_DIR_READ
)
1416 ide_tape_discard_merge_buffer(drive
, 1);
1417 return idetape_blkdev_ioctl(drive
, cmd
, arg
);
1422 * Do a mode sense page 0 with block descriptor and if it succeeds set the tape
1423 * block size with the reported value.
1425 static void ide_tape_get_bsize_from_bdesc(ide_drive_t
*drive
)
1427 idetape_tape_t
*tape
= drive
->driver_data
;
1428 struct ide_atapi_pc pc
;
1431 idetape_create_mode_sense_cmd(&pc
, IDETAPE_BLOCK_DESCRIPTOR
);
1432 if (ide_queue_pc_tail(drive
, tape
->disk
, &pc
, buf
, pc
.req_xfer
)) {
1433 printk(KERN_ERR
"ide-tape: Can't get block descriptor\n");
1434 if (tape
->blk_size
== 0) {
1435 printk(KERN_WARNING
"ide-tape: Cannot deal with zero "
1436 "block size, assuming 32k\n");
1437 tape
->blk_size
= 32768;
1441 tape
->blk_size
= (buf
[4 + 5] << 16) +
1444 tape
->drv_write_prot
= (buf
[2] & 0x80) >> 7;
1446 ide_debug_log(IDE_DBG_FUNC
, "blk_size: %d, write_prot: %d",
1447 tape
->blk_size
, tape
->drv_write_prot
);
1450 static int idetape_chrdev_open(struct inode
*inode
, struct file
*filp
)
1452 unsigned int minor
= iminor(inode
), i
= minor
& ~0xc0;
1454 idetape_tape_t
*tape
;
1457 if (i
>= MAX_HWIFS
* MAX_DRIVES
)
1461 tape
= ide_tape_get(NULL
, true, i
);
1467 drive
= tape
->drive
;
1468 filp
->private_data
= tape
;
1470 ide_debug_log(IDE_DBG_FUNC
, "enter");
1473 * We really want to do nonseekable_open(inode, filp); here, but some
1474 * versions of tar incorrectly call lseek on tapes and bail out if that
1475 * fails. So we disallow pread() and pwrite(), but permit lseeks.
1477 filp
->f_mode
&= ~(FMODE_PREAD
| FMODE_PWRITE
);
1480 if (test_and_set_bit(ilog2(IDE_AFLAG_BUSY
), &drive
->atapi_flags
)) {
1485 retval
= idetape_wait_ready(drive
, 60 * HZ
);
1487 clear_bit(ilog2(IDE_AFLAG_BUSY
), &drive
->atapi_flags
);
1488 printk(KERN_ERR
"ide-tape: %s: drive not ready\n", tape
->name
);
1492 ide_tape_read_position(drive
);
1493 if (!test_bit(ilog2(IDE_AFLAG_ADDRESS_VALID
), &drive
->atapi_flags
))
1494 (void)idetape_rewind_tape(drive
);
1496 /* Read block size and write protect status from drive. */
1497 ide_tape_get_bsize_from_bdesc(drive
);
1499 /* Set write protect flag if device is opened as read-only. */
1500 if ((filp
->f_flags
& O_ACCMODE
) == O_RDONLY
)
1501 tape
->write_prot
= 1;
1503 tape
->write_prot
= tape
->drv_write_prot
;
1505 /* Make sure drive isn't write protected if user wants to write. */
1506 if (tape
->write_prot
) {
1507 if ((filp
->f_flags
& O_ACCMODE
) == O_WRONLY
||
1508 (filp
->f_flags
& O_ACCMODE
) == O_RDWR
) {
1509 clear_bit(ilog2(IDE_AFLAG_BUSY
), &drive
->atapi_flags
);
1515 /* Lock the tape drive door so user can't eject. */
1516 if (tape
->chrdev_dir
== IDETAPE_DIR_NONE
) {
1517 if (!ide_set_media_lock(drive
, tape
->disk
, 1)) {
1518 if (tape
->door_locked
!= DOOR_EXPLICITLY_LOCKED
)
1519 tape
->door_locked
= DOOR_LOCKED
;
1531 static void idetape_write_release(ide_drive_t
*drive
, unsigned int minor
)
1533 idetape_tape_t
*tape
= drive
->driver_data
;
1535 ide_tape_flush_merge_buffer(drive
);
1536 tape
->buf
= kmalloc(tape
->buffer_size
, GFP_KERNEL
);
1537 if (tape
->buf
!= NULL
) {
1538 idetape_pad_zeros(drive
, tape
->blk_size
*
1539 (tape
->user_bs_factor
- 1));
1543 idetape_write_filemark(drive
);
1544 idetape_flush_tape_buffers(drive
);
1545 idetape_flush_tape_buffers(drive
);
1548 static int idetape_chrdev_release(struct inode
*inode
, struct file
*filp
)
1550 struct ide_tape_obj
*tape
= filp
->private_data
;
1551 ide_drive_t
*drive
= tape
->drive
;
1552 unsigned int minor
= iminor(inode
);
1555 tape
= drive
->driver_data
;
1557 ide_debug_log(IDE_DBG_FUNC
, "enter");
1559 if (tape
->chrdev_dir
== IDETAPE_DIR_WRITE
)
1560 idetape_write_release(drive
, minor
);
1561 if (tape
->chrdev_dir
== IDETAPE_DIR_READ
) {
1563 ide_tape_discard_merge_buffer(drive
, 1);
1566 if (minor
< 128 && test_bit(ilog2(IDE_AFLAG_MEDIUM_PRESENT
),
1567 &drive
->atapi_flags
))
1568 (void) idetape_rewind_tape(drive
);
1570 if (tape
->chrdev_dir
== IDETAPE_DIR_NONE
) {
1571 if (tape
->door_locked
== DOOR_LOCKED
) {
1572 if (!ide_set_media_lock(drive
, tape
->disk
, 0))
1573 tape
->door_locked
= DOOR_UNLOCKED
;
1576 clear_bit(ilog2(IDE_AFLAG_BUSY
), &drive
->atapi_flags
);
1582 static void idetape_get_inquiry_results(ide_drive_t
*drive
)
1584 idetape_tape_t
*tape
= drive
->driver_data
;
1585 struct ide_atapi_pc pc
;
1587 char fw_rev
[4], vendor_id
[8], product_id
[16];
1589 idetape_create_inquiry_cmd(&pc
);
1590 if (ide_queue_pc_tail(drive
, tape
->disk
, &pc
, pc_buf
, pc
.req_xfer
)) {
1591 printk(KERN_ERR
"ide-tape: %s: can't get INQUIRY results\n",
1595 memcpy(vendor_id
, &pc_buf
[8], 8);
1596 memcpy(product_id
, &pc_buf
[16], 16);
1597 memcpy(fw_rev
, &pc_buf
[32], 4);
1599 ide_fixstring(vendor_id
, 8, 0);
1600 ide_fixstring(product_id
, 16, 0);
1601 ide_fixstring(fw_rev
, 4, 0);
1603 printk(KERN_INFO
"ide-tape: %s <-> %s: %.8s %.16s rev %.4s\n",
1604 drive
->name
, tape
->name
, vendor_id
, product_id
, fw_rev
);
1608 * Ask the tape about its various parameters. In particular, we will adjust our
1609 * data transfer buffer size to the recommended value as returned by the tape.
1611 static void idetape_get_mode_sense_results(ide_drive_t
*drive
)
1613 idetape_tape_t
*tape
= drive
->driver_data
;
1614 struct ide_atapi_pc pc
;
1616 u8 speed
, max_speed
;
1618 idetape_create_mode_sense_cmd(&pc
, IDETAPE_CAPABILITIES_PAGE
);
1619 if (ide_queue_pc_tail(drive
, tape
->disk
, &pc
, buf
, pc
.req_xfer
)) {
1620 printk(KERN_ERR
"ide-tape: Can't get tape parameters - assuming"
1621 " some default values\n");
1622 tape
->blk_size
= 512;
1623 put_unaligned(52, (u16
*)&tape
->caps
[12]);
1624 put_unaligned(540, (u16
*)&tape
->caps
[14]);
1625 put_unaligned(6*52, (u16
*)&tape
->caps
[16]);
1628 caps
= buf
+ 4 + buf
[3];
1630 /* convert to host order and save for later use */
1631 speed
= be16_to_cpup((__be16
*)&caps
[14]);
1632 max_speed
= be16_to_cpup((__be16
*)&caps
[8]);
1634 *(u16
*)&caps
[8] = max_speed
;
1635 *(u16
*)&caps
[12] = be16_to_cpup((__be16
*)&caps
[12]);
1636 *(u16
*)&caps
[14] = speed
;
1637 *(u16
*)&caps
[16] = be16_to_cpup((__be16
*)&caps
[16]);
1640 printk(KERN_INFO
"ide-tape: %s: invalid tape speed "
1641 "(assuming 650KB/sec)\n", drive
->name
);
1642 *(u16
*)&caps
[14] = 650;
1645 printk(KERN_INFO
"ide-tape: %s: invalid max_speed "
1646 "(assuming 650KB/sec)\n", drive
->name
);
1647 *(u16
*)&caps
[8] = 650;
1650 memcpy(&tape
->caps
, caps
, 20);
1652 /* device lacks locking support according to capabilities page */
1653 if ((caps
[6] & 1) == 0)
1654 drive
->dev_flags
&= ~IDE_DFLAG_DOORLOCKING
;
1657 tape
->blk_size
= 512;
1658 else if (caps
[7] & 0x04)
1659 tape
->blk_size
= 1024;
1662 #ifdef CONFIG_IDE_PROC_FS
1663 #define ide_tape_devset_get(name, field) \
1664 static int get_##name(ide_drive_t *drive) \
1666 idetape_tape_t *tape = drive->driver_data; \
1667 return tape->field; \
1670 #define ide_tape_devset_set(name, field) \
1671 static int set_##name(ide_drive_t *drive, int arg) \
1673 idetape_tape_t *tape = drive->driver_data; \
1674 tape->field = arg; \
1678 #define ide_tape_devset_rw_field(_name, _field) \
1679 ide_tape_devset_get(_name, _field) \
1680 ide_tape_devset_set(_name, _field) \
1681 IDE_DEVSET(_name, DS_SYNC, get_##_name, set_##_name)
1683 #define ide_tape_devset_r_field(_name, _field) \
1684 ide_tape_devset_get(_name, _field) \
1685 IDE_DEVSET(_name, 0, get_##_name, NULL)
1687 static int mulf_tdsc(ide_drive_t
*drive
) { return 1000; }
1688 static int divf_tdsc(ide_drive_t
*drive
) { return HZ
; }
1689 static int divf_buffer(ide_drive_t
*drive
) { return 2; }
1690 static int divf_buffer_size(ide_drive_t
*drive
) { return 1024; }
1692 ide_devset_rw_flag(dsc_overlap
, IDE_DFLAG_DSC_OVERLAP
);
1694 ide_tape_devset_rw_field(tdsc
, best_dsc_rw_freq
);
1696 ide_tape_devset_r_field(avg_speed
, avg_speed
);
1697 ide_tape_devset_r_field(speed
, caps
[14]);
1698 ide_tape_devset_r_field(buffer
, caps
[16]);
1699 ide_tape_devset_r_field(buffer_size
, buffer_size
);
1701 static const struct ide_proc_devset idetape_settings
[] = {
1702 __IDE_PROC_DEVSET(avg_speed
, 0, 0xffff, NULL
, NULL
),
1703 __IDE_PROC_DEVSET(buffer
, 0, 0xffff, NULL
, divf_buffer
),
1704 __IDE_PROC_DEVSET(buffer_size
, 0, 0xffff, NULL
, divf_buffer_size
),
1705 __IDE_PROC_DEVSET(dsc_overlap
, 0, 1, NULL
, NULL
),
1706 __IDE_PROC_DEVSET(speed
, 0, 0xffff, NULL
, NULL
),
1707 __IDE_PROC_DEVSET(tdsc
, IDETAPE_DSC_RW_MIN
, IDETAPE_DSC_RW_MAX
,
1708 mulf_tdsc
, divf_tdsc
),
1714 * The function below is called to:
1716 * 1. Initialize our various state variables.
1717 * 2. Ask the tape for its capabilities.
1718 * 3. Allocate a buffer which will be used for data transfer. The buffer size
1719 * is chosen based on the recommendation which we received in step 2.
1721 * Note that at this point ide.c already assigned us an irq, so that we can
1722 * queue requests here and wait for their completion.
1724 static void idetape_setup(ide_drive_t
*drive
, idetape_tape_t
*tape
, int minor
)
1729 u16
*ctl
= (u16
*)&tape
->caps
[12];
1731 ide_debug_log(IDE_DBG_FUNC
, "minor: %d", minor
);
1733 drive
->pc_callback
= ide_tape_callback
;
1735 drive
->dev_flags
|= IDE_DFLAG_DSC_OVERLAP
;
1737 if (drive
->hwif
->host_flags
& IDE_HFLAG_NO_DSC
) {
1738 printk(KERN_INFO
"ide-tape: %s: disabling DSC overlap\n",
1740 drive
->dev_flags
&= ~IDE_DFLAG_DSC_OVERLAP
;
1743 /* Seagate Travan drives do not support DSC overlap. */
1744 if (strstr((char *)&drive
->id
[ATA_ID_PROD
], "Seagate STT3401"))
1745 drive
->dev_flags
&= ~IDE_DFLAG_DSC_OVERLAP
;
1747 tape
->minor
= minor
;
1748 tape
->name
[0] = 'h';
1749 tape
->name
[1] = 't';
1750 tape
->name
[2] = '0' + minor
;
1751 tape
->chrdev_dir
= IDETAPE_DIR_NONE
;
1753 idetape_get_inquiry_results(drive
);
1754 idetape_get_mode_sense_results(drive
);
1755 ide_tape_get_bsize_from_bdesc(drive
);
1756 tape
->user_bs_factor
= 1;
1757 tape
->buffer_size
= *ctl
* tape
->blk_size
;
1758 while (tape
->buffer_size
> 0xffff) {
1759 printk(KERN_NOTICE
"ide-tape: decreasing stage size\n");
1761 tape
->buffer_size
= *ctl
* tape
->blk_size
;
1763 buffer_size
= tape
->buffer_size
;
1765 /* select the "best" DSC read/write polling freq */
1766 speed
= max(*(u16
*)&tape
->caps
[14], *(u16
*)&tape
->caps
[8]);
1768 t
= (IDETAPE_FIFO_THRESHOLD
* tape
->buffer_size
* HZ
) / (speed
* 1000);
1771 * Ensure that the number we got makes sense; limit it within
1772 * IDETAPE_DSC_RW_MIN and IDETAPE_DSC_RW_MAX.
1774 tape
->best_dsc_rw_freq
= clamp_t(unsigned long, t
, IDETAPE_DSC_RW_MIN
,
1775 IDETAPE_DSC_RW_MAX
);
1776 printk(KERN_INFO
"ide-tape: %s <-> %s: %dKBps, %d*%dkB buffer, "
1778 drive
->name
, tape
->name
, *(u16
*)&tape
->caps
[14],
1779 (*(u16
*)&tape
->caps
[16] * 512) / tape
->buffer_size
,
1780 tape
->buffer_size
/ 1024,
1781 tape
->best_dsc_rw_freq
* 1000 / HZ
,
1782 (drive
->dev_flags
& IDE_DFLAG_USING_DMA
) ? ", DMA" : "");
1784 ide_proc_register_driver(drive
, tape
->driver
);
1787 static void ide_tape_remove(ide_drive_t
*drive
)
1789 idetape_tape_t
*tape
= drive
->driver_data
;
1791 ide_proc_unregister_driver(drive
, tape
->driver
);
1792 device_del(&tape
->dev
);
1793 ide_unregister_region(tape
->disk
);
1795 mutex_lock(&idetape_ref_mutex
);
1796 put_device(&tape
->dev
);
1797 mutex_unlock(&idetape_ref_mutex
);
1800 static void ide_tape_release(struct device
*dev
)
1802 struct ide_tape_obj
*tape
= to_ide_drv(dev
, ide_tape_obj
);
1803 ide_drive_t
*drive
= tape
->drive
;
1804 struct gendisk
*g
= tape
->disk
;
1806 BUG_ON(tape
->valid
);
1808 drive
->dev_flags
&= ~IDE_DFLAG_DSC_OVERLAP
;
1809 drive
->driver_data
= NULL
;
1810 device_destroy(idetape_sysfs_class
, MKDEV(IDETAPE_MAJOR
, tape
->minor
));
1811 device_destroy(idetape_sysfs_class
,
1812 MKDEV(IDETAPE_MAJOR
, tape
->minor
+ 128));
1813 idetape_devs
[tape
->minor
] = NULL
;
1814 g
->private_data
= NULL
;
1819 #ifdef CONFIG_IDE_PROC_FS
1820 static int idetape_name_proc_show(struct seq_file
*m
, void *v
)
1822 ide_drive_t
*drive
= (ide_drive_t
*) m
->private;
1823 idetape_tape_t
*tape
= drive
->driver_data
;
1825 seq_printf(m
, "%s\n", tape
->name
);
1829 static int idetape_name_proc_open(struct inode
*inode
, struct file
*file
)
1831 return single_open(file
, idetape_name_proc_show
, PDE(inode
)->data
);
1834 static const struct file_operations idetape_name_proc_fops
= {
1835 .owner
= THIS_MODULE
,
1836 .open
= idetape_name_proc_open
,
1838 .llseek
= seq_lseek
,
1839 .release
= single_release
,
1842 static ide_proc_entry_t idetape_proc
[] = {
1843 { "capacity", S_IFREG
|S_IRUGO
, &ide_capacity_proc_fops
},
1844 { "name", S_IFREG
|S_IRUGO
, &idetape_name_proc_fops
},
1848 static ide_proc_entry_t
*ide_tape_proc_entries(ide_drive_t
*drive
)
1850 return idetape_proc
;
1853 static const struct ide_proc_devset
*ide_tape_proc_devsets(ide_drive_t
*drive
)
1855 return idetape_settings
;
1859 static int ide_tape_probe(ide_drive_t
*);
1861 static struct ide_driver idetape_driver
= {
1863 .owner
= THIS_MODULE
,
1865 .bus
= &ide_bus_type
,
1867 .probe
= ide_tape_probe
,
1868 .remove
= ide_tape_remove
,
1869 .version
= IDETAPE_VERSION
,
1870 .do_request
= idetape_do_request
,
1871 #ifdef CONFIG_IDE_PROC_FS
1872 .proc_entries
= ide_tape_proc_entries
,
1873 .proc_devsets
= ide_tape_proc_devsets
,
1877 /* Our character device supporting functions, passed to register_chrdev. */
1878 static const struct file_operations idetape_fops
= {
1879 .owner
= THIS_MODULE
,
1880 .read
= idetape_chrdev_read
,
1881 .write
= idetape_chrdev_write
,
1882 .ioctl
= idetape_chrdev_ioctl
,
1883 .open
= idetape_chrdev_open
,
1884 .release
= idetape_chrdev_release
,
1887 static int idetape_open(struct block_device
*bdev
, fmode_t mode
)
1889 struct ide_tape_obj
*tape
= ide_tape_get(bdev
->bd_disk
, false, 0);
1897 static int idetape_release(struct gendisk
*disk
, fmode_t mode
)
1899 struct ide_tape_obj
*tape
= ide_drv_g(disk
, ide_tape_obj
);
1905 static int idetape_ioctl(struct block_device
*bdev
, fmode_t mode
,
1906 unsigned int cmd
, unsigned long arg
)
1908 struct ide_tape_obj
*tape
= ide_drv_g(bdev
->bd_disk
, ide_tape_obj
);
1909 ide_drive_t
*drive
= tape
->drive
;
1910 int err
= generic_ide_ioctl(drive
, bdev
, cmd
, arg
);
1912 err
= idetape_blkdev_ioctl(drive
, cmd
, arg
);
1916 static const struct block_device_operations idetape_block_ops
= {
1917 .owner
= THIS_MODULE
,
1918 .open
= idetape_open
,
1919 .release
= idetape_release
,
1920 .locked_ioctl
= idetape_ioctl
,
1923 static int ide_tape_probe(ide_drive_t
*drive
)
1925 idetape_tape_t
*tape
;
1929 ide_debug_log(IDE_DBG_FUNC
, "enter");
1931 if (!strstr(DRV_NAME
, drive
->driver_req
))
1934 if (drive
->media
!= ide_tape
)
1937 if ((drive
->dev_flags
& IDE_DFLAG_ID_READ
) &&
1938 ide_check_atapi_device(drive
, DRV_NAME
) == 0) {
1939 printk(KERN_ERR
"ide-tape: %s: not supported by this version of"
1940 " the driver\n", drive
->name
);
1943 tape
= kzalloc(sizeof(idetape_tape_t
), GFP_KERNEL
);
1945 printk(KERN_ERR
"ide-tape: %s: Can't allocate a tape struct\n",
1950 g
= alloc_disk(1 << PARTN_BITS
);
1954 ide_init_disk(g
, drive
);
1956 tape
->dev
.parent
= &drive
->gendev
;
1957 tape
->dev
.release
= ide_tape_release
;
1958 dev_set_name(&tape
->dev
, dev_name(&drive
->gendev
));
1960 if (device_register(&tape
->dev
))
1963 tape
->drive
= drive
;
1964 tape
->driver
= &idetape_driver
;
1967 g
->private_data
= &tape
->driver
;
1969 drive
->driver_data
= tape
;
1971 mutex_lock(&idetape_ref_mutex
);
1972 for (minor
= 0; idetape_devs
[minor
]; minor
++)
1974 idetape_devs
[minor
] = tape
;
1975 mutex_unlock(&idetape_ref_mutex
);
1977 idetape_setup(drive
, tape
, minor
);
1979 device_create(idetape_sysfs_class
, &drive
->gendev
,
1980 MKDEV(IDETAPE_MAJOR
, minor
), NULL
, "%s", tape
->name
);
1981 device_create(idetape_sysfs_class
, &drive
->gendev
,
1982 MKDEV(IDETAPE_MAJOR
, minor
+ 128), NULL
,
1985 g
->fops
= &idetape_block_ops
;
1986 ide_register_region(g
);
1998 static void __exit
idetape_exit(void)
2000 driver_unregister(&idetape_driver
.gen_driver
);
2001 class_destroy(idetape_sysfs_class
);
2002 unregister_chrdev(IDETAPE_MAJOR
, "ht");
2005 static int __init
idetape_init(void)
2008 idetape_sysfs_class
= class_create(THIS_MODULE
, "ide_tape");
2009 if (IS_ERR(idetape_sysfs_class
)) {
2010 idetape_sysfs_class
= NULL
;
2011 printk(KERN_ERR
"Unable to create sysfs class for ide tapes\n");
2016 if (register_chrdev(IDETAPE_MAJOR
, "ht", &idetape_fops
)) {
2017 printk(KERN_ERR
"ide-tape: Failed to register chrdev"
2020 goto out_free_class
;
2023 error
= driver_register(&idetape_driver
.gen_driver
);
2025 goto out_free_driver
;
2030 driver_unregister(&idetape_driver
.gen_driver
);
2032 class_destroy(idetape_sysfs_class
);
2037 MODULE_ALIAS("ide:*m-tape*");
2038 module_init(idetape_init
);
2039 module_exit(idetape_exit
);
2040 MODULE_ALIAS_CHARDEV_MAJOR(IDETAPE_MAJOR
);
2041 MODULE_DESCRIPTION("ATAPI Streaming TAPE Driver");
2042 MODULE_LICENSE("GPL");