blob: ad13527ff44076cd78f0a1bcc9fd843eb2a1bd9e [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>
Borislav Petkov90699ce2008-02-02 19:56:50 +010039#include <scsi/scsi.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070040
41#include <asm/byteorder.h>
42#include <asm/irq.h>
43#include <asm/uaccess.h>
44#include <asm/io.h>
45#include <asm/unaligned.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070046#include <linux/mtio.h>
47
Borislav Petkov8004a8c2008-02-06 02:57:51 +010048enum {
49 /* output errors only */
50 DBG_ERR = (1 << 0),
51 /* output all sense key/asc */
52 DBG_SENSE = (1 << 1),
53 /* info regarding all chrdev-related procedures */
54 DBG_CHRDEV = (1 << 2),
55 /* all remaining procedures */
56 DBG_PROCS = (1 << 3),
57 /* buffer alloc info (pc_stack & rq_stack) */
58 DBG_PCRQ_STACK = (1 << 4),
59};
60
61/* define to see debug info */
62#define IDETAPE_DEBUG_LOG 0
63
64#if IDETAPE_DEBUG_LOG
65#define debug_log(lvl, fmt, args...) \
66{ \
67 if (tape->debug_mask & lvl) \
68 printk(KERN_INFO "ide-tape: " fmt, ## args); \
69}
70#else
71#define debug_log(lvl, fmt, args...) do {} while (0)
72#endif
73
Linus Torvalds1da177e2005-04-16 15:20:36 -070074/**************************** Tunable parameters *****************************/
75
76
77/*
78 * Pipelined mode parameters.
79 *
80 * We try to use the minimum number of stages which is enough to
81 * keep the tape constantly streaming. To accomplish that, we implement
82 * a feedback loop around the maximum number of stages:
83 *
84 * We start from MIN maximum stages (we will not even use MIN stages
85 * if we don't need them), increment it by RATE*(MAX-MIN)
86 * whenever we sense that the pipeline is empty, until we reach
87 * the optimum value or until we reach MAX.
88 *
89 * Setting the following parameter to 0 is illegal: the pipelined mode
Borislav Petkov37016ba2008-02-06 02:57:52 +010090 * cannot be disabled (idetape_calculate_speeds() divides by
91 * tape->max_stages.)
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 */
93#define IDETAPE_MIN_PIPELINE_STAGES 1
94#define IDETAPE_MAX_PIPELINE_STAGES 400
95#define IDETAPE_INCREASE_STAGES_RATE 20
96
97/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 * After each failed packet command we issue a request sense command
99 * and retry the packet command IDETAPE_MAX_PC_RETRIES times.
100 *
101 * Setting IDETAPE_MAX_PC_RETRIES to 0 will disable retries.
102 */
103#define IDETAPE_MAX_PC_RETRIES 3
104
105/*
106 * With each packet command, we allocate a buffer of
107 * IDETAPE_PC_BUFFER_SIZE bytes. This is used for several packet
108 * commands (Not for READ/WRITE commands).
109 */
110#define IDETAPE_PC_BUFFER_SIZE 256
111
112/*
113 * In various places in the driver, we need to allocate storage
114 * for packet commands and requests, which will remain valid while
115 * we leave the driver to wait for an interrupt or a timeout event.
116 */
117#define IDETAPE_PC_STACK (10 + IDETAPE_MAX_PC_RETRIES)
118
119/*
120 * Some drives (for example, Seagate STT3401A Travan) require a very long
121 * timeout, because they don't return an interrupt or clear their busy bit
122 * until after the command completes (even retension commands).
123 */
124#define IDETAPE_WAIT_CMD (900*HZ)
125
126/*
127 * The following parameter is used to select the point in the internal
128 * tape fifo in which we will start to refill the buffer. Decreasing
129 * the following parameter will improve the system's latency and
Robert P. J. Day3a4fa0a2007-10-19 23:10:43 +0200130 * interactive response, while using a high value might improve system
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131 * throughput.
132 */
133#define IDETAPE_FIFO_THRESHOLD 2
134
135/*
136 * DSC polling parameters.
137 *
138 * Polling for DSC (a single bit in the status register) is a very
139 * important function in ide-tape. There are two cases in which we
140 * poll for DSC:
141 *
142 * 1. Before a read/write packet command, to ensure that we
143 * can transfer data from/to the tape's data buffers, without
144 * causing an actual media access. In case the tape is not
145 * ready yet, we take out our request from the device
146 * request queue, so that ide.c will service requests from
147 * the other device on the same interface meanwhile.
148 *
149 * 2. After the successful initialization of a "media access
150 * packet command", which is a command which can take a long
151 * time to complete (it can be several seconds or even an hour).
152 *
153 * Again, we postpone our request in the middle to free the bus
154 * for the other device. The polling frequency here should be
155 * lower than the read/write frequency since those media access
156 * commands are slow. We start from a "fast" frequency -
157 * IDETAPE_DSC_MA_FAST (one second), and if we don't receive DSC
158 * after IDETAPE_DSC_MA_THRESHOLD (5 minutes), we switch it to a
159 * lower frequency - IDETAPE_DSC_MA_SLOW (1 minute).
160 *
161 * We also set a timeout for the timer, in case something goes wrong.
162 * The timeout should be longer then the maximum execution time of a
163 * tape operation.
164 */
165
166/*
167 * DSC timings.
168 */
169#define IDETAPE_DSC_RW_MIN 5*HZ/100 /* 50 msec */
170#define IDETAPE_DSC_RW_MAX 40*HZ/100 /* 400 msec */
171#define IDETAPE_DSC_RW_TIMEOUT 2*60*HZ /* 2 minutes */
172#define IDETAPE_DSC_MA_FAST 2*HZ /* 2 seconds */
173#define IDETAPE_DSC_MA_THRESHOLD 5*60*HZ /* 5 minutes */
174#define IDETAPE_DSC_MA_SLOW 30*HZ /* 30 seconds */
175#define IDETAPE_DSC_MA_TIMEOUT 2*60*60*HZ /* 2 hours */
176
177/*************************** End of tunable parameters ***********************/
178
179/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 * Read/Write error simulation
181 */
182#define SIMULATE_ERRORS 0
183
184/*
185 * For general magnetic tape device compatibility.
186 */
Borislav Petkov54abf372008-02-06 02:57:52 +0100187
188/* tape directions */
189enum {
190 IDETAPE_DIR_NONE = (1 << 0),
191 IDETAPE_DIR_READ = (1 << 1),
192 IDETAPE_DIR_WRITE = (1 << 2),
193};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194
195struct idetape_bh {
Stephen Rothwellab057962007-08-01 23:46:44 +0200196 u32 b_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 atomic_t b_count;
198 struct idetape_bh *b_reqnext;
199 char *b_data;
200};
201
202/*
203 * Our view of a packet command.
204 */
205typedef struct idetape_packet_command_s {
206 u8 c[12]; /* Actual packet bytes */
207 int retries; /* On each retry, we increment retries */
208 int error; /* Error code */
209 int request_transfer; /* Bytes to transfer */
210 int actually_transferred; /* Bytes actually transferred */
211 int buffer_size; /* Size of our data buffer */
212 struct idetape_bh *bh;
213 char *b_data;
214 int b_count;
215 u8 *buffer; /* Data buffer */
216 u8 *current_position; /* Pointer into the above buffer */
217 ide_startstop_t (*callback) (ide_drive_t *); /* Called when this packet command is completed */
218 u8 pc_buffer[IDETAPE_PC_BUFFER_SIZE]; /* Temporary buffer */
219 unsigned long flags; /* Status/Action bit flags: long for set_bit */
220} idetape_pc_t;
221
222/*
223 * Packet command flag bits.
224 */
225/* Set when an error is considered normal - We won't retry */
226#define PC_ABORT 0
227/* 1 When polling for DSC on a media access command */
228#define PC_WAIT_FOR_DSC 1
229/* 1 when we prefer to use DMA if possible */
230#define PC_DMA_RECOMMENDED 2
231/* 1 while DMA in progress */
232#define PC_DMA_IN_PROGRESS 3
233/* 1 when encountered problem during DMA */
234#define PC_DMA_ERROR 4
235/* Data direction */
236#define PC_WRITING 5
237
238/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 * A pipeline stage.
240 */
241typedef struct idetape_stage_s {
242 struct request rq; /* The corresponding request */
243 struct idetape_bh *bh; /* The data buffers */
244 struct idetape_stage_s *next; /* Pointer to the next stage */
245} idetape_stage_t;
246
247/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 * Most of our global data which we need to save even as we leave the
249 * driver due to an interrupt or a timer event is stored in a variable
250 * of type idetape_tape_t, defined below.
251 */
252typedef struct ide_tape_obj {
253 ide_drive_t *drive;
254 ide_driver_t *driver;
255 struct gendisk *disk;
256 struct kref kref;
257
258 /*
259 * Since a typical character device operation requires more
260 * than one packet command, we provide here enough memory
261 * for the maximum of interconnected packet commands.
262 * The packet commands are stored in the circular array pc_stack.
263 * pc_stack_index points to the last used entry, and warps around
264 * to the start when we get to the last array entry.
265 *
266 * pc points to the current processed packet command.
267 *
268 * failed_pc points to the last failed packet command, or contains
269 * NULL if we do not need to retry any packet command. This is
270 * required since an additional packet command is needed before the
271 * retry, to get detailed information on what went wrong.
272 */
273 /* Current packet command */
274 idetape_pc_t *pc;
275 /* Last failed packet command */
276 idetape_pc_t *failed_pc;
277 /* Packet command stack */
278 idetape_pc_t pc_stack[IDETAPE_PC_STACK];
279 /* Next free packet command storage space */
280 int pc_stack_index;
281 struct request rq_stack[IDETAPE_PC_STACK];
282 /* We implement a circular array */
283 int rq_stack_index;
284
285 /*
286 * DSC polling variables.
287 *
288 * While polling for DSC we use postponed_rq to postpone the
289 * current request so that ide.c will be able to service
290 * pending requests on the other device. Note that at most
291 * we will have only one DSC (usually data transfer) request
292 * in the device request queue. Additional requests can be
293 * queued in our internal pipeline, but they will be visible
294 * to ide.c only one at a time.
295 */
296 struct request *postponed_rq;
297 /* The time in which we started polling for DSC */
298 unsigned long dsc_polling_start;
299 /* Timer used to poll for dsc */
300 struct timer_list dsc_timer;
301 /* Read/Write dsc polling frequency */
302 unsigned long best_dsc_rw_frequency;
303 /* The current polling frequency */
304 unsigned long dsc_polling_frequency;
305 /* Maximum waiting time */
306 unsigned long dsc_timeout;
307
308 /*
309 * Read position information
310 */
311 u8 partition;
312 /* Current block */
313 unsigned int first_frame_position;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314
315 /*
316 * Last error information
317 */
318 u8 sense_key, asc, ascq;
319
320 /*
321 * Character device operation
322 */
323 unsigned int minor;
324 /* device name */
325 char name[4];
326 /* Current character device data transfer direction */
Borislav Petkov54abf372008-02-06 02:57:52 +0100327 u8 chrdev_dir;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328
329 /*
330 * Device information
331 */
332 /* Usually 512 or 1024 bytes */
333 unsigned short tape_block_size;
334 int user_bs_factor;
Borislav Petkovb6422012008-02-02 19:56:49 +0100335
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 /* Copy of the tape's Capabilities and Mechanical Page */
Borislav Petkovb6422012008-02-02 19:56:49 +0100337 u8 caps[20];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338
339 /*
340 * Active data transfer request parameters.
341 *
342 * At most, there is only one ide-tape originated data transfer
343 * request in the device request queue. This allows ide.c to
344 * easily service requests from the other device when we
345 * postpone our active request. In the pipelined operation
346 * mode, we use our internal pipeline structure to hold
347 * more data requests.
348 *
349 * The data buffer size is chosen based on the tape's
350 * recommendation.
351 */
352 /* Pointer to the request which is waiting in the device request queue */
353 struct request *active_data_request;
354 /* Data buffer size (chosen based on the tape's recommendation */
355 int stage_size;
356 idetape_stage_t *merge_stage;
357 int merge_stage_size;
358 struct idetape_bh *bh;
359 char *b_data;
360 int b_count;
361
362 /*
363 * Pipeline parameters.
364 *
365 * To accomplish non-pipelined mode, we simply set the following
366 * variables to zero (or NULL, where appropriate).
367 */
368 /* Number of currently used stages */
369 int nr_stages;
370 /* Number of pending stages */
371 int nr_pending_stages;
372 /* We will not allocate more than this number of stages */
373 int max_stages, min_pipeline, max_pipeline;
374 /* The first stage which will be removed from the pipeline */
375 idetape_stage_t *first_stage;
376 /* The currently active stage */
377 idetape_stage_t *active_stage;
378 /* Will be serviced after the currently active request */
379 idetape_stage_t *next_stage;
380 /* New requests will be added to the pipeline here */
381 idetape_stage_t *last_stage;
382 /* Optional free stage which we can use */
383 idetape_stage_t *cache_stage;
384 int pages_per_stage;
385 /* Wasted space in each stage */
386 int excess_bh_size;
387
388 /* Status/Action flags: long for set_bit */
389 unsigned long flags;
390 /* protects the ide-tape queue */
391 spinlock_t spinlock;
392
393 /*
394 * Measures average tape speed
395 */
396 unsigned long avg_time;
397 int avg_size;
398 int avg_speed;
399
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 /* the door is currently locked */
401 int door_locked;
402 /* the tape hardware is write protected */
403 char drv_write_prot;
404 /* the tape is write protected (hardware or opened as read-only) */
405 char write_prot;
406
407 /*
408 * Limit the number of times a request can
409 * be postponed, to avoid an infinite postpone
410 * deadlock.
411 */
412 /* request postpone count limit */
413 int postpone_cnt;
414
415 /*
416 * Measures number of frames:
417 *
418 * 1. written/read to/from the driver pipeline (pipeline_head).
419 * 2. written/read to/from the tape buffers (idetape_bh).
420 * 3. written/read by the tape to/from the media (tape_head).
421 */
422 int pipeline_head;
423 int buffer_head;
424 int tape_head;
425 int last_tape_head;
426
427 /*
428 * Speed control at the tape buffers input/output
429 */
430 unsigned long insert_time;
431 int insert_size;
432 int insert_speed;
433 int max_insert_speed;
434 int measure_insert_time;
435
436 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 * Speed regulation negative feedback loop
438 */
439 int speed_control;
440 int pipeline_head_speed;
441 int controlled_pipeline_head_speed;
442 int uncontrolled_pipeline_head_speed;
443 int controlled_last_pipeline_head;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 unsigned long uncontrolled_pipeline_head_time;
445 unsigned long controlled_pipeline_head_time;
446 int controlled_previous_pipeline_head;
447 int uncontrolled_previous_pipeline_head;
448 unsigned long controlled_previous_head_time;
449 unsigned long uncontrolled_previous_head_time;
450 int restart_speed_control_req;
451
Borislav Petkov8004a8c2008-02-06 02:57:51 +0100452 u32 debug_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453} idetape_tape_t;
454
Arjan van de Vencf8b8972006-03-23 03:00:45 -0800455static DEFINE_MUTEX(idetape_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456
Will Dysond5dee802005-09-16 02:55:07 -0700457static struct class *idetape_sysfs_class;
458
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459#define to_ide_tape(obj) container_of(obj, struct ide_tape_obj, kref)
460
461#define ide_tape_g(disk) \
462 container_of((disk)->private_data, struct ide_tape_obj, driver)
463
464static struct ide_tape_obj *ide_tape_get(struct gendisk *disk)
465{
466 struct ide_tape_obj *tape = NULL;
467
Arjan van de Vencf8b8972006-03-23 03:00:45 -0800468 mutex_lock(&idetape_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 tape = ide_tape_g(disk);
470 if (tape)
471 kref_get(&tape->kref);
Arjan van de Vencf8b8972006-03-23 03:00:45 -0800472 mutex_unlock(&idetape_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 return tape;
474}
475
476static void ide_tape_release(struct kref *);
477
478static void ide_tape_put(struct ide_tape_obj *tape)
479{
Arjan van de Vencf8b8972006-03-23 03:00:45 -0800480 mutex_lock(&idetape_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 kref_put(&tape->kref, ide_tape_release);
Arjan van de Vencf8b8972006-03-23 03:00:45 -0800482 mutex_unlock(&idetape_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483}
484
485/*
486 * Tape door status
487 */
488#define DOOR_UNLOCKED 0
489#define DOOR_LOCKED 1
490#define DOOR_EXPLICITLY_LOCKED 2
491
492/*
493 * Tape flag bits values.
494 */
495#define IDETAPE_IGNORE_DSC 0
496#define IDETAPE_ADDRESS_VALID 1 /* 0 When the tape position is unknown */
497#define IDETAPE_BUSY 2 /* Device already opened */
498#define IDETAPE_PIPELINE_ERROR 3 /* Error detected in a pipeline stage */
499#define IDETAPE_DETECT_BS 4 /* Attempt to auto-detect the current user block size */
500#define IDETAPE_FILEMARK 5 /* Currently on a filemark */
501#define IDETAPE_DRQ_INTERRUPT 6 /* DRQ interrupt device */
502#define IDETAPE_READ_ERROR 7
503#define IDETAPE_PIPELINE_ACTIVE 8 /* pipeline active */
504/* 0 = no tape is loaded, so we don't rewind after ejecting */
505#define IDETAPE_MEDIUM_PRESENT 9
506
507/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 * Some defines for the READ BUFFER command
509 */
510#define IDETAPE_RETRIEVE_FAULTY_BLOCK 6
511
512/*
513 * Some defines for the SPACE command
514 */
515#define IDETAPE_SPACE_OVER_FILEMARK 1
516#define IDETAPE_SPACE_TO_EOD 3
517
518/*
519 * Some defines for the LOAD UNLOAD command
520 */
521#define IDETAPE_LU_LOAD_MASK 1
522#define IDETAPE_LU_RETENSION_MASK 2
523#define IDETAPE_LU_EOT_MASK 4
524
525/*
526 * Special requests for our block device strategy routine.
527 *
528 * In order to service a character device command, we add special
529 * requests to the tail of our block device request queue and wait
530 * for their completion.
531 */
532
533enum {
534 REQ_IDETAPE_PC1 = (1 << 0), /* packet command (first stage) */
535 REQ_IDETAPE_PC2 = (1 << 1), /* packet command (second stage) */
536 REQ_IDETAPE_READ = (1 << 2),
537 REQ_IDETAPE_WRITE = (1 << 3),
538 REQ_IDETAPE_READ_BUFFER = (1 << 4),
539};
540
541/*
542 * Error codes which are returned in rq->errors to the higher part
543 * of the driver.
544 */
545#define IDETAPE_ERROR_GENERAL 101
546#define IDETAPE_ERROR_FILEMARK 102
547#define IDETAPE_ERROR_EOD 103
548
549/*
550 * The following is used to format the general configuration word of
551 * the ATAPI IDENTIFY DEVICE command.
552 */
553struct idetape_id_gcw {
554 unsigned packet_size :2; /* Packet Size */
555 unsigned reserved234 :3; /* Reserved */
556 unsigned drq_type :2; /* Command packet DRQ type */
557 unsigned removable :1; /* Removable media */
558 unsigned device_type :5; /* Device type */
559 unsigned reserved13 :1; /* Reserved */
560 unsigned protocol :2; /* Protocol type */
561};
562
Borislav Petkovfa366252008-02-02 19:56:51 +0100563/* Structures related to the SELECT SENSE / MODE SENSE packet commands. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564#define IDETAPE_BLOCK_DESCRIPTOR 0
565#define IDETAPE_CAPABILITIES_PAGE 0x2a
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566
567/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 * The variables below are used for the character device interface.
569 * Additional state variables are defined in our ide_drive_t structure.
570 */
571static struct ide_tape_obj * idetape_devs[MAX_HWIFS * MAX_DRIVES];
572
573#define ide_tape_f(file) ((file)->private_data)
574
575static struct ide_tape_obj *ide_tape_chrdev_get(unsigned int i)
576{
577 struct ide_tape_obj *tape = NULL;
578
Arjan van de Vencf8b8972006-03-23 03:00:45 -0800579 mutex_lock(&idetape_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 tape = idetape_devs[i];
581 if (tape)
582 kref_get(&tape->kref);
Arjan van de Vencf8b8972006-03-23 03:00:45 -0800583 mutex_unlock(&idetape_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 return tape;
585}
586
587/*
588 * Function declarations
589 *
590 */
591static int idetape_chrdev_release (struct inode *inode, struct file *filp);
592static void idetape_write_release (ide_drive_t *drive, unsigned int minor);
593
594/*
595 * Too bad. The drive wants to send us data which we are not ready to accept.
596 * Just throw it away.
597 */
598static void idetape_discard_data (ide_drive_t *drive, unsigned int bcount)
599{
600 while (bcount--)
601 (void) HWIF(drive)->INB(IDE_DATA_REG);
602}
603
604static void idetape_input_buffers (ide_drive_t *drive, idetape_pc_t *pc, unsigned int bcount)
605{
606 struct idetape_bh *bh = pc->bh;
607 int count;
608
609 while (bcount) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 if (bh == NULL) {
611 printk(KERN_ERR "ide-tape: bh == NULL in "
612 "idetape_input_buffers\n");
613 idetape_discard_data(drive, bcount);
614 return;
615 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 count = min((unsigned int)(bh->b_size - atomic_read(&bh->b_count)), bcount);
617 HWIF(drive)->atapi_input_bytes(drive, bh->b_data + atomic_read(&bh->b_count), count);
618 bcount -= count;
619 atomic_add(count, &bh->b_count);
620 if (atomic_read(&bh->b_count) == bh->b_size) {
621 bh = bh->b_reqnext;
622 if (bh)
623 atomic_set(&bh->b_count, 0);
624 }
625 }
626 pc->bh = bh;
627}
628
629static void idetape_output_buffers (ide_drive_t *drive, idetape_pc_t *pc, unsigned int bcount)
630{
631 struct idetape_bh *bh = pc->bh;
632 int count;
633
634 while (bcount) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 if (bh == NULL) {
636 printk(KERN_ERR "ide-tape: bh == NULL in "
637 "idetape_output_buffers\n");
638 return;
639 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 count = min((unsigned int)pc->b_count, (unsigned int)bcount);
641 HWIF(drive)->atapi_output_bytes(drive, pc->b_data, count);
642 bcount -= count;
643 pc->b_data += count;
644 pc->b_count -= count;
645 if (!pc->b_count) {
646 pc->bh = bh = bh->b_reqnext;
647 if (bh) {
648 pc->b_data = bh->b_data;
649 pc->b_count = atomic_read(&bh->b_count);
650 }
651 }
652 }
653}
654
655static void idetape_update_buffers (idetape_pc_t *pc)
656{
657 struct idetape_bh *bh = pc->bh;
658 int count;
659 unsigned int bcount = pc->actually_transferred;
660
661 if (test_bit(PC_WRITING, &pc->flags))
662 return;
663 while (bcount) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664 if (bh == NULL) {
665 printk(KERN_ERR "ide-tape: bh == NULL in "
666 "idetape_update_buffers\n");
667 return;
668 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669 count = min((unsigned int)bh->b_size, (unsigned int)bcount);
670 atomic_set(&bh->b_count, count);
671 if (atomic_read(&bh->b_count) == bh->b_size)
672 bh = bh->b_reqnext;
673 bcount -= count;
674 }
675 pc->bh = bh;
676}
677
678/*
679 * idetape_next_pc_storage returns a pointer to a place in which we can
680 * safely store a packet command, even though we intend to leave the
681 * driver. A storage space for a maximum of IDETAPE_PC_STACK packet
682 * commands is allocated at initialization time.
683 */
684static idetape_pc_t *idetape_next_pc_storage (ide_drive_t *drive)
685{
686 idetape_tape_t *tape = drive->driver_data;
687
Borislav Petkov8004a8c2008-02-06 02:57:51 +0100688 debug_log(DBG_PCRQ_STACK, "pc_stack_index=%d\n", tape->pc_stack_index);
689
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 if (tape->pc_stack_index == IDETAPE_PC_STACK)
691 tape->pc_stack_index=0;
692 return (&tape->pc_stack[tape->pc_stack_index++]);
693}
694
695/*
696 * idetape_next_rq_storage is used along with idetape_next_pc_storage.
697 * Since we queue packet commands in the request queue, we need to
698 * allocate a request, along with the allocation of a packet command.
699 */
700
701/**************************************************************
702 * *
703 * This should get fixed to use kmalloc(.., GFP_ATOMIC) *
704 * followed later on by kfree(). -ml *
705 * *
706 **************************************************************/
707
708static struct request *idetape_next_rq_storage (ide_drive_t *drive)
709{
710 idetape_tape_t *tape = drive->driver_data;
711
Borislav Petkov8004a8c2008-02-06 02:57:51 +0100712 debug_log(DBG_PCRQ_STACK, "rq_stack_index=%d\n", tape->rq_stack_index);
713
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 if (tape->rq_stack_index == IDETAPE_PC_STACK)
715 tape->rq_stack_index=0;
716 return (&tape->rq_stack[tape->rq_stack_index++]);
717}
718
719/*
720 * idetape_init_pc initializes a packet command.
721 */
722static void idetape_init_pc (idetape_pc_t *pc)
723{
724 memset(pc->c, 0, 12);
725 pc->retries = 0;
726 pc->flags = 0;
727 pc->request_transfer = 0;
728 pc->buffer = pc->pc_buffer;
729 pc->buffer_size = IDETAPE_PC_BUFFER_SIZE;
730 pc->bh = NULL;
731 pc->b_data = NULL;
732}
733
734/*
Borislav Petkov1b5db432008-02-02 19:56:48 +0100735 * called on each failed packet command retry to analyze the request sense. We
736 * currently do not utilize this information.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737 */
Borislav Petkov1b5db432008-02-02 19:56:48 +0100738static void idetape_analyze_error(ide_drive_t *drive, u8 *sense)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739{
740 idetape_tape_t *tape = drive->driver_data;
741 idetape_pc_t *pc = tape->failed_pc;
742
Borislav Petkov1b5db432008-02-02 19:56:48 +0100743 tape->sense_key = sense[2] & 0xF;
744 tape->asc = sense[12];
745 tape->ascq = sense[13];
Borislav Petkov8004a8c2008-02-06 02:57:51 +0100746
747 debug_log(DBG_ERR, "pc = %x, sense key = %x, asc = %x, ascq = %x\n",
748 pc->c[0], tape->sense_key, tape->asc, tape->ascq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749
Borislav Petkov1b5db432008-02-02 19:56:48 +0100750 /* Correct pc->actually_transferred by asking the tape. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751 if (test_bit(PC_DMA_ERROR, &pc->flags)) {
Borislav Petkov1b5db432008-02-02 19:56:48 +0100752 pc->actually_transferred = pc->request_transfer -
753 tape->tape_block_size *
Borislav Petkov860ff5e2008-02-02 19:56:50 +0100754 be32_to_cpu(get_unaligned((u32 *)&sense[3]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755 idetape_update_buffers(pc);
756 }
757
758 /*
759 * If error was the result of a zero-length read or write command,
760 * with sense key=5, asc=0x22, ascq=0, let it slide. Some drives
761 * (i.e. Seagate STT3401A Travan) don't support 0-length read/writes.
762 */
Borislav Petkov90699ce2008-02-02 19:56:50 +0100763 if ((pc->c[0] == READ_6 || pc->c[0] == WRITE_6)
Borislav Petkov1b5db432008-02-02 19:56:48 +0100764 /* length == 0 */
765 && pc->c[4] == 0 && pc->c[3] == 0 && pc->c[2] == 0) {
766 if (tape->sense_key == 5) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767 /* don't report an error, everything's ok */
768 pc->error = 0;
769 /* don't retry read/write */
770 set_bit(PC_ABORT, &pc->flags);
771 }
772 }
Borislav Petkov90699ce2008-02-02 19:56:50 +0100773 if (pc->c[0] == READ_6 && (sense[2] & 0x80)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774 pc->error = IDETAPE_ERROR_FILEMARK;
775 set_bit(PC_ABORT, &pc->flags);
776 }
Borislav Petkov90699ce2008-02-02 19:56:50 +0100777 if (pc->c[0] == WRITE_6) {
Borislav Petkov1b5db432008-02-02 19:56:48 +0100778 if ((sense[2] & 0x40) || (tape->sense_key == 0xd
779 && tape->asc == 0x0 && tape->ascq == 0x2)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780 pc->error = IDETAPE_ERROR_EOD;
781 set_bit(PC_ABORT, &pc->flags);
782 }
783 }
Borislav Petkov90699ce2008-02-02 19:56:50 +0100784 if (pc->c[0] == READ_6 || pc->c[0] == WRITE_6) {
Borislav Petkov1b5db432008-02-02 19:56:48 +0100785 if (tape->sense_key == 8) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786 pc->error = IDETAPE_ERROR_EOD;
787 set_bit(PC_ABORT, &pc->flags);
788 }
789 if (!test_bit(PC_ABORT, &pc->flags) &&
790 pc->actually_transferred)
791 pc->retries = IDETAPE_MAX_PC_RETRIES + 1;
792 }
793}
794
Borislav Petkov419d4742008-02-02 19:56:51 +0100795static void idetape_activate_next_stage(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796{
797 idetape_tape_t *tape = drive->driver_data;
798 idetape_stage_t *stage = tape->next_stage;
799 struct request *rq = &stage->rq;
800
Borislav Petkov8004a8c2008-02-06 02:57:51 +0100801 debug_log(DBG_PROCS, "Enter %s\n", __func__);
802
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803 if (stage == NULL) {
Borislav Petkov8004a8c2008-02-06 02:57:51 +0100804 printk(KERN_ERR "ide-tape: bug: Trying to activate a non"
805 " existing stage\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806 return;
807 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808
809 rq->rq_disk = tape->disk;
810 rq->buffer = NULL;
811 rq->special = (void *)stage->bh;
812 tape->active_data_request = rq;
813 tape->active_stage = stage;
814 tape->next_stage = stage->next;
815}
816
817/*
818 * idetape_increase_max_pipeline_stages is a part of the feedback
819 * loop which tries to find the optimum number of stages. In the
820 * feedback loop, we are starting from a minimum maximum number of
821 * stages, and if we sense that the pipeline is empty, we try to
822 * increase it, until we reach the user compile time memory limit.
823 */
824static void idetape_increase_max_pipeline_stages (ide_drive_t *drive)
825{
826 idetape_tape_t *tape = drive->driver_data;
827 int increase = (tape->max_pipeline - tape->min_pipeline) / 10;
Borislav Petkov8004a8c2008-02-06 02:57:51 +0100828
829 debug_log(DBG_PROCS, "Enter %s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830
831 tape->max_stages += max(increase, 1);
832 tape->max_stages = max(tape->max_stages, tape->min_pipeline);
833 tape->max_stages = min(tape->max_stages, tape->max_pipeline);
834}
835
836/*
837 * idetape_kfree_stage calls kfree to completely free a stage, along with
838 * its related buffers.
839 */
840static void __idetape_kfree_stage (idetape_stage_t *stage)
841{
842 struct idetape_bh *prev_bh, *bh = stage->bh;
843 int size;
844
845 while (bh != NULL) {
846 if (bh->b_data != NULL) {
847 size = (int) bh->b_size;
848 while (size > 0) {
849 free_page((unsigned long) bh->b_data);
850 size -= PAGE_SIZE;
851 bh->b_data += PAGE_SIZE;
852 }
853 }
854 prev_bh = bh;
855 bh = bh->b_reqnext;
856 kfree(prev_bh);
857 }
858 kfree(stage);
859}
860
861static void idetape_kfree_stage (idetape_tape_t *tape, idetape_stage_t *stage)
862{
863 __idetape_kfree_stage(stage);
864}
865
866/*
867 * idetape_remove_stage_head removes tape->first_stage from the pipeline.
868 * The caller should avoid race conditions.
869 */
870static void idetape_remove_stage_head (ide_drive_t *drive)
871{
872 idetape_tape_t *tape = drive->driver_data;
873 idetape_stage_t *stage;
Borislav Petkov8004a8c2008-02-06 02:57:51 +0100874
875 debug_log(DBG_PROCS, "Enter %s\n", __func__);
876
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877 if (tape->first_stage == NULL) {
878 printk(KERN_ERR "ide-tape: bug: tape->first_stage is NULL\n");
Borislav Petkov55a5d292008-02-02 19:56:49 +0100879 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880 }
881 if (tape->active_stage == tape->first_stage) {
Borislav Petkov8004a8c2008-02-06 02:57:51 +0100882 printk(KERN_ERR "ide-tape: bug: Trying to free our active "
883 "pipeline stage\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884 return;
885 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886 stage = tape->first_stage;
887 tape->first_stage = stage->next;
888 idetape_kfree_stage(tape, stage);
889 tape->nr_stages--;
890 if (tape->first_stage == NULL) {
891 tape->last_stage = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892 if (tape->next_stage != NULL)
893 printk(KERN_ERR "ide-tape: bug: tape->next_stage != NULL\n");
894 if (tape->nr_stages)
895 printk(KERN_ERR "ide-tape: bug: nr_stages should be 0 now\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896 }
897}
898
899/*
900 * This will free all the pipeline stages starting from new_last_stage->next
901 * to the end of the list, and point tape->last_stage to new_last_stage.
902 */
903static void idetape_abort_pipeline(ide_drive_t *drive,
904 idetape_stage_t *new_last_stage)
905{
906 idetape_tape_t *tape = drive->driver_data;
907 idetape_stage_t *stage = new_last_stage->next;
908 idetape_stage_t *nstage;
909
Borislav Petkov8004a8c2008-02-06 02:57:51 +0100910 debug_log(DBG_PROCS, "%s: Enter %s\n", tape->name, __func__);
911
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912 while (stage) {
913 nstage = stage->next;
914 idetape_kfree_stage(tape, stage);
915 --tape->nr_stages;
916 --tape->nr_pending_stages;
917 stage = nstage;
918 }
919 if (new_last_stage)
920 new_last_stage->next = NULL;
921 tape->last_stage = new_last_stage;
922 tape->next_stage = NULL;
923}
924
925/*
926 * idetape_end_request is used to finish servicing a request, and to
927 * insert a pending pipeline request into the main device queue.
928 */
929static int idetape_end_request(ide_drive_t *drive, int uptodate, int nr_sects)
930{
931 struct request *rq = HWGROUP(drive)->rq;
932 idetape_tape_t *tape = drive->driver_data;
933 unsigned long flags;
934 int error;
935 int remove_stage = 0;
936 idetape_stage_t *active_stage;
937
Borislav Petkov8004a8c2008-02-06 02:57:51 +0100938 debug_log(DBG_PROCS, "Enter %s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939
940 switch (uptodate) {
941 case 0: error = IDETAPE_ERROR_GENERAL; break;
942 case 1: error = 0; break;
943 default: error = uptodate;
944 }
945 rq->errors = error;
946 if (error)
947 tape->failed_pc = NULL;
948
Bartlomiej Zolnierkiewicz36872212008-01-26 20:13:10 +0100949 if (!blk_special_request(rq)) {
950 ide_end_request(drive, uptodate, nr_sects);
951 return 0;
952 }
953
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954 spin_lock_irqsave(&tape->spinlock, flags);
955
956 /* The request was a pipelined data transfer request */
957 if (tape->active_data_request == rq) {
958 active_stage = tape->active_stage;
959 tape->active_stage = NULL;
960 tape->active_data_request = NULL;
961 tape->nr_pending_stages--;
962 if (rq->cmd[0] & REQ_IDETAPE_WRITE) {
963 remove_stage = 1;
964 if (error) {
965 set_bit(IDETAPE_PIPELINE_ERROR, &tape->flags);
966 if (error == IDETAPE_ERROR_EOD)
967 idetape_abort_pipeline(drive, active_stage);
968 }
969 } else if (rq->cmd[0] & REQ_IDETAPE_READ) {
970 if (error == IDETAPE_ERROR_EOD) {
971 set_bit(IDETAPE_PIPELINE_ERROR, &tape->flags);
972 idetape_abort_pipeline(drive, active_stage);
973 }
974 }
975 if (tape->next_stage != NULL) {
Borislav Petkov419d4742008-02-02 19:56:51 +0100976 idetape_activate_next_stage(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977
978 /*
979 * Insert the next request into the request queue.
980 */
981 (void) ide_do_drive_cmd(drive, tape->active_data_request, ide_end);
982 } else if (!error) {
983 idetape_increase_max_pipeline_stages(drive);
984 }
985 }
986 ide_end_drive_cmd(drive, 0, 0);
987// blkdev_dequeue_request(rq);
988// drive->rq = NULL;
989// end_that_request_last(rq);
990
991 if (remove_stage)
992 idetape_remove_stage_head(drive);
993 if (tape->active_data_request == NULL)
994 clear_bit(IDETAPE_PIPELINE_ACTIVE, &tape->flags);
995 spin_unlock_irqrestore(&tape->spinlock, flags);
996 return 0;
997}
998
999static ide_startstop_t idetape_request_sense_callback (ide_drive_t *drive)
1000{
1001 idetape_tape_t *tape = drive->driver_data;
1002
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001003 debug_log(DBG_PROCS, "Enter %s\n", __func__);
1004
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005 if (!tape->pc->error) {
Borislav Petkov1b5db432008-02-02 19:56:48 +01001006 idetape_analyze_error(drive, tape->pc->buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007 idetape_end_request(drive, 1, 0);
1008 } else {
1009 printk(KERN_ERR "ide-tape: Error in REQUEST SENSE itself - Aborting request!\n");
1010 idetape_end_request(drive, 0, 0);
1011 }
1012 return ide_stopped;
1013}
1014
1015static void idetape_create_request_sense_cmd (idetape_pc_t *pc)
1016{
1017 idetape_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +01001018 pc->c[0] = REQUEST_SENSE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019 pc->c[4] = 20;
1020 pc->request_transfer = 20;
1021 pc->callback = &idetape_request_sense_callback;
1022}
1023
1024static void idetape_init_rq(struct request *rq, u8 cmd)
1025{
1026 memset(rq, 0, sizeof(*rq));
Jens Axboe4aff5e22006-08-10 08:44:47 +02001027 rq->cmd_type = REQ_TYPE_SPECIAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028 rq->cmd[0] = cmd;
1029}
1030
1031/*
1032 * idetape_queue_pc_head generates a new packet command request in front
1033 * of the request queue, before the current request, so that it will be
1034 * processed immediately, on the next pass through the driver.
1035 *
1036 * idetape_queue_pc_head is called from the request handling part of
1037 * the driver (the "bottom" part). Safe storage for the request should
1038 * be allocated with idetape_next_pc_storage and idetape_next_rq_storage
1039 * before calling idetape_queue_pc_head.
1040 *
1041 * Memory for those requests is pre-allocated at initialization time, and
1042 * is limited to IDETAPE_PC_STACK requests. We assume that we have enough
1043 * space for the maximum possible number of inter-dependent packet commands.
1044 *
1045 * The higher level of the driver - The ioctl handler and the character
1046 * device handling functions should queue request to the lower level part
1047 * and wait for their completion using idetape_queue_pc_tail or
1048 * idetape_queue_rw_tail.
1049 */
1050static void idetape_queue_pc_head (ide_drive_t *drive, idetape_pc_t *pc,struct request *rq)
1051{
1052 struct ide_tape_obj *tape = drive->driver_data;
1053
1054 idetape_init_rq(rq, REQ_IDETAPE_PC1);
1055 rq->buffer = (char *) pc;
1056 rq->rq_disk = tape->disk;
1057 (void) ide_do_drive_cmd(drive, rq, ide_preempt);
1058}
1059
1060/*
1061 * idetape_retry_pc is called when an error was detected during the
1062 * last packet command. We queue a request sense packet command in
1063 * the head of the request list.
1064 */
1065static ide_startstop_t idetape_retry_pc (ide_drive_t *drive)
1066{
1067 idetape_tape_t *tape = drive->driver_data;
1068 idetape_pc_t *pc;
1069 struct request *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070
Bartlomiej Zolnierkiewicz64a57fe2008-02-06 02:57:51 +01001071 (void)ide_read_error(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072 pc = idetape_next_pc_storage(drive);
1073 rq = idetape_next_rq_storage(drive);
1074 idetape_create_request_sense_cmd(pc);
1075 set_bit(IDETAPE_IGNORE_DSC, &tape->flags);
1076 idetape_queue_pc_head(drive, pc, rq);
1077 return ide_stopped;
1078}
1079
1080/*
1081 * idetape_postpone_request postpones the current request so that
1082 * ide.c will be able to service requests from another device on
1083 * the same hwgroup while we are polling for DSC.
1084 */
1085static void idetape_postpone_request (ide_drive_t *drive)
1086{
1087 idetape_tape_t *tape = drive->driver_data;
1088
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001089 debug_log(DBG_PROCS, "Enter %s\n", __func__);
1090
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091 tape->postponed_rq = HWGROUP(drive)->rq;
1092 ide_stall_queue(drive, tape->dsc_polling_frequency);
1093}
1094
Borislav Petkova1efc852008-02-06 02:57:52 +01001095typedef void idetape_io_buf(ide_drive_t *, idetape_pc_t *, unsigned int);
1096
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097/*
Borislav Petkova1efc852008-02-06 02:57:52 +01001098 * This is the usual interrupt handler which will be called during a packet
1099 * command. We will transfer some of the data (as requested by the drive) and
1100 * will re-point interrupt handler to us. When data transfer is finished, we
1101 * will act according to the algorithm described before
1102 * idetape_issue_packet_command.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103 */
Borislav Petkova1efc852008-02-06 02:57:52 +01001104static ide_startstop_t idetape_pc_intr(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105{
1106 ide_hwif_t *hwif = drive->hwif;
1107 idetape_tape_t *tape = drive->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108 idetape_pc_t *pc = tape->pc;
Borislav Petkova1efc852008-02-06 02:57:52 +01001109 xfer_func_t *xferfunc;
1110 idetape_io_buf *iobuf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111 unsigned int temp;
1112#if SIMULATE_ERRORS
1113 static int error_sim_count = 0;
1114#endif
Bartlomiej Zolnierkiewicz790d1232008-01-25 22:17:12 +01001115 u16 bcount;
Bartlomiej Zolnierkiewicz8e7657a2008-01-25 22:17:12 +01001116 u8 stat, ireason;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001118 debug_log(DBG_PROCS, "Enter %s - interrupt handler\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119
1120 /* Clear the interrupt */
Bartlomiej Zolnierkiewiczc47137a2008-02-06 02:57:51 +01001121 stat = ide_read_status(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122
1123 if (test_bit(PC_DMA_IN_PROGRESS, &pc->flags)) {
Bartlomiej Zolnierkiewicz22c525b2008-01-25 22:17:11 +01001124 if (hwif->ide_dma_end(drive) || (stat & ERR_STAT)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125 /*
1126 * A DMA error is sometimes expected. For example,
1127 * if the tape is crossing a filemark during a
1128 * READ command, it will issue an irq and position
1129 * itself before the filemark, so that only a partial
1130 * data transfer will occur (which causes the DMA
1131 * error). In that case, we will later ask the tape
1132 * how much bytes of the original request were
1133 * actually transferred (we can't receive that
1134 * information from the DMA engine on most chipsets).
1135 */
1136
1137 /*
1138 * On the contrary, a DMA error is never expected;
1139 * it usually indicates a hardware error or abort.
1140 * If the tape crosses a filemark during a READ
1141 * command, it will issue an irq and position itself
1142 * after the filemark (not before). Only a partial
1143 * data transfer will occur, but no DMA error.
1144 * (AS, 19 Apr 2001)
1145 */
1146 set_bit(PC_DMA_ERROR, &pc->flags);
1147 } else {
1148 pc->actually_transferred = pc->request_transfer;
1149 idetape_update_buffers(pc);
1150 }
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001151 debug_log(DBG_PROCS, "DMA finished\n");
1152
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153 }
1154
1155 /* No more interrupts */
Bartlomiej Zolnierkiewicz22c525b2008-01-25 22:17:11 +01001156 if ((stat & DRQ_STAT) == 0) {
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001157 debug_log(DBG_SENSE, "Packet command completed, %d bytes"
1158 " transferred\n", pc->actually_transferred);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001160 clear_bit(PC_DMA_IN_PROGRESS, &pc->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161 local_irq_enable();
1162
1163#if SIMULATE_ERRORS
Borislav Petkov90699ce2008-02-02 19:56:50 +01001164 if ((pc->c[0] == WRITE_6 || pc->c[0] == READ_6) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165 (++error_sim_count % 100) == 0) {
1166 printk(KERN_INFO "ide-tape: %s: simulating error\n",
1167 tape->name);
Bartlomiej Zolnierkiewicz22c525b2008-01-25 22:17:11 +01001168 stat |= ERR_STAT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169 }
1170#endif
Borislav Petkov90699ce2008-02-02 19:56:50 +01001171 if ((stat & ERR_STAT) && pc->c[0] == REQUEST_SENSE)
Bartlomiej Zolnierkiewicz22c525b2008-01-25 22:17:11 +01001172 stat &= ~ERR_STAT;
1173 if ((stat & ERR_STAT) || test_bit(PC_DMA_ERROR, &pc->flags)) {
1174 /* Error detected */
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001175 debug_log(DBG_ERR, "%s: I/O error\n", tape->name);
1176
Borislav Petkov90699ce2008-02-02 19:56:50 +01001177 if (pc->c[0] == REQUEST_SENSE) {
Borislav Petkova1efc852008-02-06 02:57:52 +01001178 printk(KERN_ERR "ide-tape: I/O error in request"
1179 " sense command\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180 return ide_do_reset(drive);
1181 }
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001182 debug_log(DBG_ERR, "[cmd %x]: check condition\n",
1183 pc->c[0]);
1184
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185 /* Retry operation */
1186 return idetape_retry_pc(drive);
1187 }
1188 pc->error = 0;
1189 if (test_bit(PC_WAIT_FOR_DSC, &pc->flags) &&
Bartlomiej Zolnierkiewicz22c525b2008-01-25 22:17:11 +01001190 (stat & SEEK_STAT) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191 /* Media access command */
1192 tape->dsc_polling_start = jiffies;
1193 tape->dsc_polling_frequency = IDETAPE_DSC_MA_FAST;
1194 tape->dsc_timeout = jiffies + IDETAPE_DSC_MA_TIMEOUT;
1195 /* Allow ide.c to handle other requests */
1196 idetape_postpone_request(drive);
1197 return ide_stopped;
1198 }
1199 if (tape->failed_pc == pc)
1200 tape->failed_pc = NULL;
1201 /* Command finished - Call the callback function */
1202 return pc->callback(drive);
1203 }
1204 if (test_and_clear_bit(PC_DMA_IN_PROGRESS, &pc->flags)) {
1205 printk(KERN_ERR "ide-tape: The tape wants to issue more "
1206 "interrupts in DMA mode\n");
1207 printk(KERN_ERR "ide-tape: DMA disabled, reverting to PIO\n");
Bartlomiej Zolnierkiewicz7469aaf2007-02-17 02:40:26 +01001208 ide_dma_off(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209 return ide_do_reset(drive);
1210 }
1211 /* Get the number of bytes to transfer on this interrupt. */
Bartlomiej Zolnierkiewicz790d1232008-01-25 22:17:12 +01001212 bcount = (hwif->INB(IDE_BCOUNTH_REG) << 8) |
1213 hwif->INB(IDE_BCOUNTL_REG);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214
Bartlomiej Zolnierkiewicz8e7657a2008-01-25 22:17:12 +01001215 ireason = hwif->INB(IDE_IREASON_REG);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216
Bartlomiej Zolnierkiewicz8e7657a2008-01-25 22:17:12 +01001217 if (ireason & CD) {
Borislav Petkova1efc852008-02-06 02:57:52 +01001218 printk(KERN_ERR "ide-tape: CoD != 0 in %s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219 return ide_do_reset(drive);
1220 }
Bartlomiej Zolnierkiewicz8e7657a2008-01-25 22:17:12 +01001221 if (((ireason & IO) == IO) == test_bit(PC_WRITING, &pc->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222 /* Hopefully, we will never get here */
1223 printk(KERN_ERR "ide-tape: We wanted to %s, ",
Bartlomiej Zolnierkiewicz8e7657a2008-01-25 22:17:12 +01001224 (ireason & IO) ? "Write" : "Read");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225 printk(KERN_ERR "ide-tape: but the tape wants us to %s !\n",
Bartlomiej Zolnierkiewicz8e7657a2008-01-25 22:17:12 +01001226 (ireason & IO) ? "Read" : "Write");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227 return ide_do_reset(drive);
1228 }
1229 if (!test_bit(PC_WRITING, &pc->flags)) {
1230 /* Reading - Check that we have enough space */
Bartlomiej Zolnierkiewicz790d1232008-01-25 22:17:12 +01001231 temp = pc->actually_transferred + bcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232 if (temp > pc->request_transfer) {
1233 if (temp > pc->buffer_size) {
Borislav Petkova1efc852008-02-06 02:57:52 +01001234 printk(KERN_ERR "ide-tape: The tape wants to "
1235 "send us more data than expected "
1236 "- discarding data\n");
Bartlomiej Zolnierkiewicz790d1232008-01-25 22:17:12 +01001237 idetape_discard_data(drive, bcount);
Borislav Petkova1efc852008-02-06 02:57:52 +01001238 ide_set_handler(drive, &idetape_pc_intr,
1239 IDETAPE_WAIT_CMD, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240 return ide_started;
1241 }
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001242 debug_log(DBG_SENSE, "The tape wants to send us more "
1243 "data than expected - allowing transfer\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244 }
Borislav Petkova1efc852008-02-06 02:57:52 +01001245 iobuf = &idetape_input_buffers;
1246 xferfunc = hwif->atapi_input_bytes;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247 } else {
Borislav Petkova1efc852008-02-06 02:57:52 +01001248 iobuf = &idetape_output_buffers;
1249 xferfunc = hwif->atapi_output_bytes;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250 }
Borislav Petkova1efc852008-02-06 02:57:52 +01001251
1252 if (pc->bh)
1253 iobuf(drive, pc, bcount);
1254 else
1255 xferfunc(drive, pc->current_position, bcount);
1256
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257 /* Update the current position */
Bartlomiej Zolnierkiewicz790d1232008-01-25 22:17:12 +01001258 pc->actually_transferred += bcount;
1259 pc->current_position += bcount;
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001260
1261 debug_log(DBG_SENSE, "[cmd %x] transferred %d bytes on that intr.\n",
1262 pc->c[0], bcount);
1263
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264 /* And set the interrupt handler again */
1265 ide_set_handler(drive, &idetape_pc_intr, IDETAPE_WAIT_CMD, NULL);
1266 return ide_started;
1267}
1268
1269/*
1270 * Packet Command Interface
1271 *
1272 * The current Packet Command is available in tape->pc, and will not
1273 * change until we finish handling it. Each packet command is associated
1274 * with a callback function that will be called when the command is
1275 * finished.
1276 *
1277 * The handling will be done in three stages:
1278 *
1279 * 1. idetape_issue_packet_command will send the packet command to the
1280 * drive, and will set the interrupt handler to idetape_pc_intr.
1281 *
1282 * 2. On each interrupt, idetape_pc_intr will be called. This step
1283 * will be repeated until the device signals us that no more
1284 * interrupts will be issued.
1285 *
1286 * 3. ATAPI Tape media access commands have immediate status with a
1287 * delayed process. In case of a successful initiation of a
1288 * media access packet command, the DSC bit will be set when the
1289 * actual execution of the command is finished.
1290 * Since the tape drive will not issue an interrupt, we have to
1291 * poll for this event. In this case, we define the request as
1292 * "low priority request" by setting rq_status to
1293 * IDETAPE_RQ_POSTPONED, set a timer to poll for DSC and exit
1294 * the driver.
1295 *
1296 * ide.c will then give higher priority to requests which
1297 * originate from the other device, until will change rq_status
1298 * to RQ_ACTIVE.
1299 *
1300 * 4. When the packet command is finished, it will be checked for errors.
1301 *
1302 * 5. In case an error was found, we queue a request sense packet
1303 * command in front of the request queue and retry the operation
1304 * up to IDETAPE_MAX_PC_RETRIES times.
1305 *
1306 * 6. In case no error was found, or we decided to give up and not
1307 * to retry again, the callback function will be called and then
1308 * we will handle the next request.
1309 *
1310 */
1311static ide_startstop_t idetape_transfer_pc(ide_drive_t *drive)
1312{
1313 ide_hwif_t *hwif = drive->hwif;
1314 idetape_tape_t *tape = drive->driver_data;
1315 idetape_pc_t *pc = tape->pc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316 int retries = 100;
1317 ide_startstop_t startstop;
Bartlomiej Zolnierkiewicz8e7657a2008-01-25 22:17:12 +01001318 u8 ireason;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319
1320 if (ide_wait_stat(&startstop,drive,DRQ_STAT,BUSY_STAT,WAIT_READY)) {
1321 printk(KERN_ERR "ide-tape: Strange, packet command initiated yet DRQ isn't asserted\n");
1322 return startstop;
1323 }
Bartlomiej Zolnierkiewicz8e7657a2008-01-25 22:17:12 +01001324 ireason = hwif->INB(IDE_IREASON_REG);
1325 while (retries-- && ((ireason & CD) == 0 || (ireason & IO))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326 printk(KERN_ERR "ide-tape: (IO,CoD != (0,1) while issuing "
1327 "a packet command, retrying\n");
1328 udelay(100);
Bartlomiej Zolnierkiewicz8e7657a2008-01-25 22:17:12 +01001329 ireason = hwif->INB(IDE_IREASON_REG);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330 if (retries == 0) {
1331 printk(KERN_ERR "ide-tape: (IO,CoD != (0,1) while "
1332 "issuing a packet command, ignoring\n");
Bartlomiej Zolnierkiewicz8e7657a2008-01-25 22:17:12 +01001333 ireason |= CD;
1334 ireason &= ~IO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335 }
1336 }
Bartlomiej Zolnierkiewicz8e7657a2008-01-25 22:17:12 +01001337 if ((ireason & CD) == 0 || (ireason & IO)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338 printk(KERN_ERR "ide-tape: (IO,CoD) != (0,1) while issuing "
1339 "a packet command\n");
1340 return ide_do_reset(drive);
1341 }
1342 /* Set the interrupt routine */
1343 ide_set_handler(drive, &idetape_pc_intr, IDETAPE_WAIT_CMD, NULL);
1344#ifdef CONFIG_BLK_DEV_IDEDMA
1345 /* Begin DMA, if necessary */
1346 if (test_bit(PC_DMA_IN_PROGRESS, &pc->flags))
1347 hwif->dma_start(drive);
1348#endif
1349 /* Send the actual packet */
1350 HWIF(drive)->atapi_output_bytes(drive, pc->c, 12);
1351 return ide_started;
1352}
1353
1354static ide_startstop_t idetape_issue_packet_command (ide_drive_t *drive, idetape_pc_t *pc)
1355{
1356 ide_hwif_t *hwif = drive->hwif;
1357 idetape_tape_t *tape = drive->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358 int dma_ok = 0;
Bartlomiej Zolnierkiewicz790d1232008-01-25 22:17:12 +01001359 u16 bcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360
Borislav Petkov90699ce2008-02-02 19:56:50 +01001361 if (tape->pc->c[0] == REQUEST_SENSE &&
1362 pc->c[0] == REQUEST_SENSE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363 printk(KERN_ERR "ide-tape: possible ide-tape.c bug - "
1364 "Two request sense in serial were issued\n");
1365 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366
Borislav Petkov90699ce2008-02-02 19:56:50 +01001367 if (tape->failed_pc == NULL && pc->c[0] != REQUEST_SENSE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368 tape->failed_pc = pc;
1369 /* Set the current packet command */
1370 tape->pc = pc;
1371
1372 if (pc->retries > IDETAPE_MAX_PC_RETRIES ||
1373 test_bit(PC_ABORT, &pc->flags)) {
1374 /*
1375 * We will "abort" retrying a packet command in case
1376 * a legitimate error code was received (crossing a
1377 * filemark, or end of the media, for example).
1378 */
1379 if (!test_bit(PC_ABORT, &pc->flags)) {
Borislav Petkov90699ce2008-02-02 19:56:50 +01001380 if (!(pc->c[0] == TEST_UNIT_READY &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381 tape->sense_key == 2 && tape->asc == 4 &&
1382 (tape->ascq == 1 || tape->ascq == 8))) {
1383 printk(KERN_ERR "ide-tape: %s: I/O error, "
1384 "pc = %2x, key = %2x, "
1385 "asc = %2x, ascq = %2x\n",
1386 tape->name, pc->c[0],
1387 tape->sense_key, tape->asc,
1388 tape->ascq);
1389 }
1390 /* Giving up */
1391 pc->error = IDETAPE_ERROR_GENERAL;
1392 }
1393 tape->failed_pc = NULL;
1394 return pc->callback(drive);
1395 }
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001396 debug_log(DBG_SENSE, "Retry #%d, cmd = %02X\n", pc->retries, pc->c[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397
1398 pc->retries++;
1399 /* We haven't transferred any data yet */
1400 pc->actually_transferred = 0;
1401 pc->current_position = pc->buffer;
1402 /* Request to transfer the entire buffer at once */
Bartlomiej Zolnierkiewicz790d1232008-01-25 22:17:12 +01001403 bcount = pc->request_transfer;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404
1405 if (test_and_clear_bit(PC_DMA_ERROR, &pc->flags)) {
1406 printk(KERN_WARNING "ide-tape: DMA disabled, "
1407 "reverting to PIO\n");
Bartlomiej Zolnierkiewicz7469aaf2007-02-17 02:40:26 +01001408 ide_dma_off(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409 }
1410 if (test_bit(PC_DMA_RECOMMENDED, &pc->flags) && drive->using_dma)
1411 dma_ok = !hwif->dma_setup(drive);
1412
Bartlomiej Zolnierkiewicz2fc57382008-01-25 22:17:13 +01001413 ide_pktcmd_tf_load(drive, IDE_TFLAG_NO_SELECT_MASK |
1414 IDE_TFLAG_OUT_DEVICE, bcount, dma_ok);
1415
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416 if (dma_ok) /* Will begin DMA later */
1417 set_bit(PC_DMA_IN_PROGRESS, &pc->flags);
1418 if (test_bit(IDETAPE_DRQ_INTERRUPT, &tape->flags)) {
Bartlomiej Zolnierkiewiczc1c9dbc2008-02-02 19:56:44 +01001419 ide_execute_command(drive, WIN_PACKETCMD, &idetape_transfer_pc,
1420 IDETAPE_WAIT_CMD, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421 return ide_started;
1422 } else {
1423 hwif->OUTB(WIN_PACKETCMD, IDE_COMMAND_REG);
1424 return idetape_transfer_pc(drive);
1425 }
1426}
1427
1428/*
1429 * General packet command callback function.
1430 */
1431static ide_startstop_t idetape_pc_callback (ide_drive_t *drive)
1432{
1433 idetape_tape_t *tape = drive->driver_data;
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001434
1435 debug_log(DBG_PROCS, "Enter %s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436
1437 idetape_end_request(drive, tape->pc->error ? 0 : 1, 0);
1438 return ide_stopped;
1439}
1440
1441/*
1442 * A mode sense command is used to "sense" tape parameters.
1443 */
1444static void idetape_create_mode_sense_cmd (idetape_pc_t *pc, u8 page_code)
1445{
1446 idetape_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +01001447 pc->c[0] = MODE_SENSE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448 if (page_code != IDETAPE_BLOCK_DESCRIPTOR)
1449 pc->c[1] = 8; /* DBD = 1 - Don't return block descriptors */
1450 pc->c[2] = page_code;
1451 /*
1452 * Changed pc->c[3] to 0 (255 will at best return unused info).
1453 *
1454 * For SCSI this byte is defined as subpage instead of high byte
1455 * of length and some IDE drives seem to interpret it this way
1456 * and return an error when 255 is used.
1457 */
1458 pc->c[3] = 0;
1459 pc->c[4] = 255; /* (We will just discard data in that case) */
1460 if (page_code == IDETAPE_BLOCK_DESCRIPTOR)
1461 pc->request_transfer = 12;
1462 else if (page_code == IDETAPE_CAPABILITIES_PAGE)
1463 pc->request_transfer = 24;
1464 else
1465 pc->request_transfer = 50;
1466 pc->callback = &idetape_pc_callback;
1467}
1468
Borislav Petkov37016ba2008-02-06 02:57:52 +01001469static void idetape_calculate_speeds(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470{
1471 idetape_tape_t *tape = drive->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472
1473 if (time_after(jiffies, tape->controlled_pipeline_head_time + 120 * HZ)) {
1474 tape->controlled_previous_pipeline_head = tape->controlled_last_pipeline_head;
1475 tape->controlled_previous_head_time = tape->controlled_pipeline_head_time;
1476 tape->controlled_last_pipeline_head = tape->pipeline_head;
1477 tape->controlled_pipeline_head_time = jiffies;
1478 }
1479 if (time_after(jiffies, tape->controlled_pipeline_head_time + 60 * HZ))
1480 tape->controlled_pipeline_head_speed = (tape->pipeline_head - tape->controlled_last_pipeline_head) * 32 * HZ / (jiffies - tape->controlled_pipeline_head_time);
1481 else if (time_after(jiffies, tape->controlled_previous_head_time))
1482 tape->controlled_pipeline_head_speed = (tape->pipeline_head - tape->controlled_previous_pipeline_head) * 32 * HZ / (jiffies - tape->controlled_previous_head_time);
1483
1484 if (tape->nr_pending_stages < tape->max_stages /*- 1 */) {
1485 /* -1 for read mode error recovery */
1486 if (time_after(jiffies, tape->uncontrolled_previous_head_time + 10 * HZ)) {
1487 tape->uncontrolled_pipeline_head_time = jiffies;
1488 tape->uncontrolled_pipeline_head_speed = (tape->pipeline_head - tape->uncontrolled_previous_pipeline_head) * 32 * HZ / (jiffies - tape->uncontrolled_previous_head_time);
1489 }
1490 } else {
1491 tape->uncontrolled_previous_head_time = jiffies;
1492 tape->uncontrolled_previous_pipeline_head = tape->pipeline_head;
1493 if (time_after(jiffies, tape->uncontrolled_pipeline_head_time + 30 * HZ)) {
1494 tape->uncontrolled_pipeline_head_time = jiffies;
1495 }
1496 }
1497 tape->pipeline_head_speed = max(tape->uncontrolled_pipeline_head_speed, tape->controlled_pipeline_head_speed);
Borislav Petkov37016ba2008-02-06 02:57:52 +01001498
1499 if (tape->speed_control == 1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500 if (tape->nr_pending_stages >= tape->max_stages / 2)
1501 tape->max_insert_speed = tape->pipeline_head_speed +
1502 (1100 - tape->pipeline_head_speed) * 2 * (tape->nr_pending_stages - tape->max_stages / 2) / tape->max_stages;
1503 else
1504 tape->max_insert_speed = 500 +
1505 (tape->pipeline_head_speed - 500) * 2 * tape->nr_pending_stages / tape->max_stages;
Borislav Petkov37016ba2008-02-06 02:57:52 +01001506
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507 if (tape->nr_pending_stages >= tape->max_stages * 99 / 100)
1508 tape->max_insert_speed = 5000;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509 } else
1510 tape->max_insert_speed = tape->speed_control;
Borislav Petkov37016ba2008-02-06 02:57:52 +01001511
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512 tape->max_insert_speed = max(tape->max_insert_speed, 500);
1513}
1514
1515static ide_startstop_t idetape_media_access_finished (ide_drive_t *drive)
1516{
1517 idetape_tape_t *tape = drive->driver_data;
1518 idetape_pc_t *pc = tape->pc;
Bartlomiej Zolnierkiewicz22c525b2008-01-25 22:17:11 +01001519 u8 stat;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520
Bartlomiej Zolnierkiewiczc47137a2008-02-06 02:57:51 +01001521 stat = ide_read_status(drive);
1522
Bartlomiej Zolnierkiewicz22c525b2008-01-25 22:17:11 +01001523 if (stat & SEEK_STAT) {
1524 if (stat & ERR_STAT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001525 /* Error detected */
Borislav Petkov90699ce2008-02-02 19:56:50 +01001526 if (pc->c[0] != TEST_UNIT_READY)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527 printk(KERN_ERR "ide-tape: %s: I/O error, ",
1528 tape->name);
1529 /* Retry operation */
1530 return idetape_retry_pc(drive);
1531 }
1532 pc->error = 0;
1533 if (tape->failed_pc == pc)
1534 tape->failed_pc = NULL;
1535 } else {
1536 pc->error = IDETAPE_ERROR_GENERAL;
1537 tape->failed_pc = NULL;
1538 }
1539 return pc->callback(drive);
1540}
1541
1542static ide_startstop_t idetape_rw_callback (ide_drive_t *drive)
1543{
1544 idetape_tape_t *tape = drive->driver_data;
1545 struct request *rq = HWGROUP(drive)->rq;
1546 int blocks = tape->pc->actually_transferred / tape->tape_block_size;
1547
1548 tape->avg_size += blocks * tape->tape_block_size;
1549 tape->insert_size += blocks * tape->tape_block_size;
1550 if (tape->insert_size > 1024 * 1024)
1551 tape->measure_insert_time = 1;
1552 if (tape->measure_insert_time) {
1553 tape->measure_insert_time = 0;
1554 tape->insert_time = jiffies;
1555 tape->insert_size = 0;
1556 }
1557 if (time_after(jiffies, tape->insert_time))
1558 tape->insert_speed = tape->insert_size / 1024 * HZ / (jiffies - tape->insert_time);
Marcelo Feitoza Parisi9bae1ff2006-03-28 01:56:46 -08001559 if (time_after_eq(jiffies, tape->avg_time + HZ)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560 tape->avg_speed = tape->avg_size * HZ / (jiffies - tape->avg_time) / 1024;
1561 tape->avg_size = 0;
1562 tape->avg_time = jiffies;
1563 }
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001564 debug_log(DBG_PROCS, "Enter %s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565
1566 tape->first_frame_position += blocks;
1567 rq->current_nr_sectors -= blocks;
1568
1569 if (!tape->pc->error)
1570 idetape_end_request(drive, 1, 0);
1571 else
1572 idetape_end_request(drive, tape->pc->error, 0);
1573 return ide_stopped;
1574}
1575
1576static void idetape_create_read_cmd(idetape_tape_t *tape, idetape_pc_t *pc, unsigned int length, struct idetape_bh *bh)
1577{
1578 idetape_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +01001579 pc->c[0] = READ_6;
Borislav Petkov860ff5e2008-02-02 19:56:50 +01001580 put_unaligned(cpu_to_be32(length), (unsigned int *) &pc->c[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581 pc->c[1] = 1;
1582 pc->callback = &idetape_rw_callback;
1583 pc->bh = bh;
1584 atomic_set(&bh->b_count, 0);
1585 pc->buffer = NULL;
1586 pc->request_transfer = pc->buffer_size = length * tape->tape_block_size;
1587 if (pc->request_transfer == tape->stage_size)
1588 set_bit(PC_DMA_RECOMMENDED, &pc->flags);
1589}
1590
1591static void idetape_create_read_buffer_cmd(idetape_tape_t *tape, idetape_pc_t *pc, unsigned int length, struct idetape_bh *bh)
1592{
1593 int size = 32768;
1594 struct idetape_bh *p = bh;
1595
1596 idetape_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +01001597 pc->c[0] = READ_BUFFER;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001598 pc->c[1] = IDETAPE_RETRIEVE_FAULTY_BLOCK;
1599 pc->c[7] = size >> 8;
1600 pc->c[8] = size & 0xff;
1601 pc->callback = &idetape_pc_callback;
1602 pc->bh = bh;
1603 atomic_set(&bh->b_count, 0);
1604 pc->buffer = NULL;
1605 while (p) {
1606 atomic_set(&p->b_count, 0);
1607 p = p->b_reqnext;
1608 }
1609 pc->request_transfer = pc->buffer_size = size;
1610}
1611
1612static void idetape_create_write_cmd(idetape_tape_t *tape, idetape_pc_t *pc, unsigned int length, struct idetape_bh *bh)
1613{
1614 idetape_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +01001615 pc->c[0] = WRITE_6;
Borislav Petkov860ff5e2008-02-02 19:56:50 +01001616 put_unaligned(cpu_to_be32(length), (unsigned int *) &pc->c[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617 pc->c[1] = 1;
1618 pc->callback = &idetape_rw_callback;
1619 set_bit(PC_WRITING, &pc->flags);
1620 pc->bh = bh;
1621 pc->b_data = bh->b_data;
1622 pc->b_count = atomic_read(&bh->b_count);
1623 pc->buffer = NULL;
1624 pc->request_transfer = pc->buffer_size = length * tape->tape_block_size;
1625 if (pc->request_transfer == tape->stage_size)
1626 set_bit(PC_DMA_RECOMMENDED, &pc->flags);
1627}
1628
1629/*
1630 * idetape_do_request is our request handling function.
1631 */
1632static ide_startstop_t idetape_do_request(ide_drive_t *drive,
1633 struct request *rq, sector_t block)
1634{
1635 idetape_tape_t *tape = drive->driver_data;
1636 idetape_pc_t *pc = NULL;
1637 struct request *postponed_rq = tape->postponed_rq;
Bartlomiej Zolnierkiewicz22c525b2008-01-25 22:17:11 +01001638 u8 stat;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001639
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001640 debug_log(DBG_SENSE, "sector: %ld, nr_sectors: %ld,"
1641 " current_nr_sectors: %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642 rq->sector, rq->nr_sectors, rq->current_nr_sectors);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643
Jens Axboe4aff5e22006-08-10 08:44:47 +02001644 if (!blk_special_request(rq)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001645 /*
1646 * We do not support buffer cache originated requests.
1647 */
1648 printk(KERN_NOTICE "ide-tape: %s: Unsupported request in "
Jens Axboe4aff5e22006-08-10 08:44:47 +02001649 "request queue (%d)\n", drive->name, rq->cmd_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001650 ide_end_request(drive, 0, 0);
1651 return ide_stopped;
1652 }
1653
1654 /*
1655 * Retry a failed packet command
1656 */
1657 if (tape->failed_pc != NULL &&
Borislav Petkov90699ce2008-02-02 19:56:50 +01001658 tape->pc->c[0] == REQUEST_SENSE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001659 return idetape_issue_packet_command(drive, tape->failed_pc);
1660 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001661 if (postponed_rq != NULL)
1662 if (rq != postponed_rq) {
1663 printk(KERN_ERR "ide-tape: ide-tape.c bug - "
1664 "Two DSC requests were queued\n");
1665 idetape_end_request(drive, 0, 0);
1666 return ide_stopped;
1667 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001668
1669 tape->postponed_rq = NULL;
1670
1671 /*
1672 * If the tape is still busy, postpone our request and service
1673 * the other device meanwhile.
1674 */
Bartlomiej Zolnierkiewiczc47137a2008-02-06 02:57:51 +01001675 stat = ide_read_status(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001676
1677 if (!drive->dsc_overlap && !(rq->cmd[0] & REQ_IDETAPE_PC2))
1678 set_bit(IDETAPE_IGNORE_DSC, &tape->flags);
1679
1680 if (drive->post_reset == 1) {
1681 set_bit(IDETAPE_IGNORE_DSC, &tape->flags);
1682 drive->post_reset = 0;
1683 }
1684
Linus Torvalds1da177e2005-04-16 15:20:36 -07001685 if (time_after(jiffies, tape->insert_time))
1686 tape->insert_speed = tape->insert_size / 1024 * HZ / (jiffies - tape->insert_time);
Borislav Petkov37016ba2008-02-06 02:57:52 +01001687 idetape_calculate_speeds(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001688 if (!test_and_clear_bit(IDETAPE_IGNORE_DSC, &tape->flags) &&
Bartlomiej Zolnierkiewicz22c525b2008-01-25 22:17:11 +01001689 (stat & SEEK_STAT) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001690 if (postponed_rq == NULL) {
1691 tape->dsc_polling_start = jiffies;
1692 tape->dsc_polling_frequency = tape->best_dsc_rw_frequency;
1693 tape->dsc_timeout = jiffies + IDETAPE_DSC_RW_TIMEOUT;
1694 } else if (time_after(jiffies, tape->dsc_timeout)) {
1695 printk(KERN_ERR "ide-tape: %s: DSC timeout\n",
1696 tape->name);
1697 if (rq->cmd[0] & REQ_IDETAPE_PC2) {
1698 idetape_media_access_finished(drive);
1699 return ide_stopped;
1700 } else {
1701 return ide_do_reset(drive);
1702 }
Marcelo Feitoza Parisi9bae1ff2006-03-28 01:56:46 -08001703 } else if (time_after(jiffies, tape->dsc_polling_start + IDETAPE_DSC_MA_THRESHOLD))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001704 tape->dsc_polling_frequency = IDETAPE_DSC_MA_SLOW;
1705 idetape_postpone_request(drive);
1706 return ide_stopped;
1707 }
1708 if (rq->cmd[0] & REQ_IDETAPE_READ) {
1709 tape->buffer_head++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001710 tape->postpone_cnt = 0;
1711 pc = idetape_next_pc_storage(drive);
1712 idetape_create_read_cmd(tape, pc, rq->current_nr_sectors, (struct idetape_bh *)rq->special);
1713 goto out;
1714 }
1715 if (rq->cmd[0] & REQ_IDETAPE_WRITE) {
1716 tape->buffer_head++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001717 tape->postpone_cnt = 0;
1718 pc = idetape_next_pc_storage(drive);
1719 idetape_create_write_cmd(tape, pc, rq->current_nr_sectors, (struct idetape_bh *)rq->special);
1720 goto out;
1721 }
1722 if (rq->cmd[0] & REQ_IDETAPE_READ_BUFFER) {
1723 tape->postpone_cnt = 0;
1724 pc = idetape_next_pc_storage(drive);
1725 idetape_create_read_buffer_cmd(tape, pc, rq->current_nr_sectors, (struct idetape_bh *)rq->special);
1726 goto out;
1727 }
1728 if (rq->cmd[0] & REQ_IDETAPE_PC1) {
1729 pc = (idetape_pc_t *) rq->buffer;
1730 rq->cmd[0] &= ~(REQ_IDETAPE_PC1);
1731 rq->cmd[0] |= REQ_IDETAPE_PC2;
1732 goto out;
1733 }
1734 if (rq->cmd[0] & REQ_IDETAPE_PC2) {
1735 idetape_media_access_finished(drive);
1736 return ide_stopped;
1737 }
1738 BUG();
1739out:
1740 return idetape_issue_packet_command(drive, pc);
1741}
1742
1743/*
1744 * Pipeline related functions
1745 */
1746static inline int idetape_pipeline_active (idetape_tape_t *tape)
1747{
1748 int rc1, rc2;
1749
1750 rc1 = test_bit(IDETAPE_PIPELINE_ACTIVE, &tape->flags);
1751 rc2 = (tape->active_data_request != NULL);
1752 return rc1;
1753}
1754
1755/*
1756 * idetape_kmalloc_stage uses __get_free_page to allocate a pipeline
1757 * stage, along with all the necessary small buffers which together make
1758 * a buffer of size tape->stage_size (or a bit more). We attempt to
1759 * combine sequential pages as much as possible.
1760 *
1761 * Returns a pointer to the new allocated stage, or NULL if we
1762 * can't (or don't want to) allocate a stage.
1763 *
1764 * Pipeline stages are optional and are used to increase performance.
1765 * If we can't allocate them, we'll manage without them.
1766 */
1767static idetape_stage_t *__idetape_kmalloc_stage (idetape_tape_t *tape, int full, int clear)
1768{
1769 idetape_stage_t *stage;
1770 struct idetape_bh *prev_bh, *bh;
1771 int pages = tape->pages_per_stage;
1772 char *b_data = NULL;
1773
Robert P. J. Day5cbded52006-12-13 00:35:56 -08001774 if ((stage = kmalloc(sizeof (idetape_stage_t),GFP_KERNEL)) == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001775 return NULL;
1776 stage->next = NULL;
1777
Robert P. J. Day5cbded52006-12-13 00:35:56 -08001778 bh = stage->bh = kmalloc(sizeof(struct idetape_bh), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001779 if (bh == NULL)
1780 goto abort;
1781 bh->b_reqnext = NULL;
1782 if ((bh->b_data = (char *) __get_free_page (GFP_KERNEL)) == NULL)
1783 goto abort;
1784 if (clear)
1785 memset(bh->b_data, 0, PAGE_SIZE);
1786 bh->b_size = PAGE_SIZE;
1787 atomic_set(&bh->b_count, full ? bh->b_size : 0);
1788
1789 while (--pages) {
1790 if ((b_data = (char *) __get_free_page (GFP_KERNEL)) == NULL)
1791 goto abort;
1792 if (clear)
1793 memset(b_data, 0, PAGE_SIZE);
1794 if (bh->b_data == b_data + PAGE_SIZE) {
1795 bh->b_size += PAGE_SIZE;
1796 bh->b_data -= PAGE_SIZE;
1797 if (full)
1798 atomic_add(PAGE_SIZE, &bh->b_count);
1799 continue;
1800 }
1801 if (b_data == bh->b_data + bh->b_size) {
1802 bh->b_size += PAGE_SIZE;
1803 if (full)
1804 atomic_add(PAGE_SIZE, &bh->b_count);
1805 continue;
1806 }
1807 prev_bh = bh;
Robert P. J. Day5cbded52006-12-13 00:35:56 -08001808 if ((bh = kmalloc(sizeof(struct idetape_bh), GFP_KERNEL)) == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001809 free_page((unsigned long) b_data);
1810 goto abort;
1811 }
1812 bh->b_reqnext = NULL;
1813 bh->b_data = b_data;
1814 bh->b_size = PAGE_SIZE;
1815 atomic_set(&bh->b_count, full ? bh->b_size : 0);
1816 prev_bh->b_reqnext = bh;
1817 }
1818 bh->b_size -= tape->excess_bh_size;
1819 if (full)
1820 atomic_sub(tape->excess_bh_size, &bh->b_count);
1821 return stage;
1822abort:
1823 __idetape_kfree_stage(stage);
1824 return NULL;
1825}
1826
1827static idetape_stage_t *idetape_kmalloc_stage (idetape_tape_t *tape)
1828{
1829 idetape_stage_t *cache_stage = tape->cache_stage;
1830
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001831 debug_log(DBG_PROCS, "Enter %s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001832
1833 if (tape->nr_stages >= tape->max_stages)
1834 return NULL;
1835 if (cache_stage != NULL) {
1836 tape->cache_stage = NULL;
1837 return cache_stage;
1838 }
1839 return __idetape_kmalloc_stage(tape, 0, 0);
1840}
1841
Daniel Walkerdcd96372006-06-25 05:47:37 -07001842static 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 -07001843{
1844 struct idetape_bh *bh = tape->bh;
1845 int count;
Daniel Walkerdcd96372006-06-25 05:47:37 -07001846 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001847
1848 while (n) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001849 if (bh == NULL) {
1850 printk(KERN_ERR "ide-tape: bh == NULL in "
1851 "idetape_copy_stage_from_user\n");
Daniel Walkerdcd96372006-06-25 05:47:37 -07001852 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001853 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001854 count = min((unsigned int)(bh->b_size - atomic_read(&bh->b_count)), (unsigned int)n);
Daniel Walkerdcd96372006-06-25 05:47:37 -07001855 if (copy_from_user(bh->b_data + atomic_read(&bh->b_count), buf, count))
1856 ret = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001857 n -= count;
1858 atomic_add(count, &bh->b_count);
1859 buf += count;
1860 if (atomic_read(&bh->b_count) == bh->b_size) {
1861 bh = bh->b_reqnext;
1862 if (bh)
1863 atomic_set(&bh->b_count, 0);
1864 }
1865 }
1866 tape->bh = bh;
Daniel Walkerdcd96372006-06-25 05:47:37 -07001867 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001868}
1869
Daniel Walkerdcd96372006-06-25 05:47:37 -07001870static 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 -07001871{
1872 struct idetape_bh *bh = tape->bh;
1873 int count;
Daniel Walkerdcd96372006-06-25 05:47:37 -07001874 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001875
1876 while (n) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001877 if (bh == NULL) {
1878 printk(KERN_ERR "ide-tape: bh == NULL in "
1879 "idetape_copy_stage_to_user\n");
Daniel Walkerdcd96372006-06-25 05:47:37 -07001880 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001881 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001882 count = min(tape->b_count, n);
Daniel Walkerdcd96372006-06-25 05:47:37 -07001883 if (copy_to_user(buf, tape->b_data, count))
1884 ret = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001885 n -= count;
1886 tape->b_data += count;
1887 tape->b_count -= count;
1888 buf += count;
1889 if (!tape->b_count) {
1890 tape->bh = bh = bh->b_reqnext;
1891 if (bh) {
1892 tape->b_data = bh->b_data;
1893 tape->b_count = atomic_read(&bh->b_count);
1894 }
1895 }
1896 }
Daniel Walkerdcd96372006-06-25 05:47:37 -07001897 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001898}
1899
1900static void idetape_init_merge_stage (idetape_tape_t *tape)
1901{
1902 struct idetape_bh *bh = tape->merge_stage->bh;
1903
1904 tape->bh = bh;
Borislav Petkov54abf372008-02-06 02:57:52 +01001905 if (tape->chrdev_dir == IDETAPE_DIR_WRITE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001906 atomic_set(&bh->b_count, 0);
1907 else {
1908 tape->b_data = bh->b_data;
1909 tape->b_count = atomic_read(&bh->b_count);
1910 }
1911}
1912
1913static void idetape_switch_buffers (idetape_tape_t *tape, idetape_stage_t *stage)
1914{
1915 struct idetape_bh *tmp;
1916
1917 tmp = stage->bh;
1918 stage->bh = tape->merge_stage->bh;
1919 tape->merge_stage->bh = tmp;
1920 idetape_init_merge_stage(tape);
1921}
1922
1923/*
1924 * idetape_add_stage_tail adds a new stage at the end of the pipeline.
1925 */
1926static void idetape_add_stage_tail (ide_drive_t *drive,idetape_stage_t *stage)
1927{
1928 idetape_tape_t *tape = drive->driver_data;
1929 unsigned long flags;
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001930
1931 debug_log(DBG_PROCS, "Enter %s\n", __func__);
1932
Linus Torvalds1da177e2005-04-16 15:20:36 -07001933 spin_lock_irqsave(&tape->spinlock, flags);
1934 stage->next = NULL;
1935 if (tape->last_stage != NULL)
1936 tape->last_stage->next=stage;
1937 else
1938 tape->first_stage = tape->next_stage=stage;
1939 tape->last_stage = stage;
1940 if (tape->next_stage == NULL)
1941 tape->next_stage = tape->last_stage;
1942 tape->nr_stages++;
1943 tape->nr_pending_stages++;
1944 spin_unlock_irqrestore(&tape->spinlock, flags);
1945}
1946
1947/*
1948 * idetape_wait_for_request installs a completion in a pending request
1949 * and sleeps until it is serviced.
1950 *
1951 * The caller should ensure that the request will not be serviced
1952 * before we install the completion (usually by disabling interrupts).
1953 */
1954static void idetape_wait_for_request (ide_drive_t *drive, struct request *rq)
1955{
Peter Zijlstra6e9a4732006-09-30 23:28:10 -07001956 DECLARE_COMPLETION_ONSTACK(wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001957 idetape_tape_t *tape = drive->driver_data;
1958
Jens Axboe4aff5e22006-08-10 08:44:47 +02001959 if (rq == NULL || !blk_special_request(rq)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001960 printk (KERN_ERR "ide-tape: bug: Trying to sleep on non-valid request\n");
1961 return;
1962 }
Jens Axboec00895a2006-09-30 20:29:12 +02001963 rq->end_io_data = &wait;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001964 rq->end_io = blk_end_sync_rq;
1965 spin_unlock_irq(&tape->spinlock);
1966 wait_for_completion(&wait);
1967 /* The stage and its struct request have been deallocated */
1968 spin_lock_irq(&tape->spinlock);
1969}
1970
Borislav Petkova2f5b7f2008-02-06 02:57:51 +01001971static ide_startstop_t idetape_read_position_callback(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001972{
1973 idetape_tape_t *tape = drive->driver_data;
Borislav Petkova2f5b7f2008-02-06 02:57:51 +01001974 u8 *readpos = tape->pc->buffer;
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001975
1976 debug_log(DBG_PROCS, "Enter %s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001977
1978 if (!tape->pc->error) {
Borislav Petkova2f5b7f2008-02-06 02:57:51 +01001979 debug_log(DBG_SENSE, "BOP - %s\n",
1980 (readpos[0] & 0x80) ? "Yes" : "No");
1981 debug_log(DBG_SENSE, "EOP - %s\n",
1982 (readpos[0] & 0x40) ? "Yes" : "No");
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001983
Borislav Petkova2f5b7f2008-02-06 02:57:51 +01001984 if (readpos[0] & 0x4) {
1985 printk(KERN_INFO "ide-tape: Block location is unknown"
1986 "to the tape\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001987 clear_bit(IDETAPE_ADDRESS_VALID, &tape->flags);
1988 idetape_end_request(drive, 0, 0);
1989 } else {
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001990 debug_log(DBG_SENSE, "Block Location - %u\n",
Borislav Petkova2f5b7f2008-02-06 02:57:51 +01001991 be32_to_cpu(*(u32 *)&readpos[4]));
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001992
Borislav Petkova2f5b7f2008-02-06 02:57:51 +01001993 tape->partition = readpos[1];
1994 tape->first_frame_position =
1995 be32_to_cpu(*(u32 *)&readpos[4]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001996 set_bit(IDETAPE_ADDRESS_VALID, &tape->flags);
1997 idetape_end_request(drive, 1, 0);
1998 }
1999 } else {
2000 idetape_end_request(drive, 0, 0);
2001 }
2002 return ide_stopped;
2003}
2004
2005/*
2006 * idetape_create_write_filemark_cmd will:
2007 *
2008 * 1. Write a filemark if write_filemark=1.
2009 * 2. Flush the device buffers without writing a filemark
2010 * if write_filemark=0.
2011 *
2012 */
2013static void idetape_create_write_filemark_cmd (ide_drive_t *drive, idetape_pc_t *pc,int write_filemark)
2014{
2015 idetape_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +01002016 pc->c[0] = WRITE_FILEMARKS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002017 pc->c[4] = write_filemark;
2018 set_bit(PC_WAIT_FOR_DSC, &pc->flags);
2019 pc->callback = &idetape_pc_callback;
2020}
2021
2022static void idetape_create_test_unit_ready_cmd(idetape_pc_t *pc)
2023{
2024 idetape_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +01002025 pc->c[0] = TEST_UNIT_READY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002026 pc->callback = &idetape_pc_callback;
2027}
2028
2029/*
2030 * idetape_queue_pc_tail is based on the following functions:
2031 *
2032 * ide_do_drive_cmd from ide.c
2033 * cdrom_queue_request and cdrom_queue_packet_command from ide-cd.c
2034 *
2035 * We add a special packet command request to the tail of the request
2036 * queue, and wait for it to be serviced.
2037 *
2038 * This is not to be called from within the request handling part
2039 * of the driver ! We allocate here data in the stack, and it is valid
2040 * until the request is finished. This is not the case for the bottom
2041 * part of the driver, where we are always leaving the functions to wait
2042 * for an interrupt or a timer event.
2043 *
2044 * From the bottom part of the driver, we should allocate safe memory
2045 * using idetape_next_pc_storage and idetape_next_rq_storage, and add
2046 * the request to the request list without waiting for it to be serviced !
2047 * In that case, we usually use idetape_queue_pc_head.
2048 */
2049static int __idetape_queue_pc_tail (ide_drive_t *drive, idetape_pc_t *pc)
2050{
2051 struct ide_tape_obj *tape = drive->driver_data;
2052 struct request rq;
2053
2054 idetape_init_rq(&rq, REQ_IDETAPE_PC1);
2055 rq.buffer = (char *) pc;
2056 rq.rq_disk = tape->disk;
2057 return ide_do_drive_cmd(drive, &rq, ide_wait);
2058}
2059
2060static void idetape_create_load_unload_cmd (ide_drive_t *drive, idetape_pc_t *pc,int cmd)
2061{
2062 idetape_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +01002063 pc->c[0] = START_STOP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002064 pc->c[4] = cmd;
2065 set_bit(PC_WAIT_FOR_DSC, &pc->flags);
2066 pc->callback = &idetape_pc_callback;
2067}
2068
2069static int idetape_wait_ready(ide_drive_t *drive, unsigned long timeout)
2070{
2071 idetape_tape_t *tape = drive->driver_data;
2072 idetape_pc_t pc;
2073 int load_attempted = 0;
2074
2075 /*
2076 * Wait for the tape to become ready
2077 */
2078 set_bit(IDETAPE_MEDIUM_PRESENT, &tape->flags);
2079 timeout += jiffies;
2080 while (time_before(jiffies, timeout)) {
2081 idetape_create_test_unit_ready_cmd(&pc);
2082 if (!__idetape_queue_pc_tail(drive, &pc))
2083 return 0;
2084 if ((tape->sense_key == 2 && tape->asc == 4 && tape->ascq == 2)
2085 || (tape->asc == 0x3A)) { /* no media */
2086 if (load_attempted)
2087 return -ENOMEDIUM;
2088 idetape_create_load_unload_cmd(drive, &pc, IDETAPE_LU_LOAD_MASK);
2089 __idetape_queue_pc_tail(drive, &pc);
2090 load_attempted = 1;
2091 /* not about to be ready */
2092 } else if (!(tape->sense_key == 2 && tape->asc == 4 &&
2093 (tape->ascq == 1 || tape->ascq == 8)))
2094 return -EIO;
Nishanth Aravamudan80ce45f2005-09-10 00:27:08 -07002095 msleep(100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002096 }
2097 return -EIO;
2098}
2099
2100static int idetape_queue_pc_tail (ide_drive_t *drive,idetape_pc_t *pc)
2101{
2102 return __idetape_queue_pc_tail(drive, pc);
2103}
2104
2105static int idetape_flush_tape_buffers (ide_drive_t *drive)
2106{
2107 idetape_pc_t pc;
2108 int rc;
2109
2110 idetape_create_write_filemark_cmd(drive, &pc, 0);
2111 if ((rc = idetape_queue_pc_tail(drive, &pc)))
2112 return rc;
2113 idetape_wait_ready(drive, 60 * 5 * HZ);
2114 return 0;
2115}
2116
2117static void idetape_create_read_position_cmd (idetape_pc_t *pc)
2118{
2119 idetape_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +01002120 pc->c[0] = READ_POSITION;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002121 pc->request_transfer = 20;
2122 pc->callback = &idetape_read_position_callback;
2123}
2124
2125static int idetape_read_position (ide_drive_t *drive)
2126{
2127 idetape_tape_t *tape = drive->driver_data;
2128 idetape_pc_t pc;
2129 int position;
2130
Borislav Petkov8004a8c2008-02-06 02:57:51 +01002131 debug_log(DBG_PROCS, "Enter %s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002132
2133 idetape_create_read_position_cmd(&pc);
2134 if (idetape_queue_pc_tail(drive, &pc))
2135 return -1;
2136 position = tape->first_frame_position;
2137 return position;
2138}
2139
2140static void idetape_create_locate_cmd (ide_drive_t *drive, idetape_pc_t *pc, unsigned int block, u8 partition, int skip)
2141{
2142 idetape_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +01002143 pc->c[0] = POSITION_TO_ELEMENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002144 pc->c[1] = 2;
Borislav Petkov860ff5e2008-02-02 19:56:50 +01002145 put_unaligned(cpu_to_be32(block), (unsigned int *) &pc->c[3]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002146 pc->c[8] = partition;
2147 set_bit(PC_WAIT_FOR_DSC, &pc->flags);
2148 pc->callback = &idetape_pc_callback;
2149}
2150
2151static int idetape_create_prevent_cmd (ide_drive_t *drive, idetape_pc_t *pc, int prevent)
2152{
2153 idetape_tape_t *tape = drive->driver_data;
2154
Borislav Petkovb6422012008-02-02 19:56:49 +01002155 /* device supports locking according to capabilities page */
2156 if (!(tape->caps[6] & 0x01))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002157 return 0;
2158
2159 idetape_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +01002160 pc->c[0] = ALLOW_MEDIUM_REMOVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002161 pc->c[4] = prevent;
2162 pc->callback = &idetape_pc_callback;
2163 return 1;
2164}
2165
2166static int __idetape_discard_read_pipeline (ide_drive_t *drive)
2167{
2168 idetape_tape_t *tape = drive->driver_data;
2169 unsigned long flags;
2170 int cnt;
2171
Borislav Petkov54abf372008-02-06 02:57:52 +01002172 if (tape->chrdev_dir != IDETAPE_DIR_READ)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002173 return 0;
2174
2175 /* Remove merge stage. */
2176 cnt = tape->merge_stage_size / tape->tape_block_size;
2177 if (test_and_clear_bit(IDETAPE_FILEMARK, &tape->flags))
2178 ++cnt; /* Filemarks count as 1 sector */
2179 tape->merge_stage_size = 0;
2180 if (tape->merge_stage != NULL) {
2181 __idetape_kfree_stage(tape->merge_stage);
2182 tape->merge_stage = NULL;
2183 }
2184
2185 /* Clear pipeline flags. */
2186 clear_bit(IDETAPE_PIPELINE_ERROR, &tape->flags);
Borislav Petkov54abf372008-02-06 02:57:52 +01002187 tape->chrdev_dir = IDETAPE_DIR_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002188
2189 /* Remove pipeline stages. */
2190 if (tape->first_stage == NULL)
2191 return 0;
2192
2193 spin_lock_irqsave(&tape->spinlock, flags);
2194 tape->next_stage = NULL;
2195 if (idetape_pipeline_active(tape))
2196 idetape_wait_for_request(drive, tape->active_data_request);
2197 spin_unlock_irqrestore(&tape->spinlock, flags);
2198
2199 while (tape->first_stage != NULL) {
2200 struct request *rq_ptr = &tape->first_stage->rq;
2201
2202 cnt += rq_ptr->nr_sectors - rq_ptr->current_nr_sectors;
2203 if (rq_ptr->errors == IDETAPE_ERROR_FILEMARK)
2204 ++cnt;
2205 idetape_remove_stage_head(drive);
2206 }
2207 tape->nr_pending_stages = 0;
2208 tape->max_stages = tape->min_pipeline;
2209 return cnt;
2210}
2211
2212/*
2213 * idetape_position_tape positions the tape to the requested block
2214 * using the LOCATE packet command. A READ POSITION command is then
2215 * issued to check where we are positioned.
2216 *
2217 * Like all higher level operations, we queue the commands at the tail
2218 * of the request queue and wait for their completion.
2219 *
2220 */
2221static int idetape_position_tape (ide_drive_t *drive, unsigned int block, u8 partition, int skip)
2222{
2223 idetape_tape_t *tape = drive->driver_data;
2224 int retval;
2225 idetape_pc_t pc;
2226
Borislav Petkov54abf372008-02-06 02:57:52 +01002227 if (tape->chrdev_dir == IDETAPE_DIR_READ)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002228 __idetape_discard_read_pipeline(drive);
2229 idetape_wait_ready(drive, 60 * 5 * HZ);
2230 idetape_create_locate_cmd(drive, &pc, block, partition, skip);
2231 retval = idetape_queue_pc_tail(drive, &pc);
2232 if (retval)
2233 return (retval);
2234
2235 idetape_create_read_position_cmd(&pc);
2236 return (idetape_queue_pc_tail(drive, &pc));
2237}
2238
2239static void idetape_discard_read_pipeline (ide_drive_t *drive, int restore_position)
2240{
2241 idetape_tape_t *tape = drive->driver_data;
2242 int cnt;
2243 int seek, position;
2244
2245 cnt = __idetape_discard_read_pipeline(drive);
2246 if (restore_position) {
2247 position = idetape_read_position(drive);
2248 seek = position > cnt ? position - cnt : 0;
2249 if (idetape_position_tape(drive, seek, 0, 0)) {
2250 printk(KERN_INFO "ide-tape: %s: position_tape failed in discard_pipeline()\n", tape->name);
2251 return;
2252 }
2253 }
2254}
2255
2256/*
2257 * idetape_queue_rw_tail generates a read/write request for the block
2258 * device interface and wait for it to be serviced.
2259 */
2260static int idetape_queue_rw_tail(ide_drive_t *drive, int cmd, int blocks, struct idetape_bh *bh)
2261{
2262 idetape_tape_t *tape = drive->driver_data;
2263 struct request rq;
2264
Borislav Petkov8004a8c2008-02-06 02:57:51 +01002265 debug_log(DBG_SENSE, "%s: cmd=%d\n", __func__, cmd);
2266
Linus Torvalds1da177e2005-04-16 15:20:36 -07002267 if (idetape_pipeline_active(tape)) {
Borislav Petkov8004a8c2008-02-06 02:57:51 +01002268 printk(KERN_ERR "ide-tape: bug: the pipeline is active in %s\n",
2269 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002270 return (0);
2271 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002272
2273 idetape_init_rq(&rq, cmd);
2274 rq.rq_disk = tape->disk;
2275 rq.special = (void *)bh;
2276 rq.sector = tape->first_frame_position;
2277 rq.nr_sectors = rq.current_nr_sectors = blocks;
2278 (void) ide_do_drive_cmd(drive, &rq, ide_wait);
2279
2280 if ((cmd & (REQ_IDETAPE_READ | REQ_IDETAPE_WRITE)) == 0)
2281 return 0;
2282
2283 if (tape->merge_stage)
2284 idetape_init_merge_stage(tape);
2285 if (rq.errors == IDETAPE_ERROR_GENERAL)
2286 return -EIO;
2287 return (tape->tape_block_size * (blocks-rq.current_nr_sectors));
2288}
2289
2290/*
2291 * idetape_insert_pipeline_into_queue is used to start servicing the
2292 * pipeline stages, starting from tape->next_stage.
2293 */
2294static void idetape_insert_pipeline_into_queue (ide_drive_t *drive)
2295{
2296 idetape_tape_t *tape = drive->driver_data;
2297
2298 if (tape->next_stage == NULL)
2299 return;
2300 if (!idetape_pipeline_active(tape)) {
2301 set_bit(IDETAPE_PIPELINE_ACTIVE, &tape->flags);
Borislav Petkov419d4742008-02-02 19:56:51 +01002302 idetape_activate_next_stage(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002303 (void) ide_do_drive_cmd(drive, tape->active_data_request, ide_end);
2304 }
2305}
2306
2307static void idetape_create_inquiry_cmd (idetape_pc_t *pc)
2308{
2309 idetape_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +01002310 pc->c[0] = INQUIRY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002311 pc->c[4] = pc->request_transfer = 254;
2312 pc->callback = &idetape_pc_callback;
2313}
2314
2315static void idetape_create_rewind_cmd (ide_drive_t *drive, idetape_pc_t *pc)
2316{
2317 idetape_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +01002318 pc->c[0] = REZERO_UNIT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002319 set_bit(PC_WAIT_FOR_DSC, &pc->flags);
2320 pc->callback = &idetape_pc_callback;
2321}
2322
Linus Torvalds1da177e2005-04-16 15:20:36 -07002323static void idetape_create_erase_cmd (idetape_pc_t *pc)
2324{
2325 idetape_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +01002326 pc->c[0] = ERASE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002327 pc->c[1] = 1;
2328 set_bit(PC_WAIT_FOR_DSC, &pc->flags);
2329 pc->callback = &idetape_pc_callback;
2330}
2331
2332static void idetape_create_space_cmd (idetape_pc_t *pc,int count, u8 cmd)
2333{
2334 idetape_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +01002335 pc->c[0] = SPACE;
Borislav Petkov860ff5e2008-02-02 19:56:50 +01002336 put_unaligned(cpu_to_be32(count), (unsigned int *) &pc->c[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002337 pc->c[1] = cmd;
2338 set_bit(PC_WAIT_FOR_DSC, &pc->flags);
2339 pc->callback = &idetape_pc_callback;
2340}
2341
2342static void idetape_wait_first_stage (ide_drive_t *drive)
2343{
2344 idetape_tape_t *tape = drive->driver_data;
2345 unsigned long flags;
2346
2347 if (tape->first_stage == NULL)
2348 return;
2349 spin_lock_irqsave(&tape->spinlock, flags);
2350 if (tape->active_stage == tape->first_stage)
2351 idetape_wait_for_request(drive, tape->active_data_request);
2352 spin_unlock_irqrestore(&tape->spinlock, flags);
2353}
2354
2355/*
2356 * idetape_add_chrdev_write_request tries to add a character device
2357 * originated write request to our pipeline. In case we don't succeed,
2358 * we revert to non-pipelined operation mode for this request.
2359 *
2360 * 1. Try to allocate a new pipeline stage.
2361 * 2. If we can't, wait for more and more requests to be serviced
2362 * and try again each time.
2363 * 3. If we still can't allocate a stage, fallback to
2364 * non-pipelined operation mode for this request.
2365 */
2366static int idetape_add_chrdev_write_request (ide_drive_t *drive, int blocks)
2367{
2368 idetape_tape_t *tape = drive->driver_data;
2369 idetape_stage_t *new_stage;
2370 unsigned long flags;
2371 struct request *rq;
2372
Borislav Petkov8004a8c2008-02-06 02:57:51 +01002373 debug_log(DBG_CHRDEV, "Enter %s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002374
2375 /*
2376 * Attempt to allocate a new stage.
2377 * Pay special attention to possible race conditions.
2378 */
2379 while ((new_stage = idetape_kmalloc_stage(tape)) == NULL) {
2380 spin_lock_irqsave(&tape->spinlock, flags);
2381 if (idetape_pipeline_active(tape)) {
2382 idetape_wait_for_request(drive, tape->active_data_request);
2383 spin_unlock_irqrestore(&tape->spinlock, flags);
2384 } else {
2385 spin_unlock_irqrestore(&tape->spinlock, flags);
2386 idetape_insert_pipeline_into_queue(drive);
2387 if (idetape_pipeline_active(tape))
2388 continue;
2389 /*
2390 * Linux is short on memory. Fallback to
2391 * non-pipelined operation mode for this request.
2392 */
2393 return idetape_queue_rw_tail(drive, REQ_IDETAPE_WRITE, blocks, tape->merge_stage->bh);
2394 }
2395 }
2396 rq = &new_stage->rq;
2397 idetape_init_rq(rq, REQ_IDETAPE_WRITE);
2398 /* Doesn't actually matter - We always assume sequential access */
2399 rq->sector = tape->first_frame_position;
2400 rq->nr_sectors = rq->current_nr_sectors = blocks;
2401
2402 idetape_switch_buffers(tape, new_stage);
2403 idetape_add_stage_tail(drive, new_stage);
2404 tape->pipeline_head++;
Borislav Petkov37016ba2008-02-06 02:57:52 +01002405 idetape_calculate_speeds(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002406
2407 /*
2408 * Estimate whether the tape has stopped writing by checking
2409 * if our write pipeline is currently empty. If we are not
2410 * writing anymore, wait for the pipeline to be full enough
2411 * (90%) before starting to service requests, so that we will
2412 * be able to keep up with the higher speeds of the tape.
2413 */
2414 if (!idetape_pipeline_active(tape)) {
2415 if (tape->nr_stages >= tape->max_stages * 9 / 10 ||
2416 tape->nr_stages >= tape->max_stages - tape->uncontrolled_pipeline_head_speed * 3 * 1024 / tape->tape_block_size) {
2417 tape->measure_insert_time = 1;
2418 tape->insert_time = jiffies;
2419 tape->insert_size = 0;
2420 tape->insert_speed = 0;
2421 idetape_insert_pipeline_into_queue(drive);
2422 }
2423 }
2424 if (test_and_clear_bit(IDETAPE_PIPELINE_ERROR, &tape->flags))
2425 /* Return a deferred error */
2426 return -EIO;
2427 return blocks;
2428}
2429
2430/*
2431 * idetape_wait_for_pipeline will wait until all pending pipeline
2432 * requests are serviced. Typically called on device close.
2433 */
2434static void idetape_wait_for_pipeline (ide_drive_t *drive)
2435{
2436 idetape_tape_t *tape = drive->driver_data;
2437 unsigned long flags;
2438
2439 while (tape->next_stage || idetape_pipeline_active(tape)) {
2440 idetape_insert_pipeline_into_queue(drive);
2441 spin_lock_irqsave(&tape->spinlock, flags);
2442 if (idetape_pipeline_active(tape))
2443 idetape_wait_for_request(drive, tape->active_data_request);
2444 spin_unlock_irqrestore(&tape->spinlock, flags);
2445 }
2446}
2447
2448static void idetape_empty_write_pipeline (ide_drive_t *drive)
2449{
2450 idetape_tape_t *tape = drive->driver_data;
2451 int blocks, min;
2452 struct idetape_bh *bh;
Borislav Petkov55a5d292008-02-02 19:56:49 +01002453
Borislav Petkov54abf372008-02-06 02:57:52 +01002454 if (tape->chrdev_dir != IDETAPE_DIR_WRITE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002455 printk(KERN_ERR "ide-tape: bug: Trying to empty write pipeline, but we are not writing.\n");
2456 return;
2457 }
2458 if (tape->merge_stage_size > tape->stage_size) {
2459 printk(KERN_ERR "ide-tape: bug: merge_buffer too big\n");
2460 tape->merge_stage_size = tape->stage_size;
2461 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002462 if (tape->merge_stage_size) {
2463 blocks = tape->merge_stage_size / tape->tape_block_size;
2464 if (tape->merge_stage_size % tape->tape_block_size) {
2465 unsigned int i;
2466
2467 blocks++;
2468 i = tape->tape_block_size - tape->merge_stage_size % tape->tape_block_size;
2469 bh = tape->bh->b_reqnext;
2470 while (bh) {
2471 atomic_set(&bh->b_count, 0);
2472 bh = bh->b_reqnext;
2473 }
2474 bh = tape->bh;
2475 while (i) {
2476 if (bh == NULL) {
2477
2478 printk(KERN_INFO "ide-tape: bug, bh NULL\n");
2479 break;
2480 }
2481 min = min(i, (unsigned int)(bh->b_size - atomic_read(&bh->b_count)));
2482 memset(bh->b_data + atomic_read(&bh->b_count), 0, min);
2483 atomic_add(min, &bh->b_count);
2484 i -= min;
2485 bh = bh->b_reqnext;
2486 }
2487 }
2488 (void) idetape_add_chrdev_write_request(drive, blocks);
2489 tape->merge_stage_size = 0;
2490 }
2491 idetape_wait_for_pipeline(drive);
2492 if (tape->merge_stage != NULL) {
2493 __idetape_kfree_stage(tape->merge_stage);
2494 tape->merge_stage = NULL;
2495 }
2496 clear_bit(IDETAPE_PIPELINE_ERROR, &tape->flags);
Borislav Petkov54abf372008-02-06 02:57:52 +01002497 tape->chrdev_dir = IDETAPE_DIR_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002498
2499 /*
2500 * On the next backup, perform the feedback loop again.
2501 * (I don't want to keep sense information between backups,
2502 * as some systems are constantly on, and the system load
2503 * can be totally different on the next backup).
2504 */
2505 tape->max_stages = tape->min_pipeline;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002506 if (tape->first_stage != NULL ||
2507 tape->next_stage != NULL ||
2508 tape->last_stage != NULL ||
2509 tape->nr_stages != 0) {
2510 printk(KERN_ERR "ide-tape: ide-tape pipeline bug, "
2511 "first_stage %p, next_stage %p, "
2512 "last_stage %p, nr_stages %d\n",
2513 tape->first_stage, tape->next_stage,
2514 tape->last_stage, tape->nr_stages);
2515 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002516}
2517
2518static void idetape_restart_speed_control (ide_drive_t *drive)
2519{
2520 idetape_tape_t *tape = drive->driver_data;
2521
2522 tape->restart_speed_control_req = 0;
2523 tape->pipeline_head = 0;
Borislav Petkov41f81d542008-02-06 02:57:52 +01002524 tape->controlled_last_pipeline_head = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002525 tape->controlled_previous_pipeline_head = tape->uncontrolled_previous_pipeline_head = 0;
2526 tape->pipeline_head_speed = tape->controlled_pipeline_head_speed = 5000;
2527 tape->uncontrolled_pipeline_head_speed = 0;
2528 tape->controlled_pipeline_head_time = tape->uncontrolled_pipeline_head_time = jiffies;
2529 tape->controlled_previous_head_time = tape->uncontrolled_previous_head_time = jiffies;
2530}
2531
2532static int idetape_initiate_read (ide_drive_t *drive, int max_stages)
2533{
2534 idetape_tape_t *tape = drive->driver_data;
2535 idetape_stage_t *new_stage;
2536 struct request rq;
2537 int bytes_read;
Borislav Petkovb6422012008-02-02 19:56:49 +01002538 u16 blocks = *(u16 *)&tape->caps[12];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002539
2540 /* Initialize read operation */
Borislav Petkov54abf372008-02-06 02:57:52 +01002541 if (tape->chrdev_dir != IDETAPE_DIR_READ) {
2542 if (tape->chrdev_dir == IDETAPE_DIR_WRITE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002543 idetape_empty_write_pipeline(drive);
2544 idetape_flush_tape_buffers(drive);
2545 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002546 if (tape->merge_stage || tape->merge_stage_size) {
2547 printk (KERN_ERR "ide-tape: merge_stage_size should be 0 now\n");
2548 tape->merge_stage_size = 0;
2549 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002550 if ((tape->merge_stage = __idetape_kmalloc_stage(tape, 0, 0)) == NULL)
2551 return -ENOMEM;
Borislav Petkov54abf372008-02-06 02:57:52 +01002552 tape->chrdev_dir = IDETAPE_DIR_READ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002553
2554 /*
2555 * Issue a read 0 command to ensure that DSC handshake
2556 * is switched from completion mode to buffer available
2557 * mode.
2558 * No point in issuing this if DSC overlap isn't supported,
2559 * some drives (Seagate STT3401A) will return an error.
2560 */
2561 if (drive->dsc_overlap) {
2562 bytes_read = idetape_queue_rw_tail(drive, REQ_IDETAPE_READ, 0, tape->merge_stage->bh);
2563 if (bytes_read < 0) {
2564 __idetape_kfree_stage(tape->merge_stage);
2565 tape->merge_stage = NULL;
Borislav Petkov54abf372008-02-06 02:57:52 +01002566 tape->chrdev_dir = IDETAPE_DIR_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002567 return bytes_read;
2568 }
2569 }
2570 }
2571 if (tape->restart_speed_control_req)
2572 idetape_restart_speed_control(drive);
2573 idetape_init_rq(&rq, REQ_IDETAPE_READ);
2574 rq.sector = tape->first_frame_position;
2575 rq.nr_sectors = rq.current_nr_sectors = blocks;
2576 if (!test_bit(IDETAPE_PIPELINE_ERROR, &tape->flags) &&
2577 tape->nr_stages < max_stages) {
2578 new_stage = idetape_kmalloc_stage(tape);
2579 while (new_stage != NULL) {
2580 new_stage->rq = rq;
2581 idetape_add_stage_tail(drive, new_stage);
2582 if (tape->nr_stages >= max_stages)
2583 break;
2584 new_stage = idetape_kmalloc_stage(tape);
2585 }
2586 }
2587 if (!idetape_pipeline_active(tape)) {
2588 if (tape->nr_pending_stages >= 3 * max_stages / 4) {
2589 tape->measure_insert_time = 1;
2590 tape->insert_time = jiffies;
2591 tape->insert_size = 0;
2592 tape->insert_speed = 0;
2593 idetape_insert_pipeline_into_queue(drive);
2594 }
2595 }
2596 return 0;
2597}
2598
2599/*
2600 * idetape_add_chrdev_read_request is called from idetape_chrdev_read
2601 * to service a character device read request and add read-ahead
2602 * requests to our pipeline.
2603 */
2604static int idetape_add_chrdev_read_request (ide_drive_t *drive,int blocks)
2605{
2606 idetape_tape_t *tape = drive->driver_data;
2607 unsigned long flags;
2608 struct request *rq_ptr;
2609 int bytes_read;
2610
Borislav Petkov8004a8c2008-02-06 02:57:51 +01002611 debug_log(DBG_PROCS, "Enter %s, %d blocks\n", __func__, blocks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002612
2613 /*
2614 * If we are at a filemark, return a read length of 0
2615 */
2616 if (test_bit(IDETAPE_FILEMARK, &tape->flags))
2617 return 0;
2618
2619 /*
2620 * Wait for the next block to be available at the head
2621 * of the pipeline
2622 */
2623 idetape_initiate_read(drive, tape->max_stages);
2624 if (tape->first_stage == NULL) {
2625 if (test_bit(IDETAPE_PIPELINE_ERROR, &tape->flags))
2626 return 0;
2627 return idetape_queue_rw_tail(drive, REQ_IDETAPE_READ, blocks, tape->merge_stage->bh);
2628 }
2629 idetape_wait_first_stage(drive);
2630 rq_ptr = &tape->first_stage->rq;
2631 bytes_read = tape->tape_block_size * (rq_ptr->nr_sectors - rq_ptr->current_nr_sectors);
2632 rq_ptr->nr_sectors = rq_ptr->current_nr_sectors = 0;
2633
2634
2635 if (rq_ptr->errors == IDETAPE_ERROR_EOD)
2636 return 0;
2637 else {
2638 idetape_switch_buffers(tape, tape->first_stage);
2639 if (rq_ptr->errors == IDETAPE_ERROR_FILEMARK)
2640 set_bit(IDETAPE_FILEMARK, &tape->flags);
2641 spin_lock_irqsave(&tape->spinlock, flags);
2642 idetape_remove_stage_head(drive);
2643 spin_unlock_irqrestore(&tape->spinlock, flags);
2644 tape->pipeline_head++;
Borislav Petkov37016ba2008-02-06 02:57:52 +01002645 idetape_calculate_speeds(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002646 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002647 if (bytes_read > blocks * tape->tape_block_size) {
2648 printk(KERN_ERR "ide-tape: bug: trying to return more bytes than requested\n");
2649 bytes_read = blocks * tape->tape_block_size;
2650 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002651 return (bytes_read);
2652}
2653
2654static void idetape_pad_zeros (ide_drive_t *drive, int bcount)
2655{
2656 idetape_tape_t *tape = drive->driver_data;
2657 struct idetape_bh *bh;
2658 int blocks;
2659
2660 while (bcount) {
2661 unsigned int count;
2662
2663 bh = tape->merge_stage->bh;
2664 count = min(tape->stage_size, bcount);
2665 bcount -= count;
2666 blocks = count / tape->tape_block_size;
2667 while (count) {
2668 atomic_set(&bh->b_count, min(count, (unsigned int)bh->b_size));
2669 memset(bh->b_data, 0, atomic_read(&bh->b_count));
2670 count -= atomic_read(&bh->b_count);
2671 bh = bh->b_reqnext;
2672 }
2673 idetape_queue_rw_tail(drive, REQ_IDETAPE_WRITE, blocks, tape->merge_stage->bh);
2674 }
2675}
2676
2677static int idetape_pipeline_size (ide_drive_t *drive)
2678{
2679 idetape_tape_t *tape = drive->driver_data;
2680 idetape_stage_t *stage;
2681 struct request *rq;
2682 int size = 0;
2683
2684 idetape_wait_for_pipeline(drive);
2685 stage = tape->first_stage;
2686 while (stage != NULL) {
2687 rq = &stage->rq;
2688 size += tape->tape_block_size * (rq->nr_sectors-rq->current_nr_sectors);
2689 if (rq->errors == IDETAPE_ERROR_FILEMARK)
2690 size += tape->tape_block_size;
2691 stage = stage->next;
2692 }
2693 size += tape->merge_stage_size;
2694 return size;
2695}
2696
2697/*
2698 * Rewinds the tape to the Beginning Of the current Partition (BOP).
2699 *
2700 * We currently support only one partition.
2701 */
2702static int idetape_rewind_tape (ide_drive_t *drive)
2703{
2704 int retval;
2705 idetape_pc_t pc;
Borislav Petkov8004a8c2008-02-06 02:57:51 +01002706 idetape_tape_t *tape;
2707 tape = drive->driver_data;
2708
2709 debug_log(DBG_SENSE, "Enter %s\n", __func__);
2710
Linus Torvalds1da177e2005-04-16 15:20:36 -07002711 idetape_create_rewind_cmd(drive, &pc);
2712 retval = idetape_queue_pc_tail(drive, &pc);
2713 if (retval)
2714 return retval;
2715
2716 idetape_create_read_position_cmd(&pc);
2717 retval = idetape_queue_pc_tail(drive, &pc);
2718 if (retval)
2719 return retval;
2720 return 0;
2721}
2722
2723/*
2724 * Our special ide-tape ioctl's.
2725 *
2726 * Currently there aren't any ioctl's.
2727 * mtio.h compatible commands should be issued to the character device
2728 * interface.
2729 */
2730static int idetape_blkdev_ioctl(ide_drive_t *drive, unsigned int cmd, unsigned long arg)
2731{
2732 idetape_tape_t *tape = drive->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002733 void __user *argp = (void __user *)arg;
2734
Borislav Petkovd59823f2008-02-02 19:56:51 +01002735 struct idetape_config {
2736 int dsc_rw_frequency;
2737 int dsc_media_access_frequency;
2738 int nr_stages;
2739 } config;
2740
Borislav Petkov8004a8c2008-02-06 02:57:51 +01002741 debug_log(DBG_PROCS, "Enter %s\n", __func__);
2742
Linus Torvalds1da177e2005-04-16 15:20:36 -07002743 switch (cmd) {
2744 case 0x0340:
Borislav Petkovd59823f2008-02-02 19:56:51 +01002745 if (copy_from_user(&config, argp, sizeof(config)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002746 return -EFAULT;
2747 tape->best_dsc_rw_frequency = config.dsc_rw_frequency;
2748 tape->max_stages = config.nr_stages;
2749 break;
2750 case 0x0350:
2751 config.dsc_rw_frequency = (int) tape->best_dsc_rw_frequency;
2752 config.nr_stages = tape->max_stages;
Borislav Petkovd59823f2008-02-02 19:56:51 +01002753 if (copy_to_user(argp, &config, sizeof(config)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002754 return -EFAULT;
2755 break;
2756 default:
2757 return -EIO;
2758 }
2759 return 0;
2760}
2761
2762/*
2763 * idetape_space_over_filemarks is now a bit more complicated than just
2764 * passing the command to the tape since we may have crossed some
2765 * filemarks during our pipelined read-ahead mode.
2766 *
2767 * As a minor side effect, the pipeline enables us to support MTFSFM when
2768 * the filemark is in our internal pipeline even if the tape doesn't
2769 * support spacing over filemarks in the reverse direction.
2770 */
2771static int idetape_space_over_filemarks (ide_drive_t *drive,short mt_op,int mt_count)
2772{
2773 idetape_tape_t *tape = drive->driver_data;
2774 idetape_pc_t pc;
2775 unsigned long flags;
2776 int retval,count=0;
Borislav Petkovb6422012008-02-02 19:56:49 +01002777 int sprev = !!(tape->caps[4] & 0x20);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002778
2779 if (mt_count == 0)
2780 return 0;
2781 if (MTBSF == mt_op || MTBSFM == mt_op) {
Borislav Petkovb6422012008-02-02 19:56:49 +01002782 if (!sprev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002783 return -EIO;
2784 mt_count = - mt_count;
2785 }
2786
Borislav Petkov54abf372008-02-06 02:57:52 +01002787 if (tape->chrdev_dir == IDETAPE_DIR_READ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002788 /*
2789 * We have a read-ahead buffer. Scan it for crossed
2790 * filemarks.
2791 */
2792 tape->merge_stage_size = 0;
2793 if (test_and_clear_bit(IDETAPE_FILEMARK, &tape->flags))
2794 ++count;
2795 while (tape->first_stage != NULL) {
2796 if (count == mt_count) {
2797 if (mt_op == MTFSFM)
2798 set_bit(IDETAPE_FILEMARK, &tape->flags);
2799 return 0;
2800 }
2801 spin_lock_irqsave(&tape->spinlock, flags);
2802 if (tape->first_stage == tape->active_stage) {
2803 /*
2804 * We have reached the active stage in the read pipeline.
2805 * There is no point in allowing the drive to continue
2806 * reading any farther, so we stop the pipeline.
2807 *
2808 * This section should be moved to a separate subroutine,
2809 * because a similar function is performed in
2810 * __idetape_discard_read_pipeline(), for example.
2811 */
2812 tape->next_stage = NULL;
2813 spin_unlock_irqrestore(&tape->spinlock, flags);
2814 idetape_wait_first_stage(drive);
2815 tape->next_stage = tape->first_stage->next;
2816 } else
2817 spin_unlock_irqrestore(&tape->spinlock, flags);
2818 if (tape->first_stage->rq.errors == IDETAPE_ERROR_FILEMARK)
2819 ++count;
2820 idetape_remove_stage_head(drive);
2821 }
2822 idetape_discard_read_pipeline(drive, 0);
2823 }
2824
2825 /*
2826 * The filemark was not found in our internal pipeline.
2827 * Now we can issue the space command.
2828 */
2829 switch (mt_op) {
2830 case MTFSF:
2831 case MTBSF:
2832 idetape_create_space_cmd(&pc,mt_count-count,IDETAPE_SPACE_OVER_FILEMARK);
2833 return (idetape_queue_pc_tail(drive, &pc));
2834 case MTFSFM:
2835 case MTBSFM:
Borislav Petkovb6422012008-02-02 19:56:49 +01002836 if (!sprev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002837 return (-EIO);
2838 retval = idetape_space_over_filemarks(drive, MTFSF, mt_count-count);
2839 if (retval) return (retval);
2840 count = (MTBSFM == mt_op ? 1 : -1);
2841 return (idetape_space_over_filemarks(drive, MTFSF, count));
2842 default:
2843 printk(KERN_ERR "ide-tape: MTIO operation %d not supported\n",mt_op);
2844 return (-EIO);
2845 }
2846}
2847
2848
2849/*
2850 * Our character device read / write functions.
2851 *
2852 * The tape is optimized to maximize throughput when it is transferring
2853 * an integral number of the "continuous transfer limit", which is
2854 * a parameter of the specific tape (26 KB on my particular tape).
2855 * (32 kB for Onstream)
2856 *
2857 * As of version 1.3 of the driver, the character device provides an
2858 * abstract continuous view of the media - any mix of block sizes (even 1
2859 * byte) on the same backup/restore procedure is supported. The driver
2860 * will internally convert the requests to the recommended transfer unit,
2861 * so that an unmatch between the user's block size to the recommended
2862 * size will only result in a (slightly) increased driver overhead, but
2863 * will no longer hit performance.
2864 * This is not applicable to Onstream.
2865 */
2866static ssize_t idetape_chrdev_read (struct file *file, char __user *buf,
2867 size_t count, loff_t *ppos)
2868{
2869 struct ide_tape_obj *tape = ide_tape_f(file);
2870 ide_drive_t *drive = tape->drive;
2871 ssize_t bytes_read,temp, actually_read = 0, rc;
Daniel Walkerdcd96372006-06-25 05:47:37 -07002872 ssize_t ret = 0;
Borislav Petkovb6422012008-02-02 19:56:49 +01002873 u16 ctl = *(u16 *)&tape->caps[12];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002874
Borislav Petkov8004a8c2008-02-06 02:57:51 +01002875 debug_log(DBG_CHRDEV, "Enter %s, count %Zd\n", __func__, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002876
Borislav Petkov54abf372008-02-06 02:57:52 +01002877 if (tape->chrdev_dir != IDETAPE_DIR_READ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002878 if (test_bit(IDETAPE_DETECT_BS, &tape->flags))
2879 if (count > tape->tape_block_size &&
2880 (count % tape->tape_block_size) == 0)
2881 tape->user_bs_factor = count / tape->tape_block_size;
2882 }
2883 if ((rc = idetape_initiate_read(drive, tape->max_stages)) < 0)
2884 return rc;
2885 if (count == 0)
2886 return (0);
2887 if (tape->merge_stage_size) {
2888 actually_read = min((unsigned int)(tape->merge_stage_size), (unsigned int)count);
Daniel Walkerdcd96372006-06-25 05:47:37 -07002889 if (idetape_copy_stage_to_user(tape, buf, tape->merge_stage, actually_read))
2890 ret = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002891 buf += actually_read;
2892 tape->merge_stage_size -= actually_read;
2893 count -= actually_read;
2894 }
2895 while (count >= tape->stage_size) {
Borislav Petkovb6422012008-02-02 19:56:49 +01002896 bytes_read = idetape_add_chrdev_read_request(drive, ctl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002897 if (bytes_read <= 0)
2898 goto finish;
Daniel Walkerdcd96372006-06-25 05:47:37 -07002899 if (idetape_copy_stage_to_user(tape, buf, tape->merge_stage, bytes_read))
2900 ret = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002901 buf += bytes_read;
2902 count -= bytes_read;
2903 actually_read += bytes_read;
2904 }
2905 if (count) {
Borislav Petkovb6422012008-02-02 19:56:49 +01002906 bytes_read = idetape_add_chrdev_read_request(drive, ctl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002907 if (bytes_read <= 0)
2908 goto finish;
2909 temp = min((unsigned long)count, (unsigned long)bytes_read);
Daniel Walkerdcd96372006-06-25 05:47:37 -07002910 if (idetape_copy_stage_to_user(tape, buf, tape->merge_stage, temp))
2911 ret = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002912 actually_read += temp;
2913 tape->merge_stage_size = bytes_read-temp;
2914 }
2915finish:
2916 if (!actually_read && test_bit(IDETAPE_FILEMARK, &tape->flags)) {
Borislav Petkov8004a8c2008-02-06 02:57:51 +01002917 debug_log(DBG_SENSE, "%s: spacing over filemark\n", tape->name);
2918
Linus Torvalds1da177e2005-04-16 15:20:36 -07002919 idetape_space_over_filemarks(drive, MTFSF, 1);
2920 return 0;
2921 }
Daniel Walkerdcd96372006-06-25 05:47:37 -07002922
2923 return (ret) ? ret : actually_read;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002924}
2925
2926static ssize_t idetape_chrdev_write (struct file *file, const char __user *buf,
2927 size_t count, loff_t *ppos)
2928{
2929 struct ide_tape_obj *tape = ide_tape_f(file);
2930 ide_drive_t *drive = tape->drive;
Daniel Walkerdcd96372006-06-25 05:47:37 -07002931 ssize_t actually_written = 0;
2932 ssize_t ret = 0;
Borislav Petkovb6422012008-02-02 19:56:49 +01002933 u16 ctl = *(u16 *)&tape->caps[12];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002934
2935 /* The drive is write protected. */
2936 if (tape->write_prot)
2937 return -EACCES;
2938
Borislav Petkov8004a8c2008-02-06 02:57:51 +01002939 debug_log(DBG_CHRDEV, "Enter %s, count %Zd\n", __func__, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002940
2941 /* Initialize write operation */
Borislav Petkov54abf372008-02-06 02:57:52 +01002942 if (tape->chrdev_dir != IDETAPE_DIR_WRITE) {
2943 if (tape->chrdev_dir == IDETAPE_DIR_READ)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002944 idetape_discard_read_pipeline(drive, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002945 if (tape->merge_stage || tape->merge_stage_size) {
2946 printk(KERN_ERR "ide-tape: merge_stage_size "
2947 "should be 0 now\n");
2948 tape->merge_stage_size = 0;
2949 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002950 if ((tape->merge_stage = __idetape_kmalloc_stage(tape, 0, 0)) == NULL)
2951 return -ENOMEM;
Borislav Petkov54abf372008-02-06 02:57:52 +01002952 tape->chrdev_dir = IDETAPE_DIR_WRITE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002953 idetape_init_merge_stage(tape);
2954
2955 /*
2956 * Issue a write 0 command to ensure that DSC handshake
2957 * is switched from completion mode to buffer available
2958 * mode.
2959 * No point in issuing this if DSC overlap isn't supported,
2960 * some drives (Seagate STT3401A) will return an error.
2961 */
2962 if (drive->dsc_overlap) {
Daniel Walkerdcd96372006-06-25 05:47:37 -07002963 ssize_t retval = idetape_queue_rw_tail(drive, REQ_IDETAPE_WRITE, 0, tape->merge_stage->bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002964 if (retval < 0) {
2965 __idetape_kfree_stage(tape->merge_stage);
2966 tape->merge_stage = NULL;
Borislav Petkov54abf372008-02-06 02:57:52 +01002967 tape->chrdev_dir = IDETAPE_DIR_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002968 return retval;
2969 }
2970 }
2971 }
2972 if (count == 0)
2973 return (0);
2974 if (tape->restart_speed_control_req)
2975 idetape_restart_speed_control(drive);
2976 if (tape->merge_stage_size) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002977 if (tape->merge_stage_size >= tape->stage_size) {
2978 printk(KERN_ERR "ide-tape: bug: merge buffer too big\n");
2979 tape->merge_stage_size = 0;
2980 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002981 actually_written = min((unsigned int)(tape->stage_size - tape->merge_stage_size), (unsigned int)count);
Daniel Walkerdcd96372006-06-25 05:47:37 -07002982 if (idetape_copy_stage_from_user(tape, tape->merge_stage, buf, actually_written))
2983 ret = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002984 buf += actually_written;
2985 tape->merge_stage_size += actually_written;
2986 count -= actually_written;
2987
2988 if (tape->merge_stage_size == tape->stage_size) {
Daniel Walkerdcd96372006-06-25 05:47:37 -07002989 ssize_t retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002990 tape->merge_stage_size = 0;
Borislav Petkovb6422012008-02-02 19:56:49 +01002991 retval = idetape_add_chrdev_write_request(drive, ctl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002992 if (retval <= 0)
2993 return (retval);
2994 }
2995 }
2996 while (count >= tape->stage_size) {
Daniel Walkerdcd96372006-06-25 05:47:37 -07002997 ssize_t retval;
2998 if (idetape_copy_stage_from_user(tape, tape->merge_stage, buf, tape->stage_size))
2999 ret = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003000 buf += tape->stage_size;
3001 count -= tape->stage_size;
Borislav Petkovb6422012008-02-02 19:56:49 +01003002 retval = idetape_add_chrdev_write_request(drive, ctl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003003 actually_written += tape->stage_size;
3004 if (retval <= 0)
3005 return (retval);
3006 }
3007 if (count) {
3008 actually_written += count;
Daniel Walkerdcd96372006-06-25 05:47:37 -07003009 if (idetape_copy_stage_from_user(tape, tape->merge_stage, buf, count))
3010 ret = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003011 tape->merge_stage_size += count;
3012 }
Daniel Walkerdcd96372006-06-25 05:47:37 -07003013 return (ret) ? ret : actually_written;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003014}
3015
3016static int idetape_write_filemark (ide_drive_t *drive)
3017{
3018 idetape_pc_t pc;
3019
3020 /* Write a filemark */
3021 idetape_create_write_filemark_cmd(drive, &pc, 1);
3022 if (idetape_queue_pc_tail(drive, &pc)) {
3023 printk(KERN_ERR "ide-tape: Couldn't write a filemark\n");
3024 return -EIO;
3025 }
3026 return 0;
3027}
3028
3029/*
Borislav Petkovd99c9da2008-02-02 19:56:51 +01003030 * Called from idetape_chrdev_ioctl when the general mtio MTIOCTOP ioctl is
3031 * requested.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003032 *
Borislav Petkovd99c9da2008-02-02 19:56:51 +01003033 * Note: MTBSF and MTBSFM are not supported when the tape doesn't support
3034 * spacing over filemarks in the reverse direction. In this case, MTFSFM is also
3035 * usually not supported (it is supported in the rare case in which we crossed
3036 * the filemark during our read-ahead pipelined operation mode).
Linus Torvalds1da177e2005-04-16 15:20:36 -07003037 *
Borislav Petkovd99c9da2008-02-02 19:56:51 +01003038 * The following commands are currently not supported:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003039 *
Borislav Petkovd99c9da2008-02-02 19:56:51 +01003040 * MTFSS, MTBSS, MTWSM, MTSETDENSITY, MTSETDRVBUFFER, MT_ST_BOOLEANS,
3041 * MT_ST_WRITE_THRESHOLD.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003042 */
Borislav Petkovd99c9da2008-02-02 19:56:51 +01003043static int idetape_mtioctop(ide_drive_t *drive, short mt_op, int mt_count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003044{
3045 idetape_tape_t *tape = drive->driver_data;
3046 idetape_pc_t pc;
3047 int i,retval;
3048
Borislav Petkov8004a8c2008-02-06 02:57:51 +01003049 debug_log(DBG_ERR, "Handling MTIOCTOP ioctl: mt_op=%d, mt_count=%d\n",
3050 mt_op, mt_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003051 /*
3052 * Commands which need our pipelined read-ahead stages.
3053 */
3054 switch (mt_op) {
3055 case MTFSF:
3056 case MTFSFM:
3057 case MTBSF:
3058 case MTBSFM:
3059 if (!mt_count)
3060 return (0);
3061 return (idetape_space_over_filemarks(drive,mt_op,mt_count));
3062 default:
3063 break;
3064 }
3065 switch (mt_op) {
3066 case MTWEOF:
3067 if (tape->write_prot)
3068 return -EACCES;
3069 idetape_discard_read_pipeline(drive, 1);
3070 for (i = 0; i < mt_count; i++) {
3071 retval = idetape_write_filemark(drive);
3072 if (retval)
3073 return retval;
3074 }
3075 return (0);
3076 case MTREW:
3077 idetape_discard_read_pipeline(drive, 0);
3078 if (idetape_rewind_tape(drive))
3079 return -EIO;
3080 return 0;
3081 case MTLOAD:
3082 idetape_discard_read_pipeline(drive, 0);
3083 idetape_create_load_unload_cmd(drive, &pc, IDETAPE_LU_LOAD_MASK);
3084 return (idetape_queue_pc_tail(drive, &pc));
3085 case MTUNLOAD:
3086 case MTOFFL:
3087 /*
3088 * If door is locked, attempt to unlock before
3089 * attempting to eject.
3090 */
3091 if (tape->door_locked) {
3092 if (idetape_create_prevent_cmd(drive, &pc, 0))
3093 if (!idetape_queue_pc_tail(drive, &pc))
3094 tape->door_locked = DOOR_UNLOCKED;
3095 }
3096 idetape_discard_read_pipeline(drive, 0);
3097 idetape_create_load_unload_cmd(drive, &pc,!IDETAPE_LU_LOAD_MASK);
3098 retval = idetape_queue_pc_tail(drive, &pc);
3099 if (!retval)
3100 clear_bit(IDETAPE_MEDIUM_PRESENT, &tape->flags);
3101 return retval;
3102 case MTNOP:
3103 idetape_discard_read_pipeline(drive, 0);
3104 return (idetape_flush_tape_buffers(drive));
3105 case MTRETEN:
3106 idetape_discard_read_pipeline(drive, 0);
3107 idetape_create_load_unload_cmd(drive, &pc,IDETAPE_LU_RETENSION_MASK | IDETAPE_LU_LOAD_MASK);
3108 return (idetape_queue_pc_tail(drive, &pc));
3109 case MTEOM:
3110 idetape_create_space_cmd(&pc, 0, IDETAPE_SPACE_TO_EOD);
3111 return (idetape_queue_pc_tail(drive, &pc));
3112 case MTERASE:
3113 (void) idetape_rewind_tape(drive);
3114 idetape_create_erase_cmd(&pc);
3115 return (idetape_queue_pc_tail(drive, &pc));
3116 case MTSETBLK:
3117 if (mt_count) {
3118 if (mt_count < tape->tape_block_size || mt_count % tape->tape_block_size)
3119 return -EIO;
3120 tape->user_bs_factor = mt_count / tape->tape_block_size;
3121 clear_bit(IDETAPE_DETECT_BS, &tape->flags);
3122 } else
3123 set_bit(IDETAPE_DETECT_BS, &tape->flags);
3124 return 0;
3125 case MTSEEK:
3126 idetape_discard_read_pipeline(drive, 0);
3127 return idetape_position_tape(drive, mt_count * tape->user_bs_factor, tape->partition, 0);
3128 case MTSETPART:
3129 idetape_discard_read_pipeline(drive, 0);
3130 return (idetape_position_tape(drive, 0, mt_count, 0));
3131 case MTFSR:
3132 case MTBSR:
3133 case MTLOCK:
3134 if (!idetape_create_prevent_cmd(drive, &pc, 1))
3135 return 0;
3136 retval = idetape_queue_pc_tail(drive, &pc);
3137 if (retval) return retval;
3138 tape->door_locked = DOOR_EXPLICITLY_LOCKED;
3139 return 0;
3140 case MTUNLOCK:
3141 if (!idetape_create_prevent_cmd(drive, &pc, 0))
3142 return 0;
3143 retval = idetape_queue_pc_tail(drive, &pc);
3144 if (retval) return retval;
3145 tape->door_locked = DOOR_UNLOCKED;
3146 return 0;
3147 default:
3148 printk(KERN_ERR "ide-tape: MTIO operation %d not "
3149 "supported\n", mt_op);
3150 return (-EIO);
3151 }
3152}
3153
3154/*
Borislav Petkovd99c9da2008-02-02 19:56:51 +01003155 * Our character device ioctls. General mtio.h magnetic io commands are
3156 * supported here, and not in the corresponding block interface. Our own
3157 * ide-tape ioctls are supported on both interfaces.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003158 */
Borislav Petkovd99c9da2008-02-02 19:56:51 +01003159static int idetape_chrdev_ioctl(struct inode *inode, struct file *file,
3160 unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003161{
3162 struct ide_tape_obj *tape = ide_tape_f(file);
3163 ide_drive_t *drive = tape->drive;
3164 struct mtop mtop;
3165 struct mtget mtget;
3166 struct mtpos mtpos;
3167 int block_offset = 0, position = tape->first_frame_position;
3168 void __user *argp = (void __user *)arg;
3169
Borislav Petkov8004a8c2008-02-06 02:57:51 +01003170 debug_log(DBG_CHRDEV, "Enter %s, cmd=%u\n", __func__, cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003171
3172 tape->restart_speed_control_req = 1;
Borislav Petkov54abf372008-02-06 02:57:52 +01003173 if (tape->chrdev_dir == IDETAPE_DIR_WRITE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003174 idetape_empty_write_pipeline(drive);
3175 idetape_flush_tape_buffers(drive);
3176 }
3177 if (cmd == MTIOCGET || cmd == MTIOCPOS) {
3178 block_offset = idetape_pipeline_size(drive) / (tape->tape_block_size * tape->user_bs_factor);
3179 if ((position = idetape_read_position(drive)) < 0)
3180 return -EIO;
3181 }
3182 switch (cmd) {
3183 case MTIOCTOP:
3184 if (copy_from_user(&mtop, argp, sizeof (struct mtop)))
3185 return -EFAULT;
3186 return (idetape_mtioctop(drive,mtop.mt_op,mtop.mt_count));
3187 case MTIOCGET:
3188 memset(&mtget, 0, sizeof (struct mtget));
3189 mtget.mt_type = MT_ISSCSI2;
3190 mtget.mt_blkno = position / tape->user_bs_factor - block_offset;
3191 mtget.mt_dsreg = ((tape->tape_block_size * tape->user_bs_factor) << MT_ST_BLKSIZE_SHIFT) & MT_ST_BLKSIZE_MASK;
3192 if (tape->drv_write_prot) {
3193 mtget.mt_gstat |= GMT_WR_PROT(0xffffffff);
3194 }
3195 if (copy_to_user(argp, &mtget, sizeof(struct mtget)))
3196 return -EFAULT;
3197 return 0;
3198 case MTIOCPOS:
3199 mtpos.mt_blkno = position / tape->user_bs_factor - block_offset;
3200 if (copy_to_user(argp, &mtpos, sizeof(struct mtpos)))
3201 return -EFAULT;
3202 return 0;
3203 default:
Borislav Petkov54abf372008-02-06 02:57:52 +01003204 if (tape->chrdev_dir == IDETAPE_DIR_READ)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003205 idetape_discard_read_pipeline(drive, 1);
3206 return idetape_blkdev_ioctl(drive, cmd, arg);
3207 }
3208}
3209
Borislav Petkov3cffb9c2008-02-02 19:56:50 +01003210/*
3211 * Do a mode sense page 0 with block descriptor and if it succeeds set the tape
3212 * block size with the reported value.
3213 */
3214static void ide_tape_get_bsize_from_bdesc(ide_drive_t *drive)
3215{
3216 idetape_tape_t *tape = drive->driver_data;
3217 idetape_pc_t pc;
3218
3219 idetape_create_mode_sense_cmd(&pc, IDETAPE_BLOCK_DESCRIPTOR);
3220 if (idetape_queue_pc_tail(drive, &pc)) {
3221 printk(KERN_ERR "ide-tape: Can't get block descriptor\n");
3222 if (tape->tape_block_size == 0) {
3223 printk(KERN_WARNING "ide-tape: Cannot deal with zero "
3224 "block size, assuming 32k\n");
3225 tape->tape_block_size = 32768;
3226 }
3227 return;
3228 }
3229 tape->tape_block_size = (pc.buffer[4 + 5] << 16) +
3230 (pc.buffer[4 + 6] << 8) +
3231 pc.buffer[4 + 7];
3232 tape->drv_write_prot = (pc.buffer[2] & 0x80) >> 7;
3233}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003234
3235/*
3236 * Our character device open function.
3237 */
3238static int idetape_chrdev_open (struct inode *inode, struct file *filp)
3239{
3240 unsigned int minor = iminor(inode), i = minor & ~0xc0;
3241 ide_drive_t *drive;
3242 idetape_tape_t *tape;
3243 idetape_pc_t pc;
3244 int retval;
3245
Borislav Petkov8004a8c2008-02-06 02:57:51 +01003246 if (i >= MAX_HWIFS * MAX_DRIVES)
3247 return -ENXIO;
3248
3249 tape = ide_tape_chrdev_get(i);
3250 if (!tape)
3251 return -ENXIO;
3252
3253 debug_log(DBG_CHRDEV, "Enter %s\n", __func__);
3254
Linus Torvalds1da177e2005-04-16 15:20:36 -07003255 /*
3256 * We really want to do nonseekable_open(inode, filp); here, but some
3257 * versions of tar incorrectly call lseek on tapes and bail out if that
3258 * fails. So we disallow pread() and pwrite(), but permit lseeks.
3259 */
3260 filp->f_mode &= ~(FMODE_PREAD | FMODE_PWRITE);
3261
Linus Torvalds1da177e2005-04-16 15:20:36 -07003262 drive = tape->drive;
3263
3264 filp->private_data = tape;
3265
3266 if (test_and_set_bit(IDETAPE_BUSY, &tape->flags)) {
3267 retval = -EBUSY;
3268 goto out_put_tape;
3269 }
3270
3271 retval = idetape_wait_ready(drive, 60 * HZ);
3272 if (retval) {
3273 clear_bit(IDETAPE_BUSY, &tape->flags);
3274 printk(KERN_ERR "ide-tape: %s: drive not ready\n", tape->name);
3275 goto out_put_tape;
3276 }
3277
3278 idetape_read_position(drive);
3279 if (!test_bit(IDETAPE_ADDRESS_VALID, &tape->flags))
3280 (void)idetape_rewind_tape(drive);
3281
Borislav Petkov54abf372008-02-06 02:57:52 +01003282 if (tape->chrdev_dir != IDETAPE_DIR_READ)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003283 clear_bit(IDETAPE_PIPELINE_ERROR, &tape->flags);
3284
3285 /* Read block size and write protect status from drive. */
Borislav Petkov3cffb9c2008-02-02 19:56:50 +01003286 ide_tape_get_bsize_from_bdesc(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003287
3288 /* Set write protect flag if device is opened as read-only. */
3289 if ((filp->f_flags & O_ACCMODE) == O_RDONLY)
3290 tape->write_prot = 1;
3291 else
3292 tape->write_prot = tape->drv_write_prot;
3293
3294 /* Make sure drive isn't write protected if user wants to write. */
3295 if (tape->write_prot) {
3296 if ((filp->f_flags & O_ACCMODE) == O_WRONLY ||
3297 (filp->f_flags & O_ACCMODE) == O_RDWR) {
3298 clear_bit(IDETAPE_BUSY, &tape->flags);
3299 retval = -EROFS;
3300 goto out_put_tape;
3301 }
3302 }
3303
3304 /*
3305 * Lock the tape drive door so user can't eject.
3306 */
Borislav Petkov54abf372008-02-06 02:57:52 +01003307 if (tape->chrdev_dir == IDETAPE_DIR_NONE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003308 if (idetape_create_prevent_cmd(drive, &pc, 1)) {
3309 if (!idetape_queue_pc_tail(drive, &pc)) {
3310 if (tape->door_locked != DOOR_EXPLICITLY_LOCKED)
3311 tape->door_locked = DOOR_LOCKED;
3312 }
3313 }
3314 }
3315 idetape_restart_speed_control(drive);
3316 tape->restart_speed_control_req = 0;
3317 return 0;
3318
3319out_put_tape:
3320 ide_tape_put(tape);
3321 return retval;
3322}
3323
3324static void idetape_write_release (ide_drive_t *drive, unsigned int minor)
3325{
3326 idetape_tape_t *tape = drive->driver_data;
3327
3328 idetape_empty_write_pipeline(drive);
3329 tape->merge_stage = __idetape_kmalloc_stage(tape, 1, 0);
3330 if (tape->merge_stage != NULL) {
3331 idetape_pad_zeros(drive, tape->tape_block_size * (tape->user_bs_factor - 1));
3332 __idetape_kfree_stage(tape->merge_stage);
3333 tape->merge_stage = NULL;
3334 }
3335 idetape_write_filemark(drive);
3336 idetape_flush_tape_buffers(drive);
3337 idetape_flush_tape_buffers(drive);
3338}
3339
3340/*
3341 * Our character device release function.
3342 */
3343static int idetape_chrdev_release (struct inode *inode, struct file *filp)
3344{
3345 struct ide_tape_obj *tape = ide_tape_f(filp);
3346 ide_drive_t *drive = tape->drive;
3347 idetape_pc_t pc;
3348 unsigned int minor = iminor(inode);
3349
3350 lock_kernel();
3351 tape = drive->driver_data;
Borislav Petkov8004a8c2008-02-06 02:57:51 +01003352
3353 debug_log(DBG_CHRDEV, "Enter %s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003354
Borislav Petkov54abf372008-02-06 02:57:52 +01003355 if (tape->chrdev_dir == IDETAPE_DIR_WRITE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003356 idetape_write_release(drive, minor);
Borislav Petkov54abf372008-02-06 02:57:52 +01003357 if (tape->chrdev_dir == IDETAPE_DIR_READ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003358 if (minor < 128)
3359 idetape_discard_read_pipeline(drive, 1);
3360 else
3361 idetape_wait_for_pipeline(drive);
3362 }
3363 if (tape->cache_stage != NULL) {
3364 __idetape_kfree_stage(tape->cache_stage);
3365 tape->cache_stage = NULL;
3366 }
3367 if (minor < 128 && test_bit(IDETAPE_MEDIUM_PRESENT, &tape->flags))
3368 (void) idetape_rewind_tape(drive);
Borislav Petkov54abf372008-02-06 02:57:52 +01003369 if (tape->chrdev_dir == IDETAPE_DIR_NONE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003370 if (tape->door_locked == DOOR_LOCKED) {
3371 if (idetape_create_prevent_cmd(drive, &pc, 0)) {
3372 if (!idetape_queue_pc_tail(drive, &pc))
3373 tape->door_locked = DOOR_UNLOCKED;
3374 }
3375 }
3376 }
3377 clear_bit(IDETAPE_BUSY, &tape->flags);
3378 ide_tape_put(tape);
3379 unlock_kernel();
3380 return 0;
3381}
3382
3383/*
3384 * idetape_identify_device is called to check the contents of the
3385 * ATAPI IDENTIFY command results. We return:
3386 *
3387 * 1 If the tape can be supported by us, based on the information
3388 * we have so far.
3389 *
3390 * 0 If this tape driver is not currently supported by us.
3391 */
3392static int idetape_identify_device (ide_drive_t *drive)
3393{
3394 struct idetape_id_gcw gcw;
3395 struct hd_driveid *id = drive->id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003396
3397 if (drive->id_read == 0)
3398 return 1;
3399
3400 *((unsigned short *) &gcw) = id->config;
3401
Linus Torvalds1da177e2005-04-16 15:20:36 -07003402 /* Check that we can support this device */
3403
Bartlomiej Zolnierkiewicz16422de32008-02-02 19:56:48 +01003404 if (gcw.protocol != 2)
3405 printk(KERN_ERR "ide-tape: Protocol (0x%02x) is not ATAPI\n",
3406 gcw.protocol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003407 else if (gcw.device_type != 1)
Bartlomiej Zolnierkiewicz16422de32008-02-02 19:56:48 +01003408 printk(KERN_ERR "ide-tape: Device type (0x%02x) is not set "
3409 "to tape\n", gcw.device_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003410 else if (!gcw.removable)
3411 printk(KERN_ERR "ide-tape: The removable flag is not set\n");
3412 else if (gcw.packet_size != 0) {
Bartlomiej Zolnierkiewicz16422de32008-02-02 19:56:48 +01003413 printk(KERN_ERR "ide-tape: Packet size (0x%02x) is not 12 "
3414 "bytes long\n", gcw.packet_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003415 } else
3416 return 1;
3417 return 0;
3418}
3419
Borislav Petkov6d29c8f2008-02-02 19:56:49 +01003420static void idetape_get_inquiry_results(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003421{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003422 idetape_tape_t *tape = drive->driver_data;
3423 idetape_pc_t pc;
Borislav Petkov41f81d542008-02-06 02:57:52 +01003424 char fw_rev[6], vendor_id[10], product_id[18];
Borislav Petkov6d29c8f2008-02-02 19:56:49 +01003425
Linus Torvalds1da177e2005-04-16 15:20:36 -07003426 idetape_create_inquiry_cmd(&pc);
3427 if (idetape_queue_pc_tail(drive, &pc)) {
Borislav Petkov6d29c8f2008-02-02 19:56:49 +01003428 printk(KERN_ERR "ide-tape: %s: can't get INQUIRY results\n",
3429 tape->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003430 return;
3431 }
Borislav Petkov41f81d542008-02-06 02:57:52 +01003432 memcpy(vendor_id, &pc.buffer[8], 8);
3433 memcpy(product_id, &pc.buffer[16], 16);
3434 memcpy(fw_rev, &pc.buffer[32], 4);
Borislav Petkov6d29c8f2008-02-02 19:56:49 +01003435
Borislav Petkov41f81d542008-02-06 02:57:52 +01003436 ide_fixstring(vendor_id, 10, 0);
3437 ide_fixstring(product_id, 18, 0);
3438 ide_fixstring(fw_rev, 6, 0);
3439
Borislav Petkov6d29c8f2008-02-02 19:56:49 +01003440 printk(KERN_INFO "ide-tape: %s <-> %s: %s %s rev %s\n",
Borislav Petkov41f81d542008-02-06 02:57:52 +01003441 drive->name, tape->name, vendor_id, product_id, fw_rev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003442}
3443
3444/*
Borislav Petkovb6422012008-02-02 19:56:49 +01003445 * Ask the tape about its various parameters. In particular, we will adjust our
3446 * data transfer buffer size to the recommended value as returned by the tape.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003447 */
3448static void idetape_get_mode_sense_results (ide_drive_t *drive)
3449{
3450 idetape_tape_t *tape = drive->driver_data;
3451 idetape_pc_t pc;
Borislav Petkovb6422012008-02-02 19:56:49 +01003452 u8 *caps;
3453 u8 speed, max_speed;
Borislav Petkov47314fa2008-02-02 19:56:48 +01003454
Linus Torvalds1da177e2005-04-16 15:20:36 -07003455 idetape_create_mode_sense_cmd(&pc, IDETAPE_CAPABILITIES_PAGE);
3456 if (idetape_queue_pc_tail(drive, &pc)) {
Borislav Petkovb6422012008-02-02 19:56:49 +01003457 printk(KERN_ERR "ide-tape: Can't get tape parameters - assuming"
3458 " some default values\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003459 tape->tape_block_size = 512;
Borislav Petkovb6422012008-02-02 19:56:49 +01003460 put_unaligned(52, (u16 *)&tape->caps[12]);
3461 put_unaligned(540, (u16 *)&tape->caps[14]);
3462 put_unaligned(6*52, (u16 *)&tape->caps[16]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003463 return;
3464 }
Borislav Petkovb6422012008-02-02 19:56:49 +01003465 caps = pc.buffer + 4 + pc.buffer[3];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003466
Borislav Petkovb6422012008-02-02 19:56:49 +01003467 /* convert to host order and save for later use */
3468 speed = be16_to_cpu(*(u16 *)&caps[14]);
3469 max_speed = be16_to_cpu(*(u16 *)&caps[8]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003470
Borislav Petkovb6422012008-02-02 19:56:49 +01003471 put_unaligned(max_speed, (u16 *)&caps[8]);
3472 put_unaligned(be16_to_cpu(*(u16 *)&caps[12]), (u16 *)&caps[12]);
3473 put_unaligned(speed, (u16 *)&caps[14]);
3474 put_unaligned(be16_to_cpu(*(u16 *)&caps[16]), (u16 *)&caps[16]);
3475
3476 if (!speed) {
3477 printk(KERN_INFO "ide-tape: %s: invalid tape speed "
3478 "(assuming 650KB/sec)\n", drive->name);
3479 put_unaligned(650, (u16 *)&caps[14]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003480 }
Borislav Petkovb6422012008-02-02 19:56:49 +01003481 if (!max_speed) {
3482 printk(KERN_INFO "ide-tape: %s: invalid max_speed "
3483 "(assuming 650KB/sec)\n", drive->name);
3484 put_unaligned(650, (u16 *)&caps[8]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003485 }
3486
Borislav Petkovb6422012008-02-02 19:56:49 +01003487 memcpy(&tape->caps, caps, 20);
3488 if (caps[7] & 0x02)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003489 tape->tape_block_size = 512;
Borislav Petkovb6422012008-02-02 19:56:49 +01003490 else if (caps[7] & 0x04)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003491 tape->tape_block_size = 1024;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003492}
3493
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +02003494#ifdef CONFIG_IDE_PROC_FS
Linus Torvalds1da177e2005-04-16 15:20:36 -07003495static void idetape_add_settings (ide_drive_t *drive)
3496{
3497 idetape_tape_t *tape = drive->driver_data;
3498
3499/*
Bartlomiej Zolnierkiewicz14979432007-05-10 00:01:10 +02003500 * drive setting name read/write data type min max mul_factor div_factor data pointer set function
Linus Torvalds1da177e2005-04-16 15:20:36 -07003501 */
Borislav Petkovb6422012008-02-02 19:56:49 +01003502 ide_add_setting(drive, "buffer", SETTING_READ, TYPE_SHORT, 0, 0xffff,
3503 1, 2, (u16 *)&tape->caps[16], NULL);
Bartlomiej Zolnierkiewicz14979432007-05-10 00:01:10 +02003504 ide_add_setting(drive, "pipeline_min", SETTING_RW, TYPE_INT, 1, 0xffff, tape->stage_size / 1024, 1, &tape->min_pipeline, NULL);
3505 ide_add_setting(drive, "pipeline", SETTING_RW, TYPE_INT, 1, 0xffff, tape->stage_size / 1024, 1, &tape->max_stages, NULL);
3506 ide_add_setting(drive, "pipeline_max", SETTING_RW, TYPE_INT, 1, 0xffff, tape->stage_size / 1024, 1, &tape->max_pipeline, NULL);
3507 ide_add_setting(drive, "pipeline_used", SETTING_READ, TYPE_INT, 0, 0xffff, tape->stage_size / 1024, 1, &tape->nr_stages, NULL);
3508 ide_add_setting(drive, "pipeline_pending", SETTING_READ, TYPE_INT, 0, 0xffff, tape->stage_size / 1024, 1, &tape->nr_pending_stages, NULL);
Borislav Petkovb6422012008-02-02 19:56:49 +01003509 ide_add_setting(drive, "speed", SETTING_READ, TYPE_SHORT, 0, 0xffff,
3510 1, 1, (u16 *)&tape->caps[14], NULL);
Bartlomiej Zolnierkiewicz14979432007-05-10 00:01:10 +02003511 ide_add_setting(drive, "stage", SETTING_READ, TYPE_INT, 0, 0xffff, 1, 1024, &tape->stage_size, NULL);
3512 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);
3513 ide_add_setting(drive, "dsc_overlap", SETTING_RW, TYPE_BYTE, 0, 1, 1, 1, &drive->dsc_overlap, NULL);
3514 ide_add_setting(drive, "pipeline_head_speed_c",SETTING_READ, TYPE_INT, 0, 0xffff, 1, 1, &tape->controlled_pipeline_head_speed, NULL);
3515 ide_add_setting(drive, "pipeline_head_speed_u",SETTING_READ, TYPE_INT, 0, 0xffff, 1, 1, &tape->uncontrolled_pipeline_head_speed,NULL);
3516 ide_add_setting(drive, "avg_speed", SETTING_READ, TYPE_INT, 0, 0xffff, 1, 1, &tape->avg_speed, NULL);
Borislav Petkov8004a8c2008-02-06 02:57:51 +01003517 ide_add_setting(drive, "debug_mask", SETTING_RW, TYPE_INT, 0, 0xffff, 1,
3518 1, &tape->debug_mask, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003519}
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +02003520#else
3521static inline void idetape_add_settings(ide_drive_t *drive) { ; }
3522#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003523
3524/*
3525 * ide_setup is called to:
3526 *
3527 * 1. Initialize our various state variables.
3528 * 2. Ask the tape for its capabilities.
3529 * 3. Allocate a buffer which will be used for data
3530 * transfer. The buffer size is chosen based on
3531 * the recommendation which we received in step (2).
3532 *
3533 * Note that at this point ide.c already assigned us an irq, so that
3534 * we can queue requests here and wait for their completion.
3535 */
3536static void idetape_setup (ide_drive_t *drive, idetape_tape_t *tape, int minor)
3537{
3538 unsigned long t1, tmid, tn, t;
3539 int speed;
3540 struct idetape_id_gcw gcw;
3541 int stage_size;
3542 struct sysinfo si;
Borislav Petkovb6422012008-02-02 19:56:49 +01003543 u16 *ctl = (u16 *)&tape->caps[12];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003544
3545 spin_lock_init(&tape->spinlock);
3546 drive->dsc_overlap = 1;
Bartlomiej Zolnierkiewicz4166c192008-02-01 23:09:30 +01003547 if (drive->hwif->host_flags & IDE_HFLAG_NO_DSC) {
3548 printk(KERN_INFO "ide-tape: %s: disabling DSC overlap\n",
3549 tape->name);
3550 drive->dsc_overlap = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003551 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003552 /* Seagate Travan drives do not support DSC overlap. */
3553 if (strstr(drive->id->model, "Seagate STT3401"))
3554 drive->dsc_overlap = 0;
3555 tape->minor = minor;
3556 tape->name[0] = 'h';
3557 tape->name[1] = 't';
3558 tape->name[2] = '0' + minor;
Borislav Petkov54abf372008-02-06 02:57:52 +01003559 tape->chrdev_dir = IDETAPE_DIR_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003560 tape->pc = tape->pc_stack;
3561 tape->max_insert_speed = 10000;
3562 tape->speed_control = 1;
3563 *((unsigned short *) &gcw) = drive->id->config;
3564 if (gcw.drq_type == 1)
3565 set_bit(IDETAPE_DRQ_INTERRUPT, &tape->flags);
3566
3567 tape->min_pipeline = tape->max_pipeline = tape->max_stages = 10;
3568
3569 idetape_get_inquiry_results(drive);
3570 idetape_get_mode_sense_results(drive);
Borislav Petkov3cffb9c2008-02-02 19:56:50 +01003571 ide_tape_get_bsize_from_bdesc(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003572 tape->user_bs_factor = 1;
Borislav Petkovb6422012008-02-02 19:56:49 +01003573 tape->stage_size = *ctl * tape->tape_block_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003574 while (tape->stage_size > 0xffff) {
3575 printk(KERN_NOTICE "ide-tape: decreasing stage size\n");
Borislav Petkovb6422012008-02-02 19:56:49 +01003576 *ctl /= 2;
3577 tape->stage_size = *ctl * tape->tape_block_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003578 }
3579 stage_size = tape->stage_size;
3580 tape->pages_per_stage = stage_size / PAGE_SIZE;
3581 if (stage_size % PAGE_SIZE) {
3582 tape->pages_per_stage++;
3583 tape->excess_bh_size = PAGE_SIZE - stage_size % PAGE_SIZE;
3584 }
3585
Borislav Petkovb6422012008-02-02 19:56:49 +01003586 /* Select the "best" DSC read/write polling freq and pipeline size. */
3587 speed = max(*(u16 *)&tape->caps[14], *(u16 *)&tape->caps[8]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003588
3589 tape->max_stages = speed * 1000 * 10 / tape->stage_size;
3590
3591 /*
3592 * Limit memory use for pipeline to 10% of physical memory
3593 */
3594 si_meminfo(&si);
3595 if (tape->max_stages * tape->stage_size > si.totalram * si.mem_unit / 10)
3596 tape->max_stages = si.totalram * si.mem_unit / (10 * tape->stage_size);
3597 tape->max_stages = min(tape->max_stages, IDETAPE_MAX_PIPELINE_STAGES);
3598 tape->min_pipeline = min(tape->max_stages, IDETAPE_MIN_PIPELINE_STAGES);
3599 tape->max_pipeline = min(tape->max_stages * 2, IDETAPE_MAX_PIPELINE_STAGES);
3600 if (tape->max_stages == 0)
3601 tape->max_stages = tape->min_pipeline = tape->max_pipeline = 1;
3602
3603 t1 = (tape->stage_size * HZ) / (speed * 1000);
Borislav Petkovb6422012008-02-02 19:56:49 +01003604 tmid = (*(u16 *)&tape->caps[16] * 32 * HZ) / (speed * 125);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003605 tn = (IDETAPE_FIFO_THRESHOLD * tape->stage_size * HZ) / (speed * 1000);
3606
3607 if (tape->max_stages)
3608 t = tn;
3609 else
3610 t = t1;
3611
3612 /*
3613 * Ensure that the number we got makes sense; limit
3614 * it within IDETAPE_DSC_RW_MIN and IDETAPE_DSC_RW_MAX.
3615 */
3616 tape->best_dsc_rw_frequency = max_t(unsigned long, min_t(unsigned long, t, IDETAPE_DSC_RW_MAX), IDETAPE_DSC_RW_MIN);
3617 printk(KERN_INFO "ide-tape: %s <-> %s: %dKBps, %d*%dkB buffer, "
3618 "%dkB pipeline, %lums tDSC%s\n",
Borislav Petkovb6422012008-02-02 19:56:49 +01003619 drive->name, tape->name, *(u16 *)&tape->caps[14],
3620 (*(u16 *)&tape->caps[16] * 512) / tape->stage_size,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003621 tape->stage_size / 1024,
3622 tape->max_stages * tape->stage_size / 1024,
3623 tape->best_dsc_rw_frequency * 1000 / HZ,
3624 drive->using_dma ? ", DMA":"");
3625
3626 idetape_add_settings(drive);
3627}
3628
Russell King4031bbe2006-01-06 11:41:00 +00003629static void ide_tape_remove(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003630{
3631 idetape_tape_t *tape = drive->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003632
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +02003633 ide_proc_unregister_driver(drive, tape->driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003634
3635 ide_unregister_region(tape->disk);
3636
3637 ide_tape_put(tape);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003638}
3639
3640static void ide_tape_release(struct kref *kref)
3641{
3642 struct ide_tape_obj *tape = to_ide_tape(kref);
3643 ide_drive_t *drive = tape->drive;
3644 struct gendisk *g = tape->disk;
3645
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02003646 BUG_ON(tape->first_stage != NULL || tape->merge_stage_size);
3647
Linus Torvalds1da177e2005-04-16 15:20:36 -07003648 drive->dsc_overlap = 0;
3649 drive->driver_data = NULL;
Tony Jonesdbc12722007-09-25 02:03:03 +02003650 device_destroy(idetape_sysfs_class, MKDEV(IDETAPE_MAJOR, tape->minor));
3651 device_destroy(idetape_sysfs_class, MKDEV(IDETAPE_MAJOR, tape->minor + 128));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003652 idetape_devs[tape->minor] = NULL;
3653 g->private_data = NULL;
3654 put_disk(g);
3655 kfree(tape);
3656}
3657
Bartlomiej Zolnierkiewiczecfd80e2007-05-10 00:01:09 +02003658#ifdef CONFIG_IDE_PROC_FS
Linus Torvalds1da177e2005-04-16 15:20:36 -07003659static int proc_idetape_read_name
3660 (char *page, char **start, off_t off, int count, int *eof, void *data)
3661{
3662 ide_drive_t *drive = (ide_drive_t *) data;
3663 idetape_tape_t *tape = drive->driver_data;
3664 char *out = page;
3665 int len;
3666
3667 len = sprintf(out, "%s\n", tape->name);
3668 PROC_IDE_READ_RETURN(page, start, off, count, eof, len);
3669}
3670
3671static ide_proc_entry_t idetape_proc[] = {
3672 { "capacity", S_IFREG|S_IRUGO, proc_ide_read_capacity, NULL },
3673 { "name", S_IFREG|S_IRUGO, proc_idetape_read_name, NULL },
3674 { NULL, 0, NULL, NULL }
3675};
Linus Torvalds1da177e2005-04-16 15:20:36 -07003676#endif
3677
Russell King4031bbe2006-01-06 11:41:00 +00003678static int ide_tape_probe(ide_drive_t *);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003679
Linus Torvalds1da177e2005-04-16 15:20:36 -07003680static ide_driver_t idetape_driver = {
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02003681 .gen_driver = {
Laurent Riffard4ef3b8f2005-11-18 22:15:40 +01003682 .owner = THIS_MODULE,
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02003683 .name = "ide-tape",
3684 .bus = &ide_bus_type,
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02003685 },
Russell King4031bbe2006-01-06 11:41:00 +00003686 .probe = ide_tape_probe,
3687 .remove = ide_tape_remove,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003688 .version = IDETAPE_VERSION,
3689 .media = ide_tape,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003690 .supports_dsc_overlap = 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003691 .do_request = idetape_do_request,
3692 .end_request = idetape_end_request,
3693 .error = __ide_error,
3694 .abort = __ide_abort,
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +02003695#ifdef CONFIG_IDE_PROC_FS
Linus Torvalds1da177e2005-04-16 15:20:36 -07003696 .proc = idetape_proc,
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +02003697#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003698};
3699
3700/*
3701 * Our character device supporting functions, passed to register_chrdev.
3702 */
Arjan van de Ven2b8693c2007-02-12 00:55:32 -08003703static const struct file_operations idetape_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003704 .owner = THIS_MODULE,
3705 .read = idetape_chrdev_read,
3706 .write = idetape_chrdev_write,
3707 .ioctl = idetape_chrdev_ioctl,
3708 .open = idetape_chrdev_open,
3709 .release = idetape_chrdev_release,
3710};
3711
3712static int idetape_open(struct inode *inode, struct file *filp)
3713{
3714 struct gendisk *disk = inode->i_bdev->bd_disk;
3715 struct ide_tape_obj *tape;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003716
3717 if (!(tape = ide_tape_get(disk)))
3718 return -ENXIO;
3719
Linus Torvalds1da177e2005-04-16 15:20:36 -07003720 return 0;
3721}
3722
3723static int idetape_release(struct inode *inode, struct file *filp)
3724{
3725 struct gendisk *disk = inode->i_bdev->bd_disk;
3726 struct ide_tape_obj *tape = ide_tape_g(disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003727
3728 ide_tape_put(tape);
3729
3730 return 0;
3731}
3732
3733static int idetape_ioctl(struct inode *inode, struct file *file,
3734 unsigned int cmd, unsigned long arg)
3735{
3736 struct block_device *bdev = inode->i_bdev;
3737 struct ide_tape_obj *tape = ide_tape_g(bdev->bd_disk);
3738 ide_drive_t *drive = tape->drive;
3739 int err = generic_ide_ioctl(drive, file, bdev, cmd, arg);
3740 if (err == -EINVAL)
3741 err = idetape_blkdev_ioctl(drive, cmd, arg);
3742 return err;
3743}
3744
3745static struct block_device_operations idetape_block_ops = {
3746 .owner = THIS_MODULE,
3747 .open = idetape_open,
3748 .release = idetape_release,
3749 .ioctl = idetape_ioctl,
3750};
3751
Russell King4031bbe2006-01-06 11:41:00 +00003752static int ide_tape_probe(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003753{
3754 idetape_tape_t *tape;
3755 struct gendisk *g;
3756 int minor;
3757
3758 if (!strstr("ide-tape", drive->driver_req))
3759 goto failed;
3760 if (!drive->present)
3761 goto failed;
3762 if (drive->media != ide_tape)
3763 goto failed;
3764 if (!idetape_identify_device (drive)) {
3765 printk(KERN_ERR "ide-tape: %s: not supported by this version of ide-tape\n", drive->name);
3766 goto failed;
3767 }
3768 if (drive->scsi) {
3769 printk("ide-tape: passing drive %s to ide-scsi emulation.\n", drive->name);
3770 goto failed;
3771 }
3772 if (strstr(drive->id->model, "OnStream DI-")) {
3773 printk(KERN_WARNING "ide-tape: Use drive %s with ide-scsi emulation and osst.\n", drive->name);
3774 printk(KERN_WARNING "ide-tape: OnStream support will be removed soon from ide-tape!\n");
3775 }
Robert P. J. Day5cbded52006-12-13 00:35:56 -08003776 tape = kzalloc(sizeof (idetape_tape_t), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003777 if (tape == NULL) {
3778 printk(KERN_ERR "ide-tape: %s: Can't allocate a tape structure\n", drive->name);
3779 goto failed;
3780 }
3781
3782 g = alloc_disk(1 << PARTN_BITS);
3783 if (!g)
3784 goto out_free_tape;
3785
3786 ide_init_disk(g, drive);
3787
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +02003788 ide_proc_register_driver(drive, &idetape_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003789
Linus Torvalds1da177e2005-04-16 15:20:36 -07003790 kref_init(&tape->kref);
3791
3792 tape->drive = drive;
3793 tape->driver = &idetape_driver;
3794 tape->disk = g;
3795
3796 g->private_data = &tape->driver;
3797
3798 drive->driver_data = tape;
3799
Arjan van de Vencf8b8972006-03-23 03:00:45 -08003800 mutex_lock(&idetape_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003801 for (minor = 0; idetape_devs[minor]; minor++)
3802 ;
3803 idetape_devs[minor] = tape;
Arjan van de Vencf8b8972006-03-23 03:00:45 -08003804 mutex_unlock(&idetape_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003805
3806 idetape_setup(drive, tape, minor);
3807
Tony Jonesdbc12722007-09-25 02:03:03 +02003808 device_create(idetape_sysfs_class, &drive->gendev,
3809 MKDEV(IDETAPE_MAJOR, minor), "%s", tape->name);
3810 device_create(idetape_sysfs_class, &drive->gendev,
3811 MKDEV(IDETAPE_MAJOR, minor + 128), "n%s", tape->name);
Will Dysond5dee802005-09-16 02:55:07 -07003812
Linus Torvalds1da177e2005-04-16 15:20:36 -07003813 g->fops = &idetape_block_ops;
3814 ide_register_region(g);
3815
3816 return 0;
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02003817
Linus Torvalds1da177e2005-04-16 15:20:36 -07003818out_free_tape:
3819 kfree(tape);
3820failed:
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02003821 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003822}
3823
3824MODULE_DESCRIPTION("ATAPI Streaming TAPE Driver");
3825MODULE_LICENSE("GPL");
3826
3827static void __exit idetape_exit (void)
3828{
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02003829 driver_unregister(&idetape_driver.gen_driver);
Will Dysond5dee802005-09-16 02:55:07 -07003830 class_destroy(idetape_sysfs_class);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003831 unregister_chrdev(IDETAPE_MAJOR, "ht");
3832}
3833
Bartlomiej Zolnierkiewicz17514e82005-11-19 22:24:35 +01003834static int __init idetape_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003835{
Will Dysond5dee802005-09-16 02:55:07 -07003836 int error = 1;
3837 idetape_sysfs_class = class_create(THIS_MODULE, "ide_tape");
3838 if (IS_ERR(idetape_sysfs_class)) {
3839 idetape_sysfs_class = NULL;
3840 printk(KERN_ERR "Unable to create sysfs class for ide tapes\n");
3841 error = -EBUSY;
3842 goto out;
3843 }
3844
Linus Torvalds1da177e2005-04-16 15:20:36 -07003845 if (register_chrdev(IDETAPE_MAJOR, "ht", &idetape_fops)) {
3846 printk(KERN_ERR "ide-tape: Failed to register character device interface\n");
Will Dysond5dee802005-09-16 02:55:07 -07003847 error = -EBUSY;
3848 goto out_free_class;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003849 }
Will Dysond5dee802005-09-16 02:55:07 -07003850
3851 error = driver_register(&idetape_driver.gen_driver);
3852 if (error)
3853 goto out_free_driver;
3854
3855 return 0;
3856
3857out_free_driver:
3858 driver_unregister(&idetape_driver.gen_driver);
3859out_free_class:
3860 class_destroy(idetape_sysfs_class);
3861out:
3862 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003863}
3864
Kay Sievers263756e2005-12-12 18:03:44 +01003865MODULE_ALIAS("ide:*m-tape*");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003866module_init(idetape_init);
3867module_exit(idetape_exit);
3868MODULE_ALIAS_CHARDEV_MAJOR(IDETAPE_MAJOR);