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