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