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