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