blob: 7f56f20033423fde710b05e0adc40575c1f1f0ba [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),
59 /* buffer alloc info (pc_stack & rq_stack) */
60 DBG_PCRQ_STACK = (1 << 4),
61};
62
63/* define to see debug info */
64#define IDETAPE_DEBUG_LOG 0
65
66#if IDETAPE_DEBUG_LOG
67#define debug_log(lvl, fmt, args...) \
68{ \
69 if (tape->debug_mask & lvl) \
70 printk(KERN_INFO "ide-tape: " fmt, ## args); \
71}
72#else
73#define debug_log(lvl, fmt, args...) do {} while (0)
74#endif
75
Linus Torvalds1da177e2005-04-16 15:20:36 -070076/**************************** Tunable parameters *****************************/
Linus Torvalds1da177e2005-04-16 15:20:36 -070077/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +010078 * After each failed packet command we issue a request sense command and retry
79 * the packet command IDETAPE_MAX_PC_RETRIES times.
Linus Torvalds1da177e2005-04-16 15:20:36 -070080 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +010081 * Setting IDETAPE_MAX_PC_RETRIES to 0 will disable retries.
Linus Torvalds1da177e2005-04-16 15:20:36 -070082 */
83#define IDETAPE_MAX_PC_RETRIES 3
84
85/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +010086 * With each packet command, we allocate a buffer of IDETAPE_PC_BUFFER_SIZE
87 * bytes. This is used for several packet commands (Not for READ/WRITE commands)
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 */
89#define IDETAPE_PC_BUFFER_SIZE 256
90
91/*
92 * In various places in the driver, we need to allocate storage
93 * for packet commands and requests, which will remain valid while
94 * we leave the driver to wait for an interrupt or a timeout event.
95 */
96#define IDETAPE_PC_STACK (10 + IDETAPE_MAX_PC_RETRIES)
97
98/*
99 * Some drives (for example, Seagate STT3401A Travan) require a very long
100 * timeout, because they don't return an interrupt or clear their busy bit
101 * until after the command completes (even retension commands).
102 */
103#define IDETAPE_WAIT_CMD (900*HZ)
104
105/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100106 * The following parameter is used to select the point in the internal tape fifo
107 * in which we will start to refill the buffer. Decreasing the following
108 * parameter will improve the system's latency and interactive response, while
109 * using a high value might improve system throughput.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 */
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100111#define IDETAPE_FIFO_THRESHOLD 2
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112
113/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100114 * DSC polling parameters.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100116 * Polling for DSC (a single bit in the status register) is a very important
117 * function in ide-tape. There are two cases in which we poll for DSC:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100119 * 1. Before a read/write packet command, to ensure that we can transfer data
120 * from/to the tape's data buffers, without causing an actual media access.
121 * In case the tape is not ready yet, we take out our request from the device
122 * request queue, so that ide.c could service requests from the other device
123 * on the same interface in the meantime.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100125 * 2. After the successful initialization of a "media access packet command",
126 * which is a command that can take a long time to complete (the interval can
127 * range from several seconds to even an hour). Again, we postpone our request
128 * in the middle to free the bus for the other device. The polling frequency
129 * here should be lower than the read/write frequency since those media access
130 * commands are slow. We start from a "fast" frequency - IDETAPE_DSC_MA_FAST
131 * (1 second), and if we don't receive DSC after IDETAPE_DSC_MA_THRESHOLD
132 * (5 min), we switch it to a lower frequency - IDETAPE_DSC_MA_SLOW (1 min).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100134 * We also set a timeout for the timer, in case something goes wrong. The
135 * timeout should be longer then the maximum execution time of a tape operation.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 */
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100137
138/* DSC timings. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139#define IDETAPE_DSC_RW_MIN 5*HZ/100 /* 50 msec */
140#define IDETAPE_DSC_RW_MAX 40*HZ/100 /* 400 msec */
141#define IDETAPE_DSC_RW_TIMEOUT 2*60*HZ /* 2 minutes */
142#define IDETAPE_DSC_MA_FAST 2*HZ /* 2 seconds */
143#define IDETAPE_DSC_MA_THRESHOLD 5*60*HZ /* 5 minutes */
144#define IDETAPE_DSC_MA_SLOW 30*HZ /* 30 seconds */
145#define IDETAPE_DSC_MA_TIMEOUT 2*60*60*HZ /* 2 hours */
146
147/*************************** End of tunable parameters ***********************/
148
Borislav Petkov54abf372008-02-06 02:57:52 +0100149/* tape directions */
150enum {
151 IDETAPE_DIR_NONE = (1 << 0),
152 IDETAPE_DIR_READ = (1 << 1),
153 IDETAPE_DIR_WRITE = (1 << 2),
154};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155
156struct idetape_bh {
Stephen Rothwellab057962007-08-01 23:46:44 +0200157 u32 b_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 atomic_t b_count;
159 struct idetape_bh *b_reqnext;
160 char *b_data;
161};
162
Borislav Petkov03056b92008-04-18 00:46:26 +0200163/* Tape door status */
164#define DOOR_UNLOCKED 0
165#define DOOR_LOCKED 1
166#define DOOR_EXPLICITLY_LOCKED 2
167
168/* Some defines for the SPACE command */
169#define IDETAPE_SPACE_OVER_FILEMARK 1
170#define IDETAPE_SPACE_TO_EOD 3
171
172/* Some defines for the LOAD UNLOAD command */
173#define IDETAPE_LU_LOAD_MASK 1
174#define IDETAPE_LU_RETENSION_MASK 2
175#define IDETAPE_LU_EOT_MASK 4
176
177/*
178 * Special requests for our block device strategy routine.
179 *
180 * In order to service a character device command, we add special requests to
181 * the tail of our block device request queue and wait for their completion.
182 */
183
184enum {
185 REQ_IDETAPE_PC1 = (1 << 0), /* packet command (first stage) */
186 REQ_IDETAPE_PC2 = (1 << 1), /* packet command (second stage) */
187 REQ_IDETAPE_READ = (1 << 2),
188 REQ_IDETAPE_WRITE = (1 << 3),
189};
190
191/* Error codes returned in rq->errors to the higher part of the driver. */
192#define IDETAPE_ERROR_GENERAL 101
193#define IDETAPE_ERROR_FILEMARK 102
194#define IDETAPE_ERROR_EOD 103
195
196/* Structures related to the SELECT SENSE / MODE SENSE packet commands. */
197#define IDETAPE_BLOCK_DESCRIPTOR 0
198#define IDETAPE_CAPABILITIES_PAGE 0x2a
199
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100201 * Most of our global data which we need to save even as we leave the driver due
202 * to an interrupt or a timer event is stored in the struct defined below.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 */
204typedef struct ide_tape_obj {
205 ide_drive_t *drive;
206 ide_driver_t *driver;
207 struct gendisk *disk;
208 struct kref kref;
209
210 /*
211 * Since a typical character device operation requires more
212 * than one packet command, we provide here enough memory
213 * for the maximum of interconnected packet commands.
214 * The packet commands are stored in the circular array pc_stack.
215 * pc_stack_index points to the last used entry, and warps around
216 * to the start when we get to the last array entry.
217 *
218 * pc points to the current processed packet command.
219 *
220 * failed_pc points to the last failed packet command, or contains
221 * NULL if we do not need to retry any packet command. This is
222 * required since an additional packet command is needed before the
223 * retry, to get detailed information on what went wrong.
224 */
225 /* Current packet command */
Borislav Petkovd236d742008-04-18 00:46:27 +0200226 struct ide_atapi_pc *pc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 /* Last failed packet command */
Borislav Petkovd236d742008-04-18 00:46:27 +0200228 struct ide_atapi_pc *failed_pc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 /* Packet command stack */
Borislav Petkovd236d742008-04-18 00:46:27 +0200230 struct ide_atapi_pc pc_stack[IDETAPE_PC_STACK];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 /* Next free packet command storage space */
232 int pc_stack_index;
233 struct request rq_stack[IDETAPE_PC_STACK];
234 /* We implement a circular array */
235 int rq_stack_index;
236
237 /*
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100238 * DSC polling variables.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100240 * While polling for DSC we use postponed_rq to postpone the current
241 * request so that ide.c will be able to service pending requests on the
242 * other device. Note that at most we will have only one DSC (usually
Borislav Petkov5bd50dc2008-04-27 15:38:28 +0200243 * data transfer) request in the device request queue.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 */
245 struct request *postponed_rq;
246 /* The time in which we started polling for DSC */
247 unsigned long dsc_polling_start;
248 /* Timer used to poll for dsc */
249 struct timer_list dsc_timer;
250 /* Read/Write dsc polling frequency */
Borislav Petkov54bb2072008-02-06 02:57:52 +0100251 unsigned long best_dsc_rw_freq;
252 unsigned long dsc_poll_freq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 unsigned long dsc_timeout;
254
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100255 /* Read position information */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 u8 partition;
257 /* Current block */
Borislav Petkov54bb2072008-02-06 02:57:52 +0100258 unsigned int first_frame;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100260 /* Last error information */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 u8 sense_key, asc, ascq;
262
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100263 /* Character device operation */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 unsigned int minor;
265 /* device name */
266 char name[4];
267 /* Current character device data transfer direction */
Borislav Petkov54abf372008-02-06 02:57:52 +0100268 u8 chrdev_dir;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269
Borislav Petkov54bb2072008-02-06 02:57:52 +0100270 /* tape block size, usually 512 or 1024 bytes */
271 unsigned short blk_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 int user_bs_factor;
Borislav Petkovb6422012008-02-02 19:56:49 +0100273
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 /* Copy of the tape's Capabilities and Mechanical Page */
Borislav Petkovb6422012008-02-02 19:56:49 +0100275 u8 caps[20];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276
277 /*
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100278 * Active data transfer request parameters.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100280 * At most, there is only one ide-tape originated data transfer request
281 * in the device request queue. This allows ide.c to easily service
282 * requests from the other device when we postpone our active request.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 */
Borislav Petkov83042b22008-04-27 15:38:27 +0200284
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100285 /* Data buffer size chosen based on the tape's recommendation */
Borislav Petkovf73850a2008-04-27 15:38:33 +0200286 int buffer_size;
Borislav Petkov077e3bd2008-04-27 15:38:34 +0200287 /* merge buffer */
288 struct idetape_bh *merge_bh;
Borislav Petkov01a63aeb2008-04-27 15:38:34 +0200289 /* size of the merge buffer */
290 int merge_bh_size;
Borislav Petkov077e3bd2008-04-27 15:38:34 +0200291 /* pointer to current buffer head within the merge buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 struct idetape_bh *bh;
293 char *b_data;
294 int b_count;
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100295
Borislav Petkova997a432008-04-27 15:38:33 +0200296 int pages_per_buffer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 /* Wasted space in each stage */
298 int excess_bh_size;
299
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 /* protects the ide-tape queue */
Borislav Petkov54bb2072008-02-06 02:57:52 +0100301 spinlock_t lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100303 /* Measures average tape speed */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 unsigned long avg_time;
305 int avg_size;
306 int avg_speed;
307
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 /* the door is currently locked */
309 int door_locked;
310 /* the tape hardware is write protected */
311 char drv_write_prot;
312 /* the tape is write protected (hardware or opened as read-only) */
313 char write_prot;
314
Borislav Petkov8004a8c2008-02-06 02:57:51 +0100315 u32 debug_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316} idetape_tape_t;
317
Arjan van de Vencf8b8972006-03-23 03:00:45 -0800318static DEFINE_MUTEX(idetape_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319
Will Dysond5dee802005-09-16 02:55:07 -0700320static struct class *idetape_sysfs_class;
321
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322#define to_ide_tape(obj) container_of(obj, struct ide_tape_obj, kref)
323
324#define ide_tape_g(disk) \
325 container_of((disk)->private_data, struct ide_tape_obj, driver)
326
Bartlomiej Zolnierkiewicz08da5912008-07-24 22:53:15 +0200327static void ide_tape_release(struct kref *);
328
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329static struct ide_tape_obj *ide_tape_get(struct gendisk *disk)
330{
331 struct ide_tape_obj *tape = NULL;
332
Arjan van de Vencf8b8972006-03-23 03:00:45 -0800333 mutex_lock(&idetape_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 tape = ide_tape_g(disk);
Bartlomiej Zolnierkiewicz08da5912008-07-24 22:53:15 +0200335 if (tape) {
Bartlomiej Zolnierkiewiczd3e33ff2008-08-05 18:16:59 +0200336 if (ide_device_get(tape->drive))
Bartlomiej Zolnierkiewicz08da5912008-07-24 22:53:15 +0200337 tape = NULL;
Bartlomiej Zolnierkiewiczd3e33ff2008-08-05 18:16:59 +0200338 else
339 kref_get(&tape->kref);
Bartlomiej Zolnierkiewicz08da5912008-07-24 22:53:15 +0200340 }
Arjan van de Vencf8b8972006-03-23 03:00:45 -0800341 mutex_unlock(&idetape_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 return tape;
343}
344
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345static void ide_tape_put(struct ide_tape_obj *tape)
346{
Bartlomiej Zolnierkiewiczd3e33ff2008-08-05 18:16:59 +0200347 ide_drive_t *drive = tape->drive;
348
Arjan van de Vencf8b8972006-03-23 03:00:45 -0800349 mutex_lock(&idetape_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 kref_put(&tape->kref, ide_tape_release);
Bartlomiej Zolnierkiewiczd3e33ff2008-08-05 18:16:59 +0200351 ide_device_put(drive);
Arjan van de Vencf8b8972006-03-23 03:00:45 -0800352 mutex_unlock(&idetape_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353}
354
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100356 * The variables below are used for the character device interface. Additional
357 * state variables are defined in our ide_drive_t structure.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100359static struct ide_tape_obj *idetape_devs[MAX_HWIFS * MAX_DRIVES];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360
361#define ide_tape_f(file) ((file)->private_data)
362
363static struct ide_tape_obj *ide_tape_chrdev_get(unsigned int i)
364{
365 struct ide_tape_obj *tape = NULL;
366
Arjan van de Vencf8b8972006-03-23 03:00:45 -0800367 mutex_lock(&idetape_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 tape = idetape_devs[i];
369 if (tape)
370 kref_get(&tape->kref);
Arjan van de Vencf8b8972006-03-23 03:00:45 -0800371 mutex_unlock(&idetape_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 return tape;
373}
374
Borislav Petkovd236d742008-04-18 00:46:27 +0200375static void idetape_input_buffers(ide_drive_t *drive, struct ide_atapi_pc *pc,
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100376 unsigned int bcount)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377{
378 struct idetape_bh *bh = pc->bh;
379 int count;
380
381 while (bcount) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 if (bh == NULL) {
383 printk(KERN_ERR "ide-tape: bh == NULL in "
384 "idetape_input_buffers\n");
Bartlomiej Zolnierkiewicz9f87abe2008-04-28 23:44:41 +0200385 ide_pad_transfer(drive, 0, bcount);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 return;
387 }
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100388 count = min(
389 (unsigned int)(bh->b_size - atomic_read(&bh->b_count)),
390 bcount);
Bartlomiej Zolnierkiewicz374e0422008-07-23 19:55:56 +0200391 drive->hwif->tp_ops->input_data(drive, NULL, bh->b_data +
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100392 atomic_read(&bh->b_count), count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 bcount -= count;
394 atomic_add(count, &bh->b_count);
395 if (atomic_read(&bh->b_count) == bh->b_size) {
396 bh = bh->b_reqnext;
397 if (bh)
398 atomic_set(&bh->b_count, 0);
399 }
400 }
401 pc->bh = bh;
402}
403
Borislav Petkovd236d742008-04-18 00:46:27 +0200404static void idetape_output_buffers(ide_drive_t *drive, struct ide_atapi_pc *pc,
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100405 unsigned int bcount)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406{
407 struct idetape_bh *bh = pc->bh;
408 int count;
409
410 while (bcount) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 if (bh == NULL) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100412 printk(KERN_ERR "ide-tape: bh == NULL in %s\n",
413 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 return;
415 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 count = min((unsigned int)pc->b_count, (unsigned int)bcount);
Bartlomiej Zolnierkiewicz374e0422008-07-23 19:55:56 +0200417 drive->hwif->tp_ops->output_data(drive, NULL, pc->b_data, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 bcount -= count;
419 pc->b_data += count;
420 pc->b_count -= count;
421 if (!pc->b_count) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100422 bh = bh->b_reqnext;
423 pc->bh = bh;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 if (bh) {
425 pc->b_data = bh->b_data;
426 pc->b_count = atomic_read(&bh->b_count);
427 }
428 }
429 }
430}
431
Bartlomiej Zolnierkiewicz646c0cb2008-07-15 21:22:03 +0200432static void idetape_update_buffers(ide_drive_t *drive, struct ide_atapi_pc *pc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433{
434 struct idetape_bh *bh = pc->bh;
435 int count;
Borislav Petkovd236d742008-04-18 00:46:27 +0200436 unsigned int bcount = pc->xferred;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437
Borislav Petkov346331f2008-04-18 00:46:26 +0200438 if (pc->flags & PC_FLAG_WRITING)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 return;
440 while (bcount) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 if (bh == NULL) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100442 printk(KERN_ERR "ide-tape: bh == NULL in %s\n",
443 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 return;
445 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 count = min((unsigned int)bh->b_size, (unsigned int)bcount);
447 atomic_set(&bh->b_count, count);
448 if (atomic_read(&bh->b_count) == bh->b_size)
449 bh = bh->b_reqnext;
450 bcount -= count;
451 }
452 pc->bh = bh;
453}
454
455/*
456 * idetape_next_pc_storage returns a pointer to a place in which we can
457 * safely store a packet command, even though we intend to leave the
458 * driver. A storage space for a maximum of IDETAPE_PC_STACK packet
459 * commands is allocated at initialization time.
460 */
Borislav Petkovd236d742008-04-18 00:46:27 +0200461static struct ide_atapi_pc *idetape_next_pc_storage(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462{
463 idetape_tape_t *tape = drive->driver_data;
464
Borislav Petkov8004a8c2008-02-06 02:57:51 +0100465 debug_log(DBG_PCRQ_STACK, "pc_stack_index=%d\n", tape->pc_stack_index);
466
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 if (tape->pc_stack_index == IDETAPE_PC_STACK)
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100468 tape->pc_stack_index = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 return (&tape->pc_stack[tape->pc_stack_index++]);
470}
471
472/*
473 * idetape_next_rq_storage is used along with idetape_next_pc_storage.
474 * Since we queue packet commands in the request queue, we need to
475 * allocate a request, along with the allocation of a packet command.
476 */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100477
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478/**************************************************************
479 * *
480 * This should get fixed to use kmalloc(.., GFP_ATOMIC) *
481 * followed later on by kfree(). -ml *
482 * *
483 **************************************************************/
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100484
485static struct request *idetape_next_rq_storage(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486{
487 idetape_tape_t *tape = drive->driver_data;
488
Borislav Petkov8004a8c2008-02-06 02:57:51 +0100489 debug_log(DBG_PCRQ_STACK, "rq_stack_index=%d\n", tape->rq_stack_index);
490
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 if (tape->rq_stack_index == IDETAPE_PC_STACK)
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100492 tape->rq_stack_index = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 return (&tape->rq_stack[tape->rq_stack_index++]);
494}
495
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496/*
Borislav Petkov1b5db432008-02-02 19:56:48 +0100497 * called on each failed packet command retry to analyze the request sense. We
498 * currently do not utilize this information.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 */
Borislav Petkov1b5db432008-02-02 19:56:48 +0100500static void idetape_analyze_error(ide_drive_t *drive, u8 *sense)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501{
502 idetape_tape_t *tape = drive->driver_data;
Borislav Petkovd236d742008-04-18 00:46:27 +0200503 struct ide_atapi_pc *pc = tape->failed_pc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504
Borislav Petkov1b5db432008-02-02 19:56:48 +0100505 tape->sense_key = sense[2] & 0xF;
506 tape->asc = sense[12];
507 tape->ascq = sense[13];
Borislav Petkov8004a8c2008-02-06 02:57:51 +0100508
509 debug_log(DBG_ERR, "pc = %x, sense key = %x, asc = %x, ascq = %x\n",
510 pc->c[0], tape->sense_key, tape->asc, tape->ascq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511
Borislav Petkovd236d742008-04-18 00:46:27 +0200512 /* Correct pc->xferred by asking the tape. */
Borislav Petkov346331f2008-04-18 00:46:26 +0200513 if (pc->flags & PC_FLAG_DMA_ERROR) {
Borislav Petkovd236d742008-04-18 00:46:27 +0200514 pc->xferred = pc->req_xfer -
Borislav Petkov54bb2072008-02-06 02:57:52 +0100515 tape->blk_size *
Harvey Harrison5d0cc8a2008-07-15 21:21:41 +0200516 get_unaligned_be32(&sense[3]);
Bartlomiej Zolnierkiewicz646c0cb2008-07-15 21:22:03 +0200517 idetape_update_buffers(drive, pc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 }
519
520 /*
521 * If error was the result of a zero-length read or write command,
522 * with sense key=5, asc=0x22, ascq=0, let it slide. Some drives
523 * (i.e. Seagate STT3401A Travan) don't support 0-length read/writes.
524 */
Borislav Petkov90699ce2008-02-02 19:56:50 +0100525 if ((pc->c[0] == READ_6 || pc->c[0] == WRITE_6)
Borislav Petkov1b5db432008-02-02 19:56:48 +0100526 /* length == 0 */
527 && pc->c[4] == 0 && pc->c[3] == 0 && pc->c[2] == 0) {
528 if (tape->sense_key == 5) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 /* don't report an error, everything's ok */
530 pc->error = 0;
531 /* don't retry read/write */
Borislav Petkov346331f2008-04-18 00:46:26 +0200532 pc->flags |= PC_FLAG_ABORT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 }
534 }
Borislav Petkov90699ce2008-02-02 19:56:50 +0100535 if (pc->c[0] == READ_6 && (sense[2] & 0x80)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 pc->error = IDETAPE_ERROR_FILEMARK;
Borislav Petkov346331f2008-04-18 00:46:26 +0200537 pc->flags |= PC_FLAG_ABORT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 }
Borislav Petkov90699ce2008-02-02 19:56:50 +0100539 if (pc->c[0] == WRITE_6) {
Borislav Petkov1b5db432008-02-02 19:56:48 +0100540 if ((sense[2] & 0x40) || (tape->sense_key == 0xd
541 && tape->asc == 0x0 && tape->ascq == 0x2)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 pc->error = IDETAPE_ERROR_EOD;
Borislav Petkov346331f2008-04-18 00:46:26 +0200543 pc->flags |= PC_FLAG_ABORT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 }
545 }
Borislav Petkov90699ce2008-02-02 19:56:50 +0100546 if (pc->c[0] == READ_6 || pc->c[0] == WRITE_6) {
Borislav Petkov1b5db432008-02-02 19:56:48 +0100547 if (tape->sense_key == 8) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 pc->error = IDETAPE_ERROR_EOD;
Borislav Petkov346331f2008-04-18 00:46:26 +0200549 pc->flags |= PC_FLAG_ABORT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 }
Borislav Petkov346331f2008-04-18 00:46:26 +0200551 if (!(pc->flags & PC_FLAG_ABORT) &&
Borislav Petkovd236d742008-04-18 00:46:27 +0200552 pc->xferred)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 pc->retries = IDETAPE_MAX_PC_RETRIES + 1;
554 }
555}
556
Borislav Petkovd01dbc32008-04-27 15:38:33 +0200557/* Free data buffers completely. */
Borislav Petkov077e3bd2008-04-27 15:38:34 +0200558static void ide_tape_kfree_buffer(idetape_tape_t *tape)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559{
Borislav Petkov077e3bd2008-04-27 15:38:34 +0200560 struct idetape_bh *prev_bh, *bh = tape->merge_bh;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561
Borislav Petkovd01dbc32008-04-27 15:38:33 +0200562 while (bh) {
563 u32 size = bh->b_size;
564
565 while (size) {
566 unsigned int order = fls(size >> PAGE_SHIFT)-1;
567
568 if (bh->b_data)
569 free_pages((unsigned long)bh->b_data, order);
570
571 size &= (order-1);
572 bh->b_data += (1 << order) * PAGE_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 }
574 prev_bh = bh;
575 bh = bh->b_reqnext;
576 kfree(prev_bh);
577 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578}
579
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580static int idetape_end_request(ide_drive_t *drive, int uptodate, int nr_sects)
581{
582 struct request *rq = HWGROUP(drive)->rq;
583 idetape_tape_t *tape = drive->driver_data;
584 unsigned long flags;
585 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586
Borislav Petkov8004a8c2008-02-06 02:57:51 +0100587 debug_log(DBG_PROCS, "Enter %s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588
589 switch (uptodate) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100590 case 0: error = IDETAPE_ERROR_GENERAL; break;
591 case 1: error = 0; break;
592 default: error = uptodate;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 }
594 rq->errors = error;
595 if (error)
596 tape->failed_pc = NULL;
597
Bartlomiej Zolnierkiewicz36872212008-01-26 20:13:10 +0100598 if (!blk_special_request(rq)) {
599 ide_end_request(drive, uptodate, nr_sects);
600 return 0;
601 }
602
Borislav Petkov54bb2072008-02-06 02:57:52 +0100603 spin_lock_irqsave(&tape->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 ide_end_drive_cmd(drive, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606
Borislav Petkov54bb2072008-02-06 02:57:52 +0100607 spin_unlock_irqrestore(&tape->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608 return 0;
609}
610
Bartlomiej Zolnierkiewicz92f5daf2008-07-15 21:21:55 +0200611static void ide_tape_callback(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612{
613 idetape_tape_t *tape = drive->driver_data;
Bartlomiej Zolnierkiewicz5985e6a2008-07-15 21:21:55 +0200614 struct ide_atapi_pc *pc = tape->pc;
615 int uptodate = pc->error ? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616
Borislav Petkov8004a8c2008-02-06 02:57:51 +0100617 debug_log(DBG_PROCS, "Enter %s\n", __func__);
618
Bartlomiej Zolnierkiewiczdd2e9a02008-07-15 21:22:01 +0200619 if (tape->failed_pc == pc)
620 tape->failed_pc = NULL;
621
Bartlomiej Zolnierkiewicz5985e6a2008-07-15 21:21:55 +0200622 if (pc->c[0] == REQUEST_SENSE) {
623 if (uptodate)
624 idetape_analyze_error(drive, pc->buf);
625 else
626 printk(KERN_ERR "ide-tape: Error in REQUEST SENSE "
627 "itself - Aborting request!\n");
628 } else if (pc->c[0] == READ_6 || pc->c[0] == WRITE_6) {
629 struct request *rq = drive->hwif->hwgroup->rq;
630 int blocks = pc->xferred / tape->blk_size;
631
632 tape->avg_size += blocks * tape->blk_size;
633
634 if (time_after_eq(jiffies, tape->avg_time + HZ)) {
635 tape->avg_speed = tape->avg_size * HZ /
636 (jiffies - tape->avg_time) / 1024;
637 tape->avg_size = 0;
638 tape->avg_time = jiffies;
639 }
640
641 tape->first_frame += blocks;
642 rq->current_nr_sectors -= blocks;
643
644 if (pc->error)
645 uptodate = pc->error;
646 } else if (pc->c[0] == READ_POSITION && uptodate) {
647 u8 *readpos = tape->pc->buf;
648
649 debug_log(DBG_SENSE, "BOP - %s\n",
650 (readpos[0] & 0x80) ? "Yes" : "No");
651 debug_log(DBG_SENSE, "EOP - %s\n",
652 (readpos[0] & 0x40) ? "Yes" : "No");
653
654 if (readpos[0] & 0x4) {
655 printk(KERN_INFO "ide-tape: Block location is unknown"
656 "to the tape\n");
Borislav Petkovf2e3ab52008-07-23 19:56:01 +0200657 clear_bit(IDE_AFLAG_ADDRESS_VALID, &drive->atapi_flags);
Bartlomiej Zolnierkiewicz5985e6a2008-07-15 21:21:55 +0200658 uptodate = 0;
659 } else {
660 debug_log(DBG_SENSE, "Block Location - %u\n",
Harvey Harrisoncd740ab2008-07-24 22:53:33 +0200661 be32_to_cpup((__be32 *)&readpos[4]));
Bartlomiej Zolnierkiewicz5985e6a2008-07-15 21:21:55 +0200662
663 tape->partition = readpos[1];
Harvey Harrisoncd740ab2008-07-24 22:53:33 +0200664 tape->first_frame = be32_to_cpup((__be32 *)&readpos[4]);
Borislav Petkovf2e3ab52008-07-23 19:56:01 +0200665 set_bit(IDE_AFLAG_ADDRESS_VALID, &drive->atapi_flags);
Bartlomiej Zolnierkiewicz5985e6a2008-07-15 21:21:55 +0200666 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667 }
Bartlomiej Zolnierkiewicz5985e6a2008-07-15 21:21:55 +0200668
669 idetape_end_request(drive, uptodate, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670}
671
Bartlomiej Zolnierkiewicz5985e6a2008-07-15 21:21:55 +0200672static void idetape_init_pc(struct ide_atapi_pc *pc)
673{
674 memset(pc->c, 0, 12);
675 pc->retries = 0;
676 pc->flags = 0;
677 pc->req_xfer = 0;
678 pc->buf = pc->pc_buf;
679 pc->buf_size = IDETAPE_PC_BUFFER_SIZE;
680 pc->bh = NULL;
681 pc->b_data = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682}
683
Borislav Petkovd236d742008-04-18 00:46:27 +0200684static void idetape_create_request_sense_cmd(struct ide_atapi_pc *pc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685{
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100686 idetape_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +0100687 pc->c[0] = REQUEST_SENSE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 pc->c[4] = 20;
Borislav Petkovd236d742008-04-18 00:46:27 +0200689 pc->req_xfer = 20;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690}
691
692static void idetape_init_rq(struct request *rq, u8 cmd)
693{
FUJITA Tomonorie7b241a2008-04-29 09:54:38 +0200694 blk_rq_init(NULL, rq);
Jens Axboe4aff5e22006-08-10 08:44:47 +0200695 rq->cmd_type = REQ_TYPE_SPECIAL;
Borislav Petkov83dd5732008-07-23 19:56:00 +0200696 rq->cmd[13] = cmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697}
698
699/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100700 * Generate a new packet command request in front of the request queue, before
701 * the current request, so that it will be processed immediately, on the next
702 * pass through the driver. The function below is called from the request
703 * handling part of the driver (the "bottom" part). Safe storage for the request
704 * should be allocated with ide_tape_next_{pc,rq}_storage() prior to that.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100706 * Memory for those requests is pre-allocated at initialization time, and is
707 * limited to IDETAPE_PC_STACK requests. We assume that we have enough space for
708 * the maximum possible number of inter-dependent packet commands.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100710 * The higher level of the driver - The ioctl handler and the character device
711 * handling functions should queue request to the lower level part and wait for
712 * their completion using idetape_queue_pc_tail or idetape_queue_rw_tail.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713 */
Borislav Petkovd236d742008-04-18 00:46:27 +0200714static void idetape_queue_pc_head(ide_drive_t *drive, struct ide_atapi_pc *pc,
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100715 struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716{
717 struct ide_tape_obj *tape = drive->driver_data;
718
719 idetape_init_rq(rq, REQ_IDETAPE_PC1);
Bartlomiej Zolnierkiewicze8a96aa2008-07-15 21:21:41 +0200720 rq->cmd_flags |= REQ_PREEMPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721 rq->buffer = (char *) pc;
722 rq->rq_disk = tape->disk;
Borislav Petkov0014c752008-07-23 19:56:00 +0200723 memcpy(rq->cmd, pc->c, 12);
FUJITA Tomonori63f5abb2008-07-15 21:21:51 +0200724 ide_do_drive_cmd(drive, rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725}
726
727/*
728 * idetape_retry_pc is called when an error was detected during the
729 * last packet command. We queue a request sense packet command in
730 * the head of the request list.
731 */
Bartlomiej Zolnierkiewicz258ec412008-07-15 21:21:55 +0200732static void idetape_retry_pc(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733{
Borislav Petkovd236d742008-04-18 00:46:27 +0200734 struct ide_atapi_pc *pc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735 struct request *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736
Bartlomiej Zolnierkiewicz64a57fe2008-02-06 02:57:51 +0100737 (void)ide_read_error(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738 pc = idetape_next_pc_storage(drive);
739 rq = idetape_next_rq_storage(drive);
740 idetape_create_request_sense_cmd(pc);
Borislav Petkovf2e3ab52008-07-23 19:56:01 +0200741 set_bit(IDE_AFLAG_IGNORE_DSC, &drive->atapi_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742 idetape_queue_pc_head(drive, pc, rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743}
744
745/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100746 * Postpone the current request so that ide.c will be able to service requests
747 * from another device on the same hwgroup while we are polling for DSC.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748 */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100749static void idetape_postpone_request(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750{
751 idetape_tape_t *tape = drive->driver_data;
752
Borislav Petkov8004a8c2008-02-06 02:57:51 +0100753 debug_log(DBG_PROCS, "Enter %s\n", __func__);
754
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755 tape->postponed_rq = HWGROUP(drive)->rq;
Borislav Petkov54bb2072008-02-06 02:57:52 +0100756 ide_stall_queue(drive, tape->dsc_poll_freq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757}
758
Bartlomiej Zolnierkiewicz74e63e742008-07-15 21:22:01 +0200759static void ide_tape_handle_dsc(ide_drive_t *drive)
760{
761 idetape_tape_t *tape = drive->driver_data;
762
763 /* Media access command */
764 tape->dsc_polling_start = jiffies;
765 tape->dsc_poll_freq = IDETAPE_DSC_MA_FAST;
766 tape->dsc_timeout = jiffies + IDETAPE_DSC_MA_TIMEOUT;
767 /* Allow ide.c to handle other requests */
768 idetape_postpone_request(drive);
769}
770
Bartlomiej Zolnierkiewicz08424ac2008-07-15 21:22:01 +0200771static void ide_tape_io_buffers(ide_drive_t *drive, struct ide_atapi_pc *pc,
772 unsigned int bcount, int write)
773{
774 if (write)
775 idetape_output_buffers(drive, pc, bcount);
776 else
777 idetape_input_buffers(drive, pc, bcount);
778}
Borislav Petkova1efc852008-02-06 02:57:52 +0100779
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780/*
Borislav Petkova1efc852008-02-06 02:57:52 +0100781 * This is the usual interrupt handler which will be called during a packet
782 * command. We will transfer some of the data (as requested by the drive) and
783 * will re-point interrupt handler to us. When data transfer is finished, we
784 * will act according to the algorithm described before
Borislav Petkov8d06bfa2008-02-06 02:57:53 +0100785 * idetape_issue_pc.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786 */
Borislav Petkova1efc852008-02-06 02:57:52 +0100787static ide_startstop_t idetape_pc_intr(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789 idetape_tape_t *tape = drive->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790
Bartlomiej Zolnierkiewicz646c0cb2008-07-15 21:22:03 +0200791 return ide_pc_intr(drive, tape->pc, idetape_pc_intr, IDETAPE_WAIT_CMD,
792 NULL, idetape_update_buffers, idetape_retry_pc,
793 ide_tape_handle_dsc, ide_tape_io_buffers);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794}
795
796/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100797 * Packet Command Interface
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100799 * The current Packet Command is available in tape->pc, and will not change
800 * until we finish handling it. Each packet command is associated with a
801 * callback function that will be called when the command is finished.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100803 * The handling will be done in three stages:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100805 * 1. idetape_issue_pc will send the packet command to the drive, and will set
806 * the interrupt handler to idetape_pc_intr.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100808 * 2. On each interrupt, idetape_pc_intr will be called. This step will be
809 * repeated until the device signals us that no more interrupts will be issued.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100811 * 3. ATAPI Tape media access commands have immediate status with a delayed
812 * process. In case of a successful initiation of a media access packet command,
813 * the DSC bit will be set when the actual execution of the command is finished.
814 * Since the tape drive will not issue an interrupt, we have to poll for this
815 * event. In this case, we define the request as "low priority request" by
816 * setting rq_status to IDETAPE_RQ_POSTPONED, set a timer to poll for DSC and
817 * exit the driver.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100819 * ide.c will then give higher priority to requests which originate from the
820 * other device, until will change rq_status to RQ_ACTIVE.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100822 * 4. When the packet command is finished, it will be checked for errors.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100824 * 5. In case an error was found, we queue a request sense packet command in
825 * front of the request queue and retry the operation up to
826 * IDETAPE_MAX_PC_RETRIES times.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100828 * 6. In case no error was found, or we decided to give up and not to retry
829 * again, the callback function will be called and then we will handle the next
830 * request.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831 */
832static ide_startstop_t idetape_transfer_pc(ide_drive_t *drive)
833{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834 idetape_tape_t *tape = drive->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835
Bartlomiej Zolnierkiewicz594c16d2008-07-15 21:21:58 +0200836 return ide_transfer_pc(drive, tape->pc, idetape_pc_intr,
837 IDETAPE_WAIT_CMD, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838}
839
Borislav Petkovd236d742008-04-18 00:46:27 +0200840static ide_startstop_t idetape_issue_pc(ide_drive_t *drive,
841 struct ide_atapi_pc *pc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843 idetape_tape_t *tape = drive->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844
Borislav Petkov90699ce2008-02-02 19:56:50 +0100845 if (tape->pc->c[0] == REQUEST_SENSE &&
846 pc->c[0] == REQUEST_SENSE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847 printk(KERN_ERR "ide-tape: possible ide-tape.c bug - "
848 "Two request sense in serial were issued\n");
849 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850
Borislav Petkov90699ce2008-02-02 19:56:50 +0100851 if (tape->failed_pc == NULL && pc->c[0] != REQUEST_SENSE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852 tape->failed_pc = pc;
853 /* Set the current packet command */
854 tape->pc = pc;
855
856 if (pc->retries > IDETAPE_MAX_PC_RETRIES ||
Borislav Petkov346331f2008-04-18 00:46:26 +0200857 (pc->flags & PC_FLAG_ABORT)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858 /*
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100859 * We will "abort" retrying a packet command in case legitimate
860 * error code was received (crossing a filemark, or end of the
861 * media, for example).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862 */
Borislav Petkov346331f2008-04-18 00:46:26 +0200863 if (!(pc->flags & PC_FLAG_ABORT)) {
Borislav Petkov90699ce2008-02-02 19:56:50 +0100864 if (!(pc->c[0] == TEST_UNIT_READY &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865 tape->sense_key == 2 && tape->asc == 4 &&
866 (tape->ascq == 1 || tape->ascq == 8))) {
867 printk(KERN_ERR "ide-tape: %s: I/O error, "
868 "pc = %2x, key = %2x, "
869 "asc = %2x, ascq = %2x\n",
870 tape->name, pc->c[0],
871 tape->sense_key, tape->asc,
872 tape->ascq);
873 }
874 /* Giving up */
875 pc->error = IDETAPE_ERROR_GENERAL;
876 }
877 tape->failed_pc = NULL;
Borislav Petkov776bb022008-07-23 19:55:59 +0200878 drive->pc_callback(drive);
Bartlomiej Zolnierkiewicz92f5daf2008-07-15 21:21:55 +0200879 return ide_stopped;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880 }
Borislav Petkov8004a8c2008-02-06 02:57:51 +0100881 debug_log(DBG_SENSE, "Retry #%d, cmd = %02X\n", pc->retries, pc->c[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882
883 pc->retries++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884
Bartlomiej Zolnierkiewicz6bf16412008-07-15 21:22:00 +0200885 return ide_issue_pc(drive, pc, idetape_transfer_pc,
886 IDETAPE_WAIT_CMD, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887}
888
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100889/* A mode sense command is used to "sense" tape parameters. */
Borislav Petkovd236d742008-04-18 00:46:27 +0200890static void idetape_create_mode_sense_cmd(struct ide_atapi_pc *pc, u8 page_code)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891{
892 idetape_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +0100893 pc->c[0] = MODE_SENSE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894 if (page_code != IDETAPE_BLOCK_DESCRIPTOR)
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100895 /* DBD = 1 - Don't return block descriptors */
896 pc->c[1] = 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897 pc->c[2] = page_code;
898 /*
899 * Changed pc->c[3] to 0 (255 will at best return unused info).
900 *
901 * For SCSI this byte is defined as subpage instead of high byte
902 * of length and some IDE drives seem to interpret it this way
903 * and return an error when 255 is used.
904 */
905 pc->c[3] = 0;
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100906 /* We will just discard data in that case */
907 pc->c[4] = 255;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908 if (page_code == IDETAPE_BLOCK_DESCRIPTOR)
Borislav Petkovd236d742008-04-18 00:46:27 +0200909 pc->req_xfer = 12;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910 else if (page_code == IDETAPE_CAPABILITIES_PAGE)
Borislav Petkovd236d742008-04-18 00:46:27 +0200911 pc->req_xfer = 24;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912 else
Borislav Petkovd236d742008-04-18 00:46:27 +0200913 pc->req_xfer = 50;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914}
915
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100916static ide_startstop_t idetape_media_access_finished(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917{
Bartlomiej Zolnierkiewiczb73c7ee2008-07-23 19:55:52 +0200918 ide_hwif_t *hwif = drive->hwif;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919 idetape_tape_t *tape = drive->driver_data;
Borislav Petkovd236d742008-04-18 00:46:27 +0200920 struct ide_atapi_pc *pc = tape->pc;
Bartlomiej Zolnierkiewicz22c525b2008-01-25 22:17:11 +0100921 u8 stat;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922
Bartlomiej Zolnierkiewicz374e0422008-07-23 19:55:56 +0200923 stat = hwif->tp_ops->read_status(hwif);
Bartlomiej Zolnierkiewiczc47137a2008-02-06 02:57:51 +0100924
Bartlomiej Zolnierkiewicz3a7d2482008-10-10 22:39:21 +0200925 if (stat & ATA_DSC) {
926 if (stat & ATA_ERR) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927 /* Error detected */
Borislav Petkov90699ce2008-02-02 19:56:50 +0100928 if (pc->c[0] != TEST_UNIT_READY)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929 printk(KERN_ERR "ide-tape: %s: I/O error, ",
930 tape->name);
931 /* Retry operation */
Bartlomiej Zolnierkiewicz258ec412008-07-15 21:21:55 +0200932 idetape_retry_pc(drive);
933 return ide_stopped;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934 }
935 pc->error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936 } else {
937 pc->error = IDETAPE_ERROR_GENERAL;
938 tape->failed_pc = NULL;
939 }
Borislav Petkov776bb022008-07-23 19:55:59 +0200940 drive->pc_callback(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941 return ide_stopped;
942}
943
Borislav Petkovcd2abbf2008-07-15 21:22:03 +0200944static void ide_tape_create_rw_cmd(idetape_tape_t *tape,
Borislav Petkov0014c752008-07-23 19:56:00 +0200945 struct ide_atapi_pc *pc, struct request *rq,
946 u8 opcode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947{
Borislav Petkov0014c752008-07-23 19:56:00 +0200948 struct idetape_bh *bh = (struct idetape_bh *)rq->special;
949 unsigned int length = rq->current_nr_sectors;
950
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951 idetape_init_pc(pc);
Borislav Petkov860ff5e2008-02-02 19:56:50 +0100952 put_unaligned(cpu_to_be32(length), (unsigned int *) &pc->c[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953 pc->c[1] = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954 pc->bh = bh;
Borislav Petkovd236d742008-04-18 00:46:27 +0200955 pc->buf = NULL;
956 pc->buf_size = length * tape->blk_size;
957 pc->req_xfer = pc->buf_size;
Borislav Petkovf73850a2008-04-27 15:38:33 +0200958 if (pc->req_xfer == tape->buffer_size)
Bartlomiej Zolnierkiewicz5e331092008-07-15 21:21:56 +0200959 pc->flags |= PC_FLAG_DMA_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960
Borislav Petkovcd2abbf2008-07-15 21:22:03 +0200961 if (opcode == READ_6) {
962 pc->c[0] = READ_6;
963 atomic_set(&bh->b_count, 0);
964 } else if (opcode == WRITE_6) {
965 pc->c[0] = WRITE_6;
966 pc->flags |= PC_FLAG_WRITING;
967 pc->b_data = bh->b_data;
968 pc->b_count = atomic_read(&bh->b_count);
969 }
Borislav Petkov0014c752008-07-23 19:56:00 +0200970
971 memcpy(rq->cmd, pc->c, 12);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972}
973
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974static ide_startstop_t idetape_do_request(ide_drive_t *drive,
975 struct request *rq, sector_t block)
976{
Bartlomiej Zolnierkiewiczb73c7ee2008-07-23 19:55:52 +0200977 ide_hwif_t *hwif = drive->hwif;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978 idetape_tape_t *tape = drive->driver_data;
Borislav Petkovd236d742008-04-18 00:46:27 +0200979 struct ide_atapi_pc *pc = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980 struct request *postponed_rq = tape->postponed_rq;
Bartlomiej Zolnierkiewicz22c525b2008-01-25 22:17:11 +0100981 u8 stat;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982
Mark de Wever730616b2008-10-10 22:39:17 +0200983 debug_log(DBG_SENSE, "sector: %llu, nr_sectors: %lu,"
984 " current_nr_sectors: %u\n",
985 (unsigned long long)rq->sector, rq->nr_sectors,
986 rq->current_nr_sectors);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987
Jens Axboe4aff5e22006-08-10 08:44:47 +0200988 if (!blk_special_request(rq)) {
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100989 /* We do not support buffer cache originated requests. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990 printk(KERN_NOTICE "ide-tape: %s: Unsupported request in "
Jens Axboe4aff5e22006-08-10 08:44:47 +0200991 "request queue (%d)\n", drive->name, rq->cmd_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992 ide_end_request(drive, 0, 0);
993 return ide_stopped;
994 }
995
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100996 /* Retry a failed packet command */
Bartlomiej Zolnierkiewicz28c72142008-07-15 21:21:59 +0200997 if (tape->failed_pc && tape->pc->c[0] == REQUEST_SENSE) {
998 pc = tape->failed_pc;
999 goto out;
1000 }
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001001
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002 if (postponed_rq != NULL)
1003 if (rq != postponed_rq) {
1004 printk(KERN_ERR "ide-tape: ide-tape.c bug - "
1005 "Two DSC requests were queued\n");
1006 idetape_end_request(drive, 0, 0);
1007 return ide_stopped;
1008 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009
1010 tape->postponed_rq = NULL;
1011
1012 /*
1013 * If the tape is still busy, postpone our request and service
1014 * the other device meanwhile.
1015 */
Bartlomiej Zolnierkiewicz374e0422008-07-23 19:55:56 +02001016 stat = hwif->tp_ops->read_status(hwif);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017
Borislav Petkov83dd5732008-07-23 19:56:00 +02001018 if (!drive->dsc_overlap && !(rq->cmd[13] & REQ_IDETAPE_PC2))
Borislav Petkovf2e3ab52008-07-23 19:56:01 +02001019 set_bit(IDE_AFLAG_IGNORE_DSC, &drive->atapi_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020
1021 if (drive->post_reset == 1) {
Borislav Petkovf2e3ab52008-07-23 19:56:01 +02001022 set_bit(IDE_AFLAG_IGNORE_DSC, &drive->atapi_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023 drive->post_reset = 0;
1024 }
1025
Borislav Petkovf2e3ab52008-07-23 19:56:01 +02001026 if (!test_and_clear_bit(IDE_AFLAG_IGNORE_DSC, &drive->atapi_flags) &&
Bartlomiej Zolnierkiewicz3a7d2482008-10-10 22:39:21 +02001027 (stat & ATA_DSC) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028 if (postponed_rq == NULL) {
1029 tape->dsc_polling_start = jiffies;
Borislav Petkov54bb2072008-02-06 02:57:52 +01001030 tape->dsc_poll_freq = tape->best_dsc_rw_freq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031 tape->dsc_timeout = jiffies + IDETAPE_DSC_RW_TIMEOUT;
1032 } else if (time_after(jiffies, tape->dsc_timeout)) {
1033 printk(KERN_ERR "ide-tape: %s: DSC timeout\n",
1034 tape->name);
Borislav Petkov83dd5732008-07-23 19:56:00 +02001035 if (rq->cmd[13] & REQ_IDETAPE_PC2) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036 idetape_media_access_finished(drive);
1037 return ide_stopped;
1038 } else {
1039 return ide_do_reset(drive);
1040 }
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001041 } else if (time_after(jiffies,
1042 tape->dsc_polling_start +
1043 IDETAPE_DSC_MA_THRESHOLD))
Borislav Petkov54bb2072008-02-06 02:57:52 +01001044 tape->dsc_poll_freq = IDETAPE_DSC_MA_SLOW;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045 idetape_postpone_request(drive);
1046 return ide_stopped;
1047 }
Borislav Petkov83dd5732008-07-23 19:56:00 +02001048 if (rq->cmd[13] & REQ_IDETAPE_READ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049 pc = idetape_next_pc_storage(drive);
Borislav Petkov0014c752008-07-23 19:56:00 +02001050 ide_tape_create_rw_cmd(tape, pc, rq, READ_6);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051 goto out;
1052 }
Borislav Petkov83dd5732008-07-23 19:56:00 +02001053 if (rq->cmd[13] & REQ_IDETAPE_WRITE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054 pc = idetape_next_pc_storage(drive);
Borislav Petkov0014c752008-07-23 19:56:00 +02001055 ide_tape_create_rw_cmd(tape, pc, rq, WRITE_6);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056 goto out;
1057 }
Borislav Petkov83dd5732008-07-23 19:56:00 +02001058 if (rq->cmd[13] & REQ_IDETAPE_PC1) {
Borislav Petkovd236d742008-04-18 00:46:27 +02001059 pc = (struct ide_atapi_pc *) rq->buffer;
Borislav Petkov83dd5732008-07-23 19:56:00 +02001060 rq->cmd[13] &= ~(REQ_IDETAPE_PC1);
1061 rq->cmd[13] |= REQ_IDETAPE_PC2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062 goto out;
1063 }
Borislav Petkov83dd5732008-07-23 19:56:00 +02001064 if (rq->cmd[13] & REQ_IDETAPE_PC2) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065 idetape_media_access_finished(drive);
1066 return ide_stopped;
1067 }
1068 BUG();
Bartlomiej Zolnierkiewicz28c72142008-07-15 21:21:59 +02001069
Borislav Petkovf2e3ab52008-07-23 19:56:01 +02001070out:
Borislav Petkov8d06bfa2008-02-06 02:57:53 +01001071 return idetape_issue_pc(drive, pc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072}
1073
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074/*
Borislav Petkov41aa1702008-04-27 15:38:32 +02001075 * The function below uses __get_free_pages to allocate a data buffer of size
Borislav Petkovf73850a2008-04-27 15:38:33 +02001076 * tape->buffer_size (or a bit more). We attempt to combine sequential pages as
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001077 * much as possible.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078 *
Borislav Petkov41aa1702008-04-27 15:38:32 +02001079 * It returns a pointer to the newly allocated buffer, or NULL in case of
1080 * failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081 */
Borislav Petkov077e3bd2008-04-27 15:38:34 +02001082static struct idetape_bh *ide_tape_kmalloc_buffer(idetape_tape_t *tape,
1083 int full, int clear)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084{
Borislav Petkov077e3bd2008-04-27 15:38:34 +02001085 struct idetape_bh *prev_bh, *bh, *merge_bh;
Borislav Petkova997a432008-04-27 15:38:33 +02001086 int pages = tape->pages_per_buffer;
Borislav Petkov41aa1702008-04-27 15:38:32 +02001087 unsigned int order, b_allocd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088 char *b_data = NULL;
1089
Borislav Petkov077e3bd2008-04-27 15:38:34 +02001090 merge_bh = kmalloc(sizeof(struct idetape_bh), GFP_KERNEL);
1091 bh = merge_bh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092 if (bh == NULL)
1093 goto abort;
Borislav Petkov41aa1702008-04-27 15:38:32 +02001094
1095 order = fls(pages) - 1;
1096 bh->b_data = (char *) __get_free_pages(GFP_KERNEL, order);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001097 if (!bh->b_data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098 goto abort;
Borislav Petkov41aa1702008-04-27 15:38:32 +02001099 b_allocd = (1 << order) * PAGE_SIZE;
1100 pages &= (order-1);
1101
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102 if (clear)
Borislav Petkov41aa1702008-04-27 15:38:32 +02001103 memset(bh->b_data, 0, b_allocd);
1104 bh->b_reqnext = NULL;
1105 bh->b_size = b_allocd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106 atomic_set(&bh->b_count, full ? bh->b_size : 0);
1107
Borislav Petkov41aa1702008-04-27 15:38:32 +02001108 while (pages) {
1109 order = fls(pages) - 1;
1110 b_data = (char *) __get_free_pages(GFP_KERNEL, order);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001111 if (!b_data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112 goto abort;
Borislav Petkov41aa1702008-04-27 15:38:32 +02001113 b_allocd = (1 << order) * PAGE_SIZE;
1114
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115 if (clear)
Borislav Petkov41aa1702008-04-27 15:38:32 +02001116 memset(b_data, 0, b_allocd);
1117
1118 /* newly allocated page frames below buffer header or ...*/
1119 if (bh->b_data == b_data + b_allocd) {
1120 bh->b_size += b_allocd;
1121 bh->b_data -= b_allocd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122 if (full)
Borislav Petkov41aa1702008-04-27 15:38:32 +02001123 atomic_add(b_allocd, &bh->b_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124 continue;
1125 }
Borislav Petkov41aa1702008-04-27 15:38:32 +02001126 /* they are above the header */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127 if (b_data == bh->b_data + bh->b_size) {
Borislav Petkov41aa1702008-04-27 15:38:32 +02001128 bh->b_size += b_allocd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129 if (full)
Borislav Petkov41aa1702008-04-27 15:38:32 +02001130 atomic_add(b_allocd, &bh->b_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131 continue;
1132 }
1133 prev_bh = bh;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001134 bh = kmalloc(sizeof(struct idetape_bh), GFP_KERNEL);
1135 if (!bh) {
Borislav Petkov41aa1702008-04-27 15:38:32 +02001136 free_pages((unsigned long) b_data, order);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137 goto abort;
1138 }
1139 bh->b_reqnext = NULL;
1140 bh->b_data = b_data;
Borislav Petkov41aa1702008-04-27 15:38:32 +02001141 bh->b_size = b_allocd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142 atomic_set(&bh->b_count, full ? bh->b_size : 0);
1143 prev_bh->b_reqnext = bh;
Borislav Petkov41aa1702008-04-27 15:38:32 +02001144
1145 pages &= (order-1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146 }
Borislav Petkov41aa1702008-04-27 15:38:32 +02001147
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148 bh->b_size -= tape->excess_bh_size;
1149 if (full)
1150 atomic_sub(tape->excess_bh_size, &bh->b_count);
Borislav Petkov077e3bd2008-04-27 15:38:34 +02001151 return merge_bh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152abort:
Borislav Petkov077e3bd2008-04-27 15:38:34 +02001153 ide_tape_kfree_buffer(tape);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154 return NULL;
1155}
1156
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001157static int idetape_copy_stage_from_user(idetape_tape_t *tape,
Borislav Petkov8646c882008-04-27 15:38:26 +02001158 const char __user *buf, int n)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159{
1160 struct idetape_bh *bh = tape->bh;
1161 int count;
Daniel Walkerdcd96372006-06-25 05:47:37 -07001162 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163
1164 while (n) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165 if (bh == NULL) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001166 printk(KERN_ERR "ide-tape: bh == NULL in %s\n",
1167 __func__);
Daniel Walkerdcd96372006-06-25 05:47:37 -07001168 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169 }
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001170 count = min((unsigned int)
1171 (bh->b_size - atomic_read(&bh->b_count)),
1172 (unsigned int)n);
1173 if (copy_from_user(bh->b_data + atomic_read(&bh->b_count), buf,
1174 count))
Daniel Walkerdcd96372006-06-25 05:47:37 -07001175 ret = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176 n -= count;
1177 atomic_add(count, &bh->b_count);
1178 buf += count;
1179 if (atomic_read(&bh->b_count) == bh->b_size) {
1180 bh = bh->b_reqnext;
1181 if (bh)
1182 atomic_set(&bh->b_count, 0);
1183 }
1184 }
1185 tape->bh = bh;
Daniel Walkerdcd96372006-06-25 05:47:37 -07001186 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187}
1188
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001189static int idetape_copy_stage_to_user(idetape_tape_t *tape, char __user *buf,
Borislav Petkov99d74e62008-04-27 15:38:25 +02001190 int n)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191{
1192 struct idetape_bh *bh = tape->bh;
1193 int count;
Daniel Walkerdcd96372006-06-25 05:47:37 -07001194 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195
1196 while (n) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197 if (bh == NULL) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001198 printk(KERN_ERR "ide-tape: bh == NULL in %s\n",
1199 __func__);
Daniel Walkerdcd96372006-06-25 05:47:37 -07001200 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202 count = min(tape->b_count, n);
Daniel Walkerdcd96372006-06-25 05:47:37 -07001203 if (copy_to_user(buf, tape->b_data, count))
1204 ret = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205 n -= count;
1206 tape->b_data += count;
1207 tape->b_count -= count;
1208 buf += count;
1209 if (!tape->b_count) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001210 bh = bh->b_reqnext;
1211 tape->bh = bh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212 if (bh) {
1213 tape->b_data = bh->b_data;
1214 tape->b_count = atomic_read(&bh->b_count);
1215 }
1216 }
1217 }
Daniel Walkerdcd96372006-06-25 05:47:37 -07001218 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219}
1220
Borislav Petkov077e3bd2008-04-27 15:38:34 +02001221static void idetape_init_merge_buffer(idetape_tape_t *tape)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222{
Borislav Petkov077e3bd2008-04-27 15:38:34 +02001223 struct idetape_bh *bh = tape->merge_bh;
1224 tape->bh = tape->merge_bh;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001225
Borislav Petkov54abf372008-02-06 02:57:52 +01001226 if (tape->chrdev_dir == IDETAPE_DIR_WRITE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227 atomic_set(&bh->b_count, 0);
1228 else {
1229 tape->b_data = bh->b_data;
1230 tape->b_count = atomic_read(&bh->b_count);
1231 }
1232}
1233
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001235 * Write a filemark if write_filemark=1. Flush the device buffers without
1236 * writing a filemark otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237 */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001238static void idetape_create_write_filemark_cmd(ide_drive_t *drive,
Borislav Petkovd236d742008-04-18 00:46:27 +02001239 struct ide_atapi_pc *pc, int write_filemark)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240{
1241 idetape_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +01001242 pc->c[0] = WRITE_FILEMARKS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243 pc->c[4] = write_filemark;
Borislav Petkov346331f2008-04-18 00:46:26 +02001244 pc->flags |= PC_FLAG_WAIT_FOR_DSC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245}
1246
Borislav Petkovd236d742008-04-18 00:46:27 +02001247static void idetape_create_test_unit_ready_cmd(struct ide_atapi_pc *pc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248{
1249 idetape_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +01001250 pc->c[0] = TEST_UNIT_READY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251}
1252
1253/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001254 * We add a special packet command request to the tail of the request queue, and
1255 * wait for it to be serviced. This is not to be called from within the request
1256 * handling part of the driver! We allocate here data on the stack and it is
1257 * valid until the request is finished. This is not the case for the bottom part
1258 * of the driver, where we are always leaving the functions to wait for an
1259 * interrupt or a timer event.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001261 * From the bottom part of the driver, we should allocate safe memory using
1262 * idetape_next_pc_storage() and ide_tape_next_rq_storage(), and add the request
1263 * to the request list without waiting for it to be serviced! In that case, we
1264 * usually use idetape_queue_pc_head().
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265 */
Borislav Petkovea1ab3d2008-04-27 15:38:27 +02001266static int idetape_queue_pc_tail(ide_drive_t *drive, struct ide_atapi_pc *pc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267{
1268 struct ide_tape_obj *tape = drive->driver_data;
FUJITA Tomonori64ea1b42008-07-15 21:21:43 +02001269 struct request *rq;
1270 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271
FUJITA Tomonori64ea1b42008-07-15 21:21:43 +02001272 rq = blk_get_request(drive->queue, READ, __GFP_WAIT);
1273 rq->cmd_type = REQ_TYPE_SPECIAL;
Borislav Petkov83dd5732008-07-23 19:56:00 +02001274 rq->cmd[13] = REQ_IDETAPE_PC1;
FUJITA Tomonori64ea1b42008-07-15 21:21:43 +02001275 rq->buffer = (char *)pc;
Borislav Petkov0014c752008-07-23 19:56:00 +02001276 memcpy(rq->cmd, pc->c, 12);
FUJITA Tomonori64ea1b42008-07-15 21:21:43 +02001277 error = blk_execute_rq(drive->queue, tape->disk, rq, 0);
1278 blk_put_request(rq);
1279 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280}
1281
Borislav Petkovd236d742008-04-18 00:46:27 +02001282static void idetape_create_load_unload_cmd(ide_drive_t *drive,
1283 struct ide_atapi_pc *pc, int cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284{
1285 idetape_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +01001286 pc->c[0] = START_STOP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287 pc->c[4] = cmd;
Borislav Petkov346331f2008-04-18 00:46:26 +02001288 pc->flags |= PC_FLAG_WAIT_FOR_DSC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289}
1290
1291static int idetape_wait_ready(ide_drive_t *drive, unsigned long timeout)
1292{
1293 idetape_tape_t *tape = drive->driver_data;
Borislav Petkovd236d742008-04-18 00:46:27 +02001294 struct ide_atapi_pc pc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295 int load_attempted = 0;
1296
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001297 /* Wait for the tape to become ready */
Borislav Petkovf2e3ab52008-07-23 19:56:01 +02001298 set_bit(IDE_AFLAG_MEDIUM_PRESENT, &drive->atapi_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299 timeout += jiffies;
1300 while (time_before(jiffies, timeout)) {
1301 idetape_create_test_unit_ready_cmd(&pc);
Borislav Petkovea1ab3d2008-04-27 15:38:27 +02001302 if (!idetape_queue_pc_tail(drive, &pc))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303 return 0;
1304 if ((tape->sense_key == 2 && tape->asc == 4 && tape->ascq == 2)
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001305 || (tape->asc == 0x3A)) {
1306 /* no media */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307 if (load_attempted)
1308 return -ENOMEDIUM;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001309 idetape_create_load_unload_cmd(drive, &pc,
1310 IDETAPE_LU_LOAD_MASK);
Borislav Petkovea1ab3d2008-04-27 15:38:27 +02001311 idetape_queue_pc_tail(drive, &pc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312 load_attempted = 1;
1313 /* not about to be ready */
1314 } else if (!(tape->sense_key == 2 && tape->asc == 4 &&
1315 (tape->ascq == 1 || tape->ascq == 8)))
1316 return -EIO;
Nishanth Aravamudan80ce45f2005-09-10 00:27:08 -07001317 msleep(100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318 }
1319 return -EIO;
1320}
1321
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001322static int idetape_flush_tape_buffers(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323{
Borislav Petkovd236d742008-04-18 00:46:27 +02001324 struct ide_atapi_pc pc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325 int rc;
1326
1327 idetape_create_write_filemark_cmd(drive, &pc, 0);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001328 rc = idetape_queue_pc_tail(drive, &pc);
1329 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330 return rc;
1331 idetape_wait_ready(drive, 60 * 5 * HZ);
1332 return 0;
1333}
1334
Borislav Petkovd236d742008-04-18 00:46:27 +02001335static void idetape_create_read_position_cmd(struct ide_atapi_pc *pc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336{
1337 idetape_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +01001338 pc->c[0] = READ_POSITION;
Borislav Petkovd236d742008-04-18 00:46:27 +02001339 pc->req_xfer = 20;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340}
1341
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001342static int idetape_read_position(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343{
1344 idetape_tape_t *tape = drive->driver_data;
Borislav Petkovd236d742008-04-18 00:46:27 +02001345 struct ide_atapi_pc pc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346 int position;
1347
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001348 debug_log(DBG_PROCS, "Enter %s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349
1350 idetape_create_read_position_cmd(&pc);
1351 if (idetape_queue_pc_tail(drive, &pc))
1352 return -1;
Borislav Petkov54bb2072008-02-06 02:57:52 +01001353 position = tape->first_frame;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354 return position;
1355}
1356
Borislav Petkovd236d742008-04-18 00:46:27 +02001357static void idetape_create_locate_cmd(ide_drive_t *drive,
1358 struct ide_atapi_pc *pc,
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001359 unsigned int block, u8 partition, int skip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360{
1361 idetape_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +01001362 pc->c[0] = POSITION_TO_ELEMENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363 pc->c[1] = 2;
Borislav Petkov860ff5e2008-02-02 19:56:50 +01001364 put_unaligned(cpu_to_be32(block), (unsigned int *) &pc->c[3]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365 pc->c[8] = partition;
Borislav Petkov346331f2008-04-18 00:46:26 +02001366 pc->flags |= PC_FLAG_WAIT_FOR_DSC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367}
1368
Borislav Petkovd236d742008-04-18 00:46:27 +02001369static int idetape_create_prevent_cmd(ide_drive_t *drive,
1370 struct ide_atapi_pc *pc, int prevent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371{
1372 idetape_tape_t *tape = drive->driver_data;
1373
Borislav Petkovb6422012008-02-02 19:56:49 +01001374 /* device supports locking according to capabilities page */
1375 if (!(tape->caps[6] & 0x01))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376 return 0;
1377
1378 idetape_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +01001379 pc->c[0] = ALLOW_MEDIUM_REMOVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380 pc->c[4] = prevent;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381 return 1;
1382}
1383
Borislav Petkovec0fdb02008-04-27 15:38:34 +02001384static void __ide_tape_discard_merge_buffer(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385{
1386 idetape_tape_t *tape = drive->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387
Borislav Petkov54abf372008-02-06 02:57:52 +01001388 if (tape->chrdev_dir != IDETAPE_DIR_READ)
Borislav Petkov97986302008-04-27 15:38:34 +02001389 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390
Borislav Petkovf2e3ab52008-07-23 19:56:01 +02001391 clear_bit(IDE_AFLAG_FILEMARK, &drive->atapi_flags);
Borislav Petkov01a63aeb2008-04-27 15:38:34 +02001392 tape->merge_bh_size = 0;
Borislav Petkov077e3bd2008-04-27 15:38:34 +02001393 if (tape->merge_bh != NULL) {
1394 ide_tape_kfree_buffer(tape);
1395 tape->merge_bh = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396 }
1397
Borislav Petkov54abf372008-02-06 02:57:52 +01001398 tape->chrdev_dir = IDETAPE_DIR_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399}
1400
1401/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001402 * Position the tape to the requested block using the LOCATE packet command.
1403 * A READ POSITION command is then issued to check where we are positioned. Like
1404 * all higher level operations, we queue the commands at the tail of the request
1405 * queue and wait for their completion.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406 */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001407static int idetape_position_tape(ide_drive_t *drive, unsigned int block,
1408 u8 partition, int skip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409{
1410 idetape_tape_t *tape = drive->driver_data;
1411 int retval;
Borislav Petkovd236d742008-04-18 00:46:27 +02001412 struct ide_atapi_pc pc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413
Borislav Petkov54abf372008-02-06 02:57:52 +01001414 if (tape->chrdev_dir == IDETAPE_DIR_READ)
Borislav Petkovec0fdb02008-04-27 15:38:34 +02001415 __ide_tape_discard_merge_buffer(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416 idetape_wait_ready(drive, 60 * 5 * HZ);
1417 idetape_create_locate_cmd(drive, &pc, block, partition, skip);
1418 retval = idetape_queue_pc_tail(drive, &pc);
1419 if (retval)
1420 return (retval);
1421
1422 idetape_create_read_position_cmd(&pc);
1423 return (idetape_queue_pc_tail(drive, &pc));
1424}
1425
Borislav Petkovec0fdb02008-04-27 15:38:34 +02001426static void ide_tape_discard_merge_buffer(ide_drive_t *drive,
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001427 int restore_position)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428{
1429 idetape_tape_t *tape = drive->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430 int seek, position;
1431
Borislav Petkovec0fdb02008-04-27 15:38:34 +02001432 __ide_tape_discard_merge_buffer(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433 if (restore_position) {
1434 position = idetape_read_position(drive);
Borislav Petkov97986302008-04-27 15:38:34 +02001435 seek = position > 0 ? position : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436 if (idetape_position_tape(drive, seek, 0, 0)) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001437 printk(KERN_INFO "ide-tape: %s: position_tape failed in"
Borislav Petkovec0fdb02008-04-27 15:38:34 +02001438 " %s\n", tape->name, __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439 return;
1440 }
1441 }
1442}
1443
1444/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001445 * Generate a read/write request for the block device interface and wait for it
1446 * to be serviced.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447 */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001448static int idetape_queue_rw_tail(ide_drive_t *drive, int cmd, int blocks,
1449 struct idetape_bh *bh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450{
1451 idetape_tape_t *tape = drive->driver_data;
FUJITA Tomonori64ea1b42008-07-15 21:21:43 +02001452 struct request *rq;
1453 int ret, errors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001455 debug_log(DBG_SENSE, "%s: cmd=%d\n", __func__, cmd);
1456
FUJITA Tomonori64ea1b42008-07-15 21:21:43 +02001457 rq = blk_get_request(drive->queue, READ, __GFP_WAIT);
1458 rq->cmd_type = REQ_TYPE_SPECIAL;
Borislav Petkov83dd5732008-07-23 19:56:00 +02001459 rq->cmd[13] = cmd;
FUJITA Tomonori64ea1b42008-07-15 21:21:43 +02001460 rq->rq_disk = tape->disk;
1461 rq->special = (void *)bh;
1462 rq->sector = tape->first_frame;
1463 rq->nr_sectors = blocks;
1464 rq->current_nr_sectors = blocks;
1465 blk_execute_rq(drive->queue, tape->disk, rq, 0);
1466
1467 errors = rq->errors;
1468 ret = tape->blk_size * (blocks - rq->current_nr_sectors);
1469 blk_put_request(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470
1471 if ((cmd & (REQ_IDETAPE_READ | REQ_IDETAPE_WRITE)) == 0)
1472 return 0;
1473
Borislav Petkov077e3bd2008-04-27 15:38:34 +02001474 if (tape->merge_bh)
1475 idetape_init_merge_buffer(tape);
FUJITA Tomonori64ea1b42008-07-15 21:21:43 +02001476 if (errors == IDETAPE_ERROR_GENERAL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477 return -EIO;
FUJITA Tomonori64ea1b42008-07-15 21:21:43 +02001478 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479}
1480
Borislav Petkovd236d742008-04-18 00:46:27 +02001481static void idetape_create_inquiry_cmd(struct ide_atapi_pc *pc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482{
1483 idetape_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +01001484 pc->c[0] = INQUIRY;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001485 pc->c[4] = 254;
Borislav Petkovd236d742008-04-18 00:46:27 +02001486 pc->req_xfer = 254;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487}
1488
Borislav Petkovd236d742008-04-18 00:46:27 +02001489static void idetape_create_rewind_cmd(ide_drive_t *drive,
1490 struct ide_atapi_pc *pc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491{
1492 idetape_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +01001493 pc->c[0] = REZERO_UNIT;
Borislav Petkov346331f2008-04-18 00:46:26 +02001494 pc->flags |= PC_FLAG_WAIT_FOR_DSC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495}
1496
Borislav Petkovd236d742008-04-18 00:46:27 +02001497static void idetape_create_erase_cmd(struct ide_atapi_pc *pc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498{
1499 idetape_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +01001500 pc->c[0] = ERASE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501 pc->c[1] = 1;
Borislav Petkov346331f2008-04-18 00:46:26 +02001502 pc->flags |= PC_FLAG_WAIT_FOR_DSC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503}
1504
Borislav Petkovd236d742008-04-18 00:46:27 +02001505static void idetape_create_space_cmd(struct ide_atapi_pc *pc, int count, u8 cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506{
1507 idetape_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +01001508 pc->c[0] = SPACE;
Borislav Petkov860ff5e2008-02-02 19:56:50 +01001509 put_unaligned(cpu_to_be32(count), (unsigned int *) &pc->c[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510 pc->c[1] = cmd;
Borislav Petkov346331f2008-04-18 00:46:26 +02001511 pc->flags |= PC_FLAG_WAIT_FOR_DSC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512}
1513
Borislav Petkov97c566c2008-04-27 15:38:25 +02001514/* Queue up a character device originated write request. */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001515static int idetape_add_chrdev_write_request(ide_drive_t *drive, int blocks)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001516{
1517 idetape_tape_t *tape = drive->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001519 debug_log(DBG_CHRDEV, "Enter %s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520
Borislav Petkov0aa4b012008-04-27 15:38:27 +02001521 return idetape_queue_rw_tail(drive, REQ_IDETAPE_WRITE,
Borislav Petkov077e3bd2008-04-27 15:38:34 +02001522 blocks, tape->merge_bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523}
1524
Borislav Petkovd9df9372008-04-27 15:38:34 +02001525static void ide_tape_flush_merge_buffer(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526{
1527 idetape_tape_t *tape = drive->driver_data;
1528 int blocks, min;
1529 struct idetape_bh *bh;
Borislav Petkov55a5d292008-02-02 19:56:49 +01001530
Borislav Petkov54abf372008-02-06 02:57:52 +01001531 if (tape->chrdev_dir != IDETAPE_DIR_WRITE) {
Borislav Petkov077e3bd2008-04-27 15:38:34 +02001532 printk(KERN_ERR "ide-tape: bug: Trying to empty merge buffer"
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001533 " but we are not writing.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534 return;
1535 }
Borislav Petkov01a63aeb2008-04-27 15:38:34 +02001536 if (tape->merge_bh_size > tape->buffer_size) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537 printk(KERN_ERR "ide-tape: bug: merge_buffer too big\n");
Borislav Petkov01a63aeb2008-04-27 15:38:34 +02001538 tape->merge_bh_size = tape->buffer_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539 }
Borislav Petkov01a63aeb2008-04-27 15:38:34 +02001540 if (tape->merge_bh_size) {
1541 blocks = tape->merge_bh_size / tape->blk_size;
1542 if (tape->merge_bh_size % tape->blk_size) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001543 unsigned int i;
1544
1545 blocks++;
Borislav Petkov01a63aeb2008-04-27 15:38:34 +02001546 i = tape->blk_size - tape->merge_bh_size %
Borislav Petkov54bb2072008-02-06 02:57:52 +01001547 tape->blk_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548 bh = tape->bh->b_reqnext;
1549 while (bh) {
1550 atomic_set(&bh->b_count, 0);
1551 bh = bh->b_reqnext;
1552 }
1553 bh = tape->bh;
1554 while (i) {
1555 if (bh == NULL) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001556 printk(KERN_INFO "ide-tape: bug,"
1557 " bh NULL\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558 break;
1559 }
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001560 min = min(i, (unsigned int)(bh->b_size -
1561 atomic_read(&bh->b_count)));
1562 memset(bh->b_data + atomic_read(&bh->b_count),
1563 0, min);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001564 atomic_add(min, &bh->b_count);
1565 i -= min;
1566 bh = bh->b_reqnext;
1567 }
1568 }
1569 (void) idetape_add_chrdev_write_request(drive, blocks);
Borislav Petkov01a63aeb2008-04-27 15:38:34 +02001570 tape->merge_bh_size = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571 }
Borislav Petkov077e3bd2008-04-27 15:38:34 +02001572 if (tape->merge_bh != NULL) {
1573 ide_tape_kfree_buffer(tape);
1574 tape->merge_bh = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001575 }
Borislav Petkov54abf372008-02-06 02:57:52 +01001576 tape->chrdev_dir = IDETAPE_DIR_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001577}
1578
Borislav Petkov83042b22008-04-27 15:38:27 +02001579static int idetape_init_read(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001580{
1581 idetape_tape_t *tape = drive->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582 int bytes_read;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001583
1584 /* Initialize read operation */
Borislav Petkov54abf372008-02-06 02:57:52 +01001585 if (tape->chrdev_dir != IDETAPE_DIR_READ) {
1586 if (tape->chrdev_dir == IDETAPE_DIR_WRITE) {
Borislav Petkovd9df9372008-04-27 15:38:34 +02001587 ide_tape_flush_merge_buffer(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001588 idetape_flush_tape_buffers(drive);
1589 }
Borislav Petkov077e3bd2008-04-27 15:38:34 +02001590 if (tape->merge_bh || tape->merge_bh_size) {
Borislav Petkov01a63aeb2008-04-27 15:38:34 +02001591 printk(KERN_ERR "ide-tape: merge_bh_size should be"
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001592 " 0 now\n");
Borislav Petkov01a63aeb2008-04-27 15:38:34 +02001593 tape->merge_bh_size = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594 }
Borislav Petkov077e3bd2008-04-27 15:38:34 +02001595 tape->merge_bh = ide_tape_kmalloc_buffer(tape, 0, 0);
1596 if (!tape->merge_bh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001597 return -ENOMEM;
Borislav Petkov54abf372008-02-06 02:57:52 +01001598 tape->chrdev_dir = IDETAPE_DIR_READ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599
1600 /*
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001601 * Issue a read 0 command to ensure that DSC handshake is
1602 * switched from completion mode to buffer available mode.
1603 * No point in issuing this if DSC overlap isn't supported, some
1604 * drives (Seagate STT3401A) will return an error.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001605 */
1606 if (drive->dsc_overlap) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001607 bytes_read = idetape_queue_rw_tail(drive,
1608 REQ_IDETAPE_READ, 0,
Borislav Petkov077e3bd2008-04-27 15:38:34 +02001609 tape->merge_bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610 if (bytes_read < 0) {
Borislav Petkov077e3bd2008-04-27 15:38:34 +02001611 ide_tape_kfree_buffer(tape);
1612 tape->merge_bh = NULL;
Borislav Petkov54abf372008-02-06 02:57:52 +01001613 tape->chrdev_dir = IDETAPE_DIR_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001614 return bytes_read;
1615 }
1616 }
1617 }
Borislav Petkov5e69bd92008-04-27 15:38:25 +02001618
Linus Torvalds1da177e2005-04-16 15:20:36 -07001619 return 0;
1620}
1621
Borislav Petkov5bd50dc2008-04-27 15:38:28 +02001622/* called from idetape_chrdev_read() to service a chrdev read request. */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001623static int idetape_add_chrdev_read_request(ide_drive_t *drive, int blocks)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001624{
1625 idetape_tape_t *tape = drive->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001626
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001627 debug_log(DBG_PROCS, "Enter %s, %d blocks\n", __func__, blocks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001628
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001629 /* If we are at a filemark, return a read length of 0 */
Borislav Petkovf2e3ab52008-07-23 19:56:01 +02001630 if (test_bit(IDE_AFLAG_FILEMARK, &drive->atapi_flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001631 return 0;
1632
Borislav Petkov83042b22008-04-27 15:38:27 +02001633 idetape_init_read(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634
Borislav Petkov5e69bd92008-04-27 15:38:25 +02001635 return idetape_queue_rw_tail(drive, REQ_IDETAPE_READ, blocks,
Borislav Petkov077e3bd2008-04-27 15:38:34 +02001636 tape->merge_bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001637}
1638
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001639static void idetape_pad_zeros(ide_drive_t *drive, int bcount)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001640{
1641 idetape_tape_t *tape = drive->driver_data;
1642 struct idetape_bh *bh;
1643 int blocks;
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001644
Linus Torvalds1da177e2005-04-16 15:20:36 -07001645 while (bcount) {
1646 unsigned int count;
1647
Borislav Petkov077e3bd2008-04-27 15:38:34 +02001648 bh = tape->merge_bh;
Borislav Petkovf73850a2008-04-27 15:38:33 +02001649 count = min(tape->buffer_size, bcount);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001650 bcount -= count;
Borislav Petkov54bb2072008-02-06 02:57:52 +01001651 blocks = count / tape->blk_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001652 while (count) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001653 atomic_set(&bh->b_count,
1654 min(count, (unsigned int)bh->b_size));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001655 memset(bh->b_data, 0, atomic_read(&bh->b_count));
1656 count -= atomic_read(&bh->b_count);
1657 bh = bh->b_reqnext;
1658 }
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001659 idetape_queue_rw_tail(drive, REQ_IDETAPE_WRITE, blocks,
Borislav Petkov077e3bd2008-04-27 15:38:34 +02001660 tape->merge_bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001661 }
1662}
1663
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001665 * Rewinds the tape to the Beginning Of the current Partition (BOP). We
1666 * currently support only one partition.
1667 */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001668static int idetape_rewind_tape(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001669{
1670 int retval;
Borislav Petkovd236d742008-04-18 00:46:27 +02001671 struct ide_atapi_pc pc;
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001672 idetape_tape_t *tape;
1673 tape = drive->driver_data;
1674
1675 debug_log(DBG_SENSE, "Enter %s\n", __func__);
1676
Linus Torvalds1da177e2005-04-16 15:20:36 -07001677 idetape_create_rewind_cmd(drive, &pc);
1678 retval = idetape_queue_pc_tail(drive, &pc);
1679 if (retval)
1680 return retval;
1681
1682 idetape_create_read_position_cmd(&pc);
1683 retval = idetape_queue_pc_tail(drive, &pc);
1684 if (retval)
1685 return retval;
1686 return 0;
1687}
1688
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001689/* mtio.h compatible commands should be issued to the chrdev interface. */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001690static int idetape_blkdev_ioctl(ide_drive_t *drive, unsigned int cmd,
1691 unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001692{
1693 idetape_tape_t *tape = drive->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694 void __user *argp = (void __user *)arg;
1695
Borislav Petkovd59823f2008-02-02 19:56:51 +01001696 struct idetape_config {
1697 int dsc_rw_frequency;
1698 int dsc_media_access_frequency;
1699 int nr_stages;
1700 } config;
1701
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001702 debug_log(DBG_PROCS, "Enter %s\n", __func__);
1703
Linus Torvalds1da177e2005-04-16 15:20:36 -07001704 switch (cmd) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001705 case 0x0340:
1706 if (copy_from_user(&config, argp, sizeof(config)))
1707 return -EFAULT;
1708 tape->best_dsc_rw_freq = config.dsc_rw_frequency;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001709 break;
1710 case 0x0350:
1711 config.dsc_rw_frequency = (int) tape->best_dsc_rw_freq;
Borislav Petkov83042b22008-04-27 15:38:27 +02001712 config.nr_stages = 1;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001713 if (copy_to_user(argp, &config, sizeof(config)))
1714 return -EFAULT;
1715 break;
1716 default:
1717 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001718 }
1719 return 0;
1720}
1721
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001722static int idetape_space_over_filemarks(ide_drive_t *drive, short mt_op,
1723 int mt_count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001724{
1725 idetape_tape_t *tape = drive->driver_data;
Borislav Petkovd236d742008-04-18 00:46:27 +02001726 struct ide_atapi_pc pc;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001727 int retval, count = 0;
Borislav Petkovb6422012008-02-02 19:56:49 +01001728 int sprev = !!(tape->caps[4] & 0x20);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001729
1730 if (mt_count == 0)
1731 return 0;
1732 if (MTBSF == mt_op || MTBSFM == mt_op) {
Borislav Petkovb6422012008-02-02 19:56:49 +01001733 if (!sprev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001734 return -EIO;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001735 mt_count = -mt_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001736 }
1737
Borislav Petkov54abf372008-02-06 02:57:52 +01001738 if (tape->chrdev_dir == IDETAPE_DIR_READ) {
Borislav Petkov01a63aeb2008-04-27 15:38:34 +02001739 tape->merge_bh_size = 0;
Borislav Petkovf2e3ab52008-07-23 19:56:01 +02001740 if (test_and_clear_bit(IDE_AFLAG_FILEMARK, &drive->atapi_flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001741 ++count;
Borislav Petkovec0fdb02008-04-27 15:38:34 +02001742 ide_tape_discard_merge_buffer(drive, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001743 }
1744
Linus Torvalds1da177e2005-04-16 15:20:36 -07001745 switch (mt_op) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001746 case MTFSF:
1747 case MTBSF:
1748 idetape_create_space_cmd(&pc, mt_count - count,
1749 IDETAPE_SPACE_OVER_FILEMARK);
1750 return idetape_queue_pc_tail(drive, &pc);
1751 case MTFSFM:
1752 case MTBSFM:
1753 if (!sprev)
1754 return -EIO;
1755 retval = idetape_space_over_filemarks(drive, MTFSF,
1756 mt_count - count);
1757 if (retval)
1758 return retval;
1759 count = (MTBSFM == mt_op ? 1 : -1);
1760 return idetape_space_over_filemarks(drive, MTFSF, count);
1761 default:
1762 printk(KERN_ERR "ide-tape: MTIO operation %d not supported\n",
1763 mt_op);
1764 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001765 }
1766}
1767
Linus Torvalds1da177e2005-04-16 15:20:36 -07001768/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001769 * Our character device read / write functions.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001770 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001771 * The tape is optimized to maximize throughput when it is transferring an
1772 * integral number of the "continuous transfer limit", which is a parameter of
1773 * the specific tape (26kB on my particular tape, 32kB for Onstream).
Linus Torvalds1da177e2005-04-16 15:20:36 -07001774 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001775 * As of version 1.3 of the driver, the character device provides an abstract
1776 * continuous view of the media - any mix of block sizes (even 1 byte) on the
1777 * same backup/restore procedure is supported. The driver will internally
1778 * convert the requests to the recommended transfer unit, so that an unmatch
1779 * between the user's block size to the recommended size will only result in a
1780 * (slightly) increased driver overhead, but will no longer hit performance.
1781 * This is not applicable to Onstream.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001782 */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001783static ssize_t idetape_chrdev_read(struct file *file, char __user *buf,
1784 size_t count, loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001785{
1786 struct ide_tape_obj *tape = ide_tape_f(file);
1787 ide_drive_t *drive = tape->drive;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001788 ssize_t bytes_read, temp, actually_read = 0, rc;
Daniel Walkerdcd96372006-06-25 05:47:37 -07001789 ssize_t ret = 0;
Borislav Petkovb6422012008-02-02 19:56:49 +01001790 u16 ctl = *(u16 *)&tape->caps[12];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001791
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001792 debug_log(DBG_CHRDEV, "Enter %s, count %Zd\n", __func__, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001793
Borislav Petkov54abf372008-02-06 02:57:52 +01001794 if (tape->chrdev_dir != IDETAPE_DIR_READ) {
Borislav Petkovf2e3ab52008-07-23 19:56:01 +02001795 if (test_bit(IDE_AFLAG_DETECT_BS, &drive->atapi_flags))
Borislav Petkov54bb2072008-02-06 02:57:52 +01001796 if (count > tape->blk_size &&
1797 (count % tape->blk_size) == 0)
1798 tape->user_bs_factor = count / tape->blk_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001799 }
Borislav Petkov83042b22008-04-27 15:38:27 +02001800 rc = idetape_init_read(drive);
Borislav Petkov8d06bfa2008-02-06 02:57:53 +01001801 if (rc < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001802 return rc;
1803 if (count == 0)
1804 return (0);
Borislav Petkov01a63aeb2008-04-27 15:38:34 +02001805 if (tape->merge_bh_size) {
1806 actually_read = min((unsigned int)(tape->merge_bh_size),
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001807 (unsigned int)count);
Borislav Petkov99d74e62008-04-27 15:38:25 +02001808 if (idetape_copy_stage_to_user(tape, buf, actually_read))
Daniel Walkerdcd96372006-06-25 05:47:37 -07001809 ret = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001810 buf += actually_read;
Borislav Petkov01a63aeb2008-04-27 15:38:34 +02001811 tape->merge_bh_size -= actually_read;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001812 count -= actually_read;
1813 }
Borislav Petkovf73850a2008-04-27 15:38:33 +02001814 while (count >= tape->buffer_size) {
Borislav Petkovb6422012008-02-02 19:56:49 +01001815 bytes_read = idetape_add_chrdev_read_request(drive, ctl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001816 if (bytes_read <= 0)
1817 goto finish;
Borislav Petkov99d74e62008-04-27 15:38:25 +02001818 if (idetape_copy_stage_to_user(tape, buf, bytes_read))
Daniel Walkerdcd96372006-06-25 05:47:37 -07001819 ret = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001820 buf += bytes_read;
1821 count -= bytes_read;
1822 actually_read += bytes_read;
1823 }
1824 if (count) {
Borislav Petkovb6422012008-02-02 19:56:49 +01001825 bytes_read = idetape_add_chrdev_read_request(drive, ctl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001826 if (bytes_read <= 0)
1827 goto finish;
1828 temp = min((unsigned long)count, (unsigned long)bytes_read);
Borislav Petkov99d74e62008-04-27 15:38:25 +02001829 if (idetape_copy_stage_to_user(tape, buf, temp))
Daniel Walkerdcd96372006-06-25 05:47:37 -07001830 ret = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001831 actually_read += temp;
Borislav Petkov01a63aeb2008-04-27 15:38:34 +02001832 tape->merge_bh_size = bytes_read-temp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001833 }
1834finish:
Borislav Petkovf2e3ab52008-07-23 19:56:01 +02001835 if (!actually_read && test_bit(IDE_AFLAG_FILEMARK, &drive->atapi_flags)) {
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001836 debug_log(DBG_SENSE, "%s: spacing over filemark\n", tape->name);
1837
Linus Torvalds1da177e2005-04-16 15:20:36 -07001838 idetape_space_over_filemarks(drive, MTFSF, 1);
1839 return 0;
1840 }
Daniel Walkerdcd96372006-06-25 05:47:37 -07001841
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001842 return ret ? ret : actually_read;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001843}
1844
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001845static ssize_t idetape_chrdev_write(struct file *file, const char __user *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001846 size_t count, loff_t *ppos)
1847{
1848 struct ide_tape_obj *tape = ide_tape_f(file);
1849 ide_drive_t *drive = tape->drive;
Daniel Walkerdcd96372006-06-25 05:47:37 -07001850 ssize_t actually_written = 0;
1851 ssize_t ret = 0;
Borislav Petkovb6422012008-02-02 19:56:49 +01001852 u16 ctl = *(u16 *)&tape->caps[12];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001853
1854 /* The drive is write protected. */
1855 if (tape->write_prot)
1856 return -EACCES;
1857
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001858 debug_log(DBG_CHRDEV, "Enter %s, count %Zd\n", __func__, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001859
1860 /* Initialize write operation */
Borislav Petkov54abf372008-02-06 02:57:52 +01001861 if (tape->chrdev_dir != IDETAPE_DIR_WRITE) {
1862 if (tape->chrdev_dir == IDETAPE_DIR_READ)
Borislav Petkovec0fdb02008-04-27 15:38:34 +02001863 ide_tape_discard_merge_buffer(drive, 1);
Borislav Petkov077e3bd2008-04-27 15:38:34 +02001864 if (tape->merge_bh || tape->merge_bh_size) {
Borislav Petkov01a63aeb2008-04-27 15:38:34 +02001865 printk(KERN_ERR "ide-tape: merge_bh_size "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001866 "should be 0 now\n");
Borislav Petkov01a63aeb2008-04-27 15:38:34 +02001867 tape->merge_bh_size = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001868 }
Borislav Petkov077e3bd2008-04-27 15:38:34 +02001869 tape->merge_bh = ide_tape_kmalloc_buffer(tape, 0, 0);
1870 if (!tape->merge_bh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001871 return -ENOMEM;
Borislav Petkov54abf372008-02-06 02:57:52 +01001872 tape->chrdev_dir = IDETAPE_DIR_WRITE;
Borislav Petkov077e3bd2008-04-27 15:38:34 +02001873 idetape_init_merge_buffer(tape);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001874
1875 /*
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001876 * Issue a write 0 command to ensure that DSC handshake is
1877 * switched from completion mode to buffer available mode. No
1878 * point in issuing this if DSC overlap isn't supported, some
1879 * drives (Seagate STT3401A) will return an error.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001880 */
1881 if (drive->dsc_overlap) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001882 ssize_t retval = idetape_queue_rw_tail(drive,
1883 REQ_IDETAPE_WRITE, 0,
Borislav Petkov077e3bd2008-04-27 15:38:34 +02001884 tape->merge_bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001885 if (retval < 0) {
Borislav Petkov077e3bd2008-04-27 15:38:34 +02001886 ide_tape_kfree_buffer(tape);
1887 tape->merge_bh = NULL;
Borislav Petkov54abf372008-02-06 02:57:52 +01001888 tape->chrdev_dir = IDETAPE_DIR_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001889 return retval;
1890 }
1891 }
1892 }
1893 if (count == 0)
1894 return (0);
Borislav Petkov01a63aeb2008-04-27 15:38:34 +02001895 if (tape->merge_bh_size) {
1896 if (tape->merge_bh_size >= tape->buffer_size) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001897 printk(KERN_ERR "ide-tape: bug: merge buf too big\n");
Borislav Petkov01a63aeb2008-04-27 15:38:34 +02001898 tape->merge_bh_size = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001899 }
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001900 actually_written = min((unsigned int)
Borislav Petkov01a63aeb2008-04-27 15:38:34 +02001901 (tape->buffer_size - tape->merge_bh_size),
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001902 (unsigned int)count);
Borislav Petkov8646c882008-04-27 15:38:26 +02001903 if (idetape_copy_stage_from_user(tape, buf, actually_written))
Daniel Walkerdcd96372006-06-25 05:47:37 -07001904 ret = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001905 buf += actually_written;
Borislav Petkov01a63aeb2008-04-27 15:38:34 +02001906 tape->merge_bh_size += actually_written;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001907 count -= actually_written;
1908
Borislav Petkov01a63aeb2008-04-27 15:38:34 +02001909 if (tape->merge_bh_size == tape->buffer_size) {
Daniel Walkerdcd96372006-06-25 05:47:37 -07001910 ssize_t retval;
Borislav Petkov01a63aeb2008-04-27 15:38:34 +02001911 tape->merge_bh_size = 0;
Borislav Petkovb6422012008-02-02 19:56:49 +01001912 retval = idetape_add_chrdev_write_request(drive, ctl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001913 if (retval <= 0)
1914 return (retval);
1915 }
1916 }
Borislav Petkovf73850a2008-04-27 15:38:33 +02001917 while (count >= tape->buffer_size) {
Daniel Walkerdcd96372006-06-25 05:47:37 -07001918 ssize_t retval;
Borislav Petkovf73850a2008-04-27 15:38:33 +02001919 if (idetape_copy_stage_from_user(tape, buf, tape->buffer_size))
Daniel Walkerdcd96372006-06-25 05:47:37 -07001920 ret = -EFAULT;
Borislav Petkovf73850a2008-04-27 15:38:33 +02001921 buf += tape->buffer_size;
1922 count -= tape->buffer_size;
Borislav Petkovb6422012008-02-02 19:56:49 +01001923 retval = idetape_add_chrdev_write_request(drive, ctl);
Borislav Petkovf73850a2008-04-27 15:38:33 +02001924 actually_written += tape->buffer_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001925 if (retval <= 0)
1926 return (retval);
1927 }
1928 if (count) {
1929 actually_written += count;
Borislav Petkov8646c882008-04-27 15:38:26 +02001930 if (idetape_copy_stage_from_user(tape, buf, count))
Daniel Walkerdcd96372006-06-25 05:47:37 -07001931 ret = -EFAULT;
Borislav Petkov01a63aeb2008-04-27 15:38:34 +02001932 tape->merge_bh_size += count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001933 }
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001934 return ret ? ret : actually_written;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001935}
1936
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001937static int idetape_write_filemark(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001938{
Borislav Petkovd236d742008-04-18 00:46:27 +02001939 struct ide_atapi_pc pc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001940
1941 /* Write a filemark */
1942 idetape_create_write_filemark_cmd(drive, &pc, 1);
1943 if (idetape_queue_pc_tail(drive, &pc)) {
1944 printk(KERN_ERR "ide-tape: Couldn't write a filemark\n");
1945 return -EIO;
1946 }
1947 return 0;
1948}
1949
1950/*
Borislav Petkovd99c9da2008-02-02 19:56:51 +01001951 * Called from idetape_chrdev_ioctl when the general mtio MTIOCTOP ioctl is
1952 * requested.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001953 *
Borislav Petkovd99c9da2008-02-02 19:56:51 +01001954 * Note: MTBSF and MTBSFM are not supported when the tape doesn't support
1955 * spacing over filemarks in the reverse direction. In this case, MTFSFM is also
Borislav Petkov5bd50dc2008-04-27 15:38:28 +02001956 * usually not supported.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001957 *
Borislav Petkovd99c9da2008-02-02 19:56:51 +01001958 * The following commands are currently not supported:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001959 *
Borislav Petkovd99c9da2008-02-02 19:56:51 +01001960 * MTFSS, MTBSS, MTWSM, MTSETDENSITY, MTSETDRVBUFFER, MT_ST_BOOLEANS,
1961 * MT_ST_WRITE_THRESHOLD.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001962 */
Borislav Petkovd99c9da2008-02-02 19:56:51 +01001963static int idetape_mtioctop(ide_drive_t *drive, short mt_op, int mt_count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001964{
1965 idetape_tape_t *tape = drive->driver_data;
Borislav Petkovd236d742008-04-18 00:46:27 +02001966 struct ide_atapi_pc pc;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001967 int i, retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001968
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001969 debug_log(DBG_ERR, "Handling MTIOCTOP ioctl: mt_op=%d, mt_count=%d\n",
1970 mt_op, mt_count);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001971
Linus Torvalds1da177e2005-04-16 15:20:36 -07001972 switch (mt_op) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001973 case MTFSF:
1974 case MTFSFM:
1975 case MTBSF:
1976 case MTBSFM:
1977 if (!mt_count)
1978 return 0;
1979 return idetape_space_over_filemarks(drive, mt_op, mt_count);
1980 default:
1981 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001982 }
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001983
Linus Torvalds1da177e2005-04-16 15:20:36 -07001984 switch (mt_op) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001985 case MTWEOF:
1986 if (tape->write_prot)
1987 return -EACCES;
Borislav Petkovec0fdb02008-04-27 15:38:34 +02001988 ide_tape_discard_merge_buffer(drive, 1);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001989 for (i = 0; i < mt_count; i++) {
1990 retval = idetape_write_filemark(drive);
1991 if (retval)
1992 return retval;
1993 }
1994 return 0;
1995 case MTREW:
Borislav Petkovec0fdb02008-04-27 15:38:34 +02001996 ide_tape_discard_merge_buffer(drive, 0);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001997 if (idetape_rewind_tape(drive))
1998 return -EIO;
1999 return 0;
2000 case MTLOAD:
Borislav Petkovec0fdb02008-04-27 15:38:34 +02002001 ide_tape_discard_merge_buffer(drive, 0);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002002 idetape_create_load_unload_cmd(drive, &pc,
2003 IDETAPE_LU_LOAD_MASK);
2004 return idetape_queue_pc_tail(drive, &pc);
2005 case MTUNLOAD:
2006 case MTOFFL:
2007 /*
2008 * If door is locked, attempt to unlock before
2009 * attempting to eject.
2010 */
2011 if (tape->door_locked) {
2012 if (idetape_create_prevent_cmd(drive, &pc, 0))
2013 if (!idetape_queue_pc_tail(drive, &pc))
2014 tape->door_locked = DOOR_UNLOCKED;
2015 }
Borislav Petkovec0fdb02008-04-27 15:38:34 +02002016 ide_tape_discard_merge_buffer(drive, 0);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002017 idetape_create_load_unload_cmd(drive, &pc,
2018 !IDETAPE_LU_LOAD_MASK);
2019 retval = idetape_queue_pc_tail(drive, &pc);
2020 if (!retval)
Borislav Petkovf2e3ab52008-07-23 19:56:01 +02002021 clear_bit(IDE_AFLAG_MEDIUM_PRESENT, &drive->atapi_flags);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002022 return retval;
2023 case MTNOP:
Borislav Petkovec0fdb02008-04-27 15:38:34 +02002024 ide_tape_discard_merge_buffer(drive, 0);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002025 return idetape_flush_tape_buffers(drive);
2026 case MTRETEN:
Borislav Petkovec0fdb02008-04-27 15:38:34 +02002027 ide_tape_discard_merge_buffer(drive, 0);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002028 idetape_create_load_unload_cmd(drive, &pc,
2029 IDETAPE_LU_RETENSION_MASK | IDETAPE_LU_LOAD_MASK);
2030 return idetape_queue_pc_tail(drive, &pc);
2031 case MTEOM:
2032 idetape_create_space_cmd(&pc, 0, IDETAPE_SPACE_TO_EOD);
2033 return idetape_queue_pc_tail(drive, &pc);
2034 case MTERASE:
2035 (void)idetape_rewind_tape(drive);
2036 idetape_create_erase_cmd(&pc);
2037 return idetape_queue_pc_tail(drive, &pc);
2038 case MTSETBLK:
2039 if (mt_count) {
2040 if (mt_count < tape->blk_size ||
2041 mt_count % tape->blk_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002042 return -EIO;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002043 tape->user_bs_factor = mt_count / tape->blk_size;
Borislav Petkovf2e3ab52008-07-23 19:56:01 +02002044 clear_bit(IDE_AFLAG_DETECT_BS, &drive->atapi_flags);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002045 } else
Borislav Petkovf2e3ab52008-07-23 19:56:01 +02002046 set_bit(IDE_AFLAG_DETECT_BS, &drive->atapi_flags);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002047 return 0;
2048 case MTSEEK:
Borislav Petkovec0fdb02008-04-27 15:38:34 +02002049 ide_tape_discard_merge_buffer(drive, 0);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002050 return idetape_position_tape(drive,
2051 mt_count * tape->user_bs_factor, tape->partition, 0);
2052 case MTSETPART:
Borislav Petkovec0fdb02008-04-27 15:38:34 +02002053 ide_tape_discard_merge_buffer(drive, 0);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002054 return idetape_position_tape(drive, 0, mt_count, 0);
2055 case MTFSR:
2056 case MTBSR:
2057 case MTLOCK:
2058 if (!idetape_create_prevent_cmd(drive, &pc, 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002059 return 0;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002060 retval = idetape_queue_pc_tail(drive, &pc);
2061 if (retval)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002062 return retval;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002063 tape->door_locked = DOOR_EXPLICITLY_LOCKED;
2064 return 0;
2065 case MTUNLOCK:
2066 if (!idetape_create_prevent_cmd(drive, &pc, 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002067 return 0;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002068 retval = idetape_queue_pc_tail(drive, &pc);
2069 if (retval)
2070 return retval;
2071 tape->door_locked = DOOR_UNLOCKED;
2072 return 0;
2073 default:
2074 printk(KERN_ERR "ide-tape: MTIO operation %d not supported\n",
2075 mt_op);
2076 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002077 }
2078}
2079
2080/*
Borislav Petkovd99c9da2008-02-02 19:56:51 +01002081 * Our character device ioctls. General mtio.h magnetic io commands are
2082 * supported here, and not in the corresponding block interface. Our own
2083 * ide-tape ioctls are supported on both interfaces.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002084 */
Borislav Petkovd99c9da2008-02-02 19:56:51 +01002085static int idetape_chrdev_ioctl(struct inode *inode, struct file *file,
2086 unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002087{
2088 struct ide_tape_obj *tape = ide_tape_f(file);
2089 ide_drive_t *drive = tape->drive;
2090 struct mtop mtop;
2091 struct mtget mtget;
2092 struct mtpos mtpos;
Borislav Petkov54bb2072008-02-06 02:57:52 +01002093 int block_offset = 0, position = tape->first_frame;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002094 void __user *argp = (void __user *)arg;
2095
Borislav Petkov8004a8c2008-02-06 02:57:51 +01002096 debug_log(DBG_CHRDEV, "Enter %s, cmd=%u\n", __func__, cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002097
Borislav Petkov54abf372008-02-06 02:57:52 +01002098 if (tape->chrdev_dir == IDETAPE_DIR_WRITE) {
Borislav Petkovd9df9372008-04-27 15:38:34 +02002099 ide_tape_flush_merge_buffer(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002100 idetape_flush_tape_buffers(drive);
2101 }
2102 if (cmd == MTIOCGET || cmd == MTIOCPOS) {
Borislav Petkov01a63aeb2008-04-27 15:38:34 +02002103 block_offset = tape->merge_bh_size /
Borislav Petkov54bb2072008-02-06 02:57:52 +01002104 (tape->blk_size * tape->user_bs_factor);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002105 position = idetape_read_position(drive);
2106 if (position < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002107 return -EIO;
2108 }
2109 switch (cmd) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002110 case MTIOCTOP:
2111 if (copy_from_user(&mtop, argp, sizeof(struct mtop)))
2112 return -EFAULT;
2113 return idetape_mtioctop(drive, mtop.mt_op, mtop.mt_count);
2114 case MTIOCGET:
2115 memset(&mtget, 0, sizeof(struct mtget));
2116 mtget.mt_type = MT_ISSCSI2;
2117 mtget.mt_blkno = position / tape->user_bs_factor - block_offset;
2118 mtget.mt_dsreg =
2119 ((tape->blk_size * tape->user_bs_factor)
2120 << MT_ST_BLKSIZE_SHIFT) & MT_ST_BLKSIZE_MASK;
Borislav Petkov54bb2072008-02-06 02:57:52 +01002121
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002122 if (tape->drv_write_prot)
2123 mtget.mt_gstat |= GMT_WR_PROT(0xffffffff);
2124
2125 if (copy_to_user(argp, &mtget, sizeof(struct mtget)))
2126 return -EFAULT;
2127 return 0;
2128 case MTIOCPOS:
2129 mtpos.mt_blkno = position / tape->user_bs_factor - block_offset;
2130 if (copy_to_user(argp, &mtpos, sizeof(struct mtpos)))
2131 return -EFAULT;
2132 return 0;
2133 default:
2134 if (tape->chrdev_dir == IDETAPE_DIR_READ)
Borislav Petkovec0fdb02008-04-27 15:38:34 +02002135 ide_tape_discard_merge_buffer(drive, 1);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002136 return idetape_blkdev_ioctl(drive, cmd, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002137 }
2138}
2139
Borislav Petkov3cffb9c2008-02-02 19:56:50 +01002140/*
2141 * Do a mode sense page 0 with block descriptor and if it succeeds set the tape
2142 * block size with the reported value.
2143 */
2144static void ide_tape_get_bsize_from_bdesc(ide_drive_t *drive)
2145{
2146 idetape_tape_t *tape = drive->driver_data;
Borislav Petkovd236d742008-04-18 00:46:27 +02002147 struct ide_atapi_pc pc;
Borislav Petkov3cffb9c2008-02-02 19:56:50 +01002148
2149 idetape_create_mode_sense_cmd(&pc, IDETAPE_BLOCK_DESCRIPTOR);
2150 if (idetape_queue_pc_tail(drive, &pc)) {
2151 printk(KERN_ERR "ide-tape: Can't get block descriptor\n");
Borislav Petkov54bb2072008-02-06 02:57:52 +01002152 if (tape->blk_size == 0) {
Borislav Petkov3cffb9c2008-02-02 19:56:50 +01002153 printk(KERN_WARNING "ide-tape: Cannot deal with zero "
2154 "block size, assuming 32k\n");
Borislav Petkov54bb2072008-02-06 02:57:52 +01002155 tape->blk_size = 32768;
Borislav Petkov3cffb9c2008-02-02 19:56:50 +01002156 }
2157 return;
2158 }
Borislav Petkovd236d742008-04-18 00:46:27 +02002159 tape->blk_size = (pc.buf[4 + 5] << 16) +
2160 (pc.buf[4 + 6] << 8) +
2161 pc.buf[4 + 7];
2162 tape->drv_write_prot = (pc.buf[2] & 0x80) >> 7;
Borislav Petkov3cffb9c2008-02-02 19:56:50 +01002163}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002164
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002165static int idetape_chrdev_open(struct inode *inode, struct file *filp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002166{
2167 unsigned int minor = iminor(inode), i = minor & ~0xc0;
2168 ide_drive_t *drive;
2169 idetape_tape_t *tape;
Borislav Petkovd236d742008-04-18 00:46:27 +02002170 struct ide_atapi_pc pc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002171 int retval;
2172
Borislav Petkov8004a8c2008-02-06 02:57:51 +01002173 if (i >= MAX_HWIFS * MAX_DRIVES)
2174 return -ENXIO;
2175
Jonathan Corbet04f4ac92008-05-15 12:01:56 -06002176 lock_kernel();
Borislav Petkov8004a8c2008-02-06 02:57:51 +01002177 tape = ide_tape_chrdev_get(i);
Jonathan Corbet04f4ac92008-05-15 12:01:56 -06002178 if (!tape) {
2179 unlock_kernel();
Borislav Petkov8004a8c2008-02-06 02:57:51 +01002180 return -ENXIO;
Jonathan Corbet04f4ac92008-05-15 12:01:56 -06002181 }
Borislav Petkov8004a8c2008-02-06 02:57:51 +01002182
2183 debug_log(DBG_CHRDEV, "Enter %s\n", __func__);
2184
Linus Torvalds1da177e2005-04-16 15:20:36 -07002185 /*
2186 * We really want to do nonseekable_open(inode, filp); here, but some
2187 * versions of tar incorrectly call lseek on tapes and bail out if that
2188 * fails. So we disallow pread() and pwrite(), but permit lseeks.
2189 */
2190 filp->f_mode &= ~(FMODE_PREAD | FMODE_PWRITE);
2191
Linus Torvalds1da177e2005-04-16 15:20:36 -07002192 drive = tape->drive;
2193
2194 filp->private_data = tape;
2195
Borislav Petkovf2e3ab52008-07-23 19:56:01 +02002196 if (test_and_set_bit(IDE_AFLAG_BUSY, &drive->atapi_flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002197 retval = -EBUSY;
2198 goto out_put_tape;
2199 }
2200
2201 retval = idetape_wait_ready(drive, 60 * HZ);
2202 if (retval) {
Borislav Petkovf2e3ab52008-07-23 19:56:01 +02002203 clear_bit(IDE_AFLAG_BUSY, &drive->atapi_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002204 printk(KERN_ERR "ide-tape: %s: drive not ready\n", tape->name);
2205 goto out_put_tape;
2206 }
2207
2208 idetape_read_position(drive);
Borislav Petkovf2e3ab52008-07-23 19:56:01 +02002209 if (!test_bit(IDE_AFLAG_ADDRESS_VALID, &drive->atapi_flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002210 (void)idetape_rewind_tape(drive);
2211
Linus Torvalds1da177e2005-04-16 15:20:36 -07002212 /* Read block size and write protect status from drive. */
Borislav Petkov3cffb9c2008-02-02 19:56:50 +01002213 ide_tape_get_bsize_from_bdesc(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002214
2215 /* Set write protect flag if device is opened as read-only. */
2216 if ((filp->f_flags & O_ACCMODE) == O_RDONLY)
2217 tape->write_prot = 1;
2218 else
2219 tape->write_prot = tape->drv_write_prot;
2220
2221 /* Make sure drive isn't write protected if user wants to write. */
2222 if (tape->write_prot) {
2223 if ((filp->f_flags & O_ACCMODE) == O_WRONLY ||
2224 (filp->f_flags & O_ACCMODE) == O_RDWR) {
Borislav Petkovf2e3ab52008-07-23 19:56:01 +02002225 clear_bit(IDE_AFLAG_BUSY, &drive->atapi_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002226 retval = -EROFS;
2227 goto out_put_tape;
2228 }
2229 }
2230
Borislav Petkov3c98bf32008-02-06 02:57:53 +01002231 /* Lock the tape drive door so user can't eject. */
Borislav Petkov54abf372008-02-06 02:57:52 +01002232 if (tape->chrdev_dir == IDETAPE_DIR_NONE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002233 if (idetape_create_prevent_cmd(drive, &pc, 1)) {
2234 if (!idetape_queue_pc_tail(drive, &pc)) {
2235 if (tape->door_locked != DOOR_EXPLICITLY_LOCKED)
2236 tape->door_locked = DOOR_LOCKED;
2237 }
2238 }
2239 }
Jonathan Corbet04f4ac92008-05-15 12:01:56 -06002240 unlock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002241 return 0;
2242
2243out_put_tape:
2244 ide_tape_put(tape);
Jonathan Corbet04f4ac92008-05-15 12:01:56 -06002245 unlock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002246 return retval;
2247}
2248
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002249static void idetape_write_release(ide_drive_t *drive, unsigned int minor)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002250{
2251 idetape_tape_t *tape = drive->driver_data;
2252
Borislav Petkovd9df9372008-04-27 15:38:34 +02002253 ide_tape_flush_merge_buffer(drive);
Borislav Petkov077e3bd2008-04-27 15:38:34 +02002254 tape->merge_bh = ide_tape_kmalloc_buffer(tape, 1, 0);
2255 if (tape->merge_bh != NULL) {
Borislav Petkov54bb2072008-02-06 02:57:52 +01002256 idetape_pad_zeros(drive, tape->blk_size *
2257 (tape->user_bs_factor - 1));
Borislav Petkov077e3bd2008-04-27 15:38:34 +02002258 ide_tape_kfree_buffer(tape);
2259 tape->merge_bh = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002260 }
2261 idetape_write_filemark(drive);
2262 idetape_flush_tape_buffers(drive);
2263 idetape_flush_tape_buffers(drive);
2264}
2265
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002266static int idetape_chrdev_release(struct inode *inode, struct file *filp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002267{
2268 struct ide_tape_obj *tape = ide_tape_f(filp);
2269 ide_drive_t *drive = tape->drive;
Borislav Petkovd236d742008-04-18 00:46:27 +02002270 struct ide_atapi_pc pc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002271 unsigned int minor = iminor(inode);
2272
2273 lock_kernel();
2274 tape = drive->driver_data;
Borislav Petkov8004a8c2008-02-06 02:57:51 +01002275
2276 debug_log(DBG_CHRDEV, "Enter %s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002277
Borislav Petkov54abf372008-02-06 02:57:52 +01002278 if (tape->chrdev_dir == IDETAPE_DIR_WRITE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002279 idetape_write_release(drive, minor);
Borislav Petkov54abf372008-02-06 02:57:52 +01002280 if (tape->chrdev_dir == IDETAPE_DIR_READ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002281 if (minor < 128)
Borislav Petkovec0fdb02008-04-27 15:38:34 +02002282 ide_tape_discard_merge_buffer(drive, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002283 }
Borislav Petkovf64eee72008-04-27 15:38:25 +02002284
Borislav Petkovf2e3ab52008-07-23 19:56:01 +02002285 if (minor < 128 && test_bit(IDE_AFLAG_MEDIUM_PRESENT, &drive->atapi_flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002286 (void) idetape_rewind_tape(drive);
Borislav Petkov54abf372008-02-06 02:57:52 +01002287 if (tape->chrdev_dir == IDETAPE_DIR_NONE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002288 if (tape->door_locked == DOOR_LOCKED) {
2289 if (idetape_create_prevent_cmd(drive, &pc, 0)) {
2290 if (!idetape_queue_pc_tail(drive, &pc))
2291 tape->door_locked = DOOR_UNLOCKED;
2292 }
2293 }
2294 }
Borislav Petkovf2e3ab52008-07-23 19:56:01 +02002295 clear_bit(IDE_AFLAG_BUSY, &drive->atapi_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002296 ide_tape_put(tape);
2297 unlock_kernel();
2298 return 0;
2299}
2300
Borislav Petkov6d29c8f2008-02-02 19:56:49 +01002301static void idetape_get_inquiry_results(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002302{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002303 idetape_tape_t *tape = drive->driver_data;
Borislav Petkovd236d742008-04-18 00:46:27 +02002304 struct ide_atapi_pc pc;
Borislav Petkov801bd322008-09-27 19:32:17 +02002305 char fw_rev[4], vendor_id[8], product_id[16];
Borislav Petkov6d29c8f2008-02-02 19:56:49 +01002306
Linus Torvalds1da177e2005-04-16 15:20:36 -07002307 idetape_create_inquiry_cmd(&pc);
2308 if (idetape_queue_pc_tail(drive, &pc)) {
Borislav Petkov6d29c8f2008-02-02 19:56:49 +01002309 printk(KERN_ERR "ide-tape: %s: can't get INQUIRY results\n",
2310 tape->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002311 return;
2312 }
Borislav Petkovd236d742008-04-18 00:46:27 +02002313 memcpy(vendor_id, &pc.buf[8], 8);
2314 memcpy(product_id, &pc.buf[16], 16);
2315 memcpy(fw_rev, &pc.buf[32], 4);
Borislav Petkov6d29c8f2008-02-02 19:56:49 +01002316
Borislav Petkov801bd322008-09-27 19:32:17 +02002317 ide_fixstring(vendor_id, 8, 0);
2318 ide_fixstring(product_id, 16, 0);
2319 ide_fixstring(fw_rev, 4, 0);
Borislav Petkov41f81d542008-02-06 02:57:52 +01002320
Borislav Petkov801bd322008-09-27 19:32:17 +02002321 printk(KERN_INFO "ide-tape: %s <-> %s: %.8s %.16s rev %.4s\n",
Borislav Petkov41f81d542008-02-06 02:57:52 +01002322 drive->name, tape->name, vendor_id, product_id, fw_rev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002323}
2324
2325/*
Borislav Petkovb6422012008-02-02 19:56:49 +01002326 * Ask the tape about its various parameters. In particular, we will adjust our
2327 * data transfer buffer size to the recommended value as returned by the tape.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002328 */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002329static void idetape_get_mode_sense_results(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002330{
2331 idetape_tape_t *tape = drive->driver_data;
Borislav Petkovd236d742008-04-18 00:46:27 +02002332 struct ide_atapi_pc pc;
Borislav Petkovb6422012008-02-02 19:56:49 +01002333 u8 *caps;
2334 u8 speed, max_speed;
Borislav Petkov47314fa2008-02-02 19:56:48 +01002335
Linus Torvalds1da177e2005-04-16 15:20:36 -07002336 idetape_create_mode_sense_cmd(&pc, IDETAPE_CAPABILITIES_PAGE);
2337 if (idetape_queue_pc_tail(drive, &pc)) {
Borislav Petkovb6422012008-02-02 19:56:49 +01002338 printk(KERN_ERR "ide-tape: Can't get tape parameters - assuming"
2339 " some default values\n");
Borislav Petkov54bb2072008-02-06 02:57:52 +01002340 tape->blk_size = 512;
Borislav Petkovb6422012008-02-02 19:56:49 +01002341 put_unaligned(52, (u16 *)&tape->caps[12]);
2342 put_unaligned(540, (u16 *)&tape->caps[14]);
2343 put_unaligned(6*52, (u16 *)&tape->caps[16]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002344 return;
2345 }
Borislav Petkovd236d742008-04-18 00:46:27 +02002346 caps = pc.buf + 4 + pc.buf[3];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002347
Borislav Petkovb6422012008-02-02 19:56:49 +01002348 /* convert to host order and save for later use */
Harvey Harrisoncd740ab2008-07-24 22:53:33 +02002349 speed = be16_to_cpup((__be16 *)&caps[14]);
2350 max_speed = be16_to_cpup((__be16 *)&caps[8]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002351
Harvey Harrisoncd740ab2008-07-24 22:53:33 +02002352 *(u16 *)&caps[8] = max_speed;
2353 *(u16 *)&caps[12] = be16_to_cpup((__be16 *)&caps[12]);
2354 *(u16 *)&caps[14] = speed;
2355 *(u16 *)&caps[16] = be16_to_cpup((__be16 *)&caps[16]);
Borislav Petkovb6422012008-02-02 19:56:49 +01002356
2357 if (!speed) {
2358 printk(KERN_INFO "ide-tape: %s: invalid tape speed "
2359 "(assuming 650KB/sec)\n", drive->name);
Harvey Harrisoncd740ab2008-07-24 22:53:33 +02002360 *(u16 *)&caps[14] = 650;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002361 }
Borislav Petkovb6422012008-02-02 19:56:49 +01002362 if (!max_speed) {
2363 printk(KERN_INFO "ide-tape: %s: invalid max_speed "
2364 "(assuming 650KB/sec)\n", drive->name);
Harvey Harrisoncd740ab2008-07-24 22:53:33 +02002365 *(u16 *)&caps[8] = 650;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002366 }
2367
Borislav Petkovb6422012008-02-02 19:56:49 +01002368 memcpy(&tape->caps, caps, 20);
2369 if (caps[7] & 0x02)
Borislav Petkov54bb2072008-02-06 02:57:52 +01002370 tape->blk_size = 512;
Borislav Petkovb6422012008-02-02 19:56:49 +01002371 else if (caps[7] & 0x04)
Borislav Petkov54bb2072008-02-06 02:57:52 +01002372 tape->blk_size = 1024;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002373}
2374
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +02002375#ifdef CONFIG_IDE_PROC_FS
Bartlomiej Zolnierkiewicz8185d5a2008-10-10 22:39:28 +02002376#define ide_tape_devset_get(name, field) \
2377static int get_##name(ide_drive_t *drive) \
2378{ \
2379 idetape_tape_t *tape = drive->driver_data; \
2380 return tape->field; \
Linus Torvalds1da177e2005-04-16 15:20:36 -07002381}
Bartlomiej Zolnierkiewicz8185d5a2008-10-10 22:39:28 +02002382
2383#define ide_tape_devset_set(name, field) \
2384static int set_##name(ide_drive_t *drive, int arg) \
2385{ \
2386 idetape_tape_t *tape = drive->driver_data; \
2387 tape->field = arg; \
2388 return 0; \
2389}
2390
2391#define ide_tape_devset_rw(_name, _min, _max, _field, _mulf, _divf) \
2392ide_tape_devset_get(_name, _field) \
2393ide_tape_devset_set(_name, _field) \
2394__IDE_DEVSET(_name, S_RW, _min, _max, get_##_name, set_##_name, _mulf, _divf)
2395
2396#define ide_tape_devset_r(_name, _min, _max, _field, _mulf, _divf) \
2397ide_tape_devset_get(_name, _field) \
2398__IDE_DEVSET(_name, S_READ, _min, _max, get_##_name, NULL, _mulf, _divf)
2399
2400static int mulf_tdsc(ide_drive_t *drive) { return 1000; }
2401static int divf_tdsc(ide_drive_t *drive) { return HZ; }
2402static int divf_buffer(ide_drive_t *drive) { return 2; }
2403static int divf_buffer_size(ide_drive_t *drive) { return 1024; }
2404
2405ide_devset_rw(dsc_overlap, 0, 1, dsc_overlap);
2406
2407ide_tape_devset_rw(debug_mask, 0, 0xffff, debug_mask, NULL, NULL);
2408ide_tape_devset_rw(tdsc, IDETAPE_DSC_RW_MIN, IDETAPE_DSC_RW_MAX,
2409 best_dsc_rw_freq, mulf_tdsc, divf_tdsc);
2410
2411ide_tape_devset_r(avg_speed, 0, 0xffff, avg_speed, NULL, NULL);
2412ide_tape_devset_r(speed, 0, 0xffff, caps[14], NULL, NULL);
2413ide_tape_devset_r(buffer, 0, 0xffff, caps[16], NULL, divf_buffer);
2414ide_tape_devset_r(buffer_size, 0, 0xffff, buffer_size, NULL, divf_buffer_size);
2415
2416static const struct ide_devset *idetape_settings[] = {
2417 &ide_devset_avg_speed,
2418 &ide_devset_buffer,
2419 &ide_devset_buffer_size,
2420 &ide_devset_debug_mask,
2421 &ide_devset_dsc_overlap,
2422 &ide_devset_speed,
2423 &ide_devset_tdsc,
2424 NULL
2425};
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +02002426#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002427
2428/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +01002429 * The function below is called to:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002430 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +01002431 * 1. Initialize our various state variables.
2432 * 2. Ask the tape for its capabilities.
2433 * 3. Allocate a buffer which will be used for data transfer. The buffer size
2434 * is chosen based on the recommendation which we received in step 2.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002435 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +01002436 * Note that at this point ide.c already assigned us an irq, so that we can
2437 * queue requests here and wait for their completion.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002438 */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002439static void idetape_setup(ide_drive_t *drive, idetape_tape_t *tape, int minor)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002440{
Borislav Petkov83042b22008-04-27 15:38:27 +02002441 unsigned long t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002442 int speed;
Borislav Petkovf73850a2008-04-27 15:38:33 +02002443 int buffer_size;
Borislav Petkov71071b82008-02-06 02:57:53 +01002444 u8 gcw[2];
Borislav Petkovb6422012008-02-02 19:56:49 +01002445 u16 *ctl = (u16 *)&tape->caps[12];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002446
Borislav Petkov776bb022008-07-23 19:55:59 +02002447 drive->pc_callback = ide_tape_callback;
2448
Borislav Petkov54bb2072008-02-06 02:57:52 +01002449 spin_lock_init(&tape->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002450 drive->dsc_overlap = 1;
Bartlomiej Zolnierkiewicz4166c192008-02-01 23:09:30 +01002451 if (drive->hwif->host_flags & IDE_HFLAG_NO_DSC) {
2452 printk(KERN_INFO "ide-tape: %s: disabling DSC overlap\n",
2453 tape->name);
2454 drive->dsc_overlap = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002455 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002456 /* Seagate Travan drives do not support DSC overlap. */
Bartlomiej Zolnierkiewicz4dde4492008-10-10 22:39:19 +02002457 if (strstr((char *)&drive->id[ATA_ID_PROD], "Seagate STT3401"))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002458 drive->dsc_overlap = 0;
2459 tape->minor = minor;
2460 tape->name[0] = 'h';
2461 tape->name[1] = 't';
2462 tape->name[2] = '0' + minor;
Borislav Petkov54abf372008-02-06 02:57:52 +01002463 tape->chrdev_dir = IDETAPE_DIR_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002464 tape->pc = tape->pc_stack;
Bartlomiej Zolnierkiewicz4dde4492008-10-10 22:39:19 +02002465
2466 *((u16 *)&gcw) = drive->id[ATA_ID_CONFIG];
Borislav Petkov71071b82008-02-06 02:57:53 +01002467
2468 /* Command packet DRQ type */
2469 if (((gcw[0] & 0x60) >> 5) == 1)
Borislav Petkovf2e3ab52008-07-23 19:56:01 +02002470 set_bit(IDE_AFLAG_DRQ_INTERRUPT, &drive->atapi_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002471
Linus Torvalds1da177e2005-04-16 15:20:36 -07002472 idetape_get_inquiry_results(drive);
2473 idetape_get_mode_sense_results(drive);
Borislav Petkov3cffb9c2008-02-02 19:56:50 +01002474 ide_tape_get_bsize_from_bdesc(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002475 tape->user_bs_factor = 1;
Borislav Petkovf73850a2008-04-27 15:38:33 +02002476 tape->buffer_size = *ctl * tape->blk_size;
2477 while (tape->buffer_size > 0xffff) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002478 printk(KERN_NOTICE "ide-tape: decreasing stage size\n");
Borislav Petkovb6422012008-02-02 19:56:49 +01002479 *ctl /= 2;
Borislav Petkovf73850a2008-04-27 15:38:33 +02002480 tape->buffer_size = *ctl * tape->blk_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002481 }
Borislav Petkovf73850a2008-04-27 15:38:33 +02002482 buffer_size = tape->buffer_size;
Borislav Petkova997a432008-04-27 15:38:33 +02002483 tape->pages_per_buffer = buffer_size / PAGE_SIZE;
Borislav Petkovf73850a2008-04-27 15:38:33 +02002484 if (buffer_size % PAGE_SIZE) {
Borislav Petkova997a432008-04-27 15:38:33 +02002485 tape->pages_per_buffer++;
Borislav Petkovf73850a2008-04-27 15:38:33 +02002486 tape->excess_bh_size = PAGE_SIZE - buffer_size % PAGE_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002487 }
2488
Borislav Petkov83042b22008-04-27 15:38:27 +02002489 /* select the "best" DSC read/write polling freq */
Borislav Petkovb6422012008-02-02 19:56:49 +01002490 speed = max(*(u16 *)&tape->caps[14], *(u16 *)&tape->caps[8]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002491
Borislav Petkovf73850a2008-04-27 15:38:33 +02002492 t = (IDETAPE_FIFO_THRESHOLD * tape->buffer_size * HZ) / (speed * 1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002493
2494 /*
Borislav Petkov3c98bf32008-02-06 02:57:53 +01002495 * Ensure that the number we got makes sense; limit it within
2496 * IDETAPE_DSC_RW_MIN and IDETAPE_DSC_RW_MAX.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002497 */
Harvey Harrisona792bd52008-07-15 21:21:41 +02002498 tape->best_dsc_rw_freq = clamp_t(unsigned long, t, IDETAPE_DSC_RW_MIN,
2499 IDETAPE_DSC_RW_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002500 printk(KERN_INFO "ide-tape: %s <-> %s: %dKBps, %d*%dkB buffer, "
Borislav Petkov83042b22008-04-27 15:38:27 +02002501 "%lums tDSC%s\n",
Borislav Petkovb6422012008-02-02 19:56:49 +01002502 drive->name, tape->name, *(u16 *)&tape->caps[14],
Borislav Petkovf73850a2008-04-27 15:38:33 +02002503 (*(u16 *)&tape->caps[16] * 512) / tape->buffer_size,
2504 tape->buffer_size / 1024,
Borislav Petkov54bb2072008-02-06 02:57:52 +01002505 tape->best_dsc_rw_freq * 1000 / HZ,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002506 drive->using_dma ? ", DMA":"");
2507
Bartlomiej Zolnierkiewicz1e874f42008-10-10 22:39:27 +02002508 ide_proc_register_driver(drive, tape->driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002509}
2510
Russell King4031bbe2006-01-06 11:41:00 +00002511static void ide_tape_remove(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002512{
2513 idetape_tape_t *tape = drive->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002514
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +02002515 ide_proc_unregister_driver(drive, tape->driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002516
2517 ide_unregister_region(tape->disk);
2518
2519 ide_tape_put(tape);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002520}
2521
2522static void ide_tape_release(struct kref *kref)
2523{
2524 struct ide_tape_obj *tape = to_ide_tape(kref);
2525 ide_drive_t *drive = tape->drive;
2526 struct gendisk *g = tape->disk;
2527
Borislav Petkov01a63aeb2008-04-27 15:38:34 +02002528 BUG_ON(tape->merge_bh_size);
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02002529
Linus Torvalds1da177e2005-04-16 15:20:36 -07002530 drive->dsc_overlap = 0;
2531 drive->driver_data = NULL;
Tony Jonesdbc12722007-09-25 02:03:03 +02002532 device_destroy(idetape_sysfs_class, MKDEV(IDETAPE_MAJOR, tape->minor));
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002533 device_destroy(idetape_sysfs_class,
2534 MKDEV(IDETAPE_MAJOR, tape->minor + 128));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002535 idetape_devs[tape->minor] = NULL;
2536 g->private_data = NULL;
2537 put_disk(g);
2538 kfree(tape);
2539}
2540
Bartlomiej Zolnierkiewiczecfd80e2007-05-10 00:01:09 +02002541#ifdef CONFIG_IDE_PROC_FS
Linus Torvalds1da177e2005-04-16 15:20:36 -07002542static int proc_idetape_read_name
2543 (char *page, char **start, off_t off, int count, int *eof, void *data)
2544{
2545 ide_drive_t *drive = (ide_drive_t *) data;
2546 idetape_tape_t *tape = drive->driver_data;
2547 char *out = page;
2548 int len;
2549
2550 len = sprintf(out, "%s\n", tape->name);
2551 PROC_IDE_READ_RETURN(page, start, off, count, eof, len);
2552}
2553
2554static ide_proc_entry_t idetape_proc[] = {
2555 { "capacity", S_IFREG|S_IRUGO, proc_ide_read_capacity, NULL },
2556 { "name", S_IFREG|S_IRUGO, proc_idetape_read_name, NULL },
2557 { NULL, 0, NULL, NULL }
2558};
Linus Torvalds1da177e2005-04-16 15:20:36 -07002559#endif
2560
Russell King4031bbe2006-01-06 11:41:00 +00002561static int ide_tape_probe(ide_drive_t *);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002562
Linus Torvalds1da177e2005-04-16 15:20:36 -07002563static ide_driver_t idetape_driver = {
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02002564 .gen_driver = {
Laurent Riffard4ef3b8f2005-11-18 22:15:40 +01002565 .owner = THIS_MODULE,
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02002566 .name = "ide-tape",
2567 .bus = &ide_bus_type,
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02002568 },
Russell King4031bbe2006-01-06 11:41:00 +00002569 .probe = ide_tape_probe,
2570 .remove = ide_tape_remove,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002571 .version = IDETAPE_VERSION,
2572 .media = ide_tape,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002573 .do_request = idetape_do_request,
2574 .end_request = idetape_end_request,
2575 .error = __ide_error,
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +02002576#ifdef CONFIG_IDE_PROC_FS
Linus Torvalds1da177e2005-04-16 15:20:36 -07002577 .proc = idetape_proc,
Bartlomiej Zolnierkiewicz8185d5a2008-10-10 22:39:28 +02002578 .settings = idetape_settings,
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +02002579#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002580};
2581
Borislav Petkov3c98bf32008-02-06 02:57:53 +01002582/* Our character device supporting functions, passed to register_chrdev. */
Arjan van de Ven2b8693c2007-02-12 00:55:32 -08002583static const struct file_operations idetape_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002584 .owner = THIS_MODULE,
2585 .read = idetape_chrdev_read,
2586 .write = idetape_chrdev_write,
2587 .ioctl = idetape_chrdev_ioctl,
2588 .open = idetape_chrdev_open,
2589 .release = idetape_chrdev_release,
2590};
2591
2592static int idetape_open(struct inode *inode, struct file *filp)
2593{
2594 struct gendisk *disk = inode->i_bdev->bd_disk;
2595 struct ide_tape_obj *tape;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002596
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002597 tape = ide_tape_get(disk);
2598 if (!tape)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002599 return -ENXIO;
2600
Linus Torvalds1da177e2005-04-16 15:20:36 -07002601 return 0;
2602}
2603
2604static int idetape_release(struct inode *inode, struct file *filp)
2605{
2606 struct gendisk *disk = inode->i_bdev->bd_disk;
2607 struct ide_tape_obj *tape = ide_tape_g(disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002608
2609 ide_tape_put(tape);
2610
2611 return 0;
2612}
2613
2614static int idetape_ioctl(struct inode *inode, struct file *file,
2615 unsigned int cmd, unsigned long arg)
2616{
2617 struct block_device *bdev = inode->i_bdev;
2618 struct ide_tape_obj *tape = ide_tape_g(bdev->bd_disk);
2619 ide_drive_t *drive = tape->drive;
2620 int err = generic_ide_ioctl(drive, file, bdev, cmd, arg);
2621 if (err == -EINVAL)
2622 err = idetape_blkdev_ioctl(drive, cmd, arg);
2623 return err;
2624}
2625
2626static struct block_device_operations idetape_block_ops = {
2627 .owner = THIS_MODULE,
2628 .open = idetape_open,
2629 .release = idetape_release,
2630 .ioctl = idetape_ioctl,
2631};
2632
Russell King4031bbe2006-01-06 11:41:00 +00002633static int ide_tape_probe(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002634{
2635 idetape_tape_t *tape;
2636 struct gendisk *g;
2637 int minor;
2638
2639 if (!strstr("ide-tape", drive->driver_req))
2640 goto failed;
Bartlomiej Zolnierkiewicz2a924662008-10-10 22:39:24 +02002641
Linus Torvalds1da177e2005-04-16 15:20:36 -07002642 if (drive->media != ide_tape)
2643 goto failed;
Bartlomiej Zolnierkiewicz2a924662008-10-10 22:39:24 +02002644
Bartlomiej Zolnierkiewicz51509ee2008-10-10 22:39:34 +02002645 if (drive->id_read == 1 && !ide_check_atapi_device(drive, DRV_NAME)) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002646 printk(KERN_ERR "ide-tape: %s: not supported by this version of"
2647 " the driver\n", drive->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002648 goto failed;
2649 }
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002650 tape = kzalloc(sizeof(idetape_tape_t), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002651 if (tape == NULL) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002652 printk(KERN_ERR "ide-tape: %s: Can't allocate a tape struct\n",
2653 drive->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002654 goto failed;
2655 }
2656
2657 g = alloc_disk(1 << PARTN_BITS);
2658 if (!g)
2659 goto out_free_tape;
2660
2661 ide_init_disk(g, drive);
2662
Linus Torvalds1da177e2005-04-16 15:20:36 -07002663 kref_init(&tape->kref);
2664
2665 tape->drive = drive;
2666 tape->driver = &idetape_driver;
2667 tape->disk = g;
2668
2669 g->private_data = &tape->driver;
2670
2671 drive->driver_data = tape;
2672
Arjan van de Vencf8b8972006-03-23 03:00:45 -08002673 mutex_lock(&idetape_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002674 for (minor = 0; idetape_devs[minor]; minor++)
2675 ;
2676 idetape_devs[minor] = tape;
Arjan van de Vencf8b8972006-03-23 03:00:45 -08002677 mutex_unlock(&idetape_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002678
2679 idetape_setup(drive, tape, minor);
2680
Greg Kroah-Hartman6ecaaf92008-05-21 12:52:33 -07002681 device_create_drvdata(idetape_sysfs_class, &drive->gendev,
2682 MKDEV(IDETAPE_MAJOR, minor), NULL,
2683 "%s", tape->name);
2684 device_create_drvdata(idetape_sysfs_class, &drive->gendev,
2685 MKDEV(IDETAPE_MAJOR, minor + 128), NULL,
2686 "n%s", tape->name);
Will Dysond5dee802005-09-16 02:55:07 -07002687
Linus Torvalds1da177e2005-04-16 15:20:36 -07002688 g->fops = &idetape_block_ops;
2689 ide_register_region(g);
2690
2691 return 0;
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02002692
Linus Torvalds1da177e2005-04-16 15:20:36 -07002693out_free_tape:
2694 kfree(tape);
2695failed:
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02002696 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002697}
2698
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002699static void __exit idetape_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002700{
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02002701 driver_unregister(&idetape_driver.gen_driver);
Will Dysond5dee802005-09-16 02:55:07 -07002702 class_destroy(idetape_sysfs_class);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002703 unregister_chrdev(IDETAPE_MAJOR, "ht");
2704}
2705
Bartlomiej Zolnierkiewicz17514e82005-11-19 22:24:35 +01002706static int __init idetape_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002707{
Will Dysond5dee802005-09-16 02:55:07 -07002708 int error = 1;
2709 idetape_sysfs_class = class_create(THIS_MODULE, "ide_tape");
2710 if (IS_ERR(idetape_sysfs_class)) {
2711 idetape_sysfs_class = NULL;
2712 printk(KERN_ERR "Unable to create sysfs class for ide tapes\n");
2713 error = -EBUSY;
2714 goto out;
2715 }
2716
Linus Torvalds1da177e2005-04-16 15:20:36 -07002717 if (register_chrdev(IDETAPE_MAJOR, "ht", &idetape_fops)) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002718 printk(KERN_ERR "ide-tape: Failed to register chrdev"
2719 " interface\n");
Will Dysond5dee802005-09-16 02:55:07 -07002720 error = -EBUSY;
2721 goto out_free_class;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002722 }
Will Dysond5dee802005-09-16 02:55:07 -07002723
2724 error = driver_register(&idetape_driver.gen_driver);
2725 if (error)
2726 goto out_free_driver;
2727
2728 return 0;
2729
2730out_free_driver:
2731 driver_unregister(&idetape_driver.gen_driver);
2732out_free_class:
2733 class_destroy(idetape_sysfs_class);
2734out:
2735 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002736}
2737
Kay Sievers263756e2005-12-12 18:03:44 +01002738MODULE_ALIAS("ide:*m-tape*");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002739module_init(idetape_init);
2740module_exit(idetape_exit);
2741MODULE_ALIAS_CHARDEV_MAJOR(IDETAPE_MAJOR);
Borislav Petkov9c145762008-02-06 02:57:54 +01002742MODULE_DESCRIPTION("ATAPI Streaming TAPE Driver");
2743MODULE_LICENSE("GPL");