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