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 | dfe79936 | 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 *****************************/ |
| 75 | |
| 76 | |
| 77 | /* |
Borislav Petkov | 3c98bf3 | 2008-02-06 02:57:53 +0100 | [diff] [blame] | 78 | * Pipelined mode parameters. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 79 | * |
Borislav Petkov | 3c98bf3 | 2008-02-06 02:57:53 +0100 | [diff] [blame] | 80 | * We try to use the minimum number of stages which is enough to keep the tape |
| 81 | * constantly streaming. To accomplish that, we implement a feedback loop around |
| 82 | * the maximum number of stages: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 83 | * |
Borislav Petkov | 3c98bf3 | 2008-02-06 02:57:53 +0100 | [diff] [blame] | 84 | * We start from MIN maximum stages (we will not even use MIN stages if we don't |
| 85 | * need them), increment it by RATE*(MAX-MIN) whenever we sense that the |
| 86 | * pipeline is empty, until we reach the optimum value or until we reach MAX. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 87 | * |
Borislav Petkov | 3c98bf3 | 2008-02-06 02:57:53 +0100 | [diff] [blame] | 88 | * Setting the following parameter to 0 is illegal: the pipelined mode cannot be |
| 89 | * disabled (idetape_calculate_speeds() divides by tape->max_stages.) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 90 | */ |
| 91 | #define IDETAPE_MIN_PIPELINE_STAGES 1 |
| 92 | #define IDETAPE_MAX_PIPELINE_STAGES 400 |
| 93 | #define IDETAPE_INCREASE_STAGES_RATE 20 |
| 94 | |
| 95 | /* |
Borislav Petkov | 3c98bf3 | 2008-02-06 02:57:53 +0100 | [diff] [blame] | 96 | * After each failed packet command we issue a request sense command and retry |
| 97 | * the packet command IDETAPE_MAX_PC_RETRIES times. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 98 | * |
Borislav Petkov | 3c98bf3 | 2008-02-06 02:57:53 +0100 | [diff] [blame] | 99 | * Setting IDETAPE_MAX_PC_RETRIES to 0 will disable retries. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 100 | */ |
| 101 | #define IDETAPE_MAX_PC_RETRIES 3 |
| 102 | |
| 103 | /* |
Borislav Petkov | 3c98bf3 | 2008-02-06 02:57:53 +0100 | [diff] [blame] | 104 | * With each packet command, we allocate a buffer of IDETAPE_PC_BUFFER_SIZE |
| 105 | * 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] | 106 | */ |
| 107 | #define IDETAPE_PC_BUFFER_SIZE 256 |
| 108 | |
| 109 | /* |
| 110 | * In various places in the driver, we need to allocate storage |
| 111 | * for packet commands and requests, which will remain valid while |
| 112 | * we leave the driver to wait for an interrupt or a timeout event. |
| 113 | */ |
| 114 | #define IDETAPE_PC_STACK (10 + IDETAPE_MAX_PC_RETRIES) |
| 115 | |
| 116 | /* |
| 117 | * Some drives (for example, Seagate STT3401A Travan) require a very long |
| 118 | * timeout, because they don't return an interrupt or clear their busy bit |
| 119 | * until after the command completes (even retension commands). |
| 120 | */ |
| 121 | #define IDETAPE_WAIT_CMD (900*HZ) |
| 122 | |
| 123 | /* |
Borislav Petkov | 3c98bf3 | 2008-02-06 02:57:53 +0100 | [diff] [blame] | 124 | * The following parameter is used to select the point in the internal tape fifo |
| 125 | * in which we will start to refill the buffer. Decreasing the following |
| 126 | * parameter will improve the system's latency and interactive response, while |
| 127 | * using a high value might improve system throughput. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 128 | */ |
Borislav Petkov | 3c98bf3 | 2008-02-06 02:57:53 +0100 | [diff] [blame] | 129 | #define IDETAPE_FIFO_THRESHOLD 2 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 130 | |
| 131 | /* |
Borislav Petkov | 3c98bf3 | 2008-02-06 02:57:53 +0100 | [diff] [blame] | 132 | * DSC polling parameters. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 133 | * |
Borislav Petkov | 3c98bf3 | 2008-02-06 02:57:53 +0100 | [diff] [blame] | 134 | * Polling for DSC (a single bit in the status register) is a very important |
| 135 | * 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] | 136 | * |
Borislav Petkov | 3c98bf3 | 2008-02-06 02:57:53 +0100 | [diff] [blame] | 137 | * 1. Before a read/write packet command, to ensure that we can transfer data |
| 138 | * from/to the tape's data buffers, without causing an actual media access. |
| 139 | * In case the tape is not ready yet, we take out our request from the device |
| 140 | * request queue, so that ide.c could service requests from the other device |
| 141 | * on the same interface in the meantime. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 142 | * |
Borislav Petkov | 3c98bf3 | 2008-02-06 02:57:53 +0100 | [diff] [blame] | 143 | * 2. After the successful initialization of a "media access packet command", |
| 144 | * which is a command that can take a long time to complete (the interval can |
| 145 | * range from several seconds to even an hour). Again, we postpone our request |
| 146 | * in the middle to free the bus for the other device. The polling frequency |
| 147 | * here should be lower than the read/write frequency since those media access |
| 148 | * commands are slow. We start from a "fast" frequency - IDETAPE_DSC_MA_FAST |
| 149 | * (1 second), and if we don't receive DSC after IDETAPE_DSC_MA_THRESHOLD |
| 150 | * (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] | 151 | * |
Borislav Petkov | 3c98bf3 | 2008-02-06 02:57:53 +0100 | [diff] [blame] | 152 | * We also set a timeout for the timer, in case something goes wrong. The |
| 153 | * 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] | 154 | */ |
Borislav Petkov | 3c98bf3 | 2008-02-06 02:57:53 +0100 | [diff] [blame] | 155 | |
| 156 | /* DSC timings. */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 157 | #define IDETAPE_DSC_RW_MIN 5*HZ/100 /* 50 msec */ |
| 158 | #define IDETAPE_DSC_RW_MAX 40*HZ/100 /* 400 msec */ |
| 159 | #define IDETAPE_DSC_RW_TIMEOUT 2*60*HZ /* 2 minutes */ |
| 160 | #define IDETAPE_DSC_MA_FAST 2*HZ /* 2 seconds */ |
| 161 | #define IDETAPE_DSC_MA_THRESHOLD 5*60*HZ /* 5 minutes */ |
| 162 | #define IDETAPE_DSC_MA_SLOW 30*HZ /* 30 seconds */ |
| 163 | #define IDETAPE_DSC_MA_TIMEOUT 2*60*60*HZ /* 2 hours */ |
| 164 | |
| 165 | /*************************** End of tunable parameters ***********************/ |
| 166 | |
Borislav Petkov | 3c98bf3 | 2008-02-06 02:57:53 +0100 | [diff] [blame] | 167 | /* Read/Write error simulation */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 168 | #define SIMULATE_ERRORS 0 |
| 169 | |
Borislav Petkov | 54abf37 | 2008-02-06 02:57:52 +0100 | [diff] [blame] | 170 | /* tape directions */ |
| 171 | enum { |
| 172 | IDETAPE_DIR_NONE = (1 << 0), |
| 173 | IDETAPE_DIR_READ = (1 << 1), |
| 174 | IDETAPE_DIR_WRITE = (1 << 2), |
| 175 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 176 | |
| 177 | struct idetape_bh { |
Stephen Rothwell | ab05796 | 2007-08-01 23:46:44 +0200 | [diff] [blame] | 178 | u32 b_size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 179 | atomic_t b_count; |
| 180 | struct idetape_bh *b_reqnext; |
| 181 | char *b_data; |
| 182 | }; |
| 183 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 184 | typedef struct idetape_packet_command_s { |
Borislav Petkov | 3c98bf3 | 2008-02-06 02:57:53 +0100 | [diff] [blame] | 185 | /* Actual packet bytes */ |
| 186 | u8 c[12]; |
| 187 | /* On each retry, we increment retries */ |
| 188 | int retries; |
| 189 | /* Error code */ |
| 190 | int error; |
| 191 | /* Bytes to transfer */ |
| 192 | int request_transfer; |
| 193 | /* Bytes actually transferred */ |
| 194 | int actually_transferred; |
| 195 | /* Size of our data buffer */ |
| 196 | int buffer_size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 197 | struct idetape_bh *bh; |
| 198 | char *b_data; |
| 199 | int b_count; |
Borislav Petkov | 3c98bf3 | 2008-02-06 02:57:53 +0100 | [diff] [blame] | 200 | /* Data buffer */ |
| 201 | u8 *buffer; |
| 202 | /* Pointer into the above buffer */ |
| 203 | u8 *current_position; |
| 204 | /* Called when this packet command is completed */ |
| 205 | ide_startstop_t (*callback) (ide_drive_t *); |
| 206 | /* Temporary buffer */ |
| 207 | u8 pc_buffer[IDETAPE_PC_BUFFER_SIZE]; |
| 208 | /* Status/Action bit flags: long for set_bit */ |
| 209 | unsigned long flags; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 210 | } idetape_pc_t; |
| 211 | |
| 212 | /* |
| 213 | * Packet command flag bits. |
| 214 | */ |
| 215 | /* Set when an error is considered normal - We won't retry */ |
| 216 | #define PC_ABORT 0 |
| 217 | /* 1 When polling for DSC on a media access command */ |
| 218 | #define PC_WAIT_FOR_DSC 1 |
| 219 | /* 1 when we prefer to use DMA if possible */ |
| 220 | #define PC_DMA_RECOMMENDED 2 |
| 221 | /* 1 while DMA in progress */ |
| 222 | #define PC_DMA_IN_PROGRESS 3 |
| 223 | /* 1 when encountered problem during DMA */ |
| 224 | #define PC_DMA_ERROR 4 |
| 225 | /* Data direction */ |
| 226 | #define PC_WRITING 5 |
| 227 | |
Borislav Petkov | 03056b9 | 2008-04-18 00:46:26 +0200 | [diff] [blame^] | 228 | /* Tape door status */ |
| 229 | #define DOOR_UNLOCKED 0 |
| 230 | #define DOOR_LOCKED 1 |
| 231 | #define DOOR_EXPLICITLY_LOCKED 2 |
| 232 | |
| 233 | /* Some defines for the SPACE command */ |
| 234 | #define IDETAPE_SPACE_OVER_FILEMARK 1 |
| 235 | #define IDETAPE_SPACE_TO_EOD 3 |
| 236 | |
| 237 | /* Some defines for the LOAD UNLOAD command */ |
| 238 | #define IDETAPE_LU_LOAD_MASK 1 |
| 239 | #define IDETAPE_LU_RETENSION_MASK 2 |
| 240 | #define IDETAPE_LU_EOT_MASK 4 |
| 241 | |
| 242 | /* |
| 243 | * Special requests for our block device strategy routine. |
| 244 | * |
| 245 | * In order to service a character device command, we add special requests to |
| 246 | * the tail of our block device request queue and wait for their completion. |
| 247 | */ |
| 248 | |
| 249 | enum { |
| 250 | REQ_IDETAPE_PC1 = (1 << 0), /* packet command (first stage) */ |
| 251 | REQ_IDETAPE_PC2 = (1 << 1), /* packet command (second stage) */ |
| 252 | REQ_IDETAPE_READ = (1 << 2), |
| 253 | REQ_IDETAPE_WRITE = (1 << 3), |
| 254 | }; |
| 255 | |
| 256 | /* Error codes returned in rq->errors to the higher part of the driver. */ |
| 257 | #define IDETAPE_ERROR_GENERAL 101 |
| 258 | #define IDETAPE_ERROR_FILEMARK 102 |
| 259 | #define IDETAPE_ERROR_EOD 103 |
| 260 | |
| 261 | /* Structures related to the SELECT SENSE / MODE SENSE packet commands. */ |
| 262 | #define IDETAPE_BLOCK_DESCRIPTOR 0 |
| 263 | #define IDETAPE_CAPABILITIES_PAGE 0x2a |
| 264 | |
Borislav Petkov | 3c98bf3 | 2008-02-06 02:57:53 +0100 | [diff] [blame] | 265 | /* A pipeline stage. */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 266 | typedef struct idetape_stage_s { |
| 267 | struct request rq; /* The corresponding request */ |
| 268 | struct idetape_bh *bh; /* The data buffers */ |
| 269 | struct idetape_stage_s *next; /* Pointer to the next stage */ |
| 270 | } idetape_stage_t; |
| 271 | |
| 272 | /* |
Borislav Petkov | 3c98bf3 | 2008-02-06 02:57:53 +0100 | [diff] [blame] | 273 | * Most of our global data which we need to save even as we leave the driver due |
| 274 | * 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] | 275 | */ |
| 276 | typedef struct ide_tape_obj { |
| 277 | ide_drive_t *drive; |
| 278 | ide_driver_t *driver; |
| 279 | struct gendisk *disk; |
| 280 | struct kref kref; |
| 281 | |
| 282 | /* |
| 283 | * Since a typical character device operation requires more |
| 284 | * than one packet command, we provide here enough memory |
| 285 | * for the maximum of interconnected packet commands. |
| 286 | * The packet commands are stored in the circular array pc_stack. |
| 287 | * pc_stack_index points to the last used entry, and warps around |
| 288 | * to the start when we get to the last array entry. |
| 289 | * |
| 290 | * pc points to the current processed packet command. |
| 291 | * |
| 292 | * failed_pc points to the last failed packet command, or contains |
| 293 | * NULL if we do not need to retry any packet command. This is |
| 294 | * required since an additional packet command is needed before the |
| 295 | * retry, to get detailed information on what went wrong. |
| 296 | */ |
| 297 | /* Current packet command */ |
| 298 | idetape_pc_t *pc; |
| 299 | /* Last failed packet command */ |
| 300 | idetape_pc_t *failed_pc; |
| 301 | /* Packet command stack */ |
| 302 | idetape_pc_t pc_stack[IDETAPE_PC_STACK]; |
| 303 | /* Next free packet command storage space */ |
| 304 | int pc_stack_index; |
| 305 | struct request rq_stack[IDETAPE_PC_STACK]; |
| 306 | /* We implement a circular array */ |
| 307 | int rq_stack_index; |
| 308 | |
| 309 | /* |
Borislav Petkov | 3c98bf3 | 2008-02-06 02:57:53 +0100 | [diff] [blame] | 310 | * DSC polling variables. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 311 | * |
Borislav Petkov | 3c98bf3 | 2008-02-06 02:57:53 +0100 | [diff] [blame] | 312 | * While polling for DSC we use postponed_rq to postpone the current |
| 313 | * request so that ide.c will be able to service pending requests on the |
| 314 | * other device. Note that at most we will have only one DSC (usually |
| 315 | * data transfer) request in the device request queue. Additional |
| 316 | * requests can be queued in our internal pipeline, but they will be |
| 317 | * visible to ide.c only one at a time. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 318 | */ |
| 319 | struct request *postponed_rq; |
| 320 | /* The time in which we started polling for DSC */ |
| 321 | unsigned long dsc_polling_start; |
| 322 | /* Timer used to poll for dsc */ |
| 323 | struct timer_list dsc_timer; |
| 324 | /* Read/Write dsc polling frequency */ |
Borislav Petkov | 54bb207 | 2008-02-06 02:57:52 +0100 | [diff] [blame] | 325 | unsigned long best_dsc_rw_freq; |
| 326 | unsigned long dsc_poll_freq; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 327 | unsigned long dsc_timeout; |
| 328 | |
Borislav Petkov | 3c98bf3 | 2008-02-06 02:57:53 +0100 | [diff] [blame] | 329 | /* Read position information */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 330 | u8 partition; |
| 331 | /* Current block */ |
Borislav Petkov | 54bb207 | 2008-02-06 02:57:52 +0100 | [diff] [blame] | 332 | unsigned int first_frame; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 333 | |
Borislav Petkov | 3c98bf3 | 2008-02-06 02:57:53 +0100 | [diff] [blame] | 334 | /* Last error information */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 335 | u8 sense_key, asc, ascq; |
| 336 | |
Borislav Petkov | 3c98bf3 | 2008-02-06 02:57:53 +0100 | [diff] [blame] | 337 | /* Character device operation */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 338 | unsigned int minor; |
| 339 | /* device name */ |
| 340 | char name[4]; |
| 341 | /* Current character device data transfer direction */ |
Borislav Petkov | 54abf37 | 2008-02-06 02:57:52 +0100 | [diff] [blame] | 342 | u8 chrdev_dir; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 343 | |
Borislav Petkov | 54bb207 | 2008-02-06 02:57:52 +0100 | [diff] [blame] | 344 | /* tape block size, usually 512 or 1024 bytes */ |
| 345 | unsigned short blk_size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 346 | int user_bs_factor; |
Borislav Petkov | b642201 | 2008-02-02 19:56:49 +0100 | [diff] [blame] | 347 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 348 | /* Copy of the tape's Capabilities and Mechanical Page */ |
Borislav Petkov | b642201 | 2008-02-02 19:56:49 +0100 | [diff] [blame] | 349 | u8 caps[20]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 350 | |
| 351 | /* |
Borislav Petkov | 3c98bf3 | 2008-02-06 02:57:53 +0100 | [diff] [blame] | 352 | * Active data transfer request parameters. |
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 | * At most, there is only one ide-tape originated data transfer request |
| 355 | * in the device request queue. This allows ide.c to easily service |
| 356 | * requests from the other device when we postpone our active request. |
| 357 | * In the pipelined operation mode, we use our internal pipeline |
| 358 | * structure to hold more data requests. The data buffer size is chosen |
| 359 | * based on the tape's recommendation. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 360 | */ |
Borislav Petkov | 3c98bf3 | 2008-02-06 02:57:53 +0100 | [diff] [blame] | 361 | /* ptr to the request which is waiting in the device request queue */ |
Borislav Petkov | 54bb207 | 2008-02-06 02:57:52 +0100 | [diff] [blame] | 362 | struct request *active_data_rq; |
Borislav Petkov | 3c98bf3 | 2008-02-06 02:57:53 +0100 | [diff] [blame] | 363 | /* Data buffer size chosen based on the tape's recommendation */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 364 | int stage_size; |
| 365 | idetape_stage_t *merge_stage; |
| 366 | int merge_stage_size; |
| 367 | struct idetape_bh *bh; |
| 368 | char *b_data; |
| 369 | int b_count; |
Borislav Petkov | 3c98bf3 | 2008-02-06 02:57:53 +0100 | [diff] [blame] | 370 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 371 | /* |
Borislav Petkov | 3c98bf3 | 2008-02-06 02:57:53 +0100 | [diff] [blame] | 372 | * Pipeline parameters. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 373 | * |
Borislav Petkov | 3c98bf3 | 2008-02-06 02:57:53 +0100 | [diff] [blame] | 374 | * To accomplish non-pipelined mode, we simply set the following |
| 375 | * variables to zero (or NULL, where appropriate). |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 376 | */ |
| 377 | /* Number of currently used stages */ |
| 378 | int nr_stages; |
| 379 | /* Number of pending stages */ |
| 380 | int nr_pending_stages; |
| 381 | /* We will not allocate more than this number of stages */ |
| 382 | int max_stages, min_pipeline, max_pipeline; |
| 383 | /* The first stage which will be removed from the pipeline */ |
| 384 | idetape_stage_t *first_stage; |
| 385 | /* The currently active stage */ |
| 386 | idetape_stage_t *active_stage; |
| 387 | /* Will be serviced after the currently active request */ |
| 388 | idetape_stage_t *next_stage; |
| 389 | /* New requests will be added to the pipeline here */ |
| 390 | idetape_stage_t *last_stage; |
| 391 | /* Optional free stage which we can use */ |
| 392 | idetape_stage_t *cache_stage; |
| 393 | int pages_per_stage; |
| 394 | /* Wasted space in each stage */ |
| 395 | int excess_bh_size; |
| 396 | |
| 397 | /* Status/Action flags: long for set_bit */ |
| 398 | unsigned long flags; |
| 399 | /* protects the ide-tape queue */ |
Borislav Petkov | 54bb207 | 2008-02-06 02:57:52 +0100 | [diff] [blame] | 400 | spinlock_t lock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 401 | |
Borislav Petkov | 3c98bf3 | 2008-02-06 02:57:53 +0100 | [diff] [blame] | 402 | /* Measures average tape speed */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 403 | unsigned long avg_time; |
| 404 | int avg_size; |
| 405 | int avg_speed; |
| 406 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 407 | /* the door is currently locked */ |
| 408 | int door_locked; |
| 409 | /* the tape hardware is write protected */ |
| 410 | char drv_write_prot; |
| 411 | /* the tape is write protected (hardware or opened as read-only) */ |
| 412 | char write_prot; |
| 413 | |
| 414 | /* |
Borislav Petkov | 3c98bf3 | 2008-02-06 02:57:53 +0100 | [diff] [blame] | 415 | * Limit the number of times a request can be postponed, to avoid an |
| 416 | * infinite postpone deadlock. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 417 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 418 | int postpone_cnt; |
| 419 | |
| 420 | /* |
| 421 | * Measures number of frames: |
| 422 | * |
| 423 | * 1. written/read to/from the driver pipeline (pipeline_head). |
| 424 | * 2. written/read to/from the tape buffers (idetape_bh). |
| 425 | * 3. written/read by the tape to/from the media (tape_head). |
| 426 | */ |
| 427 | int pipeline_head; |
| 428 | int buffer_head; |
| 429 | int tape_head; |
| 430 | int last_tape_head; |
| 431 | |
Borislav Petkov | 3c98bf3 | 2008-02-06 02:57:53 +0100 | [diff] [blame] | 432 | /* Speed control at the tape buffers input/output */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 433 | unsigned long insert_time; |
| 434 | int insert_size; |
| 435 | int insert_speed; |
| 436 | int max_insert_speed; |
| 437 | int measure_insert_time; |
| 438 | |
Borislav Petkov | 3c98bf3 | 2008-02-06 02:57:53 +0100 | [diff] [blame] | 439 | /* Speed regulation negative feedback loop */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 440 | int speed_control; |
| 441 | int pipeline_head_speed; |
| 442 | int controlled_pipeline_head_speed; |
| 443 | int uncontrolled_pipeline_head_speed; |
| 444 | int controlled_last_pipeline_head; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 445 | unsigned long uncontrolled_pipeline_head_time; |
| 446 | unsigned long controlled_pipeline_head_time; |
| 447 | int controlled_previous_pipeline_head; |
| 448 | int uncontrolled_previous_pipeline_head; |
| 449 | unsigned long controlled_previous_head_time; |
| 450 | unsigned long uncontrolled_previous_head_time; |
| 451 | int restart_speed_control_req; |
| 452 | |
Borislav Petkov | 8004a8c | 2008-02-06 02:57:51 +0100 | [diff] [blame] | 453 | u32 debug_mask; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 454 | } idetape_tape_t; |
| 455 | |
Arjan van de Ven | cf8b897 | 2006-03-23 03:00:45 -0800 | [diff] [blame] | 456 | static DEFINE_MUTEX(idetape_ref_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 457 | |
Will Dyson | d5dee80 | 2005-09-16 02:55:07 -0700 | [diff] [blame] | 458 | static struct class *idetape_sysfs_class; |
| 459 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 460 | #define to_ide_tape(obj) container_of(obj, struct ide_tape_obj, kref) |
| 461 | |
| 462 | #define ide_tape_g(disk) \ |
| 463 | container_of((disk)->private_data, struct ide_tape_obj, driver) |
| 464 | |
| 465 | static struct ide_tape_obj *ide_tape_get(struct gendisk *disk) |
| 466 | { |
| 467 | struct ide_tape_obj *tape = NULL; |
| 468 | |
Arjan van de Ven | cf8b897 | 2006-03-23 03:00:45 -0800 | [diff] [blame] | 469 | mutex_lock(&idetape_ref_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 470 | tape = ide_tape_g(disk); |
| 471 | if (tape) |
| 472 | kref_get(&tape->kref); |
Arjan van de Ven | cf8b897 | 2006-03-23 03:00:45 -0800 | [diff] [blame] | 473 | mutex_unlock(&idetape_ref_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 474 | return tape; |
| 475 | } |
| 476 | |
| 477 | static void ide_tape_release(struct kref *); |
| 478 | |
| 479 | static void ide_tape_put(struct ide_tape_obj *tape) |
| 480 | { |
Arjan van de Ven | cf8b897 | 2006-03-23 03:00:45 -0800 | [diff] [blame] | 481 | mutex_lock(&idetape_ref_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 482 | kref_put(&tape->kref, ide_tape_release); |
Arjan van de Ven | cf8b897 | 2006-03-23 03:00:45 -0800 | [diff] [blame] | 483 | mutex_unlock(&idetape_ref_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 484 | } |
| 485 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 486 | /* |
| 487 | * Tape flag bits values. |
| 488 | */ |
| 489 | #define IDETAPE_IGNORE_DSC 0 |
| 490 | #define IDETAPE_ADDRESS_VALID 1 /* 0 When the tape position is unknown */ |
| 491 | #define IDETAPE_BUSY 2 /* Device already opened */ |
| 492 | #define IDETAPE_PIPELINE_ERROR 3 /* Error detected in a pipeline stage */ |
| 493 | #define IDETAPE_DETECT_BS 4 /* Attempt to auto-detect the current user block size */ |
| 494 | #define IDETAPE_FILEMARK 5 /* Currently on a filemark */ |
| 495 | #define IDETAPE_DRQ_INTERRUPT 6 /* DRQ interrupt device */ |
| 496 | #define IDETAPE_READ_ERROR 7 |
| 497 | #define IDETAPE_PIPELINE_ACTIVE 8 /* pipeline active */ |
| 498 | /* 0 = no tape is loaded, so we don't rewind after ejecting */ |
| 499 | #define IDETAPE_MEDIUM_PRESENT 9 |
| 500 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 501 | /* |
Borislav Petkov | 3c98bf3 | 2008-02-06 02:57:53 +0100 | [diff] [blame] | 502 | * The variables below are used for the character device interface. Additional |
| 503 | * state variables are defined in our ide_drive_t structure. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 504 | */ |
Borislav Petkov | 5a04cfa | 2008-02-06 02:57:54 +0100 | [diff] [blame] | 505 | static struct ide_tape_obj *idetape_devs[MAX_HWIFS * MAX_DRIVES]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 506 | |
| 507 | #define ide_tape_f(file) ((file)->private_data) |
| 508 | |
| 509 | static struct ide_tape_obj *ide_tape_chrdev_get(unsigned int i) |
| 510 | { |
| 511 | struct ide_tape_obj *tape = NULL; |
| 512 | |
Arjan van de Ven | cf8b897 | 2006-03-23 03:00:45 -0800 | [diff] [blame] | 513 | mutex_lock(&idetape_ref_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 514 | tape = idetape_devs[i]; |
| 515 | if (tape) |
| 516 | kref_get(&tape->kref); |
Arjan van de Ven | cf8b897 | 2006-03-23 03:00:45 -0800 | [diff] [blame] | 517 | mutex_unlock(&idetape_ref_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 518 | return tape; |
| 519 | } |
| 520 | |
Borislav Petkov | 5a04cfa | 2008-02-06 02:57:54 +0100 | [diff] [blame] | 521 | static void idetape_input_buffers(ide_drive_t *drive, idetape_pc_t *pc, |
| 522 | unsigned int bcount) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 523 | { |
| 524 | struct idetape_bh *bh = pc->bh; |
| 525 | int count; |
| 526 | |
| 527 | while (bcount) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 528 | if (bh == NULL) { |
| 529 | printk(KERN_ERR "ide-tape: bh == NULL in " |
| 530 | "idetape_input_buffers\n"); |
Bartlomiej Zolnierkiewicz | 7616c0a | 2008-04-18 00:46:26 +0200 | [diff] [blame] | 531 | ide_atapi_discard_data(drive, bcount); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 532 | return; |
| 533 | } |
Borislav Petkov | 5a04cfa | 2008-02-06 02:57:54 +0100 | [diff] [blame] | 534 | count = min( |
| 535 | (unsigned int)(bh->b_size - atomic_read(&bh->b_count)), |
| 536 | bcount); |
| 537 | HWIF(drive)->atapi_input_bytes(drive, bh->b_data + |
| 538 | atomic_read(&bh->b_count), count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 539 | bcount -= count; |
| 540 | atomic_add(count, &bh->b_count); |
| 541 | if (atomic_read(&bh->b_count) == bh->b_size) { |
| 542 | bh = bh->b_reqnext; |
| 543 | if (bh) |
| 544 | atomic_set(&bh->b_count, 0); |
| 545 | } |
| 546 | } |
| 547 | pc->bh = bh; |
| 548 | } |
| 549 | |
Borislav Petkov | 5a04cfa | 2008-02-06 02:57:54 +0100 | [diff] [blame] | 550 | static void idetape_output_buffers(ide_drive_t *drive, idetape_pc_t *pc, |
| 551 | unsigned int bcount) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 552 | { |
| 553 | struct idetape_bh *bh = pc->bh; |
| 554 | int count; |
| 555 | |
| 556 | while (bcount) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 557 | if (bh == NULL) { |
Borislav Petkov | 5a04cfa | 2008-02-06 02:57:54 +0100 | [diff] [blame] | 558 | printk(KERN_ERR "ide-tape: bh == NULL in %s\n", |
| 559 | __func__); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 560 | return; |
| 561 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 562 | count = min((unsigned int)pc->b_count, (unsigned int)bcount); |
| 563 | HWIF(drive)->atapi_output_bytes(drive, pc->b_data, count); |
| 564 | bcount -= count; |
| 565 | pc->b_data += count; |
| 566 | pc->b_count -= count; |
| 567 | if (!pc->b_count) { |
Borislav Petkov | 5a04cfa | 2008-02-06 02:57:54 +0100 | [diff] [blame] | 568 | bh = bh->b_reqnext; |
| 569 | pc->bh = bh; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 570 | if (bh) { |
| 571 | pc->b_data = bh->b_data; |
| 572 | pc->b_count = atomic_read(&bh->b_count); |
| 573 | } |
| 574 | } |
| 575 | } |
| 576 | } |
| 577 | |
Borislav Petkov | 5a04cfa | 2008-02-06 02:57:54 +0100 | [diff] [blame] | 578 | static void idetape_update_buffers(idetape_pc_t *pc) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 579 | { |
| 580 | struct idetape_bh *bh = pc->bh; |
| 581 | int count; |
| 582 | unsigned int bcount = pc->actually_transferred; |
| 583 | |
| 584 | if (test_bit(PC_WRITING, &pc->flags)) |
| 585 | return; |
| 586 | while (bcount) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 587 | if (bh == NULL) { |
Borislav Petkov | 5a04cfa | 2008-02-06 02:57:54 +0100 | [diff] [blame] | 588 | printk(KERN_ERR "ide-tape: bh == NULL in %s\n", |
| 589 | __func__); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 590 | return; |
| 591 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 592 | count = min((unsigned int)bh->b_size, (unsigned int)bcount); |
| 593 | atomic_set(&bh->b_count, count); |
| 594 | if (atomic_read(&bh->b_count) == bh->b_size) |
| 595 | bh = bh->b_reqnext; |
| 596 | bcount -= count; |
| 597 | } |
| 598 | pc->bh = bh; |
| 599 | } |
| 600 | |
| 601 | /* |
| 602 | * idetape_next_pc_storage returns a pointer to a place in which we can |
| 603 | * safely store a packet command, even though we intend to leave the |
| 604 | * driver. A storage space for a maximum of IDETAPE_PC_STACK packet |
| 605 | * commands is allocated at initialization time. |
| 606 | */ |
Borislav Petkov | 5a04cfa | 2008-02-06 02:57:54 +0100 | [diff] [blame] | 607 | static idetape_pc_t *idetape_next_pc_storage(ide_drive_t *drive) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 608 | { |
| 609 | idetape_tape_t *tape = drive->driver_data; |
| 610 | |
Borislav Petkov | 8004a8c | 2008-02-06 02:57:51 +0100 | [diff] [blame] | 611 | debug_log(DBG_PCRQ_STACK, "pc_stack_index=%d\n", tape->pc_stack_index); |
| 612 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 613 | if (tape->pc_stack_index == IDETAPE_PC_STACK) |
Borislav Petkov | 5a04cfa | 2008-02-06 02:57:54 +0100 | [diff] [blame] | 614 | tape->pc_stack_index = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 615 | return (&tape->pc_stack[tape->pc_stack_index++]); |
| 616 | } |
| 617 | |
| 618 | /* |
| 619 | * idetape_next_rq_storage is used along with idetape_next_pc_storage. |
| 620 | * Since we queue packet commands in the request queue, we need to |
| 621 | * allocate a request, along with the allocation of a packet command. |
| 622 | */ |
Borislav Petkov | 5a04cfa | 2008-02-06 02:57:54 +0100 | [diff] [blame] | 623 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 624 | /************************************************************** |
| 625 | * * |
| 626 | * This should get fixed to use kmalloc(.., GFP_ATOMIC) * |
| 627 | * followed later on by kfree(). -ml * |
| 628 | * * |
| 629 | **************************************************************/ |
Borislav Petkov | 5a04cfa | 2008-02-06 02:57:54 +0100 | [diff] [blame] | 630 | |
| 631 | static struct request *idetape_next_rq_storage(ide_drive_t *drive) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 632 | { |
| 633 | idetape_tape_t *tape = drive->driver_data; |
| 634 | |
Borislav Petkov | 8004a8c | 2008-02-06 02:57:51 +0100 | [diff] [blame] | 635 | debug_log(DBG_PCRQ_STACK, "rq_stack_index=%d\n", tape->rq_stack_index); |
| 636 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 637 | if (tape->rq_stack_index == IDETAPE_PC_STACK) |
Borislav Petkov | 5a04cfa | 2008-02-06 02:57:54 +0100 | [diff] [blame] | 638 | tape->rq_stack_index = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 639 | return (&tape->rq_stack[tape->rq_stack_index++]); |
| 640 | } |
| 641 | |
Borislav Petkov | 5a04cfa | 2008-02-06 02:57:54 +0100 | [diff] [blame] | 642 | static void idetape_init_pc(idetape_pc_t *pc) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 643 | { |
| 644 | memset(pc->c, 0, 12); |
| 645 | pc->retries = 0; |
| 646 | pc->flags = 0; |
| 647 | pc->request_transfer = 0; |
| 648 | pc->buffer = pc->pc_buffer; |
| 649 | pc->buffer_size = IDETAPE_PC_BUFFER_SIZE; |
| 650 | pc->bh = NULL; |
| 651 | pc->b_data = NULL; |
| 652 | } |
| 653 | |
| 654 | /* |
Borislav Petkov | 1b5db43 | 2008-02-02 19:56:48 +0100 | [diff] [blame] | 655 | * called on each failed packet command retry to analyze the request sense. We |
| 656 | * currently do not utilize this information. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 657 | */ |
Borislav Petkov | 1b5db43 | 2008-02-02 19:56:48 +0100 | [diff] [blame] | 658 | static void idetape_analyze_error(ide_drive_t *drive, u8 *sense) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 659 | { |
| 660 | idetape_tape_t *tape = drive->driver_data; |
| 661 | idetape_pc_t *pc = tape->failed_pc; |
| 662 | |
Borislav Petkov | 1b5db43 | 2008-02-02 19:56:48 +0100 | [diff] [blame] | 663 | tape->sense_key = sense[2] & 0xF; |
| 664 | tape->asc = sense[12]; |
| 665 | tape->ascq = sense[13]; |
Borislav Petkov | 8004a8c | 2008-02-06 02:57:51 +0100 | [diff] [blame] | 666 | |
| 667 | debug_log(DBG_ERR, "pc = %x, sense key = %x, asc = %x, ascq = %x\n", |
| 668 | pc->c[0], tape->sense_key, tape->asc, tape->ascq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 669 | |
Borislav Petkov | 1b5db43 | 2008-02-02 19:56:48 +0100 | [diff] [blame] | 670 | /* Correct pc->actually_transferred by asking the tape. */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 671 | if (test_bit(PC_DMA_ERROR, &pc->flags)) { |
Borislav Petkov | 1b5db43 | 2008-02-02 19:56:48 +0100 | [diff] [blame] | 672 | pc->actually_transferred = pc->request_transfer - |
Borislav Petkov | 54bb207 | 2008-02-06 02:57:52 +0100 | [diff] [blame] | 673 | tape->blk_size * |
Borislav Petkov | 860ff5e | 2008-02-02 19:56:50 +0100 | [diff] [blame] | 674 | be32_to_cpu(get_unaligned((u32 *)&sense[3])); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 675 | idetape_update_buffers(pc); |
| 676 | } |
| 677 | |
| 678 | /* |
| 679 | * If error was the result of a zero-length read or write command, |
| 680 | * with sense key=5, asc=0x22, ascq=0, let it slide. Some drives |
| 681 | * (i.e. Seagate STT3401A Travan) don't support 0-length read/writes. |
| 682 | */ |
Borislav Petkov | 90699ce | 2008-02-02 19:56:50 +0100 | [diff] [blame] | 683 | if ((pc->c[0] == READ_6 || pc->c[0] == WRITE_6) |
Borislav Petkov | 1b5db43 | 2008-02-02 19:56:48 +0100 | [diff] [blame] | 684 | /* length == 0 */ |
| 685 | && pc->c[4] == 0 && pc->c[3] == 0 && pc->c[2] == 0) { |
| 686 | if (tape->sense_key == 5) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 687 | /* don't report an error, everything's ok */ |
| 688 | pc->error = 0; |
| 689 | /* don't retry read/write */ |
| 690 | set_bit(PC_ABORT, &pc->flags); |
| 691 | } |
| 692 | } |
Borislav Petkov | 90699ce | 2008-02-02 19:56:50 +0100 | [diff] [blame] | 693 | if (pc->c[0] == READ_6 && (sense[2] & 0x80)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 694 | pc->error = IDETAPE_ERROR_FILEMARK; |
| 695 | set_bit(PC_ABORT, &pc->flags); |
| 696 | } |
Borislav Petkov | 90699ce | 2008-02-02 19:56:50 +0100 | [diff] [blame] | 697 | if (pc->c[0] == WRITE_6) { |
Borislav Petkov | 1b5db43 | 2008-02-02 19:56:48 +0100 | [diff] [blame] | 698 | if ((sense[2] & 0x40) || (tape->sense_key == 0xd |
| 699 | && tape->asc == 0x0 && tape->ascq == 0x2)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 700 | pc->error = IDETAPE_ERROR_EOD; |
| 701 | set_bit(PC_ABORT, &pc->flags); |
| 702 | } |
| 703 | } |
Borislav Petkov | 90699ce | 2008-02-02 19:56:50 +0100 | [diff] [blame] | 704 | if (pc->c[0] == READ_6 || pc->c[0] == WRITE_6) { |
Borislav Petkov | 1b5db43 | 2008-02-02 19:56:48 +0100 | [diff] [blame] | 705 | if (tape->sense_key == 8) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 706 | pc->error = IDETAPE_ERROR_EOD; |
| 707 | set_bit(PC_ABORT, &pc->flags); |
| 708 | } |
| 709 | if (!test_bit(PC_ABORT, &pc->flags) && |
| 710 | pc->actually_transferred) |
| 711 | pc->retries = IDETAPE_MAX_PC_RETRIES + 1; |
| 712 | } |
| 713 | } |
| 714 | |
Borislav Petkov | 419d474 | 2008-02-02 19:56:51 +0100 | [diff] [blame] | 715 | static void idetape_activate_next_stage(ide_drive_t *drive) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 716 | { |
| 717 | idetape_tape_t *tape = drive->driver_data; |
| 718 | idetape_stage_t *stage = tape->next_stage; |
| 719 | struct request *rq = &stage->rq; |
| 720 | |
Borislav Petkov | 8004a8c | 2008-02-06 02:57:51 +0100 | [diff] [blame] | 721 | debug_log(DBG_PROCS, "Enter %s\n", __func__); |
| 722 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 723 | if (stage == NULL) { |
Borislav Petkov | 8004a8c | 2008-02-06 02:57:51 +0100 | [diff] [blame] | 724 | printk(KERN_ERR "ide-tape: bug: Trying to activate a non" |
| 725 | " existing stage\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 726 | return; |
| 727 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 728 | |
| 729 | rq->rq_disk = tape->disk; |
| 730 | rq->buffer = NULL; |
| 731 | rq->special = (void *)stage->bh; |
Borislav Petkov | 54bb207 | 2008-02-06 02:57:52 +0100 | [diff] [blame] | 732 | tape->active_data_rq = rq; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 733 | tape->active_stage = stage; |
| 734 | tape->next_stage = stage->next; |
| 735 | } |
| 736 | |
Borislav Petkov | 3c98bf3 | 2008-02-06 02:57:53 +0100 | [diff] [blame] | 737 | /* Free a stage along with its related buffers completely. */ |
Borislav Petkov | 5a04cfa | 2008-02-06 02:57:54 +0100 | [diff] [blame] | 738 | static void __idetape_kfree_stage(idetape_stage_t *stage) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 739 | { |
| 740 | struct idetape_bh *prev_bh, *bh = stage->bh; |
| 741 | int size; |
| 742 | |
| 743 | while (bh != NULL) { |
| 744 | if (bh->b_data != NULL) { |
| 745 | size = (int) bh->b_size; |
| 746 | while (size > 0) { |
| 747 | free_page((unsigned long) bh->b_data); |
| 748 | size -= PAGE_SIZE; |
| 749 | bh->b_data += PAGE_SIZE; |
| 750 | } |
| 751 | } |
| 752 | prev_bh = bh; |
| 753 | bh = bh->b_reqnext; |
| 754 | kfree(prev_bh); |
| 755 | } |
| 756 | kfree(stage); |
| 757 | } |
| 758 | |
Borislav Petkov | 5a04cfa | 2008-02-06 02:57:54 +0100 | [diff] [blame] | 759 | static void idetape_kfree_stage(idetape_tape_t *tape, idetape_stage_t *stage) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 760 | { |
| 761 | __idetape_kfree_stage(stage); |
| 762 | } |
| 763 | |
| 764 | /* |
Borislav Petkov | 3c98bf3 | 2008-02-06 02:57:53 +0100 | [diff] [blame] | 765 | * Remove tape->first_stage from the pipeline. The caller should avoid race |
| 766 | * conditions. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 767 | */ |
Borislav Petkov | 5a04cfa | 2008-02-06 02:57:54 +0100 | [diff] [blame] | 768 | static void idetape_remove_stage_head(ide_drive_t *drive) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 769 | { |
| 770 | idetape_tape_t *tape = drive->driver_data; |
| 771 | idetape_stage_t *stage; |
Borislav Petkov | 8004a8c | 2008-02-06 02:57:51 +0100 | [diff] [blame] | 772 | |
| 773 | debug_log(DBG_PROCS, "Enter %s\n", __func__); |
| 774 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 775 | if (tape->first_stage == NULL) { |
| 776 | printk(KERN_ERR "ide-tape: bug: tape->first_stage is NULL\n"); |
Borislav Petkov | 55a5d29 | 2008-02-02 19:56:49 +0100 | [diff] [blame] | 777 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 778 | } |
| 779 | if (tape->active_stage == tape->first_stage) { |
Borislav Petkov | 8004a8c | 2008-02-06 02:57:51 +0100 | [diff] [blame] | 780 | printk(KERN_ERR "ide-tape: bug: Trying to free our active " |
| 781 | "pipeline stage\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 782 | return; |
| 783 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 784 | stage = tape->first_stage; |
| 785 | tape->first_stage = stage->next; |
| 786 | idetape_kfree_stage(tape, stage); |
| 787 | tape->nr_stages--; |
| 788 | if (tape->first_stage == NULL) { |
| 789 | tape->last_stage = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 790 | if (tape->next_stage != NULL) |
Borislav Petkov | 5a04cfa | 2008-02-06 02:57:54 +0100 | [diff] [blame] | 791 | printk(KERN_ERR "ide-tape: bug: tape->next_stage !=" |
| 792 | " NULL\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 793 | if (tape->nr_stages) |
Borislav Petkov | 5a04cfa | 2008-02-06 02:57:54 +0100 | [diff] [blame] | 794 | printk(KERN_ERR "ide-tape: bug: nr_stages should be 0 " |
| 795 | "now\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 796 | } |
| 797 | } |
| 798 | |
| 799 | /* |
| 800 | * This will free all the pipeline stages starting from new_last_stage->next |
| 801 | * to the end of the list, and point tape->last_stage to new_last_stage. |
| 802 | */ |
| 803 | static void idetape_abort_pipeline(ide_drive_t *drive, |
| 804 | idetape_stage_t *new_last_stage) |
| 805 | { |
| 806 | idetape_tape_t *tape = drive->driver_data; |
| 807 | idetape_stage_t *stage = new_last_stage->next; |
| 808 | idetape_stage_t *nstage; |
| 809 | |
Borislav Petkov | 8004a8c | 2008-02-06 02:57:51 +0100 | [diff] [blame] | 810 | debug_log(DBG_PROCS, "%s: Enter %s\n", tape->name, __func__); |
| 811 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 812 | while (stage) { |
| 813 | nstage = stage->next; |
| 814 | idetape_kfree_stage(tape, stage); |
| 815 | --tape->nr_stages; |
| 816 | --tape->nr_pending_stages; |
| 817 | stage = nstage; |
| 818 | } |
| 819 | if (new_last_stage) |
| 820 | new_last_stage->next = NULL; |
| 821 | tape->last_stage = new_last_stage; |
| 822 | tape->next_stage = NULL; |
| 823 | } |
| 824 | |
| 825 | /* |
Borislav Petkov | 3c98bf3 | 2008-02-06 02:57:53 +0100 | [diff] [blame] | 826 | * Finish servicing a request and insert a pending pipeline request into the |
| 827 | * main device queue. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 828 | */ |
| 829 | static int idetape_end_request(ide_drive_t *drive, int uptodate, int nr_sects) |
| 830 | { |
| 831 | struct request *rq = HWGROUP(drive)->rq; |
| 832 | idetape_tape_t *tape = drive->driver_data; |
| 833 | unsigned long flags; |
| 834 | int error; |
| 835 | int remove_stage = 0; |
| 836 | idetape_stage_t *active_stage; |
| 837 | |
Borislav Petkov | 8004a8c | 2008-02-06 02:57:51 +0100 | [diff] [blame] | 838 | debug_log(DBG_PROCS, "Enter %s\n", __func__); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 839 | |
| 840 | switch (uptodate) { |
Borislav Petkov | 5a04cfa | 2008-02-06 02:57:54 +0100 | [diff] [blame] | 841 | case 0: error = IDETAPE_ERROR_GENERAL; break; |
| 842 | case 1: error = 0; break; |
| 843 | default: error = uptodate; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 844 | } |
| 845 | rq->errors = error; |
| 846 | if (error) |
| 847 | tape->failed_pc = NULL; |
| 848 | |
Bartlomiej Zolnierkiewicz | 3687221 | 2008-01-26 20:13:10 +0100 | [diff] [blame] | 849 | if (!blk_special_request(rq)) { |
| 850 | ide_end_request(drive, uptodate, nr_sects); |
| 851 | return 0; |
| 852 | } |
| 853 | |
Borislav Petkov | 54bb207 | 2008-02-06 02:57:52 +0100 | [diff] [blame] | 854 | spin_lock_irqsave(&tape->lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 855 | |
| 856 | /* The request was a pipelined data transfer request */ |
Borislav Petkov | 54bb207 | 2008-02-06 02:57:52 +0100 | [diff] [blame] | 857 | if (tape->active_data_rq == rq) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 858 | active_stage = tape->active_stage; |
| 859 | tape->active_stage = NULL; |
Borislav Petkov | 54bb207 | 2008-02-06 02:57:52 +0100 | [diff] [blame] | 860 | tape->active_data_rq = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 861 | tape->nr_pending_stages--; |
| 862 | if (rq->cmd[0] & REQ_IDETAPE_WRITE) { |
| 863 | remove_stage = 1; |
| 864 | if (error) { |
| 865 | set_bit(IDETAPE_PIPELINE_ERROR, &tape->flags); |
| 866 | if (error == IDETAPE_ERROR_EOD) |
Borislav Petkov | 5a04cfa | 2008-02-06 02:57:54 +0100 | [diff] [blame] | 867 | idetape_abort_pipeline(drive, |
| 868 | active_stage); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 869 | } |
| 870 | } else if (rq->cmd[0] & REQ_IDETAPE_READ) { |
| 871 | if (error == IDETAPE_ERROR_EOD) { |
| 872 | set_bit(IDETAPE_PIPELINE_ERROR, &tape->flags); |
| 873 | idetape_abort_pipeline(drive, active_stage); |
| 874 | } |
| 875 | } |
| 876 | if (tape->next_stage != NULL) { |
Borislav Petkov | 419d474 | 2008-02-02 19:56:51 +0100 | [diff] [blame] | 877 | idetape_activate_next_stage(drive); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 878 | |
Borislav Petkov | 3c98bf3 | 2008-02-06 02:57:53 +0100 | [diff] [blame] | 879 | /* Insert the next request into the request queue. */ |
Borislav Petkov | 54bb207 | 2008-02-06 02:57:52 +0100 | [diff] [blame] | 880 | (void)ide_do_drive_cmd(drive, tape->active_data_rq, |
| 881 | ide_end); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 882 | } else if (!error) { |
Borislav Petkov | 9721985 | 2008-02-06 02:57:52 +0100 | [diff] [blame] | 883 | /* |
| 884 | * This is a part of the feedback loop which tries to |
| 885 | * find the optimum number of stages. We are starting |
| 886 | * from a minimum maximum number of stages, and if we |
| 887 | * sense that the pipeline is empty, we try to increase |
| 888 | * it, until we reach the user compile time memory |
| 889 | * limit. |
| 890 | */ |
| 891 | int i = (tape->max_pipeline - tape->min_pipeline) / 10; |
| 892 | |
| 893 | tape->max_stages += max(i, 1); |
| 894 | tape->max_stages = max(tape->max_stages, |
| 895 | tape->min_pipeline); |
| 896 | tape->max_stages = min(tape->max_stages, |
| 897 | tape->max_pipeline); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 898 | } |
| 899 | } |
| 900 | ide_end_drive_cmd(drive, 0, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 901 | |
| 902 | if (remove_stage) |
| 903 | idetape_remove_stage_head(drive); |
Borislav Petkov | 54bb207 | 2008-02-06 02:57:52 +0100 | [diff] [blame] | 904 | if (tape->active_data_rq == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 905 | clear_bit(IDETAPE_PIPELINE_ACTIVE, &tape->flags); |
Borislav Petkov | 54bb207 | 2008-02-06 02:57:52 +0100 | [diff] [blame] | 906 | spin_unlock_irqrestore(&tape->lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 907 | return 0; |
| 908 | } |
| 909 | |
Borislav Petkov | 5a04cfa | 2008-02-06 02:57:54 +0100 | [diff] [blame] | 910 | static ide_startstop_t idetape_request_sense_callback(ide_drive_t *drive) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 911 | { |
| 912 | idetape_tape_t *tape = drive->driver_data; |
| 913 | |
Borislav Petkov | 8004a8c | 2008-02-06 02:57:51 +0100 | [diff] [blame] | 914 | debug_log(DBG_PROCS, "Enter %s\n", __func__); |
| 915 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 916 | if (!tape->pc->error) { |
Borislav Petkov | 1b5db43 | 2008-02-02 19:56:48 +0100 | [diff] [blame] | 917 | idetape_analyze_error(drive, tape->pc->buffer); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 918 | idetape_end_request(drive, 1, 0); |
| 919 | } else { |
Borislav Petkov | 5a04cfa | 2008-02-06 02:57:54 +0100 | [diff] [blame] | 920 | printk(KERN_ERR "ide-tape: Error in REQUEST SENSE itself - " |
| 921 | "Aborting request!\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 922 | idetape_end_request(drive, 0, 0); |
| 923 | } |
| 924 | return ide_stopped; |
| 925 | } |
| 926 | |
Borislav Petkov | 5a04cfa | 2008-02-06 02:57:54 +0100 | [diff] [blame] | 927 | static void idetape_create_request_sense_cmd(idetape_pc_t *pc) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 928 | { |
Borislav Petkov | 5a04cfa | 2008-02-06 02:57:54 +0100 | [diff] [blame] | 929 | idetape_init_pc(pc); |
Borislav Petkov | 90699ce | 2008-02-02 19:56:50 +0100 | [diff] [blame] | 930 | pc->c[0] = REQUEST_SENSE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 931 | pc->c[4] = 20; |
| 932 | pc->request_transfer = 20; |
| 933 | pc->callback = &idetape_request_sense_callback; |
| 934 | } |
| 935 | |
| 936 | static void idetape_init_rq(struct request *rq, u8 cmd) |
| 937 | { |
| 938 | memset(rq, 0, sizeof(*rq)); |
Jens Axboe | 4aff5e2 | 2006-08-10 08:44:47 +0200 | [diff] [blame] | 939 | rq->cmd_type = REQ_TYPE_SPECIAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 940 | rq->cmd[0] = cmd; |
| 941 | } |
| 942 | |
| 943 | /* |
Borislav Petkov | 3c98bf3 | 2008-02-06 02:57:53 +0100 | [diff] [blame] | 944 | * Generate a new packet command request in front of the request queue, before |
| 945 | * the current request, so that it will be processed immediately, on the next |
| 946 | * pass through the driver. The function below is called from the request |
| 947 | * handling part of the driver (the "bottom" part). Safe storage for the request |
| 948 | * 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] | 949 | * |
Borislav Petkov | 3c98bf3 | 2008-02-06 02:57:53 +0100 | [diff] [blame] | 950 | * Memory for those requests is pre-allocated at initialization time, and is |
| 951 | * limited to IDETAPE_PC_STACK requests. We assume that we have enough space for |
| 952 | * the maximum possible number of inter-dependent packet commands. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 953 | * |
Borislav Petkov | 3c98bf3 | 2008-02-06 02:57:53 +0100 | [diff] [blame] | 954 | * The higher level of the driver - The ioctl handler and the character device |
| 955 | * handling functions should queue request to the lower level part and wait for |
| 956 | * their completion using idetape_queue_pc_tail or idetape_queue_rw_tail. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 957 | */ |
Borislav Petkov | 5a04cfa | 2008-02-06 02:57:54 +0100 | [diff] [blame] | 958 | static void idetape_queue_pc_head(ide_drive_t *drive, idetape_pc_t *pc, |
| 959 | struct request *rq) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 960 | { |
| 961 | struct ide_tape_obj *tape = drive->driver_data; |
| 962 | |
| 963 | idetape_init_rq(rq, REQ_IDETAPE_PC1); |
| 964 | rq->buffer = (char *) pc; |
| 965 | rq->rq_disk = tape->disk; |
| 966 | (void) ide_do_drive_cmd(drive, rq, ide_preempt); |
| 967 | } |
| 968 | |
| 969 | /* |
| 970 | * idetape_retry_pc is called when an error was detected during the |
| 971 | * last packet command. We queue a request sense packet command in |
| 972 | * the head of the request list. |
| 973 | */ |
| 974 | static ide_startstop_t idetape_retry_pc (ide_drive_t *drive) |
| 975 | { |
| 976 | idetape_tape_t *tape = drive->driver_data; |
| 977 | idetape_pc_t *pc; |
| 978 | struct request *rq; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 979 | |
Bartlomiej Zolnierkiewicz | 64a57fe | 2008-02-06 02:57:51 +0100 | [diff] [blame] | 980 | (void)ide_read_error(drive); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 981 | pc = idetape_next_pc_storage(drive); |
| 982 | rq = idetape_next_rq_storage(drive); |
| 983 | idetape_create_request_sense_cmd(pc); |
| 984 | set_bit(IDETAPE_IGNORE_DSC, &tape->flags); |
| 985 | idetape_queue_pc_head(drive, pc, rq); |
| 986 | return ide_stopped; |
| 987 | } |
| 988 | |
| 989 | /* |
Borislav Petkov | 3c98bf3 | 2008-02-06 02:57:53 +0100 | [diff] [blame] | 990 | * Postpone the current request so that ide.c will be able to service requests |
| 991 | * 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] | 992 | */ |
Borislav Petkov | 5a04cfa | 2008-02-06 02:57:54 +0100 | [diff] [blame] | 993 | static void idetape_postpone_request(ide_drive_t *drive) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 994 | { |
| 995 | idetape_tape_t *tape = drive->driver_data; |
| 996 | |
Borislav Petkov | 8004a8c | 2008-02-06 02:57:51 +0100 | [diff] [blame] | 997 | debug_log(DBG_PROCS, "Enter %s\n", __func__); |
| 998 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 999 | tape->postponed_rq = HWGROUP(drive)->rq; |
Borislav Petkov | 54bb207 | 2008-02-06 02:57:52 +0100 | [diff] [blame] | 1000 | ide_stall_queue(drive, tape->dsc_poll_freq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1001 | } |
| 1002 | |
Borislav Petkov | a1efc85 | 2008-02-06 02:57:52 +0100 | [diff] [blame] | 1003 | typedef void idetape_io_buf(ide_drive_t *, idetape_pc_t *, unsigned int); |
| 1004 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1005 | /* |
Borislav Petkov | a1efc85 | 2008-02-06 02:57:52 +0100 | [diff] [blame] | 1006 | * This is the usual interrupt handler which will be called during a packet |
| 1007 | * command. We will transfer some of the data (as requested by the drive) and |
| 1008 | * will re-point interrupt handler to us. When data transfer is finished, we |
| 1009 | * will act according to the algorithm described before |
Borislav Petkov | 8d06bfa | 2008-02-06 02:57:53 +0100 | [diff] [blame] | 1010 | * idetape_issue_pc. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1011 | */ |
Borislav Petkov | a1efc85 | 2008-02-06 02:57:52 +0100 | [diff] [blame] | 1012 | static ide_startstop_t idetape_pc_intr(ide_drive_t *drive) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1013 | { |
| 1014 | ide_hwif_t *hwif = drive->hwif; |
| 1015 | idetape_tape_t *tape = drive->driver_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1016 | idetape_pc_t *pc = tape->pc; |
Borislav Petkov | a1efc85 | 2008-02-06 02:57:52 +0100 | [diff] [blame] | 1017 | xfer_func_t *xferfunc; |
| 1018 | idetape_io_buf *iobuf; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1019 | unsigned int temp; |
| 1020 | #if SIMULATE_ERRORS |
Borislav Petkov | 5a04cfa | 2008-02-06 02:57:54 +0100 | [diff] [blame] | 1021 | static int error_sim_count; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1022 | #endif |
Bartlomiej Zolnierkiewicz | 790d123 | 2008-01-25 22:17:12 +0100 | [diff] [blame] | 1023 | u16 bcount; |
Bartlomiej Zolnierkiewicz | 8e7657a | 2008-01-25 22:17:12 +0100 | [diff] [blame] | 1024 | u8 stat, ireason; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1025 | |
Borislav Petkov | 8004a8c | 2008-02-06 02:57:51 +0100 | [diff] [blame] | 1026 | debug_log(DBG_PROCS, "Enter %s - interrupt handler\n", __func__); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1027 | |
| 1028 | /* Clear the interrupt */ |
Bartlomiej Zolnierkiewicz | c47137a | 2008-02-06 02:57:51 +0100 | [diff] [blame] | 1029 | stat = ide_read_status(drive); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1030 | |
| 1031 | if (test_bit(PC_DMA_IN_PROGRESS, &pc->flags)) { |
Bartlomiej Zolnierkiewicz | 22c525b | 2008-01-25 22:17:11 +0100 | [diff] [blame] | 1032 | if (hwif->ide_dma_end(drive) || (stat & ERR_STAT)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1033 | /* |
| 1034 | * A DMA error is sometimes expected. For example, |
| 1035 | * if the tape is crossing a filemark during a |
| 1036 | * READ command, it will issue an irq and position |
| 1037 | * itself before the filemark, so that only a partial |
| 1038 | * data transfer will occur (which causes the DMA |
| 1039 | * error). In that case, we will later ask the tape |
| 1040 | * how much bytes of the original request were |
| 1041 | * actually transferred (we can't receive that |
| 1042 | * information from the DMA engine on most chipsets). |
| 1043 | */ |
| 1044 | |
| 1045 | /* |
| 1046 | * On the contrary, a DMA error is never expected; |
| 1047 | * it usually indicates a hardware error or abort. |
| 1048 | * If the tape crosses a filemark during a READ |
| 1049 | * command, it will issue an irq and position itself |
| 1050 | * after the filemark (not before). Only a partial |
| 1051 | * data transfer will occur, but no DMA error. |
| 1052 | * (AS, 19 Apr 2001) |
| 1053 | */ |
| 1054 | set_bit(PC_DMA_ERROR, &pc->flags); |
| 1055 | } else { |
| 1056 | pc->actually_transferred = pc->request_transfer; |
| 1057 | idetape_update_buffers(pc); |
| 1058 | } |
Borislav Petkov | 8004a8c | 2008-02-06 02:57:51 +0100 | [diff] [blame] | 1059 | debug_log(DBG_PROCS, "DMA finished\n"); |
| 1060 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1061 | } |
| 1062 | |
| 1063 | /* No more interrupts */ |
Bartlomiej Zolnierkiewicz | 22c525b | 2008-01-25 22:17:11 +0100 | [diff] [blame] | 1064 | if ((stat & DRQ_STAT) == 0) { |
Borislav Petkov | 8004a8c | 2008-02-06 02:57:51 +0100 | [diff] [blame] | 1065 | debug_log(DBG_SENSE, "Packet command completed, %d bytes" |
| 1066 | " transferred\n", pc->actually_transferred); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1067 | |
Borislav Petkov | 8004a8c | 2008-02-06 02:57:51 +0100 | [diff] [blame] | 1068 | clear_bit(PC_DMA_IN_PROGRESS, &pc->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1069 | local_irq_enable(); |
| 1070 | |
| 1071 | #if SIMULATE_ERRORS |
Borislav Petkov | 90699ce | 2008-02-02 19:56:50 +0100 | [diff] [blame] | 1072 | if ((pc->c[0] == WRITE_6 || pc->c[0] == READ_6) && |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1073 | (++error_sim_count % 100) == 0) { |
| 1074 | printk(KERN_INFO "ide-tape: %s: simulating error\n", |
| 1075 | tape->name); |
Bartlomiej Zolnierkiewicz | 22c525b | 2008-01-25 22:17:11 +0100 | [diff] [blame] | 1076 | stat |= ERR_STAT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1077 | } |
| 1078 | #endif |
Borislav Petkov | 90699ce | 2008-02-02 19:56:50 +0100 | [diff] [blame] | 1079 | if ((stat & ERR_STAT) && pc->c[0] == REQUEST_SENSE) |
Bartlomiej Zolnierkiewicz | 22c525b | 2008-01-25 22:17:11 +0100 | [diff] [blame] | 1080 | stat &= ~ERR_STAT; |
| 1081 | if ((stat & ERR_STAT) || test_bit(PC_DMA_ERROR, &pc->flags)) { |
| 1082 | /* Error detected */ |
Borislav Petkov | 8004a8c | 2008-02-06 02:57:51 +0100 | [diff] [blame] | 1083 | debug_log(DBG_ERR, "%s: I/O error\n", tape->name); |
| 1084 | |
Borislav Petkov | 90699ce | 2008-02-02 19:56:50 +0100 | [diff] [blame] | 1085 | if (pc->c[0] == REQUEST_SENSE) { |
Borislav Petkov | a1efc85 | 2008-02-06 02:57:52 +0100 | [diff] [blame] | 1086 | printk(KERN_ERR "ide-tape: I/O error in request" |
| 1087 | " sense command\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1088 | return ide_do_reset(drive); |
| 1089 | } |
Borislav Petkov | 8004a8c | 2008-02-06 02:57:51 +0100 | [diff] [blame] | 1090 | debug_log(DBG_ERR, "[cmd %x]: check condition\n", |
| 1091 | pc->c[0]); |
| 1092 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1093 | /* Retry operation */ |
| 1094 | return idetape_retry_pc(drive); |
| 1095 | } |
| 1096 | pc->error = 0; |
| 1097 | if (test_bit(PC_WAIT_FOR_DSC, &pc->flags) && |
Bartlomiej Zolnierkiewicz | 22c525b | 2008-01-25 22:17:11 +0100 | [diff] [blame] | 1098 | (stat & SEEK_STAT) == 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1099 | /* Media access command */ |
| 1100 | tape->dsc_polling_start = jiffies; |
Borislav Petkov | 54bb207 | 2008-02-06 02:57:52 +0100 | [diff] [blame] | 1101 | tape->dsc_poll_freq = IDETAPE_DSC_MA_FAST; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1102 | tape->dsc_timeout = jiffies + IDETAPE_DSC_MA_TIMEOUT; |
| 1103 | /* Allow ide.c to handle other requests */ |
| 1104 | idetape_postpone_request(drive); |
| 1105 | return ide_stopped; |
| 1106 | } |
| 1107 | if (tape->failed_pc == pc) |
| 1108 | tape->failed_pc = NULL; |
| 1109 | /* Command finished - Call the callback function */ |
| 1110 | return pc->callback(drive); |
| 1111 | } |
| 1112 | if (test_and_clear_bit(PC_DMA_IN_PROGRESS, &pc->flags)) { |
| 1113 | printk(KERN_ERR "ide-tape: The tape wants to issue more " |
| 1114 | "interrupts in DMA mode\n"); |
| 1115 | printk(KERN_ERR "ide-tape: DMA disabled, reverting to PIO\n"); |
Bartlomiej Zolnierkiewicz | 7469aaf | 2007-02-17 02:40:26 +0100 | [diff] [blame] | 1116 | ide_dma_off(drive); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1117 | return ide_do_reset(drive); |
| 1118 | } |
| 1119 | /* Get the number of bytes to transfer on this interrupt. */ |
Bartlomiej Zolnierkiewicz | 23579a2 | 2008-04-18 00:46:26 +0200 | [diff] [blame] | 1120 | bcount = (hwif->INB(hwif->io_ports[IDE_BCOUNTH_OFFSET]) << 8) | |
| 1121 | hwif->INB(hwif->io_ports[IDE_BCOUNTL_OFFSET]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1122 | |
Bartlomiej Zolnierkiewicz | 23579a2 | 2008-04-18 00:46:26 +0200 | [diff] [blame] | 1123 | ireason = hwif->INB(hwif->io_ports[IDE_IREASON_OFFSET]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1124 | |
Bartlomiej Zolnierkiewicz | 8e7657a | 2008-01-25 22:17:12 +0100 | [diff] [blame] | 1125 | if (ireason & CD) { |
Borislav Petkov | a1efc85 | 2008-02-06 02:57:52 +0100 | [diff] [blame] | 1126 | printk(KERN_ERR "ide-tape: CoD != 0 in %s\n", __func__); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1127 | return ide_do_reset(drive); |
| 1128 | } |
Bartlomiej Zolnierkiewicz | 8e7657a | 2008-01-25 22:17:12 +0100 | [diff] [blame] | 1129 | if (((ireason & IO) == IO) == test_bit(PC_WRITING, &pc->flags)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1130 | /* Hopefully, we will never get here */ |
| 1131 | printk(KERN_ERR "ide-tape: We wanted to %s, ", |
Bartlomiej Zolnierkiewicz | 8e7657a | 2008-01-25 22:17:12 +0100 | [diff] [blame] | 1132 | (ireason & IO) ? "Write" : "Read"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1133 | printk(KERN_ERR "ide-tape: but the tape wants us to %s !\n", |
Bartlomiej Zolnierkiewicz | 8e7657a | 2008-01-25 22:17:12 +0100 | [diff] [blame] | 1134 | (ireason & IO) ? "Read" : "Write"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1135 | return ide_do_reset(drive); |
| 1136 | } |
| 1137 | if (!test_bit(PC_WRITING, &pc->flags)) { |
| 1138 | /* Reading - Check that we have enough space */ |
Bartlomiej Zolnierkiewicz | 790d123 | 2008-01-25 22:17:12 +0100 | [diff] [blame] | 1139 | temp = pc->actually_transferred + bcount; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1140 | if (temp > pc->request_transfer) { |
| 1141 | if (temp > pc->buffer_size) { |
Borislav Petkov | a1efc85 | 2008-02-06 02:57:52 +0100 | [diff] [blame] | 1142 | printk(KERN_ERR "ide-tape: The tape wants to " |
| 1143 | "send us more data than expected " |
| 1144 | "- discarding data\n"); |
Bartlomiej Zolnierkiewicz | 7616c0a | 2008-04-18 00:46:26 +0200 | [diff] [blame] | 1145 | ide_atapi_discard_data(drive, bcount); |
Borislav Petkov | a1efc85 | 2008-02-06 02:57:52 +0100 | [diff] [blame] | 1146 | ide_set_handler(drive, &idetape_pc_intr, |
| 1147 | IDETAPE_WAIT_CMD, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1148 | return ide_started; |
| 1149 | } |
Borislav Petkov | 8004a8c | 2008-02-06 02:57:51 +0100 | [diff] [blame] | 1150 | debug_log(DBG_SENSE, "The tape wants to send us more " |
| 1151 | "data than expected - allowing transfer\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1152 | } |
Borislav Petkov | a1efc85 | 2008-02-06 02:57:52 +0100 | [diff] [blame] | 1153 | iobuf = &idetape_input_buffers; |
| 1154 | xferfunc = hwif->atapi_input_bytes; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1155 | } else { |
Borislav Petkov | a1efc85 | 2008-02-06 02:57:52 +0100 | [diff] [blame] | 1156 | iobuf = &idetape_output_buffers; |
| 1157 | xferfunc = hwif->atapi_output_bytes; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1158 | } |
Borislav Petkov | a1efc85 | 2008-02-06 02:57:52 +0100 | [diff] [blame] | 1159 | |
| 1160 | if (pc->bh) |
| 1161 | iobuf(drive, pc, bcount); |
| 1162 | else |
| 1163 | xferfunc(drive, pc->current_position, bcount); |
| 1164 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1165 | /* Update the current position */ |
Bartlomiej Zolnierkiewicz | 790d123 | 2008-01-25 22:17:12 +0100 | [diff] [blame] | 1166 | pc->actually_transferred += bcount; |
| 1167 | pc->current_position += bcount; |
Borislav Petkov | 8004a8c | 2008-02-06 02:57:51 +0100 | [diff] [blame] | 1168 | |
| 1169 | debug_log(DBG_SENSE, "[cmd %x] transferred %d bytes on that intr.\n", |
| 1170 | pc->c[0], bcount); |
| 1171 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1172 | /* And set the interrupt handler again */ |
| 1173 | ide_set_handler(drive, &idetape_pc_intr, IDETAPE_WAIT_CMD, NULL); |
| 1174 | return ide_started; |
| 1175 | } |
| 1176 | |
| 1177 | /* |
Borislav Petkov | 3c98bf3 | 2008-02-06 02:57:53 +0100 | [diff] [blame] | 1178 | * Packet Command Interface |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1179 | * |
Borislav Petkov | 3c98bf3 | 2008-02-06 02:57:53 +0100 | [diff] [blame] | 1180 | * The current Packet Command is available in tape->pc, and will not change |
| 1181 | * until we finish handling it. Each packet command is associated with a |
| 1182 | * callback function that will be called when the command is finished. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1183 | * |
Borislav Petkov | 3c98bf3 | 2008-02-06 02:57:53 +0100 | [diff] [blame] | 1184 | * The handling will be done in three stages: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1185 | * |
Borislav Petkov | 3c98bf3 | 2008-02-06 02:57:53 +0100 | [diff] [blame] | 1186 | * 1. idetape_issue_pc will send the packet command to the drive, and will set |
| 1187 | * the interrupt handler to idetape_pc_intr. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1188 | * |
Borislav Petkov | 3c98bf3 | 2008-02-06 02:57:53 +0100 | [diff] [blame] | 1189 | * 2. On each interrupt, idetape_pc_intr will be called. This step will be |
| 1190 | * 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] | 1191 | * |
Borislav Petkov | 3c98bf3 | 2008-02-06 02:57:53 +0100 | [diff] [blame] | 1192 | * 3. ATAPI Tape media access commands have immediate status with a delayed |
| 1193 | * process. In case of a successful initiation of a media access packet command, |
| 1194 | * the DSC bit will be set when the actual execution of the command is finished. |
| 1195 | * Since the tape drive will not issue an interrupt, we have to poll for this |
| 1196 | * event. In this case, we define the request as "low priority request" by |
| 1197 | * setting rq_status to IDETAPE_RQ_POSTPONED, set a timer to poll for DSC and |
| 1198 | * exit the driver. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1199 | * |
Borislav Petkov | 3c98bf3 | 2008-02-06 02:57:53 +0100 | [diff] [blame] | 1200 | * ide.c will then give higher priority to requests which originate from the |
| 1201 | * other device, until will change rq_status to RQ_ACTIVE. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1202 | * |
Borislav Petkov | 3c98bf3 | 2008-02-06 02:57:53 +0100 | [diff] [blame] | 1203 | * 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] | 1204 | * |
Borislav Petkov | 3c98bf3 | 2008-02-06 02:57:53 +0100 | [diff] [blame] | 1205 | * 5. In case an error was found, we queue a request sense packet command in |
| 1206 | * front of the request queue and retry the operation up to |
| 1207 | * IDETAPE_MAX_PC_RETRIES times. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1208 | * |
Borislav Petkov | 3c98bf3 | 2008-02-06 02:57:53 +0100 | [diff] [blame] | 1209 | * 6. In case no error was found, or we decided to give up and not to retry |
| 1210 | * again, the callback function will be called and then we will handle the next |
| 1211 | * request. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1212 | */ |
| 1213 | static ide_startstop_t idetape_transfer_pc(ide_drive_t *drive) |
| 1214 | { |
| 1215 | ide_hwif_t *hwif = drive->hwif; |
| 1216 | idetape_tape_t *tape = drive->driver_data; |
| 1217 | idetape_pc_t *pc = tape->pc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1218 | int retries = 100; |
| 1219 | ide_startstop_t startstop; |
Bartlomiej Zolnierkiewicz | 8e7657a | 2008-01-25 22:17:12 +0100 | [diff] [blame] | 1220 | u8 ireason; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1221 | |
Borislav Petkov | 5a04cfa | 2008-02-06 02:57:54 +0100 | [diff] [blame] | 1222 | if (ide_wait_stat(&startstop, drive, DRQ_STAT, BUSY_STAT, WAIT_READY)) { |
| 1223 | printk(KERN_ERR "ide-tape: Strange, packet command initiated " |
| 1224 | "yet DRQ isn't asserted\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1225 | return startstop; |
| 1226 | } |
Bartlomiej Zolnierkiewicz | 23579a2 | 2008-04-18 00:46:26 +0200 | [diff] [blame] | 1227 | ireason = hwif->INB(hwif->io_ports[IDE_IREASON_OFFSET]); |
Bartlomiej Zolnierkiewicz | 8e7657a | 2008-01-25 22:17:12 +0100 | [diff] [blame] | 1228 | while (retries-- && ((ireason & CD) == 0 || (ireason & IO))) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1229 | printk(KERN_ERR "ide-tape: (IO,CoD != (0,1) while issuing " |
| 1230 | "a packet command, retrying\n"); |
| 1231 | udelay(100); |
Bartlomiej Zolnierkiewicz | 23579a2 | 2008-04-18 00:46:26 +0200 | [diff] [blame] | 1232 | ireason = hwif->INB(hwif->io_ports[IDE_IREASON_OFFSET]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1233 | if (retries == 0) { |
| 1234 | printk(KERN_ERR "ide-tape: (IO,CoD != (0,1) while " |
| 1235 | "issuing a packet command, ignoring\n"); |
Bartlomiej Zolnierkiewicz | 8e7657a | 2008-01-25 22:17:12 +0100 | [diff] [blame] | 1236 | ireason |= CD; |
| 1237 | ireason &= ~IO; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1238 | } |
| 1239 | } |
Bartlomiej Zolnierkiewicz | 8e7657a | 2008-01-25 22:17:12 +0100 | [diff] [blame] | 1240 | if ((ireason & CD) == 0 || (ireason & IO)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1241 | printk(KERN_ERR "ide-tape: (IO,CoD) != (0,1) while issuing " |
| 1242 | "a packet command\n"); |
| 1243 | return ide_do_reset(drive); |
| 1244 | } |
| 1245 | /* Set the interrupt routine */ |
| 1246 | ide_set_handler(drive, &idetape_pc_intr, IDETAPE_WAIT_CMD, NULL); |
| 1247 | #ifdef CONFIG_BLK_DEV_IDEDMA |
| 1248 | /* Begin DMA, if necessary */ |
| 1249 | if (test_bit(PC_DMA_IN_PROGRESS, &pc->flags)) |
| 1250 | hwif->dma_start(drive); |
| 1251 | #endif |
| 1252 | /* Send the actual packet */ |
| 1253 | HWIF(drive)->atapi_output_bytes(drive, pc->c, 12); |
| 1254 | return ide_started; |
| 1255 | } |
| 1256 | |
Borislav Petkov | 8d06bfa | 2008-02-06 02:57:53 +0100 | [diff] [blame] | 1257 | static ide_startstop_t idetape_issue_pc(ide_drive_t *drive, idetape_pc_t *pc) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1258 | { |
| 1259 | ide_hwif_t *hwif = drive->hwif; |
| 1260 | idetape_tape_t *tape = drive->driver_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1261 | int dma_ok = 0; |
Bartlomiej Zolnierkiewicz | 790d123 | 2008-01-25 22:17:12 +0100 | [diff] [blame] | 1262 | u16 bcount; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1263 | |
Borislav Petkov | 90699ce | 2008-02-02 19:56:50 +0100 | [diff] [blame] | 1264 | if (tape->pc->c[0] == REQUEST_SENSE && |
| 1265 | pc->c[0] == REQUEST_SENSE) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1266 | printk(KERN_ERR "ide-tape: possible ide-tape.c bug - " |
| 1267 | "Two request sense in serial were issued\n"); |
| 1268 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1269 | |
Borislav Petkov | 90699ce | 2008-02-02 19:56:50 +0100 | [diff] [blame] | 1270 | if (tape->failed_pc == NULL && pc->c[0] != REQUEST_SENSE) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1271 | tape->failed_pc = pc; |
| 1272 | /* Set the current packet command */ |
| 1273 | tape->pc = pc; |
| 1274 | |
| 1275 | if (pc->retries > IDETAPE_MAX_PC_RETRIES || |
| 1276 | test_bit(PC_ABORT, &pc->flags)) { |
| 1277 | /* |
Borislav Petkov | 3c98bf3 | 2008-02-06 02:57:53 +0100 | [diff] [blame] | 1278 | * We will "abort" retrying a packet command in case legitimate |
| 1279 | * error code was received (crossing a filemark, or end of the |
| 1280 | * media, for example). |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1281 | */ |
| 1282 | if (!test_bit(PC_ABORT, &pc->flags)) { |
Borislav Petkov | 90699ce | 2008-02-02 19:56:50 +0100 | [diff] [blame] | 1283 | if (!(pc->c[0] == TEST_UNIT_READY && |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1284 | tape->sense_key == 2 && tape->asc == 4 && |
| 1285 | (tape->ascq == 1 || tape->ascq == 8))) { |
| 1286 | printk(KERN_ERR "ide-tape: %s: I/O error, " |
| 1287 | "pc = %2x, key = %2x, " |
| 1288 | "asc = %2x, ascq = %2x\n", |
| 1289 | tape->name, pc->c[0], |
| 1290 | tape->sense_key, tape->asc, |
| 1291 | tape->ascq); |
| 1292 | } |
| 1293 | /* Giving up */ |
| 1294 | pc->error = IDETAPE_ERROR_GENERAL; |
| 1295 | } |
| 1296 | tape->failed_pc = NULL; |
| 1297 | return pc->callback(drive); |
| 1298 | } |
Borislav Petkov | 8004a8c | 2008-02-06 02:57:51 +0100 | [diff] [blame] | 1299 | 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] | 1300 | |
| 1301 | pc->retries++; |
| 1302 | /* We haven't transferred any data yet */ |
| 1303 | pc->actually_transferred = 0; |
| 1304 | pc->current_position = pc->buffer; |
| 1305 | /* Request to transfer the entire buffer at once */ |
Bartlomiej Zolnierkiewicz | 790d123 | 2008-01-25 22:17:12 +0100 | [diff] [blame] | 1306 | bcount = pc->request_transfer; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1307 | |
| 1308 | if (test_and_clear_bit(PC_DMA_ERROR, &pc->flags)) { |
| 1309 | printk(KERN_WARNING "ide-tape: DMA disabled, " |
| 1310 | "reverting to PIO\n"); |
Bartlomiej Zolnierkiewicz | 7469aaf | 2007-02-17 02:40:26 +0100 | [diff] [blame] | 1311 | ide_dma_off(drive); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1312 | } |
| 1313 | if (test_bit(PC_DMA_RECOMMENDED, &pc->flags) && drive->using_dma) |
| 1314 | dma_ok = !hwif->dma_setup(drive); |
| 1315 | |
Bartlomiej Zolnierkiewicz | 2fc5738 | 2008-01-25 22:17:13 +0100 | [diff] [blame] | 1316 | ide_pktcmd_tf_load(drive, IDE_TFLAG_NO_SELECT_MASK | |
| 1317 | IDE_TFLAG_OUT_DEVICE, bcount, dma_ok); |
| 1318 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1319 | if (dma_ok) /* Will begin DMA later */ |
| 1320 | set_bit(PC_DMA_IN_PROGRESS, &pc->flags); |
| 1321 | if (test_bit(IDETAPE_DRQ_INTERRUPT, &tape->flags)) { |
Bartlomiej Zolnierkiewicz | c1c9dbc | 2008-02-02 19:56:44 +0100 | [diff] [blame] | 1322 | ide_execute_command(drive, WIN_PACKETCMD, &idetape_transfer_pc, |
| 1323 | IDETAPE_WAIT_CMD, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1324 | return ide_started; |
| 1325 | } else { |
Bartlomiej Zolnierkiewicz | 23579a2 | 2008-04-18 00:46:26 +0200 | [diff] [blame] | 1326 | hwif->OUTB(WIN_PACKETCMD, hwif->io_ports[IDE_COMMAND_OFFSET]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1327 | return idetape_transfer_pc(drive); |
| 1328 | } |
| 1329 | } |
| 1330 | |
Borislav Petkov | 5a04cfa | 2008-02-06 02:57:54 +0100 | [diff] [blame] | 1331 | static ide_startstop_t idetape_pc_callback(ide_drive_t *drive) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1332 | { |
| 1333 | idetape_tape_t *tape = drive->driver_data; |
Borislav Petkov | 8004a8c | 2008-02-06 02:57:51 +0100 | [diff] [blame] | 1334 | |
| 1335 | debug_log(DBG_PROCS, "Enter %s\n", __func__); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1336 | |
| 1337 | idetape_end_request(drive, tape->pc->error ? 0 : 1, 0); |
| 1338 | return ide_stopped; |
| 1339 | } |
| 1340 | |
Borislav Petkov | 3c98bf3 | 2008-02-06 02:57:53 +0100 | [diff] [blame] | 1341 | /* A mode sense command is used to "sense" tape parameters. */ |
Borislav Petkov | 5a04cfa | 2008-02-06 02:57:54 +0100 | [diff] [blame] | 1342 | static void idetape_create_mode_sense_cmd(idetape_pc_t *pc, u8 page_code) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1343 | { |
| 1344 | idetape_init_pc(pc); |
Borislav Petkov | 90699ce | 2008-02-02 19:56:50 +0100 | [diff] [blame] | 1345 | pc->c[0] = MODE_SENSE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1346 | if (page_code != IDETAPE_BLOCK_DESCRIPTOR) |
Borislav Petkov | 3c98bf3 | 2008-02-06 02:57:53 +0100 | [diff] [blame] | 1347 | /* DBD = 1 - Don't return block descriptors */ |
| 1348 | pc->c[1] = 8; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1349 | pc->c[2] = page_code; |
| 1350 | /* |
| 1351 | * Changed pc->c[3] to 0 (255 will at best return unused info). |
| 1352 | * |
| 1353 | * For SCSI this byte is defined as subpage instead of high byte |
| 1354 | * of length and some IDE drives seem to interpret it this way |
| 1355 | * and return an error when 255 is used. |
| 1356 | */ |
| 1357 | pc->c[3] = 0; |
Borislav Petkov | 3c98bf3 | 2008-02-06 02:57:53 +0100 | [diff] [blame] | 1358 | /* We will just discard data in that case */ |
| 1359 | pc->c[4] = 255; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1360 | if (page_code == IDETAPE_BLOCK_DESCRIPTOR) |
| 1361 | pc->request_transfer = 12; |
| 1362 | else if (page_code == IDETAPE_CAPABILITIES_PAGE) |
| 1363 | pc->request_transfer = 24; |
| 1364 | else |
| 1365 | pc->request_transfer = 50; |
| 1366 | pc->callback = &idetape_pc_callback; |
| 1367 | } |
| 1368 | |
Borislav Petkov | 37016ba | 2008-02-06 02:57:52 +0100 | [diff] [blame] | 1369 | static void idetape_calculate_speeds(ide_drive_t *drive) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1370 | { |
| 1371 | idetape_tape_t *tape = drive->driver_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1372 | |
Borislav Petkov | 5a04cfa | 2008-02-06 02:57:54 +0100 | [diff] [blame] | 1373 | if (time_after(jiffies, |
| 1374 | tape->controlled_pipeline_head_time + 120 * HZ)) { |
| 1375 | tape->controlled_previous_pipeline_head = |
| 1376 | tape->controlled_last_pipeline_head; |
| 1377 | tape->controlled_previous_head_time = |
| 1378 | tape->controlled_pipeline_head_time; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1379 | tape->controlled_last_pipeline_head = tape->pipeline_head; |
| 1380 | tape->controlled_pipeline_head_time = jiffies; |
| 1381 | } |
| 1382 | if (time_after(jiffies, tape->controlled_pipeline_head_time + 60 * HZ)) |
Borislav Petkov | 5a04cfa | 2008-02-06 02:57:54 +0100 | [diff] [blame] | 1383 | tape->controlled_pipeline_head_speed = (tape->pipeline_head - |
| 1384 | tape->controlled_last_pipeline_head) * 32 * HZ / |
| 1385 | (jiffies - tape->controlled_pipeline_head_time); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1386 | else if (time_after(jiffies, tape->controlled_previous_head_time)) |
Borislav Petkov | 5a04cfa | 2008-02-06 02:57:54 +0100 | [diff] [blame] | 1387 | tape->controlled_pipeline_head_speed = (tape->pipeline_head - |
| 1388 | tape->controlled_previous_pipeline_head) * 32 * |
| 1389 | HZ / (jiffies - tape->controlled_previous_head_time); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1390 | |
Borislav Petkov | 5a04cfa | 2008-02-06 02:57:54 +0100 | [diff] [blame] | 1391 | if (tape->nr_pending_stages < tape->max_stages/*- 1 */) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1392 | /* -1 for read mode error recovery */ |
Borislav Petkov | 5a04cfa | 2008-02-06 02:57:54 +0100 | [diff] [blame] | 1393 | if (time_after(jiffies, tape->uncontrolled_previous_head_time + |
| 1394 | 10 * HZ)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1395 | tape->uncontrolled_pipeline_head_time = jiffies; |
Borislav Petkov | 5a04cfa | 2008-02-06 02:57:54 +0100 | [diff] [blame] | 1396 | tape->uncontrolled_pipeline_head_speed = |
| 1397 | (tape->pipeline_head - |
| 1398 | tape->uncontrolled_previous_pipeline_head) * |
| 1399 | 32 * HZ / (jiffies - |
| 1400 | tape->uncontrolled_previous_head_time); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1401 | } |
| 1402 | } else { |
| 1403 | tape->uncontrolled_previous_head_time = jiffies; |
| 1404 | tape->uncontrolled_previous_pipeline_head = tape->pipeline_head; |
Borislav Petkov | 5a04cfa | 2008-02-06 02:57:54 +0100 | [diff] [blame] | 1405 | if (time_after(jiffies, tape->uncontrolled_pipeline_head_time + |
| 1406 | 30 * HZ)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1407 | tape->uncontrolled_pipeline_head_time = jiffies; |
Borislav Petkov | 5a04cfa | 2008-02-06 02:57:54 +0100 | [diff] [blame] | 1408 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1409 | } |
Borislav Petkov | 5a04cfa | 2008-02-06 02:57:54 +0100 | [diff] [blame] | 1410 | tape->pipeline_head_speed = max(tape->uncontrolled_pipeline_head_speed, |
| 1411 | tape->controlled_pipeline_head_speed); |
Borislav Petkov | 37016ba | 2008-02-06 02:57:52 +0100 | [diff] [blame] | 1412 | |
| 1413 | if (tape->speed_control == 1) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1414 | if (tape->nr_pending_stages >= tape->max_stages / 2) |
| 1415 | tape->max_insert_speed = tape->pipeline_head_speed + |
Borislav Petkov | 5a04cfa | 2008-02-06 02:57:54 +0100 | [diff] [blame] | 1416 | (1100 - tape->pipeline_head_speed) * 2 * |
| 1417 | (tape->nr_pending_stages - tape->max_stages / 2) |
| 1418 | / tape->max_stages; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1419 | else |
| 1420 | tape->max_insert_speed = 500 + |
Borislav Petkov | 5a04cfa | 2008-02-06 02:57:54 +0100 | [diff] [blame] | 1421 | (tape->pipeline_head_speed - 500) * 2 * |
| 1422 | tape->nr_pending_stages / tape->max_stages; |
Borislav Petkov | 37016ba | 2008-02-06 02:57:52 +0100 | [diff] [blame] | 1423 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1424 | if (tape->nr_pending_stages >= tape->max_stages * 99 / 100) |
| 1425 | tape->max_insert_speed = 5000; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1426 | } else |
| 1427 | tape->max_insert_speed = tape->speed_control; |
Borislav Petkov | 37016ba | 2008-02-06 02:57:52 +0100 | [diff] [blame] | 1428 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1429 | tape->max_insert_speed = max(tape->max_insert_speed, 500); |
| 1430 | } |
| 1431 | |
Borislav Petkov | 5a04cfa | 2008-02-06 02:57:54 +0100 | [diff] [blame] | 1432 | static ide_startstop_t idetape_media_access_finished(ide_drive_t *drive) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1433 | { |
| 1434 | idetape_tape_t *tape = drive->driver_data; |
| 1435 | idetape_pc_t *pc = tape->pc; |
Bartlomiej Zolnierkiewicz | 22c525b | 2008-01-25 22:17:11 +0100 | [diff] [blame] | 1436 | u8 stat; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1437 | |
Bartlomiej Zolnierkiewicz | c47137a | 2008-02-06 02:57:51 +0100 | [diff] [blame] | 1438 | stat = ide_read_status(drive); |
| 1439 | |
Bartlomiej Zolnierkiewicz | 22c525b | 2008-01-25 22:17:11 +0100 | [diff] [blame] | 1440 | if (stat & SEEK_STAT) { |
| 1441 | if (stat & ERR_STAT) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1442 | /* Error detected */ |
Borislav Petkov | 90699ce | 2008-02-02 19:56:50 +0100 | [diff] [blame] | 1443 | if (pc->c[0] != TEST_UNIT_READY) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1444 | printk(KERN_ERR "ide-tape: %s: I/O error, ", |
| 1445 | tape->name); |
| 1446 | /* Retry operation */ |
| 1447 | return idetape_retry_pc(drive); |
| 1448 | } |
| 1449 | pc->error = 0; |
| 1450 | if (tape->failed_pc == pc) |
| 1451 | tape->failed_pc = NULL; |
| 1452 | } else { |
| 1453 | pc->error = IDETAPE_ERROR_GENERAL; |
| 1454 | tape->failed_pc = NULL; |
| 1455 | } |
| 1456 | return pc->callback(drive); |
| 1457 | } |
| 1458 | |
Borislav Petkov | 5a04cfa | 2008-02-06 02:57:54 +0100 | [diff] [blame] | 1459 | static ide_startstop_t idetape_rw_callback(ide_drive_t *drive) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1460 | { |
| 1461 | idetape_tape_t *tape = drive->driver_data; |
| 1462 | struct request *rq = HWGROUP(drive)->rq; |
Borislav Petkov | 54bb207 | 2008-02-06 02:57:52 +0100 | [diff] [blame] | 1463 | int blocks = tape->pc->actually_transferred / tape->blk_size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1464 | |
Borislav Petkov | 54bb207 | 2008-02-06 02:57:52 +0100 | [diff] [blame] | 1465 | tape->avg_size += blocks * tape->blk_size; |
| 1466 | tape->insert_size += blocks * tape->blk_size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1467 | if (tape->insert_size > 1024 * 1024) |
| 1468 | tape->measure_insert_time = 1; |
| 1469 | if (tape->measure_insert_time) { |
| 1470 | tape->measure_insert_time = 0; |
| 1471 | tape->insert_time = jiffies; |
| 1472 | tape->insert_size = 0; |
| 1473 | } |
| 1474 | if (time_after(jiffies, tape->insert_time)) |
Borislav Petkov | 5a04cfa | 2008-02-06 02:57:54 +0100 | [diff] [blame] | 1475 | tape->insert_speed = tape->insert_size / 1024 * HZ / |
| 1476 | (jiffies - tape->insert_time); |
Marcelo Feitoza Parisi | 9bae1ff | 2006-03-28 01:56:46 -0800 | [diff] [blame] | 1477 | if (time_after_eq(jiffies, tape->avg_time + HZ)) { |
Borislav Petkov | 5a04cfa | 2008-02-06 02:57:54 +0100 | [diff] [blame] | 1478 | tape->avg_speed = tape->avg_size * HZ / |
| 1479 | (jiffies - tape->avg_time) / 1024; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1480 | tape->avg_size = 0; |
| 1481 | tape->avg_time = jiffies; |
| 1482 | } |
Borislav Petkov | 8004a8c | 2008-02-06 02:57:51 +0100 | [diff] [blame] | 1483 | debug_log(DBG_PROCS, "Enter %s\n", __func__); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1484 | |
Borislav Petkov | 54bb207 | 2008-02-06 02:57:52 +0100 | [diff] [blame] | 1485 | tape->first_frame += blocks; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1486 | rq->current_nr_sectors -= blocks; |
| 1487 | |
| 1488 | if (!tape->pc->error) |
| 1489 | idetape_end_request(drive, 1, 0); |
| 1490 | else |
| 1491 | idetape_end_request(drive, tape->pc->error, 0); |
| 1492 | return ide_stopped; |
| 1493 | } |
| 1494 | |
Borislav Petkov | 5a04cfa | 2008-02-06 02:57:54 +0100 | [diff] [blame] | 1495 | static void idetape_create_read_cmd(idetape_tape_t *tape, idetape_pc_t *pc, |
| 1496 | unsigned int length, struct idetape_bh *bh) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1497 | { |
| 1498 | idetape_init_pc(pc); |
Borislav Petkov | 90699ce | 2008-02-02 19:56:50 +0100 | [diff] [blame] | 1499 | pc->c[0] = READ_6; |
Borislav Petkov | 860ff5e | 2008-02-02 19:56:50 +0100 | [diff] [blame] | 1500 | put_unaligned(cpu_to_be32(length), (unsigned int *) &pc->c[1]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1501 | pc->c[1] = 1; |
| 1502 | pc->callback = &idetape_rw_callback; |
| 1503 | pc->bh = bh; |
| 1504 | atomic_set(&bh->b_count, 0); |
| 1505 | pc->buffer = NULL; |
Borislav Petkov | 54bb207 | 2008-02-06 02:57:52 +0100 | [diff] [blame] | 1506 | pc->buffer_size = length * tape->blk_size; |
| 1507 | pc->request_transfer = pc->buffer_size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1508 | if (pc->request_transfer == tape->stage_size) |
| 1509 | set_bit(PC_DMA_RECOMMENDED, &pc->flags); |
| 1510 | } |
| 1511 | |
Borislav Petkov | 5a04cfa | 2008-02-06 02:57:54 +0100 | [diff] [blame] | 1512 | static void idetape_create_write_cmd(idetape_tape_t *tape, idetape_pc_t *pc, |
| 1513 | unsigned int length, struct idetape_bh *bh) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1514 | { |
| 1515 | idetape_init_pc(pc); |
Borislav Petkov | 90699ce | 2008-02-02 19:56:50 +0100 | [diff] [blame] | 1516 | pc->c[0] = WRITE_6; |
Borislav Petkov | 860ff5e | 2008-02-02 19:56:50 +0100 | [diff] [blame] | 1517 | put_unaligned(cpu_to_be32(length), (unsigned int *) &pc->c[1]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1518 | pc->c[1] = 1; |
| 1519 | pc->callback = &idetape_rw_callback; |
| 1520 | set_bit(PC_WRITING, &pc->flags); |
| 1521 | pc->bh = bh; |
| 1522 | pc->b_data = bh->b_data; |
| 1523 | pc->b_count = atomic_read(&bh->b_count); |
| 1524 | pc->buffer = NULL; |
Borislav Petkov | 54bb207 | 2008-02-06 02:57:52 +0100 | [diff] [blame] | 1525 | pc->buffer_size = length * tape->blk_size; |
| 1526 | pc->request_transfer = pc->buffer_size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1527 | if (pc->request_transfer == tape->stage_size) |
| 1528 | set_bit(PC_DMA_RECOMMENDED, &pc->flags); |
| 1529 | } |
| 1530 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1531 | static ide_startstop_t idetape_do_request(ide_drive_t *drive, |
| 1532 | struct request *rq, sector_t block) |
| 1533 | { |
| 1534 | idetape_tape_t *tape = drive->driver_data; |
| 1535 | idetape_pc_t *pc = NULL; |
| 1536 | struct request *postponed_rq = tape->postponed_rq; |
Bartlomiej Zolnierkiewicz | 22c525b | 2008-01-25 22:17:11 +0100 | [diff] [blame] | 1537 | u8 stat; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1538 | |
Borislav Petkov | 8004a8c | 2008-02-06 02:57:51 +0100 | [diff] [blame] | 1539 | debug_log(DBG_SENSE, "sector: %ld, nr_sectors: %ld," |
| 1540 | " current_nr_sectors: %d\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1541 | rq->sector, rq->nr_sectors, rq->current_nr_sectors); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1542 | |
Jens Axboe | 4aff5e2 | 2006-08-10 08:44:47 +0200 | [diff] [blame] | 1543 | if (!blk_special_request(rq)) { |
Borislav Petkov | 3c98bf3 | 2008-02-06 02:57:53 +0100 | [diff] [blame] | 1544 | /* We do not support buffer cache originated requests. */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1545 | printk(KERN_NOTICE "ide-tape: %s: Unsupported request in " |
Jens Axboe | 4aff5e2 | 2006-08-10 08:44:47 +0200 | [diff] [blame] | 1546 | "request queue (%d)\n", drive->name, rq->cmd_type); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1547 | ide_end_request(drive, 0, 0); |
| 1548 | return ide_stopped; |
| 1549 | } |
| 1550 | |
Borislav Petkov | 3c98bf3 | 2008-02-06 02:57:53 +0100 | [diff] [blame] | 1551 | /* Retry a failed packet command */ |
Borislav Petkov | 5a04cfa | 2008-02-06 02:57:54 +0100 | [diff] [blame] | 1552 | if (tape->failed_pc && tape->pc->c[0] == REQUEST_SENSE) |
Borislav Petkov | 8d06bfa | 2008-02-06 02:57:53 +0100 | [diff] [blame] | 1553 | return idetape_issue_pc(drive, tape->failed_pc); |
Borislav Petkov | 5a04cfa | 2008-02-06 02:57:54 +0100 | [diff] [blame] | 1554 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1555 | if (postponed_rq != NULL) |
| 1556 | if (rq != postponed_rq) { |
| 1557 | printk(KERN_ERR "ide-tape: ide-tape.c bug - " |
| 1558 | "Two DSC requests were queued\n"); |
| 1559 | idetape_end_request(drive, 0, 0); |
| 1560 | return ide_stopped; |
| 1561 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1562 | |
| 1563 | tape->postponed_rq = NULL; |
| 1564 | |
| 1565 | /* |
| 1566 | * If the tape is still busy, postpone our request and service |
| 1567 | * the other device meanwhile. |
| 1568 | */ |
Bartlomiej Zolnierkiewicz | c47137a | 2008-02-06 02:57:51 +0100 | [diff] [blame] | 1569 | stat = ide_read_status(drive); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1570 | |
| 1571 | if (!drive->dsc_overlap && !(rq->cmd[0] & REQ_IDETAPE_PC2)) |
| 1572 | set_bit(IDETAPE_IGNORE_DSC, &tape->flags); |
| 1573 | |
| 1574 | if (drive->post_reset == 1) { |
| 1575 | set_bit(IDETAPE_IGNORE_DSC, &tape->flags); |
| 1576 | drive->post_reset = 0; |
| 1577 | } |
| 1578 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1579 | if (time_after(jiffies, tape->insert_time)) |
Borislav Petkov | 5a04cfa | 2008-02-06 02:57:54 +0100 | [diff] [blame] | 1580 | tape->insert_speed = tape->insert_size / 1024 * HZ / |
| 1581 | (jiffies - tape->insert_time); |
Borislav Petkov | 37016ba | 2008-02-06 02:57:52 +0100 | [diff] [blame] | 1582 | idetape_calculate_speeds(drive); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1583 | if (!test_and_clear_bit(IDETAPE_IGNORE_DSC, &tape->flags) && |
Bartlomiej Zolnierkiewicz | 22c525b | 2008-01-25 22:17:11 +0100 | [diff] [blame] | 1584 | (stat & SEEK_STAT) == 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1585 | if (postponed_rq == NULL) { |
| 1586 | tape->dsc_polling_start = jiffies; |
Borislav Petkov | 54bb207 | 2008-02-06 02:57:52 +0100 | [diff] [blame] | 1587 | tape->dsc_poll_freq = tape->best_dsc_rw_freq; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1588 | tape->dsc_timeout = jiffies + IDETAPE_DSC_RW_TIMEOUT; |
| 1589 | } else if (time_after(jiffies, tape->dsc_timeout)) { |
| 1590 | printk(KERN_ERR "ide-tape: %s: DSC timeout\n", |
| 1591 | tape->name); |
| 1592 | if (rq->cmd[0] & REQ_IDETAPE_PC2) { |
| 1593 | idetape_media_access_finished(drive); |
| 1594 | return ide_stopped; |
| 1595 | } else { |
| 1596 | return ide_do_reset(drive); |
| 1597 | } |
Borislav Petkov | 5a04cfa | 2008-02-06 02:57:54 +0100 | [diff] [blame] | 1598 | } else if (time_after(jiffies, |
| 1599 | tape->dsc_polling_start + |
| 1600 | IDETAPE_DSC_MA_THRESHOLD)) |
Borislav Petkov | 54bb207 | 2008-02-06 02:57:52 +0100 | [diff] [blame] | 1601 | tape->dsc_poll_freq = IDETAPE_DSC_MA_SLOW; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1602 | idetape_postpone_request(drive); |
| 1603 | return ide_stopped; |
| 1604 | } |
| 1605 | if (rq->cmd[0] & REQ_IDETAPE_READ) { |
| 1606 | tape->buffer_head++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1607 | tape->postpone_cnt = 0; |
| 1608 | pc = idetape_next_pc_storage(drive); |
Borislav Petkov | 5a04cfa | 2008-02-06 02:57:54 +0100 | [diff] [blame] | 1609 | idetape_create_read_cmd(tape, pc, rq->current_nr_sectors, |
| 1610 | (struct idetape_bh *)rq->special); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1611 | goto out; |
| 1612 | } |
| 1613 | if (rq->cmd[0] & REQ_IDETAPE_WRITE) { |
| 1614 | tape->buffer_head++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1615 | tape->postpone_cnt = 0; |
| 1616 | pc = idetape_next_pc_storage(drive); |
Borislav Petkov | 5a04cfa | 2008-02-06 02:57:54 +0100 | [diff] [blame] | 1617 | idetape_create_write_cmd(tape, pc, rq->current_nr_sectors, |
| 1618 | (struct idetape_bh *)rq->special); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1619 | goto out; |
| 1620 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1621 | if (rq->cmd[0] & REQ_IDETAPE_PC1) { |
| 1622 | pc = (idetape_pc_t *) rq->buffer; |
| 1623 | rq->cmd[0] &= ~(REQ_IDETAPE_PC1); |
| 1624 | rq->cmd[0] |= REQ_IDETAPE_PC2; |
| 1625 | goto out; |
| 1626 | } |
| 1627 | if (rq->cmd[0] & REQ_IDETAPE_PC2) { |
| 1628 | idetape_media_access_finished(drive); |
| 1629 | return ide_stopped; |
| 1630 | } |
| 1631 | BUG(); |
| 1632 | out: |
Borislav Petkov | 8d06bfa | 2008-02-06 02:57:53 +0100 | [diff] [blame] | 1633 | return idetape_issue_pc(drive, pc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1634 | } |
| 1635 | |
Borislav Petkov | 3c98bf3 | 2008-02-06 02:57:53 +0100 | [diff] [blame] | 1636 | /* Pipeline related functions */ |
Borislav Petkov | 5a04cfa | 2008-02-06 02:57:54 +0100 | [diff] [blame] | 1637 | static inline int idetape_pipeline_active(idetape_tape_t *tape) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1638 | { |
| 1639 | int rc1, rc2; |
| 1640 | |
| 1641 | rc1 = test_bit(IDETAPE_PIPELINE_ACTIVE, &tape->flags); |
Borislav Petkov | 54bb207 | 2008-02-06 02:57:52 +0100 | [diff] [blame] | 1642 | rc2 = (tape->active_data_rq != NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1643 | return rc1; |
| 1644 | } |
| 1645 | |
| 1646 | /* |
Borislav Petkov | 3c98bf3 | 2008-02-06 02:57:53 +0100 | [diff] [blame] | 1647 | * The function below uses __get_free_page to allocate a pipeline stage, along |
| 1648 | * with all the necessary small buffers which together make a buffer of size |
| 1649 | * tape->stage_size (or a bit more). We attempt to combine sequential pages as |
| 1650 | * much as possible. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1651 | * |
Borislav Petkov | 3c98bf3 | 2008-02-06 02:57:53 +0100 | [diff] [blame] | 1652 | * It returns a pointer to the new allocated stage, or NULL if we can't (or |
| 1653 | * don't want to) allocate a stage. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1654 | * |
Borislav Petkov | 3c98bf3 | 2008-02-06 02:57:53 +0100 | [diff] [blame] | 1655 | * Pipeline stages are optional and are used to increase performance. If we |
| 1656 | * can't allocate them, we'll manage without them. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1657 | */ |
Borislav Petkov | 5a04cfa | 2008-02-06 02:57:54 +0100 | [diff] [blame] | 1658 | static idetape_stage_t *__idetape_kmalloc_stage(idetape_tape_t *tape, int full, |
| 1659 | int clear) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1660 | { |
| 1661 | idetape_stage_t *stage; |
| 1662 | struct idetape_bh *prev_bh, *bh; |
| 1663 | int pages = tape->pages_per_stage; |
| 1664 | char *b_data = NULL; |
| 1665 | |
Borislav Petkov | 5a04cfa | 2008-02-06 02:57:54 +0100 | [diff] [blame] | 1666 | stage = kmalloc(sizeof(idetape_stage_t), GFP_KERNEL); |
| 1667 | if (!stage) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1668 | return NULL; |
| 1669 | stage->next = NULL; |
| 1670 | |
Borislav Petkov | 5a04cfa | 2008-02-06 02:57:54 +0100 | [diff] [blame] | 1671 | stage->bh = kmalloc(sizeof(struct idetape_bh), GFP_KERNEL); |
| 1672 | bh = stage->bh; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1673 | if (bh == NULL) |
| 1674 | goto abort; |
| 1675 | bh->b_reqnext = NULL; |
Borislav Petkov | 5a04cfa | 2008-02-06 02:57:54 +0100 | [diff] [blame] | 1676 | bh->b_data = (char *) __get_free_page(GFP_KERNEL); |
| 1677 | if (!bh->b_data) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1678 | goto abort; |
| 1679 | if (clear) |
| 1680 | memset(bh->b_data, 0, PAGE_SIZE); |
| 1681 | bh->b_size = PAGE_SIZE; |
| 1682 | atomic_set(&bh->b_count, full ? bh->b_size : 0); |
| 1683 | |
| 1684 | while (--pages) { |
Borislav Petkov | 5a04cfa | 2008-02-06 02:57:54 +0100 | [diff] [blame] | 1685 | b_data = (char *) __get_free_page(GFP_KERNEL); |
| 1686 | if (!b_data) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1687 | goto abort; |
| 1688 | if (clear) |
| 1689 | memset(b_data, 0, PAGE_SIZE); |
| 1690 | if (bh->b_data == b_data + PAGE_SIZE) { |
| 1691 | bh->b_size += PAGE_SIZE; |
| 1692 | bh->b_data -= PAGE_SIZE; |
| 1693 | if (full) |
| 1694 | atomic_add(PAGE_SIZE, &bh->b_count); |
| 1695 | continue; |
| 1696 | } |
| 1697 | if (b_data == bh->b_data + bh->b_size) { |
| 1698 | bh->b_size += PAGE_SIZE; |
| 1699 | if (full) |
| 1700 | atomic_add(PAGE_SIZE, &bh->b_count); |
| 1701 | continue; |
| 1702 | } |
| 1703 | prev_bh = bh; |
Borislav Petkov | 5a04cfa | 2008-02-06 02:57:54 +0100 | [diff] [blame] | 1704 | bh = kmalloc(sizeof(struct idetape_bh), GFP_KERNEL); |
| 1705 | if (!bh) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1706 | free_page((unsigned long) b_data); |
| 1707 | goto abort; |
| 1708 | } |
| 1709 | bh->b_reqnext = NULL; |
| 1710 | bh->b_data = b_data; |
| 1711 | bh->b_size = PAGE_SIZE; |
| 1712 | atomic_set(&bh->b_count, full ? bh->b_size : 0); |
| 1713 | prev_bh->b_reqnext = bh; |
| 1714 | } |
| 1715 | bh->b_size -= tape->excess_bh_size; |
| 1716 | if (full) |
| 1717 | atomic_sub(tape->excess_bh_size, &bh->b_count); |
| 1718 | return stage; |
| 1719 | abort: |
| 1720 | __idetape_kfree_stage(stage); |
| 1721 | return NULL; |
| 1722 | } |
| 1723 | |
Borislav Petkov | 5a04cfa | 2008-02-06 02:57:54 +0100 | [diff] [blame] | 1724 | static idetape_stage_t *idetape_kmalloc_stage(idetape_tape_t *tape) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1725 | { |
| 1726 | idetape_stage_t *cache_stage = tape->cache_stage; |
| 1727 | |
Borislav Petkov | 8004a8c | 2008-02-06 02:57:51 +0100 | [diff] [blame] | 1728 | debug_log(DBG_PROCS, "Enter %s\n", __func__); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1729 | |
| 1730 | if (tape->nr_stages >= tape->max_stages) |
| 1731 | return NULL; |
| 1732 | if (cache_stage != NULL) { |
| 1733 | tape->cache_stage = NULL; |
| 1734 | return cache_stage; |
| 1735 | } |
| 1736 | return __idetape_kmalloc_stage(tape, 0, 0); |
| 1737 | } |
| 1738 | |
Borislav Petkov | 5a04cfa | 2008-02-06 02:57:54 +0100 | [diff] [blame] | 1739 | static int idetape_copy_stage_from_user(idetape_tape_t *tape, |
| 1740 | idetape_stage_t *stage, const char __user *buf, int n) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1741 | { |
| 1742 | struct idetape_bh *bh = tape->bh; |
| 1743 | int count; |
Daniel Walker | dcd9637 | 2006-06-25 05:47:37 -0700 | [diff] [blame] | 1744 | int ret = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1745 | |
| 1746 | while (n) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1747 | if (bh == NULL) { |
Borislav Petkov | 5a04cfa | 2008-02-06 02:57:54 +0100 | [diff] [blame] | 1748 | printk(KERN_ERR "ide-tape: bh == NULL in %s\n", |
| 1749 | __func__); |
Daniel Walker | dcd9637 | 2006-06-25 05:47:37 -0700 | [diff] [blame] | 1750 | return 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1751 | } |
Borislav Petkov | 5a04cfa | 2008-02-06 02:57:54 +0100 | [diff] [blame] | 1752 | count = min((unsigned int) |
| 1753 | (bh->b_size - atomic_read(&bh->b_count)), |
| 1754 | (unsigned int)n); |
| 1755 | if (copy_from_user(bh->b_data + atomic_read(&bh->b_count), buf, |
| 1756 | count)) |
Daniel Walker | dcd9637 | 2006-06-25 05:47:37 -0700 | [diff] [blame] | 1757 | ret = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1758 | n -= count; |
| 1759 | atomic_add(count, &bh->b_count); |
| 1760 | buf += count; |
| 1761 | if (atomic_read(&bh->b_count) == bh->b_size) { |
| 1762 | bh = bh->b_reqnext; |
| 1763 | if (bh) |
| 1764 | atomic_set(&bh->b_count, 0); |
| 1765 | } |
| 1766 | } |
| 1767 | tape->bh = bh; |
Daniel Walker | dcd9637 | 2006-06-25 05:47:37 -0700 | [diff] [blame] | 1768 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1769 | } |
| 1770 | |
Borislav Petkov | 5a04cfa | 2008-02-06 02:57:54 +0100 | [diff] [blame] | 1771 | static int idetape_copy_stage_to_user(idetape_tape_t *tape, char __user *buf, |
| 1772 | idetape_stage_t *stage, int n) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1773 | { |
| 1774 | struct idetape_bh *bh = tape->bh; |
| 1775 | int count; |
Daniel Walker | dcd9637 | 2006-06-25 05:47:37 -0700 | [diff] [blame] | 1776 | int ret = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1777 | |
| 1778 | while (n) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1779 | if (bh == NULL) { |
Borislav Petkov | 5a04cfa | 2008-02-06 02:57:54 +0100 | [diff] [blame] | 1780 | printk(KERN_ERR "ide-tape: bh == NULL in %s\n", |
| 1781 | __func__); |
Daniel Walker | dcd9637 | 2006-06-25 05:47:37 -0700 | [diff] [blame] | 1782 | return 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1783 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1784 | count = min(tape->b_count, n); |
Daniel Walker | dcd9637 | 2006-06-25 05:47:37 -0700 | [diff] [blame] | 1785 | if (copy_to_user(buf, tape->b_data, count)) |
| 1786 | ret = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1787 | n -= count; |
| 1788 | tape->b_data += count; |
| 1789 | tape->b_count -= count; |
| 1790 | buf += count; |
| 1791 | if (!tape->b_count) { |
Borislav Petkov | 5a04cfa | 2008-02-06 02:57:54 +0100 | [diff] [blame] | 1792 | bh = bh->b_reqnext; |
| 1793 | tape->bh = bh; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1794 | if (bh) { |
| 1795 | tape->b_data = bh->b_data; |
| 1796 | tape->b_count = atomic_read(&bh->b_count); |
| 1797 | } |
| 1798 | } |
| 1799 | } |
Daniel Walker | dcd9637 | 2006-06-25 05:47:37 -0700 | [diff] [blame] | 1800 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1801 | } |
| 1802 | |
Borislav Petkov | 5a04cfa | 2008-02-06 02:57:54 +0100 | [diff] [blame] | 1803 | static void idetape_init_merge_stage(idetape_tape_t *tape) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1804 | { |
| 1805 | struct idetape_bh *bh = tape->merge_stage->bh; |
Borislav Petkov | 5a04cfa | 2008-02-06 02:57:54 +0100 | [diff] [blame] | 1806 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1807 | tape->bh = bh; |
Borislav Petkov | 54abf37 | 2008-02-06 02:57:52 +0100 | [diff] [blame] | 1808 | if (tape->chrdev_dir == IDETAPE_DIR_WRITE) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1809 | atomic_set(&bh->b_count, 0); |
| 1810 | else { |
| 1811 | tape->b_data = bh->b_data; |
| 1812 | tape->b_count = atomic_read(&bh->b_count); |
| 1813 | } |
| 1814 | } |
| 1815 | |
Borislav Petkov | 5a04cfa | 2008-02-06 02:57:54 +0100 | [diff] [blame] | 1816 | static void idetape_switch_buffers(idetape_tape_t *tape, idetape_stage_t *stage) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1817 | { |
| 1818 | struct idetape_bh *tmp; |
| 1819 | |
| 1820 | tmp = stage->bh; |
| 1821 | stage->bh = tape->merge_stage->bh; |
| 1822 | tape->merge_stage->bh = tmp; |
| 1823 | idetape_init_merge_stage(tape); |
| 1824 | } |
| 1825 | |
Borislav Petkov | 3c98bf3 | 2008-02-06 02:57:53 +0100 | [diff] [blame] | 1826 | /* Add a new stage at the end of the pipeline. */ |
Borislav Petkov | 5a04cfa | 2008-02-06 02:57:54 +0100 | [diff] [blame] | 1827 | static void idetape_add_stage_tail(ide_drive_t *drive, idetape_stage_t *stage) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1828 | { |
| 1829 | idetape_tape_t *tape = drive->driver_data; |
| 1830 | unsigned long flags; |
Borislav Petkov | 8004a8c | 2008-02-06 02:57:51 +0100 | [diff] [blame] | 1831 | |
| 1832 | debug_log(DBG_PROCS, "Enter %s\n", __func__); |
| 1833 | |
Borislav Petkov | 54bb207 | 2008-02-06 02:57:52 +0100 | [diff] [blame] | 1834 | spin_lock_irqsave(&tape->lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1835 | stage->next = NULL; |
| 1836 | if (tape->last_stage != NULL) |
Borislav Petkov | 5a04cfa | 2008-02-06 02:57:54 +0100 | [diff] [blame] | 1837 | tape->last_stage->next = stage; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1838 | else |
Borislav Petkov | 5a04cfa | 2008-02-06 02:57:54 +0100 | [diff] [blame] | 1839 | tape->first_stage = stage; |
| 1840 | tape->next_stage = stage; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1841 | tape->last_stage = stage; |
| 1842 | if (tape->next_stage == NULL) |
| 1843 | tape->next_stage = tape->last_stage; |
| 1844 | tape->nr_stages++; |
| 1845 | tape->nr_pending_stages++; |
Borislav Petkov | 54bb207 | 2008-02-06 02:57:52 +0100 | [diff] [blame] | 1846 | spin_unlock_irqrestore(&tape->lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1847 | } |
| 1848 | |
Borislav Petkov | 3c98bf3 | 2008-02-06 02:57:53 +0100 | [diff] [blame] | 1849 | /* Install a completion in a pending request and sleep until it is serviced. The |
| 1850 | * caller should ensure that the request will not be serviced before we install |
| 1851 | * the completion (usually by disabling interrupts). |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1852 | */ |
Borislav Petkov | 5a04cfa | 2008-02-06 02:57:54 +0100 | [diff] [blame] | 1853 | static void idetape_wait_for_request(ide_drive_t *drive, struct request *rq) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1854 | { |
Peter Zijlstra | 6e9a473 | 2006-09-30 23:28:10 -0700 | [diff] [blame] | 1855 | DECLARE_COMPLETION_ONSTACK(wait); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1856 | idetape_tape_t *tape = drive->driver_data; |
| 1857 | |
Jens Axboe | 4aff5e2 | 2006-08-10 08:44:47 +0200 | [diff] [blame] | 1858 | if (rq == NULL || !blk_special_request(rq)) { |
Borislav Petkov | 5a04cfa | 2008-02-06 02:57:54 +0100 | [diff] [blame] | 1859 | printk(KERN_ERR "ide-tape: bug: Trying to sleep on non-valid" |
| 1860 | " request\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1861 | return; |
| 1862 | } |
Jens Axboe | c00895a | 2006-09-30 20:29:12 +0200 | [diff] [blame] | 1863 | rq->end_io_data = &wait; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1864 | rq->end_io = blk_end_sync_rq; |
Borislav Petkov | 54bb207 | 2008-02-06 02:57:52 +0100 | [diff] [blame] | 1865 | spin_unlock_irq(&tape->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1866 | wait_for_completion(&wait); |
| 1867 | /* The stage and its struct request have been deallocated */ |
Borislav Petkov | 54bb207 | 2008-02-06 02:57:52 +0100 | [diff] [blame] | 1868 | spin_lock_irq(&tape->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1869 | } |
| 1870 | |
Borislav Petkov | a2f5b7f | 2008-02-06 02:57:51 +0100 | [diff] [blame] | 1871 | static ide_startstop_t idetape_read_position_callback(ide_drive_t *drive) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1872 | { |
| 1873 | idetape_tape_t *tape = drive->driver_data; |
Borislav Petkov | a2f5b7f | 2008-02-06 02:57:51 +0100 | [diff] [blame] | 1874 | u8 *readpos = tape->pc->buffer; |
Borislav Petkov | 8004a8c | 2008-02-06 02:57:51 +0100 | [diff] [blame] | 1875 | |
| 1876 | debug_log(DBG_PROCS, "Enter %s\n", __func__); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1877 | |
| 1878 | if (!tape->pc->error) { |
Borislav Petkov | a2f5b7f | 2008-02-06 02:57:51 +0100 | [diff] [blame] | 1879 | debug_log(DBG_SENSE, "BOP - %s\n", |
| 1880 | (readpos[0] & 0x80) ? "Yes" : "No"); |
| 1881 | debug_log(DBG_SENSE, "EOP - %s\n", |
| 1882 | (readpos[0] & 0x40) ? "Yes" : "No"); |
Borislav Petkov | 8004a8c | 2008-02-06 02:57:51 +0100 | [diff] [blame] | 1883 | |
Borislav Petkov | a2f5b7f | 2008-02-06 02:57:51 +0100 | [diff] [blame] | 1884 | if (readpos[0] & 0x4) { |
| 1885 | printk(KERN_INFO "ide-tape: Block location is unknown" |
| 1886 | "to the tape\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1887 | clear_bit(IDETAPE_ADDRESS_VALID, &tape->flags); |
| 1888 | idetape_end_request(drive, 0, 0); |
| 1889 | } else { |
Borislav Petkov | 8004a8c | 2008-02-06 02:57:51 +0100 | [diff] [blame] | 1890 | debug_log(DBG_SENSE, "Block Location - %u\n", |
Borislav Petkov | a2f5b7f | 2008-02-06 02:57:51 +0100 | [diff] [blame] | 1891 | be32_to_cpu(*(u32 *)&readpos[4])); |
Borislav Petkov | 8004a8c | 2008-02-06 02:57:51 +0100 | [diff] [blame] | 1892 | |
Borislav Petkov | a2f5b7f | 2008-02-06 02:57:51 +0100 | [diff] [blame] | 1893 | tape->partition = readpos[1]; |
Borislav Petkov | 54bb207 | 2008-02-06 02:57:52 +0100 | [diff] [blame] | 1894 | tape->first_frame = |
Borislav Petkov | a2f5b7f | 2008-02-06 02:57:51 +0100 | [diff] [blame] | 1895 | be32_to_cpu(*(u32 *)&readpos[4]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1896 | set_bit(IDETAPE_ADDRESS_VALID, &tape->flags); |
| 1897 | idetape_end_request(drive, 1, 0); |
| 1898 | } |
| 1899 | } else { |
| 1900 | idetape_end_request(drive, 0, 0); |
| 1901 | } |
| 1902 | return ide_stopped; |
| 1903 | } |
| 1904 | |
| 1905 | /* |
Borislav Petkov | 3c98bf3 | 2008-02-06 02:57:53 +0100 | [diff] [blame] | 1906 | * Write a filemark if write_filemark=1. Flush the device buffers without |
| 1907 | * writing a filemark otherwise. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1908 | */ |
Borislav Petkov | 5a04cfa | 2008-02-06 02:57:54 +0100 | [diff] [blame] | 1909 | static void idetape_create_write_filemark_cmd(ide_drive_t *drive, |
| 1910 | idetape_pc_t *pc, int write_filemark) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1911 | { |
| 1912 | idetape_init_pc(pc); |
Borislav Petkov | 90699ce | 2008-02-02 19:56:50 +0100 | [diff] [blame] | 1913 | pc->c[0] = WRITE_FILEMARKS; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1914 | pc->c[4] = write_filemark; |
| 1915 | set_bit(PC_WAIT_FOR_DSC, &pc->flags); |
| 1916 | pc->callback = &idetape_pc_callback; |
| 1917 | } |
| 1918 | |
| 1919 | static void idetape_create_test_unit_ready_cmd(idetape_pc_t *pc) |
| 1920 | { |
| 1921 | idetape_init_pc(pc); |
Borislav Petkov | 90699ce | 2008-02-02 19:56:50 +0100 | [diff] [blame] | 1922 | pc->c[0] = TEST_UNIT_READY; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1923 | pc->callback = &idetape_pc_callback; |
| 1924 | } |
| 1925 | |
| 1926 | /* |
Borislav Petkov | 3c98bf3 | 2008-02-06 02:57:53 +0100 | [diff] [blame] | 1927 | * We add a special packet command request to the tail of the request queue, and |
| 1928 | * wait for it to be serviced. This is not to be called from within the request |
| 1929 | * handling part of the driver! We allocate here data on the stack and it is |
| 1930 | * valid until the request is finished. This is not the case for the bottom part |
| 1931 | * of the driver, where we are always leaving the functions to wait for an |
| 1932 | * interrupt or a timer event. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1933 | * |
Borislav Petkov | 3c98bf3 | 2008-02-06 02:57:53 +0100 | [diff] [blame] | 1934 | * From the bottom part of the driver, we should allocate safe memory using |
| 1935 | * idetape_next_pc_storage() and ide_tape_next_rq_storage(), and add the request |
| 1936 | * to the request list without waiting for it to be serviced! In that case, we |
| 1937 | * usually use idetape_queue_pc_head(). |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1938 | */ |
Borislav Petkov | 5a04cfa | 2008-02-06 02:57:54 +0100 | [diff] [blame] | 1939 | static int __idetape_queue_pc_tail(ide_drive_t *drive, idetape_pc_t *pc) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1940 | { |
| 1941 | struct ide_tape_obj *tape = drive->driver_data; |
| 1942 | struct request rq; |
| 1943 | |
| 1944 | idetape_init_rq(&rq, REQ_IDETAPE_PC1); |
| 1945 | rq.buffer = (char *) pc; |
| 1946 | rq.rq_disk = tape->disk; |
| 1947 | return ide_do_drive_cmd(drive, &rq, ide_wait); |
| 1948 | } |
| 1949 | |
Borislav Petkov | 5a04cfa | 2008-02-06 02:57:54 +0100 | [diff] [blame] | 1950 | static void idetape_create_load_unload_cmd(ide_drive_t *drive, idetape_pc_t *pc, |
| 1951 | int cmd) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1952 | { |
| 1953 | idetape_init_pc(pc); |
Borislav Petkov | 90699ce | 2008-02-02 19:56:50 +0100 | [diff] [blame] | 1954 | pc->c[0] = START_STOP; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1955 | pc->c[4] = cmd; |
| 1956 | set_bit(PC_WAIT_FOR_DSC, &pc->flags); |
| 1957 | pc->callback = &idetape_pc_callback; |
| 1958 | } |
| 1959 | |
| 1960 | static int idetape_wait_ready(ide_drive_t *drive, unsigned long timeout) |
| 1961 | { |
| 1962 | idetape_tape_t *tape = drive->driver_data; |
| 1963 | idetape_pc_t pc; |
| 1964 | int load_attempted = 0; |
| 1965 | |
Borislav Petkov | 3c98bf3 | 2008-02-06 02:57:53 +0100 | [diff] [blame] | 1966 | /* Wait for the tape to become ready */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1967 | set_bit(IDETAPE_MEDIUM_PRESENT, &tape->flags); |
| 1968 | timeout += jiffies; |
| 1969 | while (time_before(jiffies, timeout)) { |
| 1970 | idetape_create_test_unit_ready_cmd(&pc); |
| 1971 | if (!__idetape_queue_pc_tail(drive, &pc)) |
| 1972 | return 0; |
| 1973 | if ((tape->sense_key == 2 && tape->asc == 4 && tape->ascq == 2) |
Borislav Petkov | 3c98bf3 | 2008-02-06 02:57:53 +0100 | [diff] [blame] | 1974 | || (tape->asc == 0x3A)) { |
| 1975 | /* no media */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1976 | if (load_attempted) |
| 1977 | return -ENOMEDIUM; |
Borislav Petkov | 5a04cfa | 2008-02-06 02:57:54 +0100 | [diff] [blame] | 1978 | idetape_create_load_unload_cmd(drive, &pc, |
| 1979 | IDETAPE_LU_LOAD_MASK); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1980 | __idetape_queue_pc_tail(drive, &pc); |
| 1981 | load_attempted = 1; |
| 1982 | /* not about to be ready */ |
| 1983 | } else if (!(tape->sense_key == 2 && tape->asc == 4 && |
| 1984 | (tape->ascq == 1 || tape->ascq == 8))) |
| 1985 | return -EIO; |
Nishanth Aravamudan | 80ce45f | 2005-09-10 00:27:08 -0700 | [diff] [blame] | 1986 | msleep(100); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1987 | } |
| 1988 | return -EIO; |
| 1989 | } |
| 1990 | |
Borislav Petkov | 5a04cfa | 2008-02-06 02:57:54 +0100 | [diff] [blame] | 1991 | static int idetape_queue_pc_tail(ide_drive_t *drive, idetape_pc_t *pc) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1992 | { |
| 1993 | return __idetape_queue_pc_tail(drive, pc); |
| 1994 | } |
| 1995 | |
Borislav Petkov | 5a04cfa | 2008-02-06 02:57:54 +0100 | [diff] [blame] | 1996 | static int idetape_flush_tape_buffers(ide_drive_t *drive) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1997 | { |
| 1998 | idetape_pc_t pc; |
| 1999 | int rc; |
| 2000 | |
| 2001 | idetape_create_write_filemark_cmd(drive, &pc, 0); |
Borislav Petkov | 5a04cfa | 2008-02-06 02:57:54 +0100 | [diff] [blame] | 2002 | rc = idetape_queue_pc_tail(drive, &pc); |
| 2003 | if (rc) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2004 | return rc; |
| 2005 | idetape_wait_ready(drive, 60 * 5 * HZ); |
| 2006 | return 0; |
| 2007 | } |
| 2008 | |
Borislav Petkov | 5a04cfa | 2008-02-06 02:57:54 +0100 | [diff] [blame] | 2009 | static void idetape_create_read_position_cmd(idetape_pc_t *pc) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2010 | { |
| 2011 | idetape_init_pc(pc); |
Borislav Petkov | 90699ce | 2008-02-02 19:56:50 +0100 | [diff] [blame] | 2012 | pc->c[0] = READ_POSITION; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2013 | pc->request_transfer = 20; |
| 2014 | pc->callback = &idetape_read_position_callback; |
| 2015 | } |
| 2016 | |
Borislav Petkov | 5a04cfa | 2008-02-06 02:57:54 +0100 | [diff] [blame] | 2017 | static int idetape_read_position(ide_drive_t *drive) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2018 | { |
| 2019 | idetape_tape_t *tape = drive->driver_data; |
| 2020 | idetape_pc_t pc; |
| 2021 | int position; |
| 2022 | |
Borislav Petkov | 8004a8c | 2008-02-06 02:57:51 +0100 | [diff] [blame] | 2023 | debug_log(DBG_PROCS, "Enter %s\n", __func__); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2024 | |
| 2025 | idetape_create_read_position_cmd(&pc); |
| 2026 | if (idetape_queue_pc_tail(drive, &pc)) |
| 2027 | return -1; |
Borislav Petkov | 54bb207 | 2008-02-06 02:57:52 +0100 | [diff] [blame] | 2028 | position = tape->first_frame; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2029 | return position; |
| 2030 | } |
| 2031 | |
Borislav Petkov | 5a04cfa | 2008-02-06 02:57:54 +0100 | [diff] [blame] | 2032 | static void idetape_create_locate_cmd(ide_drive_t *drive, idetape_pc_t *pc, |
| 2033 | unsigned int block, u8 partition, int skip) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2034 | { |
| 2035 | idetape_init_pc(pc); |
Borislav Petkov | 90699ce | 2008-02-02 19:56:50 +0100 | [diff] [blame] | 2036 | pc->c[0] = POSITION_TO_ELEMENT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2037 | pc->c[1] = 2; |
Borislav Petkov | 860ff5e | 2008-02-02 19:56:50 +0100 | [diff] [blame] | 2038 | put_unaligned(cpu_to_be32(block), (unsigned int *) &pc->c[3]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2039 | pc->c[8] = partition; |
| 2040 | set_bit(PC_WAIT_FOR_DSC, &pc->flags); |
| 2041 | pc->callback = &idetape_pc_callback; |
| 2042 | } |
| 2043 | |
Borislav Petkov | 5a04cfa | 2008-02-06 02:57:54 +0100 | [diff] [blame] | 2044 | static int idetape_create_prevent_cmd(ide_drive_t *drive, idetape_pc_t *pc, |
| 2045 | int prevent) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2046 | { |
| 2047 | idetape_tape_t *tape = drive->driver_data; |
| 2048 | |
Borislav Petkov | b642201 | 2008-02-02 19:56:49 +0100 | [diff] [blame] | 2049 | /* device supports locking according to capabilities page */ |
| 2050 | if (!(tape->caps[6] & 0x01)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2051 | return 0; |
| 2052 | |
| 2053 | idetape_init_pc(pc); |
Borislav Petkov | 90699ce | 2008-02-02 19:56:50 +0100 | [diff] [blame] | 2054 | pc->c[0] = ALLOW_MEDIUM_REMOVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2055 | pc->c[4] = prevent; |
| 2056 | pc->callback = &idetape_pc_callback; |
| 2057 | return 1; |
| 2058 | } |
| 2059 | |
Borislav Petkov | 5a04cfa | 2008-02-06 02:57:54 +0100 | [diff] [blame] | 2060 | static int __idetape_discard_read_pipeline(ide_drive_t *drive) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2061 | { |
| 2062 | idetape_tape_t *tape = drive->driver_data; |
| 2063 | unsigned long flags; |
| 2064 | int cnt; |
| 2065 | |
Borislav Petkov | 54abf37 | 2008-02-06 02:57:52 +0100 | [diff] [blame] | 2066 | if (tape->chrdev_dir != IDETAPE_DIR_READ) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2067 | return 0; |
| 2068 | |
| 2069 | /* Remove merge stage. */ |
Borislav Petkov | 54bb207 | 2008-02-06 02:57:52 +0100 | [diff] [blame] | 2070 | cnt = tape->merge_stage_size / tape->blk_size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2071 | if (test_and_clear_bit(IDETAPE_FILEMARK, &tape->flags)) |
| 2072 | ++cnt; /* Filemarks count as 1 sector */ |
| 2073 | tape->merge_stage_size = 0; |
| 2074 | if (tape->merge_stage != NULL) { |
| 2075 | __idetape_kfree_stage(tape->merge_stage); |
| 2076 | tape->merge_stage = NULL; |
| 2077 | } |
| 2078 | |
| 2079 | /* Clear pipeline flags. */ |
| 2080 | clear_bit(IDETAPE_PIPELINE_ERROR, &tape->flags); |
Borislav Petkov | 54abf37 | 2008-02-06 02:57:52 +0100 | [diff] [blame] | 2081 | tape->chrdev_dir = IDETAPE_DIR_NONE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2082 | |
| 2083 | /* Remove pipeline stages. */ |
| 2084 | if (tape->first_stage == NULL) |
| 2085 | return 0; |
| 2086 | |
Borislav Petkov | 54bb207 | 2008-02-06 02:57:52 +0100 | [diff] [blame] | 2087 | spin_lock_irqsave(&tape->lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2088 | tape->next_stage = NULL; |
| 2089 | if (idetape_pipeline_active(tape)) |
Borislav Petkov | 54bb207 | 2008-02-06 02:57:52 +0100 | [diff] [blame] | 2090 | idetape_wait_for_request(drive, tape->active_data_rq); |
| 2091 | spin_unlock_irqrestore(&tape->lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2092 | |
| 2093 | while (tape->first_stage != NULL) { |
| 2094 | struct request *rq_ptr = &tape->first_stage->rq; |
| 2095 | |
Borislav Petkov | 5a04cfa | 2008-02-06 02:57:54 +0100 | [diff] [blame] | 2096 | cnt += rq_ptr->nr_sectors - rq_ptr->current_nr_sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2097 | if (rq_ptr->errors == IDETAPE_ERROR_FILEMARK) |
| 2098 | ++cnt; |
| 2099 | idetape_remove_stage_head(drive); |
| 2100 | } |
| 2101 | tape->nr_pending_stages = 0; |
| 2102 | tape->max_stages = tape->min_pipeline; |
| 2103 | return cnt; |
| 2104 | } |
| 2105 | |
| 2106 | /* |
Borislav Petkov | 3c98bf3 | 2008-02-06 02:57:53 +0100 | [diff] [blame] | 2107 | * Position the tape to the requested block using the LOCATE packet command. |
| 2108 | * A READ POSITION command is then issued to check where we are positioned. Like |
| 2109 | * all higher level operations, we queue the commands at the tail of the request |
| 2110 | * queue and wait for their completion. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2111 | */ |
Borislav Petkov | 5a04cfa | 2008-02-06 02:57:54 +0100 | [diff] [blame] | 2112 | static int idetape_position_tape(ide_drive_t *drive, unsigned int block, |
| 2113 | u8 partition, int skip) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2114 | { |
| 2115 | idetape_tape_t *tape = drive->driver_data; |
| 2116 | int retval; |
| 2117 | idetape_pc_t pc; |
| 2118 | |
Borislav Petkov | 54abf37 | 2008-02-06 02:57:52 +0100 | [diff] [blame] | 2119 | if (tape->chrdev_dir == IDETAPE_DIR_READ) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2120 | __idetape_discard_read_pipeline(drive); |
| 2121 | idetape_wait_ready(drive, 60 * 5 * HZ); |
| 2122 | idetape_create_locate_cmd(drive, &pc, block, partition, skip); |
| 2123 | retval = idetape_queue_pc_tail(drive, &pc); |
| 2124 | if (retval) |
| 2125 | return (retval); |
| 2126 | |
| 2127 | idetape_create_read_position_cmd(&pc); |
| 2128 | return (idetape_queue_pc_tail(drive, &pc)); |
| 2129 | } |
| 2130 | |
Borislav Petkov | 5a04cfa | 2008-02-06 02:57:54 +0100 | [diff] [blame] | 2131 | static void idetape_discard_read_pipeline(ide_drive_t *drive, |
| 2132 | int restore_position) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2133 | { |
| 2134 | idetape_tape_t *tape = drive->driver_data; |
| 2135 | int cnt; |
| 2136 | int seek, position; |
| 2137 | |
| 2138 | cnt = __idetape_discard_read_pipeline(drive); |
| 2139 | if (restore_position) { |
| 2140 | position = idetape_read_position(drive); |
| 2141 | seek = position > cnt ? position - cnt : 0; |
| 2142 | if (idetape_position_tape(drive, seek, 0, 0)) { |
Borislav Petkov | 5a04cfa | 2008-02-06 02:57:54 +0100 | [diff] [blame] | 2143 | printk(KERN_INFO "ide-tape: %s: position_tape failed in" |
| 2144 | " discard_pipeline()\n", tape->name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2145 | return; |
| 2146 | } |
| 2147 | } |
| 2148 | } |
| 2149 | |
| 2150 | /* |
Borislav Petkov | 3c98bf3 | 2008-02-06 02:57:53 +0100 | [diff] [blame] | 2151 | * Generate a read/write request for the block device interface and wait for it |
| 2152 | * to be serviced. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2153 | */ |
Borislav Petkov | 5a04cfa | 2008-02-06 02:57:54 +0100 | [diff] [blame] | 2154 | static int idetape_queue_rw_tail(ide_drive_t *drive, int cmd, int blocks, |
| 2155 | struct idetape_bh *bh) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2156 | { |
| 2157 | idetape_tape_t *tape = drive->driver_data; |
| 2158 | struct request rq; |
| 2159 | |
Borislav Petkov | 8004a8c | 2008-02-06 02:57:51 +0100 | [diff] [blame] | 2160 | debug_log(DBG_SENSE, "%s: cmd=%d\n", __func__, cmd); |
| 2161 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2162 | if (idetape_pipeline_active(tape)) { |
Borislav Petkov | 8004a8c | 2008-02-06 02:57:51 +0100 | [diff] [blame] | 2163 | printk(KERN_ERR "ide-tape: bug: the pipeline is active in %s\n", |
| 2164 | __func__); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2165 | return (0); |
| 2166 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2167 | |
| 2168 | idetape_init_rq(&rq, cmd); |
| 2169 | rq.rq_disk = tape->disk; |
| 2170 | rq.special = (void *)bh; |
Borislav Petkov | 54bb207 | 2008-02-06 02:57:52 +0100 | [diff] [blame] | 2171 | rq.sector = tape->first_frame; |
Borislav Petkov | 5a04cfa | 2008-02-06 02:57:54 +0100 | [diff] [blame] | 2172 | rq.nr_sectors = blocks; |
| 2173 | rq.current_nr_sectors = blocks; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2174 | (void) ide_do_drive_cmd(drive, &rq, ide_wait); |
| 2175 | |
| 2176 | if ((cmd & (REQ_IDETAPE_READ | REQ_IDETAPE_WRITE)) == 0) |
| 2177 | return 0; |
| 2178 | |
| 2179 | if (tape->merge_stage) |
| 2180 | idetape_init_merge_stage(tape); |
| 2181 | if (rq.errors == IDETAPE_ERROR_GENERAL) |
| 2182 | return -EIO; |
Borislav Petkov | 54bb207 | 2008-02-06 02:57:52 +0100 | [diff] [blame] | 2183 | return (tape->blk_size * (blocks-rq.current_nr_sectors)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2184 | } |
| 2185 | |
Borislav Petkov | 8d06bfa | 2008-02-06 02:57:53 +0100 | [diff] [blame] | 2186 | /* start servicing the pipeline stages, starting from tape->next_stage. */ |
| 2187 | static void idetape_plug_pipeline(ide_drive_t *drive) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2188 | { |
| 2189 | idetape_tape_t *tape = drive->driver_data; |
| 2190 | |
| 2191 | if (tape->next_stage == NULL) |
| 2192 | return; |
| 2193 | if (!idetape_pipeline_active(tape)) { |
| 2194 | set_bit(IDETAPE_PIPELINE_ACTIVE, &tape->flags); |
Borislav Petkov | 419d474 | 2008-02-02 19:56:51 +0100 | [diff] [blame] | 2195 | idetape_activate_next_stage(drive); |
Borislav Petkov | 54bb207 | 2008-02-06 02:57:52 +0100 | [diff] [blame] | 2196 | (void) ide_do_drive_cmd(drive, tape->active_data_rq, ide_end); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2197 | } |
| 2198 | } |
| 2199 | |
Borislav Petkov | 5a04cfa | 2008-02-06 02:57:54 +0100 | [diff] [blame] | 2200 | static void idetape_create_inquiry_cmd(idetape_pc_t *pc) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2201 | { |
| 2202 | idetape_init_pc(pc); |
Borislav Petkov | 90699ce | 2008-02-02 19:56:50 +0100 | [diff] [blame] | 2203 | pc->c[0] = INQUIRY; |
Borislav Petkov | 5a04cfa | 2008-02-06 02:57:54 +0100 | [diff] [blame] | 2204 | pc->c[4] = 254; |
| 2205 | pc->request_transfer = 254; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2206 | pc->callback = &idetape_pc_callback; |
| 2207 | } |
| 2208 | |
Borislav Petkov | 5a04cfa | 2008-02-06 02:57:54 +0100 | [diff] [blame] | 2209 | static void idetape_create_rewind_cmd(ide_drive_t *drive, idetape_pc_t *pc) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2210 | { |
| 2211 | idetape_init_pc(pc); |
Borislav Petkov | 90699ce | 2008-02-02 19:56:50 +0100 | [diff] [blame] | 2212 | pc->c[0] = REZERO_UNIT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2213 | set_bit(PC_WAIT_FOR_DSC, &pc->flags); |
| 2214 | pc->callback = &idetape_pc_callback; |
| 2215 | } |
| 2216 | |
Borislav Petkov | 5a04cfa | 2008-02-06 02:57:54 +0100 | [diff] [blame] | 2217 | static void idetape_create_erase_cmd(idetape_pc_t *pc) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2218 | { |
| 2219 | idetape_init_pc(pc); |
Borislav Petkov | 90699ce | 2008-02-02 19:56:50 +0100 | [diff] [blame] | 2220 | pc->c[0] = ERASE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2221 | pc->c[1] = 1; |
| 2222 | set_bit(PC_WAIT_FOR_DSC, &pc->flags); |
| 2223 | pc->callback = &idetape_pc_callback; |
| 2224 | } |
| 2225 | |
Borislav Petkov | 5a04cfa | 2008-02-06 02:57:54 +0100 | [diff] [blame] | 2226 | static void idetape_create_space_cmd(idetape_pc_t *pc, int count, u8 cmd) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2227 | { |
| 2228 | idetape_init_pc(pc); |
Borislav Petkov | 90699ce | 2008-02-02 19:56:50 +0100 | [diff] [blame] | 2229 | pc->c[0] = SPACE; |
Borislav Petkov | 860ff5e | 2008-02-02 19:56:50 +0100 | [diff] [blame] | 2230 | put_unaligned(cpu_to_be32(count), (unsigned int *) &pc->c[1]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2231 | pc->c[1] = cmd; |
| 2232 | set_bit(PC_WAIT_FOR_DSC, &pc->flags); |
| 2233 | pc->callback = &idetape_pc_callback; |
| 2234 | } |
| 2235 | |
Borislav Petkov | 5a04cfa | 2008-02-06 02:57:54 +0100 | [diff] [blame] | 2236 | static void idetape_wait_first_stage(ide_drive_t *drive) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2237 | { |
| 2238 | idetape_tape_t *tape = drive->driver_data; |
| 2239 | unsigned long flags; |
| 2240 | |
| 2241 | if (tape->first_stage == NULL) |
| 2242 | return; |
Borislav Petkov | 54bb207 | 2008-02-06 02:57:52 +0100 | [diff] [blame] | 2243 | spin_lock_irqsave(&tape->lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2244 | if (tape->active_stage == tape->first_stage) |
Borislav Petkov | 54bb207 | 2008-02-06 02:57:52 +0100 | [diff] [blame] | 2245 | idetape_wait_for_request(drive, tape->active_data_rq); |
| 2246 | spin_unlock_irqrestore(&tape->lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2247 | } |
| 2248 | |
| 2249 | /* |
Borislav Petkov | 3c98bf3 | 2008-02-06 02:57:53 +0100 | [diff] [blame] | 2250 | * Try to add a character device originated write request to our pipeline. In |
| 2251 | * case we don't succeed, we revert to non-pipelined operation mode for this |
| 2252 | * request. In order to accomplish that, we |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2253 | * |
Borislav Petkov | 3c98bf3 | 2008-02-06 02:57:53 +0100 | [diff] [blame] | 2254 | * 1. Try to allocate a new pipeline stage. |
| 2255 | * 2. If we can't, wait for more and more requests to be serviced and try again |
| 2256 | * each time. |
| 2257 | * 3. If we still can't allocate a stage, fallback to non-pipelined operation |
| 2258 | * mode for this request. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2259 | */ |
Borislav Petkov | 5a04cfa | 2008-02-06 02:57:54 +0100 | [diff] [blame] | 2260 | 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] | 2261 | { |
| 2262 | idetape_tape_t *tape = drive->driver_data; |
| 2263 | idetape_stage_t *new_stage; |
| 2264 | unsigned long flags; |
| 2265 | struct request *rq; |
| 2266 | |
Borislav Petkov | 8004a8c | 2008-02-06 02:57:51 +0100 | [diff] [blame] | 2267 | debug_log(DBG_CHRDEV, "Enter %s\n", __func__); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2268 | |
Borislav Petkov | 3c98bf3 | 2008-02-06 02:57:53 +0100 | [diff] [blame] | 2269 | /* Attempt to allocate a new stage. Beware possible race conditions. */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2270 | while ((new_stage = idetape_kmalloc_stage(tape)) == NULL) { |
Borislav Petkov | 54bb207 | 2008-02-06 02:57:52 +0100 | [diff] [blame] | 2271 | spin_lock_irqsave(&tape->lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2272 | if (idetape_pipeline_active(tape)) { |
Borislav Petkov | 54bb207 | 2008-02-06 02:57:52 +0100 | [diff] [blame] | 2273 | idetape_wait_for_request(drive, tape->active_data_rq); |
| 2274 | spin_unlock_irqrestore(&tape->lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2275 | } else { |
Borislav Petkov | 54bb207 | 2008-02-06 02:57:52 +0100 | [diff] [blame] | 2276 | spin_unlock_irqrestore(&tape->lock, flags); |
Borislav Petkov | 8d06bfa | 2008-02-06 02:57:53 +0100 | [diff] [blame] | 2277 | idetape_plug_pipeline(drive); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2278 | if (idetape_pipeline_active(tape)) |
| 2279 | continue; |
| 2280 | /* |
Borislav Petkov | 3c98bf3 | 2008-02-06 02:57:53 +0100 | [diff] [blame] | 2281 | * The machine is short on memory. Fallback to non- |
| 2282 | * pipelined operation mode for this request. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2283 | */ |
Borislav Petkov | 5a04cfa | 2008-02-06 02:57:54 +0100 | [diff] [blame] | 2284 | return idetape_queue_rw_tail(drive, REQ_IDETAPE_WRITE, |
| 2285 | blocks, tape->merge_stage->bh); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2286 | } |
| 2287 | } |
| 2288 | rq = &new_stage->rq; |
| 2289 | idetape_init_rq(rq, REQ_IDETAPE_WRITE); |
| 2290 | /* Doesn't actually matter - We always assume sequential access */ |
Borislav Petkov | 54bb207 | 2008-02-06 02:57:52 +0100 | [diff] [blame] | 2291 | rq->sector = tape->first_frame; |
Borislav Petkov | 5a04cfa | 2008-02-06 02:57:54 +0100 | [diff] [blame] | 2292 | rq->current_nr_sectors = blocks; |
| 2293 | rq->nr_sectors = blocks; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2294 | |
| 2295 | idetape_switch_buffers(tape, new_stage); |
| 2296 | idetape_add_stage_tail(drive, new_stage); |
| 2297 | tape->pipeline_head++; |
Borislav Petkov | 37016ba | 2008-02-06 02:57:52 +0100 | [diff] [blame] | 2298 | idetape_calculate_speeds(drive); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2299 | |
| 2300 | /* |
Borislav Petkov | 3c98bf3 | 2008-02-06 02:57:53 +0100 | [diff] [blame] | 2301 | * Estimate whether the tape has stopped writing by checking if our |
| 2302 | * write pipeline is currently empty. If we are not writing anymore, |
| 2303 | * wait for the pipeline to be almost completely full (90%) before |
| 2304 | * starting to service requests, so that we will be able to keep up with |
| 2305 | * the higher speeds of the tape. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2306 | */ |
| 2307 | if (!idetape_pipeline_active(tape)) { |
| 2308 | if (tape->nr_stages >= tape->max_stages * 9 / 10 || |
Borislav Petkov | 54bb207 | 2008-02-06 02:57:52 +0100 | [diff] [blame] | 2309 | tape->nr_stages >= tape->max_stages - |
| 2310 | tape->uncontrolled_pipeline_head_speed * 3 * 1024 / |
| 2311 | tape->blk_size) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2312 | tape->measure_insert_time = 1; |
| 2313 | tape->insert_time = jiffies; |
| 2314 | tape->insert_size = 0; |
| 2315 | tape->insert_speed = 0; |
Borislav Petkov | 8d06bfa | 2008-02-06 02:57:53 +0100 | [diff] [blame] | 2316 | idetape_plug_pipeline(drive); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2317 | } |
| 2318 | } |
| 2319 | if (test_and_clear_bit(IDETAPE_PIPELINE_ERROR, &tape->flags)) |
| 2320 | /* Return a deferred error */ |
| 2321 | return -EIO; |
| 2322 | return blocks; |
| 2323 | } |
| 2324 | |
| 2325 | /* |
Borislav Petkov | 3c98bf3 | 2008-02-06 02:57:53 +0100 | [diff] [blame] | 2326 | * Wait until all pending pipeline requests are serviced. Typically called on |
| 2327 | * device close. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2328 | */ |
Borislav Petkov | 5a04cfa | 2008-02-06 02:57:54 +0100 | [diff] [blame] | 2329 | static void idetape_wait_for_pipeline(ide_drive_t *drive) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2330 | { |
| 2331 | idetape_tape_t *tape = drive->driver_data; |
| 2332 | unsigned long flags; |
| 2333 | |
| 2334 | while (tape->next_stage || idetape_pipeline_active(tape)) { |
Borislav Petkov | 8d06bfa | 2008-02-06 02:57:53 +0100 | [diff] [blame] | 2335 | idetape_plug_pipeline(drive); |
Borislav Petkov | 54bb207 | 2008-02-06 02:57:52 +0100 | [diff] [blame] | 2336 | spin_lock_irqsave(&tape->lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2337 | if (idetape_pipeline_active(tape)) |
Borislav Petkov | 54bb207 | 2008-02-06 02:57:52 +0100 | [diff] [blame] | 2338 | idetape_wait_for_request(drive, tape->active_data_rq); |
| 2339 | spin_unlock_irqrestore(&tape->lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2340 | } |
| 2341 | } |
| 2342 | |
Borislav Petkov | 5a04cfa | 2008-02-06 02:57:54 +0100 | [diff] [blame] | 2343 | static void idetape_empty_write_pipeline(ide_drive_t *drive) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2344 | { |
| 2345 | idetape_tape_t *tape = drive->driver_data; |
| 2346 | int blocks, min; |
| 2347 | struct idetape_bh *bh; |
Borislav Petkov | 55a5d29 | 2008-02-02 19:56:49 +0100 | [diff] [blame] | 2348 | |
Borislav Petkov | 54abf37 | 2008-02-06 02:57:52 +0100 | [diff] [blame] | 2349 | if (tape->chrdev_dir != IDETAPE_DIR_WRITE) { |
Borislav Petkov | 5a04cfa | 2008-02-06 02:57:54 +0100 | [diff] [blame] | 2350 | printk(KERN_ERR "ide-tape: bug: Trying to empty write pipeline," |
| 2351 | " but we are not writing.\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2352 | return; |
| 2353 | } |
| 2354 | if (tape->merge_stage_size > tape->stage_size) { |
| 2355 | printk(KERN_ERR "ide-tape: bug: merge_buffer too big\n"); |
| 2356 | tape->merge_stage_size = tape->stage_size; |
| 2357 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2358 | if (tape->merge_stage_size) { |
Borislav Petkov | 54bb207 | 2008-02-06 02:57:52 +0100 | [diff] [blame] | 2359 | blocks = tape->merge_stage_size / tape->blk_size; |
| 2360 | if (tape->merge_stage_size % tape->blk_size) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2361 | unsigned int i; |
| 2362 | |
| 2363 | blocks++; |
Borislav Petkov | 54bb207 | 2008-02-06 02:57:52 +0100 | [diff] [blame] | 2364 | i = tape->blk_size - tape->merge_stage_size % |
| 2365 | tape->blk_size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2366 | bh = tape->bh->b_reqnext; |
| 2367 | while (bh) { |
| 2368 | atomic_set(&bh->b_count, 0); |
| 2369 | bh = bh->b_reqnext; |
| 2370 | } |
| 2371 | bh = tape->bh; |
| 2372 | while (i) { |
| 2373 | if (bh == NULL) { |
Borislav Petkov | 5a04cfa | 2008-02-06 02:57:54 +0100 | [diff] [blame] | 2374 | printk(KERN_INFO "ide-tape: bug," |
| 2375 | " bh NULL\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2376 | break; |
| 2377 | } |
Borislav Petkov | 5a04cfa | 2008-02-06 02:57:54 +0100 | [diff] [blame] | 2378 | min = min(i, (unsigned int)(bh->b_size - |
| 2379 | atomic_read(&bh->b_count))); |
| 2380 | memset(bh->b_data + atomic_read(&bh->b_count), |
| 2381 | 0, min); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2382 | atomic_add(min, &bh->b_count); |
| 2383 | i -= min; |
| 2384 | bh = bh->b_reqnext; |
| 2385 | } |
| 2386 | } |
| 2387 | (void) idetape_add_chrdev_write_request(drive, blocks); |
| 2388 | tape->merge_stage_size = 0; |
| 2389 | } |
| 2390 | idetape_wait_for_pipeline(drive); |
| 2391 | if (tape->merge_stage != NULL) { |
| 2392 | __idetape_kfree_stage(tape->merge_stage); |
| 2393 | tape->merge_stage = NULL; |
| 2394 | } |
| 2395 | clear_bit(IDETAPE_PIPELINE_ERROR, &tape->flags); |
Borislav Petkov | 54abf37 | 2008-02-06 02:57:52 +0100 | [diff] [blame] | 2396 | tape->chrdev_dir = IDETAPE_DIR_NONE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2397 | |
| 2398 | /* |
Borislav Petkov | 3c98bf3 | 2008-02-06 02:57:53 +0100 | [diff] [blame] | 2399 | * On the next backup, perform the feedback loop again. (I don't want to |
| 2400 | * keep sense information between backups, as some systems are |
| 2401 | * constantly on, and the system load can be totally different on the |
| 2402 | * next backup). |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2403 | */ |
| 2404 | tape->max_stages = tape->min_pipeline; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2405 | if (tape->first_stage != NULL || |
| 2406 | tape->next_stage != NULL || |
| 2407 | tape->last_stage != NULL || |
| 2408 | tape->nr_stages != 0) { |
| 2409 | printk(KERN_ERR "ide-tape: ide-tape pipeline bug, " |
| 2410 | "first_stage %p, next_stage %p, " |
| 2411 | "last_stage %p, nr_stages %d\n", |
| 2412 | tape->first_stage, tape->next_stage, |
| 2413 | tape->last_stage, tape->nr_stages); |
| 2414 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2415 | } |
| 2416 | |
Borislav Petkov | 5a04cfa | 2008-02-06 02:57:54 +0100 | [diff] [blame] | 2417 | static void idetape_restart_speed_control(ide_drive_t *drive) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2418 | { |
| 2419 | idetape_tape_t *tape = drive->driver_data; |
| 2420 | |
| 2421 | tape->restart_speed_control_req = 0; |
| 2422 | tape->pipeline_head = 0; |
Borislav Petkov | 41f81d54 | 2008-02-06 02:57:52 +0100 | [diff] [blame] | 2423 | tape->controlled_last_pipeline_head = 0; |
Borislav Petkov | 5a04cfa | 2008-02-06 02:57:54 +0100 | [diff] [blame] | 2424 | tape->controlled_previous_pipeline_head = 0; |
| 2425 | tape->uncontrolled_previous_pipeline_head = 0; |
| 2426 | tape->controlled_pipeline_head_speed = 5000; |
| 2427 | tape->pipeline_head_speed = 5000; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2428 | tape->uncontrolled_pipeline_head_speed = 0; |
Borislav Petkov | 5a04cfa | 2008-02-06 02:57:54 +0100 | [diff] [blame] | 2429 | tape->controlled_pipeline_head_time = |
| 2430 | tape->uncontrolled_pipeline_head_time = jiffies; |
| 2431 | tape->controlled_previous_head_time = |
| 2432 | tape->uncontrolled_previous_head_time = jiffies; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2433 | } |
| 2434 | |
Borislav Petkov | 8d06bfa | 2008-02-06 02:57:53 +0100 | [diff] [blame] | 2435 | static int idetape_init_read(ide_drive_t *drive, int max_stages) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2436 | { |
| 2437 | idetape_tape_t *tape = drive->driver_data; |
| 2438 | idetape_stage_t *new_stage; |
| 2439 | struct request rq; |
| 2440 | int bytes_read; |
Borislav Petkov | b642201 | 2008-02-02 19:56:49 +0100 | [diff] [blame] | 2441 | u16 blocks = *(u16 *)&tape->caps[12]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2442 | |
| 2443 | /* Initialize read operation */ |
Borislav Petkov | 54abf37 | 2008-02-06 02:57:52 +0100 | [diff] [blame] | 2444 | if (tape->chrdev_dir != IDETAPE_DIR_READ) { |
| 2445 | if (tape->chrdev_dir == IDETAPE_DIR_WRITE) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2446 | idetape_empty_write_pipeline(drive); |
| 2447 | idetape_flush_tape_buffers(drive); |
| 2448 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2449 | if (tape->merge_stage || tape->merge_stage_size) { |
Borislav Petkov | 5a04cfa | 2008-02-06 02:57:54 +0100 | [diff] [blame] | 2450 | printk(KERN_ERR "ide-tape: merge_stage_size should be" |
| 2451 | " 0 now\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2452 | tape->merge_stage_size = 0; |
| 2453 | } |
Borislav Petkov | 5a04cfa | 2008-02-06 02:57:54 +0100 | [diff] [blame] | 2454 | tape->merge_stage = __idetape_kmalloc_stage(tape, 0, 0); |
| 2455 | if (!tape->merge_stage) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2456 | return -ENOMEM; |
Borislav Petkov | 54abf37 | 2008-02-06 02:57:52 +0100 | [diff] [blame] | 2457 | tape->chrdev_dir = IDETAPE_DIR_READ; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2458 | |
| 2459 | /* |
Borislav Petkov | 3c98bf3 | 2008-02-06 02:57:53 +0100 | [diff] [blame] | 2460 | * Issue a read 0 command to ensure that DSC handshake is |
| 2461 | * switched from completion mode to buffer available mode. |
| 2462 | * No point in issuing this if DSC overlap isn't supported, some |
| 2463 | * drives (Seagate STT3401A) will return an error. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2464 | */ |
| 2465 | if (drive->dsc_overlap) { |
Borislav Petkov | 5a04cfa | 2008-02-06 02:57:54 +0100 | [diff] [blame] | 2466 | bytes_read = idetape_queue_rw_tail(drive, |
| 2467 | REQ_IDETAPE_READ, 0, |
| 2468 | tape->merge_stage->bh); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2469 | if (bytes_read < 0) { |
| 2470 | __idetape_kfree_stage(tape->merge_stage); |
| 2471 | tape->merge_stage = NULL; |
Borislav Petkov | 54abf37 | 2008-02-06 02:57:52 +0100 | [diff] [blame] | 2472 | tape->chrdev_dir = IDETAPE_DIR_NONE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2473 | return bytes_read; |
| 2474 | } |
| 2475 | } |
| 2476 | } |
| 2477 | if (tape->restart_speed_control_req) |
| 2478 | idetape_restart_speed_control(drive); |
| 2479 | idetape_init_rq(&rq, REQ_IDETAPE_READ); |
Borislav Petkov | 54bb207 | 2008-02-06 02:57:52 +0100 | [diff] [blame] | 2480 | rq.sector = tape->first_frame; |
Borislav Petkov | 5a04cfa | 2008-02-06 02:57:54 +0100 | [diff] [blame] | 2481 | rq.nr_sectors = blocks; |
| 2482 | rq.current_nr_sectors = blocks; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2483 | if (!test_bit(IDETAPE_PIPELINE_ERROR, &tape->flags) && |
| 2484 | tape->nr_stages < max_stages) { |
| 2485 | new_stage = idetape_kmalloc_stage(tape); |
| 2486 | while (new_stage != NULL) { |
| 2487 | new_stage->rq = rq; |
| 2488 | idetape_add_stage_tail(drive, new_stage); |
| 2489 | if (tape->nr_stages >= max_stages) |
| 2490 | break; |
| 2491 | new_stage = idetape_kmalloc_stage(tape); |
| 2492 | } |
| 2493 | } |
| 2494 | if (!idetape_pipeline_active(tape)) { |
| 2495 | if (tape->nr_pending_stages >= 3 * max_stages / 4) { |
| 2496 | tape->measure_insert_time = 1; |
| 2497 | tape->insert_time = jiffies; |
| 2498 | tape->insert_size = 0; |
| 2499 | tape->insert_speed = 0; |
Borislav Petkov | 8d06bfa | 2008-02-06 02:57:53 +0100 | [diff] [blame] | 2500 | idetape_plug_pipeline(drive); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2501 | } |
| 2502 | } |
| 2503 | return 0; |
| 2504 | } |
| 2505 | |
| 2506 | /* |
Borislav Petkov | 3c98bf3 | 2008-02-06 02:57:53 +0100 | [diff] [blame] | 2507 | * Called from idetape_chrdev_read() to service a character device read request |
| 2508 | * and add read-ahead requests to our pipeline. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2509 | */ |
Borislav Petkov | 5a04cfa | 2008-02-06 02:57:54 +0100 | [diff] [blame] | 2510 | 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] | 2511 | { |
| 2512 | idetape_tape_t *tape = drive->driver_data; |
| 2513 | unsigned long flags; |
| 2514 | struct request *rq_ptr; |
| 2515 | int bytes_read; |
| 2516 | |
Borislav Petkov | 8004a8c | 2008-02-06 02:57:51 +0100 | [diff] [blame] | 2517 | debug_log(DBG_PROCS, "Enter %s, %d blocks\n", __func__, blocks); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2518 | |
Borislav Petkov | 3c98bf3 | 2008-02-06 02:57:53 +0100 | [diff] [blame] | 2519 | /* If we are at a filemark, return a read length of 0 */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2520 | if (test_bit(IDETAPE_FILEMARK, &tape->flags)) |
| 2521 | return 0; |
| 2522 | |
Borislav Petkov | 3c98bf3 | 2008-02-06 02:57:53 +0100 | [diff] [blame] | 2523 | /* Wait for the next block to reach the head of the pipeline. */ |
Borislav Petkov | 8d06bfa | 2008-02-06 02:57:53 +0100 | [diff] [blame] | 2524 | idetape_init_read(drive, tape->max_stages); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2525 | if (tape->first_stage == NULL) { |
| 2526 | if (test_bit(IDETAPE_PIPELINE_ERROR, &tape->flags)) |
| 2527 | return 0; |
Borislav Petkov | 54bb207 | 2008-02-06 02:57:52 +0100 | [diff] [blame] | 2528 | return idetape_queue_rw_tail(drive, REQ_IDETAPE_READ, blocks, |
| 2529 | tape->merge_stage->bh); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2530 | } |
| 2531 | idetape_wait_first_stage(drive); |
| 2532 | rq_ptr = &tape->first_stage->rq; |
Borislav Petkov | 54bb207 | 2008-02-06 02:57:52 +0100 | [diff] [blame] | 2533 | bytes_read = tape->blk_size * (rq_ptr->nr_sectors - |
| 2534 | rq_ptr->current_nr_sectors); |
Borislav Petkov | 5a04cfa | 2008-02-06 02:57:54 +0100 | [diff] [blame] | 2535 | rq_ptr->nr_sectors = 0; |
| 2536 | rq_ptr->current_nr_sectors = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2537 | |
| 2538 | if (rq_ptr->errors == IDETAPE_ERROR_EOD) |
| 2539 | return 0; |
| 2540 | else { |
| 2541 | idetape_switch_buffers(tape, tape->first_stage); |
| 2542 | if (rq_ptr->errors == IDETAPE_ERROR_FILEMARK) |
| 2543 | set_bit(IDETAPE_FILEMARK, &tape->flags); |
Borislav Petkov | 54bb207 | 2008-02-06 02:57:52 +0100 | [diff] [blame] | 2544 | spin_lock_irqsave(&tape->lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2545 | idetape_remove_stage_head(drive); |
Borislav Petkov | 54bb207 | 2008-02-06 02:57:52 +0100 | [diff] [blame] | 2546 | spin_unlock_irqrestore(&tape->lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2547 | tape->pipeline_head++; |
Borislav Petkov | 37016ba | 2008-02-06 02:57:52 +0100 | [diff] [blame] | 2548 | idetape_calculate_speeds(drive); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2549 | } |
Borislav Petkov | 54bb207 | 2008-02-06 02:57:52 +0100 | [diff] [blame] | 2550 | if (bytes_read > blocks * tape->blk_size) { |
Borislav Petkov | 5a04cfa | 2008-02-06 02:57:54 +0100 | [diff] [blame] | 2551 | printk(KERN_ERR "ide-tape: bug: trying to return more bytes" |
| 2552 | " than requested\n"); |
Borislav Petkov | 54bb207 | 2008-02-06 02:57:52 +0100 | [diff] [blame] | 2553 | bytes_read = blocks * tape->blk_size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2554 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2555 | return (bytes_read); |
| 2556 | } |
| 2557 | |
Borislav Petkov | 5a04cfa | 2008-02-06 02:57:54 +0100 | [diff] [blame] | 2558 | static void idetape_pad_zeros(ide_drive_t *drive, int bcount) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2559 | { |
| 2560 | idetape_tape_t *tape = drive->driver_data; |
| 2561 | struct idetape_bh *bh; |
| 2562 | int blocks; |
Borislav Petkov | 3c98bf3 | 2008-02-06 02:57:53 +0100 | [diff] [blame] | 2563 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2564 | while (bcount) { |
| 2565 | unsigned int count; |
| 2566 | |
| 2567 | bh = tape->merge_stage->bh; |
| 2568 | count = min(tape->stage_size, bcount); |
| 2569 | bcount -= count; |
Borislav Petkov | 54bb207 | 2008-02-06 02:57:52 +0100 | [diff] [blame] | 2570 | blocks = count / tape->blk_size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2571 | while (count) { |
Borislav Petkov | 5a04cfa | 2008-02-06 02:57:54 +0100 | [diff] [blame] | 2572 | atomic_set(&bh->b_count, |
| 2573 | min(count, (unsigned int)bh->b_size)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2574 | memset(bh->b_data, 0, atomic_read(&bh->b_count)); |
| 2575 | count -= atomic_read(&bh->b_count); |
| 2576 | bh = bh->b_reqnext; |
| 2577 | } |
Borislav Petkov | 5a04cfa | 2008-02-06 02:57:54 +0100 | [diff] [blame] | 2578 | idetape_queue_rw_tail(drive, REQ_IDETAPE_WRITE, blocks, |
| 2579 | tape->merge_stage->bh); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2580 | } |
| 2581 | } |
| 2582 | |
Borislav Petkov | 5a04cfa | 2008-02-06 02:57:54 +0100 | [diff] [blame] | 2583 | static int idetape_pipeline_size(ide_drive_t *drive) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2584 | { |
| 2585 | idetape_tape_t *tape = drive->driver_data; |
| 2586 | idetape_stage_t *stage; |
| 2587 | struct request *rq; |
| 2588 | int size = 0; |
| 2589 | |
| 2590 | idetape_wait_for_pipeline(drive); |
| 2591 | stage = tape->first_stage; |
| 2592 | while (stage != NULL) { |
| 2593 | rq = &stage->rq; |
Borislav Petkov | 54bb207 | 2008-02-06 02:57:52 +0100 | [diff] [blame] | 2594 | size += tape->blk_size * (rq->nr_sectors - |
| 2595 | rq->current_nr_sectors); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2596 | if (rq->errors == IDETAPE_ERROR_FILEMARK) |
Borislav Petkov | 54bb207 | 2008-02-06 02:57:52 +0100 | [diff] [blame] | 2597 | size += tape->blk_size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2598 | stage = stage->next; |
| 2599 | } |
| 2600 | size += tape->merge_stage_size; |
| 2601 | return size; |
| 2602 | } |
| 2603 | |
| 2604 | /* |
Borislav Petkov | 3c98bf3 | 2008-02-06 02:57:53 +0100 | [diff] [blame] | 2605 | * Rewinds the tape to the Beginning Of the current Partition (BOP). We |
| 2606 | * currently support only one partition. |
| 2607 | */ |
Borislav Petkov | 5a04cfa | 2008-02-06 02:57:54 +0100 | [diff] [blame] | 2608 | static int idetape_rewind_tape(ide_drive_t *drive) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2609 | { |
| 2610 | int retval; |
| 2611 | idetape_pc_t pc; |
Borislav Petkov | 8004a8c | 2008-02-06 02:57:51 +0100 | [diff] [blame] | 2612 | idetape_tape_t *tape; |
| 2613 | tape = drive->driver_data; |
| 2614 | |
| 2615 | debug_log(DBG_SENSE, "Enter %s\n", __func__); |
| 2616 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2617 | idetape_create_rewind_cmd(drive, &pc); |
| 2618 | retval = idetape_queue_pc_tail(drive, &pc); |
| 2619 | if (retval) |
| 2620 | return retval; |
| 2621 | |
| 2622 | idetape_create_read_position_cmd(&pc); |
| 2623 | retval = idetape_queue_pc_tail(drive, &pc); |
| 2624 | if (retval) |
| 2625 | return retval; |
| 2626 | return 0; |
| 2627 | } |
| 2628 | |
Borislav Petkov | 3c98bf3 | 2008-02-06 02:57:53 +0100 | [diff] [blame] | 2629 | /* mtio.h compatible commands should be issued to the chrdev interface. */ |
Borislav Petkov | 5a04cfa | 2008-02-06 02:57:54 +0100 | [diff] [blame] | 2630 | static int idetape_blkdev_ioctl(ide_drive_t *drive, unsigned int cmd, |
| 2631 | unsigned long arg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2632 | { |
| 2633 | idetape_tape_t *tape = drive->driver_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2634 | void __user *argp = (void __user *)arg; |
| 2635 | |
Borislav Petkov | d59823f | 2008-02-02 19:56:51 +0100 | [diff] [blame] | 2636 | struct idetape_config { |
| 2637 | int dsc_rw_frequency; |
| 2638 | int dsc_media_access_frequency; |
| 2639 | int nr_stages; |
| 2640 | } config; |
| 2641 | |
Borislav Petkov | 8004a8c | 2008-02-06 02:57:51 +0100 | [diff] [blame] | 2642 | debug_log(DBG_PROCS, "Enter %s\n", __func__); |
| 2643 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2644 | switch (cmd) { |
Borislav Petkov | 5a04cfa | 2008-02-06 02:57:54 +0100 | [diff] [blame] | 2645 | case 0x0340: |
| 2646 | if (copy_from_user(&config, argp, sizeof(config))) |
| 2647 | return -EFAULT; |
| 2648 | tape->best_dsc_rw_freq = config.dsc_rw_frequency; |
| 2649 | tape->max_stages = config.nr_stages; |
| 2650 | break; |
| 2651 | case 0x0350: |
| 2652 | config.dsc_rw_frequency = (int) tape->best_dsc_rw_freq; |
| 2653 | config.nr_stages = tape->max_stages; |
| 2654 | if (copy_to_user(argp, &config, sizeof(config))) |
| 2655 | return -EFAULT; |
| 2656 | break; |
| 2657 | default: |
| 2658 | return -EIO; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2659 | } |
| 2660 | return 0; |
| 2661 | } |
| 2662 | |
| 2663 | /* |
Borislav Petkov | 3c98bf3 | 2008-02-06 02:57:53 +0100 | [diff] [blame] | 2664 | * The function below is now a bit more complicated than just passing the |
| 2665 | * command to the tape since we may have crossed some filemarks during our |
| 2666 | * pipelined read-ahead mode. As a minor side effect, the pipeline enables us to |
| 2667 | * support MTFSFM when the filemark is in our internal pipeline even if the tape |
| 2668 | * doesn't support spacing over filemarks in the reverse direction. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2669 | */ |
Borislav Petkov | 5a04cfa | 2008-02-06 02:57:54 +0100 | [diff] [blame] | 2670 | static int idetape_space_over_filemarks(ide_drive_t *drive, short mt_op, |
| 2671 | int mt_count) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2672 | { |
| 2673 | idetape_tape_t *tape = drive->driver_data; |
| 2674 | idetape_pc_t pc; |
| 2675 | unsigned long flags; |
Borislav Petkov | 5a04cfa | 2008-02-06 02:57:54 +0100 | [diff] [blame] | 2676 | int retval, count = 0; |
Borislav Petkov | b642201 | 2008-02-02 19:56:49 +0100 | [diff] [blame] | 2677 | int sprev = !!(tape->caps[4] & 0x20); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2678 | |
| 2679 | if (mt_count == 0) |
| 2680 | return 0; |
| 2681 | if (MTBSF == mt_op || MTBSFM == mt_op) { |
Borislav Petkov | b642201 | 2008-02-02 19:56:49 +0100 | [diff] [blame] | 2682 | if (!sprev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2683 | return -EIO; |
Borislav Petkov | 5a04cfa | 2008-02-06 02:57:54 +0100 | [diff] [blame] | 2684 | mt_count = -mt_count; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2685 | } |
| 2686 | |
Borislav Petkov | 54abf37 | 2008-02-06 02:57:52 +0100 | [diff] [blame] | 2687 | if (tape->chrdev_dir == IDETAPE_DIR_READ) { |
Borislav Petkov | 3c98bf3 | 2008-02-06 02:57:53 +0100 | [diff] [blame] | 2688 | /* its a read-ahead buffer, scan it for crossed filemarks. */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2689 | tape->merge_stage_size = 0; |
| 2690 | if (test_and_clear_bit(IDETAPE_FILEMARK, &tape->flags)) |
| 2691 | ++count; |
| 2692 | while (tape->first_stage != NULL) { |
| 2693 | if (count == mt_count) { |
| 2694 | if (mt_op == MTFSFM) |
| 2695 | set_bit(IDETAPE_FILEMARK, &tape->flags); |
| 2696 | return 0; |
| 2697 | } |
Borislav Petkov | 54bb207 | 2008-02-06 02:57:52 +0100 | [diff] [blame] | 2698 | spin_lock_irqsave(&tape->lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2699 | if (tape->first_stage == tape->active_stage) { |
| 2700 | /* |
Borislav Petkov | 3c98bf3 | 2008-02-06 02:57:53 +0100 | [diff] [blame] | 2701 | * We have reached the active stage in the read |
| 2702 | * pipeline. There is no point in allowing the |
| 2703 | * drive to continue reading any farther, so we |
| 2704 | * stop the pipeline. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2705 | * |
Borislav Petkov | 3c98bf3 | 2008-02-06 02:57:53 +0100 | [diff] [blame] | 2706 | * This section should be moved to a separate |
| 2707 | * subroutine because similar operations are |
| 2708 | * done in __idetape_discard_read_pipeline(), |
| 2709 | * for example. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2710 | */ |
| 2711 | tape->next_stage = NULL; |
Borislav Petkov | 54bb207 | 2008-02-06 02:57:52 +0100 | [diff] [blame] | 2712 | spin_unlock_irqrestore(&tape->lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2713 | idetape_wait_first_stage(drive); |
| 2714 | tape->next_stage = tape->first_stage->next; |
| 2715 | } else |
Borislav Petkov | 54bb207 | 2008-02-06 02:57:52 +0100 | [diff] [blame] | 2716 | spin_unlock_irqrestore(&tape->lock, flags); |
Borislav Petkov | 5a04cfa | 2008-02-06 02:57:54 +0100 | [diff] [blame] | 2717 | if (tape->first_stage->rq.errors == |
| 2718 | IDETAPE_ERROR_FILEMARK) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2719 | ++count; |
| 2720 | idetape_remove_stage_head(drive); |
| 2721 | } |
| 2722 | idetape_discard_read_pipeline(drive, 0); |
| 2723 | } |
| 2724 | |
| 2725 | /* |
Borislav Petkov | 3c98bf3 | 2008-02-06 02:57:53 +0100 | [diff] [blame] | 2726 | * The filemark was not found in our internal pipeline; now we can issue |
| 2727 | * the space command. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2728 | */ |
| 2729 | switch (mt_op) { |
Borislav Petkov | 5a04cfa | 2008-02-06 02:57:54 +0100 | [diff] [blame] | 2730 | case MTFSF: |
| 2731 | case MTBSF: |
| 2732 | idetape_create_space_cmd(&pc, mt_count - count, |
| 2733 | IDETAPE_SPACE_OVER_FILEMARK); |
| 2734 | return idetape_queue_pc_tail(drive, &pc); |
| 2735 | case MTFSFM: |
| 2736 | case MTBSFM: |
| 2737 | if (!sprev) |
| 2738 | return -EIO; |
| 2739 | retval = idetape_space_over_filemarks(drive, MTFSF, |
| 2740 | mt_count - count); |
| 2741 | if (retval) |
| 2742 | return retval; |
| 2743 | count = (MTBSFM == mt_op ? 1 : -1); |
| 2744 | return idetape_space_over_filemarks(drive, MTFSF, count); |
| 2745 | default: |
| 2746 | printk(KERN_ERR "ide-tape: MTIO operation %d not supported\n", |
| 2747 | mt_op); |
| 2748 | return -EIO; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2749 | } |
| 2750 | } |
| 2751 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2752 | /* |
Borislav Petkov | 3c98bf3 | 2008-02-06 02:57:53 +0100 | [diff] [blame] | 2753 | * Our character device read / write functions. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2754 | * |
Borislav Petkov | 3c98bf3 | 2008-02-06 02:57:53 +0100 | [diff] [blame] | 2755 | * The tape is optimized to maximize throughput when it is transferring an |
| 2756 | * integral number of the "continuous transfer limit", which is a parameter of |
| 2757 | * the specific tape (26kB on my particular tape, 32kB for Onstream). |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2758 | * |
Borislav Petkov | 3c98bf3 | 2008-02-06 02:57:53 +0100 | [diff] [blame] | 2759 | * As of version 1.3 of the driver, the character device provides an abstract |
| 2760 | * continuous view of the media - any mix of block sizes (even 1 byte) on the |
| 2761 | * same backup/restore procedure is supported. The driver will internally |
| 2762 | * convert the requests to the recommended transfer unit, so that an unmatch |
| 2763 | * between the user's block size to the recommended size will only result in a |
| 2764 | * (slightly) increased driver overhead, but will no longer hit performance. |
| 2765 | * This is not applicable to Onstream. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2766 | */ |
Borislav Petkov | 5a04cfa | 2008-02-06 02:57:54 +0100 | [diff] [blame] | 2767 | static ssize_t idetape_chrdev_read(struct file *file, char __user *buf, |
| 2768 | size_t count, loff_t *ppos) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2769 | { |
| 2770 | struct ide_tape_obj *tape = ide_tape_f(file); |
| 2771 | ide_drive_t *drive = tape->drive; |
Borislav Petkov | 5a04cfa | 2008-02-06 02:57:54 +0100 | [diff] [blame] | 2772 | ssize_t bytes_read, temp, actually_read = 0, rc; |
Daniel Walker | dcd9637 | 2006-06-25 05:47:37 -0700 | [diff] [blame] | 2773 | ssize_t ret = 0; |
Borislav Petkov | b642201 | 2008-02-02 19:56:49 +0100 | [diff] [blame] | 2774 | u16 ctl = *(u16 *)&tape->caps[12]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2775 | |
Borislav Petkov | 8004a8c | 2008-02-06 02:57:51 +0100 | [diff] [blame] | 2776 | debug_log(DBG_CHRDEV, "Enter %s, count %Zd\n", __func__, count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2777 | |
Borislav Petkov | 54abf37 | 2008-02-06 02:57:52 +0100 | [diff] [blame] | 2778 | if (tape->chrdev_dir != IDETAPE_DIR_READ) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2779 | if (test_bit(IDETAPE_DETECT_BS, &tape->flags)) |
Borislav Petkov | 54bb207 | 2008-02-06 02:57:52 +0100 | [diff] [blame] | 2780 | if (count > tape->blk_size && |
| 2781 | (count % tape->blk_size) == 0) |
| 2782 | tape->user_bs_factor = count / tape->blk_size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2783 | } |
Borislav Petkov | 8d06bfa | 2008-02-06 02:57:53 +0100 | [diff] [blame] | 2784 | rc = idetape_init_read(drive, tape->max_stages); |
| 2785 | if (rc < 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2786 | return rc; |
| 2787 | if (count == 0) |
| 2788 | return (0); |
| 2789 | if (tape->merge_stage_size) { |
Borislav Petkov | 5a04cfa | 2008-02-06 02:57:54 +0100 | [diff] [blame] | 2790 | actually_read = min((unsigned int)(tape->merge_stage_size), |
| 2791 | (unsigned int)count); |
| 2792 | if (idetape_copy_stage_to_user(tape, buf, tape->merge_stage, |
| 2793 | actually_read)) |
Daniel Walker | dcd9637 | 2006-06-25 05:47:37 -0700 | [diff] [blame] | 2794 | ret = -EFAULT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2795 | buf += actually_read; |
| 2796 | tape->merge_stage_size -= actually_read; |
| 2797 | count -= actually_read; |
| 2798 | } |
| 2799 | while (count >= tape->stage_size) { |
Borislav Petkov | b642201 | 2008-02-02 19:56:49 +0100 | [diff] [blame] | 2800 | bytes_read = idetape_add_chrdev_read_request(drive, ctl); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2801 | if (bytes_read <= 0) |
| 2802 | goto finish; |
Borislav Petkov | 5a04cfa | 2008-02-06 02:57:54 +0100 | [diff] [blame] | 2803 | if (idetape_copy_stage_to_user(tape, buf, tape->merge_stage, |
| 2804 | bytes_read)) |
Daniel Walker | dcd9637 | 2006-06-25 05:47:37 -0700 | [diff] [blame] | 2805 | ret = -EFAULT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2806 | buf += bytes_read; |
| 2807 | count -= bytes_read; |
| 2808 | actually_read += bytes_read; |
| 2809 | } |
| 2810 | if (count) { |
Borislav Petkov | b642201 | 2008-02-02 19:56:49 +0100 | [diff] [blame] | 2811 | bytes_read = idetape_add_chrdev_read_request(drive, ctl); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2812 | if (bytes_read <= 0) |
| 2813 | goto finish; |
| 2814 | temp = min((unsigned long)count, (unsigned long)bytes_read); |
Borislav Petkov | 5a04cfa | 2008-02-06 02:57:54 +0100 | [diff] [blame] | 2815 | if (idetape_copy_stage_to_user(tape, buf, tape->merge_stage, |
| 2816 | temp)) |
Daniel Walker | dcd9637 | 2006-06-25 05:47:37 -0700 | [diff] [blame] | 2817 | ret = -EFAULT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2818 | actually_read += temp; |
| 2819 | tape->merge_stage_size = bytes_read-temp; |
| 2820 | } |
| 2821 | finish: |
| 2822 | if (!actually_read && test_bit(IDETAPE_FILEMARK, &tape->flags)) { |
Borislav Petkov | 8004a8c | 2008-02-06 02:57:51 +0100 | [diff] [blame] | 2823 | debug_log(DBG_SENSE, "%s: spacing over filemark\n", tape->name); |
| 2824 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2825 | idetape_space_over_filemarks(drive, MTFSF, 1); |
| 2826 | return 0; |
| 2827 | } |
Daniel Walker | dcd9637 | 2006-06-25 05:47:37 -0700 | [diff] [blame] | 2828 | |
Borislav Petkov | 5a04cfa | 2008-02-06 02:57:54 +0100 | [diff] [blame] | 2829 | return ret ? ret : actually_read; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2830 | } |
| 2831 | |
Borislav Petkov | 5a04cfa | 2008-02-06 02:57:54 +0100 | [diff] [blame] | 2832 | 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] | 2833 | size_t count, loff_t *ppos) |
| 2834 | { |
| 2835 | struct ide_tape_obj *tape = ide_tape_f(file); |
| 2836 | ide_drive_t *drive = tape->drive; |
Daniel Walker | dcd9637 | 2006-06-25 05:47:37 -0700 | [diff] [blame] | 2837 | ssize_t actually_written = 0; |
| 2838 | ssize_t ret = 0; |
Borislav Petkov | b642201 | 2008-02-02 19:56:49 +0100 | [diff] [blame] | 2839 | u16 ctl = *(u16 *)&tape->caps[12]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2840 | |
| 2841 | /* The drive is write protected. */ |
| 2842 | if (tape->write_prot) |
| 2843 | return -EACCES; |
| 2844 | |
Borislav Petkov | 8004a8c | 2008-02-06 02:57:51 +0100 | [diff] [blame] | 2845 | debug_log(DBG_CHRDEV, "Enter %s, count %Zd\n", __func__, count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2846 | |
| 2847 | /* Initialize write operation */ |
Borislav Petkov | 54abf37 | 2008-02-06 02:57:52 +0100 | [diff] [blame] | 2848 | if (tape->chrdev_dir != IDETAPE_DIR_WRITE) { |
| 2849 | if (tape->chrdev_dir == IDETAPE_DIR_READ) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2850 | idetape_discard_read_pipeline(drive, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2851 | if (tape->merge_stage || tape->merge_stage_size) { |
| 2852 | printk(KERN_ERR "ide-tape: merge_stage_size " |
| 2853 | "should be 0 now\n"); |
| 2854 | tape->merge_stage_size = 0; |
| 2855 | } |
Borislav Petkov | 5a04cfa | 2008-02-06 02:57:54 +0100 | [diff] [blame] | 2856 | tape->merge_stage = __idetape_kmalloc_stage(tape, 0, 0); |
| 2857 | if (!tape->merge_stage) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2858 | return -ENOMEM; |
Borislav Petkov | 54abf37 | 2008-02-06 02:57:52 +0100 | [diff] [blame] | 2859 | tape->chrdev_dir = IDETAPE_DIR_WRITE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2860 | idetape_init_merge_stage(tape); |
| 2861 | |
| 2862 | /* |
Borislav Petkov | 3c98bf3 | 2008-02-06 02:57:53 +0100 | [diff] [blame] | 2863 | * Issue a write 0 command to ensure that DSC handshake is |
| 2864 | * switched from completion mode to buffer available mode. No |
| 2865 | * point in issuing this if DSC overlap isn't supported, some |
| 2866 | * drives (Seagate STT3401A) will return an error. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2867 | */ |
| 2868 | if (drive->dsc_overlap) { |
Borislav Petkov | 5a04cfa | 2008-02-06 02:57:54 +0100 | [diff] [blame] | 2869 | ssize_t retval = idetape_queue_rw_tail(drive, |
| 2870 | REQ_IDETAPE_WRITE, 0, |
| 2871 | tape->merge_stage->bh); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2872 | if (retval < 0) { |
| 2873 | __idetape_kfree_stage(tape->merge_stage); |
| 2874 | tape->merge_stage = NULL; |
Borislav Petkov | 54abf37 | 2008-02-06 02:57:52 +0100 | [diff] [blame] | 2875 | tape->chrdev_dir = IDETAPE_DIR_NONE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2876 | return retval; |
| 2877 | } |
| 2878 | } |
| 2879 | } |
| 2880 | if (count == 0) |
| 2881 | return (0); |
| 2882 | if (tape->restart_speed_control_req) |
| 2883 | idetape_restart_speed_control(drive); |
| 2884 | if (tape->merge_stage_size) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2885 | if (tape->merge_stage_size >= tape->stage_size) { |
Borislav Petkov | 5a04cfa | 2008-02-06 02:57:54 +0100 | [diff] [blame] | 2886 | printk(KERN_ERR "ide-tape: bug: merge buf too big\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2887 | tape->merge_stage_size = 0; |
| 2888 | } |
Borislav Petkov | 5a04cfa | 2008-02-06 02:57:54 +0100 | [diff] [blame] | 2889 | actually_written = min((unsigned int) |
| 2890 | (tape->stage_size - tape->merge_stage_size), |
| 2891 | (unsigned int)count); |
| 2892 | if (idetape_copy_stage_from_user(tape, tape->merge_stage, buf, |
| 2893 | actually_written)) |
Daniel Walker | dcd9637 | 2006-06-25 05:47:37 -0700 | [diff] [blame] | 2894 | ret = -EFAULT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2895 | buf += actually_written; |
| 2896 | tape->merge_stage_size += actually_written; |
| 2897 | count -= actually_written; |
| 2898 | |
| 2899 | if (tape->merge_stage_size == tape->stage_size) { |
Daniel Walker | dcd9637 | 2006-06-25 05:47:37 -0700 | [diff] [blame] | 2900 | ssize_t retval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2901 | tape->merge_stage_size = 0; |
Borislav Petkov | b642201 | 2008-02-02 19:56:49 +0100 | [diff] [blame] | 2902 | retval = idetape_add_chrdev_write_request(drive, ctl); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2903 | if (retval <= 0) |
| 2904 | return (retval); |
| 2905 | } |
| 2906 | } |
| 2907 | while (count >= tape->stage_size) { |
Daniel Walker | dcd9637 | 2006-06-25 05:47:37 -0700 | [diff] [blame] | 2908 | ssize_t retval; |
Borislav Petkov | 5a04cfa | 2008-02-06 02:57:54 +0100 | [diff] [blame] | 2909 | if (idetape_copy_stage_from_user(tape, tape->merge_stage, buf, |
| 2910 | tape->stage_size)) |
Daniel Walker | dcd9637 | 2006-06-25 05:47:37 -0700 | [diff] [blame] | 2911 | ret = -EFAULT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2912 | buf += tape->stage_size; |
| 2913 | count -= tape->stage_size; |
Borislav Petkov | b642201 | 2008-02-02 19:56:49 +0100 | [diff] [blame] | 2914 | retval = idetape_add_chrdev_write_request(drive, ctl); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2915 | actually_written += tape->stage_size; |
| 2916 | if (retval <= 0) |
| 2917 | return (retval); |
| 2918 | } |
| 2919 | if (count) { |
| 2920 | actually_written += count; |
Borislav Petkov | 5a04cfa | 2008-02-06 02:57:54 +0100 | [diff] [blame] | 2921 | if (idetape_copy_stage_from_user(tape, tape->merge_stage, buf, |
| 2922 | count)) |
Daniel Walker | dcd9637 | 2006-06-25 05:47:37 -0700 | [diff] [blame] | 2923 | ret = -EFAULT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2924 | tape->merge_stage_size += count; |
| 2925 | } |
Borislav Petkov | 5a04cfa | 2008-02-06 02:57:54 +0100 | [diff] [blame] | 2926 | return ret ? ret : actually_written; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2927 | } |
| 2928 | |
Borislav Petkov | 5a04cfa | 2008-02-06 02:57:54 +0100 | [diff] [blame] | 2929 | static int idetape_write_filemark(ide_drive_t *drive) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2930 | { |
| 2931 | idetape_pc_t pc; |
| 2932 | |
| 2933 | /* Write a filemark */ |
| 2934 | idetape_create_write_filemark_cmd(drive, &pc, 1); |
| 2935 | if (idetape_queue_pc_tail(drive, &pc)) { |
| 2936 | printk(KERN_ERR "ide-tape: Couldn't write a filemark\n"); |
| 2937 | return -EIO; |
| 2938 | } |
| 2939 | return 0; |
| 2940 | } |
| 2941 | |
| 2942 | /* |
Borislav Petkov | d99c9da | 2008-02-02 19:56:51 +0100 | [diff] [blame] | 2943 | * Called from idetape_chrdev_ioctl when the general mtio MTIOCTOP ioctl is |
| 2944 | * requested. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2945 | * |
Borislav Petkov | d99c9da | 2008-02-02 19:56:51 +0100 | [diff] [blame] | 2946 | * Note: MTBSF and MTBSFM are not supported when the tape doesn't support |
| 2947 | * spacing over filemarks in the reverse direction. In this case, MTFSFM is also |
| 2948 | * usually not supported (it is supported in the rare case in which we crossed |
| 2949 | * the filemark during our read-ahead pipelined operation mode). |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2950 | * |
Borislav Petkov | d99c9da | 2008-02-02 19:56:51 +0100 | [diff] [blame] | 2951 | * The following commands are currently not supported: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2952 | * |
Borislav Petkov | d99c9da | 2008-02-02 19:56:51 +0100 | [diff] [blame] | 2953 | * MTFSS, MTBSS, MTWSM, MTSETDENSITY, MTSETDRVBUFFER, MT_ST_BOOLEANS, |
| 2954 | * MT_ST_WRITE_THRESHOLD. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2955 | */ |
Borislav Petkov | d99c9da | 2008-02-02 19:56:51 +0100 | [diff] [blame] | 2956 | 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] | 2957 | { |
| 2958 | idetape_tape_t *tape = drive->driver_data; |
| 2959 | idetape_pc_t pc; |
Borislav Petkov | 5a04cfa | 2008-02-06 02:57:54 +0100 | [diff] [blame] | 2960 | int i, retval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2961 | |
Borislav Petkov | 8004a8c | 2008-02-06 02:57:51 +0100 | [diff] [blame] | 2962 | debug_log(DBG_ERR, "Handling MTIOCTOP ioctl: mt_op=%d, mt_count=%d\n", |
| 2963 | mt_op, mt_count); |
Borislav Petkov | 5a04cfa | 2008-02-06 02:57:54 +0100 | [diff] [blame] | 2964 | |
Borislav Petkov | 3c98bf3 | 2008-02-06 02:57:53 +0100 | [diff] [blame] | 2965 | /* Commands which need our pipelined read-ahead stages. */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2966 | switch (mt_op) { |
Borislav Petkov | 5a04cfa | 2008-02-06 02:57:54 +0100 | [diff] [blame] | 2967 | case MTFSF: |
| 2968 | case MTFSFM: |
| 2969 | case MTBSF: |
| 2970 | case MTBSFM: |
| 2971 | if (!mt_count) |
| 2972 | return 0; |
| 2973 | return idetape_space_over_filemarks(drive, mt_op, mt_count); |
| 2974 | default: |
| 2975 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2976 | } |
Borislav Petkov | 5a04cfa | 2008-02-06 02:57:54 +0100 | [diff] [blame] | 2977 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2978 | switch (mt_op) { |
Borislav Petkov | 5a04cfa | 2008-02-06 02:57:54 +0100 | [diff] [blame] | 2979 | case MTWEOF: |
| 2980 | if (tape->write_prot) |
| 2981 | return -EACCES; |
| 2982 | idetape_discard_read_pipeline(drive, 1); |
| 2983 | for (i = 0; i < mt_count; i++) { |
| 2984 | retval = idetape_write_filemark(drive); |
| 2985 | if (retval) |
| 2986 | return retval; |
| 2987 | } |
| 2988 | return 0; |
| 2989 | case MTREW: |
| 2990 | idetape_discard_read_pipeline(drive, 0); |
| 2991 | if (idetape_rewind_tape(drive)) |
| 2992 | return -EIO; |
| 2993 | return 0; |
| 2994 | case MTLOAD: |
| 2995 | idetape_discard_read_pipeline(drive, 0); |
| 2996 | idetape_create_load_unload_cmd(drive, &pc, |
| 2997 | IDETAPE_LU_LOAD_MASK); |
| 2998 | return idetape_queue_pc_tail(drive, &pc); |
| 2999 | case MTUNLOAD: |
| 3000 | case MTOFFL: |
| 3001 | /* |
| 3002 | * If door is locked, attempt to unlock before |
| 3003 | * attempting to eject. |
| 3004 | */ |
| 3005 | if (tape->door_locked) { |
| 3006 | if (idetape_create_prevent_cmd(drive, &pc, 0)) |
| 3007 | if (!idetape_queue_pc_tail(drive, &pc)) |
| 3008 | tape->door_locked = DOOR_UNLOCKED; |
| 3009 | } |
| 3010 | idetape_discard_read_pipeline(drive, 0); |
| 3011 | idetape_create_load_unload_cmd(drive, &pc, |
| 3012 | !IDETAPE_LU_LOAD_MASK); |
| 3013 | retval = idetape_queue_pc_tail(drive, &pc); |
| 3014 | if (!retval) |
| 3015 | clear_bit(IDETAPE_MEDIUM_PRESENT, &tape->flags); |
| 3016 | return retval; |
| 3017 | case MTNOP: |
| 3018 | idetape_discard_read_pipeline(drive, 0); |
| 3019 | return idetape_flush_tape_buffers(drive); |
| 3020 | case MTRETEN: |
| 3021 | idetape_discard_read_pipeline(drive, 0); |
| 3022 | idetape_create_load_unload_cmd(drive, &pc, |
| 3023 | IDETAPE_LU_RETENSION_MASK | IDETAPE_LU_LOAD_MASK); |
| 3024 | return idetape_queue_pc_tail(drive, &pc); |
| 3025 | case MTEOM: |
| 3026 | idetape_create_space_cmd(&pc, 0, IDETAPE_SPACE_TO_EOD); |
| 3027 | return idetape_queue_pc_tail(drive, &pc); |
| 3028 | case MTERASE: |
| 3029 | (void)idetape_rewind_tape(drive); |
| 3030 | idetape_create_erase_cmd(&pc); |
| 3031 | return idetape_queue_pc_tail(drive, &pc); |
| 3032 | case MTSETBLK: |
| 3033 | if (mt_count) { |
| 3034 | if (mt_count < tape->blk_size || |
| 3035 | mt_count % tape->blk_size) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3036 | return -EIO; |
Borislav Petkov | 5a04cfa | 2008-02-06 02:57:54 +0100 | [diff] [blame] | 3037 | tape->user_bs_factor = mt_count / tape->blk_size; |
| 3038 | clear_bit(IDETAPE_DETECT_BS, &tape->flags); |
| 3039 | } else |
| 3040 | set_bit(IDETAPE_DETECT_BS, &tape->flags); |
| 3041 | return 0; |
| 3042 | case MTSEEK: |
| 3043 | idetape_discard_read_pipeline(drive, 0); |
| 3044 | return idetape_position_tape(drive, |
| 3045 | mt_count * tape->user_bs_factor, tape->partition, 0); |
| 3046 | case MTSETPART: |
| 3047 | idetape_discard_read_pipeline(drive, 0); |
| 3048 | return idetape_position_tape(drive, 0, mt_count, 0); |
| 3049 | case MTFSR: |
| 3050 | case MTBSR: |
| 3051 | case MTLOCK: |
| 3052 | if (!idetape_create_prevent_cmd(drive, &pc, 1)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3053 | return 0; |
Borislav Petkov | 5a04cfa | 2008-02-06 02:57:54 +0100 | [diff] [blame] | 3054 | retval = idetape_queue_pc_tail(drive, &pc); |
| 3055 | if (retval) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3056 | return retval; |
Borislav Petkov | 5a04cfa | 2008-02-06 02:57:54 +0100 | [diff] [blame] | 3057 | tape->door_locked = DOOR_EXPLICITLY_LOCKED; |
| 3058 | return 0; |
| 3059 | case MTUNLOCK: |
| 3060 | if (!idetape_create_prevent_cmd(drive, &pc, 0)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3061 | return 0; |
Borislav Petkov | 5a04cfa | 2008-02-06 02:57:54 +0100 | [diff] [blame] | 3062 | retval = idetape_queue_pc_tail(drive, &pc); |
| 3063 | if (retval) |
| 3064 | return retval; |
| 3065 | tape->door_locked = DOOR_UNLOCKED; |
| 3066 | return 0; |
| 3067 | default: |
| 3068 | printk(KERN_ERR "ide-tape: MTIO operation %d not supported\n", |
| 3069 | mt_op); |
| 3070 | return -EIO; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3071 | } |
| 3072 | } |
| 3073 | |
| 3074 | /* |
Borislav Petkov | d99c9da | 2008-02-02 19:56:51 +0100 | [diff] [blame] | 3075 | * Our character device ioctls. General mtio.h magnetic io commands are |
| 3076 | * supported here, and not in the corresponding block interface. Our own |
| 3077 | * ide-tape ioctls are supported on both interfaces. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3078 | */ |
Borislav Petkov | d99c9da | 2008-02-02 19:56:51 +0100 | [diff] [blame] | 3079 | static int idetape_chrdev_ioctl(struct inode *inode, struct file *file, |
| 3080 | unsigned int cmd, unsigned long arg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3081 | { |
| 3082 | struct ide_tape_obj *tape = ide_tape_f(file); |
| 3083 | ide_drive_t *drive = tape->drive; |
| 3084 | struct mtop mtop; |
| 3085 | struct mtget mtget; |
| 3086 | struct mtpos mtpos; |
Borislav Petkov | 54bb207 | 2008-02-06 02:57:52 +0100 | [diff] [blame] | 3087 | int block_offset = 0, position = tape->first_frame; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3088 | void __user *argp = (void __user *)arg; |
| 3089 | |
Borislav Petkov | 8004a8c | 2008-02-06 02:57:51 +0100 | [diff] [blame] | 3090 | debug_log(DBG_CHRDEV, "Enter %s, cmd=%u\n", __func__, cmd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3091 | |
| 3092 | tape->restart_speed_control_req = 1; |
Borislav Petkov | 54abf37 | 2008-02-06 02:57:52 +0100 | [diff] [blame] | 3093 | if (tape->chrdev_dir == IDETAPE_DIR_WRITE) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3094 | idetape_empty_write_pipeline(drive); |
| 3095 | idetape_flush_tape_buffers(drive); |
| 3096 | } |
| 3097 | if (cmd == MTIOCGET || cmd == MTIOCPOS) { |
Borislav Petkov | 54bb207 | 2008-02-06 02:57:52 +0100 | [diff] [blame] | 3098 | block_offset = idetape_pipeline_size(drive) / |
| 3099 | (tape->blk_size * tape->user_bs_factor); |
Borislav Petkov | 5a04cfa | 2008-02-06 02:57:54 +0100 | [diff] [blame] | 3100 | position = idetape_read_position(drive); |
| 3101 | if (position < 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3102 | return -EIO; |
| 3103 | } |
| 3104 | switch (cmd) { |
Borislav Petkov | 5a04cfa | 2008-02-06 02:57:54 +0100 | [diff] [blame] | 3105 | case MTIOCTOP: |
| 3106 | if (copy_from_user(&mtop, argp, sizeof(struct mtop))) |
| 3107 | return -EFAULT; |
| 3108 | return idetape_mtioctop(drive, mtop.mt_op, mtop.mt_count); |
| 3109 | case MTIOCGET: |
| 3110 | memset(&mtget, 0, sizeof(struct mtget)); |
| 3111 | mtget.mt_type = MT_ISSCSI2; |
| 3112 | mtget.mt_blkno = position / tape->user_bs_factor - block_offset; |
| 3113 | mtget.mt_dsreg = |
| 3114 | ((tape->blk_size * tape->user_bs_factor) |
| 3115 | << MT_ST_BLKSIZE_SHIFT) & MT_ST_BLKSIZE_MASK; |
Borislav Petkov | 54bb207 | 2008-02-06 02:57:52 +0100 | [diff] [blame] | 3116 | |
Borislav Petkov | 5a04cfa | 2008-02-06 02:57:54 +0100 | [diff] [blame] | 3117 | if (tape->drv_write_prot) |
| 3118 | mtget.mt_gstat |= GMT_WR_PROT(0xffffffff); |
| 3119 | |
| 3120 | if (copy_to_user(argp, &mtget, sizeof(struct mtget))) |
| 3121 | return -EFAULT; |
| 3122 | return 0; |
| 3123 | case MTIOCPOS: |
| 3124 | mtpos.mt_blkno = position / tape->user_bs_factor - block_offset; |
| 3125 | if (copy_to_user(argp, &mtpos, sizeof(struct mtpos))) |
| 3126 | return -EFAULT; |
| 3127 | return 0; |
| 3128 | default: |
| 3129 | if (tape->chrdev_dir == IDETAPE_DIR_READ) |
| 3130 | idetape_discard_read_pipeline(drive, 1); |
| 3131 | return idetape_blkdev_ioctl(drive, cmd, arg); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3132 | } |
| 3133 | } |
| 3134 | |
Borislav Petkov | 3cffb9c | 2008-02-02 19:56:50 +0100 | [diff] [blame] | 3135 | /* |
| 3136 | * Do a mode sense page 0 with block descriptor and if it succeeds set the tape |
| 3137 | * block size with the reported value. |
| 3138 | */ |
| 3139 | static void ide_tape_get_bsize_from_bdesc(ide_drive_t *drive) |
| 3140 | { |
| 3141 | idetape_tape_t *tape = drive->driver_data; |
| 3142 | idetape_pc_t pc; |
| 3143 | |
| 3144 | idetape_create_mode_sense_cmd(&pc, IDETAPE_BLOCK_DESCRIPTOR); |
| 3145 | if (idetape_queue_pc_tail(drive, &pc)) { |
| 3146 | printk(KERN_ERR "ide-tape: Can't get block descriptor\n"); |
Borislav Petkov | 54bb207 | 2008-02-06 02:57:52 +0100 | [diff] [blame] | 3147 | if (tape->blk_size == 0) { |
Borislav Petkov | 3cffb9c | 2008-02-02 19:56:50 +0100 | [diff] [blame] | 3148 | printk(KERN_WARNING "ide-tape: Cannot deal with zero " |
| 3149 | "block size, assuming 32k\n"); |
Borislav Petkov | 54bb207 | 2008-02-06 02:57:52 +0100 | [diff] [blame] | 3150 | tape->blk_size = 32768; |
Borislav Petkov | 3cffb9c | 2008-02-02 19:56:50 +0100 | [diff] [blame] | 3151 | } |
| 3152 | return; |
| 3153 | } |
Borislav Petkov | 54bb207 | 2008-02-06 02:57:52 +0100 | [diff] [blame] | 3154 | tape->blk_size = (pc.buffer[4 + 5] << 16) + |
Borislav Petkov | 3cffb9c | 2008-02-02 19:56:50 +0100 | [diff] [blame] | 3155 | (pc.buffer[4 + 6] << 8) + |
| 3156 | pc.buffer[4 + 7]; |
| 3157 | tape->drv_write_prot = (pc.buffer[2] & 0x80) >> 7; |
| 3158 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3159 | |
Borislav Petkov | 5a04cfa | 2008-02-06 02:57:54 +0100 | [diff] [blame] | 3160 | static int idetape_chrdev_open(struct inode *inode, struct file *filp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3161 | { |
| 3162 | unsigned int minor = iminor(inode), i = minor & ~0xc0; |
| 3163 | ide_drive_t *drive; |
| 3164 | idetape_tape_t *tape; |
| 3165 | idetape_pc_t pc; |
| 3166 | int retval; |
| 3167 | |
Borislav Petkov | 8004a8c | 2008-02-06 02:57:51 +0100 | [diff] [blame] | 3168 | if (i >= MAX_HWIFS * MAX_DRIVES) |
| 3169 | return -ENXIO; |
| 3170 | |
| 3171 | tape = ide_tape_chrdev_get(i); |
| 3172 | if (!tape) |
| 3173 | return -ENXIO; |
| 3174 | |
| 3175 | debug_log(DBG_CHRDEV, "Enter %s\n", __func__); |
| 3176 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3177 | /* |
| 3178 | * We really want to do nonseekable_open(inode, filp); here, but some |
| 3179 | * versions of tar incorrectly call lseek on tapes and bail out if that |
| 3180 | * fails. So we disallow pread() and pwrite(), but permit lseeks. |
| 3181 | */ |
| 3182 | filp->f_mode &= ~(FMODE_PREAD | FMODE_PWRITE); |
| 3183 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3184 | drive = tape->drive; |
| 3185 | |
| 3186 | filp->private_data = tape; |
| 3187 | |
| 3188 | if (test_and_set_bit(IDETAPE_BUSY, &tape->flags)) { |
| 3189 | retval = -EBUSY; |
| 3190 | goto out_put_tape; |
| 3191 | } |
| 3192 | |
| 3193 | retval = idetape_wait_ready(drive, 60 * HZ); |
| 3194 | if (retval) { |
| 3195 | clear_bit(IDETAPE_BUSY, &tape->flags); |
| 3196 | printk(KERN_ERR "ide-tape: %s: drive not ready\n", tape->name); |
| 3197 | goto out_put_tape; |
| 3198 | } |
| 3199 | |
| 3200 | idetape_read_position(drive); |
| 3201 | if (!test_bit(IDETAPE_ADDRESS_VALID, &tape->flags)) |
| 3202 | (void)idetape_rewind_tape(drive); |
| 3203 | |
Borislav Petkov | 54abf37 | 2008-02-06 02:57:52 +0100 | [diff] [blame] | 3204 | if (tape->chrdev_dir != IDETAPE_DIR_READ) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3205 | clear_bit(IDETAPE_PIPELINE_ERROR, &tape->flags); |
| 3206 | |
| 3207 | /* Read block size and write protect status from drive. */ |
Borislav Petkov | 3cffb9c | 2008-02-02 19:56:50 +0100 | [diff] [blame] | 3208 | ide_tape_get_bsize_from_bdesc(drive); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3209 | |
| 3210 | /* Set write protect flag if device is opened as read-only. */ |
| 3211 | if ((filp->f_flags & O_ACCMODE) == O_RDONLY) |
| 3212 | tape->write_prot = 1; |
| 3213 | else |
| 3214 | tape->write_prot = tape->drv_write_prot; |
| 3215 | |
| 3216 | /* Make sure drive isn't write protected if user wants to write. */ |
| 3217 | if (tape->write_prot) { |
| 3218 | if ((filp->f_flags & O_ACCMODE) == O_WRONLY || |
| 3219 | (filp->f_flags & O_ACCMODE) == O_RDWR) { |
| 3220 | clear_bit(IDETAPE_BUSY, &tape->flags); |
| 3221 | retval = -EROFS; |
| 3222 | goto out_put_tape; |
| 3223 | } |
| 3224 | } |
| 3225 | |
Borislav Petkov | 3c98bf3 | 2008-02-06 02:57:53 +0100 | [diff] [blame] | 3226 | /* Lock the tape drive door so user can't eject. */ |
Borislav Petkov | 54abf37 | 2008-02-06 02:57:52 +0100 | [diff] [blame] | 3227 | if (tape->chrdev_dir == IDETAPE_DIR_NONE) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3228 | if (idetape_create_prevent_cmd(drive, &pc, 1)) { |
| 3229 | if (!idetape_queue_pc_tail(drive, &pc)) { |
| 3230 | if (tape->door_locked != DOOR_EXPLICITLY_LOCKED) |
| 3231 | tape->door_locked = DOOR_LOCKED; |
| 3232 | } |
| 3233 | } |
| 3234 | } |
| 3235 | idetape_restart_speed_control(drive); |
| 3236 | tape->restart_speed_control_req = 0; |
| 3237 | return 0; |
| 3238 | |
| 3239 | out_put_tape: |
| 3240 | ide_tape_put(tape); |
| 3241 | return retval; |
| 3242 | } |
| 3243 | |
Borislav Petkov | 5a04cfa | 2008-02-06 02:57:54 +0100 | [diff] [blame] | 3244 | static void idetape_write_release(ide_drive_t *drive, unsigned int minor) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3245 | { |
| 3246 | idetape_tape_t *tape = drive->driver_data; |
| 3247 | |
| 3248 | idetape_empty_write_pipeline(drive); |
| 3249 | tape->merge_stage = __idetape_kmalloc_stage(tape, 1, 0); |
| 3250 | if (tape->merge_stage != NULL) { |
Borislav Petkov | 54bb207 | 2008-02-06 02:57:52 +0100 | [diff] [blame] | 3251 | idetape_pad_zeros(drive, tape->blk_size * |
| 3252 | (tape->user_bs_factor - 1)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3253 | __idetape_kfree_stage(tape->merge_stage); |
| 3254 | tape->merge_stage = NULL; |
| 3255 | } |
| 3256 | idetape_write_filemark(drive); |
| 3257 | idetape_flush_tape_buffers(drive); |
| 3258 | idetape_flush_tape_buffers(drive); |
| 3259 | } |
| 3260 | |
Borislav Petkov | 5a04cfa | 2008-02-06 02:57:54 +0100 | [diff] [blame] | 3261 | static int idetape_chrdev_release(struct inode *inode, struct file *filp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3262 | { |
| 3263 | struct ide_tape_obj *tape = ide_tape_f(filp); |
| 3264 | ide_drive_t *drive = tape->drive; |
| 3265 | idetape_pc_t pc; |
| 3266 | unsigned int minor = iminor(inode); |
| 3267 | |
| 3268 | lock_kernel(); |
| 3269 | tape = drive->driver_data; |
Borislav Petkov | 8004a8c | 2008-02-06 02:57:51 +0100 | [diff] [blame] | 3270 | |
| 3271 | debug_log(DBG_CHRDEV, "Enter %s\n", __func__); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3272 | |
Borislav Petkov | 54abf37 | 2008-02-06 02:57:52 +0100 | [diff] [blame] | 3273 | if (tape->chrdev_dir == IDETAPE_DIR_WRITE) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3274 | idetape_write_release(drive, minor); |
Borislav Petkov | 54abf37 | 2008-02-06 02:57:52 +0100 | [diff] [blame] | 3275 | if (tape->chrdev_dir == IDETAPE_DIR_READ) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3276 | if (minor < 128) |
| 3277 | idetape_discard_read_pipeline(drive, 1); |
| 3278 | else |
| 3279 | idetape_wait_for_pipeline(drive); |
| 3280 | } |
| 3281 | if (tape->cache_stage != NULL) { |
| 3282 | __idetape_kfree_stage(tape->cache_stage); |
| 3283 | tape->cache_stage = NULL; |
| 3284 | } |
| 3285 | if (minor < 128 && test_bit(IDETAPE_MEDIUM_PRESENT, &tape->flags)) |
| 3286 | (void) idetape_rewind_tape(drive); |
Borislav Petkov | 54abf37 | 2008-02-06 02:57:52 +0100 | [diff] [blame] | 3287 | if (tape->chrdev_dir == IDETAPE_DIR_NONE) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3288 | if (tape->door_locked == DOOR_LOCKED) { |
| 3289 | if (idetape_create_prevent_cmd(drive, &pc, 0)) { |
| 3290 | if (!idetape_queue_pc_tail(drive, &pc)) |
| 3291 | tape->door_locked = DOOR_UNLOCKED; |
| 3292 | } |
| 3293 | } |
| 3294 | } |
| 3295 | clear_bit(IDETAPE_BUSY, &tape->flags); |
| 3296 | ide_tape_put(tape); |
| 3297 | unlock_kernel(); |
| 3298 | return 0; |
| 3299 | } |
| 3300 | |
| 3301 | /* |
Borislav Petkov | 71071b8 | 2008-02-06 02:57:53 +0100 | [diff] [blame] | 3302 | * check the contents of the ATAPI IDENTIFY command results. We return: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3303 | * |
Borislav Petkov | 71071b8 | 2008-02-06 02:57:53 +0100 | [diff] [blame] | 3304 | * 1 - If the tape can be supported by us, based on the information we have so |
| 3305 | * far. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3306 | * |
Borislav Petkov | 71071b8 | 2008-02-06 02:57:53 +0100 | [diff] [blame] | 3307 | * 0 - If this tape driver is not currently supported by us. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3308 | */ |
Borislav Petkov | 71071b8 | 2008-02-06 02:57:53 +0100 | [diff] [blame] | 3309 | static int idetape_identify_device(ide_drive_t *drive) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3310 | { |
Borislav Petkov | 71071b8 | 2008-02-06 02:57:53 +0100 | [diff] [blame] | 3311 | u8 gcw[2], protocol, device_type, removable, packet_size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3312 | |
| 3313 | if (drive->id_read == 0) |
| 3314 | return 1; |
| 3315 | |
Borislav Petkov | 71071b8 | 2008-02-06 02:57:53 +0100 | [diff] [blame] | 3316 | *((unsigned short *) &gcw) = drive->id->config; |
| 3317 | |
| 3318 | protocol = (gcw[1] & 0xC0) >> 6; |
| 3319 | device_type = gcw[1] & 0x1F; |
| 3320 | removable = !!(gcw[0] & 0x80); |
| 3321 | packet_size = gcw[0] & 0x3; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3322 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3323 | /* Check that we can support this device */ |
Borislav Petkov | 71071b8 | 2008-02-06 02:57:53 +0100 | [diff] [blame] | 3324 | if (protocol != 2) |
Bartlomiej Zolnierkiewicz | 16422de3 | 2008-02-02 19:56:48 +0100 | [diff] [blame] | 3325 | printk(KERN_ERR "ide-tape: Protocol (0x%02x) is not ATAPI\n", |
Borislav Petkov | 71071b8 | 2008-02-06 02:57:53 +0100 | [diff] [blame] | 3326 | protocol); |
| 3327 | else if (device_type != 1) |
Bartlomiej Zolnierkiewicz | 16422de3 | 2008-02-02 19:56:48 +0100 | [diff] [blame] | 3328 | printk(KERN_ERR "ide-tape: Device type (0x%02x) is not set " |
Borislav Petkov | 71071b8 | 2008-02-06 02:57:53 +0100 | [diff] [blame] | 3329 | "to tape\n", device_type); |
| 3330 | else if (!removable) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3331 | printk(KERN_ERR "ide-tape: The removable flag is not set\n"); |
Borislav Petkov | 71071b8 | 2008-02-06 02:57:53 +0100 | [diff] [blame] | 3332 | else if (packet_size != 0) { |
Borislav Petkov | 24d57f8 | 2008-02-06 02:57:54 +0100 | [diff] [blame] | 3333 | printk(KERN_ERR "ide-tape: Packet size (0x%02x) is not 12" |
| 3334 | " bytes\n", packet_size); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3335 | } else |
| 3336 | return 1; |
| 3337 | return 0; |
| 3338 | } |
| 3339 | |
Borislav Petkov | 6d29c8f | 2008-02-02 19:56:49 +0100 | [diff] [blame] | 3340 | static void idetape_get_inquiry_results(ide_drive_t *drive) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3341 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3342 | idetape_tape_t *tape = drive->driver_data; |
| 3343 | idetape_pc_t pc; |
Borislav Petkov | 41f81d54 | 2008-02-06 02:57:52 +0100 | [diff] [blame] | 3344 | char fw_rev[6], vendor_id[10], product_id[18]; |
Borislav Petkov | 6d29c8f | 2008-02-02 19:56:49 +0100 | [diff] [blame] | 3345 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3346 | idetape_create_inquiry_cmd(&pc); |
| 3347 | if (idetape_queue_pc_tail(drive, &pc)) { |
Borislav Petkov | 6d29c8f | 2008-02-02 19:56:49 +0100 | [diff] [blame] | 3348 | printk(KERN_ERR "ide-tape: %s: can't get INQUIRY results\n", |
| 3349 | tape->name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3350 | return; |
| 3351 | } |
Borislav Petkov | 41f81d54 | 2008-02-06 02:57:52 +0100 | [diff] [blame] | 3352 | memcpy(vendor_id, &pc.buffer[8], 8); |
| 3353 | memcpy(product_id, &pc.buffer[16], 16); |
| 3354 | memcpy(fw_rev, &pc.buffer[32], 4); |
Borislav Petkov | 6d29c8f | 2008-02-02 19:56:49 +0100 | [diff] [blame] | 3355 | |
Borislav Petkov | 41f81d54 | 2008-02-06 02:57:52 +0100 | [diff] [blame] | 3356 | ide_fixstring(vendor_id, 10, 0); |
| 3357 | ide_fixstring(product_id, 18, 0); |
| 3358 | ide_fixstring(fw_rev, 6, 0); |
| 3359 | |
Borislav Petkov | 6d29c8f | 2008-02-02 19:56:49 +0100 | [diff] [blame] | 3360 | printk(KERN_INFO "ide-tape: %s <-> %s: %s %s rev %s\n", |
Borislav Petkov | 41f81d54 | 2008-02-06 02:57:52 +0100 | [diff] [blame] | 3361 | drive->name, tape->name, vendor_id, product_id, fw_rev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3362 | } |
| 3363 | |
| 3364 | /* |
Borislav Petkov | b642201 | 2008-02-02 19:56:49 +0100 | [diff] [blame] | 3365 | * Ask the tape about its various parameters. In particular, we will adjust our |
| 3366 | * 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] | 3367 | */ |
Borislav Petkov | 5a04cfa | 2008-02-06 02:57:54 +0100 | [diff] [blame] | 3368 | static void idetape_get_mode_sense_results(ide_drive_t *drive) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3369 | { |
| 3370 | idetape_tape_t *tape = drive->driver_data; |
| 3371 | idetape_pc_t pc; |
Borislav Petkov | b642201 | 2008-02-02 19:56:49 +0100 | [diff] [blame] | 3372 | u8 *caps; |
| 3373 | u8 speed, max_speed; |
Borislav Petkov | 47314fa | 2008-02-02 19:56:48 +0100 | [diff] [blame] | 3374 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3375 | idetape_create_mode_sense_cmd(&pc, IDETAPE_CAPABILITIES_PAGE); |
| 3376 | if (idetape_queue_pc_tail(drive, &pc)) { |
Borislav Petkov | b642201 | 2008-02-02 19:56:49 +0100 | [diff] [blame] | 3377 | printk(KERN_ERR "ide-tape: Can't get tape parameters - assuming" |
| 3378 | " some default values\n"); |
Borislav Petkov | 54bb207 | 2008-02-06 02:57:52 +0100 | [diff] [blame] | 3379 | tape->blk_size = 512; |
Borislav Petkov | b642201 | 2008-02-02 19:56:49 +0100 | [diff] [blame] | 3380 | put_unaligned(52, (u16 *)&tape->caps[12]); |
| 3381 | put_unaligned(540, (u16 *)&tape->caps[14]); |
| 3382 | put_unaligned(6*52, (u16 *)&tape->caps[16]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3383 | return; |
| 3384 | } |
Borislav Petkov | b642201 | 2008-02-02 19:56:49 +0100 | [diff] [blame] | 3385 | caps = pc.buffer + 4 + pc.buffer[3]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3386 | |
Borislav Petkov | b642201 | 2008-02-02 19:56:49 +0100 | [diff] [blame] | 3387 | /* convert to host order and save for later use */ |
| 3388 | speed = be16_to_cpu(*(u16 *)&caps[14]); |
| 3389 | max_speed = be16_to_cpu(*(u16 *)&caps[8]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3390 | |
Borislav Petkov | b642201 | 2008-02-02 19:56:49 +0100 | [diff] [blame] | 3391 | put_unaligned(max_speed, (u16 *)&caps[8]); |
| 3392 | put_unaligned(be16_to_cpu(*(u16 *)&caps[12]), (u16 *)&caps[12]); |
| 3393 | put_unaligned(speed, (u16 *)&caps[14]); |
| 3394 | put_unaligned(be16_to_cpu(*(u16 *)&caps[16]), (u16 *)&caps[16]); |
| 3395 | |
| 3396 | if (!speed) { |
| 3397 | printk(KERN_INFO "ide-tape: %s: invalid tape speed " |
| 3398 | "(assuming 650KB/sec)\n", drive->name); |
| 3399 | put_unaligned(650, (u16 *)&caps[14]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3400 | } |
Borislav Petkov | b642201 | 2008-02-02 19:56:49 +0100 | [diff] [blame] | 3401 | if (!max_speed) { |
| 3402 | printk(KERN_INFO "ide-tape: %s: invalid max_speed " |
| 3403 | "(assuming 650KB/sec)\n", drive->name); |
| 3404 | put_unaligned(650, (u16 *)&caps[8]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3405 | } |
| 3406 | |
Borislav Petkov | b642201 | 2008-02-02 19:56:49 +0100 | [diff] [blame] | 3407 | memcpy(&tape->caps, caps, 20); |
| 3408 | if (caps[7] & 0x02) |
Borislav Petkov | 54bb207 | 2008-02-06 02:57:52 +0100 | [diff] [blame] | 3409 | tape->blk_size = 512; |
Borislav Petkov | b642201 | 2008-02-02 19:56:49 +0100 | [diff] [blame] | 3410 | else if (caps[7] & 0x04) |
Borislav Petkov | 54bb207 | 2008-02-06 02:57:52 +0100 | [diff] [blame] | 3411 | tape->blk_size = 1024; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3412 | } |
| 3413 | |
Bartlomiej Zolnierkiewicz | 7662d04 | 2007-05-10 00:01:10 +0200 | [diff] [blame] | 3414 | #ifdef CONFIG_IDE_PROC_FS |
Borislav Petkov | 5a04cfa | 2008-02-06 02:57:54 +0100 | [diff] [blame] | 3415 | static void idetape_add_settings(ide_drive_t *drive) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3416 | { |
| 3417 | idetape_tape_t *tape = drive->driver_data; |
| 3418 | |
Borislav Petkov | b642201 | 2008-02-02 19:56:49 +0100 | [diff] [blame] | 3419 | ide_add_setting(drive, "buffer", SETTING_READ, TYPE_SHORT, 0, 0xffff, |
| 3420 | 1, 2, (u16 *)&tape->caps[16], NULL); |
Borislav Petkov | 5a04cfa | 2008-02-06 02:57:54 +0100 | [diff] [blame] | 3421 | ide_add_setting(drive, "pipeline_min", SETTING_RW, TYPE_INT, 1, 0xffff, |
| 3422 | tape->stage_size / 1024, 1, &tape->min_pipeline, NULL); |
| 3423 | ide_add_setting(drive, "pipeline", SETTING_RW, TYPE_INT, 1, 0xffff, |
| 3424 | tape->stage_size / 1024, 1, &tape->max_stages, NULL); |
| 3425 | ide_add_setting(drive, "pipeline_max", SETTING_RW, TYPE_INT, 1, 0xffff, |
| 3426 | tape->stage_size / 1024, 1, &tape->max_pipeline, NULL); |
| 3427 | ide_add_setting(drive, "pipeline_used", SETTING_READ, TYPE_INT, 0, |
| 3428 | 0xffff, tape->stage_size / 1024, 1, &tape->nr_stages, |
| 3429 | NULL); |
| 3430 | ide_add_setting(drive, "pipeline_pending", SETTING_READ, TYPE_INT, 0, |
| 3431 | 0xffff, tape->stage_size / 1024, 1, |
| 3432 | &tape->nr_pending_stages, NULL); |
Borislav Petkov | b642201 | 2008-02-02 19:56:49 +0100 | [diff] [blame] | 3433 | ide_add_setting(drive, "speed", SETTING_READ, TYPE_SHORT, 0, 0xffff, |
| 3434 | 1, 1, (u16 *)&tape->caps[14], NULL); |
Borislav Petkov | 54bb207 | 2008-02-06 02:57:52 +0100 | [diff] [blame] | 3435 | ide_add_setting(drive, "stage", SETTING_READ, TYPE_INT, 0, 0xffff, 1, |
| 3436 | 1024, &tape->stage_size, NULL); |
| 3437 | ide_add_setting(drive, "tdsc", SETTING_RW, TYPE_INT, IDETAPE_DSC_RW_MIN, |
| 3438 | IDETAPE_DSC_RW_MAX, 1000, HZ, &tape->best_dsc_rw_freq, |
| 3439 | NULL); |
Borislav Petkov | 5a04cfa | 2008-02-06 02:57:54 +0100 | [diff] [blame] | 3440 | ide_add_setting(drive, "dsc_overlap", SETTING_RW, TYPE_BYTE, 0, 1, 1, |
| 3441 | 1, &drive->dsc_overlap, NULL); |
| 3442 | ide_add_setting(drive, "pipeline_head_speed_c", SETTING_READ, TYPE_INT, |
| 3443 | 0, 0xffff, 1, 1, &tape->controlled_pipeline_head_speed, |
| 3444 | NULL); |
| 3445 | ide_add_setting(drive, "pipeline_head_speed_u", SETTING_READ, TYPE_INT, |
| 3446 | 0, 0xffff, 1, 1, |
| 3447 | &tape->uncontrolled_pipeline_head_speed, NULL); |
| 3448 | ide_add_setting(drive, "avg_speed", SETTING_READ, TYPE_INT, 0, 0xffff, |
| 3449 | 1, 1, &tape->avg_speed, NULL); |
Borislav Petkov | 8004a8c | 2008-02-06 02:57:51 +0100 | [diff] [blame] | 3450 | ide_add_setting(drive, "debug_mask", SETTING_RW, TYPE_INT, 0, 0xffff, 1, |
| 3451 | 1, &tape->debug_mask, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3452 | } |
Bartlomiej Zolnierkiewicz | 7662d04 | 2007-05-10 00:01:10 +0200 | [diff] [blame] | 3453 | #else |
| 3454 | static inline void idetape_add_settings(ide_drive_t *drive) { ; } |
| 3455 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3456 | |
| 3457 | /* |
Borislav Petkov | 3c98bf3 | 2008-02-06 02:57:53 +0100 | [diff] [blame] | 3458 | * The function below is called to: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3459 | * |
Borislav Petkov | 3c98bf3 | 2008-02-06 02:57:53 +0100 | [diff] [blame] | 3460 | * 1. Initialize our various state variables. |
| 3461 | * 2. Ask the tape for its capabilities. |
| 3462 | * 3. Allocate a buffer which will be used for data transfer. The buffer size |
| 3463 | * is chosen based on the recommendation which we received in step 2. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3464 | * |
Borislav Petkov | 3c98bf3 | 2008-02-06 02:57:53 +0100 | [diff] [blame] | 3465 | * Note that at this point ide.c already assigned us an irq, so that we can |
| 3466 | * queue requests here and wait for their completion. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3467 | */ |
Borislav Petkov | 5a04cfa | 2008-02-06 02:57:54 +0100 | [diff] [blame] | 3468 | 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] | 3469 | { |
| 3470 | unsigned long t1, tmid, tn, t; |
| 3471 | int speed; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3472 | int stage_size; |
Borislav Petkov | 71071b8 | 2008-02-06 02:57:53 +0100 | [diff] [blame] | 3473 | u8 gcw[2]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3474 | struct sysinfo si; |
Borislav Petkov | b642201 | 2008-02-02 19:56:49 +0100 | [diff] [blame] | 3475 | u16 *ctl = (u16 *)&tape->caps[12]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3476 | |
Borislav Petkov | 54bb207 | 2008-02-06 02:57:52 +0100 | [diff] [blame] | 3477 | spin_lock_init(&tape->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3478 | drive->dsc_overlap = 1; |
Bartlomiej Zolnierkiewicz | 4166c19 | 2008-02-01 23:09:30 +0100 | [diff] [blame] | 3479 | if (drive->hwif->host_flags & IDE_HFLAG_NO_DSC) { |
| 3480 | printk(KERN_INFO "ide-tape: %s: disabling DSC overlap\n", |
| 3481 | tape->name); |
| 3482 | drive->dsc_overlap = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3483 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3484 | /* Seagate Travan drives do not support DSC overlap. */ |
| 3485 | if (strstr(drive->id->model, "Seagate STT3401")) |
| 3486 | drive->dsc_overlap = 0; |
| 3487 | tape->minor = minor; |
| 3488 | tape->name[0] = 'h'; |
| 3489 | tape->name[1] = 't'; |
| 3490 | tape->name[2] = '0' + minor; |
Borislav Petkov | 54abf37 | 2008-02-06 02:57:52 +0100 | [diff] [blame] | 3491 | tape->chrdev_dir = IDETAPE_DIR_NONE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3492 | tape->pc = tape->pc_stack; |
| 3493 | tape->max_insert_speed = 10000; |
| 3494 | tape->speed_control = 1; |
| 3495 | *((unsigned short *) &gcw) = drive->id->config; |
Borislav Petkov | 71071b8 | 2008-02-06 02:57:53 +0100 | [diff] [blame] | 3496 | |
| 3497 | /* Command packet DRQ type */ |
| 3498 | if (((gcw[0] & 0x60) >> 5) == 1) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3499 | set_bit(IDETAPE_DRQ_INTERRUPT, &tape->flags); |
| 3500 | |
Borislav Petkov | 5a04cfa | 2008-02-06 02:57:54 +0100 | [diff] [blame] | 3501 | tape->min_pipeline = 10; |
| 3502 | tape->max_pipeline = 10; |
| 3503 | tape->max_stages = 10; |
Borislav Petkov | 3c98bf3 | 2008-02-06 02:57:53 +0100 | [diff] [blame] | 3504 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3505 | idetape_get_inquiry_results(drive); |
| 3506 | idetape_get_mode_sense_results(drive); |
Borislav Petkov | 3cffb9c | 2008-02-02 19:56:50 +0100 | [diff] [blame] | 3507 | ide_tape_get_bsize_from_bdesc(drive); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3508 | tape->user_bs_factor = 1; |
Borislav Petkov | 54bb207 | 2008-02-06 02:57:52 +0100 | [diff] [blame] | 3509 | tape->stage_size = *ctl * tape->blk_size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3510 | while (tape->stage_size > 0xffff) { |
| 3511 | printk(KERN_NOTICE "ide-tape: decreasing stage size\n"); |
Borislav Petkov | b642201 | 2008-02-02 19:56:49 +0100 | [diff] [blame] | 3512 | *ctl /= 2; |
Borislav Petkov | 54bb207 | 2008-02-06 02:57:52 +0100 | [diff] [blame] | 3513 | tape->stage_size = *ctl * tape->blk_size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3514 | } |
| 3515 | stage_size = tape->stage_size; |
| 3516 | tape->pages_per_stage = stage_size / PAGE_SIZE; |
| 3517 | if (stage_size % PAGE_SIZE) { |
| 3518 | tape->pages_per_stage++; |
| 3519 | tape->excess_bh_size = PAGE_SIZE - stage_size % PAGE_SIZE; |
| 3520 | } |
| 3521 | |
Borislav Petkov | b642201 | 2008-02-02 19:56:49 +0100 | [diff] [blame] | 3522 | /* Select the "best" DSC read/write polling freq and pipeline size. */ |
| 3523 | speed = max(*(u16 *)&tape->caps[14], *(u16 *)&tape->caps[8]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3524 | |
| 3525 | tape->max_stages = speed * 1000 * 10 / tape->stage_size; |
| 3526 | |
Borislav Petkov | 3c98bf3 | 2008-02-06 02:57:53 +0100 | [diff] [blame] | 3527 | /* Limit memory use for pipeline to 10% of physical memory */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3528 | si_meminfo(&si); |
Borislav Petkov | 5a04cfa | 2008-02-06 02:57:54 +0100 | [diff] [blame] | 3529 | if (tape->max_stages * tape->stage_size > |
| 3530 | si.totalram * si.mem_unit / 10) |
| 3531 | tape->max_stages = |
| 3532 | si.totalram * si.mem_unit / (10 * tape->stage_size); |
| 3533 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3534 | tape->max_stages = min(tape->max_stages, IDETAPE_MAX_PIPELINE_STAGES); |
| 3535 | tape->min_pipeline = min(tape->max_stages, IDETAPE_MIN_PIPELINE_STAGES); |
Borislav Petkov | 5a04cfa | 2008-02-06 02:57:54 +0100 | [diff] [blame] | 3536 | tape->max_pipeline = |
| 3537 | min(tape->max_stages * 2, IDETAPE_MAX_PIPELINE_STAGES); |
| 3538 | if (tape->max_stages == 0) { |
| 3539 | tape->max_stages = 1; |
| 3540 | tape->min_pipeline = 1; |
| 3541 | tape->max_pipeline = 1; |
| 3542 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3543 | |
| 3544 | t1 = (tape->stage_size * HZ) / (speed * 1000); |
Borislav Petkov | b642201 | 2008-02-02 19:56:49 +0100 | [diff] [blame] | 3545 | tmid = (*(u16 *)&tape->caps[16] * 32 * HZ) / (speed * 125); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3546 | tn = (IDETAPE_FIFO_THRESHOLD * tape->stage_size * HZ) / (speed * 1000); |
| 3547 | |
| 3548 | if (tape->max_stages) |
| 3549 | t = tn; |
| 3550 | else |
| 3551 | t = t1; |
| 3552 | |
| 3553 | /* |
Borislav Petkov | 3c98bf3 | 2008-02-06 02:57:53 +0100 | [diff] [blame] | 3554 | * Ensure that the number we got makes sense; limit it within |
| 3555 | * IDETAPE_DSC_RW_MIN and IDETAPE_DSC_RW_MAX. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3556 | */ |
Borislav Petkov | 54bb207 | 2008-02-06 02:57:52 +0100 | [diff] [blame] | 3557 | tape->best_dsc_rw_freq = max_t(unsigned long, |
| 3558 | min_t(unsigned long, t, IDETAPE_DSC_RW_MAX), |
| 3559 | IDETAPE_DSC_RW_MIN); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3560 | printk(KERN_INFO "ide-tape: %s <-> %s: %dKBps, %d*%dkB buffer, " |
| 3561 | "%dkB pipeline, %lums tDSC%s\n", |
Borislav Petkov | b642201 | 2008-02-02 19:56:49 +0100 | [diff] [blame] | 3562 | drive->name, tape->name, *(u16 *)&tape->caps[14], |
| 3563 | (*(u16 *)&tape->caps[16] * 512) / tape->stage_size, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3564 | tape->stage_size / 1024, |
| 3565 | tape->max_stages * tape->stage_size / 1024, |
Borislav Petkov | 54bb207 | 2008-02-06 02:57:52 +0100 | [diff] [blame] | 3566 | tape->best_dsc_rw_freq * 1000 / HZ, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3567 | drive->using_dma ? ", DMA":""); |
| 3568 | |
| 3569 | idetape_add_settings(drive); |
| 3570 | } |
| 3571 | |
Russell King | 4031bbe | 2006-01-06 11:41:00 +0000 | [diff] [blame] | 3572 | static void ide_tape_remove(ide_drive_t *drive) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3573 | { |
| 3574 | idetape_tape_t *tape = drive->driver_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3575 | |
Bartlomiej Zolnierkiewicz | 7662d04 | 2007-05-10 00:01:10 +0200 | [diff] [blame] | 3576 | ide_proc_unregister_driver(drive, tape->driver); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3577 | |
| 3578 | ide_unregister_region(tape->disk); |
| 3579 | |
| 3580 | ide_tape_put(tape); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3581 | } |
| 3582 | |
| 3583 | static void ide_tape_release(struct kref *kref) |
| 3584 | { |
| 3585 | struct ide_tape_obj *tape = to_ide_tape(kref); |
| 3586 | ide_drive_t *drive = tape->drive; |
| 3587 | struct gendisk *g = tape->disk; |
| 3588 | |
Bartlomiej Zolnierkiewicz | 8604aff | 2005-05-26 14:55:34 +0200 | [diff] [blame] | 3589 | BUG_ON(tape->first_stage != NULL || tape->merge_stage_size); |
| 3590 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3591 | drive->dsc_overlap = 0; |
| 3592 | drive->driver_data = NULL; |
Tony Jones | dbc1272 | 2007-09-25 02:03:03 +0200 | [diff] [blame] | 3593 | device_destroy(idetape_sysfs_class, MKDEV(IDETAPE_MAJOR, tape->minor)); |
Borislav Petkov | 5a04cfa | 2008-02-06 02:57:54 +0100 | [diff] [blame] | 3594 | device_destroy(idetape_sysfs_class, |
| 3595 | MKDEV(IDETAPE_MAJOR, tape->minor + 128)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3596 | idetape_devs[tape->minor] = NULL; |
| 3597 | g->private_data = NULL; |
| 3598 | put_disk(g); |
| 3599 | kfree(tape); |
| 3600 | } |
| 3601 | |
Bartlomiej Zolnierkiewicz | ecfd80e | 2007-05-10 00:01:09 +0200 | [diff] [blame] | 3602 | #ifdef CONFIG_IDE_PROC_FS |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3603 | static int proc_idetape_read_name |
| 3604 | (char *page, char **start, off_t off, int count, int *eof, void *data) |
| 3605 | { |
| 3606 | ide_drive_t *drive = (ide_drive_t *) data; |
| 3607 | idetape_tape_t *tape = drive->driver_data; |
| 3608 | char *out = page; |
| 3609 | int len; |
| 3610 | |
| 3611 | len = sprintf(out, "%s\n", tape->name); |
| 3612 | PROC_IDE_READ_RETURN(page, start, off, count, eof, len); |
| 3613 | } |
| 3614 | |
| 3615 | static ide_proc_entry_t idetape_proc[] = { |
| 3616 | { "capacity", S_IFREG|S_IRUGO, proc_ide_read_capacity, NULL }, |
| 3617 | { "name", S_IFREG|S_IRUGO, proc_idetape_read_name, NULL }, |
| 3618 | { NULL, 0, NULL, NULL } |
| 3619 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3620 | #endif |
| 3621 | |
Russell King | 4031bbe | 2006-01-06 11:41:00 +0000 | [diff] [blame] | 3622 | static int ide_tape_probe(ide_drive_t *); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3623 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3624 | static ide_driver_t idetape_driver = { |
Bartlomiej Zolnierkiewicz | 8604aff | 2005-05-26 14:55:34 +0200 | [diff] [blame] | 3625 | .gen_driver = { |
Laurent Riffard | 4ef3b8f | 2005-11-18 22:15:40 +0100 | [diff] [blame] | 3626 | .owner = THIS_MODULE, |
Bartlomiej Zolnierkiewicz | 8604aff | 2005-05-26 14:55:34 +0200 | [diff] [blame] | 3627 | .name = "ide-tape", |
| 3628 | .bus = &ide_bus_type, |
Bartlomiej Zolnierkiewicz | 8604aff | 2005-05-26 14:55:34 +0200 | [diff] [blame] | 3629 | }, |
Russell King | 4031bbe | 2006-01-06 11:41:00 +0000 | [diff] [blame] | 3630 | .probe = ide_tape_probe, |
| 3631 | .remove = ide_tape_remove, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3632 | .version = IDETAPE_VERSION, |
| 3633 | .media = ide_tape, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3634 | .supports_dsc_overlap = 1, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3635 | .do_request = idetape_do_request, |
| 3636 | .end_request = idetape_end_request, |
| 3637 | .error = __ide_error, |
| 3638 | .abort = __ide_abort, |
Bartlomiej Zolnierkiewicz | 7662d04 | 2007-05-10 00:01:10 +0200 | [diff] [blame] | 3639 | #ifdef CONFIG_IDE_PROC_FS |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3640 | .proc = idetape_proc, |
Bartlomiej Zolnierkiewicz | 7662d04 | 2007-05-10 00:01:10 +0200 | [diff] [blame] | 3641 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3642 | }; |
| 3643 | |
Borislav Petkov | 3c98bf3 | 2008-02-06 02:57:53 +0100 | [diff] [blame] | 3644 | /* Our character device supporting functions, passed to register_chrdev. */ |
Arjan van de Ven | 2b8693c | 2007-02-12 00:55:32 -0800 | [diff] [blame] | 3645 | static const struct file_operations idetape_fops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3646 | .owner = THIS_MODULE, |
| 3647 | .read = idetape_chrdev_read, |
| 3648 | .write = idetape_chrdev_write, |
| 3649 | .ioctl = idetape_chrdev_ioctl, |
| 3650 | .open = idetape_chrdev_open, |
| 3651 | .release = idetape_chrdev_release, |
| 3652 | }; |
| 3653 | |
| 3654 | static int idetape_open(struct inode *inode, struct file *filp) |
| 3655 | { |
| 3656 | struct gendisk *disk = inode->i_bdev->bd_disk; |
| 3657 | struct ide_tape_obj *tape; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3658 | |
Borislav Petkov | 5a04cfa | 2008-02-06 02:57:54 +0100 | [diff] [blame] | 3659 | tape = ide_tape_get(disk); |
| 3660 | if (!tape) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3661 | return -ENXIO; |
| 3662 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3663 | return 0; |
| 3664 | } |
| 3665 | |
| 3666 | static int idetape_release(struct inode *inode, struct file *filp) |
| 3667 | { |
| 3668 | struct gendisk *disk = inode->i_bdev->bd_disk; |
| 3669 | struct ide_tape_obj *tape = ide_tape_g(disk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3670 | |
| 3671 | ide_tape_put(tape); |
| 3672 | |
| 3673 | return 0; |
| 3674 | } |
| 3675 | |
| 3676 | static int idetape_ioctl(struct inode *inode, struct file *file, |
| 3677 | unsigned int cmd, unsigned long arg) |
| 3678 | { |
| 3679 | struct block_device *bdev = inode->i_bdev; |
| 3680 | struct ide_tape_obj *tape = ide_tape_g(bdev->bd_disk); |
| 3681 | ide_drive_t *drive = tape->drive; |
| 3682 | int err = generic_ide_ioctl(drive, file, bdev, cmd, arg); |
| 3683 | if (err == -EINVAL) |
| 3684 | err = idetape_blkdev_ioctl(drive, cmd, arg); |
| 3685 | return err; |
| 3686 | } |
| 3687 | |
| 3688 | static struct block_device_operations idetape_block_ops = { |
| 3689 | .owner = THIS_MODULE, |
| 3690 | .open = idetape_open, |
| 3691 | .release = idetape_release, |
| 3692 | .ioctl = idetape_ioctl, |
| 3693 | }; |
| 3694 | |
Russell King | 4031bbe | 2006-01-06 11:41:00 +0000 | [diff] [blame] | 3695 | static int ide_tape_probe(ide_drive_t *drive) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3696 | { |
| 3697 | idetape_tape_t *tape; |
| 3698 | struct gendisk *g; |
| 3699 | int minor; |
| 3700 | |
| 3701 | if (!strstr("ide-tape", drive->driver_req)) |
| 3702 | goto failed; |
| 3703 | if (!drive->present) |
| 3704 | goto failed; |
| 3705 | if (drive->media != ide_tape) |
| 3706 | goto failed; |
Borislav Petkov | 5a04cfa | 2008-02-06 02:57:54 +0100 | [diff] [blame] | 3707 | if (!idetape_identify_device(drive)) { |
| 3708 | printk(KERN_ERR "ide-tape: %s: not supported by this version of" |
| 3709 | " the driver\n", drive->name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3710 | goto failed; |
| 3711 | } |
| 3712 | if (drive->scsi) { |
Borislav Petkov | 5a04cfa | 2008-02-06 02:57:54 +0100 | [diff] [blame] | 3713 | printk(KERN_INFO "ide-tape: passing drive %s to ide-scsi" |
| 3714 | " emulation.\n", drive->name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3715 | goto failed; |
| 3716 | } |
Borislav Petkov | 5a04cfa | 2008-02-06 02:57:54 +0100 | [diff] [blame] | 3717 | tape = kzalloc(sizeof(idetape_tape_t), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3718 | if (tape == NULL) { |
Borislav Petkov | 5a04cfa | 2008-02-06 02:57:54 +0100 | [diff] [blame] | 3719 | printk(KERN_ERR "ide-tape: %s: Can't allocate a tape struct\n", |
| 3720 | drive->name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3721 | goto failed; |
| 3722 | } |
| 3723 | |
| 3724 | g = alloc_disk(1 << PARTN_BITS); |
| 3725 | if (!g) |
| 3726 | goto out_free_tape; |
| 3727 | |
| 3728 | ide_init_disk(g, drive); |
| 3729 | |
Bartlomiej Zolnierkiewicz | 7662d04 | 2007-05-10 00:01:10 +0200 | [diff] [blame] | 3730 | ide_proc_register_driver(drive, &idetape_driver); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3731 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3732 | kref_init(&tape->kref); |
| 3733 | |
| 3734 | tape->drive = drive; |
| 3735 | tape->driver = &idetape_driver; |
| 3736 | tape->disk = g; |
| 3737 | |
| 3738 | g->private_data = &tape->driver; |
| 3739 | |
| 3740 | drive->driver_data = tape; |
| 3741 | |
Arjan van de Ven | cf8b897 | 2006-03-23 03:00:45 -0800 | [diff] [blame] | 3742 | mutex_lock(&idetape_ref_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3743 | for (minor = 0; idetape_devs[minor]; minor++) |
| 3744 | ; |
| 3745 | idetape_devs[minor] = tape; |
Arjan van de Ven | cf8b897 | 2006-03-23 03:00:45 -0800 | [diff] [blame] | 3746 | mutex_unlock(&idetape_ref_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3747 | |
| 3748 | idetape_setup(drive, tape, minor); |
| 3749 | |
Tony Jones | dbc1272 | 2007-09-25 02:03:03 +0200 | [diff] [blame] | 3750 | device_create(idetape_sysfs_class, &drive->gendev, |
| 3751 | MKDEV(IDETAPE_MAJOR, minor), "%s", tape->name); |
| 3752 | device_create(idetape_sysfs_class, &drive->gendev, |
| 3753 | MKDEV(IDETAPE_MAJOR, minor + 128), "n%s", tape->name); |
Will Dyson | d5dee80 | 2005-09-16 02:55:07 -0700 | [diff] [blame] | 3754 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3755 | g->fops = &idetape_block_ops; |
| 3756 | ide_register_region(g); |
| 3757 | |
| 3758 | return 0; |
Bartlomiej Zolnierkiewicz | 8604aff | 2005-05-26 14:55:34 +0200 | [diff] [blame] | 3759 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3760 | out_free_tape: |
| 3761 | kfree(tape); |
| 3762 | failed: |
Bartlomiej Zolnierkiewicz | 8604aff | 2005-05-26 14:55:34 +0200 | [diff] [blame] | 3763 | return -ENODEV; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3764 | } |
| 3765 | |
Borislav Petkov | 5a04cfa | 2008-02-06 02:57:54 +0100 | [diff] [blame] | 3766 | static void __exit idetape_exit(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3767 | { |
Bartlomiej Zolnierkiewicz | 8604aff | 2005-05-26 14:55:34 +0200 | [diff] [blame] | 3768 | driver_unregister(&idetape_driver.gen_driver); |
Will Dyson | d5dee80 | 2005-09-16 02:55:07 -0700 | [diff] [blame] | 3769 | class_destroy(idetape_sysfs_class); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3770 | unregister_chrdev(IDETAPE_MAJOR, "ht"); |
| 3771 | } |
| 3772 | |
Bartlomiej Zolnierkiewicz | 17514e8 | 2005-11-19 22:24:35 +0100 | [diff] [blame] | 3773 | static int __init idetape_init(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3774 | { |
Will Dyson | d5dee80 | 2005-09-16 02:55:07 -0700 | [diff] [blame] | 3775 | int error = 1; |
| 3776 | idetape_sysfs_class = class_create(THIS_MODULE, "ide_tape"); |
| 3777 | if (IS_ERR(idetape_sysfs_class)) { |
| 3778 | idetape_sysfs_class = NULL; |
| 3779 | printk(KERN_ERR "Unable to create sysfs class for ide tapes\n"); |
| 3780 | error = -EBUSY; |
| 3781 | goto out; |
| 3782 | } |
| 3783 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3784 | if (register_chrdev(IDETAPE_MAJOR, "ht", &idetape_fops)) { |
Borislav Petkov | 5a04cfa | 2008-02-06 02:57:54 +0100 | [diff] [blame] | 3785 | printk(KERN_ERR "ide-tape: Failed to register chrdev" |
| 3786 | " interface\n"); |
Will Dyson | d5dee80 | 2005-09-16 02:55:07 -0700 | [diff] [blame] | 3787 | error = -EBUSY; |
| 3788 | goto out_free_class; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3789 | } |
Will Dyson | d5dee80 | 2005-09-16 02:55:07 -0700 | [diff] [blame] | 3790 | |
| 3791 | error = driver_register(&idetape_driver.gen_driver); |
| 3792 | if (error) |
| 3793 | goto out_free_driver; |
| 3794 | |
| 3795 | return 0; |
| 3796 | |
| 3797 | out_free_driver: |
| 3798 | driver_unregister(&idetape_driver.gen_driver); |
| 3799 | out_free_class: |
| 3800 | class_destroy(idetape_sysfs_class); |
| 3801 | out: |
| 3802 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3803 | } |
| 3804 | |
Kay Sievers | 263756e | 2005-12-12 18:03:44 +0100 | [diff] [blame] | 3805 | MODULE_ALIAS("ide:*m-tape*"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3806 | module_init(idetape_init); |
| 3807 | module_exit(idetape_exit); |
| 3808 | MODULE_ALIAS_CHARDEV_MAJOR(IDETAPE_MAJOR); |
Borislav Petkov | 9c14576 | 2008-02-06 02:57:54 +0100 | [diff] [blame] | 3809 | MODULE_DESCRIPTION("ATAPI Streaming TAPE Driver"); |
| 3810 | MODULE_LICENSE("GPL"); |