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