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