Merge branch 'master' of /home/gregkh/linux/git/torvalds-2.6
[GitHub/mt8127/android_kernel_alcatel_ttab.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>
6d703a81 34#include <linux/seq_file.h>
1da177e4
LT
35#include <linux/slab.h>
36#include <linux/pci.h>
37#include <linux/ide.h>
38#include <linux/smp_lock.h>
39#include <linux/completion.h>
40#include <linux/bitops.h>
cf8b8975 41#include <linux/mutex.h>
90699ce2 42#include <scsi/scsi.h>
1da177e4
LT
43
44#include <asm/byteorder.h>
c837cfa5
BP
45#include <linux/irq.h>
46#include <linux/uaccess.h>
47#include <linux/io.h>
1da177e4 48#include <asm/unaligned.h>
1da177e4
LT
49#include <linux/mtio.h>
50
8004a8c9 51/* define to see debug info */
e972d702 52#undef IDETAPE_DEBUG_LOG
8004a8c9 53
e972d702
BP
54#ifdef IDETAPE_DEBUG_LOG
55#define ide_debug_log(lvl, fmt, args...) __ide_debug_log(lvl, fmt, ## args)
8004a8c9 56#else
e972d702 57#define ide_debug_log(lvl, fmt, args...) do {} while (0)
8004a8c9
BP
58#endif
59
1da177e4 60/**************************** Tunable parameters *****************************/
1da177e4 61/*
3c98bf34
BP
62 * After each failed packet command we issue a request sense command and retry
63 * the packet command IDETAPE_MAX_PC_RETRIES times.
1da177e4 64 *
3c98bf34 65 * Setting IDETAPE_MAX_PC_RETRIES to 0 will disable retries.
1da177e4
LT
66 */
67#define IDETAPE_MAX_PC_RETRIES 3
68
1da177e4 69/*
3c98bf34
BP
70 * The following parameter is used to select the point in the internal tape fifo
71 * in which we will start to refill the buffer. Decreasing the following
72 * parameter will improve the system's latency and interactive response, while
73 * using a high value might improve system throughput.
1da177e4 74 */
3c98bf34 75#define IDETAPE_FIFO_THRESHOLD 2
1da177e4
LT
76
77/*
3c98bf34 78 * DSC polling parameters.
1da177e4 79 *
3c98bf34
BP
80 * Polling for DSC (a single bit in the status register) is a very important
81 * function in ide-tape. There are two cases in which we poll for DSC:
1da177e4 82 *
3c98bf34
BP
83 * 1. Before a read/write packet command, to ensure that we can transfer data
84 * from/to the tape's data buffers, without causing an actual media access.
85 * In case the tape is not ready yet, we take out our request from the device
86 * request queue, so that ide.c could service requests from the other device
87 * on the same interface in the meantime.
1da177e4 88 *
3c98bf34
BP
89 * 2. After the successful initialization of a "media access packet command",
90 * which is a command that can take a long time to complete (the interval can
91 * range from several seconds to even an hour). Again, we postpone our request
92 * in the middle to free the bus for the other device. The polling frequency
93 * here should be lower than the read/write frequency since those media access
94 * commands are slow. We start from a "fast" frequency - IDETAPE_DSC_MA_FAST
95 * (1 second), and if we don't receive DSC after IDETAPE_DSC_MA_THRESHOLD
96 * (5 min), we switch it to a lower frequency - IDETAPE_DSC_MA_SLOW (1 min).
1da177e4 97 *
3c98bf34
BP
98 * We also set a timeout for the timer, in case something goes wrong. The
99 * timeout should be longer then the maximum execution time of a tape operation.
1da177e4 100 */
3c98bf34
BP
101
102/* DSC timings. */
1da177e4
LT
103#define IDETAPE_DSC_RW_MIN 5*HZ/100 /* 50 msec */
104#define IDETAPE_DSC_RW_MAX 40*HZ/100 /* 400 msec */
105#define IDETAPE_DSC_RW_TIMEOUT 2*60*HZ /* 2 minutes */
106#define IDETAPE_DSC_MA_FAST 2*HZ /* 2 seconds */
107#define IDETAPE_DSC_MA_THRESHOLD 5*60*HZ /* 5 minutes */
108#define IDETAPE_DSC_MA_SLOW 30*HZ /* 30 seconds */
109#define IDETAPE_DSC_MA_TIMEOUT 2*60*60*HZ /* 2 hours */
110
111/*************************** End of tunable parameters ***********************/
112
54abf37e
BP
113/* tape directions */
114enum {
115 IDETAPE_DIR_NONE = (1 << 0),
116 IDETAPE_DIR_READ = (1 << 1),
117 IDETAPE_DIR_WRITE = (1 << 2),
118};
1da177e4 119
03056b90
BP
120/* Tape door status */
121#define DOOR_UNLOCKED 0
122#define DOOR_LOCKED 1
123#define DOOR_EXPLICITLY_LOCKED 2
124
125/* Some defines for the SPACE command */
126#define IDETAPE_SPACE_OVER_FILEMARK 1
127#define IDETAPE_SPACE_TO_EOD 3
128
129/* Some defines for the LOAD UNLOAD command */
130#define IDETAPE_LU_LOAD_MASK 1
131#define IDETAPE_LU_RETENSION_MASK 2
132#define IDETAPE_LU_EOT_MASK 4
133
03056b90
BP
134/* Structures related to the SELECT SENSE / MODE SENSE packet commands. */
135#define IDETAPE_BLOCK_DESCRIPTOR 0
136#define IDETAPE_CAPABILITIES_PAGE 0x2a
137
1da177e4 138/*
3c98bf34
BP
139 * Most of our global data which we need to save even as we leave the driver due
140 * to an interrupt or a timer event is stored in the struct defined below.
1da177e4
LT
141 */
142typedef struct ide_tape_obj {
7f3c868b
BZ
143 ide_drive_t *drive;
144 struct ide_driver *driver;
145 struct gendisk *disk;
8fed4368 146 struct device dev;
1da177e4 147
2e8a6f89
BZ
148 /* used by REQ_IDETAPE_{READ,WRITE} requests */
149 struct ide_atapi_pc queued_pc;
394a4c21 150
1da177e4 151 /*
3c98bf34 152 * DSC polling variables.
1da177e4 153 *
3c98bf34
BP
154 * While polling for DSC we use postponed_rq to postpone the current
155 * request so that ide.c will be able to service pending requests on the
156 * other device. Note that at most we will have only one DSC (usually
5bd50dc6 157 * data transfer) request in the device request queue.
1da177e4 158 */
6f3848ac
BP
159 bool postponed_rq;
160
1da177e4
LT
161 /* The time in which we started polling for DSC */
162 unsigned long dsc_polling_start;
163 /* Timer used to poll for dsc */
164 struct timer_list dsc_timer;
165 /* Read/Write dsc polling frequency */
54bb2074
BP
166 unsigned long best_dsc_rw_freq;
167 unsigned long dsc_poll_freq;
1da177e4
LT
168 unsigned long dsc_timeout;
169
3c98bf34 170 /* Read position information */
1da177e4
LT
171 u8 partition;
172 /* Current block */
54bb2074 173 unsigned int first_frame;
1da177e4 174
3c98bf34 175 /* Last error information */
1da177e4
LT
176 u8 sense_key, asc, ascq;
177
3c98bf34 178 /* Character device operation */
1da177e4
LT
179 unsigned int minor;
180 /* device name */
181 char name[4];
182 /* Current character device data transfer direction */
54abf37e 183 u8 chrdev_dir;
1da177e4 184
54bb2074
BP
185 /* tape block size, usually 512 or 1024 bytes */
186 unsigned short blk_size;
1da177e4 187 int user_bs_factor;
b6422013 188
1da177e4 189 /* Copy of the tape's Capabilities and Mechanical Page */
b6422013 190 u8 caps[20];
1da177e4
LT
191
192 /*
3c98bf34 193 * Active data transfer request parameters.
1da177e4 194 *
3c98bf34
BP
195 * At most, there is only one ide-tape originated data transfer request
196 * in the device request queue. This allows ide.c to easily service
197 * requests from the other device when we postpone our active request.
1da177e4 198 */
83042b24 199
3c98bf34 200 /* Data buffer size chosen based on the tape's recommendation */
f73850a3 201 int buffer_size;
963da55c
TH
202 /* Staging buffer of buffer_size bytes */
203 void *buf;
204 /* The read/write cursor */
205 void *cur;
206 /* The number of valid bytes in buf */
207 size_t valid;
3c98bf34 208
3c98bf34 209 /* Measures average tape speed */
1da177e4
LT
210 unsigned long avg_time;
211 int avg_size;
212 int avg_speed;
213
1da177e4
LT
214 /* the door is currently locked */
215 int door_locked;
216 /* the tape hardware is write protected */
217 char drv_write_prot;
218 /* the tape is write protected (hardware or opened as read-only) */
219 char write_prot;
1da177e4
LT
220} idetape_tape_t;
221
cf8b8975 222static DEFINE_MUTEX(idetape_ref_mutex);
1da177e4 223
cbba2fa7
BP
224static DEFINE_MUTEX(idetape_chrdev_mutex);
225
d5dee80a
WD
226static struct class *idetape_sysfs_class;
227
8fed4368 228static void ide_tape_release(struct device *);
08da591e 229
9d01e4cd
BP
230static struct ide_tape_obj *idetape_devs[MAX_HWIFS * MAX_DRIVES];
231
232static struct ide_tape_obj *ide_tape_get(struct gendisk *disk, bool cdev,
233 unsigned int i)
1da177e4
LT
234{
235 struct ide_tape_obj *tape = NULL;
236
cf8b8975 237 mutex_lock(&idetape_ref_mutex);
9d01e4cd
BP
238
239 if (cdev)
240 tape = idetape_devs[i];
241 else
242 tape = ide_drv_g(disk, ide_tape_obj);
243
08da591e 244 if (tape) {
d3e33ff5 245 if (ide_device_get(tape->drive))
08da591e 246 tape = NULL;
d3e33ff5 247 else
8fed4368 248 get_device(&tape->dev);
08da591e 249 }
9d01e4cd 250
cf8b8975 251 mutex_unlock(&idetape_ref_mutex);
1da177e4
LT
252 return tape;
253}
254
1da177e4
LT
255static void ide_tape_put(struct ide_tape_obj *tape)
256{
d3e33ff5
BZ
257 ide_drive_t *drive = tape->drive;
258
cf8b8975 259 mutex_lock(&idetape_ref_mutex);
8fed4368 260 put_device(&tape->dev);
d3e33ff5 261 ide_device_put(drive);
cf8b8975 262 mutex_unlock(&idetape_ref_mutex);
1da177e4
LT
263}
264
1da177e4 265/*
1b5db434
BP
266 * called on each failed packet command retry to analyze the request sense. We
267 * currently do not utilize this information.
1da177e4 268 */
ae3a8387 269static void idetape_analyze_error(ide_drive_t *drive)
1da177e4
LT
270{
271 idetape_tape_t *tape = drive->driver_data;
5e2040fd 272 struct ide_atapi_pc *pc = drive->failed_pc;
dfb7e621 273 struct request *rq = drive->hwif->rq;
ae3a8387 274 u8 *sense = bio_data(rq->bio);
1da177e4 275
1b5db434
BP
276 tape->sense_key = sense[2] & 0xF;
277 tape->asc = sense[12];
278 tape->ascq = sense[13];
8004a8c9 279
e972d702
BP
280 ide_debug_log(IDE_DBG_FUNC,
281 "cmd: 0x%x, sense key = %x, asc = %x, ascq = %x",
282 rq->cmd[0], tape->sense_key, tape->asc, tape->ascq);
1da177e4 283
077e6dba 284 /* correct remaining bytes to transfer */
21d9c5d2 285 if (pc->flags & PC_FLAG_DMA_ERROR)
077e6dba 286 rq->resid_len = tape->blk_size * get_unaligned_be32(&sense[3]);
1da177e4
LT
287
288 /*
289 * If error was the result of a zero-length read or write command,
290 * with sense key=5, asc=0x22, ascq=0, let it slide. Some drives
291 * (i.e. Seagate STT3401A Travan) don't support 0-length read/writes.
292 */
90699ce2 293 if ((pc->c[0] == READ_6 || pc->c[0] == WRITE_6)
1b5db434
BP
294 /* length == 0 */
295 && pc->c[4] == 0 && pc->c[3] == 0 && pc->c[2] == 0) {
296 if (tape->sense_key == 5) {
1da177e4
LT
297 /* don't report an error, everything's ok */
298 pc->error = 0;
299 /* don't retry read/write */
346331f8 300 pc->flags |= PC_FLAG_ABORT;
1da177e4
LT
301 }
302 }
90699ce2 303 if (pc->c[0] == READ_6 && (sense[2] & 0x80)) {
c152cc1a 304 pc->error = IDE_DRV_ERROR_FILEMARK;
346331f8 305 pc->flags |= PC_FLAG_ABORT;
1da177e4 306 }
90699ce2 307 if (pc->c[0] == WRITE_6) {
1b5db434
BP
308 if ((sense[2] & 0x40) || (tape->sense_key == 0xd
309 && tape->asc == 0x0 && tape->ascq == 0x2)) {
c152cc1a 310 pc->error = IDE_DRV_ERROR_EOD;
346331f8 311 pc->flags |= PC_FLAG_ABORT;
1da177e4
LT
312 }
313 }
90699ce2 314 if (pc->c[0] == READ_6 || pc->c[0] == WRITE_6) {
1b5db434 315 if (tape->sense_key == 8) {
c152cc1a 316 pc->error = IDE_DRV_ERROR_EOD;
346331f8 317 pc->flags |= PC_FLAG_ABORT;
1da177e4 318 }
346331f8 319 if (!(pc->flags & PC_FLAG_ABORT) &&
077e6dba 320 (blk_rq_bytes(rq) - rq->resid_len))
1da177e4
LT
321 pc->retries = IDETAPE_MAX_PC_RETRIES + 1;
322 }
323}
324
b14c7212
BZ
325static void ide_tape_handle_dsc(ide_drive_t *);
326
03a2faae 327static int ide_tape_callback(ide_drive_t *drive, int dsc)
1da177e4
LT
328{
329 idetape_tape_t *tape = drive->driver_data;
2b9efba4 330 struct ide_atapi_pc *pc = drive->pc;
313afea7 331 struct request *rq = drive->hwif->rq;
5985e6ab 332 int uptodate = pc->error ? 0 : 1;
313afea7 333 int err = uptodate ? 0 : IDE_DRV_ERROR_GENERAL;
1da177e4 334
e972d702
BP
335 ide_debug_log(IDE_DBG_FUNC, "cmd: 0x%x, dsc: %d, err: %d", rq->cmd[0],
336 dsc, err);
8004a8c9 337
b14c7212
BZ
338 if (dsc)
339 ide_tape_handle_dsc(drive);
340
5e2040fd
BZ
341 if (drive->failed_pc == pc)
342 drive->failed_pc = NULL;
dd2e9a03 343
5985e6ab
BZ
344 if (pc->c[0] == REQUEST_SENSE) {
345 if (uptodate)
ae3a8387 346 idetape_analyze_error(drive);
5985e6ab
BZ
347 else
348 printk(KERN_ERR "ide-tape: Error in REQUEST SENSE "
349 "itself - Aborting request!\n");
350 } else if (pc->c[0] == READ_6 || pc->c[0] == WRITE_6) {
077e6dba
BP
351 unsigned int blocks =
352 (blk_rq_bytes(rq) - rq->resid_len) / tape->blk_size;
5985e6ab
BZ
353
354 tape->avg_size += blocks * tape->blk_size;
355
356 if (time_after_eq(jiffies, tape->avg_time + HZ)) {
357 tape->avg_speed = tape->avg_size * HZ /
358 (jiffies - tape->avg_time) / 1024;
359 tape->avg_size = 0;
360 tape->avg_time = jiffies;
361 }
362
363 tape->first_frame += blocks;
5985e6ab 364
313afea7
BZ
365 if (pc->error) {
366 uptodate = 0;
367 err = pc->error;
368 }
1da177e4 369 }
313afea7
BZ
370 rq->errors = err;
371
03a2faae 372 return uptodate;
1da177e4
LT
373}
374
1da177e4 375/*
3c98bf34 376 * Postpone the current request so that ide.c will be able to service requests
b65fac32 377 * from another device on the same port while we are polling for DSC.
1da177e4 378 */
6f3848ac 379static void ide_tape_stall_queue(ide_drive_t *drive)
1da177e4
LT
380{
381 idetape_tape_t *tape = drive->driver_data;
382
e972d702 383 ide_debug_log(IDE_DBG_FUNC, "cmd: 0x%x, dsc_poll_freq: %lu",
6f3848ac 384 drive->hwif->rq->cmd[0], tape->dsc_poll_freq);
8004a8c9 385
6f3848ac 386 tape->postponed_rq = true;
b65fac32 387
54bb2074 388 ide_stall_queue(drive, tape->dsc_poll_freq);
1da177e4
LT
389}
390
74e63e74
BZ
391static void ide_tape_handle_dsc(ide_drive_t *drive)
392{
393 idetape_tape_t *tape = drive->driver_data;
394
395 /* Media access command */
396 tape->dsc_polling_start = jiffies;
397 tape->dsc_poll_freq = IDETAPE_DSC_MA_FAST;
398 tape->dsc_timeout = jiffies + IDETAPE_DSC_MA_TIMEOUT;
399 /* Allow ide.c to handle other requests */
6f3848ac 400 ide_tape_stall_queue(drive);
74e63e74
BZ
401}
402
1da177e4 403/*
3c98bf34 404 * Packet Command Interface
1da177e4 405 *
2b9efba4 406 * The current Packet Command is available in drive->pc, and will not change
3c98bf34
BP
407 * until we finish handling it. Each packet command is associated with a
408 * callback function that will be called when the command is finished.
1da177e4 409 *
3c98bf34 410 * The handling will be done in three stages:
1da177e4 411 *
b788ee9c 412 * 1. ide_tape_issue_pc will send the packet command to the drive, and will set
aa5d2de7 413 * the interrupt handler to ide_pc_intr.
1da177e4 414 *
aa5d2de7 415 * 2. On each interrupt, ide_pc_intr will be called. This step will be
3c98bf34 416 * repeated until the device signals us that no more interrupts will be issued.
1da177e4 417 *
3c98bf34
BP
418 * 3. ATAPI Tape media access commands have immediate status with a delayed
419 * process. In case of a successful initiation of a media access packet command,
420 * the DSC bit will be set when the actual execution of the command is finished.
421 * Since the tape drive will not issue an interrupt, we have to poll for this
422 * event. In this case, we define the request as "low priority request" by
423 * setting rq_status to IDETAPE_RQ_POSTPONED, set a timer to poll for DSC and
424 * exit the driver.
1da177e4 425 *
3c98bf34
BP
426 * ide.c will then give higher priority to requests which originate from the
427 * other device, until will change rq_status to RQ_ACTIVE.
1da177e4 428 *
3c98bf34 429 * 4. When the packet command is finished, it will be checked for errors.
1da177e4 430 *
3c98bf34
BP
431 * 5. In case an error was found, we queue a request sense packet command in
432 * front of the request queue and retry the operation up to
433 * IDETAPE_MAX_PC_RETRIES times.
1da177e4 434 *
3c98bf34
BP
435 * 6. In case no error was found, or we decided to give up and not to retry
436 * again, the callback function will be called and then we will handle the next
437 * request.
1da177e4 438 */
1da177e4 439
b788ee9c
BZ
440static ide_startstop_t ide_tape_issue_pc(ide_drive_t *drive,
441 struct ide_cmd *cmd,
442 struct ide_atapi_pc *pc)
1da177e4 443{
1da177e4 444 idetape_tape_t *tape = drive->driver_data;
dfb7e621 445 struct request *rq = drive->hwif->rq;
1da177e4 446
5e2040fd
BZ
447 if (drive->failed_pc == NULL && pc->c[0] != REQUEST_SENSE)
448 drive->failed_pc = pc;
2b9efba4 449
1da177e4 450 /* Set the current packet command */
2b9efba4 451 drive->pc = pc;
1da177e4
LT
452
453 if (pc->retries > IDETAPE_MAX_PC_RETRIES ||
346331f8 454 (pc->flags & PC_FLAG_ABORT)) {
b3071d19 455
1da177e4 456 /*
3c98bf34
BP
457 * We will "abort" retrying a packet command in case legitimate
458 * error code was received (crossing a filemark, or end of the
459 * media, for example).
1da177e4 460 */
346331f8 461 if (!(pc->flags & PC_FLAG_ABORT)) {
90699ce2 462 if (!(pc->c[0] == TEST_UNIT_READY &&
1da177e4
LT
463 tape->sense_key == 2 && tape->asc == 4 &&
464 (tape->ascq == 1 || tape->ascq == 8))) {
465 printk(KERN_ERR "ide-tape: %s: I/O error, "
466 "pc = %2x, key = %2x, "
467 "asc = %2x, ascq = %2x\n",
468 tape->name, pc->c[0],
469 tape->sense_key, tape->asc,
470 tape->ascq);
471 }
472 /* Giving up */
c152cc1a 473 pc->error = IDE_DRV_ERROR_GENERAL;
1da177e4 474 }
b3071d19 475
5e2040fd 476 drive->failed_pc = NULL;
b14c7212 477 drive->pc_callback(drive, 0);
dfb7e621 478 ide_complete_rq(drive, -EIO, blk_rq_bytes(rq));
92f5daff 479 return ide_stopped;
1da177e4 480 }
e972d702
BP
481 ide_debug_log(IDE_DBG_SENSE, "retry #%d, cmd: 0x%02x", pc->retries,
482 pc->c[0]);
1da177e4
LT
483
484 pc->retries++;
1da177e4 485
b788ee9c 486 return ide_issue_pc(drive, cmd);
1da177e4
LT
487}
488
3c98bf34 489/* A mode sense command is used to "sense" tape parameters. */
d236d74c 490static void idetape_create_mode_sense_cmd(struct ide_atapi_pc *pc, u8 page_code)
1da177e4 491{
7bf7420a 492 ide_init_pc(pc);
90699ce2 493 pc->c[0] = MODE_SENSE;
1da177e4 494 if (page_code != IDETAPE_BLOCK_DESCRIPTOR)
3c98bf34
BP
495 /* DBD = 1 - Don't return block descriptors */
496 pc->c[1] = 8;
1da177e4
LT
497 pc->c[2] = page_code;
498 /*
499 * Changed pc->c[3] to 0 (255 will at best return unused info).
500 *
501 * For SCSI this byte is defined as subpage instead of high byte
502 * of length and some IDE drives seem to interpret it this way
503 * and return an error when 255 is used.
504 */
505 pc->c[3] = 0;
3c98bf34
BP
506 /* We will just discard data in that case */
507 pc->c[4] = 255;
1da177e4 508 if (page_code == IDETAPE_BLOCK_DESCRIPTOR)
d236d74c 509 pc->req_xfer = 12;
1da177e4 510 else if (page_code == IDETAPE_CAPABILITIES_PAGE)
d236d74c 511 pc->req_xfer = 24;
1da177e4 512 else
d236d74c 513 pc->req_xfer = 50;
1da177e4
LT
514}
515
5a04cfa9 516static ide_startstop_t idetape_media_access_finished(ide_drive_t *drive)
1da177e4 517{
b73c7ee2 518 ide_hwif_t *hwif = drive->hwif;
1da177e4 519 idetape_tape_t *tape = drive->driver_data;
2b9efba4 520 struct ide_atapi_pc *pc = drive->pc;
22c525b9 521 u8 stat;
1da177e4 522
374e042c 523 stat = hwif->tp_ops->read_status(hwif);
c47137a9 524
3a7d2484
BZ
525 if (stat & ATA_DSC) {
526 if (stat & ATA_ERR) {
1da177e4 527 /* Error detected */
90699ce2 528 if (pc->c[0] != TEST_UNIT_READY)
1da177e4
LT
529 printk(KERN_ERR "ide-tape: %s: I/O error, ",
530 tape->name);
531 /* Retry operation */
6b544fcc 532 ide_retry_pc(drive);
258ec411 533 return ide_stopped;
1da177e4
LT
534 }
535 pc->error = 0;
1da177e4 536 } else {
c152cc1a 537 pc->error = IDE_DRV_ERROR_GENERAL;
5e2040fd 538 drive->failed_pc = NULL;
1da177e4 539 }
b14c7212 540 drive->pc_callback(drive, 0);
1da177e4
LT
541 return ide_stopped;
542}
543
cd2abbfe 544static void ide_tape_create_rw_cmd(idetape_tape_t *tape,
0014c75b
BP
545 struct ide_atapi_pc *pc, struct request *rq,
546 u8 opcode)
1da177e4 547{
10c0b343 548 unsigned int length = blk_rq_sectors(rq) / (tape->blk_size >> 9);
0014c75b 549
7bf7420a 550 ide_init_pc(pc);
860ff5ec 551 put_unaligned(cpu_to_be32(length), (unsigned int *) &pc->c[1]);
1da177e4 552 pc->c[1] = 1;
19f52a78
BP
553
554 if (blk_rq_bytes(rq) == tape->buffer_size)
5e331095 555 pc->flags |= PC_FLAG_DMA_OK;
1da177e4 556
963da55c 557 if (opcode == READ_6)
cd2abbfe 558 pc->c[0] = READ_6;
963da55c 559 else if (opcode == WRITE_6) {
cd2abbfe
BP
560 pc->c[0] = WRITE_6;
561 pc->flags |= PC_FLAG_WRITING;
cd2abbfe 562 }
0014c75b
BP
563
564 memcpy(rq->cmd, pc->c, 12);
1da177e4
LT
565}
566
1da177e4
LT
567static ide_startstop_t idetape_do_request(ide_drive_t *drive,
568 struct request *rq, sector_t block)
569{
b73c7ee2 570 ide_hwif_t *hwif = drive->hwif;
1da177e4 571 idetape_tape_t *tape = drive->driver_data;
d236d74c 572 struct ide_atapi_pc *pc = NULL;
b788ee9c 573 struct ide_cmd cmd;
22c525b9 574 u8 stat;
1da177e4 575
e972d702
BP
576 ide_debug_log(IDE_DBG_RQ, "cmd: 0x%x, sector: %llu, nr_sectors: %u",
577 rq->cmd[0], (unsigned long long)blk_rq_pos(rq),
578 blk_rq_sectors(rq));
1da177e4 579
2c7eaa43 580 BUG_ON(!(blk_special_request(rq) || blk_sense_request(rq)));
1da177e4 581
3c98bf34 582 /* Retry a failed packet command */
5e2040fd
BZ
583 if (drive->failed_pc && drive->pc->c[0] == REQUEST_SENSE) {
584 pc = drive->failed_pc;
28c7214b
BZ
585 goto out;
586 }
5a04cfa9 587
1da177e4
LT
588 /*
589 * If the tape is still busy, postpone our request and service
590 * the other device meanwhile.
591 */
374e042c 592 stat = hwif->tp_ops->read_status(hwif);
1da177e4 593
97100fc8
BZ
594 if ((drive->dev_flags & IDE_DFLAG_DSC_OVERLAP) == 0 &&
595 (rq->cmd[13] & REQ_IDETAPE_PC2) == 0)
626542ca 596 drive->atapi_flags |= IDE_AFLAG_IGNORE_DSC;
1da177e4 597
97100fc8 598 if (drive->dev_flags & IDE_DFLAG_POST_RESET) {
626542ca 599 drive->atapi_flags |= IDE_AFLAG_IGNORE_DSC;
97100fc8 600 drive->dev_flags &= ~IDE_DFLAG_POST_RESET;
1da177e4
LT
601 }
602
626542ca
BP
603 if (!(drive->atapi_flags & IDE_AFLAG_IGNORE_DSC) &&
604 !(stat & ATA_DSC)) {
6f3848ac 605 if (!tape->postponed_rq) {
1da177e4 606 tape->dsc_polling_start = jiffies;
54bb2074 607 tape->dsc_poll_freq = tape->best_dsc_rw_freq;
1da177e4
LT
608 tape->dsc_timeout = jiffies + IDETAPE_DSC_RW_TIMEOUT;
609 } else if (time_after(jiffies, tape->dsc_timeout)) {
610 printk(KERN_ERR "ide-tape: %s: DSC timeout\n",
611 tape->name);
83dd5735 612 if (rq->cmd[13] & REQ_IDETAPE_PC2) {
1da177e4
LT
613 idetape_media_access_finished(drive);
614 return ide_stopped;
615 } else {
616 return ide_do_reset(drive);
617 }
5a04cfa9
BP
618 } else if (time_after(jiffies,
619 tape->dsc_polling_start +
620 IDETAPE_DSC_MA_THRESHOLD))
54bb2074 621 tape->dsc_poll_freq = IDETAPE_DSC_MA_SLOW;
6f3848ac 622 ide_tape_stall_queue(drive);
1da177e4 623 return ide_stopped;
6f3848ac 624 } else {
626542ca 625 drive->atapi_flags &= ~IDE_AFLAG_IGNORE_DSC;
6f3848ac
BP
626 tape->postponed_rq = false;
627 }
626542ca 628
83dd5735 629 if (rq->cmd[13] & REQ_IDETAPE_READ) {
2e8a6f89 630 pc = &tape->queued_pc;
0014c75b 631 ide_tape_create_rw_cmd(tape, pc, rq, READ_6);
1da177e4
LT
632 goto out;
633 }
83dd5735 634 if (rq->cmd[13] & REQ_IDETAPE_WRITE) {
2e8a6f89 635 pc = &tape->queued_pc;
0014c75b 636 ide_tape_create_rw_cmd(tape, pc, rq, WRITE_6);
1da177e4
LT
637 goto out;
638 }
83dd5735 639 if (rq->cmd[13] & REQ_IDETAPE_PC1) {
c267cc1c 640 pc = (struct ide_atapi_pc *)rq->special;
83dd5735
BP
641 rq->cmd[13] &= ~(REQ_IDETAPE_PC1);
642 rq->cmd[13] |= REQ_IDETAPE_PC2;
1da177e4
LT
643 goto out;
644 }
83dd5735 645 if (rq->cmd[13] & REQ_IDETAPE_PC2) {
1da177e4
LT
646 idetape_media_access_finished(drive);
647 return ide_stopped;
648 }
649 BUG();
28c7214b 650
f2e3ab52 651out:
6b544fcc
BP
652 /* prepare sense request for this command */
653 ide_prep_sense(drive, rq);
654
b788ee9c
BZ
655 memset(&cmd, 0, sizeof(cmd));
656
657 if (rq_data_dir(rq))
658 cmd.tf_flags |= IDE_TFLAG_WRITE;
659
660 cmd.rq = rq;
661
dfb7e621 662 ide_init_sg_cmd(&cmd, blk_rq_bytes(rq));
5c4be572
TH
663 ide_map_sg(drive, &cmd);
664
b788ee9c 665 return ide_tape_issue_pc(drive, &cmd, pc);
1da177e4
LT
666}
667
1da177e4 668/*
3c98bf34
BP
669 * Write a filemark if write_filemark=1. Flush the device buffers without
670 * writing a filemark otherwise.
1da177e4 671 */
5a04cfa9 672static void idetape_create_write_filemark_cmd(ide_drive_t *drive,
d236d74c 673 struct ide_atapi_pc *pc, int write_filemark)
1da177e4 674{
7bf7420a 675 ide_init_pc(pc);
90699ce2 676 pc->c[0] = WRITE_FILEMARKS;
1da177e4 677 pc->c[4] = write_filemark;
346331f8 678 pc->flags |= PC_FLAG_WAIT_FOR_DSC;
1da177e4
LT
679}
680
1da177e4
LT
681static int idetape_wait_ready(ide_drive_t *drive, unsigned long timeout)
682{
683 idetape_tape_t *tape = drive->driver_data;
2ac07d92 684 struct gendisk *disk = tape->disk;
1da177e4
LT
685 int load_attempted = 0;
686
3c98bf34 687 /* Wait for the tape to become ready */
49d8078a 688 set_bit(ilog2(IDE_AFLAG_MEDIUM_PRESENT), &drive->atapi_flags);
1da177e4
LT
689 timeout += jiffies;
690 while (time_before(jiffies, timeout)) {
de699ad5 691 if (ide_do_test_unit_ready(drive, disk) == 0)
1da177e4
LT
692 return 0;
693 if ((tape->sense_key == 2 && tape->asc == 4 && tape->ascq == 2)
3c98bf34
BP
694 || (tape->asc == 0x3A)) {
695 /* no media */
1da177e4
LT
696 if (load_attempted)
697 return -ENOMEDIUM;
0c8a6c7a 698 ide_do_start_stop(drive, disk, IDETAPE_LU_LOAD_MASK);
1da177e4
LT
699 load_attempted = 1;
700 /* not about to be ready */
701 } else if (!(tape->sense_key == 2 && tape->asc == 4 &&
702 (tape->ascq == 1 || tape->ascq == 8)))
703 return -EIO;
80ce45fd 704 msleep(100);
1da177e4
LT
705 }
706 return -EIO;
707}
708
5a04cfa9 709static int idetape_flush_tape_buffers(ide_drive_t *drive)
1da177e4 710{
2ac07d92 711 struct ide_tape_obj *tape = drive->driver_data;
d236d74c 712 struct ide_atapi_pc pc;
1da177e4
LT
713 int rc;
714
715 idetape_create_write_filemark_cmd(drive, &pc, 0);
b13345f3 716 rc = ide_queue_pc_tail(drive, tape->disk, &pc, NULL, 0);
5a04cfa9 717 if (rc)
1da177e4
LT
718 return rc;
719 idetape_wait_ready(drive, 60 * 5 * HZ);
720 return 0;
721}
722
55ce3a12 723static int ide_tape_read_position(ide_drive_t *drive)
1da177e4
LT
724{
725 idetape_tape_t *tape = drive->driver_data;
d236d74c 726 struct ide_atapi_pc pc;
55ce3a12 727 u8 buf[20];
1da177e4 728
e972d702 729 ide_debug_log(IDE_DBG_FUNC, "enter");
1da177e4 730
55ce3a12
BP
731 /* prep cmd */
732 ide_init_pc(&pc);
733 pc.c[0] = READ_POSITION;
734 pc.req_xfer = 20;
735
736 if (ide_queue_pc_tail(drive, tape->disk, &pc, buf, pc.req_xfer))
1da177e4 737 return -1;
55ce3a12
BP
738
739 if (!pc.error) {
e972d702 740 ide_debug_log(IDE_DBG_FUNC, "BOP - %s",
55ce3a12 741 (buf[0] & 0x80) ? "Yes" : "No");
e972d702 742 ide_debug_log(IDE_DBG_FUNC, "EOP - %s",
55ce3a12
BP
743 (buf[0] & 0x40) ? "Yes" : "No");
744
745 if (buf[0] & 0x4) {
746 printk(KERN_INFO "ide-tape: Block location is unknown"
747 "to the tape\n");
8dcce408
BZ
748 clear_bit(ilog2(IDE_AFLAG_ADDRESS_VALID),
749 &drive->atapi_flags);
55ce3a12
BP
750 return -1;
751 } else {
e972d702
BP
752 ide_debug_log(IDE_DBG_FUNC, "Block Location: %u",
753 be32_to_cpup((__be32 *)&buf[4]));
55ce3a12
BP
754
755 tape->partition = buf[1];
756 tape->first_frame = be32_to_cpup((__be32 *)&buf[4]);
8dcce408
BZ
757 set_bit(ilog2(IDE_AFLAG_ADDRESS_VALID),
758 &drive->atapi_flags);
55ce3a12
BP
759 }
760 }
761
762 return tape->first_frame;
1da177e4
LT
763}
764
d236d74c
BP
765static void idetape_create_locate_cmd(ide_drive_t *drive,
766 struct ide_atapi_pc *pc,
5a04cfa9 767 unsigned int block, u8 partition, int skip)
1da177e4 768{
7bf7420a 769 ide_init_pc(pc);
90699ce2 770 pc->c[0] = POSITION_TO_ELEMENT;
1da177e4 771 pc->c[1] = 2;
860ff5ec 772 put_unaligned(cpu_to_be32(block), (unsigned int *) &pc->c[3]);
1da177e4 773 pc->c[8] = partition;
346331f8 774 pc->flags |= PC_FLAG_WAIT_FOR_DSC;
1da177e4
LT
775}
776
ec0fdb01 777static void __ide_tape_discard_merge_buffer(ide_drive_t *drive)
1da177e4
LT
778{
779 idetape_tape_t *tape = drive->driver_data;
1da177e4 780
54abf37e 781 if (tape->chrdev_dir != IDETAPE_DIR_READ)
9798630a 782 return;
1da177e4 783
49d8078a 784 clear_bit(ilog2(IDE_AFLAG_FILEMARK), &drive->atapi_flags);
963da55c
TH
785 tape->valid = 0;
786 if (tape->buf != NULL) {
787 kfree(tape->buf);
788 tape->buf = NULL;
1da177e4
LT
789 }
790
54abf37e 791 tape->chrdev_dir = IDETAPE_DIR_NONE;
1da177e4
LT
792}
793
794/*
3c98bf34
BP
795 * Position the tape to the requested block using the LOCATE packet command.
796 * A READ POSITION command is then issued to check where we are positioned. Like
797 * all higher level operations, we queue the commands at the tail of the request
798 * queue and wait for their completion.
1da177e4 799 */
5a04cfa9
BP
800static int idetape_position_tape(ide_drive_t *drive, unsigned int block,
801 u8 partition, int skip)
1da177e4
LT
802{
803 idetape_tape_t *tape = drive->driver_data;
2ac07d92 804 struct gendisk *disk = tape->disk;
55ce3a12 805 int ret;
d236d74c 806 struct ide_atapi_pc pc;
1da177e4 807
54abf37e 808 if (tape->chrdev_dir == IDETAPE_DIR_READ)
ec0fdb01 809 __ide_tape_discard_merge_buffer(drive);
1da177e4
LT
810 idetape_wait_ready(drive, 60 * 5 * HZ);
811 idetape_create_locate_cmd(drive, &pc, block, partition, skip);
55ce3a12
BP
812 ret = ide_queue_pc_tail(drive, disk, &pc, NULL, 0);
813 if (ret)
814 return ret;
1da177e4 815
55ce3a12
BP
816 ret = ide_tape_read_position(drive);
817 if (ret < 0)
818 return ret;
819 return 0;
1da177e4
LT
820}
821
ec0fdb01 822static void ide_tape_discard_merge_buffer(ide_drive_t *drive,
5a04cfa9 823 int restore_position)
1da177e4
LT
824{
825 idetape_tape_t *tape = drive->driver_data;
1da177e4
LT
826 int seek, position;
827
ec0fdb01 828 __ide_tape_discard_merge_buffer(drive);
1da177e4 829 if (restore_position) {
55ce3a12 830 position = ide_tape_read_position(drive);
9798630a 831 seek = position > 0 ? position : 0;
1da177e4 832 if (idetape_position_tape(drive, seek, 0, 0)) {
5a04cfa9 833 printk(KERN_INFO "ide-tape: %s: position_tape failed in"
ec0fdb01 834 " %s\n", tape->name, __func__);
1da177e4
LT
835 return;
836 }
837 }
838}
839
840/*
3c98bf34
BP
841 * Generate a read/write request for the block device interface and wait for it
842 * to be serviced.
1da177e4 843 */
6bb11dd1 844static int idetape_queue_rw_tail(ide_drive_t *drive, int cmd, int size)
1da177e4
LT
845{
846 idetape_tape_t *tape = drive->driver_data;
64ea1b4a 847 struct request *rq;
21d9c5d2 848 int ret;
1da177e4 849
e972d702
BP
850 ide_debug_log(IDE_DBG_FUNC, "cmd: 0x%x, size: %d", cmd, size);
851
21d9c5d2 852 BUG_ON(cmd != REQ_IDETAPE_READ && cmd != REQ_IDETAPE_WRITE);
6bb11dd1 853 BUG_ON(size < 0 || size % tape->blk_size);
8004a8c9 854
64ea1b4a
FT
855 rq = blk_get_request(drive->queue, READ, __GFP_WAIT);
856 rq->cmd_type = REQ_TYPE_SPECIAL;
83dd5735 857 rq->cmd[13] = cmd;
64ea1b4a 858 rq->rq_disk = tape->disk;
c9059598 859 rq->__sector = tape->first_frame;
21d9c5d2
TH
860
861 if (size) {
963da55c 862 ret = blk_rq_map_kern(drive->queue, rq, tape->buf, size,
21d9c5d2
TH
863 __GFP_WAIT);
864 if (ret)
865 goto out_put;
866 }
867
64ea1b4a
FT
868 blk_execute_rq(drive->queue, tape->disk, rq, 0);
869
963da55c 870 /* calculate the number of transferred bytes and update buffer state */
c3a4d78c 871 size -= rq->resid_len;
963da55c 872 tape->cur = tape->buf;
21d9c5d2 873 if (cmd == REQ_IDETAPE_READ)
963da55c
TH
874 tape->valid = size;
875 else
876 tape->valid = 0;
1da177e4 877
21d9c5d2
TH
878 ret = size;
879 if (rq->errors == IDE_DRV_ERROR_GENERAL)
880 ret = -EIO;
21d9c5d2
TH
881out_put:
882 blk_put_request(rq);
64ea1b4a 883 return ret;
1da177e4
LT
884}
885
d236d74c 886static void idetape_create_inquiry_cmd(struct ide_atapi_pc *pc)
1da177e4 887{
7bf7420a 888 ide_init_pc(pc);
90699ce2 889 pc->c[0] = INQUIRY;
5a04cfa9 890 pc->c[4] = 254;
d236d74c 891 pc->req_xfer = 254;
1da177e4
LT
892}
893
d236d74c
BP
894static void idetape_create_rewind_cmd(ide_drive_t *drive,
895 struct ide_atapi_pc *pc)
1da177e4 896{
7bf7420a 897 ide_init_pc(pc);
90699ce2 898 pc->c[0] = REZERO_UNIT;
346331f8 899 pc->flags |= PC_FLAG_WAIT_FOR_DSC;
1da177e4
LT
900}
901
d236d74c 902static void idetape_create_erase_cmd(struct ide_atapi_pc *pc)
1da177e4 903{
7bf7420a 904 ide_init_pc(pc);
90699ce2 905 pc->c[0] = ERASE;
1da177e4 906 pc->c[1] = 1;
346331f8 907 pc->flags |= PC_FLAG_WAIT_FOR_DSC;
1da177e4
LT
908}
909
d236d74c 910static void idetape_create_space_cmd(struct ide_atapi_pc *pc, int count, u8 cmd)
1da177e4 911{
7bf7420a 912 ide_init_pc(pc);
90699ce2 913 pc->c[0] = SPACE;
860ff5ec 914 put_unaligned(cpu_to_be32(count), (unsigned int *) &pc->c[1]);
1da177e4 915 pc->c[1] = cmd;
346331f8 916 pc->flags |= PC_FLAG_WAIT_FOR_DSC;
1da177e4
LT
917}
918
d9df937a 919static void ide_tape_flush_merge_buffer(ide_drive_t *drive)
1da177e4
LT
920{
921 idetape_tape_t *tape = drive->driver_data;
55a5d291 922
54abf37e 923 if (tape->chrdev_dir != IDETAPE_DIR_WRITE) {
077e3bdb 924 printk(KERN_ERR "ide-tape: bug: Trying to empty merge buffer"
5a04cfa9 925 " but we are not writing.\n");
1da177e4
LT
926 return;
927 }
963da55c 928 if (tape->buf) {
6bb11dd1
TH
929 size_t aligned = roundup(tape->valid, tape->blk_size);
930
931 memset(tape->cur, 0, aligned - tape->valid);
07bd9686 932 idetape_queue_rw_tail(drive, REQ_IDETAPE_WRITE, aligned);
963da55c
TH
933 kfree(tape->buf);
934 tape->buf = NULL;
1da177e4 935 }
54abf37e 936 tape->chrdev_dir = IDETAPE_DIR_NONE;
1da177e4
LT
937}
938
88f1b941 939static int idetape_init_rw(ide_drive_t *drive, int dir)
1da177e4
LT
940{
941 idetape_tape_t *tape = drive->driver_data;
88f1b941 942 int rc;
1da177e4 943
88f1b941 944 BUG_ON(dir != IDETAPE_DIR_READ && dir != IDETAPE_DIR_WRITE);
1da177e4 945
88f1b941
TH
946 if (tape->chrdev_dir == dir)
947 return 0;
948
949 if (tape->chrdev_dir == IDETAPE_DIR_READ)
950 ide_tape_discard_merge_buffer(drive, 1);
951 else if (tape->chrdev_dir == IDETAPE_DIR_WRITE) {
952 ide_tape_flush_merge_buffer(drive);
953 idetape_flush_tape_buffers(drive);
954 }
955
956 if (tape->buf || tape->valid) {
957 printk(KERN_ERR "ide-tape: valid should be 0 now\n");
958 tape->valid = 0;
959 }
960
961 tape->buf = kmalloc(tape->buffer_size, GFP_KERNEL);
962 if (!tape->buf)
963 return -ENOMEM;
964 tape->chrdev_dir = dir;
965 tape->cur = tape->buf;
966
967 /*
968 * Issue a 0 rw command to ensure that DSC handshake is
969 * switched from completion mode to buffer available mode. No
970 * point in issuing this if DSC overlap isn't supported, some
971 * drives (Seagate STT3401A) will return an error.
972 */
973 if (drive->dev_flags & IDE_DFLAG_DSC_OVERLAP) {
974 int cmd = dir == IDETAPE_DIR_READ ? REQ_IDETAPE_READ
975 : REQ_IDETAPE_WRITE;
976
977 rc = idetape_queue_rw_tail(drive, cmd, 0);
978 if (rc < 0) {
979 kfree(tape->buf);
980 tape->buf = NULL;
981 tape->chrdev_dir = IDETAPE_DIR_NONE;
982 return rc;
1da177e4
LT
983 }
984 }
5e69bd95 985
1da177e4
LT
986 return 0;
987}
988
5a04cfa9 989static void idetape_pad_zeros(ide_drive_t *drive, int bcount)
1da177e4
LT
990{
991 idetape_tape_t *tape = drive->driver_data;
963da55c
TH
992
993 memset(tape->buf, 0, tape->buffer_size);
3c98bf34 994
1da177e4 995 while (bcount) {
963da55c 996 unsigned int count = min(tape->buffer_size, bcount);
1da177e4 997
6bb11dd1 998 idetape_queue_rw_tail(drive, REQ_IDETAPE_WRITE, count);
1da177e4 999 bcount -= count;
1da177e4
LT
1000 }
1001}
1002
1da177e4 1003/*
3c98bf34
BP
1004 * Rewinds the tape to the Beginning Of the current Partition (BOP). We
1005 * currently support only one partition.
1006 */
5a04cfa9 1007static int idetape_rewind_tape(ide_drive_t *drive)
1da177e4 1008{
2ac07d92
BZ
1009 struct ide_tape_obj *tape = drive->driver_data;
1010 struct gendisk *disk = tape->disk;
d236d74c 1011 struct ide_atapi_pc pc;
55ce3a12 1012 int ret;
8004a8c9 1013
e972d702 1014 ide_debug_log(IDE_DBG_FUNC, "enter");
8004a8c9 1015
1da177e4 1016 idetape_create_rewind_cmd(drive, &pc);
55ce3a12
BP
1017 ret = ide_queue_pc_tail(drive, disk, &pc, NULL, 0);
1018 if (ret)
1019 return ret;
1da177e4 1020
55ce3a12
BP
1021 ret = ide_tape_read_position(drive);
1022 if (ret < 0)
1023 return ret;
1da177e4
LT
1024 return 0;
1025}
1026
3c98bf34 1027/* mtio.h compatible commands should be issued to the chrdev interface. */
5a04cfa9
BP
1028static int idetape_blkdev_ioctl(ide_drive_t *drive, unsigned int cmd,
1029 unsigned long arg)
1da177e4
LT
1030{
1031 idetape_tape_t *tape = drive->driver_data;
1da177e4
LT
1032 void __user *argp = (void __user *)arg;
1033
d59823fa
BP
1034 struct idetape_config {
1035 int dsc_rw_frequency;
1036 int dsc_media_access_frequency;
1037 int nr_stages;
1038 } config;
1039
e972d702 1040 ide_debug_log(IDE_DBG_FUNC, "cmd: 0x%04x", cmd);
8004a8c9 1041
1da177e4 1042 switch (cmd) {
5a04cfa9
BP
1043 case 0x0340:
1044 if (copy_from_user(&config, argp, sizeof(config)))
1045 return -EFAULT;
1046 tape->best_dsc_rw_freq = config.dsc_rw_frequency;
5a04cfa9
BP
1047 break;
1048 case 0x0350:
2fc2111c 1049 memset(&config, 0, sizeof(config));
5a04cfa9 1050 config.dsc_rw_frequency = (int) tape->best_dsc_rw_freq;
83042b24 1051 config.nr_stages = 1;
5a04cfa9
BP
1052 if (copy_to_user(argp, &config, sizeof(config)))
1053 return -EFAULT;
1054 break;
1055 default:
1056 return -EIO;
1da177e4
LT
1057 }
1058 return 0;
1059}
1060
5a04cfa9
BP
1061static int idetape_space_over_filemarks(ide_drive_t *drive, short mt_op,
1062 int mt_count)
1da177e4
LT
1063{
1064 idetape_tape_t *tape = drive->driver_data;
2ac07d92 1065 struct gendisk *disk = tape->disk;
d236d74c 1066 struct ide_atapi_pc pc;
5a04cfa9 1067 int retval, count = 0;
b6422013 1068 int sprev = !!(tape->caps[4] & 0x20);
1da177e4 1069
e972d702
BP
1070
1071 ide_debug_log(IDE_DBG_FUNC, "mt_op: %d, mt_count: %d", mt_op, mt_count);
1072
1da177e4
LT
1073 if (mt_count == 0)
1074 return 0;
1075 if (MTBSF == mt_op || MTBSFM == mt_op) {
b6422013 1076 if (!sprev)
1da177e4 1077 return -EIO;
5a04cfa9 1078 mt_count = -mt_count;
1da177e4
LT
1079 }
1080
54abf37e 1081 if (tape->chrdev_dir == IDETAPE_DIR_READ) {
963da55c 1082 tape->valid = 0;
49d8078a
BP
1083 if (test_and_clear_bit(ilog2(IDE_AFLAG_FILEMARK),
1084 &drive->atapi_flags))
1da177e4 1085 ++count;
ec0fdb01 1086 ide_tape_discard_merge_buffer(drive, 0);
1da177e4
LT
1087 }
1088
1da177e4 1089 switch (mt_op) {
5a04cfa9
BP
1090 case MTFSF:
1091 case MTBSF:
1092 idetape_create_space_cmd(&pc, mt_count - count,
1093 IDETAPE_SPACE_OVER_FILEMARK);
b13345f3 1094 return ide_queue_pc_tail(drive, disk, &pc, NULL, 0);
5a04cfa9
BP
1095 case MTFSFM:
1096 case MTBSFM:
1097 if (!sprev)
1098 return -EIO;
1099 retval = idetape_space_over_filemarks(drive, MTFSF,
1100 mt_count - count);
1101 if (retval)
1102 return retval;
1103 count = (MTBSFM == mt_op ? 1 : -1);
1104 return idetape_space_over_filemarks(drive, MTFSF, count);
1105 default:
1106 printk(KERN_ERR "ide-tape: MTIO operation %d not supported\n",
1107 mt_op);
1108 return -EIO;
1da177e4
LT
1109 }
1110}
1111
1da177e4 1112/*
3c98bf34 1113 * Our character device read / write functions.
1da177e4 1114 *
3c98bf34
BP
1115 * The tape is optimized to maximize throughput when it is transferring an
1116 * integral number of the "continuous transfer limit", which is a parameter of
1117 * the specific tape (26kB on my particular tape, 32kB for Onstream).
1da177e4 1118 *
3c98bf34
BP
1119 * As of version 1.3 of the driver, the character device provides an abstract
1120 * continuous view of the media - any mix of block sizes (even 1 byte) on the
1121 * same backup/restore procedure is supported. The driver will internally
1122 * convert the requests to the recommended transfer unit, so that an unmatch
1123 * between the user's block size to the recommended size will only result in a
1124 * (slightly) increased driver overhead, but will no longer hit performance.
1125 * This is not applicable to Onstream.
1da177e4 1126 */
5a04cfa9
BP
1127static ssize_t idetape_chrdev_read(struct file *file, char __user *buf,
1128 size_t count, loff_t *ppos)
1da177e4 1129{
5aeddf90 1130 struct ide_tape_obj *tape = file->private_data;
1da177e4 1131 ide_drive_t *drive = tape->drive;
07bd9686 1132 size_t done = 0;
dcd96379 1133 ssize_t ret = 0;
07bd9686 1134 int rc;
1da177e4 1135
e972d702 1136 ide_debug_log(IDE_DBG_FUNC, "count %Zd", count);
1da177e4 1137
54abf37e 1138 if (tape->chrdev_dir != IDETAPE_DIR_READ) {
49d8078a 1139 if (test_bit(ilog2(IDE_AFLAG_DETECT_BS), &drive->atapi_flags))
54bb2074
BP
1140 if (count > tape->blk_size &&
1141 (count % tape->blk_size) == 0)
1142 tape->user_bs_factor = count / tape->blk_size;
1da177e4 1143 }
07bd9686 1144
88f1b941 1145 rc = idetape_init_rw(drive, IDETAPE_DIR_READ);
8d06bfad 1146 if (rc < 0)
1da177e4 1147 return rc;
07bd9686
TH
1148
1149 while (done < count) {
1150 size_t todo;
1151
1152 /* refill if staging buffer is empty */
1153 if (!tape->valid) {
1154 /* If we are at a filemark, nothing more to read */
49d8078a
BP
1155 if (test_bit(ilog2(IDE_AFLAG_FILEMARK),
1156 &drive->atapi_flags))
07bd9686
TH
1157 break;
1158 /* read */
1159 if (idetape_queue_rw_tail(drive, REQ_IDETAPE_READ,
1160 tape->buffer_size) <= 0)
1161 break;
1162 }
1163
1164 /* copy out */
1165 todo = min_t(size_t, count - done, tape->valid);
1166 if (copy_to_user(buf + done, tape->cur, todo))
dcd96379 1167 ret = -EFAULT;
07bd9686
TH
1168
1169 tape->cur += todo;
1170 tape->valid -= todo;
1171 done += todo;
1da177e4 1172 }
07bd9686 1173
49d8078a 1174 if (!done && test_bit(ilog2(IDE_AFLAG_FILEMARK), &drive->atapi_flags)) {
1da177e4
LT
1175 idetape_space_over_filemarks(drive, MTFSF, 1);
1176 return 0;
1177 }
dcd96379 1178
07bd9686 1179 return ret ? ret : done;
1da177e4
LT
1180}
1181
5a04cfa9 1182static ssize_t idetape_chrdev_write(struct file *file, const char __user *buf,
1da177e4
LT
1183 size_t count, loff_t *ppos)
1184{
5aeddf90 1185 struct ide_tape_obj *tape = file->private_data;
1da177e4 1186 ide_drive_t *drive = tape->drive;
07bd9686 1187 size_t done = 0;
dcd96379 1188 ssize_t ret = 0;
88f1b941 1189 int rc;
1da177e4
LT
1190
1191 /* The drive is write protected. */
1192 if (tape->write_prot)
1193 return -EACCES;
1194
e972d702 1195 ide_debug_log(IDE_DBG_FUNC, "count %Zd", count);
1da177e4
LT
1196
1197 /* Initialize write operation */
88f1b941
TH
1198 rc = idetape_init_rw(drive, IDETAPE_DIR_WRITE);
1199 if (rc < 0)
1200 return rc;
07bd9686
TH
1201
1202 while (done < count) {
1203 size_t todo;
1204
1205 /* flush if staging buffer is full */
1206 if (tape->valid == tape->buffer_size &&
1207 idetape_queue_rw_tail(drive, REQ_IDETAPE_WRITE,
1208 tape->buffer_size) <= 0)
1209 return rc;
1210
1211 /* copy in */
1212 todo = min_t(size_t, count - done,
1213 tape->buffer_size - tape->valid);
1214 if (copy_from_user(tape->cur, buf + done, todo))
dcd96379 1215 ret = -EFAULT;
07bd9686
TH
1216
1217 tape->cur += todo;
1218 tape->valid += todo;
1219 done += todo;
1da177e4 1220 }
07bd9686
TH
1221
1222 return ret ? ret : done;
1da177e4
LT
1223}
1224
5a04cfa9 1225static int idetape_write_filemark(ide_drive_t *drive)
1da177e4 1226{
2ac07d92 1227 struct ide_tape_obj *tape = drive->driver_data;
d236d74c 1228 struct ide_atapi_pc pc;
1da177e4
LT
1229
1230 /* Write a filemark */
1231 idetape_create_write_filemark_cmd(drive, &pc, 1);
b13345f3 1232 if (ide_queue_pc_tail(drive, tape->disk, &pc, NULL, 0)) {
1da177e4
LT
1233 printk(KERN_ERR "ide-tape: Couldn't write a filemark\n");
1234 return -EIO;
1235 }
1236 return 0;
1237}
1238
1239/*
d99c9da2
BP
1240 * Called from idetape_chrdev_ioctl when the general mtio MTIOCTOP ioctl is
1241 * requested.
1da177e4 1242 *
d99c9da2
BP
1243 * Note: MTBSF and MTBSFM are not supported when the tape doesn't support
1244 * spacing over filemarks in the reverse direction. In this case, MTFSFM is also
5bd50dc6 1245 * usually not supported.
1da177e4 1246 *
d99c9da2 1247 * The following commands are currently not supported:
1da177e4 1248 *
d99c9da2
BP
1249 * MTFSS, MTBSS, MTWSM, MTSETDENSITY, MTSETDRVBUFFER, MT_ST_BOOLEANS,
1250 * MT_ST_WRITE_THRESHOLD.
1da177e4 1251 */
d99c9da2 1252static int idetape_mtioctop(ide_drive_t *drive, short mt_op, int mt_count)
1da177e4
LT
1253{
1254 idetape_tape_t *tape = drive->driver_data;
2ac07d92 1255 struct gendisk *disk = tape->disk;
d236d74c 1256 struct ide_atapi_pc pc;
5a04cfa9 1257 int i, retval;
1da177e4 1258
e972d702
BP
1259 ide_debug_log(IDE_DBG_FUNC, "MTIOCTOP ioctl: mt_op: %d, mt_count: %d",
1260 mt_op, mt_count);
5a04cfa9 1261
1da177e4 1262 switch (mt_op) {
5a04cfa9
BP
1263 case MTFSF:
1264 case MTFSFM:
1265 case MTBSF:
1266 case MTBSFM:
1267 if (!mt_count)
1268 return 0;
1269 return idetape_space_over_filemarks(drive, mt_op, mt_count);
1270 default:
1271 break;
1da177e4 1272 }
5a04cfa9 1273
1da177e4 1274 switch (mt_op) {
5a04cfa9
BP
1275 case MTWEOF:
1276 if (tape->write_prot)
1277 return -EACCES;
ec0fdb01 1278 ide_tape_discard_merge_buffer(drive, 1);
5a04cfa9
BP
1279 for (i = 0; i < mt_count; i++) {
1280 retval = idetape_write_filemark(drive);
1281 if (retval)
1282 return retval;
1283 }
1284 return 0;
1285 case MTREW:
ec0fdb01 1286 ide_tape_discard_merge_buffer(drive, 0);
5a04cfa9
BP
1287 if (idetape_rewind_tape(drive))
1288 return -EIO;
1289 return 0;
1290 case MTLOAD:
ec0fdb01 1291 ide_tape_discard_merge_buffer(drive, 0);
0c8a6c7a 1292 return ide_do_start_stop(drive, disk, IDETAPE_LU_LOAD_MASK);
5a04cfa9
BP
1293 case MTUNLOAD:
1294 case MTOFFL:
1295 /*
1296 * If door is locked, attempt to unlock before
1297 * attempting to eject.
1298 */
1299 if (tape->door_locked) {
0578042d 1300 if (!ide_set_media_lock(drive, disk, 0))
385a4b87 1301 tape->door_locked = DOOR_UNLOCKED;
5a04cfa9 1302 }
ec0fdb01 1303 ide_tape_discard_merge_buffer(drive, 0);
0c8a6c7a 1304 retval = ide_do_start_stop(drive, disk, !IDETAPE_LU_LOAD_MASK);
5a04cfa9 1305 if (!retval)
49d8078a
BP
1306 clear_bit(ilog2(IDE_AFLAG_MEDIUM_PRESENT),
1307 &drive->atapi_flags);
5a04cfa9
BP
1308 return retval;
1309 case MTNOP:
ec0fdb01 1310 ide_tape_discard_merge_buffer(drive, 0);
5a04cfa9
BP
1311 return idetape_flush_tape_buffers(drive);
1312 case MTRETEN:
ec0fdb01 1313 ide_tape_discard_merge_buffer(drive, 0);
0c8a6c7a 1314 return ide_do_start_stop(drive, disk,
5a04cfa9 1315 IDETAPE_LU_RETENSION_MASK | IDETAPE_LU_LOAD_MASK);
5a04cfa9
BP
1316 case MTEOM:
1317 idetape_create_space_cmd(&pc, 0, IDETAPE_SPACE_TO_EOD);
b13345f3 1318 return ide_queue_pc_tail(drive, disk, &pc, NULL, 0);
5a04cfa9
BP
1319 case MTERASE:
1320 (void)idetape_rewind_tape(drive);
1321 idetape_create_erase_cmd(&pc);
b13345f3 1322 return ide_queue_pc_tail(drive, disk, &pc, NULL, 0);
5a04cfa9
BP
1323 case MTSETBLK:
1324 if (mt_count) {
1325 if (mt_count < tape->blk_size ||
1326 mt_count % tape->blk_size)
1da177e4 1327 return -EIO;
5a04cfa9 1328 tape->user_bs_factor = mt_count / tape->blk_size;
49d8078a
BP
1329 clear_bit(ilog2(IDE_AFLAG_DETECT_BS),
1330 &drive->atapi_flags);
5a04cfa9 1331 } else
49d8078a
BP
1332 set_bit(ilog2(IDE_AFLAG_DETECT_BS),
1333 &drive->atapi_flags);
5a04cfa9
BP
1334 return 0;
1335 case MTSEEK:
ec0fdb01 1336 ide_tape_discard_merge_buffer(drive, 0);
5a04cfa9
BP
1337 return idetape_position_tape(drive,
1338 mt_count * tape->user_bs_factor, tape->partition, 0);
1339 case MTSETPART:
ec0fdb01 1340 ide_tape_discard_merge_buffer(drive, 0);
5a04cfa9
BP
1341 return idetape_position_tape(drive, 0, mt_count, 0);
1342 case MTFSR:
1343 case MTBSR:
1344 case MTLOCK:
0578042d 1345 retval = ide_set_media_lock(drive, disk, 1);
5a04cfa9 1346 if (retval)
1da177e4 1347 return retval;
5a04cfa9
BP
1348 tape->door_locked = DOOR_EXPLICITLY_LOCKED;
1349 return 0;
1350 case MTUNLOCK:
0578042d 1351 retval = ide_set_media_lock(drive, disk, 0);
5a04cfa9
BP
1352 if (retval)
1353 return retval;
1354 tape->door_locked = DOOR_UNLOCKED;
1355 return 0;
1356 default:
1357 printk(KERN_ERR "ide-tape: MTIO operation %d not supported\n",
1358 mt_op);
1359 return -EIO;
1da177e4
LT
1360 }
1361}
1362
1363/*
d99c9da2
BP
1364 * Our character device ioctls. General mtio.h magnetic io commands are
1365 * supported here, and not in the corresponding block interface. Our own
1366 * ide-tape ioctls are supported on both interfaces.
1da177e4 1367 */
d99c9da2
BP
1368static int idetape_chrdev_ioctl(struct inode *inode, struct file *file,
1369 unsigned int cmd, unsigned long arg)
1da177e4 1370{
5aeddf90 1371 struct ide_tape_obj *tape = file->private_data;
1da177e4
LT
1372 ide_drive_t *drive = tape->drive;
1373 struct mtop mtop;
1374 struct mtget mtget;
1375 struct mtpos mtpos;
54bb2074 1376 int block_offset = 0, position = tape->first_frame;
1da177e4
LT
1377 void __user *argp = (void __user *)arg;
1378
e972d702 1379 ide_debug_log(IDE_DBG_FUNC, "cmd: 0x%x", cmd);
1da177e4 1380
54abf37e 1381 if (tape->chrdev_dir == IDETAPE_DIR_WRITE) {
d9df937a 1382 ide_tape_flush_merge_buffer(drive);
1da177e4
LT
1383 idetape_flush_tape_buffers(drive);
1384 }
1385 if (cmd == MTIOCGET || cmd == MTIOCPOS) {
963da55c 1386 block_offset = tape->valid /
54bb2074 1387 (tape->blk_size * tape->user_bs_factor);
55ce3a12 1388 position = ide_tape_read_position(drive);
5a04cfa9 1389 if (position < 0)
1da177e4
LT
1390 return -EIO;
1391 }
1392 switch (cmd) {
5a04cfa9
BP
1393 case MTIOCTOP:
1394 if (copy_from_user(&mtop, argp, sizeof(struct mtop)))
1395 return -EFAULT;
1396 return idetape_mtioctop(drive, mtop.mt_op, mtop.mt_count);
1397 case MTIOCGET:
1398 memset(&mtget, 0, sizeof(struct mtget));
1399 mtget.mt_type = MT_ISSCSI2;
1400 mtget.mt_blkno = position / tape->user_bs_factor - block_offset;
1401 mtget.mt_dsreg =
1402 ((tape->blk_size * tape->user_bs_factor)
1403 << MT_ST_BLKSIZE_SHIFT) & MT_ST_BLKSIZE_MASK;
1404
1405 if (tape->drv_write_prot)
1406 mtget.mt_gstat |= GMT_WR_PROT(0xffffffff);
1407
1408 if (copy_to_user(argp, &mtget, sizeof(struct mtget)))
1409 return -EFAULT;
1410 return 0;
1411 case MTIOCPOS:
1412 mtpos.mt_blkno = position / tape->user_bs_factor - block_offset;
1413 if (copy_to_user(argp, &mtpos, sizeof(struct mtpos)))
1414 return -EFAULT;
1415 return 0;
1416 default:
1417 if (tape->chrdev_dir == IDETAPE_DIR_READ)
ec0fdb01 1418 ide_tape_discard_merge_buffer(drive, 1);
5a04cfa9 1419 return idetape_blkdev_ioctl(drive, cmd, arg);
1da177e4
LT
1420 }
1421}
1422
3cffb9ce
BP
1423/*
1424 * Do a mode sense page 0 with block descriptor and if it succeeds set the tape
1425 * block size with the reported value.
1426 */
1427static void ide_tape_get_bsize_from_bdesc(ide_drive_t *drive)
1428{
1429 idetape_tape_t *tape = drive->driver_data;
d236d74c 1430 struct ide_atapi_pc pc;
837272b4 1431 u8 buf[12];
3cffb9ce
BP
1432
1433 idetape_create_mode_sense_cmd(&pc, IDETAPE_BLOCK_DESCRIPTOR);
837272b4 1434 if (ide_queue_pc_tail(drive, tape->disk, &pc, buf, pc.req_xfer)) {
3cffb9ce 1435 printk(KERN_ERR "ide-tape: Can't get block descriptor\n");
54bb2074 1436 if (tape->blk_size == 0) {
3cffb9ce
BP
1437 printk(KERN_WARNING "ide-tape: Cannot deal with zero "
1438 "block size, assuming 32k\n");
54bb2074 1439 tape->blk_size = 32768;
3cffb9ce
BP
1440 }
1441 return;
1442 }
837272b4
BP
1443 tape->blk_size = (buf[4 + 5] << 16) +
1444 (buf[4 + 6] << 8) +
1445 buf[4 + 7];
1446 tape->drv_write_prot = (buf[2] & 0x80) >> 7;
e972d702
BP
1447
1448 ide_debug_log(IDE_DBG_FUNC, "blk_size: %d, write_prot: %d",
1449 tape->blk_size, tape->drv_write_prot);
3cffb9ce 1450}
1da177e4 1451
5a04cfa9 1452static int idetape_chrdev_open(struct inode *inode, struct file *filp)
1da177e4
LT
1453{
1454 unsigned int minor = iminor(inode), i = minor & ~0xc0;
1455 ide_drive_t *drive;
1456 idetape_tape_t *tape;
1da177e4
LT
1457 int retval;
1458
8004a8c9
BP
1459 if (i >= MAX_HWIFS * MAX_DRIVES)
1460 return -ENXIO;
1461
cbba2fa7
BP
1462 mutex_lock(&idetape_chrdev_mutex);
1463
9d01e4cd 1464 tape = ide_tape_get(NULL, true, i);
04f4ac9d 1465 if (!tape) {
cbba2fa7 1466 mutex_unlock(&idetape_chrdev_mutex);
8004a8c9 1467 return -ENXIO;
04f4ac9d 1468 }
8004a8c9 1469
e972d702
BP
1470 drive = tape->drive;
1471 filp->private_data = tape;
1472
1473 ide_debug_log(IDE_DBG_FUNC, "enter");
8004a8c9 1474
1da177e4
LT
1475 /*
1476 * We really want to do nonseekable_open(inode, filp); here, but some
1477 * versions of tar incorrectly call lseek on tapes and bail out if that
1478 * fails. So we disallow pread() and pwrite(), but permit lseeks.
1479 */
1480 filp->f_mode &= ~(FMODE_PREAD | FMODE_PWRITE);
1481
1da177e4 1482
49d8078a 1483 if (test_and_set_bit(ilog2(IDE_AFLAG_BUSY), &drive->atapi_flags)) {
1da177e4
LT
1484 retval = -EBUSY;
1485 goto out_put_tape;
1486 }
1487
1488 retval = idetape_wait_ready(drive, 60 * HZ);
1489 if (retval) {
49d8078a 1490 clear_bit(ilog2(IDE_AFLAG_BUSY), &drive->atapi_flags);
1da177e4
LT
1491 printk(KERN_ERR "ide-tape: %s: drive not ready\n", tape->name);
1492 goto out_put_tape;
1493 }
1494
79ca743f 1495 ide_tape_read_position(drive);
49d8078a 1496 if (!test_bit(ilog2(IDE_AFLAG_ADDRESS_VALID), &drive->atapi_flags))
1da177e4
LT
1497 (void)idetape_rewind_tape(drive);
1498
1da177e4 1499 /* Read block size and write protect status from drive. */
3cffb9ce 1500 ide_tape_get_bsize_from_bdesc(drive);
1da177e4
LT
1501
1502 /* Set write protect flag if device is opened as read-only. */
1503 if ((filp->f_flags & O_ACCMODE) == O_RDONLY)
1504 tape->write_prot = 1;
1505 else
1506 tape->write_prot = tape->drv_write_prot;
1507
1508 /* Make sure drive isn't write protected if user wants to write. */
1509 if (tape->write_prot) {
1510 if ((filp->f_flags & O_ACCMODE) == O_WRONLY ||
1511 (filp->f_flags & O_ACCMODE) == O_RDWR) {
49d8078a 1512 clear_bit(ilog2(IDE_AFLAG_BUSY), &drive->atapi_flags);
1da177e4
LT
1513 retval = -EROFS;
1514 goto out_put_tape;
1515 }
1516 }
1517
3c98bf34 1518 /* Lock the tape drive door so user can't eject. */
54abf37e 1519 if (tape->chrdev_dir == IDETAPE_DIR_NONE) {
0578042d 1520 if (!ide_set_media_lock(drive, tape->disk, 1)) {
385a4b87
BZ
1521 if (tape->door_locked != DOOR_EXPLICITLY_LOCKED)
1522 tape->door_locked = DOOR_LOCKED;
1da177e4
LT
1523 }
1524 }
cbba2fa7
BP
1525 mutex_unlock(&idetape_chrdev_mutex);
1526
1da177e4
LT
1527 return 0;
1528
1529out_put_tape:
1530 ide_tape_put(tape);
cbba2fa7
BP
1531
1532 mutex_unlock(&idetape_chrdev_mutex);
1533
1da177e4
LT
1534 return retval;
1535}
1536
5a04cfa9 1537static void idetape_write_release(ide_drive_t *drive, unsigned int minor)
1da177e4
LT
1538{
1539 idetape_tape_t *tape = drive->driver_data;
1540
d9df937a 1541 ide_tape_flush_merge_buffer(drive);
963da55c
TH
1542 tape->buf = kmalloc(tape->buffer_size, GFP_KERNEL);
1543 if (tape->buf != NULL) {
54bb2074
BP
1544 idetape_pad_zeros(drive, tape->blk_size *
1545 (tape->user_bs_factor - 1));
963da55c
TH
1546 kfree(tape->buf);
1547 tape->buf = NULL;
1da177e4
LT
1548 }
1549 idetape_write_filemark(drive);
1550 idetape_flush_tape_buffers(drive);
1551 idetape_flush_tape_buffers(drive);
1552}
1553
5a04cfa9 1554static int idetape_chrdev_release(struct inode *inode, struct file *filp)
1da177e4 1555{
5aeddf90 1556 struct ide_tape_obj *tape = filp->private_data;
1da177e4 1557 ide_drive_t *drive = tape->drive;
1da177e4
LT
1558 unsigned int minor = iminor(inode);
1559
cbba2fa7
BP
1560 mutex_lock(&idetape_chrdev_mutex);
1561
1da177e4 1562 tape = drive->driver_data;
8004a8c9 1563
e972d702 1564 ide_debug_log(IDE_DBG_FUNC, "enter");
1da177e4 1565
54abf37e 1566 if (tape->chrdev_dir == IDETAPE_DIR_WRITE)
1da177e4 1567 idetape_write_release(drive, minor);
54abf37e 1568 if (tape->chrdev_dir == IDETAPE_DIR_READ) {
1da177e4 1569 if (minor < 128)
ec0fdb01 1570 ide_tape_discard_merge_buffer(drive, 1);
1da177e4 1571 }
f64eee7b 1572
49d8078a
BP
1573 if (minor < 128 && test_bit(ilog2(IDE_AFLAG_MEDIUM_PRESENT),
1574 &drive->atapi_flags))
1da177e4 1575 (void) idetape_rewind_tape(drive);
49d8078a 1576
54abf37e 1577 if (tape->chrdev_dir == IDETAPE_DIR_NONE) {
1da177e4 1578 if (tape->door_locked == DOOR_LOCKED) {
0578042d 1579 if (!ide_set_media_lock(drive, tape->disk, 0))
385a4b87 1580 tape->door_locked = DOOR_UNLOCKED;
1da177e4
LT
1581 }
1582 }
49d8078a 1583 clear_bit(ilog2(IDE_AFLAG_BUSY), &drive->atapi_flags);
1da177e4 1584 ide_tape_put(tape);
cbba2fa7
BP
1585
1586 mutex_unlock(&idetape_chrdev_mutex);
1587
1da177e4
LT
1588 return 0;
1589}
1590
6d29c8f0 1591static void idetape_get_inquiry_results(ide_drive_t *drive)
1da177e4 1592{
1da177e4 1593 idetape_tape_t *tape = drive->driver_data;
d236d74c 1594 struct ide_atapi_pc pc;
41fa9f86 1595 u8 pc_buf[256];
801bd32e 1596 char fw_rev[4], vendor_id[8], product_id[16];
6d29c8f0 1597
1da177e4 1598 idetape_create_inquiry_cmd(&pc);
b13345f3 1599 if (ide_queue_pc_tail(drive, tape->disk, &pc, pc_buf, pc.req_xfer)) {
6d29c8f0
BP
1600 printk(KERN_ERR "ide-tape: %s: can't get INQUIRY results\n",
1601 tape->name);
1da177e4
LT
1602 return;
1603 }
b13345f3
BP
1604 memcpy(vendor_id, &pc_buf[8], 8);
1605 memcpy(product_id, &pc_buf[16], 16);
1606 memcpy(fw_rev, &pc_buf[32], 4);
41f81d54 1607
801bd32e
BP
1608 ide_fixstring(vendor_id, 8, 0);
1609 ide_fixstring(product_id, 16, 0);
1610 ide_fixstring(fw_rev, 4, 0);
41f81d54 1611
801bd32e 1612 printk(KERN_INFO "ide-tape: %s <-> %s: %.8s %.16s rev %.4s\n",
41f81d54 1613 drive->name, tape->name, vendor_id, product_id, fw_rev);
1da177e4
LT
1614}
1615
1616/*
b6422013
BP
1617 * Ask the tape about its various parameters. In particular, we will adjust our
1618 * data transfer buffer size to the recommended value as returned by the tape.
1da177e4 1619 */
5a04cfa9 1620static void idetape_get_mode_sense_results(ide_drive_t *drive)
1da177e4
LT
1621{
1622 idetape_tape_t *tape = drive->driver_data;
d236d74c 1623 struct ide_atapi_pc pc;
b13345f3 1624 u8 buf[24], *caps;
b6422013 1625 u8 speed, max_speed;
47314fa4 1626
1da177e4 1627 idetape_create_mode_sense_cmd(&pc, IDETAPE_CAPABILITIES_PAGE);
b13345f3 1628 if (ide_queue_pc_tail(drive, tape->disk, &pc, buf, pc.req_xfer)) {
b6422013
BP
1629 printk(KERN_ERR "ide-tape: Can't get tape parameters - assuming"
1630 " some default values\n");
54bb2074 1631 tape->blk_size = 512;
b6422013
BP
1632 put_unaligned(52, (u16 *)&tape->caps[12]);
1633 put_unaligned(540, (u16 *)&tape->caps[14]);
1634 put_unaligned(6*52, (u16 *)&tape->caps[16]);
1da177e4
LT
1635 return;
1636 }
b13345f3 1637 caps = buf + 4 + buf[3];
b6422013
BP
1638
1639 /* convert to host order and save for later use */
cd740ab0
HH
1640 speed = be16_to_cpup((__be16 *)&caps[14]);
1641 max_speed = be16_to_cpup((__be16 *)&caps[8]);
1da177e4 1642
cd740ab0
HH
1643 *(u16 *)&caps[8] = max_speed;
1644 *(u16 *)&caps[12] = be16_to_cpup((__be16 *)&caps[12]);
1645 *(u16 *)&caps[14] = speed;
1646 *(u16 *)&caps[16] = be16_to_cpup((__be16 *)&caps[16]);
1da177e4 1647
b6422013
BP
1648 if (!speed) {
1649 printk(KERN_INFO "ide-tape: %s: invalid tape speed "
1650 "(assuming 650KB/sec)\n", drive->name);
cd740ab0 1651 *(u16 *)&caps[14] = 650;
1da177e4 1652 }
b6422013
BP
1653 if (!max_speed) {
1654 printk(KERN_INFO "ide-tape: %s: invalid max_speed "
1655 "(assuming 650KB/sec)\n", drive->name);
cd740ab0 1656 *(u16 *)&caps[8] = 650;
1da177e4
LT
1657 }
1658
b6422013 1659 memcpy(&tape->caps, caps, 20);
0578042d
BZ
1660
1661 /* device lacks locking support according to capabilities page */
1662 if ((caps[6] & 1) == 0)
42619d35 1663 drive->dev_flags &= ~IDE_DFLAG_DOORLOCKING;
0578042d 1664
b6422013 1665 if (caps[7] & 0x02)
54bb2074 1666 tape->blk_size = 512;
b6422013 1667 else if (caps[7] & 0x04)
54bb2074 1668 tape->blk_size = 1024;
1da177e4
LT
1669}
1670
7662d046 1671#ifdef CONFIG_IDE_PROC_FS
8185d5aa
BZ
1672#define ide_tape_devset_get(name, field) \
1673static int get_##name(ide_drive_t *drive) \
1674{ \
1675 idetape_tape_t *tape = drive->driver_data; \
1676 return tape->field; \
1677}
1678
1679#define ide_tape_devset_set(name, field) \
1680static int set_##name(ide_drive_t *drive, int arg) \
1681{ \
1682 idetape_tape_t *tape = drive->driver_data; \
1683 tape->field = arg; \
1684 return 0; \
1685}
1686
92f1f8fd 1687#define ide_tape_devset_rw_field(_name, _field) \
8185d5aa
BZ
1688ide_tape_devset_get(_name, _field) \
1689ide_tape_devset_set(_name, _field) \
92f1f8fd 1690IDE_DEVSET(_name, DS_SYNC, get_##_name, set_##_name)
8185d5aa 1691
92f1f8fd 1692#define ide_tape_devset_r_field(_name, _field) \
8185d5aa 1693ide_tape_devset_get(_name, _field) \
92f1f8fd 1694IDE_DEVSET(_name, 0, get_##_name, NULL)
8185d5aa
BZ
1695
1696static int mulf_tdsc(ide_drive_t *drive) { return 1000; }
1697static int divf_tdsc(ide_drive_t *drive) { return HZ; }
1698static int divf_buffer(ide_drive_t *drive) { return 2; }
1699static int divf_buffer_size(ide_drive_t *drive) { return 1024; }
1700
97100fc8 1701ide_devset_rw_flag(dsc_overlap, IDE_DFLAG_DSC_OVERLAP);
92f1f8fd 1702
92f1f8fd
EO
1703ide_tape_devset_rw_field(tdsc, best_dsc_rw_freq);
1704
1705ide_tape_devset_r_field(avg_speed, avg_speed);
1706ide_tape_devset_r_field(speed, caps[14]);
1707ide_tape_devset_r_field(buffer, caps[16]);
1708ide_tape_devset_r_field(buffer_size, buffer_size);
1709
1710static const struct ide_proc_devset idetape_settings[] = {
1711 __IDE_PROC_DEVSET(avg_speed, 0, 0xffff, NULL, NULL),
1712 __IDE_PROC_DEVSET(buffer, 0, 0xffff, NULL, divf_buffer),
1713 __IDE_PROC_DEVSET(buffer_size, 0, 0xffff, NULL, divf_buffer_size),
92f1f8fd
EO
1714 __IDE_PROC_DEVSET(dsc_overlap, 0, 1, NULL, NULL),
1715 __IDE_PROC_DEVSET(speed, 0, 0xffff, NULL, NULL),
1716 __IDE_PROC_DEVSET(tdsc, IDETAPE_DSC_RW_MIN, IDETAPE_DSC_RW_MAX,
1717 mulf_tdsc, divf_tdsc),
71bfc7a7 1718 { NULL },
8185d5aa 1719};
7662d046 1720#endif
1da177e4
LT
1721
1722/*
3c98bf34 1723 * The function below is called to:
1da177e4 1724 *
3c98bf34
BP
1725 * 1. Initialize our various state variables.
1726 * 2. Ask the tape for its capabilities.
1727 * 3. Allocate a buffer which will be used for data transfer. The buffer size
1728 * is chosen based on the recommendation which we received in step 2.
1da177e4 1729 *
3c98bf34
BP
1730 * Note that at this point ide.c already assigned us an irq, so that we can
1731 * queue requests here and wait for their completion.
1da177e4 1732 */
5a04cfa9 1733static void idetape_setup(ide_drive_t *drive, idetape_tape_t *tape, int minor)
1da177e4 1734{
83042b24 1735 unsigned long t;
1da177e4 1736 int speed;
f73850a3 1737 int buffer_size;
b6422013 1738 u16 *ctl = (u16 *)&tape->caps[12];
1da177e4 1739
e972d702
BP
1740 ide_debug_log(IDE_DBG_FUNC, "minor: %d", minor);
1741
1742 drive->pc_callback = ide_tape_callback;
776bb027 1743
97100fc8
BZ
1744 drive->dev_flags |= IDE_DFLAG_DSC_OVERLAP;
1745
4166c199
BZ
1746 if (drive->hwif->host_flags & IDE_HFLAG_NO_DSC) {
1747 printk(KERN_INFO "ide-tape: %s: disabling DSC overlap\n",
1748 tape->name);
97100fc8 1749 drive->dev_flags &= ~IDE_DFLAG_DSC_OVERLAP;
1da177e4 1750 }
97100fc8 1751
1da177e4 1752 /* Seagate Travan drives do not support DSC overlap. */
4dde4492 1753 if (strstr((char *)&drive->id[ATA_ID_PROD], "Seagate STT3401"))
97100fc8
BZ
1754 drive->dev_flags &= ~IDE_DFLAG_DSC_OVERLAP;
1755
1da177e4
LT
1756 tape->minor = minor;
1757 tape->name[0] = 'h';
1758 tape->name[1] = 't';
1759 tape->name[2] = '0' + minor;
54abf37e 1760 tape->chrdev_dir = IDETAPE_DIR_NONE;
4dde4492 1761
1da177e4
LT
1762 idetape_get_inquiry_results(drive);
1763 idetape_get_mode_sense_results(drive);
3cffb9ce 1764 ide_tape_get_bsize_from_bdesc(drive);
1da177e4 1765 tape->user_bs_factor = 1;
f73850a3
BP
1766 tape->buffer_size = *ctl * tape->blk_size;
1767 while (tape->buffer_size > 0xffff) {
1da177e4 1768 printk(KERN_NOTICE "ide-tape: decreasing stage size\n");
b6422013 1769 *ctl /= 2;
f73850a3 1770 tape->buffer_size = *ctl * tape->blk_size;
1da177e4 1771 }
f73850a3 1772 buffer_size = tape->buffer_size;
1da177e4 1773
83042b24 1774 /* select the "best" DSC read/write polling freq */
b6422013 1775 speed = max(*(u16 *)&tape->caps[14], *(u16 *)&tape->caps[8]);
1da177e4 1776
f73850a3 1777 t = (IDETAPE_FIFO_THRESHOLD * tape->buffer_size * HZ) / (speed * 1000);
1da177e4
LT
1778
1779 /*
3c98bf34
BP
1780 * Ensure that the number we got makes sense; limit it within
1781 * IDETAPE_DSC_RW_MIN and IDETAPE_DSC_RW_MAX.
1da177e4 1782 */
a792bd5a
HH
1783 tape->best_dsc_rw_freq = clamp_t(unsigned long, t, IDETAPE_DSC_RW_MIN,
1784 IDETAPE_DSC_RW_MAX);
1da177e4 1785 printk(KERN_INFO "ide-tape: %s <-> %s: %dKBps, %d*%dkB buffer, "
83042b24 1786 "%lums tDSC%s\n",
b6422013 1787 drive->name, tape->name, *(u16 *)&tape->caps[14],
f73850a3
BP
1788 (*(u16 *)&tape->caps[16] * 512) / tape->buffer_size,
1789 tape->buffer_size / 1024,
54bb2074 1790 tape->best_dsc_rw_freq * 1000 / HZ,
97100fc8 1791 (drive->dev_flags & IDE_DFLAG_USING_DMA) ? ", DMA" : "");
1da177e4 1792
1e874f44 1793 ide_proc_register_driver(drive, tape->driver);
1da177e4
LT
1794}
1795
4031bbe4 1796static void ide_tape_remove(ide_drive_t *drive)
1da177e4
LT
1797{
1798 idetape_tape_t *tape = drive->driver_data;
1da177e4 1799
7662d046 1800 ide_proc_unregister_driver(drive, tape->driver);
8fed4368 1801 device_del(&tape->dev);
1da177e4
LT
1802 ide_unregister_region(tape->disk);
1803
8fed4368
BZ
1804 mutex_lock(&idetape_ref_mutex);
1805 put_device(&tape->dev);
1806 mutex_unlock(&idetape_ref_mutex);
1da177e4
LT
1807}
1808
8fed4368 1809static void ide_tape_release(struct device *dev)
1da177e4 1810{
8fed4368 1811 struct ide_tape_obj *tape = to_ide_drv(dev, ide_tape_obj);
1da177e4
LT
1812 ide_drive_t *drive = tape->drive;
1813 struct gendisk *g = tape->disk;
1814
963da55c 1815 BUG_ON(tape->valid);
8604affd 1816
97100fc8 1817 drive->dev_flags &= ~IDE_DFLAG_DSC_OVERLAP;
1da177e4 1818 drive->driver_data = NULL;
dbc1272e 1819 device_destroy(idetape_sysfs_class, MKDEV(IDETAPE_MAJOR, tape->minor));
5a04cfa9
BP
1820 device_destroy(idetape_sysfs_class,
1821 MKDEV(IDETAPE_MAJOR, tape->minor + 128));
1da177e4
LT
1822 idetape_devs[tape->minor] = NULL;
1823 g->private_data = NULL;
1824 put_disk(g);
1825 kfree(tape);
1826}
1827
ecfd80e4 1828#ifdef CONFIG_IDE_PROC_FS
6d703a81 1829static int idetape_name_proc_show(struct seq_file *m, void *v)
1da177e4 1830{
6d703a81 1831 ide_drive_t *drive = (ide_drive_t *) m->private;
1da177e4 1832 idetape_tape_t *tape = drive->driver_data;
1da177e4 1833
6d703a81
AD
1834 seq_printf(m, "%s\n", tape->name);
1835 return 0;
1836}
1837
1838static int idetape_name_proc_open(struct inode *inode, struct file *file)
1839{
1840 return single_open(file, idetape_name_proc_show, PDE(inode)->data);
1da177e4
LT
1841}
1842
6d703a81
AD
1843static const struct file_operations idetape_name_proc_fops = {
1844 .owner = THIS_MODULE,
1845 .open = idetape_name_proc_open,
1846 .read = seq_read,
1847 .llseek = seq_lseek,
1848 .release = single_release,
1849};
1850
1da177e4 1851static ide_proc_entry_t idetape_proc[] = {
6d703a81
AD
1852 { "capacity", S_IFREG|S_IRUGO, &ide_capacity_proc_fops },
1853 { "name", S_IFREG|S_IRUGO, &idetape_name_proc_fops },
1854 {}
1da177e4 1855};
79cb3803
BZ
1856
1857static ide_proc_entry_t *ide_tape_proc_entries(ide_drive_t *drive)
1858{
1859 return idetape_proc;
1860}
1861
1862static const struct ide_proc_devset *ide_tape_proc_devsets(ide_drive_t *drive)
1863{
1864 return idetape_settings;
1865}
1da177e4
LT
1866#endif
1867
4031bbe4 1868static int ide_tape_probe(ide_drive_t *);
1da177e4 1869
7f3c868b 1870static struct ide_driver idetape_driver = {
8604affd 1871 .gen_driver = {
4ef3b8f4 1872 .owner = THIS_MODULE,
8604affd
BZ
1873 .name = "ide-tape",
1874 .bus = &ide_bus_type,
8604affd 1875 },
4031bbe4
RK
1876 .probe = ide_tape_probe,
1877 .remove = ide_tape_remove,
1da177e4 1878 .version = IDETAPE_VERSION,
1da177e4 1879 .do_request = idetape_do_request,
7662d046 1880#ifdef CONFIG_IDE_PROC_FS
79cb3803
BZ
1881 .proc_entries = ide_tape_proc_entries,
1882 .proc_devsets = ide_tape_proc_devsets,
7662d046 1883#endif
1da177e4
LT
1884};
1885
3c98bf34 1886/* Our character device supporting functions, passed to register_chrdev. */
2b8693c0 1887static const struct file_operations idetape_fops = {
1da177e4
LT
1888 .owner = THIS_MODULE,
1889 .read = idetape_chrdev_read,
1890 .write = idetape_chrdev_write,
1891 .ioctl = idetape_chrdev_ioctl,
1892 .open = idetape_chrdev_open,
1893 .release = idetape_chrdev_release,
1894};
1895
a4600f81 1896static int idetape_open(struct block_device *bdev, fmode_t mode)
1da177e4 1897{
9d01e4cd 1898 struct ide_tape_obj *tape = ide_tape_get(bdev->bd_disk, false, 0);
1da177e4 1899
5a04cfa9 1900 if (!tape)
1da177e4
LT
1901 return -ENXIO;
1902
1da177e4
LT
1903 return 0;
1904}
1905
a4600f81 1906static int idetape_release(struct gendisk *disk, fmode_t mode)
1da177e4 1907{
5aeddf90 1908 struct ide_tape_obj *tape = ide_drv_g(disk, ide_tape_obj);
1da177e4
LT
1909
1910 ide_tape_put(tape);
1da177e4
LT
1911 return 0;
1912}
1913
a4600f81 1914static int idetape_ioctl(struct block_device *bdev, fmode_t mode,
1da177e4
LT
1915 unsigned int cmd, unsigned long arg)
1916{
5aeddf90 1917 struct ide_tape_obj *tape = ide_drv_g(bdev->bd_disk, ide_tape_obj);
1da177e4 1918 ide_drive_t *drive = tape->drive;
1bddd9e6 1919 int err = generic_ide_ioctl(drive, bdev, cmd, arg);
1da177e4
LT
1920 if (err == -EINVAL)
1921 err = idetape_blkdev_ioctl(drive, cmd, arg);
1922 return err;
1923}
1924
83d5cde4 1925static const struct block_device_operations idetape_block_ops = {
1da177e4 1926 .owner = THIS_MODULE,
a4600f81
AV
1927 .open = idetape_open,
1928 .release = idetape_release,
1929 .locked_ioctl = idetape_ioctl,
1da177e4
LT
1930};
1931
4031bbe4 1932static int ide_tape_probe(ide_drive_t *drive)
1da177e4
LT
1933{
1934 idetape_tape_t *tape;
1935 struct gendisk *g;
1936 int minor;
1937
e972d702
BP
1938 ide_debug_log(IDE_DBG_FUNC, "enter");
1939
1940 if (!strstr(DRV_NAME, drive->driver_req))
1da177e4 1941 goto failed;
2a924662 1942
1da177e4
LT
1943 if (drive->media != ide_tape)
1944 goto failed;
2a924662 1945
97100fc8
BZ
1946 if ((drive->dev_flags & IDE_DFLAG_ID_READ) &&
1947 ide_check_atapi_device(drive, DRV_NAME) == 0) {
5a04cfa9
BP
1948 printk(KERN_ERR "ide-tape: %s: not supported by this version of"
1949 " the driver\n", drive->name);
1da177e4
LT
1950 goto failed;
1951 }
5a04cfa9 1952 tape = kzalloc(sizeof(idetape_tape_t), GFP_KERNEL);
1da177e4 1953 if (tape == NULL) {
5a04cfa9
BP
1954 printk(KERN_ERR "ide-tape: %s: Can't allocate a tape struct\n",
1955 drive->name);
1da177e4
LT
1956 goto failed;
1957 }
1958
1959 g = alloc_disk(1 << PARTN_BITS);
1960 if (!g)
1961 goto out_free_tape;
1962
1963 ide_init_disk(g, drive);
1964
8fed4368
BZ
1965 tape->dev.parent = &drive->gendev;
1966 tape->dev.release = ide_tape_release;
1967 dev_set_name(&tape->dev, dev_name(&drive->gendev));
1968
1969 if (device_register(&tape->dev))
1970 goto out_free_disk;
1da177e4
LT
1971
1972 tape->drive = drive;
1973 tape->driver = &idetape_driver;
1974 tape->disk = g;
1975
1976 g->private_data = &tape->driver;
1977
1978 drive->driver_data = tape;
1979
cf8b8975 1980 mutex_lock(&idetape_ref_mutex);
1da177e4
LT
1981 for (minor = 0; idetape_devs[minor]; minor++)
1982 ;
1983 idetape_devs[minor] = tape;
cf8b8975 1984 mutex_unlock(&idetape_ref_mutex);
1da177e4
LT
1985
1986 idetape_setup(drive, tape, minor);
1987
3ee074bf
GKH
1988 device_create(idetape_sysfs_class, &drive->gendev,
1989 MKDEV(IDETAPE_MAJOR, minor), NULL, "%s", tape->name);
1990 device_create(idetape_sysfs_class, &drive->gendev,
1991 MKDEV(IDETAPE_MAJOR, minor + 128), NULL,
1992 "n%s", tape->name);
d5dee80a 1993
1da177e4
LT
1994 g->fops = &idetape_block_ops;
1995 ide_register_region(g);
1996
1997 return 0;
8604affd 1998
8fed4368
BZ
1999out_free_disk:
2000 put_disk(g);
1da177e4
LT
2001out_free_tape:
2002 kfree(tape);
2003failed:
8604affd 2004 return -ENODEV;
1da177e4
LT
2005}
2006
5a04cfa9 2007static void __exit idetape_exit(void)
1da177e4 2008{
8604affd 2009 driver_unregister(&idetape_driver.gen_driver);
d5dee80a 2010 class_destroy(idetape_sysfs_class);
1da177e4
LT
2011 unregister_chrdev(IDETAPE_MAJOR, "ht");
2012}
2013
17514e8a 2014static int __init idetape_init(void)
1da177e4 2015{
d5dee80a
WD
2016 int error = 1;
2017 idetape_sysfs_class = class_create(THIS_MODULE, "ide_tape");
2018 if (IS_ERR(idetape_sysfs_class)) {
2019 idetape_sysfs_class = NULL;
2020 printk(KERN_ERR "Unable to create sysfs class for ide tapes\n");
2021 error = -EBUSY;
2022 goto out;
2023 }
2024
1da177e4 2025 if (register_chrdev(IDETAPE_MAJOR, "ht", &idetape_fops)) {
5a04cfa9
BP
2026 printk(KERN_ERR "ide-tape: Failed to register chrdev"
2027 " interface\n");
d5dee80a
WD
2028 error = -EBUSY;
2029 goto out_free_class;
1da177e4 2030 }
d5dee80a
WD
2031
2032 error = driver_register(&idetape_driver.gen_driver);
2033 if (error)
2034 goto out_free_driver;
2035
2036 return 0;
2037
2038out_free_driver:
2039 driver_unregister(&idetape_driver.gen_driver);
2040out_free_class:
2041 class_destroy(idetape_sysfs_class);
2042out:
2043 return error;
1da177e4
LT
2044}
2045
263756ec 2046MODULE_ALIAS("ide:*m-tape*");
1da177e4
LT
2047module_init(idetape_init);
2048module_exit(idetape_exit);
2049MODULE_ALIAS_CHARDEV_MAJOR(IDETAPE_MAJOR);
9c145768
BP
2050MODULE_DESCRIPTION("ATAPI Streaming TAPE Driver");
2051MODULE_LICENSE("GPL");