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),
134 /* Tape door status */
135 #define DOOR_UNLOCKED 0
136 #define DOOR_LOCKED 1
137 #define DOOR_EXPLICITLY_LOCKED 2
139 /* Some defines for the SPACE command */
140 #define IDETAPE_SPACE_OVER_FILEMARK 1
141 #define IDETAPE_SPACE_TO_EOD 3
143 /* Some defines for the LOAD UNLOAD command */
144 #define IDETAPE_LU_LOAD_MASK 1
145 #define IDETAPE_LU_RETENSION_MASK 2
146 #define IDETAPE_LU_EOT_MASK 4
148 /* Structures related to the SELECT SENSE / MODE SENSE packet commands. */
149 #define IDETAPE_BLOCK_DESCRIPTOR 0
150 #define IDETAPE_CAPABILITIES_PAGE 0x2a
153 * Most of our global data which we need to save even as we leave the driver due
154 * to an interrupt or a timer event is stored in the struct defined below.
156 typedef struct ide_tape_obj {
158 struct ide_driver *driver;
159 struct gendisk *disk;
162 /* used by REQ_IDETAPE_{READ,WRITE} requests */
163 struct ide_atapi_pc queued_pc;
166 * DSC polling variables.
168 * While polling for DSC we use postponed_rq to postpone the current
169 * request so that ide.c will be able to service pending requests on the
170 * other device. Note that at most we will have only one DSC (usually
171 * data transfer) request in the device request queue.
173 struct request *postponed_rq;
174 /* The time in which we started polling for DSC */
175 unsigned long dsc_polling_start;
176 /* Timer used to poll for dsc */
177 struct timer_list dsc_timer;
178 /* Read/Write dsc polling frequency */
179 unsigned long best_dsc_rw_freq;
180 unsigned long dsc_poll_freq;
181 unsigned long dsc_timeout;
183 /* Read position information */
186 unsigned int first_frame;
188 /* Last error information */
189 u8 sense_key, asc, ascq;
191 /* Character device operation */
195 /* Current character device data transfer direction */
198 /* tape block size, usually 512 or 1024 bytes */
199 unsigned short blk_size;
202 /* Copy of the tape's Capabilities and Mechanical Page */
206 * Active data transfer request parameters.
208 * At most, there is only one ide-tape originated data transfer request
209 * in the device request queue. This allows ide.c to easily service
210 * requests from the other device when we postpone our active request.
213 /* Data buffer size chosen based on the tape's recommendation */
215 /* Staging buffer of buffer_size bytes */
217 /* The read/write cursor */
219 /* The number of valid bytes in buf */
222 /* Measures average tape speed */
223 unsigned long avg_time;
227 /* the door is currently locked */
229 /* the tape hardware is write protected */
231 /* the tape is write protected (hardware or opened as read-only) */
237 static DEFINE_MUTEX(idetape_ref_mutex);
239 static struct class *idetape_sysfs_class;
241 static void ide_tape_release(struct device *);
243 static struct ide_tape_obj *ide_tape_get(struct gendisk *disk)
245 struct ide_tape_obj *tape = NULL;
247 mutex_lock(&idetape_ref_mutex);
248 tape = ide_drv_g(disk, ide_tape_obj);
250 if (ide_device_get(tape->drive))
253 get_device(&tape->dev);
255 mutex_unlock(&idetape_ref_mutex);
259 static void ide_tape_put(struct ide_tape_obj *tape)
261 ide_drive_t *drive = tape->drive;
263 mutex_lock(&idetape_ref_mutex);
264 put_device(&tape->dev);
265 ide_device_put(drive);
266 mutex_unlock(&idetape_ref_mutex);
270 * The variables below are used for the character device interface. Additional
271 * state variables are defined in our ide_drive_t structure.
273 static struct ide_tape_obj *idetape_devs[MAX_HWIFS * MAX_DRIVES];
275 static struct ide_tape_obj *ide_tape_chrdev_get(unsigned int i)
277 struct ide_tape_obj *tape = NULL;
279 mutex_lock(&idetape_ref_mutex);
280 tape = idetape_devs[i];
282 get_device(&tape->dev);
283 mutex_unlock(&idetape_ref_mutex);
288 * called on each failed packet command retry to analyze the request sense. We
289 * currently do not utilize this information.
291 static void idetape_analyze_error(ide_drive_t *drive, u8 *sense)
293 idetape_tape_t *tape = drive->driver_data;
294 struct ide_atapi_pc *pc = drive->failed_pc;
296 tape->sense_key = sense[2] & 0xF;
297 tape->asc = sense[12];
298 tape->ascq = sense[13];
300 debug_log(DBG_ERR, "pc = %x, sense key = %x, asc = %x, ascq = %x\n",
301 pc->c[0], tape->sense_key, tape->asc, tape->ascq);
303 /* Correct pc->xferred by asking the tape. */
304 if (pc->flags & PC_FLAG_DMA_ERROR)
305 pc->xferred = pc->req_xfer -
307 get_unaligned_be32(&sense[3]);
310 * If error was the result of a zero-length read or write command,
311 * with sense key=5, asc=0x22, ascq=0, let it slide. Some drives
312 * (i.e. Seagate STT3401A Travan) don't support 0-length read/writes.
314 if ((pc->c[0] == READ_6 || pc->c[0] == WRITE_6)
316 && pc->c[4] == 0 && pc->c[3] == 0 && pc->c[2] == 0) {
317 if (tape->sense_key == 5) {
318 /* don't report an error, everything's ok */
320 /* don't retry read/write */
321 pc->flags |= PC_FLAG_ABORT;
324 if (pc->c[0] == READ_6 && (sense[2] & 0x80)) {
325 pc->error = IDE_DRV_ERROR_FILEMARK;
326 pc->flags |= PC_FLAG_ABORT;
328 if (pc->c[0] == WRITE_6) {
329 if ((sense[2] & 0x40) || (tape->sense_key == 0xd
330 && tape->asc == 0x0 && tape->ascq == 0x2)) {
331 pc->error = IDE_DRV_ERROR_EOD;
332 pc->flags |= PC_FLAG_ABORT;
335 if (pc->c[0] == READ_6 || pc->c[0] == WRITE_6) {
336 if (tape->sense_key == 8) {
337 pc->error = IDE_DRV_ERROR_EOD;
338 pc->flags |= PC_FLAG_ABORT;
340 if (!(pc->flags & PC_FLAG_ABORT) &&
342 pc->retries = IDETAPE_MAX_PC_RETRIES + 1;
346 static void ide_tape_handle_dsc(ide_drive_t *);
348 static int ide_tape_callback(ide_drive_t *drive, int dsc)
350 idetape_tape_t *tape = drive->driver_data;
351 struct ide_atapi_pc *pc = drive->pc;
352 struct request *rq = drive->hwif->rq;
353 int uptodate = pc->error ? 0 : 1;
354 int err = uptodate ? 0 : IDE_DRV_ERROR_GENERAL;
356 debug_log(DBG_PROCS, "Enter %s\n", __func__);
359 ide_tape_handle_dsc(drive);
361 if (drive->failed_pc == pc)
362 drive->failed_pc = NULL;
364 if (pc->c[0] == REQUEST_SENSE) {
366 idetape_analyze_error(drive, pc->buf);
368 printk(KERN_ERR "ide-tape: Error in REQUEST SENSE "
369 "itself - Aborting request!\n");
370 } else if (pc->c[0] == READ_6 || pc->c[0] == WRITE_6) {
371 int blocks = pc->xferred / tape->blk_size;
373 tape->avg_size += blocks * tape->blk_size;
375 if (time_after_eq(jiffies, tape->avg_time + HZ)) {
376 tape->avg_speed = tape->avg_size * HZ /
377 (jiffies - tape->avg_time) / 1024;
379 tape->avg_time = jiffies;
382 tape->first_frame += blocks;
383 rq->data_len -= blocks * tape->blk_size;
389 } else if (pc->c[0] == READ_POSITION && uptodate) {
390 u8 *readpos = pc->buf;
392 debug_log(DBG_SENSE, "BOP - %s\n",
393 (readpos[0] & 0x80) ? "Yes" : "No");
394 debug_log(DBG_SENSE, "EOP - %s\n",
395 (readpos[0] & 0x40) ? "Yes" : "No");
397 if (readpos[0] & 0x4) {
398 printk(KERN_INFO "ide-tape: Block location is unknown"
400 clear_bit(ilog2(IDE_AFLAG_ADDRESS_VALID),
401 &drive->atapi_flags);
403 err = IDE_DRV_ERROR_GENERAL;
405 debug_log(DBG_SENSE, "Block Location - %u\n",
406 be32_to_cpup((__be32 *)&readpos[4]));
408 tape->partition = readpos[1];
409 tape->first_frame = be32_to_cpup((__be32 *)&readpos[4]);
410 set_bit(ilog2(IDE_AFLAG_ADDRESS_VALID),
411 &drive->atapi_flags);
421 * Postpone the current request so that ide.c will be able to service requests
422 * from another device on the same port while we are polling for DSC.
424 static void idetape_postpone_request(ide_drive_t *drive)
426 idetape_tape_t *tape = drive->driver_data;
428 debug_log(DBG_PROCS, "Enter %s\n", __func__);
430 tape->postponed_rq = drive->hwif->rq;
432 ide_stall_queue(drive, tape->dsc_poll_freq);
435 static void ide_tape_handle_dsc(ide_drive_t *drive)
437 idetape_tape_t *tape = drive->driver_data;
439 /* Media access command */
440 tape->dsc_polling_start = jiffies;
441 tape->dsc_poll_freq = IDETAPE_DSC_MA_FAST;
442 tape->dsc_timeout = jiffies + IDETAPE_DSC_MA_TIMEOUT;
443 /* Allow ide.c to handle other requests */
444 idetape_postpone_request(drive);
448 * Packet Command Interface
450 * The current Packet Command is available in drive->pc, and will not change
451 * until we finish handling it. Each packet command is associated with a
452 * callback function that will be called when the command is finished.
454 * The handling will be done in three stages:
456 * 1. ide_tape_issue_pc will send the packet command to the drive, and will set
457 * the interrupt handler to ide_pc_intr.
459 * 2. On each interrupt, ide_pc_intr will be called. This step will be
460 * repeated until the device signals us that no more interrupts will be issued.
462 * 3. ATAPI Tape media access commands have immediate status with a delayed
463 * process. In case of a successful initiation of a media access packet command,
464 * the DSC bit will be set when the actual execution of the command is finished.
465 * Since the tape drive will not issue an interrupt, we have to poll for this
466 * event. In this case, we define the request as "low priority request" by
467 * setting rq_status to IDETAPE_RQ_POSTPONED, set a timer to poll for DSC and
470 * ide.c will then give higher priority to requests which originate from the
471 * other device, until will change rq_status to RQ_ACTIVE.
473 * 4. When the packet command is finished, it will be checked for errors.
475 * 5. In case an error was found, we queue a request sense packet command in
476 * front of the request queue and retry the operation up to
477 * IDETAPE_MAX_PC_RETRIES times.
479 * 6. In case no error was found, or we decided to give up and not to retry
480 * again, the callback function will be called and then we will handle the next
484 static ide_startstop_t ide_tape_issue_pc(ide_drive_t *drive,
486 struct ide_atapi_pc *pc)
488 idetape_tape_t *tape = drive->driver_data;
490 if (drive->failed_pc == NULL && pc->c[0] != REQUEST_SENSE)
491 drive->failed_pc = pc;
493 /* Set the current packet command */
496 if (pc->retries > IDETAPE_MAX_PC_RETRIES ||
497 (pc->flags & PC_FLAG_ABORT)) {
498 unsigned int done = blk_rq_bytes(drive->hwif->rq);
501 * We will "abort" retrying a packet command in case legitimate
502 * error code was received (crossing a filemark, or end of the
503 * media, for example).
505 if (!(pc->flags & PC_FLAG_ABORT)) {
506 if (!(pc->c[0] == TEST_UNIT_READY &&
507 tape->sense_key == 2 && tape->asc == 4 &&
508 (tape->ascq == 1 || tape->ascq == 8))) {
509 printk(KERN_ERR "ide-tape: %s: I/O error, "
510 "pc = %2x, key = %2x, "
511 "asc = %2x, ascq = %2x\n",
512 tape->name, pc->c[0],
513 tape->sense_key, tape->asc,
517 pc->error = IDE_DRV_ERROR_GENERAL;
520 drive->failed_pc = NULL;
521 drive->pc_callback(drive, 0);
522 ide_complete_rq(drive, -EIO, done);
525 debug_log(DBG_SENSE, "Retry #%d, cmd = %02X\n", pc->retries, pc->c[0]);
529 return ide_issue_pc(drive, cmd);
532 /* A mode sense command is used to "sense" tape parameters. */
533 static void idetape_create_mode_sense_cmd(struct ide_atapi_pc *pc, u8 page_code)
536 pc->c[0] = MODE_SENSE;
537 if (page_code != IDETAPE_BLOCK_DESCRIPTOR)
538 /* DBD = 1 - Don't return block descriptors */
540 pc->c[2] = page_code;
542 * Changed pc->c[3] to 0 (255 will at best return unused info).
544 * For SCSI this byte is defined as subpage instead of high byte
545 * of length and some IDE drives seem to interpret it this way
546 * and return an error when 255 is used.
549 /* We will just discard data in that case */
551 if (page_code == IDETAPE_BLOCK_DESCRIPTOR)
553 else if (page_code == IDETAPE_CAPABILITIES_PAGE)
559 static ide_startstop_t idetape_media_access_finished(ide_drive_t *drive)
561 ide_hwif_t *hwif = drive->hwif;
562 idetape_tape_t *tape = drive->driver_data;
563 struct ide_atapi_pc *pc = drive->pc;
566 stat = hwif->tp_ops->read_status(hwif);
568 if (stat & ATA_DSC) {
569 if (stat & ATA_ERR) {
571 if (pc->c[0] != TEST_UNIT_READY)
572 printk(KERN_ERR "ide-tape: %s: I/O error, ",
574 /* Retry operation */
580 pc->error = IDE_DRV_ERROR_GENERAL;
581 drive->failed_pc = NULL;
583 drive->pc_callback(drive, 0);
587 static void ide_tape_create_rw_cmd(idetape_tape_t *tape,
588 struct ide_atapi_pc *pc, struct request *rq,
591 unsigned int length = rq->nr_sectors;
594 put_unaligned(cpu_to_be32(length), (unsigned int *) &pc->c[1]);
597 pc->buf_size = length * tape->blk_size;
598 pc->req_xfer = pc->buf_size;
599 if (pc->req_xfer == tape->buffer_size)
600 pc->flags |= PC_FLAG_DMA_OK;
602 if (opcode == READ_6)
604 else if (opcode == WRITE_6) {
606 pc->flags |= PC_FLAG_WRITING;
609 memcpy(rq->cmd, pc->c, 12);
612 static ide_startstop_t idetape_do_request(ide_drive_t *drive,
613 struct request *rq, sector_t block)
615 ide_hwif_t *hwif = drive->hwif;
616 idetape_tape_t *tape = drive->driver_data;
617 struct ide_atapi_pc *pc = NULL;
618 struct request *postponed_rq = tape->postponed_rq;
622 debug_log(DBG_SENSE, "sector: %llu, nr_sectors: %lu\n",
623 (unsigned long long)rq->sector, rq->nr_sectors);
625 if (!(blk_special_request(rq) || blk_sense_request(rq))) {
626 /* We do not support buffer cache originated requests. */
627 printk(KERN_NOTICE "ide-tape: %s: Unsupported request in "
628 "request queue (%d)\n", drive->name, rq->cmd_type);
629 if (blk_fs_request(rq) == 0 && rq->errors == 0)
631 ide_complete_rq(drive, -EIO, ide_rq_bytes(rq));
635 /* Retry a failed packet command */
636 if (drive->failed_pc && drive->pc->c[0] == REQUEST_SENSE) {
637 pc = drive->failed_pc;
641 if (postponed_rq != NULL)
642 if (rq != postponed_rq) {
643 printk(KERN_ERR "ide-tape: ide-tape.c bug - "
644 "Two DSC requests were queued\n");
645 drive->failed_pc = NULL;
647 ide_complete_rq(drive, 0, blk_rq_bytes(rq));
651 tape->postponed_rq = NULL;
654 * If the tape is still busy, postpone our request and service
655 * the other device meanwhile.
657 stat = hwif->tp_ops->read_status(hwif);
659 if ((drive->dev_flags & IDE_DFLAG_DSC_OVERLAP) == 0 &&
660 (rq->cmd[13] & REQ_IDETAPE_PC2) == 0)
661 drive->atapi_flags |= IDE_AFLAG_IGNORE_DSC;
663 if (drive->dev_flags & IDE_DFLAG_POST_RESET) {
664 drive->atapi_flags |= IDE_AFLAG_IGNORE_DSC;
665 drive->dev_flags &= ~IDE_DFLAG_POST_RESET;
668 if (!(drive->atapi_flags & IDE_AFLAG_IGNORE_DSC) &&
670 if (postponed_rq == NULL) {
671 tape->dsc_polling_start = jiffies;
672 tape->dsc_poll_freq = tape->best_dsc_rw_freq;
673 tape->dsc_timeout = jiffies + IDETAPE_DSC_RW_TIMEOUT;
674 } else if (time_after(jiffies, tape->dsc_timeout)) {
675 printk(KERN_ERR "ide-tape: %s: DSC timeout\n",
677 if (rq->cmd[13] & REQ_IDETAPE_PC2) {
678 idetape_media_access_finished(drive);
681 return ide_do_reset(drive);
683 } else if (time_after(jiffies,
684 tape->dsc_polling_start +
685 IDETAPE_DSC_MA_THRESHOLD))
686 tape->dsc_poll_freq = IDETAPE_DSC_MA_SLOW;
687 idetape_postpone_request(drive);
690 drive->atapi_flags &= ~IDE_AFLAG_IGNORE_DSC;
692 if (rq->cmd[13] & REQ_IDETAPE_READ) {
693 pc = &tape->queued_pc;
694 ide_tape_create_rw_cmd(tape, pc, rq, READ_6);
697 if (rq->cmd[13] & REQ_IDETAPE_WRITE) {
698 pc = &tape->queued_pc;
699 ide_tape_create_rw_cmd(tape, pc, rq, WRITE_6);
702 if (rq->cmd[13] & REQ_IDETAPE_PC1) {
703 pc = (struct ide_atapi_pc *)rq->special;
704 rq->cmd[13] &= ~(REQ_IDETAPE_PC1);
705 rq->cmd[13] |= REQ_IDETAPE_PC2;
708 if (rq->cmd[13] & REQ_IDETAPE_PC2) {
709 idetape_media_access_finished(drive);
715 /* prepare sense request for this command */
716 ide_prep_sense(drive, rq);
718 memset(&cmd, 0, sizeof(cmd));
721 cmd.tf_flags |= IDE_TFLAG_WRITE;
725 ide_init_sg_cmd(&cmd, pc->req_xfer);
726 ide_map_sg(drive, &cmd);
728 return ide_tape_issue_pc(drive, &cmd, pc);
732 * Write a filemark if write_filemark=1. Flush the device buffers without
733 * writing a filemark otherwise.
735 static void idetape_create_write_filemark_cmd(ide_drive_t *drive,
736 struct ide_atapi_pc *pc, int write_filemark)
739 pc->c[0] = WRITE_FILEMARKS;
740 pc->c[4] = write_filemark;
741 pc->flags |= PC_FLAG_WAIT_FOR_DSC;
744 static int idetape_wait_ready(ide_drive_t *drive, unsigned long timeout)
746 idetape_tape_t *tape = drive->driver_data;
747 struct gendisk *disk = tape->disk;
748 int load_attempted = 0;
750 /* Wait for the tape to become ready */
751 set_bit(ilog2(IDE_AFLAG_MEDIUM_PRESENT), &drive->atapi_flags);
753 while (time_before(jiffies, timeout)) {
754 if (ide_do_test_unit_ready(drive, disk) == 0)
756 if ((tape->sense_key == 2 && tape->asc == 4 && tape->ascq == 2)
757 || (tape->asc == 0x3A)) {
761 ide_do_start_stop(drive, disk, IDETAPE_LU_LOAD_MASK);
763 /* not about to be ready */
764 } else if (!(tape->sense_key == 2 && tape->asc == 4 &&
765 (tape->ascq == 1 || tape->ascq == 8)))
772 static int idetape_flush_tape_buffers(ide_drive_t *drive)
774 struct ide_tape_obj *tape = drive->driver_data;
775 struct ide_atapi_pc pc;
778 idetape_create_write_filemark_cmd(drive, &pc, 0);
779 rc = ide_queue_pc_tail(drive, tape->disk, &pc);
782 idetape_wait_ready(drive, 60 * 5 * HZ);
786 static void idetape_create_read_position_cmd(struct ide_atapi_pc *pc)
789 pc->c[0] = READ_POSITION;
793 static int idetape_read_position(ide_drive_t *drive)
795 idetape_tape_t *tape = drive->driver_data;
796 struct ide_atapi_pc pc;
799 debug_log(DBG_PROCS, "Enter %s\n", __func__);
801 idetape_create_read_position_cmd(&pc);
802 if (ide_queue_pc_tail(drive, tape->disk, &pc))
804 position = tape->first_frame;
808 static void idetape_create_locate_cmd(ide_drive_t *drive,
809 struct ide_atapi_pc *pc,
810 unsigned int block, u8 partition, int skip)
813 pc->c[0] = POSITION_TO_ELEMENT;
815 put_unaligned(cpu_to_be32(block), (unsigned int *) &pc->c[3]);
816 pc->c[8] = partition;
817 pc->flags |= PC_FLAG_WAIT_FOR_DSC;
820 static void __ide_tape_discard_merge_buffer(ide_drive_t *drive)
822 idetape_tape_t *tape = drive->driver_data;
824 if (tape->chrdev_dir != IDETAPE_DIR_READ)
827 clear_bit(ilog2(IDE_AFLAG_FILEMARK), &drive->atapi_flags);
829 if (tape->buf != NULL) {
834 tape->chrdev_dir = IDETAPE_DIR_NONE;
838 * Position the tape to the requested block using the LOCATE packet command.
839 * A READ POSITION command is then issued to check where we are positioned. Like
840 * all higher level operations, we queue the commands at the tail of the request
841 * queue and wait for their completion.
843 static int idetape_position_tape(ide_drive_t *drive, unsigned int block,
844 u8 partition, int skip)
846 idetape_tape_t *tape = drive->driver_data;
847 struct gendisk *disk = tape->disk;
849 struct ide_atapi_pc pc;
851 if (tape->chrdev_dir == IDETAPE_DIR_READ)
852 __ide_tape_discard_merge_buffer(drive);
853 idetape_wait_ready(drive, 60 * 5 * HZ);
854 idetape_create_locate_cmd(drive, &pc, block, partition, skip);
855 retval = ide_queue_pc_tail(drive, disk, &pc);
859 idetape_create_read_position_cmd(&pc);
860 return ide_queue_pc_tail(drive, disk, &pc);
863 static void ide_tape_discard_merge_buffer(ide_drive_t *drive,
864 int restore_position)
866 idetape_tape_t *tape = drive->driver_data;
869 __ide_tape_discard_merge_buffer(drive);
870 if (restore_position) {
871 position = idetape_read_position(drive);
872 seek = position > 0 ? position : 0;
873 if (idetape_position_tape(drive, seek, 0, 0)) {
874 printk(KERN_INFO "ide-tape: %s: position_tape failed in"
875 " %s\n", tape->name, __func__);
882 * Generate a read/write request for the block device interface and wait for it
885 static int idetape_queue_rw_tail(ide_drive_t *drive, int cmd, int size)
887 idetape_tape_t *tape = drive->driver_data;
891 debug_log(DBG_SENSE, "%s: cmd=%d\n", __func__, cmd);
892 BUG_ON(cmd != REQ_IDETAPE_READ && cmd != REQ_IDETAPE_WRITE);
893 BUG_ON(size < 0 || size % tape->blk_size);
895 rq = blk_get_request(drive->queue, READ, __GFP_WAIT);
896 rq->cmd_type = REQ_TYPE_SPECIAL;
898 rq->rq_disk = tape->disk;
899 rq->sector = tape->first_frame;
902 ret = blk_rq_map_kern(drive->queue, rq, tape->buf, size,
908 blk_execute_rq(drive->queue, tape->disk, rq, 0);
910 /* calculate the number of transferred bytes and update buffer state */
911 size -= rq->data_len;
912 tape->cur = tape->buf;
913 if (cmd == REQ_IDETAPE_READ)
919 if (rq->errors == IDE_DRV_ERROR_GENERAL)
926 static void idetape_create_inquiry_cmd(struct ide_atapi_pc *pc)
934 static void idetape_create_rewind_cmd(ide_drive_t *drive,
935 struct ide_atapi_pc *pc)
938 pc->c[0] = REZERO_UNIT;
939 pc->flags |= PC_FLAG_WAIT_FOR_DSC;
942 static void idetape_create_erase_cmd(struct ide_atapi_pc *pc)
947 pc->flags |= PC_FLAG_WAIT_FOR_DSC;
950 static void idetape_create_space_cmd(struct ide_atapi_pc *pc, int count, u8 cmd)
954 put_unaligned(cpu_to_be32(count), (unsigned int *) &pc->c[1]);
956 pc->flags |= PC_FLAG_WAIT_FOR_DSC;
959 static void ide_tape_flush_merge_buffer(ide_drive_t *drive)
961 idetape_tape_t *tape = drive->driver_data;
963 if (tape->chrdev_dir != IDETAPE_DIR_WRITE) {
964 printk(KERN_ERR "ide-tape: bug: Trying to empty merge buffer"
965 " but we are not writing.\n");
969 size_t aligned = roundup(tape->valid, tape->blk_size);
971 memset(tape->cur, 0, aligned - tape->valid);
972 idetape_queue_rw_tail(drive, REQ_IDETAPE_WRITE, aligned);
976 tape->chrdev_dir = IDETAPE_DIR_NONE;
979 static int idetape_init_rw(ide_drive_t *drive, int dir)
981 idetape_tape_t *tape = drive->driver_data;
984 BUG_ON(dir != IDETAPE_DIR_READ && dir != IDETAPE_DIR_WRITE);
986 if (tape->chrdev_dir == dir)
989 if (tape->chrdev_dir == IDETAPE_DIR_READ)
990 ide_tape_discard_merge_buffer(drive, 1);
991 else if (tape->chrdev_dir == IDETAPE_DIR_WRITE) {
992 ide_tape_flush_merge_buffer(drive);
993 idetape_flush_tape_buffers(drive);
996 if (tape->buf || tape->valid) {
997 printk(KERN_ERR "ide-tape: valid should be 0 now\n");
1001 tape->buf = kmalloc(tape->buffer_size, GFP_KERNEL);
1004 tape->chrdev_dir = dir;
1005 tape->cur = tape->buf;
1008 * Issue a 0 rw command to ensure that DSC handshake is
1009 * switched from completion mode to buffer available mode. No
1010 * point in issuing this if DSC overlap isn't supported, some
1011 * drives (Seagate STT3401A) will return an error.
1013 if (drive->dev_flags & IDE_DFLAG_DSC_OVERLAP) {
1014 int cmd = dir == IDETAPE_DIR_READ ? REQ_IDETAPE_READ
1015 : REQ_IDETAPE_WRITE;
1017 rc = idetape_queue_rw_tail(drive, cmd, 0);
1021 tape->chrdev_dir = IDETAPE_DIR_NONE;
1029 static void idetape_pad_zeros(ide_drive_t *drive, int bcount)
1031 idetape_tape_t *tape = drive->driver_data;
1033 memset(tape->buf, 0, tape->buffer_size);
1036 unsigned int count = min(tape->buffer_size, bcount);
1038 idetape_queue_rw_tail(drive, REQ_IDETAPE_WRITE, count);
1044 * Rewinds the tape to the Beginning Of the current Partition (BOP). We
1045 * currently support only one partition.
1047 static int idetape_rewind_tape(ide_drive_t *drive)
1049 struct ide_tape_obj *tape = drive->driver_data;
1050 struct gendisk *disk = tape->disk;
1052 struct ide_atapi_pc pc;
1054 debug_log(DBG_SENSE, "Enter %s\n", __func__);
1056 idetape_create_rewind_cmd(drive, &pc);
1057 retval = ide_queue_pc_tail(drive, disk, &pc);
1061 idetape_create_read_position_cmd(&pc);
1062 retval = ide_queue_pc_tail(drive, disk, &pc);
1068 /* mtio.h compatible commands should be issued to the chrdev interface. */
1069 static int idetape_blkdev_ioctl(ide_drive_t *drive, unsigned int cmd,
1072 idetape_tape_t *tape = drive->driver_data;
1073 void __user *argp = (void __user *)arg;
1075 struct idetape_config {
1076 int dsc_rw_frequency;
1077 int dsc_media_access_frequency;
1081 debug_log(DBG_PROCS, "Enter %s\n", __func__);
1085 if (copy_from_user(&config, argp, sizeof(config)))
1087 tape->best_dsc_rw_freq = config.dsc_rw_frequency;
1090 config.dsc_rw_frequency = (int) tape->best_dsc_rw_freq;
1091 config.nr_stages = 1;
1092 if (copy_to_user(argp, &config, sizeof(config)))
1101 static int idetape_space_over_filemarks(ide_drive_t *drive, short mt_op,
1104 idetape_tape_t *tape = drive->driver_data;
1105 struct gendisk *disk = tape->disk;
1106 struct ide_atapi_pc pc;
1107 int retval, count = 0;
1108 int sprev = !!(tape->caps[4] & 0x20);
1112 if (MTBSF == mt_op || MTBSFM == mt_op) {
1115 mt_count = -mt_count;
1118 if (tape->chrdev_dir == IDETAPE_DIR_READ) {
1120 if (test_and_clear_bit(ilog2(IDE_AFLAG_FILEMARK),
1121 &drive->atapi_flags))
1123 ide_tape_discard_merge_buffer(drive, 0);
1129 idetape_create_space_cmd(&pc, mt_count - count,
1130 IDETAPE_SPACE_OVER_FILEMARK);
1131 return ide_queue_pc_tail(drive, disk, &pc);
1136 retval = idetape_space_over_filemarks(drive, MTFSF,
1140 count = (MTBSFM == mt_op ? 1 : -1);
1141 return idetape_space_over_filemarks(drive, MTFSF, count);
1143 printk(KERN_ERR "ide-tape: MTIO operation %d not supported\n",
1150 * Our character device read / write functions.
1152 * The tape is optimized to maximize throughput when it is transferring an
1153 * integral number of the "continuous transfer limit", which is a parameter of
1154 * the specific tape (26kB on my particular tape, 32kB for Onstream).
1156 * As of version 1.3 of the driver, the character device provides an abstract
1157 * continuous view of the media - any mix of block sizes (even 1 byte) on the
1158 * same backup/restore procedure is supported. The driver will internally
1159 * convert the requests to the recommended transfer unit, so that an unmatch
1160 * between the user's block size to the recommended size will only result in a
1161 * (slightly) increased driver overhead, but will no longer hit performance.
1162 * This is not applicable to Onstream.
1164 static ssize_t idetape_chrdev_read(struct file *file, char __user *buf,
1165 size_t count, loff_t *ppos)
1167 struct ide_tape_obj *tape = file->private_data;
1168 ide_drive_t *drive = tape->drive;
1173 debug_log(DBG_CHRDEV, "Enter %s, count %Zd\n", __func__, count);
1175 if (tape->chrdev_dir != IDETAPE_DIR_READ) {
1176 if (test_bit(ilog2(IDE_AFLAG_DETECT_BS), &drive->atapi_flags))
1177 if (count > tape->blk_size &&
1178 (count % tape->blk_size) == 0)
1179 tape->user_bs_factor = count / tape->blk_size;
1182 rc = idetape_init_rw(drive, IDETAPE_DIR_READ);
1186 while (done < count) {
1189 /* refill if staging buffer is empty */
1191 /* If we are at a filemark, nothing more to read */
1192 if (test_bit(ilog2(IDE_AFLAG_FILEMARK),
1193 &drive->atapi_flags))
1196 if (idetape_queue_rw_tail(drive, REQ_IDETAPE_READ,
1197 tape->buffer_size) <= 0)
1202 todo = min_t(size_t, count - done, tape->valid);
1203 if (copy_to_user(buf + done, tape->cur, todo))
1207 tape->valid -= todo;
1211 if (!done && test_bit(ilog2(IDE_AFLAG_FILEMARK), &drive->atapi_flags)) {
1212 debug_log(DBG_SENSE, "%s: spacing over filemark\n", tape->name);
1214 idetape_space_over_filemarks(drive, MTFSF, 1);
1218 return ret ? ret : done;
1221 static ssize_t idetape_chrdev_write(struct file *file, const char __user *buf,
1222 size_t count, loff_t *ppos)
1224 struct ide_tape_obj *tape = file->private_data;
1225 ide_drive_t *drive = tape->drive;
1230 /* The drive is write protected. */
1231 if (tape->write_prot)
1234 debug_log(DBG_CHRDEV, "Enter %s, count %Zd\n", __func__, count);
1236 /* Initialize write operation */
1237 rc = idetape_init_rw(drive, IDETAPE_DIR_WRITE);
1241 while (done < count) {
1244 /* flush if staging buffer is full */
1245 if (tape->valid == tape->buffer_size &&
1246 idetape_queue_rw_tail(drive, REQ_IDETAPE_WRITE,
1247 tape->buffer_size) <= 0)
1251 todo = min_t(size_t, count - done,
1252 tape->buffer_size - tape->valid);
1253 if (copy_from_user(tape->cur, buf + done, todo))
1257 tape->valid += todo;
1261 return ret ? ret : done;
1264 static int idetape_write_filemark(ide_drive_t *drive)
1266 struct ide_tape_obj *tape = drive->driver_data;
1267 struct ide_atapi_pc pc;
1269 /* Write a filemark */
1270 idetape_create_write_filemark_cmd(drive, &pc, 1);
1271 if (ide_queue_pc_tail(drive, tape->disk, &pc)) {
1272 printk(KERN_ERR "ide-tape: Couldn't write a filemark\n");
1279 * Called from idetape_chrdev_ioctl when the general mtio MTIOCTOP ioctl is
1282 * Note: MTBSF and MTBSFM are not supported when the tape doesn't support
1283 * spacing over filemarks in the reverse direction. In this case, MTFSFM is also
1284 * usually not supported.
1286 * The following commands are currently not supported:
1288 * MTFSS, MTBSS, MTWSM, MTSETDENSITY, MTSETDRVBUFFER, MT_ST_BOOLEANS,
1289 * MT_ST_WRITE_THRESHOLD.
1291 static int idetape_mtioctop(ide_drive_t *drive, short mt_op, int mt_count)
1293 idetape_tape_t *tape = drive->driver_data;
1294 struct gendisk *disk = tape->disk;
1295 struct ide_atapi_pc pc;
1298 debug_log(DBG_ERR, "Handling MTIOCTOP ioctl: mt_op=%d, mt_count=%d\n",
1308 return idetape_space_over_filemarks(drive, mt_op, mt_count);
1315 if (tape->write_prot)
1317 ide_tape_discard_merge_buffer(drive, 1);
1318 for (i = 0; i < mt_count; i++) {
1319 retval = idetape_write_filemark(drive);
1325 ide_tape_discard_merge_buffer(drive, 0);
1326 if (idetape_rewind_tape(drive))
1330 ide_tape_discard_merge_buffer(drive, 0);
1331 return ide_do_start_stop(drive, disk, IDETAPE_LU_LOAD_MASK);
1335 * If door is locked, attempt to unlock before
1336 * attempting to eject.
1338 if (tape->door_locked) {
1339 if (!ide_set_media_lock(drive, disk, 0))
1340 tape->door_locked = DOOR_UNLOCKED;
1342 ide_tape_discard_merge_buffer(drive, 0);
1343 retval = ide_do_start_stop(drive, disk, !IDETAPE_LU_LOAD_MASK);
1345 clear_bit(ilog2(IDE_AFLAG_MEDIUM_PRESENT),
1346 &drive->atapi_flags);
1349 ide_tape_discard_merge_buffer(drive, 0);
1350 return idetape_flush_tape_buffers(drive);
1352 ide_tape_discard_merge_buffer(drive, 0);
1353 return ide_do_start_stop(drive, disk,
1354 IDETAPE_LU_RETENSION_MASK | IDETAPE_LU_LOAD_MASK);
1356 idetape_create_space_cmd(&pc, 0, IDETAPE_SPACE_TO_EOD);
1357 return ide_queue_pc_tail(drive, disk, &pc);
1359 (void)idetape_rewind_tape(drive);
1360 idetape_create_erase_cmd(&pc);
1361 return ide_queue_pc_tail(drive, disk, &pc);
1364 if (mt_count < tape->blk_size ||
1365 mt_count % tape->blk_size)
1367 tape->user_bs_factor = mt_count / tape->blk_size;
1368 clear_bit(ilog2(IDE_AFLAG_DETECT_BS),
1369 &drive->atapi_flags);
1371 set_bit(ilog2(IDE_AFLAG_DETECT_BS),
1372 &drive->atapi_flags);
1375 ide_tape_discard_merge_buffer(drive, 0);
1376 return idetape_position_tape(drive,
1377 mt_count * tape->user_bs_factor, tape->partition, 0);
1379 ide_tape_discard_merge_buffer(drive, 0);
1380 return idetape_position_tape(drive, 0, mt_count, 0);
1384 retval = ide_set_media_lock(drive, disk, 1);
1387 tape->door_locked = DOOR_EXPLICITLY_LOCKED;
1390 retval = ide_set_media_lock(drive, disk, 0);
1393 tape->door_locked = DOOR_UNLOCKED;
1396 printk(KERN_ERR "ide-tape: MTIO operation %d not supported\n",
1403 * Our character device ioctls. General mtio.h magnetic io commands are
1404 * supported here, and not in the corresponding block interface. Our own
1405 * ide-tape ioctls are supported on both interfaces.
1407 static int idetape_chrdev_ioctl(struct inode *inode, struct file *file,
1408 unsigned int cmd, unsigned long arg)
1410 struct ide_tape_obj *tape = file->private_data;
1411 ide_drive_t *drive = tape->drive;
1415 int block_offset = 0, position = tape->first_frame;
1416 void __user *argp = (void __user *)arg;
1418 debug_log(DBG_CHRDEV, "Enter %s, cmd=%u\n", __func__, cmd);
1420 if (tape->chrdev_dir == IDETAPE_DIR_WRITE) {
1421 ide_tape_flush_merge_buffer(drive);
1422 idetape_flush_tape_buffers(drive);
1424 if (cmd == MTIOCGET || cmd == MTIOCPOS) {
1425 block_offset = tape->valid /
1426 (tape->blk_size * tape->user_bs_factor);
1427 position = idetape_read_position(drive);
1433 if (copy_from_user(&mtop, argp, sizeof(struct mtop)))
1435 return idetape_mtioctop(drive, mtop.mt_op, mtop.mt_count);
1437 memset(&mtget, 0, sizeof(struct mtget));
1438 mtget.mt_type = MT_ISSCSI2;
1439 mtget.mt_blkno = position / tape->user_bs_factor - block_offset;
1441 ((tape->blk_size * tape->user_bs_factor)
1442 << MT_ST_BLKSIZE_SHIFT) & MT_ST_BLKSIZE_MASK;
1444 if (tape->drv_write_prot)
1445 mtget.mt_gstat |= GMT_WR_PROT(0xffffffff);
1447 if (copy_to_user(argp, &mtget, sizeof(struct mtget)))
1451 mtpos.mt_blkno = position / tape->user_bs_factor - block_offset;
1452 if (copy_to_user(argp, &mtpos, sizeof(struct mtpos)))
1456 if (tape->chrdev_dir == IDETAPE_DIR_READ)
1457 ide_tape_discard_merge_buffer(drive, 1);
1458 return idetape_blkdev_ioctl(drive, cmd, arg);
1463 * Do a mode sense page 0 with block descriptor and if it succeeds set the tape
1464 * block size with the reported value.
1466 static void ide_tape_get_bsize_from_bdesc(ide_drive_t *drive)
1468 idetape_tape_t *tape = drive->driver_data;
1469 struct ide_atapi_pc pc;
1471 idetape_create_mode_sense_cmd(&pc, IDETAPE_BLOCK_DESCRIPTOR);
1472 if (ide_queue_pc_tail(drive, tape->disk, &pc)) {
1473 printk(KERN_ERR "ide-tape: Can't get block descriptor\n");
1474 if (tape->blk_size == 0) {
1475 printk(KERN_WARNING "ide-tape: Cannot deal with zero "
1476 "block size, assuming 32k\n");
1477 tape->blk_size = 32768;
1481 tape->blk_size = (pc.buf[4 + 5] << 16) +
1482 (pc.buf[4 + 6] << 8) +
1484 tape->drv_write_prot = (pc.buf[2] & 0x80) >> 7;
1487 static int idetape_chrdev_open(struct inode *inode, struct file *filp)
1489 unsigned int minor = iminor(inode), i = minor & ~0xc0;
1491 idetape_tape_t *tape;
1494 if (i >= MAX_HWIFS * MAX_DRIVES)
1498 tape = ide_tape_chrdev_get(i);
1504 debug_log(DBG_CHRDEV, "Enter %s\n", __func__);
1507 * We really want to do nonseekable_open(inode, filp); here, but some
1508 * versions of tar incorrectly call lseek on tapes and bail out if that
1509 * fails. So we disallow pread() and pwrite(), but permit lseeks.
1511 filp->f_mode &= ~(FMODE_PREAD | FMODE_PWRITE);
1513 drive = tape->drive;
1515 filp->private_data = tape;
1517 if (test_and_set_bit(ilog2(IDE_AFLAG_BUSY), &drive->atapi_flags)) {
1522 retval = idetape_wait_ready(drive, 60 * HZ);
1524 clear_bit(ilog2(IDE_AFLAG_BUSY), &drive->atapi_flags);
1525 printk(KERN_ERR "ide-tape: %s: drive not ready\n", tape->name);
1529 idetape_read_position(drive);
1530 if (!test_bit(ilog2(IDE_AFLAG_ADDRESS_VALID), &drive->atapi_flags))
1531 (void)idetape_rewind_tape(drive);
1533 /* Read block size and write protect status from drive. */
1534 ide_tape_get_bsize_from_bdesc(drive);
1536 /* Set write protect flag if device is opened as read-only. */
1537 if ((filp->f_flags & O_ACCMODE) == O_RDONLY)
1538 tape->write_prot = 1;
1540 tape->write_prot = tape->drv_write_prot;
1542 /* Make sure drive isn't write protected if user wants to write. */
1543 if (tape->write_prot) {
1544 if ((filp->f_flags & O_ACCMODE) == O_WRONLY ||
1545 (filp->f_flags & O_ACCMODE) == O_RDWR) {
1546 clear_bit(ilog2(IDE_AFLAG_BUSY), &drive->atapi_flags);
1552 /* Lock the tape drive door so user can't eject. */
1553 if (tape->chrdev_dir == IDETAPE_DIR_NONE) {
1554 if (!ide_set_media_lock(drive, tape->disk, 1)) {
1555 if (tape->door_locked != DOOR_EXPLICITLY_LOCKED)
1556 tape->door_locked = DOOR_LOCKED;
1568 static void idetape_write_release(ide_drive_t *drive, unsigned int minor)
1570 idetape_tape_t *tape = drive->driver_data;
1572 ide_tape_flush_merge_buffer(drive);
1573 tape->buf = kmalloc(tape->buffer_size, GFP_KERNEL);
1574 if (tape->buf != NULL) {
1575 idetape_pad_zeros(drive, tape->blk_size *
1576 (tape->user_bs_factor - 1));
1580 idetape_write_filemark(drive);
1581 idetape_flush_tape_buffers(drive);
1582 idetape_flush_tape_buffers(drive);
1585 static int idetape_chrdev_release(struct inode *inode, struct file *filp)
1587 struct ide_tape_obj *tape = filp->private_data;
1588 ide_drive_t *drive = tape->drive;
1589 unsigned int minor = iminor(inode);
1592 tape = drive->driver_data;
1594 debug_log(DBG_CHRDEV, "Enter %s\n", __func__);
1596 if (tape->chrdev_dir == IDETAPE_DIR_WRITE)
1597 idetape_write_release(drive, minor);
1598 if (tape->chrdev_dir == IDETAPE_DIR_READ) {
1600 ide_tape_discard_merge_buffer(drive, 1);
1603 if (minor < 128 && test_bit(ilog2(IDE_AFLAG_MEDIUM_PRESENT),
1604 &drive->atapi_flags))
1605 (void) idetape_rewind_tape(drive);
1607 if (tape->chrdev_dir == IDETAPE_DIR_NONE) {
1608 if (tape->door_locked == DOOR_LOCKED) {
1609 if (!ide_set_media_lock(drive, tape->disk, 0))
1610 tape->door_locked = DOOR_UNLOCKED;
1613 clear_bit(ilog2(IDE_AFLAG_BUSY), &drive->atapi_flags);
1619 static void idetape_get_inquiry_results(ide_drive_t *drive)
1621 idetape_tape_t *tape = drive->driver_data;
1622 struct ide_atapi_pc pc;
1624 char fw_rev[4], vendor_id[8], product_id[16];
1626 idetape_create_inquiry_cmd(&pc);
1627 pc.buf = &pc_buf[0];
1628 pc.buf_size = sizeof(pc_buf);
1630 if (ide_queue_pc_tail(drive, tape->disk, &pc)) {
1631 printk(KERN_ERR "ide-tape: %s: can't get INQUIRY results\n",
1635 memcpy(vendor_id, &pc.buf[8], 8);
1636 memcpy(product_id, &pc.buf[16], 16);
1637 memcpy(fw_rev, &pc.buf[32], 4);
1639 ide_fixstring(vendor_id, 8, 0);
1640 ide_fixstring(product_id, 16, 0);
1641 ide_fixstring(fw_rev, 4, 0);
1643 printk(KERN_INFO "ide-tape: %s <-> %s: %.8s %.16s rev %.4s\n",
1644 drive->name, tape->name, vendor_id, product_id, fw_rev);
1648 * Ask the tape about its various parameters. In particular, we will adjust our
1649 * data transfer buffer size to the recommended value as returned by the tape.
1651 static void idetape_get_mode_sense_results(ide_drive_t *drive)
1653 idetape_tape_t *tape = drive->driver_data;
1654 struct ide_atapi_pc pc;
1656 u8 speed, max_speed;
1658 idetape_create_mode_sense_cmd(&pc, IDETAPE_CAPABILITIES_PAGE);
1659 if (ide_queue_pc_tail(drive, tape->disk, &pc)) {
1660 printk(KERN_ERR "ide-tape: Can't get tape parameters - assuming"
1661 " some default values\n");
1662 tape->blk_size = 512;
1663 put_unaligned(52, (u16 *)&tape->caps[12]);
1664 put_unaligned(540, (u16 *)&tape->caps[14]);
1665 put_unaligned(6*52, (u16 *)&tape->caps[16]);
1668 caps = pc.buf + 4 + pc.buf[3];
1670 /* convert to host order and save for later use */
1671 speed = be16_to_cpup((__be16 *)&caps[14]);
1672 max_speed = be16_to_cpup((__be16 *)&caps[8]);
1674 *(u16 *)&caps[8] = max_speed;
1675 *(u16 *)&caps[12] = be16_to_cpup((__be16 *)&caps[12]);
1676 *(u16 *)&caps[14] = speed;
1677 *(u16 *)&caps[16] = be16_to_cpup((__be16 *)&caps[16]);
1680 printk(KERN_INFO "ide-tape: %s: invalid tape speed "
1681 "(assuming 650KB/sec)\n", drive->name);
1682 *(u16 *)&caps[14] = 650;
1685 printk(KERN_INFO "ide-tape: %s: invalid max_speed "
1686 "(assuming 650KB/sec)\n", drive->name);
1687 *(u16 *)&caps[8] = 650;
1690 memcpy(&tape->caps, caps, 20);
1692 /* device lacks locking support according to capabilities page */
1693 if ((caps[6] & 1) == 0)
1694 drive->dev_flags &= ~IDE_DFLAG_DOORLOCKING;
1697 tape->blk_size = 512;
1698 else if (caps[7] & 0x04)
1699 tape->blk_size = 1024;
1702 #ifdef CONFIG_IDE_PROC_FS
1703 #define ide_tape_devset_get(name, field) \
1704 static int get_##name(ide_drive_t *drive) \
1706 idetape_tape_t *tape = drive->driver_data; \
1707 return tape->field; \
1710 #define ide_tape_devset_set(name, field) \
1711 static int set_##name(ide_drive_t *drive, int arg) \
1713 idetape_tape_t *tape = drive->driver_data; \
1714 tape->field = arg; \
1718 #define ide_tape_devset_rw_field(_name, _field) \
1719 ide_tape_devset_get(_name, _field) \
1720 ide_tape_devset_set(_name, _field) \
1721 IDE_DEVSET(_name, DS_SYNC, get_##_name, set_##_name)
1723 #define ide_tape_devset_r_field(_name, _field) \
1724 ide_tape_devset_get(_name, _field) \
1725 IDE_DEVSET(_name, 0, get_##_name, NULL)
1727 static int mulf_tdsc(ide_drive_t *drive) { return 1000; }
1728 static int divf_tdsc(ide_drive_t *drive) { return HZ; }
1729 static int divf_buffer(ide_drive_t *drive) { return 2; }
1730 static int divf_buffer_size(ide_drive_t *drive) { return 1024; }
1732 ide_devset_rw_flag(dsc_overlap, IDE_DFLAG_DSC_OVERLAP);
1734 ide_tape_devset_rw_field(debug_mask, debug_mask);
1735 ide_tape_devset_rw_field(tdsc, best_dsc_rw_freq);
1737 ide_tape_devset_r_field(avg_speed, avg_speed);
1738 ide_tape_devset_r_field(speed, caps[14]);
1739 ide_tape_devset_r_field(buffer, caps[16]);
1740 ide_tape_devset_r_field(buffer_size, buffer_size);
1742 static const struct ide_proc_devset idetape_settings[] = {
1743 __IDE_PROC_DEVSET(avg_speed, 0, 0xffff, NULL, NULL),
1744 __IDE_PROC_DEVSET(buffer, 0, 0xffff, NULL, divf_buffer),
1745 __IDE_PROC_DEVSET(buffer_size, 0, 0xffff, NULL, divf_buffer_size),
1746 __IDE_PROC_DEVSET(debug_mask, 0, 0xffff, NULL, NULL),
1747 __IDE_PROC_DEVSET(dsc_overlap, 0, 1, NULL, NULL),
1748 __IDE_PROC_DEVSET(speed, 0, 0xffff, NULL, NULL),
1749 __IDE_PROC_DEVSET(tdsc, IDETAPE_DSC_RW_MIN, IDETAPE_DSC_RW_MAX,
1750 mulf_tdsc, divf_tdsc),
1756 * The function below is called to:
1758 * 1. Initialize our various state variables.
1759 * 2. Ask the tape for its capabilities.
1760 * 3. Allocate a buffer which will be used for data transfer. The buffer size
1761 * is chosen based on the recommendation which we received in step 2.
1763 * Note that at this point ide.c already assigned us an irq, so that we can
1764 * queue requests here and wait for their completion.
1766 static void idetape_setup(ide_drive_t *drive, idetape_tape_t *tape, int minor)
1771 u16 *ctl = (u16 *)&tape->caps[12];
1773 drive->pc_callback = ide_tape_callback;
1775 drive->dev_flags |= IDE_DFLAG_DSC_OVERLAP;
1777 if (drive->hwif->host_flags & IDE_HFLAG_NO_DSC) {
1778 printk(KERN_INFO "ide-tape: %s: disabling DSC overlap\n",
1780 drive->dev_flags &= ~IDE_DFLAG_DSC_OVERLAP;
1783 /* Seagate Travan drives do not support DSC overlap. */
1784 if (strstr((char *)&drive->id[ATA_ID_PROD], "Seagate STT3401"))
1785 drive->dev_flags &= ~IDE_DFLAG_DSC_OVERLAP;
1787 tape->minor = minor;
1788 tape->name[0] = 'h';
1789 tape->name[1] = 't';
1790 tape->name[2] = '0' + minor;
1791 tape->chrdev_dir = IDETAPE_DIR_NONE;
1793 idetape_get_inquiry_results(drive);
1794 idetape_get_mode_sense_results(drive);
1795 ide_tape_get_bsize_from_bdesc(drive);
1796 tape->user_bs_factor = 1;
1797 tape->buffer_size = *ctl * tape->blk_size;
1798 while (tape->buffer_size > 0xffff) {
1799 printk(KERN_NOTICE "ide-tape: decreasing stage size\n");
1801 tape->buffer_size = *ctl * tape->blk_size;
1803 buffer_size = tape->buffer_size;
1805 /* select the "best" DSC read/write polling freq */
1806 speed = max(*(u16 *)&tape->caps[14], *(u16 *)&tape->caps[8]);
1808 t = (IDETAPE_FIFO_THRESHOLD * tape->buffer_size * HZ) / (speed * 1000);
1811 * Ensure that the number we got makes sense; limit it within
1812 * IDETAPE_DSC_RW_MIN and IDETAPE_DSC_RW_MAX.
1814 tape->best_dsc_rw_freq = clamp_t(unsigned long, t, IDETAPE_DSC_RW_MIN,
1815 IDETAPE_DSC_RW_MAX);
1816 printk(KERN_INFO "ide-tape: %s <-> %s: %dKBps, %d*%dkB buffer, "
1818 drive->name, tape->name, *(u16 *)&tape->caps[14],
1819 (*(u16 *)&tape->caps[16] * 512) / tape->buffer_size,
1820 tape->buffer_size / 1024,
1821 tape->best_dsc_rw_freq * 1000 / HZ,
1822 (drive->dev_flags & IDE_DFLAG_USING_DMA) ? ", DMA" : "");
1824 ide_proc_register_driver(drive, tape->driver);
1827 static void ide_tape_remove(ide_drive_t *drive)
1829 idetape_tape_t *tape = drive->driver_data;
1831 ide_proc_unregister_driver(drive, tape->driver);
1832 device_del(&tape->dev);
1833 ide_unregister_region(tape->disk);
1835 mutex_lock(&idetape_ref_mutex);
1836 put_device(&tape->dev);
1837 mutex_unlock(&idetape_ref_mutex);
1840 static void ide_tape_release(struct device *dev)
1842 struct ide_tape_obj *tape = to_ide_drv(dev, ide_tape_obj);
1843 ide_drive_t *drive = tape->drive;
1844 struct gendisk *g = tape->disk;
1846 BUG_ON(tape->valid);
1848 drive->dev_flags &= ~IDE_DFLAG_DSC_OVERLAP;
1849 drive->driver_data = NULL;
1850 device_destroy(idetape_sysfs_class, MKDEV(IDETAPE_MAJOR, tape->minor));
1851 device_destroy(idetape_sysfs_class,
1852 MKDEV(IDETAPE_MAJOR, tape->minor + 128));
1853 idetape_devs[tape->minor] = NULL;
1854 g->private_data = NULL;
1859 #ifdef CONFIG_IDE_PROC_FS
1860 static int proc_idetape_read_name
1861 (char *page, char **start, off_t off, int count, int *eof, void *data)
1863 ide_drive_t *drive = (ide_drive_t *) data;
1864 idetape_tape_t *tape = drive->driver_data;
1868 len = sprintf(out, "%s\n", tape->name);
1869 PROC_IDE_READ_RETURN(page, start, off, count, eof, len);
1872 static ide_proc_entry_t idetape_proc[] = {
1873 { "capacity", S_IFREG|S_IRUGO, proc_ide_read_capacity, NULL },
1874 { "name", S_IFREG|S_IRUGO, proc_idetape_read_name, NULL },
1875 { NULL, 0, NULL, NULL }
1878 static ide_proc_entry_t *ide_tape_proc_entries(ide_drive_t *drive)
1880 return idetape_proc;
1883 static const struct ide_proc_devset *ide_tape_proc_devsets(ide_drive_t *drive)
1885 return idetape_settings;
1889 static int ide_tape_probe(ide_drive_t *);
1891 static struct ide_driver idetape_driver = {
1893 .owner = THIS_MODULE,
1895 .bus = &ide_bus_type,
1897 .probe = ide_tape_probe,
1898 .remove = ide_tape_remove,
1899 .version = IDETAPE_VERSION,
1900 .do_request = idetape_do_request,
1901 #ifdef CONFIG_IDE_PROC_FS
1902 .proc_entries = ide_tape_proc_entries,
1903 .proc_devsets = ide_tape_proc_devsets,
1907 /* Our character device supporting functions, passed to register_chrdev. */
1908 static const struct file_operations idetape_fops = {
1909 .owner = THIS_MODULE,
1910 .read = idetape_chrdev_read,
1911 .write = idetape_chrdev_write,
1912 .ioctl = idetape_chrdev_ioctl,
1913 .open = idetape_chrdev_open,
1914 .release = idetape_chrdev_release,
1917 static int idetape_open(struct block_device *bdev, fmode_t mode)
1919 struct ide_tape_obj *tape = ide_tape_get(bdev->bd_disk);
1927 static int idetape_release(struct gendisk *disk, fmode_t mode)
1929 struct ide_tape_obj *tape = ide_drv_g(disk, ide_tape_obj);
1935 static int idetape_ioctl(struct block_device *bdev, fmode_t mode,
1936 unsigned int cmd, unsigned long arg)
1938 struct ide_tape_obj *tape = ide_drv_g(bdev->bd_disk, ide_tape_obj);
1939 ide_drive_t *drive = tape->drive;
1940 int err = generic_ide_ioctl(drive, bdev, cmd, arg);
1942 err = idetape_blkdev_ioctl(drive, cmd, arg);
1946 static struct block_device_operations idetape_block_ops = {
1947 .owner = THIS_MODULE,
1948 .open = idetape_open,
1949 .release = idetape_release,
1950 .locked_ioctl = idetape_ioctl,
1953 static int ide_tape_probe(ide_drive_t *drive)
1955 idetape_tape_t *tape;
1959 if (!strstr("ide-tape", drive->driver_req))
1962 if (drive->media != ide_tape)
1965 if ((drive->dev_flags & IDE_DFLAG_ID_READ) &&
1966 ide_check_atapi_device(drive, DRV_NAME) == 0) {
1967 printk(KERN_ERR "ide-tape: %s: not supported by this version of"
1968 " the driver\n", drive->name);
1971 tape = kzalloc(sizeof(idetape_tape_t), GFP_KERNEL);
1973 printk(KERN_ERR "ide-tape: %s: Can't allocate a tape struct\n",
1978 g = alloc_disk(1 << PARTN_BITS);
1982 ide_init_disk(g, drive);
1984 tape->dev.parent = &drive->gendev;
1985 tape->dev.release = ide_tape_release;
1986 dev_set_name(&tape->dev, dev_name(&drive->gendev));
1988 if (device_register(&tape->dev))
1991 tape->drive = drive;
1992 tape->driver = &idetape_driver;
1995 g->private_data = &tape->driver;
1997 drive->driver_data = tape;
1999 mutex_lock(&idetape_ref_mutex);
2000 for (minor = 0; idetape_devs[minor]; minor++)
2002 idetape_devs[minor] = tape;
2003 mutex_unlock(&idetape_ref_mutex);
2005 idetape_setup(drive, tape, minor);
2007 device_create(idetape_sysfs_class, &drive->gendev,
2008 MKDEV(IDETAPE_MAJOR, minor), NULL, "%s", tape->name);
2009 device_create(idetape_sysfs_class, &drive->gendev,
2010 MKDEV(IDETAPE_MAJOR, minor + 128), NULL,
2013 g->fops = &idetape_block_ops;
2014 ide_register_region(g);
2026 static void __exit idetape_exit(void)
2028 driver_unregister(&idetape_driver.gen_driver);
2029 class_destroy(idetape_sysfs_class);
2030 unregister_chrdev(IDETAPE_MAJOR, "ht");
2033 static int __init idetape_init(void)
2036 idetape_sysfs_class = class_create(THIS_MODULE, "ide_tape");
2037 if (IS_ERR(idetape_sysfs_class)) {
2038 idetape_sysfs_class = NULL;
2039 printk(KERN_ERR "Unable to create sysfs class for ide tapes\n");
2044 if (register_chrdev(IDETAPE_MAJOR, "ht", &idetape_fops)) {
2045 printk(KERN_ERR "ide-tape: Failed to register chrdev"
2048 goto out_free_class;
2051 error = driver_register(&idetape_driver.gen_driver);
2053 goto out_free_driver;
2058 driver_unregister(&idetape_driver.gen_driver);
2060 class_destroy(idetape_sysfs_class);
2065 MODULE_ALIAS("ide:*m-tape*");
2066 module_init(idetape_init);
2067 module_exit(idetape_exit);
2068 MODULE_ALIAS_CHARDEV_MAJOR(IDETAPE_MAJOR);
2069 MODULE_DESCRIPTION("ATAPI Streaming TAPE Driver");
2070 MODULE_LICENSE("GPL");