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