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