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