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