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