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