blob: 32f56eeb1efcd7600a990de85d7761a7cf190f01 [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/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 * Run time configurable parameters.
694 */
695typedef struct {
696 int dsc_rw_frequency;
697 int dsc_media_access_frequency;
698 int nr_stages;
699} idetape_config_t;
700
701/*
702 * The variables below are used for the character device interface.
703 * Additional state variables are defined in our ide_drive_t structure.
704 */
705static struct ide_tape_obj * idetape_devs[MAX_HWIFS * MAX_DRIVES];
706
707#define ide_tape_f(file) ((file)->private_data)
708
709static struct ide_tape_obj *ide_tape_chrdev_get(unsigned int i)
710{
711 struct ide_tape_obj *tape = NULL;
712
Arjan van de Vencf8b8972006-03-23 03:00:45 -0800713 mutex_lock(&idetape_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 tape = idetape_devs[i];
715 if (tape)
716 kref_get(&tape->kref);
Arjan van de Vencf8b8972006-03-23 03:00:45 -0800717 mutex_unlock(&idetape_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718 return tape;
719}
720
721/*
722 * Function declarations
723 *
724 */
725static int idetape_chrdev_release (struct inode *inode, struct file *filp);
726static void idetape_write_release (ide_drive_t *drive, unsigned int minor);
727
728/*
729 * Too bad. The drive wants to send us data which we are not ready to accept.
730 * Just throw it away.
731 */
732static void idetape_discard_data (ide_drive_t *drive, unsigned int bcount)
733{
734 while (bcount--)
735 (void) HWIF(drive)->INB(IDE_DATA_REG);
736}
737
738static void idetape_input_buffers (ide_drive_t *drive, idetape_pc_t *pc, unsigned int bcount)
739{
740 struct idetape_bh *bh = pc->bh;
741 int count;
742
743 while (bcount) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744 if (bh == NULL) {
745 printk(KERN_ERR "ide-tape: bh == NULL in "
746 "idetape_input_buffers\n");
747 idetape_discard_data(drive, bcount);
748 return;
749 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750 count = min((unsigned int)(bh->b_size - atomic_read(&bh->b_count)), bcount);
751 HWIF(drive)->atapi_input_bytes(drive, bh->b_data + atomic_read(&bh->b_count), count);
752 bcount -= count;
753 atomic_add(count, &bh->b_count);
754 if (atomic_read(&bh->b_count) == bh->b_size) {
755 bh = bh->b_reqnext;
756 if (bh)
757 atomic_set(&bh->b_count, 0);
758 }
759 }
760 pc->bh = bh;
761}
762
763static void idetape_output_buffers (ide_drive_t *drive, idetape_pc_t *pc, unsigned int bcount)
764{
765 struct idetape_bh *bh = pc->bh;
766 int count;
767
768 while (bcount) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769 if (bh == NULL) {
770 printk(KERN_ERR "ide-tape: bh == NULL in "
771 "idetape_output_buffers\n");
772 return;
773 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774 count = min((unsigned int)pc->b_count, (unsigned int)bcount);
775 HWIF(drive)->atapi_output_bytes(drive, pc->b_data, count);
776 bcount -= count;
777 pc->b_data += count;
778 pc->b_count -= count;
779 if (!pc->b_count) {
780 pc->bh = bh = bh->b_reqnext;
781 if (bh) {
782 pc->b_data = bh->b_data;
783 pc->b_count = atomic_read(&bh->b_count);
784 }
785 }
786 }
787}
788
789static void idetape_update_buffers (idetape_pc_t *pc)
790{
791 struct idetape_bh *bh = pc->bh;
792 int count;
793 unsigned int bcount = pc->actually_transferred;
794
795 if (test_bit(PC_WRITING, &pc->flags))
796 return;
797 while (bcount) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 if (bh == NULL) {
799 printk(KERN_ERR "ide-tape: bh == NULL in "
800 "idetape_update_buffers\n");
801 return;
802 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803 count = min((unsigned int)bh->b_size, (unsigned int)bcount);
804 atomic_set(&bh->b_count, count);
805 if (atomic_read(&bh->b_count) == bh->b_size)
806 bh = bh->b_reqnext;
807 bcount -= count;
808 }
809 pc->bh = bh;
810}
811
812/*
813 * idetape_next_pc_storage returns a pointer to a place in which we can
814 * safely store a packet command, even though we intend to leave the
815 * driver. A storage space for a maximum of IDETAPE_PC_STACK packet
816 * commands is allocated at initialization time.
817 */
818static idetape_pc_t *idetape_next_pc_storage (ide_drive_t *drive)
819{
820 idetape_tape_t *tape = drive->driver_data;
821
822#if IDETAPE_DEBUG_LOG
823 if (tape->debug_level >= 5)
824 printk(KERN_INFO "ide-tape: pc_stack_index=%d\n",
825 tape->pc_stack_index);
826#endif /* IDETAPE_DEBUG_LOG */
827 if (tape->pc_stack_index == IDETAPE_PC_STACK)
828 tape->pc_stack_index=0;
829 return (&tape->pc_stack[tape->pc_stack_index++]);
830}
831
832/*
833 * idetape_next_rq_storage is used along with idetape_next_pc_storage.
834 * Since we queue packet commands in the request queue, we need to
835 * allocate a request, along with the allocation of a packet command.
836 */
837
838/**************************************************************
839 * *
840 * This should get fixed to use kmalloc(.., GFP_ATOMIC) *
841 * followed later on by kfree(). -ml *
842 * *
843 **************************************************************/
844
845static struct request *idetape_next_rq_storage (ide_drive_t *drive)
846{
847 idetape_tape_t *tape = drive->driver_data;
848
849#if IDETAPE_DEBUG_LOG
850 if (tape->debug_level >= 5)
851 printk(KERN_INFO "ide-tape: rq_stack_index=%d\n",
852 tape->rq_stack_index);
853#endif /* IDETAPE_DEBUG_LOG */
854 if (tape->rq_stack_index == IDETAPE_PC_STACK)
855 tape->rq_stack_index=0;
856 return (&tape->rq_stack[tape->rq_stack_index++]);
857}
858
859/*
860 * idetape_init_pc initializes a packet command.
861 */
862static void idetape_init_pc (idetape_pc_t *pc)
863{
864 memset(pc->c, 0, 12);
865 pc->retries = 0;
866 pc->flags = 0;
867 pc->request_transfer = 0;
868 pc->buffer = pc->pc_buffer;
869 pc->buffer_size = IDETAPE_PC_BUFFER_SIZE;
870 pc->bh = NULL;
871 pc->b_data = NULL;
872}
873
874/*
Borislav Petkov1b5db432008-02-02 19:56:48 +0100875 * called on each failed packet command retry to analyze the request sense. We
876 * currently do not utilize this information.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877 */
Borislav Petkov1b5db432008-02-02 19:56:48 +0100878static void idetape_analyze_error(ide_drive_t *drive, u8 *sense)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879{
880 idetape_tape_t *tape = drive->driver_data;
881 idetape_pc_t *pc = tape->failed_pc;
882
Borislav Petkov1b5db432008-02-02 19:56:48 +0100883 tape->sense_key = sense[2] & 0xF;
884 tape->asc = sense[12];
885 tape->ascq = sense[13];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886#if IDETAPE_DEBUG_LOG
887 /*
Borislav Petkov1b5db432008-02-02 19:56:48 +0100888 * Without debugging, we only log an error if we decided to give up
889 * retrying.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890 */
891 if (tape->debug_level >= 1)
892 printk(KERN_INFO "ide-tape: pc = %x, sense key = %x, "
893 "asc = %x, ascq = %x\n",
Borislav Petkov1b5db432008-02-02 19:56:48 +0100894 pc->c[0], tape->sense_key,
895 tape->asc, tape->ascq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896#endif /* IDETAPE_DEBUG_LOG */
897
Borislav Petkov1b5db432008-02-02 19:56:48 +0100898 /* Correct pc->actually_transferred by asking the tape. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899 if (test_bit(PC_DMA_ERROR, &pc->flags)) {
Borislav Petkov1b5db432008-02-02 19:56:48 +0100900 pc->actually_transferred = pc->request_transfer -
901 tape->tape_block_size *
902 ntohl(get_unaligned((u32 *)&sense[3]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903 idetape_update_buffers(pc);
904 }
905
906 /*
907 * If error was the result of a zero-length read or write command,
908 * with sense key=5, asc=0x22, ascq=0, let it slide. Some drives
909 * (i.e. Seagate STT3401A Travan) don't support 0-length read/writes.
910 */
911 if ((pc->c[0] == IDETAPE_READ_CMD || pc->c[0] == IDETAPE_WRITE_CMD)
Borislav Petkov1b5db432008-02-02 19:56:48 +0100912 /* length == 0 */
913 && pc->c[4] == 0 && pc->c[3] == 0 && pc->c[2] == 0) {
914 if (tape->sense_key == 5) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915 /* don't report an error, everything's ok */
916 pc->error = 0;
917 /* don't retry read/write */
918 set_bit(PC_ABORT, &pc->flags);
919 }
920 }
Borislav Petkov1b5db432008-02-02 19:56:48 +0100921 if (pc->c[0] == IDETAPE_READ_CMD && (sense[2] & 0x80)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922 pc->error = IDETAPE_ERROR_FILEMARK;
923 set_bit(PC_ABORT, &pc->flags);
924 }
925 if (pc->c[0] == IDETAPE_WRITE_CMD) {
Borislav Petkov1b5db432008-02-02 19:56:48 +0100926 if ((sense[2] & 0x40) || (tape->sense_key == 0xd
927 && tape->asc == 0x0 && tape->ascq == 0x2)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928 pc->error = IDETAPE_ERROR_EOD;
929 set_bit(PC_ABORT, &pc->flags);
930 }
931 }
932 if (pc->c[0] == IDETAPE_READ_CMD || pc->c[0] == IDETAPE_WRITE_CMD) {
Borislav Petkov1b5db432008-02-02 19:56:48 +0100933 if (tape->sense_key == 8) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934 pc->error = IDETAPE_ERROR_EOD;
935 set_bit(PC_ABORT, &pc->flags);
936 }
937 if (!test_bit(PC_ABORT, &pc->flags) &&
938 pc->actually_transferred)
939 pc->retries = IDETAPE_MAX_PC_RETRIES + 1;
940 }
941}
942
943/*
944 * idetape_active_next_stage will declare the next stage as "active".
945 */
946static void idetape_active_next_stage (ide_drive_t *drive)
947{
948 idetape_tape_t *tape = drive->driver_data;
949 idetape_stage_t *stage = tape->next_stage;
950 struct request *rq = &stage->rq;
951
952#if IDETAPE_DEBUG_LOG
953 if (tape->debug_level >= 4)
954 printk(KERN_INFO "ide-tape: Reached idetape_active_next_stage\n");
955#endif /* IDETAPE_DEBUG_LOG */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956 if (stage == NULL) {
957 printk(KERN_ERR "ide-tape: bug: Trying to activate a non existing stage\n");
958 return;
959 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960
961 rq->rq_disk = tape->disk;
962 rq->buffer = NULL;
963 rq->special = (void *)stage->bh;
964 tape->active_data_request = rq;
965 tape->active_stage = stage;
966 tape->next_stage = stage->next;
967}
968
969/*
970 * idetape_increase_max_pipeline_stages is a part of the feedback
971 * loop which tries to find the optimum number of stages. In the
972 * feedback loop, we are starting from a minimum maximum number of
973 * stages, and if we sense that the pipeline is empty, we try to
974 * increase it, until we reach the user compile time memory limit.
975 */
976static void idetape_increase_max_pipeline_stages (ide_drive_t *drive)
977{
978 idetape_tape_t *tape = drive->driver_data;
979 int increase = (tape->max_pipeline - tape->min_pipeline) / 10;
980
981#if IDETAPE_DEBUG_LOG
982 if (tape->debug_level >= 4)
983 printk (KERN_INFO "ide-tape: Reached idetape_increase_max_pipeline_stages\n");
984#endif /* IDETAPE_DEBUG_LOG */
985
986 tape->max_stages += max(increase, 1);
987 tape->max_stages = max(tape->max_stages, tape->min_pipeline);
988 tape->max_stages = min(tape->max_stages, tape->max_pipeline);
989}
990
991/*
992 * idetape_kfree_stage calls kfree to completely free a stage, along with
993 * its related buffers.
994 */
995static void __idetape_kfree_stage (idetape_stage_t *stage)
996{
997 struct idetape_bh *prev_bh, *bh = stage->bh;
998 int size;
999
1000 while (bh != NULL) {
1001 if (bh->b_data != NULL) {
1002 size = (int) bh->b_size;
1003 while (size > 0) {
1004 free_page((unsigned long) bh->b_data);
1005 size -= PAGE_SIZE;
1006 bh->b_data += PAGE_SIZE;
1007 }
1008 }
1009 prev_bh = bh;
1010 bh = bh->b_reqnext;
1011 kfree(prev_bh);
1012 }
1013 kfree(stage);
1014}
1015
1016static void idetape_kfree_stage (idetape_tape_t *tape, idetape_stage_t *stage)
1017{
1018 __idetape_kfree_stage(stage);
1019}
1020
1021/*
1022 * idetape_remove_stage_head removes tape->first_stage from the pipeline.
1023 * The caller should avoid race conditions.
1024 */
1025static void idetape_remove_stage_head (ide_drive_t *drive)
1026{
1027 idetape_tape_t *tape = drive->driver_data;
1028 idetape_stage_t *stage;
1029
1030#if IDETAPE_DEBUG_LOG
1031 if (tape->debug_level >= 4)
1032 printk(KERN_INFO "ide-tape: Reached idetape_remove_stage_head\n");
1033#endif /* IDETAPE_DEBUG_LOG */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034 if (tape->first_stage == NULL) {
1035 printk(KERN_ERR "ide-tape: bug: tape->first_stage is NULL\n");
Borislav Petkov55a5d292008-02-02 19:56:49 +01001036 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037 }
1038 if (tape->active_stage == tape->first_stage) {
1039 printk(KERN_ERR "ide-tape: bug: Trying to free our active pipeline stage\n");
1040 return;
1041 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042 stage = tape->first_stage;
1043 tape->first_stage = stage->next;
1044 idetape_kfree_stage(tape, stage);
1045 tape->nr_stages--;
1046 if (tape->first_stage == NULL) {
1047 tape->last_stage = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048 if (tape->next_stage != NULL)
1049 printk(KERN_ERR "ide-tape: bug: tape->next_stage != NULL\n");
1050 if (tape->nr_stages)
1051 printk(KERN_ERR "ide-tape: bug: nr_stages should be 0 now\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052 }
1053}
1054
1055/*
1056 * This will free all the pipeline stages starting from new_last_stage->next
1057 * to the end of the list, and point tape->last_stage to new_last_stage.
1058 */
1059static void idetape_abort_pipeline(ide_drive_t *drive,
1060 idetape_stage_t *new_last_stage)
1061{
1062 idetape_tape_t *tape = drive->driver_data;
1063 idetape_stage_t *stage = new_last_stage->next;
1064 idetape_stage_t *nstage;
1065
1066#if IDETAPE_DEBUG_LOG
1067 if (tape->debug_level >= 4)
1068 printk(KERN_INFO "ide-tape: %s: idetape_abort_pipeline called\n", tape->name);
1069#endif
1070 while (stage) {
1071 nstage = stage->next;
1072 idetape_kfree_stage(tape, stage);
1073 --tape->nr_stages;
1074 --tape->nr_pending_stages;
1075 stage = nstage;
1076 }
1077 if (new_last_stage)
1078 new_last_stage->next = NULL;
1079 tape->last_stage = new_last_stage;
1080 tape->next_stage = NULL;
1081}
1082
1083/*
1084 * idetape_end_request is used to finish servicing a request, and to
1085 * insert a pending pipeline request into the main device queue.
1086 */
1087static int idetape_end_request(ide_drive_t *drive, int uptodate, int nr_sects)
1088{
1089 struct request *rq = HWGROUP(drive)->rq;
1090 idetape_tape_t *tape = drive->driver_data;
1091 unsigned long flags;
1092 int error;
1093 int remove_stage = 0;
1094 idetape_stage_t *active_stage;
1095
1096#if IDETAPE_DEBUG_LOG
1097 if (tape->debug_level >= 4)
1098 printk(KERN_INFO "ide-tape: Reached idetape_end_request\n");
1099#endif /* IDETAPE_DEBUG_LOG */
1100
1101 switch (uptodate) {
1102 case 0: error = IDETAPE_ERROR_GENERAL; break;
1103 case 1: error = 0; break;
1104 default: error = uptodate;
1105 }
1106 rq->errors = error;
1107 if (error)
1108 tape->failed_pc = NULL;
1109
Bartlomiej Zolnierkiewicz36872212008-01-26 20:13:10 +01001110 if (!blk_special_request(rq)) {
1111 ide_end_request(drive, uptodate, nr_sects);
1112 return 0;
1113 }
1114
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115 spin_lock_irqsave(&tape->spinlock, flags);
1116
1117 /* The request was a pipelined data transfer request */
1118 if (tape->active_data_request == rq) {
1119 active_stage = tape->active_stage;
1120 tape->active_stage = NULL;
1121 tape->active_data_request = NULL;
1122 tape->nr_pending_stages--;
1123 if (rq->cmd[0] & REQ_IDETAPE_WRITE) {
1124 remove_stage = 1;
1125 if (error) {
1126 set_bit(IDETAPE_PIPELINE_ERROR, &tape->flags);
1127 if (error == IDETAPE_ERROR_EOD)
1128 idetape_abort_pipeline(drive, active_stage);
1129 }
1130 } else if (rq->cmd[0] & REQ_IDETAPE_READ) {
1131 if (error == IDETAPE_ERROR_EOD) {
1132 set_bit(IDETAPE_PIPELINE_ERROR, &tape->flags);
1133 idetape_abort_pipeline(drive, active_stage);
1134 }
1135 }
1136 if (tape->next_stage != NULL) {
1137 idetape_active_next_stage(drive);
1138
1139 /*
1140 * Insert the next request into the request queue.
1141 */
1142 (void) ide_do_drive_cmd(drive, tape->active_data_request, ide_end);
1143 } else if (!error) {
1144 idetape_increase_max_pipeline_stages(drive);
1145 }
1146 }
1147 ide_end_drive_cmd(drive, 0, 0);
1148// blkdev_dequeue_request(rq);
1149// drive->rq = NULL;
1150// end_that_request_last(rq);
1151
1152 if (remove_stage)
1153 idetape_remove_stage_head(drive);
1154 if (tape->active_data_request == NULL)
1155 clear_bit(IDETAPE_PIPELINE_ACTIVE, &tape->flags);
1156 spin_unlock_irqrestore(&tape->spinlock, flags);
1157 return 0;
1158}
1159
1160static ide_startstop_t idetape_request_sense_callback (ide_drive_t *drive)
1161{
1162 idetape_tape_t *tape = drive->driver_data;
1163
1164#if IDETAPE_DEBUG_LOG
1165 if (tape->debug_level >= 4)
1166 printk(KERN_INFO "ide-tape: Reached idetape_request_sense_callback\n");
1167#endif /* IDETAPE_DEBUG_LOG */
1168 if (!tape->pc->error) {
Borislav Petkov1b5db432008-02-02 19:56:48 +01001169 idetape_analyze_error(drive, tape->pc->buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170 idetape_end_request(drive, 1, 0);
1171 } else {
1172 printk(KERN_ERR "ide-tape: Error in REQUEST SENSE itself - Aborting request!\n");
1173 idetape_end_request(drive, 0, 0);
1174 }
1175 return ide_stopped;
1176}
1177
1178static void idetape_create_request_sense_cmd (idetape_pc_t *pc)
1179{
1180 idetape_init_pc(pc);
1181 pc->c[0] = IDETAPE_REQUEST_SENSE_CMD;
1182 pc->c[4] = 20;
1183 pc->request_transfer = 20;
1184 pc->callback = &idetape_request_sense_callback;
1185}
1186
1187static void idetape_init_rq(struct request *rq, u8 cmd)
1188{
1189 memset(rq, 0, sizeof(*rq));
Jens Axboe4aff5e22006-08-10 08:44:47 +02001190 rq->cmd_type = REQ_TYPE_SPECIAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191 rq->cmd[0] = cmd;
1192}
1193
1194/*
1195 * idetape_queue_pc_head generates a new packet command request in front
1196 * of the request queue, before the current request, so that it will be
1197 * processed immediately, on the next pass through the driver.
1198 *
1199 * idetape_queue_pc_head is called from the request handling part of
1200 * the driver (the "bottom" part). Safe storage for the request should
1201 * be allocated with idetape_next_pc_storage and idetape_next_rq_storage
1202 * before calling idetape_queue_pc_head.
1203 *
1204 * Memory for those requests is pre-allocated at initialization time, and
1205 * is limited to IDETAPE_PC_STACK requests. We assume that we have enough
1206 * space for the maximum possible number of inter-dependent packet commands.
1207 *
1208 * The higher level of the driver - The ioctl handler and the character
1209 * device handling functions should queue request to the lower level part
1210 * and wait for their completion using idetape_queue_pc_tail or
1211 * idetape_queue_rw_tail.
1212 */
1213static void idetape_queue_pc_head (ide_drive_t *drive, idetape_pc_t *pc,struct request *rq)
1214{
1215 struct ide_tape_obj *tape = drive->driver_data;
1216
1217 idetape_init_rq(rq, REQ_IDETAPE_PC1);
1218 rq->buffer = (char *) pc;
1219 rq->rq_disk = tape->disk;
1220 (void) ide_do_drive_cmd(drive, rq, ide_preempt);
1221}
1222
1223/*
1224 * idetape_retry_pc is called when an error was detected during the
1225 * last packet command. We queue a request sense packet command in
1226 * the head of the request list.
1227 */
1228static ide_startstop_t idetape_retry_pc (ide_drive_t *drive)
1229{
1230 idetape_tape_t *tape = drive->driver_data;
1231 idetape_pc_t *pc;
1232 struct request *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233
Bartlomiej Zolnierkiewicz0e38a662008-01-25 22:17:12 +01001234 (void)drive->hwif->INB(IDE_ERROR_REG);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235 pc = idetape_next_pc_storage(drive);
1236 rq = idetape_next_rq_storage(drive);
1237 idetape_create_request_sense_cmd(pc);
1238 set_bit(IDETAPE_IGNORE_DSC, &tape->flags);
1239 idetape_queue_pc_head(drive, pc, rq);
1240 return ide_stopped;
1241}
1242
1243/*
1244 * idetape_postpone_request postpones the current request so that
1245 * ide.c will be able to service requests from another device on
1246 * the same hwgroup while we are polling for DSC.
1247 */
1248static void idetape_postpone_request (ide_drive_t *drive)
1249{
1250 idetape_tape_t *tape = drive->driver_data;
1251
1252#if IDETAPE_DEBUG_LOG
1253 if (tape->debug_level >= 4)
1254 printk(KERN_INFO "ide-tape: idetape_postpone_request\n");
1255#endif
1256 tape->postponed_rq = HWGROUP(drive)->rq;
1257 ide_stall_queue(drive, tape->dsc_polling_frequency);
1258}
1259
1260/*
1261 * idetape_pc_intr is the usual interrupt handler which will be called
1262 * during a packet command. We will transfer some of the data (as
1263 * requested by the drive) and will re-point interrupt handler to us.
1264 * When data transfer is finished, we will act according to the
1265 * algorithm described before idetape_issue_packet_command.
1266 *
1267 */
1268static ide_startstop_t idetape_pc_intr (ide_drive_t *drive)
1269{
1270 ide_hwif_t *hwif = drive->hwif;
1271 idetape_tape_t *tape = drive->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272 idetape_pc_t *pc = tape->pc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273 unsigned int temp;
1274#if SIMULATE_ERRORS
1275 static int error_sim_count = 0;
1276#endif
Bartlomiej Zolnierkiewicz790d1232008-01-25 22:17:12 +01001277 u16 bcount;
Bartlomiej Zolnierkiewicz8e7657a2008-01-25 22:17:12 +01001278 u8 stat, ireason;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279
1280#if IDETAPE_DEBUG_LOG
1281 if (tape->debug_level >= 4)
1282 printk(KERN_INFO "ide-tape: Reached idetape_pc_intr "
1283 "interrupt handler\n");
1284#endif /* IDETAPE_DEBUG_LOG */
1285
1286 /* Clear the interrupt */
Bartlomiej Zolnierkiewicz22c525b2008-01-25 22:17:11 +01001287 stat = hwif->INB(IDE_STATUS_REG);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288
1289 if (test_bit(PC_DMA_IN_PROGRESS, &pc->flags)) {
Bartlomiej Zolnierkiewicz22c525b2008-01-25 22:17:11 +01001290 if (hwif->ide_dma_end(drive) || (stat & ERR_STAT)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291 /*
1292 * A DMA error is sometimes expected. For example,
1293 * if the tape is crossing a filemark during a
1294 * READ command, it will issue an irq and position
1295 * itself before the filemark, so that only a partial
1296 * data transfer will occur (which causes the DMA
1297 * error). In that case, we will later ask the tape
1298 * how much bytes of the original request were
1299 * actually transferred (we can't receive that
1300 * information from the DMA engine on most chipsets).
1301 */
1302
1303 /*
1304 * On the contrary, a DMA error is never expected;
1305 * it usually indicates a hardware error or abort.
1306 * If the tape crosses a filemark during a READ
1307 * command, it will issue an irq and position itself
1308 * after the filemark (not before). Only a partial
1309 * data transfer will occur, but no DMA error.
1310 * (AS, 19 Apr 2001)
1311 */
1312 set_bit(PC_DMA_ERROR, &pc->flags);
1313 } else {
1314 pc->actually_transferred = pc->request_transfer;
1315 idetape_update_buffers(pc);
1316 }
1317#if IDETAPE_DEBUG_LOG
1318 if (tape->debug_level >= 4)
1319 printk(KERN_INFO "ide-tape: DMA finished\n");
1320#endif /* IDETAPE_DEBUG_LOG */
1321 }
1322
1323 /* No more interrupts */
Bartlomiej Zolnierkiewicz22c525b2008-01-25 22:17:11 +01001324 if ((stat & DRQ_STAT) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325#if IDETAPE_DEBUG_LOG
1326 if (tape->debug_level >= 2)
1327 printk(KERN_INFO "ide-tape: Packet command completed, %d bytes transferred\n", pc->actually_transferred);
1328#endif /* IDETAPE_DEBUG_LOG */
1329 clear_bit(PC_DMA_IN_PROGRESS, &pc->flags);
1330
1331 local_irq_enable();
1332
1333#if SIMULATE_ERRORS
1334 if ((pc->c[0] == IDETAPE_WRITE_CMD ||
1335 pc->c[0] == IDETAPE_READ_CMD) &&
1336 (++error_sim_count % 100) == 0) {
1337 printk(KERN_INFO "ide-tape: %s: simulating error\n",
1338 tape->name);
Bartlomiej Zolnierkiewicz22c525b2008-01-25 22:17:11 +01001339 stat |= ERR_STAT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340 }
1341#endif
Bartlomiej Zolnierkiewicz22c525b2008-01-25 22:17:11 +01001342 if ((stat & ERR_STAT) && pc->c[0] == IDETAPE_REQUEST_SENSE_CMD)
1343 stat &= ~ERR_STAT;
1344 if ((stat & ERR_STAT) || test_bit(PC_DMA_ERROR, &pc->flags)) {
1345 /* Error detected */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346#if IDETAPE_DEBUG_LOG
1347 if (tape->debug_level >= 1)
1348 printk(KERN_INFO "ide-tape: %s: I/O error\n",
1349 tape->name);
1350#endif /* IDETAPE_DEBUG_LOG */
1351 if (pc->c[0] == IDETAPE_REQUEST_SENSE_CMD) {
1352 printk(KERN_ERR "ide-tape: I/O error in request sense command\n");
1353 return ide_do_reset(drive);
1354 }
1355#if IDETAPE_DEBUG_LOG
1356 if (tape->debug_level >= 1)
1357 printk(KERN_INFO "ide-tape: [cmd %x]: check condition\n", pc->c[0]);
1358#endif
1359 /* Retry operation */
1360 return idetape_retry_pc(drive);
1361 }
1362 pc->error = 0;
1363 if (test_bit(PC_WAIT_FOR_DSC, &pc->flags) &&
Bartlomiej Zolnierkiewicz22c525b2008-01-25 22:17:11 +01001364 (stat & SEEK_STAT) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365 /* Media access command */
1366 tape->dsc_polling_start = jiffies;
1367 tape->dsc_polling_frequency = IDETAPE_DSC_MA_FAST;
1368 tape->dsc_timeout = jiffies + IDETAPE_DSC_MA_TIMEOUT;
1369 /* Allow ide.c to handle other requests */
1370 idetape_postpone_request(drive);
1371 return ide_stopped;
1372 }
1373 if (tape->failed_pc == pc)
1374 tape->failed_pc = NULL;
1375 /* Command finished - Call the callback function */
1376 return pc->callback(drive);
1377 }
1378 if (test_and_clear_bit(PC_DMA_IN_PROGRESS, &pc->flags)) {
1379 printk(KERN_ERR "ide-tape: The tape wants to issue more "
1380 "interrupts in DMA mode\n");
1381 printk(KERN_ERR "ide-tape: DMA disabled, reverting to PIO\n");
Bartlomiej Zolnierkiewicz7469aaf2007-02-17 02:40:26 +01001382 ide_dma_off(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383 return ide_do_reset(drive);
1384 }
1385 /* Get the number of bytes to transfer on this interrupt. */
Bartlomiej Zolnierkiewicz790d1232008-01-25 22:17:12 +01001386 bcount = (hwif->INB(IDE_BCOUNTH_REG) << 8) |
1387 hwif->INB(IDE_BCOUNTL_REG);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388
Bartlomiej Zolnierkiewicz8e7657a2008-01-25 22:17:12 +01001389 ireason = hwif->INB(IDE_IREASON_REG);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390
Bartlomiej Zolnierkiewicz8e7657a2008-01-25 22:17:12 +01001391 if (ireason & CD) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392 printk(KERN_ERR "ide-tape: CoD != 0 in idetape_pc_intr\n");
1393 return ide_do_reset(drive);
1394 }
Bartlomiej Zolnierkiewicz8e7657a2008-01-25 22:17:12 +01001395 if (((ireason & IO) == IO) == test_bit(PC_WRITING, &pc->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396 /* Hopefully, we will never get here */
1397 printk(KERN_ERR "ide-tape: We wanted to %s, ",
Bartlomiej Zolnierkiewicz8e7657a2008-01-25 22:17:12 +01001398 (ireason & IO) ? "Write" : "Read");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399 printk(KERN_ERR "ide-tape: but the tape wants us to %s !\n",
Bartlomiej Zolnierkiewicz8e7657a2008-01-25 22:17:12 +01001400 (ireason & IO) ? "Read" : "Write");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401 return ide_do_reset(drive);
1402 }
1403 if (!test_bit(PC_WRITING, &pc->flags)) {
1404 /* Reading - Check that we have enough space */
Bartlomiej Zolnierkiewicz790d1232008-01-25 22:17:12 +01001405 temp = pc->actually_transferred + bcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406 if (temp > pc->request_transfer) {
1407 if (temp > pc->buffer_size) {
1408 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 +01001409 idetape_discard_data(drive, bcount);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410 ide_set_handler(drive, &idetape_pc_intr, IDETAPE_WAIT_CMD, NULL);
1411 return ide_started;
1412 }
1413#if IDETAPE_DEBUG_LOG
1414 if (tape->debug_level >= 2)
1415 printk(KERN_NOTICE "ide-tape: The tape wants to send us more data than expected - allowing transfer\n");
1416#endif /* IDETAPE_DEBUG_LOG */
1417 }
1418 }
1419 if (test_bit(PC_WRITING, &pc->flags)) {
1420 if (pc->bh != NULL)
Bartlomiej Zolnierkiewicz790d1232008-01-25 22:17:12 +01001421 idetape_output_buffers(drive, pc, bcount);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422 else
1423 /* Write the current buffer */
Bartlomiej Zolnierkiewicz790d1232008-01-25 22:17:12 +01001424 hwif->atapi_output_bytes(drive, pc->current_position,
1425 bcount);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426 } else {
1427 if (pc->bh != NULL)
Bartlomiej Zolnierkiewicz790d1232008-01-25 22:17:12 +01001428 idetape_input_buffers(drive, pc, bcount);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429 else
1430 /* Read the current buffer */
Bartlomiej Zolnierkiewicz790d1232008-01-25 22:17:12 +01001431 hwif->atapi_input_bytes(drive, pc->current_position,
1432 bcount);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433 }
1434 /* Update the current position */
Bartlomiej Zolnierkiewicz790d1232008-01-25 22:17:12 +01001435 pc->actually_transferred += bcount;
1436 pc->current_position += bcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437#if IDETAPE_DEBUG_LOG
1438 if (tape->debug_level >= 2)
Bartlomiej Zolnierkiewicz790d1232008-01-25 22:17:12 +01001439 printk(KERN_INFO "ide-tape: [cmd %x] transferred %d bytes "
1440 "on that interrupt\n", pc->c[0], bcount);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441#endif
1442 /* And set the interrupt handler again */
1443 ide_set_handler(drive, &idetape_pc_intr, IDETAPE_WAIT_CMD, NULL);
1444 return ide_started;
1445}
1446
1447/*
1448 * Packet Command Interface
1449 *
1450 * The current Packet Command is available in tape->pc, and will not
1451 * change until we finish handling it. Each packet command is associated
1452 * with a callback function that will be called when the command is
1453 * finished.
1454 *
1455 * The handling will be done in three stages:
1456 *
1457 * 1. idetape_issue_packet_command will send the packet command to the
1458 * drive, and will set the interrupt handler to idetape_pc_intr.
1459 *
1460 * 2. On each interrupt, idetape_pc_intr will be called. This step
1461 * will be repeated until the device signals us that no more
1462 * interrupts will be issued.
1463 *
1464 * 3. ATAPI Tape media access commands have immediate status with a
1465 * delayed process. In case of a successful initiation of a
1466 * media access packet command, the DSC bit will be set when the
1467 * actual execution of the command is finished.
1468 * Since the tape drive will not issue an interrupt, we have to
1469 * poll for this event. In this case, we define the request as
1470 * "low priority request" by setting rq_status to
1471 * IDETAPE_RQ_POSTPONED, set a timer to poll for DSC and exit
1472 * the driver.
1473 *
1474 * ide.c will then give higher priority to requests which
1475 * originate from the other device, until will change rq_status
1476 * to RQ_ACTIVE.
1477 *
1478 * 4. When the packet command is finished, it will be checked for errors.
1479 *
1480 * 5. In case an error was found, we queue a request sense packet
1481 * command in front of the request queue and retry the operation
1482 * up to IDETAPE_MAX_PC_RETRIES times.
1483 *
1484 * 6. In case no error was found, or we decided to give up and not
1485 * to retry again, the callback function will be called and then
1486 * we will handle the next request.
1487 *
1488 */
1489static ide_startstop_t idetape_transfer_pc(ide_drive_t *drive)
1490{
1491 ide_hwif_t *hwif = drive->hwif;
1492 idetape_tape_t *tape = drive->driver_data;
1493 idetape_pc_t *pc = tape->pc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494 int retries = 100;
1495 ide_startstop_t startstop;
Bartlomiej Zolnierkiewicz8e7657a2008-01-25 22:17:12 +01001496 u8 ireason;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497
1498 if (ide_wait_stat(&startstop,drive,DRQ_STAT,BUSY_STAT,WAIT_READY)) {
1499 printk(KERN_ERR "ide-tape: Strange, packet command initiated yet DRQ isn't asserted\n");
1500 return startstop;
1501 }
Bartlomiej Zolnierkiewicz8e7657a2008-01-25 22:17:12 +01001502 ireason = hwif->INB(IDE_IREASON_REG);
1503 while (retries-- && ((ireason & CD) == 0 || (ireason & IO))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504 printk(KERN_ERR "ide-tape: (IO,CoD != (0,1) while issuing "
1505 "a packet command, retrying\n");
1506 udelay(100);
Bartlomiej Zolnierkiewicz8e7657a2008-01-25 22:17:12 +01001507 ireason = hwif->INB(IDE_IREASON_REG);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001508 if (retries == 0) {
1509 printk(KERN_ERR "ide-tape: (IO,CoD != (0,1) while "
1510 "issuing a packet command, ignoring\n");
Bartlomiej Zolnierkiewicz8e7657a2008-01-25 22:17:12 +01001511 ireason |= CD;
1512 ireason &= ~IO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513 }
1514 }
Bartlomiej Zolnierkiewicz8e7657a2008-01-25 22:17:12 +01001515 if ((ireason & CD) == 0 || (ireason & IO)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001516 printk(KERN_ERR "ide-tape: (IO,CoD) != (0,1) while issuing "
1517 "a packet command\n");
1518 return ide_do_reset(drive);
1519 }
1520 /* Set the interrupt routine */
1521 ide_set_handler(drive, &idetape_pc_intr, IDETAPE_WAIT_CMD, NULL);
1522#ifdef CONFIG_BLK_DEV_IDEDMA
1523 /* Begin DMA, if necessary */
1524 if (test_bit(PC_DMA_IN_PROGRESS, &pc->flags))
1525 hwif->dma_start(drive);
1526#endif
1527 /* Send the actual packet */
1528 HWIF(drive)->atapi_output_bytes(drive, pc->c, 12);
1529 return ide_started;
1530}
1531
1532static ide_startstop_t idetape_issue_packet_command (ide_drive_t *drive, idetape_pc_t *pc)
1533{
1534 ide_hwif_t *hwif = drive->hwif;
1535 idetape_tape_t *tape = drive->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536 int dma_ok = 0;
Bartlomiej Zolnierkiewicz790d1232008-01-25 22:17:12 +01001537 u16 bcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001538
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539 if (tape->pc->c[0] == IDETAPE_REQUEST_SENSE_CMD &&
1540 pc->c[0] == IDETAPE_REQUEST_SENSE_CMD) {
1541 printk(KERN_ERR "ide-tape: possible ide-tape.c bug - "
1542 "Two request sense in serial were issued\n");
1543 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544
1545 if (tape->failed_pc == NULL && pc->c[0] != IDETAPE_REQUEST_SENSE_CMD)
1546 tape->failed_pc = pc;
1547 /* Set the current packet command */
1548 tape->pc = pc;
1549
1550 if (pc->retries > IDETAPE_MAX_PC_RETRIES ||
1551 test_bit(PC_ABORT, &pc->flags)) {
1552 /*
1553 * We will "abort" retrying a packet command in case
1554 * a legitimate error code was received (crossing a
1555 * filemark, or end of the media, for example).
1556 */
1557 if (!test_bit(PC_ABORT, &pc->flags)) {
1558 if (!(pc->c[0] == IDETAPE_TEST_UNIT_READY_CMD &&
1559 tape->sense_key == 2 && tape->asc == 4 &&
1560 (tape->ascq == 1 || tape->ascq == 8))) {
1561 printk(KERN_ERR "ide-tape: %s: I/O error, "
1562 "pc = %2x, key = %2x, "
1563 "asc = %2x, ascq = %2x\n",
1564 tape->name, pc->c[0],
1565 tape->sense_key, tape->asc,
1566 tape->ascq);
1567 }
1568 /* Giving up */
1569 pc->error = IDETAPE_ERROR_GENERAL;
1570 }
1571 tape->failed_pc = NULL;
1572 return pc->callback(drive);
1573 }
1574#if IDETAPE_DEBUG_LOG
1575 if (tape->debug_level >= 2)
1576 printk(KERN_INFO "ide-tape: Retry number - %d, cmd = %02X\n", pc->retries, pc->c[0]);
1577#endif /* IDETAPE_DEBUG_LOG */
1578
1579 pc->retries++;
1580 /* We haven't transferred any data yet */
1581 pc->actually_transferred = 0;
1582 pc->current_position = pc->buffer;
1583 /* Request to transfer the entire buffer at once */
Bartlomiej Zolnierkiewicz790d1232008-01-25 22:17:12 +01001584 bcount = pc->request_transfer;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585
1586 if (test_and_clear_bit(PC_DMA_ERROR, &pc->flags)) {
1587 printk(KERN_WARNING "ide-tape: DMA disabled, "
1588 "reverting to PIO\n");
Bartlomiej Zolnierkiewicz7469aaf2007-02-17 02:40:26 +01001589 ide_dma_off(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590 }
1591 if (test_bit(PC_DMA_RECOMMENDED, &pc->flags) && drive->using_dma)
1592 dma_ok = !hwif->dma_setup(drive);
1593
Bartlomiej Zolnierkiewicz2fc57382008-01-25 22:17:13 +01001594 ide_pktcmd_tf_load(drive, IDE_TFLAG_NO_SELECT_MASK |
1595 IDE_TFLAG_OUT_DEVICE, bcount, dma_ok);
1596
Linus Torvalds1da177e2005-04-16 15:20:36 -07001597 if (dma_ok) /* Will begin DMA later */
1598 set_bit(PC_DMA_IN_PROGRESS, &pc->flags);
1599 if (test_bit(IDETAPE_DRQ_INTERRUPT, &tape->flags)) {
Bartlomiej Zolnierkiewiczc1c9dbc2008-02-02 19:56:44 +01001600 ide_execute_command(drive, WIN_PACKETCMD, &idetape_transfer_pc,
1601 IDETAPE_WAIT_CMD, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001602 return ide_started;
1603 } else {
1604 hwif->OUTB(WIN_PACKETCMD, IDE_COMMAND_REG);
1605 return idetape_transfer_pc(drive);
1606 }
1607}
1608
1609/*
1610 * General packet command callback function.
1611 */
1612static ide_startstop_t idetape_pc_callback (ide_drive_t *drive)
1613{
1614 idetape_tape_t *tape = drive->driver_data;
1615
1616#if IDETAPE_DEBUG_LOG
1617 if (tape->debug_level >= 4)
1618 printk(KERN_INFO "ide-tape: Reached idetape_pc_callback\n");
1619#endif /* IDETAPE_DEBUG_LOG */
1620
1621 idetape_end_request(drive, tape->pc->error ? 0 : 1, 0);
1622 return ide_stopped;
1623}
1624
1625/*
1626 * A mode sense command is used to "sense" tape parameters.
1627 */
1628static void idetape_create_mode_sense_cmd (idetape_pc_t *pc, u8 page_code)
1629{
1630 idetape_init_pc(pc);
1631 pc->c[0] = IDETAPE_MODE_SENSE_CMD;
1632 if (page_code != IDETAPE_BLOCK_DESCRIPTOR)
1633 pc->c[1] = 8; /* DBD = 1 - Don't return block descriptors */
1634 pc->c[2] = page_code;
1635 /*
1636 * Changed pc->c[3] to 0 (255 will at best return unused info).
1637 *
1638 * For SCSI this byte is defined as subpage instead of high byte
1639 * of length and some IDE drives seem to interpret it this way
1640 * and return an error when 255 is used.
1641 */
1642 pc->c[3] = 0;
1643 pc->c[4] = 255; /* (We will just discard data in that case) */
1644 if (page_code == IDETAPE_BLOCK_DESCRIPTOR)
1645 pc->request_transfer = 12;
1646 else if (page_code == IDETAPE_CAPABILITIES_PAGE)
1647 pc->request_transfer = 24;
1648 else
1649 pc->request_transfer = 50;
1650 pc->callback = &idetape_pc_callback;
1651}
1652
1653static void calculate_speeds(ide_drive_t *drive)
1654{
1655 idetape_tape_t *tape = drive->driver_data;
1656 int full = 125, empty = 75;
1657
1658 if (time_after(jiffies, tape->controlled_pipeline_head_time + 120 * HZ)) {
1659 tape->controlled_previous_pipeline_head = tape->controlled_last_pipeline_head;
1660 tape->controlled_previous_head_time = tape->controlled_pipeline_head_time;
1661 tape->controlled_last_pipeline_head = tape->pipeline_head;
1662 tape->controlled_pipeline_head_time = jiffies;
1663 }
1664 if (time_after(jiffies, tape->controlled_pipeline_head_time + 60 * HZ))
1665 tape->controlled_pipeline_head_speed = (tape->pipeline_head - tape->controlled_last_pipeline_head) * 32 * HZ / (jiffies - tape->controlled_pipeline_head_time);
1666 else if (time_after(jiffies, tape->controlled_previous_head_time))
1667 tape->controlled_pipeline_head_speed = (tape->pipeline_head - tape->controlled_previous_pipeline_head) * 32 * HZ / (jiffies - tape->controlled_previous_head_time);
1668
1669 if (tape->nr_pending_stages < tape->max_stages /*- 1 */) {
1670 /* -1 for read mode error recovery */
1671 if (time_after(jiffies, tape->uncontrolled_previous_head_time + 10 * HZ)) {
1672 tape->uncontrolled_pipeline_head_time = jiffies;
1673 tape->uncontrolled_pipeline_head_speed = (tape->pipeline_head - tape->uncontrolled_previous_pipeline_head) * 32 * HZ / (jiffies - tape->uncontrolled_previous_head_time);
1674 }
1675 } else {
1676 tape->uncontrolled_previous_head_time = jiffies;
1677 tape->uncontrolled_previous_pipeline_head = tape->pipeline_head;
1678 if (time_after(jiffies, tape->uncontrolled_pipeline_head_time + 30 * HZ)) {
1679 tape->uncontrolled_pipeline_head_time = jiffies;
1680 }
1681 }
1682 tape->pipeline_head_speed = max(tape->uncontrolled_pipeline_head_speed, tape->controlled_pipeline_head_speed);
1683 if (tape->speed_control == 0) {
1684 tape->max_insert_speed = 5000;
1685 } else if (tape->speed_control == 1) {
1686 if (tape->nr_pending_stages >= tape->max_stages / 2)
1687 tape->max_insert_speed = tape->pipeline_head_speed +
1688 (1100 - tape->pipeline_head_speed) * 2 * (tape->nr_pending_stages - tape->max_stages / 2) / tape->max_stages;
1689 else
1690 tape->max_insert_speed = 500 +
1691 (tape->pipeline_head_speed - 500) * 2 * tape->nr_pending_stages / tape->max_stages;
1692 if (tape->nr_pending_stages >= tape->max_stages * 99 / 100)
1693 tape->max_insert_speed = 5000;
1694 } else if (tape->speed_control == 2) {
1695 tape->max_insert_speed = tape->pipeline_head_speed * empty / 100 +
1696 (tape->pipeline_head_speed * full / 100 - tape->pipeline_head_speed * empty / 100) * tape->nr_pending_stages / tape->max_stages;
1697 } else
1698 tape->max_insert_speed = tape->speed_control;
1699 tape->max_insert_speed = max(tape->max_insert_speed, 500);
1700}
1701
1702static ide_startstop_t idetape_media_access_finished (ide_drive_t *drive)
1703{
1704 idetape_tape_t *tape = drive->driver_data;
1705 idetape_pc_t *pc = tape->pc;
Bartlomiej Zolnierkiewicz22c525b2008-01-25 22:17:11 +01001706 u8 stat;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001707
Bartlomiej Zolnierkiewicz22c525b2008-01-25 22:17:11 +01001708 stat = drive->hwif->INB(IDE_STATUS_REG);
1709 if (stat & SEEK_STAT) {
1710 if (stat & ERR_STAT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001711 /* Error detected */
1712 if (pc->c[0] != IDETAPE_TEST_UNIT_READY_CMD)
1713 printk(KERN_ERR "ide-tape: %s: I/O error, ",
1714 tape->name);
1715 /* Retry operation */
1716 return idetape_retry_pc(drive);
1717 }
1718 pc->error = 0;
1719 if (tape->failed_pc == pc)
1720 tape->failed_pc = NULL;
1721 } else {
1722 pc->error = IDETAPE_ERROR_GENERAL;
1723 tape->failed_pc = NULL;
1724 }
1725 return pc->callback(drive);
1726}
1727
1728static ide_startstop_t idetape_rw_callback (ide_drive_t *drive)
1729{
1730 idetape_tape_t *tape = drive->driver_data;
1731 struct request *rq = HWGROUP(drive)->rq;
1732 int blocks = tape->pc->actually_transferred / tape->tape_block_size;
1733
1734 tape->avg_size += blocks * tape->tape_block_size;
1735 tape->insert_size += blocks * tape->tape_block_size;
1736 if (tape->insert_size > 1024 * 1024)
1737 tape->measure_insert_time = 1;
1738 if (tape->measure_insert_time) {
1739 tape->measure_insert_time = 0;
1740 tape->insert_time = jiffies;
1741 tape->insert_size = 0;
1742 }
1743 if (time_after(jiffies, tape->insert_time))
1744 tape->insert_speed = tape->insert_size / 1024 * HZ / (jiffies - tape->insert_time);
Marcelo Feitoza Parisi9bae1ff2006-03-28 01:56:46 -08001745 if (time_after_eq(jiffies, tape->avg_time + HZ)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001746 tape->avg_speed = tape->avg_size * HZ / (jiffies - tape->avg_time) / 1024;
1747 tape->avg_size = 0;
1748 tape->avg_time = jiffies;
1749 }
1750
1751#if IDETAPE_DEBUG_LOG
1752 if (tape->debug_level >= 4)
1753 printk(KERN_INFO "ide-tape: Reached idetape_rw_callback\n");
1754#endif /* IDETAPE_DEBUG_LOG */
1755
1756 tape->first_frame_position += blocks;
1757 rq->current_nr_sectors -= blocks;
1758
1759 if (!tape->pc->error)
1760 idetape_end_request(drive, 1, 0);
1761 else
1762 idetape_end_request(drive, tape->pc->error, 0);
1763 return ide_stopped;
1764}
1765
1766static void idetape_create_read_cmd(idetape_tape_t *tape, idetape_pc_t *pc, unsigned int length, struct idetape_bh *bh)
1767{
1768 idetape_init_pc(pc);
1769 pc->c[0] = IDETAPE_READ_CMD;
1770 put_unaligned(htonl(length), (unsigned int *) &pc->c[1]);
1771 pc->c[1] = 1;
1772 pc->callback = &idetape_rw_callback;
1773 pc->bh = bh;
1774 atomic_set(&bh->b_count, 0);
1775 pc->buffer = NULL;
1776 pc->request_transfer = pc->buffer_size = length * tape->tape_block_size;
1777 if (pc->request_transfer == tape->stage_size)
1778 set_bit(PC_DMA_RECOMMENDED, &pc->flags);
1779}
1780
1781static void idetape_create_read_buffer_cmd(idetape_tape_t *tape, idetape_pc_t *pc, unsigned int length, struct idetape_bh *bh)
1782{
1783 int size = 32768;
1784 struct idetape_bh *p = bh;
1785
1786 idetape_init_pc(pc);
1787 pc->c[0] = IDETAPE_READ_BUFFER_CMD;
1788 pc->c[1] = IDETAPE_RETRIEVE_FAULTY_BLOCK;
1789 pc->c[7] = size >> 8;
1790 pc->c[8] = size & 0xff;
1791 pc->callback = &idetape_pc_callback;
1792 pc->bh = bh;
1793 atomic_set(&bh->b_count, 0);
1794 pc->buffer = NULL;
1795 while (p) {
1796 atomic_set(&p->b_count, 0);
1797 p = p->b_reqnext;
1798 }
1799 pc->request_transfer = pc->buffer_size = size;
1800}
1801
1802static void idetape_create_write_cmd(idetape_tape_t *tape, idetape_pc_t *pc, unsigned int length, struct idetape_bh *bh)
1803{
1804 idetape_init_pc(pc);
1805 pc->c[0] = IDETAPE_WRITE_CMD;
1806 put_unaligned(htonl(length), (unsigned int *) &pc->c[1]);
1807 pc->c[1] = 1;
1808 pc->callback = &idetape_rw_callback;
1809 set_bit(PC_WRITING, &pc->flags);
1810 pc->bh = bh;
1811 pc->b_data = bh->b_data;
1812 pc->b_count = atomic_read(&bh->b_count);
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
1819/*
1820 * idetape_do_request is our request handling function.
1821 */
1822static ide_startstop_t idetape_do_request(ide_drive_t *drive,
1823 struct request *rq, sector_t block)
1824{
1825 idetape_tape_t *tape = drive->driver_data;
1826 idetape_pc_t *pc = NULL;
1827 struct request *postponed_rq = tape->postponed_rq;
Bartlomiej Zolnierkiewicz22c525b2008-01-25 22:17:11 +01001828 u8 stat;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001829
1830#if IDETAPE_DEBUG_LOG
Linus Torvalds1da177e2005-04-16 15:20:36 -07001831 if (tape->debug_level >= 2)
1832 printk(KERN_INFO "ide-tape: sector: %ld, "
1833 "nr_sectors: %ld, current_nr_sectors: %d\n",
1834 rq->sector, rq->nr_sectors, rq->current_nr_sectors);
1835#endif /* IDETAPE_DEBUG_LOG */
1836
Jens Axboe4aff5e22006-08-10 08:44:47 +02001837 if (!blk_special_request(rq)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001838 /*
1839 * We do not support buffer cache originated requests.
1840 */
1841 printk(KERN_NOTICE "ide-tape: %s: Unsupported request in "
Jens Axboe4aff5e22006-08-10 08:44:47 +02001842 "request queue (%d)\n", drive->name, rq->cmd_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001843 ide_end_request(drive, 0, 0);
1844 return ide_stopped;
1845 }
1846
1847 /*
1848 * Retry a failed packet command
1849 */
1850 if (tape->failed_pc != NULL &&
1851 tape->pc->c[0] == IDETAPE_REQUEST_SENSE_CMD) {
1852 return idetape_issue_packet_command(drive, tape->failed_pc);
1853 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001854 if (postponed_rq != NULL)
1855 if (rq != postponed_rq) {
1856 printk(KERN_ERR "ide-tape: ide-tape.c bug - "
1857 "Two DSC requests were queued\n");
1858 idetape_end_request(drive, 0, 0);
1859 return ide_stopped;
1860 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001861
1862 tape->postponed_rq = NULL;
1863
1864 /*
1865 * If the tape is still busy, postpone our request and service
1866 * the other device meanwhile.
1867 */
Bartlomiej Zolnierkiewicz22c525b2008-01-25 22:17:11 +01001868 stat = drive->hwif->INB(IDE_STATUS_REG);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001869
1870 if (!drive->dsc_overlap && !(rq->cmd[0] & REQ_IDETAPE_PC2))
1871 set_bit(IDETAPE_IGNORE_DSC, &tape->flags);
1872
1873 if (drive->post_reset == 1) {
1874 set_bit(IDETAPE_IGNORE_DSC, &tape->flags);
1875 drive->post_reset = 0;
1876 }
1877
1878 if (tape->tape_still_time > 100 && tape->tape_still_time < 200)
1879 tape->measure_insert_time = 1;
1880 if (time_after(jiffies, tape->insert_time))
1881 tape->insert_speed = tape->insert_size / 1024 * HZ / (jiffies - tape->insert_time);
1882 calculate_speeds(drive);
1883 if (!test_and_clear_bit(IDETAPE_IGNORE_DSC, &tape->flags) &&
Bartlomiej Zolnierkiewicz22c525b2008-01-25 22:17:11 +01001884 (stat & SEEK_STAT) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001885 if (postponed_rq == NULL) {
1886 tape->dsc_polling_start = jiffies;
1887 tape->dsc_polling_frequency = tape->best_dsc_rw_frequency;
1888 tape->dsc_timeout = jiffies + IDETAPE_DSC_RW_TIMEOUT;
1889 } else if (time_after(jiffies, tape->dsc_timeout)) {
1890 printk(KERN_ERR "ide-tape: %s: DSC timeout\n",
1891 tape->name);
1892 if (rq->cmd[0] & REQ_IDETAPE_PC2) {
1893 idetape_media_access_finished(drive);
1894 return ide_stopped;
1895 } else {
1896 return ide_do_reset(drive);
1897 }
Marcelo Feitoza Parisi9bae1ff2006-03-28 01:56:46 -08001898 } else if (time_after(jiffies, tape->dsc_polling_start + IDETAPE_DSC_MA_THRESHOLD))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001899 tape->dsc_polling_frequency = IDETAPE_DSC_MA_SLOW;
1900 idetape_postpone_request(drive);
1901 return ide_stopped;
1902 }
1903 if (rq->cmd[0] & REQ_IDETAPE_READ) {
1904 tape->buffer_head++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001905 tape->postpone_cnt = 0;
1906 pc = idetape_next_pc_storage(drive);
1907 idetape_create_read_cmd(tape, pc, rq->current_nr_sectors, (struct idetape_bh *)rq->special);
1908 goto out;
1909 }
1910 if (rq->cmd[0] & REQ_IDETAPE_WRITE) {
1911 tape->buffer_head++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001912 tape->postpone_cnt = 0;
1913 pc = idetape_next_pc_storage(drive);
1914 idetape_create_write_cmd(tape, pc, rq->current_nr_sectors, (struct idetape_bh *)rq->special);
1915 goto out;
1916 }
1917 if (rq->cmd[0] & REQ_IDETAPE_READ_BUFFER) {
1918 tape->postpone_cnt = 0;
1919 pc = idetape_next_pc_storage(drive);
1920 idetape_create_read_buffer_cmd(tape, pc, rq->current_nr_sectors, (struct idetape_bh *)rq->special);
1921 goto out;
1922 }
1923 if (rq->cmd[0] & REQ_IDETAPE_PC1) {
1924 pc = (idetape_pc_t *) rq->buffer;
1925 rq->cmd[0] &= ~(REQ_IDETAPE_PC1);
1926 rq->cmd[0] |= REQ_IDETAPE_PC2;
1927 goto out;
1928 }
1929 if (rq->cmd[0] & REQ_IDETAPE_PC2) {
1930 idetape_media_access_finished(drive);
1931 return ide_stopped;
1932 }
1933 BUG();
1934out:
1935 return idetape_issue_packet_command(drive, pc);
1936}
1937
1938/*
1939 * Pipeline related functions
1940 */
1941static inline int idetape_pipeline_active (idetape_tape_t *tape)
1942{
1943 int rc1, rc2;
1944
1945 rc1 = test_bit(IDETAPE_PIPELINE_ACTIVE, &tape->flags);
1946 rc2 = (tape->active_data_request != NULL);
1947 return rc1;
1948}
1949
1950/*
1951 * idetape_kmalloc_stage uses __get_free_page to allocate a pipeline
1952 * stage, along with all the necessary small buffers which together make
1953 * a buffer of size tape->stage_size (or a bit more). We attempt to
1954 * combine sequential pages as much as possible.
1955 *
1956 * Returns a pointer to the new allocated stage, or NULL if we
1957 * can't (or don't want to) allocate a stage.
1958 *
1959 * Pipeline stages are optional and are used to increase performance.
1960 * If we can't allocate them, we'll manage without them.
1961 */
1962static idetape_stage_t *__idetape_kmalloc_stage (idetape_tape_t *tape, int full, int clear)
1963{
1964 idetape_stage_t *stage;
1965 struct idetape_bh *prev_bh, *bh;
1966 int pages = tape->pages_per_stage;
1967 char *b_data = NULL;
1968
Robert P. J. Day5cbded52006-12-13 00:35:56 -08001969 if ((stage = kmalloc(sizeof (idetape_stage_t),GFP_KERNEL)) == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001970 return NULL;
1971 stage->next = NULL;
1972
Robert P. J. Day5cbded52006-12-13 00:35:56 -08001973 bh = stage->bh = kmalloc(sizeof(struct idetape_bh), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001974 if (bh == NULL)
1975 goto abort;
1976 bh->b_reqnext = NULL;
1977 if ((bh->b_data = (char *) __get_free_page (GFP_KERNEL)) == NULL)
1978 goto abort;
1979 if (clear)
1980 memset(bh->b_data, 0, PAGE_SIZE);
1981 bh->b_size = PAGE_SIZE;
1982 atomic_set(&bh->b_count, full ? bh->b_size : 0);
1983
1984 while (--pages) {
1985 if ((b_data = (char *) __get_free_page (GFP_KERNEL)) == NULL)
1986 goto abort;
1987 if (clear)
1988 memset(b_data, 0, PAGE_SIZE);
1989 if (bh->b_data == b_data + PAGE_SIZE) {
1990 bh->b_size += PAGE_SIZE;
1991 bh->b_data -= PAGE_SIZE;
1992 if (full)
1993 atomic_add(PAGE_SIZE, &bh->b_count);
1994 continue;
1995 }
1996 if (b_data == bh->b_data + bh->b_size) {
1997 bh->b_size += PAGE_SIZE;
1998 if (full)
1999 atomic_add(PAGE_SIZE, &bh->b_count);
2000 continue;
2001 }
2002 prev_bh = bh;
Robert P. J. Day5cbded52006-12-13 00:35:56 -08002003 if ((bh = kmalloc(sizeof(struct idetape_bh), GFP_KERNEL)) == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002004 free_page((unsigned long) b_data);
2005 goto abort;
2006 }
2007 bh->b_reqnext = NULL;
2008 bh->b_data = b_data;
2009 bh->b_size = PAGE_SIZE;
2010 atomic_set(&bh->b_count, full ? bh->b_size : 0);
2011 prev_bh->b_reqnext = bh;
2012 }
2013 bh->b_size -= tape->excess_bh_size;
2014 if (full)
2015 atomic_sub(tape->excess_bh_size, &bh->b_count);
2016 return stage;
2017abort:
2018 __idetape_kfree_stage(stage);
2019 return NULL;
2020}
2021
2022static idetape_stage_t *idetape_kmalloc_stage (idetape_tape_t *tape)
2023{
2024 idetape_stage_t *cache_stage = tape->cache_stage;
2025
2026#if IDETAPE_DEBUG_LOG
2027 if (tape->debug_level >= 4)
2028 printk(KERN_INFO "ide-tape: Reached idetape_kmalloc_stage\n");
2029#endif /* IDETAPE_DEBUG_LOG */
2030
2031 if (tape->nr_stages >= tape->max_stages)
2032 return NULL;
2033 if (cache_stage != NULL) {
2034 tape->cache_stage = NULL;
2035 return cache_stage;
2036 }
2037 return __idetape_kmalloc_stage(tape, 0, 0);
2038}
2039
Daniel Walkerdcd96372006-06-25 05:47:37 -07002040static 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 -07002041{
2042 struct idetape_bh *bh = tape->bh;
2043 int count;
Daniel Walkerdcd96372006-06-25 05:47:37 -07002044 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002045
2046 while (n) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002047 if (bh == NULL) {
2048 printk(KERN_ERR "ide-tape: bh == NULL in "
2049 "idetape_copy_stage_from_user\n");
Daniel Walkerdcd96372006-06-25 05:47:37 -07002050 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002051 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002052 count = min((unsigned int)(bh->b_size - atomic_read(&bh->b_count)), (unsigned int)n);
Daniel Walkerdcd96372006-06-25 05:47:37 -07002053 if (copy_from_user(bh->b_data + atomic_read(&bh->b_count), buf, count))
2054 ret = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002055 n -= count;
2056 atomic_add(count, &bh->b_count);
2057 buf += count;
2058 if (atomic_read(&bh->b_count) == bh->b_size) {
2059 bh = bh->b_reqnext;
2060 if (bh)
2061 atomic_set(&bh->b_count, 0);
2062 }
2063 }
2064 tape->bh = bh;
Daniel Walkerdcd96372006-06-25 05:47:37 -07002065 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002066}
2067
Daniel Walkerdcd96372006-06-25 05:47:37 -07002068static 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 -07002069{
2070 struct idetape_bh *bh = tape->bh;
2071 int count;
Daniel Walkerdcd96372006-06-25 05:47:37 -07002072 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002073
2074 while (n) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002075 if (bh == NULL) {
2076 printk(KERN_ERR "ide-tape: bh == NULL in "
2077 "idetape_copy_stage_to_user\n");
Daniel Walkerdcd96372006-06-25 05:47:37 -07002078 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002079 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002080 count = min(tape->b_count, n);
Daniel Walkerdcd96372006-06-25 05:47:37 -07002081 if (copy_to_user(buf, tape->b_data, count))
2082 ret = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002083 n -= count;
2084 tape->b_data += count;
2085 tape->b_count -= count;
2086 buf += count;
2087 if (!tape->b_count) {
2088 tape->bh = bh = bh->b_reqnext;
2089 if (bh) {
2090 tape->b_data = bh->b_data;
2091 tape->b_count = atomic_read(&bh->b_count);
2092 }
2093 }
2094 }
Daniel Walkerdcd96372006-06-25 05:47:37 -07002095 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002096}
2097
2098static void idetape_init_merge_stage (idetape_tape_t *tape)
2099{
2100 struct idetape_bh *bh = tape->merge_stage->bh;
2101
2102 tape->bh = bh;
2103 if (tape->chrdev_direction == idetape_direction_write)
2104 atomic_set(&bh->b_count, 0);
2105 else {
2106 tape->b_data = bh->b_data;
2107 tape->b_count = atomic_read(&bh->b_count);
2108 }
2109}
2110
2111static void idetape_switch_buffers (idetape_tape_t *tape, idetape_stage_t *stage)
2112{
2113 struct idetape_bh *tmp;
2114
2115 tmp = stage->bh;
2116 stage->bh = tape->merge_stage->bh;
2117 tape->merge_stage->bh = tmp;
2118 idetape_init_merge_stage(tape);
2119}
2120
2121/*
2122 * idetape_add_stage_tail adds a new stage at the end of the pipeline.
2123 */
2124static void idetape_add_stage_tail (ide_drive_t *drive,idetape_stage_t *stage)
2125{
2126 idetape_tape_t *tape = drive->driver_data;
2127 unsigned long flags;
2128
2129#if IDETAPE_DEBUG_LOG
2130 if (tape->debug_level >= 4)
2131 printk (KERN_INFO "ide-tape: Reached idetape_add_stage_tail\n");
2132#endif /* IDETAPE_DEBUG_LOG */
2133 spin_lock_irqsave(&tape->spinlock, flags);
2134 stage->next = NULL;
2135 if (tape->last_stage != NULL)
2136 tape->last_stage->next=stage;
2137 else
2138 tape->first_stage = tape->next_stage=stage;
2139 tape->last_stage = stage;
2140 if (tape->next_stage == NULL)
2141 tape->next_stage = tape->last_stage;
2142 tape->nr_stages++;
2143 tape->nr_pending_stages++;
2144 spin_unlock_irqrestore(&tape->spinlock, flags);
2145}
2146
2147/*
2148 * idetape_wait_for_request installs a completion in a pending request
2149 * and sleeps until it is serviced.
2150 *
2151 * The caller should ensure that the request will not be serviced
2152 * before we install the completion (usually by disabling interrupts).
2153 */
2154static void idetape_wait_for_request (ide_drive_t *drive, struct request *rq)
2155{
Peter Zijlstra6e9a4732006-09-30 23:28:10 -07002156 DECLARE_COMPLETION_ONSTACK(wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002157 idetape_tape_t *tape = drive->driver_data;
2158
Jens Axboe4aff5e22006-08-10 08:44:47 +02002159 if (rq == NULL || !blk_special_request(rq)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002160 printk (KERN_ERR "ide-tape: bug: Trying to sleep on non-valid request\n");
2161 return;
2162 }
Jens Axboec00895a2006-09-30 20:29:12 +02002163 rq->end_io_data = &wait;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002164 rq->end_io = blk_end_sync_rq;
2165 spin_unlock_irq(&tape->spinlock);
2166 wait_for_completion(&wait);
2167 /* The stage and its struct request have been deallocated */
2168 spin_lock_irq(&tape->spinlock);
2169}
2170
2171static ide_startstop_t idetape_read_position_callback (ide_drive_t *drive)
2172{
2173 idetape_tape_t *tape = drive->driver_data;
2174 idetape_read_position_result_t *result;
2175
2176#if IDETAPE_DEBUG_LOG
2177 if (tape->debug_level >= 4)
2178 printk(KERN_INFO "ide-tape: Reached idetape_read_position_callback\n");
2179#endif /* IDETAPE_DEBUG_LOG */
2180
2181 if (!tape->pc->error) {
2182 result = (idetape_read_position_result_t *) tape->pc->buffer;
2183#if IDETAPE_DEBUG_LOG
2184 if (tape->debug_level >= 2)
2185 printk(KERN_INFO "ide-tape: BOP - %s\n",result->bop ? "Yes":"No");
2186 if (tape->debug_level >= 2)
2187 printk(KERN_INFO "ide-tape: EOP - %s\n",result->eop ? "Yes":"No");
2188#endif /* IDETAPE_DEBUG_LOG */
2189 if (result->bpu) {
2190 printk(KERN_INFO "ide-tape: Block location is unknown to the tape\n");
2191 clear_bit(IDETAPE_ADDRESS_VALID, &tape->flags);
2192 idetape_end_request(drive, 0, 0);
2193 } else {
2194#if IDETAPE_DEBUG_LOG
2195 if (tape->debug_level >= 2)
2196 printk(KERN_INFO "ide-tape: Block Location - %u\n", ntohl(result->first_block));
2197#endif /* IDETAPE_DEBUG_LOG */
2198 tape->partition = result->partition;
2199 tape->first_frame_position = ntohl(result->first_block);
2200 tape->last_frame_position = ntohl(result->last_block);
2201 tape->blocks_in_buffer = result->blocks_in_buffer[2];
2202 set_bit(IDETAPE_ADDRESS_VALID, &tape->flags);
2203 idetape_end_request(drive, 1, 0);
2204 }
2205 } else {
2206 idetape_end_request(drive, 0, 0);
2207 }
2208 return ide_stopped;
2209}
2210
2211/*
2212 * idetape_create_write_filemark_cmd will:
2213 *
2214 * 1. Write a filemark if write_filemark=1.
2215 * 2. Flush the device buffers without writing a filemark
2216 * if write_filemark=0.
2217 *
2218 */
2219static void idetape_create_write_filemark_cmd (ide_drive_t *drive, idetape_pc_t *pc,int write_filemark)
2220{
2221 idetape_init_pc(pc);
2222 pc->c[0] = IDETAPE_WRITE_FILEMARK_CMD;
2223 pc->c[4] = write_filemark;
2224 set_bit(PC_WAIT_FOR_DSC, &pc->flags);
2225 pc->callback = &idetape_pc_callback;
2226}
2227
2228static void idetape_create_test_unit_ready_cmd(idetape_pc_t *pc)
2229{
2230 idetape_init_pc(pc);
2231 pc->c[0] = IDETAPE_TEST_UNIT_READY_CMD;
2232 pc->callback = &idetape_pc_callback;
2233}
2234
2235/*
2236 * idetape_queue_pc_tail is based on the following functions:
2237 *
2238 * ide_do_drive_cmd from ide.c
2239 * cdrom_queue_request and cdrom_queue_packet_command from ide-cd.c
2240 *
2241 * We add a special packet command request to the tail of the request
2242 * queue, and wait for it to be serviced.
2243 *
2244 * This is not to be called from within the request handling part
2245 * of the driver ! We allocate here data in the stack, and it is valid
2246 * until the request is finished. This is not the case for the bottom
2247 * part of the driver, where we are always leaving the functions to wait
2248 * for an interrupt or a timer event.
2249 *
2250 * From the bottom part of the driver, we should allocate safe memory
2251 * using idetape_next_pc_storage and idetape_next_rq_storage, and add
2252 * the request to the request list without waiting for it to be serviced !
2253 * In that case, we usually use idetape_queue_pc_head.
2254 */
2255static int __idetape_queue_pc_tail (ide_drive_t *drive, idetape_pc_t *pc)
2256{
2257 struct ide_tape_obj *tape = drive->driver_data;
2258 struct request rq;
2259
2260 idetape_init_rq(&rq, REQ_IDETAPE_PC1);
2261 rq.buffer = (char *) pc;
2262 rq.rq_disk = tape->disk;
2263 return ide_do_drive_cmd(drive, &rq, ide_wait);
2264}
2265
2266static void idetape_create_load_unload_cmd (ide_drive_t *drive, idetape_pc_t *pc,int cmd)
2267{
2268 idetape_init_pc(pc);
2269 pc->c[0] = IDETAPE_LOAD_UNLOAD_CMD;
2270 pc->c[4] = cmd;
2271 set_bit(PC_WAIT_FOR_DSC, &pc->flags);
2272 pc->callback = &idetape_pc_callback;
2273}
2274
2275static int idetape_wait_ready(ide_drive_t *drive, unsigned long timeout)
2276{
2277 idetape_tape_t *tape = drive->driver_data;
2278 idetape_pc_t pc;
2279 int load_attempted = 0;
2280
2281 /*
2282 * Wait for the tape to become ready
2283 */
2284 set_bit(IDETAPE_MEDIUM_PRESENT, &tape->flags);
2285 timeout += jiffies;
2286 while (time_before(jiffies, timeout)) {
2287 idetape_create_test_unit_ready_cmd(&pc);
2288 if (!__idetape_queue_pc_tail(drive, &pc))
2289 return 0;
2290 if ((tape->sense_key == 2 && tape->asc == 4 && tape->ascq == 2)
2291 || (tape->asc == 0x3A)) { /* no media */
2292 if (load_attempted)
2293 return -ENOMEDIUM;
2294 idetape_create_load_unload_cmd(drive, &pc, IDETAPE_LU_LOAD_MASK);
2295 __idetape_queue_pc_tail(drive, &pc);
2296 load_attempted = 1;
2297 /* not about to be ready */
2298 } else if (!(tape->sense_key == 2 && tape->asc == 4 &&
2299 (tape->ascq == 1 || tape->ascq == 8)))
2300 return -EIO;
Nishanth Aravamudan80ce45f2005-09-10 00:27:08 -07002301 msleep(100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002302 }
2303 return -EIO;
2304}
2305
2306static int idetape_queue_pc_tail (ide_drive_t *drive,idetape_pc_t *pc)
2307{
2308 return __idetape_queue_pc_tail(drive, pc);
2309}
2310
2311static int idetape_flush_tape_buffers (ide_drive_t *drive)
2312{
2313 idetape_pc_t pc;
2314 int rc;
2315
2316 idetape_create_write_filemark_cmd(drive, &pc, 0);
2317 if ((rc = idetape_queue_pc_tail(drive, &pc)))
2318 return rc;
2319 idetape_wait_ready(drive, 60 * 5 * HZ);
2320 return 0;
2321}
2322
2323static void idetape_create_read_position_cmd (idetape_pc_t *pc)
2324{
2325 idetape_init_pc(pc);
2326 pc->c[0] = IDETAPE_READ_POSITION_CMD;
2327 pc->request_transfer = 20;
2328 pc->callback = &idetape_read_position_callback;
2329}
2330
2331static int idetape_read_position (ide_drive_t *drive)
2332{
2333 idetape_tape_t *tape = drive->driver_data;
2334 idetape_pc_t pc;
2335 int position;
2336
2337#if IDETAPE_DEBUG_LOG
2338 if (tape->debug_level >= 4)
2339 printk(KERN_INFO "ide-tape: Reached idetape_read_position\n");
2340#endif /* IDETAPE_DEBUG_LOG */
2341
2342 idetape_create_read_position_cmd(&pc);
2343 if (idetape_queue_pc_tail(drive, &pc))
2344 return -1;
2345 position = tape->first_frame_position;
2346 return position;
2347}
2348
2349static void idetape_create_locate_cmd (ide_drive_t *drive, idetape_pc_t *pc, unsigned int block, u8 partition, int skip)
2350{
2351 idetape_init_pc(pc);
2352 pc->c[0] = IDETAPE_LOCATE_CMD;
2353 pc->c[1] = 2;
2354 put_unaligned(htonl(block), (unsigned int *) &pc->c[3]);
2355 pc->c[8] = partition;
2356 set_bit(PC_WAIT_FOR_DSC, &pc->flags);
2357 pc->callback = &idetape_pc_callback;
2358}
2359
2360static int idetape_create_prevent_cmd (ide_drive_t *drive, idetape_pc_t *pc, int prevent)
2361{
2362 idetape_tape_t *tape = drive->driver_data;
2363
Borislav Petkovb6422012008-02-02 19:56:49 +01002364 /* device supports locking according to capabilities page */
2365 if (!(tape->caps[6] & 0x01))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002366 return 0;
2367
2368 idetape_init_pc(pc);
2369 pc->c[0] = IDETAPE_PREVENT_CMD;
2370 pc->c[4] = prevent;
2371 pc->callback = &idetape_pc_callback;
2372 return 1;
2373}
2374
2375static int __idetape_discard_read_pipeline (ide_drive_t *drive)
2376{
2377 idetape_tape_t *tape = drive->driver_data;
2378 unsigned long flags;
2379 int cnt;
2380
2381 if (tape->chrdev_direction != idetape_direction_read)
2382 return 0;
2383
2384 /* Remove merge stage. */
2385 cnt = tape->merge_stage_size / tape->tape_block_size;
2386 if (test_and_clear_bit(IDETAPE_FILEMARK, &tape->flags))
2387 ++cnt; /* Filemarks count as 1 sector */
2388 tape->merge_stage_size = 0;
2389 if (tape->merge_stage != NULL) {
2390 __idetape_kfree_stage(tape->merge_stage);
2391 tape->merge_stage = NULL;
2392 }
2393
2394 /* Clear pipeline flags. */
2395 clear_bit(IDETAPE_PIPELINE_ERROR, &tape->flags);
2396 tape->chrdev_direction = idetape_direction_none;
2397
2398 /* Remove pipeline stages. */
2399 if (tape->first_stage == NULL)
2400 return 0;
2401
2402 spin_lock_irqsave(&tape->spinlock, flags);
2403 tape->next_stage = NULL;
2404 if (idetape_pipeline_active(tape))
2405 idetape_wait_for_request(drive, tape->active_data_request);
2406 spin_unlock_irqrestore(&tape->spinlock, flags);
2407
2408 while (tape->first_stage != NULL) {
2409 struct request *rq_ptr = &tape->first_stage->rq;
2410
2411 cnt += rq_ptr->nr_sectors - rq_ptr->current_nr_sectors;
2412 if (rq_ptr->errors == IDETAPE_ERROR_FILEMARK)
2413 ++cnt;
2414 idetape_remove_stage_head(drive);
2415 }
2416 tape->nr_pending_stages = 0;
2417 tape->max_stages = tape->min_pipeline;
2418 return cnt;
2419}
2420
2421/*
2422 * idetape_position_tape positions the tape to the requested block
2423 * using the LOCATE packet command. A READ POSITION command is then
2424 * issued to check where we are positioned.
2425 *
2426 * Like all higher level operations, we queue the commands at the tail
2427 * of the request queue and wait for their completion.
2428 *
2429 */
2430static int idetape_position_tape (ide_drive_t *drive, unsigned int block, u8 partition, int skip)
2431{
2432 idetape_tape_t *tape = drive->driver_data;
2433 int retval;
2434 idetape_pc_t pc;
2435
2436 if (tape->chrdev_direction == idetape_direction_read)
2437 __idetape_discard_read_pipeline(drive);
2438 idetape_wait_ready(drive, 60 * 5 * HZ);
2439 idetape_create_locate_cmd(drive, &pc, block, partition, skip);
2440 retval = idetape_queue_pc_tail(drive, &pc);
2441 if (retval)
2442 return (retval);
2443
2444 idetape_create_read_position_cmd(&pc);
2445 return (idetape_queue_pc_tail(drive, &pc));
2446}
2447
2448static void idetape_discard_read_pipeline (ide_drive_t *drive, int restore_position)
2449{
2450 idetape_tape_t *tape = drive->driver_data;
2451 int cnt;
2452 int seek, position;
2453
2454 cnt = __idetape_discard_read_pipeline(drive);
2455 if (restore_position) {
2456 position = idetape_read_position(drive);
2457 seek = position > cnt ? position - cnt : 0;
2458 if (idetape_position_tape(drive, seek, 0, 0)) {
2459 printk(KERN_INFO "ide-tape: %s: position_tape failed in discard_pipeline()\n", tape->name);
2460 return;
2461 }
2462 }
2463}
2464
2465/*
2466 * idetape_queue_rw_tail generates a read/write request for the block
2467 * device interface and wait for it to be serviced.
2468 */
2469static int idetape_queue_rw_tail(ide_drive_t *drive, int cmd, int blocks, struct idetape_bh *bh)
2470{
2471 idetape_tape_t *tape = drive->driver_data;
2472 struct request rq;
2473
2474#if IDETAPE_DEBUG_LOG
2475 if (tape->debug_level >= 2)
2476 printk(KERN_INFO "ide-tape: idetape_queue_rw_tail: cmd=%d\n",cmd);
2477#endif /* IDETAPE_DEBUG_LOG */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002478 if (idetape_pipeline_active(tape)) {
2479 printk(KERN_ERR "ide-tape: bug: the pipeline is active in idetape_queue_rw_tail\n");
2480 return (0);
2481 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002482
2483 idetape_init_rq(&rq, cmd);
2484 rq.rq_disk = tape->disk;
2485 rq.special = (void *)bh;
2486 rq.sector = tape->first_frame_position;
2487 rq.nr_sectors = rq.current_nr_sectors = blocks;
2488 (void) ide_do_drive_cmd(drive, &rq, ide_wait);
2489
2490 if ((cmd & (REQ_IDETAPE_READ | REQ_IDETAPE_WRITE)) == 0)
2491 return 0;
2492
2493 if (tape->merge_stage)
2494 idetape_init_merge_stage(tape);
2495 if (rq.errors == IDETAPE_ERROR_GENERAL)
2496 return -EIO;
2497 return (tape->tape_block_size * (blocks-rq.current_nr_sectors));
2498}
2499
2500/*
2501 * idetape_insert_pipeline_into_queue is used to start servicing the
2502 * pipeline stages, starting from tape->next_stage.
2503 */
2504static void idetape_insert_pipeline_into_queue (ide_drive_t *drive)
2505{
2506 idetape_tape_t *tape = drive->driver_data;
2507
2508 if (tape->next_stage == NULL)
2509 return;
2510 if (!idetape_pipeline_active(tape)) {
2511 set_bit(IDETAPE_PIPELINE_ACTIVE, &tape->flags);
2512 idetape_active_next_stage(drive);
2513 (void) ide_do_drive_cmd(drive, tape->active_data_request, ide_end);
2514 }
2515}
2516
2517static void idetape_create_inquiry_cmd (idetape_pc_t *pc)
2518{
2519 idetape_init_pc(pc);
2520 pc->c[0] = IDETAPE_INQUIRY_CMD;
2521 pc->c[4] = pc->request_transfer = 254;
2522 pc->callback = &idetape_pc_callback;
2523}
2524
2525static void idetape_create_rewind_cmd (ide_drive_t *drive, idetape_pc_t *pc)
2526{
2527 idetape_init_pc(pc);
2528 pc->c[0] = IDETAPE_REWIND_CMD;
2529 set_bit(PC_WAIT_FOR_DSC, &pc->flags);
2530 pc->callback = &idetape_pc_callback;
2531}
2532
Linus Torvalds1da177e2005-04-16 15:20:36 -07002533static void idetape_create_erase_cmd (idetape_pc_t *pc)
2534{
2535 idetape_init_pc(pc);
2536 pc->c[0] = IDETAPE_ERASE_CMD;
2537 pc->c[1] = 1;
2538 set_bit(PC_WAIT_FOR_DSC, &pc->flags);
2539 pc->callback = &idetape_pc_callback;
2540}
2541
2542static void idetape_create_space_cmd (idetape_pc_t *pc,int count, u8 cmd)
2543{
2544 idetape_init_pc(pc);
2545 pc->c[0] = IDETAPE_SPACE_CMD;
2546 put_unaligned(htonl(count), (unsigned int *) &pc->c[1]);
2547 pc->c[1] = cmd;
2548 set_bit(PC_WAIT_FOR_DSC, &pc->flags);
2549 pc->callback = &idetape_pc_callback;
2550}
2551
2552static void idetape_wait_first_stage (ide_drive_t *drive)
2553{
2554 idetape_tape_t *tape = drive->driver_data;
2555 unsigned long flags;
2556
2557 if (tape->first_stage == NULL)
2558 return;
2559 spin_lock_irqsave(&tape->spinlock, flags);
2560 if (tape->active_stage == tape->first_stage)
2561 idetape_wait_for_request(drive, tape->active_data_request);
2562 spin_unlock_irqrestore(&tape->spinlock, flags);
2563}
2564
2565/*
2566 * idetape_add_chrdev_write_request tries to add a character device
2567 * originated write request to our pipeline. In case we don't succeed,
2568 * we revert to non-pipelined operation mode for this request.
2569 *
2570 * 1. Try to allocate a new pipeline stage.
2571 * 2. If we can't, wait for more and more requests to be serviced
2572 * and try again each time.
2573 * 3. If we still can't allocate a stage, fallback to
2574 * non-pipelined operation mode for this request.
2575 */
2576static int idetape_add_chrdev_write_request (ide_drive_t *drive, int blocks)
2577{
2578 idetape_tape_t *tape = drive->driver_data;
2579 idetape_stage_t *new_stage;
2580 unsigned long flags;
2581 struct request *rq;
2582
2583#if IDETAPE_DEBUG_LOG
2584 if (tape->debug_level >= 3)
2585 printk(KERN_INFO "ide-tape: Reached idetape_add_chrdev_write_request\n");
2586#endif /* IDETAPE_DEBUG_LOG */
2587
2588 /*
2589 * Attempt to allocate a new stage.
2590 * Pay special attention to possible race conditions.
2591 */
2592 while ((new_stage = idetape_kmalloc_stage(tape)) == NULL) {
2593 spin_lock_irqsave(&tape->spinlock, flags);
2594 if (idetape_pipeline_active(tape)) {
2595 idetape_wait_for_request(drive, tape->active_data_request);
2596 spin_unlock_irqrestore(&tape->spinlock, flags);
2597 } else {
2598 spin_unlock_irqrestore(&tape->spinlock, flags);
2599 idetape_insert_pipeline_into_queue(drive);
2600 if (idetape_pipeline_active(tape))
2601 continue;
2602 /*
2603 * Linux is short on memory. Fallback to
2604 * non-pipelined operation mode for this request.
2605 */
2606 return idetape_queue_rw_tail(drive, REQ_IDETAPE_WRITE, blocks, tape->merge_stage->bh);
2607 }
2608 }
2609 rq = &new_stage->rq;
2610 idetape_init_rq(rq, REQ_IDETAPE_WRITE);
2611 /* Doesn't actually matter - We always assume sequential access */
2612 rq->sector = tape->first_frame_position;
2613 rq->nr_sectors = rq->current_nr_sectors = blocks;
2614
2615 idetape_switch_buffers(tape, new_stage);
2616 idetape_add_stage_tail(drive, new_stage);
2617 tape->pipeline_head++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002618 calculate_speeds(drive);
2619
2620 /*
2621 * Estimate whether the tape has stopped writing by checking
2622 * if our write pipeline is currently empty. If we are not
2623 * writing anymore, wait for the pipeline to be full enough
2624 * (90%) before starting to service requests, so that we will
2625 * be able to keep up with the higher speeds of the tape.
2626 */
2627 if (!idetape_pipeline_active(tape)) {
2628 if (tape->nr_stages >= tape->max_stages * 9 / 10 ||
2629 tape->nr_stages >= tape->max_stages - tape->uncontrolled_pipeline_head_speed * 3 * 1024 / tape->tape_block_size) {
2630 tape->measure_insert_time = 1;
2631 tape->insert_time = jiffies;
2632 tape->insert_size = 0;
2633 tape->insert_speed = 0;
2634 idetape_insert_pipeline_into_queue(drive);
2635 }
2636 }
2637 if (test_and_clear_bit(IDETAPE_PIPELINE_ERROR, &tape->flags))
2638 /* Return a deferred error */
2639 return -EIO;
2640 return blocks;
2641}
2642
2643/*
2644 * idetape_wait_for_pipeline will wait until all pending pipeline
2645 * requests are serviced. Typically called on device close.
2646 */
2647static void idetape_wait_for_pipeline (ide_drive_t *drive)
2648{
2649 idetape_tape_t *tape = drive->driver_data;
2650 unsigned long flags;
2651
2652 while (tape->next_stage || idetape_pipeline_active(tape)) {
2653 idetape_insert_pipeline_into_queue(drive);
2654 spin_lock_irqsave(&tape->spinlock, flags);
2655 if (idetape_pipeline_active(tape))
2656 idetape_wait_for_request(drive, tape->active_data_request);
2657 spin_unlock_irqrestore(&tape->spinlock, flags);
2658 }
2659}
2660
2661static void idetape_empty_write_pipeline (ide_drive_t *drive)
2662{
2663 idetape_tape_t *tape = drive->driver_data;
2664 int blocks, min;
2665 struct idetape_bh *bh;
Borislav Petkov55a5d292008-02-02 19:56:49 +01002666
Linus Torvalds1da177e2005-04-16 15:20:36 -07002667 if (tape->chrdev_direction != idetape_direction_write) {
2668 printk(KERN_ERR "ide-tape: bug: Trying to empty write pipeline, but we are not writing.\n");
2669 return;
2670 }
2671 if (tape->merge_stage_size > tape->stage_size) {
2672 printk(KERN_ERR "ide-tape: bug: merge_buffer too big\n");
2673 tape->merge_stage_size = tape->stage_size;
2674 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002675 if (tape->merge_stage_size) {
2676 blocks = tape->merge_stage_size / tape->tape_block_size;
2677 if (tape->merge_stage_size % tape->tape_block_size) {
2678 unsigned int i;
2679
2680 blocks++;
2681 i = tape->tape_block_size - tape->merge_stage_size % tape->tape_block_size;
2682 bh = tape->bh->b_reqnext;
2683 while (bh) {
2684 atomic_set(&bh->b_count, 0);
2685 bh = bh->b_reqnext;
2686 }
2687 bh = tape->bh;
2688 while (i) {
2689 if (bh == NULL) {
2690
2691 printk(KERN_INFO "ide-tape: bug, bh NULL\n");
2692 break;
2693 }
2694 min = min(i, (unsigned int)(bh->b_size - atomic_read(&bh->b_count)));
2695 memset(bh->b_data + atomic_read(&bh->b_count), 0, min);
2696 atomic_add(min, &bh->b_count);
2697 i -= min;
2698 bh = bh->b_reqnext;
2699 }
2700 }
2701 (void) idetape_add_chrdev_write_request(drive, blocks);
2702 tape->merge_stage_size = 0;
2703 }
2704 idetape_wait_for_pipeline(drive);
2705 if (tape->merge_stage != NULL) {
2706 __idetape_kfree_stage(tape->merge_stage);
2707 tape->merge_stage = NULL;
2708 }
2709 clear_bit(IDETAPE_PIPELINE_ERROR, &tape->flags);
2710 tape->chrdev_direction = idetape_direction_none;
2711
2712 /*
2713 * On the next backup, perform the feedback loop again.
2714 * (I don't want to keep sense information between backups,
2715 * as some systems are constantly on, and the system load
2716 * can be totally different on the next backup).
2717 */
2718 tape->max_stages = tape->min_pipeline;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002719 if (tape->first_stage != NULL ||
2720 tape->next_stage != NULL ||
2721 tape->last_stage != NULL ||
2722 tape->nr_stages != 0) {
2723 printk(KERN_ERR "ide-tape: ide-tape pipeline bug, "
2724 "first_stage %p, next_stage %p, "
2725 "last_stage %p, nr_stages %d\n",
2726 tape->first_stage, tape->next_stage,
2727 tape->last_stage, tape->nr_stages);
2728 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002729}
2730
2731static void idetape_restart_speed_control (ide_drive_t *drive)
2732{
2733 idetape_tape_t *tape = drive->driver_data;
2734
2735 tape->restart_speed_control_req = 0;
2736 tape->pipeline_head = 0;
2737 tape->controlled_last_pipeline_head = tape->uncontrolled_last_pipeline_head = 0;
2738 tape->controlled_previous_pipeline_head = tape->uncontrolled_previous_pipeline_head = 0;
2739 tape->pipeline_head_speed = tape->controlled_pipeline_head_speed = 5000;
2740 tape->uncontrolled_pipeline_head_speed = 0;
2741 tape->controlled_pipeline_head_time = tape->uncontrolled_pipeline_head_time = jiffies;
2742 tape->controlled_previous_head_time = tape->uncontrolled_previous_head_time = jiffies;
2743}
2744
2745static int idetape_initiate_read (ide_drive_t *drive, int max_stages)
2746{
2747 idetape_tape_t *tape = drive->driver_data;
2748 idetape_stage_t *new_stage;
2749 struct request rq;
2750 int bytes_read;
Borislav Petkovb6422012008-02-02 19:56:49 +01002751 u16 blocks = *(u16 *)&tape->caps[12];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002752
2753 /* Initialize read operation */
2754 if (tape->chrdev_direction != idetape_direction_read) {
2755 if (tape->chrdev_direction == idetape_direction_write) {
2756 idetape_empty_write_pipeline(drive);
2757 idetape_flush_tape_buffers(drive);
2758 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002759 if (tape->merge_stage || tape->merge_stage_size) {
2760 printk (KERN_ERR "ide-tape: merge_stage_size should be 0 now\n");
2761 tape->merge_stage_size = 0;
2762 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002763 if ((tape->merge_stage = __idetape_kmalloc_stage(tape, 0, 0)) == NULL)
2764 return -ENOMEM;
2765 tape->chrdev_direction = idetape_direction_read;
2766
2767 /*
2768 * Issue a read 0 command to ensure that DSC handshake
2769 * is switched from completion mode to buffer available
2770 * mode.
2771 * No point in issuing this if DSC overlap isn't supported,
2772 * some drives (Seagate STT3401A) will return an error.
2773 */
2774 if (drive->dsc_overlap) {
2775 bytes_read = idetape_queue_rw_tail(drive, REQ_IDETAPE_READ, 0, tape->merge_stage->bh);
2776 if (bytes_read < 0) {
2777 __idetape_kfree_stage(tape->merge_stage);
2778 tape->merge_stage = NULL;
2779 tape->chrdev_direction = idetape_direction_none;
2780 return bytes_read;
2781 }
2782 }
2783 }
2784 if (tape->restart_speed_control_req)
2785 idetape_restart_speed_control(drive);
2786 idetape_init_rq(&rq, REQ_IDETAPE_READ);
2787 rq.sector = tape->first_frame_position;
2788 rq.nr_sectors = rq.current_nr_sectors = blocks;
2789 if (!test_bit(IDETAPE_PIPELINE_ERROR, &tape->flags) &&
2790 tape->nr_stages < max_stages) {
2791 new_stage = idetape_kmalloc_stage(tape);
2792 while (new_stage != NULL) {
2793 new_stage->rq = rq;
2794 idetape_add_stage_tail(drive, new_stage);
2795 if (tape->nr_stages >= max_stages)
2796 break;
2797 new_stage = idetape_kmalloc_stage(tape);
2798 }
2799 }
2800 if (!idetape_pipeline_active(tape)) {
2801 if (tape->nr_pending_stages >= 3 * max_stages / 4) {
2802 tape->measure_insert_time = 1;
2803 tape->insert_time = jiffies;
2804 tape->insert_size = 0;
2805 tape->insert_speed = 0;
2806 idetape_insert_pipeline_into_queue(drive);
2807 }
2808 }
2809 return 0;
2810}
2811
2812/*
2813 * idetape_add_chrdev_read_request is called from idetape_chrdev_read
2814 * to service a character device read request and add read-ahead
2815 * requests to our pipeline.
2816 */
2817static int idetape_add_chrdev_read_request (ide_drive_t *drive,int blocks)
2818{
2819 idetape_tape_t *tape = drive->driver_data;
2820 unsigned long flags;
2821 struct request *rq_ptr;
2822 int bytes_read;
2823
2824#if IDETAPE_DEBUG_LOG
2825 if (tape->debug_level >= 4)
2826 printk(KERN_INFO "ide-tape: Reached idetape_add_chrdev_read_request, %d blocks\n", blocks);
2827#endif /* IDETAPE_DEBUG_LOG */
2828
2829 /*
2830 * If we are at a filemark, return a read length of 0
2831 */
2832 if (test_bit(IDETAPE_FILEMARK, &tape->flags))
2833 return 0;
2834
2835 /*
2836 * Wait for the next block to be available at the head
2837 * of the pipeline
2838 */
2839 idetape_initiate_read(drive, tape->max_stages);
2840 if (tape->first_stage == NULL) {
2841 if (test_bit(IDETAPE_PIPELINE_ERROR, &tape->flags))
2842 return 0;
2843 return idetape_queue_rw_tail(drive, REQ_IDETAPE_READ, blocks, tape->merge_stage->bh);
2844 }
2845 idetape_wait_first_stage(drive);
2846 rq_ptr = &tape->first_stage->rq;
2847 bytes_read = tape->tape_block_size * (rq_ptr->nr_sectors - rq_ptr->current_nr_sectors);
2848 rq_ptr->nr_sectors = rq_ptr->current_nr_sectors = 0;
2849
2850
2851 if (rq_ptr->errors == IDETAPE_ERROR_EOD)
2852 return 0;
2853 else {
2854 idetape_switch_buffers(tape, tape->first_stage);
2855 if (rq_ptr->errors == IDETAPE_ERROR_FILEMARK)
2856 set_bit(IDETAPE_FILEMARK, &tape->flags);
2857 spin_lock_irqsave(&tape->spinlock, flags);
2858 idetape_remove_stage_head(drive);
2859 spin_unlock_irqrestore(&tape->spinlock, flags);
2860 tape->pipeline_head++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002861 calculate_speeds(drive);
2862 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002863 if (bytes_read > blocks * tape->tape_block_size) {
2864 printk(KERN_ERR "ide-tape: bug: trying to return more bytes than requested\n");
2865 bytes_read = blocks * tape->tape_block_size;
2866 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002867 return (bytes_read);
2868}
2869
2870static void idetape_pad_zeros (ide_drive_t *drive, int bcount)
2871{
2872 idetape_tape_t *tape = drive->driver_data;
2873 struct idetape_bh *bh;
2874 int blocks;
2875
2876 while (bcount) {
2877 unsigned int count;
2878
2879 bh = tape->merge_stage->bh;
2880 count = min(tape->stage_size, bcount);
2881 bcount -= count;
2882 blocks = count / tape->tape_block_size;
2883 while (count) {
2884 atomic_set(&bh->b_count, min(count, (unsigned int)bh->b_size));
2885 memset(bh->b_data, 0, atomic_read(&bh->b_count));
2886 count -= atomic_read(&bh->b_count);
2887 bh = bh->b_reqnext;
2888 }
2889 idetape_queue_rw_tail(drive, REQ_IDETAPE_WRITE, blocks, tape->merge_stage->bh);
2890 }
2891}
2892
2893static int idetape_pipeline_size (ide_drive_t *drive)
2894{
2895 idetape_tape_t *tape = drive->driver_data;
2896 idetape_stage_t *stage;
2897 struct request *rq;
2898 int size = 0;
2899
2900 idetape_wait_for_pipeline(drive);
2901 stage = tape->first_stage;
2902 while (stage != NULL) {
2903 rq = &stage->rq;
2904 size += tape->tape_block_size * (rq->nr_sectors-rq->current_nr_sectors);
2905 if (rq->errors == IDETAPE_ERROR_FILEMARK)
2906 size += tape->tape_block_size;
2907 stage = stage->next;
2908 }
2909 size += tape->merge_stage_size;
2910 return size;
2911}
2912
2913/*
2914 * Rewinds the tape to the Beginning Of the current Partition (BOP).
2915 *
2916 * We currently support only one partition.
2917 */
2918static int idetape_rewind_tape (ide_drive_t *drive)
2919{
2920 int retval;
2921 idetape_pc_t pc;
2922#if IDETAPE_DEBUG_LOG
2923 idetape_tape_t *tape = drive->driver_data;
2924 if (tape->debug_level >= 2)
2925 printk(KERN_INFO "ide-tape: Reached idetape_rewind_tape\n");
2926#endif /* IDETAPE_DEBUG_LOG */
2927
2928 idetape_create_rewind_cmd(drive, &pc);
2929 retval = idetape_queue_pc_tail(drive, &pc);
2930 if (retval)
2931 return retval;
2932
2933 idetape_create_read_position_cmd(&pc);
2934 retval = idetape_queue_pc_tail(drive, &pc);
2935 if (retval)
2936 return retval;
2937 return 0;
2938}
2939
2940/*
2941 * Our special ide-tape ioctl's.
2942 *
2943 * Currently there aren't any ioctl's.
2944 * mtio.h compatible commands should be issued to the character device
2945 * interface.
2946 */
2947static int idetape_blkdev_ioctl(ide_drive_t *drive, unsigned int cmd, unsigned long arg)
2948{
2949 idetape_tape_t *tape = drive->driver_data;
2950 idetape_config_t config;
2951 void __user *argp = (void __user *)arg;
2952
2953#if IDETAPE_DEBUG_LOG
2954 if (tape->debug_level >= 4)
2955 printk(KERN_INFO "ide-tape: Reached idetape_blkdev_ioctl\n");
2956#endif /* IDETAPE_DEBUG_LOG */
2957 switch (cmd) {
2958 case 0x0340:
2959 if (copy_from_user(&config, argp, sizeof (idetape_config_t)))
2960 return -EFAULT;
2961 tape->best_dsc_rw_frequency = config.dsc_rw_frequency;
2962 tape->max_stages = config.nr_stages;
2963 break;
2964 case 0x0350:
2965 config.dsc_rw_frequency = (int) tape->best_dsc_rw_frequency;
2966 config.nr_stages = tape->max_stages;
2967 if (copy_to_user(argp, &config, sizeof (idetape_config_t)))
2968 return -EFAULT;
2969 break;
2970 default:
2971 return -EIO;
2972 }
2973 return 0;
2974}
2975
2976/*
2977 * idetape_space_over_filemarks is now a bit more complicated than just
2978 * passing the command to the tape since we may have crossed some
2979 * filemarks during our pipelined read-ahead mode.
2980 *
2981 * As a minor side effect, the pipeline enables us to support MTFSFM when
2982 * the filemark is in our internal pipeline even if the tape doesn't
2983 * support spacing over filemarks in the reverse direction.
2984 */
2985static int idetape_space_over_filemarks (ide_drive_t *drive,short mt_op,int mt_count)
2986{
2987 idetape_tape_t *tape = drive->driver_data;
2988 idetape_pc_t pc;
2989 unsigned long flags;
2990 int retval,count=0;
Borislav Petkovb6422012008-02-02 19:56:49 +01002991 int sprev = !!(tape->caps[4] & 0x20);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002992
2993 if (mt_count == 0)
2994 return 0;
2995 if (MTBSF == mt_op || MTBSFM == mt_op) {
Borislav Petkovb6422012008-02-02 19:56:49 +01002996 if (!sprev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002997 return -EIO;
2998 mt_count = - mt_count;
2999 }
3000
3001 if (tape->chrdev_direction == idetape_direction_read) {
3002 /*
3003 * We have a read-ahead buffer. Scan it for crossed
3004 * filemarks.
3005 */
3006 tape->merge_stage_size = 0;
3007 if (test_and_clear_bit(IDETAPE_FILEMARK, &tape->flags))
3008 ++count;
3009 while (tape->first_stage != NULL) {
3010 if (count == mt_count) {
3011 if (mt_op == MTFSFM)
3012 set_bit(IDETAPE_FILEMARK, &tape->flags);
3013 return 0;
3014 }
3015 spin_lock_irqsave(&tape->spinlock, flags);
3016 if (tape->first_stage == tape->active_stage) {
3017 /*
3018 * We have reached the active stage in the read pipeline.
3019 * There is no point in allowing the drive to continue
3020 * reading any farther, so we stop the pipeline.
3021 *
3022 * This section should be moved to a separate subroutine,
3023 * because a similar function is performed in
3024 * __idetape_discard_read_pipeline(), for example.
3025 */
3026 tape->next_stage = NULL;
3027 spin_unlock_irqrestore(&tape->spinlock, flags);
3028 idetape_wait_first_stage(drive);
3029 tape->next_stage = tape->first_stage->next;
3030 } else
3031 spin_unlock_irqrestore(&tape->spinlock, flags);
3032 if (tape->first_stage->rq.errors == IDETAPE_ERROR_FILEMARK)
3033 ++count;
3034 idetape_remove_stage_head(drive);
3035 }
3036 idetape_discard_read_pipeline(drive, 0);
3037 }
3038
3039 /*
3040 * The filemark was not found in our internal pipeline.
3041 * Now we can issue the space command.
3042 */
3043 switch (mt_op) {
3044 case MTFSF:
3045 case MTBSF:
3046 idetape_create_space_cmd(&pc,mt_count-count,IDETAPE_SPACE_OVER_FILEMARK);
3047 return (idetape_queue_pc_tail(drive, &pc));
3048 case MTFSFM:
3049 case MTBSFM:
Borislav Petkovb6422012008-02-02 19:56:49 +01003050 if (!sprev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003051 return (-EIO);
3052 retval = idetape_space_over_filemarks(drive, MTFSF, mt_count-count);
3053 if (retval) return (retval);
3054 count = (MTBSFM == mt_op ? 1 : -1);
3055 return (idetape_space_over_filemarks(drive, MTFSF, count));
3056 default:
3057 printk(KERN_ERR "ide-tape: MTIO operation %d not supported\n",mt_op);
3058 return (-EIO);
3059 }
3060}
3061
3062
3063/*
3064 * Our character device read / write functions.
3065 *
3066 * The tape is optimized to maximize throughput when it is transferring
3067 * an integral number of the "continuous transfer limit", which is
3068 * a parameter of the specific tape (26 KB on my particular tape).
3069 * (32 kB for Onstream)
3070 *
3071 * As of version 1.3 of the driver, the character device provides an
3072 * abstract continuous view of the media - any mix of block sizes (even 1
3073 * byte) on the same backup/restore procedure is supported. The driver
3074 * will internally convert the requests to the recommended transfer unit,
3075 * so that an unmatch between the user's block size to the recommended
3076 * size will only result in a (slightly) increased driver overhead, but
3077 * will no longer hit performance.
3078 * This is not applicable to Onstream.
3079 */
3080static ssize_t idetape_chrdev_read (struct file *file, char __user *buf,
3081 size_t count, loff_t *ppos)
3082{
3083 struct ide_tape_obj *tape = ide_tape_f(file);
3084 ide_drive_t *drive = tape->drive;
3085 ssize_t bytes_read,temp, actually_read = 0, rc;
Daniel Walkerdcd96372006-06-25 05:47:37 -07003086 ssize_t ret = 0;
Borislav Petkovb6422012008-02-02 19:56:49 +01003087 u16 ctl = *(u16 *)&tape->caps[12];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003088
3089#if IDETAPE_DEBUG_LOG
3090 if (tape->debug_level >= 3)
3091 printk(KERN_INFO "ide-tape: Reached idetape_chrdev_read, count %Zd\n", count);
3092#endif /* IDETAPE_DEBUG_LOG */
3093
3094 if (tape->chrdev_direction != idetape_direction_read) {
3095 if (test_bit(IDETAPE_DETECT_BS, &tape->flags))
3096 if (count > tape->tape_block_size &&
3097 (count % tape->tape_block_size) == 0)
3098 tape->user_bs_factor = count / tape->tape_block_size;
3099 }
3100 if ((rc = idetape_initiate_read(drive, tape->max_stages)) < 0)
3101 return rc;
3102 if (count == 0)
3103 return (0);
3104 if (tape->merge_stage_size) {
3105 actually_read = min((unsigned int)(tape->merge_stage_size), (unsigned int)count);
Daniel Walkerdcd96372006-06-25 05:47:37 -07003106 if (idetape_copy_stage_to_user(tape, buf, tape->merge_stage, actually_read))
3107 ret = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003108 buf += actually_read;
3109 tape->merge_stage_size -= actually_read;
3110 count -= actually_read;
3111 }
3112 while (count >= tape->stage_size) {
Borislav Petkovb6422012008-02-02 19:56:49 +01003113 bytes_read = idetape_add_chrdev_read_request(drive, ctl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003114 if (bytes_read <= 0)
3115 goto finish;
Daniel Walkerdcd96372006-06-25 05:47:37 -07003116 if (idetape_copy_stage_to_user(tape, buf, tape->merge_stage, bytes_read))
3117 ret = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003118 buf += bytes_read;
3119 count -= bytes_read;
3120 actually_read += bytes_read;
3121 }
3122 if (count) {
Borislav Petkovb6422012008-02-02 19:56:49 +01003123 bytes_read = idetape_add_chrdev_read_request(drive, ctl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003124 if (bytes_read <= 0)
3125 goto finish;
3126 temp = min((unsigned long)count, (unsigned long)bytes_read);
Daniel Walkerdcd96372006-06-25 05:47:37 -07003127 if (idetape_copy_stage_to_user(tape, buf, tape->merge_stage, temp))
3128 ret = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003129 actually_read += temp;
3130 tape->merge_stage_size = bytes_read-temp;
3131 }
3132finish:
3133 if (!actually_read && test_bit(IDETAPE_FILEMARK, &tape->flags)) {
3134#if IDETAPE_DEBUG_LOG
3135 if (tape->debug_level >= 2)
3136 printk(KERN_INFO "ide-tape: %s: spacing over filemark\n", tape->name);
3137#endif
3138 idetape_space_over_filemarks(drive, MTFSF, 1);
3139 return 0;
3140 }
Daniel Walkerdcd96372006-06-25 05:47:37 -07003141
3142 return (ret) ? ret : actually_read;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003143}
3144
3145static ssize_t idetape_chrdev_write (struct file *file, const char __user *buf,
3146 size_t count, loff_t *ppos)
3147{
3148 struct ide_tape_obj *tape = ide_tape_f(file);
3149 ide_drive_t *drive = tape->drive;
Daniel Walkerdcd96372006-06-25 05:47:37 -07003150 ssize_t actually_written = 0;
3151 ssize_t ret = 0;
Borislav Petkovb6422012008-02-02 19:56:49 +01003152 u16 ctl = *(u16 *)&tape->caps[12];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003153
3154 /* The drive is write protected. */
3155 if (tape->write_prot)
3156 return -EACCES;
3157
3158#if IDETAPE_DEBUG_LOG
3159 if (tape->debug_level >= 3)
3160 printk(KERN_INFO "ide-tape: Reached idetape_chrdev_write, "
3161 "count %Zd\n", count);
3162#endif /* IDETAPE_DEBUG_LOG */
3163
3164 /* Initialize write operation */
3165 if (tape->chrdev_direction != idetape_direction_write) {
3166 if (tape->chrdev_direction == idetape_direction_read)
3167 idetape_discard_read_pipeline(drive, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003168 if (tape->merge_stage || tape->merge_stage_size) {
3169 printk(KERN_ERR "ide-tape: merge_stage_size "
3170 "should be 0 now\n");
3171 tape->merge_stage_size = 0;
3172 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003173 if ((tape->merge_stage = __idetape_kmalloc_stage(tape, 0, 0)) == NULL)
3174 return -ENOMEM;
3175 tape->chrdev_direction = idetape_direction_write;
3176 idetape_init_merge_stage(tape);
3177
3178 /*
3179 * Issue a write 0 command to ensure that DSC handshake
3180 * is switched from completion mode to buffer available
3181 * mode.
3182 * No point in issuing this if DSC overlap isn't supported,
3183 * some drives (Seagate STT3401A) will return an error.
3184 */
3185 if (drive->dsc_overlap) {
Daniel Walkerdcd96372006-06-25 05:47:37 -07003186 ssize_t retval = idetape_queue_rw_tail(drive, REQ_IDETAPE_WRITE, 0, tape->merge_stage->bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003187 if (retval < 0) {
3188 __idetape_kfree_stage(tape->merge_stage);
3189 tape->merge_stage = NULL;
3190 tape->chrdev_direction = idetape_direction_none;
3191 return retval;
3192 }
3193 }
3194 }
3195 if (count == 0)
3196 return (0);
3197 if (tape->restart_speed_control_req)
3198 idetape_restart_speed_control(drive);
3199 if (tape->merge_stage_size) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003200 if (tape->merge_stage_size >= tape->stage_size) {
3201 printk(KERN_ERR "ide-tape: bug: merge buffer too big\n");
3202 tape->merge_stage_size = 0;
3203 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003204 actually_written = min((unsigned int)(tape->stage_size - tape->merge_stage_size), (unsigned int)count);
Daniel Walkerdcd96372006-06-25 05:47:37 -07003205 if (idetape_copy_stage_from_user(tape, tape->merge_stage, buf, actually_written))
3206 ret = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003207 buf += actually_written;
3208 tape->merge_stage_size += actually_written;
3209 count -= actually_written;
3210
3211 if (tape->merge_stage_size == tape->stage_size) {
Daniel Walkerdcd96372006-06-25 05:47:37 -07003212 ssize_t retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003213 tape->merge_stage_size = 0;
Borislav Petkovb6422012008-02-02 19:56:49 +01003214 retval = idetape_add_chrdev_write_request(drive, ctl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003215 if (retval <= 0)
3216 return (retval);
3217 }
3218 }
3219 while (count >= tape->stage_size) {
Daniel Walkerdcd96372006-06-25 05:47:37 -07003220 ssize_t retval;
3221 if (idetape_copy_stage_from_user(tape, tape->merge_stage, buf, tape->stage_size))
3222 ret = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003223 buf += tape->stage_size;
3224 count -= tape->stage_size;
Borislav Petkovb6422012008-02-02 19:56:49 +01003225 retval = idetape_add_chrdev_write_request(drive, ctl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003226 actually_written += tape->stage_size;
3227 if (retval <= 0)
3228 return (retval);
3229 }
3230 if (count) {
3231 actually_written += count;
Daniel Walkerdcd96372006-06-25 05:47:37 -07003232 if (idetape_copy_stage_from_user(tape, tape->merge_stage, buf, count))
3233 ret = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003234 tape->merge_stage_size += count;
3235 }
Daniel Walkerdcd96372006-06-25 05:47:37 -07003236 return (ret) ? ret : actually_written;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003237}
3238
3239static int idetape_write_filemark (ide_drive_t *drive)
3240{
3241 idetape_pc_t pc;
3242
3243 /* Write a filemark */
3244 idetape_create_write_filemark_cmd(drive, &pc, 1);
3245 if (idetape_queue_pc_tail(drive, &pc)) {
3246 printk(KERN_ERR "ide-tape: Couldn't write a filemark\n");
3247 return -EIO;
3248 }
3249 return 0;
3250}
3251
3252/*
3253 * idetape_mtioctop is called from idetape_chrdev_ioctl when
3254 * the general mtio MTIOCTOP ioctl is requested.
3255 *
3256 * We currently support the following mtio.h operations:
3257 *
3258 * MTFSF - Space over mt_count filemarks in the positive direction.
3259 * The tape is positioned after the last spaced filemark.
3260 *
3261 * MTFSFM - Same as MTFSF, but the tape is positioned before the
3262 * last filemark.
3263 *
3264 * MTBSF - Steps background over mt_count filemarks, tape is
3265 * positioned before the last filemark.
3266 *
3267 * MTBSFM - Like MTBSF, only tape is positioned after the last filemark.
3268 *
3269 * Note:
3270 *
3271 * MTBSF and MTBSFM are not supported when the tape doesn't
3272 * support spacing over filemarks in the reverse direction.
3273 * In this case, MTFSFM is also usually not supported (it is
3274 * supported in the rare case in which we crossed the filemark
3275 * during our read-ahead pipelined operation mode).
3276 *
3277 * MTWEOF - Writes mt_count filemarks. Tape is positioned after
3278 * the last written filemark.
3279 *
3280 * MTREW - Rewinds tape.
3281 *
3282 * MTLOAD - Loads the tape.
3283 *
3284 * MTOFFL - Puts the tape drive "Offline": Rewinds the tape and
3285 * MTUNLOAD prevents further access until the media is replaced.
3286 *
3287 * MTNOP - Flushes tape buffers.
3288 *
3289 * MTRETEN - Retension media. This typically consists of one end
3290 * to end pass on the media.
3291 *
3292 * MTEOM - Moves to the end of recorded data.
3293 *
3294 * MTERASE - Erases tape.
3295 *
3296 * MTSETBLK - Sets the user block size to mt_count bytes. If
3297 * mt_count is 0, we will attempt to autodetect
3298 * the block size.
3299 *
3300 * MTSEEK - Positions the tape in a specific block number, where
3301 * each block is assumed to contain which user_block_size
3302 * bytes.
3303 *
3304 * MTSETPART - Switches to another tape partition.
3305 *
3306 * MTLOCK - Locks the tape door.
3307 *
3308 * MTUNLOCK - Unlocks the tape door.
3309 *
3310 * The following commands are currently not supported:
3311 *
3312 * MTFSS, MTBSS, MTWSM, MTSETDENSITY,
3313 * MTSETDRVBUFFER, MT_ST_BOOLEANS, MT_ST_WRITE_THRESHOLD.
3314 */
3315static int idetape_mtioctop (ide_drive_t *drive,short mt_op,int mt_count)
3316{
3317 idetape_tape_t *tape = drive->driver_data;
3318 idetape_pc_t pc;
3319 int i,retval;
3320
3321#if IDETAPE_DEBUG_LOG
3322 if (tape->debug_level >= 1)
3323 printk(KERN_INFO "ide-tape: Handling MTIOCTOP ioctl: "
3324 "mt_op=%d, mt_count=%d\n", mt_op, mt_count);
3325#endif /* IDETAPE_DEBUG_LOG */
3326 /*
3327 * Commands which need our pipelined read-ahead stages.
3328 */
3329 switch (mt_op) {
3330 case MTFSF:
3331 case MTFSFM:
3332 case MTBSF:
3333 case MTBSFM:
3334 if (!mt_count)
3335 return (0);
3336 return (idetape_space_over_filemarks(drive,mt_op,mt_count));
3337 default:
3338 break;
3339 }
3340 switch (mt_op) {
3341 case MTWEOF:
3342 if (tape->write_prot)
3343 return -EACCES;
3344 idetape_discard_read_pipeline(drive, 1);
3345 for (i = 0; i < mt_count; i++) {
3346 retval = idetape_write_filemark(drive);
3347 if (retval)
3348 return retval;
3349 }
3350 return (0);
3351 case MTREW:
3352 idetape_discard_read_pipeline(drive, 0);
3353 if (idetape_rewind_tape(drive))
3354 return -EIO;
3355 return 0;
3356 case MTLOAD:
3357 idetape_discard_read_pipeline(drive, 0);
3358 idetape_create_load_unload_cmd(drive, &pc, IDETAPE_LU_LOAD_MASK);
3359 return (idetape_queue_pc_tail(drive, &pc));
3360 case MTUNLOAD:
3361 case MTOFFL:
3362 /*
3363 * If door is locked, attempt to unlock before
3364 * attempting to eject.
3365 */
3366 if (tape->door_locked) {
3367 if (idetape_create_prevent_cmd(drive, &pc, 0))
3368 if (!idetape_queue_pc_tail(drive, &pc))
3369 tape->door_locked = DOOR_UNLOCKED;
3370 }
3371 idetape_discard_read_pipeline(drive, 0);
3372 idetape_create_load_unload_cmd(drive, &pc,!IDETAPE_LU_LOAD_MASK);
3373 retval = idetape_queue_pc_tail(drive, &pc);
3374 if (!retval)
3375 clear_bit(IDETAPE_MEDIUM_PRESENT, &tape->flags);
3376 return retval;
3377 case MTNOP:
3378 idetape_discard_read_pipeline(drive, 0);
3379 return (idetape_flush_tape_buffers(drive));
3380 case MTRETEN:
3381 idetape_discard_read_pipeline(drive, 0);
3382 idetape_create_load_unload_cmd(drive, &pc,IDETAPE_LU_RETENSION_MASK | IDETAPE_LU_LOAD_MASK);
3383 return (idetape_queue_pc_tail(drive, &pc));
3384 case MTEOM:
3385 idetape_create_space_cmd(&pc, 0, IDETAPE_SPACE_TO_EOD);
3386 return (idetape_queue_pc_tail(drive, &pc));
3387 case MTERASE:
3388 (void) idetape_rewind_tape(drive);
3389 idetape_create_erase_cmd(&pc);
3390 return (idetape_queue_pc_tail(drive, &pc));
3391 case MTSETBLK:
3392 if (mt_count) {
3393 if (mt_count < tape->tape_block_size || mt_count % tape->tape_block_size)
3394 return -EIO;
3395 tape->user_bs_factor = mt_count / tape->tape_block_size;
3396 clear_bit(IDETAPE_DETECT_BS, &tape->flags);
3397 } else
3398 set_bit(IDETAPE_DETECT_BS, &tape->flags);
3399 return 0;
3400 case MTSEEK:
3401 idetape_discard_read_pipeline(drive, 0);
3402 return idetape_position_tape(drive, mt_count * tape->user_bs_factor, tape->partition, 0);
3403 case MTSETPART:
3404 idetape_discard_read_pipeline(drive, 0);
3405 return (idetape_position_tape(drive, 0, mt_count, 0));
3406 case MTFSR:
3407 case MTBSR:
3408 case MTLOCK:
3409 if (!idetape_create_prevent_cmd(drive, &pc, 1))
3410 return 0;
3411 retval = idetape_queue_pc_tail(drive, &pc);
3412 if (retval) return retval;
3413 tape->door_locked = DOOR_EXPLICITLY_LOCKED;
3414 return 0;
3415 case MTUNLOCK:
3416 if (!idetape_create_prevent_cmd(drive, &pc, 0))
3417 return 0;
3418 retval = idetape_queue_pc_tail(drive, &pc);
3419 if (retval) return retval;
3420 tape->door_locked = DOOR_UNLOCKED;
3421 return 0;
3422 default:
3423 printk(KERN_ERR "ide-tape: MTIO operation %d not "
3424 "supported\n", mt_op);
3425 return (-EIO);
3426 }
3427}
3428
3429/*
3430 * Our character device ioctls.
3431 *
3432 * General mtio.h magnetic io commands are supported here, and not in
3433 * the corresponding block interface.
3434 *
3435 * The following ioctls are supported:
3436 *
3437 * MTIOCTOP - Refer to idetape_mtioctop for detailed description.
3438 *
3439 * MTIOCGET - The mt_dsreg field in the returned mtget structure
3440 * will be set to (user block size in bytes <<
3441 * MT_ST_BLKSIZE_SHIFT) & MT_ST_BLKSIZE_MASK.
3442 *
3443 * The mt_blkno is set to the current user block number.
3444 * The other mtget fields are not supported.
3445 *
3446 * MTIOCPOS - The current tape "block position" is returned. We
3447 * assume that each block contains user_block_size
3448 * bytes.
3449 *
3450 * Our own ide-tape ioctls are supported on both interfaces.
3451 */
3452static int idetape_chrdev_ioctl (struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg)
3453{
3454 struct ide_tape_obj *tape = ide_tape_f(file);
3455 ide_drive_t *drive = tape->drive;
3456 struct mtop mtop;
3457 struct mtget mtget;
3458 struct mtpos mtpos;
3459 int block_offset = 0, position = tape->first_frame_position;
3460 void __user *argp = (void __user *)arg;
3461
3462#if IDETAPE_DEBUG_LOG
3463 if (tape->debug_level >= 3)
3464 printk(KERN_INFO "ide-tape: Reached idetape_chrdev_ioctl, "
3465 "cmd=%u\n", cmd);
3466#endif /* IDETAPE_DEBUG_LOG */
3467
3468 tape->restart_speed_control_req = 1;
3469 if (tape->chrdev_direction == idetape_direction_write) {
3470 idetape_empty_write_pipeline(drive);
3471 idetape_flush_tape_buffers(drive);
3472 }
3473 if (cmd == MTIOCGET || cmd == MTIOCPOS) {
3474 block_offset = idetape_pipeline_size(drive) / (tape->tape_block_size * tape->user_bs_factor);
3475 if ((position = idetape_read_position(drive)) < 0)
3476 return -EIO;
3477 }
3478 switch (cmd) {
3479 case MTIOCTOP:
3480 if (copy_from_user(&mtop, argp, sizeof (struct mtop)))
3481 return -EFAULT;
3482 return (idetape_mtioctop(drive,mtop.mt_op,mtop.mt_count));
3483 case MTIOCGET:
3484 memset(&mtget, 0, sizeof (struct mtget));
3485 mtget.mt_type = MT_ISSCSI2;
3486 mtget.mt_blkno = position / tape->user_bs_factor - block_offset;
3487 mtget.mt_dsreg = ((tape->tape_block_size * tape->user_bs_factor) << MT_ST_BLKSIZE_SHIFT) & MT_ST_BLKSIZE_MASK;
3488 if (tape->drv_write_prot) {
3489 mtget.mt_gstat |= GMT_WR_PROT(0xffffffff);
3490 }
3491 if (copy_to_user(argp, &mtget, sizeof(struct mtget)))
3492 return -EFAULT;
3493 return 0;
3494 case MTIOCPOS:
3495 mtpos.mt_blkno = position / tape->user_bs_factor - block_offset;
3496 if (copy_to_user(argp, &mtpos, sizeof(struct mtpos)))
3497 return -EFAULT;
3498 return 0;
3499 default:
3500 if (tape->chrdev_direction == idetape_direction_read)
3501 idetape_discard_read_pipeline(drive, 1);
3502 return idetape_blkdev_ioctl(drive, cmd, arg);
3503 }
3504}
3505
3506static void idetape_get_blocksize_from_block_descriptor(ide_drive_t *drive);
3507
3508/*
3509 * Our character device open function.
3510 */
3511static int idetape_chrdev_open (struct inode *inode, struct file *filp)
3512{
3513 unsigned int minor = iminor(inode), i = minor & ~0xc0;
3514 ide_drive_t *drive;
3515 idetape_tape_t *tape;
3516 idetape_pc_t pc;
3517 int retval;
3518
3519 /*
3520 * We really want to do nonseekable_open(inode, filp); here, but some
3521 * versions of tar incorrectly call lseek on tapes and bail out if that
3522 * fails. So we disallow pread() and pwrite(), but permit lseeks.
3523 */
3524 filp->f_mode &= ~(FMODE_PREAD | FMODE_PWRITE);
3525
3526#if IDETAPE_DEBUG_LOG
3527 printk(KERN_INFO "ide-tape: Reached idetape_chrdev_open\n");
3528#endif /* IDETAPE_DEBUG_LOG */
3529
3530 if (i >= MAX_HWIFS * MAX_DRIVES)
3531 return -ENXIO;
3532
3533 if (!(tape = ide_tape_chrdev_get(i)))
3534 return -ENXIO;
3535
3536 drive = tape->drive;
3537
3538 filp->private_data = tape;
3539
3540 if (test_and_set_bit(IDETAPE_BUSY, &tape->flags)) {
3541 retval = -EBUSY;
3542 goto out_put_tape;
3543 }
3544
3545 retval = idetape_wait_ready(drive, 60 * HZ);
3546 if (retval) {
3547 clear_bit(IDETAPE_BUSY, &tape->flags);
3548 printk(KERN_ERR "ide-tape: %s: drive not ready\n", tape->name);
3549 goto out_put_tape;
3550 }
3551
3552 idetape_read_position(drive);
3553 if (!test_bit(IDETAPE_ADDRESS_VALID, &tape->flags))
3554 (void)idetape_rewind_tape(drive);
3555
3556 if (tape->chrdev_direction != idetape_direction_read)
3557 clear_bit(IDETAPE_PIPELINE_ERROR, &tape->flags);
3558
3559 /* Read block size and write protect status from drive. */
3560 idetape_get_blocksize_from_block_descriptor(drive);
3561
3562 /* Set write protect flag if device is opened as read-only. */
3563 if ((filp->f_flags & O_ACCMODE) == O_RDONLY)
3564 tape->write_prot = 1;
3565 else
3566 tape->write_prot = tape->drv_write_prot;
3567
3568 /* Make sure drive isn't write protected if user wants to write. */
3569 if (tape->write_prot) {
3570 if ((filp->f_flags & O_ACCMODE) == O_WRONLY ||
3571 (filp->f_flags & O_ACCMODE) == O_RDWR) {
3572 clear_bit(IDETAPE_BUSY, &tape->flags);
3573 retval = -EROFS;
3574 goto out_put_tape;
3575 }
3576 }
3577
3578 /*
3579 * Lock the tape drive door so user can't eject.
3580 */
3581 if (tape->chrdev_direction == idetape_direction_none) {
3582 if (idetape_create_prevent_cmd(drive, &pc, 1)) {
3583 if (!idetape_queue_pc_tail(drive, &pc)) {
3584 if (tape->door_locked != DOOR_EXPLICITLY_LOCKED)
3585 tape->door_locked = DOOR_LOCKED;
3586 }
3587 }
3588 }
3589 idetape_restart_speed_control(drive);
3590 tape->restart_speed_control_req = 0;
3591 return 0;
3592
3593out_put_tape:
3594 ide_tape_put(tape);
3595 return retval;
3596}
3597
3598static void idetape_write_release (ide_drive_t *drive, unsigned int minor)
3599{
3600 idetape_tape_t *tape = drive->driver_data;
3601
3602 idetape_empty_write_pipeline(drive);
3603 tape->merge_stage = __idetape_kmalloc_stage(tape, 1, 0);
3604 if (tape->merge_stage != NULL) {
3605 idetape_pad_zeros(drive, tape->tape_block_size * (tape->user_bs_factor - 1));
3606 __idetape_kfree_stage(tape->merge_stage);
3607 tape->merge_stage = NULL;
3608 }
3609 idetape_write_filemark(drive);
3610 idetape_flush_tape_buffers(drive);
3611 idetape_flush_tape_buffers(drive);
3612}
3613
3614/*
3615 * Our character device release function.
3616 */
3617static int idetape_chrdev_release (struct inode *inode, struct file *filp)
3618{
3619 struct ide_tape_obj *tape = ide_tape_f(filp);
3620 ide_drive_t *drive = tape->drive;
3621 idetape_pc_t pc;
3622 unsigned int minor = iminor(inode);
3623
3624 lock_kernel();
3625 tape = drive->driver_data;
3626#if IDETAPE_DEBUG_LOG
3627 if (tape->debug_level >= 3)
3628 printk(KERN_INFO "ide-tape: Reached idetape_chrdev_release\n");
3629#endif /* IDETAPE_DEBUG_LOG */
3630
3631 if (tape->chrdev_direction == idetape_direction_write)
3632 idetape_write_release(drive, minor);
3633 if (tape->chrdev_direction == idetape_direction_read) {
3634 if (minor < 128)
3635 idetape_discard_read_pipeline(drive, 1);
3636 else
3637 idetape_wait_for_pipeline(drive);
3638 }
3639 if (tape->cache_stage != NULL) {
3640 __idetape_kfree_stage(tape->cache_stage);
3641 tape->cache_stage = NULL;
3642 }
3643 if (minor < 128 && test_bit(IDETAPE_MEDIUM_PRESENT, &tape->flags))
3644 (void) idetape_rewind_tape(drive);
3645 if (tape->chrdev_direction == idetape_direction_none) {
3646 if (tape->door_locked == DOOR_LOCKED) {
3647 if (idetape_create_prevent_cmd(drive, &pc, 0)) {
3648 if (!idetape_queue_pc_tail(drive, &pc))
3649 tape->door_locked = DOOR_UNLOCKED;
3650 }
3651 }
3652 }
3653 clear_bit(IDETAPE_BUSY, &tape->flags);
3654 ide_tape_put(tape);
3655 unlock_kernel();
3656 return 0;
3657}
3658
3659/*
3660 * idetape_identify_device is called to check the contents of the
3661 * ATAPI IDENTIFY command results. We return:
3662 *
3663 * 1 If the tape can be supported by us, based on the information
3664 * we have so far.
3665 *
3666 * 0 If this tape driver is not currently supported by us.
3667 */
3668static int idetape_identify_device (ide_drive_t *drive)
3669{
3670 struct idetape_id_gcw gcw;
3671 struct hd_driveid *id = drive->id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003672
3673 if (drive->id_read == 0)
3674 return 1;
3675
3676 *((unsigned short *) &gcw) = id->config;
3677
Linus Torvalds1da177e2005-04-16 15:20:36 -07003678 /* Check that we can support this device */
3679
Bartlomiej Zolnierkiewicz16422de32008-02-02 19:56:48 +01003680 if (gcw.protocol != 2)
3681 printk(KERN_ERR "ide-tape: Protocol (0x%02x) is not ATAPI\n",
3682 gcw.protocol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003683 else if (gcw.device_type != 1)
Bartlomiej Zolnierkiewicz16422de32008-02-02 19:56:48 +01003684 printk(KERN_ERR "ide-tape: Device type (0x%02x) is not set "
3685 "to tape\n", gcw.device_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003686 else if (!gcw.removable)
3687 printk(KERN_ERR "ide-tape: The removable flag is not set\n");
3688 else if (gcw.packet_size != 0) {
Bartlomiej Zolnierkiewicz16422de32008-02-02 19:56:48 +01003689 printk(KERN_ERR "ide-tape: Packet size (0x%02x) is not 12 "
3690 "bytes long\n", gcw.packet_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003691 } else
3692 return 1;
3693 return 0;
3694}
3695
Borislav Petkov6d29c8f2008-02-02 19:56:49 +01003696static void idetape_get_inquiry_results(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003697{
3698 char *r;
3699 idetape_tape_t *tape = drive->driver_data;
3700 idetape_pc_t pc;
Borislav Petkov6d29c8f2008-02-02 19:56:49 +01003701
Linus Torvalds1da177e2005-04-16 15:20:36 -07003702 idetape_create_inquiry_cmd(&pc);
3703 if (idetape_queue_pc_tail(drive, &pc)) {
Borislav Petkov6d29c8f2008-02-02 19:56:49 +01003704 printk(KERN_ERR "ide-tape: %s: can't get INQUIRY results\n",
3705 tape->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003706 return;
3707 }
Borislav Petkov6d29c8f2008-02-02 19:56:49 +01003708 memcpy(tape->vendor_id, &pc.buffer[8], 8);
3709 memcpy(tape->product_id, &pc.buffer[16], 16);
3710 memcpy(tape->firmware_revision, &pc.buffer[32], 4);
3711
Linus Torvalds1da177e2005-04-16 15:20:36 -07003712 ide_fixstring(tape->vendor_id, 10, 0);
3713 ide_fixstring(tape->product_id, 18, 0);
3714 ide_fixstring(tape->firmware_revision, 6, 0);
3715 r = tape->firmware_revision;
3716 if (*(r + 1) == '.')
Borislav Petkov6d29c8f2008-02-02 19:56:49 +01003717 tape->firmware_revision_num = (*r - '0') * 100 +
3718 (*(r + 2) - '0') * 10 + *(r + 3) - '0';
3719 printk(KERN_INFO "ide-tape: %s <-> %s: %s %s rev %s\n",
3720 drive->name, tape->name, tape->vendor_id,
3721 tape->product_id, tape->firmware_revision);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003722}
3723
3724/*
Borislav Petkovb6422012008-02-02 19:56:49 +01003725 * Ask the tape about its various parameters. In particular, we will adjust our
3726 * data transfer buffer size to the recommended value as returned by the tape.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003727 */
3728static void idetape_get_mode_sense_results (ide_drive_t *drive)
3729{
3730 idetape_tape_t *tape = drive->driver_data;
3731 idetape_pc_t pc;
Borislav Petkovb6422012008-02-02 19:56:49 +01003732 u8 *caps;
3733 u8 speed, max_speed;
Borislav Petkov47314fa2008-02-02 19:56:48 +01003734
Linus Torvalds1da177e2005-04-16 15:20:36 -07003735 idetape_create_mode_sense_cmd(&pc, IDETAPE_CAPABILITIES_PAGE);
3736 if (idetape_queue_pc_tail(drive, &pc)) {
Borislav Petkovb6422012008-02-02 19:56:49 +01003737 printk(KERN_ERR "ide-tape: Can't get tape parameters - assuming"
3738 " some default values\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003739 tape->tape_block_size = 512;
Borislav Petkovb6422012008-02-02 19:56:49 +01003740 put_unaligned(52, (u16 *)&tape->caps[12]);
3741 put_unaligned(540, (u16 *)&tape->caps[14]);
3742 put_unaligned(6*52, (u16 *)&tape->caps[16]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003743 return;
3744 }
Borislav Petkovb6422012008-02-02 19:56:49 +01003745 caps = pc.buffer + 4 + pc.buffer[3];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003746
Borislav Petkovb6422012008-02-02 19:56:49 +01003747 /* convert to host order and save for later use */
3748 speed = be16_to_cpu(*(u16 *)&caps[14]);
3749 max_speed = be16_to_cpu(*(u16 *)&caps[8]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003750
Borislav Petkovb6422012008-02-02 19:56:49 +01003751 put_unaligned(max_speed, (u16 *)&caps[8]);
3752 put_unaligned(be16_to_cpu(*(u16 *)&caps[12]), (u16 *)&caps[12]);
3753 put_unaligned(speed, (u16 *)&caps[14]);
3754 put_unaligned(be16_to_cpu(*(u16 *)&caps[16]), (u16 *)&caps[16]);
3755
3756 if (!speed) {
3757 printk(KERN_INFO "ide-tape: %s: invalid tape speed "
3758 "(assuming 650KB/sec)\n", drive->name);
3759 put_unaligned(650, (u16 *)&caps[14]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003760 }
Borislav Petkovb6422012008-02-02 19:56:49 +01003761 if (!max_speed) {
3762 printk(KERN_INFO "ide-tape: %s: invalid max_speed "
3763 "(assuming 650KB/sec)\n", drive->name);
3764 put_unaligned(650, (u16 *)&caps[8]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003765 }
3766
Borislav Petkovb6422012008-02-02 19:56:49 +01003767 memcpy(&tape->caps, caps, 20);
3768 if (caps[7] & 0x02)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003769 tape->tape_block_size = 512;
Borislav Petkovb6422012008-02-02 19:56:49 +01003770 else if (caps[7] & 0x04)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003771 tape->tape_block_size = 1024;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003772}
3773
3774/*
3775 * ide_get_blocksize_from_block_descriptor does a mode sense page 0 with block descriptor
3776 * and if it succeeds sets the tape block size with the reported value
3777 */
3778static void idetape_get_blocksize_from_block_descriptor(ide_drive_t *drive)
3779{
3780
3781 idetape_tape_t *tape = drive->driver_data;
3782 idetape_pc_t pc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003783 idetape_parameter_block_descriptor_t *block_descrp;
Borislav Petkov47314fa2008-02-02 19:56:48 +01003784
Linus Torvalds1da177e2005-04-16 15:20:36 -07003785 idetape_create_mode_sense_cmd(&pc, IDETAPE_BLOCK_DESCRIPTOR);
3786 if (idetape_queue_pc_tail(drive, &pc)) {
3787 printk(KERN_ERR "ide-tape: Can't get block descriptor\n");
3788 if (tape->tape_block_size == 0) {
3789 printk(KERN_WARNING "ide-tape: Cannot deal with zero block size, assume 32k\n");
3790 tape->tape_block_size = 32768;
3791 }
3792 return;
3793 }
Borislav Petkov47314fa2008-02-02 19:56:48 +01003794 block_descrp = (idetape_parameter_block_descriptor_t *)(pc.buffer + 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003795 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 +01003796 tape->drv_write_prot = (pc.buffer[2] & 0x80) >> 7;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003797}
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +02003798
3799#ifdef CONFIG_IDE_PROC_FS
Linus Torvalds1da177e2005-04-16 15:20:36 -07003800static void idetape_add_settings (ide_drive_t *drive)
3801{
3802 idetape_tape_t *tape = drive->driver_data;
3803
3804/*
Bartlomiej Zolnierkiewicz14979432007-05-10 00:01:10 +02003805 * drive setting name read/write data type min max mul_factor div_factor data pointer set function
Linus Torvalds1da177e2005-04-16 15:20:36 -07003806 */
Borislav Petkovb6422012008-02-02 19:56:49 +01003807 ide_add_setting(drive, "buffer", SETTING_READ, TYPE_SHORT, 0, 0xffff,
3808 1, 2, (u16 *)&tape->caps[16], NULL);
Bartlomiej Zolnierkiewicz14979432007-05-10 00:01:10 +02003809 ide_add_setting(drive, "pipeline_min", SETTING_RW, TYPE_INT, 1, 0xffff, tape->stage_size / 1024, 1, &tape->min_pipeline, NULL);
3810 ide_add_setting(drive, "pipeline", SETTING_RW, TYPE_INT, 1, 0xffff, tape->stage_size / 1024, 1, &tape->max_stages, NULL);
3811 ide_add_setting(drive, "pipeline_max", SETTING_RW, TYPE_INT, 1, 0xffff, tape->stage_size / 1024, 1, &tape->max_pipeline, NULL);
3812 ide_add_setting(drive, "pipeline_used", SETTING_READ, TYPE_INT, 0, 0xffff, tape->stage_size / 1024, 1, &tape->nr_stages, NULL);
3813 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 +01003814 ide_add_setting(drive, "speed", SETTING_READ, TYPE_SHORT, 0, 0xffff,
3815 1, 1, (u16 *)&tape->caps[14], NULL);
Bartlomiej Zolnierkiewicz14979432007-05-10 00:01:10 +02003816 ide_add_setting(drive, "stage", SETTING_READ, TYPE_INT, 0, 0xffff, 1, 1024, &tape->stage_size, NULL);
3817 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);
3818 ide_add_setting(drive, "dsc_overlap", SETTING_RW, TYPE_BYTE, 0, 1, 1, 1, &drive->dsc_overlap, NULL);
3819 ide_add_setting(drive, "pipeline_head_speed_c",SETTING_READ, TYPE_INT, 0, 0xffff, 1, 1, &tape->controlled_pipeline_head_speed, NULL);
3820 ide_add_setting(drive, "pipeline_head_speed_u",SETTING_READ, TYPE_INT, 0, 0xffff, 1, 1, &tape->uncontrolled_pipeline_head_speed,NULL);
3821 ide_add_setting(drive, "avg_speed", SETTING_READ, TYPE_INT, 0, 0xffff, 1, 1, &tape->avg_speed, NULL);
3822 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 -07003823}
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +02003824#else
3825static inline void idetape_add_settings(ide_drive_t *drive) { ; }
3826#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003827
3828/*
3829 * ide_setup is called to:
3830 *
3831 * 1. Initialize our various state variables.
3832 * 2. Ask the tape for its capabilities.
3833 * 3. Allocate a buffer which will be used for data
3834 * transfer. The buffer size is chosen based on
3835 * the recommendation which we received in step (2).
3836 *
3837 * Note that at this point ide.c already assigned us an irq, so that
3838 * we can queue requests here and wait for their completion.
3839 */
3840static void idetape_setup (ide_drive_t *drive, idetape_tape_t *tape, int minor)
3841{
3842 unsigned long t1, tmid, tn, t;
3843 int speed;
3844 struct idetape_id_gcw gcw;
3845 int stage_size;
3846 struct sysinfo si;
Borislav Petkovb6422012008-02-02 19:56:49 +01003847 u16 *ctl = (u16 *)&tape->caps[12];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003848
3849 spin_lock_init(&tape->spinlock);
3850 drive->dsc_overlap = 1;
Bartlomiej Zolnierkiewicz4166c192008-02-01 23:09:30 +01003851 if (drive->hwif->host_flags & IDE_HFLAG_NO_DSC) {
3852 printk(KERN_INFO "ide-tape: %s: disabling DSC overlap\n",
3853 tape->name);
3854 drive->dsc_overlap = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003855 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003856 /* Seagate Travan drives do not support DSC overlap. */
3857 if (strstr(drive->id->model, "Seagate STT3401"))
3858 drive->dsc_overlap = 0;
3859 tape->minor = minor;
3860 tape->name[0] = 'h';
3861 tape->name[1] = 't';
3862 tape->name[2] = '0' + minor;
3863 tape->chrdev_direction = idetape_direction_none;
3864 tape->pc = tape->pc_stack;
3865 tape->max_insert_speed = 10000;
3866 tape->speed_control = 1;
3867 *((unsigned short *) &gcw) = drive->id->config;
3868 if (gcw.drq_type == 1)
3869 set_bit(IDETAPE_DRQ_INTERRUPT, &tape->flags);
3870
3871 tape->min_pipeline = tape->max_pipeline = tape->max_stages = 10;
3872
3873 idetape_get_inquiry_results(drive);
3874 idetape_get_mode_sense_results(drive);
3875 idetape_get_blocksize_from_block_descriptor(drive);
3876 tape->user_bs_factor = 1;
Borislav Petkovb6422012008-02-02 19:56:49 +01003877 tape->stage_size = *ctl * tape->tape_block_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003878 while (tape->stage_size > 0xffff) {
3879 printk(KERN_NOTICE "ide-tape: decreasing stage size\n");
Borislav Petkovb6422012008-02-02 19:56:49 +01003880 *ctl /= 2;
3881 tape->stage_size = *ctl * tape->tape_block_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003882 }
3883 stage_size = tape->stage_size;
3884 tape->pages_per_stage = stage_size / PAGE_SIZE;
3885 if (stage_size % PAGE_SIZE) {
3886 tape->pages_per_stage++;
3887 tape->excess_bh_size = PAGE_SIZE - stage_size % PAGE_SIZE;
3888 }
3889
Borislav Petkovb6422012008-02-02 19:56:49 +01003890 /* Select the "best" DSC read/write polling freq and pipeline size. */
3891 speed = max(*(u16 *)&tape->caps[14], *(u16 *)&tape->caps[8]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003892
3893 tape->max_stages = speed * 1000 * 10 / tape->stage_size;
3894
3895 /*
3896 * Limit memory use for pipeline to 10% of physical memory
3897 */
3898 si_meminfo(&si);
3899 if (tape->max_stages * tape->stage_size > si.totalram * si.mem_unit / 10)
3900 tape->max_stages = si.totalram * si.mem_unit / (10 * tape->stage_size);
3901 tape->max_stages = min(tape->max_stages, IDETAPE_MAX_PIPELINE_STAGES);
3902 tape->min_pipeline = min(tape->max_stages, IDETAPE_MIN_PIPELINE_STAGES);
3903 tape->max_pipeline = min(tape->max_stages * 2, IDETAPE_MAX_PIPELINE_STAGES);
3904 if (tape->max_stages == 0)
3905 tape->max_stages = tape->min_pipeline = tape->max_pipeline = 1;
3906
3907 t1 = (tape->stage_size * HZ) / (speed * 1000);
Borislav Petkovb6422012008-02-02 19:56:49 +01003908 tmid = (*(u16 *)&tape->caps[16] * 32 * HZ) / (speed * 125);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003909 tn = (IDETAPE_FIFO_THRESHOLD * tape->stage_size * HZ) / (speed * 1000);
3910
3911 if (tape->max_stages)
3912 t = tn;
3913 else
3914 t = t1;
3915
3916 /*
3917 * Ensure that the number we got makes sense; limit
3918 * it within IDETAPE_DSC_RW_MIN and IDETAPE_DSC_RW_MAX.
3919 */
3920 tape->best_dsc_rw_frequency = max_t(unsigned long, min_t(unsigned long, t, IDETAPE_DSC_RW_MAX), IDETAPE_DSC_RW_MIN);
3921 printk(KERN_INFO "ide-tape: %s <-> %s: %dKBps, %d*%dkB buffer, "
3922 "%dkB pipeline, %lums tDSC%s\n",
Borislav Petkovb6422012008-02-02 19:56:49 +01003923 drive->name, tape->name, *(u16 *)&tape->caps[14],
3924 (*(u16 *)&tape->caps[16] * 512) / tape->stage_size,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003925 tape->stage_size / 1024,
3926 tape->max_stages * tape->stage_size / 1024,
3927 tape->best_dsc_rw_frequency * 1000 / HZ,
3928 drive->using_dma ? ", DMA":"");
3929
3930 idetape_add_settings(drive);
3931}
3932
Russell King4031bbe2006-01-06 11:41:00 +00003933static void ide_tape_remove(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003934{
3935 idetape_tape_t *tape = drive->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003936
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +02003937 ide_proc_unregister_driver(drive, tape->driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003938
3939 ide_unregister_region(tape->disk);
3940
3941 ide_tape_put(tape);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003942}
3943
3944static void ide_tape_release(struct kref *kref)
3945{
3946 struct ide_tape_obj *tape = to_ide_tape(kref);
3947 ide_drive_t *drive = tape->drive;
3948 struct gendisk *g = tape->disk;
3949
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02003950 BUG_ON(tape->first_stage != NULL || tape->merge_stage_size);
3951
Linus Torvalds1da177e2005-04-16 15:20:36 -07003952 drive->dsc_overlap = 0;
3953 drive->driver_data = NULL;
Tony Jonesdbc12722007-09-25 02:03:03 +02003954 device_destroy(idetape_sysfs_class, MKDEV(IDETAPE_MAJOR, tape->minor));
3955 device_destroy(idetape_sysfs_class, MKDEV(IDETAPE_MAJOR, tape->minor + 128));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003956 idetape_devs[tape->minor] = NULL;
3957 g->private_data = NULL;
3958 put_disk(g);
3959 kfree(tape);
3960}
3961
Bartlomiej Zolnierkiewiczecfd80e2007-05-10 00:01:09 +02003962#ifdef CONFIG_IDE_PROC_FS
Linus Torvalds1da177e2005-04-16 15:20:36 -07003963static int proc_idetape_read_name
3964 (char *page, char **start, off_t off, int count, int *eof, void *data)
3965{
3966 ide_drive_t *drive = (ide_drive_t *) data;
3967 idetape_tape_t *tape = drive->driver_data;
3968 char *out = page;
3969 int len;
3970
3971 len = sprintf(out, "%s\n", tape->name);
3972 PROC_IDE_READ_RETURN(page, start, off, count, eof, len);
3973}
3974
3975static ide_proc_entry_t idetape_proc[] = {
3976 { "capacity", S_IFREG|S_IRUGO, proc_ide_read_capacity, NULL },
3977 { "name", S_IFREG|S_IRUGO, proc_idetape_read_name, NULL },
3978 { NULL, 0, NULL, NULL }
3979};
Linus Torvalds1da177e2005-04-16 15:20:36 -07003980#endif
3981
Russell King4031bbe2006-01-06 11:41:00 +00003982static int ide_tape_probe(ide_drive_t *);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003983
Linus Torvalds1da177e2005-04-16 15:20:36 -07003984static ide_driver_t idetape_driver = {
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02003985 .gen_driver = {
Laurent Riffard4ef3b8f2005-11-18 22:15:40 +01003986 .owner = THIS_MODULE,
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02003987 .name = "ide-tape",
3988 .bus = &ide_bus_type,
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02003989 },
Russell King4031bbe2006-01-06 11:41:00 +00003990 .probe = ide_tape_probe,
3991 .remove = ide_tape_remove,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003992 .version = IDETAPE_VERSION,
3993 .media = ide_tape,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003994 .supports_dsc_overlap = 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003995 .do_request = idetape_do_request,
3996 .end_request = idetape_end_request,
3997 .error = __ide_error,
3998 .abort = __ide_abort,
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +02003999#ifdef CONFIG_IDE_PROC_FS
Linus Torvalds1da177e2005-04-16 15:20:36 -07004000 .proc = idetape_proc,
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +02004001#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07004002};
4003
4004/*
4005 * Our character device supporting functions, passed to register_chrdev.
4006 */
Arjan van de Ven2b8693c2007-02-12 00:55:32 -08004007static const struct file_operations idetape_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004008 .owner = THIS_MODULE,
4009 .read = idetape_chrdev_read,
4010 .write = idetape_chrdev_write,
4011 .ioctl = idetape_chrdev_ioctl,
4012 .open = idetape_chrdev_open,
4013 .release = idetape_chrdev_release,
4014};
4015
4016static int idetape_open(struct inode *inode, struct file *filp)
4017{
4018 struct gendisk *disk = inode->i_bdev->bd_disk;
4019 struct ide_tape_obj *tape;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004020
4021 if (!(tape = ide_tape_get(disk)))
4022 return -ENXIO;
4023
Linus Torvalds1da177e2005-04-16 15:20:36 -07004024 return 0;
4025}
4026
4027static int idetape_release(struct inode *inode, struct file *filp)
4028{
4029 struct gendisk *disk = inode->i_bdev->bd_disk;
4030 struct ide_tape_obj *tape = ide_tape_g(disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004031
4032 ide_tape_put(tape);
4033
4034 return 0;
4035}
4036
4037static int idetape_ioctl(struct inode *inode, struct file *file,
4038 unsigned int cmd, unsigned long arg)
4039{
4040 struct block_device *bdev = inode->i_bdev;
4041 struct ide_tape_obj *tape = ide_tape_g(bdev->bd_disk);
4042 ide_drive_t *drive = tape->drive;
4043 int err = generic_ide_ioctl(drive, file, bdev, cmd, arg);
4044 if (err == -EINVAL)
4045 err = idetape_blkdev_ioctl(drive, cmd, arg);
4046 return err;
4047}
4048
4049static struct block_device_operations idetape_block_ops = {
4050 .owner = THIS_MODULE,
4051 .open = idetape_open,
4052 .release = idetape_release,
4053 .ioctl = idetape_ioctl,
4054};
4055
Russell King4031bbe2006-01-06 11:41:00 +00004056static int ide_tape_probe(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004057{
4058 idetape_tape_t *tape;
4059 struct gendisk *g;
4060 int minor;
4061
4062 if (!strstr("ide-tape", drive->driver_req))
4063 goto failed;
4064 if (!drive->present)
4065 goto failed;
4066 if (drive->media != ide_tape)
4067 goto failed;
4068 if (!idetape_identify_device (drive)) {
4069 printk(KERN_ERR "ide-tape: %s: not supported by this version of ide-tape\n", drive->name);
4070 goto failed;
4071 }
4072 if (drive->scsi) {
4073 printk("ide-tape: passing drive %s to ide-scsi emulation.\n", drive->name);
4074 goto failed;
4075 }
4076 if (strstr(drive->id->model, "OnStream DI-")) {
4077 printk(KERN_WARNING "ide-tape: Use drive %s with ide-scsi emulation and osst.\n", drive->name);
4078 printk(KERN_WARNING "ide-tape: OnStream support will be removed soon from ide-tape!\n");
4079 }
Robert P. J. Day5cbded52006-12-13 00:35:56 -08004080 tape = kzalloc(sizeof (idetape_tape_t), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004081 if (tape == NULL) {
4082 printk(KERN_ERR "ide-tape: %s: Can't allocate a tape structure\n", drive->name);
4083 goto failed;
4084 }
4085
4086 g = alloc_disk(1 << PARTN_BITS);
4087 if (!g)
4088 goto out_free_tape;
4089
4090 ide_init_disk(g, drive);
4091
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +02004092 ide_proc_register_driver(drive, &idetape_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004093
Linus Torvalds1da177e2005-04-16 15:20:36 -07004094 kref_init(&tape->kref);
4095
4096 tape->drive = drive;
4097 tape->driver = &idetape_driver;
4098 tape->disk = g;
4099
4100 g->private_data = &tape->driver;
4101
4102 drive->driver_data = tape;
4103
Arjan van de Vencf8b8972006-03-23 03:00:45 -08004104 mutex_lock(&idetape_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004105 for (minor = 0; idetape_devs[minor]; minor++)
4106 ;
4107 idetape_devs[minor] = tape;
Arjan van de Vencf8b8972006-03-23 03:00:45 -08004108 mutex_unlock(&idetape_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004109
4110 idetape_setup(drive, tape, minor);
4111
Tony Jonesdbc12722007-09-25 02:03:03 +02004112 device_create(idetape_sysfs_class, &drive->gendev,
4113 MKDEV(IDETAPE_MAJOR, minor), "%s", tape->name);
4114 device_create(idetape_sysfs_class, &drive->gendev,
4115 MKDEV(IDETAPE_MAJOR, minor + 128), "n%s", tape->name);
Will Dysond5dee802005-09-16 02:55:07 -07004116
Linus Torvalds1da177e2005-04-16 15:20:36 -07004117 g->fops = &idetape_block_ops;
4118 ide_register_region(g);
4119
4120 return 0;
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02004121
Linus Torvalds1da177e2005-04-16 15:20:36 -07004122out_free_tape:
4123 kfree(tape);
4124failed:
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02004125 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004126}
4127
4128MODULE_DESCRIPTION("ATAPI Streaming TAPE Driver");
4129MODULE_LICENSE("GPL");
4130
4131static void __exit idetape_exit (void)
4132{
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02004133 driver_unregister(&idetape_driver.gen_driver);
Will Dysond5dee802005-09-16 02:55:07 -07004134 class_destroy(idetape_sysfs_class);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004135 unregister_chrdev(IDETAPE_MAJOR, "ht");
4136}
4137
Bartlomiej Zolnierkiewicz17514e82005-11-19 22:24:35 +01004138static int __init idetape_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004139{
Will Dysond5dee802005-09-16 02:55:07 -07004140 int error = 1;
4141 idetape_sysfs_class = class_create(THIS_MODULE, "ide_tape");
4142 if (IS_ERR(idetape_sysfs_class)) {
4143 idetape_sysfs_class = NULL;
4144 printk(KERN_ERR "Unable to create sysfs class for ide tapes\n");
4145 error = -EBUSY;
4146 goto out;
4147 }
4148
Linus Torvalds1da177e2005-04-16 15:20:36 -07004149 if (register_chrdev(IDETAPE_MAJOR, "ht", &idetape_fops)) {
4150 printk(KERN_ERR "ide-tape: Failed to register character device interface\n");
Will Dysond5dee802005-09-16 02:55:07 -07004151 error = -EBUSY;
4152 goto out_free_class;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004153 }
Will Dysond5dee802005-09-16 02:55:07 -07004154
4155 error = driver_register(&idetape_driver.gen_driver);
4156 if (error)
4157 goto out_free_driver;
4158
4159 return 0;
4160
4161out_free_driver:
4162 driver_unregister(&idetape_driver.gen_driver);
4163out_free_class:
4164 class_destroy(idetape_sysfs_class);
4165out:
4166 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004167}
4168
Kay Sievers263756e2005-12-12 18:03:44 +01004169MODULE_ALIAS("ide:*m-tape*");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004170module_init(idetape_init);
4171module_exit(idetape_exit);
4172MODULE_ALIAS_CHARDEV_MAJOR(IDETAPE_MAJOR);