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