blob: b2afbc7bcb6b19adb0dfb38469f4f9788b36914d [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
Bartlomiej Zolnierkiewiczd93bc452009-03-31 20:15:26 +0200295static int idetape_input_buffers(ide_drive_t *drive, struct ide_atapi_pc *pc,
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100296 unsigned int bcount)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297{
298 struct idetape_bh *bh = pc->bh;
299 int count;
300
Tejun Heo35ab8d32009-04-19 08:46:02 +0900301 if (bcount && bh) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100302 count = min(
303 (unsigned int)(bh->b_size - atomic_read(&bh->b_count)),
304 bcount);
Bartlomiej Zolnierkiewicz374e0422008-07-23 19:55:56 +0200305 drive->hwif->tp_ops->input_data(drive, NULL, bh->b_data +
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100306 atomic_read(&bh->b_count), count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 bcount -= count;
308 atomic_add(count, &bh->b_count);
Tejun Heo35ab8d32009-04-19 08:46:02 +0900309 if (atomic_read(&bh->b_count) == bh->b_size)
310 pc->bh = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 }
Bartlomiej Zolnierkiewiczd93bc452009-03-31 20:15:26 +0200312
Bartlomiej Zolnierkiewiczd93bc452009-03-31 20:15:26 +0200313 return bcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314}
315
Bartlomiej Zolnierkiewiczd93bc452009-03-31 20:15:26 +0200316static int idetape_output_buffers(ide_drive_t *drive, struct ide_atapi_pc *pc,
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100317 unsigned int bcount)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318{
319 struct idetape_bh *bh = pc->bh;
320 int count;
321
Tejun Heo35ab8d32009-04-19 08:46:02 +0900322 if (bcount && bh) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 count = min((unsigned int)pc->b_count, (unsigned int)bcount);
Bartlomiej Zolnierkiewicz374e0422008-07-23 19:55:56 +0200324 drive->hwif->tp_ops->output_data(drive, NULL, pc->b_data, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 bcount -= count;
326 pc->b_data += count;
327 pc->b_count -= count;
Tejun Heo35ab8d32009-04-19 08:46:02 +0900328 if (!pc->b_count)
329 pc->bh = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 }
Bartlomiej Zolnierkiewiczd93bc452009-03-31 20:15:26 +0200331
332 return bcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333}
334
Bartlomiej Zolnierkiewicz646c0cb2008-07-15 21:22:03 +0200335static void idetape_update_buffers(ide_drive_t *drive, struct ide_atapi_pc *pc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336{
337 struct idetape_bh *bh = pc->bh;
Borislav Petkovd236d742008-04-18 00:46:27 +0200338 unsigned int bcount = pc->xferred;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339
Borislav Petkov346331f2008-04-18 00:46:26 +0200340 if (pc->flags & PC_FLAG_WRITING)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 return;
Tejun Heo35ab8d32009-04-19 08:46:02 +0900342 if (bcount) {
343 if (bh == NULL || bcount > bh->b_size) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100344 printk(KERN_ERR "ide-tape: bh == NULL in %s\n",
345 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 return;
347 }
Tejun Heo35ab8d32009-04-19 08:46:02 +0900348 atomic_set(&bh->b_count, bcount);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 if (atomic_read(&bh->b_count) == bh->b_size)
Tejun Heo35ab8d32009-04-19 08:46:02 +0900350 pc->bh = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352}
353
354/*
Borislav Petkov1b5db432008-02-02 19:56:48 +0100355 * called on each failed packet command retry to analyze the request sense. We
356 * currently do not utilize this information.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 */
Borislav Petkov1b5db432008-02-02 19:56:48 +0100358static void idetape_analyze_error(ide_drive_t *drive, u8 *sense)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359{
360 idetape_tape_t *tape = drive->driver_data;
Bartlomiej Zolnierkiewicz5e2040fd2009-03-27 12:46:34 +0100361 struct ide_atapi_pc *pc = drive->failed_pc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362
Borislav Petkov1b5db432008-02-02 19:56:48 +0100363 tape->sense_key = sense[2] & 0xF;
364 tape->asc = sense[12];
365 tape->ascq = sense[13];
Borislav Petkov8004a8c2008-02-06 02:57:51 +0100366
367 debug_log(DBG_ERR, "pc = %x, sense key = %x, asc = %x, ascq = %x\n",
368 pc->c[0], tape->sense_key, tape->asc, tape->ascq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369
Borislav Petkovd236d742008-04-18 00:46:27 +0200370 /* Correct pc->xferred by asking the tape. */
Borislav Petkov346331f2008-04-18 00:46:26 +0200371 if (pc->flags & PC_FLAG_DMA_ERROR) {
Borislav Petkovd236d742008-04-18 00:46:27 +0200372 pc->xferred = pc->req_xfer -
Borislav Petkov54bb2072008-02-06 02:57:52 +0100373 tape->blk_size *
Harvey Harrison5d0cc8a2008-07-15 21:21:41 +0200374 get_unaligned_be32(&sense[3]);
Bartlomiej Zolnierkiewicz646c0cb2008-07-15 21:22:03 +0200375 idetape_update_buffers(drive, pc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 }
377
378 /*
379 * If error was the result of a zero-length read or write command,
380 * with sense key=5, asc=0x22, ascq=0, let it slide. Some drives
381 * (i.e. Seagate STT3401A Travan) don't support 0-length read/writes.
382 */
Borislav Petkov90699ce2008-02-02 19:56:50 +0100383 if ((pc->c[0] == READ_6 || pc->c[0] == WRITE_6)
Borislav Petkov1b5db432008-02-02 19:56:48 +0100384 /* length == 0 */
385 && pc->c[4] == 0 && pc->c[3] == 0 && pc->c[2] == 0) {
386 if (tape->sense_key == 5) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 /* don't report an error, everything's ok */
388 pc->error = 0;
389 /* don't retry read/write */
Borislav Petkov346331f2008-04-18 00:46:26 +0200390 pc->flags |= PC_FLAG_ABORT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 }
392 }
Borislav Petkov90699ce2008-02-02 19:56:50 +0100393 if (pc->c[0] == READ_6 && (sense[2] & 0x80)) {
Bartlomiej Zolnierkiewiczc152cc12009-03-27 12:46:34 +0100394 pc->error = IDE_DRV_ERROR_FILEMARK;
Borislav Petkov346331f2008-04-18 00:46:26 +0200395 pc->flags |= PC_FLAG_ABORT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 }
Borislav Petkov90699ce2008-02-02 19:56:50 +0100397 if (pc->c[0] == WRITE_6) {
Borislav Petkov1b5db432008-02-02 19:56:48 +0100398 if ((sense[2] & 0x40) || (tape->sense_key == 0xd
399 && tape->asc == 0x0 && tape->ascq == 0x2)) {
Bartlomiej Zolnierkiewiczc152cc12009-03-27 12:46:34 +0100400 pc->error = IDE_DRV_ERROR_EOD;
Borislav Petkov346331f2008-04-18 00:46:26 +0200401 pc->flags |= PC_FLAG_ABORT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 }
403 }
Borislav Petkov90699ce2008-02-02 19:56:50 +0100404 if (pc->c[0] == READ_6 || pc->c[0] == WRITE_6) {
Borislav Petkov1b5db432008-02-02 19:56:48 +0100405 if (tape->sense_key == 8) {
Bartlomiej Zolnierkiewiczc152cc12009-03-27 12:46:34 +0100406 pc->error = IDE_DRV_ERROR_EOD;
Borislav Petkov346331f2008-04-18 00:46:26 +0200407 pc->flags |= PC_FLAG_ABORT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 }
Borislav Petkov346331f2008-04-18 00:46:26 +0200409 if (!(pc->flags & PC_FLAG_ABORT) &&
Borislav Petkovd236d742008-04-18 00:46:27 +0200410 pc->xferred)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 pc->retries = IDETAPE_MAX_PC_RETRIES + 1;
412 }
413}
414
Borislav Petkovd01dbc32008-04-27 15:38:33 +0200415/* Free data buffers completely. */
Borislav Petkov077e3bd2008-04-27 15:38:34 +0200416static void ide_tape_kfree_buffer(idetape_tape_t *tape)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417{
Tejun Heo35ab8d32009-04-19 08:46:02 +0900418 struct idetape_bh *bh = tape->merge_bh;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419
Tejun Heo35ab8d32009-04-19 08:46:02 +0900420 kfree(bh->b_data);
421 kfree(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422}
423
Bartlomiej Zolnierkiewiczb14c7212008-10-13 21:39:30 +0200424static void ide_tape_handle_dsc(ide_drive_t *);
425
Bartlomiej Zolnierkiewicz03a2faa2009-03-27 12:46:36 +0100426static int ide_tape_callback(ide_drive_t *drive, int dsc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427{
428 idetape_tape_t *tape = drive->driver_data;
Bartlomiej Zolnierkiewicz2b9efba2008-10-13 21:39:31 +0200429 struct ide_atapi_pc *pc = drive->pc;
Bartlomiej Zolnierkiewicz313afea2009-03-27 12:46:34 +0100430 struct request *rq = drive->hwif->rq;
Bartlomiej Zolnierkiewicz5985e6a2008-07-15 21:21:55 +0200431 int uptodate = pc->error ? 0 : 1;
Bartlomiej Zolnierkiewicz313afea2009-03-27 12:46:34 +0100432 int err = uptodate ? 0 : IDE_DRV_ERROR_GENERAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433
Borislav Petkov8004a8c2008-02-06 02:57:51 +0100434 debug_log(DBG_PROCS, "Enter %s\n", __func__);
435
Bartlomiej Zolnierkiewiczb14c7212008-10-13 21:39:30 +0200436 if (dsc)
437 ide_tape_handle_dsc(drive);
438
Bartlomiej Zolnierkiewicz5e2040fd2009-03-27 12:46:34 +0100439 if (drive->failed_pc == pc)
440 drive->failed_pc = NULL;
Bartlomiej Zolnierkiewiczdd2e9a02008-07-15 21:22:01 +0200441
Bartlomiej Zolnierkiewicz5985e6a2008-07-15 21:21:55 +0200442 if (pc->c[0] == REQUEST_SENSE) {
443 if (uptodate)
444 idetape_analyze_error(drive, pc->buf);
445 else
446 printk(KERN_ERR "ide-tape: Error in REQUEST SENSE "
447 "itself - Aborting request!\n");
448 } else if (pc->c[0] == READ_6 || pc->c[0] == WRITE_6) {
Bartlomiej Zolnierkiewicz5985e6a2008-07-15 21:21:55 +0200449 int blocks = pc->xferred / tape->blk_size;
450
451 tape->avg_size += blocks * tape->blk_size;
452
453 if (time_after_eq(jiffies, tape->avg_time + HZ)) {
454 tape->avg_speed = tape->avg_size * HZ /
455 (jiffies - tape->avg_time) / 1024;
456 tape->avg_size = 0;
457 tape->avg_time = jiffies;
458 }
459
460 tape->first_frame += blocks;
461 rq->current_nr_sectors -= blocks;
462
Bartlomiej Zolnierkiewicz313afea2009-03-27 12:46:34 +0100463 if (pc->error) {
464 uptodate = 0;
465 err = pc->error;
466 }
Bartlomiej Zolnierkiewicz5985e6a2008-07-15 21:21:55 +0200467 } else if (pc->c[0] == READ_POSITION && uptodate) {
Bartlomiej Zolnierkiewicz2b9efba2008-10-13 21:39:31 +0200468 u8 *readpos = pc->buf;
Bartlomiej Zolnierkiewicz5985e6a2008-07-15 21:21:55 +0200469
470 debug_log(DBG_SENSE, "BOP - %s\n",
471 (readpos[0] & 0x80) ? "Yes" : "No");
472 debug_log(DBG_SENSE, "EOP - %s\n",
473 (readpos[0] & 0x40) ? "Yes" : "No");
474
475 if (readpos[0] & 0x4) {
476 printk(KERN_INFO "ide-tape: Block location is unknown"
477 "to the tape\n");
Borislav Petkovf2e3ab52008-07-23 19:56:01 +0200478 clear_bit(IDE_AFLAG_ADDRESS_VALID, &drive->atapi_flags);
Bartlomiej Zolnierkiewicz5985e6a2008-07-15 21:21:55 +0200479 uptodate = 0;
Bartlomiej Zolnierkiewicz313afea2009-03-27 12:46:34 +0100480 err = IDE_DRV_ERROR_GENERAL;
Bartlomiej Zolnierkiewicz5985e6a2008-07-15 21:21:55 +0200481 } else {
482 debug_log(DBG_SENSE, "Block Location - %u\n",
Harvey Harrisoncd740ab2008-07-24 22:53:33 +0200483 be32_to_cpup((__be32 *)&readpos[4]));
Bartlomiej Zolnierkiewicz5985e6a2008-07-15 21:21:55 +0200484
485 tape->partition = readpos[1];
Harvey Harrisoncd740ab2008-07-24 22:53:33 +0200486 tape->first_frame = be32_to_cpup((__be32 *)&readpos[4]);
Borislav Petkovf2e3ab52008-07-23 19:56:01 +0200487 set_bit(IDE_AFLAG_ADDRESS_VALID, &drive->atapi_flags);
Bartlomiej Zolnierkiewicz5985e6a2008-07-15 21:21:55 +0200488 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 }
Bartlomiej Zolnierkiewicz5985e6a2008-07-15 21:21:55 +0200490
Bartlomiej Zolnierkiewicz313afea2009-03-27 12:46:34 +0100491 rq->errors = err;
492
Bartlomiej Zolnierkiewicz03a2faa2009-03-27 12:46:36 +0100493 return uptodate;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494}
495
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100497 * Postpone the current request so that ide.c will be able to service requests
Bartlomiej Zolnierkiewiczb65fac32009-01-06 17:20:50 +0100498 * from another device on the same port while we are polling for DSC.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100500static void idetape_postpone_request(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501{
502 idetape_tape_t *tape = drive->driver_data;
503
Borislav Petkov8004a8c2008-02-06 02:57:51 +0100504 debug_log(DBG_PROCS, "Enter %s\n", __func__);
505
Bartlomiej Zolnierkiewiczb65fac32009-01-06 17:20:50 +0100506 tape->postponed_rq = drive->hwif->rq;
507
Borislav Petkov54bb2072008-02-06 02:57:52 +0100508 ide_stall_queue(drive, tape->dsc_poll_freq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509}
510
Bartlomiej Zolnierkiewicz74e63e742008-07-15 21:22:01 +0200511static void ide_tape_handle_dsc(ide_drive_t *drive)
512{
513 idetape_tape_t *tape = drive->driver_data;
514
515 /* Media access command */
516 tape->dsc_polling_start = jiffies;
517 tape->dsc_poll_freq = IDETAPE_DSC_MA_FAST;
518 tape->dsc_timeout = jiffies + IDETAPE_DSC_MA_TIMEOUT;
519 /* Allow ide.c to handle other requests */
520 idetape_postpone_request(drive);
521}
522
Bartlomiej Zolnierkiewiczacaa0f52008-10-10 22:39:36 +0200523static int ide_tape_io_buffers(ide_drive_t *drive, struct ide_atapi_pc *pc,
Bartlomiej Zolnierkiewicz08424ac2008-07-15 21:22:01 +0200524 unsigned int bcount, int write)
525{
Bartlomiej Zolnierkiewiczd93bc452009-03-31 20:15:26 +0200526 unsigned int bleft;
Bartlomiej Zolnierkiewiczacaa0f52008-10-10 22:39:36 +0200527
Bartlomiej Zolnierkiewiczd93bc452009-03-31 20:15:26 +0200528 if (write)
529 bleft = idetape_output_buffers(drive, pc, bcount);
530 else
531 bleft = idetape_input_buffers(drive, pc, bcount);
532
533 return bcount - bleft;
Bartlomiej Zolnierkiewicz08424ac2008-07-15 21:22:01 +0200534}
Borislav Petkova1efc852008-02-06 02:57:52 +0100535
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100537 * Packet Command Interface
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 *
Bartlomiej Zolnierkiewicz2b9efba2008-10-13 21:39:31 +0200539 * The current Packet Command is available in drive->pc, and will not change
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100540 * until we finish handling it. Each packet command is associated with a
541 * callback function that will be called when the command is finished.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100543 * The handling will be done in three stages:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 *
Bartlomiej Zolnierkiewiczb788ee92009-03-27 12:46:46 +0100545 * 1. ide_tape_issue_pc will send the packet command to the drive, and will set
Bartlomiej Zolnierkiewiczaa5d2de72008-10-13 21:39:32 +0200546 * the interrupt handler to ide_pc_intr.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 *
Bartlomiej Zolnierkiewiczaa5d2de72008-10-13 21:39:32 +0200548 * 2. On each interrupt, ide_pc_intr will be called. This step will be
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100549 * repeated until the device signals us that no more interrupts will be issued.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100551 * 3. ATAPI Tape media access commands have immediate status with a delayed
552 * process. In case of a successful initiation of a media access packet command,
553 * the DSC bit will be set when the actual execution of the command is finished.
554 * Since the tape drive will not issue an interrupt, we have to poll for this
555 * event. In this case, we define the request as "low priority request" by
556 * setting rq_status to IDETAPE_RQ_POSTPONED, set a timer to poll for DSC and
557 * exit the driver.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100559 * ide.c will then give higher priority to requests which originate from the
560 * other device, until will change rq_status to RQ_ACTIVE.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100562 * 4. When the packet command is finished, it will be checked for errors.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100564 * 5. In case an error was found, we queue a request sense packet command in
565 * front of the request queue and retry the operation up to
566 * IDETAPE_MAX_PC_RETRIES times.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100568 * 6. In case no error was found, or we decided to give up and not to retry
569 * again, the callback function will be called and then we will handle the next
570 * request.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572
Bartlomiej Zolnierkiewiczb788ee92009-03-27 12:46:46 +0100573static ide_startstop_t ide_tape_issue_pc(ide_drive_t *drive,
574 struct ide_cmd *cmd,
575 struct ide_atapi_pc *pc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 idetape_tape_t *tape = drive->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578
Bartlomiej Zolnierkiewicz5e2040fd2009-03-27 12:46:34 +0100579 if (drive->failed_pc == NULL && pc->c[0] != REQUEST_SENSE)
580 drive->failed_pc = pc;
Bartlomiej Zolnierkiewicz2b9efba2008-10-13 21:39:31 +0200581
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 /* Set the current packet command */
Bartlomiej Zolnierkiewicz2b9efba2008-10-13 21:39:31 +0200583 drive->pc = pc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584
585 if (pc->retries > IDETAPE_MAX_PC_RETRIES ||
Borislav Petkov346331f2008-04-18 00:46:26 +0200586 (pc->flags & PC_FLAG_ABORT)) {
Tejun Heob3071d12009-04-19 08:46:02 +0900587 unsigned int done = blk_rq_bytes(drive->hwif->rq);
588
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 /*
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100590 * We will "abort" retrying a packet command in case legitimate
591 * error code was received (crossing a filemark, or end of the
592 * media, for example).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 */
Borislav Petkov346331f2008-04-18 00:46:26 +0200594 if (!(pc->flags & PC_FLAG_ABORT)) {
Borislav Petkov90699ce2008-02-02 19:56:50 +0100595 if (!(pc->c[0] == TEST_UNIT_READY &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 tape->sense_key == 2 && tape->asc == 4 &&
597 (tape->ascq == 1 || tape->ascq == 8))) {
598 printk(KERN_ERR "ide-tape: %s: I/O error, "
599 "pc = %2x, key = %2x, "
600 "asc = %2x, ascq = %2x\n",
601 tape->name, pc->c[0],
602 tape->sense_key, tape->asc,
603 tape->ascq);
604 }
605 /* Giving up */
Bartlomiej Zolnierkiewiczc152cc12009-03-27 12:46:34 +0100606 pc->error = IDE_DRV_ERROR_GENERAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607 }
Tejun Heob3071d12009-04-19 08:46:02 +0900608
Bartlomiej Zolnierkiewicz5e2040fd2009-03-27 12:46:34 +0100609 drive->failed_pc = NULL;
Bartlomiej Zolnierkiewiczb14c7212008-10-13 21:39:30 +0200610 drive->pc_callback(drive, 0);
Tejun Heob3071d12009-04-19 08:46:02 +0900611 ide_complete_rq(drive, -EIO, done);
Bartlomiej Zolnierkiewicz92f5daf2008-07-15 21:21:55 +0200612 return ide_stopped;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 }
Borislav Petkov8004a8c2008-02-06 02:57:51 +0100614 debug_log(DBG_SENSE, "Retry #%d, cmd = %02X\n", pc->retries, pc->c[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615
616 pc->retries++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617
Bartlomiej Zolnierkiewiczb788ee92009-03-27 12:46:46 +0100618 return ide_issue_pc(drive, cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619}
620
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100621/* A mode sense command is used to "sense" tape parameters. */
Borislav Petkovd236d742008-04-18 00:46:27 +0200622static void idetape_create_mode_sense_cmd(struct ide_atapi_pc *pc, u8 page_code)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623{
Bartlomiej Zolnierkiewicz7bf74202008-10-10 22:39:37 +0200624 ide_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +0100625 pc->c[0] = MODE_SENSE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626 if (page_code != IDETAPE_BLOCK_DESCRIPTOR)
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100627 /* DBD = 1 - Don't return block descriptors */
628 pc->c[1] = 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 pc->c[2] = page_code;
630 /*
631 * Changed pc->c[3] to 0 (255 will at best return unused info).
632 *
633 * For SCSI this byte is defined as subpage instead of high byte
634 * of length and some IDE drives seem to interpret it this way
635 * and return an error when 255 is used.
636 */
637 pc->c[3] = 0;
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100638 /* We will just discard data in that case */
639 pc->c[4] = 255;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 if (page_code == IDETAPE_BLOCK_DESCRIPTOR)
Borislav Petkovd236d742008-04-18 00:46:27 +0200641 pc->req_xfer = 12;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 else if (page_code == IDETAPE_CAPABILITIES_PAGE)
Borislav Petkovd236d742008-04-18 00:46:27 +0200643 pc->req_xfer = 24;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 else
Borislav Petkovd236d742008-04-18 00:46:27 +0200645 pc->req_xfer = 50;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646}
647
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100648static ide_startstop_t idetape_media_access_finished(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649{
Bartlomiej Zolnierkiewiczb73c7ee2008-07-23 19:55:52 +0200650 ide_hwif_t *hwif = drive->hwif;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 idetape_tape_t *tape = drive->driver_data;
Bartlomiej Zolnierkiewicz2b9efba2008-10-13 21:39:31 +0200652 struct ide_atapi_pc *pc = drive->pc;
Bartlomiej Zolnierkiewicz22c525b2008-01-25 22:17:11 +0100653 u8 stat;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654
Bartlomiej Zolnierkiewicz374e0422008-07-23 19:55:56 +0200655 stat = hwif->tp_ops->read_status(hwif);
Bartlomiej Zolnierkiewiczc47137a2008-02-06 02:57:51 +0100656
Bartlomiej Zolnierkiewicz3a7d2482008-10-10 22:39:21 +0200657 if (stat & ATA_DSC) {
658 if (stat & ATA_ERR) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659 /* Error detected */
Borislav Petkov90699ce2008-02-02 19:56:50 +0100660 if (pc->c[0] != TEST_UNIT_READY)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661 printk(KERN_ERR "ide-tape: %s: I/O error, ",
662 tape->name);
663 /* Retry operation */
Borislav Petkov6b544fc2009-04-19 07:00:42 +0900664 ide_retry_pc(drive);
Bartlomiej Zolnierkiewicz258ec412008-07-15 21:21:55 +0200665 return ide_stopped;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666 }
667 pc->error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668 } else {
Bartlomiej Zolnierkiewiczc152cc12009-03-27 12:46:34 +0100669 pc->error = IDE_DRV_ERROR_GENERAL;
Bartlomiej Zolnierkiewicz5e2040fd2009-03-27 12:46:34 +0100670 drive->failed_pc = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671 }
Bartlomiej Zolnierkiewiczb14c7212008-10-13 21:39:30 +0200672 drive->pc_callback(drive, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673 return ide_stopped;
674}
675
Borislav Petkovcd2abbf2008-07-15 21:22:03 +0200676static void ide_tape_create_rw_cmd(idetape_tape_t *tape,
Borislav Petkov0014c752008-07-23 19:56:00 +0200677 struct ide_atapi_pc *pc, struct request *rq,
678 u8 opcode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679{
Borislav Petkov0014c752008-07-23 19:56:00 +0200680 struct idetape_bh *bh = (struct idetape_bh *)rq->special;
681 unsigned int length = rq->current_nr_sectors;
682
Bartlomiej Zolnierkiewicz7bf74202008-10-10 22:39:37 +0200683 ide_init_pc(pc);
Borislav Petkov860ff5e2008-02-02 19:56:50 +0100684 put_unaligned(cpu_to_be32(length), (unsigned int *) &pc->c[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 pc->c[1] = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686 pc->bh = bh;
Borislav Petkovd236d742008-04-18 00:46:27 +0200687 pc->buf = NULL;
688 pc->buf_size = length * tape->blk_size;
689 pc->req_xfer = pc->buf_size;
Borislav Petkovf73850a2008-04-27 15:38:33 +0200690 if (pc->req_xfer == tape->buffer_size)
Bartlomiej Zolnierkiewicz5e331092008-07-15 21:21:56 +0200691 pc->flags |= PC_FLAG_DMA_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692
Borislav Petkovcd2abbf2008-07-15 21:22:03 +0200693 if (opcode == READ_6) {
694 pc->c[0] = READ_6;
695 atomic_set(&bh->b_count, 0);
696 } else if (opcode == WRITE_6) {
697 pc->c[0] = WRITE_6;
698 pc->flags |= PC_FLAG_WRITING;
699 pc->b_data = bh->b_data;
700 pc->b_count = atomic_read(&bh->b_count);
701 }
Borislav Petkov0014c752008-07-23 19:56:00 +0200702
703 memcpy(rq->cmd, pc->c, 12);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704}
705
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706static ide_startstop_t idetape_do_request(ide_drive_t *drive,
707 struct request *rq, sector_t block)
708{
Bartlomiej Zolnierkiewiczb73c7ee2008-07-23 19:55:52 +0200709 ide_hwif_t *hwif = drive->hwif;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710 idetape_tape_t *tape = drive->driver_data;
Borislav Petkovd236d742008-04-18 00:46:27 +0200711 struct ide_atapi_pc *pc = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712 struct request *postponed_rq = tape->postponed_rq;
Bartlomiej Zolnierkiewiczb788ee92009-03-27 12:46:46 +0100713 struct ide_cmd cmd;
Bartlomiej Zolnierkiewicz22c525b2008-01-25 22:17:11 +0100714 u8 stat;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715
Mark de Wever730616b2008-10-10 22:39:17 +0200716 debug_log(DBG_SENSE, "sector: %llu, nr_sectors: %lu,"
717 " current_nr_sectors: %u\n",
718 (unsigned long long)rq->sector, rq->nr_sectors,
719 rq->current_nr_sectors);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720
Borislav Petkov6b544fc2009-04-19 07:00:42 +0900721 if (!(blk_special_request(rq) || blk_sense_request(rq))) {
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100722 /* We do not support buffer cache originated requests. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 printk(KERN_NOTICE "ide-tape: %s: Unsupported request in "
Jens Axboe4aff5e22006-08-10 08:44:47 +0200724 "request queue (%d)\n", drive->name, rq->cmd_type);
Bartlomiej Zolnierkiewicz89f78b322009-03-27 12:46:43 +0100725 if (blk_fs_request(rq) == 0 && rq->errors == 0)
726 rq->errors = -EIO;
Bartlomiej Zolnierkiewicz130e8862009-03-27 12:46:45 +0100727 ide_complete_rq(drive, -EIO, ide_rq_bytes(rq));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 return ide_stopped;
729 }
730
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100731 /* Retry a failed packet command */
Bartlomiej Zolnierkiewicz5e2040fd2009-03-27 12:46:34 +0100732 if (drive->failed_pc && drive->pc->c[0] == REQUEST_SENSE) {
733 pc = drive->failed_pc;
Bartlomiej Zolnierkiewicz28c72142008-07-15 21:21:59 +0200734 goto out;
735 }
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100736
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737 if (postponed_rq != NULL)
738 if (rq != postponed_rq) {
739 printk(KERN_ERR "ide-tape: ide-tape.c bug - "
740 "Two DSC requests were queued\n");
Bartlomiej Zolnierkiewicz313afea2009-03-27 12:46:34 +0100741 drive->failed_pc = NULL;
Bartlomiej Zolnierkiewicz6902a532009-03-27 12:46:43 +0100742 rq->errors = 0;
Bartlomiej Zolnierkiewiczf974b192009-03-27 12:46:44 +0100743 ide_complete_rq(drive, 0, blk_rq_bytes(rq));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744 return ide_stopped;
745 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746
747 tape->postponed_rq = NULL;
748
749 /*
750 * If the tape is still busy, postpone our request and service
751 * the other device meanwhile.
752 */
Bartlomiej Zolnierkiewicz374e0422008-07-23 19:55:56 +0200753 stat = hwif->tp_ops->read_status(hwif);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +0200755 if ((drive->dev_flags & IDE_DFLAG_DSC_OVERLAP) == 0 &&
756 (rq->cmd[13] & REQ_IDETAPE_PC2) == 0)
Borislav Petkovf2e3ab52008-07-23 19:56:01 +0200757 set_bit(IDE_AFLAG_IGNORE_DSC, &drive->atapi_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +0200759 if (drive->dev_flags & IDE_DFLAG_POST_RESET) {
Borislav Petkovf2e3ab52008-07-23 19:56:01 +0200760 set_bit(IDE_AFLAG_IGNORE_DSC, &drive->atapi_flags);
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +0200761 drive->dev_flags &= ~IDE_DFLAG_POST_RESET;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762 }
763
Borislav Petkovf2e3ab52008-07-23 19:56:01 +0200764 if (!test_and_clear_bit(IDE_AFLAG_IGNORE_DSC, &drive->atapi_flags) &&
Bartlomiej Zolnierkiewicz3a7d2482008-10-10 22:39:21 +0200765 (stat & ATA_DSC) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766 if (postponed_rq == NULL) {
767 tape->dsc_polling_start = jiffies;
Borislav Petkov54bb2072008-02-06 02:57:52 +0100768 tape->dsc_poll_freq = tape->best_dsc_rw_freq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769 tape->dsc_timeout = jiffies + IDETAPE_DSC_RW_TIMEOUT;
770 } else if (time_after(jiffies, tape->dsc_timeout)) {
771 printk(KERN_ERR "ide-tape: %s: DSC timeout\n",
772 tape->name);
Borislav Petkov83dd5732008-07-23 19:56:00 +0200773 if (rq->cmd[13] & REQ_IDETAPE_PC2) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774 idetape_media_access_finished(drive);
775 return ide_stopped;
776 } else {
777 return ide_do_reset(drive);
778 }
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100779 } else if (time_after(jiffies,
780 tape->dsc_polling_start +
781 IDETAPE_DSC_MA_THRESHOLD))
Borislav Petkov54bb2072008-02-06 02:57:52 +0100782 tape->dsc_poll_freq = IDETAPE_DSC_MA_SLOW;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 idetape_postpone_request(drive);
784 return ide_stopped;
785 }
Borislav Petkov83dd5732008-07-23 19:56:00 +0200786 if (rq->cmd[13] & REQ_IDETAPE_READ) {
Bartlomiej Zolnierkiewicz2e8a6f82008-10-10 22:39:36 +0200787 pc = &tape->queued_pc;
Borislav Petkov0014c752008-07-23 19:56:00 +0200788 ide_tape_create_rw_cmd(tape, pc, rq, READ_6);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789 goto out;
790 }
Borislav Petkov83dd5732008-07-23 19:56:00 +0200791 if (rq->cmd[13] & REQ_IDETAPE_WRITE) {
Bartlomiej Zolnierkiewicz2e8a6f82008-10-10 22:39:36 +0200792 pc = &tape->queued_pc;
Borislav Petkov0014c752008-07-23 19:56:00 +0200793 ide_tape_create_rw_cmd(tape, pc, rq, WRITE_6);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794 goto out;
795 }
Borislav Petkov83dd5732008-07-23 19:56:00 +0200796 if (rq->cmd[13] & REQ_IDETAPE_PC1) {
Tejun Heoc267cc12009-04-19 07:00:42 +0900797 pc = (struct ide_atapi_pc *)rq->special;
Borislav Petkov83dd5732008-07-23 19:56:00 +0200798 rq->cmd[13] &= ~(REQ_IDETAPE_PC1);
799 rq->cmd[13] |= REQ_IDETAPE_PC2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800 goto out;
801 }
Borislav Petkov83dd5732008-07-23 19:56:00 +0200802 if (rq->cmd[13] & REQ_IDETAPE_PC2) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803 idetape_media_access_finished(drive);
804 return ide_stopped;
805 }
806 BUG();
Bartlomiej Zolnierkiewicz28c72142008-07-15 21:21:59 +0200807
Borislav Petkovf2e3ab52008-07-23 19:56:01 +0200808out:
Borislav Petkov6b544fc2009-04-19 07:00:42 +0900809 /* prepare sense request for this command */
810 ide_prep_sense(drive, rq);
811
Bartlomiej Zolnierkiewiczb788ee92009-03-27 12:46:46 +0100812 memset(&cmd, 0, sizeof(cmd));
813
814 if (rq_data_dir(rq))
815 cmd.tf_flags |= IDE_TFLAG_WRITE;
816
817 cmd.rq = rq;
818
Tejun Heo5c4be572009-04-19 07:00:42 +0900819 ide_init_sg_cmd(&cmd, pc->req_xfer);
820 ide_map_sg(drive, &cmd);
821
Bartlomiej Zolnierkiewiczb788ee92009-03-27 12:46:46 +0100822 return ide_tape_issue_pc(drive, &cmd, pc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823}
824
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825/*
Tejun Heo35ab8d32009-04-19 08:46:02 +0900826 * It returns a pointer to the newly allocated buffer, or NULL in case
827 * of failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828 */
Borislav Petkov077e3bd2008-04-27 15:38:34 +0200829static struct idetape_bh *ide_tape_kmalloc_buffer(idetape_tape_t *tape,
Tejun Heo35ab8d32009-04-19 08:46:02 +0900830 int full)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831{
Tejun Heo35ab8d32009-04-19 08:46:02 +0900832 struct idetape_bh *bh;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833
Tejun Heo35ab8d32009-04-19 08:46:02 +0900834 bh = kmalloc(sizeof(struct idetape_bh), GFP_KERNEL);
835 if (!bh)
836 return NULL;
Borislav Petkov41aa1702008-04-27 15:38:32 +0200837
Tejun Heo35ab8d32009-04-19 08:46:02 +0900838 bh->b_data = kmalloc(tape->buffer_size, GFP_KERNEL);
839 if (!bh->b_data) {
840 kfree(bh);
841 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842 }
Borislav Petkov41aa1702008-04-27 15:38:32 +0200843
Tejun Heo35ab8d32009-04-19 08:46:02 +0900844 bh->b_size = tape->buffer_size;
845 atomic_set(&bh->b_count, full ? bh->b_size : 0);
846
847 return bh;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848}
849
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100850static int idetape_copy_stage_from_user(idetape_tape_t *tape,
Borislav Petkov8646c882008-04-27 15:38:26 +0200851 const char __user *buf, int n)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852{
853 struct idetape_bh *bh = tape->bh;
Daniel Walkerdcd96372006-06-25 05:47:37 -0700854 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855
Tejun Heo35ab8d32009-04-19 08:46:02 +0900856 if (n) {
857 if (bh == NULL || n > bh->b_size - atomic_read(&bh->b_count)) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100858 printk(KERN_ERR "ide-tape: bh == NULL in %s\n",
859 __func__);
Daniel Walkerdcd96372006-06-25 05:47:37 -0700860 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861 }
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100862 if (copy_from_user(bh->b_data + atomic_read(&bh->b_count), buf,
Tejun Heo35ab8d32009-04-19 08:46:02 +0900863 n))
Daniel Walkerdcd96372006-06-25 05:47:37 -0700864 ret = 1;
Tejun Heo35ab8d32009-04-19 08:46:02 +0900865 atomic_add(n, &bh->b_count);
866 if (atomic_read(&bh->b_count) == bh->b_size)
867 tape->bh = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868 }
Tejun Heo35ab8d32009-04-19 08:46:02 +0900869
Daniel Walkerdcd96372006-06-25 05:47:37 -0700870 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871}
872
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100873static int idetape_copy_stage_to_user(idetape_tape_t *tape, char __user *buf,
Borislav Petkov99d74e62008-04-27 15:38:25 +0200874 int n)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875{
876 struct idetape_bh *bh = tape->bh;
Daniel Walkerdcd96372006-06-25 05:47:37 -0700877 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878
Tejun Heo35ab8d32009-04-19 08:46:02 +0900879 if (n) {
880 if (bh == NULL || n > tape->b_count) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100881 printk(KERN_ERR "ide-tape: bh == NULL in %s\n",
882 __func__);
Daniel Walkerdcd96372006-06-25 05:47:37 -0700883 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884 }
Tejun Heo35ab8d32009-04-19 08:46:02 +0900885 if (copy_to_user(buf, tape->b_data, n))
Daniel Walkerdcd96372006-06-25 05:47:37 -0700886 ret = 1;
Tejun Heo35ab8d32009-04-19 08:46:02 +0900887 tape->b_data += n;
888 tape->b_count -= n;
889 if (!tape->b_count)
890 tape->bh = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891 }
Daniel Walkerdcd96372006-06-25 05:47:37 -0700892 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893}
894
Borislav Petkov077e3bd2008-04-27 15:38:34 +0200895static void idetape_init_merge_buffer(idetape_tape_t *tape)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896{
Borislav Petkov077e3bd2008-04-27 15:38:34 +0200897 struct idetape_bh *bh = tape->merge_bh;
898 tape->bh = tape->merge_bh;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100899
Borislav Petkov54abf372008-02-06 02:57:52 +0100900 if (tape->chrdev_dir == IDETAPE_DIR_WRITE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901 atomic_set(&bh->b_count, 0);
902 else {
903 tape->b_data = bh->b_data;
904 tape->b_count = atomic_read(&bh->b_count);
905 }
906}
907
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100909 * Write a filemark if write_filemark=1. Flush the device buffers without
910 * writing a filemark otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911 */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100912static void idetape_create_write_filemark_cmd(ide_drive_t *drive,
Borislav Petkovd236d742008-04-18 00:46:27 +0200913 struct ide_atapi_pc *pc, int write_filemark)
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] = WRITE_FILEMARKS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917 pc->c[4] = write_filemark;
Borislav Petkov346331f2008-04-18 00:46:26 +0200918 pc->flags |= PC_FLAG_WAIT_FOR_DSC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919}
920
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921static int idetape_wait_ready(ide_drive_t *drive, unsigned long timeout)
922{
923 idetape_tape_t *tape = drive->driver_data;
Bartlomiej Zolnierkiewicz2ac07d92008-10-10 22:39:38 +0200924 struct gendisk *disk = tape->disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925 int load_attempted = 0;
926
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100927 /* Wait for the tape to become ready */
Borislav Petkovf2e3ab52008-07-23 19:56:01 +0200928 set_bit(IDE_AFLAG_MEDIUM_PRESENT, &drive->atapi_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929 timeout += jiffies;
930 while (time_before(jiffies, timeout)) {
Bartlomiej Zolnierkiewiczde699ad2008-10-10 22:39:39 +0200931 if (ide_do_test_unit_ready(drive, disk) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932 return 0;
933 if ((tape->sense_key == 2 && tape->asc == 4 && tape->ascq == 2)
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100934 || (tape->asc == 0x3A)) {
935 /* no media */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936 if (load_attempted)
937 return -ENOMEDIUM;
Bartlomiej Zolnierkiewicz0c8a6c72008-10-10 22:39:39 +0200938 ide_do_start_stop(drive, disk, IDETAPE_LU_LOAD_MASK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939 load_attempted = 1;
940 /* not about to be ready */
941 } else if (!(tape->sense_key == 2 && tape->asc == 4 &&
942 (tape->ascq == 1 || tape->ascq == 8)))
943 return -EIO;
Nishanth Aravamudan80ce45f2005-09-10 00:27:08 -0700944 msleep(100);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945 }
946 return -EIO;
947}
948
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100949static int idetape_flush_tape_buffers(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950{
Bartlomiej Zolnierkiewicz2ac07d92008-10-10 22:39:38 +0200951 struct ide_tape_obj *tape = drive->driver_data;
Borislav Petkovd236d742008-04-18 00:46:27 +0200952 struct ide_atapi_pc pc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953 int rc;
954
955 idetape_create_write_filemark_cmd(drive, &pc, 0);
Bartlomiej Zolnierkiewicz2ac07d92008-10-10 22:39:38 +0200956 rc = ide_queue_pc_tail(drive, tape->disk, &pc);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100957 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958 return rc;
959 idetape_wait_ready(drive, 60 * 5 * HZ);
960 return 0;
961}
962
Borislav Petkovd236d742008-04-18 00:46:27 +0200963static void idetape_create_read_position_cmd(struct ide_atapi_pc *pc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964{
Bartlomiej Zolnierkiewicz7bf74202008-10-10 22:39:37 +0200965 ide_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +0100966 pc->c[0] = READ_POSITION;
Borislav Petkovd236d742008-04-18 00:46:27 +0200967 pc->req_xfer = 20;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968}
969
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100970static int idetape_read_position(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971{
972 idetape_tape_t *tape = drive->driver_data;
Borislav Petkovd236d742008-04-18 00:46:27 +0200973 struct ide_atapi_pc pc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974 int position;
975
Borislav Petkov8004a8c2008-02-06 02:57:51 +0100976 debug_log(DBG_PROCS, "Enter %s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977
978 idetape_create_read_position_cmd(&pc);
Bartlomiej Zolnierkiewicz2ac07d92008-10-10 22:39:38 +0200979 if (ide_queue_pc_tail(drive, tape->disk, &pc))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980 return -1;
Borislav Petkov54bb2072008-02-06 02:57:52 +0100981 position = tape->first_frame;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982 return position;
983}
984
Borislav Petkovd236d742008-04-18 00:46:27 +0200985static void idetape_create_locate_cmd(ide_drive_t *drive,
986 struct ide_atapi_pc *pc,
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100987 unsigned int block, u8 partition, int skip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988{
Bartlomiej Zolnierkiewicz7bf74202008-10-10 22:39:37 +0200989 ide_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +0100990 pc->c[0] = POSITION_TO_ELEMENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991 pc->c[1] = 2;
Borislav Petkov860ff5e2008-02-02 19:56:50 +0100992 put_unaligned(cpu_to_be32(block), (unsigned int *) &pc->c[3]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993 pc->c[8] = partition;
Borislav Petkov346331f2008-04-18 00:46:26 +0200994 pc->flags |= PC_FLAG_WAIT_FOR_DSC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995}
996
Borislav Petkovec0fdb02008-04-27 15:38:34 +0200997static void __ide_tape_discard_merge_buffer(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998{
999 idetape_tape_t *tape = drive->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000
Borislav Petkov54abf372008-02-06 02:57:52 +01001001 if (tape->chrdev_dir != IDETAPE_DIR_READ)
Borislav Petkov97986302008-04-27 15:38:34 +02001002 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003
Borislav Petkovf2e3ab52008-07-23 19:56:01 +02001004 clear_bit(IDE_AFLAG_FILEMARK, &drive->atapi_flags);
Borislav Petkov01a63aeb2008-04-27 15:38:34 +02001005 tape->merge_bh_size = 0;
Borislav Petkov077e3bd2008-04-27 15:38:34 +02001006 if (tape->merge_bh != NULL) {
1007 ide_tape_kfree_buffer(tape);
1008 tape->merge_bh = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009 }
1010
Borislav Petkov54abf372008-02-06 02:57:52 +01001011 tape->chrdev_dir = IDETAPE_DIR_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012}
1013
1014/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001015 * Position the tape to the requested block using the LOCATE packet command.
1016 * A READ POSITION command is then issued to check where we are positioned. Like
1017 * all higher level operations, we queue the commands at the tail of the request
1018 * queue and wait for their completion.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019 */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001020static int idetape_position_tape(ide_drive_t *drive, unsigned int block,
1021 u8 partition, int skip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022{
1023 idetape_tape_t *tape = drive->driver_data;
Bartlomiej Zolnierkiewicz2ac07d92008-10-10 22:39:38 +02001024 struct gendisk *disk = tape->disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025 int retval;
Borislav Petkovd236d742008-04-18 00:46:27 +02001026 struct ide_atapi_pc pc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027
Borislav Petkov54abf372008-02-06 02:57:52 +01001028 if (tape->chrdev_dir == IDETAPE_DIR_READ)
Borislav Petkovec0fdb02008-04-27 15:38:34 +02001029 __ide_tape_discard_merge_buffer(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030 idetape_wait_ready(drive, 60 * 5 * HZ);
1031 idetape_create_locate_cmd(drive, &pc, block, partition, skip);
Bartlomiej Zolnierkiewicz2ac07d92008-10-10 22:39:38 +02001032 retval = ide_queue_pc_tail(drive, disk, &pc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033 if (retval)
1034 return (retval);
1035
1036 idetape_create_read_position_cmd(&pc);
Bartlomiej Zolnierkiewicz2ac07d92008-10-10 22:39:38 +02001037 return ide_queue_pc_tail(drive, disk, &pc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038}
1039
Borislav Petkovec0fdb02008-04-27 15:38:34 +02001040static void ide_tape_discard_merge_buffer(ide_drive_t *drive,
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001041 int restore_position)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042{
1043 idetape_tape_t *tape = drive->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044 int seek, position;
1045
Borislav Petkovec0fdb02008-04-27 15:38:34 +02001046 __ide_tape_discard_merge_buffer(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047 if (restore_position) {
1048 position = idetape_read_position(drive);
Borislav Petkov97986302008-04-27 15:38:34 +02001049 seek = position > 0 ? position : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050 if (idetape_position_tape(drive, seek, 0, 0)) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001051 printk(KERN_INFO "ide-tape: %s: position_tape failed in"
Borislav Petkovec0fdb02008-04-27 15:38:34 +02001052 " %s\n", tape->name, __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053 return;
1054 }
1055 }
1056}
1057
1058/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001059 * Generate a read/write request for the block device interface and wait for it
1060 * to be serviced.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061 */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001062static int idetape_queue_rw_tail(ide_drive_t *drive, int cmd, int blocks,
1063 struct idetape_bh *bh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064{
1065 idetape_tape_t *tape = drive->driver_data;
FUJITA Tomonori64ea1b42008-07-15 21:21:43 +02001066 struct request *rq;
1067 int ret, errors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001069 debug_log(DBG_SENSE, "%s: cmd=%d\n", __func__, cmd);
1070
FUJITA Tomonori64ea1b42008-07-15 21:21:43 +02001071 rq = blk_get_request(drive->queue, READ, __GFP_WAIT);
1072 rq->cmd_type = REQ_TYPE_SPECIAL;
Borislav Petkov83dd5732008-07-23 19:56:00 +02001073 rq->cmd[13] = cmd;
FUJITA Tomonori64ea1b42008-07-15 21:21:43 +02001074 rq->rq_disk = tape->disk;
1075 rq->special = (void *)bh;
1076 rq->sector = tape->first_frame;
1077 rq->nr_sectors = blocks;
1078 rq->current_nr_sectors = blocks;
1079 blk_execute_rq(drive->queue, tape->disk, rq, 0);
1080
1081 errors = rq->errors;
1082 ret = tape->blk_size * (blocks - rq->current_nr_sectors);
1083 blk_put_request(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084
1085 if ((cmd & (REQ_IDETAPE_READ | REQ_IDETAPE_WRITE)) == 0)
1086 return 0;
1087
Borislav Petkov077e3bd2008-04-27 15:38:34 +02001088 if (tape->merge_bh)
1089 idetape_init_merge_buffer(tape);
Bartlomiej Zolnierkiewiczc152cc12009-03-27 12:46:34 +01001090 if (errors == IDE_DRV_ERROR_GENERAL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091 return -EIO;
FUJITA Tomonori64ea1b42008-07-15 21:21:43 +02001092 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093}
1094
Borislav Petkovd236d742008-04-18 00:46:27 +02001095static void idetape_create_inquiry_cmd(struct ide_atapi_pc *pc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096{
Bartlomiej Zolnierkiewicz7bf74202008-10-10 22:39:37 +02001097 ide_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +01001098 pc->c[0] = INQUIRY;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001099 pc->c[4] = 254;
Borislav Petkovd236d742008-04-18 00:46:27 +02001100 pc->req_xfer = 254;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101}
1102
Borislav Petkovd236d742008-04-18 00:46:27 +02001103static void idetape_create_rewind_cmd(ide_drive_t *drive,
1104 struct ide_atapi_pc *pc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105{
Bartlomiej Zolnierkiewicz7bf74202008-10-10 22:39:37 +02001106 ide_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +01001107 pc->c[0] = REZERO_UNIT;
Borislav Petkov346331f2008-04-18 00:46:26 +02001108 pc->flags |= PC_FLAG_WAIT_FOR_DSC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109}
1110
Borislav Petkovd236d742008-04-18 00:46:27 +02001111static void idetape_create_erase_cmd(struct ide_atapi_pc *pc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112{
Bartlomiej Zolnierkiewicz7bf74202008-10-10 22:39:37 +02001113 ide_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +01001114 pc->c[0] = ERASE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115 pc->c[1] = 1;
Borislav Petkov346331f2008-04-18 00:46:26 +02001116 pc->flags |= PC_FLAG_WAIT_FOR_DSC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117}
1118
Borislav Petkovd236d742008-04-18 00:46:27 +02001119static void idetape_create_space_cmd(struct ide_atapi_pc *pc, int count, u8 cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120{
Bartlomiej Zolnierkiewicz7bf74202008-10-10 22:39:37 +02001121 ide_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +01001122 pc->c[0] = SPACE;
Borislav Petkov860ff5e2008-02-02 19:56:50 +01001123 put_unaligned(cpu_to_be32(count), (unsigned int *) &pc->c[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124 pc->c[1] = cmd;
Borislav Petkov346331f2008-04-18 00:46:26 +02001125 pc->flags |= PC_FLAG_WAIT_FOR_DSC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126}
1127
Borislav Petkov97c566c2008-04-27 15:38:25 +02001128/* Queue up a character device originated write request. */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001129static int idetape_add_chrdev_write_request(ide_drive_t *drive, int blocks)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130{
1131 idetape_tape_t *tape = drive->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001133 debug_log(DBG_CHRDEV, "Enter %s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134
Borislav Petkov0aa4b012008-04-27 15:38:27 +02001135 return idetape_queue_rw_tail(drive, REQ_IDETAPE_WRITE,
Borislav Petkov077e3bd2008-04-27 15:38:34 +02001136 blocks, tape->merge_bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137}
1138
Borislav Petkovd9df9372008-04-27 15:38:34 +02001139static void ide_tape_flush_merge_buffer(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140{
1141 idetape_tape_t *tape = drive->driver_data;
Tejun Heo35ab8d32009-04-19 08:46:02 +09001142 int blocks;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143 struct idetape_bh *bh;
Borislav Petkov55a5d292008-02-02 19:56:49 +01001144
Borislav Petkov54abf372008-02-06 02:57:52 +01001145 if (tape->chrdev_dir != IDETAPE_DIR_WRITE) {
Borislav Petkov077e3bd2008-04-27 15:38:34 +02001146 printk(KERN_ERR "ide-tape: bug: Trying to empty merge buffer"
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001147 " but we are not writing.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148 return;
1149 }
Borislav Petkov01a63aeb2008-04-27 15:38:34 +02001150 if (tape->merge_bh_size > tape->buffer_size) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151 printk(KERN_ERR "ide-tape: bug: merge_buffer too big\n");
Borislav Petkov01a63aeb2008-04-27 15:38:34 +02001152 tape->merge_bh_size = tape->buffer_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153 }
Borislav Petkov01a63aeb2008-04-27 15:38:34 +02001154 if (tape->merge_bh_size) {
1155 blocks = tape->merge_bh_size / tape->blk_size;
1156 if (tape->merge_bh_size % tape->blk_size) {
Tejun Heo35ab8d32009-04-19 08:46:02 +09001157 unsigned int i = tape->blk_size -
1158 tape->merge_bh_size % tape->blk_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159 blocks++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160 bh = tape->bh;
Tejun Heo35ab8d32009-04-19 08:46:02 +09001161 if (bh) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001162 memset(bh->b_data + atomic_read(&bh->b_count),
Tejun Heo35ab8d32009-04-19 08:46:02 +09001163 0, i);
1164 atomic_add(i, &bh->b_count);
1165 } else
1166 printk(KERN_INFO "ide-tape: bug, bh NULL\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167 }
1168 (void) idetape_add_chrdev_write_request(drive, blocks);
Borislav Petkov01a63aeb2008-04-27 15:38:34 +02001169 tape->merge_bh_size = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170 }
Borislav Petkov077e3bd2008-04-27 15:38:34 +02001171 if (tape->merge_bh != NULL) {
1172 ide_tape_kfree_buffer(tape);
1173 tape->merge_bh = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174 }
Borislav Petkov54abf372008-02-06 02:57:52 +01001175 tape->chrdev_dir = IDETAPE_DIR_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176}
1177
Borislav Petkov83042b22008-04-27 15:38:27 +02001178static int idetape_init_read(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179{
1180 idetape_tape_t *tape = drive->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181 int bytes_read;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182
1183 /* Initialize read operation */
Borislav Petkov54abf372008-02-06 02:57:52 +01001184 if (tape->chrdev_dir != IDETAPE_DIR_READ) {
1185 if (tape->chrdev_dir == IDETAPE_DIR_WRITE) {
Borislav Petkovd9df9372008-04-27 15:38:34 +02001186 ide_tape_flush_merge_buffer(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187 idetape_flush_tape_buffers(drive);
1188 }
Borislav Petkov077e3bd2008-04-27 15:38:34 +02001189 if (tape->merge_bh || tape->merge_bh_size) {
Borislav Petkov01a63aeb2008-04-27 15:38:34 +02001190 printk(KERN_ERR "ide-tape: merge_bh_size should be"
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001191 " 0 now\n");
Borislav Petkov01a63aeb2008-04-27 15:38:34 +02001192 tape->merge_bh_size = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193 }
Tejun Heo35ab8d32009-04-19 08:46:02 +09001194 tape->merge_bh = ide_tape_kmalloc_buffer(tape, 0);
Borislav Petkov077e3bd2008-04-27 15:38:34 +02001195 if (!tape->merge_bh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196 return -ENOMEM;
Borislav Petkov54abf372008-02-06 02:57:52 +01001197 tape->chrdev_dir = IDETAPE_DIR_READ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198
1199 /*
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001200 * Issue a read 0 command to ensure that DSC handshake is
1201 * switched from completion mode to buffer available mode.
1202 * No point in issuing this if DSC overlap isn't supported, some
1203 * drives (Seagate STT3401A) will return an error.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204 */
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +02001205 if (drive->dev_flags & IDE_DFLAG_DSC_OVERLAP) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001206 bytes_read = idetape_queue_rw_tail(drive,
1207 REQ_IDETAPE_READ, 0,
Borislav Petkov077e3bd2008-04-27 15:38:34 +02001208 tape->merge_bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209 if (bytes_read < 0) {
Borislav Petkov077e3bd2008-04-27 15:38:34 +02001210 ide_tape_kfree_buffer(tape);
1211 tape->merge_bh = NULL;
Borislav Petkov54abf372008-02-06 02:57:52 +01001212 tape->chrdev_dir = IDETAPE_DIR_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213 return bytes_read;
1214 }
1215 }
1216 }
Borislav Petkov5e69bd92008-04-27 15:38:25 +02001217
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218 return 0;
1219}
1220
Borislav Petkov5bd50dc2008-04-27 15:38:28 +02001221/* called from idetape_chrdev_read() to service a chrdev read request. */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001222static int idetape_add_chrdev_read_request(ide_drive_t *drive, int blocks)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223{
1224 idetape_tape_t *tape = drive->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001226 debug_log(DBG_PROCS, "Enter %s, %d blocks\n", __func__, blocks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001228 /* If we are at a filemark, return a read length of 0 */
Borislav Petkovf2e3ab52008-07-23 19:56:01 +02001229 if (test_bit(IDE_AFLAG_FILEMARK, &drive->atapi_flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230 return 0;
1231
Borislav Petkov83042b22008-04-27 15:38:27 +02001232 idetape_init_read(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233
Borislav Petkov5e69bd92008-04-27 15:38:25 +02001234 return idetape_queue_rw_tail(drive, REQ_IDETAPE_READ, blocks,
Borislav Petkov077e3bd2008-04-27 15:38:34 +02001235 tape->merge_bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236}
1237
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001238static void idetape_pad_zeros(ide_drive_t *drive, int bcount)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239{
1240 idetape_tape_t *tape = drive->driver_data;
Tejun Heo35ab8d32009-04-19 08:46:02 +09001241 struct idetape_bh *bh = tape->merge_bh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242 int blocks;
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001243
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244 while (bcount) {
1245 unsigned int count;
1246
Borislav Petkovf73850a2008-04-27 15:38:33 +02001247 count = min(tape->buffer_size, bcount);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248 bcount -= count;
Borislav Petkov54bb2072008-02-06 02:57:52 +01001249 blocks = count / tape->blk_size;
Tejun Heo35ab8d32009-04-19 08:46:02 +09001250 atomic_set(&bh->b_count, count);
1251 memset(bh->b_data, 0, atomic_read(&bh->b_count));
1252
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001253 idetape_queue_rw_tail(drive, REQ_IDETAPE_WRITE, blocks,
Borislav Petkov077e3bd2008-04-27 15:38:34 +02001254 tape->merge_bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255 }
1256}
1257
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001259 * Rewinds the tape to the Beginning Of the current Partition (BOP). We
1260 * currently support only one partition.
1261 */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001262static int idetape_rewind_tape(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263{
Bartlomiej Zolnierkiewicz2ac07d92008-10-10 22:39:38 +02001264 struct ide_tape_obj *tape = drive->driver_data;
1265 struct gendisk *disk = tape->disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266 int retval;
Borislav Petkovd236d742008-04-18 00:46:27 +02001267 struct ide_atapi_pc pc;
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001268
1269 debug_log(DBG_SENSE, "Enter %s\n", __func__);
1270
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271 idetape_create_rewind_cmd(drive, &pc);
Bartlomiej Zolnierkiewicz2ac07d92008-10-10 22:39:38 +02001272 retval = ide_queue_pc_tail(drive, disk, &pc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273 if (retval)
1274 return retval;
1275
1276 idetape_create_read_position_cmd(&pc);
Bartlomiej Zolnierkiewicz2ac07d92008-10-10 22:39:38 +02001277 retval = ide_queue_pc_tail(drive, disk, &pc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278 if (retval)
1279 return retval;
1280 return 0;
1281}
1282
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001283/* mtio.h compatible commands should be issued to the chrdev interface. */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001284static int idetape_blkdev_ioctl(ide_drive_t *drive, unsigned int cmd,
1285 unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286{
1287 idetape_tape_t *tape = drive->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288 void __user *argp = (void __user *)arg;
1289
Borislav Petkovd59823f2008-02-02 19:56:51 +01001290 struct idetape_config {
1291 int dsc_rw_frequency;
1292 int dsc_media_access_frequency;
1293 int nr_stages;
1294 } config;
1295
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001296 debug_log(DBG_PROCS, "Enter %s\n", __func__);
1297
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298 switch (cmd) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001299 case 0x0340:
1300 if (copy_from_user(&config, argp, sizeof(config)))
1301 return -EFAULT;
1302 tape->best_dsc_rw_freq = config.dsc_rw_frequency;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001303 break;
1304 case 0x0350:
1305 config.dsc_rw_frequency = (int) tape->best_dsc_rw_freq;
Borislav Petkov83042b22008-04-27 15:38:27 +02001306 config.nr_stages = 1;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001307 if (copy_to_user(argp, &config, sizeof(config)))
1308 return -EFAULT;
1309 break;
1310 default:
1311 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312 }
1313 return 0;
1314}
1315
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001316static int idetape_space_over_filemarks(ide_drive_t *drive, short mt_op,
1317 int mt_count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318{
1319 idetape_tape_t *tape = drive->driver_data;
Bartlomiej Zolnierkiewicz2ac07d92008-10-10 22:39:38 +02001320 struct gendisk *disk = tape->disk;
Borislav Petkovd236d742008-04-18 00:46:27 +02001321 struct ide_atapi_pc pc;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001322 int retval, count = 0;
Borislav Petkovb6422012008-02-02 19:56:49 +01001323 int sprev = !!(tape->caps[4] & 0x20);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324
1325 if (mt_count == 0)
1326 return 0;
1327 if (MTBSF == mt_op || MTBSFM == mt_op) {
Borislav Petkovb6422012008-02-02 19:56:49 +01001328 if (!sprev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329 return -EIO;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001330 mt_count = -mt_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331 }
1332
Borislav Petkov54abf372008-02-06 02:57:52 +01001333 if (tape->chrdev_dir == IDETAPE_DIR_READ) {
Borislav Petkov01a63aeb2008-04-27 15:38:34 +02001334 tape->merge_bh_size = 0;
Borislav Petkovf2e3ab52008-07-23 19:56:01 +02001335 if (test_and_clear_bit(IDE_AFLAG_FILEMARK, &drive->atapi_flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336 ++count;
Borislav Petkovec0fdb02008-04-27 15:38:34 +02001337 ide_tape_discard_merge_buffer(drive, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338 }
1339
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340 switch (mt_op) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001341 case MTFSF:
1342 case MTBSF:
1343 idetape_create_space_cmd(&pc, mt_count - count,
1344 IDETAPE_SPACE_OVER_FILEMARK);
Bartlomiej Zolnierkiewicz2ac07d92008-10-10 22:39:38 +02001345 return ide_queue_pc_tail(drive, disk, &pc);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001346 case MTFSFM:
1347 case MTBSFM:
1348 if (!sprev)
1349 return -EIO;
1350 retval = idetape_space_over_filemarks(drive, MTFSF,
1351 mt_count - count);
1352 if (retval)
1353 return retval;
1354 count = (MTBSFM == mt_op ? 1 : -1);
1355 return idetape_space_over_filemarks(drive, MTFSF, count);
1356 default:
1357 printk(KERN_ERR "ide-tape: MTIO operation %d not supported\n",
1358 mt_op);
1359 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360 }
1361}
1362
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001364 * Our character device read / write functions.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001366 * The tape is optimized to maximize throughput when it is transferring an
1367 * integral number of the "continuous transfer limit", which is a parameter of
1368 * the specific tape (26kB on my particular tape, 32kB for Onstream).
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001370 * As of version 1.3 of the driver, the character device provides an abstract
1371 * continuous view of the media - any mix of block sizes (even 1 byte) on the
1372 * same backup/restore procedure is supported. The driver will internally
1373 * convert the requests to the recommended transfer unit, so that an unmatch
1374 * between the user's block size to the recommended size will only result in a
1375 * (slightly) increased driver overhead, but will no longer hit performance.
1376 * This is not applicable to Onstream.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377 */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001378static ssize_t idetape_chrdev_read(struct file *file, char __user *buf,
1379 size_t count, loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380{
Borislav Petkov5aeddf92008-10-13 21:39:34 +02001381 struct ide_tape_obj *tape = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382 ide_drive_t *drive = tape->drive;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001383 ssize_t bytes_read, temp, actually_read = 0, rc;
Daniel Walkerdcd96372006-06-25 05:47:37 -07001384 ssize_t ret = 0;
Borislav Petkovb6422012008-02-02 19:56:49 +01001385 u16 ctl = *(u16 *)&tape->caps[12];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001387 debug_log(DBG_CHRDEV, "Enter %s, count %Zd\n", __func__, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388
Borislav Petkov54abf372008-02-06 02:57:52 +01001389 if (tape->chrdev_dir != IDETAPE_DIR_READ) {
Borislav Petkovf2e3ab52008-07-23 19:56:01 +02001390 if (test_bit(IDE_AFLAG_DETECT_BS, &drive->atapi_flags))
Borislav Petkov54bb2072008-02-06 02:57:52 +01001391 if (count > tape->blk_size &&
1392 (count % tape->blk_size) == 0)
1393 tape->user_bs_factor = count / tape->blk_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394 }
Borislav Petkov83042b22008-04-27 15:38:27 +02001395 rc = idetape_init_read(drive);
Borislav Petkov8d06bfa2008-02-06 02:57:53 +01001396 if (rc < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397 return rc;
1398 if (count == 0)
1399 return (0);
Borislav Petkov01a63aeb2008-04-27 15:38:34 +02001400 if (tape->merge_bh_size) {
1401 actually_read = min((unsigned int)(tape->merge_bh_size),
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001402 (unsigned int)count);
Borislav Petkov99d74e62008-04-27 15:38:25 +02001403 if (idetape_copy_stage_to_user(tape, buf, actually_read))
Daniel Walkerdcd96372006-06-25 05:47:37 -07001404 ret = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405 buf += actually_read;
Borislav Petkov01a63aeb2008-04-27 15:38:34 +02001406 tape->merge_bh_size -= actually_read;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407 count -= actually_read;
1408 }
Borislav Petkovf73850a2008-04-27 15:38:33 +02001409 while (count >= tape->buffer_size) {
Borislav Petkovb6422012008-02-02 19:56:49 +01001410 bytes_read = idetape_add_chrdev_read_request(drive, ctl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411 if (bytes_read <= 0)
1412 goto finish;
Borislav Petkov99d74e62008-04-27 15:38:25 +02001413 if (idetape_copy_stage_to_user(tape, buf, bytes_read))
Daniel Walkerdcd96372006-06-25 05:47:37 -07001414 ret = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415 buf += bytes_read;
1416 count -= bytes_read;
1417 actually_read += bytes_read;
1418 }
1419 if (count) {
Borislav Petkovb6422012008-02-02 19:56:49 +01001420 bytes_read = idetape_add_chrdev_read_request(drive, ctl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421 if (bytes_read <= 0)
1422 goto finish;
1423 temp = min((unsigned long)count, (unsigned long)bytes_read);
Borislav Petkov99d74e62008-04-27 15:38:25 +02001424 if (idetape_copy_stage_to_user(tape, buf, temp))
Daniel Walkerdcd96372006-06-25 05:47:37 -07001425 ret = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426 actually_read += temp;
Borislav Petkov01a63aeb2008-04-27 15:38:34 +02001427 tape->merge_bh_size = bytes_read-temp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428 }
1429finish:
Borislav Petkovf2e3ab52008-07-23 19:56:01 +02001430 if (!actually_read && test_bit(IDE_AFLAG_FILEMARK, &drive->atapi_flags)) {
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001431 debug_log(DBG_SENSE, "%s: spacing over filemark\n", tape->name);
1432
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433 idetape_space_over_filemarks(drive, MTFSF, 1);
1434 return 0;
1435 }
Daniel Walkerdcd96372006-06-25 05:47:37 -07001436
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001437 return ret ? ret : actually_read;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438}
1439
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001440static ssize_t idetape_chrdev_write(struct file *file, const char __user *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441 size_t count, loff_t *ppos)
1442{
Borislav Petkov5aeddf92008-10-13 21:39:34 +02001443 struct ide_tape_obj *tape = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444 ide_drive_t *drive = tape->drive;
Daniel Walkerdcd96372006-06-25 05:47:37 -07001445 ssize_t actually_written = 0;
1446 ssize_t ret = 0;
Borislav Petkovb6422012008-02-02 19:56:49 +01001447 u16 ctl = *(u16 *)&tape->caps[12];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448
1449 /* The drive is write protected. */
1450 if (tape->write_prot)
1451 return -EACCES;
1452
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001453 debug_log(DBG_CHRDEV, "Enter %s, count %Zd\n", __func__, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454
1455 /* Initialize write operation */
Borislav Petkov54abf372008-02-06 02:57:52 +01001456 if (tape->chrdev_dir != IDETAPE_DIR_WRITE) {
1457 if (tape->chrdev_dir == IDETAPE_DIR_READ)
Borislav Petkovec0fdb02008-04-27 15:38:34 +02001458 ide_tape_discard_merge_buffer(drive, 1);
Borislav Petkov077e3bd2008-04-27 15:38:34 +02001459 if (tape->merge_bh || tape->merge_bh_size) {
Borislav Petkov01a63aeb2008-04-27 15:38:34 +02001460 printk(KERN_ERR "ide-tape: merge_bh_size "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461 "should be 0 now\n");
Borislav Petkov01a63aeb2008-04-27 15:38:34 +02001462 tape->merge_bh_size = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463 }
Tejun Heo35ab8d32009-04-19 08:46:02 +09001464 tape->merge_bh = ide_tape_kmalloc_buffer(tape, 0);
Borislav Petkov077e3bd2008-04-27 15:38:34 +02001465 if (!tape->merge_bh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466 return -ENOMEM;
Borislav Petkov54abf372008-02-06 02:57:52 +01001467 tape->chrdev_dir = IDETAPE_DIR_WRITE;
Borislav Petkov077e3bd2008-04-27 15:38:34 +02001468 idetape_init_merge_buffer(tape);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469
1470 /*
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001471 * Issue a write 0 command to ensure that DSC handshake is
1472 * switched from completion mode to buffer available mode. No
1473 * point in issuing this if DSC overlap isn't supported, some
1474 * drives (Seagate STT3401A) will return an error.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475 */
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +02001476 if (drive->dev_flags & IDE_DFLAG_DSC_OVERLAP) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001477 ssize_t retval = idetape_queue_rw_tail(drive,
1478 REQ_IDETAPE_WRITE, 0,
Borislav Petkov077e3bd2008-04-27 15:38:34 +02001479 tape->merge_bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480 if (retval < 0) {
Borislav Petkov077e3bd2008-04-27 15:38:34 +02001481 ide_tape_kfree_buffer(tape);
1482 tape->merge_bh = NULL;
Borislav Petkov54abf372008-02-06 02:57:52 +01001483 tape->chrdev_dir = IDETAPE_DIR_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001484 return retval;
1485 }
1486 }
1487 }
1488 if (count == 0)
1489 return (0);
Borislav Petkov01a63aeb2008-04-27 15:38:34 +02001490 if (tape->merge_bh_size) {
1491 if (tape->merge_bh_size >= tape->buffer_size) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001492 printk(KERN_ERR "ide-tape: bug: merge buf too big\n");
Borislav Petkov01a63aeb2008-04-27 15:38:34 +02001493 tape->merge_bh_size = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494 }
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001495 actually_written = min((unsigned int)
Borislav Petkov01a63aeb2008-04-27 15:38:34 +02001496 (tape->buffer_size - tape->merge_bh_size),
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001497 (unsigned int)count);
Borislav Petkov8646c882008-04-27 15:38:26 +02001498 if (idetape_copy_stage_from_user(tape, buf, actually_written))
Daniel Walkerdcd96372006-06-25 05:47:37 -07001499 ret = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500 buf += actually_written;
Borislav Petkov01a63aeb2008-04-27 15:38:34 +02001501 tape->merge_bh_size += actually_written;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502 count -= actually_written;
1503
Borislav Petkov01a63aeb2008-04-27 15:38:34 +02001504 if (tape->merge_bh_size == tape->buffer_size) {
Daniel Walkerdcd96372006-06-25 05:47:37 -07001505 ssize_t retval;
Borislav Petkov01a63aeb2008-04-27 15:38:34 +02001506 tape->merge_bh_size = 0;
Borislav Petkovb6422012008-02-02 19:56:49 +01001507 retval = idetape_add_chrdev_write_request(drive, ctl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001508 if (retval <= 0)
1509 return (retval);
1510 }
1511 }
Borislav Petkovf73850a2008-04-27 15:38:33 +02001512 while (count >= tape->buffer_size) {
Daniel Walkerdcd96372006-06-25 05:47:37 -07001513 ssize_t retval;
Borislav Petkovf73850a2008-04-27 15:38:33 +02001514 if (idetape_copy_stage_from_user(tape, buf, tape->buffer_size))
Daniel Walkerdcd96372006-06-25 05:47:37 -07001515 ret = -EFAULT;
Borislav Petkovf73850a2008-04-27 15:38:33 +02001516 buf += tape->buffer_size;
1517 count -= tape->buffer_size;
Borislav Petkovb6422012008-02-02 19:56:49 +01001518 retval = idetape_add_chrdev_write_request(drive, ctl);
Borislav Petkovf73850a2008-04-27 15:38:33 +02001519 actually_written += tape->buffer_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520 if (retval <= 0)
1521 return (retval);
1522 }
1523 if (count) {
1524 actually_written += count;
Borislav Petkov8646c882008-04-27 15:38:26 +02001525 if (idetape_copy_stage_from_user(tape, buf, count))
Daniel Walkerdcd96372006-06-25 05:47:37 -07001526 ret = -EFAULT;
Borislav Petkov01a63aeb2008-04-27 15:38:34 +02001527 tape->merge_bh_size += count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001528 }
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001529 return ret ? ret : actually_written;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001530}
1531
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001532static int idetape_write_filemark(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533{
Bartlomiej Zolnierkiewicz2ac07d92008-10-10 22:39:38 +02001534 struct ide_tape_obj *tape = drive->driver_data;
Borislav Petkovd236d742008-04-18 00:46:27 +02001535 struct ide_atapi_pc pc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536
1537 /* Write a filemark */
1538 idetape_create_write_filemark_cmd(drive, &pc, 1);
Bartlomiej Zolnierkiewicz2ac07d92008-10-10 22:39:38 +02001539 if (ide_queue_pc_tail(drive, tape->disk, &pc)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540 printk(KERN_ERR "ide-tape: Couldn't write a filemark\n");
1541 return -EIO;
1542 }
1543 return 0;
1544}
1545
1546/*
Borislav Petkovd99c9da2008-02-02 19:56:51 +01001547 * Called from idetape_chrdev_ioctl when the general mtio MTIOCTOP ioctl is
1548 * requested.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549 *
Borislav Petkovd99c9da2008-02-02 19:56:51 +01001550 * Note: MTBSF and MTBSFM are not supported when the tape doesn't support
1551 * spacing over filemarks in the reverse direction. In this case, MTFSFM is also
Borislav Petkov5bd50dc2008-04-27 15:38:28 +02001552 * usually not supported.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553 *
Borislav Petkovd99c9da2008-02-02 19:56:51 +01001554 * The following commands are currently not supported:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001555 *
Borislav Petkovd99c9da2008-02-02 19:56:51 +01001556 * MTFSS, MTBSS, MTWSM, MTSETDENSITY, MTSETDRVBUFFER, MT_ST_BOOLEANS,
1557 * MT_ST_WRITE_THRESHOLD.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558 */
Borislav Petkovd99c9da2008-02-02 19:56:51 +01001559static int idetape_mtioctop(ide_drive_t *drive, short mt_op, int mt_count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560{
1561 idetape_tape_t *tape = drive->driver_data;
Bartlomiej Zolnierkiewicz2ac07d92008-10-10 22:39:38 +02001562 struct gendisk *disk = tape->disk;
Borislav Petkovd236d742008-04-18 00:46:27 +02001563 struct ide_atapi_pc pc;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001564 int i, retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001566 debug_log(DBG_ERR, "Handling MTIOCTOP ioctl: mt_op=%d, mt_count=%d\n",
1567 mt_op, mt_count);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001568
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569 switch (mt_op) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001570 case MTFSF:
1571 case MTFSFM:
1572 case MTBSF:
1573 case MTBSFM:
1574 if (!mt_count)
1575 return 0;
1576 return idetape_space_over_filemarks(drive, mt_op, mt_count);
1577 default:
1578 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579 }
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001580
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581 switch (mt_op) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001582 case MTWEOF:
1583 if (tape->write_prot)
1584 return -EACCES;
Borislav Petkovec0fdb02008-04-27 15:38:34 +02001585 ide_tape_discard_merge_buffer(drive, 1);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001586 for (i = 0; i < mt_count; i++) {
1587 retval = idetape_write_filemark(drive);
1588 if (retval)
1589 return retval;
1590 }
1591 return 0;
1592 case MTREW:
Borislav Petkovec0fdb02008-04-27 15:38:34 +02001593 ide_tape_discard_merge_buffer(drive, 0);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001594 if (idetape_rewind_tape(drive))
1595 return -EIO;
1596 return 0;
1597 case MTLOAD:
Borislav Petkovec0fdb02008-04-27 15:38:34 +02001598 ide_tape_discard_merge_buffer(drive, 0);
Bartlomiej Zolnierkiewicz0c8a6c72008-10-10 22:39:39 +02001599 return ide_do_start_stop(drive, disk, IDETAPE_LU_LOAD_MASK);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001600 case MTUNLOAD:
1601 case MTOFFL:
1602 /*
1603 * If door is locked, attempt to unlock before
1604 * attempting to eject.
1605 */
1606 if (tape->door_locked) {
Bartlomiej Zolnierkiewicz05780422008-10-10 22:39:38 +02001607 if (!ide_set_media_lock(drive, disk, 0))
Bartlomiej Zolnierkiewicz385a4b82008-10-10 22:39:37 +02001608 tape->door_locked = DOOR_UNLOCKED;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001609 }
Borislav Petkovec0fdb02008-04-27 15:38:34 +02001610 ide_tape_discard_merge_buffer(drive, 0);
Bartlomiej Zolnierkiewicz0c8a6c72008-10-10 22:39:39 +02001611 retval = ide_do_start_stop(drive, disk, !IDETAPE_LU_LOAD_MASK);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001612 if (!retval)
Borislav Petkovf2e3ab52008-07-23 19:56:01 +02001613 clear_bit(IDE_AFLAG_MEDIUM_PRESENT, &drive->atapi_flags);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001614 return retval;
1615 case MTNOP:
Borislav Petkovec0fdb02008-04-27 15:38:34 +02001616 ide_tape_discard_merge_buffer(drive, 0);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001617 return idetape_flush_tape_buffers(drive);
1618 case MTRETEN:
Borislav Petkovec0fdb02008-04-27 15:38:34 +02001619 ide_tape_discard_merge_buffer(drive, 0);
Bartlomiej Zolnierkiewicz0c8a6c72008-10-10 22:39:39 +02001620 return ide_do_start_stop(drive, disk,
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001621 IDETAPE_LU_RETENSION_MASK | IDETAPE_LU_LOAD_MASK);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001622 case MTEOM:
1623 idetape_create_space_cmd(&pc, 0, IDETAPE_SPACE_TO_EOD);
Bartlomiej Zolnierkiewicz2ac07d92008-10-10 22:39:38 +02001624 return ide_queue_pc_tail(drive, disk, &pc);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001625 case MTERASE:
1626 (void)idetape_rewind_tape(drive);
1627 idetape_create_erase_cmd(&pc);
Bartlomiej Zolnierkiewicz2ac07d92008-10-10 22:39:38 +02001628 return ide_queue_pc_tail(drive, disk, &pc);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001629 case MTSETBLK:
1630 if (mt_count) {
1631 if (mt_count < tape->blk_size ||
1632 mt_count % tape->blk_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001633 return -EIO;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001634 tape->user_bs_factor = mt_count / tape->blk_size;
Borislav Petkovf2e3ab52008-07-23 19:56:01 +02001635 clear_bit(IDE_AFLAG_DETECT_BS, &drive->atapi_flags);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001636 } else
Borislav Petkovf2e3ab52008-07-23 19:56:01 +02001637 set_bit(IDE_AFLAG_DETECT_BS, &drive->atapi_flags);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001638 return 0;
1639 case MTSEEK:
Borislav Petkovec0fdb02008-04-27 15:38:34 +02001640 ide_tape_discard_merge_buffer(drive, 0);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001641 return idetape_position_tape(drive,
1642 mt_count * tape->user_bs_factor, tape->partition, 0);
1643 case MTSETPART:
Borislav Petkovec0fdb02008-04-27 15:38:34 +02001644 ide_tape_discard_merge_buffer(drive, 0);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001645 return idetape_position_tape(drive, 0, mt_count, 0);
1646 case MTFSR:
1647 case MTBSR:
1648 case MTLOCK:
Bartlomiej Zolnierkiewicz05780422008-10-10 22:39:38 +02001649 retval = ide_set_media_lock(drive, disk, 1);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001650 if (retval)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001651 return retval;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001652 tape->door_locked = DOOR_EXPLICITLY_LOCKED;
1653 return 0;
1654 case MTUNLOCK:
Bartlomiej Zolnierkiewicz05780422008-10-10 22:39:38 +02001655 retval = ide_set_media_lock(drive, disk, 0);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001656 if (retval)
1657 return retval;
1658 tape->door_locked = DOOR_UNLOCKED;
1659 return 0;
1660 default:
1661 printk(KERN_ERR "ide-tape: MTIO operation %d not supported\n",
1662 mt_op);
1663 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664 }
1665}
1666
1667/*
Borislav Petkovd99c9da2008-02-02 19:56:51 +01001668 * Our character device ioctls. General mtio.h magnetic io commands are
1669 * supported here, and not in the corresponding block interface. Our own
1670 * ide-tape ioctls are supported on both interfaces.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001671 */
Borislav Petkovd99c9da2008-02-02 19:56:51 +01001672static int idetape_chrdev_ioctl(struct inode *inode, struct file *file,
1673 unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001674{
Borislav Petkov5aeddf92008-10-13 21:39:34 +02001675 struct ide_tape_obj *tape = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001676 ide_drive_t *drive = tape->drive;
1677 struct mtop mtop;
1678 struct mtget mtget;
1679 struct mtpos mtpos;
Borislav Petkov54bb2072008-02-06 02:57:52 +01001680 int block_offset = 0, position = tape->first_frame;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681 void __user *argp = (void __user *)arg;
1682
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001683 debug_log(DBG_CHRDEV, "Enter %s, cmd=%u\n", __func__, cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001684
Borislav Petkov54abf372008-02-06 02:57:52 +01001685 if (tape->chrdev_dir == IDETAPE_DIR_WRITE) {
Borislav Petkovd9df9372008-04-27 15:38:34 +02001686 ide_tape_flush_merge_buffer(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001687 idetape_flush_tape_buffers(drive);
1688 }
1689 if (cmd == MTIOCGET || cmd == MTIOCPOS) {
Borislav Petkov01a63aeb2008-04-27 15:38:34 +02001690 block_offset = tape->merge_bh_size /
Borislav Petkov54bb2072008-02-06 02:57:52 +01001691 (tape->blk_size * tape->user_bs_factor);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001692 position = idetape_read_position(drive);
1693 if (position < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694 return -EIO;
1695 }
1696 switch (cmd) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001697 case MTIOCTOP:
1698 if (copy_from_user(&mtop, argp, sizeof(struct mtop)))
1699 return -EFAULT;
1700 return idetape_mtioctop(drive, mtop.mt_op, mtop.mt_count);
1701 case MTIOCGET:
1702 memset(&mtget, 0, sizeof(struct mtget));
1703 mtget.mt_type = MT_ISSCSI2;
1704 mtget.mt_blkno = position / tape->user_bs_factor - block_offset;
1705 mtget.mt_dsreg =
1706 ((tape->blk_size * tape->user_bs_factor)
1707 << MT_ST_BLKSIZE_SHIFT) & MT_ST_BLKSIZE_MASK;
Borislav Petkov54bb2072008-02-06 02:57:52 +01001708
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001709 if (tape->drv_write_prot)
1710 mtget.mt_gstat |= GMT_WR_PROT(0xffffffff);
1711
1712 if (copy_to_user(argp, &mtget, sizeof(struct mtget)))
1713 return -EFAULT;
1714 return 0;
1715 case MTIOCPOS:
1716 mtpos.mt_blkno = position / tape->user_bs_factor - block_offset;
1717 if (copy_to_user(argp, &mtpos, sizeof(struct mtpos)))
1718 return -EFAULT;
1719 return 0;
1720 default:
1721 if (tape->chrdev_dir == IDETAPE_DIR_READ)
Borislav Petkovec0fdb02008-04-27 15:38:34 +02001722 ide_tape_discard_merge_buffer(drive, 1);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001723 return idetape_blkdev_ioctl(drive, cmd, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001724 }
1725}
1726
Borislav Petkov3cffb9c2008-02-02 19:56:50 +01001727/*
1728 * Do a mode sense page 0 with block descriptor and if it succeeds set the tape
1729 * block size with the reported value.
1730 */
1731static void ide_tape_get_bsize_from_bdesc(ide_drive_t *drive)
1732{
1733 idetape_tape_t *tape = drive->driver_data;
Borislav Petkovd236d742008-04-18 00:46:27 +02001734 struct ide_atapi_pc pc;
Borislav Petkov3cffb9c2008-02-02 19:56:50 +01001735
1736 idetape_create_mode_sense_cmd(&pc, IDETAPE_BLOCK_DESCRIPTOR);
Bartlomiej Zolnierkiewicz2ac07d92008-10-10 22:39:38 +02001737 if (ide_queue_pc_tail(drive, tape->disk, &pc)) {
Borislav Petkov3cffb9c2008-02-02 19:56:50 +01001738 printk(KERN_ERR "ide-tape: Can't get block descriptor\n");
Borislav Petkov54bb2072008-02-06 02:57:52 +01001739 if (tape->blk_size == 0) {
Borislav Petkov3cffb9c2008-02-02 19:56:50 +01001740 printk(KERN_WARNING "ide-tape: Cannot deal with zero "
1741 "block size, assuming 32k\n");
Borislav Petkov54bb2072008-02-06 02:57:52 +01001742 tape->blk_size = 32768;
Borislav Petkov3cffb9c2008-02-02 19:56:50 +01001743 }
1744 return;
1745 }
Borislav Petkovd236d742008-04-18 00:46:27 +02001746 tape->blk_size = (pc.buf[4 + 5] << 16) +
1747 (pc.buf[4 + 6] << 8) +
1748 pc.buf[4 + 7];
1749 tape->drv_write_prot = (pc.buf[2] & 0x80) >> 7;
Borislav Petkov3cffb9c2008-02-02 19:56:50 +01001750}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001751
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001752static int idetape_chrdev_open(struct inode *inode, struct file *filp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001753{
1754 unsigned int minor = iminor(inode), i = minor & ~0xc0;
1755 ide_drive_t *drive;
1756 idetape_tape_t *tape;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001757 int retval;
1758
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001759 if (i >= MAX_HWIFS * MAX_DRIVES)
1760 return -ENXIO;
1761
Jonathan Corbet04f4ac92008-05-15 12:01:56 -06001762 lock_kernel();
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001763 tape = ide_tape_chrdev_get(i);
Jonathan Corbet04f4ac92008-05-15 12:01:56 -06001764 if (!tape) {
1765 unlock_kernel();
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001766 return -ENXIO;
Jonathan Corbet04f4ac92008-05-15 12:01:56 -06001767 }
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001768
1769 debug_log(DBG_CHRDEV, "Enter %s\n", __func__);
1770
Linus Torvalds1da177e2005-04-16 15:20:36 -07001771 /*
1772 * We really want to do nonseekable_open(inode, filp); here, but some
1773 * versions of tar incorrectly call lseek on tapes and bail out if that
1774 * fails. So we disallow pread() and pwrite(), but permit lseeks.
1775 */
1776 filp->f_mode &= ~(FMODE_PREAD | FMODE_PWRITE);
1777
Linus Torvalds1da177e2005-04-16 15:20:36 -07001778 drive = tape->drive;
1779
1780 filp->private_data = tape;
1781
Borislav Petkovf2e3ab52008-07-23 19:56:01 +02001782 if (test_and_set_bit(IDE_AFLAG_BUSY, &drive->atapi_flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001783 retval = -EBUSY;
1784 goto out_put_tape;
1785 }
1786
1787 retval = idetape_wait_ready(drive, 60 * HZ);
1788 if (retval) {
Borislav Petkovf2e3ab52008-07-23 19:56:01 +02001789 clear_bit(IDE_AFLAG_BUSY, &drive->atapi_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790 printk(KERN_ERR "ide-tape: %s: drive not ready\n", tape->name);
1791 goto out_put_tape;
1792 }
1793
1794 idetape_read_position(drive);
Borislav Petkovf2e3ab52008-07-23 19:56:01 +02001795 if (!test_bit(IDE_AFLAG_ADDRESS_VALID, &drive->atapi_flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001796 (void)idetape_rewind_tape(drive);
1797
Linus Torvalds1da177e2005-04-16 15:20:36 -07001798 /* Read block size and write protect status from drive. */
Borislav Petkov3cffb9c2008-02-02 19:56:50 +01001799 ide_tape_get_bsize_from_bdesc(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001800
1801 /* Set write protect flag if device is opened as read-only. */
1802 if ((filp->f_flags & O_ACCMODE) == O_RDONLY)
1803 tape->write_prot = 1;
1804 else
1805 tape->write_prot = tape->drv_write_prot;
1806
1807 /* Make sure drive isn't write protected if user wants to write. */
1808 if (tape->write_prot) {
1809 if ((filp->f_flags & O_ACCMODE) == O_WRONLY ||
1810 (filp->f_flags & O_ACCMODE) == O_RDWR) {
Borislav Petkovf2e3ab52008-07-23 19:56:01 +02001811 clear_bit(IDE_AFLAG_BUSY, &drive->atapi_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001812 retval = -EROFS;
1813 goto out_put_tape;
1814 }
1815 }
1816
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001817 /* Lock the tape drive door so user can't eject. */
Borislav Petkov54abf372008-02-06 02:57:52 +01001818 if (tape->chrdev_dir == IDETAPE_DIR_NONE) {
Bartlomiej Zolnierkiewicz05780422008-10-10 22:39:38 +02001819 if (!ide_set_media_lock(drive, tape->disk, 1)) {
Bartlomiej Zolnierkiewicz385a4b82008-10-10 22:39:37 +02001820 if (tape->door_locked != DOOR_EXPLICITLY_LOCKED)
1821 tape->door_locked = DOOR_LOCKED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001822 }
1823 }
Jonathan Corbet04f4ac92008-05-15 12:01:56 -06001824 unlock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825 return 0;
1826
1827out_put_tape:
1828 ide_tape_put(tape);
Jonathan Corbet04f4ac92008-05-15 12:01:56 -06001829 unlock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001830 return retval;
1831}
1832
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001833static void idetape_write_release(ide_drive_t *drive, unsigned int minor)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001834{
1835 idetape_tape_t *tape = drive->driver_data;
1836
Borislav Petkovd9df9372008-04-27 15:38:34 +02001837 ide_tape_flush_merge_buffer(drive);
Tejun Heo35ab8d32009-04-19 08:46:02 +09001838 tape->merge_bh = ide_tape_kmalloc_buffer(tape, 1);
Borislav Petkov077e3bd2008-04-27 15:38:34 +02001839 if (tape->merge_bh != NULL) {
Borislav Petkov54bb2072008-02-06 02:57:52 +01001840 idetape_pad_zeros(drive, tape->blk_size *
1841 (tape->user_bs_factor - 1));
Borislav Petkov077e3bd2008-04-27 15:38:34 +02001842 ide_tape_kfree_buffer(tape);
1843 tape->merge_bh = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001844 }
1845 idetape_write_filemark(drive);
1846 idetape_flush_tape_buffers(drive);
1847 idetape_flush_tape_buffers(drive);
1848}
1849
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001850static int idetape_chrdev_release(struct inode *inode, struct file *filp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001851{
Borislav Petkov5aeddf92008-10-13 21:39:34 +02001852 struct ide_tape_obj *tape = filp->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001853 ide_drive_t *drive = tape->drive;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001854 unsigned int minor = iminor(inode);
1855
1856 lock_kernel();
1857 tape = drive->driver_data;
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001858
1859 debug_log(DBG_CHRDEV, "Enter %s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001860
Borislav Petkov54abf372008-02-06 02:57:52 +01001861 if (tape->chrdev_dir == IDETAPE_DIR_WRITE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001862 idetape_write_release(drive, minor);
Borislav Petkov54abf372008-02-06 02:57:52 +01001863 if (tape->chrdev_dir == IDETAPE_DIR_READ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001864 if (minor < 128)
Borislav Petkovec0fdb02008-04-27 15:38:34 +02001865 ide_tape_discard_merge_buffer(drive, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001866 }
Borislav Petkovf64eee72008-04-27 15:38:25 +02001867
Borislav Petkovf2e3ab52008-07-23 19:56:01 +02001868 if (minor < 128 && test_bit(IDE_AFLAG_MEDIUM_PRESENT, &drive->atapi_flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001869 (void) idetape_rewind_tape(drive);
Borislav Petkov54abf372008-02-06 02:57:52 +01001870 if (tape->chrdev_dir == IDETAPE_DIR_NONE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001871 if (tape->door_locked == DOOR_LOCKED) {
Bartlomiej Zolnierkiewicz05780422008-10-10 22:39:38 +02001872 if (!ide_set_media_lock(drive, tape->disk, 0))
Bartlomiej Zolnierkiewicz385a4b82008-10-10 22:39:37 +02001873 tape->door_locked = DOOR_UNLOCKED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001874 }
1875 }
Borislav Petkovf2e3ab52008-07-23 19:56:01 +02001876 clear_bit(IDE_AFLAG_BUSY, &drive->atapi_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001877 ide_tape_put(tape);
1878 unlock_kernel();
1879 return 0;
1880}
1881
Borislav Petkov6d29c8f2008-02-02 19:56:49 +01001882static void idetape_get_inquiry_results(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001883{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001884 idetape_tape_t *tape = drive->driver_data;
Borislav Petkovd236d742008-04-18 00:46:27 +02001885 struct ide_atapi_pc pc;
Bartlomiej Zolnierkiewicz41fa9f82009-03-31 20:15:25 +02001886 u8 pc_buf[256];
Borislav Petkov801bd322008-09-27 19:32:17 +02001887 char fw_rev[4], vendor_id[8], product_id[16];
Borislav Petkov6d29c8f2008-02-02 19:56:49 +01001888
Linus Torvalds1da177e2005-04-16 15:20:36 -07001889 idetape_create_inquiry_cmd(&pc);
Bartlomiej Zolnierkiewicz41fa9f82009-03-31 20:15:25 +02001890 pc.buf = &pc_buf[0];
1891 pc.buf_size = sizeof(pc_buf);
1892
Bartlomiej Zolnierkiewicz2ac07d92008-10-10 22:39:38 +02001893 if (ide_queue_pc_tail(drive, tape->disk, &pc)) {
Borislav Petkov6d29c8f2008-02-02 19:56:49 +01001894 printk(KERN_ERR "ide-tape: %s: can't get INQUIRY results\n",
1895 tape->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001896 return;
1897 }
Borislav Petkovd236d742008-04-18 00:46:27 +02001898 memcpy(vendor_id, &pc.buf[8], 8);
1899 memcpy(product_id, &pc.buf[16], 16);
1900 memcpy(fw_rev, &pc.buf[32], 4);
Borislav Petkov6d29c8f2008-02-02 19:56:49 +01001901
Borislav Petkov801bd322008-09-27 19:32:17 +02001902 ide_fixstring(vendor_id, 8, 0);
1903 ide_fixstring(product_id, 16, 0);
1904 ide_fixstring(fw_rev, 4, 0);
Borislav Petkov41f81d542008-02-06 02:57:52 +01001905
Borislav Petkov801bd322008-09-27 19:32:17 +02001906 printk(KERN_INFO "ide-tape: %s <-> %s: %.8s %.16s rev %.4s\n",
Borislav Petkov41f81d542008-02-06 02:57:52 +01001907 drive->name, tape->name, vendor_id, product_id, fw_rev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001908}
1909
1910/*
Borislav Petkovb6422012008-02-02 19:56:49 +01001911 * Ask the tape about its various parameters. In particular, we will adjust our
1912 * data transfer buffer size to the recommended value as returned by the tape.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001913 */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001914static void idetape_get_mode_sense_results(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001915{
1916 idetape_tape_t *tape = drive->driver_data;
Borislav Petkovd236d742008-04-18 00:46:27 +02001917 struct ide_atapi_pc pc;
Borislav Petkovb6422012008-02-02 19:56:49 +01001918 u8 *caps;
1919 u8 speed, max_speed;
Borislav Petkov47314fa2008-02-02 19:56:48 +01001920
Linus Torvalds1da177e2005-04-16 15:20:36 -07001921 idetape_create_mode_sense_cmd(&pc, IDETAPE_CAPABILITIES_PAGE);
Bartlomiej Zolnierkiewicz2ac07d92008-10-10 22:39:38 +02001922 if (ide_queue_pc_tail(drive, tape->disk, &pc)) {
Borislav Petkovb6422012008-02-02 19:56:49 +01001923 printk(KERN_ERR "ide-tape: Can't get tape parameters - assuming"
1924 " some default values\n");
Borislav Petkov54bb2072008-02-06 02:57:52 +01001925 tape->blk_size = 512;
Borislav Petkovb6422012008-02-02 19:56:49 +01001926 put_unaligned(52, (u16 *)&tape->caps[12]);
1927 put_unaligned(540, (u16 *)&tape->caps[14]);
1928 put_unaligned(6*52, (u16 *)&tape->caps[16]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001929 return;
1930 }
Borislav Petkovd236d742008-04-18 00:46:27 +02001931 caps = pc.buf + 4 + pc.buf[3];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001932
Borislav Petkovb6422012008-02-02 19:56:49 +01001933 /* convert to host order and save for later use */
Harvey Harrisoncd740ab2008-07-24 22:53:33 +02001934 speed = be16_to_cpup((__be16 *)&caps[14]);
1935 max_speed = be16_to_cpup((__be16 *)&caps[8]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001936
Harvey Harrisoncd740ab2008-07-24 22:53:33 +02001937 *(u16 *)&caps[8] = max_speed;
1938 *(u16 *)&caps[12] = be16_to_cpup((__be16 *)&caps[12]);
1939 *(u16 *)&caps[14] = speed;
1940 *(u16 *)&caps[16] = be16_to_cpup((__be16 *)&caps[16]);
Borislav Petkovb6422012008-02-02 19:56:49 +01001941
1942 if (!speed) {
1943 printk(KERN_INFO "ide-tape: %s: invalid tape speed "
1944 "(assuming 650KB/sec)\n", drive->name);
Harvey Harrisoncd740ab2008-07-24 22:53:33 +02001945 *(u16 *)&caps[14] = 650;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001946 }
Borislav Petkovb6422012008-02-02 19:56:49 +01001947 if (!max_speed) {
1948 printk(KERN_INFO "ide-tape: %s: invalid max_speed "
1949 "(assuming 650KB/sec)\n", drive->name);
Harvey Harrisoncd740ab2008-07-24 22:53:33 +02001950 *(u16 *)&caps[8] = 650;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001951 }
1952
Borislav Petkovb6422012008-02-02 19:56:49 +01001953 memcpy(&tape->caps, caps, 20);
Bartlomiej Zolnierkiewicz05780422008-10-10 22:39:38 +02001954
1955 /* device lacks locking support according to capabilities page */
1956 if ((caps[6] & 1) == 0)
Bartlomiej Zolnierkiewicz42619d32008-10-17 18:09:11 +02001957 drive->dev_flags &= ~IDE_DFLAG_DOORLOCKING;
Bartlomiej Zolnierkiewicz05780422008-10-10 22:39:38 +02001958
Borislav Petkovb6422012008-02-02 19:56:49 +01001959 if (caps[7] & 0x02)
Borislav Petkov54bb2072008-02-06 02:57:52 +01001960 tape->blk_size = 512;
Borislav Petkovb6422012008-02-02 19:56:49 +01001961 else if (caps[7] & 0x04)
Borislav Petkov54bb2072008-02-06 02:57:52 +01001962 tape->blk_size = 1024;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001963}
1964
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +02001965#ifdef CONFIG_IDE_PROC_FS
Bartlomiej Zolnierkiewicz8185d5a2008-10-10 22:39:28 +02001966#define ide_tape_devset_get(name, field) \
1967static int get_##name(ide_drive_t *drive) \
1968{ \
1969 idetape_tape_t *tape = drive->driver_data; \
1970 return tape->field; \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001971}
Bartlomiej Zolnierkiewicz8185d5a2008-10-10 22:39:28 +02001972
1973#define ide_tape_devset_set(name, field) \
1974static int set_##name(ide_drive_t *drive, int arg) \
1975{ \
1976 idetape_tape_t *tape = drive->driver_data; \
1977 tape->field = arg; \
1978 return 0; \
1979}
1980
Elias Oltmanns92f1f8f2008-10-10 22:39:40 +02001981#define ide_tape_devset_rw_field(_name, _field) \
Bartlomiej Zolnierkiewicz8185d5a2008-10-10 22:39:28 +02001982ide_tape_devset_get(_name, _field) \
1983ide_tape_devset_set(_name, _field) \
Elias Oltmanns92f1f8f2008-10-10 22:39:40 +02001984IDE_DEVSET(_name, DS_SYNC, get_##_name, set_##_name)
Bartlomiej Zolnierkiewicz8185d5a2008-10-10 22:39:28 +02001985
Elias Oltmanns92f1f8f2008-10-10 22:39:40 +02001986#define ide_tape_devset_r_field(_name, _field) \
Bartlomiej Zolnierkiewicz8185d5a2008-10-10 22:39:28 +02001987ide_tape_devset_get(_name, _field) \
Elias Oltmanns92f1f8f2008-10-10 22:39:40 +02001988IDE_DEVSET(_name, 0, get_##_name, NULL)
Bartlomiej Zolnierkiewicz8185d5a2008-10-10 22:39:28 +02001989
1990static int mulf_tdsc(ide_drive_t *drive) { return 1000; }
1991static int divf_tdsc(ide_drive_t *drive) { return HZ; }
1992static int divf_buffer(ide_drive_t *drive) { return 2; }
1993static int divf_buffer_size(ide_drive_t *drive) { return 1024; }
1994
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +02001995ide_devset_rw_flag(dsc_overlap, IDE_DFLAG_DSC_OVERLAP);
Bartlomiej Zolnierkiewicz8185d5a2008-10-10 22:39:28 +02001996
Elias Oltmanns92f1f8f2008-10-10 22:39:40 +02001997ide_tape_devset_rw_field(debug_mask, debug_mask);
1998ide_tape_devset_rw_field(tdsc, best_dsc_rw_freq);
Bartlomiej Zolnierkiewicz8185d5a2008-10-10 22:39:28 +02001999
Elias Oltmanns92f1f8f2008-10-10 22:39:40 +02002000ide_tape_devset_r_field(avg_speed, avg_speed);
2001ide_tape_devset_r_field(speed, caps[14]);
2002ide_tape_devset_r_field(buffer, caps[16]);
2003ide_tape_devset_r_field(buffer_size, buffer_size);
Bartlomiej Zolnierkiewicz8185d5a2008-10-10 22:39:28 +02002004
Elias Oltmanns92f1f8f2008-10-10 22:39:40 +02002005static const struct ide_proc_devset idetape_settings[] = {
2006 __IDE_PROC_DEVSET(avg_speed, 0, 0xffff, NULL, NULL),
2007 __IDE_PROC_DEVSET(buffer, 0, 0xffff, NULL, divf_buffer),
2008 __IDE_PROC_DEVSET(buffer_size, 0, 0xffff, NULL, divf_buffer_size),
2009 __IDE_PROC_DEVSET(debug_mask, 0, 0xffff, NULL, NULL),
2010 __IDE_PROC_DEVSET(dsc_overlap, 0, 1, NULL, NULL),
2011 __IDE_PROC_DEVSET(speed, 0, 0xffff, NULL, NULL),
2012 __IDE_PROC_DEVSET(tdsc, IDETAPE_DSC_RW_MIN, IDETAPE_DSC_RW_MAX,
2013 mulf_tdsc, divf_tdsc),
Hannes Eder71bfc7a2009-03-05 16:10:56 +01002014 { NULL },
Bartlomiej Zolnierkiewicz8185d5a2008-10-10 22:39:28 +02002015};
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +02002016#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002017
2018/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +01002019 * The function below is called to:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002020 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +01002021 * 1. Initialize our various state variables.
2022 * 2. Ask the tape for its capabilities.
2023 * 3. Allocate a buffer which will be used for data transfer. The buffer size
2024 * is chosen based on the recommendation which we received in step 2.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002025 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +01002026 * Note that at this point ide.c already assigned us an irq, so that we can
2027 * queue requests here and wait for their completion.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002028 */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002029static void idetape_setup(ide_drive_t *drive, idetape_tape_t *tape, int minor)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002030{
Borislav Petkov83042b22008-04-27 15:38:27 +02002031 unsigned long t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002032 int speed;
Borislav Petkovf73850a2008-04-27 15:38:33 +02002033 int buffer_size;
Borislav Petkovb6422012008-02-02 19:56:49 +01002034 u16 *ctl = (u16 *)&tape->caps[12];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002035
Bartlomiej Zolnierkiewicz85e39032008-10-13 21:39:32 +02002036 drive->pc_callback = ide_tape_callback;
2037 drive->pc_update_buffers = idetape_update_buffers;
2038 drive->pc_io_buffers = ide_tape_io_buffers;
Borislav Petkov776bb022008-07-23 19:55:59 +02002039
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +02002040 drive->dev_flags |= IDE_DFLAG_DSC_OVERLAP;
2041
Bartlomiej Zolnierkiewicz4166c192008-02-01 23:09:30 +01002042 if (drive->hwif->host_flags & IDE_HFLAG_NO_DSC) {
2043 printk(KERN_INFO "ide-tape: %s: disabling DSC overlap\n",
2044 tape->name);
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +02002045 drive->dev_flags &= ~IDE_DFLAG_DSC_OVERLAP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002046 }
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +02002047
Linus Torvalds1da177e2005-04-16 15:20:36 -07002048 /* Seagate Travan drives do not support DSC overlap. */
Bartlomiej Zolnierkiewicz4dde4492008-10-10 22:39:19 +02002049 if (strstr((char *)&drive->id[ATA_ID_PROD], "Seagate STT3401"))
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +02002050 drive->dev_flags &= ~IDE_DFLAG_DSC_OVERLAP;
2051
Linus Torvalds1da177e2005-04-16 15:20:36 -07002052 tape->minor = minor;
2053 tape->name[0] = 'h';
2054 tape->name[1] = 't';
2055 tape->name[2] = '0' + minor;
Borislav Petkov54abf372008-02-06 02:57:52 +01002056 tape->chrdev_dir = IDETAPE_DIR_NONE;
Bartlomiej Zolnierkiewicz4dde4492008-10-10 22:39:19 +02002057
Linus Torvalds1da177e2005-04-16 15:20:36 -07002058 idetape_get_inquiry_results(drive);
2059 idetape_get_mode_sense_results(drive);
Borislav Petkov3cffb9c2008-02-02 19:56:50 +01002060 ide_tape_get_bsize_from_bdesc(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002061 tape->user_bs_factor = 1;
Borislav Petkovf73850a2008-04-27 15:38:33 +02002062 tape->buffer_size = *ctl * tape->blk_size;
2063 while (tape->buffer_size > 0xffff) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002064 printk(KERN_NOTICE "ide-tape: decreasing stage size\n");
Borislav Petkovb6422012008-02-02 19:56:49 +01002065 *ctl /= 2;
Borislav Petkovf73850a2008-04-27 15:38:33 +02002066 tape->buffer_size = *ctl * tape->blk_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002067 }
Borislav Petkovf73850a2008-04-27 15:38:33 +02002068 buffer_size = tape->buffer_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002069
Borislav Petkov83042b22008-04-27 15:38:27 +02002070 /* select the "best" DSC read/write polling freq */
Borislav Petkovb6422012008-02-02 19:56:49 +01002071 speed = max(*(u16 *)&tape->caps[14], *(u16 *)&tape->caps[8]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002072
Borislav Petkovf73850a2008-04-27 15:38:33 +02002073 t = (IDETAPE_FIFO_THRESHOLD * tape->buffer_size * HZ) / (speed * 1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002074
2075 /*
Borislav Petkov3c98bf32008-02-06 02:57:53 +01002076 * Ensure that the number we got makes sense; limit it within
2077 * IDETAPE_DSC_RW_MIN and IDETAPE_DSC_RW_MAX.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002078 */
Harvey Harrisona792bd52008-07-15 21:21:41 +02002079 tape->best_dsc_rw_freq = clamp_t(unsigned long, t, IDETAPE_DSC_RW_MIN,
2080 IDETAPE_DSC_RW_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002081 printk(KERN_INFO "ide-tape: %s <-> %s: %dKBps, %d*%dkB buffer, "
Borislav Petkov83042b22008-04-27 15:38:27 +02002082 "%lums tDSC%s\n",
Borislav Petkovb6422012008-02-02 19:56:49 +01002083 drive->name, tape->name, *(u16 *)&tape->caps[14],
Borislav Petkovf73850a2008-04-27 15:38:33 +02002084 (*(u16 *)&tape->caps[16] * 512) / tape->buffer_size,
2085 tape->buffer_size / 1024,
Borislav Petkov54bb2072008-02-06 02:57:52 +01002086 tape->best_dsc_rw_freq * 1000 / HZ,
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +02002087 (drive->dev_flags & IDE_DFLAG_USING_DMA) ? ", DMA" : "");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002088
Bartlomiej Zolnierkiewicz1e874f42008-10-10 22:39:27 +02002089 ide_proc_register_driver(drive, tape->driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002090}
2091
Russell King4031bbe2006-01-06 11:41:00 +00002092static void ide_tape_remove(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002093{
2094 idetape_tape_t *tape = drive->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002095
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +02002096 ide_proc_unregister_driver(drive, tape->driver);
Bartlomiej Zolnierkiewicz8fed4362009-02-25 20:28:24 +01002097 device_del(&tape->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002098 ide_unregister_region(tape->disk);
2099
Bartlomiej Zolnierkiewicz8fed4362009-02-25 20:28:24 +01002100 mutex_lock(&idetape_ref_mutex);
2101 put_device(&tape->dev);
2102 mutex_unlock(&idetape_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002103}
2104
Bartlomiej Zolnierkiewicz8fed4362009-02-25 20:28:24 +01002105static void ide_tape_release(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002106{
Bartlomiej Zolnierkiewicz8fed4362009-02-25 20:28:24 +01002107 struct ide_tape_obj *tape = to_ide_drv(dev, ide_tape_obj);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002108 ide_drive_t *drive = tape->drive;
2109 struct gendisk *g = tape->disk;
2110
Borislav Petkov01a63aeb2008-04-27 15:38:34 +02002111 BUG_ON(tape->merge_bh_size);
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02002112
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +02002113 drive->dev_flags &= ~IDE_DFLAG_DSC_OVERLAP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002114 drive->driver_data = NULL;
Tony Jonesdbc12722007-09-25 02:03:03 +02002115 device_destroy(idetape_sysfs_class, MKDEV(IDETAPE_MAJOR, tape->minor));
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002116 device_destroy(idetape_sysfs_class,
2117 MKDEV(IDETAPE_MAJOR, tape->minor + 128));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002118 idetape_devs[tape->minor] = NULL;
2119 g->private_data = NULL;
2120 put_disk(g);
2121 kfree(tape);
2122}
2123
Bartlomiej Zolnierkiewiczecfd80e2007-05-10 00:01:09 +02002124#ifdef CONFIG_IDE_PROC_FS
Linus Torvalds1da177e2005-04-16 15:20:36 -07002125static int proc_idetape_read_name
2126 (char *page, char **start, off_t off, int count, int *eof, void *data)
2127{
2128 ide_drive_t *drive = (ide_drive_t *) data;
2129 idetape_tape_t *tape = drive->driver_data;
2130 char *out = page;
2131 int len;
2132
2133 len = sprintf(out, "%s\n", tape->name);
2134 PROC_IDE_READ_RETURN(page, start, off, count, eof, len);
2135}
2136
2137static ide_proc_entry_t idetape_proc[] = {
2138 { "capacity", S_IFREG|S_IRUGO, proc_ide_read_capacity, NULL },
2139 { "name", S_IFREG|S_IRUGO, proc_idetape_read_name, NULL },
2140 { NULL, 0, NULL, NULL }
2141};
Bartlomiej Zolnierkiewicz79cb3802008-10-17 18:09:13 +02002142
2143static ide_proc_entry_t *ide_tape_proc_entries(ide_drive_t *drive)
2144{
2145 return idetape_proc;
2146}
2147
2148static const struct ide_proc_devset *ide_tape_proc_devsets(ide_drive_t *drive)
2149{
2150 return idetape_settings;
2151}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002152#endif
2153
Russell King4031bbe2006-01-06 11:41:00 +00002154static int ide_tape_probe(ide_drive_t *);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002155
Bartlomiej Zolnierkiewicz7f3c8682009-01-06 17:20:53 +01002156static struct ide_driver idetape_driver = {
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02002157 .gen_driver = {
Laurent Riffard4ef3b8f2005-11-18 22:15:40 +01002158 .owner = THIS_MODULE,
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02002159 .name = "ide-tape",
2160 .bus = &ide_bus_type,
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02002161 },
Russell King4031bbe2006-01-06 11:41:00 +00002162 .probe = ide_tape_probe,
2163 .remove = ide_tape_remove,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002164 .version = IDETAPE_VERSION,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002165 .do_request = idetape_do_request,
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +02002166#ifdef CONFIG_IDE_PROC_FS
Bartlomiej Zolnierkiewicz79cb3802008-10-17 18:09:13 +02002167 .proc_entries = ide_tape_proc_entries,
2168 .proc_devsets = ide_tape_proc_devsets,
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +02002169#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002170};
2171
Borislav Petkov3c98bf32008-02-06 02:57:53 +01002172/* Our character device supporting functions, passed to register_chrdev. */
Arjan van de Ven2b8693c2007-02-12 00:55:32 -08002173static const struct file_operations idetape_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002174 .owner = THIS_MODULE,
2175 .read = idetape_chrdev_read,
2176 .write = idetape_chrdev_write,
2177 .ioctl = idetape_chrdev_ioctl,
2178 .open = idetape_chrdev_open,
2179 .release = idetape_chrdev_release,
2180};
2181
Al Viroa4600f82008-03-02 10:27:58 -05002182static int idetape_open(struct block_device *bdev, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002183{
Al Viroa4600f82008-03-02 10:27:58 -05002184 struct ide_tape_obj *tape = ide_tape_get(bdev->bd_disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002185
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002186 if (!tape)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002187 return -ENXIO;
2188
Linus Torvalds1da177e2005-04-16 15:20:36 -07002189 return 0;
2190}
2191
Al Viroa4600f82008-03-02 10:27:58 -05002192static int idetape_release(struct gendisk *disk, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002193{
Borislav Petkov5aeddf92008-10-13 21:39:34 +02002194 struct ide_tape_obj *tape = ide_drv_g(disk, ide_tape_obj);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002195
2196 ide_tape_put(tape);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002197 return 0;
2198}
2199
Al Viroa4600f82008-03-02 10:27:58 -05002200static int idetape_ioctl(struct block_device *bdev, fmode_t mode,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002201 unsigned int cmd, unsigned long arg)
2202{
Borislav Petkov5aeddf92008-10-13 21:39:34 +02002203 struct ide_tape_obj *tape = ide_drv_g(bdev->bd_disk, ide_tape_obj);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002204 ide_drive_t *drive = tape->drive;
Al Viro1bddd9e2008-09-02 17:19:43 -04002205 int err = generic_ide_ioctl(drive, bdev, cmd, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002206 if (err == -EINVAL)
2207 err = idetape_blkdev_ioctl(drive, cmd, arg);
2208 return err;
2209}
2210
2211static struct block_device_operations idetape_block_ops = {
2212 .owner = THIS_MODULE,
Al Viroa4600f82008-03-02 10:27:58 -05002213 .open = idetape_open,
2214 .release = idetape_release,
2215 .locked_ioctl = idetape_ioctl,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002216};
2217
Russell King4031bbe2006-01-06 11:41:00 +00002218static int ide_tape_probe(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002219{
2220 idetape_tape_t *tape;
2221 struct gendisk *g;
2222 int minor;
2223
2224 if (!strstr("ide-tape", drive->driver_req))
2225 goto failed;
Bartlomiej Zolnierkiewicz2a924662008-10-10 22:39:24 +02002226
Linus Torvalds1da177e2005-04-16 15:20:36 -07002227 if (drive->media != ide_tape)
2228 goto failed;
Bartlomiej Zolnierkiewicz2a924662008-10-10 22:39:24 +02002229
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +02002230 if ((drive->dev_flags & IDE_DFLAG_ID_READ) &&
2231 ide_check_atapi_device(drive, DRV_NAME) == 0) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002232 printk(KERN_ERR "ide-tape: %s: not supported by this version of"
2233 " the driver\n", drive->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002234 goto failed;
2235 }
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002236 tape = kzalloc(sizeof(idetape_tape_t), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002237 if (tape == NULL) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002238 printk(KERN_ERR "ide-tape: %s: Can't allocate a tape struct\n",
2239 drive->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002240 goto failed;
2241 }
2242
2243 g = alloc_disk(1 << PARTN_BITS);
2244 if (!g)
2245 goto out_free_tape;
2246
2247 ide_init_disk(g, drive);
2248
Bartlomiej Zolnierkiewicz8fed4362009-02-25 20:28:24 +01002249 tape->dev.parent = &drive->gendev;
2250 tape->dev.release = ide_tape_release;
2251 dev_set_name(&tape->dev, dev_name(&drive->gendev));
2252
2253 if (device_register(&tape->dev))
2254 goto out_free_disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002255
2256 tape->drive = drive;
2257 tape->driver = &idetape_driver;
2258 tape->disk = g;
2259
2260 g->private_data = &tape->driver;
2261
2262 drive->driver_data = tape;
2263
Arjan van de Vencf8b8972006-03-23 03:00:45 -08002264 mutex_lock(&idetape_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002265 for (minor = 0; idetape_devs[minor]; minor++)
2266 ;
2267 idetape_devs[minor] = tape;
Arjan van de Vencf8b8972006-03-23 03:00:45 -08002268 mutex_unlock(&idetape_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002269
2270 idetape_setup(drive, tape, minor);
2271
Greg Kroah-Hartman3ee074b2008-07-21 20:03:34 -07002272 device_create(idetape_sysfs_class, &drive->gendev,
2273 MKDEV(IDETAPE_MAJOR, minor), NULL, "%s", tape->name);
2274 device_create(idetape_sysfs_class, &drive->gendev,
2275 MKDEV(IDETAPE_MAJOR, minor + 128), NULL,
2276 "n%s", tape->name);
Will Dysond5dee802005-09-16 02:55:07 -07002277
Linus Torvalds1da177e2005-04-16 15:20:36 -07002278 g->fops = &idetape_block_ops;
2279 ide_register_region(g);
2280
2281 return 0;
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02002282
Bartlomiej Zolnierkiewicz8fed4362009-02-25 20:28:24 +01002283out_free_disk:
2284 put_disk(g);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002285out_free_tape:
2286 kfree(tape);
2287failed:
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02002288 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002289}
2290
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002291static void __exit idetape_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002292{
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02002293 driver_unregister(&idetape_driver.gen_driver);
Will Dysond5dee802005-09-16 02:55:07 -07002294 class_destroy(idetape_sysfs_class);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002295 unregister_chrdev(IDETAPE_MAJOR, "ht");
2296}
2297
Bartlomiej Zolnierkiewicz17514e82005-11-19 22:24:35 +01002298static int __init idetape_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002299{
Will Dysond5dee802005-09-16 02:55:07 -07002300 int error = 1;
2301 idetape_sysfs_class = class_create(THIS_MODULE, "ide_tape");
2302 if (IS_ERR(idetape_sysfs_class)) {
2303 idetape_sysfs_class = NULL;
2304 printk(KERN_ERR "Unable to create sysfs class for ide tapes\n");
2305 error = -EBUSY;
2306 goto out;
2307 }
2308
Linus Torvalds1da177e2005-04-16 15:20:36 -07002309 if (register_chrdev(IDETAPE_MAJOR, "ht", &idetape_fops)) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002310 printk(KERN_ERR "ide-tape: Failed to register chrdev"
2311 " interface\n");
Will Dysond5dee802005-09-16 02:55:07 -07002312 error = -EBUSY;
2313 goto out_free_class;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002314 }
Will Dysond5dee802005-09-16 02:55:07 -07002315
2316 error = driver_register(&idetape_driver.gen_driver);
2317 if (error)
2318 goto out_free_driver;
2319
2320 return 0;
2321
2322out_free_driver:
2323 driver_unregister(&idetape_driver.gen_driver);
2324out_free_class:
2325 class_destroy(idetape_sysfs_class);
2326out:
2327 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002328}
2329
Kay Sievers263756e2005-12-12 18:03:44 +01002330MODULE_ALIAS("ide:*m-tape*");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002331module_init(idetape_init);
2332module_exit(idetape_exit);
2333MODULE_ALIAS_CHARDEV_MAJOR(IDETAPE_MAJOR);
Borislav Petkov9c145762008-02-06 02:57:54 +01002334MODULE_DESCRIPTION("ATAPI Streaming TAPE Driver");
2335MODULE_LICENSE("GPL");