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 */
61 /* define to see debug info */
62 #define IDETAPE_DEBUG_LOG 0
65 #define debug_log(lvl, fmt, args...) \
67 if (tape->debug_mask & lvl) \
68 printk(KERN_INFO "ide-tape: " fmt, ## args); \
71 #define debug_log(lvl, fmt, args...) do {} while (0)
74 /**************************** Tunable parameters *****************************/
76 * After each failed packet command we issue a request sense command and retry
77 * the packet command IDETAPE_MAX_PC_RETRIES times.
79 * Setting IDETAPE_MAX_PC_RETRIES to 0 will disable retries.
81 #define IDETAPE_MAX_PC_RETRIES 3
84 * Some drives (for example, Seagate STT3401A Travan) require a very long
85 * timeout, because they don't return an interrupt or clear their busy bit
86 * until after the command completes (even retension commands).
88 #define IDETAPE_WAIT_CMD (900*HZ)
91 * The following parameter is used to select the point in the internal tape fifo
92 * in which we will start to refill the buffer. Decreasing the following
93 * parameter will improve the system's latency and interactive response, while
94 * using a high value might improve system throughput.
96 #define IDETAPE_FIFO_THRESHOLD 2
99 * DSC polling parameters.
101 * Polling for DSC (a single bit in the status register) is a very important
102 * function in ide-tape. There are two cases in which we poll for DSC:
104 * 1. Before a read/write packet command, to ensure that we can transfer data
105 * from/to the tape's data buffers, without causing an actual media access.
106 * In case the tape is not ready yet, we take out our request from the device
107 * request queue, so that ide.c could service requests from the other device
108 * on the same interface in the meantime.
110 * 2. After the successful initialization of a "media access packet command",
111 * which is a command that can take a long time to complete (the interval can
112 * range from several seconds to even an hour). Again, we postpone our request
113 * in the middle to free the bus for the other device. The polling frequency
114 * here should be lower than the read/write frequency since those media access
115 * commands are slow. We start from a "fast" frequency - IDETAPE_DSC_MA_FAST
116 * (1 second), and if we don't receive DSC after IDETAPE_DSC_MA_THRESHOLD
117 * (5 min), we switch it to a lower frequency - IDETAPE_DSC_MA_SLOW (1 min).
119 * We also set a timeout for the timer, in case something goes wrong. The
120 * timeout should be longer then the maximum execution time of a tape operation.
124 #define IDETAPE_DSC_RW_MIN 5*HZ/100 /* 50 msec */
125 #define IDETAPE_DSC_RW_MAX 40*HZ/100 /* 400 msec */
126 #define IDETAPE_DSC_RW_TIMEOUT 2*60*HZ /* 2 minutes */
127 #define IDETAPE_DSC_MA_FAST 2*HZ /* 2 seconds */
128 #define IDETAPE_DSC_MA_THRESHOLD 5*60*HZ /* 5 minutes */
129 #define IDETAPE_DSC_MA_SLOW 30*HZ /* 30 seconds */
130 #define IDETAPE_DSC_MA_TIMEOUT 2*60*60*HZ /* 2 hours */
132 /*************************** End of tunable parameters ***********************/
134 /* tape directions */
136 IDETAPE_DIR_NONE = (1 << 0),
137 IDETAPE_DIR_READ = (1 << 1),
138 IDETAPE_DIR_WRITE = (1 << 2),
144 struct idetape_bh *b_reqnext;
148 /* Tape door status */
149 #define DOOR_UNLOCKED 0
150 #define DOOR_LOCKED 1
151 #define DOOR_EXPLICITLY_LOCKED 2
153 /* Some defines for the SPACE command */
154 #define IDETAPE_SPACE_OVER_FILEMARK 1
155 #define IDETAPE_SPACE_TO_EOD 3
157 /* Some defines for the LOAD UNLOAD command */
158 #define IDETAPE_LU_LOAD_MASK 1
159 #define IDETAPE_LU_RETENSION_MASK 2
160 #define IDETAPE_LU_EOT_MASK 4
163 * Special requests for our block device strategy routine.
165 * In order to service a character device command, we add special requests to
166 * the tail of our block device request queue and wait for their completion.
170 REQ_IDETAPE_PC1 = (1 << 0), /* packet command (first stage) */
171 REQ_IDETAPE_PC2 = (1 << 1), /* packet command (second stage) */
172 REQ_IDETAPE_READ = (1 << 2),
173 REQ_IDETAPE_WRITE = (1 << 3),
176 /* Error codes returned in rq->errors to the higher part of the driver. */
177 #define IDETAPE_ERROR_GENERAL 101
178 #define IDETAPE_ERROR_FILEMARK 102
179 #define IDETAPE_ERROR_EOD 103
181 /* Structures related to the SELECT SENSE / MODE SENSE packet commands. */
182 #define IDETAPE_BLOCK_DESCRIPTOR 0
183 #define IDETAPE_CAPABILITIES_PAGE 0x2a
186 * Most of our global data which we need to save even as we leave the driver due
187 * to an interrupt or a timer event is stored in the struct defined below.
189 typedef struct ide_tape_obj {
191 ide_driver_t *driver;
192 struct gendisk *disk;
196 * pc points to the current processed packet command.
198 * failed_pc points to the last failed packet command, or contains
199 * NULL if we do not need to retry any packet command. This is
200 * required since an additional packet command is needed before the
201 * retry, to get detailed information on what went wrong.
203 /* Current packet command */
204 struct ide_atapi_pc *pc;
205 /* Last failed packet command */
206 struct ide_atapi_pc *failed_pc;
207 /* used by REQ_IDETAPE_{READ,WRITE} requests */
208 struct ide_atapi_pc queued_pc;
210 struct ide_atapi_pc request_sense_pc;
211 struct request request_sense_rq;
214 * DSC polling variables.
216 * While polling for DSC we use postponed_rq to postpone the current
217 * request so that ide.c will be able to service pending requests on the
218 * other device. Note that at most we will have only one DSC (usually
219 * data transfer) request in the device request queue.
221 struct request *postponed_rq;
222 /* The time in which we started polling for DSC */
223 unsigned long dsc_polling_start;
224 /* Timer used to poll for dsc */
225 struct timer_list dsc_timer;
226 /* Read/Write dsc polling frequency */
227 unsigned long best_dsc_rw_freq;
228 unsigned long dsc_poll_freq;
229 unsigned long dsc_timeout;
231 /* Read position information */
234 unsigned int first_frame;
236 /* Last error information */
237 u8 sense_key, asc, ascq;
239 /* Character device operation */
243 /* Current character device data transfer direction */
246 /* tape block size, usually 512 or 1024 bytes */
247 unsigned short blk_size;
250 /* Copy of the tape's Capabilities and Mechanical Page */
254 * Active data transfer request parameters.
256 * At most, there is only one ide-tape originated data transfer request
257 * in the device request queue. This allows ide.c to easily service
258 * requests from the other device when we postpone our active request.
261 /* Data buffer size chosen based on the tape's recommendation */
264 struct idetape_bh *merge_bh;
265 /* size of the merge buffer */
267 /* pointer to current buffer head within the merge buffer */
268 struct idetape_bh *bh;
272 int pages_per_buffer;
273 /* Wasted space in each stage */
276 /* protects the ide-tape queue */
279 /* Measures average tape speed */
280 unsigned long avg_time;
284 /* the door is currently locked */
286 /* the tape hardware is write protected */
288 /* the tape is write protected (hardware or opened as read-only) */
294 static DEFINE_MUTEX(idetape_ref_mutex);
296 static struct class *idetape_sysfs_class;
298 #define to_ide_tape(obj) container_of(obj, struct ide_tape_obj, kref)
300 #define ide_tape_g(disk) \
301 container_of((disk)->private_data, struct ide_tape_obj, driver)
303 static void ide_tape_release(struct kref *);
305 static struct ide_tape_obj *ide_tape_get(struct gendisk *disk)
307 struct ide_tape_obj *tape = NULL;
309 mutex_lock(&idetape_ref_mutex);
310 tape = ide_tape_g(disk);
312 if (ide_device_get(tape->drive))
315 kref_get(&tape->kref);
317 mutex_unlock(&idetape_ref_mutex);
321 static void ide_tape_put(struct ide_tape_obj *tape)
323 ide_drive_t *drive = tape->drive;
325 mutex_lock(&idetape_ref_mutex);
326 kref_put(&tape->kref, ide_tape_release);
327 ide_device_put(drive);
328 mutex_unlock(&idetape_ref_mutex);
332 * The variables below are used for the character device interface. Additional
333 * state variables are defined in our ide_drive_t structure.
335 static struct ide_tape_obj *idetape_devs[MAX_HWIFS * MAX_DRIVES];
337 #define ide_tape_f(file) ((file)->private_data)
339 static struct ide_tape_obj *ide_tape_chrdev_get(unsigned int i)
341 struct ide_tape_obj *tape = NULL;
343 mutex_lock(&idetape_ref_mutex);
344 tape = idetape_devs[i];
346 kref_get(&tape->kref);
347 mutex_unlock(&idetape_ref_mutex);
351 static void idetape_input_buffers(ide_drive_t *drive, struct ide_atapi_pc *pc,
354 struct idetape_bh *bh = pc->bh;
359 printk(KERN_ERR "ide-tape: bh == NULL in "
360 "idetape_input_buffers\n");
361 ide_pad_transfer(drive, 0, bcount);
365 (unsigned int)(bh->b_size - atomic_read(&bh->b_count)),
367 drive->hwif->tp_ops->input_data(drive, NULL, bh->b_data +
368 atomic_read(&bh->b_count), count);
370 atomic_add(count, &bh->b_count);
371 if (atomic_read(&bh->b_count) == bh->b_size) {
374 atomic_set(&bh->b_count, 0);
380 static void idetape_output_buffers(ide_drive_t *drive, struct ide_atapi_pc *pc,
383 struct idetape_bh *bh = pc->bh;
388 printk(KERN_ERR "ide-tape: bh == NULL in %s\n",
392 count = min((unsigned int)pc->b_count, (unsigned int)bcount);
393 drive->hwif->tp_ops->output_data(drive, NULL, pc->b_data, count);
396 pc->b_count -= count;
401 pc->b_data = bh->b_data;
402 pc->b_count = atomic_read(&bh->b_count);
408 static void idetape_update_buffers(ide_drive_t *drive, struct ide_atapi_pc *pc)
410 struct idetape_bh *bh = pc->bh;
412 unsigned int bcount = pc->xferred;
414 if (pc->flags & PC_FLAG_WRITING)
418 printk(KERN_ERR "ide-tape: bh == NULL in %s\n",
422 count = min((unsigned int)bh->b_size, (unsigned int)bcount);
423 atomic_set(&bh->b_count, count);
424 if (atomic_read(&bh->b_count) == bh->b_size)
432 * called on each failed packet command retry to analyze the request sense. We
433 * currently do not utilize this information.
435 static void idetape_analyze_error(ide_drive_t *drive, u8 *sense)
437 idetape_tape_t *tape = drive->driver_data;
438 struct ide_atapi_pc *pc = tape->failed_pc;
440 tape->sense_key = sense[2] & 0xF;
441 tape->asc = sense[12];
442 tape->ascq = sense[13];
444 debug_log(DBG_ERR, "pc = %x, sense key = %x, asc = %x, ascq = %x\n",
445 pc->c[0], tape->sense_key, tape->asc, tape->ascq);
447 /* Correct pc->xferred by asking the tape. */
448 if (pc->flags & PC_FLAG_DMA_ERROR) {
449 pc->xferred = pc->req_xfer -
451 get_unaligned_be32(&sense[3]);
452 idetape_update_buffers(drive, pc);
456 * If error was the result of a zero-length read or write command,
457 * with sense key=5, asc=0x22, ascq=0, let it slide. Some drives
458 * (i.e. Seagate STT3401A Travan) don't support 0-length read/writes.
460 if ((pc->c[0] == READ_6 || pc->c[0] == WRITE_6)
462 && pc->c[4] == 0 && pc->c[3] == 0 && pc->c[2] == 0) {
463 if (tape->sense_key == 5) {
464 /* don't report an error, everything's ok */
466 /* don't retry read/write */
467 pc->flags |= PC_FLAG_ABORT;
470 if (pc->c[0] == READ_6 && (sense[2] & 0x80)) {
471 pc->error = IDETAPE_ERROR_FILEMARK;
472 pc->flags |= PC_FLAG_ABORT;
474 if (pc->c[0] == WRITE_6) {
475 if ((sense[2] & 0x40) || (tape->sense_key == 0xd
476 && tape->asc == 0x0 && tape->ascq == 0x2)) {
477 pc->error = IDETAPE_ERROR_EOD;
478 pc->flags |= PC_FLAG_ABORT;
481 if (pc->c[0] == READ_6 || pc->c[0] == WRITE_6) {
482 if (tape->sense_key == 8) {
483 pc->error = IDETAPE_ERROR_EOD;
484 pc->flags |= PC_FLAG_ABORT;
486 if (!(pc->flags & PC_FLAG_ABORT) &&
488 pc->retries = IDETAPE_MAX_PC_RETRIES + 1;
492 /* Free data buffers completely. */
493 static void ide_tape_kfree_buffer(idetape_tape_t *tape)
495 struct idetape_bh *prev_bh, *bh = tape->merge_bh;
498 u32 size = bh->b_size;
501 unsigned int order = fls(size >> PAGE_SHIFT)-1;
504 free_pages((unsigned long)bh->b_data, order);
507 bh->b_data += (1 << order) * PAGE_SIZE;
515 static int idetape_end_request(ide_drive_t *drive, int uptodate, int nr_sects)
517 struct request *rq = HWGROUP(drive)->rq;
518 idetape_tape_t *tape = drive->driver_data;
522 debug_log(DBG_PROCS, "Enter %s\n", __func__);
525 case 0: error = IDETAPE_ERROR_GENERAL; break;
526 case 1: error = 0; break;
527 default: error = uptodate;
531 tape->failed_pc = NULL;
533 if (!blk_special_request(rq)) {
534 ide_end_request(drive, uptodate, nr_sects);
538 spin_lock_irqsave(&tape->lock, flags);
540 ide_end_drive_cmd(drive, 0, 0);
542 spin_unlock_irqrestore(&tape->lock, flags);
546 static void ide_tape_callback(ide_drive_t *drive)
548 idetape_tape_t *tape = drive->driver_data;
549 struct ide_atapi_pc *pc = tape->pc;
550 int uptodate = pc->error ? 0 : 1;
552 debug_log(DBG_PROCS, "Enter %s\n", __func__);
554 if (tape->failed_pc == pc)
555 tape->failed_pc = NULL;
557 if (pc->c[0] == REQUEST_SENSE) {
559 idetape_analyze_error(drive, pc->buf);
561 printk(KERN_ERR "ide-tape: Error in REQUEST SENSE "
562 "itself - Aborting request!\n");
563 } else if (pc->c[0] == READ_6 || pc->c[0] == WRITE_6) {
564 struct request *rq = drive->hwif->hwgroup->rq;
565 int blocks = pc->xferred / tape->blk_size;
567 tape->avg_size += blocks * tape->blk_size;
569 if (time_after_eq(jiffies, tape->avg_time + HZ)) {
570 tape->avg_speed = tape->avg_size * HZ /
571 (jiffies - tape->avg_time) / 1024;
573 tape->avg_time = jiffies;
576 tape->first_frame += blocks;
577 rq->current_nr_sectors -= blocks;
580 uptodate = pc->error;
581 } else if (pc->c[0] == READ_POSITION && uptodate) {
582 u8 *readpos = tape->pc->buf;
584 debug_log(DBG_SENSE, "BOP - %s\n",
585 (readpos[0] & 0x80) ? "Yes" : "No");
586 debug_log(DBG_SENSE, "EOP - %s\n",
587 (readpos[0] & 0x40) ? "Yes" : "No");
589 if (readpos[0] & 0x4) {
590 printk(KERN_INFO "ide-tape: Block location is unknown"
592 clear_bit(IDE_AFLAG_ADDRESS_VALID, &drive->atapi_flags);
595 debug_log(DBG_SENSE, "Block Location - %u\n",
596 be32_to_cpup((__be32 *)&readpos[4]));
598 tape->partition = readpos[1];
599 tape->first_frame = be32_to_cpup((__be32 *)&readpos[4]);
600 set_bit(IDE_AFLAG_ADDRESS_VALID, &drive->atapi_flags);
604 idetape_end_request(drive, uptodate, 0);
607 static void idetape_create_request_sense_cmd(struct ide_atapi_pc *pc)
610 pc->c[0] = REQUEST_SENSE;
616 * Generate a new packet command request in front of the request queue, before
617 * the current request, so that it will be processed immediately, on the next
618 * pass through the driver.
620 static void idetape_queue_pc_head(ide_drive_t *drive, struct ide_atapi_pc *pc,
623 struct ide_tape_obj *tape = drive->driver_data;
625 blk_rq_init(NULL, rq);
626 rq->cmd_type = REQ_TYPE_SPECIAL;
627 rq->cmd_flags |= REQ_PREEMPT;
628 rq->buffer = (char *) pc;
629 rq->rq_disk = tape->disk;
630 memcpy(rq->cmd, pc->c, 12);
631 rq->cmd[13] = REQ_IDETAPE_PC1;
632 ide_do_drive_cmd(drive, rq);
636 * idetape_retry_pc is called when an error was detected during the
637 * last packet command. We queue a request sense packet command in
638 * the head of the request list.
640 static void idetape_retry_pc(ide_drive_t *drive)
642 struct ide_tape_obj *tape = drive->driver_data;
643 struct request *rq = &tape->request_sense_rq;
644 struct ide_atapi_pc *pc = &tape->request_sense_pc;
646 (void)ide_read_error(drive);
647 idetape_create_request_sense_cmd(pc);
648 set_bit(IDE_AFLAG_IGNORE_DSC, &drive->atapi_flags);
649 idetape_queue_pc_head(drive, pc, rq);
653 * Postpone the current request so that ide.c will be able to service requests
654 * from another device on the same hwgroup while we are polling for DSC.
656 static void idetape_postpone_request(ide_drive_t *drive)
658 idetape_tape_t *tape = drive->driver_data;
660 debug_log(DBG_PROCS, "Enter %s\n", __func__);
662 tape->postponed_rq = HWGROUP(drive)->rq;
663 ide_stall_queue(drive, tape->dsc_poll_freq);
666 static void ide_tape_handle_dsc(ide_drive_t *drive)
668 idetape_tape_t *tape = drive->driver_data;
670 /* Media access command */
671 tape->dsc_polling_start = jiffies;
672 tape->dsc_poll_freq = IDETAPE_DSC_MA_FAST;
673 tape->dsc_timeout = jiffies + IDETAPE_DSC_MA_TIMEOUT;
674 /* Allow ide.c to handle other requests */
675 idetape_postpone_request(drive);
678 static int ide_tape_io_buffers(ide_drive_t *drive, struct ide_atapi_pc *pc,
679 unsigned int bcount, int write)
682 idetape_output_buffers(drive, pc, bcount);
684 idetape_input_buffers(drive, pc, bcount);
690 * This is the usual interrupt handler which will be called during a packet
691 * command. We will transfer some of the data (as requested by the drive) and
692 * will re-point interrupt handler to us. When data transfer is finished, we
693 * will act according to the algorithm described before
696 static ide_startstop_t idetape_pc_intr(ide_drive_t *drive)
698 idetape_tape_t *tape = drive->driver_data;
700 return ide_pc_intr(drive, tape->pc, idetape_pc_intr, IDETAPE_WAIT_CMD,
701 NULL, idetape_update_buffers, idetape_retry_pc,
702 ide_tape_handle_dsc, ide_tape_io_buffers);
706 * Packet Command Interface
708 * The current Packet Command is available in tape->pc, and will not change
709 * until we finish handling it. Each packet command is associated with a
710 * callback function that will be called when the command is finished.
712 * The handling will be done in three stages:
714 * 1. idetape_issue_pc will send the packet command to the drive, and will set
715 * the interrupt handler to idetape_pc_intr.
717 * 2. On each interrupt, idetape_pc_intr will be called. This step will be
718 * repeated until the device signals us that no more interrupts will be issued.
720 * 3. ATAPI Tape media access commands have immediate status with a delayed
721 * process. In case of a successful initiation of a media access packet command,
722 * the DSC bit will be set when the actual execution of the command is finished.
723 * Since the tape drive will not issue an interrupt, we have to poll for this
724 * event. In this case, we define the request as "low priority request" by
725 * setting rq_status to IDETAPE_RQ_POSTPONED, set a timer to poll for DSC and
728 * ide.c will then give higher priority to requests which originate from the
729 * other device, until will change rq_status to RQ_ACTIVE.
731 * 4. When the packet command is finished, it will be checked for errors.
733 * 5. In case an error was found, we queue a request sense packet command in
734 * front of the request queue and retry the operation up to
735 * IDETAPE_MAX_PC_RETRIES times.
737 * 6. In case no error was found, or we decided to give up and not to retry
738 * again, the callback function will be called and then we will handle the next
741 static ide_startstop_t idetape_transfer_pc(ide_drive_t *drive)
743 idetape_tape_t *tape = drive->driver_data;
745 return ide_transfer_pc(drive, tape->pc, idetape_pc_intr,
746 IDETAPE_WAIT_CMD, NULL);
749 static ide_startstop_t idetape_issue_pc(ide_drive_t *drive,
750 struct ide_atapi_pc *pc)
752 idetape_tape_t *tape = drive->driver_data;
754 if (tape->pc->c[0] == REQUEST_SENSE &&
755 pc->c[0] == REQUEST_SENSE) {
756 printk(KERN_ERR "ide-tape: possible ide-tape.c bug - "
757 "Two request sense in serial were issued\n");
760 if (tape->failed_pc == NULL && pc->c[0] != REQUEST_SENSE)
761 tape->failed_pc = pc;
762 /* Set the current packet command */
765 if (pc->retries > IDETAPE_MAX_PC_RETRIES ||
766 (pc->flags & PC_FLAG_ABORT)) {
768 * We will "abort" retrying a packet command in case legitimate
769 * error code was received (crossing a filemark, or end of the
770 * media, for example).
772 if (!(pc->flags & PC_FLAG_ABORT)) {
773 if (!(pc->c[0] == TEST_UNIT_READY &&
774 tape->sense_key == 2 && tape->asc == 4 &&
775 (tape->ascq == 1 || tape->ascq == 8))) {
776 printk(KERN_ERR "ide-tape: %s: I/O error, "
777 "pc = %2x, key = %2x, "
778 "asc = %2x, ascq = %2x\n",
779 tape->name, pc->c[0],
780 tape->sense_key, tape->asc,
784 pc->error = IDETAPE_ERROR_GENERAL;
786 tape->failed_pc = NULL;
787 drive->pc_callback(drive);
790 debug_log(DBG_SENSE, "Retry #%d, cmd = %02X\n", pc->retries, pc->c[0]);
794 return ide_issue_pc(drive, pc, idetape_transfer_pc,
795 IDETAPE_WAIT_CMD, NULL);
798 /* A mode sense command is used to "sense" tape parameters. */
799 static void idetape_create_mode_sense_cmd(struct ide_atapi_pc *pc, u8 page_code)
802 pc->c[0] = MODE_SENSE;
803 if (page_code != IDETAPE_BLOCK_DESCRIPTOR)
804 /* DBD = 1 - Don't return block descriptors */
806 pc->c[2] = page_code;
808 * Changed pc->c[3] to 0 (255 will at best return unused info).
810 * For SCSI this byte is defined as subpage instead of high byte
811 * of length and some IDE drives seem to interpret it this way
812 * and return an error when 255 is used.
815 /* We will just discard data in that case */
817 if (page_code == IDETAPE_BLOCK_DESCRIPTOR)
819 else if (page_code == IDETAPE_CAPABILITIES_PAGE)
825 static ide_startstop_t idetape_media_access_finished(ide_drive_t *drive)
827 ide_hwif_t *hwif = drive->hwif;
828 idetape_tape_t *tape = drive->driver_data;
829 struct ide_atapi_pc *pc = tape->pc;
832 stat = hwif->tp_ops->read_status(hwif);
834 if (stat & ATA_DSC) {
835 if (stat & ATA_ERR) {
837 if (pc->c[0] != TEST_UNIT_READY)
838 printk(KERN_ERR "ide-tape: %s: I/O error, ",
840 /* Retry operation */
841 idetape_retry_pc(drive);
846 pc->error = IDETAPE_ERROR_GENERAL;
847 tape->failed_pc = NULL;
849 drive->pc_callback(drive);
853 static void ide_tape_create_rw_cmd(idetape_tape_t *tape,
854 struct ide_atapi_pc *pc, struct request *rq,
857 struct idetape_bh *bh = (struct idetape_bh *)rq->special;
858 unsigned int length = rq->current_nr_sectors;
861 put_unaligned(cpu_to_be32(length), (unsigned int *) &pc->c[1]);
865 pc->buf_size = length * tape->blk_size;
866 pc->req_xfer = pc->buf_size;
867 if (pc->req_xfer == tape->buffer_size)
868 pc->flags |= PC_FLAG_DMA_OK;
870 if (opcode == READ_6) {
872 atomic_set(&bh->b_count, 0);
873 } else if (opcode == WRITE_6) {
875 pc->flags |= PC_FLAG_WRITING;
876 pc->b_data = bh->b_data;
877 pc->b_count = atomic_read(&bh->b_count);
880 memcpy(rq->cmd, pc->c, 12);
883 static ide_startstop_t idetape_do_request(ide_drive_t *drive,
884 struct request *rq, sector_t block)
886 ide_hwif_t *hwif = drive->hwif;
887 idetape_tape_t *tape = drive->driver_data;
888 struct ide_atapi_pc *pc = NULL;
889 struct request *postponed_rq = tape->postponed_rq;
892 debug_log(DBG_SENSE, "sector: %llu, nr_sectors: %lu,"
893 " current_nr_sectors: %u\n",
894 (unsigned long long)rq->sector, rq->nr_sectors,
895 rq->current_nr_sectors);
897 if (!blk_special_request(rq)) {
898 /* We do not support buffer cache originated requests. */
899 printk(KERN_NOTICE "ide-tape: %s: Unsupported request in "
900 "request queue (%d)\n", drive->name, rq->cmd_type);
901 ide_end_request(drive, 0, 0);
905 /* Retry a failed packet command */
906 if (tape->failed_pc && tape->pc->c[0] == REQUEST_SENSE) {
907 pc = tape->failed_pc;
911 if (postponed_rq != NULL)
912 if (rq != postponed_rq) {
913 printk(KERN_ERR "ide-tape: ide-tape.c bug - "
914 "Two DSC requests were queued\n");
915 idetape_end_request(drive, 0, 0);
919 tape->postponed_rq = NULL;
922 * If the tape is still busy, postpone our request and service
923 * the other device meanwhile.
925 stat = hwif->tp_ops->read_status(hwif);
927 if (!drive->dsc_overlap && !(rq->cmd[13] & REQ_IDETAPE_PC2))
928 set_bit(IDE_AFLAG_IGNORE_DSC, &drive->atapi_flags);
930 if (drive->post_reset == 1) {
931 set_bit(IDE_AFLAG_IGNORE_DSC, &drive->atapi_flags);
932 drive->post_reset = 0;
935 if (!test_and_clear_bit(IDE_AFLAG_IGNORE_DSC, &drive->atapi_flags) &&
936 (stat & ATA_DSC) == 0) {
937 if (postponed_rq == NULL) {
938 tape->dsc_polling_start = jiffies;
939 tape->dsc_poll_freq = tape->best_dsc_rw_freq;
940 tape->dsc_timeout = jiffies + IDETAPE_DSC_RW_TIMEOUT;
941 } else if (time_after(jiffies, tape->dsc_timeout)) {
942 printk(KERN_ERR "ide-tape: %s: DSC timeout\n",
944 if (rq->cmd[13] & REQ_IDETAPE_PC2) {
945 idetape_media_access_finished(drive);
948 return ide_do_reset(drive);
950 } else if (time_after(jiffies,
951 tape->dsc_polling_start +
952 IDETAPE_DSC_MA_THRESHOLD))
953 tape->dsc_poll_freq = IDETAPE_DSC_MA_SLOW;
954 idetape_postpone_request(drive);
957 if (rq->cmd[13] & REQ_IDETAPE_READ) {
958 pc = &tape->queued_pc;
959 ide_tape_create_rw_cmd(tape, pc, rq, READ_6);
962 if (rq->cmd[13] & REQ_IDETAPE_WRITE) {
963 pc = &tape->queued_pc;
964 ide_tape_create_rw_cmd(tape, pc, rq, WRITE_6);
967 if (rq->cmd[13] & REQ_IDETAPE_PC1) {
968 pc = (struct ide_atapi_pc *) rq->buffer;
969 rq->cmd[13] &= ~(REQ_IDETAPE_PC1);
970 rq->cmd[13] |= REQ_IDETAPE_PC2;
973 if (rq->cmd[13] & REQ_IDETAPE_PC2) {
974 idetape_media_access_finished(drive);
980 return idetape_issue_pc(drive, pc);
984 * The function below uses __get_free_pages to allocate a data buffer of size
985 * tape->buffer_size (or a bit more). We attempt to combine sequential pages as
988 * It returns a pointer to the newly allocated buffer, or NULL in case of
991 static struct idetape_bh *ide_tape_kmalloc_buffer(idetape_tape_t *tape,
994 struct idetape_bh *prev_bh, *bh, *merge_bh;
995 int pages = tape->pages_per_buffer;
996 unsigned int order, b_allocd;
999 merge_bh = kmalloc(sizeof(struct idetape_bh), GFP_KERNEL);
1004 order = fls(pages) - 1;
1005 bh->b_data = (char *) __get_free_pages(GFP_KERNEL, order);
1008 b_allocd = (1 << order) * PAGE_SIZE;
1012 memset(bh->b_data, 0, b_allocd);
1013 bh->b_reqnext = NULL;
1014 bh->b_size = b_allocd;
1015 atomic_set(&bh->b_count, full ? bh->b_size : 0);
1018 order = fls(pages) - 1;
1019 b_data = (char *) __get_free_pages(GFP_KERNEL, order);
1022 b_allocd = (1 << order) * PAGE_SIZE;
1025 memset(b_data, 0, b_allocd);
1027 /* newly allocated page frames below buffer header or ...*/
1028 if (bh->b_data == b_data + b_allocd) {
1029 bh->b_size += b_allocd;
1030 bh->b_data -= b_allocd;
1032 atomic_add(b_allocd, &bh->b_count);
1035 /* they are above the header */
1036 if (b_data == bh->b_data + bh->b_size) {
1037 bh->b_size += b_allocd;
1039 atomic_add(b_allocd, &bh->b_count);
1043 bh = kmalloc(sizeof(struct idetape_bh), GFP_KERNEL);
1045 free_pages((unsigned long) b_data, order);
1048 bh->b_reqnext = NULL;
1049 bh->b_data = b_data;
1050 bh->b_size = b_allocd;
1051 atomic_set(&bh->b_count, full ? bh->b_size : 0);
1052 prev_bh->b_reqnext = bh;
1057 bh->b_size -= tape->excess_bh_size;
1059 atomic_sub(tape->excess_bh_size, &bh->b_count);
1062 ide_tape_kfree_buffer(tape);
1066 static int idetape_copy_stage_from_user(idetape_tape_t *tape,
1067 const char __user *buf, int n)
1069 struct idetape_bh *bh = tape->bh;
1075 printk(KERN_ERR "ide-tape: bh == NULL in %s\n",
1079 count = min((unsigned int)
1080 (bh->b_size - atomic_read(&bh->b_count)),
1082 if (copy_from_user(bh->b_data + atomic_read(&bh->b_count), buf,
1086 atomic_add(count, &bh->b_count);
1088 if (atomic_read(&bh->b_count) == bh->b_size) {
1091 atomic_set(&bh->b_count, 0);
1098 static int idetape_copy_stage_to_user(idetape_tape_t *tape, char __user *buf,
1101 struct idetape_bh *bh = tape->bh;
1107 printk(KERN_ERR "ide-tape: bh == NULL in %s\n",
1111 count = min(tape->b_count, n);
1112 if (copy_to_user(buf, tape->b_data, count))
1115 tape->b_data += count;
1116 tape->b_count -= count;
1118 if (!tape->b_count) {
1122 tape->b_data = bh->b_data;
1123 tape->b_count = atomic_read(&bh->b_count);
1130 static void idetape_init_merge_buffer(idetape_tape_t *tape)
1132 struct idetape_bh *bh = tape->merge_bh;
1133 tape->bh = tape->merge_bh;
1135 if (tape->chrdev_dir == IDETAPE_DIR_WRITE)
1136 atomic_set(&bh->b_count, 0);
1138 tape->b_data = bh->b_data;
1139 tape->b_count = atomic_read(&bh->b_count);
1144 * Write a filemark if write_filemark=1. Flush the device buffers without
1145 * writing a filemark otherwise.
1147 static void idetape_create_write_filemark_cmd(ide_drive_t *drive,
1148 struct ide_atapi_pc *pc, int write_filemark)
1151 pc->c[0] = WRITE_FILEMARKS;
1152 pc->c[4] = write_filemark;
1153 pc->flags |= PC_FLAG_WAIT_FOR_DSC;
1156 static void idetape_create_test_unit_ready_cmd(struct ide_atapi_pc *pc)
1159 pc->c[0] = TEST_UNIT_READY;
1163 * We add a special packet command request to the tail of the request queue, and
1164 * wait for it to be serviced.
1166 static int idetape_queue_pc_tail(ide_drive_t *drive, struct ide_atapi_pc *pc)
1168 struct ide_tape_obj *tape = drive->driver_data;
1172 rq = blk_get_request(drive->queue, READ, __GFP_WAIT);
1173 rq->cmd_type = REQ_TYPE_SPECIAL;
1174 rq->cmd[13] = REQ_IDETAPE_PC1;
1175 rq->buffer = (char *)pc;
1176 memcpy(rq->cmd, pc->c, 12);
1177 error = blk_execute_rq(drive->queue, tape->disk, rq, 0);
1178 blk_put_request(rq);
1182 static void idetape_create_load_unload_cmd(ide_drive_t *drive,
1183 struct ide_atapi_pc *pc, int cmd)
1186 pc->c[0] = START_STOP;
1188 pc->flags |= PC_FLAG_WAIT_FOR_DSC;
1191 static int idetape_wait_ready(ide_drive_t *drive, unsigned long timeout)
1193 idetape_tape_t *tape = drive->driver_data;
1194 struct ide_atapi_pc pc;
1195 int load_attempted = 0;
1197 /* Wait for the tape to become ready */
1198 set_bit(IDE_AFLAG_MEDIUM_PRESENT, &drive->atapi_flags);
1200 while (time_before(jiffies, timeout)) {
1201 idetape_create_test_unit_ready_cmd(&pc);
1202 if (!idetape_queue_pc_tail(drive, &pc))
1204 if ((tape->sense_key == 2 && tape->asc == 4 && tape->ascq == 2)
1205 || (tape->asc == 0x3A)) {
1209 idetape_create_load_unload_cmd(drive, &pc,
1210 IDETAPE_LU_LOAD_MASK);
1211 idetape_queue_pc_tail(drive, &pc);
1213 /* not about to be ready */
1214 } else if (!(tape->sense_key == 2 && tape->asc == 4 &&
1215 (tape->ascq == 1 || tape->ascq == 8)))
1222 static int idetape_flush_tape_buffers(ide_drive_t *drive)
1224 struct ide_atapi_pc pc;
1227 idetape_create_write_filemark_cmd(drive, &pc, 0);
1228 rc = idetape_queue_pc_tail(drive, &pc);
1231 idetape_wait_ready(drive, 60 * 5 * HZ);
1235 static void idetape_create_read_position_cmd(struct ide_atapi_pc *pc)
1238 pc->c[0] = READ_POSITION;
1242 static int idetape_read_position(ide_drive_t *drive)
1244 idetape_tape_t *tape = drive->driver_data;
1245 struct ide_atapi_pc pc;
1248 debug_log(DBG_PROCS, "Enter %s\n", __func__);
1250 idetape_create_read_position_cmd(&pc);
1251 if (idetape_queue_pc_tail(drive, &pc))
1253 position = tape->first_frame;
1257 static void idetape_create_locate_cmd(ide_drive_t *drive,
1258 struct ide_atapi_pc *pc,
1259 unsigned int block, u8 partition, int skip)
1262 pc->c[0] = POSITION_TO_ELEMENT;
1264 put_unaligned(cpu_to_be32(block), (unsigned int *) &pc->c[3]);
1265 pc->c[8] = partition;
1266 pc->flags |= PC_FLAG_WAIT_FOR_DSC;
1269 static int idetape_create_prevent_cmd(ide_drive_t *drive,
1270 struct ide_atapi_pc *pc, int prevent)
1272 idetape_tape_t *tape = drive->driver_data;
1274 /* device supports locking according to capabilities page */
1275 if (!(tape->caps[6] & 0x01))
1279 pc->c[0] = ALLOW_MEDIUM_REMOVAL;
1284 static int ide_tape_set_media_lock(ide_drive_t *drive, int on)
1286 struct ide_atapi_pc pc;
1288 if (!idetape_create_prevent_cmd(drive, &pc, on))
1291 return idetape_queue_pc_tail(drive, &pc);
1294 static void __ide_tape_discard_merge_buffer(ide_drive_t *drive)
1296 idetape_tape_t *tape = drive->driver_data;
1298 if (tape->chrdev_dir != IDETAPE_DIR_READ)
1301 clear_bit(IDE_AFLAG_FILEMARK, &drive->atapi_flags);
1302 tape->merge_bh_size = 0;
1303 if (tape->merge_bh != NULL) {
1304 ide_tape_kfree_buffer(tape);
1305 tape->merge_bh = NULL;
1308 tape->chrdev_dir = IDETAPE_DIR_NONE;
1312 * Position the tape to the requested block using the LOCATE packet command.
1313 * A READ POSITION command is then issued to check where we are positioned. Like
1314 * all higher level operations, we queue the commands at the tail of the request
1315 * queue and wait for their completion.
1317 static int idetape_position_tape(ide_drive_t *drive, unsigned int block,
1318 u8 partition, int skip)
1320 idetape_tape_t *tape = drive->driver_data;
1322 struct ide_atapi_pc pc;
1324 if (tape->chrdev_dir == IDETAPE_DIR_READ)
1325 __ide_tape_discard_merge_buffer(drive);
1326 idetape_wait_ready(drive, 60 * 5 * HZ);
1327 idetape_create_locate_cmd(drive, &pc, block, partition, skip);
1328 retval = idetape_queue_pc_tail(drive, &pc);
1332 idetape_create_read_position_cmd(&pc);
1333 return (idetape_queue_pc_tail(drive, &pc));
1336 static void ide_tape_discard_merge_buffer(ide_drive_t *drive,
1337 int restore_position)
1339 idetape_tape_t *tape = drive->driver_data;
1342 __ide_tape_discard_merge_buffer(drive);
1343 if (restore_position) {
1344 position = idetape_read_position(drive);
1345 seek = position > 0 ? position : 0;
1346 if (idetape_position_tape(drive, seek, 0, 0)) {
1347 printk(KERN_INFO "ide-tape: %s: position_tape failed in"
1348 " %s\n", tape->name, __func__);
1355 * Generate a read/write request for the block device interface and wait for it
1358 static int idetape_queue_rw_tail(ide_drive_t *drive, int cmd, int blocks,
1359 struct idetape_bh *bh)
1361 idetape_tape_t *tape = drive->driver_data;
1365 debug_log(DBG_SENSE, "%s: cmd=%d\n", __func__, cmd);
1367 rq = blk_get_request(drive->queue, READ, __GFP_WAIT);
1368 rq->cmd_type = REQ_TYPE_SPECIAL;
1370 rq->rq_disk = tape->disk;
1371 rq->special = (void *)bh;
1372 rq->sector = tape->first_frame;
1373 rq->nr_sectors = blocks;
1374 rq->current_nr_sectors = blocks;
1375 blk_execute_rq(drive->queue, tape->disk, rq, 0);
1377 errors = rq->errors;
1378 ret = tape->blk_size * (blocks - rq->current_nr_sectors);
1379 blk_put_request(rq);
1381 if ((cmd & (REQ_IDETAPE_READ | REQ_IDETAPE_WRITE)) == 0)
1385 idetape_init_merge_buffer(tape);
1386 if (errors == IDETAPE_ERROR_GENERAL)
1391 static void idetape_create_inquiry_cmd(struct ide_atapi_pc *pc)
1399 static void idetape_create_rewind_cmd(ide_drive_t *drive,
1400 struct ide_atapi_pc *pc)
1403 pc->c[0] = REZERO_UNIT;
1404 pc->flags |= PC_FLAG_WAIT_FOR_DSC;
1407 static void idetape_create_erase_cmd(struct ide_atapi_pc *pc)
1412 pc->flags |= PC_FLAG_WAIT_FOR_DSC;
1415 static void idetape_create_space_cmd(struct ide_atapi_pc *pc, int count, u8 cmd)
1419 put_unaligned(cpu_to_be32(count), (unsigned int *) &pc->c[1]);
1421 pc->flags |= PC_FLAG_WAIT_FOR_DSC;
1424 /* Queue up a character device originated write request. */
1425 static int idetape_add_chrdev_write_request(ide_drive_t *drive, int blocks)
1427 idetape_tape_t *tape = drive->driver_data;
1429 debug_log(DBG_CHRDEV, "Enter %s\n", __func__);
1431 return idetape_queue_rw_tail(drive, REQ_IDETAPE_WRITE,
1432 blocks, tape->merge_bh);
1435 static void ide_tape_flush_merge_buffer(ide_drive_t *drive)
1437 idetape_tape_t *tape = drive->driver_data;
1439 struct idetape_bh *bh;
1441 if (tape->chrdev_dir != IDETAPE_DIR_WRITE) {
1442 printk(KERN_ERR "ide-tape: bug: Trying to empty merge buffer"
1443 " but we are not writing.\n");
1446 if (tape->merge_bh_size > tape->buffer_size) {
1447 printk(KERN_ERR "ide-tape: bug: merge_buffer too big\n");
1448 tape->merge_bh_size = tape->buffer_size;
1450 if (tape->merge_bh_size) {
1451 blocks = tape->merge_bh_size / tape->blk_size;
1452 if (tape->merge_bh_size % tape->blk_size) {
1456 i = tape->blk_size - tape->merge_bh_size %
1458 bh = tape->bh->b_reqnext;
1460 atomic_set(&bh->b_count, 0);
1466 printk(KERN_INFO "ide-tape: bug,"
1470 min = min(i, (unsigned int)(bh->b_size -
1471 atomic_read(&bh->b_count)));
1472 memset(bh->b_data + atomic_read(&bh->b_count),
1474 atomic_add(min, &bh->b_count);
1479 (void) idetape_add_chrdev_write_request(drive, blocks);
1480 tape->merge_bh_size = 0;
1482 if (tape->merge_bh != NULL) {
1483 ide_tape_kfree_buffer(tape);
1484 tape->merge_bh = NULL;
1486 tape->chrdev_dir = IDETAPE_DIR_NONE;
1489 static int idetape_init_read(ide_drive_t *drive)
1491 idetape_tape_t *tape = drive->driver_data;
1494 /* Initialize read operation */
1495 if (tape->chrdev_dir != IDETAPE_DIR_READ) {
1496 if (tape->chrdev_dir == IDETAPE_DIR_WRITE) {
1497 ide_tape_flush_merge_buffer(drive);
1498 idetape_flush_tape_buffers(drive);
1500 if (tape->merge_bh || tape->merge_bh_size) {
1501 printk(KERN_ERR "ide-tape: merge_bh_size should be"
1503 tape->merge_bh_size = 0;
1505 tape->merge_bh = ide_tape_kmalloc_buffer(tape, 0, 0);
1506 if (!tape->merge_bh)
1508 tape->chrdev_dir = IDETAPE_DIR_READ;
1511 * Issue a read 0 command to ensure that DSC handshake is
1512 * switched from completion mode to buffer available mode.
1513 * No point in issuing this if DSC overlap isn't supported, some
1514 * drives (Seagate STT3401A) will return an error.
1516 if (drive->dsc_overlap) {
1517 bytes_read = idetape_queue_rw_tail(drive,
1518 REQ_IDETAPE_READ, 0,
1520 if (bytes_read < 0) {
1521 ide_tape_kfree_buffer(tape);
1522 tape->merge_bh = NULL;
1523 tape->chrdev_dir = IDETAPE_DIR_NONE;
1532 /* called from idetape_chrdev_read() to service a chrdev read request. */
1533 static int idetape_add_chrdev_read_request(ide_drive_t *drive, int blocks)
1535 idetape_tape_t *tape = drive->driver_data;
1537 debug_log(DBG_PROCS, "Enter %s, %d blocks\n", __func__, blocks);
1539 /* If we are at a filemark, return a read length of 0 */
1540 if (test_bit(IDE_AFLAG_FILEMARK, &drive->atapi_flags))
1543 idetape_init_read(drive);
1545 return idetape_queue_rw_tail(drive, REQ_IDETAPE_READ, blocks,
1549 static void idetape_pad_zeros(ide_drive_t *drive, int bcount)
1551 idetape_tape_t *tape = drive->driver_data;
1552 struct idetape_bh *bh;
1558 bh = tape->merge_bh;
1559 count = min(tape->buffer_size, bcount);
1561 blocks = count / tape->blk_size;
1563 atomic_set(&bh->b_count,
1564 min(count, (unsigned int)bh->b_size));
1565 memset(bh->b_data, 0, atomic_read(&bh->b_count));
1566 count -= atomic_read(&bh->b_count);
1569 idetape_queue_rw_tail(drive, REQ_IDETAPE_WRITE, blocks,
1575 * Rewinds the tape to the Beginning Of the current Partition (BOP). We
1576 * currently support only one partition.
1578 static int idetape_rewind_tape(ide_drive_t *drive)
1581 struct ide_atapi_pc pc;
1582 idetape_tape_t *tape;
1583 tape = drive->driver_data;
1585 debug_log(DBG_SENSE, "Enter %s\n", __func__);
1587 idetape_create_rewind_cmd(drive, &pc);
1588 retval = idetape_queue_pc_tail(drive, &pc);
1592 idetape_create_read_position_cmd(&pc);
1593 retval = idetape_queue_pc_tail(drive, &pc);
1599 /* mtio.h compatible commands should be issued to the chrdev interface. */
1600 static int idetape_blkdev_ioctl(ide_drive_t *drive, unsigned int cmd,
1603 idetape_tape_t *tape = drive->driver_data;
1604 void __user *argp = (void __user *)arg;
1606 struct idetape_config {
1607 int dsc_rw_frequency;
1608 int dsc_media_access_frequency;
1612 debug_log(DBG_PROCS, "Enter %s\n", __func__);
1616 if (copy_from_user(&config, argp, sizeof(config)))
1618 tape->best_dsc_rw_freq = config.dsc_rw_frequency;
1621 config.dsc_rw_frequency = (int) tape->best_dsc_rw_freq;
1622 config.nr_stages = 1;
1623 if (copy_to_user(argp, &config, sizeof(config)))
1632 static int idetape_space_over_filemarks(ide_drive_t *drive, short mt_op,
1635 idetape_tape_t *tape = drive->driver_data;
1636 struct ide_atapi_pc pc;
1637 int retval, count = 0;
1638 int sprev = !!(tape->caps[4] & 0x20);
1642 if (MTBSF == mt_op || MTBSFM == mt_op) {
1645 mt_count = -mt_count;
1648 if (tape->chrdev_dir == IDETAPE_DIR_READ) {
1649 tape->merge_bh_size = 0;
1650 if (test_and_clear_bit(IDE_AFLAG_FILEMARK, &drive->atapi_flags))
1652 ide_tape_discard_merge_buffer(drive, 0);
1658 idetape_create_space_cmd(&pc, mt_count - count,
1659 IDETAPE_SPACE_OVER_FILEMARK);
1660 return idetape_queue_pc_tail(drive, &pc);
1665 retval = idetape_space_over_filemarks(drive, MTFSF,
1669 count = (MTBSFM == mt_op ? 1 : -1);
1670 return idetape_space_over_filemarks(drive, MTFSF, count);
1672 printk(KERN_ERR "ide-tape: MTIO operation %d not supported\n",
1679 * Our character device read / write functions.
1681 * The tape is optimized to maximize throughput when it is transferring an
1682 * integral number of the "continuous transfer limit", which is a parameter of
1683 * the specific tape (26kB on my particular tape, 32kB for Onstream).
1685 * As of version 1.3 of the driver, the character device provides an abstract
1686 * continuous view of the media - any mix of block sizes (even 1 byte) on the
1687 * same backup/restore procedure is supported. The driver will internally
1688 * convert the requests to the recommended transfer unit, so that an unmatch
1689 * between the user's block size to the recommended size will only result in a
1690 * (slightly) increased driver overhead, but will no longer hit performance.
1691 * This is not applicable to Onstream.
1693 static ssize_t idetape_chrdev_read(struct file *file, char __user *buf,
1694 size_t count, loff_t *ppos)
1696 struct ide_tape_obj *tape = ide_tape_f(file);
1697 ide_drive_t *drive = tape->drive;
1698 ssize_t bytes_read, temp, actually_read = 0, rc;
1700 u16 ctl = *(u16 *)&tape->caps[12];
1702 debug_log(DBG_CHRDEV, "Enter %s, count %Zd\n", __func__, count);
1704 if (tape->chrdev_dir != IDETAPE_DIR_READ) {
1705 if (test_bit(IDE_AFLAG_DETECT_BS, &drive->atapi_flags))
1706 if (count > tape->blk_size &&
1707 (count % tape->blk_size) == 0)
1708 tape->user_bs_factor = count / tape->blk_size;
1710 rc = idetape_init_read(drive);
1715 if (tape->merge_bh_size) {
1716 actually_read = min((unsigned int)(tape->merge_bh_size),
1717 (unsigned int)count);
1718 if (idetape_copy_stage_to_user(tape, buf, actually_read))
1720 buf += actually_read;
1721 tape->merge_bh_size -= actually_read;
1722 count -= actually_read;
1724 while (count >= tape->buffer_size) {
1725 bytes_read = idetape_add_chrdev_read_request(drive, ctl);
1726 if (bytes_read <= 0)
1728 if (idetape_copy_stage_to_user(tape, buf, bytes_read))
1731 count -= bytes_read;
1732 actually_read += bytes_read;
1735 bytes_read = idetape_add_chrdev_read_request(drive, ctl);
1736 if (bytes_read <= 0)
1738 temp = min((unsigned long)count, (unsigned long)bytes_read);
1739 if (idetape_copy_stage_to_user(tape, buf, temp))
1741 actually_read += temp;
1742 tape->merge_bh_size = bytes_read-temp;
1745 if (!actually_read && test_bit(IDE_AFLAG_FILEMARK, &drive->atapi_flags)) {
1746 debug_log(DBG_SENSE, "%s: spacing over filemark\n", tape->name);
1748 idetape_space_over_filemarks(drive, MTFSF, 1);
1752 return ret ? ret : actually_read;
1755 static ssize_t idetape_chrdev_write(struct file *file, const char __user *buf,
1756 size_t count, loff_t *ppos)
1758 struct ide_tape_obj *tape = ide_tape_f(file);
1759 ide_drive_t *drive = tape->drive;
1760 ssize_t actually_written = 0;
1762 u16 ctl = *(u16 *)&tape->caps[12];
1764 /* The drive is write protected. */
1765 if (tape->write_prot)
1768 debug_log(DBG_CHRDEV, "Enter %s, count %Zd\n", __func__, count);
1770 /* Initialize write operation */
1771 if (tape->chrdev_dir != IDETAPE_DIR_WRITE) {
1772 if (tape->chrdev_dir == IDETAPE_DIR_READ)
1773 ide_tape_discard_merge_buffer(drive, 1);
1774 if (tape->merge_bh || tape->merge_bh_size) {
1775 printk(KERN_ERR "ide-tape: merge_bh_size "
1776 "should be 0 now\n");
1777 tape->merge_bh_size = 0;
1779 tape->merge_bh = ide_tape_kmalloc_buffer(tape, 0, 0);
1780 if (!tape->merge_bh)
1782 tape->chrdev_dir = IDETAPE_DIR_WRITE;
1783 idetape_init_merge_buffer(tape);
1786 * Issue a write 0 command to ensure that DSC handshake is
1787 * switched from completion mode to buffer available mode. No
1788 * point in issuing this if DSC overlap isn't supported, some
1789 * drives (Seagate STT3401A) will return an error.
1791 if (drive->dsc_overlap) {
1792 ssize_t retval = idetape_queue_rw_tail(drive,
1793 REQ_IDETAPE_WRITE, 0,
1796 ide_tape_kfree_buffer(tape);
1797 tape->merge_bh = NULL;
1798 tape->chrdev_dir = IDETAPE_DIR_NONE;
1805 if (tape->merge_bh_size) {
1806 if (tape->merge_bh_size >= tape->buffer_size) {
1807 printk(KERN_ERR "ide-tape: bug: merge buf too big\n");
1808 tape->merge_bh_size = 0;
1810 actually_written = min((unsigned int)
1811 (tape->buffer_size - tape->merge_bh_size),
1812 (unsigned int)count);
1813 if (idetape_copy_stage_from_user(tape, buf, actually_written))
1815 buf += actually_written;
1816 tape->merge_bh_size += actually_written;
1817 count -= actually_written;
1819 if (tape->merge_bh_size == tape->buffer_size) {
1821 tape->merge_bh_size = 0;
1822 retval = idetape_add_chrdev_write_request(drive, ctl);
1827 while (count >= tape->buffer_size) {
1829 if (idetape_copy_stage_from_user(tape, buf, tape->buffer_size))
1831 buf += tape->buffer_size;
1832 count -= tape->buffer_size;
1833 retval = idetape_add_chrdev_write_request(drive, ctl);
1834 actually_written += tape->buffer_size;
1839 actually_written += count;
1840 if (idetape_copy_stage_from_user(tape, buf, count))
1842 tape->merge_bh_size += count;
1844 return ret ? ret : actually_written;
1847 static int idetape_write_filemark(ide_drive_t *drive)
1849 struct ide_atapi_pc pc;
1851 /* Write a filemark */
1852 idetape_create_write_filemark_cmd(drive, &pc, 1);
1853 if (idetape_queue_pc_tail(drive, &pc)) {
1854 printk(KERN_ERR "ide-tape: Couldn't write a filemark\n");
1861 * Called from idetape_chrdev_ioctl when the general mtio MTIOCTOP ioctl is
1864 * Note: MTBSF and MTBSFM are not supported when the tape doesn't support
1865 * spacing over filemarks in the reverse direction. In this case, MTFSFM is also
1866 * usually not supported.
1868 * The following commands are currently not supported:
1870 * MTFSS, MTBSS, MTWSM, MTSETDENSITY, MTSETDRVBUFFER, MT_ST_BOOLEANS,
1871 * MT_ST_WRITE_THRESHOLD.
1873 static int idetape_mtioctop(ide_drive_t *drive, short mt_op, int mt_count)
1875 idetape_tape_t *tape = drive->driver_data;
1876 struct ide_atapi_pc pc;
1879 debug_log(DBG_ERR, "Handling MTIOCTOP ioctl: mt_op=%d, mt_count=%d\n",
1889 return idetape_space_over_filemarks(drive, mt_op, mt_count);
1896 if (tape->write_prot)
1898 ide_tape_discard_merge_buffer(drive, 1);
1899 for (i = 0; i < mt_count; i++) {
1900 retval = idetape_write_filemark(drive);
1906 ide_tape_discard_merge_buffer(drive, 0);
1907 if (idetape_rewind_tape(drive))
1911 ide_tape_discard_merge_buffer(drive, 0);
1912 idetape_create_load_unload_cmd(drive, &pc,
1913 IDETAPE_LU_LOAD_MASK);
1914 return idetape_queue_pc_tail(drive, &pc);
1918 * If door is locked, attempt to unlock before
1919 * attempting to eject.
1921 if (tape->door_locked) {
1922 if (!ide_tape_set_media_lock(drive, 0))
1923 tape->door_locked = DOOR_UNLOCKED;
1925 ide_tape_discard_merge_buffer(drive, 0);
1926 idetape_create_load_unload_cmd(drive, &pc,
1927 !IDETAPE_LU_LOAD_MASK);
1928 retval = idetape_queue_pc_tail(drive, &pc);
1930 clear_bit(IDE_AFLAG_MEDIUM_PRESENT, &drive->atapi_flags);
1933 ide_tape_discard_merge_buffer(drive, 0);
1934 return idetape_flush_tape_buffers(drive);
1936 ide_tape_discard_merge_buffer(drive, 0);
1937 idetape_create_load_unload_cmd(drive, &pc,
1938 IDETAPE_LU_RETENSION_MASK | IDETAPE_LU_LOAD_MASK);
1939 return idetape_queue_pc_tail(drive, &pc);
1941 idetape_create_space_cmd(&pc, 0, IDETAPE_SPACE_TO_EOD);
1942 return idetape_queue_pc_tail(drive, &pc);
1944 (void)idetape_rewind_tape(drive);
1945 idetape_create_erase_cmd(&pc);
1946 return idetape_queue_pc_tail(drive, &pc);
1949 if (mt_count < tape->blk_size ||
1950 mt_count % tape->blk_size)
1952 tape->user_bs_factor = mt_count / tape->blk_size;
1953 clear_bit(IDE_AFLAG_DETECT_BS, &drive->atapi_flags);
1955 set_bit(IDE_AFLAG_DETECT_BS, &drive->atapi_flags);
1958 ide_tape_discard_merge_buffer(drive, 0);
1959 return idetape_position_tape(drive,
1960 mt_count * tape->user_bs_factor, tape->partition, 0);
1962 ide_tape_discard_merge_buffer(drive, 0);
1963 return idetape_position_tape(drive, 0, mt_count, 0);
1967 retval = ide_tape_set_media_lock(drive, 1);
1970 tape->door_locked = DOOR_EXPLICITLY_LOCKED;
1973 retval = ide_tape_set_media_lock(drive, 0);
1976 tape->door_locked = DOOR_UNLOCKED;
1979 printk(KERN_ERR "ide-tape: MTIO operation %d not supported\n",
1986 * Our character device ioctls. General mtio.h magnetic io commands are
1987 * supported here, and not in the corresponding block interface. Our own
1988 * ide-tape ioctls are supported on both interfaces.
1990 static int idetape_chrdev_ioctl(struct inode *inode, struct file *file,
1991 unsigned int cmd, unsigned long arg)
1993 struct ide_tape_obj *tape = ide_tape_f(file);
1994 ide_drive_t *drive = tape->drive;
1998 int block_offset = 0, position = tape->first_frame;
1999 void __user *argp = (void __user *)arg;
2001 debug_log(DBG_CHRDEV, "Enter %s, cmd=%u\n", __func__, cmd);
2003 if (tape->chrdev_dir == IDETAPE_DIR_WRITE) {
2004 ide_tape_flush_merge_buffer(drive);
2005 idetape_flush_tape_buffers(drive);
2007 if (cmd == MTIOCGET || cmd == MTIOCPOS) {
2008 block_offset = tape->merge_bh_size /
2009 (tape->blk_size * tape->user_bs_factor);
2010 position = idetape_read_position(drive);
2016 if (copy_from_user(&mtop, argp, sizeof(struct mtop)))
2018 return idetape_mtioctop(drive, mtop.mt_op, mtop.mt_count);
2020 memset(&mtget, 0, sizeof(struct mtget));
2021 mtget.mt_type = MT_ISSCSI2;
2022 mtget.mt_blkno = position / tape->user_bs_factor - block_offset;
2024 ((tape->blk_size * tape->user_bs_factor)
2025 << MT_ST_BLKSIZE_SHIFT) & MT_ST_BLKSIZE_MASK;
2027 if (tape->drv_write_prot)
2028 mtget.mt_gstat |= GMT_WR_PROT(0xffffffff);
2030 if (copy_to_user(argp, &mtget, sizeof(struct mtget)))
2034 mtpos.mt_blkno = position / tape->user_bs_factor - block_offset;
2035 if (copy_to_user(argp, &mtpos, sizeof(struct mtpos)))
2039 if (tape->chrdev_dir == IDETAPE_DIR_READ)
2040 ide_tape_discard_merge_buffer(drive, 1);
2041 return idetape_blkdev_ioctl(drive, cmd, arg);
2046 * Do a mode sense page 0 with block descriptor and if it succeeds set the tape
2047 * block size with the reported value.
2049 static void ide_tape_get_bsize_from_bdesc(ide_drive_t *drive)
2051 idetape_tape_t *tape = drive->driver_data;
2052 struct ide_atapi_pc pc;
2054 idetape_create_mode_sense_cmd(&pc, IDETAPE_BLOCK_DESCRIPTOR);
2055 if (idetape_queue_pc_tail(drive, &pc)) {
2056 printk(KERN_ERR "ide-tape: Can't get block descriptor\n");
2057 if (tape->blk_size == 0) {
2058 printk(KERN_WARNING "ide-tape: Cannot deal with zero "
2059 "block size, assuming 32k\n");
2060 tape->blk_size = 32768;
2064 tape->blk_size = (pc.buf[4 + 5] << 16) +
2065 (pc.buf[4 + 6] << 8) +
2067 tape->drv_write_prot = (pc.buf[2] & 0x80) >> 7;
2070 static int idetape_chrdev_open(struct inode *inode, struct file *filp)
2072 unsigned int minor = iminor(inode), i = minor & ~0xc0;
2074 idetape_tape_t *tape;
2077 if (i >= MAX_HWIFS * MAX_DRIVES)
2081 tape = ide_tape_chrdev_get(i);
2087 debug_log(DBG_CHRDEV, "Enter %s\n", __func__);
2090 * We really want to do nonseekable_open(inode, filp); here, but some
2091 * versions of tar incorrectly call lseek on tapes and bail out if that
2092 * fails. So we disallow pread() and pwrite(), but permit lseeks.
2094 filp->f_mode &= ~(FMODE_PREAD | FMODE_PWRITE);
2096 drive = tape->drive;
2098 filp->private_data = tape;
2100 if (test_and_set_bit(IDE_AFLAG_BUSY, &drive->atapi_flags)) {
2105 retval = idetape_wait_ready(drive, 60 * HZ);
2107 clear_bit(IDE_AFLAG_BUSY, &drive->atapi_flags);
2108 printk(KERN_ERR "ide-tape: %s: drive not ready\n", tape->name);
2112 idetape_read_position(drive);
2113 if (!test_bit(IDE_AFLAG_ADDRESS_VALID, &drive->atapi_flags))
2114 (void)idetape_rewind_tape(drive);
2116 /* Read block size and write protect status from drive. */
2117 ide_tape_get_bsize_from_bdesc(drive);
2119 /* Set write protect flag if device is opened as read-only. */
2120 if ((filp->f_flags & O_ACCMODE) == O_RDONLY)
2121 tape->write_prot = 1;
2123 tape->write_prot = tape->drv_write_prot;
2125 /* Make sure drive isn't write protected if user wants to write. */
2126 if (tape->write_prot) {
2127 if ((filp->f_flags & O_ACCMODE) == O_WRONLY ||
2128 (filp->f_flags & O_ACCMODE) == O_RDWR) {
2129 clear_bit(IDE_AFLAG_BUSY, &drive->atapi_flags);
2135 /* Lock the tape drive door so user can't eject. */
2136 if (tape->chrdev_dir == IDETAPE_DIR_NONE) {
2137 if (!ide_tape_set_media_lock(drive, 1)) {
2138 if (tape->door_locked != DOOR_EXPLICITLY_LOCKED)
2139 tape->door_locked = DOOR_LOCKED;
2151 static void idetape_write_release(ide_drive_t *drive, unsigned int minor)
2153 idetape_tape_t *tape = drive->driver_data;
2155 ide_tape_flush_merge_buffer(drive);
2156 tape->merge_bh = ide_tape_kmalloc_buffer(tape, 1, 0);
2157 if (tape->merge_bh != NULL) {
2158 idetape_pad_zeros(drive, tape->blk_size *
2159 (tape->user_bs_factor - 1));
2160 ide_tape_kfree_buffer(tape);
2161 tape->merge_bh = NULL;
2163 idetape_write_filemark(drive);
2164 idetape_flush_tape_buffers(drive);
2165 idetape_flush_tape_buffers(drive);
2168 static int idetape_chrdev_release(struct inode *inode, struct file *filp)
2170 struct ide_tape_obj *tape = ide_tape_f(filp);
2171 ide_drive_t *drive = tape->drive;
2172 unsigned int minor = iminor(inode);
2175 tape = drive->driver_data;
2177 debug_log(DBG_CHRDEV, "Enter %s\n", __func__);
2179 if (tape->chrdev_dir == IDETAPE_DIR_WRITE)
2180 idetape_write_release(drive, minor);
2181 if (tape->chrdev_dir == IDETAPE_DIR_READ) {
2183 ide_tape_discard_merge_buffer(drive, 1);
2186 if (minor < 128 && test_bit(IDE_AFLAG_MEDIUM_PRESENT, &drive->atapi_flags))
2187 (void) idetape_rewind_tape(drive);
2188 if (tape->chrdev_dir == IDETAPE_DIR_NONE) {
2189 if (tape->door_locked == DOOR_LOCKED) {
2190 if (!ide_tape_set_media_lock(drive, 0))
2191 tape->door_locked = DOOR_UNLOCKED;
2194 clear_bit(IDE_AFLAG_BUSY, &drive->atapi_flags);
2200 static void idetape_get_inquiry_results(ide_drive_t *drive)
2202 idetape_tape_t *tape = drive->driver_data;
2203 struct ide_atapi_pc pc;
2204 char fw_rev[4], vendor_id[8], product_id[16];
2206 idetape_create_inquiry_cmd(&pc);
2207 if (idetape_queue_pc_tail(drive, &pc)) {
2208 printk(KERN_ERR "ide-tape: %s: can't get INQUIRY results\n",
2212 memcpy(vendor_id, &pc.buf[8], 8);
2213 memcpy(product_id, &pc.buf[16], 16);
2214 memcpy(fw_rev, &pc.buf[32], 4);
2216 ide_fixstring(vendor_id, 8, 0);
2217 ide_fixstring(product_id, 16, 0);
2218 ide_fixstring(fw_rev, 4, 0);
2220 printk(KERN_INFO "ide-tape: %s <-> %s: %.8s %.16s rev %.4s\n",
2221 drive->name, tape->name, vendor_id, product_id, fw_rev);
2225 * Ask the tape about its various parameters. In particular, we will adjust our
2226 * data transfer buffer size to the recommended value as returned by the tape.
2228 static void idetape_get_mode_sense_results(ide_drive_t *drive)
2230 idetape_tape_t *tape = drive->driver_data;
2231 struct ide_atapi_pc pc;
2233 u8 speed, max_speed;
2235 idetape_create_mode_sense_cmd(&pc, IDETAPE_CAPABILITIES_PAGE);
2236 if (idetape_queue_pc_tail(drive, &pc)) {
2237 printk(KERN_ERR "ide-tape: Can't get tape parameters - assuming"
2238 " some default values\n");
2239 tape->blk_size = 512;
2240 put_unaligned(52, (u16 *)&tape->caps[12]);
2241 put_unaligned(540, (u16 *)&tape->caps[14]);
2242 put_unaligned(6*52, (u16 *)&tape->caps[16]);
2245 caps = pc.buf + 4 + pc.buf[3];
2247 /* convert to host order and save for later use */
2248 speed = be16_to_cpup((__be16 *)&caps[14]);
2249 max_speed = be16_to_cpup((__be16 *)&caps[8]);
2251 *(u16 *)&caps[8] = max_speed;
2252 *(u16 *)&caps[12] = be16_to_cpup((__be16 *)&caps[12]);
2253 *(u16 *)&caps[14] = speed;
2254 *(u16 *)&caps[16] = be16_to_cpup((__be16 *)&caps[16]);
2257 printk(KERN_INFO "ide-tape: %s: invalid tape speed "
2258 "(assuming 650KB/sec)\n", drive->name);
2259 *(u16 *)&caps[14] = 650;
2262 printk(KERN_INFO "ide-tape: %s: invalid max_speed "
2263 "(assuming 650KB/sec)\n", drive->name);
2264 *(u16 *)&caps[8] = 650;
2267 memcpy(&tape->caps, caps, 20);
2269 tape->blk_size = 512;
2270 else if (caps[7] & 0x04)
2271 tape->blk_size = 1024;
2274 #ifdef CONFIG_IDE_PROC_FS
2275 #define ide_tape_devset_get(name, field) \
2276 static int get_##name(ide_drive_t *drive) \
2278 idetape_tape_t *tape = drive->driver_data; \
2279 return tape->field; \
2282 #define ide_tape_devset_set(name, field) \
2283 static int set_##name(ide_drive_t *drive, int arg) \
2285 idetape_tape_t *tape = drive->driver_data; \
2286 tape->field = arg; \
2290 #define ide_tape_devset_rw(_name, _min, _max, _field, _mulf, _divf) \
2291 ide_tape_devset_get(_name, _field) \
2292 ide_tape_devset_set(_name, _field) \
2293 __IDE_DEVSET(_name, S_RW, _min, _max, get_##_name, set_##_name, _mulf, _divf)
2295 #define ide_tape_devset_r(_name, _min, _max, _field, _mulf, _divf) \
2296 ide_tape_devset_get(_name, _field) \
2297 __IDE_DEVSET(_name, S_READ, _min, _max, get_##_name, NULL, _mulf, _divf)
2299 static int mulf_tdsc(ide_drive_t *drive) { return 1000; }
2300 static int divf_tdsc(ide_drive_t *drive) { return HZ; }
2301 static int divf_buffer(ide_drive_t *drive) { return 2; }
2302 static int divf_buffer_size(ide_drive_t *drive) { return 1024; }
2304 ide_devset_rw(dsc_overlap, 0, 1, dsc_overlap);
2306 ide_tape_devset_rw(debug_mask, 0, 0xffff, debug_mask, NULL, NULL);
2307 ide_tape_devset_rw(tdsc, IDETAPE_DSC_RW_MIN, IDETAPE_DSC_RW_MAX,
2308 best_dsc_rw_freq, mulf_tdsc, divf_tdsc);
2310 ide_tape_devset_r(avg_speed, 0, 0xffff, avg_speed, NULL, NULL);
2311 ide_tape_devset_r(speed, 0, 0xffff, caps[14], NULL, NULL);
2312 ide_tape_devset_r(buffer, 0, 0xffff, caps[16], NULL, divf_buffer);
2313 ide_tape_devset_r(buffer_size, 0, 0xffff, buffer_size, NULL, divf_buffer_size);
2315 static const struct ide_devset *idetape_settings[] = {
2316 &ide_devset_avg_speed,
2318 &ide_devset_buffer_size,
2319 &ide_devset_debug_mask,
2320 &ide_devset_dsc_overlap,
2328 * The function below is called to:
2330 * 1. Initialize our various state variables.
2331 * 2. Ask the tape for its capabilities.
2332 * 3. Allocate a buffer which will be used for data transfer. The buffer size
2333 * is chosen based on the recommendation which we received in step 2.
2335 * Note that at this point ide.c already assigned us an irq, so that we can
2336 * queue requests here and wait for their completion.
2338 static void idetape_setup(ide_drive_t *drive, idetape_tape_t *tape, int minor)
2344 u16 *ctl = (u16 *)&tape->caps[12];
2346 drive->pc_callback = ide_tape_callback;
2348 spin_lock_init(&tape->lock);
2349 drive->dsc_overlap = 1;
2350 if (drive->hwif->host_flags & IDE_HFLAG_NO_DSC) {
2351 printk(KERN_INFO "ide-tape: %s: disabling DSC overlap\n",
2353 drive->dsc_overlap = 0;
2355 /* Seagate Travan drives do not support DSC overlap. */
2356 if (strstr((char *)&drive->id[ATA_ID_PROD], "Seagate STT3401"))
2357 drive->dsc_overlap = 0;
2358 tape->minor = minor;
2359 tape->name[0] = 'h';
2360 tape->name[1] = 't';
2361 tape->name[2] = '0' + minor;
2362 tape->chrdev_dir = IDETAPE_DIR_NONE;
2364 *((u16 *)&gcw) = drive->id[ATA_ID_CONFIG];
2366 /* Command packet DRQ type */
2367 if (((gcw[0] & 0x60) >> 5) == 1)
2368 set_bit(IDE_AFLAG_DRQ_INTERRUPT, &drive->atapi_flags);
2370 idetape_get_inquiry_results(drive);
2371 idetape_get_mode_sense_results(drive);
2372 ide_tape_get_bsize_from_bdesc(drive);
2373 tape->user_bs_factor = 1;
2374 tape->buffer_size = *ctl * tape->blk_size;
2375 while (tape->buffer_size > 0xffff) {
2376 printk(KERN_NOTICE "ide-tape: decreasing stage size\n");
2378 tape->buffer_size = *ctl * tape->blk_size;
2380 buffer_size = tape->buffer_size;
2381 tape->pages_per_buffer = buffer_size / PAGE_SIZE;
2382 if (buffer_size % PAGE_SIZE) {
2383 tape->pages_per_buffer++;
2384 tape->excess_bh_size = PAGE_SIZE - buffer_size % PAGE_SIZE;
2387 /* select the "best" DSC read/write polling freq */
2388 speed = max(*(u16 *)&tape->caps[14], *(u16 *)&tape->caps[8]);
2390 t = (IDETAPE_FIFO_THRESHOLD * tape->buffer_size * HZ) / (speed * 1000);
2393 * Ensure that the number we got makes sense; limit it within
2394 * IDETAPE_DSC_RW_MIN and IDETAPE_DSC_RW_MAX.
2396 tape->best_dsc_rw_freq = clamp_t(unsigned long, t, IDETAPE_DSC_RW_MIN,
2397 IDETAPE_DSC_RW_MAX);
2398 printk(KERN_INFO "ide-tape: %s <-> %s: %dKBps, %d*%dkB buffer, "
2400 drive->name, tape->name, *(u16 *)&tape->caps[14],
2401 (*(u16 *)&tape->caps[16] * 512) / tape->buffer_size,
2402 tape->buffer_size / 1024,
2403 tape->best_dsc_rw_freq * 1000 / HZ,
2404 drive->using_dma ? ", DMA":"");
2406 ide_proc_register_driver(drive, tape->driver);
2409 static void ide_tape_remove(ide_drive_t *drive)
2411 idetape_tape_t *tape = drive->driver_data;
2413 ide_proc_unregister_driver(drive, tape->driver);
2415 ide_unregister_region(tape->disk);
2420 static void ide_tape_release(struct kref *kref)
2422 struct ide_tape_obj *tape = to_ide_tape(kref);
2423 ide_drive_t *drive = tape->drive;
2424 struct gendisk *g = tape->disk;
2426 BUG_ON(tape->merge_bh_size);
2428 drive->dsc_overlap = 0;
2429 drive->driver_data = NULL;
2430 device_destroy(idetape_sysfs_class, MKDEV(IDETAPE_MAJOR, tape->minor));
2431 device_destroy(idetape_sysfs_class,
2432 MKDEV(IDETAPE_MAJOR, tape->minor + 128));
2433 idetape_devs[tape->minor] = NULL;
2434 g->private_data = NULL;
2439 #ifdef CONFIG_IDE_PROC_FS
2440 static int proc_idetape_read_name
2441 (char *page, char **start, off_t off, int count, int *eof, void *data)
2443 ide_drive_t *drive = (ide_drive_t *) data;
2444 idetape_tape_t *tape = drive->driver_data;
2448 len = sprintf(out, "%s\n", tape->name);
2449 PROC_IDE_READ_RETURN(page, start, off, count, eof, len);
2452 static ide_proc_entry_t idetape_proc[] = {
2453 { "capacity", S_IFREG|S_IRUGO, proc_ide_read_capacity, NULL },
2454 { "name", S_IFREG|S_IRUGO, proc_idetape_read_name, NULL },
2455 { NULL, 0, NULL, NULL }
2459 static int ide_tape_probe(ide_drive_t *);
2461 static ide_driver_t idetape_driver = {
2463 .owner = THIS_MODULE,
2465 .bus = &ide_bus_type,
2467 .probe = ide_tape_probe,
2468 .remove = ide_tape_remove,
2469 .version = IDETAPE_VERSION,
2471 .do_request = idetape_do_request,
2472 .end_request = idetape_end_request,
2473 .error = __ide_error,
2474 #ifdef CONFIG_IDE_PROC_FS
2475 .proc = idetape_proc,
2476 .settings = idetape_settings,
2480 /* Our character device supporting functions, passed to register_chrdev. */
2481 static const struct file_operations idetape_fops = {
2482 .owner = THIS_MODULE,
2483 .read = idetape_chrdev_read,
2484 .write = idetape_chrdev_write,
2485 .ioctl = idetape_chrdev_ioctl,
2486 .open = idetape_chrdev_open,
2487 .release = idetape_chrdev_release,
2490 static int idetape_open(struct inode *inode, struct file *filp)
2492 struct gendisk *disk = inode->i_bdev->bd_disk;
2493 struct ide_tape_obj *tape;
2495 tape = ide_tape_get(disk);
2502 static int idetape_release(struct inode *inode, struct file *filp)
2504 struct gendisk *disk = inode->i_bdev->bd_disk;
2505 struct ide_tape_obj *tape = ide_tape_g(disk);
2512 static int idetape_ioctl(struct inode *inode, struct file *file,
2513 unsigned int cmd, unsigned long arg)
2515 struct block_device *bdev = inode->i_bdev;
2516 struct ide_tape_obj *tape = ide_tape_g(bdev->bd_disk);
2517 ide_drive_t *drive = tape->drive;
2518 int err = generic_ide_ioctl(drive, file, bdev, cmd, arg);
2520 err = idetape_blkdev_ioctl(drive, cmd, arg);
2524 static struct block_device_operations idetape_block_ops = {
2525 .owner = THIS_MODULE,
2526 .open = idetape_open,
2527 .release = idetape_release,
2528 .ioctl = idetape_ioctl,
2531 static int ide_tape_probe(ide_drive_t *drive)
2533 idetape_tape_t *tape;
2537 if (!strstr("ide-tape", drive->driver_req))
2540 if (drive->media != ide_tape)
2543 if (drive->id_read == 1 && !ide_check_atapi_device(drive, DRV_NAME)) {
2544 printk(KERN_ERR "ide-tape: %s: not supported by this version of"
2545 " the driver\n", drive->name);
2548 tape = kzalloc(sizeof(idetape_tape_t), GFP_KERNEL);
2550 printk(KERN_ERR "ide-tape: %s: Can't allocate a tape struct\n",
2555 g = alloc_disk(1 << PARTN_BITS);
2559 ide_init_disk(g, drive);
2561 kref_init(&tape->kref);
2563 tape->drive = drive;
2564 tape->driver = &idetape_driver;
2567 g->private_data = &tape->driver;
2569 drive->driver_data = tape;
2571 mutex_lock(&idetape_ref_mutex);
2572 for (minor = 0; idetape_devs[minor]; minor++)
2574 idetape_devs[minor] = tape;
2575 mutex_unlock(&idetape_ref_mutex);
2577 idetape_setup(drive, tape, minor);
2579 device_create_drvdata(idetape_sysfs_class, &drive->gendev,
2580 MKDEV(IDETAPE_MAJOR, minor), NULL,
2582 device_create_drvdata(idetape_sysfs_class, &drive->gendev,
2583 MKDEV(IDETAPE_MAJOR, minor + 128), NULL,
2586 g->fops = &idetape_block_ops;
2587 ide_register_region(g);
2597 static void __exit idetape_exit(void)
2599 driver_unregister(&idetape_driver.gen_driver);
2600 class_destroy(idetape_sysfs_class);
2601 unregister_chrdev(IDETAPE_MAJOR, "ht");
2604 static int __init idetape_init(void)
2607 idetape_sysfs_class = class_create(THIS_MODULE, "ide_tape");
2608 if (IS_ERR(idetape_sysfs_class)) {
2609 idetape_sysfs_class = NULL;
2610 printk(KERN_ERR "Unable to create sysfs class for ide tapes\n");
2615 if (register_chrdev(IDETAPE_MAJOR, "ht", &idetape_fops)) {
2616 printk(KERN_ERR "ide-tape: Failed to register chrdev"
2619 goto out_free_class;
2622 error = driver_register(&idetape_driver.gen_driver);
2624 goto out_free_driver;
2629 driver_unregister(&idetape_driver.gen_driver);
2631 class_destroy(idetape_sysfs_class);
2636 MODULE_ALIAS("ide:*m-tape*");
2637 module_init(idetape_init);
2638 module_exit(idetape_exit);
2639 MODULE_ALIAS_CHARDEV_MAJOR(IDETAPE_MAJOR);
2640 MODULE_DESCRIPTION("ATAPI Streaming TAPE Driver");
2641 MODULE_LICENSE("GPL");