blob: b373ac876352a4366fc6e50ed0f77ef41bc45494 [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
134struct idetape_bh {
Stephen Rothwellab057962007-08-01 23:46:44 +0200135 u32 b_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 atomic_t b_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 char *b_data;
138};
139
Borislav Petkov03056b92008-04-18 00:46:26 +0200140/* Tape door status */
141#define DOOR_UNLOCKED 0
142#define DOOR_LOCKED 1
143#define DOOR_EXPLICITLY_LOCKED 2
144
145/* Some defines for the SPACE command */
146#define IDETAPE_SPACE_OVER_FILEMARK 1
147#define IDETAPE_SPACE_TO_EOD 3
148
149/* Some defines for the LOAD UNLOAD command */
150#define IDETAPE_LU_LOAD_MASK 1
151#define IDETAPE_LU_RETENSION_MASK 2
152#define IDETAPE_LU_EOT_MASK 4
153
Borislav Petkov03056b92008-04-18 00:46:26 +0200154/* Structures related to the SELECT SENSE / MODE SENSE packet commands. */
155#define IDETAPE_BLOCK_DESCRIPTOR 0
156#define IDETAPE_CAPABILITIES_PAGE 0x2a
157
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100159 * Most of our global data which we need to save even as we leave the driver due
160 * to an interrupt or a timer event is stored in the struct defined below.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 */
162typedef struct ide_tape_obj {
Bartlomiej Zolnierkiewicz7f3c8682009-01-06 17:20:53 +0100163 ide_drive_t *drive;
164 struct ide_driver *driver;
165 struct gendisk *disk;
Bartlomiej Zolnierkiewicz8fed4362009-02-25 20:28:24 +0100166 struct device dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167
Bartlomiej Zolnierkiewicz2e8a6f82008-10-10 22:39:36 +0200168 /* used by REQ_IDETAPE_{READ,WRITE} requests */
169 struct ide_atapi_pc queued_pc;
Bartlomiej Zolnierkiewicz394a4c22008-10-10 22:39:35 +0200170
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 /*
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100172 * DSC polling variables.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100174 * While polling for DSC we use postponed_rq to postpone the current
175 * request so that ide.c will be able to service pending requests on the
176 * other device. Note that at most we will have only one DSC (usually
Borislav Petkov5bd50dc2008-04-27 15:38:28 +0200177 * data transfer) request in the device request queue.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 */
179 struct request *postponed_rq;
180 /* The time in which we started polling for DSC */
181 unsigned long dsc_polling_start;
182 /* Timer used to poll for dsc */
183 struct timer_list dsc_timer;
184 /* Read/Write dsc polling frequency */
Borislav Petkov54bb2072008-02-06 02:57:52 +0100185 unsigned long best_dsc_rw_freq;
186 unsigned long dsc_poll_freq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 unsigned long dsc_timeout;
188
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100189 /* Read position information */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 u8 partition;
191 /* Current block */
Borislav Petkov54bb2072008-02-06 02:57:52 +0100192 unsigned int first_frame;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100194 /* Last error information */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 u8 sense_key, asc, ascq;
196
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100197 /* Character device operation */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 unsigned int minor;
199 /* device name */
200 char name[4];
201 /* Current character device data transfer direction */
Borislav Petkov54abf372008-02-06 02:57:52 +0100202 u8 chrdev_dir;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203
Borislav Petkov54bb2072008-02-06 02:57:52 +0100204 /* tape block size, usually 512 or 1024 bytes */
205 unsigned short blk_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 int user_bs_factor;
Borislav Petkovb6422012008-02-02 19:56:49 +0100207
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 /* Copy of the tape's Capabilities and Mechanical Page */
Borislav Petkovb6422012008-02-02 19:56:49 +0100209 u8 caps[20];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210
211 /*
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100212 * Active data transfer request parameters.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100214 * At most, there is only one ide-tape originated data transfer request
215 * in the device request queue. This allows ide.c to easily service
216 * requests from the other device when we postpone our active request.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 */
Borislav Petkov83042b22008-04-27 15:38:27 +0200218
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100219 /* Data buffer size chosen based on the tape's recommendation */
Borislav Petkovf73850a2008-04-27 15:38:33 +0200220 int buffer_size;
Borislav Petkov077e3bd2008-04-27 15:38:34 +0200221 /* merge buffer */
222 struct idetape_bh *merge_bh;
Borislav Petkov01a63aeb2008-04-27 15:38:34 +0200223 /* size of the merge buffer */
224 int merge_bh_size;
Borislav Petkov077e3bd2008-04-27 15:38:34 +0200225 /* pointer to current buffer head within the merge buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 struct idetape_bh *bh;
227 char *b_data;
228 int b_count;
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100229
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100230 /* Measures average tape speed */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 unsigned long avg_time;
232 int avg_size;
233 int avg_speed;
234
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 /* the door is currently locked */
236 int door_locked;
237 /* the tape hardware is write protected */
238 char drv_write_prot;
239 /* the tape is write protected (hardware or opened as read-only) */
240 char write_prot;
241
Borislav Petkov8004a8c2008-02-06 02:57:51 +0100242 u32 debug_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243} idetape_tape_t;
244
Arjan van de Vencf8b8972006-03-23 03:00:45 -0800245static DEFINE_MUTEX(idetape_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246
Will Dysond5dee802005-09-16 02:55:07 -0700247static struct class *idetape_sysfs_class;
248
Bartlomiej Zolnierkiewicz8fed4362009-02-25 20:28:24 +0100249static void ide_tape_release(struct device *);
Bartlomiej Zolnierkiewicz08da5912008-07-24 22:53:15 +0200250
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251static struct ide_tape_obj *ide_tape_get(struct gendisk *disk)
252{
253 struct ide_tape_obj *tape = NULL;
254
Arjan van de Vencf8b8972006-03-23 03:00:45 -0800255 mutex_lock(&idetape_ref_mutex);
Borislav Petkov5aeddf92008-10-13 21:39:34 +0200256 tape = ide_drv_g(disk, ide_tape_obj);
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 }
Arjan van de Vencf8b8972006-03-23 03:00:45 -0800263 mutex_unlock(&idetape_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 return tape;
265}
266
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267static void ide_tape_put(struct ide_tape_obj *tape)
268{
Bartlomiej Zolnierkiewiczd3e33ff2008-08-05 18:16:59 +0200269 ide_drive_t *drive = tape->drive;
270
Arjan van de Vencf8b8972006-03-23 03:00:45 -0800271 mutex_lock(&idetape_ref_mutex);
Bartlomiej Zolnierkiewicz8fed4362009-02-25 20:28:24 +0100272 put_device(&tape->dev);
Bartlomiej Zolnierkiewiczd3e33ff2008-08-05 18:16:59 +0200273 ide_device_put(drive);
Arjan van de Vencf8b8972006-03-23 03:00:45 -0800274 mutex_unlock(&idetape_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275}
276
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100278 * The variables below are used for the character device interface. Additional
279 * state variables are defined in our ide_drive_t structure.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100281static struct ide_tape_obj *idetape_devs[MAX_HWIFS * MAX_DRIVES];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283static struct ide_tape_obj *ide_tape_chrdev_get(unsigned int i)
284{
285 struct ide_tape_obj *tape = NULL;
286
Arjan van de Vencf8b8972006-03-23 03:00:45 -0800287 mutex_lock(&idetape_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 tape = idetape_devs[i];
289 if (tape)
Bartlomiej Zolnierkiewicz8fed4362009-02-25 20:28:24 +0100290 get_device(&tape->dev);
Arjan van de Vencf8b8972006-03-23 03:00:45 -0800291 mutex_unlock(&idetape_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 return tape;
293}
294
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295/*
Borislav Petkov1b5db432008-02-02 19:56:48 +0100296 * called on each failed packet command retry to analyze the request sense. We
297 * currently do not utilize this information.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 */
Borislav Petkov1b5db432008-02-02 19:56:48 +0100299static void idetape_analyze_error(ide_drive_t *drive, u8 *sense)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300{
301 idetape_tape_t *tape = drive->driver_data;
Bartlomiej Zolnierkiewicz5e2040fd2009-03-27 12:46:34 +0100302 struct ide_atapi_pc *pc = drive->failed_pc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303
Borislav Petkov1b5db432008-02-02 19:56:48 +0100304 tape->sense_key = sense[2] & 0xF;
305 tape->asc = sense[12];
306 tape->ascq = sense[13];
Borislav Petkov8004a8c2008-02-06 02:57:51 +0100307
308 debug_log(DBG_ERR, "pc = %x, sense key = %x, asc = %x, ascq = %x\n",
309 pc->c[0], tape->sense_key, tape->asc, tape->ascq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310
Borislav Petkovd236d742008-04-18 00:46:27 +0200311 /* Correct pc->xferred by asking the tape. */
Tejun Heo21d9c5d2009-04-19 08:46:02 +0900312 if (pc->flags & PC_FLAG_DMA_ERROR)
Borislav Petkovd236d742008-04-18 00:46:27 +0200313 pc->xferred = pc->req_xfer -
Borislav Petkov54bb2072008-02-06 02:57:52 +0100314 tape->blk_size *
Harvey Harrison5d0cc8a2008-07-15 21:21:41 +0200315 get_unaligned_be32(&sense[3]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316
317 /*
318 * If error was the result of a zero-length read or write command,
319 * with sense key=5, asc=0x22, ascq=0, let it slide. Some drives
320 * (i.e. Seagate STT3401A Travan) don't support 0-length read/writes.
321 */
Borislav Petkov90699ce2008-02-02 19:56:50 +0100322 if ((pc->c[0] == READ_6 || pc->c[0] == WRITE_6)
Borislav Petkov1b5db432008-02-02 19:56:48 +0100323 /* length == 0 */
324 && pc->c[4] == 0 && pc->c[3] == 0 && pc->c[2] == 0) {
325 if (tape->sense_key == 5) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 /* don't report an error, everything's ok */
327 pc->error = 0;
328 /* don't retry read/write */
Borislav Petkov346331f2008-04-18 00:46:26 +0200329 pc->flags |= PC_FLAG_ABORT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 }
331 }
Borislav Petkov90699ce2008-02-02 19:56:50 +0100332 if (pc->c[0] == READ_6 && (sense[2] & 0x80)) {
Bartlomiej Zolnierkiewiczc152cc12009-03-27 12:46:34 +0100333 pc->error = IDE_DRV_ERROR_FILEMARK;
Borislav Petkov346331f2008-04-18 00:46:26 +0200334 pc->flags |= PC_FLAG_ABORT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 }
Borislav Petkov90699ce2008-02-02 19:56:50 +0100336 if (pc->c[0] == WRITE_6) {
Borislav Petkov1b5db432008-02-02 19:56:48 +0100337 if ((sense[2] & 0x40) || (tape->sense_key == 0xd
338 && tape->asc == 0x0 && tape->ascq == 0x2)) {
Bartlomiej Zolnierkiewiczc152cc12009-03-27 12:46:34 +0100339 pc->error = IDE_DRV_ERROR_EOD;
Borislav Petkov346331f2008-04-18 00:46:26 +0200340 pc->flags |= PC_FLAG_ABORT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 }
342 }
Borislav Petkov90699ce2008-02-02 19:56:50 +0100343 if (pc->c[0] == READ_6 || pc->c[0] == WRITE_6) {
Borislav Petkov1b5db432008-02-02 19:56:48 +0100344 if (tape->sense_key == 8) {
Bartlomiej Zolnierkiewiczc152cc12009-03-27 12:46:34 +0100345 pc->error = IDE_DRV_ERROR_EOD;
Borislav Petkov346331f2008-04-18 00:46:26 +0200346 pc->flags |= PC_FLAG_ABORT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 }
Borislav Petkov346331f2008-04-18 00:46:26 +0200348 if (!(pc->flags & PC_FLAG_ABORT) &&
Borislav Petkovd236d742008-04-18 00:46:27 +0200349 pc->xferred)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 pc->retries = IDETAPE_MAX_PC_RETRIES + 1;
351 }
352}
353
Borislav Petkovd01dbc32008-04-27 15:38:33 +0200354/* Free data buffers completely. */
Borislav Petkov077e3bd2008-04-27 15:38:34 +0200355static void ide_tape_kfree_buffer(idetape_tape_t *tape)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356{
Tejun Heo35ab8d32009-04-19 08:46:02 +0900357 struct idetape_bh *bh = tape->merge_bh;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358
Tejun Heo35ab8d32009-04-19 08:46:02 +0900359 kfree(bh->b_data);
360 kfree(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361}
362
Bartlomiej Zolnierkiewiczb14c7212008-10-13 21:39:30 +0200363static void ide_tape_handle_dsc(ide_drive_t *);
364
Bartlomiej Zolnierkiewicz03a2faa2009-03-27 12:46:36 +0100365static int ide_tape_callback(ide_drive_t *drive, int dsc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366{
367 idetape_tape_t *tape = drive->driver_data;
Bartlomiej Zolnierkiewicz2b9efba2008-10-13 21:39:31 +0200368 struct ide_atapi_pc *pc = drive->pc;
Bartlomiej Zolnierkiewicz313afea2009-03-27 12:46:34 +0100369 struct request *rq = drive->hwif->rq;
Bartlomiej Zolnierkiewicz5985e6a2008-07-15 21:21:55 +0200370 int uptodate = pc->error ? 0 : 1;
Bartlomiej Zolnierkiewicz313afea2009-03-27 12:46:34 +0100371 int err = uptodate ? 0 : IDE_DRV_ERROR_GENERAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372
Borislav Petkov8004a8c2008-02-06 02:57:51 +0100373 debug_log(DBG_PROCS, "Enter %s\n", __func__);
374
Bartlomiej Zolnierkiewiczb14c7212008-10-13 21:39:30 +0200375 if (dsc)
376 ide_tape_handle_dsc(drive);
377
Bartlomiej Zolnierkiewicz5e2040fd2009-03-27 12:46:34 +0100378 if (drive->failed_pc == pc)
379 drive->failed_pc = NULL;
Bartlomiej Zolnierkiewiczdd2e9a02008-07-15 21:22:01 +0200380
Bartlomiej Zolnierkiewicz5985e6a2008-07-15 21:21:55 +0200381 if (pc->c[0] == REQUEST_SENSE) {
382 if (uptodate)
383 idetape_analyze_error(drive, pc->buf);
384 else
385 printk(KERN_ERR "ide-tape: Error in REQUEST SENSE "
386 "itself - Aborting request!\n");
387 } else if (pc->c[0] == READ_6 || pc->c[0] == WRITE_6) {
Bartlomiej Zolnierkiewicz5985e6a2008-07-15 21:21:55 +0200388 int blocks = pc->xferred / tape->blk_size;
389
390 tape->avg_size += blocks * tape->blk_size;
391
392 if (time_after_eq(jiffies, tape->avg_time + HZ)) {
393 tape->avg_speed = tape->avg_size * HZ /
394 (jiffies - tape->avg_time) / 1024;
395 tape->avg_size = 0;
396 tape->avg_time = jiffies;
397 }
398
399 tape->first_frame += blocks;
Tejun Heo21d9c5d2009-04-19 08:46:02 +0900400 rq->data_len -= blocks * tape->blk_size;
Bartlomiej Zolnierkiewicz5985e6a2008-07-15 21:21:55 +0200401
Bartlomiej Zolnierkiewicz313afea2009-03-27 12:46:34 +0100402 if (pc->error) {
403 uptodate = 0;
404 err = pc->error;
405 }
Bartlomiej Zolnierkiewicz5985e6a2008-07-15 21:21:55 +0200406 } else if (pc->c[0] == READ_POSITION && uptodate) {
Bartlomiej Zolnierkiewicz2b9efba2008-10-13 21:39:31 +0200407 u8 *readpos = pc->buf;
Bartlomiej Zolnierkiewicz5985e6a2008-07-15 21:21:55 +0200408
409 debug_log(DBG_SENSE, "BOP - %s\n",
410 (readpos[0] & 0x80) ? "Yes" : "No");
411 debug_log(DBG_SENSE, "EOP - %s\n",
412 (readpos[0] & 0x40) ? "Yes" : "No");
413
414 if (readpos[0] & 0x4) {
415 printk(KERN_INFO "ide-tape: Block location is unknown"
416 "to the tape\n");
Borislav Petkovf2e3ab52008-07-23 19:56:01 +0200417 clear_bit(IDE_AFLAG_ADDRESS_VALID, &drive->atapi_flags);
Bartlomiej Zolnierkiewicz5985e6a2008-07-15 21:21:55 +0200418 uptodate = 0;
Bartlomiej Zolnierkiewicz313afea2009-03-27 12:46:34 +0100419 err = IDE_DRV_ERROR_GENERAL;
Bartlomiej Zolnierkiewicz5985e6a2008-07-15 21:21:55 +0200420 } else {
421 debug_log(DBG_SENSE, "Block Location - %u\n",
Harvey Harrisoncd740ab2008-07-24 22:53:33 +0200422 be32_to_cpup((__be32 *)&readpos[4]));
Bartlomiej Zolnierkiewicz5985e6a2008-07-15 21:21:55 +0200423
424 tape->partition = readpos[1];
Harvey Harrisoncd740ab2008-07-24 22:53:33 +0200425 tape->first_frame = be32_to_cpup((__be32 *)&readpos[4]);
Borislav Petkovf2e3ab52008-07-23 19:56:01 +0200426 set_bit(IDE_AFLAG_ADDRESS_VALID, &drive->atapi_flags);
Bartlomiej Zolnierkiewicz5985e6a2008-07-15 21:21:55 +0200427 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 }
Bartlomiej Zolnierkiewicz5985e6a2008-07-15 21:21:55 +0200429
Bartlomiej Zolnierkiewicz313afea2009-03-27 12:46:34 +0100430 rq->errors = err;
431
Bartlomiej Zolnierkiewicz03a2faa2009-03-27 12:46:36 +0100432 return uptodate;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433}
434
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100436 * Postpone the current request so that ide.c will be able to service requests
Bartlomiej Zolnierkiewiczb65fac32009-01-06 17:20:50 +0100437 * from another device on the same port while we are polling for DSC.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100439static void idetape_postpone_request(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440{
441 idetape_tape_t *tape = drive->driver_data;
442
Borislav Petkov8004a8c2008-02-06 02:57:51 +0100443 debug_log(DBG_PROCS, "Enter %s\n", __func__);
444
Bartlomiej Zolnierkiewiczb65fac32009-01-06 17:20:50 +0100445 tape->postponed_rq = drive->hwif->rq;
446
Borislav Petkov54bb2072008-02-06 02:57:52 +0100447 ide_stall_queue(drive, tape->dsc_poll_freq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448}
449
Bartlomiej Zolnierkiewicz74e63e742008-07-15 21:22:01 +0200450static void ide_tape_handle_dsc(ide_drive_t *drive)
451{
452 idetape_tape_t *tape = drive->driver_data;
453
454 /* Media access command */
455 tape->dsc_polling_start = jiffies;
456 tape->dsc_poll_freq = IDETAPE_DSC_MA_FAST;
457 tape->dsc_timeout = jiffies + IDETAPE_DSC_MA_TIMEOUT;
458 /* Allow ide.c to handle other requests */
459 idetape_postpone_request(drive);
460}
461
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100463 * Packet Command Interface
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 *
Bartlomiej Zolnierkiewicz2b9efba2008-10-13 21:39:31 +0200465 * The current Packet Command is available in drive->pc, and will not change
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100466 * until we finish handling it. Each packet command is associated with a
467 * callback function that will be called when the command is finished.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100469 * The handling will be done in three stages:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 *
Bartlomiej Zolnierkiewiczb788ee92009-03-27 12:46:46 +0100471 * 1. ide_tape_issue_pc will send the packet command to the drive, and will set
Bartlomiej Zolnierkiewiczaa5d2de72008-10-13 21:39:32 +0200472 * the interrupt handler to ide_pc_intr.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 *
Bartlomiej Zolnierkiewiczaa5d2de72008-10-13 21:39:32 +0200474 * 2. On each interrupt, ide_pc_intr will be called. This step will be
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100475 * repeated until the device signals us that no more interrupts will be issued.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100477 * 3. ATAPI Tape media access commands have immediate status with a delayed
478 * process. In case of a successful initiation of a media access packet command,
479 * the DSC bit will be set when the actual execution of the command is finished.
480 * Since the tape drive will not issue an interrupt, we have to poll for this
481 * event. In this case, we define the request as "low priority request" by
482 * setting rq_status to IDETAPE_RQ_POSTPONED, set a timer to poll for DSC and
483 * exit the driver.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100485 * ide.c will then give higher priority to requests which originate from the
486 * other device, until will change rq_status to RQ_ACTIVE.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100488 * 4. When the packet command is finished, it will be checked for errors.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100490 * 5. In case an error was found, we queue a request sense packet command in
491 * front of the request queue and retry the operation up to
492 * IDETAPE_MAX_PC_RETRIES times.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100494 * 6. In case no error was found, or we decided to give up and not to retry
495 * again, the callback function will be called and then we will handle the next
496 * request.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498
Bartlomiej Zolnierkiewiczb788ee92009-03-27 12:46:46 +0100499static ide_startstop_t ide_tape_issue_pc(ide_drive_t *drive,
500 struct ide_cmd *cmd,
501 struct ide_atapi_pc *pc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 idetape_tape_t *tape = drive->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504
Bartlomiej Zolnierkiewicz5e2040fd2009-03-27 12:46:34 +0100505 if (drive->failed_pc == NULL && pc->c[0] != REQUEST_SENSE)
506 drive->failed_pc = pc;
Bartlomiej Zolnierkiewicz2b9efba2008-10-13 21:39:31 +0200507
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 /* Set the current packet command */
Bartlomiej Zolnierkiewicz2b9efba2008-10-13 21:39:31 +0200509 drive->pc = pc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510
511 if (pc->retries > IDETAPE_MAX_PC_RETRIES ||
Borislav Petkov346331f2008-04-18 00:46:26 +0200512 (pc->flags & PC_FLAG_ABORT)) {
Tejun Heob3071d12009-04-19 08:46:02 +0900513 unsigned int done = blk_rq_bytes(drive->hwif->rq);
514
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 /*
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100516 * We will "abort" retrying a packet command in case legitimate
517 * error code was received (crossing a filemark, or end of the
518 * media, for example).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 */
Borislav Petkov346331f2008-04-18 00:46:26 +0200520 if (!(pc->flags & PC_FLAG_ABORT)) {
Borislav Petkov90699ce2008-02-02 19:56:50 +0100521 if (!(pc->c[0] == TEST_UNIT_READY &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 tape->sense_key == 2 && tape->asc == 4 &&
523 (tape->ascq == 1 || tape->ascq == 8))) {
524 printk(KERN_ERR "ide-tape: %s: I/O error, "
525 "pc = %2x, key = %2x, "
526 "asc = %2x, ascq = %2x\n",
527 tape->name, pc->c[0],
528 tape->sense_key, tape->asc,
529 tape->ascq);
530 }
531 /* Giving up */
Bartlomiej Zolnierkiewiczc152cc12009-03-27 12:46:34 +0100532 pc->error = IDE_DRV_ERROR_GENERAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 }
Tejun Heob3071d12009-04-19 08:46:02 +0900534
Bartlomiej Zolnierkiewicz5e2040fd2009-03-27 12:46:34 +0100535 drive->failed_pc = NULL;
Bartlomiej Zolnierkiewiczb14c7212008-10-13 21:39:30 +0200536 drive->pc_callback(drive, 0);
Tejun Heob3071d12009-04-19 08:46:02 +0900537 ide_complete_rq(drive, -EIO, done);
Bartlomiej Zolnierkiewicz92f5daf2008-07-15 21:21:55 +0200538 return ide_stopped;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 }
Borislav Petkov8004a8c2008-02-06 02:57:51 +0100540 debug_log(DBG_SENSE, "Retry #%d, cmd = %02X\n", pc->retries, pc->c[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541
542 pc->retries++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543
Bartlomiej Zolnierkiewiczb788ee92009-03-27 12:46:46 +0100544 return ide_issue_pc(drive, cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545}
546
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100547/* A mode sense command is used to "sense" tape parameters. */
Borislav Petkovd236d742008-04-18 00:46:27 +0200548static void idetape_create_mode_sense_cmd(struct ide_atapi_pc *pc, u8 page_code)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549{
Bartlomiej Zolnierkiewicz7bf74202008-10-10 22:39:37 +0200550 ide_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +0100551 pc->c[0] = MODE_SENSE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 if (page_code != IDETAPE_BLOCK_DESCRIPTOR)
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100553 /* DBD = 1 - Don't return block descriptors */
554 pc->c[1] = 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 pc->c[2] = page_code;
556 /*
557 * Changed pc->c[3] to 0 (255 will at best return unused info).
558 *
559 * For SCSI this byte is defined as subpage instead of high byte
560 * of length and some IDE drives seem to interpret it this way
561 * and return an error when 255 is used.
562 */
563 pc->c[3] = 0;
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100564 /* We will just discard data in that case */
565 pc->c[4] = 255;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566 if (page_code == IDETAPE_BLOCK_DESCRIPTOR)
Borislav Petkovd236d742008-04-18 00:46:27 +0200567 pc->req_xfer = 12;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 else if (page_code == IDETAPE_CAPABILITIES_PAGE)
Borislav Petkovd236d742008-04-18 00:46:27 +0200569 pc->req_xfer = 24;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 else
Borislav Petkovd236d742008-04-18 00:46:27 +0200571 pc->req_xfer = 50;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572}
573
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100574static ide_startstop_t idetape_media_access_finished(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575{
Bartlomiej Zolnierkiewiczb73c7ee2008-07-23 19:55:52 +0200576 ide_hwif_t *hwif = drive->hwif;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 idetape_tape_t *tape = drive->driver_data;
Bartlomiej Zolnierkiewicz2b9efba2008-10-13 21:39:31 +0200578 struct ide_atapi_pc *pc = drive->pc;
Bartlomiej Zolnierkiewicz22c525b2008-01-25 22:17:11 +0100579 u8 stat;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580
Bartlomiej Zolnierkiewicz374e0422008-07-23 19:55:56 +0200581 stat = hwif->tp_ops->read_status(hwif);
Bartlomiej Zolnierkiewiczc47137a2008-02-06 02:57:51 +0100582
Bartlomiej Zolnierkiewicz3a7d2482008-10-10 22:39:21 +0200583 if (stat & ATA_DSC) {
584 if (stat & ATA_ERR) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 /* Error detected */
Borislav Petkov90699ce2008-02-02 19:56:50 +0100586 if (pc->c[0] != TEST_UNIT_READY)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 printk(KERN_ERR "ide-tape: %s: I/O error, ",
588 tape->name);
589 /* Retry operation */
Borislav Petkov6b544fc2009-04-19 07:00:42 +0900590 ide_retry_pc(drive);
Bartlomiej Zolnierkiewicz258ec412008-07-15 21:21:55 +0200591 return ide_stopped;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 }
593 pc->error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 } else {
Bartlomiej Zolnierkiewiczc152cc12009-03-27 12:46:34 +0100595 pc->error = IDE_DRV_ERROR_GENERAL;
Bartlomiej Zolnierkiewicz5e2040fd2009-03-27 12:46:34 +0100596 drive->failed_pc = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 }
Bartlomiej Zolnierkiewiczb14c7212008-10-13 21:39:30 +0200598 drive->pc_callback(drive, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 return ide_stopped;
600}
601
Borislav Petkovcd2abbf2008-07-15 21:22:03 +0200602static void ide_tape_create_rw_cmd(idetape_tape_t *tape,
Borislav Petkov0014c752008-07-23 19:56:00 +0200603 struct ide_atapi_pc *pc, struct request *rq,
604 u8 opcode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605{
Borislav Petkov0014c752008-07-23 19:56:00 +0200606 struct idetape_bh *bh = (struct idetape_bh *)rq->special;
607 unsigned int length = rq->current_nr_sectors;
608
Bartlomiej Zolnierkiewicz7bf74202008-10-10 22:39:37 +0200609 ide_init_pc(pc);
Borislav Petkov860ff5e2008-02-02 19:56:50 +0100610 put_unaligned(cpu_to_be32(length), (unsigned int *) &pc->c[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611 pc->c[1] = 1;
Tejun Heo21d9c5d2009-04-19 08:46:02 +0900612 pc->bh = NULL;
Borislav Petkovd236d742008-04-18 00:46:27 +0200613 pc->buf = NULL;
614 pc->buf_size = length * tape->blk_size;
615 pc->req_xfer = pc->buf_size;
Borislav Petkovf73850a2008-04-27 15:38:33 +0200616 if (pc->req_xfer == tape->buffer_size)
Bartlomiej Zolnierkiewicz5e331092008-07-15 21:21:56 +0200617 pc->flags |= PC_FLAG_DMA_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618
Borislav Petkovcd2abbf2008-07-15 21:22:03 +0200619 if (opcode == READ_6) {
620 pc->c[0] = READ_6;
621 atomic_set(&bh->b_count, 0);
622 } else if (opcode == WRITE_6) {
623 pc->c[0] = WRITE_6;
624 pc->flags |= PC_FLAG_WRITING;
625 pc->b_data = bh->b_data;
626 pc->b_count = atomic_read(&bh->b_count);
627 }
Borislav Petkov0014c752008-07-23 19:56:00 +0200628
629 memcpy(rq->cmd, pc->c, 12);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630}
631
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632static ide_startstop_t idetape_do_request(ide_drive_t *drive,
633 struct request *rq, sector_t block)
634{
Bartlomiej Zolnierkiewiczb73c7ee2008-07-23 19:55:52 +0200635 ide_hwif_t *hwif = drive->hwif;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636 idetape_tape_t *tape = drive->driver_data;
Borislav Petkovd236d742008-04-18 00:46:27 +0200637 struct ide_atapi_pc *pc = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 struct request *postponed_rq = tape->postponed_rq;
Bartlomiej Zolnierkiewiczb788ee92009-03-27 12:46:46 +0100639 struct ide_cmd cmd;
Bartlomiej Zolnierkiewicz22c525b2008-01-25 22:17:11 +0100640 u8 stat;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641
Mark de Wever730616b2008-10-10 22:39:17 +0200642 debug_log(DBG_SENSE, "sector: %llu, nr_sectors: %lu,"
643 " current_nr_sectors: %u\n",
644 (unsigned long long)rq->sector, rq->nr_sectors,
645 rq->current_nr_sectors);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646
Borislav Petkov6b544fc2009-04-19 07:00:42 +0900647 if (!(blk_special_request(rq) || blk_sense_request(rq))) {
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100648 /* We do not support buffer cache originated requests. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 printk(KERN_NOTICE "ide-tape: %s: Unsupported request in "
Jens Axboe4aff5e22006-08-10 08:44:47 +0200650 "request queue (%d)\n", drive->name, rq->cmd_type);
Bartlomiej Zolnierkiewicz89f78b322009-03-27 12:46:43 +0100651 if (blk_fs_request(rq) == 0 && rq->errors == 0)
652 rq->errors = -EIO;
Bartlomiej Zolnierkiewicz130e8862009-03-27 12:46:45 +0100653 ide_complete_rq(drive, -EIO, ide_rq_bytes(rq));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 return ide_stopped;
655 }
656
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100657 /* Retry a failed packet command */
Bartlomiej Zolnierkiewicz5e2040fd2009-03-27 12:46:34 +0100658 if (drive->failed_pc && drive->pc->c[0] == REQUEST_SENSE) {
659 pc = drive->failed_pc;
Bartlomiej Zolnierkiewicz28c72142008-07-15 21:21:59 +0200660 goto out;
661 }
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100662
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 if (postponed_rq != NULL)
664 if (rq != postponed_rq) {
665 printk(KERN_ERR "ide-tape: ide-tape.c bug - "
666 "Two DSC requests were queued\n");
Bartlomiej Zolnierkiewicz313afea2009-03-27 12:46:34 +0100667 drive->failed_pc = NULL;
Bartlomiej Zolnierkiewicz6902a532009-03-27 12:46:43 +0100668 rq->errors = 0;
Bartlomiej Zolnierkiewiczf974b192009-03-27 12:46:44 +0100669 ide_complete_rq(drive, 0, blk_rq_bytes(rq));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670 return ide_stopped;
671 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672
673 tape->postponed_rq = NULL;
674
675 /*
676 * If the tape is still busy, postpone our request and service
677 * the other device meanwhile.
678 */
Bartlomiej Zolnierkiewicz374e0422008-07-23 19:55:56 +0200679 stat = hwif->tp_ops->read_status(hwif);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +0200681 if ((drive->dev_flags & IDE_DFLAG_DSC_OVERLAP) == 0 &&
682 (rq->cmd[13] & REQ_IDETAPE_PC2) == 0)
Borislav Petkovf2e3ab52008-07-23 19:56:01 +0200683 set_bit(IDE_AFLAG_IGNORE_DSC, &drive->atapi_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +0200685 if (drive->dev_flags & IDE_DFLAG_POST_RESET) {
Borislav Petkovf2e3ab52008-07-23 19:56:01 +0200686 set_bit(IDE_AFLAG_IGNORE_DSC, &drive->atapi_flags);
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +0200687 drive->dev_flags &= ~IDE_DFLAG_POST_RESET;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 }
689
Borislav Petkovf2e3ab52008-07-23 19:56:01 +0200690 if (!test_and_clear_bit(IDE_AFLAG_IGNORE_DSC, &drive->atapi_flags) &&
Bartlomiej Zolnierkiewicz3a7d2482008-10-10 22:39:21 +0200691 (stat & ATA_DSC) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692 if (postponed_rq == NULL) {
693 tape->dsc_polling_start = jiffies;
Borislav Petkov54bb2072008-02-06 02:57:52 +0100694 tape->dsc_poll_freq = tape->best_dsc_rw_freq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 tape->dsc_timeout = jiffies + IDETAPE_DSC_RW_TIMEOUT;
696 } else if (time_after(jiffies, tape->dsc_timeout)) {
697 printk(KERN_ERR "ide-tape: %s: DSC timeout\n",
698 tape->name);
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 } else {
703 return ide_do_reset(drive);
704 }
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100705 } else if (time_after(jiffies,
706 tape->dsc_polling_start +
707 IDETAPE_DSC_MA_THRESHOLD))
Borislav Petkov54bb2072008-02-06 02:57:52 +0100708 tape->dsc_poll_freq = IDETAPE_DSC_MA_SLOW;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709 idetape_postpone_request(drive);
710 return ide_stopped;
711 }
Borislav Petkov83dd5732008-07-23 19:56:00 +0200712 if (rq->cmd[13] & REQ_IDETAPE_READ) {
Bartlomiej Zolnierkiewicz2e8a6f82008-10-10 22:39:36 +0200713 pc = &tape->queued_pc;
Borislav Petkov0014c752008-07-23 19:56:00 +0200714 ide_tape_create_rw_cmd(tape, pc, rq, READ_6);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 goto out;
716 }
Borislav Petkov83dd5732008-07-23 19:56:00 +0200717 if (rq->cmd[13] & REQ_IDETAPE_WRITE) {
Bartlomiej Zolnierkiewicz2e8a6f82008-10-10 22:39:36 +0200718 pc = &tape->queued_pc;
Borislav Petkov0014c752008-07-23 19:56:00 +0200719 ide_tape_create_rw_cmd(tape, pc, rq, WRITE_6);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 goto out;
721 }
Borislav Petkov83dd5732008-07-23 19:56:00 +0200722 if (rq->cmd[13] & REQ_IDETAPE_PC1) {
Tejun Heoc267cc12009-04-19 07:00:42 +0900723 pc = (struct ide_atapi_pc *)rq->special;
Borislav Petkov83dd5732008-07-23 19:56:00 +0200724 rq->cmd[13] &= ~(REQ_IDETAPE_PC1);
725 rq->cmd[13] |= REQ_IDETAPE_PC2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 goto out;
727 }
Borislav Petkov83dd5732008-07-23 19:56:00 +0200728 if (rq->cmd[13] & REQ_IDETAPE_PC2) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729 idetape_media_access_finished(drive);
730 return ide_stopped;
731 }
732 BUG();
Bartlomiej Zolnierkiewicz28c72142008-07-15 21:21:59 +0200733
Borislav Petkovf2e3ab52008-07-23 19:56:01 +0200734out:
Borislav Petkov6b544fc2009-04-19 07:00:42 +0900735 /* prepare sense request for this command */
736 ide_prep_sense(drive, rq);
737
Bartlomiej Zolnierkiewiczb788ee92009-03-27 12:46:46 +0100738 memset(&cmd, 0, sizeof(cmd));
739
740 if (rq_data_dir(rq))
741 cmd.tf_flags |= IDE_TFLAG_WRITE;
742
743 cmd.rq = rq;
744
Tejun Heo5c4be572009-04-19 07:00:42 +0900745 ide_init_sg_cmd(&cmd, pc->req_xfer);
746 ide_map_sg(drive, &cmd);
747
Bartlomiej Zolnierkiewiczb788ee92009-03-27 12:46:46 +0100748 return ide_tape_issue_pc(drive, &cmd, pc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749}
750
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751/*
Tejun Heo35ab8d32009-04-19 08:46:02 +0900752 * It returns a pointer to the newly allocated buffer, or NULL in case
753 * of failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754 */
Borislav Petkov077e3bd2008-04-27 15:38:34 +0200755static struct idetape_bh *ide_tape_kmalloc_buffer(idetape_tape_t *tape,
Tejun Heo35ab8d32009-04-19 08:46:02 +0900756 int full)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757{
Tejun Heo35ab8d32009-04-19 08:46:02 +0900758 struct idetape_bh *bh;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759
Tejun Heo35ab8d32009-04-19 08:46:02 +0900760 bh = kmalloc(sizeof(struct idetape_bh), GFP_KERNEL);
761 if (!bh)
762 return NULL;
Borislav Petkov41aa1702008-04-27 15:38:32 +0200763
Tejun Heo35ab8d32009-04-19 08:46:02 +0900764 bh->b_data = kmalloc(tape->buffer_size, GFP_KERNEL);
765 if (!bh->b_data) {
766 kfree(bh);
767 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 }
Borislav Petkov41aa1702008-04-27 15:38:32 +0200769
Tejun Heo35ab8d32009-04-19 08:46:02 +0900770 bh->b_size = tape->buffer_size;
771 atomic_set(&bh->b_count, full ? bh->b_size : 0);
772
773 return bh;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774}
775
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100776static int idetape_copy_stage_from_user(idetape_tape_t *tape,
Borislav Petkov8646c882008-04-27 15:38:26 +0200777 const char __user *buf, int n)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778{
779 struct idetape_bh *bh = tape->bh;
Daniel Walkerdcd96372006-06-25 05:47:37 -0700780 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781
Tejun Heo35ab8d32009-04-19 08:46:02 +0900782 if (n) {
783 if (bh == NULL || n > bh->b_size - atomic_read(&bh->b_count)) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100784 printk(KERN_ERR "ide-tape: bh == NULL in %s\n",
785 __func__);
Daniel Walkerdcd96372006-06-25 05:47:37 -0700786 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787 }
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100788 if (copy_from_user(bh->b_data + atomic_read(&bh->b_count), buf,
Tejun Heo35ab8d32009-04-19 08:46:02 +0900789 n))
Daniel Walkerdcd96372006-06-25 05:47:37 -0700790 ret = 1;
Tejun Heo35ab8d32009-04-19 08:46:02 +0900791 atomic_add(n, &bh->b_count);
792 if (atomic_read(&bh->b_count) == bh->b_size)
793 tape->bh = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794 }
Tejun Heo35ab8d32009-04-19 08:46:02 +0900795
Daniel Walkerdcd96372006-06-25 05:47:37 -0700796 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797}
798
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100799static int idetape_copy_stage_to_user(idetape_tape_t *tape, char __user *buf,
Borislav Petkov99d74e62008-04-27 15:38:25 +0200800 int n)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801{
802 struct idetape_bh *bh = tape->bh;
Daniel Walkerdcd96372006-06-25 05:47:37 -0700803 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804
Tejun Heo35ab8d32009-04-19 08:46:02 +0900805 if (n) {
806 if (bh == NULL || n > tape->b_count) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100807 printk(KERN_ERR "ide-tape: bh == NULL in %s\n",
808 __func__);
Daniel Walkerdcd96372006-06-25 05:47:37 -0700809 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810 }
Tejun Heo35ab8d32009-04-19 08:46:02 +0900811 if (copy_to_user(buf, tape->b_data, n))
Daniel Walkerdcd96372006-06-25 05:47:37 -0700812 ret = 1;
Tejun Heo35ab8d32009-04-19 08:46:02 +0900813 tape->b_data += n;
814 tape->b_count -= n;
815 if (!tape->b_count)
816 tape->bh = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817 }
Daniel Walkerdcd96372006-06-25 05:47:37 -0700818 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819}
820
Borislav Petkov077e3bd2008-04-27 15:38:34 +0200821static void idetape_init_merge_buffer(idetape_tape_t *tape)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822{
Borislav Petkov077e3bd2008-04-27 15:38:34 +0200823 struct idetape_bh *bh = tape->merge_bh;
824 tape->bh = tape->merge_bh;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100825
Borislav Petkov54abf372008-02-06 02:57:52 +0100826 if (tape->chrdev_dir == IDETAPE_DIR_WRITE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827 atomic_set(&bh->b_count, 0);
828 else {
829 tape->b_data = bh->b_data;
830 tape->b_count = atomic_read(&bh->b_count);
831 }
832}
833
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100835 * Write a filemark if write_filemark=1. Flush the device buffers without
836 * writing a filemark otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837 */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100838static void idetape_create_write_filemark_cmd(ide_drive_t *drive,
Borislav Petkovd236d742008-04-18 00:46:27 +0200839 struct ide_atapi_pc *pc, int write_filemark)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840{
Bartlomiej Zolnierkiewicz7bf74202008-10-10 22:39:37 +0200841 ide_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +0100842 pc->c[0] = WRITE_FILEMARKS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843 pc->c[4] = write_filemark;
Borislav Petkov346331f2008-04-18 00:46:26 +0200844 pc->flags |= PC_FLAG_WAIT_FOR_DSC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845}
846
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847static int idetape_wait_ready(ide_drive_t *drive, unsigned long timeout)
848{
849 idetape_tape_t *tape = drive->driver_data;
Bartlomiej Zolnierkiewicz2ac07d92008-10-10 22:39:38 +0200850 struct gendisk *disk = tape->disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851 int load_attempted = 0;
852
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100853 /* Wait for the tape to become ready */
Borislav Petkovf2e3ab52008-07-23 19:56:01 +0200854 set_bit(IDE_AFLAG_MEDIUM_PRESENT, &drive->atapi_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855 timeout += jiffies;
856 while (time_before(jiffies, timeout)) {
Bartlomiej Zolnierkiewiczde699ad2008-10-10 22:39:39 +0200857 if (ide_do_test_unit_ready(drive, disk) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858 return 0;
859 if ((tape->sense_key == 2 && tape->asc == 4 && tape->ascq == 2)
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100860 || (tape->asc == 0x3A)) {
861 /* no media */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862 if (load_attempted)
863 return -ENOMEDIUM;
Bartlomiej Zolnierkiewicz0c8a6c72008-10-10 22:39:39 +0200864 ide_do_start_stop(drive, disk, IDETAPE_LU_LOAD_MASK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865 load_attempted = 1;
866 /* not about to be ready */
867 } else if (!(tape->sense_key == 2 && tape->asc == 4 &&
868 (tape->ascq == 1 || tape->ascq == 8)))
869 return -EIO;
Nishanth Aravamudan80ce45f2005-09-10 00:27:08 -0700870 msleep(100);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871 }
872 return -EIO;
873}
874
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100875static int idetape_flush_tape_buffers(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876{
Bartlomiej Zolnierkiewicz2ac07d92008-10-10 22:39:38 +0200877 struct ide_tape_obj *tape = drive->driver_data;
Borislav Petkovd236d742008-04-18 00:46:27 +0200878 struct ide_atapi_pc pc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879 int rc;
880
881 idetape_create_write_filemark_cmd(drive, &pc, 0);
Bartlomiej Zolnierkiewicz2ac07d92008-10-10 22:39:38 +0200882 rc = ide_queue_pc_tail(drive, tape->disk, &pc);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100883 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884 return rc;
885 idetape_wait_ready(drive, 60 * 5 * HZ);
886 return 0;
887}
888
Borislav Petkovd236d742008-04-18 00:46:27 +0200889static void idetape_create_read_position_cmd(struct ide_atapi_pc *pc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890{
Bartlomiej Zolnierkiewicz7bf74202008-10-10 22:39:37 +0200891 ide_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +0100892 pc->c[0] = READ_POSITION;
Borislav Petkovd236d742008-04-18 00:46:27 +0200893 pc->req_xfer = 20;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894}
895
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100896static int idetape_read_position(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897{
898 idetape_tape_t *tape = drive->driver_data;
Borislav Petkovd236d742008-04-18 00:46:27 +0200899 struct ide_atapi_pc pc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900 int position;
901
Borislav Petkov8004a8c2008-02-06 02:57:51 +0100902 debug_log(DBG_PROCS, "Enter %s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903
904 idetape_create_read_position_cmd(&pc);
Bartlomiej Zolnierkiewicz2ac07d92008-10-10 22:39:38 +0200905 if (ide_queue_pc_tail(drive, tape->disk, &pc))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906 return -1;
Borislav Petkov54bb2072008-02-06 02:57:52 +0100907 position = tape->first_frame;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908 return position;
909}
910
Borislav Petkovd236d742008-04-18 00:46:27 +0200911static void idetape_create_locate_cmd(ide_drive_t *drive,
912 struct ide_atapi_pc *pc,
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100913 unsigned int block, u8 partition, int skip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914{
Bartlomiej Zolnierkiewicz7bf74202008-10-10 22:39:37 +0200915 ide_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +0100916 pc->c[0] = POSITION_TO_ELEMENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917 pc->c[1] = 2;
Borislav Petkov860ff5e2008-02-02 19:56:50 +0100918 put_unaligned(cpu_to_be32(block), (unsigned int *) &pc->c[3]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919 pc->c[8] = partition;
Borislav Petkov346331f2008-04-18 00:46:26 +0200920 pc->flags |= PC_FLAG_WAIT_FOR_DSC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921}
922
Borislav Petkovec0fdb02008-04-27 15:38:34 +0200923static void __ide_tape_discard_merge_buffer(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924{
925 idetape_tape_t *tape = drive->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926
Borislav Petkov54abf372008-02-06 02:57:52 +0100927 if (tape->chrdev_dir != IDETAPE_DIR_READ)
Borislav Petkov97986302008-04-27 15:38:34 +0200928 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929
Borislav Petkovf2e3ab52008-07-23 19:56:01 +0200930 clear_bit(IDE_AFLAG_FILEMARK, &drive->atapi_flags);
Borislav Petkov01a63aeb2008-04-27 15:38:34 +0200931 tape->merge_bh_size = 0;
Borislav Petkov077e3bd2008-04-27 15:38:34 +0200932 if (tape->merge_bh != NULL) {
933 ide_tape_kfree_buffer(tape);
934 tape->merge_bh = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935 }
936
Borislav Petkov54abf372008-02-06 02:57:52 +0100937 tape->chrdev_dir = IDETAPE_DIR_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938}
939
940/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100941 * Position the tape to the requested block using the LOCATE packet command.
942 * A READ POSITION command is then issued to check where we are positioned. Like
943 * all higher level operations, we queue the commands at the tail of the request
944 * queue and wait for their completion.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945 */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100946static int idetape_position_tape(ide_drive_t *drive, unsigned int block,
947 u8 partition, int skip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948{
949 idetape_tape_t *tape = drive->driver_data;
Bartlomiej Zolnierkiewicz2ac07d92008-10-10 22:39:38 +0200950 struct gendisk *disk = tape->disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951 int retval;
Borislav Petkovd236d742008-04-18 00:46:27 +0200952 struct ide_atapi_pc pc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953
Borislav Petkov54abf372008-02-06 02:57:52 +0100954 if (tape->chrdev_dir == IDETAPE_DIR_READ)
Borislav Petkovec0fdb02008-04-27 15:38:34 +0200955 __ide_tape_discard_merge_buffer(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956 idetape_wait_ready(drive, 60 * 5 * HZ);
957 idetape_create_locate_cmd(drive, &pc, block, partition, skip);
Bartlomiej Zolnierkiewicz2ac07d92008-10-10 22:39:38 +0200958 retval = ide_queue_pc_tail(drive, disk, &pc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959 if (retval)
960 return (retval);
961
962 idetape_create_read_position_cmd(&pc);
Bartlomiej Zolnierkiewicz2ac07d92008-10-10 22:39:38 +0200963 return ide_queue_pc_tail(drive, disk, &pc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964}
965
Borislav Petkovec0fdb02008-04-27 15:38:34 +0200966static void ide_tape_discard_merge_buffer(ide_drive_t *drive,
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100967 int restore_position)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968{
969 idetape_tape_t *tape = drive->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970 int seek, position;
971
Borislav Petkovec0fdb02008-04-27 15:38:34 +0200972 __ide_tape_discard_merge_buffer(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973 if (restore_position) {
974 position = idetape_read_position(drive);
Borislav Petkov97986302008-04-27 15:38:34 +0200975 seek = position > 0 ? position : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976 if (idetape_position_tape(drive, seek, 0, 0)) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100977 printk(KERN_INFO "ide-tape: %s: position_tape failed in"
Borislav Petkovec0fdb02008-04-27 15:38:34 +0200978 " %s\n", tape->name, __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979 return;
980 }
981 }
982}
983
984/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100985 * Generate a read/write request for the block device interface and wait for it
986 * to be serviced.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987 */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100988static int idetape_queue_rw_tail(ide_drive_t *drive, int cmd, int blocks,
989 struct idetape_bh *bh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990{
991 idetape_tape_t *tape = drive->driver_data;
Tejun Heo21d9c5d2009-04-19 08:46:02 +0900992 size_t size = blocks * tape->blk_size;
FUJITA Tomonori64ea1b42008-07-15 21:21:43 +0200993 struct request *rq;
Tejun Heo21d9c5d2009-04-19 08:46:02 +0900994 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995
Borislav Petkov8004a8c2008-02-06 02:57:51 +0100996 debug_log(DBG_SENSE, "%s: cmd=%d\n", __func__, cmd);
Tejun Heo21d9c5d2009-04-19 08:46:02 +0900997 BUG_ON(cmd != REQ_IDETAPE_READ && cmd != REQ_IDETAPE_WRITE);
Borislav Petkov8004a8c2008-02-06 02:57:51 +0100998
FUJITA Tomonori64ea1b42008-07-15 21:21:43 +0200999 rq = blk_get_request(drive->queue, READ, __GFP_WAIT);
1000 rq->cmd_type = REQ_TYPE_SPECIAL;
Borislav Petkov83dd5732008-07-23 19:56:00 +02001001 rq->cmd[13] = cmd;
FUJITA Tomonori64ea1b42008-07-15 21:21:43 +02001002 rq->rq_disk = tape->disk;
1003 rq->special = (void *)bh;
1004 rq->sector = tape->first_frame;
Tejun Heo21d9c5d2009-04-19 08:46:02 +09001005
1006 if (size) {
1007 ret = blk_rq_map_kern(drive->queue, rq, bh->b_data, size,
1008 __GFP_WAIT);
1009 if (ret)
1010 goto out_put;
1011 }
1012
FUJITA Tomonori64ea1b42008-07-15 21:21:43 +02001013 blk_execute_rq(drive->queue, tape->disk, rq, 0);
1014
Tejun Heo21d9c5d2009-04-19 08:46:02 +09001015 /* calculate the number of transferred bytes and update bh */
1016 size -= rq->data_len;
1017 if (cmd == REQ_IDETAPE_READ)
1018 atomic_add(size, &bh->b_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019
Tejun Heo21d9c5d2009-04-19 08:46:02 +09001020 ret = size;
1021 if (rq->errors == IDE_DRV_ERROR_GENERAL)
1022 ret = -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023
Borislav Petkov077e3bd2008-04-27 15:38:34 +02001024 if (tape->merge_bh)
1025 idetape_init_merge_buffer(tape);
Tejun Heo21d9c5d2009-04-19 08:46:02 +09001026out_put:
1027 blk_put_request(rq);
FUJITA Tomonori64ea1b42008-07-15 21:21:43 +02001028 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029}
1030
Borislav Petkovd236d742008-04-18 00:46:27 +02001031static void idetape_create_inquiry_cmd(struct ide_atapi_pc *pc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032{
Bartlomiej Zolnierkiewicz7bf74202008-10-10 22:39:37 +02001033 ide_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +01001034 pc->c[0] = INQUIRY;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001035 pc->c[4] = 254;
Borislav Petkovd236d742008-04-18 00:46:27 +02001036 pc->req_xfer = 254;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037}
1038
Borislav Petkovd236d742008-04-18 00:46:27 +02001039static void idetape_create_rewind_cmd(ide_drive_t *drive,
1040 struct ide_atapi_pc *pc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041{
Bartlomiej Zolnierkiewicz7bf74202008-10-10 22:39:37 +02001042 ide_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +01001043 pc->c[0] = REZERO_UNIT;
Borislav Petkov346331f2008-04-18 00:46:26 +02001044 pc->flags |= PC_FLAG_WAIT_FOR_DSC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045}
1046
Borislav Petkovd236d742008-04-18 00:46:27 +02001047static void idetape_create_erase_cmd(struct ide_atapi_pc *pc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048{
Bartlomiej Zolnierkiewicz7bf74202008-10-10 22:39:37 +02001049 ide_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +01001050 pc->c[0] = ERASE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051 pc->c[1] = 1;
Borislav Petkov346331f2008-04-18 00:46:26 +02001052 pc->flags |= PC_FLAG_WAIT_FOR_DSC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053}
1054
Borislav Petkovd236d742008-04-18 00:46:27 +02001055static void idetape_create_space_cmd(struct ide_atapi_pc *pc, int count, u8 cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056{
Bartlomiej Zolnierkiewicz7bf74202008-10-10 22:39:37 +02001057 ide_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +01001058 pc->c[0] = SPACE;
Borislav Petkov860ff5e2008-02-02 19:56:50 +01001059 put_unaligned(cpu_to_be32(count), (unsigned int *) &pc->c[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060 pc->c[1] = cmd;
Borislav Petkov346331f2008-04-18 00:46:26 +02001061 pc->flags |= PC_FLAG_WAIT_FOR_DSC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062}
1063
Borislav Petkov97c566c2008-04-27 15:38:25 +02001064/* Queue up a character device originated write request. */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001065static int idetape_add_chrdev_write_request(ide_drive_t *drive, int blocks)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066{
1067 idetape_tape_t *tape = drive->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001069 debug_log(DBG_CHRDEV, "Enter %s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070
Borislav Petkov0aa4b012008-04-27 15:38:27 +02001071 return idetape_queue_rw_tail(drive, REQ_IDETAPE_WRITE,
Borislav Petkov077e3bd2008-04-27 15:38:34 +02001072 blocks, tape->merge_bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073}
1074
Borislav Petkovd9df9372008-04-27 15:38:34 +02001075static void ide_tape_flush_merge_buffer(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076{
1077 idetape_tape_t *tape = drive->driver_data;
Tejun Heo35ab8d32009-04-19 08:46:02 +09001078 int blocks;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079 struct idetape_bh *bh;
Borislav Petkov55a5d292008-02-02 19:56:49 +01001080
Borislav Petkov54abf372008-02-06 02:57:52 +01001081 if (tape->chrdev_dir != IDETAPE_DIR_WRITE) {
Borislav Petkov077e3bd2008-04-27 15:38:34 +02001082 printk(KERN_ERR "ide-tape: bug: Trying to empty merge buffer"
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001083 " but we are not writing.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084 return;
1085 }
Borislav Petkov01a63aeb2008-04-27 15:38:34 +02001086 if (tape->merge_bh_size > tape->buffer_size) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087 printk(KERN_ERR "ide-tape: bug: merge_buffer too big\n");
Borislav Petkov01a63aeb2008-04-27 15:38:34 +02001088 tape->merge_bh_size = tape->buffer_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089 }
Borislav Petkov01a63aeb2008-04-27 15:38:34 +02001090 if (tape->merge_bh_size) {
1091 blocks = tape->merge_bh_size / tape->blk_size;
1092 if (tape->merge_bh_size % tape->blk_size) {
Tejun Heo35ab8d32009-04-19 08:46:02 +09001093 unsigned int i = tape->blk_size -
1094 tape->merge_bh_size % tape->blk_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095 blocks++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096 bh = tape->bh;
Tejun Heo35ab8d32009-04-19 08:46:02 +09001097 if (bh) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001098 memset(bh->b_data + atomic_read(&bh->b_count),
Tejun Heo35ab8d32009-04-19 08:46:02 +09001099 0, i);
1100 atomic_add(i, &bh->b_count);
1101 } else
1102 printk(KERN_INFO "ide-tape: bug, bh NULL\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103 }
1104 (void) idetape_add_chrdev_write_request(drive, blocks);
Borislav Petkov01a63aeb2008-04-27 15:38:34 +02001105 tape->merge_bh_size = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106 }
Borislav Petkov077e3bd2008-04-27 15:38:34 +02001107 if (tape->merge_bh != NULL) {
1108 ide_tape_kfree_buffer(tape);
1109 tape->merge_bh = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110 }
Borislav Petkov54abf372008-02-06 02:57:52 +01001111 tape->chrdev_dir = IDETAPE_DIR_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112}
1113
Borislav Petkov83042b22008-04-27 15:38:27 +02001114static int idetape_init_read(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115{
1116 idetape_tape_t *tape = drive->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117 int bytes_read;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118
1119 /* Initialize read operation */
Borislav Petkov54abf372008-02-06 02:57:52 +01001120 if (tape->chrdev_dir != IDETAPE_DIR_READ) {
1121 if (tape->chrdev_dir == IDETAPE_DIR_WRITE) {
Borislav Petkovd9df9372008-04-27 15:38:34 +02001122 ide_tape_flush_merge_buffer(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123 idetape_flush_tape_buffers(drive);
1124 }
Borislav Petkov077e3bd2008-04-27 15:38:34 +02001125 if (tape->merge_bh || tape->merge_bh_size) {
Borislav Petkov01a63aeb2008-04-27 15:38:34 +02001126 printk(KERN_ERR "ide-tape: merge_bh_size should be"
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001127 " 0 now\n");
Borislav Petkov01a63aeb2008-04-27 15:38:34 +02001128 tape->merge_bh_size = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129 }
Tejun Heo35ab8d32009-04-19 08:46:02 +09001130 tape->merge_bh = ide_tape_kmalloc_buffer(tape, 0);
Borislav Petkov077e3bd2008-04-27 15:38:34 +02001131 if (!tape->merge_bh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132 return -ENOMEM;
Borislav Petkov54abf372008-02-06 02:57:52 +01001133 tape->chrdev_dir = IDETAPE_DIR_READ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134
1135 /*
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001136 * Issue a read 0 command to ensure that DSC handshake is
1137 * switched from completion mode to buffer available mode.
1138 * No point in issuing this if DSC overlap isn't supported, some
1139 * drives (Seagate STT3401A) will return an error.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140 */
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +02001141 if (drive->dev_flags & IDE_DFLAG_DSC_OVERLAP) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001142 bytes_read = idetape_queue_rw_tail(drive,
1143 REQ_IDETAPE_READ, 0,
Borislav Petkov077e3bd2008-04-27 15:38:34 +02001144 tape->merge_bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145 if (bytes_read < 0) {
Borislav Petkov077e3bd2008-04-27 15:38:34 +02001146 ide_tape_kfree_buffer(tape);
1147 tape->merge_bh = NULL;
Borislav Petkov54abf372008-02-06 02:57:52 +01001148 tape->chrdev_dir = IDETAPE_DIR_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149 return bytes_read;
1150 }
1151 }
1152 }
Borislav Petkov5e69bd92008-04-27 15:38:25 +02001153
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154 return 0;
1155}
1156
Borislav Petkov5bd50dc2008-04-27 15:38:28 +02001157/* called from idetape_chrdev_read() to service a chrdev read request. */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001158static int idetape_add_chrdev_read_request(ide_drive_t *drive, int blocks)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159{
1160 idetape_tape_t *tape = drive->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001162 debug_log(DBG_PROCS, "Enter %s, %d blocks\n", __func__, blocks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001164 /* If we are at a filemark, return a read length of 0 */
Borislav Petkovf2e3ab52008-07-23 19:56:01 +02001165 if (test_bit(IDE_AFLAG_FILEMARK, &drive->atapi_flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166 return 0;
1167
Borislav Petkov83042b22008-04-27 15:38:27 +02001168 idetape_init_read(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169
Borislav Petkov5e69bd92008-04-27 15:38:25 +02001170 return idetape_queue_rw_tail(drive, REQ_IDETAPE_READ, blocks,
Borislav Petkov077e3bd2008-04-27 15:38:34 +02001171 tape->merge_bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172}
1173
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001174static void idetape_pad_zeros(ide_drive_t *drive, int bcount)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175{
1176 idetape_tape_t *tape = drive->driver_data;
Tejun Heo35ab8d32009-04-19 08:46:02 +09001177 struct idetape_bh *bh = tape->merge_bh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178 int blocks;
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001179
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180 while (bcount) {
1181 unsigned int count;
1182
Borislav Petkovf73850a2008-04-27 15:38:33 +02001183 count = min(tape->buffer_size, bcount);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184 bcount -= count;
Borislav Petkov54bb2072008-02-06 02:57:52 +01001185 blocks = count / tape->blk_size;
Tejun Heo35ab8d32009-04-19 08:46:02 +09001186 atomic_set(&bh->b_count, count);
1187 memset(bh->b_data, 0, atomic_read(&bh->b_count));
1188
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001189 idetape_queue_rw_tail(drive, REQ_IDETAPE_WRITE, blocks,
Borislav Petkov077e3bd2008-04-27 15:38:34 +02001190 tape->merge_bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191 }
1192}
1193
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001195 * Rewinds the tape to the Beginning Of the current Partition (BOP). We
1196 * currently support only one partition.
1197 */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001198static int idetape_rewind_tape(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199{
Bartlomiej Zolnierkiewicz2ac07d92008-10-10 22:39:38 +02001200 struct ide_tape_obj *tape = drive->driver_data;
1201 struct gendisk *disk = tape->disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202 int retval;
Borislav Petkovd236d742008-04-18 00:46:27 +02001203 struct ide_atapi_pc pc;
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001204
1205 debug_log(DBG_SENSE, "Enter %s\n", __func__);
1206
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207 idetape_create_rewind_cmd(drive, &pc);
Bartlomiej Zolnierkiewicz2ac07d92008-10-10 22:39:38 +02001208 retval = ide_queue_pc_tail(drive, disk, &pc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209 if (retval)
1210 return retval;
1211
1212 idetape_create_read_position_cmd(&pc);
Bartlomiej Zolnierkiewicz2ac07d92008-10-10 22:39:38 +02001213 retval = ide_queue_pc_tail(drive, disk, &pc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214 if (retval)
1215 return retval;
1216 return 0;
1217}
1218
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001219/* mtio.h compatible commands should be issued to the chrdev interface. */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001220static int idetape_blkdev_ioctl(ide_drive_t *drive, unsigned int cmd,
1221 unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222{
1223 idetape_tape_t *tape = drive->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224 void __user *argp = (void __user *)arg;
1225
Borislav Petkovd59823f2008-02-02 19:56:51 +01001226 struct idetape_config {
1227 int dsc_rw_frequency;
1228 int dsc_media_access_frequency;
1229 int nr_stages;
1230 } config;
1231
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001232 debug_log(DBG_PROCS, "Enter %s\n", __func__);
1233
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234 switch (cmd) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001235 case 0x0340:
1236 if (copy_from_user(&config, argp, sizeof(config)))
1237 return -EFAULT;
1238 tape->best_dsc_rw_freq = config.dsc_rw_frequency;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001239 break;
1240 case 0x0350:
1241 config.dsc_rw_frequency = (int) tape->best_dsc_rw_freq;
Borislav Petkov83042b22008-04-27 15:38:27 +02001242 config.nr_stages = 1;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001243 if (copy_to_user(argp, &config, sizeof(config)))
1244 return -EFAULT;
1245 break;
1246 default:
1247 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248 }
1249 return 0;
1250}
1251
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001252static int idetape_space_over_filemarks(ide_drive_t *drive, short mt_op,
1253 int mt_count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254{
1255 idetape_tape_t *tape = drive->driver_data;
Bartlomiej Zolnierkiewicz2ac07d92008-10-10 22:39:38 +02001256 struct gendisk *disk = tape->disk;
Borislav Petkovd236d742008-04-18 00:46:27 +02001257 struct ide_atapi_pc pc;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001258 int retval, count = 0;
Borislav Petkovb6422012008-02-02 19:56:49 +01001259 int sprev = !!(tape->caps[4] & 0x20);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260
1261 if (mt_count == 0)
1262 return 0;
1263 if (MTBSF == mt_op || MTBSFM == mt_op) {
Borislav Petkovb6422012008-02-02 19:56:49 +01001264 if (!sprev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265 return -EIO;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001266 mt_count = -mt_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267 }
1268
Borislav Petkov54abf372008-02-06 02:57:52 +01001269 if (tape->chrdev_dir == IDETAPE_DIR_READ) {
Borislav Petkov01a63aeb2008-04-27 15:38:34 +02001270 tape->merge_bh_size = 0;
Borislav Petkovf2e3ab52008-07-23 19:56:01 +02001271 if (test_and_clear_bit(IDE_AFLAG_FILEMARK, &drive->atapi_flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272 ++count;
Borislav Petkovec0fdb02008-04-27 15:38:34 +02001273 ide_tape_discard_merge_buffer(drive, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274 }
1275
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276 switch (mt_op) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001277 case MTFSF:
1278 case MTBSF:
1279 idetape_create_space_cmd(&pc, mt_count - count,
1280 IDETAPE_SPACE_OVER_FILEMARK);
Bartlomiej Zolnierkiewicz2ac07d92008-10-10 22:39:38 +02001281 return ide_queue_pc_tail(drive, disk, &pc);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001282 case MTFSFM:
1283 case MTBSFM:
1284 if (!sprev)
1285 return -EIO;
1286 retval = idetape_space_over_filemarks(drive, MTFSF,
1287 mt_count - count);
1288 if (retval)
1289 return retval;
1290 count = (MTBSFM == mt_op ? 1 : -1);
1291 return idetape_space_over_filemarks(drive, MTFSF, count);
1292 default:
1293 printk(KERN_ERR "ide-tape: MTIO operation %d not supported\n",
1294 mt_op);
1295 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296 }
1297}
1298
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001300 * Our character device read / write functions.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001302 * The tape is optimized to maximize throughput when it is transferring an
1303 * integral number of the "continuous transfer limit", which is a parameter of
1304 * the specific tape (26kB on my particular tape, 32kB for Onstream).
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001306 * As of version 1.3 of the driver, the character device provides an abstract
1307 * continuous view of the media - any mix of block sizes (even 1 byte) on the
1308 * same backup/restore procedure is supported. The driver will internally
1309 * convert the requests to the recommended transfer unit, so that an unmatch
1310 * between the user's block size to the recommended size will only result in a
1311 * (slightly) increased driver overhead, but will no longer hit performance.
1312 * This is not applicable to Onstream.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313 */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001314static ssize_t idetape_chrdev_read(struct file *file, char __user *buf,
1315 size_t count, loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316{
Borislav Petkov5aeddf92008-10-13 21:39:34 +02001317 struct ide_tape_obj *tape = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318 ide_drive_t *drive = tape->drive;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001319 ssize_t bytes_read, temp, actually_read = 0, rc;
Daniel Walkerdcd96372006-06-25 05:47:37 -07001320 ssize_t ret = 0;
Borislav Petkovb6422012008-02-02 19:56:49 +01001321 u16 ctl = *(u16 *)&tape->caps[12];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001323 debug_log(DBG_CHRDEV, "Enter %s, count %Zd\n", __func__, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324
Borislav Petkov54abf372008-02-06 02:57:52 +01001325 if (tape->chrdev_dir != IDETAPE_DIR_READ) {
Borislav Petkovf2e3ab52008-07-23 19:56:01 +02001326 if (test_bit(IDE_AFLAG_DETECT_BS, &drive->atapi_flags))
Borislav Petkov54bb2072008-02-06 02:57:52 +01001327 if (count > tape->blk_size &&
1328 (count % tape->blk_size) == 0)
1329 tape->user_bs_factor = count / tape->blk_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330 }
Borislav Petkov83042b22008-04-27 15:38:27 +02001331 rc = idetape_init_read(drive);
Borislav Petkov8d06bfa2008-02-06 02:57:53 +01001332 if (rc < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333 return rc;
1334 if (count == 0)
1335 return (0);
Borislav Petkov01a63aeb2008-04-27 15:38:34 +02001336 if (tape->merge_bh_size) {
1337 actually_read = min((unsigned int)(tape->merge_bh_size),
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001338 (unsigned int)count);
Borislav Petkov99d74e62008-04-27 15:38:25 +02001339 if (idetape_copy_stage_to_user(tape, buf, actually_read))
Daniel Walkerdcd96372006-06-25 05:47:37 -07001340 ret = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341 buf += actually_read;
Borislav Petkov01a63aeb2008-04-27 15:38:34 +02001342 tape->merge_bh_size -= actually_read;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343 count -= actually_read;
1344 }
Borislav Petkovf73850a2008-04-27 15:38:33 +02001345 while (count >= tape->buffer_size) {
Borislav Petkovb6422012008-02-02 19:56:49 +01001346 bytes_read = idetape_add_chrdev_read_request(drive, ctl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347 if (bytes_read <= 0)
1348 goto finish;
Borislav Petkov99d74e62008-04-27 15:38:25 +02001349 if (idetape_copy_stage_to_user(tape, buf, bytes_read))
Daniel Walkerdcd96372006-06-25 05:47:37 -07001350 ret = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351 buf += bytes_read;
1352 count -= bytes_read;
1353 actually_read += bytes_read;
1354 }
1355 if (count) {
Borislav Petkovb6422012008-02-02 19:56:49 +01001356 bytes_read = idetape_add_chrdev_read_request(drive, ctl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357 if (bytes_read <= 0)
1358 goto finish;
1359 temp = min((unsigned long)count, (unsigned long)bytes_read);
Borislav Petkov99d74e62008-04-27 15:38:25 +02001360 if (idetape_copy_stage_to_user(tape, buf, temp))
Daniel Walkerdcd96372006-06-25 05:47:37 -07001361 ret = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362 actually_read += temp;
Borislav Petkov01a63aeb2008-04-27 15:38:34 +02001363 tape->merge_bh_size = bytes_read-temp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364 }
1365finish:
Borislav Petkovf2e3ab52008-07-23 19:56:01 +02001366 if (!actually_read && test_bit(IDE_AFLAG_FILEMARK, &drive->atapi_flags)) {
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001367 debug_log(DBG_SENSE, "%s: spacing over filemark\n", tape->name);
1368
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369 idetape_space_over_filemarks(drive, MTFSF, 1);
1370 return 0;
1371 }
Daniel Walkerdcd96372006-06-25 05:47:37 -07001372
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001373 return ret ? ret : actually_read;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374}
1375
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001376static ssize_t idetape_chrdev_write(struct file *file, const char __user *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377 size_t count, loff_t *ppos)
1378{
Borislav Petkov5aeddf92008-10-13 21:39:34 +02001379 struct ide_tape_obj *tape = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380 ide_drive_t *drive = tape->drive;
Daniel Walkerdcd96372006-06-25 05:47:37 -07001381 ssize_t actually_written = 0;
1382 ssize_t ret = 0;
Borislav Petkovb6422012008-02-02 19:56:49 +01001383 u16 ctl = *(u16 *)&tape->caps[12];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384
1385 /* The drive is write protected. */
1386 if (tape->write_prot)
1387 return -EACCES;
1388
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001389 debug_log(DBG_CHRDEV, "Enter %s, count %Zd\n", __func__, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390
1391 /* Initialize write operation */
Borislav Petkov54abf372008-02-06 02:57:52 +01001392 if (tape->chrdev_dir != IDETAPE_DIR_WRITE) {
1393 if (tape->chrdev_dir == IDETAPE_DIR_READ)
Borislav Petkovec0fdb02008-04-27 15:38:34 +02001394 ide_tape_discard_merge_buffer(drive, 1);
Borislav Petkov077e3bd2008-04-27 15:38:34 +02001395 if (tape->merge_bh || tape->merge_bh_size) {
Borislav Petkov01a63aeb2008-04-27 15:38:34 +02001396 printk(KERN_ERR "ide-tape: merge_bh_size "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397 "should be 0 now\n");
Borislav Petkov01a63aeb2008-04-27 15:38:34 +02001398 tape->merge_bh_size = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399 }
Tejun Heo35ab8d32009-04-19 08:46:02 +09001400 tape->merge_bh = ide_tape_kmalloc_buffer(tape, 0);
Borislav Petkov077e3bd2008-04-27 15:38:34 +02001401 if (!tape->merge_bh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402 return -ENOMEM;
Borislav Petkov54abf372008-02-06 02:57:52 +01001403 tape->chrdev_dir = IDETAPE_DIR_WRITE;
Borislav Petkov077e3bd2008-04-27 15:38:34 +02001404 idetape_init_merge_buffer(tape);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405
1406 /*
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001407 * Issue a write 0 command to ensure that DSC handshake is
1408 * switched from completion mode to buffer available mode. No
1409 * point in issuing this if DSC overlap isn't supported, some
1410 * drives (Seagate STT3401A) will return an error.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411 */
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +02001412 if (drive->dev_flags & IDE_DFLAG_DSC_OVERLAP) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001413 ssize_t retval = idetape_queue_rw_tail(drive,
1414 REQ_IDETAPE_WRITE, 0,
Borislav Petkov077e3bd2008-04-27 15:38:34 +02001415 tape->merge_bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416 if (retval < 0) {
Borislav Petkov077e3bd2008-04-27 15:38:34 +02001417 ide_tape_kfree_buffer(tape);
1418 tape->merge_bh = NULL;
Borislav Petkov54abf372008-02-06 02:57:52 +01001419 tape->chrdev_dir = IDETAPE_DIR_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420 return retval;
1421 }
1422 }
1423 }
1424 if (count == 0)
1425 return (0);
Borislav Petkov01a63aeb2008-04-27 15:38:34 +02001426 if (tape->merge_bh_size) {
1427 if (tape->merge_bh_size >= tape->buffer_size) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001428 printk(KERN_ERR "ide-tape: bug: merge buf too big\n");
Borislav Petkov01a63aeb2008-04-27 15:38:34 +02001429 tape->merge_bh_size = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430 }
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001431 actually_written = min((unsigned int)
Borislav Petkov01a63aeb2008-04-27 15:38:34 +02001432 (tape->buffer_size - tape->merge_bh_size),
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001433 (unsigned int)count);
Borislav Petkov8646c882008-04-27 15:38:26 +02001434 if (idetape_copy_stage_from_user(tape, buf, actually_written))
Daniel Walkerdcd96372006-06-25 05:47:37 -07001435 ret = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436 buf += actually_written;
Borislav Petkov01a63aeb2008-04-27 15:38:34 +02001437 tape->merge_bh_size += actually_written;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438 count -= actually_written;
1439
Borislav Petkov01a63aeb2008-04-27 15:38:34 +02001440 if (tape->merge_bh_size == tape->buffer_size) {
Daniel Walkerdcd96372006-06-25 05:47:37 -07001441 ssize_t retval;
Borislav Petkov01a63aeb2008-04-27 15:38:34 +02001442 tape->merge_bh_size = 0;
Borislav Petkovb6422012008-02-02 19:56:49 +01001443 retval = idetape_add_chrdev_write_request(drive, ctl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444 if (retval <= 0)
1445 return (retval);
1446 }
1447 }
Borislav Petkovf73850a2008-04-27 15:38:33 +02001448 while (count >= tape->buffer_size) {
Daniel Walkerdcd96372006-06-25 05:47:37 -07001449 ssize_t retval;
Borislav Petkovf73850a2008-04-27 15:38:33 +02001450 if (idetape_copy_stage_from_user(tape, buf, tape->buffer_size))
Daniel Walkerdcd96372006-06-25 05:47:37 -07001451 ret = -EFAULT;
Borislav Petkovf73850a2008-04-27 15:38:33 +02001452 buf += tape->buffer_size;
1453 count -= tape->buffer_size;
Borislav Petkovb6422012008-02-02 19:56:49 +01001454 retval = idetape_add_chrdev_write_request(drive, ctl);
Borislav Petkovf73850a2008-04-27 15:38:33 +02001455 actually_written += tape->buffer_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456 if (retval <= 0)
1457 return (retval);
1458 }
1459 if (count) {
1460 actually_written += count;
Borislav Petkov8646c882008-04-27 15:38:26 +02001461 if (idetape_copy_stage_from_user(tape, buf, count))
Daniel Walkerdcd96372006-06-25 05:47:37 -07001462 ret = -EFAULT;
Borislav Petkov01a63aeb2008-04-27 15:38:34 +02001463 tape->merge_bh_size += count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464 }
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001465 return ret ? ret : actually_written;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466}
1467
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001468static int idetape_write_filemark(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469{
Bartlomiej Zolnierkiewicz2ac07d92008-10-10 22:39:38 +02001470 struct ide_tape_obj *tape = drive->driver_data;
Borislav Petkovd236d742008-04-18 00:46:27 +02001471 struct ide_atapi_pc pc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472
1473 /* Write a filemark */
1474 idetape_create_write_filemark_cmd(drive, &pc, 1);
Bartlomiej Zolnierkiewicz2ac07d92008-10-10 22:39:38 +02001475 if (ide_queue_pc_tail(drive, tape->disk, &pc)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476 printk(KERN_ERR "ide-tape: Couldn't write a filemark\n");
1477 return -EIO;
1478 }
1479 return 0;
1480}
1481
1482/*
Borislav Petkovd99c9da2008-02-02 19:56:51 +01001483 * Called from idetape_chrdev_ioctl when the general mtio MTIOCTOP ioctl is
1484 * requested.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485 *
Borislav Petkovd99c9da2008-02-02 19:56:51 +01001486 * Note: MTBSF and MTBSFM are not supported when the tape doesn't support
1487 * spacing over filemarks in the reverse direction. In this case, MTFSFM is also
Borislav Petkov5bd50dc2008-04-27 15:38:28 +02001488 * usually not supported.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489 *
Borislav Petkovd99c9da2008-02-02 19:56:51 +01001490 * The following commands are currently not supported:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491 *
Borislav Petkovd99c9da2008-02-02 19:56:51 +01001492 * MTFSS, MTBSS, MTWSM, MTSETDENSITY, MTSETDRVBUFFER, MT_ST_BOOLEANS,
1493 * MT_ST_WRITE_THRESHOLD.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494 */
Borislav Petkovd99c9da2008-02-02 19:56:51 +01001495static int idetape_mtioctop(ide_drive_t *drive, short mt_op, int mt_count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496{
1497 idetape_tape_t *tape = drive->driver_data;
Bartlomiej Zolnierkiewicz2ac07d92008-10-10 22:39:38 +02001498 struct gendisk *disk = tape->disk;
Borislav Petkovd236d742008-04-18 00:46:27 +02001499 struct ide_atapi_pc pc;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001500 int i, retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001502 debug_log(DBG_ERR, "Handling MTIOCTOP ioctl: mt_op=%d, mt_count=%d\n",
1503 mt_op, mt_count);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001504
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505 switch (mt_op) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001506 case MTFSF:
1507 case MTFSFM:
1508 case MTBSF:
1509 case MTBSFM:
1510 if (!mt_count)
1511 return 0;
1512 return idetape_space_over_filemarks(drive, mt_op, mt_count);
1513 default:
1514 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515 }
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001516
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517 switch (mt_op) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001518 case MTWEOF:
1519 if (tape->write_prot)
1520 return -EACCES;
Borislav Petkovec0fdb02008-04-27 15:38:34 +02001521 ide_tape_discard_merge_buffer(drive, 1);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001522 for (i = 0; i < mt_count; i++) {
1523 retval = idetape_write_filemark(drive);
1524 if (retval)
1525 return retval;
1526 }
1527 return 0;
1528 case MTREW:
Borislav Petkovec0fdb02008-04-27 15:38:34 +02001529 ide_tape_discard_merge_buffer(drive, 0);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001530 if (idetape_rewind_tape(drive))
1531 return -EIO;
1532 return 0;
1533 case MTLOAD:
Borislav Petkovec0fdb02008-04-27 15:38:34 +02001534 ide_tape_discard_merge_buffer(drive, 0);
Bartlomiej Zolnierkiewicz0c8a6c72008-10-10 22:39:39 +02001535 return ide_do_start_stop(drive, disk, IDETAPE_LU_LOAD_MASK);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001536 case MTUNLOAD:
1537 case MTOFFL:
1538 /*
1539 * If door is locked, attempt to unlock before
1540 * attempting to eject.
1541 */
1542 if (tape->door_locked) {
Bartlomiej Zolnierkiewicz05780422008-10-10 22:39:38 +02001543 if (!ide_set_media_lock(drive, disk, 0))
Bartlomiej Zolnierkiewicz385a4b82008-10-10 22:39:37 +02001544 tape->door_locked = DOOR_UNLOCKED;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001545 }
Borislav Petkovec0fdb02008-04-27 15:38:34 +02001546 ide_tape_discard_merge_buffer(drive, 0);
Bartlomiej Zolnierkiewicz0c8a6c72008-10-10 22:39:39 +02001547 retval = ide_do_start_stop(drive, disk, !IDETAPE_LU_LOAD_MASK);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001548 if (!retval)
Borislav Petkovf2e3ab52008-07-23 19:56:01 +02001549 clear_bit(IDE_AFLAG_MEDIUM_PRESENT, &drive->atapi_flags);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001550 return retval;
1551 case MTNOP:
Borislav Petkovec0fdb02008-04-27 15:38:34 +02001552 ide_tape_discard_merge_buffer(drive, 0);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001553 return idetape_flush_tape_buffers(drive);
1554 case MTRETEN:
Borislav Petkovec0fdb02008-04-27 15:38:34 +02001555 ide_tape_discard_merge_buffer(drive, 0);
Bartlomiej Zolnierkiewicz0c8a6c72008-10-10 22:39:39 +02001556 return ide_do_start_stop(drive, disk,
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001557 IDETAPE_LU_RETENSION_MASK | IDETAPE_LU_LOAD_MASK);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001558 case MTEOM:
1559 idetape_create_space_cmd(&pc, 0, IDETAPE_SPACE_TO_EOD);
Bartlomiej Zolnierkiewicz2ac07d92008-10-10 22:39:38 +02001560 return ide_queue_pc_tail(drive, disk, &pc);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001561 case MTERASE:
1562 (void)idetape_rewind_tape(drive);
1563 idetape_create_erase_cmd(&pc);
Bartlomiej Zolnierkiewicz2ac07d92008-10-10 22:39:38 +02001564 return ide_queue_pc_tail(drive, disk, &pc);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001565 case MTSETBLK:
1566 if (mt_count) {
1567 if (mt_count < tape->blk_size ||
1568 mt_count % tape->blk_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569 return -EIO;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001570 tape->user_bs_factor = mt_count / tape->blk_size;
Borislav Petkovf2e3ab52008-07-23 19:56:01 +02001571 clear_bit(IDE_AFLAG_DETECT_BS, &drive->atapi_flags);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001572 } else
Borislav Petkovf2e3ab52008-07-23 19:56:01 +02001573 set_bit(IDE_AFLAG_DETECT_BS, &drive->atapi_flags);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001574 return 0;
1575 case MTSEEK:
Borislav Petkovec0fdb02008-04-27 15:38:34 +02001576 ide_tape_discard_merge_buffer(drive, 0);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001577 return idetape_position_tape(drive,
1578 mt_count * tape->user_bs_factor, tape->partition, 0);
1579 case MTSETPART:
Borislav Petkovec0fdb02008-04-27 15:38:34 +02001580 ide_tape_discard_merge_buffer(drive, 0);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001581 return idetape_position_tape(drive, 0, mt_count, 0);
1582 case MTFSR:
1583 case MTBSR:
1584 case MTLOCK:
Bartlomiej Zolnierkiewicz05780422008-10-10 22:39:38 +02001585 retval = ide_set_media_lock(drive, disk, 1);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001586 if (retval)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587 return retval;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001588 tape->door_locked = DOOR_EXPLICITLY_LOCKED;
1589 return 0;
1590 case MTUNLOCK:
Bartlomiej Zolnierkiewicz05780422008-10-10 22:39:38 +02001591 retval = ide_set_media_lock(drive, disk, 0);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001592 if (retval)
1593 return retval;
1594 tape->door_locked = DOOR_UNLOCKED;
1595 return 0;
1596 default:
1597 printk(KERN_ERR "ide-tape: MTIO operation %d not supported\n",
1598 mt_op);
1599 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001600 }
1601}
1602
1603/*
Borislav Petkovd99c9da2008-02-02 19:56:51 +01001604 * Our character device ioctls. General mtio.h magnetic io commands are
1605 * supported here, and not in the corresponding block interface. Our own
1606 * ide-tape ioctls are supported on both interfaces.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001607 */
Borislav Petkovd99c9da2008-02-02 19:56:51 +01001608static int idetape_chrdev_ioctl(struct inode *inode, struct file *file,
1609 unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610{
Borislav Petkov5aeddf92008-10-13 21:39:34 +02001611 struct ide_tape_obj *tape = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001612 ide_drive_t *drive = tape->drive;
1613 struct mtop mtop;
1614 struct mtget mtget;
1615 struct mtpos mtpos;
Borislav Petkov54bb2072008-02-06 02:57:52 +01001616 int block_offset = 0, position = tape->first_frame;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617 void __user *argp = (void __user *)arg;
1618
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001619 debug_log(DBG_CHRDEV, "Enter %s, cmd=%u\n", __func__, cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001620
Borislav Petkov54abf372008-02-06 02:57:52 +01001621 if (tape->chrdev_dir == IDETAPE_DIR_WRITE) {
Borislav Petkovd9df9372008-04-27 15:38:34 +02001622 ide_tape_flush_merge_buffer(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623 idetape_flush_tape_buffers(drive);
1624 }
1625 if (cmd == MTIOCGET || cmd == MTIOCPOS) {
Borislav Petkov01a63aeb2008-04-27 15:38:34 +02001626 block_offset = tape->merge_bh_size /
Borislav Petkov54bb2072008-02-06 02:57:52 +01001627 (tape->blk_size * tape->user_bs_factor);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001628 position = idetape_read_position(drive);
1629 if (position < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001630 return -EIO;
1631 }
1632 switch (cmd) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001633 case MTIOCTOP:
1634 if (copy_from_user(&mtop, argp, sizeof(struct mtop)))
1635 return -EFAULT;
1636 return idetape_mtioctop(drive, mtop.mt_op, mtop.mt_count);
1637 case MTIOCGET:
1638 memset(&mtget, 0, sizeof(struct mtget));
1639 mtget.mt_type = MT_ISSCSI2;
1640 mtget.mt_blkno = position / tape->user_bs_factor - block_offset;
1641 mtget.mt_dsreg =
1642 ((tape->blk_size * tape->user_bs_factor)
1643 << MT_ST_BLKSIZE_SHIFT) & MT_ST_BLKSIZE_MASK;
Borislav Petkov54bb2072008-02-06 02:57:52 +01001644
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001645 if (tape->drv_write_prot)
1646 mtget.mt_gstat |= GMT_WR_PROT(0xffffffff);
1647
1648 if (copy_to_user(argp, &mtget, sizeof(struct mtget)))
1649 return -EFAULT;
1650 return 0;
1651 case MTIOCPOS:
1652 mtpos.mt_blkno = position / tape->user_bs_factor - block_offset;
1653 if (copy_to_user(argp, &mtpos, sizeof(struct mtpos)))
1654 return -EFAULT;
1655 return 0;
1656 default:
1657 if (tape->chrdev_dir == IDETAPE_DIR_READ)
Borislav Petkovec0fdb02008-04-27 15:38:34 +02001658 ide_tape_discard_merge_buffer(drive, 1);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001659 return idetape_blkdev_ioctl(drive, cmd, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001660 }
1661}
1662
Borislav Petkov3cffb9c2008-02-02 19:56:50 +01001663/*
1664 * Do a mode sense page 0 with block descriptor and if it succeeds set the tape
1665 * block size with the reported value.
1666 */
1667static void ide_tape_get_bsize_from_bdesc(ide_drive_t *drive)
1668{
1669 idetape_tape_t *tape = drive->driver_data;
Borislav Petkovd236d742008-04-18 00:46:27 +02001670 struct ide_atapi_pc pc;
Borislav Petkov3cffb9c2008-02-02 19:56:50 +01001671
1672 idetape_create_mode_sense_cmd(&pc, IDETAPE_BLOCK_DESCRIPTOR);
Bartlomiej Zolnierkiewicz2ac07d92008-10-10 22:39:38 +02001673 if (ide_queue_pc_tail(drive, tape->disk, &pc)) {
Borislav Petkov3cffb9c2008-02-02 19:56:50 +01001674 printk(KERN_ERR "ide-tape: Can't get block descriptor\n");
Borislav Petkov54bb2072008-02-06 02:57:52 +01001675 if (tape->blk_size == 0) {
Borislav Petkov3cffb9c2008-02-02 19:56:50 +01001676 printk(KERN_WARNING "ide-tape: Cannot deal with zero "
1677 "block size, assuming 32k\n");
Borislav Petkov54bb2072008-02-06 02:57:52 +01001678 tape->blk_size = 32768;
Borislav Petkov3cffb9c2008-02-02 19:56:50 +01001679 }
1680 return;
1681 }
Borislav Petkovd236d742008-04-18 00:46:27 +02001682 tape->blk_size = (pc.buf[4 + 5] << 16) +
1683 (pc.buf[4 + 6] << 8) +
1684 pc.buf[4 + 7];
1685 tape->drv_write_prot = (pc.buf[2] & 0x80) >> 7;
Borislav Petkov3cffb9c2008-02-02 19:56:50 +01001686}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001687
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001688static int idetape_chrdev_open(struct inode *inode, struct file *filp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001689{
1690 unsigned int minor = iminor(inode), i = minor & ~0xc0;
1691 ide_drive_t *drive;
1692 idetape_tape_t *tape;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001693 int retval;
1694
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001695 if (i >= MAX_HWIFS * MAX_DRIVES)
1696 return -ENXIO;
1697
Jonathan Corbet04f4ac92008-05-15 12:01:56 -06001698 lock_kernel();
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001699 tape = ide_tape_chrdev_get(i);
Jonathan Corbet04f4ac92008-05-15 12:01:56 -06001700 if (!tape) {
1701 unlock_kernel();
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001702 return -ENXIO;
Jonathan Corbet04f4ac92008-05-15 12:01:56 -06001703 }
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001704
1705 debug_log(DBG_CHRDEV, "Enter %s\n", __func__);
1706
Linus Torvalds1da177e2005-04-16 15:20:36 -07001707 /*
1708 * We really want to do nonseekable_open(inode, filp); here, but some
1709 * versions of tar incorrectly call lseek on tapes and bail out if that
1710 * fails. So we disallow pread() and pwrite(), but permit lseeks.
1711 */
1712 filp->f_mode &= ~(FMODE_PREAD | FMODE_PWRITE);
1713
Linus Torvalds1da177e2005-04-16 15:20:36 -07001714 drive = tape->drive;
1715
1716 filp->private_data = tape;
1717
Borislav Petkovf2e3ab52008-07-23 19:56:01 +02001718 if (test_and_set_bit(IDE_AFLAG_BUSY, &drive->atapi_flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001719 retval = -EBUSY;
1720 goto out_put_tape;
1721 }
1722
1723 retval = idetape_wait_ready(drive, 60 * HZ);
1724 if (retval) {
Borislav Petkovf2e3ab52008-07-23 19:56:01 +02001725 clear_bit(IDE_AFLAG_BUSY, &drive->atapi_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001726 printk(KERN_ERR "ide-tape: %s: drive not ready\n", tape->name);
1727 goto out_put_tape;
1728 }
1729
1730 idetape_read_position(drive);
Borislav Petkovf2e3ab52008-07-23 19:56:01 +02001731 if (!test_bit(IDE_AFLAG_ADDRESS_VALID, &drive->atapi_flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001732 (void)idetape_rewind_tape(drive);
1733
Linus Torvalds1da177e2005-04-16 15:20:36 -07001734 /* Read block size and write protect status from drive. */
Borislav Petkov3cffb9c2008-02-02 19:56:50 +01001735 ide_tape_get_bsize_from_bdesc(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001736
1737 /* Set write protect flag if device is opened as read-only. */
1738 if ((filp->f_flags & O_ACCMODE) == O_RDONLY)
1739 tape->write_prot = 1;
1740 else
1741 tape->write_prot = tape->drv_write_prot;
1742
1743 /* Make sure drive isn't write protected if user wants to write. */
1744 if (tape->write_prot) {
1745 if ((filp->f_flags & O_ACCMODE) == O_WRONLY ||
1746 (filp->f_flags & O_ACCMODE) == O_RDWR) {
Borislav Petkovf2e3ab52008-07-23 19:56:01 +02001747 clear_bit(IDE_AFLAG_BUSY, &drive->atapi_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001748 retval = -EROFS;
1749 goto out_put_tape;
1750 }
1751 }
1752
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001753 /* Lock the tape drive door so user can't eject. */
Borislav Petkov54abf372008-02-06 02:57:52 +01001754 if (tape->chrdev_dir == IDETAPE_DIR_NONE) {
Bartlomiej Zolnierkiewicz05780422008-10-10 22:39:38 +02001755 if (!ide_set_media_lock(drive, tape->disk, 1)) {
Bartlomiej Zolnierkiewicz385a4b82008-10-10 22:39:37 +02001756 if (tape->door_locked != DOOR_EXPLICITLY_LOCKED)
1757 tape->door_locked = DOOR_LOCKED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001758 }
1759 }
Jonathan Corbet04f4ac92008-05-15 12:01:56 -06001760 unlock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001761 return 0;
1762
1763out_put_tape:
1764 ide_tape_put(tape);
Jonathan Corbet04f4ac92008-05-15 12:01:56 -06001765 unlock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001766 return retval;
1767}
1768
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001769static void idetape_write_release(ide_drive_t *drive, unsigned int minor)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001770{
1771 idetape_tape_t *tape = drive->driver_data;
1772
Borislav Petkovd9df9372008-04-27 15:38:34 +02001773 ide_tape_flush_merge_buffer(drive);
Tejun Heo35ab8d32009-04-19 08:46:02 +09001774 tape->merge_bh = ide_tape_kmalloc_buffer(tape, 1);
Borislav Petkov077e3bd2008-04-27 15:38:34 +02001775 if (tape->merge_bh != NULL) {
Borislav Petkov54bb2072008-02-06 02:57:52 +01001776 idetape_pad_zeros(drive, tape->blk_size *
1777 (tape->user_bs_factor - 1));
Borislav Petkov077e3bd2008-04-27 15:38:34 +02001778 ide_tape_kfree_buffer(tape);
1779 tape->merge_bh = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001780 }
1781 idetape_write_filemark(drive);
1782 idetape_flush_tape_buffers(drive);
1783 idetape_flush_tape_buffers(drive);
1784}
1785
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001786static int idetape_chrdev_release(struct inode *inode, struct file *filp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001787{
Borislav Petkov5aeddf92008-10-13 21:39:34 +02001788 struct ide_tape_obj *tape = filp->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001789 ide_drive_t *drive = tape->drive;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790 unsigned int minor = iminor(inode);
1791
1792 lock_kernel();
1793 tape = drive->driver_data;
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001794
1795 debug_log(DBG_CHRDEV, "Enter %s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001796
Borislav Petkov54abf372008-02-06 02:57:52 +01001797 if (tape->chrdev_dir == IDETAPE_DIR_WRITE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001798 idetape_write_release(drive, minor);
Borislav Petkov54abf372008-02-06 02:57:52 +01001799 if (tape->chrdev_dir == IDETAPE_DIR_READ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001800 if (minor < 128)
Borislav Petkovec0fdb02008-04-27 15:38:34 +02001801 ide_tape_discard_merge_buffer(drive, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001802 }
Borislav Petkovf64eee72008-04-27 15:38:25 +02001803
Borislav Petkovf2e3ab52008-07-23 19:56:01 +02001804 if (minor < 128 && test_bit(IDE_AFLAG_MEDIUM_PRESENT, &drive->atapi_flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805 (void) idetape_rewind_tape(drive);
Borislav Petkov54abf372008-02-06 02:57:52 +01001806 if (tape->chrdev_dir == IDETAPE_DIR_NONE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807 if (tape->door_locked == DOOR_LOCKED) {
Bartlomiej Zolnierkiewicz05780422008-10-10 22:39:38 +02001808 if (!ide_set_media_lock(drive, tape->disk, 0))
Bartlomiej Zolnierkiewicz385a4b82008-10-10 22:39:37 +02001809 tape->door_locked = DOOR_UNLOCKED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001810 }
1811 }
Borislav Petkovf2e3ab52008-07-23 19:56:01 +02001812 clear_bit(IDE_AFLAG_BUSY, &drive->atapi_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001813 ide_tape_put(tape);
1814 unlock_kernel();
1815 return 0;
1816}
1817
Borislav Petkov6d29c8f2008-02-02 19:56:49 +01001818static void idetape_get_inquiry_results(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001819{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001820 idetape_tape_t *tape = drive->driver_data;
Borislav Petkovd236d742008-04-18 00:46:27 +02001821 struct ide_atapi_pc pc;
Bartlomiej Zolnierkiewicz41fa9f82009-03-31 20:15:25 +02001822 u8 pc_buf[256];
Borislav Petkov801bd322008-09-27 19:32:17 +02001823 char fw_rev[4], vendor_id[8], product_id[16];
Borislav Petkov6d29c8f2008-02-02 19:56:49 +01001824
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825 idetape_create_inquiry_cmd(&pc);
Bartlomiej Zolnierkiewicz41fa9f82009-03-31 20:15:25 +02001826 pc.buf = &pc_buf[0];
1827 pc.buf_size = sizeof(pc_buf);
1828
Bartlomiej Zolnierkiewicz2ac07d92008-10-10 22:39:38 +02001829 if (ide_queue_pc_tail(drive, tape->disk, &pc)) {
Borislav Petkov6d29c8f2008-02-02 19:56:49 +01001830 printk(KERN_ERR "ide-tape: %s: can't get INQUIRY results\n",
1831 tape->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001832 return;
1833 }
Borislav Petkovd236d742008-04-18 00:46:27 +02001834 memcpy(vendor_id, &pc.buf[8], 8);
1835 memcpy(product_id, &pc.buf[16], 16);
1836 memcpy(fw_rev, &pc.buf[32], 4);
Borislav Petkov6d29c8f2008-02-02 19:56:49 +01001837
Borislav Petkov801bd322008-09-27 19:32:17 +02001838 ide_fixstring(vendor_id, 8, 0);
1839 ide_fixstring(product_id, 16, 0);
1840 ide_fixstring(fw_rev, 4, 0);
Borislav Petkov41f81d542008-02-06 02:57:52 +01001841
Borislav Petkov801bd322008-09-27 19:32:17 +02001842 printk(KERN_INFO "ide-tape: %s <-> %s: %.8s %.16s rev %.4s\n",
Borislav Petkov41f81d542008-02-06 02:57:52 +01001843 drive->name, tape->name, vendor_id, product_id, fw_rev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001844}
1845
1846/*
Borislav Petkovb6422012008-02-02 19:56:49 +01001847 * Ask the tape about its various parameters. In particular, we will adjust our
1848 * data transfer buffer size to the recommended value as returned by the tape.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001849 */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001850static void idetape_get_mode_sense_results(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001851{
1852 idetape_tape_t *tape = drive->driver_data;
Borislav Petkovd236d742008-04-18 00:46:27 +02001853 struct ide_atapi_pc pc;
Borislav Petkovb6422012008-02-02 19:56:49 +01001854 u8 *caps;
1855 u8 speed, max_speed;
Borislav Petkov47314fa2008-02-02 19:56:48 +01001856
Linus Torvalds1da177e2005-04-16 15:20:36 -07001857 idetape_create_mode_sense_cmd(&pc, IDETAPE_CAPABILITIES_PAGE);
Bartlomiej Zolnierkiewicz2ac07d92008-10-10 22:39:38 +02001858 if (ide_queue_pc_tail(drive, tape->disk, &pc)) {
Borislav Petkovb6422012008-02-02 19:56:49 +01001859 printk(KERN_ERR "ide-tape: Can't get tape parameters - assuming"
1860 " some default values\n");
Borislav Petkov54bb2072008-02-06 02:57:52 +01001861 tape->blk_size = 512;
Borislav Petkovb6422012008-02-02 19:56:49 +01001862 put_unaligned(52, (u16 *)&tape->caps[12]);
1863 put_unaligned(540, (u16 *)&tape->caps[14]);
1864 put_unaligned(6*52, (u16 *)&tape->caps[16]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001865 return;
1866 }
Borislav Petkovd236d742008-04-18 00:46:27 +02001867 caps = pc.buf + 4 + pc.buf[3];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001868
Borislav Petkovb6422012008-02-02 19:56:49 +01001869 /* convert to host order and save for later use */
Harvey Harrisoncd740ab2008-07-24 22:53:33 +02001870 speed = be16_to_cpup((__be16 *)&caps[14]);
1871 max_speed = be16_to_cpup((__be16 *)&caps[8]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001872
Harvey Harrisoncd740ab2008-07-24 22:53:33 +02001873 *(u16 *)&caps[8] = max_speed;
1874 *(u16 *)&caps[12] = be16_to_cpup((__be16 *)&caps[12]);
1875 *(u16 *)&caps[14] = speed;
1876 *(u16 *)&caps[16] = be16_to_cpup((__be16 *)&caps[16]);
Borislav Petkovb6422012008-02-02 19:56:49 +01001877
1878 if (!speed) {
1879 printk(KERN_INFO "ide-tape: %s: invalid tape speed "
1880 "(assuming 650KB/sec)\n", drive->name);
Harvey Harrisoncd740ab2008-07-24 22:53:33 +02001881 *(u16 *)&caps[14] = 650;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001882 }
Borislav Petkovb6422012008-02-02 19:56:49 +01001883 if (!max_speed) {
1884 printk(KERN_INFO "ide-tape: %s: invalid max_speed "
1885 "(assuming 650KB/sec)\n", drive->name);
Harvey Harrisoncd740ab2008-07-24 22:53:33 +02001886 *(u16 *)&caps[8] = 650;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001887 }
1888
Borislav Petkovb6422012008-02-02 19:56:49 +01001889 memcpy(&tape->caps, caps, 20);
Bartlomiej Zolnierkiewicz05780422008-10-10 22:39:38 +02001890
1891 /* device lacks locking support according to capabilities page */
1892 if ((caps[6] & 1) == 0)
Bartlomiej Zolnierkiewicz42619d32008-10-17 18:09:11 +02001893 drive->dev_flags &= ~IDE_DFLAG_DOORLOCKING;
Bartlomiej Zolnierkiewicz05780422008-10-10 22:39:38 +02001894
Borislav Petkovb6422012008-02-02 19:56:49 +01001895 if (caps[7] & 0x02)
Borislav Petkov54bb2072008-02-06 02:57:52 +01001896 tape->blk_size = 512;
Borislav Petkovb6422012008-02-02 19:56:49 +01001897 else if (caps[7] & 0x04)
Borislav Petkov54bb2072008-02-06 02:57:52 +01001898 tape->blk_size = 1024;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001899}
1900
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +02001901#ifdef CONFIG_IDE_PROC_FS
Bartlomiej Zolnierkiewicz8185d5a2008-10-10 22:39:28 +02001902#define ide_tape_devset_get(name, field) \
1903static int get_##name(ide_drive_t *drive) \
1904{ \
1905 idetape_tape_t *tape = drive->driver_data; \
1906 return tape->field; \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001907}
Bartlomiej Zolnierkiewicz8185d5a2008-10-10 22:39:28 +02001908
1909#define ide_tape_devset_set(name, field) \
1910static int set_##name(ide_drive_t *drive, int arg) \
1911{ \
1912 idetape_tape_t *tape = drive->driver_data; \
1913 tape->field = arg; \
1914 return 0; \
1915}
1916
Elias Oltmanns92f1f8f2008-10-10 22:39:40 +02001917#define ide_tape_devset_rw_field(_name, _field) \
Bartlomiej Zolnierkiewicz8185d5a2008-10-10 22:39:28 +02001918ide_tape_devset_get(_name, _field) \
1919ide_tape_devset_set(_name, _field) \
Elias Oltmanns92f1f8f2008-10-10 22:39:40 +02001920IDE_DEVSET(_name, DS_SYNC, get_##_name, set_##_name)
Bartlomiej Zolnierkiewicz8185d5a2008-10-10 22:39:28 +02001921
Elias Oltmanns92f1f8f2008-10-10 22:39:40 +02001922#define ide_tape_devset_r_field(_name, _field) \
Bartlomiej Zolnierkiewicz8185d5a2008-10-10 22:39:28 +02001923ide_tape_devset_get(_name, _field) \
Elias Oltmanns92f1f8f2008-10-10 22:39:40 +02001924IDE_DEVSET(_name, 0, get_##_name, NULL)
Bartlomiej Zolnierkiewicz8185d5a2008-10-10 22:39:28 +02001925
1926static int mulf_tdsc(ide_drive_t *drive) { return 1000; }
1927static int divf_tdsc(ide_drive_t *drive) { return HZ; }
1928static int divf_buffer(ide_drive_t *drive) { return 2; }
1929static int divf_buffer_size(ide_drive_t *drive) { return 1024; }
1930
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +02001931ide_devset_rw_flag(dsc_overlap, IDE_DFLAG_DSC_OVERLAP);
Bartlomiej Zolnierkiewicz8185d5a2008-10-10 22:39:28 +02001932
Elias Oltmanns92f1f8f2008-10-10 22:39:40 +02001933ide_tape_devset_rw_field(debug_mask, debug_mask);
1934ide_tape_devset_rw_field(tdsc, best_dsc_rw_freq);
Bartlomiej Zolnierkiewicz8185d5a2008-10-10 22:39:28 +02001935
Elias Oltmanns92f1f8f2008-10-10 22:39:40 +02001936ide_tape_devset_r_field(avg_speed, avg_speed);
1937ide_tape_devset_r_field(speed, caps[14]);
1938ide_tape_devset_r_field(buffer, caps[16]);
1939ide_tape_devset_r_field(buffer_size, buffer_size);
Bartlomiej Zolnierkiewicz8185d5a2008-10-10 22:39:28 +02001940
Elias Oltmanns92f1f8f2008-10-10 22:39:40 +02001941static const struct ide_proc_devset idetape_settings[] = {
1942 __IDE_PROC_DEVSET(avg_speed, 0, 0xffff, NULL, NULL),
1943 __IDE_PROC_DEVSET(buffer, 0, 0xffff, NULL, divf_buffer),
1944 __IDE_PROC_DEVSET(buffer_size, 0, 0xffff, NULL, divf_buffer_size),
1945 __IDE_PROC_DEVSET(debug_mask, 0, 0xffff, NULL, NULL),
1946 __IDE_PROC_DEVSET(dsc_overlap, 0, 1, NULL, NULL),
1947 __IDE_PROC_DEVSET(speed, 0, 0xffff, NULL, NULL),
1948 __IDE_PROC_DEVSET(tdsc, IDETAPE_DSC_RW_MIN, IDETAPE_DSC_RW_MAX,
1949 mulf_tdsc, divf_tdsc),
Hannes Eder71bfc7a2009-03-05 16:10:56 +01001950 { NULL },
Bartlomiej Zolnierkiewicz8185d5a2008-10-10 22:39:28 +02001951};
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +02001952#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001953
1954/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001955 * The function below is called to:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001956 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001957 * 1. Initialize our various state variables.
1958 * 2. Ask the tape for its capabilities.
1959 * 3. Allocate a buffer which will be used for data transfer. The buffer size
1960 * is chosen based on the recommendation which we received in step 2.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001961 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001962 * Note that at this point ide.c already assigned us an irq, so that we can
1963 * queue requests here and wait for their completion.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001964 */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001965static void idetape_setup(ide_drive_t *drive, idetape_tape_t *tape, int minor)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001966{
Borislav Petkov83042b22008-04-27 15:38:27 +02001967 unsigned long t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001968 int speed;
Borislav Petkovf73850a2008-04-27 15:38:33 +02001969 int buffer_size;
Borislav Petkovb6422012008-02-02 19:56:49 +01001970 u16 *ctl = (u16 *)&tape->caps[12];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001971
Bartlomiej Zolnierkiewicz85e39032008-10-13 21:39:32 +02001972 drive->pc_callback = ide_tape_callback;
Borislav Petkov776bb022008-07-23 19:55:59 +02001973
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +02001974 drive->dev_flags |= IDE_DFLAG_DSC_OVERLAP;
1975
Bartlomiej Zolnierkiewicz4166c192008-02-01 23:09:30 +01001976 if (drive->hwif->host_flags & IDE_HFLAG_NO_DSC) {
1977 printk(KERN_INFO "ide-tape: %s: disabling DSC overlap\n",
1978 tape->name);
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +02001979 drive->dev_flags &= ~IDE_DFLAG_DSC_OVERLAP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001980 }
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +02001981
Linus Torvalds1da177e2005-04-16 15:20:36 -07001982 /* Seagate Travan drives do not support DSC overlap. */
Bartlomiej Zolnierkiewicz4dde4492008-10-10 22:39:19 +02001983 if (strstr((char *)&drive->id[ATA_ID_PROD], "Seagate STT3401"))
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +02001984 drive->dev_flags &= ~IDE_DFLAG_DSC_OVERLAP;
1985
Linus Torvalds1da177e2005-04-16 15:20:36 -07001986 tape->minor = minor;
1987 tape->name[0] = 'h';
1988 tape->name[1] = 't';
1989 tape->name[2] = '0' + minor;
Borislav Petkov54abf372008-02-06 02:57:52 +01001990 tape->chrdev_dir = IDETAPE_DIR_NONE;
Bartlomiej Zolnierkiewicz4dde4492008-10-10 22:39:19 +02001991
Linus Torvalds1da177e2005-04-16 15:20:36 -07001992 idetape_get_inquiry_results(drive);
1993 idetape_get_mode_sense_results(drive);
Borislav Petkov3cffb9c2008-02-02 19:56:50 +01001994 ide_tape_get_bsize_from_bdesc(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001995 tape->user_bs_factor = 1;
Borislav Petkovf73850a2008-04-27 15:38:33 +02001996 tape->buffer_size = *ctl * tape->blk_size;
1997 while (tape->buffer_size > 0xffff) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001998 printk(KERN_NOTICE "ide-tape: decreasing stage size\n");
Borislav Petkovb6422012008-02-02 19:56:49 +01001999 *ctl /= 2;
Borislav Petkovf73850a2008-04-27 15:38:33 +02002000 tape->buffer_size = *ctl * tape->blk_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002001 }
Borislav Petkovf73850a2008-04-27 15:38:33 +02002002 buffer_size = tape->buffer_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002003
Borislav Petkov83042b22008-04-27 15:38:27 +02002004 /* select the "best" DSC read/write polling freq */
Borislav Petkovb6422012008-02-02 19:56:49 +01002005 speed = max(*(u16 *)&tape->caps[14], *(u16 *)&tape->caps[8]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002006
Borislav Petkovf73850a2008-04-27 15:38:33 +02002007 t = (IDETAPE_FIFO_THRESHOLD * tape->buffer_size * HZ) / (speed * 1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002008
2009 /*
Borislav Petkov3c98bf32008-02-06 02:57:53 +01002010 * Ensure that the number we got makes sense; limit it within
2011 * IDETAPE_DSC_RW_MIN and IDETAPE_DSC_RW_MAX.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002012 */
Harvey Harrisona792bd52008-07-15 21:21:41 +02002013 tape->best_dsc_rw_freq = clamp_t(unsigned long, t, IDETAPE_DSC_RW_MIN,
2014 IDETAPE_DSC_RW_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002015 printk(KERN_INFO "ide-tape: %s <-> %s: %dKBps, %d*%dkB buffer, "
Borislav Petkov83042b22008-04-27 15:38:27 +02002016 "%lums tDSC%s\n",
Borislav Petkovb6422012008-02-02 19:56:49 +01002017 drive->name, tape->name, *(u16 *)&tape->caps[14],
Borislav Petkovf73850a2008-04-27 15:38:33 +02002018 (*(u16 *)&tape->caps[16] * 512) / tape->buffer_size,
2019 tape->buffer_size / 1024,
Borislav Petkov54bb2072008-02-06 02:57:52 +01002020 tape->best_dsc_rw_freq * 1000 / HZ,
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +02002021 (drive->dev_flags & IDE_DFLAG_USING_DMA) ? ", DMA" : "");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002022
Bartlomiej Zolnierkiewicz1e874f42008-10-10 22:39:27 +02002023 ide_proc_register_driver(drive, tape->driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002024}
2025
Russell King4031bbe2006-01-06 11:41:00 +00002026static void ide_tape_remove(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002027{
2028 idetape_tape_t *tape = drive->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002029
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +02002030 ide_proc_unregister_driver(drive, tape->driver);
Bartlomiej Zolnierkiewicz8fed4362009-02-25 20:28:24 +01002031 device_del(&tape->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002032 ide_unregister_region(tape->disk);
2033
Bartlomiej Zolnierkiewicz8fed4362009-02-25 20:28:24 +01002034 mutex_lock(&idetape_ref_mutex);
2035 put_device(&tape->dev);
2036 mutex_unlock(&idetape_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002037}
2038
Bartlomiej Zolnierkiewicz8fed4362009-02-25 20:28:24 +01002039static void ide_tape_release(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002040{
Bartlomiej Zolnierkiewicz8fed4362009-02-25 20:28:24 +01002041 struct ide_tape_obj *tape = to_ide_drv(dev, ide_tape_obj);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002042 ide_drive_t *drive = tape->drive;
2043 struct gendisk *g = tape->disk;
2044
Borislav Petkov01a63aeb2008-04-27 15:38:34 +02002045 BUG_ON(tape->merge_bh_size);
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02002046
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +02002047 drive->dev_flags &= ~IDE_DFLAG_DSC_OVERLAP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002048 drive->driver_data = NULL;
Tony Jonesdbc12722007-09-25 02:03:03 +02002049 device_destroy(idetape_sysfs_class, MKDEV(IDETAPE_MAJOR, tape->minor));
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002050 device_destroy(idetape_sysfs_class,
2051 MKDEV(IDETAPE_MAJOR, tape->minor + 128));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002052 idetape_devs[tape->minor] = NULL;
2053 g->private_data = NULL;
2054 put_disk(g);
2055 kfree(tape);
2056}
2057
Bartlomiej Zolnierkiewiczecfd80e2007-05-10 00:01:09 +02002058#ifdef CONFIG_IDE_PROC_FS
Linus Torvalds1da177e2005-04-16 15:20:36 -07002059static int proc_idetape_read_name
2060 (char *page, char **start, off_t off, int count, int *eof, void *data)
2061{
2062 ide_drive_t *drive = (ide_drive_t *) data;
2063 idetape_tape_t *tape = drive->driver_data;
2064 char *out = page;
2065 int len;
2066
2067 len = sprintf(out, "%s\n", tape->name);
2068 PROC_IDE_READ_RETURN(page, start, off, count, eof, len);
2069}
2070
2071static ide_proc_entry_t idetape_proc[] = {
2072 { "capacity", S_IFREG|S_IRUGO, proc_ide_read_capacity, NULL },
2073 { "name", S_IFREG|S_IRUGO, proc_idetape_read_name, NULL },
2074 { NULL, 0, NULL, NULL }
2075};
Bartlomiej Zolnierkiewicz79cb3802008-10-17 18:09:13 +02002076
2077static ide_proc_entry_t *ide_tape_proc_entries(ide_drive_t *drive)
2078{
2079 return idetape_proc;
2080}
2081
2082static const struct ide_proc_devset *ide_tape_proc_devsets(ide_drive_t *drive)
2083{
2084 return idetape_settings;
2085}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002086#endif
2087
Russell King4031bbe2006-01-06 11:41:00 +00002088static int ide_tape_probe(ide_drive_t *);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002089
Bartlomiej Zolnierkiewicz7f3c8682009-01-06 17:20:53 +01002090static struct ide_driver idetape_driver = {
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02002091 .gen_driver = {
Laurent Riffard4ef3b8f2005-11-18 22:15:40 +01002092 .owner = THIS_MODULE,
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02002093 .name = "ide-tape",
2094 .bus = &ide_bus_type,
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02002095 },
Russell King4031bbe2006-01-06 11:41:00 +00002096 .probe = ide_tape_probe,
2097 .remove = ide_tape_remove,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002098 .version = IDETAPE_VERSION,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002099 .do_request = idetape_do_request,
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +02002100#ifdef CONFIG_IDE_PROC_FS
Bartlomiej Zolnierkiewicz79cb3802008-10-17 18:09:13 +02002101 .proc_entries = ide_tape_proc_entries,
2102 .proc_devsets = ide_tape_proc_devsets,
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +02002103#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002104};
2105
Borislav Petkov3c98bf32008-02-06 02:57:53 +01002106/* Our character device supporting functions, passed to register_chrdev. */
Arjan van de Ven2b8693c2007-02-12 00:55:32 -08002107static const struct file_operations idetape_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002108 .owner = THIS_MODULE,
2109 .read = idetape_chrdev_read,
2110 .write = idetape_chrdev_write,
2111 .ioctl = idetape_chrdev_ioctl,
2112 .open = idetape_chrdev_open,
2113 .release = idetape_chrdev_release,
2114};
2115
Al Viroa4600f82008-03-02 10:27:58 -05002116static int idetape_open(struct block_device *bdev, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002117{
Al Viroa4600f82008-03-02 10:27:58 -05002118 struct ide_tape_obj *tape = ide_tape_get(bdev->bd_disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002119
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002120 if (!tape)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002121 return -ENXIO;
2122
Linus Torvalds1da177e2005-04-16 15:20:36 -07002123 return 0;
2124}
2125
Al Viroa4600f82008-03-02 10:27:58 -05002126static int idetape_release(struct gendisk *disk, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002127{
Borislav Petkov5aeddf92008-10-13 21:39:34 +02002128 struct ide_tape_obj *tape = ide_drv_g(disk, ide_tape_obj);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002129
2130 ide_tape_put(tape);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002131 return 0;
2132}
2133
Al Viroa4600f82008-03-02 10:27:58 -05002134static int idetape_ioctl(struct block_device *bdev, fmode_t mode,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002135 unsigned int cmd, unsigned long arg)
2136{
Borislav Petkov5aeddf92008-10-13 21:39:34 +02002137 struct ide_tape_obj *tape = ide_drv_g(bdev->bd_disk, ide_tape_obj);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002138 ide_drive_t *drive = tape->drive;
Al Viro1bddd9e2008-09-02 17:19:43 -04002139 int err = generic_ide_ioctl(drive, bdev, cmd, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002140 if (err == -EINVAL)
2141 err = idetape_blkdev_ioctl(drive, cmd, arg);
2142 return err;
2143}
2144
2145static struct block_device_operations idetape_block_ops = {
2146 .owner = THIS_MODULE,
Al Viroa4600f82008-03-02 10:27:58 -05002147 .open = idetape_open,
2148 .release = idetape_release,
2149 .locked_ioctl = idetape_ioctl,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002150};
2151
Russell King4031bbe2006-01-06 11:41:00 +00002152static int ide_tape_probe(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002153{
2154 idetape_tape_t *tape;
2155 struct gendisk *g;
2156 int minor;
2157
2158 if (!strstr("ide-tape", drive->driver_req))
2159 goto failed;
Bartlomiej Zolnierkiewicz2a924662008-10-10 22:39:24 +02002160
Linus Torvalds1da177e2005-04-16 15:20:36 -07002161 if (drive->media != ide_tape)
2162 goto failed;
Bartlomiej Zolnierkiewicz2a924662008-10-10 22:39:24 +02002163
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +02002164 if ((drive->dev_flags & IDE_DFLAG_ID_READ) &&
2165 ide_check_atapi_device(drive, DRV_NAME) == 0) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002166 printk(KERN_ERR "ide-tape: %s: not supported by this version of"
2167 " the driver\n", drive->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002168 goto failed;
2169 }
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002170 tape = kzalloc(sizeof(idetape_tape_t), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002171 if (tape == NULL) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002172 printk(KERN_ERR "ide-tape: %s: Can't allocate a tape struct\n",
2173 drive->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002174 goto failed;
2175 }
2176
2177 g = alloc_disk(1 << PARTN_BITS);
2178 if (!g)
2179 goto out_free_tape;
2180
2181 ide_init_disk(g, drive);
2182
Bartlomiej Zolnierkiewicz8fed4362009-02-25 20:28:24 +01002183 tape->dev.parent = &drive->gendev;
2184 tape->dev.release = ide_tape_release;
2185 dev_set_name(&tape->dev, dev_name(&drive->gendev));
2186
2187 if (device_register(&tape->dev))
2188 goto out_free_disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002189
2190 tape->drive = drive;
2191 tape->driver = &idetape_driver;
2192 tape->disk = g;
2193
2194 g->private_data = &tape->driver;
2195
2196 drive->driver_data = tape;
2197
Arjan van de Vencf8b8972006-03-23 03:00:45 -08002198 mutex_lock(&idetape_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002199 for (minor = 0; idetape_devs[minor]; minor++)
2200 ;
2201 idetape_devs[minor] = tape;
Arjan van de Vencf8b8972006-03-23 03:00:45 -08002202 mutex_unlock(&idetape_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002203
2204 idetape_setup(drive, tape, minor);
2205
Greg Kroah-Hartman3ee074b2008-07-21 20:03:34 -07002206 device_create(idetape_sysfs_class, &drive->gendev,
2207 MKDEV(IDETAPE_MAJOR, minor), NULL, "%s", tape->name);
2208 device_create(idetape_sysfs_class, &drive->gendev,
2209 MKDEV(IDETAPE_MAJOR, minor + 128), NULL,
2210 "n%s", tape->name);
Will Dysond5dee802005-09-16 02:55:07 -07002211
Linus Torvalds1da177e2005-04-16 15:20:36 -07002212 g->fops = &idetape_block_ops;
2213 ide_register_region(g);
2214
2215 return 0;
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02002216
Bartlomiej Zolnierkiewicz8fed4362009-02-25 20:28:24 +01002217out_free_disk:
2218 put_disk(g);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002219out_free_tape:
2220 kfree(tape);
2221failed:
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02002222 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002223}
2224
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002225static void __exit idetape_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002226{
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02002227 driver_unregister(&idetape_driver.gen_driver);
Will Dysond5dee802005-09-16 02:55:07 -07002228 class_destroy(idetape_sysfs_class);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002229 unregister_chrdev(IDETAPE_MAJOR, "ht");
2230}
2231
Bartlomiej Zolnierkiewicz17514e82005-11-19 22:24:35 +01002232static int __init idetape_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002233{
Will Dysond5dee802005-09-16 02:55:07 -07002234 int error = 1;
2235 idetape_sysfs_class = class_create(THIS_MODULE, "ide_tape");
2236 if (IS_ERR(idetape_sysfs_class)) {
2237 idetape_sysfs_class = NULL;
2238 printk(KERN_ERR "Unable to create sysfs class for ide tapes\n");
2239 error = -EBUSY;
2240 goto out;
2241 }
2242
Linus Torvalds1da177e2005-04-16 15:20:36 -07002243 if (register_chrdev(IDETAPE_MAJOR, "ht", &idetape_fops)) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002244 printk(KERN_ERR "ide-tape: Failed to register chrdev"
2245 " interface\n");
Will Dysond5dee802005-09-16 02:55:07 -07002246 error = -EBUSY;
2247 goto out_free_class;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002248 }
Will Dysond5dee802005-09-16 02:55:07 -07002249
2250 error = driver_register(&idetape_driver.gen_driver);
2251 if (error)
2252 goto out_free_driver;
2253
2254 return 0;
2255
2256out_free_driver:
2257 driver_unregister(&idetape_driver.gen_driver);
2258out_free_class:
2259 class_destroy(idetape_sysfs_class);
2260out:
2261 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002262}
2263
Kay Sievers263756e2005-12-12 18:03:44 +01002264MODULE_ALIAS("ide:*m-tape*");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002265module_init(idetape_init);
2266module_exit(idetape_exit);
2267MODULE_ALIAS_CHARDEV_MAJOR(IDETAPE_MAJOR);
Borislav Petkov9c145762008-02-06 02:57:54 +01002268MODULE_DESCRIPTION("ATAPI Streaming TAPE Driver");
2269MODULE_LICENSE("GPL");