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