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