blob: 39b0a5c45f07a179a5068d1ef81f0b436cc20d9e [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>
Arnd Bergmann8a6cfeb2010-07-08 10:18:46 +020035#include <linux/smp_lock.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070036#include <linux/slab.h>
37#include <linux/pci.h>
38#include <linux/ide.h>
39#include <linux/smp_lock.h>
40#include <linux/completion.h>
41#include <linux/bitops.h>
Arjan van de Vencf8b8972006-03-23 03:00:45 -080042#include <linux/mutex.h>
Borislav Petkov90699ce2008-02-02 19:56:50 +010043#include <scsi/scsi.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070044
45#include <asm/byteorder.h>
Borislav Petkovc837cfa2008-02-06 02:57:54 +010046#include <linux/irq.h>
47#include <linux/uaccess.h>
48#include <linux/io.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070049#include <asm/unaligned.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070050#include <linux/mtio.h>
51
Borislav Petkov8004a8c2008-02-06 02:57:51 +010052/* define to see debug info */
Borislav Petkove972d702009-07-17 23:55:16 +000053#undef IDETAPE_DEBUG_LOG
Borislav Petkov8004a8c2008-02-06 02:57:51 +010054
Borislav Petkove972d702009-07-17 23:55:16 +000055#ifdef IDETAPE_DEBUG_LOG
56#define ide_debug_log(lvl, fmt, args...) __ide_debug_log(lvl, fmt, ## args)
Borislav Petkov8004a8c2008-02-06 02:57:51 +010057#else
Borislav Petkove972d702009-07-17 23:55:16 +000058#define ide_debug_log(lvl, fmt, args...) do {} while (0)
Borislav Petkov8004a8c2008-02-06 02:57:51 +010059#endif
60
Linus Torvalds1da177e2005-04-16 15:20:36 -070061/**************************** Tunable parameters *****************************/
Linus Torvalds1da177e2005-04-16 15:20:36 -070062/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +010063 * After each failed packet command we issue a request sense command and retry
64 * the packet command IDETAPE_MAX_PC_RETRIES times.
Linus Torvalds1da177e2005-04-16 15:20:36 -070065 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +010066 * Setting IDETAPE_MAX_PC_RETRIES to 0 will disable retries.
Linus Torvalds1da177e2005-04-16 15:20:36 -070067 */
68#define IDETAPE_MAX_PC_RETRIES 3
69
70/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +010071 * The following parameter is used to select the point in the internal tape fifo
72 * in which we will start to refill the buffer. Decreasing the following
73 * parameter will improve the system's latency and interactive response, while
74 * using a high value might improve system throughput.
Linus Torvalds1da177e2005-04-16 15:20:36 -070075 */
Borislav Petkov3c98bf32008-02-06 02:57:53 +010076#define IDETAPE_FIFO_THRESHOLD 2
Linus Torvalds1da177e2005-04-16 15:20:36 -070077
78/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +010079 * DSC polling parameters.
Linus Torvalds1da177e2005-04-16 15:20:36 -070080 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +010081 * Polling for DSC (a single bit in the status register) is a very important
82 * function in ide-tape. There are two cases in which we poll for DSC:
Linus Torvalds1da177e2005-04-16 15:20:36 -070083 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +010084 * 1. Before a read/write packet command, to ensure that we can transfer data
85 * from/to the tape's data buffers, without causing an actual media access.
86 * In case the tape is not ready yet, we take out our request from the device
87 * request queue, so that ide.c could service requests from the other device
88 * on the same interface in the meantime.
Linus Torvalds1da177e2005-04-16 15:20:36 -070089 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +010090 * 2. After the successful initialization of a "media access packet command",
91 * which is a command that can take a long time to complete (the interval can
92 * range from several seconds to even an hour). Again, we postpone our request
93 * in the middle to free the bus for the other device. The polling frequency
94 * here should be lower than the read/write frequency since those media access
95 * commands are slow. We start from a "fast" frequency - IDETAPE_DSC_MA_FAST
96 * (1 second), and if we don't receive DSC after IDETAPE_DSC_MA_THRESHOLD
97 * (5 min), we switch it to a lower frequency - IDETAPE_DSC_MA_SLOW (1 min).
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +010099 * We also set a timeout for the timer, in case something goes wrong. The
100 * timeout should be longer then the maximum execution time of a tape operation.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 */
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100102
103/* DSC timings. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104#define IDETAPE_DSC_RW_MIN 5*HZ/100 /* 50 msec */
105#define IDETAPE_DSC_RW_MAX 40*HZ/100 /* 400 msec */
106#define IDETAPE_DSC_RW_TIMEOUT 2*60*HZ /* 2 minutes */
107#define IDETAPE_DSC_MA_FAST 2*HZ /* 2 seconds */
108#define IDETAPE_DSC_MA_THRESHOLD 5*60*HZ /* 5 minutes */
109#define IDETAPE_DSC_MA_SLOW 30*HZ /* 30 seconds */
110#define IDETAPE_DSC_MA_TIMEOUT 2*60*60*HZ /* 2 hours */
111
112/*************************** End of tunable parameters ***********************/
113
Borislav Petkov54abf372008-02-06 02:57:52 +0100114/* tape directions */
115enum {
116 IDETAPE_DIR_NONE = (1 << 0),
117 IDETAPE_DIR_READ = (1 << 1),
118 IDETAPE_DIR_WRITE = (1 << 2),
119};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120
Borislav Petkov03056b92008-04-18 00:46:26 +0200121/* Tape door status */
122#define DOOR_UNLOCKED 0
123#define DOOR_LOCKED 1
124#define DOOR_EXPLICITLY_LOCKED 2
125
126/* Some defines for the SPACE command */
127#define IDETAPE_SPACE_OVER_FILEMARK 1
128#define IDETAPE_SPACE_TO_EOD 3
129
130/* Some defines for the LOAD UNLOAD command */
131#define IDETAPE_LU_LOAD_MASK 1
132#define IDETAPE_LU_RETENSION_MASK 2
133#define IDETAPE_LU_EOT_MASK 4
134
Borislav Petkov03056b92008-04-18 00:46:26 +0200135/* Structures related to the SELECT SENSE / MODE SENSE packet commands. */
136#define IDETAPE_BLOCK_DESCRIPTOR 0
137#define IDETAPE_CAPABILITIES_PAGE 0x2a
138
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100140 * Most of our global data which we need to save even as we leave the driver due
141 * to an interrupt or a timer event is stored in the struct defined below.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 */
143typedef struct ide_tape_obj {
Bartlomiej Zolnierkiewicz7f3c8682009-01-06 17:20:53 +0100144 ide_drive_t *drive;
145 struct ide_driver *driver;
146 struct gendisk *disk;
Bartlomiej Zolnierkiewicz8fed4362009-02-25 20:28:24 +0100147 struct device dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148
Bartlomiej Zolnierkiewicz2e8a6f82008-10-10 22:39:36 +0200149 /* used by REQ_IDETAPE_{READ,WRITE} requests */
150 struct ide_atapi_pc queued_pc;
Bartlomiej Zolnierkiewicz394a4c22008-10-10 22:39:35 +0200151
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 /*
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100153 * DSC polling variables.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100155 * While polling for DSC we use postponed_rq to postpone the current
156 * request so that ide.c will be able to service pending requests on the
157 * other device. Note that at most we will have only one DSC (usually
Borislav Petkov5bd50dc2008-04-27 15:38:28 +0200158 * data transfer) request in the device request queue.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 */
Borislav Petkov6f3848a2009-07-21 23:08:23 -0700160 bool postponed_rq;
161
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 /* The time in which we started polling for DSC */
163 unsigned long dsc_polling_start;
164 /* Timer used to poll for dsc */
165 struct timer_list dsc_timer;
166 /* Read/Write dsc polling frequency */
Borislav Petkov54bb2072008-02-06 02:57:52 +0100167 unsigned long best_dsc_rw_freq;
168 unsigned long dsc_poll_freq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 unsigned long dsc_timeout;
170
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100171 /* Read position information */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 u8 partition;
173 /* Current block */
Borislav Petkov54bb2072008-02-06 02:57:52 +0100174 unsigned int first_frame;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100176 /* Last error information */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 u8 sense_key, asc, ascq;
178
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100179 /* Character device operation */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 unsigned int minor;
181 /* device name */
182 char name[4];
183 /* Current character device data transfer direction */
Borislav Petkov54abf372008-02-06 02:57:52 +0100184 u8 chrdev_dir;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185
Borislav Petkov54bb2072008-02-06 02:57:52 +0100186 /* tape block size, usually 512 or 1024 bytes */
187 unsigned short blk_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 int user_bs_factor;
Borislav Petkovb6422012008-02-02 19:56:49 +0100189
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 /* Copy of the tape's Capabilities and Mechanical Page */
Borislav Petkovb6422012008-02-02 19:56:49 +0100191 u8 caps[20];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192
193 /*
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100194 * Active data transfer request parameters.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100196 * At most, there is only one ide-tape originated data transfer request
197 * in the device request queue. This allows ide.c to easily service
198 * requests from the other device when we postpone our active request.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 */
Borislav Petkov83042b22008-04-27 15:38:27 +0200200
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100201 /* Data buffer size chosen based on the tape's recommendation */
Borislav Petkovf73850a2008-04-27 15:38:33 +0200202 int buffer_size;
Tejun Heo963da552009-04-19 08:46:02 +0900203 /* Staging buffer of buffer_size bytes */
204 void *buf;
205 /* The read/write cursor */
206 void *cur;
207 /* The number of valid bytes in buf */
208 size_t valid;
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100209
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100210 /* Measures average tape speed */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 unsigned long avg_time;
212 int avg_size;
213 int avg_speed;
214
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 /* the door is currently locked */
216 int door_locked;
217 /* the tape hardware is write protected */
218 char drv_write_prot;
219 /* the tape is write protected (hardware or opened as read-only) */
220 char write_prot;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221} idetape_tape_t;
222
Arjan van de Vencf8b8972006-03-23 03:00:45 -0800223static DEFINE_MUTEX(idetape_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224
Borislav Petkovcbba2fa2009-10-11 00:25:19 +0000225static DEFINE_MUTEX(idetape_chrdev_mutex);
226
Will Dysond5dee802005-09-16 02:55:07 -0700227static struct class *idetape_sysfs_class;
228
Bartlomiej Zolnierkiewicz8fed4362009-02-25 20:28:24 +0100229static void ide_tape_release(struct device *);
Bartlomiej Zolnierkiewicz08da5912008-07-24 22:53:15 +0200230
Borislav Petkov9d01e4c2009-06-08 22:03:03 +0200231static struct ide_tape_obj *idetape_devs[MAX_HWIFS * MAX_DRIVES];
232
233static struct ide_tape_obj *ide_tape_get(struct gendisk *disk, bool cdev,
234 unsigned int i)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235{
236 struct ide_tape_obj *tape = NULL;
237
Arjan van de Vencf8b8972006-03-23 03:00:45 -0800238 mutex_lock(&idetape_ref_mutex);
Borislav Petkov9d01e4c2009-06-08 22:03:03 +0200239
240 if (cdev)
241 tape = idetape_devs[i];
242 else
243 tape = ide_drv_g(disk, ide_tape_obj);
244
Bartlomiej Zolnierkiewicz08da5912008-07-24 22:53:15 +0200245 if (tape) {
Bartlomiej Zolnierkiewiczd3e33ff2008-08-05 18:16:59 +0200246 if (ide_device_get(tape->drive))
Bartlomiej Zolnierkiewicz08da5912008-07-24 22:53:15 +0200247 tape = NULL;
Bartlomiej Zolnierkiewiczd3e33ff2008-08-05 18:16:59 +0200248 else
Bartlomiej Zolnierkiewicz8fed4362009-02-25 20:28:24 +0100249 get_device(&tape->dev);
Bartlomiej Zolnierkiewicz08da5912008-07-24 22:53:15 +0200250 }
Borislav Petkov9d01e4c2009-06-08 22:03:03 +0200251
Arjan van de Vencf8b8972006-03-23 03:00:45 -0800252 mutex_unlock(&idetape_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 return tape;
254}
255
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256static void ide_tape_put(struct ide_tape_obj *tape)
257{
Bartlomiej Zolnierkiewiczd3e33ff2008-08-05 18:16:59 +0200258 ide_drive_t *drive = tape->drive;
259
Arjan van de Vencf8b8972006-03-23 03:00:45 -0800260 mutex_lock(&idetape_ref_mutex);
Bartlomiej Zolnierkiewicz8fed4362009-02-25 20:28:24 +0100261 put_device(&tape->dev);
Bartlomiej Zolnierkiewiczd3e33ff2008-08-05 18:16:59 +0200262 ide_device_put(drive);
Arjan van de Vencf8b8972006-03-23 03:00:45 -0800263 mutex_unlock(&idetape_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264}
265
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266/*
Borislav Petkov1b5db432008-02-02 19:56:48 +0100267 * called on each failed packet command retry to analyze the request sense. We
268 * currently do not utilize this information.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 */
Borislav Petkovae3a8382009-05-02 10:58:17 +0200270static void idetape_analyze_error(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271{
272 idetape_tape_t *tape = drive->driver_data;
Bartlomiej Zolnierkiewicz5e2040fd2009-03-27 12:46:34 +0100273 struct ide_atapi_pc *pc = drive->failed_pc;
Borislav Petkovdfb7e622009-05-01 20:35:21 +0200274 struct request *rq = drive->hwif->rq;
Borislav Petkovae3a8382009-05-02 10:58:17 +0200275 u8 *sense = bio_data(rq->bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276
Borislav Petkov1b5db432008-02-02 19:56:48 +0100277 tape->sense_key = sense[2] & 0xF;
278 tape->asc = sense[12];
279 tape->ascq = sense[13];
Borislav Petkov8004a8c2008-02-06 02:57:51 +0100280
Borislav Petkove972d702009-07-17 23:55:16 +0000281 ide_debug_log(IDE_DBG_FUNC,
282 "cmd: 0x%x, sense key = %x, asc = %x, ascq = %x",
283 rq->cmd[0], tape->sense_key, tape->asc, tape->ascq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284
Borislav Petkov077e6db2009-05-01 21:21:02 +0200285 /* correct remaining bytes to transfer */
Tejun Heo21d9c5d2009-04-19 08:46:02 +0900286 if (pc->flags & PC_FLAG_DMA_ERROR)
Borislav Petkov077e6db2009-05-01 21:21:02 +0200287 rq->resid_len = tape->blk_size * get_unaligned_be32(&sense[3]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288
289 /*
290 * If error was the result of a zero-length read or write command,
291 * with sense key=5, asc=0x22, ascq=0, let it slide. Some drives
292 * (i.e. Seagate STT3401A Travan) don't support 0-length read/writes.
293 */
Borislav Petkov90699ce2008-02-02 19:56:50 +0100294 if ((pc->c[0] == READ_6 || pc->c[0] == WRITE_6)
Borislav Petkov1b5db432008-02-02 19:56:48 +0100295 /* length == 0 */
296 && pc->c[4] == 0 && pc->c[3] == 0 && pc->c[2] == 0) {
297 if (tape->sense_key == 5) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 /* don't report an error, everything's ok */
299 pc->error = 0;
300 /* don't retry read/write */
Borislav Petkov346331f2008-04-18 00:46:26 +0200301 pc->flags |= PC_FLAG_ABORT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 }
303 }
Borislav Petkov90699ce2008-02-02 19:56:50 +0100304 if (pc->c[0] == READ_6 && (sense[2] & 0x80)) {
Bartlomiej Zolnierkiewiczc152cc12009-03-27 12:46:34 +0100305 pc->error = IDE_DRV_ERROR_FILEMARK;
Borislav Petkov346331f2008-04-18 00:46:26 +0200306 pc->flags |= PC_FLAG_ABORT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 }
Borislav Petkov90699ce2008-02-02 19:56:50 +0100308 if (pc->c[0] == WRITE_6) {
Borislav Petkov1b5db432008-02-02 19:56:48 +0100309 if ((sense[2] & 0x40) || (tape->sense_key == 0xd
310 && tape->asc == 0x0 && tape->ascq == 0x2)) {
Bartlomiej Zolnierkiewiczc152cc12009-03-27 12:46:34 +0100311 pc->error = IDE_DRV_ERROR_EOD;
Borislav Petkov346331f2008-04-18 00:46:26 +0200312 pc->flags |= PC_FLAG_ABORT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 }
314 }
Borislav Petkov90699ce2008-02-02 19:56:50 +0100315 if (pc->c[0] == READ_6 || pc->c[0] == WRITE_6) {
Borislav Petkov1b5db432008-02-02 19:56:48 +0100316 if (tape->sense_key == 8) {
Bartlomiej Zolnierkiewiczc152cc12009-03-27 12:46:34 +0100317 pc->error = IDE_DRV_ERROR_EOD;
Borislav Petkov346331f2008-04-18 00:46:26 +0200318 pc->flags |= PC_FLAG_ABORT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 }
Borislav Petkov346331f2008-04-18 00:46:26 +0200320 if (!(pc->flags & PC_FLAG_ABORT) &&
Borislav Petkov077e6db2009-05-01 21:21:02 +0200321 (blk_rq_bytes(rq) - rq->resid_len))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 pc->retries = IDETAPE_MAX_PC_RETRIES + 1;
323 }
324}
325
Bartlomiej Zolnierkiewiczb14c7212008-10-13 21:39:30 +0200326static void ide_tape_handle_dsc(ide_drive_t *);
327
Bartlomiej Zolnierkiewicz03a2faa2009-03-27 12:46:36 +0100328static int ide_tape_callback(ide_drive_t *drive, int dsc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329{
330 idetape_tape_t *tape = drive->driver_data;
Bartlomiej Zolnierkiewicz2b9efba2008-10-13 21:39:31 +0200331 struct ide_atapi_pc *pc = drive->pc;
Bartlomiej Zolnierkiewicz313afea2009-03-27 12:46:34 +0100332 struct request *rq = drive->hwif->rq;
Bartlomiej Zolnierkiewicz5985e6a2008-07-15 21:21:55 +0200333 int uptodate = pc->error ? 0 : 1;
Bartlomiej Zolnierkiewicz313afea2009-03-27 12:46:34 +0100334 int err = uptodate ? 0 : IDE_DRV_ERROR_GENERAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335
Borislav Petkove972d702009-07-17 23:55:16 +0000336 ide_debug_log(IDE_DBG_FUNC, "cmd: 0x%x, dsc: %d, err: %d", rq->cmd[0],
337 dsc, err);
Borislav Petkov8004a8c2008-02-06 02:57:51 +0100338
Bartlomiej Zolnierkiewiczb14c7212008-10-13 21:39:30 +0200339 if (dsc)
340 ide_tape_handle_dsc(drive);
341
Bartlomiej Zolnierkiewicz5e2040fd2009-03-27 12:46:34 +0100342 if (drive->failed_pc == pc)
343 drive->failed_pc = NULL;
Bartlomiej Zolnierkiewiczdd2e9a02008-07-15 21:22:01 +0200344
Bartlomiej Zolnierkiewicz5985e6a2008-07-15 21:21:55 +0200345 if (pc->c[0] == REQUEST_SENSE) {
346 if (uptodate)
Borislav Petkovae3a8382009-05-02 10:58:17 +0200347 idetape_analyze_error(drive);
Bartlomiej Zolnierkiewicz5985e6a2008-07-15 21:21:55 +0200348 else
349 printk(KERN_ERR "ide-tape: Error in REQUEST SENSE "
350 "itself - Aborting request!\n");
351 } else if (pc->c[0] == READ_6 || pc->c[0] == WRITE_6) {
Borislav Petkov077e6db2009-05-01 21:21:02 +0200352 unsigned int blocks =
353 (blk_rq_bytes(rq) - rq->resid_len) / tape->blk_size;
Bartlomiej Zolnierkiewicz5985e6a2008-07-15 21:21:55 +0200354
355 tape->avg_size += blocks * tape->blk_size;
356
357 if (time_after_eq(jiffies, tape->avg_time + HZ)) {
358 tape->avg_speed = tape->avg_size * HZ /
359 (jiffies - tape->avg_time) / 1024;
360 tape->avg_size = 0;
361 tape->avg_time = jiffies;
362 }
363
364 tape->first_frame += blocks;
Bartlomiej Zolnierkiewicz5985e6a2008-07-15 21:21:55 +0200365
Bartlomiej Zolnierkiewicz313afea2009-03-27 12:46:34 +0100366 if (pc->error) {
367 uptodate = 0;
368 err = pc->error;
369 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 }
Bartlomiej Zolnierkiewicz313afea2009-03-27 12:46:34 +0100371 rq->errors = err;
372
Bartlomiej Zolnierkiewicz03a2faa2009-03-27 12:46:36 +0100373 return uptodate;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374}
375
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100377 * Postpone the current request so that ide.c will be able to service requests
Bartlomiej Zolnierkiewiczb65fac32009-01-06 17:20:50 +0100378 * from another device on the same port while we are polling for DSC.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 */
Borislav Petkov6f3848a2009-07-21 23:08:23 -0700380static void ide_tape_stall_queue(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381{
382 idetape_tape_t *tape = drive->driver_data;
383
Borislav Petkove972d702009-07-17 23:55:16 +0000384 ide_debug_log(IDE_DBG_FUNC, "cmd: 0x%x, dsc_poll_freq: %lu",
Borislav Petkov6f3848a2009-07-21 23:08:23 -0700385 drive->hwif->rq->cmd[0], tape->dsc_poll_freq);
Borislav Petkov8004a8c2008-02-06 02:57:51 +0100386
Borislav Petkov6f3848a2009-07-21 23:08:23 -0700387 tape->postponed_rq = true;
Bartlomiej Zolnierkiewiczb65fac32009-01-06 17:20:50 +0100388
Borislav Petkov54bb2072008-02-06 02:57:52 +0100389 ide_stall_queue(drive, tape->dsc_poll_freq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390}
391
Bartlomiej Zolnierkiewicz74e63e742008-07-15 21:22:01 +0200392static void ide_tape_handle_dsc(ide_drive_t *drive)
393{
394 idetape_tape_t *tape = drive->driver_data;
395
396 /* Media access command */
397 tape->dsc_polling_start = jiffies;
398 tape->dsc_poll_freq = IDETAPE_DSC_MA_FAST;
399 tape->dsc_timeout = jiffies + IDETAPE_DSC_MA_TIMEOUT;
400 /* Allow ide.c to handle other requests */
Borislav Petkov6f3848a2009-07-21 23:08:23 -0700401 ide_tape_stall_queue(drive);
Bartlomiej Zolnierkiewicz74e63e742008-07-15 21:22:01 +0200402}
403
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100405 * Packet Command Interface
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 *
Bartlomiej Zolnierkiewicz2b9efba2008-10-13 21:39:31 +0200407 * The current Packet Command is available in drive->pc, and will not change
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100408 * until we finish handling it. Each packet command is associated with a
409 * callback function that will be called when the command is finished.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100411 * The handling will be done in three stages:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 *
Bartlomiej Zolnierkiewiczb788ee92009-03-27 12:46:46 +0100413 * 1. ide_tape_issue_pc will send the packet command to the drive, and will set
Bartlomiej Zolnierkiewiczaa5d2de72008-10-13 21:39:32 +0200414 * the interrupt handler to ide_pc_intr.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 *
Bartlomiej Zolnierkiewiczaa5d2de72008-10-13 21:39:32 +0200416 * 2. On each interrupt, ide_pc_intr will be called. This step will be
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100417 * repeated until the device signals us that no more interrupts will be issued.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100419 * 3. ATAPI Tape media access commands have immediate status with a delayed
420 * process. In case of a successful initiation of a media access packet command,
421 * the DSC bit will be set when the actual execution of the command is finished.
422 * Since the tape drive will not issue an interrupt, we have to poll for this
423 * event. In this case, we define the request as "low priority request" by
424 * setting rq_status to IDETAPE_RQ_POSTPONED, set a timer to poll for DSC and
425 * exit the driver.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100427 * ide.c will then give higher priority to requests which originate from the
428 * other device, until will change rq_status to RQ_ACTIVE.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100430 * 4. When the packet command is finished, it will be checked for errors.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100432 * 5. In case an error was found, we queue a request sense packet command in
433 * front of the request queue and retry the operation up to
434 * IDETAPE_MAX_PC_RETRIES times.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100436 * 6. In case no error was found, or we decided to give up and not to retry
437 * again, the callback function will be called and then we will handle the next
438 * request.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440
Bartlomiej Zolnierkiewiczb788ee92009-03-27 12:46:46 +0100441static ide_startstop_t ide_tape_issue_pc(ide_drive_t *drive,
442 struct ide_cmd *cmd,
443 struct ide_atapi_pc *pc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 idetape_tape_t *tape = drive->driver_data;
Borislav Petkovdfb7e622009-05-01 20:35:21 +0200446 struct request *rq = drive->hwif->rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447
Bartlomiej Zolnierkiewicz5e2040fd2009-03-27 12:46:34 +0100448 if (drive->failed_pc == NULL && pc->c[0] != REQUEST_SENSE)
449 drive->failed_pc = pc;
Bartlomiej Zolnierkiewicz2b9efba2008-10-13 21:39:31 +0200450
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 /* Set the current packet command */
Bartlomiej Zolnierkiewicz2b9efba2008-10-13 21:39:31 +0200452 drive->pc = pc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453
454 if (pc->retries > IDETAPE_MAX_PC_RETRIES ||
Borislav Petkov346331f2008-04-18 00:46:26 +0200455 (pc->flags & PC_FLAG_ABORT)) {
Tejun Heob3071d12009-04-19 08:46:02 +0900456
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 /*
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100458 * We will "abort" retrying a packet command in case legitimate
459 * error code was received (crossing a filemark, or end of the
460 * media, for example).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 */
Borislav Petkov346331f2008-04-18 00:46:26 +0200462 if (!(pc->flags & PC_FLAG_ABORT)) {
Borislav Petkov90699ce2008-02-02 19:56:50 +0100463 if (!(pc->c[0] == TEST_UNIT_READY &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 tape->sense_key == 2 && tape->asc == 4 &&
465 (tape->ascq == 1 || tape->ascq == 8))) {
466 printk(KERN_ERR "ide-tape: %s: I/O error, "
467 "pc = %2x, key = %2x, "
468 "asc = %2x, ascq = %2x\n",
469 tape->name, pc->c[0],
470 tape->sense_key, tape->asc,
471 tape->ascq);
472 }
473 /* Giving up */
Bartlomiej Zolnierkiewiczc152cc12009-03-27 12:46:34 +0100474 pc->error = IDE_DRV_ERROR_GENERAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 }
Tejun Heob3071d12009-04-19 08:46:02 +0900476
Bartlomiej Zolnierkiewicz5e2040fd2009-03-27 12:46:34 +0100477 drive->failed_pc = NULL;
Bartlomiej Zolnierkiewiczb14c7212008-10-13 21:39:30 +0200478 drive->pc_callback(drive, 0);
Borislav Petkovdfb7e622009-05-01 20:35:21 +0200479 ide_complete_rq(drive, -EIO, blk_rq_bytes(rq));
Bartlomiej Zolnierkiewicz92f5daf2008-07-15 21:21:55 +0200480 return ide_stopped;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 }
Borislav Petkove972d702009-07-17 23:55:16 +0000482 ide_debug_log(IDE_DBG_SENSE, "retry #%d, cmd: 0x%02x", pc->retries,
483 pc->c[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484
485 pc->retries++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486
Bartlomiej Zolnierkiewiczb788ee92009-03-27 12:46:46 +0100487 return ide_issue_pc(drive, cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488}
489
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100490/* A mode sense command is used to "sense" tape parameters. */
Borislav Petkovd236d742008-04-18 00:46:27 +0200491static void idetape_create_mode_sense_cmd(struct ide_atapi_pc *pc, u8 page_code)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492{
Bartlomiej Zolnierkiewicz7bf74202008-10-10 22:39:37 +0200493 ide_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +0100494 pc->c[0] = MODE_SENSE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 if (page_code != IDETAPE_BLOCK_DESCRIPTOR)
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100496 /* DBD = 1 - Don't return block descriptors */
497 pc->c[1] = 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 pc->c[2] = page_code;
499 /*
500 * Changed pc->c[3] to 0 (255 will at best return unused info).
501 *
502 * For SCSI this byte is defined as subpage instead of high byte
503 * of length and some IDE drives seem to interpret it this way
504 * and return an error when 255 is used.
505 */
506 pc->c[3] = 0;
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100507 /* We will just discard data in that case */
508 pc->c[4] = 255;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 if (page_code == IDETAPE_BLOCK_DESCRIPTOR)
Borislav Petkovd236d742008-04-18 00:46:27 +0200510 pc->req_xfer = 12;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 else if (page_code == IDETAPE_CAPABILITIES_PAGE)
Borislav Petkovd236d742008-04-18 00:46:27 +0200512 pc->req_xfer = 24;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 else
Borislav Petkovd236d742008-04-18 00:46:27 +0200514 pc->req_xfer = 50;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515}
516
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100517static ide_startstop_t idetape_media_access_finished(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518{
Bartlomiej Zolnierkiewiczb73c7ee2008-07-23 19:55:52 +0200519 ide_hwif_t *hwif = drive->hwif;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 idetape_tape_t *tape = drive->driver_data;
Bartlomiej Zolnierkiewicz2b9efba2008-10-13 21:39:31 +0200521 struct ide_atapi_pc *pc = drive->pc;
Bartlomiej Zolnierkiewicz22c525b2008-01-25 22:17:11 +0100522 u8 stat;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523
Bartlomiej Zolnierkiewicz374e0422008-07-23 19:55:56 +0200524 stat = hwif->tp_ops->read_status(hwif);
Bartlomiej Zolnierkiewiczc47137a2008-02-06 02:57:51 +0100525
Bartlomiej Zolnierkiewicz3a7d2482008-10-10 22:39:21 +0200526 if (stat & ATA_DSC) {
527 if (stat & ATA_ERR) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 /* Error detected */
Borislav Petkov90699ce2008-02-02 19:56:50 +0100529 if (pc->c[0] != TEST_UNIT_READY)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 printk(KERN_ERR "ide-tape: %s: I/O error, ",
531 tape->name);
532 /* Retry operation */
Borislav Petkov6b544fc2009-04-19 07:00:42 +0900533 ide_retry_pc(drive);
Bartlomiej Zolnierkiewicz258ec412008-07-15 21:21:55 +0200534 return ide_stopped;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 }
536 pc->error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 } else {
Bartlomiej Zolnierkiewiczc152cc12009-03-27 12:46:34 +0100538 pc->error = IDE_DRV_ERROR_GENERAL;
Bartlomiej Zolnierkiewicz5e2040fd2009-03-27 12:46:34 +0100539 drive->failed_pc = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 }
Bartlomiej Zolnierkiewiczb14c7212008-10-13 21:39:30 +0200541 drive->pc_callback(drive, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 return ide_stopped;
543}
544
Borislav Petkovcd2abbf2008-07-15 21:22:03 +0200545static void ide_tape_create_rw_cmd(idetape_tape_t *tape,
Borislav Petkov0014c752008-07-23 19:56:00 +0200546 struct ide_atapi_pc *pc, struct request *rq,
547 u8 opcode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548{
Borislav Petkov10c0b342009-05-01 20:29:53 +0200549 unsigned int length = blk_rq_sectors(rq) / (tape->blk_size >> 9);
Borislav Petkov0014c752008-07-23 19:56:00 +0200550
Bartlomiej Zolnierkiewicz7bf74202008-10-10 22:39:37 +0200551 ide_init_pc(pc);
Borislav Petkov860ff5e2008-02-02 19:56:50 +0100552 put_unaligned(cpu_to_be32(length), (unsigned int *) &pc->c[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 pc->c[1] = 1;
Borislav Petkov19f52a72009-05-04 09:53:03 +0200554
555 if (blk_rq_bytes(rq) == tape->buffer_size)
Bartlomiej Zolnierkiewicz5e331092008-07-15 21:21:56 +0200556 pc->flags |= PC_FLAG_DMA_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557
Tejun Heo963da552009-04-19 08:46:02 +0900558 if (opcode == READ_6)
Borislav Petkovcd2abbf2008-07-15 21:22:03 +0200559 pc->c[0] = READ_6;
Tejun Heo963da552009-04-19 08:46:02 +0900560 else if (opcode == WRITE_6) {
Borislav Petkovcd2abbf2008-07-15 21:22:03 +0200561 pc->c[0] = WRITE_6;
562 pc->flags |= PC_FLAG_WRITING;
Borislav Petkovcd2abbf2008-07-15 21:22:03 +0200563 }
Borislav Petkov0014c752008-07-23 19:56:00 +0200564
565 memcpy(rq->cmd, pc->c, 12);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566}
567
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568static ide_startstop_t idetape_do_request(ide_drive_t *drive,
569 struct request *rq, sector_t block)
570{
Bartlomiej Zolnierkiewiczb73c7ee2008-07-23 19:55:52 +0200571 ide_hwif_t *hwif = drive->hwif;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 idetape_tape_t *tape = drive->driver_data;
Borislav Petkovd236d742008-04-18 00:46:27 +0200573 struct ide_atapi_pc *pc = NULL;
Bartlomiej Zolnierkiewiczb788ee92009-03-27 12:46:46 +0100574 struct ide_cmd cmd;
Bartlomiej Zolnierkiewicz22c525b2008-01-25 22:17:11 +0100575 u8 stat;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576
Borislav Petkove972d702009-07-17 23:55:16 +0000577 ide_debug_log(IDE_DBG_RQ, "cmd: 0x%x, sector: %llu, nr_sectors: %u",
578 rq->cmd[0], (unsigned long long)blk_rq_pos(rq),
579 blk_rq_sectors(rq));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580
Christoph Hellwig33659eb2010-08-07 18:17:56 +0200581 BUG_ON(!(rq->cmd_type == REQ_TYPE_SPECIAL ||
582 rq->cmd_type == REQ_TYPE_SENSE));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100584 /* Retry a failed packet command */
Bartlomiej Zolnierkiewicz5e2040fd2009-03-27 12:46:34 +0100585 if (drive->failed_pc && drive->pc->c[0] == REQUEST_SENSE) {
586 pc = drive->failed_pc;
Bartlomiej Zolnierkiewicz28c72142008-07-15 21:21:59 +0200587 goto out;
588 }
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100589
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 /*
591 * If the tape is still busy, postpone our request and service
592 * the other device meanwhile.
593 */
Bartlomiej Zolnierkiewicz374e0422008-07-23 19:55:56 +0200594 stat = hwif->tp_ops->read_status(hwif);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +0200596 if ((drive->dev_flags & IDE_DFLAG_DSC_OVERLAP) == 0 &&
597 (rq->cmd[13] & REQ_IDETAPE_PC2) == 0)
Borislav Petkov626542c2009-06-07 15:37:05 +0200598 drive->atapi_flags |= IDE_AFLAG_IGNORE_DSC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +0200600 if (drive->dev_flags & IDE_DFLAG_POST_RESET) {
Borislav Petkov626542c2009-06-07 15:37:05 +0200601 drive->atapi_flags |= IDE_AFLAG_IGNORE_DSC;
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +0200602 drive->dev_flags &= ~IDE_DFLAG_POST_RESET;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 }
604
Borislav Petkov626542c2009-06-07 15:37:05 +0200605 if (!(drive->atapi_flags & IDE_AFLAG_IGNORE_DSC) &&
606 !(stat & ATA_DSC)) {
Borislav Petkov6f3848a2009-07-21 23:08:23 -0700607 if (!tape->postponed_rq) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608 tape->dsc_polling_start = jiffies;
Borislav Petkov54bb2072008-02-06 02:57:52 +0100609 tape->dsc_poll_freq = tape->best_dsc_rw_freq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 tape->dsc_timeout = jiffies + IDETAPE_DSC_RW_TIMEOUT;
611 } else if (time_after(jiffies, tape->dsc_timeout)) {
612 printk(KERN_ERR "ide-tape: %s: DSC timeout\n",
613 tape->name);
Borislav Petkov83dd5732008-07-23 19:56:00 +0200614 if (rq->cmd[13] & REQ_IDETAPE_PC2) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 idetape_media_access_finished(drive);
616 return ide_stopped;
617 } else {
618 return ide_do_reset(drive);
619 }
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100620 } else if (time_after(jiffies,
621 tape->dsc_polling_start +
622 IDETAPE_DSC_MA_THRESHOLD))
Borislav Petkov54bb2072008-02-06 02:57:52 +0100623 tape->dsc_poll_freq = IDETAPE_DSC_MA_SLOW;
Borislav Petkov6f3848a2009-07-21 23:08:23 -0700624 ide_tape_stall_queue(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 return ide_stopped;
Borislav Petkov6f3848a2009-07-21 23:08:23 -0700626 } else {
Borislav Petkov626542c2009-06-07 15:37:05 +0200627 drive->atapi_flags &= ~IDE_AFLAG_IGNORE_DSC;
Borislav Petkov6f3848a2009-07-21 23:08:23 -0700628 tape->postponed_rq = false;
629 }
Borislav Petkov626542c2009-06-07 15:37:05 +0200630
Borislav Petkov83dd5732008-07-23 19:56:00 +0200631 if (rq->cmd[13] & REQ_IDETAPE_READ) {
Bartlomiej Zolnierkiewicz2e8a6f82008-10-10 22:39:36 +0200632 pc = &tape->queued_pc;
Borislav Petkov0014c752008-07-23 19:56:00 +0200633 ide_tape_create_rw_cmd(tape, pc, rq, READ_6);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634 goto out;
635 }
Borislav Petkov83dd5732008-07-23 19:56:00 +0200636 if (rq->cmd[13] & REQ_IDETAPE_WRITE) {
Bartlomiej Zolnierkiewicz2e8a6f82008-10-10 22:39:36 +0200637 pc = &tape->queued_pc;
Borislav Petkov0014c752008-07-23 19:56:00 +0200638 ide_tape_create_rw_cmd(tape, pc, rq, WRITE_6);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639 goto out;
640 }
Borislav Petkov83dd5732008-07-23 19:56:00 +0200641 if (rq->cmd[13] & REQ_IDETAPE_PC1) {
Tejun Heoc267cc12009-04-19 07:00:42 +0900642 pc = (struct ide_atapi_pc *)rq->special;
Borislav Petkov83dd5732008-07-23 19:56:00 +0200643 rq->cmd[13] &= ~(REQ_IDETAPE_PC1);
644 rq->cmd[13] |= REQ_IDETAPE_PC2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645 goto out;
646 }
Borislav Petkov83dd5732008-07-23 19:56:00 +0200647 if (rq->cmd[13] & REQ_IDETAPE_PC2) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648 idetape_media_access_finished(drive);
649 return ide_stopped;
650 }
651 BUG();
Bartlomiej Zolnierkiewicz28c72142008-07-15 21:21:59 +0200652
Borislav Petkovf2e3ab52008-07-23 19:56:01 +0200653out:
Borislav Petkov6b544fc2009-04-19 07:00:42 +0900654 /* prepare sense request for this command */
655 ide_prep_sense(drive, rq);
656
Bartlomiej Zolnierkiewiczb788ee92009-03-27 12:46:46 +0100657 memset(&cmd, 0, sizeof(cmd));
658
659 if (rq_data_dir(rq))
660 cmd.tf_flags |= IDE_TFLAG_WRITE;
661
662 cmd.rq = rq;
663
Borislav Petkovdfb7e622009-05-01 20:35:21 +0200664 ide_init_sg_cmd(&cmd, blk_rq_bytes(rq));
Tejun Heo5c4be572009-04-19 07:00:42 +0900665 ide_map_sg(drive, &cmd);
666
Bartlomiej Zolnierkiewiczb788ee92009-03-27 12:46:46 +0100667 return ide_tape_issue_pc(drive, &cmd, pc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668}
669
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100671 * Write a filemark if write_filemark=1. Flush the device buffers without
672 * writing a filemark otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673 */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100674static void idetape_create_write_filemark_cmd(ide_drive_t *drive,
Borislav Petkovd236d742008-04-18 00:46:27 +0200675 struct ide_atapi_pc *pc, int write_filemark)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676{
Bartlomiej Zolnierkiewicz7bf74202008-10-10 22:39:37 +0200677 ide_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +0100678 pc->c[0] = WRITE_FILEMARKS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679 pc->c[4] = write_filemark;
Borislav Petkov346331f2008-04-18 00:46:26 +0200680 pc->flags |= PC_FLAG_WAIT_FOR_DSC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681}
682
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683static int idetape_wait_ready(ide_drive_t *drive, unsigned long timeout)
684{
685 idetape_tape_t *tape = drive->driver_data;
Bartlomiej Zolnierkiewicz2ac07d92008-10-10 22:39:38 +0200686 struct gendisk *disk = tape->disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 int load_attempted = 0;
688
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100689 /* Wait for the tape to become ready */
Borislav Petkov49d80782009-06-07 15:37:06 +0200690 set_bit(ilog2(IDE_AFLAG_MEDIUM_PRESENT), &drive->atapi_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 timeout += jiffies;
692 while (time_before(jiffies, timeout)) {
Bartlomiej Zolnierkiewiczde699ad2008-10-10 22:39:39 +0200693 if (ide_do_test_unit_ready(drive, disk) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 return 0;
695 if ((tape->sense_key == 2 && tape->asc == 4 && tape->ascq == 2)
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100696 || (tape->asc == 0x3A)) {
697 /* no media */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 if (load_attempted)
699 return -ENOMEDIUM;
Bartlomiej Zolnierkiewicz0c8a6c72008-10-10 22:39:39 +0200700 ide_do_start_stop(drive, disk, IDETAPE_LU_LOAD_MASK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 load_attempted = 1;
702 /* not about to be ready */
703 } else if (!(tape->sense_key == 2 && tape->asc == 4 &&
704 (tape->ascq == 1 || tape->ascq == 8)))
705 return -EIO;
Nishanth Aravamudan80ce45f2005-09-10 00:27:08 -0700706 msleep(100);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707 }
708 return -EIO;
709}
710
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100711static int idetape_flush_tape_buffers(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712{
Bartlomiej Zolnierkiewicz2ac07d92008-10-10 22:39:38 +0200713 struct ide_tape_obj *tape = drive->driver_data;
Borislav Petkovd236d742008-04-18 00:46:27 +0200714 struct ide_atapi_pc pc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 int rc;
716
717 idetape_create_write_filemark_cmd(drive, &pc, 0);
Borislav Petkovb13345f2009-05-02 10:26:12 +0200718 rc = ide_queue_pc_tail(drive, tape->disk, &pc, NULL, 0);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100719 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 return rc;
721 idetape_wait_ready(drive, 60 * 5 * HZ);
722 return 0;
723}
724
Borislav Petkov55ce3a12009-05-04 09:38:15 +0200725static int ide_tape_read_position(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726{
727 idetape_tape_t *tape = drive->driver_data;
Borislav Petkovd236d742008-04-18 00:46:27 +0200728 struct ide_atapi_pc pc;
Borislav Petkov55ce3a12009-05-04 09:38:15 +0200729 u8 buf[20];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730
Borislav Petkove972d702009-07-17 23:55:16 +0000731 ide_debug_log(IDE_DBG_FUNC, "enter");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732
Borislav Petkov55ce3a12009-05-04 09:38:15 +0200733 /* prep cmd */
734 ide_init_pc(&pc);
735 pc.c[0] = READ_POSITION;
736 pc.req_xfer = 20;
737
738 if (ide_queue_pc_tail(drive, tape->disk, &pc, buf, pc.req_xfer))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 return -1;
Borislav Petkov55ce3a12009-05-04 09:38:15 +0200740
741 if (!pc.error) {
Borislav Petkove972d702009-07-17 23:55:16 +0000742 ide_debug_log(IDE_DBG_FUNC, "BOP - %s",
Borislav Petkov55ce3a12009-05-04 09:38:15 +0200743 (buf[0] & 0x80) ? "Yes" : "No");
Borislav Petkove972d702009-07-17 23:55:16 +0000744 ide_debug_log(IDE_DBG_FUNC, "EOP - %s",
Borislav Petkov55ce3a12009-05-04 09:38:15 +0200745 (buf[0] & 0x40) ? "Yes" : "No");
746
747 if (buf[0] & 0x4) {
748 printk(KERN_INFO "ide-tape: Block location is unknown"
749 "to the tape\n");
Bartlomiej Zolnierkiewicz8dcce402009-06-13 12:00:54 +0200750 clear_bit(ilog2(IDE_AFLAG_ADDRESS_VALID),
751 &drive->atapi_flags);
Borislav Petkov55ce3a12009-05-04 09:38:15 +0200752 return -1;
753 } else {
Borislav Petkove972d702009-07-17 23:55:16 +0000754 ide_debug_log(IDE_DBG_FUNC, "Block Location: %u",
755 be32_to_cpup((__be32 *)&buf[4]));
Borislav Petkov55ce3a12009-05-04 09:38:15 +0200756
757 tape->partition = buf[1];
758 tape->first_frame = be32_to_cpup((__be32 *)&buf[4]);
Bartlomiej Zolnierkiewicz8dcce402009-06-13 12:00:54 +0200759 set_bit(ilog2(IDE_AFLAG_ADDRESS_VALID),
760 &drive->atapi_flags);
Borislav Petkov55ce3a12009-05-04 09:38:15 +0200761 }
762 }
763
764 return tape->first_frame;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765}
766
Borislav Petkovd236d742008-04-18 00:46:27 +0200767static void idetape_create_locate_cmd(ide_drive_t *drive,
768 struct ide_atapi_pc *pc,
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100769 unsigned int block, u8 partition, int skip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770{
Bartlomiej Zolnierkiewicz7bf74202008-10-10 22:39:37 +0200771 ide_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +0100772 pc->c[0] = POSITION_TO_ELEMENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773 pc->c[1] = 2;
Borislav Petkov860ff5e2008-02-02 19:56:50 +0100774 put_unaligned(cpu_to_be32(block), (unsigned int *) &pc->c[3]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775 pc->c[8] = partition;
Borislav Petkov346331f2008-04-18 00:46:26 +0200776 pc->flags |= PC_FLAG_WAIT_FOR_DSC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777}
778
Borislav Petkovec0fdb02008-04-27 15:38:34 +0200779static void __ide_tape_discard_merge_buffer(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780{
781 idetape_tape_t *tape = drive->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782
Borislav Petkov54abf372008-02-06 02:57:52 +0100783 if (tape->chrdev_dir != IDETAPE_DIR_READ)
Borislav Petkov97986302008-04-27 15:38:34 +0200784 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785
Borislav Petkov49d80782009-06-07 15:37:06 +0200786 clear_bit(ilog2(IDE_AFLAG_FILEMARK), &drive->atapi_flags);
Tejun Heo963da552009-04-19 08:46:02 +0900787 tape->valid = 0;
788 if (tape->buf != NULL) {
789 kfree(tape->buf);
790 tape->buf = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791 }
792
Borislav Petkov54abf372008-02-06 02:57:52 +0100793 tape->chrdev_dir = IDETAPE_DIR_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794}
795
796/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100797 * Position the tape to the requested block using the LOCATE packet command.
798 * A READ POSITION command is then issued to check where we are positioned. Like
799 * all higher level operations, we queue the commands at the tail of the request
800 * queue and wait for their completion.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801 */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100802static int idetape_position_tape(ide_drive_t *drive, unsigned int block,
803 u8 partition, int skip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804{
805 idetape_tape_t *tape = drive->driver_data;
Bartlomiej Zolnierkiewicz2ac07d92008-10-10 22:39:38 +0200806 struct gendisk *disk = tape->disk;
Borislav Petkov55ce3a12009-05-04 09:38:15 +0200807 int ret;
Borislav Petkovd236d742008-04-18 00:46:27 +0200808 struct ide_atapi_pc pc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809
Borislav Petkov54abf372008-02-06 02:57:52 +0100810 if (tape->chrdev_dir == IDETAPE_DIR_READ)
Borislav Petkovec0fdb02008-04-27 15:38:34 +0200811 __ide_tape_discard_merge_buffer(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812 idetape_wait_ready(drive, 60 * 5 * HZ);
813 idetape_create_locate_cmd(drive, &pc, block, partition, skip);
Borislav Petkov55ce3a12009-05-04 09:38:15 +0200814 ret = ide_queue_pc_tail(drive, disk, &pc, NULL, 0);
815 if (ret)
816 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817
Borislav Petkov55ce3a12009-05-04 09:38:15 +0200818 ret = ide_tape_read_position(drive);
819 if (ret < 0)
820 return ret;
821 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822}
823
Borislav Petkovec0fdb02008-04-27 15:38:34 +0200824static void ide_tape_discard_merge_buffer(ide_drive_t *drive,
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100825 int restore_position)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826{
827 idetape_tape_t *tape = drive->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828 int seek, position;
829
Borislav Petkovec0fdb02008-04-27 15:38:34 +0200830 __ide_tape_discard_merge_buffer(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831 if (restore_position) {
Borislav Petkov55ce3a12009-05-04 09:38:15 +0200832 position = ide_tape_read_position(drive);
Borislav Petkov97986302008-04-27 15:38:34 +0200833 seek = position > 0 ? position : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834 if (idetape_position_tape(drive, seek, 0, 0)) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100835 printk(KERN_INFO "ide-tape: %s: position_tape failed in"
Borislav Petkovec0fdb02008-04-27 15:38:34 +0200836 " %s\n", tape->name, __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837 return;
838 }
839 }
840}
841
842/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100843 * Generate a read/write request for the block device interface and wait for it
844 * to be serviced.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845 */
Tejun Heo6bb11dd2009-04-19 08:46:03 +0900846static int idetape_queue_rw_tail(ide_drive_t *drive, int cmd, int size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847{
848 idetape_tape_t *tape = drive->driver_data;
FUJITA Tomonori64ea1b42008-07-15 21:21:43 +0200849 struct request *rq;
Tejun Heo21d9c5d2009-04-19 08:46:02 +0900850 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851
Borislav Petkove972d702009-07-17 23:55:16 +0000852 ide_debug_log(IDE_DBG_FUNC, "cmd: 0x%x, size: %d", cmd, size);
853
Tejun Heo21d9c5d2009-04-19 08:46:02 +0900854 BUG_ON(cmd != REQ_IDETAPE_READ && cmd != REQ_IDETAPE_WRITE);
Tejun Heo6bb11dd2009-04-19 08:46:03 +0900855 BUG_ON(size < 0 || size % tape->blk_size);
Borislav Petkov8004a8c2008-02-06 02:57:51 +0100856
FUJITA Tomonori64ea1b42008-07-15 21:21:43 +0200857 rq = blk_get_request(drive->queue, READ, __GFP_WAIT);
858 rq->cmd_type = REQ_TYPE_SPECIAL;
Borislav Petkov83dd5732008-07-23 19:56:00 +0200859 rq->cmd[13] = cmd;
FUJITA Tomonori64ea1b42008-07-15 21:21:43 +0200860 rq->rq_disk = tape->disk;
Linus Torvaldsc9059592009-06-11 10:52:27 -0700861 rq->__sector = tape->first_frame;
Tejun Heo21d9c5d2009-04-19 08:46:02 +0900862
863 if (size) {
Tejun Heo963da552009-04-19 08:46:02 +0900864 ret = blk_rq_map_kern(drive->queue, rq, tape->buf, size,
Tejun Heo21d9c5d2009-04-19 08:46:02 +0900865 __GFP_WAIT);
866 if (ret)
867 goto out_put;
868 }
869
FUJITA Tomonori64ea1b42008-07-15 21:21:43 +0200870 blk_execute_rq(drive->queue, tape->disk, rq, 0);
871
Tejun Heo963da552009-04-19 08:46:02 +0900872 /* calculate the number of transferred bytes and update buffer state */
Tejun Heoc3a4d782009-05-07 22:24:37 +0900873 size -= rq->resid_len;
Tejun Heo963da552009-04-19 08:46:02 +0900874 tape->cur = tape->buf;
Tejun Heo21d9c5d2009-04-19 08:46:02 +0900875 if (cmd == REQ_IDETAPE_READ)
Tejun Heo963da552009-04-19 08:46:02 +0900876 tape->valid = size;
877 else
878 tape->valid = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879
Tejun Heo21d9c5d2009-04-19 08:46:02 +0900880 ret = size;
881 if (rq->errors == IDE_DRV_ERROR_GENERAL)
882 ret = -EIO;
Tejun Heo21d9c5d2009-04-19 08:46:02 +0900883out_put:
884 blk_put_request(rq);
FUJITA Tomonori64ea1b42008-07-15 21:21:43 +0200885 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886}
887
Borislav Petkovd236d742008-04-18 00:46:27 +0200888static void idetape_create_inquiry_cmd(struct ide_atapi_pc *pc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889{
Bartlomiej Zolnierkiewicz7bf74202008-10-10 22:39:37 +0200890 ide_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +0100891 pc->c[0] = INQUIRY;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100892 pc->c[4] = 254;
Borislav Petkovd236d742008-04-18 00:46:27 +0200893 pc->req_xfer = 254;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894}
895
Borislav Petkovd236d742008-04-18 00:46:27 +0200896static void idetape_create_rewind_cmd(ide_drive_t *drive,
897 struct ide_atapi_pc *pc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898{
Bartlomiej Zolnierkiewicz7bf74202008-10-10 22:39:37 +0200899 ide_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +0100900 pc->c[0] = REZERO_UNIT;
Borislav Petkov346331f2008-04-18 00:46:26 +0200901 pc->flags |= PC_FLAG_WAIT_FOR_DSC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902}
903
Borislav Petkovd236d742008-04-18 00:46:27 +0200904static void idetape_create_erase_cmd(struct ide_atapi_pc *pc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905{
Bartlomiej Zolnierkiewicz7bf74202008-10-10 22:39:37 +0200906 ide_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +0100907 pc->c[0] = ERASE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908 pc->c[1] = 1;
Borislav Petkov346331f2008-04-18 00:46:26 +0200909 pc->flags |= PC_FLAG_WAIT_FOR_DSC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910}
911
Borislav Petkovd236d742008-04-18 00:46:27 +0200912static void idetape_create_space_cmd(struct ide_atapi_pc *pc, int count, u8 cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913{
Bartlomiej Zolnierkiewicz7bf74202008-10-10 22:39:37 +0200914 ide_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +0100915 pc->c[0] = SPACE;
Borislav Petkov860ff5e2008-02-02 19:56:50 +0100916 put_unaligned(cpu_to_be32(count), (unsigned int *) &pc->c[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917 pc->c[1] = cmd;
Borislav Petkov346331f2008-04-18 00:46:26 +0200918 pc->flags |= PC_FLAG_WAIT_FOR_DSC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919}
920
Borislav Petkovd9df9372008-04-27 15:38:34 +0200921static void ide_tape_flush_merge_buffer(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922{
923 idetape_tape_t *tape = drive->driver_data;
Borislav Petkov55a5d292008-02-02 19:56:49 +0100924
Borislav Petkov54abf372008-02-06 02:57:52 +0100925 if (tape->chrdev_dir != IDETAPE_DIR_WRITE) {
Borislav Petkov077e3bd2008-04-27 15:38:34 +0200926 printk(KERN_ERR "ide-tape: bug: Trying to empty merge buffer"
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100927 " but we are not writing.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928 return;
929 }
Tejun Heo963da552009-04-19 08:46:02 +0900930 if (tape->buf) {
Tejun Heo6bb11dd2009-04-19 08:46:03 +0900931 size_t aligned = roundup(tape->valid, tape->blk_size);
932
933 memset(tape->cur, 0, aligned - tape->valid);
Tejun Heo07bd9682009-04-19 08:46:03 +0900934 idetape_queue_rw_tail(drive, REQ_IDETAPE_WRITE, aligned);
Tejun Heo963da552009-04-19 08:46:02 +0900935 kfree(tape->buf);
936 tape->buf = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937 }
Borislav Petkov54abf372008-02-06 02:57:52 +0100938 tape->chrdev_dir = IDETAPE_DIR_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939}
940
Tejun Heo88f1b942009-04-19 08:46:02 +0900941static int idetape_init_rw(ide_drive_t *drive, int dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942{
943 idetape_tape_t *tape = drive->driver_data;
Tejun Heo88f1b942009-04-19 08:46:02 +0900944 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945
Tejun Heo88f1b942009-04-19 08:46:02 +0900946 BUG_ON(dir != IDETAPE_DIR_READ && dir != IDETAPE_DIR_WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947
Tejun Heo88f1b942009-04-19 08:46:02 +0900948 if (tape->chrdev_dir == dir)
949 return 0;
950
951 if (tape->chrdev_dir == IDETAPE_DIR_READ)
952 ide_tape_discard_merge_buffer(drive, 1);
953 else if (tape->chrdev_dir == IDETAPE_DIR_WRITE) {
954 ide_tape_flush_merge_buffer(drive);
955 idetape_flush_tape_buffers(drive);
956 }
957
958 if (tape->buf || tape->valid) {
959 printk(KERN_ERR "ide-tape: valid should be 0 now\n");
960 tape->valid = 0;
961 }
962
963 tape->buf = kmalloc(tape->buffer_size, GFP_KERNEL);
964 if (!tape->buf)
965 return -ENOMEM;
966 tape->chrdev_dir = dir;
967 tape->cur = tape->buf;
968
969 /*
970 * Issue a 0 rw command to ensure that DSC handshake is
971 * switched from completion mode to buffer available mode. No
972 * point in issuing this if DSC overlap isn't supported, some
973 * drives (Seagate STT3401A) will return an error.
974 */
975 if (drive->dev_flags & IDE_DFLAG_DSC_OVERLAP) {
976 int cmd = dir == IDETAPE_DIR_READ ? REQ_IDETAPE_READ
977 : REQ_IDETAPE_WRITE;
978
979 rc = idetape_queue_rw_tail(drive, cmd, 0);
980 if (rc < 0) {
981 kfree(tape->buf);
982 tape->buf = NULL;
983 tape->chrdev_dir = IDETAPE_DIR_NONE;
984 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985 }
986 }
Borislav Petkov5e69bd92008-04-27 15:38:25 +0200987
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988 return 0;
989}
990
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100991static void idetape_pad_zeros(ide_drive_t *drive, int bcount)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992{
993 idetape_tape_t *tape = drive->driver_data;
Tejun Heo963da552009-04-19 08:46:02 +0900994
995 memset(tape->buf, 0, tape->buffer_size);
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100996
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997 while (bcount) {
Tejun Heo963da552009-04-19 08:46:02 +0900998 unsigned int count = min(tape->buffer_size, bcount);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999
Tejun Heo6bb11dd2009-04-19 08:46:03 +09001000 idetape_queue_rw_tail(drive, REQ_IDETAPE_WRITE, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001 bcount -= count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002 }
1003}
1004
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001006 * Rewinds the tape to the Beginning Of the current Partition (BOP). We
1007 * currently support only one partition.
1008 */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001009static int idetape_rewind_tape(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010{
Bartlomiej Zolnierkiewicz2ac07d92008-10-10 22:39:38 +02001011 struct ide_tape_obj *tape = drive->driver_data;
1012 struct gendisk *disk = tape->disk;
Borislav Petkovd236d742008-04-18 00:46:27 +02001013 struct ide_atapi_pc pc;
Borislav Petkov55ce3a12009-05-04 09:38:15 +02001014 int ret;
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001015
Borislav Petkove972d702009-07-17 23:55:16 +00001016 ide_debug_log(IDE_DBG_FUNC, "enter");
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001017
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018 idetape_create_rewind_cmd(drive, &pc);
Borislav Petkov55ce3a12009-05-04 09:38:15 +02001019 ret = ide_queue_pc_tail(drive, disk, &pc, NULL, 0);
1020 if (ret)
1021 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022
Borislav Petkov55ce3a12009-05-04 09:38:15 +02001023 ret = ide_tape_read_position(drive);
1024 if (ret < 0)
1025 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026 return 0;
1027}
1028
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001029/* mtio.h compatible commands should be issued to the chrdev interface. */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001030static int idetape_blkdev_ioctl(ide_drive_t *drive, unsigned int cmd,
1031 unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032{
1033 idetape_tape_t *tape = drive->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034 void __user *argp = (void __user *)arg;
1035
Borislav Petkovd59823f2008-02-02 19:56:51 +01001036 struct idetape_config {
1037 int dsc_rw_frequency;
1038 int dsc_media_access_frequency;
1039 int nr_stages;
1040 } config;
1041
Borislav Petkove972d702009-07-17 23:55:16 +00001042 ide_debug_log(IDE_DBG_FUNC, "cmd: 0x%04x", cmd);
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001043
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044 switch (cmd) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001045 case 0x0340:
1046 if (copy_from_user(&config, argp, sizeof(config)))
1047 return -EFAULT;
1048 tape->best_dsc_rw_freq = config.dsc_rw_frequency;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001049 break;
1050 case 0x0350:
Michael Buesch2fc21112009-07-19 09:15:19 +00001051 memset(&config, 0, sizeof(config));
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001052 config.dsc_rw_frequency = (int) tape->best_dsc_rw_freq;
Borislav Petkov83042b22008-04-27 15:38:27 +02001053 config.nr_stages = 1;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001054 if (copy_to_user(argp, &config, sizeof(config)))
1055 return -EFAULT;
1056 break;
1057 default:
1058 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059 }
1060 return 0;
1061}
1062
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001063static int idetape_space_over_filemarks(ide_drive_t *drive, short mt_op,
1064 int mt_count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065{
1066 idetape_tape_t *tape = drive->driver_data;
Bartlomiej Zolnierkiewicz2ac07d92008-10-10 22:39:38 +02001067 struct gendisk *disk = tape->disk;
Borislav Petkovd236d742008-04-18 00:46:27 +02001068 struct ide_atapi_pc pc;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001069 int retval, count = 0;
Borislav Petkovb6422012008-02-02 19:56:49 +01001070 int sprev = !!(tape->caps[4] & 0x20);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071
Borislav Petkove972d702009-07-17 23:55:16 +00001072
1073 ide_debug_log(IDE_DBG_FUNC, "mt_op: %d, mt_count: %d", mt_op, mt_count);
1074
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075 if (mt_count == 0)
1076 return 0;
1077 if (MTBSF == mt_op || MTBSFM == mt_op) {
Borislav Petkovb6422012008-02-02 19:56:49 +01001078 if (!sprev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079 return -EIO;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001080 mt_count = -mt_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081 }
1082
Borislav Petkov54abf372008-02-06 02:57:52 +01001083 if (tape->chrdev_dir == IDETAPE_DIR_READ) {
Tejun Heo963da552009-04-19 08:46:02 +09001084 tape->valid = 0;
Borislav Petkov49d80782009-06-07 15:37:06 +02001085 if (test_and_clear_bit(ilog2(IDE_AFLAG_FILEMARK),
1086 &drive->atapi_flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087 ++count;
Borislav Petkovec0fdb02008-04-27 15:38:34 +02001088 ide_tape_discard_merge_buffer(drive, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089 }
1090
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091 switch (mt_op) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001092 case MTFSF:
1093 case MTBSF:
1094 idetape_create_space_cmd(&pc, mt_count - count,
1095 IDETAPE_SPACE_OVER_FILEMARK);
Borislav Petkovb13345f2009-05-02 10:26:12 +02001096 return ide_queue_pc_tail(drive, disk, &pc, NULL, 0);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001097 case MTFSFM:
1098 case MTBSFM:
1099 if (!sprev)
1100 return -EIO;
1101 retval = idetape_space_over_filemarks(drive, MTFSF,
1102 mt_count - count);
1103 if (retval)
1104 return retval;
1105 count = (MTBSFM == mt_op ? 1 : -1);
1106 return idetape_space_over_filemarks(drive, MTFSF, count);
1107 default:
1108 printk(KERN_ERR "ide-tape: MTIO operation %d not supported\n",
1109 mt_op);
1110 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111 }
1112}
1113
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001115 * Our character device read / write functions.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001117 * The tape is optimized to maximize throughput when it is transferring an
1118 * integral number of the "continuous transfer limit", which is a parameter of
1119 * the specific tape (26kB on my particular tape, 32kB for Onstream).
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001121 * As of version 1.3 of the driver, the character device provides an abstract
1122 * continuous view of the media - any mix of block sizes (even 1 byte) on the
1123 * same backup/restore procedure is supported. The driver will internally
1124 * convert the requests to the recommended transfer unit, so that an unmatch
1125 * between the user's block size to the recommended size will only result in a
1126 * (slightly) increased driver overhead, but will no longer hit performance.
1127 * This is not applicable to Onstream.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128 */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001129static ssize_t idetape_chrdev_read(struct file *file, char __user *buf,
1130 size_t count, loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131{
Borislav Petkov5aeddf92008-10-13 21:39:34 +02001132 struct ide_tape_obj *tape = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133 ide_drive_t *drive = tape->drive;
Tejun Heo07bd9682009-04-19 08:46:03 +09001134 size_t done = 0;
Daniel Walkerdcd96372006-06-25 05:47:37 -07001135 ssize_t ret = 0;
Tejun Heo07bd9682009-04-19 08:46:03 +09001136 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137
Borislav Petkove972d702009-07-17 23:55:16 +00001138 ide_debug_log(IDE_DBG_FUNC, "count %Zd", count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139
Borislav Petkov54abf372008-02-06 02:57:52 +01001140 if (tape->chrdev_dir != IDETAPE_DIR_READ) {
Borislav Petkov49d80782009-06-07 15:37:06 +02001141 if (test_bit(ilog2(IDE_AFLAG_DETECT_BS), &drive->atapi_flags))
Borislav Petkov54bb2072008-02-06 02:57:52 +01001142 if (count > tape->blk_size &&
1143 (count % tape->blk_size) == 0)
1144 tape->user_bs_factor = count / tape->blk_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145 }
Tejun Heo07bd9682009-04-19 08:46:03 +09001146
Tejun Heo88f1b942009-04-19 08:46:02 +09001147 rc = idetape_init_rw(drive, IDETAPE_DIR_READ);
Borislav Petkov8d06bfa2008-02-06 02:57:53 +01001148 if (rc < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149 return rc;
Tejun Heo07bd9682009-04-19 08:46:03 +09001150
1151 while (done < count) {
1152 size_t todo;
1153
1154 /* refill if staging buffer is empty */
1155 if (!tape->valid) {
1156 /* If we are at a filemark, nothing more to read */
Borislav Petkov49d80782009-06-07 15:37:06 +02001157 if (test_bit(ilog2(IDE_AFLAG_FILEMARK),
1158 &drive->atapi_flags))
Tejun Heo07bd9682009-04-19 08:46:03 +09001159 break;
1160 /* read */
1161 if (idetape_queue_rw_tail(drive, REQ_IDETAPE_READ,
1162 tape->buffer_size) <= 0)
1163 break;
1164 }
1165
1166 /* copy out */
1167 todo = min_t(size_t, count - done, tape->valid);
1168 if (copy_to_user(buf + done, tape->cur, todo))
Daniel Walkerdcd96372006-06-25 05:47:37 -07001169 ret = -EFAULT;
Tejun Heo07bd9682009-04-19 08:46:03 +09001170
1171 tape->cur += todo;
1172 tape->valid -= todo;
1173 done += todo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174 }
Tejun Heo07bd9682009-04-19 08:46:03 +09001175
Borislav Petkov49d80782009-06-07 15:37:06 +02001176 if (!done && test_bit(ilog2(IDE_AFLAG_FILEMARK), &drive->atapi_flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177 idetape_space_over_filemarks(drive, MTFSF, 1);
1178 return 0;
1179 }
Daniel Walkerdcd96372006-06-25 05:47:37 -07001180
Tejun Heo07bd9682009-04-19 08:46:03 +09001181 return ret ? ret : done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182}
1183
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001184static ssize_t idetape_chrdev_write(struct file *file, const char __user *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185 size_t count, loff_t *ppos)
1186{
Borislav Petkov5aeddf92008-10-13 21:39:34 +02001187 struct ide_tape_obj *tape = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188 ide_drive_t *drive = tape->drive;
Tejun Heo07bd9682009-04-19 08:46:03 +09001189 size_t done = 0;
Daniel Walkerdcd96372006-06-25 05:47:37 -07001190 ssize_t ret = 0;
Tejun Heo88f1b942009-04-19 08:46:02 +09001191 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192
1193 /* The drive is write protected. */
1194 if (tape->write_prot)
1195 return -EACCES;
1196
Borislav Petkove972d702009-07-17 23:55:16 +00001197 ide_debug_log(IDE_DBG_FUNC, "count %Zd", count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198
1199 /* Initialize write operation */
Tejun Heo88f1b942009-04-19 08:46:02 +09001200 rc = idetape_init_rw(drive, IDETAPE_DIR_WRITE);
1201 if (rc < 0)
1202 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203
Tejun Heo07bd9682009-04-19 08:46:03 +09001204 while (done < count) {
1205 size_t todo;
1206
1207 /* flush if staging buffer is full */
1208 if (tape->valid == tape->buffer_size &&
1209 idetape_queue_rw_tail(drive, REQ_IDETAPE_WRITE,
1210 tape->buffer_size) <= 0)
1211 return rc;
1212
1213 /* copy in */
1214 todo = min_t(size_t, count - done,
1215 tape->buffer_size - tape->valid);
1216 if (copy_from_user(tape->cur, buf + done, todo))
Daniel Walkerdcd96372006-06-25 05:47:37 -07001217 ret = -EFAULT;
Tejun Heo07bd9682009-04-19 08:46:03 +09001218
1219 tape->cur += todo;
1220 tape->valid += todo;
1221 done += todo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222 }
Tejun Heo07bd9682009-04-19 08:46:03 +09001223
1224 return ret ? ret : done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225}
1226
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001227static int idetape_write_filemark(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228{
Bartlomiej Zolnierkiewicz2ac07d92008-10-10 22:39:38 +02001229 struct ide_tape_obj *tape = drive->driver_data;
Borislav Petkovd236d742008-04-18 00:46:27 +02001230 struct ide_atapi_pc pc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231
1232 /* Write a filemark */
1233 idetape_create_write_filemark_cmd(drive, &pc, 1);
Borislav Petkovb13345f2009-05-02 10:26:12 +02001234 if (ide_queue_pc_tail(drive, tape->disk, &pc, NULL, 0)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235 printk(KERN_ERR "ide-tape: Couldn't write a filemark\n");
1236 return -EIO;
1237 }
1238 return 0;
1239}
1240
1241/*
Borislav Petkovd99c9da2008-02-02 19:56:51 +01001242 * Called from idetape_chrdev_ioctl when the general mtio MTIOCTOP ioctl is
1243 * requested.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244 *
Borislav Petkovd99c9da2008-02-02 19:56:51 +01001245 * Note: MTBSF and MTBSFM are not supported when the tape doesn't support
1246 * spacing over filemarks in the reverse direction. In this case, MTFSFM is also
Borislav Petkov5bd50dc2008-04-27 15:38:28 +02001247 * usually not supported.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248 *
Borislav Petkovd99c9da2008-02-02 19:56:51 +01001249 * The following commands are currently not supported:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250 *
Borislav Petkovd99c9da2008-02-02 19:56:51 +01001251 * MTFSS, MTBSS, MTWSM, MTSETDENSITY, MTSETDRVBUFFER, MT_ST_BOOLEANS,
1252 * MT_ST_WRITE_THRESHOLD.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253 */
Borislav Petkovd99c9da2008-02-02 19:56:51 +01001254static int idetape_mtioctop(ide_drive_t *drive, short mt_op, int mt_count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255{
1256 idetape_tape_t *tape = drive->driver_data;
Bartlomiej Zolnierkiewicz2ac07d92008-10-10 22:39:38 +02001257 struct gendisk *disk = tape->disk;
Borislav Petkovd236d742008-04-18 00:46:27 +02001258 struct ide_atapi_pc pc;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001259 int i, retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260
Borislav Petkove972d702009-07-17 23:55:16 +00001261 ide_debug_log(IDE_DBG_FUNC, "MTIOCTOP ioctl: mt_op: %d, mt_count: %d",
1262 mt_op, mt_count);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001263
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264 switch (mt_op) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001265 case MTFSF:
1266 case MTFSFM:
1267 case MTBSF:
1268 case MTBSFM:
1269 if (!mt_count)
1270 return 0;
1271 return idetape_space_over_filemarks(drive, mt_op, mt_count);
1272 default:
1273 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274 }
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001275
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276 switch (mt_op) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001277 case MTWEOF:
1278 if (tape->write_prot)
1279 return -EACCES;
Borislav Petkovec0fdb02008-04-27 15:38:34 +02001280 ide_tape_discard_merge_buffer(drive, 1);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001281 for (i = 0; i < mt_count; i++) {
1282 retval = idetape_write_filemark(drive);
1283 if (retval)
1284 return retval;
1285 }
1286 return 0;
1287 case MTREW:
Borislav Petkovec0fdb02008-04-27 15:38:34 +02001288 ide_tape_discard_merge_buffer(drive, 0);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001289 if (idetape_rewind_tape(drive))
1290 return -EIO;
1291 return 0;
1292 case MTLOAD:
Borislav Petkovec0fdb02008-04-27 15:38:34 +02001293 ide_tape_discard_merge_buffer(drive, 0);
Bartlomiej Zolnierkiewicz0c8a6c72008-10-10 22:39:39 +02001294 return ide_do_start_stop(drive, disk, IDETAPE_LU_LOAD_MASK);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001295 case MTUNLOAD:
1296 case MTOFFL:
1297 /*
1298 * If door is locked, attempt to unlock before
1299 * attempting to eject.
1300 */
1301 if (tape->door_locked) {
Bartlomiej Zolnierkiewicz05780422008-10-10 22:39:38 +02001302 if (!ide_set_media_lock(drive, disk, 0))
Bartlomiej Zolnierkiewicz385a4b82008-10-10 22:39:37 +02001303 tape->door_locked = DOOR_UNLOCKED;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001304 }
Borislav Petkovec0fdb02008-04-27 15:38:34 +02001305 ide_tape_discard_merge_buffer(drive, 0);
Bartlomiej Zolnierkiewicz0c8a6c72008-10-10 22:39:39 +02001306 retval = ide_do_start_stop(drive, disk, !IDETAPE_LU_LOAD_MASK);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001307 if (!retval)
Borislav Petkov49d80782009-06-07 15:37:06 +02001308 clear_bit(ilog2(IDE_AFLAG_MEDIUM_PRESENT),
1309 &drive->atapi_flags);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001310 return retval;
1311 case MTNOP:
Borislav Petkovec0fdb02008-04-27 15:38:34 +02001312 ide_tape_discard_merge_buffer(drive, 0);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001313 return idetape_flush_tape_buffers(drive);
1314 case MTRETEN:
Borislav Petkovec0fdb02008-04-27 15:38:34 +02001315 ide_tape_discard_merge_buffer(drive, 0);
Bartlomiej Zolnierkiewicz0c8a6c72008-10-10 22:39:39 +02001316 return ide_do_start_stop(drive, disk,
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001317 IDETAPE_LU_RETENSION_MASK | IDETAPE_LU_LOAD_MASK);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001318 case MTEOM:
1319 idetape_create_space_cmd(&pc, 0, IDETAPE_SPACE_TO_EOD);
Borislav Petkovb13345f2009-05-02 10:26:12 +02001320 return ide_queue_pc_tail(drive, disk, &pc, NULL, 0);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001321 case MTERASE:
1322 (void)idetape_rewind_tape(drive);
1323 idetape_create_erase_cmd(&pc);
Borislav Petkovb13345f2009-05-02 10:26:12 +02001324 return ide_queue_pc_tail(drive, disk, &pc, NULL, 0);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001325 case MTSETBLK:
1326 if (mt_count) {
1327 if (mt_count < tape->blk_size ||
1328 mt_count % tape->blk_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329 return -EIO;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001330 tape->user_bs_factor = mt_count / tape->blk_size;
Borislav Petkov49d80782009-06-07 15:37:06 +02001331 clear_bit(ilog2(IDE_AFLAG_DETECT_BS),
1332 &drive->atapi_flags);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001333 } else
Borislav Petkov49d80782009-06-07 15:37:06 +02001334 set_bit(ilog2(IDE_AFLAG_DETECT_BS),
1335 &drive->atapi_flags);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001336 return 0;
1337 case MTSEEK:
Borislav Petkovec0fdb02008-04-27 15:38:34 +02001338 ide_tape_discard_merge_buffer(drive, 0);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001339 return idetape_position_tape(drive,
1340 mt_count * tape->user_bs_factor, tape->partition, 0);
1341 case MTSETPART:
Borislav Petkovec0fdb02008-04-27 15:38:34 +02001342 ide_tape_discard_merge_buffer(drive, 0);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001343 return idetape_position_tape(drive, 0, mt_count, 0);
1344 case MTFSR:
1345 case MTBSR:
1346 case MTLOCK:
Bartlomiej Zolnierkiewicz05780422008-10-10 22:39:38 +02001347 retval = ide_set_media_lock(drive, disk, 1);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001348 if (retval)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349 return retval;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001350 tape->door_locked = DOOR_EXPLICITLY_LOCKED;
1351 return 0;
1352 case MTUNLOCK:
Bartlomiej Zolnierkiewicz05780422008-10-10 22:39:38 +02001353 retval = ide_set_media_lock(drive, disk, 0);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001354 if (retval)
1355 return retval;
1356 tape->door_locked = DOOR_UNLOCKED;
1357 return 0;
1358 default:
1359 printk(KERN_ERR "ide-tape: MTIO operation %d not supported\n",
1360 mt_op);
1361 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362 }
1363}
1364
1365/*
Borislav Petkovd99c9da2008-02-02 19:56:51 +01001366 * Our character device ioctls. General mtio.h magnetic io commands are
1367 * supported here, and not in the corresponding block interface. Our own
1368 * ide-tape ioctls are supported on both interfaces.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369 */
Alan Cox05227ad2010-01-04 06:24:00 +00001370static long do_idetape_chrdev_ioctl(struct file *file,
Borislav Petkovd99c9da2008-02-02 19:56:51 +01001371 unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372{
Borislav Petkov5aeddf92008-10-13 21:39:34 +02001373 struct ide_tape_obj *tape = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374 ide_drive_t *drive = tape->drive;
1375 struct mtop mtop;
1376 struct mtget mtget;
1377 struct mtpos mtpos;
Borislav Petkov54bb2072008-02-06 02:57:52 +01001378 int block_offset = 0, position = tape->first_frame;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379 void __user *argp = (void __user *)arg;
1380
Borislav Petkove972d702009-07-17 23:55:16 +00001381 ide_debug_log(IDE_DBG_FUNC, "cmd: 0x%x", cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382
Borislav Petkov54abf372008-02-06 02:57:52 +01001383 if (tape->chrdev_dir == IDETAPE_DIR_WRITE) {
Borislav Petkovd9df9372008-04-27 15:38:34 +02001384 ide_tape_flush_merge_buffer(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385 idetape_flush_tape_buffers(drive);
1386 }
1387 if (cmd == MTIOCGET || cmd == MTIOCPOS) {
Tejun Heo963da552009-04-19 08:46:02 +09001388 block_offset = tape->valid /
Borislav Petkov54bb2072008-02-06 02:57:52 +01001389 (tape->blk_size * tape->user_bs_factor);
Borislav Petkov55ce3a12009-05-04 09:38:15 +02001390 position = ide_tape_read_position(drive);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001391 if (position < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392 return -EIO;
1393 }
1394 switch (cmd) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001395 case MTIOCTOP:
1396 if (copy_from_user(&mtop, argp, sizeof(struct mtop)))
1397 return -EFAULT;
1398 return idetape_mtioctop(drive, mtop.mt_op, mtop.mt_count);
1399 case MTIOCGET:
1400 memset(&mtget, 0, sizeof(struct mtget));
1401 mtget.mt_type = MT_ISSCSI2;
1402 mtget.mt_blkno = position / tape->user_bs_factor - block_offset;
1403 mtget.mt_dsreg =
1404 ((tape->blk_size * tape->user_bs_factor)
1405 << MT_ST_BLKSIZE_SHIFT) & MT_ST_BLKSIZE_MASK;
Borislav Petkov54bb2072008-02-06 02:57:52 +01001406
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001407 if (tape->drv_write_prot)
1408 mtget.mt_gstat |= GMT_WR_PROT(0xffffffff);
1409
1410 if (copy_to_user(argp, &mtget, sizeof(struct mtget)))
1411 return -EFAULT;
1412 return 0;
1413 case MTIOCPOS:
1414 mtpos.mt_blkno = position / tape->user_bs_factor - block_offset;
1415 if (copy_to_user(argp, &mtpos, sizeof(struct mtpos)))
1416 return -EFAULT;
1417 return 0;
1418 default:
1419 if (tape->chrdev_dir == IDETAPE_DIR_READ)
Borislav Petkovec0fdb02008-04-27 15:38:34 +02001420 ide_tape_discard_merge_buffer(drive, 1);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001421 return idetape_blkdev_ioctl(drive, cmd, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422 }
1423}
1424
Alan Cox05227ad2010-01-04 06:24:00 +00001425static long idetape_chrdev_ioctl(struct file *file,
1426 unsigned int cmd, unsigned long arg)
1427{
1428 long ret;
1429 lock_kernel();
1430 ret = do_idetape_chrdev_ioctl(file, cmd, arg);
1431 unlock_kernel();
1432 return ret;
1433}
1434
Borislav Petkov3cffb9c2008-02-02 19:56:50 +01001435/*
1436 * Do a mode sense page 0 with block descriptor and if it succeeds set the tape
1437 * block size with the reported value.
1438 */
1439static void ide_tape_get_bsize_from_bdesc(ide_drive_t *drive)
1440{
1441 idetape_tape_t *tape = drive->driver_data;
Borislav Petkovd236d742008-04-18 00:46:27 +02001442 struct ide_atapi_pc pc;
Borislav Petkov837272b2009-05-04 09:48:57 +02001443 u8 buf[12];
Borislav Petkov3cffb9c2008-02-02 19:56:50 +01001444
1445 idetape_create_mode_sense_cmd(&pc, IDETAPE_BLOCK_DESCRIPTOR);
Borislav Petkov837272b2009-05-04 09:48:57 +02001446 if (ide_queue_pc_tail(drive, tape->disk, &pc, buf, pc.req_xfer)) {
Borislav Petkov3cffb9c2008-02-02 19:56:50 +01001447 printk(KERN_ERR "ide-tape: Can't get block descriptor\n");
Borislav Petkov54bb2072008-02-06 02:57:52 +01001448 if (tape->blk_size == 0) {
Borislav Petkov3cffb9c2008-02-02 19:56:50 +01001449 printk(KERN_WARNING "ide-tape: Cannot deal with zero "
1450 "block size, assuming 32k\n");
Borislav Petkov54bb2072008-02-06 02:57:52 +01001451 tape->blk_size = 32768;
Borislav Petkov3cffb9c2008-02-02 19:56:50 +01001452 }
1453 return;
1454 }
Borislav Petkov837272b2009-05-04 09:48:57 +02001455 tape->blk_size = (buf[4 + 5] << 16) +
1456 (buf[4 + 6] << 8) +
1457 buf[4 + 7];
1458 tape->drv_write_prot = (buf[2] & 0x80) >> 7;
Borislav Petkove972d702009-07-17 23:55:16 +00001459
1460 ide_debug_log(IDE_DBG_FUNC, "blk_size: %d, write_prot: %d",
1461 tape->blk_size, tape->drv_write_prot);
Borislav Petkov3cffb9c2008-02-02 19:56:50 +01001462}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001464static int idetape_chrdev_open(struct inode *inode, struct file *filp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465{
1466 unsigned int minor = iminor(inode), i = minor & ~0xc0;
1467 ide_drive_t *drive;
1468 idetape_tape_t *tape;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469 int retval;
1470
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001471 if (i >= MAX_HWIFS * MAX_DRIVES)
1472 return -ENXIO;
1473
Borislav Petkovcbba2fa2009-10-11 00:25:19 +00001474 mutex_lock(&idetape_chrdev_mutex);
1475
Borislav Petkov9d01e4c2009-06-08 22:03:03 +02001476 tape = ide_tape_get(NULL, true, i);
Jonathan Corbet04f4ac92008-05-15 12:01:56 -06001477 if (!tape) {
Borislav Petkovcbba2fa2009-10-11 00:25:19 +00001478 mutex_unlock(&idetape_chrdev_mutex);
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001479 return -ENXIO;
Jonathan Corbet04f4ac92008-05-15 12:01:56 -06001480 }
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001481
Borislav Petkove972d702009-07-17 23:55:16 +00001482 drive = tape->drive;
1483 filp->private_data = tape;
1484
1485 ide_debug_log(IDE_DBG_FUNC, "enter");
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001486
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487 /*
1488 * We really want to do nonseekable_open(inode, filp); here, but some
1489 * versions of tar incorrectly call lseek on tapes and bail out if that
1490 * fails. So we disallow pread() and pwrite(), but permit lseeks.
1491 */
1492 filp->f_mode &= ~(FMODE_PREAD | FMODE_PWRITE);
1493
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494
Borislav Petkov49d80782009-06-07 15:37:06 +02001495 if (test_and_set_bit(ilog2(IDE_AFLAG_BUSY), &drive->atapi_flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496 retval = -EBUSY;
1497 goto out_put_tape;
1498 }
1499
1500 retval = idetape_wait_ready(drive, 60 * HZ);
1501 if (retval) {
Borislav Petkov49d80782009-06-07 15:37:06 +02001502 clear_bit(ilog2(IDE_AFLAG_BUSY), &drive->atapi_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503 printk(KERN_ERR "ide-tape: %s: drive not ready\n", tape->name);
1504 goto out_put_tape;
1505 }
1506
Borislav Petkov79ca7432009-06-15 07:32:04 +02001507 ide_tape_read_position(drive);
Borislav Petkov49d80782009-06-07 15:37:06 +02001508 if (!test_bit(ilog2(IDE_AFLAG_ADDRESS_VALID), &drive->atapi_flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509 (void)idetape_rewind_tape(drive);
1510
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511 /* Read block size and write protect status from drive. */
Borislav Petkov3cffb9c2008-02-02 19:56:50 +01001512 ide_tape_get_bsize_from_bdesc(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513
1514 /* Set write protect flag if device is opened as read-only. */
1515 if ((filp->f_flags & O_ACCMODE) == O_RDONLY)
1516 tape->write_prot = 1;
1517 else
1518 tape->write_prot = tape->drv_write_prot;
1519
1520 /* Make sure drive isn't write protected if user wants to write. */
1521 if (tape->write_prot) {
1522 if ((filp->f_flags & O_ACCMODE) == O_WRONLY ||
1523 (filp->f_flags & O_ACCMODE) == O_RDWR) {
Borislav Petkov49d80782009-06-07 15:37:06 +02001524 clear_bit(ilog2(IDE_AFLAG_BUSY), &drive->atapi_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001525 retval = -EROFS;
1526 goto out_put_tape;
1527 }
1528 }
1529
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001530 /* Lock the tape drive door so user can't eject. */
Borislav Petkov54abf372008-02-06 02:57:52 +01001531 if (tape->chrdev_dir == IDETAPE_DIR_NONE) {
Bartlomiej Zolnierkiewicz05780422008-10-10 22:39:38 +02001532 if (!ide_set_media_lock(drive, tape->disk, 1)) {
Bartlomiej Zolnierkiewicz385a4b82008-10-10 22:39:37 +02001533 if (tape->door_locked != DOOR_EXPLICITLY_LOCKED)
1534 tape->door_locked = DOOR_LOCKED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535 }
1536 }
Borislav Petkovcbba2fa2009-10-11 00:25:19 +00001537 mutex_unlock(&idetape_chrdev_mutex);
1538
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539 return 0;
1540
1541out_put_tape:
1542 ide_tape_put(tape);
Borislav Petkovcbba2fa2009-10-11 00:25:19 +00001543
1544 mutex_unlock(&idetape_chrdev_mutex);
1545
Linus Torvalds1da177e2005-04-16 15:20:36 -07001546 return retval;
1547}
1548
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001549static void idetape_write_release(ide_drive_t *drive, unsigned int minor)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550{
1551 idetape_tape_t *tape = drive->driver_data;
1552
Borislav Petkovd9df9372008-04-27 15:38:34 +02001553 ide_tape_flush_merge_buffer(drive);
Tejun Heo963da552009-04-19 08:46:02 +09001554 tape->buf = kmalloc(tape->buffer_size, GFP_KERNEL);
1555 if (tape->buf != NULL) {
Borislav Petkov54bb2072008-02-06 02:57:52 +01001556 idetape_pad_zeros(drive, tape->blk_size *
1557 (tape->user_bs_factor - 1));
Tejun Heo963da552009-04-19 08:46:02 +09001558 kfree(tape->buf);
1559 tape->buf = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560 }
1561 idetape_write_filemark(drive);
1562 idetape_flush_tape_buffers(drive);
1563 idetape_flush_tape_buffers(drive);
1564}
1565
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001566static int idetape_chrdev_release(struct inode *inode, struct file *filp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567{
Borislav Petkov5aeddf92008-10-13 21:39:34 +02001568 struct ide_tape_obj *tape = filp->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569 ide_drive_t *drive = tape->drive;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570 unsigned int minor = iminor(inode);
1571
Borislav Petkovcbba2fa2009-10-11 00:25:19 +00001572 mutex_lock(&idetape_chrdev_mutex);
1573
Linus Torvalds1da177e2005-04-16 15:20:36 -07001574 tape = drive->driver_data;
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001575
Borislav Petkove972d702009-07-17 23:55:16 +00001576 ide_debug_log(IDE_DBG_FUNC, "enter");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001577
Borislav Petkov54abf372008-02-06 02:57:52 +01001578 if (tape->chrdev_dir == IDETAPE_DIR_WRITE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579 idetape_write_release(drive, minor);
Borislav Petkov54abf372008-02-06 02:57:52 +01001580 if (tape->chrdev_dir == IDETAPE_DIR_READ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581 if (minor < 128)
Borislav Petkovec0fdb02008-04-27 15:38:34 +02001582 ide_tape_discard_merge_buffer(drive, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001583 }
Borislav Petkovf64eee72008-04-27 15:38:25 +02001584
Borislav Petkov49d80782009-06-07 15:37:06 +02001585 if (minor < 128 && test_bit(ilog2(IDE_AFLAG_MEDIUM_PRESENT),
1586 &drive->atapi_flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587 (void) idetape_rewind_tape(drive);
Borislav Petkov49d80782009-06-07 15:37:06 +02001588
Borislav Petkov54abf372008-02-06 02:57:52 +01001589 if (tape->chrdev_dir == IDETAPE_DIR_NONE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590 if (tape->door_locked == DOOR_LOCKED) {
Bartlomiej Zolnierkiewicz05780422008-10-10 22:39:38 +02001591 if (!ide_set_media_lock(drive, tape->disk, 0))
Bartlomiej Zolnierkiewicz385a4b82008-10-10 22:39:37 +02001592 tape->door_locked = DOOR_UNLOCKED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001593 }
1594 }
Borislav Petkov49d80782009-06-07 15:37:06 +02001595 clear_bit(ilog2(IDE_AFLAG_BUSY), &drive->atapi_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596 ide_tape_put(tape);
Borislav Petkovcbba2fa2009-10-11 00:25:19 +00001597
1598 mutex_unlock(&idetape_chrdev_mutex);
1599
Linus Torvalds1da177e2005-04-16 15:20:36 -07001600 return 0;
1601}
1602
Borislav Petkov6d29c8f2008-02-02 19:56:49 +01001603static void idetape_get_inquiry_results(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001605 idetape_tape_t *tape = drive->driver_data;
Borislav Petkovd236d742008-04-18 00:46:27 +02001606 struct ide_atapi_pc pc;
Bartlomiej Zolnierkiewicz41fa9f82009-03-31 20:15:25 +02001607 u8 pc_buf[256];
Borislav Petkov801bd322008-09-27 19:32:17 +02001608 char fw_rev[4], vendor_id[8], product_id[16];
Borislav Petkov6d29c8f2008-02-02 19:56:49 +01001609
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610 idetape_create_inquiry_cmd(&pc);
Borislav Petkovb13345f2009-05-02 10:26:12 +02001611 if (ide_queue_pc_tail(drive, tape->disk, &pc, pc_buf, pc.req_xfer)) {
Borislav Petkov6d29c8f2008-02-02 19:56:49 +01001612 printk(KERN_ERR "ide-tape: %s: can't get INQUIRY results\n",
1613 tape->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001614 return;
1615 }
Borislav Petkovb13345f2009-05-02 10:26:12 +02001616 memcpy(vendor_id, &pc_buf[8], 8);
1617 memcpy(product_id, &pc_buf[16], 16);
1618 memcpy(fw_rev, &pc_buf[32], 4);
Borislav Petkov6d29c8f2008-02-02 19:56:49 +01001619
Borislav Petkov801bd322008-09-27 19:32:17 +02001620 ide_fixstring(vendor_id, 8, 0);
1621 ide_fixstring(product_id, 16, 0);
1622 ide_fixstring(fw_rev, 4, 0);
Borislav Petkov41f81d542008-02-06 02:57:52 +01001623
Borislav Petkov801bd322008-09-27 19:32:17 +02001624 printk(KERN_INFO "ide-tape: %s <-> %s: %.8s %.16s rev %.4s\n",
Borislav Petkov41f81d542008-02-06 02:57:52 +01001625 drive->name, tape->name, vendor_id, product_id, fw_rev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001626}
1627
1628/*
Borislav Petkovb6422012008-02-02 19:56:49 +01001629 * Ask the tape about its various parameters. In particular, we will adjust our
1630 * data transfer buffer size to the recommended value as returned by the tape.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001631 */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001632static void idetape_get_mode_sense_results(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001633{
1634 idetape_tape_t *tape = drive->driver_data;
Borislav Petkovd236d742008-04-18 00:46:27 +02001635 struct ide_atapi_pc pc;
Borislav Petkovb13345f2009-05-02 10:26:12 +02001636 u8 buf[24], *caps;
Borislav Petkovb6422012008-02-02 19:56:49 +01001637 u8 speed, max_speed;
Borislav Petkov47314fa2008-02-02 19:56:48 +01001638
Linus Torvalds1da177e2005-04-16 15:20:36 -07001639 idetape_create_mode_sense_cmd(&pc, IDETAPE_CAPABILITIES_PAGE);
Borislav Petkovb13345f2009-05-02 10:26:12 +02001640 if (ide_queue_pc_tail(drive, tape->disk, &pc, buf, pc.req_xfer)) {
Borislav Petkovb6422012008-02-02 19:56:49 +01001641 printk(KERN_ERR "ide-tape: Can't get tape parameters - assuming"
1642 " some default values\n");
Borislav Petkov54bb2072008-02-06 02:57:52 +01001643 tape->blk_size = 512;
Borislav Petkovb6422012008-02-02 19:56:49 +01001644 put_unaligned(52, (u16 *)&tape->caps[12]);
1645 put_unaligned(540, (u16 *)&tape->caps[14]);
1646 put_unaligned(6*52, (u16 *)&tape->caps[16]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001647 return;
1648 }
Borislav Petkovb13345f2009-05-02 10:26:12 +02001649 caps = buf + 4 + buf[3];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001650
Borislav Petkovb6422012008-02-02 19:56:49 +01001651 /* convert to host order and save for later use */
Harvey Harrisoncd740ab2008-07-24 22:53:33 +02001652 speed = be16_to_cpup((__be16 *)&caps[14]);
1653 max_speed = be16_to_cpup((__be16 *)&caps[8]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654
Harvey Harrisoncd740ab2008-07-24 22:53:33 +02001655 *(u16 *)&caps[8] = max_speed;
1656 *(u16 *)&caps[12] = be16_to_cpup((__be16 *)&caps[12]);
1657 *(u16 *)&caps[14] = speed;
1658 *(u16 *)&caps[16] = be16_to_cpup((__be16 *)&caps[16]);
Borislav Petkovb6422012008-02-02 19:56:49 +01001659
1660 if (!speed) {
1661 printk(KERN_INFO "ide-tape: %s: invalid tape speed "
1662 "(assuming 650KB/sec)\n", drive->name);
Harvey Harrisoncd740ab2008-07-24 22:53:33 +02001663 *(u16 *)&caps[14] = 650;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664 }
Borislav Petkovb6422012008-02-02 19:56:49 +01001665 if (!max_speed) {
1666 printk(KERN_INFO "ide-tape: %s: invalid max_speed "
1667 "(assuming 650KB/sec)\n", drive->name);
Harvey Harrisoncd740ab2008-07-24 22:53:33 +02001668 *(u16 *)&caps[8] = 650;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001669 }
1670
Borislav Petkovb6422012008-02-02 19:56:49 +01001671 memcpy(&tape->caps, caps, 20);
Bartlomiej Zolnierkiewicz05780422008-10-10 22:39:38 +02001672
1673 /* device lacks locking support according to capabilities page */
1674 if ((caps[6] & 1) == 0)
Bartlomiej Zolnierkiewicz42619d32008-10-17 18:09:11 +02001675 drive->dev_flags &= ~IDE_DFLAG_DOORLOCKING;
Bartlomiej Zolnierkiewicz05780422008-10-10 22:39:38 +02001676
Borislav Petkovb6422012008-02-02 19:56:49 +01001677 if (caps[7] & 0x02)
Borislav Petkov54bb2072008-02-06 02:57:52 +01001678 tape->blk_size = 512;
Borislav Petkovb6422012008-02-02 19:56:49 +01001679 else if (caps[7] & 0x04)
Borislav Petkov54bb2072008-02-06 02:57:52 +01001680 tape->blk_size = 1024;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681}
1682
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +02001683#ifdef CONFIG_IDE_PROC_FS
Bartlomiej Zolnierkiewicz8185d5a2008-10-10 22:39:28 +02001684#define ide_tape_devset_get(name, field) \
1685static int get_##name(ide_drive_t *drive) \
1686{ \
1687 idetape_tape_t *tape = drive->driver_data; \
1688 return tape->field; \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001689}
Bartlomiej Zolnierkiewicz8185d5a2008-10-10 22:39:28 +02001690
1691#define ide_tape_devset_set(name, field) \
1692static int set_##name(ide_drive_t *drive, int arg) \
1693{ \
1694 idetape_tape_t *tape = drive->driver_data; \
1695 tape->field = arg; \
1696 return 0; \
1697}
1698
Elias Oltmanns92f1f8f2008-10-10 22:39:40 +02001699#define ide_tape_devset_rw_field(_name, _field) \
Bartlomiej Zolnierkiewicz8185d5a2008-10-10 22:39:28 +02001700ide_tape_devset_get(_name, _field) \
1701ide_tape_devset_set(_name, _field) \
Elias Oltmanns92f1f8f2008-10-10 22:39:40 +02001702IDE_DEVSET(_name, DS_SYNC, get_##_name, set_##_name)
Bartlomiej Zolnierkiewicz8185d5a2008-10-10 22:39:28 +02001703
Elias Oltmanns92f1f8f2008-10-10 22:39:40 +02001704#define ide_tape_devset_r_field(_name, _field) \
Bartlomiej Zolnierkiewicz8185d5a2008-10-10 22:39:28 +02001705ide_tape_devset_get(_name, _field) \
Elias Oltmanns92f1f8f2008-10-10 22:39:40 +02001706IDE_DEVSET(_name, 0, get_##_name, NULL)
Bartlomiej Zolnierkiewicz8185d5a2008-10-10 22:39:28 +02001707
1708static int mulf_tdsc(ide_drive_t *drive) { return 1000; }
1709static int divf_tdsc(ide_drive_t *drive) { return HZ; }
1710static int divf_buffer(ide_drive_t *drive) { return 2; }
1711static int divf_buffer_size(ide_drive_t *drive) { return 1024; }
1712
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +02001713ide_devset_rw_flag(dsc_overlap, IDE_DFLAG_DSC_OVERLAP);
Bartlomiej Zolnierkiewicz8185d5a2008-10-10 22:39:28 +02001714
Elias Oltmanns92f1f8f2008-10-10 22:39:40 +02001715ide_tape_devset_rw_field(tdsc, best_dsc_rw_freq);
Bartlomiej Zolnierkiewicz8185d5a2008-10-10 22:39:28 +02001716
Elias Oltmanns92f1f8f2008-10-10 22:39:40 +02001717ide_tape_devset_r_field(avg_speed, avg_speed);
1718ide_tape_devset_r_field(speed, caps[14]);
1719ide_tape_devset_r_field(buffer, caps[16]);
1720ide_tape_devset_r_field(buffer_size, buffer_size);
Bartlomiej Zolnierkiewicz8185d5a2008-10-10 22:39:28 +02001721
Elias Oltmanns92f1f8f2008-10-10 22:39:40 +02001722static const struct ide_proc_devset idetape_settings[] = {
1723 __IDE_PROC_DEVSET(avg_speed, 0, 0xffff, NULL, NULL),
1724 __IDE_PROC_DEVSET(buffer, 0, 0xffff, NULL, divf_buffer),
1725 __IDE_PROC_DEVSET(buffer_size, 0, 0xffff, NULL, divf_buffer_size),
Elias Oltmanns92f1f8f2008-10-10 22:39:40 +02001726 __IDE_PROC_DEVSET(dsc_overlap, 0, 1, NULL, NULL),
1727 __IDE_PROC_DEVSET(speed, 0, 0xffff, NULL, NULL),
1728 __IDE_PROC_DEVSET(tdsc, IDETAPE_DSC_RW_MIN, IDETAPE_DSC_RW_MAX,
1729 mulf_tdsc, divf_tdsc),
Hannes Eder71bfc7a2009-03-05 16:10:56 +01001730 { NULL },
Bartlomiej Zolnierkiewicz8185d5a2008-10-10 22:39:28 +02001731};
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +02001732#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001733
1734/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001735 * The function below is called to:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001736 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001737 * 1. Initialize our various state variables.
1738 * 2. Ask the tape for its capabilities.
1739 * 3. Allocate a buffer which will be used for data transfer. The buffer size
1740 * is chosen based on the recommendation which we received in step 2.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001741 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001742 * Note that at this point ide.c already assigned us an irq, so that we can
1743 * queue requests here and wait for their completion.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001744 */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001745static void idetape_setup(ide_drive_t *drive, idetape_tape_t *tape, int minor)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001746{
Borislav Petkov83042b22008-04-27 15:38:27 +02001747 unsigned long t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001748 int speed;
Borislav Petkovf73850a2008-04-27 15:38:33 +02001749 int buffer_size;
Borislav Petkovb6422012008-02-02 19:56:49 +01001750 u16 *ctl = (u16 *)&tape->caps[12];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001751
Borislav Petkove972d702009-07-17 23:55:16 +00001752 ide_debug_log(IDE_DBG_FUNC, "minor: %d", minor);
1753
1754 drive->pc_callback = ide_tape_callback;
Borislav Petkov776bb022008-07-23 19:55:59 +02001755
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +02001756 drive->dev_flags |= IDE_DFLAG_DSC_OVERLAP;
1757
Bartlomiej Zolnierkiewicz4166c192008-02-01 23:09:30 +01001758 if (drive->hwif->host_flags & IDE_HFLAG_NO_DSC) {
1759 printk(KERN_INFO "ide-tape: %s: disabling DSC overlap\n",
1760 tape->name);
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +02001761 drive->dev_flags &= ~IDE_DFLAG_DSC_OVERLAP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001762 }
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +02001763
Linus Torvalds1da177e2005-04-16 15:20:36 -07001764 /* Seagate Travan drives do not support DSC overlap. */
Bartlomiej Zolnierkiewicz4dde4492008-10-10 22:39:19 +02001765 if (strstr((char *)&drive->id[ATA_ID_PROD], "Seagate STT3401"))
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +02001766 drive->dev_flags &= ~IDE_DFLAG_DSC_OVERLAP;
1767
Linus Torvalds1da177e2005-04-16 15:20:36 -07001768 tape->minor = minor;
1769 tape->name[0] = 'h';
1770 tape->name[1] = 't';
1771 tape->name[2] = '0' + minor;
Borislav Petkov54abf372008-02-06 02:57:52 +01001772 tape->chrdev_dir = IDETAPE_DIR_NONE;
Bartlomiej Zolnierkiewicz4dde4492008-10-10 22:39:19 +02001773
Linus Torvalds1da177e2005-04-16 15:20:36 -07001774 idetape_get_inquiry_results(drive);
1775 idetape_get_mode_sense_results(drive);
Borislav Petkov3cffb9c2008-02-02 19:56:50 +01001776 ide_tape_get_bsize_from_bdesc(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001777 tape->user_bs_factor = 1;
Borislav Petkovf73850a2008-04-27 15:38:33 +02001778 tape->buffer_size = *ctl * tape->blk_size;
1779 while (tape->buffer_size > 0xffff) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001780 printk(KERN_NOTICE "ide-tape: decreasing stage size\n");
Borislav Petkovb6422012008-02-02 19:56:49 +01001781 *ctl /= 2;
Borislav Petkovf73850a2008-04-27 15:38:33 +02001782 tape->buffer_size = *ctl * tape->blk_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001783 }
Borislav Petkovf73850a2008-04-27 15:38:33 +02001784 buffer_size = tape->buffer_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001785
Borislav Petkov83042b22008-04-27 15:38:27 +02001786 /* select the "best" DSC read/write polling freq */
Borislav Petkovb6422012008-02-02 19:56:49 +01001787 speed = max(*(u16 *)&tape->caps[14], *(u16 *)&tape->caps[8]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788
Borislav Petkovf73850a2008-04-27 15:38:33 +02001789 t = (IDETAPE_FIFO_THRESHOLD * tape->buffer_size * HZ) / (speed * 1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790
1791 /*
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001792 * Ensure that the number we got makes sense; limit it within
1793 * IDETAPE_DSC_RW_MIN and IDETAPE_DSC_RW_MAX.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001794 */
Harvey Harrisona792bd52008-07-15 21:21:41 +02001795 tape->best_dsc_rw_freq = clamp_t(unsigned long, t, IDETAPE_DSC_RW_MIN,
1796 IDETAPE_DSC_RW_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001797 printk(KERN_INFO "ide-tape: %s <-> %s: %dKBps, %d*%dkB buffer, "
Borislav Petkov83042b22008-04-27 15:38:27 +02001798 "%lums tDSC%s\n",
Borislav Petkovb6422012008-02-02 19:56:49 +01001799 drive->name, tape->name, *(u16 *)&tape->caps[14],
Borislav Petkovf73850a2008-04-27 15:38:33 +02001800 (*(u16 *)&tape->caps[16] * 512) / tape->buffer_size,
1801 tape->buffer_size / 1024,
Borislav Petkov54bb2072008-02-06 02:57:52 +01001802 tape->best_dsc_rw_freq * 1000 / HZ,
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +02001803 (drive->dev_flags & IDE_DFLAG_USING_DMA) ? ", DMA" : "");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001804
Bartlomiej Zolnierkiewicz1e874f42008-10-10 22:39:27 +02001805 ide_proc_register_driver(drive, tape->driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001806}
1807
Russell King4031bbe2006-01-06 11:41:00 +00001808static void ide_tape_remove(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001809{
1810 idetape_tape_t *tape = drive->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001811
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +02001812 ide_proc_unregister_driver(drive, tape->driver);
Bartlomiej Zolnierkiewicz8fed4362009-02-25 20:28:24 +01001813 device_del(&tape->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001814 ide_unregister_region(tape->disk);
1815
Bartlomiej Zolnierkiewicz8fed4362009-02-25 20:28:24 +01001816 mutex_lock(&idetape_ref_mutex);
1817 put_device(&tape->dev);
1818 mutex_unlock(&idetape_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001819}
1820
Bartlomiej Zolnierkiewicz8fed4362009-02-25 20:28:24 +01001821static void ide_tape_release(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001822{
Bartlomiej Zolnierkiewicz8fed4362009-02-25 20:28:24 +01001823 struct ide_tape_obj *tape = to_ide_drv(dev, ide_tape_obj);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001824 ide_drive_t *drive = tape->drive;
1825 struct gendisk *g = tape->disk;
1826
Tejun Heo963da552009-04-19 08:46:02 +09001827 BUG_ON(tape->valid);
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02001828
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +02001829 drive->dev_flags &= ~IDE_DFLAG_DSC_OVERLAP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001830 drive->driver_data = NULL;
Tony Jonesdbc12722007-09-25 02:03:03 +02001831 device_destroy(idetape_sysfs_class, MKDEV(IDETAPE_MAJOR, tape->minor));
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001832 device_destroy(idetape_sysfs_class,
1833 MKDEV(IDETAPE_MAJOR, tape->minor + 128));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001834 idetape_devs[tape->minor] = NULL;
1835 g->private_data = NULL;
1836 put_disk(g);
1837 kfree(tape);
1838}
1839
Bartlomiej Zolnierkiewiczecfd80e2007-05-10 00:01:09 +02001840#ifdef CONFIG_IDE_PROC_FS
Alexey Dobriyan6d703a82009-09-01 17:52:57 -07001841static int idetape_name_proc_show(struct seq_file *m, void *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001842{
Alexey Dobriyan6d703a82009-09-01 17:52:57 -07001843 ide_drive_t *drive = (ide_drive_t *) m->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001844 idetape_tape_t *tape = drive->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001845
Alexey Dobriyan6d703a82009-09-01 17:52:57 -07001846 seq_printf(m, "%s\n", tape->name);
1847 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001848}
1849
Alexey Dobriyan6d703a82009-09-01 17:52:57 -07001850static int idetape_name_proc_open(struct inode *inode, struct file *file)
1851{
1852 return single_open(file, idetape_name_proc_show, PDE(inode)->data);
1853}
1854
1855static const struct file_operations idetape_name_proc_fops = {
1856 .owner = THIS_MODULE,
1857 .open = idetape_name_proc_open,
1858 .read = seq_read,
1859 .llseek = seq_lseek,
1860 .release = single_release,
1861};
1862
Linus Torvalds1da177e2005-04-16 15:20:36 -07001863static ide_proc_entry_t idetape_proc[] = {
Alexey Dobriyan6d703a82009-09-01 17:52:57 -07001864 { "capacity", S_IFREG|S_IRUGO, &ide_capacity_proc_fops },
1865 { "name", S_IFREG|S_IRUGO, &idetape_name_proc_fops },
1866 {}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001867};
Bartlomiej Zolnierkiewicz79cb3802008-10-17 18:09:13 +02001868
1869static ide_proc_entry_t *ide_tape_proc_entries(ide_drive_t *drive)
1870{
1871 return idetape_proc;
1872}
1873
1874static const struct ide_proc_devset *ide_tape_proc_devsets(ide_drive_t *drive)
1875{
1876 return idetape_settings;
1877}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001878#endif
1879
Russell King4031bbe2006-01-06 11:41:00 +00001880static int ide_tape_probe(ide_drive_t *);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001881
Bartlomiej Zolnierkiewicz7f3c8682009-01-06 17:20:53 +01001882static struct ide_driver idetape_driver = {
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02001883 .gen_driver = {
Laurent Riffard4ef3b8f2005-11-18 22:15:40 +01001884 .owner = THIS_MODULE,
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02001885 .name = "ide-tape",
1886 .bus = &ide_bus_type,
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02001887 },
Russell King4031bbe2006-01-06 11:41:00 +00001888 .probe = ide_tape_probe,
1889 .remove = ide_tape_remove,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001890 .version = IDETAPE_VERSION,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001891 .do_request = idetape_do_request,
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +02001892#ifdef CONFIG_IDE_PROC_FS
Bartlomiej Zolnierkiewicz79cb3802008-10-17 18:09:13 +02001893 .proc_entries = ide_tape_proc_entries,
1894 .proc_devsets = ide_tape_proc_devsets,
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +02001895#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001896};
1897
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001898/* Our character device supporting functions, passed to register_chrdev. */
Arjan van de Ven2b8693c2007-02-12 00:55:32 -08001899static const struct file_operations idetape_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001900 .owner = THIS_MODULE,
1901 .read = idetape_chrdev_read,
1902 .write = idetape_chrdev_write,
Alan Cox05227ad2010-01-04 06:24:00 +00001903 .unlocked_ioctl = idetape_chrdev_ioctl,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001904 .open = idetape_chrdev_open,
1905 .release = idetape_chrdev_release,
1906};
1907
Al Viroa4600f82008-03-02 10:27:58 -05001908static int idetape_open(struct block_device *bdev, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001909{
Borislav Petkov9d01e4c2009-06-08 22:03:03 +02001910 struct ide_tape_obj *tape = ide_tape_get(bdev->bd_disk, false, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001911
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001912 if (!tape)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001913 return -ENXIO;
1914
Linus Torvalds1da177e2005-04-16 15:20:36 -07001915 return 0;
1916}
1917
Al Viroa4600f82008-03-02 10:27:58 -05001918static int idetape_release(struct gendisk *disk, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001919{
Borislav Petkov5aeddf92008-10-13 21:39:34 +02001920 struct ide_tape_obj *tape = ide_drv_g(disk, ide_tape_obj);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001921
1922 ide_tape_put(tape);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001923 return 0;
1924}
1925
Al Viroa4600f82008-03-02 10:27:58 -05001926static int idetape_ioctl(struct block_device *bdev, fmode_t mode,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001927 unsigned int cmd, unsigned long arg)
1928{
Borislav Petkov5aeddf92008-10-13 21:39:34 +02001929 struct ide_tape_obj *tape = ide_drv_g(bdev->bd_disk, ide_tape_obj);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001930 ide_drive_t *drive = tape->drive;
Arnd Bergmann8a6cfeb2010-07-08 10:18:46 +02001931 int err;
1932
1933 lock_kernel();
1934 err = generic_ide_ioctl(drive, bdev, cmd, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001935 if (err == -EINVAL)
1936 err = idetape_blkdev_ioctl(drive, cmd, arg);
Arnd Bergmann8a6cfeb2010-07-08 10:18:46 +02001937 unlock_kernel();
1938
Linus Torvalds1da177e2005-04-16 15:20:36 -07001939 return err;
1940}
1941
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -07001942static const struct block_device_operations idetape_block_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001943 .owner = THIS_MODULE,
Al Viroa4600f82008-03-02 10:27:58 -05001944 .open = idetape_open,
1945 .release = idetape_release,
Arnd Bergmann8a6cfeb2010-07-08 10:18:46 +02001946 .ioctl = idetape_ioctl,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001947};
1948
Russell King4031bbe2006-01-06 11:41:00 +00001949static int ide_tape_probe(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001950{
1951 idetape_tape_t *tape;
1952 struct gendisk *g;
1953 int minor;
1954
Borislav Petkove972d702009-07-17 23:55:16 +00001955 ide_debug_log(IDE_DBG_FUNC, "enter");
1956
1957 if (!strstr(DRV_NAME, drive->driver_req))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001958 goto failed;
Bartlomiej Zolnierkiewicz2a924662008-10-10 22:39:24 +02001959
Linus Torvalds1da177e2005-04-16 15:20:36 -07001960 if (drive->media != ide_tape)
1961 goto failed;
Bartlomiej Zolnierkiewicz2a924662008-10-10 22:39:24 +02001962
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +02001963 if ((drive->dev_flags & IDE_DFLAG_ID_READ) &&
1964 ide_check_atapi_device(drive, DRV_NAME) == 0) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001965 printk(KERN_ERR "ide-tape: %s: not supported by this version of"
1966 " the driver\n", drive->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001967 goto failed;
1968 }
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001969 tape = kzalloc(sizeof(idetape_tape_t), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001970 if (tape == NULL) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001971 printk(KERN_ERR "ide-tape: %s: Can't allocate a tape struct\n",
1972 drive->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001973 goto failed;
1974 }
1975
1976 g = alloc_disk(1 << PARTN_BITS);
1977 if (!g)
1978 goto out_free_tape;
1979
1980 ide_init_disk(g, drive);
1981
Bartlomiej Zolnierkiewicz8fed4362009-02-25 20:28:24 +01001982 tape->dev.parent = &drive->gendev;
1983 tape->dev.release = ide_tape_release;
1984 dev_set_name(&tape->dev, dev_name(&drive->gendev));
1985
1986 if (device_register(&tape->dev))
1987 goto out_free_disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001988
1989 tape->drive = drive;
1990 tape->driver = &idetape_driver;
1991 tape->disk = g;
1992
1993 g->private_data = &tape->driver;
1994
1995 drive->driver_data = tape;
1996
Arjan van de Vencf8b8972006-03-23 03:00:45 -08001997 mutex_lock(&idetape_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001998 for (minor = 0; idetape_devs[minor]; minor++)
1999 ;
2000 idetape_devs[minor] = tape;
Arjan van de Vencf8b8972006-03-23 03:00:45 -08002001 mutex_unlock(&idetape_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002002
2003 idetape_setup(drive, tape, minor);
2004
Greg Kroah-Hartman3ee074b2008-07-21 20:03:34 -07002005 device_create(idetape_sysfs_class, &drive->gendev,
2006 MKDEV(IDETAPE_MAJOR, minor), NULL, "%s", tape->name);
2007 device_create(idetape_sysfs_class, &drive->gendev,
2008 MKDEV(IDETAPE_MAJOR, minor + 128), NULL,
2009 "n%s", tape->name);
Will Dysond5dee802005-09-16 02:55:07 -07002010
Linus Torvalds1da177e2005-04-16 15:20:36 -07002011 g->fops = &idetape_block_ops;
2012 ide_register_region(g);
2013
2014 return 0;
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02002015
Bartlomiej Zolnierkiewicz8fed4362009-02-25 20:28:24 +01002016out_free_disk:
2017 put_disk(g);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002018out_free_tape:
2019 kfree(tape);
2020failed:
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02002021 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002022}
2023
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002024static void __exit idetape_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002025{
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02002026 driver_unregister(&idetape_driver.gen_driver);
Will Dysond5dee802005-09-16 02:55:07 -07002027 class_destroy(idetape_sysfs_class);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002028 unregister_chrdev(IDETAPE_MAJOR, "ht");
2029}
2030
Bartlomiej Zolnierkiewicz17514e82005-11-19 22:24:35 +01002031static int __init idetape_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002032{
Will Dysond5dee802005-09-16 02:55:07 -07002033 int error = 1;
2034 idetape_sysfs_class = class_create(THIS_MODULE, "ide_tape");
2035 if (IS_ERR(idetape_sysfs_class)) {
2036 idetape_sysfs_class = NULL;
2037 printk(KERN_ERR "Unable to create sysfs class for ide tapes\n");
2038 error = -EBUSY;
2039 goto out;
2040 }
2041
Linus Torvalds1da177e2005-04-16 15:20:36 -07002042 if (register_chrdev(IDETAPE_MAJOR, "ht", &idetape_fops)) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002043 printk(KERN_ERR "ide-tape: Failed to register chrdev"
2044 " interface\n");
Will Dysond5dee802005-09-16 02:55:07 -07002045 error = -EBUSY;
2046 goto out_free_class;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002047 }
Will Dysond5dee802005-09-16 02:55:07 -07002048
2049 error = driver_register(&idetape_driver.gen_driver);
2050 if (error)
2051 goto out_free_driver;
2052
2053 return 0;
2054
2055out_free_driver:
2056 driver_unregister(&idetape_driver.gen_driver);
2057out_free_class:
2058 class_destroy(idetape_sysfs_class);
2059out:
2060 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002061}
2062
Kay Sievers263756e2005-12-12 18:03:44 +01002063MODULE_ALIAS("ide:*m-tape*");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002064module_init(idetape_init);
2065module_exit(idetape_exit);
2066MODULE_ALIAS_CHARDEV_MAJOR(IDETAPE_MAJOR);
Borislav Petkov9c145762008-02-06 02:57:54 +01002067MODULE_DESCRIPTION("ATAPI Streaming TAPE Driver");
2068MODULE_LICENSE("GPL");