ide-tape remove pipeline speed/control calculations
[deliverable/linux.git] / drivers / ide / ide-tape.c
CommitLineData
1da177e4 1/*
5ce78af4
BP
2 * IDE ATAPI streaming tape driver.
3 *
59bca8cc
BZ
4 * Copyright (C) 1995-1999 Gadi Oxman <gadio@netvision.net.il>
5 * Copyright (C) 2003-2005 Bartlomiej Zolnierkiewicz
1da177e4 6 *
1da177e4
LT
7 * 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).
1da177e4 13 *
5ce78af4
BP
14 * For a historical changelog see
15 * Documentation/ide/ChangeLog.ide-tape.1995-2002
1da177e4
LT
16 */
17
dfe79936 18#define IDETAPE_VERSION "1.20"
1da177e4 19
1da177e4
LT
20#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>
9bae1ff3 28#include <linux/jiffies.h>
1da177e4 29#include <linux/major.h>
1da177e4
LT
30#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>
cf8b8975 38#include <linux/mutex.h>
90699ce2 39#include <scsi/scsi.h>
1da177e4
LT
40
41#include <asm/byteorder.h>
c837cfa5
BP
42#include <linux/irq.h>
43#include <linux/uaccess.h>
44#include <linux/io.h>
1da177e4 45#include <asm/unaligned.h>
1da177e4
LT
46#include <linux/mtio.h>
47
8004a8c9
BP
48enum {
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
1da177e4
LT
74/**************************** Tunable parameters *****************************/
75
76
77/*
3c98bf34 78 * Pipelined mode parameters.
1da177e4 79 *
3c98bf34
BP
80 * We try to use the minimum number of stages which is enough to keep the tape
81 * constantly streaming. To accomplish that, we implement a feedback loop around
82 * the maximum number of stages:
1da177e4 83 *
3c98bf34
BP
84 * We start from MIN maximum stages (we will not even use MIN stages if we don't
85 * need them), increment it by RATE*(MAX-MIN) whenever we sense that the
86 * pipeline is empty, until we reach the optimum value or until we reach MAX.
1da177e4
LT
87 */
88#define IDETAPE_MIN_PIPELINE_STAGES 1
89#define IDETAPE_MAX_PIPELINE_STAGES 400
90#define IDETAPE_INCREASE_STAGES_RATE 20
91
1da177e4 92/*
3c98bf34
BP
93 * After each failed packet command we issue a request sense command and retry
94 * the packet command IDETAPE_MAX_PC_RETRIES times.
1da177e4 95 *
3c98bf34 96 * Setting IDETAPE_MAX_PC_RETRIES to 0 will disable retries.
1da177e4
LT
97 */
98#define IDETAPE_MAX_PC_RETRIES 3
99
100/*
3c98bf34
BP
101 * With each packet command, we allocate a buffer of IDETAPE_PC_BUFFER_SIZE
102 * bytes. This is used for several packet commands (Not for READ/WRITE commands)
1da177e4
LT
103 */
104#define IDETAPE_PC_BUFFER_SIZE 256
105
106/*
107 * In various places in the driver, we need to allocate storage
108 * for packet commands and requests, which will remain valid while
109 * we leave the driver to wait for an interrupt or a timeout event.
110 */
111#define IDETAPE_PC_STACK (10 + IDETAPE_MAX_PC_RETRIES)
112
113/*
114 * Some drives (for example, Seagate STT3401A Travan) require a very long
115 * timeout, because they don't return an interrupt or clear their busy bit
116 * until after the command completes (even retension commands).
117 */
118#define IDETAPE_WAIT_CMD (900*HZ)
119
120/*
3c98bf34
BP
121 * The following parameter is used to select the point in the internal tape fifo
122 * in which we will start to refill the buffer. Decreasing the following
123 * parameter will improve the system's latency and interactive response, while
124 * using a high value might improve system throughput.
1da177e4 125 */
3c98bf34 126#define IDETAPE_FIFO_THRESHOLD 2
1da177e4
LT
127
128/*
3c98bf34 129 * DSC polling parameters.
1da177e4 130 *
3c98bf34
BP
131 * Polling for DSC (a single bit in the status register) is a very important
132 * function in ide-tape. There are two cases in which we poll for DSC:
1da177e4 133 *
3c98bf34
BP
134 * 1. Before a read/write packet command, to ensure that we can transfer data
135 * from/to the tape's data buffers, without causing an actual media access.
136 * In case the tape is not ready yet, we take out our request from the device
137 * request queue, so that ide.c could service requests from the other device
138 * on the same interface in the meantime.
1da177e4 139 *
3c98bf34
BP
140 * 2. After the successful initialization of a "media access packet command",
141 * which is a command that can take a long time to complete (the interval can
142 * range from several seconds to even an hour). Again, we postpone our request
143 * in the middle to free the bus for the other device. The polling frequency
144 * here should be lower than the read/write frequency since those media access
145 * commands are slow. We start from a "fast" frequency - IDETAPE_DSC_MA_FAST
146 * (1 second), and if we don't receive DSC after IDETAPE_DSC_MA_THRESHOLD
147 * (5 min), we switch it to a lower frequency - IDETAPE_DSC_MA_SLOW (1 min).
1da177e4 148 *
3c98bf34
BP
149 * We also set a timeout for the timer, in case something goes wrong. The
150 * timeout should be longer then the maximum execution time of a tape operation.
1da177e4 151 */
3c98bf34
BP
152
153/* DSC timings. */
1da177e4
LT
154#define IDETAPE_DSC_RW_MIN 5*HZ/100 /* 50 msec */
155#define IDETAPE_DSC_RW_MAX 40*HZ/100 /* 400 msec */
156#define IDETAPE_DSC_RW_TIMEOUT 2*60*HZ /* 2 minutes */
157#define IDETAPE_DSC_MA_FAST 2*HZ /* 2 seconds */
158#define IDETAPE_DSC_MA_THRESHOLD 5*60*HZ /* 5 minutes */
159#define IDETAPE_DSC_MA_SLOW 30*HZ /* 30 seconds */
160#define IDETAPE_DSC_MA_TIMEOUT 2*60*60*HZ /* 2 hours */
161
162/*************************** End of tunable parameters ***********************/
163
3c98bf34 164/* Read/Write error simulation */
1da177e4
LT
165#define SIMULATE_ERRORS 0
166
54abf37e
BP
167/* tape directions */
168enum {
169 IDETAPE_DIR_NONE = (1 << 0),
170 IDETAPE_DIR_READ = (1 << 1),
171 IDETAPE_DIR_WRITE = (1 << 2),
172};
1da177e4
LT
173
174struct idetape_bh {
ab057968 175 u32 b_size;
1da177e4
LT
176 atomic_t b_count;
177 struct idetape_bh *b_reqnext;
178 char *b_data;
179};
180
03056b90
BP
181/* Tape door status */
182#define DOOR_UNLOCKED 0
183#define DOOR_LOCKED 1
184#define DOOR_EXPLICITLY_LOCKED 2
185
186/* Some defines for the SPACE command */
187#define IDETAPE_SPACE_OVER_FILEMARK 1
188#define IDETAPE_SPACE_TO_EOD 3
189
190/* Some defines for the LOAD UNLOAD command */
191#define IDETAPE_LU_LOAD_MASK 1
192#define IDETAPE_LU_RETENSION_MASK 2
193#define IDETAPE_LU_EOT_MASK 4
194
195/*
196 * Special requests for our block device strategy routine.
197 *
198 * In order to service a character device command, we add special requests to
199 * the tail of our block device request queue and wait for their completion.
200 */
201
202enum {
203 REQ_IDETAPE_PC1 = (1 << 0), /* packet command (first stage) */
204 REQ_IDETAPE_PC2 = (1 << 1), /* packet command (second stage) */
205 REQ_IDETAPE_READ = (1 << 2),
206 REQ_IDETAPE_WRITE = (1 << 3),
207};
208
209/* Error codes returned in rq->errors to the higher part of the driver. */
210#define IDETAPE_ERROR_GENERAL 101
211#define IDETAPE_ERROR_FILEMARK 102
212#define IDETAPE_ERROR_EOD 103
213
214/* Structures related to the SELECT SENSE / MODE SENSE packet commands. */
215#define IDETAPE_BLOCK_DESCRIPTOR 0
216#define IDETAPE_CAPABILITIES_PAGE 0x2a
217
346331f8
BP
218/* Tape flag bits values. */
219enum {
220 IDETAPE_FLAG_IGNORE_DSC = (1 << 0),
221 /* 0 When the tape position is unknown */
222 IDETAPE_FLAG_ADDRESS_VALID = (1 << 1),
223 /* Device already opened */
224 IDETAPE_FLAG_BUSY = (1 << 2),
225 /* Error detected in a pipeline stage */
226 IDETAPE_FLAG_PIPELINE_ERR = (1 << 3),
227 /* Attempt to auto-detect the current user block size */
228 IDETAPE_FLAG_DETECT_BS = (1 << 4),
229 /* Currently on a filemark */
230 IDETAPE_FLAG_FILEMARK = (1 << 5),
231 /* DRQ interrupt device */
232 IDETAPE_FLAG_DRQ_INTERRUPT = (1 << 6),
233 /* pipeline active */
234 IDETAPE_FLAG_PIPELINE_ACTIVE = (1 << 7),
235 /* 0 = no tape is loaded, so we don't rewind after ejecting */
236 IDETAPE_FLAG_MEDIUM_PRESENT = (1 << 8),
237};
238
3c98bf34 239/* A pipeline stage. */
1da177e4
LT
240typedef struct idetape_stage_s {
241 struct request rq; /* The corresponding request */
242 struct idetape_bh *bh; /* The data buffers */
243 struct idetape_stage_s *next; /* Pointer to the next stage */
244} idetape_stage_t;
245
1da177e4 246/*
3c98bf34
BP
247 * Most of our global data which we need to save even as we leave the driver due
248 * to an interrupt or a timer event is stored in the struct defined below.
1da177e4
LT
249 */
250typedef struct ide_tape_obj {
251 ide_drive_t *drive;
252 ide_driver_t *driver;
253 struct gendisk *disk;
254 struct kref kref;
255
256 /*
257 * Since a typical character device operation requires more
258 * than one packet command, we provide here enough memory
259 * for the maximum of interconnected packet commands.
260 * The packet commands are stored in the circular array pc_stack.
261 * pc_stack_index points to the last used entry, and warps around
262 * to the start when we get to the last array entry.
263 *
264 * pc points to the current processed packet command.
265 *
266 * failed_pc points to the last failed packet command, or contains
267 * NULL if we do not need to retry any packet command. This is
268 * required since an additional packet command is needed before the
269 * retry, to get detailed information on what went wrong.
270 */
271 /* Current packet command */
d236d74c 272 struct ide_atapi_pc *pc;
1da177e4 273 /* Last failed packet command */
d236d74c 274 struct ide_atapi_pc *failed_pc;
1da177e4 275 /* Packet command stack */
d236d74c 276 struct ide_atapi_pc pc_stack[IDETAPE_PC_STACK];
1da177e4
LT
277 /* Next free packet command storage space */
278 int pc_stack_index;
279 struct request rq_stack[IDETAPE_PC_STACK];
280 /* We implement a circular array */
281 int rq_stack_index;
282
283 /*
3c98bf34 284 * DSC polling variables.
1da177e4 285 *
3c98bf34
BP
286 * While polling for DSC we use postponed_rq to postpone the current
287 * request so that ide.c will be able to service pending requests on the
288 * other device. Note that at most we will have only one DSC (usually
289 * data transfer) request in the device request queue. Additional
290 * requests can be queued in our internal pipeline, but they will be
291 * visible to ide.c only one at a time.
1da177e4
LT
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 */
54bb2074
BP
299 unsigned long best_dsc_rw_freq;
300 unsigned long dsc_poll_freq;
1da177e4
LT
301 unsigned long dsc_timeout;
302
3c98bf34 303 /* Read position information */
1da177e4
LT
304 u8 partition;
305 /* Current block */
54bb2074 306 unsigned int first_frame;
1da177e4 307
3c98bf34 308 /* Last error information */
1da177e4
LT
309 u8 sense_key, asc, ascq;
310
3c98bf34 311 /* Character device operation */
1da177e4
LT
312 unsigned int minor;
313 /* device name */
314 char name[4];
315 /* Current character device data transfer direction */
54abf37e 316 u8 chrdev_dir;
1da177e4 317
54bb2074
BP
318 /* tape block size, usually 512 or 1024 bytes */
319 unsigned short blk_size;
1da177e4 320 int user_bs_factor;
b6422013 321
1da177e4 322 /* Copy of the tape's Capabilities and Mechanical Page */
b6422013 323 u8 caps[20];
1da177e4
LT
324
325 /*
3c98bf34 326 * Active data transfer request parameters.
1da177e4 327 *
3c98bf34
BP
328 * At most, there is only one ide-tape originated data transfer request
329 * in the device request queue. This allows ide.c to easily service
330 * requests from the other device when we postpone our active request.
331 * In the pipelined operation mode, we use our internal pipeline
332 * structure to hold more data requests. The data buffer size is chosen
333 * based on the tape's recommendation.
1da177e4 334 */
3c98bf34 335 /* ptr to the request which is waiting in the device request queue */
54bb2074 336 struct request *active_data_rq;
3c98bf34 337 /* Data buffer size chosen based on the tape's recommendation */
1da177e4
LT
338 int stage_size;
339 idetape_stage_t *merge_stage;
340 int merge_stage_size;
341 struct idetape_bh *bh;
342 char *b_data;
343 int b_count;
3c98bf34 344
1da177e4 345 /*
3c98bf34 346 * Pipeline parameters.
1da177e4 347 *
3c98bf34
BP
348 * To accomplish non-pipelined mode, we simply set the following
349 * variables to zero (or NULL, where appropriate).
1da177e4
LT
350 */
351 /* Number of currently used stages */
352 int nr_stages;
353 /* Number of pending stages */
354 int nr_pending_stages;
355 /* We will not allocate more than this number of stages */
356 int max_stages, min_pipeline, max_pipeline;
357 /* The first stage which will be removed from the pipeline */
358 idetape_stage_t *first_stage;
359 /* The currently active stage */
360 idetape_stage_t *active_stage;
361 /* Will be serviced after the currently active request */
362 idetape_stage_t *next_stage;
363 /* New requests will be added to the pipeline here */
364 idetape_stage_t *last_stage;
1da177e4
LT
365 int pages_per_stage;
366 /* Wasted space in each stage */
367 int excess_bh_size;
368
369 /* Status/Action flags: long for set_bit */
370 unsigned long flags;
371 /* protects the ide-tape queue */
54bb2074 372 spinlock_t lock;
1da177e4 373
3c98bf34 374 /* Measures average tape speed */
1da177e4
LT
375 unsigned long avg_time;
376 int avg_size;
377 int avg_speed;
378
1da177e4
LT
379 /* the door is currently locked */
380 int door_locked;
381 /* the tape hardware is write protected */
382 char drv_write_prot;
383 /* the tape is write protected (hardware or opened as read-only) */
384 char write_prot;
385
386 /*
3c98bf34
BP
387 * Limit the number of times a request can be postponed, to avoid an
388 * infinite postpone deadlock.
1da177e4 389 */
1da177e4
LT
390 int postpone_cnt;
391
3c98bf34 392 /* Speed control at the tape buffers input/output */
1da177e4
LT
393 unsigned long insert_time;
394 int insert_size;
395 int insert_speed;
1da177e4
LT
396 int measure_insert_time;
397
8004a8c9 398 u32 debug_mask;
1da177e4
LT
399} idetape_tape_t;
400
cf8b8975 401static DEFINE_MUTEX(idetape_ref_mutex);
1da177e4 402
d5dee80a
WD
403static struct class *idetape_sysfs_class;
404
1da177e4
LT
405#define to_ide_tape(obj) container_of(obj, struct ide_tape_obj, kref)
406
407#define ide_tape_g(disk) \
408 container_of((disk)->private_data, struct ide_tape_obj, driver)
409
410static struct ide_tape_obj *ide_tape_get(struct gendisk *disk)
411{
412 struct ide_tape_obj *tape = NULL;
413
cf8b8975 414 mutex_lock(&idetape_ref_mutex);
1da177e4
LT
415 tape = ide_tape_g(disk);
416 if (tape)
417 kref_get(&tape->kref);
cf8b8975 418 mutex_unlock(&idetape_ref_mutex);
1da177e4
LT
419 return tape;
420}
421
422static void ide_tape_release(struct kref *);
423
424static void ide_tape_put(struct ide_tape_obj *tape)
425{
cf8b8975 426 mutex_lock(&idetape_ref_mutex);
1da177e4 427 kref_put(&tape->kref, ide_tape_release);
cf8b8975 428 mutex_unlock(&idetape_ref_mutex);
1da177e4
LT
429}
430
1da177e4 431/*
3c98bf34
BP
432 * The variables below are used for the character device interface. Additional
433 * state variables are defined in our ide_drive_t structure.
1da177e4 434 */
5a04cfa9 435static struct ide_tape_obj *idetape_devs[MAX_HWIFS * MAX_DRIVES];
1da177e4
LT
436
437#define ide_tape_f(file) ((file)->private_data)
438
439static struct ide_tape_obj *ide_tape_chrdev_get(unsigned int i)
440{
441 struct ide_tape_obj *tape = NULL;
442
cf8b8975 443 mutex_lock(&idetape_ref_mutex);
1da177e4
LT
444 tape = idetape_devs[i];
445 if (tape)
446 kref_get(&tape->kref);
cf8b8975 447 mutex_unlock(&idetape_ref_mutex);
1da177e4
LT
448 return tape;
449}
450
d236d74c 451static void idetape_input_buffers(ide_drive_t *drive, struct ide_atapi_pc *pc,
5a04cfa9 452 unsigned int bcount)
1da177e4
LT
453{
454 struct idetape_bh *bh = pc->bh;
455 int count;
456
457 while (bcount) {
1da177e4
LT
458 if (bh == NULL) {
459 printk(KERN_ERR "ide-tape: bh == NULL in "
460 "idetape_input_buffers\n");
7616c0ad 461 ide_atapi_discard_data(drive, bcount);
1da177e4
LT
462 return;
463 }
5a04cfa9
BP
464 count = min(
465 (unsigned int)(bh->b_size - atomic_read(&bh->b_count)),
466 bcount);
467 HWIF(drive)->atapi_input_bytes(drive, bh->b_data +
468 atomic_read(&bh->b_count), count);
1da177e4
LT
469 bcount -= count;
470 atomic_add(count, &bh->b_count);
471 if (atomic_read(&bh->b_count) == bh->b_size) {
472 bh = bh->b_reqnext;
473 if (bh)
474 atomic_set(&bh->b_count, 0);
475 }
476 }
477 pc->bh = bh;
478}
479
d236d74c 480static void idetape_output_buffers(ide_drive_t *drive, struct ide_atapi_pc *pc,
5a04cfa9 481 unsigned int bcount)
1da177e4
LT
482{
483 struct idetape_bh *bh = pc->bh;
484 int count;
485
486 while (bcount) {
1da177e4 487 if (bh == NULL) {
5a04cfa9
BP
488 printk(KERN_ERR "ide-tape: bh == NULL in %s\n",
489 __func__);
1da177e4
LT
490 return;
491 }
1da177e4
LT
492 count = min((unsigned int)pc->b_count, (unsigned int)bcount);
493 HWIF(drive)->atapi_output_bytes(drive, pc->b_data, count);
494 bcount -= count;
495 pc->b_data += count;
496 pc->b_count -= count;
497 if (!pc->b_count) {
5a04cfa9
BP
498 bh = bh->b_reqnext;
499 pc->bh = bh;
1da177e4
LT
500 if (bh) {
501 pc->b_data = bh->b_data;
502 pc->b_count = atomic_read(&bh->b_count);
503 }
504 }
505 }
506}
507
d236d74c 508static void idetape_update_buffers(struct ide_atapi_pc *pc)
1da177e4
LT
509{
510 struct idetape_bh *bh = pc->bh;
511 int count;
d236d74c 512 unsigned int bcount = pc->xferred;
1da177e4 513
346331f8 514 if (pc->flags & PC_FLAG_WRITING)
1da177e4
LT
515 return;
516 while (bcount) {
1da177e4 517 if (bh == NULL) {
5a04cfa9
BP
518 printk(KERN_ERR "ide-tape: bh == NULL in %s\n",
519 __func__);
1da177e4
LT
520 return;
521 }
1da177e4
LT
522 count = min((unsigned int)bh->b_size, (unsigned int)bcount);
523 atomic_set(&bh->b_count, count);
524 if (atomic_read(&bh->b_count) == bh->b_size)
525 bh = bh->b_reqnext;
526 bcount -= count;
527 }
528 pc->bh = bh;
529}
530
531/*
532 * idetape_next_pc_storage returns a pointer to a place in which we can
533 * safely store a packet command, even though we intend to leave the
534 * driver. A storage space for a maximum of IDETAPE_PC_STACK packet
535 * commands is allocated at initialization time.
536 */
d236d74c 537static struct ide_atapi_pc *idetape_next_pc_storage(ide_drive_t *drive)
1da177e4
LT
538{
539 idetape_tape_t *tape = drive->driver_data;
540
8004a8c9
BP
541 debug_log(DBG_PCRQ_STACK, "pc_stack_index=%d\n", tape->pc_stack_index);
542
1da177e4 543 if (tape->pc_stack_index == IDETAPE_PC_STACK)
5a04cfa9 544 tape->pc_stack_index = 0;
1da177e4
LT
545 return (&tape->pc_stack[tape->pc_stack_index++]);
546}
547
548/*
549 * idetape_next_rq_storage is used along with idetape_next_pc_storage.
550 * Since we queue packet commands in the request queue, we need to
551 * allocate a request, along with the allocation of a packet command.
552 */
5a04cfa9 553
1da177e4
LT
554/**************************************************************
555 * *
556 * This should get fixed to use kmalloc(.., GFP_ATOMIC) *
557 * followed later on by kfree(). -ml *
558 * *
559 **************************************************************/
5a04cfa9
BP
560
561static struct request *idetape_next_rq_storage(ide_drive_t *drive)
1da177e4
LT
562{
563 idetape_tape_t *tape = drive->driver_data;
564
8004a8c9
BP
565 debug_log(DBG_PCRQ_STACK, "rq_stack_index=%d\n", tape->rq_stack_index);
566
1da177e4 567 if (tape->rq_stack_index == IDETAPE_PC_STACK)
5a04cfa9 568 tape->rq_stack_index = 0;
1da177e4
LT
569 return (&tape->rq_stack[tape->rq_stack_index++]);
570}
571
d236d74c 572static void idetape_init_pc(struct ide_atapi_pc *pc)
1da177e4
LT
573{
574 memset(pc->c, 0, 12);
575 pc->retries = 0;
576 pc->flags = 0;
d236d74c
BP
577 pc->req_xfer = 0;
578 pc->buf = pc->pc_buf;
579 pc->buf_size = IDETAPE_PC_BUFFER_SIZE;
1da177e4
LT
580 pc->bh = NULL;
581 pc->b_data = NULL;
582}
583
584/*
1b5db434
BP
585 * called on each failed packet command retry to analyze the request sense. We
586 * currently do not utilize this information.
1da177e4 587 */
1b5db434 588static void idetape_analyze_error(ide_drive_t *drive, u8 *sense)
1da177e4
LT
589{
590 idetape_tape_t *tape = drive->driver_data;
d236d74c 591 struct ide_atapi_pc *pc = tape->failed_pc;
1da177e4 592
1b5db434
BP
593 tape->sense_key = sense[2] & 0xF;
594 tape->asc = sense[12];
595 tape->ascq = sense[13];
8004a8c9
BP
596
597 debug_log(DBG_ERR, "pc = %x, sense key = %x, asc = %x, ascq = %x\n",
598 pc->c[0], tape->sense_key, tape->asc, tape->ascq);
1da177e4 599
d236d74c 600 /* Correct pc->xferred by asking the tape. */
346331f8 601 if (pc->flags & PC_FLAG_DMA_ERROR) {
d236d74c 602 pc->xferred = pc->req_xfer -
54bb2074 603 tape->blk_size *
860ff5ec 604 be32_to_cpu(get_unaligned((u32 *)&sense[3]));
1da177e4
LT
605 idetape_update_buffers(pc);
606 }
607
608 /*
609 * If error was the result of a zero-length read or write command,
610 * with sense key=5, asc=0x22, ascq=0, let it slide. Some drives
611 * (i.e. Seagate STT3401A Travan) don't support 0-length read/writes.
612 */
90699ce2 613 if ((pc->c[0] == READ_6 || pc->c[0] == WRITE_6)
1b5db434
BP
614 /* length == 0 */
615 && pc->c[4] == 0 && pc->c[3] == 0 && pc->c[2] == 0) {
616 if (tape->sense_key == 5) {
1da177e4
LT
617 /* don't report an error, everything's ok */
618 pc->error = 0;
619 /* don't retry read/write */
346331f8 620 pc->flags |= PC_FLAG_ABORT;
1da177e4
LT
621 }
622 }
90699ce2 623 if (pc->c[0] == READ_6 && (sense[2] & 0x80)) {
1da177e4 624 pc->error = IDETAPE_ERROR_FILEMARK;
346331f8 625 pc->flags |= PC_FLAG_ABORT;
1da177e4 626 }
90699ce2 627 if (pc->c[0] == WRITE_6) {
1b5db434
BP
628 if ((sense[2] & 0x40) || (tape->sense_key == 0xd
629 && tape->asc == 0x0 && tape->ascq == 0x2)) {
1da177e4 630 pc->error = IDETAPE_ERROR_EOD;
346331f8 631 pc->flags |= PC_FLAG_ABORT;
1da177e4
LT
632 }
633 }
90699ce2 634 if (pc->c[0] == READ_6 || pc->c[0] == WRITE_6) {
1b5db434 635 if (tape->sense_key == 8) {
1da177e4 636 pc->error = IDETAPE_ERROR_EOD;
346331f8 637 pc->flags |= PC_FLAG_ABORT;
1da177e4 638 }
346331f8 639 if (!(pc->flags & PC_FLAG_ABORT) &&
d236d74c 640 pc->xferred)
1da177e4
LT
641 pc->retries = IDETAPE_MAX_PC_RETRIES + 1;
642 }
643}
644
419d4741 645static void idetape_activate_next_stage(ide_drive_t *drive)
1da177e4
LT
646{
647 idetape_tape_t *tape = drive->driver_data;
648 idetape_stage_t *stage = tape->next_stage;
649 struct request *rq = &stage->rq;
650
8004a8c9
BP
651 debug_log(DBG_PROCS, "Enter %s\n", __func__);
652
1da177e4 653 if (stage == NULL) {
8004a8c9
BP
654 printk(KERN_ERR "ide-tape: bug: Trying to activate a non"
655 " existing stage\n");
1da177e4
LT
656 return;
657 }
1da177e4
LT
658
659 rq->rq_disk = tape->disk;
660 rq->buffer = NULL;
661 rq->special = (void *)stage->bh;
54bb2074 662 tape->active_data_rq = rq;
1da177e4
LT
663 tape->active_stage = stage;
664 tape->next_stage = stage->next;
665}
666
3c98bf34 667/* Free a stage along with its related buffers completely. */
5a04cfa9 668static void __idetape_kfree_stage(idetape_stage_t *stage)
1da177e4
LT
669{
670 struct idetape_bh *prev_bh, *bh = stage->bh;
671 int size;
672
673 while (bh != NULL) {
674 if (bh->b_data != NULL) {
675 size = (int) bh->b_size;
676 while (size > 0) {
677 free_page((unsigned long) bh->b_data);
678 size -= PAGE_SIZE;
679 bh->b_data += PAGE_SIZE;
680 }
681 }
682 prev_bh = bh;
683 bh = bh->b_reqnext;
684 kfree(prev_bh);
685 }
686 kfree(stage);
687}
688
5a04cfa9 689static void idetape_kfree_stage(idetape_tape_t *tape, idetape_stage_t *stage)
1da177e4
LT
690{
691 __idetape_kfree_stage(stage);
692}
693
694/*
3c98bf34
BP
695 * Remove tape->first_stage from the pipeline. The caller should avoid race
696 * conditions.
1da177e4 697 */
5a04cfa9 698static void idetape_remove_stage_head(ide_drive_t *drive)
1da177e4
LT
699{
700 idetape_tape_t *tape = drive->driver_data;
701 idetape_stage_t *stage;
8004a8c9
BP
702
703 debug_log(DBG_PROCS, "Enter %s\n", __func__);
704
1da177e4
LT
705 if (tape->first_stage == NULL) {
706 printk(KERN_ERR "ide-tape: bug: tape->first_stage is NULL\n");
55a5d291 707 return;
1da177e4
LT
708 }
709 if (tape->active_stage == tape->first_stage) {
8004a8c9
BP
710 printk(KERN_ERR "ide-tape: bug: Trying to free our active "
711 "pipeline stage\n");
1da177e4
LT
712 return;
713 }
1da177e4
LT
714 stage = tape->first_stage;
715 tape->first_stage = stage->next;
716 idetape_kfree_stage(tape, stage);
717 tape->nr_stages--;
718 if (tape->first_stage == NULL) {
719 tape->last_stage = NULL;
1da177e4 720 if (tape->next_stage != NULL)
5a04cfa9
BP
721 printk(KERN_ERR "ide-tape: bug: tape->next_stage !="
722 " NULL\n");
1da177e4 723 if (tape->nr_stages)
5a04cfa9
BP
724 printk(KERN_ERR "ide-tape: bug: nr_stages should be 0 "
725 "now\n");
1da177e4
LT
726 }
727}
728
729/*
730 * This will free all the pipeline stages starting from new_last_stage->next
731 * to the end of the list, and point tape->last_stage to new_last_stage.
732 */
733static void idetape_abort_pipeline(ide_drive_t *drive,
734 idetape_stage_t *new_last_stage)
735{
736 idetape_tape_t *tape = drive->driver_data;
737 idetape_stage_t *stage = new_last_stage->next;
738 idetape_stage_t *nstage;
739
8004a8c9
BP
740 debug_log(DBG_PROCS, "%s: Enter %s\n", tape->name, __func__);
741
1da177e4
LT
742 while (stage) {
743 nstage = stage->next;
744 idetape_kfree_stage(tape, stage);
745 --tape->nr_stages;
746 --tape->nr_pending_stages;
747 stage = nstage;
748 }
749 if (new_last_stage)
750 new_last_stage->next = NULL;
751 tape->last_stage = new_last_stage;
752 tape->next_stage = NULL;
753}
754
755/*
3c98bf34
BP
756 * Finish servicing a request and insert a pending pipeline request into the
757 * main device queue.
1da177e4
LT
758 */
759static int idetape_end_request(ide_drive_t *drive, int uptodate, int nr_sects)
760{
761 struct request *rq = HWGROUP(drive)->rq;
762 idetape_tape_t *tape = drive->driver_data;
763 unsigned long flags;
764 int error;
765 int remove_stage = 0;
766 idetape_stage_t *active_stage;
767
8004a8c9 768 debug_log(DBG_PROCS, "Enter %s\n", __func__);
1da177e4
LT
769
770 switch (uptodate) {
5a04cfa9
BP
771 case 0: error = IDETAPE_ERROR_GENERAL; break;
772 case 1: error = 0; break;
773 default: error = uptodate;
1da177e4
LT
774 }
775 rq->errors = error;
776 if (error)
777 tape->failed_pc = NULL;
778
3687221f
BZ
779 if (!blk_special_request(rq)) {
780 ide_end_request(drive, uptodate, nr_sects);
781 return 0;
782 }
783
54bb2074 784 spin_lock_irqsave(&tape->lock, flags);
1da177e4
LT
785
786 /* The request was a pipelined data transfer request */
54bb2074 787 if (tape->active_data_rq == rq) {
1da177e4
LT
788 active_stage = tape->active_stage;
789 tape->active_stage = NULL;
54bb2074 790 tape->active_data_rq = NULL;
1da177e4
LT
791 tape->nr_pending_stages--;
792 if (rq->cmd[0] & REQ_IDETAPE_WRITE) {
793 remove_stage = 1;
794 if (error) {
346331f8
BP
795 set_bit(IDETAPE_FLAG_PIPELINE_ERR,
796 &tape->flags);
1da177e4 797 if (error == IDETAPE_ERROR_EOD)
5a04cfa9
BP
798 idetape_abort_pipeline(drive,
799 active_stage);
1da177e4
LT
800 }
801 } else if (rq->cmd[0] & REQ_IDETAPE_READ) {
802 if (error == IDETAPE_ERROR_EOD) {
346331f8
BP
803 set_bit(IDETAPE_FLAG_PIPELINE_ERR,
804 &tape->flags);
1da177e4
LT
805 idetape_abort_pipeline(drive, active_stage);
806 }
807 }
808 if (tape->next_stage != NULL) {
419d4741 809 idetape_activate_next_stage(drive);
1da177e4 810
3c98bf34 811 /* Insert the next request into the request queue. */
54bb2074
BP
812 (void)ide_do_drive_cmd(drive, tape->active_data_rq,
813 ide_end);
1da177e4 814 } else if (!error) {
97219851
BP
815 /*
816 * This is a part of the feedback loop which tries to
817 * find the optimum number of stages. We are starting
818 * from a minimum maximum number of stages, and if we
819 * sense that the pipeline is empty, we try to increase
820 * it, until we reach the user compile time memory
821 * limit.
822 */
823 int i = (tape->max_pipeline - tape->min_pipeline) / 10;
824
825 tape->max_stages += max(i, 1);
826 tape->max_stages = max(tape->max_stages,
827 tape->min_pipeline);
828 tape->max_stages = min(tape->max_stages,
829 tape->max_pipeline);
1da177e4
LT
830 }
831 }
832 ide_end_drive_cmd(drive, 0, 0);
1da177e4
LT
833
834 if (remove_stage)
835 idetape_remove_stage_head(drive);
54bb2074 836 if (tape->active_data_rq == NULL)
346331f8 837 clear_bit(IDETAPE_FLAG_PIPELINE_ACTIVE, &tape->flags);
54bb2074 838 spin_unlock_irqrestore(&tape->lock, flags);
1da177e4
LT
839 return 0;
840}
841
5a04cfa9 842static ide_startstop_t idetape_request_sense_callback(ide_drive_t *drive)
1da177e4
LT
843{
844 idetape_tape_t *tape = drive->driver_data;
845
8004a8c9
BP
846 debug_log(DBG_PROCS, "Enter %s\n", __func__);
847
1da177e4 848 if (!tape->pc->error) {
d236d74c 849 idetape_analyze_error(drive, tape->pc->buf);
1da177e4
LT
850 idetape_end_request(drive, 1, 0);
851 } else {
5a04cfa9
BP
852 printk(KERN_ERR "ide-tape: Error in REQUEST SENSE itself - "
853 "Aborting request!\n");
1da177e4
LT
854 idetape_end_request(drive, 0, 0);
855 }
856 return ide_stopped;
857}
858
d236d74c 859static void idetape_create_request_sense_cmd(struct ide_atapi_pc *pc)
1da177e4 860{
5a04cfa9 861 idetape_init_pc(pc);
90699ce2 862 pc->c[0] = REQUEST_SENSE;
1da177e4 863 pc->c[4] = 20;
d236d74c
BP
864 pc->req_xfer = 20;
865 pc->idetape_callback = &idetape_request_sense_callback;
1da177e4
LT
866}
867
868static void idetape_init_rq(struct request *rq, u8 cmd)
869{
870 memset(rq, 0, sizeof(*rq));
4aff5e23 871 rq->cmd_type = REQ_TYPE_SPECIAL;
1da177e4
LT
872 rq->cmd[0] = cmd;
873}
874
875/*
3c98bf34
BP
876 * Generate a new packet command request in front of the request queue, before
877 * the current request, so that it will be processed immediately, on the next
878 * pass through the driver. The function below is called from the request
879 * handling part of the driver (the "bottom" part). Safe storage for the request
880 * should be allocated with ide_tape_next_{pc,rq}_storage() prior to that.
1da177e4 881 *
3c98bf34
BP
882 * Memory for those requests is pre-allocated at initialization time, and is
883 * limited to IDETAPE_PC_STACK requests. We assume that we have enough space for
884 * the maximum possible number of inter-dependent packet commands.
1da177e4 885 *
3c98bf34
BP
886 * The higher level of the driver - The ioctl handler and the character device
887 * handling functions should queue request to the lower level part and wait for
888 * their completion using idetape_queue_pc_tail or idetape_queue_rw_tail.
1da177e4 889 */
d236d74c 890static void idetape_queue_pc_head(ide_drive_t *drive, struct ide_atapi_pc *pc,
5a04cfa9 891 struct request *rq)
1da177e4
LT
892{
893 struct ide_tape_obj *tape = drive->driver_data;
894
895 idetape_init_rq(rq, REQ_IDETAPE_PC1);
896 rq->buffer = (char *) pc;
897 rq->rq_disk = tape->disk;
898 (void) ide_do_drive_cmd(drive, rq, ide_preempt);
899}
900
901/*
902 * idetape_retry_pc is called when an error was detected during the
903 * last packet command. We queue a request sense packet command in
904 * the head of the request list.
905 */
906static ide_startstop_t idetape_retry_pc (ide_drive_t *drive)
907{
908 idetape_tape_t *tape = drive->driver_data;
d236d74c 909 struct ide_atapi_pc *pc;
1da177e4 910 struct request *rq;
1da177e4 911
64a57fe4 912 (void)ide_read_error(drive);
1da177e4
LT
913 pc = idetape_next_pc_storage(drive);
914 rq = idetape_next_rq_storage(drive);
915 idetape_create_request_sense_cmd(pc);
346331f8 916 set_bit(IDETAPE_FLAG_IGNORE_DSC, &tape->flags);
1da177e4
LT
917 idetape_queue_pc_head(drive, pc, rq);
918 return ide_stopped;
919}
920
921/*
3c98bf34
BP
922 * Postpone the current request so that ide.c will be able to service requests
923 * from another device on the same hwgroup while we are polling for DSC.
1da177e4 924 */
5a04cfa9 925static void idetape_postpone_request(ide_drive_t *drive)
1da177e4
LT
926{
927 idetape_tape_t *tape = drive->driver_data;
928
8004a8c9
BP
929 debug_log(DBG_PROCS, "Enter %s\n", __func__);
930
1da177e4 931 tape->postponed_rq = HWGROUP(drive)->rq;
54bb2074 932 ide_stall_queue(drive, tape->dsc_poll_freq);
1da177e4
LT
933}
934
d236d74c 935typedef void idetape_io_buf(ide_drive_t *, struct ide_atapi_pc *, unsigned int);
a1efc85f 936
1da177e4 937/*
a1efc85f
BP
938 * This is the usual interrupt handler which will be called during a packet
939 * command. We will transfer some of the data (as requested by the drive) and
940 * will re-point interrupt handler to us. When data transfer is finished, we
941 * will act according to the algorithm described before
8d06bfad 942 * idetape_issue_pc.
1da177e4 943 */
a1efc85f 944static ide_startstop_t idetape_pc_intr(ide_drive_t *drive)
1da177e4
LT
945{
946 ide_hwif_t *hwif = drive->hwif;
947 idetape_tape_t *tape = drive->driver_data;
d236d74c 948 struct ide_atapi_pc *pc = tape->pc;
a1efc85f
BP
949 xfer_func_t *xferfunc;
950 idetape_io_buf *iobuf;
1da177e4
LT
951 unsigned int temp;
952#if SIMULATE_ERRORS
5a04cfa9 953 static int error_sim_count;
1da177e4 954#endif
790d1239 955 u16 bcount;
8e7657ae 956 u8 stat, ireason;
1da177e4 957
8004a8c9 958 debug_log(DBG_PROCS, "Enter %s - interrupt handler\n", __func__);
1da177e4
LT
959
960 /* Clear the interrupt */
c47137a9 961 stat = ide_read_status(drive);
1da177e4 962
346331f8 963 if (pc->flags & PC_FLAG_DMA_IN_PROGRESS) {
5e37bdc0 964 if (hwif->dma_ops->dma_end(drive) || (stat & ERR_STAT)) {
1da177e4
LT
965 /*
966 * A DMA error is sometimes expected. For example,
967 * if the tape is crossing a filemark during a
968 * READ command, it will issue an irq and position
969 * itself before the filemark, so that only a partial
970 * data transfer will occur (which causes the DMA
971 * error). In that case, we will later ask the tape
972 * how much bytes of the original request were
973 * actually transferred (we can't receive that
974 * information from the DMA engine on most chipsets).
975 */
976
977 /*
978 * On the contrary, a DMA error is never expected;
979 * it usually indicates a hardware error or abort.
980 * If the tape crosses a filemark during a READ
981 * command, it will issue an irq and position itself
982 * after the filemark (not before). Only a partial
983 * data transfer will occur, but no DMA error.
984 * (AS, 19 Apr 2001)
985 */
346331f8 986 pc->flags |= PC_FLAG_DMA_ERROR;
1da177e4 987 } else {
d236d74c 988 pc->xferred = pc->req_xfer;
1da177e4
LT
989 idetape_update_buffers(pc);
990 }
8004a8c9
BP
991 debug_log(DBG_PROCS, "DMA finished\n");
992
1da177e4
LT
993 }
994
995 /* No more interrupts */
22c525b9 996 if ((stat & DRQ_STAT) == 0) {
8004a8c9 997 debug_log(DBG_SENSE, "Packet command completed, %d bytes"
d236d74c 998 " transferred\n", pc->xferred);
1da177e4 999
346331f8 1000 pc->flags &= ~PC_FLAG_DMA_IN_PROGRESS;
1da177e4
LT
1001 local_irq_enable();
1002
1003#if SIMULATE_ERRORS
90699ce2 1004 if ((pc->c[0] == WRITE_6 || pc->c[0] == READ_6) &&
1da177e4
LT
1005 (++error_sim_count % 100) == 0) {
1006 printk(KERN_INFO "ide-tape: %s: simulating error\n",
1007 tape->name);
22c525b9 1008 stat |= ERR_STAT;
1da177e4
LT
1009 }
1010#endif
90699ce2 1011 if ((stat & ERR_STAT) && pc->c[0] == REQUEST_SENSE)
22c525b9 1012 stat &= ~ERR_STAT;
346331f8 1013 if ((stat & ERR_STAT) || (pc->flags & PC_FLAG_DMA_ERROR)) {
22c525b9 1014 /* Error detected */
8004a8c9
BP
1015 debug_log(DBG_ERR, "%s: I/O error\n", tape->name);
1016
90699ce2 1017 if (pc->c[0] == REQUEST_SENSE) {
a1efc85f
BP
1018 printk(KERN_ERR "ide-tape: I/O error in request"
1019 " sense command\n");
1da177e4
LT
1020 return ide_do_reset(drive);
1021 }
8004a8c9
BP
1022 debug_log(DBG_ERR, "[cmd %x]: check condition\n",
1023 pc->c[0]);
1024
1da177e4
LT
1025 /* Retry operation */
1026 return idetape_retry_pc(drive);
1027 }
1028 pc->error = 0;
346331f8 1029 if ((pc->flags & PC_FLAG_WAIT_FOR_DSC) &&
22c525b9 1030 (stat & SEEK_STAT) == 0) {
1da177e4
LT
1031 /* Media access command */
1032 tape->dsc_polling_start = jiffies;
54bb2074 1033 tape->dsc_poll_freq = IDETAPE_DSC_MA_FAST;
1da177e4
LT
1034 tape->dsc_timeout = jiffies + IDETAPE_DSC_MA_TIMEOUT;
1035 /* Allow ide.c to handle other requests */
1036 idetape_postpone_request(drive);
1037 return ide_stopped;
1038 }
1039 if (tape->failed_pc == pc)
1040 tape->failed_pc = NULL;
1041 /* Command finished - Call the callback function */
d236d74c 1042 return pc->idetape_callback(drive);
1da177e4 1043 }
346331f8
BP
1044
1045 if (pc->flags & PC_FLAG_DMA_IN_PROGRESS) {
1046 pc->flags &= ~PC_FLAG_DMA_IN_PROGRESS;
1da177e4
LT
1047 printk(KERN_ERR "ide-tape: The tape wants to issue more "
1048 "interrupts in DMA mode\n");
1049 printk(KERN_ERR "ide-tape: DMA disabled, reverting to PIO\n");
7469aaf6 1050 ide_dma_off(drive);
1da177e4
LT
1051 return ide_do_reset(drive);
1052 }
1053 /* Get the number of bytes to transfer on this interrupt. */
23579a2a
BZ
1054 bcount = (hwif->INB(hwif->io_ports[IDE_BCOUNTH_OFFSET]) << 8) |
1055 hwif->INB(hwif->io_ports[IDE_BCOUNTL_OFFSET]);
1da177e4 1056
23579a2a 1057 ireason = hwif->INB(hwif->io_ports[IDE_IREASON_OFFSET]);
1da177e4 1058
8e7657ae 1059 if (ireason & CD) {
a1efc85f 1060 printk(KERN_ERR "ide-tape: CoD != 0 in %s\n", __func__);
1da177e4
LT
1061 return ide_do_reset(drive);
1062 }
346331f8 1063 if (((ireason & IO) == IO) == !!(pc->flags & PC_FLAG_WRITING)) {
1da177e4
LT
1064 /* Hopefully, we will never get here */
1065 printk(KERN_ERR "ide-tape: We wanted to %s, ",
8e7657ae 1066 (ireason & IO) ? "Write" : "Read");
1da177e4 1067 printk(KERN_ERR "ide-tape: but the tape wants us to %s !\n",
8e7657ae 1068 (ireason & IO) ? "Read" : "Write");
1da177e4
LT
1069 return ide_do_reset(drive);
1070 }
346331f8 1071 if (!(pc->flags & PC_FLAG_WRITING)) {
1da177e4 1072 /* Reading - Check that we have enough space */
d236d74c
BP
1073 temp = pc->xferred + bcount;
1074 if (temp > pc->req_xfer) {
1075 if (temp > pc->buf_size) {
a1efc85f
BP
1076 printk(KERN_ERR "ide-tape: The tape wants to "
1077 "send us more data than expected "
1078 "- discarding data\n");
7616c0ad 1079 ide_atapi_discard_data(drive, bcount);
a1efc85f
BP
1080 ide_set_handler(drive, &idetape_pc_intr,
1081 IDETAPE_WAIT_CMD, NULL);
1da177e4
LT
1082 return ide_started;
1083 }
8004a8c9
BP
1084 debug_log(DBG_SENSE, "The tape wants to send us more "
1085 "data than expected - allowing transfer\n");
1da177e4 1086 }
a1efc85f
BP
1087 iobuf = &idetape_input_buffers;
1088 xferfunc = hwif->atapi_input_bytes;
1da177e4 1089 } else {
a1efc85f
BP
1090 iobuf = &idetape_output_buffers;
1091 xferfunc = hwif->atapi_output_bytes;
1da177e4 1092 }
a1efc85f
BP
1093
1094 if (pc->bh)
1095 iobuf(drive, pc, bcount);
1096 else
d236d74c 1097 xferfunc(drive, pc->cur_pos, bcount);
a1efc85f 1098
1da177e4 1099 /* Update the current position */
d236d74c
BP
1100 pc->xferred += bcount;
1101 pc->cur_pos += bcount;
8004a8c9
BP
1102
1103 debug_log(DBG_SENSE, "[cmd %x] transferred %d bytes on that intr.\n",
1104 pc->c[0], bcount);
1105
1da177e4
LT
1106 /* And set the interrupt handler again */
1107 ide_set_handler(drive, &idetape_pc_intr, IDETAPE_WAIT_CMD, NULL);
1108 return ide_started;
1109}
1110
1111/*
3c98bf34 1112 * Packet Command Interface
1da177e4 1113 *
3c98bf34
BP
1114 * The current Packet Command is available in tape->pc, and will not change
1115 * until we finish handling it. Each packet command is associated with a
1116 * callback function that will be called when the command is finished.
1da177e4 1117 *
3c98bf34 1118 * The handling will be done in three stages:
1da177e4 1119 *
3c98bf34
BP
1120 * 1. idetape_issue_pc will send the packet command to the drive, and will set
1121 * the interrupt handler to idetape_pc_intr.
1da177e4 1122 *
3c98bf34
BP
1123 * 2. On each interrupt, idetape_pc_intr will be called. This step will be
1124 * repeated until the device signals us that no more interrupts will be issued.
1da177e4 1125 *
3c98bf34
BP
1126 * 3. ATAPI Tape media access commands have immediate status with a delayed
1127 * process. In case of a successful initiation of a media access packet command,
1128 * the DSC bit will be set when the actual execution of the command is finished.
1129 * Since the tape drive will not issue an interrupt, we have to poll for this
1130 * event. In this case, we define the request as "low priority request" by
1131 * setting rq_status to IDETAPE_RQ_POSTPONED, set a timer to poll for DSC and
1132 * exit the driver.
1da177e4 1133 *
3c98bf34
BP
1134 * ide.c will then give higher priority to requests which originate from the
1135 * other device, until will change rq_status to RQ_ACTIVE.
1da177e4 1136 *
3c98bf34 1137 * 4. When the packet command is finished, it will be checked for errors.
1da177e4 1138 *
3c98bf34
BP
1139 * 5. In case an error was found, we queue a request sense packet command in
1140 * front of the request queue and retry the operation up to
1141 * IDETAPE_MAX_PC_RETRIES times.
1da177e4 1142 *
3c98bf34
BP
1143 * 6. In case no error was found, or we decided to give up and not to retry
1144 * again, the callback function will be called and then we will handle the next
1145 * request.
1da177e4
LT
1146 */
1147static ide_startstop_t idetape_transfer_pc(ide_drive_t *drive)
1148{
1149 ide_hwif_t *hwif = drive->hwif;
1150 idetape_tape_t *tape = drive->driver_data;
d236d74c 1151 struct ide_atapi_pc *pc = tape->pc;
1da177e4
LT
1152 int retries = 100;
1153 ide_startstop_t startstop;
8e7657ae 1154 u8 ireason;
1da177e4 1155
5a04cfa9
BP
1156 if (ide_wait_stat(&startstop, drive, DRQ_STAT, BUSY_STAT, WAIT_READY)) {
1157 printk(KERN_ERR "ide-tape: Strange, packet command initiated "
1158 "yet DRQ isn't asserted\n");
1da177e4
LT
1159 return startstop;
1160 }
23579a2a 1161 ireason = hwif->INB(hwif->io_ports[IDE_IREASON_OFFSET]);
8e7657ae 1162 while (retries-- && ((ireason & CD) == 0 || (ireason & IO))) {
1da177e4
LT
1163 printk(KERN_ERR "ide-tape: (IO,CoD != (0,1) while issuing "
1164 "a packet command, retrying\n");
1165 udelay(100);
23579a2a 1166 ireason = hwif->INB(hwif->io_ports[IDE_IREASON_OFFSET]);
1da177e4
LT
1167 if (retries == 0) {
1168 printk(KERN_ERR "ide-tape: (IO,CoD != (0,1) while "
1169 "issuing a packet command, ignoring\n");
8e7657ae
BZ
1170 ireason |= CD;
1171 ireason &= ~IO;
1da177e4
LT
1172 }
1173 }
8e7657ae 1174 if ((ireason & CD) == 0 || (ireason & IO)) {
1da177e4
LT
1175 printk(KERN_ERR "ide-tape: (IO,CoD) != (0,1) while issuing "
1176 "a packet command\n");
1177 return ide_do_reset(drive);
1178 }
1179 /* Set the interrupt routine */
1180 ide_set_handler(drive, &idetape_pc_intr, IDETAPE_WAIT_CMD, NULL);
1181#ifdef CONFIG_BLK_DEV_IDEDMA
1182 /* Begin DMA, if necessary */
346331f8 1183 if (pc->flags & PC_FLAG_DMA_IN_PROGRESS)
5e37bdc0 1184 hwif->dma_ops->dma_start(drive);
1da177e4
LT
1185#endif
1186 /* Send the actual packet */
1187 HWIF(drive)->atapi_output_bytes(drive, pc->c, 12);
1188 return ide_started;
1189}
1190
d236d74c
BP
1191static ide_startstop_t idetape_issue_pc(ide_drive_t *drive,
1192 struct ide_atapi_pc *pc)
1da177e4
LT
1193{
1194 ide_hwif_t *hwif = drive->hwif;
1195 idetape_tape_t *tape = drive->driver_data;
1da177e4 1196 int dma_ok = 0;
790d1239 1197 u16 bcount;
1da177e4 1198
90699ce2
BP
1199 if (tape->pc->c[0] == REQUEST_SENSE &&
1200 pc->c[0] == REQUEST_SENSE) {
1da177e4
LT
1201 printk(KERN_ERR "ide-tape: possible ide-tape.c bug - "
1202 "Two request sense in serial were issued\n");
1203 }
1da177e4 1204
90699ce2 1205 if (tape->failed_pc == NULL && pc->c[0] != REQUEST_SENSE)
1da177e4
LT
1206 tape->failed_pc = pc;
1207 /* Set the current packet command */
1208 tape->pc = pc;
1209
1210 if (pc->retries > IDETAPE_MAX_PC_RETRIES ||
346331f8 1211 (pc->flags & PC_FLAG_ABORT)) {
1da177e4 1212 /*
3c98bf34
BP
1213 * We will "abort" retrying a packet command in case legitimate
1214 * error code was received (crossing a filemark, or end of the
1215 * media, for example).
1da177e4 1216 */
346331f8 1217 if (!(pc->flags & PC_FLAG_ABORT)) {
90699ce2 1218 if (!(pc->c[0] == TEST_UNIT_READY &&
1da177e4
LT
1219 tape->sense_key == 2 && tape->asc == 4 &&
1220 (tape->ascq == 1 || tape->ascq == 8))) {
1221 printk(KERN_ERR "ide-tape: %s: I/O error, "
1222 "pc = %2x, key = %2x, "
1223 "asc = %2x, ascq = %2x\n",
1224 tape->name, pc->c[0],
1225 tape->sense_key, tape->asc,
1226 tape->ascq);
1227 }
1228 /* Giving up */
1229 pc->error = IDETAPE_ERROR_GENERAL;
1230 }
1231 tape->failed_pc = NULL;
d236d74c 1232 return pc->idetape_callback(drive);
1da177e4 1233 }
8004a8c9 1234 debug_log(DBG_SENSE, "Retry #%d, cmd = %02X\n", pc->retries, pc->c[0]);
1da177e4
LT
1235
1236 pc->retries++;
1237 /* We haven't transferred any data yet */
d236d74c
BP
1238 pc->xferred = 0;
1239 pc->cur_pos = pc->buf;
1da177e4 1240 /* Request to transfer the entire buffer at once */
d236d74c 1241 bcount = pc->req_xfer;
1da177e4 1242
346331f8
BP
1243 if (pc->flags & PC_FLAG_DMA_ERROR) {
1244 pc->flags &= ~PC_FLAG_DMA_ERROR;
1da177e4
LT
1245 printk(KERN_WARNING "ide-tape: DMA disabled, "
1246 "reverting to PIO\n");
7469aaf6 1247 ide_dma_off(drive);
1da177e4 1248 }
346331f8 1249 if ((pc->flags & PC_FLAG_DMA_RECOMMENDED) && drive->using_dma)
5e37bdc0 1250 dma_ok = !hwif->dma_ops->dma_setup(drive);
1da177e4 1251
2fc57388
BZ
1252 ide_pktcmd_tf_load(drive, IDE_TFLAG_NO_SELECT_MASK |
1253 IDE_TFLAG_OUT_DEVICE, bcount, dma_ok);
1254
346331f8
BP
1255 if (dma_ok)
1256 /* Will begin DMA later */
1257 pc->flags |= PC_FLAG_DMA_IN_PROGRESS;
1258 if (test_bit(IDETAPE_FLAG_DRQ_INTERRUPT, &tape->flags)) {
c1c9dbc8
BZ
1259 ide_execute_command(drive, WIN_PACKETCMD, &idetape_transfer_pc,
1260 IDETAPE_WAIT_CMD, NULL);
1da177e4
LT
1261 return ide_started;
1262 } else {
23579a2a 1263 hwif->OUTB(WIN_PACKETCMD, hwif->io_ports[IDE_COMMAND_OFFSET]);
1da177e4
LT
1264 return idetape_transfer_pc(drive);
1265 }
1266}
1267
5a04cfa9 1268static ide_startstop_t idetape_pc_callback(ide_drive_t *drive)
1da177e4
LT
1269{
1270 idetape_tape_t *tape = drive->driver_data;
8004a8c9
BP
1271
1272 debug_log(DBG_PROCS, "Enter %s\n", __func__);
1da177e4
LT
1273
1274 idetape_end_request(drive, tape->pc->error ? 0 : 1, 0);
1275 return ide_stopped;
1276}
1277
3c98bf34 1278/* A mode sense command is used to "sense" tape parameters. */
d236d74c 1279static void idetape_create_mode_sense_cmd(struct ide_atapi_pc *pc, u8 page_code)
1da177e4
LT
1280{
1281 idetape_init_pc(pc);
90699ce2 1282 pc->c[0] = MODE_SENSE;
1da177e4 1283 if (page_code != IDETAPE_BLOCK_DESCRIPTOR)
3c98bf34
BP
1284 /* DBD = 1 - Don't return block descriptors */
1285 pc->c[1] = 8;
1da177e4
LT
1286 pc->c[2] = page_code;
1287 /*
1288 * Changed pc->c[3] to 0 (255 will at best return unused info).
1289 *
1290 * For SCSI this byte is defined as subpage instead of high byte
1291 * of length and some IDE drives seem to interpret it this way
1292 * and return an error when 255 is used.
1293 */
1294 pc->c[3] = 0;
3c98bf34
BP
1295 /* We will just discard data in that case */
1296 pc->c[4] = 255;
1da177e4 1297 if (page_code == IDETAPE_BLOCK_DESCRIPTOR)
d236d74c 1298 pc->req_xfer = 12;
1da177e4 1299 else if (page_code == IDETAPE_CAPABILITIES_PAGE)
d236d74c 1300 pc->req_xfer = 24;
1da177e4 1301 else
d236d74c
BP
1302 pc->req_xfer = 50;
1303 pc->idetape_callback = &idetape_pc_callback;
1da177e4
LT
1304}
1305
5a04cfa9 1306static ide_startstop_t idetape_media_access_finished(ide_drive_t *drive)
1da177e4
LT
1307{
1308 idetape_tape_t *tape = drive->driver_data;
d236d74c 1309 struct ide_atapi_pc *pc = tape->pc;
22c525b9 1310 u8 stat;
1da177e4 1311
c47137a9
BZ
1312 stat = ide_read_status(drive);
1313
22c525b9
BZ
1314 if (stat & SEEK_STAT) {
1315 if (stat & ERR_STAT) {
1da177e4 1316 /* Error detected */
90699ce2 1317 if (pc->c[0] != TEST_UNIT_READY)
1da177e4
LT
1318 printk(KERN_ERR "ide-tape: %s: I/O error, ",
1319 tape->name);
1320 /* Retry operation */
1321 return idetape_retry_pc(drive);
1322 }
1323 pc->error = 0;
1324 if (tape->failed_pc == pc)
1325 tape->failed_pc = NULL;
1326 } else {
1327 pc->error = IDETAPE_ERROR_GENERAL;
1328 tape->failed_pc = NULL;
1329 }
d236d74c 1330 return pc->idetape_callback(drive);
1da177e4
LT
1331}
1332
5a04cfa9 1333static ide_startstop_t idetape_rw_callback(ide_drive_t *drive)
1da177e4
LT
1334{
1335 idetape_tape_t *tape = drive->driver_data;
1336 struct request *rq = HWGROUP(drive)->rq;
d236d74c 1337 int blocks = tape->pc->xferred / tape->blk_size;
1da177e4 1338
54bb2074
BP
1339 tape->avg_size += blocks * tape->blk_size;
1340 tape->insert_size += blocks * tape->blk_size;
1da177e4
LT
1341 if (tape->insert_size > 1024 * 1024)
1342 tape->measure_insert_time = 1;
1343 if (tape->measure_insert_time) {
1344 tape->measure_insert_time = 0;
1345 tape->insert_time = jiffies;
1346 tape->insert_size = 0;
1347 }
1348 if (time_after(jiffies, tape->insert_time))
5a04cfa9
BP
1349 tape->insert_speed = tape->insert_size / 1024 * HZ /
1350 (jiffies - tape->insert_time);
9bae1ff3 1351 if (time_after_eq(jiffies, tape->avg_time + HZ)) {
5a04cfa9
BP
1352 tape->avg_speed = tape->avg_size * HZ /
1353 (jiffies - tape->avg_time) / 1024;
1da177e4
LT
1354 tape->avg_size = 0;
1355 tape->avg_time = jiffies;
1356 }
8004a8c9 1357 debug_log(DBG_PROCS, "Enter %s\n", __func__);
1da177e4 1358
54bb2074 1359 tape->first_frame += blocks;
1da177e4
LT
1360 rq->current_nr_sectors -= blocks;
1361
1362 if (!tape->pc->error)
1363 idetape_end_request(drive, 1, 0);
1364 else
1365 idetape_end_request(drive, tape->pc->error, 0);
1366 return ide_stopped;
1367}
1368
d236d74c
BP
1369static void idetape_create_read_cmd(idetape_tape_t *tape,
1370 struct ide_atapi_pc *pc,
5a04cfa9 1371 unsigned int length, struct idetape_bh *bh)
1da177e4
LT
1372{
1373 idetape_init_pc(pc);
90699ce2 1374 pc->c[0] = READ_6;
860ff5ec 1375 put_unaligned(cpu_to_be32(length), (unsigned int *) &pc->c[1]);
1da177e4 1376 pc->c[1] = 1;
d236d74c 1377 pc->idetape_callback = &idetape_rw_callback;
1da177e4
LT
1378 pc->bh = bh;
1379 atomic_set(&bh->b_count, 0);
d236d74c
BP
1380 pc->buf = NULL;
1381 pc->buf_size = length * tape->blk_size;
1382 pc->req_xfer = pc->buf_size;
1383 if (pc->req_xfer == tape->stage_size)
346331f8 1384 pc->flags |= PC_FLAG_DMA_RECOMMENDED;
1da177e4
LT
1385}
1386
d236d74c
BP
1387static void idetape_create_write_cmd(idetape_tape_t *tape,
1388 struct ide_atapi_pc *pc,
5a04cfa9 1389 unsigned int length, struct idetape_bh *bh)
1da177e4
LT
1390{
1391 idetape_init_pc(pc);
90699ce2 1392 pc->c[0] = WRITE_6;
860ff5ec 1393 put_unaligned(cpu_to_be32(length), (unsigned int *) &pc->c[1]);
1da177e4 1394 pc->c[1] = 1;
d236d74c 1395 pc->idetape_callback = &idetape_rw_callback;
346331f8 1396 pc->flags |= PC_FLAG_WRITING;
1da177e4
LT
1397 pc->bh = bh;
1398 pc->b_data = bh->b_data;
1399 pc->b_count = atomic_read(&bh->b_count);
d236d74c
BP
1400 pc->buf = NULL;
1401 pc->buf_size = length * tape->blk_size;
1402 pc->req_xfer = pc->buf_size;
1403 if (pc->req_xfer == tape->stage_size)
346331f8 1404 pc->flags |= PC_FLAG_DMA_RECOMMENDED;
1da177e4
LT
1405}
1406
1da177e4
LT
1407static ide_startstop_t idetape_do_request(ide_drive_t *drive,
1408 struct request *rq, sector_t block)
1409{
1410 idetape_tape_t *tape = drive->driver_data;
d236d74c 1411 struct ide_atapi_pc *pc = NULL;
1da177e4 1412 struct request *postponed_rq = tape->postponed_rq;
22c525b9 1413 u8 stat;
1da177e4 1414
8004a8c9
BP
1415 debug_log(DBG_SENSE, "sector: %ld, nr_sectors: %ld,"
1416 " current_nr_sectors: %d\n",
1da177e4 1417 rq->sector, rq->nr_sectors, rq->current_nr_sectors);
1da177e4 1418
4aff5e23 1419 if (!blk_special_request(rq)) {
3c98bf34 1420 /* We do not support buffer cache originated requests. */
1da177e4 1421 printk(KERN_NOTICE "ide-tape: %s: Unsupported request in "
4aff5e23 1422 "request queue (%d)\n", drive->name, rq->cmd_type);
1da177e4
LT
1423 ide_end_request(drive, 0, 0);
1424 return ide_stopped;
1425 }
1426
3c98bf34 1427 /* Retry a failed packet command */
5a04cfa9 1428 if (tape->failed_pc && tape->pc->c[0] == REQUEST_SENSE)
8d06bfad 1429 return idetape_issue_pc(drive, tape->failed_pc);
5a04cfa9 1430
1da177e4
LT
1431 if (postponed_rq != NULL)
1432 if (rq != postponed_rq) {
1433 printk(KERN_ERR "ide-tape: ide-tape.c bug - "
1434 "Two DSC requests were queued\n");
1435 idetape_end_request(drive, 0, 0);
1436 return ide_stopped;
1437 }
1da177e4
LT
1438
1439 tape->postponed_rq = NULL;
1440
1441 /*
1442 * If the tape is still busy, postpone our request and service
1443 * the other device meanwhile.
1444 */
c47137a9 1445 stat = ide_read_status(drive);
1da177e4
LT
1446
1447 if (!drive->dsc_overlap && !(rq->cmd[0] & REQ_IDETAPE_PC2))
346331f8 1448 set_bit(IDETAPE_FLAG_IGNORE_DSC, &tape->flags);
1da177e4
LT
1449
1450 if (drive->post_reset == 1) {
346331f8 1451 set_bit(IDETAPE_FLAG_IGNORE_DSC, &tape->flags);
1da177e4
LT
1452 drive->post_reset = 0;
1453 }
1454
1da177e4 1455 if (time_after(jiffies, tape->insert_time))
5a04cfa9
BP
1456 tape->insert_speed = tape->insert_size / 1024 * HZ /
1457 (jiffies - tape->insert_time);
346331f8 1458 if (!test_and_clear_bit(IDETAPE_FLAG_IGNORE_DSC, &tape->flags) &&
22c525b9 1459 (stat & SEEK_STAT) == 0) {
1da177e4
LT
1460 if (postponed_rq == NULL) {
1461 tape->dsc_polling_start = jiffies;
54bb2074 1462 tape->dsc_poll_freq = tape->best_dsc_rw_freq;
1da177e4
LT
1463 tape->dsc_timeout = jiffies + IDETAPE_DSC_RW_TIMEOUT;
1464 } else if (time_after(jiffies, tape->dsc_timeout)) {
1465 printk(KERN_ERR "ide-tape: %s: DSC timeout\n",
1466 tape->name);
1467 if (rq->cmd[0] & REQ_IDETAPE_PC2) {
1468 idetape_media_access_finished(drive);
1469 return ide_stopped;
1470 } else {
1471 return ide_do_reset(drive);
1472 }
5a04cfa9
BP
1473 } else if (time_after(jiffies,
1474 tape->dsc_polling_start +
1475 IDETAPE_DSC_MA_THRESHOLD))
54bb2074 1476 tape->dsc_poll_freq = IDETAPE_DSC_MA_SLOW;
1da177e4
LT
1477 idetape_postpone_request(drive);
1478 return ide_stopped;
1479 }
1480 if (rq->cmd[0] & REQ_IDETAPE_READ) {
1da177e4
LT
1481 tape->postpone_cnt = 0;
1482 pc = idetape_next_pc_storage(drive);
5a04cfa9
BP
1483 idetape_create_read_cmd(tape, pc, rq->current_nr_sectors,
1484 (struct idetape_bh *)rq->special);
1da177e4
LT
1485 goto out;
1486 }
1487 if (rq->cmd[0] & REQ_IDETAPE_WRITE) {
1da177e4
LT
1488 tape->postpone_cnt = 0;
1489 pc = idetape_next_pc_storage(drive);
5a04cfa9
BP
1490 idetape_create_write_cmd(tape, pc, rq->current_nr_sectors,
1491 (struct idetape_bh *)rq->special);
1da177e4
LT
1492 goto out;
1493 }
1da177e4 1494 if (rq->cmd[0] & REQ_IDETAPE_PC1) {
d236d74c 1495 pc = (struct ide_atapi_pc *) rq->buffer;
1da177e4
LT
1496 rq->cmd[0] &= ~(REQ_IDETAPE_PC1);
1497 rq->cmd[0] |= REQ_IDETAPE_PC2;
1498 goto out;
1499 }
1500 if (rq->cmd[0] & REQ_IDETAPE_PC2) {
1501 idetape_media_access_finished(drive);
1502 return ide_stopped;
1503 }
1504 BUG();
1505out:
8d06bfad 1506 return idetape_issue_pc(drive, pc);
1da177e4
LT
1507}
1508
3c98bf34 1509/* Pipeline related functions */
1da177e4
LT
1510
1511/*
3c98bf34
BP
1512 * The function below uses __get_free_page to allocate a pipeline stage, along
1513 * with all the necessary small buffers which together make a buffer of size
1514 * tape->stage_size (or a bit more). We attempt to combine sequential pages as
1515 * much as possible.
1da177e4 1516 *
3c98bf34
BP
1517 * It returns a pointer to the new allocated stage, or NULL if we can't (or
1518 * don't want to) allocate a stage.
1da177e4 1519 *
3c98bf34
BP
1520 * Pipeline stages are optional and are used to increase performance. If we
1521 * can't allocate them, we'll manage without them.
1da177e4 1522 */
5a04cfa9
BP
1523static idetape_stage_t *__idetape_kmalloc_stage(idetape_tape_t *tape, int full,
1524 int clear)
1da177e4
LT
1525{
1526 idetape_stage_t *stage;
1527 struct idetape_bh *prev_bh, *bh;
1528 int pages = tape->pages_per_stage;
1529 char *b_data = NULL;
1530
5a04cfa9
BP
1531 stage = kmalloc(sizeof(idetape_stage_t), GFP_KERNEL);
1532 if (!stage)
1da177e4
LT
1533 return NULL;
1534 stage->next = NULL;
1535
5a04cfa9
BP
1536 stage->bh = kmalloc(sizeof(struct idetape_bh), GFP_KERNEL);
1537 bh = stage->bh;
1da177e4
LT
1538 if (bh == NULL)
1539 goto abort;
1540 bh->b_reqnext = NULL;
5a04cfa9
BP
1541 bh->b_data = (char *) __get_free_page(GFP_KERNEL);
1542 if (!bh->b_data)
1da177e4
LT
1543 goto abort;
1544 if (clear)
1545 memset(bh->b_data, 0, PAGE_SIZE);
1546 bh->b_size = PAGE_SIZE;
1547 atomic_set(&bh->b_count, full ? bh->b_size : 0);
1548
1549 while (--pages) {
5a04cfa9
BP
1550 b_data = (char *) __get_free_page(GFP_KERNEL);
1551 if (!b_data)
1da177e4
LT
1552 goto abort;
1553 if (clear)
1554 memset(b_data, 0, PAGE_SIZE);
1555 if (bh->b_data == b_data + PAGE_SIZE) {
1556 bh->b_size += PAGE_SIZE;
1557 bh->b_data -= PAGE_SIZE;
1558 if (full)
1559 atomic_add(PAGE_SIZE, &bh->b_count);
1560 continue;
1561 }
1562 if (b_data == bh->b_data + bh->b_size) {
1563 bh->b_size += PAGE_SIZE;
1564 if (full)
1565 atomic_add(PAGE_SIZE, &bh->b_count);
1566 continue;
1567 }
1568 prev_bh = bh;
5a04cfa9
BP
1569 bh = kmalloc(sizeof(struct idetape_bh), GFP_KERNEL);
1570 if (!bh) {
1da177e4
LT
1571 free_page((unsigned long) b_data);
1572 goto abort;
1573 }
1574 bh->b_reqnext = NULL;
1575 bh->b_data = b_data;
1576 bh->b_size = PAGE_SIZE;
1577 atomic_set(&bh->b_count, full ? bh->b_size : 0);
1578 prev_bh->b_reqnext = bh;
1579 }
1580 bh->b_size -= tape->excess_bh_size;
1581 if (full)
1582 atomic_sub(tape->excess_bh_size, &bh->b_count);
1583 return stage;
1584abort:
1585 __idetape_kfree_stage(stage);
1586 return NULL;
1587}
1588
5a04cfa9 1589static idetape_stage_t *idetape_kmalloc_stage(idetape_tape_t *tape)
1da177e4 1590{
8004a8c9 1591 debug_log(DBG_PROCS, "Enter %s\n", __func__);
1da177e4
LT
1592
1593 if (tape->nr_stages >= tape->max_stages)
1594 return NULL;
1da177e4
LT
1595 return __idetape_kmalloc_stage(tape, 0, 0);
1596}
1597
5a04cfa9
BP
1598static int idetape_copy_stage_from_user(idetape_tape_t *tape,
1599 idetape_stage_t *stage, const char __user *buf, int n)
1da177e4
LT
1600{
1601 struct idetape_bh *bh = tape->bh;
1602 int count;
dcd96379 1603 int ret = 0;
1da177e4
LT
1604
1605 while (n) {
1da177e4 1606 if (bh == NULL) {
5a04cfa9
BP
1607 printk(KERN_ERR "ide-tape: bh == NULL in %s\n",
1608 __func__);
dcd96379 1609 return 1;
1da177e4 1610 }
5a04cfa9
BP
1611 count = min((unsigned int)
1612 (bh->b_size - atomic_read(&bh->b_count)),
1613 (unsigned int)n);
1614 if (copy_from_user(bh->b_data + atomic_read(&bh->b_count), buf,
1615 count))
dcd96379 1616 ret = 1;
1da177e4
LT
1617 n -= count;
1618 atomic_add(count, &bh->b_count);
1619 buf += count;
1620 if (atomic_read(&bh->b_count) == bh->b_size) {
1621 bh = bh->b_reqnext;
1622 if (bh)
1623 atomic_set(&bh->b_count, 0);
1624 }
1625 }
1626 tape->bh = bh;
dcd96379 1627 return ret;
1da177e4
LT
1628}
1629
5a04cfa9
BP
1630static int idetape_copy_stage_to_user(idetape_tape_t *tape, char __user *buf,
1631 idetape_stage_t *stage, int n)
1da177e4
LT
1632{
1633 struct idetape_bh *bh = tape->bh;
1634 int count;
dcd96379 1635 int ret = 0;
1da177e4
LT
1636
1637 while (n) {
1da177e4 1638 if (bh == NULL) {
5a04cfa9
BP
1639 printk(KERN_ERR "ide-tape: bh == NULL in %s\n",
1640 __func__);
dcd96379 1641 return 1;
1da177e4 1642 }
1da177e4 1643 count = min(tape->b_count, n);
dcd96379
DW
1644 if (copy_to_user(buf, tape->b_data, count))
1645 ret = 1;
1da177e4
LT
1646 n -= count;
1647 tape->b_data += count;
1648 tape->b_count -= count;
1649 buf += count;
1650 if (!tape->b_count) {
5a04cfa9
BP
1651 bh = bh->b_reqnext;
1652 tape->bh = bh;
1da177e4
LT
1653 if (bh) {
1654 tape->b_data = bh->b_data;
1655 tape->b_count = atomic_read(&bh->b_count);
1656 }
1657 }
1658 }
dcd96379 1659 return ret;
1da177e4
LT
1660}
1661
5a04cfa9 1662static void idetape_init_merge_stage(idetape_tape_t *tape)
1da177e4
LT
1663{
1664 struct idetape_bh *bh = tape->merge_stage->bh;
5a04cfa9 1665
1da177e4 1666 tape->bh = bh;
54abf37e 1667 if (tape->chrdev_dir == IDETAPE_DIR_WRITE)
1da177e4
LT
1668 atomic_set(&bh->b_count, 0);
1669 else {
1670 tape->b_data = bh->b_data;
1671 tape->b_count = atomic_read(&bh->b_count);
1672 }
1673}
1674
5a04cfa9 1675static void idetape_switch_buffers(idetape_tape_t *tape, idetape_stage_t *stage)
1da177e4
LT
1676{
1677 struct idetape_bh *tmp;
1678
1679 tmp = stage->bh;
1680 stage->bh = tape->merge_stage->bh;
1681 tape->merge_stage->bh = tmp;
1682 idetape_init_merge_stage(tape);
1683}
1684
3c98bf34 1685/* Add a new stage at the end of the pipeline. */
5a04cfa9 1686static void idetape_add_stage_tail(ide_drive_t *drive, idetape_stage_t *stage)
1da177e4
LT
1687{
1688 idetape_tape_t *tape = drive->driver_data;
1689 unsigned long flags;
8004a8c9
BP
1690
1691 debug_log(DBG_PROCS, "Enter %s\n", __func__);
1692
54bb2074 1693 spin_lock_irqsave(&tape->lock, flags);
1da177e4
LT
1694 stage->next = NULL;
1695 if (tape->last_stage != NULL)
5a04cfa9 1696 tape->last_stage->next = stage;
1da177e4 1697 else
5a04cfa9
BP
1698 tape->first_stage = stage;
1699 tape->next_stage = stage;
1da177e4
LT
1700 tape->last_stage = stage;
1701 if (tape->next_stage == NULL)
1702 tape->next_stage = tape->last_stage;
1703 tape->nr_stages++;
1704 tape->nr_pending_stages++;
54bb2074 1705 spin_unlock_irqrestore(&tape->lock, flags);
1da177e4
LT
1706}
1707
3c98bf34
BP
1708/* Install a completion in a pending request and sleep until it is serviced. The
1709 * caller should ensure that the request will not be serviced before we install
1710 * the completion (usually by disabling interrupts).
1da177e4 1711 */
5a04cfa9 1712static void idetape_wait_for_request(ide_drive_t *drive, struct request *rq)
1da177e4 1713{
6e9a4738 1714 DECLARE_COMPLETION_ONSTACK(wait);
1da177e4
LT
1715 idetape_tape_t *tape = drive->driver_data;
1716
4aff5e23 1717 if (rq == NULL || !blk_special_request(rq)) {
5a04cfa9
BP
1718 printk(KERN_ERR "ide-tape: bug: Trying to sleep on non-valid"
1719 " request\n");
1da177e4
LT
1720 return;
1721 }
c00895ab 1722 rq->end_io_data = &wait;
1da177e4 1723 rq->end_io = blk_end_sync_rq;
54bb2074 1724 spin_unlock_irq(&tape->lock);
1da177e4
LT
1725 wait_for_completion(&wait);
1726 /* The stage and its struct request have been deallocated */
54bb2074 1727 spin_lock_irq(&tape->lock);
1da177e4
LT
1728}
1729
a2f5b7f4 1730static ide_startstop_t idetape_read_position_callback(ide_drive_t *drive)
1da177e4
LT
1731{
1732 idetape_tape_t *tape = drive->driver_data;
d236d74c 1733 u8 *readpos = tape->pc->buf;
8004a8c9
BP
1734
1735 debug_log(DBG_PROCS, "Enter %s\n", __func__);
1da177e4
LT
1736
1737 if (!tape->pc->error) {
a2f5b7f4
BP
1738 debug_log(DBG_SENSE, "BOP - %s\n",
1739 (readpos[0] & 0x80) ? "Yes" : "No");
1740 debug_log(DBG_SENSE, "EOP - %s\n",
1741 (readpos[0] & 0x40) ? "Yes" : "No");
1742
1743 if (readpos[0] & 0x4) {
1744 printk(KERN_INFO "ide-tape: Block location is unknown"
1745 "to the tape\n");
346331f8 1746 clear_bit(IDETAPE_FLAG_ADDRESS_VALID, &tape->flags);
1da177e4
LT
1747 idetape_end_request(drive, 0, 0);
1748 } else {
8004a8c9 1749 debug_log(DBG_SENSE, "Block Location - %u\n",
a2f5b7f4
BP
1750 be32_to_cpu(*(u32 *)&readpos[4]));
1751
1752 tape->partition = readpos[1];
54bb2074 1753 tape->first_frame =
a2f5b7f4 1754 be32_to_cpu(*(u32 *)&readpos[4]);
346331f8 1755 set_bit(IDETAPE_FLAG_ADDRESS_VALID, &tape->flags);
1da177e4
LT
1756 idetape_end_request(drive, 1, 0);
1757 }
1758 } else {
1759 idetape_end_request(drive, 0, 0);
1760 }
1761 return ide_stopped;
1762}
1763
1764/*
3c98bf34
BP
1765 * Write a filemark if write_filemark=1. Flush the device buffers without
1766 * writing a filemark otherwise.
1da177e4 1767 */
5a04cfa9 1768static void idetape_create_write_filemark_cmd(ide_drive_t *drive,
d236d74c 1769 struct ide_atapi_pc *pc, int write_filemark)
1da177e4
LT
1770{
1771 idetape_init_pc(pc);
90699ce2 1772 pc->c[0] = WRITE_FILEMARKS;
1da177e4 1773 pc->c[4] = write_filemark;
346331f8 1774 pc->flags |= PC_FLAG_WAIT_FOR_DSC;
d236d74c 1775 pc->idetape_callback = &idetape_pc_callback;
1da177e4
LT
1776}
1777
d236d74c 1778static void idetape_create_test_unit_ready_cmd(struct ide_atapi_pc *pc)
1da177e4
LT
1779{
1780 idetape_init_pc(pc);
90699ce2 1781 pc->c[0] = TEST_UNIT_READY;
d236d74c 1782 pc->idetape_callback = &idetape_pc_callback;
1da177e4
LT
1783}
1784
1785/*
3c98bf34
BP
1786 * We add a special packet command request to the tail of the request queue, and
1787 * wait for it to be serviced. This is not to be called from within the request
1788 * handling part of the driver! We allocate here data on the stack and it is
1789 * valid until the request is finished. This is not the case for the bottom part
1790 * of the driver, where we are always leaving the functions to wait for an
1791 * interrupt or a timer event.
1da177e4 1792 *
3c98bf34
BP
1793 * From the bottom part of the driver, we should allocate safe memory using
1794 * idetape_next_pc_storage() and ide_tape_next_rq_storage(), and add the request
1795 * to the request list without waiting for it to be serviced! In that case, we
1796 * usually use idetape_queue_pc_head().
1da177e4 1797 */
d236d74c 1798static int __idetape_queue_pc_tail(ide_drive_t *drive, struct ide_atapi_pc *pc)
1da177e4
LT
1799{
1800 struct ide_tape_obj *tape = drive->driver_data;
1801 struct request rq;
1802
1803 idetape_init_rq(&rq, REQ_IDETAPE_PC1);
1804 rq.buffer = (char *) pc;
1805 rq.rq_disk = tape->disk;
1806 return ide_do_drive_cmd(drive, &rq, ide_wait);
1807}
1808
d236d74c
BP
1809static void idetape_create_load_unload_cmd(ide_drive_t *drive,
1810 struct ide_atapi_pc *pc, int cmd)
1da177e4
LT
1811{
1812 idetape_init_pc(pc);
90699ce2 1813 pc->c[0] = START_STOP;
1da177e4 1814 pc->c[4] = cmd;
346331f8 1815 pc->flags |= PC_FLAG_WAIT_FOR_DSC;
d236d74c 1816 pc->idetape_callback = &idetape_pc_callback;
1da177e4
LT
1817}
1818
1819static int idetape_wait_ready(ide_drive_t *drive, unsigned long timeout)
1820{
1821 idetape_tape_t *tape = drive->driver_data;
d236d74c 1822 struct ide_atapi_pc pc;
1da177e4
LT
1823 int load_attempted = 0;
1824
3c98bf34 1825 /* Wait for the tape to become ready */
346331f8 1826 set_bit(IDETAPE_FLAG_MEDIUM_PRESENT, &tape->flags);
1da177e4
LT
1827 timeout += jiffies;
1828 while (time_before(jiffies, timeout)) {
1829 idetape_create_test_unit_ready_cmd(&pc);
1830 if (!__idetape_queue_pc_tail(drive, &pc))
1831 return 0;
1832 if ((tape->sense_key == 2 && tape->asc == 4 && tape->ascq == 2)
3c98bf34
BP
1833 || (tape->asc == 0x3A)) {
1834 /* no media */
1da177e4
LT
1835 if (load_attempted)
1836 return -ENOMEDIUM;
5a04cfa9
BP
1837 idetape_create_load_unload_cmd(drive, &pc,
1838 IDETAPE_LU_LOAD_MASK);
1da177e4
LT
1839 __idetape_queue_pc_tail(drive, &pc);
1840 load_attempted = 1;
1841 /* not about to be ready */
1842 } else if (!(tape->sense_key == 2 && tape->asc == 4 &&
1843 (tape->ascq == 1 || tape->ascq == 8)))
1844 return -EIO;
80ce45fd 1845 msleep(100);
1da177e4
LT
1846 }
1847 return -EIO;
1848}
1849
d236d74c 1850static int idetape_queue_pc_tail(ide_drive_t *drive, struct ide_atapi_pc *pc)
1da177e4
LT
1851{
1852 return __idetape_queue_pc_tail(drive, pc);
1853}
1854
5a04cfa9 1855static int idetape_flush_tape_buffers(ide_drive_t *drive)
1da177e4 1856{
d236d74c 1857 struct ide_atapi_pc pc;
1da177e4
LT
1858 int rc;
1859
1860 idetape_create_write_filemark_cmd(drive, &pc, 0);
5a04cfa9
BP
1861 rc = idetape_queue_pc_tail(drive, &pc);
1862 if (rc)
1da177e4
LT
1863 return rc;
1864 idetape_wait_ready(drive, 60 * 5 * HZ);
1865 return 0;
1866}
1867
d236d74c 1868static void idetape_create_read_position_cmd(struct ide_atapi_pc *pc)
1da177e4
LT
1869{
1870 idetape_init_pc(pc);
90699ce2 1871 pc->c[0] = READ_POSITION;
d236d74c
BP
1872 pc->req_xfer = 20;
1873 pc->idetape_callback = &idetape_read_position_callback;
1da177e4
LT
1874}
1875
5a04cfa9 1876static int idetape_read_position(ide_drive_t *drive)
1da177e4
LT
1877{
1878 idetape_tape_t *tape = drive->driver_data;
d236d74c 1879 struct ide_atapi_pc pc;
1da177e4
LT
1880 int position;
1881
8004a8c9 1882 debug_log(DBG_PROCS, "Enter %s\n", __func__);
1da177e4
LT
1883
1884 idetape_create_read_position_cmd(&pc);
1885 if (idetape_queue_pc_tail(drive, &pc))
1886 return -1;
54bb2074 1887 position = tape->first_frame;
1da177e4
LT
1888 return position;
1889}
1890
d236d74c
BP
1891static void idetape_create_locate_cmd(ide_drive_t *drive,
1892 struct ide_atapi_pc *pc,
5a04cfa9 1893 unsigned int block, u8 partition, int skip)
1da177e4
LT
1894{
1895 idetape_init_pc(pc);
90699ce2 1896 pc->c[0] = POSITION_TO_ELEMENT;
1da177e4 1897 pc->c[1] = 2;
860ff5ec 1898 put_unaligned(cpu_to_be32(block), (unsigned int *) &pc->c[3]);
1da177e4 1899 pc->c[8] = partition;
346331f8 1900 pc->flags |= PC_FLAG_WAIT_FOR_DSC;
d236d74c 1901 pc->idetape_callback = &idetape_pc_callback;
1da177e4
LT
1902}
1903
d236d74c
BP
1904static int idetape_create_prevent_cmd(ide_drive_t *drive,
1905 struct ide_atapi_pc *pc, int prevent)
1da177e4
LT
1906{
1907 idetape_tape_t *tape = drive->driver_data;
1908
b6422013
BP
1909 /* device supports locking according to capabilities page */
1910 if (!(tape->caps[6] & 0x01))
1da177e4
LT
1911 return 0;
1912
1913 idetape_init_pc(pc);
90699ce2 1914 pc->c[0] = ALLOW_MEDIUM_REMOVAL;
1da177e4 1915 pc->c[4] = prevent;
d236d74c 1916 pc->idetape_callback = &idetape_pc_callback;
1da177e4
LT
1917 return 1;
1918}
1919
5a04cfa9 1920static int __idetape_discard_read_pipeline(ide_drive_t *drive)
1da177e4
LT
1921{
1922 idetape_tape_t *tape = drive->driver_data;
1923 unsigned long flags;
1924 int cnt;
1925
54abf37e 1926 if (tape->chrdev_dir != IDETAPE_DIR_READ)
1da177e4
LT
1927 return 0;
1928
1929 /* Remove merge stage. */
54bb2074 1930 cnt = tape->merge_stage_size / tape->blk_size;
346331f8 1931 if (test_and_clear_bit(IDETAPE_FLAG_FILEMARK, &tape->flags))
1da177e4
LT
1932 ++cnt; /* Filemarks count as 1 sector */
1933 tape->merge_stage_size = 0;
1934 if (tape->merge_stage != NULL) {
1935 __idetape_kfree_stage(tape->merge_stage);
1936 tape->merge_stage = NULL;
1937 }
1938
1939 /* Clear pipeline flags. */
346331f8 1940 clear_bit(IDETAPE_FLAG_PIPELINE_ERR, &tape->flags);
54abf37e 1941 tape->chrdev_dir = IDETAPE_DIR_NONE;
1da177e4
LT
1942
1943 /* Remove pipeline stages. */
1944 if (tape->first_stage == NULL)
1945 return 0;
1946
54bb2074 1947 spin_lock_irqsave(&tape->lock, flags);
1da177e4 1948 tape->next_stage = NULL;
c4b22f81 1949 if (test_bit(IDETAPE_FLAG_PIPELINE_ACTIVE, &tape->flags))
54bb2074
BP
1950 idetape_wait_for_request(drive, tape->active_data_rq);
1951 spin_unlock_irqrestore(&tape->lock, flags);
1da177e4
LT
1952
1953 while (tape->first_stage != NULL) {
1954 struct request *rq_ptr = &tape->first_stage->rq;
1955
5a04cfa9 1956 cnt += rq_ptr->nr_sectors - rq_ptr->current_nr_sectors;
1da177e4
LT
1957 if (rq_ptr->errors == IDETAPE_ERROR_FILEMARK)
1958 ++cnt;
1959 idetape_remove_stage_head(drive);
1960 }
1961 tape->nr_pending_stages = 0;
1962 tape->max_stages = tape->min_pipeline;
1963 return cnt;
1964}
1965
1966/*
3c98bf34
BP
1967 * Position the tape to the requested block using the LOCATE packet command.
1968 * A READ POSITION command is then issued to check where we are positioned. Like
1969 * all higher level operations, we queue the commands at the tail of the request
1970 * queue and wait for their completion.
1da177e4 1971 */
5a04cfa9
BP
1972static int idetape_position_tape(ide_drive_t *drive, unsigned int block,
1973 u8 partition, int skip)
1da177e4
LT
1974{
1975 idetape_tape_t *tape = drive->driver_data;
1976 int retval;
d236d74c 1977 struct ide_atapi_pc pc;
1da177e4 1978
54abf37e 1979 if (tape->chrdev_dir == IDETAPE_DIR_READ)
1da177e4
LT
1980 __idetape_discard_read_pipeline(drive);
1981 idetape_wait_ready(drive, 60 * 5 * HZ);
1982 idetape_create_locate_cmd(drive, &pc, block, partition, skip);
1983 retval = idetape_queue_pc_tail(drive, &pc);
1984 if (retval)
1985 return (retval);
1986
1987 idetape_create_read_position_cmd(&pc);
1988 return (idetape_queue_pc_tail(drive, &pc));
1989}
1990
5a04cfa9
BP
1991static void idetape_discard_read_pipeline(ide_drive_t *drive,
1992 int restore_position)
1da177e4
LT
1993{
1994 idetape_tape_t *tape = drive->driver_data;
1995 int cnt;
1996 int seek, position;
1997
1998 cnt = __idetape_discard_read_pipeline(drive);
1999 if (restore_position) {
2000 position = idetape_read_position(drive);
2001 seek = position > cnt ? position - cnt : 0;
2002 if (idetape_position_tape(drive, seek, 0, 0)) {
5a04cfa9
BP
2003 printk(KERN_INFO "ide-tape: %s: position_tape failed in"
2004 " discard_pipeline()\n", tape->name);
1da177e4
LT
2005 return;
2006 }
2007 }
2008}
2009
2010/*
3c98bf34
BP
2011 * Generate a read/write request for the block device interface and wait for it
2012 * to be serviced.
1da177e4 2013 */
5a04cfa9
BP
2014static int idetape_queue_rw_tail(ide_drive_t *drive, int cmd, int blocks,
2015 struct idetape_bh *bh)
1da177e4
LT
2016{
2017 idetape_tape_t *tape = drive->driver_data;
2018 struct request rq;
2019
8004a8c9
BP
2020 debug_log(DBG_SENSE, "%s: cmd=%d\n", __func__, cmd);
2021
c4b22f81 2022 if (test_bit(IDETAPE_FLAG_PIPELINE_ACTIVE, &tape->flags)) {
8004a8c9
BP
2023 printk(KERN_ERR "ide-tape: bug: the pipeline is active in %s\n",
2024 __func__);
1da177e4
LT
2025 return (0);
2026 }
1da177e4
LT
2027
2028 idetape_init_rq(&rq, cmd);
2029 rq.rq_disk = tape->disk;
2030 rq.special = (void *)bh;
54bb2074 2031 rq.sector = tape->first_frame;
5a04cfa9
BP
2032 rq.nr_sectors = blocks;
2033 rq.current_nr_sectors = blocks;
1da177e4
LT
2034 (void) ide_do_drive_cmd(drive, &rq, ide_wait);
2035
2036 if ((cmd & (REQ_IDETAPE_READ | REQ_IDETAPE_WRITE)) == 0)
2037 return 0;
2038
2039 if (tape->merge_stage)
2040 idetape_init_merge_stage(tape);
2041 if (rq.errors == IDETAPE_ERROR_GENERAL)
2042 return -EIO;
54bb2074 2043 return (tape->blk_size * (blocks-rq.current_nr_sectors));
1da177e4
LT
2044}
2045
8d06bfad
BP
2046/* start servicing the pipeline stages, starting from tape->next_stage. */
2047static void idetape_plug_pipeline(ide_drive_t *drive)
1da177e4
LT
2048{
2049 idetape_tape_t *tape = drive->driver_data;
2050
2051 if (tape->next_stage == NULL)
2052 return;
c4b22f81 2053 if (!test_and_set_bit(IDETAPE_FLAG_PIPELINE_ACTIVE, &tape->flags)) {
419d4741 2054 idetape_activate_next_stage(drive);
54bb2074 2055 (void) ide_do_drive_cmd(drive, tape->active_data_rq, ide_end);
1da177e4
LT
2056 }
2057}
2058
d236d74c 2059static void idetape_create_inquiry_cmd(struct ide_atapi_pc *pc)
1da177e4
LT
2060{
2061 idetape_init_pc(pc);
90699ce2 2062 pc->c[0] = INQUIRY;
5a04cfa9 2063 pc->c[4] = 254;
d236d74c
BP
2064 pc->req_xfer = 254;
2065 pc->idetape_callback = &idetape_pc_callback;
1da177e4
LT
2066}
2067
d236d74c
BP
2068static void idetape_create_rewind_cmd(ide_drive_t *drive,
2069 struct ide_atapi_pc *pc)
1da177e4
LT
2070{
2071 idetape_init_pc(pc);
90699ce2 2072 pc->c[0] = REZERO_UNIT;
346331f8 2073 pc->flags |= PC_FLAG_WAIT_FOR_DSC;
d236d74c 2074 pc->idetape_callback = &idetape_pc_callback;
1da177e4
LT
2075}
2076
d236d74c 2077static void idetape_create_erase_cmd(struct ide_atapi_pc *pc)
1da177e4
LT
2078{
2079 idetape_init_pc(pc);
90699ce2 2080 pc->c[0] = ERASE;
1da177e4 2081 pc->c[1] = 1;
346331f8 2082 pc->flags |= PC_FLAG_WAIT_FOR_DSC;
d236d74c 2083 pc->idetape_callback = &idetape_pc_callback;
1da177e4
LT
2084}
2085
d236d74c 2086static void idetape_create_space_cmd(struct ide_atapi_pc *pc, int count, u8 cmd)
1da177e4
LT
2087{
2088 idetape_init_pc(pc);
90699ce2 2089 pc->c[0] = SPACE;
860ff5ec 2090 put_unaligned(cpu_to_be32(count), (unsigned int *) &pc->c[1]);
1da177e4 2091 pc->c[1] = cmd;
346331f8 2092 pc->flags |= PC_FLAG_WAIT_FOR_DSC;
d236d74c 2093 pc->idetape_callback = &idetape_pc_callback;
1da177e4
LT
2094}
2095
5a04cfa9 2096static void idetape_wait_first_stage(ide_drive_t *drive)
1da177e4
LT
2097{
2098 idetape_tape_t *tape = drive->driver_data;
2099 unsigned long flags;
2100
2101 if (tape->first_stage == NULL)
2102 return;
54bb2074 2103 spin_lock_irqsave(&tape->lock, flags);
1da177e4 2104 if (tape->active_stage == tape->first_stage)
54bb2074
BP
2105 idetape_wait_for_request(drive, tape->active_data_rq);
2106 spin_unlock_irqrestore(&tape->lock, flags);
1da177e4
LT
2107}
2108
97c566ce 2109/* Queue up a character device originated write request. */
5a04cfa9 2110static int idetape_add_chrdev_write_request(ide_drive_t *drive, int blocks)
1da177e4
LT
2111{
2112 idetape_tape_t *tape = drive->driver_data;
1da177e4 2113 unsigned long flags;
1da177e4 2114
8004a8c9 2115 debug_log(DBG_CHRDEV, "Enter %s\n", __func__);
1da177e4 2116
3c98bf34 2117 /* Attempt to allocate a new stage. Beware possible race conditions. */
97c566ce 2118 while (1) {
54bb2074 2119 spin_lock_irqsave(&tape->lock, flags);
c4b22f81 2120 if (test_bit(IDETAPE_FLAG_PIPELINE_ACTIVE, &tape->flags)) {
54bb2074
BP
2121 idetape_wait_for_request(drive, tape->active_data_rq);
2122 spin_unlock_irqrestore(&tape->lock, flags);
1da177e4 2123 } else {
54bb2074 2124 spin_unlock_irqrestore(&tape->lock, flags);
8d06bfad 2125 idetape_plug_pipeline(drive);
c4b22f81
BP
2126 if (test_bit(IDETAPE_FLAG_PIPELINE_ACTIVE,
2127 &tape->flags))
1da177e4 2128 continue;
5a04cfa9
BP
2129 return idetape_queue_rw_tail(drive, REQ_IDETAPE_WRITE,
2130 blocks, tape->merge_stage->bh);
1da177e4
LT
2131 }
2132 }
1da177e4
LT
2133}
2134
2135/*
3c98bf34
BP
2136 * Wait until all pending pipeline requests are serviced. Typically called on
2137 * device close.
1da177e4 2138 */
5a04cfa9 2139static void idetape_wait_for_pipeline(ide_drive_t *drive)
1da177e4
LT
2140{
2141 idetape_tape_t *tape = drive->driver_data;
2142 unsigned long flags;
2143
c4b22f81
BP
2144 while (tape->next_stage || test_bit(IDETAPE_FLAG_PIPELINE_ACTIVE,
2145 &tape->flags)) {
8d06bfad 2146 idetape_plug_pipeline(drive);
54bb2074 2147 spin_lock_irqsave(&tape->lock, flags);
c4b22f81 2148 if (test_bit(IDETAPE_FLAG_PIPELINE_ACTIVE, &tape->flags))
54bb2074
BP
2149 idetape_wait_for_request(drive, tape->active_data_rq);
2150 spin_unlock_irqrestore(&tape->lock, flags);
1da177e4
LT
2151 }
2152}
2153
5a04cfa9 2154static void idetape_empty_write_pipeline(ide_drive_t *drive)
1da177e4
LT
2155{
2156 idetape_tape_t *tape = drive->driver_data;
2157 int blocks, min;
2158 struct idetape_bh *bh;
55a5d291 2159
54abf37e 2160 if (tape->chrdev_dir != IDETAPE_DIR_WRITE) {
5a04cfa9
BP
2161 printk(KERN_ERR "ide-tape: bug: Trying to empty write pipeline,"
2162 " but we are not writing.\n");
1da177e4
LT
2163 return;
2164 }
2165 if (tape->merge_stage_size > tape->stage_size) {
2166 printk(KERN_ERR "ide-tape: bug: merge_buffer too big\n");
2167 tape->merge_stage_size = tape->stage_size;
2168 }
1da177e4 2169 if (tape->merge_stage_size) {
54bb2074
BP
2170 blocks = tape->merge_stage_size / tape->blk_size;
2171 if (tape->merge_stage_size % tape->blk_size) {
1da177e4
LT
2172 unsigned int i;
2173
2174 blocks++;
54bb2074
BP
2175 i = tape->blk_size - tape->merge_stage_size %
2176 tape->blk_size;
1da177e4
LT
2177 bh = tape->bh->b_reqnext;
2178 while (bh) {
2179 atomic_set(&bh->b_count, 0);
2180 bh = bh->b_reqnext;
2181 }
2182 bh = tape->bh;
2183 while (i) {
2184 if (bh == NULL) {
5a04cfa9
BP
2185 printk(KERN_INFO "ide-tape: bug,"
2186 " bh NULL\n");
1da177e4
LT
2187 break;
2188 }
5a04cfa9
BP
2189 min = min(i, (unsigned int)(bh->b_size -
2190 atomic_read(&bh->b_count)));
2191 memset(bh->b_data + atomic_read(&bh->b_count),
2192 0, min);
1da177e4
LT
2193 atomic_add(min, &bh->b_count);
2194 i -= min;
2195 bh = bh->b_reqnext;
2196 }
2197 }
2198 (void) idetape_add_chrdev_write_request(drive, blocks);
2199 tape->merge_stage_size = 0;
2200 }
2201 idetape_wait_for_pipeline(drive);
2202 if (tape->merge_stage != NULL) {
2203 __idetape_kfree_stage(tape->merge_stage);
2204 tape->merge_stage = NULL;
2205 }
346331f8 2206 clear_bit(IDETAPE_FLAG_PIPELINE_ERR, &tape->flags);
54abf37e 2207 tape->chrdev_dir = IDETAPE_DIR_NONE;
1da177e4
LT
2208
2209 /*
3c98bf34
BP
2210 * On the next backup, perform the feedback loop again. (I don't want to
2211 * keep sense information between backups, as some systems are
2212 * constantly on, and the system load can be totally different on the
2213 * next backup).
1da177e4
LT
2214 */
2215 tape->max_stages = tape->min_pipeline;
1da177e4
LT
2216 if (tape->first_stage != NULL ||
2217 tape->next_stage != NULL ||
2218 tape->last_stage != NULL ||
2219 tape->nr_stages != 0) {
2220 printk(KERN_ERR "ide-tape: ide-tape pipeline bug, "
2221 "first_stage %p, next_stage %p, "
2222 "last_stage %p, nr_stages %d\n",
2223 tape->first_stage, tape->next_stage,
2224 tape->last_stage, tape->nr_stages);
2225 }
1da177e4
LT
2226}
2227
8d06bfad 2228static int idetape_init_read(ide_drive_t *drive, int max_stages)
1da177e4
LT
2229{
2230 idetape_tape_t *tape = drive->driver_data;
2231 idetape_stage_t *new_stage;
2232 struct request rq;
2233 int bytes_read;
b6422013 2234 u16 blocks = *(u16 *)&tape->caps[12];
1da177e4
LT
2235
2236 /* Initialize read operation */
54abf37e
BP
2237 if (tape->chrdev_dir != IDETAPE_DIR_READ) {
2238 if (tape->chrdev_dir == IDETAPE_DIR_WRITE) {
1da177e4
LT
2239 idetape_empty_write_pipeline(drive);
2240 idetape_flush_tape_buffers(drive);
2241 }
1da177e4 2242 if (tape->merge_stage || tape->merge_stage_size) {
5a04cfa9
BP
2243 printk(KERN_ERR "ide-tape: merge_stage_size should be"
2244 " 0 now\n");
1da177e4
LT
2245 tape->merge_stage_size = 0;
2246 }
5a04cfa9
BP
2247 tape->merge_stage = __idetape_kmalloc_stage(tape, 0, 0);
2248 if (!tape->merge_stage)
1da177e4 2249 return -ENOMEM;
54abf37e 2250 tape->chrdev_dir = IDETAPE_DIR_READ;
1da177e4
LT
2251
2252 /*
3c98bf34
BP
2253 * Issue a read 0 command to ensure that DSC handshake is
2254 * switched from completion mode to buffer available mode.
2255 * No point in issuing this if DSC overlap isn't supported, some
2256 * drives (Seagate STT3401A) will return an error.
1da177e4
LT
2257 */
2258 if (drive->dsc_overlap) {
5a04cfa9
BP
2259 bytes_read = idetape_queue_rw_tail(drive,
2260 REQ_IDETAPE_READ, 0,
2261 tape->merge_stage->bh);
1da177e4
LT
2262 if (bytes_read < 0) {
2263 __idetape_kfree_stage(tape->merge_stage);
2264 tape->merge_stage = NULL;
54abf37e 2265 tape->chrdev_dir = IDETAPE_DIR_NONE;
1da177e4
LT
2266 return bytes_read;
2267 }
2268 }
2269 }
1da177e4 2270 idetape_init_rq(&rq, REQ_IDETAPE_READ);
54bb2074 2271 rq.sector = tape->first_frame;
5a04cfa9
BP
2272 rq.nr_sectors = blocks;
2273 rq.current_nr_sectors = blocks;
346331f8 2274 if (!test_bit(IDETAPE_FLAG_PIPELINE_ERR, &tape->flags) &&
1da177e4
LT
2275 tape->nr_stages < max_stages) {
2276 new_stage = idetape_kmalloc_stage(tape);
2277 while (new_stage != NULL) {
2278 new_stage->rq = rq;
2279 idetape_add_stage_tail(drive, new_stage);
2280 if (tape->nr_stages >= max_stages)
2281 break;
2282 new_stage = idetape_kmalloc_stage(tape);
2283 }
2284 }
c4b22f81 2285 if (!test_bit(IDETAPE_FLAG_PIPELINE_ACTIVE, &tape->flags)) {
1da177e4
LT
2286 if (tape->nr_pending_stages >= 3 * max_stages / 4) {
2287 tape->measure_insert_time = 1;
2288 tape->insert_time = jiffies;
2289 tape->insert_size = 0;
2290 tape->insert_speed = 0;
8d06bfad 2291 idetape_plug_pipeline(drive);
1da177e4
LT
2292 }
2293 }
2294 return 0;
2295}
2296
2297/*
3c98bf34
BP
2298 * Called from idetape_chrdev_read() to service a character device read request
2299 * and add read-ahead requests to our pipeline.
1da177e4 2300 */
5a04cfa9 2301static int idetape_add_chrdev_read_request(ide_drive_t *drive, int blocks)
1da177e4
LT
2302{
2303 idetape_tape_t *tape = drive->driver_data;
2304 unsigned long flags;
2305 struct request *rq_ptr;
2306 int bytes_read;
2307
8004a8c9 2308 debug_log(DBG_PROCS, "Enter %s, %d blocks\n", __func__, blocks);
1da177e4 2309
3c98bf34 2310 /* If we are at a filemark, return a read length of 0 */
346331f8 2311 if (test_bit(IDETAPE_FLAG_FILEMARK, &tape->flags))
1da177e4
LT
2312 return 0;
2313
3c98bf34 2314 /* Wait for the next block to reach the head of the pipeline. */
8d06bfad 2315 idetape_init_read(drive, tape->max_stages);
1da177e4 2316 if (tape->first_stage == NULL) {
346331f8 2317 if (test_bit(IDETAPE_FLAG_PIPELINE_ERR, &tape->flags))
1da177e4 2318 return 0;
54bb2074
BP
2319 return idetape_queue_rw_tail(drive, REQ_IDETAPE_READ, blocks,
2320 tape->merge_stage->bh);
1da177e4
LT
2321 }
2322 idetape_wait_first_stage(drive);
2323 rq_ptr = &tape->first_stage->rq;
54bb2074
BP
2324 bytes_read = tape->blk_size * (rq_ptr->nr_sectors -
2325 rq_ptr->current_nr_sectors);
5a04cfa9
BP
2326 rq_ptr->nr_sectors = 0;
2327 rq_ptr->current_nr_sectors = 0;
1da177e4
LT
2328
2329 if (rq_ptr->errors == IDETAPE_ERROR_EOD)
2330 return 0;
2331 else {
2332 idetape_switch_buffers(tape, tape->first_stage);
2333 if (rq_ptr->errors == IDETAPE_ERROR_FILEMARK)
346331f8 2334 set_bit(IDETAPE_FLAG_FILEMARK, &tape->flags);
54bb2074 2335 spin_lock_irqsave(&tape->lock, flags);
1da177e4 2336 idetape_remove_stage_head(drive);
54bb2074 2337 spin_unlock_irqrestore(&tape->lock, flags);
1da177e4 2338 }
54bb2074 2339 if (bytes_read > blocks * tape->blk_size) {
5a04cfa9
BP
2340 printk(KERN_ERR "ide-tape: bug: trying to return more bytes"
2341 " than requested\n");
54bb2074 2342 bytes_read = blocks * tape->blk_size;
1da177e4 2343 }
1da177e4
LT
2344 return (bytes_read);
2345}
2346
5a04cfa9 2347static void idetape_pad_zeros(ide_drive_t *drive, int bcount)
1da177e4
LT
2348{
2349 idetape_tape_t *tape = drive->driver_data;
2350 struct idetape_bh *bh;
2351 int blocks;
3c98bf34 2352
1da177e4
LT
2353 while (bcount) {
2354 unsigned int count;
2355
2356 bh = tape->merge_stage->bh;
2357 count = min(tape->stage_size, bcount);
2358 bcount -= count;
54bb2074 2359 blocks = count / tape->blk_size;
1da177e4 2360 while (count) {
5a04cfa9
BP
2361 atomic_set(&bh->b_count,
2362 min(count, (unsigned int)bh->b_size));
1da177e4
LT
2363 memset(bh->b_data, 0, atomic_read(&bh->b_count));
2364 count -= atomic_read(&bh->b_count);
2365 bh = bh->b_reqnext;
2366 }
5a04cfa9
BP
2367 idetape_queue_rw_tail(drive, REQ_IDETAPE_WRITE, blocks,
2368 tape->merge_stage->bh);
1da177e4
LT
2369 }
2370}
2371
5a04cfa9 2372static int idetape_pipeline_size(ide_drive_t *drive)
1da177e4
LT
2373{
2374 idetape_tape_t *tape = drive->driver_data;
2375 idetape_stage_t *stage;
2376 struct request *rq;
2377 int size = 0;
2378
2379 idetape_wait_for_pipeline(drive);
2380 stage = tape->first_stage;
2381 while (stage != NULL) {
2382 rq = &stage->rq;
54bb2074
BP
2383 size += tape->blk_size * (rq->nr_sectors -
2384 rq->current_nr_sectors);
1da177e4 2385 if (rq->errors == IDETAPE_ERROR_FILEMARK)
54bb2074 2386 size += tape->blk_size;
1da177e4
LT
2387 stage = stage->next;
2388 }
2389 size += tape->merge_stage_size;
2390 return size;
2391}
2392
2393/*
3c98bf34
BP
2394 * Rewinds the tape to the Beginning Of the current Partition (BOP). We
2395 * currently support only one partition.
2396 */
5a04cfa9 2397static int idetape_rewind_tape(ide_drive_t *drive)
1da177e4
LT
2398{
2399 int retval;
d236d74c 2400 struct ide_atapi_pc pc;
8004a8c9
BP
2401 idetape_tape_t *tape;
2402 tape = drive->driver_data;
2403
2404 debug_log(DBG_SENSE, "Enter %s\n", __func__);
2405
1da177e4
LT
2406 idetape_create_rewind_cmd(drive, &pc);
2407 retval = idetape_queue_pc_tail(drive, &pc);
2408 if (retval)
2409 return retval;
2410
2411 idetape_create_read_position_cmd(&pc);
2412 retval = idetape_queue_pc_tail(drive, &pc);
2413 if (retval)
2414 return retval;
2415 return 0;
2416}
2417
3c98bf34 2418/* mtio.h compatible commands should be issued to the chrdev interface. */
5a04cfa9
BP
2419static int idetape_blkdev_ioctl(ide_drive_t *drive, unsigned int cmd,
2420 unsigned long arg)
1da177e4
LT
2421{
2422 idetape_tape_t *tape = drive->driver_data;
1da177e4
LT
2423 void __user *argp = (void __user *)arg;
2424
d59823fa
BP
2425 struct idetape_config {
2426 int dsc_rw_frequency;
2427 int dsc_media_access_frequency;
2428 int nr_stages;
2429 } config;
2430
8004a8c9
BP
2431 debug_log(DBG_PROCS, "Enter %s\n", __func__);
2432
1da177e4 2433 switch (cmd) {
5a04cfa9
BP
2434 case 0x0340:
2435 if (copy_from_user(&config, argp, sizeof(config)))
2436 return -EFAULT;
2437 tape->best_dsc_rw_freq = config.dsc_rw_frequency;
2438 tape->max_stages = config.nr_stages;
2439 break;
2440 case 0x0350:
2441 config.dsc_rw_frequency = (int) tape->best_dsc_rw_freq;
2442 config.nr_stages = tape->max_stages;
2443 if (copy_to_user(argp, &config, sizeof(config)))
2444 return -EFAULT;
2445 break;
2446 default:
2447 return -EIO;
1da177e4
LT
2448 }
2449 return 0;
2450}
2451
2452/*
3c98bf34
BP
2453 * The function below is now a bit more complicated than just passing the
2454 * command to the tape since we may have crossed some filemarks during our
2455 * pipelined read-ahead mode. As a minor side effect, the pipeline enables us to
2456 * support MTFSFM when the filemark is in our internal pipeline even if the tape
2457 * doesn't support spacing over filemarks in the reverse direction.
1da177e4 2458 */
5a04cfa9
BP
2459static int idetape_space_over_filemarks(ide_drive_t *drive, short mt_op,
2460 int mt_count)
1da177e4
LT
2461{
2462 idetape_tape_t *tape = drive->driver_data;
d236d74c 2463 struct ide_atapi_pc pc;
1da177e4 2464 unsigned long flags;
5a04cfa9 2465 int retval, count = 0;
b6422013 2466 int sprev = !!(tape->caps[4] & 0x20);
1da177e4
LT
2467
2468 if (mt_count == 0)
2469 return 0;
2470 if (MTBSF == mt_op || MTBSFM == mt_op) {
b6422013 2471 if (!sprev)
1da177e4 2472 return -EIO;
5a04cfa9 2473 mt_count = -mt_count;
1da177e4
LT
2474 }
2475
54abf37e 2476 if (tape->chrdev_dir == IDETAPE_DIR_READ) {
3c98bf34 2477 /* its a read-ahead buffer, scan it for crossed filemarks. */
1da177e4 2478 tape->merge_stage_size = 0;
346331f8 2479 if (test_and_clear_bit(IDETAPE_FLAG_FILEMARK, &tape->flags))
1da177e4
LT
2480 ++count;
2481 while (tape->first_stage != NULL) {
2482 if (count == mt_count) {
2483 if (mt_op == MTFSFM)
346331f8
BP
2484 set_bit(IDETAPE_FLAG_FILEMARK,
2485 &tape->flags);
1da177e4
LT
2486 return 0;
2487 }
54bb2074 2488 spin_lock_irqsave(&tape->lock, flags);
1da177e4
LT
2489 if (tape->first_stage == tape->active_stage) {
2490 /*
3c98bf34
BP
2491 * We have reached the active stage in the read
2492 * pipeline. There is no point in allowing the
2493 * drive to continue reading any farther, so we
2494 * stop the pipeline.
1da177e4 2495 *
3c98bf34
BP
2496 * This section should be moved to a separate
2497 * subroutine because similar operations are
2498 * done in __idetape_discard_read_pipeline(),
2499 * for example.
1da177e4
LT
2500 */
2501 tape->next_stage = NULL;
54bb2074 2502 spin_unlock_irqrestore(&tape->lock, flags);
1da177e4
LT
2503 idetape_wait_first_stage(drive);
2504 tape->next_stage = tape->first_stage->next;
2505 } else
54bb2074 2506 spin_unlock_irqrestore(&tape->lock, flags);
5a04cfa9
BP
2507 if (tape->first_stage->rq.errors ==
2508 IDETAPE_ERROR_FILEMARK)
1da177e4
LT
2509 ++count;
2510 idetape_remove_stage_head(drive);
2511 }
2512 idetape_discard_read_pipeline(drive, 0);
2513 }
2514
2515 /*
3c98bf34
BP
2516 * The filemark was not found in our internal pipeline; now we can issue
2517 * the space command.
1da177e4
LT
2518 */
2519 switch (mt_op) {
5a04cfa9
BP
2520 case MTFSF:
2521 case MTBSF:
2522 idetape_create_space_cmd(&pc, mt_count - count,
2523 IDETAPE_SPACE_OVER_FILEMARK);
2524 return idetape_queue_pc_tail(drive, &pc);
2525 case MTFSFM:
2526 case MTBSFM:
2527 if (!sprev)
2528 return -EIO;
2529 retval = idetape_space_over_filemarks(drive, MTFSF,
2530 mt_count - count);
2531 if (retval)
2532 return retval;
2533 count = (MTBSFM == mt_op ? 1 : -1);
2534 return idetape_space_over_filemarks(drive, MTFSF, count);
2535 default:
2536 printk(KERN_ERR "ide-tape: MTIO operation %d not supported\n",
2537 mt_op);
2538 return -EIO;
1da177e4
LT
2539 }
2540}
2541
1da177e4 2542/*
3c98bf34 2543 * Our character device read / write functions.
1da177e4 2544 *
3c98bf34
BP
2545 * The tape is optimized to maximize throughput when it is transferring an
2546 * integral number of the "continuous transfer limit", which is a parameter of
2547 * the specific tape (26kB on my particular tape, 32kB for Onstream).
1da177e4 2548 *
3c98bf34
BP
2549 * As of version 1.3 of the driver, the character device provides an abstract
2550 * continuous view of the media - any mix of block sizes (even 1 byte) on the
2551 * same backup/restore procedure is supported. The driver will internally
2552 * convert the requests to the recommended transfer unit, so that an unmatch
2553 * between the user's block size to the recommended size will only result in a
2554 * (slightly) increased driver overhead, but will no longer hit performance.
2555 * This is not applicable to Onstream.
1da177e4 2556 */
5a04cfa9
BP
2557static ssize_t idetape_chrdev_read(struct file *file, char __user *buf,
2558 size_t count, loff_t *ppos)
1da177e4
LT
2559{
2560 struct ide_tape_obj *tape = ide_tape_f(file);
2561 ide_drive_t *drive = tape->drive;
5a04cfa9 2562 ssize_t bytes_read, temp, actually_read = 0, rc;
dcd96379 2563 ssize_t ret = 0;
b6422013 2564 u16 ctl = *(u16 *)&tape->caps[12];
1da177e4 2565
8004a8c9 2566 debug_log(DBG_CHRDEV, "Enter %s, count %Zd\n", __func__, count);
1da177e4 2567
54abf37e 2568 if (tape->chrdev_dir != IDETAPE_DIR_READ) {
346331f8 2569 if (test_bit(IDETAPE_FLAG_DETECT_BS, &tape->flags))
54bb2074
BP
2570 if (count > tape->blk_size &&
2571 (count % tape->blk_size) == 0)
2572 tape->user_bs_factor = count / tape->blk_size;
1da177e4 2573 }
8d06bfad
BP
2574 rc = idetape_init_read(drive, tape->max_stages);
2575 if (rc < 0)
1da177e4
LT
2576 return rc;
2577 if (count == 0)
2578 return (0);
2579 if (tape->merge_stage_size) {
5a04cfa9
BP
2580 actually_read = min((unsigned int)(tape->merge_stage_size),
2581 (unsigned int)count);
2582 if (idetape_copy_stage_to_user(tape, buf, tape->merge_stage,
2583 actually_read))
dcd96379 2584 ret = -EFAULT;
1da177e4
LT
2585 buf += actually_read;
2586 tape->merge_stage_size -= actually_read;
2587 count -= actually_read;
2588 }
2589 while (count >= tape->stage_size) {
b6422013 2590 bytes_read = idetape_add_chrdev_read_request(drive, ctl);
1da177e4
LT
2591 if (bytes_read <= 0)
2592 goto finish;
5a04cfa9
BP
2593 if (idetape_copy_stage_to_user(tape, buf, tape->merge_stage,
2594 bytes_read))
dcd96379 2595 ret = -EFAULT;
1da177e4
LT
2596 buf += bytes_read;
2597 count -= bytes_read;
2598 actually_read += bytes_read;
2599 }
2600 if (count) {
b6422013 2601 bytes_read = idetape_add_chrdev_read_request(drive, ctl);
1da177e4
LT
2602 if (bytes_read <= 0)
2603 goto finish;
2604 temp = min((unsigned long)count, (unsigned long)bytes_read);
5a04cfa9
BP
2605 if (idetape_copy_stage_to_user(tape, buf, tape->merge_stage,
2606 temp))
dcd96379 2607 ret = -EFAULT;
1da177e4
LT
2608 actually_read += temp;
2609 tape->merge_stage_size = bytes_read-temp;
2610 }
2611finish:
346331f8 2612 if (!actually_read && test_bit(IDETAPE_FLAG_FILEMARK, &tape->flags)) {
8004a8c9
BP
2613 debug_log(DBG_SENSE, "%s: spacing over filemark\n", tape->name);
2614
1da177e4
LT
2615 idetape_space_over_filemarks(drive, MTFSF, 1);
2616 return 0;
2617 }
dcd96379 2618
5a04cfa9 2619 return ret ? ret : actually_read;
1da177e4
LT
2620}
2621
5a04cfa9 2622static ssize_t idetape_chrdev_write(struct file *file, const char __user *buf,
1da177e4
LT
2623 size_t count, loff_t *ppos)
2624{
2625 struct ide_tape_obj *tape = ide_tape_f(file);
2626 ide_drive_t *drive = tape->drive;
dcd96379
DW
2627 ssize_t actually_written = 0;
2628 ssize_t ret = 0;
b6422013 2629 u16 ctl = *(u16 *)&tape->caps[12];
1da177e4
LT
2630
2631 /* The drive is write protected. */
2632 if (tape->write_prot)
2633 return -EACCES;
2634
8004a8c9 2635 debug_log(DBG_CHRDEV, "Enter %s, count %Zd\n", __func__, count);
1da177e4
LT
2636
2637 /* Initialize write operation */
54abf37e
BP
2638 if (tape->chrdev_dir != IDETAPE_DIR_WRITE) {
2639 if (tape->chrdev_dir == IDETAPE_DIR_READ)
1da177e4 2640 idetape_discard_read_pipeline(drive, 1);
1da177e4
LT
2641 if (tape->merge_stage || tape->merge_stage_size) {
2642 printk(KERN_ERR "ide-tape: merge_stage_size "
2643 "should be 0 now\n");
2644 tape->merge_stage_size = 0;
2645 }
5a04cfa9
BP
2646 tape->merge_stage = __idetape_kmalloc_stage(tape, 0, 0);
2647 if (!tape->merge_stage)
1da177e4 2648 return -ENOMEM;
54abf37e 2649 tape->chrdev_dir = IDETAPE_DIR_WRITE;
1da177e4
LT
2650 idetape_init_merge_stage(tape);
2651
2652 /*
3c98bf34
BP
2653 * Issue a write 0 command to ensure that DSC handshake is
2654 * switched from completion mode to buffer available mode. No
2655 * point in issuing this if DSC overlap isn't supported, some
2656 * drives (Seagate STT3401A) will return an error.
1da177e4
LT
2657 */
2658 if (drive->dsc_overlap) {
5a04cfa9
BP
2659 ssize_t retval = idetape_queue_rw_tail(drive,
2660 REQ_IDETAPE_WRITE, 0,
2661 tape->merge_stage->bh);
1da177e4
LT
2662 if (retval < 0) {
2663 __idetape_kfree_stage(tape->merge_stage);
2664 tape->merge_stage = NULL;
54abf37e 2665 tape->chrdev_dir = IDETAPE_DIR_NONE;
1da177e4
LT
2666 return retval;
2667 }
2668 }
2669 }
2670 if (count == 0)
2671 return (0);
1da177e4 2672 if (tape->merge_stage_size) {
1da177e4 2673 if (tape->merge_stage_size >= tape->stage_size) {
5a04cfa9 2674 printk(KERN_ERR "ide-tape: bug: merge buf too big\n");
1da177e4
LT
2675 tape->merge_stage_size = 0;
2676 }
5a04cfa9
BP
2677 actually_written = min((unsigned int)
2678 (tape->stage_size - tape->merge_stage_size),
2679 (unsigned int)count);
2680 if (idetape_copy_stage_from_user(tape, tape->merge_stage, buf,
2681 actually_written))
dcd96379 2682 ret = -EFAULT;
1da177e4
LT
2683 buf += actually_written;
2684 tape->merge_stage_size += actually_written;
2685 count -= actually_written;
2686
2687 if (tape->merge_stage_size == tape->stage_size) {
dcd96379 2688 ssize_t retval;
1da177e4 2689 tape->merge_stage_size = 0;
b6422013 2690 retval = idetape_add_chrdev_write_request(drive, ctl);
1da177e4
LT
2691 if (retval <= 0)
2692 return (retval);
2693 }
2694 }
2695 while (count >= tape->stage_size) {
dcd96379 2696 ssize_t retval;
5a04cfa9
BP
2697 if (idetape_copy_stage_from_user(tape, tape->merge_stage, buf,
2698 tape->stage_size))
dcd96379 2699 ret = -EFAULT;
1da177e4
LT
2700 buf += tape->stage_size;
2701 count -= tape->stage_size;
b6422013 2702 retval = idetape_add_chrdev_write_request(drive, ctl);
1da177e4
LT
2703 actually_written += tape->stage_size;
2704 if (retval <= 0)
2705 return (retval);
2706 }
2707 if (count) {
2708 actually_written += count;
5a04cfa9
BP
2709 if (idetape_copy_stage_from_user(tape, tape->merge_stage, buf,
2710 count))
dcd96379 2711 ret = -EFAULT;
1da177e4
LT
2712 tape->merge_stage_size += count;
2713 }
5a04cfa9 2714 return ret ? ret : actually_written;
1da177e4
LT
2715}
2716
5a04cfa9 2717static int idetape_write_filemark(ide_drive_t *drive)
1da177e4 2718{
d236d74c 2719 struct ide_atapi_pc pc;
1da177e4
LT
2720
2721 /* Write a filemark */
2722 idetape_create_write_filemark_cmd(drive, &pc, 1);
2723 if (idetape_queue_pc_tail(drive, &pc)) {
2724 printk(KERN_ERR "ide-tape: Couldn't write a filemark\n");
2725 return -EIO;
2726 }
2727 return 0;
2728}
2729
2730/*
d99c9da2
BP
2731 * Called from idetape_chrdev_ioctl when the general mtio MTIOCTOP ioctl is
2732 * requested.
1da177e4 2733 *
d99c9da2
BP
2734 * Note: MTBSF and MTBSFM are not supported when the tape doesn't support
2735 * spacing over filemarks in the reverse direction. In this case, MTFSFM is also
2736 * usually not supported (it is supported in the rare case in which we crossed
2737 * the filemark during our read-ahead pipelined operation mode).
1da177e4 2738 *
d99c9da2 2739 * The following commands are currently not supported:
1da177e4 2740 *
d99c9da2
BP
2741 * MTFSS, MTBSS, MTWSM, MTSETDENSITY, MTSETDRVBUFFER, MT_ST_BOOLEANS,
2742 * MT_ST_WRITE_THRESHOLD.
1da177e4 2743 */
d99c9da2 2744static int idetape_mtioctop(ide_drive_t *drive, short mt_op, int mt_count)
1da177e4
LT
2745{
2746 idetape_tape_t *tape = drive->driver_data;
d236d74c 2747 struct ide_atapi_pc pc;
5a04cfa9 2748 int i, retval;
1da177e4 2749
8004a8c9
BP
2750 debug_log(DBG_ERR, "Handling MTIOCTOP ioctl: mt_op=%d, mt_count=%d\n",
2751 mt_op, mt_count);
5a04cfa9 2752
3c98bf34 2753 /* Commands which need our pipelined read-ahead stages. */
1da177e4 2754 switch (mt_op) {
5a04cfa9
BP
2755 case MTFSF:
2756 case MTFSFM:
2757 case MTBSF:
2758 case MTBSFM:
2759 if (!mt_count)
2760 return 0;
2761 return idetape_space_over_filemarks(drive, mt_op, mt_count);
2762 default:
2763 break;
1da177e4 2764 }
5a04cfa9 2765
1da177e4 2766 switch (mt_op) {
5a04cfa9
BP
2767 case MTWEOF:
2768 if (tape->write_prot)
2769 return -EACCES;
2770 idetape_discard_read_pipeline(drive, 1);
2771 for (i = 0; i < mt_count; i++) {
2772 retval = idetape_write_filemark(drive);
2773 if (retval)
2774 return retval;
2775 }
2776 return 0;
2777 case MTREW:
2778 idetape_discard_read_pipeline(drive, 0);
2779 if (idetape_rewind_tape(drive))
2780 return -EIO;
2781 return 0;
2782 case MTLOAD:
2783 idetape_discard_read_pipeline(drive, 0);
2784 idetape_create_load_unload_cmd(drive, &pc,
2785 IDETAPE_LU_LOAD_MASK);
2786 return idetape_queue_pc_tail(drive, &pc);
2787 case MTUNLOAD:
2788 case MTOFFL:
2789 /*
2790 * If door is locked, attempt to unlock before
2791 * attempting to eject.
2792 */
2793 if (tape->door_locked) {
2794 if (idetape_create_prevent_cmd(drive, &pc, 0))
2795 if (!idetape_queue_pc_tail(drive, &pc))
2796 tape->door_locked = DOOR_UNLOCKED;
2797 }
2798 idetape_discard_read_pipeline(drive, 0);
2799 idetape_create_load_unload_cmd(drive, &pc,
2800 !IDETAPE_LU_LOAD_MASK);
2801 retval = idetape_queue_pc_tail(drive, &pc);
2802 if (!retval)
346331f8 2803 clear_bit(IDETAPE_FLAG_MEDIUM_PRESENT, &tape->flags);
5a04cfa9
BP
2804 return retval;
2805 case MTNOP:
2806 idetape_discard_read_pipeline(drive, 0);
2807 return idetape_flush_tape_buffers(drive);
2808 case MTRETEN:
2809 idetape_discard_read_pipeline(drive, 0);
2810 idetape_create_load_unload_cmd(drive, &pc,
2811 IDETAPE_LU_RETENSION_MASK | IDETAPE_LU_LOAD_MASK);
2812 return idetape_queue_pc_tail(drive, &pc);
2813 case MTEOM:
2814 idetape_create_space_cmd(&pc, 0, IDETAPE_SPACE_TO_EOD);
2815 return idetape_queue_pc_tail(drive, &pc);
2816 case MTERASE:
2817 (void)idetape_rewind_tape(drive);
2818 idetape_create_erase_cmd(&pc);
2819 return idetape_queue_pc_tail(drive, &pc);
2820 case MTSETBLK:
2821 if (mt_count) {
2822 if (mt_count < tape->blk_size ||
2823 mt_count % tape->blk_size)
1da177e4 2824 return -EIO;
5a04cfa9 2825 tape->user_bs_factor = mt_count / tape->blk_size;
346331f8 2826 clear_bit(IDETAPE_FLAG_DETECT_BS, &tape->flags);
5a04cfa9 2827 } else
346331f8 2828 set_bit(IDETAPE_FLAG_DETECT_BS, &tape->flags);
5a04cfa9
BP
2829 return 0;
2830 case MTSEEK:
2831 idetape_discard_read_pipeline(drive, 0);
2832 return idetape_position_tape(drive,
2833 mt_count * tape->user_bs_factor, tape->partition, 0);
2834 case MTSETPART:
2835 idetape_discard_read_pipeline(drive, 0);
2836 return idetape_position_tape(drive, 0, mt_count, 0);
2837 case MTFSR:
2838 case MTBSR:
2839 case MTLOCK:
2840 if (!idetape_create_prevent_cmd(drive, &pc, 1))
1da177e4 2841 return 0;
5a04cfa9
BP
2842 retval = idetape_queue_pc_tail(drive, &pc);
2843 if (retval)
1da177e4 2844 return retval;
5a04cfa9
BP
2845 tape->door_locked = DOOR_EXPLICITLY_LOCKED;
2846 return 0;
2847 case MTUNLOCK:
2848 if (!idetape_create_prevent_cmd(drive, &pc, 0))
1da177e4 2849 return 0;
5a04cfa9
BP
2850 retval = idetape_queue_pc_tail(drive, &pc);
2851 if (retval)
2852 return retval;
2853 tape->door_locked = DOOR_UNLOCKED;
2854 return 0;
2855 default:
2856 printk(KERN_ERR "ide-tape: MTIO operation %d not supported\n",
2857 mt_op);
2858 return -EIO;
1da177e4
LT
2859 }
2860}
2861
2862/*
d99c9da2
BP
2863 * Our character device ioctls. General mtio.h magnetic io commands are
2864 * supported here, and not in the corresponding block interface. Our own
2865 * ide-tape ioctls are supported on both interfaces.
1da177e4 2866 */
d99c9da2
BP
2867static int idetape_chrdev_ioctl(struct inode *inode, struct file *file,
2868 unsigned int cmd, unsigned long arg)
1da177e4
LT
2869{
2870 struct ide_tape_obj *tape = ide_tape_f(file);
2871 ide_drive_t *drive = tape->drive;
2872 struct mtop mtop;
2873 struct mtget mtget;
2874 struct mtpos mtpos;
54bb2074 2875 int block_offset = 0, position = tape->first_frame;
1da177e4
LT
2876 void __user *argp = (void __user *)arg;
2877
8004a8c9 2878 debug_log(DBG_CHRDEV, "Enter %s, cmd=%u\n", __func__, cmd);
1da177e4 2879
54abf37e 2880 if (tape->chrdev_dir == IDETAPE_DIR_WRITE) {
1da177e4
LT
2881 idetape_empty_write_pipeline(drive);
2882 idetape_flush_tape_buffers(drive);
2883 }
2884 if (cmd == MTIOCGET || cmd == MTIOCPOS) {
54bb2074
BP
2885 block_offset = idetape_pipeline_size(drive) /
2886 (tape->blk_size * tape->user_bs_factor);
5a04cfa9
BP
2887 position = idetape_read_position(drive);
2888 if (position < 0)
1da177e4
LT
2889 return -EIO;
2890 }
2891 switch (cmd) {
5a04cfa9
BP
2892 case MTIOCTOP:
2893 if (copy_from_user(&mtop, argp, sizeof(struct mtop)))
2894 return -EFAULT;
2895 return idetape_mtioctop(drive, mtop.mt_op, mtop.mt_count);
2896 case MTIOCGET:
2897 memset(&mtget, 0, sizeof(struct mtget));
2898 mtget.mt_type = MT_ISSCSI2;
2899 mtget.mt_blkno = position / tape->user_bs_factor - block_offset;
2900 mtget.mt_dsreg =
2901 ((tape->blk_size * tape->user_bs_factor)
2902 << MT_ST_BLKSIZE_SHIFT) & MT_ST_BLKSIZE_MASK;
2903
2904 if (tape->drv_write_prot)
2905 mtget.mt_gstat |= GMT_WR_PROT(0xffffffff);
2906
2907 if (copy_to_user(argp, &mtget, sizeof(struct mtget)))
2908 return -EFAULT;
2909 return 0;
2910 case MTIOCPOS:
2911 mtpos.mt_blkno = position / tape->user_bs_factor - block_offset;
2912 if (copy_to_user(argp, &mtpos, sizeof(struct mtpos)))
2913 return -EFAULT;
2914 return 0;
2915 default:
2916 if (tape->chrdev_dir == IDETAPE_DIR_READ)
2917 idetape_discard_read_pipeline(drive, 1);
2918 return idetape_blkdev_ioctl(drive, cmd, arg);
1da177e4
LT
2919 }
2920}
2921
3cffb9ce
BP
2922/*
2923 * Do a mode sense page 0 with block descriptor and if it succeeds set the tape
2924 * block size with the reported value.
2925 */
2926static void ide_tape_get_bsize_from_bdesc(ide_drive_t *drive)
2927{
2928 idetape_tape_t *tape = drive->driver_data;
d236d74c 2929 struct ide_atapi_pc pc;
3cffb9ce
BP
2930
2931 idetape_create_mode_sense_cmd(&pc, IDETAPE_BLOCK_DESCRIPTOR);
2932 if (idetape_queue_pc_tail(drive, &pc)) {
2933 printk(KERN_ERR "ide-tape: Can't get block descriptor\n");
54bb2074 2934 if (tape->blk_size == 0) {
3cffb9ce
BP
2935 printk(KERN_WARNING "ide-tape: Cannot deal with zero "
2936 "block size, assuming 32k\n");
54bb2074 2937 tape->blk_size = 32768;
3cffb9ce
BP
2938 }
2939 return;
2940 }
d236d74c
BP
2941 tape->blk_size = (pc.buf[4 + 5] << 16) +
2942 (pc.buf[4 + 6] << 8) +
2943 pc.buf[4 + 7];
2944 tape->drv_write_prot = (pc.buf[2] & 0x80) >> 7;
3cffb9ce 2945}
1da177e4 2946
5a04cfa9 2947static int idetape_chrdev_open(struct inode *inode, struct file *filp)
1da177e4
LT
2948{
2949 unsigned int minor = iminor(inode), i = minor & ~0xc0;
2950 ide_drive_t *drive;
2951 idetape_tape_t *tape;
d236d74c 2952 struct ide_atapi_pc pc;
1da177e4
LT
2953 int retval;
2954
8004a8c9
BP
2955 if (i >= MAX_HWIFS * MAX_DRIVES)
2956 return -ENXIO;
2957
2958 tape = ide_tape_chrdev_get(i);
2959 if (!tape)
2960 return -ENXIO;
2961
2962 debug_log(DBG_CHRDEV, "Enter %s\n", __func__);
2963
1da177e4
LT
2964 /*
2965 * We really want to do nonseekable_open(inode, filp); here, but some
2966 * versions of tar incorrectly call lseek on tapes and bail out if that
2967 * fails. So we disallow pread() and pwrite(), but permit lseeks.
2968 */
2969 filp->f_mode &= ~(FMODE_PREAD | FMODE_PWRITE);
2970
1da177e4
LT
2971 drive = tape->drive;
2972
2973 filp->private_data = tape;
2974
346331f8 2975 if (test_and_set_bit(IDETAPE_FLAG_BUSY, &tape->flags)) {
1da177e4
LT
2976 retval = -EBUSY;
2977 goto out_put_tape;
2978 }
2979
2980 retval = idetape_wait_ready(drive, 60 * HZ);
2981 if (retval) {
346331f8 2982 clear_bit(IDETAPE_FLAG_BUSY, &tape->flags);
1da177e4
LT
2983 printk(KERN_ERR "ide-tape: %s: drive not ready\n", tape->name);
2984 goto out_put_tape;
2985 }
2986
2987 idetape_read_position(drive);
346331f8 2988 if (!test_bit(IDETAPE_FLAG_ADDRESS_VALID, &tape->flags))
1da177e4
LT
2989 (void)idetape_rewind_tape(drive);
2990
54abf37e 2991 if (tape->chrdev_dir != IDETAPE_DIR_READ)
346331f8 2992 clear_bit(IDETAPE_FLAG_PIPELINE_ERR, &tape->flags);
1da177e4
LT
2993
2994 /* Read block size and write protect status from drive. */
3cffb9ce 2995 ide_tape_get_bsize_from_bdesc(drive);
1da177e4
LT
2996
2997 /* Set write protect flag if device is opened as read-only. */
2998 if ((filp->f_flags & O_ACCMODE) == O_RDONLY)
2999 tape->write_prot = 1;
3000 else
3001 tape->write_prot = tape->drv_write_prot;
3002
3003 /* Make sure drive isn't write protected if user wants to write. */
3004 if (tape->write_prot) {
3005 if ((filp->f_flags & O_ACCMODE) == O_WRONLY ||
3006 (filp->f_flags & O_ACCMODE) == O_RDWR) {
346331f8 3007 clear_bit(IDETAPE_FLAG_BUSY, &tape->flags);
1da177e4
LT
3008 retval = -EROFS;
3009 goto out_put_tape;
3010 }
3011 }
3012
3c98bf34 3013 /* Lock the tape drive door so user can't eject. */
54abf37e 3014 if (tape->chrdev_dir == IDETAPE_DIR_NONE) {
1da177e4
LT
3015 if (idetape_create_prevent_cmd(drive, &pc, 1)) {
3016 if (!idetape_queue_pc_tail(drive, &pc)) {
3017 if (tape->door_locked != DOOR_EXPLICITLY_LOCKED)
3018 tape->door_locked = DOOR_LOCKED;
3019 }
3020 }
3021 }
1da177e4
LT
3022 return 0;
3023
3024out_put_tape:
3025 ide_tape_put(tape);
3026 return retval;
3027}
3028
5a04cfa9 3029static void idetape_write_release(ide_drive_t *drive, unsigned int minor)
1da177e4
LT
3030{
3031 idetape_tape_t *tape = drive->driver_data;
3032
3033 idetape_empty_write_pipeline(drive);
3034 tape->merge_stage = __idetape_kmalloc_stage(tape, 1, 0);
3035 if (tape->merge_stage != NULL) {
54bb2074
BP
3036 idetape_pad_zeros(drive, tape->blk_size *
3037 (tape->user_bs_factor - 1));
1da177e4
LT
3038 __idetape_kfree_stage(tape->merge_stage);
3039 tape->merge_stage = NULL;
3040 }
3041 idetape_write_filemark(drive);
3042 idetape_flush_tape_buffers(drive);
3043 idetape_flush_tape_buffers(drive);
3044}
3045
5a04cfa9 3046static int idetape_chrdev_release(struct inode *inode, struct file *filp)
1da177e4
LT
3047{
3048 struct ide_tape_obj *tape = ide_tape_f(filp);
3049 ide_drive_t *drive = tape->drive;
d236d74c 3050 struct ide_atapi_pc pc;
1da177e4
LT
3051 unsigned int minor = iminor(inode);
3052
3053 lock_kernel();
3054 tape = drive->driver_data;
8004a8c9
BP
3055
3056 debug_log(DBG_CHRDEV, "Enter %s\n", __func__);
1da177e4 3057
54abf37e 3058 if (tape->chrdev_dir == IDETAPE_DIR_WRITE)
1da177e4 3059 idetape_write_release(drive, minor);
54abf37e 3060 if (tape->chrdev_dir == IDETAPE_DIR_READ) {
1da177e4
LT
3061 if (minor < 128)
3062 idetape_discard_read_pipeline(drive, 1);
3063 else
3064 idetape_wait_for_pipeline(drive);
3065 }
f64eee7b 3066
346331f8 3067 if (minor < 128 && test_bit(IDETAPE_FLAG_MEDIUM_PRESENT, &tape->flags))
1da177e4 3068 (void) idetape_rewind_tape(drive);
54abf37e 3069 if (tape->chrdev_dir == IDETAPE_DIR_NONE) {
1da177e4
LT
3070 if (tape->door_locked == DOOR_LOCKED) {
3071 if (idetape_create_prevent_cmd(drive, &pc, 0)) {
3072 if (!idetape_queue_pc_tail(drive, &pc))
3073 tape->door_locked = DOOR_UNLOCKED;
3074 }
3075 }
3076 }
346331f8 3077 clear_bit(IDETAPE_FLAG_BUSY, &tape->flags);
1da177e4
LT
3078 ide_tape_put(tape);
3079 unlock_kernel();
3080 return 0;
3081}
3082
3083/*
71071b8e 3084 * check the contents of the ATAPI IDENTIFY command results. We return:
1da177e4 3085 *
71071b8e
BP
3086 * 1 - If the tape can be supported by us, based on the information we have so
3087 * far.
1da177e4 3088 *
71071b8e 3089 * 0 - If this tape driver is not currently supported by us.
1da177e4 3090 */
71071b8e 3091static int idetape_identify_device(ide_drive_t *drive)
1da177e4 3092{
71071b8e 3093 u8 gcw[2], protocol, device_type, removable, packet_size;
1da177e4
LT
3094
3095 if (drive->id_read == 0)
3096 return 1;
3097
71071b8e 3098 *((unsigned short *) &gcw) = drive->id->config;
1da177e4 3099
71071b8e
BP
3100 protocol = (gcw[1] & 0xC0) >> 6;
3101 device_type = gcw[1] & 0x1F;
3102 removable = !!(gcw[0] & 0x80);
3103 packet_size = gcw[0] & 0x3;
1da177e4 3104
71071b8e
BP
3105 /* Check that we can support this device */
3106 if (protocol != 2)
16422de3 3107 printk(KERN_ERR "ide-tape: Protocol (0x%02x) is not ATAPI\n",
71071b8e
BP
3108 protocol);
3109 else if (device_type != 1)
16422de3 3110 printk(KERN_ERR "ide-tape: Device type (0x%02x) is not set "
71071b8e
BP
3111 "to tape\n", device_type);
3112 else if (!removable)
1da177e4 3113 printk(KERN_ERR "ide-tape: The removable flag is not set\n");
71071b8e 3114 else if (packet_size != 0) {
24d57f8b
BP
3115 printk(KERN_ERR "ide-tape: Packet size (0x%02x) is not 12"
3116 " bytes\n", packet_size);
1da177e4
LT
3117 } else
3118 return 1;
3119 return 0;
3120}
3121
6d29c8f0 3122static void idetape_get_inquiry_results(ide_drive_t *drive)
1da177e4 3123{
1da177e4 3124 idetape_tape_t *tape = drive->driver_data;
d236d74c 3125 struct ide_atapi_pc pc;
41f81d54 3126 char fw_rev[6], vendor_id[10], product_id[18];
6d29c8f0 3127
1da177e4
LT
3128 idetape_create_inquiry_cmd(&pc);
3129 if (idetape_queue_pc_tail(drive, &pc)) {
6d29c8f0
BP
3130 printk(KERN_ERR "ide-tape: %s: can't get INQUIRY results\n",
3131 tape->name);
1da177e4
LT
3132 return;
3133 }
d236d74c
BP
3134 memcpy(vendor_id, &pc.buf[8], 8);
3135 memcpy(product_id, &pc.buf[16], 16);
3136 memcpy(fw_rev, &pc.buf[32], 4);
41f81d54
BP
3137
3138 ide_fixstring(vendor_id, 10, 0);
3139 ide_fixstring(product_id, 18, 0);
3140 ide_fixstring(fw_rev, 6, 0);
3141
6d29c8f0 3142 printk(KERN_INFO "ide-tape: %s <-> %s: %s %s rev %s\n",
41f81d54 3143 drive->name, tape->name, vendor_id, product_id, fw_rev);
1da177e4
LT
3144}
3145
3146/*
b6422013
BP
3147 * Ask the tape about its various parameters. In particular, we will adjust our
3148 * data transfer buffer size to the recommended value as returned by the tape.
1da177e4 3149 */
5a04cfa9 3150static void idetape_get_mode_sense_results(ide_drive_t *drive)
1da177e4
LT
3151{
3152 idetape_tape_t *tape = drive->driver_data;
d236d74c 3153 struct ide_atapi_pc pc;
b6422013
BP
3154 u8 *caps;
3155 u8 speed, max_speed;
47314fa4 3156
1da177e4
LT
3157 idetape_create_mode_sense_cmd(&pc, IDETAPE_CAPABILITIES_PAGE);
3158 if (idetape_queue_pc_tail(drive, &pc)) {
b6422013
BP
3159 printk(KERN_ERR "ide-tape: Can't get tape parameters - assuming"
3160 " some default values\n");
54bb2074 3161 tape->blk_size = 512;
b6422013
BP
3162 put_unaligned(52, (u16 *)&tape->caps[12]);
3163 put_unaligned(540, (u16 *)&tape->caps[14]);
3164 put_unaligned(6*52, (u16 *)&tape->caps[16]);
1da177e4
LT
3165 return;
3166 }
d236d74c 3167 caps = pc.buf + 4 + pc.buf[3];
b6422013
BP
3168
3169 /* convert to host order and save for later use */
3170 speed = be16_to_cpu(*(u16 *)&caps[14]);
3171 max_speed = be16_to_cpu(*(u16 *)&caps[8]);
1da177e4 3172
b6422013
BP
3173 put_unaligned(max_speed, (u16 *)&caps[8]);
3174 put_unaligned(be16_to_cpu(*(u16 *)&caps[12]), (u16 *)&caps[12]);
3175 put_unaligned(speed, (u16 *)&caps[14]);
3176 put_unaligned(be16_to_cpu(*(u16 *)&caps[16]), (u16 *)&caps[16]);
1da177e4 3177
b6422013
BP
3178 if (!speed) {
3179 printk(KERN_INFO "ide-tape: %s: invalid tape speed "
3180 "(assuming 650KB/sec)\n", drive->name);
3181 put_unaligned(650, (u16 *)&caps[14]);
1da177e4 3182 }
b6422013
BP
3183 if (!max_speed) {
3184 printk(KERN_INFO "ide-tape: %s: invalid max_speed "
3185 "(assuming 650KB/sec)\n", drive->name);
3186 put_unaligned(650, (u16 *)&caps[8]);
1da177e4
LT
3187 }
3188
b6422013
BP
3189 memcpy(&tape->caps, caps, 20);
3190 if (caps[7] & 0x02)
54bb2074 3191 tape->blk_size = 512;
b6422013 3192 else if (caps[7] & 0x04)
54bb2074 3193 tape->blk_size = 1024;
1da177e4
LT
3194}
3195
7662d046 3196#ifdef CONFIG_IDE_PROC_FS
5a04cfa9 3197static void idetape_add_settings(ide_drive_t *drive)
1da177e4
LT
3198{
3199 idetape_tape_t *tape = drive->driver_data;
3200
b6422013
BP
3201 ide_add_setting(drive, "buffer", SETTING_READ, TYPE_SHORT, 0, 0xffff,
3202 1, 2, (u16 *)&tape->caps[16], NULL);
5a04cfa9
BP
3203 ide_add_setting(drive, "pipeline_min", SETTING_RW, TYPE_INT, 1, 0xffff,
3204 tape->stage_size / 1024, 1, &tape->min_pipeline, NULL);
3205 ide_add_setting(drive, "pipeline", SETTING_RW, TYPE_INT, 1, 0xffff,
3206 tape->stage_size / 1024, 1, &tape->max_stages, NULL);
3207 ide_add_setting(drive, "pipeline_max", SETTING_RW, TYPE_INT, 1, 0xffff,
3208 tape->stage_size / 1024, 1, &tape->max_pipeline, NULL);
3209 ide_add_setting(drive, "pipeline_used", SETTING_READ, TYPE_INT, 0,
3210 0xffff, tape->stage_size / 1024, 1, &tape->nr_stages,
3211 NULL);
3212 ide_add_setting(drive, "pipeline_pending", SETTING_READ, TYPE_INT, 0,
3213 0xffff, tape->stage_size / 1024, 1,
3214 &tape->nr_pending_stages, NULL);
b6422013
BP
3215 ide_add_setting(drive, "speed", SETTING_READ, TYPE_SHORT, 0, 0xffff,
3216 1, 1, (u16 *)&tape->caps[14], NULL);
54bb2074
BP
3217 ide_add_setting(drive, "stage", SETTING_READ, TYPE_INT, 0, 0xffff, 1,
3218 1024, &tape->stage_size, NULL);
3219 ide_add_setting(drive, "tdsc", SETTING_RW, TYPE_INT, IDETAPE_DSC_RW_MIN,
3220 IDETAPE_DSC_RW_MAX, 1000, HZ, &tape->best_dsc_rw_freq,
3221 NULL);
5a04cfa9
BP
3222 ide_add_setting(drive, "dsc_overlap", SETTING_RW, TYPE_BYTE, 0, 1, 1,
3223 1, &drive->dsc_overlap, NULL);
5a04cfa9
BP
3224 ide_add_setting(drive, "avg_speed", SETTING_READ, TYPE_INT, 0, 0xffff,
3225 1, 1, &tape->avg_speed, NULL);
8004a8c9
BP
3226 ide_add_setting(drive, "debug_mask", SETTING_RW, TYPE_INT, 0, 0xffff, 1,
3227 1, &tape->debug_mask, NULL);
1da177e4 3228}
7662d046
BZ
3229#else
3230static inline void idetape_add_settings(ide_drive_t *drive) { ; }
3231#endif
1da177e4
LT
3232
3233/*
3c98bf34 3234 * The function below is called to:
1da177e4 3235 *
3c98bf34
BP
3236 * 1. Initialize our various state variables.
3237 * 2. Ask the tape for its capabilities.
3238 * 3. Allocate a buffer which will be used for data transfer. The buffer size
3239 * is chosen based on the recommendation which we received in step 2.
1da177e4 3240 *
3c98bf34
BP
3241 * Note that at this point ide.c already assigned us an irq, so that we can
3242 * queue requests here and wait for their completion.
1da177e4 3243 */
5a04cfa9 3244static void idetape_setup(ide_drive_t *drive, idetape_tape_t *tape, int minor)
1da177e4
LT
3245{
3246 unsigned long t1, tmid, tn, t;
3247 int speed;
1da177e4 3248 int stage_size;
71071b8e 3249 u8 gcw[2];
1da177e4 3250 struct sysinfo si;
b6422013 3251 u16 *ctl = (u16 *)&tape->caps[12];
1da177e4 3252
54bb2074 3253 spin_lock_init(&tape->lock);
1da177e4 3254 drive->dsc_overlap = 1;
4166c199
BZ
3255 if (drive->hwif->host_flags & IDE_HFLAG_NO_DSC) {
3256 printk(KERN_INFO "ide-tape: %s: disabling DSC overlap\n",
3257 tape->name);
3258 drive->dsc_overlap = 0;
1da177e4 3259 }
1da177e4
LT
3260 /* Seagate Travan drives do not support DSC overlap. */
3261 if (strstr(drive->id->model, "Seagate STT3401"))
3262 drive->dsc_overlap = 0;
3263 tape->minor = minor;
3264 tape->name[0] = 'h';
3265 tape->name[1] = 't';
3266 tape->name[2] = '0' + minor;
54abf37e 3267 tape->chrdev_dir = IDETAPE_DIR_NONE;
1da177e4 3268 tape->pc = tape->pc_stack;
1da177e4 3269 *((unsigned short *) &gcw) = drive->id->config;
71071b8e
BP
3270
3271 /* Command packet DRQ type */
3272 if (((gcw[0] & 0x60) >> 5) == 1)
346331f8 3273 set_bit(IDETAPE_FLAG_DRQ_INTERRUPT, &tape->flags);
1da177e4 3274
5a04cfa9
BP
3275 tape->min_pipeline = 10;
3276 tape->max_pipeline = 10;
3277 tape->max_stages = 10;
3c98bf34 3278
1da177e4
LT
3279 idetape_get_inquiry_results(drive);
3280 idetape_get_mode_sense_results(drive);
3cffb9ce 3281 ide_tape_get_bsize_from_bdesc(drive);
1da177e4 3282 tape->user_bs_factor = 1;
54bb2074 3283 tape->stage_size = *ctl * tape->blk_size;
1da177e4
LT
3284 while (tape->stage_size > 0xffff) {
3285 printk(KERN_NOTICE "ide-tape: decreasing stage size\n");
b6422013 3286 *ctl /= 2;
54bb2074 3287 tape->stage_size = *ctl * tape->blk_size;
1da177e4
LT
3288 }
3289 stage_size = tape->stage_size;
3290 tape->pages_per_stage = stage_size / PAGE_SIZE;
3291 if (stage_size % PAGE_SIZE) {
3292 tape->pages_per_stage++;
3293 tape->excess_bh_size = PAGE_SIZE - stage_size % PAGE_SIZE;
3294 }
3295
b6422013
BP
3296 /* Select the "best" DSC read/write polling freq and pipeline size. */
3297 speed = max(*(u16 *)&tape->caps[14], *(u16 *)&tape->caps[8]);
1da177e4
LT
3298
3299 tape->max_stages = speed * 1000 * 10 / tape->stage_size;
3300
3c98bf34 3301 /* Limit memory use for pipeline to 10% of physical memory */
1da177e4 3302 si_meminfo(&si);
5a04cfa9
BP
3303 if (tape->max_stages * tape->stage_size >
3304 si.totalram * si.mem_unit / 10)
3305 tape->max_stages =
3306 si.totalram * si.mem_unit / (10 * tape->stage_size);
3307
1da177e4
LT
3308 tape->max_stages = min(tape->max_stages, IDETAPE_MAX_PIPELINE_STAGES);
3309 tape->min_pipeline = min(tape->max_stages, IDETAPE_MIN_PIPELINE_STAGES);
5a04cfa9
BP
3310 tape->max_pipeline =
3311 min(tape->max_stages * 2, IDETAPE_MAX_PIPELINE_STAGES);
3312 if (tape->max_stages == 0) {
3313 tape->max_stages = 1;
3314 tape->min_pipeline = 1;
3315 tape->max_pipeline = 1;
3316 }
1da177e4
LT
3317
3318 t1 = (tape->stage_size * HZ) / (speed * 1000);
b6422013 3319 tmid = (*(u16 *)&tape->caps[16] * 32 * HZ) / (speed * 125);
1da177e4
LT
3320 tn = (IDETAPE_FIFO_THRESHOLD * tape->stage_size * HZ) / (speed * 1000);
3321
3322 if (tape->max_stages)
3323 t = tn;
3324 else
3325 t = t1;
3326
3327 /*
3c98bf34
BP
3328 * Ensure that the number we got makes sense; limit it within
3329 * IDETAPE_DSC_RW_MIN and IDETAPE_DSC_RW_MAX.
1da177e4 3330 */
54bb2074
BP
3331 tape->best_dsc_rw_freq = max_t(unsigned long,
3332 min_t(unsigned long, t, IDETAPE_DSC_RW_MAX),
3333 IDETAPE_DSC_RW_MIN);
1da177e4
LT
3334 printk(KERN_INFO "ide-tape: %s <-> %s: %dKBps, %d*%dkB buffer, "
3335 "%dkB pipeline, %lums tDSC%s\n",
b6422013
BP
3336 drive->name, tape->name, *(u16 *)&tape->caps[14],
3337 (*(u16 *)&tape->caps[16] * 512) / tape->stage_size,
1da177e4
LT
3338 tape->stage_size / 1024,
3339 tape->max_stages * tape->stage_size / 1024,
54bb2074 3340 tape->best_dsc_rw_freq * 1000 / HZ,
1da177e4
LT
3341 drive->using_dma ? ", DMA":"");
3342
3343 idetape_add_settings(drive);
3344}
3345
4031bbe4 3346static void ide_tape_remove(ide_drive_t *drive)
1da177e4
LT
3347{
3348 idetape_tape_t *tape = drive->driver_data;
1da177e4 3349
7662d046 3350 ide_proc_unregister_driver(drive, tape->driver);
1da177e4
LT
3351
3352 ide_unregister_region(tape->disk);
3353
3354 ide_tape_put(tape);
1da177e4
LT
3355}
3356
3357static void ide_tape_release(struct kref *kref)
3358{
3359 struct ide_tape_obj *tape = to_ide_tape(kref);
3360 ide_drive_t *drive = tape->drive;
3361 struct gendisk *g = tape->disk;
3362
8604affd
BZ
3363 BUG_ON(tape->first_stage != NULL || tape->merge_stage_size);
3364
1da177e4
LT
3365 drive->dsc_overlap = 0;
3366 drive->driver_data = NULL;
dbc1272e 3367 device_destroy(idetape_sysfs_class, MKDEV(IDETAPE_MAJOR, tape->minor));
5a04cfa9
BP
3368 device_destroy(idetape_sysfs_class,
3369 MKDEV(IDETAPE_MAJOR, tape->minor + 128));
1da177e4
LT
3370 idetape_devs[tape->minor] = NULL;
3371 g->private_data = NULL;
3372 put_disk(g);
3373 kfree(tape);
3374}
3375
ecfd80e4 3376#ifdef CONFIG_IDE_PROC_FS
1da177e4
LT
3377static int proc_idetape_read_name
3378 (char *page, char **start, off_t off, int count, int *eof, void *data)
3379{
3380 ide_drive_t *drive = (ide_drive_t *) data;
3381 idetape_tape_t *tape = drive->driver_data;
3382 char *out = page;
3383 int len;
3384
3385 len = sprintf(out, "%s\n", tape->name);
3386 PROC_IDE_READ_RETURN(page, start, off, count, eof, len);
3387}
3388
3389static ide_proc_entry_t idetape_proc[] = {
3390 { "capacity", S_IFREG|S_IRUGO, proc_ide_read_capacity, NULL },
3391 { "name", S_IFREG|S_IRUGO, proc_idetape_read_name, NULL },
3392 { NULL, 0, NULL, NULL }
3393};
1da177e4
LT
3394#endif
3395
4031bbe4 3396static int ide_tape_probe(ide_drive_t *);
1da177e4 3397
1da177e4 3398static ide_driver_t idetape_driver = {
8604affd 3399 .gen_driver = {
4ef3b8f4 3400 .owner = THIS_MODULE,
8604affd
BZ
3401 .name = "ide-tape",
3402 .bus = &ide_bus_type,
8604affd 3403 },
4031bbe4
RK
3404 .probe = ide_tape_probe,
3405 .remove = ide_tape_remove,
1da177e4
LT
3406 .version = IDETAPE_VERSION,
3407 .media = ide_tape,
1da177e4 3408 .supports_dsc_overlap = 1,
1da177e4
LT
3409 .do_request = idetape_do_request,
3410 .end_request = idetape_end_request,
3411 .error = __ide_error,
3412 .abort = __ide_abort,
7662d046 3413#ifdef CONFIG_IDE_PROC_FS
1da177e4 3414 .proc = idetape_proc,
7662d046 3415#endif
1da177e4
LT
3416};
3417
3c98bf34 3418/* Our character device supporting functions, passed to register_chrdev. */
2b8693c0 3419static const struct file_operations idetape_fops = {
1da177e4
LT
3420 .owner = THIS_MODULE,
3421 .read = idetape_chrdev_read,
3422 .write = idetape_chrdev_write,
3423 .ioctl = idetape_chrdev_ioctl,
3424 .open = idetape_chrdev_open,
3425 .release = idetape_chrdev_release,
3426};
3427
3428static int idetape_open(struct inode *inode, struct file *filp)
3429{
3430 struct gendisk *disk = inode->i_bdev->bd_disk;
3431 struct ide_tape_obj *tape;
1da177e4 3432
5a04cfa9
BP
3433 tape = ide_tape_get(disk);
3434 if (!tape)
1da177e4
LT
3435 return -ENXIO;
3436
1da177e4
LT
3437 return 0;
3438}
3439
3440static int idetape_release(struct inode *inode, struct file *filp)
3441{
3442 struct gendisk *disk = inode->i_bdev->bd_disk;
3443 struct ide_tape_obj *tape = ide_tape_g(disk);
1da177e4
LT
3444
3445 ide_tape_put(tape);
3446
3447 return 0;
3448}
3449
3450static int idetape_ioctl(struct inode *inode, struct file *file,
3451 unsigned int cmd, unsigned long arg)
3452{
3453 struct block_device *bdev = inode->i_bdev;
3454 struct ide_tape_obj *tape = ide_tape_g(bdev->bd_disk);
3455 ide_drive_t *drive = tape->drive;
3456 int err = generic_ide_ioctl(drive, file, bdev, cmd, arg);
3457 if (err == -EINVAL)
3458 err = idetape_blkdev_ioctl(drive, cmd, arg);
3459 return err;
3460}
3461
3462static struct block_device_operations idetape_block_ops = {
3463 .owner = THIS_MODULE,
3464 .open = idetape_open,
3465 .release = idetape_release,
3466 .ioctl = idetape_ioctl,
3467};
3468
4031bbe4 3469static int ide_tape_probe(ide_drive_t *drive)
1da177e4
LT
3470{
3471 idetape_tape_t *tape;
3472 struct gendisk *g;
3473 int minor;
3474
3475 if (!strstr("ide-tape", drive->driver_req))
3476 goto failed;
3477 if (!drive->present)
3478 goto failed;
3479 if (drive->media != ide_tape)
3480 goto failed;
5a04cfa9
BP
3481 if (!idetape_identify_device(drive)) {
3482 printk(KERN_ERR "ide-tape: %s: not supported by this version of"
3483 " the driver\n", drive->name);
1da177e4
LT
3484 goto failed;
3485 }
3486 if (drive->scsi) {
5a04cfa9
BP
3487 printk(KERN_INFO "ide-tape: passing drive %s to ide-scsi"
3488 " emulation.\n", drive->name);
1da177e4
LT
3489 goto failed;
3490 }
5a04cfa9 3491 tape = kzalloc(sizeof(idetape_tape_t), GFP_KERNEL);
1da177e4 3492 if (tape == NULL) {
5a04cfa9
BP
3493 printk(KERN_ERR "ide-tape: %s: Can't allocate a tape struct\n",
3494 drive->name);
1da177e4
LT
3495 goto failed;
3496 }
3497
3498 g = alloc_disk(1 << PARTN_BITS);
3499 if (!g)
3500 goto out_free_tape;
3501
3502 ide_init_disk(g, drive);
3503
7662d046 3504 ide_proc_register_driver(drive, &idetape_driver);
1da177e4 3505
1da177e4
LT
3506 kref_init(&tape->kref);
3507
3508 tape->drive = drive;
3509 tape->driver = &idetape_driver;
3510 tape->disk = g;
3511
3512 g->private_data = &tape->driver;
3513
3514 drive->driver_data = tape;
3515
cf8b8975 3516 mutex_lock(&idetape_ref_mutex);
1da177e4
LT
3517 for (minor = 0; idetape_devs[minor]; minor++)
3518 ;
3519 idetape_devs[minor] = tape;
cf8b8975 3520 mutex_unlock(&idetape_ref_mutex);
1da177e4
LT
3521
3522 idetape_setup(drive, tape, minor);
3523
dbc1272e
TJ
3524 device_create(idetape_sysfs_class, &drive->gendev,
3525 MKDEV(IDETAPE_MAJOR, minor), "%s", tape->name);
3526 device_create(idetape_sysfs_class, &drive->gendev,
3527 MKDEV(IDETAPE_MAJOR, minor + 128), "n%s", tape->name);
d5dee80a 3528
1da177e4
LT
3529 g->fops = &idetape_block_ops;
3530 ide_register_region(g);
3531
3532 return 0;
8604affd 3533
1da177e4
LT
3534out_free_tape:
3535 kfree(tape);
3536failed:
8604affd 3537 return -ENODEV;
1da177e4
LT
3538}
3539
5a04cfa9 3540static void __exit idetape_exit(void)
1da177e4 3541{
8604affd 3542 driver_unregister(&idetape_driver.gen_driver);
d5dee80a 3543 class_destroy(idetape_sysfs_class);
1da177e4
LT
3544 unregister_chrdev(IDETAPE_MAJOR, "ht");
3545}
3546
17514e8a 3547static int __init idetape_init(void)
1da177e4 3548{
d5dee80a
WD
3549 int error = 1;
3550 idetape_sysfs_class = class_create(THIS_MODULE, "ide_tape");
3551 if (IS_ERR(idetape_sysfs_class)) {
3552 idetape_sysfs_class = NULL;
3553 printk(KERN_ERR "Unable to create sysfs class for ide tapes\n");
3554 error = -EBUSY;
3555 goto out;
3556 }
3557
1da177e4 3558 if (register_chrdev(IDETAPE_MAJOR, "ht", &idetape_fops)) {
5a04cfa9
BP
3559 printk(KERN_ERR "ide-tape: Failed to register chrdev"
3560 " interface\n");
d5dee80a
WD
3561 error = -EBUSY;
3562 goto out_free_class;
1da177e4 3563 }
d5dee80a
WD
3564
3565 error = driver_register(&idetape_driver.gen_driver);
3566 if (error)
3567 goto out_free_driver;
3568
3569 return 0;
3570
3571out_free_driver:
3572 driver_unregister(&idetape_driver.gen_driver);
3573out_free_class:
3574 class_destroy(idetape_sysfs_class);
3575out:
3576 return error;
1da177e4
LT
3577}
3578
263756ec 3579MODULE_ALIAS("ide:*m-tape*");
1da177e4
LT
3580module_init(idetape_init);
3581module_exit(idetape_exit);
3582MODULE_ALIAS_CHARDEV_MAJOR(IDETAPE_MAJOR);
9c145768
BP
3583MODULE_DESCRIPTION("ATAPI Streaming TAPE Driver");
3584MODULE_LICENSE("GPL");
This page took 0.601823 seconds and 5 git commands to generate.