blob: 7ecb1ade8874b86c293a5d1a4a2a59ffc7efc860 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Borislav Petkov5ce78af2008-02-02 19:56:48 +01002 * IDE ATAPI streaming tape driver.
3 *
Bartlomiej Zolnierkiewicz59bca8c2008-02-01 23:09:33 +01004 * Copyright (C) 1995-1999 Gadi Oxman <gadio@netvision.net.il>
5 * Copyright (C) 2003-2005 Bartlomiej Zolnierkiewicz
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 * This driver was constructed as a student project in the software laboratory
8 * of the faculty of electrical engineering in the Technion - Israel's
9 * Institute Of Technology, with the guide of Avner Lottem and Dr. Ilana David.
10 *
11 * It is hereby placed under the terms of the GNU general public license.
12 * (See linux/COPYING).
Linus Torvalds1da177e2005-04-16 15:20:36 -070013 *
Borislav Petkov5ce78af2008-02-02 19:56:48 +010014 * For a historical changelog see
15 * Documentation/ide/ChangeLog.ide-tape.1995-2002
Linus Torvalds1da177e2005-04-16 15:20:36 -070016 */
17
Bartlomiej Zolnierkiewicz51509ee2008-10-10 22:39:34 +020018#define DRV_NAME "ide-tape"
19
Borislav Petkovdfe79932008-02-06 02:57:55 +010020#define IDETAPE_VERSION "1.20"
Linus Torvalds1da177e2005-04-16 15:20:36 -070021
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include <linux/module.h>
23#include <linux/types.h>
24#include <linux/string.h>
25#include <linux/kernel.h>
26#include <linux/delay.h>
27#include <linux/timer.h>
28#include <linux/mm.h>
29#include <linux/interrupt.h>
Marcelo Feitoza Parisi9bae1ff2006-03-28 01:56:46 -080030#include <linux/jiffies.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#include <linux/major.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070032#include <linux/errno.h>
33#include <linux/genhd.h>
Alexey Dobriyan6d703a82009-09-01 17:52:57 -070034#include <linux/seq_file.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070035#include <linux/slab.h>
36#include <linux/pci.h>
37#include <linux/ide.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070038#include <linux/completion.h>
39#include <linux/bitops.h>
Arjan van de Vencf8b8972006-03-23 03:00:45 -080040#include <linux/mutex.h>
Borislav Petkov90699ce2008-02-02 19:56:50 +010041#include <scsi/scsi.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070042
43#include <asm/byteorder.h>
Borislav Petkovc837cfa2008-02-06 02:57:54 +010044#include <linux/irq.h>
45#include <linux/uaccess.h>
46#include <linux/io.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070047#include <asm/unaligned.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070048#include <linux/mtio.h>
49
Borislav Petkov8004a8c2008-02-06 02:57:51 +010050/* define to see debug info */
Borislav Petkove972d702009-07-17 23:55:16 +000051#undef IDETAPE_DEBUG_LOG
Borislav Petkov8004a8c2008-02-06 02:57:51 +010052
Borislav Petkove972d702009-07-17 23:55:16 +000053#ifdef IDETAPE_DEBUG_LOG
54#define ide_debug_log(lvl, fmt, args...) __ide_debug_log(lvl, fmt, ## args)
Borislav Petkov8004a8c2008-02-06 02:57:51 +010055#else
Borislav Petkove972d702009-07-17 23:55:16 +000056#define ide_debug_log(lvl, fmt, args...) do {} while (0)
Borislav Petkov8004a8c2008-02-06 02:57:51 +010057#endif
58
Linus Torvalds1da177e2005-04-16 15:20:36 -070059/**************************** Tunable parameters *****************************/
Linus Torvalds1da177e2005-04-16 15:20:36 -070060/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +010061 * After each failed packet command we issue a request sense command and retry
62 * the packet command IDETAPE_MAX_PC_RETRIES times.
Linus Torvalds1da177e2005-04-16 15:20:36 -070063 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +010064 * Setting IDETAPE_MAX_PC_RETRIES to 0 will disable retries.
Linus Torvalds1da177e2005-04-16 15:20:36 -070065 */
66#define IDETAPE_MAX_PC_RETRIES 3
67
68/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +010069 * The following parameter is used to select the point in the internal tape fifo
70 * in which we will start to refill the buffer. Decreasing the following
71 * parameter will improve the system's latency and interactive response, while
72 * using a high value might improve system throughput.
Linus Torvalds1da177e2005-04-16 15:20:36 -070073 */
Borislav Petkov3c98bf32008-02-06 02:57:53 +010074#define IDETAPE_FIFO_THRESHOLD 2
Linus Torvalds1da177e2005-04-16 15:20:36 -070075
76/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +010077 * DSC polling parameters.
Linus Torvalds1da177e2005-04-16 15:20:36 -070078 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +010079 * Polling for DSC (a single bit in the status register) is a very important
80 * function in ide-tape. There are two cases in which we poll for DSC:
Linus Torvalds1da177e2005-04-16 15:20:36 -070081 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +010082 * 1. Before a read/write packet command, to ensure that we can transfer data
83 * from/to the tape's data buffers, without causing an actual media access.
84 * In case the tape is not ready yet, we take out our request from the device
85 * request queue, so that ide.c could service requests from the other device
86 * on the same interface in the meantime.
Linus Torvalds1da177e2005-04-16 15:20:36 -070087 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +010088 * 2. After the successful initialization of a "media access packet command",
89 * which is a command that can take a long time to complete (the interval can
90 * range from several seconds to even an hour). Again, we postpone our request
91 * in the middle to free the bus for the other device. The polling frequency
92 * here should be lower than the read/write frequency since those media access
93 * commands are slow. We start from a "fast" frequency - IDETAPE_DSC_MA_FAST
94 * (1 second), and if we don't receive DSC after IDETAPE_DSC_MA_THRESHOLD
95 * (5 min), we switch it to a lower frequency - IDETAPE_DSC_MA_SLOW (1 min).
Linus Torvalds1da177e2005-04-16 15:20:36 -070096 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +010097 * We also set a timeout for the timer, in case something goes wrong. The
98 * timeout should be longer then the maximum execution time of a tape operation.
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 */
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100100
101/* DSC timings. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102#define IDETAPE_DSC_RW_MIN 5*HZ/100 /* 50 msec */
103#define IDETAPE_DSC_RW_MAX 40*HZ/100 /* 400 msec */
104#define IDETAPE_DSC_RW_TIMEOUT 2*60*HZ /* 2 minutes */
105#define IDETAPE_DSC_MA_FAST 2*HZ /* 2 seconds */
106#define IDETAPE_DSC_MA_THRESHOLD 5*60*HZ /* 5 minutes */
107#define IDETAPE_DSC_MA_SLOW 30*HZ /* 30 seconds */
108#define IDETAPE_DSC_MA_TIMEOUT 2*60*60*HZ /* 2 hours */
109
110/*************************** End of tunable parameters ***********************/
111
Borislav Petkov54abf372008-02-06 02:57:52 +0100112/* tape directions */
113enum {
114 IDETAPE_DIR_NONE = (1 << 0),
115 IDETAPE_DIR_READ = (1 << 1),
116 IDETAPE_DIR_WRITE = (1 << 2),
117};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118
Borislav Petkov03056b92008-04-18 00:46:26 +0200119/* Tape door status */
120#define DOOR_UNLOCKED 0
121#define DOOR_LOCKED 1
122#define DOOR_EXPLICITLY_LOCKED 2
123
124/* Some defines for the SPACE command */
125#define IDETAPE_SPACE_OVER_FILEMARK 1
126#define IDETAPE_SPACE_TO_EOD 3
127
128/* Some defines for the LOAD UNLOAD command */
129#define IDETAPE_LU_LOAD_MASK 1
130#define IDETAPE_LU_RETENSION_MASK 2
131#define IDETAPE_LU_EOT_MASK 4
132
Borislav Petkov03056b92008-04-18 00:46:26 +0200133/* Structures related to the SELECT SENSE / MODE SENSE packet commands. */
134#define IDETAPE_BLOCK_DESCRIPTOR 0
135#define IDETAPE_CAPABILITIES_PAGE 0x2a
136
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100138 * Most of our global data which we need to save even as we leave the driver due
139 * to an interrupt or a timer event is stored in the struct defined below.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 */
141typedef struct ide_tape_obj {
Bartlomiej Zolnierkiewicz7f3c8682009-01-06 17:20:53 +0100142 ide_drive_t *drive;
143 struct ide_driver *driver;
144 struct gendisk *disk;
Bartlomiej Zolnierkiewicz8fed4362009-02-25 20:28:24 +0100145 struct device dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146
Bartlomiej Zolnierkiewicz2e8a6f82008-10-10 22:39:36 +0200147 /* used by REQ_IDETAPE_{READ,WRITE} requests */
148 struct ide_atapi_pc queued_pc;
Bartlomiej Zolnierkiewicz394a4c22008-10-10 22:39:35 +0200149
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 /*
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100151 * DSC polling variables.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100153 * While polling for DSC we use postponed_rq to postpone the current
154 * request so that ide.c will be able to service pending requests on the
155 * other device. Note that at most we will have only one DSC (usually
Borislav Petkov5bd50dc2008-04-27 15:38:28 +0200156 * data transfer) request in the device request queue.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 */
Borislav Petkov6f3848a2009-07-21 23:08:23 -0700158 bool postponed_rq;
159
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 /* The time in which we started polling for DSC */
161 unsigned long dsc_polling_start;
162 /* Timer used to poll for dsc */
163 struct timer_list dsc_timer;
164 /* Read/Write dsc polling frequency */
Borislav Petkov54bb2072008-02-06 02:57:52 +0100165 unsigned long best_dsc_rw_freq;
166 unsigned long dsc_poll_freq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 unsigned long dsc_timeout;
168
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100169 /* Read position information */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 u8 partition;
171 /* Current block */
Borislav Petkov54bb2072008-02-06 02:57:52 +0100172 unsigned int first_frame;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100174 /* Last error information */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 u8 sense_key, asc, ascq;
176
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100177 /* Character device operation */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 unsigned int minor;
179 /* device name */
180 char name[4];
181 /* Current character device data transfer direction */
Borislav Petkov54abf372008-02-06 02:57:52 +0100182 u8 chrdev_dir;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183
Borislav Petkov54bb2072008-02-06 02:57:52 +0100184 /* tape block size, usually 512 or 1024 bytes */
185 unsigned short blk_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 int user_bs_factor;
Borislav Petkovb6422012008-02-02 19:56:49 +0100187
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 /* Copy of the tape's Capabilities and Mechanical Page */
Borislav Petkovb6422012008-02-02 19:56:49 +0100189 u8 caps[20];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190
191 /*
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100192 * Active data transfer request parameters.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100194 * At most, there is only one ide-tape originated data transfer request
195 * in the device request queue. This allows ide.c to easily service
196 * requests from the other device when we postpone our active request.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 */
Borislav Petkov83042b22008-04-27 15:38:27 +0200198
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100199 /* Data buffer size chosen based on the tape's recommendation */
Borislav Petkovf73850a2008-04-27 15:38:33 +0200200 int buffer_size;
Tejun Heo963da552009-04-19 08:46:02 +0900201 /* Staging buffer of buffer_size bytes */
202 void *buf;
203 /* The read/write cursor */
204 void *cur;
205 /* The number of valid bytes in buf */
206 size_t valid;
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100207
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100208 /* Measures average tape speed */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 unsigned long avg_time;
210 int avg_size;
211 int avg_speed;
212
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 /* the door is currently locked */
214 int door_locked;
215 /* the tape hardware is write protected */
216 char drv_write_prot;
217 /* the tape is write protected (hardware or opened as read-only) */
218 char write_prot;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219} idetape_tape_t;
220
Arnd Bergmann2a48fc02010-06-02 14:28:52 +0200221static DEFINE_MUTEX(ide_tape_mutex);
Arjan van de Vencf8b8972006-03-23 03:00:45 -0800222static DEFINE_MUTEX(idetape_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223
Borislav Petkovcbba2fa2009-10-11 00:25:19 +0000224static DEFINE_MUTEX(idetape_chrdev_mutex);
225
Will Dysond5dee802005-09-16 02:55:07 -0700226static struct class *idetape_sysfs_class;
227
Bartlomiej Zolnierkiewicz8fed4362009-02-25 20:28:24 +0100228static void ide_tape_release(struct device *);
Bartlomiej Zolnierkiewicz08da5912008-07-24 22:53:15 +0200229
Borislav Petkov9d01e4c2009-06-08 22:03:03 +0200230static struct ide_tape_obj *idetape_devs[MAX_HWIFS * MAX_DRIVES];
231
232static struct ide_tape_obj *ide_tape_get(struct gendisk *disk, bool cdev,
233 unsigned int i)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234{
235 struct ide_tape_obj *tape = NULL;
236
Arjan van de Vencf8b8972006-03-23 03:00:45 -0800237 mutex_lock(&idetape_ref_mutex);
Borislav Petkov9d01e4c2009-06-08 22:03:03 +0200238
239 if (cdev)
240 tape = idetape_devs[i];
241 else
242 tape = ide_drv_g(disk, ide_tape_obj);
243
Bartlomiej Zolnierkiewicz08da5912008-07-24 22:53:15 +0200244 if (tape) {
Bartlomiej Zolnierkiewiczd3e33ff2008-08-05 18:16:59 +0200245 if (ide_device_get(tape->drive))
Bartlomiej Zolnierkiewicz08da5912008-07-24 22:53:15 +0200246 tape = NULL;
Bartlomiej Zolnierkiewiczd3e33ff2008-08-05 18:16:59 +0200247 else
Bartlomiej Zolnierkiewicz8fed4362009-02-25 20:28:24 +0100248 get_device(&tape->dev);
Bartlomiej Zolnierkiewicz08da5912008-07-24 22:53:15 +0200249 }
Borislav Petkov9d01e4c2009-06-08 22:03:03 +0200250
Arjan van de Vencf8b8972006-03-23 03:00:45 -0800251 mutex_unlock(&idetape_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 return tape;
253}
254
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255static void ide_tape_put(struct ide_tape_obj *tape)
256{
Bartlomiej Zolnierkiewiczd3e33ff2008-08-05 18:16:59 +0200257 ide_drive_t *drive = tape->drive;
258
Arjan van de Vencf8b8972006-03-23 03:00:45 -0800259 mutex_lock(&idetape_ref_mutex);
Bartlomiej Zolnierkiewicz8fed4362009-02-25 20:28:24 +0100260 put_device(&tape->dev);
Bartlomiej Zolnierkiewiczd3e33ff2008-08-05 18:16:59 +0200261 ide_device_put(drive);
Arjan van de Vencf8b8972006-03-23 03:00:45 -0800262 mutex_unlock(&idetape_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263}
264
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265/*
Borislav Petkov1b5db432008-02-02 19:56:48 +0100266 * called on each failed packet command retry to analyze the request sense. We
267 * currently do not utilize this information.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 */
Borislav Petkovae3a8382009-05-02 10:58:17 +0200269static void idetape_analyze_error(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270{
271 idetape_tape_t *tape = drive->driver_data;
Bartlomiej Zolnierkiewicz5e2040fd2009-03-27 12:46:34 +0100272 struct ide_atapi_pc *pc = drive->failed_pc;
Borislav Petkovdfb7e622009-05-01 20:35:21 +0200273 struct request *rq = drive->hwif->rq;
Borislav Petkovae3a8382009-05-02 10:58:17 +0200274 u8 *sense = bio_data(rq->bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275
Borislav Petkov1b5db432008-02-02 19:56:48 +0100276 tape->sense_key = sense[2] & 0xF;
277 tape->asc = sense[12];
278 tape->ascq = sense[13];
Borislav Petkov8004a8c2008-02-06 02:57:51 +0100279
Borislav Petkove972d702009-07-17 23:55:16 +0000280 ide_debug_log(IDE_DBG_FUNC,
281 "cmd: 0x%x, sense key = %x, asc = %x, ascq = %x",
282 rq->cmd[0], tape->sense_key, tape->asc, tape->ascq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283
Borislav Petkov077e6db2009-05-01 21:21:02 +0200284 /* correct remaining bytes to transfer */
Tejun Heo21d9c5d2009-04-19 08:46:02 +0900285 if (pc->flags & PC_FLAG_DMA_ERROR)
Borislav Petkov077e6db2009-05-01 21:21:02 +0200286 rq->resid_len = tape->blk_size * get_unaligned_be32(&sense[3]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287
288 /*
289 * If error was the result of a zero-length read or write command,
290 * with sense key=5, asc=0x22, ascq=0, let it slide. Some drives
291 * (i.e. Seagate STT3401A Travan) don't support 0-length read/writes.
292 */
Borislav Petkov90699ce2008-02-02 19:56:50 +0100293 if ((pc->c[0] == READ_6 || pc->c[0] == WRITE_6)
Borislav Petkov1b5db432008-02-02 19:56:48 +0100294 /* length == 0 */
295 && pc->c[4] == 0 && pc->c[3] == 0 && pc->c[2] == 0) {
296 if (tape->sense_key == 5) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 /* don't report an error, everything's ok */
298 pc->error = 0;
299 /* don't retry read/write */
Borislav Petkov346331f2008-04-18 00:46:26 +0200300 pc->flags |= PC_FLAG_ABORT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 }
302 }
Borislav Petkov90699ce2008-02-02 19:56:50 +0100303 if (pc->c[0] == READ_6 && (sense[2] & 0x80)) {
Bartlomiej Zolnierkiewiczc152cc12009-03-27 12:46:34 +0100304 pc->error = IDE_DRV_ERROR_FILEMARK;
Borislav Petkov346331f2008-04-18 00:46:26 +0200305 pc->flags |= PC_FLAG_ABORT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 }
Borislav Petkov90699ce2008-02-02 19:56:50 +0100307 if (pc->c[0] == WRITE_6) {
Borislav Petkov1b5db432008-02-02 19:56:48 +0100308 if ((sense[2] & 0x40) || (tape->sense_key == 0xd
309 && tape->asc == 0x0 && tape->ascq == 0x2)) {
Bartlomiej Zolnierkiewiczc152cc12009-03-27 12:46:34 +0100310 pc->error = IDE_DRV_ERROR_EOD;
Borislav Petkov346331f2008-04-18 00:46:26 +0200311 pc->flags |= PC_FLAG_ABORT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 }
313 }
Borislav Petkov90699ce2008-02-02 19:56:50 +0100314 if (pc->c[0] == READ_6 || pc->c[0] == WRITE_6) {
Borislav Petkov1b5db432008-02-02 19:56:48 +0100315 if (tape->sense_key == 8) {
Bartlomiej Zolnierkiewiczc152cc12009-03-27 12:46:34 +0100316 pc->error = IDE_DRV_ERROR_EOD;
Borislav Petkov346331f2008-04-18 00:46:26 +0200317 pc->flags |= PC_FLAG_ABORT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 }
Borislav Petkov346331f2008-04-18 00:46:26 +0200319 if (!(pc->flags & PC_FLAG_ABORT) &&
Borislav Petkov077e6db2009-05-01 21:21:02 +0200320 (blk_rq_bytes(rq) - rq->resid_len))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 pc->retries = IDETAPE_MAX_PC_RETRIES + 1;
322 }
323}
324
Bartlomiej Zolnierkiewiczb14c7212008-10-13 21:39:30 +0200325static void ide_tape_handle_dsc(ide_drive_t *);
326
Bartlomiej Zolnierkiewicz03a2faa2009-03-27 12:46:36 +0100327static int ide_tape_callback(ide_drive_t *drive, int dsc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328{
329 idetape_tape_t *tape = drive->driver_data;
Bartlomiej Zolnierkiewicz2b9efba2008-10-13 21:39:31 +0200330 struct ide_atapi_pc *pc = drive->pc;
Bartlomiej Zolnierkiewicz313afea2009-03-27 12:46:34 +0100331 struct request *rq = drive->hwif->rq;
Bartlomiej Zolnierkiewicz5985e6a2008-07-15 21:21:55 +0200332 int uptodate = pc->error ? 0 : 1;
Bartlomiej Zolnierkiewicz313afea2009-03-27 12:46:34 +0100333 int err = uptodate ? 0 : IDE_DRV_ERROR_GENERAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334
Borislav Petkove972d702009-07-17 23:55:16 +0000335 ide_debug_log(IDE_DBG_FUNC, "cmd: 0x%x, dsc: %d, err: %d", rq->cmd[0],
336 dsc, err);
Borislav Petkov8004a8c2008-02-06 02:57:51 +0100337
Bartlomiej Zolnierkiewiczb14c7212008-10-13 21:39:30 +0200338 if (dsc)
339 ide_tape_handle_dsc(drive);
340
Bartlomiej Zolnierkiewicz5e2040fd2009-03-27 12:46:34 +0100341 if (drive->failed_pc == pc)
342 drive->failed_pc = NULL;
Bartlomiej Zolnierkiewiczdd2e9a02008-07-15 21:22:01 +0200343
Bartlomiej Zolnierkiewicz5985e6a2008-07-15 21:21:55 +0200344 if (pc->c[0] == REQUEST_SENSE) {
345 if (uptodate)
Borislav Petkovae3a8382009-05-02 10:58:17 +0200346 idetape_analyze_error(drive);
Bartlomiej Zolnierkiewicz5985e6a2008-07-15 21:21:55 +0200347 else
348 printk(KERN_ERR "ide-tape: Error in REQUEST SENSE "
349 "itself - Aborting request!\n");
350 } else if (pc->c[0] == READ_6 || pc->c[0] == WRITE_6) {
Borislav Petkov077e6db2009-05-01 21:21:02 +0200351 unsigned int blocks =
352 (blk_rq_bytes(rq) - rq->resid_len) / tape->blk_size;
Bartlomiej Zolnierkiewicz5985e6a2008-07-15 21:21:55 +0200353
354 tape->avg_size += blocks * tape->blk_size;
355
356 if (time_after_eq(jiffies, tape->avg_time + HZ)) {
357 tape->avg_speed = tape->avg_size * HZ /
358 (jiffies - tape->avg_time) / 1024;
359 tape->avg_size = 0;
360 tape->avg_time = jiffies;
361 }
362
363 tape->first_frame += blocks;
Bartlomiej Zolnierkiewicz5985e6a2008-07-15 21:21:55 +0200364
Bartlomiej Zolnierkiewicz313afea2009-03-27 12:46:34 +0100365 if (pc->error) {
366 uptodate = 0;
367 err = pc->error;
368 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 }
Bartlomiej Zolnierkiewicz313afea2009-03-27 12:46:34 +0100370 rq->errors = err;
371
Bartlomiej Zolnierkiewicz03a2faa2009-03-27 12:46:36 +0100372 return uptodate;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373}
374
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100376 * Postpone the current request so that ide.c will be able to service requests
Bartlomiej Zolnierkiewiczb65fac32009-01-06 17:20:50 +0100377 * from another device on the same port while we are polling for DSC.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 */
Borislav Petkov6f3848a2009-07-21 23:08:23 -0700379static void ide_tape_stall_queue(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380{
381 idetape_tape_t *tape = drive->driver_data;
382
Borislav Petkove972d702009-07-17 23:55:16 +0000383 ide_debug_log(IDE_DBG_FUNC, "cmd: 0x%x, dsc_poll_freq: %lu",
Borislav Petkov6f3848a2009-07-21 23:08:23 -0700384 drive->hwif->rq->cmd[0], tape->dsc_poll_freq);
Borislav Petkov8004a8c2008-02-06 02:57:51 +0100385
Borislav Petkov6f3848a2009-07-21 23:08:23 -0700386 tape->postponed_rq = true;
Bartlomiej Zolnierkiewiczb65fac32009-01-06 17:20:50 +0100387
Borislav Petkov54bb2072008-02-06 02:57:52 +0100388 ide_stall_queue(drive, tape->dsc_poll_freq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389}
390
Bartlomiej Zolnierkiewicz74e63e742008-07-15 21:22:01 +0200391static void ide_tape_handle_dsc(ide_drive_t *drive)
392{
393 idetape_tape_t *tape = drive->driver_data;
394
395 /* Media access command */
396 tape->dsc_polling_start = jiffies;
397 tape->dsc_poll_freq = IDETAPE_DSC_MA_FAST;
398 tape->dsc_timeout = jiffies + IDETAPE_DSC_MA_TIMEOUT;
399 /* Allow ide.c to handle other requests */
Borislav Petkov6f3848a2009-07-21 23:08:23 -0700400 ide_tape_stall_queue(drive);
Bartlomiej Zolnierkiewicz74e63e742008-07-15 21:22:01 +0200401}
402
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100404 * Packet Command Interface
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 *
Bartlomiej Zolnierkiewicz2b9efba2008-10-13 21:39:31 +0200406 * The current Packet Command is available in drive->pc, and will not change
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100407 * until we finish handling it. Each packet command is associated with a
408 * callback function that will be called when the command is finished.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100410 * The handling will be done in three stages:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 *
Bartlomiej Zolnierkiewiczb788ee92009-03-27 12:46:46 +0100412 * 1. ide_tape_issue_pc will send the packet command to the drive, and will set
Bartlomiej Zolnierkiewiczaa5d2de72008-10-13 21:39:32 +0200413 * the interrupt handler to ide_pc_intr.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 *
Bartlomiej Zolnierkiewiczaa5d2de72008-10-13 21:39:32 +0200415 * 2. On each interrupt, ide_pc_intr will be called. This step will be
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100416 * repeated until the device signals us that no more interrupts will be issued.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100418 * 3. ATAPI Tape media access commands have immediate status with a delayed
419 * process. In case of a successful initiation of a media access packet command,
420 * the DSC bit will be set when the actual execution of the command is finished.
421 * Since the tape drive will not issue an interrupt, we have to poll for this
422 * event. In this case, we define the request as "low priority request" by
423 * setting rq_status to IDETAPE_RQ_POSTPONED, set a timer to poll for DSC and
424 * exit the driver.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100426 * ide.c will then give higher priority to requests which originate from the
427 * other device, until will change rq_status to RQ_ACTIVE.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100429 * 4. When the packet command is finished, it will be checked for errors.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100431 * 5. In case an error was found, we queue a request sense packet command in
432 * front of the request queue and retry the operation up to
433 * IDETAPE_MAX_PC_RETRIES times.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100435 * 6. In case no error was found, or we decided to give up and not to retry
436 * again, the callback function will be called and then we will handle the next
437 * request.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439
Bartlomiej Zolnierkiewiczb788ee92009-03-27 12:46:46 +0100440static ide_startstop_t ide_tape_issue_pc(ide_drive_t *drive,
441 struct ide_cmd *cmd,
442 struct ide_atapi_pc *pc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 idetape_tape_t *tape = drive->driver_data;
Borislav Petkovdfb7e622009-05-01 20:35:21 +0200445 struct request *rq = drive->hwif->rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446
Bartlomiej Zolnierkiewicz5e2040fd2009-03-27 12:46:34 +0100447 if (drive->failed_pc == NULL && pc->c[0] != REQUEST_SENSE)
448 drive->failed_pc = pc;
Bartlomiej Zolnierkiewicz2b9efba2008-10-13 21:39:31 +0200449
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450 /* Set the current packet command */
Bartlomiej Zolnierkiewicz2b9efba2008-10-13 21:39:31 +0200451 drive->pc = pc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452
453 if (pc->retries > IDETAPE_MAX_PC_RETRIES ||
Borislav Petkov346331f2008-04-18 00:46:26 +0200454 (pc->flags & PC_FLAG_ABORT)) {
Tejun Heob3071d12009-04-19 08:46:02 +0900455
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 /*
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100457 * We will "abort" retrying a packet command in case legitimate
458 * error code was received (crossing a filemark, or end of the
459 * media, for example).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 */
Borislav Petkov346331f2008-04-18 00:46:26 +0200461 if (!(pc->flags & PC_FLAG_ABORT)) {
Borislav Petkov90699ce2008-02-02 19:56:50 +0100462 if (!(pc->c[0] == TEST_UNIT_READY &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 tape->sense_key == 2 && tape->asc == 4 &&
464 (tape->ascq == 1 || tape->ascq == 8))) {
465 printk(KERN_ERR "ide-tape: %s: I/O error, "
466 "pc = %2x, key = %2x, "
467 "asc = %2x, ascq = %2x\n",
468 tape->name, pc->c[0],
469 tape->sense_key, tape->asc,
470 tape->ascq);
471 }
472 /* Giving up */
Bartlomiej Zolnierkiewiczc152cc12009-03-27 12:46:34 +0100473 pc->error = IDE_DRV_ERROR_GENERAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 }
Tejun Heob3071d12009-04-19 08:46:02 +0900475
Bartlomiej Zolnierkiewicz5e2040fd2009-03-27 12:46:34 +0100476 drive->failed_pc = NULL;
Bartlomiej Zolnierkiewiczb14c7212008-10-13 21:39:30 +0200477 drive->pc_callback(drive, 0);
Borislav Petkovdfb7e622009-05-01 20:35:21 +0200478 ide_complete_rq(drive, -EIO, blk_rq_bytes(rq));
Bartlomiej Zolnierkiewicz92f5daf2008-07-15 21:21:55 +0200479 return ide_stopped;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 }
Borislav Petkove972d702009-07-17 23:55:16 +0000481 ide_debug_log(IDE_DBG_SENSE, "retry #%d, cmd: 0x%02x", pc->retries,
482 pc->c[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483
484 pc->retries++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485
Bartlomiej Zolnierkiewiczb788ee92009-03-27 12:46:46 +0100486 return ide_issue_pc(drive, cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487}
488
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100489/* A mode sense command is used to "sense" tape parameters. */
Borislav Petkovd236d742008-04-18 00:46:27 +0200490static void idetape_create_mode_sense_cmd(struct ide_atapi_pc *pc, u8 page_code)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491{
Bartlomiej Zolnierkiewicz7bf74202008-10-10 22:39:37 +0200492 ide_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +0100493 pc->c[0] = MODE_SENSE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 if (page_code != IDETAPE_BLOCK_DESCRIPTOR)
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100495 /* DBD = 1 - Don't return block descriptors */
496 pc->c[1] = 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497 pc->c[2] = page_code;
498 /*
499 * Changed pc->c[3] to 0 (255 will at best return unused info).
500 *
501 * For SCSI this byte is defined as subpage instead of high byte
502 * of length and some IDE drives seem to interpret it this way
503 * and return an error when 255 is used.
504 */
505 pc->c[3] = 0;
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100506 /* We will just discard data in that case */
507 pc->c[4] = 255;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 if (page_code == IDETAPE_BLOCK_DESCRIPTOR)
Borislav Petkovd236d742008-04-18 00:46:27 +0200509 pc->req_xfer = 12;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 else if (page_code == IDETAPE_CAPABILITIES_PAGE)
Borislav Petkovd236d742008-04-18 00:46:27 +0200511 pc->req_xfer = 24;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 else
Borislav Petkovd236d742008-04-18 00:46:27 +0200513 pc->req_xfer = 50;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514}
515
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100516static ide_startstop_t idetape_media_access_finished(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517{
Bartlomiej Zolnierkiewiczb73c7ee2008-07-23 19:55:52 +0200518 ide_hwif_t *hwif = drive->hwif;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 idetape_tape_t *tape = drive->driver_data;
Bartlomiej Zolnierkiewicz2b9efba2008-10-13 21:39:31 +0200520 struct ide_atapi_pc *pc = drive->pc;
Bartlomiej Zolnierkiewicz22c525b2008-01-25 22:17:11 +0100521 u8 stat;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522
Bartlomiej Zolnierkiewicz374e0422008-07-23 19:55:56 +0200523 stat = hwif->tp_ops->read_status(hwif);
Bartlomiej Zolnierkiewiczc47137a2008-02-06 02:57:51 +0100524
Bartlomiej Zolnierkiewicz3a7d2482008-10-10 22:39:21 +0200525 if (stat & ATA_DSC) {
526 if (stat & ATA_ERR) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 /* Error detected */
Borislav Petkov90699ce2008-02-02 19:56:50 +0100528 if (pc->c[0] != TEST_UNIT_READY)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 printk(KERN_ERR "ide-tape: %s: I/O error, ",
530 tape->name);
531 /* Retry operation */
Borislav Petkov6b544fc2009-04-19 07:00:42 +0900532 ide_retry_pc(drive);
Bartlomiej Zolnierkiewicz258ec412008-07-15 21:21:55 +0200533 return ide_stopped;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 }
535 pc->error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 } else {
Bartlomiej Zolnierkiewiczc152cc12009-03-27 12:46:34 +0100537 pc->error = IDE_DRV_ERROR_GENERAL;
Bartlomiej Zolnierkiewicz5e2040fd2009-03-27 12:46:34 +0100538 drive->failed_pc = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 }
Bartlomiej Zolnierkiewiczb14c7212008-10-13 21:39:30 +0200540 drive->pc_callback(drive, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 return ide_stopped;
542}
543
Borislav Petkovcd2abbf2008-07-15 21:22:03 +0200544static void ide_tape_create_rw_cmd(idetape_tape_t *tape,
Borislav Petkov0014c752008-07-23 19:56:00 +0200545 struct ide_atapi_pc *pc, struct request *rq,
546 u8 opcode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547{
Borislav Petkov10c0b342009-05-01 20:29:53 +0200548 unsigned int length = blk_rq_sectors(rq) / (tape->blk_size >> 9);
Borislav Petkov0014c752008-07-23 19:56:00 +0200549
Bartlomiej Zolnierkiewicz7bf74202008-10-10 22:39:37 +0200550 ide_init_pc(pc);
Borislav Petkov860ff5e2008-02-02 19:56:50 +0100551 put_unaligned(cpu_to_be32(length), (unsigned int *) &pc->c[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 pc->c[1] = 1;
Borislav Petkov19f52a72009-05-04 09:53:03 +0200553
554 if (blk_rq_bytes(rq) == tape->buffer_size)
Bartlomiej Zolnierkiewicz5e331092008-07-15 21:21:56 +0200555 pc->flags |= PC_FLAG_DMA_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556
Tejun Heo963da552009-04-19 08:46:02 +0900557 if (opcode == READ_6)
Borislav Petkovcd2abbf2008-07-15 21:22:03 +0200558 pc->c[0] = READ_6;
Tejun Heo963da552009-04-19 08:46:02 +0900559 else if (opcode == WRITE_6) {
Borislav Petkovcd2abbf2008-07-15 21:22:03 +0200560 pc->c[0] = WRITE_6;
561 pc->flags |= PC_FLAG_WRITING;
Borislav Petkovcd2abbf2008-07-15 21:22:03 +0200562 }
Borislav Petkov0014c752008-07-23 19:56:00 +0200563
564 memcpy(rq->cmd, pc->c, 12);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565}
566
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567static ide_startstop_t idetape_do_request(ide_drive_t *drive,
568 struct request *rq, sector_t block)
569{
Bartlomiej Zolnierkiewiczb73c7ee2008-07-23 19:55:52 +0200570 ide_hwif_t *hwif = drive->hwif;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 idetape_tape_t *tape = drive->driver_data;
Borislav Petkovd236d742008-04-18 00:46:27 +0200572 struct ide_atapi_pc *pc = NULL;
Bartlomiej Zolnierkiewiczb788ee92009-03-27 12:46:46 +0100573 struct ide_cmd cmd;
Bartlomiej Zolnierkiewicz22c525b2008-01-25 22:17:11 +0100574 u8 stat;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575
Borislav Petkove972d702009-07-17 23:55:16 +0000576 ide_debug_log(IDE_DBG_RQ, "cmd: 0x%x, sector: %llu, nr_sectors: %u",
577 rq->cmd[0], (unsigned long long)blk_rq_pos(rq),
578 blk_rq_sectors(rq));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579
Christoph Hellwig33659eb2010-08-07 18:17:56 +0200580 BUG_ON(!(rq->cmd_type == REQ_TYPE_SPECIAL ||
581 rq->cmd_type == REQ_TYPE_SENSE));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100583 /* Retry a failed packet command */
Bartlomiej Zolnierkiewicz5e2040fd2009-03-27 12:46:34 +0100584 if (drive->failed_pc && drive->pc->c[0] == REQUEST_SENSE) {
585 pc = drive->failed_pc;
Bartlomiej Zolnierkiewicz28c72142008-07-15 21:21:59 +0200586 goto out;
587 }
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100588
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 /*
590 * If the tape is still busy, postpone our request and service
591 * the other device meanwhile.
592 */
Bartlomiej Zolnierkiewicz374e0422008-07-23 19:55:56 +0200593 stat = hwif->tp_ops->read_status(hwif);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +0200595 if ((drive->dev_flags & IDE_DFLAG_DSC_OVERLAP) == 0 &&
596 (rq->cmd[13] & REQ_IDETAPE_PC2) == 0)
Borislav Petkov626542c2009-06-07 15:37:05 +0200597 drive->atapi_flags |= IDE_AFLAG_IGNORE_DSC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +0200599 if (drive->dev_flags & IDE_DFLAG_POST_RESET) {
Borislav Petkov626542c2009-06-07 15:37:05 +0200600 drive->atapi_flags |= IDE_AFLAG_IGNORE_DSC;
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +0200601 drive->dev_flags &= ~IDE_DFLAG_POST_RESET;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 }
603
Borislav Petkov626542c2009-06-07 15:37:05 +0200604 if (!(drive->atapi_flags & IDE_AFLAG_IGNORE_DSC) &&
605 !(stat & ATA_DSC)) {
Borislav Petkov6f3848a2009-07-21 23:08:23 -0700606 if (!tape->postponed_rq) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607 tape->dsc_polling_start = jiffies;
Borislav Petkov54bb2072008-02-06 02:57:52 +0100608 tape->dsc_poll_freq = tape->best_dsc_rw_freq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 tape->dsc_timeout = jiffies + IDETAPE_DSC_RW_TIMEOUT;
610 } else if (time_after(jiffies, tape->dsc_timeout)) {
611 printk(KERN_ERR "ide-tape: %s: DSC timeout\n",
612 tape->name);
Borislav Petkov83dd5732008-07-23 19:56:00 +0200613 if (rq->cmd[13] & REQ_IDETAPE_PC2) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614 idetape_media_access_finished(drive);
615 return ide_stopped;
616 } else {
617 return ide_do_reset(drive);
618 }
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100619 } else if (time_after(jiffies,
620 tape->dsc_polling_start +
621 IDETAPE_DSC_MA_THRESHOLD))
Borislav Petkov54bb2072008-02-06 02:57:52 +0100622 tape->dsc_poll_freq = IDETAPE_DSC_MA_SLOW;
Borislav Petkov6f3848a2009-07-21 23:08:23 -0700623 ide_tape_stall_queue(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624 return ide_stopped;
Borislav Petkov6f3848a2009-07-21 23:08:23 -0700625 } else {
Borislav Petkov626542c2009-06-07 15:37:05 +0200626 drive->atapi_flags &= ~IDE_AFLAG_IGNORE_DSC;
Borislav Petkov6f3848a2009-07-21 23:08:23 -0700627 tape->postponed_rq = false;
628 }
Borislav Petkov626542c2009-06-07 15:37:05 +0200629
Borislav Petkov83dd5732008-07-23 19:56:00 +0200630 if (rq->cmd[13] & REQ_IDETAPE_READ) {
Bartlomiej Zolnierkiewicz2e8a6f82008-10-10 22:39:36 +0200631 pc = &tape->queued_pc;
Borislav Petkov0014c752008-07-23 19:56:00 +0200632 ide_tape_create_rw_cmd(tape, pc, rq, READ_6);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633 goto out;
634 }
Borislav Petkov83dd5732008-07-23 19:56:00 +0200635 if (rq->cmd[13] & REQ_IDETAPE_WRITE) {
Bartlomiej Zolnierkiewicz2e8a6f82008-10-10 22:39:36 +0200636 pc = &tape->queued_pc;
Borislav Petkov0014c752008-07-23 19:56:00 +0200637 ide_tape_create_rw_cmd(tape, pc, rq, WRITE_6);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 goto out;
639 }
Borislav Petkov83dd5732008-07-23 19:56:00 +0200640 if (rq->cmd[13] & REQ_IDETAPE_PC1) {
Tejun Heoc267cc12009-04-19 07:00:42 +0900641 pc = (struct ide_atapi_pc *)rq->special;
Borislav Petkov83dd5732008-07-23 19:56:00 +0200642 rq->cmd[13] &= ~(REQ_IDETAPE_PC1);
643 rq->cmd[13] |= REQ_IDETAPE_PC2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 goto out;
645 }
Borislav Petkov83dd5732008-07-23 19:56:00 +0200646 if (rq->cmd[13] & REQ_IDETAPE_PC2) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647 idetape_media_access_finished(drive);
648 return ide_stopped;
649 }
650 BUG();
Bartlomiej Zolnierkiewicz28c72142008-07-15 21:21:59 +0200651
Borislav Petkovf2e3ab52008-07-23 19:56:01 +0200652out:
Borislav Petkov6b544fc2009-04-19 07:00:42 +0900653 /* prepare sense request for this command */
654 ide_prep_sense(drive, rq);
655
Bartlomiej Zolnierkiewiczb788ee92009-03-27 12:46:46 +0100656 memset(&cmd, 0, sizeof(cmd));
657
658 if (rq_data_dir(rq))
659 cmd.tf_flags |= IDE_TFLAG_WRITE;
660
661 cmd.rq = rq;
662
Borislav Petkovdfb7e622009-05-01 20:35:21 +0200663 ide_init_sg_cmd(&cmd, blk_rq_bytes(rq));
Tejun Heo5c4be572009-04-19 07:00:42 +0900664 ide_map_sg(drive, &cmd);
665
Bartlomiej Zolnierkiewiczb788ee92009-03-27 12:46:46 +0100666 return ide_tape_issue_pc(drive, &cmd, pc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667}
668
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100670 * Write a filemark if write_filemark=1. Flush the device buffers without
671 * writing a filemark otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672 */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100673static void idetape_create_write_filemark_cmd(ide_drive_t *drive,
Borislav Petkovd236d742008-04-18 00:46:27 +0200674 struct ide_atapi_pc *pc, int write_filemark)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675{
Bartlomiej Zolnierkiewicz7bf74202008-10-10 22:39:37 +0200676 ide_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +0100677 pc->c[0] = WRITE_FILEMARKS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 pc->c[4] = write_filemark;
Borislav Petkov346331f2008-04-18 00:46:26 +0200679 pc->flags |= PC_FLAG_WAIT_FOR_DSC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680}
681
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682static int idetape_wait_ready(ide_drive_t *drive, unsigned long timeout)
683{
684 idetape_tape_t *tape = drive->driver_data;
Bartlomiej Zolnierkiewicz2ac07d92008-10-10 22:39:38 +0200685 struct gendisk *disk = tape->disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686 int load_attempted = 0;
687
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100688 /* Wait for the tape to become ready */
Borislav Petkov49d80782009-06-07 15:37:06 +0200689 set_bit(ilog2(IDE_AFLAG_MEDIUM_PRESENT), &drive->atapi_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 timeout += jiffies;
691 while (time_before(jiffies, timeout)) {
Bartlomiej Zolnierkiewiczde699ad2008-10-10 22:39:39 +0200692 if (ide_do_test_unit_ready(drive, disk) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 return 0;
694 if ((tape->sense_key == 2 && tape->asc == 4 && tape->ascq == 2)
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100695 || (tape->asc == 0x3A)) {
696 /* no media */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 if (load_attempted)
698 return -ENOMEDIUM;
Bartlomiej Zolnierkiewicz0c8a6c72008-10-10 22:39:39 +0200699 ide_do_start_stop(drive, disk, IDETAPE_LU_LOAD_MASK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 load_attempted = 1;
701 /* not about to be ready */
702 } else if (!(tape->sense_key == 2 && tape->asc == 4 &&
703 (tape->ascq == 1 || tape->ascq == 8)))
704 return -EIO;
Nishanth Aravamudan80ce45f2005-09-10 00:27:08 -0700705 msleep(100);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 }
707 return -EIO;
708}
709
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100710static int idetape_flush_tape_buffers(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711{
Bartlomiej Zolnierkiewicz2ac07d92008-10-10 22:39:38 +0200712 struct ide_tape_obj *tape = drive->driver_data;
Borislav Petkovd236d742008-04-18 00:46:27 +0200713 struct ide_atapi_pc pc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 int rc;
715
716 idetape_create_write_filemark_cmd(drive, &pc, 0);
Borislav Petkovb13345f2009-05-02 10:26:12 +0200717 rc = ide_queue_pc_tail(drive, tape->disk, &pc, NULL, 0);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100718 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719 return rc;
720 idetape_wait_ready(drive, 60 * 5 * HZ);
721 return 0;
722}
723
Borislav Petkov55ce3a12009-05-04 09:38:15 +0200724static int ide_tape_read_position(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725{
726 idetape_tape_t *tape = drive->driver_data;
Borislav Petkovd236d742008-04-18 00:46:27 +0200727 struct ide_atapi_pc pc;
Borislav Petkov55ce3a12009-05-04 09:38:15 +0200728 u8 buf[20];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729
Borislav Petkove972d702009-07-17 23:55:16 +0000730 ide_debug_log(IDE_DBG_FUNC, "enter");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731
Borislav Petkov55ce3a12009-05-04 09:38:15 +0200732 /* prep cmd */
733 ide_init_pc(&pc);
734 pc.c[0] = READ_POSITION;
735 pc.req_xfer = 20;
736
737 if (ide_queue_pc_tail(drive, tape->disk, &pc, buf, pc.req_xfer))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738 return -1;
Borislav Petkov55ce3a12009-05-04 09:38:15 +0200739
740 if (!pc.error) {
Borislav Petkove972d702009-07-17 23:55:16 +0000741 ide_debug_log(IDE_DBG_FUNC, "BOP - %s",
Borislav Petkov55ce3a12009-05-04 09:38:15 +0200742 (buf[0] & 0x80) ? "Yes" : "No");
Borislav Petkove972d702009-07-17 23:55:16 +0000743 ide_debug_log(IDE_DBG_FUNC, "EOP - %s",
Borislav Petkov55ce3a12009-05-04 09:38:15 +0200744 (buf[0] & 0x40) ? "Yes" : "No");
745
746 if (buf[0] & 0x4) {
747 printk(KERN_INFO "ide-tape: Block location is unknown"
748 "to the tape\n");
Bartlomiej Zolnierkiewicz8dcce402009-06-13 12:00:54 +0200749 clear_bit(ilog2(IDE_AFLAG_ADDRESS_VALID),
750 &drive->atapi_flags);
Borislav Petkov55ce3a12009-05-04 09:38:15 +0200751 return -1;
752 } else {
Borislav Petkove972d702009-07-17 23:55:16 +0000753 ide_debug_log(IDE_DBG_FUNC, "Block Location: %u",
754 be32_to_cpup((__be32 *)&buf[4]));
Borislav Petkov55ce3a12009-05-04 09:38:15 +0200755
756 tape->partition = buf[1];
757 tape->first_frame = be32_to_cpup((__be32 *)&buf[4]);
Bartlomiej Zolnierkiewicz8dcce402009-06-13 12:00:54 +0200758 set_bit(ilog2(IDE_AFLAG_ADDRESS_VALID),
759 &drive->atapi_flags);
Borislav Petkov55ce3a12009-05-04 09:38:15 +0200760 }
761 }
762
763 return tape->first_frame;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764}
765
Borislav Petkovd236d742008-04-18 00:46:27 +0200766static void idetape_create_locate_cmd(ide_drive_t *drive,
767 struct ide_atapi_pc *pc,
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100768 unsigned int block, u8 partition, int skip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769{
Bartlomiej Zolnierkiewicz7bf74202008-10-10 22:39:37 +0200770 ide_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +0100771 pc->c[0] = POSITION_TO_ELEMENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772 pc->c[1] = 2;
Borislav Petkov860ff5e2008-02-02 19:56:50 +0100773 put_unaligned(cpu_to_be32(block), (unsigned int *) &pc->c[3]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774 pc->c[8] = partition;
Borislav Petkov346331f2008-04-18 00:46:26 +0200775 pc->flags |= PC_FLAG_WAIT_FOR_DSC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776}
777
Borislav Petkovec0fdb02008-04-27 15:38:34 +0200778static void __ide_tape_discard_merge_buffer(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779{
780 idetape_tape_t *tape = drive->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781
Borislav Petkov54abf372008-02-06 02:57:52 +0100782 if (tape->chrdev_dir != IDETAPE_DIR_READ)
Borislav Petkov97986302008-04-27 15:38:34 +0200783 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784
Borislav Petkov49d80782009-06-07 15:37:06 +0200785 clear_bit(ilog2(IDE_AFLAG_FILEMARK), &drive->atapi_flags);
Tejun Heo963da552009-04-19 08:46:02 +0900786 tape->valid = 0;
787 if (tape->buf != NULL) {
788 kfree(tape->buf);
789 tape->buf = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790 }
791
Borislav Petkov54abf372008-02-06 02:57:52 +0100792 tape->chrdev_dir = IDETAPE_DIR_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793}
794
795/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100796 * Position the tape to the requested block using the LOCATE packet command.
797 * A READ POSITION command is then issued to check where we are positioned. Like
798 * all higher level operations, we queue the commands at the tail of the request
799 * queue and wait for their completion.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800 */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100801static int idetape_position_tape(ide_drive_t *drive, unsigned int block,
802 u8 partition, int skip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803{
804 idetape_tape_t *tape = drive->driver_data;
Bartlomiej Zolnierkiewicz2ac07d92008-10-10 22:39:38 +0200805 struct gendisk *disk = tape->disk;
Borislav Petkov55ce3a12009-05-04 09:38:15 +0200806 int ret;
Borislav Petkovd236d742008-04-18 00:46:27 +0200807 struct ide_atapi_pc pc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808
Borislav Petkov54abf372008-02-06 02:57:52 +0100809 if (tape->chrdev_dir == IDETAPE_DIR_READ)
Borislav Petkovec0fdb02008-04-27 15:38:34 +0200810 __ide_tape_discard_merge_buffer(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811 idetape_wait_ready(drive, 60 * 5 * HZ);
812 idetape_create_locate_cmd(drive, &pc, block, partition, skip);
Borislav Petkov55ce3a12009-05-04 09:38:15 +0200813 ret = ide_queue_pc_tail(drive, disk, &pc, NULL, 0);
814 if (ret)
815 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816
Borislav Petkov55ce3a12009-05-04 09:38:15 +0200817 ret = ide_tape_read_position(drive);
818 if (ret < 0)
819 return ret;
820 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821}
822
Borislav Petkovec0fdb02008-04-27 15:38:34 +0200823static void ide_tape_discard_merge_buffer(ide_drive_t *drive,
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100824 int restore_position)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825{
826 idetape_tape_t *tape = drive->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827 int seek, position;
828
Borislav Petkovec0fdb02008-04-27 15:38:34 +0200829 __ide_tape_discard_merge_buffer(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830 if (restore_position) {
Borislav Petkov55ce3a12009-05-04 09:38:15 +0200831 position = ide_tape_read_position(drive);
Borislav Petkov97986302008-04-27 15:38:34 +0200832 seek = position > 0 ? position : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833 if (idetape_position_tape(drive, seek, 0, 0)) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100834 printk(KERN_INFO "ide-tape: %s: position_tape failed in"
Borislav Petkovec0fdb02008-04-27 15:38:34 +0200835 " %s\n", tape->name, __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836 return;
837 }
838 }
839}
840
841/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100842 * Generate a read/write request for the block device interface and wait for it
843 * to be serviced.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844 */
Tejun Heo6bb11dd2009-04-19 08:46:03 +0900845static int idetape_queue_rw_tail(ide_drive_t *drive, int cmd, int size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846{
847 idetape_tape_t *tape = drive->driver_data;
FUJITA Tomonori64ea1b42008-07-15 21:21:43 +0200848 struct request *rq;
Tejun Heo21d9c5d2009-04-19 08:46:02 +0900849 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850
Borislav Petkove972d702009-07-17 23:55:16 +0000851 ide_debug_log(IDE_DBG_FUNC, "cmd: 0x%x, size: %d", cmd, size);
852
Tejun Heo21d9c5d2009-04-19 08:46:02 +0900853 BUG_ON(cmd != REQ_IDETAPE_READ && cmd != REQ_IDETAPE_WRITE);
Tejun Heo6bb11dd2009-04-19 08:46:03 +0900854 BUG_ON(size < 0 || size % tape->blk_size);
Borislav Petkov8004a8c2008-02-06 02:57:51 +0100855
FUJITA Tomonori64ea1b42008-07-15 21:21:43 +0200856 rq = blk_get_request(drive->queue, READ, __GFP_WAIT);
857 rq->cmd_type = REQ_TYPE_SPECIAL;
Borislav Petkov83dd5732008-07-23 19:56:00 +0200858 rq->cmd[13] = cmd;
FUJITA Tomonori64ea1b42008-07-15 21:21:43 +0200859 rq->rq_disk = tape->disk;
Linus Torvaldsc9059592009-06-11 10:52:27 -0700860 rq->__sector = tape->first_frame;
Tejun Heo21d9c5d2009-04-19 08:46:02 +0900861
862 if (size) {
Tejun Heo963da552009-04-19 08:46:02 +0900863 ret = blk_rq_map_kern(drive->queue, rq, tape->buf, size,
Tejun Heo21d9c5d2009-04-19 08:46:02 +0900864 __GFP_WAIT);
865 if (ret)
866 goto out_put;
867 }
868
FUJITA Tomonori64ea1b42008-07-15 21:21:43 +0200869 blk_execute_rq(drive->queue, tape->disk, rq, 0);
870
Tejun Heo963da552009-04-19 08:46:02 +0900871 /* calculate the number of transferred bytes and update buffer state */
Tejun Heoc3a4d782009-05-07 22:24:37 +0900872 size -= rq->resid_len;
Tejun Heo963da552009-04-19 08:46:02 +0900873 tape->cur = tape->buf;
Tejun Heo21d9c5d2009-04-19 08:46:02 +0900874 if (cmd == REQ_IDETAPE_READ)
Tejun Heo963da552009-04-19 08:46:02 +0900875 tape->valid = size;
876 else
877 tape->valid = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878
Tejun Heo21d9c5d2009-04-19 08:46:02 +0900879 ret = size;
880 if (rq->errors == IDE_DRV_ERROR_GENERAL)
881 ret = -EIO;
Tejun Heo21d9c5d2009-04-19 08:46:02 +0900882out_put:
883 blk_put_request(rq);
FUJITA Tomonori64ea1b42008-07-15 21:21:43 +0200884 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885}
886
Borislav Petkovd236d742008-04-18 00:46:27 +0200887static void idetape_create_inquiry_cmd(struct ide_atapi_pc *pc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888{
Bartlomiej Zolnierkiewicz7bf74202008-10-10 22:39:37 +0200889 ide_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +0100890 pc->c[0] = INQUIRY;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100891 pc->c[4] = 254;
Borislav Petkovd236d742008-04-18 00:46:27 +0200892 pc->req_xfer = 254;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893}
894
Borislav Petkovd236d742008-04-18 00:46:27 +0200895static void idetape_create_rewind_cmd(ide_drive_t *drive,
896 struct ide_atapi_pc *pc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897{
Bartlomiej Zolnierkiewicz7bf74202008-10-10 22:39:37 +0200898 ide_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +0100899 pc->c[0] = REZERO_UNIT;
Borislav Petkov346331f2008-04-18 00:46:26 +0200900 pc->flags |= PC_FLAG_WAIT_FOR_DSC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901}
902
Borislav Petkovd236d742008-04-18 00:46:27 +0200903static void idetape_create_erase_cmd(struct ide_atapi_pc *pc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904{
Bartlomiej Zolnierkiewicz7bf74202008-10-10 22:39:37 +0200905 ide_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +0100906 pc->c[0] = ERASE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907 pc->c[1] = 1;
Borislav Petkov346331f2008-04-18 00:46:26 +0200908 pc->flags |= PC_FLAG_WAIT_FOR_DSC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909}
910
Borislav Petkovd236d742008-04-18 00:46:27 +0200911static void idetape_create_space_cmd(struct ide_atapi_pc *pc, int count, u8 cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912{
Bartlomiej Zolnierkiewicz7bf74202008-10-10 22:39:37 +0200913 ide_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +0100914 pc->c[0] = SPACE;
Borislav Petkov860ff5e2008-02-02 19:56:50 +0100915 put_unaligned(cpu_to_be32(count), (unsigned int *) &pc->c[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916 pc->c[1] = cmd;
Borislav Petkov346331f2008-04-18 00:46:26 +0200917 pc->flags |= PC_FLAG_WAIT_FOR_DSC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918}
919
Borislav Petkovd9df9372008-04-27 15:38:34 +0200920static void ide_tape_flush_merge_buffer(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921{
922 idetape_tape_t *tape = drive->driver_data;
Borislav Petkov55a5d292008-02-02 19:56:49 +0100923
Borislav Petkov54abf372008-02-06 02:57:52 +0100924 if (tape->chrdev_dir != IDETAPE_DIR_WRITE) {
Borislav Petkov077e3bd2008-04-27 15:38:34 +0200925 printk(KERN_ERR "ide-tape: bug: Trying to empty merge buffer"
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100926 " but we are not writing.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927 return;
928 }
Tejun Heo963da552009-04-19 08:46:02 +0900929 if (tape->buf) {
Tejun Heo6bb11dd2009-04-19 08:46:03 +0900930 size_t aligned = roundup(tape->valid, tape->blk_size);
931
932 memset(tape->cur, 0, aligned - tape->valid);
Tejun Heo07bd9682009-04-19 08:46:03 +0900933 idetape_queue_rw_tail(drive, REQ_IDETAPE_WRITE, aligned);
Tejun Heo963da552009-04-19 08:46:02 +0900934 kfree(tape->buf);
935 tape->buf = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936 }
Borislav Petkov54abf372008-02-06 02:57:52 +0100937 tape->chrdev_dir = IDETAPE_DIR_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938}
939
Tejun Heo88f1b942009-04-19 08:46:02 +0900940static int idetape_init_rw(ide_drive_t *drive, int dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941{
942 idetape_tape_t *tape = drive->driver_data;
Tejun Heo88f1b942009-04-19 08:46:02 +0900943 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944
Tejun Heo88f1b942009-04-19 08:46:02 +0900945 BUG_ON(dir != IDETAPE_DIR_READ && dir != IDETAPE_DIR_WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946
Tejun Heo88f1b942009-04-19 08:46:02 +0900947 if (tape->chrdev_dir == dir)
948 return 0;
949
950 if (tape->chrdev_dir == IDETAPE_DIR_READ)
951 ide_tape_discard_merge_buffer(drive, 1);
952 else if (tape->chrdev_dir == IDETAPE_DIR_WRITE) {
953 ide_tape_flush_merge_buffer(drive);
954 idetape_flush_tape_buffers(drive);
955 }
956
957 if (tape->buf || tape->valid) {
958 printk(KERN_ERR "ide-tape: valid should be 0 now\n");
959 tape->valid = 0;
960 }
961
962 tape->buf = kmalloc(tape->buffer_size, GFP_KERNEL);
963 if (!tape->buf)
964 return -ENOMEM;
965 tape->chrdev_dir = dir;
966 tape->cur = tape->buf;
967
968 /*
969 * Issue a 0 rw command to ensure that DSC handshake is
970 * switched from completion mode to buffer available mode. No
971 * point in issuing this if DSC overlap isn't supported, some
972 * drives (Seagate STT3401A) will return an error.
973 */
974 if (drive->dev_flags & IDE_DFLAG_DSC_OVERLAP) {
975 int cmd = dir == IDETAPE_DIR_READ ? REQ_IDETAPE_READ
976 : REQ_IDETAPE_WRITE;
977
978 rc = idetape_queue_rw_tail(drive, cmd, 0);
979 if (rc < 0) {
980 kfree(tape->buf);
981 tape->buf = NULL;
982 tape->chrdev_dir = IDETAPE_DIR_NONE;
983 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984 }
985 }
Borislav Petkov5e69bd92008-04-27 15:38:25 +0200986
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987 return 0;
988}
989
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100990static void idetape_pad_zeros(ide_drive_t *drive, int bcount)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991{
992 idetape_tape_t *tape = drive->driver_data;
Tejun Heo963da552009-04-19 08:46:02 +0900993
994 memset(tape->buf, 0, tape->buffer_size);
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100995
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996 while (bcount) {
Tejun Heo963da552009-04-19 08:46:02 +0900997 unsigned int count = min(tape->buffer_size, bcount);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998
Tejun Heo6bb11dd2009-04-19 08:46:03 +0900999 idetape_queue_rw_tail(drive, REQ_IDETAPE_WRITE, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000 bcount -= count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001 }
1002}
1003
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001005 * Rewinds the tape to the Beginning Of the current Partition (BOP). We
1006 * currently support only one partition.
1007 */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001008static int idetape_rewind_tape(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009{
Bartlomiej Zolnierkiewicz2ac07d92008-10-10 22:39:38 +02001010 struct ide_tape_obj *tape = drive->driver_data;
1011 struct gendisk *disk = tape->disk;
Borislav Petkovd236d742008-04-18 00:46:27 +02001012 struct ide_atapi_pc pc;
Borislav Petkov55ce3a12009-05-04 09:38:15 +02001013 int ret;
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001014
Borislav Petkove972d702009-07-17 23:55:16 +00001015 ide_debug_log(IDE_DBG_FUNC, "enter");
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001016
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017 idetape_create_rewind_cmd(drive, &pc);
Borislav Petkov55ce3a12009-05-04 09:38:15 +02001018 ret = ide_queue_pc_tail(drive, disk, &pc, NULL, 0);
1019 if (ret)
1020 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021
Borislav Petkov55ce3a12009-05-04 09:38:15 +02001022 ret = ide_tape_read_position(drive);
1023 if (ret < 0)
1024 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025 return 0;
1026}
1027
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001028/* mtio.h compatible commands should be issued to the chrdev interface. */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001029static int idetape_blkdev_ioctl(ide_drive_t *drive, unsigned int cmd,
1030 unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031{
1032 idetape_tape_t *tape = drive->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033 void __user *argp = (void __user *)arg;
1034
Borislav Petkovd59823f2008-02-02 19:56:51 +01001035 struct idetape_config {
1036 int dsc_rw_frequency;
1037 int dsc_media_access_frequency;
1038 int nr_stages;
1039 } config;
1040
Borislav Petkove972d702009-07-17 23:55:16 +00001041 ide_debug_log(IDE_DBG_FUNC, "cmd: 0x%04x", cmd);
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001042
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043 switch (cmd) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001044 case 0x0340:
1045 if (copy_from_user(&config, argp, sizeof(config)))
1046 return -EFAULT;
1047 tape->best_dsc_rw_freq = config.dsc_rw_frequency;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001048 break;
1049 case 0x0350:
Michael Buesch2fc21112009-07-19 09:15:19 +00001050 memset(&config, 0, sizeof(config));
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001051 config.dsc_rw_frequency = (int) tape->best_dsc_rw_freq;
Borislav Petkov83042b22008-04-27 15:38:27 +02001052 config.nr_stages = 1;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001053 if (copy_to_user(argp, &config, sizeof(config)))
1054 return -EFAULT;
1055 break;
1056 default:
1057 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058 }
1059 return 0;
1060}
1061
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001062static int idetape_space_over_filemarks(ide_drive_t *drive, short mt_op,
1063 int mt_count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064{
1065 idetape_tape_t *tape = drive->driver_data;
Bartlomiej Zolnierkiewicz2ac07d92008-10-10 22:39:38 +02001066 struct gendisk *disk = tape->disk;
Borislav Petkovd236d742008-04-18 00:46:27 +02001067 struct ide_atapi_pc pc;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001068 int retval, count = 0;
Borislav Petkovb6422012008-02-02 19:56:49 +01001069 int sprev = !!(tape->caps[4] & 0x20);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070
Borislav Petkove972d702009-07-17 23:55:16 +00001071
1072 ide_debug_log(IDE_DBG_FUNC, "mt_op: %d, mt_count: %d", mt_op, mt_count);
1073
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074 if (mt_count == 0)
1075 return 0;
1076 if (MTBSF == mt_op || MTBSFM == mt_op) {
Borislav Petkovb6422012008-02-02 19:56:49 +01001077 if (!sprev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078 return -EIO;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001079 mt_count = -mt_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080 }
1081
Borislav Petkov54abf372008-02-06 02:57:52 +01001082 if (tape->chrdev_dir == IDETAPE_DIR_READ) {
Tejun Heo963da552009-04-19 08:46:02 +09001083 tape->valid = 0;
Borislav Petkov49d80782009-06-07 15:37:06 +02001084 if (test_and_clear_bit(ilog2(IDE_AFLAG_FILEMARK),
1085 &drive->atapi_flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086 ++count;
Borislav Petkovec0fdb02008-04-27 15:38:34 +02001087 ide_tape_discard_merge_buffer(drive, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088 }
1089
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090 switch (mt_op) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001091 case MTFSF:
1092 case MTBSF:
1093 idetape_create_space_cmd(&pc, mt_count - count,
1094 IDETAPE_SPACE_OVER_FILEMARK);
Borislav Petkovb13345f2009-05-02 10:26:12 +02001095 return ide_queue_pc_tail(drive, disk, &pc, NULL, 0);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001096 case MTFSFM:
1097 case MTBSFM:
1098 if (!sprev)
1099 return -EIO;
1100 retval = idetape_space_over_filemarks(drive, MTFSF,
1101 mt_count - count);
1102 if (retval)
1103 return retval;
1104 count = (MTBSFM == mt_op ? 1 : -1);
1105 return idetape_space_over_filemarks(drive, MTFSF, count);
1106 default:
1107 printk(KERN_ERR "ide-tape: MTIO operation %d not supported\n",
1108 mt_op);
1109 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110 }
1111}
1112
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001114 * Our character device read / write functions.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001116 * The tape is optimized to maximize throughput when it is transferring an
1117 * integral number of the "continuous transfer limit", which is a parameter of
1118 * the specific tape (26kB on my particular tape, 32kB for Onstream).
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001120 * As of version 1.3 of the driver, the character device provides an abstract
1121 * continuous view of the media - any mix of block sizes (even 1 byte) on the
1122 * same backup/restore procedure is supported. The driver will internally
1123 * convert the requests to the recommended transfer unit, so that an unmatch
1124 * between the user's block size to the recommended size will only result in a
1125 * (slightly) increased driver overhead, but will no longer hit performance.
1126 * This is not applicable to Onstream.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127 */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001128static ssize_t idetape_chrdev_read(struct file *file, char __user *buf,
1129 size_t count, loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130{
Borislav Petkov5aeddf92008-10-13 21:39:34 +02001131 struct ide_tape_obj *tape = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132 ide_drive_t *drive = tape->drive;
Tejun Heo07bd9682009-04-19 08:46:03 +09001133 size_t done = 0;
Daniel Walkerdcd96372006-06-25 05:47:37 -07001134 ssize_t ret = 0;
Tejun Heo07bd9682009-04-19 08:46:03 +09001135 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136
Borislav Petkove972d702009-07-17 23:55:16 +00001137 ide_debug_log(IDE_DBG_FUNC, "count %Zd", count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138
Borislav Petkov54abf372008-02-06 02:57:52 +01001139 if (tape->chrdev_dir != IDETAPE_DIR_READ) {
Borislav Petkov49d80782009-06-07 15:37:06 +02001140 if (test_bit(ilog2(IDE_AFLAG_DETECT_BS), &drive->atapi_flags))
Borislav Petkov54bb2072008-02-06 02:57:52 +01001141 if (count > tape->blk_size &&
1142 (count % tape->blk_size) == 0)
1143 tape->user_bs_factor = count / tape->blk_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144 }
Tejun Heo07bd9682009-04-19 08:46:03 +09001145
Tejun Heo88f1b942009-04-19 08:46:02 +09001146 rc = idetape_init_rw(drive, IDETAPE_DIR_READ);
Borislav Petkov8d06bfa2008-02-06 02:57:53 +01001147 if (rc < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148 return rc;
Tejun Heo07bd9682009-04-19 08:46:03 +09001149
1150 while (done < count) {
1151 size_t todo;
1152
1153 /* refill if staging buffer is empty */
1154 if (!tape->valid) {
1155 /* If we are at a filemark, nothing more to read */
Borislav Petkov49d80782009-06-07 15:37:06 +02001156 if (test_bit(ilog2(IDE_AFLAG_FILEMARK),
1157 &drive->atapi_flags))
Tejun Heo07bd9682009-04-19 08:46:03 +09001158 break;
1159 /* read */
1160 if (idetape_queue_rw_tail(drive, REQ_IDETAPE_READ,
1161 tape->buffer_size) <= 0)
1162 break;
1163 }
1164
1165 /* copy out */
1166 todo = min_t(size_t, count - done, tape->valid);
1167 if (copy_to_user(buf + done, tape->cur, todo))
Daniel Walkerdcd96372006-06-25 05:47:37 -07001168 ret = -EFAULT;
Tejun Heo07bd9682009-04-19 08:46:03 +09001169
1170 tape->cur += todo;
1171 tape->valid -= todo;
1172 done += todo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173 }
Tejun Heo07bd9682009-04-19 08:46:03 +09001174
Borislav Petkov49d80782009-06-07 15:37:06 +02001175 if (!done && test_bit(ilog2(IDE_AFLAG_FILEMARK), &drive->atapi_flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176 idetape_space_over_filemarks(drive, MTFSF, 1);
1177 return 0;
1178 }
Daniel Walkerdcd96372006-06-25 05:47:37 -07001179
Tejun Heo07bd9682009-04-19 08:46:03 +09001180 return ret ? ret : done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181}
1182
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001183static ssize_t idetape_chrdev_write(struct file *file, const char __user *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184 size_t count, loff_t *ppos)
1185{
Borislav Petkov5aeddf92008-10-13 21:39:34 +02001186 struct ide_tape_obj *tape = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187 ide_drive_t *drive = tape->drive;
Tejun Heo07bd9682009-04-19 08:46:03 +09001188 size_t done = 0;
Daniel Walkerdcd96372006-06-25 05:47:37 -07001189 ssize_t ret = 0;
Tejun Heo88f1b942009-04-19 08:46:02 +09001190 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191
1192 /* The drive is write protected. */
1193 if (tape->write_prot)
1194 return -EACCES;
1195
Borislav Petkove972d702009-07-17 23:55:16 +00001196 ide_debug_log(IDE_DBG_FUNC, "count %Zd", count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197
1198 /* Initialize write operation */
Tejun Heo88f1b942009-04-19 08:46:02 +09001199 rc = idetape_init_rw(drive, IDETAPE_DIR_WRITE);
1200 if (rc < 0)
1201 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202
Tejun Heo07bd9682009-04-19 08:46:03 +09001203 while (done < count) {
1204 size_t todo;
1205
1206 /* flush if staging buffer is full */
1207 if (tape->valid == tape->buffer_size &&
1208 idetape_queue_rw_tail(drive, REQ_IDETAPE_WRITE,
1209 tape->buffer_size) <= 0)
1210 return rc;
1211
1212 /* copy in */
1213 todo = min_t(size_t, count - done,
1214 tape->buffer_size - tape->valid);
1215 if (copy_from_user(tape->cur, buf + done, todo))
Daniel Walkerdcd96372006-06-25 05:47:37 -07001216 ret = -EFAULT;
Tejun Heo07bd9682009-04-19 08:46:03 +09001217
1218 tape->cur += todo;
1219 tape->valid += todo;
1220 done += todo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221 }
Tejun Heo07bd9682009-04-19 08:46:03 +09001222
1223 return ret ? ret : done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224}
1225
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001226static int idetape_write_filemark(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227{
Bartlomiej Zolnierkiewicz2ac07d92008-10-10 22:39:38 +02001228 struct ide_tape_obj *tape = drive->driver_data;
Borislav Petkovd236d742008-04-18 00:46:27 +02001229 struct ide_atapi_pc pc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230
1231 /* Write a filemark */
1232 idetape_create_write_filemark_cmd(drive, &pc, 1);
Borislav Petkovb13345f2009-05-02 10:26:12 +02001233 if (ide_queue_pc_tail(drive, tape->disk, &pc, NULL, 0)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234 printk(KERN_ERR "ide-tape: Couldn't write a filemark\n");
1235 return -EIO;
1236 }
1237 return 0;
1238}
1239
1240/*
Borislav Petkovd99c9da2008-02-02 19:56:51 +01001241 * Called from idetape_chrdev_ioctl when the general mtio MTIOCTOP ioctl is
1242 * requested.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243 *
Borislav Petkovd99c9da2008-02-02 19:56:51 +01001244 * Note: MTBSF and MTBSFM are not supported when the tape doesn't support
1245 * spacing over filemarks in the reverse direction. In this case, MTFSFM is also
Borislav Petkov5bd50dc2008-04-27 15:38:28 +02001246 * usually not supported.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247 *
Borislav Petkovd99c9da2008-02-02 19:56:51 +01001248 * The following commands are currently not supported:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249 *
Borislav Petkovd99c9da2008-02-02 19:56:51 +01001250 * MTFSS, MTBSS, MTWSM, MTSETDENSITY, MTSETDRVBUFFER, MT_ST_BOOLEANS,
1251 * MT_ST_WRITE_THRESHOLD.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252 */
Borislav Petkovd99c9da2008-02-02 19:56:51 +01001253static int idetape_mtioctop(ide_drive_t *drive, short mt_op, int mt_count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254{
1255 idetape_tape_t *tape = drive->driver_data;
Bartlomiej Zolnierkiewicz2ac07d92008-10-10 22:39:38 +02001256 struct gendisk *disk = tape->disk;
Borislav Petkovd236d742008-04-18 00:46:27 +02001257 struct ide_atapi_pc pc;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001258 int i, retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259
Borislav Petkove972d702009-07-17 23:55:16 +00001260 ide_debug_log(IDE_DBG_FUNC, "MTIOCTOP ioctl: mt_op: %d, mt_count: %d",
1261 mt_op, mt_count);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001262
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263 switch (mt_op) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001264 case MTFSF:
1265 case MTFSFM:
1266 case MTBSF:
1267 case MTBSFM:
1268 if (!mt_count)
1269 return 0;
1270 return idetape_space_over_filemarks(drive, mt_op, mt_count);
1271 default:
1272 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273 }
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001274
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275 switch (mt_op) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001276 case MTWEOF:
1277 if (tape->write_prot)
1278 return -EACCES;
Borislav Petkovec0fdb02008-04-27 15:38:34 +02001279 ide_tape_discard_merge_buffer(drive, 1);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001280 for (i = 0; i < mt_count; i++) {
1281 retval = idetape_write_filemark(drive);
1282 if (retval)
1283 return retval;
1284 }
1285 return 0;
1286 case MTREW:
Borislav Petkovec0fdb02008-04-27 15:38:34 +02001287 ide_tape_discard_merge_buffer(drive, 0);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001288 if (idetape_rewind_tape(drive))
1289 return -EIO;
1290 return 0;
1291 case MTLOAD:
Borislav Petkovec0fdb02008-04-27 15:38:34 +02001292 ide_tape_discard_merge_buffer(drive, 0);
Bartlomiej Zolnierkiewicz0c8a6c72008-10-10 22:39:39 +02001293 return ide_do_start_stop(drive, disk, IDETAPE_LU_LOAD_MASK);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001294 case MTUNLOAD:
1295 case MTOFFL:
1296 /*
1297 * If door is locked, attempt to unlock before
1298 * attempting to eject.
1299 */
1300 if (tape->door_locked) {
Bartlomiej Zolnierkiewicz05780422008-10-10 22:39:38 +02001301 if (!ide_set_media_lock(drive, disk, 0))
Bartlomiej Zolnierkiewicz385a4b82008-10-10 22:39:37 +02001302 tape->door_locked = DOOR_UNLOCKED;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001303 }
Borislav Petkovec0fdb02008-04-27 15:38:34 +02001304 ide_tape_discard_merge_buffer(drive, 0);
Bartlomiej Zolnierkiewicz0c8a6c72008-10-10 22:39:39 +02001305 retval = ide_do_start_stop(drive, disk, !IDETAPE_LU_LOAD_MASK);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001306 if (!retval)
Borislav Petkov49d80782009-06-07 15:37:06 +02001307 clear_bit(ilog2(IDE_AFLAG_MEDIUM_PRESENT),
1308 &drive->atapi_flags);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001309 return retval;
1310 case MTNOP:
Borislav Petkovec0fdb02008-04-27 15:38:34 +02001311 ide_tape_discard_merge_buffer(drive, 0);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001312 return idetape_flush_tape_buffers(drive);
1313 case MTRETEN:
Borislav Petkovec0fdb02008-04-27 15:38:34 +02001314 ide_tape_discard_merge_buffer(drive, 0);
Bartlomiej Zolnierkiewicz0c8a6c72008-10-10 22:39:39 +02001315 return ide_do_start_stop(drive, disk,
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001316 IDETAPE_LU_RETENSION_MASK | IDETAPE_LU_LOAD_MASK);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001317 case MTEOM:
1318 idetape_create_space_cmd(&pc, 0, IDETAPE_SPACE_TO_EOD);
Borislav Petkovb13345f2009-05-02 10:26:12 +02001319 return ide_queue_pc_tail(drive, disk, &pc, NULL, 0);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001320 case MTERASE:
1321 (void)idetape_rewind_tape(drive);
1322 idetape_create_erase_cmd(&pc);
Borislav Petkovb13345f2009-05-02 10:26:12 +02001323 return ide_queue_pc_tail(drive, disk, &pc, NULL, 0);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001324 case MTSETBLK:
1325 if (mt_count) {
1326 if (mt_count < tape->blk_size ||
1327 mt_count % tape->blk_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328 return -EIO;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001329 tape->user_bs_factor = mt_count / tape->blk_size;
Borislav Petkov49d80782009-06-07 15:37:06 +02001330 clear_bit(ilog2(IDE_AFLAG_DETECT_BS),
1331 &drive->atapi_flags);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001332 } else
Borislav Petkov49d80782009-06-07 15:37:06 +02001333 set_bit(ilog2(IDE_AFLAG_DETECT_BS),
1334 &drive->atapi_flags);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001335 return 0;
1336 case MTSEEK:
Borislav Petkovec0fdb02008-04-27 15:38:34 +02001337 ide_tape_discard_merge_buffer(drive, 0);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001338 return idetape_position_tape(drive,
1339 mt_count * tape->user_bs_factor, tape->partition, 0);
1340 case MTSETPART:
Borislav Petkovec0fdb02008-04-27 15:38:34 +02001341 ide_tape_discard_merge_buffer(drive, 0);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001342 return idetape_position_tape(drive, 0, mt_count, 0);
1343 case MTFSR:
1344 case MTBSR:
1345 case MTLOCK:
Bartlomiej Zolnierkiewicz05780422008-10-10 22:39:38 +02001346 retval = ide_set_media_lock(drive, disk, 1);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001347 if (retval)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348 return retval;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001349 tape->door_locked = DOOR_EXPLICITLY_LOCKED;
1350 return 0;
1351 case MTUNLOCK:
Bartlomiej Zolnierkiewicz05780422008-10-10 22:39:38 +02001352 retval = ide_set_media_lock(drive, disk, 0);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001353 if (retval)
1354 return retval;
1355 tape->door_locked = DOOR_UNLOCKED;
1356 return 0;
1357 default:
1358 printk(KERN_ERR "ide-tape: MTIO operation %d not supported\n",
1359 mt_op);
1360 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361 }
1362}
1363
1364/*
Borislav Petkovd99c9da2008-02-02 19:56:51 +01001365 * Our character device ioctls. General mtio.h magnetic io commands are
1366 * supported here, and not in the corresponding block interface. Our own
1367 * ide-tape ioctls are supported on both interfaces.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368 */
Alan Cox05227ad2010-01-04 06:24:00 +00001369static long do_idetape_chrdev_ioctl(struct file *file,
Borislav Petkovd99c9da2008-02-02 19:56:51 +01001370 unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371{
Borislav Petkov5aeddf92008-10-13 21:39:34 +02001372 struct ide_tape_obj *tape = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373 ide_drive_t *drive = tape->drive;
1374 struct mtop mtop;
1375 struct mtget mtget;
1376 struct mtpos mtpos;
Borislav Petkov54bb2072008-02-06 02:57:52 +01001377 int block_offset = 0, position = tape->first_frame;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378 void __user *argp = (void __user *)arg;
1379
Borislav Petkove972d702009-07-17 23:55:16 +00001380 ide_debug_log(IDE_DBG_FUNC, "cmd: 0x%x", cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381
Borislav Petkov54abf372008-02-06 02:57:52 +01001382 if (tape->chrdev_dir == IDETAPE_DIR_WRITE) {
Borislav Petkovd9df9372008-04-27 15:38:34 +02001383 ide_tape_flush_merge_buffer(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384 idetape_flush_tape_buffers(drive);
1385 }
1386 if (cmd == MTIOCGET || cmd == MTIOCPOS) {
Tejun Heo963da552009-04-19 08:46:02 +09001387 block_offset = tape->valid /
Borislav Petkov54bb2072008-02-06 02:57:52 +01001388 (tape->blk_size * tape->user_bs_factor);
Borislav Petkov55ce3a12009-05-04 09:38:15 +02001389 position = ide_tape_read_position(drive);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001390 if (position < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391 return -EIO;
1392 }
1393 switch (cmd) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001394 case MTIOCTOP:
1395 if (copy_from_user(&mtop, argp, sizeof(struct mtop)))
1396 return -EFAULT;
1397 return idetape_mtioctop(drive, mtop.mt_op, mtop.mt_count);
1398 case MTIOCGET:
1399 memset(&mtget, 0, sizeof(struct mtget));
1400 mtget.mt_type = MT_ISSCSI2;
1401 mtget.mt_blkno = position / tape->user_bs_factor - block_offset;
1402 mtget.mt_dsreg =
1403 ((tape->blk_size * tape->user_bs_factor)
1404 << MT_ST_BLKSIZE_SHIFT) & MT_ST_BLKSIZE_MASK;
Borislav Petkov54bb2072008-02-06 02:57:52 +01001405
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001406 if (tape->drv_write_prot)
1407 mtget.mt_gstat |= GMT_WR_PROT(0xffffffff);
1408
1409 if (copy_to_user(argp, &mtget, sizeof(struct mtget)))
1410 return -EFAULT;
1411 return 0;
1412 case MTIOCPOS:
1413 mtpos.mt_blkno = position / tape->user_bs_factor - block_offset;
1414 if (copy_to_user(argp, &mtpos, sizeof(struct mtpos)))
1415 return -EFAULT;
1416 return 0;
1417 default:
1418 if (tape->chrdev_dir == IDETAPE_DIR_READ)
Borislav Petkovec0fdb02008-04-27 15:38:34 +02001419 ide_tape_discard_merge_buffer(drive, 1);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001420 return idetape_blkdev_ioctl(drive, cmd, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421 }
1422}
1423
Alan Cox05227ad2010-01-04 06:24:00 +00001424static long idetape_chrdev_ioctl(struct file *file,
1425 unsigned int cmd, unsigned long arg)
1426{
1427 long ret;
Arnd Bergmann2a48fc02010-06-02 14:28:52 +02001428 mutex_lock(&ide_tape_mutex);
Alan Cox05227ad2010-01-04 06:24:00 +00001429 ret = do_idetape_chrdev_ioctl(file, cmd, arg);
Arnd Bergmann2a48fc02010-06-02 14:28:52 +02001430 mutex_unlock(&ide_tape_mutex);
Alan Cox05227ad2010-01-04 06:24:00 +00001431 return ret;
1432}
1433
Borislav Petkov3cffb9c2008-02-02 19:56:50 +01001434/*
1435 * Do a mode sense page 0 with block descriptor and if it succeeds set the tape
1436 * block size with the reported value.
1437 */
1438static void ide_tape_get_bsize_from_bdesc(ide_drive_t *drive)
1439{
1440 idetape_tape_t *tape = drive->driver_data;
Borislav Petkovd236d742008-04-18 00:46:27 +02001441 struct ide_atapi_pc pc;
Borislav Petkov837272b2009-05-04 09:48:57 +02001442 u8 buf[12];
Borislav Petkov3cffb9c2008-02-02 19:56:50 +01001443
1444 idetape_create_mode_sense_cmd(&pc, IDETAPE_BLOCK_DESCRIPTOR);
Borislav Petkov837272b2009-05-04 09:48:57 +02001445 if (ide_queue_pc_tail(drive, tape->disk, &pc, buf, pc.req_xfer)) {
Borislav Petkov3cffb9c2008-02-02 19:56:50 +01001446 printk(KERN_ERR "ide-tape: Can't get block descriptor\n");
Borislav Petkov54bb2072008-02-06 02:57:52 +01001447 if (tape->blk_size == 0) {
Borislav Petkov3cffb9c2008-02-02 19:56:50 +01001448 printk(KERN_WARNING "ide-tape: Cannot deal with zero "
1449 "block size, assuming 32k\n");
Borislav Petkov54bb2072008-02-06 02:57:52 +01001450 tape->blk_size = 32768;
Borislav Petkov3cffb9c2008-02-02 19:56:50 +01001451 }
1452 return;
1453 }
Borislav Petkov837272b2009-05-04 09:48:57 +02001454 tape->blk_size = (buf[4 + 5] << 16) +
1455 (buf[4 + 6] << 8) +
1456 buf[4 + 7];
1457 tape->drv_write_prot = (buf[2] & 0x80) >> 7;
Borislav Petkove972d702009-07-17 23:55:16 +00001458
1459 ide_debug_log(IDE_DBG_FUNC, "blk_size: %d, write_prot: %d",
1460 tape->blk_size, tape->drv_write_prot);
Borislav Petkov3cffb9c2008-02-02 19:56:50 +01001461}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001463static int idetape_chrdev_open(struct inode *inode, struct file *filp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464{
1465 unsigned int minor = iminor(inode), i = minor & ~0xc0;
1466 ide_drive_t *drive;
1467 idetape_tape_t *tape;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468 int retval;
1469
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001470 if (i >= MAX_HWIFS * MAX_DRIVES)
1471 return -ENXIO;
1472
Borislav Petkovcbba2fa2009-10-11 00:25:19 +00001473 mutex_lock(&idetape_chrdev_mutex);
1474
Borislav Petkov9d01e4c2009-06-08 22:03:03 +02001475 tape = ide_tape_get(NULL, true, i);
Jonathan Corbet04f4ac92008-05-15 12:01:56 -06001476 if (!tape) {
Borislav Petkovcbba2fa2009-10-11 00:25:19 +00001477 mutex_unlock(&idetape_chrdev_mutex);
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001478 return -ENXIO;
Jonathan Corbet04f4ac92008-05-15 12:01:56 -06001479 }
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001480
Borislav Petkove972d702009-07-17 23:55:16 +00001481 drive = tape->drive;
1482 filp->private_data = tape;
1483
1484 ide_debug_log(IDE_DBG_FUNC, "enter");
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001485
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486 /*
1487 * We really want to do nonseekable_open(inode, filp); here, but some
1488 * versions of tar incorrectly call lseek on tapes and bail out if that
1489 * fails. So we disallow pread() and pwrite(), but permit lseeks.
1490 */
1491 filp->f_mode &= ~(FMODE_PREAD | FMODE_PWRITE);
1492
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493
Borislav Petkov49d80782009-06-07 15:37:06 +02001494 if (test_and_set_bit(ilog2(IDE_AFLAG_BUSY), &drive->atapi_flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495 retval = -EBUSY;
1496 goto out_put_tape;
1497 }
1498
1499 retval = idetape_wait_ready(drive, 60 * HZ);
1500 if (retval) {
Borislav Petkov49d80782009-06-07 15:37:06 +02001501 clear_bit(ilog2(IDE_AFLAG_BUSY), &drive->atapi_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502 printk(KERN_ERR "ide-tape: %s: drive not ready\n", tape->name);
1503 goto out_put_tape;
1504 }
1505
Borislav Petkov79ca7432009-06-15 07:32:04 +02001506 ide_tape_read_position(drive);
Borislav Petkov49d80782009-06-07 15:37:06 +02001507 if (!test_bit(ilog2(IDE_AFLAG_ADDRESS_VALID), &drive->atapi_flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001508 (void)idetape_rewind_tape(drive);
1509
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510 /* Read block size and write protect status from drive. */
Borislav Petkov3cffb9c2008-02-02 19:56:50 +01001511 ide_tape_get_bsize_from_bdesc(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512
1513 /* Set write protect flag if device is opened as read-only. */
1514 if ((filp->f_flags & O_ACCMODE) == O_RDONLY)
1515 tape->write_prot = 1;
1516 else
1517 tape->write_prot = tape->drv_write_prot;
1518
1519 /* Make sure drive isn't write protected if user wants to write. */
1520 if (tape->write_prot) {
1521 if ((filp->f_flags & O_ACCMODE) == O_WRONLY ||
1522 (filp->f_flags & O_ACCMODE) == O_RDWR) {
Borislav Petkov49d80782009-06-07 15:37:06 +02001523 clear_bit(ilog2(IDE_AFLAG_BUSY), &drive->atapi_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524 retval = -EROFS;
1525 goto out_put_tape;
1526 }
1527 }
1528
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001529 /* Lock the tape drive door so user can't eject. */
Borislav Petkov54abf372008-02-06 02:57:52 +01001530 if (tape->chrdev_dir == IDETAPE_DIR_NONE) {
Bartlomiej Zolnierkiewicz05780422008-10-10 22:39:38 +02001531 if (!ide_set_media_lock(drive, tape->disk, 1)) {
Bartlomiej Zolnierkiewicz385a4b82008-10-10 22:39:37 +02001532 if (tape->door_locked != DOOR_EXPLICITLY_LOCKED)
1533 tape->door_locked = DOOR_LOCKED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534 }
1535 }
Borislav Petkovcbba2fa2009-10-11 00:25:19 +00001536 mutex_unlock(&idetape_chrdev_mutex);
1537
Linus Torvalds1da177e2005-04-16 15:20:36 -07001538 return 0;
1539
1540out_put_tape:
1541 ide_tape_put(tape);
Borislav Petkovcbba2fa2009-10-11 00:25:19 +00001542
1543 mutex_unlock(&idetape_chrdev_mutex);
1544
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545 return retval;
1546}
1547
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001548static void idetape_write_release(ide_drive_t *drive, unsigned int minor)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549{
1550 idetape_tape_t *tape = drive->driver_data;
1551
Borislav Petkovd9df9372008-04-27 15:38:34 +02001552 ide_tape_flush_merge_buffer(drive);
Tejun Heo963da552009-04-19 08:46:02 +09001553 tape->buf = kmalloc(tape->buffer_size, GFP_KERNEL);
1554 if (tape->buf != NULL) {
Borislav Petkov54bb2072008-02-06 02:57:52 +01001555 idetape_pad_zeros(drive, tape->blk_size *
1556 (tape->user_bs_factor - 1));
Tejun Heo963da552009-04-19 08:46:02 +09001557 kfree(tape->buf);
1558 tape->buf = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559 }
1560 idetape_write_filemark(drive);
1561 idetape_flush_tape_buffers(drive);
1562 idetape_flush_tape_buffers(drive);
1563}
1564
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001565static int idetape_chrdev_release(struct inode *inode, struct file *filp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001566{
Borislav Petkov5aeddf92008-10-13 21:39:34 +02001567 struct ide_tape_obj *tape = filp->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001568 ide_drive_t *drive = tape->drive;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569 unsigned int minor = iminor(inode);
1570
Borislav Petkovcbba2fa2009-10-11 00:25:19 +00001571 mutex_lock(&idetape_chrdev_mutex);
1572
Linus Torvalds1da177e2005-04-16 15:20:36 -07001573 tape = drive->driver_data;
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001574
Borislav Petkove972d702009-07-17 23:55:16 +00001575 ide_debug_log(IDE_DBG_FUNC, "enter");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001576
Borislav Petkov54abf372008-02-06 02:57:52 +01001577 if (tape->chrdev_dir == IDETAPE_DIR_WRITE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001578 idetape_write_release(drive, minor);
Borislav Petkov54abf372008-02-06 02:57:52 +01001579 if (tape->chrdev_dir == IDETAPE_DIR_READ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001580 if (minor < 128)
Borislav Petkovec0fdb02008-04-27 15:38:34 +02001581 ide_tape_discard_merge_buffer(drive, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582 }
Borislav Petkovf64eee72008-04-27 15:38:25 +02001583
Borislav Petkov49d80782009-06-07 15:37:06 +02001584 if (minor < 128 && test_bit(ilog2(IDE_AFLAG_MEDIUM_PRESENT),
1585 &drive->atapi_flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001586 (void) idetape_rewind_tape(drive);
Borislav Petkov49d80782009-06-07 15:37:06 +02001587
Borislav Petkov54abf372008-02-06 02:57:52 +01001588 if (tape->chrdev_dir == IDETAPE_DIR_NONE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001589 if (tape->door_locked == DOOR_LOCKED) {
Bartlomiej Zolnierkiewicz05780422008-10-10 22:39:38 +02001590 if (!ide_set_media_lock(drive, tape->disk, 0))
Bartlomiej Zolnierkiewicz385a4b82008-10-10 22:39:37 +02001591 tape->door_locked = DOOR_UNLOCKED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592 }
1593 }
Borislav Petkov49d80782009-06-07 15:37:06 +02001594 clear_bit(ilog2(IDE_AFLAG_BUSY), &drive->atapi_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001595 ide_tape_put(tape);
Borislav Petkovcbba2fa2009-10-11 00:25:19 +00001596
1597 mutex_unlock(&idetape_chrdev_mutex);
1598
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599 return 0;
1600}
1601
Borislav Petkov6d29c8f2008-02-02 19:56:49 +01001602static void idetape_get_inquiry_results(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001603{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604 idetape_tape_t *tape = drive->driver_data;
Borislav Petkovd236d742008-04-18 00:46:27 +02001605 struct ide_atapi_pc pc;
Bartlomiej Zolnierkiewicz41fa9f82009-03-31 20:15:25 +02001606 u8 pc_buf[256];
Borislav Petkov801bd322008-09-27 19:32:17 +02001607 char fw_rev[4], vendor_id[8], product_id[16];
Borislav Petkov6d29c8f2008-02-02 19:56:49 +01001608
Linus Torvalds1da177e2005-04-16 15:20:36 -07001609 idetape_create_inquiry_cmd(&pc);
Borislav Petkovb13345f2009-05-02 10:26:12 +02001610 if (ide_queue_pc_tail(drive, tape->disk, &pc, pc_buf, pc.req_xfer)) {
Borislav Petkov6d29c8f2008-02-02 19:56:49 +01001611 printk(KERN_ERR "ide-tape: %s: can't get INQUIRY results\n",
1612 tape->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001613 return;
1614 }
Borislav Petkovb13345f2009-05-02 10:26:12 +02001615 memcpy(vendor_id, &pc_buf[8], 8);
1616 memcpy(product_id, &pc_buf[16], 16);
1617 memcpy(fw_rev, &pc_buf[32], 4);
Borislav Petkov6d29c8f2008-02-02 19:56:49 +01001618
Borislav Petkov801bd322008-09-27 19:32:17 +02001619 ide_fixstring(vendor_id, 8, 0);
1620 ide_fixstring(product_id, 16, 0);
1621 ide_fixstring(fw_rev, 4, 0);
Borislav Petkov41f81d542008-02-06 02:57:52 +01001622
Borislav Petkov801bd322008-09-27 19:32:17 +02001623 printk(KERN_INFO "ide-tape: %s <-> %s: %.8s %.16s rev %.4s\n",
Borislav Petkov41f81d542008-02-06 02:57:52 +01001624 drive->name, tape->name, vendor_id, product_id, fw_rev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001625}
1626
1627/*
Borislav Petkovb6422012008-02-02 19:56:49 +01001628 * Ask the tape about its various parameters. In particular, we will adjust our
1629 * data transfer buffer size to the recommended value as returned by the tape.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001630 */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001631static void idetape_get_mode_sense_results(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632{
1633 idetape_tape_t *tape = drive->driver_data;
Borislav Petkovd236d742008-04-18 00:46:27 +02001634 struct ide_atapi_pc pc;
Borislav Petkovb13345f2009-05-02 10:26:12 +02001635 u8 buf[24], *caps;
Borislav Petkovb6422012008-02-02 19:56:49 +01001636 u8 speed, max_speed;
Borislav Petkov47314fa2008-02-02 19:56:48 +01001637
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638 idetape_create_mode_sense_cmd(&pc, IDETAPE_CAPABILITIES_PAGE);
Borislav Petkovb13345f2009-05-02 10:26:12 +02001639 if (ide_queue_pc_tail(drive, tape->disk, &pc, buf, pc.req_xfer)) {
Borislav Petkovb6422012008-02-02 19:56:49 +01001640 printk(KERN_ERR "ide-tape: Can't get tape parameters - assuming"
1641 " some default values\n");
Borislav Petkov54bb2072008-02-06 02:57:52 +01001642 tape->blk_size = 512;
Borislav Petkovb6422012008-02-02 19:56:49 +01001643 put_unaligned(52, (u16 *)&tape->caps[12]);
1644 put_unaligned(540, (u16 *)&tape->caps[14]);
1645 put_unaligned(6*52, (u16 *)&tape->caps[16]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001646 return;
1647 }
Borislav Petkovb13345f2009-05-02 10:26:12 +02001648 caps = buf + 4 + buf[3];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001649
Borislav Petkovb6422012008-02-02 19:56:49 +01001650 /* convert to host order and save for later use */
Harvey Harrisoncd740ab2008-07-24 22:53:33 +02001651 speed = be16_to_cpup((__be16 *)&caps[14]);
1652 max_speed = be16_to_cpup((__be16 *)&caps[8]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001653
Harvey Harrisoncd740ab2008-07-24 22:53:33 +02001654 *(u16 *)&caps[8] = max_speed;
1655 *(u16 *)&caps[12] = be16_to_cpup((__be16 *)&caps[12]);
1656 *(u16 *)&caps[14] = speed;
1657 *(u16 *)&caps[16] = be16_to_cpup((__be16 *)&caps[16]);
Borislav Petkovb6422012008-02-02 19:56:49 +01001658
1659 if (!speed) {
1660 printk(KERN_INFO "ide-tape: %s: invalid tape speed "
1661 "(assuming 650KB/sec)\n", drive->name);
Harvey Harrisoncd740ab2008-07-24 22:53:33 +02001662 *(u16 *)&caps[14] = 650;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001663 }
Borislav Petkovb6422012008-02-02 19:56:49 +01001664 if (!max_speed) {
1665 printk(KERN_INFO "ide-tape: %s: invalid max_speed "
1666 "(assuming 650KB/sec)\n", drive->name);
Harvey Harrisoncd740ab2008-07-24 22:53:33 +02001667 *(u16 *)&caps[8] = 650;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001668 }
1669
Borislav Petkovb6422012008-02-02 19:56:49 +01001670 memcpy(&tape->caps, caps, 20);
Bartlomiej Zolnierkiewicz05780422008-10-10 22:39:38 +02001671
1672 /* device lacks locking support according to capabilities page */
1673 if ((caps[6] & 1) == 0)
Bartlomiej Zolnierkiewicz42619d32008-10-17 18:09:11 +02001674 drive->dev_flags &= ~IDE_DFLAG_DOORLOCKING;
Bartlomiej Zolnierkiewicz05780422008-10-10 22:39:38 +02001675
Borislav Petkovb6422012008-02-02 19:56:49 +01001676 if (caps[7] & 0x02)
Borislav Petkov54bb2072008-02-06 02:57:52 +01001677 tape->blk_size = 512;
Borislav Petkovb6422012008-02-02 19:56:49 +01001678 else if (caps[7] & 0x04)
Borislav Petkov54bb2072008-02-06 02:57:52 +01001679 tape->blk_size = 1024;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001680}
1681
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +02001682#ifdef CONFIG_IDE_PROC_FS
Bartlomiej Zolnierkiewicz8185d5a2008-10-10 22:39:28 +02001683#define ide_tape_devset_get(name, field) \
1684static int get_##name(ide_drive_t *drive) \
1685{ \
1686 idetape_tape_t *tape = drive->driver_data; \
1687 return tape->field; \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001688}
Bartlomiej Zolnierkiewicz8185d5a2008-10-10 22:39:28 +02001689
1690#define ide_tape_devset_set(name, field) \
1691static int set_##name(ide_drive_t *drive, int arg) \
1692{ \
1693 idetape_tape_t *tape = drive->driver_data; \
1694 tape->field = arg; \
1695 return 0; \
1696}
1697
Elias Oltmanns92f1f8f2008-10-10 22:39:40 +02001698#define ide_tape_devset_rw_field(_name, _field) \
Bartlomiej Zolnierkiewicz8185d5a2008-10-10 22:39:28 +02001699ide_tape_devset_get(_name, _field) \
1700ide_tape_devset_set(_name, _field) \
Elias Oltmanns92f1f8f2008-10-10 22:39:40 +02001701IDE_DEVSET(_name, DS_SYNC, get_##_name, set_##_name)
Bartlomiej Zolnierkiewicz8185d5a2008-10-10 22:39:28 +02001702
Elias Oltmanns92f1f8f2008-10-10 22:39:40 +02001703#define ide_tape_devset_r_field(_name, _field) \
Bartlomiej Zolnierkiewicz8185d5a2008-10-10 22:39:28 +02001704ide_tape_devset_get(_name, _field) \
Elias Oltmanns92f1f8f2008-10-10 22:39:40 +02001705IDE_DEVSET(_name, 0, get_##_name, NULL)
Bartlomiej Zolnierkiewicz8185d5a2008-10-10 22:39:28 +02001706
1707static int mulf_tdsc(ide_drive_t *drive) { return 1000; }
1708static int divf_tdsc(ide_drive_t *drive) { return HZ; }
1709static int divf_buffer(ide_drive_t *drive) { return 2; }
1710static int divf_buffer_size(ide_drive_t *drive) { return 1024; }
1711
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +02001712ide_devset_rw_flag(dsc_overlap, IDE_DFLAG_DSC_OVERLAP);
Bartlomiej Zolnierkiewicz8185d5a2008-10-10 22:39:28 +02001713
Elias Oltmanns92f1f8f2008-10-10 22:39:40 +02001714ide_tape_devset_rw_field(tdsc, best_dsc_rw_freq);
Bartlomiej Zolnierkiewicz8185d5a2008-10-10 22:39:28 +02001715
Elias Oltmanns92f1f8f2008-10-10 22:39:40 +02001716ide_tape_devset_r_field(avg_speed, avg_speed);
1717ide_tape_devset_r_field(speed, caps[14]);
1718ide_tape_devset_r_field(buffer, caps[16]);
1719ide_tape_devset_r_field(buffer_size, buffer_size);
Bartlomiej Zolnierkiewicz8185d5a2008-10-10 22:39:28 +02001720
Elias Oltmanns92f1f8f2008-10-10 22:39:40 +02001721static const struct ide_proc_devset idetape_settings[] = {
1722 __IDE_PROC_DEVSET(avg_speed, 0, 0xffff, NULL, NULL),
1723 __IDE_PROC_DEVSET(buffer, 0, 0xffff, NULL, divf_buffer),
1724 __IDE_PROC_DEVSET(buffer_size, 0, 0xffff, NULL, divf_buffer_size),
Elias Oltmanns92f1f8f2008-10-10 22:39:40 +02001725 __IDE_PROC_DEVSET(dsc_overlap, 0, 1, NULL, NULL),
1726 __IDE_PROC_DEVSET(speed, 0, 0xffff, NULL, NULL),
1727 __IDE_PROC_DEVSET(tdsc, IDETAPE_DSC_RW_MIN, IDETAPE_DSC_RW_MAX,
1728 mulf_tdsc, divf_tdsc),
Hannes Eder71bfc7a2009-03-05 16:10:56 +01001729 { NULL },
Bartlomiej Zolnierkiewicz8185d5a2008-10-10 22:39:28 +02001730};
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +02001731#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001732
1733/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001734 * The function below is called to:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001735 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001736 * 1. Initialize our various state variables.
1737 * 2. Ask the tape for its capabilities.
1738 * 3. Allocate a buffer which will be used for data transfer. The buffer size
1739 * is chosen based on the recommendation which we received in step 2.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001740 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001741 * Note that at this point ide.c already assigned us an irq, so that we can
1742 * queue requests here and wait for their completion.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001743 */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001744static void idetape_setup(ide_drive_t *drive, idetape_tape_t *tape, int minor)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001745{
Borislav Petkov83042b22008-04-27 15:38:27 +02001746 unsigned long t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001747 int speed;
Borislav Petkovf73850a2008-04-27 15:38:33 +02001748 int buffer_size;
Borislav Petkovb6422012008-02-02 19:56:49 +01001749 u16 *ctl = (u16 *)&tape->caps[12];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001750
Borislav Petkove972d702009-07-17 23:55:16 +00001751 ide_debug_log(IDE_DBG_FUNC, "minor: %d", minor);
1752
1753 drive->pc_callback = ide_tape_callback;
Borislav Petkov776bb022008-07-23 19:55:59 +02001754
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +02001755 drive->dev_flags |= IDE_DFLAG_DSC_OVERLAP;
1756
Bartlomiej Zolnierkiewicz4166c192008-02-01 23:09:30 +01001757 if (drive->hwif->host_flags & IDE_HFLAG_NO_DSC) {
1758 printk(KERN_INFO "ide-tape: %s: disabling DSC overlap\n",
1759 tape->name);
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +02001760 drive->dev_flags &= ~IDE_DFLAG_DSC_OVERLAP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001761 }
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +02001762
Linus Torvalds1da177e2005-04-16 15:20:36 -07001763 /* Seagate Travan drives do not support DSC overlap. */
Bartlomiej Zolnierkiewicz4dde4492008-10-10 22:39:19 +02001764 if (strstr((char *)&drive->id[ATA_ID_PROD], "Seagate STT3401"))
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +02001765 drive->dev_flags &= ~IDE_DFLAG_DSC_OVERLAP;
1766
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767 tape->minor = minor;
1768 tape->name[0] = 'h';
1769 tape->name[1] = 't';
1770 tape->name[2] = '0' + minor;
Borislav Petkov54abf372008-02-06 02:57:52 +01001771 tape->chrdev_dir = IDETAPE_DIR_NONE;
Bartlomiej Zolnierkiewicz4dde4492008-10-10 22:39:19 +02001772
Linus Torvalds1da177e2005-04-16 15:20:36 -07001773 idetape_get_inquiry_results(drive);
1774 idetape_get_mode_sense_results(drive);
Borislav Petkov3cffb9c2008-02-02 19:56:50 +01001775 ide_tape_get_bsize_from_bdesc(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001776 tape->user_bs_factor = 1;
Borislav Petkovf73850a2008-04-27 15:38:33 +02001777 tape->buffer_size = *ctl * tape->blk_size;
1778 while (tape->buffer_size > 0xffff) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001779 printk(KERN_NOTICE "ide-tape: decreasing stage size\n");
Borislav Petkovb6422012008-02-02 19:56:49 +01001780 *ctl /= 2;
Borislav Petkovf73850a2008-04-27 15:38:33 +02001781 tape->buffer_size = *ctl * tape->blk_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001782 }
Borislav Petkovf73850a2008-04-27 15:38:33 +02001783 buffer_size = tape->buffer_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001784
Borislav Petkov83042b22008-04-27 15:38:27 +02001785 /* select the "best" DSC read/write polling freq */
Borislav Petkovb6422012008-02-02 19:56:49 +01001786 speed = max(*(u16 *)&tape->caps[14], *(u16 *)&tape->caps[8]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001787
Borislav Petkovf73850a2008-04-27 15:38:33 +02001788 t = (IDETAPE_FIFO_THRESHOLD * tape->buffer_size * HZ) / (speed * 1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001789
1790 /*
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001791 * Ensure that the number we got makes sense; limit it within
1792 * IDETAPE_DSC_RW_MIN and IDETAPE_DSC_RW_MAX.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001793 */
Harvey Harrisona792bd52008-07-15 21:21:41 +02001794 tape->best_dsc_rw_freq = clamp_t(unsigned long, t, IDETAPE_DSC_RW_MIN,
1795 IDETAPE_DSC_RW_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001796 printk(KERN_INFO "ide-tape: %s <-> %s: %dKBps, %d*%dkB buffer, "
Borislav Petkov83042b22008-04-27 15:38:27 +02001797 "%lums tDSC%s\n",
Borislav Petkovb6422012008-02-02 19:56:49 +01001798 drive->name, tape->name, *(u16 *)&tape->caps[14],
Borislav Petkovf73850a2008-04-27 15:38:33 +02001799 (*(u16 *)&tape->caps[16] * 512) / tape->buffer_size,
1800 tape->buffer_size / 1024,
Borislav Petkov54bb2072008-02-06 02:57:52 +01001801 tape->best_dsc_rw_freq * 1000 / HZ,
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +02001802 (drive->dev_flags & IDE_DFLAG_USING_DMA) ? ", DMA" : "");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001803
Bartlomiej Zolnierkiewicz1e874f42008-10-10 22:39:27 +02001804 ide_proc_register_driver(drive, tape->driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805}
1806
Russell King4031bbe2006-01-06 11:41:00 +00001807static void ide_tape_remove(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001808{
1809 idetape_tape_t *tape = drive->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001810
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +02001811 ide_proc_unregister_driver(drive, tape->driver);
Bartlomiej Zolnierkiewicz8fed4362009-02-25 20:28:24 +01001812 device_del(&tape->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001813 ide_unregister_region(tape->disk);
1814
Bartlomiej Zolnierkiewicz8fed4362009-02-25 20:28:24 +01001815 mutex_lock(&idetape_ref_mutex);
1816 put_device(&tape->dev);
1817 mutex_unlock(&idetape_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001818}
1819
Bartlomiej Zolnierkiewicz8fed4362009-02-25 20:28:24 +01001820static void ide_tape_release(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001821{
Bartlomiej Zolnierkiewicz8fed4362009-02-25 20:28:24 +01001822 struct ide_tape_obj *tape = to_ide_drv(dev, ide_tape_obj);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001823 ide_drive_t *drive = tape->drive;
1824 struct gendisk *g = tape->disk;
1825
Tejun Heo963da552009-04-19 08:46:02 +09001826 BUG_ON(tape->valid);
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02001827
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +02001828 drive->dev_flags &= ~IDE_DFLAG_DSC_OVERLAP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001829 drive->driver_data = NULL;
Tony Jonesdbc12722007-09-25 02:03:03 +02001830 device_destroy(idetape_sysfs_class, MKDEV(IDETAPE_MAJOR, tape->minor));
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001831 device_destroy(idetape_sysfs_class,
1832 MKDEV(IDETAPE_MAJOR, tape->minor + 128));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001833 idetape_devs[tape->minor] = NULL;
1834 g->private_data = NULL;
1835 put_disk(g);
1836 kfree(tape);
1837}
1838
Bartlomiej Zolnierkiewiczecfd80e2007-05-10 00:01:09 +02001839#ifdef CONFIG_IDE_PROC_FS
Alexey Dobriyan6d703a82009-09-01 17:52:57 -07001840static int idetape_name_proc_show(struct seq_file *m, void *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001841{
Alexey Dobriyan6d703a82009-09-01 17:52:57 -07001842 ide_drive_t *drive = (ide_drive_t *) m->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001843 idetape_tape_t *tape = drive->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001844
Alexey Dobriyan6d703a82009-09-01 17:52:57 -07001845 seq_printf(m, "%s\n", tape->name);
1846 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001847}
1848
Alexey Dobriyan6d703a82009-09-01 17:52:57 -07001849static int idetape_name_proc_open(struct inode *inode, struct file *file)
1850{
1851 return single_open(file, idetape_name_proc_show, PDE(inode)->data);
1852}
1853
1854static const struct file_operations idetape_name_proc_fops = {
1855 .owner = THIS_MODULE,
1856 .open = idetape_name_proc_open,
1857 .read = seq_read,
1858 .llseek = seq_lseek,
1859 .release = single_release,
1860};
1861
Linus Torvalds1da177e2005-04-16 15:20:36 -07001862static ide_proc_entry_t idetape_proc[] = {
Alexey Dobriyan6d703a82009-09-01 17:52:57 -07001863 { "capacity", S_IFREG|S_IRUGO, &ide_capacity_proc_fops },
1864 { "name", S_IFREG|S_IRUGO, &idetape_name_proc_fops },
1865 {}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001866};
Bartlomiej Zolnierkiewicz79cb3802008-10-17 18:09:13 +02001867
1868static ide_proc_entry_t *ide_tape_proc_entries(ide_drive_t *drive)
1869{
1870 return idetape_proc;
1871}
1872
1873static const struct ide_proc_devset *ide_tape_proc_devsets(ide_drive_t *drive)
1874{
1875 return idetape_settings;
1876}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001877#endif
1878
Russell King4031bbe2006-01-06 11:41:00 +00001879static int ide_tape_probe(ide_drive_t *);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001880
Bartlomiej Zolnierkiewicz7f3c8682009-01-06 17:20:53 +01001881static struct ide_driver idetape_driver = {
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02001882 .gen_driver = {
Laurent Riffard4ef3b8f2005-11-18 22:15:40 +01001883 .owner = THIS_MODULE,
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02001884 .name = "ide-tape",
1885 .bus = &ide_bus_type,
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02001886 },
Russell King4031bbe2006-01-06 11:41:00 +00001887 .probe = ide_tape_probe,
1888 .remove = ide_tape_remove,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001889 .version = IDETAPE_VERSION,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001890 .do_request = idetape_do_request,
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +02001891#ifdef CONFIG_IDE_PROC_FS
Bartlomiej Zolnierkiewicz79cb3802008-10-17 18:09:13 +02001892 .proc_entries = ide_tape_proc_entries,
1893 .proc_devsets = ide_tape_proc_devsets,
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +02001894#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001895};
1896
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001897/* Our character device supporting functions, passed to register_chrdev. */
Arjan van de Ven2b8693c2007-02-12 00:55:32 -08001898static const struct file_operations idetape_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001899 .owner = THIS_MODULE,
1900 .read = idetape_chrdev_read,
1901 .write = idetape_chrdev_write,
Alan Cox05227ad2010-01-04 06:24:00 +00001902 .unlocked_ioctl = idetape_chrdev_ioctl,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001903 .open = idetape_chrdev_open,
1904 .release = idetape_chrdev_release,
Arnd Bergmann6038f372010-08-15 18:52:59 +02001905 .llseek = noop_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001906};
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{
Arnd Bergmann6e9624b2010-08-07 18:25:34 +02001910 struct ide_tape_obj *tape;
1911
Arnd Bergmann2a48fc02010-06-02 14:28:52 +02001912 mutex_lock(&ide_tape_mutex);
Arnd Bergmann6e9624b2010-08-07 18:25:34 +02001913 tape = ide_tape_get(bdev->bd_disk, false, 0);
Arnd Bergmann2a48fc02010-06-02 14:28:52 +02001914 mutex_unlock(&ide_tape_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001915
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001916 if (!tape)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001917 return -ENXIO;
1918
Linus Torvalds1da177e2005-04-16 15:20:36 -07001919 return 0;
1920}
1921
Al Viroa4600f82008-03-02 10:27:58 -05001922static int idetape_release(struct gendisk *disk, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001923{
Borislav Petkov5aeddf92008-10-13 21:39:34 +02001924 struct ide_tape_obj *tape = ide_drv_g(disk, ide_tape_obj);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001925
Arnd Bergmann2a48fc02010-06-02 14:28:52 +02001926 mutex_lock(&ide_tape_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001927 ide_tape_put(tape);
Arnd Bergmann2a48fc02010-06-02 14:28:52 +02001928 mutex_unlock(&ide_tape_mutex);
Arnd Bergmann6e9624b2010-08-07 18:25:34 +02001929
Linus Torvalds1da177e2005-04-16 15:20:36 -07001930 return 0;
1931}
1932
Al Viroa4600f82008-03-02 10:27:58 -05001933static int idetape_ioctl(struct block_device *bdev, fmode_t mode,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001934 unsigned int cmd, unsigned long arg)
1935{
Borislav Petkov5aeddf92008-10-13 21:39:34 +02001936 struct ide_tape_obj *tape = ide_drv_g(bdev->bd_disk, ide_tape_obj);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001937 ide_drive_t *drive = tape->drive;
Arnd Bergmann8a6cfeb2010-07-08 10:18:46 +02001938 int err;
1939
Arnd Bergmann2a48fc02010-06-02 14:28:52 +02001940 mutex_lock(&ide_tape_mutex);
Arnd Bergmann8a6cfeb2010-07-08 10:18:46 +02001941 err = generic_ide_ioctl(drive, bdev, cmd, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001942 if (err == -EINVAL)
1943 err = idetape_blkdev_ioctl(drive, cmd, arg);
Arnd Bergmann2a48fc02010-06-02 14:28:52 +02001944 mutex_unlock(&ide_tape_mutex);
Arnd Bergmann8a6cfeb2010-07-08 10:18:46 +02001945
Linus Torvalds1da177e2005-04-16 15:20:36 -07001946 return err;
1947}
1948
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -07001949static const struct block_device_operations idetape_block_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001950 .owner = THIS_MODULE,
Al Viroa4600f82008-03-02 10:27:58 -05001951 .open = idetape_open,
1952 .release = idetape_release,
Arnd Bergmann8a6cfeb2010-07-08 10:18:46 +02001953 .ioctl = idetape_ioctl,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001954};
1955
Russell King4031bbe2006-01-06 11:41:00 +00001956static int ide_tape_probe(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001957{
1958 idetape_tape_t *tape;
1959 struct gendisk *g;
1960 int minor;
1961
Borislav Petkove972d702009-07-17 23:55:16 +00001962 ide_debug_log(IDE_DBG_FUNC, "enter");
1963
1964 if (!strstr(DRV_NAME, drive->driver_req))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001965 goto failed;
Bartlomiej Zolnierkiewicz2a924662008-10-10 22:39:24 +02001966
Linus Torvalds1da177e2005-04-16 15:20:36 -07001967 if (drive->media != ide_tape)
1968 goto failed;
Bartlomiej Zolnierkiewicz2a924662008-10-10 22:39:24 +02001969
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +02001970 if ((drive->dev_flags & IDE_DFLAG_ID_READ) &&
1971 ide_check_atapi_device(drive, DRV_NAME) == 0) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001972 printk(KERN_ERR "ide-tape: %s: not supported by this version of"
1973 " the driver\n", drive->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001974 goto failed;
1975 }
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001976 tape = kzalloc(sizeof(idetape_tape_t), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001977 if (tape == NULL) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001978 printk(KERN_ERR "ide-tape: %s: Can't allocate a tape struct\n",
1979 drive->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001980 goto failed;
1981 }
1982
1983 g = alloc_disk(1 << PARTN_BITS);
1984 if (!g)
1985 goto out_free_tape;
1986
1987 ide_init_disk(g, drive);
1988
Bartlomiej Zolnierkiewicz8fed4362009-02-25 20:28:24 +01001989 tape->dev.parent = &drive->gendev;
1990 tape->dev.release = ide_tape_release;
1991 dev_set_name(&tape->dev, dev_name(&drive->gendev));
1992
1993 if (device_register(&tape->dev))
1994 goto out_free_disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001995
1996 tape->drive = drive;
1997 tape->driver = &idetape_driver;
1998 tape->disk = g;
1999
2000 g->private_data = &tape->driver;
2001
2002 drive->driver_data = tape;
2003
Arjan van de Vencf8b8972006-03-23 03:00:45 -08002004 mutex_lock(&idetape_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002005 for (minor = 0; idetape_devs[minor]; minor++)
2006 ;
2007 idetape_devs[minor] = tape;
Arjan van de Vencf8b8972006-03-23 03:00:45 -08002008 mutex_unlock(&idetape_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002009
2010 idetape_setup(drive, tape, minor);
2011
Greg Kroah-Hartman3ee074b2008-07-21 20:03:34 -07002012 device_create(idetape_sysfs_class, &drive->gendev,
2013 MKDEV(IDETAPE_MAJOR, minor), NULL, "%s", tape->name);
2014 device_create(idetape_sysfs_class, &drive->gendev,
2015 MKDEV(IDETAPE_MAJOR, minor + 128), NULL,
2016 "n%s", tape->name);
Will Dysond5dee802005-09-16 02:55:07 -07002017
Linus Torvalds1da177e2005-04-16 15:20:36 -07002018 g->fops = &idetape_block_ops;
2019 ide_register_region(g);
2020
2021 return 0;
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02002022
Bartlomiej Zolnierkiewicz8fed4362009-02-25 20:28:24 +01002023out_free_disk:
2024 put_disk(g);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002025out_free_tape:
2026 kfree(tape);
2027failed:
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02002028 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002029}
2030
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002031static void __exit idetape_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002032{
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02002033 driver_unregister(&idetape_driver.gen_driver);
Will Dysond5dee802005-09-16 02:55:07 -07002034 class_destroy(idetape_sysfs_class);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002035 unregister_chrdev(IDETAPE_MAJOR, "ht");
2036}
2037
Bartlomiej Zolnierkiewicz17514e82005-11-19 22:24:35 +01002038static int __init idetape_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002039{
Will Dysond5dee802005-09-16 02:55:07 -07002040 int error = 1;
2041 idetape_sysfs_class = class_create(THIS_MODULE, "ide_tape");
2042 if (IS_ERR(idetape_sysfs_class)) {
2043 idetape_sysfs_class = NULL;
2044 printk(KERN_ERR "Unable to create sysfs class for ide tapes\n");
2045 error = -EBUSY;
2046 goto out;
2047 }
2048
Linus Torvalds1da177e2005-04-16 15:20:36 -07002049 if (register_chrdev(IDETAPE_MAJOR, "ht", &idetape_fops)) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002050 printk(KERN_ERR "ide-tape: Failed to register chrdev"
2051 " interface\n");
Will Dysond5dee802005-09-16 02:55:07 -07002052 error = -EBUSY;
2053 goto out_free_class;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002054 }
Will Dysond5dee802005-09-16 02:55:07 -07002055
2056 error = driver_register(&idetape_driver.gen_driver);
2057 if (error)
2058 goto out_free_driver;
2059
2060 return 0;
2061
2062out_free_driver:
2063 driver_unregister(&idetape_driver.gen_driver);
2064out_free_class:
2065 class_destroy(idetape_sysfs_class);
2066out:
2067 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002068}
2069
Kay Sievers263756e2005-12-12 18:03:44 +01002070MODULE_ALIAS("ide:*m-tape*");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002071module_init(idetape_init);
2072module_exit(idetape_exit);
2073MODULE_ALIAS_CHARDEV_MAJOR(IDETAPE_MAJOR);
Borislav Petkov9c145762008-02-06 02:57:54 +01002074MODULE_DESCRIPTION("ATAPI Streaming TAPE Driver");
2075MODULE_LICENSE("GPL");