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