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