blob: 3c516ff1adf0f13075efa74afbd59ad37f7ec098 [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
90 * cannot be disabled (calculate_speeds() divides by tape->max_stages.)
91 */
92#define IDETAPE_MIN_PIPELINE_STAGES 1
93#define IDETAPE_MAX_PIPELINE_STAGES 400
94#define IDETAPE_INCREASE_STAGES_RATE 20
95
96/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070097 * After each failed packet command we issue a request sense command
98 * and retry the packet command IDETAPE_MAX_PC_RETRIES times.
99 *
100 * Setting IDETAPE_MAX_PC_RETRIES to 0 will disable retries.
101 */
102#define IDETAPE_MAX_PC_RETRIES 3
103
104/*
105 * With each packet command, we allocate a buffer of
106 * IDETAPE_PC_BUFFER_SIZE bytes. This is used for several packet
107 * commands (Not for READ/WRITE commands).
108 */
109#define IDETAPE_PC_BUFFER_SIZE 256
110
111/*
112 * In various places in the driver, we need to allocate storage
113 * for packet commands and requests, which will remain valid while
114 * we leave the driver to wait for an interrupt or a timeout event.
115 */
116#define IDETAPE_PC_STACK (10 + IDETAPE_MAX_PC_RETRIES)
117
118/*
119 * Some drives (for example, Seagate STT3401A Travan) require a very long
120 * timeout, because they don't return an interrupt or clear their busy bit
121 * until after the command completes (even retension commands).
122 */
123#define IDETAPE_WAIT_CMD (900*HZ)
124
125/*
126 * The following parameter is used to select the point in the internal
127 * tape fifo in which we will start to refill the buffer. Decreasing
128 * the following parameter will improve the system's latency and
Robert P. J. Day3a4fa0a2007-10-19 23:10:43 +0200129 * interactive response, while using a high value might improve system
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 * throughput.
131 */
132#define IDETAPE_FIFO_THRESHOLD 2
133
134/*
135 * DSC polling parameters.
136 *
137 * Polling for DSC (a single bit in the status register) is a very
138 * important function in ide-tape. There are two cases in which we
139 * poll for DSC:
140 *
141 * 1. Before a read/write packet command, to ensure that we
142 * can transfer data from/to the tape's data buffers, without
143 * causing an actual media access. In case the tape is not
144 * ready yet, we take out our request from the device
145 * request queue, so that ide.c will service requests from
146 * the other device on the same interface meanwhile.
147 *
148 * 2. After the successful initialization of a "media access
149 * packet command", which is a command which can take a long
150 * time to complete (it can be several seconds or even an hour).
151 *
152 * Again, we postpone our request in the middle to free the bus
153 * for the other device. The polling frequency here should be
154 * lower than the read/write frequency since those media access
155 * commands are slow. We start from a "fast" frequency -
156 * IDETAPE_DSC_MA_FAST (one second), and if we don't receive DSC
157 * after IDETAPE_DSC_MA_THRESHOLD (5 minutes), we switch it to a
158 * lower frequency - IDETAPE_DSC_MA_SLOW (1 minute).
159 *
160 * We also set a timeout for the timer, in case something goes wrong.
161 * The timeout should be longer then the maximum execution time of a
162 * tape operation.
163 */
164
165/*
166 * DSC timings.
167 */
168#define IDETAPE_DSC_RW_MIN 5*HZ/100 /* 50 msec */
169#define IDETAPE_DSC_RW_MAX 40*HZ/100 /* 400 msec */
170#define IDETAPE_DSC_RW_TIMEOUT 2*60*HZ /* 2 minutes */
171#define IDETAPE_DSC_MA_FAST 2*HZ /* 2 seconds */
172#define IDETAPE_DSC_MA_THRESHOLD 5*60*HZ /* 5 minutes */
173#define IDETAPE_DSC_MA_SLOW 30*HZ /* 30 seconds */
174#define IDETAPE_DSC_MA_TIMEOUT 2*60*60*HZ /* 2 hours */
175
176/*************************** End of tunable parameters ***********************/
177
178/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 * Read/Write error simulation
180 */
181#define SIMULATE_ERRORS 0
182
183/*
184 * For general magnetic tape device compatibility.
185 */
186typedef enum {
187 idetape_direction_none,
188 idetape_direction_read,
189 idetape_direction_write
190} idetape_chrdev_direction_t;
191
192struct idetape_bh {
Stephen Rothwellab057962007-08-01 23:46:44 +0200193 u32 b_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 atomic_t b_count;
195 struct idetape_bh *b_reqnext;
196 char *b_data;
197};
198
199/*
200 * Our view of a packet command.
201 */
202typedef struct idetape_packet_command_s {
203 u8 c[12]; /* Actual packet bytes */
204 int retries; /* On each retry, we increment retries */
205 int error; /* Error code */
206 int request_transfer; /* Bytes to transfer */
207 int actually_transferred; /* Bytes actually transferred */
208 int buffer_size; /* Size of our data buffer */
209 struct idetape_bh *bh;
210 char *b_data;
211 int b_count;
212 u8 *buffer; /* Data buffer */
213 u8 *current_position; /* Pointer into the above buffer */
214 ide_startstop_t (*callback) (ide_drive_t *); /* Called when this packet command is completed */
215 u8 pc_buffer[IDETAPE_PC_BUFFER_SIZE]; /* Temporary buffer */
216 unsigned long flags; /* Status/Action bit flags: long for set_bit */
217} idetape_pc_t;
218
219/*
220 * Packet command flag bits.
221 */
222/* Set when an error is considered normal - We won't retry */
223#define PC_ABORT 0
224/* 1 When polling for DSC on a media access command */
225#define PC_WAIT_FOR_DSC 1
226/* 1 when we prefer to use DMA if possible */
227#define PC_DMA_RECOMMENDED 2
228/* 1 while DMA in progress */
229#define PC_DMA_IN_PROGRESS 3
230/* 1 when encountered problem during DMA */
231#define PC_DMA_ERROR 4
232/* Data direction */
233#define PC_WRITING 5
234
235/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 * A pipeline stage.
237 */
238typedef struct idetape_stage_s {
239 struct request rq; /* The corresponding request */
240 struct idetape_bh *bh; /* The data buffers */
241 struct idetape_stage_s *next; /* Pointer to the next stage */
242} idetape_stage_t;
243
244/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 * Most of our global data which we need to save even as we leave the
246 * driver due to an interrupt or a timer event is stored in a variable
247 * of type idetape_tape_t, defined below.
248 */
249typedef struct ide_tape_obj {
250 ide_drive_t *drive;
251 ide_driver_t *driver;
252 struct gendisk *disk;
253 struct kref kref;
254
255 /*
256 * Since a typical character device operation requires more
257 * than one packet command, we provide here enough memory
258 * for the maximum of interconnected packet commands.
259 * The packet commands are stored in the circular array pc_stack.
260 * pc_stack_index points to the last used entry, and warps around
261 * to the start when we get to the last array entry.
262 *
263 * pc points to the current processed packet command.
264 *
265 * failed_pc points to the last failed packet command, or contains
266 * NULL if we do not need to retry any packet command. This is
267 * required since an additional packet command is needed before the
268 * retry, to get detailed information on what went wrong.
269 */
270 /* Current packet command */
271 idetape_pc_t *pc;
272 /* Last failed packet command */
273 idetape_pc_t *failed_pc;
274 /* Packet command stack */
275 idetape_pc_t pc_stack[IDETAPE_PC_STACK];
276 /* Next free packet command storage space */
277 int pc_stack_index;
278 struct request rq_stack[IDETAPE_PC_STACK];
279 /* We implement a circular array */
280 int rq_stack_index;
281
282 /*
283 * DSC polling variables.
284 *
285 * While polling for DSC we use postponed_rq to postpone the
286 * current request so that ide.c will be able to service
287 * pending requests on the other device. Note that at most
288 * we will have only one DSC (usually data transfer) request
289 * in the device request queue. Additional requests can be
290 * queued in our internal pipeline, but they will be visible
291 * to ide.c only one at a time.
292 */
293 struct request *postponed_rq;
294 /* The time in which we started polling for DSC */
295 unsigned long dsc_polling_start;
296 /* Timer used to poll for dsc */
297 struct timer_list dsc_timer;
298 /* Read/Write dsc polling frequency */
299 unsigned long best_dsc_rw_frequency;
300 /* The current polling frequency */
301 unsigned long dsc_polling_frequency;
302 /* Maximum waiting time */
303 unsigned long dsc_timeout;
304
305 /*
306 * Read position information
307 */
308 u8 partition;
309 /* Current block */
310 unsigned int first_frame_position;
311 unsigned int last_frame_position;
312 unsigned int blocks_in_buffer;
313
314 /*
315 * Last error information
316 */
317 u8 sense_key, asc, ascq;
318
319 /*
320 * Character device operation
321 */
322 unsigned int minor;
323 /* device name */
324 char name[4];
325 /* Current character device data transfer direction */
326 idetape_chrdev_direction_t chrdev_direction;
327
328 /*
329 * Device information
330 */
331 /* Usually 512 or 1024 bytes */
332 unsigned short tape_block_size;
333 int user_bs_factor;
Borislav Petkovb6422012008-02-02 19:56:49 +0100334
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 /* Copy of the tape's Capabilities and Mechanical Page */
Borislav Petkovb6422012008-02-02 19:56:49 +0100336 u8 caps[20];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337
338 /*
339 * Active data transfer request parameters.
340 *
341 * At most, there is only one ide-tape originated data transfer
342 * request in the device request queue. This allows ide.c to
343 * easily service requests from the other device when we
344 * postpone our active request. In the pipelined operation
345 * mode, we use our internal pipeline structure to hold
346 * more data requests.
347 *
348 * The data buffer size is chosen based on the tape's
349 * recommendation.
350 */
351 /* Pointer to the request which is waiting in the device request queue */
352 struct request *active_data_request;
353 /* Data buffer size (chosen based on the tape's recommendation */
354 int stage_size;
355 idetape_stage_t *merge_stage;
356 int merge_stage_size;
357 struct idetape_bh *bh;
358 char *b_data;
359 int b_count;
360
361 /*
362 * Pipeline parameters.
363 *
364 * To accomplish non-pipelined mode, we simply set the following
365 * variables to zero (or NULL, where appropriate).
366 */
367 /* Number of currently used stages */
368 int nr_stages;
369 /* Number of pending stages */
370 int nr_pending_stages;
371 /* We will not allocate more than this number of stages */
372 int max_stages, min_pipeline, max_pipeline;
373 /* The first stage which will be removed from the pipeline */
374 idetape_stage_t *first_stage;
375 /* The currently active stage */
376 idetape_stage_t *active_stage;
377 /* Will be serviced after the currently active request */
378 idetape_stage_t *next_stage;
379 /* New requests will be added to the pipeline here */
380 idetape_stage_t *last_stage;
381 /* Optional free stage which we can use */
382 idetape_stage_t *cache_stage;
383 int pages_per_stage;
384 /* Wasted space in each stage */
385 int excess_bh_size;
386
387 /* Status/Action flags: long for set_bit */
388 unsigned long flags;
389 /* protects the ide-tape queue */
390 spinlock_t spinlock;
391
392 /*
393 * Measures average tape speed
394 */
395 unsigned long avg_time;
396 int avg_size;
397 int avg_speed;
398
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 char vendor_id[10];
400 char product_id[18];
401 char firmware_revision[6];
402 int firmware_revision_num;
403
404 /* the door is currently locked */
405 int door_locked;
406 /* the tape hardware is write protected */
407 char drv_write_prot;
408 /* the tape is write protected (hardware or opened as read-only) */
409 char write_prot;
410
411 /*
412 * Limit the number of times a request can
413 * be postponed, to avoid an infinite postpone
414 * deadlock.
415 */
416 /* request postpone count limit */
417 int postpone_cnt;
418
419 /*
420 * Measures number of frames:
421 *
422 * 1. written/read to/from the driver pipeline (pipeline_head).
423 * 2. written/read to/from the tape buffers (idetape_bh).
424 * 3. written/read by the tape to/from the media (tape_head).
425 */
426 int pipeline_head;
427 int buffer_head;
428 int tape_head;
429 int last_tape_head;
430
431 /*
432 * Speed control at the tape buffers input/output
433 */
434 unsigned long insert_time;
435 int insert_size;
436 int insert_speed;
437 int max_insert_speed;
438 int measure_insert_time;
439
440 /*
441 * Measure tape still time, in milliseconds
442 */
443 unsigned long tape_still_time_begin;
444 int tape_still_time;
445
446 /*
447 * Speed regulation negative feedback loop
448 */
449 int speed_control;
450 int pipeline_head_speed;
451 int controlled_pipeline_head_speed;
452 int uncontrolled_pipeline_head_speed;
453 int controlled_last_pipeline_head;
454 int uncontrolled_last_pipeline_head;
455 unsigned long uncontrolled_pipeline_head_time;
456 unsigned long controlled_pipeline_head_time;
457 int controlled_previous_pipeline_head;
458 int uncontrolled_previous_pipeline_head;
459 unsigned long controlled_previous_head_time;
460 unsigned long uncontrolled_previous_head_time;
461 int restart_speed_control_req;
462
Borislav Petkov8004a8c2008-02-06 02:57:51 +0100463 u32 debug_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464} idetape_tape_t;
465
Arjan van de Vencf8b8972006-03-23 03:00:45 -0800466static DEFINE_MUTEX(idetape_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467
Will Dysond5dee802005-09-16 02:55:07 -0700468static struct class *idetape_sysfs_class;
469
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470#define to_ide_tape(obj) container_of(obj, struct ide_tape_obj, kref)
471
472#define ide_tape_g(disk) \
473 container_of((disk)->private_data, struct ide_tape_obj, driver)
474
475static struct ide_tape_obj *ide_tape_get(struct gendisk *disk)
476{
477 struct ide_tape_obj *tape = NULL;
478
Arjan van de Vencf8b8972006-03-23 03:00:45 -0800479 mutex_lock(&idetape_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 tape = ide_tape_g(disk);
481 if (tape)
482 kref_get(&tape->kref);
Arjan van de Vencf8b8972006-03-23 03:00:45 -0800483 mutex_unlock(&idetape_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 return tape;
485}
486
487static void ide_tape_release(struct kref *);
488
489static void ide_tape_put(struct ide_tape_obj *tape)
490{
Arjan van de Vencf8b8972006-03-23 03:00:45 -0800491 mutex_lock(&idetape_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 kref_put(&tape->kref, ide_tape_release);
Arjan van de Vencf8b8972006-03-23 03:00:45 -0800493 mutex_unlock(&idetape_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494}
495
496/*
497 * Tape door status
498 */
499#define DOOR_UNLOCKED 0
500#define DOOR_LOCKED 1
501#define DOOR_EXPLICITLY_LOCKED 2
502
503/*
504 * Tape flag bits values.
505 */
506#define IDETAPE_IGNORE_DSC 0
507#define IDETAPE_ADDRESS_VALID 1 /* 0 When the tape position is unknown */
508#define IDETAPE_BUSY 2 /* Device already opened */
509#define IDETAPE_PIPELINE_ERROR 3 /* Error detected in a pipeline stage */
510#define IDETAPE_DETECT_BS 4 /* Attempt to auto-detect the current user block size */
511#define IDETAPE_FILEMARK 5 /* Currently on a filemark */
512#define IDETAPE_DRQ_INTERRUPT 6 /* DRQ interrupt device */
513#define IDETAPE_READ_ERROR 7
514#define IDETAPE_PIPELINE_ACTIVE 8 /* pipeline active */
515/* 0 = no tape is loaded, so we don't rewind after ejecting */
516#define IDETAPE_MEDIUM_PRESENT 9
517
518/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 * Some defines for the READ BUFFER command
520 */
521#define IDETAPE_RETRIEVE_FAULTY_BLOCK 6
522
523/*
524 * Some defines for the SPACE command
525 */
526#define IDETAPE_SPACE_OVER_FILEMARK 1
527#define IDETAPE_SPACE_TO_EOD 3
528
529/*
530 * Some defines for the LOAD UNLOAD command
531 */
532#define IDETAPE_LU_LOAD_MASK 1
533#define IDETAPE_LU_RETENSION_MASK 2
534#define IDETAPE_LU_EOT_MASK 4
535
536/*
537 * Special requests for our block device strategy routine.
538 *
539 * In order to service a character device command, we add special
540 * requests to the tail of our block device request queue and wait
541 * for their completion.
542 */
543
544enum {
545 REQ_IDETAPE_PC1 = (1 << 0), /* packet command (first stage) */
546 REQ_IDETAPE_PC2 = (1 << 1), /* packet command (second stage) */
547 REQ_IDETAPE_READ = (1 << 2),
548 REQ_IDETAPE_WRITE = (1 << 3),
549 REQ_IDETAPE_READ_BUFFER = (1 << 4),
550};
551
552/*
553 * Error codes which are returned in rq->errors to the higher part
554 * of the driver.
555 */
556#define IDETAPE_ERROR_GENERAL 101
557#define IDETAPE_ERROR_FILEMARK 102
558#define IDETAPE_ERROR_EOD 103
559
560/*
561 * The following is used to format the general configuration word of
562 * the ATAPI IDENTIFY DEVICE command.
563 */
564struct idetape_id_gcw {
565 unsigned packet_size :2; /* Packet Size */
566 unsigned reserved234 :3; /* Reserved */
567 unsigned drq_type :2; /* Command packet DRQ type */
568 unsigned removable :1; /* Removable media */
569 unsigned device_type :5; /* Device type */
570 unsigned reserved13 :1; /* Reserved */
571 unsigned protocol :2; /* Protocol type */
572};
573
Borislav Petkovfa366252008-02-02 19:56:51 +0100574/* Structures related to the SELECT SENSE / MODE SENSE packet commands. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575#define IDETAPE_BLOCK_DESCRIPTOR 0
576#define IDETAPE_CAPABILITIES_PAGE 0x2a
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577
578/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 * The variables below are used for the character device interface.
580 * Additional state variables are defined in our ide_drive_t structure.
581 */
582static struct ide_tape_obj * idetape_devs[MAX_HWIFS * MAX_DRIVES];
583
584#define ide_tape_f(file) ((file)->private_data)
585
586static struct ide_tape_obj *ide_tape_chrdev_get(unsigned int i)
587{
588 struct ide_tape_obj *tape = NULL;
589
Arjan van de Vencf8b8972006-03-23 03:00:45 -0800590 mutex_lock(&idetape_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 tape = idetape_devs[i];
592 if (tape)
593 kref_get(&tape->kref);
Arjan van de Vencf8b8972006-03-23 03:00:45 -0800594 mutex_unlock(&idetape_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 return tape;
596}
597
598/*
599 * Function declarations
600 *
601 */
602static int idetape_chrdev_release (struct inode *inode, struct file *filp);
603static void idetape_write_release (ide_drive_t *drive, unsigned int minor);
604
605/*
606 * Too bad. The drive wants to send us data which we are not ready to accept.
607 * Just throw it away.
608 */
609static void idetape_discard_data (ide_drive_t *drive, unsigned int bcount)
610{
611 while (bcount--)
612 (void) HWIF(drive)->INB(IDE_DATA_REG);
613}
614
615static void idetape_input_buffers (ide_drive_t *drive, idetape_pc_t *pc, unsigned int bcount)
616{
617 struct idetape_bh *bh = pc->bh;
618 int count;
619
620 while (bcount) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621 if (bh == NULL) {
622 printk(KERN_ERR "ide-tape: bh == NULL in "
623 "idetape_input_buffers\n");
624 idetape_discard_data(drive, bcount);
625 return;
626 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 count = min((unsigned int)(bh->b_size - atomic_read(&bh->b_count)), bcount);
628 HWIF(drive)->atapi_input_bytes(drive, bh->b_data + atomic_read(&bh->b_count), count);
629 bcount -= count;
630 atomic_add(count, &bh->b_count);
631 if (atomic_read(&bh->b_count) == bh->b_size) {
632 bh = bh->b_reqnext;
633 if (bh)
634 atomic_set(&bh->b_count, 0);
635 }
636 }
637 pc->bh = bh;
638}
639
640static void idetape_output_buffers (ide_drive_t *drive, idetape_pc_t *pc, unsigned int bcount)
641{
642 struct idetape_bh *bh = pc->bh;
643 int count;
644
645 while (bcount) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 if (bh == NULL) {
647 printk(KERN_ERR "ide-tape: bh == NULL in "
648 "idetape_output_buffers\n");
649 return;
650 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 count = min((unsigned int)pc->b_count, (unsigned int)bcount);
652 HWIF(drive)->atapi_output_bytes(drive, pc->b_data, count);
653 bcount -= count;
654 pc->b_data += count;
655 pc->b_count -= count;
656 if (!pc->b_count) {
657 pc->bh = bh = bh->b_reqnext;
658 if (bh) {
659 pc->b_data = bh->b_data;
660 pc->b_count = atomic_read(&bh->b_count);
661 }
662 }
663 }
664}
665
666static void idetape_update_buffers (idetape_pc_t *pc)
667{
668 struct idetape_bh *bh = pc->bh;
669 int count;
670 unsigned int bcount = pc->actually_transferred;
671
672 if (test_bit(PC_WRITING, &pc->flags))
673 return;
674 while (bcount) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675 if (bh == NULL) {
676 printk(KERN_ERR "ide-tape: bh == NULL in "
677 "idetape_update_buffers\n");
678 return;
679 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 count = min((unsigned int)bh->b_size, (unsigned int)bcount);
681 atomic_set(&bh->b_count, count);
682 if (atomic_read(&bh->b_count) == bh->b_size)
683 bh = bh->b_reqnext;
684 bcount -= count;
685 }
686 pc->bh = bh;
687}
688
689/*
690 * idetape_next_pc_storage returns a pointer to a place in which we can
691 * safely store a packet command, even though we intend to leave the
692 * driver. A storage space for a maximum of IDETAPE_PC_STACK packet
693 * commands is allocated at initialization time.
694 */
695static idetape_pc_t *idetape_next_pc_storage (ide_drive_t *drive)
696{
697 idetape_tape_t *tape = drive->driver_data;
698
Borislav Petkov8004a8c2008-02-06 02:57:51 +0100699 debug_log(DBG_PCRQ_STACK, "pc_stack_index=%d\n", tape->pc_stack_index);
700
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 if (tape->pc_stack_index == IDETAPE_PC_STACK)
702 tape->pc_stack_index=0;
703 return (&tape->pc_stack[tape->pc_stack_index++]);
704}
705
706/*
707 * idetape_next_rq_storage is used along with idetape_next_pc_storage.
708 * Since we queue packet commands in the request queue, we need to
709 * allocate a request, along with the allocation of a packet command.
710 */
711
712/**************************************************************
713 * *
714 * This should get fixed to use kmalloc(.., GFP_ATOMIC) *
715 * followed later on by kfree(). -ml *
716 * *
717 **************************************************************/
718
719static struct request *idetape_next_rq_storage (ide_drive_t *drive)
720{
721 idetape_tape_t *tape = drive->driver_data;
722
Borislav Petkov8004a8c2008-02-06 02:57:51 +0100723 debug_log(DBG_PCRQ_STACK, "rq_stack_index=%d\n", tape->rq_stack_index);
724
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 if (tape->rq_stack_index == IDETAPE_PC_STACK)
726 tape->rq_stack_index=0;
727 return (&tape->rq_stack[tape->rq_stack_index++]);
728}
729
730/*
731 * idetape_init_pc initializes a packet command.
732 */
733static void idetape_init_pc (idetape_pc_t *pc)
734{
735 memset(pc->c, 0, 12);
736 pc->retries = 0;
737 pc->flags = 0;
738 pc->request_transfer = 0;
739 pc->buffer = pc->pc_buffer;
740 pc->buffer_size = IDETAPE_PC_BUFFER_SIZE;
741 pc->bh = NULL;
742 pc->b_data = NULL;
743}
744
745/*
Borislav Petkov1b5db432008-02-02 19:56:48 +0100746 * called on each failed packet command retry to analyze the request sense. We
747 * currently do not utilize this information.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748 */
Borislav Petkov1b5db432008-02-02 19:56:48 +0100749static void idetape_analyze_error(ide_drive_t *drive, u8 *sense)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750{
751 idetape_tape_t *tape = drive->driver_data;
752 idetape_pc_t *pc = tape->failed_pc;
753
Borislav Petkov1b5db432008-02-02 19:56:48 +0100754 tape->sense_key = sense[2] & 0xF;
755 tape->asc = sense[12];
756 tape->ascq = sense[13];
Borislav Petkov8004a8c2008-02-06 02:57:51 +0100757
758 debug_log(DBG_ERR, "pc = %x, sense key = %x, asc = %x, ascq = %x\n",
759 pc->c[0], tape->sense_key, tape->asc, tape->ascq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760
Borislav Petkov1b5db432008-02-02 19:56:48 +0100761 /* Correct pc->actually_transferred by asking the tape. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762 if (test_bit(PC_DMA_ERROR, &pc->flags)) {
Borislav Petkov1b5db432008-02-02 19:56:48 +0100763 pc->actually_transferred = pc->request_transfer -
764 tape->tape_block_size *
Borislav Petkov860ff5e2008-02-02 19:56:50 +0100765 be32_to_cpu(get_unaligned((u32 *)&sense[3]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766 idetape_update_buffers(pc);
767 }
768
769 /*
770 * If error was the result of a zero-length read or write command,
771 * with sense key=5, asc=0x22, ascq=0, let it slide. Some drives
772 * (i.e. Seagate STT3401A Travan) don't support 0-length read/writes.
773 */
Borislav Petkov90699ce2008-02-02 19:56:50 +0100774 if ((pc->c[0] == READ_6 || pc->c[0] == WRITE_6)
Borislav Petkov1b5db432008-02-02 19:56:48 +0100775 /* length == 0 */
776 && pc->c[4] == 0 && pc->c[3] == 0 && pc->c[2] == 0) {
777 if (tape->sense_key == 5) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778 /* don't report an error, everything's ok */
779 pc->error = 0;
780 /* don't retry read/write */
781 set_bit(PC_ABORT, &pc->flags);
782 }
783 }
Borislav Petkov90699ce2008-02-02 19:56:50 +0100784 if (pc->c[0] == READ_6 && (sense[2] & 0x80)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785 pc->error = IDETAPE_ERROR_FILEMARK;
786 set_bit(PC_ABORT, &pc->flags);
787 }
Borislav Petkov90699ce2008-02-02 19:56:50 +0100788 if (pc->c[0] == WRITE_6) {
Borislav Petkov1b5db432008-02-02 19:56:48 +0100789 if ((sense[2] & 0x40) || (tape->sense_key == 0xd
790 && tape->asc == 0x0 && tape->ascq == 0x2)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791 pc->error = IDETAPE_ERROR_EOD;
792 set_bit(PC_ABORT, &pc->flags);
793 }
794 }
Borislav Petkov90699ce2008-02-02 19:56:50 +0100795 if (pc->c[0] == READ_6 || pc->c[0] == WRITE_6) {
Borislav Petkov1b5db432008-02-02 19:56:48 +0100796 if (tape->sense_key == 8) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797 pc->error = IDETAPE_ERROR_EOD;
798 set_bit(PC_ABORT, &pc->flags);
799 }
800 if (!test_bit(PC_ABORT, &pc->flags) &&
801 pc->actually_transferred)
802 pc->retries = IDETAPE_MAX_PC_RETRIES + 1;
803 }
804}
805
Borislav Petkov419d4742008-02-02 19:56:51 +0100806static void idetape_activate_next_stage(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807{
808 idetape_tape_t *tape = drive->driver_data;
809 idetape_stage_t *stage = tape->next_stage;
810 struct request *rq = &stage->rq;
811
Borislav Petkov8004a8c2008-02-06 02:57:51 +0100812 debug_log(DBG_PROCS, "Enter %s\n", __func__);
813
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814 if (stage == NULL) {
Borislav Petkov8004a8c2008-02-06 02:57:51 +0100815 printk(KERN_ERR "ide-tape: bug: Trying to activate a non"
816 " existing stage\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817 return;
818 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819
820 rq->rq_disk = tape->disk;
821 rq->buffer = NULL;
822 rq->special = (void *)stage->bh;
823 tape->active_data_request = rq;
824 tape->active_stage = stage;
825 tape->next_stage = stage->next;
826}
827
828/*
829 * idetape_increase_max_pipeline_stages is a part of the feedback
830 * loop which tries to find the optimum number of stages. In the
831 * feedback loop, we are starting from a minimum maximum number of
832 * stages, and if we sense that the pipeline is empty, we try to
833 * increase it, until we reach the user compile time memory limit.
834 */
835static void idetape_increase_max_pipeline_stages (ide_drive_t *drive)
836{
837 idetape_tape_t *tape = drive->driver_data;
838 int increase = (tape->max_pipeline - tape->min_pipeline) / 10;
Borislav Petkov8004a8c2008-02-06 02:57:51 +0100839
840 debug_log(DBG_PROCS, "Enter %s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841
842 tape->max_stages += max(increase, 1);
843 tape->max_stages = max(tape->max_stages, tape->min_pipeline);
844 tape->max_stages = min(tape->max_stages, tape->max_pipeline);
845}
846
847/*
848 * idetape_kfree_stage calls kfree to completely free a stage, along with
849 * its related buffers.
850 */
851static void __idetape_kfree_stage (idetape_stage_t *stage)
852{
853 struct idetape_bh *prev_bh, *bh = stage->bh;
854 int size;
855
856 while (bh != NULL) {
857 if (bh->b_data != NULL) {
858 size = (int) bh->b_size;
859 while (size > 0) {
860 free_page((unsigned long) bh->b_data);
861 size -= PAGE_SIZE;
862 bh->b_data += PAGE_SIZE;
863 }
864 }
865 prev_bh = bh;
866 bh = bh->b_reqnext;
867 kfree(prev_bh);
868 }
869 kfree(stage);
870}
871
872static void idetape_kfree_stage (idetape_tape_t *tape, idetape_stage_t *stage)
873{
874 __idetape_kfree_stage(stage);
875}
876
877/*
878 * idetape_remove_stage_head removes tape->first_stage from the pipeline.
879 * The caller should avoid race conditions.
880 */
881static void idetape_remove_stage_head (ide_drive_t *drive)
882{
883 idetape_tape_t *tape = drive->driver_data;
884 idetape_stage_t *stage;
Borislav Petkov8004a8c2008-02-06 02:57:51 +0100885
886 debug_log(DBG_PROCS, "Enter %s\n", __func__);
887
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888 if (tape->first_stage == NULL) {
889 printk(KERN_ERR "ide-tape: bug: tape->first_stage is NULL\n");
Borislav Petkov55a5d292008-02-02 19:56:49 +0100890 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891 }
892 if (tape->active_stage == tape->first_stage) {
Borislav Petkov8004a8c2008-02-06 02:57:51 +0100893 printk(KERN_ERR "ide-tape: bug: Trying to free our active "
894 "pipeline stage\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895 return;
896 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897 stage = tape->first_stage;
898 tape->first_stage = stage->next;
899 idetape_kfree_stage(tape, stage);
900 tape->nr_stages--;
901 if (tape->first_stage == NULL) {
902 tape->last_stage = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903 if (tape->next_stage != NULL)
904 printk(KERN_ERR "ide-tape: bug: tape->next_stage != NULL\n");
905 if (tape->nr_stages)
906 printk(KERN_ERR "ide-tape: bug: nr_stages should be 0 now\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907 }
908}
909
910/*
911 * This will free all the pipeline stages starting from new_last_stage->next
912 * to the end of the list, and point tape->last_stage to new_last_stage.
913 */
914static void idetape_abort_pipeline(ide_drive_t *drive,
915 idetape_stage_t *new_last_stage)
916{
917 idetape_tape_t *tape = drive->driver_data;
918 idetape_stage_t *stage = new_last_stage->next;
919 idetape_stage_t *nstage;
920
Borislav Petkov8004a8c2008-02-06 02:57:51 +0100921 debug_log(DBG_PROCS, "%s: Enter %s\n", tape->name, __func__);
922
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923 while (stage) {
924 nstage = stage->next;
925 idetape_kfree_stage(tape, stage);
926 --tape->nr_stages;
927 --tape->nr_pending_stages;
928 stage = nstage;
929 }
930 if (new_last_stage)
931 new_last_stage->next = NULL;
932 tape->last_stage = new_last_stage;
933 tape->next_stage = NULL;
934}
935
936/*
937 * idetape_end_request is used to finish servicing a request, and to
938 * insert a pending pipeline request into the main device queue.
939 */
940static int idetape_end_request(ide_drive_t *drive, int uptodate, int nr_sects)
941{
942 struct request *rq = HWGROUP(drive)->rq;
943 idetape_tape_t *tape = drive->driver_data;
944 unsigned long flags;
945 int error;
946 int remove_stage = 0;
947 idetape_stage_t *active_stage;
948
Borislav Petkov8004a8c2008-02-06 02:57:51 +0100949 debug_log(DBG_PROCS, "Enter %s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950
951 switch (uptodate) {
952 case 0: error = IDETAPE_ERROR_GENERAL; break;
953 case 1: error = 0; break;
954 default: error = uptodate;
955 }
956 rq->errors = error;
957 if (error)
958 tape->failed_pc = NULL;
959
Bartlomiej Zolnierkiewicz36872212008-01-26 20:13:10 +0100960 if (!blk_special_request(rq)) {
961 ide_end_request(drive, uptodate, nr_sects);
962 return 0;
963 }
964
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965 spin_lock_irqsave(&tape->spinlock, flags);
966
967 /* The request was a pipelined data transfer request */
968 if (tape->active_data_request == rq) {
969 active_stage = tape->active_stage;
970 tape->active_stage = NULL;
971 tape->active_data_request = NULL;
972 tape->nr_pending_stages--;
973 if (rq->cmd[0] & REQ_IDETAPE_WRITE) {
974 remove_stage = 1;
975 if (error) {
976 set_bit(IDETAPE_PIPELINE_ERROR, &tape->flags);
977 if (error == IDETAPE_ERROR_EOD)
978 idetape_abort_pipeline(drive, active_stage);
979 }
980 } else if (rq->cmd[0] & REQ_IDETAPE_READ) {
981 if (error == IDETAPE_ERROR_EOD) {
982 set_bit(IDETAPE_PIPELINE_ERROR, &tape->flags);
983 idetape_abort_pipeline(drive, active_stage);
984 }
985 }
986 if (tape->next_stage != NULL) {
Borislav Petkov419d4742008-02-02 19:56:51 +0100987 idetape_activate_next_stage(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988
989 /*
990 * Insert the next request into the request queue.
991 */
992 (void) ide_do_drive_cmd(drive, tape->active_data_request, ide_end);
993 } else if (!error) {
994 idetape_increase_max_pipeline_stages(drive);
995 }
996 }
997 ide_end_drive_cmd(drive, 0, 0);
998// blkdev_dequeue_request(rq);
999// drive->rq = NULL;
1000// end_that_request_last(rq);
1001
1002 if (remove_stage)
1003 idetape_remove_stage_head(drive);
1004 if (tape->active_data_request == NULL)
1005 clear_bit(IDETAPE_PIPELINE_ACTIVE, &tape->flags);
1006 spin_unlock_irqrestore(&tape->spinlock, flags);
1007 return 0;
1008}
1009
1010static ide_startstop_t idetape_request_sense_callback (ide_drive_t *drive)
1011{
1012 idetape_tape_t *tape = drive->driver_data;
1013
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001014 debug_log(DBG_PROCS, "Enter %s\n", __func__);
1015
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016 if (!tape->pc->error) {
Borislav Petkov1b5db432008-02-02 19:56:48 +01001017 idetape_analyze_error(drive, tape->pc->buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018 idetape_end_request(drive, 1, 0);
1019 } else {
1020 printk(KERN_ERR "ide-tape: Error in REQUEST SENSE itself - Aborting request!\n");
1021 idetape_end_request(drive, 0, 0);
1022 }
1023 return ide_stopped;
1024}
1025
1026static void idetape_create_request_sense_cmd (idetape_pc_t *pc)
1027{
1028 idetape_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +01001029 pc->c[0] = REQUEST_SENSE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030 pc->c[4] = 20;
1031 pc->request_transfer = 20;
1032 pc->callback = &idetape_request_sense_callback;
1033}
1034
1035static void idetape_init_rq(struct request *rq, u8 cmd)
1036{
1037 memset(rq, 0, sizeof(*rq));
Jens Axboe4aff5e22006-08-10 08:44:47 +02001038 rq->cmd_type = REQ_TYPE_SPECIAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039 rq->cmd[0] = cmd;
1040}
1041
1042/*
1043 * idetape_queue_pc_head generates a new packet command request in front
1044 * of the request queue, before the current request, so that it will be
1045 * processed immediately, on the next pass through the driver.
1046 *
1047 * idetape_queue_pc_head is called from the request handling part of
1048 * the driver (the "bottom" part). Safe storage for the request should
1049 * be allocated with idetape_next_pc_storage and idetape_next_rq_storage
1050 * before calling idetape_queue_pc_head.
1051 *
1052 * Memory for those requests is pre-allocated at initialization time, and
1053 * is limited to IDETAPE_PC_STACK requests. We assume that we have enough
1054 * space for the maximum possible number of inter-dependent packet commands.
1055 *
1056 * The higher level of the driver - The ioctl handler and the character
1057 * device handling functions should queue request to the lower level part
1058 * and wait for their completion using idetape_queue_pc_tail or
1059 * idetape_queue_rw_tail.
1060 */
1061static void idetape_queue_pc_head (ide_drive_t *drive, idetape_pc_t *pc,struct request *rq)
1062{
1063 struct ide_tape_obj *tape = drive->driver_data;
1064
1065 idetape_init_rq(rq, REQ_IDETAPE_PC1);
1066 rq->buffer = (char *) pc;
1067 rq->rq_disk = tape->disk;
1068 (void) ide_do_drive_cmd(drive, rq, ide_preempt);
1069}
1070
1071/*
1072 * idetape_retry_pc is called when an error was detected during the
1073 * last packet command. We queue a request sense packet command in
1074 * the head of the request list.
1075 */
1076static ide_startstop_t idetape_retry_pc (ide_drive_t *drive)
1077{
1078 idetape_tape_t *tape = drive->driver_data;
1079 idetape_pc_t *pc;
1080 struct request *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081
Bartlomiej Zolnierkiewicz64a57fe2008-02-06 02:57:51 +01001082 (void)ide_read_error(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083 pc = idetape_next_pc_storage(drive);
1084 rq = idetape_next_rq_storage(drive);
1085 idetape_create_request_sense_cmd(pc);
1086 set_bit(IDETAPE_IGNORE_DSC, &tape->flags);
1087 idetape_queue_pc_head(drive, pc, rq);
1088 return ide_stopped;
1089}
1090
1091/*
1092 * idetape_postpone_request postpones the current request so that
1093 * ide.c will be able to service requests from another device on
1094 * the same hwgroup while we are polling for DSC.
1095 */
1096static void idetape_postpone_request (ide_drive_t *drive)
1097{
1098 idetape_tape_t *tape = drive->driver_data;
1099
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001100 debug_log(DBG_PROCS, "Enter %s\n", __func__);
1101
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102 tape->postponed_rq = HWGROUP(drive)->rq;
1103 ide_stall_queue(drive, tape->dsc_polling_frequency);
1104}
1105
1106/*
1107 * idetape_pc_intr is the usual interrupt handler which will be called
1108 * during a packet command. We will transfer some of the data (as
1109 * requested by the drive) and will re-point interrupt handler to us.
1110 * When data transfer is finished, we will act according to the
1111 * algorithm described before idetape_issue_packet_command.
1112 *
1113 */
1114static ide_startstop_t idetape_pc_intr (ide_drive_t *drive)
1115{
1116 ide_hwif_t *hwif = drive->hwif;
1117 idetape_tape_t *tape = drive->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118 idetape_pc_t *pc = tape->pc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119 unsigned int temp;
1120#if SIMULATE_ERRORS
1121 static int error_sim_count = 0;
1122#endif
Bartlomiej Zolnierkiewicz790d1232008-01-25 22:17:12 +01001123 u16 bcount;
Bartlomiej Zolnierkiewicz8e7657a2008-01-25 22:17:12 +01001124 u8 stat, ireason;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001126 debug_log(DBG_PROCS, "Enter %s - interrupt handler\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127
1128 /* Clear the interrupt */
Bartlomiej Zolnierkiewiczc47137a2008-02-06 02:57:51 +01001129 stat = ide_read_status(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130
1131 if (test_bit(PC_DMA_IN_PROGRESS, &pc->flags)) {
Bartlomiej Zolnierkiewicz22c525b2008-01-25 22:17:11 +01001132 if (hwif->ide_dma_end(drive) || (stat & ERR_STAT)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133 /*
1134 * A DMA error is sometimes expected. For example,
1135 * if the tape is crossing a filemark during a
1136 * READ command, it will issue an irq and position
1137 * itself before the filemark, so that only a partial
1138 * data transfer will occur (which causes the DMA
1139 * error). In that case, we will later ask the tape
1140 * how much bytes of the original request were
1141 * actually transferred (we can't receive that
1142 * information from the DMA engine on most chipsets).
1143 */
1144
1145 /*
1146 * On the contrary, a DMA error is never expected;
1147 * it usually indicates a hardware error or abort.
1148 * If the tape crosses a filemark during a READ
1149 * command, it will issue an irq and position itself
1150 * after the filemark (not before). Only a partial
1151 * data transfer will occur, but no DMA error.
1152 * (AS, 19 Apr 2001)
1153 */
1154 set_bit(PC_DMA_ERROR, &pc->flags);
1155 } else {
1156 pc->actually_transferred = pc->request_transfer;
1157 idetape_update_buffers(pc);
1158 }
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001159 debug_log(DBG_PROCS, "DMA finished\n");
1160
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161 }
1162
1163 /* No more interrupts */
Bartlomiej Zolnierkiewicz22c525b2008-01-25 22:17:11 +01001164 if ((stat & DRQ_STAT) == 0) {
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001165 debug_log(DBG_SENSE, "Packet command completed, %d bytes"
1166 " transferred\n", pc->actually_transferred);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001168 clear_bit(PC_DMA_IN_PROGRESS, &pc->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169 local_irq_enable();
1170
1171#if SIMULATE_ERRORS
Borislav Petkov90699ce2008-02-02 19:56:50 +01001172 if ((pc->c[0] == WRITE_6 || pc->c[0] == READ_6) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173 (++error_sim_count % 100) == 0) {
1174 printk(KERN_INFO "ide-tape: %s: simulating error\n",
1175 tape->name);
Bartlomiej Zolnierkiewicz22c525b2008-01-25 22:17:11 +01001176 stat |= ERR_STAT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177 }
1178#endif
Borislav Petkov90699ce2008-02-02 19:56:50 +01001179 if ((stat & ERR_STAT) && pc->c[0] == REQUEST_SENSE)
Bartlomiej Zolnierkiewicz22c525b2008-01-25 22:17:11 +01001180 stat &= ~ERR_STAT;
1181 if ((stat & ERR_STAT) || test_bit(PC_DMA_ERROR, &pc->flags)) {
1182 /* Error detected */
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001183 debug_log(DBG_ERR, "%s: I/O error\n", tape->name);
1184
Borislav Petkov90699ce2008-02-02 19:56:50 +01001185 if (pc->c[0] == REQUEST_SENSE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186 printk(KERN_ERR "ide-tape: I/O error in request sense command\n");
1187 return ide_do_reset(drive);
1188 }
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001189 debug_log(DBG_ERR, "[cmd %x]: check condition\n",
1190 pc->c[0]);
1191
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192 /* Retry operation */
1193 return idetape_retry_pc(drive);
1194 }
1195 pc->error = 0;
1196 if (test_bit(PC_WAIT_FOR_DSC, &pc->flags) &&
Bartlomiej Zolnierkiewicz22c525b2008-01-25 22:17:11 +01001197 (stat & SEEK_STAT) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198 /* Media access command */
1199 tape->dsc_polling_start = jiffies;
1200 tape->dsc_polling_frequency = IDETAPE_DSC_MA_FAST;
1201 tape->dsc_timeout = jiffies + IDETAPE_DSC_MA_TIMEOUT;
1202 /* Allow ide.c to handle other requests */
1203 idetape_postpone_request(drive);
1204 return ide_stopped;
1205 }
1206 if (tape->failed_pc == pc)
1207 tape->failed_pc = NULL;
1208 /* Command finished - Call the callback function */
1209 return pc->callback(drive);
1210 }
1211 if (test_and_clear_bit(PC_DMA_IN_PROGRESS, &pc->flags)) {
1212 printk(KERN_ERR "ide-tape: The tape wants to issue more "
1213 "interrupts in DMA mode\n");
1214 printk(KERN_ERR "ide-tape: DMA disabled, reverting to PIO\n");
Bartlomiej Zolnierkiewicz7469aaf2007-02-17 02:40:26 +01001215 ide_dma_off(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216 return ide_do_reset(drive);
1217 }
1218 /* Get the number of bytes to transfer on this interrupt. */
Bartlomiej Zolnierkiewicz790d1232008-01-25 22:17:12 +01001219 bcount = (hwif->INB(IDE_BCOUNTH_REG) << 8) |
1220 hwif->INB(IDE_BCOUNTL_REG);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221
Bartlomiej Zolnierkiewicz8e7657a2008-01-25 22:17:12 +01001222 ireason = hwif->INB(IDE_IREASON_REG);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223
Bartlomiej Zolnierkiewicz8e7657a2008-01-25 22:17:12 +01001224 if (ireason & CD) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225 printk(KERN_ERR "ide-tape: CoD != 0 in idetape_pc_intr\n");
1226 return ide_do_reset(drive);
1227 }
Bartlomiej Zolnierkiewicz8e7657a2008-01-25 22:17:12 +01001228 if (((ireason & IO) == IO) == test_bit(PC_WRITING, &pc->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229 /* Hopefully, we will never get here */
1230 printk(KERN_ERR "ide-tape: We wanted to %s, ",
Bartlomiej Zolnierkiewicz8e7657a2008-01-25 22:17:12 +01001231 (ireason & IO) ? "Write" : "Read");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232 printk(KERN_ERR "ide-tape: but the tape wants us to %s !\n",
Bartlomiej Zolnierkiewicz8e7657a2008-01-25 22:17:12 +01001233 (ireason & IO) ? "Read" : "Write");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234 return ide_do_reset(drive);
1235 }
1236 if (!test_bit(PC_WRITING, &pc->flags)) {
1237 /* Reading - Check that we have enough space */
Bartlomiej Zolnierkiewicz790d1232008-01-25 22:17:12 +01001238 temp = pc->actually_transferred + bcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239 if (temp > pc->request_transfer) {
1240 if (temp > pc->buffer_size) {
1241 printk(KERN_ERR "ide-tape: The tape wants to send us more data than expected - discarding data\n");
Bartlomiej Zolnierkiewicz790d1232008-01-25 22:17:12 +01001242 idetape_discard_data(drive, bcount);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243 ide_set_handler(drive, &idetape_pc_intr, IDETAPE_WAIT_CMD, NULL);
1244 return ide_started;
1245 }
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001246 debug_log(DBG_SENSE, "The tape wants to send us more "
1247 "data than expected - allowing transfer\n");
1248
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249 }
1250 }
1251 if (test_bit(PC_WRITING, &pc->flags)) {
1252 if (pc->bh != NULL)
Bartlomiej Zolnierkiewicz790d1232008-01-25 22:17:12 +01001253 idetape_output_buffers(drive, pc, bcount);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254 else
1255 /* Write the current buffer */
Bartlomiej Zolnierkiewicz790d1232008-01-25 22:17:12 +01001256 hwif->atapi_output_bytes(drive, pc->current_position,
1257 bcount);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258 } else {
1259 if (pc->bh != NULL)
Bartlomiej Zolnierkiewicz790d1232008-01-25 22:17:12 +01001260 idetape_input_buffers(drive, pc, bcount);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261 else
1262 /* Read the current buffer */
Bartlomiej Zolnierkiewicz790d1232008-01-25 22:17:12 +01001263 hwif->atapi_input_bytes(drive, pc->current_position,
1264 bcount);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265 }
1266 /* Update the current position */
Bartlomiej Zolnierkiewicz790d1232008-01-25 22:17:12 +01001267 pc->actually_transferred += bcount;
1268 pc->current_position += bcount;
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001269
1270 debug_log(DBG_SENSE, "[cmd %x] transferred %d bytes on that intr.\n",
1271 pc->c[0], bcount);
1272
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273 /* And set the interrupt handler again */
1274 ide_set_handler(drive, &idetape_pc_intr, IDETAPE_WAIT_CMD, NULL);
1275 return ide_started;
1276}
1277
1278/*
1279 * Packet Command Interface
1280 *
1281 * The current Packet Command is available in tape->pc, and will not
1282 * change until we finish handling it. Each packet command is associated
1283 * with a callback function that will be called when the command is
1284 * finished.
1285 *
1286 * The handling will be done in three stages:
1287 *
1288 * 1. idetape_issue_packet_command will send the packet command to the
1289 * drive, and will set the interrupt handler to idetape_pc_intr.
1290 *
1291 * 2. On each interrupt, idetape_pc_intr will be called. This step
1292 * will be repeated until the device signals us that no more
1293 * interrupts will be issued.
1294 *
1295 * 3. ATAPI Tape media access commands have immediate status with a
1296 * delayed process. In case of a successful initiation of a
1297 * media access packet command, the DSC bit will be set when the
1298 * actual execution of the command is finished.
1299 * Since the tape drive will not issue an interrupt, we have to
1300 * poll for this event. In this case, we define the request as
1301 * "low priority request" by setting rq_status to
1302 * IDETAPE_RQ_POSTPONED, set a timer to poll for DSC and exit
1303 * the driver.
1304 *
1305 * ide.c will then give higher priority to requests which
1306 * originate from the other device, until will change rq_status
1307 * to RQ_ACTIVE.
1308 *
1309 * 4. When the packet command is finished, it will be checked for errors.
1310 *
1311 * 5. In case an error was found, we queue a request sense packet
1312 * command in front of the request queue and retry the operation
1313 * up to IDETAPE_MAX_PC_RETRIES times.
1314 *
1315 * 6. In case no error was found, or we decided to give up and not
1316 * to retry again, the callback function will be called and then
1317 * we will handle the next request.
1318 *
1319 */
1320static ide_startstop_t idetape_transfer_pc(ide_drive_t *drive)
1321{
1322 ide_hwif_t *hwif = drive->hwif;
1323 idetape_tape_t *tape = drive->driver_data;
1324 idetape_pc_t *pc = tape->pc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325 int retries = 100;
1326 ide_startstop_t startstop;
Bartlomiej Zolnierkiewicz8e7657a2008-01-25 22:17:12 +01001327 u8 ireason;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328
1329 if (ide_wait_stat(&startstop,drive,DRQ_STAT,BUSY_STAT,WAIT_READY)) {
1330 printk(KERN_ERR "ide-tape: Strange, packet command initiated yet DRQ isn't asserted\n");
1331 return startstop;
1332 }
Bartlomiej Zolnierkiewicz8e7657a2008-01-25 22:17:12 +01001333 ireason = hwif->INB(IDE_IREASON_REG);
1334 while (retries-- && ((ireason & CD) == 0 || (ireason & IO))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335 printk(KERN_ERR "ide-tape: (IO,CoD != (0,1) while issuing "
1336 "a packet command, retrying\n");
1337 udelay(100);
Bartlomiej Zolnierkiewicz8e7657a2008-01-25 22:17:12 +01001338 ireason = hwif->INB(IDE_IREASON_REG);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339 if (retries == 0) {
1340 printk(KERN_ERR "ide-tape: (IO,CoD != (0,1) while "
1341 "issuing a packet command, ignoring\n");
Bartlomiej Zolnierkiewicz8e7657a2008-01-25 22:17:12 +01001342 ireason |= CD;
1343 ireason &= ~IO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344 }
1345 }
Bartlomiej Zolnierkiewicz8e7657a2008-01-25 22:17:12 +01001346 if ((ireason & CD) == 0 || (ireason & IO)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347 printk(KERN_ERR "ide-tape: (IO,CoD) != (0,1) while issuing "
1348 "a packet command\n");
1349 return ide_do_reset(drive);
1350 }
1351 /* Set the interrupt routine */
1352 ide_set_handler(drive, &idetape_pc_intr, IDETAPE_WAIT_CMD, NULL);
1353#ifdef CONFIG_BLK_DEV_IDEDMA
1354 /* Begin DMA, if necessary */
1355 if (test_bit(PC_DMA_IN_PROGRESS, &pc->flags))
1356 hwif->dma_start(drive);
1357#endif
1358 /* Send the actual packet */
1359 HWIF(drive)->atapi_output_bytes(drive, pc->c, 12);
1360 return ide_started;
1361}
1362
1363static ide_startstop_t idetape_issue_packet_command (ide_drive_t *drive, idetape_pc_t *pc)
1364{
1365 ide_hwif_t *hwif = drive->hwif;
1366 idetape_tape_t *tape = drive->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367 int dma_ok = 0;
Bartlomiej Zolnierkiewicz790d1232008-01-25 22:17:12 +01001368 u16 bcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369
Borislav Petkov90699ce2008-02-02 19:56:50 +01001370 if (tape->pc->c[0] == REQUEST_SENSE &&
1371 pc->c[0] == REQUEST_SENSE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372 printk(KERN_ERR "ide-tape: possible ide-tape.c bug - "
1373 "Two request sense in serial were issued\n");
1374 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375
Borislav Petkov90699ce2008-02-02 19:56:50 +01001376 if (tape->failed_pc == NULL && pc->c[0] != REQUEST_SENSE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377 tape->failed_pc = pc;
1378 /* Set the current packet command */
1379 tape->pc = pc;
1380
1381 if (pc->retries > IDETAPE_MAX_PC_RETRIES ||
1382 test_bit(PC_ABORT, &pc->flags)) {
1383 /*
1384 * We will "abort" retrying a packet command in case
1385 * a legitimate error code was received (crossing a
1386 * filemark, or end of the media, for example).
1387 */
1388 if (!test_bit(PC_ABORT, &pc->flags)) {
Borislav Petkov90699ce2008-02-02 19:56:50 +01001389 if (!(pc->c[0] == TEST_UNIT_READY &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390 tape->sense_key == 2 && tape->asc == 4 &&
1391 (tape->ascq == 1 || tape->ascq == 8))) {
1392 printk(KERN_ERR "ide-tape: %s: I/O error, "
1393 "pc = %2x, key = %2x, "
1394 "asc = %2x, ascq = %2x\n",
1395 tape->name, pc->c[0],
1396 tape->sense_key, tape->asc,
1397 tape->ascq);
1398 }
1399 /* Giving up */
1400 pc->error = IDETAPE_ERROR_GENERAL;
1401 }
1402 tape->failed_pc = NULL;
1403 return pc->callback(drive);
1404 }
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001405 debug_log(DBG_SENSE, "Retry #%d, cmd = %02X\n", pc->retries, pc->c[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406
1407 pc->retries++;
1408 /* We haven't transferred any data yet */
1409 pc->actually_transferred = 0;
1410 pc->current_position = pc->buffer;
1411 /* Request to transfer the entire buffer at once */
Bartlomiej Zolnierkiewicz790d1232008-01-25 22:17:12 +01001412 bcount = pc->request_transfer;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413
1414 if (test_and_clear_bit(PC_DMA_ERROR, &pc->flags)) {
1415 printk(KERN_WARNING "ide-tape: DMA disabled, "
1416 "reverting to PIO\n");
Bartlomiej Zolnierkiewicz7469aaf2007-02-17 02:40:26 +01001417 ide_dma_off(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418 }
1419 if (test_bit(PC_DMA_RECOMMENDED, &pc->flags) && drive->using_dma)
1420 dma_ok = !hwif->dma_setup(drive);
1421
Bartlomiej Zolnierkiewicz2fc57382008-01-25 22:17:13 +01001422 ide_pktcmd_tf_load(drive, IDE_TFLAG_NO_SELECT_MASK |
1423 IDE_TFLAG_OUT_DEVICE, bcount, dma_ok);
1424
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425 if (dma_ok) /* Will begin DMA later */
1426 set_bit(PC_DMA_IN_PROGRESS, &pc->flags);
1427 if (test_bit(IDETAPE_DRQ_INTERRUPT, &tape->flags)) {
Bartlomiej Zolnierkiewiczc1c9dbc2008-02-02 19:56:44 +01001428 ide_execute_command(drive, WIN_PACKETCMD, &idetape_transfer_pc,
1429 IDETAPE_WAIT_CMD, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430 return ide_started;
1431 } else {
1432 hwif->OUTB(WIN_PACKETCMD, IDE_COMMAND_REG);
1433 return idetape_transfer_pc(drive);
1434 }
1435}
1436
1437/*
1438 * General packet command callback function.
1439 */
1440static ide_startstop_t idetape_pc_callback (ide_drive_t *drive)
1441{
1442 idetape_tape_t *tape = drive->driver_data;
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001443
1444 debug_log(DBG_PROCS, "Enter %s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445
1446 idetape_end_request(drive, tape->pc->error ? 0 : 1, 0);
1447 return ide_stopped;
1448}
1449
1450/*
1451 * A mode sense command is used to "sense" tape parameters.
1452 */
1453static void idetape_create_mode_sense_cmd (idetape_pc_t *pc, u8 page_code)
1454{
1455 idetape_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +01001456 pc->c[0] = MODE_SENSE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457 if (page_code != IDETAPE_BLOCK_DESCRIPTOR)
1458 pc->c[1] = 8; /* DBD = 1 - Don't return block descriptors */
1459 pc->c[2] = page_code;
1460 /*
1461 * Changed pc->c[3] to 0 (255 will at best return unused info).
1462 *
1463 * For SCSI this byte is defined as subpage instead of high byte
1464 * of length and some IDE drives seem to interpret it this way
1465 * and return an error when 255 is used.
1466 */
1467 pc->c[3] = 0;
1468 pc->c[4] = 255; /* (We will just discard data in that case) */
1469 if (page_code == IDETAPE_BLOCK_DESCRIPTOR)
1470 pc->request_transfer = 12;
1471 else if (page_code == IDETAPE_CAPABILITIES_PAGE)
1472 pc->request_transfer = 24;
1473 else
1474 pc->request_transfer = 50;
1475 pc->callback = &idetape_pc_callback;
1476}
1477
1478static void calculate_speeds(ide_drive_t *drive)
1479{
1480 idetape_tape_t *tape = drive->driver_data;
1481 int full = 125, empty = 75;
1482
1483 if (time_after(jiffies, tape->controlled_pipeline_head_time + 120 * HZ)) {
1484 tape->controlled_previous_pipeline_head = tape->controlled_last_pipeline_head;
1485 tape->controlled_previous_head_time = tape->controlled_pipeline_head_time;
1486 tape->controlled_last_pipeline_head = tape->pipeline_head;
1487 tape->controlled_pipeline_head_time = jiffies;
1488 }
1489 if (time_after(jiffies, tape->controlled_pipeline_head_time + 60 * HZ))
1490 tape->controlled_pipeline_head_speed = (tape->pipeline_head - tape->controlled_last_pipeline_head) * 32 * HZ / (jiffies - tape->controlled_pipeline_head_time);
1491 else if (time_after(jiffies, tape->controlled_previous_head_time))
1492 tape->controlled_pipeline_head_speed = (tape->pipeline_head - tape->controlled_previous_pipeline_head) * 32 * HZ / (jiffies - tape->controlled_previous_head_time);
1493
1494 if (tape->nr_pending_stages < tape->max_stages /*- 1 */) {
1495 /* -1 for read mode error recovery */
1496 if (time_after(jiffies, tape->uncontrolled_previous_head_time + 10 * HZ)) {
1497 tape->uncontrolled_pipeline_head_time = jiffies;
1498 tape->uncontrolled_pipeline_head_speed = (tape->pipeline_head - tape->uncontrolled_previous_pipeline_head) * 32 * HZ / (jiffies - tape->uncontrolled_previous_head_time);
1499 }
1500 } else {
1501 tape->uncontrolled_previous_head_time = jiffies;
1502 tape->uncontrolled_previous_pipeline_head = tape->pipeline_head;
1503 if (time_after(jiffies, tape->uncontrolled_pipeline_head_time + 30 * HZ)) {
1504 tape->uncontrolled_pipeline_head_time = jiffies;
1505 }
1506 }
1507 tape->pipeline_head_speed = max(tape->uncontrolled_pipeline_head_speed, tape->controlled_pipeline_head_speed);
1508 if (tape->speed_control == 0) {
1509 tape->max_insert_speed = 5000;
1510 } else if (tape->speed_control == 1) {
1511 if (tape->nr_pending_stages >= tape->max_stages / 2)
1512 tape->max_insert_speed = tape->pipeline_head_speed +
1513 (1100 - tape->pipeline_head_speed) * 2 * (tape->nr_pending_stages - tape->max_stages / 2) / tape->max_stages;
1514 else
1515 tape->max_insert_speed = 500 +
1516 (tape->pipeline_head_speed - 500) * 2 * tape->nr_pending_stages / tape->max_stages;
1517 if (tape->nr_pending_stages >= tape->max_stages * 99 / 100)
1518 tape->max_insert_speed = 5000;
1519 } else if (tape->speed_control == 2) {
1520 tape->max_insert_speed = tape->pipeline_head_speed * empty / 100 +
1521 (tape->pipeline_head_speed * full / 100 - tape->pipeline_head_speed * empty / 100) * tape->nr_pending_stages / tape->max_stages;
1522 } else
1523 tape->max_insert_speed = tape->speed_control;
1524 tape->max_insert_speed = max(tape->max_insert_speed, 500);
1525}
1526
1527static ide_startstop_t idetape_media_access_finished (ide_drive_t *drive)
1528{
1529 idetape_tape_t *tape = drive->driver_data;
1530 idetape_pc_t *pc = tape->pc;
Bartlomiej Zolnierkiewicz22c525b2008-01-25 22:17:11 +01001531 u8 stat;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532
Bartlomiej Zolnierkiewiczc47137a2008-02-06 02:57:51 +01001533 stat = ide_read_status(drive);
1534
Bartlomiej Zolnierkiewicz22c525b2008-01-25 22:17:11 +01001535 if (stat & SEEK_STAT) {
1536 if (stat & ERR_STAT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537 /* Error detected */
Borislav Petkov90699ce2008-02-02 19:56:50 +01001538 if (pc->c[0] != TEST_UNIT_READY)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539 printk(KERN_ERR "ide-tape: %s: I/O error, ",
1540 tape->name);
1541 /* Retry operation */
1542 return idetape_retry_pc(drive);
1543 }
1544 pc->error = 0;
1545 if (tape->failed_pc == pc)
1546 tape->failed_pc = NULL;
1547 } else {
1548 pc->error = IDETAPE_ERROR_GENERAL;
1549 tape->failed_pc = NULL;
1550 }
1551 return pc->callback(drive);
1552}
1553
1554static ide_startstop_t idetape_rw_callback (ide_drive_t *drive)
1555{
1556 idetape_tape_t *tape = drive->driver_data;
1557 struct request *rq = HWGROUP(drive)->rq;
1558 int blocks = tape->pc->actually_transferred / tape->tape_block_size;
1559
1560 tape->avg_size += blocks * tape->tape_block_size;
1561 tape->insert_size += blocks * tape->tape_block_size;
1562 if (tape->insert_size > 1024 * 1024)
1563 tape->measure_insert_time = 1;
1564 if (tape->measure_insert_time) {
1565 tape->measure_insert_time = 0;
1566 tape->insert_time = jiffies;
1567 tape->insert_size = 0;
1568 }
1569 if (time_after(jiffies, tape->insert_time))
1570 tape->insert_speed = tape->insert_size / 1024 * HZ / (jiffies - tape->insert_time);
Marcelo Feitoza Parisi9bae1ff2006-03-28 01:56:46 -08001571 if (time_after_eq(jiffies, tape->avg_time + HZ)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572 tape->avg_speed = tape->avg_size * HZ / (jiffies - tape->avg_time) / 1024;
1573 tape->avg_size = 0;
1574 tape->avg_time = jiffies;
1575 }
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001576 debug_log(DBG_PROCS, "Enter %s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001577
1578 tape->first_frame_position += blocks;
1579 rq->current_nr_sectors -= blocks;
1580
1581 if (!tape->pc->error)
1582 idetape_end_request(drive, 1, 0);
1583 else
1584 idetape_end_request(drive, tape->pc->error, 0);
1585 return ide_stopped;
1586}
1587
1588static void idetape_create_read_cmd(idetape_tape_t *tape, idetape_pc_t *pc, unsigned int length, struct idetape_bh *bh)
1589{
1590 idetape_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +01001591 pc->c[0] = READ_6;
Borislav Petkov860ff5e2008-02-02 19:56:50 +01001592 put_unaligned(cpu_to_be32(length), (unsigned int *) &pc->c[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001593 pc->c[1] = 1;
1594 pc->callback = &idetape_rw_callback;
1595 pc->bh = bh;
1596 atomic_set(&bh->b_count, 0);
1597 pc->buffer = NULL;
1598 pc->request_transfer = pc->buffer_size = length * tape->tape_block_size;
1599 if (pc->request_transfer == tape->stage_size)
1600 set_bit(PC_DMA_RECOMMENDED, &pc->flags);
1601}
1602
1603static void idetape_create_read_buffer_cmd(idetape_tape_t *tape, idetape_pc_t *pc, unsigned int length, struct idetape_bh *bh)
1604{
1605 int size = 32768;
1606 struct idetape_bh *p = bh;
1607
1608 idetape_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +01001609 pc->c[0] = READ_BUFFER;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610 pc->c[1] = IDETAPE_RETRIEVE_FAULTY_BLOCK;
1611 pc->c[7] = size >> 8;
1612 pc->c[8] = size & 0xff;
1613 pc->callback = &idetape_pc_callback;
1614 pc->bh = bh;
1615 atomic_set(&bh->b_count, 0);
1616 pc->buffer = NULL;
1617 while (p) {
1618 atomic_set(&p->b_count, 0);
1619 p = p->b_reqnext;
1620 }
1621 pc->request_transfer = pc->buffer_size = size;
1622}
1623
1624static void idetape_create_write_cmd(idetape_tape_t *tape, idetape_pc_t *pc, unsigned int length, struct idetape_bh *bh)
1625{
1626 idetape_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +01001627 pc->c[0] = WRITE_6;
Borislav Petkov860ff5e2008-02-02 19:56:50 +01001628 put_unaligned(cpu_to_be32(length), (unsigned int *) &pc->c[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001629 pc->c[1] = 1;
1630 pc->callback = &idetape_rw_callback;
1631 set_bit(PC_WRITING, &pc->flags);
1632 pc->bh = bh;
1633 pc->b_data = bh->b_data;
1634 pc->b_count = atomic_read(&bh->b_count);
1635 pc->buffer = NULL;
1636 pc->request_transfer = pc->buffer_size = length * tape->tape_block_size;
1637 if (pc->request_transfer == tape->stage_size)
1638 set_bit(PC_DMA_RECOMMENDED, &pc->flags);
1639}
1640
1641/*
1642 * idetape_do_request is our request handling function.
1643 */
1644static ide_startstop_t idetape_do_request(ide_drive_t *drive,
1645 struct request *rq, sector_t block)
1646{
1647 idetape_tape_t *tape = drive->driver_data;
1648 idetape_pc_t *pc = NULL;
1649 struct request *postponed_rq = tape->postponed_rq;
Bartlomiej Zolnierkiewicz22c525b2008-01-25 22:17:11 +01001650 u8 stat;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001651
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001652 debug_log(DBG_SENSE, "sector: %ld, nr_sectors: %ld,"
1653 " current_nr_sectors: %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654 rq->sector, rq->nr_sectors, rq->current_nr_sectors);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001655
Jens Axboe4aff5e22006-08-10 08:44:47 +02001656 if (!blk_special_request(rq)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001657 /*
1658 * We do not support buffer cache originated requests.
1659 */
1660 printk(KERN_NOTICE "ide-tape: %s: Unsupported request in "
Jens Axboe4aff5e22006-08-10 08:44:47 +02001661 "request queue (%d)\n", drive->name, rq->cmd_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001662 ide_end_request(drive, 0, 0);
1663 return ide_stopped;
1664 }
1665
1666 /*
1667 * Retry a failed packet command
1668 */
1669 if (tape->failed_pc != NULL &&
Borislav Petkov90699ce2008-02-02 19:56:50 +01001670 tape->pc->c[0] == REQUEST_SENSE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001671 return idetape_issue_packet_command(drive, tape->failed_pc);
1672 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001673 if (postponed_rq != NULL)
1674 if (rq != postponed_rq) {
1675 printk(KERN_ERR "ide-tape: ide-tape.c bug - "
1676 "Two DSC requests were queued\n");
1677 idetape_end_request(drive, 0, 0);
1678 return ide_stopped;
1679 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001680
1681 tape->postponed_rq = NULL;
1682
1683 /*
1684 * If the tape is still busy, postpone our request and service
1685 * the other device meanwhile.
1686 */
Bartlomiej Zolnierkiewiczc47137a2008-02-06 02:57:51 +01001687 stat = ide_read_status(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001688
1689 if (!drive->dsc_overlap && !(rq->cmd[0] & REQ_IDETAPE_PC2))
1690 set_bit(IDETAPE_IGNORE_DSC, &tape->flags);
1691
1692 if (drive->post_reset == 1) {
1693 set_bit(IDETAPE_IGNORE_DSC, &tape->flags);
1694 drive->post_reset = 0;
1695 }
1696
1697 if (tape->tape_still_time > 100 && tape->tape_still_time < 200)
1698 tape->measure_insert_time = 1;
1699 if (time_after(jiffies, tape->insert_time))
1700 tape->insert_speed = tape->insert_size / 1024 * HZ / (jiffies - tape->insert_time);
1701 calculate_speeds(drive);
1702 if (!test_and_clear_bit(IDETAPE_IGNORE_DSC, &tape->flags) &&
Bartlomiej Zolnierkiewicz22c525b2008-01-25 22:17:11 +01001703 (stat & SEEK_STAT) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001704 if (postponed_rq == NULL) {
1705 tape->dsc_polling_start = jiffies;
1706 tape->dsc_polling_frequency = tape->best_dsc_rw_frequency;
1707 tape->dsc_timeout = jiffies + IDETAPE_DSC_RW_TIMEOUT;
1708 } else if (time_after(jiffies, tape->dsc_timeout)) {
1709 printk(KERN_ERR "ide-tape: %s: DSC timeout\n",
1710 tape->name);
1711 if (rq->cmd[0] & REQ_IDETAPE_PC2) {
1712 idetape_media_access_finished(drive);
1713 return ide_stopped;
1714 } else {
1715 return ide_do_reset(drive);
1716 }
Marcelo Feitoza Parisi9bae1ff2006-03-28 01:56:46 -08001717 } else if (time_after(jiffies, tape->dsc_polling_start + IDETAPE_DSC_MA_THRESHOLD))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001718 tape->dsc_polling_frequency = IDETAPE_DSC_MA_SLOW;
1719 idetape_postpone_request(drive);
1720 return ide_stopped;
1721 }
1722 if (rq->cmd[0] & REQ_IDETAPE_READ) {
1723 tape->buffer_head++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001724 tape->postpone_cnt = 0;
1725 pc = idetape_next_pc_storage(drive);
1726 idetape_create_read_cmd(tape, pc, rq->current_nr_sectors, (struct idetape_bh *)rq->special);
1727 goto out;
1728 }
1729 if (rq->cmd[0] & REQ_IDETAPE_WRITE) {
1730 tape->buffer_head++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001731 tape->postpone_cnt = 0;
1732 pc = idetape_next_pc_storage(drive);
1733 idetape_create_write_cmd(tape, pc, rq->current_nr_sectors, (struct idetape_bh *)rq->special);
1734 goto out;
1735 }
1736 if (rq->cmd[0] & REQ_IDETAPE_READ_BUFFER) {
1737 tape->postpone_cnt = 0;
1738 pc = idetape_next_pc_storage(drive);
1739 idetape_create_read_buffer_cmd(tape, pc, rq->current_nr_sectors, (struct idetape_bh *)rq->special);
1740 goto out;
1741 }
1742 if (rq->cmd[0] & REQ_IDETAPE_PC1) {
1743 pc = (idetape_pc_t *) rq->buffer;
1744 rq->cmd[0] &= ~(REQ_IDETAPE_PC1);
1745 rq->cmd[0] |= REQ_IDETAPE_PC2;
1746 goto out;
1747 }
1748 if (rq->cmd[0] & REQ_IDETAPE_PC2) {
1749 idetape_media_access_finished(drive);
1750 return ide_stopped;
1751 }
1752 BUG();
1753out:
1754 return idetape_issue_packet_command(drive, pc);
1755}
1756
1757/*
1758 * Pipeline related functions
1759 */
1760static inline int idetape_pipeline_active (idetape_tape_t *tape)
1761{
1762 int rc1, rc2;
1763
1764 rc1 = test_bit(IDETAPE_PIPELINE_ACTIVE, &tape->flags);
1765 rc2 = (tape->active_data_request != NULL);
1766 return rc1;
1767}
1768
1769/*
1770 * idetape_kmalloc_stage uses __get_free_page to allocate a pipeline
1771 * stage, along with all the necessary small buffers which together make
1772 * a buffer of size tape->stage_size (or a bit more). We attempt to
1773 * combine sequential pages as much as possible.
1774 *
1775 * Returns a pointer to the new allocated stage, or NULL if we
1776 * can't (or don't want to) allocate a stage.
1777 *
1778 * Pipeline stages are optional and are used to increase performance.
1779 * If we can't allocate them, we'll manage without them.
1780 */
1781static idetape_stage_t *__idetape_kmalloc_stage (idetape_tape_t *tape, int full, int clear)
1782{
1783 idetape_stage_t *stage;
1784 struct idetape_bh *prev_bh, *bh;
1785 int pages = tape->pages_per_stage;
1786 char *b_data = NULL;
1787
Robert P. J. Day5cbded52006-12-13 00:35:56 -08001788 if ((stage = kmalloc(sizeof (idetape_stage_t),GFP_KERNEL)) == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001789 return NULL;
1790 stage->next = NULL;
1791
Robert P. J. Day5cbded52006-12-13 00:35:56 -08001792 bh = stage->bh = kmalloc(sizeof(struct idetape_bh), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001793 if (bh == NULL)
1794 goto abort;
1795 bh->b_reqnext = NULL;
1796 if ((bh->b_data = (char *) __get_free_page (GFP_KERNEL)) == NULL)
1797 goto abort;
1798 if (clear)
1799 memset(bh->b_data, 0, PAGE_SIZE);
1800 bh->b_size = PAGE_SIZE;
1801 atomic_set(&bh->b_count, full ? bh->b_size : 0);
1802
1803 while (--pages) {
1804 if ((b_data = (char *) __get_free_page (GFP_KERNEL)) == NULL)
1805 goto abort;
1806 if (clear)
1807 memset(b_data, 0, PAGE_SIZE);
1808 if (bh->b_data == b_data + PAGE_SIZE) {
1809 bh->b_size += PAGE_SIZE;
1810 bh->b_data -= PAGE_SIZE;
1811 if (full)
1812 atomic_add(PAGE_SIZE, &bh->b_count);
1813 continue;
1814 }
1815 if (b_data == bh->b_data + bh->b_size) {
1816 bh->b_size += PAGE_SIZE;
1817 if (full)
1818 atomic_add(PAGE_SIZE, &bh->b_count);
1819 continue;
1820 }
1821 prev_bh = bh;
Robert P. J. Day5cbded52006-12-13 00:35:56 -08001822 if ((bh = kmalloc(sizeof(struct idetape_bh), GFP_KERNEL)) == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001823 free_page((unsigned long) b_data);
1824 goto abort;
1825 }
1826 bh->b_reqnext = NULL;
1827 bh->b_data = b_data;
1828 bh->b_size = PAGE_SIZE;
1829 atomic_set(&bh->b_count, full ? bh->b_size : 0);
1830 prev_bh->b_reqnext = bh;
1831 }
1832 bh->b_size -= tape->excess_bh_size;
1833 if (full)
1834 atomic_sub(tape->excess_bh_size, &bh->b_count);
1835 return stage;
1836abort:
1837 __idetape_kfree_stage(stage);
1838 return NULL;
1839}
1840
1841static idetape_stage_t *idetape_kmalloc_stage (idetape_tape_t *tape)
1842{
1843 idetape_stage_t *cache_stage = tape->cache_stage;
1844
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001845 debug_log(DBG_PROCS, "Enter %s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001846
1847 if (tape->nr_stages >= tape->max_stages)
1848 return NULL;
1849 if (cache_stage != NULL) {
1850 tape->cache_stage = NULL;
1851 return cache_stage;
1852 }
1853 return __idetape_kmalloc_stage(tape, 0, 0);
1854}
1855
Daniel Walkerdcd96372006-06-25 05:47:37 -07001856static 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 -07001857{
1858 struct idetape_bh *bh = tape->bh;
1859 int count;
Daniel Walkerdcd96372006-06-25 05:47:37 -07001860 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001861
1862 while (n) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001863 if (bh == NULL) {
1864 printk(KERN_ERR "ide-tape: bh == NULL in "
1865 "idetape_copy_stage_from_user\n");
Daniel Walkerdcd96372006-06-25 05:47:37 -07001866 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001867 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001868 count = min((unsigned int)(bh->b_size - atomic_read(&bh->b_count)), (unsigned int)n);
Daniel Walkerdcd96372006-06-25 05:47:37 -07001869 if (copy_from_user(bh->b_data + atomic_read(&bh->b_count), buf, count))
1870 ret = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001871 n -= count;
1872 atomic_add(count, &bh->b_count);
1873 buf += count;
1874 if (atomic_read(&bh->b_count) == bh->b_size) {
1875 bh = bh->b_reqnext;
1876 if (bh)
1877 atomic_set(&bh->b_count, 0);
1878 }
1879 }
1880 tape->bh = bh;
Daniel Walkerdcd96372006-06-25 05:47:37 -07001881 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001882}
1883
Daniel Walkerdcd96372006-06-25 05:47:37 -07001884static 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 -07001885{
1886 struct idetape_bh *bh = tape->bh;
1887 int count;
Daniel Walkerdcd96372006-06-25 05:47:37 -07001888 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001889
1890 while (n) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001891 if (bh == NULL) {
1892 printk(KERN_ERR "ide-tape: bh == NULL in "
1893 "idetape_copy_stage_to_user\n");
Daniel Walkerdcd96372006-06-25 05:47:37 -07001894 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001895 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001896 count = min(tape->b_count, n);
Daniel Walkerdcd96372006-06-25 05:47:37 -07001897 if (copy_to_user(buf, tape->b_data, count))
1898 ret = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001899 n -= count;
1900 tape->b_data += count;
1901 tape->b_count -= count;
1902 buf += count;
1903 if (!tape->b_count) {
1904 tape->bh = bh = bh->b_reqnext;
1905 if (bh) {
1906 tape->b_data = bh->b_data;
1907 tape->b_count = atomic_read(&bh->b_count);
1908 }
1909 }
1910 }
Daniel Walkerdcd96372006-06-25 05:47:37 -07001911 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001912}
1913
1914static void idetape_init_merge_stage (idetape_tape_t *tape)
1915{
1916 struct idetape_bh *bh = tape->merge_stage->bh;
1917
1918 tape->bh = bh;
1919 if (tape->chrdev_direction == idetape_direction_write)
1920 atomic_set(&bh->b_count, 0);
1921 else {
1922 tape->b_data = bh->b_data;
1923 tape->b_count = atomic_read(&bh->b_count);
1924 }
1925}
1926
1927static void idetape_switch_buffers (idetape_tape_t *tape, idetape_stage_t *stage)
1928{
1929 struct idetape_bh *tmp;
1930
1931 tmp = stage->bh;
1932 stage->bh = tape->merge_stage->bh;
1933 tape->merge_stage->bh = tmp;
1934 idetape_init_merge_stage(tape);
1935}
1936
1937/*
1938 * idetape_add_stage_tail adds a new stage at the end of the pipeline.
1939 */
1940static void idetape_add_stage_tail (ide_drive_t *drive,idetape_stage_t *stage)
1941{
1942 idetape_tape_t *tape = drive->driver_data;
1943 unsigned long flags;
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001944
1945 debug_log(DBG_PROCS, "Enter %s\n", __func__);
1946
Linus Torvalds1da177e2005-04-16 15:20:36 -07001947 spin_lock_irqsave(&tape->spinlock, flags);
1948 stage->next = NULL;
1949 if (tape->last_stage != NULL)
1950 tape->last_stage->next=stage;
1951 else
1952 tape->first_stage = tape->next_stage=stage;
1953 tape->last_stage = stage;
1954 if (tape->next_stage == NULL)
1955 tape->next_stage = tape->last_stage;
1956 tape->nr_stages++;
1957 tape->nr_pending_stages++;
1958 spin_unlock_irqrestore(&tape->spinlock, flags);
1959}
1960
1961/*
1962 * idetape_wait_for_request installs a completion in a pending request
1963 * and sleeps until it is serviced.
1964 *
1965 * The caller should ensure that the request will not be serviced
1966 * before we install the completion (usually by disabling interrupts).
1967 */
1968static void idetape_wait_for_request (ide_drive_t *drive, struct request *rq)
1969{
Peter Zijlstra6e9a4732006-09-30 23:28:10 -07001970 DECLARE_COMPLETION_ONSTACK(wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001971 idetape_tape_t *tape = drive->driver_data;
1972
Jens Axboe4aff5e22006-08-10 08:44:47 +02001973 if (rq == NULL || !blk_special_request(rq)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001974 printk (KERN_ERR "ide-tape: bug: Trying to sleep on non-valid request\n");
1975 return;
1976 }
Jens Axboec00895a2006-09-30 20:29:12 +02001977 rq->end_io_data = &wait;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001978 rq->end_io = blk_end_sync_rq;
1979 spin_unlock_irq(&tape->spinlock);
1980 wait_for_completion(&wait);
1981 /* The stage and its struct request have been deallocated */
1982 spin_lock_irq(&tape->spinlock);
1983}
1984
Borislav Petkova2f5b7f2008-02-06 02:57:51 +01001985static ide_startstop_t idetape_read_position_callback(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001986{
1987 idetape_tape_t *tape = drive->driver_data;
Borislav Petkova2f5b7f2008-02-06 02:57:51 +01001988 u8 *readpos = tape->pc->buffer;
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001989
1990 debug_log(DBG_PROCS, "Enter %s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001991
1992 if (!tape->pc->error) {
Borislav Petkova2f5b7f2008-02-06 02:57:51 +01001993 debug_log(DBG_SENSE, "BOP - %s\n",
1994 (readpos[0] & 0x80) ? "Yes" : "No");
1995 debug_log(DBG_SENSE, "EOP - %s\n",
1996 (readpos[0] & 0x40) ? "Yes" : "No");
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001997
Borislav Petkova2f5b7f2008-02-06 02:57:51 +01001998 if (readpos[0] & 0x4) {
1999 printk(KERN_INFO "ide-tape: Block location is unknown"
2000 "to the tape\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002001 clear_bit(IDETAPE_ADDRESS_VALID, &tape->flags);
2002 idetape_end_request(drive, 0, 0);
2003 } else {
Borislav Petkov8004a8c2008-02-06 02:57:51 +01002004 debug_log(DBG_SENSE, "Block Location - %u\n",
Borislav Petkova2f5b7f2008-02-06 02:57:51 +01002005 be32_to_cpu(*(u32 *)&readpos[4]));
Borislav Petkov8004a8c2008-02-06 02:57:51 +01002006
Borislav Petkova2f5b7f2008-02-06 02:57:51 +01002007 tape->partition = readpos[1];
2008 tape->first_frame_position =
2009 be32_to_cpu(*(u32 *)&readpos[4]);
2010 tape->last_frame_position =
2011 be32_to_cpu(*(u32 *)&readpos[8]);
2012 tape->blocks_in_buffer = readpos[15];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002013 set_bit(IDETAPE_ADDRESS_VALID, &tape->flags);
2014 idetape_end_request(drive, 1, 0);
2015 }
2016 } else {
2017 idetape_end_request(drive, 0, 0);
2018 }
2019 return ide_stopped;
2020}
2021
2022/*
2023 * idetape_create_write_filemark_cmd will:
2024 *
2025 * 1. Write a filemark if write_filemark=1.
2026 * 2. Flush the device buffers without writing a filemark
2027 * if write_filemark=0.
2028 *
2029 */
2030static void idetape_create_write_filemark_cmd (ide_drive_t *drive, idetape_pc_t *pc,int write_filemark)
2031{
2032 idetape_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +01002033 pc->c[0] = WRITE_FILEMARKS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002034 pc->c[4] = write_filemark;
2035 set_bit(PC_WAIT_FOR_DSC, &pc->flags);
2036 pc->callback = &idetape_pc_callback;
2037}
2038
2039static void idetape_create_test_unit_ready_cmd(idetape_pc_t *pc)
2040{
2041 idetape_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +01002042 pc->c[0] = TEST_UNIT_READY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002043 pc->callback = &idetape_pc_callback;
2044}
2045
2046/*
2047 * idetape_queue_pc_tail is based on the following functions:
2048 *
2049 * ide_do_drive_cmd from ide.c
2050 * cdrom_queue_request and cdrom_queue_packet_command from ide-cd.c
2051 *
2052 * We add a special packet command request to the tail of the request
2053 * queue, and wait for it to be serviced.
2054 *
2055 * This is not to be called from within the request handling part
2056 * of the driver ! We allocate here data in the stack, and it is valid
2057 * until the request is finished. This is not the case for the bottom
2058 * part of the driver, where we are always leaving the functions to wait
2059 * for an interrupt or a timer event.
2060 *
2061 * From the bottom part of the driver, we should allocate safe memory
2062 * using idetape_next_pc_storage and idetape_next_rq_storage, and add
2063 * the request to the request list without waiting for it to be serviced !
2064 * In that case, we usually use idetape_queue_pc_head.
2065 */
2066static int __idetape_queue_pc_tail (ide_drive_t *drive, idetape_pc_t *pc)
2067{
2068 struct ide_tape_obj *tape = drive->driver_data;
2069 struct request rq;
2070
2071 idetape_init_rq(&rq, REQ_IDETAPE_PC1);
2072 rq.buffer = (char *) pc;
2073 rq.rq_disk = tape->disk;
2074 return ide_do_drive_cmd(drive, &rq, ide_wait);
2075}
2076
2077static void idetape_create_load_unload_cmd (ide_drive_t *drive, idetape_pc_t *pc,int cmd)
2078{
2079 idetape_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +01002080 pc->c[0] = START_STOP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002081 pc->c[4] = cmd;
2082 set_bit(PC_WAIT_FOR_DSC, &pc->flags);
2083 pc->callback = &idetape_pc_callback;
2084}
2085
2086static int idetape_wait_ready(ide_drive_t *drive, unsigned long timeout)
2087{
2088 idetape_tape_t *tape = drive->driver_data;
2089 idetape_pc_t pc;
2090 int load_attempted = 0;
2091
2092 /*
2093 * Wait for the tape to become ready
2094 */
2095 set_bit(IDETAPE_MEDIUM_PRESENT, &tape->flags);
2096 timeout += jiffies;
2097 while (time_before(jiffies, timeout)) {
2098 idetape_create_test_unit_ready_cmd(&pc);
2099 if (!__idetape_queue_pc_tail(drive, &pc))
2100 return 0;
2101 if ((tape->sense_key == 2 && tape->asc == 4 && tape->ascq == 2)
2102 || (tape->asc == 0x3A)) { /* no media */
2103 if (load_attempted)
2104 return -ENOMEDIUM;
2105 idetape_create_load_unload_cmd(drive, &pc, IDETAPE_LU_LOAD_MASK);
2106 __idetape_queue_pc_tail(drive, &pc);
2107 load_attempted = 1;
2108 /* not about to be ready */
2109 } else if (!(tape->sense_key == 2 && tape->asc == 4 &&
2110 (tape->ascq == 1 || tape->ascq == 8)))
2111 return -EIO;
Nishanth Aravamudan80ce45f2005-09-10 00:27:08 -07002112 msleep(100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002113 }
2114 return -EIO;
2115}
2116
2117static int idetape_queue_pc_tail (ide_drive_t *drive,idetape_pc_t *pc)
2118{
2119 return __idetape_queue_pc_tail(drive, pc);
2120}
2121
2122static int idetape_flush_tape_buffers (ide_drive_t *drive)
2123{
2124 idetape_pc_t pc;
2125 int rc;
2126
2127 idetape_create_write_filemark_cmd(drive, &pc, 0);
2128 if ((rc = idetape_queue_pc_tail(drive, &pc)))
2129 return rc;
2130 idetape_wait_ready(drive, 60 * 5 * HZ);
2131 return 0;
2132}
2133
2134static void idetape_create_read_position_cmd (idetape_pc_t *pc)
2135{
2136 idetape_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +01002137 pc->c[0] = READ_POSITION;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002138 pc->request_transfer = 20;
2139 pc->callback = &idetape_read_position_callback;
2140}
2141
2142static int idetape_read_position (ide_drive_t *drive)
2143{
2144 idetape_tape_t *tape = drive->driver_data;
2145 idetape_pc_t pc;
2146 int position;
2147
Borislav Petkov8004a8c2008-02-06 02:57:51 +01002148 debug_log(DBG_PROCS, "Enter %s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002149
2150 idetape_create_read_position_cmd(&pc);
2151 if (idetape_queue_pc_tail(drive, &pc))
2152 return -1;
2153 position = tape->first_frame_position;
2154 return position;
2155}
2156
2157static void idetape_create_locate_cmd (ide_drive_t *drive, idetape_pc_t *pc, unsigned int block, u8 partition, int skip)
2158{
2159 idetape_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +01002160 pc->c[0] = POSITION_TO_ELEMENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002161 pc->c[1] = 2;
Borislav Petkov860ff5e2008-02-02 19:56:50 +01002162 put_unaligned(cpu_to_be32(block), (unsigned int *) &pc->c[3]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002163 pc->c[8] = partition;
2164 set_bit(PC_WAIT_FOR_DSC, &pc->flags);
2165 pc->callback = &idetape_pc_callback;
2166}
2167
2168static int idetape_create_prevent_cmd (ide_drive_t *drive, idetape_pc_t *pc, int prevent)
2169{
2170 idetape_tape_t *tape = drive->driver_data;
2171
Borislav Petkovb6422012008-02-02 19:56:49 +01002172 /* device supports locking according to capabilities page */
2173 if (!(tape->caps[6] & 0x01))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002174 return 0;
2175
2176 idetape_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +01002177 pc->c[0] = ALLOW_MEDIUM_REMOVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002178 pc->c[4] = prevent;
2179 pc->callback = &idetape_pc_callback;
2180 return 1;
2181}
2182
2183static int __idetape_discard_read_pipeline (ide_drive_t *drive)
2184{
2185 idetape_tape_t *tape = drive->driver_data;
2186 unsigned long flags;
2187 int cnt;
2188
2189 if (tape->chrdev_direction != idetape_direction_read)
2190 return 0;
2191
2192 /* Remove merge stage. */
2193 cnt = tape->merge_stage_size / tape->tape_block_size;
2194 if (test_and_clear_bit(IDETAPE_FILEMARK, &tape->flags))
2195 ++cnt; /* Filemarks count as 1 sector */
2196 tape->merge_stage_size = 0;
2197 if (tape->merge_stage != NULL) {
2198 __idetape_kfree_stage(tape->merge_stage);
2199 tape->merge_stage = NULL;
2200 }
2201
2202 /* Clear pipeline flags. */
2203 clear_bit(IDETAPE_PIPELINE_ERROR, &tape->flags);
2204 tape->chrdev_direction = idetape_direction_none;
2205
2206 /* Remove pipeline stages. */
2207 if (tape->first_stage == NULL)
2208 return 0;
2209
2210 spin_lock_irqsave(&tape->spinlock, flags);
2211 tape->next_stage = NULL;
2212 if (idetape_pipeline_active(tape))
2213 idetape_wait_for_request(drive, tape->active_data_request);
2214 spin_unlock_irqrestore(&tape->spinlock, flags);
2215
2216 while (tape->first_stage != NULL) {
2217 struct request *rq_ptr = &tape->first_stage->rq;
2218
2219 cnt += rq_ptr->nr_sectors - rq_ptr->current_nr_sectors;
2220 if (rq_ptr->errors == IDETAPE_ERROR_FILEMARK)
2221 ++cnt;
2222 idetape_remove_stage_head(drive);
2223 }
2224 tape->nr_pending_stages = 0;
2225 tape->max_stages = tape->min_pipeline;
2226 return cnt;
2227}
2228
2229/*
2230 * idetape_position_tape positions the tape to the requested block
2231 * using the LOCATE packet command. A READ POSITION command is then
2232 * issued to check where we are positioned.
2233 *
2234 * Like all higher level operations, we queue the commands at the tail
2235 * of the request queue and wait for their completion.
2236 *
2237 */
2238static int idetape_position_tape (ide_drive_t *drive, unsigned int block, u8 partition, int skip)
2239{
2240 idetape_tape_t *tape = drive->driver_data;
2241 int retval;
2242 idetape_pc_t pc;
2243
2244 if (tape->chrdev_direction == idetape_direction_read)
2245 __idetape_discard_read_pipeline(drive);
2246 idetape_wait_ready(drive, 60 * 5 * HZ);
2247 idetape_create_locate_cmd(drive, &pc, block, partition, skip);
2248 retval = idetape_queue_pc_tail(drive, &pc);
2249 if (retval)
2250 return (retval);
2251
2252 idetape_create_read_position_cmd(&pc);
2253 return (idetape_queue_pc_tail(drive, &pc));
2254}
2255
2256static void idetape_discard_read_pipeline (ide_drive_t *drive, int restore_position)
2257{
2258 idetape_tape_t *tape = drive->driver_data;
2259 int cnt;
2260 int seek, position;
2261
2262 cnt = __idetape_discard_read_pipeline(drive);
2263 if (restore_position) {
2264 position = idetape_read_position(drive);
2265 seek = position > cnt ? position - cnt : 0;
2266 if (idetape_position_tape(drive, seek, 0, 0)) {
2267 printk(KERN_INFO "ide-tape: %s: position_tape failed in discard_pipeline()\n", tape->name);
2268 return;
2269 }
2270 }
2271}
2272
2273/*
2274 * idetape_queue_rw_tail generates a read/write request for the block
2275 * device interface and wait for it to be serviced.
2276 */
2277static int idetape_queue_rw_tail(ide_drive_t *drive, int cmd, int blocks, struct idetape_bh *bh)
2278{
2279 idetape_tape_t *tape = drive->driver_data;
2280 struct request rq;
2281
Borislav Petkov8004a8c2008-02-06 02:57:51 +01002282 debug_log(DBG_SENSE, "%s: cmd=%d\n", __func__, cmd);
2283
Linus Torvalds1da177e2005-04-16 15:20:36 -07002284 if (idetape_pipeline_active(tape)) {
Borislav Petkov8004a8c2008-02-06 02:57:51 +01002285 printk(KERN_ERR "ide-tape: bug: the pipeline is active in %s\n",
2286 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002287 return (0);
2288 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002289
2290 idetape_init_rq(&rq, cmd);
2291 rq.rq_disk = tape->disk;
2292 rq.special = (void *)bh;
2293 rq.sector = tape->first_frame_position;
2294 rq.nr_sectors = rq.current_nr_sectors = blocks;
2295 (void) ide_do_drive_cmd(drive, &rq, ide_wait);
2296
2297 if ((cmd & (REQ_IDETAPE_READ | REQ_IDETAPE_WRITE)) == 0)
2298 return 0;
2299
2300 if (tape->merge_stage)
2301 idetape_init_merge_stage(tape);
2302 if (rq.errors == IDETAPE_ERROR_GENERAL)
2303 return -EIO;
2304 return (tape->tape_block_size * (blocks-rq.current_nr_sectors));
2305}
2306
2307/*
2308 * idetape_insert_pipeline_into_queue is used to start servicing the
2309 * pipeline stages, starting from tape->next_stage.
2310 */
2311static void idetape_insert_pipeline_into_queue (ide_drive_t *drive)
2312{
2313 idetape_tape_t *tape = drive->driver_data;
2314
2315 if (tape->next_stage == NULL)
2316 return;
2317 if (!idetape_pipeline_active(tape)) {
2318 set_bit(IDETAPE_PIPELINE_ACTIVE, &tape->flags);
Borislav Petkov419d4742008-02-02 19:56:51 +01002319 idetape_activate_next_stage(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002320 (void) ide_do_drive_cmd(drive, tape->active_data_request, ide_end);
2321 }
2322}
2323
2324static void idetape_create_inquiry_cmd (idetape_pc_t *pc)
2325{
2326 idetape_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +01002327 pc->c[0] = INQUIRY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002328 pc->c[4] = pc->request_transfer = 254;
2329 pc->callback = &idetape_pc_callback;
2330}
2331
2332static void idetape_create_rewind_cmd (ide_drive_t *drive, idetape_pc_t *pc)
2333{
2334 idetape_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +01002335 pc->c[0] = REZERO_UNIT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002336 set_bit(PC_WAIT_FOR_DSC, &pc->flags);
2337 pc->callback = &idetape_pc_callback;
2338}
2339
Linus Torvalds1da177e2005-04-16 15:20:36 -07002340static void idetape_create_erase_cmd (idetape_pc_t *pc)
2341{
2342 idetape_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +01002343 pc->c[0] = ERASE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002344 pc->c[1] = 1;
2345 set_bit(PC_WAIT_FOR_DSC, &pc->flags);
2346 pc->callback = &idetape_pc_callback;
2347}
2348
2349static void idetape_create_space_cmd (idetape_pc_t *pc,int count, u8 cmd)
2350{
2351 idetape_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +01002352 pc->c[0] = SPACE;
Borislav Petkov860ff5e2008-02-02 19:56:50 +01002353 put_unaligned(cpu_to_be32(count), (unsigned int *) &pc->c[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002354 pc->c[1] = cmd;
2355 set_bit(PC_WAIT_FOR_DSC, &pc->flags);
2356 pc->callback = &idetape_pc_callback;
2357}
2358
2359static void idetape_wait_first_stage (ide_drive_t *drive)
2360{
2361 idetape_tape_t *tape = drive->driver_data;
2362 unsigned long flags;
2363
2364 if (tape->first_stage == NULL)
2365 return;
2366 spin_lock_irqsave(&tape->spinlock, flags);
2367 if (tape->active_stage == tape->first_stage)
2368 idetape_wait_for_request(drive, tape->active_data_request);
2369 spin_unlock_irqrestore(&tape->spinlock, flags);
2370}
2371
2372/*
2373 * idetape_add_chrdev_write_request tries to add a character device
2374 * originated write request to our pipeline. In case we don't succeed,
2375 * we revert to non-pipelined operation mode for this request.
2376 *
2377 * 1. Try to allocate a new pipeline stage.
2378 * 2. If we can't, wait for more and more requests to be serviced
2379 * and try again each time.
2380 * 3. If we still can't allocate a stage, fallback to
2381 * non-pipelined operation mode for this request.
2382 */
2383static int idetape_add_chrdev_write_request (ide_drive_t *drive, int blocks)
2384{
2385 idetape_tape_t *tape = drive->driver_data;
2386 idetape_stage_t *new_stage;
2387 unsigned long flags;
2388 struct request *rq;
2389
Borislav Petkov8004a8c2008-02-06 02:57:51 +01002390 debug_log(DBG_CHRDEV, "Enter %s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002391
2392 /*
2393 * Attempt to allocate a new stage.
2394 * Pay special attention to possible race conditions.
2395 */
2396 while ((new_stage = idetape_kmalloc_stage(tape)) == NULL) {
2397 spin_lock_irqsave(&tape->spinlock, flags);
2398 if (idetape_pipeline_active(tape)) {
2399 idetape_wait_for_request(drive, tape->active_data_request);
2400 spin_unlock_irqrestore(&tape->spinlock, flags);
2401 } else {
2402 spin_unlock_irqrestore(&tape->spinlock, flags);
2403 idetape_insert_pipeline_into_queue(drive);
2404 if (idetape_pipeline_active(tape))
2405 continue;
2406 /*
2407 * Linux is short on memory. Fallback to
2408 * non-pipelined operation mode for this request.
2409 */
2410 return idetape_queue_rw_tail(drive, REQ_IDETAPE_WRITE, blocks, tape->merge_stage->bh);
2411 }
2412 }
2413 rq = &new_stage->rq;
2414 idetape_init_rq(rq, REQ_IDETAPE_WRITE);
2415 /* Doesn't actually matter - We always assume sequential access */
2416 rq->sector = tape->first_frame_position;
2417 rq->nr_sectors = rq->current_nr_sectors = blocks;
2418
2419 idetape_switch_buffers(tape, new_stage);
2420 idetape_add_stage_tail(drive, new_stage);
2421 tape->pipeline_head++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002422 calculate_speeds(drive);
2423
2424 /*
2425 * Estimate whether the tape has stopped writing by checking
2426 * if our write pipeline is currently empty. If we are not
2427 * writing anymore, wait for the pipeline to be full enough
2428 * (90%) before starting to service requests, so that we will
2429 * be able to keep up with the higher speeds of the tape.
2430 */
2431 if (!idetape_pipeline_active(tape)) {
2432 if (tape->nr_stages >= tape->max_stages * 9 / 10 ||
2433 tape->nr_stages >= tape->max_stages - tape->uncontrolled_pipeline_head_speed * 3 * 1024 / tape->tape_block_size) {
2434 tape->measure_insert_time = 1;
2435 tape->insert_time = jiffies;
2436 tape->insert_size = 0;
2437 tape->insert_speed = 0;
2438 idetape_insert_pipeline_into_queue(drive);
2439 }
2440 }
2441 if (test_and_clear_bit(IDETAPE_PIPELINE_ERROR, &tape->flags))
2442 /* Return a deferred error */
2443 return -EIO;
2444 return blocks;
2445}
2446
2447/*
2448 * idetape_wait_for_pipeline will wait until all pending pipeline
2449 * requests are serviced. Typically called on device close.
2450 */
2451static void idetape_wait_for_pipeline (ide_drive_t *drive)
2452{
2453 idetape_tape_t *tape = drive->driver_data;
2454 unsigned long flags;
2455
2456 while (tape->next_stage || idetape_pipeline_active(tape)) {
2457 idetape_insert_pipeline_into_queue(drive);
2458 spin_lock_irqsave(&tape->spinlock, flags);
2459 if (idetape_pipeline_active(tape))
2460 idetape_wait_for_request(drive, tape->active_data_request);
2461 spin_unlock_irqrestore(&tape->spinlock, flags);
2462 }
2463}
2464
2465static void idetape_empty_write_pipeline (ide_drive_t *drive)
2466{
2467 idetape_tape_t *tape = drive->driver_data;
2468 int blocks, min;
2469 struct idetape_bh *bh;
Borislav Petkov55a5d292008-02-02 19:56:49 +01002470
Linus Torvalds1da177e2005-04-16 15:20:36 -07002471 if (tape->chrdev_direction != idetape_direction_write) {
2472 printk(KERN_ERR "ide-tape: bug: Trying to empty write pipeline, but we are not writing.\n");
2473 return;
2474 }
2475 if (tape->merge_stage_size > tape->stage_size) {
2476 printk(KERN_ERR "ide-tape: bug: merge_buffer too big\n");
2477 tape->merge_stage_size = tape->stage_size;
2478 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002479 if (tape->merge_stage_size) {
2480 blocks = tape->merge_stage_size / tape->tape_block_size;
2481 if (tape->merge_stage_size % tape->tape_block_size) {
2482 unsigned int i;
2483
2484 blocks++;
2485 i = tape->tape_block_size - tape->merge_stage_size % tape->tape_block_size;
2486 bh = tape->bh->b_reqnext;
2487 while (bh) {
2488 atomic_set(&bh->b_count, 0);
2489 bh = bh->b_reqnext;
2490 }
2491 bh = tape->bh;
2492 while (i) {
2493 if (bh == NULL) {
2494
2495 printk(KERN_INFO "ide-tape: bug, bh NULL\n");
2496 break;
2497 }
2498 min = min(i, (unsigned int)(bh->b_size - atomic_read(&bh->b_count)));
2499 memset(bh->b_data + atomic_read(&bh->b_count), 0, min);
2500 atomic_add(min, &bh->b_count);
2501 i -= min;
2502 bh = bh->b_reqnext;
2503 }
2504 }
2505 (void) idetape_add_chrdev_write_request(drive, blocks);
2506 tape->merge_stage_size = 0;
2507 }
2508 idetape_wait_for_pipeline(drive);
2509 if (tape->merge_stage != NULL) {
2510 __idetape_kfree_stage(tape->merge_stage);
2511 tape->merge_stage = NULL;
2512 }
2513 clear_bit(IDETAPE_PIPELINE_ERROR, &tape->flags);
2514 tape->chrdev_direction = idetape_direction_none;
2515
2516 /*
2517 * On the next backup, perform the feedback loop again.
2518 * (I don't want to keep sense information between backups,
2519 * as some systems are constantly on, and the system load
2520 * can be totally different on the next backup).
2521 */
2522 tape->max_stages = tape->min_pipeline;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002523 if (tape->first_stage != NULL ||
2524 tape->next_stage != NULL ||
2525 tape->last_stage != NULL ||
2526 tape->nr_stages != 0) {
2527 printk(KERN_ERR "ide-tape: ide-tape pipeline bug, "
2528 "first_stage %p, next_stage %p, "
2529 "last_stage %p, nr_stages %d\n",
2530 tape->first_stage, tape->next_stage,
2531 tape->last_stage, tape->nr_stages);
2532 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002533}
2534
2535static void idetape_restart_speed_control (ide_drive_t *drive)
2536{
2537 idetape_tape_t *tape = drive->driver_data;
2538
2539 tape->restart_speed_control_req = 0;
2540 tape->pipeline_head = 0;
2541 tape->controlled_last_pipeline_head = tape->uncontrolled_last_pipeline_head = 0;
2542 tape->controlled_previous_pipeline_head = tape->uncontrolled_previous_pipeline_head = 0;
2543 tape->pipeline_head_speed = tape->controlled_pipeline_head_speed = 5000;
2544 tape->uncontrolled_pipeline_head_speed = 0;
2545 tape->controlled_pipeline_head_time = tape->uncontrolled_pipeline_head_time = jiffies;
2546 tape->controlled_previous_head_time = tape->uncontrolled_previous_head_time = jiffies;
2547}
2548
2549static int idetape_initiate_read (ide_drive_t *drive, int max_stages)
2550{
2551 idetape_tape_t *tape = drive->driver_data;
2552 idetape_stage_t *new_stage;
2553 struct request rq;
2554 int bytes_read;
Borislav Petkovb6422012008-02-02 19:56:49 +01002555 u16 blocks = *(u16 *)&tape->caps[12];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002556
2557 /* Initialize read operation */
2558 if (tape->chrdev_direction != idetape_direction_read) {
2559 if (tape->chrdev_direction == idetape_direction_write) {
2560 idetape_empty_write_pipeline(drive);
2561 idetape_flush_tape_buffers(drive);
2562 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002563 if (tape->merge_stage || tape->merge_stage_size) {
2564 printk (KERN_ERR "ide-tape: merge_stage_size should be 0 now\n");
2565 tape->merge_stage_size = 0;
2566 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002567 if ((tape->merge_stage = __idetape_kmalloc_stage(tape, 0, 0)) == NULL)
2568 return -ENOMEM;
2569 tape->chrdev_direction = idetape_direction_read;
2570
2571 /*
2572 * Issue a read 0 command to ensure that DSC handshake
2573 * is switched from completion mode to buffer available
2574 * mode.
2575 * No point in issuing this if DSC overlap isn't supported,
2576 * some drives (Seagate STT3401A) will return an error.
2577 */
2578 if (drive->dsc_overlap) {
2579 bytes_read = idetape_queue_rw_tail(drive, REQ_IDETAPE_READ, 0, tape->merge_stage->bh);
2580 if (bytes_read < 0) {
2581 __idetape_kfree_stage(tape->merge_stage);
2582 tape->merge_stage = NULL;
2583 tape->chrdev_direction = idetape_direction_none;
2584 return bytes_read;
2585 }
2586 }
2587 }
2588 if (tape->restart_speed_control_req)
2589 idetape_restart_speed_control(drive);
2590 idetape_init_rq(&rq, REQ_IDETAPE_READ);
2591 rq.sector = tape->first_frame_position;
2592 rq.nr_sectors = rq.current_nr_sectors = blocks;
2593 if (!test_bit(IDETAPE_PIPELINE_ERROR, &tape->flags) &&
2594 tape->nr_stages < max_stages) {
2595 new_stage = idetape_kmalloc_stage(tape);
2596 while (new_stage != NULL) {
2597 new_stage->rq = rq;
2598 idetape_add_stage_tail(drive, new_stage);
2599 if (tape->nr_stages >= max_stages)
2600 break;
2601 new_stage = idetape_kmalloc_stage(tape);
2602 }
2603 }
2604 if (!idetape_pipeline_active(tape)) {
2605 if (tape->nr_pending_stages >= 3 * max_stages / 4) {
2606 tape->measure_insert_time = 1;
2607 tape->insert_time = jiffies;
2608 tape->insert_size = 0;
2609 tape->insert_speed = 0;
2610 idetape_insert_pipeline_into_queue(drive);
2611 }
2612 }
2613 return 0;
2614}
2615
2616/*
2617 * idetape_add_chrdev_read_request is called from idetape_chrdev_read
2618 * to service a character device read request and add read-ahead
2619 * requests to our pipeline.
2620 */
2621static int idetape_add_chrdev_read_request (ide_drive_t *drive,int blocks)
2622{
2623 idetape_tape_t *tape = drive->driver_data;
2624 unsigned long flags;
2625 struct request *rq_ptr;
2626 int bytes_read;
2627
Borislav Petkov8004a8c2008-02-06 02:57:51 +01002628 debug_log(DBG_PROCS, "Enter %s, %d blocks\n", __func__, blocks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002629
2630 /*
2631 * If we are at a filemark, return a read length of 0
2632 */
2633 if (test_bit(IDETAPE_FILEMARK, &tape->flags))
2634 return 0;
2635
2636 /*
2637 * Wait for the next block to be available at the head
2638 * of the pipeline
2639 */
2640 idetape_initiate_read(drive, tape->max_stages);
2641 if (tape->first_stage == NULL) {
2642 if (test_bit(IDETAPE_PIPELINE_ERROR, &tape->flags))
2643 return 0;
2644 return idetape_queue_rw_tail(drive, REQ_IDETAPE_READ, blocks, tape->merge_stage->bh);
2645 }
2646 idetape_wait_first_stage(drive);
2647 rq_ptr = &tape->first_stage->rq;
2648 bytes_read = tape->tape_block_size * (rq_ptr->nr_sectors - rq_ptr->current_nr_sectors);
2649 rq_ptr->nr_sectors = rq_ptr->current_nr_sectors = 0;
2650
2651
2652 if (rq_ptr->errors == IDETAPE_ERROR_EOD)
2653 return 0;
2654 else {
2655 idetape_switch_buffers(tape, tape->first_stage);
2656 if (rq_ptr->errors == IDETAPE_ERROR_FILEMARK)
2657 set_bit(IDETAPE_FILEMARK, &tape->flags);
2658 spin_lock_irqsave(&tape->spinlock, flags);
2659 idetape_remove_stage_head(drive);
2660 spin_unlock_irqrestore(&tape->spinlock, flags);
2661 tape->pipeline_head++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002662 calculate_speeds(drive);
2663 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002664 if (bytes_read > blocks * tape->tape_block_size) {
2665 printk(KERN_ERR "ide-tape: bug: trying to return more bytes than requested\n");
2666 bytes_read = blocks * tape->tape_block_size;
2667 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002668 return (bytes_read);
2669}
2670
2671static void idetape_pad_zeros (ide_drive_t *drive, int bcount)
2672{
2673 idetape_tape_t *tape = drive->driver_data;
2674 struct idetape_bh *bh;
2675 int blocks;
2676
2677 while (bcount) {
2678 unsigned int count;
2679
2680 bh = tape->merge_stage->bh;
2681 count = min(tape->stage_size, bcount);
2682 bcount -= count;
2683 blocks = count / tape->tape_block_size;
2684 while (count) {
2685 atomic_set(&bh->b_count, min(count, (unsigned int)bh->b_size));
2686 memset(bh->b_data, 0, atomic_read(&bh->b_count));
2687 count -= atomic_read(&bh->b_count);
2688 bh = bh->b_reqnext;
2689 }
2690 idetape_queue_rw_tail(drive, REQ_IDETAPE_WRITE, blocks, tape->merge_stage->bh);
2691 }
2692}
2693
2694static int idetape_pipeline_size (ide_drive_t *drive)
2695{
2696 idetape_tape_t *tape = drive->driver_data;
2697 idetape_stage_t *stage;
2698 struct request *rq;
2699 int size = 0;
2700
2701 idetape_wait_for_pipeline(drive);
2702 stage = tape->first_stage;
2703 while (stage != NULL) {
2704 rq = &stage->rq;
2705 size += tape->tape_block_size * (rq->nr_sectors-rq->current_nr_sectors);
2706 if (rq->errors == IDETAPE_ERROR_FILEMARK)
2707 size += tape->tape_block_size;
2708 stage = stage->next;
2709 }
2710 size += tape->merge_stage_size;
2711 return size;
2712}
2713
2714/*
2715 * Rewinds the tape to the Beginning Of the current Partition (BOP).
2716 *
2717 * We currently support only one partition.
2718 */
2719static int idetape_rewind_tape (ide_drive_t *drive)
2720{
2721 int retval;
2722 idetape_pc_t pc;
Borislav Petkov8004a8c2008-02-06 02:57:51 +01002723 idetape_tape_t *tape;
2724 tape = drive->driver_data;
2725
2726 debug_log(DBG_SENSE, "Enter %s\n", __func__);
2727
Linus Torvalds1da177e2005-04-16 15:20:36 -07002728 idetape_create_rewind_cmd(drive, &pc);
2729 retval = idetape_queue_pc_tail(drive, &pc);
2730 if (retval)
2731 return retval;
2732
2733 idetape_create_read_position_cmd(&pc);
2734 retval = idetape_queue_pc_tail(drive, &pc);
2735 if (retval)
2736 return retval;
2737 return 0;
2738}
2739
2740/*
2741 * Our special ide-tape ioctl's.
2742 *
2743 * Currently there aren't any ioctl's.
2744 * mtio.h compatible commands should be issued to the character device
2745 * interface.
2746 */
2747static int idetape_blkdev_ioctl(ide_drive_t *drive, unsigned int cmd, unsigned long arg)
2748{
2749 idetape_tape_t *tape = drive->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002750 void __user *argp = (void __user *)arg;
2751
Borislav Petkovd59823f2008-02-02 19:56:51 +01002752 struct idetape_config {
2753 int dsc_rw_frequency;
2754 int dsc_media_access_frequency;
2755 int nr_stages;
2756 } config;
2757
Borislav Petkov8004a8c2008-02-06 02:57:51 +01002758 debug_log(DBG_PROCS, "Enter %s\n", __func__);
2759
Linus Torvalds1da177e2005-04-16 15:20:36 -07002760 switch (cmd) {
2761 case 0x0340:
Borislav Petkovd59823f2008-02-02 19:56:51 +01002762 if (copy_from_user(&config, argp, sizeof(config)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002763 return -EFAULT;
2764 tape->best_dsc_rw_frequency = config.dsc_rw_frequency;
2765 tape->max_stages = config.nr_stages;
2766 break;
2767 case 0x0350:
2768 config.dsc_rw_frequency = (int) tape->best_dsc_rw_frequency;
2769 config.nr_stages = tape->max_stages;
Borislav Petkovd59823f2008-02-02 19:56:51 +01002770 if (copy_to_user(argp, &config, sizeof(config)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002771 return -EFAULT;
2772 break;
2773 default:
2774 return -EIO;
2775 }
2776 return 0;
2777}
2778
2779/*
2780 * idetape_space_over_filemarks is now a bit more complicated than just
2781 * passing the command to the tape since we may have crossed some
2782 * filemarks during our pipelined read-ahead mode.
2783 *
2784 * As a minor side effect, the pipeline enables us to support MTFSFM when
2785 * the filemark is in our internal pipeline even if the tape doesn't
2786 * support spacing over filemarks in the reverse direction.
2787 */
2788static int idetape_space_over_filemarks (ide_drive_t *drive,short mt_op,int mt_count)
2789{
2790 idetape_tape_t *tape = drive->driver_data;
2791 idetape_pc_t pc;
2792 unsigned long flags;
2793 int retval,count=0;
Borislav Petkovb6422012008-02-02 19:56:49 +01002794 int sprev = !!(tape->caps[4] & 0x20);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002795
2796 if (mt_count == 0)
2797 return 0;
2798 if (MTBSF == mt_op || MTBSFM == mt_op) {
Borislav Petkovb6422012008-02-02 19:56:49 +01002799 if (!sprev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002800 return -EIO;
2801 mt_count = - mt_count;
2802 }
2803
2804 if (tape->chrdev_direction == idetape_direction_read) {
2805 /*
2806 * We have a read-ahead buffer. Scan it for crossed
2807 * filemarks.
2808 */
2809 tape->merge_stage_size = 0;
2810 if (test_and_clear_bit(IDETAPE_FILEMARK, &tape->flags))
2811 ++count;
2812 while (tape->first_stage != NULL) {
2813 if (count == mt_count) {
2814 if (mt_op == MTFSFM)
2815 set_bit(IDETAPE_FILEMARK, &tape->flags);
2816 return 0;
2817 }
2818 spin_lock_irqsave(&tape->spinlock, flags);
2819 if (tape->first_stage == tape->active_stage) {
2820 /*
2821 * We have reached the active stage in the read pipeline.
2822 * There is no point in allowing the drive to continue
2823 * reading any farther, so we stop the pipeline.
2824 *
2825 * This section should be moved to a separate subroutine,
2826 * because a similar function is performed in
2827 * __idetape_discard_read_pipeline(), for example.
2828 */
2829 tape->next_stage = NULL;
2830 spin_unlock_irqrestore(&tape->spinlock, flags);
2831 idetape_wait_first_stage(drive);
2832 tape->next_stage = tape->first_stage->next;
2833 } else
2834 spin_unlock_irqrestore(&tape->spinlock, flags);
2835 if (tape->first_stage->rq.errors == IDETAPE_ERROR_FILEMARK)
2836 ++count;
2837 idetape_remove_stage_head(drive);
2838 }
2839 idetape_discard_read_pipeline(drive, 0);
2840 }
2841
2842 /*
2843 * The filemark was not found in our internal pipeline.
2844 * Now we can issue the space command.
2845 */
2846 switch (mt_op) {
2847 case MTFSF:
2848 case MTBSF:
2849 idetape_create_space_cmd(&pc,mt_count-count,IDETAPE_SPACE_OVER_FILEMARK);
2850 return (idetape_queue_pc_tail(drive, &pc));
2851 case MTFSFM:
2852 case MTBSFM:
Borislav Petkovb6422012008-02-02 19:56:49 +01002853 if (!sprev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002854 return (-EIO);
2855 retval = idetape_space_over_filemarks(drive, MTFSF, mt_count-count);
2856 if (retval) return (retval);
2857 count = (MTBSFM == mt_op ? 1 : -1);
2858 return (idetape_space_over_filemarks(drive, MTFSF, count));
2859 default:
2860 printk(KERN_ERR "ide-tape: MTIO operation %d not supported\n",mt_op);
2861 return (-EIO);
2862 }
2863}
2864
2865
2866/*
2867 * Our character device read / write functions.
2868 *
2869 * The tape is optimized to maximize throughput when it is transferring
2870 * an integral number of the "continuous transfer limit", which is
2871 * a parameter of the specific tape (26 KB on my particular tape).
2872 * (32 kB for Onstream)
2873 *
2874 * As of version 1.3 of the driver, the character device provides an
2875 * abstract continuous view of the media - any mix of block sizes (even 1
2876 * byte) on the same backup/restore procedure is supported. The driver
2877 * will internally convert the requests to the recommended transfer unit,
2878 * so that an unmatch between the user's block size to the recommended
2879 * size will only result in a (slightly) increased driver overhead, but
2880 * will no longer hit performance.
2881 * This is not applicable to Onstream.
2882 */
2883static ssize_t idetape_chrdev_read (struct file *file, char __user *buf,
2884 size_t count, loff_t *ppos)
2885{
2886 struct ide_tape_obj *tape = ide_tape_f(file);
2887 ide_drive_t *drive = tape->drive;
2888 ssize_t bytes_read,temp, actually_read = 0, rc;
Daniel Walkerdcd96372006-06-25 05:47:37 -07002889 ssize_t ret = 0;
Borislav Petkovb6422012008-02-02 19:56:49 +01002890 u16 ctl = *(u16 *)&tape->caps[12];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002891
Borislav Petkov8004a8c2008-02-06 02:57:51 +01002892 debug_log(DBG_CHRDEV, "Enter %s, count %Zd\n", __func__, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002893
2894 if (tape->chrdev_direction != idetape_direction_read) {
2895 if (test_bit(IDETAPE_DETECT_BS, &tape->flags))
2896 if (count > tape->tape_block_size &&
2897 (count % tape->tape_block_size) == 0)
2898 tape->user_bs_factor = count / tape->tape_block_size;
2899 }
2900 if ((rc = idetape_initiate_read(drive, tape->max_stages)) < 0)
2901 return rc;
2902 if (count == 0)
2903 return (0);
2904 if (tape->merge_stage_size) {
2905 actually_read = min((unsigned int)(tape->merge_stage_size), (unsigned int)count);
Daniel Walkerdcd96372006-06-25 05:47:37 -07002906 if (idetape_copy_stage_to_user(tape, buf, tape->merge_stage, actually_read))
2907 ret = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002908 buf += actually_read;
2909 tape->merge_stage_size -= actually_read;
2910 count -= actually_read;
2911 }
2912 while (count >= tape->stage_size) {
Borislav Petkovb6422012008-02-02 19:56:49 +01002913 bytes_read = idetape_add_chrdev_read_request(drive, ctl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002914 if (bytes_read <= 0)
2915 goto finish;
Daniel Walkerdcd96372006-06-25 05:47:37 -07002916 if (idetape_copy_stage_to_user(tape, buf, tape->merge_stage, bytes_read))
2917 ret = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002918 buf += bytes_read;
2919 count -= bytes_read;
2920 actually_read += bytes_read;
2921 }
2922 if (count) {
Borislav Petkovb6422012008-02-02 19:56:49 +01002923 bytes_read = idetape_add_chrdev_read_request(drive, ctl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002924 if (bytes_read <= 0)
2925 goto finish;
2926 temp = min((unsigned long)count, (unsigned long)bytes_read);
Daniel Walkerdcd96372006-06-25 05:47:37 -07002927 if (idetape_copy_stage_to_user(tape, buf, tape->merge_stage, temp))
2928 ret = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002929 actually_read += temp;
2930 tape->merge_stage_size = bytes_read-temp;
2931 }
2932finish:
2933 if (!actually_read && test_bit(IDETAPE_FILEMARK, &tape->flags)) {
Borislav Petkov8004a8c2008-02-06 02:57:51 +01002934 debug_log(DBG_SENSE, "%s: spacing over filemark\n", tape->name);
2935
Linus Torvalds1da177e2005-04-16 15:20:36 -07002936 idetape_space_over_filemarks(drive, MTFSF, 1);
2937 return 0;
2938 }
Daniel Walkerdcd96372006-06-25 05:47:37 -07002939
2940 return (ret) ? ret : actually_read;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002941}
2942
2943static ssize_t idetape_chrdev_write (struct file *file, const char __user *buf,
2944 size_t count, loff_t *ppos)
2945{
2946 struct ide_tape_obj *tape = ide_tape_f(file);
2947 ide_drive_t *drive = tape->drive;
Daniel Walkerdcd96372006-06-25 05:47:37 -07002948 ssize_t actually_written = 0;
2949 ssize_t ret = 0;
Borislav Petkovb6422012008-02-02 19:56:49 +01002950 u16 ctl = *(u16 *)&tape->caps[12];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002951
2952 /* The drive is write protected. */
2953 if (tape->write_prot)
2954 return -EACCES;
2955
Borislav Petkov8004a8c2008-02-06 02:57:51 +01002956 debug_log(DBG_CHRDEV, "Enter %s, count %Zd\n", __func__, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002957
2958 /* Initialize write operation */
2959 if (tape->chrdev_direction != idetape_direction_write) {
2960 if (tape->chrdev_direction == idetape_direction_read)
2961 idetape_discard_read_pipeline(drive, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002962 if (tape->merge_stage || tape->merge_stage_size) {
2963 printk(KERN_ERR "ide-tape: merge_stage_size "
2964 "should be 0 now\n");
2965 tape->merge_stage_size = 0;
2966 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002967 if ((tape->merge_stage = __idetape_kmalloc_stage(tape, 0, 0)) == NULL)
2968 return -ENOMEM;
2969 tape->chrdev_direction = idetape_direction_write;
2970 idetape_init_merge_stage(tape);
2971
2972 /*
2973 * Issue a write 0 command to ensure that DSC handshake
2974 * is switched from completion mode to buffer available
2975 * mode.
2976 * No point in issuing this if DSC overlap isn't supported,
2977 * some drives (Seagate STT3401A) will return an error.
2978 */
2979 if (drive->dsc_overlap) {
Daniel Walkerdcd96372006-06-25 05:47:37 -07002980 ssize_t retval = idetape_queue_rw_tail(drive, REQ_IDETAPE_WRITE, 0, tape->merge_stage->bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002981 if (retval < 0) {
2982 __idetape_kfree_stage(tape->merge_stage);
2983 tape->merge_stage = NULL;
2984 tape->chrdev_direction = idetape_direction_none;
2985 return retval;
2986 }
2987 }
2988 }
2989 if (count == 0)
2990 return (0);
2991 if (tape->restart_speed_control_req)
2992 idetape_restart_speed_control(drive);
2993 if (tape->merge_stage_size) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002994 if (tape->merge_stage_size >= tape->stage_size) {
2995 printk(KERN_ERR "ide-tape: bug: merge buffer too big\n");
2996 tape->merge_stage_size = 0;
2997 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002998 actually_written = min((unsigned int)(tape->stage_size - tape->merge_stage_size), (unsigned int)count);
Daniel Walkerdcd96372006-06-25 05:47:37 -07002999 if (idetape_copy_stage_from_user(tape, tape->merge_stage, buf, actually_written))
3000 ret = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003001 buf += actually_written;
3002 tape->merge_stage_size += actually_written;
3003 count -= actually_written;
3004
3005 if (tape->merge_stage_size == tape->stage_size) {
Daniel Walkerdcd96372006-06-25 05:47:37 -07003006 ssize_t retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003007 tape->merge_stage_size = 0;
Borislav Petkovb6422012008-02-02 19:56:49 +01003008 retval = idetape_add_chrdev_write_request(drive, ctl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003009 if (retval <= 0)
3010 return (retval);
3011 }
3012 }
3013 while (count >= tape->stage_size) {
Daniel Walkerdcd96372006-06-25 05:47:37 -07003014 ssize_t retval;
3015 if (idetape_copy_stage_from_user(tape, tape->merge_stage, buf, tape->stage_size))
3016 ret = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003017 buf += tape->stage_size;
3018 count -= tape->stage_size;
Borislav Petkovb6422012008-02-02 19:56:49 +01003019 retval = idetape_add_chrdev_write_request(drive, ctl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003020 actually_written += tape->stage_size;
3021 if (retval <= 0)
3022 return (retval);
3023 }
3024 if (count) {
3025 actually_written += count;
Daniel Walkerdcd96372006-06-25 05:47:37 -07003026 if (idetape_copy_stage_from_user(tape, tape->merge_stage, buf, count))
3027 ret = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003028 tape->merge_stage_size += count;
3029 }
Daniel Walkerdcd96372006-06-25 05:47:37 -07003030 return (ret) ? ret : actually_written;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003031}
3032
3033static int idetape_write_filemark (ide_drive_t *drive)
3034{
3035 idetape_pc_t pc;
3036
3037 /* Write a filemark */
3038 idetape_create_write_filemark_cmd(drive, &pc, 1);
3039 if (idetape_queue_pc_tail(drive, &pc)) {
3040 printk(KERN_ERR "ide-tape: Couldn't write a filemark\n");
3041 return -EIO;
3042 }
3043 return 0;
3044}
3045
3046/*
Borislav Petkovd99c9da2008-02-02 19:56:51 +01003047 * Called from idetape_chrdev_ioctl when the general mtio MTIOCTOP ioctl is
3048 * requested.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003049 *
Borislav Petkovd99c9da2008-02-02 19:56:51 +01003050 * Note: MTBSF and MTBSFM are not supported when the tape doesn't support
3051 * spacing over filemarks in the reverse direction. In this case, MTFSFM is also
3052 * usually not supported (it is supported in the rare case in which we crossed
3053 * the filemark during our read-ahead pipelined operation mode).
Linus Torvalds1da177e2005-04-16 15:20:36 -07003054 *
Borislav Petkovd99c9da2008-02-02 19:56:51 +01003055 * The following commands are currently not supported:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003056 *
Borislav Petkovd99c9da2008-02-02 19:56:51 +01003057 * MTFSS, MTBSS, MTWSM, MTSETDENSITY, MTSETDRVBUFFER, MT_ST_BOOLEANS,
3058 * MT_ST_WRITE_THRESHOLD.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003059 */
Borislav Petkovd99c9da2008-02-02 19:56:51 +01003060static int idetape_mtioctop(ide_drive_t *drive, short mt_op, int mt_count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003061{
3062 idetape_tape_t *tape = drive->driver_data;
3063 idetape_pc_t pc;
3064 int i,retval;
3065
Borislav Petkov8004a8c2008-02-06 02:57:51 +01003066 debug_log(DBG_ERR, "Handling MTIOCTOP ioctl: mt_op=%d, mt_count=%d\n",
3067 mt_op, mt_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003068 /*
3069 * Commands which need our pipelined read-ahead stages.
3070 */
3071 switch (mt_op) {
3072 case MTFSF:
3073 case MTFSFM:
3074 case MTBSF:
3075 case MTBSFM:
3076 if (!mt_count)
3077 return (0);
3078 return (idetape_space_over_filemarks(drive,mt_op,mt_count));
3079 default:
3080 break;
3081 }
3082 switch (mt_op) {
3083 case MTWEOF:
3084 if (tape->write_prot)
3085 return -EACCES;
3086 idetape_discard_read_pipeline(drive, 1);
3087 for (i = 0; i < mt_count; i++) {
3088 retval = idetape_write_filemark(drive);
3089 if (retval)
3090 return retval;
3091 }
3092 return (0);
3093 case MTREW:
3094 idetape_discard_read_pipeline(drive, 0);
3095 if (idetape_rewind_tape(drive))
3096 return -EIO;
3097 return 0;
3098 case MTLOAD:
3099 idetape_discard_read_pipeline(drive, 0);
3100 idetape_create_load_unload_cmd(drive, &pc, IDETAPE_LU_LOAD_MASK);
3101 return (idetape_queue_pc_tail(drive, &pc));
3102 case MTUNLOAD:
3103 case MTOFFL:
3104 /*
3105 * If door is locked, attempt to unlock before
3106 * attempting to eject.
3107 */
3108 if (tape->door_locked) {
3109 if (idetape_create_prevent_cmd(drive, &pc, 0))
3110 if (!idetape_queue_pc_tail(drive, &pc))
3111 tape->door_locked = DOOR_UNLOCKED;
3112 }
3113 idetape_discard_read_pipeline(drive, 0);
3114 idetape_create_load_unload_cmd(drive, &pc,!IDETAPE_LU_LOAD_MASK);
3115 retval = idetape_queue_pc_tail(drive, &pc);
3116 if (!retval)
3117 clear_bit(IDETAPE_MEDIUM_PRESENT, &tape->flags);
3118 return retval;
3119 case MTNOP:
3120 idetape_discard_read_pipeline(drive, 0);
3121 return (idetape_flush_tape_buffers(drive));
3122 case MTRETEN:
3123 idetape_discard_read_pipeline(drive, 0);
3124 idetape_create_load_unload_cmd(drive, &pc,IDETAPE_LU_RETENSION_MASK | IDETAPE_LU_LOAD_MASK);
3125 return (idetape_queue_pc_tail(drive, &pc));
3126 case MTEOM:
3127 idetape_create_space_cmd(&pc, 0, IDETAPE_SPACE_TO_EOD);
3128 return (idetape_queue_pc_tail(drive, &pc));
3129 case MTERASE:
3130 (void) idetape_rewind_tape(drive);
3131 idetape_create_erase_cmd(&pc);
3132 return (idetape_queue_pc_tail(drive, &pc));
3133 case MTSETBLK:
3134 if (mt_count) {
3135 if (mt_count < tape->tape_block_size || mt_count % tape->tape_block_size)
3136 return -EIO;
3137 tape->user_bs_factor = mt_count / tape->tape_block_size;
3138 clear_bit(IDETAPE_DETECT_BS, &tape->flags);
3139 } else
3140 set_bit(IDETAPE_DETECT_BS, &tape->flags);
3141 return 0;
3142 case MTSEEK:
3143 idetape_discard_read_pipeline(drive, 0);
3144 return idetape_position_tape(drive, mt_count * tape->user_bs_factor, tape->partition, 0);
3145 case MTSETPART:
3146 idetape_discard_read_pipeline(drive, 0);
3147 return (idetape_position_tape(drive, 0, mt_count, 0));
3148 case MTFSR:
3149 case MTBSR:
3150 case MTLOCK:
3151 if (!idetape_create_prevent_cmd(drive, &pc, 1))
3152 return 0;
3153 retval = idetape_queue_pc_tail(drive, &pc);
3154 if (retval) return retval;
3155 tape->door_locked = DOOR_EXPLICITLY_LOCKED;
3156 return 0;
3157 case MTUNLOCK:
3158 if (!idetape_create_prevent_cmd(drive, &pc, 0))
3159 return 0;
3160 retval = idetape_queue_pc_tail(drive, &pc);
3161 if (retval) return retval;
3162 tape->door_locked = DOOR_UNLOCKED;
3163 return 0;
3164 default:
3165 printk(KERN_ERR "ide-tape: MTIO operation %d not "
3166 "supported\n", mt_op);
3167 return (-EIO);
3168 }
3169}
3170
3171/*
Borislav Petkovd99c9da2008-02-02 19:56:51 +01003172 * Our character device ioctls. General mtio.h magnetic io commands are
3173 * supported here, and not in the corresponding block interface. Our own
3174 * ide-tape ioctls are supported on both interfaces.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003175 */
Borislav Petkovd99c9da2008-02-02 19:56:51 +01003176static int idetape_chrdev_ioctl(struct inode *inode, struct file *file,
3177 unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003178{
3179 struct ide_tape_obj *tape = ide_tape_f(file);
3180 ide_drive_t *drive = tape->drive;
3181 struct mtop mtop;
3182 struct mtget mtget;
3183 struct mtpos mtpos;
3184 int block_offset = 0, position = tape->first_frame_position;
3185 void __user *argp = (void __user *)arg;
3186
Borislav Petkov8004a8c2008-02-06 02:57:51 +01003187 debug_log(DBG_CHRDEV, "Enter %s, cmd=%u\n", __func__, cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003188
3189 tape->restart_speed_control_req = 1;
3190 if (tape->chrdev_direction == idetape_direction_write) {
3191 idetape_empty_write_pipeline(drive);
3192 idetape_flush_tape_buffers(drive);
3193 }
3194 if (cmd == MTIOCGET || cmd == MTIOCPOS) {
3195 block_offset = idetape_pipeline_size(drive) / (tape->tape_block_size * tape->user_bs_factor);
3196 if ((position = idetape_read_position(drive)) < 0)
3197 return -EIO;
3198 }
3199 switch (cmd) {
3200 case MTIOCTOP:
3201 if (copy_from_user(&mtop, argp, sizeof (struct mtop)))
3202 return -EFAULT;
3203 return (idetape_mtioctop(drive,mtop.mt_op,mtop.mt_count));
3204 case MTIOCGET:
3205 memset(&mtget, 0, sizeof (struct mtget));
3206 mtget.mt_type = MT_ISSCSI2;
3207 mtget.mt_blkno = position / tape->user_bs_factor - block_offset;
3208 mtget.mt_dsreg = ((tape->tape_block_size * tape->user_bs_factor) << MT_ST_BLKSIZE_SHIFT) & MT_ST_BLKSIZE_MASK;
3209 if (tape->drv_write_prot) {
3210 mtget.mt_gstat |= GMT_WR_PROT(0xffffffff);
3211 }
3212 if (copy_to_user(argp, &mtget, sizeof(struct mtget)))
3213 return -EFAULT;
3214 return 0;
3215 case MTIOCPOS:
3216 mtpos.mt_blkno = position / tape->user_bs_factor - block_offset;
3217 if (copy_to_user(argp, &mtpos, sizeof(struct mtpos)))
3218 return -EFAULT;
3219 return 0;
3220 default:
3221 if (tape->chrdev_direction == idetape_direction_read)
3222 idetape_discard_read_pipeline(drive, 1);
3223 return idetape_blkdev_ioctl(drive, cmd, arg);
3224 }
3225}
3226
Borislav Petkov3cffb9c2008-02-02 19:56:50 +01003227/*
3228 * Do a mode sense page 0 with block descriptor and if it succeeds set the tape
3229 * block size with the reported value.
3230 */
3231static void ide_tape_get_bsize_from_bdesc(ide_drive_t *drive)
3232{
3233 idetape_tape_t *tape = drive->driver_data;
3234 idetape_pc_t pc;
3235
3236 idetape_create_mode_sense_cmd(&pc, IDETAPE_BLOCK_DESCRIPTOR);
3237 if (idetape_queue_pc_tail(drive, &pc)) {
3238 printk(KERN_ERR "ide-tape: Can't get block descriptor\n");
3239 if (tape->tape_block_size == 0) {
3240 printk(KERN_WARNING "ide-tape: Cannot deal with zero "
3241 "block size, assuming 32k\n");
3242 tape->tape_block_size = 32768;
3243 }
3244 return;
3245 }
3246 tape->tape_block_size = (pc.buffer[4 + 5] << 16) +
3247 (pc.buffer[4 + 6] << 8) +
3248 pc.buffer[4 + 7];
3249 tape->drv_write_prot = (pc.buffer[2] & 0x80) >> 7;
3250}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003251
3252/*
3253 * Our character device open function.
3254 */
3255static int idetape_chrdev_open (struct inode *inode, struct file *filp)
3256{
3257 unsigned int minor = iminor(inode), i = minor & ~0xc0;
3258 ide_drive_t *drive;
3259 idetape_tape_t *tape;
3260 idetape_pc_t pc;
3261 int retval;
3262
Borislav Petkov8004a8c2008-02-06 02:57:51 +01003263 if (i >= MAX_HWIFS * MAX_DRIVES)
3264 return -ENXIO;
3265
3266 tape = ide_tape_chrdev_get(i);
3267 if (!tape)
3268 return -ENXIO;
3269
3270 debug_log(DBG_CHRDEV, "Enter %s\n", __func__);
3271
Linus Torvalds1da177e2005-04-16 15:20:36 -07003272 /*
3273 * We really want to do nonseekable_open(inode, filp); here, but some
3274 * versions of tar incorrectly call lseek on tapes and bail out if that
3275 * fails. So we disallow pread() and pwrite(), but permit lseeks.
3276 */
3277 filp->f_mode &= ~(FMODE_PREAD | FMODE_PWRITE);
3278
Linus Torvalds1da177e2005-04-16 15:20:36 -07003279 drive = tape->drive;
3280
3281 filp->private_data = tape;
3282
3283 if (test_and_set_bit(IDETAPE_BUSY, &tape->flags)) {
3284 retval = -EBUSY;
3285 goto out_put_tape;
3286 }
3287
3288 retval = idetape_wait_ready(drive, 60 * HZ);
3289 if (retval) {
3290 clear_bit(IDETAPE_BUSY, &tape->flags);
3291 printk(KERN_ERR "ide-tape: %s: drive not ready\n", tape->name);
3292 goto out_put_tape;
3293 }
3294
3295 idetape_read_position(drive);
3296 if (!test_bit(IDETAPE_ADDRESS_VALID, &tape->flags))
3297 (void)idetape_rewind_tape(drive);
3298
3299 if (tape->chrdev_direction != idetape_direction_read)
3300 clear_bit(IDETAPE_PIPELINE_ERROR, &tape->flags);
3301
3302 /* Read block size and write protect status from drive. */
Borislav Petkov3cffb9c2008-02-02 19:56:50 +01003303 ide_tape_get_bsize_from_bdesc(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003304
3305 /* Set write protect flag if device is opened as read-only. */
3306 if ((filp->f_flags & O_ACCMODE) == O_RDONLY)
3307 tape->write_prot = 1;
3308 else
3309 tape->write_prot = tape->drv_write_prot;
3310
3311 /* Make sure drive isn't write protected if user wants to write. */
3312 if (tape->write_prot) {
3313 if ((filp->f_flags & O_ACCMODE) == O_WRONLY ||
3314 (filp->f_flags & O_ACCMODE) == O_RDWR) {
3315 clear_bit(IDETAPE_BUSY, &tape->flags);
3316 retval = -EROFS;
3317 goto out_put_tape;
3318 }
3319 }
3320
3321 /*
3322 * Lock the tape drive door so user can't eject.
3323 */
3324 if (tape->chrdev_direction == idetape_direction_none) {
3325 if (idetape_create_prevent_cmd(drive, &pc, 1)) {
3326 if (!idetape_queue_pc_tail(drive, &pc)) {
3327 if (tape->door_locked != DOOR_EXPLICITLY_LOCKED)
3328 tape->door_locked = DOOR_LOCKED;
3329 }
3330 }
3331 }
3332 idetape_restart_speed_control(drive);
3333 tape->restart_speed_control_req = 0;
3334 return 0;
3335
3336out_put_tape:
3337 ide_tape_put(tape);
3338 return retval;
3339}
3340
3341static void idetape_write_release (ide_drive_t *drive, unsigned int minor)
3342{
3343 idetape_tape_t *tape = drive->driver_data;
3344
3345 idetape_empty_write_pipeline(drive);
3346 tape->merge_stage = __idetape_kmalloc_stage(tape, 1, 0);
3347 if (tape->merge_stage != NULL) {
3348 idetape_pad_zeros(drive, tape->tape_block_size * (tape->user_bs_factor - 1));
3349 __idetape_kfree_stage(tape->merge_stage);
3350 tape->merge_stage = NULL;
3351 }
3352 idetape_write_filemark(drive);
3353 idetape_flush_tape_buffers(drive);
3354 idetape_flush_tape_buffers(drive);
3355}
3356
3357/*
3358 * Our character device release function.
3359 */
3360static int idetape_chrdev_release (struct inode *inode, struct file *filp)
3361{
3362 struct ide_tape_obj *tape = ide_tape_f(filp);
3363 ide_drive_t *drive = tape->drive;
3364 idetape_pc_t pc;
3365 unsigned int minor = iminor(inode);
3366
3367 lock_kernel();
3368 tape = drive->driver_data;
Borislav Petkov8004a8c2008-02-06 02:57:51 +01003369
3370 debug_log(DBG_CHRDEV, "Enter %s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003371
3372 if (tape->chrdev_direction == idetape_direction_write)
3373 idetape_write_release(drive, minor);
3374 if (tape->chrdev_direction == idetape_direction_read) {
3375 if (minor < 128)
3376 idetape_discard_read_pipeline(drive, 1);
3377 else
3378 idetape_wait_for_pipeline(drive);
3379 }
3380 if (tape->cache_stage != NULL) {
3381 __idetape_kfree_stage(tape->cache_stage);
3382 tape->cache_stage = NULL;
3383 }
3384 if (minor < 128 && test_bit(IDETAPE_MEDIUM_PRESENT, &tape->flags))
3385 (void) idetape_rewind_tape(drive);
3386 if (tape->chrdev_direction == idetape_direction_none) {
3387 if (tape->door_locked == DOOR_LOCKED) {
3388 if (idetape_create_prevent_cmd(drive, &pc, 0)) {
3389 if (!idetape_queue_pc_tail(drive, &pc))
3390 tape->door_locked = DOOR_UNLOCKED;
3391 }
3392 }
3393 }
3394 clear_bit(IDETAPE_BUSY, &tape->flags);
3395 ide_tape_put(tape);
3396 unlock_kernel();
3397 return 0;
3398}
3399
3400/*
3401 * idetape_identify_device is called to check the contents of the
3402 * ATAPI IDENTIFY command results. We return:
3403 *
3404 * 1 If the tape can be supported by us, based on the information
3405 * we have so far.
3406 *
3407 * 0 If this tape driver is not currently supported by us.
3408 */
3409static int idetape_identify_device (ide_drive_t *drive)
3410{
3411 struct idetape_id_gcw gcw;
3412 struct hd_driveid *id = drive->id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003413
3414 if (drive->id_read == 0)
3415 return 1;
3416
3417 *((unsigned short *) &gcw) = id->config;
3418
Linus Torvalds1da177e2005-04-16 15:20:36 -07003419 /* Check that we can support this device */
3420
Bartlomiej Zolnierkiewicz16422de32008-02-02 19:56:48 +01003421 if (gcw.protocol != 2)
3422 printk(KERN_ERR "ide-tape: Protocol (0x%02x) is not ATAPI\n",
3423 gcw.protocol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003424 else if (gcw.device_type != 1)
Bartlomiej Zolnierkiewicz16422de32008-02-02 19:56:48 +01003425 printk(KERN_ERR "ide-tape: Device type (0x%02x) is not set "
3426 "to tape\n", gcw.device_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003427 else if (!gcw.removable)
3428 printk(KERN_ERR "ide-tape: The removable flag is not set\n");
3429 else if (gcw.packet_size != 0) {
Bartlomiej Zolnierkiewicz16422de32008-02-02 19:56:48 +01003430 printk(KERN_ERR "ide-tape: Packet size (0x%02x) is not 12 "
3431 "bytes long\n", gcw.packet_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003432 } else
3433 return 1;
3434 return 0;
3435}
3436
Borislav Petkov6d29c8f2008-02-02 19:56:49 +01003437static void idetape_get_inquiry_results(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003438{
3439 char *r;
3440 idetape_tape_t *tape = drive->driver_data;
3441 idetape_pc_t pc;
Borislav Petkov6d29c8f2008-02-02 19:56:49 +01003442
Linus Torvalds1da177e2005-04-16 15:20:36 -07003443 idetape_create_inquiry_cmd(&pc);
3444 if (idetape_queue_pc_tail(drive, &pc)) {
Borislav Petkov6d29c8f2008-02-02 19:56:49 +01003445 printk(KERN_ERR "ide-tape: %s: can't get INQUIRY results\n",
3446 tape->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003447 return;
3448 }
Borislav Petkov6d29c8f2008-02-02 19:56:49 +01003449 memcpy(tape->vendor_id, &pc.buffer[8], 8);
3450 memcpy(tape->product_id, &pc.buffer[16], 16);
3451 memcpy(tape->firmware_revision, &pc.buffer[32], 4);
3452
Linus Torvalds1da177e2005-04-16 15:20:36 -07003453 ide_fixstring(tape->vendor_id, 10, 0);
3454 ide_fixstring(tape->product_id, 18, 0);
3455 ide_fixstring(tape->firmware_revision, 6, 0);
3456 r = tape->firmware_revision;
3457 if (*(r + 1) == '.')
Borislav Petkov6d29c8f2008-02-02 19:56:49 +01003458 tape->firmware_revision_num = (*r - '0') * 100 +
3459 (*(r + 2) - '0') * 10 + *(r + 3) - '0';
3460 printk(KERN_INFO "ide-tape: %s <-> %s: %s %s rev %s\n",
3461 drive->name, tape->name, tape->vendor_id,
3462 tape->product_id, tape->firmware_revision);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003463}
3464
3465/*
Borislav Petkovb6422012008-02-02 19:56:49 +01003466 * Ask the tape about its various parameters. In particular, we will adjust our
3467 * data transfer buffer size to the recommended value as returned by the tape.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003468 */
3469static void idetape_get_mode_sense_results (ide_drive_t *drive)
3470{
3471 idetape_tape_t *tape = drive->driver_data;
3472 idetape_pc_t pc;
Borislav Petkovb6422012008-02-02 19:56:49 +01003473 u8 *caps;
3474 u8 speed, max_speed;
Borislav Petkov47314fa2008-02-02 19:56:48 +01003475
Linus Torvalds1da177e2005-04-16 15:20:36 -07003476 idetape_create_mode_sense_cmd(&pc, IDETAPE_CAPABILITIES_PAGE);
3477 if (idetape_queue_pc_tail(drive, &pc)) {
Borislav Petkovb6422012008-02-02 19:56:49 +01003478 printk(KERN_ERR "ide-tape: Can't get tape parameters - assuming"
3479 " some default values\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003480 tape->tape_block_size = 512;
Borislav Petkovb6422012008-02-02 19:56:49 +01003481 put_unaligned(52, (u16 *)&tape->caps[12]);
3482 put_unaligned(540, (u16 *)&tape->caps[14]);
3483 put_unaligned(6*52, (u16 *)&tape->caps[16]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003484 return;
3485 }
Borislav Petkovb6422012008-02-02 19:56:49 +01003486 caps = pc.buffer + 4 + pc.buffer[3];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003487
Borislav Petkovb6422012008-02-02 19:56:49 +01003488 /* convert to host order and save for later use */
3489 speed = be16_to_cpu(*(u16 *)&caps[14]);
3490 max_speed = be16_to_cpu(*(u16 *)&caps[8]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003491
Borislav Petkovb6422012008-02-02 19:56:49 +01003492 put_unaligned(max_speed, (u16 *)&caps[8]);
3493 put_unaligned(be16_to_cpu(*(u16 *)&caps[12]), (u16 *)&caps[12]);
3494 put_unaligned(speed, (u16 *)&caps[14]);
3495 put_unaligned(be16_to_cpu(*(u16 *)&caps[16]), (u16 *)&caps[16]);
3496
3497 if (!speed) {
3498 printk(KERN_INFO "ide-tape: %s: invalid tape speed "
3499 "(assuming 650KB/sec)\n", drive->name);
3500 put_unaligned(650, (u16 *)&caps[14]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003501 }
Borislav Petkovb6422012008-02-02 19:56:49 +01003502 if (!max_speed) {
3503 printk(KERN_INFO "ide-tape: %s: invalid max_speed "
3504 "(assuming 650KB/sec)\n", drive->name);
3505 put_unaligned(650, (u16 *)&caps[8]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003506 }
3507
Borislav Petkovb6422012008-02-02 19:56:49 +01003508 memcpy(&tape->caps, caps, 20);
3509 if (caps[7] & 0x02)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003510 tape->tape_block_size = 512;
Borislav Petkovb6422012008-02-02 19:56:49 +01003511 else if (caps[7] & 0x04)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003512 tape->tape_block_size = 1024;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003513}
3514
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +02003515#ifdef CONFIG_IDE_PROC_FS
Linus Torvalds1da177e2005-04-16 15:20:36 -07003516static void idetape_add_settings (ide_drive_t *drive)
3517{
3518 idetape_tape_t *tape = drive->driver_data;
3519
3520/*
Bartlomiej Zolnierkiewicz14979432007-05-10 00:01:10 +02003521 * drive setting name read/write data type min max mul_factor div_factor data pointer set function
Linus Torvalds1da177e2005-04-16 15:20:36 -07003522 */
Borislav Petkovb6422012008-02-02 19:56:49 +01003523 ide_add_setting(drive, "buffer", SETTING_READ, TYPE_SHORT, 0, 0xffff,
3524 1, 2, (u16 *)&tape->caps[16], NULL);
Bartlomiej Zolnierkiewicz14979432007-05-10 00:01:10 +02003525 ide_add_setting(drive, "pipeline_min", SETTING_RW, TYPE_INT, 1, 0xffff, tape->stage_size / 1024, 1, &tape->min_pipeline, NULL);
3526 ide_add_setting(drive, "pipeline", SETTING_RW, TYPE_INT, 1, 0xffff, tape->stage_size / 1024, 1, &tape->max_stages, NULL);
3527 ide_add_setting(drive, "pipeline_max", SETTING_RW, TYPE_INT, 1, 0xffff, tape->stage_size / 1024, 1, &tape->max_pipeline, NULL);
3528 ide_add_setting(drive, "pipeline_used", SETTING_READ, TYPE_INT, 0, 0xffff, tape->stage_size / 1024, 1, &tape->nr_stages, NULL);
3529 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 +01003530 ide_add_setting(drive, "speed", SETTING_READ, TYPE_SHORT, 0, 0xffff,
3531 1, 1, (u16 *)&tape->caps[14], NULL);
Bartlomiej Zolnierkiewicz14979432007-05-10 00:01:10 +02003532 ide_add_setting(drive, "stage", SETTING_READ, TYPE_INT, 0, 0xffff, 1, 1024, &tape->stage_size, NULL);
3533 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);
3534 ide_add_setting(drive, "dsc_overlap", SETTING_RW, TYPE_BYTE, 0, 1, 1, 1, &drive->dsc_overlap, NULL);
3535 ide_add_setting(drive, "pipeline_head_speed_c",SETTING_READ, TYPE_INT, 0, 0xffff, 1, 1, &tape->controlled_pipeline_head_speed, NULL);
3536 ide_add_setting(drive, "pipeline_head_speed_u",SETTING_READ, TYPE_INT, 0, 0xffff, 1, 1, &tape->uncontrolled_pipeline_head_speed,NULL);
3537 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 +01003538 ide_add_setting(drive, "debug_mask", SETTING_RW, TYPE_INT, 0, 0xffff, 1,
3539 1, &tape->debug_mask, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003540}
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +02003541#else
3542static inline void idetape_add_settings(ide_drive_t *drive) { ; }
3543#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003544
3545/*
3546 * ide_setup is called to:
3547 *
3548 * 1. Initialize our various state variables.
3549 * 2. Ask the tape for its capabilities.
3550 * 3. Allocate a buffer which will be used for data
3551 * transfer. The buffer size is chosen based on
3552 * the recommendation which we received in step (2).
3553 *
3554 * Note that at this point ide.c already assigned us an irq, so that
3555 * we can queue requests here and wait for their completion.
3556 */
3557static void idetape_setup (ide_drive_t *drive, idetape_tape_t *tape, int minor)
3558{
3559 unsigned long t1, tmid, tn, t;
3560 int speed;
3561 struct idetape_id_gcw gcw;
3562 int stage_size;
3563 struct sysinfo si;
Borislav Petkovb6422012008-02-02 19:56:49 +01003564 u16 *ctl = (u16 *)&tape->caps[12];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003565
3566 spin_lock_init(&tape->spinlock);
3567 drive->dsc_overlap = 1;
Bartlomiej Zolnierkiewicz4166c192008-02-01 23:09:30 +01003568 if (drive->hwif->host_flags & IDE_HFLAG_NO_DSC) {
3569 printk(KERN_INFO "ide-tape: %s: disabling DSC overlap\n",
3570 tape->name);
3571 drive->dsc_overlap = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003572 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003573 /* Seagate Travan drives do not support DSC overlap. */
3574 if (strstr(drive->id->model, "Seagate STT3401"))
3575 drive->dsc_overlap = 0;
3576 tape->minor = minor;
3577 tape->name[0] = 'h';
3578 tape->name[1] = 't';
3579 tape->name[2] = '0' + minor;
3580 tape->chrdev_direction = idetape_direction_none;
3581 tape->pc = tape->pc_stack;
3582 tape->max_insert_speed = 10000;
3583 tape->speed_control = 1;
3584 *((unsigned short *) &gcw) = drive->id->config;
3585 if (gcw.drq_type == 1)
3586 set_bit(IDETAPE_DRQ_INTERRUPT, &tape->flags);
3587
3588 tape->min_pipeline = tape->max_pipeline = tape->max_stages = 10;
3589
3590 idetape_get_inquiry_results(drive);
3591 idetape_get_mode_sense_results(drive);
Borislav Petkov3cffb9c2008-02-02 19:56:50 +01003592 ide_tape_get_bsize_from_bdesc(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003593 tape->user_bs_factor = 1;
Borislav Petkovb6422012008-02-02 19:56:49 +01003594 tape->stage_size = *ctl * tape->tape_block_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003595 while (tape->stage_size > 0xffff) {
3596 printk(KERN_NOTICE "ide-tape: decreasing stage size\n");
Borislav Petkovb6422012008-02-02 19:56:49 +01003597 *ctl /= 2;
3598 tape->stage_size = *ctl * tape->tape_block_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003599 }
3600 stage_size = tape->stage_size;
3601 tape->pages_per_stage = stage_size / PAGE_SIZE;
3602 if (stage_size % PAGE_SIZE) {
3603 tape->pages_per_stage++;
3604 tape->excess_bh_size = PAGE_SIZE - stage_size % PAGE_SIZE;
3605 }
3606
Borislav Petkovb6422012008-02-02 19:56:49 +01003607 /* Select the "best" DSC read/write polling freq and pipeline size. */
3608 speed = max(*(u16 *)&tape->caps[14], *(u16 *)&tape->caps[8]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003609
3610 tape->max_stages = speed * 1000 * 10 / tape->stage_size;
3611
3612 /*
3613 * Limit memory use for pipeline to 10% of physical memory
3614 */
3615 si_meminfo(&si);
3616 if (tape->max_stages * tape->stage_size > si.totalram * si.mem_unit / 10)
3617 tape->max_stages = si.totalram * si.mem_unit / (10 * tape->stage_size);
3618 tape->max_stages = min(tape->max_stages, IDETAPE_MAX_PIPELINE_STAGES);
3619 tape->min_pipeline = min(tape->max_stages, IDETAPE_MIN_PIPELINE_STAGES);
3620 tape->max_pipeline = min(tape->max_stages * 2, IDETAPE_MAX_PIPELINE_STAGES);
3621 if (tape->max_stages == 0)
3622 tape->max_stages = tape->min_pipeline = tape->max_pipeline = 1;
3623
3624 t1 = (tape->stage_size * HZ) / (speed * 1000);
Borislav Petkovb6422012008-02-02 19:56:49 +01003625 tmid = (*(u16 *)&tape->caps[16] * 32 * HZ) / (speed * 125);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003626 tn = (IDETAPE_FIFO_THRESHOLD * tape->stage_size * HZ) / (speed * 1000);
3627
3628 if (tape->max_stages)
3629 t = tn;
3630 else
3631 t = t1;
3632
3633 /*
3634 * Ensure that the number we got makes sense; limit
3635 * it within IDETAPE_DSC_RW_MIN and IDETAPE_DSC_RW_MAX.
3636 */
3637 tape->best_dsc_rw_frequency = max_t(unsigned long, min_t(unsigned long, t, IDETAPE_DSC_RW_MAX), IDETAPE_DSC_RW_MIN);
3638 printk(KERN_INFO "ide-tape: %s <-> %s: %dKBps, %d*%dkB buffer, "
3639 "%dkB pipeline, %lums tDSC%s\n",
Borislav Petkovb6422012008-02-02 19:56:49 +01003640 drive->name, tape->name, *(u16 *)&tape->caps[14],
3641 (*(u16 *)&tape->caps[16] * 512) / tape->stage_size,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003642 tape->stage_size / 1024,
3643 tape->max_stages * tape->stage_size / 1024,
3644 tape->best_dsc_rw_frequency * 1000 / HZ,
3645 drive->using_dma ? ", DMA":"");
3646
3647 idetape_add_settings(drive);
3648}
3649
Russell King4031bbe2006-01-06 11:41:00 +00003650static void ide_tape_remove(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003651{
3652 idetape_tape_t *tape = drive->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003653
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +02003654 ide_proc_unregister_driver(drive, tape->driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003655
3656 ide_unregister_region(tape->disk);
3657
3658 ide_tape_put(tape);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003659}
3660
3661static void ide_tape_release(struct kref *kref)
3662{
3663 struct ide_tape_obj *tape = to_ide_tape(kref);
3664 ide_drive_t *drive = tape->drive;
3665 struct gendisk *g = tape->disk;
3666
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02003667 BUG_ON(tape->first_stage != NULL || tape->merge_stage_size);
3668
Linus Torvalds1da177e2005-04-16 15:20:36 -07003669 drive->dsc_overlap = 0;
3670 drive->driver_data = NULL;
Tony Jonesdbc12722007-09-25 02:03:03 +02003671 device_destroy(idetape_sysfs_class, MKDEV(IDETAPE_MAJOR, tape->minor));
3672 device_destroy(idetape_sysfs_class, MKDEV(IDETAPE_MAJOR, tape->minor + 128));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003673 idetape_devs[tape->minor] = NULL;
3674 g->private_data = NULL;
3675 put_disk(g);
3676 kfree(tape);
3677}
3678
Bartlomiej Zolnierkiewiczecfd80e2007-05-10 00:01:09 +02003679#ifdef CONFIG_IDE_PROC_FS
Linus Torvalds1da177e2005-04-16 15:20:36 -07003680static int proc_idetape_read_name
3681 (char *page, char **start, off_t off, int count, int *eof, void *data)
3682{
3683 ide_drive_t *drive = (ide_drive_t *) data;
3684 idetape_tape_t *tape = drive->driver_data;
3685 char *out = page;
3686 int len;
3687
3688 len = sprintf(out, "%s\n", tape->name);
3689 PROC_IDE_READ_RETURN(page, start, off, count, eof, len);
3690}
3691
3692static ide_proc_entry_t idetape_proc[] = {
3693 { "capacity", S_IFREG|S_IRUGO, proc_ide_read_capacity, NULL },
3694 { "name", S_IFREG|S_IRUGO, proc_idetape_read_name, NULL },
3695 { NULL, 0, NULL, NULL }
3696};
Linus Torvalds1da177e2005-04-16 15:20:36 -07003697#endif
3698
Russell King4031bbe2006-01-06 11:41:00 +00003699static int ide_tape_probe(ide_drive_t *);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003700
Linus Torvalds1da177e2005-04-16 15:20:36 -07003701static ide_driver_t idetape_driver = {
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02003702 .gen_driver = {
Laurent Riffard4ef3b8f2005-11-18 22:15:40 +01003703 .owner = THIS_MODULE,
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02003704 .name = "ide-tape",
3705 .bus = &ide_bus_type,
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02003706 },
Russell King4031bbe2006-01-06 11:41:00 +00003707 .probe = ide_tape_probe,
3708 .remove = ide_tape_remove,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003709 .version = IDETAPE_VERSION,
3710 .media = ide_tape,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003711 .supports_dsc_overlap = 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003712 .do_request = idetape_do_request,
3713 .end_request = idetape_end_request,
3714 .error = __ide_error,
3715 .abort = __ide_abort,
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +02003716#ifdef CONFIG_IDE_PROC_FS
Linus Torvalds1da177e2005-04-16 15:20:36 -07003717 .proc = idetape_proc,
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +02003718#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003719};
3720
3721/*
3722 * Our character device supporting functions, passed to register_chrdev.
3723 */
Arjan van de Ven2b8693c2007-02-12 00:55:32 -08003724static const struct file_operations idetape_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003725 .owner = THIS_MODULE,
3726 .read = idetape_chrdev_read,
3727 .write = idetape_chrdev_write,
3728 .ioctl = idetape_chrdev_ioctl,
3729 .open = idetape_chrdev_open,
3730 .release = idetape_chrdev_release,
3731};
3732
3733static int idetape_open(struct inode *inode, struct file *filp)
3734{
3735 struct gendisk *disk = inode->i_bdev->bd_disk;
3736 struct ide_tape_obj *tape;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003737
3738 if (!(tape = ide_tape_get(disk)))
3739 return -ENXIO;
3740
Linus Torvalds1da177e2005-04-16 15:20:36 -07003741 return 0;
3742}
3743
3744static int idetape_release(struct inode *inode, struct file *filp)
3745{
3746 struct gendisk *disk = inode->i_bdev->bd_disk;
3747 struct ide_tape_obj *tape = ide_tape_g(disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003748
3749 ide_tape_put(tape);
3750
3751 return 0;
3752}
3753
3754static int idetape_ioctl(struct inode *inode, struct file *file,
3755 unsigned int cmd, unsigned long arg)
3756{
3757 struct block_device *bdev = inode->i_bdev;
3758 struct ide_tape_obj *tape = ide_tape_g(bdev->bd_disk);
3759 ide_drive_t *drive = tape->drive;
3760 int err = generic_ide_ioctl(drive, file, bdev, cmd, arg);
3761 if (err == -EINVAL)
3762 err = idetape_blkdev_ioctl(drive, cmd, arg);
3763 return err;
3764}
3765
3766static struct block_device_operations idetape_block_ops = {
3767 .owner = THIS_MODULE,
3768 .open = idetape_open,
3769 .release = idetape_release,
3770 .ioctl = idetape_ioctl,
3771};
3772
Russell King4031bbe2006-01-06 11:41:00 +00003773static int ide_tape_probe(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003774{
3775 idetape_tape_t *tape;
3776 struct gendisk *g;
3777 int minor;
3778
3779 if (!strstr("ide-tape", drive->driver_req))
3780 goto failed;
3781 if (!drive->present)
3782 goto failed;
3783 if (drive->media != ide_tape)
3784 goto failed;
3785 if (!idetape_identify_device (drive)) {
3786 printk(KERN_ERR "ide-tape: %s: not supported by this version of ide-tape\n", drive->name);
3787 goto failed;
3788 }
3789 if (drive->scsi) {
3790 printk("ide-tape: passing drive %s to ide-scsi emulation.\n", drive->name);
3791 goto failed;
3792 }
3793 if (strstr(drive->id->model, "OnStream DI-")) {
3794 printk(KERN_WARNING "ide-tape: Use drive %s with ide-scsi emulation and osst.\n", drive->name);
3795 printk(KERN_WARNING "ide-tape: OnStream support will be removed soon from ide-tape!\n");
3796 }
Robert P. J. Day5cbded52006-12-13 00:35:56 -08003797 tape = kzalloc(sizeof (idetape_tape_t), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003798 if (tape == NULL) {
3799 printk(KERN_ERR "ide-tape: %s: Can't allocate a tape structure\n", drive->name);
3800 goto failed;
3801 }
3802
3803 g = alloc_disk(1 << PARTN_BITS);
3804 if (!g)
3805 goto out_free_tape;
3806
3807 ide_init_disk(g, drive);
3808
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +02003809 ide_proc_register_driver(drive, &idetape_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003810
Linus Torvalds1da177e2005-04-16 15:20:36 -07003811 kref_init(&tape->kref);
3812
3813 tape->drive = drive;
3814 tape->driver = &idetape_driver;
3815 tape->disk = g;
3816
3817 g->private_data = &tape->driver;
3818
3819 drive->driver_data = tape;
3820
Arjan van de Vencf8b8972006-03-23 03:00:45 -08003821 mutex_lock(&idetape_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003822 for (minor = 0; idetape_devs[minor]; minor++)
3823 ;
3824 idetape_devs[minor] = tape;
Arjan van de Vencf8b8972006-03-23 03:00:45 -08003825 mutex_unlock(&idetape_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003826
3827 idetape_setup(drive, tape, minor);
3828
Tony Jonesdbc12722007-09-25 02:03:03 +02003829 device_create(idetape_sysfs_class, &drive->gendev,
3830 MKDEV(IDETAPE_MAJOR, minor), "%s", tape->name);
3831 device_create(idetape_sysfs_class, &drive->gendev,
3832 MKDEV(IDETAPE_MAJOR, minor + 128), "n%s", tape->name);
Will Dysond5dee802005-09-16 02:55:07 -07003833
Linus Torvalds1da177e2005-04-16 15:20:36 -07003834 g->fops = &idetape_block_ops;
3835 ide_register_region(g);
3836
3837 return 0;
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02003838
Linus Torvalds1da177e2005-04-16 15:20:36 -07003839out_free_tape:
3840 kfree(tape);
3841failed:
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02003842 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003843}
3844
3845MODULE_DESCRIPTION("ATAPI Streaming TAPE Driver");
3846MODULE_LICENSE("GPL");
3847
3848static void __exit idetape_exit (void)
3849{
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02003850 driver_unregister(&idetape_driver.gen_driver);
Will Dysond5dee802005-09-16 02:55:07 -07003851 class_destroy(idetape_sysfs_class);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003852 unregister_chrdev(IDETAPE_MAJOR, "ht");
3853}
3854
Bartlomiej Zolnierkiewicz17514e82005-11-19 22:24:35 +01003855static int __init idetape_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003856{
Will Dysond5dee802005-09-16 02:55:07 -07003857 int error = 1;
3858 idetape_sysfs_class = class_create(THIS_MODULE, "ide_tape");
3859 if (IS_ERR(idetape_sysfs_class)) {
3860 idetape_sysfs_class = NULL;
3861 printk(KERN_ERR "Unable to create sysfs class for ide tapes\n");
3862 error = -EBUSY;
3863 goto out;
3864 }
3865
Linus Torvalds1da177e2005-04-16 15:20:36 -07003866 if (register_chrdev(IDETAPE_MAJOR, "ht", &idetape_fops)) {
3867 printk(KERN_ERR "ide-tape: Failed to register character device interface\n");
Will Dysond5dee802005-09-16 02:55:07 -07003868 error = -EBUSY;
3869 goto out_free_class;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003870 }
Will Dysond5dee802005-09-16 02:55:07 -07003871
3872 error = driver_register(&idetape_driver.gen_driver);
3873 if (error)
3874 goto out_free_driver;
3875
3876 return 0;
3877
3878out_free_driver:
3879 driver_unregister(&idetape_driver.gen_driver);
3880out_free_class:
3881 class_destroy(idetape_sysfs_class);
3882out:
3883 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003884}
3885
Kay Sievers263756e2005-12-12 18:03:44 +01003886MODULE_ALIAS("ide:*m-tape*");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003887module_init(idetape_init);
3888module_exit(idetape_exit);
3889MODULE_ALIAS_CHARDEV_MAJOR(IDETAPE_MAJOR);