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 IDETAPE_VERSION "1.20"
20 #include <linux/module.h>
21 #include <linux/types.h>
22 #include <linux/string.h>
23 #include <linux/kernel.h>
24 #include <linux/delay.h>
25 #include <linux/timer.h>
27 #include <linux/interrupt.h>
28 #include <linux/jiffies.h>
29 #include <linux/major.h>
30 #include <linux/errno.h>
31 #include <linux/genhd.h>
32 #include <linux/slab.h>
33 #include <linux/pci.h>
34 #include <linux/ide.h>
35 #include <linux/smp_lock.h>
36 #include <linux/completion.h>
37 #include <linux/bitops.h>
38 #include <linux/mutex.h>
39 #include <scsi/scsi.h>
41 #include <asm/byteorder.h>
42 #include <linux/irq.h>
43 #include <linux/uaccess.h>
45 #include <asm/unaligned.h>
46 #include <linux/mtio.h>
49 /* output errors only */
51 /* output all sense key/asc */
53 /* info regarding all chrdev-related procedures */
54 DBG_CHRDEV = (1 << 2),
55 /* all remaining procedures */
57 /* buffer alloc info (pc_stack & rq_stack) */
58 DBG_PCRQ_STACK = (1 << 4),
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 * With each packet command, we allocate a buffer of IDETAPE_PC_BUFFER_SIZE
85 * bytes. This is used for several packet commands (Not for READ/WRITE commands)
87 #define IDETAPE_PC_BUFFER_SIZE 256
90 * In various places in the driver, we need to allocate storage
91 * for packet commands and requests, which will remain valid while
92 * we leave the driver to wait for an interrupt or a timeout event.
94 #define IDETAPE_PC_STACK (10 + IDETAPE_MAX_PC_RETRIES)
97 * Some drives (for example, Seagate STT3401A Travan) require a very long
98 * timeout, because they don't return an interrupt or clear their busy bit
99 * until after the command completes (even retension commands).
101 #define IDETAPE_WAIT_CMD (900*HZ)
104 * The following parameter is used to select the point in the internal tape fifo
105 * in which we will start to refill the buffer. Decreasing the following
106 * parameter will improve the system's latency and interactive response, while
107 * using a high value might improve system throughput.
109 #define IDETAPE_FIFO_THRESHOLD 2
112 * DSC polling parameters.
114 * Polling for DSC (a single bit in the status register) is a very important
115 * function in ide-tape. There are two cases in which we poll for DSC:
117 * 1. Before a read/write packet command, to ensure that we can transfer data
118 * from/to the tape's data buffers, without causing an actual media access.
119 * In case the tape is not ready yet, we take out our request from the device
120 * request queue, so that ide.c could service requests from the other device
121 * on the same interface in the meantime.
123 * 2. After the successful initialization of a "media access packet command",
124 * which is a command that can take a long time to complete (the interval can
125 * range from several seconds to even an hour). Again, we postpone our request
126 * in the middle to free the bus for the other device. The polling frequency
127 * here should be lower than the read/write frequency since those media access
128 * commands are slow. We start from a "fast" frequency - IDETAPE_DSC_MA_FAST
129 * (1 second), and if we don't receive DSC after IDETAPE_DSC_MA_THRESHOLD
130 * (5 min), we switch it to a lower frequency - IDETAPE_DSC_MA_SLOW (1 min).
132 * We also set a timeout for the timer, in case something goes wrong. The
133 * timeout should be longer then the maximum execution time of a tape operation.
137 #define IDETAPE_DSC_RW_MIN 5*HZ/100 /* 50 msec */
138 #define IDETAPE_DSC_RW_MAX 40*HZ/100 /* 400 msec */
139 #define IDETAPE_DSC_RW_TIMEOUT 2*60*HZ /* 2 minutes */
140 #define IDETAPE_DSC_MA_FAST 2*HZ /* 2 seconds */
141 #define IDETAPE_DSC_MA_THRESHOLD 5*60*HZ /* 5 minutes */
142 #define IDETAPE_DSC_MA_SLOW 30*HZ /* 30 seconds */
143 #define IDETAPE_DSC_MA_TIMEOUT 2*60*60*HZ /* 2 hours */
145 /*************************** End of tunable parameters ***********************/
147 /* tape directions */
149 IDETAPE_DIR_NONE = (1 << 0),
150 IDETAPE_DIR_READ = (1 << 1),
151 IDETAPE_DIR_WRITE = (1 << 2),
157 struct idetape_bh *b_reqnext;
161 /* Tape door status */
162 #define DOOR_UNLOCKED 0
163 #define DOOR_LOCKED 1
164 #define DOOR_EXPLICITLY_LOCKED 2
166 /* Some defines for the SPACE command */
167 #define IDETAPE_SPACE_OVER_FILEMARK 1
168 #define IDETAPE_SPACE_TO_EOD 3
170 /* Some defines for the LOAD UNLOAD command */
171 #define IDETAPE_LU_LOAD_MASK 1
172 #define IDETAPE_LU_RETENSION_MASK 2
173 #define IDETAPE_LU_EOT_MASK 4
176 * Special requests for our block device strategy routine.
178 * In order to service a character device command, we add special requests to
179 * the tail of our block device request queue and wait for their completion.
183 REQ_IDETAPE_PC1 = (1 << 0), /* packet command (first stage) */
184 REQ_IDETAPE_PC2 = (1 << 1), /* packet command (second stage) */
185 REQ_IDETAPE_READ = (1 << 2),
186 REQ_IDETAPE_WRITE = (1 << 3),
189 /* Error codes returned in rq->errors to the higher part of the driver. */
190 #define IDETAPE_ERROR_GENERAL 101
191 #define IDETAPE_ERROR_FILEMARK 102
192 #define IDETAPE_ERROR_EOD 103
194 /* Structures related to the SELECT SENSE / MODE SENSE packet commands. */
195 #define IDETAPE_BLOCK_DESCRIPTOR 0
196 #define IDETAPE_CAPABILITIES_PAGE 0x2a
199 * Most of our global data which we need to save even as we leave the driver due
200 * to an interrupt or a timer event is stored in the struct defined below.
202 typedef struct ide_tape_obj {
204 ide_driver_t *driver;
205 struct gendisk *disk;
209 * Since a typical character device operation requires more
210 * than one packet command, we provide here enough memory
211 * for the maximum of interconnected packet commands.
212 * The packet commands are stored in the circular array pc_stack.
213 * pc_stack_index points to the last used entry, and warps around
214 * to the start when we get to the last array entry.
216 * pc points to the current processed packet command.
218 * failed_pc points to the last failed packet command, or contains
219 * NULL if we do not need to retry any packet command. This is
220 * required since an additional packet command is needed before the
221 * retry, to get detailed information on what went wrong.
223 /* Current packet command */
224 struct ide_atapi_pc *pc;
225 /* Last failed packet command */
226 struct ide_atapi_pc *failed_pc;
227 /* Packet command stack */
228 struct ide_atapi_pc pc_stack[IDETAPE_PC_STACK];
229 /* Next free packet command storage space */
231 struct request rq_stack[IDETAPE_PC_STACK];
232 /* We implement a circular array */
236 * DSC polling variables.
238 * While polling for DSC we use postponed_rq to postpone the current
239 * request so that ide.c will be able to service pending requests on the
240 * other device. Note that at most we will have only one DSC (usually
241 * data transfer) request in the device request queue.
243 struct request *postponed_rq;
244 /* The time in which we started polling for DSC */
245 unsigned long dsc_polling_start;
246 /* Timer used to poll for dsc */
247 struct timer_list dsc_timer;
248 /* Read/Write dsc polling frequency */
249 unsigned long best_dsc_rw_freq;
250 unsigned long dsc_poll_freq;
251 unsigned long dsc_timeout;
253 /* Read position information */
256 unsigned int first_frame;
258 /* Last error information */
259 u8 sense_key, asc, ascq;
261 /* Character device operation */
265 /* Current character device data transfer direction */
268 /* tape block size, usually 512 or 1024 bytes */
269 unsigned short blk_size;
272 /* Copy of the tape's Capabilities and Mechanical Page */
276 * Active data transfer request parameters.
278 * At most, there is only one ide-tape originated data transfer request
279 * in the device request queue. This allows ide.c to easily service
280 * requests from the other device when we postpone our active request.
283 /* Data buffer size chosen based on the tape's recommendation */
286 struct idetape_bh *merge_bh;
287 /* size of the merge buffer */
289 /* pointer to current buffer head within the merge buffer */
290 struct idetape_bh *bh;
294 int pages_per_buffer;
295 /* Wasted space in each stage */
298 /* protects the ide-tape queue */
301 /* Measures average tape speed */
302 unsigned long avg_time;
306 /* the door is currently locked */
308 /* the tape hardware is write protected */
310 /* the tape is write protected (hardware or opened as read-only) */
316 static DEFINE_MUTEX(idetape_ref_mutex);
318 static struct class *idetape_sysfs_class;
320 #define to_ide_tape(obj) container_of(obj, struct ide_tape_obj, kref)
322 #define ide_tape_g(disk) \
323 container_of((disk)->private_data, struct ide_tape_obj, driver)
325 static struct ide_tape_obj *ide_tape_get(struct gendisk *disk)
327 struct ide_tape_obj *tape = NULL;
329 mutex_lock(&idetape_ref_mutex);
330 tape = ide_tape_g(disk);
332 kref_get(&tape->kref);
333 mutex_unlock(&idetape_ref_mutex);
337 static void ide_tape_release(struct kref *);
339 static void ide_tape_put(struct ide_tape_obj *tape)
341 mutex_lock(&idetape_ref_mutex);
342 kref_put(&tape->kref, ide_tape_release);
343 mutex_unlock(&idetape_ref_mutex);
347 * The variables below are used for the character device interface. Additional
348 * state variables are defined in our ide_drive_t structure.
350 static struct ide_tape_obj *idetape_devs[MAX_HWIFS * MAX_DRIVES];
352 #define ide_tape_f(file) ((file)->private_data)
354 static struct ide_tape_obj *ide_tape_chrdev_get(unsigned int i)
356 struct ide_tape_obj *tape = NULL;
358 mutex_lock(&idetape_ref_mutex);
359 tape = idetape_devs[i];
361 kref_get(&tape->kref);
362 mutex_unlock(&idetape_ref_mutex);
366 static void idetape_input_buffers(ide_drive_t *drive, struct ide_atapi_pc *pc,
369 struct idetape_bh *bh = pc->bh;
374 printk(KERN_ERR "ide-tape: bh == NULL in "
375 "idetape_input_buffers\n");
376 ide_pad_transfer(drive, 0, bcount);
380 (unsigned int)(bh->b_size - atomic_read(&bh->b_count)),
382 drive->hwif->tp_ops->input_data(drive, NULL, bh->b_data +
383 atomic_read(&bh->b_count), count);
385 atomic_add(count, &bh->b_count);
386 if (atomic_read(&bh->b_count) == bh->b_size) {
389 atomic_set(&bh->b_count, 0);
395 static void idetape_output_buffers(ide_drive_t *drive, struct ide_atapi_pc *pc,
398 struct idetape_bh *bh = pc->bh;
403 printk(KERN_ERR "ide-tape: bh == NULL in %s\n",
407 count = min((unsigned int)pc->b_count, (unsigned int)bcount);
408 drive->hwif->tp_ops->output_data(drive, NULL, pc->b_data, count);
411 pc->b_count -= count;
416 pc->b_data = bh->b_data;
417 pc->b_count = atomic_read(&bh->b_count);
423 static void idetape_update_buffers(ide_drive_t *drive, struct ide_atapi_pc *pc)
425 struct idetape_bh *bh = pc->bh;
427 unsigned int bcount = pc->xferred;
429 if (pc->flags & PC_FLAG_WRITING)
433 printk(KERN_ERR "ide-tape: bh == NULL in %s\n",
437 count = min((unsigned int)bh->b_size, (unsigned int)bcount);
438 atomic_set(&bh->b_count, count);
439 if (atomic_read(&bh->b_count) == bh->b_size)
447 * idetape_next_pc_storage returns a pointer to a place in which we can
448 * safely store a packet command, even though we intend to leave the
449 * driver. A storage space for a maximum of IDETAPE_PC_STACK packet
450 * commands is allocated at initialization time.
452 static struct ide_atapi_pc *idetape_next_pc_storage(ide_drive_t *drive)
454 idetape_tape_t *tape = drive->driver_data;
456 debug_log(DBG_PCRQ_STACK, "pc_stack_index=%d\n", tape->pc_stack_index);
458 if (tape->pc_stack_index == IDETAPE_PC_STACK)
459 tape->pc_stack_index = 0;
460 return (&tape->pc_stack[tape->pc_stack_index++]);
464 * idetape_next_rq_storage is used along with idetape_next_pc_storage.
465 * Since we queue packet commands in the request queue, we need to
466 * allocate a request, along with the allocation of a packet command.
469 /**************************************************************
471 * This should get fixed to use kmalloc(.., GFP_ATOMIC) *
472 * followed later on by kfree(). -ml *
474 **************************************************************/
476 static struct request *idetape_next_rq_storage(ide_drive_t *drive)
478 idetape_tape_t *tape = drive->driver_data;
480 debug_log(DBG_PCRQ_STACK, "rq_stack_index=%d\n", tape->rq_stack_index);
482 if (tape->rq_stack_index == IDETAPE_PC_STACK)
483 tape->rq_stack_index = 0;
484 return (&tape->rq_stack[tape->rq_stack_index++]);
488 * called on each failed packet command retry to analyze the request sense. We
489 * currently do not utilize this information.
491 static void idetape_analyze_error(ide_drive_t *drive, u8 *sense)
493 idetape_tape_t *tape = drive->driver_data;
494 struct ide_atapi_pc *pc = tape->failed_pc;
496 tape->sense_key = sense[2] & 0xF;
497 tape->asc = sense[12];
498 tape->ascq = sense[13];
500 debug_log(DBG_ERR, "pc = %x, sense key = %x, asc = %x, ascq = %x\n",
501 pc->c[0], tape->sense_key, tape->asc, tape->ascq);
503 /* Correct pc->xferred by asking the tape. */
504 if (pc->flags & PC_FLAG_DMA_ERROR) {
505 pc->xferred = pc->req_xfer -
507 get_unaligned_be32(&sense[3]);
508 idetape_update_buffers(drive, pc);
512 * If error was the result of a zero-length read or write command,
513 * with sense key=5, asc=0x22, ascq=0, let it slide. Some drives
514 * (i.e. Seagate STT3401A Travan) don't support 0-length read/writes.
516 if ((pc->c[0] == READ_6 || pc->c[0] == WRITE_6)
518 && pc->c[4] == 0 && pc->c[3] == 0 && pc->c[2] == 0) {
519 if (tape->sense_key == 5) {
520 /* don't report an error, everything's ok */
522 /* don't retry read/write */
523 pc->flags |= PC_FLAG_ABORT;
526 if (pc->c[0] == READ_6 && (sense[2] & 0x80)) {
527 pc->error = IDETAPE_ERROR_FILEMARK;
528 pc->flags |= PC_FLAG_ABORT;
530 if (pc->c[0] == WRITE_6) {
531 if ((sense[2] & 0x40) || (tape->sense_key == 0xd
532 && tape->asc == 0x0 && tape->ascq == 0x2)) {
533 pc->error = IDETAPE_ERROR_EOD;
534 pc->flags |= PC_FLAG_ABORT;
537 if (pc->c[0] == READ_6 || pc->c[0] == WRITE_6) {
538 if (tape->sense_key == 8) {
539 pc->error = IDETAPE_ERROR_EOD;
540 pc->flags |= PC_FLAG_ABORT;
542 if (!(pc->flags & PC_FLAG_ABORT) &&
544 pc->retries = IDETAPE_MAX_PC_RETRIES + 1;
548 /* Free data buffers completely. */
549 static void ide_tape_kfree_buffer(idetape_tape_t *tape)
551 struct idetape_bh *prev_bh, *bh = tape->merge_bh;
554 u32 size = bh->b_size;
557 unsigned int order = fls(size >> PAGE_SHIFT)-1;
560 free_pages((unsigned long)bh->b_data, order);
563 bh->b_data += (1 << order) * PAGE_SIZE;
571 static int idetape_end_request(ide_drive_t *drive, int uptodate, int nr_sects)
573 struct request *rq = HWGROUP(drive)->rq;
574 idetape_tape_t *tape = drive->driver_data;
578 debug_log(DBG_PROCS, "Enter %s\n", __func__);
581 case 0: error = IDETAPE_ERROR_GENERAL; break;
582 case 1: error = 0; break;
583 default: error = uptodate;
587 tape->failed_pc = NULL;
589 if (!blk_special_request(rq)) {
590 ide_end_request(drive, uptodate, nr_sects);
594 spin_lock_irqsave(&tape->lock, flags);
596 ide_end_drive_cmd(drive, 0, 0);
598 spin_unlock_irqrestore(&tape->lock, flags);
602 static void ide_tape_callback(ide_drive_t *drive)
604 idetape_tape_t *tape = drive->driver_data;
605 struct ide_atapi_pc *pc = tape->pc;
606 int uptodate = pc->error ? 0 : 1;
608 debug_log(DBG_PROCS, "Enter %s\n", __func__);
610 if (tape->failed_pc == pc)
611 tape->failed_pc = NULL;
613 if (pc->c[0] == REQUEST_SENSE) {
615 idetape_analyze_error(drive, pc->buf);
617 printk(KERN_ERR "ide-tape: Error in REQUEST SENSE "
618 "itself - Aborting request!\n");
619 } else if (pc->c[0] == READ_6 || pc->c[0] == WRITE_6) {
620 struct request *rq = drive->hwif->hwgroup->rq;
621 int blocks = pc->xferred / tape->blk_size;
623 tape->avg_size += blocks * tape->blk_size;
625 if (time_after_eq(jiffies, tape->avg_time + HZ)) {
626 tape->avg_speed = tape->avg_size * HZ /
627 (jiffies - tape->avg_time) / 1024;
629 tape->avg_time = jiffies;
632 tape->first_frame += blocks;
633 rq->current_nr_sectors -= blocks;
636 uptodate = pc->error;
637 } else if (pc->c[0] == READ_POSITION && uptodate) {
638 u8 *readpos = tape->pc->buf;
640 debug_log(DBG_SENSE, "BOP - %s\n",
641 (readpos[0] & 0x80) ? "Yes" : "No");
642 debug_log(DBG_SENSE, "EOP - %s\n",
643 (readpos[0] & 0x40) ? "Yes" : "No");
645 if (readpos[0] & 0x4) {
646 printk(KERN_INFO "ide-tape: Block location is unknown"
648 clear_bit(IDE_AFLAG_ADDRESS_VALID, &drive->atapi_flags);
651 debug_log(DBG_SENSE, "Block Location - %u\n",
652 be32_to_cpu(*(u32 *)&readpos[4]));
654 tape->partition = readpos[1];
655 tape->first_frame = be32_to_cpu(*(u32 *)&readpos[4]);
656 set_bit(IDE_AFLAG_ADDRESS_VALID, &drive->atapi_flags);
660 idetape_end_request(drive, uptodate, 0);
663 static void idetape_init_pc(struct ide_atapi_pc *pc)
665 memset(pc->c, 0, 12);
669 pc->buf = pc->pc_buf;
670 pc->buf_size = IDETAPE_PC_BUFFER_SIZE;
675 static void idetape_create_request_sense_cmd(struct ide_atapi_pc *pc)
678 pc->c[0] = REQUEST_SENSE;
683 static void idetape_init_rq(struct request *rq, u8 cmd)
685 blk_rq_init(NULL, rq);
686 rq->cmd_type = REQ_TYPE_SPECIAL;
691 * Generate a new packet command request in front of the request queue, before
692 * the current request, so that it will be processed immediately, on the next
693 * pass through the driver. The function below is called from the request
694 * handling part of the driver (the "bottom" part). Safe storage for the request
695 * should be allocated with ide_tape_next_{pc,rq}_storage() prior to that.
697 * Memory for those requests is pre-allocated at initialization time, and is
698 * limited to IDETAPE_PC_STACK requests. We assume that we have enough space for
699 * the maximum possible number of inter-dependent packet commands.
701 * The higher level of the driver - The ioctl handler and the character device
702 * handling functions should queue request to the lower level part and wait for
703 * their completion using idetape_queue_pc_tail or idetape_queue_rw_tail.
705 static void idetape_queue_pc_head(ide_drive_t *drive, struct ide_atapi_pc *pc,
708 struct ide_tape_obj *tape = drive->driver_data;
710 idetape_init_rq(rq, REQ_IDETAPE_PC1);
711 rq->cmd_flags |= REQ_PREEMPT;
712 rq->buffer = (char *) pc;
713 rq->rq_disk = tape->disk;
714 memcpy(rq->cmd, pc->c, 12);
715 ide_do_drive_cmd(drive, rq);
719 * idetape_retry_pc is called when an error was detected during the
720 * last packet command. We queue a request sense packet command in
721 * the head of the request list.
723 static void idetape_retry_pc(ide_drive_t *drive)
725 struct ide_atapi_pc *pc;
728 (void)ide_read_error(drive);
729 pc = idetape_next_pc_storage(drive);
730 rq = idetape_next_rq_storage(drive);
731 idetape_create_request_sense_cmd(pc);
732 set_bit(IDE_AFLAG_IGNORE_DSC, &drive->atapi_flags);
733 idetape_queue_pc_head(drive, pc, rq);
737 * Postpone the current request so that ide.c will be able to service requests
738 * from another device on the same hwgroup while we are polling for DSC.
740 static void idetape_postpone_request(ide_drive_t *drive)
742 idetape_tape_t *tape = drive->driver_data;
744 debug_log(DBG_PROCS, "Enter %s\n", __func__);
746 tape->postponed_rq = HWGROUP(drive)->rq;
747 ide_stall_queue(drive, tape->dsc_poll_freq);
750 static void ide_tape_handle_dsc(ide_drive_t *drive)
752 idetape_tape_t *tape = drive->driver_data;
754 /* Media access command */
755 tape->dsc_polling_start = jiffies;
756 tape->dsc_poll_freq = IDETAPE_DSC_MA_FAST;
757 tape->dsc_timeout = jiffies + IDETAPE_DSC_MA_TIMEOUT;
758 /* Allow ide.c to handle other requests */
759 idetape_postpone_request(drive);
762 static void ide_tape_io_buffers(ide_drive_t *drive, struct ide_atapi_pc *pc,
763 unsigned int bcount, int write)
766 idetape_output_buffers(drive, pc, bcount);
768 idetape_input_buffers(drive, pc, bcount);
772 * This is the usual interrupt handler which will be called during a packet
773 * command. We will transfer some of the data (as requested by the drive) and
774 * will re-point interrupt handler to us. When data transfer is finished, we
775 * will act according to the algorithm described before
778 static ide_startstop_t idetape_pc_intr(ide_drive_t *drive)
780 idetape_tape_t *tape = drive->driver_data;
782 return ide_pc_intr(drive, tape->pc, idetape_pc_intr, IDETAPE_WAIT_CMD,
783 NULL, idetape_update_buffers, idetape_retry_pc,
784 ide_tape_handle_dsc, ide_tape_io_buffers);
788 * Packet Command Interface
790 * The current Packet Command is available in tape->pc, and will not change
791 * until we finish handling it. Each packet command is associated with a
792 * callback function that will be called when the command is finished.
794 * The handling will be done in three stages:
796 * 1. idetape_issue_pc will send the packet command to the drive, and will set
797 * the interrupt handler to idetape_pc_intr.
799 * 2. On each interrupt, idetape_pc_intr will be called. This step will be
800 * repeated until the device signals us that no more interrupts will be issued.
802 * 3. ATAPI Tape media access commands have immediate status with a delayed
803 * process. In case of a successful initiation of a media access packet command,
804 * the DSC bit will be set when the actual execution of the command is finished.
805 * Since the tape drive will not issue an interrupt, we have to poll for this
806 * event. In this case, we define the request as "low priority request" by
807 * setting rq_status to IDETAPE_RQ_POSTPONED, set a timer to poll for DSC and
810 * ide.c will then give higher priority to requests which originate from the
811 * other device, until will change rq_status to RQ_ACTIVE.
813 * 4. When the packet command is finished, it will be checked for errors.
815 * 5. In case an error was found, we queue a request sense packet command in
816 * front of the request queue and retry the operation up to
817 * IDETAPE_MAX_PC_RETRIES times.
819 * 6. In case no error was found, or we decided to give up and not to retry
820 * again, the callback function will be called and then we will handle the next
823 static ide_startstop_t idetape_transfer_pc(ide_drive_t *drive)
825 idetape_tape_t *tape = drive->driver_data;
827 return ide_transfer_pc(drive, tape->pc, idetape_pc_intr,
828 IDETAPE_WAIT_CMD, NULL);
831 static ide_startstop_t idetape_issue_pc(ide_drive_t *drive,
832 struct ide_atapi_pc *pc)
834 idetape_tape_t *tape = drive->driver_data;
836 if (tape->pc->c[0] == REQUEST_SENSE &&
837 pc->c[0] == REQUEST_SENSE) {
838 printk(KERN_ERR "ide-tape: possible ide-tape.c bug - "
839 "Two request sense in serial were issued\n");
842 if (tape->failed_pc == NULL && pc->c[0] != REQUEST_SENSE)
843 tape->failed_pc = pc;
844 /* Set the current packet command */
847 if (pc->retries > IDETAPE_MAX_PC_RETRIES ||
848 (pc->flags & PC_FLAG_ABORT)) {
850 * We will "abort" retrying a packet command in case legitimate
851 * error code was received (crossing a filemark, or end of the
852 * media, for example).
854 if (!(pc->flags & PC_FLAG_ABORT)) {
855 if (!(pc->c[0] == TEST_UNIT_READY &&
856 tape->sense_key == 2 && tape->asc == 4 &&
857 (tape->ascq == 1 || tape->ascq == 8))) {
858 printk(KERN_ERR "ide-tape: %s: I/O error, "
859 "pc = %2x, key = %2x, "
860 "asc = %2x, ascq = %2x\n",
861 tape->name, pc->c[0],
862 tape->sense_key, tape->asc,
866 pc->error = IDETAPE_ERROR_GENERAL;
868 tape->failed_pc = NULL;
869 drive->pc_callback(drive);
872 debug_log(DBG_SENSE, "Retry #%d, cmd = %02X\n", pc->retries, pc->c[0]);
876 return ide_issue_pc(drive, pc, idetape_transfer_pc,
877 IDETAPE_WAIT_CMD, NULL);
880 /* A mode sense command is used to "sense" tape parameters. */
881 static void idetape_create_mode_sense_cmd(struct ide_atapi_pc *pc, u8 page_code)
884 pc->c[0] = MODE_SENSE;
885 if (page_code != IDETAPE_BLOCK_DESCRIPTOR)
886 /* DBD = 1 - Don't return block descriptors */
888 pc->c[2] = page_code;
890 * Changed pc->c[3] to 0 (255 will at best return unused info).
892 * For SCSI this byte is defined as subpage instead of high byte
893 * of length and some IDE drives seem to interpret it this way
894 * and return an error when 255 is used.
897 /* We will just discard data in that case */
899 if (page_code == IDETAPE_BLOCK_DESCRIPTOR)
901 else if (page_code == IDETAPE_CAPABILITIES_PAGE)
907 static ide_startstop_t idetape_media_access_finished(ide_drive_t *drive)
909 ide_hwif_t *hwif = drive->hwif;
910 idetape_tape_t *tape = drive->driver_data;
911 struct ide_atapi_pc *pc = tape->pc;
914 stat = hwif->tp_ops->read_status(hwif);
916 if (stat & SEEK_STAT) {
917 if (stat & ERR_STAT) {
919 if (pc->c[0] != TEST_UNIT_READY)
920 printk(KERN_ERR "ide-tape: %s: I/O error, ",
922 /* Retry operation */
923 idetape_retry_pc(drive);
928 pc->error = IDETAPE_ERROR_GENERAL;
929 tape->failed_pc = NULL;
931 drive->pc_callback(drive);
935 static void ide_tape_create_rw_cmd(idetape_tape_t *tape,
936 struct ide_atapi_pc *pc, struct request *rq,
939 struct idetape_bh *bh = (struct idetape_bh *)rq->special;
940 unsigned int length = rq->current_nr_sectors;
943 put_unaligned(cpu_to_be32(length), (unsigned int *) &pc->c[1]);
947 pc->buf_size = length * tape->blk_size;
948 pc->req_xfer = pc->buf_size;
949 if (pc->req_xfer == tape->buffer_size)
950 pc->flags |= PC_FLAG_DMA_OK;
952 if (opcode == READ_6) {
954 atomic_set(&bh->b_count, 0);
955 } else if (opcode == WRITE_6) {
957 pc->flags |= PC_FLAG_WRITING;
958 pc->b_data = bh->b_data;
959 pc->b_count = atomic_read(&bh->b_count);
962 memcpy(rq->cmd, pc->c, 12);
965 static ide_startstop_t idetape_do_request(ide_drive_t *drive,
966 struct request *rq, sector_t block)
968 ide_hwif_t *hwif = drive->hwif;
969 idetape_tape_t *tape = drive->driver_data;
970 struct ide_atapi_pc *pc = NULL;
971 struct request *postponed_rq = tape->postponed_rq;
974 debug_log(DBG_SENSE, "sector: %ld, nr_sectors: %ld,"
975 " current_nr_sectors: %d\n",
976 rq->sector, rq->nr_sectors, rq->current_nr_sectors);
978 if (!blk_special_request(rq)) {
979 /* We do not support buffer cache originated requests. */
980 printk(KERN_NOTICE "ide-tape: %s: Unsupported request in "
981 "request queue (%d)\n", drive->name, rq->cmd_type);
982 ide_end_request(drive, 0, 0);
986 /* Retry a failed packet command */
987 if (tape->failed_pc && tape->pc->c[0] == REQUEST_SENSE) {
988 pc = tape->failed_pc;
992 if (postponed_rq != NULL)
993 if (rq != postponed_rq) {
994 printk(KERN_ERR "ide-tape: ide-tape.c bug - "
995 "Two DSC requests were queued\n");
996 idetape_end_request(drive, 0, 0);
1000 tape->postponed_rq = NULL;
1003 * If the tape is still busy, postpone our request and service
1004 * the other device meanwhile.
1006 stat = hwif->tp_ops->read_status(hwif);
1008 if (!drive->dsc_overlap && !(rq->cmd[13] & REQ_IDETAPE_PC2))
1009 set_bit(IDE_AFLAG_IGNORE_DSC, &drive->atapi_flags);
1011 if (drive->post_reset == 1) {
1012 set_bit(IDE_AFLAG_IGNORE_DSC, &drive->atapi_flags);
1013 drive->post_reset = 0;
1016 if (!test_and_clear_bit(IDE_AFLAG_IGNORE_DSC, &drive->atapi_flags) &&
1017 (stat & SEEK_STAT) == 0) {
1018 if (postponed_rq == NULL) {
1019 tape->dsc_polling_start = jiffies;
1020 tape->dsc_poll_freq = tape->best_dsc_rw_freq;
1021 tape->dsc_timeout = jiffies + IDETAPE_DSC_RW_TIMEOUT;
1022 } else if (time_after(jiffies, tape->dsc_timeout)) {
1023 printk(KERN_ERR "ide-tape: %s: DSC timeout\n",
1025 if (rq->cmd[13] & REQ_IDETAPE_PC2) {
1026 idetape_media_access_finished(drive);
1029 return ide_do_reset(drive);
1031 } else if (time_after(jiffies,
1032 tape->dsc_polling_start +
1033 IDETAPE_DSC_MA_THRESHOLD))
1034 tape->dsc_poll_freq = IDETAPE_DSC_MA_SLOW;
1035 idetape_postpone_request(drive);
1038 if (rq->cmd[13] & REQ_IDETAPE_READ) {
1039 pc = idetape_next_pc_storage(drive);
1040 ide_tape_create_rw_cmd(tape, pc, rq, READ_6);
1043 if (rq->cmd[13] & REQ_IDETAPE_WRITE) {
1044 pc = idetape_next_pc_storage(drive);
1045 ide_tape_create_rw_cmd(tape, pc, rq, WRITE_6);
1048 if (rq->cmd[13] & REQ_IDETAPE_PC1) {
1049 pc = (struct ide_atapi_pc *) rq->buffer;
1050 rq->cmd[13] &= ~(REQ_IDETAPE_PC1);
1051 rq->cmd[13] |= REQ_IDETAPE_PC2;
1054 if (rq->cmd[13] & REQ_IDETAPE_PC2) {
1055 idetape_media_access_finished(drive);
1061 return idetape_issue_pc(drive, pc);
1065 * The function below uses __get_free_pages to allocate a data buffer of size
1066 * tape->buffer_size (or a bit more). We attempt to combine sequential pages as
1069 * It returns a pointer to the newly allocated buffer, or NULL in case of
1072 static struct idetape_bh *ide_tape_kmalloc_buffer(idetape_tape_t *tape,
1073 int full, int clear)
1075 struct idetape_bh *prev_bh, *bh, *merge_bh;
1076 int pages = tape->pages_per_buffer;
1077 unsigned int order, b_allocd;
1078 char *b_data = NULL;
1080 merge_bh = kmalloc(sizeof(struct idetape_bh), GFP_KERNEL);
1085 order = fls(pages) - 1;
1086 bh->b_data = (char *) __get_free_pages(GFP_KERNEL, order);
1089 b_allocd = (1 << order) * PAGE_SIZE;
1093 memset(bh->b_data, 0, b_allocd);
1094 bh->b_reqnext = NULL;
1095 bh->b_size = b_allocd;
1096 atomic_set(&bh->b_count, full ? bh->b_size : 0);
1099 order = fls(pages) - 1;
1100 b_data = (char *) __get_free_pages(GFP_KERNEL, order);
1103 b_allocd = (1 << order) * PAGE_SIZE;
1106 memset(b_data, 0, b_allocd);
1108 /* newly allocated page frames below buffer header or ...*/
1109 if (bh->b_data == b_data + b_allocd) {
1110 bh->b_size += b_allocd;
1111 bh->b_data -= b_allocd;
1113 atomic_add(b_allocd, &bh->b_count);
1116 /* they are above the header */
1117 if (b_data == bh->b_data + bh->b_size) {
1118 bh->b_size += b_allocd;
1120 atomic_add(b_allocd, &bh->b_count);
1124 bh = kmalloc(sizeof(struct idetape_bh), GFP_KERNEL);
1126 free_pages((unsigned long) b_data, order);
1129 bh->b_reqnext = NULL;
1130 bh->b_data = b_data;
1131 bh->b_size = b_allocd;
1132 atomic_set(&bh->b_count, full ? bh->b_size : 0);
1133 prev_bh->b_reqnext = bh;
1138 bh->b_size -= tape->excess_bh_size;
1140 atomic_sub(tape->excess_bh_size, &bh->b_count);
1143 ide_tape_kfree_buffer(tape);
1147 static int idetape_copy_stage_from_user(idetape_tape_t *tape,
1148 const char __user *buf, int n)
1150 struct idetape_bh *bh = tape->bh;
1156 printk(KERN_ERR "ide-tape: bh == NULL in %s\n",
1160 count = min((unsigned int)
1161 (bh->b_size - atomic_read(&bh->b_count)),
1163 if (copy_from_user(bh->b_data + atomic_read(&bh->b_count), buf,
1167 atomic_add(count, &bh->b_count);
1169 if (atomic_read(&bh->b_count) == bh->b_size) {
1172 atomic_set(&bh->b_count, 0);
1179 static int idetape_copy_stage_to_user(idetape_tape_t *tape, char __user *buf,
1182 struct idetape_bh *bh = tape->bh;
1188 printk(KERN_ERR "ide-tape: bh == NULL in %s\n",
1192 count = min(tape->b_count, n);
1193 if (copy_to_user(buf, tape->b_data, count))
1196 tape->b_data += count;
1197 tape->b_count -= count;
1199 if (!tape->b_count) {
1203 tape->b_data = bh->b_data;
1204 tape->b_count = atomic_read(&bh->b_count);
1211 static void idetape_init_merge_buffer(idetape_tape_t *tape)
1213 struct idetape_bh *bh = tape->merge_bh;
1214 tape->bh = tape->merge_bh;
1216 if (tape->chrdev_dir == IDETAPE_DIR_WRITE)
1217 atomic_set(&bh->b_count, 0);
1219 tape->b_data = bh->b_data;
1220 tape->b_count = atomic_read(&bh->b_count);
1225 * Write a filemark if write_filemark=1. Flush the device buffers without
1226 * writing a filemark otherwise.
1228 static void idetape_create_write_filemark_cmd(ide_drive_t *drive,
1229 struct ide_atapi_pc *pc, int write_filemark)
1231 idetape_init_pc(pc);
1232 pc->c[0] = WRITE_FILEMARKS;
1233 pc->c[4] = write_filemark;
1234 pc->flags |= PC_FLAG_WAIT_FOR_DSC;
1237 static void idetape_create_test_unit_ready_cmd(struct ide_atapi_pc *pc)
1239 idetape_init_pc(pc);
1240 pc->c[0] = TEST_UNIT_READY;
1244 * We add a special packet command request to the tail of the request queue, and
1245 * wait for it to be serviced. This is not to be called from within the request
1246 * handling part of the driver! We allocate here data on the stack and it is
1247 * valid until the request is finished. This is not the case for the bottom part
1248 * of the driver, where we are always leaving the functions to wait for an
1249 * interrupt or a timer event.
1251 * From the bottom part of the driver, we should allocate safe memory using
1252 * idetape_next_pc_storage() and ide_tape_next_rq_storage(), and add the request
1253 * to the request list without waiting for it to be serviced! In that case, we
1254 * usually use idetape_queue_pc_head().
1256 static int idetape_queue_pc_tail(ide_drive_t *drive, struct ide_atapi_pc *pc)
1258 struct ide_tape_obj *tape = drive->driver_data;
1262 rq = blk_get_request(drive->queue, READ, __GFP_WAIT);
1263 rq->cmd_type = REQ_TYPE_SPECIAL;
1264 rq->cmd[13] = REQ_IDETAPE_PC1;
1265 rq->buffer = (char *)pc;
1266 memcpy(rq->cmd, pc->c, 12);
1267 error = blk_execute_rq(drive->queue, tape->disk, rq, 0);
1268 blk_put_request(rq);
1272 static void idetape_create_load_unload_cmd(ide_drive_t *drive,
1273 struct ide_atapi_pc *pc, int cmd)
1275 idetape_init_pc(pc);
1276 pc->c[0] = START_STOP;
1278 pc->flags |= PC_FLAG_WAIT_FOR_DSC;
1281 static int idetape_wait_ready(ide_drive_t *drive, unsigned long timeout)
1283 idetape_tape_t *tape = drive->driver_data;
1284 struct ide_atapi_pc pc;
1285 int load_attempted = 0;
1287 /* Wait for the tape to become ready */
1288 set_bit(IDE_AFLAG_MEDIUM_PRESENT, &drive->atapi_flags);
1290 while (time_before(jiffies, timeout)) {
1291 idetape_create_test_unit_ready_cmd(&pc);
1292 if (!idetape_queue_pc_tail(drive, &pc))
1294 if ((tape->sense_key == 2 && tape->asc == 4 && tape->ascq == 2)
1295 || (tape->asc == 0x3A)) {
1299 idetape_create_load_unload_cmd(drive, &pc,
1300 IDETAPE_LU_LOAD_MASK);
1301 idetape_queue_pc_tail(drive, &pc);
1303 /* not about to be ready */
1304 } else if (!(tape->sense_key == 2 && tape->asc == 4 &&
1305 (tape->ascq == 1 || tape->ascq == 8)))
1312 static int idetape_flush_tape_buffers(ide_drive_t *drive)
1314 struct ide_atapi_pc pc;
1317 idetape_create_write_filemark_cmd(drive, &pc, 0);
1318 rc = idetape_queue_pc_tail(drive, &pc);
1321 idetape_wait_ready(drive, 60 * 5 * HZ);
1325 static void idetape_create_read_position_cmd(struct ide_atapi_pc *pc)
1327 idetape_init_pc(pc);
1328 pc->c[0] = READ_POSITION;
1332 static int idetape_read_position(ide_drive_t *drive)
1334 idetape_tape_t *tape = drive->driver_data;
1335 struct ide_atapi_pc pc;
1338 debug_log(DBG_PROCS, "Enter %s\n", __func__);
1340 idetape_create_read_position_cmd(&pc);
1341 if (idetape_queue_pc_tail(drive, &pc))
1343 position = tape->first_frame;
1347 static void idetape_create_locate_cmd(ide_drive_t *drive,
1348 struct ide_atapi_pc *pc,
1349 unsigned int block, u8 partition, int skip)
1351 idetape_init_pc(pc);
1352 pc->c[0] = POSITION_TO_ELEMENT;
1354 put_unaligned(cpu_to_be32(block), (unsigned int *) &pc->c[3]);
1355 pc->c[8] = partition;
1356 pc->flags |= PC_FLAG_WAIT_FOR_DSC;
1359 static int idetape_create_prevent_cmd(ide_drive_t *drive,
1360 struct ide_atapi_pc *pc, int prevent)
1362 idetape_tape_t *tape = drive->driver_data;
1364 /* device supports locking according to capabilities page */
1365 if (!(tape->caps[6] & 0x01))
1368 idetape_init_pc(pc);
1369 pc->c[0] = ALLOW_MEDIUM_REMOVAL;
1374 static void __ide_tape_discard_merge_buffer(ide_drive_t *drive)
1376 idetape_tape_t *tape = drive->driver_data;
1378 if (tape->chrdev_dir != IDETAPE_DIR_READ)
1381 clear_bit(IDE_AFLAG_FILEMARK, &drive->atapi_flags);
1382 tape->merge_bh_size = 0;
1383 if (tape->merge_bh != NULL) {
1384 ide_tape_kfree_buffer(tape);
1385 tape->merge_bh = NULL;
1388 tape->chrdev_dir = IDETAPE_DIR_NONE;
1392 * Position the tape to the requested block using the LOCATE packet command.
1393 * A READ POSITION command is then issued to check where we are positioned. Like
1394 * all higher level operations, we queue the commands at the tail of the request
1395 * queue and wait for their completion.
1397 static int idetape_position_tape(ide_drive_t *drive, unsigned int block,
1398 u8 partition, int skip)
1400 idetape_tape_t *tape = drive->driver_data;
1402 struct ide_atapi_pc pc;
1404 if (tape->chrdev_dir == IDETAPE_DIR_READ)
1405 __ide_tape_discard_merge_buffer(drive);
1406 idetape_wait_ready(drive, 60 * 5 * HZ);
1407 idetape_create_locate_cmd(drive, &pc, block, partition, skip);
1408 retval = idetape_queue_pc_tail(drive, &pc);
1412 idetape_create_read_position_cmd(&pc);
1413 return (idetape_queue_pc_tail(drive, &pc));
1416 static void ide_tape_discard_merge_buffer(ide_drive_t *drive,
1417 int restore_position)
1419 idetape_tape_t *tape = drive->driver_data;
1422 __ide_tape_discard_merge_buffer(drive);
1423 if (restore_position) {
1424 position = idetape_read_position(drive);
1425 seek = position > 0 ? position : 0;
1426 if (idetape_position_tape(drive, seek, 0, 0)) {
1427 printk(KERN_INFO "ide-tape: %s: position_tape failed in"
1428 " %s\n", tape->name, __func__);
1435 * Generate a read/write request for the block device interface and wait for it
1438 static int idetape_queue_rw_tail(ide_drive_t *drive, int cmd, int blocks,
1439 struct idetape_bh *bh)
1441 idetape_tape_t *tape = drive->driver_data;
1445 debug_log(DBG_SENSE, "%s: cmd=%d\n", __func__, cmd);
1447 rq = blk_get_request(drive->queue, READ, __GFP_WAIT);
1448 rq->cmd_type = REQ_TYPE_SPECIAL;
1450 rq->rq_disk = tape->disk;
1451 rq->special = (void *)bh;
1452 rq->sector = tape->first_frame;
1453 rq->nr_sectors = blocks;
1454 rq->current_nr_sectors = blocks;
1455 blk_execute_rq(drive->queue, tape->disk, rq, 0);
1457 errors = rq->errors;
1458 ret = tape->blk_size * (blocks - rq->current_nr_sectors);
1459 blk_put_request(rq);
1461 if ((cmd & (REQ_IDETAPE_READ | REQ_IDETAPE_WRITE)) == 0)
1465 idetape_init_merge_buffer(tape);
1466 if (errors == IDETAPE_ERROR_GENERAL)
1471 static void idetape_create_inquiry_cmd(struct ide_atapi_pc *pc)
1473 idetape_init_pc(pc);
1479 static void idetape_create_rewind_cmd(ide_drive_t *drive,
1480 struct ide_atapi_pc *pc)
1482 idetape_init_pc(pc);
1483 pc->c[0] = REZERO_UNIT;
1484 pc->flags |= PC_FLAG_WAIT_FOR_DSC;
1487 static void idetape_create_erase_cmd(struct ide_atapi_pc *pc)
1489 idetape_init_pc(pc);
1492 pc->flags |= PC_FLAG_WAIT_FOR_DSC;
1495 static void idetape_create_space_cmd(struct ide_atapi_pc *pc, int count, u8 cmd)
1497 idetape_init_pc(pc);
1499 put_unaligned(cpu_to_be32(count), (unsigned int *) &pc->c[1]);
1501 pc->flags |= PC_FLAG_WAIT_FOR_DSC;
1504 /* Queue up a character device originated write request. */
1505 static int idetape_add_chrdev_write_request(ide_drive_t *drive, int blocks)
1507 idetape_tape_t *tape = drive->driver_data;
1509 debug_log(DBG_CHRDEV, "Enter %s\n", __func__);
1511 return idetape_queue_rw_tail(drive, REQ_IDETAPE_WRITE,
1512 blocks, tape->merge_bh);
1515 static void ide_tape_flush_merge_buffer(ide_drive_t *drive)
1517 idetape_tape_t *tape = drive->driver_data;
1519 struct idetape_bh *bh;
1521 if (tape->chrdev_dir != IDETAPE_DIR_WRITE) {
1522 printk(KERN_ERR "ide-tape: bug: Trying to empty merge buffer"
1523 " but we are not writing.\n");
1526 if (tape->merge_bh_size > tape->buffer_size) {
1527 printk(KERN_ERR "ide-tape: bug: merge_buffer too big\n");
1528 tape->merge_bh_size = tape->buffer_size;
1530 if (tape->merge_bh_size) {
1531 blocks = tape->merge_bh_size / tape->blk_size;
1532 if (tape->merge_bh_size % tape->blk_size) {
1536 i = tape->blk_size - tape->merge_bh_size %
1538 bh = tape->bh->b_reqnext;
1540 atomic_set(&bh->b_count, 0);
1546 printk(KERN_INFO "ide-tape: bug,"
1550 min = min(i, (unsigned int)(bh->b_size -
1551 atomic_read(&bh->b_count)));
1552 memset(bh->b_data + atomic_read(&bh->b_count),
1554 atomic_add(min, &bh->b_count);
1559 (void) idetape_add_chrdev_write_request(drive, blocks);
1560 tape->merge_bh_size = 0;
1562 if (tape->merge_bh != NULL) {
1563 ide_tape_kfree_buffer(tape);
1564 tape->merge_bh = NULL;
1566 tape->chrdev_dir = IDETAPE_DIR_NONE;
1569 static int idetape_init_read(ide_drive_t *drive)
1571 idetape_tape_t *tape = drive->driver_data;
1574 /* Initialize read operation */
1575 if (tape->chrdev_dir != IDETAPE_DIR_READ) {
1576 if (tape->chrdev_dir == IDETAPE_DIR_WRITE) {
1577 ide_tape_flush_merge_buffer(drive);
1578 idetape_flush_tape_buffers(drive);
1580 if (tape->merge_bh || tape->merge_bh_size) {
1581 printk(KERN_ERR "ide-tape: merge_bh_size should be"
1583 tape->merge_bh_size = 0;
1585 tape->merge_bh = ide_tape_kmalloc_buffer(tape, 0, 0);
1586 if (!tape->merge_bh)
1588 tape->chrdev_dir = IDETAPE_DIR_READ;
1591 * Issue a read 0 command to ensure that DSC handshake is
1592 * switched from completion mode to buffer available mode.
1593 * No point in issuing this if DSC overlap isn't supported, some
1594 * drives (Seagate STT3401A) will return an error.
1596 if (drive->dsc_overlap) {
1597 bytes_read = idetape_queue_rw_tail(drive,
1598 REQ_IDETAPE_READ, 0,
1600 if (bytes_read < 0) {
1601 ide_tape_kfree_buffer(tape);
1602 tape->merge_bh = NULL;
1603 tape->chrdev_dir = IDETAPE_DIR_NONE;
1612 /* called from idetape_chrdev_read() to service a chrdev read request. */
1613 static int idetape_add_chrdev_read_request(ide_drive_t *drive, int blocks)
1615 idetape_tape_t *tape = drive->driver_data;
1617 debug_log(DBG_PROCS, "Enter %s, %d blocks\n", __func__, blocks);
1619 /* If we are at a filemark, return a read length of 0 */
1620 if (test_bit(IDE_AFLAG_FILEMARK, &drive->atapi_flags))
1623 idetape_init_read(drive);
1625 return idetape_queue_rw_tail(drive, REQ_IDETAPE_READ, blocks,
1629 static void idetape_pad_zeros(ide_drive_t *drive, int bcount)
1631 idetape_tape_t *tape = drive->driver_data;
1632 struct idetape_bh *bh;
1638 bh = tape->merge_bh;
1639 count = min(tape->buffer_size, bcount);
1641 blocks = count / tape->blk_size;
1643 atomic_set(&bh->b_count,
1644 min(count, (unsigned int)bh->b_size));
1645 memset(bh->b_data, 0, atomic_read(&bh->b_count));
1646 count -= atomic_read(&bh->b_count);
1649 idetape_queue_rw_tail(drive, REQ_IDETAPE_WRITE, blocks,
1655 * Rewinds the tape to the Beginning Of the current Partition (BOP). We
1656 * currently support only one partition.
1658 static int idetape_rewind_tape(ide_drive_t *drive)
1661 struct ide_atapi_pc pc;
1662 idetape_tape_t *tape;
1663 tape = drive->driver_data;
1665 debug_log(DBG_SENSE, "Enter %s\n", __func__);
1667 idetape_create_rewind_cmd(drive, &pc);
1668 retval = idetape_queue_pc_tail(drive, &pc);
1672 idetape_create_read_position_cmd(&pc);
1673 retval = idetape_queue_pc_tail(drive, &pc);
1679 /* mtio.h compatible commands should be issued to the chrdev interface. */
1680 static int idetape_blkdev_ioctl(ide_drive_t *drive, unsigned int cmd,
1683 idetape_tape_t *tape = drive->driver_data;
1684 void __user *argp = (void __user *)arg;
1686 struct idetape_config {
1687 int dsc_rw_frequency;
1688 int dsc_media_access_frequency;
1692 debug_log(DBG_PROCS, "Enter %s\n", __func__);
1696 if (copy_from_user(&config, argp, sizeof(config)))
1698 tape->best_dsc_rw_freq = config.dsc_rw_frequency;
1701 config.dsc_rw_frequency = (int) tape->best_dsc_rw_freq;
1702 config.nr_stages = 1;
1703 if (copy_to_user(argp, &config, sizeof(config)))
1712 static int idetape_space_over_filemarks(ide_drive_t *drive, short mt_op,
1715 idetape_tape_t *tape = drive->driver_data;
1716 struct ide_atapi_pc pc;
1717 int retval, count = 0;
1718 int sprev = !!(tape->caps[4] & 0x20);
1722 if (MTBSF == mt_op || MTBSFM == mt_op) {
1725 mt_count = -mt_count;
1728 if (tape->chrdev_dir == IDETAPE_DIR_READ) {
1729 tape->merge_bh_size = 0;
1730 if (test_and_clear_bit(IDE_AFLAG_FILEMARK, &drive->atapi_flags))
1732 ide_tape_discard_merge_buffer(drive, 0);
1738 idetape_create_space_cmd(&pc, mt_count - count,
1739 IDETAPE_SPACE_OVER_FILEMARK);
1740 return idetape_queue_pc_tail(drive, &pc);
1745 retval = idetape_space_over_filemarks(drive, MTFSF,
1749 count = (MTBSFM == mt_op ? 1 : -1);
1750 return idetape_space_over_filemarks(drive, MTFSF, count);
1752 printk(KERN_ERR "ide-tape: MTIO operation %d not supported\n",
1759 * Our character device read / write functions.
1761 * The tape is optimized to maximize throughput when it is transferring an
1762 * integral number of the "continuous transfer limit", which is a parameter of
1763 * the specific tape (26kB on my particular tape, 32kB for Onstream).
1765 * As of version 1.3 of the driver, the character device provides an abstract
1766 * continuous view of the media - any mix of block sizes (even 1 byte) on the
1767 * same backup/restore procedure is supported. The driver will internally
1768 * convert the requests to the recommended transfer unit, so that an unmatch
1769 * between the user's block size to the recommended size will only result in a
1770 * (slightly) increased driver overhead, but will no longer hit performance.
1771 * This is not applicable to Onstream.
1773 static ssize_t idetape_chrdev_read(struct file *file, char __user *buf,
1774 size_t count, loff_t *ppos)
1776 struct ide_tape_obj *tape = ide_tape_f(file);
1777 ide_drive_t *drive = tape->drive;
1778 ssize_t bytes_read, temp, actually_read = 0, rc;
1780 u16 ctl = *(u16 *)&tape->caps[12];
1782 debug_log(DBG_CHRDEV, "Enter %s, count %Zd\n", __func__, count);
1784 if (tape->chrdev_dir != IDETAPE_DIR_READ) {
1785 if (test_bit(IDE_AFLAG_DETECT_BS, &drive->atapi_flags))
1786 if (count > tape->blk_size &&
1787 (count % tape->blk_size) == 0)
1788 tape->user_bs_factor = count / tape->blk_size;
1790 rc = idetape_init_read(drive);
1795 if (tape->merge_bh_size) {
1796 actually_read = min((unsigned int)(tape->merge_bh_size),
1797 (unsigned int)count);
1798 if (idetape_copy_stage_to_user(tape, buf, actually_read))
1800 buf += actually_read;
1801 tape->merge_bh_size -= actually_read;
1802 count -= actually_read;
1804 while (count >= tape->buffer_size) {
1805 bytes_read = idetape_add_chrdev_read_request(drive, ctl);
1806 if (bytes_read <= 0)
1808 if (idetape_copy_stage_to_user(tape, buf, bytes_read))
1811 count -= bytes_read;
1812 actually_read += bytes_read;
1815 bytes_read = idetape_add_chrdev_read_request(drive, ctl);
1816 if (bytes_read <= 0)
1818 temp = min((unsigned long)count, (unsigned long)bytes_read);
1819 if (idetape_copy_stage_to_user(tape, buf, temp))
1821 actually_read += temp;
1822 tape->merge_bh_size = bytes_read-temp;
1825 if (!actually_read && test_bit(IDE_AFLAG_FILEMARK, &drive->atapi_flags)) {
1826 debug_log(DBG_SENSE, "%s: spacing over filemark\n", tape->name);
1828 idetape_space_over_filemarks(drive, MTFSF, 1);
1832 return ret ? ret : actually_read;
1835 static ssize_t idetape_chrdev_write(struct file *file, const char __user *buf,
1836 size_t count, loff_t *ppos)
1838 struct ide_tape_obj *tape = ide_tape_f(file);
1839 ide_drive_t *drive = tape->drive;
1840 ssize_t actually_written = 0;
1842 u16 ctl = *(u16 *)&tape->caps[12];
1844 /* The drive is write protected. */
1845 if (tape->write_prot)
1848 debug_log(DBG_CHRDEV, "Enter %s, count %Zd\n", __func__, count);
1850 /* Initialize write operation */
1851 if (tape->chrdev_dir != IDETAPE_DIR_WRITE) {
1852 if (tape->chrdev_dir == IDETAPE_DIR_READ)
1853 ide_tape_discard_merge_buffer(drive, 1);
1854 if (tape->merge_bh || tape->merge_bh_size) {
1855 printk(KERN_ERR "ide-tape: merge_bh_size "
1856 "should be 0 now\n");
1857 tape->merge_bh_size = 0;
1859 tape->merge_bh = ide_tape_kmalloc_buffer(tape, 0, 0);
1860 if (!tape->merge_bh)
1862 tape->chrdev_dir = IDETAPE_DIR_WRITE;
1863 idetape_init_merge_buffer(tape);
1866 * Issue a write 0 command to ensure that DSC handshake is
1867 * switched from completion mode to buffer available mode. No
1868 * point in issuing this if DSC overlap isn't supported, some
1869 * drives (Seagate STT3401A) will return an error.
1871 if (drive->dsc_overlap) {
1872 ssize_t retval = idetape_queue_rw_tail(drive,
1873 REQ_IDETAPE_WRITE, 0,
1876 ide_tape_kfree_buffer(tape);
1877 tape->merge_bh = NULL;
1878 tape->chrdev_dir = IDETAPE_DIR_NONE;
1885 if (tape->merge_bh_size) {
1886 if (tape->merge_bh_size >= tape->buffer_size) {
1887 printk(KERN_ERR "ide-tape: bug: merge buf too big\n");
1888 tape->merge_bh_size = 0;
1890 actually_written = min((unsigned int)
1891 (tape->buffer_size - tape->merge_bh_size),
1892 (unsigned int)count);
1893 if (idetape_copy_stage_from_user(tape, buf, actually_written))
1895 buf += actually_written;
1896 tape->merge_bh_size += actually_written;
1897 count -= actually_written;
1899 if (tape->merge_bh_size == tape->buffer_size) {
1901 tape->merge_bh_size = 0;
1902 retval = idetape_add_chrdev_write_request(drive, ctl);
1907 while (count >= tape->buffer_size) {
1909 if (idetape_copy_stage_from_user(tape, buf, tape->buffer_size))
1911 buf += tape->buffer_size;
1912 count -= tape->buffer_size;
1913 retval = idetape_add_chrdev_write_request(drive, ctl);
1914 actually_written += tape->buffer_size;
1919 actually_written += count;
1920 if (idetape_copy_stage_from_user(tape, buf, count))
1922 tape->merge_bh_size += count;
1924 return ret ? ret : actually_written;
1927 static int idetape_write_filemark(ide_drive_t *drive)
1929 struct ide_atapi_pc pc;
1931 /* Write a filemark */
1932 idetape_create_write_filemark_cmd(drive, &pc, 1);
1933 if (idetape_queue_pc_tail(drive, &pc)) {
1934 printk(KERN_ERR "ide-tape: Couldn't write a filemark\n");
1941 * Called from idetape_chrdev_ioctl when the general mtio MTIOCTOP ioctl is
1944 * Note: MTBSF and MTBSFM are not supported when the tape doesn't support
1945 * spacing over filemarks in the reverse direction. In this case, MTFSFM is also
1946 * usually not supported.
1948 * The following commands are currently not supported:
1950 * MTFSS, MTBSS, MTWSM, MTSETDENSITY, MTSETDRVBUFFER, MT_ST_BOOLEANS,
1951 * MT_ST_WRITE_THRESHOLD.
1953 static int idetape_mtioctop(ide_drive_t *drive, short mt_op, int mt_count)
1955 idetape_tape_t *tape = drive->driver_data;
1956 struct ide_atapi_pc pc;
1959 debug_log(DBG_ERR, "Handling MTIOCTOP ioctl: mt_op=%d, mt_count=%d\n",
1969 return idetape_space_over_filemarks(drive, mt_op, mt_count);
1976 if (tape->write_prot)
1978 ide_tape_discard_merge_buffer(drive, 1);
1979 for (i = 0; i < mt_count; i++) {
1980 retval = idetape_write_filemark(drive);
1986 ide_tape_discard_merge_buffer(drive, 0);
1987 if (idetape_rewind_tape(drive))
1991 ide_tape_discard_merge_buffer(drive, 0);
1992 idetape_create_load_unload_cmd(drive, &pc,
1993 IDETAPE_LU_LOAD_MASK);
1994 return idetape_queue_pc_tail(drive, &pc);
1998 * If door is locked, attempt to unlock before
1999 * attempting to eject.
2001 if (tape->door_locked) {
2002 if (idetape_create_prevent_cmd(drive, &pc, 0))
2003 if (!idetape_queue_pc_tail(drive, &pc))
2004 tape->door_locked = DOOR_UNLOCKED;
2006 ide_tape_discard_merge_buffer(drive, 0);
2007 idetape_create_load_unload_cmd(drive, &pc,
2008 !IDETAPE_LU_LOAD_MASK);
2009 retval = idetape_queue_pc_tail(drive, &pc);
2011 clear_bit(IDE_AFLAG_MEDIUM_PRESENT, &drive->atapi_flags);
2014 ide_tape_discard_merge_buffer(drive, 0);
2015 return idetape_flush_tape_buffers(drive);
2017 ide_tape_discard_merge_buffer(drive, 0);
2018 idetape_create_load_unload_cmd(drive, &pc,
2019 IDETAPE_LU_RETENSION_MASK | IDETAPE_LU_LOAD_MASK);
2020 return idetape_queue_pc_tail(drive, &pc);
2022 idetape_create_space_cmd(&pc, 0, IDETAPE_SPACE_TO_EOD);
2023 return idetape_queue_pc_tail(drive, &pc);
2025 (void)idetape_rewind_tape(drive);
2026 idetape_create_erase_cmd(&pc);
2027 return idetape_queue_pc_tail(drive, &pc);
2030 if (mt_count < tape->blk_size ||
2031 mt_count % tape->blk_size)
2033 tape->user_bs_factor = mt_count / tape->blk_size;
2034 clear_bit(IDE_AFLAG_DETECT_BS, &drive->atapi_flags);
2036 set_bit(IDE_AFLAG_DETECT_BS, &drive->atapi_flags);
2039 ide_tape_discard_merge_buffer(drive, 0);
2040 return idetape_position_tape(drive,
2041 mt_count * tape->user_bs_factor, tape->partition, 0);
2043 ide_tape_discard_merge_buffer(drive, 0);
2044 return idetape_position_tape(drive, 0, mt_count, 0);
2048 if (!idetape_create_prevent_cmd(drive, &pc, 1))
2050 retval = idetape_queue_pc_tail(drive, &pc);
2053 tape->door_locked = DOOR_EXPLICITLY_LOCKED;
2056 if (!idetape_create_prevent_cmd(drive, &pc, 0))
2058 retval = idetape_queue_pc_tail(drive, &pc);
2061 tape->door_locked = DOOR_UNLOCKED;
2064 printk(KERN_ERR "ide-tape: MTIO operation %d not supported\n",
2071 * Our character device ioctls. General mtio.h magnetic io commands are
2072 * supported here, and not in the corresponding block interface. Our own
2073 * ide-tape ioctls are supported on both interfaces.
2075 static int idetape_chrdev_ioctl(struct inode *inode, struct file *file,
2076 unsigned int cmd, unsigned long arg)
2078 struct ide_tape_obj *tape = ide_tape_f(file);
2079 ide_drive_t *drive = tape->drive;
2083 int block_offset = 0, position = tape->first_frame;
2084 void __user *argp = (void __user *)arg;
2086 debug_log(DBG_CHRDEV, "Enter %s, cmd=%u\n", __func__, cmd);
2088 if (tape->chrdev_dir == IDETAPE_DIR_WRITE) {
2089 ide_tape_flush_merge_buffer(drive);
2090 idetape_flush_tape_buffers(drive);
2092 if (cmd == MTIOCGET || cmd == MTIOCPOS) {
2093 block_offset = tape->merge_bh_size /
2094 (tape->blk_size * tape->user_bs_factor);
2095 position = idetape_read_position(drive);
2101 if (copy_from_user(&mtop, argp, sizeof(struct mtop)))
2103 return idetape_mtioctop(drive, mtop.mt_op, mtop.mt_count);
2105 memset(&mtget, 0, sizeof(struct mtget));
2106 mtget.mt_type = MT_ISSCSI2;
2107 mtget.mt_blkno = position / tape->user_bs_factor - block_offset;
2109 ((tape->blk_size * tape->user_bs_factor)
2110 << MT_ST_BLKSIZE_SHIFT) & MT_ST_BLKSIZE_MASK;
2112 if (tape->drv_write_prot)
2113 mtget.mt_gstat |= GMT_WR_PROT(0xffffffff);
2115 if (copy_to_user(argp, &mtget, sizeof(struct mtget)))
2119 mtpos.mt_blkno = position / tape->user_bs_factor - block_offset;
2120 if (copy_to_user(argp, &mtpos, sizeof(struct mtpos)))
2124 if (tape->chrdev_dir == IDETAPE_DIR_READ)
2125 ide_tape_discard_merge_buffer(drive, 1);
2126 return idetape_blkdev_ioctl(drive, cmd, arg);
2131 * Do a mode sense page 0 with block descriptor and if it succeeds set the tape
2132 * block size with the reported value.
2134 static void ide_tape_get_bsize_from_bdesc(ide_drive_t *drive)
2136 idetape_tape_t *tape = drive->driver_data;
2137 struct ide_atapi_pc pc;
2139 idetape_create_mode_sense_cmd(&pc, IDETAPE_BLOCK_DESCRIPTOR);
2140 if (idetape_queue_pc_tail(drive, &pc)) {
2141 printk(KERN_ERR "ide-tape: Can't get block descriptor\n");
2142 if (tape->blk_size == 0) {
2143 printk(KERN_WARNING "ide-tape: Cannot deal with zero "
2144 "block size, assuming 32k\n");
2145 tape->blk_size = 32768;
2149 tape->blk_size = (pc.buf[4 + 5] << 16) +
2150 (pc.buf[4 + 6] << 8) +
2152 tape->drv_write_prot = (pc.buf[2] & 0x80) >> 7;
2155 static int idetape_chrdev_open(struct inode *inode, struct file *filp)
2157 unsigned int minor = iminor(inode), i = minor & ~0xc0;
2159 idetape_tape_t *tape;
2160 struct ide_atapi_pc pc;
2163 if (i >= MAX_HWIFS * MAX_DRIVES)
2167 tape = ide_tape_chrdev_get(i);
2173 debug_log(DBG_CHRDEV, "Enter %s\n", __func__);
2176 * We really want to do nonseekable_open(inode, filp); here, but some
2177 * versions of tar incorrectly call lseek on tapes and bail out if that
2178 * fails. So we disallow pread() and pwrite(), but permit lseeks.
2180 filp->f_mode &= ~(FMODE_PREAD | FMODE_PWRITE);
2182 drive = tape->drive;
2184 filp->private_data = tape;
2186 if (test_and_set_bit(IDE_AFLAG_BUSY, &drive->atapi_flags)) {
2191 retval = idetape_wait_ready(drive, 60 * HZ);
2193 clear_bit(IDE_AFLAG_BUSY, &drive->atapi_flags);
2194 printk(KERN_ERR "ide-tape: %s: drive not ready\n", tape->name);
2198 idetape_read_position(drive);
2199 if (!test_bit(IDE_AFLAG_ADDRESS_VALID, &drive->atapi_flags))
2200 (void)idetape_rewind_tape(drive);
2202 /* Read block size and write protect status from drive. */
2203 ide_tape_get_bsize_from_bdesc(drive);
2205 /* Set write protect flag if device is opened as read-only. */
2206 if ((filp->f_flags & O_ACCMODE) == O_RDONLY)
2207 tape->write_prot = 1;
2209 tape->write_prot = tape->drv_write_prot;
2211 /* Make sure drive isn't write protected if user wants to write. */
2212 if (tape->write_prot) {
2213 if ((filp->f_flags & O_ACCMODE) == O_WRONLY ||
2214 (filp->f_flags & O_ACCMODE) == O_RDWR) {
2215 clear_bit(IDE_AFLAG_BUSY, &drive->atapi_flags);
2221 /* Lock the tape drive door so user can't eject. */
2222 if (tape->chrdev_dir == IDETAPE_DIR_NONE) {
2223 if (idetape_create_prevent_cmd(drive, &pc, 1)) {
2224 if (!idetape_queue_pc_tail(drive, &pc)) {
2225 if (tape->door_locked != DOOR_EXPLICITLY_LOCKED)
2226 tape->door_locked = DOOR_LOCKED;
2239 static void idetape_write_release(ide_drive_t *drive, unsigned int minor)
2241 idetape_tape_t *tape = drive->driver_data;
2243 ide_tape_flush_merge_buffer(drive);
2244 tape->merge_bh = ide_tape_kmalloc_buffer(tape, 1, 0);
2245 if (tape->merge_bh != NULL) {
2246 idetape_pad_zeros(drive, tape->blk_size *
2247 (tape->user_bs_factor - 1));
2248 ide_tape_kfree_buffer(tape);
2249 tape->merge_bh = NULL;
2251 idetape_write_filemark(drive);
2252 idetape_flush_tape_buffers(drive);
2253 idetape_flush_tape_buffers(drive);
2256 static int idetape_chrdev_release(struct inode *inode, struct file *filp)
2258 struct ide_tape_obj *tape = ide_tape_f(filp);
2259 ide_drive_t *drive = tape->drive;
2260 struct ide_atapi_pc pc;
2261 unsigned int minor = iminor(inode);
2264 tape = drive->driver_data;
2266 debug_log(DBG_CHRDEV, "Enter %s\n", __func__);
2268 if (tape->chrdev_dir == IDETAPE_DIR_WRITE)
2269 idetape_write_release(drive, minor);
2270 if (tape->chrdev_dir == IDETAPE_DIR_READ) {
2272 ide_tape_discard_merge_buffer(drive, 1);
2275 if (minor < 128 && test_bit(IDE_AFLAG_MEDIUM_PRESENT, &drive->atapi_flags))
2276 (void) idetape_rewind_tape(drive);
2277 if (tape->chrdev_dir == IDETAPE_DIR_NONE) {
2278 if (tape->door_locked == DOOR_LOCKED) {
2279 if (idetape_create_prevent_cmd(drive, &pc, 0)) {
2280 if (!idetape_queue_pc_tail(drive, &pc))
2281 tape->door_locked = DOOR_UNLOCKED;
2285 clear_bit(IDE_AFLAG_BUSY, &drive->atapi_flags);
2292 * check the contents of the ATAPI IDENTIFY command results. We return:
2294 * 1 - If the tape can be supported by us, based on the information we have so
2297 * 0 - If this tape driver is not currently supported by us.
2299 static int idetape_identify_device(ide_drive_t *drive)
2301 u8 gcw[2], protocol, device_type, removable, packet_size;
2303 if (drive->id_read == 0)
2306 *((unsigned short *) &gcw) = drive->id->config;
2308 protocol = (gcw[1] & 0xC0) >> 6;
2309 device_type = gcw[1] & 0x1F;
2310 removable = !!(gcw[0] & 0x80);
2311 packet_size = gcw[0] & 0x3;
2313 /* Check that we can support this device */
2315 printk(KERN_ERR "ide-tape: Protocol (0x%02x) is not ATAPI\n",
2317 else if (device_type != 1)
2318 printk(KERN_ERR "ide-tape: Device type (0x%02x) is not set "
2319 "to tape\n", device_type);
2320 else if (!removable)
2321 printk(KERN_ERR "ide-tape: The removable flag is not set\n");
2322 else if (packet_size != 0) {
2323 printk(KERN_ERR "ide-tape: Packet size (0x%02x) is not 12"
2324 " bytes\n", packet_size);
2330 static void idetape_get_inquiry_results(ide_drive_t *drive)
2332 idetape_tape_t *tape = drive->driver_data;
2333 struct ide_atapi_pc pc;
2334 char fw_rev[6], vendor_id[10], product_id[18];
2336 idetape_create_inquiry_cmd(&pc);
2337 if (idetape_queue_pc_tail(drive, &pc)) {
2338 printk(KERN_ERR "ide-tape: %s: can't get INQUIRY results\n",
2342 memcpy(vendor_id, &pc.buf[8], 8);
2343 memcpy(product_id, &pc.buf[16], 16);
2344 memcpy(fw_rev, &pc.buf[32], 4);
2346 ide_fixstring(vendor_id, 10, 0);
2347 ide_fixstring(product_id, 18, 0);
2348 ide_fixstring(fw_rev, 6, 0);
2350 printk(KERN_INFO "ide-tape: %s <-> %s: %s %s rev %s\n",
2351 drive->name, tape->name, vendor_id, product_id, fw_rev);
2355 * Ask the tape about its various parameters. In particular, we will adjust our
2356 * data transfer buffer size to the recommended value as returned by the tape.
2358 static void idetape_get_mode_sense_results(ide_drive_t *drive)
2360 idetape_tape_t *tape = drive->driver_data;
2361 struct ide_atapi_pc pc;
2363 u8 speed, max_speed;
2365 idetape_create_mode_sense_cmd(&pc, IDETAPE_CAPABILITIES_PAGE);
2366 if (idetape_queue_pc_tail(drive, &pc)) {
2367 printk(KERN_ERR "ide-tape: Can't get tape parameters - assuming"
2368 " some default values\n");
2369 tape->blk_size = 512;
2370 put_unaligned(52, (u16 *)&tape->caps[12]);
2371 put_unaligned(540, (u16 *)&tape->caps[14]);
2372 put_unaligned(6*52, (u16 *)&tape->caps[16]);
2375 caps = pc.buf + 4 + pc.buf[3];
2377 /* convert to host order and save for later use */
2378 speed = be16_to_cpu(*(u16 *)&caps[14]);
2379 max_speed = be16_to_cpu(*(u16 *)&caps[8]);
2381 put_unaligned(max_speed, (u16 *)&caps[8]);
2382 put_unaligned(be16_to_cpu(*(u16 *)&caps[12]), (u16 *)&caps[12]);
2383 put_unaligned(speed, (u16 *)&caps[14]);
2384 put_unaligned(be16_to_cpu(*(u16 *)&caps[16]), (u16 *)&caps[16]);
2387 printk(KERN_INFO "ide-tape: %s: invalid tape speed "
2388 "(assuming 650KB/sec)\n", drive->name);
2389 put_unaligned(650, (u16 *)&caps[14]);
2392 printk(KERN_INFO "ide-tape: %s: invalid max_speed "
2393 "(assuming 650KB/sec)\n", drive->name);
2394 put_unaligned(650, (u16 *)&caps[8]);
2397 memcpy(&tape->caps, caps, 20);
2399 tape->blk_size = 512;
2400 else if (caps[7] & 0x04)
2401 tape->blk_size = 1024;
2404 #ifdef CONFIG_IDE_PROC_FS
2405 static void idetape_add_settings(ide_drive_t *drive)
2407 idetape_tape_t *tape = drive->driver_data;
2409 ide_add_setting(drive, "buffer", SETTING_READ, TYPE_SHORT, 0, 0xffff,
2410 1, 2, (u16 *)&tape->caps[16], NULL);
2411 ide_add_setting(drive, "speed", SETTING_READ, TYPE_SHORT, 0, 0xffff,
2412 1, 1, (u16 *)&tape->caps[14], NULL);
2413 ide_add_setting(drive, "buffer_size", SETTING_READ, TYPE_INT, 0, 0xffff,
2414 1, 1024, &tape->buffer_size, NULL);
2415 ide_add_setting(drive, "tdsc", SETTING_RW, TYPE_INT, IDETAPE_DSC_RW_MIN,
2416 IDETAPE_DSC_RW_MAX, 1000, HZ, &tape->best_dsc_rw_freq,
2418 ide_add_setting(drive, "dsc_overlap", SETTING_RW, TYPE_BYTE, 0, 1, 1,
2419 1, &drive->dsc_overlap, NULL);
2420 ide_add_setting(drive, "avg_speed", SETTING_READ, TYPE_INT, 0, 0xffff,
2421 1, 1, &tape->avg_speed, NULL);
2422 ide_add_setting(drive, "debug_mask", SETTING_RW, TYPE_INT, 0, 0xffff, 1,
2423 1, &tape->debug_mask, NULL);
2426 static inline void idetape_add_settings(ide_drive_t *drive) { ; }
2430 * The function below is called to:
2432 * 1. Initialize our various state variables.
2433 * 2. Ask the tape for its capabilities.
2434 * 3. Allocate a buffer which will be used for data transfer. The buffer size
2435 * is chosen based on the recommendation which we received in step 2.
2437 * Note that at this point ide.c already assigned us an irq, so that we can
2438 * queue requests here and wait for their completion.
2440 static void idetape_setup(ide_drive_t *drive, idetape_tape_t *tape, int minor)
2446 u16 *ctl = (u16 *)&tape->caps[12];
2448 drive->pc_callback = ide_tape_callback;
2450 spin_lock_init(&tape->lock);
2451 drive->dsc_overlap = 1;
2452 if (drive->hwif->host_flags & IDE_HFLAG_NO_DSC) {
2453 printk(KERN_INFO "ide-tape: %s: disabling DSC overlap\n",
2455 drive->dsc_overlap = 0;
2457 /* Seagate Travan drives do not support DSC overlap. */
2458 if (strstr(drive->id->model, "Seagate STT3401"))
2459 drive->dsc_overlap = 0;
2460 tape->minor = minor;
2461 tape->name[0] = 'h';
2462 tape->name[1] = 't';
2463 tape->name[2] = '0' + minor;
2464 tape->chrdev_dir = IDETAPE_DIR_NONE;
2465 tape->pc = tape->pc_stack;
2466 *((unsigned short *) &gcw) = drive->id->config;
2468 /* Command packet DRQ type */
2469 if (((gcw[0] & 0x60) >> 5) == 1)
2470 set_bit(IDE_AFLAG_DRQ_INTERRUPT, &drive->atapi_flags);
2472 idetape_get_inquiry_results(drive);
2473 idetape_get_mode_sense_results(drive);
2474 ide_tape_get_bsize_from_bdesc(drive);
2475 tape->user_bs_factor = 1;
2476 tape->buffer_size = *ctl * tape->blk_size;
2477 while (tape->buffer_size > 0xffff) {
2478 printk(KERN_NOTICE "ide-tape: decreasing stage size\n");
2480 tape->buffer_size = *ctl * tape->blk_size;
2482 buffer_size = tape->buffer_size;
2483 tape->pages_per_buffer = buffer_size / PAGE_SIZE;
2484 if (buffer_size % PAGE_SIZE) {
2485 tape->pages_per_buffer++;
2486 tape->excess_bh_size = PAGE_SIZE - buffer_size % PAGE_SIZE;
2489 /* select the "best" DSC read/write polling freq */
2490 speed = max(*(u16 *)&tape->caps[14], *(u16 *)&tape->caps[8]);
2492 t = (IDETAPE_FIFO_THRESHOLD * tape->buffer_size * HZ) / (speed * 1000);
2495 * Ensure that the number we got makes sense; limit it within
2496 * IDETAPE_DSC_RW_MIN and IDETAPE_DSC_RW_MAX.
2498 tape->best_dsc_rw_freq = clamp_t(unsigned long, t, IDETAPE_DSC_RW_MIN,
2499 IDETAPE_DSC_RW_MAX);
2500 printk(KERN_INFO "ide-tape: %s <-> %s: %dKBps, %d*%dkB buffer, "
2502 drive->name, tape->name, *(u16 *)&tape->caps[14],
2503 (*(u16 *)&tape->caps[16] * 512) / tape->buffer_size,
2504 tape->buffer_size / 1024,
2505 tape->best_dsc_rw_freq * 1000 / HZ,
2506 drive->using_dma ? ", DMA":"");
2508 idetape_add_settings(drive);
2511 static void ide_tape_remove(ide_drive_t *drive)
2513 idetape_tape_t *tape = drive->driver_data;
2515 ide_proc_unregister_driver(drive, tape->driver);
2517 ide_unregister_region(tape->disk);
2522 static void ide_tape_release(struct kref *kref)
2524 struct ide_tape_obj *tape = to_ide_tape(kref);
2525 ide_drive_t *drive = tape->drive;
2526 struct gendisk *g = tape->disk;
2528 BUG_ON(tape->merge_bh_size);
2530 drive->dsc_overlap = 0;
2531 drive->driver_data = NULL;
2532 device_destroy(idetape_sysfs_class, MKDEV(IDETAPE_MAJOR, tape->minor));
2533 device_destroy(idetape_sysfs_class,
2534 MKDEV(IDETAPE_MAJOR, tape->minor + 128));
2535 idetape_devs[tape->minor] = NULL;
2536 g->private_data = NULL;
2541 #ifdef CONFIG_IDE_PROC_FS
2542 static int proc_idetape_read_name
2543 (char *page, char **start, off_t off, int count, int *eof, void *data)
2545 ide_drive_t *drive = (ide_drive_t *) data;
2546 idetape_tape_t *tape = drive->driver_data;
2550 len = sprintf(out, "%s\n", tape->name);
2551 PROC_IDE_READ_RETURN(page, start, off, count, eof, len);
2554 static ide_proc_entry_t idetape_proc[] = {
2555 { "capacity", S_IFREG|S_IRUGO, proc_ide_read_capacity, NULL },
2556 { "name", S_IFREG|S_IRUGO, proc_idetape_read_name, NULL },
2557 { NULL, 0, NULL, NULL }
2561 static int ide_tape_probe(ide_drive_t *);
2563 static ide_driver_t idetape_driver = {
2565 .owner = THIS_MODULE,
2567 .bus = &ide_bus_type,
2569 .probe = ide_tape_probe,
2570 .remove = ide_tape_remove,
2571 .version = IDETAPE_VERSION,
2573 .supports_dsc_overlap = 1,
2574 .do_request = idetape_do_request,
2575 .end_request = idetape_end_request,
2576 .error = __ide_error,
2577 #ifdef CONFIG_IDE_PROC_FS
2578 .proc = idetape_proc,
2582 /* Our character device supporting functions, passed to register_chrdev. */
2583 static const struct file_operations idetape_fops = {
2584 .owner = THIS_MODULE,
2585 .read = idetape_chrdev_read,
2586 .write = idetape_chrdev_write,
2587 .ioctl = idetape_chrdev_ioctl,
2588 .open = idetape_chrdev_open,
2589 .release = idetape_chrdev_release,
2592 static int idetape_open(struct inode *inode, struct file *filp)
2594 struct gendisk *disk = inode->i_bdev->bd_disk;
2595 struct ide_tape_obj *tape;
2597 tape = ide_tape_get(disk);
2604 static int idetape_release(struct inode *inode, struct file *filp)
2606 struct gendisk *disk = inode->i_bdev->bd_disk;
2607 struct ide_tape_obj *tape = ide_tape_g(disk);
2614 static int idetape_ioctl(struct inode *inode, struct file *file,
2615 unsigned int cmd, unsigned long arg)
2617 struct block_device *bdev = inode->i_bdev;
2618 struct ide_tape_obj *tape = ide_tape_g(bdev->bd_disk);
2619 ide_drive_t *drive = tape->drive;
2620 int err = generic_ide_ioctl(drive, file, bdev, cmd, arg);
2622 err = idetape_blkdev_ioctl(drive, cmd, arg);
2626 static struct block_device_operations idetape_block_ops = {
2627 .owner = THIS_MODULE,
2628 .open = idetape_open,
2629 .release = idetape_release,
2630 .ioctl = idetape_ioctl,
2633 static int ide_tape_probe(ide_drive_t *drive)
2635 idetape_tape_t *tape;
2639 if (!strstr("ide-tape", drive->driver_req))
2641 if (!drive->present)
2643 if (drive->media != ide_tape)
2645 if (!idetape_identify_device(drive)) {
2646 printk(KERN_ERR "ide-tape: %s: not supported by this version of"
2647 " the driver\n", drive->name);
2650 tape = kzalloc(sizeof(idetape_tape_t), GFP_KERNEL);
2652 printk(KERN_ERR "ide-tape: %s: Can't allocate a tape struct\n",
2657 g = alloc_disk(1 << PARTN_BITS);
2661 ide_init_disk(g, drive);
2663 ide_proc_register_driver(drive, &idetape_driver);
2665 kref_init(&tape->kref);
2667 tape->drive = drive;
2668 tape->driver = &idetape_driver;
2671 g->private_data = &tape->driver;
2673 drive->driver_data = tape;
2675 mutex_lock(&idetape_ref_mutex);
2676 for (minor = 0; idetape_devs[minor]; minor++)
2678 idetape_devs[minor] = tape;
2679 mutex_unlock(&idetape_ref_mutex);
2681 idetape_setup(drive, tape, minor);
2683 device_create_drvdata(idetape_sysfs_class, &drive->gendev,
2684 MKDEV(IDETAPE_MAJOR, minor), NULL,
2686 device_create_drvdata(idetape_sysfs_class, &drive->gendev,
2687 MKDEV(IDETAPE_MAJOR, minor + 128), NULL,
2690 g->fops = &idetape_block_ops;
2691 ide_register_region(g);
2701 static void __exit idetape_exit(void)
2703 driver_unregister(&idetape_driver.gen_driver);
2704 class_destroy(idetape_sysfs_class);
2705 unregister_chrdev(IDETAPE_MAJOR, "ht");
2708 static int __init idetape_init(void)
2711 idetape_sysfs_class = class_create(THIS_MODULE, "ide_tape");
2712 if (IS_ERR(idetape_sysfs_class)) {
2713 idetape_sysfs_class = NULL;
2714 printk(KERN_ERR "Unable to create sysfs class for ide tapes\n");
2719 if (register_chrdev(IDETAPE_MAJOR, "ht", &idetape_fops)) {
2720 printk(KERN_ERR "ide-tape: Failed to register chrdev"
2723 goto out_free_class;
2726 error = driver_register(&idetape_driver.gen_driver);
2728 goto out_free_driver;
2733 driver_unregister(&idetape_driver.gen_driver);
2735 class_destroy(idetape_sysfs_class);
2740 MODULE_ALIAS("ide:*m-tape*");
2741 module_init(idetape_init);
2742 module_exit(idetape_exit);
2743 MODULE_ALIAS_CHARDEV_MAJOR(IDETAPE_MAJOR);
2744 MODULE_DESCRIPTION("ATAPI Streaming TAPE Driver");
2745 MODULE_LICENSE("GPL");