blob: 055f52e1ea0ebddf937b3f75fe5e0cff794a0981 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Borislav Petkov5ce78af2008-02-02 19:56:48 +01002 * IDE ATAPI streaming tape driver.
3 *
Bartlomiej Zolnierkiewicz59bca8c2008-02-01 23:09:33 +01004 * Copyright (C) 1995-1999 Gadi Oxman <gadio@netvision.net.il>
5 * Copyright (C) 2003-2005 Bartlomiej Zolnierkiewicz
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 * 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.
10 *
11 * It is hereby placed under the terms of the GNU general public license.
12 * (See linux/COPYING).
Linus Torvalds1da177e2005-04-16 15:20:36 -070013 *
Borislav Petkov5ce78af2008-02-02 19:56:48 +010014 * For a historical changelog see
15 * Documentation/ide/ChangeLog.ide-tape.1995-2002
Linus Torvalds1da177e2005-04-16 15:20:36 -070016 */
17
Bartlomiej Zolnierkiewicz51509ee2008-10-10 22:39:34 +020018#define DRV_NAME "ide-tape"
19
Borislav Petkovdfe79932008-02-06 02:57:55 +010020#define IDETAPE_VERSION "1.20"
Linus Torvalds1da177e2005-04-16 15:20:36 -070021
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#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>
28#include <linux/mm.h>
29#include <linux/interrupt.h>
Marcelo Feitoza Parisi9bae1ff2006-03-28 01:56:46 -080030#include <linux/jiffies.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#include <linux/major.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070032#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>
Arjan van de Vencf8b8972006-03-23 03:00:45 -080040#include <linux/mutex.h>
Borislav Petkov90699ce2008-02-02 19:56:50 +010041#include <scsi/scsi.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070042
43#include <asm/byteorder.h>
Borislav Petkovc837cfa2008-02-06 02:57:54 +010044#include <linux/irq.h>
45#include <linux/uaccess.h>
46#include <linux/io.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070047#include <asm/unaligned.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070048#include <linux/mtio.h>
49
Borislav Petkov8004a8c2008-02-06 02:57:51 +010050enum {
51 /* output errors only */
52 DBG_ERR = (1 << 0),
53 /* output all sense key/asc */
54 DBG_SENSE = (1 << 1),
55 /* info regarding all chrdev-related procedures */
56 DBG_CHRDEV = (1 << 2),
57 /* all remaining procedures */
58 DBG_PROCS = (1 << 3),
Borislav Petkov8004a8c2008-02-06 02:57:51 +010059};
60
61/* define to see debug info */
62#define IDETAPE_DEBUG_LOG 0
63
64#if IDETAPE_DEBUG_LOG
65#define debug_log(lvl, fmt, args...) \
66{ \
67 if (tape->debug_mask & lvl) \
68 printk(KERN_INFO "ide-tape: " fmt, ## args); \
69}
70#else
71#define debug_log(lvl, fmt, args...) do {} while (0)
72#endif
73
Linus Torvalds1da177e2005-04-16 15:20:36 -070074/**************************** Tunable parameters *****************************/
Linus Torvalds1da177e2005-04-16 15:20:36 -070075/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +010076 * After each failed packet command we issue a request sense command and retry
77 * the packet command IDETAPE_MAX_PC_RETRIES times.
Linus Torvalds1da177e2005-04-16 15:20:36 -070078 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +010079 * Setting IDETAPE_MAX_PC_RETRIES to 0 will disable retries.
Linus Torvalds1da177e2005-04-16 15:20:36 -070080 */
81#define IDETAPE_MAX_PC_RETRIES 3
82
83/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +010084 * 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.
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 */
Borislav Petkov3c98bf32008-02-06 02:57:53 +010089#define IDETAPE_FIFO_THRESHOLD 2
Linus Torvalds1da177e2005-04-16 15:20:36 -070090
91/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +010092 * DSC polling parameters.
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +010094 * 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:
Linus Torvalds1da177e2005-04-16 15:20:36 -070096 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +010097 * 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.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100103 * 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).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100112 * 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.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 */
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100115
116/* DSC timings. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117#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 */
124
125/*************************** End of tunable parameters ***********************/
126
Borislav Petkov54abf372008-02-06 02:57:52 +0100127/* tape directions */
128enum {
129 IDETAPE_DIR_NONE = (1 << 0),
130 IDETAPE_DIR_READ = (1 << 1),
131 IDETAPE_DIR_WRITE = (1 << 2),
132};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133
Borislav Petkov03056b92008-04-18 00:46:26 +0200134/* Tape door status */
135#define DOOR_UNLOCKED 0
136#define DOOR_LOCKED 1
137#define DOOR_EXPLICITLY_LOCKED 2
138
139/* Some defines for the SPACE command */
140#define IDETAPE_SPACE_OVER_FILEMARK 1
141#define IDETAPE_SPACE_TO_EOD 3
142
143/* Some defines for the LOAD UNLOAD command */
144#define IDETAPE_LU_LOAD_MASK 1
145#define IDETAPE_LU_RETENSION_MASK 2
146#define IDETAPE_LU_EOT_MASK 4
147
Borislav Petkov03056b92008-04-18 00:46:26 +0200148/* Structures related to the SELECT SENSE / MODE SENSE packet commands. */
149#define IDETAPE_BLOCK_DESCRIPTOR 0
150#define IDETAPE_CAPABILITIES_PAGE 0x2a
151
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100153 * Most of our global data which we need to save even as we leave the driver due
154 * to an interrupt or a timer event is stored in the struct defined below.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 */
156typedef struct ide_tape_obj {
Bartlomiej Zolnierkiewicz7f3c8682009-01-06 17:20:53 +0100157 ide_drive_t *drive;
158 struct ide_driver *driver;
159 struct gendisk *disk;
Bartlomiej Zolnierkiewicz8fed4362009-02-25 20:28:24 +0100160 struct device dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161
Bartlomiej Zolnierkiewicz2e8a6f82008-10-10 22:39:36 +0200162 /* used by REQ_IDETAPE_{READ,WRITE} requests */
163 struct ide_atapi_pc queued_pc;
Bartlomiej Zolnierkiewicz394a4c22008-10-10 22:39:35 +0200164
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 /*
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100166 * DSC polling variables.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100168 * While polling for DSC we use postponed_rq to postpone the current
169 * request so that ide.c will be able to service pending requests on the
170 * other device. Note that at most we will have only one DSC (usually
Borislav Petkov5bd50dc2008-04-27 15:38:28 +0200171 * data transfer) request in the device request queue.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 */
173 struct request *postponed_rq;
174 /* The time in which we started polling for DSC */
175 unsigned long dsc_polling_start;
176 /* Timer used to poll for dsc */
177 struct timer_list dsc_timer;
178 /* Read/Write dsc polling frequency */
Borislav Petkov54bb2072008-02-06 02:57:52 +0100179 unsigned long best_dsc_rw_freq;
180 unsigned long dsc_poll_freq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 unsigned long dsc_timeout;
182
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100183 /* Read position information */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 u8 partition;
185 /* Current block */
Borislav Petkov54bb2072008-02-06 02:57:52 +0100186 unsigned int first_frame;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100188 /* Last error information */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 u8 sense_key, asc, ascq;
190
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100191 /* Character device operation */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 unsigned int minor;
193 /* device name */
194 char name[4];
195 /* Current character device data transfer direction */
Borislav Petkov54abf372008-02-06 02:57:52 +0100196 u8 chrdev_dir;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197
Borislav Petkov54bb2072008-02-06 02:57:52 +0100198 /* tape block size, usually 512 or 1024 bytes */
199 unsigned short blk_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 int user_bs_factor;
Borislav Petkovb6422012008-02-02 19:56:49 +0100201
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 /* Copy of the tape's Capabilities and Mechanical Page */
Borislav Petkovb6422012008-02-02 19:56:49 +0100203 u8 caps[20];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204
205 /*
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100206 * Active data transfer request parameters.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100208 * At most, there is only one ide-tape originated data transfer request
209 * in the device request queue. This allows ide.c to easily service
210 * requests from the other device when we postpone our active request.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 */
Borislav Petkov83042b22008-04-27 15:38:27 +0200212
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100213 /* Data buffer size chosen based on the tape's recommendation */
Borislav Petkovf73850a2008-04-27 15:38:33 +0200214 int buffer_size;
Tejun Heo963da552009-04-19 08:46:02 +0900215 /* Staging buffer of buffer_size bytes */
216 void *buf;
217 /* The read/write cursor */
218 void *cur;
219 /* The number of valid bytes in buf */
220 size_t valid;
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100221
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100222 /* Measures average tape speed */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 unsigned long avg_time;
224 int avg_size;
225 int avg_speed;
226
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 /* the door is currently locked */
228 int door_locked;
229 /* the tape hardware is write protected */
230 char drv_write_prot;
231 /* the tape is write protected (hardware or opened as read-only) */
232 char write_prot;
233
Borislav Petkov8004a8c2008-02-06 02:57:51 +0100234 u32 debug_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235} idetape_tape_t;
236
Arjan van de Vencf8b8972006-03-23 03:00:45 -0800237static DEFINE_MUTEX(idetape_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238
Will Dysond5dee802005-09-16 02:55:07 -0700239static struct class *idetape_sysfs_class;
240
Bartlomiej Zolnierkiewicz8fed4362009-02-25 20:28:24 +0100241static void ide_tape_release(struct device *);
Bartlomiej Zolnierkiewicz08da5912008-07-24 22:53:15 +0200242
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243static struct ide_tape_obj *ide_tape_get(struct gendisk *disk)
244{
245 struct ide_tape_obj *tape = NULL;
246
Arjan van de Vencf8b8972006-03-23 03:00:45 -0800247 mutex_lock(&idetape_ref_mutex);
Borislav Petkov5aeddf92008-10-13 21:39:34 +0200248 tape = ide_drv_g(disk, ide_tape_obj);
Bartlomiej Zolnierkiewicz08da5912008-07-24 22:53:15 +0200249 if (tape) {
Bartlomiej Zolnierkiewiczd3e33ff2008-08-05 18:16:59 +0200250 if (ide_device_get(tape->drive))
Bartlomiej Zolnierkiewicz08da5912008-07-24 22:53:15 +0200251 tape = NULL;
Bartlomiej Zolnierkiewiczd3e33ff2008-08-05 18:16:59 +0200252 else
Bartlomiej Zolnierkiewicz8fed4362009-02-25 20:28:24 +0100253 get_device(&tape->dev);
Bartlomiej Zolnierkiewicz08da5912008-07-24 22:53:15 +0200254 }
Arjan van de Vencf8b8972006-03-23 03:00:45 -0800255 mutex_unlock(&idetape_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 return tape;
257}
258
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259static void ide_tape_put(struct ide_tape_obj *tape)
260{
Bartlomiej Zolnierkiewiczd3e33ff2008-08-05 18:16:59 +0200261 ide_drive_t *drive = tape->drive;
262
Arjan van de Vencf8b8972006-03-23 03:00:45 -0800263 mutex_lock(&idetape_ref_mutex);
Bartlomiej Zolnierkiewicz8fed4362009-02-25 20:28:24 +0100264 put_device(&tape->dev);
Bartlomiej Zolnierkiewiczd3e33ff2008-08-05 18:16:59 +0200265 ide_device_put(drive);
Arjan van de Vencf8b8972006-03-23 03:00:45 -0800266 mutex_unlock(&idetape_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267}
268
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100270 * The variables below are used for the character device interface. Additional
271 * state variables are defined in our ide_drive_t structure.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100273static struct ide_tape_obj *idetape_devs[MAX_HWIFS * MAX_DRIVES];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275static struct ide_tape_obj *ide_tape_chrdev_get(unsigned int i)
276{
277 struct ide_tape_obj *tape = NULL;
278
Arjan van de Vencf8b8972006-03-23 03:00:45 -0800279 mutex_lock(&idetape_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 tape = idetape_devs[i];
281 if (tape)
Bartlomiej Zolnierkiewicz8fed4362009-02-25 20:28:24 +0100282 get_device(&tape->dev);
Arjan van de Vencf8b8972006-03-23 03:00:45 -0800283 mutex_unlock(&idetape_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 return tape;
285}
286
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287/*
Borislav Petkov1b5db432008-02-02 19:56:48 +0100288 * called on each failed packet command retry to analyze the request sense. We
289 * currently do not utilize this information.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 */
Borislav Petkov1b5db432008-02-02 19:56:48 +0100291static void idetape_analyze_error(ide_drive_t *drive, u8 *sense)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292{
293 idetape_tape_t *tape = drive->driver_data;
Bartlomiej Zolnierkiewicz5e2040fd2009-03-27 12:46:34 +0100294 struct ide_atapi_pc *pc = drive->failed_pc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295
Borislav Petkov1b5db432008-02-02 19:56:48 +0100296 tape->sense_key = sense[2] & 0xF;
297 tape->asc = sense[12];
298 tape->ascq = sense[13];
Borislav Petkov8004a8c2008-02-06 02:57:51 +0100299
300 debug_log(DBG_ERR, "pc = %x, sense key = %x, asc = %x, ascq = %x\n",
301 pc->c[0], tape->sense_key, tape->asc, tape->ascq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302
Borislav Petkovd236d742008-04-18 00:46:27 +0200303 /* Correct pc->xferred by asking the tape. */
Tejun Heo21d9c5d2009-04-19 08:46:02 +0900304 if (pc->flags & PC_FLAG_DMA_ERROR)
Borislav Petkovd236d742008-04-18 00:46:27 +0200305 pc->xferred = pc->req_xfer -
Borislav Petkov54bb2072008-02-06 02:57:52 +0100306 tape->blk_size *
Harvey Harrison5d0cc8a2008-07-15 21:21:41 +0200307 get_unaligned_be32(&sense[3]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308
309 /*
310 * If error was the result of a zero-length read or write command,
311 * with sense key=5, asc=0x22, ascq=0, let it slide. Some drives
312 * (i.e. Seagate STT3401A Travan) don't support 0-length read/writes.
313 */
Borislav Petkov90699ce2008-02-02 19:56:50 +0100314 if ((pc->c[0] == READ_6 || pc->c[0] == WRITE_6)
Borislav Petkov1b5db432008-02-02 19:56:48 +0100315 /* length == 0 */
316 && pc->c[4] == 0 && pc->c[3] == 0 && pc->c[2] == 0) {
317 if (tape->sense_key == 5) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 /* don't report an error, everything's ok */
319 pc->error = 0;
320 /* don't retry read/write */
Borislav Petkov346331f2008-04-18 00:46:26 +0200321 pc->flags |= PC_FLAG_ABORT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 }
323 }
Borislav Petkov90699ce2008-02-02 19:56:50 +0100324 if (pc->c[0] == READ_6 && (sense[2] & 0x80)) {
Bartlomiej Zolnierkiewiczc152cc12009-03-27 12:46:34 +0100325 pc->error = IDE_DRV_ERROR_FILEMARK;
Borislav Petkov346331f2008-04-18 00:46:26 +0200326 pc->flags |= PC_FLAG_ABORT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 }
Borislav Petkov90699ce2008-02-02 19:56:50 +0100328 if (pc->c[0] == WRITE_6) {
Borislav Petkov1b5db432008-02-02 19:56:48 +0100329 if ((sense[2] & 0x40) || (tape->sense_key == 0xd
330 && tape->asc == 0x0 && tape->ascq == 0x2)) {
Bartlomiej Zolnierkiewiczc152cc12009-03-27 12:46:34 +0100331 pc->error = IDE_DRV_ERROR_EOD;
Borislav Petkov346331f2008-04-18 00:46:26 +0200332 pc->flags |= PC_FLAG_ABORT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 }
334 }
Borislav Petkov90699ce2008-02-02 19:56:50 +0100335 if (pc->c[0] == READ_6 || pc->c[0] == WRITE_6) {
Borislav Petkov1b5db432008-02-02 19:56:48 +0100336 if (tape->sense_key == 8) {
Bartlomiej Zolnierkiewiczc152cc12009-03-27 12:46:34 +0100337 pc->error = IDE_DRV_ERROR_EOD;
Borislav Petkov346331f2008-04-18 00:46:26 +0200338 pc->flags |= PC_FLAG_ABORT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 }
Borislav Petkov346331f2008-04-18 00:46:26 +0200340 if (!(pc->flags & PC_FLAG_ABORT) &&
Borislav Petkovd236d742008-04-18 00:46:27 +0200341 pc->xferred)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 pc->retries = IDETAPE_MAX_PC_RETRIES + 1;
343 }
344}
345
Bartlomiej Zolnierkiewiczb14c7212008-10-13 21:39:30 +0200346static void ide_tape_handle_dsc(ide_drive_t *);
347
Bartlomiej Zolnierkiewicz03a2faa2009-03-27 12:46:36 +0100348static int ide_tape_callback(ide_drive_t *drive, int dsc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349{
350 idetape_tape_t *tape = drive->driver_data;
Bartlomiej Zolnierkiewicz2b9efba2008-10-13 21:39:31 +0200351 struct ide_atapi_pc *pc = drive->pc;
Bartlomiej Zolnierkiewicz313afea2009-03-27 12:46:34 +0100352 struct request *rq = drive->hwif->rq;
Bartlomiej Zolnierkiewicz5985e6a2008-07-15 21:21:55 +0200353 int uptodate = pc->error ? 0 : 1;
Bartlomiej Zolnierkiewicz313afea2009-03-27 12:46:34 +0100354 int err = uptodate ? 0 : IDE_DRV_ERROR_GENERAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355
Borislav Petkov8004a8c2008-02-06 02:57:51 +0100356 debug_log(DBG_PROCS, "Enter %s\n", __func__);
357
Bartlomiej Zolnierkiewiczb14c7212008-10-13 21:39:30 +0200358 if (dsc)
359 ide_tape_handle_dsc(drive);
360
Bartlomiej Zolnierkiewicz5e2040fd2009-03-27 12:46:34 +0100361 if (drive->failed_pc == pc)
362 drive->failed_pc = NULL;
Bartlomiej Zolnierkiewiczdd2e9a02008-07-15 21:22:01 +0200363
Bartlomiej Zolnierkiewicz5985e6a2008-07-15 21:21:55 +0200364 if (pc->c[0] == REQUEST_SENSE) {
365 if (uptodate)
366 idetape_analyze_error(drive, pc->buf);
367 else
368 printk(KERN_ERR "ide-tape: Error in REQUEST SENSE "
369 "itself - Aborting request!\n");
370 } else if (pc->c[0] == READ_6 || pc->c[0] == WRITE_6) {
Bartlomiej Zolnierkiewicz5985e6a2008-07-15 21:21:55 +0200371 int blocks = pc->xferred / tape->blk_size;
372
373 tape->avg_size += blocks * tape->blk_size;
374
375 if (time_after_eq(jiffies, tape->avg_time + HZ)) {
376 tape->avg_speed = tape->avg_size * HZ /
377 (jiffies - tape->avg_time) / 1024;
378 tape->avg_size = 0;
379 tape->avg_time = jiffies;
380 }
381
382 tape->first_frame += blocks;
Tejun Heo21d9c5d2009-04-19 08:46:02 +0900383 rq->data_len -= blocks * tape->blk_size;
Bartlomiej Zolnierkiewicz5985e6a2008-07-15 21:21:55 +0200384
Bartlomiej Zolnierkiewicz313afea2009-03-27 12:46:34 +0100385 if (pc->error) {
386 uptodate = 0;
387 err = pc->error;
388 }
Bartlomiej Zolnierkiewicz5985e6a2008-07-15 21:21:55 +0200389 } else if (pc->c[0] == READ_POSITION && uptodate) {
Bartlomiej Zolnierkiewicz2b9efba2008-10-13 21:39:31 +0200390 u8 *readpos = pc->buf;
Bartlomiej Zolnierkiewicz5985e6a2008-07-15 21:21:55 +0200391
392 debug_log(DBG_SENSE, "BOP - %s\n",
393 (readpos[0] & 0x80) ? "Yes" : "No");
394 debug_log(DBG_SENSE, "EOP - %s\n",
395 (readpos[0] & 0x40) ? "Yes" : "No");
396
397 if (readpos[0] & 0x4) {
398 printk(KERN_INFO "ide-tape: Block location is unknown"
399 "to the tape\n");
Borislav Petkov49d80782009-06-07 15:37:06 +0200400 clear_bit(ilog2(IDE_AFLAG_ADDRESS_VALID),
401 &drive->atapi_flags);
Bartlomiej Zolnierkiewicz5985e6a2008-07-15 21:21:55 +0200402 uptodate = 0;
Bartlomiej Zolnierkiewicz313afea2009-03-27 12:46:34 +0100403 err = IDE_DRV_ERROR_GENERAL;
Bartlomiej Zolnierkiewicz5985e6a2008-07-15 21:21:55 +0200404 } else {
405 debug_log(DBG_SENSE, "Block Location - %u\n",
Harvey Harrisoncd740ab2008-07-24 22:53:33 +0200406 be32_to_cpup((__be32 *)&readpos[4]));
Bartlomiej Zolnierkiewicz5985e6a2008-07-15 21:21:55 +0200407
408 tape->partition = readpos[1];
Harvey Harrisoncd740ab2008-07-24 22:53:33 +0200409 tape->first_frame = be32_to_cpup((__be32 *)&readpos[4]);
Borislav Petkov49d80782009-06-07 15:37:06 +0200410 set_bit(ilog2(IDE_AFLAG_ADDRESS_VALID),
411 &drive->atapi_flags);
Bartlomiej Zolnierkiewicz5985e6a2008-07-15 21:21:55 +0200412 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 }
Bartlomiej Zolnierkiewicz5985e6a2008-07-15 21:21:55 +0200414
Bartlomiej Zolnierkiewicz313afea2009-03-27 12:46:34 +0100415 rq->errors = err;
416
Bartlomiej Zolnierkiewicz03a2faa2009-03-27 12:46:36 +0100417 return uptodate;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418}
419
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100421 * Postpone the current request so that ide.c will be able to service requests
Bartlomiej Zolnierkiewiczb65fac32009-01-06 17:20:50 +0100422 * from another device on the same port while we are polling for DSC.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100424static void idetape_postpone_request(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425{
426 idetape_tape_t *tape = drive->driver_data;
427
Borislav Petkov8004a8c2008-02-06 02:57:51 +0100428 debug_log(DBG_PROCS, "Enter %s\n", __func__);
429
Bartlomiej Zolnierkiewiczb65fac32009-01-06 17:20:50 +0100430 tape->postponed_rq = drive->hwif->rq;
431
Borislav Petkov54bb2072008-02-06 02:57:52 +0100432 ide_stall_queue(drive, tape->dsc_poll_freq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433}
434
Bartlomiej Zolnierkiewicz74e63e742008-07-15 21:22:01 +0200435static void ide_tape_handle_dsc(ide_drive_t *drive)
436{
437 idetape_tape_t *tape = drive->driver_data;
438
439 /* Media access command */
440 tape->dsc_polling_start = jiffies;
441 tape->dsc_poll_freq = IDETAPE_DSC_MA_FAST;
442 tape->dsc_timeout = jiffies + IDETAPE_DSC_MA_TIMEOUT;
443 /* Allow ide.c to handle other requests */
444 idetape_postpone_request(drive);
445}
446
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100448 * Packet Command Interface
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 *
Bartlomiej Zolnierkiewicz2b9efba2008-10-13 21:39:31 +0200450 * The current Packet Command is available in drive->pc, and will not change
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100451 * until we finish handling it. Each packet command is associated with a
452 * callback function that will be called when the command is finished.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100454 * The handling will be done in three stages:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 *
Bartlomiej Zolnierkiewiczb788ee92009-03-27 12:46:46 +0100456 * 1. ide_tape_issue_pc will send the packet command to the drive, and will set
Bartlomiej Zolnierkiewiczaa5d2de72008-10-13 21:39:32 +0200457 * the interrupt handler to ide_pc_intr.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 *
Bartlomiej Zolnierkiewiczaa5d2de72008-10-13 21:39:32 +0200459 * 2. On each interrupt, ide_pc_intr will be called. This step will be
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100460 * repeated until the device signals us that no more interrupts will be issued.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100462 * 3. ATAPI Tape media access commands have immediate status with a delayed
463 * process. In case of a successful initiation of a media access packet command,
464 * the DSC bit will be set when the actual execution of the command is finished.
465 * Since the tape drive will not issue an interrupt, we have to poll for this
466 * event. In this case, we define the request as "low priority request" by
467 * setting rq_status to IDETAPE_RQ_POSTPONED, set a timer to poll for DSC and
468 * exit the driver.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100470 * ide.c will then give higher priority to requests which originate from the
471 * other device, until will change rq_status to RQ_ACTIVE.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100473 * 4. When the packet command is finished, it will be checked for errors.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100475 * 5. In case an error was found, we queue a request sense packet command in
476 * front of the request queue and retry the operation up to
477 * IDETAPE_MAX_PC_RETRIES times.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100479 * 6. In case no error was found, or we decided to give up and not to retry
480 * again, the callback function will be called and then we will handle the next
481 * request.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483
Bartlomiej Zolnierkiewiczb788ee92009-03-27 12:46:46 +0100484static ide_startstop_t ide_tape_issue_pc(ide_drive_t *drive,
485 struct ide_cmd *cmd,
486 struct ide_atapi_pc *pc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 idetape_tape_t *tape = drive->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489
Bartlomiej Zolnierkiewicz5e2040fd2009-03-27 12:46:34 +0100490 if (drive->failed_pc == NULL && pc->c[0] != REQUEST_SENSE)
491 drive->failed_pc = pc;
Bartlomiej Zolnierkiewicz2b9efba2008-10-13 21:39:31 +0200492
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 /* Set the current packet command */
Bartlomiej Zolnierkiewicz2b9efba2008-10-13 21:39:31 +0200494 drive->pc = pc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495
496 if (pc->retries > IDETAPE_MAX_PC_RETRIES ||
Borislav Petkov346331f2008-04-18 00:46:26 +0200497 (pc->flags & PC_FLAG_ABORT)) {
Tejun Heob3071d12009-04-19 08:46:02 +0900498 unsigned int done = blk_rq_bytes(drive->hwif->rq);
499
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 /*
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100501 * We will "abort" retrying a packet command in case legitimate
502 * error code was received (crossing a filemark, or end of the
503 * media, for example).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 */
Borislav Petkov346331f2008-04-18 00:46:26 +0200505 if (!(pc->flags & PC_FLAG_ABORT)) {
Borislav Petkov90699ce2008-02-02 19:56:50 +0100506 if (!(pc->c[0] == TEST_UNIT_READY &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 tape->sense_key == 2 && tape->asc == 4 &&
508 (tape->ascq == 1 || tape->ascq == 8))) {
509 printk(KERN_ERR "ide-tape: %s: I/O error, "
510 "pc = %2x, key = %2x, "
511 "asc = %2x, ascq = %2x\n",
512 tape->name, pc->c[0],
513 tape->sense_key, tape->asc,
514 tape->ascq);
515 }
516 /* Giving up */
Bartlomiej Zolnierkiewiczc152cc12009-03-27 12:46:34 +0100517 pc->error = IDE_DRV_ERROR_GENERAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 }
Tejun Heob3071d12009-04-19 08:46:02 +0900519
Bartlomiej Zolnierkiewicz5e2040fd2009-03-27 12:46:34 +0100520 drive->failed_pc = NULL;
Bartlomiej Zolnierkiewiczb14c7212008-10-13 21:39:30 +0200521 drive->pc_callback(drive, 0);
Tejun Heob3071d12009-04-19 08:46:02 +0900522 ide_complete_rq(drive, -EIO, done);
Bartlomiej Zolnierkiewicz92f5daf2008-07-15 21:21:55 +0200523 return ide_stopped;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 }
Borislav Petkov8004a8c2008-02-06 02:57:51 +0100525 debug_log(DBG_SENSE, "Retry #%d, cmd = %02X\n", pc->retries, pc->c[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526
527 pc->retries++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528
Bartlomiej Zolnierkiewiczb788ee92009-03-27 12:46:46 +0100529 return ide_issue_pc(drive, cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530}
531
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100532/* A mode sense command is used to "sense" tape parameters. */
Borislav Petkovd236d742008-04-18 00:46:27 +0200533static void idetape_create_mode_sense_cmd(struct ide_atapi_pc *pc, u8 page_code)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534{
Bartlomiej Zolnierkiewicz7bf74202008-10-10 22:39:37 +0200535 ide_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +0100536 pc->c[0] = MODE_SENSE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 if (page_code != IDETAPE_BLOCK_DESCRIPTOR)
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100538 /* DBD = 1 - Don't return block descriptors */
539 pc->c[1] = 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 pc->c[2] = page_code;
541 /*
542 * Changed pc->c[3] to 0 (255 will at best return unused info).
543 *
544 * For SCSI this byte is defined as subpage instead of high byte
545 * of length and some IDE drives seem to interpret it this way
546 * and return an error when 255 is used.
547 */
548 pc->c[3] = 0;
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100549 /* We will just discard data in that case */
550 pc->c[4] = 255;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 if (page_code == IDETAPE_BLOCK_DESCRIPTOR)
Borislav Petkovd236d742008-04-18 00:46:27 +0200552 pc->req_xfer = 12;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 else if (page_code == IDETAPE_CAPABILITIES_PAGE)
Borislav Petkovd236d742008-04-18 00:46:27 +0200554 pc->req_xfer = 24;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 else
Borislav Petkovd236d742008-04-18 00:46:27 +0200556 pc->req_xfer = 50;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557}
558
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100559static ide_startstop_t idetape_media_access_finished(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560{
Bartlomiej Zolnierkiewiczb73c7ee2008-07-23 19:55:52 +0200561 ide_hwif_t *hwif = drive->hwif;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 idetape_tape_t *tape = drive->driver_data;
Bartlomiej Zolnierkiewicz2b9efba2008-10-13 21:39:31 +0200563 struct ide_atapi_pc *pc = drive->pc;
Bartlomiej Zolnierkiewicz22c525b2008-01-25 22:17:11 +0100564 u8 stat;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565
Bartlomiej Zolnierkiewicz374e0422008-07-23 19:55:56 +0200566 stat = hwif->tp_ops->read_status(hwif);
Bartlomiej Zolnierkiewiczc47137a2008-02-06 02:57:51 +0100567
Bartlomiej Zolnierkiewicz3a7d2482008-10-10 22:39:21 +0200568 if (stat & ATA_DSC) {
569 if (stat & ATA_ERR) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 /* Error detected */
Borislav Petkov90699ce2008-02-02 19:56:50 +0100571 if (pc->c[0] != TEST_UNIT_READY)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 printk(KERN_ERR "ide-tape: %s: I/O error, ",
573 tape->name);
574 /* Retry operation */
Borislav Petkov6b544fc2009-04-19 07:00:42 +0900575 ide_retry_pc(drive);
Bartlomiej Zolnierkiewicz258ec412008-07-15 21:21:55 +0200576 return ide_stopped;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 }
578 pc->error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 } else {
Bartlomiej Zolnierkiewiczc152cc12009-03-27 12:46:34 +0100580 pc->error = IDE_DRV_ERROR_GENERAL;
Bartlomiej Zolnierkiewicz5e2040fd2009-03-27 12:46:34 +0100581 drive->failed_pc = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 }
Bartlomiej Zolnierkiewiczb14c7212008-10-13 21:39:30 +0200583 drive->pc_callback(drive, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 return ide_stopped;
585}
586
Borislav Petkovcd2abbf2008-07-15 21:22:03 +0200587static void ide_tape_create_rw_cmd(idetape_tape_t *tape,
Borislav Petkov0014c752008-07-23 19:56:00 +0200588 struct ide_atapi_pc *pc, struct request *rq,
589 u8 opcode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590{
Tejun Heo963da552009-04-19 08:46:02 +0900591 unsigned int length = rq->nr_sectors;
Borislav Petkov0014c752008-07-23 19:56:00 +0200592
Bartlomiej Zolnierkiewicz7bf74202008-10-10 22:39:37 +0200593 ide_init_pc(pc);
Borislav Petkov860ff5e2008-02-02 19:56:50 +0100594 put_unaligned(cpu_to_be32(length), (unsigned int *) &pc->c[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 pc->c[1] = 1;
Borislav Petkovd236d742008-04-18 00:46:27 +0200596 pc->buf = NULL;
597 pc->buf_size = length * tape->blk_size;
598 pc->req_xfer = pc->buf_size;
Borislav Petkovf73850a2008-04-27 15:38:33 +0200599 if (pc->req_xfer == tape->buffer_size)
Bartlomiej Zolnierkiewicz5e331092008-07-15 21:21:56 +0200600 pc->flags |= PC_FLAG_DMA_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601
Tejun Heo963da552009-04-19 08:46:02 +0900602 if (opcode == READ_6)
Borislav Petkovcd2abbf2008-07-15 21:22:03 +0200603 pc->c[0] = READ_6;
Tejun Heo963da552009-04-19 08:46:02 +0900604 else if (opcode == WRITE_6) {
Borislav Petkovcd2abbf2008-07-15 21:22:03 +0200605 pc->c[0] = WRITE_6;
606 pc->flags |= PC_FLAG_WRITING;
Borislav Petkovcd2abbf2008-07-15 21:22:03 +0200607 }
Borislav Petkov0014c752008-07-23 19:56:00 +0200608
609 memcpy(rq->cmd, pc->c, 12);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610}
611
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612static ide_startstop_t idetape_do_request(ide_drive_t *drive,
613 struct request *rq, sector_t block)
614{
Bartlomiej Zolnierkiewiczb73c7ee2008-07-23 19:55:52 +0200615 ide_hwif_t *hwif = drive->hwif;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 idetape_tape_t *tape = drive->driver_data;
Borislav Petkovd236d742008-04-18 00:46:27 +0200617 struct ide_atapi_pc *pc = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 struct request *postponed_rq = tape->postponed_rq;
Bartlomiej Zolnierkiewiczb788ee92009-03-27 12:46:46 +0100619 struct ide_cmd cmd;
Bartlomiej Zolnierkiewicz22c525b2008-01-25 22:17:11 +0100620 u8 stat;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621
Mark de Wevere8e75262009-05-17 17:22:53 +0200622 debug_log(DBG_SENSE, "sector: %llu, nr_sectors: %lu\n",
Tejun Heo963da552009-04-19 08:46:02 +0900623 (unsigned long long)rq->sector, rq->nr_sectors);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624
Borislav Petkov6b544fc2009-04-19 07:00:42 +0900625 if (!(blk_special_request(rq) || blk_sense_request(rq))) {
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100626 /* We do not support buffer cache originated requests. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 printk(KERN_NOTICE "ide-tape: %s: Unsupported request in "
Jens Axboe4aff5e22006-08-10 08:44:47 +0200628 "request queue (%d)\n", drive->name, rq->cmd_type);
Bartlomiej Zolnierkiewicz89f78b322009-03-27 12:46:43 +0100629 if (blk_fs_request(rq) == 0 && rq->errors == 0)
630 rq->errors = -EIO;
Bartlomiej Zolnierkiewicz130e8862009-03-27 12:46:45 +0100631 ide_complete_rq(drive, -EIO, ide_rq_bytes(rq));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 return ide_stopped;
633 }
634
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100635 /* Retry a failed packet command */
Bartlomiej Zolnierkiewicz5e2040fd2009-03-27 12:46:34 +0100636 if (drive->failed_pc && drive->pc->c[0] == REQUEST_SENSE) {
637 pc = drive->failed_pc;
Bartlomiej Zolnierkiewicz28c72142008-07-15 21:21:59 +0200638 goto out;
639 }
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100640
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 if (postponed_rq != NULL)
642 if (rq != postponed_rq) {
643 printk(KERN_ERR "ide-tape: ide-tape.c bug - "
644 "Two DSC requests were queued\n");
Bartlomiej Zolnierkiewicz313afea2009-03-27 12:46:34 +0100645 drive->failed_pc = NULL;
Bartlomiej Zolnierkiewicz6902a532009-03-27 12:46:43 +0100646 rq->errors = 0;
Bartlomiej Zolnierkiewiczf974b192009-03-27 12:46:44 +0100647 ide_complete_rq(drive, 0, blk_rq_bytes(rq));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648 return ide_stopped;
649 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650
651 tape->postponed_rq = NULL;
652
653 /*
654 * If the tape is still busy, postpone our request and service
655 * the other device meanwhile.
656 */
Bartlomiej Zolnierkiewicz374e0422008-07-23 19:55:56 +0200657 stat = hwif->tp_ops->read_status(hwif);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +0200659 if ((drive->dev_flags & IDE_DFLAG_DSC_OVERLAP) == 0 &&
660 (rq->cmd[13] & REQ_IDETAPE_PC2) == 0)
Borislav Petkov626542c2009-06-07 15:37:05 +0200661 drive->atapi_flags |= IDE_AFLAG_IGNORE_DSC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +0200663 if (drive->dev_flags & IDE_DFLAG_POST_RESET) {
Borislav Petkov626542c2009-06-07 15:37:05 +0200664 drive->atapi_flags |= IDE_AFLAG_IGNORE_DSC;
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +0200665 drive->dev_flags &= ~IDE_DFLAG_POST_RESET;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666 }
667
Borislav Petkov626542c2009-06-07 15:37:05 +0200668 if (!(drive->atapi_flags & IDE_AFLAG_IGNORE_DSC) &&
669 !(stat & ATA_DSC)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670 if (postponed_rq == NULL) {
671 tape->dsc_polling_start = jiffies;
Borislav Petkov54bb2072008-02-06 02:57:52 +0100672 tape->dsc_poll_freq = tape->best_dsc_rw_freq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673 tape->dsc_timeout = jiffies + IDETAPE_DSC_RW_TIMEOUT;
674 } else if (time_after(jiffies, tape->dsc_timeout)) {
675 printk(KERN_ERR "ide-tape: %s: DSC timeout\n",
676 tape->name);
Borislav Petkov83dd5732008-07-23 19:56:00 +0200677 if (rq->cmd[13] & REQ_IDETAPE_PC2) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 idetape_media_access_finished(drive);
679 return ide_stopped;
680 } else {
681 return ide_do_reset(drive);
682 }
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100683 } else if (time_after(jiffies,
684 tape->dsc_polling_start +
685 IDETAPE_DSC_MA_THRESHOLD))
Borislav Petkov54bb2072008-02-06 02:57:52 +0100686 tape->dsc_poll_freq = IDETAPE_DSC_MA_SLOW;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 idetape_postpone_request(drive);
688 return ide_stopped;
Borislav Petkov626542c2009-06-07 15:37:05 +0200689 } else
690 drive->atapi_flags &= ~IDE_AFLAG_IGNORE_DSC;
691
Borislav Petkov83dd5732008-07-23 19:56:00 +0200692 if (rq->cmd[13] & REQ_IDETAPE_READ) {
Bartlomiej Zolnierkiewicz2e8a6f82008-10-10 22:39:36 +0200693 pc = &tape->queued_pc;
Borislav Petkov0014c752008-07-23 19:56:00 +0200694 ide_tape_create_rw_cmd(tape, pc, rq, READ_6);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 goto out;
696 }
Borislav Petkov83dd5732008-07-23 19:56:00 +0200697 if (rq->cmd[13] & REQ_IDETAPE_WRITE) {
Bartlomiej Zolnierkiewicz2e8a6f82008-10-10 22:39:36 +0200698 pc = &tape->queued_pc;
Borislav Petkov0014c752008-07-23 19:56:00 +0200699 ide_tape_create_rw_cmd(tape, pc, rq, WRITE_6);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 goto out;
701 }
Borislav Petkov83dd5732008-07-23 19:56:00 +0200702 if (rq->cmd[13] & REQ_IDETAPE_PC1) {
Tejun Heoc267cc12009-04-19 07:00:42 +0900703 pc = (struct ide_atapi_pc *)rq->special;
Borislav Petkov83dd5732008-07-23 19:56:00 +0200704 rq->cmd[13] &= ~(REQ_IDETAPE_PC1);
705 rq->cmd[13] |= REQ_IDETAPE_PC2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 goto out;
707 }
Borislav Petkov83dd5732008-07-23 19:56:00 +0200708 if (rq->cmd[13] & REQ_IDETAPE_PC2) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709 idetape_media_access_finished(drive);
710 return ide_stopped;
711 }
712 BUG();
Bartlomiej Zolnierkiewicz28c72142008-07-15 21:21:59 +0200713
Borislav Petkovf2e3ab52008-07-23 19:56:01 +0200714out:
Borislav Petkov6b544fc2009-04-19 07:00:42 +0900715 /* prepare sense request for this command */
716 ide_prep_sense(drive, rq);
717
Bartlomiej Zolnierkiewiczb788ee92009-03-27 12:46:46 +0100718 memset(&cmd, 0, sizeof(cmd));
719
720 if (rq_data_dir(rq))
721 cmd.tf_flags |= IDE_TFLAG_WRITE;
722
723 cmd.rq = rq;
724
Tejun Heo5c4be572009-04-19 07:00:42 +0900725 ide_init_sg_cmd(&cmd, pc->req_xfer);
726 ide_map_sg(drive, &cmd);
727
Bartlomiej Zolnierkiewiczb788ee92009-03-27 12:46:46 +0100728 return ide_tape_issue_pc(drive, &cmd, pc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729}
730
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100732 * Write a filemark if write_filemark=1. Flush the device buffers without
733 * writing a filemark otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100735static void idetape_create_write_filemark_cmd(ide_drive_t *drive,
Borislav Petkovd236d742008-04-18 00:46:27 +0200736 struct ide_atapi_pc *pc, int write_filemark)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737{
Bartlomiej Zolnierkiewicz7bf74202008-10-10 22:39:37 +0200738 ide_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +0100739 pc->c[0] = WRITE_FILEMARKS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740 pc->c[4] = write_filemark;
Borislav Petkov346331f2008-04-18 00:46:26 +0200741 pc->flags |= PC_FLAG_WAIT_FOR_DSC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742}
743
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744static int idetape_wait_ready(ide_drive_t *drive, unsigned long timeout)
745{
746 idetape_tape_t *tape = drive->driver_data;
Bartlomiej Zolnierkiewicz2ac07d92008-10-10 22:39:38 +0200747 struct gendisk *disk = tape->disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748 int load_attempted = 0;
749
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100750 /* Wait for the tape to become ready */
Borislav Petkov49d80782009-06-07 15:37:06 +0200751 set_bit(ilog2(IDE_AFLAG_MEDIUM_PRESENT), &drive->atapi_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752 timeout += jiffies;
753 while (time_before(jiffies, timeout)) {
Bartlomiej Zolnierkiewiczde699ad2008-10-10 22:39:39 +0200754 if (ide_do_test_unit_ready(drive, disk) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755 return 0;
756 if ((tape->sense_key == 2 && tape->asc == 4 && tape->ascq == 2)
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100757 || (tape->asc == 0x3A)) {
758 /* no media */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 if (load_attempted)
760 return -ENOMEDIUM;
Bartlomiej Zolnierkiewicz0c8a6c72008-10-10 22:39:39 +0200761 ide_do_start_stop(drive, disk, IDETAPE_LU_LOAD_MASK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762 load_attempted = 1;
763 /* not about to be ready */
764 } else if (!(tape->sense_key == 2 && tape->asc == 4 &&
765 (tape->ascq == 1 || tape->ascq == 8)))
766 return -EIO;
Nishanth Aravamudan80ce45f2005-09-10 00:27:08 -0700767 msleep(100);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 }
769 return -EIO;
770}
771
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100772static int idetape_flush_tape_buffers(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773{
Bartlomiej Zolnierkiewicz2ac07d92008-10-10 22:39:38 +0200774 struct ide_tape_obj *tape = drive->driver_data;
Borislav Petkovd236d742008-04-18 00:46:27 +0200775 struct ide_atapi_pc pc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776 int rc;
777
778 idetape_create_write_filemark_cmd(drive, &pc, 0);
Bartlomiej Zolnierkiewicz2ac07d92008-10-10 22:39:38 +0200779 rc = ide_queue_pc_tail(drive, tape->disk, &pc);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100780 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781 return rc;
782 idetape_wait_ready(drive, 60 * 5 * HZ);
783 return 0;
784}
785
Borislav Petkovd236d742008-04-18 00:46:27 +0200786static void idetape_create_read_position_cmd(struct ide_atapi_pc *pc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787{
Bartlomiej Zolnierkiewicz7bf74202008-10-10 22:39:37 +0200788 ide_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +0100789 pc->c[0] = READ_POSITION;
Borislav Petkovd236d742008-04-18 00:46:27 +0200790 pc->req_xfer = 20;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791}
792
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100793static int idetape_read_position(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794{
795 idetape_tape_t *tape = drive->driver_data;
Borislav Petkovd236d742008-04-18 00:46:27 +0200796 struct ide_atapi_pc pc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797 int position;
798
Borislav Petkov8004a8c2008-02-06 02:57:51 +0100799 debug_log(DBG_PROCS, "Enter %s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800
801 idetape_create_read_position_cmd(&pc);
Bartlomiej Zolnierkiewicz2ac07d92008-10-10 22:39:38 +0200802 if (ide_queue_pc_tail(drive, tape->disk, &pc))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803 return -1;
Borislav Petkov54bb2072008-02-06 02:57:52 +0100804 position = tape->first_frame;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805 return position;
806}
807
Borislav Petkovd236d742008-04-18 00:46:27 +0200808static void idetape_create_locate_cmd(ide_drive_t *drive,
809 struct ide_atapi_pc *pc,
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100810 unsigned int block, u8 partition, int skip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811{
Bartlomiej Zolnierkiewicz7bf74202008-10-10 22:39:37 +0200812 ide_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +0100813 pc->c[0] = POSITION_TO_ELEMENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814 pc->c[1] = 2;
Borislav Petkov860ff5e2008-02-02 19:56:50 +0100815 put_unaligned(cpu_to_be32(block), (unsigned int *) &pc->c[3]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816 pc->c[8] = partition;
Borislav Petkov346331f2008-04-18 00:46:26 +0200817 pc->flags |= PC_FLAG_WAIT_FOR_DSC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818}
819
Borislav Petkovec0fdb02008-04-27 15:38:34 +0200820static void __ide_tape_discard_merge_buffer(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821{
822 idetape_tape_t *tape = drive->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823
Borislav Petkov54abf372008-02-06 02:57:52 +0100824 if (tape->chrdev_dir != IDETAPE_DIR_READ)
Borislav Petkov97986302008-04-27 15:38:34 +0200825 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826
Borislav Petkov49d80782009-06-07 15:37:06 +0200827 clear_bit(ilog2(IDE_AFLAG_FILEMARK), &drive->atapi_flags);
Tejun Heo963da552009-04-19 08:46:02 +0900828 tape->valid = 0;
829 if (tape->buf != NULL) {
830 kfree(tape->buf);
831 tape->buf = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832 }
833
Borislav Petkov54abf372008-02-06 02:57:52 +0100834 tape->chrdev_dir = IDETAPE_DIR_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835}
836
837/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100838 * Position the tape to the requested block using the LOCATE packet command.
839 * A READ POSITION command is then issued to check where we are positioned. Like
840 * all higher level operations, we queue the commands at the tail of the request
841 * queue and wait for their completion.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842 */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100843static int idetape_position_tape(ide_drive_t *drive, unsigned int block,
844 u8 partition, int skip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845{
846 idetape_tape_t *tape = drive->driver_data;
Bartlomiej Zolnierkiewicz2ac07d92008-10-10 22:39:38 +0200847 struct gendisk *disk = tape->disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848 int retval;
Borislav Petkovd236d742008-04-18 00:46:27 +0200849 struct ide_atapi_pc pc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850
Borislav Petkov54abf372008-02-06 02:57:52 +0100851 if (tape->chrdev_dir == IDETAPE_DIR_READ)
Borislav Petkovec0fdb02008-04-27 15:38:34 +0200852 __ide_tape_discard_merge_buffer(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853 idetape_wait_ready(drive, 60 * 5 * HZ);
854 idetape_create_locate_cmd(drive, &pc, block, partition, skip);
Bartlomiej Zolnierkiewicz2ac07d92008-10-10 22:39:38 +0200855 retval = ide_queue_pc_tail(drive, disk, &pc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856 if (retval)
857 return (retval);
858
859 idetape_create_read_position_cmd(&pc);
Bartlomiej Zolnierkiewicz2ac07d92008-10-10 22:39:38 +0200860 return ide_queue_pc_tail(drive, disk, &pc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861}
862
Borislav Petkovec0fdb02008-04-27 15:38:34 +0200863static void ide_tape_discard_merge_buffer(ide_drive_t *drive,
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100864 int restore_position)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865{
866 idetape_tape_t *tape = drive->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867 int seek, position;
868
Borislav Petkovec0fdb02008-04-27 15:38:34 +0200869 __ide_tape_discard_merge_buffer(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870 if (restore_position) {
871 position = idetape_read_position(drive);
Borislav Petkov97986302008-04-27 15:38:34 +0200872 seek = position > 0 ? position : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873 if (idetape_position_tape(drive, seek, 0, 0)) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100874 printk(KERN_INFO "ide-tape: %s: position_tape failed in"
Borislav Petkovec0fdb02008-04-27 15:38:34 +0200875 " %s\n", tape->name, __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876 return;
877 }
878 }
879}
880
881/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100882 * Generate a read/write request for the block device interface and wait for it
883 * to be serviced.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884 */
Tejun Heo6bb11dd2009-04-19 08:46:03 +0900885static int idetape_queue_rw_tail(ide_drive_t *drive, int cmd, int size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886{
887 idetape_tape_t *tape = drive->driver_data;
FUJITA Tomonori64ea1b42008-07-15 21:21:43 +0200888 struct request *rq;
Tejun Heo21d9c5d2009-04-19 08:46:02 +0900889 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890
Borislav Petkov8004a8c2008-02-06 02:57:51 +0100891 debug_log(DBG_SENSE, "%s: cmd=%d\n", __func__, cmd);
Tejun Heo21d9c5d2009-04-19 08:46:02 +0900892 BUG_ON(cmd != REQ_IDETAPE_READ && cmd != REQ_IDETAPE_WRITE);
Tejun Heo6bb11dd2009-04-19 08:46:03 +0900893 BUG_ON(size < 0 || size % tape->blk_size);
Borislav Petkov8004a8c2008-02-06 02:57:51 +0100894
FUJITA Tomonori64ea1b42008-07-15 21:21:43 +0200895 rq = blk_get_request(drive->queue, READ, __GFP_WAIT);
896 rq->cmd_type = REQ_TYPE_SPECIAL;
Borislav Petkov83dd5732008-07-23 19:56:00 +0200897 rq->cmd[13] = cmd;
FUJITA Tomonori64ea1b42008-07-15 21:21:43 +0200898 rq->rq_disk = tape->disk;
FUJITA Tomonori64ea1b42008-07-15 21:21:43 +0200899 rq->sector = tape->first_frame;
Tejun Heo21d9c5d2009-04-19 08:46:02 +0900900
901 if (size) {
Tejun Heo963da552009-04-19 08:46:02 +0900902 ret = blk_rq_map_kern(drive->queue, rq, tape->buf, size,
Tejun Heo21d9c5d2009-04-19 08:46:02 +0900903 __GFP_WAIT);
904 if (ret)
905 goto out_put;
906 }
907
FUJITA Tomonori64ea1b42008-07-15 21:21:43 +0200908 blk_execute_rq(drive->queue, tape->disk, rq, 0);
909
Tejun Heo963da552009-04-19 08:46:02 +0900910 /* calculate the number of transferred bytes and update buffer state */
Tejun Heo21d9c5d2009-04-19 08:46:02 +0900911 size -= rq->data_len;
Tejun Heo963da552009-04-19 08:46:02 +0900912 tape->cur = tape->buf;
Tejun Heo21d9c5d2009-04-19 08:46:02 +0900913 if (cmd == REQ_IDETAPE_READ)
Tejun Heo963da552009-04-19 08:46:02 +0900914 tape->valid = size;
915 else
916 tape->valid = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917
Tejun Heo21d9c5d2009-04-19 08:46:02 +0900918 ret = size;
919 if (rq->errors == IDE_DRV_ERROR_GENERAL)
920 ret = -EIO;
Tejun Heo21d9c5d2009-04-19 08:46:02 +0900921out_put:
922 blk_put_request(rq);
FUJITA Tomonori64ea1b42008-07-15 21:21:43 +0200923 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924}
925
Borislav Petkovd236d742008-04-18 00:46:27 +0200926static void idetape_create_inquiry_cmd(struct ide_atapi_pc *pc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927{
Bartlomiej Zolnierkiewicz7bf74202008-10-10 22:39:37 +0200928 ide_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +0100929 pc->c[0] = INQUIRY;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100930 pc->c[4] = 254;
Borislav Petkovd236d742008-04-18 00:46:27 +0200931 pc->req_xfer = 254;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932}
933
Borislav Petkovd236d742008-04-18 00:46:27 +0200934static void idetape_create_rewind_cmd(ide_drive_t *drive,
935 struct ide_atapi_pc *pc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936{
Bartlomiej Zolnierkiewicz7bf74202008-10-10 22:39:37 +0200937 ide_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +0100938 pc->c[0] = REZERO_UNIT;
Borislav Petkov346331f2008-04-18 00:46:26 +0200939 pc->flags |= PC_FLAG_WAIT_FOR_DSC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940}
941
Borislav Petkovd236d742008-04-18 00:46:27 +0200942static void idetape_create_erase_cmd(struct ide_atapi_pc *pc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943{
Bartlomiej Zolnierkiewicz7bf74202008-10-10 22:39:37 +0200944 ide_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +0100945 pc->c[0] = ERASE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946 pc->c[1] = 1;
Borislav Petkov346331f2008-04-18 00:46:26 +0200947 pc->flags |= PC_FLAG_WAIT_FOR_DSC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948}
949
Borislav Petkovd236d742008-04-18 00:46:27 +0200950static void idetape_create_space_cmd(struct ide_atapi_pc *pc, int count, u8 cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951{
Bartlomiej Zolnierkiewicz7bf74202008-10-10 22:39:37 +0200952 ide_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +0100953 pc->c[0] = SPACE;
Borislav Petkov860ff5e2008-02-02 19:56:50 +0100954 put_unaligned(cpu_to_be32(count), (unsigned int *) &pc->c[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955 pc->c[1] = cmd;
Borislav Petkov346331f2008-04-18 00:46:26 +0200956 pc->flags |= PC_FLAG_WAIT_FOR_DSC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957}
958
Borislav Petkovd9df9372008-04-27 15:38:34 +0200959static void ide_tape_flush_merge_buffer(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960{
961 idetape_tape_t *tape = drive->driver_data;
Borislav Petkov55a5d292008-02-02 19:56:49 +0100962
Borislav Petkov54abf372008-02-06 02:57:52 +0100963 if (tape->chrdev_dir != IDETAPE_DIR_WRITE) {
Borislav Petkov077e3bd2008-04-27 15:38:34 +0200964 printk(KERN_ERR "ide-tape: bug: Trying to empty merge buffer"
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100965 " but we are not writing.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966 return;
967 }
Tejun Heo963da552009-04-19 08:46:02 +0900968 if (tape->buf) {
Tejun Heo6bb11dd2009-04-19 08:46:03 +0900969 size_t aligned = roundup(tape->valid, tape->blk_size);
970
971 memset(tape->cur, 0, aligned - tape->valid);
Tejun Heo07bd9682009-04-19 08:46:03 +0900972 idetape_queue_rw_tail(drive, REQ_IDETAPE_WRITE, aligned);
Tejun Heo963da552009-04-19 08:46:02 +0900973 kfree(tape->buf);
974 tape->buf = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975 }
Borislav Petkov54abf372008-02-06 02:57:52 +0100976 tape->chrdev_dir = IDETAPE_DIR_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977}
978
Tejun Heo88f1b942009-04-19 08:46:02 +0900979static int idetape_init_rw(ide_drive_t *drive, int dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980{
981 idetape_tape_t *tape = drive->driver_data;
Tejun Heo88f1b942009-04-19 08:46:02 +0900982 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983
Tejun Heo88f1b942009-04-19 08:46:02 +0900984 BUG_ON(dir != IDETAPE_DIR_READ && dir != IDETAPE_DIR_WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985
Tejun Heo88f1b942009-04-19 08:46:02 +0900986 if (tape->chrdev_dir == dir)
987 return 0;
988
989 if (tape->chrdev_dir == IDETAPE_DIR_READ)
990 ide_tape_discard_merge_buffer(drive, 1);
991 else if (tape->chrdev_dir == IDETAPE_DIR_WRITE) {
992 ide_tape_flush_merge_buffer(drive);
993 idetape_flush_tape_buffers(drive);
994 }
995
996 if (tape->buf || tape->valid) {
997 printk(KERN_ERR "ide-tape: valid should be 0 now\n");
998 tape->valid = 0;
999 }
1000
1001 tape->buf = kmalloc(tape->buffer_size, GFP_KERNEL);
1002 if (!tape->buf)
1003 return -ENOMEM;
1004 tape->chrdev_dir = dir;
1005 tape->cur = tape->buf;
1006
1007 /*
1008 * Issue a 0 rw command to ensure that DSC handshake is
1009 * switched from completion mode to buffer available mode. No
1010 * point in issuing this if DSC overlap isn't supported, some
1011 * drives (Seagate STT3401A) will return an error.
1012 */
1013 if (drive->dev_flags & IDE_DFLAG_DSC_OVERLAP) {
1014 int cmd = dir == IDETAPE_DIR_READ ? REQ_IDETAPE_READ
1015 : REQ_IDETAPE_WRITE;
1016
1017 rc = idetape_queue_rw_tail(drive, cmd, 0);
1018 if (rc < 0) {
1019 kfree(tape->buf);
1020 tape->buf = NULL;
1021 tape->chrdev_dir = IDETAPE_DIR_NONE;
1022 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023 }
1024 }
Borislav Petkov5e69bd92008-04-27 15:38:25 +02001025
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026 return 0;
1027}
1028
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001029static void idetape_pad_zeros(ide_drive_t *drive, int bcount)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030{
1031 idetape_tape_t *tape = drive->driver_data;
Tejun Heo963da552009-04-19 08:46:02 +09001032
1033 memset(tape->buf, 0, tape->buffer_size);
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001034
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035 while (bcount) {
Tejun Heo963da552009-04-19 08:46:02 +09001036 unsigned int count = min(tape->buffer_size, bcount);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037
Tejun Heo6bb11dd2009-04-19 08:46:03 +09001038 idetape_queue_rw_tail(drive, REQ_IDETAPE_WRITE, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039 bcount -= count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040 }
1041}
1042
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001044 * Rewinds the tape to the Beginning Of the current Partition (BOP). We
1045 * currently support only one partition.
1046 */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001047static int idetape_rewind_tape(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048{
Bartlomiej Zolnierkiewicz2ac07d92008-10-10 22:39:38 +02001049 struct ide_tape_obj *tape = drive->driver_data;
1050 struct gendisk *disk = tape->disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051 int retval;
Borislav Petkovd236d742008-04-18 00:46:27 +02001052 struct ide_atapi_pc pc;
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001053
1054 debug_log(DBG_SENSE, "Enter %s\n", __func__);
1055
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056 idetape_create_rewind_cmd(drive, &pc);
Bartlomiej Zolnierkiewicz2ac07d92008-10-10 22:39:38 +02001057 retval = ide_queue_pc_tail(drive, disk, &pc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058 if (retval)
1059 return retval;
1060
1061 idetape_create_read_position_cmd(&pc);
Bartlomiej Zolnierkiewicz2ac07d92008-10-10 22:39:38 +02001062 retval = ide_queue_pc_tail(drive, disk, &pc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063 if (retval)
1064 return retval;
1065 return 0;
1066}
1067
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001068/* mtio.h compatible commands should be issued to the chrdev interface. */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001069static int idetape_blkdev_ioctl(ide_drive_t *drive, unsigned int cmd,
1070 unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071{
1072 idetape_tape_t *tape = drive->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073 void __user *argp = (void __user *)arg;
1074
Borislav Petkovd59823f2008-02-02 19:56:51 +01001075 struct idetape_config {
1076 int dsc_rw_frequency;
1077 int dsc_media_access_frequency;
1078 int nr_stages;
1079 } config;
1080
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001081 debug_log(DBG_PROCS, "Enter %s\n", __func__);
1082
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083 switch (cmd) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001084 case 0x0340:
1085 if (copy_from_user(&config, argp, sizeof(config)))
1086 return -EFAULT;
1087 tape->best_dsc_rw_freq = config.dsc_rw_frequency;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001088 break;
1089 case 0x0350:
1090 config.dsc_rw_frequency = (int) tape->best_dsc_rw_freq;
Borislav Petkov83042b22008-04-27 15:38:27 +02001091 config.nr_stages = 1;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001092 if (copy_to_user(argp, &config, sizeof(config)))
1093 return -EFAULT;
1094 break;
1095 default:
1096 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097 }
1098 return 0;
1099}
1100
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001101static int idetape_space_over_filemarks(ide_drive_t *drive, short mt_op,
1102 int mt_count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103{
1104 idetape_tape_t *tape = drive->driver_data;
Bartlomiej Zolnierkiewicz2ac07d92008-10-10 22:39:38 +02001105 struct gendisk *disk = tape->disk;
Borislav Petkovd236d742008-04-18 00:46:27 +02001106 struct ide_atapi_pc pc;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001107 int retval, count = 0;
Borislav Petkovb6422012008-02-02 19:56:49 +01001108 int sprev = !!(tape->caps[4] & 0x20);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109
1110 if (mt_count == 0)
1111 return 0;
1112 if (MTBSF == mt_op || MTBSFM == mt_op) {
Borislav Petkovb6422012008-02-02 19:56:49 +01001113 if (!sprev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114 return -EIO;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001115 mt_count = -mt_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116 }
1117
Borislav Petkov54abf372008-02-06 02:57:52 +01001118 if (tape->chrdev_dir == IDETAPE_DIR_READ) {
Tejun Heo963da552009-04-19 08:46:02 +09001119 tape->valid = 0;
Borislav Petkov49d80782009-06-07 15:37:06 +02001120 if (test_and_clear_bit(ilog2(IDE_AFLAG_FILEMARK),
1121 &drive->atapi_flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122 ++count;
Borislav Petkovec0fdb02008-04-27 15:38:34 +02001123 ide_tape_discard_merge_buffer(drive, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124 }
1125
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126 switch (mt_op) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001127 case MTFSF:
1128 case MTBSF:
1129 idetape_create_space_cmd(&pc, mt_count - count,
1130 IDETAPE_SPACE_OVER_FILEMARK);
Bartlomiej Zolnierkiewicz2ac07d92008-10-10 22:39:38 +02001131 return ide_queue_pc_tail(drive, disk, &pc);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001132 case MTFSFM:
1133 case MTBSFM:
1134 if (!sprev)
1135 return -EIO;
1136 retval = idetape_space_over_filemarks(drive, MTFSF,
1137 mt_count - count);
1138 if (retval)
1139 return retval;
1140 count = (MTBSFM == mt_op ? 1 : -1);
1141 return idetape_space_over_filemarks(drive, MTFSF, count);
1142 default:
1143 printk(KERN_ERR "ide-tape: MTIO operation %d not supported\n",
1144 mt_op);
1145 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146 }
1147}
1148
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001150 * Our character device read / write functions.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001152 * The tape is optimized to maximize throughput when it is transferring an
1153 * integral number of the "continuous transfer limit", which is a parameter of
1154 * the specific tape (26kB on my particular tape, 32kB for Onstream).
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001156 * As of version 1.3 of the driver, the character device provides an abstract
1157 * continuous view of the media - any mix of block sizes (even 1 byte) on the
1158 * same backup/restore procedure is supported. The driver will internally
1159 * convert the requests to the recommended transfer unit, so that an unmatch
1160 * between the user's block size to the recommended size will only result in a
1161 * (slightly) increased driver overhead, but will no longer hit performance.
1162 * This is not applicable to Onstream.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163 */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001164static ssize_t idetape_chrdev_read(struct file *file, char __user *buf,
1165 size_t count, loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166{
Borislav Petkov5aeddf92008-10-13 21:39:34 +02001167 struct ide_tape_obj *tape = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168 ide_drive_t *drive = tape->drive;
Tejun Heo07bd9682009-04-19 08:46:03 +09001169 size_t done = 0;
Daniel Walkerdcd96372006-06-25 05:47:37 -07001170 ssize_t ret = 0;
Tejun Heo07bd9682009-04-19 08:46:03 +09001171 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001173 debug_log(DBG_CHRDEV, "Enter %s, count %Zd\n", __func__, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174
Borislav Petkov54abf372008-02-06 02:57:52 +01001175 if (tape->chrdev_dir != IDETAPE_DIR_READ) {
Borislav Petkov49d80782009-06-07 15:37:06 +02001176 if (test_bit(ilog2(IDE_AFLAG_DETECT_BS), &drive->atapi_flags))
Borislav Petkov54bb2072008-02-06 02:57:52 +01001177 if (count > tape->blk_size &&
1178 (count % tape->blk_size) == 0)
1179 tape->user_bs_factor = count / tape->blk_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180 }
Tejun Heo07bd9682009-04-19 08:46:03 +09001181
Tejun Heo88f1b942009-04-19 08:46:02 +09001182 rc = idetape_init_rw(drive, IDETAPE_DIR_READ);
Borislav Petkov8d06bfa2008-02-06 02:57:53 +01001183 if (rc < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184 return rc;
Tejun Heo07bd9682009-04-19 08:46:03 +09001185
1186 while (done < count) {
1187 size_t todo;
1188
1189 /* refill if staging buffer is empty */
1190 if (!tape->valid) {
1191 /* If we are at a filemark, nothing more to read */
Borislav Petkov49d80782009-06-07 15:37:06 +02001192 if (test_bit(ilog2(IDE_AFLAG_FILEMARK),
1193 &drive->atapi_flags))
Tejun Heo07bd9682009-04-19 08:46:03 +09001194 break;
1195 /* read */
1196 if (idetape_queue_rw_tail(drive, REQ_IDETAPE_READ,
1197 tape->buffer_size) <= 0)
1198 break;
1199 }
1200
1201 /* copy out */
1202 todo = min_t(size_t, count - done, tape->valid);
1203 if (copy_to_user(buf + done, tape->cur, todo))
Daniel Walkerdcd96372006-06-25 05:47:37 -07001204 ret = -EFAULT;
Tejun Heo07bd9682009-04-19 08:46:03 +09001205
1206 tape->cur += todo;
1207 tape->valid -= todo;
1208 done += todo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209 }
Tejun Heo07bd9682009-04-19 08:46:03 +09001210
Borislav Petkov49d80782009-06-07 15:37:06 +02001211 if (!done && test_bit(ilog2(IDE_AFLAG_FILEMARK), &drive->atapi_flags)) {
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001212 debug_log(DBG_SENSE, "%s: spacing over filemark\n", tape->name);
1213
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214 idetape_space_over_filemarks(drive, MTFSF, 1);
1215 return 0;
1216 }
Daniel Walkerdcd96372006-06-25 05:47:37 -07001217
Tejun Heo07bd9682009-04-19 08:46:03 +09001218 return ret ? ret : done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219}
1220
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001221static ssize_t idetape_chrdev_write(struct file *file, const char __user *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222 size_t count, loff_t *ppos)
1223{
Borislav Petkov5aeddf92008-10-13 21:39:34 +02001224 struct ide_tape_obj *tape = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225 ide_drive_t *drive = tape->drive;
Tejun Heo07bd9682009-04-19 08:46:03 +09001226 size_t done = 0;
Daniel Walkerdcd96372006-06-25 05:47:37 -07001227 ssize_t ret = 0;
Tejun Heo88f1b942009-04-19 08:46:02 +09001228 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229
1230 /* The drive is write protected. */
1231 if (tape->write_prot)
1232 return -EACCES;
1233
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001234 debug_log(DBG_CHRDEV, "Enter %s, count %Zd\n", __func__, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235
1236 /* Initialize write operation */
Tejun Heo88f1b942009-04-19 08:46:02 +09001237 rc = idetape_init_rw(drive, IDETAPE_DIR_WRITE);
1238 if (rc < 0)
1239 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240
Tejun Heo07bd9682009-04-19 08:46:03 +09001241 while (done < count) {
1242 size_t todo;
1243
1244 /* flush if staging buffer is full */
1245 if (tape->valid == tape->buffer_size &&
1246 idetape_queue_rw_tail(drive, REQ_IDETAPE_WRITE,
1247 tape->buffer_size) <= 0)
1248 return rc;
1249
1250 /* copy in */
1251 todo = min_t(size_t, count - done,
1252 tape->buffer_size - tape->valid);
1253 if (copy_from_user(tape->cur, buf + done, todo))
Daniel Walkerdcd96372006-06-25 05:47:37 -07001254 ret = -EFAULT;
Tejun Heo07bd9682009-04-19 08:46:03 +09001255
1256 tape->cur += todo;
1257 tape->valid += todo;
1258 done += todo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259 }
Tejun Heo07bd9682009-04-19 08:46:03 +09001260
1261 return ret ? ret : done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262}
1263
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001264static int idetape_write_filemark(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265{
Bartlomiej Zolnierkiewicz2ac07d92008-10-10 22:39:38 +02001266 struct ide_tape_obj *tape = drive->driver_data;
Borislav Petkovd236d742008-04-18 00:46:27 +02001267 struct ide_atapi_pc pc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268
1269 /* Write a filemark */
1270 idetape_create_write_filemark_cmd(drive, &pc, 1);
Bartlomiej Zolnierkiewicz2ac07d92008-10-10 22:39:38 +02001271 if (ide_queue_pc_tail(drive, tape->disk, &pc)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272 printk(KERN_ERR "ide-tape: Couldn't write a filemark\n");
1273 return -EIO;
1274 }
1275 return 0;
1276}
1277
1278/*
Borislav Petkovd99c9da2008-02-02 19:56:51 +01001279 * Called from idetape_chrdev_ioctl when the general mtio MTIOCTOP ioctl is
1280 * requested.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281 *
Borislav Petkovd99c9da2008-02-02 19:56:51 +01001282 * Note: MTBSF and MTBSFM are not supported when the tape doesn't support
1283 * spacing over filemarks in the reverse direction. In this case, MTFSFM is also
Borislav Petkov5bd50dc2008-04-27 15:38:28 +02001284 * usually not supported.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285 *
Borislav Petkovd99c9da2008-02-02 19:56:51 +01001286 * The following commands are currently not supported:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287 *
Borislav Petkovd99c9da2008-02-02 19:56:51 +01001288 * MTFSS, MTBSS, MTWSM, MTSETDENSITY, MTSETDRVBUFFER, MT_ST_BOOLEANS,
1289 * MT_ST_WRITE_THRESHOLD.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290 */
Borislav Petkovd99c9da2008-02-02 19:56:51 +01001291static int idetape_mtioctop(ide_drive_t *drive, short mt_op, int mt_count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292{
1293 idetape_tape_t *tape = drive->driver_data;
Bartlomiej Zolnierkiewicz2ac07d92008-10-10 22:39:38 +02001294 struct gendisk *disk = tape->disk;
Borislav Petkovd236d742008-04-18 00:46:27 +02001295 struct ide_atapi_pc pc;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001296 int i, retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001298 debug_log(DBG_ERR, "Handling MTIOCTOP ioctl: mt_op=%d, mt_count=%d\n",
1299 mt_op, mt_count);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001300
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301 switch (mt_op) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001302 case MTFSF:
1303 case MTFSFM:
1304 case MTBSF:
1305 case MTBSFM:
1306 if (!mt_count)
1307 return 0;
1308 return idetape_space_over_filemarks(drive, mt_op, mt_count);
1309 default:
1310 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311 }
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001312
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313 switch (mt_op) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001314 case MTWEOF:
1315 if (tape->write_prot)
1316 return -EACCES;
Borislav Petkovec0fdb02008-04-27 15:38:34 +02001317 ide_tape_discard_merge_buffer(drive, 1);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001318 for (i = 0; i < mt_count; i++) {
1319 retval = idetape_write_filemark(drive);
1320 if (retval)
1321 return retval;
1322 }
1323 return 0;
1324 case MTREW:
Borislav Petkovec0fdb02008-04-27 15:38:34 +02001325 ide_tape_discard_merge_buffer(drive, 0);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001326 if (idetape_rewind_tape(drive))
1327 return -EIO;
1328 return 0;
1329 case MTLOAD:
Borislav Petkovec0fdb02008-04-27 15:38:34 +02001330 ide_tape_discard_merge_buffer(drive, 0);
Bartlomiej Zolnierkiewicz0c8a6c72008-10-10 22:39:39 +02001331 return ide_do_start_stop(drive, disk, IDETAPE_LU_LOAD_MASK);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001332 case MTUNLOAD:
1333 case MTOFFL:
1334 /*
1335 * If door is locked, attempt to unlock before
1336 * attempting to eject.
1337 */
1338 if (tape->door_locked) {
Bartlomiej Zolnierkiewicz05780422008-10-10 22:39:38 +02001339 if (!ide_set_media_lock(drive, disk, 0))
Bartlomiej Zolnierkiewicz385a4b82008-10-10 22:39:37 +02001340 tape->door_locked = DOOR_UNLOCKED;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001341 }
Borislav Petkovec0fdb02008-04-27 15:38:34 +02001342 ide_tape_discard_merge_buffer(drive, 0);
Bartlomiej Zolnierkiewicz0c8a6c72008-10-10 22:39:39 +02001343 retval = ide_do_start_stop(drive, disk, !IDETAPE_LU_LOAD_MASK);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001344 if (!retval)
Borislav Petkov49d80782009-06-07 15:37:06 +02001345 clear_bit(ilog2(IDE_AFLAG_MEDIUM_PRESENT),
1346 &drive->atapi_flags);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001347 return retval;
1348 case MTNOP:
Borislav Petkovec0fdb02008-04-27 15:38:34 +02001349 ide_tape_discard_merge_buffer(drive, 0);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001350 return idetape_flush_tape_buffers(drive);
1351 case MTRETEN:
Borislav Petkovec0fdb02008-04-27 15:38:34 +02001352 ide_tape_discard_merge_buffer(drive, 0);
Bartlomiej Zolnierkiewicz0c8a6c72008-10-10 22:39:39 +02001353 return ide_do_start_stop(drive, disk,
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001354 IDETAPE_LU_RETENSION_MASK | IDETAPE_LU_LOAD_MASK);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001355 case MTEOM:
1356 idetape_create_space_cmd(&pc, 0, IDETAPE_SPACE_TO_EOD);
Bartlomiej Zolnierkiewicz2ac07d92008-10-10 22:39:38 +02001357 return ide_queue_pc_tail(drive, disk, &pc);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001358 case MTERASE:
1359 (void)idetape_rewind_tape(drive);
1360 idetape_create_erase_cmd(&pc);
Bartlomiej Zolnierkiewicz2ac07d92008-10-10 22:39:38 +02001361 return ide_queue_pc_tail(drive, disk, &pc);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001362 case MTSETBLK:
1363 if (mt_count) {
1364 if (mt_count < tape->blk_size ||
1365 mt_count % tape->blk_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366 return -EIO;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001367 tape->user_bs_factor = mt_count / tape->blk_size;
Borislav Petkov49d80782009-06-07 15:37:06 +02001368 clear_bit(ilog2(IDE_AFLAG_DETECT_BS),
1369 &drive->atapi_flags);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001370 } else
Borislav Petkov49d80782009-06-07 15:37:06 +02001371 set_bit(ilog2(IDE_AFLAG_DETECT_BS),
1372 &drive->atapi_flags);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001373 return 0;
1374 case MTSEEK:
Borislav Petkovec0fdb02008-04-27 15:38:34 +02001375 ide_tape_discard_merge_buffer(drive, 0);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001376 return idetape_position_tape(drive,
1377 mt_count * tape->user_bs_factor, tape->partition, 0);
1378 case MTSETPART:
Borislav Petkovec0fdb02008-04-27 15:38:34 +02001379 ide_tape_discard_merge_buffer(drive, 0);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001380 return idetape_position_tape(drive, 0, mt_count, 0);
1381 case MTFSR:
1382 case MTBSR:
1383 case MTLOCK:
Bartlomiej Zolnierkiewicz05780422008-10-10 22:39:38 +02001384 retval = ide_set_media_lock(drive, disk, 1);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001385 if (retval)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386 return retval;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001387 tape->door_locked = DOOR_EXPLICITLY_LOCKED;
1388 return 0;
1389 case MTUNLOCK:
Bartlomiej Zolnierkiewicz05780422008-10-10 22:39:38 +02001390 retval = ide_set_media_lock(drive, disk, 0);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001391 if (retval)
1392 return retval;
1393 tape->door_locked = DOOR_UNLOCKED;
1394 return 0;
1395 default:
1396 printk(KERN_ERR "ide-tape: MTIO operation %d not supported\n",
1397 mt_op);
1398 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399 }
1400}
1401
1402/*
Borislav Petkovd99c9da2008-02-02 19:56:51 +01001403 * Our character device ioctls. General mtio.h magnetic io commands are
1404 * supported here, and not in the corresponding block interface. Our own
1405 * ide-tape ioctls are supported on both interfaces.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406 */
Borislav Petkovd99c9da2008-02-02 19:56:51 +01001407static int idetape_chrdev_ioctl(struct inode *inode, struct file *file,
1408 unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409{
Borislav Petkov5aeddf92008-10-13 21:39:34 +02001410 struct ide_tape_obj *tape = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411 ide_drive_t *drive = tape->drive;
1412 struct mtop mtop;
1413 struct mtget mtget;
1414 struct mtpos mtpos;
Borislav Petkov54bb2072008-02-06 02:57:52 +01001415 int block_offset = 0, position = tape->first_frame;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416 void __user *argp = (void __user *)arg;
1417
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001418 debug_log(DBG_CHRDEV, "Enter %s, cmd=%u\n", __func__, cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419
Borislav Petkov54abf372008-02-06 02:57:52 +01001420 if (tape->chrdev_dir == IDETAPE_DIR_WRITE) {
Borislav Petkovd9df9372008-04-27 15:38:34 +02001421 ide_tape_flush_merge_buffer(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422 idetape_flush_tape_buffers(drive);
1423 }
1424 if (cmd == MTIOCGET || cmd == MTIOCPOS) {
Tejun Heo963da552009-04-19 08:46:02 +09001425 block_offset = tape->valid /
Borislav Petkov54bb2072008-02-06 02:57:52 +01001426 (tape->blk_size * tape->user_bs_factor);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001427 position = idetape_read_position(drive);
1428 if (position < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429 return -EIO;
1430 }
1431 switch (cmd) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001432 case MTIOCTOP:
1433 if (copy_from_user(&mtop, argp, sizeof(struct mtop)))
1434 return -EFAULT;
1435 return idetape_mtioctop(drive, mtop.mt_op, mtop.mt_count);
1436 case MTIOCGET:
1437 memset(&mtget, 0, sizeof(struct mtget));
1438 mtget.mt_type = MT_ISSCSI2;
1439 mtget.mt_blkno = position / tape->user_bs_factor - block_offset;
1440 mtget.mt_dsreg =
1441 ((tape->blk_size * tape->user_bs_factor)
1442 << MT_ST_BLKSIZE_SHIFT) & MT_ST_BLKSIZE_MASK;
Borislav Petkov54bb2072008-02-06 02:57:52 +01001443
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001444 if (tape->drv_write_prot)
1445 mtget.mt_gstat |= GMT_WR_PROT(0xffffffff);
1446
1447 if (copy_to_user(argp, &mtget, sizeof(struct mtget)))
1448 return -EFAULT;
1449 return 0;
1450 case MTIOCPOS:
1451 mtpos.mt_blkno = position / tape->user_bs_factor - block_offset;
1452 if (copy_to_user(argp, &mtpos, sizeof(struct mtpos)))
1453 return -EFAULT;
1454 return 0;
1455 default:
1456 if (tape->chrdev_dir == IDETAPE_DIR_READ)
Borislav Petkovec0fdb02008-04-27 15:38:34 +02001457 ide_tape_discard_merge_buffer(drive, 1);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001458 return idetape_blkdev_ioctl(drive, cmd, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459 }
1460}
1461
Borislav Petkov3cffb9c2008-02-02 19:56:50 +01001462/*
1463 * Do a mode sense page 0 with block descriptor and if it succeeds set the tape
1464 * block size with the reported value.
1465 */
1466static void ide_tape_get_bsize_from_bdesc(ide_drive_t *drive)
1467{
1468 idetape_tape_t *tape = drive->driver_data;
Borislav Petkovd236d742008-04-18 00:46:27 +02001469 struct ide_atapi_pc pc;
Borislav Petkov3cffb9c2008-02-02 19:56:50 +01001470
1471 idetape_create_mode_sense_cmd(&pc, IDETAPE_BLOCK_DESCRIPTOR);
Bartlomiej Zolnierkiewicz2ac07d92008-10-10 22:39:38 +02001472 if (ide_queue_pc_tail(drive, tape->disk, &pc)) {
Borislav Petkov3cffb9c2008-02-02 19:56:50 +01001473 printk(KERN_ERR "ide-tape: Can't get block descriptor\n");
Borislav Petkov54bb2072008-02-06 02:57:52 +01001474 if (tape->blk_size == 0) {
Borislav Petkov3cffb9c2008-02-02 19:56:50 +01001475 printk(KERN_WARNING "ide-tape: Cannot deal with zero "
1476 "block size, assuming 32k\n");
Borislav Petkov54bb2072008-02-06 02:57:52 +01001477 tape->blk_size = 32768;
Borislav Petkov3cffb9c2008-02-02 19:56:50 +01001478 }
1479 return;
1480 }
Borislav Petkovd236d742008-04-18 00:46:27 +02001481 tape->blk_size = (pc.buf[4 + 5] << 16) +
1482 (pc.buf[4 + 6] << 8) +
1483 pc.buf[4 + 7];
1484 tape->drv_write_prot = (pc.buf[2] & 0x80) >> 7;
Borislav Petkov3cffb9c2008-02-02 19:56:50 +01001485}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001487static int idetape_chrdev_open(struct inode *inode, struct file *filp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488{
1489 unsigned int minor = iminor(inode), i = minor & ~0xc0;
1490 ide_drive_t *drive;
1491 idetape_tape_t *tape;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492 int retval;
1493
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001494 if (i >= MAX_HWIFS * MAX_DRIVES)
1495 return -ENXIO;
1496
Jonathan Corbet04f4ac92008-05-15 12:01:56 -06001497 lock_kernel();
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001498 tape = ide_tape_chrdev_get(i);
Jonathan Corbet04f4ac92008-05-15 12:01:56 -06001499 if (!tape) {
1500 unlock_kernel();
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001501 return -ENXIO;
Jonathan Corbet04f4ac92008-05-15 12:01:56 -06001502 }
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001503
1504 debug_log(DBG_CHRDEV, "Enter %s\n", __func__);
1505
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506 /*
1507 * We really want to do nonseekable_open(inode, filp); here, but some
1508 * versions of tar incorrectly call lseek on tapes and bail out if that
1509 * fails. So we disallow pread() and pwrite(), but permit lseeks.
1510 */
1511 filp->f_mode &= ~(FMODE_PREAD | FMODE_PWRITE);
1512
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513 drive = tape->drive;
1514
1515 filp->private_data = tape;
1516
Borislav Petkov49d80782009-06-07 15:37:06 +02001517 if (test_and_set_bit(ilog2(IDE_AFLAG_BUSY), &drive->atapi_flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518 retval = -EBUSY;
1519 goto out_put_tape;
1520 }
1521
1522 retval = idetape_wait_ready(drive, 60 * HZ);
1523 if (retval) {
Borislav Petkov49d80782009-06-07 15:37:06 +02001524 clear_bit(ilog2(IDE_AFLAG_BUSY), &drive->atapi_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001525 printk(KERN_ERR "ide-tape: %s: drive not ready\n", tape->name);
1526 goto out_put_tape;
1527 }
1528
1529 idetape_read_position(drive);
Borislav Petkov49d80782009-06-07 15:37:06 +02001530 if (!test_bit(ilog2(IDE_AFLAG_ADDRESS_VALID), &drive->atapi_flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531 (void)idetape_rewind_tape(drive);
1532
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533 /* Read block size and write protect status from drive. */
Borislav Petkov3cffb9c2008-02-02 19:56:50 +01001534 ide_tape_get_bsize_from_bdesc(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535
1536 /* Set write protect flag if device is opened as read-only. */
1537 if ((filp->f_flags & O_ACCMODE) == O_RDONLY)
1538 tape->write_prot = 1;
1539 else
1540 tape->write_prot = tape->drv_write_prot;
1541
1542 /* Make sure drive isn't write protected if user wants to write. */
1543 if (tape->write_prot) {
1544 if ((filp->f_flags & O_ACCMODE) == O_WRONLY ||
1545 (filp->f_flags & O_ACCMODE) == O_RDWR) {
Borislav Petkov49d80782009-06-07 15:37:06 +02001546 clear_bit(ilog2(IDE_AFLAG_BUSY), &drive->atapi_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001547 retval = -EROFS;
1548 goto out_put_tape;
1549 }
1550 }
1551
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001552 /* Lock the tape drive door so user can't eject. */
Borislav Petkov54abf372008-02-06 02:57:52 +01001553 if (tape->chrdev_dir == IDETAPE_DIR_NONE) {
Bartlomiej Zolnierkiewicz05780422008-10-10 22:39:38 +02001554 if (!ide_set_media_lock(drive, tape->disk, 1)) {
Bartlomiej Zolnierkiewicz385a4b82008-10-10 22:39:37 +02001555 if (tape->door_locked != DOOR_EXPLICITLY_LOCKED)
1556 tape->door_locked = DOOR_LOCKED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557 }
1558 }
Jonathan Corbet04f4ac92008-05-15 12:01:56 -06001559 unlock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560 return 0;
1561
1562out_put_tape:
1563 ide_tape_put(tape);
Jonathan Corbet04f4ac92008-05-15 12:01:56 -06001564 unlock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565 return retval;
1566}
1567
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001568static void idetape_write_release(ide_drive_t *drive, unsigned int minor)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569{
1570 idetape_tape_t *tape = drive->driver_data;
1571
Borislav Petkovd9df9372008-04-27 15:38:34 +02001572 ide_tape_flush_merge_buffer(drive);
Tejun Heo963da552009-04-19 08:46:02 +09001573 tape->buf = kmalloc(tape->buffer_size, GFP_KERNEL);
1574 if (tape->buf != NULL) {
Borislav Petkov54bb2072008-02-06 02:57:52 +01001575 idetape_pad_zeros(drive, tape->blk_size *
1576 (tape->user_bs_factor - 1));
Tejun Heo963da552009-04-19 08:46:02 +09001577 kfree(tape->buf);
1578 tape->buf = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579 }
1580 idetape_write_filemark(drive);
1581 idetape_flush_tape_buffers(drive);
1582 idetape_flush_tape_buffers(drive);
1583}
1584
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001585static int idetape_chrdev_release(struct inode *inode, struct file *filp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001586{
Borislav Petkov5aeddf92008-10-13 21:39:34 +02001587 struct ide_tape_obj *tape = filp->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001588 ide_drive_t *drive = tape->drive;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001589 unsigned int minor = iminor(inode);
1590
1591 lock_kernel();
1592 tape = drive->driver_data;
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001593
1594 debug_log(DBG_CHRDEV, "Enter %s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001595
Borislav Petkov54abf372008-02-06 02:57:52 +01001596 if (tape->chrdev_dir == IDETAPE_DIR_WRITE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001597 idetape_write_release(drive, minor);
Borislav Petkov54abf372008-02-06 02:57:52 +01001598 if (tape->chrdev_dir == IDETAPE_DIR_READ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599 if (minor < 128)
Borislav Petkovec0fdb02008-04-27 15:38:34 +02001600 ide_tape_discard_merge_buffer(drive, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001601 }
Borislav Petkovf64eee72008-04-27 15:38:25 +02001602
Borislav Petkov49d80782009-06-07 15:37:06 +02001603 if (minor < 128 && test_bit(ilog2(IDE_AFLAG_MEDIUM_PRESENT),
1604 &drive->atapi_flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001605 (void) idetape_rewind_tape(drive);
Borislav Petkov49d80782009-06-07 15:37:06 +02001606
Borislav Petkov54abf372008-02-06 02:57:52 +01001607 if (tape->chrdev_dir == IDETAPE_DIR_NONE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608 if (tape->door_locked == DOOR_LOCKED) {
Bartlomiej Zolnierkiewicz05780422008-10-10 22:39:38 +02001609 if (!ide_set_media_lock(drive, tape->disk, 0))
Bartlomiej Zolnierkiewicz385a4b82008-10-10 22:39:37 +02001610 tape->door_locked = DOOR_UNLOCKED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001611 }
1612 }
Borislav Petkov49d80782009-06-07 15:37:06 +02001613 clear_bit(ilog2(IDE_AFLAG_BUSY), &drive->atapi_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001614 ide_tape_put(tape);
1615 unlock_kernel();
1616 return 0;
1617}
1618
Borislav Petkov6d29c8f2008-02-02 19:56:49 +01001619static void idetape_get_inquiry_results(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001620{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001621 idetape_tape_t *tape = drive->driver_data;
Borislav Petkovd236d742008-04-18 00:46:27 +02001622 struct ide_atapi_pc pc;
Bartlomiej Zolnierkiewicz41fa9f82009-03-31 20:15:25 +02001623 u8 pc_buf[256];
Borislav Petkov801bd322008-09-27 19:32:17 +02001624 char fw_rev[4], vendor_id[8], product_id[16];
Borislav Petkov6d29c8f2008-02-02 19:56:49 +01001625
Linus Torvalds1da177e2005-04-16 15:20:36 -07001626 idetape_create_inquiry_cmd(&pc);
Bartlomiej Zolnierkiewicz41fa9f82009-03-31 20:15:25 +02001627 pc.buf = &pc_buf[0];
1628 pc.buf_size = sizeof(pc_buf);
1629
Bartlomiej Zolnierkiewicz2ac07d92008-10-10 22:39:38 +02001630 if (ide_queue_pc_tail(drive, tape->disk, &pc)) {
Borislav Petkov6d29c8f2008-02-02 19:56:49 +01001631 printk(KERN_ERR "ide-tape: %s: can't get INQUIRY results\n",
1632 tape->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001633 return;
1634 }
Borislav Petkovd236d742008-04-18 00:46:27 +02001635 memcpy(vendor_id, &pc.buf[8], 8);
1636 memcpy(product_id, &pc.buf[16], 16);
1637 memcpy(fw_rev, &pc.buf[32], 4);
Borislav Petkov6d29c8f2008-02-02 19:56:49 +01001638
Borislav Petkov801bd322008-09-27 19:32:17 +02001639 ide_fixstring(vendor_id, 8, 0);
1640 ide_fixstring(product_id, 16, 0);
1641 ide_fixstring(fw_rev, 4, 0);
Borislav Petkov41f81d542008-02-06 02:57:52 +01001642
Borislav Petkov801bd322008-09-27 19:32:17 +02001643 printk(KERN_INFO "ide-tape: %s <-> %s: %.8s %.16s rev %.4s\n",
Borislav Petkov41f81d542008-02-06 02:57:52 +01001644 drive->name, tape->name, vendor_id, product_id, fw_rev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001645}
1646
1647/*
Borislav Petkovb6422012008-02-02 19:56:49 +01001648 * Ask the tape about its various parameters. In particular, we will adjust our
1649 * data transfer buffer size to the recommended value as returned by the tape.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001650 */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001651static void idetape_get_mode_sense_results(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001652{
1653 idetape_tape_t *tape = drive->driver_data;
Borislav Petkovd236d742008-04-18 00:46:27 +02001654 struct ide_atapi_pc pc;
Borislav Petkovb6422012008-02-02 19:56:49 +01001655 u8 *caps;
1656 u8 speed, max_speed;
Borislav Petkov47314fa2008-02-02 19:56:48 +01001657
Linus Torvalds1da177e2005-04-16 15:20:36 -07001658 idetape_create_mode_sense_cmd(&pc, IDETAPE_CAPABILITIES_PAGE);
Bartlomiej Zolnierkiewicz2ac07d92008-10-10 22:39:38 +02001659 if (ide_queue_pc_tail(drive, tape->disk, &pc)) {
Borislav Petkovb6422012008-02-02 19:56:49 +01001660 printk(KERN_ERR "ide-tape: Can't get tape parameters - assuming"
1661 " some default values\n");
Borislav Petkov54bb2072008-02-06 02:57:52 +01001662 tape->blk_size = 512;
Borislav Petkovb6422012008-02-02 19:56:49 +01001663 put_unaligned(52, (u16 *)&tape->caps[12]);
1664 put_unaligned(540, (u16 *)&tape->caps[14]);
1665 put_unaligned(6*52, (u16 *)&tape->caps[16]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001666 return;
1667 }
Borislav Petkovd236d742008-04-18 00:46:27 +02001668 caps = pc.buf + 4 + pc.buf[3];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001669
Borislav Petkovb6422012008-02-02 19:56:49 +01001670 /* convert to host order and save for later use */
Harvey Harrisoncd740ab2008-07-24 22:53:33 +02001671 speed = be16_to_cpup((__be16 *)&caps[14]);
1672 max_speed = be16_to_cpup((__be16 *)&caps[8]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001673
Harvey Harrisoncd740ab2008-07-24 22:53:33 +02001674 *(u16 *)&caps[8] = max_speed;
1675 *(u16 *)&caps[12] = be16_to_cpup((__be16 *)&caps[12]);
1676 *(u16 *)&caps[14] = speed;
1677 *(u16 *)&caps[16] = be16_to_cpup((__be16 *)&caps[16]);
Borislav Petkovb6422012008-02-02 19:56:49 +01001678
1679 if (!speed) {
1680 printk(KERN_INFO "ide-tape: %s: invalid tape speed "
1681 "(assuming 650KB/sec)\n", drive->name);
Harvey Harrisoncd740ab2008-07-24 22:53:33 +02001682 *(u16 *)&caps[14] = 650;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001683 }
Borislav Petkovb6422012008-02-02 19:56:49 +01001684 if (!max_speed) {
1685 printk(KERN_INFO "ide-tape: %s: invalid max_speed "
1686 "(assuming 650KB/sec)\n", drive->name);
Harvey Harrisoncd740ab2008-07-24 22:53:33 +02001687 *(u16 *)&caps[8] = 650;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001688 }
1689
Borislav Petkovb6422012008-02-02 19:56:49 +01001690 memcpy(&tape->caps, caps, 20);
Bartlomiej Zolnierkiewicz05780422008-10-10 22:39:38 +02001691
1692 /* device lacks locking support according to capabilities page */
1693 if ((caps[6] & 1) == 0)
Bartlomiej Zolnierkiewicz42619d32008-10-17 18:09:11 +02001694 drive->dev_flags &= ~IDE_DFLAG_DOORLOCKING;
Bartlomiej Zolnierkiewicz05780422008-10-10 22:39:38 +02001695
Borislav Petkovb6422012008-02-02 19:56:49 +01001696 if (caps[7] & 0x02)
Borislav Petkov54bb2072008-02-06 02:57:52 +01001697 tape->blk_size = 512;
Borislav Petkovb6422012008-02-02 19:56:49 +01001698 else if (caps[7] & 0x04)
Borislav Petkov54bb2072008-02-06 02:57:52 +01001699 tape->blk_size = 1024;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001700}
1701
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +02001702#ifdef CONFIG_IDE_PROC_FS
Bartlomiej Zolnierkiewicz8185d5a2008-10-10 22:39:28 +02001703#define ide_tape_devset_get(name, field) \
1704static int get_##name(ide_drive_t *drive) \
1705{ \
1706 idetape_tape_t *tape = drive->driver_data; \
1707 return tape->field; \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001708}
Bartlomiej Zolnierkiewicz8185d5a2008-10-10 22:39:28 +02001709
1710#define ide_tape_devset_set(name, field) \
1711static int set_##name(ide_drive_t *drive, int arg) \
1712{ \
1713 idetape_tape_t *tape = drive->driver_data; \
1714 tape->field = arg; \
1715 return 0; \
1716}
1717
Elias Oltmanns92f1f8f2008-10-10 22:39:40 +02001718#define ide_tape_devset_rw_field(_name, _field) \
Bartlomiej Zolnierkiewicz8185d5a2008-10-10 22:39:28 +02001719ide_tape_devset_get(_name, _field) \
1720ide_tape_devset_set(_name, _field) \
Elias Oltmanns92f1f8f2008-10-10 22:39:40 +02001721IDE_DEVSET(_name, DS_SYNC, get_##_name, set_##_name)
Bartlomiej Zolnierkiewicz8185d5a2008-10-10 22:39:28 +02001722
Elias Oltmanns92f1f8f2008-10-10 22:39:40 +02001723#define ide_tape_devset_r_field(_name, _field) \
Bartlomiej Zolnierkiewicz8185d5a2008-10-10 22:39:28 +02001724ide_tape_devset_get(_name, _field) \
Elias Oltmanns92f1f8f2008-10-10 22:39:40 +02001725IDE_DEVSET(_name, 0, get_##_name, NULL)
Bartlomiej Zolnierkiewicz8185d5a2008-10-10 22:39:28 +02001726
1727static int mulf_tdsc(ide_drive_t *drive) { return 1000; }
1728static int divf_tdsc(ide_drive_t *drive) { return HZ; }
1729static int divf_buffer(ide_drive_t *drive) { return 2; }
1730static int divf_buffer_size(ide_drive_t *drive) { return 1024; }
1731
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +02001732ide_devset_rw_flag(dsc_overlap, IDE_DFLAG_DSC_OVERLAP);
Bartlomiej Zolnierkiewicz8185d5a2008-10-10 22:39:28 +02001733
Elias Oltmanns92f1f8f2008-10-10 22:39:40 +02001734ide_tape_devset_rw_field(debug_mask, debug_mask);
1735ide_tape_devset_rw_field(tdsc, best_dsc_rw_freq);
Bartlomiej Zolnierkiewicz8185d5a2008-10-10 22:39:28 +02001736
Elias Oltmanns92f1f8f2008-10-10 22:39:40 +02001737ide_tape_devset_r_field(avg_speed, avg_speed);
1738ide_tape_devset_r_field(speed, caps[14]);
1739ide_tape_devset_r_field(buffer, caps[16]);
1740ide_tape_devset_r_field(buffer_size, buffer_size);
Bartlomiej Zolnierkiewicz8185d5a2008-10-10 22:39:28 +02001741
Elias Oltmanns92f1f8f2008-10-10 22:39:40 +02001742static const struct ide_proc_devset idetape_settings[] = {
1743 __IDE_PROC_DEVSET(avg_speed, 0, 0xffff, NULL, NULL),
1744 __IDE_PROC_DEVSET(buffer, 0, 0xffff, NULL, divf_buffer),
1745 __IDE_PROC_DEVSET(buffer_size, 0, 0xffff, NULL, divf_buffer_size),
1746 __IDE_PROC_DEVSET(debug_mask, 0, 0xffff, NULL, NULL),
1747 __IDE_PROC_DEVSET(dsc_overlap, 0, 1, NULL, NULL),
1748 __IDE_PROC_DEVSET(speed, 0, 0xffff, NULL, NULL),
1749 __IDE_PROC_DEVSET(tdsc, IDETAPE_DSC_RW_MIN, IDETAPE_DSC_RW_MAX,
1750 mulf_tdsc, divf_tdsc),
Hannes Eder71bfc7a2009-03-05 16:10:56 +01001751 { NULL },
Bartlomiej Zolnierkiewicz8185d5a2008-10-10 22:39:28 +02001752};
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +02001753#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001754
1755/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001756 * The function below is called to:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001757 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001758 * 1. Initialize our various state variables.
1759 * 2. Ask the tape for its capabilities.
1760 * 3. Allocate a buffer which will be used for data transfer. The buffer size
1761 * is chosen based on the recommendation which we received in step 2.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001762 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001763 * Note that at this point ide.c already assigned us an irq, so that we can
1764 * queue requests here and wait for their completion.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001765 */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001766static void idetape_setup(ide_drive_t *drive, idetape_tape_t *tape, int minor)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767{
Borislav Petkov83042b22008-04-27 15:38:27 +02001768 unsigned long t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001769 int speed;
Borislav Petkovf73850a2008-04-27 15:38:33 +02001770 int buffer_size;
Borislav Petkovb6422012008-02-02 19:56:49 +01001771 u16 *ctl = (u16 *)&tape->caps[12];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001772
Bartlomiej Zolnierkiewicz85e39032008-10-13 21:39:32 +02001773 drive->pc_callback = ide_tape_callback;
Borislav Petkov776bb022008-07-23 19:55:59 +02001774
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +02001775 drive->dev_flags |= IDE_DFLAG_DSC_OVERLAP;
1776
Bartlomiej Zolnierkiewicz4166c192008-02-01 23:09:30 +01001777 if (drive->hwif->host_flags & IDE_HFLAG_NO_DSC) {
1778 printk(KERN_INFO "ide-tape: %s: disabling DSC overlap\n",
1779 tape->name);
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +02001780 drive->dev_flags &= ~IDE_DFLAG_DSC_OVERLAP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001781 }
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +02001782
Linus Torvalds1da177e2005-04-16 15:20:36 -07001783 /* Seagate Travan drives do not support DSC overlap. */
Bartlomiej Zolnierkiewicz4dde4492008-10-10 22:39:19 +02001784 if (strstr((char *)&drive->id[ATA_ID_PROD], "Seagate STT3401"))
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +02001785 drive->dev_flags &= ~IDE_DFLAG_DSC_OVERLAP;
1786
Linus Torvalds1da177e2005-04-16 15:20:36 -07001787 tape->minor = minor;
1788 tape->name[0] = 'h';
1789 tape->name[1] = 't';
1790 tape->name[2] = '0' + minor;
Borislav Petkov54abf372008-02-06 02:57:52 +01001791 tape->chrdev_dir = IDETAPE_DIR_NONE;
Bartlomiej Zolnierkiewicz4dde4492008-10-10 22:39:19 +02001792
Linus Torvalds1da177e2005-04-16 15:20:36 -07001793 idetape_get_inquiry_results(drive);
1794 idetape_get_mode_sense_results(drive);
Borislav Petkov3cffb9c2008-02-02 19:56:50 +01001795 ide_tape_get_bsize_from_bdesc(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001796 tape->user_bs_factor = 1;
Borislav Petkovf73850a2008-04-27 15:38:33 +02001797 tape->buffer_size = *ctl * tape->blk_size;
1798 while (tape->buffer_size > 0xffff) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001799 printk(KERN_NOTICE "ide-tape: decreasing stage size\n");
Borislav Petkovb6422012008-02-02 19:56:49 +01001800 *ctl /= 2;
Borislav Petkovf73850a2008-04-27 15:38:33 +02001801 tape->buffer_size = *ctl * tape->blk_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001802 }
Borislav Petkovf73850a2008-04-27 15:38:33 +02001803 buffer_size = tape->buffer_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001804
Borislav Petkov83042b22008-04-27 15:38:27 +02001805 /* select the "best" DSC read/write polling freq */
Borislav Petkovb6422012008-02-02 19:56:49 +01001806 speed = max(*(u16 *)&tape->caps[14], *(u16 *)&tape->caps[8]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807
Borislav Petkovf73850a2008-04-27 15:38:33 +02001808 t = (IDETAPE_FIFO_THRESHOLD * tape->buffer_size * HZ) / (speed * 1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001809
1810 /*
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001811 * Ensure that the number we got makes sense; limit it within
1812 * IDETAPE_DSC_RW_MIN and IDETAPE_DSC_RW_MAX.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001813 */
Harvey Harrisona792bd52008-07-15 21:21:41 +02001814 tape->best_dsc_rw_freq = clamp_t(unsigned long, t, IDETAPE_DSC_RW_MIN,
1815 IDETAPE_DSC_RW_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001816 printk(KERN_INFO "ide-tape: %s <-> %s: %dKBps, %d*%dkB buffer, "
Borislav Petkov83042b22008-04-27 15:38:27 +02001817 "%lums tDSC%s\n",
Borislav Petkovb6422012008-02-02 19:56:49 +01001818 drive->name, tape->name, *(u16 *)&tape->caps[14],
Borislav Petkovf73850a2008-04-27 15:38:33 +02001819 (*(u16 *)&tape->caps[16] * 512) / tape->buffer_size,
1820 tape->buffer_size / 1024,
Borislav Petkov54bb2072008-02-06 02:57:52 +01001821 tape->best_dsc_rw_freq * 1000 / HZ,
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +02001822 (drive->dev_flags & IDE_DFLAG_USING_DMA) ? ", DMA" : "");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001823
Bartlomiej Zolnierkiewicz1e874f42008-10-10 22:39:27 +02001824 ide_proc_register_driver(drive, tape->driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825}
1826
Russell King4031bbe2006-01-06 11:41:00 +00001827static void ide_tape_remove(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001828{
1829 idetape_tape_t *tape = drive->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001830
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +02001831 ide_proc_unregister_driver(drive, tape->driver);
Bartlomiej Zolnierkiewicz8fed4362009-02-25 20:28:24 +01001832 device_del(&tape->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001833 ide_unregister_region(tape->disk);
1834
Bartlomiej Zolnierkiewicz8fed4362009-02-25 20:28:24 +01001835 mutex_lock(&idetape_ref_mutex);
1836 put_device(&tape->dev);
1837 mutex_unlock(&idetape_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001838}
1839
Bartlomiej Zolnierkiewicz8fed4362009-02-25 20:28:24 +01001840static void ide_tape_release(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001841{
Bartlomiej Zolnierkiewicz8fed4362009-02-25 20:28:24 +01001842 struct ide_tape_obj *tape = to_ide_drv(dev, ide_tape_obj);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001843 ide_drive_t *drive = tape->drive;
1844 struct gendisk *g = tape->disk;
1845
Tejun Heo963da552009-04-19 08:46:02 +09001846 BUG_ON(tape->valid);
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02001847
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +02001848 drive->dev_flags &= ~IDE_DFLAG_DSC_OVERLAP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001849 drive->driver_data = NULL;
Tony Jonesdbc12722007-09-25 02:03:03 +02001850 device_destroy(idetape_sysfs_class, MKDEV(IDETAPE_MAJOR, tape->minor));
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001851 device_destroy(idetape_sysfs_class,
1852 MKDEV(IDETAPE_MAJOR, tape->minor + 128));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001853 idetape_devs[tape->minor] = NULL;
1854 g->private_data = NULL;
1855 put_disk(g);
1856 kfree(tape);
1857}
1858
Bartlomiej Zolnierkiewiczecfd80e2007-05-10 00:01:09 +02001859#ifdef CONFIG_IDE_PROC_FS
Linus Torvalds1da177e2005-04-16 15:20:36 -07001860static int proc_idetape_read_name
1861 (char *page, char **start, off_t off, int count, int *eof, void *data)
1862{
1863 ide_drive_t *drive = (ide_drive_t *) data;
1864 idetape_tape_t *tape = drive->driver_data;
1865 char *out = page;
1866 int len;
1867
1868 len = sprintf(out, "%s\n", tape->name);
1869 PROC_IDE_READ_RETURN(page, start, off, count, eof, len);
1870}
1871
1872static ide_proc_entry_t idetape_proc[] = {
1873 { "capacity", S_IFREG|S_IRUGO, proc_ide_read_capacity, NULL },
1874 { "name", S_IFREG|S_IRUGO, proc_idetape_read_name, NULL },
1875 { NULL, 0, NULL, NULL }
1876};
Bartlomiej Zolnierkiewicz79cb3802008-10-17 18:09:13 +02001877
1878static ide_proc_entry_t *ide_tape_proc_entries(ide_drive_t *drive)
1879{
1880 return idetape_proc;
1881}
1882
1883static const struct ide_proc_devset *ide_tape_proc_devsets(ide_drive_t *drive)
1884{
1885 return idetape_settings;
1886}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001887#endif
1888
Russell King4031bbe2006-01-06 11:41:00 +00001889static int ide_tape_probe(ide_drive_t *);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001890
Bartlomiej Zolnierkiewicz7f3c8682009-01-06 17:20:53 +01001891static struct ide_driver idetape_driver = {
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02001892 .gen_driver = {
Laurent Riffard4ef3b8f2005-11-18 22:15:40 +01001893 .owner = THIS_MODULE,
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02001894 .name = "ide-tape",
1895 .bus = &ide_bus_type,
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02001896 },
Russell King4031bbe2006-01-06 11:41:00 +00001897 .probe = ide_tape_probe,
1898 .remove = ide_tape_remove,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001899 .version = IDETAPE_VERSION,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001900 .do_request = idetape_do_request,
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +02001901#ifdef CONFIG_IDE_PROC_FS
Bartlomiej Zolnierkiewicz79cb3802008-10-17 18:09:13 +02001902 .proc_entries = ide_tape_proc_entries,
1903 .proc_devsets = ide_tape_proc_devsets,
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +02001904#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001905};
1906
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001907/* Our character device supporting functions, passed to register_chrdev. */
Arjan van de Ven2b8693c2007-02-12 00:55:32 -08001908static const struct file_operations idetape_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001909 .owner = THIS_MODULE,
1910 .read = idetape_chrdev_read,
1911 .write = idetape_chrdev_write,
1912 .ioctl = idetape_chrdev_ioctl,
1913 .open = idetape_chrdev_open,
1914 .release = idetape_chrdev_release,
1915};
1916
Al Viroa4600f82008-03-02 10:27:58 -05001917static int idetape_open(struct block_device *bdev, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001918{
Al Viroa4600f82008-03-02 10:27:58 -05001919 struct ide_tape_obj *tape = ide_tape_get(bdev->bd_disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001920
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001921 if (!tape)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001922 return -ENXIO;
1923
Linus Torvalds1da177e2005-04-16 15:20:36 -07001924 return 0;
1925}
1926
Al Viroa4600f82008-03-02 10:27:58 -05001927static int idetape_release(struct gendisk *disk, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001928{
Borislav Petkov5aeddf92008-10-13 21:39:34 +02001929 struct ide_tape_obj *tape = ide_drv_g(disk, ide_tape_obj);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001930
1931 ide_tape_put(tape);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001932 return 0;
1933}
1934
Al Viroa4600f82008-03-02 10:27:58 -05001935static int idetape_ioctl(struct block_device *bdev, fmode_t mode,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001936 unsigned int cmd, unsigned long arg)
1937{
Borislav Petkov5aeddf92008-10-13 21:39:34 +02001938 struct ide_tape_obj *tape = ide_drv_g(bdev->bd_disk, ide_tape_obj);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001939 ide_drive_t *drive = tape->drive;
Al Viro1bddd9e2008-09-02 17:19:43 -04001940 int err = generic_ide_ioctl(drive, bdev, cmd, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001941 if (err == -EINVAL)
1942 err = idetape_blkdev_ioctl(drive, cmd, arg);
1943 return err;
1944}
1945
1946static struct block_device_operations idetape_block_ops = {
1947 .owner = THIS_MODULE,
Al Viroa4600f82008-03-02 10:27:58 -05001948 .open = idetape_open,
1949 .release = idetape_release,
1950 .locked_ioctl = idetape_ioctl,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001951};
1952
Russell King4031bbe2006-01-06 11:41:00 +00001953static int ide_tape_probe(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001954{
1955 idetape_tape_t *tape;
1956 struct gendisk *g;
1957 int minor;
1958
1959 if (!strstr("ide-tape", drive->driver_req))
1960 goto failed;
Bartlomiej Zolnierkiewicz2a924662008-10-10 22:39:24 +02001961
Linus Torvalds1da177e2005-04-16 15:20:36 -07001962 if (drive->media != ide_tape)
1963 goto failed;
Bartlomiej Zolnierkiewicz2a924662008-10-10 22:39:24 +02001964
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +02001965 if ((drive->dev_flags & IDE_DFLAG_ID_READ) &&
1966 ide_check_atapi_device(drive, DRV_NAME) == 0) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001967 printk(KERN_ERR "ide-tape: %s: not supported by this version of"
1968 " the driver\n", drive->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001969 goto failed;
1970 }
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001971 tape = kzalloc(sizeof(idetape_tape_t), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001972 if (tape == NULL) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001973 printk(KERN_ERR "ide-tape: %s: Can't allocate a tape struct\n",
1974 drive->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001975 goto failed;
1976 }
1977
1978 g = alloc_disk(1 << PARTN_BITS);
1979 if (!g)
1980 goto out_free_tape;
1981
1982 ide_init_disk(g, drive);
1983
Bartlomiej Zolnierkiewicz8fed4362009-02-25 20:28:24 +01001984 tape->dev.parent = &drive->gendev;
1985 tape->dev.release = ide_tape_release;
1986 dev_set_name(&tape->dev, dev_name(&drive->gendev));
1987
1988 if (device_register(&tape->dev))
1989 goto out_free_disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001990
1991 tape->drive = drive;
1992 tape->driver = &idetape_driver;
1993 tape->disk = g;
1994
1995 g->private_data = &tape->driver;
1996
1997 drive->driver_data = tape;
1998
Arjan van de Vencf8b8972006-03-23 03:00:45 -08001999 mutex_lock(&idetape_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002000 for (minor = 0; idetape_devs[minor]; minor++)
2001 ;
2002 idetape_devs[minor] = tape;
Arjan van de Vencf8b8972006-03-23 03:00:45 -08002003 mutex_unlock(&idetape_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002004
2005 idetape_setup(drive, tape, minor);
2006
Greg Kroah-Hartman3ee074b2008-07-21 20:03:34 -07002007 device_create(idetape_sysfs_class, &drive->gendev,
2008 MKDEV(IDETAPE_MAJOR, minor), NULL, "%s", tape->name);
2009 device_create(idetape_sysfs_class, &drive->gendev,
2010 MKDEV(IDETAPE_MAJOR, minor + 128), NULL,
2011 "n%s", tape->name);
Will Dysond5dee802005-09-16 02:55:07 -07002012
Linus Torvalds1da177e2005-04-16 15:20:36 -07002013 g->fops = &idetape_block_ops;
2014 ide_register_region(g);
2015
2016 return 0;
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02002017
Bartlomiej Zolnierkiewicz8fed4362009-02-25 20:28:24 +01002018out_free_disk:
2019 put_disk(g);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002020out_free_tape:
2021 kfree(tape);
2022failed:
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02002023 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002024}
2025
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002026static void __exit idetape_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002027{
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02002028 driver_unregister(&idetape_driver.gen_driver);
Will Dysond5dee802005-09-16 02:55:07 -07002029 class_destroy(idetape_sysfs_class);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002030 unregister_chrdev(IDETAPE_MAJOR, "ht");
2031}
2032
Bartlomiej Zolnierkiewicz17514e82005-11-19 22:24:35 +01002033static int __init idetape_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002034{
Will Dysond5dee802005-09-16 02:55:07 -07002035 int error = 1;
2036 idetape_sysfs_class = class_create(THIS_MODULE, "ide_tape");
2037 if (IS_ERR(idetape_sysfs_class)) {
2038 idetape_sysfs_class = NULL;
2039 printk(KERN_ERR "Unable to create sysfs class for ide tapes\n");
2040 error = -EBUSY;
2041 goto out;
2042 }
2043
Linus Torvalds1da177e2005-04-16 15:20:36 -07002044 if (register_chrdev(IDETAPE_MAJOR, "ht", &idetape_fops)) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002045 printk(KERN_ERR "ide-tape: Failed to register chrdev"
2046 " interface\n");
Will Dysond5dee802005-09-16 02:55:07 -07002047 error = -EBUSY;
2048 goto out_free_class;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002049 }
Will Dysond5dee802005-09-16 02:55:07 -07002050
2051 error = driver_register(&idetape_driver.gen_driver);
2052 if (error)
2053 goto out_free_driver;
2054
2055 return 0;
2056
2057out_free_driver:
2058 driver_unregister(&idetape_driver.gen_driver);
2059out_free_class:
2060 class_destroy(idetape_sysfs_class);
2061out:
2062 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002063}
2064
Kay Sievers263756e2005-12-12 18:03:44 +01002065MODULE_ALIAS("ide:*m-tape*");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002066module_init(idetape_init);
2067module_exit(idetape_exit);
2068MODULE_ALIAS_CHARDEV_MAJOR(IDETAPE_MAJOR);
Borislav Petkov9c145762008-02-06 02:57:54 +01002069MODULE_DESCRIPTION("ATAPI Streaming TAPE Driver");
2070MODULE_LICENSE("GPL");