2 * IDE ATAPI streaming tape driver.
4 * Copyright (C) 1995-1999 Gadi Oxman <gadio@netvision.net.il>
5 * Copyright (C) 2003-2005 Bartlomiej Zolnierkiewicz
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.
11 * It is hereby placed under the terms of the GNU general public license.
12 * (See linux/COPYING).
14 * For a historical changelog see
15 * Documentation/ide/ChangeLog.ide-tape.1995-2002
18 #define DRV_NAME "ide-tape"
20 #define IDETAPE_VERSION "1.20"
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>
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>
43 #include <asm/byteorder.h>
44 #include <linux/irq.h>
45 #include <linux/uaccess.h>
47 #include <asm/unaligned.h>
48 #include <linux/mtio.h>
51 /* output errors only */
53 /* output all sense key/asc */
55 /* info regarding all chrdev-related procedures */
56 DBG_CHRDEV = (1 << 2),
57 /* all remaining procedures */
59 /* buffer alloc info (pc_stack & rq_stack) */
60 DBG_PCRQ_STACK = (1 << 4),
63 /* define to see debug info */
64 #define IDETAPE_DEBUG_LOG 0
67 #define debug_log(lvl, fmt, args...) \
69 if (tape->debug_mask & lvl) \
70 printk(KERN_INFO "ide-tape: " fmt, ## args); \
73 #define debug_log(lvl, fmt, args...) do {} while (0)
76 /**************************** Tunable parameters *****************************/
78 * After each failed packet command we issue a request sense command and retry
79 * the packet command IDETAPE_MAX_PC_RETRIES times.
81 * Setting IDETAPE_MAX_PC_RETRIES to 0 will disable retries.
83 #define IDETAPE_MAX_PC_RETRIES 3
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)
89 #define IDETAPE_PC_BUFFER_SIZE 256
92 * In various places in the driver, we need to allocate storage
93 * for packet commands and requests, which will remain valid while
94 * we leave the driver to wait for an interrupt or a timeout event.
96 #define IDETAPE_PC_STACK (10 + IDETAPE_MAX_PC_RETRIES)
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).
103 #define IDETAPE_WAIT_CMD (900*HZ)
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.
111 #define IDETAPE_FIFO_THRESHOLD 2
114 * DSC polling parameters.
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:
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.
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).
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.
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 */
147 /*************************** End of tunable parameters ***********************/
149 /* tape directions */
151 IDETAPE_DIR_NONE = (1 << 0),
152 IDETAPE_DIR_READ = (1 << 1),
153 IDETAPE_DIR_WRITE = (1 << 2),
159 struct idetape_bh *b_reqnext;
163 /* Tape door status */
164 #define DOOR_UNLOCKED 0
165 #define DOOR_LOCKED 1
166 #define DOOR_EXPLICITLY_LOCKED 2
168 /* Some defines for the SPACE command */
169 #define IDETAPE_SPACE_OVER_FILEMARK 1
170 #define IDETAPE_SPACE_TO_EOD 3
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
178 * Special requests for our block device strategy routine.
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.
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),
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
196 /* Structures related to the SELECT SENSE / MODE SENSE packet commands. */
197 #define IDETAPE_BLOCK_DESCRIPTOR 0
198 #define IDETAPE_CAPABILITIES_PAGE 0x2a
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.
204 typedef struct ide_tape_obj {
206 ide_driver_t *driver;
207 struct gendisk *disk;
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.
218 * pc points to the current processed packet command.
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.
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 */
233 struct request rq_stack[IDETAPE_PC_STACK];
234 /* We implement a circular array */
238 * DSC polling variables.
240 * While polling for DSC we use postponed_rq to postpone the current
241 * request so that ide.c will be able to service pending requests on the
242 * other device. Note that at most we will have only one DSC (usually
243 * data transfer) request in the device request queue.
245 struct request *postponed_rq;
246 /* The time in which we started polling for DSC */
247 unsigned long dsc_polling_start;
248 /* Timer used to poll for dsc */
249 struct timer_list dsc_timer;
250 /* Read/Write dsc polling frequency */
251 unsigned long best_dsc_rw_freq;
252 unsigned long dsc_poll_freq;
253 unsigned long dsc_timeout;
255 /* Read position information */
258 unsigned int first_frame;
260 /* Last error information */
261 u8 sense_key, asc, ascq;
263 /* Character device operation */
267 /* Current character device data transfer direction */
270 /* tape block size, usually 512 or 1024 bytes */
271 unsigned short blk_size;
274 /* Copy of the tape's Capabilities and Mechanical Page */
278 * Active data transfer request parameters.
280 * At most, there is only one ide-tape originated data transfer request
281 * in the device request queue. This allows ide.c to easily service
282 * requests from the other device when we postpone our active request.
285 /* Data buffer size chosen based on the tape's recommendation */
288 struct idetape_bh *merge_bh;
289 /* size of the merge buffer */
291 /* pointer to current buffer head within the merge buffer */
292 struct idetape_bh *bh;
296 int pages_per_buffer;
297 /* Wasted space in each stage */
300 /* protects the ide-tape queue */
303 /* Measures average tape speed */
304 unsigned long avg_time;
308 /* the door is currently locked */
310 /* the tape hardware is write protected */
312 /* the tape is write protected (hardware or opened as read-only) */
318 static DEFINE_MUTEX(idetape_ref_mutex);
320 static struct class *idetape_sysfs_class;
322 #define to_ide_tape(obj) container_of(obj, struct ide_tape_obj, kref)
324 #define ide_tape_g(disk) \
325 container_of((disk)->private_data, struct ide_tape_obj, driver)
327 static void ide_tape_release(struct kref *);
329 static struct ide_tape_obj *ide_tape_get(struct gendisk *disk)
331 struct ide_tape_obj *tape = NULL;
333 mutex_lock(&idetape_ref_mutex);
334 tape = ide_tape_g(disk);
336 if (ide_device_get(tape->drive))
339 kref_get(&tape->kref);
341 mutex_unlock(&idetape_ref_mutex);
345 static void ide_tape_put(struct ide_tape_obj *tape)
347 ide_drive_t *drive = tape->drive;
349 mutex_lock(&idetape_ref_mutex);
350 kref_put(&tape->kref, ide_tape_release);
351 ide_device_put(drive);
352 mutex_unlock(&idetape_ref_mutex);
356 * The variables below are used for the character device interface. Additional
357 * state variables are defined in our ide_drive_t structure.
359 static struct ide_tape_obj *idetape_devs[MAX_HWIFS * MAX_DRIVES];
361 #define ide_tape_f(file) ((file)->private_data)
363 static struct ide_tape_obj *ide_tape_chrdev_get(unsigned int i)
365 struct ide_tape_obj *tape = NULL;
367 mutex_lock(&idetape_ref_mutex);
368 tape = idetape_devs[i];
370 kref_get(&tape->kref);
371 mutex_unlock(&idetape_ref_mutex);
375 static void idetape_input_buffers(ide_drive_t *drive, struct ide_atapi_pc *pc,
378 struct idetape_bh *bh = pc->bh;
383 printk(KERN_ERR "ide-tape: bh == NULL in "
384 "idetape_input_buffers\n");
385 ide_pad_transfer(drive, 0, bcount);
389 (unsigned int)(bh->b_size - atomic_read(&bh->b_count)),
391 drive->hwif->tp_ops->input_data(drive, NULL, bh->b_data +
392 atomic_read(&bh->b_count), count);
394 atomic_add(count, &bh->b_count);
395 if (atomic_read(&bh->b_count) == bh->b_size) {
398 atomic_set(&bh->b_count, 0);
404 static void idetape_output_buffers(ide_drive_t *drive, struct ide_atapi_pc *pc,
407 struct idetape_bh *bh = pc->bh;
412 printk(KERN_ERR "ide-tape: bh == NULL in %s\n",
416 count = min((unsigned int)pc->b_count, (unsigned int)bcount);
417 drive->hwif->tp_ops->output_data(drive, NULL, pc->b_data, count);
420 pc->b_count -= count;
425 pc->b_data = bh->b_data;
426 pc->b_count = atomic_read(&bh->b_count);
432 static void idetape_update_buffers(ide_drive_t *drive, struct ide_atapi_pc *pc)
434 struct idetape_bh *bh = pc->bh;
436 unsigned int bcount = pc->xferred;
438 if (pc->flags & PC_FLAG_WRITING)
442 printk(KERN_ERR "ide-tape: bh == NULL in %s\n",
446 count = min((unsigned int)bh->b_size, (unsigned int)bcount);
447 atomic_set(&bh->b_count, count);
448 if (atomic_read(&bh->b_count) == bh->b_size)
456 * idetape_next_pc_storage returns a pointer to a place in which we can
457 * safely store a packet command, even though we intend to leave the
458 * driver. A storage space for a maximum of IDETAPE_PC_STACK packet
459 * commands is allocated at initialization time.
461 static struct ide_atapi_pc *idetape_next_pc_storage(ide_drive_t *drive)
463 idetape_tape_t *tape = drive->driver_data;
465 debug_log(DBG_PCRQ_STACK, "pc_stack_index=%d\n", tape->pc_stack_index);
467 if (tape->pc_stack_index == IDETAPE_PC_STACK)
468 tape->pc_stack_index = 0;
469 return (&tape->pc_stack[tape->pc_stack_index++]);
473 * idetape_next_rq_storage is used along with idetape_next_pc_storage.
474 * Since we queue packet commands in the request queue, we need to
475 * allocate a request, along with the allocation of a packet command.
478 /**************************************************************
480 * This should get fixed to use kmalloc(.., GFP_ATOMIC) *
481 * followed later on by kfree(). -ml *
483 **************************************************************/
485 static struct request *idetape_next_rq_storage(ide_drive_t *drive)
487 idetape_tape_t *tape = drive->driver_data;
489 debug_log(DBG_PCRQ_STACK, "rq_stack_index=%d\n", tape->rq_stack_index);
491 if (tape->rq_stack_index == IDETAPE_PC_STACK)
492 tape->rq_stack_index = 0;
493 return (&tape->rq_stack[tape->rq_stack_index++]);
497 * called on each failed packet command retry to analyze the request sense. We
498 * currently do not utilize this information.
500 static void idetape_analyze_error(ide_drive_t *drive, u8 *sense)
502 idetape_tape_t *tape = drive->driver_data;
503 struct ide_atapi_pc *pc = tape->failed_pc;
505 tape->sense_key = sense[2] & 0xF;
506 tape->asc = sense[12];
507 tape->ascq = sense[13];
509 debug_log(DBG_ERR, "pc = %x, sense key = %x, asc = %x, ascq = %x\n",
510 pc->c[0], tape->sense_key, tape->asc, tape->ascq);
512 /* Correct pc->xferred by asking the tape. */
513 if (pc->flags & PC_FLAG_DMA_ERROR) {
514 pc->xferred = pc->req_xfer -
516 get_unaligned_be32(&sense[3]);
517 idetape_update_buffers(drive, pc);
521 * If error was the result of a zero-length read or write command,
522 * with sense key=5, asc=0x22, ascq=0, let it slide. Some drives
523 * (i.e. Seagate STT3401A Travan) don't support 0-length read/writes.
525 if ((pc->c[0] == READ_6 || pc->c[0] == WRITE_6)
527 && pc->c[4] == 0 && pc->c[3] == 0 && pc->c[2] == 0) {
528 if (tape->sense_key == 5) {
529 /* don't report an error, everything's ok */
531 /* don't retry read/write */
532 pc->flags |= PC_FLAG_ABORT;
535 if (pc->c[0] == READ_6 && (sense[2] & 0x80)) {
536 pc->error = IDETAPE_ERROR_FILEMARK;
537 pc->flags |= PC_FLAG_ABORT;
539 if (pc->c[0] == WRITE_6) {
540 if ((sense[2] & 0x40) || (tape->sense_key == 0xd
541 && tape->asc == 0x0 && tape->ascq == 0x2)) {
542 pc->error = IDETAPE_ERROR_EOD;
543 pc->flags |= PC_FLAG_ABORT;
546 if (pc->c[0] == READ_6 || pc->c[0] == WRITE_6) {
547 if (tape->sense_key == 8) {
548 pc->error = IDETAPE_ERROR_EOD;
549 pc->flags |= PC_FLAG_ABORT;
551 if (!(pc->flags & PC_FLAG_ABORT) &&
553 pc->retries = IDETAPE_MAX_PC_RETRIES + 1;
557 /* Free data buffers completely. */
558 static void ide_tape_kfree_buffer(idetape_tape_t *tape)
560 struct idetape_bh *prev_bh, *bh = tape->merge_bh;
563 u32 size = bh->b_size;
566 unsigned int order = fls(size >> PAGE_SHIFT)-1;
569 free_pages((unsigned long)bh->b_data, order);
572 bh->b_data += (1 << order) * PAGE_SIZE;
580 static int idetape_end_request(ide_drive_t *drive, int uptodate, int nr_sects)
582 struct request *rq = HWGROUP(drive)->rq;
583 idetape_tape_t *tape = drive->driver_data;
587 debug_log(DBG_PROCS, "Enter %s\n", __func__);
590 case 0: error = IDETAPE_ERROR_GENERAL; break;
591 case 1: error = 0; break;
592 default: error = uptodate;
596 tape->failed_pc = NULL;
598 if (!blk_special_request(rq)) {
599 ide_end_request(drive, uptodate, nr_sects);
603 spin_lock_irqsave(&tape->lock, flags);
605 ide_end_drive_cmd(drive, 0, 0);
607 spin_unlock_irqrestore(&tape->lock, flags);
611 static void ide_tape_callback(ide_drive_t *drive)
613 idetape_tape_t *tape = drive->driver_data;
614 struct ide_atapi_pc *pc = tape->pc;
615 int uptodate = pc->error ? 0 : 1;
617 debug_log(DBG_PROCS, "Enter %s\n", __func__);
619 if (tape->failed_pc == pc)
620 tape->failed_pc = NULL;
622 if (pc->c[0] == REQUEST_SENSE) {
624 idetape_analyze_error(drive, pc->buf);
626 printk(KERN_ERR "ide-tape: Error in REQUEST SENSE "
627 "itself - Aborting request!\n");
628 } else if (pc->c[0] == READ_6 || pc->c[0] == WRITE_6) {
629 struct request *rq = drive->hwif->hwgroup->rq;
630 int blocks = pc->xferred / tape->blk_size;
632 tape->avg_size += blocks * tape->blk_size;
634 if (time_after_eq(jiffies, tape->avg_time + HZ)) {
635 tape->avg_speed = tape->avg_size * HZ /
636 (jiffies - tape->avg_time) / 1024;
638 tape->avg_time = jiffies;
641 tape->first_frame += blocks;
642 rq->current_nr_sectors -= blocks;
645 uptodate = pc->error;
646 } else if (pc->c[0] == READ_POSITION && uptodate) {
647 u8 *readpos = tape->pc->buf;
649 debug_log(DBG_SENSE, "BOP - %s\n",
650 (readpos[0] & 0x80) ? "Yes" : "No");
651 debug_log(DBG_SENSE, "EOP - %s\n",
652 (readpos[0] & 0x40) ? "Yes" : "No");
654 if (readpos[0] & 0x4) {
655 printk(KERN_INFO "ide-tape: Block location is unknown"
657 clear_bit(IDE_AFLAG_ADDRESS_VALID, &drive->atapi_flags);
660 debug_log(DBG_SENSE, "Block Location - %u\n",
661 be32_to_cpup((__be32 *)&readpos[4]));
663 tape->partition = readpos[1];
664 tape->first_frame = be32_to_cpup((__be32 *)&readpos[4]);
665 set_bit(IDE_AFLAG_ADDRESS_VALID, &drive->atapi_flags);
669 idetape_end_request(drive, uptodate, 0);
672 static void idetape_init_pc(struct ide_atapi_pc *pc)
674 memset(pc->c, 0, 12);
678 pc->buf = pc->pc_buf;
679 pc->buf_size = IDETAPE_PC_BUFFER_SIZE;
684 static void idetape_create_request_sense_cmd(struct ide_atapi_pc *pc)
687 pc->c[0] = REQUEST_SENSE;
693 * Generate a new packet command request in front of the request queue, before
694 * the current request, so that it will be processed immediately, on the next
695 * pass through the driver. The function below is called from the request
696 * handling part of the driver (the "bottom" part). Safe storage for the request
697 * should be allocated with ide_tape_next_{pc,rq}_storage() prior to that.
699 * Memory for those requests is pre-allocated at initialization time, and is
700 * limited to IDETAPE_PC_STACK requests. We assume that we have enough space for
701 * the maximum possible number of inter-dependent packet commands.
703 * The higher level of the driver - The ioctl handler and the character device
704 * handling functions should queue request to the lower level part and wait for
705 * their completion using idetape_queue_pc_tail or idetape_queue_rw_tail.
707 static void idetape_queue_pc_head(ide_drive_t *drive, struct ide_atapi_pc *pc,
710 struct ide_tape_obj *tape = drive->driver_data;
712 blk_rq_init(NULL, rq);
713 rq->cmd_type = REQ_TYPE_SPECIAL;
714 rq->cmd_flags |= REQ_PREEMPT;
715 rq->buffer = (char *) pc;
716 rq->rq_disk = tape->disk;
717 memcpy(rq->cmd, pc->c, 12);
718 rq->cmd[13] = REQ_IDETAPE_PC1;
719 ide_do_drive_cmd(drive, rq);
723 * idetape_retry_pc is called when an error was detected during the
724 * last packet command. We queue a request sense packet command in
725 * the head of the request list.
727 static void idetape_retry_pc(ide_drive_t *drive)
729 struct ide_atapi_pc *pc;
732 (void)ide_read_error(drive);
733 pc = idetape_next_pc_storage(drive);
734 rq = idetape_next_rq_storage(drive);
735 idetape_create_request_sense_cmd(pc);
736 set_bit(IDE_AFLAG_IGNORE_DSC, &drive->atapi_flags);
737 idetape_queue_pc_head(drive, pc, rq);
741 * Postpone the current request so that ide.c will be able to service requests
742 * from another device on the same hwgroup while we are polling for DSC.
744 static void idetape_postpone_request(ide_drive_t *drive)
746 idetape_tape_t *tape = drive->driver_data;
748 debug_log(DBG_PROCS, "Enter %s\n", __func__);
750 tape->postponed_rq = HWGROUP(drive)->rq;
751 ide_stall_queue(drive, tape->dsc_poll_freq);
754 static void ide_tape_handle_dsc(ide_drive_t *drive)
756 idetape_tape_t *tape = drive->driver_data;
758 /* Media access command */
759 tape->dsc_polling_start = jiffies;
760 tape->dsc_poll_freq = IDETAPE_DSC_MA_FAST;
761 tape->dsc_timeout = jiffies + IDETAPE_DSC_MA_TIMEOUT;
762 /* Allow ide.c to handle other requests */
763 idetape_postpone_request(drive);
766 static void ide_tape_io_buffers(ide_drive_t *drive, struct ide_atapi_pc *pc,
767 unsigned int bcount, int write)
770 idetape_output_buffers(drive, pc, bcount);
772 idetape_input_buffers(drive, pc, bcount);
776 * This is the usual interrupt handler which will be called during a packet
777 * command. We will transfer some of the data (as requested by the drive) and
778 * will re-point interrupt handler to us. When data transfer is finished, we
779 * will act according to the algorithm described before
782 static ide_startstop_t idetape_pc_intr(ide_drive_t *drive)
784 idetape_tape_t *tape = drive->driver_data;
786 return ide_pc_intr(drive, tape->pc, idetape_pc_intr, IDETAPE_WAIT_CMD,
787 NULL, idetape_update_buffers, idetape_retry_pc,
788 ide_tape_handle_dsc, ide_tape_io_buffers);
792 * Packet Command Interface
794 * The current Packet Command is available in tape->pc, and will not change
795 * until we finish handling it. Each packet command is associated with a
796 * callback function that will be called when the command is finished.
798 * The handling will be done in three stages:
800 * 1. idetape_issue_pc will send the packet command to the drive, and will set
801 * the interrupt handler to idetape_pc_intr.
803 * 2. On each interrupt, idetape_pc_intr will be called. This step will be
804 * repeated until the device signals us that no more interrupts will be issued.
806 * 3. ATAPI Tape media access commands have immediate status with a delayed
807 * process. In case of a successful initiation of a media access packet command,
808 * the DSC bit will be set when the actual execution of the command is finished.
809 * Since the tape drive will not issue an interrupt, we have to poll for this
810 * event. In this case, we define the request as "low priority request" by
811 * setting rq_status to IDETAPE_RQ_POSTPONED, set a timer to poll for DSC and
814 * ide.c will then give higher priority to requests which originate from the
815 * other device, until will change rq_status to RQ_ACTIVE.
817 * 4. When the packet command is finished, it will be checked for errors.
819 * 5. In case an error was found, we queue a request sense packet command in
820 * front of the request queue and retry the operation up to
821 * IDETAPE_MAX_PC_RETRIES times.
823 * 6. In case no error was found, or we decided to give up and not to retry
824 * again, the callback function will be called and then we will handle the next
827 static ide_startstop_t idetape_transfer_pc(ide_drive_t *drive)
829 idetape_tape_t *tape = drive->driver_data;
831 return ide_transfer_pc(drive, tape->pc, idetape_pc_intr,
832 IDETAPE_WAIT_CMD, NULL);
835 static ide_startstop_t idetape_issue_pc(ide_drive_t *drive,
836 struct ide_atapi_pc *pc)
838 idetape_tape_t *tape = drive->driver_data;
840 if (tape->pc->c[0] == REQUEST_SENSE &&
841 pc->c[0] == REQUEST_SENSE) {
842 printk(KERN_ERR "ide-tape: possible ide-tape.c bug - "
843 "Two request sense in serial were issued\n");
846 if (tape->failed_pc == NULL && pc->c[0] != REQUEST_SENSE)
847 tape->failed_pc = pc;
848 /* Set the current packet command */
851 if (pc->retries > IDETAPE_MAX_PC_RETRIES ||
852 (pc->flags & PC_FLAG_ABORT)) {
854 * We will "abort" retrying a packet command in case legitimate
855 * error code was received (crossing a filemark, or end of the
856 * media, for example).
858 if (!(pc->flags & PC_FLAG_ABORT)) {
859 if (!(pc->c[0] == TEST_UNIT_READY &&
860 tape->sense_key == 2 && tape->asc == 4 &&
861 (tape->ascq == 1 || tape->ascq == 8))) {
862 printk(KERN_ERR "ide-tape: %s: I/O error, "
863 "pc = %2x, key = %2x, "
864 "asc = %2x, ascq = %2x\n",
865 tape->name, pc->c[0],
866 tape->sense_key, tape->asc,
870 pc->error = IDETAPE_ERROR_GENERAL;
872 tape->failed_pc = NULL;
873 drive->pc_callback(drive);
876 debug_log(DBG_SENSE, "Retry #%d, cmd = %02X\n", pc->retries, pc->c[0]);
880 return ide_issue_pc(drive, pc, idetape_transfer_pc,
881 IDETAPE_WAIT_CMD, NULL);
884 /* A mode sense command is used to "sense" tape parameters. */
885 static void idetape_create_mode_sense_cmd(struct ide_atapi_pc *pc, u8 page_code)
888 pc->c[0] = MODE_SENSE;
889 if (page_code != IDETAPE_BLOCK_DESCRIPTOR)
890 /* DBD = 1 - Don't return block descriptors */
892 pc->c[2] = page_code;
894 * Changed pc->c[3] to 0 (255 will at best return unused info).
896 * For SCSI this byte is defined as subpage instead of high byte
897 * of length and some IDE drives seem to interpret it this way
898 * and return an error when 255 is used.
901 /* We will just discard data in that case */
903 if (page_code == IDETAPE_BLOCK_DESCRIPTOR)
905 else if (page_code == IDETAPE_CAPABILITIES_PAGE)
911 static ide_startstop_t idetape_media_access_finished(ide_drive_t *drive)
913 ide_hwif_t *hwif = drive->hwif;
914 idetape_tape_t *tape = drive->driver_data;
915 struct ide_atapi_pc *pc = tape->pc;
918 stat = hwif->tp_ops->read_status(hwif);
920 if (stat & ATA_DSC) {
921 if (stat & ATA_ERR) {
923 if (pc->c[0] != TEST_UNIT_READY)
924 printk(KERN_ERR "ide-tape: %s: I/O error, ",
926 /* Retry operation */
927 idetape_retry_pc(drive);
932 pc->error = IDETAPE_ERROR_GENERAL;
933 tape->failed_pc = NULL;
935 drive->pc_callback(drive);
939 static void ide_tape_create_rw_cmd(idetape_tape_t *tape,
940 struct ide_atapi_pc *pc, struct request *rq,
943 struct idetape_bh *bh = (struct idetape_bh *)rq->special;
944 unsigned int length = rq->current_nr_sectors;
947 put_unaligned(cpu_to_be32(length), (unsigned int *) &pc->c[1]);
951 pc->buf_size = length * tape->blk_size;
952 pc->req_xfer = pc->buf_size;
953 if (pc->req_xfer == tape->buffer_size)
954 pc->flags |= PC_FLAG_DMA_OK;
956 if (opcode == READ_6) {
958 atomic_set(&bh->b_count, 0);
959 } else if (opcode == WRITE_6) {
961 pc->flags |= PC_FLAG_WRITING;
962 pc->b_data = bh->b_data;
963 pc->b_count = atomic_read(&bh->b_count);
966 memcpy(rq->cmd, pc->c, 12);
969 static ide_startstop_t idetape_do_request(ide_drive_t *drive,
970 struct request *rq, sector_t block)
972 ide_hwif_t *hwif = drive->hwif;
973 idetape_tape_t *tape = drive->driver_data;
974 struct ide_atapi_pc *pc = NULL;
975 struct request *postponed_rq = tape->postponed_rq;
978 debug_log(DBG_SENSE, "sector: %llu, nr_sectors: %lu,"
979 " current_nr_sectors: %u\n",
980 (unsigned long long)rq->sector, rq->nr_sectors,
981 rq->current_nr_sectors);
983 if (!blk_special_request(rq)) {
984 /* We do not support buffer cache originated requests. */
985 printk(KERN_NOTICE "ide-tape: %s: Unsupported request in "
986 "request queue (%d)\n", drive->name, rq->cmd_type);
987 ide_end_request(drive, 0, 0);
991 /* Retry a failed packet command */
992 if (tape->failed_pc && tape->pc->c[0] == REQUEST_SENSE) {
993 pc = tape->failed_pc;
997 if (postponed_rq != NULL)
998 if (rq != postponed_rq) {
999 printk(KERN_ERR "ide-tape: ide-tape.c bug - "
1000 "Two DSC requests were queued\n");
1001 idetape_end_request(drive, 0, 0);
1005 tape->postponed_rq = NULL;
1008 * If the tape is still busy, postpone our request and service
1009 * the other device meanwhile.
1011 stat = hwif->tp_ops->read_status(hwif);
1013 if (!drive->dsc_overlap && !(rq->cmd[13] & REQ_IDETAPE_PC2))
1014 set_bit(IDE_AFLAG_IGNORE_DSC, &drive->atapi_flags);
1016 if (drive->post_reset == 1) {
1017 set_bit(IDE_AFLAG_IGNORE_DSC, &drive->atapi_flags);
1018 drive->post_reset = 0;
1021 if (!test_and_clear_bit(IDE_AFLAG_IGNORE_DSC, &drive->atapi_flags) &&
1022 (stat & ATA_DSC) == 0) {
1023 if (postponed_rq == NULL) {
1024 tape->dsc_polling_start = jiffies;
1025 tape->dsc_poll_freq = tape->best_dsc_rw_freq;
1026 tape->dsc_timeout = jiffies + IDETAPE_DSC_RW_TIMEOUT;
1027 } else if (time_after(jiffies, tape->dsc_timeout)) {
1028 printk(KERN_ERR "ide-tape: %s: DSC timeout\n",
1030 if (rq->cmd[13] & REQ_IDETAPE_PC2) {
1031 idetape_media_access_finished(drive);
1034 return ide_do_reset(drive);
1036 } else if (time_after(jiffies,
1037 tape->dsc_polling_start +
1038 IDETAPE_DSC_MA_THRESHOLD))
1039 tape->dsc_poll_freq = IDETAPE_DSC_MA_SLOW;
1040 idetape_postpone_request(drive);
1043 if (rq->cmd[13] & REQ_IDETAPE_READ) {
1044 pc = idetape_next_pc_storage(drive);
1045 ide_tape_create_rw_cmd(tape, pc, rq, READ_6);
1048 if (rq->cmd[13] & REQ_IDETAPE_WRITE) {
1049 pc = idetape_next_pc_storage(drive);
1050 ide_tape_create_rw_cmd(tape, pc, rq, WRITE_6);
1053 if (rq->cmd[13] & REQ_IDETAPE_PC1) {
1054 pc = (struct ide_atapi_pc *) rq->buffer;
1055 rq->cmd[13] &= ~(REQ_IDETAPE_PC1);
1056 rq->cmd[13] |= REQ_IDETAPE_PC2;
1059 if (rq->cmd[13] & REQ_IDETAPE_PC2) {
1060 idetape_media_access_finished(drive);
1066 return idetape_issue_pc(drive, pc);
1070 * The function below uses __get_free_pages to allocate a data buffer of size
1071 * tape->buffer_size (or a bit more). We attempt to combine sequential pages as
1074 * It returns a pointer to the newly allocated buffer, or NULL in case of
1077 static struct idetape_bh *ide_tape_kmalloc_buffer(idetape_tape_t *tape,
1078 int full, int clear)
1080 struct idetape_bh *prev_bh, *bh, *merge_bh;
1081 int pages = tape->pages_per_buffer;
1082 unsigned int order, b_allocd;
1083 char *b_data = NULL;
1085 merge_bh = kmalloc(sizeof(struct idetape_bh), GFP_KERNEL);
1090 order = fls(pages) - 1;
1091 bh->b_data = (char *) __get_free_pages(GFP_KERNEL, order);
1094 b_allocd = (1 << order) * PAGE_SIZE;
1098 memset(bh->b_data, 0, b_allocd);
1099 bh->b_reqnext = NULL;
1100 bh->b_size = b_allocd;
1101 atomic_set(&bh->b_count, full ? bh->b_size : 0);
1104 order = fls(pages) - 1;
1105 b_data = (char *) __get_free_pages(GFP_KERNEL, order);
1108 b_allocd = (1 << order) * PAGE_SIZE;
1111 memset(b_data, 0, b_allocd);
1113 /* newly allocated page frames below buffer header or ...*/
1114 if (bh->b_data == b_data + b_allocd) {
1115 bh->b_size += b_allocd;
1116 bh->b_data -= b_allocd;
1118 atomic_add(b_allocd, &bh->b_count);
1121 /* they are above the header */
1122 if (b_data == bh->b_data + bh->b_size) {
1123 bh->b_size += b_allocd;
1125 atomic_add(b_allocd, &bh->b_count);
1129 bh = kmalloc(sizeof(struct idetape_bh), GFP_KERNEL);
1131 free_pages((unsigned long) b_data, order);
1134 bh->b_reqnext = NULL;
1135 bh->b_data = b_data;
1136 bh->b_size = b_allocd;
1137 atomic_set(&bh->b_count, full ? bh->b_size : 0);
1138 prev_bh->b_reqnext = bh;
1143 bh->b_size -= tape->excess_bh_size;
1145 atomic_sub(tape->excess_bh_size, &bh->b_count);
1148 ide_tape_kfree_buffer(tape);
1152 static int idetape_copy_stage_from_user(idetape_tape_t *tape,
1153 const char __user *buf, int n)
1155 struct idetape_bh *bh = tape->bh;
1161 printk(KERN_ERR "ide-tape: bh == NULL in %s\n",
1165 count = min((unsigned int)
1166 (bh->b_size - atomic_read(&bh->b_count)),
1168 if (copy_from_user(bh->b_data + atomic_read(&bh->b_count), buf,
1172 atomic_add(count, &bh->b_count);
1174 if (atomic_read(&bh->b_count) == bh->b_size) {
1177 atomic_set(&bh->b_count, 0);
1184 static int idetape_copy_stage_to_user(idetape_tape_t *tape, char __user *buf,
1187 struct idetape_bh *bh = tape->bh;
1193 printk(KERN_ERR "ide-tape: bh == NULL in %s\n",
1197 count = min(tape->b_count, n);
1198 if (copy_to_user(buf, tape->b_data, count))
1201 tape->b_data += count;
1202 tape->b_count -= count;
1204 if (!tape->b_count) {
1208 tape->b_data = bh->b_data;
1209 tape->b_count = atomic_read(&bh->b_count);
1216 static void idetape_init_merge_buffer(idetape_tape_t *tape)
1218 struct idetape_bh *bh = tape->merge_bh;
1219 tape->bh = tape->merge_bh;
1221 if (tape->chrdev_dir == IDETAPE_DIR_WRITE)
1222 atomic_set(&bh->b_count, 0);
1224 tape->b_data = bh->b_data;
1225 tape->b_count = atomic_read(&bh->b_count);
1230 * Write a filemark if write_filemark=1. Flush the device buffers without
1231 * writing a filemark otherwise.
1233 static void idetape_create_write_filemark_cmd(ide_drive_t *drive,
1234 struct ide_atapi_pc *pc, int write_filemark)
1236 idetape_init_pc(pc);
1237 pc->c[0] = WRITE_FILEMARKS;
1238 pc->c[4] = write_filemark;
1239 pc->flags |= PC_FLAG_WAIT_FOR_DSC;
1242 static void idetape_create_test_unit_ready_cmd(struct ide_atapi_pc *pc)
1244 idetape_init_pc(pc);
1245 pc->c[0] = TEST_UNIT_READY;
1249 * We add a special packet command request to the tail of the request queue, and
1250 * wait for it to be serviced. This is not to be called from within the request
1251 * handling part of the driver! We allocate here data on the stack and it is
1252 * valid until the request is finished. This is not the case for the bottom part
1253 * of the driver, where we are always leaving the functions to wait for an
1254 * interrupt or a timer event.
1256 * From the bottom part of the driver, we should allocate safe memory using
1257 * idetape_next_pc_storage() and ide_tape_next_rq_storage(), and add the request
1258 * to the request list without waiting for it to be serviced! In that case, we
1259 * usually use idetape_queue_pc_head().
1261 static int idetape_queue_pc_tail(ide_drive_t *drive, struct ide_atapi_pc *pc)
1263 struct ide_tape_obj *tape = drive->driver_data;
1267 rq = blk_get_request(drive->queue, READ, __GFP_WAIT);
1268 rq->cmd_type = REQ_TYPE_SPECIAL;
1269 rq->cmd[13] = REQ_IDETAPE_PC1;
1270 rq->buffer = (char *)pc;
1271 memcpy(rq->cmd, pc->c, 12);
1272 error = blk_execute_rq(drive->queue, tape->disk, rq, 0);
1273 blk_put_request(rq);
1277 static void idetape_create_load_unload_cmd(ide_drive_t *drive,
1278 struct ide_atapi_pc *pc, int cmd)
1280 idetape_init_pc(pc);
1281 pc->c[0] = START_STOP;
1283 pc->flags |= PC_FLAG_WAIT_FOR_DSC;
1286 static int idetape_wait_ready(ide_drive_t *drive, unsigned long timeout)
1288 idetape_tape_t *tape = drive->driver_data;
1289 struct ide_atapi_pc pc;
1290 int load_attempted = 0;
1292 /* Wait for the tape to become ready */
1293 set_bit(IDE_AFLAG_MEDIUM_PRESENT, &drive->atapi_flags);
1295 while (time_before(jiffies, timeout)) {
1296 idetape_create_test_unit_ready_cmd(&pc);
1297 if (!idetape_queue_pc_tail(drive, &pc))
1299 if ((tape->sense_key == 2 && tape->asc == 4 && tape->ascq == 2)
1300 || (tape->asc == 0x3A)) {
1304 idetape_create_load_unload_cmd(drive, &pc,
1305 IDETAPE_LU_LOAD_MASK);
1306 idetape_queue_pc_tail(drive, &pc);
1308 /* not about to be ready */
1309 } else if (!(tape->sense_key == 2 && tape->asc == 4 &&
1310 (tape->ascq == 1 || tape->ascq == 8)))
1317 static int idetape_flush_tape_buffers(ide_drive_t *drive)
1319 struct ide_atapi_pc pc;
1322 idetape_create_write_filemark_cmd(drive, &pc, 0);
1323 rc = idetape_queue_pc_tail(drive, &pc);
1326 idetape_wait_ready(drive, 60 * 5 * HZ);
1330 static void idetape_create_read_position_cmd(struct ide_atapi_pc *pc)
1332 idetape_init_pc(pc);
1333 pc->c[0] = READ_POSITION;
1337 static int idetape_read_position(ide_drive_t *drive)
1339 idetape_tape_t *tape = drive->driver_data;
1340 struct ide_atapi_pc pc;
1343 debug_log(DBG_PROCS, "Enter %s\n", __func__);
1345 idetape_create_read_position_cmd(&pc);
1346 if (idetape_queue_pc_tail(drive, &pc))
1348 position = tape->first_frame;
1352 static void idetape_create_locate_cmd(ide_drive_t *drive,
1353 struct ide_atapi_pc *pc,
1354 unsigned int block, u8 partition, int skip)
1356 idetape_init_pc(pc);
1357 pc->c[0] = POSITION_TO_ELEMENT;
1359 put_unaligned(cpu_to_be32(block), (unsigned int *) &pc->c[3]);
1360 pc->c[8] = partition;
1361 pc->flags |= PC_FLAG_WAIT_FOR_DSC;
1364 static int idetape_create_prevent_cmd(ide_drive_t *drive,
1365 struct ide_atapi_pc *pc, int prevent)
1367 idetape_tape_t *tape = drive->driver_data;
1369 /* device supports locking according to capabilities page */
1370 if (!(tape->caps[6] & 0x01))
1373 idetape_init_pc(pc);
1374 pc->c[0] = ALLOW_MEDIUM_REMOVAL;
1379 static void __ide_tape_discard_merge_buffer(ide_drive_t *drive)
1381 idetape_tape_t *tape = drive->driver_data;
1383 if (tape->chrdev_dir != IDETAPE_DIR_READ)
1386 clear_bit(IDE_AFLAG_FILEMARK, &drive->atapi_flags);
1387 tape->merge_bh_size = 0;
1388 if (tape->merge_bh != NULL) {
1389 ide_tape_kfree_buffer(tape);
1390 tape->merge_bh = NULL;
1393 tape->chrdev_dir = IDETAPE_DIR_NONE;
1397 * Position the tape to the requested block using the LOCATE packet command.
1398 * A READ POSITION command is then issued to check where we are positioned. Like
1399 * all higher level operations, we queue the commands at the tail of the request
1400 * queue and wait for their completion.
1402 static int idetape_position_tape(ide_drive_t *drive, unsigned int block,
1403 u8 partition, int skip)
1405 idetape_tape_t *tape = drive->driver_data;
1407 struct ide_atapi_pc pc;
1409 if (tape->chrdev_dir == IDETAPE_DIR_READ)
1410 __ide_tape_discard_merge_buffer(drive);
1411 idetape_wait_ready(drive, 60 * 5 * HZ);
1412 idetape_create_locate_cmd(drive, &pc, block, partition, skip);
1413 retval = idetape_queue_pc_tail(drive, &pc);
1417 idetape_create_read_position_cmd(&pc);
1418 return (idetape_queue_pc_tail(drive, &pc));
1421 static void ide_tape_discard_merge_buffer(ide_drive_t *drive,
1422 int restore_position)
1424 idetape_tape_t *tape = drive->driver_data;
1427 __ide_tape_discard_merge_buffer(drive);
1428 if (restore_position) {
1429 position = idetape_read_position(drive);
1430 seek = position > 0 ? position : 0;
1431 if (idetape_position_tape(drive, seek, 0, 0)) {
1432 printk(KERN_INFO "ide-tape: %s: position_tape failed in"
1433 " %s\n", tape->name, __func__);
1440 * Generate a read/write request for the block device interface and wait for it
1443 static int idetape_queue_rw_tail(ide_drive_t *drive, int cmd, int blocks,
1444 struct idetape_bh *bh)
1446 idetape_tape_t *tape = drive->driver_data;
1450 debug_log(DBG_SENSE, "%s: cmd=%d\n", __func__, cmd);
1452 rq = blk_get_request(drive->queue, READ, __GFP_WAIT);
1453 rq->cmd_type = REQ_TYPE_SPECIAL;
1455 rq->rq_disk = tape->disk;
1456 rq->special = (void *)bh;
1457 rq->sector = tape->first_frame;
1458 rq->nr_sectors = blocks;
1459 rq->current_nr_sectors = blocks;
1460 blk_execute_rq(drive->queue, tape->disk, rq, 0);
1462 errors = rq->errors;
1463 ret = tape->blk_size * (blocks - rq->current_nr_sectors);
1464 blk_put_request(rq);
1466 if ((cmd & (REQ_IDETAPE_READ | REQ_IDETAPE_WRITE)) == 0)
1470 idetape_init_merge_buffer(tape);
1471 if (errors == IDETAPE_ERROR_GENERAL)
1476 static void idetape_create_inquiry_cmd(struct ide_atapi_pc *pc)
1478 idetape_init_pc(pc);
1484 static void idetape_create_rewind_cmd(ide_drive_t *drive,
1485 struct ide_atapi_pc *pc)
1487 idetape_init_pc(pc);
1488 pc->c[0] = REZERO_UNIT;
1489 pc->flags |= PC_FLAG_WAIT_FOR_DSC;
1492 static void idetape_create_erase_cmd(struct ide_atapi_pc *pc)
1494 idetape_init_pc(pc);
1497 pc->flags |= PC_FLAG_WAIT_FOR_DSC;
1500 static void idetape_create_space_cmd(struct ide_atapi_pc *pc, int count, u8 cmd)
1502 idetape_init_pc(pc);
1504 put_unaligned(cpu_to_be32(count), (unsigned int *) &pc->c[1]);
1506 pc->flags |= PC_FLAG_WAIT_FOR_DSC;
1509 /* Queue up a character device originated write request. */
1510 static int idetape_add_chrdev_write_request(ide_drive_t *drive, int blocks)
1512 idetape_tape_t *tape = drive->driver_data;
1514 debug_log(DBG_CHRDEV, "Enter %s\n", __func__);
1516 return idetape_queue_rw_tail(drive, REQ_IDETAPE_WRITE,
1517 blocks, tape->merge_bh);
1520 static void ide_tape_flush_merge_buffer(ide_drive_t *drive)
1522 idetape_tape_t *tape = drive->driver_data;
1524 struct idetape_bh *bh;
1526 if (tape->chrdev_dir != IDETAPE_DIR_WRITE) {
1527 printk(KERN_ERR "ide-tape: bug: Trying to empty merge buffer"
1528 " but we are not writing.\n");
1531 if (tape->merge_bh_size > tape->buffer_size) {
1532 printk(KERN_ERR "ide-tape: bug: merge_buffer too big\n");
1533 tape->merge_bh_size = tape->buffer_size;
1535 if (tape->merge_bh_size) {
1536 blocks = tape->merge_bh_size / tape->blk_size;
1537 if (tape->merge_bh_size % tape->blk_size) {
1541 i = tape->blk_size - tape->merge_bh_size %
1543 bh = tape->bh->b_reqnext;
1545 atomic_set(&bh->b_count, 0);
1551 printk(KERN_INFO "ide-tape: bug,"
1555 min = min(i, (unsigned int)(bh->b_size -
1556 atomic_read(&bh->b_count)));
1557 memset(bh->b_data + atomic_read(&bh->b_count),
1559 atomic_add(min, &bh->b_count);
1564 (void) idetape_add_chrdev_write_request(drive, blocks);
1565 tape->merge_bh_size = 0;
1567 if (tape->merge_bh != NULL) {
1568 ide_tape_kfree_buffer(tape);
1569 tape->merge_bh = NULL;
1571 tape->chrdev_dir = IDETAPE_DIR_NONE;
1574 static int idetape_init_read(ide_drive_t *drive)
1576 idetape_tape_t *tape = drive->driver_data;
1579 /* Initialize read operation */
1580 if (tape->chrdev_dir != IDETAPE_DIR_READ) {
1581 if (tape->chrdev_dir == IDETAPE_DIR_WRITE) {
1582 ide_tape_flush_merge_buffer(drive);
1583 idetape_flush_tape_buffers(drive);
1585 if (tape->merge_bh || tape->merge_bh_size) {
1586 printk(KERN_ERR "ide-tape: merge_bh_size should be"
1588 tape->merge_bh_size = 0;
1590 tape->merge_bh = ide_tape_kmalloc_buffer(tape, 0, 0);
1591 if (!tape->merge_bh)
1593 tape->chrdev_dir = IDETAPE_DIR_READ;
1596 * Issue a read 0 command to ensure that DSC handshake is
1597 * switched from completion mode to buffer available mode.
1598 * No point in issuing this if DSC overlap isn't supported, some
1599 * drives (Seagate STT3401A) will return an error.
1601 if (drive->dsc_overlap) {
1602 bytes_read = idetape_queue_rw_tail(drive,
1603 REQ_IDETAPE_READ, 0,
1605 if (bytes_read < 0) {
1606 ide_tape_kfree_buffer(tape);
1607 tape->merge_bh = NULL;
1608 tape->chrdev_dir = IDETAPE_DIR_NONE;
1617 /* called from idetape_chrdev_read() to service a chrdev read request. */
1618 static int idetape_add_chrdev_read_request(ide_drive_t *drive, int blocks)
1620 idetape_tape_t *tape = drive->driver_data;
1622 debug_log(DBG_PROCS, "Enter %s, %d blocks\n", __func__, blocks);
1624 /* If we are at a filemark, return a read length of 0 */
1625 if (test_bit(IDE_AFLAG_FILEMARK, &drive->atapi_flags))
1628 idetape_init_read(drive);
1630 return idetape_queue_rw_tail(drive, REQ_IDETAPE_READ, blocks,
1634 static void idetape_pad_zeros(ide_drive_t *drive, int bcount)
1636 idetape_tape_t *tape = drive->driver_data;
1637 struct idetape_bh *bh;
1643 bh = tape->merge_bh;
1644 count = min(tape->buffer_size, bcount);
1646 blocks = count / tape->blk_size;
1648 atomic_set(&bh->b_count,
1649 min(count, (unsigned int)bh->b_size));
1650 memset(bh->b_data, 0, atomic_read(&bh->b_count));
1651 count -= atomic_read(&bh->b_count);
1654 idetape_queue_rw_tail(drive, REQ_IDETAPE_WRITE, blocks,
1660 * Rewinds the tape to the Beginning Of the current Partition (BOP). We
1661 * currently support only one partition.
1663 static int idetape_rewind_tape(ide_drive_t *drive)
1666 struct ide_atapi_pc pc;
1667 idetape_tape_t *tape;
1668 tape = drive->driver_data;
1670 debug_log(DBG_SENSE, "Enter %s\n", __func__);
1672 idetape_create_rewind_cmd(drive, &pc);
1673 retval = idetape_queue_pc_tail(drive, &pc);
1677 idetape_create_read_position_cmd(&pc);
1678 retval = idetape_queue_pc_tail(drive, &pc);
1684 /* mtio.h compatible commands should be issued to the chrdev interface. */
1685 static int idetape_blkdev_ioctl(ide_drive_t *drive, unsigned int cmd,
1688 idetape_tape_t *tape = drive->driver_data;
1689 void __user *argp = (void __user *)arg;
1691 struct idetape_config {
1692 int dsc_rw_frequency;
1693 int dsc_media_access_frequency;
1697 debug_log(DBG_PROCS, "Enter %s\n", __func__);
1701 if (copy_from_user(&config, argp, sizeof(config)))
1703 tape->best_dsc_rw_freq = config.dsc_rw_frequency;
1706 config.dsc_rw_frequency = (int) tape->best_dsc_rw_freq;
1707 config.nr_stages = 1;
1708 if (copy_to_user(argp, &config, sizeof(config)))
1717 static int idetape_space_over_filemarks(ide_drive_t *drive, short mt_op,
1720 idetape_tape_t *tape = drive->driver_data;
1721 struct ide_atapi_pc pc;
1722 int retval, count = 0;
1723 int sprev = !!(tape->caps[4] & 0x20);
1727 if (MTBSF == mt_op || MTBSFM == mt_op) {
1730 mt_count = -mt_count;
1733 if (tape->chrdev_dir == IDETAPE_DIR_READ) {
1734 tape->merge_bh_size = 0;
1735 if (test_and_clear_bit(IDE_AFLAG_FILEMARK, &drive->atapi_flags))
1737 ide_tape_discard_merge_buffer(drive, 0);
1743 idetape_create_space_cmd(&pc, mt_count - count,
1744 IDETAPE_SPACE_OVER_FILEMARK);
1745 return idetape_queue_pc_tail(drive, &pc);
1750 retval = idetape_space_over_filemarks(drive, MTFSF,
1754 count = (MTBSFM == mt_op ? 1 : -1);
1755 return idetape_space_over_filemarks(drive, MTFSF, count);
1757 printk(KERN_ERR "ide-tape: MTIO operation %d not supported\n",
1764 * Our character device read / write functions.
1766 * The tape is optimized to maximize throughput when it is transferring an
1767 * integral number of the "continuous transfer limit", which is a parameter of
1768 * the specific tape (26kB on my particular tape, 32kB for Onstream).
1770 * As of version 1.3 of the driver, the character device provides an abstract
1771 * continuous view of the media - any mix of block sizes (even 1 byte) on the
1772 * same backup/restore procedure is supported. The driver will internally
1773 * convert the requests to the recommended transfer unit, so that an unmatch
1774 * between the user's block size to the recommended size will only result in a
1775 * (slightly) increased driver overhead, but will no longer hit performance.
1776 * This is not applicable to Onstream.
1778 static ssize_t idetape_chrdev_read(struct file *file, char __user *buf,
1779 size_t count, loff_t *ppos)
1781 struct ide_tape_obj *tape = ide_tape_f(file);
1782 ide_drive_t *drive = tape->drive;
1783 ssize_t bytes_read, temp, actually_read = 0, rc;
1785 u16 ctl = *(u16 *)&tape->caps[12];
1787 debug_log(DBG_CHRDEV, "Enter %s, count %Zd\n", __func__, count);
1789 if (tape->chrdev_dir != IDETAPE_DIR_READ) {
1790 if (test_bit(IDE_AFLAG_DETECT_BS, &drive->atapi_flags))
1791 if (count > tape->blk_size &&
1792 (count % tape->blk_size) == 0)
1793 tape->user_bs_factor = count / tape->blk_size;
1795 rc = idetape_init_read(drive);
1800 if (tape->merge_bh_size) {
1801 actually_read = min((unsigned int)(tape->merge_bh_size),
1802 (unsigned int)count);
1803 if (idetape_copy_stage_to_user(tape, buf, actually_read))
1805 buf += actually_read;
1806 tape->merge_bh_size -= actually_read;
1807 count -= actually_read;
1809 while (count >= tape->buffer_size) {
1810 bytes_read = idetape_add_chrdev_read_request(drive, ctl);
1811 if (bytes_read <= 0)
1813 if (idetape_copy_stage_to_user(tape, buf, bytes_read))
1816 count -= bytes_read;
1817 actually_read += bytes_read;
1820 bytes_read = idetape_add_chrdev_read_request(drive, ctl);
1821 if (bytes_read <= 0)
1823 temp = min((unsigned long)count, (unsigned long)bytes_read);
1824 if (idetape_copy_stage_to_user(tape, buf, temp))
1826 actually_read += temp;
1827 tape->merge_bh_size = bytes_read-temp;
1830 if (!actually_read && test_bit(IDE_AFLAG_FILEMARK, &drive->atapi_flags)) {
1831 debug_log(DBG_SENSE, "%s: spacing over filemark\n", tape->name);
1833 idetape_space_over_filemarks(drive, MTFSF, 1);
1837 return ret ? ret : actually_read;
1840 static ssize_t idetape_chrdev_write(struct file *file, const char __user *buf,
1841 size_t count, loff_t *ppos)
1843 struct ide_tape_obj *tape = ide_tape_f(file);
1844 ide_drive_t *drive = tape->drive;
1845 ssize_t actually_written = 0;
1847 u16 ctl = *(u16 *)&tape->caps[12];
1849 /* The drive is write protected. */
1850 if (tape->write_prot)
1853 debug_log(DBG_CHRDEV, "Enter %s, count %Zd\n", __func__, count);
1855 /* Initialize write operation */
1856 if (tape->chrdev_dir != IDETAPE_DIR_WRITE) {
1857 if (tape->chrdev_dir == IDETAPE_DIR_READ)
1858 ide_tape_discard_merge_buffer(drive, 1);
1859 if (tape->merge_bh || tape->merge_bh_size) {
1860 printk(KERN_ERR "ide-tape: merge_bh_size "
1861 "should be 0 now\n");
1862 tape->merge_bh_size = 0;
1864 tape->merge_bh = ide_tape_kmalloc_buffer(tape, 0, 0);
1865 if (!tape->merge_bh)
1867 tape->chrdev_dir = IDETAPE_DIR_WRITE;
1868 idetape_init_merge_buffer(tape);
1871 * Issue a write 0 command to ensure that DSC handshake is
1872 * switched from completion mode to buffer available mode. No
1873 * point in issuing this if DSC overlap isn't supported, some
1874 * drives (Seagate STT3401A) will return an error.
1876 if (drive->dsc_overlap) {
1877 ssize_t retval = idetape_queue_rw_tail(drive,
1878 REQ_IDETAPE_WRITE, 0,
1881 ide_tape_kfree_buffer(tape);
1882 tape->merge_bh = NULL;
1883 tape->chrdev_dir = IDETAPE_DIR_NONE;
1890 if (tape->merge_bh_size) {
1891 if (tape->merge_bh_size >= tape->buffer_size) {
1892 printk(KERN_ERR "ide-tape: bug: merge buf too big\n");
1893 tape->merge_bh_size = 0;
1895 actually_written = min((unsigned int)
1896 (tape->buffer_size - tape->merge_bh_size),
1897 (unsigned int)count);
1898 if (idetape_copy_stage_from_user(tape, buf, actually_written))
1900 buf += actually_written;
1901 tape->merge_bh_size += actually_written;
1902 count -= actually_written;
1904 if (tape->merge_bh_size == tape->buffer_size) {
1906 tape->merge_bh_size = 0;
1907 retval = idetape_add_chrdev_write_request(drive, ctl);
1912 while (count >= tape->buffer_size) {
1914 if (idetape_copy_stage_from_user(tape, buf, tape->buffer_size))
1916 buf += tape->buffer_size;
1917 count -= tape->buffer_size;
1918 retval = idetape_add_chrdev_write_request(drive, ctl);
1919 actually_written += tape->buffer_size;
1924 actually_written += count;
1925 if (idetape_copy_stage_from_user(tape, buf, count))
1927 tape->merge_bh_size += count;
1929 return ret ? ret : actually_written;
1932 static int idetape_write_filemark(ide_drive_t *drive)
1934 struct ide_atapi_pc pc;
1936 /* Write a filemark */
1937 idetape_create_write_filemark_cmd(drive, &pc, 1);
1938 if (idetape_queue_pc_tail(drive, &pc)) {
1939 printk(KERN_ERR "ide-tape: Couldn't write a filemark\n");
1946 * Called from idetape_chrdev_ioctl when the general mtio MTIOCTOP ioctl is
1949 * Note: MTBSF and MTBSFM are not supported when the tape doesn't support
1950 * spacing over filemarks in the reverse direction. In this case, MTFSFM is also
1951 * usually not supported.
1953 * The following commands are currently not supported:
1955 * MTFSS, MTBSS, MTWSM, MTSETDENSITY, MTSETDRVBUFFER, MT_ST_BOOLEANS,
1956 * MT_ST_WRITE_THRESHOLD.
1958 static int idetape_mtioctop(ide_drive_t *drive, short mt_op, int mt_count)
1960 idetape_tape_t *tape = drive->driver_data;
1961 struct ide_atapi_pc pc;
1964 debug_log(DBG_ERR, "Handling MTIOCTOP ioctl: mt_op=%d, mt_count=%d\n",
1974 return idetape_space_over_filemarks(drive, mt_op, mt_count);
1981 if (tape->write_prot)
1983 ide_tape_discard_merge_buffer(drive, 1);
1984 for (i = 0; i < mt_count; i++) {
1985 retval = idetape_write_filemark(drive);
1991 ide_tape_discard_merge_buffer(drive, 0);
1992 if (idetape_rewind_tape(drive))
1996 ide_tape_discard_merge_buffer(drive, 0);
1997 idetape_create_load_unload_cmd(drive, &pc,
1998 IDETAPE_LU_LOAD_MASK);
1999 return idetape_queue_pc_tail(drive, &pc);
2003 * If door is locked, attempt to unlock before
2004 * attempting to eject.
2006 if (tape->door_locked) {
2007 if (idetape_create_prevent_cmd(drive, &pc, 0))
2008 if (!idetape_queue_pc_tail(drive, &pc))
2009 tape->door_locked = DOOR_UNLOCKED;
2011 ide_tape_discard_merge_buffer(drive, 0);
2012 idetape_create_load_unload_cmd(drive, &pc,
2013 !IDETAPE_LU_LOAD_MASK);
2014 retval = idetape_queue_pc_tail(drive, &pc);
2016 clear_bit(IDE_AFLAG_MEDIUM_PRESENT, &drive->atapi_flags);
2019 ide_tape_discard_merge_buffer(drive, 0);
2020 return idetape_flush_tape_buffers(drive);
2022 ide_tape_discard_merge_buffer(drive, 0);
2023 idetape_create_load_unload_cmd(drive, &pc,
2024 IDETAPE_LU_RETENSION_MASK | IDETAPE_LU_LOAD_MASK);
2025 return idetape_queue_pc_tail(drive, &pc);
2027 idetape_create_space_cmd(&pc, 0, IDETAPE_SPACE_TO_EOD);
2028 return idetape_queue_pc_tail(drive, &pc);
2030 (void)idetape_rewind_tape(drive);
2031 idetape_create_erase_cmd(&pc);
2032 return idetape_queue_pc_tail(drive, &pc);
2035 if (mt_count < tape->blk_size ||
2036 mt_count % tape->blk_size)
2038 tape->user_bs_factor = mt_count / tape->blk_size;
2039 clear_bit(IDE_AFLAG_DETECT_BS, &drive->atapi_flags);
2041 set_bit(IDE_AFLAG_DETECT_BS, &drive->atapi_flags);
2044 ide_tape_discard_merge_buffer(drive, 0);
2045 return idetape_position_tape(drive,
2046 mt_count * tape->user_bs_factor, tape->partition, 0);
2048 ide_tape_discard_merge_buffer(drive, 0);
2049 return idetape_position_tape(drive, 0, mt_count, 0);
2053 if (!idetape_create_prevent_cmd(drive, &pc, 1))
2055 retval = idetape_queue_pc_tail(drive, &pc);
2058 tape->door_locked = DOOR_EXPLICITLY_LOCKED;
2061 if (!idetape_create_prevent_cmd(drive, &pc, 0))
2063 retval = idetape_queue_pc_tail(drive, &pc);
2066 tape->door_locked = DOOR_UNLOCKED;
2069 printk(KERN_ERR "ide-tape: MTIO operation %d not supported\n",
2076 * Our character device ioctls. General mtio.h magnetic io commands are
2077 * supported here, and not in the corresponding block interface. Our own
2078 * ide-tape ioctls are supported on both interfaces.
2080 static int idetape_chrdev_ioctl(struct inode *inode, struct file *file,
2081 unsigned int cmd, unsigned long arg)
2083 struct ide_tape_obj *tape = ide_tape_f(file);
2084 ide_drive_t *drive = tape->drive;
2088 int block_offset = 0, position = tape->first_frame;
2089 void __user *argp = (void __user *)arg;
2091 debug_log(DBG_CHRDEV, "Enter %s, cmd=%u\n", __func__, cmd);
2093 if (tape->chrdev_dir == IDETAPE_DIR_WRITE) {
2094 ide_tape_flush_merge_buffer(drive);
2095 idetape_flush_tape_buffers(drive);
2097 if (cmd == MTIOCGET || cmd == MTIOCPOS) {
2098 block_offset = tape->merge_bh_size /
2099 (tape->blk_size * tape->user_bs_factor);
2100 position = idetape_read_position(drive);
2106 if (copy_from_user(&mtop, argp, sizeof(struct mtop)))
2108 return idetape_mtioctop(drive, mtop.mt_op, mtop.mt_count);
2110 memset(&mtget, 0, sizeof(struct mtget));
2111 mtget.mt_type = MT_ISSCSI2;
2112 mtget.mt_blkno = position / tape->user_bs_factor - block_offset;
2114 ((tape->blk_size * tape->user_bs_factor)
2115 << MT_ST_BLKSIZE_SHIFT) & MT_ST_BLKSIZE_MASK;
2117 if (tape->drv_write_prot)
2118 mtget.mt_gstat |= GMT_WR_PROT(0xffffffff);
2120 if (copy_to_user(argp, &mtget, sizeof(struct mtget)))
2124 mtpos.mt_blkno = position / tape->user_bs_factor - block_offset;
2125 if (copy_to_user(argp, &mtpos, sizeof(struct mtpos)))
2129 if (tape->chrdev_dir == IDETAPE_DIR_READ)
2130 ide_tape_discard_merge_buffer(drive, 1);
2131 return idetape_blkdev_ioctl(drive, cmd, arg);
2136 * Do a mode sense page 0 with block descriptor and if it succeeds set the tape
2137 * block size with the reported value.
2139 static void ide_tape_get_bsize_from_bdesc(ide_drive_t *drive)
2141 idetape_tape_t *tape = drive->driver_data;
2142 struct ide_atapi_pc pc;
2144 idetape_create_mode_sense_cmd(&pc, IDETAPE_BLOCK_DESCRIPTOR);
2145 if (idetape_queue_pc_tail(drive, &pc)) {
2146 printk(KERN_ERR "ide-tape: Can't get block descriptor\n");
2147 if (tape->blk_size == 0) {
2148 printk(KERN_WARNING "ide-tape: Cannot deal with zero "
2149 "block size, assuming 32k\n");
2150 tape->blk_size = 32768;
2154 tape->blk_size = (pc.buf[4 + 5] << 16) +
2155 (pc.buf[4 + 6] << 8) +
2157 tape->drv_write_prot = (pc.buf[2] & 0x80) >> 7;
2160 static int idetape_chrdev_open(struct inode *inode, struct file *filp)
2162 unsigned int minor = iminor(inode), i = minor & ~0xc0;
2164 idetape_tape_t *tape;
2165 struct ide_atapi_pc pc;
2168 if (i >= MAX_HWIFS * MAX_DRIVES)
2172 tape = ide_tape_chrdev_get(i);
2178 debug_log(DBG_CHRDEV, "Enter %s\n", __func__);
2181 * We really want to do nonseekable_open(inode, filp); here, but some
2182 * versions of tar incorrectly call lseek on tapes and bail out if that
2183 * fails. So we disallow pread() and pwrite(), but permit lseeks.
2185 filp->f_mode &= ~(FMODE_PREAD | FMODE_PWRITE);
2187 drive = tape->drive;
2189 filp->private_data = tape;
2191 if (test_and_set_bit(IDE_AFLAG_BUSY, &drive->atapi_flags)) {
2196 retval = idetape_wait_ready(drive, 60 * HZ);
2198 clear_bit(IDE_AFLAG_BUSY, &drive->atapi_flags);
2199 printk(KERN_ERR "ide-tape: %s: drive not ready\n", tape->name);
2203 idetape_read_position(drive);
2204 if (!test_bit(IDE_AFLAG_ADDRESS_VALID, &drive->atapi_flags))
2205 (void)idetape_rewind_tape(drive);
2207 /* Read block size and write protect status from drive. */
2208 ide_tape_get_bsize_from_bdesc(drive);
2210 /* Set write protect flag if device is opened as read-only. */
2211 if ((filp->f_flags & O_ACCMODE) == O_RDONLY)
2212 tape->write_prot = 1;
2214 tape->write_prot = tape->drv_write_prot;
2216 /* Make sure drive isn't write protected if user wants to write. */
2217 if (tape->write_prot) {
2218 if ((filp->f_flags & O_ACCMODE) == O_WRONLY ||
2219 (filp->f_flags & O_ACCMODE) == O_RDWR) {
2220 clear_bit(IDE_AFLAG_BUSY, &drive->atapi_flags);
2226 /* Lock the tape drive door so user can't eject. */
2227 if (tape->chrdev_dir == IDETAPE_DIR_NONE) {
2228 if (idetape_create_prevent_cmd(drive, &pc, 1)) {
2229 if (!idetape_queue_pc_tail(drive, &pc)) {
2230 if (tape->door_locked != DOOR_EXPLICITLY_LOCKED)
2231 tape->door_locked = DOOR_LOCKED;
2244 static void idetape_write_release(ide_drive_t *drive, unsigned int minor)
2246 idetape_tape_t *tape = drive->driver_data;
2248 ide_tape_flush_merge_buffer(drive);
2249 tape->merge_bh = ide_tape_kmalloc_buffer(tape, 1, 0);
2250 if (tape->merge_bh != NULL) {
2251 idetape_pad_zeros(drive, tape->blk_size *
2252 (tape->user_bs_factor - 1));
2253 ide_tape_kfree_buffer(tape);
2254 tape->merge_bh = NULL;
2256 idetape_write_filemark(drive);
2257 idetape_flush_tape_buffers(drive);
2258 idetape_flush_tape_buffers(drive);
2261 static int idetape_chrdev_release(struct inode *inode, struct file *filp)
2263 struct ide_tape_obj *tape = ide_tape_f(filp);
2264 ide_drive_t *drive = tape->drive;
2265 struct ide_atapi_pc pc;
2266 unsigned int minor = iminor(inode);
2269 tape = drive->driver_data;
2271 debug_log(DBG_CHRDEV, "Enter %s\n", __func__);
2273 if (tape->chrdev_dir == IDETAPE_DIR_WRITE)
2274 idetape_write_release(drive, minor);
2275 if (tape->chrdev_dir == IDETAPE_DIR_READ) {
2277 ide_tape_discard_merge_buffer(drive, 1);
2280 if (minor < 128 && test_bit(IDE_AFLAG_MEDIUM_PRESENT, &drive->atapi_flags))
2281 (void) idetape_rewind_tape(drive);
2282 if (tape->chrdev_dir == IDETAPE_DIR_NONE) {
2283 if (tape->door_locked == DOOR_LOCKED) {
2284 if (idetape_create_prevent_cmd(drive, &pc, 0)) {
2285 if (!idetape_queue_pc_tail(drive, &pc))
2286 tape->door_locked = DOOR_UNLOCKED;
2290 clear_bit(IDE_AFLAG_BUSY, &drive->atapi_flags);
2296 static void idetape_get_inquiry_results(ide_drive_t *drive)
2298 idetape_tape_t *tape = drive->driver_data;
2299 struct ide_atapi_pc pc;
2300 char fw_rev[4], vendor_id[8], product_id[16];
2302 idetape_create_inquiry_cmd(&pc);
2303 if (idetape_queue_pc_tail(drive, &pc)) {
2304 printk(KERN_ERR "ide-tape: %s: can't get INQUIRY results\n",
2308 memcpy(vendor_id, &pc.buf[8], 8);
2309 memcpy(product_id, &pc.buf[16], 16);
2310 memcpy(fw_rev, &pc.buf[32], 4);
2312 ide_fixstring(vendor_id, 8, 0);
2313 ide_fixstring(product_id, 16, 0);
2314 ide_fixstring(fw_rev, 4, 0);
2316 printk(KERN_INFO "ide-tape: %s <-> %s: %.8s %.16s rev %.4s\n",
2317 drive->name, tape->name, vendor_id, product_id, fw_rev);
2321 * Ask the tape about its various parameters. In particular, we will adjust our
2322 * data transfer buffer size to the recommended value as returned by the tape.
2324 static void idetape_get_mode_sense_results(ide_drive_t *drive)
2326 idetape_tape_t *tape = drive->driver_data;
2327 struct ide_atapi_pc pc;
2329 u8 speed, max_speed;
2331 idetape_create_mode_sense_cmd(&pc, IDETAPE_CAPABILITIES_PAGE);
2332 if (idetape_queue_pc_tail(drive, &pc)) {
2333 printk(KERN_ERR "ide-tape: Can't get tape parameters - assuming"
2334 " some default values\n");
2335 tape->blk_size = 512;
2336 put_unaligned(52, (u16 *)&tape->caps[12]);
2337 put_unaligned(540, (u16 *)&tape->caps[14]);
2338 put_unaligned(6*52, (u16 *)&tape->caps[16]);
2341 caps = pc.buf + 4 + pc.buf[3];
2343 /* convert to host order and save for later use */
2344 speed = be16_to_cpup((__be16 *)&caps[14]);
2345 max_speed = be16_to_cpup((__be16 *)&caps[8]);
2347 *(u16 *)&caps[8] = max_speed;
2348 *(u16 *)&caps[12] = be16_to_cpup((__be16 *)&caps[12]);
2349 *(u16 *)&caps[14] = speed;
2350 *(u16 *)&caps[16] = be16_to_cpup((__be16 *)&caps[16]);
2353 printk(KERN_INFO "ide-tape: %s: invalid tape speed "
2354 "(assuming 650KB/sec)\n", drive->name);
2355 *(u16 *)&caps[14] = 650;
2358 printk(KERN_INFO "ide-tape: %s: invalid max_speed "
2359 "(assuming 650KB/sec)\n", drive->name);
2360 *(u16 *)&caps[8] = 650;
2363 memcpy(&tape->caps, caps, 20);
2365 tape->blk_size = 512;
2366 else if (caps[7] & 0x04)
2367 tape->blk_size = 1024;
2370 #ifdef CONFIG_IDE_PROC_FS
2371 #define ide_tape_devset_get(name, field) \
2372 static int get_##name(ide_drive_t *drive) \
2374 idetape_tape_t *tape = drive->driver_data; \
2375 return tape->field; \
2378 #define ide_tape_devset_set(name, field) \
2379 static int set_##name(ide_drive_t *drive, int arg) \
2381 idetape_tape_t *tape = drive->driver_data; \
2382 tape->field = arg; \
2386 #define ide_tape_devset_rw(_name, _min, _max, _field, _mulf, _divf) \
2387 ide_tape_devset_get(_name, _field) \
2388 ide_tape_devset_set(_name, _field) \
2389 __IDE_DEVSET(_name, S_RW, _min, _max, get_##_name, set_##_name, _mulf, _divf)
2391 #define ide_tape_devset_r(_name, _min, _max, _field, _mulf, _divf) \
2392 ide_tape_devset_get(_name, _field) \
2393 __IDE_DEVSET(_name, S_READ, _min, _max, get_##_name, NULL, _mulf, _divf)
2395 static int mulf_tdsc(ide_drive_t *drive) { return 1000; }
2396 static int divf_tdsc(ide_drive_t *drive) { return HZ; }
2397 static int divf_buffer(ide_drive_t *drive) { return 2; }
2398 static int divf_buffer_size(ide_drive_t *drive) { return 1024; }
2400 ide_devset_rw(dsc_overlap, 0, 1, dsc_overlap);
2402 ide_tape_devset_rw(debug_mask, 0, 0xffff, debug_mask, NULL, NULL);
2403 ide_tape_devset_rw(tdsc, IDETAPE_DSC_RW_MIN, IDETAPE_DSC_RW_MAX,
2404 best_dsc_rw_freq, mulf_tdsc, divf_tdsc);
2406 ide_tape_devset_r(avg_speed, 0, 0xffff, avg_speed, NULL, NULL);
2407 ide_tape_devset_r(speed, 0, 0xffff, caps[14], NULL, NULL);
2408 ide_tape_devset_r(buffer, 0, 0xffff, caps[16], NULL, divf_buffer);
2409 ide_tape_devset_r(buffer_size, 0, 0xffff, buffer_size, NULL, divf_buffer_size);
2411 static const struct ide_devset *idetape_settings[] = {
2412 &ide_devset_avg_speed,
2414 &ide_devset_buffer_size,
2415 &ide_devset_debug_mask,
2416 &ide_devset_dsc_overlap,
2424 * The function below is called to:
2426 * 1. Initialize our various state variables.
2427 * 2. Ask the tape for its capabilities.
2428 * 3. Allocate a buffer which will be used for data transfer. The buffer size
2429 * is chosen based on the recommendation which we received in step 2.
2431 * Note that at this point ide.c already assigned us an irq, so that we can
2432 * queue requests here and wait for their completion.
2434 static void idetape_setup(ide_drive_t *drive, idetape_tape_t *tape, int minor)
2440 u16 *ctl = (u16 *)&tape->caps[12];
2442 drive->pc_callback = ide_tape_callback;
2444 spin_lock_init(&tape->lock);
2445 drive->dsc_overlap = 1;
2446 if (drive->hwif->host_flags & IDE_HFLAG_NO_DSC) {
2447 printk(KERN_INFO "ide-tape: %s: disabling DSC overlap\n",
2449 drive->dsc_overlap = 0;
2451 /* Seagate Travan drives do not support DSC overlap. */
2452 if (strstr((char *)&drive->id[ATA_ID_PROD], "Seagate STT3401"))
2453 drive->dsc_overlap = 0;
2454 tape->minor = minor;
2455 tape->name[0] = 'h';
2456 tape->name[1] = 't';
2457 tape->name[2] = '0' + minor;
2458 tape->chrdev_dir = IDETAPE_DIR_NONE;
2459 tape->pc = tape->pc_stack;
2461 *((u16 *)&gcw) = drive->id[ATA_ID_CONFIG];
2463 /* Command packet DRQ type */
2464 if (((gcw[0] & 0x60) >> 5) == 1)
2465 set_bit(IDE_AFLAG_DRQ_INTERRUPT, &drive->atapi_flags);
2467 idetape_get_inquiry_results(drive);
2468 idetape_get_mode_sense_results(drive);
2469 ide_tape_get_bsize_from_bdesc(drive);
2470 tape->user_bs_factor = 1;
2471 tape->buffer_size = *ctl * tape->blk_size;
2472 while (tape->buffer_size > 0xffff) {
2473 printk(KERN_NOTICE "ide-tape: decreasing stage size\n");
2475 tape->buffer_size = *ctl * tape->blk_size;
2477 buffer_size = tape->buffer_size;
2478 tape->pages_per_buffer = buffer_size / PAGE_SIZE;
2479 if (buffer_size % PAGE_SIZE) {
2480 tape->pages_per_buffer++;
2481 tape->excess_bh_size = PAGE_SIZE - buffer_size % PAGE_SIZE;
2484 /* select the "best" DSC read/write polling freq */
2485 speed = max(*(u16 *)&tape->caps[14], *(u16 *)&tape->caps[8]);
2487 t = (IDETAPE_FIFO_THRESHOLD * tape->buffer_size * HZ) / (speed * 1000);
2490 * Ensure that the number we got makes sense; limit it within
2491 * IDETAPE_DSC_RW_MIN and IDETAPE_DSC_RW_MAX.
2493 tape->best_dsc_rw_freq = clamp_t(unsigned long, t, IDETAPE_DSC_RW_MIN,
2494 IDETAPE_DSC_RW_MAX);
2495 printk(KERN_INFO "ide-tape: %s <-> %s: %dKBps, %d*%dkB buffer, "
2497 drive->name, tape->name, *(u16 *)&tape->caps[14],
2498 (*(u16 *)&tape->caps[16] * 512) / tape->buffer_size,
2499 tape->buffer_size / 1024,
2500 tape->best_dsc_rw_freq * 1000 / HZ,
2501 drive->using_dma ? ", DMA":"");
2503 ide_proc_register_driver(drive, tape->driver);
2506 static void ide_tape_remove(ide_drive_t *drive)
2508 idetape_tape_t *tape = drive->driver_data;
2510 ide_proc_unregister_driver(drive, tape->driver);
2512 ide_unregister_region(tape->disk);
2517 static void ide_tape_release(struct kref *kref)
2519 struct ide_tape_obj *tape = to_ide_tape(kref);
2520 ide_drive_t *drive = tape->drive;
2521 struct gendisk *g = tape->disk;
2523 BUG_ON(tape->merge_bh_size);
2525 drive->dsc_overlap = 0;
2526 drive->driver_data = NULL;
2527 device_destroy(idetape_sysfs_class, MKDEV(IDETAPE_MAJOR, tape->minor));
2528 device_destroy(idetape_sysfs_class,
2529 MKDEV(IDETAPE_MAJOR, tape->minor + 128));
2530 idetape_devs[tape->minor] = NULL;
2531 g->private_data = NULL;
2536 #ifdef CONFIG_IDE_PROC_FS
2537 static int proc_idetape_read_name
2538 (char *page, char **start, off_t off, int count, int *eof, void *data)
2540 ide_drive_t *drive = (ide_drive_t *) data;
2541 idetape_tape_t *tape = drive->driver_data;
2545 len = sprintf(out, "%s\n", tape->name);
2546 PROC_IDE_READ_RETURN(page, start, off, count, eof, len);
2549 static ide_proc_entry_t idetape_proc[] = {
2550 { "capacity", S_IFREG|S_IRUGO, proc_ide_read_capacity, NULL },
2551 { "name", S_IFREG|S_IRUGO, proc_idetape_read_name, NULL },
2552 { NULL, 0, NULL, NULL }
2556 static int ide_tape_probe(ide_drive_t *);
2558 static ide_driver_t idetape_driver = {
2560 .owner = THIS_MODULE,
2562 .bus = &ide_bus_type,
2564 .probe = ide_tape_probe,
2565 .remove = ide_tape_remove,
2566 .version = IDETAPE_VERSION,
2568 .do_request = idetape_do_request,
2569 .end_request = idetape_end_request,
2570 .error = __ide_error,
2571 #ifdef CONFIG_IDE_PROC_FS
2572 .proc = idetape_proc,
2573 .settings = idetape_settings,
2577 /* Our character device supporting functions, passed to register_chrdev. */
2578 static const struct file_operations idetape_fops = {
2579 .owner = THIS_MODULE,
2580 .read = idetape_chrdev_read,
2581 .write = idetape_chrdev_write,
2582 .ioctl = idetape_chrdev_ioctl,
2583 .open = idetape_chrdev_open,
2584 .release = idetape_chrdev_release,
2587 static int idetape_open(struct inode *inode, struct file *filp)
2589 struct gendisk *disk = inode->i_bdev->bd_disk;
2590 struct ide_tape_obj *tape;
2592 tape = ide_tape_get(disk);
2599 static int idetape_release(struct inode *inode, struct file *filp)
2601 struct gendisk *disk = inode->i_bdev->bd_disk;
2602 struct ide_tape_obj *tape = ide_tape_g(disk);
2609 static int idetape_ioctl(struct inode *inode, struct file *file,
2610 unsigned int cmd, unsigned long arg)
2612 struct block_device *bdev = inode->i_bdev;
2613 struct ide_tape_obj *tape = ide_tape_g(bdev->bd_disk);
2614 ide_drive_t *drive = tape->drive;
2615 int err = generic_ide_ioctl(drive, file, bdev, cmd, arg);
2617 err = idetape_blkdev_ioctl(drive, cmd, arg);
2621 static struct block_device_operations idetape_block_ops = {
2622 .owner = THIS_MODULE,
2623 .open = idetape_open,
2624 .release = idetape_release,
2625 .ioctl = idetape_ioctl,
2628 static int ide_tape_probe(ide_drive_t *drive)
2630 idetape_tape_t *tape;
2634 if (!strstr("ide-tape", drive->driver_req))
2637 if (drive->media != ide_tape)
2640 if (drive->id_read == 1 && !ide_check_atapi_device(drive, DRV_NAME)) {
2641 printk(KERN_ERR "ide-tape: %s: not supported by this version of"
2642 " the driver\n", drive->name);
2645 tape = kzalloc(sizeof(idetape_tape_t), GFP_KERNEL);
2647 printk(KERN_ERR "ide-tape: %s: Can't allocate a tape struct\n",
2652 g = alloc_disk(1 << PARTN_BITS);
2656 ide_init_disk(g, drive);
2658 kref_init(&tape->kref);
2660 tape->drive = drive;
2661 tape->driver = &idetape_driver;
2664 g->private_data = &tape->driver;
2666 drive->driver_data = tape;
2668 mutex_lock(&idetape_ref_mutex);
2669 for (minor = 0; idetape_devs[minor]; minor++)
2671 idetape_devs[minor] = tape;
2672 mutex_unlock(&idetape_ref_mutex);
2674 idetape_setup(drive, tape, minor);
2676 device_create_drvdata(idetape_sysfs_class, &drive->gendev,
2677 MKDEV(IDETAPE_MAJOR, minor), NULL,
2679 device_create_drvdata(idetape_sysfs_class, &drive->gendev,
2680 MKDEV(IDETAPE_MAJOR, minor + 128), NULL,
2683 g->fops = &idetape_block_ops;
2684 ide_register_region(g);
2694 static void __exit idetape_exit(void)
2696 driver_unregister(&idetape_driver.gen_driver);
2697 class_destroy(idetape_sysfs_class);
2698 unregister_chrdev(IDETAPE_MAJOR, "ht");
2701 static int __init idetape_init(void)
2704 idetape_sysfs_class = class_create(THIS_MODULE, "ide_tape");
2705 if (IS_ERR(idetape_sysfs_class)) {
2706 idetape_sysfs_class = NULL;
2707 printk(KERN_ERR "Unable to create sysfs class for ide tapes\n");
2712 if (register_chrdev(IDETAPE_MAJOR, "ht", &idetape_fops)) {
2713 printk(KERN_ERR "ide-tape: Failed to register chrdev"
2716 goto out_free_class;
2719 error = driver_register(&idetape_driver.gen_driver);
2721 goto out_free_driver;
2726 driver_unregister(&idetape_driver.gen_driver);
2728 class_destroy(idetape_sysfs_class);
2733 MODULE_ALIAS("ide:*m-tape*");
2734 module_init(idetape_init);
2735 module_exit(idetape_exit);
2736 MODULE_ALIAS_CHARDEV_MAJOR(IDETAPE_MAJOR);
2737 MODULE_DESCRIPTION("ATAPI Streaming TAPE Driver");
2738 MODULE_LICENSE("GPL");