blob: dd3533505e3b57ef40d73b9d49ab8b54887fc31c [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),
Bartlomiej Zolnierkiewicz394a4c22008-10-10 22:39:35 +020059 /* buffer alloc info (pc_stack) */
60 DBG_PC_STACK = (1 << 4),
Borislav Petkov8004a8c2008-02-06 02:57:51 +010061};
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/*
Bartlomiej Zolnierkiewicz394a4c22008-10-10 22:39:35 +020092 * In various places in the driver, we need to allocate storage for packet
93 * commands, which will remain valid while we leave the driver to wait for
94 * an interrupt or a timeout event.
Linus Torvalds1da177e2005-04-16 15:20:36 -070095 */
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;
Bartlomiej Zolnierkiewicz394a4c22008-10-10 22:39:35 +0200233
234 struct request request_sense_rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235
236 /*
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100237 * DSC polling variables.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100239 * While polling for DSC we use postponed_rq to postpone the current
240 * request so that ide.c will be able to service pending requests on the
241 * other device. Note that at most we will have only one DSC (usually
Borislav Petkov5bd50dc2008-04-27 15:38:28 +0200242 * data transfer) request in the device request queue.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 */
244 struct request *postponed_rq;
245 /* The time in which we started polling for DSC */
246 unsigned long dsc_polling_start;
247 /* Timer used to poll for dsc */
248 struct timer_list dsc_timer;
249 /* Read/Write dsc polling frequency */
Borislav Petkov54bb2072008-02-06 02:57:52 +0100250 unsigned long best_dsc_rw_freq;
251 unsigned long dsc_poll_freq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 unsigned long dsc_timeout;
253
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100254 /* Read position information */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 u8 partition;
256 /* Current block */
Borislav Petkov54bb2072008-02-06 02:57:52 +0100257 unsigned int first_frame;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100259 /* Last error information */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 u8 sense_key, asc, ascq;
261
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100262 /* Character device operation */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 unsigned int minor;
264 /* device name */
265 char name[4];
266 /* Current character device data transfer direction */
Borislav Petkov54abf372008-02-06 02:57:52 +0100267 u8 chrdev_dir;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268
Borislav Petkov54bb2072008-02-06 02:57:52 +0100269 /* tape block size, usually 512 or 1024 bytes */
270 unsigned short blk_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 int user_bs_factor;
Borislav Petkovb6422012008-02-02 19:56:49 +0100272
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 /* Copy of the tape's Capabilities and Mechanical Page */
Borislav Petkovb6422012008-02-02 19:56:49 +0100274 u8 caps[20];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275
276 /*
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100277 * Active data transfer request parameters.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100279 * At most, there is only one ide-tape originated data transfer request
280 * in the device request queue. This allows ide.c to easily service
281 * requests from the other device when we postpone our active request.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 */
Borislav Petkov83042b22008-04-27 15:38:27 +0200283
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100284 /* Data buffer size chosen based on the tape's recommendation */
Borislav Petkovf73850a2008-04-27 15:38:33 +0200285 int buffer_size;
Borislav Petkov077e3bd2008-04-27 15:38:34 +0200286 /* merge buffer */
287 struct idetape_bh *merge_bh;
Borislav Petkov01a63aeb2008-04-27 15:38:34 +0200288 /* size of the merge buffer */
289 int merge_bh_size;
Borislav Petkov077e3bd2008-04-27 15:38:34 +0200290 /* pointer to current buffer head within the merge buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 struct idetape_bh *bh;
292 char *b_data;
293 int b_count;
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100294
Borislav Petkova997a432008-04-27 15:38:33 +0200295 int pages_per_buffer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 /* Wasted space in each stage */
297 int excess_bh_size;
298
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 /* protects the ide-tape queue */
Borislav Petkov54bb2072008-02-06 02:57:52 +0100300 spinlock_t lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100302 /* Measures average tape speed */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303 unsigned long avg_time;
304 int avg_size;
305 int avg_speed;
306
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 /* the door is currently locked */
308 int door_locked;
309 /* the tape hardware is write protected */
310 char drv_write_prot;
311 /* the tape is write protected (hardware or opened as read-only) */
312 char write_prot;
313
Borislav Petkov8004a8c2008-02-06 02:57:51 +0100314 u32 debug_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315} idetape_tape_t;
316
Arjan van de Vencf8b8972006-03-23 03:00:45 -0800317static DEFINE_MUTEX(idetape_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318
Will Dysond5dee802005-09-16 02:55:07 -0700319static struct class *idetape_sysfs_class;
320
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321#define to_ide_tape(obj) container_of(obj, struct ide_tape_obj, kref)
322
323#define ide_tape_g(disk) \
324 container_of((disk)->private_data, struct ide_tape_obj, driver)
325
Bartlomiej Zolnierkiewicz08da5912008-07-24 22:53:15 +0200326static void ide_tape_release(struct kref *);
327
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328static struct ide_tape_obj *ide_tape_get(struct gendisk *disk)
329{
330 struct ide_tape_obj *tape = NULL;
331
Arjan van de Vencf8b8972006-03-23 03:00:45 -0800332 mutex_lock(&idetape_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 tape = ide_tape_g(disk);
Bartlomiej Zolnierkiewicz08da5912008-07-24 22:53:15 +0200334 if (tape) {
Bartlomiej Zolnierkiewiczd3e33ff2008-08-05 18:16:59 +0200335 if (ide_device_get(tape->drive))
Bartlomiej Zolnierkiewicz08da5912008-07-24 22:53:15 +0200336 tape = NULL;
Bartlomiej Zolnierkiewiczd3e33ff2008-08-05 18:16:59 +0200337 else
338 kref_get(&tape->kref);
Bartlomiej Zolnierkiewicz08da5912008-07-24 22:53:15 +0200339 }
Arjan van de Vencf8b8972006-03-23 03:00:45 -0800340 mutex_unlock(&idetape_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 return tape;
342}
343
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344static void ide_tape_put(struct ide_tape_obj *tape)
345{
Bartlomiej Zolnierkiewiczd3e33ff2008-08-05 18:16:59 +0200346 ide_drive_t *drive = tape->drive;
347
Arjan van de Vencf8b8972006-03-23 03:00:45 -0800348 mutex_lock(&idetape_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 kref_put(&tape->kref, ide_tape_release);
Bartlomiej Zolnierkiewiczd3e33ff2008-08-05 18:16:59 +0200350 ide_device_put(drive);
Arjan van de Vencf8b8972006-03-23 03:00:45 -0800351 mutex_unlock(&idetape_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352}
353
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100355 * The variables below are used for the character device interface. Additional
356 * state variables are defined in our ide_drive_t structure.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100358static struct ide_tape_obj *idetape_devs[MAX_HWIFS * MAX_DRIVES];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359
360#define ide_tape_f(file) ((file)->private_data)
361
362static struct ide_tape_obj *ide_tape_chrdev_get(unsigned int i)
363{
364 struct ide_tape_obj *tape = NULL;
365
Arjan van de Vencf8b8972006-03-23 03:00:45 -0800366 mutex_lock(&idetape_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 tape = idetape_devs[i];
368 if (tape)
369 kref_get(&tape->kref);
Arjan van de Vencf8b8972006-03-23 03:00:45 -0800370 mutex_unlock(&idetape_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 return tape;
372}
373
Borislav Petkovd236d742008-04-18 00:46:27 +0200374static void idetape_input_buffers(ide_drive_t *drive, struct ide_atapi_pc *pc,
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100375 unsigned int bcount)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376{
377 struct idetape_bh *bh = pc->bh;
378 int count;
379
380 while (bcount) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 if (bh == NULL) {
382 printk(KERN_ERR "ide-tape: bh == NULL in "
383 "idetape_input_buffers\n");
Bartlomiej Zolnierkiewicz9f87abe2008-04-28 23:44:41 +0200384 ide_pad_transfer(drive, 0, bcount);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 return;
386 }
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100387 count = min(
388 (unsigned int)(bh->b_size - atomic_read(&bh->b_count)),
389 bcount);
Bartlomiej Zolnierkiewicz374e0422008-07-23 19:55:56 +0200390 drive->hwif->tp_ops->input_data(drive, NULL, bh->b_data +
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100391 atomic_read(&bh->b_count), count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 bcount -= count;
393 atomic_add(count, &bh->b_count);
394 if (atomic_read(&bh->b_count) == bh->b_size) {
395 bh = bh->b_reqnext;
396 if (bh)
397 atomic_set(&bh->b_count, 0);
398 }
399 }
400 pc->bh = bh;
401}
402
Borislav Petkovd236d742008-04-18 00:46:27 +0200403static void idetape_output_buffers(ide_drive_t *drive, struct ide_atapi_pc *pc,
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100404 unsigned int bcount)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405{
406 struct idetape_bh *bh = pc->bh;
407 int count;
408
409 while (bcount) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 if (bh == NULL) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100411 printk(KERN_ERR "ide-tape: bh == NULL in %s\n",
412 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 return;
414 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 count = min((unsigned int)pc->b_count, (unsigned int)bcount);
Bartlomiej Zolnierkiewicz374e0422008-07-23 19:55:56 +0200416 drive->hwif->tp_ops->output_data(drive, NULL, pc->b_data, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 bcount -= count;
418 pc->b_data += count;
419 pc->b_count -= count;
420 if (!pc->b_count) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100421 bh = bh->b_reqnext;
422 pc->bh = bh;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 if (bh) {
424 pc->b_data = bh->b_data;
425 pc->b_count = atomic_read(&bh->b_count);
426 }
427 }
428 }
429}
430
Bartlomiej Zolnierkiewicz646c0cb2008-07-15 21:22:03 +0200431static void idetape_update_buffers(ide_drive_t *drive, struct ide_atapi_pc *pc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432{
433 struct idetape_bh *bh = pc->bh;
434 int count;
Borislav Petkovd236d742008-04-18 00:46:27 +0200435 unsigned int bcount = pc->xferred;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436
Borislav Petkov346331f2008-04-18 00:46:26 +0200437 if (pc->flags & PC_FLAG_WRITING)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 return;
439 while (bcount) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 if (bh == NULL) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100441 printk(KERN_ERR "ide-tape: bh == NULL in %s\n",
442 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 return;
444 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 count = min((unsigned int)bh->b_size, (unsigned int)bcount);
446 atomic_set(&bh->b_count, count);
447 if (atomic_read(&bh->b_count) == bh->b_size)
448 bh = bh->b_reqnext;
449 bcount -= count;
450 }
451 pc->bh = bh;
452}
453
454/*
455 * idetape_next_pc_storage returns a pointer to a place in which we can
456 * safely store a packet command, even though we intend to leave the
457 * driver. A storage space for a maximum of IDETAPE_PC_STACK packet
458 * commands is allocated at initialization time.
459 */
Borislav Petkovd236d742008-04-18 00:46:27 +0200460static struct ide_atapi_pc *idetape_next_pc_storage(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461{
462 idetape_tape_t *tape = drive->driver_data;
463
Bartlomiej Zolnierkiewicz394a4c22008-10-10 22:39:35 +0200464 debug_log(DBG_PC_STACK, "pc_stack_index=%d\n", tape->pc_stack_index);
Borislav Petkov8004a8c2008-02-06 02:57:51 +0100465
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 if (tape->pc_stack_index == IDETAPE_PC_STACK)
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100467 tape->pc_stack_index = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 return (&tape->pc_stack[tape->pc_stack_index++]);
469}
470
471/*
Borislav Petkov1b5db432008-02-02 19:56:48 +0100472 * called on each failed packet command retry to analyze the request sense. We
473 * currently do not utilize this information.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 */
Borislav Petkov1b5db432008-02-02 19:56:48 +0100475static void idetape_analyze_error(ide_drive_t *drive, u8 *sense)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476{
477 idetape_tape_t *tape = drive->driver_data;
Borislav Petkovd236d742008-04-18 00:46:27 +0200478 struct ide_atapi_pc *pc = tape->failed_pc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479
Borislav Petkov1b5db432008-02-02 19:56:48 +0100480 tape->sense_key = sense[2] & 0xF;
481 tape->asc = sense[12];
482 tape->ascq = sense[13];
Borislav Petkov8004a8c2008-02-06 02:57:51 +0100483
484 debug_log(DBG_ERR, "pc = %x, sense key = %x, asc = %x, ascq = %x\n",
485 pc->c[0], tape->sense_key, tape->asc, tape->ascq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486
Borislav Petkovd236d742008-04-18 00:46:27 +0200487 /* Correct pc->xferred by asking the tape. */
Borislav Petkov346331f2008-04-18 00:46:26 +0200488 if (pc->flags & PC_FLAG_DMA_ERROR) {
Borislav Petkovd236d742008-04-18 00:46:27 +0200489 pc->xferred = pc->req_xfer -
Borislav Petkov54bb2072008-02-06 02:57:52 +0100490 tape->blk_size *
Harvey Harrison5d0cc8a2008-07-15 21:21:41 +0200491 get_unaligned_be32(&sense[3]);
Bartlomiej Zolnierkiewicz646c0cb2008-07-15 21:22:03 +0200492 idetape_update_buffers(drive, pc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 }
494
495 /*
496 * If error was the result of a zero-length read or write command,
497 * with sense key=5, asc=0x22, ascq=0, let it slide. Some drives
498 * (i.e. Seagate STT3401A Travan) don't support 0-length read/writes.
499 */
Borislav Petkov90699ce2008-02-02 19:56:50 +0100500 if ((pc->c[0] == READ_6 || pc->c[0] == WRITE_6)
Borislav Petkov1b5db432008-02-02 19:56:48 +0100501 /* length == 0 */
502 && pc->c[4] == 0 && pc->c[3] == 0 && pc->c[2] == 0) {
503 if (tape->sense_key == 5) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 /* don't report an error, everything's ok */
505 pc->error = 0;
506 /* don't retry read/write */
Borislav Petkov346331f2008-04-18 00:46:26 +0200507 pc->flags |= PC_FLAG_ABORT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 }
509 }
Borislav Petkov90699ce2008-02-02 19:56:50 +0100510 if (pc->c[0] == READ_6 && (sense[2] & 0x80)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 pc->error = IDETAPE_ERROR_FILEMARK;
Borislav Petkov346331f2008-04-18 00:46:26 +0200512 pc->flags |= PC_FLAG_ABORT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 }
Borislav Petkov90699ce2008-02-02 19:56:50 +0100514 if (pc->c[0] == WRITE_6) {
Borislav Petkov1b5db432008-02-02 19:56:48 +0100515 if ((sense[2] & 0x40) || (tape->sense_key == 0xd
516 && tape->asc == 0x0 && tape->ascq == 0x2)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 pc->error = IDETAPE_ERROR_EOD;
Borislav Petkov346331f2008-04-18 00:46:26 +0200518 pc->flags |= PC_FLAG_ABORT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 }
520 }
Borislav Petkov90699ce2008-02-02 19:56:50 +0100521 if (pc->c[0] == READ_6 || pc->c[0] == WRITE_6) {
Borislav Petkov1b5db432008-02-02 19:56:48 +0100522 if (tape->sense_key == 8) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 pc->error = IDETAPE_ERROR_EOD;
Borislav Petkov346331f2008-04-18 00:46:26 +0200524 pc->flags |= PC_FLAG_ABORT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 }
Borislav Petkov346331f2008-04-18 00:46:26 +0200526 if (!(pc->flags & PC_FLAG_ABORT) &&
Borislav Petkovd236d742008-04-18 00:46:27 +0200527 pc->xferred)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 pc->retries = IDETAPE_MAX_PC_RETRIES + 1;
529 }
530}
531
Borislav Petkovd01dbc32008-04-27 15:38:33 +0200532/* Free data buffers completely. */
Borislav Petkov077e3bd2008-04-27 15:38:34 +0200533static void ide_tape_kfree_buffer(idetape_tape_t *tape)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534{
Borislav Petkov077e3bd2008-04-27 15:38:34 +0200535 struct idetape_bh *prev_bh, *bh = tape->merge_bh;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536
Borislav Petkovd01dbc32008-04-27 15:38:33 +0200537 while (bh) {
538 u32 size = bh->b_size;
539
540 while (size) {
541 unsigned int order = fls(size >> PAGE_SHIFT)-1;
542
543 if (bh->b_data)
544 free_pages((unsigned long)bh->b_data, order);
545
546 size &= (order-1);
547 bh->b_data += (1 << order) * PAGE_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 }
549 prev_bh = bh;
550 bh = bh->b_reqnext;
551 kfree(prev_bh);
552 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553}
554
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555static int idetape_end_request(ide_drive_t *drive, int uptodate, int nr_sects)
556{
557 struct request *rq = HWGROUP(drive)->rq;
558 idetape_tape_t *tape = drive->driver_data;
559 unsigned long flags;
560 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561
Borislav Petkov8004a8c2008-02-06 02:57:51 +0100562 debug_log(DBG_PROCS, "Enter %s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563
564 switch (uptodate) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100565 case 0: error = IDETAPE_ERROR_GENERAL; break;
566 case 1: error = 0; break;
567 default: error = uptodate;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 }
569 rq->errors = error;
570 if (error)
571 tape->failed_pc = NULL;
572
Bartlomiej Zolnierkiewicz36872212008-01-26 20:13:10 +0100573 if (!blk_special_request(rq)) {
574 ide_end_request(drive, uptodate, nr_sects);
575 return 0;
576 }
577
Borislav Petkov54bb2072008-02-06 02:57:52 +0100578 spin_lock_irqsave(&tape->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 ide_end_drive_cmd(drive, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581
Borislav Petkov54bb2072008-02-06 02:57:52 +0100582 spin_unlock_irqrestore(&tape->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 return 0;
584}
585
Bartlomiej Zolnierkiewicz92f5daf2008-07-15 21:21:55 +0200586static void ide_tape_callback(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587{
588 idetape_tape_t *tape = drive->driver_data;
Bartlomiej Zolnierkiewicz5985e6a2008-07-15 21:21:55 +0200589 struct ide_atapi_pc *pc = tape->pc;
590 int uptodate = pc->error ? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591
Borislav Petkov8004a8c2008-02-06 02:57:51 +0100592 debug_log(DBG_PROCS, "Enter %s\n", __func__);
593
Bartlomiej Zolnierkiewiczdd2e9a02008-07-15 21:22:01 +0200594 if (tape->failed_pc == pc)
595 tape->failed_pc = NULL;
596
Bartlomiej Zolnierkiewicz5985e6a2008-07-15 21:21:55 +0200597 if (pc->c[0] == REQUEST_SENSE) {
598 if (uptodate)
599 idetape_analyze_error(drive, pc->buf);
600 else
601 printk(KERN_ERR "ide-tape: Error in REQUEST SENSE "
602 "itself - Aborting request!\n");
603 } else if (pc->c[0] == READ_6 || pc->c[0] == WRITE_6) {
604 struct request *rq = drive->hwif->hwgroup->rq;
605 int blocks = pc->xferred / tape->blk_size;
606
607 tape->avg_size += blocks * tape->blk_size;
608
609 if (time_after_eq(jiffies, tape->avg_time + HZ)) {
610 tape->avg_speed = tape->avg_size * HZ /
611 (jiffies - tape->avg_time) / 1024;
612 tape->avg_size = 0;
613 tape->avg_time = jiffies;
614 }
615
616 tape->first_frame += blocks;
617 rq->current_nr_sectors -= blocks;
618
619 if (pc->error)
620 uptodate = pc->error;
621 } else if (pc->c[0] == READ_POSITION && uptodate) {
622 u8 *readpos = tape->pc->buf;
623
624 debug_log(DBG_SENSE, "BOP - %s\n",
625 (readpos[0] & 0x80) ? "Yes" : "No");
626 debug_log(DBG_SENSE, "EOP - %s\n",
627 (readpos[0] & 0x40) ? "Yes" : "No");
628
629 if (readpos[0] & 0x4) {
630 printk(KERN_INFO "ide-tape: Block location is unknown"
631 "to the tape\n");
Borislav Petkovf2e3ab52008-07-23 19:56:01 +0200632 clear_bit(IDE_AFLAG_ADDRESS_VALID, &drive->atapi_flags);
Bartlomiej Zolnierkiewicz5985e6a2008-07-15 21:21:55 +0200633 uptodate = 0;
634 } else {
635 debug_log(DBG_SENSE, "Block Location - %u\n",
Harvey Harrisoncd740ab2008-07-24 22:53:33 +0200636 be32_to_cpup((__be32 *)&readpos[4]));
Bartlomiej Zolnierkiewicz5985e6a2008-07-15 21:21:55 +0200637
638 tape->partition = readpos[1];
Harvey Harrisoncd740ab2008-07-24 22:53:33 +0200639 tape->first_frame = be32_to_cpup((__be32 *)&readpos[4]);
Borislav Petkovf2e3ab52008-07-23 19:56:01 +0200640 set_bit(IDE_AFLAG_ADDRESS_VALID, &drive->atapi_flags);
Bartlomiej Zolnierkiewicz5985e6a2008-07-15 21:21:55 +0200641 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 }
Bartlomiej Zolnierkiewicz5985e6a2008-07-15 21:21:55 +0200643
644 idetape_end_request(drive, uptodate, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645}
646
Bartlomiej Zolnierkiewicz5985e6a2008-07-15 21:21:55 +0200647static void idetape_init_pc(struct ide_atapi_pc *pc)
648{
649 memset(pc->c, 0, 12);
650 pc->retries = 0;
651 pc->flags = 0;
652 pc->req_xfer = 0;
653 pc->buf = pc->pc_buf;
654 pc->buf_size = IDETAPE_PC_BUFFER_SIZE;
655 pc->bh = NULL;
656 pc->b_data = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657}
658
Borislav Petkovd236d742008-04-18 00:46:27 +0200659static void idetape_create_request_sense_cmd(struct ide_atapi_pc *pc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660{
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100661 idetape_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +0100662 pc->c[0] = REQUEST_SENSE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 pc->c[4] = 20;
Borislav Petkovd236d742008-04-18 00:46:27 +0200664 pc->req_xfer = 20;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665}
666
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100668 * Generate a new packet command request in front of the request queue, before
669 * the current request, so that it will be processed immediately, on the next
Bartlomiej Zolnierkiewicz394a4c22008-10-10 22:39:35 +0200670 * pass through the driver.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671 */
Borislav Petkovd236d742008-04-18 00:46:27 +0200672static void idetape_queue_pc_head(ide_drive_t *drive, struct ide_atapi_pc *pc,
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100673 struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674{
675 struct ide_tape_obj *tape = drive->driver_data;
676
Bartlomiej Zolnierkiewiczf025ffd2008-10-10 22:39:34 +0200677 blk_rq_init(NULL, rq);
678 rq->cmd_type = REQ_TYPE_SPECIAL;
Bartlomiej Zolnierkiewicze8a96aa2008-07-15 21:21:41 +0200679 rq->cmd_flags |= REQ_PREEMPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 rq->buffer = (char *) pc;
681 rq->rq_disk = tape->disk;
Borislav Petkov0014c752008-07-23 19:56:00 +0200682 memcpy(rq->cmd, pc->c, 12);
Bartlomiej Zolnierkiewiczf025ffd2008-10-10 22:39:34 +0200683 rq->cmd[13] = REQ_IDETAPE_PC1;
FUJITA Tomonori63f5abb2008-07-15 21:21:51 +0200684 ide_do_drive_cmd(drive, rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685}
686
687/*
688 * idetape_retry_pc is called when an error was detected during the
689 * last packet command. We queue a request sense packet command in
690 * the head of the request list.
691 */
Bartlomiej Zolnierkiewicz258ec412008-07-15 21:21:55 +0200692static void idetape_retry_pc(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693{
Bartlomiej Zolnierkiewicz394a4c22008-10-10 22:39:35 +0200694 struct ide_tape_obj *tape = drive->driver_data;
695 struct request *rq = &tape->request_sense_rq;
Borislav Petkovd236d742008-04-18 00:46:27 +0200696 struct ide_atapi_pc *pc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697
Bartlomiej Zolnierkiewicz64a57fe2008-02-06 02:57:51 +0100698 (void)ide_read_error(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699 pc = idetape_next_pc_storage(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 idetape_create_request_sense_cmd(pc);
Borislav Petkovf2e3ab52008-07-23 19:56:01 +0200701 set_bit(IDE_AFLAG_IGNORE_DSC, &drive->atapi_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 idetape_queue_pc_head(drive, pc, rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703}
704
705/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100706 * Postpone the current request so that ide.c will be able to service requests
707 * from another device on the same hwgroup while we are polling for DSC.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708 */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100709static void idetape_postpone_request(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710{
711 idetape_tape_t *tape = drive->driver_data;
712
Borislav Petkov8004a8c2008-02-06 02:57:51 +0100713 debug_log(DBG_PROCS, "Enter %s\n", __func__);
714
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 tape->postponed_rq = HWGROUP(drive)->rq;
Borislav Petkov54bb2072008-02-06 02:57:52 +0100716 ide_stall_queue(drive, tape->dsc_poll_freq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717}
718
Bartlomiej Zolnierkiewicz74e63e742008-07-15 21:22:01 +0200719static void ide_tape_handle_dsc(ide_drive_t *drive)
720{
721 idetape_tape_t *tape = drive->driver_data;
722
723 /* Media access command */
724 tape->dsc_polling_start = jiffies;
725 tape->dsc_poll_freq = IDETAPE_DSC_MA_FAST;
726 tape->dsc_timeout = jiffies + IDETAPE_DSC_MA_TIMEOUT;
727 /* Allow ide.c to handle other requests */
728 idetape_postpone_request(drive);
729}
730
Bartlomiej Zolnierkiewicz08424ac2008-07-15 21:22:01 +0200731static void ide_tape_io_buffers(ide_drive_t *drive, struct ide_atapi_pc *pc,
732 unsigned int bcount, int write)
733{
734 if (write)
735 idetape_output_buffers(drive, pc, bcount);
736 else
737 idetape_input_buffers(drive, pc, bcount);
738}
Borislav Petkova1efc852008-02-06 02:57:52 +0100739
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740/*
Borislav Petkova1efc852008-02-06 02:57:52 +0100741 * This is the usual interrupt handler which will be called during a packet
742 * command. We will transfer some of the data (as requested by the drive) and
743 * will re-point interrupt handler to us. When data transfer is finished, we
744 * will act according to the algorithm described before
Borislav Petkov8d06bfa2008-02-06 02:57:53 +0100745 * idetape_issue_pc.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746 */
Borislav Petkova1efc852008-02-06 02:57:52 +0100747static ide_startstop_t idetape_pc_intr(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749 idetape_tape_t *tape = drive->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750
Bartlomiej Zolnierkiewicz646c0cb2008-07-15 21:22:03 +0200751 return ide_pc_intr(drive, tape->pc, idetape_pc_intr, IDETAPE_WAIT_CMD,
752 NULL, idetape_update_buffers, idetape_retry_pc,
753 ide_tape_handle_dsc, ide_tape_io_buffers);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754}
755
756/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100757 * Packet Command Interface
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100759 * The current Packet Command is available in tape->pc, and will not change
760 * until we finish handling it. Each packet command is associated with a
761 * callback function that will be called when the command is finished.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100763 * The handling will be done in three stages:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100765 * 1. idetape_issue_pc will send the packet command to the drive, and will set
766 * the interrupt handler to idetape_pc_intr.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100768 * 2. On each interrupt, idetape_pc_intr will be called. This step will be
769 * repeated until the device signals us that no more interrupts will be issued.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100771 * 3. ATAPI Tape media access commands have immediate status with a delayed
772 * process. In case of a successful initiation of a media access packet command,
773 * the DSC bit will be set when the actual execution of the command is finished.
774 * Since the tape drive will not issue an interrupt, we have to poll for this
775 * event. In this case, we define the request as "low priority request" by
776 * setting rq_status to IDETAPE_RQ_POSTPONED, set a timer to poll for DSC and
777 * exit the driver.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100779 * ide.c will then give higher priority to requests which originate from the
780 * other device, until will change rq_status to RQ_ACTIVE.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100782 * 4. When the packet command is finished, it will be checked for errors.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100784 * 5. In case an error was found, we queue a request sense packet command in
785 * front of the request queue and retry the operation up to
786 * IDETAPE_MAX_PC_RETRIES times.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100788 * 6. In case no error was found, or we decided to give up and not to retry
789 * again, the callback function will be called and then we will handle the next
790 * request.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791 */
792static ide_startstop_t idetape_transfer_pc(ide_drive_t *drive)
793{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794 idetape_tape_t *tape = drive->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795
Bartlomiej Zolnierkiewicz594c16d2008-07-15 21:21:58 +0200796 return ide_transfer_pc(drive, tape->pc, idetape_pc_intr,
797 IDETAPE_WAIT_CMD, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798}
799
Borislav Petkovd236d742008-04-18 00:46:27 +0200800static ide_startstop_t idetape_issue_pc(ide_drive_t *drive,
801 struct ide_atapi_pc *pc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803 idetape_tape_t *tape = drive->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804
Borislav Petkov90699ce2008-02-02 19:56:50 +0100805 if (tape->pc->c[0] == REQUEST_SENSE &&
806 pc->c[0] == REQUEST_SENSE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807 printk(KERN_ERR "ide-tape: possible ide-tape.c bug - "
808 "Two request sense in serial were issued\n");
809 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810
Borislav Petkov90699ce2008-02-02 19:56:50 +0100811 if (tape->failed_pc == NULL && pc->c[0] != REQUEST_SENSE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812 tape->failed_pc = pc;
813 /* Set the current packet command */
814 tape->pc = pc;
815
816 if (pc->retries > IDETAPE_MAX_PC_RETRIES ||
Borislav Petkov346331f2008-04-18 00:46:26 +0200817 (pc->flags & PC_FLAG_ABORT)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818 /*
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100819 * We will "abort" retrying a packet command in case legitimate
820 * error code was received (crossing a filemark, or end of the
821 * media, for example).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822 */
Borislav Petkov346331f2008-04-18 00:46:26 +0200823 if (!(pc->flags & PC_FLAG_ABORT)) {
Borislav Petkov90699ce2008-02-02 19:56:50 +0100824 if (!(pc->c[0] == TEST_UNIT_READY &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825 tape->sense_key == 2 && tape->asc == 4 &&
826 (tape->ascq == 1 || tape->ascq == 8))) {
827 printk(KERN_ERR "ide-tape: %s: I/O error, "
828 "pc = %2x, key = %2x, "
829 "asc = %2x, ascq = %2x\n",
830 tape->name, pc->c[0],
831 tape->sense_key, tape->asc,
832 tape->ascq);
833 }
834 /* Giving up */
835 pc->error = IDETAPE_ERROR_GENERAL;
836 }
837 tape->failed_pc = NULL;
Borislav Petkov776bb022008-07-23 19:55:59 +0200838 drive->pc_callback(drive);
Bartlomiej Zolnierkiewicz92f5daf2008-07-15 21:21:55 +0200839 return ide_stopped;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840 }
Borislav Petkov8004a8c2008-02-06 02:57:51 +0100841 debug_log(DBG_SENSE, "Retry #%d, cmd = %02X\n", pc->retries, pc->c[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842
843 pc->retries++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844
Bartlomiej Zolnierkiewicz6bf16412008-07-15 21:22:00 +0200845 return ide_issue_pc(drive, pc, idetape_transfer_pc,
846 IDETAPE_WAIT_CMD, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847}
848
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100849/* A mode sense command is used to "sense" tape parameters. */
Borislav Petkovd236d742008-04-18 00:46:27 +0200850static void idetape_create_mode_sense_cmd(struct ide_atapi_pc *pc, u8 page_code)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851{
852 idetape_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +0100853 pc->c[0] = MODE_SENSE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854 if (page_code != IDETAPE_BLOCK_DESCRIPTOR)
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100855 /* DBD = 1 - Don't return block descriptors */
856 pc->c[1] = 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857 pc->c[2] = page_code;
858 /*
859 * Changed pc->c[3] to 0 (255 will at best return unused info).
860 *
861 * For SCSI this byte is defined as subpage instead of high byte
862 * of length and some IDE drives seem to interpret it this way
863 * and return an error when 255 is used.
864 */
865 pc->c[3] = 0;
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100866 /* We will just discard data in that case */
867 pc->c[4] = 255;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868 if (page_code == IDETAPE_BLOCK_DESCRIPTOR)
Borislav Petkovd236d742008-04-18 00:46:27 +0200869 pc->req_xfer = 12;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870 else if (page_code == IDETAPE_CAPABILITIES_PAGE)
Borislav Petkovd236d742008-04-18 00:46:27 +0200871 pc->req_xfer = 24;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872 else
Borislav Petkovd236d742008-04-18 00:46:27 +0200873 pc->req_xfer = 50;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874}
875
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100876static ide_startstop_t idetape_media_access_finished(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877{
Bartlomiej Zolnierkiewiczb73c7ee2008-07-23 19:55:52 +0200878 ide_hwif_t *hwif = drive->hwif;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879 idetape_tape_t *tape = drive->driver_data;
Borislav Petkovd236d742008-04-18 00:46:27 +0200880 struct ide_atapi_pc *pc = tape->pc;
Bartlomiej Zolnierkiewicz22c525b2008-01-25 22:17:11 +0100881 u8 stat;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882
Bartlomiej Zolnierkiewicz374e0422008-07-23 19:55:56 +0200883 stat = hwif->tp_ops->read_status(hwif);
Bartlomiej Zolnierkiewiczc47137a2008-02-06 02:57:51 +0100884
Bartlomiej Zolnierkiewicz3a7d2482008-10-10 22:39:21 +0200885 if (stat & ATA_DSC) {
886 if (stat & ATA_ERR) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887 /* Error detected */
Borislav Petkov90699ce2008-02-02 19:56:50 +0100888 if (pc->c[0] != TEST_UNIT_READY)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889 printk(KERN_ERR "ide-tape: %s: I/O error, ",
890 tape->name);
891 /* Retry operation */
Bartlomiej Zolnierkiewicz258ec412008-07-15 21:21:55 +0200892 idetape_retry_pc(drive);
893 return ide_stopped;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894 }
895 pc->error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896 } else {
897 pc->error = IDETAPE_ERROR_GENERAL;
898 tape->failed_pc = NULL;
899 }
Borislav Petkov776bb022008-07-23 19:55:59 +0200900 drive->pc_callback(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901 return ide_stopped;
902}
903
Borislav Petkovcd2abbf2008-07-15 21:22:03 +0200904static void ide_tape_create_rw_cmd(idetape_tape_t *tape,
Borislav Petkov0014c752008-07-23 19:56:00 +0200905 struct ide_atapi_pc *pc, struct request *rq,
906 u8 opcode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907{
Borislav Petkov0014c752008-07-23 19:56:00 +0200908 struct idetape_bh *bh = (struct idetape_bh *)rq->special;
909 unsigned int length = rq->current_nr_sectors;
910
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911 idetape_init_pc(pc);
Borislav Petkov860ff5e2008-02-02 19:56:50 +0100912 put_unaligned(cpu_to_be32(length), (unsigned int *) &pc->c[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913 pc->c[1] = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914 pc->bh = bh;
Borislav Petkovd236d742008-04-18 00:46:27 +0200915 pc->buf = NULL;
916 pc->buf_size = length * tape->blk_size;
917 pc->req_xfer = pc->buf_size;
Borislav Petkovf73850a2008-04-27 15:38:33 +0200918 if (pc->req_xfer == tape->buffer_size)
Bartlomiej Zolnierkiewicz5e331092008-07-15 21:21:56 +0200919 pc->flags |= PC_FLAG_DMA_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920
Borislav Petkovcd2abbf2008-07-15 21:22:03 +0200921 if (opcode == READ_6) {
922 pc->c[0] = READ_6;
923 atomic_set(&bh->b_count, 0);
924 } else if (opcode == WRITE_6) {
925 pc->c[0] = WRITE_6;
926 pc->flags |= PC_FLAG_WRITING;
927 pc->b_data = bh->b_data;
928 pc->b_count = atomic_read(&bh->b_count);
929 }
Borislav Petkov0014c752008-07-23 19:56:00 +0200930
931 memcpy(rq->cmd, pc->c, 12);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932}
933
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934static ide_startstop_t idetape_do_request(ide_drive_t *drive,
935 struct request *rq, sector_t block)
936{
Bartlomiej Zolnierkiewiczb73c7ee2008-07-23 19:55:52 +0200937 ide_hwif_t *hwif = drive->hwif;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938 idetape_tape_t *tape = drive->driver_data;
Borislav Petkovd236d742008-04-18 00:46:27 +0200939 struct ide_atapi_pc *pc = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940 struct request *postponed_rq = tape->postponed_rq;
Bartlomiej Zolnierkiewicz22c525b2008-01-25 22:17:11 +0100941 u8 stat;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942
Mark de Wever730616b2008-10-10 22:39:17 +0200943 debug_log(DBG_SENSE, "sector: %llu, nr_sectors: %lu,"
944 " current_nr_sectors: %u\n",
945 (unsigned long long)rq->sector, rq->nr_sectors,
946 rq->current_nr_sectors);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947
Jens Axboe4aff5e22006-08-10 08:44:47 +0200948 if (!blk_special_request(rq)) {
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100949 /* We do not support buffer cache originated requests. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950 printk(KERN_NOTICE "ide-tape: %s: Unsupported request in "
Jens Axboe4aff5e22006-08-10 08:44:47 +0200951 "request queue (%d)\n", drive->name, rq->cmd_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952 ide_end_request(drive, 0, 0);
953 return ide_stopped;
954 }
955
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100956 /* Retry a failed packet command */
Bartlomiej Zolnierkiewicz28c72142008-07-15 21:21:59 +0200957 if (tape->failed_pc && tape->pc->c[0] == REQUEST_SENSE) {
958 pc = tape->failed_pc;
959 goto out;
960 }
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100961
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962 if (postponed_rq != NULL)
963 if (rq != postponed_rq) {
964 printk(KERN_ERR "ide-tape: ide-tape.c bug - "
965 "Two DSC requests were queued\n");
966 idetape_end_request(drive, 0, 0);
967 return ide_stopped;
968 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969
970 tape->postponed_rq = NULL;
971
972 /*
973 * If the tape is still busy, postpone our request and service
974 * the other device meanwhile.
975 */
Bartlomiej Zolnierkiewicz374e0422008-07-23 19:55:56 +0200976 stat = hwif->tp_ops->read_status(hwif);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977
Borislav Petkov83dd5732008-07-23 19:56:00 +0200978 if (!drive->dsc_overlap && !(rq->cmd[13] & REQ_IDETAPE_PC2))
Borislav Petkovf2e3ab52008-07-23 19:56:01 +0200979 set_bit(IDE_AFLAG_IGNORE_DSC, &drive->atapi_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980
981 if (drive->post_reset == 1) {
Borislav Petkovf2e3ab52008-07-23 19:56:01 +0200982 set_bit(IDE_AFLAG_IGNORE_DSC, &drive->atapi_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983 drive->post_reset = 0;
984 }
985
Borislav Petkovf2e3ab52008-07-23 19:56:01 +0200986 if (!test_and_clear_bit(IDE_AFLAG_IGNORE_DSC, &drive->atapi_flags) &&
Bartlomiej Zolnierkiewicz3a7d2482008-10-10 22:39:21 +0200987 (stat & ATA_DSC) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988 if (postponed_rq == NULL) {
989 tape->dsc_polling_start = jiffies;
Borislav Petkov54bb2072008-02-06 02:57:52 +0100990 tape->dsc_poll_freq = tape->best_dsc_rw_freq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991 tape->dsc_timeout = jiffies + IDETAPE_DSC_RW_TIMEOUT;
992 } else if (time_after(jiffies, tape->dsc_timeout)) {
993 printk(KERN_ERR "ide-tape: %s: DSC timeout\n",
994 tape->name);
Borislav Petkov83dd5732008-07-23 19:56:00 +0200995 if (rq->cmd[13] & REQ_IDETAPE_PC2) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996 idetape_media_access_finished(drive);
997 return ide_stopped;
998 } else {
999 return ide_do_reset(drive);
1000 }
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001001 } else if (time_after(jiffies,
1002 tape->dsc_polling_start +
1003 IDETAPE_DSC_MA_THRESHOLD))
Borislav Petkov54bb2072008-02-06 02:57:52 +01001004 tape->dsc_poll_freq = IDETAPE_DSC_MA_SLOW;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005 idetape_postpone_request(drive);
1006 return ide_stopped;
1007 }
Borislav Petkov83dd5732008-07-23 19:56:00 +02001008 if (rq->cmd[13] & REQ_IDETAPE_READ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009 pc = idetape_next_pc_storage(drive);
Borislav Petkov0014c752008-07-23 19:56:00 +02001010 ide_tape_create_rw_cmd(tape, pc, rq, READ_6);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011 goto out;
1012 }
Borislav Petkov83dd5732008-07-23 19:56:00 +02001013 if (rq->cmd[13] & REQ_IDETAPE_WRITE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014 pc = idetape_next_pc_storage(drive);
Borislav Petkov0014c752008-07-23 19:56:00 +02001015 ide_tape_create_rw_cmd(tape, pc, rq, WRITE_6);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016 goto out;
1017 }
Borislav Petkov83dd5732008-07-23 19:56:00 +02001018 if (rq->cmd[13] & REQ_IDETAPE_PC1) {
Borislav Petkovd236d742008-04-18 00:46:27 +02001019 pc = (struct ide_atapi_pc *) rq->buffer;
Borislav Petkov83dd5732008-07-23 19:56:00 +02001020 rq->cmd[13] &= ~(REQ_IDETAPE_PC1);
1021 rq->cmd[13] |= REQ_IDETAPE_PC2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022 goto out;
1023 }
Borislav Petkov83dd5732008-07-23 19:56:00 +02001024 if (rq->cmd[13] & REQ_IDETAPE_PC2) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025 idetape_media_access_finished(drive);
1026 return ide_stopped;
1027 }
1028 BUG();
Bartlomiej Zolnierkiewicz28c72142008-07-15 21:21:59 +02001029
Borislav Petkovf2e3ab52008-07-23 19:56:01 +02001030out:
Borislav Petkov8d06bfa2008-02-06 02:57:53 +01001031 return idetape_issue_pc(drive, pc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032}
1033
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034/*
Borislav Petkov41aa1702008-04-27 15:38:32 +02001035 * The function below uses __get_free_pages to allocate a data buffer of size
Borislav Petkovf73850a2008-04-27 15:38:33 +02001036 * tape->buffer_size (or a bit more). We attempt to combine sequential pages as
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001037 * much as possible.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038 *
Borislav Petkov41aa1702008-04-27 15:38:32 +02001039 * It returns a pointer to the newly allocated buffer, or NULL in case of
1040 * failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041 */
Borislav Petkov077e3bd2008-04-27 15:38:34 +02001042static struct idetape_bh *ide_tape_kmalloc_buffer(idetape_tape_t *tape,
1043 int full, int clear)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044{
Borislav Petkov077e3bd2008-04-27 15:38:34 +02001045 struct idetape_bh *prev_bh, *bh, *merge_bh;
Borislav Petkova997a432008-04-27 15:38:33 +02001046 int pages = tape->pages_per_buffer;
Borislav Petkov41aa1702008-04-27 15:38:32 +02001047 unsigned int order, b_allocd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048 char *b_data = NULL;
1049
Borislav Petkov077e3bd2008-04-27 15:38:34 +02001050 merge_bh = kmalloc(sizeof(struct idetape_bh), GFP_KERNEL);
1051 bh = merge_bh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052 if (bh == NULL)
1053 goto abort;
Borislav Petkov41aa1702008-04-27 15:38:32 +02001054
1055 order = fls(pages) - 1;
1056 bh->b_data = (char *) __get_free_pages(GFP_KERNEL, order);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001057 if (!bh->b_data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058 goto abort;
Borislav Petkov41aa1702008-04-27 15:38:32 +02001059 b_allocd = (1 << order) * PAGE_SIZE;
1060 pages &= (order-1);
1061
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062 if (clear)
Borislav Petkov41aa1702008-04-27 15:38:32 +02001063 memset(bh->b_data, 0, b_allocd);
1064 bh->b_reqnext = NULL;
1065 bh->b_size = b_allocd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066 atomic_set(&bh->b_count, full ? bh->b_size : 0);
1067
Borislav Petkov41aa1702008-04-27 15:38:32 +02001068 while (pages) {
1069 order = fls(pages) - 1;
1070 b_data = (char *) __get_free_pages(GFP_KERNEL, order);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001071 if (!b_data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072 goto abort;
Borislav Petkov41aa1702008-04-27 15:38:32 +02001073 b_allocd = (1 << order) * PAGE_SIZE;
1074
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075 if (clear)
Borislav Petkov41aa1702008-04-27 15:38:32 +02001076 memset(b_data, 0, b_allocd);
1077
1078 /* newly allocated page frames below buffer header or ...*/
1079 if (bh->b_data == b_data + b_allocd) {
1080 bh->b_size += b_allocd;
1081 bh->b_data -= b_allocd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082 if (full)
Borislav Petkov41aa1702008-04-27 15:38:32 +02001083 atomic_add(b_allocd, &bh->b_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084 continue;
1085 }
Borislav Petkov41aa1702008-04-27 15:38:32 +02001086 /* they are above the header */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087 if (b_data == bh->b_data + bh->b_size) {
Borislav Petkov41aa1702008-04-27 15:38:32 +02001088 bh->b_size += b_allocd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089 if (full)
Borislav Petkov41aa1702008-04-27 15:38:32 +02001090 atomic_add(b_allocd, &bh->b_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091 continue;
1092 }
1093 prev_bh = bh;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001094 bh = kmalloc(sizeof(struct idetape_bh), GFP_KERNEL);
1095 if (!bh) {
Borislav Petkov41aa1702008-04-27 15:38:32 +02001096 free_pages((unsigned long) b_data, order);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097 goto abort;
1098 }
1099 bh->b_reqnext = NULL;
1100 bh->b_data = b_data;
Borislav Petkov41aa1702008-04-27 15:38:32 +02001101 bh->b_size = b_allocd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102 atomic_set(&bh->b_count, full ? bh->b_size : 0);
1103 prev_bh->b_reqnext = bh;
Borislav Petkov41aa1702008-04-27 15:38:32 +02001104
1105 pages &= (order-1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106 }
Borislav Petkov41aa1702008-04-27 15:38:32 +02001107
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108 bh->b_size -= tape->excess_bh_size;
1109 if (full)
1110 atomic_sub(tape->excess_bh_size, &bh->b_count);
Borislav Petkov077e3bd2008-04-27 15:38:34 +02001111 return merge_bh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112abort:
Borislav Petkov077e3bd2008-04-27 15:38:34 +02001113 ide_tape_kfree_buffer(tape);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114 return NULL;
1115}
1116
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001117static int idetape_copy_stage_from_user(idetape_tape_t *tape,
Borislav Petkov8646c882008-04-27 15:38:26 +02001118 const char __user *buf, int n)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119{
1120 struct idetape_bh *bh = tape->bh;
1121 int count;
Daniel Walkerdcd96372006-06-25 05:47:37 -07001122 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123
1124 while (n) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125 if (bh == NULL) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001126 printk(KERN_ERR "ide-tape: bh == NULL in %s\n",
1127 __func__);
Daniel Walkerdcd96372006-06-25 05:47:37 -07001128 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129 }
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001130 count = min((unsigned int)
1131 (bh->b_size - atomic_read(&bh->b_count)),
1132 (unsigned int)n);
1133 if (copy_from_user(bh->b_data + atomic_read(&bh->b_count), buf,
1134 count))
Daniel Walkerdcd96372006-06-25 05:47:37 -07001135 ret = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136 n -= count;
1137 atomic_add(count, &bh->b_count);
1138 buf += count;
1139 if (atomic_read(&bh->b_count) == bh->b_size) {
1140 bh = bh->b_reqnext;
1141 if (bh)
1142 atomic_set(&bh->b_count, 0);
1143 }
1144 }
1145 tape->bh = bh;
Daniel Walkerdcd96372006-06-25 05:47:37 -07001146 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147}
1148
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001149static int idetape_copy_stage_to_user(idetape_tape_t *tape, char __user *buf,
Borislav Petkov99d74e62008-04-27 15:38:25 +02001150 int n)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151{
1152 struct idetape_bh *bh = tape->bh;
1153 int count;
Daniel Walkerdcd96372006-06-25 05:47:37 -07001154 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155
1156 while (n) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157 if (bh == NULL) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001158 printk(KERN_ERR "ide-tape: bh == NULL in %s\n",
1159 __func__);
Daniel Walkerdcd96372006-06-25 05:47:37 -07001160 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162 count = min(tape->b_count, n);
Daniel Walkerdcd96372006-06-25 05:47:37 -07001163 if (copy_to_user(buf, tape->b_data, count))
1164 ret = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165 n -= count;
1166 tape->b_data += count;
1167 tape->b_count -= count;
1168 buf += count;
1169 if (!tape->b_count) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001170 bh = bh->b_reqnext;
1171 tape->bh = bh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172 if (bh) {
1173 tape->b_data = bh->b_data;
1174 tape->b_count = atomic_read(&bh->b_count);
1175 }
1176 }
1177 }
Daniel Walkerdcd96372006-06-25 05:47:37 -07001178 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179}
1180
Borislav Petkov077e3bd2008-04-27 15:38:34 +02001181static void idetape_init_merge_buffer(idetape_tape_t *tape)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182{
Borislav Petkov077e3bd2008-04-27 15:38:34 +02001183 struct idetape_bh *bh = tape->merge_bh;
1184 tape->bh = tape->merge_bh;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001185
Borislav Petkov54abf372008-02-06 02:57:52 +01001186 if (tape->chrdev_dir == IDETAPE_DIR_WRITE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187 atomic_set(&bh->b_count, 0);
1188 else {
1189 tape->b_data = bh->b_data;
1190 tape->b_count = atomic_read(&bh->b_count);
1191 }
1192}
1193
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001195 * Write a filemark if write_filemark=1. Flush the device buffers without
1196 * writing a filemark otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197 */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001198static void idetape_create_write_filemark_cmd(ide_drive_t *drive,
Borislav Petkovd236d742008-04-18 00:46:27 +02001199 struct ide_atapi_pc *pc, int write_filemark)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200{
1201 idetape_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +01001202 pc->c[0] = WRITE_FILEMARKS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203 pc->c[4] = write_filemark;
Borislav Petkov346331f2008-04-18 00:46:26 +02001204 pc->flags |= PC_FLAG_WAIT_FOR_DSC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205}
1206
Borislav Petkovd236d742008-04-18 00:46:27 +02001207static void idetape_create_test_unit_ready_cmd(struct ide_atapi_pc *pc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208{
1209 idetape_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +01001210 pc->c[0] = TEST_UNIT_READY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211}
1212
1213/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001214 * We add a special packet command request to the tail of the request queue, and
Bartlomiej Zolnierkiewicz394a4c22008-10-10 22:39:35 +02001215 * wait for it to be serviced.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216 */
Borislav Petkovea1ab3d2008-04-27 15:38:27 +02001217static int idetape_queue_pc_tail(ide_drive_t *drive, struct ide_atapi_pc *pc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218{
1219 struct ide_tape_obj *tape = drive->driver_data;
FUJITA Tomonori64ea1b42008-07-15 21:21:43 +02001220 struct request *rq;
1221 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222
FUJITA Tomonori64ea1b42008-07-15 21:21:43 +02001223 rq = blk_get_request(drive->queue, READ, __GFP_WAIT);
1224 rq->cmd_type = REQ_TYPE_SPECIAL;
Borislav Petkov83dd5732008-07-23 19:56:00 +02001225 rq->cmd[13] = REQ_IDETAPE_PC1;
FUJITA Tomonori64ea1b42008-07-15 21:21:43 +02001226 rq->buffer = (char *)pc;
Borislav Petkov0014c752008-07-23 19:56:00 +02001227 memcpy(rq->cmd, pc->c, 12);
FUJITA Tomonori64ea1b42008-07-15 21:21:43 +02001228 error = blk_execute_rq(drive->queue, tape->disk, rq, 0);
1229 blk_put_request(rq);
1230 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231}
1232
Borislav Petkovd236d742008-04-18 00:46:27 +02001233static void idetape_create_load_unload_cmd(ide_drive_t *drive,
1234 struct ide_atapi_pc *pc, int cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235{
1236 idetape_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +01001237 pc->c[0] = START_STOP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238 pc->c[4] = cmd;
Borislav Petkov346331f2008-04-18 00:46:26 +02001239 pc->flags |= PC_FLAG_WAIT_FOR_DSC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240}
1241
1242static int idetape_wait_ready(ide_drive_t *drive, unsigned long timeout)
1243{
1244 idetape_tape_t *tape = drive->driver_data;
Borislav Petkovd236d742008-04-18 00:46:27 +02001245 struct ide_atapi_pc pc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246 int load_attempted = 0;
1247
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001248 /* Wait for the tape to become ready */
Borislav Petkovf2e3ab52008-07-23 19:56:01 +02001249 set_bit(IDE_AFLAG_MEDIUM_PRESENT, &drive->atapi_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250 timeout += jiffies;
1251 while (time_before(jiffies, timeout)) {
1252 idetape_create_test_unit_ready_cmd(&pc);
Borislav Petkovea1ab3d2008-04-27 15:38:27 +02001253 if (!idetape_queue_pc_tail(drive, &pc))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254 return 0;
1255 if ((tape->sense_key == 2 && tape->asc == 4 && tape->ascq == 2)
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001256 || (tape->asc == 0x3A)) {
1257 /* no media */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258 if (load_attempted)
1259 return -ENOMEDIUM;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001260 idetape_create_load_unload_cmd(drive, &pc,
1261 IDETAPE_LU_LOAD_MASK);
Borislav Petkovea1ab3d2008-04-27 15:38:27 +02001262 idetape_queue_pc_tail(drive, &pc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263 load_attempted = 1;
1264 /* not about to be ready */
1265 } else if (!(tape->sense_key == 2 && tape->asc == 4 &&
1266 (tape->ascq == 1 || tape->ascq == 8)))
1267 return -EIO;
Nishanth Aravamudan80ce45f2005-09-10 00:27:08 -07001268 msleep(100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269 }
1270 return -EIO;
1271}
1272
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001273static int idetape_flush_tape_buffers(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274{
Borislav Petkovd236d742008-04-18 00:46:27 +02001275 struct ide_atapi_pc pc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276 int rc;
1277
1278 idetape_create_write_filemark_cmd(drive, &pc, 0);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001279 rc = idetape_queue_pc_tail(drive, &pc);
1280 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281 return rc;
1282 idetape_wait_ready(drive, 60 * 5 * HZ);
1283 return 0;
1284}
1285
Borislav Petkovd236d742008-04-18 00:46:27 +02001286static void idetape_create_read_position_cmd(struct ide_atapi_pc *pc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287{
1288 idetape_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +01001289 pc->c[0] = READ_POSITION;
Borislav Petkovd236d742008-04-18 00:46:27 +02001290 pc->req_xfer = 20;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291}
1292
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001293static int idetape_read_position(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294{
1295 idetape_tape_t *tape = drive->driver_data;
Borislav Petkovd236d742008-04-18 00:46:27 +02001296 struct ide_atapi_pc pc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297 int position;
1298
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001299 debug_log(DBG_PROCS, "Enter %s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300
1301 idetape_create_read_position_cmd(&pc);
1302 if (idetape_queue_pc_tail(drive, &pc))
1303 return -1;
Borislav Petkov54bb2072008-02-06 02:57:52 +01001304 position = tape->first_frame;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305 return position;
1306}
1307
Borislav Petkovd236d742008-04-18 00:46:27 +02001308static void idetape_create_locate_cmd(ide_drive_t *drive,
1309 struct ide_atapi_pc *pc,
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001310 unsigned int block, u8 partition, int skip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311{
1312 idetape_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +01001313 pc->c[0] = POSITION_TO_ELEMENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314 pc->c[1] = 2;
Borislav Petkov860ff5e2008-02-02 19:56:50 +01001315 put_unaligned(cpu_to_be32(block), (unsigned int *) &pc->c[3]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316 pc->c[8] = partition;
Borislav Petkov346331f2008-04-18 00:46:26 +02001317 pc->flags |= PC_FLAG_WAIT_FOR_DSC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318}
1319
Borislav Petkovd236d742008-04-18 00:46:27 +02001320static int idetape_create_prevent_cmd(ide_drive_t *drive,
1321 struct ide_atapi_pc *pc, int prevent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322{
1323 idetape_tape_t *tape = drive->driver_data;
1324
Borislav Petkovb6422012008-02-02 19:56:49 +01001325 /* device supports locking according to capabilities page */
1326 if (!(tape->caps[6] & 0x01))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327 return 0;
1328
1329 idetape_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +01001330 pc->c[0] = ALLOW_MEDIUM_REMOVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331 pc->c[4] = prevent;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332 return 1;
1333}
1334
Borislav Petkovec0fdb02008-04-27 15:38:34 +02001335static void __ide_tape_discard_merge_buffer(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336{
1337 idetape_tape_t *tape = drive->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338
Borislav Petkov54abf372008-02-06 02:57:52 +01001339 if (tape->chrdev_dir != IDETAPE_DIR_READ)
Borislav Petkov97986302008-04-27 15:38:34 +02001340 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341
Borislav Petkovf2e3ab52008-07-23 19:56:01 +02001342 clear_bit(IDE_AFLAG_FILEMARK, &drive->atapi_flags);
Borislav Petkov01a63aeb2008-04-27 15:38:34 +02001343 tape->merge_bh_size = 0;
Borislav Petkov077e3bd2008-04-27 15:38:34 +02001344 if (tape->merge_bh != NULL) {
1345 ide_tape_kfree_buffer(tape);
1346 tape->merge_bh = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347 }
1348
Borislav Petkov54abf372008-02-06 02:57:52 +01001349 tape->chrdev_dir = IDETAPE_DIR_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350}
1351
1352/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001353 * Position the tape to the requested block using the LOCATE packet command.
1354 * A READ POSITION command is then issued to check where we are positioned. Like
1355 * all higher level operations, we queue the commands at the tail of the request
1356 * queue and wait for their completion.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357 */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001358static int idetape_position_tape(ide_drive_t *drive, unsigned int block,
1359 u8 partition, int skip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360{
1361 idetape_tape_t *tape = drive->driver_data;
1362 int retval;
Borislav Petkovd236d742008-04-18 00:46:27 +02001363 struct ide_atapi_pc pc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364
Borislav Petkov54abf372008-02-06 02:57:52 +01001365 if (tape->chrdev_dir == IDETAPE_DIR_READ)
Borislav Petkovec0fdb02008-04-27 15:38:34 +02001366 __ide_tape_discard_merge_buffer(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367 idetape_wait_ready(drive, 60 * 5 * HZ);
1368 idetape_create_locate_cmd(drive, &pc, block, partition, skip);
1369 retval = idetape_queue_pc_tail(drive, &pc);
1370 if (retval)
1371 return (retval);
1372
1373 idetape_create_read_position_cmd(&pc);
1374 return (idetape_queue_pc_tail(drive, &pc));
1375}
1376
Borislav Petkovec0fdb02008-04-27 15:38:34 +02001377static void ide_tape_discard_merge_buffer(ide_drive_t *drive,
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001378 int restore_position)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379{
1380 idetape_tape_t *tape = drive->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381 int seek, position;
1382
Borislav Petkovec0fdb02008-04-27 15:38:34 +02001383 __ide_tape_discard_merge_buffer(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384 if (restore_position) {
1385 position = idetape_read_position(drive);
Borislav Petkov97986302008-04-27 15:38:34 +02001386 seek = position > 0 ? position : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387 if (idetape_position_tape(drive, seek, 0, 0)) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001388 printk(KERN_INFO "ide-tape: %s: position_tape failed in"
Borislav Petkovec0fdb02008-04-27 15:38:34 +02001389 " %s\n", tape->name, __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390 return;
1391 }
1392 }
1393}
1394
1395/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001396 * Generate a read/write request for the block device interface and wait for it
1397 * to be serviced.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398 */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001399static int idetape_queue_rw_tail(ide_drive_t *drive, int cmd, int blocks,
1400 struct idetape_bh *bh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401{
1402 idetape_tape_t *tape = drive->driver_data;
FUJITA Tomonori64ea1b42008-07-15 21:21:43 +02001403 struct request *rq;
1404 int ret, errors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001406 debug_log(DBG_SENSE, "%s: cmd=%d\n", __func__, cmd);
1407
FUJITA Tomonori64ea1b42008-07-15 21:21:43 +02001408 rq = blk_get_request(drive->queue, READ, __GFP_WAIT);
1409 rq->cmd_type = REQ_TYPE_SPECIAL;
Borislav Petkov83dd5732008-07-23 19:56:00 +02001410 rq->cmd[13] = cmd;
FUJITA Tomonori64ea1b42008-07-15 21:21:43 +02001411 rq->rq_disk = tape->disk;
1412 rq->special = (void *)bh;
1413 rq->sector = tape->first_frame;
1414 rq->nr_sectors = blocks;
1415 rq->current_nr_sectors = blocks;
1416 blk_execute_rq(drive->queue, tape->disk, rq, 0);
1417
1418 errors = rq->errors;
1419 ret = tape->blk_size * (blocks - rq->current_nr_sectors);
1420 blk_put_request(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421
1422 if ((cmd & (REQ_IDETAPE_READ | REQ_IDETAPE_WRITE)) == 0)
1423 return 0;
1424
Borislav Petkov077e3bd2008-04-27 15:38:34 +02001425 if (tape->merge_bh)
1426 idetape_init_merge_buffer(tape);
FUJITA Tomonori64ea1b42008-07-15 21:21:43 +02001427 if (errors == IDETAPE_ERROR_GENERAL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428 return -EIO;
FUJITA Tomonori64ea1b42008-07-15 21:21:43 +02001429 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430}
1431
Borislav Petkovd236d742008-04-18 00:46:27 +02001432static void idetape_create_inquiry_cmd(struct ide_atapi_pc *pc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433{
1434 idetape_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +01001435 pc->c[0] = INQUIRY;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001436 pc->c[4] = 254;
Borislav Petkovd236d742008-04-18 00:46:27 +02001437 pc->req_xfer = 254;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438}
1439
Borislav Petkovd236d742008-04-18 00:46:27 +02001440static void idetape_create_rewind_cmd(ide_drive_t *drive,
1441 struct ide_atapi_pc *pc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442{
1443 idetape_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +01001444 pc->c[0] = REZERO_UNIT;
Borislav Petkov346331f2008-04-18 00:46:26 +02001445 pc->flags |= PC_FLAG_WAIT_FOR_DSC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446}
1447
Borislav Petkovd236d742008-04-18 00:46:27 +02001448static void idetape_create_erase_cmd(struct ide_atapi_pc *pc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449{
1450 idetape_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +01001451 pc->c[0] = ERASE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452 pc->c[1] = 1;
Borislav Petkov346331f2008-04-18 00:46:26 +02001453 pc->flags |= PC_FLAG_WAIT_FOR_DSC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454}
1455
Borislav Petkovd236d742008-04-18 00:46:27 +02001456static void idetape_create_space_cmd(struct ide_atapi_pc *pc, int count, u8 cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457{
1458 idetape_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +01001459 pc->c[0] = SPACE;
Borislav Petkov860ff5e2008-02-02 19:56:50 +01001460 put_unaligned(cpu_to_be32(count), (unsigned int *) &pc->c[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461 pc->c[1] = cmd;
Borislav Petkov346331f2008-04-18 00:46:26 +02001462 pc->flags |= PC_FLAG_WAIT_FOR_DSC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463}
1464
Borislav Petkov97c566c2008-04-27 15:38:25 +02001465/* Queue up a character device originated write request. */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001466static int idetape_add_chrdev_write_request(ide_drive_t *drive, int blocks)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467{
1468 idetape_tape_t *tape = drive->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001470 debug_log(DBG_CHRDEV, "Enter %s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001471
Borislav Petkov0aa4b012008-04-27 15:38:27 +02001472 return idetape_queue_rw_tail(drive, REQ_IDETAPE_WRITE,
Borislav Petkov077e3bd2008-04-27 15:38:34 +02001473 blocks, tape->merge_bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474}
1475
Borislav Petkovd9df9372008-04-27 15:38:34 +02001476static void ide_tape_flush_merge_buffer(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477{
1478 idetape_tape_t *tape = drive->driver_data;
1479 int blocks, min;
1480 struct idetape_bh *bh;
Borislav Petkov55a5d292008-02-02 19:56:49 +01001481
Borislav Petkov54abf372008-02-06 02:57:52 +01001482 if (tape->chrdev_dir != IDETAPE_DIR_WRITE) {
Borislav Petkov077e3bd2008-04-27 15:38:34 +02001483 printk(KERN_ERR "ide-tape: bug: Trying to empty merge buffer"
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001484 " but we are not writing.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485 return;
1486 }
Borislav Petkov01a63aeb2008-04-27 15:38:34 +02001487 if (tape->merge_bh_size > tape->buffer_size) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488 printk(KERN_ERR "ide-tape: bug: merge_buffer too big\n");
Borislav Petkov01a63aeb2008-04-27 15:38:34 +02001489 tape->merge_bh_size = tape->buffer_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490 }
Borislav Petkov01a63aeb2008-04-27 15:38:34 +02001491 if (tape->merge_bh_size) {
1492 blocks = tape->merge_bh_size / tape->blk_size;
1493 if (tape->merge_bh_size % tape->blk_size) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494 unsigned int i;
1495
1496 blocks++;
Borislav Petkov01a63aeb2008-04-27 15:38:34 +02001497 i = tape->blk_size - tape->merge_bh_size %
Borislav Petkov54bb2072008-02-06 02:57:52 +01001498 tape->blk_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499 bh = tape->bh->b_reqnext;
1500 while (bh) {
1501 atomic_set(&bh->b_count, 0);
1502 bh = bh->b_reqnext;
1503 }
1504 bh = tape->bh;
1505 while (i) {
1506 if (bh == NULL) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001507 printk(KERN_INFO "ide-tape: bug,"
1508 " bh NULL\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509 break;
1510 }
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001511 min = min(i, (unsigned int)(bh->b_size -
1512 atomic_read(&bh->b_count)));
1513 memset(bh->b_data + atomic_read(&bh->b_count),
1514 0, min);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515 atomic_add(min, &bh->b_count);
1516 i -= min;
1517 bh = bh->b_reqnext;
1518 }
1519 }
1520 (void) idetape_add_chrdev_write_request(drive, blocks);
Borislav Petkov01a63aeb2008-04-27 15:38:34 +02001521 tape->merge_bh_size = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522 }
Borislav Petkov077e3bd2008-04-27 15:38:34 +02001523 if (tape->merge_bh != NULL) {
1524 ide_tape_kfree_buffer(tape);
1525 tape->merge_bh = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526 }
Borislav Petkov54abf372008-02-06 02:57:52 +01001527 tape->chrdev_dir = IDETAPE_DIR_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001528}
1529
Borislav Petkov83042b22008-04-27 15:38:27 +02001530static int idetape_init_read(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531{
1532 idetape_tape_t *tape = drive->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533 int bytes_read;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534
1535 /* Initialize read operation */
Borislav Petkov54abf372008-02-06 02:57:52 +01001536 if (tape->chrdev_dir != IDETAPE_DIR_READ) {
1537 if (tape->chrdev_dir == IDETAPE_DIR_WRITE) {
Borislav Petkovd9df9372008-04-27 15:38:34 +02001538 ide_tape_flush_merge_buffer(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539 idetape_flush_tape_buffers(drive);
1540 }
Borislav Petkov077e3bd2008-04-27 15:38:34 +02001541 if (tape->merge_bh || tape->merge_bh_size) {
Borislav Petkov01a63aeb2008-04-27 15:38:34 +02001542 printk(KERN_ERR "ide-tape: merge_bh_size should be"
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001543 " 0 now\n");
Borislav Petkov01a63aeb2008-04-27 15:38:34 +02001544 tape->merge_bh_size = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545 }
Borislav Petkov077e3bd2008-04-27 15:38:34 +02001546 tape->merge_bh = ide_tape_kmalloc_buffer(tape, 0, 0);
1547 if (!tape->merge_bh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548 return -ENOMEM;
Borislav Petkov54abf372008-02-06 02:57:52 +01001549 tape->chrdev_dir = IDETAPE_DIR_READ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550
1551 /*
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001552 * Issue a read 0 command to ensure that DSC handshake is
1553 * switched from completion mode to buffer available mode.
1554 * No point in issuing this if DSC overlap isn't supported, some
1555 * drives (Seagate STT3401A) will return an error.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556 */
1557 if (drive->dsc_overlap) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001558 bytes_read = idetape_queue_rw_tail(drive,
1559 REQ_IDETAPE_READ, 0,
Borislav Petkov077e3bd2008-04-27 15:38:34 +02001560 tape->merge_bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001561 if (bytes_read < 0) {
Borislav Petkov077e3bd2008-04-27 15:38:34 +02001562 ide_tape_kfree_buffer(tape);
1563 tape->merge_bh = NULL;
Borislav Petkov54abf372008-02-06 02:57:52 +01001564 tape->chrdev_dir = IDETAPE_DIR_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565 return bytes_read;
1566 }
1567 }
1568 }
Borislav Petkov5e69bd92008-04-27 15:38:25 +02001569
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570 return 0;
1571}
1572
Borislav Petkov5bd50dc2008-04-27 15:38:28 +02001573/* called from idetape_chrdev_read() to service a chrdev read request. */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001574static int idetape_add_chrdev_read_request(ide_drive_t *drive, int blocks)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001575{
1576 idetape_tape_t *tape = drive->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001577
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001578 debug_log(DBG_PROCS, "Enter %s, %d blocks\n", __func__, blocks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001580 /* If we are at a filemark, return a read length of 0 */
Borislav Petkovf2e3ab52008-07-23 19:56:01 +02001581 if (test_bit(IDE_AFLAG_FILEMARK, &drive->atapi_flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582 return 0;
1583
Borislav Petkov83042b22008-04-27 15:38:27 +02001584 idetape_init_read(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585
Borislav Petkov5e69bd92008-04-27 15:38:25 +02001586 return idetape_queue_rw_tail(drive, REQ_IDETAPE_READ, blocks,
Borislav Petkov077e3bd2008-04-27 15:38:34 +02001587 tape->merge_bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001588}
1589
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001590static void idetape_pad_zeros(ide_drive_t *drive, int bcount)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591{
1592 idetape_tape_t *tape = drive->driver_data;
1593 struct idetape_bh *bh;
1594 int blocks;
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001595
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596 while (bcount) {
1597 unsigned int count;
1598
Borislav Petkov077e3bd2008-04-27 15:38:34 +02001599 bh = tape->merge_bh;
Borislav Petkovf73850a2008-04-27 15:38:33 +02001600 count = min(tape->buffer_size, bcount);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001601 bcount -= count;
Borislav Petkov54bb2072008-02-06 02:57:52 +01001602 blocks = count / tape->blk_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001603 while (count) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001604 atomic_set(&bh->b_count,
1605 min(count, (unsigned int)bh->b_size));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606 memset(bh->b_data, 0, atomic_read(&bh->b_count));
1607 count -= atomic_read(&bh->b_count);
1608 bh = bh->b_reqnext;
1609 }
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001610 idetape_queue_rw_tail(drive, REQ_IDETAPE_WRITE, blocks,
Borislav Petkov077e3bd2008-04-27 15:38:34 +02001611 tape->merge_bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001612 }
1613}
1614
Linus Torvalds1da177e2005-04-16 15:20:36 -07001615/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001616 * Rewinds the tape to the Beginning Of the current Partition (BOP). We
1617 * currently support only one partition.
1618 */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001619static int idetape_rewind_tape(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001620{
1621 int retval;
Borislav Petkovd236d742008-04-18 00:46:27 +02001622 struct ide_atapi_pc pc;
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001623 idetape_tape_t *tape;
1624 tape = drive->driver_data;
1625
1626 debug_log(DBG_SENSE, "Enter %s\n", __func__);
1627
Linus Torvalds1da177e2005-04-16 15:20:36 -07001628 idetape_create_rewind_cmd(drive, &pc);
1629 retval = idetape_queue_pc_tail(drive, &pc);
1630 if (retval)
1631 return retval;
1632
1633 idetape_create_read_position_cmd(&pc);
1634 retval = idetape_queue_pc_tail(drive, &pc);
1635 if (retval)
1636 return retval;
1637 return 0;
1638}
1639
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001640/* mtio.h compatible commands should be issued to the chrdev interface. */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001641static int idetape_blkdev_ioctl(ide_drive_t *drive, unsigned int cmd,
1642 unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643{
1644 idetape_tape_t *tape = drive->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001645 void __user *argp = (void __user *)arg;
1646
Borislav Petkovd59823f2008-02-02 19:56:51 +01001647 struct idetape_config {
1648 int dsc_rw_frequency;
1649 int dsc_media_access_frequency;
1650 int nr_stages;
1651 } config;
1652
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001653 debug_log(DBG_PROCS, "Enter %s\n", __func__);
1654
Linus Torvalds1da177e2005-04-16 15:20:36 -07001655 switch (cmd) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001656 case 0x0340:
1657 if (copy_from_user(&config, argp, sizeof(config)))
1658 return -EFAULT;
1659 tape->best_dsc_rw_freq = config.dsc_rw_frequency;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001660 break;
1661 case 0x0350:
1662 config.dsc_rw_frequency = (int) tape->best_dsc_rw_freq;
Borislav Petkov83042b22008-04-27 15:38:27 +02001663 config.nr_stages = 1;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001664 if (copy_to_user(argp, &config, sizeof(config)))
1665 return -EFAULT;
1666 break;
1667 default:
1668 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001669 }
1670 return 0;
1671}
1672
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001673static int idetape_space_over_filemarks(ide_drive_t *drive, short mt_op,
1674 int mt_count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675{
1676 idetape_tape_t *tape = drive->driver_data;
Borislav Petkovd236d742008-04-18 00:46:27 +02001677 struct ide_atapi_pc pc;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001678 int retval, count = 0;
Borislav Petkovb6422012008-02-02 19:56:49 +01001679 int sprev = !!(tape->caps[4] & 0x20);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001680
1681 if (mt_count == 0)
1682 return 0;
1683 if (MTBSF == mt_op || MTBSFM == mt_op) {
Borislav Petkovb6422012008-02-02 19:56:49 +01001684 if (!sprev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001685 return -EIO;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001686 mt_count = -mt_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001687 }
1688
Borislav Petkov54abf372008-02-06 02:57:52 +01001689 if (tape->chrdev_dir == IDETAPE_DIR_READ) {
Borislav Petkov01a63aeb2008-04-27 15:38:34 +02001690 tape->merge_bh_size = 0;
Borislav Petkovf2e3ab52008-07-23 19:56:01 +02001691 if (test_and_clear_bit(IDE_AFLAG_FILEMARK, &drive->atapi_flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001692 ++count;
Borislav Petkovec0fdb02008-04-27 15:38:34 +02001693 ide_tape_discard_merge_buffer(drive, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694 }
1695
Linus Torvalds1da177e2005-04-16 15:20:36 -07001696 switch (mt_op) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001697 case MTFSF:
1698 case MTBSF:
1699 idetape_create_space_cmd(&pc, mt_count - count,
1700 IDETAPE_SPACE_OVER_FILEMARK);
1701 return idetape_queue_pc_tail(drive, &pc);
1702 case MTFSFM:
1703 case MTBSFM:
1704 if (!sprev)
1705 return -EIO;
1706 retval = idetape_space_over_filemarks(drive, MTFSF,
1707 mt_count - count);
1708 if (retval)
1709 return retval;
1710 count = (MTBSFM == mt_op ? 1 : -1);
1711 return idetape_space_over_filemarks(drive, MTFSF, count);
1712 default:
1713 printk(KERN_ERR "ide-tape: MTIO operation %d not supported\n",
1714 mt_op);
1715 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001716 }
1717}
1718
Linus Torvalds1da177e2005-04-16 15:20:36 -07001719/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001720 * Our character device read / write functions.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001721 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001722 * The tape is optimized to maximize throughput when it is transferring an
1723 * integral number of the "continuous transfer limit", which is a parameter of
1724 * the specific tape (26kB on my particular tape, 32kB for Onstream).
Linus Torvalds1da177e2005-04-16 15:20:36 -07001725 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001726 * As of version 1.3 of the driver, the character device provides an abstract
1727 * continuous view of the media - any mix of block sizes (even 1 byte) on the
1728 * same backup/restore procedure is supported. The driver will internally
1729 * convert the requests to the recommended transfer unit, so that an unmatch
1730 * between the user's block size to the recommended size will only result in a
1731 * (slightly) increased driver overhead, but will no longer hit performance.
1732 * This is not applicable to Onstream.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001733 */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001734static ssize_t idetape_chrdev_read(struct file *file, char __user *buf,
1735 size_t count, loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001736{
1737 struct ide_tape_obj *tape = ide_tape_f(file);
1738 ide_drive_t *drive = tape->drive;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001739 ssize_t bytes_read, temp, actually_read = 0, rc;
Daniel Walkerdcd96372006-06-25 05:47:37 -07001740 ssize_t ret = 0;
Borislav Petkovb6422012008-02-02 19:56:49 +01001741 u16 ctl = *(u16 *)&tape->caps[12];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001742
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001743 debug_log(DBG_CHRDEV, "Enter %s, count %Zd\n", __func__, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001744
Borislav Petkov54abf372008-02-06 02:57:52 +01001745 if (tape->chrdev_dir != IDETAPE_DIR_READ) {
Borislav Petkovf2e3ab52008-07-23 19:56:01 +02001746 if (test_bit(IDE_AFLAG_DETECT_BS, &drive->atapi_flags))
Borislav Petkov54bb2072008-02-06 02:57:52 +01001747 if (count > tape->blk_size &&
1748 (count % tape->blk_size) == 0)
1749 tape->user_bs_factor = count / tape->blk_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001750 }
Borislav Petkov83042b22008-04-27 15:38:27 +02001751 rc = idetape_init_read(drive);
Borislav Petkov8d06bfa2008-02-06 02:57:53 +01001752 if (rc < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001753 return rc;
1754 if (count == 0)
1755 return (0);
Borislav Petkov01a63aeb2008-04-27 15:38:34 +02001756 if (tape->merge_bh_size) {
1757 actually_read = min((unsigned int)(tape->merge_bh_size),
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001758 (unsigned int)count);
Borislav Petkov99d74e62008-04-27 15:38:25 +02001759 if (idetape_copy_stage_to_user(tape, buf, actually_read))
Daniel Walkerdcd96372006-06-25 05:47:37 -07001760 ret = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001761 buf += actually_read;
Borislav Petkov01a63aeb2008-04-27 15:38:34 +02001762 tape->merge_bh_size -= actually_read;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001763 count -= actually_read;
1764 }
Borislav Petkovf73850a2008-04-27 15:38:33 +02001765 while (count >= tape->buffer_size) {
Borislav Petkovb6422012008-02-02 19:56:49 +01001766 bytes_read = idetape_add_chrdev_read_request(drive, ctl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767 if (bytes_read <= 0)
1768 goto finish;
Borislav Petkov99d74e62008-04-27 15:38:25 +02001769 if (idetape_copy_stage_to_user(tape, buf, bytes_read))
Daniel Walkerdcd96372006-06-25 05:47:37 -07001770 ret = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001771 buf += bytes_read;
1772 count -= bytes_read;
1773 actually_read += bytes_read;
1774 }
1775 if (count) {
Borislav Petkovb6422012008-02-02 19:56:49 +01001776 bytes_read = idetape_add_chrdev_read_request(drive, ctl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001777 if (bytes_read <= 0)
1778 goto finish;
1779 temp = min((unsigned long)count, (unsigned long)bytes_read);
Borislav Petkov99d74e62008-04-27 15:38:25 +02001780 if (idetape_copy_stage_to_user(tape, buf, temp))
Daniel Walkerdcd96372006-06-25 05:47:37 -07001781 ret = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001782 actually_read += temp;
Borislav Petkov01a63aeb2008-04-27 15:38:34 +02001783 tape->merge_bh_size = bytes_read-temp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001784 }
1785finish:
Borislav Petkovf2e3ab52008-07-23 19:56:01 +02001786 if (!actually_read && test_bit(IDE_AFLAG_FILEMARK, &drive->atapi_flags)) {
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001787 debug_log(DBG_SENSE, "%s: spacing over filemark\n", tape->name);
1788
Linus Torvalds1da177e2005-04-16 15:20:36 -07001789 idetape_space_over_filemarks(drive, MTFSF, 1);
1790 return 0;
1791 }
Daniel Walkerdcd96372006-06-25 05:47:37 -07001792
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001793 return ret ? ret : actually_read;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001794}
1795
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001796static ssize_t idetape_chrdev_write(struct file *file, const char __user *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001797 size_t count, loff_t *ppos)
1798{
1799 struct ide_tape_obj *tape = ide_tape_f(file);
1800 ide_drive_t *drive = tape->drive;
Daniel Walkerdcd96372006-06-25 05:47:37 -07001801 ssize_t actually_written = 0;
1802 ssize_t ret = 0;
Borislav Petkovb6422012008-02-02 19:56:49 +01001803 u16 ctl = *(u16 *)&tape->caps[12];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001804
1805 /* The drive is write protected. */
1806 if (tape->write_prot)
1807 return -EACCES;
1808
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001809 debug_log(DBG_CHRDEV, "Enter %s, count %Zd\n", __func__, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001810
1811 /* Initialize write operation */
Borislav Petkov54abf372008-02-06 02:57:52 +01001812 if (tape->chrdev_dir != IDETAPE_DIR_WRITE) {
1813 if (tape->chrdev_dir == IDETAPE_DIR_READ)
Borislav Petkovec0fdb02008-04-27 15:38:34 +02001814 ide_tape_discard_merge_buffer(drive, 1);
Borislav Petkov077e3bd2008-04-27 15:38:34 +02001815 if (tape->merge_bh || tape->merge_bh_size) {
Borislav Petkov01a63aeb2008-04-27 15:38:34 +02001816 printk(KERN_ERR "ide-tape: merge_bh_size "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001817 "should be 0 now\n");
Borislav Petkov01a63aeb2008-04-27 15:38:34 +02001818 tape->merge_bh_size = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001819 }
Borislav Petkov077e3bd2008-04-27 15:38:34 +02001820 tape->merge_bh = ide_tape_kmalloc_buffer(tape, 0, 0);
1821 if (!tape->merge_bh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001822 return -ENOMEM;
Borislav Petkov54abf372008-02-06 02:57:52 +01001823 tape->chrdev_dir = IDETAPE_DIR_WRITE;
Borislav Petkov077e3bd2008-04-27 15:38:34 +02001824 idetape_init_merge_buffer(tape);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825
1826 /*
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001827 * Issue a write 0 command to ensure that DSC handshake is
1828 * switched from completion mode to buffer available mode. No
1829 * point in issuing this if DSC overlap isn't supported, some
1830 * drives (Seagate STT3401A) will return an error.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001831 */
1832 if (drive->dsc_overlap) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001833 ssize_t retval = idetape_queue_rw_tail(drive,
1834 REQ_IDETAPE_WRITE, 0,
Borislav Petkov077e3bd2008-04-27 15:38:34 +02001835 tape->merge_bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001836 if (retval < 0) {
Borislav Petkov077e3bd2008-04-27 15:38:34 +02001837 ide_tape_kfree_buffer(tape);
1838 tape->merge_bh = NULL;
Borislav Petkov54abf372008-02-06 02:57:52 +01001839 tape->chrdev_dir = IDETAPE_DIR_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001840 return retval;
1841 }
1842 }
1843 }
1844 if (count == 0)
1845 return (0);
Borislav Petkov01a63aeb2008-04-27 15:38:34 +02001846 if (tape->merge_bh_size) {
1847 if (tape->merge_bh_size >= tape->buffer_size) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001848 printk(KERN_ERR "ide-tape: bug: merge buf too big\n");
Borislav Petkov01a63aeb2008-04-27 15:38:34 +02001849 tape->merge_bh_size = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001850 }
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001851 actually_written = min((unsigned int)
Borislav Petkov01a63aeb2008-04-27 15:38:34 +02001852 (tape->buffer_size - tape->merge_bh_size),
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001853 (unsigned int)count);
Borislav Petkov8646c882008-04-27 15:38:26 +02001854 if (idetape_copy_stage_from_user(tape, buf, actually_written))
Daniel Walkerdcd96372006-06-25 05:47:37 -07001855 ret = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001856 buf += actually_written;
Borislav Petkov01a63aeb2008-04-27 15:38:34 +02001857 tape->merge_bh_size += actually_written;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001858 count -= actually_written;
1859
Borislav Petkov01a63aeb2008-04-27 15:38:34 +02001860 if (tape->merge_bh_size == tape->buffer_size) {
Daniel Walkerdcd96372006-06-25 05:47:37 -07001861 ssize_t retval;
Borislav Petkov01a63aeb2008-04-27 15:38:34 +02001862 tape->merge_bh_size = 0;
Borislav Petkovb6422012008-02-02 19:56:49 +01001863 retval = idetape_add_chrdev_write_request(drive, ctl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001864 if (retval <= 0)
1865 return (retval);
1866 }
1867 }
Borislav Petkovf73850a2008-04-27 15:38:33 +02001868 while (count >= tape->buffer_size) {
Daniel Walkerdcd96372006-06-25 05:47:37 -07001869 ssize_t retval;
Borislav Petkovf73850a2008-04-27 15:38:33 +02001870 if (idetape_copy_stage_from_user(tape, buf, tape->buffer_size))
Daniel Walkerdcd96372006-06-25 05:47:37 -07001871 ret = -EFAULT;
Borislav Petkovf73850a2008-04-27 15:38:33 +02001872 buf += tape->buffer_size;
1873 count -= tape->buffer_size;
Borislav Petkovb6422012008-02-02 19:56:49 +01001874 retval = idetape_add_chrdev_write_request(drive, ctl);
Borislav Petkovf73850a2008-04-27 15:38:33 +02001875 actually_written += tape->buffer_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001876 if (retval <= 0)
1877 return (retval);
1878 }
1879 if (count) {
1880 actually_written += count;
Borislav Petkov8646c882008-04-27 15:38:26 +02001881 if (idetape_copy_stage_from_user(tape, buf, count))
Daniel Walkerdcd96372006-06-25 05:47:37 -07001882 ret = -EFAULT;
Borislav Petkov01a63aeb2008-04-27 15:38:34 +02001883 tape->merge_bh_size += count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001884 }
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001885 return ret ? ret : actually_written;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001886}
1887
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001888static int idetape_write_filemark(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001889{
Borislav Petkovd236d742008-04-18 00:46:27 +02001890 struct ide_atapi_pc pc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001891
1892 /* Write a filemark */
1893 idetape_create_write_filemark_cmd(drive, &pc, 1);
1894 if (idetape_queue_pc_tail(drive, &pc)) {
1895 printk(KERN_ERR "ide-tape: Couldn't write a filemark\n");
1896 return -EIO;
1897 }
1898 return 0;
1899}
1900
1901/*
Borislav Petkovd99c9da2008-02-02 19:56:51 +01001902 * Called from idetape_chrdev_ioctl when the general mtio MTIOCTOP ioctl is
1903 * requested.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001904 *
Borislav Petkovd99c9da2008-02-02 19:56:51 +01001905 * Note: MTBSF and MTBSFM are not supported when the tape doesn't support
1906 * spacing over filemarks in the reverse direction. In this case, MTFSFM is also
Borislav Petkov5bd50dc2008-04-27 15:38:28 +02001907 * usually not supported.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001908 *
Borislav Petkovd99c9da2008-02-02 19:56:51 +01001909 * The following commands are currently not supported:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001910 *
Borislav Petkovd99c9da2008-02-02 19:56:51 +01001911 * MTFSS, MTBSS, MTWSM, MTSETDENSITY, MTSETDRVBUFFER, MT_ST_BOOLEANS,
1912 * MT_ST_WRITE_THRESHOLD.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001913 */
Borislav Petkovd99c9da2008-02-02 19:56:51 +01001914static int idetape_mtioctop(ide_drive_t *drive, short mt_op, int mt_count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001915{
1916 idetape_tape_t *tape = drive->driver_data;
Borislav Petkovd236d742008-04-18 00:46:27 +02001917 struct ide_atapi_pc pc;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001918 int i, retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001919
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001920 debug_log(DBG_ERR, "Handling MTIOCTOP ioctl: mt_op=%d, mt_count=%d\n",
1921 mt_op, mt_count);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001922
Linus Torvalds1da177e2005-04-16 15:20:36 -07001923 switch (mt_op) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001924 case MTFSF:
1925 case MTFSFM:
1926 case MTBSF:
1927 case MTBSFM:
1928 if (!mt_count)
1929 return 0;
1930 return idetape_space_over_filemarks(drive, mt_op, mt_count);
1931 default:
1932 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001933 }
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001934
Linus Torvalds1da177e2005-04-16 15:20:36 -07001935 switch (mt_op) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001936 case MTWEOF:
1937 if (tape->write_prot)
1938 return -EACCES;
Borislav Petkovec0fdb02008-04-27 15:38:34 +02001939 ide_tape_discard_merge_buffer(drive, 1);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001940 for (i = 0; i < mt_count; i++) {
1941 retval = idetape_write_filemark(drive);
1942 if (retval)
1943 return retval;
1944 }
1945 return 0;
1946 case MTREW:
Borislav Petkovec0fdb02008-04-27 15:38:34 +02001947 ide_tape_discard_merge_buffer(drive, 0);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001948 if (idetape_rewind_tape(drive))
1949 return -EIO;
1950 return 0;
1951 case MTLOAD:
Borislav Petkovec0fdb02008-04-27 15:38:34 +02001952 ide_tape_discard_merge_buffer(drive, 0);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001953 idetape_create_load_unload_cmd(drive, &pc,
1954 IDETAPE_LU_LOAD_MASK);
1955 return idetape_queue_pc_tail(drive, &pc);
1956 case MTUNLOAD:
1957 case MTOFFL:
1958 /*
1959 * If door is locked, attempt to unlock before
1960 * attempting to eject.
1961 */
1962 if (tape->door_locked) {
1963 if (idetape_create_prevent_cmd(drive, &pc, 0))
1964 if (!idetape_queue_pc_tail(drive, &pc))
1965 tape->door_locked = DOOR_UNLOCKED;
1966 }
Borislav Petkovec0fdb02008-04-27 15:38:34 +02001967 ide_tape_discard_merge_buffer(drive, 0);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001968 idetape_create_load_unload_cmd(drive, &pc,
1969 !IDETAPE_LU_LOAD_MASK);
1970 retval = idetape_queue_pc_tail(drive, &pc);
1971 if (!retval)
Borislav Petkovf2e3ab52008-07-23 19:56:01 +02001972 clear_bit(IDE_AFLAG_MEDIUM_PRESENT, &drive->atapi_flags);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001973 return retval;
1974 case MTNOP:
Borislav Petkovec0fdb02008-04-27 15:38:34 +02001975 ide_tape_discard_merge_buffer(drive, 0);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001976 return idetape_flush_tape_buffers(drive);
1977 case MTRETEN:
Borislav Petkovec0fdb02008-04-27 15:38:34 +02001978 ide_tape_discard_merge_buffer(drive, 0);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001979 idetape_create_load_unload_cmd(drive, &pc,
1980 IDETAPE_LU_RETENSION_MASK | IDETAPE_LU_LOAD_MASK);
1981 return idetape_queue_pc_tail(drive, &pc);
1982 case MTEOM:
1983 idetape_create_space_cmd(&pc, 0, IDETAPE_SPACE_TO_EOD);
1984 return idetape_queue_pc_tail(drive, &pc);
1985 case MTERASE:
1986 (void)idetape_rewind_tape(drive);
1987 idetape_create_erase_cmd(&pc);
1988 return idetape_queue_pc_tail(drive, &pc);
1989 case MTSETBLK:
1990 if (mt_count) {
1991 if (mt_count < tape->blk_size ||
1992 mt_count % tape->blk_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001993 return -EIO;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001994 tape->user_bs_factor = mt_count / tape->blk_size;
Borislav Petkovf2e3ab52008-07-23 19:56:01 +02001995 clear_bit(IDE_AFLAG_DETECT_BS, &drive->atapi_flags);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001996 } else
Borislav Petkovf2e3ab52008-07-23 19:56:01 +02001997 set_bit(IDE_AFLAG_DETECT_BS, &drive->atapi_flags);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001998 return 0;
1999 case MTSEEK:
Borislav Petkovec0fdb02008-04-27 15:38:34 +02002000 ide_tape_discard_merge_buffer(drive, 0);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002001 return idetape_position_tape(drive,
2002 mt_count * tape->user_bs_factor, tape->partition, 0);
2003 case MTSETPART:
Borislav Petkovec0fdb02008-04-27 15:38:34 +02002004 ide_tape_discard_merge_buffer(drive, 0);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002005 return idetape_position_tape(drive, 0, mt_count, 0);
2006 case MTFSR:
2007 case MTBSR:
2008 case MTLOCK:
2009 if (!idetape_create_prevent_cmd(drive, &pc, 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002010 return 0;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002011 retval = idetape_queue_pc_tail(drive, &pc);
2012 if (retval)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002013 return retval;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002014 tape->door_locked = DOOR_EXPLICITLY_LOCKED;
2015 return 0;
2016 case MTUNLOCK:
2017 if (!idetape_create_prevent_cmd(drive, &pc, 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002018 return 0;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002019 retval = idetape_queue_pc_tail(drive, &pc);
2020 if (retval)
2021 return retval;
2022 tape->door_locked = DOOR_UNLOCKED;
2023 return 0;
2024 default:
2025 printk(KERN_ERR "ide-tape: MTIO operation %d not supported\n",
2026 mt_op);
2027 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002028 }
2029}
2030
2031/*
Borislav Petkovd99c9da2008-02-02 19:56:51 +01002032 * Our character device ioctls. General mtio.h magnetic io commands are
2033 * supported here, and not in the corresponding block interface. Our own
2034 * ide-tape ioctls are supported on both interfaces.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002035 */
Borislav Petkovd99c9da2008-02-02 19:56:51 +01002036static int idetape_chrdev_ioctl(struct inode *inode, struct file *file,
2037 unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002038{
2039 struct ide_tape_obj *tape = ide_tape_f(file);
2040 ide_drive_t *drive = tape->drive;
2041 struct mtop mtop;
2042 struct mtget mtget;
2043 struct mtpos mtpos;
Borislav Petkov54bb2072008-02-06 02:57:52 +01002044 int block_offset = 0, position = tape->first_frame;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002045 void __user *argp = (void __user *)arg;
2046
Borislav Petkov8004a8c2008-02-06 02:57:51 +01002047 debug_log(DBG_CHRDEV, "Enter %s, cmd=%u\n", __func__, cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002048
Borislav Petkov54abf372008-02-06 02:57:52 +01002049 if (tape->chrdev_dir == IDETAPE_DIR_WRITE) {
Borislav Petkovd9df9372008-04-27 15:38:34 +02002050 ide_tape_flush_merge_buffer(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002051 idetape_flush_tape_buffers(drive);
2052 }
2053 if (cmd == MTIOCGET || cmd == MTIOCPOS) {
Borislav Petkov01a63aeb2008-04-27 15:38:34 +02002054 block_offset = tape->merge_bh_size /
Borislav Petkov54bb2072008-02-06 02:57:52 +01002055 (tape->blk_size * tape->user_bs_factor);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002056 position = idetape_read_position(drive);
2057 if (position < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002058 return -EIO;
2059 }
2060 switch (cmd) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002061 case MTIOCTOP:
2062 if (copy_from_user(&mtop, argp, sizeof(struct mtop)))
2063 return -EFAULT;
2064 return idetape_mtioctop(drive, mtop.mt_op, mtop.mt_count);
2065 case MTIOCGET:
2066 memset(&mtget, 0, sizeof(struct mtget));
2067 mtget.mt_type = MT_ISSCSI2;
2068 mtget.mt_blkno = position / tape->user_bs_factor - block_offset;
2069 mtget.mt_dsreg =
2070 ((tape->blk_size * tape->user_bs_factor)
2071 << MT_ST_BLKSIZE_SHIFT) & MT_ST_BLKSIZE_MASK;
Borislav Petkov54bb2072008-02-06 02:57:52 +01002072
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002073 if (tape->drv_write_prot)
2074 mtget.mt_gstat |= GMT_WR_PROT(0xffffffff);
2075
2076 if (copy_to_user(argp, &mtget, sizeof(struct mtget)))
2077 return -EFAULT;
2078 return 0;
2079 case MTIOCPOS:
2080 mtpos.mt_blkno = position / tape->user_bs_factor - block_offset;
2081 if (copy_to_user(argp, &mtpos, sizeof(struct mtpos)))
2082 return -EFAULT;
2083 return 0;
2084 default:
2085 if (tape->chrdev_dir == IDETAPE_DIR_READ)
Borislav Petkovec0fdb02008-04-27 15:38:34 +02002086 ide_tape_discard_merge_buffer(drive, 1);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002087 return idetape_blkdev_ioctl(drive, cmd, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002088 }
2089}
2090
Borislav Petkov3cffb9c2008-02-02 19:56:50 +01002091/*
2092 * Do a mode sense page 0 with block descriptor and if it succeeds set the tape
2093 * block size with the reported value.
2094 */
2095static void ide_tape_get_bsize_from_bdesc(ide_drive_t *drive)
2096{
2097 idetape_tape_t *tape = drive->driver_data;
Borislav Petkovd236d742008-04-18 00:46:27 +02002098 struct ide_atapi_pc pc;
Borislav Petkov3cffb9c2008-02-02 19:56:50 +01002099
2100 idetape_create_mode_sense_cmd(&pc, IDETAPE_BLOCK_DESCRIPTOR);
2101 if (idetape_queue_pc_tail(drive, &pc)) {
2102 printk(KERN_ERR "ide-tape: Can't get block descriptor\n");
Borislav Petkov54bb2072008-02-06 02:57:52 +01002103 if (tape->blk_size == 0) {
Borislav Petkov3cffb9c2008-02-02 19:56:50 +01002104 printk(KERN_WARNING "ide-tape: Cannot deal with zero "
2105 "block size, assuming 32k\n");
Borislav Petkov54bb2072008-02-06 02:57:52 +01002106 tape->blk_size = 32768;
Borislav Petkov3cffb9c2008-02-02 19:56:50 +01002107 }
2108 return;
2109 }
Borislav Petkovd236d742008-04-18 00:46:27 +02002110 tape->blk_size = (pc.buf[4 + 5] << 16) +
2111 (pc.buf[4 + 6] << 8) +
2112 pc.buf[4 + 7];
2113 tape->drv_write_prot = (pc.buf[2] & 0x80) >> 7;
Borislav Petkov3cffb9c2008-02-02 19:56:50 +01002114}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002115
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002116static int idetape_chrdev_open(struct inode *inode, struct file *filp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002117{
2118 unsigned int minor = iminor(inode), i = minor & ~0xc0;
2119 ide_drive_t *drive;
2120 idetape_tape_t *tape;
Borislav Petkovd236d742008-04-18 00:46:27 +02002121 struct ide_atapi_pc pc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002122 int retval;
2123
Borislav Petkov8004a8c2008-02-06 02:57:51 +01002124 if (i >= MAX_HWIFS * MAX_DRIVES)
2125 return -ENXIO;
2126
Jonathan Corbet04f4ac92008-05-15 12:01:56 -06002127 lock_kernel();
Borislav Petkov8004a8c2008-02-06 02:57:51 +01002128 tape = ide_tape_chrdev_get(i);
Jonathan Corbet04f4ac92008-05-15 12:01:56 -06002129 if (!tape) {
2130 unlock_kernel();
Borislav Petkov8004a8c2008-02-06 02:57:51 +01002131 return -ENXIO;
Jonathan Corbet04f4ac92008-05-15 12:01:56 -06002132 }
Borislav Petkov8004a8c2008-02-06 02:57:51 +01002133
2134 debug_log(DBG_CHRDEV, "Enter %s\n", __func__);
2135
Linus Torvalds1da177e2005-04-16 15:20:36 -07002136 /*
2137 * We really want to do nonseekable_open(inode, filp); here, but some
2138 * versions of tar incorrectly call lseek on tapes and bail out if that
2139 * fails. So we disallow pread() and pwrite(), but permit lseeks.
2140 */
2141 filp->f_mode &= ~(FMODE_PREAD | FMODE_PWRITE);
2142
Linus Torvalds1da177e2005-04-16 15:20:36 -07002143 drive = tape->drive;
2144
2145 filp->private_data = tape;
2146
Borislav Petkovf2e3ab52008-07-23 19:56:01 +02002147 if (test_and_set_bit(IDE_AFLAG_BUSY, &drive->atapi_flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002148 retval = -EBUSY;
2149 goto out_put_tape;
2150 }
2151
2152 retval = idetape_wait_ready(drive, 60 * HZ);
2153 if (retval) {
Borislav Petkovf2e3ab52008-07-23 19:56:01 +02002154 clear_bit(IDE_AFLAG_BUSY, &drive->atapi_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002155 printk(KERN_ERR "ide-tape: %s: drive not ready\n", tape->name);
2156 goto out_put_tape;
2157 }
2158
2159 idetape_read_position(drive);
Borislav Petkovf2e3ab52008-07-23 19:56:01 +02002160 if (!test_bit(IDE_AFLAG_ADDRESS_VALID, &drive->atapi_flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002161 (void)idetape_rewind_tape(drive);
2162
Linus Torvalds1da177e2005-04-16 15:20:36 -07002163 /* Read block size and write protect status from drive. */
Borislav Petkov3cffb9c2008-02-02 19:56:50 +01002164 ide_tape_get_bsize_from_bdesc(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002165
2166 /* Set write protect flag if device is opened as read-only. */
2167 if ((filp->f_flags & O_ACCMODE) == O_RDONLY)
2168 tape->write_prot = 1;
2169 else
2170 tape->write_prot = tape->drv_write_prot;
2171
2172 /* Make sure drive isn't write protected if user wants to write. */
2173 if (tape->write_prot) {
2174 if ((filp->f_flags & O_ACCMODE) == O_WRONLY ||
2175 (filp->f_flags & O_ACCMODE) == O_RDWR) {
Borislav Petkovf2e3ab52008-07-23 19:56:01 +02002176 clear_bit(IDE_AFLAG_BUSY, &drive->atapi_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002177 retval = -EROFS;
2178 goto out_put_tape;
2179 }
2180 }
2181
Borislav Petkov3c98bf32008-02-06 02:57:53 +01002182 /* Lock the tape drive door so user can't eject. */
Borislav Petkov54abf372008-02-06 02:57:52 +01002183 if (tape->chrdev_dir == IDETAPE_DIR_NONE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002184 if (idetape_create_prevent_cmd(drive, &pc, 1)) {
2185 if (!idetape_queue_pc_tail(drive, &pc)) {
2186 if (tape->door_locked != DOOR_EXPLICITLY_LOCKED)
2187 tape->door_locked = DOOR_LOCKED;
2188 }
2189 }
2190 }
Jonathan Corbet04f4ac92008-05-15 12:01:56 -06002191 unlock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002192 return 0;
2193
2194out_put_tape:
2195 ide_tape_put(tape);
Jonathan Corbet04f4ac92008-05-15 12:01:56 -06002196 unlock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002197 return retval;
2198}
2199
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002200static void idetape_write_release(ide_drive_t *drive, unsigned int minor)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002201{
2202 idetape_tape_t *tape = drive->driver_data;
2203
Borislav Petkovd9df9372008-04-27 15:38:34 +02002204 ide_tape_flush_merge_buffer(drive);
Borislav Petkov077e3bd2008-04-27 15:38:34 +02002205 tape->merge_bh = ide_tape_kmalloc_buffer(tape, 1, 0);
2206 if (tape->merge_bh != NULL) {
Borislav Petkov54bb2072008-02-06 02:57:52 +01002207 idetape_pad_zeros(drive, tape->blk_size *
2208 (tape->user_bs_factor - 1));
Borislav Petkov077e3bd2008-04-27 15:38:34 +02002209 ide_tape_kfree_buffer(tape);
2210 tape->merge_bh = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002211 }
2212 idetape_write_filemark(drive);
2213 idetape_flush_tape_buffers(drive);
2214 idetape_flush_tape_buffers(drive);
2215}
2216
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002217static int idetape_chrdev_release(struct inode *inode, struct file *filp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002218{
2219 struct ide_tape_obj *tape = ide_tape_f(filp);
2220 ide_drive_t *drive = tape->drive;
Borislav Petkovd236d742008-04-18 00:46:27 +02002221 struct ide_atapi_pc pc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002222 unsigned int minor = iminor(inode);
2223
2224 lock_kernel();
2225 tape = drive->driver_data;
Borislav Petkov8004a8c2008-02-06 02:57:51 +01002226
2227 debug_log(DBG_CHRDEV, "Enter %s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002228
Borislav Petkov54abf372008-02-06 02:57:52 +01002229 if (tape->chrdev_dir == IDETAPE_DIR_WRITE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002230 idetape_write_release(drive, minor);
Borislav Petkov54abf372008-02-06 02:57:52 +01002231 if (tape->chrdev_dir == IDETAPE_DIR_READ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002232 if (minor < 128)
Borislav Petkovec0fdb02008-04-27 15:38:34 +02002233 ide_tape_discard_merge_buffer(drive, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002234 }
Borislav Petkovf64eee72008-04-27 15:38:25 +02002235
Borislav Petkovf2e3ab52008-07-23 19:56:01 +02002236 if (minor < 128 && test_bit(IDE_AFLAG_MEDIUM_PRESENT, &drive->atapi_flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002237 (void) idetape_rewind_tape(drive);
Borislav Petkov54abf372008-02-06 02:57:52 +01002238 if (tape->chrdev_dir == IDETAPE_DIR_NONE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002239 if (tape->door_locked == DOOR_LOCKED) {
2240 if (idetape_create_prevent_cmd(drive, &pc, 0)) {
2241 if (!idetape_queue_pc_tail(drive, &pc))
2242 tape->door_locked = DOOR_UNLOCKED;
2243 }
2244 }
2245 }
Borislav Petkovf2e3ab52008-07-23 19:56:01 +02002246 clear_bit(IDE_AFLAG_BUSY, &drive->atapi_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002247 ide_tape_put(tape);
2248 unlock_kernel();
2249 return 0;
2250}
2251
Borislav Petkov6d29c8f2008-02-02 19:56:49 +01002252static void idetape_get_inquiry_results(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002253{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002254 idetape_tape_t *tape = drive->driver_data;
Borislav Petkovd236d742008-04-18 00:46:27 +02002255 struct ide_atapi_pc pc;
Borislav Petkov801bd322008-09-27 19:32:17 +02002256 char fw_rev[4], vendor_id[8], product_id[16];
Borislav Petkov6d29c8f2008-02-02 19:56:49 +01002257
Linus Torvalds1da177e2005-04-16 15:20:36 -07002258 idetape_create_inquiry_cmd(&pc);
2259 if (idetape_queue_pc_tail(drive, &pc)) {
Borislav Petkov6d29c8f2008-02-02 19:56:49 +01002260 printk(KERN_ERR "ide-tape: %s: can't get INQUIRY results\n",
2261 tape->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002262 return;
2263 }
Borislav Petkovd236d742008-04-18 00:46:27 +02002264 memcpy(vendor_id, &pc.buf[8], 8);
2265 memcpy(product_id, &pc.buf[16], 16);
2266 memcpy(fw_rev, &pc.buf[32], 4);
Borislav Petkov6d29c8f2008-02-02 19:56:49 +01002267
Borislav Petkov801bd322008-09-27 19:32:17 +02002268 ide_fixstring(vendor_id, 8, 0);
2269 ide_fixstring(product_id, 16, 0);
2270 ide_fixstring(fw_rev, 4, 0);
Borislav Petkov41f81d542008-02-06 02:57:52 +01002271
Borislav Petkov801bd322008-09-27 19:32:17 +02002272 printk(KERN_INFO "ide-tape: %s <-> %s: %.8s %.16s rev %.4s\n",
Borislav Petkov41f81d542008-02-06 02:57:52 +01002273 drive->name, tape->name, vendor_id, product_id, fw_rev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002274}
2275
2276/*
Borislav Petkovb6422012008-02-02 19:56:49 +01002277 * Ask the tape about its various parameters. In particular, we will adjust our
2278 * data transfer buffer size to the recommended value as returned by the tape.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002279 */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002280static void idetape_get_mode_sense_results(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002281{
2282 idetape_tape_t *tape = drive->driver_data;
Borislav Petkovd236d742008-04-18 00:46:27 +02002283 struct ide_atapi_pc pc;
Borislav Petkovb6422012008-02-02 19:56:49 +01002284 u8 *caps;
2285 u8 speed, max_speed;
Borislav Petkov47314fa2008-02-02 19:56:48 +01002286
Linus Torvalds1da177e2005-04-16 15:20:36 -07002287 idetape_create_mode_sense_cmd(&pc, IDETAPE_CAPABILITIES_PAGE);
2288 if (idetape_queue_pc_tail(drive, &pc)) {
Borislav Petkovb6422012008-02-02 19:56:49 +01002289 printk(KERN_ERR "ide-tape: Can't get tape parameters - assuming"
2290 " some default values\n");
Borislav Petkov54bb2072008-02-06 02:57:52 +01002291 tape->blk_size = 512;
Borislav Petkovb6422012008-02-02 19:56:49 +01002292 put_unaligned(52, (u16 *)&tape->caps[12]);
2293 put_unaligned(540, (u16 *)&tape->caps[14]);
2294 put_unaligned(6*52, (u16 *)&tape->caps[16]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002295 return;
2296 }
Borislav Petkovd236d742008-04-18 00:46:27 +02002297 caps = pc.buf + 4 + pc.buf[3];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002298
Borislav Petkovb6422012008-02-02 19:56:49 +01002299 /* convert to host order and save for later use */
Harvey Harrisoncd740ab2008-07-24 22:53:33 +02002300 speed = be16_to_cpup((__be16 *)&caps[14]);
2301 max_speed = be16_to_cpup((__be16 *)&caps[8]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002302
Harvey Harrisoncd740ab2008-07-24 22:53:33 +02002303 *(u16 *)&caps[8] = max_speed;
2304 *(u16 *)&caps[12] = be16_to_cpup((__be16 *)&caps[12]);
2305 *(u16 *)&caps[14] = speed;
2306 *(u16 *)&caps[16] = be16_to_cpup((__be16 *)&caps[16]);
Borislav Petkovb6422012008-02-02 19:56:49 +01002307
2308 if (!speed) {
2309 printk(KERN_INFO "ide-tape: %s: invalid tape speed "
2310 "(assuming 650KB/sec)\n", drive->name);
Harvey Harrisoncd740ab2008-07-24 22:53:33 +02002311 *(u16 *)&caps[14] = 650;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002312 }
Borislav Petkovb6422012008-02-02 19:56:49 +01002313 if (!max_speed) {
2314 printk(KERN_INFO "ide-tape: %s: invalid max_speed "
2315 "(assuming 650KB/sec)\n", drive->name);
Harvey Harrisoncd740ab2008-07-24 22:53:33 +02002316 *(u16 *)&caps[8] = 650;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002317 }
2318
Borislav Petkovb6422012008-02-02 19:56:49 +01002319 memcpy(&tape->caps, caps, 20);
2320 if (caps[7] & 0x02)
Borislav Petkov54bb2072008-02-06 02:57:52 +01002321 tape->blk_size = 512;
Borislav Petkovb6422012008-02-02 19:56:49 +01002322 else if (caps[7] & 0x04)
Borislav Petkov54bb2072008-02-06 02:57:52 +01002323 tape->blk_size = 1024;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002324}
2325
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +02002326#ifdef CONFIG_IDE_PROC_FS
Bartlomiej Zolnierkiewicz8185d5a2008-10-10 22:39:28 +02002327#define ide_tape_devset_get(name, field) \
2328static int get_##name(ide_drive_t *drive) \
2329{ \
2330 idetape_tape_t *tape = drive->driver_data; \
2331 return tape->field; \
Linus Torvalds1da177e2005-04-16 15:20:36 -07002332}
Bartlomiej Zolnierkiewicz8185d5a2008-10-10 22:39:28 +02002333
2334#define ide_tape_devset_set(name, field) \
2335static int set_##name(ide_drive_t *drive, int arg) \
2336{ \
2337 idetape_tape_t *tape = drive->driver_data; \
2338 tape->field = arg; \
2339 return 0; \
2340}
2341
2342#define ide_tape_devset_rw(_name, _min, _max, _field, _mulf, _divf) \
2343ide_tape_devset_get(_name, _field) \
2344ide_tape_devset_set(_name, _field) \
2345__IDE_DEVSET(_name, S_RW, _min, _max, get_##_name, set_##_name, _mulf, _divf)
2346
2347#define ide_tape_devset_r(_name, _min, _max, _field, _mulf, _divf) \
2348ide_tape_devset_get(_name, _field) \
2349__IDE_DEVSET(_name, S_READ, _min, _max, get_##_name, NULL, _mulf, _divf)
2350
2351static int mulf_tdsc(ide_drive_t *drive) { return 1000; }
2352static int divf_tdsc(ide_drive_t *drive) { return HZ; }
2353static int divf_buffer(ide_drive_t *drive) { return 2; }
2354static int divf_buffer_size(ide_drive_t *drive) { return 1024; }
2355
2356ide_devset_rw(dsc_overlap, 0, 1, dsc_overlap);
2357
2358ide_tape_devset_rw(debug_mask, 0, 0xffff, debug_mask, NULL, NULL);
2359ide_tape_devset_rw(tdsc, IDETAPE_DSC_RW_MIN, IDETAPE_DSC_RW_MAX,
2360 best_dsc_rw_freq, mulf_tdsc, divf_tdsc);
2361
2362ide_tape_devset_r(avg_speed, 0, 0xffff, avg_speed, NULL, NULL);
2363ide_tape_devset_r(speed, 0, 0xffff, caps[14], NULL, NULL);
2364ide_tape_devset_r(buffer, 0, 0xffff, caps[16], NULL, divf_buffer);
2365ide_tape_devset_r(buffer_size, 0, 0xffff, buffer_size, NULL, divf_buffer_size);
2366
2367static const struct ide_devset *idetape_settings[] = {
2368 &ide_devset_avg_speed,
2369 &ide_devset_buffer,
2370 &ide_devset_buffer_size,
2371 &ide_devset_debug_mask,
2372 &ide_devset_dsc_overlap,
2373 &ide_devset_speed,
2374 &ide_devset_tdsc,
2375 NULL
2376};
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +02002377#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002378
2379/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +01002380 * The function below is called to:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002381 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +01002382 * 1. Initialize our various state variables.
2383 * 2. Ask the tape for its capabilities.
2384 * 3. Allocate a buffer which will be used for data transfer. The buffer size
2385 * is chosen based on the recommendation which we received in step 2.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002386 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +01002387 * Note that at this point ide.c already assigned us an irq, so that we can
2388 * queue requests here and wait for their completion.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002389 */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002390static void idetape_setup(ide_drive_t *drive, idetape_tape_t *tape, int minor)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002391{
Borislav Petkov83042b22008-04-27 15:38:27 +02002392 unsigned long t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002393 int speed;
Borislav Petkovf73850a2008-04-27 15:38:33 +02002394 int buffer_size;
Borislav Petkov71071b82008-02-06 02:57:53 +01002395 u8 gcw[2];
Borislav Petkovb6422012008-02-02 19:56:49 +01002396 u16 *ctl = (u16 *)&tape->caps[12];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002397
Borislav Petkov776bb022008-07-23 19:55:59 +02002398 drive->pc_callback = ide_tape_callback;
2399
Borislav Petkov54bb2072008-02-06 02:57:52 +01002400 spin_lock_init(&tape->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002401 drive->dsc_overlap = 1;
Bartlomiej Zolnierkiewicz4166c192008-02-01 23:09:30 +01002402 if (drive->hwif->host_flags & IDE_HFLAG_NO_DSC) {
2403 printk(KERN_INFO "ide-tape: %s: disabling DSC overlap\n",
2404 tape->name);
2405 drive->dsc_overlap = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002406 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002407 /* Seagate Travan drives do not support DSC overlap. */
Bartlomiej Zolnierkiewicz4dde4492008-10-10 22:39:19 +02002408 if (strstr((char *)&drive->id[ATA_ID_PROD], "Seagate STT3401"))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002409 drive->dsc_overlap = 0;
2410 tape->minor = minor;
2411 tape->name[0] = 'h';
2412 tape->name[1] = 't';
2413 tape->name[2] = '0' + minor;
Borislav Petkov54abf372008-02-06 02:57:52 +01002414 tape->chrdev_dir = IDETAPE_DIR_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002415 tape->pc = tape->pc_stack;
Bartlomiej Zolnierkiewicz4dde4492008-10-10 22:39:19 +02002416
2417 *((u16 *)&gcw) = drive->id[ATA_ID_CONFIG];
Borislav Petkov71071b82008-02-06 02:57:53 +01002418
2419 /* Command packet DRQ type */
2420 if (((gcw[0] & 0x60) >> 5) == 1)
Borislav Petkovf2e3ab52008-07-23 19:56:01 +02002421 set_bit(IDE_AFLAG_DRQ_INTERRUPT, &drive->atapi_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002422
Linus Torvalds1da177e2005-04-16 15:20:36 -07002423 idetape_get_inquiry_results(drive);
2424 idetape_get_mode_sense_results(drive);
Borislav Petkov3cffb9c2008-02-02 19:56:50 +01002425 ide_tape_get_bsize_from_bdesc(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002426 tape->user_bs_factor = 1;
Borislav Petkovf73850a2008-04-27 15:38:33 +02002427 tape->buffer_size = *ctl * tape->blk_size;
2428 while (tape->buffer_size > 0xffff) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002429 printk(KERN_NOTICE "ide-tape: decreasing stage size\n");
Borislav Petkovb6422012008-02-02 19:56:49 +01002430 *ctl /= 2;
Borislav Petkovf73850a2008-04-27 15:38:33 +02002431 tape->buffer_size = *ctl * tape->blk_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002432 }
Borislav Petkovf73850a2008-04-27 15:38:33 +02002433 buffer_size = tape->buffer_size;
Borislav Petkova997a432008-04-27 15:38:33 +02002434 tape->pages_per_buffer = buffer_size / PAGE_SIZE;
Borislav Petkovf73850a2008-04-27 15:38:33 +02002435 if (buffer_size % PAGE_SIZE) {
Borislav Petkova997a432008-04-27 15:38:33 +02002436 tape->pages_per_buffer++;
Borislav Petkovf73850a2008-04-27 15:38:33 +02002437 tape->excess_bh_size = PAGE_SIZE - buffer_size % PAGE_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002438 }
2439
Borislav Petkov83042b22008-04-27 15:38:27 +02002440 /* select the "best" DSC read/write polling freq */
Borislav Petkovb6422012008-02-02 19:56:49 +01002441 speed = max(*(u16 *)&tape->caps[14], *(u16 *)&tape->caps[8]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002442
Borislav Petkovf73850a2008-04-27 15:38:33 +02002443 t = (IDETAPE_FIFO_THRESHOLD * tape->buffer_size * HZ) / (speed * 1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002444
2445 /*
Borislav Petkov3c98bf32008-02-06 02:57:53 +01002446 * Ensure that the number we got makes sense; limit it within
2447 * IDETAPE_DSC_RW_MIN and IDETAPE_DSC_RW_MAX.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002448 */
Harvey Harrisona792bd52008-07-15 21:21:41 +02002449 tape->best_dsc_rw_freq = clamp_t(unsigned long, t, IDETAPE_DSC_RW_MIN,
2450 IDETAPE_DSC_RW_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002451 printk(KERN_INFO "ide-tape: %s <-> %s: %dKBps, %d*%dkB buffer, "
Borislav Petkov83042b22008-04-27 15:38:27 +02002452 "%lums tDSC%s\n",
Borislav Petkovb6422012008-02-02 19:56:49 +01002453 drive->name, tape->name, *(u16 *)&tape->caps[14],
Borislav Petkovf73850a2008-04-27 15:38:33 +02002454 (*(u16 *)&tape->caps[16] * 512) / tape->buffer_size,
2455 tape->buffer_size / 1024,
Borislav Petkov54bb2072008-02-06 02:57:52 +01002456 tape->best_dsc_rw_freq * 1000 / HZ,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002457 drive->using_dma ? ", DMA":"");
2458
Bartlomiej Zolnierkiewicz1e874f42008-10-10 22:39:27 +02002459 ide_proc_register_driver(drive, tape->driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002460}
2461
Russell King4031bbe2006-01-06 11:41:00 +00002462static void ide_tape_remove(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002463{
2464 idetape_tape_t *tape = drive->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002465
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +02002466 ide_proc_unregister_driver(drive, tape->driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002467
2468 ide_unregister_region(tape->disk);
2469
2470 ide_tape_put(tape);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002471}
2472
2473static void ide_tape_release(struct kref *kref)
2474{
2475 struct ide_tape_obj *tape = to_ide_tape(kref);
2476 ide_drive_t *drive = tape->drive;
2477 struct gendisk *g = tape->disk;
2478
Borislav Petkov01a63aeb2008-04-27 15:38:34 +02002479 BUG_ON(tape->merge_bh_size);
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02002480
Linus Torvalds1da177e2005-04-16 15:20:36 -07002481 drive->dsc_overlap = 0;
2482 drive->driver_data = NULL;
Tony Jonesdbc12722007-09-25 02:03:03 +02002483 device_destroy(idetape_sysfs_class, MKDEV(IDETAPE_MAJOR, tape->minor));
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002484 device_destroy(idetape_sysfs_class,
2485 MKDEV(IDETAPE_MAJOR, tape->minor + 128));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002486 idetape_devs[tape->minor] = NULL;
2487 g->private_data = NULL;
2488 put_disk(g);
2489 kfree(tape);
2490}
2491
Bartlomiej Zolnierkiewiczecfd80e2007-05-10 00:01:09 +02002492#ifdef CONFIG_IDE_PROC_FS
Linus Torvalds1da177e2005-04-16 15:20:36 -07002493static int proc_idetape_read_name
2494 (char *page, char **start, off_t off, int count, int *eof, void *data)
2495{
2496 ide_drive_t *drive = (ide_drive_t *) data;
2497 idetape_tape_t *tape = drive->driver_data;
2498 char *out = page;
2499 int len;
2500
2501 len = sprintf(out, "%s\n", tape->name);
2502 PROC_IDE_READ_RETURN(page, start, off, count, eof, len);
2503}
2504
2505static ide_proc_entry_t idetape_proc[] = {
2506 { "capacity", S_IFREG|S_IRUGO, proc_ide_read_capacity, NULL },
2507 { "name", S_IFREG|S_IRUGO, proc_idetape_read_name, NULL },
2508 { NULL, 0, NULL, NULL }
2509};
Linus Torvalds1da177e2005-04-16 15:20:36 -07002510#endif
2511
Russell King4031bbe2006-01-06 11:41:00 +00002512static int ide_tape_probe(ide_drive_t *);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002513
Linus Torvalds1da177e2005-04-16 15:20:36 -07002514static ide_driver_t idetape_driver = {
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02002515 .gen_driver = {
Laurent Riffard4ef3b8f2005-11-18 22:15:40 +01002516 .owner = THIS_MODULE,
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02002517 .name = "ide-tape",
2518 .bus = &ide_bus_type,
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02002519 },
Russell King4031bbe2006-01-06 11:41:00 +00002520 .probe = ide_tape_probe,
2521 .remove = ide_tape_remove,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002522 .version = IDETAPE_VERSION,
2523 .media = ide_tape,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002524 .do_request = idetape_do_request,
2525 .end_request = idetape_end_request,
2526 .error = __ide_error,
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +02002527#ifdef CONFIG_IDE_PROC_FS
Linus Torvalds1da177e2005-04-16 15:20:36 -07002528 .proc = idetape_proc,
Bartlomiej Zolnierkiewicz8185d5a2008-10-10 22:39:28 +02002529 .settings = idetape_settings,
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +02002530#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002531};
2532
Borislav Petkov3c98bf32008-02-06 02:57:53 +01002533/* Our character device supporting functions, passed to register_chrdev. */
Arjan van de Ven2b8693c2007-02-12 00:55:32 -08002534static const struct file_operations idetape_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002535 .owner = THIS_MODULE,
2536 .read = idetape_chrdev_read,
2537 .write = idetape_chrdev_write,
2538 .ioctl = idetape_chrdev_ioctl,
2539 .open = idetape_chrdev_open,
2540 .release = idetape_chrdev_release,
2541};
2542
2543static int idetape_open(struct inode *inode, struct file *filp)
2544{
2545 struct gendisk *disk = inode->i_bdev->bd_disk;
2546 struct ide_tape_obj *tape;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002547
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002548 tape = ide_tape_get(disk);
2549 if (!tape)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002550 return -ENXIO;
2551
Linus Torvalds1da177e2005-04-16 15:20:36 -07002552 return 0;
2553}
2554
2555static int idetape_release(struct inode *inode, struct file *filp)
2556{
2557 struct gendisk *disk = inode->i_bdev->bd_disk;
2558 struct ide_tape_obj *tape = ide_tape_g(disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002559
2560 ide_tape_put(tape);
2561
2562 return 0;
2563}
2564
2565static int idetape_ioctl(struct inode *inode, struct file *file,
2566 unsigned int cmd, unsigned long arg)
2567{
2568 struct block_device *bdev = inode->i_bdev;
2569 struct ide_tape_obj *tape = ide_tape_g(bdev->bd_disk);
2570 ide_drive_t *drive = tape->drive;
2571 int err = generic_ide_ioctl(drive, file, bdev, cmd, arg);
2572 if (err == -EINVAL)
2573 err = idetape_blkdev_ioctl(drive, cmd, arg);
2574 return err;
2575}
2576
2577static struct block_device_operations idetape_block_ops = {
2578 .owner = THIS_MODULE,
2579 .open = idetape_open,
2580 .release = idetape_release,
2581 .ioctl = idetape_ioctl,
2582};
2583
Russell King4031bbe2006-01-06 11:41:00 +00002584static int ide_tape_probe(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002585{
2586 idetape_tape_t *tape;
2587 struct gendisk *g;
2588 int minor;
2589
2590 if (!strstr("ide-tape", drive->driver_req))
2591 goto failed;
Bartlomiej Zolnierkiewicz2a924662008-10-10 22:39:24 +02002592
Linus Torvalds1da177e2005-04-16 15:20:36 -07002593 if (drive->media != ide_tape)
2594 goto failed;
Bartlomiej Zolnierkiewicz2a924662008-10-10 22:39:24 +02002595
Bartlomiej Zolnierkiewicz51509ee2008-10-10 22:39:34 +02002596 if (drive->id_read == 1 && !ide_check_atapi_device(drive, DRV_NAME)) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002597 printk(KERN_ERR "ide-tape: %s: not supported by this version of"
2598 " the driver\n", drive->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002599 goto failed;
2600 }
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002601 tape = kzalloc(sizeof(idetape_tape_t), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002602 if (tape == NULL) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002603 printk(KERN_ERR "ide-tape: %s: Can't allocate a tape struct\n",
2604 drive->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002605 goto failed;
2606 }
2607
2608 g = alloc_disk(1 << PARTN_BITS);
2609 if (!g)
2610 goto out_free_tape;
2611
2612 ide_init_disk(g, drive);
2613
Linus Torvalds1da177e2005-04-16 15:20:36 -07002614 kref_init(&tape->kref);
2615
2616 tape->drive = drive;
2617 tape->driver = &idetape_driver;
2618 tape->disk = g;
2619
2620 g->private_data = &tape->driver;
2621
2622 drive->driver_data = tape;
2623
Arjan van de Vencf8b8972006-03-23 03:00:45 -08002624 mutex_lock(&idetape_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002625 for (minor = 0; idetape_devs[minor]; minor++)
2626 ;
2627 idetape_devs[minor] = tape;
Arjan van de Vencf8b8972006-03-23 03:00:45 -08002628 mutex_unlock(&idetape_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002629
2630 idetape_setup(drive, tape, minor);
2631
Greg Kroah-Hartman6ecaaf92008-05-21 12:52:33 -07002632 device_create_drvdata(idetape_sysfs_class, &drive->gendev,
2633 MKDEV(IDETAPE_MAJOR, minor), NULL,
2634 "%s", tape->name);
2635 device_create_drvdata(idetape_sysfs_class, &drive->gendev,
2636 MKDEV(IDETAPE_MAJOR, minor + 128), NULL,
2637 "n%s", tape->name);
Will Dysond5dee802005-09-16 02:55:07 -07002638
Linus Torvalds1da177e2005-04-16 15:20:36 -07002639 g->fops = &idetape_block_ops;
2640 ide_register_region(g);
2641
2642 return 0;
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02002643
Linus Torvalds1da177e2005-04-16 15:20:36 -07002644out_free_tape:
2645 kfree(tape);
2646failed:
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02002647 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002648}
2649
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002650static void __exit idetape_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002651{
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02002652 driver_unregister(&idetape_driver.gen_driver);
Will Dysond5dee802005-09-16 02:55:07 -07002653 class_destroy(idetape_sysfs_class);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002654 unregister_chrdev(IDETAPE_MAJOR, "ht");
2655}
2656
Bartlomiej Zolnierkiewicz17514e82005-11-19 22:24:35 +01002657static int __init idetape_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002658{
Will Dysond5dee802005-09-16 02:55:07 -07002659 int error = 1;
2660 idetape_sysfs_class = class_create(THIS_MODULE, "ide_tape");
2661 if (IS_ERR(idetape_sysfs_class)) {
2662 idetape_sysfs_class = NULL;
2663 printk(KERN_ERR "Unable to create sysfs class for ide tapes\n");
2664 error = -EBUSY;
2665 goto out;
2666 }
2667
Linus Torvalds1da177e2005-04-16 15:20:36 -07002668 if (register_chrdev(IDETAPE_MAJOR, "ht", &idetape_fops)) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002669 printk(KERN_ERR "ide-tape: Failed to register chrdev"
2670 " interface\n");
Will Dysond5dee802005-09-16 02:55:07 -07002671 error = -EBUSY;
2672 goto out_free_class;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002673 }
Will Dysond5dee802005-09-16 02:55:07 -07002674
2675 error = driver_register(&idetape_driver.gen_driver);
2676 if (error)
2677 goto out_free_driver;
2678
2679 return 0;
2680
2681out_free_driver:
2682 driver_unregister(&idetape_driver.gen_driver);
2683out_free_class:
2684 class_destroy(idetape_sysfs_class);
2685out:
2686 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002687}
2688
Kay Sievers263756e2005-12-12 18:03:44 +01002689MODULE_ALIAS("ide:*m-tape*");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002690module_init(idetape_init);
2691module_exit(idetape_exit);
2692MODULE_ALIAS_CHARDEV_MAJOR(IDETAPE_MAJOR);
Borislav Petkov9c145762008-02-06 02:57:54 +01002693MODULE_DESCRIPTION("ATAPI Streaming TAPE Driver");
2694MODULE_LICENSE("GPL");