Commit | Line | Data |
---|---|---|
1da177e4 | 1 | /* |
5ce78af4 BP |
2 | * IDE ATAPI streaming tape driver. |
3 | * | |
59bca8cc BZ |
4 | * Copyright (C) 1995-1999 Gadi Oxman <gadio@netvision.net.il> |
5 | * Copyright (C) 2003-2005 Bartlomiej Zolnierkiewicz | |
1da177e4 | 6 | * |
1da177e4 LT |
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). | |
1da177e4 | 13 | * |
5ce78af4 BP |
14 | * For a historical changelog see |
15 | * Documentation/ide/ChangeLog.ide-tape.1995-2002 | |
1da177e4 LT |
16 | */ |
17 | ||
dfe79936 | 18 | #define IDETAPE_VERSION "1.20" |
1da177e4 | 19 | |
1da177e4 LT |
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> | |
9bae1ff3 | 28 | #include <linux/jiffies.h> |
1da177e4 | 29 | #include <linux/major.h> |
1da177e4 LT |
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> | |
cf8b8975 | 38 | #include <linux/mutex.h> |
90699ce2 | 39 | #include <scsi/scsi.h> |
1da177e4 LT |
40 | |
41 | #include <asm/byteorder.h> | |
c837cfa5 BP |
42 | #include <linux/irq.h> |
43 | #include <linux/uaccess.h> | |
44 | #include <linux/io.h> | |
1da177e4 | 45 | #include <asm/unaligned.h> |
1da177e4 LT |
46 | #include <linux/mtio.h> |
47 | ||
8004a8c9 BP |
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 | ||
1da177e4 | 74 | /**************************** Tunable parameters *****************************/ |
1da177e4 | 75 | /* |
3c98bf34 BP |
76 | * After each failed packet command we issue a request sense command and retry |
77 | * the packet command IDETAPE_MAX_PC_RETRIES times. | |
1da177e4 | 78 | * |
3c98bf34 | 79 | * Setting IDETAPE_MAX_PC_RETRIES to 0 will disable retries. |
1da177e4 LT |
80 | */ |
81 | #define IDETAPE_MAX_PC_RETRIES 3 | |
82 | ||
83 | /* | |
3c98bf34 BP |
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) | |
1da177e4 LT |
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 | /* | |
3c98bf34 BP |
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. | |
1da177e4 | 108 | */ |
3c98bf34 | 109 | #define IDETAPE_FIFO_THRESHOLD 2 |
1da177e4 LT |
110 | |
111 | /* | |
3c98bf34 | 112 | * DSC polling parameters. |
1da177e4 | 113 | * |
3c98bf34 BP |
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: | |
1da177e4 | 116 | * |
3c98bf34 BP |
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. | |
1da177e4 | 122 | * |
3c98bf34 BP |
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). | |
1da177e4 | 131 | * |
3c98bf34 BP |
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. | |
1da177e4 | 134 | */ |
3c98bf34 BP |
135 | |
136 | /* DSC timings. */ | |
1da177e4 LT |
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 | ||
3c98bf34 | 147 | /* Read/Write error simulation */ |
1da177e4 LT |
148 | #define SIMULATE_ERRORS 0 |
149 | ||
54abf37e BP |
150 | /* tape directions */ |
151 | enum { | |
152 | IDETAPE_DIR_NONE = (1 << 0), | |
153 | IDETAPE_DIR_READ = (1 << 1), | |
154 | IDETAPE_DIR_WRITE = (1 << 2), | |
155 | }; | |
1da177e4 LT |
156 | |
157 | struct idetape_bh { | |
ab057968 | 158 | u32 b_size; |
1da177e4 LT |
159 | atomic_t b_count; |
160 | struct idetape_bh *b_reqnext; | |
161 | char *b_data; | |
162 | }; | |
163 | ||
03056b90 BP |
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 | ||
346331f8 BP |
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 */ | |
42d54689 | 207 | IDETAPE_FLAG_BUSY = (1 << 2), |
346331f8 | 208 | /* Attempt to auto-detect the current user block size */ |
42d54689 | 209 | IDETAPE_FLAG_DETECT_BS = (1 << 3), |
346331f8 | 210 | /* Currently on a filemark */ |
42d54689 | 211 | IDETAPE_FLAG_FILEMARK = (1 << 4), |
346331f8 | 212 | /* DRQ interrupt device */ |
42d54689 | 213 | IDETAPE_FLAG_DRQ_INTERRUPT = (1 << 5), |
346331f8 | 214 | /* 0 = no tape is loaded, so we don't rewind after ejecting */ |
42d54689 | 215 | IDETAPE_FLAG_MEDIUM_PRESENT = (1 << 6), |
346331f8 BP |
216 | }; |
217 | ||
3c98bf34 | 218 | /* A pipeline stage. */ |
1da177e4 LT |
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 | ||
1da177e4 | 225 | /* |
3c98bf34 BP |
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. | |
1da177e4 LT |
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 */ | |
d236d74c | 251 | struct ide_atapi_pc *pc; |
1da177e4 | 252 | /* Last failed packet command */ |
d236d74c | 253 | struct ide_atapi_pc *failed_pc; |
1da177e4 | 254 | /* Packet command stack */ |
d236d74c | 255 | struct ide_atapi_pc pc_stack[IDETAPE_PC_STACK]; |
1da177e4 LT |
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 | /* | |
3c98bf34 | 263 | * DSC polling variables. |
1da177e4 | 264 | * |
3c98bf34 BP |
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. | |
1da177e4 LT |
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 */ | |
54bb2074 BP |
278 | unsigned long best_dsc_rw_freq; |
279 | unsigned long dsc_poll_freq; | |
1da177e4 LT |
280 | unsigned long dsc_timeout; |
281 | ||
3c98bf34 | 282 | /* Read position information */ |
1da177e4 LT |
283 | u8 partition; |
284 | /* Current block */ | |
54bb2074 | 285 | unsigned int first_frame; |
1da177e4 | 286 | |
3c98bf34 | 287 | /* Last error information */ |
1da177e4 LT |
288 | u8 sense_key, asc, ascq; |
289 | ||
3c98bf34 | 290 | /* Character device operation */ |
1da177e4 LT |
291 | unsigned int minor; |
292 | /* device name */ | |
293 | char name[4]; | |
294 | /* Current character device data transfer direction */ | |
54abf37e | 295 | u8 chrdev_dir; |
1da177e4 | 296 | |
54bb2074 BP |
297 | /* tape block size, usually 512 or 1024 bytes */ |
298 | unsigned short blk_size; | |
1da177e4 | 299 | int user_bs_factor; |
b6422013 | 300 | |
1da177e4 | 301 | /* Copy of the tape's Capabilities and Mechanical Page */ |
b6422013 | 302 | u8 caps[20]; |
1da177e4 LT |
303 | |
304 | /* | |
3c98bf34 | 305 | * Active data transfer request parameters. |
1da177e4 | 306 | * |
3c98bf34 BP |
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. | |
1da177e4 | 310 | */ |
83042b24 | 311 | |
3c98bf34 | 312 | /* Data buffer size chosen based on the tape's recommendation */ |
1da177e4 LT |
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; | |
3c98bf34 | 319 | |
83042b24 | 320 | /* Pipeline parameters. */ |
1da177e4 LT |
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 */ | |
54bb2074 | 328 | spinlock_t lock; |
1da177e4 | 329 | |
3c98bf34 | 330 | /* Measures average tape speed */ |
1da177e4 LT |
331 | unsigned long avg_time; |
332 | int avg_size; | |
333 | int avg_speed; | |
334 | ||
1da177e4 LT |
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 | ||
8004a8c9 | 342 | u32 debug_mask; |
1da177e4 LT |
343 | } idetape_tape_t; |
344 | ||
cf8b8975 | 345 | static DEFINE_MUTEX(idetape_ref_mutex); |
1da177e4 | 346 | |
d5dee80a WD |
347 | static struct class *idetape_sysfs_class; |
348 | ||
1da177e4 LT |
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 | ||
cf8b8975 | 358 | mutex_lock(&idetape_ref_mutex); |
1da177e4 LT |
359 | tape = ide_tape_g(disk); |
360 | if (tape) | |
361 | kref_get(&tape->kref); | |
cf8b8975 | 362 | mutex_unlock(&idetape_ref_mutex); |
1da177e4 LT |
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 | { | |
cf8b8975 | 370 | mutex_lock(&idetape_ref_mutex); |
1da177e4 | 371 | kref_put(&tape->kref, ide_tape_release); |
cf8b8975 | 372 | mutex_unlock(&idetape_ref_mutex); |
1da177e4 LT |
373 | } |
374 | ||
1da177e4 | 375 | /* |
3c98bf34 BP |
376 | * The variables below are used for the character device interface. Additional |
377 | * state variables are defined in our ide_drive_t structure. | |
1da177e4 | 378 | */ |
5a04cfa9 | 379 | static struct ide_tape_obj *idetape_devs[MAX_HWIFS * MAX_DRIVES]; |
1da177e4 LT |
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 | ||
cf8b8975 | 387 | mutex_lock(&idetape_ref_mutex); |
1da177e4 LT |
388 | tape = idetape_devs[i]; |
389 | if (tape) | |
390 | kref_get(&tape->kref); | |
cf8b8975 | 391 | mutex_unlock(&idetape_ref_mutex); |
1da177e4 LT |
392 | return tape; |
393 | } | |
394 | ||
d236d74c | 395 | static void idetape_input_buffers(ide_drive_t *drive, struct ide_atapi_pc *pc, |
5a04cfa9 | 396 | unsigned int bcount) |
1da177e4 LT |
397 | { |
398 | struct idetape_bh *bh = pc->bh; | |
399 | int count; | |
400 | ||
401 | while (bcount) { | |
1da177e4 LT |
402 | if (bh == NULL) { |
403 | printk(KERN_ERR "ide-tape: bh == NULL in " | |
404 | "idetape_input_buffers\n"); | |
7616c0ad | 405 | ide_atapi_discard_data(drive, bcount); |
1da177e4 LT |
406 | return; |
407 | } | |
5a04cfa9 BP |
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); | |
1da177e4 LT |
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 | ||
d236d74c | 424 | static void idetape_output_buffers(ide_drive_t *drive, struct ide_atapi_pc *pc, |
5a04cfa9 | 425 | unsigned int bcount) |
1da177e4 LT |
426 | { |
427 | struct idetape_bh *bh = pc->bh; | |
428 | int count; | |
429 | ||
430 | while (bcount) { | |
1da177e4 | 431 | if (bh == NULL) { |
5a04cfa9 BP |
432 | printk(KERN_ERR "ide-tape: bh == NULL in %s\n", |
433 | __func__); | |
1da177e4 LT |
434 | return; |
435 | } | |
1da177e4 LT |
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) { | |
5a04cfa9 BP |
442 | bh = bh->b_reqnext; |
443 | pc->bh = bh; | |
1da177e4 LT |
444 | if (bh) { |
445 | pc->b_data = bh->b_data; | |
446 | pc->b_count = atomic_read(&bh->b_count); | |
447 | } | |
448 | } | |
449 | } | |
450 | } | |
451 | ||
d236d74c | 452 | static void idetape_update_buffers(struct ide_atapi_pc *pc) |
1da177e4 LT |
453 | { |
454 | struct idetape_bh *bh = pc->bh; | |
455 | int count; | |
d236d74c | 456 | unsigned int bcount = pc->xferred; |
1da177e4 | 457 | |
346331f8 | 458 | if (pc->flags & PC_FLAG_WRITING) |
1da177e4 LT |
459 | return; |
460 | while (bcount) { | |
1da177e4 | 461 | if (bh == NULL) { |
5a04cfa9 BP |
462 | printk(KERN_ERR "ide-tape: bh == NULL in %s\n", |
463 | __func__); | |
1da177e4 LT |
464 | return; |
465 | } | |
1da177e4 LT |
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 | */ | |
d236d74c | 481 | static struct ide_atapi_pc *idetape_next_pc_storage(ide_drive_t *drive) |
1da177e4 LT |
482 | { |
483 | idetape_tape_t *tape = drive->driver_data; | |
484 | ||
8004a8c9 BP |
485 | debug_log(DBG_PCRQ_STACK, "pc_stack_index=%d\n", tape->pc_stack_index); |
486 | ||
1da177e4 | 487 | if (tape->pc_stack_index == IDETAPE_PC_STACK) |
5a04cfa9 | 488 | tape->pc_stack_index = 0; |
1da177e4 LT |
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 | */ | |
5a04cfa9 | 497 | |
1da177e4 LT |
498 | /************************************************************** |
499 | * * | |
500 | * This should get fixed to use kmalloc(.., GFP_ATOMIC) * | |
501 | * followed later on by kfree(). -ml * | |
502 | * * | |
503 | **************************************************************/ | |
5a04cfa9 BP |
504 | |
505 | static struct request *idetape_next_rq_storage(ide_drive_t *drive) | |
1da177e4 LT |
506 | { |
507 | idetape_tape_t *tape = drive->driver_data; | |
508 | ||
8004a8c9 BP |
509 | debug_log(DBG_PCRQ_STACK, "rq_stack_index=%d\n", tape->rq_stack_index); |
510 | ||
1da177e4 | 511 | if (tape->rq_stack_index == IDETAPE_PC_STACK) |
5a04cfa9 | 512 | tape->rq_stack_index = 0; |
1da177e4 LT |
513 | return (&tape->rq_stack[tape->rq_stack_index++]); |
514 | } | |
515 | ||
d236d74c | 516 | static void idetape_init_pc(struct ide_atapi_pc *pc) |
1da177e4 LT |
517 | { |
518 | memset(pc->c, 0, 12); | |
519 | pc->retries = 0; | |
520 | pc->flags = 0; | |
d236d74c BP |
521 | pc->req_xfer = 0; |
522 | pc->buf = pc->pc_buf; | |
523 | pc->buf_size = IDETAPE_PC_BUFFER_SIZE; | |
1da177e4 LT |
524 | pc->bh = NULL; |
525 | pc->b_data = NULL; | |
526 | } | |
527 | ||
528 | /* | |
1b5db434 BP |
529 | * called on each failed packet command retry to analyze the request sense. We |
530 | * currently do not utilize this information. | |
1da177e4 | 531 | */ |
1b5db434 | 532 | static void idetape_analyze_error(ide_drive_t *drive, u8 *sense) |
1da177e4 LT |
533 | { |
534 | idetape_tape_t *tape = drive->driver_data; | |
d236d74c | 535 | struct ide_atapi_pc *pc = tape->failed_pc; |
1da177e4 | 536 | |
1b5db434 BP |
537 | tape->sense_key = sense[2] & 0xF; |
538 | tape->asc = sense[12]; | |
539 | tape->ascq = sense[13]; | |
8004a8c9 BP |
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); | |
1da177e4 | 543 | |
d236d74c | 544 | /* Correct pc->xferred by asking the tape. */ |
346331f8 | 545 | if (pc->flags & PC_FLAG_DMA_ERROR) { |
d236d74c | 546 | pc->xferred = pc->req_xfer - |
54bb2074 | 547 | tape->blk_size * |
860ff5ec | 548 | be32_to_cpu(get_unaligned((u32 *)&sense[3])); |
1da177e4 LT |
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 | */ | |
90699ce2 | 557 | if ((pc->c[0] == READ_6 || pc->c[0] == WRITE_6) |
1b5db434 BP |
558 | /* length == 0 */ |
559 | && pc->c[4] == 0 && pc->c[3] == 0 && pc->c[2] == 0) { | |
560 | if (tape->sense_key == 5) { | |
1da177e4 LT |
561 | /* don't report an error, everything's ok */ |
562 | pc->error = 0; | |
563 | /* don't retry read/write */ | |
346331f8 | 564 | pc->flags |= PC_FLAG_ABORT; |
1da177e4 LT |
565 | } |
566 | } | |
90699ce2 | 567 | if (pc->c[0] == READ_6 && (sense[2] & 0x80)) { |
1da177e4 | 568 | pc->error = IDETAPE_ERROR_FILEMARK; |
346331f8 | 569 | pc->flags |= PC_FLAG_ABORT; |
1da177e4 | 570 | } |
90699ce2 | 571 | if (pc->c[0] == WRITE_6) { |
1b5db434 BP |
572 | if ((sense[2] & 0x40) || (tape->sense_key == 0xd |
573 | && tape->asc == 0x0 && tape->ascq == 0x2)) { | |
1da177e4 | 574 | pc->error = IDETAPE_ERROR_EOD; |
346331f8 | 575 | pc->flags |= PC_FLAG_ABORT; |
1da177e4 LT |
576 | } |
577 | } | |
90699ce2 | 578 | if (pc->c[0] == READ_6 || pc->c[0] == WRITE_6) { |
1b5db434 | 579 | if (tape->sense_key == 8) { |
1da177e4 | 580 | pc->error = IDETAPE_ERROR_EOD; |
346331f8 | 581 | pc->flags |= PC_FLAG_ABORT; |
1da177e4 | 582 | } |
346331f8 | 583 | if (!(pc->flags & PC_FLAG_ABORT) && |
d236d74c | 584 | pc->xferred) |
1da177e4 LT |
585 | pc->retries = IDETAPE_MAX_PC_RETRIES + 1; |
586 | } | |
587 | } | |
588 | ||
3c98bf34 | 589 | /* Free a stage along with its related buffers completely. */ |
5a04cfa9 | 590 | static void __idetape_kfree_stage(idetape_stage_t *stage) |
1da177e4 LT |
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 | ||
1da177e4 | 611 | /* |
3c98bf34 BP |
612 | * Finish servicing a request and insert a pending pipeline request into the |
613 | * main device queue. | |
1da177e4 LT |
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; | |
1da177e4 | 621 | |
8004a8c9 | 622 | debug_log(DBG_PROCS, "Enter %s\n", __func__); |
1da177e4 LT |
623 | |
624 | switch (uptodate) { | |
5a04cfa9 BP |
625 | case 0: error = IDETAPE_ERROR_GENERAL; break; |
626 | case 1: error = 0; break; | |
627 | default: error = uptodate; | |
1da177e4 LT |
628 | } |
629 | rq->errors = error; | |
630 | if (error) | |
631 | tape->failed_pc = NULL; | |
632 | ||
3687221f BZ |
633 | if (!blk_special_request(rq)) { |
634 | ide_end_request(drive, uptodate, nr_sects); | |
635 | return 0; | |
636 | } | |
637 | ||
54bb2074 | 638 | spin_lock_irqsave(&tape->lock, flags); |
1da177e4 | 639 | |
1da177e4 | 640 | ide_end_drive_cmd(drive, 0, 0); |
1da177e4 | 641 | |
54bb2074 | 642 | spin_unlock_irqrestore(&tape->lock, flags); |
1da177e4 LT |
643 | return 0; |
644 | } | |
645 | ||
5a04cfa9 | 646 | static ide_startstop_t idetape_request_sense_callback(ide_drive_t *drive) |
1da177e4 LT |
647 | { |
648 | idetape_tape_t *tape = drive->driver_data; | |
649 | ||
8004a8c9 BP |
650 | debug_log(DBG_PROCS, "Enter %s\n", __func__); |
651 | ||
1da177e4 | 652 | if (!tape->pc->error) { |
d236d74c | 653 | idetape_analyze_error(drive, tape->pc->buf); |
1da177e4 LT |
654 | idetape_end_request(drive, 1, 0); |
655 | } else { | |
5a04cfa9 BP |
656 | printk(KERN_ERR "ide-tape: Error in REQUEST SENSE itself - " |
657 | "Aborting request!\n"); | |
1da177e4 LT |
658 | idetape_end_request(drive, 0, 0); |
659 | } | |
660 | return ide_stopped; | |
661 | } | |
662 | ||
d236d74c | 663 | static void idetape_create_request_sense_cmd(struct ide_atapi_pc *pc) |
1da177e4 | 664 | { |
5a04cfa9 | 665 | idetape_init_pc(pc); |
90699ce2 | 666 | pc->c[0] = REQUEST_SENSE; |
1da177e4 | 667 | pc->c[4] = 20; |
d236d74c BP |
668 | pc->req_xfer = 20; |
669 | pc->idetape_callback = &idetape_request_sense_callback; | |
1da177e4 LT |
670 | } |
671 | ||
672 | static void idetape_init_rq(struct request *rq, u8 cmd) | |
673 | { | |
674 | memset(rq, 0, sizeof(*rq)); | |
4aff5e23 | 675 | rq->cmd_type = REQ_TYPE_SPECIAL; |
1da177e4 LT |
676 | rq->cmd[0] = cmd; |
677 | } | |
678 | ||
679 | /* | |
3c98bf34 BP |
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. | |
1da177e4 | 685 | * |
3c98bf34 BP |
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. | |
1da177e4 | 689 | * |
3c98bf34 BP |
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. | |
1da177e4 | 693 | */ |
d236d74c | 694 | static void idetape_queue_pc_head(ide_drive_t *drive, struct ide_atapi_pc *pc, |
5a04cfa9 | 695 | struct request *rq) |
1da177e4 LT |
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; | |
d236d74c | 713 | struct ide_atapi_pc *pc; |
1da177e4 | 714 | struct request *rq; |
1da177e4 | 715 | |
64a57fe4 | 716 | (void)ide_read_error(drive); |
1da177e4 LT |
717 | pc = idetape_next_pc_storage(drive); |
718 | rq = idetape_next_rq_storage(drive); | |
719 | idetape_create_request_sense_cmd(pc); | |
346331f8 | 720 | set_bit(IDETAPE_FLAG_IGNORE_DSC, &tape->flags); |
1da177e4 LT |
721 | idetape_queue_pc_head(drive, pc, rq); |
722 | return ide_stopped; | |
723 | } | |
724 | ||
725 | /* | |
3c98bf34 BP |
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. | |
1da177e4 | 728 | */ |
5a04cfa9 | 729 | static void idetape_postpone_request(ide_drive_t *drive) |
1da177e4 LT |
730 | { |
731 | idetape_tape_t *tape = drive->driver_data; | |
732 | ||
8004a8c9 BP |
733 | debug_log(DBG_PROCS, "Enter %s\n", __func__); |
734 | ||
1da177e4 | 735 | tape->postponed_rq = HWGROUP(drive)->rq; |
54bb2074 | 736 | ide_stall_queue(drive, tape->dsc_poll_freq); |
1da177e4 LT |
737 | } |
738 | ||
d236d74c | 739 | typedef void idetape_io_buf(ide_drive_t *, struct ide_atapi_pc *, unsigned int); |
a1efc85f | 740 | |
1da177e4 | 741 | /* |
a1efc85f BP |
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 | |
8d06bfad | 746 | * idetape_issue_pc. |
1da177e4 | 747 | */ |
a1efc85f | 748 | static ide_startstop_t idetape_pc_intr(ide_drive_t *drive) |
1da177e4 LT |
749 | { |
750 | ide_hwif_t *hwif = drive->hwif; | |
751 | idetape_tape_t *tape = drive->driver_data; | |
d236d74c | 752 | struct ide_atapi_pc *pc = tape->pc; |
a1efc85f BP |
753 | xfer_func_t *xferfunc; |
754 | idetape_io_buf *iobuf; | |
1da177e4 LT |
755 | unsigned int temp; |
756 | #if SIMULATE_ERRORS | |
5a04cfa9 | 757 | static int error_sim_count; |
1da177e4 | 758 | #endif |
790d1239 | 759 | u16 bcount; |
8e7657ae | 760 | u8 stat, ireason; |
1da177e4 | 761 | |
8004a8c9 | 762 | debug_log(DBG_PROCS, "Enter %s - interrupt handler\n", __func__); |
1da177e4 LT |
763 | |
764 | /* Clear the interrupt */ | |
c47137a9 | 765 | stat = ide_read_status(drive); |
1da177e4 | 766 | |
346331f8 | 767 | if (pc->flags & PC_FLAG_DMA_IN_PROGRESS) { |
5e37bdc0 | 768 | if (hwif->dma_ops->dma_end(drive) || (stat & ERR_STAT)) { |
1da177e4 LT |
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 | */ | |
346331f8 | 790 | pc->flags |= PC_FLAG_DMA_ERROR; |
1da177e4 | 791 | } else { |
d236d74c | 792 | pc->xferred = pc->req_xfer; |
1da177e4 LT |
793 | idetape_update_buffers(pc); |
794 | } | |
8004a8c9 BP |
795 | debug_log(DBG_PROCS, "DMA finished\n"); |
796 | ||
1da177e4 LT |
797 | } |
798 | ||
799 | /* No more interrupts */ | |
22c525b9 | 800 | if ((stat & DRQ_STAT) == 0) { |
8004a8c9 | 801 | debug_log(DBG_SENSE, "Packet command completed, %d bytes" |
d236d74c | 802 | " transferred\n", pc->xferred); |
1da177e4 | 803 | |
346331f8 | 804 | pc->flags &= ~PC_FLAG_DMA_IN_PROGRESS; |
1da177e4 LT |
805 | local_irq_enable(); |
806 | ||
807 | #if SIMULATE_ERRORS | |
90699ce2 | 808 | if ((pc->c[0] == WRITE_6 || pc->c[0] == READ_6) && |
1da177e4 LT |
809 | (++error_sim_count % 100) == 0) { |
810 | printk(KERN_INFO "ide-tape: %s: simulating error\n", | |
811 | tape->name); | |
22c525b9 | 812 | stat |= ERR_STAT; |
1da177e4 LT |
813 | } |
814 | #endif | |
90699ce2 | 815 | if ((stat & ERR_STAT) && pc->c[0] == REQUEST_SENSE) |
22c525b9 | 816 | stat &= ~ERR_STAT; |
346331f8 | 817 | if ((stat & ERR_STAT) || (pc->flags & PC_FLAG_DMA_ERROR)) { |
22c525b9 | 818 | /* Error detected */ |
8004a8c9 BP |
819 | debug_log(DBG_ERR, "%s: I/O error\n", tape->name); |
820 | ||
90699ce2 | 821 | if (pc->c[0] == REQUEST_SENSE) { |
a1efc85f BP |
822 | printk(KERN_ERR "ide-tape: I/O error in request" |
823 | " sense command\n"); | |
1da177e4 LT |
824 | return ide_do_reset(drive); |
825 | } | |
8004a8c9 BP |
826 | debug_log(DBG_ERR, "[cmd %x]: check condition\n", |
827 | pc->c[0]); | |
828 | ||
1da177e4 LT |
829 | /* Retry operation */ |
830 | return idetape_retry_pc(drive); | |
831 | } | |
832 | pc->error = 0; | |
346331f8 | 833 | if ((pc->flags & PC_FLAG_WAIT_FOR_DSC) && |
22c525b9 | 834 | (stat & SEEK_STAT) == 0) { |
1da177e4 LT |
835 | /* Media access command */ |
836 | tape->dsc_polling_start = jiffies; | |
54bb2074 | 837 | tape->dsc_poll_freq = IDETAPE_DSC_MA_FAST; |
1da177e4 LT |
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 */ | |
d236d74c | 846 | return pc->idetape_callback(drive); |
1da177e4 | 847 | } |
346331f8 BP |
848 | |
849 | if (pc->flags & PC_FLAG_DMA_IN_PROGRESS) { | |
850 | pc->flags &= ~PC_FLAG_DMA_IN_PROGRESS; | |
1da177e4 LT |
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"); | |
7469aaf6 | 854 | ide_dma_off(drive); |
1da177e4 LT |
855 | return ide_do_reset(drive); |
856 | } | |
857 | /* Get the number of bytes to transfer on this interrupt. */ | |
23579a2a BZ |
858 | bcount = (hwif->INB(hwif->io_ports[IDE_BCOUNTH_OFFSET]) << 8) | |
859 | hwif->INB(hwif->io_ports[IDE_BCOUNTL_OFFSET]); | |
1da177e4 | 860 | |
23579a2a | 861 | ireason = hwif->INB(hwif->io_ports[IDE_IREASON_OFFSET]); |
1da177e4 | 862 | |
8e7657ae | 863 | if (ireason & CD) { |
a1efc85f | 864 | printk(KERN_ERR "ide-tape: CoD != 0 in %s\n", __func__); |
1da177e4 LT |
865 | return ide_do_reset(drive); |
866 | } | |
346331f8 | 867 | if (((ireason & IO) == IO) == !!(pc->flags & PC_FLAG_WRITING)) { |
1da177e4 LT |
868 | /* Hopefully, we will never get here */ |
869 | printk(KERN_ERR "ide-tape: We wanted to %s, ", | |
8e7657ae | 870 | (ireason & IO) ? "Write" : "Read"); |
1da177e4 | 871 | printk(KERN_ERR "ide-tape: but the tape wants us to %s !\n", |
8e7657ae | 872 | (ireason & IO) ? "Read" : "Write"); |
1da177e4 LT |
873 | return ide_do_reset(drive); |
874 | } | |
346331f8 | 875 | if (!(pc->flags & PC_FLAG_WRITING)) { |
1da177e4 | 876 | /* Reading - Check that we have enough space */ |
d236d74c BP |
877 | temp = pc->xferred + bcount; |
878 | if (temp > pc->req_xfer) { | |
879 | if (temp > pc->buf_size) { | |
a1efc85f BP |
880 | printk(KERN_ERR "ide-tape: The tape wants to " |
881 | "send us more data than expected " | |
882 | "- discarding data\n"); | |
7616c0ad | 883 | ide_atapi_discard_data(drive, bcount); |
a1efc85f BP |
884 | ide_set_handler(drive, &idetape_pc_intr, |
885 | IDETAPE_WAIT_CMD, NULL); | |
1da177e4 LT |
886 | return ide_started; |
887 | } | |
8004a8c9 BP |
888 | debug_log(DBG_SENSE, "The tape wants to send us more " |
889 | "data than expected - allowing transfer\n"); | |
1da177e4 | 890 | } |
a1efc85f BP |
891 | iobuf = &idetape_input_buffers; |
892 | xferfunc = hwif->atapi_input_bytes; | |
1da177e4 | 893 | } else { |
a1efc85f BP |
894 | iobuf = &idetape_output_buffers; |
895 | xferfunc = hwif->atapi_output_bytes; | |
1da177e4 | 896 | } |
a1efc85f BP |
897 | |
898 | if (pc->bh) | |
899 | iobuf(drive, pc, bcount); | |
900 | else | |
d236d74c | 901 | xferfunc(drive, pc->cur_pos, bcount); |
a1efc85f | 902 | |
1da177e4 | 903 | /* Update the current position */ |
d236d74c BP |
904 | pc->xferred += bcount; |
905 | pc->cur_pos += bcount; | |
8004a8c9 BP |
906 | |
907 | debug_log(DBG_SENSE, "[cmd %x] transferred %d bytes on that intr.\n", | |
908 | pc->c[0], bcount); | |
909 | ||
1da177e4 LT |
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 | /* | |
3c98bf34 | 916 | * Packet Command Interface |
1da177e4 | 917 | * |
3c98bf34 BP |
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. | |
1da177e4 | 921 | * |
3c98bf34 | 922 | * The handling will be done in three stages: |
1da177e4 | 923 | * |
3c98bf34 BP |
924 | * 1. idetape_issue_pc will send the packet command to the drive, and will set |
925 | * the interrupt handler to idetape_pc_intr. | |
1da177e4 | 926 | * |
3c98bf34 BP |
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. | |
1da177e4 | 929 | * |
3c98bf34 BP |
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. | |
1da177e4 | 937 | * |
3c98bf34 BP |
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. | |
1da177e4 | 940 | * |
3c98bf34 | 941 | * 4. When the packet command is finished, it will be checked for errors. |
1da177e4 | 942 | * |
3c98bf34 BP |
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. | |
1da177e4 | 946 | * |
3c98bf34 BP |
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. | |
1da177e4 LT |
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; | |
d236d74c | 955 | struct ide_atapi_pc *pc = tape->pc; |
1da177e4 LT |
956 | int retries = 100; |
957 | ide_startstop_t startstop; | |
8e7657ae | 958 | u8 ireason; |
1da177e4 | 959 | |
5a04cfa9 BP |
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"); | |
1da177e4 LT |
963 | return startstop; |
964 | } | |
23579a2a | 965 | ireason = hwif->INB(hwif->io_ports[IDE_IREASON_OFFSET]); |
8e7657ae | 966 | while (retries-- && ((ireason & CD) == 0 || (ireason & IO))) { |
1da177e4 LT |
967 | printk(KERN_ERR "ide-tape: (IO,CoD != (0,1) while issuing " |
968 | "a packet command, retrying\n"); | |
969 | udelay(100); | |
23579a2a | 970 | ireason = hwif->INB(hwif->io_ports[IDE_IREASON_OFFSET]); |
1da177e4 LT |
971 | if (retries == 0) { |
972 | printk(KERN_ERR "ide-tape: (IO,CoD != (0,1) while " | |
973 | "issuing a packet command, ignoring\n"); | |
8e7657ae BZ |
974 | ireason |= CD; |
975 | ireason &= ~IO; | |
1da177e4 LT |
976 | } |
977 | } | |
8e7657ae | 978 | if ((ireason & CD) == 0 || (ireason & IO)) { |
1da177e4 LT |
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 */ | |
346331f8 | 987 | if (pc->flags & PC_FLAG_DMA_IN_PROGRESS) |
5e37bdc0 | 988 | hwif->dma_ops->dma_start(drive); |
1da177e4 LT |
989 | #endif |
990 | /* Send the actual packet */ | |
991 | HWIF(drive)->atapi_output_bytes(drive, pc->c, 12); | |
992 | return ide_started; | |
993 | } | |
994 | ||
d236d74c BP |
995 | static ide_startstop_t idetape_issue_pc(ide_drive_t *drive, |
996 | struct ide_atapi_pc *pc) | |
1da177e4 LT |
997 | { |
998 | ide_hwif_t *hwif = drive->hwif; | |
999 | idetape_tape_t *tape = drive->driver_data; | |
1da177e4 | 1000 | int dma_ok = 0; |
790d1239 | 1001 | u16 bcount; |
1da177e4 | 1002 | |
90699ce2 BP |
1003 | if (tape->pc->c[0] == REQUEST_SENSE && |
1004 | pc->c[0] == REQUEST_SENSE) { | |
1da177e4 LT |
1005 | printk(KERN_ERR "ide-tape: possible ide-tape.c bug - " |
1006 | "Two request sense in serial were issued\n"); | |
1007 | } | |
1da177e4 | 1008 | |
90699ce2 | 1009 | if (tape->failed_pc == NULL && pc->c[0] != REQUEST_SENSE) |
1da177e4 LT |
1010 | tape->failed_pc = pc; |
1011 | /* Set the current packet command */ | |
1012 | tape->pc = pc; | |
1013 | ||
1014 | if (pc->retries > IDETAPE_MAX_PC_RETRIES || | |
346331f8 | 1015 | (pc->flags & PC_FLAG_ABORT)) { |
1da177e4 | 1016 | /* |
3c98bf34 BP |
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). | |
1da177e4 | 1020 | */ |
346331f8 | 1021 | if (!(pc->flags & PC_FLAG_ABORT)) { |
90699ce2 | 1022 | if (!(pc->c[0] == TEST_UNIT_READY && |
1da177e4 LT |
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; | |
d236d74c | 1036 | return pc->idetape_callback(drive); |
1da177e4 | 1037 | } |
8004a8c9 | 1038 | debug_log(DBG_SENSE, "Retry #%d, cmd = %02X\n", pc->retries, pc->c[0]); |
1da177e4 LT |
1039 | |
1040 | pc->retries++; | |
1041 | /* We haven't transferred any data yet */ | |
d236d74c BP |
1042 | pc->xferred = 0; |
1043 | pc->cur_pos = pc->buf; | |
1da177e4 | 1044 | /* Request to transfer the entire buffer at once */ |
d236d74c | 1045 | bcount = pc->req_xfer; |
1da177e4 | 1046 | |
346331f8 BP |
1047 | if (pc->flags & PC_FLAG_DMA_ERROR) { |
1048 | pc->flags &= ~PC_FLAG_DMA_ERROR; | |
1da177e4 LT |
1049 | printk(KERN_WARNING "ide-tape: DMA disabled, " |
1050 | "reverting to PIO\n"); | |
7469aaf6 | 1051 | ide_dma_off(drive); |
1da177e4 | 1052 | } |
346331f8 | 1053 | if ((pc->flags & PC_FLAG_DMA_RECOMMENDED) && drive->using_dma) |
5e37bdc0 | 1054 | dma_ok = !hwif->dma_ops->dma_setup(drive); |
1da177e4 | 1055 | |
2fc57388 BZ |
1056 | ide_pktcmd_tf_load(drive, IDE_TFLAG_NO_SELECT_MASK | |
1057 | IDE_TFLAG_OUT_DEVICE, bcount, dma_ok); | |
1058 | ||
346331f8 BP |
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)) { | |
c1c9dbc8 BZ |
1063 | ide_execute_command(drive, WIN_PACKETCMD, &idetape_transfer_pc, |
1064 | IDETAPE_WAIT_CMD, NULL); | |
1da177e4 LT |
1065 | return ide_started; |
1066 | } else { | |
23579a2a | 1067 | hwif->OUTB(WIN_PACKETCMD, hwif->io_ports[IDE_COMMAND_OFFSET]); |
1da177e4 LT |
1068 | return idetape_transfer_pc(drive); |
1069 | } | |
1070 | } | |
1071 | ||
5a04cfa9 | 1072 | static ide_startstop_t idetape_pc_callback(ide_drive_t *drive) |
1da177e4 LT |
1073 | { |
1074 | idetape_tape_t *tape = drive->driver_data; | |
8004a8c9 BP |
1075 | |
1076 | debug_log(DBG_PROCS, "Enter %s\n", __func__); | |
1da177e4 LT |
1077 | |
1078 | idetape_end_request(drive, tape->pc->error ? 0 : 1, 0); | |
1079 | return ide_stopped; | |
1080 | } | |
1081 | ||
3c98bf34 | 1082 | /* A mode sense command is used to "sense" tape parameters. */ |
d236d74c | 1083 | static void idetape_create_mode_sense_cmd(struct ide_atapi_pc *pc, u8 page_code) |
1da177e4 LT |
1084 | { |
1085 | idetape_init_pc(pc); | |
90699ce2 | 1086 | pc->c[0] = MODE_SENSE; |
1da177e4 | 1087 | if (page_code != IDETAPE_BLOCK_DESCRIPTOR) |
3c98bf34 BP |
1088 | /* DBD = 1 - Don't return block descriptors */ |
1089 | pc->c[1] = 8; | |
1da177e4 LT |
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; | |
3c98bf34 BP |
1099 | /* We will just discard data in that case */ |
1100 | pc->c[4] = 255; | |
1da177e4 | 1101 | if (page_code == IDETAPE_BLOCK_DESCRIPTOR) |
d236d74c | 1102 | pc->req_xfer = 12; |
1da177e4 | 1103 | else if (page_code == IDETAPE_CAPABILITIES_PAGE) |
d236d74c | 1104 | pc->req_xfer = 24; |
1da177e4 | 1105 | else |
d236d74c BP |
1106 | pc->req_xfer = 50; |
1107 | pc->idetape_callback = &idetape_pc_callback; | |
1da177e4 LT |
1108 | } |
1109 | ||
5a04cfa9 | 1110 | static ide_startstop_t idetape_media_access_finished(ide_drive_t *drive) |
1da177e4 LT |
1111 | { |
1112 | idetape_tape_t *tape = drive->driver_data; | |
d236d74c | 1113 | struct ide_atapi_pc *pc = tape->pc; |
22c525b9 | 1114 | u8 stat; |
1da177e4 | 1115 | |
c47137a9 BZ |
1116 | stat = ide_read_status(drive); |
1117 | ||
22c525b9 BZ |
1118 | if (stat & SEEK_STAT) { |
1119 | if (stat & ERR_STAT) { | |
1da177e4 | 1120 | /* Error detected */ |
90699ce2 | 1121 | if (pc->c[0] != TEST_UNIT_READY) |
1da177e4 LT |
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 | } | |
d236d74c | 1134 | return pc->idetape_callback(drive); |
1da177e4 LT |
1135 | } |
1136 | ||
5a04cfa9 | 1137 | static ide_startstop_t idetape_rw_callback(ide_drive_t *drive) |
1da177e4 LT |
1138 | { |
1139 | idetape_tape_t *tape = drive->driver_data; | |
1140 | struct request *rq = HWGROUP(drive)->rq; | |
d236d74c | 1141 | int blocks = tape->pc->xferred / tape->blk_size; |
1da177e4 | 1142 | |
54bb2074 | 1143 | tape->avg_size += blocks * tape->blk_size; |
83042b24 | 1144 | |
9bae1ff3 | 1145 | if (time_after_eq(jiffies, tape->avg_time + HZ)) { |
5a04cfa9 BP |
1146 | tape->avg_speed = tape->avg_size * HZ / |
1147 | (jiffies - tape->avg_time) / 1024; | |
1da177e4 LT |
1148 | tape->avg_size = 0; |
1149 | tape->avg_time = jiffies; | |
1150 | } | |
8004a8c9 | 1151 | debug_log(DBG_PROCS, "Enter %s\n", __func__); |
1da177e4 | 1152 | |
54bb2074 | 1153 | tape->first_frame += blocks; |
1da177e4 LT |
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 | ||
d236d74c BP |
1163 | static void idetape_create_read_cmd(idetape_tape_t *tape, |
1164 | struct ide_atapi_pc *pc, | |
5a04cfa9 | 1165 | unsigned int length, struct idetape_bh *bh) |
1da177e4 LT |
1166 | { |
1167 | idetape_init_pc(pc); | |
90699ce2 | 1168 | pc->c[0] = READ_6; |
860ff5ec | 1169 | put_unaligned(cpu_to_be32(length), (unsigned int *) &pc->c[1]); |
1da177e4 | 1170 | pc->c[1] = 1; |
d236d74c | 1171 | pc->idetape_callback = &idetape_rw_callback; |
1da177e4 LT |
1172 | pc->bh = bh; |
1173 | atomic_set(&bh->b_count, 0); | |
d236d74c BP |
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) | |
346331f8 | 1178 | pc->flags |= PC_FLAG_DMA_RECOMMENDED; |
1da177e4 LT |
1179 | } |
1180 | ||
d236d74c BP |
1181 | static void idetape_create_write_cmd(idetape_tape_t *tape, |
1182 | struct ide_atapi_pc *pc, | |
5a04cfa9 | 1183 | unsigned int length, struct idetape_bh *bh) |
1da177e4 LT |
1184 | { |
1185 | idetape_init_pc(pc); | |
90699ce2 | 1186 | pc->c[0] = WRITE_6; |
860ff5ec | 1187 | put_unaligned(cpu_to_be32(length), (unsigned int *) &pc->c[1]); |
1da177e4 | 1188 | pc->c[1] = 1; |
d236d74c | 1189 | pc->idetape_callback = &idetape_rw_callback; |
346331f8 | 1190 | pc->flags |= PC_FLAG_WRITING; |
1da177e4 LT |
1191 | pc->bh = bh; |
1192 | pc->b_data = bh->b_data; | |
1193 | pc->b_count = atomic_read(&bh->b_count); | |
d236d74c BP |
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) | |
346331f8 | 1198 | pc->flags |= PC_FLAG_DMA_RECOMMENDED; |
1da177e4 LT |
1199 | } |
1200 | ||
1da177e4 LT |
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; | |
d236d74c | 1205 | struct ide_atapi_pc *pc = NULL; |
1da177e4 | 1206 | struct request *postponed_rq = tape->postponed_rq; |
22c525b9 | 1207 | u8 stat; |
1da177e4 | 1208 | |
8004a8c9 BP |
1209 | debug_log(DBG_SENSE, "sector: %ld, nr_sectors: %ld," |
1210 | " current_nr_sectors: %d\n", | |
1da177e4 | 1211 | rq->sector, rq->nr_sectors, rq->current_nr_sectors); |
1da177e4 | 1212 | |
4aff5e23 | 1213 | if (!blk_special_request(rq)) { |
3c98bf34 | 1214 | /* We do not support buffer cache originated requests. */ |
1da177e4 | 1215 | printk(KERN_NOTICE "ide-tape: %s: Unsupported request in " |
4aff5e23 | 1216 | "request queue (%d)\n", drive->name, rq->cmd_type); |
1da177e4 LT |
1217 | ide_end_request(drive, 0, 0); |
1218 | return ide_stopped; | |
1219 | } | |
1220 | ||
3c98bf34 | 1221 | /* Retry a failed packet command */ |
5a04cfa9 | 1222 | if (tape->failed_pc && tape->pc->c[0] == REQUEST_SENSE) |
8d06bfad | 1223 | return idetape_issue_pc(drive, tape->failed_pc); |
5a04cfa9 | 1224 | |
1da177e4 LT |
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 | } | |
1da177e4 LT |
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 | */ | |
c47137a9 | 1239 | stat = ide_read_status(drive); |
1da177e4 LT |
1240 | |
1241 | if (!drive->dsc_overlap && !(rq->cmd[0] & REQ_IDETAPE_PC2)) | |
346331f8 | 1242 | set_bit(IDETAPE_FLAG_IGNORE_DSC, &tape->flags); |
1da177e4 LT |
1243 | |
1244 | if (drive->post_reset == 1) { | |
346331f8 | 1245 | set_bit(IDETAPE_FLAG_IGNORE_DSC, &tape->flags); |
1da177e4 LT |
1246 | drive->post_reset = 0; |
1247 | } | |
1248 | ||
346331f8 | 1249 | if (!test_and_clear_bit(IDETAPE_FLAG_IGNORE_DSC, &tape->flags) && |
22c525b9 | 1250 | (stat & SEEK_STAT) == 0) { |
1da177e4 LT |
1251 | if (postponed_rq == NULL) { |
1252 | tape->dsc_polling_start = jiffies; | |
54bb2074 | 1253 | tape->dsc_poll_freq = tape->best_dsc_rw_freq; |
1da177e4 LT |
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 | } | |
5a04cfa9 BP |
1264 | } else if (time_after(jiffies, |
1265 | tape->dsc_polling_start + | |
1266 | IDETAPE_DSC_MA_THRESHOLD)) | |
54bb2074 | 1267 | tape->dsc_poll_freq = IDETAPE_DSC_MA_SLOW; |
1da177e4 LT |
1268 | idetape_postpone_request(drive); |
1269 | return ide_stopped; | |
1270 | } | |
1271 | if (rq->cmd[0] & REQ_IDETAPE_READ) { | |
1da177e4 | 1272 | pc = idetape_next_pc_storage(drive); |
5a04cfa9 BP |
1273 | idetape_create_read_cmd(tape, pc, rq->current_nr_sectors, |
1274 | (struct idetape_bh *)rq->special); | |
1da177e4 LT |
1275 | goto out; |
1276 | } | |
1277 | if (rq->cmd[0] & REQ_IDETAPE_WRITE) { | |
1da177e4 | 1278 | pc = idetape_next_pc_storage(drive); |
5a04cfa9 BP |
1279 | idetape_create_write_cmd(tape, pc, rq->current_nr_sectors, |
1280 | (struct idetape_bh *)rq->special); | |
1da177e4 LT |
1281 | goto out; |
1282 | } | |
1da177e4 | 1283 | if (rq->cmd[0] & REQ_IDETAPE_PC1) { |
d236d74c | 1284 | pc = (struct ide_atapi_pc *) rq->buffer; |
1da177e4 LT |
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: | |
8d06bfad | 1295 | return idetape_issue_pc(drive, pc); |
1da177e4 LT |
1296 | } |
1297 | ||
3c98bf34 | 1298 | /* Pipeline related functions */ |
1da177e4 LT |
1299 | |
1300 | /* | |
3c98bf34 BP |
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. | |
1da177e4 | 1305 | * |
3c98bf34 BP |
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. | |
1da177e4 | 1308 | * |
3c98bf34 BP |
1309 | * Pipeline stages are optional and are used to increase performance. If we |
1310 | * can't allocate them, we'll manage without them. | |
1da177e4 | 1311 | */ |
5a04cfa9 BP |
1312 | static idetape_stage_t *__idetape_kmalloc_stage(idetape_tape_t *tape, int full, |
1313 | int clear) | |
1da177e4 LT |
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 | ||
5a04cfa9 BP |
1320 | stage = kmalloc(sizeof(idetape_stage_t), GFP_KERNEL); |
1321 | if (!stage) | |
1da177e4 LT |
1322 | return NULL; |
1323 | stage->next = NULL; | |
1324 | ||
5a04cfa9 BP |
1325 | stage->bh = kmalloc(sizeof(struct idetape_bh), GFP_KERNEL); |
1326 | bh = stage->bh; | |
1da177e4 LT |
1327 | if (bh == NULL) |
1328 | goto abort; | |
1329 | bh->b_reqnext = NULL; | |
5a04cfa9 BP |
1330 | bh->b_data = (char *) __get_free_page(GFP_KERNEL); |
1331 | if (!bh->b_data) | |
1da177e4 LT |
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) { | |
5a04cfa9 BP |
1339 | b_data = (char *) __get_free_page(GFP_KERNEL); |
1340 | if (!b_data) | |
1da177e4 LT |
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; | |
5a04cfa9 BP |
1358 | bh = kmalloc(sizeof(struct idetape_bh), GFP_KERNEL); |
1359 | if (!bh) { | |
1da177e4 LT |
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 | ||
5a04cfa9 | 1378 | static int idetape_copy_stage_from_user(idetape_tape_t *tape, |
8646c88f | 1379 | const char __user *buf, int n) |
1da177e4 LT |
1380 | { |
1381 | struct idetape_bh *bh = tape->bh; | |
1382 | int count; | |
dcd96379 | 1383 | int ret = 0; |
1da177e4 LT |
1384 | |
1385 | while (n) { | |
1da177e4 | 1386 | if (bh == NULL) { |
5a04cfa9 BP |
1387 | printk(KERN_ERR "ide-tape: bh == NULL in %s\n", |
1388 | __func__); | |
dcd96379 | 1389 | return 1; |
1da177e4 | 1390 | } |
5a04cfa9 BP |
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)) | |
dcd96379 | 1396 | ret = 1; |
1da177e4 LT |
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; | |
dcd96379 | 1407 | return ret; |
1da177e4 LT |
1408 | } |
1409 | ||
5a04cfa9 | 1410 | static int idetape_copy_stage_to_user(idetape_tape_t *tape, char __user *buf, |
99d74e61 | 1411 | int n) |
1da177e4 LT |
1412 | { |
1413 | struct idetape_bh *bh = tape->bh; | |
1414 | int count; | |
dcd96379 | 1415 | int ret = 0; |
1da177e4 LT |
1416 | |
1417 | while (n) { | |
1da177e4 | 1418 | if (bh == NULL) { |
5a04cfa9 BP |
1419 | printk(KERN_ERR "ide-tape: bh == NULL in %s\n", |
1420 | __func__); | |
dcd96379 | 1421 | return 1; |
1da177e4 | 1422 | } |
1da177e4 | 1423 | count = min(tape->b_count, n); |
dcd96379 DW |
1424 | if (copy_to_user(buf, tape->b_data, count)) |
1425 | ret = 1; | |
1da177e4 LT |
1426 | n -= count; |
1427 | tape->b_data += count; | |
1428 | tape->b_count -= count; | |
1429 | buf += count; | |
1430 | if (!tape->b_count) { | |
5a04cfa9 BP |
1431 | bh = bh->b_reqnext; |
1432 | tape->bh = bh; | |
1da177e4 LT |
1433 | if (bh) { |
1434 | tape->b_data = bh->b_data; | |
1435 | tape->b_count = atomic_read(&bh->b_count); | |
1436 | } | |
1437 | } | |
1438 | } | |
dcd96379 | 1439 | return ret; |
1da177e4 LT |
1440 | } |
1441 | ||
5a04cfa9 | 1442 | static void idetape_init_merge_stage(idetape_tape_t *tape) |
1da177e4 LT |
1443 | { |
1444 | struct idetape_bh *bh = tape->merge_stage->bh; | |
5a04cfa9 | 1445 | |
1da177e4 | 1446 | tape->bh = bh; |
54abf37e | 1447 | if (tape->chrdev_dir == IDETAPE_DIR_WRITE) |
1da177e4 LT |
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 | ||
a2f5b7f4 | 1455 | static ide_startstop_t idetape_read_position_callback(ide_drive_t *drive) |
1da177e4 LT |
1456 | { |
1457 | idetape_tape_t *tape = drive->driver_data; | |
d236d74c | 1458 | u8 *readpos = tape->pc->buf; |
8004a8c9 BP |
1459 | |
1460 | debug_log(DBG_PROCS, "Enter %s\n", __func__); | |
1da177e4 LT |
1461 | |
1462 | if (!tape->pc->error) { | |
a2f5b7f4 BP |
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"); | |
1467 | ||
1468 | if (readpos[0] & 0x4) { | |
1469 | printk(KERN_INFO "ide-tape: Block location is unknown" | |
1470 | "to the tape\n"); | |
346331f8 | 1471 | clear_bit(IDETAPE_FLAG_ADDRESS_VALID, &tape->flags); |
1da177e4 LT |
1472 | idetape_end_request(drive, 0, 0); |
1473 | } else { | |
8004a8c9 | 1474 | debug_log(DBG_SENSE, "Block Location - %u\n", |
a2f5b7f4 BP |
1475 | be32_to_cpu(*(u32 *)&readpos[4])); |
1476 | ||
1477 | tape->partition = readpos[1]; | |
54bb2074 | 1478 | tape->first_frame = |
a2f5b7f4 | 1479 | be32_to_cpu(*(u32 *)&readpos[4]); |
346331f8 | 1480 | set_bit(IDETAPE_FLAG_ADDRESS_VALID, &tape->flags); |
1da177e4 LT |
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 | /* | |
3c98bf34 BP |
1490 | * Write a filemark if write_filemark=1. Flush the device buffers without |
1491 | * writing a filemark otherwise. | |
1da177e4 | 1492 | */ |
5a04cfa9 | 1493 | static void idetape_create_write_filemark_cmd(ide_drive_t *drive, |
d236d74c | 1494 | struct ide_atapi_pc *pc, int write_filemark) |
1da177e4 LT |
1495 | { |
1496 | idetape_init_pc(pc); | |
90699ce2 | 1497 | pc->c[0] = WRITE_FILEMARKS; |
1da177e4 | 1498 | pc->c[4] = write_filemark; |
346331f8 | 1499 | pc->flags |= PC_FLAG_WAIT_FOR_DSC; |
d236d74c | 1500 | pc->idetape_callback = &idetape_pc_callback; |
1da177e4 LT |
1501 | } |
1502 | ||
d236d74c | 1503 | static void idetape_create_test_unit_ready_cmd(struct ide_atapi_pc *pc) |
1da177e4 LT |
1504 | { |
1505 | idetape_init_pc(pc); | |
90699ce2 | 1506 | pc->c[0] = TEST_UNIT_READY; |
d236d74c | 1507 | pc->idetape_callback = &idetape_pc_callback; |
1da177e4 LT |
1508 | } |
1509 | ||
1510 | /* | |
3c98bf34 BP |
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. | |
1da177e4 | 1517 | * |
3c98bf34 BP |
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(). | |
1da177e4 | 1522 | */ |
ea1ab3d3 | 1523 | static int idetape_queue_pc_tail(ide_drive_t *drive, struct ide_atapi_pc *pc) |
1da177e4 LT |
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 | ||
d236d74c BP |
1534 | static void idetape_create_load_unload_cmd(ide_drive_t *drive, |
1535 | struct ide_atapi_pc *pc, int cmd) | |
1da177e4 LT |
1536 | { |
1537 | idetape_init_pc(pc); | |
90699ce2 | 1538 | pc->c[0] = START_STOP; |
1da177e4 | 1539 | pc->c[4] = cmd; |
346331f8 | 1540 | pc->flags |= PC_FLAG_WAIT_FOR_DSC; |
d236d74c | 1541 | pc->idetape_callback = &idetape_pc_callback; |
1da177e4 LT |
1542 | } |
1543 | ||
1544 | static int idetape_wait_ready(ide_drive_t *drive, unsigned long timeout) | |
1545 | { | |
1546 | idetape_tape_t *tape = drive->driver_data; | |
d236d74c | 1547 | struct ide_atapi_pc pc; |
1da177e4 LT |
1548 | int load_attempted = 0; |
1549 | ||
3c98bf34 | 1550 | /* Wait for the tape to become ready */ |
346331f8 | 1551 | set_bit(IDETAPE_FLAG_MEDIUM_PRESENT, &tape->flags); |
1da177e4 LT |
1552 | timeout += jiffies; |
1553 | while (time_before(jiffies, timeout)) { | |
1554 | idetape_create_test_unit_ready_cmd(&pc); | |
ea1ab3d3 | 1555 | if (!idetape_queue_pc_tail(drive, &pc)) |
1da177e4 LT |
1556 | return 0; |
1557 | if ((tape->sense_key == 2 && tape->asc == 4 && tape->ascq == 2) | |
3c98bf34 BP |
1558 | || (tape->asc == 0x3A)) { |
1559 | /* no media */ | |
1da177e4 LT |
1560 | if (load_attempted) |
1561 | return -ENOMEDIUM; | |
5a04cfa9 BP |
1562 | idetape_create_load_unload_cmd(drive, &pc, |
1563 | IDETAPE_LU_LOAD_MASK); | |
ea1ab3d3 | 1564 | idetape_queue_pc_tail(drive, &pc); |
1da177e4 LT |
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; | |
80ce45fd | 1570 | msleep(100); |
1da177e4 LT |
1571 | } |
1572 | return -EIO; | |
1573 | } | |
1574 | ||
5a04cfa9 | 1575 | static int idetape_flush_tape_buffers(ide_drive_t *drive) |
1da177e4 | 1576 | { |
d236d74c | 1577 | struct ide_atapi_pc pc; |
1da177e4 LT |
1578 | int rc; |
1579 | ||
1580 | idetape_create_write_filemark_cmd(drive, &pc, 0); | |
5a04cfa9 BP |
1581 | rc = idetape_queue_pc_tail(drive, &pc); |
1582 | if (rc) | |
1da177e4 LT |
1583 | return rc; |
1584 | idetape_wait_ready(drive, 60 * 5 * HZ); | |
1585 | return 0; | |
1586 | } | |
1587 | ||
d236d74c | 1588 | static void idetape_create_read_position_cmd(struct ide_atapi_pc *pc) |
1da177e4 LT |
1589 | { |
1590 | idetape_init_pc(pc); | |
90699ce2 | 1591 | pc->c[0] = READ_POSITION; |
d236d74c BP |
1592 | pc->req_xfer = 20; |
1593 | pc->idetape_callback = &idetape_read_position_callback; | |
1da177e4 LT |
1594 | } |
1595 | ||
5a04cfa9 | 1596 | static int idetape_read_position(ide_drive_t *drive) |
1da177e4 LT |
1597 | { |
1598 | idetape_tape_t *tape = drive->driver_data; | |
d236d74c | 1599 | struct ide_atapi_pc pc; |
1da177e4 LT |
1600 | int position; |
1601 | ||
8004a8c9 | 1602 | debug_log(DBG_PROCS, "Enter %s\n", __func__); |
1da177e4 LT |
1603 | |
1604 | idetape_create_read_position_cmd(&pc); | |
1605 | if (idetape_queue_pc_tail(drive, &pc)) | |
1606 | return -1; | |
54bb2074 | 1607 | position = tape->first_frame; |
1da177e4 LT |
1608 | return position; |
1609 | } | |
1610 | ||
d236d74c BP |
1611 | static void idetape_create_locate_cmd(ide_drive_t *drive, |
1612 | struct ide_atapi_pc *pc, | |
5a04cfa9 | 1613 | unsigned int block, u8 partition, int skip) |
1da177e4 LT |
1614 | { |
1615 | idetape_init_pc(pc); | |
90699ce2 | 1616 | pc->c[0] = POSITION_TO_ELEMENT; |
1da177e4 | 1617 | pc->c[1] = 2; |
860ff5ec | 1618 | put_unaligned(cpu_to_be32(block), (unsigned int *) &pc->c[3]); |
1da177e4 | 1619 | pc->c[8] = partition; |
346331f8 | 1620 | pc->flags |= PC_FLAG_WAIT_FOR_DSC; |
d236d74c | 1621 | pc->idetape_callback = &idetape_pc_callback; |
1da177e4 LT |
1622 | } |
1623 | ||
d236d74c BP |
1624 | static int idetape_create_prevent_cmd(ide_drive_t *drive, |
1625 | struct ide_atapi_pc *pc, int prevent) | |
1da177e4 LT |
1626 | { |
1627 | idetape_tape_t *tape = drive->driver_data; | |
1628 | ||
b6422013 BP |
1629 | /* device supports locking according to capabilities page */ |
1630 | if (!(tape->caps[6] & 0x01)) | |
1da177e4 LT |
1631 | return 0; |
1632 | ||
1633 | idetape_init_pc(pc); | |
90699ce2 | 1634 | pc->c[0] = ALLOW_MEDIUM_REMOVAL; |
1da177e4 | 1635 | pc->c[4] = prevent; |
d236d74c | 1636 | pc->idetape_callback = &idetape_pc_callback; |
1da177e4 LT |
1637 | return 1; |
1638 | } | |
1639 | ||
5a04cfa9 | 1640 | static int __idetape_discard_read_pipeline(ide_drive_t *drive) |
1da177e4 LT |
1641 | { |
1642 | idetape_tape_t *tape = drive->driver_data; | |
1da177e4 | 1643 | |
54abf37e | 1644 | if (tape->chrdev_dir != IDETAPE_DIR_READ) |
1da177e4 LT |
1645 | return 0; |
1646 | ||
83042b24 | 1647 | clear_bit(IDETAPE_FLAG_FILEMARK, &tape->flags); |
1da177e4 LT |
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 | ||
54abf37e | 1654 | tape->chrdev_dir = IDETAPE_DIR_NONE; |
1da177e4 | 1655 | |
83042b24 | 1656 | return 0; |
1da177e4 LT |
1657 | } |
1658 | ||
1659 | /* | |
3c98bf34 BP |
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. | |
1da177e4 | 1664 | */ |
5a04cfa9 BP |
1665 | static int idetape_position_tape(ide_drive_t *drive, unsigned int block, |
1666 | u8 partition, int skip) | |
1da177e4 LT |
1667 | { |
1668 | idetape_tape_t *tape = drive->driver_data; | |
1669 | int retval; | |
d236d74c | 1670 | struct ide_atapi_pc pc; |
1da177e4 | 1671 | |
54abf37e | 1672 | if (tape->chrdev_dir == IDETAPE_DIR_READ) |
1da177e4 LT |
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 | ||
5a04cfa9 BP |
1684 | static void idetape_discard_read_pipeline(ide_drive_t *drive, |
1685 | int restore_position) | |
1da177e4 LT |
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)) { | |
5a04cfa9 BP |
1696 | printk(KERN_INFO "ide-tape: %s: position_tape failed in" |
1697 | " discard_pipeline()\n", tape->name); | |
1da177e4 LT |
1698 | return; |
1699 | } | |
1700 | } | |
1701 | } | |
1702 | ||
1703 | /* | |
3c98bf34 BP |
1704 | * Generate a read/write request for the block device interface and wait for it |
1705 | * to be serviced. | |
1da177e4 | 1706 | */ |
5a04cfa9 BP |
1707 | static int idetape_queue_rw_tail(ide_drive_t *drive, int cmd, int blocks, |
1708 | struct idetape_bh *bh) | |
1da177e4 LT |
1709 | { |
1710 | idetape_tape_t *tape = drive->driver_data; | |
1711 | struct request rq; | |
1712 | ||
8004a8c9 BP |
1713 | debug_log(DBG_SENSE, "%s: cmd=%d\n", __func__, cmd); |
1714 | ||
1da177e4 LT |
1715 | idetape_init_rq(&rq, cmd); |
1716 | rq.rq_disk = tape->disk; | |
1717 | rq.special = (void *)bh; | |
54bb2074 | 1718 | rq.sector = tape->first_frame; |
5a04cfa9 BP |
1719 | rq.nr_sectors = blocks; |
1720 | rq.current_nr_sectors = blocks; | |
1da177e4 LT |
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; | |
54bb2074 | 1730 | return (tape->blk_size * (blocks-rq.current_nr_sectors)); |
1da177e4 LT |
1731 | } |
1732 | ||
d236d74c | 1733 | static void idetape_create_inquiry_cmd(struct ide_atapi_pc *pc) |
1da177e4 LT |
1734 | { |
1735 | idetape_init_pc(pc); | |
90699ce2 | 1736 | pc->c[0] = INQUIRY; |
5a04cfa9 | 1737 | pc->c[4] = 254; |
d236d74c BP |
1738 | pc->req_xfer = 254; |
1739 | pc->idetape_callback = &idetape_pc_callback; | |
1da177e4 LT |
1740 | } |
1741 | ||
d236d74c BP |
1742 | static void idetape_create_rewind_cmd(ide_drive_t *drive, |
1743 | struct ide_atapi_pc *pc) | |
1da177e4 LT |
1744 | { |
1745 | idetape_init_pc(pc); | |
90699ce2 | 1746 | pc->c[0] = REZERO_UNIT; |
346331f8 | 1747 | pc->flags |= PC_FLAG_WAIT_FOR_DSC; |
d236d74c | 1748 | pc->idetape_callback = &idetape_pc_callback; |
1da177e4 LT |
1749 | } |
1750 | ||
d236d74c | 1751 | static void idetape_create_erase_cmd(struct ide_atapi_pc *pc) |
1da177e4 LT |
1752 | { |
1753 | idetape_init_pc(pc); | |
90699ce2 | 1754 | pc->c[0] = ERASE; |
1da177e4 | 1755 | pc->c[1] = 1; |
346331f8 | 1756 | pc->flags |= PC_FLAG_WAIT_FOR_DSC; |
d236d74c | 1757 | pc->idetape_callback = &idetape_pc_callback; |
1da177e4 LT |
1758 | } |
1759 | ||
d236d74c | 1760 | static void idetape_create_space_cmd(struct ide_atapi_pc *pc, int count, u8 cmd) |
1da177e4 LT |
1761 | { |
1762 | idetape_init_pc(pc); | |
90699ce2 | 1763 | pc->c[0] = SPACE; |
860ff5ec | 1764 | put_unaligned(cpu_to_be32(count), (unsigned int *) &pc->c[1]); |
1da177e4 | 1765 | pc->c[1] = cmd; |
346331f8 | 1766 | pc->flags |= PC_FLAG_WAIT_FOR_DSC; |
d236d74c | 1767 | pc->idetape_callback = &idetape_pc_callback; |
1da177e4 LT |
1768 | } |
1769 | ||
97c566ce | 1770 | /* Queue up a character device originated write request. */ |
5a04cfa9 | 1771 | static int idetape_add_chrdev_write_request(ide_drive_t *drive, int blocks) |
1da177e4 LT |
1772 | { |
1773 | idetape_tape_t *tape = drive->driver_data; | |
1da177e4 | 1774 | |
8004a8c9 | 1775 | debug_log(DBG_CHRDEV, "Enter %s\n", __func__); |
1da177e4 | 1776 | |
0aa4b01e BP |
1777 | return idetape_queue_rw_tail(drive, REQ_IDETAPE_WRITE, |
1778 | blocks, tape->merge_stage->bh); | |
1da177e4 LT |
1779 | } |
1780 | ||
5a04cfa9 | 1781 | static void idetape_empty_write_pipeline(ide_drive_t *drive) |
1da177e4 LT |
1782 | { |
1783 | idetape_tape_t *tape = drive->driver_data; | |
1784 | int blocks, min; | |
1785 | struct idetape_bh *bh; | |
55a5d291 | 1786 | |
54abf37e | 1787 | if (tape->chrdev_dir != IDETAPE_DIR_WRITE) { |
5a04cfa9 BP |
1788 | printk(KERN_ERR "ide-tape: bug: Trying to empty write pipeline," |
1789 | " but we are not writing.\n"); | |
1da177e4 LT |
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 | } | |
1da177e4 | 1796 | if (tape->merge_stage_size) { |
54bb2074 BP |
1797 | blocks = tape->merge_stage_size / tape->blk_size; |
1798 | if (tape->merge_stage_size % tape->blk_size) { | |
1da177e4 LT |
1799 | unsigned int i; |
1800 | ||
1801 | blocks++; | |
54bb2074 BP |
1802 | i = tape->blk_size - tape->merge_stage_size % |
1803 | tape->blk_size; | |
1da177e4 LT |
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) { | |
5a04cfa9 BP |
1812 | printk(KERN_INFO "ide-tape: bug," |
1813 | " bh NULL\n"); | |
1da177e4 LT |
1814 | break; |
1815 | } | |
5a04cfa9 BP |
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); | |
1da177e4 LT |
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 | } | |
1da177e4 LT |
1828 | if (tape->merge_stage != NULL) { |
1829 | __idetape_kfree_stage(tape->merge_stage); | |
1830 | tape->merge_stage = NULL; | |
1831 | } | |
54abf37e | 1832 | tape->chrdev_dir = IDETAPE_DIR_NONE; |
1da177e4 LT |
1833 | } |
1834 | ||
83042b24 | 1835 | static int idetape_init_read(ide_drive_t *drive) |
1da177e4 LT |
1836 | { |
1837 | idetape_tape_t *tape = drive->driver_data; | |
1da177e4 | 1838 | int bytes_read; |
1da177e4 LT |
1839 | |
1840 | /* Initialize read operation */ | |
54abf37e BP |
1841 | if (tape->chrdev_dir != IDETAPE_DIR_READ) { |
1842 | if (tape->chrdev_dir == IDETAPE_DIR_WRITE) { | |
1da177e4 LT |
1843 | idetape_empty_write_pipeline(drive); |
1844 | idetape_flush_tape_buffers(drive); | |
1845 | } | |
1da177e4 | 1846 | if (tape->merge_stage || tape->merge_stage_size) { |
5a04cfa9 BP |
1847 | printk(KERN_ERR "ide-tape: merge_stage_size should be" |
1848 | " 0 now\n"); | |
1da177e4 LT |
1849 | tape->merge_stage_size = 0; |
1850 | } | |
5a04cfa9 BP |
1851 | tape->merge_stage = __idetape_kmalloc_stage(tape, 0, 0); |
1852 | if (!tape->merge_stage) | |
1da177e4 | 1853 | return -ENOMEM; |
54abf37e | 1854 | tape->chrdev_dir = IDETAPE_DIR_READ; |
1da177e4 LT |
1855 | |
1856 | /* | |
3c98bf34 BP |
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. | |
1da177e4 LT |
1861 | */ |
1862 | if (drive->dsc_overlap) { | |
5a04cfa9 BP |
1863 | bytes_read = idetape_queue_rw_tail(drive, |
1864 | REQ_IDETAPE_READ, 0, | |
1865 | tape->merge_stage->bh); | |
1da177e4 LT |
1866 | if (bytes_read < 0) { |
1867 | __idetape_kfree_stage(tape->merge_stage); | |
1868 | tape->merge_stage = NULL; | |
54abf37e | 1869 | tape->chrdev_dir = IDETAPE_DIR_NONE; |
1da177e4 LT |
1870 | return bytes_read; |
1871 | } | |
1872 | } | |
1873 | } | |
5e69bd95 | 1874 | |
1da177e4 LT |
1875 | return 0; |
1876 | } | |
1877 | ||
1878 | /* | |
3c98bf34 BP |
1879 | * Called from idetape_chrdev_read() to service a character device read request |
1880 | * and add read-ahead requests to our pipeline. | |
1da177e4 | 1881 | */ |
5a04cfa9 | 1882 | static int idetape_add_chrdev_read_request(ide_drive_t *drive, int blocks) |
1da177e4 LT |
1883 | { |
1884 | idetape_tape_t *tape = drive->driver_data; | |
1da177e4 | 1885 | |
8004a8c9 | 1886 | debug_log(DBG_PROCS, "Enter %s, %d blocks\n", __func__, blocks); |
1da177e4 | 1887 | |
3c98bf34 | 1888 | /* If we are at a filemark, return a read length of 0 */ |
346331f8 | 1889 | if (test_bit(IDETAPE_FLAG_FILEMARK, &tape->flags)) |
1da177e4 LT |
1890 | return 0; |
1891 | ||
83042b24 | 1892 | idetape_init_read(drive); |
5e69bd95 | 1893 | |
5e69bd95 BP |
1894 | return idetape_queue_rw_tail(drive, REQ_IDETAPE_READ, blocks, |
1895 | tape->merge_stage->bh); | |
1da177e4 LT |
1896 | } |
1897 | ||
5a04cfa9 | 1898 | static void idetape_pad_zeros(ide_drive_t *drive, int bcount) |
1da177e4 LT |
1899 | { |
1900 | idetape_tape_t *tape = drive->driver_data; | |
1901 | struct idetape_bh *bh; | |
1902 | int blocks; | |
3c98bf34 | 1903 | |
1da177e4 LT |
1904 | while (bcount) { |
1905 | unsigned int count; | |
1906 | ||
1907 | bh = tape->merge_stage->bh; | |
1908 | count = min(tape->stage_size, bcount); | |
1909 | bcount -= count; | |
54bb2074 | 1910 | blocks = count / tape->blk_size; |
1da177e4 | 1911 | while (count) { |
5a04cfa9 BP |
1912 | atomic_set(&bh->b_count, |
1913 | min(count, (unsigned int)bh->b_size)); | |
1da177e4 LT |
1914 | memset(bh->b_data, 0, atomic_read(&bh->b_count)); |
1915 | count -= atomic_read(&bh->b_count); | |
1916 | bh = bh->b_reqnext; | |
1917 | } | |
5a04cfa9 BP |
1918 | idetape_queue_rw_tail(drive, REQ_IDETAPE_WRITE, blocks, |
1919 | tape->merge_stage->bh); | |
1da177e4 LT |
1920 | } |
1921 | } | |
1922 | ||
1da177e4 | 1923 | /* |
3c98bf34 BP |
1924 | * Rewinds the tape to the Beginning Of the current Partition (BOP). We |
1925 | * currently support only one partition. | |
1926 | */ | |
5a04cfa9 | 1927 | static int idetape_rewind_tape(ide_drive_t *drive) |
1da177e4 LT |
1928 | { |
1929 | int retval; | |
d236d74c | 1930 | struct ide_atapi_pc pc; |
8004a8c9 BP |
1931 | idetape_tape_t *tape; |
1932 | tape = drive->driver_data; | |
1933 | ||
1934 | debug_log(DBG_SENSE, "Enter %s\n", __func__); | |
1935 | ||
1da177e4 LT |
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 | ||
3c98bf34 | 1948 | /* mtio.h compatible commands should be issued to the chrdev interface. */ |
5a04cfa9 BP |
1949 | static int idetape_blkdev_ioctl(ide_drive_t *drive, unsigned int cmd, |
1950 | unsigned long arg) | |
1da177e4 LT |
1951 | { |
1952 | idetape_tape_t *tape = drive->driver_data; | |
1da177e4 LT |
1953 | void __user *argp = (void __user *)arg; |
1954 | ||
d59823fa BP |
1955 | struct idetape_config { |
1956 | int dsc_rw_frequency; | |
1957 | int dsc_media_access_frequency; | |
1958 | int nr_stages; | |
1959 | } config; | |
1960 | ||
8004a8c9 BP |
1961 | debug_log(DBG_PROCS, "Enter %s\n", __func__); |
1962 | ||
1da177e4 | 1963 | switch (cmd) { |
5a04cfa9 BP |
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; | |
5a04cfa9 BP |
1968 | break; |
1969 | case 0x0350: | |
1970 | config.dsc_rw_frequency = (int) tape->best_dsc_rw_freq; | |
83042b24 | 1971 | config.nr_stages = 1; |
5a04cfa9 BP |
1972 | if (copy_to_user(argp, &config, sizeof(config))) |
1973 | return -EFAULT; | |
1974 | break; | |
1975 | default: | |
1976 | return -EIO; | |
1da177e4 LT |
1977 | } |
1978 | return 0; | |
1979 | } | |
1980 | ||
5a04cfa9 BP |
1981 | static int idetape_space_over_filemarks(ide_drive_t *drive, short mt_op, |
1982 | int mt_count) | |
1da177e4 LT |
1983 | { |
1984 | idetape_tape_t *tape = drive->driver_data; | |
d236d74c | 1985 | struct ide_atapi_pc pc; |
5a04cfa9 | 1986 | int retval, count = 0; |
b6422013 | 1987 | int sprev = !!(tape->caps[4] & 0x20); |
1da177e4 LT |
1988 | |
1989 | if (mt_count == 0) | |
1990 | return 0; | |
1991 | if (MTBSF == mt_op || MTBSFM == mt_op) { | |
b6422013 | 1992 | if (!sprev) |
1da177e4 | 1993 | return -EIO; |
5a04cfa9 | 1994 | mt_count = -mt_count; |
1da177e4 LT |
1995 | } |
1996 | ||
54abf37e | 1997 | if (tape->chrdev_dir == IDETAPE_DIR_READ) { |
1da177e4 | 1998 | tape->merge_stage_size = 0; |
346331f8 | 1999 | if (test_and_clear_bit(IDETAPE_FLAG_FILEMARK, &tape->flags)) |
1da177e4 | 2000 | ++count; |
1da177e4 LT |
2001 | idetape_discard_read_pipeline(drive, 0); |
2002 | } | |
2003 | ||
2004 | /* | |
3c98bf34 BP |
2005 | * The filemark was not found in our internal pipeline; now we can issue |
2006 | * the space command. | |
1da177e4 LT |
2007 | */ |
2008 | switch (mt_op) { | |
5a04cfa9 BP |
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; | |
1da177e4 LT |
2028 | } |
2029 | } | |
2030 | ||
1da177e4 | 2031 | /* |
3c98bf34 | 2032 | * Our character device read / write functions. |
1da177e4 | 2033 | * |
3c98bf34 BP |
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). | |
1da177e4 | 2037 | * |
3c98bf34 BP |
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. | |
1da177e4 | 2045 | */ |
5a04cfa9 BP |
2046 | static ssize_t idetape_chrdev_read(struct file *file, char __user *buf, |
2047 | size_t count, loff_t *ppos) | |
1da177e4 LT |
2048 | { |
2049 | struct ide_tape_obj *tape = ide_tape_f(file); | |
2050 | ide_drive_t *drive = tape->drive; | |
5a04cfa9 | 2051 | ssize_t bytes_read, temp, actually_read = 0, rc; |
dcd96379 | 2052 | ssize_t ret = 0; |
b6422013 | 2053 | u16 ctl = *(u16 *)&tape->caps[12]; |
1da177e4 | 2054 | |
8004a8c9 | 2055 | debug_log(DBG_CHRDEV, "Enter %s, count %Zd\n", __func__, count); |
1da177e4 | 2056 | |
54abf37e | 2057 | if (tape->chrdev_dir != IDETAPE_DIR_READ) { |
346331f8 | 2058 | if (test_bit(IDETAPE_FLAG_DETECT_BS, &tape->flags)) |
54bb2074 BP |
2059 | if (count > tape->blk_size && |
2060 | (count % tape->blk_size) == 0) | |
2061 | tape->user_bs_factor = count / tape->blk_size; | |
1da177e4 | 2062 | } |
83042b24 | 2063 | rc = idetape_init_read(drive); |
8d06bfad | 2064 | if (rc < 0) |
1da177e4 LT |
2065 | return rc; |
2066 | if (count == 0) | |
2067 | return (0); | |
2068 | if (tape->merge_stage_size) { | |
5a04cfa9 BP |
2069 | actually_read = min((unsigned int)(tape->merge_stage_size), |
2070 | (unsigned int)count); | |
99d74e61 | 2071 | if (idetape_copy_stage_to_user(tape, buf, actually_read)) |
dcd96379 | 2072 | ret = -EFAULT; |
1da177e4 LT |
2073 | buf += actually_read; |
2074 | tape->merge_stage_size -= actually_read; | |
2075 | count -= actually_read; | |
2076 | } | |
2077 | while (count >= tape->stage_size) { | |
b6422013 | 2078 | bytes_read = idetape_add_chrdev_read_request(drive, ctl); |
1da177e4 LT |
2079 | if (bytes_read <= 0) |
2080 | goto finish; | |
99d74e61 | 2081 | if (idetape_copy_stage_to_user(tape, buf, bytes_read)) |
dcd96379 | 2082 | ret = -EFAULT; |
1da177e4 LT |
2083 | buf += bytes_read; |
2084 | count -= bytes_read; | |
2085 | actually_read += bytes_read; | |
2086 | } | |
2087 | if (count) { | |
b6422013 | 2088 | bytes_read = idetape_add_chrdev_read_request(drive, ctl); |
1da177e4 LT |
2089 | if (bytes_read <= 0) |
2090 | goto finish; | |
2091 | temp = min((unsigned long)count, (unsigned long)bytes_read); | |
99d74e61 | 2092 | if (idetape_copy_stage_to_user(tape, buf, temp)) |
dcd96379 | 2093 | ret = -EFAULT; |
1da177e4 LT |
2094 | actually_read += temp; |
2095 | tape->merge_stage_size = bytes_read-temp; | |
2096 | } | |
2097 | finish: | |
346331f8 | 2098 | if (!actually_read && test_bit(IDETAPE_FLAG_FILEMARK, &tape->flags)) { |
8004a8c9 BP |
2099 | debug_log(DBG_SENSE, "%s: spacing over filemark\n", tape->name); |
2100 | ||
1da177e4 LT |
2101 | idetape_space_over_filemarks(drive, MTFSF, 1); |
2102 | return 0; | |
2103 | } | |
dcd96379 | 2104 | |
5a04cfa9 | 2105 | return ret ? ret : actually_read; |
1da177e4 LT |
2106 | } |
2107 | ||
5a04cfa9 | 2108 | static ssize_t idetape_chrdev_write(struct file *file, const char __user *buf, |
1da177e4 LT |
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; | |
dcd96379 DW |
2113 | ssize_t actually_written = 0; |
2114 | ssize_t ret = 0; | |
b6422013 | 2115 | u16 ctl = *(u16 *)&tape->caps[12]; |
1da177e4 LT |
2116 | |
2117 | /* The drive is write protected. */ | |
2118 | if (tape->write_prot) | |
2119 | return -EACCES; | |
2120 | ||
8004a8c9 | 2121 | debug_log(DBG_CHRDEV, "Enter %s, count %Zd\n", __func__, count); |
1da177e4 LT |
2122 | |
2123 | /* Initialize write operation */ | |
54abf37e BP |
2124 | if (tape->chrdev_dir != IDETAPE_DIR_WRITE) { |
2125 | if (tape->chrdev_dir == IDETAPE_DIR_READ) | |
1da177e4 | 2126 | idetape_discard_read_pipeline(drive, 1); |
1da177e4 LT |
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 | } | |
5a04cfa9 BP |
2132 | tape->merge_stage = __idetape_kmalloc_stage(tape, 0, 0); |
2133 | if (!tape->merge_stage) | |
1da177e4 | 2134 | return -ENOMEM; |
54abf37e | 2135 | tape->chrdev_dir = IDETAPE_DIR_WRITE; |
1da177e4 LT |
2136 | idetape_init_merge_stage(tape); |
2137 | ||
2138 | /* | |
3c98bf34 BP |
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. | |
1da177e4 LT |
2143 | */ |
2144 | if (drive->dsc_overlap) { | |
5a04cfa9 BP |
2145 | ssize_t retval = idetape_queue_rw_tail(drive, |
2146 | REQ_IDETAPE_WRITE, 0, | |
2147 | tape->merge_stage->bh); | |
1da177e4 LT |
2148 | if (retval < 0) { |
2149 | __idetape_kfree_stage(tape->merge_stage); | |
2150 | tape->merge_stage = NULL; | |
54abf37e | 2151 | tape->chrdev_dir = IDETAPE_DIR_NONE; |
1da177e4 LT |
2152 | return retval; |
2153 | } | |
2154 | } | |
2155 | } | |
2156 | if (count == 0) | |
2157 | return (0); | |
1da177e4 | 2158 | if (tape->merge_stage_size) { |
1da177e4 | 2159 | if (tape->merge_stage_size >= tape->stage_size) { |
5a04cfa9 | 2160 | printk(KERN_ERR "ide-tape: bug: merge buf too big\n"); |
1da177e4 LT |
2161 | tape->merge_stage_size = 0; |
2162 | } | |
5a04cfa9 BP |
2163 | actually_written = min((unsigned int) |
2164 | (tape->stage_size - tape->merge_stage_size), | |
2165 | (unsigned int)count); | |
8646c88f | 2166 | if (idetape_copy_stage_from_user(tape, buf, actually_written)) |
dcd96379 | 2167 | ret = -EFAULT; |
1da177e4 LT |
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) { | |
dcd96379 | 2173 | ssize_t retval; |
1da177e4 | 2174 | tape->merge_stage_size = 0; |
b6422013 | 2175 | retval = idetape_add_chrdev_write_request(drive, ctl); |
1da177e4 LT |
2176 | if (retval <= 0) |
2177 | return (retval); | |
2178 | } | |
2179 | } | |
2180 | while (count >= tape->stage_size) { | |
dcd96379 | 2181 | ssize_t retval; |
8646c88f | 2182 | if (idetape_copy_stage_from_user(tape, buf, tape->stage_size)) |
dcd96379 | 2183 | ret = -EFAULT; |
1da177e4 LT |
2184 | buf += tape->stage_size; |
2185 | count -= tape->stage_size; | |
b6422013 | 2186 | retval = idetape_add_chrdev_write_request(drive, ctl); |
1da177e4 LT |
2187 | actually_written += tape->stage_size; |
2188 | if (retval <= 0) | |
2189 | return (retval); | |
2190 | } | |
2191 | if (count) { | |
2192 | actually_written += count; | |
8646c88f | 2193 | if (idetape_copy_stage_from_user(tape, buf, count)) |
dcd96379 | 2194 | ret = -EFAULT; |
1da177e4 LT |
2195 | tape->merge_stage_size += count; |
2196 | } | |
5a04cfa9 | 2197 | return ret ? ret : actually_written; |
1da177e4 LT |
2198 | } |
2199 | ||
5a04cfa9 | 2200 | static int idetape_write_filemark(ide_drive_t *drive) |
1da177e4 | 2201 | { |
d236d74c | 2202 | struct ide_atapi_pc pc; |
1da177e4 LT |
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 | /* | |
d99c9da2 BP |
2214 | * Called from idetape_chrdev_ioctl when the general mtio MTIOCTOP ioctl is |
2215 | * requested. | |
1da177e4 | 2216 | * |
d99c9da2 BP |
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). | |
1da177e4 | 2221 | * |
d99c9da2 | 2222 | * The following commands are currently not supported: |
1da177e4 | 2223 | * |
d99c9da2 BP |
2224 | * MTFSS, MTBSS, MTWSM, MTSETDENSITY, MTSETDRVBUFFER, MT_ST_BOOLEANS, |
2225 | * MT_ST_WRITE_THRESHOLD. | |
1da177e4 | 2226 | */ |
d99c9da2 | 2227 | static int idetape_mtioctop(ide_drive_t *drive, short mt_op, int mt_count) |
1da177e4 LT |
2228 | { |
2229 | idetape_tape_t *tape = drive->driver_data; | |
d236d74c | 2230 | struct ide_atapi_pc pc; |
5a04cfa9 | 2231 | int i, retval; |
1da177e4 | 2232 | |
8004a8c9 BP |
2233 | debug_log(DBG_ERR, "Handling MTIOCTOP ioctl: mt_op=%d, mt_count=%d\n", |
2234 | mt_op, mt_count); | |
5a04cfa9 | 2235 | |
3c98bf34 | 2236 | /* Commands which need our pipelined read-ahead stages. */ |
1da177e4 | 2237 | switch (mt_op) { |
5a04cfa9 BP |
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; | |
1da177e4 | 2247 | } |
5a04cfa9 | 2248 | |
1da177e4 | 2249 | switch (mt_op) { |
5a04cfa9 BP |
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) | |
346331f8 | 2286 | clear_bit(IDETAPE_FLAG_MEDIUM_PRESENT, &tape->flags); |
5a04cfa9 BP |
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) | |
1da177e4 | 2307 | return -EIO; |
5a04cfa9 | 2308 | tape->user_bs_factor = mt_count / tape->blk_size; |
346331f8 | 2309 | clear_bit(IDETAPE_FLAG_DETECT_BS, &tape->flags); |
5a04cfa9 | 2310 | } else |
346331f8 | 2311 | set_bit(IDETAPE_FLAG_DETECT_BS, &tape->flags); |
5a04cfa9 BP |
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)) | |
1da177e4 | 2324 | return 0; |
5a04cfa9 BP |
2325 | retval = idetape_queue_pc_tail(drive, &pc); |
2326 | if (retval) | |
1da177e4 | 2327 | return retval; |
5a04cfa9 BP |
2328 | tape->door_locked = DOOR_EXPLICITLY_LOCKED; |
2329 | return 0; | |
2330 | case MTUNLOCK: | |
2331 | if (!idetape_create_prevent_cmd(drive, &pc, 0)) | |
1da177e4 | 2332 | return 0; |
5a04cfa9 BP |
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; | |
1da177e4 LT |
2342 | } |
2343 | } | |
2344 | ||
2345 | /* | |
d99c9da2 BP |
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. | |
1da177e4 | 2349 | */ |
d99c9da2 BP |
2350 | static int idetape_chrdev_ioctl(struct inode *inode, struct file *file, |
2351 | unsigned int cmd, unsigned long arg) | |
1da177e4 LT |
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; | |
54bb2074 | 2358 | int block_offset = 0, position = tape->first_frame; |
1da177e4 LT |
2359 | void __user *argp = (void __user *)arg; |
2360 | ||
8004a8c9 | 2361 | debug_log(DBG_CHRDEV, "Enter %s, cmd=%u\n", __func__, cmd); |
1da177e4 | 2362 | |
54abf37e | 2363 | if (tape->chrdev_dir == IDETAPE_DIR_WRITE) { |
1da177e4 LT |
2364 | idetape_empty_write_pipeline(drive); |
2365 | idetape_flush_tape_buffers(drive); | |
2366 | } | |
2367 | if (cmd == MTIOCGET || cmd == MTIOCPOS) { | |
b361acb1 | 2368 | block_offset = tape->merge_stage_size / |
54bb2074 | 2369 | (tape->blk_size * tape->user_bs_factor); |
5a04cfa9 BP |
2370 | position = idetape_read_position(drive); |
2371 | if (position < 0) | |
1da177e4 LT |
2372 | return -EIO; |
2373 | } | |
2374 | switch (cmd) { | |
5a04cfa9 BP |
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; | |
2386 | ||
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); | |
1da177e4 LT |
2402 | } |
2403 | } | |
2404 | ||
3cffb9ce BP |
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; | |
d236d74c | 2412 | struct ide_atapi_pc pc; |
3cffb9ce BP |
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"); | |
54bb2074 | 2417 | if (tape->blk_size == 0) { |
3cffb9ce BP |
2418 | printk(KERN_WARNING "ide-tape: Cannot deal with zero " |
2419 | "block size, assuming 32k\n"); | |
54bb2074 | 2420 | tape->blk_size = 32768; |
3cffb9ce BP |
2421 | } |
2422 | return; | |
2423 | } | |
d236d74c BP |
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; | |
3cffb9ce | 2428 | } |
1da177e4 | 2429 | |
5a04cfa9 | 2430 | static int idetape_chrdev_open(struct inode *inode, struct file *filp) |
1da177e4 LT |
2431 | { |
2432 | unsigned int minor = iminor(inode), i = minor & ~0xc0; | |
2433 | ide_drive_t *drive; | |
2434 | idetape_tape_t *tape; | |
d236d74c | 2435 | struct ide_atapi_pc pc; |
1da177e4 LT |
2436 | int retval; |
2437 | ||
8004a8c9 BP |
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 | ||
1da177e4 LT |
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 | ||
1da177e4 LT |
2454 | drive = tape->drive; |
2455 | ||
2456 | filp->private_data = tape; | |
2457 | ||
346331f8 | 2458 | if (test_and_set_bit(IDETAPE_FLAG_BUSY, &tape->flags)) { |
1da177e4 LT |
2459 | retval = -EBUSY; |
2460 | goto out_put_tape; | |
2461 | } | |
2462 | ||
2463 | retval = idetape_wait_ready(drive, 60 * HZ); | |
2464 | if (retval) { | |
346331f8 | 2465 | clear_bit(IDETAPE_FLAG_BUSY, &tape->flags); |
1da177e4 LT |
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); | |
346331f8 | 2471 | if (!test_bit(IDETAPE_FLAG_ADDRESS_VALID, &tape->flags)) |
1da177e4 LT |
2472 | (void)idetape_rewind_tape(drive); |
2473 | ||
1da177e4 | 2474 | /* Read block size and write protect status from drive. */ |
3cffb9ce | 2475 | ide_tape_get_bsize_from_bdesc(drive); |
1da177e4 LT |
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) { | |
346331f8 | 2487 | clear_bit(IDETAPE_FLAG_BUSY, &tape->flags); |
1da177e4 LT |
2488 | retval = -EROFS; |
2489 | goto out_put_tape; | |
2490 | } | |
2491 | } | |
2492 | ||
3c98bf34 | 2493 | /* Lock the tape drive door so user can't eject. */ |
54abf37e | 2494 | if (tape->chrdev_dir == IDETAPE_DIR_NONE) { |
1da177e4 LT |
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 | } | |
1da177e4 LT |
2502 | return 0; |
2503 | ||
2504 | out_put_tape: | |
2505 | ide_tape_put(tape); | |
2506 | return retval; | |
2507 | } | |
2508 | ||
5a04cfa9 | 2509 | static void idetape_write_release(ide_drive_t *drive, unsigned int minor) |
1da177e4 LT |
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) { | |
54bb2074 BP |
2516 | idetape_pad_zeros(drive, tape->blk_size * |
2517 | (tape->user_bs_factor - 1)); | |
1da177e4 LT |
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 | ||
5a04cfa9 | 2526 | static int idetape_chrdev_release(struct inode *inode, struct file *filp) |
1da177e4 LT |
2527 | { |
2528 | struct ide_tape_obj *tape = ide_tape_f(filp); | |
2529 | ide_drive_t *drive = tape->drive; | |
d236d74c | 2530 | struct ide_atapi_pc pc; |
1da177e4 LT |
2531 | unsigned int minor = iminor(inode); |
2532 | ||
2533 | lock_kernel(); | |
2534 | tape = drive->driver_data; | |
8004a8c9 BP |
2535 | |
2536 | debug_log(DBG_CHRDEV, "Enter %s\n", __func__); | |
1da177e4 | 2537 | |
54abf37e | 2538 | if (tape->chrdev_dir == IDETAPE_DIR_WRITE) |
1da177e4 | 2539 | idetape_write_release(drive, minor); |
54abf37e | 2540 | if (tape->chrdev_dir == IDETAPE_DIR_READ) { |
1da177e4 LT |
2541 | if (minor < 128) |
2542 | idetape_discard_read_pipeline(drive, 1); | |
1da177e4 | 2543 | } |
f64eee7b | 2544 | |
346331f8 | 2545 | if (minor < 128 && test_bit(IDETAPE_FLAG_MEDIUM_PRESENT, &tape->flags)) |
1da177e4 | 2546 | (void) idetape_rewind_tape(drive); |
54abf37e | 2547 | if (tape->chrdev_dir == IDETAPE_DIR_NONE) { |
1da177e4 LT |
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 | } | |
346331f8 | 2555 | clear_bit(IDETAPE_FLAG_BUSY, &tape->flags); |
1da177e4 LT |
2556 | ide_tape_put(tape); |
2557 | unlock_kernel(); | |
2558 | return 0; | |
2559 | } | |
2560 | ||
2561 | /* | |
71071b8e | 2562 | * check the contents of the ATAPI IDENTIFY command results. We return: |
1da177e4 | 2563 | * |
71071b8e BP |
2564 | * 1 - If the tape can be supported by us, based on the information we have so |
2565 | * far. | |
1da177e4 | 2566 | * |
71071b8e | 2567 | * 0 - If this tape driver is not currently supported by us. |
1da177e4 | 2568 | */ |
71071b8e | 2569 | static int idetape_identify_device(ide_drive_t *drive) |
1da177e4 | 2570 | { |
71071b8e | 2571 | u8 gcw[2], protocol, device_type, removable, packet_size; |
1da177e4 LT |
2572 | |
2573 | if (drive->id_read == 0) | |
2574 | return 1; | |
2575 | ||
71071b8e | 2576 | *((unsigned short *) &gcw) = drive->id->config; |
1da177e4 | 2577 | |
71071b8e BP |
2578 | protocol = (gcw[1] & 0xC0) >> 6; |
2579 | device_type = gcw[1] & 0x1F; | |
2580 | removable = !!(gcw[0] & 0x80); | |
2581 | packet_size = gcw[0] & 0x3; | |
1da177e4 | 2582 | |
71071b8e BP |
2583 | /* Check that we can support this device */ |
2584 | if (protocol != 2) | |
16422de3 | 2585 | printk(KERN_ERR "ide-tape: Protocol (0x%02x) is not ATAPI\n", |
71071b8e BP |
2586 | protocol); |
2587 | else if (device_type != 1) | |
16422de3 | 2588 | printk(KERN_ERR "ide-tape: Device type (0x%02x) is not set " |
71071b8e BP |
2589 | "to tape\n", device_type); |
2590 | else if (!removable) | |
1da177e4 | 2591 | printk(KERN_ERR "ide-tape: The removable flag is not set\n"); |
71071b8e | 2592 | else if (packet_size != 0) { |
24d57f8b BP |
2593 | printk(KERN_ERR "ide-tape: Packet size (0x%02x) is not 12" |
2594 | " bytes\n", packet_size); | |
1da177e4 LT |
2595 | } else |
2596 | return 1; | |
2597 | return 0; | |
2598 | } | |
2599 | ||
6d29c8f0 | 2600 | static void idetape_get_inquiry_results(ide_drive_t *drive) |
1da177e4 | 2601 | { |
1da177e4 | 2602 | idetape_tape_t *tape = drive->driver_data; |
d236d74c | 2603 | struct ide_atapi_pc pc; |
41f81d54 | 2604 | char fw_rev[6], vendor_id[10], product_id[18]; |
6d29c8f0 | 2605 | |
1da177e4 LT |
2606 | idetape_create_inquiry_cmd(&pc); |
2607 | if (idetape_queue_pc_tail(drive, &pc)) { | |
6d29c8f0 BP |
2608 | printk(KERN_ERR "ide-tape: %s: can't get INQUIRY results\n", |
2609 | tape->name); | |
1da177e4 LT |
2610 | return; |
2611 | } | |
d236d74c BP |
2612 | memcpy(vendor_id, &pc.buf[8], 8); |
2613 | memcpy(product_id, &pc.buf[16], 16); | |
2614 | memcpy(fw_rev, &pc.buf[32], 4); | |
41f81d54 BP |
2615 | |
2616 | ide_fixstring(vendor_id, 10, 0); | |
2617 | ide_fixstring(product_id, 18, 0); | |
2618 | ide_fixstring(fw_rev, 6, 0); | |
2619 | ||
6d29c8f0 | 2620 | printk(KERN_INFO "ide-tape: %s <-> %s: %s %s rev %s\n", |
41f81d54 | 2621 | drive->name, tape->name, vendor_id, product_id, fw_rev); |
1da177e4 LT |
2622 | } |
2623 | ||
2624 | /* | |
b6422013 BP |
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. | |
1da177e4 | 2627 | */ |
5a04cfa9 | 2628 | static void idetape_get_mode_sense_results(ide_drive_t *drive) |
1da177e4 LT |
2629 | { |
2630 | idetape_tape_t *tape = drive->driver_data; | |
d236d74c | 2631 | struct ide_atapi_pc pc; |
b6422013 BP |
2632 | u8 *caps; |
2633 | u8 speed, max_speed; | |
47314fa4 | 2634 | |
1da177e4 LT |
2635 | idetape_create_mode_sense_cmd(&pc, IDETAPE_CAPABILITIES_PAGE); |
2636 | if (idetape_queue_pc_tail(drive, &pc)) { | |
b6422013 BP |
2637 | printk(KERN_ERR "ide-tape: Can't get tape parameters - assuming" |
2638 | " some default values\n"); | |
54bb2074 | 2639 | tape->blk_size = 512; |
b6422013 BP |
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]); | |
1da177e4 LT |
2643 | return; |
2644 | } | |
d236d74c | 2645 | caps = pc.buf + 4 + pc.buf[3]; |
b6422013 BP |
2646 | |
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]); | |
1da177e4 | 2650 | |
b6422013 BP |
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]); | |
1da177e4 | 2655 | |
b6422013 BP |
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]); | |
1da177e4 | 2660 | } |
b6422013 BP |
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]); | |
1da177e4 LT |
2665 | } |
2666 | ||
b6422013 BP |
2667 | memcpy(&tape->caps, caps, 20); |
2668 | if (caps[7] & 0x02) | |
54bb2074 | 2669 | tape->blk_size = 512; |
b6422013 | 2670 | else if (caps[7] & 0x04) |
54bb2074 | 2671 | tape->blk_size = 1024; |
1da177e4 LT |
2672 | } |
2673 | ||
7662d046 | 2674 | #ifdef CONFIG_IDE_PROC_FS |
5a04cfa9 | 2675 | static void idetape_add_settings(ide_drive_t *drive) |
1da177e4 LT |
2676 | { |
2677 | idetape_tape_t *tape = drive->driver_data; | |
2678 | ||
b6422013 BP |
2679 | ide_add_setting(drive, "buffer", SETTING_READ, TYPE_SHORT, 0, 0xffff, |
2680 | 1, 2, (u16 *)&tape->caps[16], NULL); | |
b6422013 BP |
2681 | ide_add_setting(drive, "speed", SETTING_READ, TYPE_SHORT, 0, 0xffff, |
2682 | 1, 1, (u16 *)&tape->caps[14], NULL); | |
54bb2074 BP |
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); | |
5a04cfa9 BP |
2688 | ide_add_setting(drive, "dsc_overlap", SETTING_RW, TYPE_BYTE, 0, 1, 1, |
2689 | 1, &drive->dsc_overlap, NULL); | |
5a04cfa9 BP |
2690 | ide_add_setting(drive, "avg_speed", SETTING_READ, TYPE_INT, 0, 0xffff, |
2691 | 1, 1, &tape->avg_speed, NULL); | |
8004a8c9 BP |
2692 | ide_add_setting(drive, "debug_mask", SETTING_RW, TYPE_INT, 0, 0xffff, 1, |
2693 | 1, &tape->debug_mask, NULL); | |
1da177e4 | 2694 | } |
7662d046 BZ |
2695 | #else |
2696 | static inline void idetape_add_settings(ide_drive_t *drive) { ; } | |
2697 | #endif | |
1da177e4 LT |
2698 | |
2699 | /* | |
3c98bf34 | 2700 | * The function below is called to: |
1da177e4 | 2701 | * |
3c98bf34 BP |
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. | |
1da177e4 | 2706 | * |
3c98bf34 BP |
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. | |
1da177e4 | 2709 | */ |
5a04cfa9 | 2710 | static void idetape_setup(ide_drive_t *drive, idetape_tape_t *tape, int minor) |
1da177e4 | 2711 | { |
83042b24 | 2712 | unsigned long t; |
1da177e4 | 2713 | int speed; |
1da177e4 | 2714 | int stage_size; |
71071b8e | 2715 | u8 gcw[2]; |
b6422013 | 2716 | u16 *ctl = (u16 *)&tape->caps[12]; |
1da177e4 | 2717 | |
54bb2074 | 2718 | spin_lock_init(&tape->lock); |
1da177e4 | 2719 | drive->dsc_overlap = 1; |
4166c199 BZ |
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; | |
1da177e4 | 2724 | } |
1da177e4 LT |
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; | |
54abf37e | 2732 | tape->chrdev_dir = IDETAPE_DIR_NONE; |
1da177e4 | 2733 | tape->pc = tape->pc_stack; |
1da177e4 | 2734 | *((unsigned short *) &gcw) = drive->id->config; |
71071b8e BP |
2735 | |
2736 | /* Command packet DRQ type */ | |
2737 | if (((gcw[0] & 0x60) >> 5) == 1) | |
346331f8 | 2738 | set_bit(IDETAPE_FLAG_DRQ_INTERRUPT, &tape->flags); |
1da177e4 | 2739 | |
1da177e4 LT |
2740 | idetape_get_inquiry_results(drive); |
2741 | idetape_get_mode_sense_results(drive); | |
3cffb9ce | 2742 | ide_tape_get_bsize_from_bdesc(drive); |
1da177e4 | 2743 | tape->user_bs_factor = 1; |
54bb2074 | 2744 | tape->stage_size = *ctl * tape->blk_size; |
1da177e4 LT |
2745 | while (tape->stage_size > 0xffff) { |
2746 | printk(KERN_NOTICE "ide-tape: decreasing stage size\n"); | |
b6422013 | 2747 | *ctl /= 2; |
54bb2074 | 2748 | tape->stage_size = *ctl * tape->blk_size; |
1da177e4 LT |
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 | ||
83042b24 | 2757 | /* select the "best" DSC read/write polling freq */ |
b6422013 | 2758 | speed = max(*(u16 *)&tape->caps[14], *(u16 *)&tape->caps[8]); |
1da177e4 | 2759 | |
83042b24 | 2760 | t = (IDETAPE_FIFO_THRESHOLD * tape->stage_size * HZ) / (speed * 1000); |
1da177e4 LT |
2761 | |
2762 | /* | |
3c98bf34 BP |
2763 | * Ensure that the number we got makes sense; limit it within |
2764 | * IDETAPE_DSC_RW_MIN and IDETAPE_DSC_RW_MAX. | |
1da177e4 | 2765 | */ |
54bb2074 BP |
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); | |
1da177e4 | 2769 | printk(KERN_INFO "ide-tape: %s <-> %s: %dKBps, %d*%dkB buffer, " |
83042b24 | 2770 | "%lums tDSC%s\n", |
b6422013 BP |
2771 | drive->name, tape->name, *(u16 *)&tape->caps[14], |
2772 | (*(u16 *)&tape->caps[16] * 512) / tape->stage_size, | |
1da177e4 | 2773 | tape->stage_size / 1024, |
54bb2074 | 2774 | tape->best_dsc_rw_freq * 1000 / HZ, |
1da177e4 LT |
2775 | drive->using_dma ? ", DMA":""); |
2776 | ||
2777 | idetape_add_settings(drive); | |
2778 | } | |
2779 | ||
4031bbe4 | 2780 | static void ide_tape_remove(ide_drive_t *drive) |
1da177e4 LT |
2781 | { |
2782 | idetape_tape_t *tape = drive->driver_data; | |
1da177e4 | 2783 | |
7662d046 | 2784 | ide_proc_unregister_driver(drive, tape->driver); |
1da177e4 LT |
2785 | |
2786 | ide_unregister_region(tape->disk); | |
2787 | ||
2788 | ide_tape_put(tape); | |
1da177e4 LT |
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 | ||
83042b24 | 2797 | BUG_ON(tape->merge_stage_size); |
8604affd | 2798 | |
1da177e4 LT |
2799 | drive->dsc_overlap = 0; |
2800 | drive->driver_data = NULL; | |
dbc1272e | 2801 | device_destroy(idetape_sysfs_class, MKDEV(IDETAPE_MAJOR, tape->minor)); |
5a04cfa9 BP |
2802 | device_destroy(idetape_sysfs_class, |
2803 | MKDEV(IDETAPE_MAJOR, tape->minor + 128)); | |
1da177e4 LT |
2804 | idetape_devs[tape->minor] = NULL; |
2805 | g->private_data = NULL; | |
2806 | put_disk(g); | |
2807 | kfree(tape); | |
2808 | } | |
2809 | ||
ecfd80e4 | 2810 | #ifdef CONFIG_IDE_PROC_FS |
1da177e4 LT |
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 | }; | |
1da177e4 LT |
2828 | #endif |
2829 | ||
4031bbe4 | 2830 | static int ide_tape_probe(ide_drive_t *); |
1da177e4 | 2831 | |
1da177e4 | 2832 | static ide_driver_t idetape_driver = { |
8604affd | 2833 | .gen_driver = { |
4ef3b8f4 | 2834 | .owner = THIS_MODULE, |
8604affd BZ |
2835 | .name = "ide-tape", |
2836 | .bus = &ide_bus_type, | |
8604affd | 2837 | }, |
4031bbe4 RK |
2838 | .probe = ide_tape_probe, |
2839 | .remove = ide_tape_remove, | |
1da177e4 LT |
2840 | .version = IDETAPE_VERSION, |
2841 | .media = ide_tape, | |
1da177e4 | 2842 | .supports_dsc_overlap = 1, |
1da177e4 LT |
2843 | .do_request = idetape_do_request, |
2844 | .end_request = idetape_end_request, | |
2845 | .error = __ide_error, | |
2846 | .abort = __ide_abort, | |
7662d046 | 2847 | #ifdef CONFIG_IDE_PROC_FS |
1da177e4 | 2848 | .proc = idetape_proc, |
7662d046 | 2849 | #endif |
1da177e4 LT |
2850 | }; |
2851 | ||
3c98bf34 | 2852 | /* Our character device supporting functions, passed to register_chrdev. */ |
2b8693c0 | 2853 | static const struct file_operations idetape_fops = { |
1da177e4 LT |
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; | |
1da177e4 | 2866 | |
5a04cfa9 BP |
2867 | tape = ide_tape_get(disk); |
2868 | if (!tape) | |
1da177e4 LT |
2869 | return -ENXIO; |
2870 | ||
1da177e4 LT |
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); | |
1da177e4 LT |
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 | ||
4031bbe4 | 2903 | static int ide_tape_probe(ide_drive_t *drive) |
1da177e4 LT |
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; | |
5a04cfa9 BP |
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); | |
1da177e4 LT |
2918 | goto failed; |
2919 | } | |
2920 | if (drive->scsi) { | |
5a04cfa9 BP |
2921 | printk(KERN_INFO "ide-tape: passing drive %s to ide-scsi" |
2922 | " emulation.\n", drive->name); | |
1da177e4 LT |
2923 | goto failed; |
2924 | } | |
5a04cfa9 | 2925 | tape = kzalloc(sizeof(idetape_tape_t), GFP_KERNEL); |
1da177e4 | 2926 | if (tape == NULL) { |
5a04cfa9 BP |
2927 | printk(KERN_ERR "ide-tape: %s: Can't allocate a tape struct\n", |
2928 | drive->name); | |
1da177e4 LT |
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 | ||
7662d046 | 2938 | ide_proc_register_driver(drive, &idetape_driver); |
1da177e4 | 2939 | |
1da177e4 LT |
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 | ||
cf8b8975 | 2950 | mutex_lock(&idetape_ref_mutex); |
1da177e4 LT |
2951 | for (minor = 0; idetape_devs[minor]; minor++) |
2952 | ; | |
2953 | idetape_devs[minor] = tape; | |
cf8b8975 | 2954 | mutex_unlock(&idetape_ref_mutex); |
1da177e4 LT |
2955 | |
2956 | idetape_setup(drive, tape, minor); | |
2957 | ||
dbc1272e TJ |
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); | |
d5dee80a | 2962 | |
1da177e4 LT |
2963 | g->fops = &idetape_block_ops; |
2964 | ide_register_region(g); | |
2965 | ||
2966 | return 0; | |
8604affd | 2967 | |
1da177e4 LT |
2968 | out_free_tape: |
2969 | kfree(tape); | |
2970 | failed: | |
8604affd | 2971 | return -ENODEV; |
1da177e4 LT |
2972 | } |
2973 | ||
5a04cfa9 | 2974 | static void __exit idetape_exit(void) |
1da177e4 | 2975 | { |
8604affd | 2976 | driver_unregister(&idetape_driver.gen_driver); |
d5dee80a | 2977 | class_destroy(idetape_sysfs_class); |
1da177e4 LT |
2978 | unregister_chrdev(IDETAPE_MAJOR, "ht"); |
2979 | } | |
2980 | ||
17514e8a | 2981 | static int __init idetape_init(void) |
1da177e4 | 2982 | { |
d5dee80a WD |
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 | ||
1da177e4 | 2992 | if (register_chrdev(IDETAPE_MAJOR, "ht", &idetape_fops)) { |
5a04cfa9 BP |
2993 | printk(KERN_ERR "ide-tape: Failed to register chrdev" |
2994 | " interface\n"); | |
d5dee80a WD |
2995 | error = -EBUSY; |
2996 | goto out_free_class; | |
1da177e4 | 2997 | } |
d5dee80a WD |
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; | |
1da177e4 LT |
3011 | } |
3012 | ||
263756ec | 3013 | MODULE_ALIAS("ide:*m-tape*"); |
1da177e4 LT |
3014 | module_init(idetape_init); |
3015 | module_exit(idetape_exit); | |
3016 | MODULE_ALIAS_CHARDEV_MAJOR(IDETAPE_MAJOR); | |
9c145768 BP |
3017 | MODULE_DESCRIPTION("ATAPI Streaming TAPE Driver"); |
3018 | MODULE_LICENSE("GPL"); |