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) */
60 DBG_PC_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 for packet
93 * commands, which will remain valid while we leave the driver to wait for
94 * 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 */
234 struct request request_sense_rq;
237 * DSC polling variables.
239 * While polling for DSC we use postponed_rq to postpone the current
240 * request so that ide.c will be able to service pending requests on the
241 * other device. Note that at most we will have only one DSC (usually
242 * data transfer) request in the device request queue.
244 struct request *postponed_rq;
245 /* The time in which we started polling for DSC */
246 unsigned long dsc_polling_start;
247 /* Timer used to poll for dsc */
248 struct timer_list dsc_timer;
249 /* Read/Write dsc polling frequency */
250 unsigned long best_dsc_rw_freq;
251 unsigned long dsc_poll_freq;
252 unsigned long dsc_timeout;
254 /* Read position information */
257 unsigned int first_frame;
259 /* Last error information */
260 u8 sense_key, asc, ascq;
262 /* Character device operation */
266 /* Current character device data transfer direction */
269 /* tape block size, usually 512 or 1024 bytes */
270 unsigned short blk_size;
273 /* Copy of the tape's Capabilities and Mechanical Page */
277 * Active data transfer request parameters.
279 * At most, there is only one ide-tape originated data transfer request
280 * in the device request queue. This allows ide.c to easily service
281 * requests from the other device when we postpone our active request.
284 /* Data buffer size chosen based on the tape's recommendation */
287 struct idetape_bh *merge_bh;
288 /* size of the merge buffer */
290 /* pointer to current buffer head within the merge buffer */
291 struct idetape_bh *bh;
295 int pages_per_buffer;
296 /* Wasted space in each stage */
299 /* protects the ide-tape queue */
302 /* Measures average tape speed */
303 unsigned long avg_time;
307 /* the door is currently locked */
309 /* the tape hardware is write protected */
311 /* the tape is write protected (hardware or opened as read-only) */
317 static DEFINE_MUTEX(idetape_ref_mutex);
319 static struct class *idetape_sysfs_class;
321 #define to_ide_tape(obj) container_of(obj, struct ide_tape_obj, kref)
323 #define ide_tape_g(disk) \
324 container_of((disk)->private_data, struct ide_tape_obj, driver)
326 static void ide_tape_release(struct kref *);
328 static struct ide_tape_obj *ide_tape_get(struct gendisk *disk)
330 struct ide_tape_obj *tape = NULL;
332 mutex_lock(&idetape_ref_mutex);
333 tape = ide_tape_g(disk);
335 if (ide_device_get(tape->drive))
338 kref_get(&tape->kref);
340 mutex_unlock(&idetape_ref_mutex);
344 static void ide_tape_put(struct ide_tape_obj *tape)
346 ide_drive_t *drive = tape->drive;
348 mutex_lock(&idetape_ref_mutex);
349 kref_put(&tape->kref, ide_tape_release);
350 ide_device_put(drive);
351 mutex_unlock(&idetape_ref_mutex);
355 * The variables below are used for the character device interface. Additional
356 * state variables are defined in our ide_drive_t structure.
358 static struct ide_tape_obj *idetape_devs[MAX_HWIFS * MAX_DRIVES];
360 #define ide_tape_f(file) ((file)->private_data)
362 static struct ide_tape_obj *ide_tape_chrdev_get(unsigned int i)
364 struct ide_tape_obj *tape = NULL;
366 mutex_lock(&idetape_ref_mutex);
367 tape = idetape_devs[i];
369 kref_get(&tape->kref);
370 mutex_unlock(&idetape_ref_mutex);
374 static void idetape_input_buffers(ide_drive_t *drive, struct ide_atapi_pc *pc,
377 struct idetape_bh *bh = pc->bh;
382 printk(KERN_ERR "ide-tape: bh == NULL in "
383 "idetape_input_buffers\n");
384 ide_pad_transfer(drive, 0, bcount);
388 (unsigned int)(bh->b_size - atomic_read(&bh->b_count)),
390 drive->hwif->tp_ops->input_data(drive, NULL, bh->b_data +
391 atomic_read(&bh->b_count), count);
393 atomic_add(count, &bh->b_count);
394 if (atomic_read(&bh->b_count) == bh->b_size) {
397 atomic_set(&bh->b_count, 0);
403 static void idetape_output_buffers(ide_drive_t *drive, struct ide_atapi_pc *pc,
406 struct idetape_bh *bh = pc->bh;
411 printk(KERN_ERR "ide-tape: bh == NULL in %s\n",
415 count = min((unsigned int)pc->b_count, (unsigned int)bcount);
416 drive->hwif->tp_ops->output_data(drive, NULL, pc->b_data, count);
419 pc->b_count -= count;
424 pc->b_data = bh->b_data;
425 pc->b_count = atomic_read(&bh->b_count);
431 static void idetape_update_buffers(ide_drive_t *drive, struct ide_atapi_pc *pc)
433 struct idetape_bh *bh = pc->bh;
435 unsigned int bcount = pc->xferred;
437 if (pc->flags & PC_FLAG_WRITING)
441 printk(KERN_ERR "ide-tape: bh == NULL in %s\n",
445 count = min((unsigned int)bh->b_size, (unsigned int)bcount);
446 atomic_set(&bh->b_count, count);
447 if (atomic_read(&bh->b_count) == bh->b_size)
455 * idetape_next_pc_storage returns a pointer to a place in which we can
456 * safely store a packet command, even though we intend to leave the
457 * driver. A storage space for a maximum of IDETAPE_PC_STACK packet
458 * commands is allocated at initialization time.
460 static struct ide_atapi_pc *idetape_next_pc_storage(ide_drive_t *drive)
462 idetape_tape_t *tape = drive->driver_data;
464 debug_log(DBG_PC_STACK, "pc_stack_index=%d\n", tape->pc_stack_index);
466 if (tape->pc_stack_index == IDETAPE_PC_STACK)
467 tape->pc_stack_index = 0;
468 return (&tape->pc_stack[tape->pc_stack_index++]);
472 * called on each failed packet command retry to analyze the request sense. We
473 * currently do not utilize this information.
475 static void idetape_analyze_error(ide_drive_t *drive, u8 *sense)
477 idetape_tape_t *tape = drive->driver_data;
478 struct ide_atapi_pc *pc = tape->failed_pc;
480 tape->sense_key = sense[2] & 0xF;
481 tape->asc = sense[12];
482 tape->ascq = sense[13];
484 debug_log(DBG_ERR, "pc = %x, sense key = %x, asc = %x, ascq = %x\n",
485 pc->c[0], tape->sense_key, tape->asc, tape->ascq);
487 /* Correct pc->xferred by asking the tape. */
488 if (pc->flags & PC_FLAG_DMA_ERROR) {
489 pc->xferred = pc->req_xfer -
491 get_unaligned_be32(&sense[3]);
492 idetape_update_buffers(drive, pc);
496 * If error was the result of a zero-length read or write command,
497 * with sense key=5, asc=0x22, ascq=0, let it slide. Some drives
498 * (i.e. Seagate STT3401A Travan) don't support 0-length read/writes.
500 if ((pc->c[0] == READ_6 || pc->c[0] == WRITE_6)
502 && pc->c[4] == 0 && pc->c[3] == 0 && pc->c[2] == 0) {
503 if (tape->sense_key == 5) {
504 /* don't report an error, everything's ok */
506 /* don't retry read/write */
507 pc->flags |= PC_FLAG_ABORT;
510 if (pc->c[0] == READ_6 && (sense[2] & 0x80)) {
511 pc->error = IDETAPE_ERROR_FILEMARK;
512 pc->flags |= PC_FLAG_ABORT;
514 if (pc->c[0] == WRITE_6) {
515 if ((sense[2] & 0x40) || (tape->sense_key == 0xd
516 && tape->asc == 0x0 && tape->ascq == 0x2)) {
517 pc->error = IDETAPE_ERROR_EOD;
518 pc->flags |= PC_FLAG_ABORT;
521 if (pc->c[0] == READ_6 || pc->c[0] == WRITE_6) {
522 if (tape->sense_key == 8) {
523 pc->error = IDETAPE_ERROR_EOD;
524 pc->flags |= PC_FLAG_ABORT;
526 if (!(pc->flags & PC_FLAG_ABORT) &&
528 pc->retries = IDETAPE_MAX_PC_RETRIES + 1;
532 /* Free data buffers completely. */
533 static void ide_tape_kfree_buffer(idetape_tape_t *tape)
535 struct idetape_bh *prev_bh, *bh = tape->merge_bh;
538 u32 size = bh->b_size;
541 unsigned int order = fls(size >> PAGE_SHIFT)-1;
544 free_pages((unsigned long)bh->b_data, order);
547 bh->b_data += (1 << order) * PAGE_SIZE;
555 static int idetape_end_request(ide_drive_t *drive, int uptodate, int nr_sects)
557 struct request *rq = HWGROUP(drive)->rq;
558 idetape_tape_t *tape = drive->driver_data;
562 debug_log(DBG_PROCS, "Enter %s\n", __func__);
565 case 0: error = IDETAPE_ERROR_GENERAL; break;
566 case 1: error = 0; break;
567 default: error = uptodate;
571 tape->failed_pc = NULL;
573 if (!blk_special_request(rq)) {
574 ide_end_request(drive, uptodate, nr_sects);
578 spin_lock_irqsave(&tape->lock, flags);
580 ide_end_drive_cmd(drive, 0, 0);
582 spin_unlock_irqrestore(&tape->lock, flags);
586 static void ide_tape_callback(ide_drive_t *drive)
588 idetape_tape_t *tape = drive->driver_data;
589 struct ide_atapi_pc *pc = tape->pc;
590 int uptodate = pc->error ? 0 : 1;
592 debug_log(DBG_PROCS, "Enter %s\n", __func__);
594 if (tape->failed_pc == pc)
595 tape->failed_pc = NULL;
597 if (pc->c[0] == REQUEST_SENSE) {
599 idetape_analyze_error(drive, pc->buf);
601 printk(KERN_ERR "ide-tape: Error in REQUEST SENSE "
602 "itself - Aborting request!\n");
603 } else if (pc->c[0] == READ_6 || pc->c[0] == WRITE_6) {
604 struct request *rq = drive->hwif->hwgroup->rq;
605 int blocks = pc->xferred / tape->blk_size;
607 tape->avg_size += blocks * tape->blk_size;
609 if (time_after_eq(jiffies, tape->avg_time + HZ)) {
610 tape->avg_speed = tape->avg_size * HZ /
611 (jiffies - tape->avg_time) / 1024;
613 tape->avg_time = jiffies;
616 tape->first_frame += blocks;
617 rq->current_nr_sectors -= blocks;
620 uptodate = pc->error;
621 } else if (pc->c[0] == READ_POSITION && uptodate) {
622 u8 *readpos = tape->pc->buf;
624 debug_log(DBG_SENSE, "BOP - %s\n",
625 (readpos[0] & 0x80) ? "Yes" : "No");
626 debug_log(DBG_SENSE, "EOP - %s\n",
627 (readpos[0] & 0x40) ? "Yes" : "No");
629 if (readpos[0] & 0x4) {
630 printk(KERN_INFO "ide-tape: Block location is unknown"
632 clear_bit(IDE_AFLAG_ADDRESS_VALID, &drive->atapi_flags);
635 debug_log(DBG_SENSE, "Block Location - %u\n",
636 be32_to_cpup((__be32 *)&readpos[4]));
638 tape->partition = readpos[1];
639 tape->first_frame = be32_to_cpup((__be32 *)&readpos[4]);
640 set_bit(IDE_AFLAG_ADDRESS_VALID, &drive->atapi_flags);
644 idetape_end_request(drive, uptodate, 0);
647 static void idetape_init_pc(struct ide_atapi_pc *pc)
649 memset(pc->c, 0, 12);
653 pc->buf = pc->pc_buf;
654 pc->buf_size = IDETAPE_PC_BUFFER_SIZE;
659 static void idetape_create_request_sense_cmd(struct ide_atapi_pc *pc)
662 pc->c[0] = REQUEST_SENSE;
668 * Generate a new packet command request in front of the request queue, before
669 * the current request, so that it will be processed immediately, on the next
670 * pass through the driver.
672 static void idetape_queue_pc_head(ide_drive_t *drive, struct ide_atapi_pc *pc,
675 struct ide_tape_obj *tape = drive->driver_data;
677 blk_rq_init(NULL, rq);
678 rq->cmd_type = REQ_TYPE_SPECIAL;
679 rq->cmd_flags |= REQ_PREEMPT;
680 rq->buffer = (char *) pc;
681 rq->rq_disk = tape->disk;
682 memcpy(rq->cmd, pc->c, 12);
683 rq->cmd[13] = REQ_IDETAPE_PC1;
684 ide_do_drive_cmd(drive, rq);
688 * idetape_retry_pc is called when an error was detected during the
689 * last packet command. We queue a request sense packet command in
690 * the head of the request list.
692 static void idetape_retry_pc(ide_drive_t *drive)
694 struct ide_tape_obj *tape = drive->driver_data;
695 struct request *rq = &tape->request_sense_rq;
696 struct ide_atapi_pc *pc;
698 (void)ide_read_error(drive);
699 pc = idetape_next_pc_storage(drive);
700 idetape_create_request_sense_cmd(pc);
701 set_bit(IDE_AFLAG_IGNORE_DSC, &drive->atapi_flags);
702 idetape_queue_pc_head(drive, pc, rq);
706 * Postpone the current request so that ide.c will be able to service requests
707 * from another device on the same hwgroup while we are polling for DSC.
709 static void idetape_postpone_request(ide_drive_t *drive)
711 idetape_tape_t *tape = drive->driver_data;
713 debug_log(DBG_PROCS, "Enter %s\n", __func__);
715 tape->postponed_rq = HWGROUP(drive)->rq;
716 ide_stall_queue(drive, tape->dsc_poll_freq);
719 static void ide_tape_handle_dsc(ide_drive_t *drive)
721 idetape_tape_t *tape = drive->driver_data;
723 /* Media access command */
724 tape->dsc_polling_start = jiffies;
725 tape->dsc_poll_freq = IDETAPE_DSC_MA_FAST;
726 tape->dsc_timeout = jiffies + IDETAPE_DSC_MA_TIMEOUT;
727 /* Allow ide.c to handle other requests */
728 idetape_postpone_request(drive);
731 static void ide_tape_io_buffers(ide_drive_t *drive, struct ide_atapi_pc *pc,
732 unsigned int bcount, int write)
735 idetape_output_buffers(drive, pc, bcount);
737 idetape_input_buffers(drive, pc, bcount);
741 * This is the usual interrupt handler which will be called during a packet
742 * command. We will transfer some of the data (as requested by the drive) and
743 * will re-point interrupt handler to us. When data transfer is finished, we
744 * will act according to the algorithm described before
747 static ide_startstop_t idetape_pc_intr(ide_drive_t *drive)
749 idetape_tape_t *tape = drive->driver_data;
751 return ide_pc_intr(drive, tape->pc, idetape_pc_intr, IDETAPE_WAIT_CMD,
752 NULL, idetape_update_buffers, idetape_retry_pc,
753 ide_tape_handle_dsc, ide_tape_io_buffers);
757 * Packet Command Interface
759 * The current Packet Command is available in tape->pc, and will not change
760 * until we finish handling it. Each packet command is associated with a
761 * callback function that will be called when the command is finished.
763 * The handling will be done in three stages:
765 * 1. idetape_issue_pc will send the packet command to the drive, and will set
766 * the interrupt handler to idetape_pc_intr.
768 * 2. On each interrupt, idetape_pc_intr will be called. This step will be
769 * repeated until the device signals us that no more interrupts will be issued.
771 * 3. ATAPI Tape media access commands have immediate status with a delayed
772 * process. In case of a successful initiation of a media access packet command,
773 * the DSC bit will be set when the actual execution of the command is finished.
774 * Since the tape drive will not issue an interrupt, we have to poll for this
775 * event. In this case, we define the request as "low priority request" by
776 * setting rq_status to IDETAPE_RQ_POSTPONED, set a timer to poll for DSC and
779 * ide.c will then give higher priority to requests which originate from the
780 * other device, until will change rq_status to RQ_ACTIVE.
782 * 4. When the packet command is finished, it will be checked for errors.
784 * 5. In case an error was found, we queue a request sense packet command in
785 * front of the request queue and retry the operation up to
786 * IDETAPE_MAX_PC_RETRIES times.
788 * 6. In case no error was found, or we decided to give up and not to retry
789 * again, the callback function will be called and then we will handle the next
792 static ide_startstop_t idetape_transfer_pc(ide_drive_t *drive)
794 idetape_tape_t *tape = drive->driver_data;
796 return ide_transfer_pc(drive, tape->pc, idetape_pc_intr,
797 IDETAPE_WAIT_CMD, NULL);
800 static ide_startstop_t idetape_issue_pc(ide_drive_t *drive,
801 struct ide_atapi_pc *pc)
803 idetape_tape_t *tape = drive->driver_data;
805 if (tape->pc->c[0] == REQUEST_SENSE &&
806 pc->c[0] == REQUEST_SENSE) {
807 printk(KERN_ERR "ide-tape: possible ide-tape.c bug - "
808 "Two request sense in serial were issued\n");
811 if (tape->failed_pc == NULL && pc->c[0] != REQUEST_SENSE)
812 tape->failed_pc = pc;
813 /* Set the current packet command */
816 if (pc->retries > IDETAPE_MAX_PC_RETRIES ||
817 (pc->flags & PC_FLAG_ABORT)) {
819 * We will "abort" retrying a packet command in case legitimate
820 * error code was received (crossing a filemark, or end of the
821 * media, for example).
823 if (!(pc->flags & PC_FLAG_ABORT)) {
824 if (!(pc->c[0] == TEST_UNIT_READY &&
825 tape->sense_key == 2 && tape->asc == 4 &&
826 (tape->ascq == 1 || tape->ascq == 8))) {
827 printk(KERN_ERR "ide-tape: %s: I/O error, "
828 "pc = %2x, key = %2x, "
829 "asc = %2x, ascq = %2x\n",
830 tape->name, pc->c[0],
831 tape->sense_key, tape->asc,
835 pc->error = IDETAPE_ERROR_GENERAL;
837 tape->failed_pc = NULL;
838 drive->pc_callback(drive);
841 debug_log(DBG_SENSE, "Retry #%d, cmd = %02X\n", pc->retries, pc->c[0]);
845 return ide_issue_pc(drive, pc, idetape_transfer_pc,
846 IDETAPE_WAIT_CMD, NULL);
849 /* A mode sense command is used to "sense" tape parameters. */
850 static void idetape_create_mode_sense_cmd(struct ide_atapi_pc *pc, u8 page_code)
853 pc->c[0] = MODE_SENSE;
854 if (page_code != IDETAPE_BLOCK_DESCRIPTOR)
855 /* DBD = 1 - Don't return block descriptors */
857 pc->c[2] = page_code;
859 * Changed pc->c[3] to 0 (255 will at best return unused info).
861 * For SCSI this byte is defined as subpage instead of high byte
862 * of length and some IDE drives seem to interpret it this way
863 * and return an error when 255 is used.
866 /* We will just discard data in that case */
868 if (page_code == IDETAPE_BLOCK_DESCRIPTOR)
870 else if (page_code == IDETAPE_CAPABILITIES_PAGE)
876 static ide_startstop_t idetape_media_access_finished(ide_drive_t *drive)
878 ide_hwif_t *hwif = drive->hwif;
879 idetape_tape_t *tape = drive->driver_data;
880 struct ide_atapi_pc *pc = tape->pc;
883 stat = hwif->tp_ops->read_status(hwif);
885 if (stat & ATA_DSC) {
886 if (stat & ATA_ERR) {
888 if (pc->c[0] != TEST_UNIT_READY)
889 printk(KERN_ERR "ide-tape: %s: I/O error, ",
891 /* Retry operation */
892 idetape_retry_pc(drive);
897 pc->error = IDETAPE_ERROR_GENERAL;
898 tape->failed_pc = NULL;
900 drive->pc_callback(drive);
904 static void ide_tape_create_rw_cmd(idetape_tape_t *tape,
905 struct ide_atapi_pc *pc, struct request *rq,
908 struct idetape_bh *bh = (struct idetape_bh *)rq->special;
909 unsigned int length = rq->current_nr_sectors;
912 put_unaligned(cpu_to_be32(length), (unsigned int *) &pc->c[1]);
916 pc->buf_size = length * tape->blk_size;
917 pc->req_xfer = pc->buf_size;
918 if (pc->req_xfer == tape->buffer_size)
919 pc->flags |= PC_FLAG_DMA_OK;
921 if (opcode == READ_6) {
923 atomic_set(&bh->b_count, 0);
924 } else if (opcode == WRITE_6) {
926 pc->flags |= PC_FLAG_WRITING;
927 pc->b_data = bh->b_data;
928 pc->b_count = atomic_read(&bh->b_count);
931 memcpy(rq->cmd, pc->c, 12);
934 static ide_startstop_t idetape_do_request(ide_drive_t *drive,
935 struct request *rq, sector_t block)
937 ide_hwif_t *hwif = drive->hwif;
938 idetape_tape_t *tape = drive->driver_data;
939 struct ide_atapi_pc *pc = NULL;
940 struct request *postponed_rq = tape->postponed_rq;
943 debug_log(DBG_SENSE, "sector: %llu, nr_sectors: %lu,"
944 " current_nr_sectors: %u\n",
945 (unsigned long long)rq->sector, rq->nr_sectors,
946 rq->current_nr_sectors);
948 if (!blk_special_request(rq)) {
949 /* We do not support buffer cache originated requests. */
950 printk(KERN_NOTICE "ide-tape: %s: Unsupported request in "
951 "request queue (%d)\n", drive->name, rq->cmd_type);
952 ide_end_request(drive, 0, 0);
956 /* Retry a failed packet command */
957 if (tape->failed_pc && tape->pc->c[0] == REQUEST_SENSE) {
958 pc = tape->failed_pc;
962 if (postponed_rq != NULL)
963 if (rq != postponed_rq) {
964 printk(KERN_ERR "ide-tape: ide-tape.c bug - "
965 "Two DSC requests were queued\n");
966 idetape_end_request(drive, 0, 0);
970 tape->postponed_rq = NULL;
973 * If the tape is still busy, postpone our request and service
974 * the other device meanwhile.
976 stat = hwif->tp_ops->read_status(hwif);
978 if (!drive->dsc_overlap && !(rq->cmd[13] & REQ_IDETAPE_PC2))
979 set_bit(IDE_AFLAG_IGNORE_DSC, &drive->atapi_flags);
981 if (drive->post_reset == 1) {
982 set_bit(IDE_AFLAG_IGNORE_DSC, &drive->atapi_flags);
983 drive->post_reset = 0;
986 if (!test_and_clear_bit(IDE_AFLAG_IGNORE_DSC, &drive->atapi_flags) &&
987 (stat & ATA_DSC) == 0) {
988 if (postponed_rq == NULL) {
989 tape->dsc_polling_start = jiffies;
990 tape->dsc_poll_freq = tape->best_dsc_rw_freq;
991 tape->dsc_timeout = jiffies + IDETAPE_DSC_RW_TIMEOUT;
992 } else if (time_after(jiffies, tape->dsc_timeout)) {
993 printk(KERN_ERR "ide-tape: %s: DSC timeout\n",
995 if (rq->cmd[13] & REQ_IDETAPE_PC2) {
996 idetape_media_access_finished(drive);
999 return ide_do_reset(drive);
1001 } else if (time_after(jiffies,
1002 tape->dsc_polling_start +
1003 IDETAPE_DSC_MA_THRESHOLD))
1004 tape->dsc_poll_freq = IDETAPE_DSC_MA_SLOW;
1005 idetape_postpone_request(drive);
1008 if (rq->cmd[13] & REQ_IDETAPE_READ) {
1009 pc = idetape_next_pc_storage(drive);
1010 ide_tape_create_rw_cmd(tape, pc, rq, READ_6);
1013 if (rq->cmd[13] & REQ_IDETAPE_WRITE) {
1014 pc = idetape_next_pc_storage(drive);
1015 ide_tape_create_rw_cmd(tape, pc, rq, WRITE_6);
1018 if (rq->cmd[13] & REQ_IDETAPE_PC1) {
1019 pc = (struct ide_atapi_pc *) rq->buffer;
1020 rq->cmd[13] &= ~(REQ_IDETAPE_PC1);
1021 rq->cmd[13] |= REQ_IDETAPE_PC2;
1024 if (rq->cmd[13] & REQ_IDETAPE_PC2) {
1025 idetape_media_access_finished(drive);
1031 return idetape_issue_pc(drive, pc);
1035 * The function below uses __get_free_pages to allocate a data buffer of size
1036 * tape->buffer_size (or a bit more). We attempt to combine sequential pages as
1039 * It returns a pointer to the newly allocated buffer, or NULL in case of
1042 static struct idetape_bh *ide_tape_kmalloc_buffer(idetape_tape_t *tape,
1043 int full, int clear)
1045 struct idetape_bh *prev_bh, *bh, *merge_bh;
1046 int pages = tape->pages_per_buffer;
1047 unsigned int order, b_allocd;
1048 char *b_data = NULL;
1050 merge_bh = kmalloc(sizeof(struct idetape_bh), GFP_KERNEL);
1055 order = fls(pages) - 1;
1056 bh->b_data = (char *) __get_free_pages(GFP_KERNEL, order);
1059 b_allocd = (1 << order) * PAGE_SIZE;
1063 memset(bh->b_data, 0, b_allocd);
1064 bh->b_reqnext = NULL;
1065 bh->b_size = b_allocd;
1066 atomic_set(&bh->b_count, full ? bh->b_size : 0);
1069 order = fls(pages) - 1;
1070 b_data = (char *) __get_free_pages(GFP_KERNEL, order);
1073 b_allocd = (1 << order) * PAGE_SIZE;
1076 memset(b_data, 0, b_allocd);
1078 /* newly allocated page frames below buffer header or ...*/
1079 if (bh->b_data == b_data + b_allocd) {
1080 bh->b_size += b_allocd;
1081 bh->b_data -= b_allocd;
1083 atomic_add(b_allocd, &bh->b_count);
1086 /* they are above the header */
1087 if (b_data == bh->b_data + bh->b_size) {
1088 bh->b_size += b_allocd;
1090 atomic_add(b_allocd, &bh->b_count);
1094 bh = kmalloc(sizeof(struct idetape_bh), GFP_KERNEL);
1096 free_pages((unsigned long) b_data, order);
1099 bh->b_reqnext = NULL;
1100 bh->b_data = b_data;
1101 bh->b_size = b_allocd;
1102 atomic_set(&bh->b_count, full ? bh->b_size : 0);
1103 prev_bh->b_reqnext = bh;
1108 bh->b_size -= tape->excess_bh_size;
1110 atomic_sub(tape->excess_bh_size, &bh->b_count);
1113 ide_tape_kfree_buffer(tape);
1117 static int idetape_copy_stage_from_user(idetape_tape_t *tape,
1118 const char __user *buf, int n)
1120 struct idetape_bh *bh = tape->bh;
1126 printk(KERN_ERR "ide-tape: bh == NULL in %s\n",
1130 count = min((unsigned int)
1131 (bh->b_size - atomic_read(&bh->b_count)),
1133 if (copy_from_user(bh->b_data + atomic_read(&bh->b_count), buf,
1137 atomic_add(count, &bh->b_count);
1139 if (atomic_read(&bh->b_count) == bh->b_size) {
1142 atomic_set(&bh->b_count, 0);
1149 static int idetape_copy_stage_to_user(idetape_tape_t *tape, char __user *buf,
1152 struct idetape_bh *bh = tape->bh;
1158 printk(KERN_ERR "ide-tape: bh == NULL in %s\n",
1162 count = min(tape->b_count, n);
1163 if (copy_to_user(buf, tape->b_data, count))
1166 tape->b_data += count;
1167 tape->b_count -= count;
1169 if (!tape->b_count) {
1173 tape->b_data = bh->b_data;
1174 tape->b_count = atomic_read(&bh->b_count);
1181 static void idetape_init_merge_buffer(idetape_tape_t *tape)
1183 struct idetape_bh *bh = tape->merge_bh;
1184 tape->bh = tape->merge_bh;
1186 if (tape->chrdev_dir == IDETAPE_DIR_WRITE)
1187 atomic_set(&bh->b_count, 0);
1189 tape->b_data = bh->b_data;
1190 tape->b_count = atomic_read(&bh->b_count);
1195 * Write a filemark if write_filemark=1. Flush the device buffers without
1196 * writing a filemark otherwise.
1198 static void idetape_create_write_filemark_cmd(ide_drive_t *drive,
1199 struct ide_atapi_pc *pc, int write_filemark)
1201 idetape_init_pc(pc);
1202 pc->c[0] = WRITE_FILEMARKS;
1203 pc->c[4] = write_filemark;
1204 pc->flags |= PC_FLAG_WAIT_FOR_DSC;
1207 static void idetape_create_test_unit_ready_cmd(struct ide_atapi_pc *pc)
1209 idetape_init_pc(pc);
1210 pc->c[0] = TEST_UNIT_READY;
1214 * We add a special packet command request to the tail of the request queue, and
1215 * wait for it to be serviced.
1217 static int idetape_queue_pc_tail(ide_drive_t *drive, struct ide_atapi_pc *pc)
1219 struct ide_tape_obj *tape = drive->driver_data;
1223 rq = blk_get_request(drive->queue, READ, __GFP_WAIT);
1224 rq->cmd_type = REQ_TYPE_SPECIAL;
1225 rq->cmd[13] = REQ_IDETAPE_PC1;
1226 rq->buffer = (char *)pc;
1227 memcpy(rq->cmd, pc->c, 12);
1228 error = blk_execute_rq(drive->queue, tape->disk, rq, 0);
1229 blk_put_request(rq);
1233 static void idetape_create_load_unload_cmd(ide_drive_t *drive,
1234 struct ide_atapi_pc *pc, int cmd)
1236 idetape_init_pc(pc);
1237 pc->c[0] = START_STOP;
1239 pc->flags |= PC_FLAG_WAIT_FOR_DSC;
1242 static int idetape_wait_ready(ide_drive_t *drive, unsigned long timeout)
1244 idetape_tape_t *tape = drive->driver_data;
1245 struct ide_atapi_pc pc;
1246 int load_attempted = 0;
1248 /* Wait for the tape to become ready */
1249 set_bit(IDE_AFLAG_MEDIUM_PRESENT, &drive->atapi_flags);
1251 while (time_before(jiffies, timeout)) {
1252 idetape_create_test_unit_ready_cmd(&pc);
1253 if (!idetape_queue_pc_tail(drive, &pc))
1255 if ((tape->sense_key == 2 && tape->asc == 4 && tape->ascq == 2)
1256 || (tape->asc == 0x3A)) {
1260 idetape_create_load_unload_cmd(drive, &pc,
1261 IDETAPE_LU_LOAD_MASK);
1262 idetape_queue_pc_tail(drive, &pc);
1264 /* not about to be ready */
1265 } else if (!(tape->sense_key == 2 && tape->asc == 4 &&
1266 (tape->ascq == 1 || tape->ascq == 8)))
1273 static int idetape_flush_tape_buffers(ide_drive_t *drive)
1275 struct ide_atapi_pc pc;
1278 idetape_create_write_filemark_cmd(drive, &pc, 0);
1279 rc = idetape_queue_pc_tail(drive, &pc);
1282 idetape_wait_ready(drive, 60 * 5 * HZ);
1286 static void idetape_create_read_position_cmd(struct ide_atapi_pc *pc)
1288 idetape_init_pc(pc);
1289 pc->c[0] = READ_POSITION;
1293 static int idetape_read_position(ide_drive_t *drive)
1295 idetape_tape_t *tape = drive->driver_data;
1296 struct ide_atapi_pc pc;
1299 debug_log(DBG_PROCS, "Enter %s\n", __func__);
1301 idetape_create_read_position_cmd(&pc);
1302 if (idetape_queue_pc_tail(drive, &pc))
1304 position = tape->first_frame;
1308 static void idetape_create_locate_cmd(ide_drive_t *drive,
1309 struct ide_atapi_pc *pc,
1310 unsigned int block, u8 partition, int skip)
1312 idetape_init_pc(pc);
1313 pc->c[0] = POSITION_TO_ELEMENT;
1315 put_unaligned(cpu_to_be32(block), (unsigned int *) &pc->c[3]);
1316 pc->c[8] = partition;
1317 pc->flags |= PC_FLAG_WAIT_FOR_DSC;
1320 static int idetape_create_prevent_cmd(ide_drive_t *drive,
1321 struct ide_atapi_pc *pc, int prevent)
1323 idetape_tape_t *tape = drive->driver_data;
1325 /* device supports locking according to capabilities page */
1326 if (!(tape->caps[6] & 0x01))
1329 idetape_init_pc(pc);
1330 pc->c[0] = ALLOW_MEDIUM_REMOVAL;
1335 static void __ide_tape_discard_merge_buffer(ide_drive_t *drive)
1337 idetape_tape_t *tape = drive->driver_data;
1339 if (tape->chrdev_dir != IDETAPE_DIR_READ)
1342 clear_bit(IDE_AFLAG_FILEMARK, &drive->atapi_flags);
1343 tape->merge_bh_size = 0;
1344 if (tape->merge_bh != NULL) {
1345 ide_tape_kfree_buffer(tape);
1346 tape->merge_bh = NULL;
1349 tape->chrdev_dir = IDETAPE_DIR_NONE;
1353 * Position the tape to the requested block using the LOCATE packet command.
1354 * A READ POSITION command is then issued to check where we are positioned. Like
1355 * all higher level operations, we queue the commands at the tail of the request
1356 * queue and wait for their completion.
1358 static int idetape_position_tape(ide_drive_t *drive, unsigned int block,
1359 u8 partition, int skip)
1361 idetape_tape_t *tape = drive->driver_data;
1363 struct ide_atapi_pc pc;
1365 if (tape->chrdev_dir == IDETAPE_DIR_READ)
1366 __ide_tape_discard_merge_buffer(drive);
1367 idetape_wait_ready(drive, 60 * 5 * HZ);
1368 idetape_create_locate_cmd(drive, &pc, block, partition, skip);
1369 retval = idetape_queue_pc_tail(drive, &pc);
1373 idetape_create_read_position_cmd(&pc);
1374 return (idetape_queue_pc_tail(drive, &pc));
1377 static void ide_tape_discard_merge_buffer(ide_drive_t *drive,
1378 int restore_position)
1380 idetape_tape_t *tape = drive->driver_data;
1383 __ide_tape_discard_merge_buffer(drive);
1384 if (restore_position) {
1385 position = idetape_read_position(drive);
1386 seek = position > 0 ? position : 0;
1387 if (idetape_position_tape(drive, seek, 0, 0)) {
1388 printk(KERN_INFO "ide-tape: %s: position_tape failed in"
1389 " %s\n", tape->name, __func__);
1396 * Generate a read/write request for the block device interface and wait for it
1399 static int idetape_queue_rw_tail(ide_drive_t *drive, int cmd, int blocks,
1400 struct idetape_bh *bh)
1402 idetape_tape_t *tape = drive->driver_data;
1406 debug_log(DBG_SENSE, "%s: cmd=%d\n", __func__, cmd);
1408 rq = blk_get_request(drive->queue, READ, __GFP_WAIT);
1409 rq->cmd_type = REQ_TYPE_SPECIAL;
1411 rq->rq_disk = tape->disk;
1412 rq->special = (void *)bh;
1413 rq->sector = tape->first_frame;
1414 rq->nr_sectors = blocks;
1415 rq->current_nr_sectors = blocks;
1416 blk_execute_rq(drive->queue, tape->disk, rq, 0);
1418 errors = rq->errors;
1419 ret = tape->blk_size * (blocks - rq->current_nr_sectors);
1420 blk_put_request(rq);
1422 if ((cmd & (REQ_IDETAPE_READ | REQ_IDETAPE_WRITE)) == 0)
1426 idetape_init_merge_buffer(tape);
1427 if (errors == IDETAPE_ERROR_GENERAL)
1432 static void idetape_create_inquiry_cmd(struct ide_atapi_pc *pc)
1434 idetape_init_pc(pc);
1440 static void idetape_create_rewind_cmd(ide_drive_t *drive,
1441 struct ide_atapi_pc *pc)
1443 idetape_init_pc(pc);
1444 pc->c[0] = REZERO_UNIT;
1445 pc->flags |= PC_FLAG_WAIT_FOR_DSC;
1448 static void idetape_create_erase_cmd(struct ide_atapi_pc *pc)
1450 idetape_init_pc(pc);
1453 pc->flags |= PC_FLAG_WAIT_FOR_DSC;
1456 static void idetape_create_space_cmd(struct ide_atapi_pc *pc, int count, u8 cmd)
1458 idetape_init_pc(pc);
1460 put_unaligned(cpu_to_be32(count), (unsigned int *) &pc->c[1]);
1462 pc->flags |= PC_FLAG_WAIT_FOR_DSC;
1465 /* Queue up a character device originated write request. */
1466 static int idetape_add_chrdev_write_request(ide_drive_t *drive, int blocks)
1468 idetape_tape_t *tape = drive->driver_data;
1470 debug_log(DBG_CHRDEV, "Enter %s\n", __func__);
1472 return idetape_queue_rw_tail(drive, REQ_IDETAPE_WRITE,
1473 blocks, tape->merge_bh);
1476 static void ide_tape_flush_merge_buffer(ide_drive_t *drive)
1478 idetape_tape_t *tape = drive->driver_data;
1480 struct idetape_bh *bh;
1482 if (tape->chrdev_dir != IDETAPE_DIR_WRITE) {
1483 printk(KERN_ERR "ide-tape: bug: Trying to empty merge buffer"
1484 " but we are not writing.\n");
1487 if (tape->merge_bh_size > tape->buffer_size) {
1488 printk(KERN_ERR "ide-tape: bug: merge_buffer too big\n");
1489 tape->merge_bh_size = tape->buffer_size;
1491 if (tape->merge_bh_size) {
1492 blocks = tape->merge_bh_size / tape->blk_size;
1493 if (tape->merge_bh_size % tape->blk_size) {
1497 i = tape->blk_size - tape->merge_bh_size %
1499 bh = tape->bh->b_reqnext;
1501 atomic_set(&bh->b_count, 0);
1507 printk(KERN_INFO "ide-tape: bug,"
1511 min = min(i, (unsigned int)(bh->b_size -
1512 atomic_read(&bh->b_count)));
1513 memset(bh->b_data + atomic_read(&bh->b_count),
1515 atomic_add(min, &bh->b_count);
1520 (void) idetape_add_chrdev_write_request(drive, blocks);
1521 tape->merge_bh_size = 0;
1523 if (tape->merge_bh != NULL) {
1524 ide_tape_kfree_buffer(tape);
1525 tape->merge_bh = NULL;
1527 tape->chrdev_dir = IDETAPE_DIR_NONE;
1530 static int idetape_init_read(ide_drive_t *drive)
1532 idetape_tape_t *tape = drive->driver_data;
1535 /* Initialize read operation */
1536 if (tape->chrdev_dir != IDETAPE_DIR_READ) {
1537 if (tape->chrdev_dir == IDETAPE_DIR_WRITE) {
1538 ide_tape_flush_merge_buffer(drive);
1539 idetape_flush_tape_buffers(drive);
1541 if (tape->merge_bh || tape->merge_bh_size) {
1542 printk(KERN_ERR "ide-tape: merge_bh_size should be"
1544 tape->merge_bh_size = 0;
1546 tape->merge_bh = ide_tape_kmalloc_buffer(tape, 0, 0);
1547 if (!tape->merge_bh)
1549 tape->chrdev_dir = IDETAPE_DIR_READ;
1552 * Issue a read 0 command to ensure that DSC handshake is
1553 * switched from completion mode to buffer available mode.
1554 * No point in issuing this if DSC overlap isn't supported, some
1555 * drives (Seagate STT3401A) will return an error.
1557 if (drive->dsc_overlap) {
1558 bytes_read = idetape_queue_rw_tail(drive,
1559 REQ_IDETAPE_READ, 0,
1561 if (bytes_read < 0) {
1562 ide_tape_kfree_buffer(tape);
1563 tape->merge_bh = NULL;
1564 tape->chrdev_dir = IDETAPE_DIR_NONE;
1573 /* called from idetape_chrdev_read() to service a chrdev read request. */
1574 static int idetape_add_chrdev_read_request(ide_drive_t *drive, int blocks)
1576 idetape_tape_t *tape = drive->driver_data;
1578 debug_log(DBG_PROCS, "Enter %s, %d blocks\n", __func__, blocks);
1580 /* If we are at a filemark, return a read length of 0 */
1581 if (test_bit(IDE_AFLAG_FILEMARK, &drive->atapi_flags))
1584 idetape_init_read(drive);
1586 return idetape_queue_rw_tail(drive, REQ_IDETAPE_READ, blocks,
1590 static void idetape_pad_zeros(ide_drive_t *drive, int bcount)
1592 idetape_tape_t *tape = drive->driver_data;
1593 struct idetape_bh *bh;
1599 bh = tape->merge_bh;
1600 count = min(tape->buffer_size, bcount);
1602 blocks = count / tape->blk_size;
1604 atomic_set(&bh->b_count,
1605 min(count, (unsigned int)bh->b_size));
1606 memset(bh->b_data, 0, atomic_read(&bh->b_count));
1607 count -= atomic_read(&bh->b_count);
1610 idetape_queue_rw_tail(drive, REQ_IDETAPE_WRITE, blocks,
1616 * Rewinds the tape to the Beginning Of the current Partition (BOP). We
1617 * currently support only one partition.
1619 static int idetape_rewind_tape(ide_drive_t *drive)
1622 struct ide_atapi_pc pc;
1623 idetape_tape_t *tape;
1624 tape = drive->driver_data;
1626 debug_log(DBG_SENSE, "Enter %s\n", __func__);
1628 idetape_create_rewind_cmd(drive, &pc);
1629 retval = idetape_queue_pc_tail(drive, &pc);
1633 idetape_create_read_position_cmd(&pc);
1634 retval = idetape_queue_pc_tail(drive, &pc);
1640 /* mtio.h compatible commands should be issued to the chrdev interface. */
1641 static int idetape_blkdev_ioctl(ide_drive_t *drive, unsigned int cmd,
1644 idetape_tape_t *tape = drive->driver_data;
1645 void __user *argp = (void __user *)arg;
1647 struct idetape_config {
1648 int dsc_rw_frequency;
1649 int dsc_media_access_frequency;
1653 debug_log(DBG_PROCS, "Enter %s\n", __func__);
1657 if (copy_from_user(&config, argp, sizeof(config)))
1659 tape->best_dsc_rw_freq = config.dsc_rw_frequency;
1662 config.dsc_rw_frequency = (int) tape->best_dsc_rw_freq;
1663 config.nr_stages = 1;
1664 if (copy_to_user(argp, &config, sizeof(config)))
1673 static int idetape_space_over_filemarks(ide_drive_t *drive, short mt_op,
1676 idetape_tape_t *tape = drive->driver_data;
1677 struct ide_atapi_pc pc;
1678 int retval, count = 0;
1679 int sprev = !!(tape->caps[4] & 0x20);
1683 if (MTBSF == mt_op || MTBSFM == mt_op) {
1686 mt_count = -mt_count;
1689 if (tape->chrdev_dir == IDETAPE_DIR_READ) {
1690 tape->merge_bh_size = 0;
1691 if (test_and_clear_bit(IDE_AFLAG_FILEMARK, &drive->atapi_flags))
1693 ide_tape_discard_merge_buffer(drive, 0);
1699 idetape_create_space_cmd(&pc, mt_count - count,
1700 IDETAPE_SPACE_OVER_FILEMARK);
1701 return idetape_queue_pc_tail(drive, &pc);
1706 retval = idetape_space_over_filemarks(drive, MTFSF,
1710 count = (MTBSFM == mt_op ? 1 : -1);
1711 return idetape_space_over_filemarks(drive, MTFSF, count);
1713 printk(KERN_ERR "ide-tape: MTIO operation %d not supported\n",
1720 * Our character device read / write functions.
1722 * The tape is optimized to maximize throughput when it is transferring an
1723 * integral number of the "continuous transfer limit", which is a parameter of
1724 * the specific tape (26kB on my particular tape, 32kB for Onstream).
1726 * As of version 1.3 of the driver, the character device provides an abstract
1727 * continuous view of the media - any mix of block sizes (even 1 byte) on the
1728 * same backup/restore procedure is supported. The driver will internally
1729 * convert the requests to the recommended transfer unit, so that an unmatch
1730 * between the user's block size to the recommended size will only result in a
1731 * (slightly) increased driver overhead, but will no longer hit performance.
1732 * This is not applicable to Onstream.
1734 static ssize_t idetape_chrdev_read(struct file *file, char __user *buf,
1735 size_t count, loff_t *ppos)
1737 struct ide_tape_obj *tape = ide_tape_f(file);
1738 ide_drive_t *drive = tape->drive;
1739 ssize_t bytes_read, temp, actually_read = 0, rc;
1741 u16 ctl = *(u16 *)&tape->caps[12];
1743 debug_log(DBG_CHRDEV, "Enter %s, count %Zd\n", __func__, count);
1745 if (tape->chrdev_dir != IDETAPE_DIR_READ) {
1746 if (test_bit(IDE_AFLAG_DETECT_BS, &drive->atapi_flags))
1747 if (count > tape->blk_size &&
1748 (count % tape->blk_size) == 0)
1749 tape->user_bs_factor = count / tape->blk_size;
1751 rc = idetape_init_read(drive);
1756 if (tape->merge_bh_size) {
1757 actually_read = min((unsigned int)(tape->merge_bh_size),
1758 (unsigned int)count);
1759 if (idetape_copy_stage_to_user(tape, buf, actually_read))
1761 buf += actually_read;
1762 tape->merge_bh_size -= actually_read;
1763 count -= actually_read;
1765 while (count >= tape->buffer_size) {
1766 bytes_read = idetape_add_chrdev_read_request(drive, ctl);
1767 if (bytes_read <= 0)
1769 if (idetape_copy_stage_to_user(tape, buf, bytes_read))
1772 count -= bytes_read;
1773 actually_read += bytes_read;
1776 bytes_read = idetape_add_chrdev_read_request(drive, ctl);
1777 if (bytes_read <= 0)
1779 temp = min((unsigned long)count, (unsigned long)bytes_read);
1780 if (idetape_copy_stage_to_user(tape, buf, temp))
1782 actually_read += temp;
1783 tape->merge_bh_size = bytes_read-temp;
1786 if (!actually_read && test_bit(IDE_AFLAG_FILEMARK, &drive->atapi_flags)) {
1787 debug_log(DBG_SENSE, "%s: spacing over filemark\n", tape->name);
1789 idetape_space_over_filemarks(drive, MTFSF, 1);
1793 return ret ? ret : actually_read;
1796 static ssize_t idetape_chrdev_write(struct file *file, const char __user *buf,
1797 size_t count, loff_t *ppos)
1799 struct ide_tape_obj *tape = ide_tape_f(file);
1800 ide_drive_t *drive = tape->drive;
1801 ssize_t actually_written = 0;
1803 u16 ctl = *(u16 *)&tape->caps[12];
1805 /* The drive is write protected. */
1806 if (tape->write_prot)
1809 debug_log(DBG_CHRDEV, "Enter %s, count %Zd\n", __func__, count);
1811 /* Initialize write operation */
1812 if (tape->chrdev_dir != IDETAPE_DIR_WRITE) {
1813 if (tape->chrdev_dir == IDETAPE_DIR_READ)
1814 ide_tape_discard_merge_buffer(drive, 1);
1815 if (tape->merge_bh || tape->merge_bh_size) {
1816 printk(KERN_ERR "ide-tape: merge_bh_size "
1817 "should be 0 now\n");
1818 tape->merge_bh_size = 0;
1820 tape->merge_bh = ide_tape_kmalloc_buffer(tape, 0, 0);
1821 if (!tape->merge_bh)
1823 tape->chrdev_dir = IDETAPE_DIR_WRITE;
1824 idetape_init_merge_buffer(tape);
1827 * Issue a write 0 command to ensure that DSC handshake is
1828 * switched from completion mode to buffer available mode. No
1829 * point in issuing this if DSC overlap isn't supported, some
1830 * drives (Seagate STT3401A) will return an error.
1832 if (drive->dsc_overlap) {
1833 ssize_t retval = idetape_queue_rw_tail(drive,
1834 REQ_IDETAPE_WRITE, 0,
1837 ide_tape_kfree_buffer(tape);
1838 tape->merge_bh = NULL;
1839 tape->chrdev_dir = IDETAPE_DIR_NONE;
1846 if (tape->merge_bh_size) {
1847 if (tape->merge_bh_size >= tape->buffer_size) {
1848 printk(KERN_ERR "ide-tape: bug: merge buf too big\n");
1849 tape->merge_bh_size = 0;
1851 actually_written = min((unsigned int)
1852 (tape->buffer_size - tape->merge_bh_size),
1853 (unsigned int)count);
1854 if (idetape_copy_stage_from_user(tape, buf, actually_written))
1856 buf += actually_written;
1857 tape->merge_bh_size += actually_written;
1858 count -= actually_written;
1860 if (tape->merge_bh_size == tape->buffer_size) {
1862 tape->merge_bh_size = 0;
1863 retval = idetape_add_chrdev_write_request(drive, ctl);
1868 while (count >= tape->buffer_size) {
1870 if (idetape_copy_stage_from_user(tape, buf, tape->buffer_size))
1872 buf += tape->buffer_size;
1873 count -= tape->buffer_size;
1874 retval = idetape_add_chrdev_write_request(drive, ctl);
1875 actually_written += tape->buffer_size;
1880 actually_written += count;
1881 if (idetape_copy_stage_from_user(tape, buf, count))
1883 tape->merge_bh_size += count;
1885 return ret ? ret : actually_written;
1888 static int idetape_write_filemark(ide_drive_t *drive)
1890 struct ide_atapi_pc pc;
1892 /* Write a filemark */
1893 idetape_create_write_filemark_cmd(drive, &pc, 1);
1894 if (idetape_queue_pc_tail(drive, &pc)) {
1895 printk(KERN_ERR "ide-tape: Couldn't write a filemark\n");
1902 * Called from idetape_chrdev_ioctl when the general mtio MTIOCTOP ioctl is
1905 * Note: MTBSF and MTBSFM are not supported when the tape doesn't support
1906 * spacing over filemarks in the reverse direction. In this case, MTFSFM is also
1907 * usually not supported.
1909 * The following commands are currently not supported:
1911 * MTFSS, MTBSS, MTWSM, MTSETDENSITY, MTSETDRVBUFFER, MT_ST_BOOLEANS,
1912 * MT_ST_WRITE_THRESHOLD.
1914 static int idetape_mtioctop(ide_drive_t *drive, short mt_op, int mt_count)
1916 idetape_tape_t *tape = drive->driver_data;
1917 struct ide_atapi_pc pc;
1920 debug_log(DBG_ERR, "Handling MTIOCTOP ioctl: mt_op=%d, mt_count=%d\n",
1930 return idetape_space_over_filemarks(drive, mt_op, mt_count);
1937 if (tape->write_prot)
1939 ide_tape_discard_merge_buffer(drive, 1);
1940 for (i = 0; i < mt_count; i++) {
1941 retval = idetape_write_filemark(drive);
1947 ide_tape_discard_merge_buffer(drive, 0);
1948 if (idetape_rewind_tape(drive))
1952 ide_tape_discard_merge_buffer(drive, 0);
1953 idetape_create_load_unload_cmd(drive, &pc,
1954 IDETAPE_LU_LOAD_MASK);
1955 return idetape_queue_pc_tail(drive, &pc);
1959 * If door is locked, attempt to unlock before
1960 * attempting to eject.
1962 if (tape->door_locked) {
1963 if (idetape_create_prevent_cmd(drive, &pc, 0))
1964 if (!idetape_queue_pc_tail(drive, &pc))
1965 tape->door_locked = DOOR_UNLOCKED;
1967 ide_tape_discard_merge_buffer(drive, 0);
1968 idetape_create_load_unload_cmd(drive, &pc,
1969 !IDETAPE_LU_LOAD_MASK);
1970 retval = idetape_queue_pc_tail(drive, &pc);
1972 clear_bit(IDE_AFLAG_MEDIUM_PRESENT, &drive->atapi_flags);
1975 ide_tape_discard_merge_buffer(drive, 0);
1976 return idetape_flush_tape_buffers(drive);
1978 ide_tape_discard_merge_buffer(drive, 0);
1979 idetape_create_load_unload_cmd(drive, &pc,
1980 IDETAPE_LU_RETENSION_MASK | IDETAPE_LU_LOAD_MASK);
1981 return idetape_queue_pc_tail(drive, &pc);
1983 idetape_create_space_cmd(&pc, 0, IDETAPE_SPACE_TO_EOD);
1984 return idetape_queue_pc_tail(drive, &pc);
1986 (void)idetape_rewind_tape(drive);
1987 idetape_create_erase_cmd(&pc);
1988 return idetape_queue_pc_tail(drive, &pc);
1991 if (mt_count < tape->blk_size ||
1992 mt_count % tape->blk_size)
1994 tape->user_bs_factor = mt_count / tape->blk_size;
1995 clear_bit(IDE_AFLAG_DETECT_BS, &drive->atapi_flags);
1997 set_bit(IDE_AFLAG_DETECT_BS, &drive->atapi_flags);
2000 ide_tape_discard_merge_buffer(drive, 0);
2001 return idetape_position_tape(drive,
2002 mt_count * tape->user_bs_factor, tape->partition, 0);
2004 ide_tape_discard_merge_buffer(drive, 0);
2005 return idetape_position_tape(drive, 0, mt_count, 0);
2009 if (!idetape_create_prevent_cmd(drive, &pc, 1))
2011 retval = idetape_queue_pc_tail(drive, &pc);
2014 tape->door_locked = DOOR_EXPLICITLY_LOCKED;
2017 if (!idetape_create_prevent_cmd(drive, &pc, 0))
2019 retval = idetape_queue_pc_tail(drive, &pc);
2022 tape->door_locked = DOOR_UNLOCKED;
2025 printk(KERN_ERR "ide-tape: MTIO operation %d not supported\n",
2032 * Our character device ioctls. General mtio.h magnetic io commands are
2033 * supported here, and not in the corresponding block interface. Our own
2034 * ide-tape ioctls are supported on both interfaces.
2036 static int idetape_chrdev_ioctl(struct inode *inode, struct file *file,
2037 unsigned int cmd, unsigned long arg)
2039 struct ide_tape_obj *tape = ide_tape_f(file);
2040 ide_drive_t *drive = tape->drive;
2044 int block_offset = 0, position = tape->first_frame;
2045 void __user *argp = (void __user *)arg;
2047 debug_log(DBG_CHRDEV, "Enter %s, cmd=%u\n", __func__, cmd);
2049 if (tape->chrdev_dir == IDETAPE_DIR_WRITE) {
2050 ide_tape_flush_merge_buffer(drive);
2051 idetape_flush_tape_buffers(drive);
2053 if (cmd == MTIOCGET || cmd == MTIOCPOS) {
2054 block_offset = tape->merge_bh_size /
2055 (tape->blk_size * tape->user_bs_factor);
2056 position = idetape_read_position(drive);
2062 if (copy_from_user(&mtop, argp, sizeof(struct mtop)))
2064 return idetape_mtioctop(drive, mtop.mt_op, mtop.mt_count);
2066 memset(&mtget, 0, sizeof(struct mtget));
2067 mtget.mt_type = MT_ISSCSI2;
2068 mtget.mt_blkno = position / tape->user_bs_factor - block_offset;
2070 ((tape->blk_size * tape->user_bs_factor)
2071 << MT_ST_BLKSIZE_SHIFT) & MT_ST_BLKSIZE_MASK;
2073 if (tape->drv_write_prot)
2074 mtget.mt_gstat |= GMT_WR_PROT(0xffffffff);
2076 if (copy_to_user(argp, &mtget, sizeof(struct mtget)))
2080 mtpos.mt_blkno = position / tape->user_bs_factor - block_offset;
2081 if (copy_to_user(argp, &mtpos, sizeof(struct mtpos)))
2085 if (tape->chrdev_dir == IDETAPE_DIR_READ)
2086 ide_tape_discard_merge_buffer(drive, 1);
2087 return idetape_blkdev_ioctl(drive, cmd, arg);
2092 * Do a mode sense page 0 with block descriptor and if it succeeds set the tape
2093 * block size with the reported value.
2095 static void ide_tape_get_bsize_from_bdesc(ide_drive_t *drive)
2097 idetape_tape_t *tape = drive->driver_data;
2098 struct ide_atapi_pc pc;
2100 idetape_create_mode_sense_cmd(&pc, IDETAPE_BLOCK_DESCRIPTOR);
2101 if (idetape_queue_pc_tail(drive, &pc)) {
2102 printk(KERN_ERR "ide-tape: Can't get block descriptor\n");
2103 if (tape->blk_size == 0) {
2104 printk(KERN_WARNING "ide-tape: Cannot deal with zero "
2105 "block size, assuming 32k\n");
2106 tape->blk_size = 32768;
2110 tape->blk_size = (pc.buf[4 + 5] << 16) +
2111 (pc.buf[4 + 6] << 8) +
2113 tape->drv_write_prot = (pc.buf[2] & 0x80) >> 7;
2116 static int idetape_chrdev_open(struct inode *inode, struct file *filp)
2118 unsigned int minor = iminor(inode), i = minor & ~0xc0;
2120 idetape_tape_t *tape;
2121 struct ide_atapi_pc pc;
2124 if (i >= MAX_HWIFS * MAX_DRIVES)
2128 tape = ide_tape_chrdev_get(i);
2134 debug_log(DBG_CHRDEV, "Enter %s\n", __func__);
2137 * We really want to do nonseekable_open(inode, filp); here, but some
2138 * versions of tar incorrectly call lseek on tapes and bail out if that
2139 * fails. So we disallow pread() and pwrite(), but permit lseeks.
2141 filp->f_mode &= ~(FMODE_PREAD | FMODE_PWRITE);
2143 drive = tape->drive;
2145 filp->private_data = tape;
2147 if (test_and_set_bit(IDE_AFLAG_BUSY, &drive->atapi_flags)) {
2152 retval = idetape_wait_ready(drive, 60 * HZ);
2154 clear_bit(IDE_AFLAG_BUSY, &drive->atapi_flags);
2155 printk(KERN_ERR "ide-tape: %s: drive not ready\n", tape->name);
2159 idetape_read_position(drive);
2160 if (!test_bit(IDE_AFLAG_ADDRESS_VALID, &drive->atapi_flags))
2161 (void)idetape_rewind_tape(drive);
2163 /* Read block size and write protect status from drive. */
2164 ide_tape_get_bsize_from_bdesc(drive);
2166 /* Set write protect flag if device is opened as read-only. */
2167 if ((filp->f_flags & O_ACCMODE) == O_RDONLY)
2168 tape->write_prot = 1;
2170 tape->write_prot = tape->drv_write_prot;
2172 /* Make sure drive isn't write protected if user wants to write. */
2173 if (tape->write_prot) {
2174 if ((filp->f_flags & O_ACCMODE) == O_WRONLY ||
2175 (filp->f_flags & O_ACCMODE) == O_RDWR) {
2176 clear_bit(IDE_AFLAG_BUSY, &drive->atapi_flags);
2182 /* Lock the tape drive door so user can't eject. */
2183 if (tape->chrdev_dir == IDETAPE_DIR_NONE) {
2184 if (idetape_create_prevent_cmd(drive, &pc, 1)) {
2185 if (!idetape_queue_pc_tail(drive, &pc)) {
2186 if (tape->door_locked != DOOR_EXPLICITLY_LOCKED)
2187 tape->door_locked = DOOR_LOCKED;
2200 static void idetape_write_release(ide_drive_t *drive, unsigned int minor)
2202 idetape_tape_t *tape = drive->driver_data;
2204 ide_tape_flush_merge_buffer(drive);
2205 tape->merge_bh = ide_tape_kmalloc_buffer(tape, 1, 0);
2206 if (tape->merge_bh != NULL) {
2207 idetape_pad_zeros(drive, tape->blk_size *
2208 (tape->user_bs_factor - 1));
2209 ide_tape_kfree_buffer(tape);
2210 tape->merge_bh = NULL;
2212 idetape_write_filemark(drive);
2213 idetape_flush_tape_buffers(drive);
2214 idetape_flush_tape_buffers(drive);
2217 static int idetape_chrdev_release(struct inode *inode, struct file *filp)
2219 struct ide_tape_obj *tape = ide_tape_f(filp);
2220 ide_drive_t *drive = tape->drive;
2221 struct ide_atapi_pc pc;
2222 unsigned int minor = iminor(inode);
2225 tape = drive->driver_data;
2227 debug_log(DBG_CHRDEV, "Enter %s\n", __func__);
2229 if (tape->chrdev_dir == IDETAPE_DIR_WRITE)
2230 idetape_write_release(drive, minor);
2231 if (tape->chrdev_dir == IDETAPE_DIR_READ) {
2233 ide_tape_discard_merge_buffer(drive, 1);
2236 if (minor < 128 && test_bit(IDE_AFLAG_MEDIUM_PRESENT, &drive->atapi_flags))
2237 (void) idetape_rewind_tape(drive);
2238 if (tape->chrdev_dir == IDETAPE_DIR_NONE) {
2239 if (tape->door_locked == DOOR_LOCKED) {
2240 if (idetape_create_prevent_cmd(drive, &pc, 0)) {
2241 if (!idetape_queue_pc_tail(drive, &pc))
2242 tape->door_locked = DOOR_UNLOCKED;
2246 clear_bit(IDE_AFLAG_BUSY, &drive->atapi_flags);
2252 static void idetape_get_inquiry_results(ide_drive_t *drive)
2254 idetape_tape_t *tape = drive->driver_data;
2255 struct ide_atapi_pc pc;
2256 char fw_rev[4], vendor_id[8], product_id[16];
2258 idetape_create_inquiry_cmd(&pc);
2259 if (idetape_queue_pc_tail(drive, &pc)) {
2260 printk(KERN_ERR "ide-tape: %s: can't get INQUIRY results\n",
2264 memcpy(vendor_id, &pc.buf[8], 8);
2265 memcpy(product_id, &pc.buf[16], 16);
2266 memcpy(fw_rev, &pc.buf[32], 4);
2268 ide_fixstring(vendor_id, 8, 0);
2269 ide_fixstring(product_id, 16, 0);
2270 ide_fixstring(fw_rev, 4, 0);
2272 printk(KERN_INFO "ide-tape: %s <-> %s: %.8s %.16s rev %.4s\n",
2273 drive->name, tape->name, vendor_id, product_id, fw_rev);
2277 * Ask the tape about its various parameters. In particular, we will adjust our
2278 * data transfer buffer size to the recommended value as returned by the tape.
2280 static void idetape_get_mode_sense_results(ide_drive_t *drive)
2282 idetape_tape_t *tape = drive->driver_data;
2283 struct ide_atapi_pc pc;
2285 u8 speed, max_speed;
2287 idetape_create_mode_sense_cmd(&pc, IDETAPE_CAPABILITIES_PAGE);
2288 if (idetape_queue_pc_tail(drive, &pc)) {
2289 printk(KERN_ERR "ide-tape: Can't get tape parameters - assuming"
2290 " some default values\n");
2291 tape->blk_size = 512;
2292 put_unaligned(52, (u16 *)&tape->caps[12]);
2293 put_unaligned(540, (u16 *)&tape->caps[14]);
2294 put_unaligned(6*52, (u16 *)&tape->caps[16]);
2297 caps = pc.buf + 4 + pc.buf[3];
2299 /* convert to host order and save for later use */
2300 speed = be16_to_cpup((__be16 *)&caps[14]);
2301 max_speed = be16_to_cpup((__be16 *)&caps[8]);
2303 *(u16 *)&caps[8] = max_speed;
2304 *(u16 *)&caps[12] = be16_to_cpup((__be16 *)&caps[12]);
2305 *(u16 *)&caps[14] = speed;
2306 *(u16 *)&caps[16] = be16_to_cpup((__be16 *)&caps[16]);
2309 printk(KERN_INFO "ide-tape: %s: invalid tape speed "
2310 "(assuming 650KB/sec)\n", drive->name);
2311 *(u16 *)&caps[14] = 650;
2314 printk(KERN_INFO "ide-tape: %s: invalid max_speed "
2315 "(assuming 650KB/sec)\n", drive->name);
2316 *(u16 *)&caps[8] = 650;
2319 memcpy(&tape->caps, caps, 20);
2321 tape->blk_size = 512;
2322 else if (caps[7] & 0x04)
2323 tape->blk_size = 1024;
2326 #ifdef CONFIG_IDE_PROC_FS
2327 #define ide_tape_devset_get(name, field) \
2328 static int get_##name(ide_drive_t *drive) \
2330 idetape_tape_t *tape = drive->driver_data; \
2331 return tape->field; \
2334 #define ide_tape_devset_set(name, field) \
2335 static int set_##name(ide_drive_t *drive, int arg) \
2337 idetape_tape_t *tape = drive->driver_data; \
2338 tape->field = arg; \
2342 #define ide_tape_devset_rw(_name, _min, _max, _field, _mulf, _divf) \
2343 ide_tape_devset_get(_name, _field) \
2344 ide_tape_devset_set(_name, _field) \
2345 __IDE_DEVSET(_name, S_RW, _min, _max, get_##_name, set_##_name, _mulf, _divf)
2347 #define ide_tape_devset_r(_name, _min, _max, _field, _mulf, _divf) \
2348 ide_tape_devset_get(_name, _field) \
2349 __IDE_DEVSET(_name, S_READ, _min, _max, get_##_name, NULL, _mulf, _divf)
2351 static int mulf_tdsc(ide_drive_t *drive) { return 1000; }
2352 static int divf_tdsc(ide_drive_t *drive) { return HZ; }
2353 static int divf_buffer(ide_drive_t *drive) { return 2; }
2354 static int divf_buffer_size(ide_drive_t *drive) { return 1024; }
2356 ide_devset_rw(dsc_overlap, 0, 1, dsc_overlap);
2358 ide_tape_devset_rw(debug_mask, 0, 0xffff, debug_mask, NULL, NULL);
2359 ide_tape_devset_rw(tdsc, IDETAPE_DSC_RW_MIN, IDETAPE_DSC_RW_MAX,
2360 best_dsc_rw_freq, mulf_tdsc, divf_tdsc);
2362 ide_tape_devset_r(avg_speed, 0, 0xffff, avg_speed, NULL, NULL);
2363 ide_tape_devset_r(speed, 0, 0xffff, caps[14], NULL, NULL);
2364 ide_tape_devset_r(buffer, 0, 0xffff, caps[16], NULL, divf_buffer);
2365 ide_tape_devset_r(buffer_size, 0, 0xffff, buffer_size, NULL, divf_buffer_size);
2367 static const struct ide_devset *idetape_settings[] = {
2368 &ide_devset_avg_speed,
2370 &ide_devset_buffer_size,
2371 &ide_devset_debug_mask,
2372 &ide_devset_dsc_overlap,
2380 * The function below is called to:
2382 * 1. Initialize our various state variables.
2383 * 2. Ask the tape for its capabilities.
2384 * 3. Allocate a buffer which will be used for data transfer. The buffer size
2385 * is chosen based on the recommendation which we received in step 2.
2387 * Note that at this point ide.c already assigned us an irq, so that we can
2388 * queue requests here and wait for their completion.
2390 static void idetape_setup(ide_drive_t *drive, idetape_tape_t *tape, int minor)
2396 u16 *ctl = (u16 *)&tape->caps[12];
2398 drive->pc_callback = ide_tape_callback;
2400 spin_lock_init(&tape->lock);
2401 drive->dsc_overlap = 1;
2402 if (drive->hwif->host_flags & IDE_HFLAG_NO_DSC) {
2403 printk(KERN_INFO "ide-tape: %s: disabling DSC overlap\n",
2405 drive->dsc_overlap = 0;
2407 /* Seagate Travan drives do not support DSC overlap. */
2408 if (strstr((char *)&drive->id[ATA_ID_PROD], "Seagate STT3401"))
2409 drive->dsc_overlap = 0;
2410 tape->minor = minor;
2411 tape->name[0] = 'h';
2412 tape->name[1] = 't';
2413 tape->name[2] = '0' + minor;
2414 tape->chrdev_dir = IDETAPE_DIR_NONE;
2415 tape->pc = tape->pc_stack;
2417 *((u16 *)&gcw) = drive->id[ATA_ID_CONFIG];
2419 /* Command packet DRQ type */
2420 if (((gcw[0] & 0x60) >> 5) == 1)
2421 set_bit(IDE_AFLAG_DRQ_INTERRUPT, &drive->atapi_flags);
2423 idetape_get_inquiry_results(drive);
2424 idetape_get_mode_sense_results(drive);
2425 ide_tape_get_bsize_from_bdesc(drive);
2426 tape->user_bs_factor = 1;
2427 tape->buffer_size = *ctl * tape->blk_size;
2428 while (tape->buffer_size > 0xffff) {
2429 printk(KERN_NOTICE "ide-tape: decreasing stage size\n");
2431 tape->buffer_size = *ctl * tape->blk_size;
2433 buffer_size = tape->buffer_size;
2434 tape->pages_per_buffer = buffer_size / PAGE_SIZE;
2435 if (buffer_size % PAGE_SIZE) {
2436 tape->pages_per_buffer++;
2437 tape->excess_bh_size = PAGE_SIZE - buffer_size % PAGE_SIZE;
2440 /* select the "best" DSC read/write polling freq */
2441 speed = max(*(u16 *)&tape->caps[14], *(u16 *)&tape->caps[8]);
2443 t = (IDETAPE_FIFO_THRESHOLD * tape->buffer_size * HZ) / (speed * 1000);
2446 * Ensure that the number we got makes sense; limit it within
2447 * IDETAPE_DSC_RW_MIN and IDETAPE_DSC_RW_MAX.
2449 tape->best_dsc_rw_freq = clamp_t(unsigned long, t, IDETAPE_DSC_RW_MIN,
2450 IDETAPE_DSC_RW_MAX);
2451 printk(KERN_INFO "ide-tape: %s <-> %s: %dKBps, %d*%dkB buffer, "
2453 drive->name, tape->name, *(u16 *)&tape->caps[14],
2454 (*(u16 *)&tape->caps[16] * 512) / tape->buffer_size,
2455 tape->buffer_size / 1024,
2456 tape->best_dsc_rw_freq * 1000 / HZ,
2457 drive->using_dma ? ", DMA":"");
2459 ide_proc_register_driver(drive, tape->driver);
2462 static void ide_tape_remove(ide_drive_t *drive)
2464 idetape_tape_t *tape = drive->driver_data;
2466 ide_proc_unregister_driver(drive, tape->driver);
2468 ide_unregister_region(tape->disk);
2473 static void ide_tape_release(struct kref *kref)
2475 struct ide_tape_obj *tape = to_ide_tape(kref);
2476 ide_drive_t *drive = tape->drive;
2477 struct gendisk *g = tape->disk;
2479 BUG_ON(tape->merge_bh_size);
2481 drive->dsc_overlap = 0;
2482 drive->driver_data = NULL;
2483 device_destroy(idetape_sysfs_class, MKDEV(IDETAPE_MAJOR, tape->minor));
2484 device_destroy(idetape_sysfs_class,
2485 MKDEV(IDETAPE_MAJOR, tape->minor + 128));
2486 idetape_devs[tape->minor] = NULL;
2487 g->private_data = NULL;
2492 #ifdef CONFIG_IDE_PROC_FS
2493 static int proc_idetape_read_name
2494 (char *page, char **start, off_t off, int count, int *eof, void *data)
2496 ide_drive_t *drive = (ide_drive_t *) data;
2497 idetape_tape_t *tape = drive->driver_data;
2501 len = sprintf(out, "%s\n", tape->name);
2502 PROC_IDE_READ_RETURN(page, start, off, count, eof, len);
2505 static ide_proc_entry_t idetape_proc[] = {
2506 { "capacity", S_IFREG|S_IRUGO, proc_ide_read_capacity, NULL },
2507 { "name", S_IFREG|S_IRUGO, proc_idetape_read_name, NULL },
2508 { NULL, 0, NULL, NULL }
2512 static int ide_tape_probe(ide_drive_t *);
2514 static ide_driver_t idetape_driver = {
2516 .owner = THIS_MODULE,
2518 .bus = &ide_bus_type,
2520 .probe = ide_tape_probe,
2521 .remove = ide_tape_remove,
2522 .version = IDETAPE_VERSION,
2524 .do_request = idetape_do_request,
2525 .end_request = idetape_end_request,
2526 .error = __ide_error,
2527 #ifdef CONFIG_IDE_PROC_FS
2528 .proc = idetape_proc,
2529 .settings = idetape_settings,
2533 /* Our character device supporting functions, passed to register_chrdev. */
2534 static const struct file_operations idetape_fops = {
2535 .owner = THIS_MODULE,
2536 .read = idetape_chrdev_read,
2537 .write = idetape_chrdev_write,
2538 .ioctl = idetape_chrdev_ioctl,
2539 .open = idetape_chrdev_open,
2540 .release = idetape_chrdev_release,
2543 static int idetape_open(struct inode *inode, struct file *filp)
2545 struct gendisk *disk = inode->i_bdev->bd_disk;
2546 struct ide_tape_obj *tape;
2548 tape = ide_tape_get(disk);
2555 static int idetape_release(struct inode *inode, struct file *filp)
2557 struct gendisk *disk = inode->i_bdev->bd_disk;
2558 struct ide_tape_obj *tape = ide_tape_g(disk);
2565 static int idetape_ioctl(struct inode *inode, struct file *file,
2566 unsigned int cmd, unsigned long arg)
2568 struct block_device *bdev = inode->i_bdev;
2569 struct ide_tape_obj *tape = ide_tape_g(bdev->bd_disk);
2570 ide_drive_t *drive = tape->drive;
2571 int err = generic_ide_ioctl(drive, file, bdev, cmd, arg);
2573 err = idetape_blkdev_ioctl(drive, cmd, arg);
2577 static struct block_device_operations idetape_block_ops = {
2578 .owner = THIS_MODULE,
2579 .open = idetape_open,
2580 .release = idetape_release,
2581 .ioctl = idetape_ioctl,
2584 static int ide_tape_probe(ide_drive_t *drive)
2586 idetape_tape_t *tape;
2590 if (!strstr("ide-tape", drive->driver_req))
2593 if (drive->media != ide_tape)
2596 if (drive->id_read == 1 && !ide_check_atapi_device(drive, DRV_NAME)) {
2597 printk(KERN_ERR "ide-tape: %s: not supported by this version of"
2598 " the driver\n", drive->name);
2601 tape = kzalloc(sizeof(idetape_tape_t), GFP_KERNEL);
2603 printk(KERN_ERR "ide-tape: %s: Can't allocate a tape struct\n",
2608 g = alloc_disk(1 << PARTN_BITS);
2612 ide_init_disk(g, drive);
2614 kref_init(&tape->kref);
2616 tape->drive = drive;
2617 tape->driver = &idetape_driver;
2620 g->private_data = &tape->driver;
2622 drive->driver_data = tape;
2624 mutex_lock(&idetape_ref_mutex);
2625 for (minor = 0; idetape_devs[minor]; minor++)
2627 idetape_devs[minor] = tape;
2628 mutex_unlock(&idetape_ref_mutex);
2630 idetape_setup(drive, tape, minor);
2632 device_create_drvdata(idetape_sysfs_class, &drive->gendev,
2633 MKDEV(IDETAPE_MAJOR, minor), NULL,
2635 device_create_drvdata(idetape_sysfs_class, &drive->gendev,
2636 MKDEV(IDETAPE_MAJOR, minor + 128), NULL,
2639 g->fops = &idetape_block_ops;
2640 ide_register_region(g);
2650 static void __exit idetape_exit(void)
2652 driver_unregister(&idetape_driver.gen_driver);
2653 class_destroy(idetape_sysfs_class);
2654 unregister_chrdev(IDETAPE_MAJOR, "ht");
2657 static int __init idetape_init(void)
2660 idetape_sysfs_class = class_create(THIS_MODULE, "ide_tape");
2661 if (IS_ERR(idetape_sysfs_class)) {
2662 idetape_sysfs_class = NULL;
2663 printk(KERN_ERR "Unable to create sysfs class for ide tapes\n");
2668 if (register_chrdev(IDETAPE_MAJOR, "ht", &idetape_fops)) {
2669 printk(KERN_ERR "ide-tape: Failed to register chrdev"
2672 goto out_free_class;
2675 error = driver_register(&idetape_driver.gen_driver);
2677 goto out_free_driver;
2682 driver_unregister(&idetape_driver.gen_driver);
2684 class_destroy(idetape_sysfs_class);
2689 MODULE_ALIAS("ide:*m-tape*");
2690 module_init(idetape_init);
2691 module_exit(idetape_exit);
2692 MODULE_ALIAS_CHARDEV_MAJOR(IDETAPE_MAJOR);
2693 MODULE_DESCRIPTION("ATAPI Streaming TAPE Driver");
2694 MODULE_LICENSE("GPL");