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 * The following parameter is used to select the point in the internal tape fifo
85 * in which we will start to refill the buffer. Decreasing the following
86 * parameter will improve the system's latency and interactive response, while
87 * using a high value might improve system throughput.
89 #define IDETAPE_FIFO_THRESHOLD 2
92 * DSC polling parameters.
94 * Polling for DSC (a single bit in the status register) is a very important
95 * function in ide-tape. There are two cases in which we poll for DSC:
97 * 1. Before a read/write packet command, to ensure that we can transfer data
98 * from/to the tape's data buffers, without causing an actual media access.
99 * In case the tape is not ready yet, we take out our request from the device
100 * request queue, so that ide.c could service requests from the other device
101 * on the same interface in the meantime.
103 * 2. After the successful initialization of a "media access packet command",
104 * which is a command that can take a long time to complete (the interval can
105 * range from several seconds to even an hour). Again, we postpone our request
106 * in the middle to free the bus for the other device. The polling frequency
107 * here should be lower than the read/write frequency since those media access
108 * commands are slow. We start from a "fast" frequency - IDETAPE_DSC_MA_FAST
109 * (1 second), and if we don't receive DSC after IDETAPE_DSC_MA_THRESHOLD
110 * (5 min), we switch it to a lower frequency - IDETAPE_DSC_MA_SLOW (1 min).
112 * We also set a timeout for the timer, in case something goes wrong. The
113 * timeout should be longer then the maximum execution time of a tape operation.
117 #define IDETAPE_DSC_RW_MIN 5*HZ/100 /* 50 msec */
118 #define IDETAPE_DSC_RW_MAX 40*HZ/100 /* 400 msec */
119 #define IDETAPE_DSC_RW_TIMEOUT 2*60*HZ /* 2 minutes */
120 #define IDETAPE_DSC_MA_FAST 2*HZ /* 2 seconds */
121 #define IDETAPE_DSC_MA_THRESHOLD 5*60*HZ /* 5 minutes */
122 #define IDETAPE_DSC_MA_SLOW 30*HZ /* 30 seconds */
123 #define IDETAPE_DSC_MA_TIMEOUT 2*60*60*HZ /* 2 hours */
125 /*************************** End of tunable parameters ***********************/
127 /* tape directions */
129 IDETAPE_DIR_NONE = (1 << 0),
130 IDETAPE_DIR_READ = (1 << 1),
131 IDETAPE_DIR_WRITE = (1 << 2),
137 struct idetape_bh *b_reqnext;
141 /* Tape door status */
142 #define DOOR_UNLOCKED 0
143 #define DOOR_LOCKED 1
144 #define DOOR_EXPLICITLY_LOCKED 2
146 /* Some defines for the SPACE command */
147 #define IDETAPE_SPACE_OVER_FILEMARK 1
148 #define IDETAPE_SPACE_TO_EOD 3
150 /* Some defines for the LOAD UNLOAD command */
151 #define IDETAPE_LU_LOAD_MASK 1
152 #define IDETAPE_LU_RETENSION_MASK 2
153 #define IDETAPE_LU_EOT_MASK 4
155 /* Structures related to the SELECT SENSE / MODE SENSE packet commands. */
156 #define IDETAPE_BLOCK_DESCRIPTOR 0
157 #define IDETAPE_CAPABILITIES_PAGE 0x2a
160 * Most of our global data which we need to save even as we leave the driver due
161 * to an interrupt or a timer event is stored in the struct defined below.
163 typedef struct ide_tape_obj {
165 struct ide_driver *driver;
166 struct gendisk *disk;
169 /* used by REQ_IDETAPE_{READ,WRITE} requests */
170 struct ide_atapi_pc queued_pc;
173 * DSC polling variables.
175 * While polling for DSC we use postponed_rq to postpone the current
176 * request so that ide.c will be able to service pending requests on the
177 * other device. Note that at most we will have only one DSC (usually
178 * data transfer) request in the device request queue.
180 struct request *postponed_rq;
181 /* The time in which we started polling for DSC */
182 unsigned long dsc_polling_start;
183 /* Timer used to poll for dsc */
184 struct timer_list dsc_timer;
185 /* Read/Write dsc polling frequency */
186 unsigned long best_dsc_rw_freq;
187 unsigned long dsc_poll_freq;
188 unsigned long dsc_timeout;
190 /* Read position information */
193 unsigned int first_frame;
195 /* Last error information */
196 u8 sense_key, asc, ascq;
198 /* Character device operation */
202 /* Current character device data transfer direction */
205 /* tape block size, usually 512 or 1024 bytes */
206 unsigned short blk_size;
209 /* Copy of the tape's Capabilities and Mechanical Page */
213 * Active data transfer request parameters.
215 * At most, there is only one ide-tape originated data transfer request
216 * in the device request queue. This allows ide.c to easily service
217 * requests from the other device when we postpone our active request.
220 /* Data buffer size chosen based on the tape's recommendation */
223 struct idetape_bh *merge_bh;
224 /* size of the merge buffer */
226 /* pointer to current buffer head within the merge buffer */
227 struct idetape_bh *bh;
231 int pages_per_buffer;
232 /* Wasted space in each stage */
235 /* Measures average tape speed */
236 unsigned long avg_time;
240 /* the door is currently locked */
242 /* the tape hardware is write protected */
244 /* the tape is write protected (hardware or opened as read-only) */
250 static DEFINE_MUTEX(idetape_ref_mutex);
252 static struct class *idetape_sysfs_class;
254 static void ide_tape_release(struct device *);
256 static struct ide_tape_obj *ide_tape_get(struct gendisk *disk)
258 struct ide_tape_obj *tape = NULL;
260 mutex_lock(&idetape_ref_mutex);
261 tape = ide_drv_g(disk, ide_tape_obj);
263 if (ide_device_get(tape->drive))
266 get_device(&tape->dev);
268 mutex_unlock(&idetape_ref_mutex);
272 static void ide_tape_put(struct ide_tape_obj *tape)
274 ide_drive_t *drive = tape->drive;
276 mutex_lock(&idetape_ref_mutex);
277 put_device(&tape->dev);
278 ide_device_put(drive);
279 mutex_unlock(&idetape_ref_mutex);
283 * The variables below are used for the character device interface. Additional
284 * state variables are defined in our ide_drive_t structure.
286 static struct ide_tape_obj *idetape_devs[MAX_HWIFS * MAX_DRIVES];
288 static struct ide_tape_obj *ide_tape_chrdev_get(unsigned int i)
290 struct ide_tape_obj *tape = NULL;
292 mutex_lock(&idetape_ref_mutex);
293 tape = idetape_devs[i];
295 get_device(&tape->dev);
296 mutex_unlock(&idetape_ref_mutex);
300 static int idetape_input_buffers(ide_drive_t *drive, struct ide_atapi_pc *pc,
303 struct idetape_bh *bh = pc->bh;
310 (unsigned int)(bh->b_size - atomic_read(&bh->b_count)),
312 drive->hwif->tp_ops->input_data(drive, NULL, bh->b_data +
313 atomic_read(&bh->b_count), count);
315 atomic_add(count, &bh->b_count);
316 if (atomic_read(&bh->b_count) == bh->b_size) {
319 atomic_set(&bh->b_count, 0);
328 static int idetape_output_buffers(ide_drive_t *drive, struct ide_atapi_pc *pc,
331 struct idetape_bh *bh = pc->bh;
337 count = min((unsigned int)pc->b_count, (unsigned int)bcount);
338 drive->hwif->tp_ops->output_data(drive, NULL, pc->b_data, count);
341 pc->b_count -= count;
346 pc->b_data = bh->b_data;
347 pc->b_count = atomic_read(&bh->b_count);
355 static void idetape_update_buffers(ide_drive_t *drive, struct ide_atapi_pc *pc)
357 struct idetape_bh *bh = pc->bh;
359 unsigned int bcount = pc->xferred;
361 if (pc->flags & PC_FLAG_WRITING)
365 printk(KERN_ERR "ide-tape: bh == NULL in %s\n",
369 count = min((unsigned int)bh->b_size, (unsigned int)bcount);
370 atomic_set(&bh->b_count, count);
371 if (atomic_read(&bh->b_count) == bh->b_size)
379 * called on each failed packet command retry to analyze the request sense. We
380 * currently do not utilize this information.
382 static void idetape_analyze_error(ide_drive_t *drive, u8 *sense)
384 idetape_tape_t *tape = drive->driver_data;
385 struct ide_atapi_pc *pc = drive->failed_pc;
387 tape->sense_key = sense[2] & 0xF;
388 tape->asc = sense[12];
389 tape->ascq = sense[13];
391 debug_log(DBG_ERR, "pc = %x, sense key = %x, asc = %x, ascq = %x\n",
392 pc->c[0], tape->sense_key, tape->asc, tape->ascq);
394 /* Correct pc->xferred by asking the tape. */
395 if (pc->flags & PC_FLAG_DMA_ERROR) {
396 pc->xferred = pc->req_xfer -
398 get_unaligned_be32(&sense[3]);
399 idetape_update_buffers(drive, pc);
403 * If error was the result of a zero-length read or write command,
404 * with sense key=5, asc=0x22, ascq=0, let it slide. Some drives
405 * (i.e. Seagate STT3401A Travan) don't support 0-length read/writes.
407 if ((pc->c[0] == READ_6 || pc->c[0] == WRITE_6)
409 && pc->c[4] == 0 && pc->c[3] == 0 && pc->c[2] == 0) {
410 if (tape->sense_key == 5) {
411 /* don't report an error, everything's ok */
413 /* don't retry read/write */
414 pc->flags |= PC_FLAG_ABORT;
417 if (pc->c[0] == READ_6 && (sense[2] & 0x80)) {
418 pc->error = IDE_DRV_ERROR_FILEMARK;
419 pc->flags |= PC_FLAG_ABORT;
421 if (pc->c[0] == WRITE_6) {
422 if ((sense[2] & 0x40) || (tape->sense_key == 0xd
423 && tape->asc == 0x0 && tape->ascq == 0x2)) {
424 pc->error = IDE_DRV_ERROR_EOD;
425 pc->flags |= PC_FLAG_ABORT;
428 if (pc->c[0] == READ_6 || pc->c[0] == WRITE_6) {
429 if (tape->sense_key == 8) {
430 pc->error = IDE_DRV_ERROR_EOD;
431 pc->flags |= PC_FLAG_ABORT;
433 if (!(pc->flags & PC_FLAG_ABORT) &&
435 pc->retries = IDETAPE_MAX_PC_RETRIES + 1;
439 /* Free data buffers completely. */
440 static void ide_tape_kfree_buffer(idetape_tape_t *tape)
442 struct idetape_bh *prev_bh, *bh = tape->merge_bh;
445 u32 size = bh->b_size;
448 unsigned int order = fls(size >> PAGE_SHIFT)-1;
451 free_pages((unsigned long)bh->b_data, order);
454 bh->b_data += (1 << order) * PAGE_SIZE;
462 static void ide_tape_handle_dsc(ide_drive_t *);
464 static int ide_tape_callback(ide_drive_t *drive, int dsc)
466 idetape_tape_t *tape = drive->driver_data;
467 struct ide_atapi_pc *pc = drive->pc;
468 struct request *rq = drive->hwif->rq;
469 int uptodate = pc->error ? 0 : 1;
470 int err = uptodate ? 0 : IDE_DRV_ERROR_GENERAL;
472 debug_log(DBG_PROCS, "Enter %s\n", __func__);
475 ide_tape_handle_dsc(drive);
477 if (drive->failed_pc == pc)
478 drive->failed_pc = NULL;
480 if (pc->c[0] == REQUEST_SENSE) {
482 idetape_analyze_error(drive, pc->buf);
484 printk(KERN_ERR "ide-tape: Error in REQUEST SENSE "
485 "itself - Aborting request!\n");
486 } else if (pc->c[0] == READ_6 || pc->c[0] == WRITE_6) {
487 int blocks = pc->xferred / tape->blk_size;
489 tape->avg_size += blocks * tape->blk_size;
491 if (time_after_eq(jiffies, tape->avg_time + HZ)) {
492 tape->avg_speed = tape->avg_size * HZ /
493 (jiffies - tape->avg_time) / 1024;
495 tape->avg_time = jiffies;
498 tape->first_frame += blocks;
499 rq->current_nr_sectors -= blocks;
505 } else if (pc->c[0] == READ_POSITION && uptodate) {
506 u8 *readpos = pc->buf;
508 debug_log(DBG_SENSE, "BOP - %s\n",
509 (readpos[0] & 0x80) ? "Yes" : "No");
510 debug_log(DBG_SENSE, "EOP - %s\n",
511 (readpos[0] & 0x40) ? "Yes" : "No");
513 if (readpos[0] & 0x4) {
514 printk(KERN_INFO "ide-tape: Block location is unknown"
516 clear_bit(IDE_AFLAG_ADDRESS_VALID, &drive->atapi_flags);
518 err = IDE_DRV_ERROR_GENERAL;
520 debug_log(DBG_SENSE, "Block Location - %u\n",
521 be32_to_cpup((__be32 *)&readpos[4]));
523 tape->partition = readpos[1];
524 tape->first_frame = be32_to_cpup((__be32 *)&readpos[4]);
525 set_bit(IDE_AFLAG_ADDRESS_VALID, &drive->atapi_flags);
535 * Postpone the current request so that ide.c will be able to service requests
536 * from another device on the same port while we are polling for DSC.
538 static void idetape_postpone_request(ide_drive_t *drive)
540 idetape_tape_t *tape = drive->driver_data;
542 debug_log(DBG_PROCS, "Enter %s\n", __func__);
544 tape->postponed_rq = drive->hwif->rq;
546 ide_stall_queue(drive, tape->dsc_poll_freq);
549 static void ide_tape_handle_dsc(ide_drive_t *drive)
551 idetape_tape_t *tape = drive->driver_data;
553 /* Media access command */
554 tape->dsc_polling_start = jiffies;
555 tape->dsc_poll_freq = IDETAPE_DSC_MA_FAST;
556 tape->dsc_timeout = jiffies + IDETAPE_DSC_MA_TIMEOUT;
557 /* Allow ide.c to handle other requests */
558 idetape_postpone_request(drive);
561 static int ide_tape_io_buffers(ide_drive_t *drive, struct ide_atapi_pc *pc,
562 unsigned int bcount, int write)
567 bleft = idetape_output_buffers(drive, pc, bcount);
569 bleft = idetape_input_buffers(drive, pc, bcount);
571 return bcount - bleft;
575 * Packet Command Interface
577 * The current Packet Command is available in drive->pc, and will not change
578 * until we finish handling it. Each packet command is associated with a
579 * callback function that will be called when the command is finished.
581 * The handling will be done in three stages:
583 * 1. ide_tape_issue_pc will send the packet command to the drive, and will set
584 * the interrupt handler to ide_pc_intr.
586 * 2. On each interrupt, ide_pc_intr will be called. This step will be
587 * repeated until the device signals us that no more interrupts will be issued.
589 * 3. ATAPI Tape media access commands have immediate status with a delayed
590 * process. In case of a successful initiation of a media access packet command,
591 * the DSC bit will be set when the actual execution of the command is finished.
592 * Since the tape drive will not issue an interrupt, we have to poll for this
593 * event. In this case, we define the request as "low priority request" by
594 * setting rq_status to IDETAPE_RQ_POSTPONED, set a timer to poll for DSC and
597 * ide.c will then give higher priority to requests which originate from the
598 * other device, until will change rq_status to RQ_ACTIVE.
600 * 4. When the packet command is finished, it will be checked for errors.
602 * 5. In case an error was found, we queue a request sense packet command in
603 * front of the request queue and retry the operation up to
604 * IDETAPE_MAX_PC_RETRIES times.
606 * 6. In case no error was found, or we decided to give up and not to retry
607 * again, the callback function will be called and then we will handle the next
611 static ide_startstop_t ide_tape_issue_pc(ide_drive_t *drive,
613 struct ide_atapi_pc *pc)
615 idetape_tape_t *tape = drive->driver_data;
617 if (drive->failed_pc == NULL && pc->c[0] != REQUEST_SENSE)
618 drive->failed_pc = pc;
620 /* Set the current packet command */
623 if (pc->retries > IDETAPE_MAX_PC_RETRIES ||
624 (pc->flags & PC_FLAG_ABORT)) {
626 * We will "abort" retrying a packet command in case legitimate
627 * error code was received (crossing a filemark, or end of the
628 * media, for example).
630 if (!(pc->flags & PC_FLAG_ABORT)) {
631 if (!(pc->c[0] == TEST_UNIT_READY &&
632 tape->sense_key == 2 && tape->asc == 4 &&
633 (tape->ascq == 1 || tape->ascq == 8))) {
634 printk(KERN_ERR "ide-tape: %s: I/O error, "
635 "pc = %2x, key = %2x, "
636 "asc = %2x, ascq = %2x\n",
637 tape->name, pc->c[0],
638 tape->sense_key, tape->asc,
642 pc->error = IDE_DRV_ERROR_GENERAL;
644 drive->failed_pc = NULL;
645 drive->pc_callback(drive, 0);
646 ide_complete_rq(drive, -EIO, blk_rq_bytes(drive->hwif->rq));
649 debug_log(DBG_SENSE, "Retry #%d, cmd = %02X\n", pc->retries, pc->c[0]);
653 return ide_issue_pc(drive, cmd);
656 /* A mode sense command is used to "sense" tape parameters. */
657 static void idetape_create_mode_sense_cmd(struct ide_atapi_pc *pc, u8 page_code)
660 pc->c[0] = MODE_SENSE;
661 if (page_code != IDETAPE_BLOCK_DESCRIPTOR)
662 /* DBD = 1 - Don't return block descriptors */
664 pc->c[2] = page_code;
666 * Changed pc->c[3] to 0 (255 will at best return unused info).
668 * For SCSI this byte is defined as subpage instead of high byte
669 * of length and some IDE drives seem to interpret it this way
670 * and return an error when 255 is used.
673 /* We will just discard data in that case */
675 if (page_code == IDETAPE_BLOCK_DESCRIPTOR)
677 else if (page_code == IDETAPE_CAPABILITIES_PAGE)
683 static ide_startstop_t idetape_media_access_finished(ide_drive_t *drive)
685 ide_hwif_t *hwif = drive->hwif;
686 idetape_tape_t *tape = drive->driver_data;
687 struct ide_atapi_pc *pc = drive->pc;
690 stat = hwif->tp_ops->read_status(hwif);
692 if (stat & ATA_DSC) {
693 if (stat & ATA_ERR) {
695 if (pc->c[0] != TEST_UNIT_READY)
696 printk(KERN_ERR "ide-tape: %s: I/O error, ",
698 /* Retry operation */
704 pc->error = IDE_DRV_ERROR_GENERAL;
705 drive->failed_pc = NULL;
707 drive->pc_callback(drive, 0);
711 static void ide_tape_create_rw_cmd(idetape_tape_t *tape,
712 struct ide_atapi_pc *pc, struct request *rq,
715 struct idetape_bh *bh = (struct idetape_bh *)rq->special;
716 unsigned int length = rq->current_nr_sectors;
719 put_unaligned(cpu_to_be32(length), (unsigned int *) &pc->c[1]);
723 pc->buf_size = length * tape->blk_size;
724 pc->req_xfer = pc->buf_size;
725 if (pc->req_xfer == tape->buffer_size)
726 pc->flags |= PC_FLAG_DMA_OK;
728 if (opcode == READ_6) {
730 atomic_set(&bh->b_count, 0);
731 } else if (opcode == WRITE_6) {
733 pc->flags |= PC_FLAG_WRITING;
734 pc->b_data = bh->b_data;
735 pc->b_count = atomic_read(&bh->b_count);
738 memcpy(rq->cmd, pc->c, 12);
741 static ide_startstop_t idetape_do_request(ide_drive_t *drive,
742 struct request *rq, sector_t block)
744 ide_hwif_t *hwif = drive->hwif;
745 idetape_tape_t *tape = drive->driver_data;
746 struct ide_atapi_pc *pc = NULL;
747 struct request *postponed_rq = tape->postponed_rq;
751 debug_log(DBG_SENSE, "sector: %llu, nr_sectors: %lu,"
752 " current_nr_sectors: %u\n",
753 (unsigned long long)rq->sector, rq->nr_sectors,
754 rq->current_nr_sectors);
756 if (!(blk_special_request(rq) || blk_sense_request(rq))) {
757 /* We do not support buffer cache originated requests. */
758 printk(KERN_NOTICE "ide-tape: %s: Unsupported request in "
759 "request queue (%d)\n", drive->name, rq->cmd_type);
760 if (blk_fs_request(rq) == 0 && rq->errors == 0)
762 ide_complete_rq(drive, -EIO, ide_rq_bytes(rq));
766 /* Retry a failed packet command */
767 if (drive->failed_pc && drive->pc->c[0] == REQUEST_SENSE) {
768 pc = drive->failed_pc;
772 if (postponed_rq != NULL)
773 if (rq != postponed_rq) {
774 printk(KERN_ERR "ide-tape: ide-tape.c bug - "
775 "Two DSC requests were queued\n");
776 drive->failed_pc = NULL;
778 ide_complete_rq(drive, 0, blk_rq_bytes(rq));
782 tape->postponed_rq = NULL;
785 * If the tape is still busy, postpone our request and service
786 * the other device meanwhile.
788 stat = hwif->tp_ops->read_status(hwif);
790 if ((drive->dev_flags & IDE_DFLAG_DSC_OVERLAP) == 0 &&
791 (rq->cmd[13] & REQ_IDETAPE_PC2) == 0)
792 set_bit(IDE_AFLAG_IGNORE_DSC, &drive->atapi_flags);
794 if (drive->dev_flags & IDE_DFLAG_POST_RESET) {
795 set_bit(IDE_AFLAG_IGNORE_DSC, &drive->atapi_flags);
796 drive->dev_flags &= ~IDE_DFLAG_POST_RESET;
799 if (!test_and_clear_bit(IDE_AFLAG_IGNORE_DSC, &drive->atapi_flags) &&
800 (stat & ATA_DSC) == 0) {
801 if (postponed_rq == NULL) {
802 tape->dsc_polling_start = jiffies;
803 tape->dsc_poll_freq = tape->best_dsc_rw_freq;
804 tape->dsc_timeout = jiffies + IDETAPE_DSC_RW_TIMEOUT;
805 } else if (time_after(jiffies, tape->dsc_timeout)) {
806 printk(KERN_ERR "ide-tape: %s: DSC timeout\n",
808 if (rq->cmd[13] & REQ_IDETAPE_PC2) {
809 idetape_media_access_finished(drive);
812 return ide_do_reset(drive);
814 } else if (time_after(jiffies,
815 tape->dsc_polling_start +
816 IDETAPE_DSC_MA_THRESHOLD))
817 tape->dsc_poll_freq = IDETAPE_DSC_MA_SLOW;
818 idetape_postpone_request(drive);
821 if (rq->cmd[13] & REQ_IDETAPE_READ) {
822 pc = &tape->queued_pc;
823 ide_tape_create_rw_cmd(tape, pc, rq, READ_6);
826 if (rq->cmd[13] & REQ_IDETAPE_WRITE) {
827 pc = &tape->queued_pc;
828 ide_tape_create_rw_cmd(tape, pc, rq, WRITE_6);
831 if (rq->cmd[13] & REQ_IDETAPE_PC1) {
832 pc = (struct ide_atapi_pc *)rq->special;
833 rq->cmd[13] &= ~(REQ_IDETAPE_PC1);
834 rq->cmd[13] |= REQ_IDETAPE_PC2;
837 if (rq->cmd[13] & REQ_IDETAPE_PC2) {
838 idetape_media_access_finished(drive);
844 /* prepare sense request for this command */
845 ide_prep_sense(drive, rq);
847 memset(&cmd, 0, sizeof(cmd));
850 cmd.tf_flags |= IDE_TFLAG_WRITE;
854 ide_init_sg_cmd(&cmd, pc->req_xfer);
855 ide_map_sg(drive, &cmd);
857 return ide_tape_issue_pc(drive, &cmd, pc);
861 * The function below uses __get_free_pages to allocate a data buffer of size
862 * tape->buffer_size (or a bit more). We attempt to combine sequential pages as
865 * It returns a pointer to the newly allocated buffer, or NULL in case of
868 static struct idetape_bh *ide_tape_kmalloc_buffer(idetape_tape_t *tape,
871 struct idetape_bh *prev_bh, *bh, *merge_bh;
872 int pages = tape->pages_per_buffer;
873 unsigned int order, b_allocd;
876 merge_bh = kmalloc(sizeof(struct idetape_bh), GFP_KERNEL);
881 order = fls(pages) - 1;
882 bh->b_data = (char *) __get_free_pages(GFP_KERNEL, order);
885 b_allocd = (1 << order) * PAGE_SIZE;
889 memset(bh->b_data, 0, b_allocd);
890 bh->b_reqnext = NULL;
891 bh->b_size = b_allocd;
892 atomic_set(&bh->b_count, full ? bh->b_size : 0);
895 order = fls(pages) - 1;
896 b_data = (char *) __get_free_pages(GFP_KERNEL, order);
899 b_allocd = (1 << order) * PAGE_SIZE;
902 memset(b_data, 0, b_allocd);
904 /* newly allocated page frames below buffer header or ...*/
905 if (bh->b_data == b_data + b_allocd) {
906 bh->b_size += b_allocd;
907 bh->b_data -= b_allocd;
909 atomic_add(b_allocd, &bh->b_count);
912 /* they are above the header */
913 if (b_data == bh->b_data + bh->b_size) {
914 bh->b_size += b_allocd;
916 atomic_add(b_allocd, &bh->b_count);
920 bh = kmalloc(sizeof(struct idetape_bh), GFP_KERNEL);
922 free_pages((unsigned long) b_data, order);
925 bh->b_reqnext = NULL;
927 bh->b_size = b_allocd;
928 atomic_set(&bh->b_count, full ? bh->b_size : 0);
929 prev_bh->b_reqnext = bh;
934 bh->b_size -= tape->excess_bh_size;
936 atomic_sub(tape->excess_bh_size, &bh->b_count);
939 ide_tape_kfree_buffer(tape);
943 static int idetape_copy_stage_from_user(idetape_tape_t *tape,
944 const char __user *buf, int n)
946 struct idetape_bh *bh = tape->bh;
952 printk(KERN_ERR "ide-tape: bh == NULL in %s\n",
956 count = min((unsigned int)
957 (bh->b_size - atomic_read(&bh->b_count)),
959 if (copy_from_user(bh->b_data + atomic_read(&bh->b_count), buf,
963 atomic_add(count, &bh->b_count);
965 if (atomic_read(&bh->b_count) == bh->b_size) {
968 atomic_set(&bh->b_count, 0);
975 static int idetape_copy_stage_to_user(idetape_tape_t *tape, char __user *buf,
978 struct idetape_bh *bh = tape->bh;
984 printk(KERN_ERR "ide-tape: bh == NULL in %s\n",
988 count = min(tape->b_count, n);
989 if (copy_to_user(buf, tape->b_data, count))
992 tape->b_data += count;
993 tape->b_count -= count;
995 if (!tape->b_count) {
999 tape->b_data = bh->b_data;
1000 tape->b_count = atomic_read(&bh->b_count);
1007 static void idetape_init_merge_buffer(idetape_tape_t *tape)
1009 struct idetape_bh *bh = tape->merge_bh;
1010 tape->bh = tape->merge_bh;
1012 if (tape->chrdev_dir == IDETAPE_DIR_WRITE)
1013 atomic_set(&bh->b_count, 0);
1015 tape->b_data = bh->b_data;
1016 tape->b_count = atomic_read(&bh->b_count);
1021 * Write a filemark if write_filemark=1. Flush the device buffers without
1022 * writing a filemark otherwise.
1024 static void idetape_create_write_filemark_cmd(ide_drive_t *drive,
1025 struct ide_atapi_pc *pc, int write_filemark)
1028 pc->c[0] = WRITE_FILEMARKS;
1029 pc->c[4] = write_filemark;
1030 pc->flags |= PC_FLAG_WAIT_FOR_DSC;
1033 static int idetape_wait_ready(ide_drive_t *drive, unsigned long timeout)
1035 idetape_tape_t *tape = drive->driver_data;
1036 struct gendisk *disk = tape->disk;
1037 int load_attempted = 0;
1039 /* Wait for the tape to become ready */
1040 set_bit(IDE_AFLAG_MEDIUM_PRESENT, &drive->atapi_flags);
1042 while (time_before(jiffies, timeout)) {
1043 if (ide_do_test_unit_ready(drive, disk) == 0)
1045 if ((tape->sense_key == 2 && tape->asc == 4 && tape->ascq == 2)
1046 || (tape->asc == 0x3A)) {
1050 ide_do_start_stop(drive, disk, IDETAPE_LU_LOAD_MASK);
1052 /* not about to be ready */
1053 } else if (!(tape->sense_key == 2 && tape->asc == 4 &&
1054 (tape->ascq == 1 || tape->ascq == 8)))
1061 static int idetape_flush_tape_buffers(ide_drive_t *drive)
1063 struct ide_tape_obj *tape = drive->driver_data;
1064 struct ide_atapi_pc pc;
1067 idetape_create_write_filemark_cmd(drive, &pc, 0);
1068 rc = ide_queue_pc_tail(drive, tape->disk, &pc);
1071 idetape_wait_ready(drive, 60 * 5 * HZ);
1075 static void idetape_create_read_position_cmd(struct ide_atapi_pc *pc)
1078 pc->c[0] = READ_POSITION;
1082 static int idetape_read_position(ide_drive_t *drive)
1084 idetape_tape_t *tape = drive->driver_data;
1085 struct ide_atapi_pc pc;
1088 debug_log(DBG_PROCS, "Enter %s\n", __func__);
1090 idetape_create_read_position_cmd(&pc);
1091 if (ide_queue_pc_tail(drive, tape->disk, &pc))
1093 position = tape->first_frame;
1097 static void idetape_create_locate_cmd(ide_drive_t *drive,
1098 struct ide_atapi_pc *pc,
1099 unsigned int block, u8 partition, int skip)
1102 pc->c[0] = POSITION_TO_ELEMENT;
1104 put_unaligned(cpu_to_be32(block), (unsigned int *) &pc->c[3]);
1105 pc->c[8] = partition;
1106 pc->flags |= PC_FLAG_WAIT_FOR_DSC;
1109 static void __ide_tape_discard_merge_buffer(ide_drive_t *drive)
1111 idetape_tape_t *tape = drive->driver_data;
1113 if (tape->chrdev_dir != IDETAPE_DIR_READ)
1116 clear_bit(IDE_AFLAG_FILEMARK, &drive->atapi_flags);
1117 tape->merge_bh_size = 0;
1118 if (tape->merge_bh != NULL) {
1119 ide_tape_kfree_buffer(tape);
1120 tape->merge_bh = NULL;
1123 tape->chrdev_dir = IDETAPE_DIR_NONE;
1127 * Position the tape to the requested block using the LOCATE packet command.
1128 * A READ POSITION command is then issued to check where we are positioned. Like
1129 * all higher level operations, we queue the commands at the tail of the request
1130 * queue and wait for their completion.
1132 static int idetape_position_tape(ide_drive_t *drive, unsigned int block,
1133 u8 partition, int skip)
1135 idetape_tape_t *tape = drive->driver_data;
1136 struct gendisk *disk = tape->disk;
1138 struct ide_atapi_pc pc;
1140 if (tape->chrdev_dir == IDETAPE_DIR_READ)
1141 __ide_tape_discard_merge_buffer(drive);
1142 idetape_wait_ready(drive, 60 * 5 * HZ);
1143 idetape_create_locate_cmd(drive, &pc, block, partition, skip);
1144 retval = ide_queue_pc_tail(drive, disk, &pc);
1148 idetape_create_read_position_cmd(&pc);
1149 return ide_queue_pc_tail(drive, disk, &pc);
1152 static void ide_tape_discard_merge_buffer(ide_drive_t *drive,
1153 int restore_position)
1155 idetape_tape_t *tape = drive->driver_data;
1158 __ide_tape_discard_merge_buffer(drive);
1159 if (restore_position) {
1160 position = idetape_read_position(drive);
1161 seek = position > 0 ? position : 0;
1162 if (idetape_position_tape(drive, seek, 0, 0)) {
1163 printk(KERN_INFO "ide-tape: %s: position_tape failed in"
1164 " %s\n", tape->name, __func__);
1171 * Generate a read/write request for the block device interface and wait for it
1174 static int idetape_queue_rw_tail(ide_drive_t *drive, int cmd, int blocks,
1175 struct idetape_bh *bh)
1177 idetape_tape_t *tape = drive->driver_data;
1181 debug_log(DBG_SENSE, "%s: cmd=%d\n", __func__, cmd);
1183 rq = blk_get_request(drive->queue, READ, __GFP_WAIT);
1184 rq->cmd_type = REQ_TYPE_SPECIAL;
1186 rq->rq_disk = tape->disk;
1187 rq->special = (void *)bh;
1188 rq->sector = tape->first_frame;
1189 rq->nr_sectors = blocks;
1190 rq->current_nr_sectors = blocks;
1191 blk_execute_rq(drive->queue, tape->disk, rq, 0);
1193 errors = rq->errors;
1194 ret = tape->blk_size * (blocks - rq->current_nr_sectors);
1195 blk_put_request(rq);
1197 if ((cmd & (REQ_IDETAPE_READ | REQ_IDETAPE_WRITE)) == 0)
1201 idetape_init_merge_buffer(tape);
1202 if (errors == IDE_DRV_ERROR_GENERAL)
1207 static void idetape_create_inquiry_cmd(struct ide_atapi_pc *pc)
1215 static void idetape_create_rewind_cmd(ide_drive_t *drive,
1216 struct ide_atapi_pc *pc)
1219 pc->c[0] = REZERO_UNIT;
1220 pc->flags |= PC_FLAG_WAIT_FOR_DSC;
1223 static void idetape_create_erase_cmd(struct ide_atapi_pc *pc)
1228 pc->flags |= PC_FLAG_WAIT_FOR_DSC;
1231 static void idetape_create_space_cmd(struct ide_atapi_pc *pc, int count, u8 cmd)
1235 put_unaligned(cpu_to_be32(count), (unsigned int *) &pc->c[1]);
1237 pc->flags |= PC_FLAG_WAIT_FOR_DSC;
1240 /* Queue up a character device originated write request. */
1241 static int idetape_add_chrdev_write_request(ide_drive_t *drive, int blocks)
1243 idetape_tape_t *tape = drive->driver_data;
1245 debug_log(DBG_CHRDEV, "Enter %s\n", __func__);
1247 return idetape_queue_rw_tail(drive, REQ_IDETAPE_WRITE,
1248 blocks, tape->merge_bh);
1251 static void ide_tape_flush_merge_buffer(ide_drive_t *drive)
1253 idetape_tape_t *tape = drive->driver_data;
1255 struct idetape_bh *bh;
1257 if (tape->chrdev_dir != IDETAPE_DIR_WRITE) {
1258 printk(KERN_ERR "ide-tape: bug: Trying to empty merge buffer"
1259 " but we are not writing.\n");
1262 if (tape->merge_bh_size > tape->buffer_size) {
1263 printk(KERN_ERR "ide-tape: bug: merge_buffer too big\n");
1264 tape->merge_bh_size = tape->buffer_size;
1266 if (tape->merge_bh_size) {
1267 blocks = tape->merge_bh_size / tape->blk_size;
1268 if (tape->merge_bh_size % tape->blk_size) {
1272 i = tape->blk_size - tape->merge_bh_size %
1274 bh = tape->bh->b_reqnext;
1276 atomic_set(&bh->b_count, 0);
1282 printk(KERN_INFO "ide-tape: bug,"
1286 min = min(i, (unsigned int)(bh->b_size -
1287 atomic_read(&bh->b_count)));
1288 memset(bh->b_data + atomic_read(&bh->b_count),
1290 atomic_add(min, &bh->b_count);
1295 (void) idetape_add_chrdev_write_request(drive, blocks);
1296 tape->merge_bh_size = 0;
1298 if (tape->merge_bh != NULL) {
1299 ide_tape_kfree_buffer(tape);
1300 tape->merge_bh = NULL;
1302 tape->chrdev_dir = IDETAPE_DIR_NONE;
1305 static int idetape_init_read(ide_drive_t *drive)
1307 idetape_tape_t *tape = drive->driver_data;
1310 /* Initialize read operation */
1311 if (tape->chrdev_dir != IDETAPE_DIR_READ) {
1312 if (tape->chrdev_dir == IDETAPE_DIR_WRITE) {
1313 ide_tape_flush_merge_buffer(drive);
1314 idetape_flush_tape_buffers(drive);
1316 if (tape->merge_bh || tape->merge_bh_size) {
1317 printk(KERN_ERR "ide-tape: merge_bh_size should be"
1319 tape->merge_bh_size = 0;
1321 tape->merge_bh = ide_tape_kmalloc_buffer(tape, 0, 0);
1322 if (!tape->merge_bh)
1324 tape->chrdev_dir = IDETAPE_DIR_READ;
1327 * Issue a read 0 command to ensure that DSC handshake is
1328 * switched from completion mode to buffer available mode.
1329 * No point in issuing this if DSC overlap isn't supported, some
1330 * drives (Seagate STT3401A) will return an error.
1332 if (drive->dev_flags & IDE_DFLAG_DSC_OVERLAP) {
1333 bytes_read = idetape_queue_rw_tail(drive,
1334 REQ_IDETAPE_READ, 0,
1336 if (bytes_read < 0) {
1337 ide_tape_kfree_buffer(tape);
1338 tape->merge_bh = NULL;
1339 tape->chrdev_dir = IDETAPE_DIR_NONE;
1348 /* called from idetape_chrdev_read() to service a chrdev read request. */
1349 static int idetape_add_chrdev_read_request(ide_drive_t *drive, int blocks)
1351 idetape_tape_t *tape = drive->driver_data;
1353 debug_log(DBG_PROCS, "Enter %s, %d blocks\n", __func__, blocks);
1355 /* If we are at a filemark, return a read length of 0 */
1356 if (test_bit(IDE_AFLAG_FILEMARK, &drive->atapi_flags))
1359 idetape_init_read(drive);
1361 return idetape_queue_rw_tail(drive, REQ_IDETAPE_READ, blocks,
1365 static void idetape_pad_zeros(ide_drive_t *drive, int bcount)
1367 idetape_tape_t *tape = drive->driver_data;
1368 struct idetape_bh *bh;
1374 bh = tape->merge_bh;
1375 count = min(tape->buffer_size, bcount);
1377 blocks = count / tape->blk_size;
1379 atomic_set(&bh->b_count,
1380 min(count, (unsigned int)bh->b_size));
1381 memset(bh->b_data, 0, atomic_read(&bh->b_count));
1382 count -= atomic_read(&bh->b_count);
1385 idetape_queue_rw_tail(drive, REQ_IDETAPE_WRITE, blocks,
1391 * Rewinds the tape to the Beginning Of the current Partition (BOP). We
1392 * currently support only one partition.
1394 static int idetape_rewind_tape(ide_drive_t *drive)
1396 struct ide_tape_obj *tape = drive->driver_data;
1397 struct gendisk *disk = tape->disk;
1399 struct ide_atapi_pc pc;
1401 debug_log(DBG_SENSE, "Enter %s\n", __func__);
1403 idetape_create_rewind_cmd(drive, &pc);
1404 retval = ide_queue_pc_tail(drive, disk, &pc);
1408 idetape_create_read_position_cmd(&pc);
1409 retval = ide_queue_pc_tail(drive, disk, &pc);
1415 /* mtio.h compatible commands should be issued to the chrdev interface. */
1416 static int idetape_blkdev_ioctl(ide_drive_t *drive, unsigned int cmd,
1419 idetape_tape_t *tape = drive->driver_data;
1420 void __user *argp = (void __user *)arg;
1422 struct idetape_config {
1423 int dsc_rw_frequency;
1424 int dsc_media_access_frequency;
1428 debug_log(DBG_PROCS, "Enter %s\n", __func__);
1432 if (copy_from_user(&config, argp, sizeof(config)))
1434 tape->best_dsc_rw_freq = config.dsc_rw_frequency;
1437 config.dsc_rw_frequency = (int) tape->best_dsc_rw_freq;
1438 config.nr_stages = 1;
1439 if (copy_to_user(argp, &config, sizeof(config)))
1448 static int idetape_space_over_filemarks(ide_drive_t *drive, short mt_op,
1451 idetape_tape_t *tape = drive->driver_data;
1452 struct gendisk *disk = tape->disk;
1453 struct ide_atapi_pc pc;
1454 int retval, count = 0;
1455 int sprev = !!(tape->caps[4] & 0x20);
1459 if (MTBSF == mt_op || MTBSFM == mt_op) {
1462 mt_count = -mt_count;
1465 if (tape->chrdev_dir == IDETAPE_DIR_READ) {
1466 tape->merge_bh_size = 0;
1467 if (test_and_clear_bit(IDE_AFLAG_FILEMARK, &drive->atapi_flags))
1469 ide_tape_discard_merge_buffer(drive, 0);
1475 idetape_create_space_cmd(&pc, mt_count - count,
1476 IDETAPE_SPACE_OVER_FILEMARK);
1477 return ide_queue_pc_tail(drive, disk, &pc);
1482 retval = idetape_space_over_filemarks(drive, MTFSF,
1486 count = (MTBSFM == mt_op ? 1 : -1);
1487 return idetape_space_over_filemarks(drive, MTFSF, count);
1489 printk(KERN_ERR "ide-tape: MTIO operation %d not supported\n",
1496 * Our character device read / write functions.
1498 * The tape is optimized to maximize throughput when it is transferring an
1499 * integral number of the "continuous transfer limit", which is a parameter of
1500 * the specific tape (26kB on my particular tape, 32kB for Onstream).
1502 * As of version 1.3 of the driver, the character device provides an abstract
1503 * continuous view of the media - any mix of block sizes (even 1 byte) on the
1504 * same backup/restore procedure is supported. The driver will internally
1505 * convert the requests to the recommended transfer unit, so that an unmatch
1506 * between the user's block size to the recommended size will only result in a
1507 * (slightly) increased driver overhead, but will no longer hit performance.
1508 * This is not applicable to Onstream.
1510 static ssize_t idetape_chrdev_read(struct file *file, char __user *buf,
1511 size_t count, loff_t *ppos)
1513 struct ide_tape_obj *tape = file->private_data;
1514 ide_drive_t *drive = tape->drive;
1515 ssize_t bytes_read, temp, actually_read = 0, rc;
1517 u16 ctl = *(u16 *)&tape->caps[12];
1519 debug_log(DBG_CHRDEV, "Enter %s, count %Zd\n", __func__, count);
1521 if (tape->chrdev_dir != IDETAPE_DIR_READ) {
1522 if (test_bit(IDE_AFLAG_DETECT_BS, &drive->atapi_flags))
1523 if (count > tape->blk_size &&
1524 (count % tape->blk_size) == 0)
1525 tape->user_bs_factor = count / tape->blk_size;
1527 rc = idetape_init_read(drive);
1532 if (tape->merge_bh_size) {
1533 actually_read = min((unsigned int)(tape->merge_bh_size),
1534 (unsigned int)count);
1535 if (idetape_copy_stage_to_user(tape, buf, actually_read))
1537 buf += actually_read;
1538 tape->merge_bh_size -= actually_read;
1539 count -= actually_read;
1541 while (count >= tape->buffer_size) {
1542 bytes_read = idetape_add_chrdev_read_request(drive, ctl);
1543 if (bytes_read <= 0)
1545 if (idetape_copy_stage_to_user(tape, buf, bytes_read))
1548 count -= bytes_read;
1549 actually_read += bytes_read;
1552 bytes_read = idetape_add_chrdev_read_request(drive, ctl);
1553 if (bytes_read <= 0)
1555 temp = min((unsigned long)count, (unsigned long)bytes_read);
1556 if (idetape_copy_stage_to_user(tape, buf, temp))
1558 actually_read += temp;
1559 tape->merge_bh_size = bytes_read-temp;
1562 if (!actually_read && test_bit(IDE_AFLAG_FILEMARK, &drive->atapi_flags)) {
1563 debug_log(DBG_SENSE, "%s: spacing over filemark\n", tape->name);
1565 idetape_space_over_filemarks(drive, MTFSF, 1);
1569 return ret ? ret : actually_read;
1572 static ssize_t idetape_chrdev_write(struct file *file, const char __user *buf,
1573 size_t count, loff_t *ppos)
1575 struct ide_tape_obj *tape = file->private_data;
1576 ide_drive_t *drive = tape->drive;
1577 ssize_t actually_written = 0;
1579 u16 ctl = *(u16 *)&tape->caps[12];
1581 /* The drive is write protected. */
1582 if (tape->write_prot)
1585 debug_log(DBG_CHRDEV, "Enter %s, count %Zd\n", __func__, count);
1587 /* Initialize write operation */
1588 if (tape->chrdev_dir != IDETAPE_DIR_WRITE) {
1589 if (tape->chrdev_dir == IDETAPE_DIR_READ)
1590 ide_tape_discard_merge_buffer(drive, 1);
1591 if (tape->merge_bh || tape->merge_bh_size) {
1592 printk(KERN_ERR "ide-tape: merge_bh_size "
1593 "should be 0 now\n");
1594 tape->merge_bh_size = 0;
1596 tape->merge_bh = ide_tape_kmalloc_buffer(tape, 0, 0);
1597 if (!tape->merge_bh)
1599 tape->chrdev_dir = IDETAPE_DIR_WRITE;
1600 idetape_init_merge_buffer(tape);
1603 * Issue a write 0 command to ensure that DSC handshake is
1604 * switched from completion mode to buffer available mode. No
1605 * point in issuing this if DSC overlap isn't supported, some
1606 * drives (Seagate STT3401A) will return an error.
1608 if (drive->dev_flags & IDE_DFLAG_DSC_OVERLAP) {
1609 ssize_t retval = idetape_queue_rw_tail(drive,
1610 REQ_IDETAPE_WRITE, 0,
1613 ide_tape_kfree_buffer(tape);
1614 tape->merge_bh = NULL;
1615 tape->chrdev_dir = IDETAPE_DIR_NONE;
1622 if (tape->merge_bh_size) {
1623 if (tape->merge_bh_size >= tape->buffer_size) {
1624 printk(KERN_ERR "ide-tape: bug: merge buf too big\n");
1625 tape->merge_bh_size = 0;
1627 actually_written = min((unsigned int)
1628 (tape->buffer_size - tape->merge_bh_size),
1629 (unsigned int)count);
1630 if (idetape_copy_stage_from_user(tape, buf, actually_written))
1632 buf += actually_written;
1633 tape->merge_bh_size += actually_written;
1634 count -= actually_written;
1636 if (tape->merge_bh_size == tape->buffer_size) {
1638 tape->merge_bh_size = 0;
1639 retval = idetape_add_chrdev_write_request(drive, ctl);
1644 while (count >= tape->buffer_size) {
1646 if (idetape_copy_stage_from_user(tape, buf, tape->buffer_size))
1648 buf += tape->buffer_size;
1649 count -= tape->buffer_size;
1650 retval = idetape_add_chrdev_write_request(drive, ctl);
1651 actually_written += tape->buffer_size;
1656 actually_written += count;
1657 if (idetape_copy_stage_from_user(tape, buf, count))
1659 tape->merge_bh_size += count;
1661 return ret ? ret : actually_written;
1664 static int idetape_write_filemark(ide_drive_t *drive)
1666 struct ide_tape_obj *tape = drive->driver_data;
1667 struct ide_atapi_pc pc;
1669 /* Write a filemark */
1670 idetape_create_write_filemark_cmd(drive, &pc, 1);
1671 if (ide_queue_pc_tail(drive, tape->disk, &pc)) {
1672 printk(KERN_ERR "ide-tape: Couldn't write a filemark\n");
1679 * Called from idetape_chrdev_ioctl when the general mtio MTIOCTOP ioctl is
1682 * Note: MTBSF and MTBSFM are not supported when the tape doesn't support
1683 * spacing over filemarks in the reverse direction. In this case, MTFSFM is also
1684 * usually not supported.
1686 * The following commands are currently not supported:
1688 * MTFSS, MTBSS, MTWSM, MTSETDENSITY, MTSETDRVBUFFER, MT_ST_BOOLEANS,
1689 * MT_ST_WRITE_THRESHOLD.
1691 static int idetape_mtioctop(ide_drive_t *drive, short mt_op, int mt_count)
1693 idetape_tape_t *tape = drive->driver_data;
1694 struct gendisk *disk = tape->disk;
1695 struct ide_atapi_pc pc;
1698 debug_log(DBG_ERR, "Handling MTIOCTOP ioctl: mt_op=%d, mt_count=%d\n",
1708 return idetape_space_over_filemarks(drive, mt_op, mt_count);
1715 if (tape->write_prot)
1717 ide_tape_discard_merge_buffer(drive, 1);
1718 for (i = 0; i < mt_count; i++) {
1719 retval = idetape_write_filemark(drive);
1725 ide_tape_discard_merge_buffer(drive, 0);
1726 if (idetape_rewind_tape(drive))
1730 ide_tape_discard_merge_buffer(drive, 0);
1731 return ide_do_start_stop(drive, disk, IDETAPE_LU_LOAD_MASK);
1735 * If door is locked, attempt to unlock before
1736 * attempting to eject.
1738 if (tape->door_locked) {
1739 if (!ide_set_media_lock(drive, disk, 0))
1740 tape->door_locked = DOOR_UNLOCKED;
1742 ide_tape_discard_merge_buffer(drive, 0);
1743 retval = ide_do_start_stop(drive, disk, !IDETAPE_LU_LOAD_MASK);
1745 clear_bit(IDE_AFLAG_MEDIUM_PRESENT, &drive->atapi_flags);
1748 ide_tape_discard_merge_buffer(drive, 0);
1749 return idetape_flush_tape_buffers(drive);
1751 ide_tape_discard_merge_buffer(drive, 0);
1752 return ide_do_start_stop(drive, disk,
1753 IDETAPE_LU_RETENSION_MASK | IDETAPE_LU_LOAD_MASK);
1755 idetape_create_space_cmd(&pc, 0, IDETAPE_SPACE_TO_EOD);
1756 return ide_queue_pc_tail(drive, disk, &pc);
1758 (void)idetape_rewind_tape(drive);
1759 idetape_create_erase_cmd(&pc);
1760 return ide_queue_pc_tail(drive, disk, &pc);
1763 if (mt_count < tape->blk_size ||
1764 mt_count % tape->blk_size)
1766 tape->user_bs_factor = mt_count / tape->blk_size;
1767 clear_bit(IDE_AFLAG_DETECT_BS, &drive->atapi_flags);
1769 set_bit(IDE_AFLAG_DETECT_BS, &drive->atapi_flags);
1772 ide_tape_discard_merge_buffer(drive, 0);
1773 return idetape_position_tape(drive,
1774 mt_count * tape->user_bs_factor, tape->partition, 0);
1776 ide_tape_discard_merge_buffer(drive, 0);
1777 return idetape_position_tape(drive, 0, mt_count, 0);
1781 retval = ide_set_media_lock(drive, disk, 1);
1784 tape->door_locked = DOOR_EXPLICITLY_LOCKED;
1787 retval = ide_set_media_lock(drive, disk, 0);
1790 tape->door_locked = DOOR_UNLOCKED;
1793 printk(KERN_ERR "ide-tape: MTIO operation %d not supported\n",
1800 * Our character device ioctls. General mtio.h magnetic io commands are
1801 * supported here, and not in the corresponding block interface. Our own
1802 * ide-tape ioctls are supported on both interfaces.
1804 static int idetape_chrdev_ioctl(struct inode *inode, struct file *file,
1805 unsigned int cmd, unsigned long arg)
1807 struct ide_tape_obj *tape = file->private_data;
1808 ide_drive_t *drive = tape->drive;
1812 int block_offset = 0, position = tape->first_frame;
1813 void __user *argp = (void __user *)arg;
1815 debug_log(DBG_CHRDEV, "Enter %s, cmd=%u\n", __func__, cmd);
1817 if (tape->chrdev_dir == IDETAPE_DIR_WRITE) {
1818 ide_tape_flush_merge_buffer(drive);
1819 idetape_flush_tape_buffers(drive);
1821 if (cmd == MTIOCGET || cmd == MTIOCPOS) {
1822 block_offset = tape->merge_bh_size /
1823 (tape->blk_size * tape->user_bs_factor);
1824 position = idetape_read_position(drive);
1830 if (copy_from_user(&mtop, argp, sizeof(struct mtop)))
1832 return idetape_mtioctop(drive, mtop.mt_op, mtop.mt_count);
1834 memset(&mtget, 0, sizeof(struct mtget));
1835 mtget.mt_type = MT_ISSCSI2;
1836 mtget.mt_blkno = position / tape->user_bs_factor - block_offset;
1838 ((tape->blk_size * tape->user_bs_factor)
1839 << MT_ST_BLKSIZE_SHIFT) & MT_ST_BLKSIZE_MASK;
1841 if (tape->drv_write_prot)
1842 mtget.mt_gstat |= GMT_WR_PROT(0xffffffff);
1844 if (copy_to_user(argp, &mtget, sizeof(struct mtget)))
1848 mtpos.mt_blkno = position / tape->user_bs_factor - block_offset;
1849 if (copy_to_user(argp, &mtpos, sizeof(struct mtpos)))
1853 if (tape->chrdev_dir == IDETAPE_DIR_READ)
1854 ide_tape_discard_merge_buffer(drive, 1);
1855 return idetape_blkdev_ioctl(drive, cmd, arg);
1860 * Do a mode sense page 0 with block descriptor and if it succeeds set the tape
1861 * block size with the reported value.
1863 static void ide_tape_get_bsize_from_bdesc(ide_drive_t *drive)
1865 idetape_tape_t *tape = drive->driver_data;
1866 struct ide_atapi_pc pc;
1868 idetape_create_mode_sense_cmd(&pc, IDETAPE_BLOCK_DESCRIPTOR);
1869 if (ide_queue_pc_tail(drive, tape->disk, &pc)) {
1870 printk(KERN_ERR "ide-tape: Can't get block descriptor\n");
1871 if (tape->blk_size == 0) {
1872 printk(KERN_WARNING "ide-tape: Cannot deal with zero "
1873 "block size, assuming 32k\n");
1874 tape->blk_size = 32768;
1878 tape->blk_size = (pc.buf[4 + 5] << 16) +
1879 (pc.buf[4 + 6] << 8) +
1881 tape->drv_write_prot = (pc.buf[2] & 0x80) >> 7;
1884 static int idetape_chrdev_open(struct inode *inode, struct file *filp)
1886 unsigned int minor = iminor(inode), i = minor & ~0xc0;
1888 idetape_tape_t *tape;
1891 if (i >= MAX_HWIFS * MAX_DRIVES)
1895 tape = ide_tape_chrdev_get(i);
1901 debug_log(DBG_CHRDEV, "Enter %s\n", __func__);
1904 * We really want to do nonseekable_open(inode, filp); here, but some
1905 * versions of tar incorrectly call lseek on tapes and bail out if that
1906 * fails. So we disallow pread() and pwrite(), but permit lseeks.
1908 filp->f_mode &= ~(FMODE_PREAD | FMODE_PWRITE);
1910 drive = tape->drive;
1912 filp->private_data = tape;
1914 if (test_and_set_bit(IDE_AFLAG_BUSY, &drive->atapi_flags)) {
1919 retval = idetape_wait_ready(drive, 60 * HZ);
1921 clear_bit(IDE_AFLAG_BUSY, &drive->atapi_flags);
1922 printk(KERN_ERR "ide-tape: %s: drive not ready\n", tape->name);
1926 idetape_read_position(drive);
1927 if (!test_bit(IDE_AFLAG_ADDRESS_VALID, &drive->atapi_flags))
1928 (void)idetape_rewind_tape(drive);
1930 /* Read block size and write protect status from drive. */
1931 ide_tape_get_bsize_from_bdesc(drive);
1933 /* Set write protect flag if device is opened as read-only. */
1934 if ((filp->f_flags & O_ACCMODE) == O_RDONLY)
1935 tape->write_prot = 1;
1937 tape->write_prot = tape->drv_write_prot;
1939 /* Make sure drive isn't write protected if user wants to write. */
1940 if (tape->write_prot) {
1941 if ((filp->f_flags & O_ACCMODE) == O_WRONLY ||
1942 (filp->f_flags & O_ACCMODE) == O_RDWR) {
1943 clear_bit(IDE_AFLAG_BUSY, &drive->atapi_flags);
1949 /* Lock the tape drive door so user can't eject. */
1950 if (tape->chrdev_dir == IDETAPE_DIR_NONE) {
1951 if (!ide_set_media_lock(drive, tape->disk, 1)) {
1952 if (tape->door_locked != DOOR_EXPLICITLY_LOCKED)
1953 tape->door_locked = DOOR_LOCKED;
1965 static void idetape_write_release(ide_drive_t *drive, unsigned int minor)
1967 idetape_tape_t *tape = drive->driver_data;
1969 ide_tape_flush_merge_buffer(drive);
1970 tape->merge_bh = ide_tape_kmalloc_buffer(tape, 1, 0);
1971 if (tape->merge_bh != NULL) {
1972 idetape_pad_zeros(drive, tape->blk_size *
1973 (tape->user_bs_factor - 1));
1974 ide_tape_kfree_buffer(tape);
1975 tape->merge_bh = NULL;
1977 idetape_write_filemark(drive);
1978 idetape_flush_tape_buffers(drive);
1979 idetape_flush_tape_buffers(drive);
1982 static int idetape_chrdev_release(struct inode *inode, struct file *filp)
1984 struct ide_tape_obj *tape = filp->private_data;
1985 ide_drive_t *drive = tape->drive;
1986 unsigned int minor = iminor(inode);
1989 tape = drive->driver_data;
1991 debug_log(DBG_CHRDEV, "Enter %s\n", __func__);
1993 if (tape->chrdev_dir == IDETAPE_DIR_WRITE)
1994 idetape_write_release(drive, minor);
1995 if (tape->chrdev_dir == IDETAPE_DIR_READ) {
1997 ide_tape_discard_merge_buffer(drive, 1);
2000 if (minor < 128 && test_bit(IDE_AFLAG_MEDIUM_PRESENT, &drive->atapi_flags))
2001 (void) idetape_rewind_tape(drive);
2002 if (tape->chrdev_dir == IDETAPE_DIR_NONE) {
2003 if (tape->door_locked == DOOR_LOCKED) {
2004 if (!ide_set_media_lock(drive, tape->disk, 0))
2005 tape->door_locked = DOOR_UNLOCKED;
2008 clear_bit(IDE_AFLAG_BUSY, &drive->atapi_flags);
2014 static void idetape_get_inquiry_results(ide_drive_t *drive)
2016 idetape_tape_t *tape = drive->driver_data;
2017 struct ide_atapi_pc pc;
2019 char fw_rev[4], vendor_id[8], product_id[16];
2021 idetape_create_inquiry_cmd(&pc);
2022 pc.buf = &pc_buf[0];
2023 pc.buf_size = sizeof(pc_buf);
2025 if (ide_queue_pc_tail(drive, tape->disk, &pc)) {
2026 printk(KERN_ERR "ide-tape: %s: can't get INQUIRY results\n",
2030 memcpy(vendor_id, &pc.buf[8], 8);
2031 memcpy(product_id, &pc.buf[16], 16);
2032 memcpy(fw_rev, &pc.buf[32], 4);
2034 ide_fixstring(vendor_id, 8, 0);
2035 ide_fixstring(product_id, 16, 0);
2036 ide_fixstring(fw_rev, 4, 0);
2038 printk(KERN_INFO "ide-tape: %s <-> %s: %.8s %.16s rev %.4s\n",
2039 drive->name, tape->name, vendor_id, product_id, fw_rev);
2043 * Ask the tape about its various parameters. In particular, we will adjust our
2044 * data transfer buffer size to the recommended value as returned by the tape.
2046 static void idetape_get_mode_sense_results(ide_drive_t *drive)
2048 idetape_tape_t *tape = drive->driver_data;
2049 struct ide_atapi_pc pc;
2051 u8 speed, max_speed;
2053 idetape_create_mode_sense_cmd(&pc, IDETAPE_CAPABILITIES_PAGE);
2054 if (ide_queue_pc_tail(drive, tape->disk, &pc)) {
2055 printk(KERN_ERR "ide-tape: Can't get tape parameters - assuming"
2056 " some default values\n");
2057 tape->blk_size = 512;
2058 put_unaligned(52, (u16 *)&tape->caps[12]);
2059 put_unaligned(540, (u16 *)&tape->caps[14]);
2060 put_unaligned(6*52, (u16 *)&tape->caps[16]);
2063 caps = pc.buf + 4 + pc.buf[3];
2065 /* convert to host order and save for later use */
2066 speed = be16_to_cpup((__be16 *)&caps[14]);
2067 max_speed = be16_to_cpup((__be16 *)&caps[8]);
2069 *(u16 *)&caps[8] = max_speed;
2070 *(u16 *)&caps[12] = be16_to_cpup((__be16 *)&caps[12]);
2071 *(u16 *)&caps[14] = speed;
2072 *(u16 *)&caps[16] = be16_to_cpup((__be16 *)&caps[16]);
2075 printk(KERN_INFO "ide-tape: %s: invalid tape speed "
2076 "(assuming 650KB/sec)\n", drive->name);
2077 *(u16 *)&caps[14] = 650;
2080 printk(KERN_INFO "ide-tape: %s: invalid max_speed "
2081 "(assuming 650KB/sec)\n", drive->name);
2082 *(u16 *)&caps[8] = 650;
2085 memcpy(&tape->caps, caps, 20);
2087 /* device lacks locking support according to capabilities page */
2088 if ((caps[6] & 1) == 0)
2089 drive->dev_flags &= ~IDE_DFLAG_DOORLOCKING;
2092 tape->blk_size = 512;
2093 else if (caps[7] & 0x04)
2094 tape->blk_size = 1024;
2097 #ifdef CONFIG_IDE_PROC_FS
2098 #define ide_tape_devset_get(name, field) \
2099 static int get_##name(ide_drive_t *drive) \
2101 idetape_tape_t *tape = drive->driver_data; \
2102 return tape->field; \
2105 #define ide_tape_devset_set(name, field) \
2106 static int set_##name(ide_drive_t *drive, int arg) \
2108 idetape_tape_t *tape = drive->driver_data; \
2109 tape->field = arg; \
2113 #define ide_tape_devset_rw_field(_name, _field) \
2114 ide_tape_devset_get(_name, _field) \
2115 ide_tape_devset_set(_name, _field) \
2116 IDE_DEVSET(_name, DS_SYNC, get_##_name, set_##_name)
2118 #define ide_tape_devset_r_field(_name, _field) \
2119 ide_tape_devset_get(_name, _field) \
2120 IDE_DEVSET(_name, 0, get_##_name, NULL)
2122 static int mulf_tdsc(ide_drive_t *drive) { return 1000; }
2123 static int divf_tdsc(ide_drive_t *drive) { return HZ; }
2124 static int divf_buffer(ide_drive_t *drive) { return 2; }
2125 static int divf_buffer_size(ide_drive_t *drive) { return 1024; }
2127 ide_devset_rw_flag(dsc_overlap, IDE_DFLAG_DSC_OVERLAP);
2129 ide_tape_devset_rw_field(debug_mask, debug_mask);
2130 ide_tape_devset_rw_field(tdsc, best_dsc_rw_freq);
2132 ide_tape_devset_r_field(avg_speed, avg_speed);
2133 ide_tape_devset_r_field(speed, caps[14]);
2134 ide_tape_devset_r_field(buffer, caps[16]);
2135 ide_tape_devset_r_field(buffer_size, buffer_size);
2137 static const struct ide_proc_devset idetape_settings[] = {
2138 __IDE_PROC_DEVSET(avg_speed, 0, 0xffff, NULL, NULL),
2139 __IDE_PROC_DEVSET(buffer, 0, 0xffff, NULL, divf_buffer),
2140 __IDE_PROC_DEVSET(buffer_size, 0, 0xffff, NULL, divf_buffer_size),
2141 __IDE_PROC_DEVSET(debug_mask, 0, 0xffff, NULL, NULL),
2142 __IDE_PROC_DEVSET(dsc_overlap, 0, 1, NULL, NULL),
2143 __IDE_PROC_DEVSET(speed, 0, 0xffff, NULL, NULL),
2144 __IDE_PROC_DEVSET(tdsc, IDETAPE_DSC_RW_MIN, IDETAPE_DSC_RW_MAX,
2145 mulf_tdsc, divf_tdsc),
2151 * The function below is called to:
2153 * 1. Initialize our various state variables.
2154 * 2. Ask the tape for its capabilities.
2155 * 3. Allocate a buffer which will be used for data transfer. The buffer size
2156 * is chosen based on the recommendation which we received in step 2.
2158 * Note that at this point ide.c already assigned us an irq, so that we can
2159 * queue requests here and wait for their completion.
2161 static void idetape_setup(ide_drive_t *drive, idetape_tape_t *tape, int minor)
2166 u16 *ctl = (u16 *)&tape->caps[12];
2168 drive->pc_callback = ide_tape_callback;
2169 drive->pc_update_buffers = idetape_update_buffers;
2170 drive->pc_io_buffers = ide_tape_io_buffers;
2172 drive->dev_flags |= IDE_DFLAG_DSC_OVERLAP;
2174 if (drive->hwif->host_flags & IDE_HFLAG_NO_DSC) {
2175 printk(KERN_INFO "ide-tape: %s: disabling DSC overlap\n",
2177 drive->dev_flags &= ~IDE_DFLAG_DSC_OVERLAP;
2180 /* Seagate Travan drives do not support DSC overlap. */
2181 if (strstr((char *)&drive->id[ATA_ID_PROD], "Seagate STT3401"))
2182 drive->dev_flags &= ~IDE_DFLAG_DSC_OVERLAP;
2184 tape->minor = minor;
2185 tape->name[0] = 'h';
2186 tape->name[1] = 't';
2187 tape->name[2] = '0' + minor;
2188 tape->chrdev_dir = IDETAPE_DIR_NONE;
2190 idetape_get_inquiry_results(drive);
2191 idetape_get_mode_sense_results(drive);
2192 ide_tape_get_bsize_from_bdesc(drive);
2193 tape->user_bs_factor = 1;
2194 tape->buffer_size = *ctl * tape->blk_size;
2195 while (tape->buffer_size > 0xffff) {
2196 printk(KERN_NOTICE "ide-tape: decreasing stage size\n");
2198 tape->buffer_size = *ctl * tape->blk_size;
2200 buffer_size = tape->buffer_size;
2201 tape->pages_per_buffer = buffer_size / PAGE_SIZE;
2202 if (buffer_size % PAGE_SIZE) {
2203 tape->pages_per_buffer++;
2204 tape->excess_bh_size = PAGE_SIZE - buffer_size % PAGE_SIZE;
2207 /* select the "best" DSC read/write polling freq */
2208 speed = max(*(u16 *)&tape->caps[14], *(u16 *)&tape->caps[8]);
2210 t = (IDETAPE_FIFO_THRESHOLD * tape->buffer_size * HZ) / (speed * 1000);
2213 * Ensure that the number we got makes sense; limit it within
2214 * IDETAPE_DSC_RW_MIN and IDETAPE_DSC_RW_MAX.
2216 tape->best_dsc_rw_freq = clamp_t(unsigned long, t, IDETAPE_DSC_RW_MIN,
2217 IDETAPE_DSC_RW_MAX);
2218 printk(KERN_INFO "ide-tape: %s <-> %s: %dKBps, %d*%dkB buffer, "
2220 drive->name, tape->name, *(u16 *)&tape->caps[14],
2221 (*(u16 *)&tape->caps[16] * 512) / tape->buffer_size,
2222 tape->buffer_size / 1024,
2223 tape->best_dsc_rw_freq * 1000 / HZ,
2224 (drive->dev_flags & IDE_DFLAG_USING_DMA) ? ", DMA" : "");
2226 ide_proc_register_driver(drive, tape->driver);
2229 static void ide_tape_remove(ide_drive_t *drive)
2231 idetape_tape_t *tape = drive->driver_data;
2233 ide_proc_unregister_driver(drive, tape->driver);
2234 device_del(&tape->dev);
2235 ide_unregister_region(tape->disk);
2237 mutex_lock(&idetape_ref_mutex);
2238 put_device(&tape->dev);
2239 mutex_unlock(&idetape_ref_mutex);
2242 static void ide_tape_release(struct device *dev)
2244 struct ide_tape_obj *tape = to_ide_drv(dev, ide_tape_obj);
2245 ide_drive_t *drive = tape->drive;
2246 struct gendisk *g = tape->disk;
2248 BUG_ON(tape->merge_bh_size);
2250 drive->dev_flags &= ~IDE_DFLAG_DSC_OVERLAP;
2251 drive->driver_data = NULL;
2252 device_destroy(idetape_sysfs_class, MKDEV(IDETAPE_MAJOR, tape->minor));
2253 device_destroy(idetape_sysfs_class,
2254 MKDEV(IDETAPE_MAJOR, tape->minor + 128));
2255 idetape_devs[tape->minor] = NULL;
2256 g->private_data = NULL;
2261 #ifdef CONFIG_IDE_PROC_FS
2262 static int proc_idetape_read_name
2263 (char *page, char **start, off_t off, int count, int *eof, void *data)
2265 ide_drive_t *drive = (ide_drive_t *) data;
2266 idetape_tape_t *tape = drive->driver_data;
2270 len = sprintf(out, "%s\n", tape->name);
2271 PROC_IDE_READ_RETURN(page, start, off, count, eof, len);
2274 static ide_proc_entry_t idetape_proc[] = {
2275 { "capacity", S_IFREG|S_IRUGO, proc_ide_read_capacity, NULL },
2276 { "name", S_IFREG|S_IRUGO, proc_idetape_read_name, NULL },
2277 { NULL, 0, NULL, NULL }
2280 static ide_proc_entry_t *ide_tape_proc_entries(ide_drive_t *drive)
2282 return idetape_proc;
2285 static const struct ide_proc_devset *ide_tape_proc_devsets(ide_drive_t *drive)
2287 return idetape_settings;
2291 static int ide_tape_probe(ide_drive_t *);
2293 static struct ide_driver idetape_driver = {
2295 .owner = THIS_MODULE,
2297 .bus = &ide_bus_type,
2299 .probe = ide_tape_probe,
2300 .remove = ide_tape_remove,
2301 .version = IDETAPE_VERSION,
2302 .do_request = idetape_do_request,
2303 #ifdef CONFIG_IDE_PROC_FS
2304 .proc_entries = ide_tape_proc_entries,
2305 .proc_devsets = ide_tape_proc_devsets,
2309 /* Our character device supporting functions, passed to register_chrdev. */
2310 static const struct file_operations idetape_fops = {
2311 .owner = THIS_MODULE,
2312 .read = idetape_chrdev_read,
2313 .write = idetape_chrdev_write,
2314 .ioctl = idetape_chrdev_ioctl,
2315 .open = idetape_chrdev_open,
2316 .release = idetape_chrdev_release,
2319 static int idetape_open(struct block_device *bdev, fmode_t mode)
2321 struct ide_tape_obj *tape = ide_tape_get(bdev->bd_disk);
2329 static int idetape_release(struct gendisk *disk, fmode_t mode)
2331 struct ide_tape_obj *tape = ide_drv_g(disk, ide_tape_obj);
2337 static int idetape_ioctl(struct block_device *bdev, fmode_t mode,
2338 unsigned int cmd, unsigned long arg)
2340 struct ide_tape_obj *tape = ide_drv_g(bdev->bd_disk, ide_tape_obj);
2341 ide_drive_t *drive = tape->drive;
2342 int err = generic_ide_ioctl(drive, bdev, cmd, arg);
2344 err = idetape_blkdev_ioctl(drive, cmd, arg);
2348 static struct block_device_operations idetape_block_ops = {
2349 .owner = THIS_MODULE,
2350 .open = idetape_open,
2351 .release = idetape_release,
2352 .locked_ioctl = idetape_ioctl,
2355 static int ide_tape_probe(ide_drive_t *drive)
2357 idetape_tape_t *tape;
2361 if (!strstr("ide-tape", drive->driver_req))
2364 if (drive->media != ide_tape)
2367 if ((drive->dev_flags & IDE_DFLAG_ID_READ) &&
2368 ide_check_atapi_device(drive, DRV_NAME) == 0) {
2369 printk(KERN_ERR "ide-tape: %s: not supported by this version of"
2370 " the driver\n", drive->name);
2373 tape = kzalloc(sizeof(idetape_tape_t), GFP_KERNEL);
2375 printk(KERN_ERR "ide-tape: %s: Can't allocate a tape struct\n",
2380 g = alloc_disk(1 << PARTN_BITS);
2384 ide_init_disk(g, drive);
2386 tape->dev.parent = &drive->gendev;
2387 tape->dev.release = ide_tape_release;
2388 dev_set_name(&tape->dev, dev_name(&drive->gendev));
2390 if (device_register(&tape->dev))
2393 tape->drive = drive;
2394 tape->driver = &idetape_driver;
2397 g->private_data = &tape->driver;
2399 drive->driver_data = tape;
2401 mutex_lock(&idetape_ref_mutex);
2402 for (minor = 0; idetape_devs[minor]; minor++)
2404 idetape_devs[minor] = tape;
2405 mutex_unlock(&idetape_ref_mutex);
2407 idetape_setup(drive, tape, minor);
2409 device_create(idetape_sysfs_class, &drive->gendev,
2410 MKDEV(IDETAPE_MAJOR, minor), NULL, "%s", tape->name);
2411 device_create(idetape_sysfs_class, &drive->gendev,
2412 MKDEV(IDETAPE_MAJOR, minor + 128), NULL,
2415 g->fops = &idetape_block_ops;
2416 ide_register_region(g);
2428 static void __exit idetape_exit(void)
2430 driver_unregister(&idetape_driver.gen_driver);
2431 class_destroy(idetape_sysfs_class);
2432 unregister_chrdev(IDETAPE_MAJOR, "ht");
2435 static int __init idetape_init(void)
2438 idetape_sysfs_class = class_create(THIS_MODULE, "ide_tape");
2439 if (IS_ERR(idetape_sysfs_class)) {
2440 idetape_sysfs_class = NULL;
2441 printk(KERN_ERR "Unable to create sysfs class for ide tapes\n");
2446 if (register_chrdev(IDETAPE_MAJOR, "ht", &idetape_fops)) {
2447 printk(KERN_ERR "ide-tape: Failed to register chrdev"
2450 goto out_free_class;
2453 error = driver_register(&idetape_driver.gen_driver);
2455 goto out_free_driver;
2460 driver_unregister(&idetape_driver.gen_driver);
2462 class_destroy(idetape_sysfs_class);
2467 MODULE_ALIAS("ide:*m-tape*");
2468 module_init(idetape_init);
2469 module_exit(idetape_exit);
2470 MODULE_ALIAS_CHARDEV_MAJOR(IDETAPE_MAJOR);
2471 MODULE_DESCRIPTION("ATAPI Streaming TAPE Driver");
2472 MODULE_LICENSE("GPL");