Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
Borislav Petkov | 5ce78af | 2008-02-02 19:56:48 +0100 | [diff] [blame] | 2 | * IDE ATAPI streaming tape driver. |
| 3 | * |
Bartlomiej Zolnierkiewicz | 59bca8c | 2008-02-01 23:09:33 +0100 | [diff] [blame] | 4 | * Copyright (C) 1995-1999 Gadi Oxman <gadio@netvision.net.il> |
| 5 | * Copyright (C) 2003-2005 Bartlomiej Zolnierkiewicz |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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. |
| 10 | * |
| 11 | * It is hereby placed under the terms of the GNU general public license. |
| 12 | * (See linux/COPYING). |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 13 | * |
Borislav Petkov | 5ce78af | 2008-02-02 19:56:48 +0100 | [diff] [blame] | 14 | * For a historical changelog see |
| 15 | * Documentation/ide/ChangeLog.ide-tape.1995-2002 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 16 | */ |
| 17 | |
Bartlomiej Zolnierkiewicz | 51509ee | 2008-10-10 22:39:34 +0200 | [diff] [blame] | 18 | #define DRV_NAME "ide-tape" |
| 19 | |
Borislav Petkov | dfe7993 | 2008-02-06 02:57:55 +0100 | [diff] [blame] | 20 | #define IDETAPE_VERSION "1.20" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 21 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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> |
| 28 | #include <linux/mm.h> |
| 29 | #include <linux/interrupt.h> |
Marcelo Feitoza Parisi | 9bae1ff | 2006-03-28 01:56:46 -0800 | [diff] [blame] | 30 | #include <linux/jiffies.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 31 | #include <linux/major.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 32 | #include <linux/errno.h> |
| 33 | #include <linux/genhd.h> |
Alexey Dobriyan | 6d703a8 | 2009-09-01 17:52:57 -0700 | [diff] [blame] | 34 | #include <linux/seq_file.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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> |
Arjan van de Ven | cf8b897 | 2006-03-23 03:00:45 -0800 | [diff] [blame] | 41 | #include <linux/mutex.h> |
Borislav Petkov | 90699ce | 2008-02-02 19:56:50 +0100 | [diff] [blame] | 42 | #include <scsi/scsi.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 43 | |
| 44 | #include <asm/byteorder.h> |
Borislav Petkov | c837cfa | 2008-02-06 02:57:54 +0100 | [diff] [blame] | 45 | #include <linux/irq.h> |
| 46 | #include <linux/uaccess.h> |
| 47 | #include <linux/io.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 48 | #include <asm/unaligned.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 49 | #include <linux/mtio.h> |
| 50 | |
Borislav Petkov | 8004a8c | 2008-02-06 02:57:51 +0100 | [diff] [blame] | 51 | /* define to see debug info */ |
Borislav Petkov | e972d70 | 2009-07-17 23:55:16 +0000 | [diff] [blame] | 52 | #undef IDETAPE_DEBUG_LOG |
Borislav Petkov | 8004a8c | 2008-02-06 02:57:51 +0100 | [diff] [blame] | 53 | |
Borislav Petkov | e972d70 | 2009-07-17 23:55:16 +0000 | [diff] [blame] | 54 | #ifdef IDETAPE_DEBUG_LOG |
| 55 | #define ide_debug_log(lvl, fmt, args...) __ide_debug_log(lvl, fmt, ## args) |
Borislav Petkov | 8004a8c | 2008-02-06 02:57:51 +0100 | [diff] [blame] | 56 | #else |
Borislav Petkov | e972d70 | 2009-07-17 23:55:16 +0000 | [diff] [blame] | 57 | #define ide_debug_log(lvl, fmt, args...) do {} while (0) |
Borislav Petkov | 8004a8c | 2008-02-06 02:57:51 +0100 | [diff] [blame] | 58 | #endif |
| 59 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 60 | /**************************** Tunable parameters *****************************/ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 61 | /* |
Borislav Petkov | 3c98bf3 | 2008-02-06 02:57:53 +0100 | [diff] [blame] | 62 | * After each failed packet command we issue a request sense command and retry |
| 63 | * the packet command IDETAPE_MAX_PC_RETRIES times. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 64 | * |
Borislav Petkov | 3c98bf3 | 2008-02-06 02:57:53 +0100 | [diff] [blame] | 65 | * Setting IDETAPE_MAX_PC_RETRIES to 0 will disable retries. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 66 | */ |
| 67 | #define IDETAPE_MAX_PC_RETRIES 3 |
| 68 | |
| 69 | /* |
Borislav Petkov | 3c98bf3 | 2008-02-06 02:57:53 +0100 | [diff] [blame] | 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. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 74 | */ |
Borislav Petkov | 3c98bf3 | 2008-02-06 02:57:53 +0100 | [diff] [blame] | 75 | #define IDETAPE_FIFO_THRESHOLD 2 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 76 | |
| 77 | /* |
Borislav Petkov | 3c98bf3 | 2008-02-06 02:57:53 +0100 | [diff] [blame] | 78 | * DSC polling parameters. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 79 | * |
Borislav Petkov | 3c98bf3 | 2008-02-06 02:57:53 +0100 | [diff] [blame] | 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: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 82 | * |
Borislav Petkov | 3c98bf3 | 2008-02-06 02:57:53 +0100 | [diff] [blame] | 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. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 88 | * |
Borislav Petkov | 3c98bf3 | 2008-02-06 02:57:53 +0100 | [diff] [blame] | 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). |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 97 | * |
Borislav Petkov | 3c98bf3 | 2008-02-06 02:57:53 +0100 | [diff] [blame] | 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. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 100 | */ |
Borislav Petkov | 3c98bf3 | 2008-02-06 02:57:53 +0100 | [diff] [blame] | 101 | |
| 102 | /* DSC timings. */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 */ |
| 110 | |
| 111 | /*************************** End of tunable parameters ***********************/ |
| 112 | |
Borislav Petkov | 54abf37 | 2008-02-06 02:57:52 +0100 | [diff] [blame] | 113 | /* tape directions */ |
| 114 | enum { |
| 115 | IDETAPE_DIR_NONE = (1 << 0), |
| 116 | IDETAPE_DIR_READ = (1 << 1), |
| 117 | IDETAPE_DIR_WRITE = (1 << 2), |
| 118 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 119 | |
Borislav Petkov | 03056b9 | 2008-04-18 00:46:26 +0200 | [diff] [blame] | 120 | /* Tape door status */ |
| 121 | #define DOOR_UNLOCKED 0 |
| 122 | #define DOOR_LOCKED 1 |
| 123 | #define DOOR_EXPLICITLY_LOCKED 2 |
| 124 | |
| 125 | /* Some defines for the SPACE command */ |
| 126 | #define IDETAPE_SPACE_OVER_FILEMARK 1 |
| 127 | #define IDETAPE_SPACE_TO_EOD 3 |
| 128 | |
| 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 |
| 133 | |
Borislav Petkov | 03056b9 | 2008-04-18 00:46:26 +0200 | [diff] [blame] | 134 | /* Structures related to the SELECT SENSE / MODE SENSE packet commands. */ |
| 135 | #define IDETAPE_BLOCK_DESCRIPTOR 0 |
| 136 | #define IDETAPE_CAPABILITIES_PAGE 0x2a |
| 137 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 138 | /* |
Borislav Petkov | 3c98bf3 | 2008-02-06 02:57:53 +0100 | [diff] [blame] | 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. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 141 | */ |
| 142 | typedef struct ide_tape_obj { |
Bartlomiej Zolnierkiewicz | 7f3c868 | 2009-01-06 17:20:53 +0100 | [diff] [blame] | 143 | ide_drive_t *drive; |
| 144 | struct ide_driver *driver; |
| 145 | struct gendisk *disk; |
Bartlomiej Zolnierkiewicz | 8fed436 | 2009-02-25 20:28:24 +0100 | [diff] [blame] | 146 | struct device dev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 147 | |
Bartlomiej Zolnierkiewicz | 2e8a6f8 | 2008-10-10 22:39:36 +0200 | [diff] [blame] | 148 | /* used by REQ_IDETAPE_{READ,WRITE} requests */ |
| 149 | struct ide_atapi_pc queued_pc; |
Bartlomiej Zolnierkiewicz | 394a4c2 | 2008-10-10 22:39:35 +0200 | [diff] [blame] | 150 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 151 | /* |
Borislav Petkov | 3c98bf3 | 2008-02-06 02:57:53 +0100 | [diff] [blame] | 152 | * DSC polling variables. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 153 | * |
Borislav Petkov | 3c98bf3 | 2008-02-06 02:57:53 +0100 | [diff] [blame] | 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 |
Borislav Petkov | 5bd50dc | 2008-04-27 15:38:28 +0200 | [diff] [blame] | 157 | * data transfer) request in the device request queue. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 158 | */ |
Borislav Petkov | 6f3848a | 2009-07-21 23:08:23 -0700 | [diff] [blame] | 159 | bool postponed_rq; |
| 160 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 */ |
Borislav Petkov | 54bb207 | 2008-02-06 02:57:52 +0100 | [diff] [blame] | 166 | unsigned long best_dsc_rw_freq; |
| 167 | unsigned long dsc_poll_freq; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 168 | unsigned long dsc_timeout; |
| 169 | |
Borislav Petkov | 3c98bf3 | 2008-02-06 02:57:53 +0100 | [diff] [blame] | 170 | /* Read position information */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 171 | u8 partition; |
| 172 | /* Current block */ |
Borislav Petkov | 54bb207 | 2008-02-06 02:57:52 +0100 | [diff] [blame] | 173 | unsigned int first_frame; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 174 | |
Borislav Petkov | 3c98bf3 | 2008-02-06 02:57:53 +0100 | [diff] [blame] | 175 | /* Last error information */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 176 | u8 sense_key, asc, ascq; |
| 177 | |
Borislav Petkov | 3c98bf3 | 2008-02-06 02:57:53 +0100 | [diff] [blame] | 178 | /* Character device operation */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 179 | unsigned int minor; |
| 180 | /* device name */ |
| 181 | char name[4]; |
| 182 | /* Current character device data transfer direction */ |
Borislav Petkov | 54abf37 | 2008-02-06 02:57:52 +0100 | [diff] [blame] | 183 | u8 chrdev_dir; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 184 | |
Borislav Petkov | 54bb207 | 2008-02-06 02:57:52 +0100 | [diff] [blame] | 185 | /* tape block size, usually 512 or 1024 bytes */ |
| 186 | unsigned short blk_size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 187 | int user_bs_factor; |
Borislav Petkov | b642201 | 2008-02-02 19:56:49 +0100 | [diff] [blame] | 188 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 189 | /* Copy of the tape's Capabilities and Mechanical Page */ |
Borislav Petkov | b642201 | 2008-02-02 19:56:49 +0100 | [diff] [blame] | 190 | u8 caps[20]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 191 | |
| 192 | /* |
Borislav Petkov | 3c98bf3 | 2008-02-06 02:57:53 +0100 | [diff] [blame] | 193 | * Active data transfer request parameters. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 194 | * |
Borislav Petkov | 3c98bf3 | 2008-02-06 02:57:53 +0100 | [diff] [blame] | 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. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 198 | */ |
Borislav Petkov | 83042b2 | 2008-04-27 15:38:27 +0200 | [diff] [blame] | 199 | |
Borislav Petkov | 3c98bf3 | 2008-02-06 02:57:53 +0100 | [diff] [blame] | 200 | /* Data buffer size chosen based on the tape's recommendation */ |
Borislav Petkov | f73850a | 2008-04-27 15:38:33 +0200 | [diff] [blame] | 201 | int buffer_size; |
Tejun Heo | 963da55 | 2009-04-19 08:46:02 +0900 | [diff] [blame] | 202 | /* Staging buffer of buffer_size bytes */ |
| 203 | void *buf; |
| 204 | /* The read/write cursor */ |
| 205 | void *cur; |
| 206 | /* The number of valid bytes in buf */ |
| 207 | size_t valid; |
Borislav Petkov | 3c98bf3 | 2008-02-06 02:57:53 +0100 | [diff] [blame] | 208 | |
Borislav Petkov | 3c98bf3 | 2008-02-06 02:57:53 +0100 | [diff] [blame] | 209 | /* Measures average tape speed */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 210 | unsigned long avg_time; |
| 211 | int avg_size; |
| 212 | int avg_speed; |
| 213 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 214 | /* the door is currently locked */ |
| 215 | int door_locked; |
| 216 | /* the tape hardware is write protected */ |
| 217 | char drv_write_prot; |
| 218 | /* the tape is write protected (hardware or opened as read-only) */ |
| 219 | char write_prot; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 220 | } idetape_tape_t; |
| 221 | |
Arjan van de Ven | cf8b897 | 2006-03-23 03:00:45 -0800 | [diff] [blame] | 222 | static DEFINE_MUTEX(idetape_ref_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 223 | |
Borislav Petkov | cbba2fa | 2009-10-11 00:25:19 +0000 | [diff] [blame] | 224 | static DEFINE_MUTEX(idetape_chrdev_mutex); |
| 225 | |
Will Dyson | d5dee80 | 2005-09-16 02:55:07 -0700 | [diff] [blame] | 226 | static struct class *idetape_sysfs_class; |
| 227 | |
Bartlomiej Zolnierkiewicz | 8fed436 | 2009-02-25 20:28:24 +0100 | [diff] [blame] | 228 | static void ide_tape_release(struct device *); |
Bartlomiej Zolnierkiewicz | 08da591 | 2008-07-24 22:53:15 +0200 | [diff] [blame] | 229 | |
Borislav Petkov | 9d01e4c | 2009-06-08 22:03:03 +0200 | [diff] [blame] | 230 | static struct ide_tape_obj *idetape_devs[MAX_HWIFS * MAX_DRIVES]; |
| 231 | |
| 232 | static struct ide_tape_obj *ide_tape_get(struct gendisk *disk, bool cdev, |
| 233 | unsigned int i) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 234 | { |
| 235 | struct ide_tape_obj *tape = NULL; |
| 236 | |
Arjan van de Ven | cf8b897 | 2006-03-23 03:00:45 -0800 | [diff] [blame] | 237 | mutex_lock(&idetape_ref_mutex); |
Borislav Petkov | 9d01e4c | 2009-06-08 22:03:03 +0200 | [diff] [blame] | 238 | |
| 239 | if (cdev) |
| 240 | tape = idetape_devs[i]; |
| 241 | else |
| 242 | tape = ide_drv_g(disk, ide_tape_obj); |
| 243 | |
Bartlomiej Zolnierkiewicz | 08da591 | 2008-07-24 22:53:15 +0200 | [diff] [blame] | 244 | if (tape) { |
Bartlomiej Zolnierkiewicz | d3e33ff | 2008-08-05 18:16:59 +0200 | [diff] [blame] | 245 | if (ide_device_get(tape->drive)) |
Bartlomiej Zolnierkiewicz | 08da591 | 2008-07-24 22:53:15 +0200 | [diff] [blame] | 246 | tape = NULL; |
Bartlomiej Zolnierkiewicz | d3e33ff | 2008-08-05 18:16:59 +0200 | [diff] [blame] | 247 | else |
Bartlomiej Zolnierkiewicz | 8fed436 | 2009-02-25 20:28:24 +0100 | [diff] [blame] | 248 | get_device(&tape->dev); |
Bartlomiej Zolnierkiewicz | 08da591 | 2008-07-24 22:53:15 +0200 | [diff] [blame] | 249 | } |
Borislav Petkov | 9d01e4c | 2009-06-08 22:03:03 +0200 | [diff] [blame] | 250 | |
Arjan van de Ven | cf8b897 | 2006-03-23 03:00:45 -0800 | [diff] [blame] | 251 | mutex_unlock(&idetape_ref_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 252 | return tape; |
| 253 | } |
| 254 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 255 | static void ide_tape_put(struct ide_tape_obj *tape) |
| 256 | { |
Bartlomiej Zolnierkiewicz | d3e33ff | 2008-08-05 18:16:59 +0200 | [diff] [blame] | 257 | ide_drive_t *drive = tape->drive; |
| 258 | |
Arjan van de Ven | cf8b897 | 2006-03-23 03:00:45 -0800 | [diff] [blame] | 259 | mutex_lock(&idetape_ref_mutex); |
Bartlomiej Zolnierkiewicz | 8fed436 | 2009-02-25 20:28:24 +0100 | [diff] [blame] | 260 | put_device(&tape->dev); |
Bartlomiej Zolnierkiewicz | d3e33ff | 2008-08-05 18:16:59 +0200 | [diff] [blame] | 261 | ide_device_put(drive); |
Arjan van de Ven | cf8b897 | 2006-03-23 03:00:45 -0800 | [diff] [blame] | 262 | mutex_unlock(&idetape_ref_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 263 | } |
| 264 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 265 | /* |
Borislav Petkov | 1b5db43 | 2008-02-02 19:56:48 +0100 | [diff] [blame] | 266 | * called on each failed packet command retry to analyze the request sense. We |
| 267 | * currently do not utilize this information. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 268 | */ |
Borislav Petkov | ae3a838 | 2009-05-02 10:58:17 +0200 | [diff] [blame] | 269 | static void idetape_analyze_error(ide_drive_t *drive) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 270 | { |
| 271 | idetape_tape_t *tape = drive->driver_data; |
Bartlomiej Zolnierkiewicz | 5e2040fd | 2009-03-27 12:46:34 +0100 | [diff] [blame] | 272 | struct ide_atapi_pc *pc = drive->failed_pc; |
Borislav Petkov | dfb7e62 | 2009-05-01 20:35:21 +0200 | [diff] [blame] | 273 | struct request *rq = drive->hwif->rq; |
Borislav Petkov | ae3a838 | 2009-05-02 10:58:17 +0200 | [diff] [blame] | 274 | u8 *sense = bio_data(rq->bio); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 275 | |
Borislav Petkov | 1b5db43 | 2008-02-02 19:56:48 +0100 | [diff] [blame] | 276 | tape->sense_key = sense[2] & 0xF; |
| 277 | tape->asc = sense[12]; |
| 278 | tape->ascq = sense[13]; |
Borislav Petkov | 8004a8c | 2008-02-06 02:57:51 +0100 | [diff] [blame] | 279 | |
Borislav Petkov | e972d70 | 2009-07-17 23:55:16 +0000 | [diff] [blame] | 280 | ide_debug_log(IDE_DBG_FUNC, |
| 281 | "cmd: 0x%x, sense key = %x, asc = %x, ascq = %x", |
| 282 | rq->cmd[0], tape->sense_key, tape->asc, tape->ascq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 283 | |
Borislav Petkov | 077e6db | 2009-05-01 21:21:02 +0200 | [diff] [blame] | 284 | /* correct remaining bytes to transfer */ |
Tejun Heo | 21d9c5d | 2009-04-19 08:46:02 +0900 | [diff] [blame] | 285 | if (pc->flags & PC_FLAG_DMA_ERROR) |
Borislav Petkov | 077e6db | 2009-05-01 21:21:02 +0200 | [diff] [blame] | 286 | rq->resid_len = tape->blk_size * get_unaligned_be32(&sense[3]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 287 | |
| 288 | /* |
| 289 | * If error was the result of a zero-length read or write command, |
| 290 | * with sense key=5, asc=0x22, ascq=0, let it slide. Some drives |
| 291 | * (i.e. Seagate STT3401A Travan) don't support 0-length read/writes. |
| 292 | */ |
Borislav Petkov | 90699ce | 2008-02-02 19:56:50 +0100 | [diff] [blame] | 293 | if ((pc->c[0] == READ_6 || pc->c[0] == WRITE_6) |
Borislav Petkov | 1b5db43 | 2008-02-02 19:56:48 +0100 | [diff] [blame] | 294 | /* length == 0 */ |
| 295 | && pc->c[4] == 0 && pc->c[3] == 0 && pc->c[2] == 0) { |
| 296 | if (tape->sense_key == 5) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 297 | /* don't report an error, everything's ok */ |
| 298 | pc->error = 0; |
| 299 | /* don't retry read/write */ |
Borislav Petkov | 346331f | 2008-04-18 00:46:26 +0200 | [diff] [blame] | 300 | pc->flags |= PC_FLAG_ABORT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 301 | } |
| 302 | } |
Borislav Petkov | 90699ce | 2008-02-02 19:56:50 +0100 | [diff] [blame] | 303 | if (pc->c[0] == READ_6 && (sense[2] & 0x80)) { |
Bartlomiej Zolnierkiewicz | c152cc1 | 2009-03-27 12:46:34 +0100 | [diff] [blame] | 304 | pc->error = IDE_DRV_ERROR_FILEMARK; |
Borislav Petkov | 346331f | 2008-04-18 00:46:26 +0200 | [diff] [blame] | 305 | pc->flags |= PC_FLAG_ABORT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 306 | } |
Borislav Petkov | 90699ce | 2008-02-02 19:56:50 +0100 | [diff] [blame] | 307 | if (pc->c[0] == WRITE_6) { |
Borislav Petkov | 1b5db43 | 2008-02-02 19:56:48 +0100 | [diff] [blame] | 308 | if ((sense[2] & 0x40) || (tape->sense_key == 0xd |
| 309 | && tape->asc == 0x0 && tape->ascq == 0x2)) { |
Bartlomiej Zolnierkiewicz | c152cc1 | 2009-03-27 12:46:34 +0100 | [diff] [blame] | 310 | pc->error = IDE_DRV_ERROR_EOD; |
Borislav Petkov | 346331f | 2008-04-18 00:46:26 +0200 | [diff] [blame] | 311 | pc->flags |= PC_FLAG_ABORT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 312 | } |
| 313 | } |
Borislav Petkov | 90699ce | 2008-02-02 19:56:50 +0100 | [diff] [blame] | 314 | if (pc->c[0] == READ_6 || pc->c[0] == WRITE_6) { |
Borislav Petkov | 1b5db43 | 2008-02-02 19:56:48 +0100 | [diff] [blame] | 315 | if (tape->sense_key == 8) { |
Bartlomiej Zolnierkiewicz | c152cc1 | 2009-03-27 12:46:34 +0100 | [diff] [blame] | 316 | pc->error = IDE_DRV_ERROR_EOD; |
Borislav Petkov | 346331f | 2008-04-18 00:46:26 +0200 | [diff] [blame] | 317 | pc->flags |= PC_FLAG_ABORT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 318 | } |
Borislav Petkov | 346331f | 2008-04-18 00:46:26 +0200 | [diff] [blame] | 319 | if (!(pc->flags & PC_FLAG_ABORT) && |
Borislav Petkov | 077e6db | 2009-05-01 21:21:02 +0200 | [diff] [blame] | 320 | (blk_rq_bytes(rq) - rq->resid_len)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 321 | pc->retries = IDETAPE_MAX_PC_RETRIES + 1; |
| 322 | } |
| 323 | } |
| 324 | |
Bartlomiej Zolnierkiewicz | b14c721 | 2008-10-13 21:39:30 +0200 | [diff] [blame] | 325 | static void ide_tape_handle_dsc(ide_drive_t *); |
| 326 | |
Bartlomiej Zolnierkiewicz | 03a2faa | 2009-03-27 12:46:36 +0100 | [diff] [blame] | 327 | static int ide_tape_callback(ide_drive_t *drive, int dsc) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 328 | { |
| 329 | idetape_tape_t *tape = drive->driver_data; |
Bartlomiej Zolnierkiewicz | 2b9efba | 2008-10-13 21:39:31 +0200 | [diff] [blame] | 330 | struct ide_atapi_pc *pc = drive->pc; |
Bartlomiej Zolnierkiewicz | 313afea | 2009-03-27 12:46:34 +0100 | [diff] [blame] | 331 | struct request *rq = drive->hwif->rq; |
Bartlomiej Zolnierkiewicz | 5985e6a | 2008-07-15 21:21:55 +0200 | [diff] [blame] | 332 | int uptodate = pc->error ? 0 : 1; |
Bartlomiej Zolnierkiewicz | 313afea | 2009-03-27 12:46:34 +0100 | [diff] [blame] | 333 | int err = uptodate ? 0 : IDE_DRV_ERROR_GENERAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 334 | |
Borislav Petkov | e972d70 | 2009-07-17 23:55:16 +0000 | [diff] [blame] | 335 | ide_debug_log(IDE_DBG_FUNC, "cmd: 0x%x, dsc: %d, err: %d", rq->cmd[0], |
| 336 | dsc, err); |
Borislav Petkov | 8004a8c | 2008-02-06 02:57:51 +0100 | [diff] [blame] | 337 | |
Bartlomiej Zolnierkiewicz | b14c721 | 2008-10-13 21:39:30 +0200 | [diff] [blame] | 338 | if (dsc) |
| 339 | ide_tape_handle_dsc(drive); |
| 340 | |
Bartlomiej Zolnierkiewicz | 5e2040fd | 2009-03-27 12:46:34 +0100 | [diff] [blame] | 341 | if (drive->failed_pc == pc) |
| 342 | drive->failed_pc = NULL; |
Bartlomiej Zolnierkiewicz | dd2e9a0 | 2008-07-15 21:22:01 +0200 | [diff] [blame] | 343 | |
Bartlomiej Zolnierkiewicz | 5985e6a | 2008-07-15 21:21:55 +0200 | [diff] [blame] | 344 | if (pc->c[0] == REQUEST_SENSE) { |
| 345 | if (uptodate) |
Borislav Petkov | ae3a838 | 2009-05-02 10:58:17 +0200 | [diff] [blame] | 346 | idetape_analyze_error(drive); |
Bartlomiej Zolnierkiewicz | 5985e6a | 2008-07-15 21:21:55 +0200 | [diff] [blame] | 347 | else |
| 348 | printk(KERN_ERR "ide-tape: Error in REQUEST SENSE " |
| 349 | "itself - Aborting request!\n"); |
| 350 | } else if (pc->c[0] == READ_6 || pc->c[0] == WRITE_6) { |
Borislav Petkov | 077e6db | 2009-05-01 21:21:02 +0200 | [diff] [blame] | 351 | unsigned int blocks = |
| 352 | (blk_rq_bytes(rq) - rq->resid_len) / tape->blk_size; |
Bartlomiej Zolnierkiewicz | 5985e6a | 2008-07-15 21:21:55 +0200 | [diff] [blame] | 353 | |
| 354 | tape->avg_size += blocks * tape->blk_size; |
| 355 | |
| 356 | if (time_after_eq(jiffies, tape->avg_time + HZ)) { |
| 357 | tape->avg_speed = tape->avg_size * HZ / |
| 358 | (jiffies - tape->avg_time) / 1024; |
| 359 | tape->avg_size = 0; |
| 360 | tape->avg_time = jiffies; |
| 361 | } |
| 362 | |
| 363 | tape->first_frame += blocks; |
Bartlomiej Zolnierkiewicz | 5985e6a | 2008-07-15 21:21:55 +0200 | [diff] [blame] | 364 | |
Bartlomiej Zolnierkiewicz | 313afea | 2009-03-27 12:46:34 +0100 | [diff] [blame] | 365 | if (pc->error) { |
| 366 | uptodate = 0; |
| 367 | err = pc->error; |
| 368 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 369 | } |
Bartlomiej Zolnierkiewicz | 313afea | 2009-03-27 12:46:34 +0100 | [diff] [blame] | 370 | rq->errors = err; |
| 371 | |
Bartlomiej Zolnierkiewicz | 03a2faa | 2009-03-27 12:46:36 +0100 | [diff] [blame] | 372 | return uptodate; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 373 | } |
| 374 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 375 | /* |
Borislav Petkov | 3c98bf3 | 2008-02-06 02:57:53 +0100 | [diff] [blame] | 376 | * Postpone the current request so that ide.c will be able to service requests |
Bartlomiej Zolnierkiewicz | b65fac3 | 2009-01-06 17:20:50 +0100 | [diff] [blame] | 377 | * from another device on the same port while we are polling for DSC. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 378 | */ |
Borislav Petkov | 6f3848a | 2009-07-21 23:08:23 -0700 | [diff] [blame] | 379 | static void ide_tape_stall_queue(ide_drive_t *drive) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 380 | { |
| 381 | idetape_tape_t *tape = drive->driver_data; |
| 382 | |
Borislav Petkov | e972d70 | 2009-07-17 23:55:16 +0000 | [diff] [blame] | 383 | ide_debug_log(IDE_DBG_FUNC, "cmd: 0x%x, dsc_poll_freq: %lu", |
Borislav Petkov | 6f3848a | 2009-07-21 23:08:23 -0700 | [diff] [blame] | 384 | drive->hwif->rq->cmd[0], tape->dsc_poll_freq); |
Borislav Petkov | 8004a8c | 2008-02-06 02:57:51 +0100 | [diff] [blame] | 385 | |
Borislav Petkov | 6f3848a | 2009-07-21 23:08:23 -0700 | [diff] [blame] | 386 | tape->postponed_rq = true; |
Bartlomiej Zolnierkiewicz | b65fac3 | 2009-01-06 17:20:50 +0100 | [diff] [blame] | 387 | |
Borislav Petkov | 54bb207 | 2008-02-06 02:57:52 +0100 | [diff] [blame] | 388 | ide_stall_queue(drive, tape->dsc_poll_freq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 389 | } |
| 390 | |
Bartlomiej Zolnierkiewicz | 74e63e7 | 2008-07-15 21:22:01 +0200 | [diff] [blame] | 391 | static void ide_tape_handle_dsc(ide_drive_t *drive) |
| 392 | { |
| 393 | idetape_tape_t *tape = drive->driver_data; |
| 394 | |
| 395 | /* Media access command */ |
| 396 | tape->dsc_polling_start = jiffies; |
| 397 | tape->dsc_poll_freq = IDETAPE_DSC_MA_FAST; |
| 398 | tape->dsc_timeout = jiffies + IDETAPE_DSC_MA_TIMEOUT; |
| 399 | /* Allow ide.c to handle other requests */ |
Borislav Petkov | 6f3848a | 2009-07-21 23:08:23 -0700 | [diff] [blame] | 400 | ide_tape_stall_queue(drive); |
Bartlomiej Zolnierkiewicz | 74e63e7 | 2008-07-15 21:22:01 +0200 | [diff] [blame] | 401 | } |
| 402 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 403 | /* |
Borislav Petkov | 3c98bf3 | 2008-02-06 02:57:53 +0100 | [diff] [blame] | 404 | * Packet Command Interface |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 405 | * |
Bartlomiej Zolnierkiewicz | 2b9efba | 2008-10-13 21:39:31 +0200 | [diff] [blame] | 406 | * The current Packet Command is available in drive->pc, and will not change |
Borislav Petkov | 3c98bf3 | 2008-02-06 02:57:53 +0100 | [diff] [blame] | 407 | * until we finish handling it. Each packet command is associated with a |
| 408 | * callback function that will be called when the command is finished. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 409 | * |
Borislav Petkov | 3c98bf3 | 2008-02-06 02:57:53 +0100 | [diff] [blame] | 410 | * The handling will be done in three stages: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 411 | * |
Bartlomiej Zolnierkiewicz | b788ee9 | 2009-03-27 12:46:46 +0100 | [diff] [blame] | 412 | * 1. ide_tape_issue_pc will send the packet command to the drive, and will set |
Bartlomiej Zolnierkiewicz | aa5d2de | 2008-10-13 21:39:32 +0200 | [diff] [blame] | 413 | * the interrupt handler to ide_pc_intr. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 414 | * |
Bartlomiej Zolnierkiewicz | aa5d2de | 2008-10-13 21:39:32 +0200 | [diff] [blame] | 415 | * 2. On each interrupt, ide_pc_intr will be called. This step will be |
Borislav Petkov | 3c98bf3 | 2008-02-06 02:57:53 +0100 | [diff] [blame] | 416 | * repeated until the device signals us that no more interrupts will be issued. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 417 | * |
Borislav Petkov | 3c98bf3 | 2008-02-06 02:57:53 +0100 | [diff] [blame] | 418 | * 3. ATAPI Tape media access commands have immediate status with a delayed |
| 419 | * process. In case of a successful initiation of a media access packet command, |
| 420 | * the DSC bit will be set when the actual execution of the command is finished. |
| 421 | * Since the tape drive will not issue an interrupt, we have to poll for this |
| 422 | * event. In this case, we define the request as "low priority request" by |
| 423 | * setting rq_status to IDETAPE_RQ_POSTPONED, set a timer to poll for DSC and |
| 424 | * exit the driver. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 425 | * |
Borislav Petkov | 3c98bf3 | 2008-02-06 02:57:53 +0100 | [diff] [blame] | 426 | * ide.c will then give higher priority to requests which originate from the |
| 427 | * other device, until will change rq_status to RQ_ACTIVE. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 428 | * |
Borislav Petkov | 3c98bf3 | 2008-02-06 02:57:53 +0100 | [diff] [blame] | 429 | * 4. When the packet command is finished, it will be checked for errors. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 430 | * |
Borislav Petkov | 3c98bf3 | 2008-02-06 02:57:53 +0100 | [diff] [blame] | 431 | * 5. In case an error was found, we queue a request sense packet command in |
| 432 | * front of the request queue and retry the operation up to |
| 433 | * IDETAPE_MAX_PC_RETRIES times. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 434 | * |
Borislav Petkov | 3c98bf3 | 2008-02-06 02:57:53 +0100 | [diff] [blame] | 435 | * 6. In case no error was found, or we decided to give up and not to retry |
| 436 | * again, the callback function will be called and then we will handle the next |
| 437 | * request. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 438 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 439 | |
Bartlomiej Zolnierkiewicz | b788ee9 | 2009-03-27 12:46:46 +0100 | [diff] [blame] | 440 | static ide_startstop_t ide_tape_issue_pc(ide_drive_t *drive, |
| 441 | struct ide_cmd *cmd, |
| 442 | struct ide_atapi_pc *pc) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 443 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 444 | idetape_tape_t *tape = drive->driver_data; |
Borislav Petkov | dfb7e62 | 2009-05-01 20:35:21 +0200 | [diff] [blame] | 445 | struct request *rq = drive->hwif->rq; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 446 | |
Bartlomiej Zolnierkiewicz | 5e2040fd | 2009-03-27 12:46:34 +0100 | [diff] [blame] | 447 | if (drive->failed_pc == NULL && pc->c[0] != REQUEST_SENSE) |
| 448 | drive->failed_pc = pc; |
Bartlomiej Zolnierkiewicz | 2b9efba | 2008-10-13 21:39:31 +0200 | [diff] [blame] | 449 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 450 | /* Set the current packet command */ |
Bartlomiej Zolnierkiewicz | 2b9efba | 2008-10-13 21:39:31 +0200 | [diff] [blame] | 451 | drive->pc = pc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 452 | |
| 453 | if (pc->retries > IDETAPE_MAX_PC_RETRIES || |
Borislav Petkov | 346331f | 2008-04-18 00:46:26 +0200 | [diff] [blame] | 454 | (pc->flags & PC_FLAG_ABORT)) { |
Tejun Heo | b3071d1 | 2009-04-19 08:46:02 +0900 | [diff] [blame] | 455 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 456 | /* |
Borislav Petkov | 3c98bf3 | 2008-02-06 02:57:53 +0100 | [diff] [blame] | 457 | * We will "abort" retrying a packet command in case legitimate |
| 458 | * error code was received (crossing a filemark, or end of the |
| 459 | * media, for example). |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 460 | */ |
Borislav Petkov | 346331f | 2008-04-18 00:46:26 +0200 | [diff] [blame] | 461 | if (!(pc->flags & PC_FLAG_ABORT)) { |
Borislav Petkov | 90699ce | 2008-02-02 19:56:50 +0100 | [diff] [blame] | 462 | if (!(pc->c[0] == TEST_UNIT_READY && |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 463 | tape->sense_key == 2 && tape->asc == 4 && |
| 464 | (tape->ascq == 1 || tape->ascq == 8))) { |
| 465 | printk(KERN_ERR "ide-tape: %s: I/O error, " |
| 466 | "pc = %2x, key = %2x, " |
| 467 | "asc = %2x, ascq = %2x\n", |
| 468 | tape->name, pc->c[0], |
| 469 | tape->sense_key, tape->asc, |
| 470 | tape->ascq); |
| 471 | } |
| 472 | /* Giving up */ |
Bartlomiej Zolnierkiewicz | c152cc1 | 2009-03-27 12:46:34 +0100 | [diff] [blame] | 473 | pc->error = IDE_DRV_ERROR_GENERAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 474 | } |
Tejun Heo | b3071d1 | 2009-04-19 08:46:02 +0900 | [diff] [blame] | 475 | |
Bartlomiej Zolnierkiewicz | 5e2040fd | 2009-03-27 12:46:34 +0100 | [diff] [blame] | 476 | drive->failed_pc = NULL; |
Bartlomiej Zolnierkiewicz | b14c721 | 2008-10-13 21:39:30 +0200 | [diff] [blame] | 477 | drive->pc_callback(drive, 0); |
Borislav Petkov | dfb7e62 | 2009-05-01 20:35:21 +0200 | [diff] [blame] | 478 | ide_complete_rq(drive, -EIO, blk_rq_bytes(rq)); |
Bartlomiej Zolnierkiewicz | 92f5daf | 2008-07-15 21:21:55 +0200 | [diff] [blame] | 479 | return ide_stopped; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 480 | } |
Borislav Petkov | e972d70 | 2009-07-17 23:55:16 +0000 | [diff] [blame] | 481 | ide_debug_log(IDE_DBG_SENSE, "retry #%d, cmd: 0x%02x", pc->retries, |
| 482 | pc->c[0]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 483 | |
| 484 | pc->retries++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 485 | |
Bartlomiej Zolnierkiewicz | b788ee9 | 2009-03-27 12:46:46 +0100 | [diff] [blame] | 486 | return ide_issue_pc(drive, cmd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 487 | } |
| 488 | |
Borislav Petkov | 3c98bf3 | 2008-02-06 02:57:53 +0100 | [diff] [blame] | 489 | /* A mode sense command is used to "sense" tape parameters. */ |
Borislav Petkov | d236d74 | 2008-04-18 00:46:27 +0200 | [diff] [blame] | 490 | static void idetape_create_mode_sense_cmd(struct ide_atapi_pc *pc, u8 page_code) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 491 | { |
Bartlomiej Zolnierkiewicz | 7bf7420 | 2008-10-10 22:39:37 +0200 | [diff] [blame] | 492 | ide_init_pc(pc); |
Borislav Petkov | 90699ce | 2008-02-02 19:56:50 +0100 | [diff] [blame] | 493 | pc->c[0] = MODE_SENSE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 494 | if (page_code != IDETAPE_BLOCK_DESCRIPTOR) |
Borislav Petkov | 3c98bf3 | 2008-02-06 02:57:53 +0100 | [diff] [blame] | 495 | /* DBD = 1 - Don't return block descriptors */ |
| 496 | pc->c[1] = 8; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 497 | pc->c[2] = page_code; |
| 498 | /* |
| 499 | * Changed pc->c[3] to 0 (255 will at best return unused info). |
| 500 | * |
| 501 | * For SCSI this byte is defined as subpage instead of high byte |
| 502 | * of length and some IDE drives seem to interpret it this way |
| 503 | * and return an error when 255 is used. |
| 504 | */ |
| 505 | pc->c[3] = 0; |
Borislav Petkov | 3c98bf3 | 2008-02-06 02:57:53 +0100 | [diff] [blame] | 506 | /* We will just discard data in that case */ |
| 507 | pc->c[4] = 255; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 508 | if (page_code == IDETAPE_BLOCK_DESCRIPTOR) |
Borislav Petkov | d236d74 | 2008-04-18 00:46:27 +0200 | [diff] [blame] | 509 | pc->req_xfer = 12; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 510 | else if (page_code == IDETAPE_CAPABILITIES_PAGE) |
Borislav Petkov | d236d74 | 2008-04-18 00:46:27 +0200 | [diff] [blame] | 511 | pc->req_xfer = 24; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 512 | else |
Borislav Petkov | d236d74 | 2008-04-18 00:46:27 +0200 | [diff] [blame] | 513 | pc->req_xfer = 50; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 514 | } |
| 515 | |
Borislav Petkov | 5a04cfa | 2008-02-06 02:57:54 +0100 | [diff] [blame] | 516 | static ide_startstop_t idetape_media_access_finished(ide_drive_t *drive) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 517 | { |
Bartlomiej Zolnierkiewicz | b73c7ee | 2008-07-23 19:55:52 +0200 | [diff] [blame] | 518 | ide_hwif_t *hwif = drive->hwif; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 519 | idetape_tape_t *tape = drive->driver_data; |
Bartlomiej Zolnierkiewicz | 2b9efba | 2008-10-13 21:39:31 +0200 | [diff] [blame] | 520 | struct ide_atapi_pc *pc = drive->pc; |
Bartlomiej Zolnierkiewicz | 22c525b | 2008-01-25 22:17:11 +0100 | [diff] [blame] | 521 | u8 stat; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 522 | |
Bartlomiej Zolnierkiewicz | 374e042 | 2008-07-23 19:55:56 +0200 | [diff] [blame] | 523 | stat = hwif->tp_ops->read_status(hwif); |
Bartlomiej Zolnierkiewicz | c47137a | 2008-02-06 02:57:51 +0100 | [diff] [blame] | 524 | |
Bartlomiej Zolnierkiewicz | 3a7d248 | 2008-10-10 22:39:21 +0200 | [diff] [blame] | 525 | if (stat & ATA_DSC) { |
| 526 | if (stat & ATA_ERR) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 527 | /* Error detected */ |
Borislav Petkov | 90699ce | 2008-02-02 19:56:50 +0100 | [diff] [blame] | 528 | if (pc->c[0] != TEST_UNIT_READY) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 529 | printk(KERN_ERR "ide-tape: %s: I/O error, ", |
| 530 | tape->name); |
| 531 | /* Retry operation */ |
Borislav Petkov | 6b544fc | 2009-04-19 07:00:42 +0900 | [diff] [blame] | 532 | ide_retry_pc(drive); |
Bartlomiej Zolnierkiewicz | 258ec41 | 2008-07-15 21:21:55 +0200 | [diff] [blame] | 533 | return ide_stopped; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 534 | } |
| 535 | pc->error = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 536 | } else { |
Bartlomiej Zolnierkiewicz | c152cc1 | 2009-03-27 12:46:34 +0100 | [diff] [blame] | 537 | pc->error = IDE_DRV_ERROR_GENERAL; |
Bartlomiej Zolnierkiewicz | 5e2040fd | 2009-03-27 12:46:34 +0100 | [diff] [blame] | 538 | drive->failed_pc = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 539 | } |
Bartlomiej Zolnierkiewicz | b14c721 | 2008-10-13 21:39:30 +0200 | [diff] [blame] | 540 | drive->pc_callback(drive, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 541 | return ide_stopped; |
| 542 | } |
| 543 | |
Borislav Petkov | cd2abbf | 2008-07-15 21:22:03 +0200 | [diff] [blame] | 544 | static void ide_tape_create_rw_cmd(idetape_tape_t *tape, |
Borislav Petkov | 0014c75 | 2008-07-23 19:56:00 +0200 | [diff] [blame] | 545 | struct ide_atapi_pc *pc, struct request *rq, |
| 546 | u8 opcode) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 547 | { |
Borislav Petkov | 10c0b34 | 2009-05-01 20:29:53 +0200 | [diff] [blame] | 548 | unsigned int length = blk_rq_sectors(rq) / (tape->blk_size >> 9); |
Borislav Petkov | 0014c75 | 2008-07-23 19:56:00 +0200 | [diff] [blame] | 549 | |
Bartlomiej Zolnierkiewicz | 7bf7420 | 2008-10-10 22:39:37 +0200 | [diff] [blame] | 550 | ide_init_pc(pc); |
Borislav Petkov | 860ff5e | 2008-02-02 19:56:50 +0100 | [diff] [blame] | 551 | put_unaligned(cpu_to_be32(length), (unsigned int *) &pc->c[1]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 552 | pc->c[1] = 1; |
Borislav Petkov | 19f52a7 | 2009-05-04 09:53:03 +0200 | [diff] [blame] | 553 | |
| 554 | if (blk_rq_bytes(rq) == tape->buffer_size) |
Bartlomiej Zolnierkiewicz | 5e33109 | 2008-07-15 21:21:56 +0200 | [diff] [blame] | 555 | pc->flags |= PC_FLAG_DMA_OK; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 556 | |
Tejun Heo | 963da55 | 2009-04-19 08:46:02 +0900 | [diff] [blame] | 557 | if (opcode == READ_6) |
Borislav Petkov | cd2abbf | 2008-07-15 21:22:03 +0200 | [diff] [blame] | 558 | pc->c[0] = READ_6; |
Tejun Heo | 963da55 | 2009-04-19 08:46:02 +0900 | [diff] [blame] | 559 | else if (opcode == WRITE_6) { |
Borislav Petkov | cd2abbf | 2008-07-15 21:22:03 +0200 | [diff] [blame] | 560 | pc->c[0] = WRITE_6; |
| 561 | pc->flags |= PC_FLAG_WRITING; |
Borislav Petkov | cd2abbf | 2008-07-15 21:22:03 +0200 | [diff] [blame] | 562 | } |
Borislav Petkov | 0014c75 | 2008-07-23 19:56:00 +0200 | [diff] [blame] | 563 | |
| 564 | memcpy(rq->cmd, pc->c, 12); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 565 | } |
| 566 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 567 | static ide_startstop_t idetape_do_request(ide_drive_t *drive, |
| 568 | struct request *rq, sector_t block) |
| 569 | { |
Bartlomiej Zolnierkiewicz | b73c7ee | 2008-07-23 19:55:52 +0200 | [diff] [blame] | 570 | ide_hwif_t *hwif = drive->hwif; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 571 | idetape_tape_t *tape = drive->driver_data; |
Borislav Petkov | d236d74 | 2008-04-18 00:46:27 +0200 | [diff] [blame] | 572 | struct ide_atapi_pc *pc = NULL; |
Bartlomiej Zolnierkiewicz | b788ee9 | 2009-03-27 12:46:46 +0100 | [diff] [blame] | 573 | struct ide_cmd cmd; |
Bartlomiej Zolnierkiewicz | 22c525b | 2008-01-25 22:17:11 +0100 | [diff] [blame] | 574 | u8 stat; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 575 | |
Borislav Petkov | e972d70 | 2009-07-17 23:55:16 +0000 | [diff] [blame] | 576 | ide_debug_log(IDE_DBG_RQ, "cmd: 0x%x, sector: %llu, nr_sectors: %u", |
| 577 | rq->cmd[0], (unsigned long long)blk_rq_pos(rq), |
| 578 | blk_rq_sectors(rq)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 579 | |
Bartlomiej Zolnierkiewicz | 2c7eaa4 | 2009-06-15 22:16:10 +0200 | [diff] [blame] | 580 | BUG_ON(!(blk_special_request(rq) || blk_sense_request(rq))); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 581 | |
Borislav Petkov | 3c98bf3 | 2008-02-06 02:57:53 +0100 | [diff] [blame] | 582 | /* Retry a failed packet command */ |
Bartlomiej Zolnierkiewicz | 5e2040fd | 2009-03-27 12:46:34 +0100 | [diff] [blame] | 583 | if (drive->failed_pc && drive->pc->c[0] == REQUEST_SENSE) { |
| 584 | pc = drive->failed_pc; |
Bartlomiej Zolnierkiewicz | 28c7214 | 2008-07-15 21:21:59 +0200 | [diff] [blame] | 585 | goto out; |
| 586 | } |
Borislav Petkov | 5a04cfa | 2008-02-06 02:57:54 +0100 | [diff] [blame] | 587 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 588 | /* |
| 589 | * If the tape is still busy, postpone our request and service |
| 590 | * the other device meanwhile. |
| 591 | */ |
Bartlomiej Zolnierkiewicz | 374e042 | 2008-07-23 19:55:56 +0200 | [diff] [blame] | 592 | stat = hwif->tp_ops->read_status(hwif); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 593 | |
Bartlomiej Zolnierkiewicz | 97100fc | 2008-10-13 21:39:36 +0200 | [diff] [blame] | 594 | if ((drive->dev_flags & IDE_DFLAG_DSC_OVERLAP) == 0 && |
| 595 | (rq->cmd[13] & REQ_IDETAPE_PC2) == 0) |
Borislav Petkov | 626542c | 2009-06-07 15:37:05 +0200 | [diff] [blame] | 596 | drive->atapi_flags |= IDE_AFLAG_IGNORE_DSC; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 597 | |
Bartlomiej Zolnierkiewicz | 97100fc | 2008-10-13 21:39:36 +0200 | [diff] [blame] | 598 | if (drive->dev_flags & IDE_DFLAG_POST_RESET) { |
Borislav Petkov | 626542c | 2009-06-07 15:37:05 +0200 | [diff] [blame] | 599 | drive->atapi_flags |= IDE_AFLAG_IGNORE_DSC; |
Bartlomiej Zolnierkiewicz | 97100fc | 2008-10-13 21:39:36 +0200 | [diff] [blame] | 600 | drive->dev_flags &= ~IDE_DFLAG_POST_RESET; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 601 | } |
| 602 | |
Borislav Petkov | 626542c | 2009-06-07 15:37:05 +0200 | [diff] [blame] | 603 | if (!(drive->atapi_flags & IDE_AFLAG_IGNORE_DSC) && |
| 604 | !(stat & ATA_DSC)) { |
Borislav Petkov | 6f3848a | 2009-07-21 23:08:23 -0700 | [diff] [blame] | 605 | if (!tape->postponed_rq) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 606 | tape->dsc_polling_start = jiffies; |
Borislav Petkov | 54bb207 | 2008-02-06 02:57:52 +0100 | [diff] [blame] | 607 | tape->dsc_poll_freq = tape->best_dsc_rw_freq; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 608 | tape->dsc_timeout = jiffies + IDETAPE_DSC_RW_TIMEOUT; |
| 609 | } else if (time_after(jiffies, tape->dsc_timeout)) { |
| 610 | printk(KERN_ERR "ide-tape: %s: DSC timeout\n", |
| 611 | tape->name); |
Borislav Petkov | 83dd573 | 2008-07-23 19:56:00 +0200 | [diff] [blame] | 612 | if (rq->cmd[13] & REQ_IDETAPE_PC2) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 613 | idetape_media_access_finished(drive); |
| 614 | return ide_stopped; |
| 615 | } else { |
| 616 | return ide_do_reset(drive); |
| 617 | } |
Borislav Petkov | 5a04cfa | 2008-02-06 02:57:54 +0100 | [diff] [blame] | 618 | } else if (time_after(jiffies, |
| 619 | tape->dsc_polling_start + |
| 620 | IDETAPE_DSC_MA_THRESHOLD)) |
Borislav Petkov | 54bb207 | 2008-02-06 02:57:52 +0100 | [diff] [blame] | 621 | tape->dsc_poll_freq = IDETAPE_DSC_MA_SLOW; |
Borislav Petkov | 6f3848a | 2009-07-21 23:08:23 -0700 | [diff] [blame] | 622 | ide_tape_stall_queue(drive); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 623 | return ide_stopped; |
Borislav Petkov | 6f3848a | 2009-07-21 23:08:23 -0700 | [diff] [blame] | 624 | } else { |
Borislav Petkov | 626542c | 2009-06-07 15:37:05 +0200 | [diff] [blame] | 625 | drive->atapi_flags &= ~IDE_AFLAG_IGNORE_DSC; |
Borislav Petkov | 6f3848a | 2009-07-21 23:08:23 -0700 | [diff] [blame] | 626 | tape->postponed_rq = false; |
| 627 | } |
Borislav Petkov | 626542c | 2009-06-07 15:37:05 +0200 | [diff] [blame] | 628 | |
Borislav Petkov | 83dd573 | 2008-07-23 19:56:00 +0200 | [diff] [blame] | 629 | if (rq->cmd[13] & REQ_IDETAPE_READ) { |
Bartlomiej Zolnierkiewicz | 2e8a6f8 | 2008-10-10 22:39:36 +0200 | [diff] [blame] | 630 | pc = &tape->queued_pc; |
Borislav Petkov | 0014c75 | 2008-07-23 19:56:00 +0200 | [diff] [blame] | 631 | ide_tape_create_rw_cmd(tape, pc, rq, READ_6); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 632 | goto out; |
| 633 | } |
Borislav Petkov | 83dd573 | 2008-07-23 19:56:00 +0200 | [diff] [blame] | 634 | if (rq->cmd[13] & REQ_IDETAPE_WRITE) { |
Bartlomiej Zolnierkiewicz | 2e8a6f8 | 2008-10-10 22:39:36 +0200 | [diff] [blame] | 635 | pc = &tape->queued_pc; |
Borislav Petkov | 0014c75 | 2008-07-23 19:56:00 +0200 | [diff] [blame] | 636 | ide_tape_create_rw_cmd(tape, pc, rq, WRITE_6); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 637 | goto out; |
| 638 | } |
Borislav Petkov | 83dd573 | 2008-07-23 19:56:00 +0200 | [diff] [blame] | 639 | if (rq->cmd[13] & REQ_IDETAPE_PC1) { |
Tejun Heo | c267cc1 | 2009-04-19 07:00:42 +0900 | [diff] [blame] | 640 | pc = (struct ide_atapi_pc *)rq->special; |
Borislav Petkov | 83dd573 | 2008-07-23 19:56:00 +0200 | [diff] [blame] | 641 | rq->cmd[13] &= ~(REQ_IDETAPE_PC1); |
| 642 | rq->cmd[13] |= REQ_IDETAPE_PC2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 643 | goto out; |
| 644 | } |
Borislav Petkov | 83dd573 | 2008-07-23 19:56:00 +0200 | [diff] [blame] | 645 | if (rq->cmd[13] & REQ_IDETAPE_PC2) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 646 | idetape_media_access_finished(drive); |
| 647 | return ide_stopped; |
| 648 | } |
| 649 | BUG(); |
Bartlomiej Zolnierkiewicz | 28c7214 | 2008-07-15 21:21:59 +0200 | [diff] [blame] | 650 | |
Borislav Petkov | f2e3ab5 | 2008-07-23 19:56:01 +0200 | [diff] [blame] | 651 | out: |
Borislav Petkov | 6b544fc | 2009-04-19 07:00:42 +0900 | [diff] [blame] | 652 | /* prepare sense request for this command */ |
| 653 | ide_prep_sense(drive, rq); |
| 654 | |
Bartlomiej Zolnierkiewicz | b788ee9 | 2009-03-27 12:46:46 +0100 | [diff] [blame] | 655 | memset(&cmd, 0, sizeof(cmd)); |
| 656 | |
| 657 | if (rq_data_dir(rq)) |
| 658 | cmd.tf_flags |= IDE_TFLAG_WRITE; |
| 659 | |
| 660 | cmd.rq = rq; |
| 661 | |
Borislav Petkov | dfb7e62 | 2009-05-01 20:35:21 +0200 | [diff] [blame] | 662 | ide_init_sg_cmd(&cmd, blk_rq_bytes(rq)); |
Tejun Heo | 5c4be57 | 2009-04-19 07:00:42 +0900 | [diff] [blame] | 663 | ide_map_sg(drive, &cmd); |
| 664 | |
Bartlomiej Zolnierkiewicz | b788ee9 | 2009-03-27 12:46:46 +0100 | [diff] [blame] | 665 | return ide_tape_issue_pc(drive, &cmd, pc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 666 | } |
| 667 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 668 | /* |
Borislav Petkov | 3c98bf3 | 2008-02-06 02:57:53 +0100 | [diff] [blame] | 669 | * Write a filemark if write_filemark=1. Flush the device buffers without |
| 670 | * writing a filemark otherwise. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 671 | */ |
Borislav Petkov | 5a04cfa | 2008-02-06 02:57:54 +0100 | [diff] [blame] | 672 | static void idetape_create_write_filemark_cmd(ide_drive_t *drive, |
Borislav Petkov | d236d74 | 2008-04-18 00:46:27 +0200 | [diff] [blame] | 673 | struct ide_atapi_pc *pc, int write_filemark) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 674 | { |
Bartlomiej Zolnierkiewicz | 7bf7420 | 2008-10-10 22:39:37 +0200 | [diff] [blame] | 675 | ide_init_pc(pc); |
Borislav Petkov | 90699ce | 2008-02-02 19:56:50 +0100 | [diff] [blame] | 676 | pc->c[0] = WRITE_FILEMARKS; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 677 | pc->c[4] = write_filemark; |
Borislav Petkov | 346331f | 2008-04-18 00:46:26 +0200 | [diff] [blame] | 678 | pc->flags |= PC_FLAG_WAIT_FOR_DSC; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 679 | } |
| 680 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 681 | static int idetape_wait_ready(ide_drive_t *drive, unsigned long timeout) |
| 682 | { |
| 683 | idetape_tape_t *tape = drive->driver_data; |
Bartlomiej Zolnierkiewicz | 2ac07d9 | 2008-10-10 22:39:38 +0200 | [diff] [blame] | 684 | struct gendisk *disk = tape->disk; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 685 | int load_attempted = 0; |
| 686 | |
Borislav Petkov | 3c98bf3 | 2008-02-06 02:57:53 +0100 | [diff] [blame] | 687 | /* Wait for the tape to become ready */ |
Borislav Petkov | 49d8078 | 2009-06-07 15:37:06 +0200 | [diff] [blame] | 688 | set_bit(ilog2(IDE_AFLAG_MEDIUM_PRESENT), &drive->atapi_flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 689 | timeout += jiffies; |
| 690 | while (time_before(jiffies, timeout)) { |
Bartlomiej Zolnierkiewicz | de699ad | 2008-10-10 22:39:39 +0200 | [diff] [blame] | 691 | if (ide_do_test_unit_ready(drive, disk) == 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 692 | return 0; |
| 693 | if ((tape->sense_key == 2 && tape->asc == 4 && tape->ascq == 2) |
Borislav Petkov | 3c98bf3 | 2008-02-06 02:57:53 +0100 | [diff] [blame] | 694 | || (tape->asc == 0x3A)) { |
| 695 | /* no media */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 696 | if (load_attempted) |
| 697 | return -ENOMEDIUM; |
Bartlomiej Zolnierkiewicz | 0c8a6c7 | 2008-10-10 22:39:39 +0200 | [diff] [blame] | 698 | ide_do_start_stop(drive, disk, IDETAPE_LU_LOAD_MASK); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 699 | load_attempted = 1; |
| 700 | /* not about to be ready */ |
| 701 | } else if (!(tape->sense_key == 2 && tape->asc == 4 && |
| 702 | (tape->ascq == 1 || tape->ascq == 8))) |
| 703 | return -EIO; |
Nishanth Aravamudan | 80ce45f | 2005-09-10 00:27:08 -0700 | [diff] [blame] | 704 | msleep(100); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 705 | } |
| 706 | return -EIO; |
| 707 | } |
| 708 | |
Borislav Petkov | 5a04cfa | 2008-02-06 02:57:54 +0100 | [diff] [blame] | 709 | static int idetape_flush_tape_buffers(ide_drive_t *drive) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 710 | { |
Bartlomiej Zolnierkiewicz | 2ac07d9 | 2008-10-10 22:39:38 +0200 | [diff] [blame] | 711 | struct ide_tape_obj *tape = drive->driver_data; |
Borislav Petkov | d236d74 | 2008-04-18 00:46:27 +0200 | [diff] [blame] | 712 | struct ide_atapi_pc pc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 713 | int rc; |
| 714 | |
| 715 | idetape_create_write_filemark_cmd(drive, &pc, 0); |
Borislav Petkov | b13345f | 2009-05-02 10:26:12 +0200 | [diff] [blame] | 716 | rc = ide_queue_pc_tail(drive, tape->disk, &pc, NULL, 0); |
Borislav Petkov | 5a04cfa | 2008-02-06 02:57:54 +0100 | [diff] [blame] | 717 | if (rc) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 718 | return rc; |
| 719 | idetape_wait_ready(drive, 60 * 5 * HZ); |
| 720 | return 0; |
| 721 | } |
| 722 | |
Borislav Petkov | 55ce3a1 | 2009-05-04 09:38:15 +0200 | [diff] [blame] | 723 | static int ide_tape_read_position(ide_drive_t *drive) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 724 | { |
| 725 | idetape_tape_t *tape = drive->driver_data; |
Borislav Petkov | d236d74 | 2008-04-18 00:46:27 +0200 | [diff] [blame] | 726 | struct ide_atapi_pc pc; |
Borislav Petkov | 55ce3a1 | 2009-05-04 09:38:15 +0200 | [diff] [blame] | 727 | u8 buf[20]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 728 | |
Borislav Petkov | e972d70 | 2009-07-17 23:55:16 +0000 | [diff] [blame] | 729 | ide_debug_log(IDE_DBG_FUNC, "enter"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 730 | |
Borislav Petkov | 55ce3a1 | 2009-05-04 09:38:15 +0200 | [diff] [blame] | 731 | /* prep cmd */ |
| 732 | ide_init_pc(&pc); |
| 733 | pc.c[0] = READ_POSITION; |
| 734 | pc.req_xfer = 20; |
| 735 | |
| 736 | if (ide_queue_pc_tail(drive, tape->disk, &pc, buf, pc.req_xfer)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 737 | return -1; |
Borislav Petkov | 55ce3a1 | 2009-05-04 09:38:15 +0200 | [diff] [blame] | 738 | |
| 739 | if (!pc.error) { |
Borislav Petkov | e972d70 | 2009-07-17 23:55:16 +0000 | [diff] [blame] | 740 | ide_debug_log(IDE_DBG_FUNC, "BOP - %s", |
Borislav Petkov | 55ce3a1 | 2009-05-04 09:38:15 +0200 | [diff] [blame] | 741 | (buf[0] & 0x80) ? "Yes" : "No"); |
Borislav Petkov | e972d70 | 2009-07-17 23:55:16 +0000 | [diff] [blame] | 742 | ide_debug_log(IDE_DBG_FUNC, "EOP - %s", |
Borislav Petkov | 55ce3a1 | 2009-05-04 09:38:15 +0200 | [diff] [blame] | 743 | (buf[0] & 0x40) ? "Yes" : "No"); |
| 744 | |
| 745 | if (buf[0] & 0x4) { |
| 746 | printk(KERN_INFO "ide-tape: Block location is unknown" |
| 747 | "to the tape\n"); |
Bartlomiej Zolnierkiewicz | 8dcce40 | 2009-06-13 12:00:54 +0200 | [diff] [blame] | 748 | clear_bit(ilog2(IDE_AFLAG_ADDRESS_VALID), |
| 749 | &drive->atapi_flags); |
Borislav Petkov | 55ce3a1 | 2009-05-04 09:38:15 +0200 | [diff] [blame] | 750 | return -1; |
| 751 | } else { |
Borislav Petkov | e972d70 | 2009-07-17 23:55:16 +0000 | [diff] [blame] | 752 | ide_debug_log(IDE_DBG_FUNC, "Block Location: %u", |
| 753 | be32_to_cpup((__be32 *)&buf[4])); |
Borislav Petkov | 55ce3a1 | 2009-05-04 09:38:15 +0200 | [diff] [blame] | 754 | |
| 755 | tape->partition = buf[1]; |
| 756 | tape->first_frame = be32_to_cpup((__be32 *)&buf[4]); |
Bartlomiej Zolnierkiewicz | 8dcce40 | 2009-06-13 12:00:54 +0200 | [diff] [blame] | 757 | set_bit(ilog2(IDE_AFLAG_ADDRESS_VALID), |
| 758 | &drive->atapi_flags); |
Borislav Petkov | 55ce3a1 | 2009-05-04 09:38:15 +0200 | [diff] [blame] | 759 | } |
| 760 | } |
| 761 | |
| 762 | return tape->first_frame; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 763 | } |
| 764 | |
Borislav Petkov | d236d74 | 2008-04-18 00:46:27 +0200 | [diff] [blame] | 765 | static void idetape_create_locate_cmd(ide_drive_t *drive, |
| 766 | struct ide_atapi_pc *pc, |
Borislav Petkov | 5a04cfa | 2008-02-06 02:57:54 +0100 | [diff] [blame] | 767 | unsigned int block, u8 partition, int skip) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 768 | { |
Bartlomiej Zolnierkiewicz | 7bf7420 | 2008-10-10 22:39:37 +0200 | [diff] [blame] | 769 | ide_init_pc(pc); |
Borislav Petkov | 90699ce | 2008-02-02 19:56:50 +0100 | [diff] [blame] | 770 | pc->c[0] = POSITION_TO_ELEMENT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 771 | pc->c[1] = 2; |
Borislav Petkov | 860ff5e | 2008-02-02 19:56:50 +0100 | [diff] [blame] | 772 | put_unaligned(cpu_to_be32(block), (unsigned int *) &pc->c[3]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 773 | pc->c[8] = partition; |
Borislav Petkov | 346331f | 2008-04-18 00:46:26 +0200 | [diff] [blame] | 774 | pc->flags |= PC_FLAG_WAIT_FOR_DSC; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 775 | } |
| 776 | |
Borislav Petkov | ec0fdb0 | 2008-04-27 15:38:34 +0200 | [diff] [blame] | 777 | static void __ide_tape_discard_merge_buffer(ide_drive_t *drive) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 778 | { |
| 779 | idetape_tape_t *tape = drive->driver_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 780 | |
Borislav Petkov | 54abf37 | 2008-02-06 02:57:52 +0100 | [diff] [blame] | 781 | if (tape->chrdev_dir != IDETAPE_DIR_READ) |
Borislav Petkov | 9798630 | 2008-04-27 15:38:34 +0200 | [diff] [blame] | 782 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 783 | |
Borislav Petkov | 49d8078 | 2009-06-07 15:37:06 +0200 | [diff] [blame] | 784 | clear_bit(ilog2(IDE_AFLAG_FILEMARK), &drive->atapi_flags); |
Tejun Heo | 963da55 | 2009-04-19 08:46:02 +0900 | [diff] [blame] | 785 | tape->valid = 0; |
| 786 | if (tape->buf != NULL) { |
| 787 | kfree(tape->buf); |
| 788 | tape->buf = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 789 | } |
| 790 | |
Borislav Petkov | 54abf37 | 2008-02-06 02:57:52 +0100 | [diff] [blame] | 791 | tape->chrdev_dir = IDETAPE_DIR_NONE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 792 | } |
| 793 | |
| 794 | /* |
Borislav Petkov | 3c98bf3 | 2008-02-06 02:57:53 +0100 | [diff] [blame] | 795 | * Position the tape to the requested block using the LOCATE packet command. |
| 796 | * A READ POSITION command is then issued to check where we are positioned. Like |
| 797 | * all higher level operations, we queue the commands at the tail of the request |
| 798 | * queue and wait for their completion. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 799 | */ |
Borislav Petkov | 5a04cfa | 2008-02-06 02:57:54 +0100 | [diff] [blame] | 800 | static int idetape_position_tape(ide_drive_t *drive, unsigned int block, |
| 801 | u8 partition, int skip) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 802 | { |
| 803 | idetape_tape_t *tape = drive->driver_data; |
Bartlomiej Zolnierkiewicz | 2ac07d9 | 2008-10-10 22:39:38 +0200 | [diff] [blame] | 804 | struct gendisk *disk = tape->disk; |
Borislav Petkov | 55ce3a1 | 2009-05-04 09:38:15 +0200 | [diff] [blame] | 805 | int ret; |
Borislav Petkov | d236d74 | 2008-04-18 00:46:27 +0200 | [diff] [blame] | 806 | struct ide_atapi_pc pc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 807 | |
Borislav Petkov | 54abf37 | 2008-02-06 02:57:52 +0100 | [diff] [blame] | 808 | if (tape->chrdev_dir == IDETAPE_DIR_READ) |
Borislav Petkov | ec0fdb0 | 2008-04-27 15:38:34 +0200 | [diff] [blame] | 809 | __ide_tape_discard_merge_buffer(drive); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 810 | idetape_wait_ready(drive, 60 * 5 * HZ); |
| 811 | idetape_create_locate_cmd(drive, &pc, block, partition, skip); |
Borislav Petkov | 55ce3a1 | 2009-05-04 09:38:15 +0200 | [diff] [blame] | 812 | ret = ide_queue_pc_tail(drive, disk, &pc, NULL, 0); |
| 813 | if (ret) |
| 814 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 815 | |
Borislav Petkov | 55ce3a1 | 2009-05-04 09:38:15 +0200 | [diff] [blame] | 816 | ret = ide_tape_read_position(drive); |
| 817 | if (ret < 0) |
| 818 | return ret; |
| 819 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 820 | } |
| 821 | |
Borislav Petkov | ec0fdb0 | 2008-04-27 15:38:34 +0200 | [diff] [blame] | 822 | static void ide_tape_discard_merge_buffer(ide_drive_t *drive, |
Borislav Petkov | 5a04cfa | 2008-02-06 02:57:54 +0100 | [diff] [blame] | 823 | int restore_position) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 824 | { |
| 825 | idetape_tape_t *tape = drive->driver_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 826 | int seek, position; |
| 827 | |
Borislav Petkov | ec0fdb0 | 2008-04-27 15:38:34 +0200 | [diff] [blame] | 828 | __ide_tape_discard_merge_buffer(drive); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 829 | if (restore_position) { |
Borislav Petkov | 55ce3a1 | 2009-05-04 09:38:15 +0200 | [diff] [blame] | 830 | position = ide_tape_read_position(drive); |
Borislav Petkov | 9798630 | 2008-04-27 15:38:34 +0200 | [diff] [blame] | 831 | seek = position > 0 ? position : 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 832 | if (idetape_position_tape(drive, seek, 0, 0)) { |
Borislav Petkov | 5a04cfa | 2008-02-06 02:57:54 +0100 | [diff] [blame] | 833 | printk(KERN_INFO "ide-tape: %s: position_tape failed in" |
Borislav Petkov | ec0fdb0 | 2008-04-27 15:38:34 +0200 | [diff] [blame] | 834 | " %s\n", tape->name, __func__); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 835 | return; |
| 836 | } |
| 837 | } |
| 838 | } |
| 839 | |
| 840 | /* |
Borislav Petkov | 3c98bf3 | 2008-02-06 02:57:53 +0100 | [diff] [blame] | 841 | * Generate a read/write request for the block device interface and wait for it |
| 842 | * to be serviced. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 843 | */ |
Tejun Heo | 6bb11dd | 2009-04-19 08:46:03 +0900 | [diff] [blame] | 844 | static int idetape_queue_rw_tail(ide_drive_t *drive, int cmd, int size) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 845 | { |
| 846 | idetape_tape_t *tape = drive->driver_data; |
FUJITA Tomonori | 64ea1b4 | 2008-07-15 21:21:43 +0200 | [diff] [blame] | 847 | struct request *rq; |
Tejun Heo | 21d9c5d | 2009-04-19 08:46:02 +0900 | [diff] [blame] | 848 | int ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 849 | |
Borislav Petkov | e972d70 | 2009-07-17 23:55:16 +0000 | [diff] [blame] | 850 | ide_debug_log(IDE_DBG_FUNC, "cmd: 0x%x, size: %d", cmd, size); |
| 851 | |
Tejun Heo | 21d9c5d | 2009-04-19 08:46:02 +0900 | [diff] [blame] | 852 | BUG_ON(cmd != REQ_IDETAPE_READ && cmd != REQ_IDETAPE_WRITE); |
Tejun Heo | 6bb11dd | 2009-04-19 08:46:03 +0900 | [diff] [blame] | 853 | BUG_ON(size < 0 || size % tape->blk_size); |
Borislav Petkov | 8004a8c | 2008-02-06 02:57:51 +0100 | [diff] [blame] | 854 | |
FUJITA Tomonori | 64ea1b4 | 2008-07-15 21:21:43 +0200 | [diff] [blame] | 855 | rq = blk_get_request(drive->queue, READ, __GFP_WAIT); |
| 856 | rq->cmd_type = REQ_TYPE_SPECIAL; |
Borislav Petkov | 83dd573 | 2008-07-23 19:56:00 +0200 | [diff] [blame] | 857 | rq->cmd[13] = cmd; |
FUJITA Tomonori | 64ea1b4 | 2008-07-15 21:21:43 +0200 | [diff] [blame] | 858 | rq->rq_disk = tape->disk; |
Linus Torvalds | c905959 | 2009-06-11 10:52:27 -0700 | [diff] [blame] | 859 | rq->__sector = tape->first_frame; |
Tejun Heo | 21d9c5d | 2009-04-19 08:46:02 +0900 | [diff] [blame] | 860 | |
| 861 | if (size) { |
Tejun Heo | 963da55 | 2009-04-19 08:46:02 +0900 | [diff] [blame] | 862 | ret = blk_rq_map_kern(drive->queue, rq, tape->buf, size, |
Tejun Heo | 21d9c5d | 2009-04-19 08:46:02 +0900 | [diff] [blame] | 863 | __GFP_WAIT); |
| 864 | if (ret) |
| 865 | goto out_put; |
| 866 | } |
| 867 | |
FUJITA Tomonori | 64ea1b4 | 2008-07-15 21:21:43 +0200 | [diff] [blame] | 868 | blk_execute_rq(drive->queue, tape->disk, rq, 0); |
| 869 | |
Tejun Heo | 963da55 | 2009-04-19 08:46:02 +0900 | [diff] [blame] | 870 | /* calculate the number of transferred bytes and update buffer state */ |
Tejun Heo | c3a4d78 | 2009-05-07 22:24:37 +0900 | [diff] [blame] | 871 | size -= rq->resid_len; |
Tejun Heo | 963da55 | 2009-04-19 08:46:02 +0900 | [diff] [blame] | 872 | tape->cur = tape->buf; |
Tejun Heo | 21d9c5d | 2009-04-19 08:46:02 +0900 | [diff] [blame] | 873 | if (cmd == REQ_IDETAPE_READ) |
Tejun Heo | 963da55 | 2009-04-19 08:46:02 +0900 | [diff] [blame] | 874 | tape->valid = size; |
| 875 | else |
| 876 | tape->valid = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 877 | |
Tejun Heo | 21d9c5d | 2009-04-19 08:46:02 +0900 | [diff] [blame] | 878 | ret = size; |
| 879 | if (rq->errors == IDE_DRV_ERROR_GENERAL) |
| 880 | ret = -EIO; |
Tejun Heo | 21d9c5d | 2009-04-19 08:46:02 +0900 | [diff] [blame] | 881 | out_put: |
| 882 | blk_put_request(rq); |
FUJITA Tomonori | 64ea1b4 | 2008-07-15 21:21:43 +0200 | [diff] [blame] | 883 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 884 | } |
| 885 | |
Borislav Petkov | d236d74 | 2008-04-18 00:46:27 +0200 | [diff] [blame] | 886 | static void idetape_create_inquiry_cmd(struct ide_atapi_pc *pc) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 887 | { |
Bartlomiej Zolnierkiewicz | 7bf7420 | 2008-10-10 22:39:37 +0200 | [diff] [blame] | 888 | ide_init_pc(pc); |
Borislav Petkov | 90699ce | 2008-02-02 19:56:50 +0100 | [diff] [blame] | 889 | pc->c[0] = INQUIRY; |
Borislav Petkov | 5a04cfa | 2008-02-06 02:57:54 +0100 | [diff] [blame] | 890 | pc->c[4] = 254; |
Borislav Petkov | d236d74 | 2008-04-18 00:46:27 +0200 | [diff] [blame] | 891 | pc->req_xfer = 254; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 892 | } |
| 893 | |
Borislav Petkov | d236d74 | 2008-04-18 00:46:27 +0200 | [diff] [blame] | 894 | static void idetape_create_rewind_cmd(ide_drive_t *drive, |
| 895 | struct ide_atapi_pc *pc) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 896 | { |
Bartlomiej Zolnierkiewicz | 7bf7420 | 2008-10-10 22:39:37 +0200 | [diff] [blame] | 897 | ide_init_pc(pc); |
Borislav Petkov | 90699ce | 2008-02-02 19:56:50 +0100 | [diff] [blame] | 898 | pc->c[0] = REZERO_UNIT; |
Borislav Petkov | 346331f | 2008-04-18 00:46:26 +0200 | [diff] [blame] | 899 | pc->flags |= PC_FLAG_WAIT_FOR_DSC; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 900 | } |
| 901 | |
Borislav Petkov | d236d74 | 2008-04-18 00:46:27 +0200 | [diff] [blame] | 902 | static void idetape_create_erase_cmd(struct ide_atapi_pc *pc) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 903 | { |
Bartlomiej Zolnierkiewicz | 7bf7420 | 2008-10-10 22:39:37 +0200 | [diff] [blame] | 904 | ide_init_pc(pc); |
Borislav Petkov | 90699ce | 2008-02-02 19:56:50 +0100 | [diff] [blame] | 905 | pc->c[0] = ERASE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 906 | pc->c[1] = 1; |
Borislav Petkov | 346331f | 2008-04-18 00:46:26 +0200 | [diff] [blame] | 907 | pc->flags |= PC_FLAG_WAIT_FOR_DSC; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 908 | } |
| 909 | |
Borislav Petkov | d236d74 | 2008-04-18 00:46:27 +0200 | [diff] [blame] | 910 | static void idetape_create_space_cmd(struct ide_atapi_pc *pc, int count, u8 cmd) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 911 | { |
Bartlomiej Zolnierkiewicz | 7bf7420 | 2008-10-10 22:39:37 +0200 | [diff] [blame] | 912 | ide_init_pc(pc); |
Borislav Petkov | 90699ce | 2008-02-02 19:56:50 +0100 | [diff] [blame] | 913 | pc->c[0] = SPACE; |
Borislav Petkov | 860ff5e | 2008-02-02 19:56:50 +0100 | [diff] [blame] | 914 | put_unaligned(cpu_to_be32(count), (unsigned int *) &pc->c[1]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 915 | pc->c[1] = cmd; |
Borislav Petkov | 346331f | 2008-04-18 00:46:26 +0200 | [diff] [blame] | 916 | pc->flags |= PC_FLAG_WAIT_FOR_DSC; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 917 | } |
| 918 | |
Borislav Petkov | d9df937 | 2008-04-27 15:38:34 +0200 | [diff] [blame] | 919 | static void ide_tape_flush_merge_buffer(ide_drive_t *drive) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 920 | { |
| 921 | idetape_tape_t *tape = drive->driver_data; |
Borislav Petkov | 55a5d29 | 2008-02-02 19:56:49 +0100 | [diff] [blame] | 922 | |
Borislav Petkov | 54abf37 | 2008-02-06 02:57:52 +0100 | [diff] [blame] | 923 | if (tape->chrdev_dir != IDETAPE_DIR_WRITE) { |
Borislav Petkov | 077e3bd | 2008-04-27 15:38:34 +0200 | [diff] [blame] | 924 | printk(KERN_ERR "ide-tape: bug: Trying to empty merge buffer" |
Borislav Petkov | 5a04cfa | 2008-02-06 02:57:54 +0100 | [diff] [blame] | 925 | " but we are not writing.\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 926 | return; |
| 927 | } |
Tejun Heo | 963da55 | 2009-04-19 08:46:02 +0900 | [diff] [blame] | 928 | if (tape->buf) { |
Tejun Heo | 6bb11dd | 2009-04-19 08:46:03 +0900 | [diff] [blame] | 929 | size_t aligned = roundup(tape->valid, tape->blk_size); |
| 930 | |
| 931 | memset(tape->cur, 0, aligned - tape->valid); |
Tejun Heo | 07bd968 | 2009-04-19 08:46:03 +0900 | [diff] [blame] | 932 | idetape_queue_rw_tail(drive, REQ_IDETAPE_WRITE, aligned); |
Tejun Heo | 963da55 | 2009-04-19 08:46:02 +0900 | [diff] [blame] | 933 | kfree(tape->buf); |
| 934 | tape->buf = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 935 | } |
Borislav Petkov | 54abf37 | 2008-02-06 02:57:52 +0100 | [diff] [blame] | 936 | tape->chrdev_dir = IDETAPE_DIR_NONE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 937 | } |
| 938 | |
Tejun Heo | 88f1b94 | 2009-04-19 08:46:02 +0900 | [diff] [blame] | 939 | static int idetape_init_rw(ide_drive_t *drive, int dir) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 940 | { |
| 941 | idetape_tape_t *tape = drive->driver_data; |
Tejun Heo | 88f1b94 | 2009-04-19 08:46:02 +0900 | [diff] [blame] | 942 | int rc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 943 | |
Tejun Heo | 88f1b94 | 2009-04-19 08:46:02 +0900 | [diff] [blame] | 944 | BUG_ON(dir != IDETAPE_DIR_READ && dir != IDETAPE_DIR_WRITE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 945 | |
Tejun Heo | 88f1b94 | 2009-04-19 08:46:02 +0900 | [diff] [blame] | 946 | if (tape->chrdev_dir == dir) |
| 947 | return 0; |
| 948 | |
| 949 | if (tape->chrdev_dir == IDETAPE_DIR_READ) |
| 950 | ide_tape_discard_merge_buffer(drive, 1); |
| 951 | else if (tape->chrdev_dir == IDETAPE_DIR_WRITE) { |
| 952 | ide_tape_flush_merge_buffer(drive); |
| 953 | idetape_flush_tape_buffers(drive); |
| 954 | } |
| 955 | |
| 956 | if (tape->buf || tape->valid) { |
| 957 | printk(KERN_ERR "ide-tape: valid should be 0 now\n"); |
| 958 | tape->valid = 0; |
| 959 | } |
| 960 | |
| 961 | tape->buf = kmalloc(tape->buffer_size, GFP_KERNEL); |
| 962 | if (!tape->buf) |
| 963 | return -ENOMEM; |
| 964 | tape->chrdev_dir = dir; |
| 965 | tape->cur = tape->buf; |
| 966 | |
| 967 | /* |
| 968 | * Issue a 0 rw command to ensure that DSC handshake is |
| 969 | * switched from completion mode to buffer available mode. No |
| 970 | * point in issuing this if DSC overlap isn't supported, some |
| 971 | * drives (Seagate STT3401A) will return an error. |
| 972 | */ |
| 973 | if (drive->dev_flags & IDE_DFLAG_DSC_OVERLAP) { |
| 974 | int cmd = dir == IDETAPE_DIR_READ ? REQ_IDETAPE_READ |
| 975 | : REQ_IDETAPE_WRITE; |
| 976 | |
| 977 | rc = idetape_queue_rw_tail(drive, cmd, 0); |
| 978 | if (rc < 0) { |
| 979 | kfree(tape->buf); |
| 980 | tape->buf = NULL; |
| 981 | tape->chrdev_dir = IDETAPE_DIR_NONE; |
| 982 | return rc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 983 | } |
| 984 | } |
Borislav Petkov | 5e69bd9 | 2008-04-27 15:38:25 +0200 | [diff] [blame] | 985 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 986 | return 0; |
| 987 | } |
| 988 | |
Borislav Petkov | 5a04cfa | 2008-02-06 02:57:54 +0100 | [diff] [blame] | 989 | static void idetape_pad_zeros(ide_drive_t *drive, int bcount) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 990 | { |
| 991 | idetape_tape_t *tape = drive->driver_data; |
Tejun Heo | 963da55 | 2009-04-19 08:46:02 +0900 | [diff] [blame] | 992 | |
| 993 | memset(tape->buf, 0, tape->buffer_size); |
Borislav Petkov | 3c98bf3 | 2008-02-06 02:57:53 +0100 | [diff] [blame] | 994 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 995 | while (bcount) { |
Tejun Heo | 963da55 | 2009-04-19 08:46:02 +0900 | [diff] [blame] | 996 | unsigned int count = min(tape->buffer_size, bcount); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 997 | |
Tejun Heo | 6bb11dd | 2009-04-19 08:46:03 +0900 | [diff] [blame] | 998 | idetape_queue_rw_tail(drive, REQ_IDETAPE_WRITE, count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 999 | bcount -= count; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1000 | } |
| 1001 | } |
| 1002 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1003 | /* |
Borislav Petkov | 3c98bf3 | 2008-02-06 02:57:53 +0100 | [diff] [blame] | 1004 | * Rewinds the tape to the Beginning Of the current Partition (BOP). We |
| 1005 | * currently support only one partition. |
| 1006 | */ |
Borislav Petkov | 5a04cfa | 2008-02-06 02:57:54 +0100 | [diff] [blame] | 1007 | static int idetape_rewind_tape(ide_drive_t *drive) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1008 | { |
Bartlomiej Zolnierkiewicz | 2ac07d9 | 2008-10-10 22:39:38 +0200 | [diff] [blame] | 1009 | struct ide_tape_obj *tape = drive->driver_data; |
| 1010 | struct gendisk *disk = tape->disk; |
Borislav Petkov | d236d74 | 2008-04-18 00:46:27 +0200 | [diff] [blame] | 1011 | struct ide_atapi_pc pc; |
Borislav Petkov | 55ce3a1 | 2009-05-04 09:38:15 +0200 | [diff] [blame] | 1012 | int ret; |
Borislav Petkov | 8004a8c | 2008-02-06 02:57:51 +0100 | [diff] [blame] | 1013 | |
Borislav Petkov | e972d70 | 2009-07-17 23:55:16 +0000 | [diff] [blame] | 1014 | ide_debug_log(IDE_DBG_FUNC, "enter"); |
Borislav Petkov | 8004a8c | 2008-02-06 02:57:51 +0100 | [diff] [blame] | 1015 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1016 | idetape_create_rewind_cmd(drive, &pc); |
Borislav Petkov | 55ce3a1 | 2009-05-04 09:38:15 +0200 | [diff] [blame] | 1017 | ret = ide_queue_pc_tail(drive, disk, &pc, NULL, 0); |
| 1018 | if (ret) |
| 1019 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1020 | |
Borislav Petkov | 55ce3a1 | 2009-05-04 09:38:15 +0200 | [diff] [blame] | 1021 | ret = ide_tape_read_position(drive); |
| 1022 | if (ret < 0) |
| 1023 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1024 | return 0; |
| 1025 | } |
| 1026 | |
Borislav Petkov | 3c98bf3 | 2008-02-06 02:57:53 +0100 | [diff] [blame] | 1027 | /* mtio.h compatible commands should be issued to the chrdev interface. */ |
Borislav Petkov | 5a04cfa | 2008-02-06 02:57:54 +0100 | [diff] [blame] | 1028 | static int idetape_blkdev_ioctl(ide_drive_t *drive, unsigned int cmd, |
| 1029 | unsigned long arg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1030 | { |
| 1031 | idetape_tape_t *tape = drive->driver_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1032 | void __user *argp = (void __user *)arg; |
| 1033 | |
Borislav Petkov | d59823f | 2008-02-02 19:56:51 +0100 | [diff] [blame] | 1034 | struct idetape_config { |
| 1035 | int dsc_rw_frequency; |
| 1036 | int dsc_media_access_frequency; |
| 1037 | int nr_stages; |
| 1038 | } config; |
| 1039 | |
Borislav Petkov | e972d70 | 2009-07-17 23:55:16 +0000 | [diff] [blame] | 1040 | ide_debug_log(IDE_DBG_FUNC, "cmd: 0x%04x", cmd); |
Borislav Petkov | 8004a8c | 2008-02-06 02:57:51 +0100 | [diff] [blame] | 1041 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1042 | switch (cmd) { |
Borislav Petkov | 5a04cfa | 2008-02-06 02:57:54 +0100 | [diff] [blame] | 1043 | case 0x0340: |
| 1044 | if (copy_from_user(&config, argp, sizeof(config))) |
| 1045 | return -EFAULT; |
| 1046 | tape->best_dsc_rw_freq = config.dsc_rw_frequency; |
Borislav Petkov | 5a04cfa | 2008-02-06 02:57:54 +0100 | [diff] [blame] | 1047 | break; |
| 1048 | case 0x0350: |
Michael Buesch | 2fc2111 | 2009-07-19 09:15:19 +0000 | [diff] [blame] | 1049 | memset(&config, 0, sizeof(config)); |
Borislav Petkov | 5a04cfa | 2008-02-06 02:57:54 +0100 | [diff] [blame] | 1050 | config.dsc_rw_frequency = (int) tape->best_dsc_rw_freq; |
Borislav Petkov | 83042b2 | 2008-04-27 15:38:27 +0200 | [diff] [blame] | 1051 | config.nr_stages = 1; |
Borislav Petkov | 5a04cfa | 2008-02-06 02:57:54 +0100 | [diff] [blame] | 1052 | if (copy_to_user(argp, &config, sizeof(config))) |
| 1053 | return -EFAULT; |
| 1054 | break; |
| 1055 | default: |
| 1056 | return -EIO; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1057 | } |
| 1058 | return 0; |
| 1059 | } |
| 1060 | |
Borislav Petkov | 5a04cfa | 2008-02-06 02:57:54 +0100 | [diff] [blame] | 1061 | static int idetape_space_over_filemarks(ide_drive_t *drive, short mt_op, |
| 1062 | int mt_count) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1063 | { |
| 1064 | idetape_tape_t *tape = drive->driver_data; |
Bartlomiej Zolnierkiewicz | 2ac07d9 | 2008-10-10 22:39:38 +0200 | [diff] [blame] | 1065 | struct gendisk *disk = tape->disk; |
Borislav Petkov | d236d74 | 2008-04-18 00:46:27 +0200 | [diff] [blame] | 1066 | struct ide_atapi_pc pc; |
Borislav Petkov | 5a04cfa | 2008-02-06 02:57:54 +0100 | [diff] [blame] | 1067 | int retval, count = 0; |
Borislav Petkov | b642201 | 2008-02-02 19:56:49 +0100 | [diff] [blame] | 1068 | int sprev = !!(tape->caps[4] & 0x20); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1069 | |
Borislav Petkov | e972d70 | 2009-07-17 23:55:16 +0000 | [diff] [blame] | 1070 | |
| 1071 | ide_debug_log(IDE_DBG_FUNC, "mt_op: %d, mt_count: %d", mt_op, mt_count); |
| 1072 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1073 | if (mt_count == 0) |
| 1074 | return 0; |
| 1075 | if (MTBSF == mt_op || MTBSFM == mt_op) { |
Borislav Petkov | b642201 | 2008-02-02 19:56:49 +0100 | [diff] [blame] | 1076 | if (!sprev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1077 | return -EIO; |
Borislav Petkov | 5a04cfa | 2008-02-06 02:57:54 +0100 | [diff] [blame] | 1078 | mt_count = -mt_count; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1079 | } |
| 1080 | |
Borislav Petkov | 54abf37 | 2008-02-06 02:57:52 +0100 | [diff] [blame] | 1081 | if (tape->chrdev_dir == IDETAPE_DIR_READ) { |
Tejun Heo | 963da55 | 2009-04-19 08:46:02 +0900 | [diff] [blame] | 1082 | tape->valid = 0; |
Borislav Petkov | 49d8078 | 2009-06-07 15:37:06 +0200 | [diff] [blame] | 1083 | if (test_and_clear_bit(ilog2(IDE_AFLAG_FILEMARK), |
| 1084 | &drive->atapi_flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1085 | ++count; |
Borislav Petkov | ec0fdb0 | 2008-04-27 15:38:34 +0200 | [diff] [blame] | 1086 | ide_tape_discard_merge_buffer(drive, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1087 | } |
| 1088 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1089 | switch (mt_op) { |
Borislav Petkov | 5a04cfa | 2008-02-06 02:57:54 +0100 | [diff] [blame] | 1090 | case MTFSF: |
| 1091 | case MTBSF: |
| 1092 | idetape_create_space_cmd(&pc, mt_count - count, |
| 1093 | IDETAPE_SPACE_OVER_FILEMARK); |
Borislav Petkov | b13345f | 2009-05-02 10:26:12 +0200 | [diff] [blame] | 1094 | return ide_queue_pc_tail(drive, disk, &pc, NULL, 0); |
Borislav Petkov | 5a04cfa | 2008-02-06 02:57:54 +0100 | [diff] [blame] | 1095 | case MTFSFM: |
| 1096 | case MTBSFM: |
| 1097 | if (!sprev) |
| 1098 | return -EIO; |
| 1099 | retval = idetape_space_over_filemarks(drive, MTFSF, |
| 1100 | mt_count - count); |
| 1101 | if (retval) |
| 1102 | return retval; |
| 1103 | count = (MTBSFM == mt_op ? 1 : -1); |
| 1104 | return idetape_space_over_filemarks(drive, MTFSF, count); |
| 1105 | default: |
| 1106 | printk(KERN_ERR "ide-tape: MTIO operation %d not supported\n", |
| 1107 | mt_op); |
| 1108 | return -EIO; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1109 | } |
| 1110 | } |
| 1111 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1112 | /* |
Borislav Petkov | 3c98bf3 | 2008-02-06 02:57:53 +0100 | [diff] [blame] | 1113 | * Our character device read / write functions. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1114 | * |
Borislav Petkov | 3c98bf3 | 2008-02-06 02:57:53 +0100 | [diff] [blame] | 1115 | * The tape is optimized to maximize throughput when it is transferring an |
| 1116 | * integral number of the "continuous transfer limit", which is a parameter of |
| 1117 | * the specific tape (26kB on my particular tape, 32kB for Onstream). |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1118 | * |
Borislav Petkov | 3c98bf3 | 2008-02-06 02:57:53 +0100 | [diff] [blame] | 1119 | * As of version 1.3 of the driver, the character device provides an abstract |
| 1120 | * continuous view of the media - any mix of block sizes (even 1 byte) on the |
| 1121 | * same backup/restore procedure is supported. The driver will internally |
| 1122 | * convert the requests to the recommended transfer unit, so that an unmatch |
| 1123 | * between the user's block size to the recommended size will only result in a |
| 1124 | * (slightly) increased driver overhead, but will no longer hit performance. |
| 1125 | * This is not applicable to Onstream. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1126 | */ |
Borislav Petkov | 5a04cfa | 2008-02-06 02:57:54 +0100 | [diff] [blame] | 1127 | static ssize_t idetape_chrdev_read(struct file *file, char __user *buf, |
| 1128 | size_t count, loff_t *ppos) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1129 | { |
Borislav Petkov | 5aeddf9 | 2008-10-13 21:39:34 +0200 | [diff] [blame] | 1130 | struct ide_tape_obj *tape = file->private_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1131 | ide_drive_t *drive = tape->drive; |
Tejun Heo | 07bd968 | 2009-04-19 08:46:03 +0900 | [diff] [blame] | 1132 | size_t done = 0; |
Daniel Walker | dcd9637 | 2006-06-25 05:47:37 -0700 | [diff] [blame] | 1133 | ssize_t ret = 0; |
Tejun Heo | 07bd968 | 2009-04-19 08:46:03 +0900 | [diff] [blame] | 1134 | int rc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1135 | |
Borislav Petkov | e972d70 | 2009-07-17 23:55:16 +0000 | [diff] [blame] | 1136 | ide_debug_log(IDE_DBG_FUNC, "count %Zd", count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1137 | |
Borislav Petkov | 54abf37 | 2008-02-06 02:57:52 +0100 | [diff] [blame] | 1138 | if (tape->chrdev_dir != IDETAPE_DIR_READ) { |
Borislav Petkov | 49d8078 | 2009-06-07 15:37:06 +0200 | [diff] [blame] | 1139 | if (test_bit(ilog2(IDE_AFLAG_DETECT_BS), &drive->atapi_flags)) |
Borislav Petkov | 54bb207 | 2008-02-06 02:57:52 +0100 | [diff] [blame] | 1140 | if (count > tape->blk_size && |
| 1141 | (count % tape->blk_size) == 0) |
| 1142 | tape->user_bs_factor = count / tape->blk_size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1143 | } |
Tejun Heo | 07bd968 | 2009-04-19 08:46:03 +0900 | [diff] [blame] | 1144 | |
Tejun Heo | 88f1b94 | 2009-04-19 08:46:02 +0900 | [diff] [blame] | 1145 | rc = idetape_init_rw(drive, IDETAPE_DIR_READ); |
Borislav Petkov | 8d06bfa | 2008-02-06 02:57:53 +0100 | [diff] [blame] | 1146 | if (rc < 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1147 | return rc; |
Tejun Heo | 07bd968 | 2009-04-19 08:46:03 +0900 | [diff] [blame] | 1148 | |
| 1149 | while (done < count) { |
| 1150 | size_t todo; |
| 1151 | |
| 1152 | /* refill if staging buffer is empty */ |
| 1153 | if (!tape->valid) { |
| 1154 | /* If we are at a filemark, nothing more to read */ |
Borislav Petkov | 49d8078 | 2009-06-07 15:37:06 +0200 | [diff] [blame] | 1155 | if (test_bit(ilog2(IDE_AFLAG_FILEMARK), |
| 1156 | &drive->atapi_flags)) |
Tejun Heo | 07bd968 | 2009-04-19 08:46:03 +0900 | [diff] [blame] | 1157 | break; |
| 1158 | /* read */ |
| 1159 | if (idetape_queue_rw_tail(drive, REQ_IDETAPE_READ, |
| 1160 | tape->buffer_size) <= 0) |
| 1161 | break; |
| 1162 | } |
| 1163 | |
| 1164 | /* copy out */ |
| 1165 | todo = min_t(size_t, count - done, tape->valid); |
| 1166 | if (copy_to_user(buf + done, tape->cur, todo)) |
Daniel Walker | dcd9637 | 2006-06-25 05:47:37 -0700 | [diff] [blame] | 1167 | ret = -EFAULT; |
Tejun Heo | 07bd968 | 2009-04-19 08:46:03 +0900 | [diff] [blame] | 1168 | |
| 1169 | tape->cur += todo; |
| 1170 | tape->valid -= todo; |
| 1171 | done += todo; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1172 | } |
Tejun Heo | 07bd968 | 2009-04-19 08:46:03 +0900 | [diff] [blame] | 1173 | |
Borislav Petkov | 49d8078 | 2009-06-07 15:37:06 +0200 | [diff] [blame] | 1174 | if (!done && test_bit(ilog2(IDE_AFLAG_FILEMARK), &drive->atapi_flags)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1175 | idetape_space_over_filemarks(drive, MTFSF, 1); |
| 1176 | return 0; |
| 1177 | } |
Daniel Walker | dcd9637 | 2006-06-25 05:47:37 -0700 | [diff] [blame] | 1178 | |
Tejun Heo | 07bd968 | 2009-04-19 08:46:03 +0900 | [diff] [blame] | 1179 | return ret ? ret : done; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1180 | } |
| 1181 | |
Borislav Petkov | 5a04cfa | 2008-02-06 02:57:54 +0100 | [diff] [blame] | 1182 | static ssize_t idetape_chrdev_write(struct file *file, const char __user *buf, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1183 | size_t count, loff_t *ppos) |
| 1184 | { |
Borislav Petkov | 5aeddf9 | 2008-10-13 21:39:34 +0200 | [diff] [blame] | 1185 | struct ide_tape_obj *tape = file->private_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1186 | ide_drive_t *drive = tape->drive; |
Tejun Heo | 07bd968 | 2009-04-19 08:46:03 +0900 | [diff] [blame] | 1187 | size_t done = 0; |
Daniel Walker | dcd9637 | 2006-06-25 05:47:37 -0700 | [diff] [blame] | 1188 | ssize_t ret = 0; |
Tejun Heo | 88f1b94 | 2009-04-19 08:46:02 +0900 | [diff] [blame] | 1189 | int rc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1190 | |
| 1191 | /* The drive is write protected. */ |
| 1192 | if (tape->write_prot) |
| 1193 | return -EACCES; |
| 1194 | |
Borislav Petkov | e972d70 | 2009-07-17 23:55:16 +0000 | [diff] [blame] | 1195 | ide_debug_log(IDE_DBG_FUNC, "count %Zd", count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1196 | |
| 1197 | /* Initialize write operation */ |
Tejun Heo | 88f1b94 | 2009-04-19 08:46:02 +0900 | [diff] [blame] | 1198 | rc = idetape_init_rw(drive, IDETAPE_DIR_WRITE); |
| 1199 | if (rc < 0) |
| 1200 | return rc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1201 | |
Tejun Heo | 07bd968 | 2009-04-19 08:46:03 +0900 | [diff] [blame] | 1202 | while (done < count) { |
| 1203 | size_t todo; |
| 1204 | |
| 1205 | /* flush if staging buffer is full */ |
| 1206 | if (tape->valid == tape->buffer_size && |
| 1207 | idetape_queue_rw_tail(drive, REQ_IDETAPE_WRITE, |
| 1208 | tape->buffer_size) <= 0) |
| 1209 | return rc; |
| 1210 | |
| 1211 | /* copy in */ |
| 1212 | todo = min_t(size_t, count - done, |
| 1213 | tape->buffer_size - tape->valid); |
| 1214 | if (copy_from_user(tape->cur, buf + done, todo)) |
Daniel Walker | dcd9637 | 2006-06-25 05:47:37 -0700 | [diff] [blame] | 1215 | ret = -EFAULT; |
Tejun Heo | 07bd968 | 2009-04-19 08:46:03 +0900 | [diff] [blame] | 1216 | |
| 1217 | tape->cur += todo; |
| 1218 | tape->valid += todo; |
| 1219 | done += todo; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1220 | } |
Tejun Heo | 07bd968 | 2009-04-19 08:46:03 +0900 | [diff] [blame] | 1221 | |
| 1222 | return ret ? ret : done; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1223 | } |
| 1224 | |
Borislav Petkov | 5a04cfa | 2008-02-06 02:57:54 +0100 | [diff] [blame] | 1225 | static int idetape_write_filemark(ide_drive_t *drive) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1226 | { |
Bartlomiej Zolnierkiewicz | 2ac07d9 | 2008-10-10 22:39:38 +0200 | [diff] [blame] | 1227 | struct ide_tape_obj *tape = drive->driver_data; |
Borislav Petkov | d236d74 | 2008-04-18 00:46:27 +0200 | [diff] [blame] | 1228 | struct ide_atapi_pc pc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1229 | |
| 1230 | /* Write a filemark */ |
| 1231 | idetape_create_write_filemark_cmd(drive, &pc, 1); |
Borislav Petkov | b13345f | 2009-05-02 10:26:12 +0200 | [diff] [blame] | 1232 | if (ide_queue_pc_tail(drive, tape->disk, &pc, NULL, 0)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1233 | printk(KERN_ERR "ide-tape: Couldn't write a filemark\n"); |
| 1234 | return -EIO; |
| 1235 | } |
| 1236 | return 0; |
| 1237 | } |
| 1238 | |
| 1239 | /* |
Borislav Petkov | d99c9da | 2008-02-02 19:56:51 +0100 | [diff] [blame] | 1240 | * Called from idetape_chrdev_ioctl when the general mtio MTIOCTOP ioctl is |
| 1241 | * requested. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1242 | * |
Borislav Petkov | d99c9da | 2008-02-02 19:56:51 +0100 | [diff] [blame] | 1243 | * Note: MTBSF and MTBSFM are not supported when the tape doesn't support |
| 1244 | * spacing over filemarks in the reverse direction. In this case, MTFSFM is also |
Borislav Petkov | 5bd50dc | 2008-04-27 15:38:28 +0200 | [diff] [blame] | 1245 | * usually not supported. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1246 | * |
Borislav Petkov | d99c9da | 2008-02-02 19:56:51 +0100 | [diff] [blame] | 1247 | * The following commands are currently not supported: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1248 | * |
Borislav Petkov | d99c9da | 2008-02-02 19:56:51 +0100 | [diff] [blame] | 1249 | * MTFSS, MTBSS, MTWSM, MTSETDENSITY, MTSETDRVBUFFER, MT_ST_BOOLEANS, |
| 1250 | * MT_ST_WRITE_THRESHOLD. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1251 | */ |
Borislav Petkov | d99c9da | 2008-02-02 19:56:51 +0100 | [diff] [blame] | 1252 | static int idetape_mtioctop(ide_drive_t *drive, short mt_op, int mt_count) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1253 | { |
| 1254 | idetape_tape_t *tape = drive->driver_data; |
Bartlomiej Zolnierkiewicz | 2ac07d9 | 2008-10-10 22:39:38 +0200 | [diff] [blame] | 1255 | struct gendisk *disk = tape->disk; |
Borislav Petkov | d236d74 | 2008-04-18 00:46:27 +0200 | [diff] [blame] | 1256 | struct ide_atapi_pc pc; |
Borislav Petkov | 5a04cfa | 2008-02-06 02:57:54 +0100 | [diff] [blame] | 1257 | int i, retval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1258 | |
Borislav Petkov | e972d70 | 2009-07-17 23:55:16 +0000 | [diff] [blame] | 1259 | ide_debug_log(IDE_DBG_FUNC, "MTIOCTOP ioctl: mt_op: %d, mt_count: %d", |
| 1260 | mt_op, mt_count); |
Borislav Petkov | 5a04cfa | 2008-02-06 02:57:54 +0100 | [diff] [blame] | 1261 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1262 | switch (mt_op) { |
Borislav Petkov | 5a04cfa | 2008-02-06 02:57:54 +0100 | [diff] [blame] | 1263 | case MTFSF: |
| 1264 | case MTFSFM: |
| 1265 | case MTBSF: |
| 1266 | case MTBSFM: |
| 1267 | if (!mt_count) |
| 1268 | return 0; |
| 1269 | return idetape_space_over_filemarks(drive, mt_op, mt_count); |
| 1270 | default: |
| 1271 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1272 | } |
Borislav Petkov | 5a04cfa | 2008-02-06 02:57:54 +0100 | [diff] [blame] | 1273 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1274 | switch (mt_op) { |
Borislav Petkov | 5a04cfa | 2008-02-06 02:57:54 +0100 | [diff] [blame] | 1275 | case MTWEOF: |
| 1276 | if (tape->write_prot) |
| 1277 | return -EACCES; |
Borislav Petkov | ec0fdb0 | 2008-04-27 15:38:34 +0200 | [diff] [blame] | 1278 | ide_tape_discard_merge_buffer(drive, 1); |
Borislav Petkov | 5a04cfa | 2008-02-06 02:57:54 +0100 | [diff] [blame] | 1279 | for (i = 0; i < mt_count; i++) { |
| 1280 | retval = idetape_write_filemark(drive); |
| 1281 | if (retval) |
| 1282 | return retval; |
| 1283 | } |
| 1284 | return 0; |
| 1285 | case MTREW: |
Borislav Petkov | ec0fdb0 | 2008-04-27 15:38:34 +0200 | [diff] [blame] | 1286 | ide_tape_discard_merge_buffer(drive, 0); |
Borislav Petkov | 5a04cfa | 2008-02-06 02:57:54 +0100 | [diff] [blame] | 1287 | if (idetape_rewind_tape(drive)) |
| 1288 | return -EIO; |
| 1289 | return 0; |
| 1290 | case MTLOAD: |
Borislav Petkov | ec0fdb0 | 2008-04-27 15:38:34 +0200 | [diff] [blame] | 1291 | ide_tape_discard_merge_buffer(drive, 0); |
Bartlomiej Zolnierkiewicz | 0c8a6c7 | 2008-10-10 22:39:39 +0200 | [diff] [blame] | 1292 | return ide_do_start_stop(drive, disk, IDETAPE_LU_LOAD_MASK); |
Borislav Petkov | 5a04cfa | 2008-02-06 02:57:54 +0100 | [diff] [blame] | 1293 | case MTUNLOAD: |
| 1294 | case MTOFFL: |
| 1295 | /* |
| 1296 | * If door is locked, attempt to unlock before |
| 1297 | * attempting to eject. |
| 1298 | */ |
| 1299 | if (tape->door_locked) { |
Bartlomiej Zolnierkiewicz | 0578042 | 2008-10-10 22:39:38 +0200 | [diff] [blame] | 1300 | if (!ide_set_media_lock(drive, disk, 0)) |
Bartlomiej Zolnierkiewicz | 385a4b8 | 2008-10-10 22:39:37 +0200 | [diff] [blame] | 1301 | tape->door_locked = DOOR_UNLOCKED; |
Borislav Petkov | 5a04cfa | 2008-02-06 02:57:54 +0100 | [diff] [blame] | 1302 | } |
Borislav Petkov | ec0fdb0 | 2008-04-27 15:38:34 +0200 | [diff] [blame] | 1303 | ide_tape_discard_merge_buffer(drive, 0); |
Bartlomiej Zolnierkiewicz | 0c8a6c7 | 2008-10-10 22:39:39 +0200 | [diff] [blame] | 1304 | retval = ide_do_start_stop(drive, disk, !IDETAPE_LU_LOAD_MASK); |
Borislav Petkov | 5a04cfa | 2008-02-06 02:57:54 +0100 | [diff] [blame] | 1305 | if (!retval) |
Borislav Petkov | 49d8078 | 2009-06-07 15:37:06 +0200 | [diff] [blame] | 1306 | clear_bit(ilog2(IDE_AFLAG_MEDIUM_PRESENT), |
| 1307 | &drive->atapi_flags); |
Borislav Petkov | 5a04cfa | 2008-02-06 02:57:54 +0100 | [diff] [blame] | 1308 | return retval; |
| 1309 | case MTNOP: |
Borislav Petkov | ec0fdb0 | 2008-04-27 15:38:34 +0200 | [diff] [blame] | 1310 | ide_tape_discard_merge_buffer(drive, 0); |
Borislav Petkov | 5a04cfa | 2008-02-06 02:57:54 +0100 | [diff] [blame] | 1311 | return idetape_flush_tape_buffers(drive); |
| 1312 | case MTRETEN: |
Borislav Petkov | ec0fdb0 | 2008-04-27 15:38:34 +0200 | [diff] [blame] | 1313 | ide_tape_discard_merge_buffer(drive, 0); |
Bartlomiej Zolnierkiewicz | 0c8a6c7 | 2008-10-10 22:39:39 +0200 | [diff] [blame] | 1314 | return ide_do_start_stop(drive, disk, |
Borislav Petkov | 5a04cfa | 2008-02-06 02:57:54 +0100 | [diff] [blame] | 1315 | IDETAPE_LU_RETENSION_MASK | IDETAPE_LU_LOAD_MASK); |
Borislav Petkov | 5a04cfa | 2008-02-06 02:57:54 +0100 | [diff] [blame] | 1316 | case MTEOM: |
| 1317 | idetape_create_space_cmd(&pc, 0, IDETAPE_SPACE_TO_EOD); |
Borislav Petkov | b13345f | 2009-05-02 10:26:12 +0200 | [diff] [blame] | 1318 | return ide_queue_pc_tail(drive, disk, &pc, NULL, 0); |
Borislav Petkov | 5a04cfa | 2008-02-06 02:57:54 +0100 | [diff] [blame] | 1319 | case MTERASE: |
| 1320 | (void)idetape_rewind_tape(drive); |
| 1321 | idetape_create_erase_cmd(&pc); |
Borislav Petkov | b13345f | 2009-05-02 10:26:12 +0200 | [diff] [blame] | 1322 | return ide_queue_pc_tail(drive, disk, &pc, NULL, 0); |
Borislav Petkov | 5a04cfa | 2008-02-06 02:57:54 +0100 | [diff] [blame] | 1323 | case MTSETBLK: |
| 1324 | if (mt_count) { |
| 1325 | if (mt_count < tape->blk_size || |
| 1326 | mt_count % tape->blk_size) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1327 | return -EIO; |
Borislav Petkov | 5a04cfa | 2008-02-06 02:57:54 +0100 | [diff] [blame] | 1328 | tape->user_bs_factor = mt_count / tape->blk_size; |
Borislav Petkov | 49d8078 | 2009-06-07 15:37:06 +0200 | [diff] [blame] | 1329 | clear_bit(ilog2(IDE_AFLAG_DETECT_BS), |
| 1330 | &drive->atapi_flags); |
Borislav Petkov | 5a04cfa | 2008-02-06 02:57:54 +0100 | [diff] [blame] | 1331 | } else |
Borislav Petkov | 49d8078 | 2009-06-07 15:37:06 +0200 | [diff] [blame] | 1332 | set_bit(ilog2(IDE_AFLAG_DETECT_BS), |
| 1333 | &drive->atapi_flags); |
Borislav Petkov | 5a04cfa | 2008-02-06 02:57:54 +0100 | [diff] [blame] | 1334 | return 0; |
| 1335 | case MTSEEK: |
Borislav Petkov | ec0fdb0 | 2008-04-27 15:38:34 +0200 | [diff] [blame] | 1336 | ide_tape_discard_merge_buffer(drive, 0); |
Borislav Petkov | 5a04cfa | 2008-02-06 02:57:54 +0100 | [diff] [blame] | 1337 | return idetape_position_tape(drive, |
| 1338 | mt_count * tape->user_bs_factor, tape->partition, 0); |
| 1339 | case MTSETPART: |
Borislav Petkov | ec0fdb0 | 2008-04-27 15:38:34 +0200 | [diff] [blame] | 1340 | ide_tape_discard_merge_buffer(drive, 0); |
Borislav Petkov | 5a04cfa | 2008-02-06 02:57:54 +0100 | [diff] [blame] | 1341 | return idetape_position_tape(drive, 0, mt_count, 0); |
| 1342 | case MTFSR: |
| 1343 | case MTBSR: |
| 1344 | case MTLOCK: |
Bartlomiej Zolnierkiewicz | 0578042 | 2008-10-10 22:39:38 +0200 | [diff] [blame] | 1345 | retval = ide_set_media_lock(drive, disk, 1); |
Borislav Petkov | 5a04cfa | 2008-02-06 02:57:54 +0100 | [diff] [blame] | 1346 | if (retval) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1347 | return retval; |
Borislav Petkov | 5a04cfa | 2008-02-06 02:57:54 +0100 | [diff] [blame] | 1348 | tape->door_locked = DOOR_EXPLICITLY_LOCKED; |
| 1349 | return 0; |
| 1350 | case MTUNLOCK: |
Bartlomiej Zolnierkiewicz | 0578042 | 2008-10-10 22:39:38 +0200 | [diff] [blame] | 1351 | retval = ide_set_media_lock(drive, disk, 0); |
Borislav Petkov | 5a04cfa | 2008-02-06 02:57:54 +0100 | [diff] [blame] | 1352 | if (retval) |
| 1353 | return retval; |
| 1354 | tape->door_locked = DOOR_UNLOCKED; |
| 1355 | return 0; |
| 1356 | default: |
| 1357 | printk(KERN_ERR "ide-tape: MTIO operation %d not supported\n", |
| 1358 | mt_op); |
| 1359 | return -EIO; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1360 | } |
| 1361 | } |
| 1362 | |
| 1363 | /* |
Borislav Petkov | d99c9da | 2008-02-02 19:56:51 +0100 | [diff] [blame] | 1364 | * Our character device ioctls. General mtio.h magnetic io commands are |
| 1365 | * supported here, and not in the corresponding block interface. Our own |
| 1366 | * ide-tape ioctls are supported on both interfaces. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1367 | */ |
Borislav Petkov | d99c9da | 2008-02-02 19:56:51 +0100 | [diff] [blame] | 1368 | static int idetape_chrdev_ioctl(struct inode *inode, struct file *file, |
| 1369 | unsigned int cmd, unsigned long arg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1370 | { |
Borislav Petkov | 5aeddf9 | 2008-10-13 21:39:34 +0200 | [diff] [blame] | 1371 | struct ide_tape_obj *tape = file->private_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1372 | ide_drive_t *drive = tape->drive; |
| 1373 | struct mtop mtop; |
| 1374 | struct mtget mtget; |
| 1375 | struct mtpos mtpos; |
Borislav Petkov | 54bb207 | 2008-02-06 02:57:52 +0100 | [diff] [blame] | 1376 | int block_offset = 0, position = tape->first_frame; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1377 | void __user *argp = (void __user *)arg; |
| 1378 | |
Borislav Petkov | e972d70 | 2009-07-17 23:55:16 +0000 | [diff] [blame] | 1379 | ide_debug_log(IDE_DBG_FUNC, "cmd: 0x%x", cmd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1380 | |
Borislav Petkov | 54abf37 | 2008-02-06 02:57:52 +0100 | [diff] [blame] | 1381 | if (tape->chrdev_dir == IDETAPE_DIR_WRITE) { |
Borislav Petkov | d9df937 | 2008-04-27 15:38:34 +0200 | [diff] [blame] | 1382 | ide_tape_flush_merge_buffer(drive); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1383 | idetape_flush_tape_buffers(drive); |
| 1384 | } |
| 1385 | if (cmd == MTIOCGET || cmd == MTIOCPOS) { |
Tejun Heo | 963da55 | 2009-04-19 08:46:02 +0900 | [diff] [blame] | 1386 | block_offset = tape->valid / |
Borislav Petkov | 54bb207 | 2008-02-06 02:57:52 +0100 | [diff] [blame] | 1387 | (tape->blk_size * tape->user_bs_factor); |
Borislav Petkov | 55ce3a1 | 2009-05-04 09:38:15 +0200 | [diff] [blame] | 1388 | position = ide_tape_read_position(drive); |
Borislav Petkov | 5a04cfa | 2008-02-06 02:57:54 +0100 | [diff] [blame] | 1389 | if (position < 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1390 | return -EIO; |
| 1391 | } |
| 1392 | switch (cmd) { |
Borislav Petkov | 5a04cfa | 2008-02-06 02:57:54 +0100 | [diff] [blame] | 1393 | case MTIOCTOP: |
| 1394 | if (copy_from_user(&mtop, argp, sizeof(struct mtop))) |
| 1395 | return -EFAULT; |
| 1396 | return idetape_mtioctop(drive, mtop.mt_op, mtop.mt_count); |
| 1397 | case MTIOCGET: |
| 1398 | memset(&mtget, 0, sizeof(struct mtget)); |
| 1399 | mtget.mt_type = MT_ISSCSI2; |
| 1400 | mtget.mt_blkno = position / tape->user_bs_factor - block_offset; |
| 1401 | mtget.mt_dsreg = |
| 1402 | ((tape->blk_size * tape->user_bs_factor) |
| 1403 | << MT_ST_BLKSIZE_SHIFT) & MT_ST_BLKSIZE_MASK; |
Borislav Petkov | 54bb207 | 2008-02-06 02:57:52 +0100 | [diff] [blame] | 1404 | |
Borislav Petkov | 5a04cfa | 2008-02-06 02:57:54 +0100 | [diff] [blame] | 1405 | if (tape->drv_write_prot) |
| 1406 | mtget.mt_gstat |= GMT_WR_PROT(0xffffffff); |
| 1407 | |
| 1408 | if (copy_to_user(argp, &mtget, sizeof(struct mtget))) |
| 1409 | return -EFAULT; |
| 1410 | return 0; |
| 1411 | case MTIOCPOS: |
| 1412 | mtpos.mt_blkno = position / tape->user_bs_factor - block_offset; |
| 1413 | if (copy_to_user(argp, &mtpos, sizeof(struct mtpos))) |
| 1414 | return -EFAULT; |
| 1415 | return 0; |
| 1416 | default: |
| 1417 | if (tape->chrdev_dir == IDETAPE_DIR_READ) |
Borislav Petkov | ec0fdb0 | 2008-04-27 15:38:34 +0200 | [diff] [blame] | 1418 | ide_tape_discard_merge_buffer(drive, 1); |
Borislav Petkov | 5a04cfa | 2008-02-06 02:57:54 +0100 | [diff] [blame] | 1419 | return idetape_blkdev_ioctl(drive, cmd, arg); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1420 | } |
| 1421 | } |
| 1422 | |
Borislav Petkov | 3cffb9c | 2008-02-02 19:56:50 +0100 | [diff] [blame] | 1423 | /* |
| 1424 | * Do a mode sense page 0 with block descriptor and if it succeeds set the tape |
| 1425 | * block size with the reported value. |
| 1426 | */ |
| 1427 | static void ide_tape_get_bsize_from_bdesc(ide_drive_t *drive) |
| 1428 | { |
| 1429 | idetape_tape_t *tape = drive->driver_data; |
Borislav Petkov | d236d74 | 2008-04-18 00:46:27 +0200 | [diff] [blame] | 1430 | struct ide_atapi_pc pc; |
Borislav Petkov | 837272b | 2009-05-04 09:48:57 +0200 | [diff] [blame] | 1431 | u8 buf[12]; |
Borislav Petkov | 3cffb9c | 2008-02-02 19:56:50 +0100 | [diff] [blame] | 1432 | |
| 1433 | idetape_create_mode_sense_cmd(&pc, IDETAPE_BLOCK_DESCRIPTOR); |
Borislav Petkov | 837272b | 2009-05-04 09:48:57 +0200 | [diff] [blame] | 1434 | if (ide_queue_pc_tail(drive, tape->disk, &pc, buf, pc.req_xfer)) { |
Borislav Petkov | 3cffb9c | 2008-02-02 19:56:50 +0100 | [diff] [blame] | 1435 | printk(KERN_ERR "ide-tape: Can't get block descriptor\n"); |
Borislav Petkov | 54bb207 | 2008-02-06 02:57:52 +0100 | [diff] [blame] | 1436 | if (tape->blk_size == 0) { |
Borislav Petkov | 3cffb9c | 2008-02-02 19:56:50 +0100 | [diff] [blame] | 1437 | printk(KERN_WARNING "ide-tape: Cannot deal with zero " |
| 1438 | "block size, assuming 32k\n"); |
Borislav Petkov | 54bb207 | 2008-02-06 02:57:52 +0100 | [diff] [blame] | 1439 | tape->blk_size = 32768; |
Borislav Petkov | 3cffb9c | 2008-02-02 19:56:50 +0100 | [diff] [blame] | 1440 | } |
| 1441 | return; |
| 1442 | } |
Borislav Petkov | 837272b | 2009-05-04 09:48:57 +0200 | [diff] [blame] | 1443 | tape->blk_size = (buf[4 + 5] << 16) + |
| 1444 | (buf[4 + 6] << 8) + |
| 1445 | buf[4 + 7]; |
| 1446 | tape->drv_write_prot = (buf[2] & 0x80) >> 7; |
Borislav Petkov | e972d70 | 2009-07-17 23:55:16 +0000 | [diff] [blame] | 1447 | |
| 1448 | ide_debug_log(IDE_DBG_FUNC, "blk_size: %d, write_prot: %d", |
| 1449 | tape->blk_size, tape->drv_write_prot); |
Borislav Petkov | 3cffb9c | 2008-02-02 19:56:50 +0100 | [diff] [blame] | 1450 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1451 | |
Borislav Petkov | 5a04cfa | 2008-02-06 02:57:54 +0100 | [diff] [blame] | 1452 | static int idetape_chrdev_open(struct inode *inode, struct file *filp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1453 | { |
| 1454 | unsigned int minor = iminor(inode), i = minor & ~0xc0; |
| 1455 | ide_drive_t *drive; |
| 1456 | idetape_tape_t *tape; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1457 | int retval; |
| 1458 | |
Borislav Petkov | 8004a8c | 2008-02-06 02:57:51 +0100 | [diff] [blame] | 1459 | if (i >= MAX_HWIFS * MAX_DRIVES) |
| 1460 | return -ENXIO; |
| 1461 | |
Borislav Petkov | cbba2fa | 2009-10-11 00:25:19 +0000 | [diff] [blame] | 1462 | mutex_lock(&idetape_chrdev_mutex); |
| 1463 | |
Borislav Petkov | 9d01e4c | 2009-06-08 22:03:03 +0200 | [diff] [blame] | 1464 | tape = ide_tape_get(NULL, true, i); |
Jonathan Corbet | 04f4ac9 | 2008-05-15 12:01:56 -0600 | [diff] [blame] | 1465 | if (!tape) { |
Borislav Petkov | cbba2fa | 2009-10-11 00:25:19 +0000 | [diff] [blame] | 1466 | mutex_unlock(&idetape_chrdev_mutex); |
Borislav Petkov | 8004a8c | 2008-02-06 02:57:51 +0100 | [diff] [blame] | 1467 | return -ENXIO; |
Jonathan Corbet | 04f4ac9 | 2008-05-15 12:01:56 -0600 | [diff] [blame] | 1468 | } |
Borislav Petkov | 8004a8c | 2008-02-06 02:57:51 +0100 | [diff] [blame] | 1469 | |
Borislav Petkov | e972d70 | 2009-07-17 23:55:16 +0000 | [diff] [blame] | 1470 | drive = tape->drive; |
| 1471 | filp->private_data = tape; |
| 1472 | |
| 1473 | ide_debug_log(IDE_DBG_FUNC, "enter"); |
Borislav Petkov | 8004a8c | 2008-02-06 02:57:51 +0100 | [diff] [blame] | 1474 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1475 | /* |
| 1476 | * We really want to do nonseekable_open(inode, filp); here, but some |
| 1477 | * versions of tar incorrectly call lseek on tapes and bail out if that |
| 1478 | * fails. So we disallow pread() and pwrite(), but permit lseeks. |
| 1479 | */ |
| 1480 | filp->f_mode &= ~(FMODE_PREAD | FMODE_PWRITE); |
| 1481 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1482 | |
Borislav Petkov | 49d8078 | 2009-06-07 15:37:06 +0200 | [diff] [blame] | 1483 | if (test_and_set_bit(ilog2(IDE_AFLAG_BUSY), &drive->atapi_flags)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1484 | retval = -EBUSY; |
| 1485 | goto out_put_tape; |
| 1486 | } |
| 1487 | |
| 1488 | retval = idetape_wait_ready(drive, 60 * HZ); |
| 1489 | if (retval) { |
Borislav Petkov | 49d8078 | 2009-06-07 15:37:06 +0200 | [diff] [blame] | 1490 | clear_bit(ilog2(IDE_AFLAG_BUSY), &drive->atapi_flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1491 | printk(KERN_ERR "ide-tape: %s: drive not ready\n", tape->name); |
| 1492 | goto out_put_tape; |
| 1493 | } |
| 1494 | |
Borislav Petkov | 79ca743 | 2009-06-15 07:32:04 +0200 | [diff] [blame] | 1495 | ide_tape_read_position(drive); |
Borislav Petkov | 49d8078 | 2009-06-07 15:37:06 +0200 | [diff] [blame] | 1496 | if (!test_bit(ilog2(IDE_AFLAG_ADDRESS_VALID), &drive->atapi_flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1497 | (void)idetape_rewind_tape(drive); |
| 1498 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1499 | /* Read block size and write protect status from drive. */ |
Borislav Petkov | 3cffb9c | 2008-02-02 19:56:50 +0100 | [diff] [blame] | 1500 | ide_tape_get_bsize_from_bdesc(drive); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1501 | |
| 1502 | /* Set write protect flag if device is opened as read-only. */ |
| 1503 | if ((filp->f_flags & O_ACCMODE) == O_RDONLY) |
| 1504 | tape->write_prot = 1; |
| 1505 | else |
| 1506 | tape->write_prot = tape->drv_write_prot; |
| 1507 | |
| 1508 | /* Make sure drive isn't write protected if user wants to write. */ |
| 1509 | if (tape->write_prot) { |
| 1510 | if ((filp->f_flags & O_ACCMODE) == O_WRONLY || |
| 1511 | (filp->f_flags & O_ACCMODE) == O_RDWR) { |
Borislav Petkov | 49d8078 | 2009-06-07 15:37:06 +0200 | [diff] [blame] | 1512 | clear_bit(ilog2(IDE_AFLAG_BUSY), &drive->atapi_flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1513 | retval = -EROFS; |
| 1514 | goto out_put_tape; |
| 1515 | } |
| 1516 | } |
| 1517 | |
Borislav Petkov | 3c98bf3 | 2008-02-06 02:57:53 +0100 | [diff] [blame] | 1518 | /* Lock the tape drive door so user can't eject. */ |
Borislav Petkov | 54abf37 | 2008-02-06 02:57:52 +0100 | [diff] [blame] | 1519 | if (tape->chrdev_dir == IDETAPE_DIR_NONE) { |
Bartlomiej Zolnierkiewicz | 0578042 | 2008-10-10 22:39:38 +0200 | [diff] [blame] | 1520 | if (!ide_set_media_lock(drive, tape->disk, 1)) { |
Bartlomiej Zolnierkiewicz | 385a4b8 | 2008-10-10 22:39:37 +0200 | [diff] [blame] | 1521 | if (tape->door_locked != DOOR_EXPLICITLY_LOCKED) |
| 1522 | tape->door_locked = DOOR_LOCKED; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1523 | } |
| 1524 | } |
Borislav Petkov | cbba2fa | 2009-10-11 00:25:19 +0000 | [diff] [blame] | 1525 | mutex_unlock(&idetape_chrdev_mutex); |
| 1526 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1527 | return 0; |
| 1528 | |
| 1529 | out_put_tape: |
| 1530 | ide_tape_put(tape); |
Borislav Petkov | cbba2fa | 2009-10-11 00:25:19 +0000 | [diff] [blame] | 1531 | |
| 1532 | mutex_unlock(&idetape_chrdev_mutex); |
| 1533 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1534 | return retval; |
| 1535 | } |
| 1536 | |
Borislav Petkov | 5a04cfa | 2008-02-06 02:57:54 +0100 | [diff] [blame] | 1537 | static void idetape_write_release(ide_drive_t *drive, unsigned int minor) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1538 | { |
| 1539 | idetape_tape_t *tape = drive->driver_data; |
| 1540 | |
Borislav Petkov | d9df937 | 2008-04-27 15:38:34 +0200 | [diff] [blame] | 1541 | ide_tape_flush_merge_buffer(drive); |
Tejun Heo | 963da55 | 2009-04-19 08:46:02 +0900 | [diff] [blame] | 1542 | tape->buf = kmalloc(tape->buffer_size, GFP_KERNEL); |
| 1543 | if (tape->buf != NULL) { |
Borislav Petkov | 54bb207 | 2008-02-06 02:57:52 +0100 | [diff] [blame] | 1544 | idetape_pad_zeros(drive, tape->blk_size * |
| 1545 | (tape->user_bs_factor - 1)); |
Tejun Heo | 963da55 | 2009-04-19 08:46:02 +0900 | [diff] [blame] | 1546 | kfree(tape->buf); |
| 1547 | tape->buf = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1548 | } |
| 1549 | idetape_write_filemark(drive); |
| 1550 | idetape_flush_tape_buffers(drive); |
| 1551 | idetape_flush_tape_buffers(drive); |
| 1552 | } |
| 1553 | |
Borislav Petkov | 5a04cfa | 2008-02-06 02:57:54 +0100 | [diff] [blame] | 1554 | static int idetape_chrdev_release(struct inode *inode, struct file *filp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1555 | { |
Borislav Petkov | 5aeddf9 | 2008-10-13 21:39:34 +0200 | [diff] [blame] | 1556 | struct ide_tape_obj *tape = filp->private_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1557 | ide_drive_t *drive = tape->drive; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1558 | unsigned int minor = iminor(inode); |
| 1559 | |
Borislav Petkov | cbba2fa | 2009-10-11 00:25:19 +0000 | [diff] [blame] | 1560 | mutex_lock(&idetape_chrdev_mutex); |
| 1561 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1562 | tape = drive->driver_data; |
Borislav Petkov | 8004a8c | 2008-02-06 02:57:51 +0100 | [diff] [blame] | 1563 | |
Borislav Petkov | e972d70 | 2009-07-17 23:55:16 +0000 | [diff] [blame] | 1564 | ide_debug_log(IDE_DBG_FUNC, "enter"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1565 | |
Borislav Petkov | 54abf37 | 2008-02-06 02:57:52 +0100 | [diff] [blame] | 1566 | if (tape->chrdev_dir == IDETAPE_DIR_WRITE) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1567 | idetape_write_release(drive, minor); |
Borislav Petkov | 54abf37 | 2008-02-06 02:57:52 +0100 | [diff] [blame] | 1568 | if (tape->chrdev_dir == IDETAPE_DIR_READ) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1569 | if (minor < 128) |
Borislav Petkov | ec0fdb0 | 2008-04-27 15:38:34 +0200 | [diff] [blame] | 1570 | ide_tape_discard_merge_buffer(drive, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1571 | } |
Borislav Petkov | f64eee7 | 2008-04-27 15:38:25 +0200 | [diff] [blame] | 1572 | |
Borislav Petkov | 49d8078 | 2009-06-07 15:37:06 +0200 | [diff] [blame] | 1573 | if (minor < 128 && test_bit(ilog2(IDE_AFLAG_MEDIUM_PRESENT), |
| 1574 | &drive->atapi_flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1575 | (void) idetape_rewind_tape(drive); |
Borislav Petkov | 49d8078 | 2009-06-07 15:37:06 +0200 | [diff] [blame] | 1576 | |
Borislav Petkov | 54abf37 | 2008-02-06 02:57:52 +0100 | [diff] [blame] | 1577 | if (tape->chrdev_dir == IDETAPE_DIR_NONE) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1578 | if (tape->door_locked == DOOR_LOCKED) { |
Bartlomiej Zolnierkiewicz | 0578042 | 2008-10-10 22:39:38 +0200 | [diff] [blame] | 1579 | if (!ide_set_media_lock(drive, tape->disk, 0)) |
Bartlomiej Zolnierkiewicz | 385a4b8 | 2008-10-10 22:39:37 +0200 | [diff] [blame] | 1580 | tape->door_locked = DOOR_UNLOCKED; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1581 | } |
| 1582 | } |
Borislav Petkov | 49d8078 | 2009-06-07 15:37:06 +0200 | [diff] [blame] | 1583 | clear_bit(ilog2(IDE_AFLAG_BUSY), &drive->atapi_flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1584 | ide_tape_put(tape); |
Borislav Petkov | cbba2fa | 2009-10-11 00:25:19 +0000 | [diff] [blame] | 1585 | |
| 1586 | mutex_unlock(&idetape_chrdev_mutex); |
| 1587 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1588 | return 0; |
| 1589 | } |
| 1590 | |
Borislav Petkov | 6d29c8f | 2008-02-02 19:56:49 +0100 | [diff] [blame] | 1591 | static void idetape_get_inquiry_results(ide_drive_t *drive) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1592 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1593 | idetape_tape_t *tape = drive->driver_data; |
Borislav Petkov | d236d74 | 2008-04-18 00:46:27 +0200 | [diff] [blame] | 1594 | struct ide_atapi_pc pc; |
Bartlomiej Zolnierkiewicz | 41fa9f8 | 2009-03-31 20:15:25 +0200 | [diff] [blame] | 1595 | u8 pc_buf[256]; |
Borislav Petkov | 801bd32 | 2008-09-27 19:32:17 +0200 | [diff] [blame] | 1596 | char fw_rev[4], vendor_id[8], product_id[16]; |
Borislav Petkov | 6d29c8f | 2008-02-02 19:56:49 +0100 | [diff] [blame] | 1597 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1598 | idetape_create_inquiry_cmd(&pc); |
Borislav Petkov | b13345f | 2009-05-02 10:26:12 +0200 | [diff] [blame] | 1599 | if (ide_queue_pc_tail(drive, tape->disk, &pc, pc_buf, pc.req_xfer)) { |
Borislav Petkov | 6d29c8f | 2008-02-02 19:56:49 +0100 | [diff] [blame] | 1600 | printk(KERN_ERR "ide-tape: %s: can't get INQUIRY results\n", |
| 1601 | tape->name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1602 | return; |
| 1603 | } |
Borislav Petkov | b13345f | 2009-05-02 10:26:12 +0200 | [diff] [blame] | 1604 | memcpy(vendor_id, &pc_buf[8], 8); |
| 1605 | memcpy(product_id, &pc_buf[16], 16); |
| 1606 | memcpy(fw_rev, &pc_buf[32], 4); |
Borislav Petkov | 6d29c8f | 2008-02-02 19:56:49 +0100 | [diff] [blame] | 1607 | |
Borislav Petkov | 801bd32 | 2008-09-27 19:32:17 +0200 | [diff] [blame] | 1608 | ide_fixstring(vendor_id, 8, 0); |
| 1609 | ide_fixstring(product_id, 16, 0); |
| 1610 | ide_fixstring(fw_rev, 4, 0); |
Borislav Petkov | 41f81d5 | 2008-02-06 02:57:52 +0100 | [diff] [blame] | 1611 | |
Borislav Petkov | 801bd32 | 2008-09-27 19:32:17 +0200 | [diff] [blame] | 1612 | printk(KERN_INFO "ide-tape: %s <-> %s: %.8s %.16s rev %.4s\n", |
Borislav Petkov | 41f81d5 | 2008-02-06 02:57:52 +0100 | [diff] [blame] | 1613 | drive->name, tape->name, vendor_id, product_id, fw_rev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1614 | } |
| 1615 | |
| 1616 | /* |
Borislav Petkov | b642201 | 2008-02-02 19:56:49 +0100 | [diff] [blame] | 1617 | * Ask the tape about its various parameters. In particular, we will adjust our |
| 1618 | * data transfer buffer size to the recommended value as returned by the tape. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1619 | */ |
Borislav Petkov | 5a04cfa | 2008-02-06 02:57:54 +0100 | [diff] [blame] | 1620 | static void idetape_get_mode_sense_results(ide_drive_t *drive) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1621 | { |
| 1622 | idetape_tape_t *tape = drive->driver_data; |
Borislav Petkov | d236d74 | 2008-04-18 00:46:27 +0200 | [diff] [blame] | 1623 | struct ide_atapi_pc pc; |
Borislav Petkov | b13345f | 2009-05-02 10:26:12 +0200 | [diff] [blame] | 1624 | u8 buf[24], *caps; |
Borislav Petkov | b642201 | 2008-02-02 19:56:49 +0100 | [diff] [blame] | 1625 | u8 speed, max_speed; |
Borislav Petkov | 47314fa | 2008-02-02 19:56:48 +0100 | [diff] [blame] | 1626 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1627 | idetape_create_mode_sense_cmd(&pc, IDETAPE_CAPABILITIES_PAGE); |
Borislav Petkov | b13345f | 2009-05-02 10:26:12 +0200 | [diff] [blame] | 1628 | if (ide_queue_pc_tail(drive, tape->disk, &pc, buf, pc.req_xfer)) { |
Borislav Petkov | b642201 | 2008-02-02 19:56:49 +0100 | [diff] [blame] | 1629 | printk(KERN_ERR "ide-tape: Can't get tape parameters - assuming" |
| 1630 | " some default values\n"); |
Borislav Petkov | 54bb207 | 2008-02-06 02:57:52 +0100 | [diff] [blame] | 1631 | tape->blk_size = 512; |
Borislav Petkov | b642201 | 2008-02-02 19:56:49 +0100 | [diff] [blame] | 1632 | put_unaligned(52, (u16 *)&tape->caps[12]); |
| 1633 | put_unaligned(540, (u16 *)&tape->caps[14]); |
| 1634 | put_unaligned(6*52, (u16 *)&tape->caps[16]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1635 | return; |
| 1636 | } |
Borislav Petkov | b13345f | 2009-05-02 10:26:12 +0200 | [diff] [blame] | 1637 | caps = buf + 4 + buf[3]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1638 | |
Borislav Petkov | b642201 | 2008-02-02 19:56:49 +0100 | [diff] [blame] | 1639 | /* convert to host order and save for later use */ |
Harvey Harrison | cd740ab | 2008-07-24 22:53:33 +0200 | [diff] [blame] | 1640 | speed = be16_to_cpup((__be16 *)&caps[14]); |
| 1641 | max_speed = be16_to_cpup((__be16 *)&caps[8]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1642 | |
Harvey Harrison | cd740ab | 2008-07-24 22:53:33 +0200 | [diff] [blame] | 1643 | *(u16 *)&caps[8] = max_speed; |
| 1644 | *(u16 *)&caps[12] = be16_to_cpup((__be16 *)&caps[12]); |
| 1645 | *(u16 *)&caps[14] = speed; |
| 1646 | *(u16 *)&caps[16] = be16_to_cpup((__be16 *)&caps[16]); |
Borislav Petkov | b642201 | 2008-02-02 19:56:49 +0100 | [diff] [blame] | 1647 | |
| 1648 | if (!speed) { |
| 1649 | printk(KERN_INFO "ide-tape: %s: invalid tape speed " |
| 1650 | "(assuming 650KB/sec)\n", drive->name); |
Harvey Harrison | cd740ab | 2008-07-24 22:53:33 +0200 | [diff] [blame] | 1651 | *(u16 *)&caps[14] = 650; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1652 | } |
Borislav Petkov | b642201 | 2008-02-02 19:56:49 +0100 | [diff] [blame] | 1653 | if (!max_speed) { |
| 1654 | printk(KERN_INFO "ide-tape: %s: invalid max_speed " |
| 1655 | "(assuming 650KB/sec)\n", drive->name); |
Harvey Harrison | cd740ab | 2008-07-24 22:53:33 +0200 | [diff] [blame] | 1656 | *(u16 *)&caps[8] = 650; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1657 | } |
| 1658 | |
Borislav Petkov | b642201 | 2008-02-02 19:56:49 +0100 | [diff] [blame] | 1659 | memcpy(&tape->caps, caps, 20); |
Bartlomiej Zolnierkiewicz | 0578042 | 2008-10-10 22:39:38 +0200 | [diff] [blame] | 1660 | |
| 1661 | /* device lacks locking support according to capabilities page */ |
| 1662 | if ((caps[6] & 1) == 0) |
Bartlomiej Zolnierkiewicz | 42619d3 | 2008-10-17 18:09:11 +0200 | [diff] [blame] | 1663 | drive->dev_flags &= ~IDE_DFLAG_DOORLOCKING; |
Bartlomiej Zolnierkiewicz | 0578042 | 2008-10-10 22:39:38 +0200 | [diff] [blame] | 1664 | |
Borislav Petkov | b642201 | 2008-02-02 19:56:49 +0100 | [diff] [blame] | 1665 | if (caps[7] & 0x02) |
Borislav Petkov | 54bb207 | 2008-02-06 02:57:52 +0100 | [diff] [blame] | 1666 | tape->blk_size = 512; |
Borislav Petkov | b642201 | 2008-02-02 19:56:49 +0100 | [diff] [blame] | 1667 | else if (caps[7] & 0x04) |
Borislav Petkov | 54bb207 | 2008-02-06 02:57:52 +0100 | [diff] [blame] | 1668 | tape->blk_size = 1024; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1669 | } |
| 1670 | |
Bartlomiej Zolnierkiewicz | 7662d04 | 2007-05-10 00:01:10 +0200 | [diff] [blame] | 1671 | #ifdef CONFIG_IDE_PROC_FS |
Bartlomiej Zolnierkiewicz | 8185d5a | 2008-10-10 22:39:28 +0200 | [diff] [blame] | 1672 | #define ide_tape_devset_get(name, field) \ |
| 1673 | static int get_##name(ide_drive_t *drive) \ |
| 1674 | { \ |
| 1675 | idetape_tape_t *tape = drive->driver_data; \ |
| 1676 | return tape->field; \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1677 | } |
Bartlomiej Zolnierkiewicz | 8185d5a | 2008-10-10 22:39:28 +0200 | [diff] [blame] | 1678 | |
| 1679 | #define ide_tape_devset_set(name, field) \ |
| 1680 | static int set_##name(ide_drive_t *drive, int arg) \ |
| 1681 | { \ |
| 1682 | idetape_tape_t *tape = drive->driver_data; \ |
| 1683 | tape->field = arg; \ |
| 1684 | return 0; \ |
| 1685 | } |
| 1686 | |
Elias Oltmanns | 92f1f8f | 2008-10-10 22:39:40 +0200 | [diff] [blame] | 1687 | #define ide_tape_devset_rw_field(_name, _field) \ |
Bartlomiej Zolnierkiewicz | 8185d5a | 2008-10-10 22:39:28 +0200 | [diff] [blame] | 1688 | ide_tape_devset_get(_name, _field) \ |
| 1689 | ide_tape_devset_set(_name, _field) \ |
Elias Oltmanns | 92f1f8f | 2008-10-10 22:39:40 +0200 | [diff] [blame] | 1690 | IDE_DEVSET(_name, DS_SYNC, get_##_name, set_##_name) |
Bartlomiej Zolnierkiewicz | 8185d5a | 2008-10-10 22:39:28 +0200 | [diff] [blame] | 1691 | |
Elias Oltmanns | 92f1f8f | 2008-10-10 22:39:40 +0200 | [diff] [blame] | 1692 | #define ide_tape_devset_r_field(_name, _field) \ |
Bartlomiej Zolnierkiewicz | 8185d5a | 2008-10-10 22:39:28 +0200 | [diff] [blame] | 1693 | ide_tape_devset_get(_name, _field) \ |
Elias Oltmanns | 92f1f8f | 2008-10-10 22:39:40 +0200 | [diff] [blame] | 1694 | IDE_DEVSET(_name, 0, get_##_name, NULL) |
Bartlomiej Zolnierkiewicz | 8185d5a | 2008-10-10 22:39:28 +0200 | [diff] [blame] | 1695 | |
| 1696 | static int mulf_tdsc(ide_drive_t *drive) { return 1000; } |
| 1697 | static int divf_tdsc(ide_drive_t *drive) { return HZ; } |
| 1698 | static int divf_buffer(ide_drive_t *drive) { return 2; } |
| 1699 | static int divf_buffer_size(ide_drive_t *drive) { return 1024; } |
| 1700 | |
Bartlomiej Zolnierkiewicz | 97100fc | 2008-10-13 21:39:36 +0200 | [diff] [blame] | 1701 | ide_devset_rw_flag(dsc_overlap, IDE_DFLAG_DSC_OVERLAP); |
Bartlomiej Zolnierkiewicz | 8185d5a | 2008-10-10 22:39:28 +0200 | [diff] [blame] | 1702 | |
Elias Oltmanns | 92f1f8f | 2008-10-10 22:39:40 +0200 | [diff] [blame] | 1703 | ide_tape_devset_rw_field(tdsc, best_dsc_rw_freq); |
Bartlomiej Zolnierkiewicz | 8185d5a | 2008-10-10 22:39:28 +0200 | [diff] [blame] | 1704 | |
Elias Oltmanns | 92f1f8f | 2008-10-10 22:39:40 +0200 | [diff] [blame] | 1705 | ide_tape_devset_r_field(avg_speed, avg_speed); |
| 1706 | ide_tape_devset_r_field(speed, caps[14]); |
| 1707 | ide_tape_devset_r_field(buffer, caps[16]); |
| 1708 | ide_tape_devset_r_field(buffer_size, buffer_size); |
Bartlomiej Zolnierkiewicz | 8185d5a | 2008-10-10 22:39:28 +0200 | [diff] [blame] | 1709 | |
Elias Oltmanns | 92f1f8f | 2008-10-10 22:39:40 +0200 | [diff] [blame] | 1710 | static const struct ide_proc_devset idetape_settings[] = { |
| 1711 | __IDE_PROC_DEVSET(avg_speed, 0, 0xffff, NULL, NULL), |
| 1712 | __IDE_PROC_DEVSET(buffer, 0, 0xffff, NULL, divf_buffer), |
| 1713 | __IDE_PROC_DEVSET(buffer_size, 0, 0xffff, NULL, divf_buffer_size), |
Elias Oltmanns | 92f1f8f | 2008-10-10 22:39:40 +0200 | [diff] [blame] | 1714 | __IDE_PROC_DEVSET(dsc_overlap, 0, 1, NULL, NULL), |
| 1715 | __IDE_PROC_DEVSET(speed, 0, 0xffff, NULL, NULL), |
| 1716 | __IDE_PROC_DEVSET(tdsc, IDETAPE_DSC_RW_MIN, IDETAPE_DSC_RW_MAX, |
| 1717 | mulf_tdsc, divf_tdsc), |
Hannes Eder | 71bfc7a | 2009-03-05 16:10:56 +0100 | [diff] [blame] | 1718 | { NULL }, |
Bartlomiej Zolnierkiewicz | 8185d5a | 2008-10-10 22:39:28 +0200 | [diff] [blame] | 1719 | }; |
Bartlomiej Zolnierkiewicz | 7662d04 | 2007-05-10 00:01:10 +0200 | [diff] [blame] | 1720 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1721 | |
| 1722 | /* |
Borislav Petkov | 3c98bf3 | 2008-02-06 02:57:53 +0100 | [diff] [blame] | 1723 | * The function below is called to: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1724 | * |
Borislav Petkov | 3c98bf3 | 2008-02-06 02:57:53 +0100 | [diff] [blame] | 1725 | * 1. Initialize our various state variables. |
| 1726 | * 2. Ask the tape for its capabilities. |
| 1727 | * 3. Allocate a buffer which will be used for data transfer. The buffer size |
| 1728 | * is chosen based on the recommendation which we received in step 2. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1729 | * |
Borislav Petkov | 3c98bf3 | 2008-02-06 02:57:53 +0100 | [diff] [blame] | 1730 | * Note that at this point ide.c already assigned us an irq, so that we can |
| 1731 | * queue requests here and wait for their completion. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1732 | */ |
Borislav Petkov | 5a04cfa | 2008-02-06 02:57:54 +0100 | [diff] [blame] | 1733 | static void idetape_setup(ide_drive_t *drive, idetape_tape_t *tape, int minor) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1734 | { |
Borislav Petkov | 83042b2 | 2008-04-27 15:38:27 +0200 | [diff] [blame] | 1735 | unsigned long t; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1736 | int speed; |
Borislav Petkov | f73850a | 2008-04-27 15:38:33 +0200 | [diff] [blame] | 1737 | int buffer_size; |
Borislav Petkov | b642201 | 2008-02-02 19:56:49 +0100 | [diff] [blame] | 1738 | u16 *ctl = (u16 *)&tape->caps[12]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1739 | |
Borislav Petkov | e972d70 | 2009-07-17 23:55:16 +0000 | [diff] [blame] | 1740 | ide_debug_log(IDE_DBG_FUNC, "minor: %d", minor); |
| 1741 | |
| 1742 | drive->pc_callback = ide_tape_callback; |
Borislav Petkov | 776bb02 | 2008-07-23 19:55:59 +0200 | [diff] [blame] | 1743 | |
Bartlomiej Zolnierkiewicz | 97100fc | 2008-10-13 21:39:36 +0200 | [diff] [blame] | 1744 | drive->dev_flags |= IDE_DFLAG_DSC_OVERLAP; |
| 1745 | |
Bartlomiej Zolnierkiewicz | 4166c19 | 2008-02-01 23:09:30 +0100 | [diff] [blame] | 1746 | if (drive->hwif->host_flags & IDE_HFLAG_NO_DSC) { |
| 1747 | printk(KERN_INFO "ide-tape: %s: disabling DSC overlap\n", |
| 1748 | tape->name); |
Bartlomiej Zolnierkiewicz | 97100fc | 2008-10-13 21:39:36 +0200 | [diff] [blame] | 1749 | drive->dev_flags &= ~IDE_DFLAG_DSC_OVERLAP; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1750 | } |
Bartlomiej Zolnierkiewicz | 97100fc | 2008-10-13 21:39:36 +0200 | [diff] [blame] | 1751 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1752 | /* Seagate Travan drives do not support DSC overlap. */ |
Bartlomiej Zolnierkiewicz | 4dde449 | 2008-10-10 22:39:19 +0200 | [diff] [blame] | 1753 | if (strstr((char *)&drive->id[ATA_ID_PROD], "Seagate STT3401")) |
Bartlomiej Zolnierkiewicz | 97100fc | 2008-10-13 21:39:36 +0200 | [diff] [blame] | 1754 | drive->dev_flags &= ~IDE_DFLAG_DSC_OVERLAP; |
| 1755 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1756 | tape->minor = minor; |
| 1757 | tape->name[0] = 'h'; |
| 1758 | tape->name[1] = 't'; |
| 1759 | tape->name[2] = '0' + minor; |
Borislav Petkov | 54abf37 | 2008-02-06 02:57:52 +0100 | [diff] [blame] | 1760 | tape->chrdev_dir = IDETAPE_DIR_NONE; |
Bartlomiej Zolnierkiewicz | 4dde449 | 2008-10-10 22:39:19 +0200 | [diff] [blame] | 1761 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1762 | idetape_get_inquiry_results(drive); |
| 1763 | idetape_get_mode_sense_results(drive); |
Borislav Petkov | 3cffb9c | 2008-02-02 19:56:50 +0100 | [diff] [blame] | 1764 | ide_tape_get_bsize_from_bdesc(drive); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1765 | tape->user_bs_factor = 1; |
Borislav Petkov | f73850a | 2008-04-27 15:38:33 +0200 | [diff] [blame] | 1766 | tape->buffer_size = *ctl * tape->blk_size; |
| 1767 | while (tape->buffer_size > 0xffff) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1768 | printk(KERN_NOTICE "ide-tape: decreasing stage size\n"); |
Borislav Petkov | b642201 | 2008-02-02 19:56:49 +0100 | [diff] [blame] | 1769 | *ctl /= 2; |
Borislav Petkov | f73850a | 2008-04-27 15:38:33 +0200 | [diff] [blame] | 1770 | tape->buffer_size = *ctl * tape->blk_size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1771 | } |
Borislav Petkov | f73850a | 2008-04-27 15:38:33 +0200 | [diff] [blame] | 1772 | buffer_size = tape->buffer_size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1773 | |
Borislav Petkov | 83042b2 | 2008-04-27 15:38:27 +0200 | [diff] [blame] | 1774 | /* select the "best" DSC read/write polling freq */ |
Borislav Petkov | b642201 | 2008-02-02 19:56:49 +0100 | [diff] [blame] | 1775 | speed = max(*(u16 *)&tape->caps[14], *(u16 *)&tape->caps[8]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1776 | |
Borislav Petkov | f73850a | 2008-04-27 15:38:33 +0200 | [diff] [blame] | 1777 | t = (IDETAPE_FIFO_THRESHOLD * tape->buffer_size * HZ) / (speed * 1000); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1778 | |
| 1779 | /* |
Borislav Petkov | 3c98bf3 | 2008-02-06 02:57:53 +0100 | [diff] [blame] | 1780 | * Ensure that the number we got makes sense; limit it within |
| 1781 | * IDETAPE_DSC_RW_MIN and IDETAPE_DSC_RW_MAX. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1782 | */ |
Harvey Harrison | a792bd5 | 2008-07-15 21:21:41 +0200 | [diff] [blame] | 1783 | tape->best_dsc_rw_freq = clamp_t(unsigned long, t, IDETAPE_DSC_RW_MIN, |
| 1784 | IDETAPE_DSC_RW_MAX); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1785 | printk(KERN_INFO "ide-tape: %s <-> %s: %dKBps, %d*%dkB buffer, " |
Borislav Petkov | 83042b2 | 2008-04-27 15:38:27 +0200 | [diff] [blame] | 1786 | "%lums tDSC%s\n", |
Borislav Petkov | b642201 | 2008-02-02 19:56:49 +0100 | [diff] [blame] | 1787 | drive->name, tape->name, *(u16 *)&tape->caps[14], |
Borislav Petkov | f73850a | 2008-04-27 15:38:33 +0200 | [diff] [blame] | 1788 | (*(u16 *)&tape->caps[16] * 512) / tape->buffer_size, |
| 1789 | tape->buffer_size / 1024, |
Borislav Petkov | 54bb207 | 2008-02-06 02:57:52 +0100 | [diff] [blame] | 1790 | tape->best_dsc_rw_freq * 1000 / HZ, |
Bartlomiej Zolnierkiewicz | 97100fc | 2008-10-13 21:39:36 +0200 | [diff] [blame] | 1791 | (drive->dev_flags & IDE_DFLAG_USING_DMA) ? ", DMA" : ""); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1792 | |
Bartlomiej Zolnierkiewicz | 1e874f4 | 2008-10-10 22:39:27 +0200 | [diff] [blame] | 1793 | ide_proc_register_driver(drive, tape->driver); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1794 | } |
| 1795 | |
Russell King | 4031bbe | 2006-01-06 11:41:00 +0000 | [diff] [blame] | 1796 | static void ide_tape_remove(ide_drive_t *drive) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1797 | { |
| 1798 | idetape_tape_t *tape = drive->driver_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1799 | |
Bartlomiej Zolnierkiewicz | 7662d04 | 2007-05-10 00:01:10 +0200 | [diff] [blame] | 1800 | ide_proc_unregister_driver(drive, tape->driver); |
Bartlomiej Zolnierkiewicz | 8fed436 | 2009-02-25 20:28:24 +0100 | [diff] [blame] | 1801 | device_del(&tape->dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1802 | ide_unregister_region(tape->disk); |
| 1803 | |
Bartlomiej Zolnierkiewicz | 8fed436 | 2009-02-25 20:28:24 +0100 | [diff] [blame] | 1804 | mutex_lock(&idetape_ref_mutex); |
| 1805 | put_device(&tape->dev); |
| 1806 | mutex_unlock(&idetape_ref_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1807 | } |
| 1808 | |
Bartlomiej Zolnierkiewicz | 8fed436 | 2009-02-25 20:28:24 +0100 | [diff] [blame] | 1809 | static void ide_tape_release(struct device *dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1810 | { |
Bartlomiej Zolnierkiewicz | 8fed436 | 2009-02-25 20:28:24 +0100 | [diff] [blame] | 1811 | struct ide_tape_obj *tape = to_ide_drv(dev, ide_tape_obj); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1812 | ide_drive_t *drive = tape->drive; |
| 1813 | struct gendisk *g = tape->disk; |
| 1814 | |
Tejun Heo | 963da55 | 2009-04-19 08:46:02 +0900 | [diff] [blame] | 1815 | BUG_ON(tape->valid); |
Bartlomiej Zolnierkiewicz | 8604aff | 2005-05-26 14:55:34 +0200 | [diff] [blame] | 1816 | |
Bartlomiej Zolnierkiewicz | 97100fc | 2008-10-13 21:39:36 +0200 | [diff] [blame] | 1817 | drive->dev_flags &= ~IDE_DFLAG_DSC_OVERLAP; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1818 | drive->driver_data = NULL; |
Tony Jones | dbc1272 | 2007-09-25 02:03:03 +0200 | [diff] [blame] | 1819 | device_destroy(idetape_sysfs_class, MKDEV(IDETAPE_MAJOR, tape->minor)); |
Borislav Petkov | 5a04cfa | 2008-02-06 02:57:54 +0100 | [diff] [blame] | 1820 | device_destroy(idetape_sysfs_class, |
| 1821 | MKDEV(IDETAPE_MAJOR, tape->minor + 128)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1822 | idetape_devs[tape->minor] = NULL; |
| 1823 | g->private_data = NULL; |
| 1824 | put_disk(g); |
| 1825 | kfree(tape); |
| 1826 | } |
| 1827 | |
Bartlomiej Zolnierkiewicz | ecfd80e | 2007-05-10 00:01:09 +0200 | [diff] [blame] | 1828 | #ifdef CONFIG_IDE_PROC_FS |
Alexey Dobriyan | 6d703a8 | 2009-09-01 17:52:57 -0700 | [diff] [blame] | 1829 | static int idetape_name_proc_show(struct seq_file *m, void *v) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1830 | { |
Alexey Dobriyan | 6d703a8 | 2009-09-01 17:52:57 -0700 | [diff] [blame] | 1831 | ide_drive_t *drive = (ide_drive_t *) m->private; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1832 | idetape_tape_t *tape = drive->driver_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1833 | |
Alexey Dobriyan | 6d703a8 | 2009-09-01 17:52:57 -0700 | [diff] [blame] | 1834 | seq_printf(m, "%s\n", tape->name); |
| 1835 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1836 | } |
| 1837 | |
Alexey Dobriyan | 6d703a8 | 2009-09-01 17:52:57 -0700 | [diff] [blame] | 1838 | static int idetape_name_proc_open(struct inode *inode, struct file *file) |
| 1839 | { |
| 1840 | return single_open(file, idetape_name_proc_show, PDE(inode)->data); |
| 1841 | } |
| 1842 | |
| 1843 | static const struct file_operations idetape_name_proc_fops = { |
| 1844 | .owner = THIS_MODULE, |
| 1845 | .open = idetape_name_proc_open, |
| 1846 | .read = seq_read, |
| 1847 | .llseek = seq_lseek, |
| 1848 | .release = single_release, |
| 1849 | }; |
| 1850 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1851 | static ide_proc_entry_t idetape_proc[] = { |
Alexey Dobriyan | 6d703a8 | 2009-09-01 17:52:57 -0700 | [diff] [blame] | 1852 | { "capacity", S_IFREG|S_IRUGO, &ide_capacity_proc_fops }, |
| 1853 | { "name", S_IFREG|S_IRUGO, &idetape_name_proc_fops }, |
| 1854 | {} |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1855 | }; |
Bartlomiej Zolnierkiewicz | 79cb380 | 2008-10-17 18:09:13 +0200 | [diff] [blame] | 1856 | |
| 1857 | static ide_proc_entry_t *ide_tape_proc_entries(ide_drive_t *drive) |
| 1858 | { |
| 1859 | return idetape_proc; |
| 1860 | } |
| 1861 | |
| 1862 | static const struct ide_proc_devset *ide_tape_proc_devsets(ide_drive_t *drive) |
| 1863 | { |
| 1864 | return idetape_settings; |
| 1865 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1866 | #endif |
| 1867 | |
Russell King | 4031bbe | 2006-01-06 11:41:00 +0000 | [diff] [blame] | 1868 | static int ide_tape_probe(ide_drive_t *); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1869 | |
Bartlomiej Zolnierkiewicz | 7f3c868 | 2009-01-06 17:20:53 +0100 | [diff] [blame] | 1870 | static struct ide_driver idetape_driver = { |
Bartlomiej Zolnierkiewicz | 8604aff | 2005-05-26 14:55:34 +0200 | [diff] [blame] | 1871 | .gen_driver = { |
Laurent Riffard | 4ef3b8f | 2005-11-18 22:15:40 +0100 | [diff] [blame] | 1872 | .owner = THIS_MODULE, |
Bartlomiej Zolnierkiewicz | 8604aff | 2005-05-26 14:55:34 +0200 | [diff] [blame] | 1873 | .name = "ide-tape", |
| 1874 | .bus = &ide_bus_type, |
Bartlomiej Zolnierkiewicz | 8604aff | 2005-05-26 14:55:34 +0200 | [diff] [blame] | 1875 | }, |
Russell King | 4031bbe | 2006-01-06 11:41:00 +0000 | [diff] [blame] | 1876 | .probe = ide_tape_probe, |
| 1877 | .remove = ide_tape_remove, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1878 | .version = IDETAPE_VERSION, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1879 | .do_request = idetape_do_request, |
Bartlomiej Zolnierkiewicz | 7662d04 | 2007-05-10 00:01:10 +0200 | [diff] [blame] | 1880 | #ifdef CONFIG_IDE_PROC_FS |
Bartlomiej Zolnierkiewicz | 79cb380 | 2008-10-17 18:09:13 +0200 | [diff] [blame] | 1881 | .proc_entries = ide_tape_proc_entries, |
| 1882 | .proc_devsets = ide_tape_proc_devsets, |
Bartlomiej Zolnierkiewicz | 7662d04 | 2007-05-10 00:01:10 +0200 | [diff] [blame] | 1883 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1884 | }; |
| 1885 | |
Borislav Petkov | 3c98bf3 | 2008-02-06 02:57:53 +0100 | [diff] [blame] | 1886 | /* Our character device supporting functions, passed to register_chrdev. */ |
Arjan van de Ven | 2b8693c | 2007-02-12 00:55:32 -0800 | [diff] [blame] | 1887 | static const struct file_operations idetape_fops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1888 | .owner = THIS_MODULE, |
| 1889 | .read = idetape_chrdev_read, |
| 1890 | .write = idetape_chrdev_write, |
| 1891 | .ioctl = idetape_chrdev_ioctl, |
| 1892 | .open = idetape_chrdev_open, |
| 1893 | .release = idetape_chrdev_release, |
| 1894 | }; |
| 1895 | |
Al Viro | a4600f8 | 2008-03-02 10:27:58 -0500 | [diff] [blame] | 1896 | static int idetape_open(struct block_device *bdev, fmode_t mode) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1897 | { |
Borislav Petkov | 9d01e4c | 2009-06-08 22:03:03 +0200 | [diff] [blame] | 1898 | struct ide_tape_obj *tape = ide_tape_get(bdev->bd_disk, false, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1899 | |
Borislav Petkov | 5a04cfa | 2008-02-06 02:57:54 +0100 | [diff] [blame] | 1900 | if (!tape) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1901 | return -ENXIO; |
| 1902 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1903 | return 0; |
| 1904 | } |
| 1905 | |
Al Viro | a4600f8 | 2008-03-02 10:27:58 -0500 | [diff] [blame] | 1906 | static int idetape_release(struct gendisk *disk, fmode_t mode) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1907 | { |
Borislav Petkov | 5aeddf9 | 2008-10-13 21:39:34 +0200 | [diff] [blame] | 1908 | struct ide_tape_obj *tape = ide_drv_g(disk, ide_tape_obj); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1909 | |
| 1910 | ide_tape_put(tape); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1911 | return 0; |
| 1912 | } |
| 1913 | |
Al Viro | a4600f8 | 2008-03-02 10:27:58 -0500 | [diff] [blame] | 1914 | static int idetape_ioctl(struct block_device *bdev, fmode_t mode, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1915 | unsigned int cmd, unsigned long arg) |
| 1916 | { |
Borislav Petkov | 5aeddf9 | 2008-10-13 21:39:34 +0200 | [diff] [blame] | 1917 | struct ide_tape_obj *tape = ide_drv_g(bdev->bd_disk, ide_tape_obj); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1918 | ide_drive_t *drive = tape->drive; |
Al Viro | 1bddd9e | 2008-09-02 17:19:43 -0400 | [diff] [blame] | 1919 | int err = generic_ide_ioctl(drive, bdev, cmd, arg); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1920 | if (err == -EINVAL) |
| 1921 | err = idetape_blkdev_ioctl(drive, cmd, arg); |
| 1922 | return err; |
| 1923 | } |
| 1924 | |
Alexey Dobriyan | 83d5cde | 2009-09-21 17:01:13 -0700 | [diff] [blame] | 1925 | static const struct block_device_operations idetape_block_ops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1926 | .owner = THIS_MODULE, |
Al Viro | a4600f8 | 2008-03-02 10:27:58 -0500 | [diff] [blame] | 1927 | .open = idetape_open, |
| 1928 | .release = idetape_release, |
| 1929 | .locked_ioctl = idetape_ioctl, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1930 | }; |
| 1931 | |
Russell King | 4031bbe | 2006-01-06 11:41:00 +0000 | [diff] [blame] | 1932 | static int ide_tape_probe(ide_drive_t *drive) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1933 | { |
| 1934 | idetape_tape_t *tape; |
| 1935 | struct gendisk *g; |
| 1936 | int minor; |
| 1937 | |
Borislav Petkov | e972d70 | 2009-07-17 23:55:16 +0000 | [diff] [blame] | 1938 | ide_debug_log(IDE_DBG_FUNC, "enter"); |
| 1939 | |
| 1940 | if (!strstr(DRV_NAME, drive->driver_req)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1941 | goto failed; |
Bartlomiej Zolnierkiewicz | 2a92466 | 2008-10-10 22:39:24 +0200 | [diff] [blame] | 1942 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1943 | if (drive->media != ide_tape) |
| 1944 | goto failed; |
Bartlomiej Zolnierkiewicz | 2a92466 | 2008-10-10 22:39:24 +0200 | [diff] [blame] | 1945 | |
Bartlomiej Zolnierkiewicz | 97100fc | 2008-10-13 21:39:36 +0200 | [diff] [blame] | 1946 | if ((drive->dev_flags & IDE_DFLAG_ID_READ) && |
| 1947 | ide_check_atapi_device(drive, DRV_NAME) == 0) { |
Borislav Petkov | 5a04cfa | 2008-02-06 02:57:54 +0100 | [diff] [blame] | 1948 | printk(KERN_ERR "ide-tape: %s: not supported by this version of" |
| 1949 | " the driver\n", drive->name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1950 | goto failed; |
| 1951 | } |
Borislav Petkov | 5a04cfa | 2008-02-06 02:57:54 +0100 | [diff] [blame] | 1952 | tape = kzalloc(sizeof(idetape_tape_t), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1953 | if (tape == NULL) { |
Borislav Petkov | 5a04cfa | 2008-02-06 02:57:54 +0100 | [diff] [blame] | 1954 | printk(KERN_ERR "ide-tape: %s: Can't allocate a tape struct\n", |
| 1955 | drive->name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1956 | goto failed; |
| 1957 | } |
| 1958 | |
| 1959 | g = alloc_disk(1 << PARTN_BITS); |
| 1960 | if (!g) |
| 1961 | goto out_free_tape; |
| 1962 | |
| 1963 | ide_init_disk(g, drive); |
| 1964 | |
Bartlomiej Zolnierkiewicz | 8fed436 | 2009-02-25 20:28:24 +0100 | [diff] [blame] | 1965 | tape->dev.parent = &drive->gendev; |
| 1966 | tape->dev.release = ide_tape_release; |
| 1967 | dev_set_name(&tape->dev, dev_name(&drive->gendev)); |
| 1968 | |
| 1969 | if (device_register(&tape->dev)) |
| 1970 | goto out_free_disk; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1971 | |
| 1972 | tape->drive = drive; |
| 1973 | tape->driver = &idetape_driver; |
| 1974 | tape->disk = g; |
| 1975 | |
| 1976 | g->private_data = &tape->driver; |
| 1977 | |
| 1978 | drive->driver_data = tape; |
| 1979 | |
Arjan van de Ven | cf8b897 | 2006-03-23 03:00:45 -0800 | [diff] [blame] | 1980 | mutex_lock(&idetape_ref_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1981 | for (minor = 0; idetape_devs[minor]; minor++) |
| 1982 | ; |
| 1983 | idetape_devs[minor] = tape; |
Arjan van de Ven | cf8b897 | 2006-03-23 03:00:45 -0800 | [diff] [blame] | 1984 | mutex_unlock(&idetape_ref_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1985 | |
| 1986 | idetape_setup(drive, tape, minor); |
| 1987 | |
Greg Kroah-Hartman | 3ee074b | 2008-07-21 20:03:34 -0700 | [diff] [blame] | 1988 | device_create(idetape_sysfs_class, &drive->gendev, |
| 1989 | MKDEV(IDETAPE_MAJOR, minor), NULL, "%s", tape->name); |
| 1990 | device_create(idetape_sysfs_class, &drive->gendev, |
| 1991 | MKDEV(IDETAPE_MAJOR, minor + 128), NULL, |
| 1992 | "n%s", tape->name); |
Will Dyson | d5dee80 | 2005-09-16 02:55:07 -0700 | [diff] [blame] | 1993 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1994 | g->fops = &idetape_block_ops; |
| 1995 | ide_register_region(g); |
| 1996 | |
| 1997 | return 0; |
Bartlomiej Zolnierkiewicz | 8604aff | 2005-05-26 14:55:34 +0200 | [diff] [blame] | 1998 | |
Bartlomiej Zolnierkiewicz | 8fed436 | 2009-02-25 20:28:24 +0100 | [diff] [blame] | 1999 | out_free_disk: |
| 2000 | put_disk(g); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2001 | out_free_tape: |
| 2002 | kfree(tape); |
| 2003 | failed: |
Bartlomiej Zolnierkiewicz | 8604aff | 2005-05-26 14:55:34 +0200 | [diff] [blame] | 2004 | return -ENODEV; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2005 | } |
| 2006 | |
Borislav Petkov | 5a04cfa | 2008-02-06 02:57:54 +0100 | [diff] [blame] | 2007 | static void __exit idetape_exit(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2008 | { |
Bartlomiej Zolnierkiewicz | 8604aff | 2005-05-26 14:55:34 +0200 | [diff] [blame] | 2009 | driver_unregister(&idetape_driver.gen_driver); |
Will Dyson | d5dee80 | 2005-09-16 02:55:07 -0700 | [diff] [blame] | 2010 | class_destroy(idetape_sysfs_class); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2011 | unregister_chrdev(IDETAPE_MAJOR, "ht"); |
| 2012 | } |
| 2013 | |
Bartlomiej Zolnierkiewicz | 17514e8 | 2005-11-19 22:24:35 +0100 | [diff] [blame] | 2014 | static int __init idetape_init(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2015 | { |
Will Dyson | d5dee80 | 2005-09-16 02:55:07 -0700 | [diff] [blame] | 2016 | int error = 1; |
| 2017 | idetape_sysfs_class = class_create(THIS_MODULE, "ide_tape"); |
| 2018 | if (IS_ERR(idetape_sysfs_class)) { |
| 2019 | idetape_sysfs_class = NULL; |
| 2020 | printk(KERN_ERR "Unable to create sysfs class for ide tapes\n"); |
| 2021 | error = -EBUSY; |
| 2022 | goto out; |
| 2023 | } |
| 2024 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2025 | if (register_chrdev(IDETAPE_MAJOR, "ht", &idetape_fops)) { |
Borislav Petkov | 5a04cfa | 2008-02-06 02:57:54 +0100 | [diff] [blame] | 2026 | printk(KERN_ERR "ide-tape: Failed to register chrdev" |
| 2027 | " interface\n"); |
Will Dyson | d5dee80 | 2005-09-16 02:55:07 -0700 | [diff] [blame] | 2028 | error = -EBUSY; |
| 2029 | goto out_free_class; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2030 | } |
Will Dyson | d5dee80 | 2005-09-16 02:55:07 -0700 | [diff] [blame] | 2031 | |
| 2032 | error = driver_register(&idetape_driver.gen_driver); |
| 2033 | if (error) |
| 2034 | goto out_free_driver; |
| 2035 | |
| 2036 | return 0; |
| 2037 | |
| 2038 | out_free_driver: |
| 2039 | driver_unregister(&idetape_driver.gen_driver); |
| 2040 | out_free_class: |
| 2041 | class_destroy(idetape_sysfs_class); |
| 2042 | out: |
| 2043 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2044 | } |
| 2045 | |
Kay Sievers | 263756e | 2005-12-12 18:03:44 +0100 | [diff] [blame] | 2046 | MODULE_ALIAS("ide:*m-tape*"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2047 | module_init(idetape_init); |
| 2048 | module_exit(idetape_exit); |
| 2049 | MODULE_ALIAS_CHARDEV_MAJOR(IDETAPE_MAJOR); |
Borislav Petkov | 9c14576 | 2008-02-06 02:57:54 +0100 | [diff] [blame] | 2050 | MODULE_DESCRIPTION("ATAPI Streaming TAPE Driver"); |
| 2051 | MODULE_LICENSE("GPL"); |