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),
140 /* Tape door status */
141 #define DOOR_UNLOCKED 0
142 #define DOOR_LOCKED 1
143 #define DOOR_EXPLICITLY_LOCKED 2
145 /* Some defines for the SPACE command */
146 #define IDETAPE_SPACE_OVER_FILEMARK 1
147 #define IDETAPE_SPACE_TO_EOD 3
149 /* Some defines for the LOAD UNLOAD command */
150 #define IDETAPE_LU_LOAD_MASK 1
151 #define IDETAPE_LU_RETENSION_MASK 2
152 #define IDETAPE_LU_EOT_MASK 4
154 /* Structures related to the SELECT SENSE / MODE SENSE packet commands. */
155 #define IDETAPE_BLOCK_DESCRIPTOR 0
156 #define IDETAPE_CAPABILITIES_PAGE 0x2a
159 * Most of our global data which we need to save even as we leave the driver due
160 * to an interrupt or a timer event is stored in the struct defined below.
162 typedef struct ide_tape_obj {
164 struct ide_driver *driver;
165 struct gendisk *disk;
168 /* used by REQ_IDETAPE_{READ,WRITE} requests */
169 struct ide_atapi_pc queued_pc;
172 * DSC polling variables.
174 * While polling for DSC we use postponed_rq to postpone the current
175 * request so that ide.c will be able to service pending requests on the
176 * other device. Note that at most we will have only one DSC (usually
177 * data transfer) request in the device request queue.
179 struct request *postponed_rq;
180 /* The time in which we started polling for DSC */
181 unsigned long dsc_polling_start;
182 /* Timer used to poll for dsc */
183 struct timer_list dsc_timer;
184 /* Read/Write dsc polling frequency */
185 unsigned long best_dsc_rw_freq;
186 unsigned long dsc_poll_freq;
187 unsigned long dsc_timeout;
189 /* Read position information */
192 unsigned int first_frame;
194 /* Last error information */
195 u8 sense_key, asc, ascq;
197 /* Character device operation */
201 /* Current character device data transfer direction */
204 /* tape block size, usually 512 or 1024 bytes */
205 unsigned short blk_size;
208 /* Copy of the tape's Capabilities and Mechanical Page */
212 * Active data transfer request parameters.
214 * At most, there is only one ide-tape originated data transfer request
215 * in the device request queue. This allows ide.c to easily service
216 * requests from the other device when we postpone our active request.
219 /* Data buffer size chosen based on the tape's recommendation */
222 struct idetape_bh *merge_bh;
223 /* size of the merge buffer */
225 /* pointer to current buffer head within the merge buffer */
226 struct idetape_bh *bh;
230 /* Measures average tape speed */
231 unsigned long avg_time;
235 /* the door is currently locked */
237 /* the tape hardware is write protected */
239 /* the tape is write protected (hardware or opened as read-only) */
245 static DEFINE_MUTEX(idetape_ref_mutex);
247 static struct class *idetape_sysfs_class;
249 static void ide_tape_release(struct device *);
251 static struct ide_tape_obj *ide_tape_get(struct gendisk *disk)
253 struct ide_tape_obj *tape = NULL;
255 mutex_lock(&idetape_ref_mutex);
256 tape = ide_drv_g(disk, ide_tape_obj);
258 if (ide_device_get(tape->drive))
261 get_device(&tape->dev);
263 mutex_unlock(&idetape_ref_mutex);
267 static void ide_tape_put(struct ide_tape_obj *tape)
269 ide_drive_t *drive = tape->drive;
271 mutex_lock(&idetape_ref_mutex);
272 put_device(&tape->dev);
273 ide_device_put(drive);
274 mutex_unlock(&idetape_ref_mutex);
278 * The variables below are used for the character device interface. Additional
279 * state variables are defined in our ide_drive_t structure.
281 static struct ide_tape_obj *idetape_devs[MAX_HWIFS * MAX_DRIVES];
283 static struct ide_tape_obj *ide_tape_chrdev_get(unsigned int i)
285 struct ide_tape_obj *tape = NULL;
287 mutex_lock(&idetape_ref_mutex);
288 tape = idetape_devs[i];
290 get_device(&tape->dev);
291 mutex_unlock(&idetape_ref_mutex);
296 * called on each failed packet command retry to analyze the request sense. We
297 * currently do not utilize this information.
299 static void idetape_analyze_error(ide_drive_t *drive, u8 *sense)
301 idetape_tape_t *tape = drive->driver_data;
302 struct ide_atapi_pc *pc = drive->failed_pc;
304 tape->sense_key = sense[2] & 0xF;
305 tape->asc = sense[12];
306 tape->ascq = sense[13];
308 debug_log(DBG_ERR, "pc = %x, sense key = %x, asc = %x, ascq = %x\n",
309 pc->c[0], tape->sense_key, tape->asc, tape->ascq);
311 /* Correct pc->xferred by asking the tape. */
312 if (pc->flags & PC_FLAG_DMA_ERROR)
313 pc->xferred = pc->req_xfer -
315 get_unaligned_be32(&sense[3]);
318 * If error was the result of a zero-length read or write command,
319 * with sense key=5, asc=0x22, ascq=0, let it slide. Some drives
320 * (i.e. Seagate STT3401A Travan) don't support 0-length read/writes.
322 if ((pc->c[0] == READ_6 || pc->c[0] == WRITE_6)
324 && pc->c[4] == 0 && pc->c[3] == 0 && pc->c[2] == 0) {
325 if (tape->sense_key == 5) {
326 /* don't report an error, everything's ok */
328 /* don't retry read/write */
329 pc->flags |= PC_FLAG_ABORT;
332 if (pc->c[0] == READ_6 && (sense[2] & 0x80)) {
333 pc->error = IDE_DRV_ERROR_FILEMARK;
334 pc->flags |= PC_FLAG_ABORT;
336 if (pc->c[0] == WRITE_6) {
337 if ((sense[2] & 0x40) || (tape->sense_key == 0xd
338 && tape->asc == 0x0 && tape->ascq == 0x2)) {
339 pc->error = IDE_DRV_ERROR_EOD;
340 pc->flags |= PC_FLAG_ABORT;
343 if (pc->c[0] == READ_6 || pc->c[0] == WRITE_6) {
344 if (tape->sense_key == 8) {
345 pc->error = IDE_DRV_ERROR_EOD;
346 pc->flags |= PC_FLAG_ABORT;
348 if (!(pc->flags & PC_FLAG_ABORT) &&
350 pc->retries = IDETAPE_MAX_PC_RETRIES + 1;
354 /* Free data buffers completely. */
355 static void ide_tape_kfree_buffer(idetape_tape_t *tape)
357 struct idetape_bh *bh = tape->merge_bh;
363 static void ide_tape_handle_dsc(ide_drive_t *);
365 static int ide_tape_callback(ide_drive_t *drive, int dsc)
367 idetape_tape_t *tape = drive->driver_data;
368 struct ide_atapi_pc *pc = drive->pc;
369 struct request *rq = drive->hwif->rq;
370 int uptodate = pc->error ? 0 : 1;
371 int err = uptodate ? 0 : IDE_DRV_ERROR_GENERAL;
373 debug_log(DBG_PROCS, "Enter %s\n", __func__);
376 ide_tape_handle_dsc(drive);
378 if (drive->failed_pc == pc)
379 drive->failed_pc = NULL;
381 if (pc->c[0] == REQUEST_SENSE) {
383 idetape_analyze_error(drive, pc->buf);
385 printk(KERN_ERR "ide-tape: Error in REQUEST SENSE "
386 "itself - Aborting request!\n");
387 } else if (pc->c[0] == READ_6 || pc->c[0] == WRITE_6) {
388 int blocks = pc->xferred / tape->blk_size;
390 tape->avg_size += blocks * tape->blk_size;
392 if (time_after_eq(jiffies, tape->avg_time + HZ)) {
393 tape->avg_speed = tape->avg_size * HZ /
394 (jiffies - tape->avg_time) / 1024;
396 tape->avg_time = jiffies;
399 tape->first_frame += blocks;
400 rq->data_len -= blocks * tape->blk_size;
406 } else if (pc->c[0] == READ_POSITION && uptodate) {
407 u8 *readpos = pc->buf;
409 debug_log(DBG_SENSE, "BOP - %s\n",
410 (readpos[0] & 0x80) ? "Yes" : "No");
411 debug_log(DBG_SENSE, "EOP - %s\n",
412 (readpos[0] & 0x40) ? "Yes" : "No");
414 if (readpos[0] & 0x4) {
415 printk(KERN_INFO "ide-tape: Block location is unknown"
417 clear_bit(IDE_AFLAG_ADDRESS_VALID, &drive->atapi_flags);
419 err = IDE_DRV_ERROR_GENERAL;
421 debug_log(DBG_SENSE, "Block Location - %u\n",
422 be32_to_cpup((__be32 *)&readpos[4]));
424 tape->partition = readpos[1];
425 tape->first_frame = be32_to_cpup((__be32 *)&readpos[4]);
426 set_bit(IDE_AFLAG_ADDRESS_VALID, &drive->atapi_flags);
436 * Postpone the current request so that ide.c will be able to service requests
437 * from another device on the same port while we are polling for DSC.
439 static void idetape_postpone_request(ide_drive_t *drive)
441 idetape_tape_t *tape = drive->driver_data;
443 debug_log(DBG_PROCS, "Enter %s\n", __func__);
445 tape->postponed_rq = drive->hwif->rq;
447 ide_stall_queue(drive, tape->dsc_poll_freq);
450 static void ide_tape_handle_dsc(ide_drive_t *drive)
452 idetape_tape_t *tape = drive->driver_data;
454 /* Media access command */
455 tape->dsc_polling_start = jiffies;
456 tape->dsc_poll_freq = IDETAPE_DSC_MA_FAST;
457 tape->dsc_timeout = jiffies + IDETAPE_DSC_MA_TIMEOUT;
458 /* Allow ide.c to handle other requests */
459 idetape_postpone_request(drive);
463 * Packet Command Interface
465 * The current Packet Command is available in drive->pc, and will not change
466 * until we finish handling it. Each packet command is associated with a
467 * callback function that will be called when the command is finished.
469 * The handling will be done in three stages:
471 * 1. ide_tape_issue_pc will send the packet command to the drive, and will set
472 * the interrupt handler to ide_pc_intr.
474 * 2. On each interrupt, ide_pc_intr will be called. This step will be
475 * repeated until the device signals us that no more interrupts will be issued.
477 * 3. ATAPI Tape media access commands have immediate status with a delayed
478 * process. In case of a successful initiation of a media access packet command,
479 * the DSC bit will be set when the actual execution of the command is finished.
480 * Since the tape drive will not issue an interrupt, we have to poll for this
481 * event. In this case, we define the request as "low priority request" by
482 * setting rq_status to IDETAPE_RQ_POSTPONED, set a timer to poll for DSC and
485 * ide.c will then give higher priority to requests which originate from the
486 * other device, until will change rq_status to RQ_ACTIVE.
488 * 4. When the packet command is finished, it will be checked for errors.
490 * 5. In case an error was found, we queue a request sense packet command in
491 * front of the request queue and retry the operation up to
492 * IDETAPE_MAX_PC_RETRIES times.
494 * 6. In case no error was found, or we decided to give up and not to retry
495 * again, the callback function will be called and then we will handle the next
499 static ide_startstop_t ide_tape_issue_pc(ide_drive_t *drive,
501 struct ide_atapi_pc *pc)
503 idetape_tape_t *tape = drive->driver_data;
505 if (drive->failed_pc == NULL && pc->c[0] != REQUEST_SENSE)
506 drive->failed_pc = pc;
508 /* Set the current packet command */
511 if (pc->retries > IDETAPE_MAX_PC_RETRIES ||
512 (pc->flags & PC_FLAG_ABORT)) {
513 unsigned int done = blk_rq_bytes(drive->hwif->rq);
516 * We will "abort" retrying a packet command in case legitimate
517 * error code was received (crossing a filemark, or end of the
518 * media, for example).
520 if (!(pc->flags & PC_FLAG_ABORT)) {
521 if (!(pc->c[0] == TEST_UNIT_READY &&
522 tape->sense_key == 2 && tape->asc == 4 &&
523 (tape->ascq == 1 || tape->ascq == 8))) {
524 printk(KERN_ERR "ide-tape: %s: I/O error, "
525 "pc = %2x, key = %2x, "
526 "asc = %2x, ascq = %2x\n",
527 tape->name, pc->c[0],
528 tape->sense_key, tape->asc,
532 pc->error = IDE_DRV_ERROR_GENERAL;
535 drive->failed_pc = NULL;
536 drive->pc_callback(drive, 0);
537 ide_complete_rq(drive, -EIO, done);
540 debug_log(DBG_SENSE, "Retry #%d, cmd = %02X\n", pc->retries, pc->c[0]);
544 return ide_issue_pc(drive, cmd);
547 /* A mode sense command is used to "sense" tape parameters. */
548 static void idetape_create_mode_sense_cmd(struct ide_atapi_pc *pc, u8 page_code)
551 pc->c[0] = MODE_SENSE;
552 if (page_code != IDETAPE_BLOCK_DESCRIPTOR)
553 /* DBD = 1 - Don't return block descriptors */
555 pc->c[2] = page_code;
557 * Changed pc->c[3] to 0 (255 will at best return unused info).
559 * For SCSI this byte is defined as subpage instead of high byte
560 * of length and some IDE drives seem to interpret it this way
561 * and return an error when 255 is used.
564 /* We will just discard data in that case */
566 if (page_code == IDETAPE_BLOCK_DESCRIPTOR)
568 else if (page_code == IDETAPE_CAPABILITIES_PAGE)
574 static ide_startstop_t idetape_media_access_finished(ide_drive_t *drive)
576 ide_hwif_t *hwif = drive->hwif;
577 idetape_tape_t *tape = drive->driver_data;
578 struct ide_atapi_pc *pc = drive->pc;
581 stat = hwif->tp_ops->read_status(hwif);
583 if (stat & ATA_DSC) {
584 if (stat & ATA_ERR) {
586 if (pc->c[0] != TEST_UNIT_READY)
587 printk(KERN_ERR "ide-tape: %s: I/O error, ",
589 /* Retry operation */
595 pc->error = IDE_DRV_ERROR_GENERAL;
596 drive->failed_pc = NULL;
598 drive->pc_callback(drive, 0);
602 static void ide_tape_create_rw_cmd(idetape_tape_t *tape,
603 struct ide_atapi_pc *pc, struct request *rq,
606 struct idetape_bh *bh = (struct idetape_bh *)rq->special;
607 unsigned int length = rq->current_nr_sectors;
610 put_unaligned(cpu_to_be32(length), (unsigned int *) &pc->c[1]);
614 pc->buf_size = length * tape->blk_size;
615 pc->req_xfer = pc->buf_size;
616 if (pc->req_xfer == tape->buffer_size)
617 pc->flags |= PC_FLAG_DMA_OK;
619 if (opcode == READ_6) {
621 atomic_set(&bh->b_count, 0);
622 } else if (opcode == WRITE_6) {
624 pc->flags |= PC_FLAG_WRITING;
625 pc->b_data = bh->b_data;
626 pc->b_count = atomic_read(&bh->b_count);
629 memcpy(rq->cmd, pc->c, 12);
632 static ide_startstop_t idetape_do_request(ide_drive_t *drive,
633 struct request *rq, sector_t block)
635 ide_hwif_t *hwif = drive->hwif;
636 idetape_tape_t *tape = drive->driver_data;
637 struct ide_atapi_pc *pc = NULL;
638 struct request *postponed_rq = tape->postponed_rq;
642 debug_log(DBG_SENSE, "sector: %llu, nr_sectors: %lu,"
643 " current_nr_sectors: %u\n",
644 (unsigned long long)rq->sector, rq->nr_sectors,
645 rq->current_nr_sectors);
647 if (!(blk_special_request(rq) || blk_sense_request(rq))) {
648 /* We do not support buffer cache originated requests. */
649 printk(KERN_NOTICE "ide-tape: %s: Unsupported request in "
650 "request queue (%d)\n", drive->name, rq->cmd_type);
651 if (blk_fs_request(rq) == 0 && rq->errors == 0)
653 ide_complete_rq(drive, -EIO, ide_rq_bytes(rq));
657 /* Retry a failed packet command */
658 if (drive->failed_pc && drive->pc->c[0] == REQUEST_SENSE) {
659 pc = drive->failed_pc;
663 if (postponed_rq != NULL)
664 if (rq != postponed_rq) {
665 printk(KERN_ERR "ide-tape: ide-tape.c bug - "
666 "Two DSC requests were queued\n");
667 drive->failed_pc = NULL;
669 ide_complete_rq(drive, 0, blk_rq_bytes(rq));
673 tape->postponed_rq = NULL;
676 * If the tape is still busy, postpone our request and service
677 * the other device meanwhile.
679 stat = hwif->tp_ops->read_status(hwif);
681 if ((drive->dev_flags & IDE_DFLAG_DSC_OVERLAP) == 0 &&
682 (rq->cmd[13] & REQ_IDETAPE_PC2) == 0)
683 set_bit(IDE_AFLAG_IGNORE_DSC, &drive->atapi_flags);
685 if (drive->dev_flags & IDE_DFLAG_POST_RESET) {
686 set_bit(IDE_AFLAG_IGNORE_DSC, &drive->atapi_flags);
687 drive->dev_flags &= ~IDE_DFLAG_POST_RESET;
690 if (!test_and_clear_bit(IDE_AFLAG_IGNORE_DSC, &drive->atapi_flags) &&
691 (stat & ATA_DSC) == 0) {
692 if (postponed_rq == NULL) {
693 tape->dsc_polling_start = jiffies;
694 tape->dsc_poll_freq = tape->best_dsc_rw_freq;
695 tape->dsc_timeout = jiffies + IDETAPE_DSC_RW_TIMEOUT;
696 } else if (time_after(jiffies, tape->dsc_timeout)) {
697 printk(KERN_ERR "ide-tape: %s: DSC timeout\n",
699 if (rq->cmd[13] & REQ_IDETAPE_PC2) {
700 idetape_media_access_finished(drive);
703 return ide_do_reset(drive);
705 } else if (time_after(jiffies,
706 tape->dsc_polling_start +
707 IDETAPE_DSC_MA_THRESHOLD))
708 tape->dsc_poll_freq = IDETAPE_DSC_MA_SLOW;
709 idetape_postpone_request(drive);
712 if (rq->cmd[13] & REQ_IDETAPE_READ) {
713 pc = &tape->queued_pc;
714 ide_tape_create_rw_cmd(tape, pc, rq, READ_6);
717 if (rq->cmd[13] & REQ_IDETAPE_WRITE) {
718 pc = &tape->queued_pc;
719 ide_tape_create_rw_cmd(tape, pc, rq, WRITE_6);
722 if (rq->cmd[13] & REQ_IDETAPE_PC1) {
723 pc = (struct ide_atapi_pc *)rq->special;
724 rq->cmd[13] &= ~(REQ_IDETAPE_PC1);
725 rq->cmd[13] |= REQ_IDETAPE_PC2;
728 if (rq->cmd[13] & REQ_IDETAPE_PC2) {
729 idetape_media_access_finished(drive);
735 /* prepare sense request for this command */
736 ide_prep_sense(drive, rq);
738 memset(&cmd, 0, sizeof(cmd));
741 cmd.tf_flags |= IDE_TFLAG_WRITE;
745 ide_init_sg_cmd(&cmd, pc->req_xfer);
746 ide_map_sg(drive, &cmd);
748 return ide_tape_issue_pc(drive, &cmd, pc);
752 * It returns a pointer to the newly allocated buffer, or NULL in case
755 static struct idetape_bh *ide_tape_kmalloc_buffer(idetape_tape_t *tape,
758 struct idetape_bh *bh;
760 bh = kmalloc(sizeof(struct idetape_bh), GFP_KERNEL);
764 bh->b_data = kmalloc(tape->buffer_size, GFP_KERNEL);
770 bh->b_size = tape->buffer_size;
771 atomic_set(&bh->b_count, full ? bh->b_size : 0);
776 static int idetape_copy_stage_from_user(idetape_tape_t *tape,
777 const char __user *buf, int n)
779 struct idetape_bh *bh = tape->bh;
783 if (bh == NULL || n > bh->b_size - atomic_read(&bh->b_count)) {
784 printk(KERN_ERR "ide-tape: bh == NULL in %s\n",
788 if (copy_from_user(bh->b_data + atomic_read(&bh->b_count), buf,
791 atomic_add(n, &bh->b_count);
792 if (atomic_read(&bh->b_count) == bh->b_size)
799 static int idetape_copy_stage_to_user(idetape_tape_t *tape, char __user *buf,
802 struct idetape_bh *bh = tape->bh;
806 if (bh == NULL || n > tape->b_count) {
807 printk(KERN_ERR "ide-tape: bh == NULL in %s\n",
811 if (copy_to_user(buf, tape->b_data, n))
821 static void idetape_init_merge_buffer(idetape_tape_t *tape)
823 struct idetape_bh *bh = tape->merge_bh;
824 tape->bh = tape->merge_bh;
826 if (tape->chrdev_dir == IDETAPE_DIR_WRITE)
827 atomic_set(&bh->b_count, 0);
829 tape->b_data = bh->b_data;
830 tape->b_count = atomic_read(&bh->b_count);
835 * Write a filemark if write_filemark=1. Flush the device buffers without
836 * writing a filemark otherwise.
838 static void idetape_create_write_filemark_cmd(ide_drive_t *drive,
839 struct ide_atapi_pc *pc, int write_filemark)
842 pc->c[0] = WRITE_FILEMARKS;
843 pc->c[4] = write_filemark;
844 pc->flags |= PC_FLAG_WAIT_FOR_DSC;
847 static int idetape_wait_ready(ide_drive_t *drive, unsigned long timeout)
849 idetape_tape_t *tape = drive->driver_data;
850 struct gendisk *disk = tape->disk;
851 int load_attempted = 0;
853 /* Wait for the tape to become ready */
854 set_bit(IDE_AFLAG_MEDIUM_PRESENT, &drive->atapi_flags);
856 while (time_before(jiffies, timeout)) {
857 if (ide_do_test_unit_ready(drive, disk) == 0)
859 if ((tape->sense_key == 2 && tape->asc == 4 && tape->ascq == 2)
860 || (tape->asc == 0x3A)) {
864 ide_do_start_stop(drive, disk, IDETAPE_LU_LOAD_MASK);
866 /* not about to be ready */
867 } else if (!(tape->sense_key == 2 && tape->asc == 4 &&
868 (tape->ascq == 1 || tape->ascq == 8)))
875 static int idetape_flush_tape_buffers(ide_drive_t *drive)
877 struct ide_tape_obj *tape = drive->driver_data;
878 struct ide_atapi_pc pc;
881 idetape_create_write_filemark_cmd(drive, &pc, 0);
882 rc = ide_queue_pc_tail(drive, tape->disk, &pc);
885 idetape_wait_ready(drive, 60 * 5 * HZ);
889 static void idetape_create_read_position_cmd(struct ide_atapi_pc *pc)
892 pc->c[0] = READ_POSITION;
896 static int idetape_read_position(ide_drive_t *drive)
898 idetape_tape_t *tape = drive->driver_data;
899 struct ide_atapi_pc pc;
902 debug_log(DBG_PROCS, "Enter %s\n", __func__);
904 idetape_create_read_position_cmd(&pc);
905 if (ide_queue_pc_tail(drive, tape->disk, &pc))
907 position = tape->first_frame;
911 static void idetape_create_locate_cmd(ide_drive_t *drive,
912 struct ide_atapi_pc *pc,
913 unsigned int block, u8 partition, int skip)
916 pc->c[0] = POSITION_TO_ELEMENT;
918 put_unaligned(cpu_to_be32(block), (unsigned int *) &pc->c[3]);
919 pc->c[8] = partition;
920 pc->flags |= PC_FLAG_WAIT_FOR_DSC;
923 static void __ide_tape_discard_merge_buffer(ide_drive_t *drive)
925 idetape_tape_t *tape = drive->driver_data;
927 if (tape->chrdev_dir != IDETAPE_DIR_READ)
930 clear_bit(IDE_AFLAG_FILEMARK, &drive->atapi_flags);
931 tape->merge_bh_size = 0;
932 if (tape->merge_bh != NULL) {
933 ide_tape_kfree_buffer(tape);
934 tape->merge_bh = NULL;
937 tape->chrdev_dir = IDETAPE_DIR_NONE;
941 * Position the tape to the requested block using the LOCATE packet command.
942 * A READ POSITION command is then issued to check where we are positioned. Like
943 * all higher level operations, we queue the commands at the tail of the request
944 * queue and wait for their completion.
946 static int idetape_position_tape(ide_drive_t *drive, unsigned int block,
947 u8 partition, int skip)
949 idetape_tape_t *tape = drive->driver_data;
950 struct gendisk *disk = tape->disk;
952 struct ide_atapi_pc pc;
954 if (tape->chrdev_dir == IDETAPE_DIR_READ)
955 __ide_tape_discard_merge_buffer(drive);
956 idetape_wait_ready(drive, 60 * 5 * HZ);
957 idetape_create_locate_cmd(drive, &pc, block, partition, skip);
958 retval = ide_queue_pc_tail(drive, disk, &pc);
962 idetape_create_read_position_cmd(&pc);
963 return ide_queue_pc_tail(drive, disk, &pc);
966 static void ide_tape_discard_merge_buffer(ide_drive_t *drive,
967 int restore_position)
969 idetape_tape_t *tape = drive->driver_data;
972 __ide_tape_discard_merge_buffer(drive);
973 if (restore_position) {
974 position = idetape_read_position(drive);
975 seek = position > 0 ? position : 0;
976 if (idetape_position_tape(drive, seek, 0, 0)) {
977 printk(KERN_INFO "ide-tape: %s: position_tape failed in"
978 " %s\n", tape->name, __func__);
985 * Generate a read/write request for the block device interface and wait for it
988 static int idetape_queue_rw_tail(ide_drive_t *drive, int cmd, int blocks,
989 struct idetape_bh *bh)
991 idetape_tape_t *tape = drive->driver_data;
992 size_t size = blocks * tape->blk_size;
996 debug_log(DBG_SENSE, "%s: cmd=%d\n", __func__, cmd);
997 BUG_ON(cmd != REQ_IDETAPE_READ && cmd != REQ_IDETAPE_WRITE);
999 rq = blk_get_request(drive->queue, READ, __GFP_WAIT);
1000 rq->cmd_type = REQ_TYPE_SPECIAL;
1002 rq->rq_disk = tape->disk;
1003 rq->special = (void *)bh;
1004 rq->sector = tape->first_frame;
1007 ret = blk_rq_map_kern(drive->queue, rq, bh->b_data, size,
1013 blk_execute_rq(drive->queue, tape->disk, rq, 0);
1015 /* calculate the number of transferred bytes and update bh */
1016 size -= rq->data_len;
1017 if (cmd == REQ_IDETAPE_READ)
1018 atomic_add(size, &bh->b_count);
1021 if (rq->errors == IDE_DRV_ERROR_GENERAL)
1025 idetape_init_merge_buffer(tape);
1027 blk_put_request(rq);
1031 static void idetape_create_inquiry_cmd(struct ide_atapi_pc *pc)
1039 static void idetape_create_rewind_cmd(ide_drive_t *drive,
1040 struct ide_atapi_pc *pc)
1043 pc->c[0] = REZERO_UNIT;
1044 pc->flags |= PC_FLAG_WAIT_FOR_DSC;
1047 static void idetape_create_erase_cmd(struct ide_atapi_pc *pc)
1052 pc->flags |= PC_FLAG_WAIT_FOR_DSC;
1055 static void idetape_create_space_cmd(struct ide_atapi_pc *pc, int count, u8 cmd)
1059 put_unaligned(cpu_to_be32(count), (unsigned int *) &pc->c[1]);
1061 pc->flags |= PC_FLAG_WAIT_FOR_DSC;
1064 /* Queue up a character device originated write request. */
1065 static int idetape_add_chrdev_write_request(ide_drive_t *drive, int blocks)
1067 idetape_tape_t *tape = drive->driver_data;
1069 debug_log(DBG_CHRDEV, "Enter %s\n", __func__);
1071 return idetape_queue_rw_tail(drive, REQ_IDETAPE_WRITE,
1072 blocks, tape->merge_bh);
1075 static void ide_tape_flush_merge_buffer(ide_drive_t *drive)
1077 idetape_tape_t *tape = drive->driver_data;
1079 struct idetape_bh *bh;
1081 if (tape->chrdev_dir != IDETAPE_DIR_WRITE) {
1082 printk(KERN_ERR "ide-tape: bug: Trying to empty merge buffer"
1083 " but we are not writing.\n");
1086 if (tape->merge_bh_size > tape->buffer_size) {
1087 printk(KERN_ERR "ide-tape: bug: merge_buffer too big\n");
1088 tape->merge_bh_size = tape->buffer_size;
1090 if (tape->merge_bh_size) {
1091 blocks = tape->merge_bh_size / tape->blk_size;
1092 if (tape->merge_bh_size % tape->blk_size) {
1093 unsigned int i = tape->blk_size -
1094 tape->merge_bh_size % tape->blk_size;
1098 memset(bh->b_data + atomic_read(&bh->b_count),
1100 atomic_add(i, &bh->b_count);
1102 printk(KERN_INFO "ide-tape: bug, bh NULL\n");
1104 (void) idetape_add_chrdev_write_request(drive, blocks);
1105 tape->merge_bh_size = 0;
1107 if (tape->merge_bh != NULL) {
1108 ide_tape_kfree_buffer(tape);
1109 tape->merge_bh = NULL;
1111 tape->chrdev_dir = IDETAPE_DIR_NONE;
1114 static int idetape_init_read(ide_drive_t *drive)
1116 idetape_tape_t *tape = drive->driver_data;
1119 /* Initialize read operation */
1120 if (tape->chrdev_dir != IDETAPE_DIR_READ) {
1121 if (tape->chrdev_dir == IDETAPE_DIR_WRITE) {
1122 ide_tape_flush_merge_buffer(drive);
1123 idetape_flush_tape_buffers(drive);
1125 if (tape->merge_bh || tape->merge_bh_size) {
1126 printk(KERN_ERR "ide-tape: merge_bh_size should be"
1128 tape->merge_bh_size = 0;
1130 tape->merge_bh = ide_tape_kmalloc_buffer(tape, 0);
1131 if (!tape->merge_bh)
1133 tape->chrdev_dir = IDETAPE_DIR_READ;
1136 * Issue a read 0 command to ensure that DSC handshake is
1137 * switched from completion mode to buffer available mode.
1138 * No point in issuing this if DSC overlap isn't supported, some
1139 * drives (Seagate STT3401A) will return an error.
1141 if (drive->dev_flags & IDE_DFLAG_DSC_OVERLAP) {
1142 bytes_read = idetape_queue_rw_tail(drive,
1143 REQ_IDETAPE_READ, 0,
1145 if (bytes_read < 0) {
1146 ide_tape_kfree_buffer(tape);
1147 tape->merge_bh = NULL;
1148 tape->chrdev_dir = IDETAPE_DIR_NONE;
1157 /* called from idetape_chrdev_read() to service a chrdev read request. */
1158 static int idetape_add_chrdev_read_request(ide_drive_t *drive, int blocks)
1160 idetape_tape_t *tape = drive->driver_data;
1162 debug_log(DBG_PROCS, "Enter %s, %d blocks\n", __func__, blocks);
1164 /* If we are at a filemark, return a read length of 0 */
1165 if (test_bit(IDE_AFLAG_FILEMARK, &drive->atapi_flags))
1168 idetape_init_read(drive);
1170 return idetape_queue_rw_tail(drive, REQ_IDETAPE_READ, blocks,
1174 static void idetape_pad_zeros(ide_drive_t *drive, int bcount)
1176 idetape_tape_t *tape = drive->driver_data;
1177 struct idetape_bh *bh = tape->merge_bh;
1183 count = min(tape->buffer_size, bcount);
1185 blocks = count / tape->blk_size;
1186 atomic_set(&bh->b_count, count);
1187 memset(bh->b_data, 0, atomic_read(&bh->b_count));
1189 idetape_queue_rw_tail(drive, REQ_IDETAPE_WRITE, blocks,
1195 * Rewinds the tape to the Beginning Of the current Partition (BOP). We
1196 * currently support only one partition.
1198 static int idetape_rewind_tape(ide_drive_t *drive)
1200 struct ide_tape_obj *tape = drive->driver_data;
1201 struct gendisk *disk = tape->disk;
1203 struct ide_atapi_pc pc;
1205 debug_log(DBG_SENSE, "Enter %s\n", __func__);
1207 idetape_create_rewind_cmd(drive, &pc);
1208 retval = ide_queue_pc_tail(drive, disk, &pc);
1212 idetape_create_read_position_cmd(&pc);
1213 retval = ide_queue_pc_tail(drive, disk, &pc);
1219 /* mtio.h compatible commands should be issued to the chrdev interface. */
1220 static int idetape_blkdev_ioctl(ide_drive_t *drive, unsigned int cmd,
1223 idetape_tape_t *tape = drive->driver_data;
1224 void __user *argp = (void __user *)arg;
1226 struct idetape_config {
1227 int dsc_rw_frequency;
1228 int dsc_media_access_frequency;
1232 debug_log(DBG_PROCS, "Enter %s\n", __func__);
1236 if (copy_from_user(&config, argp, sizeof(config)))
1238 tape->best_dsc_rw_freq = config.dsc_rw_frequency;
1241 config.dsc_rw_frequency = (int) tape->best_dsc_rw_freq;
1242 config.nr_stages = 1;
1243 if (copy_to_user(argp, &config, sizeof(config)))
1252 static int idetape_space_over_filemarks(ide_drive_t *drive, short mt_op,
1255 idetape_tape_t *tape = drive->driver_data;
1256 struct gendisk *disk = tape->disk;
1257 struct ide_atapi_pc pc;
1258 int retval, count = 0;
1259 int sprev = !!(tape->caps[4] & 0x20);
1263 if (MTBSF == mt_op || MTBSFM == mt_op) {
1266 mt_count = -mt_count;
1269 if (tape->chrdev_dir == IDETAPE_DIR_READ) {
1270 tape->merge_bh_size = 0;
1271 if (test_and_clear_bit(IDE_AFLAG_FILEMARK, &drive->atapi_flags))
1273 ide_tape_discard_merge_buffer(drive, 0);
1279 idetape_create_space_cmd(&pc, mt_count - count,
1280 IDETAPE_SPACE_OVER_FILEMARK);
1281 return ide_queue_pc_tail(drive, disk, &pc);
1286 retval = idetape_space_over_filemarks(drive, MTFSF,
1290 count = (MTBSFM == mt_op ? 1 : -1);
1291 return idetape_space_over_filemarks(drive, MTFSF, count);
1293 printk(KERN_ERR "ide-tape: MTIO operation %d not supported\n",
1300 * Our character device read / write functions.
1302 * The tape is optimized to maximize throughput when it is transferring an
1303 * integral number of the "continuous transfer limit", which is a parameter of
1304 * the specific tape (26kB on my particular tape, 32kB for Onstream).
1306 * As of version 1.3 of the driver, the character device provides an abstract
1307 * continuous view of the media - any mix of block sizes (even 1 byte) on the
1308 * same backup/restore procedure is supported. The driver will internally
1309 * convert the requests to the recommended transfer unit, so that an unmatch
1310 * between the user's block size to the recommended size will only result in a
1311 * (slightly) increased driver overhead, but will no longer hit performance.
1312 * This is not applicable to Onstream.
1314 static ssize_t idetape_chrdev_read(struct file *file, char __user *buf,
1315 size_t count, loff_t *ppos)
1317 struct ide_tape_obj *tape = file->private_data;
1318 ide_drive_t *drive = tape->drive;
1319 ssize_t bytes_read, temp, actually_read = 0, rc;
1321 u16 ctl = *(u16 *)&tape->caps[12];
1323 debug_log(DBG_CHRDEV, "Enter %s, count %Zd\n", __func__, count);
1325 if (tape->chrdev_dir != IDETAPE_DIR_READ) {
1326 if (test_bit(IDE_AFLAG_DETECT_BS, &drive->atapi_flags))
1327 if (count > tape->blk_size &&
1328 (count % tape->blk_size) == 0)
1329 tape->user_bs_factor = count / tape->blk_size;
1331 rc = idetape_init_read(drive);
1336 if (tape->merge_bh_size) {
1337 actually_read = min((unsigned int)(tape->merge_bh_size),
1338 (unsigned int)count);
1339 if (idetape_copy_stage_to_user(tape, buf, actually_read))
1341 buf += actually_read;
1342 tape->merge_bh_size -= actually_read;
1343 count -= actually_read;
1345 while (count >= tape->buffer_size) {
1346 bytes_read = idetape_add_chrdev_read_request(drive, ctl);
1347 if (bytes_read <= 0)
1349 if (idetape_copy_stage_to_user(tape, buf, bytes_read))
1352 count -= bytes_read;
1353 actually_read += bytes_read;
1356 bytes_read = idetape_add_chrdev_read_request(drive, ctl);
1357 if (bytes_read <= 0)
1359 temp = min((unsigned long)count, (unsigned long)bytes_read);
1360 if (idetape_copy_stage_to_user(tape, buf, temp))
1362 actually_read += temp;
1363 tape->merge_bh_size = bytes_read-temp;
1366 if (!actually_read && test_bit(IDE_AFLAG_FILEMARK, &drive->atapi_flags)) {
1367 debug_log(DBG_SENSE, "%s: spacing over filemark\n", tape->name);
1369 idetape_space_over_filemarks(drive, MTFSF, 1);
1373 return ret ? ret : actually_read;
1376 static ssize_t idetape_chrdev_write(struct file *file, const char __user *buf,
1377 size_t count, loff_t *ppos)
1379 struct ide_tape_obj *tape = file->private_data;
1380 ide_drive_t *drive = tape->drive;
1381 ssize_t actually_written = 0;
1383 u16 ctl = *(u16 *)&tape->caps[12];
1385 /* The drive is write protected. */
1386 if (tape->write_prot)
1389 debug_log(DBG_CHRDEV, "Enter %s, count %Zd\n", __func__, count);
1391 /* Initialize write operation */
1392 if (tape->chrdev_dir != IDETAPE_DIR_WRITE) {
1393 if (tape->chrdev_dir == IDETAPE_DIR_READ)
1394 ide_tape_discard_merge_buffer(drive, 1);
1395 if (tape->merge_bh || tape->merge_bh_size) {
1396 printk(KERN_ERR "ide-tape: merge_bh_size "
1397 "should be 0 now\n");
1398 tape->merge_bh_size = 0;
1400 tape->merge_bh = ide_tape_kmalloc_buffer(tape, 0);
1401 if (!tape->merge_bh)
1403 tape->chrdev_dir = IDETAPE_DIR_WRITE;
1404 idetape_init_merge_buffer(tape);
1407 * Issue a write 0 command to ensure that DSC handshake is
1408 * switched from completion mode to buffer available mode. No
1409 * point in issuing this if DSC overlap isn't supported, some
1410 * drives (Seagate STT3401A) will return an error.
1412 if (drive->dev_flags & IDE_DFLAG_DSC_OVERLAP) {
1413 ssize_t retval = idetape_queue_rw_tail(drive,
1414 REQ_IDETAPE_WRITE, 0,
1417 ide_tape_kfree_buffer(tape);
1418 tape->merge_bh = NULL;
1419 tape->chrdev_dir = IDETAPE_DIR_NONE;
1426 if (tape->merge_bh_size) {
1427 if (tape->merge_bh_size >= tape->buffer_size) {
1428 printk(KERN_ERR "ide-tape: bug: merge buf too big\n");
1429 tape->merge_bh_size = 0;
1431 actually_written = min((unsigned int)
1432 (tape->buffer_size - tape->merge_bh_size),
1433 (unsigned int)count);
1434 if (idetape_copy_stage_from_user(tape, buf, actually_written))
1436 buf += actually_written;
1437 tape->merge_bh_size += actually_written;
1438 count -= actually_written;
1440 if (tape->merge_bh_size == tape->buffer_size) {
1442 tape->merge_bh_size = 0;
1443 retval = idetape_add_chrdev_write_request(drive, ctl);
1448 while (count >= tape->buffer_size) {
1450 if (idetape_copy_stage_from_user(tape, buf, tape->buffer_size))
1452 buf += tape->buffer_size;
1453 count -= tape->buffer_size;
1454 retval = idetape_add_chrdev_write_request(drive, ctl);
1455 actually_written += tape->buffer_size;
1460 actually_written += count;
1461 if (idetape_copy_stage_from_user(tape, buf, count))
1463 tape->merge_bh_size += count;
1465 return ret ? ret : actually_written;
1468 static int idetape_write_filemark(ide_drive_t *drive)
1470 struct ide_tape_obj *tape = drive->driver_data;
1471 struct ide_atapi_pc pc;
1473 /* Write a filemark */
1474 idetape_create_write_filemark_cmd(drive, &pc, 1);
1475 if (ide_queue_pc_tail(drive, tape->disk, &pc)) {
1476 printk(KERN_ERR "ide-tape: Couldn't write a filemark\n");
1483 * Called from idetape_chrdev_ioctl when the general mtio MTIOCTOP ioctl is
1486 * Note: MTBSF and MTBSFM are not supported when the tape doesn't support
1487 * spacing over filemarks in the reverse direction. In this case, MTFSFM is also
1488 * usually not supported.
1490 * The following commands are currently not supported:
1492 * MTFSS, MTBSS, MTWSM, MTSETDENSITY, MTSETDRVBUFFER, MT_ST_BOOLEANS,
1493 * MT_ST_WRITE_THRESHOLD.
1495 static int idetape_mtioctop(ide_drive_t *drive, short mt_op, int mt_count)
1497 idetape_tape_t *tape = drive->driver_data;
1498 struct gendisk *disk = tape->disk;
1499 struct ide_atapi_pc pc;
1502 debug_log(DBG_ERR, "Handling MTIOCTOP ioctl: mt_op=%d, mt_count=%d\n",
1512 return idetape_space_over_filemarks(drive, mt_op, mt_count);
1519 if (tape->write_prot)
1521 ide_tape_discard_merge_buffer(drive, 1);
1522 for (i = 0; i < mt_count; i++) {
1523 retval = idetape_write_filemark(drive);
1529 ide_tape_discard_merge_buffer(drive, 0);
1530 if (idetape_rewind_tape(drive))
1534 ide_tape_discard_merge_buffer(drive, 0);
1535 return ide_do_start_stop(drive, disk, IDETAPE_LU_LOAD_MASK);
1539 * If door is locked, attempt to unlock before
1540 * attempting to eject.
1542 if (tape->door_locked) {
1543 if (!ide_set_media_lock(drive, disk, 0))
1544 tape->door_locked = DOOR_UNLOCKED;
1546 ide_tape_discard_merge_buffer(drive, 0);
1547 retval = ide_do_start_stop(drive, disk, !IDETAPE_LU_LOAD_MASK);
1549 clear_bit(IDE_AFLAG_MEDIUM_PRESENT, &drive->atapi_flags);
1552 ide_tape_discard_merge_buffer(drive, 0);
1553 return idetape_flush_tape_buffers(drive);
1555 ide_tape_discard_merge_buffer(drive, 0);
1556 return ide_do_start_stop(drive, disk,
1557 IDETAPE_LU_RETENSION_MASK | IDETAPE_LU_LOAD_MASK);
1559 idetape_create_space_cmd(&pc, 0, IDETAPE_SPACE_TO_EOD);
1560 return ide_queue_pc_tail(drive, disk, &pc);
1562 (void)idetape_rewind_tape(drive);
1563 idetape_create_erase_cmd(&pc);
1564 return ide_queue_pc_tail(drive, disk, &pc);
1567 if (mt_count < tape->blk_size ||
1568 mt_count % tape->blk_size)
1570 tape->user_bs_factor = mt_count / tape->blk_size;
1571 clear_bit(IDE_AFLAG_DETECT_BS, &drive->atapi_flags);
1573 set_bit(IDE_AFLAG_DETECT_BS, &drive->atapi_flags);
1576 ide_tape_discard_merge_buffer(drive, 0);
1577 return idetape_position_tape(drive,
1578 mt_count * tape->user_bs_factor, tape->partition, 0);
1580 ide_tape_discard_merge_buffer(drive, 0);
1581 return idetape_position_tape(drive, 0, mt_count, 0);
1585 retval = ide_set_media_lock(drive, disk, 1);
1588 tape->door_locked = DOOR_EXPLICITLY_LOCKED;
1591 retval = ide_set_media_lock(drive, disk, 0);
1594 tape->door_locked = DOOR_UNLOCKED;
1597 printk(KERN_ERR "ide-tape: MTIO operation %d not supported\n",
1604 * Our character device ioctls. General mtio.h magnetic io commands are
1605 * supported here, and not in the corresponding block interface. Our own
1606 * ide-tape ioctls are supported on both interfaces.
1608 static int idetape_chrdev_ioctl(struct inode *inode, struct file *file,
1609 unsigned int cmd, unsigned long arg)
1611 struct ide_tape_obj *tape = file->private_data;
1612 ide_drive_t *drive = tape->drive;
1616 int block_offset = 0, position = tape->first_frame;
1617 void __user *argp = (void __user *)arg;
1619 debug_log(DBG_CHRDEV, "Enter %s, cmd=%u\n", __func__, cmd);
1621 if (tape->chrdev_dir == IDETAPE_DIR_WRITE) {
1622 ide_tape_flush_merge_buffer(drive);
1623 idetape_flush_tape_buffers(drive);
1625 if (cmd == MTIOCGET || cmd == MTIOCPOS) {
1626 block_offset = tape->merge_bh_size /
1627 (tape->blk_size * tape->user_bs_factor);
1628 position = idetape_read_position(drive);
1634 if (copy_from_user(&mtop, argp, sizeof(struct mtop)))
1636 return idetape_mtioctop(drive, mtop.mt_op, mtop.mt_count);
1638 memset(&mtget, 0, sizeof(struct mtget));
1639 mtget.mt_type = MT_ISSCSI2;
1640 mtget.mt_blkno = position / tape->user_bs_factor - block_offset;
1642 ((tape->blk_size * tape->user_bs_factor)
1643 << MT_ST_BLKSIZE_SHIFT) & MT_ST_BLKSIZE_MASK;
1645 if (tape->drv_write_prot)
1646 mtget.mt_gstat |= GMT_WR_PROT(0xffffffff);
1648 if (copy_to_user(argp, &mtget, sizeof(struct mtget)))
1652 mtpos.mt_blkno = position / tape->user_bs_factor - block_offset;
1653 if (copy_to_user(argp, &mtpos, sizeof(struct mtpos)))
1657 if (tape->chrdev_dir == IDETAPE_DIR_READ)
1658 ide_tape_discard_merge_buffer(drive, 1);
1659 return idetape_blkdev_ioctl(drive, cmd, arg);
1664 * Do a mode sense page 0 with block descriptor and if it succeeds set the tape
1665 * block size with the reported value.
1667 static void ide_tape_get_bsize_from_bdesc(ide_drive_t *drive)
1669 idetape_tape_t *tape = drive->driver_data;
1670 struct ide_atapi_pc pc;
1672 idetape_create_mode_sense_cmd(&pc, IDETAPE_BLOCK_DESCRIPTOR);
1673 if (ide_queue_pc_tail(drive, tape->disk, &pc)) {
1674 printk(KERN_ERR "ide-tape: Can't get block descriptor\n");
1675 if (tape->blk_size == 0) {
1676 printk(KERN_WARNING "ide-tape: Cannot deal with zero "
1677 "block size, assuming 32k\n");
1678 tape->blk_size = 32768;
1682 tape->blk_size = (pc.buf[4 + 5] << 16) +
1683 (pc.buf[4 + 6] << 8) +
1685 tape->drv_write_prot = (pc.buf[2] & 0x80) >> 7;
1688 static int idetape_chrdev_open(struct inode *inode, struct file *filp)
1690 unsigned int minor = iminor(inode), i = minor & ~0xc0;
1692 idetape_tape_t *tape;
1695 if (i >= MAX_HWIFS * MAX_DRIVES)
1699 tape = ide_tape_chrdev_get(i);
1705 debug_log(DBG_CHRDEV, "Enter %s\n", __func__);
1708 * We really want to do nonseekable_open(inode, filp); here, but some
1709 * versions of tar incorrectly call lseek on tapes and bail out if that
1710 * fails. So we disallow pread() and pwrite(), but permit lseeks.
1712 filp->f_mode &= ~(FMODE_PREAD | FMODE_PWRITE);
1714 drive = tape->drive;
1716 filp->private_data = tape;
1718 if (test_and_set_bit(IDE_AFLAG_BUSY, &drive->atapi_flags)) {
1723 retval = idetape_wait_ready(drive, 60 * HZ);
1725 clear_bit(IDE_AFLAG_BUSY, &drive->atapi_flags);
1726 printk(KERN_ERR "ide-tape: %s: drive not ready\n", tape->name);
1730 idetape_read_position(drive);
1731 if (!test_bit(IDE_AFLAG_ADDRESS_VALID, &drive->atapi_flags))
1732 (void)idetape_rewind_tape(drive);
1734 /* Read block size and write protect status from drive. */
1735 ide_tape_get_bsize_from_bdesc(drive);
1737 /* Set write protect flag if device is opened as read-only. */
1738 if ((filp->f_flags & O_ACCMODE) == O_RDONLY)
1739 tape->write_prot = 1;
1741 tape->write_prot = tape->drv_write_prot;
1743 /* Make sure drive isn't write protected if user wants to write. */
1744 if (tape->write_prot) {
1745 if ((filp->f_flags & O_ACCMODE) == O_WRONLY ||
1746 (filp->f_flags & O_ACCMODE) == O_RDWR) {
1747 clear_bit(IDE_AFLAG_BUSY, &drive->atapi_flags);
1753 /* Lock the tape drive door so user can't eject. */
1754 if (tape->chrdev_dir == IDETAPE_DIR_NONE) {
1755 if (!ide_set_media_lock(drive, tape->disk, 1)) {
1756 if (tape->door_locked != DOOR_EXPLICITLY_LOCKED)
1757 tape->door_locked = DOOR_LOCKED;
1769 static void idetape_write_release(ide_drive_t *drive, unsigned int minor)
1771 idetape_tape_t *tape = drive->driver_data;
1773 ide_tape_flush_merge_buffer(drive);
1774 tape->merge_bh = ide_tape_kmalloc_buffer(tape, 1);
1775 if (tape->merge_bh != NULL) {
1776 idetape_pad_zeros(drive, tape->blk_size *
1777 (tape->user_bs_factor - 1));
1778 ide_tape_kfree_buffer(tape);
1779 tape->merge_bh = NULL;
1781 idetape_write_filemark(drive);
1782 idetape_flush_tape_buffers(drive);
1783 idetape_flush_tape_buffers(drive);
1786 static int idetape_chrdev_release(struct inode *inode, struct file *filp)
1788 struct ide_tape_obj *tape = filp->private_data;
1789 ide_drive_t *drive = tape->drive;
1790 unsigned int minor = iminor(inode);
1793 tape = drive->driver_data;
1795 debug_log(DBG_CHRDEV, "Enter %s\n", __func__);
1797 if (tape->chrdev_dir == IDETAPE_DIR_WRITE)
1798 idetape_write_release(drive, minor);
1799 if (tape->chrdev_dir == IDETAPE_DIR_READ) {
1801 ide_tape_discard_merge_buffer(drive, 1);
1804 if (minor < 128 && test_bit(IDE_AFLAG_MEDIUM_PRESENT, &drive->atapi_flags))
1805 (void) idetape_rewind_tape(drive);
1806 if (tape->chrdev_dir == IDETAPE_DIR_NONE) {
1807 if (tape->door_locked == DOOR_LOCKED) {
1808 if (!ide_set_media_lock(drive, tape->disk, 0))
1809 tape->door_locked = DOOR_UNLOCKED;
1812 clear_bit(IDE_AFLAG_BUSY, &drive->atapi_flags);
1818 static void idetape_get_inquiry_results(ide_drive_t *drive)
1820 idetape_tape_t *tape = drive->driver_data;
1821 struct ide_atapi_pc pc;
1823 char fw_rev[4], vendor_id[8], product_id[16];
1825 idetape_create_inquiry_cmd(&pc);
1826 pc.buf = &pc_buf[0];
1827 pc.buf_size = sizeof(pc_buf);
1829 if (ide_queue_pc_tail(drive, tape->disk, &pc)) {
1830 printk(KERN_ERR "ide-tape: %s: can't get INQUIRY results\n",
1834 memcpy(vendor_id, &pc.buf[8], 8);
1835 memcpy(product_id, &pc.buf[16], 16);
1836 memcpy(fw_rev, &pc.buf[32], 4);
1838 ide_fixstring(vendor_id, 8, 0);
1839 ide_fixstring(product_id, 16, 0);
1840 ide_fixstring(fw_rev, 4, 0);
1842 printk(KERN_INFO "ide-tape: %s <-> %s: %.8s %.16s rev %.4s\n",
1843 drive->name, tape->name, vendor_id, product_id, fw_rev);
1847 * Ask the tape about its various parameters. In particular, we will adjust our
1848 * data transfer buffer size to the recommended value as returned by the tape.
1850 static void idetape_get_mode_sense_results(ide_drive_t *drive)
1852 idetape_tape_t *tape = drive->driver_data;
1853 struct ide_atapi_pc pc;
1855 u8 speed, max_speed;
1857 idetape_create_mode_sense_cmd(&pc, IDETAPE_CAPABILITIES_PAGE);
1858 if (ide_queue_pc_tail(drive, tape->disk, &pc)) {
1859 printk(KERN_ERR "ide-tape: Can't get tape parameters - assuming"
1860 " some default values\n");
1861 tape->blk_size = 512;
1862 put_unaligned(52, (u16 *)&tape->caps[12]);
1863 put_unaligned(540, (u16 *)&tape->caps[14]);
1864 put_unaligned(6*52, (u16 *)&tape->caps[16]);
1867 caps = pc.buf + 4 + pc.buf[3];
1869 /* convert to host order and save for later use */
1870 speed = be16_to_cpup((__be16 *)&caps[14]);
1871 max_speed = be16_to_cpup((__be16 *)&caps[8]);
1873 *(u16 *)&caps[8] = max_speed;
1874 *(u16 *)&caps[12] = be16_to_cpup((__be16 *)&caps[12]);
1875 *(u16 *)&caps[14] = speed;
1876 *(u16 *)&caps[16] = be16_to_cpup((__be16 *)&caps[16]);
1879 printk(KERN_INFO "ide-tape: %s: invalid tape speed "
1880 "(assuming 650KB/sec)\n", drive->name);
1881 *(u16 *)&caps[14] = 650;
1884 printk(KERN_INFO "ide-tape: %s: invalid max_speed "
1885 "(assuming 650KB/sec)\n", drive->name);
1886 *(u16 *)&caps[8] = 650;
1889 memcpy(&tape->caps, caps, 20);
1891 /* device lacks locking support according to capabilities page */
1892 if ((caps[6] & 1) == 0)
1893 drive->dev_flags &= ~IDE_DFLAG_DOORLOCKING;
1896 tape->blk_size = 512;
1897 else if (caps[7] & 0x04)
1898 tape->blk_size = 1024;
1901 #ifdef CONFIG_IDE_PROC_FS
1902 #define ide_tape_devset_get(name, field) \
1903 static int get_##name(ide_drive_t *drive) \
1905 idetape_tape_t *tape = drive->driver_data; \
1906 return tape->field; \
1909 #define ide_tape_devset_set(name, field) \
1910 static int set_##name(ide_drive_t *drive, int arg) \
1912 idetape_tape_t *tape = drive->driver_data; \
1913 tape->field = arg; \
1917 #define ide_tape_devset_rw_field(_name, _field) \
1918 ide_tape_devset_get(_name, _field) \
1919 ide_tape_devset_set(_name, _field) \
1920 IDE_DEVSET(_name, DS_SYNC, get_##_name, set_##_name)
1922 #define ide_tape_devset_r_field(_name, _field) \
1923 ide_tape_devset_get(_name, _field) \
1924 IDE_DEVSET(_name, 0, get_##_name, NULL)
1926 static int mulf_tdsc(ide_drive_t *drive) { return 1000; }
1927 static int divf_tdsc(ide_drive_t *drive) { return HZ; }
1928 static int divf_buffer(ide_drive_t *drive) { return 2; }
1929 static int divf_buffer_size(ide_drive_t *drive) { return 1024; }
1931 ide_devset_rw_flag(dsc_overlap, IDE_DFLAG_DSC_OVERLAP);
1933 ide_tape_devset_rw_field(debug_mask, debug_mask);
1934 ide_tape_devset_rw_field(tdsc, best_dsc_rw_freq);
1936 ide_tape_devset_r_field(avg_speed, avg_speed);
1937 ide_tape_devset_r_field(speed, caps[14]);
1938 ide_tape_devset_r_field(buffer, caps[16]);
1939 ide_tape_devset_r_field(buffer_size, buffer_size);
1941 static const struct ide_proc_devset idetape_settings[] = {
1942 __IDE_PROC_DEVSET(avg_speed, 0, 0xffff, NULL, NULL),
1943 __IDE_PROC_DEVSET(buffer, 0, 0xffff, NULL, divf_buffer),
1944 __IDE_PROC_DEVSET(buffer_size, 0, 0xffff, NULL, divf_buffer_size),
1945 __IDE_PROC_DEVSET(debug_mask, 0, 0xffff, NULL, NULL),
1946 __IDE_PROC_DEVSET(dsc_overlap, 0, 1, NULL, NULL),
1947 __IDE_PROC_DEVSET(speed, 0, 0xffff, NULL, NULL),
1948 __IDE_PROC_DEVSET(tdsc, IDETAPE_DSC_RW_MIN, IDETAPE_DSC_RW_MAX,
1949 mulf_tdsc, divf_tdsc),
1955 * The function below is called to:
1957 * 1. Initialize our various state variables.
1958 * 2. Ask the tape for its capabilities.
1959 * 3. Allocate a buffer which will be used for data transfer. The buffer size
1960 * is chosen based on the recommendation which we received in step 2.
1962 * Note that at this point ide.c already assigned us an irq, so that we can
1963 * queue requests here and wait for their completion.
1965 static void idetape_setup(ide_drive_t *drive, idetape_tape_t *tape, int minor)
1970 u16 *ctl = (u16 *)&tape->caps[12];
1972 drive->pc_callback = ide_tape_callback;
1974 drive->dev_flags |= IDE_DFLAG_DSC_OVERLAP;
1976 if (drive->hwif->host_flags & IDE_HFLAG_NO_DSC) {
1977 printk(KERN_INFO "ide-tape: %s: disabling DSC overlap\n",
1979 drive->dev_flags &= ~IDE_DFLAG_DSC_OVERLAP;
1982 /* Seagate Travan drives do not support DSC overlap. */
1983 if (strstr((char *)&drive->id[ATA_ID_PROD], "Seagate STT3401"))
1984 drive->dev_flags &= ~IDE_DFLAG_DSC_OVERLAP;
1986 tape->minor = minor;
1987 tape->name[0] = 'h';
1988 tape->name[1] = 't';
1989 tape->name[2] = '0' + minor;
1990 tape->chrdev_dir = IDETAPE_DIR_NONE;
1992 idetape_get_inquiry_results(drive);
1993 idetape_get_mode_sense_results(drive);
1994 ide_tape_get_bsize_from_bdesc(drive);
1995 tape->user_bs_factor = 1;
1996 tape->buffer_size = *ctl * tape->blk_size;
1997 while (tape->buffer_size > 0xffff) {
1998 printk(KERN_NOTICE "ide-tape: decreasing stage size\n");
2000 tape->buffer_size = *ctl * tape->blk_size;
2002 buffer_size = tape->buffer_size;
2004 /* select the "best" DSC read/write polling freq */
2005 speed = max(*(u16 *)&tape->caps[14], *(u16 *)&tape->caps[8]);
2007 t = (IDETAPE_FIFO_THRESHOLD * tape->buffer_size * HZ) / (speed * 1000);
2010 * Ensure that the number we got makes sense; limit it within
2011 * IDETAPE_DSC_RW_MIN and IDETAPE_DSC_RW_MAX.
2013 tape->best_dsc_rw_freq = clamp_t(unsigned long, t, IDETAPE_DSC_RW_MIN,
2014 IDETAPE_DSC_RW_MAX);
2015 printk(KERN_INFO "ide-tape: %s <-> %s: %dKBps, %d*%dkB buffer, "
2017 drive->name, tape->name, *(u16 *)&tape->caps[14],
2018 (*(u16 *)&tape->caps[16] * 512) / tape->buffer_size,
2019 tape->buffer_size / 1024,
2020 tape->best_dsc_rw_freq * 1000 / HZ,
2021 (drive->dev_flags & IDE_DFLAG_USING_DMA) ? ", DMA" : "");
2023 ide_proc_register_driver(drive, tape->driver);
2026 static void ide_tape_remove(ide_drive_t *drive)
2028 idetape_tape_t *tape = drive->driver_data;
2030 ide_proc_unregister_driver(drive, tape->driver);
2031 device_del(&tape->dev);
2032 ide_unregister_region(tape->disk);
2034 mutex_lock(&idetape_ref_mutex);
2035 put_device(&tape->dev);
2036 mutex_unlock(&idetape_ref_mutex);
2039 static void ide_tape_release(struct device *dev)
2041 struct ide_tape_obj *tape = to_ide_drv(dev, ide_tape_obj);
2042 ide_drive_t *drive = tape->drive;
2043 struct gendisk *g = tape->disk;
2045 BUG_ON(tape->merge_bh_size);
2047 drive->dev_flags &= ~IDE_DFLAG_DSC_OVERLAP;
2048 drive->driver_data = NULL;
2049 device_destroy(idetape_sysfs_class, MKDEV(IDETAPE_MAJOR, tape->minor));
2050 device_destroy(idetape_sysfs_class,
2051 MKDEV(IDETAPE_MAJOR, tape->minor + 128));
2052 idetape_devs[tape->minor] = NULL;
2053 g->private_data = NULL;
2058 #ifdef CONFIG_IDE_PROC_FS
2059 static int proc_idetape_read_name
2060 (char *page, char **start, off_t off, int count, int *eof, void *data)
2062 ide_drive_t *drive = (ide_drive_t *) data;
2063 idetape_tape_t *tape = drive->driver_data;
2067 len = sprintf(out, "%s\n", tape->name);
2068 PROC_IDE_READ_RETURN(page, start, off, count, eof, len);
2071 static ide_proc_entry_t idetape_proc[] = {
2072 { "capacity", S_IFREG|S_IRUGO, proc_ide_read_capacity, NULL },
2073 { "name", S_IFREG|S_IRUGO, proc_idetape_read_name, NULL },
2074 { NULL, 0, NULL, NULL }
2077 static ide_proc_entry_t *ide_tape_proc_entries(ide_drive_t *drive)
2079 return idetape_proc;
2082 static const struct ide_proc_devset *ide_tape_proc_devsets(ide_drive_t *drive)
2084 return idetape_settings;
2088 static int ide_tape_probe(ide_drive_t *);
2090 static struct ide_driver idetape_driver = {
2092 .owner = THIS_MODULE,
2094 .bus = &ide_bus_type,
2096 .probe = ide_tape_probe,
2097 .remove = ide_tape_remove,
2098 .version = IDETAPE_VERSION,
2099 .do_request = idetape_do_request,
2100 #ifdef CONFIG_IDE_PROC_FS
2101 .proc_entries = ide_tape_proc_entries,
2102 .proc_devsets = ide_tape_proc_devsets,
2106 /* Our character device supporting functions, passed to register_chrdev. */
2107 static const struct file_operations idetape_fops = {
2108 .owner = THIS_MODULE,
2109 .read = idetape_chrdev_read,
2110 .write = idetape_chrdev_write,
2111 .ioctl = idetape_chrdev_ioctl,
2112 .open = idetape_chrdev_open,
2113 .release = idetape_chrdev_release,
2116 static int idetape_open(struct block_device *bdev, fmode_t mode)
2118 struct ide_tape_obj *tape = ide_tape_get(bdev->bd_disk);
2126 static int idetape_release(struct gendisk *disk, fmode_t mode)
2128 struct ide_tape_obj *tape = ide_drv_g(disk, ide_tape_obj);
2134 static int idetape_ioctl(struct block_device *bdev, fmode_t mode,
2135 unsigned int cmd, unsigned long arg)
2137 struct ide_tape_obj *tape = ide_drv_g(bdev->bd_disk, ide_tape_obj);
2138 ide_drive_t *drive = tape->drive;
2139 int err = generic_ide_ioctl(drive, bdev, cmd, arg);
2141 err = idetape_blkdev_ioctl(drive, cmd, arg);
2145 static struct block_device_operations idetape_block_ops = {
2146 .owner = THIS_MODULE,
2147 .open = idetape_open,
2148 .release = idetape_release,
2149 .locked_ioctl = idetape_ioctl,
2152 static int ide_tape_probe(ide_drive_t *drive)
2154 idetape_tape_t *tape;
2158 if (!strstr("ide-tape", drive->driver_req))
2161 if (drive->media != ide_tape)
2164 if ((drive->dev_flags & IDE_DFLAG_ID_READ) &&
2165 ide_check_atapi_device(drive, DRV_NAME) == 0) {
2166 printk(KERN_ERR "ide-tape: %s: not supported by this version of"
2167 " the driver\n", drive->name);
2170 tape = kzalloc(sizeof(idetape_tape_t), GFP_KERNEL);
2172 printk(KERN_ERR "ide-tape: %s: Can't allocate a tape struct\n",
2177 g = alloc_disk(1 << PARTN_BITS);
2181 ide_init_disk(g, drive);
2183 tape->dev.parent = &drive->gendev;
2184 tape->dev.release = ide_tape_release;
2185 dev_set_name(&tape->dev, dev_name(&drive->gendev));
2187 if (device_register(&tape->dev))
2190 tape->drive = drive;
2191 tape->driver = &idetape_driver;
2194 g->private_data = &tape->driver;
2196 drive->driver_data = tape;
2198 mutex_lock(&idetape_ref_mutex);
2199 for (minor = 0; idetape_devs[minor]; minor++)
2201 idetape_devs[minor] = tape;
2202 mutex_unlock(&idetape_ref_mutex);
2204 idetape_setup(drive, tape, minor);
2206 device_create(idetape_sysfs_class, &drive->gendev,
2207 MKDEV(IDETAPE_MAJOR, minor), NULL, "%s", tape->name);
2208 device_create(idetape_sysfs_class, &drive->gendev,
2209 MKDEV(IDETAPE_MAJOR, minor + 128), NULL,
2212 g->fops = &idetape_block_ops;
2213 ide_register_region(g);
2225 static void __exit idetape_exit(void)
2227 driver_unregister(&idetape_driver.gen_driver);
2228 class_destroy(idetape_sysfs_class);
2229 unregister_chrdev(IDETAPE_MAJOR, "ht");
2232 static int __init idetape_init(void)
2235 idetape_sysfs_class = class_create(THIS_MODULE, "ide_tape");
2236 if (IS_ERR(idetape_sysfs_class)) {
2237 idetape_sysfs_class = NULL;
2238 printk(KERN_ERR "Unable to create sysfs class for ide tapes\n");
2243 if (register_chrdev(IDETAPE_MAJOR, "ht", &idetape_fops)) {
2244 printk(KERN_ERR "ide-tape: Failed to register chrdev"
2247 goto out_free_class;
2250 error = driver_register(&idetape_driver.gen_driver);
2252 goto out_free_driver;
2257 driver_unregister(&idetape_driver.gen_driver);
2259 class_destroy(idetape_sysfs_class);
2264 MODULE_ALIAS("ide:*m-tape*");
2265 module_init(idetape_init);
2266 module_exit(idetape_exit);
2267 MODULE_ALIAS_CHARDEV_MAJOR(IDETAPE_MAJOR);
2268 MODULE_DESCRIPTION("ATAPI Streaming TAPE Driver");
2269 MODULE_LICENSE("GPL");