blob: 83014abf330f5eb9749182d25cff83692d0a7ab0 [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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100693 * Generate a new packet command request in front of the request queue, before
694 * the current request, so that it will be processed immediately, on the next
695 * pass through the driver. The function below is called from the request
696 * handling part of the driver (the "bottom" part). Safe storage for the request
697 * should be allocated with ide_tape_next_{pc,rq}_storage() prior to that.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100699 * Memory for those requests is pre-allocated at initialization time, and is
700 * limited to IDETAPE_PC_STACK requests. We assume that we have enough space for
701 * the maximum possible number of inter-dependent packet commands.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100703 * The higher level of the driver - The ioctl handler and the character device
704 * handling functions should queue request to the lower level part and wait for
705 * their completion using idetape_queue_pc_tail or idetape_queue_rw_tail.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 */
Borislav Petkovd236d742008-04-18 00:46:27 +0200707static void idetape_queue_pc_head(ide_drive_t *drive, struct ide_atapi_pc *pc,
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100708 struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709{
710 struct ide_tape_obj *tape = drive->driver_data;
711
Bartlomiej Zolnierkiewiczf025ffd2008-10-10 22:39:34 +0200712 blk_rq_init(NULL, rq);
713 rq->cmd_type = REQ_TYPE_SPECIAL;
Bartlomiej Zolnierkiewicze8a96aa2008-07-15 21:21:41 +0200714 rq->cmd_flags |= REQ_PREEMPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 rq->buffer = (char *) pc;
716 rq->rq_disk = tape->disk;
Borislav Petkov0014c752008-07-23 19:56:00 +0200717 memcpy(rq->cmd, pc->c, 12);
Bartlomiej Zolnierkiewiczf025ffd2008-10-10 22:39:34 +0200718 rq->cmd[13] = REQ_IDETAPE_PC1;
FUJITA Tomonori63f5abb2008-07-15 21:21:51 +0200719 ide_do_drive_cmd(drive, rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720}
721
722/*
723 * idetape_retry_pc is called when an error was detected during the
724 * last packet command. We queue a request sense packet command in
725 * the head of the request list.
726 */
Bartlomiej Zolnierkiewicz258ec412008-07-15 21:21:55 +0200727static void idetape_retry_pc(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728{
Borislav Petkovd236d742008-04-18 00:46:27 +0200729 struct ide_atapi_pc *pc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 struct request *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731
Bartlomiej Zolnierkiewicz64a57fe2008-02-06 02:57:51 +0100732 (void)ide_read_error(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 pc = idetape_next_pc_storage(drive);
734 rq = idetape_next_rq_storage(drive);
735 idetape_create_request_sense_cmd(pc);
Borislav Petkovf2e3ab52008-07-23 19:56:01 +0200736 set_bit(IDE_AFLAG_IGNORE_DSC, &drive->atapi_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737 idetape_queue_pc_head(drive, pc, rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738}
739
740/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100741 * Postpone the current request so that ide.c will be able to service requests
742 * from another device on the same hwgroup while we are polling for DSC.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743 */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100744static void idetape_postpone_request(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745{
746 idetape_tape_t *tape = drive->driver_data;
747
Borislav Petkov8004a8c2008-02-06 02:57:51 +0100748 debug_log(DBG_PROCS, "Enter %s\n", __func__);
749
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750 tape->postponed_rq = HWGROUP(drive)->rq;
Borislav Petkov54bb2072008-02-06 02:57:52 +0100751 ide_stall_queue(drive, tape->dsc_poll_freq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752}
753
Bartlomiej Zolnierkiewicz74e63e742008-07-15 21:22:01 +0200754static void ide_tape_handle_dsc(ide_drive_t *drive)
755{
756 idetape_tape_t *tape = drive->driver_data;
757
758 /* Media access command */
759 tape->dsc_polling_start = jiffies;
760 tape->dsc_poll_freq = IDETAPE_DSC_MA_FAST;
761 tape->dsc_timeout = jiffies + IDETAPE_DSC_MA_TIMEOUT;
762 /* Allow ide.c to handle other requests */
763 idetape_postpone_request(drive);
764}
765
Bartlomiej Zolnierkiewicz08424ac2008-07-15 21:22:01 +0200766static void ide_tape_io_buffers(ide_drive_t *drive, struct ide_atapi_pc *pc,
767 unsigned int bcount, int write)
768{
769 if (write)
770 idetape_output_buffers(drive, pc, bcount);
771 else
772 idetape_input_buffers(drive, pc, bcount);
773}
Borislav Petkova1efc852008-02-06 02:57:52 +0100774
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775/*
Borislav Petkova1efc852008-02-06 02:57:52 +0100776 * This is the usual interrupt handler which will be called during a packet
777 * command. We will transfer some of the data (as requested by the drive) and
778 * will re-point interrupt handler to us. When data transfer is finished, we
779 * will act according to the algorithm described before
Borislav Petkov8d06bfa2008-02-06 02:57:53 +0100780 * idetape_issue_pc.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781 */
Borislav Petkova1efc852008-02-06 02:57:52 +0100782static ide_startstop_t idetape_pc_intr(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784 idetape_tape_t *tape = drive->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785
Bartlomiej Zolnierkiewicz646c0cb2008-07-15 21:22:03 +0200786 return ide_pc_intr(drive, tape->pc, idetape_pc_intr, IDETAPE_WAIT_CMD,
787 NULL, idetape_update_buffers, idetape_retry_pc,
788 ide_tape_handle_dsc, ide_tape_io_buffers);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789}
790
791/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100792 * Packet Command Interface
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100794 * The current Packet Command is available in tape->pc, and will not change
795 * until we finish handling it. Each packet command is associated with a
796 * callback function that will be called when the command is finished.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100798 * The handling will be done in three stages:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100800 * 1. idetape_issue_pc will send the packet command to the drive, and will set
801 * the interrupt handler to idetape_pc_intr.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100803 * 2. On each interrupt, idetape_pc_intr will be called. This step will be
804 * repeated until the device signals us that no more interrupts will be issued.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100806 * 3. ATAPI Tape media access commands have immediate status with a delayed
807 * process. In case of a successful initiation of a media access packet command,
808 * the DSC bit will be set when the actual execution of the command is finished.
809 * Since the tape drive will not issue an interrupt, we have to poll for this
810 * event. In this case, we define the request as "low priority request" by
811 * setting rq_status to IDETAPE_RQ_POSTPONED, set a timer to poll for DSC and
812 * exit the driver.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100814 * ide.c will then give higher priority to requests which originate from the
815 * other device, until will change rq_status to RQ_ACTIVE.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100817 * 4. When the packet command is finished, it will be checked for errors.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100819 * 5. In case an error was found, we queue a request sense packet command in
820 * front of the request queue and retry the operation up to
821 * IDETAPE_MAX_PC_RETRIES times.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100823 * 6. In case no error was found, or we decided to give up and not to retry
824 * again, the callback function will be called and then we will handle the next
825 * request.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826 */
827static ide_startstop_t idetape_transfer_pc(ide_drive_t *drive)
828{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829 idetape_tape_t *tape = drive->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830
Bartlomiej Zolnierkiewicz594c16d2008-07-15 21:21:58 +0200831 return ide_transfer_pc(drive, tape->pc, idetape_pc_intr,
832 IDETAPE_WAIT_CMD, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833}
834
Borislav Petkovd236d742008-04-18 00:46:27 +0200835static ide_startstop_t idetape_issue_pc(ide_drive_t *drive,
836 struct ide_atapi_pc *pc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838 idetape_tape_t *tape = drive->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839
Borislav Petkov90699ce2008-02-02 19:56:50 +0100840 if (tape->pc->c[0] == REQUEST_SENSE &&
841 pc->c[0] == REQUEST_SENSE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842 printk(KERN_ERR "ide-tape: possible ide-tape.c bug - "
843 "Two request sense in serial were issued\n");
844 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845
Borislav Petkov90699ce2008-02-02 19:56:50 +0100846 if (tape->failed_pc == NULL && pc->c[0] != REQUEST_SENSE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847 tape->failed_pc = pc;
848 /* Set the current packet command */
849 tape->pc = pc;
850
851 if (pc->retries > IDETAPE_MAX_PC_RETRIES ||
Borislav Petkov346331f2008-04-18 00:46:26 +0200852 (pc->flags & PC_FLAG_ABORT)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853 /*
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100854 * We will "abort" retrying a packet command in case legitimate
855 * error code was received (crossing a filemark, or end of the
856 * media, for example).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857 */
Borislav Petkov346331f2008-04-18 00:46:26 +0200858 if (!(pc->flags & PC_FLAG_ABORT)) {
Borislav Petkov90699ce2008-02-02 19:56:50 +0100859 if (!(pc->c[0] == TEST_UNIT_READY &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860 tape->sense_key == 2 && tape->asc == 4 &&
861 (tape->ascq == 1 || tape->ascq == 8))) {
862 printk(KERN_ERR "ide-tape: %s: I/O error, "
863 "pc = %2x, key = %2x, "
864 "asc = %2x, ascq = %2x\n",
865 tape->name, pc->c[0],
866 tape->sense_key, tape->asc,
867 tape->ascq);
868 }
869 /* Giving up */
870 pc->error = IDETAPE_ERROR_GENERAL;
871 }
872 tape->failed_pc = NULL;
Borislav Petkov776bb022008-07-23 19:55:59 +0200873 drive->pc_callback(drive);
Bartlomiej Zolnierkiewicz92f5daf2008-07-15 21:21:55 +0200874 return ide_stopped;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875 }
Borislav Petkov8004a8c2008-02-06 02:57:51 +0100876 debug_log(DBG_SENSE, "Retry #%d, cmd = %02X\n", pc->retries, pc->c[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877
878 pc->retries++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879
Bartlomiej Zolnierkiewicz6bf16412008-07-15 21:22:00 +0200880 return ide_issue_pc(drive, pc, idetape_transfer_pc,
881 IDETAPE_WAIT_CMD, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882}
883
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100884/* A mode sense command is used to "sense" tape parameters. */
Borislav Petkovd236d742008-04-18 00:46:27 +0200885static void idetape_create_mode_sense_cmd(struct ide_atapi_pc *pc, u8 page_code)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886{
887 idetape_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +0100888 pc->c[0] = MODE_SENSE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889 if (page_code != IDETAPE_BLOCK_DESCRIPTOR)
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100890 /* DBD = 1 - Don't return block descriptors */
891 pc->c[1] = 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892 pc->c[2] = page_code;
893 /*
894 * Changed pc->c[3] to 0 (255 will at best return unused info).
895 *
896 * For SCSI this byte is defined as subpage instead of high byte
897 * of length and some IDE drives seem to interpret it this way
898 * and return an error when 255 is used.
899 */
900 pc->c[3] = 0;
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100901 /* We will just discard data in that case */
902 pc->c[4] = 255;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903 if (page_code == IDETAPE_BLOCK_DESCRIPTOR)
Borislav Petkovd236d742008-04-18 00:46:27 +0200904 pc->req_xfer = 12;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905 else if (page_code == IDETAPE_CAPABILITIES_PAGE)
Borislav Petkovd236d742008-04-18 00:46:27 +0200906 pc->req_xfer = 24;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907 else
Borislav Petkovd236d742008-04-18 00:46:27 +0200908 pc->req_xfer = 50;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909}
910
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100911static ide_startstop_t idetape_media_access_finished(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912{
Bartlomiej Zolnierkiewiczb73c7ee2008-07-23 19:55:52 +0200913 ide_hwif_t *hwif = drive->hwif;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914 idetape_tape_t *tape = drive->driver_data;
Borislav Petkovd236d742008-04-18 00:46:27 +0200915 struct ide_atapi_pc *pc = tape->pc;
Bartlomiej Zolnierkiewicz22c525b2008-01-25 22:17:11 +0100916 u8 stat;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917
Bartlomiej Zolnierkiewicz374e0422008-07-23 19:55:56 +0200918 stat = hwif->tp_ops->read_status(hwif);
Bartlomiej Zolnierkiewiczc47137a2008-02-06 02:57:51 +0100919
Bartlomiej Zolnierkiewicz3a7d2482008-10-10 22:39:21 +0200920 if (stat & ATA_DSC) {
921 if (stat & ATA_ERR) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922 /* Error detected */
Borislav Petkov90699ce2008-02-02 19:56:50 +0100923 if (pc->c[0] != TEST_UNIT_READY)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924 printk(KERN_ERR "ide-tape: %s: I/O error, ",
925 tape->name);
926 /* Retry operation */
Bartlomiej Zolnierkiewicz258ec412008-07-15 21:21:55 +0200927 idetape_retry_pc(drive);
928 return ide_stopped;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929 }
930 pc->error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931 } else {
932 pc->error = IDETAPE_ERROR_GENERAL;
933 tape->failed_pc = NULL;
934 }
Borislav Petkov776bb022008-07-23 19:55:59 +0200935 drive->pc_callback(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936 return ide_stopped;
937}
938
Borislav Petkovcd2abbf2008-07-15 21:22:03 +0200939static void ide_tape_create_rw_cmd(idetape_tape_t *tape,
Borislav Petkov0014c752008-07-23 19:56:00 +0200940 struct ide_atapi_pc *pc, struct request *rq,
941 u8 opcode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942{
Borislav Petkov0014c752008-07-23 19:56:00 +0200943 struct idetape_bh *bh = (struct idetape_bh *)rq->special;
944 unsigned int length = rq->current_nr_sectors;
945
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946 idetape_init_pc(pc);
Borislav Petkov860ff5e2008-02-02 19:56:50 +0100947 put_unaligned(cpu_to_be32(length), (unsigned int *) &pc->c[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948 pc->c[1] = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949 pc->bh = bh;
Borislav Petkovd236d742008-04-18 00:46:27 +0200950 pc->buf = NULL;
951 pc->buf_size = length * tape->blk_size;
952 pc->req_xfer = pc->buf_size;
Borislav Petkovf73850a2008-04-27 15:38:33 +0200953 if (pc->req_xfer == tape->buffer_size)
Bartlomiej Zolnierkiewicz5e331092008-07-15 21:21:56 +0200954 pc->flags |= PC_FLAG_DMA_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955
Borislav Petkovcd2abbf2008-07-15 21:22:03 +0200956 if (opcode == READ_6) {
957 pc->c[0] = READ_6;
958 atomic_set(&bh->b_count, 0);
959 } else if (opcode == WRITE_6) {
960 pc->c[0] = WRITE_6;
961 pc->flags |= PC_FLAG_WRITING;
962 pc->b_data = bh->b_data;
963 pc->b_count = atomic_read(&bh->b_count);
964 }
Borislav Petkov0014c752008-07-23 19:56:00 +0200965
966 memcpy(rq->cmd, pc->c, 12);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967}
968
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969static ide_startstop_t idetape_do_request(ide_drive_t *drive,
970 struct request *rq, sector_t block)
971{
Bartlomiej Zolnierkiewiczb73c7ee2008-07-23 19:55:52 +0200972 ide_hwif_t *hwif = drive->hwif;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973 idetape_tape_t *tape = drive->driver_data;
Borislav Petkovd236d742008-04-18 00:46:27 +0200974 struct ide_atapi_pc *pc = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975 struct request *postponed_rq = tape->postponed_rq;
Bartlomiej Zolnierkiewicz22c525b2008-01-25 22:17:11 +0100976 u8 stat;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977
Mark de Wever730616b2008-10-10 22:39:17 +0200978 debug_log(DBG_SENSE, "sector: %llu, nr_sectors: %lu,"
979 " current_nr_sectors: %u\n",
980 (unsigned long long)rq->sector, rq->nr_sectors,
981 rq->current_nr_sectors);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982
Jens Axboe4aff5e22006-08-10 08:44:47 +0200983 if (!blk_special_request(rq)) {
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100984 /* We do not support buffer cache originated requests. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985 printk(KERN_NOTICE "ide-tape: %s: Unsupported request in "
Jens Axboe4aff5e22006-08-10 08:44:47 +0200986 "request queue (%d)\n", drive->name, rq->cmd_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987 ide_end_request(drive, 0, 0);
988 return ide_stopped;
989 }
990
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100991 /* Retry a failed packet command */
Bartlomiej Zolnierkiewicz28c72142008-07-15 21:21:59 +0200992 if (tape->failed_pc && tape->pc->c[0] == REQUEST_SENSE) {
993 pc = tape->failed_pc;
994 goto out;
995 }
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100996
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997 if (postponed_rq != NULL)
998 if (rq != postponed_rq) {
999 printk(KERN_ERR "ide-tape: ide-tape.c bug - "
1000 "Two DSC requests were queued\n");
1001 idetape_end_request(drive, 0, 0);
1002 return ide_stopped;
1003 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004
1005 tape->postponed_rq = NULL;
1006
1007 /*
1008 * If the tape is still busy, postpone our request and service
1009 * the other device meanwhile.
1010 */
Bartlomiej Zolnierkiewicz374e0422008-07-23 19:55:56 +02001011 stat = hwif->tp_ops->read_status(hwif);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012
Borislav Petkov83dd5732008-07-23 19:56:00 +02001013 if (!drive->dsc_overlap && !(rq->cmd[13] & REQ_IDETAPE_PC2))
Borislav Petkovf2e3ab52008-07-23 19:56:01 +02001014 set_bit(IDE_AFLAG_IGNORE_DSC, &drive->atapi_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015
1016 if (drive->post_reset == 1) {
Borislav Petkovf2e3ab52008-07-23 19:56:01 +02001017 set_bit(IDE_AFLAG_IGNORE_DSC, &drive->atapi_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018 drive->post_reset = 0;
1019 }
1020
Borislav Petkovf2e3ab52008-07-23 19:56:01 +02001021 if (!test_and_clear_bit(IDE_AFLAG_IGNORE_DSC, &drive->atapi_flags) &&
Bartlomiej Zolnierkiewicz3a7d2482008-10-10 22:39:21 +02001022 (stat & ATA_DSC) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023 if (postponed_rq == NULL) {
1024 tape->dsc_polling_start = jiffies;
Borislav Petkov54bb2072008-02-06 02:57:52 +01001025 tape->dsc_poll_freq = tape->best_dsc_rw_freq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026 tape->dsc_timeout = jiffies + IDETAPE_DSC_RW_TIMEOUT;
1027 } else if (time_after(jiffies, tape->dsc_timeout)) {
1028 printk(KERN_ERR "ide-tape: %s: DSC timeout\n",
1029 tape->name);
Borislav Petkov83dd5732008-07-23 19:56:00 +02001030 if (rq->cmd[13] & REQ_IDETAPE_PC2) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031 idetape_media_access_finished(drive);
1032 return ide_stopped;
1033 } else {
1034 return ide_do_reset(drive);
1035 }
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001036 } else if (time_after(jiffies,
1037 tape->dsc_polling_start +
1038 IDETAPE_DSC_MA_THRESHOLD))
Borislav Petkov54bb2072008-02-06 02:57:52 +01001039 tape->dsc_poll_freq = IDETAPE_DSC_MA_SLOW;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040 idetape_postpone_request(drive);
1041 return ide_stopped;
1042 }
Borislav Petkov83dd5732008-07-23 19:56:00 +02001043 if (rq->cmd[13] & REQ_IDETAPE_READ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044 pc = idetape_next_pc_storage(drive);
Borislav Petkov0014c752008-07-23 19:56:00 +02001045 ide_tape_create_rw_cmd(tape, pc, rq, READ_6);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046 goto out;
1047 }
Borislav Petkov83dd5732008-07-23 19:56:00 +02001048 if (rq->cmd[13] & REQ_IDETAPE_WRITE) {
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, WRITE_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_PC1) {
Borislav Petkovd236d742008-04-18 00:46:27 +02001054 pc = (struct ide_atapi_pc *) rq->buffer;
Borislav Petkov83dd5732008-07-23 19:56:00 +02001055 rq->cmd[13] &= ~(REQ_IDETAPE_PC1);
1056 rq->cmd[13] |= REQ_IDETAPE_PC2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057 goto out;
1058 }
Borislav Petkov83dd5732008-07-23 19:56:00 +02001059 if (rq->cmd[13] & REQ_IDETAPE_PC2) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060 idetape_media_access_finished(drive);
1061 return ide_stopped;
1062 }
1063 BUG();
Bartlomiej Zolnierkiewicz28c72142008-07-15 21:21:59 +02001064
Borislav Petkovf2e3ab52008-07-23 19:56:01 +02001065out:
Borislav Petkov8d06bfa2008-02-06 02:57:53 +01001066 return idetape_issue_pc(drive, pc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067}
1068
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069/*
Borislav Petkov41aa1702008-04-27 15:38:32 +02001070 * The function below uses __get_free_pages to allocate a data buffer of size
Borislav Petkovf73850a2008-04-27 15:38:33 +02001071 * tape->buffer_size (or a bit more). We attempt to combine sequential pages as
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001072 * much as possible.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073 *
Borislav Petkov41aa1702008-04-27 15:38:32 +02001074 * It returns a pointer to the newly allocated buffer, or NULL in case of
1075 * failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076 */
Borislav Petkov077e3bd2008-04-27 15:38:34 +02001077static struct idetape_bh *ide_tape_kmalloc_buffer(idetape_tape_t *tape,
1078 int full, int clear)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079{
Borislav Petkov077e3bd2008-04-27 15:38:34 +02001080 struct idetape_bh *prev_bh, *bh, *merge_bh;
Borislav Petkova997a432008-04-27 15:38:33 +02001081 int pages = tape->pages_per_buffer;
Borislav Petkov41aa1702008-04-27 15:38:32 +02001082 unsigned int order, b_allocd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083 char *b_data = NULL;
1084
Borislav Petkov077e3bd2008-04-27 15:38:34 +02001085 merge_bh = kmalloc(sizeof(struct idetape_bh), GFP_KERNEL);
1086 bh = merge_bh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087 if (bh == NULL)
1088 goto abort;
Borislav Petkov41aa1702008-04-27 15:38:32 +02001089
1090 order = fls(pages) - 1;
1091 bh->b_data = (char *) __get_free_pages(GFP_KERNEL, order);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001092 if (!bh->b_data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093 goto abort;
Borislav Petkov41aa1702008-04-27 15:38:32 +02001094 b_allocd = (1 << order) * PAGE_SIZE;
1095 pages &= (order-1);
1096
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097 if (clear)
Borislav Petkov41aa1702008-04-27 15:38:32 +02001098 memset(bh->b_data, 0, b_allocd);
1099 bh->b_reqnext = NULL;
1100 bh->b_size = b_allocd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101 atomic_set(&bh->b_count, full ? bh->b_size : 0);
1102
Borislav Petkov41aa1702008-04-27 15:38:32 +02001103 while (pages) {
1104 order = fls(pages) - 1;
1105 b_data = (char *) __get_free_pages(GFP_KERNEL, order);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001106 if (!b_data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107 goto abort;
Borislav Petkov41aa1702008-04-27 15:38:32 +02001108 b_allocd = (1 << order) * PAGE_SIZE;
1109
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110 if (clear)
Borislav Petkov41aa1702008-04-27 15:38:32 +02001111 memset(b_data, 0, b_allocd);
1112
1113 /* newly allocated page frames below buffer header or ...*/
1114 if (bh->b_data == b_data + b_allocd) {
1115 bh->b_size += b_allocd;
1116 bh->b_data -= b_allocd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117 if (full)
Borislav Petkov41aa1702008-04-27 15:38:32 +02001118 atomic_add(b_allocd, &bh->b_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119 continue;
1120 }
Borislav Petkov41aa1702008-04-27 15:38:32 +02001121 /* they are above the header */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122 if (b_data == bh->b_data + bh->b_size) {
Borislav Petkov41aa1702008-04-27 15:38:32 +02001123 bh->b_size += b_allocd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124 if (full)
Borislav Petkov41aa1702008-04-27 15:38:32 +02001125 atomic_add(b_allocd, &bh->b_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126 continue;
1127 }
1128 prev_bh = bh;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001129 bh = kmalloc(sizeof(struct idetape_bh), GFP_KERNEL);
1130 if (!bh) {
Borislav Petkov41aa1702008-04-27 15:38:32 +02001131 free_pages((unsigned long) b_data, order);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132 goto abort;
1133 }
1134 bh->b_reqnext = NULL;
1135 bh->b_data = b_data;
Borislav Petkov41aa1702008-04-27 15:38:32 +02001136 bh->b_size = b_allocd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137 atomic_set(&bh->b_count, full ? bh->b_size : 0);
1138 prev_bh->b_reqnext = bh;
Borislav Petkov41aa1702008-04-27 15:38:32 +02001139
1140 pages &= (order-1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141 }
Borislav Petkov41aa1702008-04-27 15:38:32 +02001142
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143 bh->b_size -= tape->excess_bh_size;
1144 if (full)
1145 atomic_sub(tape->excess_bh_size, &bh->b_count);
Borislav Petkov077e3bd2008-04-27 15:38:34 +02001146 return merge_bh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147abort:
Borislav Petkov077e3bd2008-04-27 15:38:34 +02001148 ide_tape_kfree_buffer(tape);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149 return NULL;
1150}
1151
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001152static int idetape_copy_stage_from_user(idetape_tape_t *tape,
Borislav Petkov8646c882008-04-27 15:38:26 +02001153 const char __user *buf, int n)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154{
1155 struct idetape_bh *bh = tape->bh;
1156 int count;
Daniel Walkerdcd96372006-06-25 05:47:37 -07001157 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158
1159 while (n) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160 if (bh == NULL) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001161 printk(KERN_ERR "ide-tape: bh == NULL in %s\n",
1162 __func__);
Daniel Walkerdcd96372006-06-25 05:47:37 -07001163 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164 }
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001165 count = min((unsigned int)
1166 (bh->b_size - atomic_read(&bh->b_count)),
1167 (unsigned int)n);
1168 if (copy_from_user(bh->b_data + atomic_read(&bh->b_count), buf,
1169 count))
Daniel Walkerdcd96372006-06-25 05:47:37 -07001170 ret = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171 n -= count;
1172 atomic_add(count, &bh->b_count);
1173 buf += count;
1174 if (atomic_read(&bh->b_count) == bh->b_size) {
1175 bh = bh->b_reqnext;
1176 if (bh)
1177 atomic_set(&bh->b_count, 0);
1178 }
1179 }
1180 tape->bh = bh;
Daniel Walkerdcd96372006-06-25 05:47:37 -07001181 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182}
1183
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001184static int idetape_copy_stage_to_user(idetape_tape_t *tape, char __user *buf,
Borislav Petkov99d74e62008-04-27 15:38:25 +02001185 int n)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186{
1187 struct idetape_bh *bh = tape->bh;
1188 int count;
Daniel Walkerdcd96372006-06-25 05:47:37 -07001189 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190
1191 while (n) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192 if (bh == NULL) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001193 printk(KERN_ERR "ide-tape: bh == NULL in %s\n",
1194 __func__);
Daniel Walkerdcd96372006-06-25 05:47:37 -07001195 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197 count = min(tape->b_count, n);
Daniel Walkerdcd96372006-06-25 05:47:37 -07001198 if (copy_to_user(buf, tape->b_data, count))
1199 ret = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200 n -= count;
1201 tape->b_data += count;
1202 tape->b_count -= count;
1203 buf += count;
1204 if (!tape->b_count) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001205 bh = bh->b_reqnext;
1206 tape->bh = bh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207 if (bh) {
1208 tape->b_data = bh->b_data;
1209 tape->b_count = atomic_read(&bh->b_count);
1210 }
1211 }
1212 }
Daniel Walkerdcd96372006-06-25 05:47:37 -07001213 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214}
1215
Borislav Petkov077e3bd2008-04-27 15:38:34 +02001216static void idetape_init_merge_buffer(idetape_tape_t *tape)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217{
Borislav Petkov077e3bd2008-04-27 15:38:34 +02001218 struct idetape_bh *bh = tape->merge_bh;
1219 tape->bh = tape->merge_bh;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001220
Borislav Petkov54abf372008-02-06 02:57:52 +01001221 if (tape->chrdev_dir == IDETAPE_DIR_WRITE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222 atomic_set(&bh->b_count, 0);
1223 else {
1224 tape->b_data = bh->b_data;
1225 tape->b_count = atomic_read(&bh->b_count);
1226 }
1227}
1228
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001230 * Write a filemark if write_filemark=1. Flush the device buffers without
1231 * writing a filemark otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232 */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001233static void idetape_create_write_filemark_cmd(ide_drive_t *drive,
Borislav Petkovd236d742008-04-18 00:46:27 +02001234 struct ide_atapi_pc *pc, int write_filemark)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235{
1236 idetape_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +01001237 pc->c[0] = WRITE_FILEMARKS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238 pc->c[4] = write_filemark;
Borislav Petkov346331f2008-04-18 00:46:26 +02001239 pc->flags |= PC_FLAG_WAIT_FOR_DSC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240}
1241
Borislav Petkovd236d742008-04-18 00:46:27 +02001242static void idetape_create_test_unit_ready_cmd(struct ide_atapi_pc *pc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243{
1244 idetape_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +01001245 pc->c[0] = TEST_UNIT_READY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246}
1247
1248/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001249 * We add a special packet command request to the tail of the request queue, and
1250 * wait for it to be serviced. This is not to be called from within the request
1251 * handling part of the driver! We allocate here data on the stack and it is
1252 * valid until the request is finished. This is not the case for the bottom part
1253 * of the driver, where we are always leaving the functions to wait for an
1254 * interrupt or a timer event.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001256 * From the bottom part of the driver, we should allocate safe memory using
1257 * idetape_next_pc_storage() and ide_tape_next_rq_storage(), and add the request
1258 * to the request list without waiting for it to be serviced! In that case, we
1259 * usually use idetape_queue_pc_head().
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260 */
Borislav Petkovea1ab3d2008-04-27 15:38:27 +02001261static int idetape_queue_pc_tail(ide_drive_t *drive, struct ide_atapi_pc *pc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262{
1263 struct ide_tape_obj *tape = drive->driver_data;
FUJITA Tomonori64ea1b42008-07-15 21:21:43 +02001264 struct request *rq;
1265 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266
FUJITA Tomonori64ea1b42008-07-15 21:21:43 +02001267 rq = blk_get_request(drive->queue, READ, __GFP_WAIT);
1268 rq->cmd_type = REQ_TYPE_SPECIAL;
Borislav Petkov83dd5732008-07-23 19:56:00 +02001269 rq->cmd[13] = REQ_IDETAPE_PC1;
FUJITA Tomonori64ea1b42008-07-15 21:21:43 +02001270 rq->buffer = (char *)pc;
Borislav Petkov0014c752008-07-23 19:56:00 +02001271 memcpy(rq->cmd, pc->c, 12);
FUJITA Tomonori64ea1b42008-07-15 21:21:43 +02001272 error = blk_execute_rq(drive->queue, tape->disk, rq, 0);
1273 blk_put_request(rq);
1274 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275}
1276
Borislav Petkovd236d742008-04-18 00:46:27 +02001277static void idetape_create_load_unload_cmd(ide_drive_t *drive,
1278 struct ide_atapi_pc *pc, int cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279{
1280 idetape_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +01001281 pc->c[0] = START_STOP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282 pc->c[4] = cmd;
Borislav Petkov346331f2008-04-18 00:46:26 +02001283 pc->flags |= PC_FLAG_WAIT_FOR_DSC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284}
1285
1286static int idetape_wait_ready(ide_drive_t *drive, unsigned long timeout)
1287{
1288 idetape_tape_t *tape = drive->driver_data;
Borislav Petkovd236d742008-04-18 00:46:27 +02001289 struct ide_atapi_pc pc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290 int load_attempted = 0;
1291
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001292 /* Wait for the tape to become ready */
Borislav Petkovf2e3ab52008-07-23 19:56:01 +02001293 set_bit(IDE_AFLAG_MEDIUM_PRESENT, &drive->atapi_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294 timeout += jiffies;
1295 while (time_before(jiffies, timeout)) {
1296 idetape_create_test_unit_ready_cmd(&pc);
Borislav Petkovea1ab3d2008-04-27 15:38:27 +02001297 if (!idetape_queue_pc_tail(drive, &pc))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298 return 0;
1299 if ((tape->sense_key == 2 && tape->asc == 4 && tape->ascq == 2)
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001300 || (tape->asc == 0x3A)) {
1301 /* no media */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302 if (load_attempted)
1303 return -ENOMEDIUM;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001304 idetape_create_load_unload_cmd(drive, &pc,
1305 IDETAPE_LU_LOAD_MASK);
Borislav Petkovea1ab3d2008-04-27 15:38:27 +02001306 idetape_queue_pc_tail(drive, &pc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307 load_attempted = 1;
1308 /* not about to be ready */
1309 } else if (!(tape->sense_key == 2 && tape->asc == 4 &&
1310 (tape->ascq == 1 || tape->ascq == 8)))
1311 return -EIO;
Nishanth Aravamudan80ce45f2005-09-10 00:27:08 -07001312 msleep(100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313 }
1314 return -EIO;
1315}
1316
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001317static int idetape_flush_tape_buffers(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318{
Borislav Petkovd236d742008-04-18 00:46:27 +02001319 struct ide_atapi_pc pc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320 int rc;
1321
1322 idetape_create_write_filemark_cmd(drive, &pc, 0);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001323 rc = idetape_queue_pc_tail(drive, &pc);
1324 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325 return rc;
1326 idetape_wait_ready(drive, 60 * 5 * HZ);
1327 return 0;
1328}
1329
Borislav Petkovd236d742008-04-18 00:46:27 +02001330static void idetape_create_read_position_cmd(struct ide_atapi_pc *pc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331{
1332 idetape_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +01001333 pc->c[0] = READ_POSITION;
Borislav Petkovd236d742008-04-18 00:46:27 +02001334 pc->req_xfer = 20;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335}
1336
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001337static int idetape_read_position(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338{
1339 idetape_tape_t *tape = drive->driver_data;
Borislav Petkovd236d742008-04-18 00:46:27 +02001340 struct ide_atapi_pc pc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341 int position;
1342
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001343 debug_log(DBG_PROCS, "Enter %s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344
1345 idetape_create_read_position_cmd(&pc);
1346 if (idetape_queue_pc_tail(drive, &pc))
1347 return -1;
Borislav Petkov54bb2072008-02-06 02:57:52 +01001348 position = tape->first_frame;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349 return position;
1350}
1351
Borislav Petkovd236d742008-04-18 00:46:27 +02001352static void idetape_create_locate_cmd(ide_drive_t *drive,
1353 struct ide_atapi_pc *pc,
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001354 unsigned int block, u8 partition, int skip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355{
1356 idetape_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +01001357 pc->c[0] = POSITION_TO_ELEMENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358 pc->c[1] = 2;
Borislav Petkov860ff5e2008-02-02 19:56:50 +01001359 put_unaligned(cpu_to_be32(block), (unsigned int *) &pc->c[3]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360 pc->c[8] = partition;
Borislav Petkov346331f2008-04-18 00:46:26 +02001361 pc->flags |= PC_FLAG_WAIT_FOR_DSC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362}
1363
Borislav Petkovd236d742008-04-18 00:46:27 +02001364static int idetape_create_prevent_cmd(ide_drive_t *drive,
1365 struct ide_atapi_pc *pc, int prevent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366{
1367 idetape_tape_t *tape = drive->driver_data;
1368
Borislav Petkovb6422012008-02-02 19:56:49 +01001369 /* device supports locking according to capabilities page */
1370 if (!(tape->caps[6] & 0x01))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371 return 0;
1372
1373 idetape_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +01001374 pc->c[0] = ALLOW_MEDIUM_REMOVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375 pc->c[4] = prevent;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376 return 1;
1377}
1378
Borislav Petkovec0fdb02008-04-27 15:38:34 +02001379static void __ide_tape_discard_merge_buffer(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380{
1381 idetape_tape_t *tape = drive->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382
Borislav Petkov54abf372008-02-06 02:57:52 +01001383 if (tape->chrdev_dir != IDETAPE_DIR_READ)
Borislav Petkov97986302008-04-27 15:38:34 +02001384 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385
Borislav Petkovf2e3ab52008-07-23 19:56:01 +02001386 clear_bit(IDE_AFLAG_FILEMARK, &drive->atapi_flags);
Borislav Petkov01a63aeb2008-04-27 15:38:34 +02001387 tape->merge_bh_size = 0;
Borislav Petkov077e3bd2008-04-27 15:38:34 +02001388 if (tape->merge_bh != NULL) {
1389 ide_tape_kfree_buffer(tape);
1390 tape->merge_bh = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391 }
1392
Borislav Petkov54abf372008-02-06 02:57:52 +01001393 tape->chrdev_dir = IDETAPE_DIR_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394}
1395
1396/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001397 * Position the tape to the requested block using the LOCATE packet command.
1398 * A READ POSITION command is then issued to check where we are positioned. Like
1399 * all higher level operations, we queue the commands at the tail of the request
1400 * queue and wait for their completion.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401 */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001402static int idetape_position_tape(ide_drive_t *drive, unsigned int block,
1403 u8 partition, int skip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404{
1405 idetape_tape_t *tape = drive->driver_data;
1406 int retval;
Borislav Petkovd236d742008-04-18 00:46:27 +02001407 struct ide_atapi_pc pc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408
Borislav Petkov54abf372008-02-06 02:57:52 +01001409 if (tape->chrdev_dir == IDETAPE_DIR_READ)
Borislav Petkovec0fdb02008-04-27 15:38:34 +02001410 __ide_tape_discard_merge_buffer(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411 idetape_wait_ready(drive, 60 * 5 * HZ);
1412 idetape_create_locate_cmd(drive, &pc, block, partition, skip);
1413 retval = idetape_queue_pc_tail(drive, &pc);
1414 if (retval)
1415 return (retval);
1416
1417 idetape_create_read_position_cmd(&pc);
1418 return (idetape_queue_pc_tail(drive, &pc));
1419}
1420
Borislav Petkovec0fdb02008-04-27 15:38:34 +02001421static void ide_tape_discard_merge_buffer(ide_drive_t *drive,
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001422 int restore_position)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423{
1424 idetape_tape_t *tape = drive->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425 int seek, position;
1426
Borislav Petkovec0fdb02008-04-27 15:38:34 +02001427 __ide_tape_discard_merge_buffer(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428 if (restore_position) {
1429 position = idetape_read_position(drive);
Borislav Petkov97986302008-04-27 15:38:34 +02001430 seek = position > 0 ? position : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431 if (idetape_position_tape(drive, seek, 0, 0)) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001432 printk(KERN_INFO "ide-tape: %s: position_tape failed in"
Borislav Petkovec0fdb02008-04-27 15:38:34 +02001433 " %s\n", tape->name, __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434 return;
1435 }
1436 }
1437}
1438
1439/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001440 * Generate a read/write request for the block device interface and wait for it
1441 * to be serviced.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442 */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001443static int idetape_queue_rw_tail(ide_drive_t *drive, int cmd, int blocks,
1444 struct idetape_bh *bh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445{
1446 idetape_tape_t *tape = drive->driver_data;
FUJITA Tomonori64ea1b42008-07-15 21:21:43 +02001447 struct request *rq;
1448 int ret, errors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001450 debug_log(DBG_SENSE, "%s: cmd=%d\n", __func__, cmd);
1451
FUJITA Tomonori64ea1b42008-07-15 21:21:43 +02001452 rq = blk_get_request(drive->queue, READ, __GFP_WAIT);
1453 rq->cmd_type = REQ_TYPE_SPECIAL;
Borislav Petkov83dd5732008-07-23 19:56:00 +02001454 rq->cmd[13] = cmd;
FUJITA Tomonori64ea1b42008-07-15 21:21:43 +02001455 rq->rq_disk = tape->disk;
1456 rq->special = (void *)bh;
1457 rq->sector = tape->first_frame;
1458 rq->nr_sectors = blocks;
1459 rq->current_nr_sectors = blocks;
1460 blk_execute_rq(drive->queue, tape->disk, rq, 0);
1461
1462 errors = rq->errors;
1463 ret = tape->blk_size * (blocks - rq->current_nr_sectors);
1464 blk_put_request(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465
1466 if ((cmd & (REQ_IDETAPE_READ | REQ_IDETAPE_WRITE)) == 0)
1467 return 0;
1468
Borislav Petkov077e3bd2008-04-27 15:38:34 +02001469 if (tape->merge_bh)
1470 idetape_init_merge_buffer(tape);
FUJITA Tomonori64ea1b42008-07-15 21:21:43 +02001471 if (errors == IDETAPE_ERROR_GENERAL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472 return -EIO;
FUJITA Tomonori64ea1b42008-07-15 21:21:43 +02001473 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474}
1475
Borislav Petkovd236d742008-04-18 00:46:27 +02001476static void idetape_create_inquiry_cmd(struct ide_atapi_pc *pc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477{
1478 idetape_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +01001479 pc->c[0] = INQUIRY;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001480 pc->c[4] = 254;
Borislav Petkovd236d742008-04-18 00:46:27 +02001481 pc->req_xfer = 254;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482}
1483
Borislav Petkovd236d742008-04-18 00:46:27 +02001484static void idetape_create_rewind_cmd(ide_drive_t *drive,
1485 struct ide_atapi_pc *pc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486{
1487 idetape_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +01001488 pc->c[0] = REZERO_UNIT;
Borislav Petkov346331f2008-04-18 00:46:26 +02001489 pc->flags |= PC_FLAG_WAIT_FOR_DSC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490}
1491
Borislav Petkovd236d742008-04-18 00:46:27 +02001492static void idetape_create_erase_cmd(struct ide_atapi_pc *pc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493{
1494 idetape_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +01001495 pc->c[0] = ERASE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496 pc->c[1] = 1;
Borislav Petkov346331f2008-04-18 00:46:26 +02001497 pc->flags |= PC_FLAG_WAIT_FOR_DSC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498}
1499
Borislav Petkovd236d742008-04-18 00:46:27 +02001500static void idetape_create_space_cmd(struct ide_atapi_pc *pc, int count, u8 cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501{
1502 idetape_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +01001503 pc->c[0] = SPACE;
Borislav Petkov860ff5e2008-02-02 19:56:50 +01001504 put_unaligned(cpu_to_be32(count), (unsigned int *) &pc->c[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505 pc->c[1] = cmd;
Borislav Petkov346331f2008-04-18 00:46:26 +02001506 pc->flags |= PC_FLAG_WAIT_FOR_DSC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507}
1508
Borislav Petkov97c566c2008-04-27 15:38:25 +02001509/* Queue up a character device originated write request. */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001510static int idetape_add_chrdev_write_request(ide_drive_t *drive, int blocks)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511{
1512 idetape_tape_t *tape = drive->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001514 debug_log(DBG_CHRDEV, "Enter %s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515
Borislav Petkov0aa4b012008-04-27 15:38:27 +02001516 return idetape_queue_rw_tail(drive, REQ_IDETAPE_WRITE,
Borislav Petkov077e3bd2008-04-27 15:38:34 +02001517 blocks, tape->merge_bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518}
1519
Borislav Petkovd9df9372008-04-27 15:38:34 +02001520static void ide_tape_flush_merge_buffer(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521{
1522 idetape_tape_t *tape = drive->driver_data;
1523 int blocks, min;
1524 struct idetape_bh *bh;
Borislav Petkov55a5d292008-02-02 19:56:49 +01001525
Borislav Petkov54abf372008-02-06 02:57:52 +01001526 if (tape->chrdev_dir != IDETAPE_DIR_WRITE) {
Borislav Petkov077e3bd2008-04-27 15:38:34 +02001527 printk(KERN_ERR "ide-tape: bug: Trying to empty merge buffer"
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001528 " but we are not writing.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529 return;
1530 }
Borislav Petkov01a63aeb2008-04-27 15:38:34 +02001531 if (tape->merge_bh_size > tape->buffer_size) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532 printk(KERN_ERR "ide-tape: bug: merge_buffer too big\n");
Borislav Petkov01a63aeb2008-04-27 15:38:34 +02001533 tape->merge_bh_size = tape->buffer_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534 }
Borislav Petkov01a63aeb2008-04-27 15:38:34 +02001535 if (tape->merge_bh_size) {
1536 blocks = tape->merge_bh_size / tape->blk_size;
1537 if (tape->merge_bh_size % tape->blk_size) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001538 unsigned int i;
1539
1540 blocks++;
Borislav Petkov01a63aeb2008-04-27 15:38:34 +02001541 i = tape->blk_size - tape->merge_bh_size %
Borislav Petkov54bb2072008-02-06 02:57:52 +01001542 tape->blk_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001543 bh = tape->bh->b_reqnext;
1544 while (bh) {
1545 atomic_set(&bh->b_count, 0);
1546 bh = bh->b_reqnext;
1547 }
1548 bh = tape->bh;
1549 while (i) {
1550 if (bh == NULL) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001551 printk(KERN_INFO "ide-tape: bug,"
1552 " bh NULL\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553 break;
1554 }
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001555 min = min(i, (unsigned int)(bh->b_size -
1556 atomic_read(&bh->b_count)));
1557 memset(bh->b_data + atomic_read(&bh->b_count),
1558 0, min);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559 atomic_add(min, &bh->b_count);
1560 i -= min;
1561 bh = bh->b_reqnext;
1562 }
1563 }
1564 (void) idetape_add_chrdev_write_request(drive, blocks);
Borislav Petkov01a63aeb2008-04-27 15:38:34 +02001565 tape->merge_bh_size = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001566 }
Borislav Petkov077e3bd2008-04-27 15:38:34 +02001567 if (tape->merge_bh != NULL) {
1568 ide_tape_kfree_buffer(tape);
1569 tape->merge_bh = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570 }
Borislav Petkov54abf372008-02-06 02:57:52 +01001571 tape->chrdev_dir = IDETAPE_DIR_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572}
1573
Borislav Petkov83042b22008-04-27 15:38:27 +02001574static int idetape_init_read(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001575{
1576 idetape_tape_t *tape = drive->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001577 int bytes_read;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001578
1579 /* Initialize read operation */
Borislav Petkov54abf372008-02-06 02:57:52 +01001580 if (tape->chrdev_dir != IDETAPE_DIR_READ) {
1581 if (tape->chrdev_dir == IDETAPE_DIR_WRITE) {
Borislav Petkovd9df9372008-04-27 15:38:34 +02001582 ide_tape_flush_merge_buffer(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001583 idetape_flush_tape_buffers(drive);
1584 }
Borislav Petkov077e3bd2008-04-27 15:38:34 +02001585 if (tape->merge_bh || tape->merge_bh_size) {
Borislav Petkov01a63aeb2008-04-27 15:38:34 +02001586 printk(KERN_ERR "ide-tape: merge_bh_size should be"
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001587 " 0 now\n");
Borislav Petkov01a63aeb2008-04-27 15:38:34 +02001588 tape->merge_bh_size = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001589 }
Borislav Petkov077e3bd2008-04-27 15:38:34 +02001590 tape->merge_bh = ide_tape_kmalloc_buffer(tape, 0, 0);
1591 if (!tape->merge_bh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592 return -ENOMEM;
Borislav Petkov54abf372008-02-06 02:57:52 +01001593 tape->chrdev_dir = IDETAPE_DIR_READ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594
1595 /*
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001596 * Issue a read 0 command to ensure that DSC handshake is
1597 * switched from completion mode to buffer available mode.
1598 * No point in issuing this if DSC overlap isn't supported, some
1599 * drives (Seagate STT3401A) will return an error.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001600 */
1601 if (drive->dsc_overlap) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001602 bytes_read = idetape_queue_rw_tail(drive,
1603 REQ_IDETAPE_READ, 0,
Borislav Petkov077e3bd2008-04-27 15:38:34 +02001604 tape->merge_bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001605 if (bytes_read < 0) {
Borislav Petkov077e3bd2008-04-27 15:38:34 +02001606 ide_tape_kfree_buffer(tape);
1607 tape->merge_bh = NULL;
Borislav Petkov54abf372008-02-06 02:57:52 +01001608 tape->chrdev_dir = IDETAPE_DIR_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001609 return bytes_read;
1610 }
1611 }
1612 }
Borislav Petkov5e69bd92008-04-27 15:38:25 +02001613
Linus Torvalds1da177e2005-04-16 15:20:36 -07001614 return 0;
1615}
1616
Borislav Petkov5bd50dc2008-04-27 15:38:28 +02001617/* called from idetape_chrdev_read() to service a chrdev read request. */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001618static int idetape_add_chrdev_read_request(ide_drive_t *drive, int blocks)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001619{
1620 idetape_tape_t *tape = drive->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001621
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001622 debug_log(DBG_PROCS, "Enter %s, %d blocks\n", __func__, blocks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001624 /* If we are at a filemark, return a read length of 0 */
Borislav Petkovf2e3ab52008-07-23 19:56:01 +02001625 if (test_bit(IDE_AFLAG_FILEMARK, &drive->atapi_flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001626 return 0;
1627
Borislav Petkov83042b22008-04-27 15:38:27 +02001628 idetape_init_read(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001629
Borislav Petkov5e69bd92008-04-27 15:38:25 +02001630 return idetape_queue_rw_tail(drive, REQ_IDETAPE_READ, blocks,
Borislav Petkov077e3bd2008-04-27 15:38:34 +02001631 tape->merge_bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632}
1633
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001634static void idetape_pad_zeros(ide_drive_t *drive, int bcount)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001635{
1636 idetape_tape_t *tape = drive->driver_data;
1637 struct idetape_bh *bh;
1638 int blocks;
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001639
Linus Torvalds1da177e2005-04-16 15:20:36 -07001640 while (bcount) {
1641 unsigned int count;
1642
Borislav Petkov077e3bd2008-04-27 15:38:34 +02001643 bh = tape->merge_bh;
Borislav Petkovf73850a2008-04-27 15:38:33 +02001644 count = min(tape->buffer_size, bcount);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001645 bcount -= count;
Borislav Petkov54bb2072008-02-06 02:57:52 +01001646 blocks = count / tape->blk_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001647 while (count) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001648 atomic_set(&bh->b_count,
1649 min(count, (unsigned int)bh->b_size));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001650 memset(bh->b_data, 0, atomic_read(&bh->b_count));
1651 count -= atomic_read(&bh->b_count);
1652 bh = bh->b_reqnext;
1653 }
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001654 idetape_queue_rw_tail(drive, REQ_IDETAPE_WRITE, blocks,
Borislav Petkov077e3bd2008-04-27 15:38:34 +02001655 tape->merge_bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001656 }
1657}
1658
Linus Torvalds1da177e2005-04-16 15:20:36 -07001659/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001660 * Rewinds the tape to the Beginning Of the current Partition (BOP). We
1661 * currently support only one partition.
1662 */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001663static int idetape_rewind_tape(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664{
1665 int retval;
Borislav Petkovd236d742008-04-18 00:46:27 +02001666 struct ide_atapi_pc pc;
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001667 idetape_tape_t *tape;
1668 tape = drive->driver_data;
1669
1670 debug_log(DBG_SENSE, "Enter %s\n", __func__);
1671
Linus Torvalds1da177e2005-04-16 15:20:36 -07001672 idetape_create_rewind_cmd(drive, &pc);
1673 retval = idetape_queue_pc_tail(drive, &pc);
1674 if (retval)
1675 return retval;
1676
1677 idetape_create_read_position_cmd(&pc);
1678 retval = idetape_queue_pc_tail(drive, &pc);
1679 if (retval)
1680 return retval;
1681 return 0;
1682}
1683
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001684/* mtio.h compatible commands should be issued to the chrdev interface. */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001685static int idetape_blkdev_ioctl(ide_drive_t *drive, unsigned int cmd,
1686 unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001687{
1688 idetape_tape_t *tape = drive->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001689 void __user *argp = (void __user *)arg;
1690
Borislav Petkovd59823f2008-02-02 19:56:51 +01001691 struct idetape_config {
1692 int dsc_rw_frequency;
1693 int dsc_media_access_frequency;
1694 int nr_stages;
1695 } config;
1696
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001697 debug_log(DBG_PROCS, "Enter %s\n", __func__);
1698
Linus Torvalds1da177e2005-04-16 15:20:36 -07001699 switch (cmd) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001700 case 0x0340:
1701 if (copy_from_user(&config, argp, sizeof(config)))
1702 return -EFAULT;
1703 tape->best_dsc_rw_freq = config.dsc_rw_frequency;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001704 break;
1705 case 0x0350:
1706 config.dsc_rw_frequency = (int) tape->best_dsc_rw_freq;
Borislav Petkov83042b22008-04-27 15:38:27 +02001707 config.nr_stages = 1;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001708 if (copy_to_user(argp, &config, sizeof(config)))
1709 return -EFAULT;
1710 break;
1711 default:
1712 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713 }
1714 return 0;
1715}
1716
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001717static int idetape_space_over_filemarks(ide_drive_t *drive, short mt_op,
1718 int mt_count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001719{
1720 idetape_tape_t *tape = drive->driver_data;
Borislav Petkovd236d742008-04-18 00:46:27 +02001721 struct ide_atapi_pc pc;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001722 int retval, count = 0;
Borislav Petkovb6422012008-02-02 19:56:49 +01001723 int sprev = !!(tape->caps[4] & 0x20);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001724
1725 if (mt_count == 0)
1726 return 0;
1727 if (MTBSF == mt_op || MTBSFM == mt_op) {
Borislav Petkovb6422012008-02-02 19:56:49 +01001728 if (!sprev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001729 return -EIO;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001730 mt_count = -mt_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001731 }
1732
Borislav Petkov54abf372008-02-06 02:57:52 +01001733 if (tape->chrdev_dir == IDETAPE_DIR_READ) {
Borislav Petkov01a63aeb2008-04-27 15:38:34 +02001734 tape->merge_bh_size = 0;
Borislav Petkovf2e3ab52008-07-23 19:56:01 +02001735 if (test_and_clear_bit(IDE_AFLAG_FILEMARK, &drive->atapi_flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001736 ++count;
Borislav Petkovec0fdb02008-04-27 15:38:34 +02001737 ide_tape_discard_merge_buffer(drive, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001738 }
1739
Linus Torvalds1da177e2005-04-16 15:20:36 -07001740 switch (mt_op) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001741 case MTFSF:
1742 case MTBSF:
1743 idetape_create_space_cmd(&pc, mt_count - count,
1744 IDETAPE_SPACE_OVER_FILEMARK);
1745 return idetape_queue_pc_tail(drive, &pc);
1746 case MTFSFM:
1747 case MTBSFM:
1748 if (!sprev)
1749 return -EIO;
1750 retval = idetape_space_over_filemarks(drive, MTFSF,
1751 mt_count - count);
1752 if (retval)
1753 return retval;
1754 count = (MTBSFM == mt_op ? 1 : -1);
1755 return idetape_space_over_filemarks(drive, MTFSF, count);
1756 default:
1757 printk(KERN_ERR "ide-tape: MTIO operation %d not supported\n",
1758 mt_op);
1759 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001760 }
1761}
1762
Linus Torvalds1da177e2005-04-16 15:20:36 -07001763/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001764 * Our character device read / write functions.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001765 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001766 * The tape is optimized to maximize throughput when it is transferring an
1767 * integral number of the "continuous transfer limit", which is a parameter of
1768 * the specific tape (26kB on my particular tape, 32kB for Onstream).
Linus Torvalds1da177e2005-04-16 15:20:36 -07001769 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001770 * As of version 1.3 of the driver, the character device provides an abstract
1771 * continuous view of the media - any mix of block sizes (even 1 byte) on the
1772 * same backup/restore procedure is supported. The driver will internally
1773 * convert the requests to the recommended transfer unit, so that an unmatch
1774 * between the user's block size to the recommended size will only result in a
1775 * (slightly) increased driver overhead, but will no longer hit performance.
1776 * This is not applicable to Onstream.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001777 */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001778static ssize_t idetape_chrdev_read(struct file *file, char __user *buf,
1779 size_t count, loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001780{
1781 struct ide_tape_obj *tape = ide_tape_f(file);
1782 ide_drive_t *drive = tape->drive;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001783 ssize_t bytes_read, temp, actually_read = 0, rc;
Daniel Walkerdcd96372006-06-25 05:47:37 -07001784 ssize_t ret = 0;
Borislav Petkovb6422012008-02-02 19:56:49 +01001785 u16 ctl = *(u16 *)&tape->caps[12];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001786
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001787 debug_log(DBG_CHRDEV, "Enter %s, count %Zd\n", __func__, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788
Borislav Petkov54abf372008-02-06 02:57:52 +01001789 if (tape->chrdev_dir != IDETAPE_DIR_READ) {
Borislav Petkovf2e3ab52008-07-23 19:56:01 +02001790 if (test_bit(IDE_AFLAG_DETECT_BS, &drive->atapi_flags))
Borislav Petkov54bb2072008-02-06 02:57:52 +01001791 if (count > tape->blk_size &&
1792 (count % tape->blk_size) == 0)
1793 tape->user_bs_factor = count / tape->blk_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001794 }
Borislav Petkov83042b22008-04-27 15:38:27 +02001795 rc = idetape_init_read(drive);
Borislav Petkov8d06bfa2008-02-06 02:57:53 +01001796 if (rc < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001797 return rc;
1798 if (count == 0)
1799 return (0);
Borislav Petkov01a63aeb2008-04-27 15:38:34 +02001800 if (tape->merge_bh_size) {
1801 actually_read = min((unsigned int)(tape->merge_bh_size),
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001802 (unsigned int)count);
Borislav Petkov99d74e62008-04-27 15:38:25 +02001803 if (idetape_copy_stage_to_user(tape, buf, actually_read))
Daniel Walkerdcd96372006-06-25 05:47:37 -07001804 ret = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805 buf += actually_read;
Borislav Petkov01a63aeb2008-04-27 15:38:34 +02001806 tape->merge_bh_size -= actually_read;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807 count -= actually_read;
1808 }
Borislav Petkovf73850a2008-04-27 15:38:33 +02001809 while (count >= tape->buffer_size) {
Borislav Petkovb6422012008-02-02 19:56:49 +01001810 bytes_read = idetape_add_chrdev_read_request(drive, ctl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001811 if (bytes_read <= 0)
1812 goto finish;
Borislav Petkov99d74e62008-04-27 15:38:25 +02001813 if (idetape_copy_stage_to_user(tape, buf, bytes_read))
Daniel Walkerdcd96372006-06-25 05:47:37 -07001814 ret = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001815 buf += bytes_read;
1816 count -= bytes_read;
1817 actually_read += bytes_read;
1818 }
1819 if (count) {
Borislav Petkovb6422012008-02-02 19:56:49 +01001820 bytes_read = idetape_add_chrdev_read_request(drive, ctl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001821 if (bytes_read <= 0)
1822 goto finish;
1823 temp = min((unsigned long)count, (unsigned long)bytes_read);
Borislav Petkov99d74e62008-04-27 15:38:25 +02001824 if (idetape_copy_stage_to_user(tape, buf, temp))
Daniel Walkerdcd96372006-06-25 05:47:37 -07001825 ret = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001826 actually_read += temp;
Borislav Petkov01a63aeb2008-04-27 15:38:34 +02001827 tape->merge_bh_size = bytes_read-temp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001828 }
1829finish:
Borislav Petkovf2e3ab52008-07-23 19:56:01 +02001830 if (!actually_read && test_bit(IDE_AFLAG_FILEMARK, &drive->atapi_flags)) {
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001831 debug_log(DBG_SENSE, "%s: spacing over filemark\n", tape->name);
1832
Linus Torvalds1da177e2005-04-16 15:20:36 -07001833 idetape_space_over_filemarks(drive, MTFSF, 1);
1834 return 0;
1835 }
Daniel Walkerdcd96372006-06-25 05:47:37 -07001836
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001837 return ret ? ret : actually_read;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001838}
1839
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001840static ssize_t idetape_chrdev_write(struct file *file, const char __user *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001841 size_t count, loff_t *ppos)
1842{
1843 struct ide_tape_obj *tape = ide_tape_f(file);
1844 ide_drive_t *drive = tape->drive;
Daniel Walkerdcd96372006-06-25 05:47:37 -07001845 ssize_t actually_written = 0;
1846 ssize_t ret = 0;
Borislav Petkovb6422012008-02-02 19:56:49 +01001847 u16 ctl = *(u16 *)&tape->caps[12];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001848
1849 /* The drive is write protected. */
1850 if (tape->write_prot)
1851 return -EACCES;
1852
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001853 debug_log(DBG_CHRDEV, "Enter %s, count %Zd\n", __func__, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001854
1855 /* Initialize write operation */
Borislav Petkov54abf372008-02-06 02:57:52 +01001856 if (tape->chrdev_dir != IDETAPE_DIR_WRITE) {
1857 if (tape->chrdev_dir == IDETAPE_DIR_READ)
Borislav Petkovec0fdb02008-04-27 15:38:34 +02001858 ide_tape_discard_merge_buffer(drive, 1);
Borislav Petkov077e3bd2008-04-27 15:38:34 +02001859 if (tape->merge_bh || tape->merge_bh_size) {
Borislav Petkov01a63aeb2008-04-27 15:38:34 +02001860 printk(KERN_ERR "ide-tape: merge_bh_size "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001861 "should be 0 now\n");
Borislav Petkov01a63aeb2008-04-27 15:38:34 +02001862 tape->merge_bh_size = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001863 }
Borislav Petkov077e3bd2008-04-27 15:38:34 +02001864 tape->merge_bh = ide_tape_kmalloc_buffer(tape, 0, 0);
1865 if (!tape->merge_bh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001866 return -ENOMEM;
Borislav Petkov54abf372008-02-06 02:57:52 +01001867 tape->chrdev_dir = IDETAPE_DIR_WRITE;
Borislav Petkov077e3bd2008-04-27 15:38:34 +02001868 idetape_init_merge_buffer(tape);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001869
1870 /*
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001871 * Issue a write 0 command to ensure that DSC handshake is
1872 * switched from completion mode to buffer available mode. No
1873 * point in issuing this if DSC overlap isn't supported, some
1874 * drives (Seagate STT3401A) will return an error.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001875 */
1876 if (drive->dsc_overlap) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001877 ssize_t retval = idetape_queue_rw_tail(drive,
1878 REQ_IDETAPE_WRITE, 0,
Borislav Petkov077e3bd2008-04-27 15:38:34 +02001879 tape->merge_bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001880 if (retval < 0) {
Borislav Petkov077e3bd2008-04-27 15:38:34 +02001881 ide_tape_kfree_buffer(tape);
1882 tape->merge_bh = NULL;
Borislav Petkov54abf372008-02-06 02:57:52 +01001883 tape->chrdev_dir = IDETAPE_DIR_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001884 return retval;
1885 }
1886 }
1887 }
1888 if (count == 0)
1889 return (0);
Borislav Petkov01a63aeb2008-04-27 15:38:34 +02001890 if (tape->merge_bh_size) {
1891 if (tape->merge_bh_size >= tape->buffer_size) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001892 printk(KERN_ERR "ide-tape: bug: merge buf too big\n");
Borislav Petkov01a63aeb2008-04-27 15:38:34 +02001893 tape->merge_bh_size = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001894 }
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001895 actually_written = min((unsigned int)
Borislav Petkov01a63aeb2008-04-27 15:38:34 +02001896 (tape->buffer_size - tape->merge_bh_size),
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001897 (unsigned int)count);
Borislav Petkov8646c882008-04-27 15:38:26 +02001898 if (idetape_copy_stage_from_user(tape, buf, actually_written))
Daniel Walkerdcd96372006-06-25 05:47:37 -07001899 ret = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001900 buf += actually_written;
Borislav Petkov01a63aeb2008-04-27 15:38:34 +02001901 tape->merge_bh_size += actually_written;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001902 count -= actually_written;
1903
Borislav Petkov01a63aeb2008-04-27 15:38:34 +02001904 if (tape->merge_bh_size == tape->buffer_size) {
Daniel Walkerdcd96372006-06-25 05:47:37 -07001905 ssize_t retval;
Borislav Petkov01a63aeb2008-04-27 15:38:34 +02001906 tape->merge_bh_size = 0;
Borislav Petkovb6422012008-02-02 19:56:49 +01001907 retval = idetape_add_chrdev_write_request(drive, ctl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001908 if (retval <= 0)
1909 return (retval);
1910 }
1911 }
Borislav Petkovf73850a2008-04-27 15:38:33 +02001912 while (count >= tape->buffer_size) {
Daniel Walkerdcd96372006-06-25 05:47:37 -07001913 ssize_t retval;
Borislav Petkovf73850a2008-04-27 15:38:33 +02001914 if (idetape_copy_stage_from_user(tape, buf, tape->buffer_size))
Daniel Walkerdcd96372006-06-25 05:47:37 -07001915 ret = -EFAULT;
Borislav Petkovf73850a2008-04-27 15:38:33 +02001916 buf += tape->buffer_size;
1917 count -= tape->buffer_size;
Borislav Petkovb6422012008-02-02 19:56:49 +01001918 retval = idetape_add_chrdev_write_request(drive, ctl);
Borislav Petkovf73850a2008-04-27 15:38:33 +02001919 actually_written += tape->buffer_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001920 if (retval <= 0)
1921 return (retval);
1922 }
1923 if (count) {
1924 actually_written += count;
Borislav Petkov8646c882008-04-27 15:38:26 +02001925 if (idetape_copy_stage_from_user(tape, buf, count))
Daniel Walkerdcd96372006-06-25 05:47:37 -07001926 ret = -EFAULT;
Borislav Petkov01a63aeb2008-04-27 15:38:34 +02001927 tape->merge_bh_size += count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001928 }
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001929 return ret ? ret : actually_written;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001930}
1931
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001932static int idetape_write_filemark(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001933{
Borislav Petkovd236d742008-04-18 00:46:27 +02001934 struct ide_atapi_pc pc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001935
1936 /* Write a filemark */
1937 idetape_create_write_filemark_cmd(drive, &pc, 1);
1938 if (idetape_queue_pc_tail(drive, &pc)) {
1939 printk(KERN_ERR "ide-tape: Couldn't write a filemark\n");
1940 return -EIO;
1941 }
1942 return 0;
1943}
1944
1945/*
Borislav Petkovd99c9da2008-02-02 19:56:51 +01001946 * Called from idetape_chrdev_ioctl when the general mtio MTIOCTOP ioctl is
1947 * requested.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001948 *
Borislav Petkovd99c9da2008-02-02 19:56:51 +01001949 * Note: MTBSF and MTBSFM are not supported when the tape doesn't support
1950 * spacing over filemarks in the reverse direction. In this case, MTFSFM is also
Borislav Petkov5bd50dc2008-04-27 15:38:28 +02001951 * usually not supported.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001952 *
Borislav Petkovd99c9da2008-02-02 19:56:51 +01001953 * The following commands are currently not supported:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001954 *
Borislav Petkovd99c9da2008-02-02 19:56:51 +01001955 * MTFSS, MTBSS, MTWSM, MTSETDENSITY, MTSETDRVBUFFER, MT_ST_BOOLEANS,
1956 * MT_ST_WRITE_THRESHOLD.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001957 */
Borislav Petkovd99c9da2008-02-02 19:56:51 +01001958static int idetape_mtioctop(ide_drive_t *drive, short mt_op, int mt_count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001959{
1960 idetape_tape_t *tape = drive->driver_data;
Borislav Petkovd236d742008-04-18 00:46:27 +02001961 struct ide_atapi_pc pc;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001962 int i, retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001963
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001964 debug_log(DBG_ERR, "Handling MTIOCTOP ioctl: mt_op=%d, mt_count=%d\n",
1965 mt_op, mt_count);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001966
Linus Torvalds1da177e2005-04-16 15:20:36 -07001967 switch (mt_op) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001968 case MTFSF:
1969 case MTFSFM:
1970 case MTBSF:
1971 case MTBSFM:
1972 if (!mt_count)
1973 return 0;
1974 return idetape_space_over_filemarks(drive, mt_op, mt_count);
1975 default:
1976 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001977 }
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001978
Linus Torvalds1da177e2005-04-16 15:20:36 -07001979 switch (mt_op) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001980 case MTWEOF:
1981 if (tape->write_prot)
1982 return -EACCES;
Borislav Petkovec0fdb02008-04-27 15:38:34 +02001983 ide_tape_discard_merge_buffer(drive, 1);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001984 for (i = 0; i < mt_count; i++) {
1985 retval = idetape_write_filemark(drive);
1986 if (retval)
1987 return retval;
1988 }
1989 return 0;
1990 case MTREW:
Borislav Petkovec0fdb02008-04-27 15:38:34 +02001991 ide_tape_discard_merge_buffer(drive, 0);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001992 if (idetape_rewind_tape(drive))
1993 return -EIO;
1994 return 0;
1995 case MTLOAD:
Borislav Petkovec0fdb02008-04-27 15:38:34 +02001996 ide_tape_discard_merge_buffer(drive, 0);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001997 idetape_create_load_unload_cmd(drive, &pc,
1998 IDETAPE_LU_LOAD_MASK);
1999 return idetape_queue_pc_tail(drive, &pc);
2000 case MTUNLOAD:
2001 case MTOFFL:
2002 /*
2003 * If door is locked, attempt to unlock before
2004 * attempting to eject.
2005 */
2006 if (tape->door_locked) {
2007 if (idetape_create_prevent_cmd(drive, &pc, 0))
2008 if (!idetape_queue_pc_tail(drive, &pc))
2009 tape->door_locked = DOOR_UNLOCKED;
2010 }
Borislav Petkovec0fdb02008-04-27 15:38:34 +02002011 ide_tape_discard_merge_buffer(drive, 0);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002012 idetape_create_load_unload_cmd(drive, &pc,
2013 !IDETAPE_LU_LOAD_MASK);
2014 retval = idetape_queue_pc_tail(drive, &pc);
2015 if (!retval)
Borislav Petkovf2e3ab52008-07-23 19:56:01 +02002016 clear_bit(IDE_AFLAG_MEDIUM_PRESENT, &drive->atapi_flags);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002017 return retval;
2018 case MTNOP:
Borislav Petkovec0fdb02008-04-27 15:38:34 +02002019 ide_tape_discard_merge_buffer(drive, 0);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002020 return idetape_flush_tape_buffers(drive);
2021 case MTRETEN:
Borislav Petkovec0fdb02008-04-27 15:38:34 +02002022 ide_tape_discard_merge_buffer(drive, 0);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002023 idetape_create_load_unload_cmd(drive, &pc,
2024 IDETAPE_LU_RETENSION_MASK | IDETAPE_LU_LOAD_MASK);
2025 return idetape_queue_pc_tail(drive, &pc);
2026 case MTEOM:
2027 idetape_create_space_cmd(&pc, 0, IDETAPE_SPACE_TO_EOD);
2028 return idetape_queue_pc_tail(drive, &pc);
2029 case MTERASE:
2030 (void)idetape_rewind_tape(drive);
2031 idetape_create_erase_cmd(&pc);
2032 return idetape_queue_pc_tail(drive, &pc);
2033 case MTSETBLK:
2034 if (mt_count) {
2035 if (mt_count < tape->blk_size ||
2036 mt_count % tape->blk_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002037 return -EIO;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002038 tape->user_bs_factor = mt_count / tape->blk_size;
Borislav Petkovf2e3ab52008-07-23 19:56:01 +02002039 clear_bit(IDE_AFLAG_DETECT_BS, &drive->atapi_flags);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002040 } else
Borislav Petkovf2e3ab52008-07-23 19:56:01 +02002041 set_bit(IDE_AFLAG_DETECT_BS, &drive->atapi_flags);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002042 return 0;
2043 case MTSEEK:
Borislav Petkovec0fdb02008-04-27 15:38:34 +02002044 ide_tape_discard_merge_buffer(drive, 0);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002045 return idetape_position_tape(drive,
2046 mt_count * tape->user_bs_factor, tape->partition, 0);
2047 case MTSETPART:
Borislav Petkovec0fdb02008-04-27 15:38:34 +02002048 ide_tape_discard_merge_buffer(drive, 0);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002049 return idetape_position_tape(drive, 0, mt_count, 0);
2050 case MTFSR:
2051 case MTBSR:
2052 case MTLOCK:
2053 if (!idetape_create_prevent_cmd(drive, &pc, 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002054 return 0;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002055 retval = idetape_queue_pc_tail(drive, &pc);
2056 if (retval)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002057 return retval;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002058 tape->door_locked = DOOR_EXPLICITLY_LOCKED;
2059 return 0;
2060 case MTUNLOCK:
2061 if (!idetape_create_prevent_cmd(drive, &pc, 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002062 return 0;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002063 retval = idetape_queue_pc_tail(drive, &pc);
2064 if (retval)
2065 return retval;
2066 tape->door_locked = DOOR_UNLOCKED;
2067 return 0;
2068 default:
2069 printk(KERN_ERR "ide-tape: MTIO operation %d not supported\n",
2070 mt_op);
2071 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002072 }
2073}
2074
2075/*
Borislav Petkovd99c9da2008-02-02 19:56:51 +01002076 * Our character device ioctls. General mtio.h magnetic io commands are
2077 * supported here, and not in the corresponding block interface. Our own
2078 * ide-tape ioctls are supported on both interfaces.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002079 */
Borislav Petkovd99c9da2008-02-02 19:56:51 +01002080static int idetape_chrdev_ioctl(struct inode *inode, struct file *file,
2081 unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002082{
2083 struct ide_tape_obj *tape = ide_tape_f(file);
2084 ide_drive_t *drive = tape->drive;
2085 struct mtop mtop;
2086 struct mtget mtget;
2087 struct mtpos mtpos;
Borislav Petkov54bb2072008-02-06 02:57:52 +01002088 int block_offset = 0, position = tape->first_frame;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002089 void __user *argp = (void __user *)arg;
2090
Borislav Petkov8004a8c2008-02-06 02:57:51 +01002091 debug_log(DBG_CHRDEV, "Enter %s, cmd=%u\n", __func__, cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002092
Borislav Petkov54abf372008-02-06 02:57:52 +01002093 if (tape->chrdev_dir == IDETAPE_DIR_WRITE) {
Borislav Petkovd9df9372008-04-27 15:38:34 +02002094 ide_tape_flush_merge_buffer(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002095 idetape_flush_tape_buffers(drive);
2096 }
2097 if (cmd == MTIOCGET || cmd == MTIOCPOS) {
Borislav Petkov01a63aeb2008-04-27 15:38:34 +02002098 block_offset = tape->merge_bh_size /
Borislav Petkov54bb2072008-02-06 02:57:52 +01002099 (tape->blk_size * tape->user_bs_factor);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002100 position = idetape_read_position(drive);
2101 if (position < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002102 return -EIO;
2103 }
2104 switch (cmd) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002105 case MTIOCTOP:
2106 if (copy_from_user(&mtop, argp, sizeof(struct mtop)))
2107 return -EFAULT;
2108 return idetape_mtioctop(drive, mtop.mt_op, mtop.mt_count);
2109 case MTIOCGET:
2110 memset(&mtget, 0, sizeof(struct mtget));
2111 mtget.mt_type = MT_ISSCSI2;
2112 mtget.mt_blkno = position / tape->user_bs_factor - block_offset;
2113 mtget.mt_dsreg =
2114 ((tape->blk_size * tape->user_bs_factor)
2115 << MT_ST_BLKSIZE_SHIFT) & MT_ST_BLKSIZE_MASK;
Borislav Petkov54bb2072008-02-06 02:57:52 +01002116
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002117 if (tape->drv_write_prot)
2118 mtget.mt_gstat |= GMT_WR_PROT(0xffffffff);
2119
2120 if (copy_to_user(argp, &mtget, sizeof(struct mtget)))
2121 return -EFAULT;
2122 return 0;
2123 case MTIOCPOS:
2124 mtpos.mt_blkno = position / tape->user_bs_factor - block_offset;
2125 if (copy_to_user(argp, &mtpos, sizeof(struct mtpos)))
2126 return -EFAULT;
2127 return 0;
2128 default:
2129 if (tape->chrdev_dir == IDETAPE_DIR_READ)
Borislav Petkovec0fdb02008-04-27 15:38:34 +02002130 ide_tape_discard_merge_buffer(drive, 1);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002131 return idetape_blkdev_ioctl(drive, cmd, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002132 }
2133}
2134
Borislav Petkov3cffb9c2008-02-02 19:56:50 +01002135/*
2136 * Do a mode sense page 0 with block descriptor and if it succeeds set the tape
2137 * block size with the reported value.
2138 */
2139static void ide_tape_get_bsize_from_bdesc(ide_drive_t *drive)
2140{
2141 idetape_tape_t *tape = drive->driver_data;
Borislav Petkovd236d742008-04-18 00:46:27 +02002142 struct ide_atapi_pc pc;
Borislav Petkov3cffb9c2008-02-02 19:56:50 +01002143
2144 idetape_create_mode_sense_cmd(&pc, IDETAPE_BLOCK_DESCRIPTOR);
2145 if (idetape_queue_pc_tail(drive, &pc)) {
2146 printk(KERN_ERR "ide-tape: Can't get block descriptor\n");
Borislav Petkov54bb2072008-02-06 02:57:52 +01002147 if (tape->blk_size == 0) {
Borislav Petkov3cffb9c2008-02-02 19:56:50 +01002148 printk(KERN_WARNING "ide-tape: Cannot deal with zero "
2149 "block size, assuming 32k\n");
Borislav Petkov54bb2072008-02-06 02:57:52 +01002150 tape->blk_size = 32768;
Borislav Petkov3cffb9c2008-02-02 19:56:50 +01002151 }
2152 return;
2153 }
Borislav Petkovd236d742008-04-18 00:46:27 +02002154 tape->blk_size = (pc.buf[4 + 5] << 16) +
2155 (pc.buf[4 + 6] << 8) +
2156 pc.buf[4 + 7];
2157 tape->drv_write_prot = (pc.buf[2] & 0x80) >> 7;
Borislav Petkov3cffb9c2008-02-02 19:56:50 +01002158}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002159
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002160static int idetape_chrdev_open(struct inode *inode, struct file *filp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002161{
2162 unsigned int minor = iminor(inode), i = minor & ~0xc0;
2163 ide_drive_t *drive;
2164 idetape_tape_t *tape;
Borislav Petkovd236d742008-04-18 00:46:27 +02002165 struct ide_atapi_pc pc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002166 int retval;
2167
Borislav Petkov8004a8c2008-02-06 02:57:51 +01002168 if (i >= MAX_HWIFS * MAX_DRIVES)
2169 return -ENXIO;
2170
Jonathan Corbet04f4ac92008-05-15 12:01:56 -06002171 lock_kernel();
Borislav Petkov8004a8c2008-02-06 02:57:51 +01002172 tape = ide_tape_chrdev_get(i);
Jonathan Corbet04f4ac92008-05-15 12:01:56 -06002173 if (!tape) {
2174 unlock_kernel();
Borislav Petkov8004a8c2008-02-06 02:57:51 +01002175 return -ENXIO;
Jonathan Corbet04f4ac92008-05-15 12:01:56 -06002176 }
Borislav Petkov8004a8c2008-02-06 02:57:51 +01002177
2178 debug_log(DBG_CHRDEV, "Enter %s\n", __func__);
2179
Linus Torvalds1da177e2005-04-16 15:20:36 -07002180 /*
2181 * We really want to do nonseekable_open(inode, filp); here, but some
2182 * versions of tar incorrectly call lseek on tapes and bail out if that
2183 * fails. So we disallow pread() and pwrite(), but permit lseeks.
2184 */
2185 filp->f_mode &= ~(FMODE_PREAD | FMODE_PWRITE);
2186
Linus Torvalds1da177e2005-04-16 15:20:36 -07002187 drive = tape->drive;
2188
2189 filp->private_data = tape;
2190
Borislav Petkovf2e3ab52008-07-23 19:56:01 +02002191 if (test_and_set_bit(IDE_AFLAG_BUSY, &drive->atapi_flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002192 retval = -EBUSY;
2193 goto out_put_tape;
2194 }
2195
2196 retval = idetape_wait_ready(drive, 60 * HZ);
2197 if (retval) {
Borislav Petkovf2e3ab52008-07-23 19:56:01 +02002198 clear_bit(IDE_AFLAG_BUSY, &drive->atapi_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002199 printk(KERN_ERR "ide-tape: %s: drive not ready\n", tape->name);
2200 goto out_put_tape;
2201 }
2202
2203 idetape_read_position(drive);
Borislav Petkovf2e3ab52008-07-23 19:56:01 +02002204 if (!test_bit(IDE_AFLAG_ADDRESS_VALID, &drive->atapi_flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002205 (void)idetape_rewind_tape(drive);
2206
Linus Torvalds1da177e2005-04-16 15:20:36 -07002207 /* Read block size and write protect status from drive. */
Borislav Petkov3cffb9c2008-02-02 19:56:50 +01002208 ide_tape_get_bsize_from_bdesc(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002209
2210 /* Set write protect flag if device is opened as read-only. */
2211 if ((filp->f_flags & O_ACCMODE) == O_RDONLY)
2212 tape->write_prot = 1;
2213 else
2214 tape->write_prot = tape->drv_write_prot;
2215
2216 /* Make sure drive isn't write protected if user wants to write. */
2217 if (tape->write_prot) {
2218 if ((filp->f_flags & O_ACCMODE) == O_WRONLY ||
2219 (filp->f_flags & O_ACCMODE) == O_RDWR) {
Borislav Petkovf2e3ab52008-07-23 19:56:01 +02002220 clear_bit(IDE_AFLAG_BUSY, &drive->atapi_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002221 retval = -EROFS;
2222 goto out_put_tape;
2223 }
2224 }
2225
Borislav Petkov3c98bf32008-02-06 02:57:53 +01002226 /* Lock the tape drive door so user can't eject. */
Borislav Petkov54abf372008-02-06 02:57:52 +01002227 if (tape->chrdev_dir == IDETAPE_DIR_NONE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002228 if (idetape_create_prevent_cmd(drive, &pc, 1)) {
2229 if (!idetape_queue_pc_tail(drive, &pc)) {
2230 if (tape->door_locked != DOOR_EXPLICITLY_LOCKED)
2231 tape->door_locked = DOOR_LOCKED;
2232 }
2233 }
2234 }
Jonathan Corbet04f4ac92008-05-15 12:01:56 -06002235 unlock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002236 return 0;
2237
2238out_put_tape:
2239 ide_tape_put(tape);
Jonathan Corbet04f4ac92008-05-15 12:01:56 -06002240 unlock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002241 return retval;
2242}
2243
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002244static void idetape_write_release(ide_drive_t *drive, unsigned int minor)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002245{
2246 idetape_tape_t *tape = drive->driver_data;
2247
Borislav Petkovd9df9372008-04-27 15:38:34 +02002248 ide_tape_flush_merge_buffer(drive);
Borislav Petkov077e3bd2008-04-27 15:38:34 +02002249 tape->merge_bh = ide_tape_kmalloc_buffer(tape, 1, 0);
2250 if (tape->merge_bh != NULL) {
Borislav Petkov54bb2072008-02-06 02:57:52 +01002251 idetape_pad_zeros(drive, tape->blk_size *
2252 (tape->user_bs_factor - 1));
Borislav Petkov077e3bd2008-04-27 15:38:34 +02002253 ide_tape_kfree_buffer(tape);
2254 tape->merge_bh = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002255 }
2256 idetape_write_filemark(drive);
2257 idetape_flush_tape_buffers(drive);
2258 idetape_flush_tape_buffers(drive);
2259}
2260
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002261static int idetape_chrdev_release(struct inode *inode, struct file *filp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002262{
2263 struct ide_tape_obj *tape = ide_tape_f(filp);
2264 ide_drive_t *drive = tape->drive;
Borislav Petkovd236d742008-04-18 00:46:27 +02002265 struct ide_atapi_pc pc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002266 unsigned int minor = iminor(inode);
2267
2268 lock_kernel();
2269 tape = drive->driver_data;
Borislav Petkov8004a8c2008-02-06 02:57:51 +01002270
2271 debug_log(DBG_CHRDEV, "Enter %s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002272
Borislav Petkov54abf372008-02-06 02:57:52 +01002273 if (tape->chrdev_dir == IDETAPE_DIR_WRITE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002274 idetape_write_release(drive, minor);
Borislav Petkov54abf372008-02-06 02:57:52 +01002275 if (tape->chrdev_dir == IDETAPE_DIR_READ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002276 if (minor < 128)
Borislav Petkovec0fdb02008-04-27 15:38:34 +02002277 ide_tape_discard_merge_buffer(drive, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002278 }
Borislav Petkovf64eee72008-04-27 15:38:25 +02002279
Borislav Petkovf2e3ab52008-07-23 19:56:01 +02002280 if (minor < 128 && test_bit(IDE_AFLAG_MEDIUM_PRESENT, &drive->atapi_flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002281 (void) idetape_rewind_tape(drive);
Borislav Petkov54abf372008-02-06 02:57:52 +01002282 if (tape->chrdev_dir == IDETAPE_DIR_NONE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002283 if (tape->door_locked == DOOR_LOCKED) {
2284 if (idetape_create_prevent_cmd(drive, &pc, 0)) {
2285 if (!idetape_queue_pc_tail(drive, &pc))
2286 tape->door_locked = DOOR_UNLOCKED;
2287 }
2288 }
2289 }
Borislav Petkovf2e3ab52008-07-23 19:56:01 +02002290 clear_bit(IDE_AFLAG_BUSY, &drive->atapi_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002291 ide_tape_put(tape);
2292 unlock_kernel();
2293 return 0;
2294}
2295
Borislav Petkov6d29c8f2008-02-02 19:56:49 +01002296static void idetape_get_inquiry_results(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002297{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002298 idetape_tape_t *tape = drive->driver_data;
Borislav Petkovd236d742008-04-18 00:46:27 +02002299 struct ide_atapi_pc pc;
Borislav Petkov801bd322008-09-27 19:32:17 +02002300 char fw_rev[4], vendor_id[8], product_id[16];
Borislav Petkov6d29c8f2008-02-02 19:56:49 +01002301
Linus Torvalds1da177e2005-04-16 15:20:36 -07002302 idetape_create_inquiry_cmd(&pc);
2303 if (idetape_queue_pc_tail(drive, &pc)) {
Borislav Petkov6d29c8f2008-02-02 19:56:49 +01002304 printk(KERN_ERR "ide-tape: %s: can't get INQUIRY results\n",
2305 tape->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002306 return;
2307 }
Borislav Petkovd236d742008-04-18 00:46:27 +02002308 memcpy(vendor_id, &pc.buf[8], 8);
2309 memcpy(product_id, &pc.buf[16], 16);
2310 memcpy(fw_rev, &pc.buf[32], 4);
Borislav Petkov6d29c8f2008-02-02 19:56:49 +01002311
Borislav Petkov801bd322008-09-27 19:32:17 +02002312 ide_fixstring(vendor_id, 8, 0);
2313 ide_fixstring(product_id, 16, 0);
2314 ide_fixstring(fw_rev, 4, 0);
Borislav Petkov41f81d542008-02-06 02:57:52 +01002315
Borislav Petkov801bd322008-09-27 19:32:17 +02002316 printk(KERN_INFO "ide-tape: %s <-> %s: %.8s %.16s rev %.4s\n",
Borislav Petkov41f81d542008-02-06 02:57:52 +01002317 drive->name, tape->name, vendor_id, product_id, fw_rev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002318}
2319
2320/*
Borislav Petkovb6422012008-02-02 19:56:49 +01002321 * Ask the tape about its various parameters. In particular, we will adjust our
2322 * data transfer buffer size to the recommended value as returned by the tape.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002323 */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002324static void idetape_get_mode_sense_results(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002325{
2326 idetape_tape_t *tape = drive->driver_data;
Borislav Petkovd236d742008-04-18 00:46:27 +02002327 struct ide_atapi_pc pc;
Borislav Petkovb6422012008-02-02 19:56:49 +01002328 u8 *caps;
2329 u8 speed, max_speed;
Borislav Petkov47314fa2008-02-02 19:56:48 +01002330
Linus Torvalds1da177e2005-04-16 15:20:36 -07002331 idetape_create_mode_sense_cmd(&pc, IDETAPE_CAPABILITIES_PAGE);
2332 if (idetape_queue_pc_tail(drive, &pc)) {
Borislav Petkovb6422012008-02-02 19:56:49 +01002333 printk(KERN_ERR "ide-tape: Can't get tape parameters - assuming"
2334 " some default values\n");
Borislav Petkov54bb2072008-02-06 02:57:52 +01002335 tape->blk_size = 512;
Borislav Petkovb6422012008-02-02 19:56:49 +01002336 put_unaligned(52, (u16 *)&tape->caps[12]);
2337 put_unaligned(540, (u16 *)&tape->caps[14]);
2338 put_unaligned(6*52, (u16 *)&tape->caps[16]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002339 return;
2340 }
Borislav Petkovd236d742008-04-18 00:46:27 +02002341 caps = pc.buf + 4 + pc.buf[3];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002342
Borislav Petkovb6422012008-02-02 19:56:49 +01002343 /* convert to host order and save for later use */
Harvey Harrisoncd740ab2008-07-24 22:53:33 +02002344 speed = be16_to_cpup((__be16 *)&caps[14]);
2345 max_speed = be16_to_cpup((__be16 *)&caps[8]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002346
Harvey Harrisoncd740ab2008-07-24 22:53:33 +02002347 *(u16 *)&caps[8] = max_speed;
2348 *(u16 *)&caps[12] = be16_to_cpup((__be16 *)&caps[12]);
2349 *(u16 *)&caps[14] = speed;
2350 *(u16 *)&caps[16] = be16_to_cpup((__be16 *)&caps[16]);
Borislav Petkovb6422012008-02-02 19:56:49 +01002351
2352 if (!speed) {
2353 printk(KERN_INFO "ide-tape: %s: invalid tape speed "
2354 "(assuming 650KB/sec)\n", drive->name);
Harvey Harrisoncd740ab2008-07-24 22:53:33 +02002355 *(u16 *)&caps[14] = 650;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002356 }
Borislav Petkovb6422012008-02-02 19:56:49 +01002357 if (!max_speed) {
2358 printk(KERN_INFO "ide-tape: %s: invalid max_speed "
2359 "(assuming 650KB/sec)\n", drive->name);
Harvey Harrisoncd740ab2008-07-24 22:53:33 +02002360 *(u16 *)&caps[8] = 650;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002361 }
2362
Borislav Petkovb6422012008-02-02 19:56:49 +01002363 memcpy(&tape->caps, caps, 20);
2364 if (caps[7] & 0x02)
Borislav Petkov54bb2072008-02-06 02:57:52 +01002365 tape->blk_size = 512;
Borislav Petkovb6422012008-02-02 19:56:49 +01002366 else if (caps[7] & 0x04)
Borislav Petkov54bb2072008-02-06 02:57:52 +01002367 tape->blk_size = 1024;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002368}
2369
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +02002370#ifdef CONFIG_IDE_PROC_FS
Bartlomiej Zolnierkiewicz8185d5a2008-10-10 22:39:28 +02002371#define ide_tape_devset_get(name, field) \
2372static int get_##name(ide_drive_t *drive) \
2373{ \
2374 idetape_tape_t *tape = drive->driver_data; \
2375 return tape->field; \
Linus Torvalds1da177e2005-04-16 15:20:36 -07002376}
Bartlomiej Zolnierkiewicz8185d5a2008-10-10 22:39:28 +02002377
2378#define ide_tape_devset_set(name, field) \
2379static int set_##name(ide_drive_t *drive, int arg) \
2380{ \
2381 idetape_tape_t *tape = drive->driver_data; \
2382 tape->field = arg; \
2383 return 0; \
2384}
2385
2386#define ide_tape_devset_rw(_name, _min, _max, _field, _mulf, _divf) \
2387ide_tape_devset_get(_name, _field) \
2388ide_tape_devset_set(_name, _field) \
2389__IDE_DEVSET(_name, S_RW, _min, _max, get_##_name, set_##_name, _mulf, _divf)
2390
2391#define ide_tape_devset_r(_name, _min, _max, _field, _mulf, _divf) \
2392ide_tape_devset_get(_name, _field) \
2393__IDE_DEVSET(_name, S_READ, _min, _max, get_##_name, NULL, _mulf, _divf)
2394
2395static int mulf_tdsc(ide_drive_t *drive) { return 1000; }
2396static int divf_tdsc(ide_drive_t *drive) { return HZ; }
2397static int divf_buffer(ide_drive_t *drive) { return 2; }
2398static int divf_buffer_size(ide_drive_t *drive) { return 1024; }
2399
2400ide_devset_rw(dsc_overlap, 0, 1, dsc_overlap);
2401
2402ide_tape_devset_rw(debug_mask, 0, 0xffff, debug_mask, NULL, NULL);
2403ide_tape_devset_rw(tdsc, IDETAPE_DSC_RW_MIN, IDETAPE_DSC_RW_MAX,
2404 best_dsc_rw_freq, mulf_tdsc, divf_tdsc);
2405
2406ide_tape_devset_r(avg_speed, 0, 0xffff, avg_speed, NULL, NULL);
2407ide_tape_devset_r(speed, 0, 0xffff, caps[14], NULL, NULL);
2408ide_tape_devset_r(buffer, 0, 0xffff, caps[16], NULL, divf_buffer);
2409ide_tape_devset_r(buffer_size, 0, 0xffff, buffer_size, NULL, divf_buffer_size);
2410
2411static const struct ide_devset *idetape_settings[] = {
2412 &ide_devset_avg_speed,
2413 &ide_devset_buffer,
2414 &ide_devset_buffer_size,
2415 &ide_devset_debug_mask,
2416 &ide_devset_dsc_overlap,
2417 &ide_devset_speed,
2418 &ide_devset_tdsc,
2419 NULL
2420};
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +02002421#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002422
2423/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +01002424 * The function below is called to:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002425 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +01002426 * 1. Initialize our various state variables.
2427 * 2. Ask the tape for its capabilities.
2428 * 3. Allocate a buffer which will be used for data transfer. The buffer size
2429 * is chosen based on the recommendation which we received in step 2.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002430 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +01002431 * Note that at this point ide.c already assigned us an irq, so that we can
2432 * queue requests here and wait for their completion.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002433 */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002434static void idetape_setup(ide_drive_t *drive, idetape_tape_t *tape, int minor)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002435{
Borislav Petkov83042b22008-04-27 15:38:27 +02002436 unsigned long t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002437 int speed;
Borislav Petkovf73850a2008-04-27 15:38:33 +02002438 int buffer_size;
Borislav Petkov71071b82008-02-06 02:57:53 +01002439 u8 gcw[2];
Borislav Petkovb6422012008-02-02 19:56:49 +01002440 u16 *ctl = (u16 *)&tape->caps[12];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002441
Borislav Petkov776bb022008-07-23 19:55:59 +02002442 drive->pc_callback = ide_tape_callback;
2443
Borislav Petkov54bb2072008-02-06 02:57:52 +01002444 spin_lock_init(&tape->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002445 drive->dsc_overlap = 1;
Bartlomiej Zolnierkiewicz4166c192008-02-01 23:09:30 +01002446 if (drive->hwif->host_flags & IDE_HFLAG_NO_DSC) {
2447 printk(KERN_INFO "ide-tape: %s: disabling DSC overlap\n",
2448 tape->name);
2449 drive->dsc_overlap = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002450 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002451 /* Seagate Travan drives do not support DSC overlap. */
Bartlomiej Zolnierkiewicz4dde4492008-10-10 22:39:19 +02002452 if (strstr((char *)&drive->id[ATA_ID_PROD], "Seagate STT3401"))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002453 drive->dsc_overlap = 0;
2454 tape->minor = minor;
2455 tape->name[0] = 'h';
2456 tape->name[1] = 't';
2457 tape->name[2] = '0' + minor;
Borislav Petkov54abf372008-02-06 02:57:52 +01002458 tape->chrdev_dir = IDETAPE_DIR_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002459 tape->pc = tape->pc_stack;
Bartlomiej Zolnierkiewicz4dde4492008-10-10 22:39:19 +02002460
2461 *((u16 *)&gcw) = drive->id[ATA_ID_CONFIG];
Borislav Petkov71071b82008-02-06 02:57:53 +01002462
2463 /* Command packet DRQ type */
2464 if (((gcw[0] & 0x60) >> 5) == 1)
Borislav Petkovf2e3ab52008-07-23 19:56:01 +02002465 set_bit(IDE_AFLAG_DRQ_INTERRUPT, &drive->atapi_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002466
Linus Torvalds1da177e2005-04-16 15:20:36 -07002467 idetape_get_inquiry_results(drive);
2468 idetape_get_mode_sense_results(drive);
Borislav Petkov3cffb9c2008-02-02 19:56:50 +01002469 ide_tape_get_bsize_from_bdesc(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002470 tape->user_bs_factor = 1;
Borislav Petkovf73850a2008-04-27 15:38:33 +02002471 tape->buffer_size = *ctl * tape->blk_size;
2472 while (tape->buffer_size > 0xffff) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002473 printk(KERN_NOTICE "ide-tape: decreasing stage size\n");
Borislav Petkovb6422012008-02-02 19:56:49 +01002474 *ctl /= 2;
Borislav Petkovf73850a2008-04-27 15:38:33 +02002475 tape->buffer_size = *ctl * tape->blk_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002476 }
Borislav Petkovf73850a2008-04-27 15:38:33 +02002477 buffer_size = tape->buffer_size;
Borislav Petkova997a432008-04-27 15:38:33 +02002478 tape->pages_per_buffer = buffer_size / PAGE_SIZE;
Borislav Petkovf73850a2008-04-27 15:38:33 +02002479 if (buffer_size % PAGE_SIZE) {
Borislav Petkova997a432008-04-27 15:38:33 +02002480 tape->pages_per_buffer++;
Borislav Petkovf73850a2008-04-27 15:38:33 +02002481 tape->excess_bh_size = PAGE_SIZE - buffer_size % PAGE_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002482 }
2483
Borislav Petkov83042b22008-04-27 15:38:27 +02002484 /* select the "best" DSC read/write polling freq */
Borislav Petkovb6422012008-02-02 19:56:49 +01002485 speed = max(*(u16 *)&tape->caps[14], *(u16 *)&tape->caps[8]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002486
Borislav Petkovf73850a2008-04-27 15:38:33 +02002487 t = (IDETAPE_FIFO_THRESHOLD * tape->buffer_size * HZ) / (speed * 1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002488
2489 /*
Borislav Petkov3c98bf32008-02-06 02:57:53 +01002490 * Ensure that the number we got makes sense; limit it within
2491 * IDETAPE_DSC_RW_MIN and IDETAPE_DSC_RW_MAX.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002492 */
Harvey Harrisona792bd52008-07-15 21:21:41 +02002493 tape->best_dsc_rw_freq = clamp_t(unsigned long, t, IDETAPE_DSC_RW_MIN,
2494 IDETAPE_DSC_RW_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002495 printk(KERN_INFO "ide-tape: %s <-> %s: %dKBps, %d*%dkB buffer, "
Borislav Petkov83042b22008-04-27 15:38:27 +02002496 "%lums tDSC%s\n",
Borislav Petkovb6422012008-02-02 19:56:49 +01002497 drive->name, tape->name, *(u16 *)&tape->caps[14],
Borislav Petkovf73850a2008-04-27 15:38:33 +02002498 (*(u16 *)&tape->caps[16] * 512) / tape->buffer_size,
2499 tape->buffer_size / 1024,
Borislav Petkov54bb2072008-02-06 02:57:52 +01002500 tape->best_dsc_rw_freq * 1000 / HZ,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002501 drive->using_dma ? ", DMA":"");
2502
Bartlomiej Zolnierkiewicz1e874f42008-10-10 22:39:27 +02002503 ide_proc_register_driver(drive, tape->driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002504}
2505
Russell King4031bbe2006-01-06 11:41:00 +00002506static void ide_tape_remove(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002507{
2508 idetape_tape_t *tape = drive->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002509
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +02002510 ide_proc_unregister_driver(drive, tape->driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002511
2512 ide_unregister_region(tape->disk);
2513
2514 ide_tape_put(tape);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002515}
2516
2517static void ide_tape_release(struct kref *kref)
2518{
2519 struct ide_tape_obj *tape = to_ide_tape(kref);
2520 ide_drive_t *drive = tape->drive;
2521 struct gendisk *g = tape->disk;
2522
Borislav Petkov01a63aeb2008-04-27 15:38:34 +02002523 BUG_ON(tape->merge_bh_size);
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02002524
Linus Torvalds1da177e2005-04-16 15:20:36 -07002525 drive->dsc_overlap = 0;
2526 drive->driver_data = NULL;
Tony Jonesdbc12722007-09-25 02:03:03 +02002527 device_destroy(idetape_sysfs_class, MKDEV(IDETAPE_MAJOR, tape->minor));
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002528 device_destroy(idetape_sysfs_class,
2529 MKDEV(IDETAPE_MAJOR, tape->minor + 128));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002530 idetape_devs[tape->minor] = NULL;
2531 g->private_data = NULL;
2532 put_disk(g);
2533 kfree(tape);
2534}
2535
Bartlomiej Zolnierkiewiczecfd80e2007-05-10 00:01:09 +02002536#ifdef CONFIG_IDE_PROC_FS
Linus Torvalds1da177e2005-04-16 15:20:36 -07002537static int proc_idetape_read_name
2538 (char *page, char **start, off_t off, int count, int *eof, void *data)
2539{
2540 ide_drive_t *drive = (ide_drive_t *) data;
2541 idetape_tape_t *tape = drive->driver_data;
2542 char *out = page;
2543 int len;
2544
2545 len = sprintf(out, "%s\n", tape->name);
2546 PROC_IDE_READ_RETURN(page, start, off, count, eof, len);
2547}
2548
2549static ide_proc_entry_t idetape_proc[] = {
2550 { "capacity", S_IFREG|S_IRUGO, proc_ide_read_capacity, NULL },
2551 { "name", S_IFREG|S_IRUGO, proc_idetape_read_name, NULL },
2552 { NULL, 0, NULL, NULL }
2553};
Linus Torvalds1da177e2005-04-16 15:20:36 -07002554#endif
2555
Russell King4031bbe2006-01-06 11:41:00 +00002556static int ide_tape_probe(ide_drive_t *);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002557
Linus Torvalds1da177e2005-04-16 15:20:36 -07002558static ide_driver_t idetape_driver = {
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02002559 .gen_driver = {
Laurent Riffard4ef3b8f2005-11-18 22:15:40 +01002560 .owner = THIS_MODULE,
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02002561 .name = "ide-tape",
2562 .bus = &ide_bus_type,
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02002563 },
Russell King4031bbe2006-01-06 11:41:00 +00002564 .probe = ide_tape_probe,
2565 .remove = ide_tape_remove,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002566 .version = IDETAPE_VERSION,
2567 .media = ide_tape,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002568 .do_request = idetape_do_request,
2569 .end_request = idetape_end_request,
2570 .error = __ide_error,
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +02002571#ifdef CONFIG_IDE_PROC_FS
Linus Torvalds1da177e2005-04-16 15:20:36 -07002572 .proc = idetape_proc,
Bartlomiej Zolnierkiewicz8185d5a2008-10-10 22:39:28 +02002573 .settings = idetape_settings,
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +02002574#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002575};
2576
Borislav Petkov3c98bf32008-02-06 02:57:53 +01002577/* Our character device supporting functions, passed to register_chrdev. */
Arjan van de Ven2b8693c2007-02-12 00:55:32 -08002578static const struct file_operations idetape_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002579 .owner = THIS_MODULE,
2580 .read = idetape_chrdev_read,
2581 .write = idetape_chrdev_write,
2582 .ioctl = idetape_chrdev_ioctl,
2583 .open = idetape_chrdev_open,
2584 .release = idetape_chrdev_release,
2585};
2586
2587static int idetape_open(struct inode *inode, struct file *filp)
2588{
2589 struct gendisk *disk = inode->i_bdev->bd_disk;
2590 struct ide_tape_obj *tape;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002591
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002592 tape = ide_tape_get(disk);
2593 if (!tape)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002594 return -ENXIO;
2595
Linus Torvalds1da177e2005-04-16 15:20:36 -07002596 return 0;
2597}
2598
2599static int idetape_release(struct inode *inode, struct file *filp)
2600{
2601 struct gendisk *disk = inode->i_bdev->bd_disk;
2602 struct ide_tape_obj *tape = ide_tape_g(disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002603
2604 ide_tape_put(tape);
2605
2606 return 0;
2607}
2608
2609static int idetape_ioctl(struct inode *inode, struct file *file,
2610 unsigned int cmd, unsigned long arg)
2611{
2612 struct block_device *bdev = inode->i_bdev;
2613 struct ide_tape_obj *tape = ide_tape_g(bdev->bd_disk);
2614 ide_drive_t *drive = tape->drive;
2615 int err = generic_ide_ioctl(drive, file, bdev, cmd, arg);
2616 if (err == -EINVAL)
2617 err = idetape_blkdev_ioctl(drive, cmd, arg);
2618 return err;
2619}
2620
2621static struct block_device_operations idetape_block_ops = {
2622 .owner = THIS_MODULE,
2623 .open = idetape_open,
2624 .release = idetape_release,
2625 .ioctl = idetape_ioctl,
2626};
2627
Russell King4031bbe2006-01-06 11:41:00 +00002628static int ide_tape_probe(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002629{
2630 idetape_tape_t *tape;
2631 struct gendisk *g;
2632 int minor;
2633
2634 if (!strstr("ide-tape", drive->driver_req))
2635 goto failed;
Bartlomiej Zolnierkiewicz2a924662008-10-10 22:39:24 +02002636
Linus Torvalds1da177e2005-04-16 15:20:36 -07002637 if (drive->media != ide_tape)
2638 goto failed;
Bartlomiej Zolnierkiewicz2a924662008-10-10 22:39:24 +02002639
Bartlomiej Zolnierkiewicz51509ee2008-10-10 22:39:34 +02002640 if (drive->id_read == 1 && !ide_check_atapi_device(drive, DRV_NAME)) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002641 printk(KERN_ERR "ide-tape: %s: not supported by this version of"
2642 " the driver\n", drive->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002643 goto failed;
2644 }
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002645 tape = kzalloc(sizeof(idetape_tape_t), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002646 if (tape == NULL) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002647 printk(KERN_ERR "ide-tape: %s: Can't allocate a tape struct\n",
2648 drive->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002649 goto failed;
2650 }
2651
2652 g = alloc_disk(1 << PARTN_BITS);
2653 if (!g)
2654 goto out_free_tape;
2655
2656 ide_init_disk(g, drive);
2657
Linus Torvalds1da177e2005-04-16 15:20:36 -07002658 kref_init(&tape->kref);
2659
2660 tape->drive = drive;
2661 tape->driver = &idetape_driver;
2662 tape->disk = g;
2663
2664 g->private_data = &tape->driver;
2665
2666 drive->driver_data = tape;
2667
Arjan van de Vencf8b8972006-03-23 03:00:45 -08002668 mutex_lock(&idetape_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002669 for (minor = 0; idetape_devs[minor]; minor++)
2670 ;
2671 idetape_devs[minor] = tape;
Arjan van de Vencf8b8972006-03-23 03:00:45 -08002672 mutex_unlock(&idetape_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002673
2674 idetape_setup(drive, tape, minor);
2675
Greg Kroah-Hartman6ecaaf92008-05-21 12:52:33 -07002676 device_create_drvdata(idetape_sysfs_class, &drive->gendev,
2677 MKDEV(IDETAPE_MAJOR, minor), NULL,
2678 "%s", tape->name);
2679 device_create_drvdata(idetape_sysfs_class, &drive->gendev,
2680 MKDEV(IDETAPE_MAJOR, minor + 128), NULL,
2681 "n%s", tape->name);
Will Dysond5dee802005-09-16 02:55:07 -07002682
Linus Torvalds1da177e2005-04-16 15:20:36 -07002683 g->fops = &idetape_block_ops;
2684 ide_register_region(g);
2685
2686 return 0;
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02002687
Linus Torvalds1da177e2005-04-16 15:20:36 -07002688out_free_tape:
2689 kfree(tape);
2690failed:
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02002691 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002692}
2693
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002694static void __exit idetape_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002695{
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02002696 driver_unregister(&idetape_driver.gen_driver);
Will Dysond5dee802005-09-16 02:55:07 -07002697 class_destroy(idetape_sysfs_class);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002698 unregister_chrdev(IDETAPE_MAJOR, "ht");
2699}
2700
Bartlomiej Zolnierkiewicz17514e82005-11-19 22:24:35 +01002701static int __init idetape_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002702{
Will Dysond5dee802005-09-16 02:55:07 -07002703 int error = 1;
2704 idetape_sysfs_class = class_create(THIS_MODULE, "ide_tape");
2705 if (IS_ERR(idetape_sysfs_class)) {
2706 idetape_sysfs_class = NULL;
2707 printk(KERN_ERR "Unable to create sysfs class for ide tapes\n");
2708 error = -EBUSY;
2709 goto out;
2710 }
2711
Linus Torvalds1da177e2005-04-16 15:20:36 -07002712 if (register_chrdev(IDETAPE_MAJOR, "ht", &idetape_fops)) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002713 printk(KERN_ERR "ide-tape: Failed to register chrdev"
2714 " interface\n");
Will Dysond5dee802005-09-16 02:55:07 -07002715 error = -EBUSY;
2716 goto out_free_class;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002717 }
Will Dysond5dee802005-09-16 02:55:07 -07002718
2719 error = driver_register(&idetape_driver.gen_driver);
2720 if (error)
2721 goto out_free_driver;
2722
2723 return 0;
2724
2725out_free_driver:
2726 driver_unregister(&idetape_driver.gen_driver);
2727out_free_class:
2728 class_destroy(idetape_sysfs_class);
2729out:
2730 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002731}
2732
Kay Sievers263756e2005-12-12 18:03:44 +01002733MODULE_ALIAS("ide:*m-tape*");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002734module_init(idetape_init);
2735module_exit(idetape_exit);
2736MODULE_ALIAS_CHARDEV_MAJOR(IDETAPE_MAJOR);
Borislav Petkov9c145762008-02-06 02:57:54 +01002737MODULE_DESCRIPTION("ATAPI Streaming TAPE Driver");
2738MODULE_LICENSE("GPL");