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