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