]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - drivers/ide/ide-tape.c
ide-generic: handle probing of legacy io-ports v5
[mirror_ubuntu-bionic-kernel.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
51509eec
BZ
18#define DRV_NAME "ide-tape"
19
dfe79936 20#define IDETAPE_VERSION "1.20"
1da177e4 21
1da177e4
LT
22#include <linux/module.h>
23#include <linux/types.h>
24#include <linux/string.h>
25#include <linux/kernel.h>
26#include <linux/delay.h>
27#include <linux/timer.h>
28#include <linux/mm.h>
29#include <linux/interrupt.h>
9bae1ff3 30#include <linux/jiffies.h>
1da177e4 31#include <linux/major.h>
1da177e4
LT
32#include <linux/errno.h>
33#include <linux/genhd.h>
34#include <linux/slab.h>
35#include <linux/pci.h>
36#include <linux/ide.h>
37#include <linux/smp_lock.h>
38#include <linux/completion.h>
39#include <linux/bitops.h>
cf8b8975 40#include <linux/mutex.h>
90699ce2 41#include <scsi/scsi.h>
1da177e4
LT
42
43#include <asm/byteorder.h>
c837cfa5
BP
44#include <linux/irq.h>
45#include <linux/uaccess.h>
46#include <linux/io.h>
1da177e4 47#include <asm/unaligned.h>
1da177e4
LT
48#include <linux/mtio.h>
49
8004a8c9
BP
50enum {
51 /* output errors only */
52 DBG_ERR = (1 << 0),
53 /* output all sense key/asc */
54 DBG_SENSE = (1 << 1),
55 /* info regarding all chrdev-related procedures */
56 DBG_CHRDEV = (1 << 2),
57 /* all remaining procedures */
58 DBG_PROCS = (1 << 3),
59 /* buffer alloc info (pc_stack & rq_stack) */
60 DBG_PCRQ_STACK = (1 << 4),
61};
62
63/* define to see debug info */
64#define IDETAPE_DEBUG_LOG 0
65
66#if IDETAPE_DEBUG_LOG
67#define debug_log(lvl, fmt, args...) \
68{ \
69 if (tape->debug_mask & lvl) \
70 printk(KERN_INFO "ide-tape: " fmt, ## args); \
71}
72#else
73#define debug_log(lvl, fmt, args...) do {} while (0)
74#endif
75
1da177e4 76/**************************** Tunable parameters *****************************/
1da177e4 77/*
3c98bf34
BP
78 * After each failed packet command we issue a request sense command and retry
79 * the packet command IDETAPE_MAX_PC_RETRIES times.
1da177e4 80 *
3c98bf34 81 * Setting IDETAPE_MAX_PC_RETRIES to 0 will disable retries.
1da177e4
LT
82 */
83#define IDETAPE_MAX_PC_RETRIES 3
84
85/*
3c98bf34
BP
86 * With each packet command, we allocate a buffer of IDETAPE_PC_BUFFER_SIZE
87 * bytes. This is used for several packet commands (Not for READ/WRITE commands)
1da177e4
LT
88 */
89#define IDETAPE_PC_BUFFER_SIZE 256
90
91/*
92 * In various places in the driver, we need to allocate storage
93 * for packet commands and requests, which will remain valid while
94 * we leave the driver to wait for an interrupt or a timeout event.
95 */
96#define IDETAPE_PC_STACK (10 + IDETAPE_MAX_PC_RETRIES)
97
98/*
99 * Some drives (for example, Seagate STT3401A Travan) require a very long
100 * timeout, because they don't return an interrupt or clear their busy bit
101 * until after the command completes (even retension commands).
102 */
103#define IDETAPE_WAIT_CMD (900*HZ)
104
105/*
3c98bf34
BP
106 * The following parameter is used to select the point in the internal tape fifo
107 * in which we will start to refill the buffer. Decreasing the following
108 * parameter will improve the system's latency and interactive response, while
109 * using a high value might improve system throughput.
1da177e4 110 */
3c98bf34 111#define IDETAPE_FIFO_THRESHOLD 2
1da177e4
LT
112
113/*
3c98bf34 114 * DSC polling parameters.
1da177e4 115 *
3c98bf34
BP
116 * Polling for DSC (a single bit in the status register) is a very important
117 * function in ide-tape. There are two cases in which we poll for DSC:
1da177e4 118 *
3c98bf34
BP
119 * 1. Before a read/write packet command, to ensure that we can transfer data
120 * from/to the tape's data buffers, without causing an actual media access.
121 * In case the tape is not ready yet, we take out our request from the device
122 * request queue, so that ide.c could service requests from the other device
123 * on the same interface in the meantime.
1da177e4 124 *
3c98bf34
BP
125 * 2. After the successful initialization of a "media access packet command",
126 * which is a command that can take a long time to complete (the interval can
127 * range from several seconds to even an hour). Again, we postpone our request
128 * in the middle to free the bus for the other device. The polling frequency
129 * here should be lower than the read/write frequency since those media access
130 * commands are slow. We start from a "fast" frequency - IDETAPE_DSC_MA_FAST
131 * (1 second), and if we don't receive DSC after IDETAPE_DSC_MA_THRESHOLD
132 * (5 min), we switch it to a lower frequency - IDETAPE_DSC_MA_SLOW (1 min).
1da177e4 133 *
3c98bf34
BP
134 * We also set a timeout for the timer, in case something goes wrong. The
135 * timeout should be longer then the maximum execution time of a tape operation.
1da177e4 136 */
3c98bf34
BP
137
138/* DSC timings. */
1da177e4
LT
139#define IDETAPE_DSC_RW_MIN 5*HZ/100 /* 50 msec */
140#define IDETAPE_DSC_RW_MAX 40*HZ/100 /* 400 msec */
141#define IDETAPE_DSC_RW_TIMEOUT 2*60*HZ /* 2 minutes */
142#define IDETAPE_DSC_MA_FAST 2*HZ /* 2 seconds */
143#define IDETAPE_DSC_MA_THRESHOLD 5*60*HZ /* 5 minutes */
144#define IDETAPE_DSC_MA_SLOW 30*HZ /* 30 seconds */
145#define IDETAPE_DSC_MA_TIMEOUT 2*60*60*HZ /* 2 hours */
146
147/*************************** End of tunable parameters ***********************/
148
54abf37e
BP
149/* tape directions */
150enum {
151 IDETAPE_DIR_NONE = (1 << 0),
152 IDETAPE_DIR_READ = (1 << 1),
153 IDETAPE_DIR_WRITE = (1 << 2),
154};
1da177e4
LT
155
156struct idetape_bh {
ab057968 157 u32 b_size;
1da177e4
LT
158 atomic_t b_count;
159 struct idetape_bh *b_reqnext;
160 char *b_data;
161};
162
03056b90
BP
163/* Tape door status */
164#define DOOR_UNLOCKED 0
165#define DOOR_LOCKED 1
166#define DOOR_EXPLICITLY_LOCKED 2
167
168/* Some defines for the SPACE command */
169#define IDETAPE_SPACE_OVER_FILEMARK 1
170#define IDETAPE_SPACE_TO_EOD 3
171
172/* Some defines for the LOAD UNLOAD command */
173#define IDETAPE_LU_LOAD_MASK 1
174#define IDETAPE_LU_RETENSION_MASK 2
175#define IDETAPE_LU_EOT_MASK 4
176
177/*
178 * Special requests for our block device strategy routine.
179 *
180 * In order to service a character device command, we add special requests to
181 * the tail of our block device request queue and wait for their completion.
182 */
183
184enum {
185 REQ_IDETAPE_PC1 = (1 << 0), /* packet command (first stage) */
186 REQ_IDETAPE_PC2 = (1 << 1), /* packet command (second stage) */
187 REQ_IDETAPE_READ = (1 << 2),
188 REQ_IDETAPE_WRITE = (1 << 3),
189};
190
191/* Error codes returned in rq->errors to the higher part of the driver. */
192#define IDETAPE_ERROR_GENERAL 101
193#define IDETAPE_ERROR_FILEMARK 102
194#define IDETAPE_ERROR_EOD 103
195
196/* Structures related to the SELECT SENSE / MODE SENSE packet commands. */
197#define IDETAPE_BLOCK_DESCRIPTOR 0
198#define IDETAPE_CAPABILITIES_PAGE 0x2a
199
1da177e4 200/*
3c98bf34
BP
201 * Most of our global data which we need to save even as we leave the driver due
202 * to an interrupt or a timer event is stored in the struct defined below.
1da177e4
LT
203 */
204typedef struct ide_tape_obj {
205 ide_drive_t *drive;
206 ide_driver_t *driver;
207 struct gendisk *disk;
208 struct kref kref;
209
210 /*
211 * Since a typical character device operation requires more
212 * than one packet command, we provide here enough memory
213 * for the maximum of interconnected packet commands.
214 * The packet commands are stored in the circular array pc_stack.
215 * pc_stack_index points to the last used entry, and warps around
216 * to the start when we get to the last array entry.
217 *
218 * pc points to the current processed packet command.
219 *
220 * failed_pc points to the last failed packet command, or contains
221 * NULL if we do not need to retry any packet command. This is
222 * required since an additional packet command is needed before the
223 * retry, to get detailed information on what went wrong.
224 */
225 /* Current packet command */
d236d74c 226 struct ide_atapi_pc *pc;
1da177e4 227 /* Last failed packet command */
d236d74c 228 struct ide_atapi_pc *failed_pc;
1da177e4 229 /* Packet command stack */
d236d74c 230 struct ide_atapi_pc pc_stack[IDETAPE_PC_STACK];
1da177e4
LT
231 /* Next free packet command storage space */
232 int pc_stack_index;
233 struct request rq_stack[IDETAPE_PC_STACK];
234 /* We implement a circular array */
235 int rq_stack_index;
236
237 /*
3c98bf34 238 * DSC polling variables.
1da177e4 239 *
3c98bf34
BP
240 * While polling for DSC we use postponed_rq to postpone the current
241 * request so that ide.c will be able to service pending requests on the
242 * other device. Note that at most we will have only one DSC (usually
5bd50dc6 243 * data transfer) request in the device request queue.
1da177e4
LT
244 */
245 struct request *postponed_rq;
246 /* The time in which we started polling for DSC */
247 unsigned long dsc_polling_start;
248 /* Timer used to poll for dsc */
249 struct timer_list dsc_timer;
250 /* Read/Write dsc polling frequency */
54bb2074
BP
251 unsigned long best_dsc_rw_freq;
252 unsigned long dsc_poll_freq;
1da177e4
LT
253 unsigned long dsc_timeout;
254
3c98bf34 255 /* Read position information */
1da177e4
LT
256 u8 partition;
257 /* Current block */
54bb2074 258 unsigned int first_frame;
1da177e4 259
3c98bf34 260 /* Last error information */
1da177e4
LT
261 u8 sense_key, asc, ascq;
262
3c98bf34 263 /* Character device operation */
1da177e4
LT
264 unsigned int minor;
265 /* device name */
266 char name[4];
267 /* Current character device data transfer direction */
54abf37e 268 u8 chrdev_dir;
1da177e4 269
54bb2074
BP
270 /* tape block size, usually 512 or 1024 bytes */
271 unsigned short blk_size;
1da177e4 272 int user_bs_factor;
b6422013 273
1da177e4 274 /* Copy of the tape's Capabilities and Mechanical Page */
b6422013 275 u8 caps[20];
1da177e4
LT
276
277 /*
3c98bf34 278 * Active data transfer request parameters.
1da177e4 279 *
3c98bf34
BP
280 * At most, there is only one ide-tape originated data transfer request
281 * in the device request queue. This allows ide.c to easily service
282 * requests from the other device when we postpone our active request.
1da177e4 283 */
83042b24 284
3c98bf34 285 /* Data buffer size chosen based on the tape's recommendation */
f73850a3 286 int buffer_size;
077e3bdb
BP
287 /* merge buffer */
288 struct idetape_bh *merge_bh;
01a63aeb
BP
289 /* size of the merge buffer */
290 int merge_bh_size;
077e3bdb 291 /* pointer to current buffer head within the merge buffer */
1da177e4
LT
292 struct idetape_bh *bh;
293 char *b_data;
294 int b_count;
3c98bf34 295
a997a435 296 int pages_per_buffer;
1da177e4
LT
297 /* Wasted space in each stage */
298 int excess_bh_size;
299
1da177e4 300 /* protects the ide-tape queue */
54bb2074 301 spinlock_t lock;
1da177e4 302
3c98bf34 303 /* Measures average tape speed */
1da177e4
LT
304 unsigned long avg_time;
305 int avg_size;
306 int avg_speed;
307
1da177e4
LT
308 /* the door is currently locked */
309 int door_locked;
310 /* the tape hardware is write protected */
311 char drv_write_prot;
312 /* the tape is write protected (hardware or opened as read-only) */
313 char write_prot;
314
8004a8c9 315 u32 debug_mask;
1da177e4
LT
316} idetape_tape_t;
317
cf8b8975 318static DEFINE_MUTEX(idetape_ref_mutex);
1da177e4 319
d5dee80a
WD
320static struct class *idetape_sysfs_class;
321
1da177e4
LT
322#define to_ide_tape(obj) container_of(obj, struct ide_tape_obj, kref)
323
324#define ide_tape_g(disk) \
325 container_of((disk)->private_data, struct ide_tape_obj, driver)
326
08da591e
BZ
327static void ide_tape_release(struct kref *);
328
1da177e4
LT
329static struct ide_tape_obj *ide_tape_get(struct gendisk *disk)
330{
331 struct ide_tape_obj *tape = NULL;
332
cf8b8975 333 mutex_lock(&idetape_ref_mutex);
1da177e4 334 tape = ide_tape_g(disk);
08da591e 335 if (tape) {
d3e33ff5 336 if (ide_device_get(tape->drive))
08da591e 337 tape = NULL;
d3e33ff5
BZ
338 else
339 kref_get(&tape->kref);
08da591e 340 }
cf8b8975 341 mutex_unlock(&idetape_ref_mutex);
1da177e4
LT
342 return tape;
343}
344
1da177e4
LT
345static void ide_tape_put(struct ide_tape_obj *tape)
346{
d3e33ff5
BZ
347 ide_drive_t *drive = tape->drive;
348
cf8b8975 349 mutex_lock(&idetape_ref_mutex);
1da177e4 350 kref_put(&tape->kref, ide_tape_release);
d3e33ff5 351 ide_device_put(drive);
cf8b8975 352 mutex_unlock(&idetape_ref_mutex);
1da177e4
LT
353}
354
1da177e4 355/*
3c98bf34
BP
356 * The variables below are used for the character device interface. Additional
357 * state variables are defined in our ide_drive_t structure.
1da177e4 358 */
5a04cfa9 359static struct ide_tape_obj *idetape_devs[MAX_HWIFS * MAX_DRIVES];
1da177e4
LT
360
361#define ide_tape_f(file) ((file)->private_data)
362
363static struct ide_tape_obj *ide_tape_chrdev_get(unsigned int i)
364{
365 struct ide_tape_obj *tape = NULL;
366
cf8b8975 367 mutex_lock(&idetape_ref_mutex);
1da177e4
LT
368 tape = idetape_devs[i];
369 if (tape)
370 kref_get(&tape->kref);
cf8b8975 371 mutex_unlock(&idetape_ref_mutex);
1da177e4
LT
372 return tape;
373}
374
d236d74c 375static void idetape_input_buffers(ide_drive_t *drive, struct ide_atapi_pc *pc,
5a04cfa9 376 unsigned int bcount)
1da177e4
LT
377{
378 struct idetape_bh *bh = pc->bh;
379 int count;
380
381 while (bcount) {
1da177e4
LT
382 if (bh == NULL) {
383 printk(KERN_ERR "ide-tape: bh == NULL in "
384 "idetape_input_buffers\n");
9f87abe8 385 ide_pad_transfer(drive, 0, bcount);
1da177e4
LT
386 return;
387 }
5a04cfa9
BP
388 count = min(
389 (unsigned int)(bh->b_size - atomic_read(&bh->b_count)),
390 bcount);
374e042c 391 drive->hwif->tp_ops->input_data(drive, NULL, bh->b_data +
5a04cfa9 392 atomic_read(&bh->b_count), count);
1da177e4
LT
393 bcount -= count;
394 atomic_add(count, &bh->b_count);
395 if (atomic_read(&bh->b_count) == bh->b_size) {
396 bh = bh->b_reqnext;
397 if (bh)
398 atomic_set(&bh->b_count, 0);
399 }
400 }
401 pc->bh = bh;
402}
403
d236d74c 404static void idetape_output_buffers(ide_drive_t *drive, struct ide_atapi_pc *pc,
5a04cfa9 405 unsigned int bcount)
1da177e4
LT
406{
407 struct idetape_bh *bh = pc->bh;
408 int count;
409
410 while (bcount) {
1da177e4 411 if (bh == NULL) {
5a04cfa9
BP
412 printk(KERN_ERR "ide-tape: bh == NULL in %s\n",
413 __func__);
1da177e4
LT
414 return;
415 }
1da177e4 416 count = min((unsigned int)pc->b_count, (unsigned int)bcount);
374e042c 417 drive->hwif->tp_ops->output_data(drive, NULL, pc->b_data, count);
1da177e4
LT
418 bcount -= count;
419 pc->b_data += count;
420 pc->b_count -= count;
421 if (!pc->b_count) {
5a04cfa9
BP
422 bh = bh->b_reqnext;
423 pc->bh = bh;
1da177e4
LT
424 if (bh) {
425 pc->b_data = bh->b_data;
426 pc->b_count = atomic_read(&bh->b_count);
427 }
428 }
429 }
430}
431
646c0cb6 432static void idetape_update_buffers(ide_drive_t *drive, struct ide_atapi_pc *pc)
1da177e4
LT
433{
434 struct idetape_bh *bh = pc->bh;
435 int count;
d236d74c 436 unsigned int bcount = pc->xferred;
1da177e4 437
346331f8 438 if (pc->flags & PC_FLAG_WRITING)
1da177e4
LT
439 return;
440 while (bcount) {
1da177e4 441 if (bh == NULL) {
5a04cfa9
BP
442 printk(KERN_ERR "ide-tape: bh == NULL in %s\n",
443 __func__);
1da177e4
LT
444 return;
445 }
1da177e4
LT
446 count = min((unsigned int)bh->b_size, (unsigned int)bcount);
447 atomic_set(&bh->b_count, count);
448 if (atomic_read(&bh->b_count) == bh->b_size)
449 bh = bh->b_reqnext;
450 bcount -= count;
451 }
452 pc->bh = bh;
453}
454
455/*
456 * idetape_next_pc_storage returns a pointer to a place in which we can
457 * safely store a packet command, even though we intend to leave the
458 * driver. A storage space for a maximum of IDETAPE_PC_STACK packet
459 * commands is allocated at initialization time.
460 */
d236d74c 461static struct ide_atapi_pc *idetape_next_pc_storage(ide_drive_t *drive)
1da177e4
LT
462{
463 idetape_tape_t *tape = drive->driver_data;
464
8004a8c9
BP
465 debug_log(DBG_PCRQ_STACK, "pc_stack_index=%d\n", tape->pc_stack_index);
466
1da177e4 467 if (tape->pc_stack_index == IDETAPE_PC_STACK)
5a04cfa9 468 tape->pc_stack_index = 0;
1da177e4
LT
469 return (&tape->pc_stack[tape->pc_stack_index++]);
470}
471
472/*
473 * idetape_next_rq_storage is used along with idetape_next_pc_storage.
474 * Since we queue packet commands in the request queue, we need to
475 * allocate a request, along with the allocation of a packet command.
476 */
5a04cfa9 477
1da177e4
LT
478/**************************************************************
479 * *
480 * This should get fixed to use kmalloc(.., GFP_ATOMIC) *
481 * followed later on by kfree(). -ml *
482 * *
483 **************************************************************/
5a04cfa9
BP
484
485static struct request *idetape_next_rq_storage(ide_drive_t *drive)
1da177e4
LT
486{
487 idetape_tape_t *tape = drive->driver_data;
488
8004a8c9
BP
489 debug_log(DBG_PCRQ_STACK, "rq_stack_index=%d\n", tape->rq_stack_index);
490
1da177e4 491 if (tape->rq_stack_index == IDETAPE_PC_STACK)
5a04cfa9 492 tape->rq_stack_index = 0;
1da177e4
LT
493 return (&tape->rq_stack[tape->rq_stack_index++]);
494}
495
1da177e4 496/*
1b5db434
BP
497 * called on each failed packet command retry to analyze the request sense. We
498 * currently do not utilize this information.
1da177e4 499 */
1b5db434 500static void idetape_analyze_error(ide_drive_t *drive, u8 *sense)
1da177e4
LT
501{
502 idetape_tape_t *tape = drive->driver_data;
d236d74c 503 struct ide_atapi_pc *pc = tape->failed_pc;
1da177e4 504
1b5db434
BP
505 tape->sense_key = sense[2] & 0xF;
506 tape->asc = sense[12];
507 tape->ascq = sense[13];
8004a8c9
BP
508
509 debug_log(DBG_ERR, "pc = %x, sense key = %x, asc = %x, ascq = %x\n",
510 pc->c[0], tape->sense_key, tape->asc, tape->ascq);
1da177e4 511
d236d74c 512 /* Correct pc->xferred by asking the tape. */
346331f8 513 if (pc->flags & PC_FLAG_DMA_ERROR) {
d236d74c 514 pc->xferred = pc->req_xfer -
54bb2074 515 tape->blk_size *
5d0cc8ae 516 get_unaligned_be32(&sense[3]);
646c0cb6 517 idetape_update_buffers(drive, pc);
1da177e4
LT
518 }
519
520 /*
521 * If error was the result of a zero-length read or write command,
522 * with sense key=5, asc=0x22, ascq=0, let it slide. Some drives
523 * (i.e. Seagate STT3401A Travan) don't support 0-length read/writes.
524 */
90699ce2 525 if ((pc->c[0] == READ_6 || pc->c[0] == WRITE_6)
1b5db434
BP
526 /* length == 0 */
527 && pc->c[4] == 0 && pc->c[3] == 0 && pc->c[2] == 0) {
528 if (tape->sense_key == 5) {
1da177e4
LT
529 /* don't report an error, everything's ok */
530 pc->error = 0;
531 /* don't retry read/write */
346331f8 532 pc->flags |= PC_FLAG_ABORT;
1da177e4
LT
533 }
534 }
90699ce2 535 if (pc->c[0] == READ_6 && (sense[2] & 0x80)) {
1da177e4 536 pc->error = IDETAPE_ERROR_FILEMARK;
346331f8 537 pc->flags |= PC_FLAG_ABORT;
1da177e4 538 }
90699ce2 539 if (pc->c[0] == WRITE_6) {
1b5db434
BP
540 if ((sense[2] & 0x40) || (tape->sense_key == 0xd
541 && tape->asc == 0x0 && tape->ascq == 0x2)) {
1da177e4 542 pc->error = IDETAPE_ERROR_EOD;
346331f8 543 pc->flags |= PC_FLAG_ABORT;
1da177e4
LT
544 }
545 }
90699ce2 546 if (pc->c[0] == READ_6 || pc->c[0] == WRITE_6) {
1b5db434 547 if (tape->sense_key == 8) {
1da177e4 548 pc->error = IDETAPE_ERROR_EOD;
346331f8 549 pc->flags |= PC_FLAG_ABORT;
1da177e4 550 }
346331f8 551 if (!(pc->flags & PC_FLAG_ABORT) &&
d236d74c 552 pc->xferred)
1da177e4
LT
553 pc->retries = IDETAPE_MAX_PC_RETRIES + 1;
554 }
555}
556
d01dbc3b 557/* Free data buffers completely. */
077e3bdb 558static void ide_tape_kfree_buffer(idetape_tape_t *tape)
1da177e4 559{
077e3bdb 560 struct idetape_bh *prev_bh, *bh = tape->merge_bh;
d01dbc3b
BP
561
562 while (bh) {
563 u32 size = bh->b_size;
564
565 while (size) {
566 unsigned int order = fls(size >> PAGE_SHIFT)-1;
567
568 if (bh->b_data)
569 free_pages((unsigned long)bh->b_data, order);
570
571 size &= (order-1);
572 bh->b_data += (1 << order) * PAGE_SIZE;
1da177e4
LT
573 }
574 prev_bh = bh;
575 bh = bh->b_reqnext;
576 kfree(prev_bh);
577 }
1da177e4
LT
578}
579
1da177e4
LT
580static int idetape_end_request(ide_drive_t *drive, int uptodate, int nr_sects)
581{
582 struct request *rq = HWGROUP(drive)->rq;
583 idetape_tape_t *tape = drive->driver_data;
584 unsigned long flags;
585 int error;
1da177e4 586
8004a8c9 587 debug_log(DBG_PROCS, "Enter %s\n", __func__);
1da177e4
LT
588
589 switch (uptodate) {
5a04cfa9
BP
590 case 0: error = IDETAPE_ERROR_GENERAL; break;
591 case 1: error = 0; break;
592 default: error = uptodate;
1da177e4
LT
593 }
594 rq->errors = error;
595 if (error)
596 tape->failed_pc = NULL;
597
3687221f
BZ
598 if (!blk_special_request(rq)) {
599 ide_end_request(drive, uptodate, nr_sects);
600 return 0;
601 }
602
54bb2074 603 spin_lock_irqsave(&tape->lock, flags);
1da177e4 604
1da177e4 605 ide_end_drive_cmd(drive, 0, 0);
1da177e4 606
54bb2074 607 spin_unlock_irqrestore(&tape->lock, flags);
1da177e4
LT
608 return 0;
609}
610
92f5daff 611static void ide_tape_callback(ide_drive_t *drive)
1da177e4
LT
612{
613 idetape_tape_t *tape = drive->driver_data;
5985e6ab
BZ
614 struct ide_atapi_pc *pc = tape->pc;
615 int uptodate = pc->error ? 0 : 1;
1da177e4 616
8004a8c9
BP
617 debug_log(DBG_PROCS, "Enter %s\n", __func__);
618
dd2e9a03
BZ
619 if (tape->failed_pc == pc)
620 tape->failed_pc = NULL;
621
5985e6ab
BZ
622 if (pc->c[0] == REQUEST_SENSE) {
623 if (uptodate)
624 idetape_analyze_error(drive, pc->buf);
625 else
626 printk(KERN_ERR "ide-tape: Error in REQUEST SENSE "
627 "itself - Aborting request!\n");
628 } else if (pc->c[0] == READ_6 || pc->c[0] == WRITE_6) {
629 struct request *rq = drive->hwif->hwgroup->rq;
630 int blocks = pc->xferred / tape->blk_size;
631
632 tape->avg_size += blocks * tape->blk_size;
633
634 if (time_after_eq(jiffies, tape->avg_time + HZ)) {
635 tape->avg_speed = tape->avg_size * HZ /
636 (jiffies - tape->avg_time) / 1024;
637 tape->avg_size = 0;
638 tape->avg_time = jiffies;
639 }
640
641 tape->first_frame += blocks;
642 rq->current_nr_sectors -= blocks;
643
644 if (pc->error)
645 uptodate = pc->error;
646 } else if (pc->c[0] == READ_POSITION && uptodate) {
647 u8 *readpos = tape->pc->buf;
648
649 debug_log(DBG_SENSE, "BOP - %s\n",
650 (readpos[0] & 0x80) ? "Yes" : "No");
651 debug_log(DBG_SENSE, "EOP - %s\n",
652 (readpos[0] & 0x40) ? "Yes" : "No");
653
654 if (readpos[0] & 0x4) {
655 printk(KERN_INFO "ide-tape: Block location is unknown"
656 "to the tape\n");
f2e3ab52 657 clear_bit(IDE_AFLAG_ADDRESS_VALID, &drive->atapi_flags);
5985e6ab
BZ
658 uptodate = 0;
659 } else {
660 debug_log(DBG_SENSE, "Block Location - %u\n",
cd740ab0 661 be32_to_cpup((__be32 *)&readpos[4]));
5985e6ab
BZ
662
663 tape->partition = readpos[1];
cd740ab0 664 tape->first_frame = be32_to_cpup((__be32 *)&readpos[4]);
f2e3ab52 665 set_bit(IDE_AFLAG_ADDRESS_VALID, &drive->atapi_flags);
5985e6ab 666 }
1da177e4 667 }
5985e6ab
BZ
668
669 idetape_end_request(drive, uptodate, 0);
1da177e4
LT
670}
671
5985e6ab
BZ
672static void idetape_init_pc(struct ide_atapi_pc *pc)
673{
674 memset(pc->c, 0, 12);
675 pc->retries = 0;
676 pc->flags = 0;
677 pc->req_xfer = 0;
678 pc->buf = pc->pc_buf;
679 pc->buf_size = IDETAPE_PC_BUFFER_SIZE;
680 pc->bh = NULL;
681 pc->b_data = NULL;
1da177e4
LT
682}
683
d236d74c 684static void idetape_create_request_sense_cmd(struct ide_atapi_pc *pc)
1da177e4 685{
5a04cfa9 686 idetape_init_pc(pc);
90699ce2 687 pc->c[0] = REQUEST_SENSE;
1da177e4 688 pc->c[4] = 20;
d236d74c 689 pc->req_xfer = 20;
1da177e4
LT
690}
691
1da177e4 692/*
3c98bf34
BP
693 * Generate a new packet command request in front of the request queue, before
694 * the current request, so that it will be processed immediately, on the next
695 * pass through the driver. The function below is called from the request
696 * handling part of the driver (the "bottom" part). Safe storage for the request
697 * should be allocated with ide_tape_next_{pc,rq}_storage() prior to that.
1da177e4 698 *
3c98bf34
BP
699 * Memory for those requests is pre-allocated at initialization time, and is
700 * limited to IDETAPE_PC_STACK requests. We assume that we have enough space for
701 * the maximum possible number of inter-dependent packet commands.
1da177e4 702 *
3c98bf34
BP
703 * The higher level of the driver - The ioctl handler and the character device
704 * handling functions should queue request to the lower level part and wait for
705 * their completion using idetape_queue_pc_tail or idetape_queue_rw_tail.
1da177e4 706 */
d236d74c 707static void idetape_queue_pc_head(ide_drive_t *drive, struct ide_atapi_pc *pc,
5a04cfa9 708 struct request *rq)
1da177e4
LT
709{
710 struct ide_tape_obj *tape = drive->driver_data;
711
f025ffdc
BZ
712 blk_rq_init(NULL, rq);
713 rq->cmd_type = REQ_TYPE_SPECIAL;
e8a96aa7 714 rq->cmd_flags |= REQ_PREEMPT;
1da177e4
LT
715 rq->buffer = (char *) pc;
716 rq->rq_disk = tape->disk;
0014c75b 717 memcpy(rq->cmd, pc->c, 12);
f025ffdc 718 rq->cmd[13] = REQ_IDETAPE_PC1;
63f5abb0 719 ide_do_drive_cmd(drive, rq);
1da177e4
LT
720}
721
722/*
723 * idetape_retry_pc is called when an error was detected during the
724 * last packet command. We queue a request sense packet command in
725 * the head of the request list.
726 */
258ec411 727static void idetape_retry_pc(ide_drive_t *drive)
1da177e4 728{
d236d74c 729 struct ide_atapi_pc *pc;
1da177e4 730 struct request *rq;
1da177e4 731
64a57fe4 732 (void)ide_read_error(drive);
1da177e4
LT
733 pc = idetape_next_pc_storage(drive);
734 rq = idetape_next_rq_storage(drive);
735 idetape_create_request_sense_cmd(pc);
f2e3ab52 736 set_bit(IDE_AFLAG_IGNORE_DSC, &drive->atapi_flags);
1da177e4 737 idetape_queue_pc_head(drive, pc, rq);
1da177e4
LT
738}
739
740/*
3c98bf34
BP
741 * Postpone the current request so that ide.c will be able to service requests
742 * from another device on the same hwgroup while we are polling for DSC.
1da177e4 743 */
5a04cfa9 744static void idetape_postpone_request(ide_drive_t *drive)
1da177e4
LT
745{
746 idetape_tape_t *tape = drive->driver_data;
747
8004a8c9
BP
748 debug_log(DBG_PROCS, "Enter %s\n", __func__);
749
1da177e4 750 tape->postponed_rq = HWGROUP(drive)->rq;
54bb2074 751 ide_stall_queue(drive, tape->dsc_poll_freq);
1da177e4
LT
752}
753
74e63e74
BZ
754static void ide_tape_handle_dsc(ide_drive_t *drive)
755{
756 idetape_tape_t *tape = drive->driver_data;
757
758 /* Media access command */
759 tape->dsc_polling_start = jiffies;
760 tape->dsc_poll_freq = IDETAPE_DSC_MA_FAST;
761 tape->dsc_timeout = jiffies + IDETAPE_DSC_MA_TIMEOUT;
762 /* Allow ide.c to handle other requests */
763 idetape_postpone_request(drive);
764}
765
08424ac2
BZ
766static void ide_tape_io_buffers(ide_drive_t *drive, struct ide_atapi_pc *pc,
767 unsigned int bcount, int write)
768{
769 if (write)
770 idetape_output_buffers(drive, pc, bcount);
771 else
772 idetape_input_buffers(drive, pc, bcount);
773}
a1efc85f 774
1da177e4 775/*
a1efc85f
BP
776 * This is the usual interrupt handler which will be called during a packet
777 * command. We will transfer some of the data (as requested by the drive) and
778 * will re-point interrupt handler to us. When data transfer is finished, we
779 * will act according to the algorithm described before
8d06bfad 780 * idetape_issue_pc.
1da177e4 781 */
a1efc85f 782static ide_startstop_t idetape_pc_intr(ide_drive_t *drive)
1da177e4 783{
1da177e4 784 idetape_tape_t *tape = drive->driver_data;
1da177e4 785
646c0cb6
BZ
786 return ide_pc_intr(drive, tape->pc, idetape_pc_intr, IDETAPE_WAIT_CMD,
787 NULL, idetape_update_buffers, idetape_retry_pc,
788 ide_tape_handle_dsc, ide_tape_io_buffers);
1da177e4
LT
789}
790
791/*
3c98bf34 792 * Packet Command Interface
1da177e4 793 *
3c98bf34
BP
794 * The current Packet Command is available in tape->pc, and will not change
795 * until we finish handling it. Each packet command is associated with a
796 * callback function that will be called when the command is finished.
1da177e4 797 *
3c98bf34 798 * The handling will be done in three stages:
1da177e4 799 *
3c98bf34
BP
800 * 1. idetape_issue_pc will send the packet command to the drive, and will set
801 * the interrupt handler to idetape_pc_intr.
1da177e4 802 *
3c98bf34
BP
803 * 2. On each interrupt, idetape_pc_intr will be called. This step will be
804 * repeated until the device signals us that no more interrupts will be issued.
1da177e4 805 *
3c98bf34
BP
806 * 3. ATAPI Tape media access commands have immediate status with a delayed
807 * process. In case of a successful initiation of a media access packet command,
808 * the DSC bit will be set when the actual execution of the command is finished.
809 * Since the tape drive will not issue an interrupt, we have to poll for this
810 * event. In this case, we define the request as "low priority request" by
811 * setting rq_status to IDETAPE_RQ_POSTPONED, set a timer to poll for DSC and
812 * exit the driver.
1da177e4 813 *
3c98bf34
BP
814 * ide.c will then give higher priority to requests which originate from the
815 * other device, until will change rq_status to RQ_ACTIVE.
1da177e4 816 *
3c98bf34 817 * 4. When the packet command is finished, it will be checked for errors.
1da177e4 818 *
3c98bf34
BP
819 * 5. In case an error was found, we queue a request sense packet command in
820 * front of the request queue and retry the operation up to
821 * IDETAPE_MAX_PC_RETRIES times.
1da177e4 822 *
3c98bf34
BP
823 * 6. In case no error was found, or we decided to give up and not to retry
824 * again, the callback function will be called and then we will handle the next
825 * request.
1da177e4
LT
826 */
827static ide_startstop_t idetape_transfer_pc(ide_drive_t *drive)
828{
1da177e4 829 idetape_tape_t *tape = drive->driver_data;
9567b349 830
594c16d8
BZ
831 return ide_transfer_pc(drive, tape->pc, idetape_pc_intr,
832 IDETAPE_WAIT_CMD, NULL);
1da177e4
LT
833}
834
d236d74c
BP
835static ide_startstop_t idetape_issue_pc(ide_drive_t *drive,
836 struct ide_atapi_pc *pc)
1da177e4 837{
1da177e4 838 idetape_tape_t *tape = drive->driver_data;
1da177e4 839
90699ce2
BP
840 if (tape->pc->c[0] == REQUEST_SENSE &&
841 pc->c[0] == REQUEST_SENSE) {
1da177e4
LT
842 printk(KERN_ERR "ide-tape: possible ide-tape.c bug - "
843 "Two request sense in serial were issued\n");
844 }
1da177e4 845
90699ce2 846 if (tape->failed_pc == NULL && pc->c[0] != REQUEST_SENSE)
1da177e4
LT
847 tape->failed_pc = pc;
848 /* Set the current packet command */
849 tape->pc = pc;
850
851 if (pc->retries > IDETAPE_MAX_PC_RETRIES ||
346331f8 852 (pc->flags & PC_FLAG_ABORT)) {
1da177e4 853 /*
3c98bf34
BP
854 * We will "abort" retrying a packet command in case legitimate
855 * error code was received (crossing a filemark, or end of the
856 * media, for example).
1da177e4 857 */
346331f8 858 if (!(pc->flags & PC_FLAG_ABORT)) {
90699ce2 859 if (!(pc->c[0] == TEST_UNIT_READY &&
1da177e4
LT
860 tape->sense_key == 2 && tape->asc == 4 &&
861 (tape->ascq == 1 || tape->ascq == 8))) {
862 printk(KERN_ERR "ide-tape: %s: I/O error, "
863 "pc = %2x, key = %2x, "
864 "asc = %2x, ascq = %2x\n",
865 tape->name, pc->c[0],
866 tape->sense_key, tape->asc,
867 tape->ascq);
868 }
869 /* Giving up */
870 pc->error = IDETAPE_ERROR_GENERAL;
871 }
872 tape->failed_pc = NULL;
776bb027 873 drive->pc_callback(drive);
92f5daff 874 return ide_stopped;
1da177e4 875 }
8004a8c9 876 debug_log(DBG_SENSE, "Retry #%d, cmd = %02X\n", pc->retries, pc->c[0]);
1da177e4
LT
877
878 pc->retries++;
1da177e4 879
6bf1641c
BZ
880 return ide_issue_pc(drive, pc, idetape_transfer_pc,
881 IDETAPE_WAIT_CMD, NULL);
1da177e4
LT
882}
883
3c98bf34 884/* A mode sense command is used to "sense" tape parameters. */
d236d74c 885static void idetape_create_mode_sense_cmd(struct ide_atapi_pc *pc, u8 page_code)
1da177e4
LT
886{
887 idetape_init_pc(pc);
90699ce2 888 pc->c[0] = MODE_SENSE;
1da177e4 889 if (page_code != IDETAPE_BLOCK_DESCRIPTOR)
3c98bf34
BP
890 /* DBD = 1 - Don't return block descriptors */
891 pc->c[1] = 8;
1da177e4
LT
892 pc->c[2] = page_code;
893 /*
894 * Changed pc->c[3] to 0 (255 will at best return unused info).
895 *
896 * For SCSI this byte is defined as subpage instead of high byte
897 * of length and some IDE drives seem to interpret it this way
898 * and return an error when 255 is used.
899 */
900 pc->c[3] = 0;
3c98bf34
BP
901 /* We will just discard data in that case */
902 pc->c[4] = 255;
1da177e4 903 if (page_code == IDETAPE_BLOCK_DESCRIPTOR)
d236d74c 904 pc->req_xfer = 12;
1da177e4 905 else if (page_code == IDETAPE_CAPABILITIES_PAGE)
d236d74c 906 pc->req_xfer = 24;
1da177e4 907 else
d236d74c 908 pc->req_xfer = 50;
1da177e4
LT
909}
910
5a04cfa9 911static ide_startstop_t idetape_media_access_finished(ide_drive_t *drive)
1da177e4 912{
b73c7ee2 913 ide_hwif_t *hwif = drive->hwif;
1da177e4 914 idetape_tape_t *tape = drive->driver_data;
d236d74c 915 struct ide_atapi_pc *pc = tape->pc;
22c525b9 916 u8 stat;
1da177e4 917
374e042c 918 stat = hwif->tp_ops->read_status(hwif);
c47137a9 919
3a7d2484
BZ
920 if (stat & ATA_DSC) {
921 if (stat & ATA_ERR) {
1da177e4 922 /* Error detected */
90699ce2 923 if (pc->c[0] != TEST_UNIT_READY)
1da177e4
LT
924 printk(KERN_ERR "ide-tape: %s: I/O error, ",
925 tape->name);
926 /* Retry operation */
258ec411
BZ
927 idetape_retry_pc(drive);
928 return ide_stopped;
1da177e4
LT
929 }
930 pc->error = 0;
1da177e4
LT
931 } else {
932 pc->error = IDETAPE_ERROR_GENERAL;
933 tape->failed_pc = NULL;
934 }
776bb027 935 drive->pc_callback(drive);
1da177e4
LT
936 return ide_stopped;
937}
938
cd2abbfe 939static void ide_tape_create_rw_cmd(idetape_tape_t *tape,
0014c75b
BP
940 struct ide_atapi_pc *pc, struct request *rq,
941 u8 opcode)
1da177e4 942{
0014c75b
BP
943 struct idetape_bh *bh = (struct idetape_bh *)rq->special;
944 unsigned int length = rq->current_nr_sectors;
945
1da177e4 946 idetape_init_pc(pc);
860ff5ec 947 put_unaligned(cpu_to_be32(length), (unsigned int *) &pc->c[1]);
1da177e4 948 pc->c[1] = 1;
1da177e4 949 pc->bh = bh;
d236d74c
BP
950 pc->buf = NULL;
951 pc->buf_size = length * tape->blk_size;
952 pc->req_xfer = pc->buf_size;
f73850a3 953 if (pc->req_xfer == tape->buffer_size)
5e331095 954 pc->flags |= PC_FLAG_DMA_OK;
1da177e4 955
cd2abbfe
BP
956 if (opcode == READ_6) {
957 pc->c[0] = READ_6;
958 atomic_set(&bh->b_count, 0);
959 } else if (opcode == WRITE_6) {
960 pc->c[0] = WRITE_6;
961 pc->flags |= PC_FLAG_WRITING;
962 pc->b_data = bh->b_data;
963 pc->b_count = atomic_read(&bh->b_count);
964 }
0014c75b
BP
965
966 memcpy(rq->cmd, pc->c, 12);
1da177e4
LT
967}
968
1da177e4
LT
969static ide_startstop_t idetape_do_request(ide_drive_t *drive,
970 struct request *rq, sector_t block)
971{
b73c7ee2 972 ide_hwif_t *hwif = drive->hwif;
1da177e4 973 idetape_tape_t *tape = drive->driver_data;
d236d74c 974 struct ide_atapi_pc *pc = NULL;
1da177e4 975 struct request *postponed_rq = tape->postponed_rq;
22c525b9 976 u8 stat;
1da177e4 977
730616b2
MW
978 debug_log(DBG_SENSE, "sector: %llu, nr_sectors: %lu,"
979 " current_nr_sectors: %u\n",
980 (unsigned long long)rq->sector, rq->nr_sectors,
981 rq->current_nr_sectors);
1da177e4 982
4aff5e23 983 if (!blk_special_request(rq)) {
3c98bf34 984 /* We do not support buffer cache originated requests. */
1da177e4 985 printk(KERN_NOTICE "ide-tape: %s: Unsupported request in "
4aff5e23 986 "request queue (%d)\n", drive->name, rq->cmd_type);
1da177e4
LT
987 ide_end_request(drive, 0, 0);
988 return ide_stopped;
989 }
990
3c98bf34 991 /* Retry a failed packet command */
28c7214b
BZ
992 if (tape->failed_pc && tape->pc->c[0] == REQUEST_SENSE) {
993 pc = tape->failed_pc;
994 goto out;
995 }
5a04cfa9 996
1da177e4
LT
997 if (postponed_rq != NULL)
998 if (rq != postponed_rq) {
999 printk(KERN_ERR "ide-tape: ide-tape.c bug - "
1000 "Two DSC requests were queued\n");
1001 idetape_end_request(drive, 0, 0);
1002 return ide_stopped;
1003 }
1da177e4
LT
1004
1005 tape->postponed_rq = NULL;
1006
1007 /*
1008 * If the tape is still busy, postpone our request and service
1009 * the other device meanwhile.
1010 */
374e042c 1011 stat = hwif->tp_ops->read_status(hwif);
1da177e4 1012
83dd5735 1013 if (!drive->dsc_overlap && !(rq->cmd[13] & REQ_IDETAPE_PC2))
f2e3ab52 1014 set_bit(IDE_AFLAG_IGNORE_DSC, &drive->atapi_flags);
1da177e4
LT
1015
1016 if (drive->post_reset == 1) {
f2e3ab52 1017 set_bit(IDE_AFLAG_IGNORE_DSC, &drive->atapi_flags);
1da177e4
LT
1018 drive->post_reset = 0;
1019 }
1020
f2e3ab52 1021 if (!test_and_clear_bit(IDE_AFLAG_IGNORE_DSC, &drive->atapi_flags) &&
3a7d2484 1022 (stat & ATA_DSC) == 0) {
1da177e4
LT
1023 if (postponed_rq == NULL) {
1024 tape->dsc_polling_start = jiffies;
54bb2074 1025 tape->dsc_poll_freq = tape->best_dsc_rw_freq;
1da177e4
LT
1026 tape->dsc_timeout = jiffies + IDETAPE_DSC_RW_TIMEOUT;
1027 } else if (time_after(jiffies, tape->dsc_timeout)) {
1028 printk(KERN_ERR "ide-tape: %s: DSC timeout\n",
1029 tape->name);
83dd5735 1030 if (rq->cmd[13] & REQ_IDETAPE_PC2) {
1da177e4
LT
1031 idetape_media_access_finished(drive);
1032 return ide_stopped;
1033 } else {
1034 return ide_do_reset(drive);
1035 }
5a04cfa9
BP
1036 } else if (time_after(jiffies,
1037 tape->dsc_polling_start +
1038 IDETAPE_DSC_MA_THRESHOLD))
54bb2074 1039 tape->dsc_poll_freq = IDETAPE_DSC_MA_SLOW;
1da177e4
LT
1040 idetape_postpone_request(drive);
1041 return ide_stopped;
1042 }
83dd5735 1043 if (rq->cmd[13] & REQ_IDETAPE_READ) {
1da177e4 1044 pc = idetape_next_pc_storage(drive);
0014c75b 1045 ide_tape_create_rw_cmd(tape, pc, rq, READ_6);
1da177e4
LT
1046 goto out;
1047 }
83dd5735 1048 if (rq->cmd[13] & REQ_IDETAPE_WRITE) {
1da177e4 1049 pc = idetape_next_pc_storage(drive);
0014c75b 1050 ide_tape_create_rw_cmd(tape, pc, rq, WRITE_6);
1da177e4
LT
1051 goto out;
1052 }
83dd5735 1053 if (rq->cmd[13] & REQ_IDETAPE_PC1) {
d236d74c 1054 pc = (struct ide_atapi_pc *) rq->buffer;
83dd5735
BP
1055 rq->cmd[13] &= ~(REQ_IDETAPE_PC1);
1056 rq->cmd[13] |= REQ_IDETAPE_PC2;
1da177e4
LT
1057 goto out;
1058 }
83dd5735 1059 if (rq->cmd[13] & REQ_IDETAPE_PC2) {
1da177e4
LT
1060 idetape_media_access_finished(drive);
1061 return ide_stopped;
1062 }
1063 BUG();
28c7214b 1064
f2e3ab52 1065out:
8d06bfad 1066 return idetape_issue_pc(drive, pc);
1da177e4
LT
1067}
1068
1da177e4 1069/*
41aa1706 1070 * The function below uses __get_free_pages to allocate a data buffer of size
f73850a3 1071 * tape->buffer_size (or a bit more). We attempt to combine sequential pages as
3c98bf34 1072 * much as possible.
1da177e4 1073 *
41aa1706
BP
1074 * It returns a pointer to the newly allocated buffer, or NULL in case of
1075 * failure.
1da177e4 1076 */
077e3bdb
BP
1077static struct idetape_bh *ide_tape_kmalloc_buffer(idetape_tape_t *tape,
1078 int full, int clear)
1da177e4 1079{
077e3bdb 1080 struct idetape_bh *prev_bh, *bh, *merge_bh;
a997a435 1081 int pages = tape->pages_per_buffer;
41aa1706 1082 unsigned int order, b_allocd;
1da177e4
LT
1083 char *b_data = NULL;
1084
077e3bdb
BP
1085 merge_bh = kmalloc(sizeof(struct idetape_bh), GFP_KERNEL);
1086 bh = merge_bh;
1da177e4
LT
1087 if (bh == NULL)
1088 goto abort;
41aa1706
BP
1089
1090 order = fls(pages) - 1;
1091 bh->b_data = (char *) __get_free_pages(GFP_KERNEL, order);
5a04cfa9 1092 if (!bh->b_data)
1da177e4 1093 goto abort;
41aa1706
BP
1094 b_allocd = (1 << order) * PAGE_SIZE;
1095 pages &= (order-1);
1096
1da177e4 1097 if (clear)
41aa1706
BP
1098 memset(bh->b_data, 0, b_allocd);
1099 bh->b_reqnext = NULL;
1100 bh->b_size = b_allocd;
1da177e4
LT
1101 atomic_set(&bh->b_count, full ? bh->b_size : 0);
1102
41aa1706
BP
1103 while (pages) {
1104 order = fls(pages) - 1;
1105 b_data = (char *) __get_free_pages(GFP_KERNEL, order);
5a04cfa9 1106 if (!b_data)
1da177e4 1107 goto abort;
41aa1706
BP
1108 b_allocd = (1 << order) * PAGE_SIZE;
1109
1da177e4 1110 if (clear)
41aa1706
BP
1111 memset(b_data, 0, b_allocd);
1112
1113 /* newly allocated page frames below buffer header or ...*/
1114 if (bh->b_data == b_data + b_allocd) {
1115 bh->b_size += b_allocd;
1116 bh->b_data -= b_allocd;
1da177e4 1117 if (full)
41aa1706 1118 atomic_add(b_allocd, &bh->b_count);
1da177e4
LT
1119 continue;
1120 }
41aa1706 1121 /* they are above the header */
1da177e4 1122 if (b_data == bh->b_data + bh->b_size) {
41aa1706 1123 bh->b_size += b_allocd;
1da177e4 1124 if (full)
41aa1706 1125 atomic_add(b_allocd, &bh->b_count);
1da177e4
LT
1126 continue;
1127 }
1128 prev_bh = bh;
5a04cfa9
BP
1129 bh = kmalloc(sizeof(struct idetape_bh), GFP_KERNEL);
1130 if (!bh) {
41aa1706 1131 free_pages((unsigned long) b_data, order);
1da177e4
LT
1132 goto abort;
1133 }
1134 bh->b_reqnext = NULL;
1135 bh->b_data = b_data;
41aa1706 1136 bh->b_size = b_allocd;
1da177e4
LT
1137 atomic_set(&bh->b_count, full ? bh->b_size : 0);
1138 prev_bh->b_reqnext = bh;
41aa1706
BP
1139
1140 pages &= (order-1);
1da177e4 1141 }
41aa1706 1142
1da177e4
LT
1143 bh->b_size -= tape->excess_bh_size;
1144 if (full)
1145 atomic_sub(tape->excess_bh_size, &bh->b_count);
077e3bdb 1146 return merge_bh;
1da177e4 1147abort:
077e3bdb 1148 ide_tape_kfree_buffer(tape);
1da177e4
LT
1149 return NULL;
1150}
1151
5a04cfa9 1152static int idetape_copy_stage_from_user(idetape_tape_t *tape,
8646c88f 1153 const char __user *buf, int n)
1da177e4
LT
1154{
1155 struct idetape_bh *bh = tape->bh;
1156 int count;
dcd96379 1157 int ret = 0;
1da177e4
LT
1158
1159 while (n) {
1da177e4 1160 if (bh == NULL) {
5a04cfa9
BP
1161 printk(KERN_ERR "ide-tape: bh == NULL in %s\n",
1162 __func__);
dcd96379 1163 return 1;
1da177e4 1164 }
5a04cfa9
BP
1165 count = min((unsigned int)
1166 (bh->b_size - atomic_read(&bh->b_count)),
1167 (unsigned int)n);
1168 if (copy_from_user(bh->b_data + atomic_read(&bh->b_count), buf,
1169 count))
dcd96379 1170 ret = 1;
1da177e4
LT
1171 n -= count;
1172 atomic_add(count, &bh->b_count);
1173 buf += count;
1174 if (atomic_read(&bh->b_count) == bh->b_size) {
1175 bh = bh->b_reqnext;
1176 if (bh)
1177 atomic_set(&bh->b_count, 0);
1178 }
1179 }
1180 tape->bh = bh;
dcd96379 1181 return ret;
1da177e4
LT
1182}
1183
5a04cfa9 1184static int idetape_copy_stage_to_user(idetape_tape_t *tape, char __user *buf,
99d74e61 1185 int n)
1da177e4
LT
1186{
1187 struct idetape_bh *bh = tape->bh;
1188 int count;
dcd96379 1189 int ret = 0;
1da177e4
LT
1190
1191 while (n) {
1da177e4 1192 if (bh == NULL) {
5a04cfa9
BP
1193 printk(KERN_ERR "ide-tape: bh == NULL in %s\n",
1194 __func__);
dcd96379 1195 return 1;
1da177e4 1196 }
1da177e4 1197 count = min(tape->b_count, n);
dcd96379
DW
1198 if (copy_to_user(buf, tape->b_data, count))
1199 ret = 1;
1da177e4
LT
1200 n -= count;
1201 tape->b_data += count;
1202 tape->b_count -= count;
1203 buf += count;
1204 if (!tape->b_count) {
5a04cfa9
BP
1205 bh = bh->b_reqnext;
1206 tape->bh = bh;
1da177e4
LT
1207 if (bh) {
1208 tape->b_data = bh->b_data;
1209 tape->b_count = atomic_read(&bh->b_count);
1210 }
1211 }
1212 }
dcd96379 1213 return ret;
1da177e4
LT
1214}
1215
077e3bdb 1216static void idetape_init_merge_buffer(idetape_tape_t *tape)
1da177e4 1217{
077e3bdb
BP
1218 struct idetape_bh *bh = tape->merge_bh;
1219 tape->bh = tape->merge_bh;
5a04cfa9 1220
54abf37e 1221 if (tape->chrdev_dir == IDETAPE_DIR_WRITE)
1da177e4
LT
1222 atomic_set(&bh->b_count, 0);
1223 else {
1224 tape->b_data = bh->b_data;
1225 tape->b_count = atomic_read(&bh->b_count);
1226 }
1227}
1228
1da177e4 1229/*
3c98bf34
BP
1230 * Write a filemark if write_filemark=1. Flush the device buffers without
1231 * writing a filemark otherwise.
1da177e4 1232 */
5a04cfa9 1233static void idetape_create_write_filemark_cmd(ide_drive_t *drive,
d236d74c 1234 struct ide_atapi_pc *pc, int write_filemark)
1da177e4
LT
1235{
1236 idetape_init_pc(pc);
90699ce2 1237 pc->c[0] = WRITE_FILEMARKS;
1da177e4 1238 pc->c[4] = write_filemark;
346331f8 1239 pc->flags |= PC_FLAG_WAIT_FOR_DSC;
1da177e4
LT
1240}
1241
d236d74c 1242static void idetape_create_test_unit_ready_cmd(struct ide_atapi_pc *pc)
1da177e4
LT
1243{
1244 idetape_init_pc(pc);
90699ce2 1245 pc->c[0] = TEST_UNIT_READY;
1da177e4
LT
1246}
1247
1248/*
3c98bf34
BP
1249 * We add a special packet command request to the tail of the request queue, and
1250 * wait for it to be serviced. This is not to be called from within the request
1251 * handling part of the driver! We allocate here data on the stack and it is
1252 * valid until the request is finished. This is not the case for the bottom part
1253 * of the driver, where we are always leaving the functions to wait for an
1254 * interrupt or a timer event.
1da177e4 1255 *
3c98bf34
BP
1256 * From the bottom part of the driver, we should allocate safe memory using
1257 * idetape_next_pc_storage() and ide_tape_next_rq_storage(), and add the request
1258 * to the request list without waiting for it to be serviced! In that case, we
1259 * usually use idetape_queue_pc_head().
1da177e4 1260 */
ea1ab3d3 1261static int idetape_queue_pc_tail(ide_drive_t *drive, struct ide_atapi_pc *pc)
1da177e4
LT
1262{
1263 struct ide_tape_obj *tape = drive->driver_data;
64ea1b4a
FT
1264 struct request *rq;
1265 int error;
1da177e4 1266
64ea1b4a
FT
1267 rq = blk_get_request(drive->queue, READ, __GFP_WAIT);
1268 rq->cmd_type = REQ_TYPE_SPECIAL;
83dd5735 1269 rq->cmd[13] = REQ_IDETAPE_PC1;
64ea1b4a 1270 rq->buffer = (char *)pc;
0014c75b 1271 memcpy(rq->cmd, pc->c, 12);
64ea1b4a
FT
1272 error = blk_execute_rq(drive->queue, tape->disk, rq, 0);
1273 blk_put_request(rq);
1274 return error;
1da177e4
LT
1275}
1276
d236d74c
BP
1277static void idetape_create_load_unload_cmd(ide_drive_t *drive,
1278 struct ide_atapi_pc *pc, int cmd)
1da177e4
LT
1279{
1280 idetape_init_pc(pc);
90699ce2 1281 pc->c[0] = START_STOP;
1da177e4 1282 pc->c[4] = cmd;
346331f8 1283 pc->flags |= PC_FLAG_WAIT_FOR_DSC;
1da177e4
LT
1284}
1285
1286static int idetape_wait_ready(ide_drive_t *drive, unsigned long timeout)
1287{
1288 idetape_tape_t *tape = drive->driver_data;
d236d74c 1289 struct ide_atapi_pc pc;
1da177e4
LT
1290 int load_attempted = 0;
1291
3c98bf34 1292 /* Wait for the tape to become ready */
f2e3ab52 1293 set_bit(IDE_AFLAG_MEDIUM_PRESENT, &drive->atapi_flags);
1da177e4
LT
1294 timeout += jiffies;
1295 while (time_before(jiffies, timeout)) {
1296 idetape_create_test_unit_ready_cmd(&pc);
ea1ab3d3 1297 if (!idetape_queue_pc_tail(drive, &pc))
1da177e4
LT
1298 return 0;
1299 if ((tape->sense_key == 2 && tape->asc == 4 && tape->ascq == 2)
3c98bf34
BP
1300 || (tape->asc == 0x3A)) {
1301 /* no media */
1da177e4
LT
1302 if (load_attempted)
1303 return -ENOMEDIUM;
5a04cfa9
BP
1304 idetape_create_load_unload_cmd(drive, &pc,
1305 IDETAPE_LU_LOAD_MASK);
ea1ab3d3 1306 idetape_queue_pc_tail(drive, &pc);
1da177e4
LT
1307 load_attempted = 1;
1308 /* not about to be ready */
1309 } else if (!(tape->sense_key == 2 && tape->asc == 4 &&
1310 (tape->ascq == 1 || tape->ascq == 8)))
1311 return -EIO;
80ce45fd 1312 msleep(100);
1da177e4
LT
1313 }
1314 return -EIO;
1315}
1316
5a04cfa9 1317static int idetape_flush_tape_buffers(ide_drive_t *drive)
1da177e4 1318{
d236d74c 1319 struct ide_atapi_pc pc;
1da177e4
LT
1320 int rc;
1321
1322 idetape_create_write_filemark_cmd(drive, &pc, 0);
5a04cfa9
BP
1323 rc = idetape_queue_pc_tail(drive, &pc);
1324 if (rc)
1da177e4
LT
1325 return rc;
1326 idetape_wait_ready(drive, 60 * 5 * HZ);
1327 return 0;
1328}
1329
d236d74c 1330static void idetape_create_read_position_cmd(struct ide_atapi_pc *pc)
1da177e4
LT
1331{
1332 idetape_init_pc(pc);
90699ce2 1333 pc->c[0] = READ_POSITION;
d236d74c 1334 pc->req_xfer = 20;
1da177e4
LT
1335}
1336
5a04cfa9 1337static int idetape_read_position(ide_drive_t *drive)
1da177e4
LT
1338{
1339 idetape_tape_t *tape = drive->driver_data;
d236d74c 1340 struct ide_atapi_pc pc;
1da177e4
LT
1341 int position;
1342
8004a8c9 1343 debug_log(DBG_PROCS, "Enter %s\n", __func__);
1da177e4
LT
1344
1345 idetape_create_read_position_cmd(&pc);
1346 if (idetape_queue_pc_tail(drive, &pc))
1347 return -1;
54bb2074 1348 position = tape->first_frame;
1da177e4
LT
1349 return position;
1350}
1351
d236d74c
BP
1352static void idetape_create_locate_cmd(ide_drive_t *drive,
1353 struct ide_atapi_pc *pc,
5a04cfa9 1354 unsigned int block, u8 partition, int skip)
1da177e4
LT
1355{
1356 idetape_init_pc(pc);
90699ce2 1357 pc->c[0] = POSITION_TO_ELEMENT;
1da177e4 1358 pc->c[1] = 2;
860ff5ec 1359 put_unaligned(cpu_to_be32(block), (unsigned int *) &pc->c[3]);
1da177e4 1360 pc->c[8] = partition;
346331f8 1361 pc->flags |= PC_FLAG_WAIT_FOR_DSC;
1da177e4
LT
1362}
1363
d236d74c
BP
1364static int idetape_create_prevent_cmd(ide_drive_t *drive,
1365 struct ide_atapi_pc *pc, int prevent)
1da177e4
LT
1366{
1367 idetape_tape_t *tape = drive->driver_data;
1368
b6422013
BP
1369 /* device supports locking according to capabilities page */
1370 if (!(tape->caps[6] & 0x01))
1da177e4
LT
1371 return 0;
1372
1373 idetape_init_pc(pc);
90699ce2 1374 pc->c[0] = ALLOW_MEDIUM_REMOVAL;
1da177e4 1375 pc->c[4] = prevent;
1da177e4
LT
1376 return 1;
1377}
1378
ec0fdb01 1379static void __ide_tape_discard_merge_buffer(ide_drive_t *drive)
1da177e4
LT
1380{
1381 idetape_tape_t *tape = drive->driver_data;
1da177e4 1382
54abf37e 1383 if (tape->chrdev_dir != IDETAPE_DIR_READ)
9798630a 1384 return;
1da177e4 1385
f2e3ab52 1386 clear_bit(IDE_AFLAG_FILEMARK, &drive->atapi_flags);
01a63aeb 1387 tape->merge_bh_size = 0;
077e3bdb
BP
1388 if (tape->merge_bh != NULL) {
1389 ide_tape_kfree_buffer(tape);
1390 tape->merge_bh = NULL;
1da177e4
LT
1391 }
1392
54abf37e 1393 tape->chrdev_dir = IDETAPE_DIR_NONE;
1da177e4
LT
1394}
1395
1396/*
3c98bf34
BP
1397 * Position the tape to the requested block using the LOCATE packet command.
1398 * A READ POSITION command is then issued to check where we are positioned. Like
1399 * all higher level operations, we queue the commands at the tail of the request
1400 * queue and wait for their completion.
1da177e4 1401 */
5a04cfa9
BP
1402static int idetape_position_tape(ide_drive_t *drive, unsigned int block,
1403 u8 partition, int skip)
1da177e4
LT
1404{
1405 idetape_tape_t *tape = drive->driver_data;
1406 int retval;
d236d74c 1407 struct ide_atapi_pc pc;
1da177e4 1408
54abf37e 1409 if (tape->chrdev_dir == IDETAPE_DIR_READ)
ec0fdb01 1410 __ide_tape_discard_merge_buffer(drive);
1da177e4
LT
1411 idetape_wait_ready(drive, 60 * 5 * HZ);
1412 idetape_create_locate_cmd(drive, &pc, block, partition, skip);
1413 retval = idetape_queue_pc_tail(drive, &pc);
1414 if (retval)
1415 return (retval);
1416
1417 idetape_create_read_position_cmd(&pc);
1418 return (idetape_queue_pc_tail(drive, &pc));
1419}
1420
ec0fdb01 1421static void ide_tape_discard_merge_buffer(ide_drive_t *drive,
5a04cfa9 1422 int restore_position)
1da177e4
LT
1423{
1424 idetape_tape_t *tape = drive->driver_data;
1da177e4
LT
1425 int seek, position;
1426
ec0fdb01 1427 __ide_tape_discard_merge_buffer(drive);
1da177e4
LT
1428 if (restore_position) {
1429 position = idetape_read_position(drive);
9798630a 1430 seek = position > 0 ? position : 0;
1da177e4 1431 if (idetape_position_tape(drive, seek, 0, 0)) {
5a04cfa9 1432 printk(KERN_INFO "ide-tape: %s: position_tape failed in"
ec0fdb01 1433 " %s\n", tape->name, __func__);
1da177e4
LT
1434 return;
1435 }
1436 }
1437}
1438
1439/*
3c98bf34
BP
1440 * Generate a read/write request for the block device interface and wait for it
1441 * to be serviced.
1da177e4 1442 */
5a04cfa9
BP
1443static int idetape_queue_rw_tail(ide_drive_t *drive, int cmd, int blocks,
1444 struct idetape_bh *bh)
1da177e4
LT
1445{
1446 idetape_tape_t *tape = drive->driver_data;
64ea1b4a
FT
1447 struct request *rq;
1448 int ret, errors;
1da177e4 1449
8004a8c9
BP
1450 debug_log(DBG_SENSE, "%s: cmd=%d\n", __func__, cmd);
1451
64ea1b4a
FT
1452 rq = blk_get_request(drive->queue, READ, __GFP_WAIT);
1453 rq->cmd_type = REQ_TYPE_SPECIAL;
83dd5735 1454 rq->cmd[13] = cmd;
64ea1b4a
FT
1455 rq->rq_disk = tape->disk;
1456 rq->special = (void *)bh;
1457 rq->sector = tape->first_frame;
1458 rq->nr_sectors = blocks;
1459 rq->current_nr_sectors = blocks;
1460 blk_execute_rq(drive->queue, tape->disk, rq, 0);
1461
1462 errors = rq->errors;
1463 ret = tape->blk_size * (blocks - rq->current_nr_sectors);
1464 blk_put_request(rq);
1da177e4
LT
1465
1466 if ((cmd & (REQ_IDETAPE_READ | REQ_IDETAPE_WRITE)) == 0)
1467 return 0;
1468
077e3bdb
BP
1469 if (tape->merge_bh)
1470 idetape_init_merge_buffer(tape);
64ea1b4a 1471 if (errors == IDETAPE_ERROR_GENERAL)
1da177e4 1472 return -EIO;
64ea1b4a 1473 return ret;
1da177e4
LT
1474}
1475
d236d74c 1476static void idetape_create_inquiry_cmd(struct ide_atapi_pc *pc)
1da177e4
LT
1477{
1478 idetape_init_pc(pc);
90699ce2 1479 pc->c[0] = INQUIRY;
5a04cfa9 1480 pc->c[4] = 254;
d236d74c 1481 pc->req_xfer = 254;
1da177e4
LT
1482}
1483
d236d74c
BP
1484static void idetape_create_rewind_cmd(ide_drive_t *drive,
1485 struct ide_atapi_pc *pc)
1da177e4
LT
1486{
1487 idetape_init_pc(pc);
90699ce2 1488 pc->c[0] = REZERO_UNIT;
346331f8 1489 pc->flags |= PC_FLAG_WAIT_FOR_DSC;
1da177e4
LT
1490}
1491
d236d74c 1492static void idetape_create_erase_cmd(struct ide_atapi_pc *pc)
1da177e4
LT
1493{
1494 idetape_init_pc(pc);
90699ce2 1495 pc->c[0] = ERASE;
1da177e4 1496 pc->c[1] = 1;
346331f8 1497 pc->flags |= PC_FLAG_WAIT_FOR_DSC;
1da177e4
LT
1498}
1499
d236d74c 1500static void idetape_create_space_cmd(struct ide_atapi_pc *pc, int count, u8 cmd)
1da177e4
LT
1501{
1502 idetape_init_pc(pc);
90699ce2 1503 pc->c[0] = SPACE;
860ff5ec 1504 put_unaligned(cpu_to_be32(count), (unsigned int *) &pc->c[1]);
1da177e4 1505 pc->c[1] = cmd;
346331f8 1506 pc->flags |= PC_FLAG_WAIT_FOR_DSC;
1da177e4
LT
1507}
1508
97c566ce 1509/* Queue up a character device originated write request. */
5a04cfa9 1510static int idetape_add_chrdev_write_request(ide_drive_t *drive, int blocks)
1da177e4
LT
1511{
1512 idetape_tape_t *tape = drive->driver_data;
1da177e4 1513
8004a8c9 1514 debug_log(DBG_CHRDEV, "Enter %s\n", __func__);
1da177e4 1515
0aa4b01e 1516 return idetape_queue_rw_tail(drive, REQ_IDETAPE_WRITE,
077e3bdb 1517 blocks, tape->merge_bh);
1da177e4
LT
1518}
1519
d9df937a 1520static void ide_tape_flush_merge_buffer(ide_drive_t *drive)
1da177e4
LT
1521{
1522 idetape_tape_t *tape = drive->driver_data;
1523 int blocks, min;
1524 struct idetape_bh *bh;
55a5d291 1525
54abf37e 1526 if (tape->chrdev_dir != IDETAPE_DIR_WRITE) {
077e3bdb 1527 printk(KERN_ERR "ide-tape: bug: Trying to empty merge buffer"
5a04cfa9 1528 " but we are not writing.\n");
1da177e4
LT
1529 return;
1530 }
01a63aeb 1531 if (tape->merge_bh_size > tape->buffer_size) {
1da177e4 1532 printk(KERN_ERR "ide-tape: bug: merge_buffer too big\n");
01a63aeb 1533 tape->merge_bh_size = tape->buffer_size;
1da177e4 1534 }
01a63aeb
BP
1535 if (tape->merge_bh_size) {
1536 blocks = tape->merge_bh_size / tape->blk_size;
1537 if (tape->merge_bh_size % tape->blk_size) {
1da177e4
LT
1538 unsigned int i;
1539
1540 blocks++;
01a63aeb 1541 i = tape->blk_size - tape->merge_bh_size %
54bb2074 1542 tape->blk_size;
1da177e4
LT
1543 bh = tape->bh->b_reqnext;
1544 while (bh) {
1545 atomic_set(&bh->b_count, 0);
1546 bh = bh->b_reqnext;
1547 }
1548 bh = tape->bh;
1549 while (i) {
1550 if (bh == NULL) {
5a04cfa9
BP
1551 printk(KERN_INFO "ide-tape: bug,"
1552 " bh NULL\n");
1da177e4
LT
1553 break;
1554 }
5a04cfa9
BP
1555 min = min(i, (unsigned int)(bh->b_size -
1556 atomic_read(&bh->b_count)));
1557 memset(bh->b_data + atomic_read(&bh->b_count),
1558 0, min);
1da177e4
LT
1559 atomic_add(min, &bh->b_count);
1560 i -= min;
1561 bh = bh->b_reqnext;
1562 }
1563 }
1564 (void) idetape_add_chrdev_write_request(drive, blocks);
01a63aeb 1565 tape->merge_bh_size = 0;
1da177e4 1566 }
077e3bdb
BP
1567 if (tape->merge_bh != NULL) {
1568 ide_tape_kfree_buffer(tape);
1569 tape->merge_bh = NULL;
1da177e4 1570 }
54abf37e 1571 tape->chrdev_dir = IDETAPE_DIR_NONE;
1da177e4
LT
1572}
1573
83042b24 1574static int idetape_init_read(ide_drive_t *drive)
1da177e4
LT
1575{
1576 idetape_tape_t *tape = drive->driver_data;
1da177e4 1577 int bytes_read;
1da177e4
LT
1578
1579 /* Initialize read operation */
54abf37e
BP
1580 if (tape->chrdev_dir != IDETAPE_DIR_READ) {
1581 if (tape->chrdev_dir == IDETAPE_DIR_WRITE) {
d9df937a 1582 ide_tape_flush_merge_buffer(drive);
1da177e4
LT
1583 idetape_flush_tape_buffers(drive);
1584 }
077e3bdb 1585 if (tape->merge_bh || tape->merge_bh_size) {
01a63aeb 1586 printk(KERN_ERR "ide-tape: merge_bh_size should be"
5a04cfa9 1587 " 0 now\n");
01a63aeb 1588 tape->merge_bh_size = 0;
1da177e4 1589 }
077e3bdb
BP
1590 tape->merge_bh = ide_tape_kmalloc_buffer(tape, 0, 0);
1591 if (!tape->merge_bh)
1da177e4 1592 return -ENOMEM;
54abf37e 1593 tape->chrdev_dir = IDETAPE_DIR_READ;
1da177e4
LT
1594
1595 /*
3c98bf34
BP
1596 * Issue a read 0 command to ensure that DSC handshake is
1597 * switched from completion mode to buffer available mode.
1598 * No point in issuing this if DSC overlap isn't supported, some
1599 * drives (Seagate STT3401A) will return an error.
1da177e4
LT
1600 */
1601 if (drive->dsc_overlap) {
5a04cfa9
BP
1602 bytes_read = idetape_queue_rw_tail(drive,
1603 REQ_IDETAPE_READ, 0,
077e3bdb 1604 tape->merge_bh);
1da177e4 1605 if (bytes_read < 0) {
077e3bdb
BP
1606 ide_tape_kfree_buffer(tape);
1607 tape->merge_bh = NULL;
54abf37e 1608 tape->chrdev_dir = IDETAPE_DIR_NONE;
1da177e4
LT
1609 return bytes_read;
1610 }
1611 }
1612 }
5e69bd95 1613
1da177e4
LT
1614 return 0;
1615}
1616
5bd50dc6 1617/* called from idetape_chrdev_read() to service a chrdev read request. */
5a04cfa9 1618static int idetape_add_chrdev_read_request(ide_drive_t *drive, int blocks)
1da177e4
LT
1619{
1620 idetape_tape_t *tape = drive->driver_data;
1da177e4 1621
8004a8c9 1622 debug_log(DBG_PROCS, "Enter %s, %d blocks\n", __func__, blocks);
1da177e4 1623
3c98bf34 1624 /* If we are at a filemark, return a read length of 0 */
f2e3ab52 1625 if (test_bit(IDE_AFLAG_FILEMARK, &drive->atapi_flags))
1da177e4
LT
1626 return 0;
1627
83042b24 1628 idetape_init_read(drive);
5e69bd95 1629
5e69bd95 1630 return idetape_queue_rw_tail(drive, REQ_IDETAPE_READ, blocks,
077e3bdb 1631 tape->merge_bh);
1da177e4
LT
1632}
1633
5a04cfa9 1634static void idetape_pad_zeros(ide_drive_t *drive, int bcount)
1da177e4
LT
1635{
1636 idetape_tape_t *tape = drive->driver_data;
1637 struct idetape_bh *bh;
1638 int blocks;
3c98bf34 1639
1da177e4
LT
1640 while (bcount) {
1641 unsigned int count;
1642
077e3bdb 1643 bh = tape->merge_bh;
f73850a3 1644 count = min(tape->buffer_size, bcount);
1da177e4 1645 bcount -= count;
54bb2074 1646 blocks = count / tape->blk_size;
1da177e4 1647 while (count) {
5a04cfa9
BP
1648 atomic_set(&bh->b_count,
1649 min(count, (unsigned int)bh->b_size));
1da177e4
LT
1650 memset(bh->b_data, 0, atomic_read(&bh->b_count));
1651 count -= atomic_read(&bh->b_count);
1652 bh = bh->b_reqnext;
1653 }
5a04cfa9 1654 idetape_queue_rw_tail(drive, REQ_IDETAPE_WRITE, blocks,
077e3bdb 1655 tape->merge_bh);
1da177e4
LT
1656 }
1657}
1658
1da177e4 1659/*
3c98bf34
BP
1660 * Rewinds the tape to the Beginning Of the current Partition (BOP). We
1661 * currently support only one partition.
1662 */
5a04cfa9 1663static int idetape_rewind_tape(ide_drive_t *drive)
1da177e4
LT
1664{
1665 int retval;
d236d74c 1666 struct ide_atapi_pc pc;
8004a8c9
BP
1667 idetape_tape_t *tape;
1668 tape = drive->driver_data;
1669
1670 debug_log(DBG_SENSE, "Enter %s\n", __func__);
1671
1da177e4
LT
1672 idetape_create_rewind_cmd(drive, &pc);
1673 retval = idetape_queue_pc_tail(drive, &pc);
1674 if (retval)
1675 return retval;
1676
1677 idetape_create_read_position_cmd(&pc);
1678 retval = idetape_queue_pc_tail(drive, &pc);
1679 if (retval)
1680 return retval;
1681 return 0;
1682}
1683
3c98bf34 1684/* mtio.h compatible commands should be issued to the chrdev interface. */
5a04cfa9
BP
1685static int idetape_blkdev_ioctl(ide_drive_t *drive, unsigned int cmd,
1686 unsigned long arg)
1da177e4
LT
1687{
1688 idetape_tape_t *tape = drive->driver_data;
1da177e4
LT
1689 void __user *argp = (void __user *)arg;
1690
d59823fa
BP
1691 struct idetape_config {
1692 int dsc_rw_frequency;
1693 int dsc_media_access_frequency;
1694 int nr_stages;
1695 } config;
1696
8004a8c9
BP
1697 debug_log(DBG_PROCS, "Enter %s\n", __func__);
1698
1da177e4 1699 switch (cmd) {
5a04cfa9
BP
1700 case 0x0340:
1701 if (copy_from_user(&config, argp, sizeof(config)))
1702 return -EFAULT;
1703 tape->best_dsc_rw_freq = config.dsc_rw_frequency;
5a04cfa9
BP
1704 break;
1705 case 0x0350:
1706 config.dsc_rw_frequency = (int) tape->best_dsc_rw_freq;
83042b24 1707 config.nr_stages = 1;
5a04cfa9
BP
1708 if (copy_to_user(argp, &config, sizeof(config)))
1709 return -EFAULT;
1710 break;
1711 default:
1712 return -EIO;
1da177e4
LT
1713 }
1714 return 0;
1715}
1716
5a04cfa9
BP
1717static int idetape_space_over_filemarks(ide_drive_t *drive, short mt_op,
1718 int mt_count)
1da177e4
LT
1719{
1720 idetape_tape_t *tape = drive->driver_data;
d236d74c 1721 struct ide_atapi_pc pc;
5a04cfa9 1722 int retval, count = 0;
b6422013 1723 int sprev = !!(tape->caps[4] & 0x20);
1da177e4
LT
1724
1725 if (mt_count == 0)
1726 return 0;
1727 if (MTBSF == mt_op || MTBSFM == mt_op) {
b6422013 1728 if (!sprev)
1da177e4 1729 return -EIO;
5a04cfa9 1730 mt_count = -mt_count;
1da177e4
LT
1731 }
1732
54abf37e 1733 if (tape->chrdev_dir == IDETAPE_DIR_READ) {
01a63aeb 1734 tape->merge_bh_size = 0;
f2e3ab52 1735 if (test_and_clear_bit(IDE_AFLAG_FILEMARK, &drive->atapi_flags))
1da177e4 1736 ++count;
ec0fdb01 1737 ide_tape_discard_merge_buffer(drive, 0);
1da177e4
LT
1738 }
1739
1da177e4 1740 switch (mt_op) {
5a04cfa9
BP
1741 case MTFSF:
1742 case MTBSF:
1743 idetape_create_space_cmd(&pc, mt_count - count,
1744 IDETAPE_SPACE_OVER_FILEMARK);
1745 return idetape_queue_pc_tail(drive, &pc);
1746 case MTFSFM:
1747 case MTBSFM:
1748 if (!sprev)
1749 return -EIO;
1750 retval = idetape_space_over_filemarks(drive, MTFSF,
1751 mt_count - count);
1752 if (retval)
1753 return retval;
1754 count = (MTBSFM == mt_op ? 1 : -1);
1755 return idetape_space_over_filemarks(drive, MTFSF, count);
1756 default:
1757 printk(KERN_ERR "ide-tape: MTIO operation %d not supported\n",
1758 mt_op);
1759 return -EIO;
1da177e4
LT
1760 }
1761}
1762
1da177e4 1763/*
3c98bf34 1764 * Our character device read / write functions.
1da177e4 1765 *
3c98bf34
BP
1766 * The tape is optimized to maximize throughput when it is transferring an
1767 * integral number of the "continuous transfer limit", which is a parameter of
1768 * the specific tape (26kB on my particular tape, 32kB for Onstream).
1da177e4 1769 *
3c98bf34
BP
1770 * As of version 1.3 of the driver, the character device provides an abstract
1771 * continuous view of the media - any mix of block sizes (even 1 byte) on the
1772 * same backup/restore procedure is supported. The driver will internally
1773 * convert the requests to the recommended transfer unit, so that an unmatch
1774 * between the user's block size to the recommended size will only result in a
1775 * (slightly) increased driver overhead, but will no longer hit performance.
1776 * This is not applicable to Onstream.
1da177e4 1777 */
5a04cfa9
BP
1778static ssize_t idetape_chrdev_read(struct file *file, char __user *buf,
1779 size_t count, loff_t *ppos)
1da177e4
LT
1780{
1781 struct ide_tape_obj *tape = ide_tape_f(file);
1782 ide_drive_t *drive = tape->drive;
5a04cfa9 1783 ssize_t bytes_read, temp, actually_read = 0, rc;
dcd96379 1784 ssize_t ret = 0;
b6422013 1785 u16 ctl = *(u16 *)&tape->caps[12];
1da177e4 1786
8004a8c9 1787 debug_log(DBG_CHRDEV, "Enter %s, count %Zd\n", __func__, count);
1da177e4 1788
54abf37e 1789 if (tape->chrdev_dir != IDETAPE_DIR_READ) {
f2e3ab52 1790 if (test_bit(IDE_AFLAG_DETECT_BS, &drive->atapi_flags))
54bb2074
BP
1791 if (count > tape->blk_size &&
1792 (count % tape->blk_size) == 0)
1793 tape->user_bs_factor = count / tape->blk_size;
1da177e4 1794 }
83042b24 1795 rc = idetape_init_read(drive);
8d06bfad 1796 if (rc < 0)
1da177e4
LT
1797 return rc;
1798 if (count == 0)
1799 return (0);
01a63aeb
BP
1800 if (tape->merge_bh_size) {
1801 actually_read = min((unsigned int)(tape->merge_bh_size),
5a04cfa9 1802 (unsigned int)count);
99d74e61 1803 if (idetape_copy_stage_to_user(tape, buf, actually_read))
dcd96379 1804 ret = -EFAULT;
1da177e4 1805 buf += actually_read;
01a63aeb 1806 tape->merge_bh_size -= actually_read;
1da177e4
LT
1807 count -= actually_read;
1808 }
f73850a3 1809 while (count >= tape->buffer_size) {
b6422013 1810 bytes_read = idetape_add_chrdev_read_request(drive, ctl);
1da177e4
LT
1811 if (bytes_read <= 0)
1812 goto finish;
99d74e61 1813 if (idetape_copy_stage_to_user(tape, buf, bytes_read))
dcd96379 1814 ret = -EFAULT;
1da177e4
LT
1815 buf += bytes_read;
1816 count -= bytes_read;
1817 actually_read += bytes_read;
1818 }
1819 if (count) {
b6422013 1820 bytes_read = idetape_add_chrdev_read_request(drive, ctl);
1da177e4
LT
1821 if (bytes_read <= 0)
1822 goto finish;
1823 temp = min((unsigned long)count, (unsigned long)bytes_read);
99d74e61 1824 if (idetape_copy_stage_to_user(tape, buf, temp))
dcd96379 1825 ret = -EFAULT;
1da177e4 1826 actually_read += temp;
01a63aeb 1827 tape->merge_bh_size = bytes_read-temp;
1da177e4
LT
1828 }
1829finish:
f2e3ab52 1830 if (!actually_read && test_bit(IDE_AFLAG_FILEMARK, &drive->atapi_flags)) {
8004a8c9
BP
1831 debug_log(DBG_SENSE, "%s: spacing over filemark\n", tape->name);
1832
1da177e4
LT
1833 idetape_space_over_filemarks(drive, MTFSF, 1);
1834 return 0;
1835 }
dcd96379 1836
5a04cfa9 1837 return ret ? ret : actually_read;
1da177e4
LT
1838}
1839
5a04cfa9 1840static ssize_t idetape_chrdev_write(struct file *file, const char __user *buf,
1da177e4
LT
1841 size_t count, loff_t *ppos)
1842{
1843 struct ide_tape_obj *tape = ide_tape_f(file);
1844 ide_drive_t *drive = tape->drive;
dcd96379
DW
1845 ssize_t actually_written = 0;
1846 ssize_t ret = 0;
b6422013 1847 u16 ctl = *(u16 *)&tape->caps[12];
1da177e4
LT
1848
1849 /* The drive is write protected. */
1850 if (tape->write_prot)
1851 return -EACCES;
1852
8004a8c9 1853 debug_log(DBG_CHRDEV, "Enter %s, count %Zd\n", __func__, count);
1da177e4
LT
1854
1855 /* Initialize write operation */
54abf37e
BP
1856 if (tape->chrdev_dir != IDETAPE_DIR_WRITE) {
1857 if (tape->chrdev_dir == IDETAPE_DIR_READ)
ec0fdb01 1858 ide_tape_discard_merge_buffer(drive, 1);
077e3bdb 1859 if (tape->merge_bh || tape->merge_bh_size) {
01a63aeb 1860 printk(KERN_ERR "ide-tape: merge_bh_size "
1da177e4 1861 "should be 0 now\n");
01a63aeb 1862 tape->merge_bh_size = 0;
1da177e4 1863 }
077e3bdb
BP
1864 tape->merge_bh = ide_tape_kmalloc_buffer(tape, 0, 0);
1865 if (!tape->merge_bh)
1da177e4 1866 return -ENOMEM;
54abf37e 1867 tape->chrdev_dir = IDETAPE_DIR_WRITE;
077e3bdb 1868 idetape_init_merge_buffer(tape);
1da177e4
LT
1869
1870 /*
3c98bf34
BP
1871 * Issue a write 0 command to ensure that DSC handshake is
1872 * switched from completion mode to buffer available mode. No
1873 * point in issuing this if DSC overlap isn't supported, some
1874 * drives (Seagate STT3401A) will return an error.
1da177e4
LT
1875 */
1876 if (drive->dsc_overlap) {
5a04cfa9
BP
1877 ssize_t retval = idetape_queue_rw_tail(drive,
1878 REQ_IDETAPE_WRITE, 0,
077e3bdb 1879 tape->merge_bh);
1da177e4 1880 if (retval < 0) {
077e3bdb
BP
1881 ide_tape_kfree_buffer(tape);
1882 tape->merge_bh = NULL;
54abf37e 1883 tape->chrdev_dir = IDETAPE_DIR_NONE;
1da177e4
LT
1884 return retval;
1885 }
1886 }
1887 }
1888 if (count == 0)
1889 return (0);
01a63aeb
BP
1890 if (tape->merge_bh_size) {
1891 if (tape->merge_bh_size >= tape->buffer_size) {
5a04cfa9 1892 printk(KERN_ERR "ide-tape: bug: merge buf too big\n");
01a63aeb 1893 tape->merge_bh_size = 0;
1da177e4 1894 }
5a04cfa9 1895 actually_written = min((unsigned int)
01a63aeb 1896 (tape->buffer_size - tape->merge_bh_size),
5a04cfa9 1897 (unsigned int)count);
8646c88f 1898 if (idetape_copy_stage_from_user(tape, buf, actually_written))
dcd96379 1899 ret = -EFAULT;
1da177e4 1900 buf += actually_written;
01a63aeb 1901 tape->merge_bh_size += actually_written;
1da177e4
LT
1902 count -= actually_written;
1903
01a63aeb 1904 if (tape->merge_bh_size == tape->buffer_size) {
dcd96379 1905 ssize_t retval;
01a63aeb 1906 tape->merge_bh_size = 0;
b6422013 1907 retval = idetape_add_chrdev_write_request(drive, ctl);
1da177e4
LT
1908 if (retval <= 0)
1909 return (retval);
1910 }
1911 }
f73850a3 1912 while (count >= tape->buffer_size) {
dcd96379 1913 ssize_t retval;
f73850a3 1914 if (idetape_copy_stage_from_user(tape, buf, tape->buffer_size))
dcd96379 1915 ret = -EFAULT;
f73850a3
BP
1916 buf += tape->buffer_size;
1917 count -= tape->buffer_size;
b6422013 1918 retval = idetape_add_chrdev_write_request(drive, ctl);
f73850a3 1919 actually_written += tape->buffer_size;
1da177e4
LT
1920 if (retval <= 0)
1921 return (retval);
1922 }
1923 if (count) {
1924 actually_written += count;
8646c88f 1925 if (idetape_copy_stage_from_user(tape, buf, count))
dcd96379 1926 ret = -EFAULT;
01a63aeb 1927 tape->merge_bh_size += count;
1da177e4 1928 }
5a04cfa9 1929 return ret ? ret : actually_written;
1da177e4
LT
1930}
1931
5a04cfa9 1932static int idetape_write_filemark(ide_drive_t *drive)
1da177e4 1933{
d236d74c 1934 struct ide_atapi_pc pc;
1da177e4
LT
1935
1936 /* Write a filemark */
1937 idetape_create_write_filemark_cmd(drive, &pc, 1);
1938 if (idetape_queue_pc_tail(drive, &pc)) {
1939 printk(KERN_ERR "ide-tape: Couldn't write a filemark\n");
1940 return -EIO;
1941 }
1942 return 0;
1943}
1944
1945/*
d99c9da2
BP
1946 * Called from idetape_chrdev_ioctl when the general mtio MTIOCTOP ioctl is
1947 * requested.
1da177e4 1948 *
d99c9da2
BP
1949 * Note: MTBSF and MTBSFM are not supported when the tape doesn't support
1950 * spacing over filemarks in the reverse direction. In this case, MTFSFM is also
5bd50dc6 1951 * usually not supported.
1da177e4 1952 *
d99c9da2 1953 * The following commands are currently not supported:
1da177e4 1954 *
d99c9da2
BP
1955 * MTFSS, MTBSS, MTWSM, MTSETDENSITY, MTSETDRVBUFFER, MT_ST_BOOLEANS,
1956 * MT_ST_WRITE_THRESHOLD.
1da177e4 1957 */
d99c9da2 1958static int idetape_mtioctop(ide_drive_t *drive, short mt_op, int mt_count)
1da177e4
LT
1959{
1960 idetape_tape_t *tape = drive->driver_data;
d236d74c 1961 struct ide_atapi_pc pc;
5a04cfa9 1962 int i, retval;
1da177e4 1963
8004a8c9
BP
1964 debug_log(DBG_ERR, "Handling MTIOCTOP ioctl: mt_op=%d, mt_count=%d\n",
1965 mt_op, mt_count);
5a04cfa9 1966
1da177e4 1967 switch (mt_op) {
5a04cfa9
BP
1968 case MTFSF:
1969 case MTFSFM:
1970 case MTBSF:
1971 case MTBSFM:
1972 if (!mt_count)
1973 return 0;
1974 return idetape_space_over_filemarks(drive, mt_op, mt_count);
1975 default:
1976 break;
1da177e4 1977 }
5a04cfa9 1978
1da177e4 1979 switch (mt_op) {
5a04cfa9
BP
1980 case MTWEOF:
1981 if (tape->write_prot)
1982 return -EACCES;
ec0fdb01 1983 ide_tape_discard_merge_buffer(drive, 1);
5a04cfa9
BP
1984 for (i = 0; i < mt_count; i++) {
1985 retval = idetape_write_filemark(drive);
1986 if (retval)
1987 return retval;
1988 }
1989 return 0;
1990 case MTREW:
ec0fdb01 1991 ide_tape_discard_merge_buffer(drive, 0);
5a04cfa9
BP
1992 if (idetape_rewind_tape(drive))
1993 return -EIO;
1994 return 0;
1995 case MTLOAD:
ec0fdb01 1996 ide_tape_discard_merge_buffer(drive, 0);
5a04cfa9
BP
1997 idetape_create_load_unload_cmd(drive, &pc,
1998 IDETAPE_LU_LOAD_MASK);
1999 return idetape_queue_pc_tail(drive, &pc);
2000 case MTUNLOAD:
2001 case MTOFFL:
2002 /*
2003 * If door is locked, attempt to unlock before
2004 * attempting to eject.
2005 */
2006 if (tape->door_locked) {
2007 if (idetape_create_prevent_cmd(drive, &pc, 0))
2008 if (!idetape_queue_pc_tail(drive, &pc))
2009 tape->door_locked = DOOR_UNLOCKED;
2010 }
ec0fdb01 2011 ide_tape_discard_merge_buffer(drive, 0);
5a04cfa9
BP
2012 idetape_create_load_unload_cmd(drive, &pc,
2013 !IDETAPE_LU_LOAD_MASK);
2014 retval = idetape_queue_pc_tail(drive, &pc);
2015 if (!retval)
f2e3ab52 2016 clear_bit(IDE_AFLAG_MEDIUM_PRESENT, &drive->atapi_flags);
5a04cfa9
BP
2017 return retval;
2018 case MTNOP:
ec0fdb01 2019 ide_tape_discard_merge_buffer(drive, 0);
5a04cfa9
BP
2020 return idetape_flush_tape_buffers(drive);
2021 case MTRETEN:
ec0fdb01 2022 ide_tape_discard_merge_buffer(drive, 0);
5a04cfa9
BP
2023 idetape_create_load_unload_cmd(drive, &pc,
2024 IDETAPE_LU_RETENSION_MASK | IDETAPE_LU_LOAD_MASK);
2025 return idetape_queue_pc_tail(drive, &pc);
2026 case MTEOM:
2027 idetape_create_space_cmd(&pc, 0, IDETAPE_SPACE_TO_EOD);
2028 return idetape_queue_pc_tail(drive, &pc);
2029 case MTERASE:
2030 (void)idetape_rewind_tape(drive);
2031 idetape_create_erase_cmd(&pc);
2032 return idetape_queue_pc_tail(drive, &pc);
2033 case MTSETBLK:
2034 if (mt_count) {
2035 if (mt_count < tape->blk_size ||
2036 mt_count % tape->blk_size)
1da177e4 2037 return -EIO;
5a04cfa9 2038 tape->user_bs_factor = mt_count / tape->blk_size;
f2e3ab52 2039 clear_bit(IDE_AFLAG_DETECT_BS, &drive->atapi_flags);
5a04cfa9 2040 } else
f2e3ab52 2041 set_bit(IDE_AFLAG_DETECT_BS, &drive->atapi_flags);
5a04cfa9
BP
2042 return 0;
2043 case MTSEEK:
ec0fdb01 2044 ide_tape_discard_merge_buffer(drive, 0);
5a04cfa9
BP
2045 return idetape_position_tape(drive,
2046 mt_count * tape->user_bs_factor, tape->partition, 0);
2047 case MTSETPART:
ec0fdb01 2048 ide_tape_discard_merge_buffer(drive, 0);
5a04cfa9
BP
2049 return idetape_position_tape(drive, 0, mt_count, 0);
2050 case MTFSR:
2051 case MTBSR:
2052 case MTLOCK:
2053 if (!idetape_create_prevent_cmd(drive, &pc, 1))
1da177e4 2054 return 0;
5a04cfa9
BP
2055 retval = idetape_queue_pc_tail(drive, &pc);
2056 if (retval)
1da177e4 2057 return retval;
5a04cfa9
BP
2058 tape->door_locked = DOOR_EXPLICITLY_LOCKED;
2059 return 0;
2060 case MTUNLOCK:
2061 if (!idetape_create_prevent_cmd(drive, &pc, 0))
1da177e4 2062 return 0;
5a04cfa9
BP
2063 retval = idetape_queue_pc_tail(drive, &pc);
2064 if (retval)
2065 return retval;
2066 tape->door_locked = DOOR_UNLOCKED;
2067 return 0;
2068 default:
2069 printk(KERN_ERR "ide-tape: MTIO operation %d not supported\n",
2070 mt_op);
2071 return -EIO;
1da177e4
LT
2072 }
2073}
2074
2075/*
d99c9da2
BP
2076 * Our character device ioctls. General mtio.h magnetic io commands are
2077 * supported here, and not in the corresponding block interface. Our own
2078 * ide-tape ioctls are supported on both interfaces.
1da177e4 2079 */
d99c9da2
BP
2080static int idetape_chrdev_ioctl(struct inode *inode, struct file *file,
2081 unsigned int cmd, unsigned long arg)
1da177e4
LT
2082{
2083 struct ide_tape_obj *tape = ide_tape_f(file);
2084 ide_drive_t *drive = tape->drive;
2085 struct mtop mtop;
2086 struct mtget mtget;
2087 struct mtpos mtpos;
54bb2074 2088 int block_offset = 0, position = tape->first_frame;
1da177e4
LT
2089 void __user *argp = (void __user *)arg;
2090
8004a8c9 2091 debug_log(DBG_CHRDEV, "Enter %s, cmd=%u\n", __func__, cmd);
1da177e4 2092
54abf37e 2093 if (tape->chrdev_dir == IDETAPE_DIR_WRITE) {
d9df937a 2094 ide_tape_flush_merge_buffer(drive);
1da177e4
LT
2095 idetape_flush_tape_buffers(drive);
2096 }
2097 if (cmd == MTIOCGET || cmd == MTIOCPOS) {
01a63aeb 2098 block_offset = tape->merge_bh_size /
54bb2074 2099 (tape->blk_size * tape->user_bs_factor);
5a04cfa9
BP
2100 position = idetape_read_position(drive);
2101 if (position < 0)
1da177e4
LT
2102 return -EIO;
2103 }
2104 switch (cmd) {
5a04cfa9
BP
2105 case MTIOCTOP:
2106 if (copy_from_user(&mtop, argp, sizeof(struct mtop)))
2107 return -EFAULT;
2108 return idetape_mtioctop(drive, mtop.mt_op, mtop.mt_count);
2109 case MTIOCGET:
2110 memset(&mtget, 0, sizeof(struct mtget));
2111 mtget.mt_type = MT_ISSCSI2;
2112 mtget.mt_blkno = position / tape->user_bs_factor - block_offset;
2113 mtget.mt_dsreg =
2114 ((tape->blk_size * tape->user_bs_factor)
2115 << MT_ST_BLKSIZE_SHIFT) & MT_ST_BLKSIZE_MASK;
2116
2117 if (tape->drv_write_prot)
2118 mtget.mt_gstat |= GMT_WR_PROT(0xffffffff);
2119
2120 if (copy_to_user(argp, &mtget, sizeof(struct mtget)))
2121 return -EFAULT;
2122 return 0;
2123 case MTIOCPOS:
2124 mtpos.mt_blkno = position / tape->user_bs_factor - block_offset;
2125 if (copy_to_user(argp, &mtpos, sizeof(struct mtpos)))
2126 return -EFAULT;
2127 return 0;
2128 default:
2129 if (tape->chrdev_dir == IDETAPE_DIR_READ)
ec0fdb01 2130 ide_tape_discard_merge_buffer(drive, 1);
5a04cfa9 2131 return idetape_blkdev_ioctl(drive, cmd, arg);
1da177e4
LT
2132 }
2133}
2134
3cffb9ce
BP
2135/*
2136 * Do a mode sense page 0 with block descriptor and if it succeeds set the tape
2137 * block size with the reported value.
2138 */
2139static void ide_tape_get_bsize_from_bdesc(ide_drive_t *drive)
2140{
2141 idetape_tape_t *tape = drive->driver_data;
d236d74c 2142 struct ide_atapi_pc pc;
3cffb9ce
BP
2143
2144 idetape_create_mode_sense_cmd(&pc, IDETAPE_BLOCK_DESCRIPTOR);
2145 if (idetape_queue_pc_tail(drive, &pc)) {
2146 printk(KERN_ERR "ide-tape: Can't get block descriptor\n");
54bb2074 2147 if (tape->blk_size == 0) {
3cffb9ce
BP
2148 printk(KERN_WARNING "ide-tape: Cannot deal with zero "
2149 "block size, assuming 32k\n");
54bb2074 2150 tape->blk_size = 32768;
3cffb9ce
BP
2151 }
2152 return;
2153 }
d236d74c
BP
2154 tape->blk_size = (pc.buf[4 + 5] << 16) +
2155 (pc.buf[4 + 6] << 8) +
2156 pc.buf[4 + 7];
2157 tape->drv_write_prot = (pc.buf[2] & 0x80) >> 7;
3cffb9ce 2158}
1da177e4 2159
5a04cfa9 2160static int idetape_chrdev_open(struct inode *inode, struct file *filp)
1da177e4
LT
2161{
2162 unsigned int minor = iminor(inode), i = minor & ~0xc0;
2163 ide_drive_t *drive;
2164 idetape_tape_t *tape;
d236d74c 2165 struct ide_atapi_pc pc;
1da177e4
LT
2166 int retval;
2167
8004a8c9
BP
2168 if (i >= MAX_HWIFS * MAX_DRIVES)
2169 return -ENXIO;
2170
04f4ac9d 2171 lock_kernel();
8004a8c9 2172 tape = ide_tape_chrdev_get(i);
04f4ac9d
JC
2173 if (!tape) {
2174 unlock_kernel();
8004a8c9 2175 return -ENXIO;
04f4ac9d 2176 }
8004a8c9
BP
2177
2178 debug_log(DBG_CHRDEV, "Enter %s\n", __func__);
2179
1da177e4
LT
2180 /*
2181 * We really want to do nonseekable_open(inode, filp); here, but some
2182 * versions of tar incorrectly call lseek on tapes and bail out if that
2183 * fails. So we disallow pread() and pwrite(), but permit lseeks.
2184 */
2185 filp->f_mode &= ~(FMODE_PREAD | FMODE_PWRITE);
2186
1da177e4
LT
2187 drive = tape->drive;
2188
2189 filp->private_data = tape;
2190
f2e3ab52 2191 if (test_and_set_bit(IDE_AFLAG_BUSY, &drive->atapi_flags)) {
1da177e4
LT
2192 retval = -EBUSY;
2193 goto out_put_tape;
2194 }
2195
2196 retval = idetape_wait_ready(drive, 60 * HZ);
2197 if (retval) {
f2e3ab52 2198 clear_bit(IDE_AFLAG_BUSY, &drive->atapi_flags);
1da177e4
LT
2199 printk(KERN_ERR "ide-tape: %s: drive not ready\n", tape->name);
2200 goto out_put_tape;
2201 }
2202
2203 idetape_read_position(drive);
f2e3ab52 2204 if (!test_bit(IDE_AFLAG_ADDRESS_VALID, &drive->atapi_flags))
1da177e4
LT
2205 (void)idetape_rewind_tape(drive);
2206
1da177e4 2207 /* Read block size and write protect status from drive. */
3cffb9ce 2208 ide_tape_get_bsize_from_bdesc(drive);
1da177e4
LT
2209
2210 /* Set write protect flag if device is opened as read-only. */
2211 if ((filp->f_flags & O_ACCMODE) == O_RDONLY)
2212 tape->write_prot = 1;
2213 else
2214 tape->write_prot = tape->drv_write_prot;
2215
2216 /* Make sure drive isn't write protected if user wants to write. */
2217 if (tape->write_prot) {
2218 if ((filp->f_flags & O_ACCMODE) == O_WRONLY ||
2219 (filp->f_flags & O_ACCMODE) == O_RDWR) {
f2e3ab52 2220 clear_bit(IDE_AFLAG_BUSY, &drive->atapi_flags);
1da177e4
LT
2221 retval = -EROFS;
2222 goto out_put_tape;
2223 }
2224 }
2225
3c98bf34 2226 /* Lock the tape drive door so user can't eject. */
54abf37e 2227 if (tape->chrdev_dir == IDETAPE_DIR_NONE) {
1da177e4
LT
2228 if (idetape_create_prevent_cmd(drive, &pc, 1)) {
2229 if (!idetape_queue_pc_tail(drive, &pc)) {
2230 if (tape->door_locked != DOOR_EXPLICITLY_LOCKED)
2231 tape->door_locked = DOOR_LOCKED;
2232 }
2233 }
2234 }
04f4ac9d 2235 unlock_kernel();
1da177e4
LT
2236 return 0;
2237
2238out_put_tape:
2239 ide_tape_put(tape);
04f4ac9d 2240 unlock_kernel();
1da177e4
LT
2241 return retval;
2242}
2243
5a04cfa9 2244static void idetape_write_release(ide_drive_t *drive, unsigned int minor)
1da177e4
LT
2245{
2246 idetape_tape_t *tape = drive->driver_data;
2247
d9df937a 2248 ide_tape_flush_merge_buffer(drive);
077e3bdb
BP
2249 tape->merge_bh = ide_tape_kmalloc_buffer(tape, 1, 0);
2250 if (tape->merge_bh != NULL) {
54bb2074
BP
2251 idetape_pad_zeros(drive, tape->blk_size *
2252 (tape->user_bs_factor - 1));
077e3bdb
BP
2253 ide_tape_kfree_buffer(tape);
2254 tape->merge_bh = NULL;
1da177e4
LT
2255 }
2256 idetape_write_filemark(drive);
2257 idetape_flush_tape_buffers(drive);
2258 idetape_flush_tape_buffers(drive);
2259}
2260
5a04cfa9 2261static int idetape_chrdev_release(struct inode *inode, struct file *filp)
1da177e4
LT
2262{
2263 struct ide_tape_obj *tape = ide_tape_f(filp);
2264 ide_drive_t *drive = tape->drive;
d236d74c 2265 struct ide_atapi_pc pc;
1da177e4
LT
2266 unsigned int minor = iminor(inode);
2267
2268 lock_kernel();
2269 tape = drive->driver_data;
8004a8c9
BP
2270
2271 debug_log(DBG_CHRDEV, "Enter %s\n", __func__);
1da177e4 2272
54abf37e 2273 if (tape->chrdev_dir == IDETAPE_DIR_WRITE)
1da177e4 2274 idetape_write_release(drive, minor);
54abf37e 2275 if (tape->chrdev_dir == IDETAPE_DIR_READ) {
1da177e4 2276 if (minor < 128)
ec0fdb01 2277 ide_tape_discard_merge_buffer(drive, 1);
1da177e4 2278 }
f64eee7b 2279
f2e3ab52 2280 if (minor < 128 && test_bit(IDE_AFLAG_MEDIUM_PRESENT, &drive->atapi_flags))
1da177e4 2281 (void) idetape_rewind_tape(drive);
54abf37e 2282 if (tape->chrdev_dir == IDETAPE_DIR_NONE) {
1da177e4
LT
2283 if (tape->door_locked == DOOR_LOCKED) {
2284 if (idetape_create_prevent_cmd(drive, &pc, 0)) {
2285 if (!idetape_queue_pc_tail(drive, &pc))
2286 tape->door_locked = DOOR_UNLOCKED;
2287 }
2288 }
2289 }
f2e3ab52 2290 clear_bit(IDE_AFLAG_BUSY, &drive->atapi_flags);
1da177e4
LT
2291 ide_tape_put(tape);
2292 unlock_kernel();
2293 return 0;
2294}
2295
6d29c8f0 2296static void idetape_get_inquiry_results(ide_drive_t *drive)
1da177e4 2297{
1da177e4 2298 idetape_tape_t *tape = drive->driver_data;
d236d74c 2299 struct ide_atapi_pc pc;
801bd32e 2300 char fw_rev[4], vendor_id[8], product_id[16];
6d29c8f0 2301
1da177e4
LT
2302 idetape_create_inquiry_cmd(&pc);
2303 if (idetape_queue_pc_tail(drive, &pc)) {
6d29c8f0
BP
2304 printk(KERN_ERR "ide-tape: %s: can't get INQUIRY results\n",
2305 tape->name);
1da177e4
LT
2306 return;
2307 }
d236d74c
BP
2308 memcpy(vendor_id, &pc.buf[8], 8);
2309 memcpy(product_id, &pc.buf[16], 16);
2310 memcpy(fw_rev, &pc.buf[32], 4);
41f81d54 2311
801bd32e
BP
2312 ide_fixstring(vendor_id, 8, 0);
2313 ide_fixstring(product_id, 16, 0);
2314 ide_fixstring(fw_rev, 4, 0);
41f81d54 2315
801bd32e 2316 printk(KERN_INFO "ide-tape: %s <-> %s: %.8s %.16s rev %.4s\n",
41f81d54 2317 drive->name, tape->name, vendor_id, product_id, fw_rev);
1da177e4
LT
2318}
2319
2320/*
b6422013
BP
2321 * Ask the tape about its various parameters. In particular, we will adjust our
2322 * data transfer buffer size to the recommended value as returned by the tape.
1da177e4 2323 */
5a04cfa9 2324static void idetape_get_mode_sense_results(ide_drive_t *drive)
1da177e4
LT
2325{
2326 idetape_tape_t *tape = drive->driver_data;
d236d74c 2327 struct ide_atapi_pc pc;
b6422013
BP
2328 u8 *caps;
2329 u8 speed, max_speed;
47314fa4 2330
1da177e4
LT
2331 idetape_create_mode_sense_cmd(&pc, IDETAPE_CAPABILITIES_PAGE);
2332 if (idetape_queue_pc_tail(drive, &pc)) {
b6422013
BP
2333 printk(KERN_ERR "ide-tape: Can't get tape parameters - assuming"
2334 " some default values\n");
54bb2074 2335 tape->blk_size = 512;
b6422013
BP
2336 put_unaligned(52, (u16 *)&tape->caps[12]);
2337 put_unaligned(540, (u16 *)&tape->caps[14]);
2338 put_unaligned(6*52, (u16 *)&tape->caps[16]);
1da177e4
LT
2339 return;
2340 }
d236d74c 2341 caps = pc.buf + 4 + pc.buf[3];
b6422013
BP
2342
2343 /* convert to host order and save for later use */
cd740ab0
HH
2344 speed = be16_to_cpup((__be16 *)&caps[14]);
2345 max_speed = be16_to_cpup((__be16 *)&caps[8]);
1da177e4 2346
cd740ab0
HH
2347 *(u16 *)&caps[8] = max_speed;
2348 *(u16 *)&caps[12] = be16_to_cpup((__be16 *)&caps[12]);
2349 *(u16 *)&caps[14] = speed;
2350 *(u16 *)&caps[16] = be16_to_cpup((__be16 *)&caps[16]);
1da177e4 2351
b6422013
BP
2352 if (!speed) {
2353 printk(KERN_INFO "ide-tape: %s: invalid tape speed "
2354 "(assuming 650KB/sec)\n", drive->name);
cd740ab0 2355 *(u16 *)&caps[14] = 650;
1da177e4 2356 }
b6422013
BP
2357 if (!max_speed) {
2358 printk(KERN_INFO "ide-tape: %s: invalid max_speed "
2359 "(assuming 650KB/sec)\n", drive->name);
cd740ab0 2360 *(u16 *)&caps[8] = 650;
1da177e4
LT
2361 }
2362
b6422013
BP
2363 memcpy(&tape->caps, caps, 20);
2364 if (caps[7] & 0x02)
54bb2074 2365 tape->blk_size = 512;
b6422013 2366 else if (caps[7] & 0x04)
54bb2074 2367 tape->blk_size = 1024;
1da177e4
LT
2368}
2369
7662d046 2370#ifdef CONFIG_IDE_PROC_FS
8185d5aa
BZ
2371#define ide_tape_devset_get(name, field) \
2372static int get_##name(ide_drive_t *drive) \
2373{ \
2374 idetape_tape_t *tape = drive->driver_data; \
2375 return tape->field; \
2376}
2377
2378#define ide_tape_devset_set(name, field) \
2379static int set_##name(ide_drive_t *drive, int arg) \
2380{ \
2381 idetape_tape_t *tape = drive->driver_data; \
2382 tape->field = arg; \
2383 return 0; \
2384}
2385
2386#define ide_tape_devset_rw(_name, _min, _max, _field, _mulf, _divf) \
2387ide_tape_devset_get(_name, _field) \
2388ide_tape_devset_set(_name, _field) \
2389__IDE_DEVSET(_name, S_RW, _min, _max, get_##_name, set_##_name, _mulf, _divf)
2390
2391#define ide_tape_devset_r(_name, _min, _max, _field, _mulf, _divf) \
2392ide_tape_devset_get(_name, _field) \
2393__IDE_DEVSET(_name, S_READ, _min, _max, get_##_name, NULL, _mulf, _divf)
2394
2395static int mulf_tdsc(ide_drive_t *drive) { return 1000; }
2396static int divf_tdsc(ide_drive_t *drive) { return HZ; }
2397static int divf_buffer(ide_drive_t *drive) { return 2; }
2398static int divf_buffer_size(ide_drive_t *drive) { return 1024; }
2399
2400ide_devset_rw(dsc_overlap, 0, 1, dsc_overlap);
2401
2402ide_tape_devset_rw(debug_mask, 0, 0xffff, debug_mask, NULL, NULL);
2403ide_tape_devset_rw(tdsc, IDETAPE_DSC_RW_MIN, IDETAPE_DSC_RW_MAX,
2404 best_dsc_rw_freq, mulf_tdsc, divf_tdsc);
2405
2406ide_tape_devset_r(avg_speed, 0, 0xffff, avg_speed, NULL, NULL);
2407ide_tape_devset_r(speed, 0, 0xffff, caps[14], NULL, NULL);
2408ide_tape_devset_r(buffer, 0, 0xffff, caps[16], NULL, divf_buffer);
2409ide_tape_devset_r(buffer_size, 0, 0xffff, buffer_size, NULL, divf_buffer_size);
2410
2411static const struct ide_devset *idetape_settings[] = {
2412 &ide_devset_avg_speed,
2413 &ide_devset_buffer,
2414 &ide_devset_buffer_size,
2415 &ide_devset_debug_mask,
2416 &ide_devset_dsc_overlap,
2417 &ide_devset_speed,
2418 &ide_devset_tdsc,
2419 NULL
2420};
7662d046 2421#endif
1da177e4
LT
2422
2423/*
3c98bf34 2424 * The function below is called to:
1da177e4 2425 *
3c98bf34
BP
2426 * 1. Initialize our various state variables.
2427 * 2. Ask the tape for its capabilities.
2428 * 3. Allocate a buffer which will be used for data transfer. The buffer size
2429 * is chosen based on the recommendation which we received in step 2.
1da177e4 2430 *
3c98bf34
BP
2431 * Note that at this point ide.c already assigned us an irq, so that we can
2432 * queue requests here and wait for their completion.
1da177e4 2433 */
5a04cfa9 2434static void idetape_setup(ide_drive_t *drive, idetape_tape_t *tape, int minor)
1da177e4 2435{
83042b24 2436 unsigned long t;
1da177e4 2437 int speed;
f73850a3 2438 int buffer_size;
71071b8e 2439 u8 gcw[2];
b6422013 2440 u16 *ctl = (u16 *)&tape->caps[12];
1da177e4 2441
776bb027
BP
2442 drive->pc_callback = ide_tape_callback;
2443
54bb2074 2444 spin_lock_init(&tape->lock);
1da177e4 2445 drive->dsc_overlap = 1;
4166c199
BZ
2446 if (drive->hwif->host_flags & IDE_HFLAG_NO_DSC) {
2447 printk(KERN_INFO "ide-tape: %s: disabling DSC overlap\n",
2448 tape->name);
2449 drive->dsc_overlap = 0;
1da177e4 2450 }
1da177e4 2451 /* Seagate Travan drives do not support DSC overlap. */
4dde4492 2452 if (strstr((char *)&drive->id[ATA_ID_PROD], "Seagate STT3401"))
1da177e4
LT
2453 drive->dsc_overlap = 0;
2454 tape->minor = minor;
2455 tape->name[0] = 'h';
2456 tape->name[1] = 't';
2457 tape->name[2] = '0' + minor;
54abf37e 2458 tape->chrdev_dir = IDETAPE_DIR_NONE;
1da177e4 2459 tape->pc = tape->pc_stack;
4dde4492
BZ
2460
2461 *((u16 *)&gcw) = drive->id[ATA_ID_CONFIG];
71071b8e
BP
2462
2463 /* Command packet DRQ type */
2464 if (((gcw[0] & 0x60) >> 5) == 1)
f2e3ab52 2465 set_bit(IDE_AFLAG_DRQ_INTERRUPT, &drive->atapi_flags);
1da177e4 2466
1da177e4
LT
2467 idetape_get_inquiry_results(drive);
2468 idetape_get_mode_sense_results(drive);
3cffb9ce 2469 ide_tape_get_bsize_from_bdesc(drive);
1da177e4 2470 tape->user_bs_factor = 1;
f73850a3
BP
2471 tape->buffer_size = *ctl * tape->blk_size;
2472 while (tape->buffer_size > 0xffff) {
1da177e4 2473 printk(KERN_NOTICE "ide-tape: decreasing stage size\n");
b6422013 2474 *ctl /= 2;
f73850a3 2475 tape->buffer_size = *ctl * tape->blk_size;
1da177e4 2476 }
f73850a3 2477 buffer_size = tape->buffer_size;
a997a435 2478 tape->pages_per_buffer = buffer_size / PAGE_SIZE;
f73850a3 2479 if (buffer_size % PAGE_SIZE) {
a997a435 2480 tape->pages_per_buffer++;
f73850a3 2481 tape->excess_bh_size = PAGE_SIZE - buffer_size % PAGE_SIZE;
1da177e4
LT
2482 }
2483
83042b24 2484 /* select the "best" DSC read/write polling freq */
b6422013 2485 speed = max(*(u16 *)&tape->caps[14], *(u16 *)&tape->caps[8]);
1da177e4 2486
f73850a3 2487 t = (IDETAPE_FIFO_THRESHOLD * tape->buffer_size * HZ) / (speed * 1000);
1da177e4
LT
2488
2489 /*
3c98bf34
BP
2490 * Ensure that the number we got makes sense; limit it within
2491 * IDETAPE_DSC_RW_MIN and IDETAPE_DSC_RW_MAX.
1da177e4 2492 */
a792bd5a
HH
2493 tape->best_dsc_rw_freq = clamp_t(unsigned long, t, IDETAPE_DSC_RW_MIN,
2494 IDETAPE_DSC_RW_MAX);
1da177e4 2495 printk(KERN_INFO "ide-tape: %s <-> %s: %dKBps, %d*%dkB buffer, "
83042b24 2496 "%lums tDSC%s\n",
b6422013 2497 drive->name, tape->name, *(u16 *)&tape->caps[14],
f73850a3
BP
2498 (*(u16 *)&tape->caps[16] * 512) / tape->buffer_size,
2499 tape->buffer_size / 1024,
54bb2074 2500 tape->best_dsc_rw_freq * 1000 / HZ,
1da177e4
LT
2501 drive->using_dma ? ", DMA":"");
2502
1e874f44 2503 ide_proc_register_driver(drive, tape->driver);
1da177e4
LT
2504}
2505
4031bbe4 2506static void ide_tape_remove(ide_drive_t *drive)
1da177e4
LT
2507{
2508 idetape_tape_t *tape = drive->driver_data;
1da177e4 2509
7662d046 2510 ide_proc_unregister_driver(drive, tape->driver);
1da177e4
LT
2511
2512 ide_unregister_region(tape->disk);
2513
2514 ide_tape_put(tape);
1da177e4
LT
2515}
2516
2517static void ide_tape_release(struct kref *kref)
2518{
2519 struct ide_tape_obj *tape = to_ide_tape(kref);
2520 ide_drive_t *drive = tape->drive;
2521 struct gendisk *g = tape->disk;
2522
01a63aeb 2523 BUG_ON(tape->merge_bh_size);
8604affd 2524
1da177e4
LT
2525 drive->dsc_overlap = 0;
2526 drive->driver_data = NULL;
dbc1272e 2527 device_destroy(idetape_sysfs_class, MKDEV(IDETAPE_MAJOR, tape->minor));
5a04cfa9
BP
2528 device_destroy(idetape_sysfs_class,
2529 MKDEV(IDETAPE_MAJOR, tape->minor + 128));
1da177e4
LT
2530 idetape_devs[tape->minor] = NULL;
2531 g->private_data = NULL;
2532 put_disk(g);
2533 kfree(tape);
2534}
2535
ecfd80e4 2536#ifdef CONFIG_IDE_PROC_FS
1da177e4
LT
2537static int proc_idetape_read_name
2538 (char *page, char **start, off_t off, int count, int *eof, void *data)
2539{
2540 ide_drive_t *drive = (ide_drive_t *) data;
2541 idetape_tape_t *tape = drive->driver_data;
2542 char *out = page;
2543 int len;
2544
2545 len = sprintf(out, "%s\n", tape->name);
2546 PROC_IDE_READ_RETURN(page, start, off, count, eof, len);
2547}
2548
2549static ide_proc_entry_t idetape_proc[] = {
2550 { "capacity", S_IFREG|S_IRUGO, proc_ide_read_capacity, NULL },
2551 { "name", S_IFREG|S_IRUGO, proc_idetape_read_name, NULL },
2552 { NULL, 0, NULL, NULL }
2553};
1da177e4
LT
2554#endif
2555
4031bbe4 2556static int ide_tape_probe(ide_drive_t *);
1da177e4 2557
1da177e4 2558static ide_driver_t idetape_driver = {
8604affd 2559 .gen_driver = {
4ef3b8f4 2560 .owner = THIS_MODULE,
8604affd
BZ
2561 .name = "ide-tape",
2562 .bus = &ide_bus_type,
8604affd 2563 },
4031bbe4
RK
2564 .probe = ide_tape_probe,
2565 .remove = ide_tape_remove,
1da177e4
LT
2566 .version = IDETAPE_VERSION,
2567 .media = ide_tape,
1da177e4
LT
2568 .do_request = idetape_do_request,
2569 .end_request = idetape_end_request,
2570 .error = __ide_error,
7662d046 2571#ifdef CONFIG_IDE_PROC_FS
1da177e4 2572 .proc = idetape_proc,
8185d5aa 2573 .settings = idetape_settings,
7662d046 2574#endif
1da177e4
LT
2575};
2576
3c98bf34 2577/* Our character device supporting functions, passed to register_chrdev. */
2b8693c0 2578static const struct file_operations idetape_fops = {
1da177e4
LT
2579 .owner = THIS_MODULE,
2580 .read = idetape_chrdev_read,
2581 .write = idetape_chrdev_write,
2582 .ioctl = idetape_chrdev_ioctl,
2583 .open = idetape_chrdev_open,
2584 .release = idetape_chrdev_release,
2585};
2586
2587static int idetape_open(struct inode *inode, struct file *filp)
2588{
2589 struct gendisk *disk = inode->i_bdev->bd_disk;
2590 struct ide_tape_obj *tape;
1da177e4 2591
5a04cfa9
BP
2592 tape = ide_tape_get(disk);
2593 if (!tape)
1da177e4
LT
2594 return -ENXIO;
2595
1da177e4
LT
2596 return 0;
2597}
2598
2599static int idetape_release(struct inode *inode, struct file *filp)
2600{
2601 struct gendisk *disk = inode->i_bdev->bd_disk;
2602 struct ide_tape_obj *tape = ide_tape_g(disk);
1da177e4
LT
2603
2604 ide_tape_put(tape);
2605
2606 return 0;
2607}
2608
2609static int idetape_ioctl(struct inode *inode, struct file *file,
2610 unsigned int cmd, unsigned long arg)
2611{
2612 struct block_device *bdev = inode->i_bdev;
2613 struct ide_tape_obj *tape = ide_tape_g(bdev->bd_disk);
2614 ide_drive_t *drive = tape->drive;
2615 int err = generic_ide_ioctl(drive, file, bdev, cmd, arg);
2616 if (err == -EINVAL)
2617 err = idetape_blkdev_ioctl(drive, cmd, arg);
2618 return err;
2619}
2620
2621static struct block_device_operations idetape_block_ops = {
2622 .owner = THIS_MODULE,
2623 .open = idetape_open,
2624 .release = idetape_release,
2625 .ioctl = idetape_ioctl,
2626};
2627
4031bbe4 2628static int ide_tape_probe(ide_drive_t *drive)
1da177e4
LT
2629{
2630 idetape_tape_t *tape;
2631 struct gendisk *g;
2632 int minor;
2633
2634 if (!strstr("ide-tape", drive->driver_req))
2635 goto failed;
2a924662 2636
1da177e4
LT
2637 if (drive->media != ide_tape)
2638 goto failed;
2a924662 2639
51509eec 2640 if (drive->id_read == 1 && !ide_check_atapi_device(drive, DRV_NAME)) {
5a04cfa9
BP
2641 printk(KERN_ERR "ide-tape: %s: not supported by this version of"
2642 " the driver\n", drive->name);
1da177e4
LT
2643 goto failed;
2644 }
5a04cfa9 2645 tape = kzalloc(sizeof(idetape_tape_t), GFP_KERNEL);
1da177e4 2646 if (tape == NULL) {
5a04cfa9
BP
2647 printk(KERN_ERR "ide-tape: %s: Can't allocate a tape struct\n",
2648 drive->name);
1da177e4
LT
2649 goto failed;
2650 }
2651
2652 g = alloc_disk(1 << PARTN_BITS);
2653 if (!g)
2654 goto out_free_tape;
2655
2656 ide_init_disk(g, drive);
2657
1da177e4
LT
2658 kref_init(&tape->kref);
2659
2660 tape->drive = drive;
2661 tape->driver = &idetape_driver;
2662 tape->disk = g;
2663
2664 g->private_data = &tape->driver;
2665
2666 drive->driver_data = tape;
2667
cf8b8975 2668 mutex_lock(&idetape_ref_mutex);
1da177e4
LT
2669 for (minor = 0; idetape_devs[minor]; minor++)
2670 ;
2671 idetape_devs[minor] = tape;
cf8b8975 2672 mutex_unlock(&idetape_ref_mutex);
1da177e4
LT
2673
2674 idetape_setup(drive, tape, minor);
2675
6ecaaf94
GKH
2676 device_create_drvdata(idetape_sysfs_class, &drive->gendev,
2677 MKDEV(IDETAPE_MAJOR, minor), NULL,
2678 "%s", tape->name);
2679 device_create_drvdata(idetape_sysfs_class, &drive->gendev,
2680 MKDEV(IDETAPE_MAJOR, minor + 128), NULL,
2681 "n%s", tape->name);
d5dee80a 2682
1da177e4
LT
2683 g->fops = &idetape_block_ops;
2684 ide_register_region(g);
2685
2686 return 0;
8604affd 2687
1da177e4
LT
2688out_free_tape:
2689 kfree(tape);
2690failed:
8604affd 2691 return -ENODEV;
1da177e4
LT
2692}
2693
5a04cfa9 2694static void __exit idetape_exit(void)
1da177e4 2695{
8604affd 2696 driver_unregister(&idetape_driver.gen_driver);
d5dee80a 2697 class_destroy(idetape_sysfs_class);
1da177e4
LT
2698 unregister_chrdev(IDETAPE_MAJOR, "ht");
2699}
2700
17514e8a 2701static int __init idetape_init(void)
1da177e4 2702{
d5dee80a
WD
2703 int error = 1;
2704 idetape_sysfs_class = class_create(THIS_MODULE, "ide_tape");
2705 if (IS_ERR(idetape_sysfs_class)) {
2706 idetape_sysfs_class = NULL;
2707 printk(KERN_ERR "Unable to create sysfs class for ide tapes\n");
2708 error = -EBUSY;
2709 goto out;
2710 }
2711
1da177e4 2712 if (register_chrdev(IDETAPE_MAJOR, "ht", &idetape_fops)) {
5a04cfa9
BP
2713 printk(KERN_ERR "ide-tape: Failed to register chrdev"
2714 " interface\n");
d5dee80a
WD
2715 error = -EBUSY;
2716 goto out_free_class;
1da177e4 2717 }
d5dee80a
WD
2718
2719 error = driver_register(&idetape_driver.gen_driver);
2720 if (error)
2721 goto out_free_driver;
2722
2723 return 0;
2724
2725out_free_driver:
2726 driver_unregister(&idetape_driver.gen_driver);
2727out_free_class:
2728 class_destroy(idetape_sysfs_class);
2729out:
2730 return error;
1da177e4
LT
2731}
2732
263756ec 2733MODULE_ALIAS("ide:*m-tape*");
1da177e4
LT
2734module_init(idetape_init);
2735module_exit(idetape_exit);
2736MODULE_ALIAS_CHARDEV_MAJOR(IDETAPE_MAJOR);
9c145768
BP
2737MODULE_DESCRIPTION("ATAPI Streaming TAPE Driver");
2738MODULE_LICENSE("GPL");