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