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>
32 #include <linux/errno.h>
35 #include <linux/slab.h>
36 #include <linux/pci.h>
39 #include <linux/bitops.h>
41 #include <scsi/scsi.h>
43 #include <asm/byteorder.h>
46 #include <asm/unaligned.h>
50 #undef IDETAPE_DEBUG_LOG
52 #ifdef IDETAPE_DEBUG_LOG
53 #define ide_debug_log(lvl, fmt, args...) __ide_debug_log(lvl, fmt, ## args)
55 #define ide_debug_log(lvl, fmt, args...) do {} while (0)
65 #define IDETAPE_MAX_PC_RETRIES 3
73 #define IDETAPE_FIFO_THRESHOLD 2
101 #define IDETAPE_DSC_RW_MIN 5*HZ/100
102 #define IDETAPE_DSC_RW_MAX 40*HZ/100
103 #define IDETAPE_DSC_RW_TIMEOUT 2*60*HZ
104 #define IDETAPE_DSC_MA_FAST 2*HZ
105 #define IDETAPE_DSC_MA_THRESHOLD 5*60*HZ
106 #define IDETAPE_DSC_MA_SLOW 30*HZ
107 #define IDETAPE_DSC_MA_TIMEOUT 2*60*60*HZ
119 #define DOOR_UNLOCKED 0
120 #define DOOR_LOCKED 1
121 #define DOOR_EXPLICITLY_LOCKED 2
124 #define IDETAPE_SPACE_OVER_FILEMARK 1
125 #define IDETAPE_SPACE_TO_EOD 3
128 #define IDETAPE_LU_LOAD_MASK 1
129 #define IDETAPE_LU_RETENSION_MASK 2
130 #define IDETAPE_LU_EOT_MASK 4
133 #define IDETAPE_BLOCK_DESCRIPTOR 0
134 #define IDETAPE_CAPABILITIES_PAGE 0x2a
225 static struct class *idetape_sysfs_class;
227 static void ide_tape_release(
struct device *);
239 tape = idetape_devs[
i];
276 tape->
asc = sense[12];
277 tape->
ascq = sense[13];
280 "cmd: 0x%x, sense key = %x, asc = %x, ascq = %x",
294 && pc->
c[4] == 0 && pc->
c[3] == 0 && pc->
c[2] == 0) {
302 if (pc->
c[0] ==
READ_6 && (sense[2] & 0x80)) {
307 if ((sense[2] & 0x40) || (tape->
sense_key == 0xd
308 && tape->
asc == 0x0 && tape->
ascq == 0x2)) {
319 (blk_rq_bytes(rq) - rq->resid_len))
326 static int ide_tape_callback(
ide_drive_t *drive,
int dsc)
331 int uptodate = pc->
error ? 0 : 1;
338 ide_tape_handle_dsc(drive);
345 idetape_analyze_error(drive);
348 "itself - Aborting request!\n");
350 unsigned int blocks =
351 (blk_rq_bytes(rq) - rq->resid_len) / tape->
blk_size;
378 static void ide_tape_stall_queue(
ide_drive_t *drive)
390 static void ide_tape_handle_dsc(
ide_drive_t *drive)
399 ide_tape_stall_queue(drive);
463 (tape->
ascq == 1 || tape->
ascq == 8))) {
465 "pc = %2x, key = %2x, "
466 "asc = %2x, ascq = %2x\n",
467 tape->
name, pc->
c[0],
522 stat = hwif->
tp_ops->read_status(hwif);
576 rq->cmd[0], (
unsigned long long)blk_rq_pos(rq),
579 BUG_ON(!(rq->cmd_type == REQ_TYPE_SPECIAL ||
580 rq->cmd_type == REQ_TYPE_SENSE));
592 stat = hwif->
tp_ops->read_status(hwif);
613 idetape_media_access_finished(drive);
622 ide_tape_stall_queue(drive);
631 ide_tape_create_rw_cmd(tape, pc, rq,
READ_6);
636 ide_tape_create_rw_cmd(tape, pc, rq,
WRITE_6);
646 idetape_media_access_finished(drive);
655 memset(&cmd, 0,
sizeof(cmd));
665 return ide_tape_issue_pc(drive, &cmd, pc);
672 static void idetape_create_write_filemark_cmd(
ide_drive_t *drive,
677 pc->
c[4] = write_filemark;
684 struct gendisk *disk = tape->
disk;
685 int load_attempted = 0;
694 || (tape->
asc == 0x3A)) {
702 (tape->
ascq == 1 || tape->
ascq == 8)))
709 static int idetape_flush_tape_buffers(
ide_drive_t *drive)
715 idetape_create_write_filemark_cmd(drive, &pc, 0);
719 idetape_wait_ready(drive, 60 * 5 *
HZ);
723 static int ide_tape_read_position(
ide_drive_t *drive)
741 (buf[0] & 0x80) ?
"Yes" :
"No");
743 (buf[0] & 0x40) ?
"Yes" :
"No");
765 static void idetape_create_locate_cmd(
ide_drive_t *drive,
773 pc->
c[8] = partition;
777 static void __ide_tape_discard_merge_buffer(
ide_drive_t *drive)
800 static int idetape_position_tape(
ide_drive_t *drive,
unsigned int block,
801 u8 partition,
int skip)
804 struct gendisk *disk = tape->
disk;
809 __ide_tape_discard_merge_buffer(drive);
810 idetape_wait_ready(drive, 60 * 5 *
HZ);
811 idetape_create_locate_cmd(drive, &pc, block, partition, skip);
816 ret = ide_tape_read_position(drive);
822 static void ide_tape_discard_merge_buffer(
ide_drive_t *drive,
823 int restore_position)
828 __ide_tape_discard_merge_buffer(drive);
829 if (restore_position) {
830 position = ide_tape_read_position(drive);
831 seek = position > 0 ? position : 0;
832 if (idetape_position_tape(drive, seek, 0, 0)) {
834 " %s\n", tape->
name, __func__);
844 static int idetape_queue_rw_tail(
ide_drive_t *drive,
int cmd,
int size)
856 rq->cmd_type = REQ_TYPE_SPECIAL;
858 rq->rq_disk = tape->
disk;
871 size -= rq->resid_len;
886 static void idetape_create_inquiry_cmd(
struct ide_atapi_pc *pc)
894 static void idetape_create_rewind_cmd(
ide_drive_t *drive,
902 static void idetape_create_erase_cmd(
struct ide_atapi_pc *pc)
919 static void ide_tape_flush_merge_buffer(
ide_drive_t *drive)
925 " but we are not writing.\n");
939 static int idetape_init_rw(
ide_drive_t *drive,
int dir)
950 ide_tape_discard_merge_buffer(drive, 1);
952 ide_tape_flush_merge_buffer(drive);
953 idetape_flush_tape_buffers(drive);
977 rc = idetape_queue_rw_tail(drive, cmd, 0);
1007 static int idetape_rewind_tape(
ide_drive_t *drive)
1010 struct gendisk *disk = tape->
disk;
1016 idetape_create_rewind_cmd(drive, &pc);
1021 ret = ide_tape_read_position(drive);
1028 static int idetape_blkdev_ioctl(
ide_drive_t *drive,
unsigned int cmd,
1034 struct idetape_config {
1035 int dsc_rw_frequency;
1036 int dsc_media_access_frequency;
1061 static int idetape_space_over_filemarks(
ide_drive_t *drive,
short mt_op,
1065 struct gendisk *disk = tape->
disk;
1068 int sprev = !!(tape->
caps[4] & 0x20);
1078 mt_count = -mt_count;
1086 ide_tape_discard_merge_buffer(drive, 0);
1092 idetape_create_space_cmd(&pc, mt_count - count,
1099 retval = idetape_space_over_filemarks(drive,
MTFSF,
1103 count = (
MTBSFM == mt_op ? 1 : -1);
1104 return idetape_space_over_filemarks(drive,
MTFSF, count);
1127 static ssize_t idetape_chrdev_read(
struct file *
file,
char __user *buf,
1128 size_t count, loff_t *ppos)
1149 while (done < count) {
1165 todo =
min_t(
size_t, count - done, tape->
valid);
1175 idetape_space_over_filemarks(drive,
MTFSF, 1);
1179 return ret ? ret :
done;
1182 static ssize_t idetape_chrdev_write(
struct file *file,
const char __user *buf,
1183 size_t count, loff_t *ppos)
1202 while (done < count) {
1212 todo =
min_t(
size_t, count - done,
1222 return ret ? ret :
done;
1225 static int idetape_write_filemark(
ide_drive_t *drive)
1231 idetape_create_write_filemark_cmd(drive, &pc, 1);
1252 static int idetape_mtioctop(
ide_drive_t *drive,
short mt_op,
int mt_count)
1255 struct gendisk *disk = tape->
disk;
1269 return idetape_space_over_filemarks(drive, mt_op, mt_count);
1278 ide_tape_discard_merge_buffer(drive, 1);
1279 for (i = 0; i < mt_count; i++) {
1280 retval = idetape_write_filemark(drive);
1286 ide_tape_discard_merge_buffer(drive, 0);
1287 if (idetape_rewind_tape(drive))
1291 ide_tape_discard_merge_buffer(drive, 0);
1303 ide_tape_discard_merge_buffer(drive, 0);
1310 ide_tape_discard_merge_buffer(drive, 0);
1311 return idetape_flush_tape_buffers(drive);
1313 ide_tape_discard_merge_buffer(drive, 0);
1320 (
void)idetape_rewind_tape(drive);
1321 idetape_create_erase_cmd(&pc);
1336 ide_tape_discard_merge_buffer(drive, 0);
1337 return idetape_position_tape(drive,
1340 ide_tape_discard_merge_buffer(drive, 0);
1341 return idetape_position_tape(drive, 0, mt_count, 0);
1368 static long do_idetape_chrdev_ioctl(
struct file *file,
1369 unsigned int cmd,
unsigned long arg)
1382 ide_tape_flush_merge_buffer(drive);
1383 idetape_flush_tape_buffers(drive);
1386 block_offset = tape->
valid /
1388 position = ide_tape_read_position(drive);
1418 ide_tape_discard_merge_buffer(drive, 1);
1419 return idetape_blkdev_ioctl(drive, cmd, arg);
1423 static long idetape_chrdev_ioctl(
struct file *file,
1424 unsigned int cmd,
unsigned long arg)
1428 ret = do_idetape_chrdev_ioctl(file, cmd, arg);
1437 static void ide_tape_get_bsize_from_bdesc(
ide_drive_t *drive)
1448 "block size, assuming 32k\n");
1453 tape->
blk_size = (buf[4 + 5] << 16) +
1462 static int idetape_chrdev_open(
struct inode *
inode,
struct file *filp)
1464 unsigned int minor = iminor(inode), i = minor & ~0xc0;
1474 tape = ide_tape_get(
NULL,
true, i);
1480 drive = tape->
drive;
1498 retval = idetape_wait_ready(drive, 60 *
HZ);
1505 ide_tape_read_position(drive);
1507 (
void)idetape_rewind_tape(drive);
1510 ide_tape_get_bsize_from_bdesc(drive);
1547 static void idetape_write_release(
ide_drive_t *drive,
unsigned int minor)
1551 ide_tape_flush_merge_buffer(drive);
1554 idetape_pad_zeros(drive, tape->
blk_size *
1559 idetape_write_filemark(drive);
1560 idetape_flush_tape_buffers(drive);
1561 idetape_flush_tape_buffers(drive);
1564 static int idetape_chrdev_release(
struct inode *inode,
struct file *filp)
1568 unsigned int minor = iminor(inode);
1577 idetape_write_release(drive, minor);
1580 ide_tape_discard_merge_buffer(drive, 1);
1585 (
void) idetape_rewind_tape(drive);
1601 static void idetape_get_inquiry_results(
ide_drive_t *drive)
1608 idetape_create_inquiry_cmd(&pc);
1614 memcpy(vendor_id, &pc_buf[8], 8);
1615 memcpy(product_id, &pc_buf[16], 16);
1616 memcpy(fw_rev, &pc_buf[32], 4);
1623 drive->
name, tape->
name, vendor_id, product_id, fw_rev);
1630 static void idetape_get_mode_sense_results(
ide_drive_t *drive)
1640 " some default values\n");
1647 caps = buf + 4 + buf[3];
1653 *(
u16 *)&caps[8] = max_speed;
1655 *(
u16 *)&caps[14] = speed;
1660 "(assuming 650KB/sec)\n", drive->
name);
1661 *(
u16 *)&caps[14] = 650;
1665 "(assuming 650KB/sec)\n", drive->
name);
1666 *(
u16 *)&caps[8] = 650;
1672 if ((caps[6] & 1) == 0)
1677 else if (caps[7] & 0x04)
1681 #ifdef CONFIG_IDE_PROC_FS
1682 #define ide_tape_devset_get(name, field) \
1683 static int get_##name(ide_drive_t *drive) \
1685 idetape_tape_t *tape = drive->driver_data; \
1686 return tape->field; \
1689 #define ide_tape_devset_set(name, field) \
1690 static int set_##name(ide_drive_t *drive, int arg) \
1692 idetape_tape_t *tape = drive->driver_data; \
1693 tape->field = arg; \
1697 #define ide_tape_devset_rw_field(_name, _field) \
1698 ide_tape_devset_get(_name, _field) \
1699 ide_tape_devset_set(_name, _field) \
1700 IDE_DEVSET(_name, DS_SYNC, get_##_name, set_##_name)
1702 #define ide_tape_devset_r_field(_name, _field) \
1703 ide_tape_devset_get(_name, _field) \
1704 IDE_DEVSET(_name, 0, get_##_name, NULL)
1706 static int mulf_tdsc(
ide_drive_t *drive) {
return 1000; }
1708 static int divf_buffer(
ide_drive_t *drive) {
return 2; }
1709 static int divf_buffer_size(
ide_drive_t *drive) {
return 1024; }
1713 ide_tape_devset_rw_field(tdsc, best_dsc_rw_freq);
1715 ide_tape_devset_r_field(avg_speed, avg_speed);
1716 ide_tape_devset_r_field(speed, caps[14]);
1717 ide_tape_devset_r_field(
buffer, caps[16]);
1718 ide_tape_devset_r_field(buffer_size, buffer_size);
1720 static const struct ide_proc_devset idetape_settings[] = {
1721 __IDE_PROC_DEVSET(avg_speed, 0, 0xffff,
NULL,
NULL),
1722 __IDE_PROC_DEVSET(
buffer, 0, 0xffff,
NULL, divf_buffer),
1723 __IDE_PROC_DEVSET(buffer_size, 0, 0xffff,
NULL, divf_buffer_size),
1724 __IDE_PROC_DEVSET(dsc_overlap, 0, 1,
NULL,
NULL),
1725 __IDE_PROC_DEVSET(speed, 0, 0xffff,
NULL,
NULL),
1727 mulf_tdsc, divf_tdsc),
1766 tape->
minor = minor;
1767 tape->
name[0] =
'h';
1768 tape->
name[1] =
't';
1769 tape->
name[2] =
'0' + minor;
1772 idetape_get_inquiry_results(drive);
1773 idetape_get_mode_sense_results(drive);
1774 ide_tape_get_bsize_from_bdesc(drive);
1819 static void ide_tape_release(
struct device *
dev)
1823 struct gendisk *
g = tape->
disk;
1833 g->private_data =
NULL;
1838 #ifdef CONFIG_IDE_PROC_FS
1839 static int idetape_name_proc_show(
struct seq_file *
m,
void *
v)
1848 static int idetape_name_proc_open(
struct inode *inode,
struct file *file)
1855 .open = idetape_name_proc_open,
1861 static ide_proc_entry_t idetape_proc[] = {
1867 static ide_proc_entry_t *ide_tape_proc_entries(
ide_drive_t *drive)
1869 return idetape_proc;
1872 static const struct ide_proc_devset *ide_tape_proc_devsets(
ide_drive_t *drive)
1874 return idetape_settings;
1886 .probe = ide_tape_probe,
1887 .remove = ide_tape_remove,
1889 .do_request = idetape_do_request,
1890 #ifdef CONFIG_IDE_PROC_FS
1891 .proc_entries = ide_tape_proc_entries,
1892 .proc_devsets = ide_tape_proc_devsets,
1899 .read = idetape_chrdev_read,
1900 .write = idetape_chrdev_write,
1901 .unlocked_ioctl = idetape_chrdev_ioctl,
1902 .open = idetape_chrdev_open,
1903 .release = idetape_chrdev_release,
1912 tape = ide_tape_get(bdev->
bd_disk,
false, 0);
1921 static int idetape_release(
struct gendisk *disk,
fmode_t mode)
1933 unsigned int cmd,
unsigned long arg)
1942 err = idetape_blkdev_ioctl(drive, cmd, arg);
1948 static const struct block_device_operations idetape_block_ops = {
1950 .open = idetape_open,
1951 .release = idetape_release,
1952 .ioctl = idetape_ioctl,
1972 " the driver\n", drive->
name);
1989 tape->
dev.release = ide_tape_release;
1995 tape->
drive = drive;
1996 tape->
driver = &idetape_driver;
1999 g->private_data = &tape->
driver;
2004 for (minor = 0; idetape_devs[minor]; minor++)
2006 idetape_devs[minor] = tape;
2009 idetape_setup(drive, tape, minor);
2017 g->fops = &idetape_block_ops;
2030 static void __exit idetape_exit(
void)
2037 static int __init idetape_init(
void)
2041 if (IS_ERR(idetape_sysfs_class)) {
2042 idetape_sysfs_class =
NULL;
2052 goto out_free_class;
2057 goto out_free_driver;