blob: 5d23b70c68566585e795a79774336acd151994ab [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 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 * Setting IDETAPE_DEBUG_LOG to 1 will log driver flow control.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 *
111 * Setting them to 0 will restore normal operation mode:
112 *
113 * 1. Disable logging normal successful operations.
114 * 2. Disable self-sanity checks.
115 * 3. Errors will still be logged, of course.
116 *
117 * All the #if DEBUG code will be removed some day, when the driver
118 * is verified to be stable enough. This will make it much more
119 * esthetic.
120 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121#define IDETAPE_DEBUG_LOG 0
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122
123/*
124 * After each failed packet command we issue a request sense command
125 * and retry the packet command IDETAPE_MAX_PC_RETRIES times.
126 *
127 * Setting IDETAPE_MAX_PC_RETRIES to 0 will disable retries.
128 */
129#define IDETAPE_MAX_PC_RETRIES 3
130
131/*
132 * With each packet command, we allocate a buffer of
133 * IDETAPE_PC_BUFFER_SIZE bytes. This is used for several packet
134 * commands (Not for READ/WRITE commands).
135 */
136#define IDETAPE_PC_BUFFER_SIZE 256
137
138/*
139 * In various places in the driver, we need to allocate storage
140 * for packet commands and requests, which will remain valid while
141 * we leave the driver to wait for an interrupt or a timeout event.
142 */
143#define IDETAPE_PC_STACK (10 + IDETAPE_MAX_PC_RETRIES)
144
145/*
146 * Some drives (for example, Seagate STT3401A Travan) require a very long
147 * timeout, because they don't return an interrupt or clear their busy bit
148 * until after the command completes (even retension commands).
149 */
150#define IDETAPE_WAIT_CMD (900*HZ)
151
152/*
153 * The following parameter is used to select the point in the internal
154 * tape fifo in which we will start to refill the buffer. Decreasing
155 * the following parameter will improve the system's latency and
Robert P. J. Day3a4fa0a2007-10-19 23:10:43 +0200156 * interactive response, while using a high value might improve system
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 * throughput.
158 */
159#define IDETAPE_FIFO_THRESHOLD 2
160
161/*
162 * DSC polling parameters.
163 *
164 * Polling for DSC (a single bit in the status register) is a very
165 * important function in ide-tape. There are two cases in which we
166 * poll for DSC:
167 *
168 * 1. Before a read/write packet command, to ensure that we
169 * can transfer data from/to the tape's data buffers, without
170 * causing an actual media access. In case the tape is not
171 * ready yet, we take out our request from the device
172 * request queue, so that ide.c will service requests from
173 * the other device on the same interface meanwhile.
174 *
175 * 2. After the successful initialization of a "media access
176 * packet command", which is a command which can take a long
177 * time to complete (it can be several seconds or even an hour).
178 *
179 * Again, we postpone our request in the middle to free the bus
180 * for the other device. The polling frequency here should be
181 * lower than the read/write frequency since those media access
182 * commands are slow. We start from a "fast" frequency -
183 * IDETAPE_DSC_MA_FAST (one second), and if we don't receive DSC
184 * after IDETAPE_DSC_MA_THRESHOLD (5 minutes), we switch it to a
185 * lower frequency - IDETAPE_DSC_MA_SLOW (1 minute).
186 *
187 * We also set a timeout for the timer, in case something goes wrong.
188 * The timeout should be longer then the maximum execution time of a
189 * tape operation.
190 */
191
192/*
193 * DSC timings.
194 */
195#define IDETAPE_DSC_RW_MIN 5*HZ/100 /* 50 msec */
196#define IDETAPE_DSC_RW_MAX 40*HZ/100 /* 400 msec */
197#define IDETAPE_DSC_RW_TIMEOUT 2*60*HZ /* 2 minutes */
198#define IDETAPE_DSC_MA_FAST 2*HZ /* 2 seconds */
199#define IDETAPE_DSC_MA_THRESHOLD 5*60*HZ /* 5 minutes */
200#define IDETAPE_DSC_MA_SLOW 30*HZ /* 30 seconds */
201#define IDETAPE_DSC_MA_TIMEOUT 2*60*60*HZ /* 2 hours */
202
203/*************************** End of tunable parameters ***********************/
204
205/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 * Read/Write error simulation
207 */
208#define SIMULATE_ERRORS 0
209
210/*
211 * For general magnetic tape device compatibility.
212 */
213typedef enum {
214 idetape_direction_none,
215 idetape_direction_read,
216 idetape_direction_write
217} idetape_chrdev_direction_t;
218
219struct idetape_bh {
Stephen Rothwellab057962007-08-01 23:46:44 +0200220 u32 b_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 atomic_t b_count;
222 struct idetape_bh *b_reqnext;
223 char *b_data;
224};
225
226/*
227 * Our view of a packet command.
228 */
229typedef struct idetape_packet_command_s {
230 u8 c[12]; /* Actual packet bytes */
231 int retries; /* On each retry, we increment retries */
232 int error; /* Error code */
233 int request_transfer; /* Bytes to transfer */
234 int actually_transferred; /* Bytes actually transferred */
235 int buffer_size; /* Size of our data buffer */
236 struct idetape_bh *bh;
237 char *b_data;
238 int b_count;
239 u8 *buffer; /* Data buffer */
240 u8 *current_position; /* Pointer into the above buffer */
241 ide_startstop_t (*callback) (ide_drive_t *); /* Called when this packet command is completed */
242 u8 pc_buffer[IDETAPE_PC_BUFFER_SIZE]; /* Temporary buffer */
243 unsigned long flags; /* Status/Action bit flags: long for set_bit */
244} idetape_pc_t;
245
246/*
247 * Packet command flag bits.
248 */
249/* Set when an error is considered normal - We won't retry */
250#define PC_ABORT 0
251/* 1 When polling for DSC on a media access command */
252#define PC_WAIT_FOR_DSC 1
253/* 1 when we prefer to use DMA if possible */
254#define PC_DMA_RECOMMENDED 2
255/* 1 while DMA in progress */
256#define PC_DMA_IN_PROGRESS 3
257/* 1 when encountered problem during DMA */
258#define PC_DMA_ERROR 4
259/* Data direction */
260#define PC_WRITING 5
261
262/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 * Block Size Page
264 */
265typedef struct {
266 unsigned page_code :6; /* Page code - Should be 0x30 */
267 unsigned reserved1_6 :1;
268 unsigned ps :1;
269 __u8 page_length; /* Page Length - Should be 2 */
270 __u8 reserved2;
271 unsigned play32 :1;
272 unsigned play32_5 :1;
273 unsigned reserved2_23 :2;
274 unsigned record32 :1;
275 unsigned record32_5 :1;
276 unsigned reserved2_6 :1;
277 unsigned one :1;
278} idetape_block_size_page_t;
279
280/*
281 * A pipeline stage.
282 */
283typedef struct idetape_stage_s {
284 struct request rq; /* The corresponding request */
285 struct idetape_bh *bh; /* The data buffers */
286 struct idetape_stage_s *next; /* Pointer to the next stage */
287} idetape_stage_t;
288
289/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 * Most of our global data which we need to save even as we leave the
291 * driver due to an interrupt or a timer event is stored in a variable
292 * of type idetape_tape_t, defined below.
293 */
294typedef struct ide_tape_obj {
295 ide_drive_t *drive;
296 ide_driver_t *driver;
297 struct gendisk *disk;
298 struct kref kref;
299
300 /*
301 * Since a typical character device operation requires more
302 * than one packet command, we provide here enough memory
303 * for the maximum of interconnected packet commands.
304 * The packet commands are stored in the circular array pc_stack.
305 * pc_stack_index points to the last used entry, and warps around
306 * to the start when we get to the last array entry.
307 *
308 * pc points to the current processed packet command.
309 *
310 * failed_pc points to the last failed packet command, or contains
311 * NULL if we do not need to retry any packet command. This is
312 * required since an additional packet command is needed before the
313 * retry, to get detailed information on what went wrong.
314 */
315 /* Current packet command */
316 idetape_pc_t *pc;
317 /* Last failed packet command */
318 idetape_pc_t *failed_pc;
319 /* Packet command stack */
320 idetape_pc_t pc_stack[IDETAPE_PC_STACK];
321 /* Next free packet command storage space */
322 int pc_stack_index;
323 struct request rq_stack[IDETAPE_PC_STACK];
324 /* We implement a circular array */
325 int rq_stack_index;
326
327 /*
328 * DSC polling variables.
329 *
330 * While polling for DSC we use postponed_rq to postpone the
331 * current request so that ide.c will be able to service
332 * pending requests on the other device. Note that at most
333 * we will have only one DSC (usually data transfer) request
334 * in the device request queue. Additional requests can be
335 * queued in our internal pipeline, but they will be visible
336 * to ide.c only one at a time.
337 */
338 struct request *postponed_rq;
339 /* The time in which we started polling for DSC */
340 unsigned long dsc_polling_start;
341 /* Timer used to poll for dsc */
342 struct timer_list dsc_timer;
343 /* Read/Write dsc polling frequency */
344 unsigned long best_dsc_rw_frequency;
345 /* The current polling frequency */
346 unsigned long dsc_polling_frequency;
347 /* Maximum waiting time */
348 unsigned long dsc_timeout;
349
350 /*
351 * Read position information
352 */
353 u8 partition;
354 /* Current block */
355 unsigned int first_frame_position;
356 unsigned int last_frame_position;
357 unsigned int blocks_in_buffer;
358
359 /*
360 * Last error information
361 */
362 u8 sense_key, asc, ascq;
363
364 /*
365 * Character device operation
366 */
367 unsigned int minor;
368 /* device name */
369 char name[4];
370 /* Current character device data transfer direction */
371 idetape_chrdev_direction_t chrdev_direction;
372
373 /*
374 * Device information
375 */
376 /* Usually 512 or 1024 bytes */
377 unsigned short tape_block_size;
378 int user_bs_factor;
Borislav Petkovb6422012008-02-02 19:56:49 +0100379
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 /* Copy of the tape's Capabilities and Mechanical Page */
Borislav Petkovb6422012008-02-02 19:56:49 +0100381 u8 caps[20];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382
383 /*
384 * Active data transfer request parameters.
385 *
386 * At most, there is only one ide-tape originated data transfer
387 * request in the device request queue. This allows ide.c to
388 * easily service requests from the other device when we
389 * postpone our active request. In the pipelined operation
390 * mode, we use our internal pipeline structure to hold
391 * more data requests.
392 *
393 * The data buffer size is chosen based on the tape's
394 * recommendation.
395 */
396 /* Pointer to the request which is waiting in the device request queue */
397 struct request *active_data_request;
398 /* Data buffer size (chosen based on the tape's recommendation */
399 int stage_size;
400 idetape_stage_t *merge_stage;
401 int merge_stage_size;
402 struct idetape_bh *bh;
403 char *b_data;
404 int b_count;
405
406 /*
407 * Pipeline parameters.
408 *
409 * To accomplish non-pipelined mode, we simply set the following
410 * variables to zero (or NULL, where appropriate).
411 */
412 /* Number of currently used stages */
413 int nr_stages;
414 /* Number of pending stages */
415 int nr_pending_stages;
416 /* We will not allocate more than this number of stages */
417 int max_stages, min_pipeline, max_pipeline;
418 /* The first stage which will be removed from the pipeline */
419 idetape_stage_t *first_stage;
420 /* The currently active stage */
421 idetape_stage_t *active_stage;
422 /* Will be serviced after the currently active request */
423 idetape_stage_t *next_stage;
424 /* New requests will be added to the pipeline here */
425 idetape_stage_t *last_stage;
426 /* Optional free stage which we can use */
427 idetape_stage_t *cache_stage;
428 int pages_per_stage;
429 /* Wasted space in each stage */
430 int excess_bh_size;
431
432 /* Status/Action flags: long for set_bit */
433 unsigned long flags;
434 /* protects the ide-tape queue */
435 spinlock_t spinlock;
436
437 /*
438 * Measures average tape speed
439 */
440 unsigned long avg_time;
441 int avg_size;
442 int avg_speed;
443
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 char vendor_id[10];
445 char product_id[18];
446 char firmware_revision[6];
447 int firmware_revision_num;
448
449 /* the door is currently locked */
450 int door_locked;
451 /* the tape hardware is write protected */
452 char drv_write_prot;
453 /* the tape is write protected (hardware or opened as read-only) */
454 char write_prot;
455
456 /*
457 * Limit the number of times a request can
458 * be postponed, to avoid an infinite postpone
459 * deadlock.
460 */
461 /* request postpone count limit */
462 int postpone_cnt;
463
464 /*
465 * Measures number of frames:
466 *
467 * 1. written/read to/from the driver pipeline (pipeline_head).
468 * 2. written/read to/from the tape buffers (idetape_bh).
469 * 3. written/read by the tape to/from the media (tape_head).
470 */
471 int pipeline_head;
472 int buffer_head;
473 int tape_head;
474 int last_tape_head;
475
476 /*
477 * Speed control at the tape buffers input/output
478 */
479 unsigned long insert_time;
480 int insert_size;
481 int insert_speed;
482 int max_insert_speed;
483 int measure_insert_time;
484
485 /*
486 * Measure tape still time, in milliseconds
487 */
488 unsigned long tape_still_time_begin;
489 int tape_still_time;
490
491 /*
492 * Speed regulation negative feedback loop
493 */
494 int speed_control;
495 int pipeline_head_speed;
496 int controlled_pipeline_head_speed;
497 int uncontrolled_pipeline_head_speed;
498 int controlled_last_pipeline_head;
499 int uncontrolled_last_pipeline_head;
500 unsigned long uncontrolled_pipeline_head_time;
501 unsigned long controlled_pipeline_head_time;
502 int controlled_previous_pipeline_head;
503 int uncontrolled_previous_pipeline_head;
504 unsigned long controlled_previous_head_time;
505 unsigned long uncontrolled_previous_head_time;
506 int restart_speed_control_req;
507
508 /*
509 * Debug_level determines amount of debugging output;
510 * can be changed using /proc/ide/hdx/settings
511 * 0 : almost no debugging output
512 * 1 : 0+output errors only
513 * 2 : 1+output all sensekey/asc
514 * 3 : 2+follow all chrdev related procedures
515 * 4 : 3+follow all procedures
516 * 5 : 4+include pc_stack rq_stack info
517 * 6 : 5+USE_COUNT updates
518 */
519 int debug_level;
520} idetape_tape_t;
521
Arjan van de Vencf8b8972006-03-23 03:00:45 -0800522static DEFINE_MUTEX(idetape_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523
Will Dysond5dee802005-09-16 02:55:07 -0700524static struct class *idetape_sysfs_class;
525
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526#define to_ide_tape(obj) container_of(obj, struct ide_tape_obj, kref)
527
528#define ide_tape_g(disk) \
529 container_of((disk)->private_data, struct ide_tape_obj, driver)
530
531static struct ide_tape_obj *ide_tape_get(struct gendisk *disk)
532{
533 struct ide_tape_obj *tape = NULL;
534
Arjan van de Vencf8b8972006-03-23 03:00:45 -0800535 mutex_lock(&idetape_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 tape = ide_tape_g(disk);
537 if (tape)
538 kref_get(&tape->kref);
Arjan van de Vencf8b8972006-03-23 03:00:45 -0800539 mutex_unlock(&idetape_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 return tape;
541}
542
543static void ide_tape_release(struct kref *);
544
545static void ide_tape_put(struct ide_tape_obj *tape)
546{
Arjan van de Vencf8b8972006-03-23 03:00:45 -0800547 mutex_lock(&idetape_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 kref_put(&tape->kref, ide_tape_release);
Arjan van de Vencf8b8972006-03-23 03:00:45 -0800549 mutex_unlock(&idetape_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550}
551
552/*
553 * Tape door status
554 */
555#define DOOR_UNLOCKED 0
556#define DOOR_LOCKED 1
557#define DOOR_EXPLICITLY_LOCKED 2
558
559/*
560 * Tape flag bits values.
561 */
562#define IDETAPE_IGNORE_DSC 0
563#define IDETAPE_ADDRESS_VALID 1 /* 0 When the tape position is unknown */
564#define IDETAPE_BUSY 2 /* Device already opened */
565#define IDETAPE_PIPELINE_ERROR 3 /* Error detected in a pipeline stage */
566#define IDETAPE_DETECT_BS 4 /* Attempt to auto-detect the current user block size */
567#define IDETAPE_FILEMARK 5 /* Currently on a filemark */
568#define IDETAPE_DRQ_INTERRUPT 6 /* DRQ interrupt device */
569#define IDETAPE_READ_ERROR 7
570#define IDETAPE_PIPELINE_ACTIVE 8 /* pipeline active */
571/* 0 = no tape is loaded, so we don't rewind after ejecting */
572#define IDETAPE_MEDIUM_PRESENT 9
573
574/*
575 * Supported ATAPI tape drives packet commands
576 */
577#define IDETAPE_TEST_UNIT_READY_CMD 0x00
578#define IDETAPE_REWIND_CMD 0x01
579#define IDETAPE_REQUEST_SENSE_CMD 0x03
580#define IDETAPE_READ_CMD 0x08
581#define IDETAPE_WRITE_CMD 0x0a
582#define IDETAPE_WRITE_FILEMARK_CMD 0x10
583#define IDETAPE_SPACE_CMD 0x11
584#define IDETAPE_INQUIRY_CMD 0x12
585#define IDETAPE_ERASE_CMD 0x19
586#define IDETAPE_MODE_SENSE_CMD 0x1a
587#define IDETAPE_MODE_SELECT_CMD 0x15
588#define IDETAPE_LOAD_UNLOAD_CMD 0x1b
589#define IDETAPE_PREVENT_CMD 0x1e
590#define IDETAPE_LOCATE_CMD 0x2b
591#define IDETAPE_READ_POSITION_CMD 0x34
592#define IDETAPE_READ_BUFFER_CMD 0x3c
593#define IDETAPE_SET_SPEED_CMD 0xbb
594
595/*
596 * Some defines for the READ BUFFER command
597 */
598#define IDETAPE_RETRIEVE_FAULTY_BLOCK 6
599
600/*
601 * Some defines for the SPACE command
602 */
603#define IDETAPE_SPACE_OVER_FILEMARK 1
604#define IDETAPE_SPACE_TO_EOD 3
605
606/*
607 * Some defines for the LOAD UNLOAD command
608 */
609#define IDETAPE_LU_LOAD_MASK 1
610#define IDETAPE_LU_RETENSION_MASK 2
611#define IDETAPE_LU_EOT_MASK 4
612
613/*
614 * Special requests for our block device strategy routine.
615 *
616 * In order to service a character device command, we add special
617 * requests to the tail of our block device request queue and wait
618 * for their completion.
619 */
620
621enum {
622 REQ_IDETAPE_PC1 = (1 << 0), /* packet command (first stage) */
623 REQ_IDETAPE_PC2 = (1 << 1), /* packet command (second stage) */
624 REQ_IDETAPE_READ = (1 << 2),
625 REQ_IDETAPE_WRITE = (1 << 3),
626 REQ_IDETAPE_READ_BUFFER = (1 << 4),
627};
628
629/*
630 * Error codes which are returned in rq->errors to the higher part
631 * of the driver.
632 */
633#define IDETAPE_ERROR_GENERAL 101
634#define IDETAPE_ERROR_FILEMARK 102
635#define IDETAPE_ERROR_EOD 103
636
637/*
638 * The following is used to format the general configuration word of
639 * the ATAPI IDENTIFY DEVICE command.
640 */
641struct idetape_id_gcw {
642 unsigned packet_size :2; /* Packet Size */
643 unsigned reserved234 :3; /* Reserved */
644 unsigned drq_type :2; /* Command packet DRQ type */
645 unsigned removable :1; /* Removable media */
646 unsigned device_type :5; /* Device type */
647 unsigned reserved13 :1; /* Reserved */
648 unsigned protocol :2; /* Protocol type */
649};
650
651/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652 * READ POSITION packet command - Data Format (From Table 6-57)
653 */
654typedef struct {
655 unsigned reserved0_10 :2; /* Reserved */
656 unsigned bpu :1; /* Block Position Unknown */
657 unsigned reserved0_543 :3; /* Reserved */
658 unsigned eop :1; /* End Of Partition */
659 unsigned bop :1; /* Beginning Of Partition */
660 u8 partition; /* Partition Number */
661 u8 reserved2, reserved3; /* Reserved */
662 u32 first_block; /* First Block Location */
663 u32 last_block; /* Last Block Location (Optional) */
664 u8 reserved12; /* Reserved */
665 u8 blocks_in_buffer[3]; /* Blocks In Buffer - (Optional) */
666 u32 bytes_in_buffer; /* Bytes In Buffer (Optional) */
667} idetape_read_position_result_t;
668
669/*
670 * Follows structures which are related to the SELECT SENSE / MODE SENSE
671 * packet commands. Those packet commands are still not supported
672 * by ide-tape.
673 */
674#define IDETAPE_BLOCK_DESCRIPTOR 0
675#define IDETAPE_CAPABILITIES_PAGE 0x2a
676#define IDETAPE_PARAMTR_PAGE 0x2b /* Onstream DI-x0 only */
677#define IDETAPE_BLOCK_SIZE_PAGE 0x30
678#define IDETAPE_BUFFER_FILLING_PAGE 0x33
679
680/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 * Mode Parameter Block Descriptor the MODE SENSE packet command
682 *
683 * Support for block descriptors is optional.
684 */
685typedef struct {
686 __u8 density_code; /* Medium density code */
687 __u8 blocks[3]; /* Number of blocks */
688 __u8 reserved4; /* Reserved */
689 __u8 length[3]; /* Block Length */
690} idetape_parameter_block_descriptor_t;
691
692/*
693 * The Data Compression Page, as returned by the MODE SENSE packet command.
694 */
695typedef struct {
696 unsigned page_code :6; /* Page Code - Should be 0xf */
697 unsigned reserved0 :1; /* Reserved */
698 unsigned ps :1;
699 __u8 page_length; /* Page Length - Should be 14 */
700 unsigned reserved2 :6; /* Reserved */
701 unsigned dcc :1; /* Data Compression Capable */
702 unsigned dce :1; /* Data Compression Enable */
703 unsigned reserved3 :5; /* Reserved */
704 unsigned red :2; /* Report Exception on Decompression */
705 unsigned dde :1; /* Data Decompression Enable */
706 __u32 ca; /* Compression Algorithm */
707 __u32 da; /* Decompression Algorithm */
708 __u8 reserved[4]; /* Reserved */
709} idetape_data_compression_page_t;
710
711/*
712 * The Medium Partition Page, as returned by the MODE SENSE packet command.
713 */
714typedef struct {
715 unsigned page_code :6; /* Page Code - Should be 0x11 */
716 unsigned reserved1_6 :1; /* Reserved */
717 unsigned ps :1;
718 __u8 page_length; /* Page Length - Should be 6 */
719 __u8 map; /* Maximum Additional Partitions - Should be 0 */
720 __u8 apd; /* Additional Partitions Defined - Should be 0 */
721 unsigned reserved4_012 :3; /* Reserved */
722 unsigned psum :2; /* Should be 0 */
723 unsigned idp :1; /* Should be 0 */
724 unsigned sdp :1; /* Should be 0 */
725 unsigned fdp :1; /* Fixed Data Partitions */
726 __u8 mfr; /* Medium Format Recognition */
727 __u8 reserved[2]; /* Reserved */
728} idetape_medium_partition_page_t;
729
730/*
731 * Run time configurable parameters.
732 */
733typedef struct {
734 int dsc_rw_frequency;
735 int dsc_media_access_frequency;
736 int nr_stages;
737} idetape_config_t;
738
739/*
740 * The variables below are used for the character device interface.
741 * Additional state variables are defined in our ide_drive_t structure.
742 */
743static struct ide_tape_obj * idetape_devs[MAX_HWIFS * MAX_DRIVES];
744
745#define ide_tape_f(file) ((file)->private_data)
746
747static struct ide_tape_obj *ide_tape_chrdev_get(unsigned int i)
748{
749 struct ide_tape_obj *tape = NULL;
750
Arjan van de Vencf8b8972006-03-23 03:00:45 -0800751 mutex_lock(&idetape_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752 tape = idetape_devs[i];
753 if (tape)
754 kref_get(&tape->kref);
Arjan van de Vencf8b8972006-03-23 03:00:45 -0800755 mutex_unlock(&idetape_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756 return tape;
757}
758
759/*
760 * Function declarations
761 *
762 */
763static int idetape_chrdev_release (struct inode *inode, struct file *filp);
764static void idetape_write_release (ide_drive_t *drive, unsigned int minor);
765
766/*
767 * Too bad. The drive wants to send us data which we are not ready to accept.
768 * Just throw it away.
769 */
770static void idetape_discard_data (ide_drive_t *drive, unsigned int bcount)
771{
772 while (bcount--)
773 (void) HWIF(drive)->INB(IDE_DATA_REG);
774}
775
776static void idetape_input_buffers (ide_drive_t *drive, idetape_pc_t *pc, unsigned int bcount)
777{
778 struct idetape_bh *bh = pc->bh;
779 int count;
780
781 while (bcount) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782 if (bh == NULL) {
783 printk(KERN_ERR "ide-tape: bh == NULL in "
784 "idetape_input_buffers\n");
785 idetape_discard_data(drive, bcount);
786 return;
787 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788 count = min((unsigned int)(bh->b_size - atomic_read(&bh->b_count)), bcount);
789 HWIF(drive)->atapi_input_bytes(drive, bh->b_data + atomic_read(&bh->b_count), count);
790 bcount -= count;
791 atomic_add(count, &bh->b_count);
792 if (atomic_read(&bh->b_count) == bh->b_size) {
793 bh = bh->b_reqnext;
794 if (bh)
795 atomic_set(&bh->b_count, 0);
796 }
797 }
798 pc->bh = bh;
799}
800
801static void idetape_output_buffers (ide_drive_t *drive, idetape_pc_t *pc, unsigned int bcount)
802{
803 struct idetape_bh *bh = pc->bh;
804 int count;
805
806 while (bcount) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807 if (bh == NULL) {
808 printk(KERN_ERR "ide-tape: bh == NULL in "
809 "idetape_output_buffers\n");
810 return;
811 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812 count = min((unsigned int)pc->b_count, (unsigned int)bcount);
813 HWIF(drive)->atapi_output_bytes(drive, pc->b_data, count);
814 bcount -= count;
815 pc->b_data += count;
816 pc->b_count -= count;
817 if (!pc->b_count) {
818 pc->bh = bh = bh->b_reqnext;
819 if (bh) {
820 pc->b_data = bh->b_data;
821 pc->b_count = atomic_read(&bh->b_count);
822 }
823 }
824 }
825}
826
827static void idetape_update_buffers (idetape_pc_t *pc)
828{
829 struct idetape_bh *bh = pc->bh;
830 int count;
831 unsigned int bcount = pc->actually_transferred;
832
833 if (test_bit(PC_WRITING, &pc->flags))
834 return;
835 while (bcount) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836 if (bh == NULL) {
837 printk(KERN_ERR "ide-tape: bh == NULL in "
838 "idetape_update_buffers\n");
839 return;
840 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841 count = min((unsigned int)bh->b_size, (unsigned int)bcount);
842 atomic_set(&bh->b_count, count);
843 if (atomic_read(&bh->b_count) == bh->b_size)
844 bh = bh->b_reqnext;
845 bcount -= count;
846 }
847 pc->bh = bh;
848}
849
850/*
851 * idetape_next_pc_storage returns a pointer to a place in which we can
852 * safely store a packet command, even though we intend to leave the
853 * driver. A storage space for a maximum of IDETAPE_PC_STACK packet
854 * commands is allocated at initialization time.
855 */
856static idetape_pc_t *idetape_next_pc_storage (ide_drive_t *drive)
857{
858 idetape_tape_t *tape = drive->driver_data;
859
860#if IDETAPE_DEBUG_LOG
861 if (tape->debug_level >= 5)
862 printk(KERN_INFO "ide-tape: pc_stack_index=%d\n",
863 tape->pc_stack_index);
864#endif /* IDETAPE_DEBUG_LOG */
865 if (tape->pc_stack_index == IDETAPE_PC_STACK)
866 tape->pc_stack_index=0;
867 return (&tape->pc_stack[tape->pc_stack_index++]);
868}
869
870/*
871 * idetape_next_rq_storage is used along with idetape_next_pc_storage.
872 * Since we queue packet commands in the request queue, we need to
873 * allocate a request, along with the allocation of a packet command.
874 */
875
876/**************************************************************
877 * *
878 * This should get fixed to use kmalloc(.., GFP_ATOMIC) *
879 * followed later on by kfree(). -ml *
880 * *
881 **************************************************************/
882
883static struct request *idetape_next_rq_storage (ide_drive_t *drive)
884{
885 idetape_tape_t *tape = drive->driver_data;
886
887#if IDETAPE_DEBUG_LOG
888 if (tape->debug_level >= 5)
889 printk(KERN_INFO "ide-tape: rq_stack_index=%d\n",
890 tape->rq_stack_index);
891#endif /* IDETAPE_DEBUG_LOG */
892 if (tape->rq_stack_index == IDETAPE_PC_STACK)
893 tape->rq_stack_index=0;
894 return (&tape->rq_stack[tape->rq_stack_index++]);
895}
896
897/*
898 * idetape_init_pc initializes a packet command.
899 */
900static void idetape_init_pc (idetape_pc_t *pc)
901{
902 memset(pc->c, 0, 12);
903 pc->retries = 0;
904 pc->flags = 0;
905 pc->request_transfer = 0;
906 pc->buffer = pc->pc_buffer;
907 pc->buffer_size = IDETAPE_PC_BUFFER_SIZE;
908 pc->bh = NULL;
909 pc->b_data = NULL;
910}
911
912/*
Borislav Petkov1b5db432008-02-02 19:56:48 +0100913 * called on each failed packet command retry to analyze the request sense. We
914 * currently do not utilize this information.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915 */
Borislav Petkov1b5db432008-02-02 19:56:48 +0100916static void idetape_analyze_error(ide_drive_t *drive, u8 *sense)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917{
918 idetape_tape_t *tape = drive->driver_data;
919 idetape_pc_t *pc = tape->failed_pc;
920
Borislav Petkov1b5db432008-02-02 19:56:48 +0100921 tape->sense_key = sense[2] & 0xF;
922 tape->asc = sense[12];
923 tape->ascq = sense[13];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924#if IDETAPE_DEBUG_LOG
925 /*
Borislav Petkov1b5db432008-02-02 19:56:48 +0100926 * Without debugging, we only log an error if we decided to give up
927 * retrying.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928 */
929 if (tape->debug_level >= 1)
930 printk(KERN_INFO "ide-tape: pc = %x, sense key = %x, "
931 "asc = %x, ascq = %x\n",
Borislav Petkov1b5db432008-02-02 19:56:48 +0100932 pc->c[0], tape->sense_key,
933 tape->asc, tape->ascq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934#endif /* IDETAPE_DEBUG_LOG */
935
Borislav Petkov1b5db432008-02-02 19:56:48 +0100936 /* Correct pc->actually_transferred by asking the tape. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937 if (test_bit(PC_DMA_ERROR, &pc->flags)) {
Borislav Petkov1b5db432008-02-02 19:56:48 +0100938 pc->actually_transferred = pc->request_transfer -
939 tape->tape_block_size *
940 ntohl(get_unaligned((u32 *)&sense[3]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941 idetape_update_buffers(pc);
942 }
943
944 /*
945 * If error was the result of a zero-length read or write command,
946 * with sense key=5, asc=0x22, ascq=0, let it slide. Some drives
947 * (i.e. Seagate STT3401A Travan) don't support 0-length read/writes.
948 */
949 if ((pc->c[0] == IDETAPE_READ_CMD || pc->c[0] == IDETAPE_WRITE_CMD)
Borislav Petkov1b5db432008-02-02 19:56:48 +0100950 /* length == 0 */
951 && pc->c[4] == 0 && pc->c[3] == 0 && pc->c[2] == 0) {
952 if (tape->sense_key == 5) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953 /* don't report an error, everything's ok */
954 pc->error = 0;
955 /* don't retry read/write */
956 set_bit(PC_ABORT, &pc->flags);
957 }
958 }
Borislav Petkov1b5db432008-02-02 19:56:48 +0100959 if (pc->c[0] == IDETAPE_READ_CMD && (sense[2] & 0x80)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960 pc->error = IDETAPE_ERROR_FILEMARK;
961 set_bit(PC_ABORT, &pc->flags);
962 }
963 if (pc->c[0] == IDETAPE_WRITE_CMD) {
Borislav Petkov1b5db432008-02-02 19:56:48 +0100964 if ((sense[2] & 0x40) || (tape->sense_key == 0xd
965 && tape->asc == 0x0 && tape->ascq == 0x2)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966 pc->error = IDETAPE_ERROR_EOD;
967 set_bit(PC_ABORT, &pc->flags);
968 }
969 }
970 if (pc->c[0] == IDETAPE_READ_CMD || pc->c[0] == IDETAPE_WRITE_CMD) {
Borislav Petkov1b5db432008-02-02 19:56:48 +0100971 if (tape->sense_key == 8) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972 pc->error = IDETAPE_ERROR_EOD;
973 set_bit(PC_ABORT, &pc->flags);
974 }
975 if (!test_bit(PC_ABORT, &pc->flags) &&
976 pc->actually_transferred)
977 pc->retries = IDETAPE_MAX_PC_RETRIES + 1;
978 }
979}
980
981/*
982 * idetape_active_next_stage will declare the next stage as "active".
983 */
984static void idetape_active_next_stage (ide_drive_t *drive)
985{
986 idetape_tape_t *tape = drive->driver_data;
987 idetape_stage_t *stage = tape->next_stage;
988 struct request *rq = &stage->rq;
989
990#if IDETAPE_DEBUG_LOG
991 if (tape->debug_level >= 4)
992 printk(KERN_INFO "ide-tape: Reached idetape_active_next_stage\n");
993#endif /* IDETAPE_DEBUG_LOG */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994 if (stage == NULL) {
995 printk(KERN_ERR "ide-tape: bug: Trying to activate a non existing stage\n");
996 return;
997 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998
999 rq->rq_disk = tape->disk;
1000 rq->buffer = NULL;
1001 rq->special = (void *)stage->bh;
1002 tape->active_data_request = rq;
1003 tape->active_stage = stage;
1004 tape->next_stage = stage->next;
1005}
1006
1007/*
1008 * idetape_increase_max_pipeline_stages is a part of the feedback
1009 * loop which tries to find the optimum number of stages. In the
1010 * feedback loop, we are starting from a minimum maximum number of
1011 * stages, and if we sense that the pipeline is empty, we try to
1012 * increase it, until we reach the user compile time memory limit.
1013 */
1014static void idetape_increase_max_pipeline_stages (ide_drive_t *drive)
1015{
1016 idetape_tape_t *tape = drive->driver_data;
1017 int increase = (tape->max_pipeline - tape->min_pipeline) / 10;
1018
1019#if IDETAPE_DEBUG_LOG
1020 if (tape->debug_level >= 4)
1021 printk (KERN_INFO "ide-tape: Reached idetape_increase_max_pipeline_stages\n");
1022#endif /* IDETAPE_DEBUG_LOG */
1023
1024 tape->max_stages += max(increase, 1);
1025 tape->max_stages = max(tape->max_stages, tape->min_pipeline);
1026 tape->max_stages = min(tape->max_stages, tape->max_pipeline);
1027}
1028
1029/*
1030 * idetape_kfree_stage calls kfree to completely free a stage, along with
1031 * its related buffers.
1032 */
1033static void __idetape_kfree_stage (idetape_stage_t *stage)
1034{
1035 struct idetape_bh *prev_bh, *bh = stage->bh;
1036 int size;
1037
1038 while (bh != NULL) {
1039 if (bh->b_data != NULL) {
1040 size = (int) bh->b_size;
1041 while (size > 0) {
1042 free_page((unsigned long) bh->b_data);
1043 size -= PAGE_SIZE;
1044 bh->b_data += PAGE_SIZE;
1045 }
1046 }
1047 prev_bh = bh;
1048 bh = bh->b_reqnext;
1049 kfree(prev_bh);
1050 }
1051 kfree(stage);
1052}
1053
1054static void idetape_kfree_stage (idetape_tape_t *tape, idetape_stage_t *stage)
1055{
1056 __idetape_kfree_stage(stage);
1057}
1058
1059/*
1060 * idetape_remove_stage_head removes tape->first_stage from the pipeline.
1061 * The caller should avoid race conditions.
1062 */
1063static void idetape_remove_stage_head (ide_drive_t *drive)
1064{
1065 idetape_tape_t *tape = drive->driver_data;
1066 idetape_stage_t *stage;
1067
1068#if IDETAPE_DEBUG_LOG
1069 if (tape->debug_level >= 4)
1070 printk(KERN_INFO "ide-tape: Reached idetape_remove_stage_head\n");
1071#endif /* IDETAPE_DEBUG_LOG */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072 if (tape->first_stage == NULL) {
1073 printk(KERN_ERR "ide-tape: bug: tape->first_stage is NULL\n");
Borislav Petkov55a5d292008-02-02 19:56:49 +01001074 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075 }
1076 if (tape->active_stage == tape->first_stage) {
1077 printk(KERN_ERR "ide-tape: bug: Trying to free our active pipeline stage\n");
1078 return;
1079 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080 stage = tape->first_stage;
1081 tape->first_stage = stage->next;
1082 idetape_kfree_stage(tape, stage);
1083 tape->nr_stages--;
1084 if (tape->first_stage == NULL) {
1085 tape->last_stage = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086 if (tape->next_stage != NULL)
1087 printk(KERN_ERR "ide-tape: bug: tape->next_stage != NULL\n");
1088 if (tape->nr_stages)
1089 printk(KERN_ERR "ide-tape: bug: nr_stages should be 0 now\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090 }
1091}
1092
1093/*
1094 * This will free all the pipeline stages starting from new_last_stage->next
1095 * to the end of the list, and point tape->last_stage to new_last_stage.
1096 */
1097static void idetape_abort_pipeline(ide_drive_t *drive,
1098 idetape_stage_t *new_last_stage)
1099{
1100 idetape_tape_t *tape = drive->driver_data;
1101 idetape_stage_t *stage = new_last_stage->next;
1102 idetape_stage_t *nstage;
1103
1104#if IDETAPE_DEBUG_LOG
1105 if (tape->debug_level >= 4)
1106 printk(KERN_INFO "ide-tape: %s: idetape_abort_pipeline called\n", tape->name);
1107#endif
1108 while (stage) {
1109 nstage = stage->next;
1110 idetape_kfree_stage(tape, stage);
1111 --tape->nr_stages;
1112 --tape->nr_pending_stages;
1113 stage = nstage;
1114 }
1115 if (new_last_stage)
1116 new_last_stage->next = NULL;
1117 tape->last_stage = new_last_stage;
1118 tape->next_stage = NULL;
1119}
1120
1121/*
1122 * idetape_end_request is used to finish servicing a request, and to
1123 * insert a pending pipeline request into the main device queue.
1124 */
1125static int idetape_end_request(ide_drive_t *drive, int uptodate, int nr_sects)
1126{
1127 struct request *rq = HWGROUP(drive)->rq;
1128 idetape_tape_t *tape = drive->driver_data;
1129 unsigned long flags;
1130 int error;
1131 int remove_stage = 0;
1132 idetape_stage_t *active_stage;
1133
1134#if IDETAPE_DEBUG_LOG
1135 if (tape->debug_level >= 4)
1136 printk(KERN_INFO "ide-tape: Reached idetape_end_request\n");
1137#endif /* IDETAPE_DEBUG_LOG */
1138
1139 switch (uptodate) {
1140 case 0: error = IDETAPE_ERROR_GENERAL; break;
1141 case 1: error = 0; break;
1142 default: error = uptodate;
1143 }
1144 rq->errors = error;
1145 if (error)
1146 tape->failed_pc = NULL;
1147
Bartlomiej Zolnierkiewicz36872212008-01-26 20:13:10 +01001148 if (!blk_special_request(rq)) {
1149 ide_end_request(drive, uptodate, nr_sects);
1150 return 0;
1151 }
1152
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153 spin_lock_irqsave(&tape->spinlock, flags);
1154
1155 /* The request was a pipelined data transfer request */
1156 if (tape->active_data_request == rq) {
1157 active_stage = tape->active_stage;
1158 tape->active_stage = NULL;
1159 tape->active_data_request = NULL;
1160 tape->nr_pending_stages--;
1161 if (rq->cmd[0] & REQ_IDETAPE_WRITE) {
1162 remove_stage = 1;
1163 if (error) {
1164 set_bit(IDETAPE_PIPELINE_ERROR, &tape->flags);
1165 if (error == IDETAPE_ERROR_EOD)
1166 idetape_abort_pipeline(drive, active_stage);
1167 }
1168 } else if (rq->cmd[0] & REQ_IDETAPE_READ) {
1169 if (error == IDETAPE_ERROR_EOD) {
1170 set_bit(IDETAPE_PIPELINE_ERROR, &tape->flags);
1171 idetape_abort_pipeline(drive, active_stage);
1172 }
1173 }
1174 if (tape->next_stage != NULL) {
1175 idetape_active_next_stage(drive);
1176
1177 /*
1178 * Insert the next request into the request queue.
1179 */
1180 (void) ide_do_drive_cmd(drive, tape->active_data_request, ide_end);
1181 } else if (!error) {
1182 idetape_increase_max_pipeline_stages(drive);
1183 }
1184 }
1185 ide_end_drive_cmd(drive, 0, 0);
1186// blkdev_dequeue_request(rq);
1187// drive->rq = NULL;
1188// end_that_request_last(rq);
1189
1190 if (remove_stage)
1191 idetape_remove_stage_head(drive);
1192 if (tape->active_data_request == NULL)
1193 clear_bit(IDETAPE_PIPELINE_ACTIVE, &tape->flags);
1194 spin_unlock_irqrestore(&tape->spinlock, flags);
1195 return 0;
1196}
1197
1198static ide_startstop_t idetape_request_sense_callback (ide_drive_t *drive)
1199{
1200 idetape_tape_t *tape = drive->driver_data;
1201
1202#if IDETAPE_DEBUG_LOG
1203 if (tape->debug_level >= 4)
1204 printk(KERN_INFO "ide-tape: Reached idetape_request_sense_callback\n");
1205#endif /* IDETAPE_DEBUG_LOG */
1206 if (!tape->pc->error) {
Borislav Petkov1b5db432008-02-02 19:56:48 +01001207 idetape_analyze_error(drive, tape->pc->buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208 idetape_end_request(drive, 1, 0);
1209 } else {
1210 printk(KERN_ERR "ide-tape: Error in REQUEST SENSE itself - Aborting request!\n");
1211 idetape_end_request(drive, 0, 0);
1212 }
1213 return ide_stopped;
1214}
1215
1216static void idetape_create_request_sense_cmd (idetape_pc_t *pc)
1217{
1218 idetape_init_pc(pc);
1219 pc->c[0] = IDETAPE_REQUEST_SENSE_CMD;
1220 pc->c[4] = 20;
1221 pc->request_transfer = 20;
1222 pc->callback = &idetape_request_sense_callback;
1223}
1224
1225static void idetape_init_rq(struct request *rq, u8 cmd)
1226{
1227 memset(rq, 0, sizeof(*rq));
Jens Axboe4aff5e22006-08-10 08:44:47 +02001228 rq->cmd_type = REQ_TYPE_SPECIAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229 rq->cmd[0] = cmd;
1230}
1231
1232/*
1233 * idetape_queue_pc_head generates a new packet command request in front
1234 * of the request queue, before the current request, so that it will be
1235 * processed immediately, on the next pass through the driver.
1236 *
1237 * idetape_queue_pc_head is called from the request handling part of
1238 * the driver (the "bottom" part). Safe storage for the request should
1239 * be allocated with idetape_next_pc_storage and idetape_next_rq_storage
1240 * before calling idetape_queue_pc_head.
1241 *
1242 * Memory for those requests is pre-allocated at initialization time, and
1243 * is limited to IDETAPE_PC_STACK requests. We assume that we have enough
1244 * space for the maximum possible number of inter-dependent packet commands.
1245 *
1246 * The higher level of the driver - The ioctl handler and the character
1247 * device handling functions should queue request to the lower level part
1248 * and wait for their completion using idetape_queue_pc_tail or
1249 * idetape_queue_rw_tail.
1250 */
1251static void idetape_queue_pc_head (ide_drive_t *drive, idetape_pc_t *pc,struct request *rq)
1252{
1253 struct ide_tape_obj *tape = drive->driver_data;
1254
1255 idetape_init_rq(rq, REQ_IDETAPE_PC1);
1256 rq->buffer = (char *) pc;
1257 rq->rq_disk = tape->disk;
1258 (void) ide_do_drive_cmd(drive, rq, ide_preempt);
1259}
1260
1261/*
1262 * idetape_retry_pc is called when an error was detected during the
1263 * last packet command. We queue a request sense packet command in
1264 * the head of the request list.
1265 */
1266static ide_startstop_t idetape_retry_pc (ide_drive_t *drive)
1267{
1268 idetape_tape_t *tape = drive->driver_data;
1269 idetape_pc_t *pc;
1270 struct request *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271
Bartlomiej Zolnierkiewicz0e38a662008-01-25 22:17:12 +01001272 (void)drive->hwif->INB(IDE_ERROR_REG);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273 pc = idetape_next_pc_storage(drive);
1274 rq = idetape_next_rq_storage(drive);
1275 idetape_create_request_sense_cmd(pc);
1276 set_bit(IDETAPE_IGNORE_DSC, &tape->flags);
1277 idetape_queue_pc_head(drive, pc, rq);
1278 return ide_stopped;
1279}
1280
1281/*
1282 * idetape_postpone_request postpones the current request so that
1283 * ide.c will be able to service requests from another device on
1284 * the same hwgroup while we are polling for DSC.
1285 */
1286static void idetape_postpone_request (ide_drive_t *drive)
1287{
1288 idetape_tape_t *tape = drive->driver_data;
1289
1290#if IDETAPE_DEBUG_LOG
1291 if (tape->debug_level >= 4)
1292 printk(KERN_INFO "ide-tape: idetape_postpone_request\n");
1293#endif
1294 tape->postponed_rq = HWGROUP(drive)->rq;
1295 ide_stall_queue(drive, tape->dsc_polling_frequency);
1296}
1297
1298/*
1299 * idetape_pc_intr is the usual interrupt handler which will be called
1300 * during a packet command. We will transfer some of the data (as
1301 * requested by the drive) and will re-point interrupt handler to us.
1302 * When data transfer is finished, we will act according to the
1303 * algorithm described before idetape_issue_packet_command.
1304 *
1305 */
1306static ide_startstop_t idetape_pc_intr (ide_drive_t *drive)
1307{
1308 ide_hwif_t *hwif = drive->hwif;
1309 idetape_tape_t *tape = drive->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310 idetape_pc_t *pc = tape->pc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311 unsigned int temp;
1312#if SIMULATE_ERRORS
1313 static int error_sim_count = 0;
1314#endif
Bartlomiej Zolnierkiewicz790d1232008-01-25 22:17:12 +01001315 u16 bcount;
Bartlomiej Zolnierkiewicz8e7657a2008-01-25 22:17:12 +01001316 u8 stat, ireason;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317
1318#if IDETAPE_DEBUG_LOG
1319 if (tape->debug_level >= 4)
1320 printk(KERN_INFO "ide-tape: Reached idetape_pc_intr "
1321 "interrupt handler\n");
1322#endif /* IDETAPE_DEBUG_LOG */
1323
1324 /* Clear the interrupt */
Bartlomiej Zolnierkiewicz22c525b2008-01-25 22:17:11 +01001325 stat = hwif->INB(IDE_STATUS_REG);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326
1327 if (test_bit(PC_DMA_IN_PROGRESS, &pc->flags)) {
Bartlomiej Zolnierkiewicz22c525b2008-01-25 22:17:11 +01001328 if (hwif->ide_dma_end(drive) || (stat & ERR_STAT)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329 /*
1330 * A DMA error is sometimes expected. For example,
1331 * if the tape is crossing a filemark during a
1332 * READ command, it will issue an irq and position
1333 * itself before the filemark, so that only a partial
1334 * data transfer will occur (which causes the DMA
1335 * error). In that case, we will later ask the tape
1336 * how much bytes of the original request were
1337 * actually transferred (we can't receive that
1338 * information from the DMA engine on most chipsets).
1339 */
1340
1341 /*
1342 * On the contrary, a DMA error is never expected;
1343 * it usually indicates a hardware error or abort.
1344 * If the tape crosses a filemark during a READ
1345 * command, it will issue an irq and position itself
1346 * after the filemark (not before). Only a partial
1347 * data transfer will occur, but no DMA error.
1348 * (AS, 19 Apr 2001)
1349 */
1350 set_bit(PC_DMA_ERROR, &pc->flags);
1351 } else {
1352 pc->actually_transferred = pc->request_transfer;
1353 idetape_update_buffers(pc);
1354 }
1355#if IDETAPE_DEBUG_LOG
1356 if (tape->debug_level >= 4)
1357 printk(KERN_INFO "ide-tape: DMA finished\n");
1358#endif /* IDETAPE_DEBUG_LOG */
1359 }
1360
1361 /* No more interrupts */
Bartlomiej Zolnierkiewicz22c525b2008-01-25 22:17:11 +01001362 if ((stat & DRQ_STAT) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363#if IDETAPE_DEBUG_LOG
1364 if (tape->debug_level >= 2)
1365 printk(KERN_INFO "ide-tape: Packet command completed, %d bytes transferred\n", pc->actually_transferred);
1366#endif /* IDETAPE_DEBUG_LOG */
1367 clear_bit(PC_DMA_IN_PROGRESS, &pc->flags);
1368
1369 local_irq_enable();
1370
1371#if SIMULATE_ERRORS
1372 if ((pc->c[0] == IDETAPE_WRITE_CMD ||
1373 pc->c[0] == IDETAPE_READ_CMD) &&
1374 (++error_sim_count % 100) == 0) {
1375 printk(KERN_INFO "ide-tape: %s: simulating error\n",
1376 tape->name);
Bartlomiej Zolnierkiewicz22c525b2008-01-25 22:17:11 +01001377 stat |= ERR_STAT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378 }
1379#endif
Bartlomiej Zolnierkiewicz22c525b2008-01-25 22:17:11 +01001380 if ((stat & ERR_STAT) && pc->c[0] == IDETAPE_REQUEST_SENSE_CMD)
1381 stat &= ~ERR_STAT;
1382 if ((stat & ERR_STAT) || test_bit(PC_DMA_ERROR, &pc->flags)) {
1383 /* Error detected */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384#if IDETAPE_DEBUG_LOG
1385 if (tape->debug_level >= 1)
1386 printk(KERN_INFO "ide-tape: %s: I/O error\n",
1387 tape->name);
1388#endif /* IDETAPE_DEBUG_LOG */
1389 if (pc->c[0] == IDETAPE_REQUEST_SENSE_CMD) {
1390 printk(KERN_ERR "ide-tape: I/O error in request sense command\n");
1391 return ide_do_reset(drive);
1392 }
1393#if IDETAPE_DEBUG_LOG
1394 if (tape->debug_level >= 1)
1395 printk(KERN_INFO "ide-tape: [cmd %x]: check condition\n", pc->c[0]);
1396#endif
1397 /* Retry operation */
1398 return idetape_retry_pc(drive);
1399 }
1400 pc->error = 0;
1401 if (test_bit(PC_WAIT_FOR_DSC, &pc->flags) &&
Bartlomiej Zolnierkiewicz22c525b2008-01-25 22:17:11 +01001402 (stat & SEEK_STAT) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403 /* Media access command */
1404 tape->dsc_polling_start = jiffies;
1405 tape->dsc_polling_frequency = IDETAPE_DSC_MA_FAST;
1406 tape->dsc_timeout = jiffies + IDETAPE_DSC_MA_TIMEOUT;
1407 /* Allow ide.c to handle other requests */
1408 idetape_postpone_request(drive);
1409 return ide_stopped;
1410 }
1411 if (tape->failed_pc == pc)
1412 tape->failed_pc = NULL;
1413 /* Command finished - Call the callback function */
1414 return pc->callback(drive);
1415 }
1416 if (test_and_clear_bit(PC_DMA_IN_PROGRESS, &pc->flags)) {
1417 printk(KERN_ERR "ide-tape: The tape wants to issue more "
1418 "interrupts in DMA mode\n");
1419 printk(KERN_ERR "ide-tape: DMA disabled, reverting to PIO\n");
Bartlomiej Zolnierkiewicz7469aaf2007-02-17 02:40:26 +01001420 ide_dma_off(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421 return ide_do_reset(drive);
1422 }
1423 /* Get the number of bytes to transfer on this interrupt. */
Bartlomiej Zolnierkiewicz790d1232008-01-25 22:17:12 +01001424 bcount = (hwif->INB(IDE_BCOUNTH_REG) << 8) |
1425 hwif->INB(IDE_BCOUNTL_REG);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426
Bartlomiej Zolnierkiewicz8e7657a2008-01-25 22:17:12 +01001427 ireason = hwif->INB(IDE_IREASON_REG);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428
Bartlomiej Zolnierkiewicz8e7657a2008-01-25 22:17:12 +01001429 if (ireason & CD) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430 printk(KERN_ERR "ide-tape: CoD != 0 in idetape_pc_intr\n");
1431 return ide_do_reset(drive);
1432 }
Bartlomiej Zolnierkiewicz8e7657a2008-01-25 22:17:12 +01001433 if (((ireason & IO) == IO) == test_bit(PC_WRITING, &pc->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434 /* Hopefully, we will never get here */
1435 printk(KERN_ERR "ide-tape: We wanted to %s, ",
Bartlomiej Zolnierkiewicz8e7657a2008-01-25 22:17:12 +01001436 (ireason & IO) ? "Write" : "Read");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437 printk(KERN_ERR "ide-tape: but the tape wants us to %s !\n",
Bartlomiej Zolnierkiewicz8e7657a2008-01-25 22:17:12 +01001438 (ireason & IO) ? "Read" : "Write");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439 return ide_do_reset(drive);
1440 }
1441 if (!test_bit(PC_WRITING, &pc->flags)) {
1442 /* Reading - Check that we have enough space */
Bartlomiej Zolnierkiewicz790d1232008-01-25 22:17:12 +01001443 temp = pc->actually_transferred + bcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444 if (temp > pc->request_transfer) {
1445 if (temp > pc->buffer_size) {
1446 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 +01001447 idetape_discard_data(drive, bcount);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448 ide_set_handler(drive, &idetape_pc_intr, IDETAPE_WAIT_CMD, NULL);
1449 return ide_started;
1450 }
1451#if IDETAPE_DEBUG_LOG
1452 if (tape->debug_level >= 2)
1453 printk(KERN_NOTICE "ide-tape: The tape wants to send us more data than expected - allowing transfer\n");
1454#endif /* IDETAPE_DEBUG_LOG */
1455 }
1456 }
1457 if (test_bit(PC_WRITING, &pc->flags)) {
1458 if (pc->bh != NULL)
Bartlomiej Zolnierkiewicz790d1232008-01-25 22:17:12 +01001459 idetape_output_buffers(drive, pc, bcount);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460 else
1461 /* Write the current buffer */
Bartlomiej Zolnierkiewicz790d1232008-01-25 22:17:12 +01001462 hwif->atapi_output_bytes(drive, pc->current_position,
1463 bcount);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464 } else {
1465 if (pc->bh != NULL)
Bartlomiej Zolnierkiewicz790d1232008-01-25 22:17:12 +01001466 idetape_input_buffers(drive, pc, bcount);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467 else
1468 /* Read the current buffer */
Bartlomiej Zolnierkiewicz790d1232008-01-25 22:17:12 +01001469 hwif->atapi_input_bytes(drive, pc->current_position,
1470 bcount);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001471 }
1472 /* Update the current position */
Bartlomiej Zolnierkiewicz790d1232008-01-25 22:17:12 +01001473 pc->actually_transferred += bcount;
1474 pc->current_position += bcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475#if IDETAPE_DEBUG_LOG
1476 if (tape->debug_level >= 2)
Bartlomiej Zolnierkiewicz790d1232008-01-25 22:17:12 +01001477 printk(KERN_INFO "ide-tape: [cmd %x] transferred %d bytes "
1478 "on that interrupt\n", pc->c[0], bcount);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479#endif
1480 /* And set the interrupt handler again */
1481 ide_set_handler(drive, &idetape_pc_intr, IDETAPE_WAIT_CMD, NULL);
1482 return ide_started;
1483}
1484
1485/*
1486 * Packet Command Interface
1487 *
1488 * The current Packet Command is available in tape->pc, and will not
1489 * change until we finish handling it. Each packet command is associated
1490 * with a callback function that will be called when the command is
1491 * finished.
1492 *
1493 * The handling will be done in three stages:
1494 *
1495 * 1. idetape_issue_packet_command will send the packet command to the
1496 * drive, and will set the interrupt handler to idetape_pc_intr.
1497 *
1498 * 2. On each interrupt, idetape_pc_intr will be called. This step
1499 * will be repeated until the device signals us that no more
1500 * interrupts will be issued.
1501 *
1502 * 3. ATAPI Tape media access commands have immediate status with a
1503 * delayed process. In case of a successful initiation of a
1504 * media access packet command, the DSC bit will be set when the
1505 * actual execution of the command is finished.
1506 * Since the tape drive will not issue an interrupt, we have to
1507 * poll for this event. In this case, we define the request as
1508 * "low priority request" by setting rq_status to
1509 * IDETAPE_RQ_POSTPONED, set a timer to poll for DSC and exit
1510 * the driver.
1511 *
1512 * ide.c will then give higher priority to requests which
1513 * originate from the other device, until will change rq_status
1514 * to RQ_ACTIVE.
1515 *
1516 * 4. When the packet command is finished, it will be checked for errors.
1517 *
1518 * 5. In case an error was found, we queue a request sense packet
1519 * command in front of the request queue and retry the operation
1520 * up to IDETAPE_MAX_PC_RETRIES times.
1521 *
1522 * 6. In case no error was found, or we decided to give up and not
1523 * to retry again, the callback function will be called and then
1524 * we will handle the next request.
1525 *
1526 */
1527static ide_startstop_t idetape_transfer_pc(ide_drive_t *drive)
1528{
1529 ide_hwif_t *hwif = drive->hwif;
1530 idetape_tape_t *tape = drive->driver_data;
1531 idetape_pc_t *pc = tape->pc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532 int retries = 100;
1533 ide_startstop_t startstop;
Bartlomiej Zolnierkiewicz8e7657a2008-01-25 22:17:12 +01001534 u8 ireason;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535
1536 if (ide_wait_stat(&startstop,drive,DRQ_STAT,BUSY_STAT,WAIT_READY)) {
1537 printk(KERN_ERR "ide-tape: Strange, packet command initiated yet DRQ isn't asserted\n");
1538 return startstop;
1539 }
Bartlomiej Zolnierkiewicz8e7657a2008-01-25 22:17:12 +01001540 ireason = hwif->INB(IDE_IREASON_REG);
1541 while (retries-- && ((ireason & CD) == 0 || (ireason & IO))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542 printk(KERN_ERR "ide-tape: (IO,CoD != (0,1) while issuing "
1543 "a packet command, retrying\n");
1544 udelay(100);
Bartlomiej Zolnierkiewicz8e7657a2008-01-25 22:17:12 +01001545 ireason = hwif->INB(IDE_IREASON_REG);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001546 if (retries == 0) {
1547 printk(KERN_ERR "ide-tape: (IO,CoD != (0,1) while "
1548 "issuing a packet command, ignoring\n");
Bartlomiej Zolnierkiewicz8e7657a2008-01-25 22:17:12 +01001549 ireason |= CD;
1550 ireason &= ~IO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551 }
1552 }
Bartlomiej Zolnierkiewicz8e7657a2008-01-25 22:17:12 +01001553 if ((ireason & CD) == 0 || (ireason & IO)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554 printk(KERN_ERR "ide-tape: (IO,CoD) != (0,1) while issuing "
1555 "a packet command\n");
1556 return ide_do_reset(drive);
1557 }
1558 /* Set the interrupt routine */
1559 ide_set_handler(drive, &idetape_pc_intr, IDETAPE_WAIT_CMD, NULL);
1560#ifdef CONFIG_BLK_DEV_IDEDMA
1561 /* Begin DMA, if necessary */
1562 if (test_bit(PC_DMA_IN_PROGRESS, &pc->flags))
1563 hwif->dma_start(drive);
1564#endif
1565 /* Send the actual packet */
1566 HWIF(drive)->atapi_output_bytes(drive, pc->c, 12);
1567 return ide_started;
1568}
1569
1570static ide_startstop_t idetape_issue_packet_command (ide_drive_t *drive, idetape_pc_t *pc)
1571{
1572 ide_hwif_t *hwif = drive->hwif;
1573 idetape_tape_t *tape = drive->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001574 int dma_ok = 0;
Bartlomiej Zolnierkiewicz790d1232008-01-25 22:17:12 +01001575 u16 bcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001576
Linus Torvalds1da177e2005-04-16 15:20:36 -07001577 if (tape->pc->c[0] == IDETAPE_REQUEST_SENSE_CMD &&
1578 pc->c[0] == IDETAPE_REQUEST_SENSE_CMD) {
1579 printk(KERN_ERR "ide-tape: possible ide-tape.c bug - "
1580 "Two request sense in serial were issued\n");
1581 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582
1583 if (tape->failed_pc == NULL && pc->c[0] != IDETAPE_REQUEST_SENSE_CMD)
1584 tape->failed_pc = pc;
1585 /* Set the current packet command */
1586 tape->pc = pc;
1587
1588 if (pc->retries > IDETAPE_MAX_PC_RETRIES ||
1589 test_bit(PC_ABORT, &pc->flags)) {
1590 /*
1591 * We will "abort" retrying a packet command in case
1592 * a legitimate error code was received (crossing a
1593 * filemark, or end of the media, for example).
1594 */
1595 if (!test_bit(PC_ABORT, &pc->flags)) {
1596 if (!(pc->c[0] == IDETAPE_TEST_UNIT_READY_CMD &&
1597 tape->sense_key == 2 && tape->asc == 4 &&
1598 (tape->ascq == 1 || tape->ascq == 8))) {
1599 printk(KERN_ERR "ide-tape: %s: I/O error, "
1600 "pc = %2x, key = %2x, "
1601 "asc = %2x, ascq = %2x\n",
1602 tape->name, pc->c[0],
1603 tape->sense_key, tape->asc,
1604 tape->ascq);
1605 }
1606 /* Giving up */
1607 pc->error = IDETAPE_ERROR_GENERAL;
1608 }
1609 tape->failed_pc = NULL;
1610 return pc->callback(drive);
1611 }
1612#if IDETAPE_DEBUG_LOG
1613 if (tape->debug_level >= 2)
1614 printk(KERN_INFO "ide-tape: Retry number - %d, cmd = %02X\n", pc->retries, pc->c[0]);
1615#endif /* IDETAPE_DEBUG_LOG */
1616
1617 pc->retries++;
1618 /* We haven't transferred any data yet */
1619 pc->actually_transferred = 0;
1620 pc->current_position = pc->buffer;
1621 /* Request to transfer the entire buffer at once */
Bartlomiej Zolnierkiewicz790d1232008-01-25 22:17:12 +01001622 bcount = pc->request_transfer;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623
1624 if (test_and_clear_bit(PC_DMA_ERROR, &pc->flags)) {
1625 printk(KERN_WARNING "ide-tape: DMA disabled, "
1626 "reverting to PIO\n");
Bartlomiej Zolnierkiewicz7469aaf2007-02-17 02:40:26 +01001627 ide_dma_off(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001628 }
1629 if (test_bit(PC_DMA_RECOMMENDED, &pc->flags) && drive->using_dma)
1630 dma_ok = !hwif->dma_setup(drive);
1631
Bartlomiej Zolnierkiewicz2fc57382008-01-25 22:17:13 +01001632 ide_pktcmd_tf_load(drive, IDE_TFLAG_NO_SELECT_MASK |
1633 IDE_TFLAG_OUT_DEVICE, bcount, dma_ok);
1634
Linus Torvalds1da177e2005-04-16 15:20:36 -07001635 if (dma_ok) /* Will begin DMA later */
1636 set_bit(PC_DMA_IN_PROGRESS, &pc->flags);
1637 if (test_bit(IDETAPE_DRQ_INTERRUPT, &tape->flags)) {
Bartlomiej Zolnierkiewiczc1c9dbc2008-02-02 19:56:44 +01001638 ide_execute_command(drive, WIN_PACKETCMD, &idetape_transfer_pc,
1639 IDETAPE_WAIT_CMD, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001640 return ide_started;
1641 } else {
1642 hwif->OUTB(WIN_PACKETCMD, IDE_COMMAND_REG);
1643 return idetape_transfer_pc(drive);
1644 }
1645}
1646
1647/*
1648 * General packet command callback function.
1649 */
1650static ide_startstop_t idetape_pc_callback (ide_drive_t *drive)
1651{
1652 idetape_tape_t *tape = drive->driver_data;
1653
1654#if IDETAPE_DEBUG_LOG
1655 if (tape->debug_level >= 4)
1656 printk(KERN_INFO "ide-tape: Reached idetape_pc_callback\n");
1657#endif /* IDETAPE_DEBUG_LOG */
1658
1659 idetape_end_request(drive, tape->pc->error ? 0 : 1, 0);
1660 return ide_stopped;
1661}
1662
1663/*
1664 * A mode sense command is used to "sense" tape parameters.
1665 */
1666static void idetape_create_mode_sense_cmd (idetape_pc_t *pc, u8 page_code)
1667{
1668 idetape_init_pc(pc);
1669 pc->c[0] = IDETAPE_MODE_SENSE_CMD;
1670 if (page_code != IDETAPE_BLOCK_DESCRIPTOR)
1671 pc->c[1] = 8; /* DBD = 1 - Don't return block descriptors */
1672 pc->c[2] = page_code;
1673 /*
1674 * Changed pc->c[3] to 0 (255 will at best return unused info).
1675 *
1676 * For SCSI this byte is defined as subpage instead of high byte
1677 * of length and some IDE drives seem to interpret it this way
1678 * and return an error when 255 is used.
1679 */
1680 pc->c[3] = 0;
1681 pc->c[4] = 255; /* (We will just discard data in that case) */
1682 if (page_code == IDETAPE_BLOCK_DESCRIPTOR)
1683 pc->request_transfer = 12;
1684 else if (page_code == IDETAPE_CAPABILITIES_PAGE)
1685 pc->request_transfer = 24;
1686 else
1687 pc->request_transfer = 50;
1688 pc->callback = &idetape_pc_callback;
1689}
1690
1691static void calculate_speeds(ide_drive_t *drive)
1692{
1693 idetape_tape_t *tape = drive->driver_data;
1694 int full = 125, empty = 75;
1695
1696 if (time_after(jiffies, tape->controlled_pipeline_head_time + 120 * HZ)) {
1697 tape->controlled_previous_pipeline_head = tape->controlled_last_pipeline_head;
1698 tape->controlled_previous_head_time = tape->controlled_pipeline_head_time;
1699 tape->controlled_last_pipeline_head = tape->pipeline_head;
1700 tape->controlled_pipeline_head_time = jiffies;
1701 }
1702 if (time_after(jiffies, tape->controlled_pipeline_head_time + 60 * HZ))
1703 tape->controlled_pipeline_head_speed = (tape->pipeline_head - tape->controlled_last_pipeline_head) * 32 * HZ / (jiffies - tape->controlled_pipeline_head_time);
1704 else if (time_after(jiffies, tape->controlled_previous_head_time))
1705 tape->controlled_pipeline_head_speed = (tape->pipeline_head - tape->controlled_previous_pipeline_head) * 32 * HZ / (jiffies - tape->controlled_previous_head_time);
1706
1707 if (tape->nr_pending_stages < tape->max_stages /*- 1 */) {
1708 /* -1 for read mode error recovery */
1709 if (time_after(jiffies, tape->uncontrolled_previous_head_time + 10 * HZ)) {
1710 tape->uncontrolled_pipeline_head_time = jiffies;
1711 tape->uncontrolled_pipeline_head_speed = (tape->pipeline_head - tape->uncontrolled_previous_pipeline_head) * 32 * HZ / (jiffies - tape->uncontrolled_previous_head_time);
1712 }
1713 } else {
1714 tape->uncontrolled_previous_head_time = jiffies;
1715 tape->uncontrolled_previous_pipeline_head = tape->pipeline_head;
1716 if (time_after(jiffies, tape->uncontrolled_pipeline_head_time + 30 * HZ)) {
1717 tape->uncontrolled_pipeline_head_time = jiffies;
1718 }
1719 }
1720 tape->pipeline_head_speed = max(tape->uncontrolled_pipeline_head_speed, tape->controlled_pipeline_head_speed);
1721 if (tape->speed_control == 0) {
1722 tape->max_insert_speed = 5000;
1723 } else if (tape->speed_control == 1) {
1724 if (tape->nr_pending_stages >= tape->max_stages / 2)
1725 tape->max_insert_speed = tape->pipeline_head_speed +
1726 (1100 - tape->pipeline_head_speed) * 2 * (tape->nr_pending_stages - tape->max_stages / 2) / tape->max_stages;
1727 else
1728 tape->max_insert_speed = 500 +
1729 (tape->pipeline_head_speed - 500) * 2 * tape->nr_pending_stages / tape->max_stages;
1730 if (tape->nr_pending_stages >= tape->max_stages * 99 / 100)
1731 tape->max_insert_speed = 5000;
1732 } else if (tape->speed_control == 2) {
1733 tape->max_insert_speed = tape->pipeline_head_speed * empty / 100 +
1734 (tape->pipeline_head_speed * full / 100 - tape->pipeline_head_speed * empty / 100) * tape->nr_pending_stages / tape->max_stages;
1735 } else
1736 tape->max_insert_speed = tape->speed_control;
1737 tape->max_insert_speed = max(tape->max_insert_speed, 500);
1738}
1739
1740static ide_startstop_t idetape_media_access_finished (ide_drive_t *drive)
1741{
1742 idetape_tape_t *tape = drive->driver_data;
1743 idetape_pc_t *pc = tape->pc;
Bartlomiej Zolnierkiewicz22c525b2008-01-25 22:17:11 +01001744 u8 stat;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001745
Bartlomiej Zolnierkiewicz22c525b2008-01-25 22:17:11 +01001746 stat = drive->hwif->INB(IDE_STATUS_REG);
1747 if (stat & SEEK_STAT) {
1748 if (stat & ERR_STAT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001749 /* Error detected */
1750 if (pc->c[0] != IDETAPE_TEST_UNIT_READY_CMD)
1751 printk(KERN_ERR "ide-tape: %s: I/O error, ",
1752 tape->name);
1753 /* Retry operation */
1754 return idetape_retry_pc(drive);
1755 }
1756 pc->error = 0;
1757 if (tape->failed_pc == pc)
1758 tape->failed_pc = NULL;
1759 } else {
1760 pc->error = IDETAPE_ERROR_GENERAL;
1761 tape->failed_pc = NULL;
1762 }
1763 return pc->callback(drive);
1764}
1765
1766static ide_startstop_t idetape_rw_callback (ide_drive_t *drive)
1767{
1768 idetape_tape_t *tape = drive->driver_data;
1769 struct request *rq = HWGROUP(drive)->rq;
1770 int blocks = tape->pc->actually_transferred / tape->tape_block_size;
1771
1772 tape->avg_size += blocks * tape->tape_block_size;
1773 tape->insert_size += blocks * tape->tape_block_size;
1774 if (tape->insert_size > 1024 * 1024)
1775 tape->measure_insert_time = 1;
1776 if (tape->measure_insert_time) {
1777 tape->measure_insert_time = 0;
1778 tape->insert_time = jiffies;
1779 tape->insert_size = 0;
1780 }
1781 if (time_after(jiffies, tape->insert_time))
1782 tape->insert_speed = tape->insert_size / 1024 * HZ / (jiffies - tape->insert_time);
Marcelo Feitoza Parisi9bae1ff2006-03-28 01:56:46 -08001783 if (time_after_eq(jiffies, tape->avg_time + HZ)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001784 tape->avg_speed = tape->avg_size * HZ / (jiffies - tape->avg_time) / 1024;
1785 tape->avg_size = 0;
1786 tape->avg_time = jiffies;
1787 }
1788
1789#if IDETAPE_DEBUG_LOG
1790 if (tape->debug_level >= 4)
1791 printk(KERN_INFO "ide-tape: Reached idetape_rw_callback\n");
1792#endif /* IDETAPE_DEBUG_LOG */
1793
1794 tape->first_frame_position += blocks;
1795 rq->current_nr_sectors -= blocks;
1796
1797 if (!tape->pc->error)
1798 idetape_end_request(drive, 1, 0);
1799 else
1800 idetape_end_request(drive, tape->pc->error, 0);
1801 return ide_stopped;
1802}
1803
1804static void idetape_create_read_cmd(idetape_tape_t *tape, idetape_pc_t *pc, unsigned int length, struct idetape_bh *bh)
1805{
1806 idetape_init_pc(pc);
1807 pc->c[0] = IDETAPE_READ_CMD;
1808 put_unaligned(htonl(length), (unsigned int *) &pc->c[1]);
1809 pc->c[1] = 1;
1810 pc->callback = &idetape_rw_callback;
1811 pc->bh = bh;
1812 atomic_set(&bh->b_count, 0);
1813 pc->buffer = NULL;
1814 pc->request_transfer = pc->buffer_size = length * tape->tape_block_size;
1815 if (pc->request_transfer == tape->stage_size)
1816 set_bit(PC_DMA_RECOMMENDED, &pc->flags);
1817}
1818
1819static void idetape_create_read_buffer_cmd(idetape_tape_t *tape, idetape_pc_t *pc, unsigned int length, struct idetape_bh *bh)
1820{
1821 int size = 32768;
1822 struct idetape_bh *p = bh;
1823
1824 idetape_init_pc(pc);
1825 pc->c[0] = IDETAPE_READ_BUFFER_CMD;
1826 pc->c[1] = IDETAPE_RETRIEVE_FAULTY_BLOCK;
1827 pc->c[7] = size >> 8;
1828 pc->c[8] = size & 0xff;
1829 pc->callback = &idetape_pc_callback;
1830 pc->bh = bh;
1831 atomic_set(&bh->b_count, 0);
1832 pc->buffer = NULL;
1833 while (p) {
1834 atomic_set(&p->b_count, 0);
1835 p = p->b_reqnext;
1836 }
1837 pc->request_transfer = pc->buffer_size = size;
1838}
1839
1840static void idetape_create_write_cmd(idetape_tape_t *tape, idetape_pc_t *pc, unsigned int length, struct idetape_bh *bh)
1841{
1842 idetape_init_pc(pc);
1843 pc->c[0] = IDETAPE_WRITE_CMD;
1844 put_unaligned(htonl(length), (unsigned int *) &pc->c[1]);
1845 pc->c[1] = 1;
1846 pc->callback = &idetape_rw_callback;
1847 set_bit(PC_WRITING, &pc->flags);
1848 pc->bh = bh;
1849 pc->b_data = bh->b_data;
1850 pc->b_count = atomic_read(&bh->b_count);
1851 pc->buffer = NULL;
1852 pc->request_transfer = pc->buffer_size = length * tape->tape_block_size;
1853 if (pc->request_transfer == tape->stage_size)
1854 set_bit(PC_DMA_RECOMMENDED, &pc->flags);
1855}
1856
1857/*
1858 * idetape_do_request is our request handling function.
1859 */
1860static ide_startstop_t idetape_do_request(ide_drive_t *drive,
1861 struct request *rq, sector_t block)
1862{
1863 idetape_tape_t *tape = drive->driver_data;
1864 idetape_pc_t *pc = NULL;
1865 struct request *postponed_rq = tape->postponed_rq;
Bartlomiej Zolnierkiewicz22c525b2008-01-25 22:17:11 +01001866 u8 stat;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001867
1868#if IDETAPE_DEBUG_LOG
Linus Torvalds1da177e2005-04-16 15:20:36 -07001869 if (tape->debug_level >= 2)
1870 printk(KERN_INFO "ide-tape: sector: %ld, "
1871 "nr_sectors: %ld, current_nr_sectors: %d\n",
1872 rq->sector, rq->nr_sectors, rq->current_nr_sectors);
1873#endif /* IDETAPE_DEBUG_LOG */
1874
Jens Axboe4aff5e22006-08-10 08:44:47 +02001875 if (!blk_special_request(rq)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001876 /*
1877 * We do not support buffer cache originated requests.
1878 */
1879 printk(KERN_NOTICE "ide-tape: %s: Unsupported request in "
Jens Axboe4aff5e22006-08-10 08:44:47 +02001880 "request queue (%d)\n", drive->name, rq->cmd_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001881 ide_end_request(drive, 0, 0);
1882 return ide_stopped;
1883 }
1884
1885 /*
1886 * Retry a failed packet command
1887 */
1888 if (tape->failed_pc != NULL &&
1889 tape->pc->c[0] == IDETAPE_REQUEST_SENSE_CMD) {
1890 return idetape_issue_packet_command(drive, tape->failed_pc);
1891 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001892 if (postponed_rq != NULL)
1893 if (rq != postponed_rq) {
1894 printk(KERN_ERR "ide-tape: ide-tape.c bug - "
1895 "Two DSC requests were queued\n");
1896 idetape_end_request(drive, 0, 0);
1897 return ide_stopped;
1898 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001899
1900 tape->postponed_rq = NULL;
1901
1902 /*
1903 * If the tape is still busy, postpone our request and service
1904 * the other device meanwhile.
1905 */
Bartlomiej Zolnierkiewicz22c525b2008-01-25 22:17:11 +01001906 stat = drive->hwif->INB(IDE_STATUS_REG);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001907
1908 if (!drive->dsc_overlap && !(rq->cmd[0] & REQ_IDETAPE_PC2))
1909 set_bit(IDETAPE_IGNORE_DSC, &tape->flags);
1910
1911 if (drive->post_reset == 1) {
1912 set_bit(IDETAPE_IGNORE_DSC, &tape->flags);
1913 drive->post_reset = 0;
1914 }
1915
1916 if (tape->tape_still_time > 100 && tape->tape_still_time < 200)
1917 tape->measure_insert_time = 1;
1918 if (time_after(jiffies, tape->insert_time))
1919 tape->insert_speed = tape->insert_size / 1024 * HZ / (jiffies - tape->insert_time);
1920 calculate_speeds(drive);
1921 if (!test_and_clear_bit(IDETAPE_IGNORE_DSC, &tape->flags) &&
Bartlomiej Zolnierkiewicz22c525b2008-01-25 22:17:11 +01001922 (stat & SEEK_STAT) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001923 if (postponed_rq == NULL) {
1924 tape->dsc_polling_start = jiffies;
1925 tape->dsc_polling_frequency = tape->best_dsc_rw_frequency;
1926 tape->dsc_timeout = jiffies + IDETAPE_DSC_RW_TIMEOUT;
1927 } else if (time_after(jiffies, tape->dsc_timeout)) {
1928 printk(KERN_ERR "ide-tape: %s: DSC timeout\n",
1929 tape->name);
1930 if (rq->cmd[0] & REQ_IDETAPE_PC2) {
1931 idetape_media_access_finished(drive);
1932 return ide_stopped;
1933 } else {
1934 return ide_do_reset(drive);
1935 }
Marcelo Feitoza Parisi9bae1ff2006-03-28 01:56:46 -08001936 } else if (time_after(jiffies, tape->dsc_polling_start + IDETAPE_DSC_MA_THRESHOLD))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001937 tape->dsc_polling_frequency = IDETAPE_DSC_MA_SLOW;
1938 idetape_postpone_request(drive);
1939 return ide_stopped;
1940 }
1941 if (rq->cmd[0] & REQ_IDETAPE_READ) {
1942 tape->buffer_head++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001943 tape->postpone_cnt = 0;
1944 pc = idetape_next_pc_storage(drive);
1945 idetape_create_read_cmd(tape, pc, rq->current_nr_sectors, (struct idetape_bh *)rq->special);
1946 goto out;
1947 }
1948 if (rq->cmd[0] & REQ_IDETAPE_WRITE) {
1949 tape->buffer_head++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001950 tape->postpone_cnt = 0;
1951 pc = idetape_next_pc_storage(drive);
1952 idetape_create_write_cmd(tape, pc, rq->current_nr_sectors, (struct idetape_bh *)rq->special);
1953 goto out;
1954 }
1955 if (rq->cmd[0] & REQ_IDETAPE_READ_BUFFER) {
1956 tape->postpone_cnt = 0;
1957 pc = idetape_next_pc_storage(drive);
1958 idetape_create_read_buffer_cmd(tape, pc, rq->current_nr_sectors, (struct idetape_bh *)rq->special);
1959 goto out;
1960 }
1961 if (rq->cmd[0] & REQ_IDETAPE_PC1) {
1962 pc = (idetape_pc_t *) rq->buffer;
1963 rq->cmd[0] &= ~(REQ_IDETAPE_PC1);
1964 rq->cmd[0] |= REQ_IDETAPE_PC2;
1965 goto out;
1966 }
1967 if (rq->cmd[0] & REQ_IDETAPE_PC2) {
1968 idetape_media_access_finished(drive);
1969 return ide_stopped;
1970 }
1971 BUG();
1972out:
1973 return idetape_issue_packet_command(drive, pc);
1974}
1975
1976/*
1977 * Pipeline related functions
1978 */
1979static inline int idetape_pipeline_active (idetape_tape_t *tape)
1980{
1981 int rc1, rc2;
1982
1983 rc1 = test_bit(IDETAPE_PIPELINE_ACTIVE, &tape->flags);
1984 rc2 = (tape->active_data_request != NULL);
1985 return rc1;
1986}
1987
1988/*
1989 * idetape_kmalloc_stage uses __get_free_page to allocate a pipeline
1990 * stage, along with all the necessary small buffers which together make
1991 * a buffer of size tape->stage_size (or a bit more). We attempt to
1992 * combine sequential pages as much as possible.
1993 *
1994 * Returns a pointer to the new allocated stage, or NULL if we
1995 * can't (or don't want to) allocate a stage.
1996 *
1997 * Pipeline stages are optional and are used to increase performance.
1998 * If we can't allocate them, we'll manage without them.
1999 */
2000static idetape_stage_t *__idetape_kmalloc_stage (idetape_tape_t *tape, int full, int clear)
2001{
2002 idetape_stage_t *stage;
2003 struct idetape_bh *prev_bh, *bh;
2004 int pages = tape->pages_per_stage;
2005 char *b_data = NULL;
2006
Robert P. J. Day5cbded52006-12-13 00:35:56 -08002007 if ((stage = kmalloc(sizeof (idetape_stage_t),GFP_KERNEL)) == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002008 return NULL;
2009 stage->next = NULL;
2010
Robert P. J. Day5cbded52006-12-13 00:35:56 -08002011 bh = stage->bh = kmalloc(sizeof(struct idetape_bh), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002012 if (bh == NULL)
2013 goto abort;
2014 bh->b_reqnext = NULL;
2015 if ((bh->b_data = (char *) __get_free_page (GFP_KERNEL)) == NULL)
2016 goto abort;
2017 if (clear)
2018 memset(bh->b_data, 0, PAGE_SIZE);
2019 bh->b_size = PAGE_SIZE;
2020 atomic_set(&bh->b_count, full ? bh->b_size : 0);
2021
2022 while (--pages) {
2023 if ((b_data = (char *) __get_free_page (GFP_KERNEL)) == NULL)
2024 goto abort;
2025 if (clear)
2026 memset(b_data, 0, PAGE_SIZE);
2027 if (bh->b_data == b_data + PAGE_SIZE) {
2028 bh->b_size += PAGE_SIZE;
2029 bh->b_data -= PAGE_SIZE;
2030 if (full)
2031 atomic_add(PAGE_SIZE, &bh->b_count);
2032 continue;
2033 }
2034 if (b_data == bh->b_data + bh->b_size) {
2035 bh->b_size += PAGE_SIZE;
2036 if (full)
2037 atomic_add(PAGE_SIZE, &bh->b_count);
2038 continue;
2039 }
2040 prev_bh = bh;
Robert P. J. Day5cbded52006-12-13 00:35:56 -08002041 if ((bh = kmalloc(sizeof(struct idetape_bh), GFP_KERNEL)) == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002042 free_page((unsigned long) b_data);
2043 goto abort;
2044 }
2045 bh->b_reqnext = NULL;
2046 bh->b_data = b_data;
2047 bh->b_size = PAGE_SIZE;
2048 atomic_set(&bh->b_count, full ? bh->b_size : 0);
2049 prev_bh->b_reqnext = bh;
2050 }
2051 bh->b_size -= tape->excess_bh_size;
2052 if (full)
2053 atomic_sub(tape->excess_bh_size, &bh->b_count);
2054 return stage;
2055abort:
2056 __idetape_kfree_stage(stage);
2057 return NULL;
2058}
2059
2060static idetape_stage_t *idetape_kmalloc_stage (idetape_tape_t *tape)
2061{
2062 idetape_stage_t *cache_stage = tape->cache_stage;
2063
2064#if IDETAPE_DEBUG_LOG
2065 if (tape->debug_level >= 4)
2066 printk(KERN_INFO "ide-tape: Reached idetape_kmalloc_stage\n");
2067#endif /* IDETAPE_DEBUG_LOG */
2068
2069 if (tape->nr_stages >= tape->max_stages)
2070 return NULL;
2071 if (cache_stage != NULL) {
2072 tape->cache_stage = NULL;
2073 return cache_stage;
2074 }
2075 return __idetape_kmalloc_stage(tape, 0, 0);
2076}
2077
Daniel Walkerdcd96372006-06-25 05:47:37 -07002078static 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 -07002079{
2080 struct idetape_bh *bh = tape->bh;
2081 int count;
Daniel Walkerdcd96372006-06-25 05:47:37 -07002082 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002083
2084 while (n) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002085 if (bh == NULL) {
2086 printk(KERN_ERR "ide-tape: bh == NULL in "
2087 "idetape_copy_stage_from_user\n");
Daniel Walkerdcd96372006-06-25 05:47:37 -07002088 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002089 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002090 count = min((unsigned int)(bh->b_size - atomic_read(&bh->b_count)), (unsigned int)n);
Daniel Walkerdcd96372006-06-25 05:47:37 -07002091 if (copy_from_user(bh->b_data + atomic_read(&bh->b_count), buf, count))
2092 ret = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002093 n -= count;
2094 atomic_add(count, &bh->b_count);
2095 buf += count;
2096 if (atomic_read(&bh->b_count) == bh->b_size) {
2097 bh = bh->b_reqnext;
2098 if (bh)
2099 atomic_set(&bh->b_count, 0);
2100 }
2101 }
2102 tape->bh = bh;
Daniel Walkerdcd96372006-06-25 05:47:37 -07002103 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002104}
2105
Daniel Walkerdcd96372006-06-25 05:47:37 -07002106static 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 -07002107{
2108 struct idetape_bh *bh = tape->bh;
2109 int count;
Daniel Walkerdcd96372006-06-25 05:47:37 -07002110 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002111
2112 while (n) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002113 if (bh == NULL) {
2114 printk(KERN_ERR "ide-tape: bh == NULL in "
2115 "idetape_copy_stage_to_user\n");
Daniel Walkerdcd96372006-06-25 05:47:37 -07002116 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002117 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002118 count = min(tape->b_count, n);
Daniel Walkerdcd96372006-06-25 05:47:37 -07002119 if (copy_to_user(buf, tape->b_data, count))
2120 ret = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002121 n -= count;
2122 tape->b_data += count;
2123 tape->b_count -= count;
2124 buf += count;
2125 if (!tape->b_count) {
2126 tape->bh = bh = bh->b_reqnext;
2127 if (bh) {
2128 tape->b_data = bh->b_data;
2129 tape->b_count = atomic_read(&bh->b_count);
2130 }
2131 }
2132 }
Daniel Walkerdcd96372006-06-25 05:47:37 -07002133 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002134}
2135
2136static void idetape_init_merge_stage (idetape_tape_t *tape)
2137{
2138 struct idetape_bh *bh = tape->merge_stage->bh;
2139
2140 tape->bh = bh;
2141 if (tape->chrdev_direction == idetape_direction_write)
2142 atomic_set(&bh->b_count, 0);
2143 else {
2144 tape->b_data = bh->b_data;
2145 tape->b_count = atomic_read(&bh->b_count);
2146 }
2147}
2148
2149static void idetape_switch_buffers (idetape_tape_t *tape, idetape_stage_t *stage)
2150{
2151 struct idetape_bh *tmp;
2152
2153 tmp = stage->bh;
2154 stage->bh = tape->merge_stage->bh;
2155 tape->merge_stage->bh = tmp;
2156 idetape_init_merge_stage(tape);
2157}
2158
2159/*
2160 * idetape_add_stage_tail adds a new stage at the end of the pipeline.
2161 */
2162static void idetape_add_stage_tail (ide_drive_t *drive,idetape_stage_t *stage)
2163{
2164 idetape_tape_t *tape = drive->driver_data;
2165 unsigned long flags;
2166
2167#if IDETAPE_DEBUG_LOG
2168 if (tape->debug_level >= 4)
2169 printk (KERN_INFO "ide-tape: Reached idetape_add_stage_tail\n");
2170#endif /* IDETAPE_DEBUG_LOG */
2171 spin_lock_irqsave(&tape->spinlock, flags);
2172 stage->next = NULL;
2173 if (tape->last_stage != NULL)
2174 tape->last_stage->next=stage;
2175 else
2176 tape->first_stage = tape->next_stage=stage;
2177 tape->last_stage = stage;
2178 if (tape->next_stage == NULL)
2179 tape->next_stage = tape->last_stage;
2180 tape->nr_stages++;
2181 tape->nr_pending_stages++;
2182 spin_unlock_irqrestore(&tape->spinlock, flags);
2183}
2184
2185/*
2186 * idetape_wait_for_request installs a completion in a pending request
2187 * and sleeps until it is serviced.
2188 *
2189 * The caller should ensure that the request will not be serviced
2190 * before we install the completion (usually by disabling interrupts).
2191 */
2192static void idetape_wait_for_request (ide_drive_t *drive, struct request *rq)
2193{
Peter Zijlstra6e9a4732006-09-30 23:28:10 -07002194 DECLARE_COMPLETION_ONSTACK(wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002195 idetape_tape_t *tape = drive->driver_data;
2196
Jens Axboe4aff5e22006-08-10 08:44:47 +02002197 if (rq == NULL || !blk_special_request(rq)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002198 printk (KERN_ERR "ide-tape: bug: Trying to sleep on non-valid request\n");
2199 return;
2200 }
Jens Axboec00895a2006-09-30 20:29:12 +02002201 rq->end_io_data = &wait;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002202 rq->end_io = blk_end_sync_rq;
2203 spin_unlock_irq(&tape->spinlock);
2204 wait_for_completion(&wait);
2205 /* The stage and its struct request have been deallocated */
2206 spin_lock_irq(&tape->spinlock);
2207}
2208
2209static ide_startstop_t idetape_read_position_callback (ide_drive_t *drive)
2210{
2211 idetape_tape_t *tape = drive->driver_data;
2212 idetape_read_position_result_t *result;
2213
2214#if IDETAPE_DEBUG_LOG
2215 if (tape->debug_level >= 4)
2216 printk(KERN_INFO "ide-tape: Reached idetape_read_position_callback\n");
2217#endif /* IDETAPE_DEBUG_LOG */
2218
2219 if (!tape->pc->error) {
2220 result = (idetape_read_position_result_t *) tape->pc->buffer;
2221#if IDETAPE_DEBUG_LOG
2222 if (tape->debug_level >= 2)
2223 printk(KERN_INFO "ide-tape: BOP - %s\n",result->bop ? "Yes":"No");
2224 if (tape->debug_level >= 2)
2225 printk(KERN_INFO "ide-tape: EOP - %s\n",result->eop ? "Yes":"No");
2226#endif /* IDETAPE_DEBUG_LOG */
2227 if (result->bpu) {
2228 printk(KERN_INFO "ide-tape: Block location is unknown to the tape\n");
2229 clear_bit(IDETAPE_ADDRESS_VALID, &tape->flags);
2230 idetape_end_request(drive, 0, 0);
2231 } else {
2232#if IDETAPE_DEBUG_LOG
2233 if (tape->debug_level >= 2)
2234 printk(KERN_INFO "ide-tape: Block Location - %u\n", ntohl(result->first_block));
2235#endif /* IDETAPE_DEBUG_LOG */
2236 tape->partition = result->partition;
2237 tape->first_frame_position = ntohl(result->first_block);
2238 tape->last_frame_position = ntohl(result->last_block);
2239 tape->blocks_in_buffer = result->blocks_in_buffer[2];
2240 set_bit(IDETAPE_ADDRESS_VALID, &tape->flags);
2241 idetape_end_request(drive, 1, 0);
2242 }
2243 } else {
2244 idetape_end_request(drive, 0, 0);
2245 }
2246 return ide_stopped;
2247}
2248
2249/*
2250 * idetape_create_write_filemark_cmd will:
2251 *
2252 * 1. Write a filemark if write_filemark=1.
2253 * 2. Flush the device buffers without writing a filemark
2254 * if write_filemark=0.
2255 *
2256 */
2257static void idetape_create_write_filemark_cmd (ide_drive_t *drive, idetape_pc_t *pc,int write_filemark)
2258{
2259 idetape_init_pc(pc);
2260 pc->c[0] = IDETAPE_WRITE_FILEMARK_CMD;
2261 pc->c[4] = write_filemark;
2262 set_bit(PC_WAIT_FOR_DSC, &pc->flags);
2263 pc->callback = &idetape_pc_callback;
2264}
2265
2266static void idetape_create_test_unit_ready_cmd(idetape_pc_t *pc)
2267{
2268 idetape_init_pc(pc);
2269 pc->c[0] = IDETAPE_TEST_UNIT_READY_CMD;
2270 pc->callback = &idetape_pc_callback;
2271}
2272
2273/*
2274 * idetape_queue_pc_tail is based on the following functions:
2275 *
2276 * ide_do_drive_cmd from ide.c
2277 * cdrom_queue_request and cdrom_queue_packet_command from ide-cd.c
2278 *
2279 * We add a special packet command request to the tail of the request
2280 * queue, and wait for it to be serviced.
2281 *
2282 * This is not to be called from within the request handling part
2283 * of the driver ! We allocate here data in the stack, and it is valid
2284 * until the request is finished. This is not the case for the bottom
2285 * part of the driver, where we are always leaving the functions to wait
2286 * for an interrupt or a timer event.
2287 *
2288 * From the bottom part of the driver, we should allocate safe memory
2289 * using idetape_next_pc_storage and idetape_next_rq_storage, and add
2290 * the request to the request list without waiting for it to be serviced !
2291 * In that case, we usually use idetape_queue_pc_head.
2292 */
2293static int __idetape_queue_pc_tail (ide_drive_t *drive, idetape_pc_t *pc)
2294{
2295 struct ide_tape_obj *tape = drive->driver_data;
2296 struct request rq;
2297
2298 idetape_init_rq(&rq, REQ_IDETAPE_PC1);
2299 rq.buffer = (char *) pc;
2300 rq.rq_disk = tape->disk;
2301 return ide_do_drive_cmd(drive, &rq, ide_wait);
2302}
2303
2304static void idetape_create_load_unload_cmd (ide_drive_t *drive, idetape_pc_t *pc,int cmd)
2305{
2306 idetape_init_pc(pc);
2307 pc->c[0] = IDETAPE_LOAD_UNLOAD_CMD;
2308 pc->c[4] = cmd;
2309 set_bit(PC_WAIT_FOR_DSC, &pc->flags);
2310 pc->callback = &idetape_pc_callback;
2311}
2312
2313static int idetape_wait_ready(ide_drive_t *drive, unsigned long timeout)
2314{
2315 idetape_tape_t *tape = drive->driver_data;
2316 idetape_pc_t pc;
2317 int load_attempted = 0;
2318
2319 /*
2320 * Wait for the tape to become ready
2321 */
2322 set_bit(IDETAPE_MEDIUM_PRESENT, &tape->flags);
2323 timeout += jiffies;
2324 while (time_before(jiffies, timeout)) {
2325 idetape_create_test_unit_ready_cmd(&pc);
2326 if (!__idetape_queue_pc_tail(drive, &pc))
2327 return 0;
2328 if ((tape->sense_key == 2 && tape->asc == 4 && tape->ascq == 2)
2329 || (tape->asc == 0x3A)) { /* no media */
2330 if (load_attempted)
2331 return -ENOMEDIUM;
2332 idetape_create_load_unload_cmd(drive, &pc, IDETAPE_LU_LOAD_MASK);
2333 __idetape_queue_pc_tail(drive, &pc);
2334 load_attempted = 1;
2335 /* not about to be ready */
2336 } else if (!(tape->sense_key == 2 && tape->asc == 4 &&
2337 (tape->ascq == 1 || tape->ascq == 8)))
2338 return -EIO;
Nishanth Aravamudan80ce45f2005-09-10 00:27:08 -07002339 msleep(100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002340 }
2341 return -EIO;
2342}
2343
2344static int idetape_queue_pc_tail (ide_drive_t *drive,idetape_pc_t *pc)
2345{
2346 return __idetape_queue_pc_tail(drive, pc);
2347}
2348
2349static int idetape_flush_tape_buffers (ide_drive_t *drive)
2350{
2351 idetape_pc_t pc;
2352 int rc;
2353
2354 idetape_create_write_filemark_cmd(drive, &pc, 0);
2355 if ((rc = idetape_queue_pc_tail(drive, &pc)))
2356 return rc;
2357 idetape_wait_ready(drive, 60 * 5 * HZ);
2358 return 0;
2359}
2360
2361static void idetape_create_read_position_cmd (idetape_pc_t *pc)
2362{
2363 idetape_init_pc(pc);
2364 pc->c[0] = IDETAPE_READ_POSITION_CMD;
2365 pc->request_transfer = 20;
2366 pc->callback = &idetape_read_position_callback;
2367}
2368
2369static int idetape_read_position (ide_drive_t *drive)
2370{
2371 idetape_tape_t *tape = drive->driver_data;
2372 idetape_pc_t pc;
2373 int position;
2374
2375#if IDETAPE_DEBUG_LOG
2376 if (tape->debug_level >= 4)
2377 printk(KERN_INFO "ide-tape: Reached idetape_read_position\n");
2378#endif /* IDETAPE_DEBUG_LOG */
2379
2380 idetape_create_read_position_cmd(&pc);
2381 if (idetape_queue_pc_tail(drive, &pc))
2382 return -1;
2383 position = tape->first_frame_position;
2384 return position;
2385}
2386
2387static void idetape_create_locate_cmd (ide_drive_t *drive, idetape_pc_t *pc, unsigned int block, u8 partition, int skip)
2388{
2389 idetape_init_pc(pc);
2390 pc->c[0] = IDETAPE_LOCATE_CMD;
2391 pc->c[1] = 2;
2392 put_unaligned(htonl(block), (unsigned int *) &pc->c[3]);
2393 pc->c[8] = partition;
2394 set_bit(PC_WAIT_FOR_DSC, &pc->flags);
2395 pc->callback = &idetape_pc_callback;
2396}
2397
2398static int idetape_create_prevent_cmd (ide_drive_t *drive, idetape_pc_t *pc, int prevent)
2399{
2400 idetape_tape_t *tape = drive->driver_data;
2401
Borislav Petkovb6422012008-02-02 19:56:49 +01002402 /* device supports locking according to capabilities page */
2403 if (!(tape->caps[6] & 0x01))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002404 return 0;
2405
2406 idetape_init_pc(pc);
2407 pc->c[0] = IDETAPE_PREVENT_CMD;
2408 pc->c[4] = prevent;
2409 pc->callback = &idetape_pc_callback;
2410 return 1;
2411}
2412
2413static int __idetape_discard_read_pipeline (ide_drive_t *drive)
2414{
2415 idetape_tape_t *tape = drive->driver_data;
2416 unsigned long flags;
2417 int cnt;
2418
2419 if (tape->chrdev_direction != idetape_direction_read)
2420 return 0;
2421
2422 /* Remove merge stage. */
2423 cnt = tape->merge_stage_size / tape->tape_block_size;
2424 if (test_and_clear_bit(IDETAPE_FILEMARK, &tape->flags))
2425 ++cnt; /* Filemarks count as 1 sector */
2426 tape->merge_stage_size = 0;
2427 if (tape->merge_stage != NULL) {
2428 __idetape_kfree_stage(tape->merge_stage);
2429 tape->merge_stage = NULL;
2430 }
2431
2432 /* Clear pipeline flags. */
2433 clear_bit(IDETAPE_PIPELINE_ERROR, &tape->flags);
2434 tape->chrdev_direction = idetape_direction_none;
2435
2436 /* Remove pipeline stages. */
2437 if (tape->first_stage == NULL)
2438 return 0;
2439
2440 spin_lock_irqsave(&tape->spinlock, flags);
2441 tape->next_stage = NULL;
2442 if (idetape_pipeline_active(tape))
2443 idetape_wait_for_request(drive, tape->active_data_request);
2444 spin_unlock_irqrestore(&tape->spinlock, flags);
2445
2446 while (tape->first_stage != NULL) {
2447 struct request *rq_ptr = &tape->first_stage->rq;
2448
2449 cnt += rq_ptr->nr_sectors - rq_ptr->current_nr_sectors;
2450 if (rq_ptr->errors == IDETAPE_ERROR_FILEMARK)
2451 ++cnt;
2452 idetape_remove_stage_head(drive);
2453 }
2454 tape->nr_pending_stages = 0;
2455 tape->max_stages = tape->min_pipeline;
2456 return cnt;
2457}
2458
2459/*
2460 * idetape_position_tape positions the tape to the requested block
2461 * using the LOCATE packet command. A READ POSITION command is then
2462 * issued to check where we are positioned.
2463 *
2464 * Like all higher level operations, we queue the commands at the tail
2465 * of the request queue and wait for their completion.
2466 *
2467 */
2468static int idetape_position_tape (ide_drive_t *drive, unsigned int block, u8 partition, int skip)
2469{
2470 idetape_tape_t *tape = drive->driver_data;
2471 int retval;
2472 idetape_pc_t pc;
2473
2474 if (tape->chrdev_direction == idetape_direction_read)
2475 __idetape_discard_read_pipeline(drive);
2476 idetape_wait_ready(drive, 60 * 5 * HZ);
2477 idetape_create_locate_cmd(drive, &pc, block, partition, skip);
2478 retval = idetape_queue_pc_tail(drive, &pc);
2479 if (retval)
2480 return (retval);
2481
2482 idetape_create_read_position_cmd(&pc);
2483 return (idetape_queue_pc_tail(drive, &pc));
2484}
2485
2486static void idetape_discard_read_pipeline (ide_drive_t *drive, int restore_position)
2487{
2488 idetape_tape_t *tape = drive->driver_data;
2489 int cnt;
2490 int seek, position;
2491
2492 cnt = __idetape_discard_read_pipeline(drive);
2493 if (restore_position) {
2494 position = idetape_read_position(drive);
2495 seek = position > cnt ? position - cnt : 0;
2496 if (idetape_position_tape(drive, seek, 0, 0)) {
2497 printk(KERN_INFO "ide-tape: %s: position_tape failed in discard_pipeline()\n", tape->name);
2498 return;
2499 }
2500 }
2501}
2502
2503/*
2504 * idetape_queue_rw_tail generates a read/write request for the block
2505 * device interface and wait for it to be serviced.
2506 */
2507static int idetape_queue_rw_tail(ide_drive_t *drive, int cmd, int blocks, struct idetape_bh *bh)
2508{
2509 idetape_tape_t *tape = drive->driver_data;
2510 struct request rq;
2511
2512#if IDETAPE_DEBUG_LOG
2513 if (tape->debug_level >= 2)
2514 printk(KERN_INFO "ide-tape: idetape_queue_rw_tail: cmd=%d\n",cmd);
2515#endif /* IDETAPE_DEBUG_LOG */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002516 if (idetape_pipeline_active(tape)) {
2517 printk(KERN_ERR "ide-tape: bug: the pipeline is active in idetape_queue_rw_tail\n");
2518 return (0);
2519 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002520
2521 idetape_init_rq(&rq, cmd);
2522 rq.rq_disk = tape->disk;
2523 rq.special = (void *)bh;
2524 rq.sector = tape->first_frame_position;
2525 rq.nr_sectors = rq.current_nr_sectors = blocks;
2526 (void) ide_do_drive_cmd(drive, &rq, ide_wait);
2527
2528 if ((cmd & (REQ_IDETAPE_READ | REQ_IDETAPE_WRITE)) == 0)
2529 return 0;
2530
2531 if (tape->merge_stage)
2532 idetape_init_merge_stage(tape);
2533 if (rq.errors == IDETAPE_ERROR_GENERAL)
2534 return -EIO;
2535 return (tape->tape_block_size * (blocks-rq.current_nr_sectors));
2536}
2537
2538/*
2539 * idetape_insert_pipeline_into_queue is used to start servicing the
2540 * pipeline stages, starting from tape->next_stage.
2541 */
2542static void idetape_insert_pipeline_into_queue (ide_drive_t *drive)
2543{
2544 idetape_tape_t *tape = drive->driver_data;
2545
2546 if (tape->next_stage == NULL)
2547 return;
2548 if (!idetape_pipeline_active(tape)) {
2549 set_bit(IDETAPE_PIPELINE_ACTIVE, &tape->flags);
2550 idetape_active_next_stage(drive);
2551 (void) ide_do_drive_cmd(drive, tape->active_data_request, ide_end);
2552 }
2553}
2554
2555static void idetape_create_inquiry_cmd (idetape_pc_t *pc)
2556{
2557 idetape_init_pc(pc);
2558 pc->c[0] = IDETAPE_INQUIRY_CMD;
2559 pc->c[4] = pc->request_transfer = 254;
2560 pc->callback = &idetape_pc_callback;
2561}
2562
2563static void idetape_create_rewind_cmd (ide_drive_t *drive, idetape_pc_t *pc)
2564{
2565 idetape_init_pc(pc);
2566 pc->c[0] = IDETAPE_REWIND_CMD;
2567 set_bit(PC_WAIT_FOR_DSC, &pc->flags);
2568 pc->callback = &idetape_pc_callback;
2569}
2570
Linus Torvalds1da177e2005-04-16 15:20:36 -07002571static void idetape_create_erase_cmd (idetape_pc_t *pc)
2572{
2573 idetape_init_pc(pc);
2574 pc->c[0] = IDETAPE_ERASE_CMD;
2575 pc->c[1] = 1;
2576 set_bit(PC_WAIT_FOR_DSC, &pc->flags);
2577 pc->callback = &idetape_pc_callback;
2578}
2579
2580static void idetape_create_space_cmd (idetape_pc_t *pc,int count, u8 cmd)
2581{
2582 idetape_init_pc(pc);
2583 pc->c[0] = IDETAPE_SPACE_CMD;
2584 put_unaligned(htonl(count), (unsigned int *) &pc->c[1]);
2585 pc->c[1] = cmd;
2586 set_bit(PC_WAIT_FOR_DSC, &pc->flags);
2587 pc->callback = &idetape_pc_callback;
2588}
2589
2590static void idetape_wait_first_stage (ide_drive_t *drive)
2591{
2592 idetape_tape_t *tape = drive->driver_data;
2593 unsigned long flags;
2594
2595 if (tape->first_stage == NULL)
2596 return;
2597 spin_lock_irqsave(&tape->spinlock, flags);
2598 if (tape->active_stage == tape->first_stage)
2599 idetape_wait_for_request(drive, tape->active_data_request);
2600 spin_unlock_irqrestore(&tape->spinlock, flags);
2601}
2602
2603/*
2604 * idetape_add_chrdev_write_request tries to add a character device
2605 * originated write request to our pipeline. In case we don't succeed,
2606 * we revert to non-pipelined operation mode for this request.
2607 *
2608 * 1. Try to allocate a new pipeline stage.
2609 * 2. If we can't, wait for more and more requests to be serviced
2610 * and try again each time.
2611 * 3. If we still can't allocate a stage, fallback to
2612 * non-pipelined operation mode for this request.
2613 */
2614static int idetape_add_chrdev_write_request (ide_drive_t *drive, int blocks)
2615{
2616 idetape_tape_t *tape = drive->driver_data;
2617 idetape_stage_t *new_stage;
2618 unsigned long flags;
2619 struct request *rq;
2620
2621#if IDETAPE_DEBUG_LOG
2622 if (tape->debug_level >= 3)
2623 printk(KERN_INFO "ide-tape: Reached idetape_add_chrdev_write_request\n");
2624#endif /* IDETAPE_DEBUG_LOG */
2625
2626 /*
2627 * Attempt to allocate a new stage.
2628 * Pay special attention to possible race conditions.
2629 */
2630 while ((new_stage = idetape_kmalloc_stage(tape)) == NULL) {
2631 spin_lock_irqsave(&tape->spinlock, flags);
2632 if (idetape_pipeline_active(tape)) {
2633 idetape_wait_for_request(drive, tape->active_data_request);
2634 spin_unlock_irqrestore(&tape->spinlock, flags);
2635 } else {
2636 spin_unlock_irqrestore(&tape->spinlock, flags);
2637 idetape_insert_pipeline_into_queue(drive);
2638 if (idetape_pipeline_active(tape))
2639 continue;
2640 /*
2641 * Linux is short on memory. Fallback to
2642 * non-pipelined operation mode for this request.
2643 */
2644 return idetape_queue_rw_tail(drive, REQ_IDETAPE_WRITE, blocks, tape->merge_stage->bh);
2645 }
2646 }
2647 rq = &new_stage->rq;
2648 idetape_init_rq(rq, REQ_IDETAPE_WRITE);
2649 /* Doesn't actually matter - We always assume sequential access */
2650 rq->sector = tape->first_frame_position;
2651 rq->nr_sectors = rq->current_nr_sectors = blocks;
2652
2653 idetape_switch_buffers(tape, new_stage);
2654 idetape_add_stage_tail(drive, new_stage);
2655 tape->pipeline_head++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002656 calculate_speeds(drive);
2657
2658 /*
2659 * Estimate whether the tape has stopped writing by checking
2660 * if our write pipeline is currently empty. If we are not
2661 * writing anymore, wait for the pipeline to be full enough
2662 * (90%) before starting to service requests, so that we will
2663 * be able to keep up with the higher speeds of the tape.
2664 */
2665 if (!idetape_pipeline_active(tape)) {
2666 if (tape->nr_stages >= tape->max_stages * 9 / 10 ||
2667 tape->nr_stages >= tape->max_stages - tape->uncontrolled_pipeline_head_speed * 3 * 1024 / tape->tape_block_size) {
2668 tape->measure_insert_time = 1;
2669 tape->insert_time = jiffies;
2670 tape->insert_size = 0;
2671 tape->insert_speed = 0;
2672 idetape_insert_pipeline_into_queue(drive);
2673 }
2674 }
2675 if (test_and_clear_bit(IDETAPE_PIPELINE_ERROR, &tape->flags))
2676 /* Return a deferred error */
2677 return -EIO;
2678 return blocks;
2679}
2680
2681/*
2682 * idetape_wait_for_pipeline will wait until all pending pipeline
2683 * requests are serviced. Typically called on device close.
2684 */
2685static void idetape_wait_for_pipeline (ide_drive_t *drive)
2686{
2687 idetape_tape_t *tape = drive->driver_data;
2688 unsigned long flags;
2689
2690 while (tape->next_stage || idetape_pipeline_active(tape)) {
2691 idetape_insert_pipeline_into_queue(drive);
2692 spin_lock_irqsave(&tape->spinlock, flags);
2693 if (idetape_pipeline_active(tape))
2694 idetape_wait_for_request(drive, tape->active_data_request);
2695 spin_unlock_irqrestore(&tape->spinlock, flags);
2696 }
2697}
2698
2699static void idetape_empty_write_pipeline (ide_drive_t *drive)
2700{
2701 idetape_tape_t *tape = drive->driver_data;
2702 int blocks, min;
2703 struct idetape_bh *bh;
Borislav Petkov55a5d292008-02-02 19:56:49 +01002704
Linus Torvalds1da177e2005-04-16 15:20:36 -07002705 if (tape->chrdev_direction != idetape_direction_write) {
2706 printk(KERN_ERR "ide-tape: bug: Trying to empty write pipeline, but we are not writing.\n");
2707 return;
2708 }
2709 if (tape->merge_stage_size > tape->stage_size) {
2710 printk(KERN_ERR "ide-tape: bug: merge_buffer too big\n");
2711 tape->merge_stage_size = tape->stage_size;
2712 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002713 if (tape->merge_stage_size) {
2714 blocks = tape->merge_stage_size / tape->tape_block_size;
2715 if (tape->merge_stage_size % tape->tape_block_size) {
2716 unsigned int i;
2717
2718 blocks++;
2719 i = tape->tape_block_size - tape->merge_stage_size % tape->tape_block_size;
2720 bh = tape->bh->b_reqnext;
2721 while (bh) {
2722 atomic_set(&bh->b_count, 0);
2723 bh = bh->b_reqnext;
2724 }
2725 bh = tape->bh;
2726 while (i) {
2727 if (bh == NULL) {
2728
2729 printk(KERN_INFO "ide-tape: bug, bh NULL\n");
2730 break;
2731 }
2732 min = min(i, (unsigned int)(bh->b_size - atomic_read(&bh->b_count)));
2733 memset(bh->b_data + atomic_read(&bh->b_count), 0, min);
2734 atomic_add(min, &bh->b_count);
2735 i -= min;
2736 bh = bh->b_reqnext;
2737 }
2738 }
2739 (void) idetape_add_chrdev_write_request(drive, blocks);
2740 tape->merge_stage_size = 0;
2741 }
2742 idetape_wait_for_pipeline(drive);
2743 if (tape->merge_stage != NULL) {
2744 __idetape_kfree_stage(tape->merge_stage);
2745 tape->merge_stage = NULL;
2746 }
2747 clear_bit(IDETAPE_PIPELINE_ERROR, &tape->flags);
2748 tape->chrdev_direction = idetape_direction_none;
2749
2750 /*
2751 * On the next backup, perform the feedback loop again.
2752 * (I don't want to keep sense information between backups,
2753 * as some systems are constantly on, and the system load
2754 * can be totally different on the next backup).
2755 */
2756 tape->max_stages = tape->min_pipeline;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002757 if (tape->first_stage != NULL ||
2758 tape->next_stage != NULL ||
2759 tape->last_stage != NULL ||
2760 tape->nr_stages != 0) {
2761 printk(KERN_ERR "ide-tape: ide-tape pipeline bug, "
2762 "first_stage %p, next_stage %p, "
2763 "last_stage %p, nr_stages %d\n",
2764 tape->first_stage, tape->next_stage,
2765 tape->last_stage, tape->nr_stages);
2766 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002767}
2768
2769static void idetape_restart_speed_control (ide_drive_t *drive)
2770{
2771 idetape_tape_t *tape = drive->driver_data;
2772
2773 tape->restart_speed_control_req = 0;
2774 tape->pipeline_head = 0;
2775 tape->controlled_last_pipeline_head = tape->uncontrolled_last_pipeline_head = 0;
2776 tape->controlled_previous_pipeline_head = tape->uncontrolled_previous_pipeline_head = 0;
2777 tape->pipeline_head_speed = tape->controlled_pipeline_head_speed = 5000;
2778 tape->uncontrolled_pipeline_head_speed = 0;
2779 tape->controlled_pipeline_head_time = tape->uncontrolled_pipeline_head_time = jiffies;
2780 tape->controlled_previous_head_time = tape->uncontrolled_previous_head_time = jiffies;
2781}
2782
2783static int idetape_initiate_read (ide_drive_t *drive, int max_stages)
2784{
2785 idetape_tape_t *tape = drive->driver_data;
2786 idetape_stage_t *new_stage;
2787 struct request rq;
2788 int bytes_read;
Borislav Petkovb6422012008-02-02 19:56:49 +01002789 u16 blocks = *(u16 *)&tape->caps[12];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002790
2791 /* Initialize read operation */
2792 if (tape->chrdev_direction != idetape_direction_read) {
2793 if (tape->chrdev_direction == idetape_direction_write) {
2794 idetape_empty_write_pipeline(drive);
2795 idetape_flush_tape_buffers(drive);
2796 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002797 if (tape->merge_stage || tape->merge_stage_size) {
2798 printk (KERN_ERR "ide-tape: merge_stage_size should be 0 now\n");
2799 tape->merge_stage_size = 0;
2800 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002801 if ((tape->merge_stage = __idetape_kmalloc_stage(tape, 0, 0)) == NULL)
2802 return -ENOMEM;
2803 tape->chrdev_direction = idetape_direction_read;
2804
2805 /*
2806 * Issue a read 0 command to ensure that DSC handshake
2807 * is switched from completion mode to buffer available
2808 * mode.
2809 * No point in issuing this if DSC overlap isn't supported,
2810 * some drives (Seagate STT3401A) will return an error.
2811 */
2812 if (drive->dsc_overlap) {
2813 bytes_read = idetape_queue_rw_tail(drive, REQ_IDETAPE_READ, 0, tape->merge_stage->bh);
2814 if (bytes_read < 0) {
2815 __idetape_kfree_stage(tape->merge_stage);
2816 tape->merge_stage = NULL;
2817 tape->chrdev_direction = idetape_direction_none;
2818 return bytes_read;
2819 }
2820 }
2821 }
2822 if (tape->restart_speed_control_req)
2823 idetape_restart_speed_control(drive);
2824 idetape_init_rq(&rq, REQ_IDETAPE_READ);
2825 rq.sector = tape->first_frame_position;
2826 rq.nr_sectors = rq.current_nr_sectors = blocks;
2827 if (!test_bit(IDETAPE_PIPELINE_ERROR, &tape->flags) &&
2828 tape->nr_stages < max_stages) {
2829 new_stage = idetape_kmalloc_stage(tape);
2830 while (new_stage != NULL) {
2831 new_stage->rq = rq;
2832 idetape_add_stage_tail(drive, new_stage);
2833 if (tape->nr_stages >= max_stages)
2834 break;
2835 new_stage = idetape_kmalloc_stage(tape);
2836 }
2837 }
2838 if (!idetape_pipeline_active(tape)) {
2839 if (tape->nr_pending_stages >= 3 * max_stages / 4) {
2840 tape->measure_insert_time = 1;
2841 tape->insert_time = jiffies;
2842 tape->insert_size = 0;
2843 tape->insert_speed = 0;
2844 idetape_insert_pipeline_into_queue(drive);
2845 }
2846 }
2847 return 0;
2848}
2849
2850/*
2851 * idetape_add_chrdev_read_request is called from idetape_chrdev_read
2852 * to service a character device read request and add read-ahead
2853 * requests to our pipeline.
2854 */
2855static int idetape_add_chrdev_read_request (ide_drive_t *drive,int blocks)
2856{
2857 idetape_tape_t *tape = drive->driver_data;
2858 unsigned long flags;
2859 struct request *rq_ptr;
2860 int bytes_read;
2861
2862#if IDETAPE_DEBUG_LOG
2863 if (tape->debug_level >= 4)
2864 printk(KERN_INFO "ide-tape: Reached idetape_add_chrdev_read_request, %d blocks\n", blocks);
2865#endif /* IDETAPE_DEBUG_LOG */
2866
2867 /*
2868 * If we are at a filemark, return a read length of 0
2869 */
2870 if (test_bit(IDETAPE_FILEMARK, &tape->flags))
2871 return 0;
2872
2873 /*
2874 * Wait for the next block to be available at the head
2875 * of the pipeline
2876 */
2877 idetape_initiate_read(drive, tape->max_stages);
2878 if (tape->first_stage == NULL) {
2879 if (test_bit(IDETAPE_PIPELINE_ERROR, &tape->flags))
2880 return 0;
2881 return idetape_queue_rw_tail(drive, REQ_IDETAPE_READ, blocks, tape->merge_stage->bh);
2882 }
2883 idetape_wait_first_stage(drive);
2884 rq_ptr = &tape->first_stage->rq;
2885 bytes_read = tape->tape_block_size * (rq_ptr->nr_sectors - rq_ptr->current_nr_sectors);
2886 rq_ptr->nr_sectors = rq_ptr->current_nr_sectors = 0;
2887
2888
2889 if (rq_ptr->errors == IDETAPE_ERROR_EOD)
2890 return 0;
2891 else {
2892 idetape_switch_buffers(tape, tape->first_stage);
2893 if (rq_ptr->errors == IDETAPE_ERROR_FILEMARK)
2894 set_bit(IDETAPE_FILEMARK, &tape->flags);
2895 spin_lock_irqsave(&tape->spinlock, flags);
2896 idetape_remove_stage_head(drive);
2897 spin_unlock_irqrestore(&tape->spinlock, flags);
2898 tape->pipeline_head++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002899 calculate_speeds(drive);
2900 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002901 if (bytes_read > blocks * tape->tape_block_size) {
2902 printk(KERN_ERR "ide-tape: bug: trying to return more bytes than requested\n");
2903 bytes_read = blocks * tape->tape_block_size;
2904 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002905 return (bytes_read);
2906}
2907
2908static void idetape_pad_zeros (ide_drive_t *drive, int bcount)
2909{
2910 idetape_tape_t *tape = drive->driver_data;
2911 struct idetape_bh *bh;
2912 int blocks;
2913
2914 while (bcount) {
2915 unsigned int count;
2916
2917 bh = tape->merge_stage->bh;
2918 count = min(tape->stage_size, bcount);
2919 bcount -= count;
2920 blocks = count / tape->tape_block_size;
2921 while (count) {
2922 atomic_set(&bh->b_count, min(count, (unsigned int)bh->b_size));
2923 memset(bh->b_data, 0, atomic_read(&bh->b_count));
2924 count -= atomic_read(&bh->b_count);
2925 bh = bh->b_reqnext;
2926 }
2927 idetape_queue_rw_tail(drive, REQ_IDETAPE_WRITE, blocks, tape->merge_stage->bh);
2928 }
2929}
2930
2931static int idetape_pipeline_size (ide_drive_t *drive)
2932{
2933 idetape_tape_t *tape = drive->driver_data;
2934 idetape_stage_t *stage;
2935 struct request *rq;
2936 int size = 0;
2937
2938 idetape_wait_for_pipeline(drive);
2939 stage = tape->first_stage;
2940 while (stage != NULL) {
2941 rq = &stage->rq;
2942 size += tape->tape_block_size * (rq->nr_sectors-rq->current_nr_sectors);
2943 if (rq->errors == IDETAPE_ERROR_FILEMARK)
2944 size += tape->tape_block_size;
2945 stage = stage->next;
2946 }
2947 size += tape->merge_stage_size;
2948 return size;
2949}
2950
2951/*
2952 * Rewinds the tape to the Beginning Of the current Partition (BOP).
2953 *
2954 * We currently support only one partition.
2955 */
2956static int idetape_rewind_tape (ide_drive_t *drive)
2957{
2958 int retval;
2959 idetape_pc_t pc;
2960#if IDETAPE_DEBUG_LOG
2961 idetape_tape_t *tape = drive->driver_data;
2962 if (tape->debug_level >= 2)
2963 printk(KERN_INFO "ide-tape: Reached idetape_rewind_tape\n");
2964#endif /* IDETAPE_DEBUG_LOG */
2965
2966 idetape_create_rewind_cmd(drive, &pc);
2967 retval = idetape_queue_pc_tail(drive, &pc);
2968 if (retval)
2969 return retval;
2970
2971 idetape_create_read_position_cmd(&pc);
2972 retval = idetape_queue_pc_tail(drive, &pc);
2973 if (retval)
2974 return retval;
2975 return 0;
2976}
2977
2978/*
2979 * Our special ide-tape ioctl's.
2980 *
2981 * Currently there aren't any ioctl's.
2982 * mtio.h compatible commands should be issued to the character device
2983 * interface.
2984 */
2985static int idetape_blkdev_ioctl(ide_drive_t *drive, unsigned int cmd, unsigned long arg)
2986{
2987 idetape_tape_t *tape = drive->driver_data;
2988 idetape_config_t config;
2989 void __user *argp = (void __user *)arg;
2990
2991#if IDETAPE_DEBUG_LOG
2992 if (tape->debug_level >= 4)
2993 printk(KERN_INFO "ide-tape: Reached idetape_blkdev_ioctl\n");
2994#endif /* IDETAPE_DEBUG_LOG */
2995 switch (cmd) {
2996 case 0x0340:
2997 if (copy_from_user(&config, argp, sizeof (idetape_config_t)))
2998 return -EFAULT;
2999 tape->best_dsc_rw_frequency = config.dsc_rw_frequency;
3000 tape->max_stages = config.nr_stages;
3001 break;
3002 case 0x0350:
3003 config.dsc_rw_frequency = (int) tape->best_dsc_rw_frequency;
3004 config.nr_stages = tape->max_stages;
3005 if (copy_to_user(argp, &config, sizeof (idetape_config_t)))
3006 return -EFAULT;
3007 break;
3008 default:
3009 return -EIO;
3010 }
3011 return 0;
3012}
3013
3014/*
3015 * idetape_space_over_filemarks is now a bit more complicated than just
3016 * passing the command to the tape since we may have crossed some
3017 * filemarks during our pipelined read-ahead mode.
3018 *
3019 * As a minor side effect, the pipeline enables us to support MTFSFM when
3020 * the filemark is in our internal pipeline even if the tape doesn't
3021 * support spacing over filemarks in the reverse direction.
3022 */
3023static int idetape_space_over_filemarks (ide_drive_t *drive,short mt_op,int mt_count)
3024{
3025 idetape_tape_t *tape = drive->driver_data;
3026 idetape_pc_t pc;
3027 unsigned long flags;
3028 int retval,count=0;
Borislav Petkovb6422012008-02-02 19:56:49 +01003029 int sprev = !!(tape->caps[4] & 0x20);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003030
3031 if (mt_count == 0)
3032 return 0;
3033 if (MTBSF == mt_op || MTBSFM == mt_op) {
Borislav Petkovb6422012008-02-02 19:56:49 +01003034 if (!sprev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003035 return -EIO;
3036 mt_count = - mt_count;
3037 }
3038
3039 if (tape->chrdev_direction == idetape_direction_read) {
3040 /*
3041 * We have a read-ahead buffer. Scan it for crossed
3042 * filemarks.
3043 */
3044 tape->merge_stage_size = 0;
3045 if (test_and_clear_bit(IDETAPE_FILEMARK, &tape->flags))
3046 ++count;
3047 while (tape->first_stage != NULL) {
3048 if (count == mt_count) {
3049 if (mt_op == MTFSFM)
3050 set_bit(IDETAPE_FILEMARK, &tape->flags);
3051 return 0;
3052 }
3053 spin_lock_irqsave(&tape->spinlock, flags);
3054 if (tape->first_stage == tape->active_stage) {
3055 /*
3056 * We have reached the active stage in the read pipeline.
3057 * There is no point in allowing the drive to continue
3058 * reading any farther, so we stop the pipeline.
3059 *
3060 * This section should be moved to a separate subroutine,
3061 * because a similar function is performed in
3062 * __idetape_discard_read_pipeline(), for example.
3063 */
3064 tape->next_stage = NULL;
3065 spin_unlock_irqrestore(&tape->spinlock, flags);
3066 idetape_wait_first_stage(drive);
3067 tape->next_stage = tape->first_stage->next;
3068 } else
3069 spin_unlock_irqrestore(&tape->spinlock, flags);
3070 if (tape->first_stage->rq.errors == IDETAPE_ERROR_FILEMARK)
3071 ++count;
3072 idetape_remove_stage_head(drive);
3073 }
3074 idetape_discard_read_pipeline(drive, 0);
3075 }
3076
3077 /*
3078 * The filemark was not found in our internal pipeline.
3079 * Now we can issue the space command.
3080 */
3081 switch (mt_op) {
3082 case MTFSF:
3083 case MTBSF:
3084 idetape_create_space_cmd(&pc,mt_count-count,IDETAPE_SPACE_OVER_FILEMARK);
3085 return (idetape_queue_pc_tail(drive, &pc));
3086 case MTFSFM:
3087 case MTBSFM:
Borislav Petkovb6422012008-02-02 19:56:49 +01003088 if (!sprev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003089 return (-EIO);
3090 retval = idetape_space_over_filemarks(drive, MTFSF, mt_count-count);
3091 if (retval) return (retval);
3092 count = (MTBSFM == mt_op ? 1 : -1);
3093 return (idetape_space_over_filemarks(drive, MTFSF, count));
3094 default:
3095 printk(KERN_ERR "ide-tape: MTIO operation %d not supported\n",mt_op);
3096 return (-EIO);
3097 }
3098}
3099
3100
3101/*
3102 * Our character device read / write functions.
3103 *
3104 * The tape is optimized to maximize throughput when it is transferring
3105 * an integral number of the "continuous transfer limit", which is
3106 * a parameter of the specific tape (26 KB on my particular tape).
3107 * (32 kB for Onstream)
3108 *
3109 * As of version 1.3 of the driver, the character device provides an
3110 * abstract continuous view of the media - any mix of block sizes (even 1
3111 * byte) on the same backup/restore procedure is supported. The driver
3112 * will internally convert the requests to the recommended transfer unit,
3113 * so that an unmatch between the user's block size to the recommended
3114 * size will only result in a (slightly) increased driver overhead, but
3115 * will no longer hit performance.
3116 * This is not applicable to Onstream.
3117 */
3118static ssize_t idetape_chrdev_read (struct file *file, char __user *buf,
3119 size_t count, loff_t *ppos)
3120{
3121 struct ide_tape_obj *tape = ide_tape_f(file);
3122 ide_drive_t *drive = tape->drive;
3123 ssize_t bytes_read,temp, actually_read = 0, rc;
Daniel Walkerdcd96372006-06-25 05:47:37 -07003124 ssize_t ret = 0;
Borislav Petkovb6422012008-02-02 19:56:49 +01003125 u16 ctl = *(u16 *)&tape->caps[12];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003126
3127#if IDETAPE_DEBUG_LOG
3128 if (tape->debug_level >= 3)
3129 printk(KERN_INFO "ide-tape: Reached idetape_chrdev_read, count %Zd\n", count);
3130#endif /* IDETAPE_DEBUG_LOG */
3131
3132 if (tape->chrdev_direction != idetape_direction_read) {
3133 if (test_bit(IDETAPE_DETECT_BS, &tape->flags))
3134 if (count > tape->tape_block_size &&
3135 (count % tape->tape_block_size) == 0)
3136 tape->user_bs_factor = count / tape->tape_block_size;
3137 }
3138 if ((rc = idetape_initiate_read(drive, tape->max_stages)) < 0)
3139 return rc;
3140 if (count == 0)
3141 return (0);
3142 if (tape->merge_stage_size) {
3143 actually_read = min((unsigned int)(tape->merge_stage_size), (unsigned int)count);
Daniel Walkerdcd96372006-06-25 05:47:37 -07003144 if (idetape_copy_stage_to_user(tape, buf, tape->merge_stage, actually_read))
3145 ret = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003146 buf += actually_read;
3147 tape->merge_stage_size -= actually_read;
3148 count -= actually_read;
3149 }
3150 while (count >= tape->stage_size) {
Borislav Petkovb6422012008-02-02 19:56:49 +01003151 bytes_read = idetape_add_chrdev_read_request(drive, ctl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003152 if (bytes_read <= 0)
3153 goto finish;
Daniel Walkerdcd96372006-06-25 05:47:37 -07003154 if (idetape_copy_stage_to_user(tape, buf, tape->merge_stage, bytes_read))
3155 ret = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003156 buf += bytes_read;
3157 count -= bytes_read;
3158 actually_read += bytes_read;
3159 }
3160 if (count) {
Borislav Petkovb6422012008-02-02 19:56:49 +01003161 bytes_read = idetape_add_chrdev_read_request(drive, ctl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003162 if (bytes_read <= 0)
3163 goto finish;
3164 temp = min((unsigned long)count, (unsigned long)bytes_read);
Daniel Walkerdcd96372006-06-25 05:47:37 -07003165 if (idetape_copy_stage_to_user(tape, buf, tape->merge_stage, temp))
3166 ret = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003167 actually_read += temp;
3168 tape->merge_stage_size = bytes_read-temp;
3169 }
3170finish:
3171 if (!actually_read && test_bit(IDETAPE_FILEMARK, &tape->flags)) {
3172#if IDETAPE_DEBUG_LOG
3173 if (tape->debug_level >= 2)
3174 printk(KERN_INFO "ide-tape: %s: spacing over filemark\n", tape->name);
3175#endif
3176 idetape_space_over_filemarks(drive, MTFSF, 1);
3177 return 0;
3178 }
Daniel Walkerdcd96372006-06-25 05:47:37 -07003179
3180 return (ret) ? ret : actually_read;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003181}
3182
3183static ssize_t idetape_chrdev_write (struct file *file, const char __user *buf,
3184 size_t count, loff_t *ppos)
3185{
3186 struct ide_tape_obj *tape = ide_tape_f(file);
3187 ide_drive_t *drive = tape->drive;
Daniel Walkerdcd96372006-06-25 05:47:37 -07003188 ssize_t actually_written = 0;
3189 ssize_t ret = 0;
Borislav Petkovb6422012008-02-02 19:56:49 +01003190 u16 ctl = *(u16 *)&tape->caps[12];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003191
3192 /* The drive is write protected. */
3193 if (tape->write_prot)
3194 return -EACCES;
3195
3196#if IDETAPE_DEBUG_LOG
3197 if (tape->debug_level >= 3)
3198 printk(KERN_INFO "ide-tape: Reached idetape_chrdev_write, "
3199 "count %Zd\n", count);
3200#endif /* IDETAPE_DEBUG_LOG */
3201
3202 /* Initialize write operation */
3203 if (tape->chrdev_direction != idetape_direction_write) {
3204 if (tape->chrdev_direction == idetape_direction_read)
3205 idetape_discard_read_pipeline(drive, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003206 if (tape->merge_stage || tape->merge_stage_size) {
3207 printk(KERN_ERR "ide-tape: merge_stage_size "
3208 "should be 0 now\n");
3209 tape->merge_stage_size = 0;
3210 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003211 if ((tape->merge_stage = __idetape_kmalloc_stage(tape, 0, 0)) == NULL)
3212 return -ENOMEM;
3213 tape->chrdev_direction = idetape_direction_write;
3214 idetape_init_merge_stage(tape);
3215
3216 /*
3217 * Issue a write 0 command to ensure that DSC handshake
3218 * is switched from completion mode to buffer available
3219 * mode.
3220 * No point in issuing this if DSC overlap isn't supported,
3221 * some drives (Seagate STT3401A) will return an error.
3222 */
3223 if (drive->dsc_overlap) {
Daniel Walkerdcd96372006-06-25 05:47:37 -07003224 ssize_t retval = idetape_queue_rw_tail(drive, REQ_IDETAPE_WRITE, 0, tape->merge_stage->bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003225 if (retval < 0) {
3226 __idetape_kfree_stage(tape->merge_stage);
3227 tape->merge_stage = NULL;
3228 tape->chrdev_direction = idetape_direction_none;
3229 return retval;
3230 }
3231 }
3232 }
3233 if (count == 0)
3234 return (0);
3235 if (tape->restart_speed_control_req)
3236 idetape_restart_speed_control(drive);
3237 if (tape->merge_stage_size) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003238 if (tape->merge_stage_size >= tape->stage_size) {
3239 printk(KERN_ERR "ide-tape: bug: merge buffer too big\n");
3240 tape->merge_stage_size = 0;
3241 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003242 actually_written = min((unsigned int)(tape->stage_size - tape->merge_stage_size), (unsigned int)count);
Daniel Walkerdcd96372006-06-25 05:47:37 -07003243 if (idetape_copy_stage_from_user(tape, tape->merge_stage, buf, actually_written))
3244 ret = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003245 buf += actually_written;
3246 tape->merge_stage_size += actually_written;
3247 count -= actually_written;
3248
3249 if (tape->merge_stage_size == tape->stage_size) {
Daniel Walkerdcd96372006-06-25 05:47:37 -07003250 ssize_t retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003251 tape->merge_stage_size = 0;
Borislav Petkovb6422012008-02-02 19:56:49 +01003252 retval = idetape_add_chrdev_write_request(drive, ctl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003253 if (retval <= 0)
3254 return (retval);
3255 }
3256 }
3257 while (count >= tape->stage_size) {
Daniel Walkerdcd96372006-06-25 05:47:37 -07003258 ssize_t retval;
3259 if (idetape_copy_stage_from_user(tape, tape->merge_stage, buf, tape->stage_size))
3260 ret = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003261 buf += tape->stage_size;
3262 count -= tape->stage_size;
Borislav Petkovb6422012008-02-02 19:56:49 +01003263 retval = idetape_add_chrdev_write_request(drive, ctl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003264 actually_written += tape->stage_size;
3265 if (retval <= 0)
3266 return (retval);
3267 }
3268 if (count) {
3269 actually_written += count;
Daniel Walkerdcd96372006-06-25 05:47:37 -07003270 if (idetape_copy_stage_from_user(tape, tape->merge_stage, buf, count))
3271 ret = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003272 tape->merge_stage_size += count;
3273 }
Daniel Walkerdcd96372006-06-25 05:47:37 -07003274 return (ret) ? ret : actually_written;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003275}
3276
3277static int idetape_write_filemark (ide_drive_t *drive)
3278{
3279 idetape_pc_t pc;
3280
3281 /* Write a filemark */
3282 idetape_create_write_filemark_cmd(drive, &pc, 1);
3283 if (idetape_queue_pc_tail(drive, &pc)) {
3284 printk(KERN_ERR "ide-tape: Couldn't write a filemark\n");
3285 return -EIO;
3286 }
3287 return 0;
3288}
3289
3290/*
3291 * idetape_mtioctop is called from idetape_chrdev_ioctl when
3292 * the general mtio MTIOCTOP ioctl is requested.
3293 *
3294 * We currently support the following mtio.h operations:
3295 *
3296 * MTFSF - Space over mt_count filemarks in the positive direction.
3297 * The tape is positioned after the last spaced filemark.
3298 *
3299 * MTFSFM - Same as MTFSF, but the tape is positioned before the
3300 * last filemark.
3301 *
3302 * MTBSF - Steps background over mt_count filemarks, tape is
3303 * positioned before the last filemark.
3304 *
3305 * MTBSFM - Like MTBSF, only tape is positioned after the last filemark.
3306 *
3307 * Note:
3308 *
3309 * MTBSF and MTBSFM are not supported when the tape doesn't
3310 * support spacing over filemarks in the reverse direction.
3311 * In this case, MTFSFM is also usually not supported (it is
3312 * supported in the rare case in which we crossed the filemark
3313 * during our read-ahead pipelined operation mode).
3314 *
3315 * MTWEOF - Writes mt_count filemarks. Tape is positioned after
3316 * the last written filemark.
3317 *
3318 * MTREW - Rewinds tape.
3319 *
3320 * MTLOAD - Loads the tape.
3321 *
3322 * MTOFFL - Puts the tape drive "Offline": Rewinds the tape and
3323 * MTUNLOAD prevents further access until the media is replaced.
3324 *
3325 * MTNOP - Flushes tape buffers.
3326 *
3327 * MTRETEN - Retension media. This typically consists of one end
3328 * to end pass on the media.
3329 *
3330 * MTEOM - Moves to the end of recorded data.
3331 *
3332 * MTERASE - Erases tape.
3333 *
3334 * MTSETBLK - Sets the user block size to mt_count bytes. If
3335 * mt_count is 0, we will attempt to autodetect
3336 * the block size.
3337 *
3338 * MTSEEK - Positions the tape in a specific block number, where
3339 * each block is assumed to contain which user_block_size
3340 * bytes.
3341 *
3342 * MTSETPART - Switches to another tape partition.
3343 *
3344 * MTLOCK - Locks the tape door.
3345 *
3346 * MTUNLOCK - Unlocks the tape door.
3347 *
3348 * The following commands are currently not supported:
3349 *
3350 * MTFSS, MTBSS, MTWSM, MTSETDENSITY,
3351 * MTSETDRVBUFFER, MT_ST_BOOLEANS, MT_ST_WRITE_THRESHOLD.
3352 */
3353static int idetape_mtioctop (ide_drive_t *drive,short mt_op,int mt_count)
3354{
3355 idetape_tape_t *tape = drive->driver_data;
3356 idetape_pc_t pc;
3357 int i,retval;
3358
3359#if IDETAPE_DEBUG_LOG
3360 if (tape->debug_level >= 1)
3361 printk(KERN_INFO "ide-tape: Handling MTIOCTOP ioctl: "
3362 "mt_op=%d, mt_count=%d\n", mt_op, mt_count);
3363#endif /* IDETAPE_DEBUG_LOG */
3364 /*
3365 * Commands which need our pipelined read-ahead stages.
3366 */
3367 switch (mt_op) {
3368 case MTFSF:
3369 case MTFSFM:
3370 case MTBSF:
3371 case MTBSFM:
3372 if (!mt_count)
3373 return (0);
3374 return (idetape_space_over_filemarks(drive,mt_op,mt_count));
3375 default:
3376 break;
3377 }
3378 switch (mt_op) {
3379 case MTWEOF:
3380 if (tape->write_prot)
3381 return -EACCES;
3382 idetape_discard_read_pipeline(drive, 1);
3383 for (i = 0; i < mt_count; i++) {
3384 retval = idetape_write_filemark(drive);
3385 if (retval)
3386 return retval;
3387 }
3388 return (0);
3389 case MTREW:
3390 idetape_discard_read_pipeline(drive, 0);
3391 if (idetape_rewind_tape(drive))
3392 return -EIO;
3393 return 0;
3394 case MTLOAD:
3395 idetape_discard_read_pipeline(drive, 0);
3396 idetape_create_load_unload_cmd(drive, &pc, IDETAPE_LU_LOAD_MASK);
3397 return (idetape_queue_pc_tail(drive, &pc));
3398 case MTUNLOAD:
3399 case MTOFFL:
3400 /*
3401 * If door is locked, attempt to unlock before
3402 * attempting to eject.
3403 */
3404 if (tape->door_locked) {
3405 if (idetape_create_prevent_cmd(drive, &pc, 0))
3406 if (!idetape_queue_pc_tail(drive, &pc))
3407 tape->door_locked = DOOR_UNLOCKED;
3408 }
3409 idetape_discard_read_pipeline(drive, 0);
3410 idetape_create_load_unload_cmd(drive, &pc,!IDETAPE_LU_LOAD_MASK);
3411 retval = idetape_queue_pc_tail(drive, &pc);
3412 if (!retval)
3413 clear_bit(IDETAPE_MEDIUM_PRESENT, &tape->flags);
3414 return retval;
3415 case MTNOP:
3416 idetape_discard_read_pipeline(drive, 0);
3417 return (idetape_flush_tape_buffers(drive));
3418 case MTRETEN:
3419 idetape_discard_read_pipeline(drive, 0);
3420 idetape_create_load_unload_cmd(drive, &pc,IDETAPE_LU_RETENSION_MASK | IDETAPE_LU_LOAD_MASK);
3421 return (idetape_queue_pc_tail(drive, &pc));
3422 case MTEOM:
3423 idetape_create_space_cmd(&pc, 0, IDETAPE_SPACE_TO_EOD);
3424 return (idetape_queue_pc_tail(drive, &pc));
3425 case MTERASE:
3426 (void) idetape_rewind_tape(drive);
3427 idetape_create_erase_cmd(&pc);
3428 return (idetape_queue_pc_tail(drive, &pc));
3429 case MTSETBLK:
3430 if (mt_count) {
3431 if (mt_count < tape->tape_block_size || mt_count % tape->tape_block_size)
3432 return -EIO;
3433 tape->user_bs_factor = mt_count / tape->tape_block_size;
3434 clear_bit(IDETAPE_DETECT_BS, &tape->flags);
3435 } else
3436 set_bit(IDETAPE_DETECT_BS, &tape->flags);
3437 return 0;
3438 case MTSEEK:
3439 idetape_discard_read_pipeline(drive, 0);
3440 return idetape_position_tape(drive, mt_count * tape->user_bs_factor, tape->partition, 0);
3441 case MTSETPART:
3442 idetape_discard_read_pipeline(drive, 0);
3443 return (idetape_position_tape(drive, 0, mt_count, 0));
3444 case MTFSR:
3445 case MTBSR:
3446 case MTLOCK:
3447 if (!idetape_create_prevent_cmd(drive, &pc, 1))
3448 return 0;
3449 retval = idetape_queue_pc_tail(drive, &pc);
3450 if (retval) return retval;
3451 tape->door_locked = DOOR_EXPLICITLY_LOCKED;
3452 return 0;
3453 case MTUNLOCK:
3454 if (!idetape_create_prevent_cmd(drive, &pc, 0))
3455 return 0;
3456 retval = idetape_queue_pc_tail(drive, &pc);
3457 if (retval) return retval;
3458 tape->door_locked = DOOR_UNLOCKED;
3459 return 0;
3460 default:
3461 printk(KERN_ERR "ide-tape: MTIO operation %d not "
3462 "supported\n", mt_op);
3463 return (-EIO);
3464 }
3465}
3466
3467/*
3468 * Our character device ioctls.
3469 *
3470 * General mtio.h magnetic io commands are supported here, and not in
3471 * the corresponding block interface.
3472 *
3473 * The following ioctls are supported:
3474 *
3475 * MTIOCTOP - Refer to idetape_mtioctop for detailed description.
3476 *
3477 * MTIOCGET - The mt_dsreg field in the returned mtget structure
3478 * will be set to (user block size in bytes <<
3479 * MT_ST_BLKSIZE_SHIFT) & MT_ST_BLKSIZE_MASK.
3480 *
3481 * The mt_blkno is set to the current user block number.
3482 * The other mtget fields are not supported.
3483 *
3484 * MTIOCPOS - The current tape "block position" is returned. We
3485 * assume that each block contains user_block_size
3486 * bytes.
3487 *
3488 * Our own ide-tape ioctls are supported on both interfaces.
3489 */
3490static int idetape_chrdev_ioctl (struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg)
3491{
3492 struct ide_tape_obj *tape = ide_tape_f(file);
3493 ide_drive_t *drive = tape->drive;
3494 struct mtop mtop;
3495 struct mtget mtget;
3496 struct mtpos mtpos;
3497 int block_offset = 0, position = tape->first_frame_position;
3498 void __user *argp = (void __user *)arg;
3499
3500#if IDETAPE_DEBUG_LOG
3501 if (tape->debug_level >= 3)
3502 printk(KERN_INFO "ide-tape: Reached idetape_chrdev_ioctl, "
3503 "cmd=%u\n", cmd);
3504#endif /* IDETAPE_DEBUG_LOG */
3505
3506 tape->restart_speed_control_req = 1;
3507 if (tape->chrdev_direction == idetape_direction_write) {
3508 idetape_empty_write_pipeline(drive);
3509 idetape_flush_tape_buffers(drive);
3510 }
3511 if (cmd == MTIOCGET || cmd == MTIOCPOS) {
3512 block_offset = idetape_pipeline_size(drive) / (tape->tape_block_size * tape->user_bs_factor);
3513 if ((position = idetape_read_position(drive)) < 0)
3514 return -EIO;
3515 }
3516 switch (cmd) {
3517 case MTIOCTOP:
3518 if (copy_from_user(&mtop, argp, sizeof (struct mtop)))
3519 return -EFAULT;
3520 return (idetape_mtioctop(drive,mtop.mt_op,mtop.mt_count));
3521 case MTIOCGET:
3522 memset(&mtget, 0, sizeof (struct mtget));
3523 mtget.mt_type = MT_ISSCSI2;
3524 mtget.mt_blkno = position / tape->user_bs_factor - block_offset;
3525 mtget.mt_dsreg = ((tape->tape_block_size * tape->user_bs_factor) << MT_ST_BLKSIZE_SHIFT) & MT_ST_BLKSIZE_MASK;
3526 if (tape->drv_write_prot) {
3527 mtget.mt_gstat |= GMT_WR_PROT(0xffffffff);
3528 }
3529 if (copy_to_user(argp, &mtget, sizeof(struct mtget)))
3530 return -EFAULT;
3531 return 0;
3532 case MTIOCPOS:
3533 mtpos.mt_blkno = position / tape->user_bs_factor - block_offset;
3534 if (copy_to_user(argp, &mtpos, sizeof(struct mtpos)))
3535 return -EFAULT;
3536 return 0;
3537 default:
3538 if (tape->chrdev_direction == idetape_direction_read)
3539 idetape_discard_read_pipeline(drive, 1);
3540 return idetape_blkdev_ioctl(drive, cmd, arg);
3541 }
3542}
3543
3544static void idetape_get_blocksize_from_block_descriptor(ide_drive_t *drive);
3545
3546/*
3547 * Our character device open function.
3548 */
3549static int idetape_chrdev_open (struct inode *inode, struct file *filp)
3550{
3551 unsigned int minor = iminor(inode), i = minor & ~0xc0;
3552 ide_drive_t *drive;
3553 idetape_tape_t *tape;
3554 idetape_pc_t pc;
3555 int retval;
3556
3557 /*
3558 * We really want to do nonseekable_open(inode, filp); here, but some
3559 * versions of tar incorrectly call lseek on tapes and bail out if that
3560 * fails. So we disallow pread() and pwrite(), but permit lseeks.
3561 */
3562 filp->f_mode &= ~(FMODE_PREAD | FMODE_PWRITE);
3563
3564#if IDETAPE_DEBUG_LOG
3565 printk(KERN_INFO "ide-tape: Reached idetape_chrdev_open\n");
3566#endif /* IDETAPE_DEBUG_LOG */
3567
3568 if (i >= MAX_HWIFS * MAX_DRIVES)
3569 return -ENXIO;
3570
3571 if (!(tape = ide_tape_chrdev_get(i)))
3572 return -ENXIO;
3573
3574 drive = tape->drive;
3575
3576 filp->private_data = tape;
3577
3578 if (test_and_set_bit(IDETAPE_BUSY, &tape->flags)) {
3579 retval = -EBUSY;
3580 goto out_put_tape;
3581 }
3582
3583 retval = idetape_wait_ready(drive, 60 * HZ);
3584 if (retval) {
3585 clear_bit(IDETAPE_BUSY, &tape->flags);
3586 printk(KERN_ERR "ide-tape: %s: drive not ready\n", tape->name);
3587 goto out_put_tape;
3588 }
3589
3590 idetape_read_position(drive);
3591 if (!test_bit(IDETAPE_ADDRESS_VALID, &tape->flags))
3592 (void)idetape_rewind_tape(drive);
3593
3594 if (tape->chrdev_direction != idetape_direction_read)
3595 clear_bit(IDETAPE_PIPELINE_ERROR, &tape->flags);
3596
3597 /* Read block size and write protect status from drive. */
3598 idetape_get_blocksize_from_block_descriptor(drive);
3599
3600 /* Set write protect flag if device is opened as read-only. */
3601 if ((filp->f_flags & O_ACCMODE) == O_RDONLY)
3602 tape->write_prot = 1;
3603 else
3604 tape->write_prot = tape->drv_write_prot;
3605
3606 /* Make sure drive isn't write protected if user wants to write. */
3607 if (tape->write_prot) {
3608 if ((filp->f_flags & O_ACCMODE) == O_WRONLY ||
3609 (filp->f_flags & O_ACCMODE) == O_RDWR) {
3610 clear_bit(IDETAPE_BUSY, &tape->flags);
3611 retval = -EROFS;
3612 goto out_put_tape;
3613 }
3614 }
3615
3616 /*
3617 * Lock the tape drive door so user can't eject.
3618 */
3619 if (tape->chrdev_direction == idetape_direction_none) {
3620 if (idetape_create_prevent_cmd(drive, &pc, 1)) {
3621 if (!idetape_queue_pc_tail(drive, &pc)) {
3622 if (tape->door_locked != DOOR_EXPLICITLY_LOCKED)
3623 tape->door_locked = DOOR_LOCKED;
3624 }
3625 }
3626 }
3627 idetape_restart_speed_control(drive);
3628 tape->restart_speed_control_req = 0;
3629 return 0;
3630
3631out_put_tape:
3632 ide_tape_put(tape);
3633 return retval;
3634}
3635
3636static void idetape_write_release (ide_drive_t *drive, unsigned int minor)
3637{
3638 idetape_tape_t *tape = drive->driver_data;
3639
3640 idetape_empty_write_pipeline(drive);
3641 tape->merge_stage = __idetape_kmalloc_stage(tape, 1, 0);
3642 if (tape->merge_stage != NULL) {
3643 idetape_pad_zeros(drive, tape->tape_block_size * (tape->user_bs_factor - 1));
3644 __idetape_kfree_stage(tape->merge_stage);
3645 tape->merge_stage = NULL;
3646 }
3647 idetape_write_filemark(drive);
3648 idetape_flush_tape_buffers(drive);
3649 idetape_flush_tape_buffers(drive);
3650}
3651
3652/*
3653 * Our character device release function.
3654 */
3655static int idetape_chrdev_release (struct inode *inode, struct file *filp)
3656{
3657 struct ide_tape_obj *tape = ide_tape_f(filp);
3658 ide_drive_t *drive = tape->drive;
3659 idetape_pc_t pc;
3660 unsigned int minor = iminor(inode);
3661
3662 lock_kernel();
3663 tape = drive->driver_data;
3664#if IDETAPE_DEBUG_LOG
3665 if (tape->debug_level >= 3)
3666 printk(KERN_INFO "ide-tape: Reached idetape_chrdev_release\n");
3667#endif /* IDETAPE_DEBUG_LOG */
3668
3669 if (tape->chrdev_direction == idetape_direction_write)
3670 idetape_write_release(drive, minor);
3671 if (tape->chrdev_direction == idetape_direction_read) {
3672 if (minor < 128)
3673 idetape_discard_read_pipeline(drive, 1);
3674 else
3675 idetape_wait_for_pipeline(drive);
3676 }
3677 if (tape->cache_stage != NULL) {
3678 __idetape_kfree_stage(tape->cache_stage);
3679 tape->cache_stage = NULL;
3680 }
3681 if (minor < 128 && test_bit(IDETAPE_MEDIUM_PRESENT, &tape->flags))
3682 (void) idetape_rewind_tape(drive);
3683 if (tape->chrdev_direction == idetape_direction_none) {
3684 if (tape->door_locked == DOOR_LOCKED) {
3685 if (idetape_create_prevent_cmd(drive, &pc, 0)) {
3686 if (!idetape_queue_pc_tail(drive, &pc))
3687 tape->door_locked = DOOR_UNLOCKED;
3688 }
3689 }
3690 }
3691 clear_bit(IDETAPE_BUSY, &tape->flags);
3692 ide_tape_put(tape);
3693 unlock_kernel();
3694 return 0;
3695}
3696
3697/*
3698 * idetape_identify_device is called to check the contents of the
3699 * ATAPI IDENTIFY command results. We return:
3700 *
3701 * 1 If the tape can be supported by us, based on the information
3702 * we have so far.
3703 *
3704 * 0 If this tape driver is not currently supported by us.
3705 */
3706static int idetape_identify_device (ide_drive_t *drive)
3707{
3708 struct idetape_id_gcw gcw;
3709 struct hd_driveid *id = drive->id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003710
3711 if (drive->id_read == 0)
3712 return 1;
3713
3714 *((unsigned short *) &gcw) = id->config;
3715
Linus Torvalds1da177e2005-04-16 15:20:36 -07003716 /* Check that we can support this device */
3717
Bartlomiej Zolnierkiewicz16422de32008-02-02 19:56:48 +01003718 if (gcw.protocol != 2)
3719 printk(KERN_ERR "ide-tape: Protocol (0x%02x) is not ATAPI\n",
3720 gcw.protocol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003721 else if (gcw.device_type != 1)
Bartlomiej Zolnierkiewicz16422de32008-02-02 19:56:48 +01003722 printk(KERN_ERR "ide-tape: Device type (0x%02x) is not set "
3723 "to tape\n", gcw.device_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003724 else if (!gcw.removable)
3725 printk(KERN_ERR "ide-tape: The removable flag is not set\n");
3726 else if (gcw.packet_size != 0) {
Bartlomiej Zolnierkiewicz16422de32008-02-02 19:56:48 +01003727 printk(KERN_ERR "ide-tape: Packet size (0x%02x) is not 12 "
3728 "bytes long\n", gcw.packet_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003729 } else
3730 return 1;
3731 return 0;
3732}
3733
Borislav Petkov6d29c8f2008-02-02 19:56:49 +01003734static void idetape_get_inquiry_results(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003735{
3736 char *r;
3737 idetape_tape_t *tape = drive->driver_data;
3738 idetape_pc_t pc;
Borislav Petkov6d29c8f2008-02-02 19:56:49 +01003739
Linus Torvalds1da177e2005-04-16 15:20:36 -07003740 idetape_create_inquiry_cmd(&pc);
3741 if (idetape_queue_pc_tail(drive, &pc)) {
Borislav Petkov6d29c8f2008-02-02 19:56:49 +01003742 printk(KERN_ERR "ide-tape: %s: can't get INQUIRY results\n",
3743 tape->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003744 return;
3745 }
Borislav Petkov6d29c8f2008-02-02 19:56:49 +01003746 memcpy(tape->vendor_id, &pc.buffer[8], 8);
3747 memcpy(tape->product_id, &pc.buffer[16], 16);
3748 memcpy(tape->firmware_revision, &pc.buffer[32], 4);
3749
Linus Torvalds1da177e2005-04-16 15:20:36 -07003750 ide_fixstring(tape->vendor_id, 10, 0);
3751 ide_fixstring(tape->product_id, 18, 0);
3752 ide_fixstring(tape->firmware_revision, 6, 0);
3753 r = tape->firmware_revision;
3754 if (*(r + 1) == '.')
Borislav Petkov6d29c8f2008-02-02 19:56:49 +01003755 tape->firmware_revision_num = (*r - '0') * 100 +
3756 (*(r + 2) - '0') * 10 + *(r + 3) - '0';
3757 printk(KERN_INFO "ide-tape: %s <-> %s: %s %s rev %s\n",
3758 drive->name, tape->name, tape->vendor_id,
3759 tape->product_id, tape->firmware_revision);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003760}
3761
3762/*
Borislav Petkovb6422012008-02-02 19:56:49 +01003763 * Ask the tape about its various parameters. In particular, we will adjust our
3764 * data transfer buffer size to the recommended value as returned by the tape.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003765 */
3766static void idetape_get_mode_sense_results (ide_drive_t *drive)
3767{
3768 idetape_tape_t *tape = drive->driver_data;
3769 idetape_pc_t pc;
Borislav Petkovb6422012008-02-02 19:56:49 +01003770 u8 *caps;
3771 u8 speed, max_speed;
Borislav Petkov47314fa2008-02-02 19:56:48 +01003772
Linus Torvalds1da177e2005-04-16 15:20:36 -07003773 idetape_create_mode_sense_cmd(&pc, IDETAPE_CAPABILITIES_PAGE);
3774 if (idetape_queue_pc_tail(drive, &pc)) {
Borislav Petkovb6422012008-02-02 19:56:49 +01003775 printk(KERN_ERR "ide-tape: Can't get tape parameters - assuming"
3776 " some default values\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003777 tape->tape_block_size = 512;
Borislav Petkovb6422012008-02-02 19:56:49 +01003778 put_unaligned(52, (u16 *)&tape->caps[12]);
3779 put_unaligned(540, (u16 *)&tape->caps[14]);
3780 put_unaligned(6*52, (u16 *)&tape->caps[16]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003781 return;
3782 }
Borislav Petkovb6422012008-02-02 19:56:49 +01003783 caps = pc.buffer + 4 + pc.buffer[3];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003784
Borislav Petkovb6422012008-02-02 19:56:49 +01003785 /* convert to host order and save for later use */
3786 speed = be16_to_cpu(*(u16 *)&caps[14]);
3787 max_speed = be16_to_cpu(*(u16 *)&caps[8]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003788
Borislav Petkovb6422012008-02-02 19:56:49 +01003789 put_unaligned(max_speed, (u16 *)&caps[8]);
3790 put_unaligned(be16_to_cpu(*(u16 *)&caps[12]), (u16 *)&caps[12]);
3791 put_unaligned(speed, (u16 *)&caps[14]);
3792 put_unaligned(be16_to_cpu(*(u16 *)&caps[16]), (u16 *)&caps[16]);
3793
3794 if (!speed) {
3795 printk(KERN_INFO "ide-tape: %s: invalid tape speed "
3796 "(assuming 650KB/sec)\n", drive->name);
3797 put_unaligned(650, (u16 *)&caps[14]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003798 }
Borislav Petkovb6422012008-02-02 19:56:49 +01003799 if (!max_speed) {
3800 printk(KERN_INFO "ide-tape: %s: invalid max_speed "
3801 "(assuming 650KB/sec)\n", drive->name);
3802 put_unaligned(650, (u16 *)&caps[8]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003803 }
3804
Borislav Petkovb6422012008-02-02 19:56:49 +01003805 memcpy(&tape->caps, caps, 20);
3806 if (caps[7] & 0x02)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003807 tape->tape_block_size = 512;
Borislav Petkovb6422012008-02-02 19:56:49 +01003808 else if (caps[7] & 0x04)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003809 tape->tape_block_size = 1024;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003810}
3811
3812/*
3813 * ide_get_blocksize_from_block_descriptor does a mode sense page 0 with block descriptor
3814 * and if it succeeds sets the tape block size with the reported value
3815 */
3816static void idetape_get_blocksize_from_block_descriptor(ide_drive_t *drive)
3817{
3818
3819 idetape_tape_t *tape = drive->driver_data;
3820 idetape_pc_t pc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003821 idetape_parameter_block_descriptor_t *block_descrp;
Borislav Petkov47314fa2008-02-02 19:56:48 +01003822
Linus Torvalds1da177e2005-04-16 15:20:36 -07003823 idetape_create_mode_sense_cmd(&pc, IDETAPE_BLOCK_DESCRIPTOR);
3824 if (idetape_queue_pc_tail(drive, &pc)) {
3825 printk(KERN_ERR "ide-tape: Can't get block descriptor\n");
3826 if (tape->tape_block_size == 0) {
3827 printk(KERN_WARNING "ide-tape: Cannot deal with zero block size, assume 32k\n");
3828 tape->tape_block_size = 32768;
3829 }
3830 return;
3831 }
Borislav Petkov47314fa2008-02-02 19:56:48 +01003832 block_descrp = (idetape_parameter_block_descriptor_t *)(pc.buffer + 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003833 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 +01003834 tape->drv_write_prot = (pc.buffer[2] & 0x80) >> 7;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003835}
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +02003836
3837#ifdef CONFIG_IDE_PROC_FS
Linus Torvalds1da177e2005-04-16 15:20:36 -07003838static void idetape_add_settings (ide_drive_t *drive)
3839{
3840 idetape_tape_t *tape = drive->driver_data;
3841
3842/*
Bartlomiej Zolnierkiewicz14979432007-05-10 00:01:10 +02003843 * drive setting name read/write data type min max mul_factor div_factor data pointer set function
Linus Torvalds1da177e2005-04-16 15:20:36 -07003844 */
Borislav Petkovb6422012008-02-02 19:56:49 +01003845 ide_add_setting(drive, "buffer", SETTING_READ, TYPE_SHORT, 0, 0xffff,
3846 1, 2, (u16 *)&tape->caps[16], NULL);
Bartlomiej Zolnierkiewicz14979432007-05-10 00:01:10 +02003847 ide_add_setting(drive, "pipeline_min", SETTING_RW, TYPE_INT, 1, 0xffff, tape->stage_size / 1024, 1, &tape->min_pipeline, NULL);
3848 ide_add_setting(drive, "pipeline", SETTING_RW, TYPE_INT, 1, 0xffff, tape->stage_size / 1024, 1, &tape->max_stages, NULL);
3849 ide_add_setting(drive, "pipeline_max", SETTING_RW, TYPE_INT, 1, 0xffff, tape->stage_size / 1024, 1, &tape->max_pipeline, NULL);
3850 ide_add_setting(drive, "pipeline_used", SETTING_READ, TYPE_INT, 0, 0xffff, tape->stage_size / 1024, 1, &tape->nr_stages, NULL);
3851 ide_add_setting(drive, "pipeline_pending", SETTING_READ, TYPE_INT, 0, 0xffff, tape->stage_size / 1024, 1, &tape->nr_pending_stages, NULL);
Borislav Petkovb6422012008-02-02 19:56:49 +01003852 ide_add_setting(drive, "speed", SETTING_READ, TYPE_SHORT, 0, 0xffff,
3853 1, 1, (u16 *)&tape->caps[14], NULL);
Bartlomiej Zolnierkiewicz14979432007-05-10 00:01:10 +02003854 ide_add_setting(drive, "stage", SETTING_READ, TYPE_INT, 0, 0xffff, 1, 1024, &tape->stage_size, NULL);
3855 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);
3856 ide_add_setting(drive, "dsc_overlap", SETTING_RW, TYPE_BYTE, 0, 1, 1, 1, &drive->dsc_overlap, NULL);
3857 ide_add_setting(drive, "pipeline_head_speed_c",SETTING_READ, TYPE_INT, 0, 0xffff, 1, 1, &tape->controlled_pipeline_head_speed, NULL);
3858 ide_add_setting(drive, "pipeline_head_speed_u",SETTING_READ, TYPE_INT, 0, 0xffff, 1, 1, &tape->uncontrolled_pipeline_head_speed,NULL);
3859 ide_add_setting(drive, "avg_speed", SETTING_READ, TYPE_INT, 0, 0xffff, 1, 1, &tape->avg_speed, NULL);
3860 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 -07003861}
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +02003862#else
3863static inline void idetape_add_settings(ide_drive_t *drive) { ; }
3864#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003865
3866/*
3867 * ide_setup is called to:
3868 *
3869 * 1. Initialize our various state variables.
3870 * 2. Ask the tape for its capabilities.
3871 * 3. Allocate a buffer which will be used for data
3872 * transfer. The buffer size is chosen based on
3873 * the recommendation which we received in step (2).
3874 *
3875 * Note that at this point ide.c already assigned us an irq, so that
3876 * we can queue requests here and wait for their completion.
3877 */
3878static void idetape_setup (ide_drive_t *drive, idetape_tape_t *tape, int minor)
3879{
3880 unsigned long t1, tmid, tn, t;
3881 int speed;
3882 struct idetape_id_gcw gcw;
3883 int stage_size;
3884 struct sysinfo si;
Borislav Petkovb6422012008-02-02 19:56:49 +01003885 u16 *ctl = (u16 *)&tape->caps[12];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003886
3887 spin_lock_init(&tape->spinlock);
3888 drive->dsc_overlap = 1;
Bartlomiej Zolnierkiewicz4166c192008-02-01 23:09:30 +01003889 if (drive->hwif->host_flags & IDE_HFLAG_NO_DSC) {
3890 printk(KERN_INFO "ide-tape: %s: disabling DSC overlap\n",
3891 tape->name);
3892 drive->dsc_overlap = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003893 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003894 /* Seagate Travan drives do not support DSC overlap. */
3895 if (strstr(drive->id->model, "Seagate STT3401"))
3896 drive->dsc_overlap = 0;
3897 tape->minor = minor;
3898 tape->name[0] = 'h';
3899 tape->name[1] = 't';
3900 tape->name[2] = '0' + minor;
3901 tape->chrdev_direction = idetape_direction_none;
3902 tape->pc = tape->pc_stack;
3903 tape->max_insert_speed = 10000;
3904 tape->speed_control = 1;
3905 *((unsigned short *) &gcw) = drive->id->config;
3906 if (gcw.drq_type == 1)
3907 set_bit(IDETAPE_DRQ_INTERRUPT, &tape->flags);
3908
3909 tape->min_pipeline = tape->max_pipeline = tape->max_stages = 10;
3910
3911 idetape_get_inquiry_results(drive);
3912 idetape_get_mode_sense_results(drive);
3913 idetape_get_blocksize_from_block_descriptor(drive);
3914 tape->user_bs_factor = 1;
Borislav Petkovb6422012008-02-02 19:56:49 +01003915 tape->stage_size = *ctl * tape->tape_block_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003916 while (tape->stage_size > 0xffff) {
3917 printk(KERN_NOTICE "ide-tape: decreasing stage size\n");
Borislav Petkovb6422012008-02-02 19:56:49 +01003918 *ctl /= 2;
3919 tape->stage_size = *ctl * tape->tape_block_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003920 }
3921 stage_size = tape->stage_size;
3922 tape->pages_per_stage = stage_size / PAGE_SIZE;
3923 if (stage_size % PAGE_SIZE) {
3924 tape->pages_per_stage++;
3925 tape->excess_bh_size = PAGE_SIZE - stage_size % PAGE_SIZE;
3926 }
3927
Borislav Petkovb6422012008-02-02 19:56:49 +01003928 /* Select the "best" DSC read/write polling freq and pipeline size. */
3929 speed = max(*(u16 *)&tape->caps[14], *(u16 *)&tape->caps[8]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003930
3931 tape->max_stages = speed * 1000 * 10 / tape->stage_size;
3932
3933 /*
3934 * Limit memory use for pipeline to 10% of physical memory
3935 */
3936 si_meminfo(&si);
3937 if (tape->max_stages * tape->stage_size > si.totalram * si.mem_unit / 10)
3938 tape->max_stages = si.totalram * si.mem_unit / (10 * tape->stage_size);
3939 tape->max_stages = min(tape->max_stages, IDETAPE_MAX_PIPELINE_STAGES);
3940 tape->min_pipeline = min(tape->max_stages, IDETAPE_MIN_PIPELINE_STAGES);
3941 tape->max_pipeline = min(tape->max_stages * 2, IDETAPE_MAX_PIPELINE_STAGES);
3942 if (tape->max_stages == 0)
3943 tape->max_stages = tape->min_pipeline = tape->max_pipeline = 1;
3944
3945 t1 = (tape->stage_size * HZ) / (speed * 1000);
Borislav Petkovb6422012008-02-02 19:56:49 +01003946 tmid = (*(u16 *)&tape->caps[16] * 32 * HZ) / (speed * 125);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003947 tn = (IDETAPE_FIFO_THRESHOLD * tape->stage_size * HZ) / (speed * 1000);
3948
3949 if (tape->max_stages)
3950 t = tn;
3951 else
3952 t = t1;
3953
3954 /*
3955 * Ensure that the number we got makes sense; limit
3956 * it within IDETAPE_DSC_RW_MIN and IDETAPE_DSC_RW_MAX.
3957 */
3958 tape->best_dsc_rw_frequency = max_t(unsigned long, min_t(unsigned long, t, IDETAPE_DSC_RW_MAX), IDETAPE_DSC_RW_MIN);
3959 printk(KERN_INFO "ide-tape: %s <-> %s: %dKBps, %d*%dkB buffer, "
3960 "%dkB pipeline, %lums tDSC%s\n",
Borislav Petkovb6422012008-02-02 19:56:49 +01003961 drive->name, tape->name, *(u16 *)&tape->caps[14],
3962 (*(u16 *)&tape->caps[16] * 512) / tape->stage_size,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003963 tape->stage_size / 1024,
3964 tape->max_stages * tape->stage_size / 1024,
3965 tape->best_dsc_rw_frequency * 1000 / HZ,
3966 drive->using_dma ? ", DMA":"");
3967
3968 idetape_add_settings(drive);
3969}
3970
Russell King4031bbe2006-01-06 11:41:00 +00003971static void ide_tape_remove(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003972{
3973 idetape_tape_t *tape = drive->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003974
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +02003975 ide_proc_unregister_driver(drive, tape->driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003976
3977 ide_unregister_region(tape->disk);
3978
3979 ide_tape_put(tape);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003980}
3981
3982static void ide_tape_release(struct kref *kref)
3983{
3984 struct ide_tape_obj *tape = to_ide_tape(kref);
3985 ide_drive_t *drive = tape->drive;
3986 struct gendisk *g = tape->disk;
3987
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02003988 BUG_ON(tape->first_stage != NULL || tape->merge_stage_size);
3989
Linus Torvalds1da177e2005-04-16 15:20:36 -07003990 drive->dsc_overlap = 0;
3991 drive->driver_data = NULL;
Tony Jonesdbc12722007-09-25 02:03:03 +02003992 device_destroy(idetape_sysfs_class, MKDEV(IDETAPE_MAJOR, tape->minor));
3993 device_destroy(idetape_sysfs_class, MKDEV(IDETAPE_MAJOR, tape->minor + 128));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003994 idetape_devs[tape->minor] = NULL;
3995 g->private_data = NULL;
3996 put_disk(g);
3997 kfree(tape);
3998}
3999
Bartlomiej Zolnierkiewiczecfd80e2007-05-10 00:01:09 +02004000#ifdef CONFIG_IDE_PROC_FS
Linus Torvalds1da177e2005-04-16 15:20:36 -07004001static int proc_idetape_read_name
4002 (char *page, char **start, off_t off, int count, int *eof, void *data)
4003{
4004 ide_drive_t *drive = (ide_drive_t *) data;
4005 idetape_tape_t *tape = drive->driver_data;
4006 char *out = page;
4007 int len;
4008
4009 len = sprintf(out, "%s\n", tape->name);
4010 PROC_IDE_READ_RETURN(page, start, off, count, eof, len);
4011}
4012
4013static ide_proc_entry_t idetape_proc[] = {
4014 { "capacity", S_IFREG|S_IRUGO, proc_ide_read_capacity, NULL },
4015 { "name", S_IFREG|S_IRUGO, proc_idetape_read_name, NULL },
4016 { NULL, 0, NULL, NULL }
4017};
Linus Torvalds1da177e2005-04-16 15:20:36 -07004018#endif
4019
Russell King4031bbe2006-01-06 11:41:00 +00004020static int ide_tape_probe(ide_drive_t *);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004021
Linus Torvalds1da177e2005-04-16 15:20:36 -07004022static ide_driver_t idetape_driver = {
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02004023 .gen_driver = {
Laurent Riffard4ef3b8f2005-11-18 22:15:40 +01004024 .owner = THIS_MODULE,
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02004025 .name = "ide-tape",
4026 .bus = &ide_bus_type,
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02004027 },
Russell King4031bbe2006-01-06 11:41:00 +00004028 .probe = ide_tape_probe,
4029 .remove = ide_tape_remove,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004030 .version = IDETAPE_VERSION,
4031 .media = ide_tape,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004032 .supports_dsc_overlap = 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004033 .do_request = idetape_do_request,
4034 .end_request = idetape_end_request,
4035 .error = __ide_error,
4036 .abort = __ide_abort,
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +02004037#ifdef CONFIG_IDE_PROC_FS
Linus Torvalds1da177e2005-04-16 15:20:36 -07004038 .proc = idetape_proc,
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +02004039#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07004040};
4041
4042/*
4043 * Our character device supporting functions, passed to register_chrdev.
4044 */
Arjan van de Ven2b8693c2007-02-12 00:55:32 -08004045static const struct file_operations idetape_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004046 .owner = THIS_MODULE,
4047 .read = idetape_chrdev_read,
4048 .write = idetape_chrdev_write,
4049 .ioctl = idetape_chrdev_ioctl,
4050 .open = idetape_chrdev_open,
4051 .release = idetape_chrdev_release,
4052};
4053
4054static int idetape_open(struct inode *inode, struct file *filp)
4055{
4056 struct gendisk *disk = inode->i_bdev->bd_disk;
4057 struct ide_tape_obj *tape;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004058
4059 if (!(tape = ide_tape_get(disk)))
4060 return -ENXIO;
4061
Linus Torvalds1da177e2005-04-16 15:20:36 -07004062 return 0;
4063}
4064
4065static int idetape_release(struct inode *inode, struct file *filp)
4066{
4067 struct gendisk *disk = inode->i_bdev->bd_disk;
4068 struct ide_tape_obj *tape = ide_tape_g(disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004069
4070 ide_tape_put(tape);
4071
4072 return 0;
4073}
4074
4075static int idetape_ioctl(struct inode *inode, struct file *file,
4076 unsigned int cmd, unsigned long arg)
4077{
4078 struct block_device *bdev = inode->i_bdev;
4079 struct ide_tape_obj *tape = ide_tape_g(bdev->bd_disk);
4080 ide_drive_t *drive = tape->drive;
4081 int err = generic_ide_ioctl(drive, file, bdev, cmd, arg);
4082 if (err == -EINVAL)
4083 err = idetape_blkdev_ioctl(drive, cmd, arg);
4084 return err;
4085}
4086
4087static struct block_device_operations idetape_block_ops = {
4088 .owner = THIS_MODULE,
4089 .open = idetape_open,
4090 .release = idetape_release,
4091 .ioctl = idetape_ioctl,
4092};
4093
Russell King4031bbe2006-01-06 11:41:00 +00004094static int ide_tape_probe(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004095{
4096 idetape_tape_t *tape;
4097 struct gendisk *g;
4098 int minor;
4099
4100 if (!strstr("ide-tape", drive->driver_req))
4101 goto failed;
4102 if (!drive->present)
4103 goto failed;
4104 if (drive->media != ide_tape)
4105 goto failed;
4106 if (!idetape_identify_device (drive)) {
4107 printk(KERN_ERR "ide-tape: %s: not supported by this version of ide-tape\n", drive->name);
4108 goto failed;
4109 }
4110 if (drive->scsi) {
4111 printk("ide-tape: passing drive %s to ide-scsi emulation.\n", drive->name);
4112 goto failed;
4113 }
4114 if (strstr(drive->id->model, "OnStream DI-")) {
4115 printk(KERN_WARNING "ide-tape: Use drive %s with ide-scsi emulation and osst.\n", drive->name);
4116 printk(KERN_WARNING "ide-tape: OnStream support will be removed soon from ide-tape!\n");
4117 }
Robert P. J. Day5cbded52006-12-13 00:35:56 -08004118 tape = kzalloc(sizeof (idetape_tape_t), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004119 if (tape == NULL) {
4120 printk(KERN_ERR "ide-tape: %s: Can't allocate a tape structure\n", drive->name);
4121 goto failed;
4122 }
4123
4124 g = alloc_disk(1 << PARTN_BITS);
4125 if (!g)
4126 goto out_free_tape;
4127
4128 ide_init_disk(g, drive);
4129
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +02004130 ide_proc_register_driver(drive, &idetape_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004131
Linus Torvalds1da177e2005-04-16 15:20:36 -07004132 kref_init(&tape->kref);
4133
4134 tape->drive = drive;
4135 tape->driver = &idetape_driver;
4136 tape->disk = g;
4137
4138 g->private_data = &tape->driver;
4139
4140 drive->driver_data = tape;
4141
Arjan van de Vencf8b8972006-03-23 03:00:45 -08004142 mutex_lock(&idetape_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004143 for (minor = 0; idetape_devs[minor]; minor++)
4144 ;
4145 idetape_devs[minor] = tape;
Arjan van de Vencf8b8972006-03-23 03:00:45 -08004146 mutex_unlock(&idetape_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004147
4148 idetape_setup(drive, tape, minor);
4149
Tony Jonesdbc12722007-09-25 02:03:03 +02004150 device_create(idetape_sysfs_class, &drive->gendev,
4151 MKDEV(IDETAPE_MAJOR, minor), "%s", tape->name);
4152 device_create(idetape_sysfs_class, &drive->gendev,
4153 MKDEV(IDETAPE_MAJOR, minor + 128), "n%s", tape->name);
Will Dysond5dee802005-09-16 02:55:07 -07004154
Linus Torvalds1da177e2005-04-16 15:20:36 -07004155 g->fops = &idetape_block_ops;
4156 ide_register_region(g);
4157
4158 return 0;
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02004159
Linus Torvalds1da177e2005-04-16 15:20:36 -07004160out_free_tape:
4161 kfree(tape);
4162failed:
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02004163 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004164}
4165
4166MODULE_DESCRIPTION("ATAPI Streaming TAPE Driver");
4167MODULE_LICENSE("GPL");
4168
4169static void __exit idetape_exit (void)
4170{
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02004171 driver_unregister(&idetape_driver.gen_driver);
Will Dysond5dee802005-09-16 02:55:07 -07004172 class_destroy(idetape_sysfs_class);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004173 unregister_chrdev(IDETAPE_MAJOR, "ht");
4174}
4175
Bartlomiej Zolnierkiewicz17514e82005-11-19 22:24:35 +01004176static int __init idetape_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004177{
Will Dysond5dee802005-09-16 02:55:07 -07004178 int error = 1;
4179 idetape_sysfs_class = class_create(THIS_MODULE, "ide_tape");
4180 if (IS_ERR(idetape_sysfs_class)) {
4181 idetape_sysfs_class = NULL;
4182 printk(KERN_ERR "Unable to create sysfs class for ide tapes\n");
4183 error = -EBUSY;
4184 goto out;
4185 }
4186
Linus Torvalds1da177e2005-04-16 15:20:36 -07004187 if (register_chrdev(IDETAPE_MAJOR, "ht", &idetape_fops)) {
4188 printk(KERN_ERR "ide-tape: Failed to register character device interface\n");
Will Dysond5dee802005-09-16 02:55:07 -07004189 error = -EBUSY;
4190 goto out_free_class;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004191 }
Will Dysond5dee802005-09-16 02:55:07 -07004192
4193 error = driver_register(&idetape_driver.gen_driver);
4194 if (error)
4195 goto out_free_driver;
4196
4197 return 0;
4198
4199out_free_driver:
4200 driver_unregister(&idetape_driver.gen_driver);
4201out_free_class:
4202 class_destroy(idetape_sysfs_class);
4203out:
4204 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004205}
4206
Kay Sievers263756e2005-12-12 18:03:44 +01004207MODULE_ALIAS("ide:*m-tape*");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004208module_init(idetape_init);
4209module_exit(idetape_exit);
4210MODULE_ALIAS_CHARDEV_MAJOR(IDETAPE_MAJOR);