blob: ce8237d361596840cb679c6f41dbd89bb0ad7d48 [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>
Alexey Dobriyan6d703a82009-09-01 17:52:57 -070034#include <linux/seq_file.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070035#include <linux/slab.h>
36#include <linux/pci.h>
37#include <linux/ide.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070038#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/uaccess.h>
45#include <linux/io.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070046#include <asm/unaligned.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070047#include <linux/mtio.h>
48
Borislav Petkov8004a8c2008-02-06 02:57:51 +010049/* define to see debug info */
Borislav Petkove972d702009-07-17 23:55:16 +000050#undef IDETAPE_DEBUG_LOG
Borislav Petkov8004a8c2008-02-06 02:57:51 +010051
Borislav Petkove972d702009-07-17 23:55:16 +000052#ifdef IDETAPE_DEBUG_LOG
53#define ide_debug_log(lvl, fmt, args...) __ide_debug_log(lvl, fmt, ## args)
Borislav Petkov8004a8c2008-02-06 02:57:51 +010054#else
Borislav Petkove972d702009-07-17 23:55:16 +000055#define ide_debug_log(lvl, fmt, args...) do {} while (0)
Borislav Petkov8004a8c2008-02-06 02:57:51 +010056#endif
57
Linus Torvalds1da177e2005-04-16 15:20:36 -070058/**************************** Tunable parameters *****************************/
Linus Torvalds1da177e2005-04-16 15:20:36 -070059/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +010060 * After each failed packet command we issue a request sense command and retry
61 * the packet command IDETAPE_MAX_PC_RETRIES times.
Linus Torvalds1da177e2005-04-16 15:20:36 -070062 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +010063 * Setting IDETAPE_MAX_PC_RETRIES to 0 will disable retries.
Linus Torvalds1da177e2005-04-16 15:20:36 -070064 */
65#define IDETAPE_MAX_PC_RETRIES 3
66
67/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +010068 * The following parameter is used to select the point in the internal tape fifo
69 * in which we will start to refill the buffer. Decreasing the following
70 * parameter will improve the system's latency and interactive response, while
71 * using a high value might improve system throughput.
Linus Torvalds1da177e2005-04-16 15:20:36 -070072 */
Borislav Petkov3c98bf32008-02-06 02:57:53 +010073#define IDETAPE_FIFO_THRESHOLD 2
Linus Torvalds1da177e2005-04-16 15:20:36 -070074
75/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +010076 * DSC polling parameters.
Linus Torvalds1da177e2005-04-16 15:20:36 -070077 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +010078 * Polling for DSC (a single bit in the status register) is a very important
79 * function in ide-tape. There are two cases in which we poll for DSC:
Linus Torvalds1da177e2005-04-16 15:20:36 -070080 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +010081 * 1. Before a read/write packet command, to ensure that we can transfer data
82 * from/to the tape's data buffers, without causing an actual media access.
83 * In case the tape is not ready yet, we take out our request from the device
84 * request queue, so that ide.c could service requests from the other device
85 * on the same interface in the meantime.
Linus Torvalds1da177e2005-04-16 15:20:36 -070086 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +010087 * 2. After the successful initialization of a "media access packet command",
88 * which is a command that can take a long time to complete (the interval can
89 * range from several seconds to even an hour). Again, we postpone our request
90 * in the middle to free the bus for the other device. The polling frequency
91 * here should be lower than the read/write frequency since those media access
92 * commands are slow. We start from a "fast" frequency - IDETAPE_DSC_MA_FAST
93 * (1 second), and if we don't receive DSC after IDETAPE_DSC_MA_THRESHOLD
94 * (5 min), we switch it to a lower frequency - IDETAPE_DSC_MA_SLOW (1 min).
Linus Torvalds1da177e2005-04-16 15:20:36 -070095 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +010096 * We also set a timeout for the timer, in case something goes wrong. The
97 * timeout should be longer then the maximum execution time of a tape operation.
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 */
Borislav Petkov3c98bf32008-02-06 02:57:53 +010099
100/* DSC timings. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101#define IDETAPE_DSC_RW_MIN 5*HZ/100 /* 50 msec */
102#define IDETAPE_DSC_RW_MAX 40*HZ/100 /* 400 msec */
103#define IDETAPE_DSC_RW_TIMEOUT 2*60*HZ /* 2 minutes */
104#define IDETAPE_DSC_MA_FAST 2*HZ /* 2 seconds */
105#define IDETAPE_DSC_MA_THRESHOLD 5*60*HZ /* 5 minutes */
106#define IDETAPE_DSC_MA_SLOW 30*HZ /* 30 seconds */
107#define IDETAPE_DSC_MA_TIMEOUT 2*60*60*HZ /* 2 hours */
108
109/*************************** End of tunable parameters ***********************/
110
Borislav Petkov54abf372008-02-06 02:57:52 +0100111/* tape directions */
112enum {
113 IDETAPE_DIR_NONE = (1 << 0),
114 IDETAPE_DIR_READ = (1 << 1),
115 IDETAPE_DIR_WRITE = (1 << 2),
116};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117
Borislav Petkov03056b92008-04-18 00:46:26 +0200118/* Tape door status */
119#define DOOR_UNLOCKED 0
120#define DOOR_LOCKED 1
121#define DOOR_EXPLICITLY_LOCKED 2
122
123/* Some defines for the SPACE command */
124#define IDETAPE_SPACE_OVER_FILEMARK 1
125#define IDETAPE_SPACE_TO_EOD 3
126
127/* Some defines for the LOAD UNLOAD command */
128#define IDETAPE_LU_LOAD_MASK 1
129#define IDETAPE_LU_RETENSION_MASK 2
130#define IDETAPE_LU_EOT_MASK 4
131
Borislav Petkov03056b92008-04-18 00:46:26 +0200132/* Structures related to the SELECT SENSE / MODE SENSE packet commands. */
133#define IDETAPE_BLOCK_DESCRIPTOR 0
134#define IDETAPE_CAPABILITIES_PAGE 0x2a
135
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100137 * Most of our global data which we need to save even as we leave the driver due
138 * to an interrupt or a timer event is stored in the struct defined below.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 */
140typedef struct ide_tape_obj {
Bartlomiej Zolnierkiewicz7f3c8682009-01-06 17:20:53 +0100141 ide_drive_t *drive;
142 struct ide_driver *driver;
143 struct gendisk *disk;
Bartlomiej Zolnierkiewicz8fed4362009-02-25 20:28:24 +0100144 struct device dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145
Bartlomiej Zolnierkiewicz2e8a6f82008-10-10 22:39:36 +0200146 /* used by REQ_IDETAPE_{READ,WRITE} requests */
147 struct ide_atapi_pc queued_pc;
Bartlomiej Zolnierkiewicz394a4c22008-10-10 22:39:35 +0200148
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149 /*
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100150 * DSC polling variables.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100152 * While polling for DSC we use postponed_rq to postpone the current
153 * request so that ide.c will be able to service pending requests on the
154 * other device. Note that at most we will have only one DSC (usually
Borislav Petkov5bd50dc2008-04-27 15:38:28 +0200155 * data transfer) request in the device request queue.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 */
Borislav Petkov6f3848a2009-07-21 23:08:23 -0700157 bool postponed_rq;
158
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 /* The time in which we started polling for DSC */
160 unsigned long dsc_polling_start;
161 /* Timer used to poll for dsc */
162 struct timer_list dsc_timer;
163 /* Read/Write dsc polling frequency */
Borislav Petkov54bb2072008-02-06 02:57:52 +0100164 unsigned long best_dsc_rw_freq;
165 unsigned long dsc_poll_freq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 unsigned long dsc_timeout;
167
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100168 /* Read position information */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 u8 partition;
170 /* Current block */
Borislav Petkov54bb2072008-02-06 02:57:52 +0100171 unsigned int first_frame;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100173 /* Last error information */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 u8 sense_key, asc, ascq;
175
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100176 /* Character device operation */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 unsigned int minor;
178 /* device name */
179 char name[4];
180 /* Current character device data transfer direction */
Borislav Petkov54abf372008-02-06 02:57:52 +0100181 u8 chrdev_dir;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182
Borislav Petkov54bb2072008-02-06 02:57:52 +0100183 /* tape block size, usually 512 or 1024 bytes */
184 unsigned short blk_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 int user_bs_factor;
Borislav Petkovb6422012008-02-02 19:56:49 +0100186
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 /* Copy of the tape's Capabilities and Mechanical Page */
Borislav Petkovb6422012008-02-02 19:56:49 +0100188 u8 caps[20];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189
190 /*
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100191 * Active data transfer request parameters.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100193 * At most, there is only one ide-tape originated data transfer request
194 * in the device request queue. This allows ide.c to easily service
195 * requests from the other device when we postpone our active request.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 */
Borislav Petkov83042b22008-04-27 15:38:27 +0200197
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100198 /* Data buffer size chosen based on the tape's recommendation */
Borislav Petkovf73850a2008-04-27 15:38:33 +0200199 int buffer_size;
Tejun Heo963da552009-04-19 08:46:02 +0900200 /* Staging buffer of buffer_size bytes */
201 void *buf;
202 /* The read/write cursor */
203 void *cur;
204 /* The number of valid bytes in buf */
205 size_t valid;
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100206
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100207 /* Measures average tape speed */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 unsigned long avg_time;
209 int avg_size;
210 int avg_speed;
211
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 /* the door is currently locked */
213 int door_locked;
214 /* the tape hardware is write protected */
215 char drv_write_prot;
216 /* the tape is write protected (hardware or opened as read-only) */
217 char write_prot;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218} idetape_tape_t;
219
Arnd Bergmann2a48fc02010-06-02 14:28:52 +0200220static DEFINE_MUTEX(ide_tape_mutex);
Arjan van de Vencf8b8972006-03-23 03:00:45 -0800221static DEFINE_MUTEX(idetape_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222
Borislav Petkovcbba2fa2009-10-11 00:25:19 +0000223static DEFINE_MUTEX(idetape_chrdev_mutex);
224
Will Dysond5dee802005-09-16 02:55:07 -0700225static struct class *idetape_sysfs_class;
226
Bartlomiej Zolnierkiewicz8fed4362009-02-25 20:28:24 +0100227static void ide_tape_release(struct device *);
Bartlomiej Zolnierkiewicz08da5912008-07-24 22:53:15 +0200228
Borislav Petkov9d01e4c2009-06-08 22:03:03 +0200229static struct ide_tape_obj *idetape_devs[MAX_HWIFS * MAX_DRIVES];
230
231static struct ide_tape_obj *ide_tape_get(struct gendisk *disk, bool cdev,
232 unsigned int i)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233{
234 struct ide_tape_obj *tape = NULL;
235
Arjan van de Vencf8b8972006-03-23 03:00:45 -0800236 mutex_lock(&idetape_ref_mutex);
Borislav Petkov9d01e4c2009-06-08 22:03:03 +0200237
238 if (cdev)
239 tape = idetape_devs[i];
240 else
241 tape = ide_drv_g(disk, ide_tape_obj);
242
Bartlomiej Zolnierkiewicz08da5912008-07-24 22:53:15 +0200243 if (tape) {
Bartlomiej Zolnierkiewiczd3e33ff2008-08-05 18:16:59 +0200244 if (ide_device_get(tape->drive))
Bartlomiej Zolnierkiewicz08da5912008-07-24 22:53:15 +0200245 tape = NULL;
Bartlomiej Zolnierkiewiczd3e33ff2008-08-05 18:16:59 +0200246 else
Bartlomiej Zolnierkiewicz8fed4362009-02-25 20:28:24 +0100247 get_device(&tape->dev);
Bartlomiej Zolnierkiewicz08da5912008-07-24 22:53:15 +0200248 }
Borislav Petkov9d01e4c2009-06-08 22:03:03 +0200249
Arjan van de Vencf8b8972006-03-23 03:00:45 -0800250 mutex_unlock(&idetape_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 return tape;
252}
253
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254static void ide_tape_put(struct ide_tape_obj *tape)
255{
Bartlomiej Zolnierkiewiczd3e33ff2008-08-05 18:16:59 +0200256 ide_drive_t *drive = tape->drive;
257
Arjan van de Vencf8b8972006-03-23 03:00:45 -0800258 mutex_lock(&idetape_ref_mutex);
Bartlomiej Zolnierkiewicz8fed4362009-02-25 20:28:24 +0100259 put_device(&tape->dev);
Bartlomiej Zolnierkiewiczd3e33ff2008-08-05 18:16:59 +0200260 ide_device_put(drive);
Arjan van de Vencf8b8972006-03-23 03:00:45 -0800261 mutex_unlock(&idetape_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262}
263
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264/*
Borislav Petkov1b5db432008-02-02 19:56:48 +0100265 * called on each failed packet command retry to analyze the request sense. We
266 * currently do not utilize this information.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 */
Borislav Petkovae3a8382009-05-02 10:58:17 +0200268static void idetape_analyze_error(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269{
270 idetape_tape_t *tape = drive->driver_data;
Bartlomiej Zolnierkiewicz5e2040fd2009-03-27 12:46:34 +0100271 struct ide_atapi_pc *pc = drive->failed_pc;
Borislav Petkovdfb7e622009-05-01 20:35:21 +0200272 struct request *rq = drive->hwif->rq;
Borislav Petkovae3a8382009-05-02 10:58:17 +0200273 u8 *sense = bio_data(rq->bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274
Borislav Petkov1b5db432008-02-02 19:56:48 +0100275 tape->sense_key = sense[2] & 0xF;
276 tape->asc = sense[12];
277 tape->ascq = sense[13];
Borislav Petkov8004a8c2008-02-06 02:57:51 +0100278
Borislav Petkove972d702009-07-17 23:55:16 +0000279 ide_debug_log(IDE_DBG_FUNC,
280 "cmd: 0x%x, sense key = %x, asc = %x, ascq = %x",
281 rq->cmd[0], tape->sense_key, tape->asc, tape->ascq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282
Borislav Petkov077e6db2009-05-01 21:21:02 +0200283 /* correct remaining bytes to transfer */
Tejun Heo21d9c5d2009-04-19 08:46:02 +0900284 if (pc->flags & PC_FLAG_DMA_ERROR)
Borislav Petkov077e6db2009-05-01 21:21:02 +0200285 rq->resid_len = tape->blk_size * get_unaligned_be32(&sense[3]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286
287 /*
288 * If error was the result of a zero-length read or write command,
289 * with sense key=5, asc=0x22, ascq=0, let it slide. Some drives
290 * (i.e. Seagate STT3401A Travan) don't support 0-length read/writes.
291 */
Borislav Petkov90699ce2008-02-02 19:56:50 +0100292 if ((pc->c[0] == READ_6 || pc->c[0] == WRITE_6)
Borislav Petkov1b5db432008-02-02 19:56:48 +0100293 /* length == 0 */
294 && pc->c[4] == 0 && pc->c[3] == 0 && pc->c[2] == 0) {
295 if (tape->sense_key == 5) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 /* don't report an error, everything's ok */
297 pc->error = 0;
298 /* don't retry read/write */
Borislav Petkov346331f2008-04-18 00:46:26 +0200299 pc->flags |= PC_FLAG_ABORT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 }
301 }
Borislav Petkov90699ce2008-02-02 19:56:50 +0100302 if (pc->c[0] == READ_6 && (sense[2] & 0x80)) {
Bartlomiej Zolnierkiewiczc152cc12009-03-27 12:46:34 +0100303 pc->error = IDE_DRV_ERROR_FILEMARK;
Borislav Petkov346331f2008-04-18 00:46:26 +0200304 pc->flags |= PC_FLAG_ABORT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 }
Borislav Petkov90699ce2008-02-02 19:56:50 +0100306 if (pc->c[0] == WRITE_6) {
Borislav Petkov1b5db432008-02-02 19:56:48 +0100307 if ((sense[2] & 0x40) || (tape->sense_key == 0xd
308 && tape->asc == 0x0 && tape->ascq == 0x2)) {
Bartlomiej Zolnierkiewiczc152cc12009-03-27 12:46:34 +0100309 pc->error = IDE_DRV_ERROR_EOD;
Borislav Petkov346331f2008-04-18 00:46:26 +0200310 pc->flags |= PC_FLAG_ABORT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 }
312 }
Borislav Petkov90699ce2008-02-02 19:56:50 +0100313 if (pc->c[0] == READ_6 || pc->c[0] == WRITE_6) {
Borislav Petkov1b5db432008-02-02 19:56:48 +0100314 if (tape->sense_key == 8) {
Bartlomiej Zolnierkiewiczc152cc12009-03-27 12:46:34 +0100315 pc->error = IDE_DRV_ERROR_EOD;
Borislav Petkov346331f2008-04-18 00:46:26 +0200316 pc->flags |= PC_FLAG_ABORT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 }
Borislav Petkov346331f2008-04-18 00:46:26 +0200318 if (!(pc->flags & PC_FLAG_ABORT) &&
Borislav Petkov077e6db2009-05-01 21:21:02 +0200319 (blk_rq_bytes(rq) - rq->resid_len))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 pc->retries = IDETAPE_MAX_PC_RETRIES + 1;
321 }
322}
323
Bartlomiej Zolnierkiewiczb14c7212008-10-13 21:39:30 +0200324static void ide_tape_handle_dsc(ide_drive_t *);
325
Bartlomiej Zolnierkiewicz03a2faa2009-03-27 12:46:36 +0100326static int ide_tape_callback(ide_drive_t *drive, int dsc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327{
328 idetape_tape_t *tape = drive->driver_data;
Bartlomiej Zolnierkiewicz2b9efba2008-10-13 21:39:31 +0200329 struct ide_atapi_pc *pc = drive->pc;
Bartlomiej Zolnierkiewicz313afea2009-03-27 12:46:34 +0100330 struct request *rq = drive->hwif->rq;
Bartlomiej Zolnierkiewicz5985e6a2008-07-15 21:21:55 +0200331 int uptodate = pc->error ? 0 : 1;
Bartlomiej Zolnierkiewicz313afea2009-03-27 12:46:34 +0100332 int err = uptodate ? 0 : IDE_DRV_ERROR_GENERAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333
Borislav Petkove972d702009-07-17 23:55:16 +0000334 ide_debug_log(IDE_DBG_FUNC, "cmd: 0x%x, dsc: %d, err: %d", rq->cmd[0],
335 dsc, err);
Borislav Petkov8004a8c2008-02-06 02:57:51 +0100336
Bartlomiej Zolnierkiewiczb14c7212008-10-13 21:39:30 +0200337 if (dsc)
338 ide_tape_handle_dsc(drive);
339
Bartlomiej Zolnierkiewicz5e2040fd2009-03-27 12:46:34 +0100340 if (drive->failed_pc == pc)
341 drive->failed_pc = NULL;
Bartlomiej Zolnierkiewiczdd2e9a02008-07-15 21:22:01 +0200342
Bartlomiej Zolnierkiewicz5985e6a2008-07-15 21:21:55 +0200343 if (pc->c[0] == REQUEST_SENSE) {
344 if (uptodate)
Borislav Petkovae3a8382009-05-02 10:58:17 +0200345 idetape_analyze_error(drive);
Bartlomiej Zolnierkiewicz5985e6a2008-07-15 21:21:55 +0200346 else
347 printk(KERN_ERR "ide-tape: Error in REQUEST SENSE "
348 "itself - Aborting request!\n");
349 } else if (pc->c[0] == READ_6 || pc->c[0] == WRITE_6) {
Borislav Petkov077e6db2009-05-01 21:21:02 +0200350 unsigned int blocks =
351 (blk_rq_bytes(rq) - rq->resid_len) / tape->blk_size;
Bartlomiej Zolnierkiewicz5985e6a2008-07-15 21:21:55 +0200352
353 tape->avg_size += blocks * tape->blk_size;
354
355 if (time_after_eq(jiffies, tape->avg_time + HZ)) {
356 tape->avg_speed = tape->avg_size * HZ /
357 (jiffies - tape->avg_time) / 1024;
358 tape->avg_size = 0;
359 tape->avg_time = jiffies;
360 }
361
362 tape->first_frame += blocks;
Bartlomiej Zolnierkiewicz5985e6a2008-07-15 21:21:55 +0200363
Bartlomiej Zolnierkiewicz313afea2009-03-27 12:46:34 +0100364 if (pc->error) {
365 uptodate = 0;
366 err = pc->error;
367 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 }
Bartlomiej Zolnierkiewicz313afea2009-03-27 12:46:34 +0100369 rq->errors = err;
370
Bartlomiej Zolnierkiewicz03a2faa2009-03-27 12:46:36 +0100371 return uptodate;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372}
373
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100375 * Postpone the current request so that ide.c will be able to service requests
Bartlomiej Zolnierkiewiczb65fac32009-01-06 17:20:50 +0100376 * from another device on the same port while we are polling for DSC.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 */
Borislav Petkov6f3848a2009-07-21 23:08:23 -0700378static void ide_tape_stall_queue(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379{
380 idetape_tape_t *tape = drive->driver_data;
381
Borislav Petkove972d702009-07-17 23:55:16 +0000382 ide_debug_log(IDE_DBG_FUNC, "cmd: 0x%x, dsc_poll_freq: %lu",
Borislav Petkov6f3848a2009-07-21 23:08:23 -0700383 drive->hwif->rq->cmd[0], tape->dsc_poll_freq);
Borislav Petkov8004a8c2008-02-06 02:57:51 +0100384
Borislav Petkov6f3848a2009-07-21 23:08:23 -0700385 tape->postponed_rq = true;
Bartlomiej Zolnierkiewiczb65fac32009-01-06 17:20:50 +0100386
Borislav Petkov54bb2072008-02-06 02:57:52 +0100387 ide_stall_queue(drive, tape->dsc_poll_freq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388}
389
Bartlomiej Zolnierkiewicz74e63e72008-07-15 21:22:01 +0200390static void ide_tape_handle_dsc(ide_drive_t *drive)
391{
392 idetape_tape_t *tape = drive->driver_data;
393
394 /* Media access command */
395 tape->dsc_polling_start = jiffies;
396 tape->dsc_poll_freq = IDETAPE_DSC_MA_FAST;
397 tape->dsc_timeout = jiffies + IDETAPE_DSC_MA_TIMEOUT;
398 /* Allow ide.c to handle other requests */
Borislav Petkov6f3848a2009-07-21 23:08:23 -0700399 ide_tape_stall_queue(drive);
Bartlomiej Zolnierkiewicz74e63e72008-07-15 21:22:01 +0200400}
401
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100403 * Packet Command Interface
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 *
Bartlomiej Zolnierkiewicz2b9efba2008-10-13 21:39:31 +0200405 * The current Packet Command is available in drive->pc, and will not change
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100406 * until we finish handling it. Each packet command is associated with a
407 * callback function that will be called when the command is finished.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100409 * The handling will be done in three stages:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 *
Bartlomiej Zolnierkiewiczb788ee92009-03-27 12:46:46 +0100411 * 1. ide_tape_issue_pc will send the packet command to the drive, and will set
Bartlomiej Zolnierkiewiczaa5d2de2008-10-13 21:39:32 +0200412 * the interrupt handler to ide_pc_intr.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 *
Bartlomiej Zolnierkiewiczaa5d2de2008-10-13 21:39:32 +0200414 * 2. On each interrupt, ide_pc_intr will be called. This step will be
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100415 * repeated until the device signals us that no more interrupts will be issued.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100417 * 3. ATAPI Tape media access commands have immediate status with a delayed
418 * process. In case of a successful initiation of a media access packet command,
419 * the DSC bit will be set when the actual execution of the command is finished.
420 * Since the tape drive will not issue an interrupt, we have to poll for this
421 * event. In this case, we define the request as "low priority request" by
422 * setting rq_status to IDETAPE_RQ_POSTPONED, set a timer to poll for DSC and
423 * exit the driver.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100425 * ide.c will then give higher priority to requests which originate from the
426 * other device, until will change rq_status to RQ_ACTIVE.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100428 * 4. When the packet command is finished, it will be checked for errors.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100430 * 5. In case an error was found, we queue a request sense packet command in
431 * front of the request queue and retry the operation up to
432 * IDETAPE_MAX_PC_RETRIES times.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100434 * 6. In case no error was found, or we decided to give up and not to retry
435 * again, the callback function will be called and then we will handle the next
436 * request.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438
Bartlomiej Zolnierkiewiczb788ee92009-03-27 12:46:46 +0100439static ide_startstop_t ide_tape_issue_pc(ide_drive_t *drive,
440 struct ide_cmd *cmd,
441 struct ide_atapi_pc *pc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 idetape_tape_t *tape = drive->driver_data;
Borislav Petkovdfb7e622009-05-01 20:35:21 +0200444 struct request *rq = drive->hwif->rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445
Bartlomiej Zolnierkiewicz5e2040fd2009-03-27 12:46:34 +0100446 if (drive->failed_pc == NULL && pc->c[0] != REQUEST_SENSE)
447 drive->failed_pc = pc;
Bartlomiej Zolnierkiewicz2b9efba2008-10-13 21:39:31 +0200448
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 /* Set the current packet command */
Bartlomiej Zolnierkiewicz2b9efba2008-10-13 21:39:31 +0200450 drive->pc = pc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451
452 if (pc->retries > IDETAPE_MAX_PC_RETRIES ||
Borislav Petkov346331f2008-04-18 00:46:26 +0200453 (pc->flags & PC_FLAG_ABORT)) {
Tejun Heob3071d12009-04-19 08:46:02 +0900454
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 /*
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100456 * We will "abort" retrying a packet command in case legitimate
457 * error code was received (crossing a filemark, or end of the
458 * media, for example).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 */
Borislav Petkov346331f2008-04-18 00:46:26 +0200460 if (!(pc->flags & PC_FLAG_ABORT)) {
Borislav Petkov90699ce2008-02-02 19:56:50 +0100461 if (!(pc->c[0] == TEST_UNIT_READY &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 tape->sense_key == 2 && tape->asc == 4 &&
463 (tape->ascq == 1 || tape->ascq == 8))) {
464 printk(KERN_ERR "ide-tape: %s: I/O error, "
465 "pc = %2x, key = %2x, "
466 "asc = %2x, ascq = %2x\n",
467 tape->name, pc->c[0],
468 tape->sense_key, tape->asc,
469 tape->ascq);
470 }
471 /* Giving up */
Bartlomiej Zolnierkiewiczc152cc12009-03-27 12:46:34 +0100472 pc->error = IDE_DRV_ERROR_GENERAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 }
Tejun Heob3071d12009-04-19 08:46:02 +0900474
Bartlomiej Zolnierkiewicz5e2040fd2009-03-27 12:46:34 +0100475 drive->failed_pc = NULL;
Bartlomiej Zolnierkiewiczb14c7212008-10-13 21:39:30 +0200476 drive->pc_callback(drive, 0);
Borislav Petkovdfb7e622009-05-01 20:35:21 +0200477 ide_complete_rq(drive, -EIO, blk_rq_bytes(rq));
Bartlomiej Zolnierkiewicz92f5daf2008-07-15 21:21:55 +0200478 return ide_stopped;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 }
Borislav Petkove972d702009-07-17 23:55:16 +0000480 ide_debug_log(IDE_DBG_SENSE, "retry #%d, cmd: 0x%02x", pc->retries,
481 pc->c[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482
483 pc->retries++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484
Bartlomiej Zolnierkiewiczb788ee92009-03-27 12:46:46 +0100485 return ide_issue_pc(drive, cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486}
487
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100488/* A mode sense command is used to "sense" tape parameters. */
Borislav Petkovd236d742008-04-18 00:46:27 +0200489static void idetape_create_mode_sense_cmd(struct ide_atapi_pc *pc, u8 page_code)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490{
Bartlomiej Zolnierkiewicz7bf74202008-10-10 22:39:37 +0200491 ide_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +0100492 pc->c[0] = MODE_SENSE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 if (page_code != IDETAPE_BLOCK_DESCRIPTOR)
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100494 /* DBD = 1 - Don't return block descriptors */
495 pc->c[1] = 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 pc->c[2] = page_code;
497 /*
498 * Changed pc->c[3] to 0 (255 will at best return unused info).
499 *
500 * For SCSI this byte is defined as subpage instead of high byte
501 * of length and some IDE drives seem to interpret it this way
502 * and return an error when 255 is used.
503 */
504 pc->c[3] = 0;
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100505 /* We will just discard data in that case */
506 pc->c[4] = 255;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 if (page_code == IDETAPE_BLOCK_DESCRIPTOR)
Borislav Petkovd236d742008-04-18 00:46:27 +0200508 pc->req_xfer = 12;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 else if (page_code == IDETAPE_CAPABILITIES_PAGE)
Borislav Petkovd236d742008-04-18 00:46:27 +0200510 pc->req_xfer = 24;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 else
Borislav Petkovd236d742008-04-18 00:46:27 +0200512 pc->req_xfer = 50;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513}
514
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100515static ide_startstop_t idetape_media_access_finished(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516{
Bartlomiej Zolnierkiewiczb73c7ee2008-07-23 19:55:52 +0200517 ide_hwif_t *hwif = drive->hwif;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 idetape_tape_t *tape = drive->driver_data;
Bartlomiej Zolnierkiewicz2b9efba2008-10-13 21:39:31 +0200519 struct ide_atapi_pc *pc = drive->pc;
Bartlomiej Zolnierkiewicz22c525b2008-01-25 22:17:11 +0100520 u8 stat;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521
Bartlomiej Zolnierkiewicz374e0422008-07-23 19:55:56 +0200522 stat = hwif->tp_ops->read_status(hwif);
Bartlomiej Zolnierkiewiczc47137a2008-02-06 02:57:51 +0100523
Bartlomiej Zolnierkiewicz3a7d2482008-10-10 22:39:21 +0200524 if (stat & ATA_DSC) {
525 if (stat & ATA_ERR) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 /* Error detected */
Borislav Petkov90699ce2008-02-02 19:56:50 +0100527 if (pc->c[0] != TEST_UNIT_READY)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 printk(KERN_ERR "ide-tape: %s: I/O error, ",
529 tape->name);
530 /* Retry operation */
Borislav Petkov6b544fc2009-04-19 07:00:42 +0900531 ide_retry_pc(drive);
Bartlomiej Zolnierkiewicz258ec412008-07-15 21:21:55 +0200532 return ide_stopped;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 }
534 pc->error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 } else {
Bartlomiej Zolnierkiewiczc152cc12009-03-27 12:46:34 +0100536 pc->error = IDE_DRV_ERROR_GENERAL;
Bartlomiej Zolnierkiewicz5e2040fd2009-03-27 12:46:34 +0100537 drive->failed_pc = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 }
Bartlomiej Zolnierkiewiczb14c7212008-10-13 21:39:30 +0200539 drive->pc_callback(drive, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 return ide_stopped;
541}
542
Borislav Petkovcd2abbf2008-07-15 21:22:03 +0200543static void ide_tape_create_rw_cmd(idetape_tape_t *tape,
Borislav Petkov0014c752008-07-23 19:56:00 +0200544 struct ide_atapi_pc *pc, struct request *rq,
545 u8 opcode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546{
Borislav Petkov10c0b342009-05-01 20:29:53 +0200547 unsigned int length = blk_rq_sectors(rq) / (tape->blk_size >> 9);
Borislav Petkov0014c752008-07-23 19:56:00 +0200548
Bartlomiej Zolnierkiewicz7bf74202008-10-10 22:39:37 +0200549 ide_init_pc(pc);
Borislav Petkov860ff5e2008-02-02 19:56:50 +0100550 put_unaligned(cpu_to_be32(length), (unsigned int *) &pc->c[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 pc->c[1] = 1;
Borislav Petkov19f52a72009-05-04 09:53:03 +0200552
553 if (blk_rq_bytes(rq) == tape->buffer_size)
Bartlomiej Zolnierkiewicz5e331092008-07-15 21:21:56 +0200554 pc->flags |= PC_FLAG_DMA_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555
Tejun Heo963da552009-04-19 08:46:02 +0900556 if (opcode == READ_6)
Borislav Petkovcd2abbf2008-07-15 21:22:03 +0200557 pc->c[0] = READ_6;
Tejun Heo963da552009-04-19 08:46:02 +0900558 else if (opcode == WRITE_6) {
Borislav Petkovcd2abbf2008-07-15 21:22:03 +0200559 pc->c[0] = WRITE_6;
560 pc->flags |= PC_FLAG_WRITING;
Borislav Petkovcd2abbf2008-07-15 21:22:03 +0200561 }
Borislav Petkov0014c752008-07-23 19:56:00 +0200562
563 memcpy(rq->cmd, pc->c, 12);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564}
565
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566static ide_startstop_t idetape_do_request(ide_drive_t *drive,
567 struct request *rq, sector_t block)
568{
Bartlomiej Zolnierkiewiczb73c7ee2008-07-23 19:55:52 +0200569 ide_hwif_t *hwif = drive->hwif;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 idetape_tape_t *tape = drive->driver_data;
Borislav Petkovd236d742008-04-18 00:46:27 +0200571 struct ide_atapi_pc *pc = NULL;
Bartlomiej Zolnierkiewiczb788ee92009-03-27 12:46:46 +0100572 struct ide_cmd cmd;
Bartlomiej Zolnierkiewicz22c525b2008-01-25 22:17:11 +0100573 u8 stat;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574
Borislav Petkove972d702009-07-17 23:55:16 +0000575 ide_debug_log(IDE_DBG_RQ, "cmd: 0x%x, sector: %llu, nr_sectors: %u",
576 rq->cmd[0], (unsigned long long)blk_rq_pos(rq),
577 blk_rq_sectors(rq));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578
Christoph Hellwig33659eb2010-08-07 18:17:56 +0200579 BUG_ON(!(rq->cmd_type == REQ_TYPE_SPECIAL ||
580 rq->cmd_type == REQ_TYPE_SENSE));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100582 /* Retry a failed packet command */
Bartlomiej Zolnierkiewicz5e2040fd2009-03-27 12:46:34 +0100583 if (drive->failed_pc && drive->pc->c[0] == REQUEST_SENSE) {
584 pc = drive->failed_pc;
Bartlomiej Zolnierkiewicz28c72142008-07-15 21:21:59 +0200585 goto out;
586 }
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100587
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588 /*
589 * If the tape is still busy, postpone our request and service
590 * the other device meanwhile.
591 */
Bartlomiej Zolnierkiewicz374e0422008-07-23 19:55:56 +0200592 stat = hwif->tp_ops->read_status(hwif);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +0200594 if ((drive->dev_flags & IDE_DFLAG_DSC_OVERLAP) == 0 &&
595 (rq->cmd[13] & REQ_IDETAPE_PC2) == 0)
Borislav Petkov626542c2009-06-07 15:37:05 +0200596 drive->atapi_flags |= IDE_AFLAG_IGNORE_DSC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +0200598 if (drive->dev_flags & IDE_DFLAG_POST_RESET) {
Borislav Petkov626542c2009-06-07 15:37:05 +0200599 drive->atapi_flags |= IDE_AFLAG_IGNORE_DSC;
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +0200600 drive->dev_flags &= ~IDE_DFLAG_POST_RESET;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 }
602
Borislav Petkov626542c2009-06-07 15:37:05 +0200603 if (!(drive->atapi_flags & IDE_AFLAG_IGNORE_DSC) &&
604 !(stat & ATA_DSC)) {
Borislav Petkov6f3848a2009-07-21 23:08:23 -0700605 if (!tape->postponed_rq) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 tape->dsc_polling_start = jiffies;
Borislav Petkov54bb2072008-02-06 02:57:52 +0100607 tape->dsc_poll_freq = tape->best_dsc_rw_freq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608 tape->dsc_timeout = jiffies + IDETAPE_DSC_RW_TIMEOUT;
609 } else if (time_after(jiffies, tape->dsc_timeout)) {
610 printk(KERN_ERR "ide-tape: %s: DSC timeout\n",
611 tape->name);
Borislav Petkov83dd5732008-07-23 19:56:00 +0200612 if (rq->cmd[13] & REQ_IDETAPE_PC2) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 idetape_media_access_finished(drive);
614 return ide_stopped;
615 } else {
616 return ide_do_reset(drive);
617 }
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100618 } else if (time_after(jiffies,
619 tape->dsc_polling_start +
620 IDETAPE_DSC_MA_THRESHOLD))
Borislav Petkov54bb2072008-02-06 02:57:52 +0100621 tape->dsc_poll_freq = IDETAPE_DSC_MA_SLOW;
Borislav Petkov6f3848a2009-07-21 23:08:23 -0700622 ide_tape_stall_queue(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 return ide_stopped;
Borislav Petkov6f3848a2009-07-21 23:08:23 -0700624 } else {
Borislav Petkov626542c2009-06-07 15:37:05 +0200625 drive->atapi_flags &= ~IDE_AFLAG_IGNORE_DSC;
Borislav Petkov6f3848a2009-07-21 23:08:23 -0700626 tape->postponed_rq = false;
627 }
Borislav Petkov626542c2009-06-07 15:37:05 +0200628
Borislav Petkov83dd5732008-07-23 19:56:00 +0200629 if (rq->cmd[13] & REQ_IDETAPE_READ) {
Bartlomiej Zolnierkiewicz2e8a6f82008-10-10 22:39:36 +0200630 pc = &tape->queued_pc;
Borislav Petkov0014c752008-07-23 19:56:00 +0200631 ide_tape_create_rw_cmd(tape, pc, rq, READ_6);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 goto out;
633 }
Borislav Petkov83dd5732008-07-23 19:56:00 +0200634 if (rq->cmd[13] & REQ_IDETAPE_WRITE) {
Bartlomiej Zolnierkiewicz2e8a6f82008-10-10 22:39:36 +0200635 pc = &tape->queued_pc;
Borislav Petkov0014c752008-07-23 19:56:00 +0200636 ide_tape_create_rw_cmd(tape, pc, rq, WRITE_6);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637 goto out;
638 }
Borislav Petkov83dd5732008-07-23 19:56:00 +0200639 if (rq->cmd[13] & REQ_IDETAPE_PC1) {
Tejun Heoc267cc12009-04-19 07:00:42 +0900640 pc = (struct ide_atapi_pc *)rq->special;
Borislav Petkov83dd5732008-07-23 19:56:00 +0200641 rq->cmd[13] &= ~(REQ_IDETAPE_PC1);
642 rq->cmd[13] |= REQ_IDETAPE_PC2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 goto out;
644 }
Borislav Petkov83dd5732008-07-23 19:56:00 +0200645 if (rq->cmd[13] & REQ_IDETAPE_PC2) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 idetape_media_access_finished(drive);
647 return ide_stopped;
648 }
649 BUG();
Bartlomiej Zolnierkiewicz28c72142008-07-15 21:21:59 +0200650
Borislav Petkovf2e3ab52008-07-23 19:56:01 +0200651out:
Borislav Petkov6b544fc2009-04-19 07:00:42 +0900652 /* prepare sense request for this command */
653 ide_prep_sense(drive, rq);
654
Bartlomiej Zolnierkiewiczb788ee92009-03-27 12:46:46 +0100655 memset(&cmd, 0, sizeof(cmd));
656
657 if (rq_data_dir(rq))
658 cmd.tf_flags |= IDE_TFLAG_WRITE;
659
660 cmd.rq = rq;
661
Borislav Petkovdfb7e622009-05-01 20:35:21 +0200662 ide_init_sg_cmd(&cmd, blk_rq_bytes(rq));
Tejun Heo5c4be572009-04-19 07:00:42 +0900663 ide_map_sg(drive, &cmd);
664
Bartlomiej Zolnierkiewiczb788ee92009-03-27 12:46:46 +0100665 return ide_tape_issue_pc(drive, &cmd, pc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666}
667
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100669 * Write a filemark if write_filemark=1. Flush the device buffers without
670 * writing a filemark otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671 */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100672static void idetape_create_write_filemark_cmd(ide_drive_t *drive,
Borislav Petkovd236d742008-04-18 00:46:27 +0200673 struct ide_atapi_pc *pc, int write_filemark)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674{
Bartlomiej Zolnierkiewicz7bf74202008-10-10 22:39:37 +0200675 ide_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +0100676 pc->c[0] = WRITE_FILEMARKS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 pc->c[4] = write_filemark;
Borislav Petkov346331f2008-04-18 00:46:26 +0200678 pc->flags |= PC_FLAG_WAIT_FOR_DSC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679}
680
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681static int idetape_wait_ready(ide_drive_t *drive, unsigned long timeout)
682{
683 idetape_tape_t *tape = drive->driver_data;
Bartlomiej Zolnierkiewicz2ac07d92008-10-10 22:39:38 +0200684 struct gendisk *disk = tape->disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 int load_attempted = 0;
686
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100687 /* Wait for the tape to become ready */
Borislav Petkov49d80782009-06-07 15:37:06 +0200688 set_bit(ilog2(IDE_AFLAG_MEDIUM_PRESENT), &drive->atapi_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689 timeout += jiffies;
690 while (time_before(jiffies, timeout)) {
Bartlomiej Zolnierkiewiczde699ad2008-10-10 22:39:39 +0200691 if (ide_do_test_unit_ready(drive, disk) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692 return 0;
693 if ((tape->sense_key == 2 && tape->asc == 4 && tape->ascq == 2)
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100694 || (tape->asc == 0x3A)) {
695 /* no media */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 if (load_attempted)
697 return -ENOMEDIUM;
Bartlomiej Zolnierkiewicz0c8a6c72008-10-10 22:39:39 +0200698 ide_do_start_stop(drive, disk, IDETAPE_LU_LOAD_MASK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699 load_attempted = 1;
700 /* not about to be ready */
701 } else if (!(tape->sense_key == 2 && tape->asc == 4 &&
702 (tape->ascq == 1 || tape->ascq == 8)))
703 return -EIO;
Nishanth Aravamudan80ce45f2005-09-10 00:27:08 -0700704 msleep(100);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705 }
706 return -EIO;
707}
708
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100709static int idetape_flush_tape_buffers(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710{
Bartlomiej Zolnierkiewicz2ac07d92008-10-10 22:39:38 +0200711 struct ide_tape_obj *tape = drive->driver_data;
Borislav Petkovd236d742008-04-18 00:46:27 +0200712 struct ide_atapi_pc pc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713 int rc;
714
715 idetape_create_write_filemark_cmd(drive, &pc, 0);
Borislav Petkovb13345f2009-05-02 10:26:12 +0200716 rc = ide_queue_pc_tail(drive, tape->disk, &pc, NULL, 0);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100717 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718 return rc;
719 idetape_wait_ready(drive, 60 * 5 * HZ);
720 return 0;
721}
722
Borislav Petkov55ce3a12009-05-04 09:38:15 +0200723static int ide_tape_read_position(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724{
725 idetape_tape_t *tape = drive->driver_data;
Borislav Petkovd236d742008-04-18 00:46:27 +0200726 struct ide_atapi_pc pc;
Borislav Petkov55ce3a12009-05-04 09:38:15 +0200727 u8 buf[20];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728
Borislav Petkove972d702009-07-17 23:55:16 +0000729 ide_debug_log(IDE_DBG_FUNC, "enter");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730
Borislav Petkov55ce3a12009-05-04 09:38:15 +0200731 /* prep cmd */
732 ide_init_pc(&pc);
733 pc.c[0] = READ_POSITION;
734 pc.req_xfer = 20;
735
736 if (ide_queue_pc_tail(drive, tape->disk, &pc, buf, pc.req_xfer))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737 return -1;
Borislav Petkov55ce3a12009-05-04 09:38:15 +0200738
739 if (!pc.error) {
Borislav Petkove972d702009-07-17 23:55:16 +0000740 ide_debug_log(IDE_DBG_FUNC, "BOP - %s",
Borislav Petkov55ce3a12009-05-04 09:38:15 +0200741 (buf[0] & 0x80) ? "Yes" : "No");
Borislav Petkove972d702009-07-17 23:55:16 +0000742 ide_debug_log(IDE_DBG_FUNC, "EOP - %s",
Borislav Petkov55ce3a12009-05-04 09:38:15 +0200743 (buf[0] & 0x40) ? "Yes" : "No");
744
745 if (buf[0] & 0x4) {
746 printk(KERN_INFO "ide-tape: Block location is unknown"
747 "to the tape\n");
Bartlomiej Zolnierkiewicz8dcce402009-06-13 12:00:54 +0200748 clear_bit(ilog2(IDE_AFLAG_ADDRESS_VALID),
749 &drive->atapi_flags);
Borislav Petkov55ce3a12009-05-04 09:38:15 +0200750 return -1;
751 } else {
Borislav Petkove972d702009-07-17 23:55:16 +0000752 ide_debug_log(IDE_DBG_FUNC, "Block Location: %u",
753 be32_to_cpup((__be32 *)&buf[4]));
Borislav Petkov55ce3a12009-05-04 09:38:15 +0200754
755 tape->partition = buf[1];
756 tape->first_frame = be32_to_cpup((__be32 *)&buf[4]);
Bartlomiej Zolnierkiewicz8dcce402009-06-13 12:00:54 +0200757 set_bit(ilog2(IDE_AFLAG_ADDRESS_VALID),
758 &drive->atapi_flags);
Borislav Petkov55ce3a12009-05-04 09:38:15 +0200759 }
760 }
761
762 return tape->first_frame;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763}
764
Borislav Petkovd236d742008-04-18 00:46:27 +0200765static void idetape_create_locate_cmd(ide_drive_t *drive,
766 struct ide_atapi_pc *pc,
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100767 unsigned int block, u8 partition, int skip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768{
Bartlomiej Zolnierkiewicz7bf74202008-10-10 22:39:37 +0200769 ide_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +0100770 pc->c[0] = POSITION_TO_ELEMENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771 pc->c[1] = 2;
Borislav Petkov860ff5e2008-02-02 19:56:50 +0100772 put_unaligned(cpu_to_be32(block), (unsigned int *) &pc->c[3]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773 pc->c[8] = partition;
Borislav Petkov346331f2008-04-18 00:46:26 +0200774 pc->flags |= PC_FLAG_WAIT_FOR_DSC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775}
776
Borislav Petkovec0fdb02008-04-27 15:38:34 +0200777static void __ide_tape_discard_merge_buffer(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778{
779 idetape_tape_t *tape = drive->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780
Borislav Petkov54abf372008-02-06 02:57:52 +0100781 if (tape->chrdev_dir != IDETAPE_DIR_READ)
Borislav Petkov97986302008-04-27 15:38:34 +0200782 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783
Borislav Petkov49d80782009-06-07 15:37:06 +0200784 clear_bit(ilog2(IDE_AFLAG_FILEMARK), &drive->atapi_flags);
Tejun Heo963da552009-04-19 08:46:02 +0900785 tape->valid = 0;
786 if (tape->buf != NULL) {
787 kfree(tape->buf);
788 tape->buf = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789 }
790
Borislav Petkov54abf372008-02-06 02:57:52 +0100791 tape->chrdev_dir = IDETAPE_DIR_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792}
793
794/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100795 * Position the tape to the requested block using the LOCATE packet command.
796 * A READ POSITION command is then issued to check where we are positioned. Like
797 * all higher level operations, we queue the commands at the tail of the request
798 * queue and wait for their completion.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799 */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100800static int idetape_position_tape(ide_drive_t *drive, unsigned int block,
801 u8 partition, int skip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802{
803 idetape_tape_t *tape = drive->driver_data;
Bartlomiej Zolnierkiewicz2ac07d92008-10-10 22:39:38 +0200804 struct gendisk *disk = tape->disk;
Borislav Petkov55ce3a12009-05-04 09:38:15 +0200805 int ret;
Borislav Petkovd236d742008-04-18 00:46:27 +0200806 struct ide_atapi_pc pc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807
Borislav Petkov54abf372008-02-06 02:57:52 +0100808 if (tape->chrdev_dir == IDETAPE_DIR_READ)
Borislav Petkovec0fdb02008-04-27 15:38:34 +0200809 __ide_tape_discard_merge_buffer(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810 idetape_wait_ready(drive, 60 * 5 * HZ);
811 idetape_create_locate_cmd(drive, &pc, block, partition, skip);
Borislav Petkov55ce3a12009-05-04 09:38:15 +0200812 ret = ide_queue_pc_tail(drive, disk, &pc, NULL, 0);
813 if (ret)
814 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815
Borislav Petkov55ce3a12009-05-04 09:38:15 +0200816 ret = ide_tape_read_position(drive);
817 if (ret < 0)
818 return ret;
819 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820}
821
Borislav Petkovec0fdb02008-04-27 15:38:34 +0200822static void ide_tape_discard_merge_buffer(ide_drive_t *drive,
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100823 int restore_position)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824{
825 idetape_tape_t *tape = drive->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826 int seek, position;
827
Borislav Petkovec0fdb02008-04-27 15:38:34 +0200828 __ide_tape_discard_merge_buffer(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829 if (restore_position) {
Borislav Petkov55ce3a12009-05-04 09:38:15 +0200830 position = ide_tape_read_position(drive);
Borislav Petkov97986302008-04-27 15:38:34 +0200831 seek = position > 0 ? position : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832 if (idetape_position_tape(drive, seek, 0, 0)) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100833 printk(KERN_INFO "ide-tape: %s: position_tape failed in"
Borislav Petkovec0fdb02008-04-27 15:38:34 +0200834 " %s\n", tape->name, __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835 return;
836 }
837 }
838}
839
840/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100841 * Generate a read/write request for the block device interface and wait for it
842 * to be serviced.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843 */
Tejun Heo6bb11dd2009-04-19 08:46:03 +0900844static int idetape_queue_rw_tail(ide_drive_t *drive, int cmd, int size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845{
846 idetape_tape_t *tape = drive->driver_data;
FUJITA Tomonori64ea1b42008-07-15 21:21:43 +0200847 struct request *rq;
Tejun Heo21d9c5d2009-04-19 08:46:02 +0900848 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849
Borislav Petkove972d702009-07-17 23:55:16 +0000850 ide_debug_log(IDE_DBG_FUNC, "cmd: 0x%x, size: %d", cmd, size);
851
Tejun Heo21d9c5d2009-04-19 08:46:02 +0900852 BUG_ON(cmd != REQ_IDETAPE_READ && cmd != REQ_IDETAPE_WRITE);
Tejun Heo6bb11dd2009-04-19 08:46:03 +0900853 BUG_ON(size < 0 || size % tape->blk_size);
Borislav Petkov8004a8c2008-02-06 02:57:51 +0100854
FUJITA Tomonori64ea1b42008-07-15 21:21:43 +0200855 rq = blk_get_request(drive->queue, READ, __GFP_WAIT);
856 rq->cmd_type = REQ_TYPE_SPECIAL;
Borislav Petkov83dd5732008-07-23 19:56:00 +0200857 rq->cmd[13] = cmd;
FUJITA Tomonori64ea1b42008-07-15 21:21:43 +0200858 rq->rq_disk = tape->disk;
Linus Torvaldsc9059592009-06-11 10:52:27 -0700859 rq->__sector = tape->first_frame;
Tejun Heo21d9c5d2009-04-19 08:46:02 +0900860
861 if (size) {
Tejun Heo963da552009-04-19 08:46:02 +0900862 ret = blk_rq_map_kern(drive->queue, rq, tape->buf, size,
Tejun Heo21d9c5d2009-04-19 08:46:02 +0900863 __GFP_WAIT);
864 if (ret)
865 goto out_put;
866 }
867
FUJITA Tomonori64ea1b42008-07-15 21:21:43 +0200868 blk_execute_rq(drive->queue, tape->disk, rq, 0);
869
Tejun Heo963da552009-04-19 08:46:02 +0900870 /* calculate the number of transferred bytes and update buffer state */
Tejun Heoc3a4d782009-05-07 22:24:37 +0900871 size -= rq->resid_len;
Tejun Heo963da552009-04-19 08:46:02 +0900872 tape->cur = tape->buf;
Tejun Heo21d9c5d2009-04-19 08:46:02 +0900873 if (cmd == REQ_IDETAPE_READ)
Tejun Heo963da552009-04-19 08:46:02 +0900874 tape->valid = size;
875 else
876 tape->valid = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877
Tejun Heo21d9c5d2009-04-19 08:46:02 +0900878 ret = size;
879 if (rq->errors == IDE_DRV_ERROR_GENERAL)
880 ret = -EIO;
Tejun Heo21d9c5d2009-04-19 08:46:02 +0900881out_put:
882 blk_put_request(rq);
FUJITA Tomonori64ea1b42008-07-15 21:21:43 +0200883 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884}
885
Borislav Petkovd236d742008-04-18 00:46:27 +0200886static void idetape_create_inquiry_cmd(struct ide_atapi_pc *pc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887{
Bartlomiej Zolnierkiewicz7bf74202008-10-10 22:39:37 +0200888 ide_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +0100889 pc->c[0] = INQUIRY;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100890 pc->c[4] = 254;
Borislav Petkovd236d742008-04-18 00:46:27 +0200891 pc->req_xfer = 254;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892}
893
Borislav Petkovd236d742008-04-18 00:46:27 +0200894static void idetape_create_rewind_cmd(ide_drive_t *drive,
895 struct ide_atapi_pc *pc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896{
Bartlomiej Zolnierkiewicz7bf74202008-10-10 22:39:37 +0200897 ide_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +0100898 pc->c[0] = REZERO_UNIT;
Borislav Petkov346331f2008-04-18 00:46:26 +0200899 pc->flags |= PC_FLAG_WAIT_FOR_DSC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900}
901
Borislav Petkovd236d742008-04-18 00:46:27 +0200902static void idetape_create_erase_cmd(struct ide_atapi_pc *pc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903{
Bartlomiej Zolnierkiewicz7bf74202008-10-10 22:39:37 +0200904 ide_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +0100905 pc->c[0] = ERASE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906 pc->c[1] = 1;
Borislav Petkov346331f2008-04-18 00:46:26 +0200907 pc->flags |= PC_FLAG_WAIT_FOR_DSC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908}
909
Borislav Petkovd236d742008-04-18 00:46:27 +0200910static void idetape_create_space_cmd(struct ide_atapi_pc *pc, int count, u8 cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911{
Bartlomiej Zolnierkiewicz7bf74202008-10-10 22:39:37 +0200912 ide_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +0100913 pc->c[0] = SPACE;
Borislav Petkov860ff5e2008-02-02 19:56:50 +0100914 put_unaligned(cpu_to_be32(count), (unsigned int *) &pc->c[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915 pc->c[1] = cmd;
Borislav Petkov346331f2008-04-18 00:46:26 +0200916 pc->flags |= PC_FLAG_WAIT_FOR_DSC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917}
918
Borislav Petkovd9df9372008-04-27 15:38:34 +0200919static void ide_tape_flush_merge_buffer(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920{
921 idetape_tape_t *tape = drive->driver_data;
Borislav Petkov55a5d292008-02-02 19:56:49 +0100922
Borislav Petkov54abf372008-02-06 02:57:52 +0100923 if (tape->chrdev_dir != IDETAPE_DIR_WRITE) {
Borislav Petkov077e3bd2008-04-27 15:38:34 +0200924 printk(KERN_ERR "ide-tape: bug: Trying to empty merge buffer"
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100925 " but we are not writing.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926 return;
927 }
Tejun Heo963da552009-04-19 08:46:02 +0900928 if (tape->buf) {
Tejun Heo6bb11dd2009-04-19 08:46:03 +0900929 size_t aligned = roundup(tape->valid, tape->blk_size);
930
931 memset(tape->cur, 0, aligned - tape->valid);
Tejun Heo07bd9682009-04-19 08:46:03 +0900932 idetape_queue_rw_tail(drive, REQ_IDETAPE_WRITE, aligned);
Tejun Heo963da552009-04-19 08:46:02 +0900933 kfree(tape->buf);
934 tape->buf = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935 }
Borislav Petkov54abf372008-02-06 02:57:52 +0100936 tape->chrdev_dir = IDETAPE_DIR_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937}
938
Tejun Heo88f1b942009-04-19 08:46:02 +0900939static int idetape_init_rw(ide_drive_t *drive, int dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940{
941 idetape_tape_t *tape = drive->driver_data;
Tejun Heo88f1b942009-04-19 08:46:02 +0900942 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943
Tejun Heo88f1b942009-04-19 08:46:02 +0900944 BUG_ON(dir != IDETAPE_DIR_READ && dir != IDETAPE_DIR_WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945
Tejun Heo88f1b942009-04-19 08:46:02 +0900946 if (tape->chrdev_dir == dir)
947 return 0;
948
949 if (tape->chrdev_dir == IDETAPE_DIR_READ)
950 ide_tape_discard_merge_buffer(drive, 1);
951 else if (tape->chrdev_dir == IDETAPE_DIR_WRITE) {
952 ide_tape_flush_merge_buffer(drive);
953 idetape_flush_tape_buffers(drive);
954 }
955
956 if (tape->buf || tape->valid) {
957 printk(KERN_ERR "ide-tape: valid should be 0 now\n");
958 tape->valid = 0;
959 }
960
961 tape->buf = kmalloc(tape->buffer_size, GFP_KERNEL);
962 if (!tape->buf)
963 return -ENOMEM;
964 tape->chrdev_dir = dir;
965 tape->cur = tape->buf;
966
967 /*
968 * Issue a 0 rw command to ensure that DSC handshake is
969 * switched from completion mode to buffer available mode. No
970 * point in issuing this if DSC overlap isn't supported, some
971 * drives (Seagate STT3401A) will return an error.
972 */
973 if (drive->dev_flags & IDE_DFLAG_DSC_OVERLAP) {
974 int cmd = dir == IDETAPE_DIR_READ ? REQ_IDETAPE_READ
975 : REQ_IDETAPE_WRITE;
976
977 rc = idetape_queue_rw_tail(drive, cmd, 0);
978 if (rc < 0) {
979 kfree(tape->buf);
980 tape->buf = NULL;
981 tape->chrdev_dir = IDETAPE_DIR_NONE;
982 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983 }
984 }
Borislav Petkov5e69bd92008-04-27 15:38:25 +0200985
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986 return 0;
987}
988
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100989static void idetape_pad_zeros(ide_drive_t *drive, int bcount)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990{
991 idetape_tape_t *tape = drive->driver_data;
Tejun Heo963da552009-04-19 08:46:02 +0900992
993 memset(tape->buf, 0, tape->buffer_size);
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100994
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995 while (bcount) {
Tejun Heo963da552009-04-19 08:46:02 +0900996 unsigned int count = min(tape->buffer_size, bcount);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997
Tejun Heo6bb11dd2009-04-19 08:46:03 +0900998 idetape_queue_rw_tail(drive, REQ_IDETAPE_WRITE, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999 bcount -= count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000 }
1001}
1002
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001004 * Rewinds the tape to the Beginning Of the current Partition (BOP). We
1005 * currently support only one partition.
1006 */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001007static int idetape_rewind_tape(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008{
Bartlomiej Zolnierkiewicz2ac07d92008-10-10 22:39:38 +02001009 struct ide_tape_obj *tape = drive->driver_data;
1010 struct gendisk *disk = tape->disk;
Borislav Petkovd236d742008-04-18 00:46:27 +02001011 struct ide_atapi_pc pc;
Borislav Petkov55ce3a12009-05-04 09:38:15 +02001012 int ret;
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001013
Borislav Petkove972d702009-07-17 23:55:16 +00001014 ide_debug_log(IDE_DBG_FUNC, "enter");
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001015
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016 idetape_create_rewind_cmd(drive, &pc);
Borislav Petkov55ce3a12009-05-04 09:38:15 +02001017 ret = ide_queue_pc_tail(drive, disk, &pc, NULL, 0);
1018 if (ret)
1019 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020
Borislav Petkov55ce3a12009-05-04 09:38:15 +02001021 ret = ide_tape_read_position(drive);
1022 if (ret < 0)
1023 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024 return 0;
1025}
1026
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001027/* mtio.h compatible commands should be issued to the chrdev interface. */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001028static int idetape_blkdev_ioctl(ide_drive_t *drive, unsigned int cmd,
1029 unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030{
1031 idetape_tape_t *tape = drive->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032 void __user *argp = (void __user *)arg;
1033
Borislav Petkovd59823f2008-02-02 19:56:51 +01001034 struct idetape_config {
1035 int dsc_rw_frequency;
1036 int dsc_media_access_frequency;
1037 int nr_stages;
1038 } config;
1039
Borislav Petkove972d702009-07-17 23:55:16 +00001040 ide_debug_log(IDE_DBG_FUNC, "cmd: 0x%04x", cmd);
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001041
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042 switch (cmd) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001043 case 0x0340:
1044 if (copy_from_user(&config, argp, sizeof(config)))
1045 return -EFAULT;
1046 tape->best_dsc_rw_freq = config.dsc_rw_frequency;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001047 break;
1048 case 0x0350:
Michael Buesch2fc21112009-07-19 09:15:19 +00001049 memset(&config, 0, sizeof(config));
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001050 config.dsc_rw_frequency = (int) tape->best_dsc_rw_freq;
Borislav Petkov83042b22008-04-27 15:38:27 +02001051 config.nr_stages = 1;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001052 if (copy_to_user(argp, &config, sizeof(config)))
1053 return -EFAULT;
1054 break;
1055 default:
1056 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057 }
1058 return 0;
1059}
1060
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001061static int idetape_space_over_filemarks(ide_drive_t *drive, short mt_op,
1062 int mt_count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063{
1064 idetape_tape_t *tape = drive->driver_data;
Bartlomiej Zolnierkiewicz2ac07d92008-10-10 22:39:38 +02001065 struct gendisk *disk = tape->disk;
Borislav Petkovd236d742008-04-18 00:46:27 +02001066 struct ide_atapi_pc pc;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001067 int retval, count = 0;
Borislav Petkovb6422012008-02-02 19:56:49 +01001068 int sprev = !!(tape->caps[4] & 0x20);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069
Borislav Petkove972d702009-07-17 23:55:16 +00001070
1071 ide_debug_log(IDE_DBG_FUNC, "mt_op: %d, mt_count: %d", mt_op, mt_count);
1072
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073 if (mt_count == 0)
1074 return 0;
1075 if (MTBSF == mt_op || MTBSFM == mt_op) {
Borislav Petkovb6422012008-02-02 19:56:49 +01001076 if (!sprev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077 return -EIO;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001078 mt_count = -mt_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079 }
1080
Borislav Petkov54abf372008-02-06 02:57:52 +01001081 if (tape->chrdev_dir == IDETAPE_DIR_READ) {
Tejun Heo963da552009-04-19 08:46:02 +09001082 tape->valid = 0;
Borislav Petkov49d80782009-06-07 15:37:06 +02001083 if (test_and_clear_bit(ilog2(IDE_AFLAG_FILEMARK),
1084 &drive->atapi_flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085 ++count;
Borislav Petkovec0fdb02008-04-27 15:38:34 +02001086 ide_tape_discard_merge_buffer(drive, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087 }
1088
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089 switch (mt_op) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001090 case MTFSF:
1091 case MTBSF:
1092 idetape_create_space_cmd(&pc, mt_count - count,
1093 IDETAPE_SPACE_OVER_FILEMARK);
Borislav Petkovb13345f2009-05-02 10:26:12 +02001094 return ide_queue_pc_tail(drive, disk, &pc, NULL, 0);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001095 case MTFSFM:
1096 case MTBSFM:
1097 if (!sprev)
1098 return -EIO;
1099 retval = idetape_space_over_filemarks(drive, MTFSF,
1100 mt_count - count);
1101 if (retval)
1102 return retval;
1103 count = (MTBSFM == mt_op ? 1 : -1);
1104 return idetape_space_over_filemarks(drive, MTFSF, count);
1105 default:
1106 printk(KERN_ERR "ide-tape: MTIO operation %d not supported\n",
1107 mt_op);
1108 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109 }
1110}
1111
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001113 * Our character device read / write functions.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001115 * The tape is optimized to maximize throughput when it is transferring an
1116 * integral number of the "continuous transfer limit", which is a parameter of
1117 * the specific tape (26kB on my particular tape, 32kB for Onstream).
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001119 * As of version 1.3 of the driver, the character device provides an abstract
1120 * continuous view of the media - any mix of block sizes (even 1 byte) on the
1121 * same backup/restore procedure is supported. The driver will internally
1122 * convert the requests to the recommended transfer unit, so that an unmatch
1123 * between the user's block size to the recommended size will only result in a
1124 * (slightly) increased driver overhead, but will no longer hit performance.
1125 * This is not applicable to Onstream.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126 */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001127static ssize_t idetape_chrdev_read(struct file *file, char __user *buf,
1128 size_t count, loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129{
Borislav Petkov5aeddf92008-10-13 21:39:34 +02001130 struct ide_tape_obj *tape = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131 ide_drive_t *drive = tape->drive;
Tejun Heo07bd9682009-04-19 08:46:03 +09001132 size_t done = 0;
Daniel Walkerdcd96372006-06-25 05:47:37 -07001133 ssize_t ret = 0;
Tejun Heo07bd9682009-04-19 08:46:03 +09001134 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135
Borislav Petkove972d702009-07-17 23:55:16 +00001136 ide_debug_log(IDE_DBG_FUNC, "count %Zd", count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137
Borislav Petkov54abf372008-02-06 02:57:52 +01001138 if (tape->chrdev_dir != IDETAPE_DIR_READ) {
Borislav Petkov49d80782009-06-07 15:37:06 +02001139 if (test_bit(ilog2(IDE_AFLAG_DETECT_BS), &drive->atapi_flags))
Borislav Petkov54bb2072008-02-06 02:57:52 +01001140 if (count > tape->blk_size &&
1141 (count % tape->blk_size) == 0)
1142 tape->user_bs_factor = count / tape->blk_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143 }
Tejun Heo07bd9682009-04-19 08:46:03 +09001144
Tejun Heo88f1b942009-04-19 08:46:02 +09001145 rc = idetape_init_rw(drive, IDETAPE_DIR_READ);
Borislav Petkov8d06bfa2008-02-06 02:57:53 +01001146 if (rc < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147 return rc;
Tejun Heo07bd9682009-04-19 08:46:03 +09001148
1149 while (done < count) {
1150 size_t todo;
1151
1152 /* refill if staging buffer is empty */
1153 if (!tape->valid) {
1154 /* If we are at a filemark, nothing more to read */
Borislav Petkov49d80782009-06-07 15:37:06 +02001155 if (test_bit(ilog2(IDE_AFLAG_FILEMARK),
1156 &drive->atapi_flags))
Tejun Heo07bd9682009-04-19 08:46:03 +09001157 break;
1158 /* read */
1159 if (idetape_queue_rw_tail(drive, REQ_IDETAPE_READ,
1160 tape->buffer_size) <= 0)
1161 break;
1162 }
1163
1164 /* copy out */
1165 todo = min_t(size_t, count - done, tape->valid);
1166 if (copy_to_user(buf + done, tape->cur, todo))
Daniel Walkerdcd96372006-06-25 05:47:37 -07001167 ret = -EFAULT;
Tejun Heo07bd9682009-04-19 08:46:03 +09001168
1169 tape->cur += todo;
1170 tape->valid -= todo;
1171 done += todo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172 }
Tejun Heo07bd9682009-04-19 08:46:03 +09001173
Borislav Petkov49d80782009-06-07 15:37:06 +02001174 if (!done && test_bit(ilog2(IDE_AFLAG_FILEMARK), &drive->atapi_flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175 idetape_space_over_filemarks(drive, MTFSF, 1);
1176 return 0;
1177 }
Daniel Walkerdcd96372006-06-25 05:47:37 -07001178
Tejun Heo07bd9682009-04-19 08:46:03 +09001179 return ret ? ret : done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180}
1181
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001182static ssize_t idetape_chrdev_write(struct file *file, const char __user *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183 size_t count, loff_t *ppos)
1184{
Borislav Petkov5aeddf92008-10-13 21:39:34 +02001185 struct ide_tape_obj *tape = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186 ide_drive_t *drive = tape->drive;
Tejun Heo07bd9682009-04-19 08:46:03 +09001187 size_t done = 0;
Daniel Walkerdcd96372006-06-25 05:47:37 -07001188 ssize_t ret = 0;
Tejun Heo88f1b942009-04-19 08:46:02 +09001189 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190
1191 /* The drive is write protected. */
1192 if (tape->write_prot)
1193 return -EACCES;
1194
Borislav Petkove972d702009-07-17 23:55:16 +00001195 ide_debug_log(IDE_DBG_FUNC, "count %Zd", count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196
1197 /* Initialize write operation */
Tejun Heo88f1b942009-04-19 08:46:02 +09001198 rc = idetape_init_rw(drive, IDETAPE_DIR_WRITE);
1199 if (rc < 0)
1200 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201
Tejun Heo07bd9682009-04-19 08:46:03 +09001202 while (done < count) {
1203 size_t todo;
1204
1205 /* flush if staging buffer is full */
1206 if (tape->valid == tape->buffer_size &&
1207 idetape_queue_rw_tail(drive, REQ_IDETAPE_WRITE,
1208 tape->buffer_size) <= 0)
1209 return rc;
1210
1211 /* copy in */
1212 todo = min_t(size_t, count - done,
1213 tape->buffer_size - tape->valid);
1214 if (copy_from_user(tape->cur, buf + done, todo))
Daniel Walkerdcd96372006-06-25 05:47:37 -07001215 ret = -EFAULT;
Tejun Heo07bd9682009-04-19 08:46:03 +09001216
1217 tape->cur += todo;
1218 tape->valid += todo;
1219 done += todo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220 }
Tejun Heo07bd9682009-04-19 08:46:03 +09001221
1222 return ret ? ret : done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223}
1224
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001225static int idetape_write_filemark(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226{
Bartlomiej Zolnierkiewicz2ac07d92008-10-10 22:39:38 +02001227 struct ide_tape_obj *tape = drive->driver_data;
Borislav Petkovd236d742008-04-18 00:46:27 +02001228 struct ide_atapi_pc pc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229
1230 /* Write a filemark */
1231 idetape_create_write_filemark_cmd(drive, &pc, 1);
Borislav Petkovb13345f2009-05-02 10:26:12 +02001232 if (ide_queue_pc_tail(drive, tape->disk, &pc, NULL, 0)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233 printk(KERN_ERR "ide-tape: Couldn't write a filemark\n");
1234 return -EIO;
1235 }
1236 return 0;
1237}
1238
1239/*
Borislav Petkovd99c9da2008-02-02 19:56:51 +01001240 * Called from idetape_chrdev_ioctl when the general mtio MTIOCTOP ioctl is
1241 * requested.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242 *
Borislav Petkovd99c9da2008-02-02 19:56:51 +01001243 * Note: MTBSF and MTBSFM are not supported when the tape doesn't support
1244 * spacing over filemarks in the reverse direction. In this case, MTFSFM is also
Borislav Petkov5bd50dc2008-04-27 15:38:28 +02001245 * usually not supported.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246 *
Borislav Petkovd99c9da2008-02-02 19:56:51 +01001247 * The following commands are currently not supported:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248 *
Borislav Petkovd99c9da2008-02-02 19:56:51 +01001249 * MTFSS, MTBSS, MTWSM, MTSETDENSITY, MTSETDRVBUFFER, MT_ST_BOOLEANS,
1250 * MT_ST_WRITE_THRESHOLD.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251 */
Borislav Petkovd99c9da2008-02-02 19:56:51 +01001252static int idetape_mtioctop(ide_drive_t *drive, short mt_op, int mt_count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253{
1254 idetape_tape_t *tape = drive->driver_data;
Bartlomiej Zolnierkiewicz2ac07d92008-10-10 22:39:38 +02001255 struct gendisk *disk = tape->disk;
Borislav Petkovd236d742008-04-18 00:46:27 +02001256 struct ide_atapi_pc pc;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001257 int i, retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258
Borislav Petkove972d702009-07-17 23:55:16 +00001259 ide_debug_log(IDE_DBG_FUNC, "MTIOCTOP ioctl: mt_op: %d, mt_count: %d",
1260 mt_op, mt_count);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001261
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262 switch (mt_op) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001263 case MTFSF:
1264 case MTFSFM:
1265 case MTBSF:
1266 case MTBSFM:
1267 if (!mt_count)
1268 return 0;
1269 return idetape_space_over_filemarks(drive, mt_op, mt_count);
1270 default:
1271 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272 }
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001273
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274 switch (mt_op) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001275 case MTWEOF:
1276 if (tape->write_prot)
1277 return -EACCES;
Borislav Petkovec0fdb02008-04-27 15:38:34 +02001278 ide_tape_discard_merge_buffer(drive, 1);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001279 for (i = 0; i < mt_count; i++) {
1280 retval = idetape_write_filemark(drive);
1281 if (retval)
1282 return retval;
1283 }
1284 return 0;
1285 case MTREW:
Borislav Petkovec0fdb02008-04-27 15:38:34 +02001286 ide_tape_discard_merge_buffer(drive, 0);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001287 if (idetape_rewind_tape(drive))
1288 return -EIO;
1289 return 0;
1290 case MTLOAD:
Borislav Petkovec0fdb02008-04-27 15:38:34 +02001291 ide_tape_discard_merge_buffer(drive, 0);
Bartlomiej Zolnierkiewicz0c8a6c72008-10-10 22:39:39 +02001292 return ide_do_start_stop(drive, disk, IDETAPE_LU_LOAD_MASK);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001293 case MTUNLOAD:
1294 case MTOFFL:
1295 /*
1296 * If door is locked, attempt to unlock before
1297 * attempting to eject.
1298 */
1299 if (tape->door_locked) {
Bartlomiej Zolnierkiewicz05780422008-10-10 22:39:38 +02001300 if (!ide_set_media_lock(drive, disk, 0))
Bartlomiej Zolnierkiewicz385a4b82008-10-10 22:39:37 +02001301 tape->door_locked = DOOR_UNLOCKED;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001302 }
Borislav Petkovec0fdb02008-04-27 15:38:34 +02001303 ide_tape_discard_merge_buffer(drive, 0);
Bartlomiej Zolnierkiewicz0c8a6c72008-10-10 22:39:39 +02001304 retval = ide_do_start_stop(drive, disk, !IDETAPE_LU_LOAD_MASK);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001305 if (!retval)
Borislav Petkov49d80782009-06-07 15:37:06 +02001306 clear_bit(ilog2(IDE_AFLAG_MEDIUM_PRESENT),
1307 &drive->atapi_flags);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001308 return retval;
1309 case MTNOP:
Borislav Petkovec0fdb02008-04-27 15:38:34 +02001310 ide_tape_discard_merge_buffer(drive, 0);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001311 return idetape_flush_tape_buffers(drive);
1312 case MTRETEN:
Borislav Petkovec0fdb02008-04-27 15:38:34 +02001313 ide_tape_discard_merge_buffer(drive, 0);
Bartlomiej Zolnierkiewicz0c8a6c72008-10-10 22:39:39 +02001314 return ide_do_start_stop(drive, disk,
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001315 IDETAPE_LU_RETENSION_MASK | IDETAPE_LU_LOAD_MASK);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001316 case MTEOM:
1317 idetape_create_space_cmd(&pc, 0, IDETAPE_SPACE_TO_EOD);
Borislav Petkovb13345f2009-05-02 10:26:12 +02001318 return ide_queue_pc_tail(drive, disk, &pc, NULL, 0);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001319 case MTERASE:
1320 (void)idetape_rewind_tape(drive);
1321 idetape_create_erase_cmd(&pc);
Borislav Petkovb13345f2009-05-02 10:26:12 +02001322 return ide_queue_pc_tail(drive, disk, &pc, NULL, 0);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001323 case MTSETBLK:
1324 if (mt_count) {
1325 if (mt_count < tape->blk_size ||
1326 mt_count % tape->blk_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327 return -EIO;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001328 tape->user_bs_factor = mt_count / tape->blk_size;
Borislav Petkov49d80782009-06-07 15:37:06 +02001329 clear_bit(ilog2(IDE_AFLAG_DETECT_BS),
1330 &drive->atapi_flags);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001331 } else
Borislav Petkov49d80782009-06-07 15:37:06 +02001332 set_bit(ilog2(IDE_AFLAG_DETECT_BS),
1333 &drive->atapi_flags);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001334 return 0;
1335 case MTSEEK:
Borislav Petkovec0fdb02008-04-27 15:38:34 +02001336 ide_tape_discard_merge_buffer(drive, 0);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001337 return idetape_position_tape(drive,
1338 mt_count * tape->user_bs_factor, tape->partition, 0);
1339 case MTSETPART:
Borislav Petkovec0fdb02008-04-27 15:38:34 +02001340 ide_tape_discard_merge_buffer(drive, 0);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001341 return idetape_position_tape(drive, 0, mt_count, 0);
1342 case MTFSR:
1343 case MTBSR:
1344 case MTLOCK:
Bartlomiej Zolnierkiewicz05780422008-10-10 22:39:38 +02001345 retval = ide_set_media_lock(drive, disk, 1);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001346 if (retval)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347 return retval;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001348 tape->door_locked = DOOR_EXPLICITLY_LOCKED;
1349 return 0;
1350 case MTUNLOCK:
Bartlomiej Zolnierkiewicz05780422008-10-10 22:39:38 +02001351 retval = ide_set_media_lock(drive, disk, 0);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001352 if (retval)
1353 return retval;
1354 tape->door_locked = DOOR_UNLOCKED;
1355 return 0;
1356 default:
1357 printk(KERN_ERR "ide-tape: MTIO operation %d not supported\n",
1358 mt_op);
1359 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360 }
1361}
1362
1363/*
Borislav Petkovd99c9da2008-02-02 19:56:51 +01001364 * Our character device ioctls. General mtio.h magnetic io commands are
1365 * supported here, and not in the corresponding block interface. Our own
1366 * ide-tape ioctls are supported on both interfaces.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367 */
Alan Cox05227ad2010-01-04 06:24:00 +00001368static long do_idetape_chrdev_ioctl(struct file *file,
Borislav Petkovd99c9da2008-02-02 19:56:51 +01001369 unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370{
Borislav Petkov5aeddf92008-10-13 21:39:34 +02001371 struct ide_tape_obj *tape = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372 ide_drive_t *drive = tape->drive;
1373 struct mtop mtop;
1374 struct mtget mtget;
1375 struct mtpos mtpos;
Borislav Petkov54bb2072008-02-06 02:57:52 +01001376 int block_offset = 0, position = tape->first_frame;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377 void __user *argp = (void __user *)arg;
1378
Borislav Petkove972d702009-07-17 23:55:16 +00001379 ide_debug_log(IDE_DBG_FUNC, "cmd: 0x%x", cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380
Borislav Petkov54abf372008-02-06 02:57:52 +01001381 if (tape->chrdev_dir == IDETAPE_DIR_WRITE) {
Borislav Petkovd9df9372008-04-27 15:38:34 +02001382 ide_tape_flush_merge_buffer(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383 idetape_flush_tape_buffers(drive);
1384 }
1385 if (cmd == MTIOCGET || cmd == MTIOCPOS) {
Tejun Heo963da552009-04-19 08:46:02 +09001386 block_offset = tape->valid /
Borislav Petkov54bb2072008-02-06 02:57:52 +01001387 (tape->blk_size * tape->user_bs_factor);
Borislav Petkov55ce3a12009-05-04 09:38:15 +02001388 position = ide_tape_read_position(drive);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001389 if (position < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390 return -EIO;
1391 }
1392 switch (cmd) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001393 case MTIOCTOP:
1394 if (copy_from_user(&mtop, argp, sizeof(struct mtop)))
1395 return -EFAULT;
1396 return idetape_mtioctop(drive, mtop.mt_op, mtop.mt_count);
1397 case MTIOCGET:
1398 memset(&mtget, 0, sizeof(struct mtget));
1399 mtget.mt_type = MT_ISSCSI2;
1400 mtget.mt_blkno = position / tape->user_bs_factor - block_offset;
1401 mtget.mt_dsreg =
1402 ((tape->blk_size * tape->user_bs_factor)
1403 << MT_ST_BLKSIZE_SHIFT) & MT_ST_BLKSIZE_MASK;
Borislav Petkov54bb2072008-02-06 02:57:52 +01001404
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001405 if (tape->drv_write_prot)
1406 mtget.mt_gstat |= GMT_WR_PROT(0xffffffff);
1407
1408 if (copy_to_user(argp, &mtget, sizeof(struct mtget)))
1409 return -EFAULT;
1410 return 0;
1411 case MTIOCPOS:
1412 mtpos.mt_blkno = position / tape->user_bs_factor - block_offset;
1413 if (copy_to_user(argp, &mtpos, sizeof(struct mtpos)))
1414 return -EFAULT;
1415 return 0;
1416 default:
1417 if (tape->chrdev_dir == IDETAPE_DIR_READ)
Borislav Petkovec0fdb02008-04-27 15:38:34 +02001418 ide_tape_discard_merge_buffer(drive, 1);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001419 return idetape_blkdev_ioctl(drive, cmd, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420 }
1421}
1422
Alan Cox05227ad2010-01-04 06:24:00 +00001423static long idetape_chrdev_ioctl(struct file *file,
1424 unsigned int cmd, unsigned long arg)
1425{
1426 long ret;
Arnd Bergmann2a48fc02010-06-02 14:28:52 +02001427 mutex_lock(&ide_tape_mutex);
Alan Cox05227ad2010-01-04 06:24:00 +00001428 ret = do_idetape_chrdev_ioctl(file, cmd, arg);
Arnd Bergmann2a48fc02010-06-02 14:28:52 +02001429 mutex_unlock(&ide_tape_mutex);
Alan Cox05227ad2010-01-04 06:24:00 +00001430 return ret;
1431}
1432
Borislav Petkov3cffb9c2008-02-02 19:56:50 +01001433/*
1434 * Do a mode sense page 0 with block descriptor and if it succeeds set the tape
1435 * block size with the reported value.
1436 */
1437static void ide_tape_get_bsize_from_bdesc(ide_drive_t *drive)
1438{
1439 idetape_tape_t *tape = drive->driver_data;
Borislav Petkovd236d742008-04-18 00:46:27 +02001440 struct ide_atapi_pc pc;
Borislav Petkov837272b2009-05-04 09:48:57 +02001441 u8 buf[12];
Borislav Petkov3cffb9c2008-02-02 19:56:50 +01001442
1443 idetape_create_mode_sense_cmd(&pc, IDETAPE_BLOCK_DESCRIPTOR);
Borislav Petkov837272b2009-05-04 09:48:57 +02001444 if (ide_queue_pc_tail(drive, tape->disk, &pc, buf, pc.req_xfer)) {
Borislav Petkov3cffb9c2008-02-02 19:56:50 +01001445 printk(KERN_ERR "ide-tape: Can't get block descriptor\n");
Borislav Petkov54bb2072008-02-06 02:57:52 +01001446 if (tape->blk_size == 0) {
Borislav Petkov3cffb9c2008-02-02 19:56:50 +01001447 printk(KERN_WARNING "ide-tape: Cannot deal with zero "
1448 "block size, assuming 32k\n");
Borislav Petkov54bb2072008-02-06 02:57:52 +01001449 tape->blk_size = 32768;
Borislav Petkov3cffb9c2008-02-02 19:56:50 +01001450 }
1451 return;
1452 }
Borislav Petkov837272b2009-05-04 09:48:57 +02001453 tape->blk_size = (buf[4 + 5] << 16) +
1454 (buf[4 + 6] << 8) +
1455 buf[4 + 7];
1456 tape->drv_write_prot = (buf[2] & 0x80) >> 7;
Borislav Petkove972d702009-07-17 23:55:16 +00001457
1458 ide_debug_log(IDE_DBG_FUNC, "blk_size: %d, write_prot: %d",
1459 tape->blk_size, tape->drv_write_prot);
Borislav Petkov3cffb9c2008-02-02 19:56:50 +01001460}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001462static int idetape_chrdev_open(struct inode *inode, struct file *filp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463{
1464 unsigned int minor = iminor(inode), i = minor & ~0xc0;
1465 ide_drive_t *drive;
1466 idetape_tape_t *tape;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467 int retval;
1468
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001469 if (i >= MAX_HWIFS * MAX_DRIVES)
1470 return -ENXIO;
1471
Borislav Petkovcbba2fa2009-10-11 00:25:19 +00001472 mutex_lock(&idetape_chrdev_mutex);
1473
Borislav Petkov9d01e4c2009-06-08 22:03:03 +02001474 tape = ide_tape_get(NULL, true, i);
Jonathan Corbet04f4ac92008-05-15 12:01:56 -06001475 if (!tape) {
Borislav Petkovcbba2fa2009-10-11 00:25:19 +00001476 mutex_unlock(&idetape_chrdev_mutex);
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001477 return -ENXIO;
Jonathan Corbet04f4ac92008-05-15 12:01:56 -06001478 }
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001479
Borislav Petkove972d702009-07-17 23:55:16 +00001480 drive = tape->drive;
1481 filp->private_data = tape;
1482
1483 ide_debug_log(IDE_DBG_FUNC, "enter");
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001484
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485 /*
1486 * We really want to do nonseekable_open(inode, filp); here, but some
1487 * versions of tar incorrectly call lseek on tapes and bail out if that
1488 * fails. So we disallow pread() and pwrite(), but permit lseeks.
1489 */
1490 filp->f_mode &= ~(FMODE_PREAD | FMODE_PWRITE);
1491
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492
Borislav Petkov49d80782009-06-07 15:37:06 +02001493 if (test_and_set_bit(ilog2(IDE_AFLAG_BUSY), &drive->atapi_flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494 retval = -EBUSY;
1495 goto out_put_tape;
1496 }
1497
1498 retval = idetape_wait_ready(drive, 60 * HZ);
1499 if (retval) {
Borislav Petkov49d80782009-06-07 15:37:06 +02001500 clear_bit(ilog2(IDE_AFLAG_BUSY), &drive->atapi_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501 printk(KERN_ERR "ide-tape: %s: drive not ready\n", tape->name);
1502 goto out_put_tape;
1503 }
1504
Borislav Petkov79ca7432009-06-15 07:32:04 +02001505 ide_tape_read_position(drive);
Borislav Petkov49d80782009-06-07 15:37:06 +02001506 if (!test_bit(ilog2(IDE_AFLAG_ADDRESS_VALID), &drive->atapi_flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507 (void)idetape_rewind_tape(drive);
1508
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509 /* Read block size and write protect status from drive. */
Borislav Petkov3cffb9c2008-02-02 19:56:50 +01001510 ide_tape_get_bsize_from_bdesc(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511
1512 /* Set write protect flag if device is opened as read-only. */
1513 if ((filp->f_flags & O_ACCMODE) == O_RDONLY)
1514 tape->write_prot = 1;
1515 else
1516 tape->write_prot = tape->drv_write_prot;
1517
1518 /* Make sure drive isn't write protected if user wants to write. */
1519 if (tape->write_prot) {
1520 if ((filp->f_flags & O_ACCMODE) == O_WRONLY ||
1521 (filp->f_flags & O_ACCMODE) == O_RDWR) {
Borislav Petkov49d80782009-06-07 15:37:06 +02001522 clear_bit(ilog2(IDE_AFLAG_BUSY), &drive->atapi_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523 retval = -EROFS;
1524 goto out_put_tape;
1525 }
1526 }
1527
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001528 /* Lock the tape drive door so user can't eject. */
Borislav Petkov54abf372008-02-06 02:57:52 +01001529 if (tape->chrdev_dir == IDETAPE_DIR_NONE) {
Bartlomiej Zolnierkiewicz05780422008-10-10 22:39:38 +02001530 if (!ide_set_media_lock(drive, tape->disk, 1)) {
Bartlomiej Zolnierkiewicz385a4b82008-10-10 22:39:37 +02001531 if (tape->door_locked != DOOR_EXPLICITLY_LOCKED)
1532 tape->door_locked = DOOR_LOCKED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533 }
1534 }
Borislav Petkovcbba2fa2009-10-11 00:25:19 +00001535 mutex_unlock(&idetape_chrdev_mutex);
1536
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537 return 0;
1538
1539out_put_tape:
1540 ide_tape_put(tape);
Borislav Petkovcbba2fa2009-10-11 00:25:19 +00001541
1542 mutex_unlock(&idetape_chrdev_mutex);
1543
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544 return retval;
1545}
1546
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001547static void idetape_write_release(ide_drive_t *drive, unsigned int minor)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548{
1549 idetape_tape_t *tape = drive->driver_data;
1550
Borislav Petkovd9df9372008-04-27 15:38:34 +02001551 ide_tape_flush_merge_buffer(drive);
Tejun Heo963da552009-04-19 08:46:02 +09001552 tape->buf = kmalloc(tape->buffer_size, GFP_KERNEL);
1553 if (tape->buf != NULL) {
Borislav Petkov54bb2072008-02-06 02:57:52 +01001554 idetape_pad_zeros(drive, tape->blk_size *
1555 (tape->user_bs_factor - 1));
Tejun Heo963da552009-04-19 08:46:02 +09001556 kfree(tape->buf);
1557 tape->buf = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558 }
1559 idetape_write_filemark(drive);
1560 idetape_flush_tape_buffers(drive);
1561 idetape_flush_tape_buffers(drive);
1562}
1563
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001564static int idetape_chrdev_release(struct inode *inode, struct file *filp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565{
Borislav Petkov5aeddf92008-10-13 21:39:34 +02001566 struct ide_tape_obj *tape = filp->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567 ide_drive_t *drive = tape->drive;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001568 unsigned int minor = iminor(inode);
1569
Borislav Petkovcbba2fa2009-10-11 00:25:19 +00001570 mutex_lock(&idetape_chrdev_mutex);
1571
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572 tape = drive->driver_data;
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001573
Borislav Petkove972d702009-07-17 23:55:16 +00001574 ide_debug_log(IDE_DBG_FUNC, "enter");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001575
Borislav Petkov54abf372008-02-06 02:57:52 +01001576 if (tape->chrdev_dir == IDETAPE_DIR_WRITE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001577 idetape_write_release(drive, minor);
Borislav Petkov54abf372008-02-06 02:57:52 +01001578 if (tape->chrdev_dir == IDETAPE_DIR_READ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579 if (minor < 128)
Borislav Petkovec0fdb02008-04-27 15:38:34 +02001580 ide_tape_discard_merge_buffer(drive, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581 }
Borislav Petkovf64eee72008-04-27 15:38:25 +02001582
Borislav Petkov49d80782009-06-07 15:37:06 +02001583 if (minor < 128 && test_bit(ilog2(IDE_AFLAG_MEDIUM_PRESENT),
1584 &drive->atapi_flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585 (void) idetape_rewind_tape(drive);
Borislav Petkov49d80782009-06-07 15:37:06 +02001586
Borislav Petkov54abf372008-02-06 02:57:52 +01001587 if (tape->chrdev_dir == IDETAPE_DIR_NONE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001588 if (tape->door_locked == DOOR_LOCKED) {
Bartlomiej Zolnierkiewicz05780422008-10-10 22:39:38 +02001589 if (!ide_set_media_lock(drive, tape->disk, 0))
Bartlomiej Zolnierkiewicz385a4b82008-10-10 22:39:37 +02001590 tape->door_locked = DOOR_UNLOCKED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591 }
1592 }
Borislav Petkov49d80782009-06-07 15:37:06 +02001593 clear_bit(ilog2(IDE_AFLAG_BUSY), &drive->atapi_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594 ide_tape_put(tape);
Borislav Petkovcbba2fa2009-10-11 00:25:19 +00001595
1596 mutex_unlock(&idetape_chrdev_mutex);
1597
Linus Torvalds1da177e2005-04-16 15:20:36 -07001598 return 0;
1599}
1600
Borislav Petkov6d29c8f2008-02-02 19:56:49 +01001601static void idetape_get_inquiry_results(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001602{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001603 idetape_tape_t *tape = drive->driver_data;
Borislav Petkovd236d742008-04-18 00:46:27 +02001604 struct ide_atapi_pc pc;
Bartlomiej Zolnierkiewicz41fa9f82009-03-31 20:15:25 +02001605 u8 pc_buf[256];
Borislav Petkov801bd322008-09-27 19:32:17 +02001606 char fw_rev[4], vendor_id[8], product_id[16];
Borislav Petkov6d29c8f2008-02-02 19:56:49 +01001607
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608 idetape_create_inquiry_cmd(&pc);
Borislav Petkovb13345f2009-05-02 10:26:12 +02001609 if (ide_queue_pc_tail(drive, tape->disk, &pc, pc_buf, pc.req_xfer)) {
Borislav Petkov6d29c8f2008-02-02 19:56:49 +01001610 printk(KERN_ERR "ide-tape: %s: can't get INQUIRY results\n",
1611 tape->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001612 return;
1613 }
Borislav Petkovb13345f2009-05-02 10:26:12 +02001614 memcpy(vendor_id, &pc_buf[8], 8);
1615 memcpy(product_id, &pc_buf[16], 16);
1616 memcpy(fw_rev, &pc_buf[32], 4);
Borislav Petkov6d29c8f2008-02-02 19:56:49 +01001617
Borislav Petkov801bd322008-09-27 19:32:17 +02001618 ide_fixstring(vendor_id, 8, 0);
1619 ide_fixstring(product_id, 16, 0);
1620 ide_fixstring(fw_rev, 4, 0);
Borislav Petkov41f81d52008-02-06 02:57:52 +01001621
Borislav Petkov801bd322008-09-27 19:32:17 +02001622 printk(KERN_INFO "ide-tape: %s <-> %s: %.8s %.16s rev %.4s\n",
Borislav Petkov41f81d52008-02-06 02:57:52 +01001623 drive->name, tape->name, vendor_id, product_id, fw_rev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001624}
1625
1626/*
Borislav Petkovb6422012008-02-02 19:56:49 +01001627 * Ask the tape about its various parameters. In particular, we will adjust our
1628 * data transfer buffer size to the recommended value as returned by the tape.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001629 */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001630static void idetape_get_mode_sense_results(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001631{
1632 idetape_tape_t *tape = drive->driver_data;
Borislav Petkovd236d742008-04-18 00:46:27 +02001633 struct ide_atapi_pc pc;
Borislav Petkovb13345f2009-05-02 10:26:12 +02001634 u8 buf[24], *caps;
Borislav Petkovb6422012008-02-02 19:56:49 +01001635 u8 speed, max_speed;
Borislav Petkov47314fa2008-02-02 19:56:48 +01001636
Linus Torvalds1da177e2005-04-16 15:20:36 -07001637 idetape_create_mode_sense_cmd(&pc, IDETAPE_CAPABILITIES_PAGE);
Borislav Petkovb13345f2009-05-02 10:26:12 +02001638 if (ide_queue_pc_tail(drive, tape->disk, &pc, buf, pc.req_xfer)) {
Borislav Petkovb6422012008-02-02 19:56:49 +01001639 printk(KERN_ERR "ide-tape: Can't get tape parameters - assuming"
1640 " some default values\n");
Borislav Petkov54bb2072008-02-06 02:57:52 +01001641 tape->blk_size = 512;
Borislav Petkovb6422012008-02-02 19:56:49 +01001642 put_unaligned(52, (u16 *)&tape->caps[12]);
1643 put_unaligned(540, (u16 *)&tape->caps[14]);
1644 put_unaligned(6*52, (u16 *)&tape->caps[16]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001645 return;
1646 }
Borislav Petkovb13345f2009-05-02 10:26:12 +02001647 caps = buf + 4 + buf[3];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001648
Borislav Petkovb6422012008-02-02 19:56:49 +01001649 /* convert to host order and save for later use */
Harvey Harrisoncd740ab2008-07-24 22:53:33 +02001650 speed = be16_to_cpup((__be16 *)&caps[14]);
1651 max_speed = be16_to_cpup((__be16 *)&caps[8]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001652
Harvey Harrisoncd740ab2008-07-24 22:53:33 +02001653 *(u16 *)&caps[8] = max_speed;
1654 *(u16 *)&caps[12] = be16_to_cpup((__be16 *)&caps[12]);
1655 *(u16 *)&caps[14] = speed;
1656 *(u16 *)&caps[16] = be16_to_cpup((__be16 *)&caps[16]);
Borislav Petkovb6422012008-02-02 19:56:49 +01001657
1658 if (!speed) {
1659 printk(KERN_INFO "ide-tape: %s: invalid tape speed "
1660 "(assuming 650KB/sec)\n", drive->name);
Harvey Harrisoncd740ab2008-07-24 22:53:33 +02001661 *(u16 *)&caps[14] = 650;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001662 }
Borislav Petkovb6422012008-02-02 19:56:49 +01001663 if (!max_speed) {
1664 printk(KERN_INFO "ide-tape: %s: invalid max_speed "
1665 "(assuming 650KB/sec)\n", drive->name);
Harvey Harrisoncd740ab2008-07-24 22:53:33 +02001666 *(u16 *)&caps[8] = 650;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001667 }
1668
Borislav Petkovb6422012008-02-02 19:56:49 +01001669 memcpy(&tape->caps, caps, 20);
Bartlomiej Zolnierkiewicz05780422008-10-10 22:39:38 +02001670
1671 /* device lacks locking support according to capabilities page */
1672 if ((caps[6] & 1) == 0)
Bartlomiej Zolnierkiewicz42619d32008-10-17 18:09:11 +02001673 drive->dev_flags &= ~IDE_DFLAG_DOORLOCKING;
Bartlomiej Zolnierkiewicz05780422008-10-10 22:39:38 +02001674
Borislav Petkovb6422012008-02-02 19:56:49 +01001675 if (caps[7] & 0x02)
Borislav Petkov54bb2072008-02-06 02:57:52 +01001676 tape->blk_size = 512;
Borislav Petkovb6422012008-02-02 19:56:49 +01001677 else if (caps[7] & 0x04)
Borislav Petkov54bb2072008-02-06 02:57:52 +01001678 tape->blk_size = 1024;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679}
1680
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +02001681#ifdef CONFIG_IDE_PROC_FS
Bartlomiej Zolnierkiewicz8185d5a2008-10-10 22:39:28 +02001682#define ide_tape_devset_get(name, field) \
1683static int get_##name(ide_drive_t *drive) \
1684{ \
1685 idetape_tape_t *tape = drive->driver_data; \
1686 return tape->field; \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001687}
Bartlomiej Zolnierkiewicz8185d5a2008-10-10 22:39:28 +02001688
1689#define ide_tape_devset_set(name, field) \
1690static int set_##name(ide_drive_t *drive, int arg) \
1691{ \
1692 idetape_tape_t *tape = drive->driver_data; \
1693 tape->field = arg; \
1694 return 0; \
1695}
1696
Elias Oltmanns92f1f8f2008-10-10 22:39:40 +02001697#define ide_tape_devset_rw_field(_name, _field) \
Bartlomiej Zolnierkiewicz8185d5a2008-10-10 22:39:28 +02001698ide_tape_devset_get(_name, _field) \
1699ide_tape_devset_set(_name, _field) \
Elias Oltmanns92f1f8f2008-10-10 22:39:40 +02001700IDE_DEVSET(_name, DS_SYNC, get_##_name, set_##_name)
Bartlomiej Zolnierkiewicz8185d5a2008-10-10 22:39:28 +02001701
Elias Oltmanns92f1f8f2008-10-10 22:39:40 +02001702#define ide_tape_devset_r_field(_name, _field) \
Bartlomiej Zolnierkiewicz8185d5a2008-10-10 22:39:28 +02001703ide_tape_devset_get(_name, _field) \
Elias Oltmanns92f1f8f2008-10-10 22:39:40 +02001704IDE_DEVSET(_name, 0, get_##_name, NULL)
Bartlomiej Zolnierkiewicz8185d5a2008-10-10 22:39:28 +02001705
1706static int mulf_tdsc(ide_drive_t *drive) { return 1000; }
1707static int divf_tdsc(ide_drive_t *drive) { return HZ; }
1708static int divf_buffer(ide_drive_t *drive) { return 2; }
1709static int divf_buffer_size(ide_drive_t *drive) { return 1024; }
1710
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +02001711ide_devset_rw_flag(dsc_overlap, IDE_DFLAG_DSC_OVERLAP);
Bartlomiej Zolnierkiewicz8185d5a2008-10-10 22:39:28 +02001712
Elias Oltmanns92f1f8f2008-10-10 22:39:40 +02001713ide_tape_devset_rw_field(tdsc, best_dsc_rw_freq);
Bartlomiej Zolnierkiewicz8185d5a2008-10-10 22:39:28 +02001714
Elias Oltmanns92f1f8f2008-10-10 22:39:40 +02001715ide_tape_devset_r_field(avg_speed, avg_speed);
1716ide_tape_devset_r_field(speed, caps[14]);
1717ide_tape_devset_r_field(buffer, caps[16]);
1718ide_tape_devset_r_field(buffer_size, buffer_size);
Bartlomiej Zolnierkiewicz8185d5a2008-10-10 22:39:28 +02001719
Elias Oltmanns92f1f8f2008-10-10 22:39:40 +02001720static const struct ide_proc_devset idetape_settings[] = {
1721 __IDE_PROC_DEVSET(avg_speed, 0, 0xffff, NULL, NULL),
1722 __IDE_PROC_DEVSET(buffer, 0, 0xffff, NULL, divf_buffer),
1723 __IDE_PROC_DEVSET(buffer_size, 0, 0xffff, NULL, divf_buffer_size),
Elias Oltmanns92f1f8f2008-10-10 22:39:40 +02001724 __IDE_PROC_DEVSET(dsc_overlap, 0, 1, NULL, NULL),
1725 __IDE_PROC_DEVSET(speed, 0, 0xffff, NULL, NULL),
1726 __IDE_PROC_DEVSET(tdsc, IDETAPE_DSC_RW_MIN, IDETAPE_DSC_RW_MAX,
1727 mulf_tdsc, divf_tdsc),
Hannes Eder71bfc7a2009-03-05 16:10:56 +01001728 { NULL },
Bartlomiej Zolnierkiewicz8185d5a2008-10-10 22:39:28 +02001729};
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +02001730#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001731
1732/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001733 * The function below is called to:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001734 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001735 * 1. Initialize our various state variables.
1736 * 2. Ask the tape for its capabilities.
1737 * 3. Allocate a buffer which will be used for data transfer. The buffer size
1738 * is chosen based on the recommendation which we received in step 2.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001739 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001740 * Note that at this point ide.c already assigned us an irq, so that we can
1741 * queue requests here and wait for their completion.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001742 */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001743static void idetape_setup(ide_drive_t *drive, idetape_tape_t *tape, int minor)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001744{
Borislav Petkov83042b22008-04-27 15:38:27 +02001745 unsigned long t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001746 int speed;
Borislav Petkovf73850a2008-04-27 15:38:33 +02001747 int buffer_size;
Borislav Petkovb6422012008-02-02 19:56:49 +01001748 u16 *ctl = (u16 *)&tape->caps[12];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001749
Borislav Petkove972d702009-07-17 23:55:16 +00001750 ide_debug_log(IDE_DBG_FUNC, "minor: %d", minor);
1751
1752 drive->pc_callback = ide_tape_callback;
Borislav Petkov776bb022008-07-23 19:55:59 +02001753
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +02001754 drive->dev_flags |= IDE_DFLAG_DSC_OVERLAP;
1755
Bartlomiej Zolnierkiewicz4166c192008-02-01 23:09:30 +01001756 if (drive->hwif->host_flags & IDE_HFLAG_NO_DSC) {
1757 printk(KERN_INFO "ide-tape: %s: disabling DSC overlap\n",
1758 tape->name);
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +02001759 drive->dev_flags &= ~IDE_DFLAG_DSC_OVERLAP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001760 }
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +02001761
Linus Torvalds1da177e2005-04-16 15:20:36 -07001762 /* Seagate Travan drives do not support DSC overlap. */
Bartlomiej Zolnierkiewicz4dde4492008-10-10 22:39:19 +02001763 if (strstr((char *)&drive->id[ATA_ID_PROD], "Seagate STT3401"))
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +02001764 drive->dev_flags &= ~IDE_DFLAG_DSC_OVERLAP;
1765
Linus Torvalds1da177e2005-04-16 15:20:36 -07001766 tape->minor = minor;
1767 tape->name[0] = 'h';
1768 tape->name[1] = 't';
1769 tape->name[2] = '0' + minor;
Borislav Petkov54abf372008-02-06 02:57:52 +01001770 tape->chrdev_dir = IDETAPE_DIR_NONE;
Bartlomiej Zolnierkiewicz4dde4492008-10-10 22:39:19 +02001771
Linus Torvalds1da177e2005-04-16 15:20:36 -07001772 idetape_get_inquiry_results(drive);
1773 idetape_get_mode_sense_results(drive);
Borislav Petkov3cffb9c2008-02-02 19:56:50 +01001774 ide_tape_get_bsize_from_bdesc(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001775 tape->user_bs_factor = 1;
Borislav Petkovf73850a2008-04-27 15:38:33 +02001776 tape->buffer_size = *ctl * tape->blk_size;
1777 while (tape->buffer_size > 0xffff) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001778 printk(KERN_NOTICE "ide-tape: decreasing stage size\n");
Borislav Petkovb6422012008-02-02 19:56:49 +01001779 *ctl /= 2;
Borislav Petkovf73850a2008-04-27 15:38:33 +02001780 tape->buffer_size = *ctl * tape->blk_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001781 }
Borislav Petkovf73850a2008-04-27 15:38:33 +02001782 buffer_size = tape->buffer_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001783
Borislav Petkov83042b22008-04-27 15:38:27 +02001784 /* select the "best" DSC read/write polling freq */
Borislav Petkovb6422012008-02-02 19:56:49 +01001785 speed = max(*(u16 *)&tape->caps[14], *(u16 *)&tape->caps[8]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001786
Borislav Petkovf73850a2008-04-27 15:38:33 +02001787 t = (IDETAPE_FIFO_THRESHOLD * tape->buffer_size * HZ) / (speed * 1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788
1789 /*
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001790 * Ensure that the number we got makes sense; limit it within
1791 * IDETAPE_DSC_RW_MIN and IDETAPE_DSC_RW_MAX.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001792 */
Harvey Harrisona792bd52008-07-15 21:21:41 +02001793 tape->best_dsc_rw_freq = clamp_t(unsigned long, t, IDETAPE_DSC_RW_MIN,
1794 IDETAPE_DSC_RW_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001795 printk(KERN_INFO "ide-tape: %s <-> %s: %dKBps, %d*%dkB buffer, "
Borislav Petkov83042b22008-04-27 15:38:27 +02001796 "%lums tDSC%s\n",
Borislav Petkovb6422012008-02-02 19:56:49 +01001797 drive->name, tape->name, *(u16 *)&tape->caps[14],
Borislav Petkovf73850a2008-04-27 15:38:33 +02001798 (*(u16 *)&tape->caps[16] * 512) / tape->buffer_size,
1799 tape->buffer_size / 1024,
Borislav Petkov54bb2072008-02-06 02:57:52 +01001800 tape->best_dsc_rw_freq * 1000 / HZ,
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +02001801 (drive->dev_flags & IDE_DFLAG_USING_DMA) ? ", DMA" : "");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001802
Bartlomiej Zolnierkiewicz1e874f42008-10-10 22:39:27 +02001803 ide_proc_register_driver(drive, tape->driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001804}
1805
Russell King4031bbe2006-01-06 11:41:00 +00001806static void ide_tape_remove(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807{
1808 idetape_tape_t *tape = drive->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001809
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +02001810 ide_proc_unregister_driver(drive, tape->driver);
Bartlomiej Zolnierkiewicz8fed4362009-02-25 20:28:24 +01001811 device_del(&tape->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001812 ide_unregister_region(tape->disk);
1813
Bartlomiej Zolnierkiewicz8fed4362009-02-25 20:28:24 +01001814 mutex_lock(&idetape_ref_mutex);
1815 put_device(&tape->dev);
1816 mutex_unlock(&idetape_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001817}
1818
Bartlomiej Zolnierkiewicz8fed4362009-02-25 20:28:24 +01001819static void ide_tape_release(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001820{
Bartlomiej Zolnierkiewicz8fed4362009-02-25 20:28:24 +01001821 struct ide_tape_obj *tape = to_ide_drv(dev, ide_tape_obj);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001822 ide_drive_t *drive = tape->drive;
1823 struct gendisk *g = tape->disk;
1824
Tejun Heo963da552009-04-19 08:46:02 +09001825 BUG_ON(tape->valid);
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02001826
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +02001827 drive->dev_flags &= ~IDE_DFLAG_DSC_OVERLAP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001828 drive->driver_data = NULL;
Tony Jonesdbc12722007-09-25 02:03:03 +02001829 device_destroy(idetape_sysfs_class, MKDEV(IDETAPE_MAJOR, tape->minor));
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001830 device_destroy(idetape_sysfs_class,
1831 MKDEV(IDETAPE_MAJOR, tape->minor + 128));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001832 idetape_devs[tape->minor] = NULL;
1833 g->private_data = NULL;
1834 put_disk(g);
1835 kfree(tape);
1836}
1837
Bartlomiej Zolnierkiewiczecfd80e2007-05-10 00:01:09 +02001838#ifdef CONFIG_IDE_PROC_FS
Alexey Dobriyan6d703a82009-09-01 17:52:57 -07001839static int idetape_name_proc_show(struct seq_file *m, void *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001840{
Alexey Dobriyan6d703a82009-09-01 17:52:57 -07001841 ide_drive_t *drive = (ide_drive_t *) m->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001842 idetape_tape_t *tape = drive->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001843
Alexey Dobriyan6d703a82009-09-01 17:52:57 -07001844 seq_printf(m, "%s\n", tape->name);
1845 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001846}
1847
Alexey Dobriyan6d703a82009-09-01 17:52:57 -07001848static int idetape_name_proc_open(struct inode *inode, struct file *file)
1849{
1850 return single_open(file, idetape_name_proc_show, PDE(inode)->data);
1851}
1852
1853static const struct file_operations idetape_name_proc_fops = {
1854 .owner = THIS_MODULE,
1855 .open = idetape_name_proc_open,
1856 .read = seq_read,
1857 .llseek = seq_lseek,
1858 .release = single_release,
1859};
1860
Linus Torvalds1da177e2005-04-16 15:20:36 -07001861static ide_proc_entry_t idetape_proc[] = {
Alexey Dobriyan6d703a82009-09-01 17:52:57 -07001862 { "capacity", S_IFREG|S_IRUGO, &ide_capacity_proc_fops },
1863 { "name", S_IFREG|S_IRUGO, &idetape_name_proc_fops },
1864 {}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001865};
Bartlomiej Zolnierkiewicz79cb3802008-10-17 18:09:13 +02001866
1867static ide_proc_entry_t *ide_tape_proc_entries(ide_drive_t *drive)
1868{
1869 return idetape_proc;
1870}
1871
1872static const struct ide_proc_devset *ide_tape_proc_devsets(ide_drive_t *drive)
1873{
1874 return idetape_settings;
1875}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001876#endif
1877
Russell King4031bbe2006-01-06 11:41:00 +00001878static int ide_tape_probe(ide_drive_t *);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001879
Bartlomiej Zolnierkiewicz7f3c8682009-01-06 17:20:53 +01001880static struct ide_driver idetape_driver = {
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02001881 .gen_driver = {
Laurent Riffard4ef3b8f2005-11-18 22:15:40 +01001882 .owner = THIS_MODULE,
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02001883 .name = "ide-tape",
1884 .bus = &ide_bus_type,
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02001885 },
Russell King4031bbe2006-01-06 11:41:00 +00001886 .probe = ide_tape_probe,
1887 .remove = ide_tape_remove,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001888 .version = IDETAPE_VERSION,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001889 .do_request = idetape_do_request,
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +02001890#ifdef CONFIG_IDE_PROC_FS
Bartlomiej Zolnierkiewicz79cb3802008-10-17 18:09:13 +02001891 .proc_entries = ide_tape_proc_entries,
1892 .proc_devsets = ide_tape_proc_devsets,
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +02001893#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001894};
1895
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001896/* Our character device supporting functions, passed to register_chrdev. */
Arjan van de Ven2b8693c2007-02-12 00:55:32 -08001897static const struct file_operations idetape_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001898 .owner = THIS_MODULE,
1899 .read = idetape_chrdev_read,
1900 .write = idetape_chrdev_write,
Alan Cox05227ad2010-01-04 06:24:00 +00001901 .unlocked_ioctl = idetape_chrdev_ioctl,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001902 .open = idetape_chrdev_open,
1903 .release = idetape_chrdev_release,
Arnd Bergmann6038f372010-08-15 18:52:59 +02001904 .llseek = noop_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001905};
1906
Al Viroa4600f82008-03-02 10:27:58 -05001907static int idetape_open(struct block_device *bdev, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001908{
Arnd Bergmann6e9624b2010-08-07 18:25:34 +02001909 struct ide_tape_obj *tape;
1910
Arnd Bergmann2a48fc02010-06-02 14:28:52 +02001911 mutex_lock(&ide_tape_mutex);
Arnd Bergmann6e9624b2010-08-07 18:25:34 +02001912 tape = ide_tape_get(bdev->bd_disk, false, 0);
Arnd Bergmann2a48fc02010-06-02 14:28:52 +02001913 mutex_unlock(&ide_tape_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001914
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001915 if (!tape)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001916 return -ENXIO;
1917
Linus Torvalds1da177e2005-04-16 15:20:36 -07001918 return 0;
1919}
1920
Al Viroa4600f82008-03-02 10:27:58 -05001921static int idetape_release(struct gendisk *disk, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001922{
Borislav Petkov5aeddf92008-10-13 21:39:34 +02001923 struct ide_tape_obj *tape = ide_drv_g(disk, ide_tape_obj);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001924
Arnd Bergmann2a48fc02010-06-02 14:28:52 +02001925 mutex_lock(&ide_tape_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001926 ide_tape_put(tape);
Arnd Bergmann2a48fc02010-06-02 14:28:52 +02001927 mutex_unlock(&ide_tape_mutex);
Arnd Bergmann6e9624b2010-08-07 18:25:34 +02001928
Linus Torvalds1da177e2005-04-16 15:20:36 -07001929 return 0;
1930}
1931
Al Viroa4600f82008-03-02 10:27:58 -05001932static int idetape_ioctl(struct block_device *bdev, fmode_t mode,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001933 unsigned int cmd, unsigned long arg)
1934{
Borislav Petkov5aeddf92008-10-13 21:39:34 +02001935 struct ide_tape_obj *tape = ide_drv_g(bdev->bd_disk, ide_tape_obj);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001936 ide_drive_t *drive = tape->drive;
Arnd Bergmann8a6cfeb2010-07-08 10:18:46 +02001937 int err;
1938
Arnd Bergmann2a48fc02010-06-02 14:28:52 +02001939 mutex_lock(&ide_tape_mutex);
Arnd Bergmann8a6cfeb2010-07-08 10:18:46 +02001940 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);
Arnd Bergmann2a48fc02010-06-02 14:28:52 +02001943 mutex_unlock(&ide_tape_mutex);
Arnd Bergmann8a6cfeb2010-07-08 10:18:46 +02001944
Linus Torvalds1da177e2005-04-16 15:20:36 -07001945 return err;
1946}
1947
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -07001948static const struct block_device_operations idetape_block_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001949 .owner = THIS_MODULE,
Al Viroa4600f82008-03-02 10:27:58 -05001950 .open = idetape_open,
1951 .release = idetape_release,
Arnd Bergmann8a6cfeb2010-07-08 10:18:46 +02001952 .ioctl = idetape_ioctl,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001953};
1954
Russell King4031bbe2006-01-06 11:41:00 +00001955static int ide_tape_probe(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001956{
1957 idetape_tape_t *tape;
1958 struct gendisk *g;
1959 int minor;
1960
Borislav Petkove972d702009-07-17 23:55:16 +00001961 ide_debug_log(IDE_DBG_FUNC, "enter");
1962
1963 if (!strstr(DRV_NAME, drive->driver_req))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001964 goto failed;
Bartlomiej Zolnierkiewicz2a924662008-10-10 22:39:24 +02001965
Linus Torvalds1da177e2005-04-16 15:20:36 -07001966 if (drive->media != ide_tape)
1967 goto failed;
Bartlomiej Zolnierkiewicz2a924662008-10-10 22:39:24 +02001968
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +02001969 if ((drive->dev_flags & IDE_DFLAG_ID_READ) &&
1970 ide_check_atapi_device(drive, DRV_NAME) == 0) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001971 printk(KERN_ERR "ide-tape: %s: not supported by this version of"
1972 " the driver\n", drive->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001973 goto failed;
1974 }
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001975 tape = kzalloc(sizeof(idetape_tape_t), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001976 if (tape == NULL) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001977 printk(KERN_ERR "ide-tape: %s: Can't allocate a tape struct\n",
1978 drive->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001979 goto failed;
1980 }
1981
1982 g = alloc_disk(1 << PARTN_BITS);
1983 if (!g)
1984 goto out_free_tape;
1985
1986 ide_init_disk(g, drive);
1987
Bartlomiej Zolnierkiewicz8fed4362009-02-25 20:28:24 +01001988 tape->dev.parent = &drive->gendev;
1989 tape->dev.release = ide_tape_release;
1990 dev_set_name(&tape->dev, dev_name(&drive->gendev));
1991
1992 if (device_register(&tape->dev))
1993 goto out_free_disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001994
1995 tape->drive = drive;
1996 tape->driver = &idetape_driver;
1997 tape->disk = g;
1998
1999 g->private_data = &tape->driver;
2000
2001 drive->driver_data = tape;
2002
Arjan van de Vencf8b8972006-03-23 03:00:45 -08002003 mutex_lock(&idetape_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002004 for (minor = 0; idetape_devs[minor]; minor++)
2005 ;
2006 idetape_devs[minor] = tape;
Arjan van de Vencf8b8972006-03-23 03:00:45 -08002007 mutex_unlock(&idetape_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002008
2009 idetape_setup(drive, tape, minor);
2010
Greg Kroah-Hartman3ee074b2008-07-21 20:03:34 -07002011 device_create(idetape_sysfs_class, &drive->gendev,
2012 MKDEV(IDETAPE_MAJOR, minor), NULL, "%s", tape->name);
2013 device_create(idetape_sysfs_class, &drive->gendev,
2014 MKDEV(IDETAPE_MAJOR, minor + 128), NULL,
2015 "n%s", tape->name);
Will Dysond5dee802005-09-16 02:55:07 -07002016
Linus Torvalds1da177e2005-04-16 15:20:36 -07002017 g->fops = &idetape_block_ops;
2018 ide_register_region(g);
2019
2020 return 0;
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02002021
Bartlomiej Zolnierkiewicz8fed4362009-02-25 20:28:24 +01002022out_free_disk:
2023 put_disk(g);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002024out_free_tape:
2025 kfree(tape);
2026failed:
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02002027 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002028}
2029
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002030static void __exit idetape_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002031{
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02002032 driver_unregister(&idetape_driver.gen_driver);
Will Dysond5dee802005-09-16 02:55:07 -07002033 class_destroy(idetape_sysfs_class);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002034 unregister_chrdev(IDETAPE_MAJOR, "ht");
2035}
2036
Bartlomiej Zolnierkiewicz17514e82005-11-19 22:24:35 +01002037static int __init idetape_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002038{
Will Dysond5dee802005-09-16 02:55:07 -07002039 int error = 1;
2040 idetape_sysfs_class = class_create(THIS_MODULE, "ide_tape");
2041 if (IS_ERR(idetape_sysfs_class)) {
2042 idetape_sysfs_class = NULL;
2043 printk(KERN_ERR "Unable to create sysfs class for ide tapes\n");
2044 error = -EBUSY;
2045 goto out;
2046 }
2047
Linus Torvalds1da177e2005-04-16 15:20:36 -07002048 if (register_chrdev(IDETAPE_MAJOR, "ht", &idetape_fops)) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002049 printk(KERN_ERR "ide-tape: Failed to register chrdev"
2050 " interface\n");
Will Dysond5dee802005-09-16 02:55:07 -07002051 error = -EBUSY;
2052 goto out_free_class;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002053 }
Will Dysond5dee802005-09-16 02:55:07 -07002054
2055 error = driver_register(&idetape_driver.gen_driver);
2056 if (error)
2057 goto out_free_driver;
2058
2059 return 0;
2060
2061out_free_driver:
2062 driver_unregister(&idetape_driver.gen_driver);
2063out_free_class:
2064 class_destroy(idetape_sysfs_class);
2065out:
2066 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002067}
2068
Kay Sievers263756e2005-12-12 18:03:44 +01002069MODULE_ALIAS("ide:*m-tape*");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002070module_init(idetape_init);
2071module_exit(idetape_exit);
2072MODULE_ALIAS_CHARDEV_MAJOR(IDETAPE_MAJOR);
Borislav Petkov9c145762008-02-06 02:57:54 +01002073MODULE_DESCRIPTION("ATAPI Streaming TAPE Driver");
2074MODULE_LICENSE("GPL");