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