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;
692 static void idetape_init_rq(struct request *rq, u8 cmd)
694 blk_rq_init(NULL, rq);
695 rq->cmd_type = REQ_TYPE_SPECIAL;
700 * Generate a new packet command request in front of the request queue, before
701 * the current request, so that it will be processed immediately, on the next
702 * pass through the driver. The function below is called from the request
703 * handling part of the driver (the "bottom" part). Safe storage for the request
704 * should be allocated with ide_tape_next_{pc,rq}_storage() prior to that.
706 * Memory for those requests is pre-allocated at initialization time, and is
707 * limited to IDETAPE_PC_STACK requests. We assume that we have enough space for
708 * the maximum possible number of inter-dependent packet commands.
710 * The higher level of the driver - The ioctl handler and the character device
711 * handling functions should queue request to the lower level part and wait for
712 * their completion using idetape_queue_pc_tail or idetape_queue_rw_tail.
714 static void idetape_queue_pc_head(ide_drive_t *drive, struct ide_atapi_pc *pc,
717 struct ide_tape_obj *tape = drive->driver_data;
719 idetape_init_rq(rq, REQ_IDETAPE_PC1);
720 rq->cmd_flags |= REQ_PREEMPT;
721 rq->buffer = (char *) pc;
722 rq->rq_disk = tape->disk;
723 memcpy(rq->cmd, pc->c, 12);
724 ide_do_drive_cmd(drive, rq);
728 * idetape_retry_pc is called when an error was detected during the
729 * last packet command. We queue a request sense packet command in
730 * the head of the request list.
732 static void idetape_retry_pc(ide_drive_t *drive)
734 struct ide_atapi_pc *pc;
737 (void)ide_read_error(drive);
738 pc = idetape_next_pc_storage(drive);
739 rq = idetape_next_rq_storage(drive);
740 idetape_create_request_sense_cmd(pc);
741 set_bit(IDE_AFLAG_IGNORE_DSC, &drive->atapi_flags);
742 idetape_queue_pc_head(drive, pc, rq);
746 * Postpone the current request so that ide.c will be able to service requests
747 * from another device on the same hwgroup while we are polling for DSC.
749 static void idetape_postpone_request(ide_drive_t *drive)
751 idetape_tape_t *tape = drive->driver_data;
753 debug_log(DBG_PROCS, "Enter %s\n", __func__);
755 tape->postponed_rq = HWGROUP(drive)->rq;
756 ide_stall_queue(drive, tape->dsc_poll_freq);
759 static void ide_tape_handle_dsc(ide_drive_t *drive)
761 idetape_tape_t *tape = drive->driver_data;
763 /* Media access command */
764 tape->dsc_polling_start = jiffies;
765 tape->dsc_poll_freq = IDETAPE_DSC_MA_FAST;
766 tape->dsc_timeout = jiffies + IDETAPE_DSC_MA_TIMEOUT;
767 /* Allow ide.c to handle other requests */
768 idetape_postpone_request(drive);
771 static void ide_tape_io_buffers(ide_drive_t *drive, struct ide_atapi_pc *pc,
772 unsigned int bcount, int write)
775 idetape_output_buffers(drive, pc, bcount);
777 idetape_input_buffers(drive, pc, bcount);
781 * This is the usual interrupt handler which will be called during a packet
782 * command. We will transfer some of the data (as requested by the drive) and
783 * will re-point interrupt handler to us. When data transfer is finished, we
784 * will act according to the algorithm described before
787 static ide_startstop_t idetape_pc_intr(ide_drive_t *drive)
789 idetape_tape_t *tape = drive->driver_data;
791 return ide_pc_intr(drive, tape->pc, idetape_pc_intr, IDETAPE_WAIT_CMD,
792 NULL, idetape_update_buffers, idetape_retry_pc,
793 ide_tape_handle_dsc, ide_tape_io_buffers);
797 * Packet Command Interface
799 * The current Packet Command is available in tape->pc, and will not change
800 * until we finish handling it. Each packet command is associated with a
801 * callback function that will be called when the command is finished.
803 * The handling will be done in three stages:
805 * 1. idetape_issue_pc will send the packet command to the drive, and will set
806 * the interrupt handler to idetape_pc_intr.
808 * 2. On each interrupt, idetape_pc_intr will be called. This step will be
809 * repeated until the device signals us that no more interrupts will be issued.
811 * 3. ATAPI Tape media access commands have immediate status with a delayed
812 * process. In case of a successful initiation of a media access packet command,
813 * the DSC bit will be set when the actual execution of the command is finished.
814 * Since the tape drive will not issue an interrupt, we have to poll for this
815 * event. In this case, we define the request as "low priority request" by
816 * setting rq_status to IDETAPE_RQ_POSTPONED, set a timer to poll for DSC and
819 * ide.c will then give higher priority to requests which originate from the
820 * other device, until will change rq_status to RQ_ACTIVE.
822 * 4. When the packet command is finished, it will be checked for errors.
824 * 5. In case an error was found, we queue a request sense packet command in
825 * front of the request queue and retry the operation up to
826 * IDETAPE_MAX_PC_RETRIES times.
828 * 6. In case no error was found, or we decided to give up and not to retry
829 * again, the callback function will be called and then we will handle the next
832 static ide_startstop_t idetape_transfer_pc(ide_drive_t *drive)
834 idetape_tape_t *tape = drive->driver_data;
836 return ide_transfer_pc(drive, tape->pc, idetape_pc_intr,
837 IDETAPE_WAIT_CMD, NULL);
840 static ide_startstop_t idetape_issue_pc(ide_drive_t *drive,
841 struct ide_atapi_pc *pc)
843 idetape_tape_t *tape = drive->driver_data;
845 if (tape->pc->c[0] == REQUEST_SENSE &&
846 pc->c[0] == REQUEST_SENSE) {
847 printk(KERN_ERR "ide-tape: possible ide-tape.c bug - "
848 "Two request sense in serial were issued\n");
851 if (tape->failed_pc == NULL && pc->c[0] != REQUEST_SENSE)
852 tape->failed_pc = pc;
853 /* Set the current packet command */
856 if (pc->retries > IDETAPE_MAX_PC_RETRIES ||
857 (pc->flags & PC_FLAG_ABORT)) {
859 * We will "abort" retrying a packet command in case legitimate
860 * error code was received (crossing a filemark, or end of the
861 * media, for example).
863 if (!(pc->flags & PC_FLAG_ABORT)) {
864 if (!(pc->c[0] == TEST_UNIT_READY &&
865 tape->sense_key == 2 && tape->asc == 4 &&
866 (tape->ascq == 1 || tape->ascq == 8))) {
867 printk(KERN_ERR "ide-tape: %s: I/O error, "
868 "pc = %2x, key = %2x, "
869 "asc = %2x, ascq = %2x\n",
870 tape->name, pc->c[0],
871 tape->sense_key, tape->asc,
875 pc->error = IDETAPE_ERROR_GENERAL;
877 tape->failed_pc = NULL;
878 drive->pc_callback(drive);
881 debug_log(DBG_SENSE, "Retry #%d, cmd = %02X\n", pc->retries, pc->c[0]);
885 return ide_issue_pc(drive, pc, idetape_transfer_pc,
886 IDETAPE_WAIT_CMD, NULL);
889 /* A mode sense command is used to "sense" tape parameters. */
890 static void idetape_create_mode_sense_cmd(struct ide_atapi_pc *pc, u8 page_code)
893 pc->c[0] = MODE_SENSE;
894 if (page_code != IDETAPE_BLOCK_DESCRIPTOR)
895 /* DBD = 1 - Don't return block descriptors */
897 pc->c[2] = page_code;
899 * Changed pc->c[3] to 0 (255 will at best return unused info).
901 * For SCSI this byte is defined as subpage instead of high byte
902 * of length and some IDE drives seem to interpret it this way
903 * and return an error when 255 is used.
906 /* We will just discard data in that case */
908 if (page_code == IDETAPE_BLOCK_DESCRIPTOR)
910 else if (page_code == IDETAPE_CAPABILITIES_PAGE)
916 static ide_startstop_t idetape_media_access_finished(ide_drive_t *drive)
918 ide_hwif_t *hwif = drive->hwif;
919 idetape_tape_t *tape = drive->driver_data;
920 struct ide_atapi_pc *pc = tape->pc;
923 stat = hwif->tp_ops->read_status(hwif);
925 if (stat & ATA_DSC) {
926 if (stat & ATA_ERR) {
928 if (pc->c[0] != TEST_UNIT_READY)
929 printk(KERN_ERR "ide-tape: %s: I/O error, ",
931 /* Retry operation */
932 idetape_retry_pc(drive);
937 pc->error = IDETAPE_ERROR_GENERAL;
938 tape->failed_pc = NULL;
940 drive->pc_callback(drive);
944 static void ide_tape_create_rw_cmd(idetape_tape_t *tape,
945 struct ide_atapi_pc *pc, struct request *rq,
948 struct idetape_bh *bh = (struct idetape_bh *)rq->special;
949 unsigned int length = rq->current_nr_sectors;
952 put_unaligned(cpu_to_be32(length), (unsigned int *) &pc->c[1]);
956 pc->buf_size = length * tape->blk_size;
957 pc->req_xfer = pc->buf_size;
958 if (pc->req_xfer == tape->buffer_size)
959 pc->flags |= PC_FLAG_DMA_OK;
961 if (opcode == READ_6) {
963 atomic_set(&bh->b_count, 0);
964 } else if (opcode == WRITE_6) {
966 pc->flags |= PC_FLAG_WRITING;
967 pc->b_data = bh->b_data;
968 pc->b_count = atomic_read(&bh->b_count);
971 memcpy(rq->cmd, pc->c, 12);
974 static ide_startstop_t idetape_do_request(ide_drive_t *drive,
975 struct request *rq, sector_t block)
977 ide_hwif_t *hwif = drive->hwif;
978 idetape_tape_t *tape = drive->driver_data;
979 struct ide_atapi_pc *pc = NULL;
980 struct request *postponed_rq = tape->postponed_rq;
983 debug_log(DBG_SENSE, "sector: %llu, nr_sectors: %lu,"
984 " current_nr_sectors: %u\n",
985 (unsigned long long)rq->sector, rq->nr_sectors,
986 rq->current_nr_sectors);
988 if (!blk_special_request(rq)) {
989 /* We do not support buffer cache originated requests. */
990 printk(KERN_NOTICE "ide-tape: %s: Unsupported request in "
991 "request queue (%d)\n", drive->name, rq->cmd_type);
992 ide_end_request(drive, 0, 0);
996 /* Retry a failed packet command */
997 if (tape->failed_pc && tape->pc->c[0] == REQUEST_SENSE) {
998 pc = tape->failed_pc;
1002 if (postponed_rq != NULL)
1003 if (rq != postponed_rq) {
1004 printk(KERN_ERR "ide-tape: ide-tape.c bug - "
1005 "Two DSC requests were queued\n");
1006 idetape_end_request(drive, 0, 0);
1010 tape->postponed_rq = NULL;
1013 * If the tape is still busy, postpone our request and service
1014 * the other device meanwhile.
1016 stat = hwif->tp_ops->read_status(hwif);
1018 if (!drive->dsc_overlap && !(rq->cmd[13] & REQ_IDETAPE_PC2))
1019 set_bit(IDE_AFLAG_IGNORE_DSC, &drive->atapi_flags);
1021 if (drive->post_reset == 1) {
1022 set_bit(IDE_AFLAG_IGNORE_DSC, &drive->atapi_flags);
1023 drive->post_reset = 0;
1026 if (!test_and_clear_bit(IDE_AFLAG_IGNORE_DSC, &drive->atapi_flags) &&
1027 (stat & ATA_DSC) == 0) {
1028 if (postponed_rq == NULL) {
1029 tape->dsc_polling_start = jiffies;
1030 tape->dsc_poll_freq = tape->best_dsc_rw_freq;
1031 tape->dsc_timeout = jiffies + IDETAPE_DSC_RW_TIMEOUT;
1032 } else if (time_after(jiffies, tape->dsc_timeout)) {
1033 printk(KERN_ERR "ide-tape: %s: DSC timeout\n",
1035 if (rq->cmd[13] & REQ_IDETAPE_PC2) {
1036 idetape_media_access_finished(drive);
1039 return ide_do_reset(drive);
1041 } else if (time_after(jiffies,
1042 tape->dsc_polling_start +
1043 IDETAPE_DSC_MA_THRESHOLD))
1044 tape->dsc_poll_freq = IDETAPE_DSC_MA_SLOW;
1045 idetape_postpone_request(drive);
1048 if (rq->cmd[13] & REQ_IDETAPE_READ) {
1049 pc = idetape_next_pc_storage(drive);
1050 ide_tape_create_rw_cmd(tape, pc, rq, READ_6);
1053 if (rq->cmd[13] & REQ_IDETAPE_WRITE) {
1054 pc = idetape_next_pc_storage(drive);
1055 ide_tape_create_rw_cmd(tape, pc, rq, WRITE_6);
1058 if (rq->cmd[13] & REQ_IDETAPE_PC1) {
1059 pc = (struct ide_atapi_pc *) rq->buffer;
1060 rq->cmd[13] &= ~(REQ_IDETAPE_PC1);
1061 rq->cmd[13] |= REQ_IDETAPE_PC2;
1064 if (rq->cmd[13] & REQ_IDETAPE_PC2) {
1065 idetape_media_access_finished(drive);
1071 return idetape_issue_pc(drive, pc);
1075 * The function below uses __get_free_pages to allocate a data buffer of size
1076 * tape->buffer_size (or a bit more). We attempt to combine sequential pages as
1079 * It returns a pointer to the newly allocated buffer, or NULL in case of
1082 static struct idetape_bh *ide_tape_kmalloc_buffer(idetape_tape_t *tape,
1083 int full, int clear)
1085 struct idetape_bh *prev_bh, *bh, *merge_bh;
1086 int pages = tape->pages_per_buffer;
1087 unsigned int order, b_allocd;
1088 char *b_data = NULL;
1090 merge_bh = kmalloc(sizeof(struct idetape_bh), GFP_KERNEL);
1095 order = fls(pages) - 1;
1096 bh->b_data = (char *) __get_free_pages(GFP_KERNEL, order);
1099 b_allocd = (1 << order) * PAGE_SIZE;
1103 memset(bh->b_data, 0, b_allocd);
1104 bh->b_reqnext = NULL;
1105 bh->b_size = b_allocd;
1106 atomic_set(&bh->b_count, full ? bh->b_size : 0);
1109 order = fls(pages) - 1;
1110 b_data = (char *) __get_free_pages(GFP_KERNEL, order);
1113 b_allocd = (1 << order) * PAGE_SIZE;
1116 memset(b_data, 0, b_allocd);
1118 /* newly allocated page frames below buffer header or ...*/
1119 if (bh->b_data == b_data + b_allocd) {
1120 bh->b_size += b_allocd;
1121 bh->b_data -= b_allocd;
1123 atomic_add(b_allocd, &bh->b_count);
1126 /* they are above the header */
1127 if (b_data == bh->b_data + bh->b_size) {
1128 bh->b_size += b_allocd;
1130 atomic_add(b_allocd, &bh->b_count);
1134 bh = kmalloc(sizeof(struct idetape_bh), GFP_KERNEL);
1136 free_pages((unsigned long) b_data, order);
1139 bh->b_reqnext = NULL;
1140 bh->b_data = b_data;
1141 bh->b_size = b_allocd;
1142 atomic_set(&bh->b_count, full ? bh->b_size : 0);
1143 prev_bh->b_reqnext = bh;
1148 bh->b_size -= tape->excess_bh_size;
1150 atomic_sub(tape->excess_bh_size, &bh->b_count);
1153 ide_tape_kfree_buffer(tape);
1157 static int idetape_copy_stage_from_user(idetape_tape_t *tape,
1158 const char __user *buf, int n)
1160 struct idetape_bh *bh = tape->bh;
1166 printk(KERN_ERR "ide-tape: bh == NULL in %s\n",
1170 count = min((unsigned int)
1171 (bh->b_size - atomic_read(&bh->b_count)),
1173 if (copy_from_user(bh->b_data + atomic_read(&bh->b_count), buf,
1177 atomic_add(count, &bh->b_count);
1179 if (atomic_read(&bh->b_count) == bh->b_size) {
1182 atomic_set(&bh->b_count, 0);
1189 static int idetape_copy_stage_to_user(idetape_tape_t *tape, char __user *buf,
1192 struct idetape_bh *bh = tape->bh;
1198 printk(KERN_ERR "ide-tape: bh == NULL in %s\n",
1202 count = min(tape->b_count, n);
1203 if (copy_to_user(buf, tape->b_data, count))
1206 tape->b_data += count;
1207 tape->b_count -= count;
1209 if (!tape->b_count) {
1213 tape->b_data = bh->b_data;
1214 tape->b_count = atomic_read(&bh->b_count);
1221 static void idetape_init_merge_buffer(idetape_tape_t *tape)
1223 struct idetape_bh *bh = tape->merge_bh;
1224 tape->bh = tape->merge_bh;
1226 if (tape->chrdev_dir == IDETAPE_DIR_WRITE)
1227 atomic_set(&bh->b_count, 0);
1229 tape->b_data = bh->b_data;
1230 tape->b_count = atomic_read(&bh->b_count);
1235 * Write a filemark if write_filemark=1. Flush the device buffers without
1236 * writing a filemark otherwise.
1238 static void idetape_create_write_filemark_cmd(ide_drive_t *drive,
1239 struct ide_atapi_pc *pc, int write_filemark)
1241 idetape_init_pc(pc);
1242 pc->c[0] = WRITE_FILEMARKS;
1243 pc->c[4] = write_filemark;
1244 pc->flags |= PC_FLAG_WAIT_FOR_DSC;
1247 static void idetape_create_test_unit_ready_cmd(struct ide_atapi_pc *pc)
1249 idetape_init_pc(pc);
1250 pc->c[0] = TEST_UNIT_READY;
1254 * We add a special packet command request to the tail of the request queue, and
1255 * wait for it to be serviced. This is not to be called from within the request
1256 * handling part of the driver! We allocate here data on the stack and it is
1257 * valid until the request is finished. This is not the case for the bottom part
1258 * of the driver, where we are always leaving the functions to wait for an
1259 * interrupt or a timer event.
1261 * From the bottom part of the driver, we should allocate safe memory using
1262 * idetape_next_pc_storage() and ide_tape_next_rq_storage(), and add the request
1263 * to the request list without waiting for it to be serviced! In that case, we
1264 * usually use idetape_queue_pc_head().
1266 static int idetape_queue_pc_tail(ide_drive_t *drive, struct ide_atapi_pc *pc)
1268 struct ide_tape_obj *tape = drive->driver_data;
1272 rq = blk_get_request(drive->queue, READ, __GFP_WAIT);
1273 rq->cmd_type = REQ_TYPE_SPECIAL;
1274 rq->cmd[13] = REQ_IDETAPE_PC1;
1275 rq->buffer = (char *)pc;
1276 memcpy(rq->cmd, pc->c, 12);
1277 error = blk_execute_rq(drive->queue, tape->disk, rq, 0);
1278 blk_put_request(rq);
1282 static void idetape_create_load_unload_cmd(ide_drive_t *drive,
1283 struct ide_atapi_pc *pc, int cmd)
1285 idetape_init_pc(pc);
1286 pc->c[0] = START_STOP;
1288 pc->flags |= PC_FLAG_WAIT_FOR_DSC;
1291 static int idetape_wait_ready(ide_drive_t *drive, unsigned long timeout)
1293 idetape_tape_t *tape = drive->driver_data;
1294 struct ide_atapi_pc pc;
1295 int load_attempted = 0;
1297 /* Wait for the tape to become ready */
1298 set_bit(IDE_AFLAG_MEDIUM_PRESENT, &drive->atapi_flags);
1300 while (time_before(jiffies, timeout)) {
1301 idetape_create_test_unit_ready_cmd(&pc);
1302 if (!idetape_queue_pc_tail(drive, &pc))
1304 if ((tape->sense_key == 2 && tape->asc == 4 && tape->ascq == 2)
1305 || (tape->asc == 0x3A)) {
1309 idetape_create_load_unload_cmd(drive, &pc,
1310 IDETAPE_LU_LOAD_MASK);
1311 idetape_queue_pc_tail(drive, &pc);
1313 /* not about to be ready */
1314 } else if (!(tape->sense_key == 2 && tape->asc == 4 &&
1315 (tape->ascq == 1 || tape->ascq == 8)))
1322 static int idetape_flush_tape_buffers(ide_drive_t *drive)
1324 struct ide_atapi_pc pc;
1327 idetape_create_write_filemark_cmd(drive, &pc, 0);
1328 rc = idetape_queue_pc_tail(drive, &pc);
1331 idetape_wait_ready(drive, 60 * 5 * HZ);
1335 static void idetape_create_read_position_cmd(struct ide_atapi_pc *pc)
1337 idetape_init_pc(pc);
1338 pc->c[0] = READ_POSITION;
1342 static int idetape_read_position(ide_drive_t *drive)
1344 idetape_tape_t *tape = drive->driver_data;
1345 struct ide_atapi_pc pc;
1348 debug_log(DBG_PROCS, "Enter %s\n", __func__);
1350 idetape_create_read_position_cmd(&pc);
1351 if (idetape_queue_pc_tail(drive, &pc))
1353 position = tape->first_frame;
1357 static void idetape_create_locate_cmd(ide_drive_t *drive,
1358 struct ide_atapi_pc *pc,
1359 unsigned int block, u8 partition, int skip)
1361 idetape_init_pc(pc);
1362 pc->c[0] = POSITION_TO_ELEMENT;
1364 put_unaligned(cpu_to_be32(block), (unsigned int *) &pc->c[3]);
1365 pc->c[8] = partition;
1366 pc->flags |= PC_FLAG_WAIT_FOR_DSC;
1369 static int idetape_create_prevent_cmd(ide_drive_t *drive,
1370 struct ide_atapi_pc *pc, int prevent)
1372 idetape_tape_t *tape = drive->driver_data;
1374 /* device supports locking according to capabilities page */
1375 if (!(tape->caps[6] & 0x01))
1378 idetape_init_pc(pc);
1379 pc->c[0] = ALLOW_MEDIUM_REMOVAL;
1384 static void __ide_tape_discard_merge_buffer(ide_drive_t *drive)
1386 idetape_tape_t *tape = drive->driver_data;
1388 if (tape->chrdev_dir != IDETAPE_DIR_READ)
1391 clear_bit(IDE_AFLAG_FILEMARK, &drive->atapi_flags);
1392 tape->merge_bh_size = 0;
1393 if (tape->merge_bh != NULL) {
1394 ide_tape_kfree_buffer(tape);
1395 tape->merge_bh = NULL;
1398 tape->chrdev_dir = IDETAPE_DIR_NONE;
1402 * Position the tape to the requested block using the LOCATE packet command.
1403 * A READ POSITION command is then issued to check where we are positioned. Like
1404 * all higher level operations, we queue the commands at the tail of the request
1405 * queue and wait for their completion.
1407 static int idetape_position_tape(ide_drive_t *drive, unsigned int block,
1408 u8 partition, int skip)
1410 idetape_tape_t *tape = drive->driver_data;
1412 struct ide_atapi_pc pc;
1414 if (tape->chrdev_dir == IDETAPE_DIR_READ)
1415 __ide_tape_discard_merge_buffer(drive);
1416 idetape_wait_ready(drive, 60 * 5 * HZ);
1417 idetape_create_locate_cmd(drive, &pc, block, partition, skip);
1418 retval = idetape_queue_pc_tail(drive, &pc);
1422 idetape_create_read_position_cmd(&pc);
1423 return (idetape_queue_pc_tail(drive, &pc));
1426 static void ide_tape_discard_merge_buffer(ide_drive_t *drive,
1427 int restore_position)
1429 idetape_tape_t *tape = drive->driver_data;
1432 __ide_tape_discard_merge_buffer(drive);
1433 if (restore_position) {
1434 position = idetape_read_position(drive);
1435 seek = position > 0 ? position : 0;
1436 if (idetape_position_tape(drive, seek, 0, 0)) {
1437 printk(KERN_INFO "ide-tape: %s: position_tape failed in"
1438 " %s\n", tape->name, __func__);
1445 * Generate a read/write request for the block device interface and wait for it
1448 static int idetape_queue_rw_tail(ide_drive_t *drive, int cmd, int blocks,
1449 struct idetape_bh *bh)
1451 idetape_tape_t *tape = drive->driver_data;
1455 debug_log(DBG_SENSE, "%s: cmd=%d\n", __func__, cmd);
1457 rq = blk_get_request(drive->queue, READ, __GFP_WAIT);
1458 rq->cmd_type = REQ_TYPE_SPECIAL;
1460 rq->rq_disk = tape->disk;
1461 rq->special = (void *)bh;
1462 rq->sector = tape->first_frame;
1463 rq->nr_sectors = blocks;
1464 rq->current_nr_sectors = blocks;
1465 blk_execute_rq(drive->queue, tape->disk, rq, 0);
1467 errors = rq->errors;
1468 ret = tape->blk_size * (blocks - rq->current_nr_sectors);
1469 blk_put_request(rq);
1471 if ((cmd & (REQ_IDETAPE_READ | REQ_IDETAPE_WRITE)) == 0)
1475 idetape_init_merge_buffer(tape);
1476 if (errors == IDETAPE_ERROR_GENERAL)
1481 static void idetape_create_inquiry_cmd(struct ide_atapi_pc *pc)
1483 idetape_init_pc(pc);
1489 static void idetape_create_rewind_cmd(ide_drive_t *drive,
1490 struct ide_atapi_pc *pc)
1492 idetape_init_pc(pc);
1493 pc->c[0] = REZERO_UNIT;
1494 pc->flags |= PC_FLAG_WAIT_FOR_DSC;
1497 static void idetape_create_erase_cmd(struct ide_atapi_pc *pc)
1499 idetape_init_pc(pc);
1502 pc->flags |= PC_FLAG_WAIT_FOR_DSC;
1505 static void idetape_create_space_cmd(struct ide_atapi_pc *pc, int count, u8 cmd)
1507 idetape_init_pc(pc);
1509 put_unaligned(cpu_to_be32(count), (unsigned int *) &pc->c[1]);
1511 pc->flags |= PC_FLAG_WAIT_FOR_DSC;
1514 /* Queue up a character device originated write request. */
1515 static int idetape_add_chrdev_write_request(ide_drive_t *drive, int blocks)
1517 idetape_tape_t *tape = drive->driver_data;
1519 debug_log(DBG_CHRDEV, "Enter %s\n", __func__);
1521 return idetape_queue_rw_tail(drive, REQ_IDETAPE_WRITE,
1522 blocks, tape->merge_bh);
1525 static void ide_tape_flush_merge_buffer(ide_drive_t *drive)
1527 idetape_tape_t *tape = drive->driver_data;
1529 struct idetape_bh *bh;
1531 if (tape->chrdev_dir != IDETAPE_DIR_WRITE) {
1532 printk(KERN_ERR "ide-tape: bug: Trying to empty merge buffer"
1533 " but we are not writing.\n");
1536 if (tape->merge_bh_size > tape->buffer_size) {
1537 printk(KERN_ERR "ide-tape: bug: merge_buffer too big\n");
1538 tape->merge_bh_size = tape->buffer_size;
1540 if (tape->merge_bh_size) {
1541 blocks = tape->merge_bh_size / tape->blk_size;
1542 if (tape->merge_bh_size % tape->blk_size) {
1546 i = tape->blk_size - tape->merge_bh_size %
1548 bh = tape->bh->b_reqnext;
1550 atomic_set(&bh->b_count, 0);
1556 printk(KERN_INFO "ide-tape: bug,"
1560 min = min(i, (unsigned int)(bh->b_size -
1561 atomic_read(&bh->b_count)));
1562 memset(bh->b_data + atomic_read(&bh->b_count),
1564 atomic_add(min, &bh->b_count);
1569 (void) idetape_add_chrdev_write_request(drive, blocks);
1570 tape->merge_bh_size = 0;
1572 if (tape->merge_bh != NULL) {
1573 ide_tape_kfree_buffer(tape);
1574 tape->merge_bh = NULL;
1576 tape->chrdev_dir = IDETAPE_DIR_NONE;
1579 static int idetape_init_read(ide_drive_t *drive)
1581 idetape_tape_t *tape = drive->driver_data;
1584 /* Initialize read operation */
1585 if (tape->chrdev_dir != IDETAPE_DIR_READ) {
1586 if (tape->chrdev_dir == IDETAPE_DIR_WRITE) {
1587 ide_tape_flush_merge_buffer(drive);
1588 idetape_flush_tape_buffers(drive);
1590 if (tape->merge_bh || tape->merge_bh_size) {
1591 printk(KERN_ERR "ide-tape: merge_bh_size should be"
1593 tape->merge_bh_size = 0;
1595 tape->merge_bh = ide_tape_kmalloc_buffer(tape, 0, 0);
1596 if (!tape->merge_bh)
1598 tape->chrdev_dir = IDETAPE_DIR_READ;
1601 * Issue a read 0 command to ensure that DSC handshake is
1602 * switched from completion mode to buffer available mode.
1603 * No point in issuing this if DSC overlap isn't supported, some
1604 * drives (Seagate STT3401A) will return an error.
1606 if (drive->dsc_overlap) {
1607 bytes_read = idetape_queue_rw_tail(drive,
1608 REQ_IDETAPE_READ, 0,
1610 if (bytes_read < 0) {
1611 ide_tape_kfree_buffer(tape);
1612 tape->merge_bh = NULL;
1613 tape->chrdev_dir = IDETAPE_DIR_NONE;
1622 /* called from idetape_chrdev_read() to service a chrdev read request. */
1623 static int idetape_add_chrdev_read_request(ide_drive_t *drive, int blocks)
1625 idetape_tape_t *tape = drive->driver_data;
1627 debug_log(DBG_PROCS, "Enter %s, %d blocks\n", __func__, blocks);
1629 /* If we are at a filemark, return a read length of 0 */
1630 if (test_bit(IDE_AFLAG_FILEMARK, &drive->atapi_flags))
1633 idetape_init_read(drive);
1635 return idetape_queue_rw_tail(drive, REQ_IDETAPE_READ, blocks,
1639 static void idetape_pad_zeros(ide_drive_t *drive, int bcount)
1641 idetape_tape_t *tape = drive->driver_data;
1642 struct idetape_bh *bh;
1648 bh = tape->merge_bh;
1649 count = min(tape->buffer_size, bcount);
1651 blocks = count / tape->blk_size;
1653 atomic_set(&bh->b_count,
1654 min(count, (unsigned int)bh->b_size));
1655 memset(bh->b_data, 0, atomic_read(&bh->b_count));
1656 count -= atomic_read(&bh->b_count);
1659 idetape_queue_rw_tail(drive, REQ_IDETAPE_WRITE, blocks,
1665 * Rewinds the tape to the Beginning Of the current Partition (BOP). We
1666 * currently support only one partition.
1668 static int idetape_rewind_tape(ide_drive_t *drive)
1671 struct ide_atapi_pc pc;
1672 idetape_tape_t *tape;
1673 tape = drive->driver_data;
1675 debug_log(DBG_SENSE, "Enter %s\n", __func__);
1677 idetape_create_rewind_cmd(drive, &pc);
1678 retval = idetape_queue_pc_tail(drive, &pc);
1682 idetape_create_read_position_cmd(&pc);
1683 retval = idetape_queue_pc_tail(drive, &pc);
1689 /* mtio.h compatible commands should be issued to the chrdev interface. */
1690 static int idetape_blkdev_ioctl(ide_drive_t *drive, unsigned int cmd,
1693 idetape_tape_t *tape = drive->driver_data;
1694 void __user *argp = (void __user *)arg;
1696 struct idetape_config {
1697 int dsc_rw_frequency;
1698 int dsc_media_access_frequency;
1702 debug_log(DBG_PROCS, "Enter %s\n", __func__);
1706 if (copy_from_user(&config, argp, sizeof(config)))
1708 tape->best_dsc_rw_freq = config.dsc_rw_frequency;
1711 config.dsc_rw_frequency = (int) tape->best_dsc_rw_freq;
1712 config.nr_stages = 1;
1713 if (copy_to_user(argp, &config, sizeof(config)))
1722 static int idetape_space_over_filemarks(ide_drive_t *drive, short mt_op,
1725 idetape_tape_t *tape = drive->driver_data;
1726 struct ide_atapi_pc pc;
1727 int retval, count = 0;
1728 int sprev = !!(tape->caps[4] & 0x20);
1732 if (MTBSF == mt_op || MTBSFM == mt_op) {
1735 mt_count = -mt_count;
1738 if (tape->chrdev_dir == IDETAPE_DIR_READ) {
1739 tape->merge_bh_size = 0;
1740 if (test_and_clear_bit(IDE_AFLAG_FILEMARK, &drive->atapi_flags))
1742 ide_tape_discard_merge_buffer(drive, 0);
1748 idetape_create_space_cmd(&pc, mt_count - count,
1749 IDETAPE_SPACE_OVER_FILEMARK);
1750 return idetape_queue_pc_tail(drive, &pc);
1755 retval = idetape_space_over_filemarks(drive, MTFSF,
1759 count = (MTBSFM == mt_op ? 1 : -1);
1760 return idetape_space_over_filemarks(drive, MTFSF, count);
1762 printk(KERN_ERR "ide-tape: MTIO operation %d not supported\n",
1769 * Our character device read / write functions.
1771 * The tape is optimized to maximize throughput when it is transferring an
1772 * integral number of the "continuous transfer limit", which is a parameter of
1773 * the specific tape (26kB on my particular tape, 32kB for Onstream).
1775 * As of version 1.3 of the driver, the character device provides an abstract
1776 * continuous view of the media - any mix of block sizes (even 1 byte) on the
1777 * same backup/restore procedure is supported. The driver will internally
1778 * convert the requests to the recommended transfer unit, so that an unmatch
1779 * between the user's block size to the recommended size will only result in a
1780 * (slightly) increased driver overhead, but will no longer hit performance.
1781 * This is not applicable to Onstream.
1783 static ssize_t idetape_chrdev_read(struct file *file, char __user *buf,
1784 size_t count, loff_t *ppos)
1786 struct ide_tape_obj *tape = ide_tape_f(file);
1787 ide_drive_t *drive = tape->drive;
1788 ssize_t bytes_read, temp, actually_read = 0, rc;
1790 u16 ctl = *(u16 *)&tape->caps[12];
1792 debug_log(DBG_CHRDEV, "Enter %s, count %Zd\n", __func__, count);
1794 if (tape->chrdev_dir != IDETAPE_DIR_READ) {
1795 if (test_bit(IDE_AFLAG_DETECT_BS, &drive->atapi_flags))
1796 if (count > tape->blk_size &&
1797 (count % tape->blk_size) == 0)
1798 tape->user_bs_factor = count / tape->blk_size;
1800 rc = idetape_init_read(drive);
1805 if (tape->merge_bh_size) {
1806 actually_read = min((unsigned int)(tape->merge_bh_size),
1807 (unsigned int)count);
1808 if (idetape_copy_stage_to_user(tape, buf, actually_read))
1810 buf += actually_read;
1811 tape->merge_bh_size -= actually_read;
1812 count -= actually_read;
1814 while (count >= tape->buffer_size) {
1815 bytes_read = idetape_add_chrdev_read_request(drive, ctl);
1816 if (bytes_read <= 0)
1818 if (idetape_copy_stage_to_user(tape, buf, bytes_read))
1821 count -= bytes_read;
1822 actually_read += bytes_read;
1825 bytes_read = idetape_add_chrdev_read_request(drive, ctl);
1826 if (bytes_read <= 0)
1828 temp = min((unsigned long)count, (unsigned long)bytes_read);
1829 if (idetape_copy_stage_to_user(tape, buf, temp))
1831 actually_read += temp;
1832 tape->merge_bh_size = bytes_read-temp;
1835 if (!actually_read && test_bit(IDE_AFLAG_FILEMARK, &drive->atapi_flags)) {
1836 debug_log(DBG_SENSE, "%s: spacing over filemark\n", tape->name);
1838 idetape_space_over_filemarks(drive, MTFSF, 1);
1842 return ret ? ret : actually_read;
1845 static ssize_t idetape_chrdev_write(struct file *file, const char __user *buf,
1846 size_t count, loff_t *ppos)
1848 struct ide_tape_obj *tape = ide_tape_f(file);
1849 ide_drive_t *drive = tape->drive;
1850 ssize_t actually_written = 0;
1852 u16 ctl = *(u16 *)&tape->caps[12];
1854 /* The drive is write protected. */
1855 if (tape->write_prot)
1858 debug_log(DBG_CHRDEV, "Enter %s, count %Zd\n", __func__, count);
1860 /* Initialize write operation */
1861 if (tape->chrdev_dir != IDETAPE_DIR_WRITE) {
1862 if (tape->chrdev_dir == IDETAPE_DIR_READ)
1863 ide_tape_discard_merge_buffer(drive, 1);
1864 if (tape->merge_bh || tape->merge_bh_size) {
1865 printk(KERN_ERR "ide-tape: merge_bh_size "
1866 "should be 0 now\n");
1867 tape->merge_bh_size = 0;
1869 tape->merge_bh = ide_tape_kmalloc_buffer(tape, 0, 0);
1870 if (!tape->merge_bh)
1872 tape->chrdev_dir = IDETAPE_DIR_WRITE;
1873 idetape_init_merge_buffer(tape);
1876 * Issue a write 0 command to ensure that DSC handshake is
1877 * switched from completion mode to buffer available mode. No
1878 * point in issuing this if DSC overlap isn't supported, some
1879 * drives (Seagate STT3401A) will return an error.
1881 if (drive->dsc_overlap) {
1882 ssize_t retval = idetape_queue_rw_tail(drive,
1883 REQ_IDETAPE_WRITE, 0,
1886 ide_tape_kfree_buffer(tape);
1887 tape->merge_bh = NULL;
1888 tape->chrdev_dir = IDETAPE_DIR_NONE;
1895 if (tape->merge_bh_size) {
1896 if (tape->merge_bh_size >= tape->buffer_size) {
1897 printk(KERN_ERR "ide-tape: bug: merge buf too big\n");
1898 tape->merge_bh_size = 0;
1900 actually_written = min((unsigned int)
1901 (tape->buffer_size - tape->merge_bh_size),
1902 (unsigned int)count);
1903 if (idetape_copy_stage_from_user(tape, buf, actually_written))
1905 buf += actually_written;
1906 tape->merge_bh_size += actually_written;
1907 count -= actually_written;
1909 if (tape->merge_bh_size == tape->buffer_size) {
1911 tape->merge_bh_size = 0;
1912 retval = idetape_add_chrdev_write_request(drive, ctl);
1917 while (count >= tape->buffer_size) {
1919 if (idetape_copy_stage_from_user(tape, buf, tape->buffer_size))
1921 buf += tape->buffer_size;
1922 count -= tape->buffer_size;
1923 retval = idetape_add_chrdev_write_request(drive, ctl);
1924 actually_written += tape->buffer_size;
1929 actually_written += count;
1930 if (idetape_copy_stage_from_user(tape, buf, count))
1932 tape->merge_bh_size += count;
1934 return ret ? ret : actually_written;
1937 static int idetape_write_filemark(ide_drive_t *drive)
1939 struct ide_atapi_pc pc;
1941 /* Write a filemark */
1942 idetape_create_write_filemark_cmd(drive, &pc, 1);
1943 if (idetape_queue_pc_tail(drive, &pc)) {
1944 printk(KERN_ERR "ide-tape: Couldn't write a filemark\n");
1951 * Called from idetape_chrdev_ioctl when the general mtio MTIOCTOP ioctl is
1954 * Note: MTBSF and MTBSFM are not supported when the tape doesn't support
1955 * spacing over filemarks in the reverse direction. In this case, MTFSFM is also
1956 * usually not supported.
1958 * The following commands are currently not supported:
1960 * MTFSS, MTBSS, MTWSM, MTSETDENSITY, MTSETDRVBUFFER, MT_ST_BOOLEANS,
1961 * MT_ST_WRITE_THRESHOLD.
1963 static int idetape_mtioctop(ide_drive_t *drive, short mt_op, int mt_count)
1965 idetape_tape_t *tape = drive->driver_data;
1966 struct ide_atapi_pc pc;
1969 debug_log(DBG_ERR, "Handling MTIOCTOP ioctl: mt_op=%d, mt_count=%d\n",
1979 return idetape_space_over_filemarks(drive, mt_op, mt_count);
1986 if (tape->write_prot)
1988 ide_tape_discard_merge_buffer(drive, 1);
1989 for (i = 0; i < mt_count; i++) {
1990 retval = idetape_write_filemark(drive);
1996 ide_tape_discard_merge_buffer(drive, 0);
1997 if (idetape_rewind_tape(drive))
2001 ide_tape_discard_merge_buffer(drive, 0);
2002 idetape_create_load_unload_cmd(drive, &pc,
2003 IDETAPE_LU_LOAD_MASK);
2004 return idetape_queue_pc_tail(drive, &pc);
2008 * If door is locked, attempt to unlock before
2009 * attempting to eject.
2011 if (tape->door_locked) {
2012 if (idetape_create_prevent_cmd(drive, &pc, 0))
2013 if (!idetape_queue_pc_tail(drive, &pc))
2014 tape->door_locked = DOOR_UNLOCKED;
2016 ide_tape_discard_merge_buffer(drive, 0);
2017 idetape_create_load_unload_cmd(drive, &pc,
2018 !IDETAPE_LU_LOAD_MASK);
2019 retval = idetape_queue_pc_tail(drive, &pc);
2021 clear_bit(IDE_AFLAG_MEDIUM_PRESENT, &drive->atapi_flags);
2024 ide_tape_discard_merge_buffer(drive, 0);
2025 return idetape_flush_tape_buffers(drive);
2027 ide_tape_discard_merge_buffer(drive, 0);
2028 idetape_create_load_unload_cmd(drive, &pc,
2029 IDETAPE_LU_RETENSION_MASK | IDETAPE_LU_LOAD_MASK);
2030 return idetape_queue_pc_tail(drive, &pc);
2032 idetape_create_space_cmd(&pc, 0, IDETAPE_SPACE_TO_EOD);
2033 return idetape_queue_pc_tail(drive, &pc);
2035 (void)idetape_rewind_tape(drive);
2036 idetape_create_erase_cmd(&pc);
2037 return idetape_queue_pc_tail(drive, &pc);
2040 if (mt_count < tape->blk_size ||
2041 mt_count % tape->blk_size)
2043 tape->user_bs_factor = mt_count / tape->blk_size;
2044 clear_bit(IDE_AFLAG_DETECT_BS, &drive->atapi_flags);
2046 set_bit(IDE_AFLAG_DETECT_BS, &drive->atapi_flags);
2049 ide_tape_discard_merge_buffer(drive, 0);
2050 return idetape_position_tape(drive,
2051 mt_count * tape->user_bs_factor, tape->partition, 0);
2053 ide_tape_discard_merge_buffer(drive, 0);
2054 return idetape_position_tape(drive, 0, mt_count, 0);
2058 if (!idetape_create_prevent_cmd(drive, &pc, 1))
2060 retval = idetape_queue_pc_tail(drive, &pc);
2063 tape->door_locked = DOOR_EXPLICITLY_LOCKED;
2066 if (!idetape_create_prevent_cmd(drive, &pc, 0))
2068 retval = idetape_queue_pc_tail(drive, &pc);
2071 tape->door_locked = DOOR_UNLOCKED;
2074 printk(KERN_ERR "ide-tape: MTIO operation %d not supported\n",
2081 * Our character device ioctls. General mtio.h magnetic io commands are
2082 * supported here, and not in the corresponding block interface. Our own
2083 * ide-tape ioctls are supported on both interfaces.
2085 static int idetape_chrdev_ioctl(struct inode *inode, struct file *file,
2086 unsigned int cmd, unsigned long arg)
2088 struct ide_tape_obj *tape = ide_tape_f(file);
2089 ide_drive_t *drive = tape->drive;
2093 int block_offset = 0, position = tape->first_frame;
2094 void __user *argp = (void __user *)arg;
2096 debug_log(DBG_CHRDEV, "Enter %s, cmd=%u\n", __func__, cmd);
2098 if (tape->chrdev_dir == IDETAPE_DIR_WRITE) {
2099 ide_tape_flush_merge_buffer(drive);
2100 idetape_flush_tape_buffers(drive);
2102 if (cmd == MTIOCGET || cmd == MTIOCPOS) {
2103 block_offset = tape->merge_bh_size /
2104 (tape->blk_size * tape->user_bs_factor);
2105 position = idetape_read_position(drive);
2111 if (copy_from_user(&mtop, argp, sizeof(struct mtop)))
2113 return idetape_mtioctop(drive, mtop.mt_op, mtop.mt_count);
2115 memset(&mtget, 0, sizeof(struct mtget));
2116 mtget.mt_type = MT_ISSCSI2;
2117 mtget.mt_blkno = position / tape->user_bs_factor - block_offset;
2119 ((tape->blk_size * tape->user_bs_factor)
2120 << MT_ST_BLKSIZE_SHIFT) & MT_ST_BLKSIZE_MASK;
2122 if (tape->drv_write_prot)
2123 mtget.mt_gstat |= GMT_WR_PROT(0xffffffff);
2125 if (copy_to_user(argp, &mtget, sizeof(struct mtget)))
2129 mtpos.mt_blkno = position / tape->user_bs_factor - block_offset;
2130 if (copy_to_user(argp, &mtpos, sizeof(struct mtpos)))
2134 if (tape->chrdev_dir == IDETAPE_DIR_READ)
2135 ide_tape_discard_merge_buffer(drive, 1);
2136 return idetape_blkdev_ioctl(drive, cmd, arg);
2141 * Do a mode sense page 0 with block descriptor and if it succeeds set the tape
2142 * block size with the reported value.
2144 static void ide_tape_get_bsize_from_bdesc(ide_drive_t *drive)
2146 idetape_tape_t *tape = drive->driver_data;
2147 struct ide_atapi_pc pc;
2149 idetape_create_mode_sense_cmd(&pc, IDETAPE_BLOCK_DESCRIPTOR);
2150 if (idetape_queue_pc_tail(drive, &pc)) {
2151 printk(KERN_ERR "ide-tape: Can't get block descriptor\n");
2152 if (tape->blk_size == 0) {
2153 printk(KERN_WARNING "ide-tape: Cannot deal with zero "
2154 "block size, assuming 32k\n");
2155 tape->blk_size = 32768;
2159 tape->blk_size = (pc.buf[4 + 5] << 16) +
2160 (pc.buf[4 + 6] << 8) +
2162 tape->drv_write_prot = (pc.buf[2] & 0x80) >> 7;
2165 static int idetape_chrdev_open(struct inode *inode, struct file *filp)
2167 unsigned int minor = iminor(inode), i = minor & ~0xc0;
2169 idetape_tape_t *tape;
2170 struct ide_atapi_pc pc;
2173 if (i >= MAX_HWIFS * MAX_DRIVES)
2177 tape = ide_tape_chrdev_get(i);
2183 debug_log(DBG_CHRDEV, "Enter %s\n", __func__);
2186 * We really want to do nonseekable_open(inode, filp); here, but some
2187 * versions of tar incorrectly call lseek on tapes and bail out if that
2188 * fails. So we disallow pread() and pwrite(), but permit lseeks.
2190 filp->f_mode &= ~(FMODE_PREAD | FMODE_PWRITE);
2192 drive = tape->drive;
2194 filp->private_data = tape;
2196 if (test_and_set_bit(IDE_AFLAG_BUSY, &drive->atapi_flags)) {
2201 retval = idetape_wait_ready(drive, 60 * HZ);
2203 clear_bit(IDE_AFLAG_BUSY, &drive->atapi_flags);
2204 printk(KERN_ERR "ide-tape: %s: drive not ready\n", tape->name);
2208 idetape_read_position(drive);
2209 if (!test_bit(IDE_AFLAG_ADDRESS_VALID, &drive->atapi_flags))
2210 (void)idetape_rewind_tape(drive);
2212 /* Read block size and write protect status from drive. */
2213 ide_tape_get_bsize_from_bdesc(drive);
2215 /* Set write protect flag if device is opened as read-only. */
2216 if ((filp->f_flags & O_ACCMODE) == O_RDONLY)
2217 tape->write_prot = 1;
2219 tape->write_prot = tape->drv_write_prot;
2221 /* Make sure drive isn't write protected if user wants to write. */
2222 if (tape->write_prot) {
2223 if ((filp->f_flags & O_ACCMODE) == O_WRONLY ||
2224 (filp->f_flags & O_ACCMODE) == O_RDWR) {
2225 clear_bit(IDE_AFLAG_BUSY, &drive->atapi_flags);
2231 /* Lock the tape drive door so user can't eject. */
2232 if (tape->chrdev_dir == IDETAPE_DIR_NONE) {
2233 if (idetape_create_prevent_cmd(drive, &pc, 1)) {
2234 if (!idetape_queue_pc_tail(drive, &pc)) {
2235 if (tape->door_locked != DOOR_EXPLICITLY_LOCKED)
2236 tape->door_locked = DOOR_LOCKED;
2249 static void idetape_write_release(ide_drive_t *drive, unsigned int minor)
2251 idetape_tape_t *tape = drive->driver_data;
2253 ide_tape_flush_merge_buffer(drive);
2254 tape->merge_bh = ide_tape_kmalloc_buffer(tape, 1, 0);
2255 if (tape->merge_bh != NULL) {
2256 idetape_pad_zeros(drive, tape->blk_size *
2257 (tape->user_bs_factor - 1));
2258 ide_tape_kfree_buffer(tape);
2259 tape->merge_bh = NULL;
2261 idetape_write_filemark(drive);
2262 idetape_flush_tape_buffers(drive);
2263 idetape_flush_tape_buffers(drive);
2266 static int idetape_chrdev_release(struct inode *inode, struct file *filp)
2268 struct ide_tape_obj *tape = ide_tape_f(filp);
2269 ide_drive_t *drive = tape->drive;
2270 struct ide_atapi_pc pc;
2271 unsigned int minor = iminor(inode);
2274 tape = drive->driver_data;
2276 debug_log(DBG_CHRDEV, "Enter %s\n", __func__);
2278 if (tape->chrdev_dir == IDETAPE_DIR_WRITE)
2279 idetape_write_release(drive, minor);
2280 if (tape->chrdev_dir == IDETAPE_DIR_READ) {
2282 ide_tape_discard_merge_buffer(drive, 1);
2285 if (minor < 128 && test_bit(IDE_AFLAG_MEDIUM_PRESENT, &drive->atapi_flags))
2286 (void) idetape_rewind_tape(drive);
2287 if (tape->chrdev_dir == IDETAPE_DIR_NONE) {
2288 if (tape->door_locked == DOOR_LOCKED) {
2289 if (idetape_create_prevent_cmd(drive, &pc, 0)) {
2290 if (!idetape_queue_pc_tail(drive, &pc))
2291 tape->door_locked = DOOR_UNLOCKED;
2295 clear_bit(IDE_AFLAG_BUSY, &drive->atapi_flags);
2301 static void idetape_get_inquiry_results(ide_drive_t *drive)
2303 idetape_tape_t *tape = drive->driver_data;
2304 struct ide_atapi_pc pc;
2305 char fw_rev[4], vendor_id[8], product_id[16];
2307 idetape_create_inquiry_cmd(&pc);
2308 if (idetape_queue_pc_tail(drive, &pc)) {
2309 printk(KERN_ERR "ide-tape: %s: can't get INQUIRY results\n",
2313 memcpy(vendor_id, &pc.buf[8], 8);
2314 memcpy(product_id, &pc.buf[16], 16);
2315 memcpy(fw_rev, &pc.buf[32], 4);
2317 ide_fixstring(vendor_id, 8, 0);
2318 ide_fixstring(product_id, 16, 0);
2319 ide_fixstring(fw_rev, 4, 0);
2321 printk(KERN_INFO "ide-tape: %s <-> %s: %.8s %.16s rev %.4s\n",
2322 drive->name, tape->name, vendor_id, product_id, fw_rev);
2326 * Ask the tape about its various parameters. In particular, we will adjust our
2327 * data transfer buffer size to the recommended value as returned by the tape.
2329 static void idetape_get_mode_sense_results(ide_drive_t *drive)
2331 idetape_tape_t *tape = drive->driver_data;
2332 struct ide_atapi_pc pc;
2334 u8 speed, max_speed;
2336 idetape_create_mode_sense_cmd(&pc, IDETAPE_CAPABILITIES_PAGE);
2337 if (idetape_queue_pc_tail(drive, &pc)) {
2338 printk(KERN_ERR "ide-tape: Can't get tape parameters - assuming"
2339 " some default values\n");
2340 tape->blk_size = 512;
2341 put_unaligned(52, (u16 *)&tape->caps[12]);
2342 put_unaligned(540, (u16 *)&tape->caps[14]);
2343 put_unaligned(6*52, (u16 *)&tape->caps[16]);
2346 caps = pc.buf + 4 + pc.buf[3];
2348 /* convert to host order and save for later use */
2349 speed = be16_to_cpup((__be16 *)&caps[14]);
2350 max_speed = be16_to_cpup((__be16 *)&caps[8]);
2352 *(u16 *)&caps[8] = max_speed;
2353 *(u16 *)&caps[12] = be16_to_cpup((__be16 *)&caps[12]);
2354 *(u16 *)&caps[14] = speed;
2355 *(u16 *)&caps[16] = be16_to_cpup((__be16 *)&caps[16]);
2358 printk(KERN_INFO "ide-tape: %s: invalid tape speed "
2359 "(assuming 650KB/sec)\n", drive->name);
2360 *(u16 *)&caps[14] = 650;
2363 printk(KERN_INFO "ide-tape: %s: invalid max_speed "
2364 "(assuming 650KB/sec)\n", drive->name);
2365 *(u16 *)&caps[8] = 650;
2368 memcpy(&tape->caps, caps, 20);
2370 tape->blk_size = 512;
2371 else if (caps[7] & 0x04)
2372 tape->blk_size = 1024;
2375 #ifdef CONFIG_IDE_PROC_FS
2376 #define ide_tape_devset_get(name, field) \
2377 static int get_##name(ide_drive_t *drive) \
2379 idetape_tape_t *tape = drive->driver_data; \
2380 return tape->field; \
2383 #define ide_tape_devset_set(name, field) \
2384 static int set_##name(ide_drive_t *drive, int arg) \
2386 idetape_tape_t *tape = drive->driver_data; \
2387 tape->field = arg; \
2391 #define ide_tape_devset_rw(_name, _min, _max, _field, _mulf, _divf) \
2392 ide_tape_devset_get(_name, _field) \
2393 ide_tape_devset_set(_name, _field) \
2394 __IDE_DEVSET(_name, S_RW, _min, _max, get_##_name, set_##_name, _mulf, _divf)
2396 #define ide_tape_devset_r(_name, _min, _max, _field, _mulf, _divf) \
2397 ide_tape_devset_get(_name, _field) \
2398 __IDE_DEVSET(_name, S_READ, _min, _max, get_##_name, NULL, _mulf, _divf)
2400 static int mulf_tdsc(ide_drive_t *drive) { return 1000; }
2401 static int divf_tdsc(ide_drive_t *drive) { return HZ; }
2402 static int divf_buffer(ide_drive_t *drive) { return 2; }
2403 static int divf_buffer_size(ide_drive_t *drive) { return 1024; }
2405 ide_devset_rw(dsc_overlap, 0, 1, dsc_overlap);
2407 ide_tape_devset_rw(debug_mask, 0, 0xffff, debug_mask, NULL, NULL);
2408 ide_tape_devset_rw(tdsc, IDETAPE_DSC_RW_MIN, IDETAPE_DSC_RW_MAX,
2409 best_dsc_rw_freq, mulf_tdsc, divf_tdsc);
2411 ide_tape_devset_r(avg_speed, 0, 0xffff, avg_speed, NULL, NULL);
2412 ide_tape_devset_r(speed, 0, 0xffff, caps[14], NULL, NULL);
2413 ide_tape_devset_r(buffer, 0, 0xffff, caps[16], NULL, divf_buffer);
2414 ide_tape_devset_r(buffer_size, 0, 0xffff, buffer_size, NULL, divf_buffer_size);
2416 static const struct ide_devset *idetape_settings[] = {
2417 &ide_devset_avg_speed,
2419 &ide_devset_buffer_size,
2420 &ide_devset_debug_mask,
2421 &ide_devset_dsc_overlap,
2429 * The function below is called to:
2431 * 1. Initialize our various state variables.
2432 * 2. Ask the tape for its capabilities.
2433 * 3. Allocate a buffer which will be used for data transfer. The buffer size
2434 * is chosen based on the recommendation which we received in step 2.
2436 * Note that at this point ide.c already assigned us an irq, so that we can
2437 * queue requests here and wait for their completion.
2439 static void idetape_setup(ide_drive_t *drive, idetape_tape_t *tape, int minor)
2445 u16 *ctl = (u16 *)&tape->caps[12];
2447 drive->pc_callback = ide_tape_callback;
2449 spin_lock_init(&tape->lock);
2450 drive->dsc_overlap = 1;
2451 if (drive->hwif->host_flags & IDE_HFLAG_NO_DSC) {
2452 printk(KERN_INFO "ide-tape: %s: disabling DSC overlap\n",
2454 drive->dsc_overlap = 0;
2456 /* Seagate Travan drives do not support DSC overlap. */
2457 if (strstr((char *)&drive->id[ATA_ID_PROD], "Seagate STT3401"))
2458 drive->dsc_overlap = 0;
2459 tape->minor = minor;
2460 tape->name[0] = 'h';
2461 tape->name[1] = 't';
2462 tape->name[2] = '0' + minor;
2463 tape->chrdev_dir = IDETAPE_DIR_NONE;
2464 tape->pc = tape->pc_stack;
2466 *((u16 *)&gcw) = drive->id[ATA_ID_CONFIG];
2468 /* Command packet DRQ type */
2469 if (((gcw[0] & 0x60) >> 5) == 1)
2470 set_bit(IDE_AFLAG_DRQ_INTERRUPT, &drive->atapi_flags);
2472 idetape_get_inquiry_results(drive);
2473 idetape_get_mode_sense_results(drive);
2474 ide_tape_get_bsize_from_bdesc(drive);
2475 tape->user_bs_factor = 1;
2476 tape->buffer_size = *ctl * tape->blk_size;
2477 while (tape->buffer_size > 0xffff) {
2478 printk(KERN_NOTICE "ide-tape: decreasing stage size\n");
2480 tape->buffer_size = *ctl * tape->blk_size;
2482 buffer_size = tape->buffer_size;
2483 tape->pages_per_buffer = buffer_size / PAGE_SIZE;
2484 if (buffer_size % PAGE_SIZE) {
2485 tape->pages_per_buffer++;
2486 tape->excess_bh_size = PAGE_SIZE - buffer_size % PAGE_SIZE;
2489 /* select the "best" DSC read/write polling freq */
2490 speed = max(*(u16 *)&tape->caps[14], *(u16 *)&tape->caps[8]);
2492 t = (IDETAPE_FIFO_THRESHOLD * tape->buffer_size * HZ) / (speed * 1000);
2495 * Ensure that the number we got makes sense; limit it within
2496 * IDETAPE_DSC_RW_MIN and IDETAPE_DSC_RW_MAX.
2498 tape->best_dsc_rw_freq = clamp_t(unsigned long, t, IDETAPE_DSC_RW_MIN,
2499 IDETAPE_DSC_RW_MAX);
2500 printk(KERN_INFO "ide-tape: %s <-> %s: %dKBps, %d*%dkB buffer, "
2502 drive->name, tape->name, *(u16 *)&tape->caps[14],
2503 (*(u16 *)&tape->caps[16] * 512) / tape->buffer_size,
2504 tape->buffer_size / 1024,
2505 tape->best_dsc_rw_freq * 1000 / HZ,
2506 drive->using_dma ? ", DMA":"");
2508 ide_proc_register_driver(drive, tape->driver);
2511 static void ide_tape_remove(ide_drive_t *drive)
2513 idetape_tape_t *tape = drive->driver_data;
2515 ide_proc_unregister_driver(drive, tape->driver);
2517 ide_unregister_region(tape->disk);
2522 static void ide_tape_release(struct kref *kref)
2524 struct ide_tape_obj *tape = to_ide_tape(kref);
2525 ide_drive_t *drive = tape->drive;
2526 struct gendisk *g = tape->disk;
2528 BUG_ON(tape->merge_bh_size);
2530 drive->dsc_overlap = 0;
2531 drive->driver_data = NULL;
2532 device_destroy(idetape_sysfs_class, MKDEV(IDETAPE_MAJOR, tape->minor));
2533 device_destroy(idetape_sysfs_class,
2534 MKDEV(IDETAPE_MAJOR, tape->minor + 128));
2535 idetape_devs[tape->minor] = NULL;
2536 g->private_data = NULL;
2541 #ifdef CONFIG_IDE_PROC_FS
2542 static int proc_idetape_read_name
2543 (char *page, char **start, off_t off, int count, int *eof, void *data)
2545 ide_drive_t *drive = (ide_drive_t *) data;
2546 idetape_tape_t *tape = drive->driver_data;
2550 len = sprintf(out, "%s\n", tape->name);
2551 PROC_IDE_READ_RETURN(page, start, off, count, eof, len);
2554 static ide_proc_entry_t idetape_proc[] = {
2555 { "capacity", S_IFREG|S_IRUGO, proc_ide_read_capacity, NULL },
2556 { "name", S_IFREG|S_IRUGO, proc_idetape_read_name, NULL },
2557 { NULL, 0, NULL, NULL }
2561 static int ide_tape_probe(ide_drive_t *);
2563 static ide_driver_t idetape_driver = {
2565 .owner = THIS_MODULE,
2567 .bus = &ide_bus_type,
2569 .probe = ide_tape_probe,
2570 .remove = ide_tape_remove,
2571 .version = IDETAPE_VERSION,
2573 .do_request = idetape_do_request,
2574 .end_request = idetape_end_request,
2575 .error = __ide_error,
2576 #ifdef CONFIG_IDE_PROC_FS
2577 .proc = idetape_proc,
2578 .settings = idetape_settings,
2582 /* Our character device supporting functions, passed to register_chrdev. */
2583 static const struct file_operations idetape_fops = {
2584 .owner = THIS_MODULE,
2585 .read = idetape_chrdev_read,
2586 .write = idetape_chrdev_write,
2587 .ioctl = idetape_chrdev_ioctl,
2588 .open = idetape_chrdev_open,
2589 .release = idetape_chrdev_release,
2592 static int idetape_open(struct inode *inode, struct file *filp)
2594 struct gendisk *disk = inode->i_bdev->bd_disk;
2595 struct ide_tape_obj *tape;
2597 tape = ide_tape_get(disk);
2604 static int idetape_release(struct inode *inode, struct file *filp)
2606 struct gendisk *disk = inode->i_bdev->bd_disk;
2607 struct ide_tape_obj *tape = ide_tape_g(disk);
2614 static int idetape_ioctl(struct inode *inode, struct file *file,
2615 unsigned int cmd, unsigned long arg)
2617 struct block_device *bdev = inode->i_bdev;
2618 struct ide_tape_obj *tape = ide_tape_g(bdev->bd_disk);
2619 ide_drive_t *drive = tape->drive;
2620 int err = generic_ide_ioctl(drive, file, bdev, cmd, arg);
2622 err = idetape_blkdev_ioctl(drive, cmd, arg);
2626 static struct block_device_operations idetape_block_ops = {
2627 .owner = THIS_MODULE,
2628 .open = idetape_open,
2629 .release = idetape_release,
2630 .ioctl = idetape_ioctl,
2633 static int ide_tape_probe(ide_drive_t *drive)
2635 idetape_tape_t *tape;
2639 if (!strstr("ide-tape", drive->driver_req))
2642 if (drive->media != ide_tape)
2645 if (drive->id_read == 1 && !ide_check_atapi_device(drive, DRV_NAME)) {
2646 printk(KERN_ERR "ide-tape: %s: not supported by this version of"
2647 " the driver\n", drive->name);
2650 tape = kzalloc(sizeof(idetape_tape_t), GFP_KERNEL);
2652 printk(KERN_ERR "ide-tape: %s: Can't allocate a tape struct\n",
2657 g = alloc_disk(1 << PARTN_BITS);
2661 ide_init_disk(g, drive);
2663 kref_init(&tape->kref);
2665 tape->drive = drive;
2666 tape->driver = &idetape_driver;
2669 g->private_data = &tape->driver;
2671 drive->driver_data = tape;
2673 mutex_lock(&idetape_ref_mutex);
2674 for (minor = 0; idetape_devs[minor]; minor++)
2676 idetape_devs[minor] = tape;
2677 mutex_unlock(&idetape_ref_mutex);
2679 idetape_setup(drive, tape, minor);
2681 device_create_drvdata(idetape_sysfs_class, &drive->gendev,
2682 MKDEV(IDETAPE_MAJOR, minor), NULL,
2684 device_create_drvdata(idetape_sysfs_class, &drive->gendev,
2685 MKDEV(IDETAPE_MAJOR, minor + 128), NULL,
2688 g->fops = &idetape_block_ops;
2689 ide_register_region(g);
2699 static void __exit idetape_exit(void)
2701 driver_unregister(&idetape_driver.gen_driver);
2702 class_destroy(idetape_sysfs_class);
2703 unregister_chrdev(IDETAPE_MAJOR, "ht");
2706 static int __init idetape_init(void)
2709 idetape_sysfs_class = class_create(THIS_MODULE, "ide_tape");
2710 if (IS_ERR(idetape_sysfs_class)) {
2711 idetape_sysfs_class = NULL;
2712 printk(KERN_ERR "Unable to create sysfs class for ide tapes\n");
2717 if (register_chrdev(IDETAPE_MAJOR, "ht", &idetape_fops)) {
2718 printk(KERN_ERR "ide-tape: Failed to register chrdev"
2721 goto out_free_class;
2724 error = driver_register(&idetape_driver.gen_driver);
2726 goto out_free_driver;
2731 driver_unregister(&idetape_driver.gen_driver);
2733 class_destroy(idetape_sysfs_class);
2738 MODULE_ALIAS("ide:*m-tape*");
2739 module_init(idetape_init);
2740 module_exit(idetape_exit);
2741 MODULE_ALIAS_CHARDEV_MAJOR(IDETAPE_MAJOR);
2742 MODULE_DESCRIPTION("ATAPI Streaming TAPE Driver");
2743 MODULE_LICENSE("GPL");