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