blob: 51ea59e3f6ad81b22e803330f47d9c010c86bee9 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Borislav Petkov5ce78af2008-02-02 19:56:48 +01002 * IDE ATAPI streaming tape driver.
3 *
Bartlomiej Zolnierkiewicz59bca8c2008-02-01 23:09:33 +01004 * Copyright (C) 1995-1999 Gadi Oxman <gadio@netvision.net.il>
5 * Copyright (C) 2003-2005 Bartlomiej Zolnierkiewicz
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 * This driver was constructed as a student project in the software laboratory
8 * of the faculty of electrical engineering in the Technion - Israel's
9 * Institute Of Technology, with the guide of Avner Lottem and Dr. Ilana David.
10 *
11 * It is hereby placed under the terms of the GNU general public license.
12 * (See linux/COPYING).
Linus Torvalds1da177e2005-04-16 15:20:36 -070013 *
Borislav Petkov5ce78af2008-02-02 19:56:48 +010014 * For a historical changelog see
15 * Documentation/ide/ChangeLog.ide-tape.1995-2002
Linus Torvalds1da177e2005-04-16 15:20:36 -070016 */
17
Bartlomiej Zolnierkiewicz51509ee2008-10-10 22:39:34 +020018#define DRV_NAME "ide-tape"
19
Borislav Petkovdfe79932008-02-06 02:57:55 +010020#define IDETAPE_VERSION "1.20"
Linus Torvalds1da177e2005-04-16 15:20:36 -070021
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include <linux/module.h>
23#include <linux/types.h>
24#include <linux/string.h>
25#include <linux/kernel.h>
26#include <linux/delay.h>
27#include <linux/timer.h>
28#include <linux/mm.h>
29#include <linux/interrupt.h>
Marcelo Feitoza Parisi9bae1ff2006-03-28 01:56:46 -080030#include <linux/jiffies.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#include <linux/major.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070032#include <linux/errno.h>
33#include <linux/genhd.h>
34#include <linux/slab.h>
35#include <linux/pci.h>
36#include <linux/ide.h>
37#include <linux/smp_lock.h>
38#include <linux/completion.h>
39#include <linux/bitops.h>
Arjan van de Vencf8b8972006-03-23 03:00:45 -080040#include <linux/mutex.h>
Borislav Petkov90699ce2008-02-02 19:56:50 +010041#include <scsi/scsi.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070042
43#include <asm/byteorder.h>
Borislav Petkovc837cfa2008-02-06 02:57:54 +010044#include <linux/irq.h>
45#include <linux/uaccess.h>
46#include <linux/io.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070047#include <asm/unaligned.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070048#include <linux/mtio.h>
49
Borislav Petkov8004a8c2008-02-06 02:57:51 +010050enum {
51 /* output errors only */
52 DBG_ERR = (1 << 0),
53 /* output all sense key/asc */
54 DBG_SENSE = (1 << 1),
55 /* info regarding all chrdev-related procedures */
56 DBG_CHRDEV = (1 << 2),
57 /* all remaining procedures */
58 DBG_PROCS = (1 << 3),
Borislav Petkov8004a8c2008-02-06 02:57:51 +010059};
60
61/* define to see debug info */
62#define IDETAPE_DEBUG_LOG 0
63
64#if IDETAPE_DEBUG_LOG
65#define debug_log(lvl, fmt, args...) \
66{ \
67 if (tape->debug_mask & lvl) \
68 printk(KERN_INFO "ide-tape: " fmt, ## args); \
69}
70#else
71#define debug_log(lvl, fmt, args...) do {} while (0)
72#endif
73
Linus Torvalds1da177e2005-04-16 15:20:36 -070074/**************************** Tunable parameters *****************************/
Linus Torvalds1da177e2005-04-16 15:20:36 -070075/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +010076 * After each failed packet command we issue a request sense command and retry
77 * the packet command IDETAPE_MAX_PC_RETRIES times.
Linus Torvalds1da177e2005-04-16 15:20:36 -070078 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +010079 * Setting IDETAPE_MAX_PC_RETRIES to 0 will disable retries.
Linus Torvalds1da177e2005-04-16 15:20:36 -070080 */
81#define IDETAPE_MAX_PC_RETRIES 3
82
83/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +010084 * The following parameter is used to select the point in the internal tape fifo
85 * in which we will start to refill the buffer. Decreasing the following
86 * parameter will improve the system's latency and interactive response, while
87 * using a high value might improve system throughput.
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 */
Borislav Petkov3c98bf32008-02-06 02:57:53 +010089#define IDETAPE_FIFO_THRESHOLD 2
Linus Torvalds1da177e2005-04-16 15:20:36 -070090
91/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +010092 * DSC polling parameters.
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +010094 * Polling for DSC (a single bit in the status register) is a very important
95 * function in ide-tape. There are two cases in which we poll for DSC:
Linus Torvalds1da177e2005-04-16 15:20:36 -070096 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +010097 * 1. Before a read/write packet command, to ensure that we can transfer data
98 * from/to the tape's data buffers, without causing an actual media access.
99 * In case the tape is not ready yet, we take out our request from the device
100 * request queue, so that ide.c could service requests from the other device
101 * on the same interface in the meantime.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100103 * 2. After the successful initialization of a "media access packet command",
104 * which is a command that can take a long time to complete (the interval can
105 * range from several seconds to even an hour). Again, we postpone our request
106 * in the middle to free the bus for the other device. The polling frequency
107 * here should be lower than the read/write frequency since those media access
108 * commands are slow. We start from a "fast" frequency - IDETAPE_DSC_MA_FAST
109 * (1 second), and if we don't receive DSC after IDETAPE_DSC_MA_THRESHOLD
110 * (5 min), we switch it to a lower frequency - IDETAPE_DSC_MA_SLOW (1 min).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100112 * We also set a timeout for the timer, in case something goes wrong. The
113 * timeout should be longer then the maximum execution time of a tape operation.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 */
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100115
116/* DSC timings. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117#define IDETAPE_DSC_RW_MIN 5*HZ/100 /* 50 msec */
118#define IDETAPE_DSC_RW_MAX 40*HZ/100 /* 400 msec */
119#define IDETAPE_DSC_RW_TIMEOUT 2*60*HZ /* 2 minutes */
120#define IDETAPE_DSC_MA_FAST 2*HZ /* 2 seconds */
121#define IDETAPE_DSC_MA_THRESHOLD 5*60*HZ /* 5 minutes */
122#define IDETAPE_DSC_MA_SLOW 30*HZ /* 30 seconds */
123#define IDETAPE_DSC_MA_TIMEOUT 2*60*60*HZ /* 2 hours */
124
125/*************************** End of tunable parameters ***********************/
126
Borislav Petkov54abf372008-02-06 02:57:52 +0100127/* tape directions */
128enum {
129 IDETAPE_DIR_NONE = (1 << 0),
130 IDETAPE_DIR_READ = (1 << 1),
131 IDETAPE_DIR_WRITE = (1 << 2),
132};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133
Borislav Petkov03056b92008-04-18 00:46:26 +0200134/* Tape door status */
135#define DOOR_UNLOCKED 0
136#define DOOR_LOCKED 1
137#define DOOR_EXPLICITLY_LOCKED 2
138
139/* Some defines for the SPACE command */
140#define IDETAPE_SPACE_OVER_FILEMARK 1
141#define IDETAPE_SPACE_TO_EOD 3
142
143/* Some defines for the LOAD UNLOAD command */
144#define IDETAPE_LU_LOAD_MASK 1
145#define IDETAPE_LU_RETENSION_MASK 2
146#define IDETAPE_LU_EOT_MASK 4
147
Borislav Petkov03056b92008-04-18 00:46:26 +0200148/* Structures related to the SELECT SENSE / MODE SENSE packet commands. */
149#define IDETAPE_BLOCK_DESCRIPTOR 0
150#define IDETAPE_CAPABILITIES_PAGE 0x2a
151
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100153 * Most of our global data which we need to save even as we leave the driver due
154 * to an interrupt or a timer event is stored in the struct defined below.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 */
156typedef struct ide_tape_obj {
Bartlomiej Zolnierkiewicz7f3c8682009-01-06 17:20:53 +0100157 ide_drive_t *drive;
158 struct ide_driver *driver;
159 struct gendisk *disk;
Bartlomiej Zolnierkiewicz8fed4362009-02-25 20:28:24 +0100160 struct device dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161
Bartlomiej Zolnierkiewicz2e8a6f82008-10-10 22:39:36 +0200162 /* used by REQ_IDETAPE_{READ,WRITE} requests */
163 struct ide_atapi_pc queued_pc;
Bartlomiej Zolnierkiewicz394a4c22008-10-10 22:39:35 +0200164
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 /*
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100166 * DSC polling variables.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100168 * While polling for DSC we use postponed_rq to postpone the current
169 * request so that ide.c will be able to service pending requests on the
170 * other device. Note that at most we will have only one DSC (usually
Borislav Petkov5bd50dc2008-04-27 15:38:28 +0200171 * data transfer) request in the device request queue.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 */
173 struct request *postponed_rq;
174 /* The time in which we started polling for DSC */
175 unsigned long dsc_polling_start;
176 /* Timer used to poll for dsc */
177 struct timer_list dsc_timer;
178 /* Read/Write dsc polling frequency */
Borislav Petkov54bb2072008-02-06 02:57:52 +0100179 unsigned long best_dsc_rw_freq;
180 unsigned long dsc_poll_freq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 unsigned long dsc_timeout;
182
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100183 /* Read position information */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 u8 partition;
185 /* Current block */
Borislav Petkov54bb2072008-02-06 02:57:52 +0100186 unsigned int first_frame;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100188 /* Last error information */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 u8 sense_key, asc, ascq;
190
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100191 /* Character device operation */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 unsigned int minor;
193 /* device name */
194 char name[4];
195 /* Current character device data transfer direction */
Borislav Petkov54abf372008-02-06 02:57:52 +0100196 u8 chrdev_dir;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197
Borislav Petkov54bb2072008-02-06 02:57:52 +0100198 /* tape block size, usually 512 or 1024 bytes */
199 unsigned short blk_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 int user_bs_factor;
Borislav Petkovb6422012008-02-02 19:56:49 +0100201
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 /* Copy of the tape's Capabilities and Mechanical Page */
Borislav Petkovb6422012008-02-02 19:56:49 +0100203 u8 caps[20];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204
205 /*
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100206 * Active data transfer request parameters.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100208 * At most, there is only one ide-tape originated data transfer request
209 * in the device request queue. This allows ide.c to easily service
210 * requests from the other device when we postpone our active request.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 */
Borislav Petkov83042b22008-04-27 15:38:27 +0200212
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100213 /* Data buffer size chosen based on the tape's recommendation */
Borislav Petkovf73850a2008-04-27 15:38:33 +0200214 int buffer_size;
Tejun Heo963da552009-04-19 08:46:02 +0900215 /* Staging buffer of buffer_size bytes */
216 void *buf;
217 /* The read/write cursor */
218 void *cur;
219 /* The number of valid bytes in buf */
220 size_t valid;
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100221
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100222 /* Measures average tape speed */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 unsigned long avg_time;
224 int avg_size;
225 int avg_speed;
226
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 /* the door is currently locked */
228 int door_locked;
229 /* the tape hardware is write protected */
230 char drv_write_prot;
231 /* the tape is write protected (hardware or opened as read-only) */
232 char write_prot;
233
Borislav Petkov8004a8c2008-02-06 02:57:51 +0100234 u32 debug_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235} idetape_tape_t;
236
Arjan van de Vencf8b8972006-03-23 03:00:45 -0800237static DEFINE_MUTEX(idetape_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238
Will Dysond5dee802005-09-16 02:55:07 -0700239static struct class *idetape_sysfs_class;
240
Bartlomiej Zolnierkiewicz8fed4362009-02-25 20:28:24 +0100241static void ide_tape_release(struct device *);
Bartlomiej Zolnierkiewicz08da5912008-07-24 22:53:15 +0200242
Borislav Petkov9d01e4c2009-06-08 22:03:03 +0200243static struct ide_tape_obj *idetape_devs[MAX_HWIFS * MAX_DRIVES];
244
245static struct ide_tape_obj *ide_tape_get(struct gendisk *disk, bool cdev,
246 unsigned int i)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247{
248 struct ide_tape_obj *tape = NULL;
249
Arjan van de Vencf8b8972006-03-23 03:00:45 -0800250 mutex_lock(&idetape_ref_mutex);
Borislav Petkov9d01e4c2009-06-08 22:03:03 +0200251
252 if (cdev)
253 tape = idetape_devs[i];
254 else
255 tape = ide_drv_g(disk, ide_tape_obj);
256
Bartlomiej Zolnierkiewicz08da5912008-07-24 22:53:15 +0200257 if (tape) {
Bartlomiej Zolnierkiewiczd3e33ff2008-08-05 18:16:59 +0200258 if (ide_device_get(tape->drive))
Bartlomiej Zolnierkiewicz08da5912008-07-24 22:53:15 +0200259 tape = NULL;
Bartlomiej Zolnierkiewiczd3e33ff2008-08-05 18:16:59 +0200260 else
Bartlomiej Zolnierkiewicz8fed4362009-02-25 20:28:24 +0100261 get_device(&tape->dev);
Bartlomiej Zolnierkiewicz08da5912008-07-24 22:53:15 +0200262 }
Borislav Petkov9d01e4c2009-06-08 22:03:03 +0200263
Arjan van de Vencf8b8972006-03-23 03:00:45 -0800264 mutex_unlock(&idetape_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 return tape;
266}
267
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268static void ide_tape_put(struct ide_tape_obj *tape)
269{
Bartlomiej Zolnierkiewiczd3e33ff2008-08-05 18:16:59 +0200270 ide_drive_t *drive = tape->drive;
271
Arjan van de Vencf8b8972006-03-23 03:00:45 -0800272 mutex_lock(&idetape_ref_mutex);
Bartlomiej Zolnierkiewicz8fed4362009-02-25 20:28:24 +0100273 put_device(&tape->dev);
Bartlomiej Zolnierkiewiczd3e33ff2008-08-05 18:16:59 +0200274 ide_device_put(drive);
Arjan van de Vencf8b8972006-03-23 03:00:45 -0800275 mutex_unlock(&idetape_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276}
277
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278/*
Borislav Petkov1b5db432008-02-02 19:56:48 +0100279 * called on each failed packet command retry to analyze the request sense. We
280 * currently do not utilize this information.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 */
Borislav Petkov1b5db432008-02-02 19:56:48 +0100282static void idetape_analyze_error(ide_drive_t *drive, u8 *sense)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283{
284 idetape_tape_t *tape = drive->driver_data;
Bartlomiej Zolnierkiewicz5e2040fd2009-03-27 12:46:34 +0100285 struct ide_atapi_pc *pc = drive->failed_pc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286
Borislav Petkov1b5db432008-02-02 19:56:48 +0100287 tape->sense_key = sense[2] & 0xF;
288 tape->asc = sense[12];
289 tape->ascq = sense[13];
Borislav Petkov8004a8c2008-02-06 02:57:51 +0100290
291 debug_log(DBG_ERR, "pc = %x, sense key = %x, asc = %x, ascq = %x\n",
292 pc->c[0], tape->sense_key, tape->asc, tape->ascq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293
Borislav Petkovd236d742008-04-18 00:46:27 +0200294 /* Correct pc->xferred by asking the tape. */
Tejun Heo21d9c5d2009-04-19 08:46:02 +0900295 if (pc->flags & PC_FLAG_DMA_ERROR)
Borislav Petkovd236d742008-04-18 00:46:27 +0200296 pc->xferred = pc->req_xfer -
Borislav Petkov54bb2072008-02-06 02:57:52 +0100297 tape->blk_size *
Harvey Harrison5d0cc8a2008-07-15 21:21:41 +0200298 get_unaligned_be32(&sense[3]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299
300 /*
301 * If error was the result of a zero-length read or write command,
302 * with sense key=5, asc=0x22, ascq=0, let it slide. Some drives
303 * (i.e. Seagate STT3401A Travan) don't support 0-length read/writes.
304 */
Borislav Petkov90699ce2008-02-02 19:56:50 +0100305 if ((pc->c[0] == READ_6 || pc->c[0] == WRITE_6)
Borislav Petkov1b5db432008-02-02 19:56:48 +0100306 /* length == 0 */
307 && pc->c[4] == 0 && pc->c[3] == 0 && pc->c[2] == 0) {
308 if (tape->sense_key == 5) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 /* don't report an error, everything's ok */
310 pc->error = 0;
311 /* don't retry read/write */
Borislav Petkov346331f2008-04-18 00:46:26 +0200312 pc->flags |= PC_FLAG_ABORT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 }
314 }
Borislav Petkov90699ce2008-02-02 19:56:50 +0100315 if (pc->c[0] == READ_6 && (sense[2] & 0x80)) {
Bartlomiej Zolnierkiewiczc152cc12009-03-27 12:46:34 +0100316 pc->error = IDE_DRV_ERROR_FILEMARK;
Borislav Petkov346331f2008-04-18 00:46:26 +0200317 pc->flags |= PC_FLAG_ABORT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 }
Borislav Petkov90699ce2008-02-02 19:56:50 +0100319 if (pc->c[0] == WRITE_6) {
Borislav Petkov1b5db432008-02-02 19:56:48 +0100320 if ((sense[2] & 0x40) || (tape->sense_key == 0xd
321 && tape->asc == 0x0 && tape->ascq == 0x2)) {
Bartlomiej Zolnierkiewiczc152cc12009-03-27 12:46:34 +0100322 pc->error = IDE_DRV_ERROR_EOD;
Borislav Petkov346331f2008-04-18 00:46:26 +0200323 pc->flags |= PC_FLAG_ABORT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 }
325 }
Borislav Petkov90699ce2008-02-02 19:56:50 +0100326 if (pc->c[0] == READ_6 || pc->c[0] == WRITE_6) {
Borislav Petkov1b5db432008-02-02 19:56:48 +0100327 if (tape->sense_key == 8) {
Bartlomiej Zolnierkiewiczc152cc12009-03-27 12:46:34 +0100328 pc->error = IDE_DRV_ERROR_EOD;
Borislav Petkov346331f2008-04-18 00:46:26 +0200329 pc->flags |= PC_FLAG_ABORT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 }
Borislav Petkov346331f2008-04-18 00:46:26 +0200331 if (!(pc->flags & PC_FLAG_ABORT) &&
Borislav Petkovd236d742008-04-18 00:46:27 +0200332 pc->xferred)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 pc->retries = IDETAPE_MAX_PC_RETRIES + 1;
334 }
335}
336
Bartlomiej Zolnierkiewiczb14c7212008-10-13 21:39:30 +0200337static void ide_tape_handle_dsc(ide_drive_t *);
338
Bartlomiej Zolnierkiewicz03a2faa2009-03-27 12:46:36 +0100339static int ide_tape_callback(ide_drive_t *drive, int dsc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340{
341 idetape_tape_t *tape = drive->driver_data;
Bartlomiej Zolnierkiewicz2b9efba2008-10-13 21:39:31 +0200342 struct ide_atapi_pc *pc = drive->pc;
Bartlomiej Zolnierkiewicz313afea2009-03-27 12:46:34 +0100343 struct request *rq = drive->hwif->rq;
Bartlomiej Zolnierkiewicz5985e6a2008-07-15 21:21:55 +0200344 int uptodate = pc->error ? 0 : 1;
Bartlomiej Zolnierkiewicz313afea2009-03-27 12:46:34 +0100345 int err = uptodate ? 0 : IDE_DRV_ERROR_GENERAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346
Borislav Petkov8004a8c2008-02-06 02:57:51 +0100347 debug_log(DBG_PROCS, "Enter %s\n", __func__);
348
Bartlomiej Zolnierkiewiczb14c7212008-10-13 21:39:30 +0200349 if (dsc)
350 ide_tape_handle_dsc(drive);
351
Bartlomiej Zolnierkiewicz5e2040fd2009-03-27 12:46:34 +0100352 if (drive->failed_pc == pc)
353 drive->failed_pc = NULL;
Bartlomiej Zolnierkiewiczdd2e9a02008-07-15 21:22:01 +0200354
Bartlomiej Zolnierkiewicz5985e6a2008-07-15 21:21:55 +0200355 if (pc->c[0] == REQUEST_SENSE) {
356 if (uptodate)
357 idetape_analyze_error(drive, pc->buf);
358 else
359 printk(KERN_ERR "ide-tape: Error in REQUEST SENSE "
360 "itself - Aborting request!\n");
361 } else if (pc->c[0] == READ_6 || pc->c[0] == WRITE_6) {
Bartlomiej Zolnierkiewicz5985e6a2008-07-15 21:21:55 +0200362 int blocks = pc->xferred / tape->blk_size;
363
364 tape->avg_size += blocks * tape->blk_size;
365
366 if (time_after_eq(jiffies, tape->avg_time + HZ)) {
367 tape->avg_speed = tape->avg_size * HZ /
368 (jiffies - tape->avg_time) / 1024;
369 tape->avg_size = 0;
370 tape->avg_time = jiffies;
371 }
372
373 tape->first_frame += blocks;
Tejun Heo21d9c5d2009-04-19 08:46:02 +0900374 rq->data_len -= blocks * tape->blk_size;
Bartlomiej Zolnierkiewicz5985e6a2008-07-15 21:21:55 +0200375
Bartlomiej Zolnierkiewicz313afea2009-03-27 12:46:34 +0100376 if (pc->error) {
377 uptodate = 0;
378 err = pc->error;
379 }
Bartlomiej Zolnierkiewicz5985e6a2008-07-15 21:21:55 +0200380 } else if (pc->c[0] == READ_POSITION && uptodate) {
Bartlomiej Zolnierkiewicz2b9efba2008-10-13 21:39:31 +0200381 u8 *readpos = pc->buf;
Bartlomiej Zolnierkiewicz5985e6a2008-07-15 21:21:55 +0200382
383 debug_log(DBG_SENSE, "BOP - %s\n",
384 (readpos[0] & 0x80) ? "Yes" : "No");
385 debug_log(DBG_SENSE, "EOP - %s\n",
386 (readpos[0] & 0x40) ? "Yes" : "No");
387
388 if (readpos[0] & 0x4) {
389 printk(KERN_INFO "ide-tape: Block location is unknown"
390 "to the tape\n");
Borislav Petkov49d80782009-06-07 15:37:06 +0200391 clear_bit(ilog2(IDE_AFLAG_ADDRESS_VALID),
392 &drive->atapi_flags);
Bartlomiej Zolnierkiewicz5985e6a2008-07-15 21:21:55 +0200393 uptodate = 0;
Bartlomiej Zolnierkiewicz313afea2009-03-27 12:46:34 +0100394 err = IDE_DRV_ERROR_GENERAL;
Bartlomiej Zolnierkiewicz5985e6a2008-07-15 21:21:55 +0200395 } else {
396 debug_log(DBG_SENSE, "Block Location - %u\n",
Harvey Harrisoncd740ab2008-07-24 22:53:33 +0200397 be32_to_cpup((__be32 *)&readpos[4]));
Bartlomiej Zolnierkiewicz5985e6a2008-07-15 21:21:55 +0200398
399 tape->partition = readpos[1];
Harvey Harrisoncd740ab2008-07-24 22:53:33 +0200400 tape->first_frame = be32_to_cpup((__be32 *)&readpos[4]);
Borislav Petkov49d80782009-06-07 15:37:06 +0200401 set_bit(ilog2(IDE_AFLAG_ADDRESS_VALID),
402 &drive->atapi_flags);
Bartlomiej Zolnierkiewicz5985e6a2008-07-15 21:21:55 +0200403 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 }
Bartlomiej Zolnierkiewicz5985e6a2008-07-15 21:21:55 +0200405
Bartlomiej Zolnierkiewicz313afea2009-03-27 12:46:34 +0100406 rq->errors = err;
407
Bartlomiej Zolnierkiewicz03a2faa2009-03-27 12:46:36 +0100408 return uptodate;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409}
410
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100412 * Postpone the current request so that ide.c will be able to service requests
Bartlomiej Zolnierkiewiczb65fac32009-01-06 17:20:50 +0100413 * from another device on the same port while we are polling for DSC.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100415static void idetape_postpone_request(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416{
417 idetape_tape_t *tape = drive->driver_data;
418
Borislav Petkov8004a8c2008-02-06 02:57:51 +0100419 debug_log(DBG_PROCS, "Enter %s\n", __func__);
420
Bartlomiej Zolnierkiewiczb65fac32009-01-06 17:20:50 +0100421 tape->postponed_rq = drive->hwif->rq;
422
Borislav Petkov54bb2072008-02-06 02:57:52 +0100423 ide_stall_queue(drive, tape->dsc_poll_freq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424}
425
Bartlomiej Zolnierkiewicz74e63e742008-07-15 21:22:01 +0200426static void ide_tape_handle_dsc(ide_drive_t *drive)
427{
428 idetape_tape_t *tape = drive->driver_data;
429
430 /* Media access command */
431 tape->dsc_polling_start = jiffies;
432 tape->dsc_poll_freq = IDETAPE_DSC_MA_FAST;
433 tape->dsc_timeout = jiffies + IDETAPE_DSC_MA_TIMEOUT;
434 /* Allow ide.c to handle other requests */
435 idetape_postpone_request(drive);
436}
437
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100439 * Packet Command Interface
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 *
Bartlomiej Zolnierkiewicz2b9efba2008-10-13 21:39:31 +0200441 * The current Packet Command is available in drive->pc, and will not change
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100442 * until we finish handling it. Each packet command is associated with a
443 * callback function that will be called when the command is finished.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100445 * The handling will be done in three stages:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 *
Bartlomiej Zolnierkiewiczb788ee92009-03-27 12:46:46 +0100447 * 1. ide_tape_issue_pc will send the packet command to the drive, and will set
Bartlomiej Zolnierkiewiczaa5d2de72008-10-13 21:39:32 +0200448 * the interrupt handler to ide_pc_intr.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 *
Bartlomiej Zolnierkiewiczaa5d2de72008-10-13 21:39:32 +0200450 * 2. On each interrupt, ide_pc_intr will be called. This step will be
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100451 * repeated until the device signals us that no more interrupts will be issued.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100453 * 3. ATAPI Tape media access commands have immediate status with a delayed
454 * process. In case of a successful initiation of a media access packet command,
455 * the DSC bit will be set when the actual execution of the command is finished.
456 * Since the tape drive will not issue an interrupt, we have to poll for this
457 * event. In this case, we define the request as "low priority request" by
458 * setting rq_status to IDETAPE_RQ_POSTPONED, set a timer to poll for DSC and
459 * exit the driver.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100461 * ide.c will then give higher priority to requests which originate from the
462 * other device, until will change rq_status to RQ_ACTIVE.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100464 * 4. When the packet command is finished, it will be checked for errors.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100466 * 5. In case an error was found, we queue a request sense packet command in
467 * front of the request queue and retry the operation up to
468 * IDETAPE_MAX_PC_RETRIES times.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100470 * 6. In case no error was found, or we decided to give up and not to retry
471 * again, the callback function will be called and then we will handle the next
472 * request.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474
Bartlomiej Zolnierkiewiczb788ee92009-03-27 12:46:46 +0100475static ide_startstop_t ide_tape_issue_pc(ide_drive_t *drive,
476 struct ide_cmd *cmd,
477 struct ide_atapi_pc *pc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 idetape_tape_t *tape = drive->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480
Bartlomiej Zolnierkiewicz5e2040fd2009-03-27 12:46:34 +0100481 if (drive->failed_pc == NULL && pc->c[0] != REQUEST_SENSE)
482 drive->failed_pc = pc;
Bartlomiej Zolnierkiewicz2b9efba2008-10-13 21:39:31 +0200483
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 /* Set the current packet command */
Bartlomiej Zolnierkiewicz2b9efba2008-10-13 21:39:31 +0200485 drive->pc = pc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486
487 if (pc->retries > IDETAPE_MAX_PC_RETRIES ||
Borislav Petkov346331f2008-04-18 00:46:26 +0200488 (pc->flags & PC_FLAG_ABORT)) {
Tejun Heob3071d12009-04-19 08:46:02 +0900489 unsigned int done = blk_rq_bytes(drive->hwif->rq);
490
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 /*
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100492 * We will "abort" retrying a packet command in case legitimate
493 * error code was received (crossing a filemark, or end of the
494 * media, for example).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 */
Borislav Petkov346331f2008-04-18 00:46:26 +0200496 if (!(pc->flags & PC_FLAG_ABORT)) {
Borislav Petkov90699ce2008-02-02 19:56:50 +0100497 if (!(pc->c[0] == TEST_UNIT_READY &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 tape->sense_key == 2 && tape->asc == 4 &&
499 (tape->ascq == 1 || tape->ascq == 8))) {
500 printk(KERN_ERR "ide-tape: %s: I/O error, "
501 "pc = %2x, key = %2x, "
502 "asc = %2x, ascq = %2x\n",
503 tape->name, pc->c[0],
504 tape->sense_key, tape->asc,
505 tape->ascq);
506 }
507 /* Giving up */
Bartlomiej Zolnierkiewiczc152cc12009-03-27 12:46:34 +0100508 pc->error = IDE_DRV_ERROR_GENERAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 }
Tejun Heob3071d12009-04-19 08:46:02 +0900510
Bartlomiej Zolnierkiewicz5e2040fd2009-03-27 12:46:34 +0100511 drive->failed_pc = NULL;
Bartlomiej Zolnierkiewiczb14c7212008-10-13 21:39:30 +0200512 drive->pc_callback(drive, 0);
Tejun Heob3071d12009-04-19 08:46:02 +0900513 ide_complete_rq(drive, -EIO, done);
Bartlomiej Zolnierkiewicz92f5daf2008-07-15 21:21:55 +0200514 return ide_stopped;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 }
Borislav Petkov8004a8c2008-02-06 02:57:51 +0100516 debug_log(DBG_SENSE, "Retry #%d, cmd = %02X\n", pc->retries, pc->c[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517
518 pc->retries++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519
Bartlomiej Zolnierkiewiczb788ee92009-03-27 12:46:46 +0100520 return ide_issue_pc(drive, cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521}
522
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100523/* A mode sense command is used to "sense" tape parameters. */
Borislav Petkovd236d742008-04-18 00:46:27 +0200524static void idetape_create_mode_sense_cmd(struct ide_atapi_pc *pc, u8 page_code)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525{
Bartlomiej Zolnierkiewicz7bf74202008-10-10 22:39:37 +0200526 ide_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +0100527 pc->c[0] = MODE_SENSE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 if (page_code != IDETAPE_BLOCK_DESCRIPTOR)
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100529 /* DBD = 1 - Don't return block descriptors */
530 pc->c[1] = 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 pc->c[2] = page_code;
532 /*
533 * Changed pc->c[3] to 0 (255 will at best return unused info).
534 *
535 * For SCSI this byte is defined as subpage instead of high byte
536 * of length and some IDE drives seem to interpret it this way
537 * and return an error when 255 is used.
538 */
539 pc->c[3] = 0;
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100540 /* We will just discard data in that case */
541 pc->c[4] = 255;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 if (page_code == IDETAPE_BLOCK_DESCRIPTOR)
Borislav Petkovd236d742008-04-18 00:46:27 +0200543 pc->req_xfer = 12;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 else if (page_code == IDETAPE_CAPABILITIES_PAGE)
Borislav Petkovd236d742008-04-18 00:46:27 +0200545 pc->req_xfer = 24;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 else
Borislav Petkovd236d742008-04-18 00:46:27 +0200547 pc->req_xfer = 50;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548}
549
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100550static ide_startstop_t idetape_media_access_finished(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551{
Bartlomiej Zolnierkiewiczb73c7ee2008-07-23 19:55:52 +0200552 ide_hwif_t *hwif = drive->hwif;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 idetape_tape_t *tape = drive->driver_data;
Bartlomiej Zolnierkiewicz2b9efba2008-10-13 21:39:31 +0200554 struct ide_atapi_pc *pc = drive->pc;
Bartlomiej Zolnierkiewicz22c525b2008-01-25 22:17:11 +0100555 u8 stat;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556
Bartlomiej Zolnierkiewicz374e0422008-07-23 19:55:56 +0200557 stat = hwif->tp_ops->read_status(hwif);
Bartlomiej Zolnierkiewiczc47137a2008-02-06 02:57:51 +0100558
Bartlomiej Zolnierkiewicz3a7d2482008-10-10 22:39:21 +0200559 if (stat & ATA_DSC) {
560 if (stat & ATA_ERR) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 /* Error detected */
Borislav Petkov90699ce2008-02-02 19:56:50 +0100562 if (pc->c[0] != TEST_UNIT_READY)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 printk(KERN_ERR "ide-tape: %s: I/O error, ",
564 tape->name);
565 /* Retry operation */
Borislav Petkov6b544fc2009-04-19 07:00:42 +0900566 ide_retry_pc(drive);
Bartlomiej Zolnierkiewicz258ec412008-07-15 21:21:55 +0200567 return ide_stopped;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 }
569 pc->error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 } else {
Bartlomiej Zolnierkiewiczc152cc12009-03-27 12:46:34 +0100571 pc->error = IDE_DRV_ERROR_GENERAL;
Bartlomiej Zolnierkiewicz5e2040fd2009-03-27 12:46:34 +0100572 drive->failed_pc = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 }
Bartlomiej Zolnierkiewiczb14c7212008-10-13 21:39:30 +0200574 drive->pc_callback(drive, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 return ide_stopped;
576}
577
Borislav Petkovcd2abbf2008-07-15 21:22:03 +0200578static void ide_tape_create_rw_cmd(idetape_tape_t *tape,
Borislav Petkov0014c752008-07-23 19:56:00 +0200579 struct ide_atapi_pc *pc, struct request *rq,
580 u8 opcode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581{
Tejun Heo963da552009-04-19 08:46:02 +0900582 unsigned int length = rq->nr_sectors;
Borislav Petkov0014c752008-07-23 19:56:00 +0200583
Bartlomiej Zolnierkiewicz7bf74202008-10-10 22:39:37 +0200584 ide_init_pc(pc);
Borislav Petkov860ff5e2008-02-02 19:56:50 +0100585 put_unaligned(cpu_to_be32(length), (unsigned int *) &pc->c[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 pc->c[1] = 1;
Borislav Petkovd236d742008-04-18 00:46:27 +0200587 pc->buf = NULL;
588 pc->buf_size = length * tape->blk_size;
589 pc->req_xfer = pc->buf_size;
Borislav Petkovf73850a2008-04-27 15:38:33 +0200590 if (pc->req_xfer == tape->buffer_size)
Bartlomiej Zolnierkiewicz5e331092008-07-15 21:21:56 +0200591 pc->flags |= PC_FLAG_DMA_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592
Tejun Heo963da552009-04-19 08:46:02 +0900593 if (opcode == READ_6)
Borislav Petkovcd2abbf2008-07-15 21:22:03 +0200594 pc->c[0] = READ_6;
Tejun Heo963da552009-04-19 08:46:02 +0900595 else if (opcode == WRITE_6) {
Borislav Petkovcd2abbf2008-07-15 21:22:03 +0200596 pc->c[0] = WRITE_6;
597 pc->flags |= PC_FLAG_WRITING;
Borislav Petkovcd2abbf2008-07-15 21:22:03 +0200598 }
Borislav Petkov0014c752008-07-23 19:56:00 +0200599
600 memcpy(rq->cmd, pc->c, 12);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601}
602
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603static ide_startstop_t idetape_do_request(ide_drive_t *drive,
604 struct request *rq, sector_t block)
605{
Bartlomiej Zolnierkiewiczb73c7ee2008-07-23 19:55:52 +0200606 ide_hwif_t *hwif = drive->hwif;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607 idetape_tape_t *tape = drive->driver_data;
Borislav Petkovd236d742008-04-18 00:46:27 +0200608 struct ide_atapi_pc *pc = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 struct request *postponed_rq = tape->postponed_rq;
Bartlomiej Zolnierkiewiczb788ee92009-03-27 12:46:46 +0100610 struct ide_cmd cmd;
Bartlomiej Zolnierkiewicz22c525b2008-01-25 22:17:11 +0100611 u8 stat;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612
Mark de Wevere8e75262009-05-17 17:22:53 +0200613 debug_log(DBG_SENSE, "sector: %llu, nr_sectors: %lu\n",
Tejun Heo963da552009-04-19 08:46:02 +0900614 (unsigned long long)rq->sector, rq->nr_sectors);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615
Borislav Petkov6b544fc2009-04-19 07:00:42 +0900616 if (!(blk_special_request(rq) || blk_sense_request(rq))) {
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100617 /* We do not support buffer cache originated requests. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 printk(KERN_NOTICE "ide-tape: %s: Unsupported request in "
Jens Axboe4aff5e22006-08-10 08:44:47 +0200619 "request queue (%d)\n", drive->name, rq->cmd_type);
Bartlomiej Zolnierkiewicz89f78b322009-03-27 12:46:43 +0100620 if (blk_fs_request(rq) == 0 && rq->errors == 0)
621 rq->errors = -EIO;
Bartlomiej Zolnierkiewicz130e8862009-03-27 12:46:45 +0100622 ide_complete_rq(drive, -EIO, ide_rq_bytes(rq));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 return ide_stopped;
624 }
625
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100626 /* Retry a failed packet command */
Bartlomiej Zolnierkiewicz5e2040fd2009-03-27 12:46:34 +0100627 if (drive->failed_pc && drive->pc->c[0] == REQUEST_SENSE) {
628 pc = drive->failed_pc;
Bartlomiej Zolnierkiewicz28c72142008-07-15 21:21:59 +0200629 goto out;
630 }
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100631
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 if (postponed_rq != NULL)
633 if (rq != postponed_rq) {
634 printk(KERN_ERR "ide-tape: ide-tape.c bug - "
635 "Two DSC requests were queued\n");
Bartlomiej Zolnierkiewicz313afea2009-03-27 12:46:34 +0100636 drive->failed_pc = NULL;
Bartlomiej Zolnierkiewicz6902a532009-03-27 12:46:43 +0100637 rq->errors = 0;
Bartlomiej Zolnierkiewiczf974b192009-03-27 12:46:44 +0100638 ide_complete_rq(drive, 0, blk_rq_bytes(rq));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639 return ide_stopped;
640 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641
642 tape->postponed_rq = NULL;
643
644 /*
645 * If the tape is still busy, postpone our request and service
646 * the other device meanwhile.
647 */
Bartlomiej Zolnierkiewicz374e0422008-07-23 19:55:56 +0200648 stat = hwif->tp_ops->read_status(hwif);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +0200650 if ((drive->dev_flags & IDE_DFLAG_DSC_OVERLAP) == 0 &&
651 (rq->cmd[13] & REQ_IDETAPE_PC2) == 0)
Borislav Petkov626542c2009-06-07 15:37:05 +0200652 drive->atapi_flags |= IDE_AFLAG_IGNORE_DSC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +0200654 if (drive->dev_flags & IDE_DFLAG_POST_RESET) {
Borislav Petkov626542c2009-06-07 15:37:05 +0200655 drive->atapi_flags |= IDE_AFLAG_IGNORE_DSC;
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +0200656 drive->dev_flags &= ~IDE_DFLAG_POST_RESET;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 }
658
Borislav Petkov626542c2009-06-07 15:37:05 +0200659 if (!(drive->atapi_flags & IDE_AFLAG_IGNORE_DSC) &&
660 !(stat & ATA_DSC)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661 if (postponed_rq == NULL) {
662 tape->dsc_polling_start = jiffies;
Borislav Petkov54bb2072008-02-06 02:57:52 +0100663 tape->dsc_poll_freq = tape->best_dsc_rw_freq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664 tape->dsc_timeout = jiffies + IDETAPE_DSC_RW_TIMEOUT;
665 } else if (time_after(jiffies, tape->dsc_timeout)) {
666 printk(KERN_ERR "ide-tape: %s: DSC timeout\n",
667 tape->name);
Borislav Petkov83dd5732008-07-23 19:56:00 +0200668 if (rq->cmd[13] & REQ_IDETAPE_PC2) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669 idetape_media_access_finished(drive);
670 return ide_stopped;
671 } else {
672 return ide_do_reset(drive);
673 }
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100674 } else if (time_after(jiffies,
675 tape->dsc_polling_start +
676 IDETAPE_DSC_MA_THRESHOLD))
Borislav Petkov54bb2072008-02-06 02:57:52 +0100677 tape->dsc_poll_freq = IDETAPE_DSC_MA_SLOW;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 idetape_postpone_request(drive);
679 return ide_stopped;
Borislav Petkov626542c2009-06-07 15:37:05 +0200680 } else
681 drive->atapi_flags &= ~IDE_AFLAG_IGNORE_DSC;
682
Borislav Petkov83dd5732008-07-23 19:56:00 +0200683 if (rq->cmd[13] & REQ_IDETAPE_READ) {
Bartlomiej Zolnierkiewicz2e8a6f82008-10-10 22:39:36 +0200684 pc = &tape->queued_pc;
Borislav Petkov0014c752008-07-23 19:56:00 +0200685 ide_tape_create_rw_cmd(tape, pc, rq, READ_6);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686 goto out;
687 }
Borislav Petkov83dd5732008-07-23 19:56:00 +0200688 if (rq->cmd[13] & REQ_IDETAPE_WRITE) {
Bartlomiej Zolnierkiewicz2e8a6f82008-10-10 22:39:36 +0200689 pc = &tape->queued_pc;
Borislav Petkov0014c752008-07-23 19:56:00 +0200690 ide_tape_create_rw_cmd(tape, pc, rq, WRITE_6);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 goto out;
692 }
Borislav Petkov83dd5732008-07-23 19:56:00 +0200693 if (rq->cmd[13] & REQ_IDETAPE_PC1) {
Tejun Heoc267cc12009-04-19 07:00:42 +0900694 pc = (struct ide_atapi_pc *)rq->special;
Borislav Petkov83dd5732008-07-23 19:56:00 +0200695 rq->cmd[13] &= ~(REQ_IDETAPE_PC1);
696 rq->cmd[13] |= REQ_IDETAPE_PC2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 goto out;
698 }
Borislav Petkov83dd5732008-07-23 19:56:00 +0200699 if (rq->cmd[13] & REQ_IDETAPE_PC2) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 idetape_media_access_finished(drive);
701 return ide_stopped;
702 }
703 BUG();
Bartlomiej Zolnierkiewicz28c72142008-07-15 21:21:59 +0200704
Borislav Petkovf2e3ab52008-07-23 19:56:01 +0200705out:
Borislav Petkov6b544fc2009-04-19 07:00:42 +0900706 /* prepare sense request for this command */
707 ide_prep_sense(drive, rq);
708
Bartlomiej Zolnierkiewiczb788ee92009-03-27 12:46:46 +0100709 memset(&cmd, 0, sizeof(cmd));
710
711 if (rq_data_dir(rq))
712 cmd.tf_flags |= IDE_TFLAG_WRITE;
713
714 cmd.rq = rq;
715
Tejun Heo5c4be572009-04-19 07:00:42 +0900716 ide_init_sg_cmd(&cmd, pc->req_xfer);
717 ide_map_sg(drive, &cmd);
718
Bartlomiej Zolnierkiewiczb788ee92009-03-27 12:46:46 +0100719 return ide_tape_issue_pc(drive, &cmd, pc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720}
721
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100723 * Write a filemark if write_filemark=1. Flush the device buffers without
724 * writing a filemark otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100726static void idetape_create_write_filemark_cmd(ide_drive_t *drive,
Borislav Petkovd236d742008-04-18 00:46:27 +0200727 struct ide_atapi_pc *pc, int write_filemark)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728{
Bartlomiej Zolnierkiewicz7bf74202008-10-10 22:39:37 +0200729 ide_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +0100730 pc->c[0] = WRITE_FILEMARKS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 pc->c[4] = write_filemark;
Borislav Petkov346331f2008-04-18 00:46:26 +0200732 pc->flags |= PC_FLAG_WAIT_FOR_DSC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733}
734
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735static int idetape_wait_ready(ide_drive_t *drive, unsigned long timeout)
736{
737 idetape_tape_t *tape = drive->driver_data;
Bartlomiej Zolnierkiewicz2ac07d92008-10-10 22:39:38 +0200738 struct gendisk *disk = tape->disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 int load_attempted = 0;
740
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100741 /* Wait for the tape to become ready */
Borislav Petkov49d80782009-06-07 15:37:06 +0200742 set_bit(ilog2(IDE_AFLAG_MEDIUM_PRESENT), &drive->atapi_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743 timeout += jiffies;
744 while (time_before(jiffies, timeout)) {
Bartlomiej Zolnierkiewiczde699ad2008-10-10 22:39:39 +0200745 if (ide_do_test_unit_ready(drive, disk) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746 return 0;
747 if ((tape->sense_key == 2 && tape->asc == 4 && tape->ascq == 2)
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100748 || (tape->asc == 0x3A)) {
749 /* no media */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750 if (load_attempted)
751 return -ENOMEDIUM;
Bartlomiej Zolnierkiewicz0c8a6c72008-10-10 22:39:39 +0200752 ide_do_start_stop(drive, disk, IDETAPE_LU_LOAD_MASK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753 load_attempted = 1;
754 /* not about to be ready */
755 } else if (!(tape->sense_key == 2 && tape->asc == 4 &&
756 (tape->ascq == 1 || tape->ascq == 8)))
757 return -EIO;
Nishanth Aravamudan80ce45f2005-09-10 00:27:08 -0700758 msleep(100);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 }
760 return -EIO;
761}
762
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100763static int idetape_flush_tape_buffers(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764{
Bartlomiej Zolnierkiewicz2ac07d92008-10-10 22:39:38 +0200765 struct ide_tape_obj *tape = drive->driver_data;
Borislav Petkovd236d742008-04-18 00:46:27 +0200766 struct ide_atapi_pc pc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767 int rc;
768
769 idetape_create_write_filemark_cmd(drive, &pc, 0);
Bartlomiej Zolnierkiewicz2ac07d92008-10-10 22:39:38 +0200770 rc = ide_queue_pc_tail(drive, tape->disk, &pc);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100771 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772 return rc;
773 idetape_wait_ready(drive, 60 * 5 * HZ);
774 return 0;
775}
776
Borislav Petkovd236d742008-04-18 00:46:27 +0200777static void idetape_create_read_position_cmd(struct ide_atapi_pc *pc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778{
Bartlomiej Zolnierkiewicz7bf74202008-10-10 22:39:37 +0200779 ide_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +0100780 pc->c[0] = READ_POSITION;
Borislav Petkovd236d742008-04-18 00:46:27 +0200781 pc->req_xfer = 20;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782}
783
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100784static int idetape_read_position(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785{
786 idetape_tape_t *tape = drive->driver_data;
Borislav Petkovd236d742008-04-18 00:46:27 +0200787 struct ide_atapi_pc pc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788 int position;
789
Borislav Petkov8004a8c2008-02-06 02:57:51 +0100790 debug_log(DBG_PROCS, "Enter %s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791
792 idetape_create_read_position_cmd(&pc);
Bartlomiej Zolnierkiewicz2ac07d92008-10-10 22:39:38 +0200793 if (ide_queue_pc_tail(drive, tape->disk, &pc))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794 return -1;
Borislav Petkov54bb2072008-02-06 02:57:52 +0100795 position = tape->first_frame;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796 return position;
797}
798
Borislav Petkovd236d742008-04-18 00:46:27 +0200799static void idetape_create_locate_cmd(ide_drive_t *drive,
800 struct ide_atapi_pc *pc,
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100801 unsigned int block, u8 partition, int skip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802{
Bartlomiej Zolnierkiewicz7bf74202008-10-10 22:39:37 +0200803 ide_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +0100804 pc->c[0] = POSITION_TO_ELEMENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805 pc->c[1] = 2;
Borislav Petkov860ff5e2008-02-02 19:56:50 +0100806 put_unaligned(cpu_to_be32(block), (unsigned int *) &pc->c[3]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807 pc->c[8] = partition;
Borislav Petkov346331f2008-04-18 00:46:26 +0200808 pc->flags |= PC_FLAG_WAIT_FOR_DSC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809}
810
Borislav Petkovec0fdb02008-04-27 15:38:34 +0200811static void __ide_tape_discard_merge_buffer(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812{
813 idetape_tape_t *tape = drive->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814
Borislav Petkov54abf372008-02-06 02:57:52 +0100815 if (tape->chrdev_dir != IDETAPE_DIR_READ)
Borislav Petkov97986302008-04-27 15:38:34 +0200816 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817
Borislav Petkov49d80782009-06-07 15:37:06 +0200818 clear_bit(ilog2(IDE_AFLAG_FILEMARK), &drive->atapi_flags);
Tejun Heo963da552009-04-19 08:46:02 +0900819 tape->valid = 0;
820 if (tape->buf != NULL) {
821 kfree(tape->buf);
822 tape->buf = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823 }
824
Borislav Petkov54abf372008-02-06 02:57:52 +0100825 tape->chrdev_dir = IDETAPE_DIR_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826}
827
828/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100829 * Position the tape to the requested block using the LOCATE packet command.
830 * A READ POSITION command is then issued to check where we are positioned. Like
831 * all higher level operations, we queue the commands at the tail of the request
832 * queue and wait for their completion.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833 */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100834static int idetape_position_tape(ide_drive_t *drive, unsigned int block,
835 u8 partition, int skip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836{
837 idetape_tape_t *tape = drive->driver_data;
Bartlomiej Zolnierkiewicz2ac07d92008-10-10 22:39:38 +0200838 struct gendisk *disk = tape->disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839 int retval;
Borislav Petkovd236d742008-04-18 00:46:27 +0200840 struct ide_atapi_pc pc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841
Borislav Petkov54abf372008-02-06 02:57:52 +0100842 if (tape->chrdev_dir == IDETAPE_DIR_READ)
Borislav Petkovec0fdb02008-04-27 15:38:34 +0200843 __ide_tape_discard_merge_buffer(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844 idetape_wait_ready(drive, 60 * 5 * HZ);
845 idetape_create_locate_cmd(drive, &pc, block, partition, skip);
Bartlomiej Zolnierkiewicz2ac07d92008-10-10 22:39:38 +0200846 retval = ide_queue_pc_tail(drive, disk, &pc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847 if (retval)
848 return (retval);
849
850 idetape_create_read_position_cmd(&pc);
Bartlomiej Zolnierkiewicz2ac07d92008-10-10 22:39:38 +0200851 return ide_queue_pc_tail(drive, disk, &pc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852}
853
Borislav Petkovec0fdb02008-04-27 15:38:34 +0200854static void ide_tape_discard_merge_buffer(ide_drive_t *drive,
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100855 int restore_position)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856{
857 idetape_tape_t *tape = drive->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858 int seek, position;
859
Borislav Petkovec0fdb02008-04-27 15:38:34 +0200860 __ide_tape_discard_merge_buffer(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861 if (restore_position) {
862 position = idetape_read_position(drive);
Borislav Petkov97986302008-04-27 15:38:34 +0200863 seek = position > 0 ? position : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864 if (idetape_position_tape(drive, seek, 0, 0)) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100865 printk(KERN_INFO "ide-tape: %s: position_tape failed in"
Borislav Petkovec0fdb02008-04-27 15:38:34 +0200866 " %s\n", tape->name, __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867 return;
868 }
869 }
870}
871
872/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100873 * Generate a read/write request for the block device interface and wait for it
874 * to be serviced.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875 */
Tejun Heo6bb11dd2009-04-19 08:46:03 +0900876static int idetape_queue_rw_tail(ide_drive_t *drive, int cmd, int size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877{
878 idetape_tape_t *tape = drive->driver_data;
FUJITA Tomonori64ea1b42008-07-15 21:21:43 +0200879 struct request *rq;
Tejun Heo21d9c5d2009-04-19 08:46:02 +0900880 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881
Borislav Petkov8004a8c2008-02-06 02:57:51 +0100882 debug_log(DBG_SENSE, "%s: cmd=%d\n", __func__, cmd);
Tejun Heo21d9c5d2009-04-19 08:46:02 +0900883 BUG_ON(cmd != REQ_IDETAPE_READ && cmd != REQ_IDETAPE_WRITE);
Tejun Heo6bb11dd2009-04-19 08:46:03 +0900884 BUG_ON(size < 0 || size % tape->blk_size);
Borislav Petkov8004a8c2008-02-06 02:57:51 +0100885
FUJITA Tomonori64ea1b42008-07-15 21:21:43 +0200886 rq = blk_get_request(drive->queue, READ, __GFP_WAIT);
887 rq->cmd_type = REQ_TYPE_SPECIAL;
Borislav Petkov83dd5732008-07-23 19:56:00 +0200888 rq->cmd[13] = cmd;
FUJITA Tomonori64ea1b42008-07-15 21:21:43 +0200889 rq->rq_disk = tape->disk;
FUJITA Tomonori64ea1b42008-07-15 21:21:43 +0200890 rq->sector = tape->first_frame;
Tejun Heo21d9c5d2009-04-19 08:46:02 +0900891
892 if (size) {
Tejun Heo963da552009-04-19 08:46:02 +0900893 ret = blk_rq_map_kern(drive->queue, rq, tape->buf, size,
Tejun Heo21d9c5d2009-04-19 08:46:02 +0900894 __GFP_WAIT);
895 if (ret)
896 goto out_put;
897 }
898
FUJITA Tomonori64ea1b42008-07-15 21:21:43 +0200899 blk_execute_rq(drive->queue, tape->disk, rq, 0);
900
Tejun Heo963da552009-04-19 08:46:02 +0900901 /* calculate the number of transferred bytes and update buffer state */
Tejun Heo21d9c5d2009-04-19 08:46:02 +0900902 size -= rq->data_len;
Tejun Heo963da552009-04-19 08:46:02 +0900903 tape->cur = tape->buf;
Tejun Heo21d9c5d2009-04-19 08:46:02 +0900904 if (cmd == REQ_IDETAPE_READ)
Tejun Heo963da552009-04-19 08:46:02 +0900905 tape->valid = size;
906 else
907 tape->valid = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908
Tejun Heo21d9c5d2009-04-19 08:46:02 +0900909 ret = size;
910 if (rq->errors == IDE_DRV_ERROR_GENERAL)
911 ret = -EIO;
Tejun Heo21d9c5d2009-04-19 08:46:02 +0900912out_put:
913 blk_put_request(rq);
FUJITA Tomonori64ea1b42008-07-15 21:21:43 +0200914 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915}
916
Borislav Petkovd236d742008-04-18 00:46:27 +0200917static void idetape_create_inquiry_cmd(struct ide_atapi_pc *pc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918{
Bartlomiej Zolnierkiewicz7bf74202008-10-10 22:39:37 +0200919 ide_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +0100920 pc->c[0] = INQUIRY;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100921 pc->c[4] = 254;
Borislav Petkovd236d742008-04-18 00:46:27 +0200922 pc->req_xfer = 254;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923}
924
Borislav Petkovd236d742008-04-18 00:46:27 +0200925static void idetape_create_rewind_cmd(ide_drive_t *drive,
926 struct ide_atapi_pc *pc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927{
Bartlomiej Zolnierkiewicz7bf74202008-10-10 22:39:37 +0200928 ide_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +0100929 pc->c[0] = REZERO_UNIT;
Borislav Petkov346331f2008-04-18 00:46:26 +0200930 pc->flags |= PC_FLAG_WAIT_FOR_DSC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931}
932
Borislav Petkovd236d742008-04-18 00:46:27 +0200933static void idetape_create_erase_cmd(struct ide_atapi_pc *pc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934{
Bartlomiej Zolnierkiewicz7bf74202008-10-10 22:39:37 +0200935 ide_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +0100936 pc->c[0] = ERASE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937 pc->c[1] = 1;
Borislav Petkov346331f2008-04-18 00:46:26 +0200938 pc->flags |= PC_FLAG_WAIT_FOR_DSC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939}
940
Borislav Petkovd236d742008-04-18 00:46:27 +0200941static void idetape_create_space_cmd(struct ide_atapi_pc *pc, int count, u8 cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942{
Bartlomiej Zolnierkiewicz7bf74202008-10-10 22:39:37 +0200943 ide_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +0100944 pc->c[0] = SPACE;
Borislav Petkov860ff5e2008-02-02 19:56:50 +0100945 put_unaligned(cpu_to_be32(count), (unsigned int *) &pc->c[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946 pc->c[1] = cmd;
Borislav Petkov346331f2008-04-18 00:46:26 +0200947 pc->flags |= PC_FLAG_WAIT_FOR_DSC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948}
949
Borislav Petkovd9df9372008-04-27 15:38:34 +0200950static void ide_tape_flush_merge_buffer(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951{
952 idetape_tape_t *tape = drive->driver_data;
Borislav Petkov55a5d292008-02-02 19:56:49 +0100953
Borislav Petkov54abf372008-02-06 02:57:52 +0100954 if (tape->chrdev_dir != IDETAPE_DIR_WRITE) {
Borislav Petkov077e3bd2008-04-27 15:38:34 +0200955 printk(KERN_ERR "ide-tape: bug: Trying to empty merge buffer"
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100956 " but we are not writing.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957 return;
958 }
Tejun Heo963da552009-04-19 08:46:02 +0900959 if (tape->buf) {
Tejun Heo6bb11dd2009-04-19 08:46:03 +0900960 size_t aligned = roundup(tape->valid, tape->blk_size);
961
962 memset(tape->cur, 0, aligned - tape->valid);
Tejun Heo07bd9682009-04-19 08:46:03 +0900963 idetape_queue_rw_tail(drive, REQ_IDETAPE_WRITE, aligned);
Tejun Heo963da552009-04-19 08:46:02 +0900964 kfree(tape->buf);
965 tape->buf = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966 }
Borislav Petkov54abf372008-02-06 02:57:52 +0100967 tape->chrdev_dir = IDETAPE_DIR_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968}
969
Tejun Heo88f1b942009-04-19 08:46:02 +0900970static int idetape_init_rw(ide_drive_t *drive, int dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971{
972 idetape_tape_t *tape = drive->driver_data;
Tejun Heo88f1b942009-04-19 08:46:02 +0900973 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974
Tejun Heo88f1b942009-04-19 08:46:02 +0900975 BUG_ON(dir != IDETAPE_DIR_READ && dir != IDETAPE_DIR_WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976
Tejun Heo88f1b942009-04-19 08:46:02 +0900977 if (tape->chrdev_dir == dir)
978 return 0;
979
980 if (tape->chrdev_dir == IDETAPE_DIR_READ)
981 ide_tape_discard_merge_buffer(drive, 1);
982 else if (tape->chrdev_dir == IDETAPE_DIR_WRITE) {
983 ide_tape_flush_merge_buffer(drive);
984 idetape_flush_tape_buffers(drive);
985 }
986
987 if (tape->buf || tape->valid) {
988 printk(KERN_ERR "ide-tape: valid should be 0 now\n");
989 tape->valid = 0;
990 }
991
992 tape->buf = kmalloc(tape->buffer_size, GFP_KERNEL);
993 if (!tape->buf)
994 return -ENOMEM;
995 tape->chrdev_dir = dir;
996 tape->cur = tape->buf;
997
998 /*
999 * Issue a 0 rw command to ensure that DSC handshake is
1000 * switched from completion mode to buffer available mode. No
1001 * point in issuing this if DSC overlap isn't supported, some
1002 * drives (Seagate STT3401A) will return an error.
1003 */
1004 if (drive->dev_flags & IDE_DFLAG_DSC_OVERLAP) {
1005 int cmd = dir == IDETAPE_DIR_READ ? REQ_IDETAPE_READ
1006 : REQ_IDETAPE_WRITE;
1007
1008 rc = idetape_queue_rw_tail(drive, cmd, 0);
1009 if (rc < 0) {
1010 kfree(tape->buf);
1011 tape->buf = NULL;
1012 tape->chrdev_dir = IDETAPE_DIR_NONE;
1013 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014 }
1015 }
Borislav Petkov5e69bd92008-04-27 15:38:25 +02001016
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017 return 0;
1018}
1019
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001020static void idetape_pad_zeros(ide_drive_t *drive, int bcount)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021{
1022 idetape_tape_t *tape = drive->driver_data;
Tejun Heo963da552009-04-19 08:46:02 +09001023
1024 memset(tape->buf, 0, tape->buffer_size);
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001025
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026 while (bcount) {
Tejun Heo963da552009-04-19 08:46:02 +09001027 unsigned int count = min(tape->buffer_size, bcount);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028
Tejun Heo6bb11dd2009-04-19 08:46:03 +09001029 idetape_queue_rw_tail(drive, REQ_IDETAPE_WRITE, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030 bcount -= count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031 }
1032}
1033
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001035 * Rewinds the tape to the Beginning Of the current Partition (BOP). We
1036 * currently support only one partition.
1037 */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001038static int idetape_rewind_tape(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039{
Bartlomiej Zolnierkiewicz2ac07d92008-10-10 22:39:38 +02001040 struct ide_tape_obj *tape = drive->driver_data;
1041 struct gendisk *disk = tape->disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042 int retval;
Borislav Petkovd236d742008-04-18 00:46:27 +02001043 struct ide_atapi_pc pc;
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001044
1045 debug_log(DBG_SENSE, "Enter %s\n", __func__);
1046
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047 idetape_create_rewind_cmd(drive, &pc);
Bartlomiej Zolnierkiewicz2ac07d92008-10-10 22:39:38 +02001048 retval = ide_queue_pc_tail(drive, disk, &pc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049 if (retval)
1050 return retval;
1051
1052 idetape_create_read_position_cmd(&pc);
Bartlomiej Zolnierkiewicz2ac07d92008-10-10 22:39:38 +02001053 retval = ide_queue_pc_tail(drive, disk, &pc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054 if (retval)
1055 return retval;
1056 return 0;
1057}
1058
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001059/* mtio.h compatible commands should be issued to the chrdev interface. */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001060static int idetape_blkdev_ioctl(ide_drive_t *drive, unsigned int cmd,
1061 unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062{
1063 idetape_tape_t *tape = drive->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064 void __user *argp = (void __user *)arg;
1065
Borislav Petkovd59823f2008-02-02 19:56:51 +01001066 struct idetape_config {
1067 int dsc_rw_frequency;
1068 int dsc_media_access_frequency;
1069 int nr_stages;
1070 } config;
1071
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001072 debug_log(DBG_PROCS, "Enter %s\n", __func__);
1073
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074 switch (cmd) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001075 case 0x0340:
1076 if (copy_from_user(&config, argp, sizeof(config)))
1077 return -EFAULT;
1078 tape->best_dsc_rw_freq = config.dsc_rw_frequency;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001079 break;
1080 case 0x0350:
1081 config.dsc_rw_frequency = (int) tape->best_dsc_rw_freq;
Borislav Petkov83042b22008-04-27 15:38:27 +02001082 config.nr_stages = 1;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001083 if (copy_to_user(argp, &config, sizeof(config)))
1084 return -EFAULT;
1085 break;
1086 default:
1087 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088 }
1089 return 0;
1090}
1091
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001092static int idetape_space_over_filemarks(ide_drive_t *drive, short mt_op,
1093 int mt_count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094{
1095 idetape_tape_t *tape = drive->driver_data;
Bartlomiej Zolnierkiewicz2ac07d92008-10-10 22:39:38 +02001096 struct gendisk *disk = tape->disk;
Borislav Petkovd236d742008-04-18 00:46:27 +02001097 struct ide_atapi_pc pc;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001098 int retval, count = 0;
Borislav Petkovb6422012008-02-02 19:56:49 +01001099 int sprev = !!(tape->caps[4] & 0x20);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100
1101 if (mt_count == 0)
1102 return 0;
1103 if (MTBSF == mt_op || MTBSFM == mt_op) {
Borislav Petkovb6422012008-02-02 19:56:49 +01001104 if (!sprev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105 return -EIO;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001106 mt_count = -mt_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107 }
1108
Borislav Petkov54abf372008-02-06 02:57:52 +01001109 if (tape->chrdev_dir == IDETAPE_DIR_READ) {
Tejun Heo963da552009-04-19 08:46:02 +09001110 tape->valid = 0;
Borislav Petkov49d80782009-06-07 15:37:06 +02001111 if (test_and_clear_bit(ilog2(IDE_AFLAG_FILEMARK),
1112 &drive->atapi_flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113 ++count;
Borislav Petkovec0fdb02008-04-27 15:38:34 +02001114 ide_tape_discard_merge_buffer(drive, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115 }
1116
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117 switch (mt_op) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001118 case MTFSF:
1119 case MTBSF:
1120 idetape_create_space_cmd(&pc, mt_count - count,
1121 IDETAPE_SPACE_OVER_FILEMARK);
Bartlomiej Zolnierkiewicz2ac07d92008-10-10 22:39:38 +02001122 return ide_queue_pc_tail(drive, disk, &pc);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001123 case MTFSFM:
1124 case MTBSFM:
1125 if (!sprev)
1126 return -EIO;
1127 retval = idetape_space_over_filemarks(drive, MTFSF,
1128 mt_count - count);
1129 if (retval)
1130 return retval;
1131 count = (MTBSFM == mt_op ? 1 : -1);
1132 return idetape_space_over_filemarks(drive, MTFSF, count);
1133 default:
1134 printk(KERN_ERR "ide-tape: MTIO operation %d not supported\n",
1135 mt_op);
1136 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137 }
1138}
1139
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001141 * Our character device read / write functions.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001143 * The tape is optimized to maximize throughput when it is transferring an
1144 * integral number of the "continuous transfer limit", which is a parameter of
1145 * the specific tape (26kB on my particular tape, 32kB for Onstream).
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001147 * As of version 1.3 of the driver, the character device provides an abstract
1148 * continuous view of the media - any mix of block sizes (even 1 byte) on the
1149 * same backup/restore procedure is supported. The driver will internally
1150 * convert the requests to the recommended transfer unit, so that an unmatch
1151 * between the user's block size to the recommended size will only result in a
1152 * (slightly) increased driver overhead, but will no longer hit performance.
1153 * This is not applicable to Onstream.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154 */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001155static ssize_t idetape_chrdev_read(struct file *file, char __user *buf,
1156 size_t count, loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157{
Borislav Petkov5aeddf92008-10-13 21:39:34 +02001158 struct ide_tape_obj *tape = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159 ide_drive_t *drive = tape->drive;
Tejun Heo07bd9682009-04-19 08:46:03 +09001160 size_t done = 0;
Daniel Walkerdcd96372006-06-25 05:47:37 -07001161 ssize_t ret = 0;
Tejun Heo07bd9682009-04-19 08:46:03 +09001162 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001164 debug_log(DBG_CHRDEV, "Enter %s, count %Zd\n", __func__, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165
Borislav Petkov54abf372008-02-06 02:57:52 +01001166 if (tape->chrdev_dir != IDETAPE_DIR_READ) {
Borislav Petkov49d80782009-06-07 15:37:06 +02001167 if (test_bit(ilog2(IDE_AFLAG_DETECT_BS), &drive->atapi_flags))
Borislav Petkov54bb2072008-02-06 02:57:52 +01001168 if (count > tape->blk_size &&
1169 (count % tape->blk_size) == 0)
1170 tape->user_bs_factor = count / tape->blk_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171 }
Tejun Heo07bd9682009-04-19 08:46:03 +09001172
Tejun Heo88f1b942009-04-19 08:46:02 +09001173 rc = idetape_init_rw(drive, IDETAPE_DIR_READ);
Borislav Petkov8d06bfa2008-02-06 02:57:53 +01001174 if (rc < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175 return rc;
Tejun Heo07bd9682009-04-19 08:46:03 +09001176
1177 while (done < count) {
1178 size_t todo;
1179
1180 /* refill if staging buffer is empty */
1181 if (!tape->valid) {
1182 /* If we are at a filemark, nothing more to read */
Borislav Petkov49d80782009-06-07 15:37:06 +02001183 if (test_bit(ilog2(IDE_AFLAG_FILEMARK),
1184 &drive->atapi_flags))
Tejun Heo07bd9682009-04-19 08:46:03 +09001185 break;
1186 /* read */
1187 if (idetape_queue_rw_tail(drive, REQ_IDETAPE_READ,
1188 tape->buffer_size) <= 0)
1189 break;
1190 }
1191
1192 /* copy out */
1193 todo = min_t(size_t, count - done, tape->valid);
1194 if (copy_to_user(buf + done, tape->cur, todo))
Daniel Walkerdcd96372006-06-25 05:47:37 -07001195 ret = -EFAULT;
Tejun Heo07bd9682009-04-19 08:46:03 +09001196
1197 tape->cur += todo;
1198 tape->valid -= todo;
1199 done += todo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200 }
Tejun Heo07bd9682009-04-19 08:46:03 +09001201
Borislav Petkov49d80782009-06-07 15:37:06 +02001202 if (!done && test_bit(ilog2(IDE_AFLAG_FILEMARK), &drive->atapi_flags)) {
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001203 debug_log(DBG_SENSE, "%s: spacing over filemark\n", tape->name);
1204
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205 idetape_space_over_filemarks(drive, MTFSF, 1);
1206 return 0;
1207 }
Daniel Walkerdcd96372006-06-25 05:47:37 -07001208
Tejun Heo07bd9682009-04-19 08:46:03 +09001209 return ret ? ret : done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210}
1211
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001212static ssize_t idetape_chrdev_write(struct file *file, const char __user *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213 size_t count, loff_t *ppos)
1214{
Borislav Petkov5aeddf92008-10-13 21:39:34 +02001215 struct ide_tape_obj *tape = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216 ide_drive_t *drive = tape->drive;
Tejun Heo07bd9682009-04-19 08:46:03 +09001217 size_t done = 0;
Daniel Walkerdcd96372006-06-25 05:47:37 -07001218 ssize_t ret = 0;
Tejun Heo88f1b942009-04-19 08:46:02 +09001219 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220
1221 /* The drive is write protected. */
1222 if (tape->write_prot)
1223 return -EACCES;
1224
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001225 debug_log(DBG_CHRDEV, "Enter %s, count %Zd\n", __func__, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226
1227 /* Initialize write operation */
Tejun Heo88f1b942009-04-19 08:46:02 +09001228 rc = idetape_init_rw(drive, IDETAPE_DIR_WRITE);
1229 if (rc < 0)
1230 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231
Tejun Heo07bd9682009-04-19 08:46:03 +09001232 while (done < count) {
1233 size_t todo;
1234
1235 /* flush if staging buffer is full */
1236 if (tape->valid == tape->buffer_size &&
1237 idetape_queue_rw_tail(drive, REQ_IDETAPE_WRITE,
1238 tape->buffer_size) <= 0)
1239 return rc;
1240
1241 /* copy in */
1242 todo = min_t(size_t, count - done,
1243 tape->buffer_size - tape->valid);
1244 if (copy_from_user(tape->cur, buf + done, todo))
Daniel Walkerdcd96372006-06-25 05:47:37 -07001245 ret = -EFAULT;
Tejun Heo07bd9682009-04-19 08:46:03 +09001246
1247 tape->cur += todo;
1248 tape->valid += todo;
1249 done += todo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250 }
Tejun Heo07bd9682009-04-19 08:46:03 +09001251
1252 return ret ? ret : done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253}
1254
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001255static int idetape_write_filemark(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256{
Bartlomiej Zolnierkiewicz2ac07d92008-10-10 22:39:38 +02001257 struct ide_tape_obj *tape = drive->driver_data;
Borislav Petkovd236d742008-04-18 00:46:27 +02001258 struct ide_atapi_pc pc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259
1260 /* Write a filemark */
1261 idetape_create_write_filemark_cmd(drive, &pc, 1);
Bartlomiej Zolnierkiewicz2ac07d92008-10-10 22:39:38 +02001262 if (ide_queue_pc_tail(drive, tape->disk, &pc)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263 printk(KERN_ERR "ide-tape: Couldn't write a filemark\n");
1264 return -EIO;
1265 }
1266 return 0;
1267}
1268
1269/*
Borislav Petkovd99c9da2008-02-02 19:56:51 +01001270 * Called from idetape_chrdev_ioctl when the general mtio MTIOCTOP ioctl is
1271 * requested.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272 *
Borislav Petkovd99c9da2008-02-02 19:56:51 +01001273 * Note: MTBSF and MTBSFM are not supported when the tape doesn't support
1274 * spacing over filemarks in the reverse direction. In this case, MTFSFM is also
Borislav Petkov5bd50dc2008-04-27 15:38:28 +02001275 * usually not supported.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276 *
Borislav Petkovd99c9da2008-02-02 19:56:51 +01001277 * The following commands are currently not supported:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278 *
Borislav Petkovd99c9da2008-02-02 19:56:51 +01001279 * MTFSS, MTBSS, MTWSM, MTSETDENSITY, MTSETDRVBUFFER, MT_ST_BOOLEANS,
1280 * MT_ST_WRITE_THRESHOLD.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281 */
Borislav Petkovd99c9da2008-02-02 19:56:51 +01001282static int idetape_mtioctop(ide_drive_t *drive, short mt_op, int mt_count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283{
1284 idetape_tape_t *tape = drive->driver_data;
Bartlomiej Zolnierkiewicz2ac07d92008-10-10 22:39:38 +02001285 struct gendisk *disk = tape->disk;
Borislav Petkovd236d742008-04-18 00:46:27 +02001286 struct ide_atapi_pc pc;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001287 int i, retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001289 debug_log(DBG_ERR, "Handling MTIOCTOP ioctl: mt_op=%d, mt_count=%d\n",
1290 mt_op, mt_count);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001291
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292 switch (mt_op) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001293 case MTFSF:
1294 case MTFSFM:
1295 case MTBSF:
1296 case MTBSFM:
1297 if (!mt_count)
1298 return 0;
1299 return idetape_space_over_filemarks(drive, mt_op, mt_count);
1300 default:
1301 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302 }
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001303
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304 switch (mt_op) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001305 case MTWEOF:
1306 if (tape->write_prot)
1307 return -EACCES;
Borislav Petkovec0fdb02008-04-27 15:38:34 +02001308 ide_tape_discard_merge_buffer(drive, 1);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001309 for (i = 0; i < mt_count; i++) {
1310 retval = idetape_write_filemark(drive);
1311 if (retval)
1312 return retval;
1313 }
1314 return 0;
1315 case MTREW:
Borislav Petkovec0fdb02008-04-27 15:38:34 +02001316 ide_tape_discard_merge_buffer(drive, 0);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001317 if (idetape_rewind_tape(drive))
1318 return -EIO;
1319 return 0;
1320 case MTLOAD:
Borislav Petkovec0fdb02008-04-27 15:38:34 +02001321 ide_tape_discard_merge_buffer(drive, 0);
Bartlomiej Zolnierkiewicz0c8a6c72008-10-10 22:39:39 +02001322 return ide_do_start_stop(drive, disk, IDETAPE_LU_LOAD_MASK);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001323 case MTUNLOAD:
1324 case MTOFFL:
1325 /*
1326 * If door is locked, attempt to unlock before
1327 * attempting to eject.
1328 */
1329 if (tape->door_locked) {
Bartlomiej Zolnierkiewicz05780422008-10-10 22:39:38 +02001330 if (!ide_set_media_lock(drive, disk, 0))
Bartlomiej Zolnierkiewicz385a4b82008-10-10 22:39:37 +02001331 tape->door_locked = DOOR_UNLOCKED;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001332 }
Borislav Petkovec0fdb02008-04-27 15:38:34 +02001333 ide_tape_discard_merge_buffer(drive, 0);
Bartlomiej Zolnierkiewicz0c8a6c72008-10-10 22:39:39 +02001334 retval = ide_do_start_stop(drive, disk, !IDETAPE_LU_LOAD_MASK);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001335 if (!retval)
Borislav Petkov49d80782009-06-07 15:37:06 +02001336 clear_bit(ilog2(IDE_AFLAG_MEDIUM_PRESENT),
1337 &drive->atapi_flags);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001338 return retval;
1339 case MTNOP:
Borislav Petkovec0fdb02008-04-27 15:38:34 +02001340 ide_tape_discard_merge_buffer(drive, 0);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001341 return idetape_flush_tape_buffers(drive);
1342 case MTRETEN:
Borislav Petkovec0fdb02008-04-27 15:38:34 +02001343 ide_tape_discard_merge_buffer(drive, 0);
Bartlomiej Zolnierkiewicz0c8a6c72008-10-10 22:39:39 +02001344 return ide_do_start_stop(drive, disk,
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001345 IDETAPE_LU_RETENSION_MASK | IDETAPE_LU_LOAD_MASK);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001346 case MTEOM:
1347 idetape_create_space_cmd(&pc, 0, IDETAPE_SPACE_TO_EOD);
Bartlomiej Zolnierkiewicz2ac07d92008-10-10 22:39:38 +02001348 return ide_queue_pc_tail(drive, disk, &pc);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001349 case MTERASE:
1350 (void)idetape_rewind_tape(drive);
1351 idetape_create_erase_cmd(&pc);
Bartlomiej Zolnierkiewicz2ac07d92008-10-10 22:39:38 +02001352 return ide_queue_pc_tail(drive, disk, &pc);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001353 case MTSETBLK:
1354 if (mt_count) {
1355 if (mt_count < tape->blk_size ||
1356 mt_count % tape->blk_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357 return -EIO;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001358 tape->user_bs_factor = mt_count / tape->blk_size;
Borislav Petkov49d80782009-06-07 15:37:06 +02001359 clear_bit(ilog2(IDE_AFLAG_DETECT_BS),
1360 &drive->atapi_flags);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001361 } else
Borislav Petkov49d80782009-06-07 15:37:06 +02001362 set_bit(ilog2(IDE_AFLAG_DETECT_BS),
1363 &drive->atapi_flags);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001364 return 0;
1365 case MTSEEK:
Borislav Petkovec0fdb02008-04-27 15:38:34 +02001366 ide_tape_discard_merge_buffer(drive, 0);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001367 return idetape_position_tape(drive,
1368 mt_count * tape->user_bs_factor, tape->partition, 0);
1369 case MTSETPART:
Borislav Petkovec0fdb02008-04-27 15:38:34 +02001370 ide_tape_discard_merge_buffer(drive, 0);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001371 return idetape_position_tape(drive, 0, mt_count, 0);
1372 case MTFSR:
1373 case MTBSR:
1374 case MTLOCK:
Bartlomiej Zolnierkiewicz05780422008-10-10 22:39:38 +02001375 retval = ide_set_media_lock(drive, disk, 1);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001376 if (retval)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377 return retval;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001378 tape->door_locked = DOOR_EXPLICITLY_LOCKED;
1379 return 0;
1380 case MTUNLOCK:
Bartlomiej Zolnierkiewicz05780422008-10-10 22:39:38 +02001381 retval = ide_set_media_lock(drive, disk, 0);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001382 if (retval)
1383 return retval;
1384 tape->door_locked = DOOR_UNLOCKED;
1385 return 0;
1386 default:
1387 printk(KERN_ERR "ide-tape: MTIO operation %d not supported\n",
1388 mt_op);
1389 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390 }
1391}
1392
1393/*
Borislav Petkovd99c9da2008-02-02 19:56:51 +01001394 * Our character device ioctls. General mtio.h magnetic io commands are
1395 * supported here, and not in the corresponding block interface. Our own
1396 * ide-tape ioctls are supported on both interfaces.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397 */
Borislav Petkovd99c9da2008-02-02 19:56:51 +01001398static int idetape_chrdev_ioctl(struct inode *inode, struct file *file,
1399 unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400{
Borislav Petkov5aeddf92008-10-13 21:39:34 +02001401 struct ide_tape_obj *tape = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402 ide_drive_t *drive = tape->drive;
1403 struct mtop mtop;
1404 struct mtget mtget;
1405 struct mtpos mtpos;
Borislav Petkov54bb2072008-02-06 02:57:52 +01001406 int block_offset = 0, position = tape->first_frame;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407 void __user *argp = (void __user *)arg;
1408
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001409 debug_log(DBG_CHRDEV, "Enter %s, cmd=%u\n", __func__, cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410
Borislav Petkov54abf372008-02-06 02:57:52 +01001411 if (tape->chrdev_dir == IDETAPE_DIR_WRITE) {
Borislav Petkovd9df9372008-04-27 15:38:34 +02001412 ide_tape_flush_merge_buffer(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413 idetape_flush_tape_buffers(drive);
1414 }
1415 if (cmd == MTIOCGET || cmd == MTIOCPOS) {
Tejun Heo963da552009-04-19 08:46:02 +09001416 block_offset = tape->valid /
Borislav Petkov54bb2072008-02-06 02:57:52 +01001417 (tape->blk_size * tape->user_bs_factor);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001418 position = idetape_read_position(drive);
1419 if (position < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420 return -EIO;
1421 }
1422 switch (cmd) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001423 case MTIOCTOP:
1424 if (copy_from_user(&mtop, argp, sizeof(struct mtop)))
1425 return -EFAULT;
1426 return idetape_mtioctop(drive, mtop.mt_op, mtop.mt_count);
1427 case MTIOCGET:
1428 memset(&mtget, 0, sizeof(struct mtget));
1429 mtget.mt_type = MT_ISSCSI2;
1430 mtget.mt_blkno = position / tape->user_bs_factor - block_offset;
1431 mtget.mt_dsreg =
1432 ((tape->blk_size * tape->user_bs_factor)
1433 << MT_ST_BLKSIZE_SHIFT) & MT_ST_BLKSIZE_MASK;
Borislav Petkov54bb2072008-02-06 02:57:52 +01001434
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001435 if (tape->drv_write_prot)
1436 mtget.mt_gstat |= GMT_WR_PROT(0xffffffff);
1437
1438 if (copy_to_user(argp, &mtget, sizeof(struct mtget)))
1439 return -EFAULT;
1440 return 0;
1441 case MTIOCPOS:
1442 mtpos.mt_blkno = position / tape->user_bs_factor - block_offset;
1443 if (copy_to_user(argp, &mtpos, sizeof(struct mtpos)))
1444 return -EFAULT;
1445 return 0;
1446 default:
1447 if (tape->chrdev_dir == IDETAPE_DIR_READ)
Borislav Petkovec0fdb02008-04-27 15:38:34 +02001448 ide_tape_discard_merge_buffer(drive, 1);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001449 return idetape_blkdev_ioctl(drive, cmd, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450 }
1451}
1452
Borislav Petkov3cffb9c2008-02-02 19:56:50 +01001453/*
1454 * Do a mode sense page 0 with block descriptor and if it succeeds set the tape
1455 * block size with the reported value.
1456 */
1457static void ide_tape_get_bsize_from_bdesc(ide_drive_t *drive)
1458{
1459 idetape_tape_t *tape = drive->driver_data;
Borislav Petkovd236d742008-04-18 00:46:27 +02001460 struct ide_atapi_pc pc;
Borislav Petkov3cffb9c2008-02-02 19:56:50 +01001461
1462 idetape_create_mode_sense_cmd(&pc, IDETAPE_BLOCK_DESCRIPTOR);
Bartlomiej Zolnierkiewicz2ac07d92008-10-10 22:39:38 +02001463 if (ide_queue_pc_tail(drive, tape->disk, &pc)) {
Borislav Petkov3cffb9c2008-02-02 19:56:50 +01001464 printk(KERN_ERR "ide-tape: Can't get block descriptor\n");
Borislav Petkov54bb2072008-02-06 02:57:52 +01001465 if (tape->blk_size == 0) {
Borislav Petkov3cffb9c2008-02-02 19:56:50 +01001466 printk(KERN_WARNING "ide-tape: Cannot deal with zero "
1467 "block size, assuming 32k\n");
Borislav Petkov54bb2072008-02-06 02:57:52 +01001468 tape->blk_size = 32768;
Borislav Petkov3cffb9c2008-02-02 19:56:50 +01001469 }
1470 return;
1471 }
Borislav Petkovd236d742008-04-18 00:46:27 +02001472 tape->blk_size = (pc.buf[4 + 5] << 16) +
1473 (pc.buf[4 + 6] << 8) +
1474 pc.buf[4 + 7];
1475 tape->drv_write_prot = (pc.buf[2] & 0x80) >> 7;
Borislav Petkov3cffb9c2008-02-02 19:56:50 +01001476}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001478static int idetape_chrdev_open(struct inode *inode, struct file *filp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479{
1480 unsigned int minor = iminor(inode), i = minor & ~0xc0;
1481 ide_drive_t *drive;
1482 idetape_tape_t *tape;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483 int retval;
1484
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001485 if (i >= MAX_HWIFS * MAX_DRIVES)
1486 return -ENXIO;
1487
Jonathan Corbet04f4ac92008-05-15 12:01:56 -06001488 lock_kernel();
Borislav Petkov9d01e4c2009-06-08 22:03:03 +02001489 tape = ide_tape_get(NULL, true, i);
Jonathan Corbet04f4ac92008-05-15 12:01:56 -06001490 if (!tape) {
1491 unlock_kernel();
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001492 return -ENXIO;
Jonathan Corbet04f4ac92008-05-15 12:01:56 -06001493 }
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001494
1495 debug_log(DBG_CHRDEV, "Enter %s\n", __func__);
1496
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497 /*
1498 * We really want to do nonseekable_open(inode, filp); here, but some
1499 * versions of tar incorrectly call lseek on tapes and bail out if that
1500 * fails. So we disallow pread() and pwrite(), but permit lseeks.
1501 */
1502 filp->f_mode &= ~(FMODE_PREAD | FMODE_PWRITE);
1503
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504 drive = tape->drive;
1505
1506 filp->private_data = tape;
1507
Borislav Petkov49d80782009-06-07 15:37:06 +02001508 if (test_and_set_bit(ilog2(IDE_AFLAG_BUSY), &drive->atapi_flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509 retval = -EBUSY;
1510 goto out_put_tape;
1511 }
1512
1513 retval = idetape_wait_ready(drive, 60 * HZ);
1514 if (retval) {
Borislav Petkov49d80782009-06-07 15:37:06 +02001515 clear_bit(ilog2(IDE_AFLAG_BUSY), &drive->atapi_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001516 printk(KERN_ERR "ide-tape: %s: drive not ready\n", tape->name);
1517 goto out_put_tape;
1518 }
1519
1520 idetape_read_position(drive);
Borislav Petkov49d80782009-06-07 15:37:06 +02001521 if (!test_bit(ilog2(IDE_AFLAG_ADDRESS_VALID), &drive->atapi_flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522 (void)idetape_rewind_tape(drive);
1523
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524 /* Read block size and write protect status from drive. */
Borislav Petkov3cffb9c2008-02-02 19:56:50 +01001525 ide_tape_get_bsize_from_bdesc(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526
1527 /* Set write protect flag if device is opened as read-only. */
1528 if ((filp->f_flags & O_ACCMODE) == O_RDONLY)
1529 tape->write_prot = 1;
1530 else
1531 tape->write_prot = tape->drv_write_prot;
1532
1533 /* Make sure drive isn't write protected if user wants to write. */
1534 if (tape->write_prot) {
1535 if ((filp->f_flags & O_ACCMODE) == O_WRONLY ||
1536 (filp->f_flags & O_ACCMODE) == O_RDWR) {
Borislav Petkov49d80782009-06-07 15:37:06 +02001537 clear_bit(ilog2(IDE_AFLAG_BUSY), &drive->atapi_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001538 retval = -EROFS;
1539 goto out_put_tape;
1540 }
1541 }
1542
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001543 /* Lock the tape drive door so user can't eject. */
Borislav Petkov54abf372008-02-06 02:57:52 +01001544 if (tape->chrdev_dir == IDETAPE_DIR_NONE) {
Bartlomiej Zolnierkiewicz05780422008-10-10 22:39:38 +02001545 if (!ide_set_media_lock(drive, tape->disk, 1)) {
Bartlomiej Zolnierkiewicz385a4b82008-10-10 22:39:37 +02001546 if (tape->door_locked != DOOR_EXPLICITLY_LOCKED)
1547 tape->door_locked = DOOR_LOCKED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548 }
1549 }
Jonathan Corbet04f4ac92008-05-15 12:01:56 -06001550 unlock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551 return 0;
1552
1553out_put_tape:
1554 ide_tape_put(tape);
Jonathan Corbet04f4ac92008-05-15 12:01:56 -06001555 unlock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556 return retval;
1557}
1558
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001559static void idetape_write_release(ide_drive_t *drive, unsigned int minor)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560{
1561 idetape_tape_t *tape = drive->driver_data;
1562
Borislav Petkovd9df9372008-04-27 15:38:34 +02001563 ide_tape_flush_merge_buffer(drive);
Tejun Heo963da552009-04-19 08:46:02 +09001564 tape->buf = kmalloc(tape->buffer_size, GFP_KERNEL);
1565 if (tape->buf != NULL) {
Borislav Petkov54bb2072008-02-06 02:57:52 +01001566 idetape_pad_zeros(drive, tape->blk_size *
1567 (tape->user_bs_factor - 1));
Tejun Heo963da552009-04-19 08:46:02 +09001568 kfree(tape->buf);
1569 tape->buf = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570 }
1571 idetape_write_filemark(drive);
1572 idetape_flush_tape_buffers(drive);
1573 idetape_flush_tape_buffers(drive);
1574}
1575
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001576static int idetape_chrdev_release(struct inode *inode, struct file *filp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001577{
Borislav Petkov5aeddf92008-10-13 21:39:34 +02001578 struct ide_tape_obj *tape = filp->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579 ide_drive_t *drive = tape->drive;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001580 unsigned int minor = iminor(inode);
1581
1582 lock_kernel();
1583 tape = drive->driver_data;
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001584
1585 debug_log(DBG_CHRDEV, "Enter %s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001586
Borislav Petkov54abf372008-02-06 02:57:52 +01001587 if (tape->chrdev_dir == IDETAPE_DIR_WRITE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001588 idetape_write_release(drive, minor);
Borislav Petkov54abf372008-02-06 02:57:52 +01001589 if (tape->chrdev_dir == IDETAPE_DIR_READ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590 if (minor < 128)
Borislav Petkovec0fdb02008-04-27 15:38:34 +02001591 ide_tape_discard_merge_buffer(drive, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592 }
Borislav Petkovf64eee72008-04-27 15:38:25 +02001593
Borislav Petkov49d80782009-06-07 15:37:06 +02001594 if (minor < 128 && test_bit(ilog2(IDE_AFLAG_MEDIUM_PRESENT),
1595 &drive->atapi_flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596 (void) idetape_rewind_tape(drive);
Borislav Petkov49d80782009-06-07 15:37:06 +02001597
Borislav Petkov54abf372008-02-06 02:57:52 +01001598 if (tape->chrdev_dir == IDETAPE_DIR_NONE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599 if (tape->door_locked == DOOR_LOCKED) {
Bartlomiej Zolnierkiewicz05780422008-10-10 22:39:38 +02001600 if (!ide_set_media_lock(drive, tape->disk, 0))
Bartlomiej Zolnierkiewicz385a4b82008-10-10 22:39:37 +02001601 tape->door_locked = DOOR_UNLOCKED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001602 }
1603 }
Borislav Petkov49d80782009-06-07 15:37:06 +02001604 clear_bit(ilog2(IDE_AFLAG_BUSY), &drive->atapi_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001605 ide_tape_put(tape);
1606 unlock_kernel();
1607 return 0;
1608}
1609
Borislav Petkov6d29c8f2008-02-02 19:56:49 +01001610static void idetape_get_inquiry_results(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001611{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001612 idetape_tape_t *tape = drive->driver_data;
Borislav Petkovd236d742008-04-18 00:46:27 +02001613 struct ide_atapi_pc pc;
Bartlomiej Zolnierkiewicz41fa9f82009-03-31 20:15:25 +02001614 u8 pc_buf[256];
Borislav Petkov801bd322008-09-27 19:32:17 +02001615 char fw_rev[4], vendor_id[8], product_id[16];
Borislav Petkov6d29c8f2008-02-02 19:56:49 +01001616
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617 idetape_create_inquiry_cmd(&pc);
Bartlomiej Zolnierkiewicz41fa9f82009-03-31 20:15:25 +02001618 pc.buf = &pc_buf[0];
1619 pc.buf_size = sizeof(pc_buf);
1620
Bartlomiej Zolnierkiewicz2ac07d92008-10-10 22:39:38 +02001621 if (ide_queue_pc_tail(drive, tape->disk, &pc)) {
Borislav Petkov6d29c8f2008-02-02 19:56:49 +01001622 printk(KERN_ERR "ide-tape: %s: can't get INQUIRY results\n",
1623 tape->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001624 return;
1625 }
Borislav Petkovd236d742008-04-18 00:46:27 +02001626 memcpy(vendor_id, &pc.buf[8], 8);
1627 memcpy(product_id, &pc.buf[16], 16);
1628 memcpy(fw_rev, &pc.buf[32], 4);
Borislav Petkov6d29c8f2008-02-02 19:56:49 +01001629
Borislav Petkov801bd322008-09-27 19:32:17 +02001630 ide_fixstring(vendor_id, 8, 0);
1631 ide_fixstring(product_id, 16, 0);
1632 ide_fixstring(fw_rev, 4, 0);
Borislav Petkov41f81d542008-02-06 02:57:52 +01001633
Borislav Petkov801bd322008-09-27 19:32:17 +02001634 printk(KERN_INFO "ide-tape: %s <-> %s: %.8s %.16s rev %.4s\n",
Borislav Petkov41f81d542008-02-06 02:57:52 +01001635 drive->name, tape->name, vendor_id, product_id, fw_rev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636}
1637
1638/*
Borislav Petkovb6422012008-02-02 19:56:49 +01001639 * Ask the tape about its various parameters. In particular, we will adjust our
1640 * data transfer buffer size to the recommended value as returned by the tape.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641 */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001642static void idetape_get_mode_sense_results(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643{
1644 idetape_tape_t *tape = drive->driver_data;
Borislav Petkovd236d742008-04-18 00:46:27 +02001645 struct ide_atapi_pc pc;
Borislav Petkovb6422012008-02-02 19:56:49 +01001646 u8 *caps;
1647 u8 speed, max_speed;
Borislav Petkov47314fa2008-02-02 19:56:48 +01001648
Linus Torvalds1da177e2005-04-16 15:20:36 -07001649 idetape_create_mode_sense_cmd(&pc, IDETAPE_CAPABILITIES_PAGE);
Bartlomiej Zolnierkiewicz2ac07d92008-10-10 22:39:38 +02001650 if (ide_queue_pc_tail(drive, tape->disk, &pc)) {
Borislav Petkovb6422012008-02-02 19:56:49 +01001651 printk(KERN_ERR "ide-tape: Can't get tape parameters - assuming"
1652 " some default values\n");
Borislav Petkov54bb2072008-02-06 02:57:52 +01001653 tape->blk_size = 512;
Borislav Petkovb6422012008-02-02 19:56:49 +01001654 put_unaligned(52, (u16 *)&tape->caps[12]);
1655 put_unaligned(540, (u16 *)&tape->caps[14]);
1656 put_unaligned(6*52, (u16 *)&tape->caps[16]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001657 return;
1658 }
Borislav Petkovd236d742008-04-18 00:46:27 +02001659 caps = pc.buf + 4 + pc.buf[3];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001660
Borislav Petkovb6422012008-02-02 19:56:49 +01001661 /* convert to host order and save for later use */
Harvey Harrisoncd740ab2008-07-24 22:53:33 +02001662 speed = be16_to_cpup((__be16 *)&caps[14]);
1663 max_speed = be16_to_cpup((__be16 *)&caps[8]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664
Harvey Harrisoncd740ab2008-07-24 22:53:33 +02001665 *(u16 *)&caps[8] = max_speed;
1666 *(u16 *)&caps[12] = be16_to_cpup((__be16 *)&caps[12]);
1667 *(u16 *)&caps[14] = speed;
1668 *(u16 *)&caps[16] = be16_to_cpup((__be16 *)&caps[16]);
Borislav Petkovb6422012008-02-02 19:56:49 +01001669
1670 if (!speed) {
1671 printk(KERN_INFO "ide-tape: %s: invalid tape speed "
1672 "(assuming 650KB/sec)\n", drive->name);
Harvey Harrisoncd740ab2008-07-24 22:53:33 +02001673 *(u16 *)&caps[14] = 650;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001674 }
Borislav Petkovb6422012008-02-02 19:56:49 +01001675 if (!max_speed) {
1676 printk(KERN_INFO "ide-tape: %s: invalid max_speed "
1677 "(assuming 650KB/sec)\n", drive->name);
Harvey Harrisoncd740ab2008-07-24 22:53:33 +02001678 *(u16 *)&caps[8] = 650;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679 }
1680
Borislav Petkovb6422012008-02-02 19:56:49 +01001681 memcpy(&tape->caps, caps, 20);
Bartlomiej Zolnierkiewicz05780422008-10-10 22:39:38 +02001682
1683 /* device lacks locking support according to capabilities page */
1684 if ((caps[6] & 1) == 0)
Bartlomiej Zolnierkiewicz42619d32008-10-17 18:09:11 +02001685 drive->dev_flags &= ~IDE_DFLAG_DOORLOCKING;
Bartlomiej Zolnierkiewicz05780422008-10-10 22:39:38 +02001686
Borislav Petkovb6422012008-02-02 19:56:49 +01001687 if (caps[7] & 0x02)
Borislav Petkov54bb2072008-02-06 02:57:52 +01001688 tape->blk_size = 512;
Borislav Petkovb6422012008-02-02 19:56:49 +01001689 else if (caps[7] & 0x04)
Borislav Petkov54bb2072008-02-06 02:57:52 +01001690 tape->blk_size = 1024;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001691}
1692
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +02001693#ifdef CONFIG_IDE_PROC_FS
Bartlomiej Zolnierkiewicz8185d5a2008-10-10 22:39:28 +02001694#define ide_tape_devset_get(name, field) \
1695static int get_##name(ide_drive_t *drive) \
1696{ \
1697 idetape_tape_t *tape = drive->driver_data; \
1698 return tape->field; \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001699}
Bartlomiej Zolnierkiewicz8185d5a2008-10-10 22:39:28 +02001700
1701#define ide_tape_devset_set(name, field) \
1702static int set_##name(ide_drive_t *drive, int arg) \
1703{ \
1704 idetape_tape_t *tape = drive->driver_data; \
1705 tape->field = arg; \
1706 return 0; \
1707}
1708
Elias Oltmanns92f1f8f2008-10-10 22:39:40 +02001709#define ide_tape_devset_rw_field(_name, _field) \
Bartlomiej Zolnierkiewicz8185d5a2008-10-10 22:39:28 +02001710ide_tape_devset_get(_name, _field) \
1711ide_tape_devset_set(_name, _field) \
Elias Oltmanns92f1f8f2008-10-10 22:39:40 +02001712IDE_DEVSET(_name, DS_SYNC, get_##_name, set_##_name)
Bartlomiej Zolnierkiewicz8185d5a2008-10-10 22:39:28 +02001713
Elias Oltmanns92f1f8f2008-10-10 22:39:40 +02001714#define ide_tape_devset_r_field(_name, _field) \
Bartlomiej Zolnierkiewicz8185d5a2008-10-10 22:39:28 +02001715ide_tape_devset_get(_name, _field) \
Elias Oltmanns92f1f8f2008-10-10 22:39:40 +02001716IDE_DEVSET(_name, 0, get_##_name, NULL)
Bartlomiej Zolnierkiewicz8185d5a2008-10-10 22:39:28 +02001717
1718static int mulf_tdsc(ide_drive_t *drive) { return 1000; }
1719static int divf_tdsc(ide_drive_t *drive) { return HZ; }
1720static int divf_buffer(ide_drive_t *drive) { return 2; }
1721static int divf_buffer_size(ide_drive_t *drive) { return 1024; }
1722
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +02001723ide_devset_rw_flag(dsc_overlap, IDE_DFLAG_DSC_OVERLAP);
Bartlomiej Zolnierkiewicz8185d5a2008-10-10 22:39:28 +02001724
Elias Oltmanns92f1f8f2008-10-10 22:39:40 +02001725ide_tape_devset_rw_field(debug_mask, debug_mask);
1726ide_tape_devset_rw_field(tdsc, best_dsc_rw_freq);
Bartlomiej Zolnierkiewicz8185d5a2008-10-10 22:39:28 +02001727
Elias Oltmanns92f1f8f2008-10-10 22:39:40 +02001728ide_tape_devset_r_field(avg_speed, avg_speed);
1729ide_tape_devset_r_field(speed, caps[14]);
1730ide_tape_devset_r_field(buffer, caps[16]);
1731ide_tape_devset_r_field(buffer_size, buffer_size);
Bartlomiej Zolnierkiewicz8185d5a2008-10-10 22:39:28 +02001732
Elias Oltmanns92f1f8f2008-10-10 22:39:40 +02001733static const struct ide_proc_devset idetape_settings[] = {
1734 __IDE_PROC_DEVSET(avg_speed, 0, 0xffff, NULL, NULL),
1735 __IDE_PROC_DEVSET(buffer, 0, 0xffff, NULL, divf_buffer),
1736 __IDE_PROC_DEVSET(buffer_size, 0, 0xffff, NULL, divf_buffer_size),
1737 __IDE_PROC_DEVSET(debug_mask, 0, 0xffff, NULL, NULL),
1738 __IDE_PROC_DEVSET(dsc_overlap, 0, 1, NULL, NULL),
1739 __IDE_PROC_DEVSET(speed, 0, 0xffff, NULL, NULL),
1740 __IDE_PROC_DEVSET(tdsc, IDETAPE_DSC_RW_MIN, IDETAPE_DSC_RW_MAX,
1741 mulf_tdsc, divf_tdsc),
Hannes Eder71bfc7a2009-03-05 16:10:56 +01001742 { NULL },
Bartlomiej Zolnierkiewicz8185d5a2008-10-10 22:39:28 +02001743};
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +02001744#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001745
1746/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001747 * The function below is called to:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001748 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001749 * 1. Initialize our various state variables.
1750 * 2. Ask the tape for its capabilities.
1751 * 3. Allocate a buffer which will be used for data transfer. The buffer size
1752 * is chosen based on the recommendation which we received in step 2.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001753 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001754 * Note that at this point ide.c already assigned us an irq, so that we can
1755 * queue requests here and wait for their completion.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001756 */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001757static void idetape_setup(ide_drive_t *drive, idetape_tape_t *tape, int minor)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001758{
Borislav Petkov83042b22008-04-27 15:38:27 +02001759 unsigned long t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001760 int speed;
Borislav Petkovf73850a2008-04-27 15:38:33 +02001761 int buffer_size;
Borislav Petkovb6422012008-02-02 19:56:49 +01001762 u16 *ctl = (u16 *)&tape->caps[12];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001763
Bartlomiej Zolnierkiewicz85e39032008-10-13 21:39:32 +02001764 drive->pc_callback = ide_tape_callback;
Borislav Petkov776bb022008-07-23 19:55:59 +02001765
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +02001766 drive->dev_flags |= IDE_DFLAG_DSC_OVERLAP;
1767
Bartlomiej Zolnierkiewicz4166c192008-02-01 23:09:30 +01001768 if (drive->hwif->host_flags & IDE_HFLAG_NO_DSC) {
1769 printk(KERN_INFO "ide-tape: %s: disabling DSC overlap\n",
1770 tape->name);
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +02001771 drive->dev_flags &= ~IDE_DFLAG_DSC_OVERLAP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001772 }
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +02001773
Linus Torvalds1da177e2005-04-16 15:20:36 -07001774 /* Seagate Travan drives do not support DSC overlap. */
Bartlomiej Zolnierkiewicz4dde4492008-10-10 22:39:19 +02001775 if (strstr((char *)&drive->id[ATA_ID_PROD], "Seagate STT3401"))
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +02001776 drive->dev_flags &= ~IDE_DFLAG_DSC_OVERLAP;
1777
Linus Torvalds1da177e2005-04-16 15:20:36 -07001778 tape->minor = minor;
1779 tape->name[0] = 'h';
1780 tape->name[1] = 't';
1781 tape->name[2] = '0' + minor;
Borislav Petkov54abf372008-02-06 02:57:52 +01001782 tape->chrdev_dir = IDETAPE_DIR_NONE;
Bartlomiej Zolnierkiewicz4dde4492008-10-10 22:39:19 +02001783
Linus Torvalds1da177e2005-04-16 15:20:36 -07001784 idetape_get_inquiry_results(drive);
1785 idetape_get_mode_sense_results(drive);
Borislav Petkov3cffb9c2008-02-02 19:56:50 +01001786 ide_tape_get_bsize_from_bdesc(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001787 tape->user_bs_factor = 1;
Borislav Petkovf73850a2008-04-27 15:38:33 +02001788 tape->buffer_size = *ctl * tape->blk_size;
1789 while (tape->buffer_size > 0xffff) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790 printk(KERN_NOTICE "ide-tape: decreasing stage size\n");
Borislav Petkovb6422012008-02-02 19:56:49 +01001791 *ctl /= 2;
Borislav Petkovf73850a2008-04-27 15:38:33 +02001792 tape->buffer_size = *ctl * tape->blk_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001793 }
Borislav Petkovf73850a2008-04-27 15:38:33 +02001794 buffer_size = tape->buffer_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001795
Borislav Petkov83042b22008-04-27 15:38:27 +02001796 /* select the "best" DSC read/write polling freq */
Borislav Petkovb6422012008-02-02 19:56:49 +01001797 speed = max(*(u16 *)&tape->caps[14], *(u16 *)&tape->caps[8]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001798
Borislav Petkovf73850a2008-04-27 15:38:33 +02001799 t = (IDETAPE_FIFO_THRESHOLD * tape->buffer_size * HZ) / (speed * 1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001800
1801 /*
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001802 * Ensure that the number we got makes sense; limit it within
1803 * IDETAPE_DSC_RW_MIN and IDETAPE_DSC_RW_MAX.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001804 */
Harvey Harrisona792bd52008-07-15 21:21:41 +02001805 tape->best_dsc_rw_freq = clamp_t(unsigned long, t, IDETAPE_DSC_RW_MIN,
1806 IDETAPE_DSC_RW_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807 printk(KERN_INFO "ide-tape: %s <-> %s: %dKBps, %d*%dkB buffer, "
Borislav Petkov83042b22008-04-27 15:38:27 +02001808 "%lums tDSC%s\n",
Borislav Petkovb6422012008-02-02 19:56:49 +01001809 drive->name, tape->name, *(u16 *)&tape->caps[14],
Borislav Petkovf73850a2008-04-27 15:38:33 +02001810 (*(u16 *)&tape->caps[16] * 512) / tape->buffer_size,
1811 tape->buffer_size / 1024,
Borislav Petkov54bb2072008-02-06 02:57:52 +01001812 tape->best_dsc_rw_freq * 1000 / HZ,
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +02001813 (drive->dev_flags & IDE_DFLAG_USING_DMA) ? ", DMA" : "");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001814
Bartlomiej Zolnierkiewicz1e874f42008-10-10 22:39:27 +02001815 ide_proc_register_driver(drive, tape->driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001816}
1817
Russell King4031bbe2006-01-06 11:41:00 +00001818static void ide_tape_remove(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001819{
1820 idetape_tape_t *tape = drive->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001821
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +02001822 ide_proc_unregister_driver(drive, tape->driver);
Bartlomiej Zolnierkiewicz8fed4362009-02-25 20:28:24 +01001823 device_del(&tape->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001824 ide_unregister_region(tape->disk);
1825
Bartlomiej Zolnierkiewicz8fed4362009-02-25 20:28:24 +01001826 mutex_lock(&idetape_ref_mutex);
1827 put_device(&tape->dev);
1828 mutex_unlock(&idetape_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001829}
1830
Bartlomiej Zolnierkiewicz8fed4362009-02-25 20:28:24 +01001831static void ide_tape_release(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001832{
Bartlomiej Zolnierkiewicz8fed4362009-02-25 20:28:24 +01001833 struct ide_tape_obj *tape = to_ide_drv(dev, ide_tape_obj);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001834 ide_drive_t *drive = tape->drive;
1835 struct gendisk *g = tape->disk;
1836
Tejun Heo963da552009-04-19 08:46:02 +09001837 BUG_ON(tape->valid);
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02001838
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +02001839 drive->dev_flags &= ~IDE_DFLAG_DSC_OVERLAP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001840 drive->driver_data = NULL;
Tony Jonesdbc12722007-09-25 02:03:03 +02001841 device_destroy(idetape_sysfs_class, MKDEV(IDETAPE_MAJOR, tape->minor));
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001842 device_destroy(idetape_sysfs_class,
1843 MKDEV(IDETAPE_MAJOR, tape->minor + 128));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001844 idetape_devs[tape->minor] = NULL;
1845 g->private_data = NULL;
1846 put_disk(g);
1847 kfree(tape);
1848}
1849
Bartlomiej Zolnierkiewiczecfd80e2007-05-10 00:01:09 +02001850#ifdef CONFIG_IDE_PROC_FS
Linus Torvalds1da177e2005-04-16 15:20:36 -07001851static int proc_idetape_read_name
1852 (char *page, char **start, off_t off, int count, int *eof, void *data)
1853{
1854 ide_drive_t *drive = (ide_drive_t *) data;
1855 idetape_tape_t *tape = drive->driver_data;
1856 char *out = page;
1857 int len;
1858
1859 len = sprintf(out, "%s\n", tape->name);
1860 PROC_IDE_READ_RETURN(page, start, off, count, eof, len);
1861}
1862
1863static ide_proc_entry_t idetape_proc[] = {
1864 { "capacity", S_IFREG|S_IRUGO, proc_ide_read_capacity, NULL },
1865 { "name", S_IFREG|S_IRUGO, proc_idetape_read_name, NULL },
1866 { NULL, 0, NULL, NULL }
1867};
Bartlomiej Zolnierkiewicz79cb3802008-10-17 18:09:13 +02001868
1869static ide_proc_entry_t *ide_tape_proc_entries(ide_drive_t *drive)
1870{
1871 return idetape_proc;
1872}
1873
1874static const struct ide_proc_devset *ide_tape_proc_devsets(ide_drive_t *drive)
1875{
1876 return idetape_settings;
1877}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001878#endif
1879
Russell King4031bbe2006-01-06 11:41:00 +00001880static int ide_tape_probe(ide_drive_t *);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001881
Bartlomiej Zolnierkiewicz7f3c8682009-01-06 17:20:53 +01001882static struct ide_driver idetape_driver = {
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02001883 .gen_driver = {
Laurent Riffard4ef3b8f2005-11-18 22:15:40 +01001884 .owner = THIS_MODULE,
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02001885 .name = "ide-tape",
1886 .bus = &ide_bus_type,
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02001887 },
Russell King4031bbe2006-01-06 11:41:00 +00001888 .probe = ide_tape_probe,
1889 .remove = ide_tape_remove,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001890 .version = IDETAPE_VERSION,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001891 .do_request = idetape_do_request,
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +02001892#ifdef CONFIG_IDE_PROC_FS
Bartlomiej Zolnierkiewicz79cb3802008-10-17 18:09:13 +02001893 .proc_entries = ide_tape_proc_entries,
1894 .proc_devsets = ide_tape_proc_devsets,
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +02001895#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001896};
1897
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001898/* Our character device supporting functions, passed to register_chrdev. */
Arjan van de Ven2b8693c2007-02-12 00:55:32 -08001899static const struct file_operations idetape_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001900 .owner = THIS_MODULE,
1901 .read = idetape_chrdev_read,
1902 .write = idetape_chrdev_write,
1903 .ioctl = idetape_chrdev_ioctl,
1904 .open = idetape_chrdev_open,
1905 .release = idetape_chrdev_release,
1906};
1907
Al Viroa4600f82008-03-02 10:27:58 -05001908static int idetape_open(struct block_device *bdev, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001909{
Borislav Petkov9d01e4c2009-06-08 22:03:03 +02001910 struct ide_tape_obj *tape = ide_tape_get(bdev->bd_disk, false, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001911
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001912 if (!tape)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001913 return -ENXIO;
1914
Linus Torvalds1da177e2005-04-16 15:20:36 -07001915 return 0;
1916}
1917
Al Viroa4600f82008-03-02 10:27:58 -05001918static int idetape_release(struct gendisk *disk, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001919{
Borislav Petkov5aeddf92008-10-13 21:39:34 +02001920 struct ide_tape_obj *tape = ide_drv_g(disk, ide_tape_obj);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001921
1922 ide_tape_put(tape);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001923 return 0;
1924}
1925
Al Viroa4600f82008-03-02 10:27:58 -05001926static int idetape_ioctl(struct block_device *bdev, fmode_t mode,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001927 unsigned int cmd, unsigned long arg)
1928{
Borislav Petkov5aeddf92008-10-13 21:39:34 +02001929 struct ide_tape_obj *tape = ide_drv_g(bdev->bd_disk, ide_tape_obj);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001930 ide_drive_t *drive = tape->drive;
Al Viro1bddd9e2008-09-02 17:19:43 -04001931 int err = generic_ide_ioctl(drive, bdev, cmd, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001932 if (err == -EINVAL)
1933 err = idetape_blkdev_ioctl(drive, cmd, arg);
1934 return err;
1935}
1936
1937static struct block_device_operations idetape_block_ops = {
1938 .owner = THIS_MODULE,
Al Viroa4600f82008-03-02 10:27:58 -05001939 .open = idetape_open,
1940 .release = idetape_release,
1941 .locked_ioctl = idetape_ioctl,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001942};
1943
Russell King4031bbe2006-01-06 11:41:00 +00001944static int ide_tape_probe(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001945{
1946 idetape_tape_t *tape;
1947 struct gendisk *g;
1948 int minor;
1949
1950 if (!strstr("ide-tape", drive->driver_req))
1951 goto failed;
Bartlomiej Zolnierkiewicz2a924662008-10-10 22:39:24 +02001952
Linus Torvalds1da177e2005-04-16 15:20:36 -07001953 if (drive->media != ide_tape)
1954 goto failed;
Bartlomiej Zolnierkiewicz2a924662008-10-10 22:39:24 +02001955
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +02001956 if ((drive->dev_flags & IDE_DFLAG_ID_READ) &&
1957 ide_check_atapi_device(drive, DRV_NAME) == 0) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001958 printk(KERN_ERR "ide-tape: %s: not supported by this version of"
1959 " the driver\n", drive->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001960 goto failed;
1961 }
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001962 tape = kzalloc(sizeof(idetape_tape_t), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001963 if (tape == NULL) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001964 printk(KERN_ERR "ide-tape: %s: Can't allocate a tape struct\n",
1965 drive->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001966 goto failed;
1967 }
1968
1969 g = alloc_disk(1 << PARTN_BITS);
1970 if (!g)
1971 goto out_free_tape;
1972
1973 ide_init_disk(g, drive);
1974
Bartlomiej Zolnierkiewicz8fed4362009-02-25 20:28:24 +01001975 tape->dev.parent = &drive->gendev;
1976 tape->dev.release = ide_tape_release;
1977 dev_set_name(&tape->dev, dev_name(&drive->gendev));
1978
1979 if (device_register(&tape->dev))
1980 goto out_free_disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001981
1982 tape->drive = drive;
1983 tape->driver = &idetape_driver;
1984 tape->disk = g;
1985
1986 g->private_data = &tape->driver;
1987
1988 drive->driver_data = tape;
1989
Arjan van de Vencf8b8972006-03-23 03:00:45 -08001990 mutex_lock(&idetape_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001991 for (minor = 0; idetape_devs[minor]; minor++)
1992 ;
1993 idetape_devs[minor] = tape;
Arjan van de Vencf8b8972006-03-23 03:00:45 -08001994 mutex_unlock(&idetape_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001995
1996 idetape_setup(drive, tape, minor);
1997
Greg Kroah-Hartman3ee074b2008-07-21 20:03:34 -07001998 device_create(idetape_sysfs_class, &drive->gendev,
1999 MKDEV(IDETAPE_MAJOR, minor), NULL, "%s", tape->name);
2000 device_create(idetape_sysfs_class, &drive->gendev,
2001 MKDEV(IDETAPE_MAJOR, minor + 128), NULL,
2002 "n%s", tape->name);
Will Dysond5dee802005-09-16 02:55:07 -07002003
Linus Torvalds1da177e2005-04-16 15:20:36 -07002004 g->fops = &idetape_block_ops;
2005 ide_register_region(g);
2006
2007 return 0;
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02002008
Bartlomiej Zolnierkiewicz8fed4362009-02-25 20:28:24 +01002009out_free_disk:
2010 put_disk(g);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002011out_free_tape:
2012 kfree(tape);
2013failed:
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02002014 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002015}
2016
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002017static void __exit idetape_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002018{
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02002019 driver_unregister(&idetape_driver.gen_driver);
Will Dysond5dee802005-09-16 02:55:07 -07002020 class_destroy(idetape_sysfs_class);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002021 unregister_chrdev(IDETAPE_MAJOR, "ht");
2022}
2023
Bartlomiej Zolnierkiewicz17514e82005-11-19 22:24:35 +01002024static int __init idetape_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002025{
Will Dysond5dee802005-09-16 02:55:07 -07002026 int error = 1;
2027 idetape_sysfs_class = class_create(THIS_MODULE, "ide_tape");
2028 if (IS_ERR(idetape_sysfs_class)) {
2029 idetape_sysfs_class = NULL;
2030 printk(KERN_ERR "Unable to create sysfs class for ide tapes\n");
2031 error = -EBUSY;
2032 goto out;
2033 }
2034
Linus Torvalds1da177e2005-04-16 15:20:36 -07002035 if (register_chrdev(IDETAPE_MAJOR, "ht", &idetape_fops)) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002036 printk(KERN_ERR "ide-tape: Failed to register chrdev"
2037 " interface\n");
Will Dysond5dee802005-09-16 02:55:07 -07002038 error = -EBUSY;
2039 goto out_free_class;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002040 }
Will Dysond5dee802005-09-16 02:55:07 -07002041
2042 error = driver_register(&idetape_driver.gen_driver);
2043 if (error)
2044 goto out_free_driver;
2045
2046 return 0;
2047
2048out_free_driver:
2049 driver_unregister(&idetape_driver.gen_driver);
2050out_free_class:
2051 class_destroy(idetape_sysfs_class);
2052out:
2053 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002054}
2055
Kay Sievers263756e2005-12-12 18:03:44 +01002056MODULE_ALIAS("ide:*m-tape*");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002057module_init(idetape_init);
2058module_exit(idetape_exit);
2059MODULE_ALIAS_CHARDEV_MAJOR(IDETAPE_MAJOR);
Borislav Petkov9c145762008-02-06 02:57:54 +01002060MODULE_DESCRIPTION("ATAPI Streaming TAPE Driver");
2061MODULE_LICENSE("GPL");