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>
50 /* define to see debug info */
51 #undef IDETAPE_DEBUG_LOG
53 #ifdef IDETAPE_DEBUG_LOG
54 #define ide_debug_log(lvl, fmt, args...) __ide_debug_log(lvl, fmt, ## args)
56 #define ide_debug_log(lvl, fmt, args...) do {} while (0)
59 /**************************** Tunable parameters *****************************/
61 * After each failed packet command we issue a request sense command and retry
62 * the packet command IDETAPE_MAX_PC_RETRIES times.
64 * Setting IDETAPE_MAX_PC_RETRIES to 0 will disable retries.
66 #define IDETAPE_MAX_PC_RETRIES 3
69 * The following parameter is used to select the point in the internal tape fifo
70 * in which we will start to refill the buffer. Decreasing the following
71 * parameter will improve the system's latency and interactive response, while
72 * using a high value might improve system throughput.
74 #define IDETAPE_FIFO_THRESHOLD 2
77 * DSC polling parameters.
79 * Polling for DSC (a single bit in the status register) is a very important
80 * function in ide-tape. There are two cases in which we poll for DSC:
82 * 1. Before a read/write packet command, to ensure that we can transfer data
83 * from/to the tape's data buffers, without causing an actual media access.
84 * In case the tape is not ready yet, we take out our request from the device
85 * request queue, so that ide.c could service requests from the other device
86 * on the same interface in the meantime.
88 * 2. After the successful initialization of a "media access packet command",
89 * which is a command that can take a long time to complete (the interval can
90 * range from several seconds to even an hour). Again, we postpone our request
91 * in the middle to free the bus for the other device. The polling frequency
92 * here should be lower than the read/write frequency since those media access
93 * commands are slow. We start from a "fast" frequency - IDETAPE_DSC_MA_FAST
94 * (1 second), and if we don't receive DSC after IDETAPE_DSC_MA_THRESHOLD
95 * (5 min), we switch it to a lower frequency - IDETAPE_DSC_MA_SLOW (1 min).
97 * We also set a timeout for the timer, in case something goes wrong. The
98 * timeout should be longer then the maximum execution time of a tape operation.
102 #define IDETAPE_DSC_RW_MIN 5*HZ/100 /* 50 msec */
103 #define IDETAPE_DSC_RW_MAX 40*HZ/100 /* 400 msec */
104 #define IDETAPE_DSC_RW_TIMEOUT 2*60*HZ /* 2 minutes */
105 #define IDETAPE_DSC_MA_FAST 2*HZ /* 2 seconds */
106 #define IDETAPE_DSC_MA_THRESHOLD 5*60*HZ /* 5 minutes */
107 #define IDETAPE_DSC_MA_SLOW 30*HZ /* 30 seconds */
108 #define IDETAPE_DSC_MA_TIMEOUT 2*60*60*HZ /* 2 hours */
110 /*************************** End of tunable parameters ***********************/
112 /* tape directions */
114 IDETAPE_DIR_NONE = (1 << 0),
115 IDETAPE_DIR_READ = (1 << 1),
116 IDETAPE_DIR_WRITE = (1 << 2),
119 /* Tape door status */
120 #define DOOR_UNLOCKED 0
121 #define DOOR_LOCKED 1
122 #define DOOR_EXPLICITLY_LOCKED 2
124 /* Some defines for the SPACE command */
125 #define IDETAPE_SPACE_OVER_FILEMARK 1
126 #define IDETAPE_SPACE_TO_EOD 3
128 /* Some defines for the LOAD UNLOAD command */
129 #define IDETAPE_LU_LOAD_MASK 1
130 #define IDETAPE_LU_RETENSION_MASK 2
131 #define IDETAPE_LU_EOT_MASK 4
133 /* Structures related to the SELECT SENSE / MODE SENSE packet commands. */
134 #define IDETAPE_BLOCK_DESCRIPTOR 0
135 #define IDETAPE_CAPABILITIES_PAGE 0x2a
138 * Most of our global data which we need to save even as we leave the driver due
139 * to an interrupt or a timer event is stored in the struct defined below.
141 typedef struct ide_tape_obj {
143 struct ide_driver *driver;
144 struct gendisk *disk;
147 /* used by REQ_IDETAPE_{READ,WRITE} requests */
148 struct ide_atapi_pc queued_pc;
151 * DSC polling variables.
153 * While polling for DSC we use postponed_rq to postpone the current
154 * request so that ide.c will be able to service pending requests on the
155 * other device. Note that at most we will have only one DSC (usually
156 * data transfer) request in the device request queue.
160 /* The time in which we started polling for DSC */
161 unsigned long dsc_polling_start;
162 /* Timer used to poll for dsc */
163 struct timer_list dsc_timer;
164 /* Read/Write dsc polling frequency */
165 unsigned long best_dsc_rw_freq;
166 unsigned long dsc_poll_freq;
167 unsigned long dsc_timeout;
169 /* Read position information */
172 unsigned int first_frame;
174 /* Last error information */
175 u8 sense_key, asc, ascq;
177 /* Character device operation */
181 /* Current character device data transfer direction */
184 /* tape block size, usually 512 or 1024 bytes */
185 unsigned short blk_size;
188 /* Copy of the tape's Capabilities and Mechanical Page */
192 * Active data transfer request parameters.
194 * At most, there is only one ide-tape originated data transfer request
195 * in the device request queue. This allows ide.c to easily service
196 * requests from the other device when we postpone our active request.
199 /* Data buffer size chosen based on the tape's recommendation */
201 /* Staging buffer of buffer_size bytes */
203 /* The read/write cursor */
205 /* The number of valid bytes in buf */
208 /* Measures average tape speed */
209 unsigned long avg_time;
213 /* the door is currently locked */
215 /* the tape hardware is write protected */
217 /* the tape is write protected (hardware or opened as read-only) */
221 static DEFINE_MUTEX(idetape_ref_mutex);
223 static struct class *idetape_sysfs_class;
225 static void ide_tape_release(struct device *);
227 static struct ide_tape_obj *idetape_devs[MAX_HWIFS * MAX_DRIVES];
229 static struct ide_tape_obj *ide_tape_get(struct gendisk *disk, bool cdev,
232 struct ide_tape_obj *tape = NULL;
234 mutex_lock(&idetape_ref_mutex);
237 tape = idetape_devs[i];
239 tape = ide_drv_g(disk, ide_tape_obj);
242 if (ide_device_get(tape->drive))
245 get_device(&tape->dev);
248 mutex_unlock(&idetape_ref_mutex);
252 static void ide_tape_put(struct ide_tape_obj *tape)
254 ide_drive_t *drive = tape->drive;
256 mutex_lock(&idetape_ref_mutex);
257 put_device(&tape->dev);
258 ide_device_put(drive);
259 mutex_unlock(&idetape_ref_mutex);
263 * called on each failed packet command retry to analyze the request sense. We
264 * currently do not utilize this information.
266 static void idetape_analyze_error(ide_drive_t *drive)
268 idetape_tape_t *tape = drive->driver_data;
269 struct ide_atapi_pc *pc = drive->failed_pc;
270 struct request *rq = drive->hwif->rq;
271 u8 *sense = bio_data(rq->bio);
273 tape->sense_key = sense[2] & 0xF;
274 tape->asc = sense[12];
275 tape->ascq = sense[13];
277 ide_debug_log(IDE_DBG_FUNC,
278 "cmd: 0x%x, sense key = %x, asc = %x, ascq = %x",
279 rq->cmd[0], tape->sense_key, tape->asc, tape->ascq);
281 /* correct remaining bytes to transfer */
282 if (pc->flags & PC_FLAG_DMA_ERROR)
283 rq->resid_len = tape->blk_size * get_unaligned_be32(&sense[3]);
286 * If error was the result of a zero-length read or write command,
287 * with sense key=5, asc=0x22, ascq=0, let it slide. Some drives
288 * (i.e. Seagate STT3401A Travan) don't support 0-length read/writes.
290 if ((pc->c[0] == READ_6 || pc->c[0] == WRITE_6)
292 && pc->c[4] == 0 && pc->c[3] == 0 && pc->c[2] == 0) {
293 if (tape->sense_key == 5) {
294 /* don't report an error, everything's ok */
296 /* don't retry read/write */
297 pc->flags |= PC_FLAG_ABORT;
300 if (pc->c[0] == READ_6 && (sense[2] & 0x80)) {
301 pc->error = IDE_DRV_ERROR_FILEMARK;
302 pc->flags |= PC_FLAG_ABORT;
304 if (pc->c[0] == WRITE_6) {
305 if ((sense[2] & 0x40) || (tape->sense_key == 0xd
306 && tape->asc == 0x0 && tape->ascq == 0x2)) {
307 pc->error = IDE_DRV_ERROR_EOD;
308 pc->flags |= PC_FLAG_ABORT;
311 if (pc->c[0] == READ_6 || pc->c[0] == WRITE_6) {
312 if (tape->sense_key == 8) {
313 pc->error = IDE_DRV_ERROR_EOD;
314 pc->flags |= PC_FLAG_ABORT;
316 if (!(pc->flags & PC_FLAG_ABORT) &&
317 (blk_rq_bytes(rq) - rq->resid_len))
318 pc->retries = IDETAPE_MAX_PC_RETRIES + 1;
322 static void ide_tape_handle_dsc(ide_drive_t *);
324 static int ide_tape_callback(ide_drive_t *drive, int dsc)
326 idetape_tape_t *tape = drive->driver_data;
327 struct ide_atapi_pc *pc = drive->pc;
328 struct request *rq = drive->hwif->rq;
329 int uptodate = pc->error ? 0 : 1;
330 int err = uptodate ? 0 : IDE_DRV_ERROR_GENERAL;
332 ide_debug_log(IDE_DBG_FUNC, "cmd: 0x%x, dsc: %d, err: %d", rq->cmd[0],
336 ide_tape_handle_dsc(drive);
338 if (drive->failed_pc == pc)
339 drive->failed_pc = NULL;
341 if (pc->c[0] == REQUEST_SENSE) {
343 idetape_analyze_error(drive);
345 printk(KERN_ERR "ide-tape: Error in REQUEST SENSE "
346 "itself - Aborting request!\n");
347 } else if (pc->c[0] == READ_6 || pc->c[0] == WRITE_6) {
348 unsigned int blocks =
349 (blk_rq_bytes(rq) - rq->resid_len) / tape->blk_size;
351 tape->avg_size += blocks * tape->blk_size;
353 if (time_after_eq(jiffies, tape->avg_time + HZ)) {
354 tape->avg_speed = tape->avg_size * HZ /
355 (jiffies - tape->avg_time) / 1024;
357 tape->avg_time = jiffies;
360 tape->first_frame += blocks;
373 * Postpone the current request so that ide.c will be able to service requests
374 * from another device on the same port while we are polling for DSC.
376 static void ide_tape_stall_queue(ide_drive_t *drive)
378 idetape_tape_t *tape = drive->driver_data;
380 ide_debug_log(IDE_DBG_FUNC, "cmd: 0x%x, dsc_poll_freq: %lu",
381 drive->hwif->rq->cmd[0], tape->dsc_poll_freq);
383 tape->postponed_rq = true;
385 ide_stall_queue(drive, tape->dsc_poll_freq);
388 static void ide_tape_handle_dsc(ide_drive_t *drive)
390 idetape_tape_t *tape = drive->driver_data;
392 /* Media access command */
393 tape->dsc_polling_start = jiffies;
394 tape->dsc_poll_freq = IDETAPE_DSC_MA_FAST;
395 tape->dsc_timeout = jiffies + IDETAPE_DSC_MA_TIMEOUT;
396 /* Allow ide.c to handle other requests */
397 ide_tape_stall_queue(drive);
401 * Packet Command Interface
403 * The current Packet Command is available in drive->pc, and will not change
404 * until we finish handling it. Each packet command is associated with a
405 * callback function that will be called when the command is finished.
407 * The handling will be done in three stages:
409 * 1. ide_tape_issue_pc will send the packet command to the drive, and will set
410 * the interrupt handler to ide_pc_intr.
412 * 2. On each interrupt, ide_pc_intr will be called. This step will be
413 * repeated until the device signals us that no more interrupts will be issued.
415 * 3. ATAPI Tape media access commands have immediate status with a delayed
416 * process. In case of a successful initiation of a media access packet command,
417 * the DSC bit will be set when the actual execution of the command is finished.
418 * Since the tape drive will not issue an interrupt, we have to poll for this
419 * event. In this case, we define the request as "low priority request" by
420 * setting rq_status to IDETAPE_RQ_POSTPONED, set a timer to poll for DSC and
423 * ide.c will then give higher priority to requests which originate from the
424 * other device, until will change rq_status to RQ_ACTIVE.
426 * 4. When the packet command is finished, it will be checked for errors.
428 * 5. In case an error was found, we queue a request sense packet command in
429 * front of the request queue and retry the operation up to
430 * IDETAPE_MAX_PC_RETRIES times.
432 * 6. In case no error was found, or we decided to give up and not to retry
433 * again, the callback function will be called and then we will handle the next
437 static ide_startstop_t ide_tape_issue_pc(ide_drive_t *drive,
439 struct ide_atapi_pc *pc)
441 idetape_tape_t *tape = drive->driver_data;
442 struct request *rq = drive->hwif->rq;
444 if (drive->failed_pc == NULL && pc->c[0] != REQUEST_SENSE)
445 drive->failed_pc = pc;
447 /* Set the current packet command */
450 if (pc->retries > IDETAPE_MAX_PC_RETRIES ||
451 (pc->flags & PC_FLAG_ABORT)) {
454 * We will "abort" retrying a packet command in case legitimate
455 * error code was received (crossing a filemark, or end of the
456 * media, for example).
458 if (!(pc->flags & PC_FLAG_ABORT)) {
459 if (!(pc->c[0] == TEST_UNIT_READY &&
460 tape->sense_key == 2 && tape->asc == 4 &&
461 (tape->ascq == 1 || tape->ascq == 8))) {
462 printk(KERN_ERR "ide-tape: %s: I/O error, "
463 "pc = %2x, key = %2x, "
464 "asc = %2x, ascq = %2x\n",
465 tape->name, pc->c[0],
466 tape->sense_key, tape->asc,
470 pc->error = IDE_DRV_ERROR_GENERAL;
473 drive->failed_pc = NULL;
474 drive->pc_callback(drive, 0);
475 ide_complete_rq(drive, -EIO, blk_rq_bytes(rq));
478 ide_debug_log(IDE_DBG_SENSE, "retry #%d, cmd: 0x%02x", pc->retries,
483 return ide_issue_pc(drive, cmd);
486 /* A mode sense command is used to "sense" tape parameters. */
487 static void idetape_create_mode_sense_cmd(struct ide_atapi_pc *pc, u8 page_code)
490 pc->c[0] = MODE_SENSE;
491 if (page_code != IDETAPE_BLOCK_DESCRIPTOR)
492 /* DBD = 1 - Don't return block descriptors */
494 pc->c[2] = page_code;
496 * Changed pc->c[3] to 0 (255 will at best return unused info).
498 * For SCSI this byte is defined as subpage instead of high byte
499 * of length and some IDE drives seem to interpret it this way
500 * and return an error when 255 is used.
503 /* We will just discard data in that case */
505 if (page_code == IDETAPE_BLOCK_DESCRIPTOR)
507 else if (page_code == IDETAPE_CAPABILITIES_PAGE)
513 static ide_startstop_t idetape_media_access_finished(ide_drive_t *drive)
515 ide_hwif_t *hwif = drive->hwif;
516 idetape_tape_t *tape = drive->driver_data;
517 struct ide_atapi_pc *pc = drive->pc;
520 stat = hwif->tp_ops->read_status(hwif);
522 if (stat & ATA_DSC) {
523 if (stat & ATA_ERR) {
525 if (pc->c[0] != TEST_UNIT_READY)
526 printk(KERN_ERR "ide-tape: %s: I/O error, ",
528 /* Retry operation */
534 pc->error = IDE_DRV_ERROR_GENERAL;
535 drive->failed_pc = NULL;
537 drive->pc_callback(drive, 0);
541 static void ide_tape_create_rw_cmd(idetape_tape_t *tape,
542 struct ide_atapi_pc *pc, struct request *rq,
545 unsigned int length = blk_rq_sectors(rq) / (tape->blk_size >> 9);
548 put_unaligned(cpu_to_be32(length), (unsigned int *) &pc->c[1]);
551 if (blk_rq_bytes(rq) == tape->buffer_size)
552 pc->flags |= PC_FLAG_DMA_OK;
554 if (opcode == READ_6)
556 else if (opcode == WRITE_6) {
558 pc->flags |= PC_FLAG_WRITING;
561 memcpy(rq->cmd, pc->c, 12);
564 static ide_startstop_t idetape_do_request(ide_drive_t *drive,
565 struct request *rq, sector_t block)
567 ide_hwif_t *hwif = drive->hwif;
568 idetape_tape_t *tape = drive->driver_data;
569 struct ide_atapi_pc *pc = NULL;
573 ide_debug_log(IDE_DBG_RQ, "cmd: 0x%x, sector: %llu, nr_sectors: %u",
574 rq->cmd[0], (unsigned long long)blk_rq_pos(rq),
577 BUG_ON(!(blk_special_request(rq) || blk_sense_request(rq)));
579 /* Retry a failed packet command */
580 if (drive->failed_pc && drive->pc->c[0] == REQUEST_SENSE) {
581 pc = drive->failed_pc;
586 * If the tape is still busy, postpone our request and service
587 * the other device meanwhile.
589 stat = hwif->tp_ops->read_status(hwif);
591 if ((drive->dev_flags & IDE_DFLAG_DSC_OVERLAP) == 0 &&
592 (rq->cmd[13] & REQ_IDETAPE_PC2) == 0)
593 drive->atapi_flags |= IDE_AFLAG_IGNORE_DSC;
595 if (drive->dev_flags & IDE_DFLAG_POST_RESET) {
596 drive->atapi_flags |= IDE_AFLAG_IGNORE_DSC;
597 drive->dev_flags &= ~IDE_DFLAG_POST_RESET;
600 if (!(drive->atapi_flags & IDE_AFLAG_IGNORE_DSC) &&
602 if (!tape->postponed_rq) {
603 tape->dsc_polling_start = jiffies;
604 tape->dsc_poll_freq = tape->best_dsc_rw_freq;
605 tape->dsc_timeout = jiffies + IDETAPE_DSC_RW_TIMEOUT;
606 } else if (time_after(jiffies, tape->dsc_timeout)) {
607 printk(KERN_ERR "ide-tape: %s: DSC timeout\n",
609 if (rq->cmd[13] & REQ_IDETAPE_PC2) {
610 idetape_media_access_finished(drive);
613 return ide_do_reset(drive);
615 } else if (time_after(jiffies,
616 tape->dsc_polling_start +
617 IDETAPE_DSC_MA_THRESHOLD))
618 tape->dsc_poll_freq = IDETAPE_DSC_MA_SLOW;
619 ide_tape_stall_queue(drive);
622 drive->atapi_flags &= ~IDE_AFLAG_IGNORE_DSC;
623 tape->postponed_rq = false;
626 if (rq->cmd[13] & REQ_IDETAPE_READ) {
627 pc = &tape->queued_pc;
628 ide_tape_create_rw_cmd(tape, pc, rq, READ_6);
631 if (rq->cmd[13] & REQ_IDETAPE_WRITE) {
632 pc = &tape->queued_pc;
633 ide_tape_create_rw_cmd(tape, pc, rq, WRITE_6);
636 if (rq->cmd[13] & REQ_IDETAPE_PC1) {
637 pc = (struct ide_atapi_pc *)rq->special;
638 rq->cmd[13] &= ~(REQ_IDETAPE_PC1);
639 rq->cmd[13] |= REQ_IDETAPE_PC2;
642 if (rq->cmd[13] & REQ_IDETAPE_PC2) {
643 idetape_media_access_finished(drive);
649 /* prepare sense request for this command */
650 ide_prep_sense(drive, rq);
652 memset(&cmd, 0, sizeof(cmd));
655 cmd.tf_flags |= IDE_TFLAG_WRITE;
659 ide_init_sg_cmd(&cmd, blk_rq_bytes(rq));
660 ide_map_sg(drive, &cmd);
662 return ide_tape_issue_pc(drive, &cmd, pc);
666 * Write a filemark if write_filemark=1. Flush the device buffers without
667 * writing a filemark otherwise.
669 static void idetape_create_write_filemark_cmd(ide_drive_t *drive,
670 struct ide_atapi_pc *pc, int write_filemark)
673 pc->c[0] = WRITE_FILEMARKS;
674 pc->c[4] = write_filemark;
675 pc->flags |= PC_FLAG_WAIT_FOR_DSC;
678 static int idetape_wait_ready(ide_drive_t *drive, unsigned long timeout)
680 idetape_tape_t *tape = drive->driver_data;
681 struct gendisk *disk = tape->disk;
682 int load_attempted = 0;
684 /* Wait for the tape to become ready */
685 set_bit(ilog2(IDE_AFLAG_MEDIUM_PRESENT), &drive->atapi_flags);
687 while (time_before(jiffies, timeout)) {
688 if (ide_do_test_unit_ready(drive, disk) == 0)
690 if ((tape->sense_key == 2 && tape->asc == 4 && tape->ascq == 2)
691 || (tape->asc == 0x3A)) {
695 ide_do_start_stop(drive, disk, IDETAPE_LU_LOAD_MASK);
697 /* not about to be ready */
698 } else if (!(tape->sense_key == 2 && tape->asc == 4 &&
699 (tape->ascq == 1 || tape->ascq == 8)))
706 static int idetape_flush_tape_buffers(ide_drive_t *drive)
708 struct ide_tape_obj *tape = drive->driver_data;
709 struct ide_atapi_pc pc;
712 idetape_create_write_filemark_cmd(drive, &pc, 0);
713 rc = ide_queue_pc_tail(drive, tape->disk, &pc, NULL, 0);
716 idetape_wait_ready(drive, 60 * 5 * HZ);
720 static int ide_tape_read_position(ide_drive_t *drive)
722 idetape_tape_t *tape = drive->driver_data;
723 struct ide_atapi_pc pc;
726 ide_debug_log(IDE_DBG_FUNC, "enter");
730 pc.c[0] = READ_POSITION;
733 if (ide_queue_pc_tail(drive, tape->disk, &pc, buf, pc.req_xfer))
737 ide_debug_log(IDE_DBG_FUNC, "BOP - %s",
738 (buf[0] & 0x80) ? "Yes" : "No");
739 ide_debug_log(IDE_DBG_FUNC, "EOP - %s",
740 (buf[0] & 0x40) ? "Yes" : "No");
743 printk(KERN_INFO "ide-tape: Block location is unknown"
745 clear_bit(ilog2(IDE_AFLAG_ADDRESS_VALID),
746 &drive->atapi_flags);
749 ide_debug_log(IDE_DBG_FUNC, "Block Location: %u",
750 be32_to_cpup((__be32 *)&buf[4]));
752 tape->partition = buf[1];
753 tape->first_frame = be32_to_cpup((__be32 *)&buf[4]);
754 set_bit(ilog2(IDE_AFLAG_ADDRESS_VALID),
755 &drive->atapi_flags);
759 return tape->first_frame;
762 static void idetape_create_locate_cmd(ide_drive_t *drive,
763 struct ide_atapi_pc *pc,
764 unsigned int block, u8 partition, int skip)
767 pc->c[0] = POSITION_TO_ELEMENT;
769 put_unaligned(cpu_to_be32(block), (unsigned int *) &pc->c[3]);
770 pc->c[8] = partition;
771 pc->flags |= PC_FLAG_WAIT_FOR_DSC;
774 static void __ide_tape_discard_merge_buffer(ide_drive_t *drive)
776 idetape_tape_t *tape = drive->driver_data;
778 if (tape->chrdev_dir != IDETAPE_DIR_READ)
781 clear_bit(ilog2(IDE_AFLAG_FILEMARK), &drive->atapi_flags);
783 if (tape->buf != NULL) {
788 tape->chrdev_dir = IDETAPE_DIR_NONE;
792 * Position the tape to the requested block using the LOCATE packet command.
793 * A READ POSITION command is then issued to check where we are positioned. Like
794 * all higher level operations, we queue the commands at the tail of the request
795 * queue and wait for their completion.
797 static int idetape_position_tape(ide_drive_t *drive, unsigned int block,
798 u8 partition, int skip)
800 idetape_tape_t *tape = drive->driver_data;
801 struct gendisk *disk = tape->disk;
803 struct ide_atapi_pc pc;
805 if (tape->chrdev_dir == IDETAPE_DIR_READ)
806 __ide_tape_discard_merge_buffer(drive);
807 idetape_wait_ready(drive, 60 * 5 * HZ);
808 idetape_create_locate_cmd(drive, &pc, block, partition, skip);
809 ret = ide_queue_pc_tail(drive, disk, &pc, NULL, 0);
813 ret = ide_tape_read_position(drive);
819 static void ide_tape_discard_merge_buffer(ide_drive_t *drive,
820 int restore_position)
822 idetape_tape_t *tape = drive->driver_data;
825 __ide_tape_discard_merge_buffer(drive);
826 if (restore_position) {
827 position = ide_tape_read_position(drive);
828 seek = position > 0 ? position : 0;
829 if (idetape_position_tape(drive, seek, 0, 0)) {
830 printk(KERN_INFO "ide-tape: %s: position_tape failed in"
831 " %s\n", tape->name, __func__);
838 * Generate a read/write request for the block device interface and wait for it
841 static int idetape_queue_rw_tail(ide_drive_t *drive, int cmd, int size)
843 idetape_tape_t *tape = drive->driver_data;
847 ide_debug_log(IDE_DBG_FUNC, "cmd: 0x%x, size: %d", cmd, size);
849 BUG_ON(cmd != REQ_IDETAPE_READ && cmd != REQ_IDETAPE_WRITE);
850 BUG_ON(size < 0 || size % tape->blk_size);
852 rq = blk_get_request(drive->queue, READ, __GFP_WAIT);
853 rq->cmd_type = REQ_TYPE_SPECIAL;
855 rq->rq_disk = tape->disk;
856 rq->__sector = tape->first_frame;
859 ret = blk_rq_map_kern(drive->queue, rq, tape->buf, size,
865 blk_execute_rq(drive->queue, tape->disk, rq, 0);
867 /* calculate the number of transferred bytes and update buffer state */
868 size -= rq->resid_len;
869 tape->cur = tape->buf;
870 if (cmd == REQ_IDETAPE_READ)
876 if (rq->errors == IDE_DRV_ERROR_GENERAL)
883 static void idetape_create_inquiry_cmd(struct ide_atapi_pc *pc)
891 static void idetape_create_rewind_cmd(ide_drive_t *drive,
892 struct ide_atapi_pc *pc)
895 pc->c[0] = REZERO_UNIT;
896 pc->flags |= PC_FLAG_WAIT_FOR_DSC;
899 static void idetape_create_erase_cmd(struct ide_atapi_pc *pc)
904 pc->flags |= PC_FLAG_WAIT_FOR_DSC;
907 static void idetape_create_space_cmd(struct ide_atapi_pc *pc, int count, u8 cmd)
911 put_unaligned(cpu_to_be32(count), (unsigned int *) &pc->c[1]);
913 pc->flags |= PC_FLAG_WAIT_FOR_DSC;
916 static void ide_tape_flush_merge_buffer(ide_drive_t *drive)
918 idetape_tape_t *tape = drive->driver_data;
920 if (tape->chrdev_dir != IDETAPE_DIR_WRITE) {
921 printk(KERN_ERR "ide-tape: bug: Trying to empty merge buffer"
922 " but we are not writing.\n");
926 size_t aligned = roundup(tape->valid, tape->blk_size);
928 memset(tape->cur, 0, aligned - tape->valid);
929 idetape_queue_rw_tail(drive, REQ_IDETAPE_WRITE, aligned);
933 tape->chrdev_dir = IDETAPE_DIR_NONE;
936 static int idetape_init_rw(ide_drive_t *drive, int dir)
938 idetape_tape_t *tape = drive->driver_data;
941 BUG_ON(dir != IDETAPE_DIR_READ && dir != IDETAPE_DIR_WRITE);
943 if (tape->chrdev_dir == dir)
946 if (tape->chrdev_dir == IDETAPE_DIR_READ)
947 ide_tape_discard_merge_buffer(drive, 1);
948 else if (tape->chrdev_dir == IDETAPE_DIR_WRITE) {
949 ide_tape_flush_merge_buffer(drive);
950 idetape_flush_tape_buffers(drive);
953 if (tape->buf || tape->valid) {
954 printk(KERN_ERR "ide-tape: valid should be 0 now\n");
958 tape->buf = kmalloc(tape->buffer_size, GFP_KERNEL);
961 tape->chrdev_dir = dir;
962 tape->cur = tape->buf;
965 * Issue a 0 rw command to ensure that DSC handshake is
966 * switched from completion mode to buffer available mode. No
967 * point in issuing this if DSC overlap isn't supported, some
968 * drives (Seagate STT3401A) will return an error.
970 if (drive->dev_flags & IDE_DFLAG_DSC_OVERLAP) {
971 int cmd = dir == IDETAPE_DIR_READ ? REQ_IDETAPE_READ
974 rc = idetape_queue_rw_tail(drive, cmd, 0);
978 tape->chrdev_dir = IDETAPE_DIR_NONE;
986 static void idetape_pad_zeros(ide_drive_t *drive, int bcount)
988 idetape_tape_t *tape = drive->driver_data;
990 memset(tape->buf, 0, tape->buffer_size);
993 unsigned int count = min(tape->buffer_size, bcount);
995 idetape_queue_rw_tail(drive, REQ_IDETAPE_WRITE, count);
1001 * Rewinds the tape to the Beginning Of the current Partition (BOP). We
1002 * currently support only one partition.
1004 static int idetape_rewind_tape(ide_drive_t *drive)
1006 struct ide_tape_obj *tape = drive->driver_data;
1007 struct gendisk *disk = tape->disk;
1008 struct ide_atapi_pc pc;
1011 ide_debug_log(IDE_DBG_FUNC, "enter");
1013 idetape_create_rewind_cmd(drive, &pc);
1014 ret = ide_queue_pc_tail(drive, disk, &pc, NULL, 0);
1018 ret = ide_tape_read_position(drive);
1024 /* mtio.h compatible commands should be issued to the chrdev interface. */
1025 static int idetape_blkdev_ioctl(ide_drive_t *drive, unsigned int cmd,
1028 idetape_tape_t *tape = drive->driver_data;
1029 void __user *argp = (void __user *)arg;
1031 struct idetape_config {
1032 int dsc_rw_frequency;
1033 int dsc_media_access_frequency;
1037 ide_debug_log(IDE_DBG_FUNC, "cmd: 0x%04x", cmd);
1041 if (copy_from_user(&config, argp, sizeof(config)))
1043 tape->best_dsc_rw_freq = config.dsc_rw_frequency;
1046 memset(&config, 0, sizeof(config));
1047 config.dsc_rw_frequency = (int) tape->best_dsc_rw_freq;
1048 config.nr_stages = 1;
1049 if (copy_to_user(argp, &config, sizeof(config)))
1058 static int idetape_space_over_filemarks(ide_drive_t *drive, short mt_op,
1061 idetape_tape_t *tape = drive->driver_data;
1062 struct gendisk *disk = tape->disk;
1063 struct ide_atapi_pc pc;
1064 int retval, count = 0;
1065 int sprev = !!(tape->caps[4] & 0x20);
1068 ide_debug_log(IDE_DBG_FUNC, "mt_op: %d, mt_count: %d", mt_op, mt_count);
1072 if (MTBSF == mt_op || MTBSFM == mt_op) {
1075 mt_count = -mt_count;
1078 if (tape->chrdev_dir == IDETAPE_DIR_READ) {
1080 if (test_and_clear_bit(ilog2(IDE_AFLAG_FILEMARK),
1081 &drive->atapi_flags))
1083 ide_tape_discard_merge_buffer(drive, 0);
1089 idetape_create_space_cmd(&pc, mt_count - count,
1090 IDETAPE_SPACE_OVER_FILEMARK);
1091 return ide_queue_pc_tail(drive, disk, &pc, NULL, 0);
1096 retval = idetape_space_over_filemarks(drive, MTFSF,
1100 count = (MTBSFM == mt_op ? 1 : -1);
1101 return idetape_space_over_filemarks(drive, MTFSF, count);
1103 printk(KERN_ERR "ide-tape: MTIO operation %d not supported\n",
1110 * Our character device read / write functions.
1112 * The tape is optimized to maximize throughput when it is transferring an
1113 * integral number of the "continuous transfer limit", which is a parameter of
1114 * the specific tape (26kB on my particular tape, 32kB for Onstream).
1116 * As of version 1.3 of the driver, the character device provides an abstract
1117 * continuous view of the media - any mix of block sizes (even 1 byte) on the
1118 * same backup/restore procedure is supported. The driver will internally
1119 * convert the requests to the recommended transfer unit, so that an unmatch
1120 * between the user's block size to the recommended size will only result in a
1121 * (slightly) increased driver overhead, but will no longer hit performance.
1122 * This is not applicable to Onstream.
1124 static ssize_t idetape_chrdev_read(struct file *file, char __user *buf,
1125 size_t count, loff_t *ppos)
1127 struct ide_tape_obj *tape = file->private_data;
1128 ide_drive_t *drive = tape->drive;
1133 ide_debug_log(IDE_DBG_FUNC, "count %Zd", count);
1135 if (tape->chrdev_dir != IDETAPE_DIR_READ) {
1136 if (test_bit(ilog2(IDE_AFLAG_DETECT_BS), &drive->atapi_flags))
1137 if (count > tape->blk_size &&
1138 (count % tape->blk_size) == 0)
1139 tape->user_bs_factor = count / tape->blk_size;
1142 rc = idetape_init_rw(drive, IDETAPE_DIR_READ);
1146 while (done < count) {
1149 /* refill if staging buffer is empty */
1151 /* If we are at a filemark, nothing more to read */
1152 if (test_bit(ilog2(IDE_AFLAG_FILEMARK),
1153 &drive->atapi_flags))
1156 if (idetape_queue_rw_tail(drive, REQ_IDETAPE_READ,
1157 tape->buffer_size) <= 0)
1162 todo = min_t(size_t, count - done, tape->valid);
1163 if (copy_to_user(buf + done, tape->cur, todo))
1167 tape->valid -= todo;
1171 if (!done && test_bit(ilog2(IDE_AFLAG_FILEMARK), &drive->atapi_flags)) {
1172 idetape_space_over_filemarks(drive, MTFSF, 1);
1176 return ret ? ret : done;
1179 static ssize_t idetape_chrdev_write(struct file *file, const char __user *buf,
1180 size_t count, loff_t *ppos)
1182 struct ide_tape_obj *tape = file->private_data;
1183 ide_drive_t *drive = tape->drive;
1188 /* The drive is write protected. */
1189 if (tape->write_prot)
1192 ide_debug_log(IDE_DBG_FUNC, "count %Zd", count);
1194 /* Initialize write operation */
1195 rc = idetape_init_rw(drive, IDETAPE_DIR_WRITE);
1199 while (done < count) {
1202 /* flush if staging buffer is full */
1203 if (tape->valid == tape->buffer_size &&
1204 idetape_queue_rw_tail(drive, REQ_IDETAPE_WRITE,
1205 tape->buffer_size) <= 0)
1209 todo = min_t(size_t, count - done,
1210 tape->buffer_size - tape->valid);
1211 if (copy_from_user(tape->cur, buf + done, todo))
1215 tape->valid += todo;
1219 return ret ? ret : done;
1222 static int idetape_write_filemark(ide_drive_t *drive)
1224 struct ide_tape_obj *tape = drive->driver_data;
1225 struct ide_atapi_pc pc;
1227 /* Write a filemark */
1228 idetape_create_write_filemark_cmd(drive, &pc, 1);
1229 if (ide_queue_pc_tail(drive, tape->disk, &pc, NULL, 0)) {
1230 printk(KERN_ERR "ide-tape: Couldn't write a filemark\n");
1237 * Called from idetape_chrdev_ioctl when the general mtio MTIOCTOP ioctl is
1240 * Note: MTBSF and MTBSFM are not supported when the tape doesn't support
1241 * spacing over filemarks in the reverse direction. In this case, MTFSFM is also
1242 * usually not supported.
1244 * The following commands are currently not supported:
1246 * MTFSS, MTBSS, MTWSM, MTSETDENSITY, MTSETDRVBUFFER, MT_ST_BOOLEANS,
1247 * MT_ST_WRITE_THRESHOLD.
1249 static int idetape_mtioctop(ide_drive_t *drive, short mt_op, int mt_count)
1251 idetape_tape_t *tape = drive->driver_data;
1252 struct gendisk *disk = tape->disk;
1253 struct ide_atapi_pc pc;
1256 ide_debug_log(IDE_DBG_FUNC, "MTIOCTOP ioctl: mt_op: %d, mt_count: %d",
1266 return idetape_space_over_filemarks(drive, mt_op, mt_count);
1273 if (tape->write_prot)
1275 ide_tape_discard_merge_buffer(drive, 1);
1276 for (i = 0; i < mt_count; i++) {
1277 retval = idetape_write_filemark(drive);
1283 ide_tape_discard_merge_buffer(drive, 0);
1284 if (idetape_rewind_tape(drive))
1288 ide_tape_discard_merge_buffer(drive, 0);
1289 return ide_do_start_stop(drive, disk, IDETAPE_LU_LOAD_MASK);
1293 * If door is locked, attempt to unlock before
1294 * attempting to eject.
1296 if (tape->door_locked) {
1297 if (!ide_set_media_lock(drive, disk, 0))
1298 tape->door_locked = DOOR_UNLOCKED;
1300 ide_tape_discard_merge_buffer(drive, 0);
1301 retval = ide_do_start_stop(drive, disk, !IDETAPE_LU_LOAD_MASK);
1303 clear_bit(ilog2(IDE_AFLAG_MEDIUM_PRESENT),
1304 &drive->atapi_flags);
1307 ide_tape_discard_merge_buffer(drive, 0);
1308 return idetape_flush_tape_buffers(drive);
1310 ide_tape_discard_merge_buffer(drive, 0);
1311 return ide_do_start_stop(drive, disk,
1312 IDETAPE_LU_RETENSION_MASK | IDETAPE_LU_LOAD_MASK);
1314 idetape_create_space_cmd(&pc, 0, IDETAPE_SPACE_TO_EOD);
1315 return ide_queue_pc_tail(drive, disk, &pc, NULL, 0);
1317 (void)idetape_rewind_tape(drive);
1318 idetape_create_erase_cmd(&pc);
1319 return ide_queue_pc_tail(drive, disk, &pc, NULL, 0);
1322 if (mt_count < tape->blk_size ||
1323 mt_count % tape->blk_size)
1325 tape->user_bs_factor = mt_count / tape->blk_size;
1326 clear_bit(ilog2(IDE_AFLAG_DETECT_BS),
1327 &drive->atapi_flags);
1329 set_bit(ilog2(IDE_AFLAG_DETECT_BS),
1330 &drive->atapi_flags);
1333 ide_tape_discard_merge_buffer(drive, 0);
1334 return idetape_position_tape(drive,
1335 mt_count * tape->user_bs_factor, tape->partition, 0);
1337 ide_tape_discard_merge_buffer(drive, 0);
1338 return idetape_position_tape(drive, 0, mt_count, 0);
1342 retval = ide_set_media_lock(drive, disk, 1);
1345 tape->door_locked = DOOR_EXPLICITLY_LOCKED;
1348 retval = ide_set_media_lock(drive, disk, 0);
1351 tape->door_locked = DOOR_UNLOCKED;
1354 printk(KERN_ERR "ide-tape: MTIO operation %d not supported\n",
1361 * Our character device ioctls. General mtio.h magnetic io commands are
1362 * supported here, and not in the corresponding block interface. Our own
1363 * ide-tape ioctls are supported on both interfaces.
1365 static int idetape_chrdev_ioctl(struct inode *inode, struct file *file,
1366 unsigned int cmd, unsigned long arg)
1368 struct ide_tape_obj *tape = file->private_data;
1369 ide_drive_t *drive = tape->drive;
1373 int block_offset = 0, position = tape->first_frame;
1374 void __user *argp = (void __user *)arg;
1376 ide_debug_log(IDE_DBG_FUNC, "cmd: 0x%x", cmd);
1378 if (tape->chrdev_dir == IDETAPE_DIR_WRITE) {
1379 ide_tape_flush_merge_buffer(drive);
1380 idetape_flush_tape_buffers(drive);
1382 if (cmd == MTIOCGET || cmd == MTIOCPOS) {
1383 block_offset = tape->valid /
1384 (tape->blk_size * tape->user_bs_factor);
1385 position = ide_tape_read_position(drive);
1391 if (copy_from_user(&mtop, argp, sizeof(struct mtop)))
1393 return idetape_mtioctop(drive, mtop.mt_op, mtop.mt_count);
1395 memset(&mtget, 0, sizeof(struct mtget));
1396 mtget.mt_type = MT_ISSCSI2;
1397 mtget.mt_blkno = position / tape->user_bs_factor - block_offset;
1399 ((tape->blk_size * tape->user_bs_factor)
1400 << MT_ST_BLKSIZE_SHIFT) & MT_ST_BLKSIZE_MASK;
1402 if (tape->drv_write_prot)
1403 mtget.mt_gstat |= GMT_WR_PROT(0xffffffff);
1405 if (copy_to_user(argp, &mtget, sizeof(struct mtget)))
1409 mtpos.mt_blkno = position / tape->user_bs_factor - block_offset;
1410 if (copy_to_user(argp, &mtpos, sizeof(struct mtpos)))
1414 if (tape->chrdev_dir == IDETAPE_DIR_READ)
1415 ide_tape_discard_merge_buffer(drive, 1);
1416 return idetape_blkdev_ioctl(drive, cmd, arg);
1421 * Do a mode sense page 0 with block descriptor and if it succeeds set the tape
1422 * block size with the reported value.
1424 static void ide_tape_get_bsize_from_bdesc(ide_drive_t *drive)
1426 idetape_tape_t *tape = drive->driver_data;
1427 struct ide_atapi_pc pc;
1430 idetape_create_mode_sense_cmd(&pc, IDETAPE_BLOCK_DESCRIPTOR);
1431 if (ide_queue_pc_tail(drive, tape->disk, &pc, buf, pc.req_xfer)) {
1432 printk(KERN_ERR "ide-tape: Can't get block descriptor\n");
1433 if (tape->blk_size == 0) {
1434 printk(KERN_WARNING "ide-tape: Cannot deal with zero "
1435 "block size, assuming 32k\n");
1436 tape->blk_size = 32768;
1440 tape->blk_size = (buf[4 + 5] << 16) +
1443 tape->drv_write_prot = (buf[2] & 0x80) >> 7;
1445 ide_debug_log(IDE_DBG_FUNC, "blk_size: %d, write_prot: %d",
1446 tape->blk_size, tape->drv_write_prot);
1449 static int idetape_chrdev_open(struct inode *inode, struct file *filp)
1451 unsigned int minor = iminor(inode), i = minor & ~0xc0;
1453 idetape_tape_t *tape;
1456 if (i >= MAX_HWIFS * MAX_DRIVES)
1460 tape = ide_tape_get(NULL, true, i);
1466 drive = tape->drive;
1467 filp->private_data = tape;
1469 ide_debug_log(IDE_DBG_FUNC, "enter");
1472 * We really want to do nonseekable_open(inode, filp); here, but some
1473 * versions of tar incorrectly call lseek on tapes and bail out if that
1474 * fails. So we disallow pread() and pwrite(), but permit lseeks.
1476 filp->f_mode &= ~(FMODE_PREAD | FMODE_PWRITE);
1479 if (test_and_set_bit(ilog2(IDE_AFLAG_BUSY), &drive->atapi_flags)) {
1484 retval = idetape_wait_ready(drive, 60 * HZ);
1486 clear_bit(ilog2(IDE_AFLAG_BUSY), &drive->atapi_flags);
1487 printk(KERN_ERR "ide-tape: %s: drive not ready\n", tape->name);
1491 ide_tape_read_position(drive);
1492 if (!test_bit(ilog2(IDE_AFLAG_ADDRESS_VALID), &drive->atapi_flags))
1493 (void)idetape_rewind_tape(drive);
1495 /* Read block size and write protect status from drive. */
1496 ide_tape_get_bsize_from_bdesc(drive);
1498 /* Set write protect flag if device is opened as read-only. */
1499 if ((filp->f_flags & O_ACCMODE) == O_RDONLY)
1500 tape->write_prot = 1;
1502 tape->write_prot = tape->drv_write_prot;
1504 /* Make sure drive isn't write protected if user wants to write. */
1505 if (tape->write_prot) {
1506 if ((filp->f_flags & O_ACCMODE) == O_WRONLY ||
1507 (filp->f_flags & O_ACCMODE) == O_RDWR) {
1508 clear_bit(ilog2(IDE_AFLAG_BUSY), &drive->atapi_flags);
1514 /* Lock the tape drive door so user can't eject. */
1515 if (tape->chrdev_dir == IDETAPE_DIR_NONE) {
1516 if (!ide_set_media_lock(drive, tape->disk, 1)) {
1517 if (tape->door_locked != DOOR_EXPLICITLY_LOCKED)
1518 tape->door_locked = DOOR_LOCKED;
1530 static void idetape_write_release(ide_drive_t *drive, unsigned int minor)
1532 idetape_tape_t *tape = drive->driver_data;
1534 ide_tape_flush_merge_buffer(drive);
1535 tape->buf = kmalloc(tape->buffer_size, GFP_KERNEL);
1536 if (tape->buf != NULL) {
1537 idetape_pad_zeros(drive, tape->blk_size *
1538 (tape->user_bs_factor - 1));
1542 idetape_write_filemark(drive);
1543 idetape_flush_tape_buffers(drive);
1544 idetape_flush_tape_buffers(drive);
1547 static int idetape_chrdev_release(struct inode *inode, struct file *filp)
1549 struct ide_tape_obj *tape = filp->private_data;
1550 ide_drive_t *drive = tape->drive;
1551 unsigned int minor = iminor(inode);
1554 tape = drive->driver_data;
1556 ide_debug_log(IDE_DBG_FUNC, "enter");
1558 if (tape->chrdev_dir == IDETAPE_DIR_WRITE)
1559 idetape_write_release(drive, minor);
1560 if (tape->chrdev_dir == IDETAPE_DIR_READ) {
1562 ide_tape_discard_merge_buffer(drive, 1);
1565 if (minor < 128 && test_bit(ilog2(IDE_AFLAG_MEDIUM_PRESENT),
1566 &drive->atapi_flags))
1567 (void) idetape_rewind_tape(drive);
1569 if (tape->chrdev_dir == IDETAPE_DIR_NONE) {
1570 if (tape->door_locked == DOOR_LOCKED) {
1571 if (!ide_set_media_lock(drive, tape->disk, 0))
1572 tape->door_locked = DOOR_UNLOCKED;
1575 clear_bit(ilog2(IDE_AFLAG_BUSY), &drive->atapi_flags);
1581 static void idetape_get_inquiry_results(ide_drive_t *drive)
1583 idetape_tape_t *tape = drive->driver_data;
1584 struct ide_atapi_pc pc;
1586 char fw_rev[4], vendor_id[8], product_id[16];
1588 idetape_create_inquiry_cmd(&pc);
1589 if (ide_queue_pc_tail(drive, tape->disk, &pc, pc_buf, pc.req_xfer)) {
1590 printk(KERN_ERR "ide-tape: %s: can't get INQUIRY results\n",
1594 memcpy(vendor_id, &pc_buf[8], 8);
1595 memcpy(product_id, &pc_buf[16], 16);
1596 memcpy(fw_rev, &pc_buf[32], 4);
1598 ide_fixstring(vendor_id, 8, 0);
1599 ide_fixstring(product_id, 16, 0);
1600 ide_fixstring(fw_rev, 4, 0);
1602 printk(KERN_INFO "ide-tape: %s <-> %s: %.8s %.16s rev %.4s\n",
1603 drive->name, tape->name, vendor_id, product_id, fw_rev);
1607 * Ask the tape about its various parameters. In particular, we will adjust our
1608 * data transfer buffer size to the recommended value as returned by the tape.
1610 static void idetape_get_mode_sense_results(ide_drive_t *drive)
1612 idetape_tape_t *tape = drive->driver_data;
1613 struct ide_atapi_pc pc;
1615 u8 speed, max_speed;
1617 idetape_create_mode_sense_cmd(&pc, IDETAPE_CAPABILITIES_PAGE);
1618 if (ide_queue_pc_tail(drive, tape->disk, &pc, buf, pc.req_xfer)) {
1619 printk(KERN_ERR "ide-tape: Can't get tape parameters - assuming"
1620 " some default values\n");
1621 tape->blk_size = 512;
1622 put_unaligned(52, (u16 *)&tape->caps[12]);
1623 put_unaligned(540, (u16 *)&tape->caps[14]);
1624 put_unaligned(6*52, (u16 *)&tape->caps[16]);
1627 caps = buf + 4 + buf[3];
1629 /* convert to host order and save for later use */
1630 speed = be16_to_cpup((__be16 *)&caps[14]);
1631 max_speed = be16_to_cpup((__be16 *)&caps[8]);
1633 *(u16 *)&caps[8] = max_speed;
1634 *(u16 *)&caps[12] = be16_to_cpup((__be16 *)&caps[12]);
1635 *(u16 *)&caps[14] = speed;
1636 *(u16 *)&caps[16] = be16_to_cpup((__be16 *)&caps[16]);
1639 printk(KERN_INFO "ide-tape: %s: invalid tape speed "
1640 "(assuming 650KB/sec)\n", drive->name);
1641 *(u16 *)&caps[14] = 650;
1644 printk(KERN_INFO "ide-tape: %s: invalid max_speed "
1645 "(assuming 650KB/sec)\n", drive->name);
1646 *(u16 *)&caps[8] = 650;
1649 memcpy(&tape->caps, caps, 20);
1651 /* device lacks locking support according to capabilities page */
1652 if ((caps[6] & 1) == 0)
1653 drive->dev_flags &= ~IDE_DFLAG_DOORLOCKING;
1656 tape->blk_size = 512;
1657 else if (caps[7] & 0x04)
1658 tape->blk_size = 1024;
1661 #ifdef CONFIG_IDE_PROC_FS
1662 #define ide_tape_devset_get(name, field) \
1663 static int get_##name(ide_drive_t *drive) \
1665 idetape_tape_t *tape = drive->driver_data; \
1666 return tape->field; \
1669 #define ide_tape_devset_set(name, field) \
1670 static int set_##name(ide_drive_t *drive, int arg) \
1672 idetape_tape_t *tape = drive->driver_data; \
1673 tape->field = arg; \
1677 #define ide_tape_devset_rw_field(_name, _field) \
1678 ide_tape_devset_get(_name, _field) \
1679 ide_tape_devset_set(_name, _field) \
1680 IDE_DEVSET(_name, DS_SYNC, get_##_name, set_##_name)
1682 #define ide_tape_devset_r_field(_name, _field) \
1683 ide_tape_devset_get(_name, _field) \
1684 IDE_DEVSET(_name, 0, get_##_name, NULL)
1686 static int mulf_tdsc(ide_drive_t *drive) { return 1000; }
1687 static int divf_tdsc(ide_drive_t *drive) { return HZ; }
1688 static int divf_buffer(ide_drive_t *drive) { return 2; }
1689 static int divf_buffer_size(ide_drive_t *drive) { return 1024; }
1691 ide_devset_rw_flag(dsc_overlap, IDE_DFLAG_DSC_OVERLAP);
1693 ide_tape_devset_rw_field(tdsc, best_dsc_rw_freq);
1695 ide_tape_devset_r_field(avg_speed, avg_speed);
1696 ide_tape_devset_r_field(speed, caps[14]);
1697 ide_tape_devset_r_field(buffer, caps[16]);
1698 ide_tape_devset_r_field(buffer_size, buffer_size);
1700 static const struct ide_proc_devset idetape_settings[] = {
1701 __IDE_PROC_DEVSET(avg_speed, 0, 0xffff, NULL, NULL),
1702 __IDE_PROC_DEVSET(buffer, 0, 0xffff, NULL, divf_buffer),
1703 __IDE_PROC_DEVSET(buffer_size, 0, 0xffff, NULL, divf_buffer_size),
1704 __IDE_PROC_DEVSET(dsc_overlap, 0, 1, NULL, NULL),
1705 __IDE_PROC_DEVSET(speed, 0, 0xffff, NULL, NULL),
1706 __IDE_PROC_DEVSET(tdsc, IDETAPE_DSC_RW_MIN, IDETAPE_DSC_RW_MAX,
1707 mulf_tdsc, divf_tdsc),
1713 * The function below is called to:
1715 * 1. Initialize our various state variables.
1716 * 2. Ask the tape for its capabilities.
1717 * 3. Allocate a buffer which will be used for data transfer. The buffer size
1718 * is chosen based on the recommendation which we received in step 2.
1720 * Note that at this point ide.c already assigned us an irq, so that we can
1721 * queue requests here and wait for their completion.
1723 static void idetape_setup(ide_drive_t *drive, idetape_tape_t *tape, int minor)
1728 u16 *ctl = (u16 *)&tape->caps[12];
1730 ide_debug_log(IDE_DBG_FUNC, "minor: %d", minor);
1732 drive->pc_callback = ide_tape_callback;
1734 drive->dev_flags |= IDE_DFLAG_DSC_OVERLAP;
1736 if (drive->hwif->host_flags & IDE_HFLAG_NO_DSC) {
1737 printk(KERN_INFO "ide-tape: %s: disabling DSC overlap\n",
1739 drive->dev_flags &= ~IDE_DFLAG_DSC_OVERLAP;
1742 /* Seagate Travan drives do not support DSC overlap. */
1743 if (strstr((char *)&drive->id[ATA_ID_PROD], "Seagate STT3401"))
1744 drive->dev_flags &= ~IDE_DFLAG_DSC_OVERLAP;
1746 tape->minor = minor;
1747 tape->name[0] = 'h';
1748 tape->name[1] = 't';
1749 tape->name[2] = '0' + minor;
1750 tape->chrdev_dir = IDETAPE_DIR_NONE;
1752 idetape_get_inquiry_results(drive);
1753 idetape_get_mode_sense_results(drive);
1754 ide_tape_get_bsize_from_bdesc(drive);
1755 tape->user_bs_factor = 1;
1756 tape->buffer_size = *ctl * tape->blk_size;
1757 while (tape->buffer_size > 0xffff) {
1758 printk(KERN_NOTICE "ide-tape: decreasing stage size\n");
1760 tape->buffer_size = *ctl * tape->blk_size;
1762 buffer_size = tape->buffer_size;
1764 /* select the "best" DSC read/write polling freq */
1765 speed = max(*(u16 *)&tape->caps[14], *(u16 *)&tape->caps[8]);
1767 t = (IDETAPE_FIFO_THRESHOLD * tape->buffer_size * HZ) / (speed * 1000);
1770 * Ensure that the number we got makes sense; limit it within
1771 * IDETAPE_DSC_RW_MIN and IDETAPE_DSC_RW_MAX.
1773 tape->best_dsc_rw_freq = clamp_t(unsigned long, t, IDETAPE_DSC_RW_MIN,
1774 IDETAPE_DSC_RW_MAX);
1775 printk(KERN_INFO "ide-tape: %s <-> %s: %dKBps, %d*%dkB buffer, "
1777 drive->name, tape->name, *(u16 *)&tape->caps[14],
1778 (*(u16 *)&tape->caps[16] * 512) / tape->buffer_size,
1779 tape->buffer_size / 1024,
1780 tape->best_dsc_rw_freq * 1000 / HZ,
1781 (drive->dev_flags & IDE_DFLAG_USING_DMA) ? ", DMA" : "");
1783 ide_proc_register_driver(drive, tape->driver);
1786 static void ide_tape_remove(ide_drive_t *drive)
1788 idetape_tape_t *tape = drive->driver_data;
1790 ide_proc_unregister_driver(drive, tape->driver);
1791 device_del(&tape->dev);
1792 ide_unregister_region(tape->disk);
1794 mutex_lock(&idetape_ref_mutex);
1795 put_device(&tape->dev);
1796 mutex_unlock(&idetape_ref_mutex);
1799 static void ide_tape_release(struct device *dev)
1801 struct ide_tape_obj *tape = to_ide_drv(dev, ide_tape_obj);
1802 ide_drive_t *drive = tape->drive;
1803 struct gendisk *g = tape->disk;
1805 BUG_ON(tape->valid);
1807 drive->dev_flags &= ~IDE_DFLAG_DSC_OVERLAP;
1808 drive->driver_data = NULL;
1809 device_destroy(idetape_sysfs_class, MKDEV(IDETAPE_MAJOR, tape->minor));
1810 device_destroy(idetape_sysfs_class,
1811 MKDEV(IDETAPE_MAJOR, tape->minor + 128));
1812 idetape_devs[tape->minor] = NULL;
1813 g->private_data = NULL;
1818 #ifdef CONFIG_IDE_PROC_FS
1819 static int proc_idetape_read_name
1820 (char *page, char **start, off_t off, int count, int *eof, void *data)
1822 ide_drive_t *drive = (ide_drive_t *) data;
1823 idetape_tape_t *tape = drive->driver_data;
1827 len = sprintf(out, "%s\n", tape->name);
1828 PROC_IDE_READ_RETURN(page, start, off, count, eof, len);
1831 static ide_proc_entry_t idetape_proc[] = {
1832 { "capacity", S_IFREG|S_IRUGO, proc_ide_read_capacity, NULL },
1833 { "name", S_IFREG|S_IRUGO, proc_idetape_read_name, NULL },
1834 { NULL, 0, NULL, NULL }
1837 static ide_proc_entry_t *ide_tape_proc_entries(ide_drive_t *drive)
1839 return idetape_proc;
1842 static const struct ide_proc_devset *ide_tape_proc_devsets(ide_drive_t *drive)
1844 return idetape_settings;
1848 static int ide_tape_probe(ide_drive_t *);
1850 static struct ide_driver idetape_driver = {
1852 .owner = THIS_MODULE,
1854 .bus = &ide_bus_type,
1856 .probe = ide_tape_probe,
1857 .remove = ide_tape_remove,
1858 .version = IDETAPE_VERSION,
1859 .do_request = idetape_do_request,
1860 #ifdef CONFIG_IDE_PROC_FS
1861 .proc_entries = ide_tape_proc_entries,
1862 .proc_devsets = ide_tape_proc_devsets,
1866 /* Our character device supporting functions, passed to register_chrdev. */
1867 static const struct file_operations idetape_fops = {
1868 .owner = THIS_MODULE,
1869 .read = idetape_chrdev_read,
1870 .write = idetape_chrdev_write,
1871 .ioctl = idetape_chrdev_ioctl,
1872 .open = idetape_chrdev_open,
1873 .release = idetape_chrdev_release,
1876 static int idetape_open(struct block_device *bdev, fmode_t mode)
1878 struct ide_tape_obj *tape = ide_tape_get(bdev->bd_disk, false, 0);
1886 static int idetape_release(struct gendisk *disk, fmode_t mode)
1888 struct ide_tape_obj *tape = ide_drv_g(disk, ide_tape_obj);
1894 static int idetape_ioctl(struct block_device *bdev, fmode_t mode,
1895 unsigned int cmd, unsigned long arg)
1897 struct ide_tape_obj *tape = ide_drv_g(bdev->bd_disk, ide_tape_obj);
1898 ide_drive_t *drive = tape->drive;
1899 int err = generic_ide_ioctl(drive, bdev, cmd, arg);
1901 err = idetape_blkdev_ioctl(drive, cmd, arg);
1905 static struct block_device_operations idetape_block_ops = {
1906 .owner = THIS_MODULE,
1907 .open = idetape_open,
1908 .release = idetape_release,
1909 .locked_ioctl = idetape_ioctl,
1912 static int ide_tape_probe(ide_drive_t *drive)
1914 idetape_tape_t *tape;
1918 ide_debug_log(IDE_DBG_FUNC, "enter");
1920 if (!strstr(DRV_NAME, drive->driver_req))
1923 if (drive->media != ide_tape)
1926 if ((drive->dev_flags & IDE_DFLAG_ID_READ) &&
1927 ide_check_atapi_device(drive, DRV_NAME) == 0) {
1928 printk(KERN_ERR "ide-tape: %s: not supported by this version of"
1929 " the driver\n", drive->name);
1932 tape = kzalloc(sizeof(idetape_tape_t), GFP_KERNEL);
1934 printk(KERN_ERR "ide-tape: %s: Can't allocate a tape struct\n",
1939 g = alloc_disk(1 << PARTN_BITS);
1943 ide_init_disk(g, drive);
1945 tape->dev.parent = &drive->gendev;
1946 tape->dev.release = ide_tape_release;
1947 dev_set_name(&tape->dev, dev_name(&drive->gendev));
1949 if (device_register(&tape->dev))
1952 tape->drive = drive;
1953 tape->driver = &idetape_driver;
1956 g->private_data = &tape->driver;
1958 drive->driver_data = tape;
1960 mutex_lock(&idetape_ref_mutex);
1961 for (minor = 0; idetape_devs[minor]; minor++)
1963 idetape_devs[minor] = tape;
1964 mutex_unlock(&idetape_ref_mutex);
1966 idetape_setup(drive, tape, minor);
1968 device_create(idetape_sysfs_class, &drive->gendev,
1969 MKDEV(IDETAPE_MAJOR, minor), NULL, "%s", tape->name);
1970 device_create(idetape_sysfs_class, &drive->gendev,
1971 MKDEV(IDETAPE_MAJOR, minor + 128), NULL,
1974 g->fops = &idetape_block_ops;
1975 ide_register_region(g);
1987 static void __exit idetape_exit(void)
1989 driver_unregister(&idetape_driver.gen_driver);
1990 class_destroy(idetape_sysfs_class);
1991 unregister_chrdev(IDETAPE_MAJOR, "ht");
1994 static int __init idetape_init(void)
1997 idetape_sysfs_class = class_create(THIS_MODULE, "ide_tape");
1998 if (IS_ERR(idetape_sysfs_class)) {
1999 idetape_sysfs_class = NULL;
2000 printk(KERN_ERR "Unable to create sysfs class for ide tapes\n");
2005 if (register_chrdev(IDETAPE_MAJOR, "ht", &idetape_fops)) {
2006 printk(KERN_ERR "ide-tape: Failed to register chrdev"
2009 goto out_free_class;
2012 error = driver_register(&idetape_driver.gen_driver);
2014 goto out_free_driver;
2019 driver_unregister(&idetape_driver.gen_driver);
2021 class_destroy(idetape_sysfs_class);
2026 MODULE_ALIAS("ide:*m-tape*");
2027 module_init(idetape_init);
2028 module_exit(idetape_exit);
2029 MODULE_ALIAS_CHARDEV_MAJOR(IDETAPE_MAJOR);
2030 MODULE_DESCRIPTION("ATAPI Streaming TAPE Driver");
2031 MODULE_LICENSE("GPL");