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