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