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 | dfe79936 | 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> |
| 34 | #include <linux/slab.h> |
| 35 | #include <linux/pci.h> |
| 36 | #include <linux/ide.h> |
| 37 | #include <linux/smp_lock.h> |
| 38 | #include <linux/completion.h> |
| 39 | #include <linux/bitops.h> |
Arjan van de Ven | cf8b897 | 2006-03-23 03:00:45 -0800 | [diff] [blame] | 40 | #include <linux/mutex.h> |
Borislav Petkov | 90699ce | 2008-02-02 19:56:50 +0100 | [diff] [blame] | 41 | #include <scsi/scsi.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 42 | |
| 43 | #include <asm/byteorder.h> |
Borislav Petkov | c837cfa | 2008-02-06 02:57:54 +0100 | [diff] [blame] | 44 | #include <linux/irq.h> |
| 45 | #include <linux/uaccess.h> |
| 46 | #include <linux/io.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 47 | #include <asm/unaligned.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 48 | #include <linux/mtio.h> |
| 49 | |
Borislav Petkov | 8004a8c | 2008-02-06 02:57:51 +0100 | [diff] [blame] | 50 | enum { |
| 51 | /* output errors only */ |
| 52 | DBG_ERR = (1 << 0), |
| 53 | /* output all sense key/asc */ |
| 54 | DBG_SENSE = (1 << 1), |
| 55 | /* info regarding all chrdev-related procedures */ |
| 56 | DBG_CHRDEV = (1 << 2), |
| 57 | /* all remaining procedures */ |
| 58 | DBG_PROCS = (1 << 3), |
Bartlomiej Zolnierkiewicz | 394a4c2 | 2008-10-10 22:39:35 +0200 | [diff] [blame^] | 59 | /* buffer alloc info (pc_stack) */ |
| 60 | DBG_PC_STACK = (1 << 4), |
Borislav Petkov | 8004a8c | 2008-02-06 02:57:51 +0100 | [diff] [blame] | 61 | }; |
| 62 | |
| 63 | /* define to see debug info */ |
| 64 | #define IDETAPE_DEBUG_LOG 0 |
| 65 | |
| 66 | #if IDETAPE_DEBUG_LOG |
| 67 | #define debug_log(lvl, fmt, args...) \ |
| 68 | { \ |
| 69 | if (tape->debug_mask & lvl) \ |
| 70 | printk(KERN_INFO "ide-tape: " fmt, ## args); \ |
| 71 | } |
| 72 | #else |
| 73 | #define debug_log(lvl, fmt, args...) do {} while (0) |
| 74 | #endif |
| 75 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 76 | /**************************** Tunable parameters *****************************/ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 77 | /* |
Borislav Petkov | 3c98bf3 | 2008-02-06 02:57:53 +0100 | [diff] [blame] | 78 | * After each failed packet command we issue a request sense command and retry |
| 79 | * the packet command IDETAPE_MAX_PC_RETRIES times. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 80 | * |
Borislav Petkov | 3c98bf3 | 2008-02-06 02:57:53 +0100 | [diff] [blame] | 81 | * Setting IDETAPE_MAX_PC_RETRIES to 0 will disable retries. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 82 | */ |
| 83 | #define IDETAPE_MAX_PC_RETRIES 3 |
| 84 | |
| 85 | /* |
Borislav Petkov | 3c98bf3 | 2008-02-06 02:57:53 +0100 | [diff] [blame] | 86 | * With each packet command, we allocate a buffer of IDETAPE_PC_BUFFER_SIZE |
| 87 | * bytes. This is used for several packet commands (Not for READ/WRITE commands) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 88 | */ |
| 89 | #define IDETAPE_PC_BUFFER_SIZE 256 |
| 90 | |
| 91 | /* |
Bartlomiej Zolnierkiewicz | 394a4c2 | 2008-10-10 22:39:35 +0200 | [diff] [blame^] | 92 | * In various places in the driver, we need to allocate storage for packet |
| 93 | * commands, which will remain valid while we leave the driver to wait for |
| 94 | * an interrupt or a timeout event. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 95 | */ |
| 96 | #define IDETAPE_PC_STACK (10 + IDETAPE_MAX_PC_RETRIES) |
| 97 | |
| 98 | /* |
| 99 | * Some drives (for example, Seagate STT3401A Travan) require a very long |
| 100 | * timeout, because they don't return an interrupt or clear their busy bit |
| 101 | * until after the command completes (even retension commands). |
| 102 | */ |
| 103 | #define IDETAPE_WAIT_CMD (900*HZ) |
| 104 | |
| 105 | /* |
Borislav Petkov | 3c98bf3 | 2008-02-06 02:57:53 +0100 | [diff] [blame] | 106 | * The following parameter is used to select the point in the internal tape fifo |
| 107 | * in which we will start to refill the buffer. Decreasing the following |
| 108 | * parameter will improve the system's latency and interactive response, while |
| 109 | * using a high value might improve system throughput. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 110 | */ |
Borislav Petkov | 3c98bf3 | 2008-02-06 02:57:53 +0100 | [diff] [blame] | 111 | #define IDETAPE_FIFO_THRESHOLD 2 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 112 | |
| 113 | /* |
Borislav Petkov | 3c98bf3 | 2008-02-06 02:57:53 +0100 | [diff] [blame] | 114 | * DSC polling parameters. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 115 | * |
Borislav Petkov | 3c98bf3 | 2008-02-06 02:57:53 +0100 | [diff] [blame] | 116 | * Polling for DSC (a single bit in the status register) is a very important |
| 117 | * 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] | 118 | * |
Borislav Petkov | 3c98bf3 | 2008-02-06 02:57:53 +0100 | [diff] [blame] | 119 | * 1. Before a read/write packet command, to ensure that we can transfer data |
| 120 | * from/to the tape's data buffers, without causing an actual media access. |
| 121 | * In case the tape is not ready yet, we take out our request from the device |
| 122 | * request queue, so that ide.c could service requests from the other device |
| 123 | * on the same interface in the meantime. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 124 | * |
Borislav Petkov | 3c98bf3 | 2008-02-06 02:57:53 +0100 | [diff] [blame] | 125 | * 2. After the successful initialization of a "media access packet command", |
| 126 | * which is a command that can take a long time to complete (the interval can |
| 127 | * range from several seconds to even an hour). Again, we postpone our request |
| 128 | * in the middle to free the bus for the other device. The polling frequency |
| 129 | * here should be lower than the read/write frequency since those media access |
| 130 | * commands are slow. We start from a "fast" frequency - IDETAPE_DSC_MA_FAST |
| 131 | * (1 second), and if we don't receive DSC after IDETAPE_DSC_MA_THRESHOLD |
| 132 | * (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] | 133 | * |
Borislav Petkov | 3c98bf3 | 2008-02-06 02:57:53 +0100 | [diff] [blame] | 134 | * We also set a timeout for the timer, in case something goes wrong. The |
| 135 | * 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] | 136 | */ |
Borislav Petkov | 3c98bf3 | 2008-02-06 02:57:53 +0100 | [diff] [blame] | 137 | |
| 138 | /* DSC timings. */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 139 | #define IDETAPE_DSC_RW_MIN 5*HZ/100 /* 50 msec */ |
| 140 | #define IDETAPE_DSC_RW_MAX 40*HZ/100 /* 400 msec */ |
| 141 | #define IDETAPE_DSC_RW_TIMEOUT 2*60*HZ /* 2 minutes */ |
| 142 | #define IDETAPE_DSC_MA_FAST 2*HZ /* 2 seconds */ |
| 143 | #define IDETAPE_DSC_MA_THRESHOLD 5*60*HZ /* 5 minutes */ |
| 144 | #define IDETAPE_DSC_MA_SLOW 30*HZ /* 30 seconds */ |
| 145 | #define IDETAPE_DSC_MA_TIMEOUT 2*60*60*HZ /* 2 hours */ |
| 146 | |
| 147 | /*************************** End of tunable parameters ***********************/ |
| 148 | |
Borislav Petkov | 54abf37 | 2008-02-06 02:57:52 +0100 | [diff] [blame] | 149 | /* tape directions */ |
| 150 | enum { |
| 151 | IDETAPE_DIR_NONE = (1 << 0), |
| 152 | IDETAPE_DIR_READ = (1 << 1), |
| 153 | IDETAPE_DIR_WRITE = (1 << 2), |
| 154 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 155 | |
| 156 | struct idetape_bh { |
Stephen Rothwell | ab05796 | 2007-08-01 23:46:44 +0200 | [diff] [blame] | 157 | u32 b_size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 158 | atomic_t b_count; |
| 159 | struct idetape_bh *b_reqnext; |
| 160 | char *b_data; |
| 161 | }; |
| 162 | |
Borislav Petkov | 03056b9 | 2008-04-18 00:46:26 +0200 | [diff] [blame] | 163 | /* Tape door status */ |
| 164 | #define DOOR_UNLOCKED 0 |
| 165 | #define DOOR_LOCKED 1 |
| 166 | #define DOOR_EXPLICITLY_LOCKED 2 |
| 167 | |
| 168 | /* Some defines for the SPACE command */ |
| 169 | #define IDETAPE_SPACE_OVER_FILEMARK 1 |
| 170 | #define IDETAPE_SPACE_TO_EOD 3 |
| 171 | |
| 172 | /* Some defines for the LOAD UNLOAD command */ |
| 173 | #define IDETAPE_LU_LOAD_MASK 1 |
| 174 | #define IDETAPE_LU_RETENSION_MASK 2 |
| 175 | #define IDETAPE_LU_EOT_MASK 4 |
| 176 | |
| 177 | /* |
| 178 | * Special requests for our block device strategy routine. |
| 179 | * |
| 180 | * In order to service a character device command, we add special requests to |
| 181 | * the tail of our block device request queue and wait for their completion. |
| 182 | */ |
| 183 | |
| 184 | enum { |
| 185 | REQ_IDETAPE_PC1 = (1 << 0), /* packet command (first stage) */ |
| 186 | REQ_IDETAPE_PC2 = (1 << 1), /* packet command (second stage) */ |
| 187 | REQ_IDETAPE_READ = (1 << 2), |
| 188 | REQ_IDETAPE_WRITE = (1 << 3), |
| 189 | }; |
| 190 | |
| 191 | /* Error codes returned in rq->errors to the higher part of the driver. */ |
| 192 | #define IDETAPE_ERROR_GENERAL 101 |
| 193 | #define IDETAPE_ERROR_FILEMARK 102 |
| 194 | #define IDETAPE_ERROR_EOD 103 |
| 195 | |
| 196 | /* Structures related to the SELECT SENSE / MODE SENSE packet commands. */ |
| 197 | #define IDETAPE_BLOCK_DESCRIPTOR 0 |
| 198 | #define IDETAPE_CAPABILITIES_PAGE 0x2a |
| 199 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 200 | /* |
Borislav Petkov | 3c98bf3 | 2008-02-06 02:57:53 +0100 | [diff] [blame] | 201 | * Most of our global data which we need to save even as we leave the driver due |
| 202 | * 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] | 203 | */ |
| 204 | typedef struct ide_tape_obj { |
| 205 | ide_drive_t *drive; |
| 206 | ide_driver_t *driver; |
| 207 | struct gendisk *disk; |
| 208 | struct kref kref; |
| 209 | |
| 210 | /* |
| 211 | * Since a typical character device operation requires more |
| 212 | * than one packet command, we provide here enough memory |
| 213 | * for the maximum of interconnected packet commands. |
| 214 | * The packet commands are stored in the circular array pc_stack. |
| 215 | * pc_stack_index points to the last used entry, and warps around |
| 216 | * to the start when we get to the last array entry. |
| 217 | * |
| 218 | * pc points to the current processed packet command. |
| 219 | * |
| 220 | * failed_pc points to the last failed packet command, or contains |
| 221 | * NULL if we do not need to retry any packet command. This is |
| 222 | * required since an additional packet command is needed before the |
| 223 | * retry, to get detailed information on what went wrong. |
| 224 | */ |
| 225 | /* Current packet command */ |
Borislav Petkov | d236d74 | 2008-04-18 00:46:27 +0200 | [diff] [blame] | 226 | struct ide_atapi_pc *pc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 227 | /* Last failed packet command */ |
Borislav Petkov | d236d74 | 2008-04-18 00:46:27 +0200 | [diff] [blame] | 228 | struct ide_atapi_pc *failed_pc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 229 | /* Packet command stack */ |
Borislav Petkov | d236d74 | 2008-04-18 00:46:27 +0200 | [diff] [blame] | 230 | struct ide_atapi_pc pc_stack[IDETAPE_PC_STACK]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 231 | /* Next free packet command storage space */ |
| 232 | int pc_stack_index; |
Bartlomiej Zolnierkiewicz | 394a4c2 | 2008-10-10 22:39:35 +0200 | [diff] [blame^] | 233 | |
| 234 | struct request request_sense_rq; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 235 | |
| 236 | /* |
Borislav Petkov | 3c98bf3 | 2008-02-06 02:57:53 +0100 | [diff] [blame] | 237 | * DSC polling variables. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 238 | * |
Borislav Petkov | 3c98bf3 | 2008-02-06 02:57:53 +0100 | [diff] [blame] | 239 | * While polling for DSC we use postponed_rq to postpone the current |
| 240 | * request so that ide.c will be able to service pending requests on the |
| 241 | * 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] | 242 | * data transfer) request in the device request queue. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 243 | */ |
| 244 | struct request *postponed_rq; |
| 245 | /* The time in which we started polling for DSC */ |
| 246 | unsigned long dsc_polling_start; |
| 247 | /* Timer used to poll for dsc */ |
| 248 | struct timer_list dsc_timer; |
| 249 | /* Read/Write dsc polling frequency */ |
Borislav Petkov | 54bb207 | 2008-02-06 02:57:52 +0100 | [diff] [blame] | 250 | unsigned long best_dsc_rw_freq; |
| 251 | unsigned long dsc_poll_freq; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 252 | unsigned long dsc_timeout; |
| 253 | |
Borislav Petkov | 3c98bf3 | 2008-02-06 02:57:53 +0100 | [diff] [blame] | 254 | /* Read position information */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 255 | u8 partition; |
| 256 | /* Current block */ |
Borislav Petkov | 54bb207 | 2008-02-06 02:57:52 +0100 | [diff] [blame] | 257 | unsigned int first_frame; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 258 | |
Borislav Petkov | 3c98bf3 | 2008-02-06 02:57:53 +0100 | [diff] [blame] | 259 | /* Last error information */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 260 | u8 sense_key, asc, ascq; |
| 261 | |
Borislav Petkov | 3c98bf3 | 2008-02-06 02:57:53 +0100 | [diff] [blame] | 262 | /* Character device operation */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 263 | unsigned int minor; |
| 264 | /* device name */ |
| 265 | char name[4]; |
| 266 | /* Current character device data transfer direction */ |
Borislav Petkov | 54abf37 | 2008-02-06 02:57:52 +0100 | [diff] [blame] | 267 | u8 chrdev_dir; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 268 | |
Borislav Petkov | 54bb207 | 2008-02-06 02:57:52 +0100 | [diff] [blame] | 269 | /* tape block size, usually 512 or 1024 bytes */ |
| 270 | unsigned short blk_size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 271 | int user_bs_factor; |
Borislav Petkov | b642201 | 2008-02-02 19:56:49 +0100 | [diff] [blame] | 272 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 273 | /* Copy of the tape's Capabilities and Mechanical Page */ |
Borislav Petkov | b642201 | 2008-02-02 19:56:49 +0100 | [diff] [blame] | 274 | u8 caps[20]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 275 | |
| 276 | /* |
Borislav Petkov | 3c98bf3 | 2008-02-06 02:57:53 +0100 | [diff] [blame] | 277 | * Active data transfer request parameters. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 278 | * |
Borislav Petkov | 3c98bf3 | 2008-02-06 02:57:53 +0100 | [diff] [blame] | 279 | * At most, there is only one ide-tape originated data transfer request |
| 280 | * in the device request queue. This allows ide.c to easily service |
| 281 | * requests from the other device when we postpone our active request. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 282 | */ |
Borislav Petkov | 83042b2 | 2008-04-27 15:38:27 +0200 | [diff] [blame] | 283 | |
Borislav Petkov | 3c98bf3 | 2008-02-06 02:57:53 +0100 | [diff] [blame] | 284 | /* Data buffer size chosen based on the tape's recommendation */ |
Borislav Petkov | f73850a | 2008-04-27 15:38:33 +0200 | [diff] [blame] | 285 | int buffer_size; |
Borislav Petkov | 077e3bd | 2008-04-27 15:38:34 +0200 | [diff] [blame] | 286 | /* merge buffer */ |
| 287 | struct idetape_bh *merge_bh; |
Borislav Petkov | 01a63aeb | 2008-04-27 15:38:34 +0200 | [diff] [blame] | 288 | /* size of the merge buffer */ |
| 289 | int merge_bh_size; |
Borislav Petkov | 077e3bd | 2008-04-27 15:38:34 +0200 | [diff] [blame] | 290 | /* pointer to current buffer head within the merge buffer */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 291 | struct idetape_bh *bh; |
| 292 | char *b_data; |
| 293 | int b_count; |
Borislav Petkov | 3c98bf3 | 2008-02-06 02:57:53 +0100 | [diff] [blame] | 294 | |
Borislav Petkov | a997a43 | 2008-04-27 15:38:33 +0200 | [diff] [blame] | 295 | int pages_per_buffer; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 296 | /* Wasted space in each stage */ |
| 297 | int excess_bh_size; |
| 298 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 299 | /* protects the ide-tape queue */ |
Borislav Petkov | 54bb207 | 2008-02-06 02:57:52 +0100 | [diff] [blame] | 300 | spinlock_t lock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 301 | |
Borislav Petkov | 3c98bf3 | 2008-02-06 02:57:53 +0100 | [diff] [blame] | 302 | /* Measures average tape speed */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 303 | unsigned long avg_time; |
| 304 | int avg_size; |
| 305 | int avg_speed; |
| 306 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 307 | /* the door is currently locked */ |
| 308 | int door_locked; |
| 309 | /* the tape hardware is write protected */ |
| 310 | char drv_write_prot; |
| 311 | /* the tape is write protected (hardware or opened as read-only) */ |
| 312 | char write_prot; |
| 313 | |
Borislav Petkov | 8004a8c | 2008-02-06 02:57:51 +0100 | [diff] [blame] | 314 | u32 debug_mask; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 315 | } idetape_tape_t; |
| 316 | |
Arjan van de Ven | cf8b897 | 2006-03-23 03:00:45 -0800 | [diff] [blame] | 317 | static DEFINE_MUTEX(idetape_ref_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 318 | |
Will Dyson | d5dee80 | 2005-09-16 02:55:07 -0700 | [diff] [blame] | 319 | static struct class *idetape_sysfs_class; |
| 320 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 321 | #define to_ide_tape(obj) container_of(obj, struct ide_tape_obj, kref) |
| 322 | |
| 323 | #define ide_tape_g(disk) \ |
| 324 | container_of((disk)->private_data, struct ide_tape_obj, driver) |
| 325 | |
Bartlomiej Zolnierkiewicz | 08da591 | 2008-07-24 22:53:15 +0200 | [diff] [blame] | 326 | static void ide_tape_release(struct kref *); |
| 327 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 328 | static struct ide_tape_obj *ide_tape_get(struct gendisk *disk) |
| 329 | { |
| 330 | struct ide_tape_obj *tape = NULL; |
| 331 | |
Arjan van de Ven | cf8b897 | 2006-03-23 03:00:45 -0800 | [diff] [blame] | 332 | mutex_lock(&idetape_ref_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 333 | tape = ide_tape_g(disk); |
Bartlomiej Zolnierkiewicz | 08da591 | 2008-07-24 22:53:15 +0200 | [diff] [blame] | 334 | if (tape) { |
Bartlomiej Zolnierkiewicz | d3e33ff | 2008-08-05 18:16:59 +0200 | [diff] [blame] | 335 | if (ide_device_get(tape->drive)) |
Bartlomiej Zolnierkiewicz | 08da591 | 2008-07-24 22:53:15 +0200 | [diff] [blame] | 336 | tape = NULL; |
Bartlomiej Zolnierkiewicz | d3e33ff | 2008-08-05 18:16:59 +0200 | [diff] [blame] | 337 | else |
| 338 | kref_get(&tape->kref); |
Bartlomiej Zolnierkiewicz | 08da591 | 2008-07-24 22:53:15 +0200 | [diff] [blame] | 339 | } |
Arjan van de Ven | cf8b897 | 2006-03-23 03:00:45 -0800 | [diff] [blame] | 340 | mutex_unlock(&idetape_ref_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 341 | return tape; |
| 342 | } |
| 343 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 344 | static void ide_tape_put(struct ide_tape_obj *tape) |
| 345 | { |
Bartlomiej Zolnierkiewicz | d3e33ff | 2008-08-05 18:16:59 +0200 | [diff] [blame] | 346 | ide_drive_t *drive = tape->drive; |
| 347 | |
Arjan van de Ven | cf8b897 | 2006-03-23 03:00:45 -0800 | [diff] [blame] | 348 | mutex_lock(&idetape_ref_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 349 | kref_put(&tape->kref, ide_tape_release); |
Bartlomiej Zolnierkiewicz | d3e33ff | 2008-08-05 18:16:59 +0200 | [diff] [blame] | 350 | ide_device_put(drive); |
Arjan van de Ven | cf8b897 | 2006-03-23 03:00:45 -0800 | [diff] [blame] | 351 | mutex_unlock(&idetape_ref_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 352 | } |
| 353 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 354 | /* |
Borislav Petkov | 3c98bf3 | 2008-02-06 02:57:53 +0100 | [diff] [blame] | 355 | * The variables below are used for the character device interface. Additional |
| 356 | * state variables are defined in our ide_drive_t structure. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 357 | */ |
Borislav Petkov | 5a04cfa | 2008-02-06 02:57:54 +0100 | [diff] [blame] | 358 | static struct ide_tape_obj *idetape_devs[MAX_HWIFS * MAX_DRIVES]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 359 | |
| 360 | #define ide_tape_f(file) ((file)->private_data) |
| 361 | |
| 362 | static struct ide_tape_obj *ide_tape_chrdev_get(unsigned int i) |
| 363 | { |
| 364 | struct ide_tape_obj *tape = NULL; |
| 365 | |
Arjan van de Ven | cf8b897 | 2006-03-23 03:00:45 -0800 | [diff] [blame] | 366 | mutex_lock(&idetape_ref_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 367 | tape = idetape_devs[i]; |
| 368 | if (tape) |
| 369 | kref_get(&tape->kref); |
Arjan van de Ven | cf8b897 | 2006-03-23 03:00:45 -0800 | [diff] [blame] | 370 | mutex_unlock(&idetape_ref_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 371 | return tape; |
| 372 | } |
| 373 | |
Borislav Petkov | d236d74 | 2008-04-18 00:46:27 +0200 | [diff] [blame] | 374 | static void idetape_input_buffers(ide_drive_t *drive, struct ide_atapi_pc *pc, |
Borislav Petkov | 5a04cfa | 2008-02-06 02:57:54 +0100 | [diff] [blame] | 375 | unsigned int bcount) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 376 | { |
| 377 | struct idetape_bh *bh = pc->bh; |
| 378 | int count; |
| 379 | |
| 380 | while (bcount) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 381 | if (bh == NULL) { |
| 382 | printk(KERN_ERR "ide-tape: bh == NULL in " |
| 383 | "idetape_input_buffers\n"); |
Bartlomiej Zolnierkiewicz | 9f87abe | 2008-04-28 23:44:41 +0200 | [diff] [blame] | 384 | ide_pad_transfer(drive, 0, bcount); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 385 | return; |
| 386 | } |
Borislav Petkov | 5a04cfa | 2008-02-06 02:57:54 +0100 | [diff] [blame] | 387 | count = min( |
| 388 | (unsigned int)(bh->b_size - atomic_read(&bh->b_count)), |
| 389 | bcount); |
Bartlomiej Zolnierkiewicz | 374e042 | 2008-07-23 19:55:56 +0200 | [diff] [blame] | 390 | drive->hwif->tp_ops->input_data(drive, NULL, bh->b_data + |
Borislav Petkov | 5a04cfa | 2008-02-06 02:57:54 +0100 | [diff] [blame] | 391 | atomic_read(&bh->b_count), count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 392 | bcount -= count; |
| 393 | atomic_add(count, &bh->b_count); |
| 394 | if (atomic_read(&bh->b_count) == bh->b_size) { |
| 395 | bh = bh->b_reqnext; |
| 396 | if (bh) |
| 397 | atomic_set(&bh->b_count, 0); |
| 398 | } |
| 399 | } |
| 400 | pc->bh = bh; |
| 401 | } |
| 402 | |
Borislav Petkov | d236d74 | 2008-04-18 00:46:27 +0200 | [diff] [blame] | 403 | static void idetape_output_buffers(ide_drive_t *drive, struct ide_atapi_pc *pc, |
Borislav Petkov | 5a04cfa | 2008-02-06 02:57:54 +0100 | [diff] [blame] | 404 | unsigned int bcount) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 405 | { |
| 406 | struct idetape_bh *bh = pc->bh; |
| 407 | int count; |
| 408 | |
| 409 | while (bcount) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 410 | if (bh == NULL) { |
Borislav Petkov | 5a04cfa | 2008-02-06 02:57:54 +0100 | [diff] [blame] | 411 | printk(KERN_ERR "ide-tape: bh == NULL in %s\n", |
| 412 | __func__); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 413 | return; |
| 414 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 415 | count = min((unsigned int)pc->b_count, (unsigned int)bcount); |
Bartlomiej Zolnierkiewicz | 374e042 | 2008-07-23 19:55:56 +0200 | [diff] [blame] | 416 | drive->hwif->tp_ops->output_data(drive, NULL, pc->b_data, count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 417 | bcount -= count; |
| 418 | pc->b_data += count; |
| 419 | pc->b_count -= count; |
| 420 | if (!pc->b_count) { |
Borislav Petkov | 5a04cfa | 2008-02-06 02:57:54 +0100 | [diff] [blame] | 421 | bh = bh->b_reqnext; |
| 422 | pc->bh = bh; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 423 | if (bh) { |
| 424 | pc->b_data = bh->b_data; |
| 425 | pc->b_count = atomic_read(&bh->b_count); |
| 426 | } |
| 427 | } |
| 428 | } |
| 429 | } |
| 430 | |
Bartlomiej Zolnierkiewicz | 646c0cb | 2008-07-15 21:22:03 +0200 | [diff] [blame] | 431 | static void idetape_update_buffers(ide_drive_t *drive, struct ide_atapi_pc *pc) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 432 | { |
| 433 | struct idetape_bh *bh = pc->bh; |
| 434 | int count; |
Borislav Petkov | d236d74 | 2008-04-18 00:46:27 +0200 | [diff] [blame] | 435 | unsigned int bcount = pc->xferred; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 436 | |
Borislav Petkov | 346331f | 2008-04-18 00:46:26 +0200 | [diff] [blame] | 437 | if (pc->flags & PC_FLAG_WRITING) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 438 | return; |
| 439 | while (bcount) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 440 | if (bh == NULL) { |
Borislav Petkov | 5a04cfa | 2008-02-06 02:57:54 +0100 | [diff] [blame] | 441 | printk(KERN_ERR "ide-tape: bh == NULL in %s\n", |
| 442 | __func__); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 443 | return; |
| 444 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 445 | count = min((unsigned int)bh->b_size, (unsigned int)bcount); |
| 446 | atomic_set(&bh->b_count, count); |
| 447 | if (atomic_read(&bh->b_count) == bh->b_size) |
| 448 | bh = bh->b_reqnext; |
| 449 | bcount -= count; |
| 450 | } |
| 451 | pc->bh = bh; |
| 452 | } |
| 453 | |
| 454 | /* |
| 455 | * idetape_next_pc_storage returns a pointer to a place in which we can |
| 456 | * safely store a packet command, even though we intend to leave the |
| 457 | * driver. A storage space for a maximum of IDETAPE_PC_STACK packet |
| 458 | * commands is allocated at initialization time. |
| 459 | */ |
Borislav Petkov | d236d74 | 2008-04-18 00:46:27 +0200 | [diff] [blame] | 460 | static struct ide_atapi_pc *idetape_next_pc_storage(ide_drive_t *drive) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 461 | { |
| 462 | idetape_tape_t *tape = drive->driver_data; |
| 463 | |
Bartlomiej Zolnierkiewicz | 394a4c2 | 2008-10-10 22:39:35 +0200 | [diff] [blame^] | 464 | debug_log(DBG_PC_STACK, "pc_stack_index=%d\n", tape->pc_stack_index); |
Borislav Petkov | 8004a8c | 2008-02-06 02:57:51 +0100 | [diff] [blame] | 465 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 466 | if (tape->pc_stack_index == IDETAPE_PC_STACK) |
Borislav Petkov | 5a04cfa | 2008-02-06 02:57:54 +0100 | [diff] [blame] | 467 | tape->pc_stack_index = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 468 | return (&tape->pc_stack[tape->pc_stack_index++]); |
| 469 | } |
| 470 | |
| 471 | /* |
Borislav Petkov | 1b5db43 | 2008-02-02 19:56:48 +0100 | [diff] [blame] | 472 | * called on each failed packet command retry to analyze the request sense. We |
| 473 | * currently do not utilize this information. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 474 | */ |
Borislav Petkov | 1b5db43 | 2008-02-02 19:56:48 +0100 | [diff] [blame] | 475 | static void idetape_analyze_error(ide_drive_t *drive, u8 *sense) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 476 | { |
| 477 | idetape_tape_t *tape = drive->driver_data; |
Borislav Petkov | d236d74 | 2008-04-18 00:46:27 +0200 | [diff] [blame] | 478 | struct ide_atapi_pc *pc = tape->failed_pc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 479 | |
Borislav Petkov | 1b5db43 | 2008-02-02 19:56:48 +0100 | [diff] [blame] | 480 | tape->sense_key = sense[2] & 0xF; |
| 481 | tape->asc = sense[12]; |
| 482 | tape->ascq = sense[13]; |
Borislav Petkov | 8004a8c | 2008-02-06 02:57:51 +0100 | [diff] [blame] | 483 | |
| 484 | debug_log(DBG_ERR, "pc = %x, sense key = %x, asc = %x, ascq = %x\n", |
| 485 | pc->c[0], tape->sense_key, tape->asc, tape->ascq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 486 | |
Borislav Petkov | d236d74 | 2008-04-18 00:46:27 +0200 | [diff] [blame] | 487 | /* Correct pc->xferred by asking the tape. */ |
Borislav Petkov | 346331f | 2008-04-18 00:46:26 +0200 | [diff] [blame] | 488 | if (pc->flags & PC_FLAG_DMA_ERROR) { |
Borislav Petkov | d236d74 | 2008-04-18 00:46:27 +0200 | [diff] [blame] | 489 | pc->xferred = pc->req_xfer - |
Borislav Petkov | 54bb207 | 2008-02-06 02:57:52 +0100 | [diff] [blame] | 490 | tape->blk_size * |
Harvey Harrison | 5d0cc8a | 2008-07-15 21:21:41 +0200 | [diff] [blame] | 491 | get_unaligned_be32(&sense[3]); |
Bartlomiej Zolnierkiewicz | 646c0cb | 2008-07-15 21:22:03 +0200 | [diff] [blame] | 492 | idetape_update_buffers(drive, pc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 493 | } |
| 494 | |
| 495 | /* |
| 496 | * If error was the result of a zero-length read or write command, |
| 497 | * with sense key=5, asc=0x22, ascq=0, let it slide. Some drives |
| 498 | * (i.e. Seagate STT3401A Travan) don't support 0-length read/writes. |
| 499 | */ |
Borislav Petkov | 90699ce | 2008-02-02 19:56:50 +0100 | [diff] [blame] | 500 | if ((pc->c[0] == READ_6 || pc->c[0] == WRITE_6) |
Borislav Petkov | 1b5db43 | 2008-02-02 19:56:48 +0100 | [diff] [blame] | 501 | /* length == 0 */ |
| 502 | && pc->c[4] == 0 && pc->c[3] == 0 && pc->c[2] == 0) { |
| 503 | if (tape->sense_key == 5) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 504 | /* don't report an error, everything's ok */ |
| 505 | pc->error = 0; |
| 506 | /* don't retry read/write */ |
Borislav Petkov | 346331f | 2008-04-18 00:46:26 +0200 | [diff] [blame] | 507 | pc->flags |= PC_FLAG_ABORT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 508 | } |
| 509 | } |
Borislav Petkov | 90699ce | 2008-02-02 19:56:50 +0100 | [diff] [blame] | 510 | if (pc->c[0] == READ_6 && (sense[2] & 0x80)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 511 | pc->error = IDETAPE_ERROR_FILEMARK; |
Borislav Petkov | 346331f | 2008-04-18 00:46:26 +0200 | [diff] [blame] | 512 | pc->flags |= PC_FLAG_ABORT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 513 | } |
Borislav Petkov | 90699ce | 2008-02-02 19:56:50 +0100 | [diff] [blame] | 514 | if (pc->c[0] == WRITE_6) { |
Borislav Petkov | 1b5db43 | 2008-02-02 19:56:48 +0100 | [diff] [blame] | 515 | if ((sense[2] & 0x40) || (tape->sense_key == 0xd |
| 516 | && tape->asc == 0x0 && tape->ascq == 0x2)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 517 | pc->error = IDETAPE_ERROR_EOD; |
Borislav Petkov | 346331f | 2008-04-18 00:46:26 +0200 | [diff] [blame] | 518 | pc->flags |= PC_FLAG_ABORT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 519 | } |
| 520 | } |
Borislav Petkov | 90699ce | 2008-02-02 19:56:50 +0100 | [diff] [blame] | 521 | if (pc->c[0] == READ_6 || pc->c[0] == WRITE_6) { |
Borislav Petkov | 1b5db43 | 2008-02-02 19:56:48 +0100 | [diff] [blame] | 522 | if (tape->sense_key == 8) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 523 | pc->error = IDETAPE_ERROR_EOD; |
Borislav Petkov | 346331f | 2008-04-18 00:46:26 +0200 | [diff] [blame] | 524 | pc->flags |= PC_FLAG_ABORT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 525 | } |
Borislav Petkov | 346331f | 2008-04-18 00:46:26 +0200 | [diff] [blame] | 526 | if (!(pc->flags & PC_FLAG_ABORT) && |
Borislav Petkov | d236d74 | 2008-04-18 00:46:27 +0200 | [diff] [blame] | 527 | pc->xferred) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 528 | pc->retries = IDETAPE_MAX_PC_RETRIES + 1; |
| 529 | } |
| 530 | } |
| 531 | |
Borislav Petkov | d01dbc3 | 2008-04-27 15:38:33 +0200 | [diff] [blame] | 532 | /* Free data buffers completely. */ |
Borislav Petkov | 077e3bd | 2008-04-27 15:38:34 +0200 | [diff] [blame] | 533 | static void ide_tape_kfree_buffer(idetape_tape_t *tape) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 534 | { |
Borislav Petkov | 077e3bd | 2008-04-27 15:38:34 +0200 | [diff] [blame] | 535 | struct idetape_bh *prev_bh, *bh = tape->merge_bh; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 536 | |
Borislav Petkov | d01dbc3 | 2008-04-27 15:38:33 +0200 | [diff] [blame] | 537 | while (bh) { |
| 538 | u32 size = bh->b_size; |
| 539 | |
| 540 | while (size) { |
| 541 | unsigned int order = fls(size >> PAGE_SHIFT)-1; |
| 542 | |
| 543 | if (bh->b_data) |
| 544 | free_pages((unsigned long)bh->b_data, order); |
| 545 | |
| 546 | size &= (order-1); |
| 547 | bh->b_data += (1 << order) * PAGE_SIZE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 548 | } |
| 549 | prev_bh = bh; |
| 550 | bh = bh->b_reqnext; |
| 551 | kfree(prev_bh); |
| 552 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 553 | } |
| 554 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 555 | static int idetape_end_request(ide_drive_t *drive, int uptodate, int nr_sects) |
| 556 | { |
| 557 | struct request *rq = HWGROUP(drive)->rq; |
| 558 | idetape_tape_t *tape = drive->driver_data; |
| 559 | unsigned long flags; |
| 560 | int error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 561 | |
Borislav Petkov | 8004a8c | 2008-02-06 02:57:51 +0100 | [diff] [blame] | 562 | debug_log(DBG_PROCS, "Enter %s\n", __func__); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 563 | |
| 564 | switch (uptodate) { |
Borislav Petkov | 5a04cfa | 2008-02-06 02:57:54 +0100 | [diff] [blame] | 565 | case 0: error = IDETAPE_ERROR_GENERAL; break; |
| 566 | case 1: error = 0; break; |
| 567 | default: error = uptodate; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 568 | } |
| 569 | rq->errors = error; |
| 570 | if (error) |
| 571 | tape->failed_pc = NULL; |
| 572 | |
Bartlomiej Zolnierkiewicz | 3687221 | 2008-01-26 20:13:10 +0100 | [diff] [blame] | 573 | if (!blk_special_request(rq)) { |
| 574 | ide_end_request(drive, uptodate, nr_sects); |
| 575 | return 0; |
| 576 | } |
| 577 | |
Borislav Petkov | 54bb207 | 2008-02-06 02:57:52 +0100 | [diff] [blame] | 578 | spin_lock_irqsave(&tape->lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 579 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 580 | ide_end_drive_cmd(drive, 0, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 581 | |
Borislav Petkov | 54bb207 | 2008-02-06 02:57:52 +0100 | [diff] [blame] | 582 | spin_unlock_irqrestore(&tape->lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 583 | return 0; |
| 584 | } |
| 585 | |
Bartlomiej Zolnierkiewicz | 92f5daf | 2008-07-15 21:21:55 +0200 | [diff] [blame] | 586 | static void ide_tape_callback(ide_drive_t *drive) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 587 | { |
| 588 | idetape_tape_t *tape = drive->driver_data; |
Bartlomiej Zolnierkiewicz | 5985e6a | 2008-07-15 21:21:55 +0200 | [diff] [blame] | 589 | struct ide_atapi_pc *pc = tape->pc; |
| 590 | int uptodate = pc->error ? 0 : 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 591 | |
Borislav Petkov | 8004a8c | 2008-02-06 02:57:51 +0100 | [diff] [blame] | 592 | debug_log(DBG_PROCS, "Enter %s\n", __func__); |
| 593 | |
Bartlomiej Zolnierkiewicz | dd2e9a0 | 2008-07-15 21:22:01 +0200 | [diff] [blame] | 594 | if (tape->failed_pc == pc) |
| 595 | tape->failed_pc = NULL; |
| 596 | |
Bartlomiej Zolnierkiewicz | 5985e6a | 2008-07-15 21:21:55 +0200 | [diff] [blame] | 597 | if (pc->c[0] == REQUEST_SENSE) { |
| 598 | if (uptodate) |
| 599 | idetape_analyze_error(drive, pc->buf); |
| 600 | else |
| 601 | printk(KERN_ERR "ide-tape: Error in REQUEST SENSE " |
| 602 | "itself - Aborting request!\n"); |
| 603 | } else if (pc->c[0] == READ_6 || pc->c[0] == WRITE_6) { |
| 604 | struct request *rq = drive->hwif->hwgroup->rq; |
| 605 | int blocks = pc->xferred / tape->blk_size; |
| 606 | |
| 607 | tape->avg_size += blocks * tape->blk_size; |
| 608 | |
| 609 | if (time_after_eq(jiffies, tape->avg_time + HZ)) { |
| 610 | tape->avg_speed = tape->avg_size * HZ / |
| 611 | (jiffies - tape->avg_time) / 1024; |
| 612 | tape->avg_size = 0; |
| 613 | tape->avg_time = jiffies; |
| 614 | } |
| 615 | |
| 616 | tape->first_frame += blocks; |
| 617 | rq->current_nr_sectors -= blocks; |
| 618 | |
| 619 | if (pc->error) |
| 620 | uptodate = pc->error; |
| 621 | } else if (pc->c[0] == READ_POSITION && uptodate) { |
| 622 | u8 *readpos = tape->pc->buf; |
| 623 | |
| 624 | debug_log(DBG_SENSE, "BOP - %s\n", |
| 625 | (readpos[0] & 0x80) ? "Yes" : "No"); |
| 626 | debug_log(DBG_SENSE, "EOP - %s\n", |
| 627 | (readpos[0] & 0x40) ? "Yes" : "No"); |
| 628 | |
| 629 | if (readpos[0] & 0x4) { |
| 630 | printk(KERN_INFO "ide-tape: Block location is unknown" |
| 631 | "to the tape\n"); |
Borislav Petkov | f2e3ab5 | 2008-07-23 19:56:01 +0200 | [diff] [blame] | 632 | clear_bit(IDE_AFLAG_ADDRESS_VALID, &drive->atapi_flags); |
Bartlomiej Zolnierkiewicz | 5985e6a | 2008-07-15 21:21:55 +0200 | [diff] [blame] | 633 | uptodate = 0; |
| 634 | } else { |
| 635 | debug_log(DBG_SENSE, "Block Location - %u\n", |
Harvey Harrison | cd740ab | 2008-07-24 22:53:33 +0200 | [diff] [blame] | 636 | be32_to_cpup((__be32 *)&readpos[4])); |
Bartlomiej Zolnierkiewicz | 5985e6a | 2008-07-15 21:21:55 +0200 | [diff] [blame] | 637 | |
| 638 | tape->partition = readpos[1]; |
Harvey Harrison | cd740ab | 2008-07-24 22:53:33 +0200 | [diff] [blame] | 639 | tape->first_frame = be32_to_cpup((__be32 *)&readpos[4]); |
Borislav Petkov | f2e3ab5 | 2008-07-23 19:56:01 +0200 | [diff] [blame] | 640 | set_bit(IDE_AFLAG_ADDRESS_VALID, &drive->atapi_flags); |
Bartlomiej Zolnierkiewicz | 5985e6a | 2008-07-15 21:21:55 +0200 | [diff] [blame] | 641 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 642 | } |
Bartlomiej Zolnierkiewicz | 5985e6a | 2008-07-15 21:21:55 +0200 | [diff] [blame] | 643 | |
| 644 | idetape_end_request(drive, uptodate, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 645 | } |
| 646 | |
Bartlomiej Zolnierkiewicz | 5985e6a | 2008-07-15 21:21:55 +0200 | [diff] [blame] | 647 | static void idetape_init_pc(struct ide_atapi_pc *pc) |
| 648 | { |
| 649 | memset(pc->c, 0, 12); |
| 650 | pc->retries = 0; |
| 651 | pc->flags = 0; |
| 652 | pc->req_xfer = 0; |
| 653 | pc->buf = pc->pc_buf; |
| 654 | pc->buf_size = IDETAPE_PC_BUFFER_SIZE; |
| 655 | pc->bh = NULL; |
| 656 | pc->b_data = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 657 | } |
| 658 | |
Borislav Petkov | d236d74 | 2008-04-18 00:46:27 +0200 | [diff] [blame] | 659 | static void idetape_create_request_sense_cmd(struct ide_atapi_pc *pc) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 660 | { |
Borislav Petkov | 5a04cfa | 2008-02-06 02:57:54 +0100 | [diff] [blame] | 661 | idetape_init_pc(pc); |
Borislav Petkov | 90699ce | 2008-02-02 19:56:50 +0100 | [diff] [blame] | 662 | pc->c[0] = REQUEST_SENSE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 663 | pc->c[4] = 20; |
Borislav Petkov | d236d74 | 2008-04-18 00:46:27 +0200 | [diff] [blame] | 664 | pc->req_xfer = 20; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 665 | } |
| 666 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 667 | /* |
Borislav Petkov | 3c98bf3 | 2008-02-06 02:57:53 +0100 | [diff] [blame] | 668 | * Generate a new packet command request in front of the request queue, before |
| 669 | * the current request, so that it will be processed immediately, on the next |
Bartlomiej Zolnierkiewicz | 394a4c2 | 2008-10-10 22:39:35 +0200 | [diff] [blame^] | 670 | * pass through the driver. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 671 | */ |
Borislav Petkov | d236d74 | 2008-04-18 00:46:27 +0200 | [diff] [blame] | 672 | static void idetape_queue_pc_head(ide_drive_t *drive, struct ide_atapi_pc *pc, |
Borislav Petkov | 5a04cfa | 2008-02-06 02:57:54 +0100 | [diff] [blame] | 673 | struct request *rq) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 674 | { |
| 675 | struct ide_tape_obj *tape = drive->driver_data; |
| 676 | |
Bartlomiej Zolnierkiewicz | f025ffd | 2008-10-10 22:39:34 +0200 | [diff] [blame] | 677 | blk_rq_init(NULL, rq); |
| 678 | rq->cmd_type = REQ_TYPE_SPECIAL; |
Bartlomiej Zolnierkiewicz | e8a96aa | 2008-07-15 21:21:41 +0200 | [diff] [blame] | 679 | rq->cmd_flags |= REQ_PREEMPT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 680 | rq->buffer = (char *) pc; |
| 681 | rq->rq_disk = tape->disk; |
Borislav Petkov | 0014c75 | 2008-07-23 19:56:00 +0200 | [diff] [blame] | 682 | memcpy(rq->cmd, pc->c, 12); |
Bartlomiej Zolnierkiewicz | f025ffd | 2008-10-10 22:39:34 +0200 | [diff] [blame] | 683 | rq->cmd[13] = REQ_IDETAPE_PC1; |
FUJITA Tomonori | 63f5abb | 2008-07-15 21:21:51 +0200 | [diff] [blame] | 684 | ide_do_drive_cmd(drive, rq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 685 | } |
| 686 | |
| 687 | /* |
| 688 | * idetape_retry_pc is called when an error was detected during the |
| 689 | * last packet command. We queue a request sense packet command in |
| 690 | * the head of the request list. |
| 691 | */ |
Bartlomiej Zolnierkiewicz | 258ec41 | 2008-07-15 21:21:55 +0200 | [diff] [blame] | 692 | static void idetape_retry_pc(ide_drive_t *drive) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 693 | { |
Bartlomiej Zolnierkiewicz | 394a4c2 | 2008-10-10 22:39:35 +0200 | [diff] [blame^] | 694 | struct ide_tape_obj *tape = drive->driver_data; |
| 695 | struct request *rq = &tape->request_sense_rq; |
Borislav Petkov | d236d74 | 2008-04-18 00:46:27 +0200 | [diff] [blame] | 696 | struct ide_atapi_pc *pc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 697 | |
Bartlomiej Zolnierkiewicz | 64a57fe | 2008-02-06 02:57:51 +0100 | [diff] [blame] | 698 | (void)ide_read_error(drive); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 699 | pc = idetape_next_pc_storage(drive); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 700 | idetape_create_request_sense_cmd(pc); |
Borislav Petkov | f2e3ab5 | 2008-07-23 19:56:01 +0200 | [diff] [blame] | 701 | set_bit(IDE_AFLAG_IGNORE_DSC, &drive->atapi_flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 702 | idetape_queue_pc_head(drive, pc, rq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 703 | } |
| 704 | |
| 705 | /* |
Borislav Petkov | 3c98bf3 | 2008-02-06 02:57:53 +0100 | [diff] [blame] | 706 | * Postpone the current request so that ide.c will be able to service requests |
| 707 | * from another device on the same hwgroup while we are polling for DSC. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 708 | */ |
Borislav Petkov | 5a04cfa | 2008-02-06 02:57:54 +0100 | [diff] [blame] | 709 | static void idetape_postpone_request(ide_drive_t *drive) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 710 | { |
| 711 | idetape_tape_t *tape = drive->driver_data; |
| 712 | |
Borislav Petkov | 8004a8c | 2008-02-06 02:57:51 +0100 | [diff] [blame] | 713 | debug_log(DBG_PROCS, "Enter %s\n", __func__); |
| 714 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 715 | tape->postponed_rq = HWGROUP(drive)->rq; |
Borislav Petkov | 54bb207 | 2008-02-06 02:57:52 +0100 | [diff] [blame] | 716 | ide_stall_queue(drive, tape->dsc_poll_freq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 717 | } |
| 718 | |
Bartlomiej Zolnierkiewicz | 74e63e74 | 2008-07-15 21:22:01 +0200 | [diff] [blame] | 719 | static void ide_tape_handle_dsc(ide_drive_t *drive) |
| 720 | { |
| 721 | idetape_tape_t *tape = drive->driver_data; |
| 722 | |
| 723 | /* Media access command */ |
| 724 | tape->dsc_polling_start = jiffies; |
| 725 | tape->dsc_poll_freq = IDETAPE_DSC_MA_FAST; |
| 726 | tape->dsc_timeout = jiffies + IDETAPE_DSC_MA_TIMEOUT; |
| 727 | /* Allow ide.c to handle other requests */ |
| 728 | idetape_postpone_request(drive); |
| 729 | } |
| 730 | |
Bartlomiej Zolnierkiewicz | 08424ac | 2008-07-15 21:22:01 +0200 | [diff] [blame] | 731 | static void ide_tape_io_buffers(ide_drive_t *drive, struct ide_atapi_pc *pc, |
| 732 | unsigned int bcount, int write) |
| 733 | { |
| 734 | if (write) |
| 735 | idetape_output_buffers(drive, pc, bcount); |
| 736 | else |
| 737 | idetape_input_buffers(drive, pc, bcount); |
| 738 | } |
Borislav Petkov | a1efc85 | 2008-02-06 02:57:52 +0100 | [diff] [blame] | 739 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 740 | /* |
Borislav Petkov | a1efc85 | 2008-02-06 02:57:52 +0100 | [diff] [blame] | 741 | * This is the usual interrupt handler which will be called during a packet |
| 742 | * command. We will transfer some of the data (as requested by the drive) and |
| 743 | * will re-point interrupt handler to us. When data transfer is finished, we |
| 744 | * will act according to the algorithm described before |
Borislav Petkov | 8d06bfa | 2008-02-06 02:57:53 +0100 | [diff] [blame] | 745 | * idetape_issue_pc. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 746 | */ |
Borislav Petkov | a1efc85 | 2008-02-06 02:57:52 +0100 | [diff] [blame] | 747 | static ide_startstop_t idetape_pc_intr(ide_drive_t *drive) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 748 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 749 | idetape_tape_t *tape = drive->driver_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 750 | |
Bartlomiej Zolnierkiewicz | 646c0cb | 2008-07-15 21:22:03 +0200 | [diff] [blame] | 751 | return ide_pc_intr(drive, tape->pc, idetape_pc_intr, IDETAPE_WAIT_CMD, |
| 752 | NULL, idetape_update_buffers, idetape_retry_pc, |
| 753 | ide_tape_handle_dsc, ide_tape_io_buffers); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 754 | } |
| 755 | |
| 756 | /* |
Borislav Petkov | 3c98bf3 | 2008-02-06 02:57:53 +0100 | [diff] [blame] | 757 | * Packet Command Interface |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 758 | * |
Borislav Petkov | 3c98bf3 | 2008-02-06 02:57:53 +0100 | [diff] [blame] | 759 | * The current Packet Command is available in tape->pc, and will not change |
| 760 | * until we finish handling it. Each packet command is associated with a |
| 761 | * callback function that will be called when the command is finished. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 762 | * |
Borislav Petkov | 3c98bf3 | 2008-02-06 02:57:53 +0100 | [diff] [blame] | 763 | * The handling will be done in three stages: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 764 | * |
Borislav Petkov | 3c98bf3 | 2008-02-06 02:57:53 +0100 | [diff] [blame] | 765 | * 1. idetape_issue_pc will send the packet command to the drive, and will set |
| 766 | * the interrupt handler to idetape_pc_intr. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 767 | * |
Borislav Petkov | 3c98bf3 | 2008-02-06 02:57:53 +0100 | [diff] [blame] | 768 | * 2. On each interrupt, idetape_pc_intr will be called. This step will be |
| 769 | * 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] | 770 | * |
Borislav Petkov | 3c98bf3 | 2008-02-06 02:57:53 +0100 | [diff] [blame] | 771 | * 3. ATAPI Tape media access commands have immediate status with a delayed |
| 772 | * process. In case of a successful initiation of a media access packet command, |
| 773 | * the DSC bit will be set when the actual execution of the command is finished. |
| 774 | * Since the tape drive will not issue an interrupt, we have to poll for this |
| 775 | * event. In this case, we define the request as "low priority request" by |
| 776 | * setting rq_status to IDETAPE_RQ_POSTPONED, set a timer to poll for DSC and |
| 777 | * exit the driver. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 778 | * |
Borislav Petkov | 3c98bf3 | 2008-02-06 02:57:53 +0100 | [diff] [blame] | 779 | * ide.c will then give higher priority to requests which originate from the |
| 780 | * other device, until will change rq_status to RQ_ACTIVE. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 781 | * |
Borislav Petkov | 3c98bf3 | 2008-02-06 02:57:53 +0100 | [diff] [blame] | 782 | * 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] | 783 | * |
Borislav Petkov | 3c98bf3 | 2008-02-06 02:57:53 +0100 | [diff] [blame] | 784 | * 5. In case an error was found, we queue a request sense packet command in |
| 785 | * front of the request queue and retry the operation up to |
| 786 | * IDETAPE_MAX_PC_RETRIES times. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 787 | * |
Borislav Petkov | 3c98bf3 | 2008-02-06 02:57:53 +0100 | [diff] [blame] | 788 | * 6. In case no error was found, or we decided to give up and not to retry |
| 789 | * again, the callback function will be called and then we will handle the next |
| 790 | * request. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 791 | */ |
| 792 | static ide_startstop_t idetape_transfer_pc(ide_drive_t *drive) |
| 793 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 794 | idetape_tape_t *tape = drive->driver_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 795 | |
Bartlomiej Zolnierkiewicz | 594c16d | 2008-07-15 21:21:58 +0200 | [diff] [blame] | 796 | return ide_transfer_pc(drive, tape->pc, idetape_pc_intr, |
| 797 | IDETAPE_WAIT_CMD, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 798 | } |
| 799 | |
Borislav Petkov | d236d74 | 2008-04-18 00:46:27 +0200 | [diff] [blame] | 800 | static ide_startstop_t idetape_issue_pc(ide_drive_t *drive, |
| 801 | struct ide_atapi_pc *pc) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 802 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 803 | idetape_tape_t *tape = drive->driver_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 804 | |
Borislav Petkov | 90699ce | 2008-02-02 19:56:50 +0100 | [diff] [blame] | 805 | if (tape->pc->c[0] == REQUEST_SENSE && |
| 806 | pc->c[0] == REQUEST_SENSE) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 807 | printk(KERN_ERR "ide-tape: possible ide-tape.c bug - " |
| 808 | "Two request sense in serial were issued\n"); |
| 809 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 810 | |
Borislav Petkov | 90699ce | 2008-02-02 19:56:50 +0100 | [diff] [blame] | 811 | if (tape->failed_pc == NULL && pc->c[0] != REQUEST_SENSE) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 812 | tape->failed_pc = pc; |
| 813 | /* Set the current packet command */ |
| 814 | tape->pc = pc; |
| 815 | |
| 816 | if (pc->retries > IDETAPE_MAX_PC_RETRIES || |
Borislav Petkov | 346331f | 2008-04-18 00:46:26 +0200 | [diff] [blame] | 817 | (pc->flags & PC_FLAG_ABORT)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 818 | /* |
Borislav Petkov | 3c98bf3 | 2008-02-06 02:57:53 +0100 | [diff] [blame] | 819 | * We will "abort" retrying a packet command in case legitimate |
| 820 | * error code was received (crossing a filemark, or end of the |
| 821 | * media, for example). |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 822 | */ |
Borislav Petkov | 346331f | 2008-04-18 00:46:26 +0200 | [diff] [blame] | 823 | if (!(pc->flags & PC_FLAG_ABORT)) { |
Borislav Petkov | 90699ce | 2008-02-02 19:56:50 +0100 | [diff] [blame] | 824 | if (!(pc->c[0] == TEST_UNIT_READY && |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 825 | tape->sense_key == 2 && tape->asc == 4 && |
| 826 | (tape->ascq == 1 || tape->ascq == 8))) { |
| 827 | printk(KERN_ERR "ide-tape: %s: I/O error, " |
| 828 | "pc = %2x, key = %2x, " |
| 829 | "asc = %2x, ascq = %2x\n", |
| 830 | tape->name, pc->c[0], |
| 831 | tape->sense_key, tape->asc, |
| 832 | tape->ascq); |
| 833 | } |
| 834 | /* Giving up */ |
| 835 | pc->error = IDETAPE_ERROR_GENERAL; |
| 836 | } |
| 837 | tape->failed_pc = NULL; |
Borislav Petkov | 776bb02 | 2008-07-23 19:55:59 +0200 | [diff] [blame] | 838 | drive->pc_callback(drive); |
Bartlomiej Zolnierkiewicz | 92f5daf | 2008-07-15 21:21:55 +0200 | [diff] [blame] | 839 | return ide_stopped; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 840 | } |
Borislav Petkov | 8004a8c | 2008-02-06 02:57:51 +0100 | [diff] [blame] | 841 | debug_log(DBG_SENSE, "Retry #%d, cmd = %02X\n", pc->retries, pc->c[0]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 842 | |
| 843 | pc->retries++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 844 | |
Bartlomiej Zolnierkiewicz | 6bf1641 | 2008-07-15 21:22:00 +0200 | [diff] [blame] | 845 | return ide_issue_pc(drive, pc, idetape_transfer_pc, |
| 846 | IDETAPE_WAIT_CMD, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 847 | } |
| 848 | |
Borislav Petkov | 3c98bf3 | 2008-02-06 02:57:53 +0100 | [diff] [blame] | 849 | /* A mode sense command is used to "sense" tape parameters. */ |
Borislav Petkov | d236d74 | 2008-04-18 00:46:27 +0200 | [diff] [blame] | 850 | 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] | 851 | { |
| 852 | idetape_init_pc(pc); |
Borislav Petkov | 90699ce | 2008-02-02 19:56:50 +0100 | [diff] [blame] | 853 | pc->c[0] = MODE_SENSE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 854 | if (page_code != IDETAPE_BLOCK_DESCRIPTOR) |
Borislav Petkov | 3c98bf3 | 2008-02-06 02:57:53 +0100 | [diff] [blame] | 855 | /* DBD = 1 - Don't return block descriptors */ |
| 856 | pc->c[1] = 8; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 857 | pc->c[2] = page_code; |
| 858 | /* |
| 859 | * Changed pc->c[3] to 0 (255 will at best return unused info). |
| 860 | * |
| 861 | * For SCSI this byte is defined as subpage instead of high byte |
| 862 | * of length and some IDE drives seem to interpret it this way |
| 863 | * and return an error when 255 is used. |
| 864 | */ |
| 865 | pc->c[3] = 0; |
Borislav Petkov | 3c98bf3 | 2008-02-06 02:57:53 +0100 | [diff] [blame] | 866 | /* We will just discard data in that case */ |
| 867 | pc->c[4] = 255; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 868 | if (page_code == IDETAPE_BLOCK_DESCRIPTOR) |
Borislav Petkov | d236d74 | 2008-04-18 00:46:27 +0200 | [diff] [blame] | 869 | pc->req_xfer = 12; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 870 | else if (page_code == IDETAPE_CAPABILITIES_PAGE) |
Borislav Petkov | d236d74 | 2008-04-18 00:46:27 +0200 | [diff] [blame] | 871 | pc->req_xfer = 24; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 872 | else |
Borislav Petkov | d236d74 | 2008-04-18 00:46:27 +0200 | [diff] [blame] | 873 | pc->req_xfer = 50; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 874 | } |
| 875 | |
Borislav Petkov | 5a04cfa | 2008-02-06 02:57:54 +0100 | [diff] [blame] | 876 | static ide_startstop_t idetape_media_access_finished(ide_drive_t *drive) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 877 | { |
Bartlomiej Zolnierkiewicz | b73c7ee | 2008-07-23 19:55:52 +0200 | [diff] [blame] | 878 | ide_hwif_t *hwif = drive->hwif; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 879 | idetape_tape_t *tape = drive->driver_data; |
Borislav Petkov | d236d74 | 2008-04-18 00:46:27 +0200 | [diff] [blame] | 880 | struct ide_atapi_pc *pc = tape->pc; |
Bartlomiej Zolnierkiewicz | 22c525b | 2008-01-25 22:17:11 +0100 | [diff] [blame] | 881 | u8 stat; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 882 | |
Bartlomiej Zolnierkiewicz | 374e042 | 2008-07-23 19:55:56 +0200 | [diff] [blame] | 883 | stat = hwif->tp_ops->read_status(hwif); |
Bartlomiej Zolnierkiewicz | c47137a | 2008-02-06 02:57:51 +0100 | [diff] [blame] | 884 | |
Bartlomiej Zolnierkiewicz | 3a7d248 | 2008-10-10 22:39:21 +0200 | [diff] [blame] | 885 | if (stat & ATA_DSC) { |
| 886 | if (stat & ATA_ERR) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 887 | /* Error detected */ |
Borislav Petkov | 90699ce | 2008-02-02 19:56:50 +0100 | [diff] [blame] | 888 | if (pc->c[0] != TEST_UNIT_READY) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 889 | printk(KERN_ERR "ide-tape: %s: I/O error, ", |
| 890 | tape->name); |
| 891 | /* Retry operation */ |
Bartlomiej Zolnierkiewicz | 258ec41 | 2008-07-15 21:21:55 +0200 | [diff] [blame] | 892 | idetape_retry_pc(drive); |
| 893 | return ide_stopped; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 894 | } |
| 895 | pc->error = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 896 | } else { |
| 897 | pc->error = IDETAPE_ERROR_GENERAL; |
| 898 | tape->failed_pc = NULL; |
| 899 | } |
Borislav Petkov | 776bb02 | 2008-07-23 19:55:59 +0200 | [diff] [blame] | 900 | drive->pc_callback(drive); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 901 | return ide_stopped; |
| 902 | } |
| 903 | |
Borislav Petkov | cd2abbf | 2008-07-15 21:22:03 +0200 | [diff] [blame] | 904 | static void ide_tape_create_rw_cmd(idetape_tape_t *tape, |
Borislav Petkov | 0014c75 | 2008-07-23 19:56:00 +0200 | [diff] [blame] | 905 | struct ide_atapi_pc *pc, struct request *rq, |
| 906 | u8 opcode) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 907 | { |
Borislav Petkov | 0014c75 | 2008-07-23 19:56:00 +0200 | [diff] [blame] | 908 | struct idetape_bh *bh = (struct idetape_bh *)rq->special; |
| 909 | unsigned int length = rq->current_nr_sectors; |
| 910 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 911 | idetape_init_pc(pc); |
Borislav Petkov | 860ff5e | 2008-02-02 19:56:50 +0100 | [diff] [blame] | 912 | put_unaligned(cpu_to_be32(length), (unsigned int *) &pc->c[1]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 913 | pc->c[1] = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 914 | pc->bh = bh; |
Borislav Petkov | d236d74 | 2008-04-18 00:46:27 +0200 | [diff] [blame] | 915 | pc->buf = NULL; |
| 916 | pc->buf_size = length * tape->blk_size; |
| 917 | pc->req_xfer = pc->buf_size; |
Borislav Petkov | f73850a | 2008-04-27 15:38:33 +0200 | [diff] [blame] | 918 | if (pc->req_xfer == tape->buffer_size) |
Bartlomiej Zolnierkiewicz | 5e33109 | 2008-07-15 21:21:56 +0200 | [diff] [blame] | 919 | pc->flags |= PC_FLAG_DMA_OK; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 920 | |
Borislav Petkov | cd2abbf | 2008-07-15 21:22:03 +0200 | [diff] [blame] | 921 | if (opcode == READ_6) { |
| 922 | pc->c[0] = READ_6; |
| 923 | atomic_set(&bh->b_count, 0); |
| 924 | } else if (opcode == WRITE_6) { |
| 925 | pc->c[0] = WRITE_6; |
| 926 | pc->flags |= PC_FLAG_WRITING; |
| 927 | pc->b_data = bh->b_data; |
| 928 | pc->b_count = atomic_read(&bh->b_count); |
| 929 | } |
Borislav Petkov | 0014c75 | 2008-07-23 19:56:00 +0200 | [diff] [blame] | 930 | |
| 931 | memcpy(rq->cmd, pc->c, 12); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 932 | } |
| 933 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 934 | static ide_startstop_t idetape_do_request(ide_drive_t *drive, |
| 935 | struct request *rq, sector_t block) |
| 936 | { |
Bartlomiej Zolnierkiewicz | b73c7ee | 2008-07-23 19:55:52 +0200 | [diff] [blame] | 937 | ide_hwif_t *hwif = drive->hwif; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 938 | idetape_tape_t *tape = drive->driver_data; |
Borislav Petkov | d236d74 | 2008-04-18 00:46:27 +0200 | [diff] [blame] | 939 | struct ide_atapi_pc *pc = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 940 | struct request *postponed_rq = tape->postponed_rq; |
Bartlomiej Zolnierkiewicz | 22c525b | 2008-01-25 22:17:11 +0100 | [diff] [blame] | 941 | u8 stat; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 942 | |
Mark de Wever | 730616b | 2008-10-10 22:39:17 +0200 | [diff] [blame] | 943 | debug_log(DBG_SENSE, "sector: %llu, nr_sectors: %lu," |
| 944 | " current_nr_sectors: %u\n", |
| 945 | (unsigned long long)rq->sector, rq->nr_sectors, |
| 946 | rq->current_nr_sectors); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 947 | |
Jens Axboe | 4aff5e2 | 2006-08-10 08:44:47 +0200 | [diff] [blame] | 948 | if (!blk_special_request(rq)) { |
Borislav Petkov | 3c98bf3 | 2008-02-06 02:57:53 +0100 | [diff] [blame] | 949 | /* We do not support buffer cache originated requests. */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 950 | printk(KERN_NOTICE "ide-tape: %s: Unsupported request in " |
Jens Axboe | 4aff5e2 | 2006-08-10 08:44:47 +0200 | [diff] [blame] | 951 | "request queue (%d)\n", drive->name, rq->cmd_type); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 952 | ide_end_request(drive, 0, 0); |
| 953 | return ide_stopped; |
| 954 | } |
| 955 | |
Borislav Petkov | 3c98bf3 | 2008-02-06 02:57:53 +0100 | [diff] [blame] | 956 | /* Retry a failed packet command */ |
Bartlomiej Zolnierkiewicz | 28c7214 | 2008-07-15 21:21:59 +0200 | [diff] [blame] | 957 | if (tape->failed_pc && tape->pc->c[0] == REQUEST_SENSE) { |
| 958 | pc = tape->failed_pc; |
| 959 | goto out; |
| 960 | } |
Borislav Petkov | 5a04cfa | 2008-02-06 02:57:54 +0100 | [diff] [blame] | 961 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 962 | if (postponed_rq != NULL) |
| 963 | if (rq != postponed_rq) { |
| 964 | printk(KERN_ERR "ide-tape: ide-tape.c bug - " |
| 965 | "Two DSC requests were queued\n"); |
| 966 | idetape_end_request(drive, 0, 0); |
| 967 | return ide_stopped; |
| 968 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 969 | |
| 970 | tape->postponed_rq = NULL; |
| 971 | |
| 972 | /* |
| 973 | * If the tape is still busy, postpone our request and service |
| 974 | * the other device meanwhile. |
| 975 | */ |
Bartlomiej Zolnierkiewicz | 374e042 | 2008-07-23 19:55:56 +0200 | [diff] [blame] | 976 | stat = hwif->tp_ops->read_status(hwif); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 977 | |
Borislav Petkov | 83dd573 | 2008-07-23 19:56:00 +0200 | [diff] [blame] | 978 | if (!drive->dsc_overlap && !(rq->cmd[13] & REQ_IDETAPE_PC2)) |
Borislav Petkov | f2e3ab5 | 2008-07-23 19:56:01 +0200 | [diff] [blame] | 979 | set_bit(IDE_AFLAG_IGNORE_DSC, &drive->atapi_flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 980 | |
| 981 | if (drive->post_reset == 1) { |
Borislav Petkov | f2e3ab5 | 2008-07-23 19:56:01 +0200 | [diff] [blame] | 982 | set_bit(IDE_AFLAG_IGNORE_DSC, &drive->atapi_flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 983 | drive->post_reset = 0; |
| 984 | } |
| 985 | |
Borislav Petkov | f2e3ab5 | 2008-07-23 19:56:01 +0200 | [diff] [blame] | 986 | if (!test_and_clear_bit(IDE_AFLAG_IGNORE_DSC, &drive->atapi_flags) && |
Bartlomiej Zolnierkiewicz | 3a7d248 | 2008-10-10 22:39:21 +0200 | [diff] [blame] | 987 | (stat & ATA_DSC) == 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 988 | if (postponed_rq == NULL) { |
| 989 | tape->dsc_polling_start = jiffies; |
Borislav Petkov | 54bb207 | 2008-02-06 02:57:52 +0100 | [diff] [blame] | 990 | tape->dsc_poll_freq = tape->best_dsc_rw_freq; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 991 | tape->dsc_timeout = jiffies + IDETAPE_DSC_RW_TIMEOUT; |
| 992 | } else if (time_after(jiffies, tape->dsc_timeout)) { |
| 993 | printk(KERN_ERR "ide-tape: %s: DSC timeout\n", |
| 994 | tape->name); |
Borislav Petkov | 83dd573 | 2008-07-23 19:56:00 +0200 | [diff] [blame] | 995 | if (rq->cmd[13] & REQ_IDETAPE_PC2) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 996 | idetape_media_access_finished(drive); |
| 997 | return ide_stopped; |
| 998 | } else { |
| 999 | return ide_do_reset(drive); |
| 1000 | } |
Borislav Petkov | 5a04cfa | 2008-02-06 02:57:54 +0100 | [diff] [blame] | 1001 | } else if (time_after(jiffies, |
| 1002 | tape->dsc_polling_start + |
| 1003 | IDETAPE_DSC_MA_THRESHOLD)) |
Borislav Petkov | 54bb207 | 2008-02-06 02:57:52 +0100 | [diff] [blame] | 1004 | tape->dsc_poll_freq = IDETAPE_DSC_MA_SLOW; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1005 | idetape_postpone_request(drive); |
| 1006 | return ide_stopped; |
| 1007 | } |
Borislav Petkov | 83dd573 | 2008-07-23 19:56:00 +0200 | [diff] [blame] | 1008 | if (rq->cmd[13] & REQ_IDETAPE_READ) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1009 | pc = idetape_next_pc_storage(drive); |
Borislav Petkov | 0014c75 | 2008-07-23 19:56:00 +0200 | [diff] [blame] | 1010 | ide_tape_create_rw_cmd(tape, pc, rq, READ_6); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1011 | goto out; |
| 1012 | } |
Borislav Petkov | 83dd573 | 2008-07-23 19:56:00 +0200 | [diff] [blame] | 1013 | if (rq->cmd[13] & REQ_IDETAPE_WRITE) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1014 | pc = idetape_next_pc_storage(drive); |
Borislav Petkov | 0014c75 | 2008-07-23 19:56:00 +0200 | [diff] [blame] | 1015 | ide_tape_create_rw_cmd(tape, pc, rq, WRITE_6); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1016 | goto out; |
| 1017 | } |
Borislav Petkov | 83dd573 | 2008-07-23 19:56:00 +0200 | [diff] [blame] | 1018 | if (rq->cmd[13] & REQ_IDETAPE_PC1) { |
Borislav Petkov | d236d74 | 2008-04-18 00:46:27 +0200 | [diff] [blame] | 1019 | pc = (struct ide_atapi_pc *) rq->buffer; |
Borislav Petkov | 83dd573 | 2008-07-23 19:56:00 +0200 | [diff] [blame] | 1020 | rq->cmd[13] &= ~(REQ_IDETAPE_PC1); |
| 1021 | rq->cmd[13] |= REQ_IDETAPE_PC2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1022 | goto out; |
| 1023 | } |
Borislav Petkov | 83dd573 | 2008-07-23 19:56:00 +0200 | [diff] [blame] | 1024 | if (rq->cmd[13] & REQ_IDETAPE_PC2) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1025 | idetape_media_access_finished(drive); |
| 1026 | return ide_stopped; |
| 1027 | } |
| 1028 | BUG(); |
Bartlomiej Zolnierkiewicz | 28c7214 | 2008-07-15 21:21:59 +0200 | [diff] [blame] | 1029 | |
Borislav Petkov | f2e3ab5 | 2008-07-23 19:56:01 +0200 | [diff] [blame] | 1030 | out: |
Borislav Petkov | 8d06bfa | 2008-02-06 02:57:53 +0100 | [diff] [blame] | 1031 | return idetape_issue_pc(drive, pc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1032 | } |
| 1033 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1034 | /* |
Borislav Petkov | 41aa170 | 2008-04-27 15:38:32 +0200 | [diff] [blame] | 1035 | * The function below uses __get_free_pages to allocate a data buffer of size |
Borislav Petkov | f73850a | 2008-04-27 15:38:33 +0200 | [diff] [blame] | 1036 | * tape->buffer_size (or a bit more). We attempt to combine sequential pages as |
Borislav Petkov | 3c98bf3 | 2008-02-06 02:57:53 +0100 | [diff] [blame] | 1037 | * much as possible. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1038 | * |
Borislav Petkov | 41aa170 | 2008-04-27 15:38:32 +0200 | [diff] [blame] | 1039 | * It returns a pointer to the newly allocated buffer, or NULL in case of |
| 1040 | * failure. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1041 | */ |
Borislav Petkov | 077e3bd | 2008-04-27 15:38:34 +0200 | [diff] [blame] | 1042 | static struct idetape_bh *ide_tape_kmalloc_buffer(idetape_tape_t *tape, |
| 1043 | int full, int clear) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1044 | { |
Borislav Petkov | 077e3bd | 2008-04-27 15:38:34 +0200 | [diff] [blame] | 1045 | struct idetape_bh *prev_bh, *bh, *merge_bh; |
Borislav Petkov | a997a43 | 2008-04-27 15:38:33 +0200 | [diff] [blame] | 1046 | int pages = tape->pages_per_buffer; |
Borislav Petkov | 41aa170 | 2008-04-27 15:38:32 +0200 | [diff] [blame] | 1047 | unsigned int order, b_allocd; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1048 | char *b_data = NULL; |
| 1049 | |
Borislav Petkov | 077e3bd | 2008-04-27 15:38:34 +0200 | [diff] [blame] | 1050 | merge_bh = kmalloc(sizeof(struct idetape_bh), GFP_KERNEL); |
| 1051 | bh = merge_bh; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1052 | if (bh == NULL) |
| 1053 | goto abort; |
Borislav Petkov | 41aa170 | 2008-04-27 15:38:32 +0200 | [diff] [blame] | 1054 | |
| 1055 | order = fls(pages) - 1; |
| 1056 | bh->b_data = (char *) __get_free_pages(GFP_KERNEL, order); |
Borislav Petkov | 5a04cfa | 2008-02-06 02:57:54 +0100 | [diff] [blame] | 1057 | if (!bh->b_data) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1058 | goto abort; |
Borislav Petkov | 41aa170 | 2008-04-27 15:38:32 +0200 | [diff] [blame] | 1059 | b_allocd = (1 << order) * PAGE_SIZE; |
| 1060 | pages &= (order-1); |
| 1061 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1062 | if (clear) |
Borislav Petkov | 41aa170 | 2008-04-27 15:38:32 +0200 | [diff] [blame] | 1063 | memset(bh->b_data, 0, b_allocd); |
| 1064 | bh->b_reqnext = NULL; |
| 1065 | bh->b_size = b_allocd; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1066 | atomic_set(&bh->b_count, full ? bh->b_size : 0); |
| 1067 | |
Borislav Petkov | 41aa170 | 2008-04-27 15:38:32 +0200 | [diff] [blame] | 1068 | while (pages) { |
| 1069 | order = fls(pages) - 1; |
| 1070 | b_data = (char *) __get_free_pages(GFP_KERNEL, order); |
Borislav Petkov | 5a04cfa | 2008-02-06 02:57:54 +0100 | [diff] [blame] | 1071 | if (!b_data) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1072 | goto abort; |
Borislav Petkov | 41aa170 | 2008-04-27 15:38:32 +0200 | [diff] [blame] | 1073 | b_allocd = (1 << order) * PAGE_SIZE; |
| 1074 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1075 | if (clear) |
Borislav Petkov | 41aa170 | 2008-04-27 15:38:32 +0200 | [diff] [blame] | 1076 | memset(b_data, 0, b_allocd); |
| 1077 | |
| 1078 | /* newly allocated page frames below buffer header or ...*/ |
| 1079 | if (bh->b_data == b_data + b_allocd) { |
| 1080 | bh->b_size += b_allocd; |
| 1081 | bh->b_data -= b_allocd; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1082 | if (full) |
Borislav Petkov | 41aa170 | 2008-04-27 15:38:32 +0200 | [diff] [blame] | 1083 | atomic_add(b_allocd, &bh->b_count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1084 | continue; |
| 1085 | } |
Borislav Petkov | 41aa170 | 2008-04-27 15:38:32 +0200 | [diff] [blame] | 1086 | /* they are above the header */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1087 | if (b_data == bh->b_data + bh->b_size) { |
Borislav Petkov | 41aa170 | 2008-04-27 15:38:32 +0200 | [diff] [blame] | 1088 | bh->b_size += b_allocd; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1089 | if (full) |
Borislav Petkov | 41aa170 | 2008-04-27 15:38:32 +0200 | [diff] [blame] | 1090 | atomic_add(b_allocd, &bh->b_count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1091 | continue; |
| 1092 | } |
| 1093 | prev_bh = bh; |
Borislav Petkov | 5a04cfa | 2008-02-06 02:57:54 +0100 | [diff] [blame] | 1094 | bh = kmalloc(sizeof(struct idetape_bh), GFP_KERNEL); |
| 1095 | if (!bh) { |
Borislav Petkov | 41aa170 | 2008-04-27 15:38:32 +0200 | [diff] [blame] | 1096 | free_pages((unsigned long) b_data, order); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1097 | goto abort; |
| 1098 | } |
| 1099 | bh->b_reqnext = NULL; |
| 1100 | bh->b_data = b_data; |
Borislav Petkov | 41aa170 | 2008-04-27 15:38:32 +0200 | [diff] [blame] | 1101 | bh->b_size = b_allocd; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1102 | atomic_set(&bh->b_count, full ? bh->b_size : 0); |
| 1103 | prev_bh->b_reqnext = bh; |
Borislav Petkov | 41aa170 | 2008-04-27 15:38:32 +0200 | [diff] [blame] | 1104 | |
| 1105 | pages &= (order-1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1106 | } |
Borislav Petkov | 41aa170 | 2008-04-27 15:38:32 +0200 | [diff] [blame] | 1107 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1108 | bh->b_size -= tape->excess_bh_size; |
| 1109 | if (full) |
| 1110 | atomic_sub(tape->excess_bh_size, &bh->b_count); |
Borislav Petkov | 077e3bd | 2008-04-27 15:38:34 +0200 | [diff] [blame] | 1111 | return merge_bh; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1112 | abort: |
Borislav Petkov | 077e3bd | 2008-04-27 15:38:34 +0200 | [diff] [blame] | 1113 | ide_tape_kfree_buffer(tape); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1114 | return NULL; |
| 1115 | } |
| 1116 | |
Borislav Petkov | 5a04cfa | 2008-02-06 02:57:54 +0100 | [diff] [blame] | 1117 | static int idetape_copy_stage_from_user(idetape_tape_t *tape, |
Borislav Petkov | 8646c88 | 2008-04-27 15:38:26 +0200 | [diff] [blame] | 1118 | const char __user *buf, int n) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1119 | { |
| 1120 | struct idetape_bh *bh = tape->bh; |
| 1121 | int count; |
Daniel Walker | dcd9637 | 2006-06-25 05:47:37 -0700 | [diff] [blame] | 1122 | int ret = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1123 | |
| 1124 | while (n) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1125 | if (bh == NULL) { |
Borislav Petkov | 5a04cfa | 2008-02-06 02:57:54 +0100 | [diff] [blame] | 1126 | printk(KERN_ERR "ide-tape: bh == NULL in %s\n", |
| 1127 | __func__); |
Daniel Walker | dcd9637 | 2006-06-25 05:47:37 -0700 | [diff] [blame] | 1128 | return 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1129 | } |
Borislav Petkov | 5a04cfa | 2008-02-06 02:57:54 +0100 | [diff] [blame] | 1130 | count = min((unsigned int) |
| 1131 | (bh->b_size - atomic_read(&bh->b_count)), |
| 1132 | (unsigned int)n); |
| 1133 | if (copy_from_user(bh->b_data + atomic_read(&bh->b_count), buf, |
| 1134 | count)) |
Daniel Walker | dcd9637 | 2006-06-25 05:47:37 -0700 | [diff] [blame] | 1135 | ret = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1136 | n -= count; |
| 1137 | atomic_add(count, &bh->b_count); |
| 1138 | buf += count; |
| 1139 | if (atomic_read(&bh->b_count) == bh->b_size) { |
| 1140 | bh = bh->b_reqnext; |
| 1141 | if (bh) |
| 1142 | atomic_set(&bh->b_count, 0); |
| 1143 | } |
| 1144 | } |
| 1145 | tape->bh = bh; |
Daniel Walker | dcd9637 | 2006-06-25 05:47:37 -0700 | [diff] [blame] | 1146 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1147 | } |
| 1148 | |
Borislav Petkov | 5a04cfa | 2008-02-06 02:57:54 +0100 | [diff] [blame] | 1149 | static int idetape_copy_stage_to_user(idetape_tape_t *tape, char __user *buf, |
Borislav Petkov | 99d74e6 | 2008-04-27 15:38:25 +0200 | [diff] [blame] | 1150 | int n) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1151 | { |
| 1152 | struct idetape_bh *bh = tape->bh; |
| 1153 | int count; |
Daniel Walker | dcd9637 | 2006-06-25 05:47:37 -0700 | [diff] [blame] | 1154 | int ret = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1155 | |
| 1156 | while (n) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1157 | if (bh == NULL) { |
Borislav Petkov | 5a04cfa | 2008-02-06 02:57:54 +0100 | [diff] [blame] | 1158 | printk(KERN_ERR "ide-tape: bh == NULL in %s\n", |
| 1159 | __func__); |
Daniel Walker | dcd9637 | 2006-06-25 05:47:37 -0700 | [diff] [blame] | 1160 | return 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1161 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1162 | count = min(tape->b_count, n); |
Daniel Walker | dcd9637 | 2006-06-25 05:47:37 -0700 | [diff] [blame] | 1163 | if (copy_to_user(buf, tape->b_data, count)) |
| 1164 | ret = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1165 | n -= count; |
| 1166 | tape->b_data += count; |
| 1167 | tape->b_count -= count; |
| 1168 | buf += count; |
| 1169 | if (!tape->b_count) { |
Borislav Petkov | 5a04cfa | 2008-02-06 02:57:54 +0100 | [diff] [blame] | 1170 | bh = bh->b_reqnext; |
| 1171 | tape->bh = bh; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1172 | if (bh) { |
| 1173 | tape->b_data = bh->b_data; |
| 1174 | tape->b_count = atomic_read(&bh->b_count); |
| 1175 | } |
| 1176 | } |
| 1177 | } |
Daniel Walker | dcd9637 | 2006-06-25 05:47:37 -0700 | [diff] [blame] | 1178 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1179 | } |
| 1180 | |
Borislav Petkov | 077e3bd | 2008-04-27 15:38:34 +0200 | [diff] [blame] | 1181 | static void idetape_init_merge_buffer(idetape_tape_t *tape) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1182 | { |
Borislav Petkov | 077e3bd | 2008-04-27 15:38:34 +0200 | [diff] [blame] | 1183 | struct idetape_bh *bh = tape->merge_bh; |
| 1184 | tape->bh = tape->merge_bh; |
Borislav Petkov | 5a04cfa | 2008-02-06 02:57:54 +0100 | [diff] [blame] | 1185 | |
Borislav Petkov | 54abf37 | 2008-02-06 02:57:52 +0100 | [diff] [blame] | 1186 | if (tape->chrdev_dir == IDETAPE_DIR_WRITE) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1187 | atomic_set(&bh->b_count, 0); |
| 1188 | else { |
| 1189 | tape->b_data = bh->b_data; |
| 1190 | tape->b_count = atomic_read(&bh->b_count); |
| 1191 | } |
| 1192 | } |
| 1193 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1194 | /* |
Borislav Petkov | 3c98bf3 | 2008-02-06 02:57:53 +0100 | [diff] [blame] | 1195 | * Write a filemark if write_filemark=1. Flush the device buffers without |
| 1196 | * writing a filemark otherwise. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1197 | */ |
Borislav Petkov | 5a04cfa | 2008-02-06 02:57:54 +0100 | [diff] [blame] | 1198 | static void idetape_create_write_filemark_cmd(ide_drive_t *drive, |
Borislav Petkov | d236d74 | 2008-04-18 00:46:27 +0200 | [diff] [blame] | 1199 | struct ide_atapi_pc *pc, int write_filemark) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1200 | { |
| 1201 | idetape_init_pc(pc); |
Borislav Petkov | 90699ce | 2008-02-02 19:56:50 +0100 | [diff] [blame] | 1202 | pc->c[0] = WRITE_FILEMARKS; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1203 | pc->c[4] = write_filemark; |
Borislav Petkov | 346331f | 2008-04-18 00:46:26 +0200 | [diff] [blame] | 1204 | pc->flags |= PC_FLAG_WAIT_FOR_DSC; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1205 | } |
| 1206 | |
Borislav Petkov | d236d74 | 2008-04-18 00:46:27 +0200 | [diff] [blame] | 1207 | static void idetape_create_test_unit_ready_cmd(struct ide_atapi_pc *pc) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1208 | { |
| 1209 | idetape_init_pc(pc); |
Borislav Petkov | 90699ce | 2008-02-02 19:56:50 +0100 | [diff] [blame] | 1210 | pc->c[0] = TEST_UNIT_READY; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1211 | } |
| 1212 | |
| 1213 | /* |
Borislav Petkov | 3c98bf3 | 2008-02-06 02:57:53 +0100 | [diff] [blame] | 1214 | * We add a special packet command request to the tail of the request queue, and |
Bartlomiej Zolnierkiewicz | 394a4c2 | 2008-10-10 22:39:35 +0200 | [diff] [blame^] | 1215 | * wait for it to be serviced. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1216 | */ |
Borislav Petkov | ea1ab3d | 2008-04-27 15:38:27 +0200 | [diff] [blame] | 1217 | static int idetape_queue_pc_tail(ide_drive_t *drive, struct ide_atapi_pc *pc) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1218 | { |
| 1219 | struct ide_tape_obj *tape = drive->driver_data; |
FUJITA Tomonori | 64ea1b4 | 2008-07-15 21:21:43 +0200 | [diff] [blame] | 1220 | struct request *rq; |
| 1221 | int error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1222 | |
FUJITA Tomonori | 64ea1b4 | 2008-07-15 21:21:43 +0200 | [diff] [blame] | 1223 | rq = blk_get_request(drive->queue, READ, __GFP_WAIT); |
| 1224 | rq->cmd_type = REQ_TYPE_SPECIAL; |
Borislav Petkov | 83dd573 | 2008-07-23 19:56:00 +0200 | [diff] [blame] | 1225 | rq->cmd[13] = REQ_IDETAPE_PC1; |
FUJITA Tomonori | 64ea1b4 | 2008-07-15 21:21:43 +0200 | [diff] [blame] | 1226 | rq->buffer = (char *)pc; |
Borislav Petkov | 0014c75 | 2008-07-23 19:56:00 +0200 | [diff] [blame] | 1227 | memcpy(rq->cmd, pc->c, 12); |
FUJITA Tomonori | 64ea1b4 | 2008-07-15 21:21:43 +0200 | [diff] [blame] | 1228 | error = blk_execute_rq(drive->queue, tape->disk, rq, 0); |
| 1229 | blk_put_request(rq); |
| 1230 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1231 | } |
| 1232 | |
Borislav Petkov | d236d74 | 2008-04-18 00:46:27 +0200 | [diff] [blame] | 1233 | static void idetape_create_load_unload_cmd(ide_drive_t *drive, |
| 1234 | struct ide_atapi_pc *pc, int cmd) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1235 | { |
| 1236 | idetape_init_pc(pc); |
Borislav Petkov | 90699ce | 2008-02-02 19:56:50 +0100 | [diff] [blame] | 1237 | pc->c[0] = START_STOP; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1238 | pc->c[4] = cmd; |
Borislav Petkov | 346331f | 2008-04-18 00:46:26 +0200 | [diff] [blame] | 1239 | pc->flags |= PC_FLAG_WAIT_FOR_DSC; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1240 | } |
| 1241 | |
| 1242 | static int idetape_wait_ready(ide_drive_t *drive, unsigned long timeout) |
| 1243 | { |
| 1244 | idetape_tape_t *tape = drive->driver_data; |
Borislav Petkov | d236d74 | 2008-04-18 00:46:27 +0200 | [diff] [blame] | 1245 | struct ide_atapi_pc pc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1246 | int load_attempted = 0; |
| 1247 | |
Borislav Petkov | 3c98bf3 | 2008-02-06 02:57:53 +0100 | [diff] [blame] | 1248 | /* Wait for the tape to become ready */ |
Borislav Petkov | f2e3ab5 | 2008-07-23 19:56:01 +0200 | [diff] [blame] | 1249 | set_bit(IDE_AFLAG_MEDIUM_PRESENT, &drive->atapi_flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1250 | timeout += jiffies; |
| 1251 | while (time_before(jiffies, timeout)) { |
| 1252 | idetape_create_test_unit_ready_cmd(&pc); |
Borislav Petkov | ea1ab3d | 2008-04-27 15:38:27 +0200 | [diff] [blame] | 1253 | if (!idetape_queue_pc_tail(drive, &pc)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1254 | return 0; |
| 1255 | if ((tape->sense_key == 2 && tape->asc == 4 && tape->ascq == 2) |
Borislav Petkov | 3c98bf3 | 2008-02-06 02:57:53 +0100 | [diff] [blame] | 1256 | || (tape->asc == 0x3A)) { |
| 1257 | /* no media */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1258 | if (load_attempted) |
| 1259 | return -ENOMEDIUM; |
Borislav Petkov | 5a04cfa | 2008-02-06 02:57:54 +0100 | [diff] [blame] | 1260 | idetape_create_load_unload_cmd(drive, &pc, |
| 1261 | IDETAPE_LU_LOAD_MASK); |
Borislav Petkov | ea1ab3d | 2008-04-27 15:38:27 +0200 | [diff] [blame] | 1262 | idetape_queue_pc_tail(drive, &pc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1263 | load_attempted = 1; |
| 1264 | /* not about to be ready */ |
| 1265 | } else if (!(tape->sense_key == 2 && tape->asc == 4 && |
| 1266 | (tape->ascq == 1 || tape->ascq == 8))) |
| 1267 | return -EIO; |
Nishanth Aravamudan | 80ce45f | 2005-09-10 00:27:08 -0700 | [diff] [blame] | 1268 | msleep(100); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1269 | } |
| 1270 | return -EIO; |
| 1271 | } |
| 1272 | |
Borislav Petkov | 5a04cfa | 2008-02-06 02:57:54 +0100 | [diff] [blame] | 1273 | static int idetape_flush_tape_buffers(ide_drive_t *drive) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1274 | { |
Borislav Petkov | d236d74 | 2008-04-18 00:46:27 +0200 | [diff] [blame] | 1275 | struct ide_atapi_pc pc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1276 | int rc; |
| 1277 | |
| 1278 | idetape_create_write_filemark_cmd(drive, &pc, 0); |
Borislav Petkov | 5a04cfa | 2008-02-06 02:57:54 +0100 | [diff] [blame] | 1279 | rc = idetape_queue_pc_tail(drive, &pc); |
| 1280 | if (rc) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1281 | return rc; |
| 1282 | idetape_wait_ready(drive, 60 * 5 * HZ); |
| 1283 | return 0; |
| 1284 | } |
| 1285 | |
Borislav Petkov | d236d74 | 2008-04-18 00:46:27 +0200 | [diff] [blame] | 1286 | static void idetape_create_read_position_cmd(struct ide_atapi_pc *pc) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1287 | { |
| 1288 | idetape_init_pc(pc); |
Borislav Petkov | 90699ce | 2008-02-02 19:56:50 +0100 | [diff] [blame] | 1289 | pc->c[0] = READ_POSITION; |
Borislav Petkov | d236d74 | 2008-04-18 00:46:27 +0200 | [diff] [blame] | 1290 | pc->req_xfer = 20; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1291 | } |
| 1292 | |
Borislav Petkov | 5a04cfa | 2008-02-06 02:57:54 +0100 | [diff] [blame] | 1293 | static int idetape_read_position(ide_drive_t *drive) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1294 | { |
| 1295 | idetape_tape_t *tape = drive->driver_data; |
Borislav Petkov | d236d74 | 2008-04-18 00:46:27 +0200 | [diff] [blame] | 1296 | struct ide_atapi_pc pc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1297 | int position; |
| 1298 | |
Borislav Petkov | 8004a8c | 2008-02-06 02:57:51 +0100 | [diff] [blame] | 1299 | debug_log(DBG_PROCS, "Enter %s\n", __func__); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1300 | |
| 1301 | idetape_create_read_position_cmd(&pc); |
| 1302 | if (idetape_queue_pc_tail(drive, &pc)) |
| 1303 | return -1; |
Borislav Petkov | 54bb207 | 2008-02-06 02:57:52 +0100 | [diff] [blame] | 1304 | position = tape->first_frame; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1305 | return position; |
| 1306 | } |
| 1307 | |
Borislav Petkov | d236d74 | 2008-04-18 00:46:27 +0200 | [diff] [blame] | 1308 | static void idetape_create_locate_cmd(ide_drive_t *drive, |
| 1309 | struct ide_atapi_pc *pc, |
Borislav Petkov | 5a04cfa | 2008-02-06 02:57:54 +0100 | [diff] [blame] | 1310 | unsigned int block, u8 partition, int skip) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1311 | { |
| 1312 | idetape_init_pc(pc); |
Borislav Petkov | 90699ce | 2008-02-02 19:56:50 +0100 | [diff] [blame] | 1313 | pc->c[0] = POSITION_TO_ELEMENT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1314 | pc->c[1] = 2; |
Borislav Petkov | 860ff5e | 2008-02-02 19:56:50 +0100 | [diff] [blame] | 1315 | put_unaligned(cpu_to_be32(block), (unsigned int *) &pc->c[3]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1316 | pc->c[8] = partition; |
Borislav Petkov | 346331f | 2008-04-18 00:46:26 +0200 | [diff] [blame] | 1317 | pc->flags |= PC_FLAG_WAIT_FOR_DSC; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1318 | } |
| 1319 | |
Borislav Petkov | d236d74 | 2008-04-18 00:46:27 +0200 | [diff] [blame] | 1320 | static int idetape_create_prevent_cmd(ide_drive_t *drive, |
| 1321 | struct ide_atapi_pc *pc, int prevent) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1322 | { |
| 1323 | idetape_tape_t *tape = drive->driver_data; |
| 1324 | |
Borislav Petkov | b642201 | 2008-02-02 19:56:49 +0100 | [diff] [blame] | 1325 | /* device supports locking according to capabilities page */ |
| 1326 | if (!(tape->caps[6] & 0x01)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1327 | return 0; |
| 1328 | |
| 1329 | idetape_init_pc(pc); |
Borislav Petkov | 90699ce | 2008-02-02 19:56:50 +0100 | [diff] [blame] | 1330 | pc->c[0] = ALLOW_MEDIUM_REMOVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1331 | pc->c[4] = prevent; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1332 | return 1; |
| 1333 | } |
| 1334 | |
Borislav Petkov | ec0fdb0 | 2008-04-27 15:38:34 +0200 | [diff] [blame] | 1335 | static void __ide_tape_discard_merge_buffer(ide_drive_t *drive) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1336 | { |
| 1337 | idetape_tape_t *tape = drive->driver_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1338 | |
Borislav Petkov | 54abf37 | 2008-02-06 02:57:52 +0100 | [diff] [blame] | 1339 | if (tape->chrdev_dir != IDETAPE_DIR_READ) |
Borislav Petkov | 9798630 | 2008-04-27 15:38:34 +0200 | [diff] [blame] | 1340 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1341 | |
Borislav Petkov | f2e3ab5 | 2008-07-23 19:56:01 +0200 | [diff] [blame] | 1342 | clear_bit(IDE_AFLAG_FILEMARK, &drive->atapi_flags); |
Borislav Petkov | 01a63aeb | 2008-04-27 15:38:34 +0200 | [diff] [blame] | 1343 | tape->merge_bh_size = 0; |
Borislav Petkov | 077e3bd | 2008-04-27 15:38:34 +0200 | [diff] [blame] | 1344 | if (tape->merge_bh != NULL) { |
| 1345 | ide_tape_kfree_buffer(tape); |
| 1346 | tape->merge_bh = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1347 | } |
| 1348 | |
Borislav Petkov | 54abf37 | 2008-02-06 02:57:52 +0100 | [diff] [blame] | 1349 | tape->chrdev_dir = IDETAPE_DIR_NONE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1350 | } |
| 1351 | |
| 1352 | /* |
Borislav Petkov | 3c98bf3 | 2008-02-06 02:57:53 +0100 | [diff] [blame] | 1353 | * Position the tape to the requested block using the LOCATE packet command. |
| 1354 | * A READ POSITION command is then issued to check where we are positioned. Like |
| 1355 | * all higher level operations, we queue the commands at the tail of the request |
| 1356 | * queue and wait for their completion. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1357 | */ |
Borislav Petkov | 5a04cfa | 2008-02-06 02:57:54 +0100 | [diff] [blame] | 1358 | static int idetape_position_tape(ide_drive_t *drive, unsigned int block, |
| 1359 | u8 partition, int skip) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1360 | { |
| 1361 | idetape_tape_t *tape = drive->driver_data; |
| 1362 | int retval; |
Borislav Petkov | d236d74 | 2008-04-18 00:46:27 +0200 | [diff] [blame] | 1363 | struct ide_atapi_pc pc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1364 | |
Borislav Petkov | 54abf37 | 2008-02-06 02:57:52 +0100 | [diff] [blame] | 1365 | if (tape->chrdev_dir == IDETAPE_DIR_READ) |
Borislav Petkov | ec0fdb0 | 2008-04-27 15:38:34 +0200 | [diff] [blame] | 1366 | __ide_tape_discard_merge_buffer(drive); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1367 | idetape_wait_ready(drive, 60 * 5 * HZ); |
| 1368 | idetape_create_locate_cmd(drive, &pc, block, partition, skip); |
| 1369 | retval = idetape_queue_pc_tail(drive, &pc); |
| 1370 | if (retval) |
| 1371 | return (retval); |
| 1372 | |
| 1373 | idetape_create_read_position_cmd(&pc); |
| 1374 | return (idetape_queue_pc_tail(drive, &pc)); |
| 1375 | } |
| 1376 | |
Borislav Petkov | ec0fdb0 | 2008-04-27 15:38:34 +0200 | [diff] [blame] | 1377 | static void ide_tape_discard_merge_buffer(ide_drive_t *drive, |
Borislav Petkov | 5a04cfa | 2008-02-06 02:57:54 +0100 | [diff] [blame] | 1378 | int restore_position) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1379 | { |
| 1380 | idetape_tape_t *tape = drive->driver_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1381 | int seek, position; |
| 1382 | |
Borislav Petkov | ec0fdb0 | 2008-04-27 15:38:34 +0200 | [diff] [blame] | 1383 | __ide_tape_discard_merge_buffer(drive); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1384 | if (restore_position) { |
| 1385 | position = idetape_read_position(drive); |
Borislav Petkov | 9798630 | 2008-04-27 15:38:34 +0200 | [diff] [blame] | 1386 | seek = position > 0 ? position : 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1387 | if (idetape_position_tape(drive, seek, 0, 0)) { |
Borislav Petkov | 5a04cfa | 2008-02-06 02:57:54 +0100 | [diff] [blame] | 1388 | printk(KERN_INFO "ide-tape: %s: position_tape failed in" |
Borislav Petkov | ec0fdb0 | 2008-04-27 15:38:34 +0200 | [diff] [blame] | 1389 | " %s\n", tape->name, __func__); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1390 | return; |
| 1391 | } |
| 1392 | } |
| 1393 | } |
| 1394 | |
| 1395 | /* |
Borislav Petkov | 3c98bf3 | 2008-02-06 02:57:53 +0100 | [diff] [blame] | 1396 | * Generate a read/write request for the block device interface and wait for it |
| 1397 | * to be serviced. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1398 | */ |
Borislav Petkov | 5a04cfa | 2008-02-06 02:57:54 +0100 | [diff] [blame] | 1399 | static int idetape_queue_rw_tail(ide_drive_t *drive, int cmd, int blocks, |
| 1400 | struct idetape_bh *bh) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1401 | { |
| 1402 | idetape_tape_t *tape = drive->driver_data; |
FUJITA Tomonori | 64ea1b4 | 2008-07-15 21:21:43 +0200 | [diff] [blame] | 1403 | struct request *rq; |
| 1404 | int ret, errors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1405 | |
Borislav Petkov | 8004a8c | 2008-02-06 02:57:51 +0100 | [diff] [blame] | 1406 | debug_log(DBG_SENSE, "%s: cmd=%d\n", __func__, cmd); |
| 1407 | |
FUJITA Tomonori | 64ea1b4 | 2008-07-15 21:21:43 +0200 | [diff] [blame] | 1408 | rq = blk_get_request(drive->queue, READ, __GFP_WAIT); |
| 1409 | rq->cmd_type = REQ_TYPE_SPECIAL; |
Borislav Petkov | 83dd573 | 2008-07-23 19:56:00 +0200 | [diff] [blame] | 1410 | rq->cmd[13] = cmd; |
FUJITA Tomonori | 64ea1b4 | 2008-07-15 21:21:43 +0200 | [diff] [blame] | 1411 | rq->rq_disk = tape->disk; |
| 1412 | rq->special = (void *)bh; |
| 1413 | rq->sector = tape->first_frame; |
| 1414 | rq->nr_sectors = blocks; |
| 1415 | rq->current_nr_sectors = blocks; |
| 1416 | blk_execute_rq(drive->queue, tape->disk, rq, 0); |
| 1417 | |
| 1418 | errors = rq->errors; |
| 1419 | ret = tape->blk_size * (blocks - rq->current_nr_sectors); |
| 1420 | blk_put_request(rq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1421 | |
| 1422 | if ((cmd & (REQ_IDETAPE_READ | REQ_IDETAPE_WRITE)) == 0) |
| 1423 | return 0; |
| 1424 | |
Borislav Petkov | 077e3bd | 2008-04-27 15:38:34 +0200 | [diff] [blame] | 1425 | if (tape->merge_bh) |
| 1426 | idetape_init_merge_buffer(tape); |
FUJITA Tomonori | 64ea1b4 | 2008-07-15 21:21:43 +0200 | [diff] [blame] | 1427 | if (errors == IDETAPE_ERROR_GENERAL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1428 | return -EIO; |
FUJITA Tomonori | 64ea1b4 | 2008-07-15 21:21:43 +0200 | [diff] [blame] | 1429 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1430 | } |
| 1431 | |
Borislav Petkov | d236d74 | 2008-04-18 00:46:27 +0200 | [diff] [blame] | 1432 | static void idetape_create_inquiry_cmd(struct ide_atapi_pc *pc) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1433 | { |
| 1434 | idetape_init_pc(pc); |
Borislav Petkov | 90699ce | 2008-02-02 19:56:50 +0100 | [diff] [blame] | 1435 | pc->c[0] = INQUIRY; |
Borislav Petkov | 5a04cfa | 2008-02-06 02:57:54 +0100 | [diff] [blame] | 1436 | pc->c[4] = 254; |
Borislav Petkov | d236d74 | 2008-04-18 00:46:27 +0200 | [diff] [blame] | 1437 | pc->req_xfer = 254; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1438 | } |
| 1439 | |
Borislav Petkov | d236d74 | 2008-04-18 00:46:27 +0200 | [diff] [blame] | 1440 | static void idetape_create_rewind_cmd(ide_drive_t *drive, |
| 1441 | struct ide_atapi_pc *pc) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1442 | { |
| 1443 | idetape_init_pc(pc); |
Borislav Petkov | 90699ce | 2008-02-02 19:56:50 +0100 | [diff] [blame] | 1444 | pc->c[0] = REZERO_UNIT; |
Borislav Petkov | 346331f | 2008-04-18 00:46:26 +0200 | [diff] [blame] | 1445 | pc->flags |= PC_FLAG_WAIT_FOR_DSC; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1446 | } |
| 1447 | |
Borislav Petkov | d236d74 | 2008-04-18 00:46:27 +0200 | [diff] [blame] | 1448 | static void idetape_create_erase_cmd(struct ide_atapi_pc *pc) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1449 | { |
| 1450 | idetape_init_pc(pc); |
Borislav Petkov | 90699ce | 2008-02-02 19:56:50 +0100 | [diff] [blame] | 1451 | pc->c[0] = ERASE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1452 | pc->c[1] = 1; |
Borislav Petkov | 346331f | 2008-04-18 00:46:26 +0200 | [diff] [blame] | 1453 | pc->flags |= PC_FLAG_WAIT_FOR_DSC; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1454 | } |
| 1455 | |
Borislav Petkov | d236d74 | 2008-04-18 00:46:27 +0200 | [diff] [blame] | 1456 | 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] | 1457 | { |
| 1458 | idetape_init_pc(pc); |
Borislav Petkov | 90699ce | 2008-02-02 19:56:50 +0100 | [diff] [blame] | 1459 | pc->c[0] = SPACE; |
Borislav Petkov | 860ff5e | 2008-02-02 19:56:50 +0100 | [diff] [blame] | 1460 | put_unaligned(cpu_to_be32(count), (unsigned int *) &pc->c[1]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1461 | pc->c[1] = cmd; |
Borislav Petkov | 346331f | 2008-04-18 00:46:26 +0200 | [diff] [blame] | 1462 | pc->flags |= PC_FLAG_WAIT_FOR_DSC; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1463 | } |
| 1464 | |
Borislav Petkov | 97c566c | 2008-04-27 15:38:25 +0200 | [diff] [blame] | 1465 | /* Queue up a character device originated write request. */ |
Borislav Petkov | 5a04cfa | 2008-02-06 02:57:54 +0100 | [diff] [blame] | 1466 | static int idetape_add_chrdev_write_request(ide_drive_t *drive, int blocks) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1467 | { |
| 1468 | idetape_tape_t *tape = drive->driver_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1469 | |
Borislav Petkov | 8004a8c | 2008-02-06 02:57:51 +0100 | [diff] [blame] | 1470 | debug_log(DBG_CHRDEV, "Enter %s\n", __func__); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1471 | |
Borislav Petkov | 0aa4b01 | 2008-04-27 15:38:27 +0200 | [diff] [blame] | 1472 | return idetape_queue_rw_tail(drive, REQ_IDETAPE_WRITE, |
Borislav Petkov | 077e3bd | 2008-04-27 15:38:34 +0200 | [diff] [blame] | 1473 | blocks, tape->merge_bh); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1474 | } |
| 1475 | |
Borislav Petkov | d9df937 | 2008-04-27 15:38:34 +0200 | [diff] [blame] | 1476 | static void ide_tape_flush_merge_buffer(ide_drive_t *drive) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1477 | { |
| 1478 | idetape_tape_t *tape = drive->driver_data; |
| 1479 | int blocks, min; |
| 1480 | struct idetape_bh *bh; |
Borislav Petkov | 55a5d29 | 2008-02-02 19:56:49 +0100 | [diff] [blame] | 1481 | |
Borislav Petkov | 54abf37 | 2008-02-06 02:57:52 +0100 | [diff] [blame] | 1482 | if (tape->chrdev_dir != IDETAPE_DIR_WRITE) { |
Borislav Petkov | 077e3bd | 2008-04-27 15:38:34 +0200 | [diff] [blame] | 1483 | printk(KERN_ERR "ide-tape: bug: Trying to empty merge buffer" |
Borislav Petkov | 5a04cfa | 2008-02-06 02:57:54 +0100 | [diff] [blame] | 1484 | " but we are not writing.\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1485 | return; |
| 1486 | } |
Borislav Petkov | 01a63aeb | 2008-04-27 15:38:34 +0200 | [diff] [blame] | 1487 | if (tape->merge_bh_size > tape->buffer_size) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1488 | printk(KERN_ERR "ide-tape: bug: merge_buffer too big\n"); |
Borislav Petkov | 01a63aeb | 2008-04-27 15:38:34 +0200 | [diff] [blame] | 1489 | tape->merge_bh_size = tape->buffer_size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1490 | } |
Borislav Petkov | 01a63aeb | 2008-04-27 15:38:34 +0200 | [diff] [blame] | 1491 | if (tape->merge_bh_size) { |
| 1492 | blocks = tape->merge_bh_size / tape->blk_size; |
| 1493 | if (tape->merge_bh_size % tape->blk_size) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1494 | unsigned int i; |
| 1495 | |
| 1496 | blocks++; |
Borislav Petkov | 01a63aeb | 2008-04-27 15:38:34 +0200 | [diff] [blame] | 1497 | i = tape->blk_size - tape->merge_bh_size % |
Borislav Petkov | 54bb207 | 2008-02-06 02:57:52 +0100 | [diff] [blame] | 1498 | tape->blk_size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1499 | bh = tape->bh->b_reqnext; |
| 1500 | while (bh) { |
| 1501 | atomic_set(&bh->b_count, 0); |
| 1502 | bh = bh->b_reqnext; |
| 1503 | } |
| 1504 | bh = tape->bh; |
| 1505 | while (i) { |
| 1506 | if (bh == NULL) { |
Borislav Petkov | 5a04cfa | 2008-02-06 02:57:54 +0100 | [diff] [blame] | 1507 | printk(KERN_INFO "ide-tape: bug," |
| 1508 | " bh NULL\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1509 | break; |
| 1510 | } |
Borislav Petkov | 5a04cfa | 2008-02-06 02:57:54 +0100 | [diff] [blame] | 1511 | min = min(i, (unsigned int)(bh->b_size - |
| 1512 | atomic_read(&bh->b_count))); |
| 1513 | memset(bh->b_data + atomic_read(&bh->b_count), |
| 1514 | 0, min); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1515 | atomic_add(min, &bh->b_count); |
| 1516 | i -= min; |
| 1517 | bh = bh->b_reqnext; |
| 1518 | } |
| 1519 | } |
| 1520 | (void) idetape_add_chrdev_write_request(drive, blocks); |
Borislav Petkov | 01a63aeb | 2008-04-27 15:38:34 +0200 | [diff] [blame] | 1521 | tape->merge_bh_size = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1522 | } |
Borislav Petkov | 077e3bd | 2008-04-27 15:38:34 +0200 | [diff] [blame] | 1523 | if (tape->merge_bh != NULL) { |
| 1524 | ide_tape_kfree_buffer(tape); |
| 1525 | tape->merge_bh = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1526 | } |
Borislav Petkov | 54abf37 | 2008-02-06 02:57:52 +0100 | [diff] [blame] | 1527 | tape->chrdev_dir = IDETAPE_DIR_NONE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1528 | } |
| 1529 | |
Borislav Petkov | 83042b2 | 2008-04-27 15:38:27 +0200 | [diff] [blame] | 1530 | static int idetape_init_read(ide_drive_t *drive) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1531 | { |
| 1532 | idetape_tape_t *tape = drive->driver_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1533 | int bytes_read; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1534 | |
| 1535 | /* Initialize read operation */ |
Borislav Petkov | 54abf37 | 2008-02-06 02:57:52 +0100 | [diff] [blame] | 1536 | if (tape->chrdev_dir != IDETAPE_DIR_READ) { |
| 1537 | if (tape->chrdev_dir == IDETAPE_DIR_WRITE) { |
Borislav Petkov | d9df937 | 2008-04-27 15:38:34 +0200 | [diff] [blame] | 1538 | ide_tape_flush_merge_buffer(drive); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1539 | idetape_flush_tape_buffers(drive); |
| 1540 | } |
Borislav Petkov | 077e3bd | 2008-04-27 15:38:34 +0200 | [diff] [blame] | 1541 | if (tape->merge_bh || tape->merge_bh_size) { |
Borislav Petkov | 01a63aeb | 2008-04-27 15:38:34 +0200 | [diff] [blame] | 1542 | printk(KERN_ERR "ide-tape: merge_bh_size should be" |
Borislav Petkov | 5a04cfa | 2008-02-06 02:57:54 +0100 | [diff] [blame] | 1543 | " 0 now\n"); |
Borislav Petkov | 01a63aeb | 2008-04-27 15:38:34 +0200 | [diff] [blame] | 1544 | tape->merge_bh_size = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1545 | } |
Borislav Petkov | 077e3bd | 2008-04-27 15:38:34 +0200 | [diff] [blame] | 1546 | tape->merge_bh = ide_tape_kmalloc_buffer(tape, 0, 0); |
| 1547 | if (!tape->merge_bh) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1548 | return -ENOMEM; |
Borislav Petkov | 54abf37 | 2008-02-06 02:57:52 +0100 | [diff] [blame] | 1549 | tape->chrdev_dir = IDETAPE_DIR_READ; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1550 | |
| 1551 | /* |
Borislav Petkov | 3c98bf3 | 2008-02-06 02:57:53 +0100 | [diff] [blame] | 1552 | * Issue a read 0 command to ensure that DSC handshake is |
| 1553 | * switched from completion mode to buffer available mode. |
| 1554 | * No point in issuing this if DSC overlap isn't supported, some |
| 1555 | * drives (Seagate STT3401A) will return an error. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1556 | */ |
| 1557 | if (drive->dsc_overlap) { |
Borislav Petkov | 5a04cfa | 2008-02-06 02:57:54 +0100 | [diff] [blame] | 1558 | bytes_read = idetape_queue_rw_tail(drive, |
| 1559 | REQ_IDETAPE_READ, 0, |
Borislav Petkov | 077e3bd | 2008-04-27 15:38:34 +0200 | [diff] [blame] | 1560 | tape->merge_bh); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1561 | if (bytes_read < 0) { |
Borislav Petkov | 077e3bd | 2008-04-27 15:38:34 +0200 | [diff] [blame] | 1562 | ide_tape_kfree_buffer(tape); |
| 1563 | tape->merge_bh = NULL; |
Borislav Petkov | 54abf37 | 2008-02-06 02:57:52 +0100 | [diff] [blame] | 1564 | tape->chrdev_dir = IDETAPE_DIR_NONE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1565 | return bytes_read; |
| 1566 | } |
| 1567 | } |
| 1568 | } |
Borislav Petkov | 5e69bd9 | 2008-04-27 15:38:25 +0200 | [diff] [blame] | 1569 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1570 | return 0; |
| 1571 | } |
| 1572 | |
Borislav Petkov | 5bd50dc | 2008-04-27 15:38:28 +0200 | [diff] [blame] | 1573 | /* called from idetape_chrdev_read() to service a chrdev read request. */ |
Borislav Petkov | 5a04cfa | 2008-02-06 02:57:54 +0100 | [diff] [blame] | 1574 | static int idetape_add_chrdev_read_request(ide_drive_t *drive, int blocks) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1575 | { |
| 1576 | idetape_tape_t *tape = drive->driver_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1577 | |
Borislav Petkov | 8004a8c | 2008-02-06 02:57:51 +0100 | [diff] [blame] | 1578 | debug_log(DBG_PROCS, "Enter %s, %d blocks\n", __func__, blocks); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1579 | |
Borislav Petkov | 3c98bf3 | 2008-02-06 02:57:53 +0100 | [diff] [blame] | 1580 | /* If we are at a filemark, return a read length of 0 */ |
Borislav Petkov | f2e3ab5 | 2008-07-23 19:56:01 +0200 | [diff] [blame] | 1581 | if (test_bit(IDE_AFLAG_FILEMARK, &drive->atapi_flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1582 | return 0; |
| 1583 | |
Borislav Petkov | 83042b2 | 2008-04-27 15:38:27 +0200 | [diff] [blame] | 1584 | idetape_init_read(drive); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1585 | |
Borislav Petkov | 5e69bd9 | 2008-04-27 15:38:25 +0200 | [diff] [blame] | 1586 | return idetape_queue_rw_tail(drive, REQ_IDETAPE_READ, blocks, |
Borislav Petkov | 077e3bd | 2008-04-27 15:38:34 +0200 | [diff] [blame] | 1587 | tape->merge_bh); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1588 | } |
| 1589 | |
Borislav Petkov | 5a04cfa | 2008-02-06 02:57:54 +0100 | [diff] [blame] | 1590 | static void idetape_pad_zeros(ide_drive_t *drive, int bcount) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1591 | { |
| 1592 | idetape_tape_t *tape = drive->driver_data; |
| 1593 | struct idetape_bh *bh; |
| 1594 | int blocks; |
Borislav Petkov | 3c98bf3 | 2008-02-06 02:57:53 +0100 | [diff] [blame] | 1595 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1596 | while (bcount) { |
| 1597 | unsigned int count; |
| 1598 | |
Borislav Petkov | 077e3bd | 2008-04-27 15:38:34 +0200 | [diff] [blame] | 1599 | bh = tape->merge_bh; |
Borislav Petkov | f73850a | 2008-04-27 15:38:33 +0200 | [diff] [blame] | 1600 | count = min(tape->buffer_size, bcount); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1601 | bcount -= count; |
Borislav Petkov | 54bb207 | 2008-02-06 02:57:52 +0100 | [diff] [blame] | 1602 | blocks = count / tape->blk_size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1603 | while (count) { |
Borislav Petkov | 5a04cfa | 2008-02-06 02:57:54 +0100 | [diff] [blame] | 1604 | atomic_set(&bh->b_count, |
| 1605 | min(count, (unsigned int)bh->b_size)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1606 | memset(bh->b_data, 0, atomic_read(&bh->b_count)); |
| 1607 | count -= atomic_read(&bh->b_count); |
| 1608 | bh = bh->b_reqnext; |
| 1609 | } |
Borislav Petkov | 5a04cfa | 2008-02-06 02:57:54 +0100 | [diff] [blame] | 1610 | idetape_queue_rw_tail(drive, REQ_IDETAPE_WRITE, blocks, |
Borislav Petkov | 077e3bd | 2008-04-27 15:38:34 +0200 | [diff] [blame] | 1611 | tape->merge_bh); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1612 | } |
| 1613 | } |
| 1614 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1615 | /* |
Borislav Petkov | 3c98bf3 | 2008-02-06 02:57:53 +0100 | [diff] [blame] | 1616 | * Rewinds the tape to the Beginning Of the current Partition (BOP). We |
| 1617 | * currently support only one partition. |
| 1618 | */ |
Borislav Petkov | 5a04cfa | 2008-02-06 02:57:54 +0100 | [diff] [blame] | 1619 | static int idetape_rewind_tape(ide_drive_t *drive) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1620 | { |
| 1621 | int retval; |
Borislav Petkov | d236d74 | 2008-04-18 00:46:27 +0200 | [diff] [blame] | 1622 | struct ide_atapi_pc pc; |
Borislav Petkov | 8004a8c | 2008-02-06 02:57:51 +0100 | [diff] [blame] | 1623 | idetape_tape_t *tape; |
| 1624 | tape = drive->driver_data; |
| 1625 | |
| 1626 | debug_log(DBG_SENSE, "Enter %s\n", __func__); |
| 1627 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1628 | idetape_create_rewind_cmd(drive, &pc); |
| 1629 | retval = idetape_queue_pc_tail(drive, &pc); |
| 1630 | if (retval) |
| 1631 | return retval; |
| 1632 | |
| 1633 | idetape_create_read_position_cmd(&pc); |
| 1634 | retval = idetape_queue_pc_tail(drive, &pc); |
| 1635 | if (retval) |
| 1636 | return retval; |
| 1637 | return 0; |
| 1638 | } |
| 1639 | |
Borislav Petkov | 3c98bf3 | 2008-02-06 02:57:53 +0100 | [diff] [blame] | 1640 | /* mtio.h compatible commands should be issued to the chrdev interface. */ |
Borislav Petkov | 5a04cfa | 2008-02-06 02:57:54 +0100 | [diff] [blame] | 1641 | static int idetape_blkdev_ioctl(ide_drive_t *drive, unsigned int cmd, |
| 1642 | unsigned long arg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1643 | { |
| 1644 | idetape_tape_t *tape = drive->driver_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1645 | void __user *argp = (void __user *)arg; |
| 1646 | |
Borislav Petkov | d59823f | 2008-02-02 19:56:51 +0100 | [diff] [blame] | 1647 | struct idetape_config { |
| 1648 | int dsc_rw_frequency; |
| 1649 | int dsc_media_access_frequency; |
| 1650 | int nr_stages; |
| 1651 | } config; |
| 1652 | |
Borislav Petkov | 8004a8c | 2008-02-06 02:57:51 +0100 | [diff] [blame] | 1653 | debug_log(DBG_PROCS, "Enter %s\n", __func__); |
| 1654 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1655 | switch (cmd) { |
Borislav Petkov | 5a04cfa | 2008-02-06 02:57:54 +0100 | [diff] [blame] | 1656 | case 0x0340: |
| 1657 | if (copy_from_user(&config, argp, sizeof(config))) |
| 1658 | return -EFAULT; |
| 1659 | tape->best_dsc_rw_freq = config.dsc_rw_frequency; |
Borislav Petkov | 5a04cfa | 2008-02-06 02:57:54 +0100 | [diff] [blame] | 1660 | break; |
| 1661 | case 0x0350: |
| 1662 | config.dsc_rw_frequency = (int) tape->best_dsc_rw_freq; |
Borislav Petkov | 83042b2 | 2008-04-27 15:38:27 +0200 | [diff] [blame] | 1663 | config.nr_stages = 1; |
Borislav Petkov | 5a04cfa | 2008-02-06 02:57:54 +0100 | [diff] [blame] | 1664 | if (copy_to_user(argp, &config, sizeof(config))) |
| 1665 | return -EFAULT; |
| 1666 | break; |
| 1667 | default: |
| 1668 | return -EIO; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1669 | } |
| 1670 | return 0; |
| 1671 | } |
| 1672 | |
Borislav Petkov | 5a04cfa | 2008-02-06 02:57:54 +0100 | [diff] [blame] | 1673 | static int idetape_space_over_filemarks(ide_drive_t *drive, short mt_op, |
| 1674 | int mt_count) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1675 | { |
| 1676 | idetape_tape_t *tape = drive->driver_data; |
Borislav Petkov | d236d74 | 2008-04-18 00:46:27 +0200 | [diff] [blame] | 1677 | struct ide_atapi_pc pc; |
Borislav Petkov | 5a04cfa | 2008-02-06 02:57:54 +0100 | [diff] [blame] | 1678 | int retval, count = 0; |
Borislav Petkov | b642201 | 2008-02-02 19:56:49 +0100 | [diff] [blame] | 1679 | int sprev = !!(tape->caps[4] & 0x20); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1680 | |
| 1681 | if (mt_count == 0) |
| 1682 | return 0; |
| 1683 | if (MTBSF == mt_op || MTBSFM == mt_op) { |
Borislav Petkov | b642201 | 2008-02-02 19:56:49 +0100 | [diff] [blame] | 1684 | if (!sprev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1685 | return -EIO; |
Borislav Petkov | 5a04cfa | 2008-02-06 02:57:54 +0100 | [diff] [blame] | 1686 | mt_count = -mt_count; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1687 | } |
| 1688 | |
Borislav Petkov | 54abf37 | 2008-02-06 02:57:52 +0100 | [diff] [blame] | 1689 | if (tape->chrdev_dir == IDETAPE_DIR_READ) { |
Borislav Petkov | 01a63aeb | 2008-04-27 15:38:34 +0200 | [diff] [blame] | 1690 | tape->merge_bh_size = 0; |
Borislav Petkov | f2e3ab5 | 2008-07-23 19:56:01 +0200 | [diff] [blame] | 1691 | if (test_and_clear_bit(IDE_AFLAG_FILEMARK, &drive->atapi_flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1692 | ++count; |
Borislav Petkov | ec0fdb0 | 2008-04-27 15:38:34 +0200 | [diff] [blame] | 1693 | ide_tape_discard_merge_buffer(drive, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1694 | } |
| 1695 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1696 | switch (mt_op) { |
Borislav Petkov | 5a04cfa | 2008-02-06 02:57:54 +0100 | [diff] [blame] | 1697 | case MTFSF: |
| 1698 | case MTBSF: |
| 1699 | idetape_create_space_cmd(&pc, mt_count - count, |
| 1700 | IDETAPE_SPACE_OVER_FILEMARK); |
| 1701 | return idetape_queue_pc_tail(drive, &pc); |
| 1702 | case MTFSFM: |
| 1703 | case MTBSFM: |
| 1704 | if (!sprev) |
| 1705 | return -EIO; |
| 1706 | retval = idetape_space_over_filemarks(drive, MTFSF, |
| 1707 | mt_count - count); |
| 1708 | if (retval) |
| 1709 | return retval; |
| 1710 | count = (MTBSFM == mt_op ? 1 : -1); |
| 1711 | return idetape_space_over_filemarks(drive, MTFSF, count); |
| 1712 | default: |
| 1713 | printk(KERN_ERR "ide-tape: MTIO operation %d not supported\n", |
| 1714 | mt_op); |
| 1715 | return -EIO; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1716 | } |
| 1717 | } |
| 1718 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1719 | /* |
Borislav Petkov | 3c98bf3 | 2008-02-06 02:57:53 +0100 | [diff] [blame] | 1720 | * Our character device read / write functions. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1721 | * |
Borislav Petkov | 3c98bf3 | 2008-02-06 02:57:53 +0100 | [diff] [blame] | 1722 | * The tape is optimized to maximize throughput when it is transferring an |
| 1723 | * integral number of the "continuous transfer limit", which is a parameter of |
| 1724 | * the specific tape (26kB on my particular tape, 32kB for Onstream). |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1725 | * |
Borislav Petkov | 3c98bf3 | 2008-02-06 02:57:53 +0100 | [diff] [blame] | 1726 | * As of version 1.3 of the driver, the character device provides an abstract |
| 1727 | * continuous view of the media - any mix of block sizes (even 1 byte) on the |
| 1728 | * same backup/restore procedure is supported. The driver will internally |
| 1729 | * convert the requests to the recommended transfer unit, so that an unmatch |
| 1730 | * between the user's block size to the recommended size will only result in a |
| 1731 | * (slightly) increased driver overhead, but will no longer hit performance. |
| 1732 | * This is not applicable to Onstream. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1733 | */ |
Borislav Petkov | 5a04cfa | 2008-02-06 02:57:54 +0100 | [diff] [blame] | 1734 | static ssize_t idetape_chrdev_read(struct file *file, char __user *buf, |
| 1735 | size_t count, loff_t *ppos) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1736 | { |
| 1737 | struct ide_tape_obj *tape = ide_tape_f(file); |
| 1738 | ide_drive_t *drive = tape->drive; |
Borislav Petkov | 5a04cfa | 2008-02-06 02:57:54 +0100 | [diff] [blame] | 1739 | ssize_t bytes_read, temp, actually_read = 0, rc; |
Daniel Walker | dcd9637 | 2006-06-25 05:47:37 -0700 | [diff] [blame] | 1740 | ssize_t ret = 0; |
Borislav Petkov | b642201 | 2008-02-02 19:56:49 +0100 | [diff] [blame] | 1741 | u16 ctl = *(u16 *)&tape->caps[12]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1742 | |
Borislav Petkov | 8004a8c | 2008-02-06 02:57:51 +0100 | [diff] [blame] | 1743 | debug_log(DBG_CHRDEV, "Enter %s, count %Zd\n", __func__, count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1744 | |
Borislav Petkov | 54abf37 | 2008-02-06 02:57:52 +0100 | [diff] [blame] | 1745 | if (tape->chrdev_dir != IDETAPE_DIR_READ) { |
Borislav Petkov | f2e3ab5 | 2008-07-23 19:56:01 +0200 | [diff] [blame] | 1746 | if (test_bit(IDE_AFLAG_DETECT_BS, &drive->atapi_flags)) |
Borislav Petkov | 54bb207 | 2008-02-06 02:57:52 +0100 | [diff] [blame] | 1747 | if (count > tape->blk_size && |
| 1748 | (count % tape->blk_size) == 0) |
| 1749 | tape->user_bs_factor = count / tape->blk_size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1750 | } |
Borislav Petkov | 83042b2 | 2008-04-27 15:38:27 +0200 | [diff] [blame] | 1751 | rc = idetape_init_read(drive); |
Borislav Petkov | 8d06bfa | 2008-02-06 02:57:53 +0100 | [diff] [blame] | 1752 | if (rc < 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1753 | return rc; |
| 1754 | if (count == 0) |
| 1755 | return (0); |
Borislav Petkov | 01a63aeb | 2008-04-27 15:38:34 +0200 | [diff] [blame] | 1756 | if (tape->merge_bh_size) { |
| 1757 | actually_read = min((unsigned int)(tape->merge_bh_size), |
Borislav Petkov | 5a04cfa | 2008-02-06 02:57:54 +0100 | [diff] [blame] | 1758 | (unsigned int)count); |
Borislav Petkov | 99d74e6 | 2008-04-27 15:38:25 +0200 | [diff] [blame] | 1759 | if (idetape_copy_stage_to_user(tape, buf, actually_read)) |
Daniel Walker | dcd9637 | 2006-06-25 05:47:37 -0700 | [diff] [blame] | 1760 | ret = -EFAULT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1761 | buf += actually_read; |
Borislav Petkov | 01a63aeb | 2008-04-27 15:38:34 +0200 | [diff] [blame] | 1762 | tape->merge_bh_size -= actually_read; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1763 | count -= actually_read; |
| 1764 | } |
Borislav Petkov | f73850a | 2008-04-27 15:38:33 +0200 | [diff] [blame] | 1765 | while (count >= tape->buffer_size) { |
Borislav Petkov | b642201 | 2008-02-02 19:56:49 +0100 | [diff] [blame] | 1766 | bytes_read = idetape_add_chrdev_read_request(drive, ctl); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1767 | if (bytes_read <= 0) |
| 1768 | goto finish; |
Borislav Petkov | 99d74e6 | 2008-04-27 15:38:25 +0200 | [diff] [blame] | 1769 | if (idetape_copy_stage_to_user(tape, buf, bytes_read)) |
Daniel Walker | dcd9637 | 2006-06-25 05:47:37 -0700 | [diff] [blame] | 1770 | ret = -EFAULT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1771 | buf += bytes_read; |
| 1772 | count -= bytes_read; |
| 1773 | actually_read += bytes_read; |
| 1774 | } |
| 1775 | if (count) { |
Borislav Petkov | b642201 | 2008-02-02 19:56:49 +0100 | [diff] [blame] | 1776 | bytes_read = idetape_add_chrdev_read_request(drive, ctl); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1777 | if (bytes_read <= 0) |
| 1778 | goto finish; |
| 1779 | temp = min((unsigned long)count, (unsigned long)bytes_read); |
Borislav Petkov | 99d74e6 | 2008-04-27 15:38:25 +0200 | [diff] [blame] | 1780 | if (idetape_copy_stage_to_user(tape, buf, temp)) |
Daniel Walker | dcd9637 | 2006-06-25 05:47:37 -0700 | [diff] [blame] | 1781 | ret = -EFAULT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1782 | actually_read += temp; |
Borislav Petkov | 01a63aeb | 2008-04-27 15:38:34 +0200 | [diff] [blame] | 1783 | tape->merge_bh_size = bytes_read-temp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1784 | } |
| 1785 | finish: |
Borislav Petkov | f2e3ab5 | 2008-07-23 19:56:01 +0200 | [diff] [blame] | 1786 | if (!actually_read && test_bit(IDE_AFLAG_FILEMARK, &drive->atapi_flags)) { |
Borislav Petkov | 8004a8c | 2008-02-06 02:57:51 +0100 | [diff] [blame] | 1787 | debug_log(DBG_SENSE, "%s: spacing over filemark\n", tape->name); |
| 1788 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1789 | idetape_space_over_filemarks(drive, MTFSF, 1); |
| 1790 | return 0; |
| 1791 | } |
Daniel Walker | dcd9637 | 2006-06-25 05:47:37 -0700 | [diff] [blame] | 1792 | |
Borislav Petkov | 5a04cfa | 2008-02-06 02:57:54 +0100 | [diff] [blame] | 1793 | return ret ? ret : actually_read; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1794 | } |
| 1795 | |
Borislav Petkov | 5a04cfa | 2008-02-06 02:57:54 +0100 | [diff] [blame] | 1796 | 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] | 1797 | size_t count, loff_t *ppos) |
| 1798 | { |
| 1799 | struct ide_tape_obj *tape = ide_tape_f(file); |
| 1800 | ide_drive_t *drive = tape->drive; |
Daniel Walker | dcd9637 | 2006-06-25 05:47:37 -0700 | [diff] [blame] | 1801 | ssize_t actually_written = 0; |
| 1802 | ssize_t ret = 0; |
Borislav Petkov | b642201 | 2008-02-02 19:56:49 +0100 | [diff] [blame] | 1803 | u16 ctl = *(u16 *)&tape->caps[12]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1804 | |
| 1805 | /* The drive is write protected. */ |
| 1806 | if (tape->write_prot) |
| 1807 | return -EACCES; |
| 1808 | |
Borislav Petkov | 8004a8c | 2008-02-06 02:57:51 +0100 | [diff] [blame] | 1809 | debug_log(DBG_CHRDEV, "Enter %s, count %Zd\n", __func__, count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1810 | |
| 1811 | /* Initialize write operation */ |
Borislav Petkov | 54abf37 | 2008-02-06 02:57:52 +0100 | [diff] [blame] | 1812 | if (tape->chrdev_dir != IDETAPE_DIR_WRITE) { |
| 1813 | if (tape->chrdev_dir == IDETAPE_DIR_READ) |
Borislav Petkov | ec0fdb0 | 2008-04-27 15:38:34 +0200 | [diff] [blame] | 1814 | ide_tape_discard_merge_buffer(drive, 1); |
Borislav Petkov | 077e3bd | 2008-04-27 15:38:34 +0200 | [diff] [blame] | 1815 | if (tape->merge_bh || tape->merge_bh_size) { |
Borislav Petkov | 01a63aeb | 2008-04-27 15:38:34 +0200 | [diff] [blame] | 1816 | printk(KERN_ERR "ide-tape: merge_bh_size " |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1817 | "should be 0 now\n"); |
Borislav Petkov | 01a63aeb | 2008-04-27 15:38:34 +0200 | [diff] [blame] | 1818 | tape->merge_bh_size = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1819 | } |
Borislav Petkov | 077e3bd | 2008-04-27 15:38:34 +0200 | [diff] [blame] | 1820 | tape->merge_bh = ide_tape_kmalloc_buffer(tape, 0, 0); |
| 1821 | if (!tape->merge_bh) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1822 | return -ENOMEM; |
Borislav Petkov | 54abf37 | 2008-02-06 02:57:52 +0100 | [diff] [blame] | 1823 | tape->chrdev_dir = IDETAPE_DIR_WRITE; |
Borislav Petkov | 077e3bd | 2008-04-27 15:38:34 +0200 | [diff] [blame] | 1824 | idetape_init_merge_buffer(tape); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1825 | |
| 1826 | /* |
Borislav Petkov | 3c98bf3 | 2008-02-06 02:57:53 +0100 | [diff] [blame] | 1827 | * Issue a write 0 command to ensure that DSC handshake is |
| 1828 | * switched from completion mode to buffer available mode. No |
| 1829 | * point in issuing this if DSC overlap isn't supported, some |
| 1830 | * drives (Seagate STT3401A) will return an error. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1831 | */ |
| 1832 | if (drive->dsc_overlap) { |
Borislav Petkov | 5a04cfa | 2008-02-06 02:57:54 +0100 | [diff] [blame] | 1833 | ssize_t retval = idetape_queue_rw_tail(drive, |
| 1834 | REQ_IDETAPE_WRITE, 0, |
Borislav Petkov | 077e3bd | 2008-04-27 15:38:34 +0200 | [diff] [blame] | 1835 | tape->merge_bh); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1836 | if (retval < 0) { |
Borislav Petkov | 077e3bd | 2008-04-27 15:38:34 +0200 | [diff] [blame] | 1837 | ide_tape_kfree_buffer(tape); |
| 1838 | tape->merge_bh = NULL; |
Borislav Petkov | 54abf37 | 2008-02-06 02:57:52 +0100 | [diff] [blame] | 1839 | tape->chrdev_dir = IDETAPE_DIR_NONE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1840 | return retval; |
| 1841 | } |
| 1842 | } |
| 1843 | } |
| 1844 | if (count == 0) |
| 1845 | return (0); |
Borislav Petkov | 01a63aeb | 2008-04-27 15:38:34 +0200 | [diff] [blame] | 1846 | if (tape->merge_bh_size) { |
| 1847 | if (tape->merge_bh_size >= tape->buffer_size) { |
Borislav Petkov | 5a04cfa | 2008-02-06 02:57:54 +0100 | [diff] [blame] | 1848 | printk(KERN_ERR "ide-tape: bug: merge buf too big\n"); |
Borislav Petkov | 01a63aeb | 2008-04-27 15:38:34 +0200 | [diff] [blame] | 1849 | tape->merge_bh_size = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1850 | } |
Borislav Petkov | 5a04cfa | 2008-02-06 02:57:54 +0100 | [diff] [blame] | 1851 | actually_written = min((unsigned int) |
Borislav Petkov | 01a63aeb | 2008-04-27 15:38:34 +0200 | [diff] [blame] | 1852 | (tape->buffer_size - tape->merge_bh_size), |
Borislav Petkov | 5a04cfa | 2008-02-06 02:57:54 +0100 | [diff] [blame] | 1853 | (unsigned int)count); |
Borislav Petkov | 8646c88 | 2008-04-27 15:38:26 +0200 | [diff] [blame] | 1854 | if (idetape_copy_stage_from_user(tape, buf, actually_written)) |
Daniel Walker | dcd9637 | 2006-06-25 05:47:37 -0700 | [diff] [blame] | 1855 | ret = -EFAULT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1856 | buf += actually_written; |
Borislav Petkov | 01a63aeb | 2008-04-27 15:38:34 +0200 | [diff] [blame] | 1857 | tape->merge_bh_size += actually_written; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1858 | count -= actually_written; |
| 1859 | |
Borislav Petkov | 01a63aeb | 2008-04-27 15:38:34 +0200 | [diff] [blame] | 1860 | if (tape->merge_bh_size == tape->buffer_size) { |
Daniel Walker | dcd9637 | 2006-06-25 05:47:37 -0700 | [diff] [blame] | 1861 | ssize_t retval; |
Borislav Petkov | 01a63aeb | 2008-04-27 15:38:34 +0200 | [diff] [blame] | 1862 | tape->merge_bh_size = 0; |
Borislav Petkov | b642201 | 2008-02-02 19:56:49 +0100 | [diff] [blame] | 1863 | retval = idetape_add_chrdev_write_request(drive, ctl); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1864 | if (retval <= 0) |
| 1865 | return (retval); |
| 1866 | } |
| 1867 | } |
Borislav Petkov | f73850a | 2008-04-27 15:38:33 +0200 | [diff] [blame] | 1868 | while (count >= tape->buffer_size) { |
Daniel Walker | dcd9637 | 2006-06-25 05:47:37 -0700 | [diff] [blame] | 1869 | ssize_t retval; |
Borislav Petkov | f73850a | 2008-04-27 15:38:33 +0200 | [diff] [blame] | 1870 | if (idetape_copy_stage_from_user(tape, buf, tape->buffer_size)) |
Daniel Walker | dcd9637 | 2006-06-25 05:47:37 -0700 | [diff] [blame] | 1871 | ret = -EFAULT; |
Borislav Petkov | f73850a | 2008-04-27 15:38:33 +0200 | [diff] [blame] | 1872 | buf += tape->buffer_size; |
| 1873 | count -= tape->buffer_size; |
Borislav Petkov | b642201 | 2008-02-02 19:56:49 +0100 | [diff] [blame] | 1874 | retval = idetape_add_chrdev_write_request(drive, ctl); |
Borislav Petkov | f73850a | 2008-04-27 15:38:33 +0200 | [diff] [blame] | 1875 | actually_written += tape->buffer_size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1876 | if (retval <= 0) |
| 1877 | return (retval); |
| 1878 | } |
| 1879 | if (count) { |
| 1880 | actually_written += count; |
Borislav Petkov | 8646c88 | 2008-04-27 15:38:26 +0200 | [diff] [blame] | 1881 | if (idetape_copy_stage_from_user(tape, buf, count)) |
Daniel Walker | dcd9637 | 2006-06-25 05:47:37 -0700 | [diff] [blame] | 1882 | ret = -EFAULT; |
Borislav Petkov | 01a63aeb | 2008-04-27 15:38:34 +0200 | [diff] [blame] | 1883 | tape->merge_bh_size += count; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1884 | } |
Borislav Petkov | 5a04cfa | 2008-02-06 02:57:54 +0100 | [diff] [blame] | 1885 | return ret ? ret : actually_written; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1886 | } |
| 1887 | |
Borislav Petkov | 5a04cfa | 2008-02-06 02:57:54 +0100 | [diff] [blame] | 1888 | static int idetape_write_filemark(ide_drive_t *drive) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1889 | { |
Borislav Petkov | d236d74 | 2008-04-18 00:46:27 +0200 | [diff] [blame] | 1890 | struct ide_atapi_pc pc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1891 | |
| 1892 | /* Write a filemark */ |
| 1893 | idetape_create_write_filemark_cmd(drive, &pc, 1); |
| 1894 | if (idetape_queue_pc_tail(drive, &pc)) { |
| 1895 | printk(KERN_ERR "ide-tape: Couldn't write a filemark\n"); |
| 1896 | return -EIO; |
| 1897 | } |
| 1898 | return 0; |
| 1899 | } |
| 1900 | |
| 1901 | /* |
Borislav Petkov | d99c9da | 2008-02-02 19:56:51 +0100 | [diff] [blame] | 1902 | * Called from idetape_chrdev_ioctl when the general mtio MTIOCTOP ioctl is |
| 1903 | * requested. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1904 | * |
Borislav Petkov | d99c9da | 2008-02-02 19:56:51 +0100 | [diff] [blame] | 1905 | * Note: MTBSF and MTBSFM are not supported when the tape doesn't support |
| 1906 | * 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] | 1907 | * usually not supported. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1908 | * |
Borislav Petkov | d99c9da | 2008-02-02 19:56:51 +0100 | [diff] [blame] | 1909 | * The following commands are currently not supported: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1910 | * |
Borislav Petkov | d99c9da | 2008-02-02 19:56:51 +0100 | [diff] [blame] | 1911 | * MTFSS, MTBSS, MTWSM, MTSETDENSITY, MTSETDRVBUFFER, MT_ST_BOOLEANS, |
| 1912 | * MT_ST_WRITE_THRESHOLD. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1913 | */ |
Borislav Petkov | d99c9da | 2008-02-02 19:56:51 +0100 | [diff] [blame] | 1914 | 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] | 1915 | { |
| 1916 | idetape_tape_t *tape = drive->driver_data; |
Borislav Petkov | d236d74 | 2008-04-18 00:46:27 +0200 | [diff] [blame] | 1917 | struct ide_atapi_pc pc; |
Borislav Petkov | 5a04cfa | 2008-02-06 02:57:54 +0100 | [diff] [blame] | 1918 | int i, retval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1919 | |
Borislav Petkov | 8004a8c | 2008-02-06 02:57:51 +0100 | [diff] [blame] | 1920 | debug_log(DBG_ERR, "Handling MTIOCTOP ioctl: mt_op=%d, mt_count=%d\n", |
| 1921 | mt_op, mt_count); |
Borislav Petkov | 5a04cfa | 2008-02-06 02:57:54 +0100 | [diff] [blame] | 1922 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1923 | switch (mt_op) { |
Borislav Petkov | 5a04cfa | 2008-02-06 02:57:54 +0100 | [diff] [blame] | 1924 | case MTFSF: |
| 1925 | case MTFSFM: |
| 1926 | case MTBSF: |
| 1927 | case MTBSFM: |
| 1928 | if (!mt_count) |
| 1929 | return 0; |
| 1930 | return idetape_space_over_filemarks(drive, mt_op, mt_count); |
| 1931 | default: |
| 1932 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1933 | } |
Borislav Petkov | 5a04cfa | 2008-02-06 02:57:54 +0100 | [diff] [blame] | 1934 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1935 | switch (mt_op) { |
Borislav Petkov | 5a04cfa | 2008-02-06 02:57:54 +0100 | [diff] [blame] | 1936 | case MTWEOF: |
| 1937 | if (tape->write_prot) |
| 1938 | return -EACCES; |
Borislav Petkov | ec0fdb0 | 2008-04-27 15:38:34 +0200 | [diff] [blame] | 1939 | ide_tape_discard_merge_buffer(drive, 1); |
Borislav Petkov | 5a04cfa | 2008-02-06 02:57:54 +0100 | [diff] [blame] | 1940 | for (i = 0; i < mt_count; i++) { |
| 1941 | retval = idetape_write_filemark(drive); |
| 1942 | if (retval) |
| 1943 | return retval; |
| 1944 | } |
| 1945 | return 0; |
| 1946 | case MTREW: |
Borislav Petkov | ec0fdb0 | 2008-04-27 15:38:34 +0200 | [diff] [blame] | 1947 | ide_tape_discard_merge_buffer(drive, 0); |
Borislav Petkov | 5a04cfa | 2008-02-06 02:57:54 +0100 | [diff] [blame] | 1948 | if (idetape_rewind_tape(drive)) |
| 1949 | return -EIO; |
| 1950 | return 0; |
| 1951 | case MTLOAD: |
Borislav Petkov | ec0fdb0 | 2008-04-27 15:38:34 +0200 | [diff] [blame] | 1952 | ide_tape_discard_merge_buffer(drive, 0); |
Borislav Petkov | 5a04cfa | 2008-02-06 02:57:54 +0100 | [diff] [blame] | 1953 | idetape_create_load_unload_cmd(drive, &pc, |
| 1954 | IDETAPE_LU_LOAD_MASK); |
| 1955 | return idetape_queue_pc_tail(drive, &pc); |
| 1956 | case MTUNLOAD: |
| 1957 | case MTOFFL: |
| 1958 | /* |
| 1959 | * If door is locked, attempt to unlock before |
| 1960 | * attempting to eject. |
| 1961 | */ |
| 1962 | if (tape->door_locked) { |
| 1963 | if (idetape_create_prevent_cmd(drive, &pc, 0)) |
| 1964 | if (!idetape_queue_pc_tail(drive, &pc)) |
| 1965 | tape->door_locked = DOOR_UNLOCKED; |
| 1966 | } |
Borislav Petkov | ec0fdb0 | 2008-04-27 15:38:34 +0200 | [diff] [blame] | 1967 | ide_tape_discard_merge_buffer(drive, 0); |
Borislav Petkov | 5a04cfa | 2008-02-06 02:57:54 +0100 | [diff] [blame] | 1968 | idetape_create_load_unload_cmd(drive, &pc, |
| 1969 | !IDETAPE_LU_LOAD_MASK); |
| 1970 | retval = idetape_queue_pc_tail(drive, &pc); |
| 1971 | if (!retval) |
Borislav Petkov | f2e3ab5 | 2008-07-23 19:56:01 +0200 | [diff] [blame] | 1972 | clear_bit(IDE_AFLAG_MEDIUM_PRESENT, &drive->atapi_flags); |
Borislav Petkov | 5a04cfa | 2008-02-06 02:57:54 +0100 | [diff] [blame] | 1973 | return retval; |
| 1974 | case MTNOP: |
Borislav Petkov | ec0fdb0 | 2008-04-27 15:38:34 +0200 | [diff] [blame] | 1975 | ide_tape_discard_merge_buffer(drive, 0); |
Borislav Petkov | 5a04cfa | 2008-02-06 02:57:54 +0100 | [diff] [blame] | 1976 | return idetape_flush_tape_buffers(drive); |
| 1977 | case MTRETEN: |
Borislav Petkov | ec0fdb0 | 2008-04-27 15:38:34 +0200 | [diff] [blame] | 1978 | ide_tape_discard_merge_buffer(drive, 0); |
Borislav Petkov | 5a04cfa | 2008-02-06 02:57:54 +0100 | [diff] [blame] | 1979 | idetape_create_load_unload_cmd(drive, &pc, |
| 1980 | IDETAPE_LU_RETENSION_MASK | IDETAPE_LU_LOAD_MASK); |
| 1981 | return idetape_queue_pc_tail(drive, &pc); |
| 1982 | case MTEOM: |
| 1983 | idetape_create_space_cmd(&pc, 0, IDETAPE_SPACE_TO_EOD); |
| 1984 | return idetape_queue_pc_tail(drive, &pc); |
| 1985 | case MTERASE: |
| 1986 | (void)idetape_rewind_tape(drive); |
| 1987 | idetape_create_erase_cmd(&pc); |
| 1988 | return idetape_queue_pc_tail(drive, &pc); |
| 1989 | case MTSETBLK: |
| 1990 | if (mt_count) { |
| 1991 | if (mt_count < tape->blk_size || |
| 1992 | mt_count % tape->blk_size) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1993 | return -EIO; |
Borislav Petkov | 5a04cfa | 2008-02-06 02:57:54 +0100 | [diff] [blame] | 1994 | tape->user_bs_factor = mt_count / tape->blk_size; |
Borislav Petkov | f2e3ab5 | 2008-07-23 19:56:01 +0200 | [diff] [blame] | 1995 | clear_bit(IDE_AFLAG_DETECT_BS, &drive->atapi_flags); |
Borislav Petkov | 5a04cfa | 2008-02-06 02:57:54 +0100 | [diff] [blame] | 1996 | } else |
Borislav Petkov | f2e3ab5 | 2008-07-23 19:56:01 +0200 | [diff] [blame] | 1997 | set_bit(IDE_AFLAG_DETECT_BS, &drive->atapi_flags); |
Borislav Petkov | 5a04cfa | 2008-02-06 02:57:54 +0100 | [diff] [blame] | 1998 | return 0; |
| 1999 | case MTSEEK: |
Borislav Petkov | ec0fdb0 | 2008-04-27 15:38:34 +0200 | [diff] [blame] | 2000 | ide_tape_discard_merge_buffer(drive, 0); |
Borislav Petkov | 5a04cfa | 2008-02-06 02:57:54 +0100 | [diff] [blame] | 2001 | return idetape_position_tape(drive, |
| 2002 | mt_count * tape->user_bs_factor, tape->partition, 0); |
| 2003 | case MTSETPART: |
Borislav Petkov | ec0fdb0 | 2008-04-27 15:38:34 +0200 | [diff] [blame] | 2004 | ide_tape_discard_merge_buffer(drive, 0); |
Borislav Petkov | 5a04cfa | 2008-02-06 02:57:54 +0100 | [diff] [blame] | 2005 | return idetape_position_tape(drive, 0, mt_count, 0); |
| 2006 | case MTFSR: |
| 2007 | case MTBSR: |
| 2008 | case MTLOCK: |
| 2009 | if (!idetape_create_prevent_cmd(drive, &pc, 1)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2010 | return 0; |
Borislav Petkov | 5a04cfa | 2008-02-06 02:57:54 +0100 | [diff] [blame] | 2011 | retval = idetape_queue_pc_tail(drive, &pc); |
| 2012 | if (retval) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2013 | return retval; |
Borislav Petkov | 5a04cfa | 2008-02-06 02:57:54 +0100 | [diff] [blame] | 2014 | tape->door_locked = DOOR_EXPLICITLY_LOCKED; |
| 2015 | return 0; |
| 2016 | case MTUNLOCK: |
| 2017 | if (!idetape_create_prevent_cmd(drive, &pc, 0)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2018 | return 0; |
Borislav Petkov | 5a04cfa | 2008-02-06 02:57:54 +0100 | [diff] [blame] | 2019 | retval = idetape_queue_pc_tail(drive, &pc); |
| 2020 | if (retval) |
| 2021 | return retval; |
| 2022 | tape->door_locked = DOOR_UNLOCKED; |
| 2023 | return 0; |
| 2024 | default: |
| 2025 | printk(KERN_ERR "ide-tape: MTIO operation %d not supported\n", |
| 2026 | mt_op); |
| 2027 | return -EIO; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2028 | } |
| 2029 | } |
| 2030 | |
| 2031 | /* |
Borislav Petkov | d99c9da | 2008-02-02 19:56:51 +0100 | [diff] [blame] | 2032 | * Our character device ioctls. General mtio.h magnetic io commands are |
| 2033 | * supported here, and not in the corresponding block interface. Our own |
| 2034 | * ide-tape ioctls are supported on both interfaces. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2035 | */ |
Borislav Petkov | d99c9da | 2008-02-02 19:56:51 +0100 | [diff] [blame] | 2036 | static int idetape_chrdev_ioctl(struct inode *inode, struct file *file, |
| 2037 | unsigned int cmd, unsigned long arg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2038 | { |
| 2039 | struct ide_tape_obj *tape = ide_tape_f(file); |
| 2040 | ide_drive_t *drive = tape->drive; |
| 2041 | struct mtop mtop; |
| 2042 | struct mtget mtget; |
| 2043 | struct mtpos mtpos; |
Borislav Petkov | 54bb207 | 2008-02-06 02:57:52 +0100 | [diff] [blame] | 2044 | int block_offset = 0, position = tape->first_frame; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2045 | void __user *argp = (void __user *)arg; |
| 2046 | |
Borislav Petkov | 8004a8c | 2008-02-06 02:57:51 +0100 | [diff] [blame] | 2047 | debug_log(DBG_CHRDEV, "Enter %s, cmd=%u\n", __func__, cmd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2048 | |
Borislav Petkov | 54abf37 | 2008-02-06 02:57:52 +0100 | [diff] [blame] | 2049 | if (tape->chrdev_dir == IDETAPE_DIR_WRITE) { |
Borislav Petkov | d9df937 | 2008-04-27 15:38:34 +0200 | [diff] [blame] | 2050 | ide_tape_flush_merge_buffer(drive); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2051 | idetape_flush_tape_buffers(drive); |
| 2052 | } |
| 2053 | if (cmd == MTIOCGET || cmd == MTIOCPOS) { |
Borislav Petkov | 01a63aeb | 2008-04-27 15:38:34 +0200 | [diff] [blame] | 2054 | block_offset = tape->merge_bh_size / |
Borislav Petkov | 54bb207 | 2008-02-06 02:57:52 +0100 | [diff] [blame] | 2055 | (tape->blk_size * tape->user_bs_factor); |
Borislav Petkov | 5a04cfa | 2008-02-06 02:57:54 +0100 | [diff] [blame] | 2056 | position = idetape_read_position(drive); |
| 2057 | if (position < 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2058 | return -EIO; |
| 2059 | } |
| 2060 | switch (cmd) { |
Borislav Petkov | 5a04cfa | 2008-02-06 02:57:54 +0100 | [diff] [blame] | 2061 | case MTIOCTOP: |
| 2062 | if (copy_from_user(&mtop, argp, sizeof(struct mtop))) |
| 2063 | return -EFAULT; |
| 2064 | return idetape_mtioctop(drive, mtop.mt_op, mtop.mt_count); |
| 2065 | case MTIOCGET: |
| 2066 | memset(&mtget, 0, sizeof(struct mtget)); |
| 2067 | mtget.mt_type = MT_ISSCSI2; |
| 2068 | mtget.mt_blkno = position / tape->user_bs_factor - block_offset; |
| 2069 | mtget.mt_dsreg = |
| 2070 | ((tape->blk_size * tape->user_bs_factor) |
| 2071 | << MT_ST_BLKSIZE_SHIFT) & MT_ST_BLKSIZE_MASK; |
Borislav Petkov | 54bb207 | 2008-02-06 02:57:52 +0100 | [diff] [blame] | 2072 | |
Borislav Petkov | 5a04cfa | 2008-02-06 02:57:54 +0100 | [diff] [blame] | 2073 | if (tape->drv_write_prot) |
| 2074 | mtget.mt_gstat |= GMT_WR_PROT(0xffffffff); |
| 2075 | |
| 2076 | if (copy_to_user(argp, &mtget, sizeof(struct mtget))) |
| 2077 | return -EFAULT; |
| 2078 | return 0; |
| 2079 | case MTIOCPOS: |
| 2080 | mtpos.mt_blkno = position / tape->user_bs_factor - block_offset; |
| 2081 | if (copy_to_user(argp, &mtpos, sizeof(struct mtpos))) |
| 2082 | return -EFAULT; |
| 2083 | return 0; |
| 2084 | default: |
| 2085 | if (tape->chrdev_dir == IDETAPE_DIR_READ) |
Borislav Petkov | ec0fdb0 | 2008-04-27 15:38:34 +0200 | [diff] [blame] | 2086 | ide_tape_discard_merge_buffer(drive, 1); |
Borislav Petkov | 5a04cfa | 2008-02-06 02:57:54 +0100 | [diff] [blame] | 2087 | return idetape_blkdev_ioctl(drive, cmd, arg); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2088 | } |
| 2089 | } |
| 2090 | |
Borislav Petkov | 3cffb9c | 2008-02-02 19:56:50 +0100 | [diff] [blame] | 2091 | /* |
| 2092 | * Do a mode sense page 0 with block descriptor and if it succeeds set the tape |
| 2093 | * block size with the reported value. |
| 2094 | */ |
| 2095 | static void ide_tape_get_bsize_from_bdesc(ide_drive_t *drive) |
| 2096 | { |
| 2097 | idetape_tape_t *tape = drive->driver_data; |
Borislav Petkov | d236d74 | 2008-04-18 00:46:27 +0200 | [diff] [blame] | 2098 | struct ide_atapi_pc pc; |
Borislav Petkov | 3cffb9c | 2008-02-02 19:56:50 +0100 | [diff] [blame] | 2099 | |
| 2100 | idetape_create_mode_sense_cmd(&pc, IDETAPE_BLOCK_DESCRIPTOR); |
| 2101 | if (idetape_queue_pc_tail(drive, &pc)) { |
| 2102 | printk(KERN_ERR "ide-tape: Can't get block descriptor\n"); |
Borislav Petkov | 54bb207 | 2008-02-06 02:57:52 +0100 | [diff] [blame] | 2103 | if (tape->blk_size == 0) { |
Borislav Petkov | 3cffb9c | 2008-02-02 19:56:50 +0100 | [diff] [blame] | 2104 | printk(KERN_WARNING "ide-tape: Cannot deal with zero " |
| 2105 | "block size, assuming 32k\n"); |
Borislav Petkov | 54bb207 | 2008-02-06 02:57:52 +0100 | [diff] [blame] | 2106 | tape->blk_size = 32768; |
Borislav Petkov | 3cffb9c | 2008-02-02 19:56:50 +0100 | [diff] [blame] | 2107 | } |
| 2108 | return; |
| 2109 | } |
Borislav Petkov | d236d74 | 2008-04-18 00:46:27 +0200 | [diff] [blame] | 2110 | tape->blk_size = (pc.buf[4 + 5] << 16) + |
| 2111 | (pc.buf[4 + 6] << 8) + |
| 2112 | pc.buf[4 + 7]; |
| 2113 | tape->drv_write_prot = (pc.buf[2] & 0x80) >> 7; |
Borislav Petkov | 3cffb9c | 2008-02-02 19:56:50 +0100 | [diff] [blame] | 2114 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2115 | |
Borislav Petkov | 5a04cfa | 2008-02-06 02:57:54 +0100 | [diff] [blame] | 2116 | static int idetape_chrdev_open(struct inode *inode, struct file *filp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2117 | { |
| 2118 | unsigned int minor = iminor(inode), i = minor & ~0xc0; |
| 2119 | ide_drive_t *drive; |
| 2120 | idetape_tape_t *tape; |
Borislav Petkov | d236d74 | 2008-04-18 00:46:27 +0200 | [diff] [blame] | 2121 | struct ide_atapi_pc pc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2122 | int retval; |
| 2123 | |
Borislav Petkov | 8004a8c | 2008-02-06 02:57:51 +0100 | [diff] [blame] | 2124 | if (i >= MAX_HWIFS * MAX_DRIVES) |
| 2125 | return -ENXIO; |
| 2126 | |
Jonathan Corbet | 04f4ac9 | 2008-05-15 12:01:56 -0600 | [diff] [blame] | 2127 | lock_kernel(); |
Borislav Petkov | 8004a8c | 2008-02-06 02:57:51 +0100 | [diff] [blame] | 2128 | tape = ide_tape_chrdev_get(i); |
Jonathan Corbet | 04f4ac9 | 2008-05-15 12:01:56 -0600 | [diff] [blame] | 2129 | if (!tape) { |
| 2130 | unlock_kernel(); |
Borislav Petkov | 8004a8c | 2008-02-06 02:57:51 +0100 | [diff] [blame] | 2131 | return -ENXIO; |
Jonathan Corbet | 04f4ac9 | 2008-05-15 12:01:56 -0600 | [diff] [blame] | 2132 | } |
Borislav Petkov | 8004a8c | 2008-02-06 02:57:51 +0100 | [diff] [blame] | 2133 | |
| 2134 | debug_log(DBG_CHRDEV, "Enter %s\n", __func__); |
| 2135 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2136 | /* |
| 2137 | * We really want to do nonseekable_open(inode, filp); here, but some |
| 2138 | * versions of tar incorrectly call lseek on tapes and bail out if that |
| 2139 | * fails. So we disallow pread() and pwrite(), but permit lseeks. |
| 2140 | */ |
| 2141 | filp->f_mode &= ~(FMODE_PREAD | FMODE_PWRITE); |
| 2142 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2143 | drive = tape->drive; |
| 2144 | |
| 2145 | filp->private_data = tape; |
| 2146 | |
Borislav Petkov | f2e3ab5 | 2008-07-23 19:56:01 +0200 | [diff] [blame] | 2147 | if (test_and_set_bit(IDE_AFLAG_BUSY, &drive->atapi_flags)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2148 | retval = -EBUSY; |
| 2149 | goto out_put_tape; |
| 2150 | } |
| 2151 | |
| 2152 | retval = idetape_wait_ready(drive, 60 * HZ); |
| 2153 | if (retval) { |
Borislav Petkov | f2e3ab5 | 2008-07-23 19:56:01 +0200 | [diff] [blame] | 2154 | clear_bit(IDE_AFLAG_BUSY, &drive->atapi_flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2155 | printk(KERN_ERR "ide-tape: %s: drive not ready\n", tape->name); |
| 2156 | goto out_put_tape; |
| 2157 | } |
| 2158 | |
| 2159 | idetape_read_position(drive); |
Borislav Petkov | f2e3ab5 | 2008-07-23 19:56:01 +0200 | [diff] [blame] | 2160 | if (!test_bit(IDE_AFLAG_ADDRESS_VALID, &drive->atapi_flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2161 | (void)idetape_rewind_tape(drive); |
| 2162 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2163 | /* Read block size and write protect status from drive. */ |
Borislav Petkov | 3cffb9c | 2008-02-02 19:56:50 +0100 | [diff] [blame] | 2164 | ide_tape_get_bsize_from_bdesc(drive); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2165 | |
| 2166 | /* Set write protect flag if device is opened as read-only. */ |
| 2167 | if ((filp->f_flags & O_ACCMODE) == O_RDONLY) |
| 2168 | tape->write_prot = 1; |
| 2169 | else |
| 2170 | tape->write_prot = tape->drv_write_prot; |
| 2171 | |
| 2172 | /* Make sure drive isn't write protected if user wants to write. */ |
| 2173 | if (tape->write_prot) { |
| 2174 | if ((filp->f_flags & O_ACCMODE) == O_WRONLY || |
| 2175 | (filp->f_flags & O_ACCMODE) == O_RDWR) { |
Borislav Petkov | f2e3ab5 | 2008-07-23 19:56:01 +0200 | [diff] [blame] | 2176 | clear_bit(IDE_AFLAG_BUSY, &drive->atapi_flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2177 | retval = -EROFS; |
| 2178 | goto out_put_tape; |
| 2179 | } |
| 2180 | } |
| 2181 | |
Borislav Petkov | 3c98bf3 | 2008-02-06 02:57:53 +0100 | [diff] [blame] | 2182 | /* Lock the tape drive door so user can't eject. */ |
Borislav Petkov | 54abf37 | 2008-02-06 02:57:52 +0100 | [diff] [blame] | 2183 | if (tape->chrdev_dir == IDETAPE_DIR_NONE) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2184 | if (idetape_create_prevent_cmd(drive, &pc, 1)) { |
| 2185 | if (!idetape_queue_pc_tail(drive, &pc)) { |
| 2186 | if (tape->door_locked != DOOR_EXPLICITLY_LOCKED) |
| 2187 | tape->door_locked = DOOR_LOCKED; |
| 2188 | } |
| 2189 | } |
| 2190 | } |
Jonathan Corbet | 04f4ac9 | 2008-05-15 12:01:56 -0600 | [diff] [blame] | 2191 | unlock_kernel(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2192 | return 0; |
| 2193 | |
| 2194 | out_put_tape: |
| 2195 | ide_tape_put(tape); |
Jonathan Corbet | 04f4ac9 | 2008-05-15 12:01:56 -0600 | [diff] [blame] | 2196 | unlock_kernel(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2197 | return retval; |
| 2198 | } |
| 2199 | |
Borislav Petkov | 5a04cfa | 2008-02-06 02:57:54 +0100 | [diff] [blame] | 2200 | static void idetape_write_release(ide_drive_t *drive, unsigned int minor) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2201 | { |
| 2202 | idetape_tape_t *tape = drive->driver_data; |
| 2203 | |
Borislav Petkov | d9df937 | 2008-04-27 15:38:34 +0200 | [diff] [blame] | 2204 | ide_tape_flush_merge_buffer(drive); |
Borislav Petkov | 077e3bd | 2008-04-27 15:38:34 +0200 | [diff] [blame] | 2205 | tape->merge_bh = ide_tape_kmalloc_buffer(tape, 1, 0); |
| 2206 | if (tape->merge_bh != NULL) { |
Borislav Petkov | 54bb207 | 2008-02-06 02:57:52 +0100 | [diff] [blame] | 2207 | idetape_pad_zeros(drive, tape->blk_size * |
| 2208 | (tape->user_bs_factor - 1)); |
Borislav Petkov | 077e3bd | 2008-04-27 15:38:34 +0200 | [diff] [blame] | 2209 | ide_tape_kfree_buffer(tape); |
| 2210 | tape->merge_bh = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2211 | } |
| 2212 | idetape_write_filemark(drive); |
| 2213 | idetape_flush_tape_buffers(drive); |
| 2214 | idetape_flush_tape_buffers(drive); |
| 2215 | } |
| 2216 | |
Borislav Petkov | 5a04cfa | 2008-02-06 02:57:54 +0100 | [diff] [blame] | 2217 | static int idetape_chrdev_release(struct inode *inode, struct file *filp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2218 | { |
| 2219 | struct ide_tape_obj *tape = ide_tape_f(filp); |
| 2220 | ide_drive_t *drive = tape->drive; |
Borislav Petkov | d236d74 | 2008-04-18 00:46:27 +0200 | [diff] [blame] | 2221 | struct ide_atapi_pc pc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2222 | unsigned int minor = iminor(inode); |
| 2223 | |
| 2224 | lock_kernel(); |
| 2225 | tape = drive->driver_data; |
Borislav Petkov | 8004a8c | 2008-02-06 02:57:51 +0100 | [diff] [blame] | 2226 | |
| 2227 | debug_log(DBG_CHRDEV, "Enter %s\n", __func__); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2228 | |
Borislav Petkov | 54abf37 | 2008-02-06 02:57:52 +0100 | [diff] [blame] | 2229 | if (tape->chrdev_dir == IDETAPE_DIR_WRITE) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2230 | idetape_write_release(drive, minor); |
Borislav Petkov | 54abf37 | 2008-02-06 02:57:52 +0100 | [diff] [blame] | 2231 | if (tape->chrdev_dir == IDETAPE_DIR_READ) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2232 | if (minor < 128) |
Borislav Petkov | ec0fdb0 | 2008-04-27 15:38:34 +0200 | [diff] [blame] | 2233 | ide_tape_discard_merge_buffer(drive, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2234 | } |
Borislav Petkov | f64eee7 | 2008-04-27 15:38:25 +0200 | [diff] [blame] | 2235 | |
Borislav Petkov | f2e3ab5 | 2008-07-23 19:56:01 +0200 | [diff] [blame] | 2236 | if (minor < 128 && test_bit(IDE_AFLAG_MEDIUM_PRESENT, &drive->atapi_flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2237 | (void) idetape_rewind_tape(drive); |
Borislav Petkov | 54abf37 | 2008-02-06 02:57:52 +0100 | [diff] [blame] | 2238 | if (tape->chrdev_dir == IDETAPE_DIR_NONE) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2239 | if (tape->door_locked == DOOR_LOCKED) { |
| 2240 | if (idetape_create_prevent_cmd(drive, &pc, 0)) { |
| 2241 | if (!idetape_queue_pc_tail(drive, &pc)) |
| 2242 | tape->door_locked = DOOR_UNLOCKED; |
| 2243 | } |
| 2244 | } |
| 2245 | } |
Borislav Petkov | f2e3ab5 | 2008-07-23 19:56:01 +0200 | [diff] [blame] | 2246 | clear_bit(IDE_AFLAG_BUSY, &drive->atapi_flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2247 | ide_tape_put(tape); |
| 2248 | unlock_kernel(); |
| 2249 | return 0; |
| 2250 | } |
| 2251 | |
Borislav Petkov | 6d29c8f | 2008-02-02 19:56:49 +0100 | [diff] [blame] | 2252 | static void idetape_get_inquiry_results(ide_drive_t *drive) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2253 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2254 | idetape_tape_t *tape = drive->driver_data; |
Borislav Petkov | d236d74 | 2008-04-18 00:46:27 +0200 | [diff] [blame] | 2255 | struct ide_atapi_pc pc; |
Borislav Petkov | 801bd32 | 2008-09-27 19:32:17 +0200 | [diff] [blame] | 2256 | char fw_rev[4], vendor_id[8], product_id[16]; |
Borislav Petkov | 6d29c8f | 2008-02-02 19:56:49 +0100 | [diff] [blame] | 2257 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2258 | idetape_create_inquiry_cmd(&pc); |
| 2259 | if (idetape_queue_pc_tail(drive, &pc)) { |
Borislav Petkov | 6d29c8f | 2008-02-02 19:56:49 +0100 | [diff] [blame] | 2260 | printk(KERN_ERR "ide-tape: %s: can't get INQUIRY results\n", |
| 2261 | tape->name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2262 | return; |
| 2263 | } |
Borislav Petkov | d236d74 | 2008-04-18 00:46:27 +0200 | [diff] [blame] | 2264 | memcpy(vendor_id, &pc.buf[8], 8); |
| 2265 | memcpy(product_id, &pc.buf[16], 16); |
| 2266 | memcpy(fw_rev, &pc.buf[32], 4); |
Borislav Petkov | 6d29c8f | 2008-02-02 19:56:49 +0100 | [diff] [blame] | 2267 | |
Borislav Petkov | 801bd32 | 2008-09-27 19:32:17 +0200 | [diff] [blame] | 2268 | ide_fixstring(vendor_id, 8, 0); |
| 2269 | ide_fixstring(product_id, 16, 0); |
| 2270 | ide_fixstring(fw_rev, 4, 0); |
Borislav Petkov | 41f81d54 | 2008-02-06 02:57:52 +0100 | [diff] [blame] | 2271 | |
Borislav Petkov | 801bd32 | 2008-09-27 19:32:17 +0200 | [diff] [blame] | 2272 | printk(KERN_INFO "ide-tape: %s <-> %s: %.8s %.16s rev %.4s\n", |
Borislav Petkov | 41f81d54 | 2008-02-06 02:57:52 +0100 | [diff] [blame] | 2273 | drive->name, tape->name, vendor_id, product_id, fw_rev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2274 | } |
| 2275 | |
| 2276 | /* |
Borislav Petkov | b642201 | 2008-02-02 19:56:49 +0100 | [diff] [blame] | 2277 | * Ask the tape about its various parameters. In particular, we will adjust our |
| 2278 | * 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] | 2279 | */ |
Borislav Petkov | 5a04cfa | 2008-02-06 02:57:54 +0100 | [diff] [blame] | 2280 | static void idetape_get_mode_sense_results(ide_drive_t *drive) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2281 | { |
| 2282 | idetape_tape_t *tape = drive->driver_data; |
Borislav Petkov | d236d74 | 2008-04-18 00:46:27 +0200 | [diff] [blame] | 2283 | struct ide_atapi_pc pc; |
Borislav Petkov | b642201 | 2008-02-02 19:56:49 +0100 | [diff] [blame] | 2284 | u8 *caps; |
| 2285 | u8 speed, max_speed; |
Borislav Petkov | 47314fa | 2008-02-02 19:56:48 +0100 | [diff] [blame] | 2286 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2287 | idetape_create_mode_sense_cmd(&pc, IDETAPE_CAPABILITIES_PAGE); |
| 2288 | if (idetape_queue_pc_tail(drive, &pc)) { |
Borislav Petkov | b642201 | 2008-02-02 19:56:49 +0100 | [diff] [blame] | 2289 | printk(KERN_ERR "ide-tape: Can't get tape parameters - assuming" |
| 2290 | " some default values\n"); |
Borislav Petkov | 54bb207 | 2008-02-06 02:57:52 +0100 | [diff] [blame] | 2291 | tape->blk_size = 512; |
Borislav Petkov | b642201 | 2008-02-02 19:56:49 +0100 | [diff] [blame] | 2292 | put_unaligned(52, (u16 *)&tape->caps[12]); |
| 2293 | put_unaligned(540, (u16 *)&tape->caps[14]); |
| 2294 | put_unaligned(6*52, (u16 *)&tape->caps[16]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2295 | return; |
| 2296 | } |
Borislav Petkov | d236d74 | 2008-04-18 00:46:27 +0200 | [diff] [blame] | 2297 | caps = pc.buf + 4 + pc.buf[3]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2298 | |
Borislav Petkov | b642201 | 2008-02-02 19:56:49 +0100 | [diff] [blame] | 2299 | /* convert to host order and save for later use */ |
Harvey Harrison | cd740ab | 2008-07-24 22:53:33 +0200 | [diff] [blame] | 2300 | speed = be16_to_cpup((__be16 *)&caps[14]); |
| 2301 | max_speed = be16_to_cpup((__be16 *)&caps[8]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2302 | |
Harvey Harrison | cd740ab | 2008-07-24 22:53:33 +0200 | [diff] [blame] | 2303 | *(u16 *)&caps[8] = max_speed; |
| 2304 | *(u16 *)&caps[12] = be16_to_cpup((__be16 *)&caps[12]); |
| 2305 | *(u16 *)&caps[14] = speed; |
| 2306 | *(u16 *)&caps[16] = be16_to_cpup((__be16 *)&caps[16]); |
Borislav Petkov | b642201 | 2008-02-02 19:56:49 +0100 | [diff] [blame] | 2307 | |
| 2308 | if (!speed) { |
| 2309 | printk(KERN_INFO "ide-tape: %s: invalid tape speed " |
| 2310 | "(assuming 650KB/sec)\n", drive->name); |
Harvey Harrison | cd740ab | 2008-07-24 22:53:33 +0200 | [diff] [blame] | 2311 | *(u16 *)&caps[14] = 650; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2312 | } |
Borislav Petkov | b642201 | 2008-02-02 19:56:49 +0100 | [diff] [blame] | 2313 | if (!max_speed) { |
| 2314 | printk(KERN_INFO "ide-tape: %s: invalid max_speed " |
| 2315 | "(assuming 650KB/sec)\n", drive->name); |
Harvey Harrison | cd740ab | 2008-07-24 22:53:33 +0200 | [diff] [blame] | 2316 | *(u16 *)&caps[8] = 650; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2317 | } |
| 2318 | |
Borislav Petkov | b642201 | 2008-02-02 19:56:49 +0100 | [diff] [blame] | 2319 | memcpy(&tape->caps, caps, 20); |
| 2320 | if (caps[7] & 0x02) |
Borislav Petkov | 54bb207 | 2008-02-06 02:57:52 +0100 | [diff] [blame] | 2321 | tape->blk_size = 512; |
Borislav Petkov | b642201 | 2008-02-02 19:56:49 +0100 | [diff] [blame] | 2322 | else if (caps[7] & 0x04) |
Borislav Petkov | 54bb207 | 2008-02-06 02:57:52 +0100 | [diff] [blame] | 2323 | tape->blk_size = 1024; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2324 | } |
| 2325 | |
Bartlomiej Zolnierkiewicz | 7662d04 | 2007-05-10 00:01:10 +0200 | [diff] [blame] | 2326 | #ifdef CONFIG_IDE_PROC_FS |
Bartlomiej Zolnierkiewicz | 8185d5a | 2008-10-10 22:39:28 +0200 | [diff] [blame] | 2327 | #define ide_tape_devset_get(name, field) \ |
| 2328 | static int get_##name(ide_drive_t *drive) \ |
| 2329 | { \ |
| 2330 | idetape_tape_t *tape = drive->driver_data; \ |
| 2331 | return tape->field; \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2332 | } |
Bartlomiej Zolnierkiewicz | 8185d5a | 2008-10-10 22:39:28 +0200 | [diff] [blame] | 2333 | |
| 2334 | #define ide_tape_devset_set(name, field) \ |
| 2335 | static int set_##name(ide_drive_t *drive, int arg) \ |
| 2336 | { \ |
| 2337 | idetape_tape_t *tape = drive->driver_data; \ |
| 2338 | tape->field = arg; \ |
| 2339 | return 0; \ |
| 2340 | } |
| 2341 | |
| 2342 | #define ide_tape_devset_rw(_name, _min, _max, _field, _mulf, _divf) \ |
| 2343 | ide_tape_devset_get(_name, _field) \ |
| 2344 | ide_tape_devset_set(_name, _field) \ |
| 2345 | __IDE_DEVSET(_name, S_RW, _min, _max, get_##_name, set_##_name, _mulf, _divf) |
| 2346 | |
| 2347 | #define ide_tape_devset_r(_name, _min, _max, _field, _mulf, _divf) \ |
| 2348 | ide_tape_devset_get(_name, _field) \ |
| 2349 | __IDE_DEVSET(_name, S_READ, _min, _max, get_##_name, NULL, _mulf, _divf) |
| 2350 | |
| 2351 | static int mulf_tdsc(ide_drive_t *drive) { return 1000; } |
| 2352 | static int divf_tdsc(ide_drive_t *drive) { return HZ; } |
| 2353 | static int divf_buffer(ide_drive_t *drive) { return 2; } |
| 2354 | static int divf_buffer_size(ide_drive_t *drive) { return 1024; } |
| 2355 | |
| 2356 | ide_devset_rw(dsc_overlap, 0, 1, dsc_overlap); |
| 2357 | |
| 2358 | ide_tape_devset_rw(debug_mask, 0, 0xffff, debug_mask, NULL, NULL); |
| 2359 | ide_tape_devset_rw(tdsc, IDETAPE_DSC_RW_MIN, IDETAPE_DSC_RW_MAX, |
| 2360 | best_dsc_rw_freq, mulf_tdsc, divf_tdsc); |
| 2361 | |
| 2362 | ide_tape_devset_r(avg_speed, 0, 0xffff, avg_speed, NULL, NULL); |
| 2363 | ide_tape_devset_r(speed, 0, 0xffff, caps[14], NULL, NULL); |
| 2364 | ide_tape_devset_r(buffer, 0, 0xffff, caps[16], NULL, divf_buffer); |
| 2365 | ide_tape_devset_r(buffer_size, 0, 0xffff, buffer_size, NULL, divf_buffer_size); |
| 2366 | |
| 2367 | static const struct ide_devset *idetape_settings[] = { |
| 2368 | &ide_devset_avg_speed, |
| 2369 | &ide_devset_buffer, |
| 2370 | &ide_devset_buffer_size, |
| 2371 | &ide_devset_debug_mask, |
| 2372 | &ide_devset_dsc_overlap, |
| 2373 | &ide_devset_speed, |
| 2374 | &ide_devset_tdsc, |
| 2375 | NULL |
| 2376 | }; |
Bartlomiej Zolnierkiewicz | 7662d04 | 2007-05-10 00:01:10 +0200 | [diff] [blame] | 2377 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2378 | |
| 2379 | /* |
Borislav Petkov | 3c98bf3 | 2008-02-06 02:57:53 +0100 | [diff] [blame] | 2380 | * The function below is called to: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2381 | * |
Borislav Petkov | 3c98bf3 | 2008-02-06 02:57:53 +0100 | [diff] [blame] | 2382 | * 1. Initialize our various state variables. |
| 2383 | * 2. Ask the tape for its capabilities. |
| 2384 | * 3. Allocate a buffer which will be used for data transfer. The buffer size |
| 2385 | * is chosen based on the recommendation which we received in step 2. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2386 | * |
Borislav Petkov | 3c98bf3 | 2008-02-06 02:57:53 +0100 | [diff] [blame] | 2387 | * Note that at this point ide.c already assigned us an irq, so that we can |
| 2388 | * queue requests here and wait for their completion. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2389 | */ |
Borislav Petkov | 5a04cfa | 2008-02-06 02:57:54 +0100 | [diff] [blame] | 2390 | 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] | 2391 | { |
Borislav Petkov | 83042b2 | 2008-04-27 15:38:27 +0200 | [diff] [blame] | 2392 | unsigned long t; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2393 | int speed; |
Borislav Petkov | f73850a | 2008-04-27 15:38:33 +0200 | [diff] [blame] | 2394 | int buffer_size; |
Borislav Petkov | 71071b8 | 2008-02-06 02:57:53 +0100 | [diff] [blame] | 2395 | u8 gcw[2]; |
Borislav Petkov | b642201 | 2008-02-02 19:56:49 +0100 | [diff] [blame] | 2396 | u16 *ctl = (u16 *)&tape->caps[12]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2397 | |
Borislav Petkov | 776bb02 | 2008-07-23 19:55:59 +0200 | [diff] [blame] | 2398 | drive->pc_callback = ide_tape_callback; |
| 2399 | |
Borislav Petkov | 54bb207 | 2008-02-06 02:57:52 +0100 | [diff] [blame] | 2400 | spin_lock_init(&tape->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2401 | drive->dsc_overlap = 1; |
Bartlomiej Zolnierkiewicz | 4166c19 | 2008-02-01 23:09:30 +0100 | [diff] [blame] | 2402 | if (drive->hwif->host_flags & IDE_HFLAG_NO_DSC) { |
| 2403 | printk(KERN_INFO "ide-tape: %s: disabling DSC overlap\n", |
| 2404 | tape->name); |
| 2405 | drive->dsc_overlap = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2406 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2407 | /* Seagate Travan drives do not support DSC overlap. */ |
Bartlomiej Zolnierkiewicz | 4dde449 | 2008-10-10 22:39:19 +0200 | [diff] [blame] | 2408 | if (strstr((char *)&drive->id[ATA_ID_PROD], "Seagate STT3401")) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2409 | drive->dsc_overlap = 0; |
| 2410 | tape->minor = minor; |
| 2411 | tape->name[0] = 'h'; |
| 2412 | tape->name[1] = 't'; |
| 2413 | tape->name[2] = '0' + minor; |
Borislav Petkov | 54abf37 | 2008-02-06 02:57:52 +0100 | [diff] [blame] | 2414 | tape->chrdev_dir = IDETAPE_DIR_NONE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2415 | tape->pc = tape->pc_stack; |
Bartlomiej Zolnierkiewicz | 4dde449 | 2008-10-10 22:39:19 +0200 | [diff] [blame] | 2416 | |
| 2417 | *((u16 *)&gcw) = drive->id[ATA_ID_CONFIG]; |
Borislav Petkov | 71071b8 | 2008-02-06 02:57:53 +0100 | [diff] [blame] | 2418 | |
| 2419 | /* Command packet DRQ type */ |
| 2420 | if (((gcw[0] & 0x60) >> 5) == 1) |
Borislav Petkov | f2e3ab5 | 2008-07-23 19:56:01 +0200 | [diff] [blame] | 2421 | set_bit(IDE_AFLAG_DRQ_INTERRUPT, &drive->atapi_flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2422 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2423 | idetape_get_inquiry_results(drive); |
| 2424 | idetape_get_mode_sense_results(drive); |
Borislav Petkov | 3cffb9c | 2008-02-02 19:56:50 +0100 | [diff] [blame] | 2425 | ide_tape_get_bsize_from_bdesc(drive); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2426 | tape->user_bs_factor = 1; |
Borislav Petkov | f73850a | 2008-04-27 15:38:33 +0200 | [diff] [blame] | 2427 | tape->buffer_size = *ctl * tape->blk_size; |
| 2428 | while (tape->buffer_size > 0xffff) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2429 | printk(KERN_NOTICE "ide-tape: decreasing stage size\n"); |
Borislav Petkov | b642201 | 2008-02-02 19:56:49 +0100 | [diff] [blame] | 2430 | *ctl /= 2; |
Borislav Petkov | f73850a | 2008-04-27 15:38:33 +0200 | [diff] [blame] | 2431 | tape->buffer_size = *ctl * tape->blk_size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2432 | } |
Borislav Petkov | f73850a | 2008-04-27 15:38:33 +0200 | [diff] [blame] | 2433 | buffer_size = tape->buffer_size; |
Borislav Petkov | a997a43 | 2008-04-27 15:38:33 +0200 | [diff] [blame] | 2434 | tape->pages_per_buffer = buffer_size / PAGE_SIZE; |
Borislav Petkov | f73850a | 2008-04-27 15:38:33 +0200 | [diff] [blame] | 2435 | if (buffer_size % PAGE_SIZE) { |
Borislav Petkov | a997a43 | 2008-04-27 15:38:33 +0200 | [diff] [blame] | 2436 | tape->pages_per_buffer++; |
Borislav Petkov | f73850a | 2008-04-27 15:38:33 +0200 | [diff] [blame] | 2437 | tape->excess_bh_size = PAGE_SIZE - buffer_size % PAGE_SIZE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2438 | } |
| 2439 | |
Borislav Petkov | 83042b2 | 2008-04-27 15:38:27 +0200 | [diff] [blame] | 2440 | /* select the "best" DSC read/write polling freq */ |
Borislav Petkov | b642201 | 2008-02-02 19:56:49 +0100 | [diff] [blame] | 2441 | speed = max(*(u16 *)&tape->caps[14], *(u16 *)&tape->caps[8]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2442 | |
Borislav Petkov | f73850a | 2008-04-27 15:38:33 +0200 | [diff] [blame] | 2443 | t = (IDETAPE_FIFO_THRESHOLD * tape->buffer_size * HZ) / (speed * 1000); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2444 | |
| 2445 | /* |
Borislav Petkov | 3c98bf3 | 2008-02-06 02:57:53 +0100 | [diff] [blame] | 2446 | * Ensure that the number we got makes sense; limit it within |
| 2447 | * IDETAPE_DSC_RW_MIN and IDETAPE_DSC_RW_MAX. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2448 | */ |
Harvey Harrison | a792bd5 | 2008-07-15 21:21:41 +0200 | [diff] [blame] | 2449 | tape->best_dsc_rw_freq = clamp_t(unsigned long, t, IDETAPE_DSC_RW_MIN, |
| 2450 | IDETAPE_DSC_RW_MAX); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2451 | printk(KERN_INFO "ide-tape: %s <-> %s: %dKBps, %d*%dkB buffer, " |
Borislav Petkov | 83042b2 | 2008-04-27 15:38:27 +0200 | [diff] [blame] | 2452 | "%lums tDSC%s\n", |
Borislav Petkov | b642201 | 2008-02-02 19:56:49 +0100 | [diff] [blame] | 2453 | drive->name, tape->name, *(u16 *)&tape->caps[14], |
Borislav Petkov | f73850a | 2008-04-27 15:38:33 +0200 | [diff] [blame] | 2454 | (*(u16 *)&tape->caps[16] * 512) / tape->buffer_size, |
| 2455 | tape->buffer_size / 1024, |
Borislav Petkov | 54bb207 | 2008-02-06 02:57:52 +0100 | [diff] [blame] | 2456 | tape->best_dsc_rw_freq * 1000 / HZ, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2457 | drive->using_dma ? ", DMA":""); |
| 2458 | |
Bartlomiej Zolnierkiewicz | 1e874f4 | 2008-10-10 22:39:27 +0200 | [diff] [blame] | 2459 | ide_proc_register_driver(drive, tape->driver); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2460 | } |
| 2461 | |
Russell King | 4031bbe | 2006-01-06 11:41:00 +0000 | [diff] [blame] | 2462 | static void ide_tape_remove(ide_drive_t *drive) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2463 | { |
| 2464 | idetape_tape_t *tape = drive->driver_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2465 | |
Bartlomiej Zolnierkiewicz | 7662d04 | 2007-05-10 00:01:10 +0200 | [diff] [blame] | 2466 | ide_proc_unregister_driver(drive, tape->driver); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2467 | |
| 2468 | ide_unregister_region(tape->disk); |
| 2469 | |
| 2470 | ide_tape_put(tape); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2471 | } |
| 2472 | |
| 2473 | static void ide_tape_release(struct kref *kref) |
| 2474 | { |
| 2475 | struct ide_tape_obj *tape = to_ide_tape(kref); |
| 2476 | ide_drive_t *drive = tape->drive; |
| 2477 | struct gendisk *g = tape->disk; |
| 2478 | |
Borislav Petkov | 01a63aeb | 2008-04-27 15:38:34 +0200 | [diff] [blame] | 2479 | BUG_ON(tape->merge_bh_size); |
Bartlomiej Zolnierkiewicz | 8604aff | 2005-05-26 14:55:34 +0200 | [diff] [blame] | 2480 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2481 | drive->dsc_overlap = 0; |
| 2482 | drive->driver_data = NULL; |
Tony Jones | dbc1272 | 2007-09-25 02:03:03 +0200 | [diff] [blame] | 2483 | device_destroy(idetape_sysfs_class, MKDEV(IDETAPE_MAJOR, tape->minor)); |
Borislav Petkov | 5a04cfa | 2008-02-06 02:57:54 +0100 | [diff] [blame] | 2484 | device_destroy(idetape_sysfs_class, |
| 2485 | MKDEV(IDETAPE_MAJOR, tape->minor + 128)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2486 | idetape_devs[tape->minor] = NULL; |
| 2487 | g->private_data = NULL; |
| 2488 | put_disk(g); |
| 2489 | kfree(tape); |
| 2490 | } |
| 2491 | |
Bartlomiej Zolnierkiewicz | ecfd80e | 2007-05-10 00:01:09 +0200 | [diff] [blame] | 2492 | #ifdef CONFIG_IDE_PROC_FS |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2493 | static int proc_idetape_read_name |
| 2494 | (char *page, char **start, off_t off, int count, int *eof, void *data) |
| 2495 | { |
| 2496 | ide_drive_t *drive = (ide_drive_t *) data; |
| 2497 | idetape_tape_t *tape = drive->driver_data; |
| 2498 | char *out = page; |
| 2499 | int len; |
| 2500 | |
| 2501 | len = sprintf(out, "%s\n", tape->name); |
| 2502 | PROC_IDE_READ_RETURN(page, start, off, count, eof, len); |
| 2503 | } |
| 2504 | |
| 2505 | static ide_proc_entry_t idetape_proc[] = { |
| 2506 | { "capacity", S_IFREG|S_IRUGO, proc_ide_read_capacity, NULL }, |
| 2507 | { "name", S_IFREG|S_IRUGO, proc_idetape_read_name, NULL }, |
| 2508 | { NULL, 0, NULL, NULL } |
| 2509 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2510 | #endif |
| 2511 | |
Russell King | 4031bbe | 2006-01-06 11:41:00 +0000 | [diff] [blame] | 2512 | static int ide_tape_probe(ide_drive_t *); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2513 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2514 | static ide_driver_t idetape_driver = { |
Bartlomiej Zolnierkiewicz | 8604aff | 2005-05-26 14:55:34 +0200 | [diff] [blame] | 2515 | .gen_driver = { |
Laurent Riffard | 4ef3b8f | 2005-11-18 22:15:40 +0100 | [diff] [blame] | 2516 | .owner = THIS_MODULE, |
Bartlomiej Zolnierkiewicz | 8604aff | 2005-05-26 14:55:34 +0200 | [diff] [blame] | 2517 | .name = "ide-tape", |
| 2518 | .bus = &ide_bus_type, |
Bartlomiej Zolnierkiewicz | 8604aff | 2005-05-26 14:55:34 +0200 | [diff] [blame] | 2519 | }, |
Russell King | 4031bbe | 2006-01-06 11:41:00 +0000 | [diff] [blame] | 2520 | .probe = ide_tape_probe, |
| 2521 | .remove = ide_tape_remove, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2522 | .version = IDETAPE_VERSION, |
| 2523 | .media = ide_tape, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2524 | .do_request = idetape_do_request, |
| 2525 | .end_request = idetape_end_request, |
| 2526 | .error = __ide_error, |
Bartlomiej Zolnierkiewicz | 7662d04 | 2007-05-10 00:01:10 +0200 | [diff] [blame] | 2527 | #ifdef CONFIG_IDE_PROC_FS |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2528 | .proc = idetape_proc, |
Bartlomiej Zolnierkiewicz | 8185d5a | 2008-10-10 22:39:28 +0200 | [diff] [blame] | 2529 | .settings = idetape_settings, |
Bartlomiej Zolnierkiewicz | 7662d04 | 2007-05-10 00:01:10 +0200 | [diff] [blame] | 2530 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2531 | }; |
| 2532 | |
Borislav Petkov | 3c98bf3 | 2008-02-06 02:57:53 +0100 | [diff] [blame] | 2533 | /* Our character device supporting functions, passed to register_chrdev. */ |
Arjan van de Ven | 2b8693c | 2007-02-12 00:55:32 -0800 | [diff] [blame] | 2534 | static const struct file_operations idetape_fops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2535 | .owner = THIS_MODULE, |
| 2536 | .read = idetape_chrdev_read, |
| 2537 | .write = idetape_chrdev_write, |
| 2538 | .ioctl = idetape_chrdev_ioctl, |
| 2539 | .open = idetape_chrdev_open, |
| 2540 | .release = idetape_chrdev_release, |
| 2541 | }; |
| 2542 | |
| 2543 | static int idetape_open(struct inode *inode, struct file *filp) |
| 2544 | { |
| 2545 | struct gendisk *disk = inode->i_bdev->bd_disk; |
| 2546 | struct ide_tape_obj *tape; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2547 | |
Borislav Petkov | 5a04cfa | 2008-02-06 02:57:54 +0100 | [diff] [blame] | 2548 | tape = ide_tape_get(disk); |
| 2549 | if (!tape) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2550 | return -ENXIO; |
| 2551 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2552 | return 0; |
| 2553 | } |
| 2554 | |
| 2555 | static int idetape_release(struct inode *inode, struct file *filp) |
| 2556 | { |
| 2557 | struct gendisk *disk = inode->i_bdev->bd_disk; |
| 2558 | struct ide_tape_obj *tape = ide_tape_g(disk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2559 | |
| 2560 | ide_tape_put(tape); |
| 2561 | |
| 2562 | return 0; |
| 2563 | } |
| 2564 | |
| 2565 | static int idetape_ioctl(struct inode *inode, struct file *file, |
| 2566 | unsigned int cmd, unsigned long arg) |
| 2567 | { |
| 2568 | struct block_device *bdev = inode->i_bdev; |
| 2569 | struct ide_tape_obj *tape = ide_tape_g(bdev->bd_disk); |
| 2570 | ide_drive_t *drive = tape->drive; |
| 2571 | int err = generic_ide_ioctl(drive, file, bdev, cmd, arg); |
| 2572 | if (err == -EINVAL) |
| 2573 | err = idetape_blkdev_ioctl(drive, cmd, arg); |
| 2574 | return err; |
| 2575 | } |
| 2576 | |
| 2577 | static struct block_device_operations idetape_block_ops = { |
| 2578 | .owner = THIS_MODULE, |
| 2579 | .open = idetape_open, |
| 2580 | .release = idetape_release, |
| 2581 | .ioctl = idetape_ioctl, |
| 2582 | }; |
| 2583 | |
Russell King | 4031bbe | 2006-01-06 11:41:00 +0000 | [diff] [blame] | 2584 | static int ide_tape_probe(ide_drive_t *drive) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2585 | { |
| 2586 | idetape_tape_t *tape; |
| 2587 | struct gendisk *g; |
| 2588 | int minor; |
| 2589 | |
| 2590 | if (!strstr("ide-tape", drive->driver_req)) |
| 2591 | goto failed; |
Bartlomiej Zolnierkiewicz | 2a92466 | 2008-10-10 22:39:24 +0200 | [diff] [blame] | 2592 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2593 | if (drive->media != ide_tape) |
| 2594 | goto failed; |
Bartlomiej Zolnierkiewicz | 2a92466 | 2008-10-10 22:39:24 +0200 | [diff] [blame] | 2595 | |
Bartlomiej Zolnierkiewicz | 51509ee | 2008-10-10 22:39:34 +0200 | [diff] [blame] | 2596 | if (drive->id_read == 1 && !ide_check_atapi_device(drive, DRV_NAME)) { |
Borislav Petkov | 5a04cfa | 2008-02-06 02:57:54 +0100 | [diff] [blame] | 2597 | printk(KERN_ERR "ide-tape: %s: not supported by this version of" |
| 2598 | " the driver\n", drive->name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2599 | goto failed; |
| 2600 | } |
Borislav Petkov | 5a04cfa | 2008-02-06 02:57:54 +0100 | [diff] [blame] | 2601 | tape = kzalloc(sizeof(idetape_tape_t), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2602 | if (tape == NULL) { |
Borislav Petkov | 5a04cfa | 2008-02-06 02:57:54 +0100 | [diff] [blame] | 2603 | printk(KERN_ERR "ide-tape: %s: Can't allocate a tape struct\n", |
| 2604 | drive->name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2605 | goto failed; |
| 2606 | } |
| 2607 | |
| 2608 | g = alloc_disk(1 << PARTN_BITS); |
| 2609 | if (!g) |
| 2610 | goto out_free_tape; |
| 2611 | |
| 2612 | ide_init_disk(g, drive); |
| 2613 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2614 | kref_init(&tape->kref); |
| 2615 | |
| 2616 | tape->drive = drive; |
| 2617 | tape->driver = &idetape_driver; |
| 2618 | tape->disk = g; |
| 2619 | |
| 2620 | g->private_data = &tape->driver; |
| 2621 | |
| 2622 | drive->driver_data = tape; |
| 2623 | |
Arjan van de Ven | cf8b897 | 2006-03-23 03:00:45 -0800 | [diff] [blame] | 2624 | mutex_lock(&idetape_ref_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2625 | for (minor = 0; idetape_devs[minor]; minor++) |
| 2626 | ; |
| 2627 | idetape_devs[minor] = tape; |
Arjan van de Ven | cf8b897 | 2006-03-23 03:00:45 -0800 | [diff] [blame] | 2628 | mutex_unlock(&idetape_ref_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2629 | |
| 2630 | idetape_setup(drive, tape, minor); |
| 2631 | |
Greg Kroah-Hartman | 6ecaaf9 | 2008-05-21 12:52:33 -0700 | [diff] [blame] | 2632 | device_create_drvdata(idetape_sysfs_class, &drive->gendev, |
| 2633 | MKDEV(IDETAPE_MAJOR, minor), NULL, |
| 2634 | "%s", tape->name); |
| 2635 | device_create_drvdata(idetape_sysfs_class, &drive->gendev, |
| 2636 | MKDEV(IDETAPE_MAJOR, minor + 128), NULL, |
| 2637 | "n%s", tape->name); |
Will Dyson | d5dee80 | 2005-09-16 02:55:07 -0700 | [diff] [blame] | 2638 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2639 | g->fops = &idetape_block_ops; |
| 2640 | ide_register_region(g); |
| 2641 | |
| 2642 | return 0; |
Bartlomiej Zolnierkiewicz | 8604aff | 2005-05-26 14:55:34 +0200 | [diff] [blame] | 2643 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2644 | out_free_tape: |
| 2645 | kfree(tape); |
| 2646 | failed: |
Bartlomiej Zolnierkiewicz | 8604aff | 2005-05-26 14:55:34 +0200 | [diff] [blame] | 2647 | return -ENODEV; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2648 | } |
| 2649 | |
Borislav Petkov | 5a04cfa | 2008-02-06 02:57:54 +0100 | [diff] [blame] | 2650 | static void __exit idetape_exit(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2651 | { |
Bartlomiej Zolnierkiewicz | 8604aff | 2005-05-26 14:55:34 +0200 | [diff] [blame] | 2652 | driver_unregister(&idetape_driver.gen_driver); |
Will Dyson | d5dee80 | 2005-09-16 02:55:07 -0700 | [diff] [blame] | 2653 | class_destroy(idetape_sysfs_class); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2654 | unregister_chrdev(IDETAPE_MAJOR, "ht"); |
| 2655 | } |
| 2656 | |
Bartlomiej Zolnierkiewicz | 17514e8 | 2005-11-19 22:24:35 +0100 | [diff] [blame] | 2657 | static int __init idetape_init(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2658 | { |
Will Dyson | d5dee80 | 2005-09-16 02:55:07 -0700 | [diff] [blame] | 2659 | int error = 1; |
| 2660 | idetape_sysfs_class = class_create(THIS_MODULE, "ide_tape"); |
| 2661 | if (IS_ERR(idetape_sysfs_class)) { |
| 2662 | idetape_sysfs_class = NULL; |
| 2663 | printk(KERN_ERR "Unable to create sysfs class for ide tapes\n"); |
| 2664 | error = -EBUSY; |
| 2665 | goto out; |
| 2666 | } |
| 2667 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2668 | if (register_chrdev(IDETAPE_MAJOR, "ht", &idetape_fops)) { |
Borislav Petkov | 5a04cfa | 2008-02-06 02:57:54 +0100 | [diff] [blame] | 2669 | printk(KERN_ERR "ide-tape: Failed to register chrdev" |
| 2670 | " interface\n"); |
Will Dyson | d5dee80 | 2005-09-16 02:55:07 -0700 | [diff] [blame] | 2671 | error = -EBUSY; |
| 2672 | goto out_free_class; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2673 | } |
Will Dyson | d5dee80 | 2005-09-16 02:55:07 -0700 | [diff] [blame] | 2674 | |
| 2675 | error = driver_register(&idetape_driver.gen_driver); |
| 2676 | if (error) |
| 2677 | goto out_free_driver; |
| 2678 | |
| 2679 | return 0; |
| 2680 | |
| 2681 | out_free_driver: |
| 2682 | driver_unregister(&idetape_driver.gen_driver); |
| 2683 | out_free_class: |
| 2684 | class_destroy(idetape_sysfs_class); |
| 2685 | out: |
| 2686 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2687 | } |
| 2688 | |
Kay Sievers | 263756e | 2005-12-12 18:03:44 +0100 | [diff] [blame] | 2689 | MODULE_ALIAS("ide:*m-tape*"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2690 | module_init(idetape_init); |
| 2691 | module_exit(idetape_exit); |
| 2692 | MODULE_ALIAS_CHARDEV_MAJOR(IDETAPE_MAJOR); |
Borislav Petkov | 9c14576 | 2008-02-06 02:57:54 +0100 | [diff] [blame] | 2693 | MODULE_DESCRIPTION("ATAPI Streaming TAPE Driver"); |
| 2694 | MODULE_LICENSE("GPL"); |