blob: 789f3428f07210295b76cd32bf74c97f08b84011 [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
Borislav Petkovdfe79932008-02-06 02:57:55 +010018#define IDETAPE_VERSION "1.20"
Linus Torvalds1da177e2005-04-16 15:20:36 -070019
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include <linux/module.h>
21#include <linux/types.h>
22#include <linux/string.h>
23#include <linux/kernel.h>
24#include <linux/delay.h>
25#include <linux/timer.h>
26#include <linux/mm.h>
27#include <linux/interrupt.h>
Marcelo Feitoza Parisi9bae1ff2006-03-28 01:56:46 -080028#include <linux/jiffies.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#include <linux/major.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include <linux/errno.h>
31#include <linux/genhd.h>
32#include <linux/slab.h>
33#include <linux/pci.h>
34#include <linux/ide.h>
35#include <linux/smp_lock.h>
36#include <linux/completion.h>
37#include <linux/bitops.h>
Arjan van de Vencf8b8972006-03-23 03:00:45 -080038#include <linux/mutex.h>
Borislav Petkov90699ce2008-02-02 19:56:50 +010039#include <scsi/scsi.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070040
41#include <asm/byteorder.h>
Borislav Petkovc837cfa2008-02-06 02:57:54 +010042#include <linux/irq.h>
43#include <linux/uaccess.h>
44#include <linux/io.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070045#include <asm/unaligned.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070046#include <linux/mtio.h>
47
Borislav Petkov8004a8c2008-02-06 02:57:51 +010048enum {
49 /* output errors only */
50 DBG_ERR = (1 << 0),
51 /* output all sense key/asc */
52 DBG_SENSE = (1 << 1),
53 /* info regarding all chrdev-related procedures */
54 DBG_CHRDEV = (1 << 2),
55 /* all remaining procedures */
56 DBG_PROCS = (1 << 3),
57 /* buffer alloc info (pc_stack & rq_stack) */
58 DBG_PCRQ_STACK = (1 << 4),
59};
60
61/* define to see debug info */
62#define IDETAPE_DEBUG_LOG 0
63
64#if IDETAPE_DEBUG_LOG
65#define debug_log(lvl, fmt, args...) \
66{ \
67 if (tape->debug_mask & lvl) \
68 printk(KERN_INFO "ide-tape: " fmt, ## args); \
69}
70#else
71#define debug_log(lvl, fmt, args...) do {} while (0)
72#endif
73
Linus Torvalds1da177e2005-04-16 15:20:36 -070074/**************************** Tunable parameters *****************************/
Linus Torvalds1da177e2005-04-16 15:20:36 -070075/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +010076 * After each failed packet command we issue a request sense command and retry
77 * the packet command IDETAPE_MAX_PC_RETRIES times.
Linus Torvalds1da177e2005-04-16 15:20:36 -070078 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +010079 * Setting IDETAPE_MAX_PC_RETRIES to 0 will disable retries.
Linus Torvalds1da177e2005-04-16 15:20:36 -070080 */
81#define IDETAPE_MAX_PC_RETRIES 3
82
83/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +010084 * With each packet command, we allocate a buffer of IDETAPE_PC_BUFFER_SIZE
85 * bytes. This is used for several packet commands (Not for READ/WRITE commands)
Linus Torvalds1da177e2005-04-16 15:20:36 -070086 */
87#define IDETAPE_PC_BUFFER_SIZE 256
88
89/*
90 * In various places in the driver, we need to allocate storage
91 * for packet commands and requests, which will remain valid while
92 * we leave the driver to wait for an interrupt or a timeout event.
93 */
94#define IDETAPE_PC_STACK (10 + IDETAPE_MAX_PC_RETRIES)
95
96/*
97 * Some drives (for example, Seagate STT3401A Travan) require a very long
98 * timeout, because they don't return an interrupt or clear their busy bit
99 * until after the command completes (even retension commands).
100 */
101#define IDETAPE_WAIT_CMD (900*HZ)
102
103/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100104 * The following parameter is used to select the point in the internal tape fifo
105 * in which we will start to refill the buffer. Decreasing the following
106 * parameter will improve the system's latency and interactive response, while
107 * using a high value might improve system throughput.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 */
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100109#define IDETAPE_FIFO_THRESHOLD 2
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110
111/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100112 * DSC polling parameters.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100114 * Polling for DSC (a single bit in the status register) is a very important
115 * function in ide-tape. There are two cases in which we poll for DSC:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100117 * 1. Before a read/write packet command, to ensure that we can transfer data
118 * from/to the tape's data buffers, without causing an actual media access.
119 * In case the tape is not ready yet, we take out our request from the device
120 * request queue, so that ide.c could service requests from the other device
121 * on the same interface in the meantime.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100123 * 2. After the successful initialization of a "media access packet command",
124 * which is a command that can take a long time to complete (the interval can
125 * range from several seconds to even an hour). Again, we postpone our request
126 * in the middle to free the bus for the other device. The polling frequency
127 * here should be lower than the read/write frequency since those media access
128 * commands are slow. We start from a "fast" frequency - IDETAPE_DSC_MA_FAST
129 * (1 second), and if we don't receive DSC after IDETAPE_DSC_MA_THRESHOLD
130 * (5 min), we switch it to a lower frequency - IDETAPE_DSC_MA_SLOW (1 min).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100132 * We also set a timeout for the timer, in case something goes wrong. The
133 * timeout should be longer then the maximum execution time of a tape operation.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 */
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100135
136/* DSC timings. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137#define IDETAPE_DSC_RW_MIN 5*HZ/100 /* 50 msec */
138#define IDETAPE_DSC_RW_MAX 40*HZ/100 /* 400 msec */
139#define IDETAPE_DSC_RW_TIMEOUT 2*60*HZ /* 2 minutes */
140#define IDETAPE_DSC_MA_FAST 2*HZ /* 2 seconds */
141#define IDETAPE_DSC_MA_THRESHOLD 5*60*HZ /* 5 minutes */
142#define IDETAPE_DSC_MA_SLOW 30*HZ /* 30 seconds */
143#define IDETAPE_DSC_MA_TIMEOUT 2*60*60*HZ /* 2 hours */
144
145/*************************** End of tunable parameters ***********************/
146
Borislav Petkov54abf372008-02-06 02:57:52 +0100147/* tape directions */
148enum {
149 IDETAPE_DIR_NONE = (1 << 0),
150 IDETAPE_DIR_READ = (1 << 1),
151 IDETAPE_DIR_WRITE = (1 << 2),
152};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153
154struct idetape_bh {
Stephen Rothwellab057962007-08-01 23:46:44 +0200155 u32 b_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 atomic_t b_count;
157 struct idetape_bh *b_reqnext;
158 char *b_data;
159};
160
Borislav Petkov03056b92008-04-18 00:46:26 +0200161/* Tape door status */
162#define DOOR_UNLOCKED 0
163#define DOOR_LOCKED 1
164#define DOOR_EXPLICITLY_LOCKED 2
165
166/* Some defines for the SPACE command */
167#define IDETAPE_SPACE_OVER_FILEMARK 1
168#define IDETAPE_SPACE_TO_EOD 3
169
170/* Some defines for the LOAD UNLOAD command */
171#define IDETAPE_LU_LOAD_MASK 1
172#define IDETAPE_LU_RETENSION_MASK 2
173#define IDETAPE_LU_EOT_MASK 4
174
175/*
176 * Special requests for our block device strategy routine.
177 *
178 * In order to service a character device command, we add special requests to
179 * the tail of our block device request queue and wait for their completion.
180 */
181
182enum {
183 REQ_IDETAPE_PC1 = (1 << 0), /* packet command (first stage) */
184 REQ_IDETAPE_PC2 = (1 << 1), /* packet command (second stage) */
185 REQ_IDETAPE_READ = (1 << 2),
186 REQ_IDETAPE_WRITE = (1 << 3),
187};
188
189/* Error codes returned in rq->errors to the higher part of the driver. */
190#define IDETAPE_ERROR_GENERAL 101
191#define IDETAPE_ERROR_FILEMARK 102
192#define IDETAPE_ERROR_EOD 103
193
194/* Structures related to the SELECT SENSE / MODE SENSE packet commands. */
195#define IDETAPE_BLOCK_DESCRIPTOR 0
196#define IDETAPE_CAPABILITIES_PAGE 0x2a
197
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100199 * Most of our global data which we need to save even as we leave the driver due
200 * to an interrupt or a timer event is stored in the struct defined below.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 */
202typedef struct ide_tape_obj {
203 ide_drive_t *drive;
204 ide_driver_t *driver;
205 struct gendisk *disk;
206 struct kref kref;
207
208 /*
209 * Since a typical character device operation requires more
210 * than one packet command, we provide here enough memory
211 * for the maximum of interconnected packet commands.
212 * The packet commands are stored in the circular array pc_stack.
213 * pc_stack_index points to the last used entry, and warps around
214 * to the start when we get to the last array entry.
215 *
216 * pc points to the current processed packet command.
217 *
218 * failed_pc points to the last failed packet command, or contains
219 * NULL if we do not need to retry any packet command. This is
220 * required since an additional packet command is needed before the
221 * retry, to get detailed information on what went wrong.
222 */
223 /* Current packet command */
Borislav Petkovd236d742008-04-18 00:46:27 +0200224 struct ide_atapi_pc *pc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 /* Last failed packet command */
Borislav Petkovd236d742008-04-18 00:46:27 +0200226 struct ide_atapi_pc *failed_pc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 /* Packet command stack */
Borislav Petkovd236d742008-04-18 00:46:27 +0200228 struct ide_atapi_pc pc_stack[IDETAPE_PC_STACK];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 /* Next free packet command storage space */
230 int pc_stack_index;
231 struct request rq_stack[IDETAPE_PC_STACK];
232 /* We implement a circular array */
233 int rq_stack_index;
234
235 /*
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100236 * DSC polling variables.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100238 * While polling for DSC we use postponed_rq to postpone the current
239 * request so that ide.c will be able to service pending requests on the
240 * other device. Note that at most we will have only one DSC (usually
Borislav Petkov5bd50dc2008-04-27 15:38:28 +0200241 * data transfer) request in the device request queue.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 */
243 struct request *postponed_rq;
244 /* The time in which we started polling for DSC */
245 unsigned long dsc_polling_start;
246 /* Timer used to poll for dsc */
247 struct timer_list dsc_timer;
248 /* Read/Write dsc polling frequency */
Borislav Petkov54bb2072008-02-06 02:57:52 +0100249 unsigned long best_dsc_rw_freq;
250 unsigned long dsc_poll_freq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 unsigned long dsc_timeout;
252
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100253 /* Read position information */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 u8 partition;
255 /* Current block */
Borislav Petkov54bb2072008-02-06 02:57:52 +0100256 unsigned int first_frame;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100258 /* Last error information */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 u8 sense_key, asc, ascq;
260
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100261 /* Character device operation */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 unsigned int minor;
263 /* device name */
264 char name[4];
265 /* Current character device data transfer direction */
Borislav Petkov54abf372008-02-06 02:57:52 +0100266 u8 chrdev_dir;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267
Borislav Petkov54bb2072008-02-06 02:57:52 +0100268 /* tape block size, usually 512 or 1024 bytes */
269 unsigned short blk_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 int user_bs_factor;
Borislav Petkovb6422012008-02-02 19:56:49 +0100271
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 /* Copy of the tape's Capabilities and Mechanical Page */
Borislav Petkovb6422012008-02-02 19:56:49 +0100273 u8 caps[20];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274
275 /*
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100276 * Active data transfer request parameters.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100278 * At most, there is only one ide-tape originated data transfer request
279 * in the device request queue. This allows ide.c to easily service
280 * requests from the other device when we postpone our active request.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 */
Borislav Petkov83042b22008-04-27 15:38:27 +0200282
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100283 /* Data buffer size chosen based on the tape's recommendation */
Borislav Petkovf73850a2008-04-27 15:38:33 +0200284 int buffer_size;
Borislav Petkov077e3bd2008-04-27 15:38:34 +0200285 /* merge buffer */
286 struct idetape_bh *merge_bh;
Borislav Petkov01a63aeb2008-04-27 15:38:34 +0200287 /* size of the merge buffer */
288 int merge_bh_size;
Borislav Petkov077e3bd2008-04-27 15:38:34 +0200289 /* pointer to current buffer head within the merge buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 struct idetape_bh *bh;
291 char *b_data;
292 int b_count;
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100293
Borislav Petkova997a432008-04-27 15:38:33 +0200294 int pages_per_buffer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 /* Wasted space in each stage */
296 int excess_bh_size;
297
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 /* protects the ide-tape queue */
Borislav Petkov54bb2072008-02-06 02:57:52 +0100299 spinlock_t lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100301 /* Measures average tape speed */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 unsigned long avg_time;
303 int avg_size;
304 int avg_speed;
305
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 /* the door is currently locked */
307 int door_locked;
308 /* the tape hardware is write protected */
309 char drv_write_prot;
310 /* the tape is write protected (hardware or opened as read-only) */
311 char write_prot;
312
Borislav Petkov8004a8c2008-02-06 02:57:51 +0100313 u32 debug_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314} idetape_tape_t;
315
Arjan van de Vencf8b8972006-03-23 03:00:45 -0800316static DEFINE_MUTEX(idetape_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317
Will Dysond5dee802005-09-16 02:55:07 -0700318static struct class *idetape_sysfs_class;
319
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320#define to_ide_tape(obj) container_of(obj, struct ide_tape_obj, kref)
321
322#define ide_tape_g(disk) \
323 container_of((disk)->private_data, struct ide_tape_obj, driver)
324
Bartlomiej Zolnierkiewicz08da5912008-07-24 22:53:15 +0200325static void ide_tape_release(struct kref *);
326
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327static struct ide_tape_obj *ide_tape_get(struct gendisk *disk)
328{
329 struct ide_tape_obj *tape = NULL;
330
Arjan van de Vencf8b8972006-03-23 03:00:45 -0800331 mutex_lock(&idetape_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 tape = ide_tape_g(disk);
Bartlomiej Zolnierkiewicz08da5912008-07-24 22:53:15 +0200333 if (tape) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 kref_get(&tape->kref);
Bartlomiej Zolnierkiewicz08da5912008-07-24 22:53:15 +0200335 if (ide_device_get(tape->drive)) {
336 kref_put(&tape->kref, ide_tape_release);
337 tape = NULL;
338 }
339 }
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{
Arjan van de Vencf8b8972006-03-23 03:00:45 -0800346 mutex_lock(&idetape_ref_mutex);
Bartlomiej Zolnierkiewicz08da5912008-07-24 22:53:15 +0200347 ide_device_put(tape->drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 kref_put(&tape->kref, ide_tape_release);
Arjan van de Vencf8b8972006-03-23 03:00:45 -0800349 mutex_unlock(&idetape_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350}
351
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100353 * The variables below are used for the character device interface. Additional
354 * state variables are defined in our ide_drive_t structure.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100356static struct ide_tape_obj *idetape_devs[MAX_HWIFS * MAX_DRIVES];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357
358#define ide_tape_f(file) ((file)->private_data)
359
360static struct ide_tape_obj *ide_tape_chrdev_get(unsigned int i)
361{
362 struct ide_tape_obj *tape = NULL;
363
Arjan van de Vencf8b8972006-03-23 03:00:45 -0800364 mutex_lock(&idetape_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 tape = idetape_devs[i];
366 if (tape)
367 kref_get(&tape->kref);
Arjan van de Vencf8b8972006-03-23 03:00:45 -0800368 mutex_unlock(&idetape_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 return tape;
370}
371
Borislav Petkovd236d742008-04-18 00:46:27 +0200372static void idetape_input_buffers(ide_drive_t *drive, struct ide_atapi_pc *pc,
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100373 unsigned int bcount)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374{
375 struct idetape_bh *bh = pc->bh;
376 int count;
377
378 while (bcount) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 if (bh == NULL) {
380 printk(KERN_ERR "ide-tape: bh == NULL in "
381 "idetape_input_buffers\n");
Bartlomiej Zolnierkiewicz9f87abe2008-04-28 23:44:41 +0200382 ide_pad_transfer(drive, 0, bcount);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 return;
384 }
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100385 count = min(
386 (unsigned int)(bh->b_size - atomic_read(&bh->b_count)),
387 bcount);
Bartlomiej Zolnierkiewicz374e0422008-07-23 19:55:56 +0200388 drive->hwif->tp_ops->input_data(drive, NULL, bh->b_data +
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100389 atomic_read(&bh->b_count), count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 bcount -= count;
391 atomic_add(count, &bh->b_count);
392 if (atomic_read(&bh->b_count) == bh->b_size) {
393 bh = bh->b_reqnext;
394 if (bh)
395 atomic_set(&bh->b_count, 0);
396 }
397 }
398 pc->bh = bh;
399}
400
Borislav Petkovd236d742008-04-18 00:46:27 +0200401static void idetape_output_buffers(ide_drive_t *drive, struct ide_atapi_pc *pc,
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100402 unsigned int bcount)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403{
404 struct idetape_bh *bh = pc->bh;
405 int count;
406
407 while (bcount) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 if (bh == NULL) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100409 printk(KERN_ERR "ide-tape: bh == NULL in %s\n",
410 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 return;
412 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 count = min((unsigned int)pc->b_count, (unsigned int)bcount);
Bartlomiej Zolnierkiewicz374e0422008-07-23 19:55:56 +0200414 drive->hwif->tp_ops->output_data(drive, NULL, pc->b_data, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 bcount -= count;
416 pc->b_data += count;
417 pc->b_count -= count;
418 if (!pc->b_count) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100419 bh = bh->b_reqnext;
420 pc->bh = bh;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 if (bh) {
422 pc->b_data = bh->b_data;
423 pc->b_count = atomic_read(&bh->b_count);
424 }
425 }
426 }
427}
428
Bartlomiej Zolnierkiewicz646c0cb2008-07-15 21:22:03 +0200429static void idetape_update_buffers(ide_drive_t *drive, struct ide_atapi_pc *pc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430{
431 struct idetape_bh *bh = pc->bh;
432 int count;
Borislav Petkovd236d742008-04-18 00:46:27 +0200433 unsigned int bcount = pc->xferred;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434
Borislav Petkov346331f2008-04-18 00:46:26 +0200435 if (pc->flags & PC_FLAG_WRITING)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 return;
437 while (bcount) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 if (bh == NULL) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100439 printk(KERN_ERR "ide-tape: bh == NULL in %s\n",
440 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 return;
442 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 count = min((unsigned int)bh->b_size, (unsigned int)bcount);
444 atomic_set(&bh->b_count, count);
445 if (atomic_read(&bh->b_count) == bh->b_size)
446 bh = bh->b_reqnext;
447 bcount -= count;
448 }
449 pc->bh = bh;
450}
451
452/*
453 * idetape_next_pc_storage returns a pointer to a place in which we can
454 * safely store a packet command, even though we intend to leave the
455 * driver. A storage space for a maximum of IDETAPE_PC_STACK packet
456 * commands is allocated at initialization time.
457 */
Borislav Petkovd236d742008-04-18 00:46:27 +0200458static struct ide_atapi_pc *idetape_next_pc_storage(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459{
460 idetape_tape_t *tape = drive->driver_data;
461
Borislav Petkov8004a8c2008-02-06 02:57:51 +0100462 debug_log(DBG_PCRQ_STACK, "pc_stack_index=%d\n", tape->pc_stack_index);
463
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 if (tape->pc_stack_index == IDETAPE_PC_STACK)
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100465 tape->pc_stack_index = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 return (&tape->pc_stack[tape->pc_stack_index++]);
467}
468
469/*
470 * idetape_next_rq_storage is used along with idetape_next_pc_storage.
471 * Since we queue packet commands in the request queue, we need to
472 * allocate a request, along with the allocation of a packet command.
473 */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100474
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475/**************************************************************
476 * *
477 * This should get fixed to use kmalloc(.., GFP_ATOMIC) *
478 * followed later on by kfree(). -ml *
479 * *
480 **************************************************************/
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100481
482static struct request *idetape_next_rq_storage(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483{
484 idetape_tape_t *tape = drive->driver_data;
485
Borislav Petkov8004a8c2008-02-06 02:57:51 +0100486 debug_log(DBG_PCRQ_STACK, "rq_stack_index=%d\n", tape->rq_stack_index);
487
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 if (tape->rq_stack_index == IDETAPE_PC_STACK)
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100489 tape->rq_stack_index = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 return (&tape->rq_stack[tape->rq_stack_index++]);
491}
492
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493/*
Borislav Petkov1b5db432008-02-02 19:56:48 +0100494 * called on each failed packet command retry to analyze the request sense. We
495 * currently do not utilize this information.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 */
Borislav Petkov1b5db432008-02-02 19:56:48 +0100497static void idetape_analyze_error(ide_drive_t *drive, u8 *sense)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498{
499 idetape_tape_t *tape = drive->driver_data;
Borislav Petkovd236d742008-04-18 00:46:27 +0200500 struct ide_atapi_pc *pc = tape->failed_pc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501
Borislav Petkov1b5db432008-02-02 19:56:48 +0100502 tape->sense_key = sense[2] & 0xF;
503 tape->asc = sense[12];
504 tape->ascq = sense[13];
Borislav Petkov8004a8c2008-02-06 02:57:51 +0100505
506 debug_log(DBG_ERR, "pc = %x, sense key = %x, asc = %x, ascq = %x\n",
507 pc->c[0], tape->sense_key, tape->asc, tape->ascq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508
Borislav Petkovd236d742008-04-18 00:46:27 +0200509 /* Correct pc->xferred by asking the tape. */
Borislav Petkov346331f2008-04-18 00:46:26 +0200510 if (pc->flags & PC_FLAG_DMA_ERROR) {
Borislav Petkovd236d742008-04-18 00:46:27 +0200511 pc->xferred = pc->req_xfer -
Borislav Petkov54bb2072008-02-06 02:57:52 +0100512 tape->blk_size *
Harvey Harrison5d0cc8a2008-07-15 21:21:41 +0200513 get_unaligned_be32(&sense[3]);
Bartlomiej Zolnierkiewicz646c0cb2008-07-15 21:22:03 +0200514 idetape_update_buffers(drive, pc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 }
516
517 /*
518 * If error was the result of a zero-length read or write command,
519 * with sense key=5, asc=0x22, ascq=0, let it slide. Some drives
520 * (i.e. Seagate STT3401A Travan) don't support 0-length read/writes.
521 */
Borislav Petkov90699ce2008-02-02 19:56:50 +0100522 if ((pc->c[0] == READ_6 || pc->c[0] == WRITE_6)
Borislav Petkov1b5db432008-02-02 19:56:48 +0100523 /* length == 0 */
524 && pc->c[4] == 0 && pc->c[3] == 0 && pc->c[2] == 0) {
525 if (tape->sense_key == 5) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 /* don't report an error, everything's ok */
527 pc->error = 0;
528 /* don't retry read/write */
Borislav Petkov346331f2008-04-18 00:46:26 +0200529 pc->flags |= PC_FLAG_ABORT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 }
531 }
Borislav Petkov90699ce2008-02-02 19:56:50 +0100532 if (pc->c[0] == READ_6 && (sense[2] & 0x80)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 pc->error = IDETAPE_ERROR_FILEMARK;
Borislav Petkov346331f2008-04-18 00:46:26 +0200534 pc->flags |= PC_FLAG_ABORT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 }
Borislav Petkov90699ce2008-02-02 19:56:50 +0100536 if (pc->c[0] == WRITE_6) {
Borislav Petkov1b5db432008-02-02 19:56:48 +0100537 if ((sense[2] & 0x40) || (tape->sense_key == 0xd
538 && tape->asc == 0x0 && tape->ascq == 0x2)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 pc->error = IDETAPE_ERROR_EOD;
Borislav Petkov346331f2008-04-18 00:46:26 +0200540 pc->flags |= PC_FLAG_ABORT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 }
542 }
Borislav Petkov90699ce2008-02-02 19:56:50 +0100543 if (pc->c[0] == READ_6 || pc->c[0] == WRITE_6) {
Borislav Petkov1b5db432008-02-02 19:56:48 +0100544 if (tape->sense_key == 8) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545 pc->error = IDETAPE_ERROR_EOD;
Borislav Petkov346331f2008-04-18 00:46:26 +0200546 pc->flags |= PC_FLAG_ABORT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 }
Borislav Petkov346331f2008-04-18 00:46:26 +0200548 if (!(pc->flags & PC_FLAG_ABORT) &&
Borislav Petkovd236d742008-04-18 00:46:27 +0200549 pc->xferred)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 pc->retries = IDETAPE_MAX_PC_RETRIES + 1;
551 }
552}
553
Borislav Petkovd01dbc32008-04-27 15:38:33 +0200554/* Free data buffers completely. */
Borislav Petkov077e3bd2008-04-27 15:38:34 +0200555static void ide_tape_kfree_buffer(idetape_tape_t *tape)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556{
Borislav Petkov077e3bd2008-04-27 15:38:34 +0200557 struct idetape_bh *prev_bh, *bh = tape->merge_bh;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558
Borislav Petkovd01dbc32008-04-27 15:38:33 +0200559 while (bh) {
560 u32 size = bh->b_size;
561
562 while (size) {
563 unsigned int order = fls(size >> PAGE_SHIFT)-1;
564
565 if (bh->b_data)
566 free_pages((unsigned long)bh->b_data, order);
567
568 size &= (order-1);
569 bh->b_data += (1 << order) * PAGE_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 }
571 prev_bh = bh;
572 bh = bh->b_reqnext;
573 kfree(prev_bh);
574 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575}
576
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577static int idetape_end_request(ide_drive_t *drive, int uptodate, int nr_sects)
578{
579 struct request *rq = HWGROUP(drive)->rq;
580 idetape_tape_t *tape = drive->driver_data;
581 unsigned long flags;
582 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583
Borislav Petkov8004a8c2008-02-06 02:57:51 +0100584 debug_log(DBG_PROCS, "Enter %s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585
586 switch (uptodate) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100587 case 0: error = IDETAPE_ERROR_GENERAL; break;
588 case 1: error = 0; break;
589 default: error = uptodate;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 }
591 rq->errors = error;
592 if (error)
593 tape->failed_pc = NULL;
594
Bartlomiej Zolnierkiewicz36872212008-01-26 20:13:10 +0100595 if (!blk_special_request(rq)) {
596 ide_end_request(drive, uptodate, nr_sects);
597 return 0;
598 }
599
Borislav Petkov54bb2072008-02-06 02:57:52 +0100600 spin_lock_irqsave(&tape->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 ide_end_drive_cmd(drive, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603
Borislav Petkov54bb2072008-02-06 02:57:52 +0100604 spin_unlock_irqrestore(&tape->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 return 0;
606}
607
Bartlomiej Zolnierkiewicz92f5daf2008-07-15 21:21:55 +0200608static void ide_tape_callback(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609{
610 idetape_tape_t *tape = drive->driver_data;
Bartlomiej Zolnierkiewicz5985e6a2008-07-15 21:21:55 +0200611 struct ide_atapi_pc *pc = tape->pc;
612 int uptodate = pc->error ? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613
Borislav Petkov8004a8c2008-02-06 02:57:51 +0100614 debug_log(DBG_PROCS, "Enter %s\n", __func__);
615
Bartlomiej Zolnierkiewiczdd2e9a02008-07-15 21:22:01 +0200616 if (tape->failed_pc == pc)
617 tape->failed_pc = NULL;
618
Bartlomiej Zolnierkiewicz5985e6a2008-07-15 21:21:55 +0200619 if (pc->c[0] == REQUEST_SENSE) {
620 if (uptodate)
621 idetape_analyze_error(drive, pc->buf);
622 else
623 printk(KERN_ERR "ide-tape: Error in REQUEST SENSE "
624 "itself - Aborting request!\n");
625 } else if (pc->c[0] == READ_6 || pc->c[0] == WRITE_6) {
626 struct request *rq = drive->hwif->hwgroup->rq;
627 int blocks = pc->xferred / tape->blk_size;
628
629 tape->avg_size += blocks * tape->blk_size;
630
631 if (time_after_eq(jiffies, tape->avg_time + HZ)) {
632 tape->avg_speed = tape->avg_size * HZ /
633 (jiffies - tape->avg_time) / 1024;
634 tape->avg_size = 0;
635 tape->avg_time = jiffies;
636 }
637
638 tape->first_frame += blocks;
639 rq->current_nr_sectors -= blocks;
640
641 if (pc->error)
642 uptodate = pc->error;
643 } else if (pc->c[0] == READ_POSITION && uptodate) {
644 u8 *readpos = tape->pc->buf;
645
646 debug_log(DBG_SENSE, "BOP - %s\n",
647 (readpos[0] & 0x80) ? "Yes" : "No");
648 debug_log(DBG_SENSE, "EOP - %s\n",
649 (readpos[0] & 0x40) ? "Yes" : "No");
650
651 if (readpos[0] & 0x4) {
652 printk(KERN_INFO "ide-tape: Block location is unknown"
653 "to the tape\n");
Borislav Petkovf2e3ab52008-07-23 19:56:01 +0200654 clear_bit(IDE_AFLAG_ADDRESS_VALID, &drive->atapi_flags);
Bartlomiej Zolnierkiewicz5985e6a2008-07-15 21:21:55 +0200655 uptodate = 0;
656 } else {
657 debug_log(DBG_SENSE, "Block Location - %u\n",
658 be32_to_cpu(*(u32 *)&readpos[4]));
659
660 tape->partition = readpos[1];
661 tape->first_frame = be32_to_cpu(*(u32 *)&readpos[4]);
Borislav Petkovf2e3ab52008-07-23 19:56:01 +0200662 set_bit(IDE_AFLAG_ADDRESS_VALID, &drive->atapi_flags);
Bartlomiej Zolnierkiewicz5985e6a2008-07-15 21:21:55 +0200663 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664 }
Bartlomiej Zolnierkiewicz5985e6a2008-07-15 21:21:55 +0200665
666 idetape_end_request(drive, uptodate, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667}
668
Bartlomiej Zolnierkiewicz5985e6a2008-07-15 21:21:55 +0200669static void idetape_init_pc(struct ide_atapi_pc *pc)
670{
671 memset(pc->c, 0, 12);
672 pc->retries = 0;
673 pc->flags = 0;
674 pc->req_xfer = 0;
675 pc->buf = pc->pc_buf;
676 pc->buf_size = IDETAPE_PC_BUFFER_SIZE;
677 pc->bh = NULL;
678 pc->b_data = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679}
680
Borislav Petkovd236d742008-04-18 00:46:27 +0200681static void idetape_create_request_sense_cmd(struct ide_atapi_pc *pc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682{
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100683 idetape_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +0100684 pc->c[0] = REQUEST_SENSE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 pc->c[4] = 20;
Borislav Petkovd236d742008-04-18 00:46:27 +0200686 pc->req_xfer = 20;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687}
688
689static void idetape_init_rq(struct request *rq, u8 cmd)
690{
FUJITA Tomonorie7b241a2008-04-29 09:54:38 +0200691 blk_rq_init(NULL, rq);
Jens Axboe4aff5e22006-08-10 08:44:47 +0200692 rq->cmd_type = REQ_TYPE_SPECIAL;
Borislav Petkov83dd5732008-07-23 19:56:00 +0200693 rq->cmd[13] = cmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694}
695
696/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100697 * Generate a new packet command request in front of the request queue, before
698 * the current request, so that it will be processed immediately, on the next
699 * pass through the driver. The function below is called from the request
700 * handling part of the driver (the "bottom" part). Safe storage for the request
701 * should be allocated with ide_tape_next_{pc,rq}_storage() prior to that.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100703 * Memory for those requests is pre-allocated at initialization time, and is
704 * limited to IDETAPE_PC_STACK requests. We assume that we have enough space for
705 * the maximum possible number of inter-dependent packet commands.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100707 * The higher level of the driver - The ioctl handler and the character device
708 * handling functions should queue request to the lower level part and wait for
709 * their completion using idetape_queue_pc_tail or idetape_queue_rw_tail.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710 */
Borislav Petkovd236d742008-04-18 00:46:27 +0200711static void idetape_queue_pc_head(ide_drive_t *drive, struct ide_atapi_pc *pc,
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100712 struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713{
714 struct ide_tape_obj *tape = drive->driver_data;
715
716 idetape_init_rq(rq, REQ_IDETAPE_PC1);
Bartlomiej Zolnierkiewicze8a96aa2008-07-15 21:21:41 +0200717 rq->cmd_flags |= REQ_PREEMPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718 rq->buffer = (char *) pc;
719 rq->rq_disk = tape->disk;
Borislav Petkov0014c752008-07-23 19:56:00 +0200720 memcpy(rq->cmd, pc->c, 12);
FUJITA Tomonori63f5abb2008-07-15 21:21:51 +0200721 ide_do_drive_cmd(drive, rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722}
723
724/*
725 * idetape_retry_pc is called when an error was detected during the
726 * last packet command. We queue a request sense packet command in
727 * the head of the request list.
728 */
Bartlomiej Zolnierkiewicz258ec412008-07-15 21:21:55 +0200729static void idetape_retry_pc(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730{
Borislav Petkovd236d742008-04-18 00:46:27 +0200731 struct ide_atapi_pc *pc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732 struct request *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733
Bartlomiej Zolnierkiewicz64a57fe2008-02-06 02:57:51 +0100734 (void)ide_read_error(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735 pc = idetape_next_pc_storage(drive);
736 rq = idetape_next_rq_storage(drive);
737 idetape_create_request_sense_cmd(pc);
Borislav Petkovf2e3ab52008-07-23 19:56:01 +0200738 set_bit(IDE_AFLAG_IGNORE_DSC, &drive->atapi_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 idetape_queue_pc_head(drive, pc, rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740}
741
742/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100743 * Postpone the current request so that ide.c will be able to service requests
744 * from another device on the same hwgroup while we are polling for DSC.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745 */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100746static void idetape_postpone_request(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747{
748 idetape_tape_t *tape = drive->driver_data;
749
Borislav Petkov8004a8c2008-02-06 02:57:51 +0100750 debug_log(DBG_PROCS, "Enter %s\n", __func__);
751
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752 tape->postponed_rq = HWGROUP(drive)->rq;
Borislav Petkov54bb2072008-02-06 02:57:52 +0100753 ide_stall_queue(drive, tape->dsc_poll_freq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754}
755
Bartlomiej Zolnierkiewicz74e63e742008-07-15 21:22:01 +0200756static void ide_tape_handle_dsc(ide_drive_t *drive)
757{
758 idetape_tape_t *tape = drive->driver_data;
759
760 /* Media access command */
761 tape->dsc_polling_start = jiffies;
762 tape->dsc_poll_freq = IDETAPE_DSC_MA_FAST;
763 tape->dsc_timeout = jiffies + IDETAPE_DSC_MA_TIMEOUT;
764 /* Allow ide.c to handle other requests */
765 idetape_postpone_request(drive);
766}
767
Bartlomiej Zolnierkiewicz08424ac2008-07-15 21:22:01 +0200768static void ide_tape_io_buffers(ide_drive_t *drive, struct ide_atapi_pc *pc,
769 unsigned int bcount, int write)
770{
771 if (write)
772 idetape_output_buffers(drive, pc, bcount);
773 else
774 idetape_input_buffers(drive, pc, bcount);
775}
Borislav Petkova1efc852008-02-06 02:57:52 +0100776
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777/*
Borislav Petkova1efc852008-02-06 02:57:52 +0100778 * This is the usual interrupt handler which will be called during a packet
779 * command. We will transfer some of the data (as requested by the drive) and
780 * will re-point interrupt handler to us. When data transfer is finished, we
781 * will act according to the algorithm described before
Borislav Petkov8d06bfa2008-02-06 02:57:53 +0100782 * idetape_issue_pc.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 */
Borislav Petkova1efc852008-02-06 02:57:52 +0100784static ide_startstop_t idetape_pc_intr(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786 idetape_tape_t *tape = drive->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787
Bartlomiej Zolnierkiewicz646c0cb2008-07-15 21:22:03 +0200788 return ide_pc_intr(drive, tape->pc, idetape_pc_intr, IDETAPE_WAIT_CMD,
789 NULL, idetape_update_buffers, idetape_retry_pc,
790 ide_tape_handle_dsc, ide_tape_io_buffers);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791}
792
793/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100794 * Packet Command Interface
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100796 * The current Packet Command is available in tape->pc, and will not change
797 * until we finish handling it. Each packet command is associated with a
798 * callback function that will be called when the command is finished.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100800 * The handling will be done in three stages:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100802 * 1. idetape_issue_pc will send the packet command to the drive, and will set
803 * the interrupt handler to idetape_pc_intr.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100805 * 2. On each interrupt, idetape_pc_intr will be called. This step will be
806 * repeated until the device signals us that no more interrupts will be issued.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100808 * 3. ATAPI Tape media access commands have immediate status with a delayed
809 * process. In case of a successful initiation of a media access packet command,
810 * the DSC bit will be set when the actual execution of the command is finished.
811 * Since the tape drive will not issue an interrupt, we have to poll for this
812 * event. In this case, we define the request as "low priority request" by
813 * setting rq_status to IDETAPE_RQ_POSTPONED, set a timer to poll for DSC and
814 * exit the driver.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100816 * ide.c will then give higher priority to requests which originate from the
817 * other device, until will change rq_status to RQ_ACTIVE.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100819 * 4. When the packet command is finished, it will be checked for errors.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100821 * 5. In case an error was found, we queue a request sense packet command in
822 * front of the request queue and retry the operation up to
823 * IDETAPE_MAX_PC_RETRIES times.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100825 * 6. In case no error was found, or we decided to give up and not to retry
826 * again, the callback function will be called and then we will handle the next
827 * request.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828 */
829static ide_startstop_t idetape_transfer_pc(ide_drive_t *drive)
830{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831 idetape_tape_t *tape = drive->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832
Bartlomiej Zolnierkiewicz594c16d2008-07-15 21:21:58 +0200833 return ide_transfer_pc(drive, tape->pc, idetape_pc_intr,
834 IDETAPE_WAIT_CMD, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835}
836
Borislav Petkovd236d742008-04-18 00:46:27 +0200837static ide_startstop_t idetape_issue_pc(ide_drive_t *drive,
838 struct ide_atapi_pc *pc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840 idetape_tape_t *tape = drive->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841
Borislav Petkov90699ce2008-02-02 19:56:50 +0100842 if (tape->pc->c[0] == REQUEST_SENSE &&
843 pc->c[0] == REQUEST_SENSE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844 printk(KERN_ERR "ide-tape: possible ide-tape.c bug - "
845 "Two request sense in serial were issued\n");
846 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847
Borislav Petkov90699ce2008-02-02 19:56:50 +0100848 if (tape->failed_pc == NULL && pc->c[0] != REQUEST_SENSE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849 tape->failed_pc = pc;
850 /* Set the current packet command */
851 tape->pc = pc;
852
853 if (pc->retries > IDETAPE_MAX_PC_RETRIES ||
Borislav Petkov346331f2008-04-18 00:46:26 +0200854 (pc->flags & PC_FLAG_ABORT)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855 /*
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100856 * We will "abort" retrying a packet command in case legitimate
857 * error code was received (crossing a filemark, or end of the
858 * media, for example).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859 */
Borislav Petkov346331f2008-04-18 00:46:26 +0200860 if (!(pc->flags & PC_FLAG_ABORT)) {
Borislav Petkov90699ce2008-02-02 19:56:50 +0100861 if (!(pc->c[0] == TEST_UNIT_READY &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862 tape->sense_key == 2 && tape->asc == 4 &&
863 (tape->ascq == 1 || tape->ascq == 8))) {
864 printk(KERN_ERR "ide-tape: %s: I/O error, "
865 "pc = %2x, key = %2x, "
866 "asc = %2x, ascq = %2x\n",
867 tape->name, pc->c[0],
868 tape->sense_key, tape->asc,
869 tape->ascq);
870 }
871 /* Giving up */
872 pc->error = IDETAPE_ERROR_GENERAL;
873 }
874 tape->failed_pc = NULL;
Borislav Petkov776bb022008-07-23 19:55:59 +0200875 drive->pc_callback(drive);
Bartlomiej Zolnierkiewicz92f5daf2008-07-15 21:21:55 +0200876 return ide_stopped;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877 }
Borislav Petkov8004a8c2008-02-06 02:57:51 +0100878 debug_log(DBG_SENSE, "Retry #%d, cmd = %02X\n", pc->retries, pc->c[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879
880 pc->retries++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881
Bartlomiej Zolnierkiewicz6bf16412008-07-15 21:22:00 +0200882 return ide_issue_pc(drive, pc, idetape_transfer_pc,
883 IDETAPE_WAIT_CMD, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884}
885
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100886/* A mode sense command is used to "sense" tape parameters. */
Borislav Petkovd236d742008-04-18 00:46:27 +0200887static void idetape_create_mode_sense_cmd(struct ide_atapi_pc *pc, u8 page_code)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888{
889 idetape_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +0100890 pc->c[0] = MODE_SENSE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891 if (page_code != IDETAPE_BLOCK_DESCRIPTOR)
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100892 /* DBD = 1 - Don't return block descriptors */
893 pc->c[1] = 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894 pc->c[2] = page_code;
895 /*
896 * Changed pc->c[3] to 0 (255 will at best return unused info).
897 *
898 * For SCSI this byte is defined as subpage instead of high byte
899 * of length and some IDE drives seem to interpret it this way
900 * and return an error when 255 is used.
901 */
902 pc->c[3] = 0;
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100903 /* We will just discard data in that case */
904 pc->c[4] = 255;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905 if (page_code == IDETAPE_BLOCK_DESCRIPTOR)
Borislav Petkovd236d742008-04-18 00:46:27 +0200906 pc->req_xfer = 12;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907 else if (page_code == IDETAPE_CAPABILITIES_PAGE)
Borislav Petkovd236d742008-04-18 00:46:27 +0200908 pc->req_xfer = 24;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909 else
Borislav Petkovd236d742008-04-18 00:46:27 +0200910 pc->req_xfer = 50;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911}
912
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100913static ide_startstop_t idetape_media_access_finished(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914{
Bartlomiej Zolnierkiewiczb73c7ee2008-07-23 19:55:52 +0200915 ide_hwif_t *hwif = drive->hwif;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916 idetape_tape_t *tape = drive->driver_data;
Borislav Petkovd236d742008-04-18 00:46:27 +0200917 struct ide_atapi_pc *pc = tape->pc;
Bartlomiej Zolnierkiewicz22c525b2008-01-25 22:17:11 +0100918 u8 stat;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919
Bartlomiej Zolnierkiewicz374e0422008-07-23 19:55:56 +0200920 stat = hwif->tp_ops->read_status(hwif);
Bartlomiej Zolnierkiewiczc47137a2008-02-06 02:57:51 +0100921
Bartlomiej Zolnierkiewicz22c525b2008-01-25 22:17:11 +0100922 if (stat & SEEK_STAT) {
923 if (stat & ERR_STAT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924 /* Error detected */
Borislav Petkov90699ce2008-02-02 19:56:50 +0100925 if (pc->c[0] != TEST_UNIT_READY)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926 printk(KERN_ERR "ide-tape: %s: I/O error, ",
927 tape->name);
928 /* Retry operation */
Bartlomiej Zolnierkiewicz258ec412008-07-15 21:21:55 +0200929 idetape_retry_pc(drive);
930 return ide_stopped;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931 }
932 pc->error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933 } else {
934 pc->error = IDETAPE_ERROR_GENERAL;
935 tape->failed_pc = NULL;
936 }
Borislav Petkov776bb022008-07-23 19:55:59 +0200937 drive->pc_callback(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938 return ide_stopped;
939}
940
Borislav Petkovcd2abbf2008-07-15 21:22:03 +0200941static void ide_tape_create_rw_cmd(idetape_tape_t *tape,
Borislav Petkov0014c752008-07-23 19:56:00 +0200942 struct ide_atapi_pc *pc, struct request *rq,
943 u8 opcode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944{
Borislav Petkov0014c752008-07-23 19:56:00 +0200945 struct idetape_bh *bh = (struct idetape_bh *)rq->special;
946 unsigned int length = rq->current_nr_sectors;
947
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948 idetape_init_pc(pc);
Borislav Petkov860ff5e2008-02-02 19:56:50 +0100949 put_unaligned(cpu_to_be32(length), (unsigned int *) &pc->c[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950 pc->c[1] = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951 pc->bh = bh;
Borislav Petkovd236d742008-04-18 00:46:27 +0200952 pc->buf = NULL;
953 pc->buf_size = length * tape->blk_size;
954 pc->req_xfer = pc->buf_size;
Borislav Petkovf73850a2008-04-27 15:38:33 +0200955 if (pc->req_xfer == tape->buffer_size)
Bartlomiej Zolnierkiewicz5e331092008-07-15 21:21:56 +0200956 pc->flags |= PC_FLAG_DMA_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957
Borislav Petkovcd2abbf2008-07-15 21:22:03 +0200958 if (opcode == READ_6) {
959 pc->c[0] = READ_6;
960 atomic_set(&bh->b_count, 0);
961 } else if (opcode == WRITE_6) {
962 pc->c[0] = WRITE_6;
963 pc->flags |= PC_FLAG_WRITING;
964 pc->b_data = bh->b_data;
965 pc->b_count = atomic_read(&bh->b_count);
966 }
Borislav Petkov0014c752008-07-23 19:56:00 +0200967
968 memcpy(rq->cmd, pc->c, 12);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969}
970
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971static ide_startstop_t idetape_do_request(ide_drive_t *drive,
972 struct request *rq, sector_t block)
973{
Bartlomiej Zolnierkiewiczb73c7ee2008-07-23 19:55:52 +0200974 ide_hwif_t *hwif = drive->hwif;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975 idetape_tape_t *tape = drive->driver_data;
Borislav Petkovd236d742008-04-18 00:46:27 +0200976 struct ide_atapi_pc *pc = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977 struct request *postponed_rq = tape->postponed_rq;
Bartlomiej Zolnierkiewicz22c525b2008-01-25 22:17:11 +0100978 u8 stat;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979
Borislav Petkov8004a8c2008-02-06 02:57:51 +0100980 debug_log(DBG_SENSE, "sector: %ld, nr_sectors: %ld,"
981 " current_nr_sectors: %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982 rq->sector, rq->nr_sectors, rq->current_nr_sectors);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983
Jens Axboe4aff5e22006-08-10 08:44:47 +0200984 if (!blk_special_request(rq)) {
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100985 /* We do not support buffer cache originated requests. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986 printk(KERN_NOTICE "ide-tape: %s: Unsupported request in "
Jens Axboe4aff5e22006-08-10 08:44:47 +0200987 "request queue (%d)\n", drive->name, rq->cmd_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988 ide_end_request(drive, 0, 0);
989 return ide_stopped;
990 }
991
Borislav Petkov3c98bf32008-02-06 02:57:53 +0100992 /* Retry a failed packet command */
Bartlomiej Zolnierkiewicz28c72142008-07-15 21:21:59 +0200993 if (tape->failed_pc && tape->pc->c[0] == REQUEST_SENSE) {
994 pc = tape->failed_pc;
995 goto out;
996 }
Borislav Petkov5a04cfa2008-02-06 02:57:54 +0100997
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998 if (postponed_rq != NULL)
999 if (rq != postponed_rq) {
1000 printk(KERN_ERR "ide-tape: ide-tape.c bug - "
1001 "Two DSC requests were queued\n");
1002 idetape_end_request(drive, 0, 0);
1003 return ide_stopped;
1004 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005
1006 tape->postponed_rq = NULL;
1007
1008 /*
1009 * If the tape is still busy, postpone our request and service
1010 * the other device meanwhile.
1011 */
Bartlomiej Zolnierkiewicz374e0422008-07-23 19:55:56 +02001012 stat = hwif->tp_ops->read_status(hwif);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013
Borislav Petkov83dd5732008-07-23 19:56:00 +02001014 if (!drive->dsc_overlap && !(rq->cmd[13] & REQ_IDETAPE_PC2))
Borislav Petkovf2e3ab52008-07-23 19:56:01 +02001015 set_bit(IDE_AFLAG_IGNORE_DSC, &drive->atapi_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016
1017 if (drive->post_reset == 1) {
Borislav Petkovf2e3ab52008-07-23 19:56:01 +02001018 set_bit(IDE_AFLAG_IGNORE_DSC, &drive->atapi_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019 drive->post_reset = 0;
1020 }
1021
Borislav Petkovf2e3ab52008-07-23 19:56:01 +02001022 if (!test_and_clear_bit(IDE_AFLAG_IGNORE_DSC, &drive->atapi_flags) &&
Bartlomiej Zolnierkiewicz22c525b2008-01-25 22:17:11 +01001023 (stat & SEEK_STAT) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024 if (postponed_rq == NULL) {
1025 tape->dsc_polling_start = jiffies;
Borislav Petkov54bb2072008-02-06 02:57:52 +01001026 tape->dsc_poll_freq = tape->best_dsc_rw_freq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027 tape->dsc_timeout = jiffies + IDETAPE_DSC_RW_TIMEOUT;
1028 } else if (time_after(jiffies, tape->dsc_timeout)) {
1029 printk(KERN_ERR "ide-tape: %s: DSC timeout\n",
1030 tape->name);
Borislav Petkov83dd5732008-07-23 19:56:00 +02001031 if (rq->cmd[13] & REQ_IDETAPE_PC2) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032 idetape_media_access_finished(drive);
1033 return ide_stopped;
1034 } else {
1035 return ide_do_reset(drive);
1036 }
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001037 } else if (time_after(jiffies,
1038 tape->dsc_polling_start +
1039 IDETAPE_DSC_MA_THRESHOLD))
Borislav Petkov54bb2072008-02-06 02:57:52 +01001040 tape->dsc_poll_freq = IDETAPE_DSC_MA_SLOW;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041 idetape_postpone_request(drive);
1042 return ide_stopped;
1043 }
Borislav Petkov83dd5732008-07-23 19:56:00 +02001044 if (rq->cmd[13] & REQ_IDETAPE_READ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045 pc = idetape_next_pc_storage(drive);
Borislav Petkov0014c752008-07-23 19:56:00 +02001046 ide_tape_create_rw_cmd(tape, pc, rq, READ_6);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047 goto out;
1048 }
Borislav Petkov83dd5732008-07-23 19:56:00 +02001049 if (rq->cmd[13] & REQ_IDETAPE_WRITE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050 pc = idetape_next_pc_storage(drive);
Borislav Petkov0014c752008-07-23 19:56:00 +02001051 ide_tape_create_rw_cmd(tape, pc, rq, WRITE_6);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052 goto out;
1053 }
Borislav Petkov83dd5732008-07-23 19:56:00 +02001054 if (rq->cmd[13] & REQ_IDETAPE_PC1) {
Borislav Petkovd236d742008-04-18 00:46:27 +02001055 pc = (struct ide_atapi_pc *) rq->buffer;
Borislav Petkov83dd5732008-07-23 19:56:00 +02001056 rq->cmd[13] &= ~(REQ_IDETAPE_PC1);
1057 rq->cmd[13] |= REQ_IDETAPE_PC2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058 goto out;
1059 }
Borislav Petkov83dd5732008-07-23 19:56:00 +02001060 if (rq->cmd[13] & REQ_IDETAPE_PC2) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061 idetape_media_access_finished(drive);
1062 return ide_stopped;
1063 }
1064 BUG();
Bartlomiej Zolnierkiewicz28c72142008-07-15 21:21:59 +02001065
Borislav Petkovf2e3ab52008-07-23 19:56:01 +02001066out:
Borislav Petkov8d06bfa2008-02-06 02:57:53 +01001067 return idetape_issue_pc(drive, pc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068}
1069
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070/*
Borislav Petkov41aa1702008-04-27 15:38:32 +02001071 * The function below uses __get_free_pages to allocate a data buffer of size
Borislav Petkovf73850a2008-04-27 15:38:33 +02001072 * tape->buffer_size (or a bit more). We attempt to combine sequential pages as
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001073 * much as possible.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074 *
Borislav Petkov41aa1702008-04-27 15:38:32 +02001075 * It returns a pointer to the newly allocated buffer, or NULL in case of
1076 * failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077 */
Borislav Petkov077e3bd2008-04-27 15:38:34 +02001078static struct idetape_bh *ide_tape_kmalloc_buffer(idetape_tape_t *tape,
1079 int full, int clear)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080{
Borislav Petkov077e3bd2008-04-27 15:38:34 +02001081 struct idetape_bh *prev_bh, *bh, *merge_bh;
Borislav Petkova997a432008-04-27 15:38:33 +02001082 int pages = tape->pages_per_buffer;
Borislav Petkov41aa1702008-04-27 15:38:32 +02001083 unsigned int order, b_allocd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084 char *b_data = NULL;
1085
Borislav Petkov077e3bd2008-04-27 15:38:34 +02001086 merge_bh = kmalloc(sizeof(struct idetape_bh), GFP_KERNEL);
1087 bh = merge_bh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088 if (bh == NULL)
1089 goto abort;
Borislav Petkov41aa1702008-04-27 15:38:32 +02001090
1091 order = fls(pages) - 1;
1092 bh->b_data = (char *) __get_free_pages(GFP_KERNEL, order);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001093 if (!bh->b_data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094 goto abort;
Borislav Petkov41aa1702008-04-27 15:38:32 +02001095 b_allocd = (1 << order) * PAGE_SIZE;
1096 pages &= (order-1);
1097
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098 if (clear)
Borislav Petkov41aa1702008-04-27 15:38:32 +02001099 memset(bh->b_data, 0, b_allocd);
1100 bh->b_reqnext = NULL;
1101 bh->b_size = b_allocd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102 atomic_set(&bh->b_count, full ? bh->b_size : 0);
1103
Borislav Petkov41aa1702008-04-27 15:38:32 +02001104 while (pages) {
1105 order = fls(pages) - 1;
1106 b_data = (char *) __get_free_pages(GFP_KERNEL, order);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001107 if (!b_data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108 goto abort;
Borislav Petkov41aa1702008-04-27 15:38:32 +02001109 b_allocd = (1 << order) * PAGE_SIZE;
1110
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111 if (clear)
Borislav Petkov41aa1702008-04-27 15:38:32 +02001112 memset(b_data, 0, b_allocd);
1113
1114 /* newly allocated page frames below buffer header or ...*/
1115 if (bh->b_data == b_data + b_allocd) {
1116 bh->b_size += b_allocd;
1117 bh->b_data -= b_allocd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118 if (full)
Borislav Petkov41aa1702008-04-27 15:38:32 +02001119 atomic_add(b_allocd, &bh->b_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120 continue;
1121 }
Borislav Petkov41aa1702008-04-27 15:38:32 +02001122 /* they are above the header */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123 if (b_data == bh->b_data + bh->b_size) {
Borislav Petkov41aa1702008-04-27 15:38:32 +02001124 bh->b_size += b_allocd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125 if (full)
Borislav Petkov41aa1702008-04-27 15:38:32 +02001126 atomic_add(b_allocd, &bh->b_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127 continue;
1128 }
1129 prev_bh = bh;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001130 bh = kmalloc(sizeof(struct idetape_bh), GFP_KERNEL);
1131 if (!bh) {
Borislav Petkov41aa1702008-04-27 15:38:32 +02001132 free_pages((unsigned long) b_data, order);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133 goto abort;
1134 }
1135 bh->b_reqnext = NULL;
1136 bh->b_data = b_data;
Borislav Petkov41aa1702008-04-27 15:38:32 +02001137 bh->b_size = b_allocd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138 atomic_set(&bh->b_count, full ? bh->b_size : 0);
1139 prev_bh->b_reqnext = bh;
Borislav Petkov41aa1702008-04-27 15:38:32 +02001140
1141 pages &= (order-1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142 }
Borislav Petkov41aa1702008-04-27 15:38:32 +02001143
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144 bh->b_size -= tape->excess_bh_size;
1145 if (full)
1146 atomic_sub(tape->excess_bh_size, &bh->b_count);
Borislav Petkov077e3bd2008-04-27 15:38:34 +02001147 return merge_bh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148abort:
Borislav Petkov077e3bd2008-04-27 15:38:34 +02001149 ide_tape_kfree_buffer(tape);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150 return NULL;
1151}
1152
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001153static int idetape_copy_stage_from_user(idetape_tape_t *tape,
Borislav Petkov8646c882008-04-27 15:38:26 +02001154 const char __user *buf, int n)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155{
1156 struct idetape_bh *bh = tape->bh;
1157 int count;
Daniel Walkerdcd96372006-06-25 05:47:37 -07001158 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159
1160 while (n) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161 if (bh == NULL) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001162 printk(KERN_ERR "ide-tape: bh == NULL in %s\n",
1163 __func__);
Daniel Walkerdcd96372006-06-25 05:47:37 -07001164 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165 }
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001166 count = min((unsigned int)
1167 (bh->b_size - atomic_read(&bh->b_count)),
1168 (unsigned int)n);
1169 if (copy_from_user(bh->b_data + atomic_read(&bh->b_count), buf,
1170 count))
Daniel Walkerdcd96372006-06-25 05:47:37 -07001171 ret = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172 n -= count;
1173 atomic_add(count, &bh->b_count);
1174 buf += count;
1175 if (atomic_read(&bh->b_count) == bh->b_size) {
1176 bh = bh->b_reqnext;
1177 if (bh)
1178 atomic_set(&bh->b_count, 0);
1179 }
1180 }
1181 tape->bh = bh;
Daniel Walkerdcd96372006-06-25 05:47:37 -07001182 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183}
1184
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001185static int idetape_copy_stage_to_user(idetape_tape_t *tape, char __user *buf,
Borislav Petkov99d74e62008-04-27 15:38:25 +02001186 int n)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187{
1188 struct idetape_bh *bh = tape->bh;
1189 int count;
Daniel Walkerdcd96372006-06-25 05:47:37 -07001190 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191
1192 while (n) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193 if (bh == NULL) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001194 printk(KERN_ERR "ide-tape: bh == NULL in %s\n",
1195 __func__);
Daniel Walkerdcd96372006-06-25 05:47:37 -07001196 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198 count = min(tape->b_count, n);
Daniel Walkerdcd96372006-06-25 05:47:37 -07001199 if (copy_to_user(buf, tape->b_data, count))
1200 ret = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201 n -= count;
1202 tape->b_data += count;
1203 tape->b_count -= count;
1204 buf += count;
1205 if (!tape->b_count) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001206 bh = bh->b_reqnext;
1207 tape->bh = bh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208 if (bh) {
1209 tape->b_data = bh->b_data;
1210 tape->b_count = atomic_read(&bh->b_count);
1211 }
1212 }
1213 }
Daniel Walkerdcd96372006-06-25 05:47:37 -07001214 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215}
1216
Borislav Petkov077e3bd2008-04-27 15:38:34 +02001217static void idetape_init_merge_buffer(idetape_tape_t *tape)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218{
Borislav Petkov077e3bd2008-04-27 15:38:34 +02001219 struct idetape_bh *bh = tape->merge_bh;
1220 tape->bh = tape->merge_bh;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001221
Borislav Petkov54abf372008-02-06 02:57:52 +01001222 if (tape->chrdev_dir == IDETAPE_DIR_WRITE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223 atomic_set(&bh->b_count, 0);
1224 else {
1225 tape->b_data = bh->b_data;
1226 tape->b_count = atomic_read(&bh->b_count);
1227 }
1228}
1229
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001231 * Write a filemark if write_filemark=1. Flush the device buffers without
1232 * writing a filemark otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233 */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001234static void idetape_create_write_filemark_cmd(ide_drive_t *drive,
Borislav Petkovd236d742008-04-18 00:46:27 +02001235 struct ide_atapi_pc *pc, int write_filemark)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236{
1237 idetape_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +01001238 pc->c[0] = WRITE_FILEMARKS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239 pc->c[4] = write_filemark;
Borislav Petkov346331f2008-04-18 00:46:26 +02001240 pc->flags |= PC_FLAG_WAIT_FOR_DSC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241}
1242
Borislav Petkovd236d742008-04-18 00:46:27 +02001243static void idetape_create_test_unit_ready_cmd(struct ide_atapi_pc *pc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244{
1245 idetape_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +01001246 pc->c[0] = TEST_UNIT_READY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247}
1248
1249/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001250 * We add a special packet command request to the tail of the request queue, and
1251 * wait for it to be serviced. This is not to be called from within the request
1252 * handling part of the driver! We allocate here data on the stack and it is
1253 * valid until the request is finished. This is not the case for the bottom part
1254 * of the driver, where we are always leaving the functions to wait for an
1255 * interrupt or a timer event.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001257 * From the bottom part of the driver, we should allocate safe memory using
1258 * idetape_next_pc_storage() and ide_tape_next_rq_storage(), and add the request
1259 * to the request list without waiting for it to be serviced! In that case, we
1260 * usually use idetape_queue_pc_head().
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261 */
Borislav Petkovea1ab3d2008-04-27 15:38:27 +02001262static int idetape_queue_pc_tail(ide_drive_t *drive, struct ide_atapi_pc *pc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263{
1264 struct ide_tape_obj *tape = drive->driver_data;
FUJITA Tomonori64ea1b42008-07-15 21:21:43 +02001265 struct request *rq;
1266 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267
FUJITA Tomonori64ea1b42008-07-15 21:21:43 +02001268 rq = blk_get_request(drive->queue, READ, __GFP_WAIT);
1269 rq->cmd_type = REQ_TYPE_SPECIAL;
Borislav Petkov83dd5732008-07-23 19:56:00 +02001270 rq->cmd[13] = REQ_IDETAPE_PC1;
FUJITA Tomonori64ea1b42008-07-15 21:21:43 +02001271 rq->buffer = (char *)pc;
Borislav Petkov0014c752008-07-23 19:56:00 +02001272 memcpy(rq->cmd, pc->c, 12);
FUJITA Tomonori64ea1b42008-07-15 21:21:43 +02001273 error = blk_execute_rq(drive->queue, tape->disk, rq, 0);
1274 blk_put_request(rq);
1275 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276}
1277
Borislav Petkovd236d742008-04-18 00:46:27 +02001278static void idetape_create_load_unload_cmd(ide_drive_t *drive,
1279 struct ide_atapi_pc *pc, int cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280{
1281 idetape_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +01001282 pc->c[0] = START_STOP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283 pc->c[4] = cmd;
Borislav Petkov346331f2008-04-18 00:46:26 +02001284 pc->flags |= PC_FLAG_WAIT_FOR_DSC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285}
1286
1287static int idetape_wait_ready(ide_drive_t *drive, unsigned long timeout)
1288{
1289 idetape_tape_t *tape = drive->driver_data;
Borislav Petkovd236d742008-04-18 00:46:27 +02001290 struct ide_atapi_pc pc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291 int load_attempted = 0;
1292
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001293 /* Wait for the tape to become ready */
Borislav Petkovf2e3ab52008-07-23 19:56:01 +02001294 set_bit(IDE_AFLAG_MEDIUM_PRESENT, &drive->atapi_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295 timeout += jiffies;
1296 while (time_before(jiffies, timeout)) {
1297 idetape_create_test_unit_ready_cmd(&pc);
Borislav Petkovea1ab3d2008-04-27 15:38:27 +02001298 if (!idetape_queue_pc_tail(drive, &pc))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299 return 0;
1300 if ((tape->sense_key == 2 && tape->asc == 4 && tape->ascq == 2)
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001301 || (tape->asc == 0x3A)) {
1302 /* no media */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303 if (load_attempted)
1304 return -ENOMEDIUM;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001305 idetape_create_load_unload_cmd(drive, &pc,
1306 IDETAPE_LU_LOAD_MASK);
Borislav Petkovea1ab3d2008-04-27 15:38:27 +02001307 idetape_queue_pc_tail(drive, &pc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308 load_attempted = 1;
1309 /* not about to be ready */
1310 } else if (!(tape->sense_key == 2 && tape->asc == 4 &&
1311 (tape->ascq == 1 || tape->ascq == 8)))
1312 return -EIO;
Nishanth Aravamudan80ce45f2005-09-10 00:27:08 -07001313 msleep(100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314 }
1315 return -EIO;
1316}
1317
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001318static int idetape_flush_tape_buffers(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319{
Borislav Petkovd236d742008-04-18 00:46:27 +02001320 struct ide_atapi_pc pc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321 int rc;
1322
1323 idetape_create_write_filemark_cmd(drive, &pc, 0);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001324 rc = idetape_queue_pc_tail(drive, &pc);
1325 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326 return rc;
1327 idetape_wait_ready(drive, 60 * 5 * HZ);
1328 return 0;
1329}
1330
Borislav Petkovd236d742008-04-18 00:46:27 +02001331static void idetape_create_read_position_cmd(struct ide_atapi_pc *pc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332{
1333 idetape_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +01001334 pc->c[0] = READ_POSITION;
Borislav Petkovd236d742008-04-18 00:46:27 +02001335 pc->req_xfer = 20;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336}
1337
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001338static int idetape_read_position(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339{
1340 idetape_tape_t *tape = drive->driver_data;
Borislav Petkovd236d742008-04-18 00:46:27 +02001341 struct ide_atapi_pc pc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342 int position;
1343
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001344 debug_log(DBG_PROCS, "Enter %s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345
1346 idetape_create_read_position_cmd(&pc);
1347 if (idetape_queue_pc_tail(drive, &pc))
1348 return -1;
Borislav Petkov54bb2072008-02-06 02:57:52 +01001349 position = tape->first_frame;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350 return position;
1351}
1352
Borislav Petkovd236d742008-04-18 00:46:27 +02001353static void idetape_create_locate_cmd(ide_drive_t *drive,
1354 struct ide_atapi_pc *pc,
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001355 unsigned int block, u8 partition, int skip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356{
1357 idetape_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +01001358 pc->c[0] = POSITION_TO_ELEMENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359 pc->c[1] = 2;
Borislav Petkov860ff5e2008-02-02 19:56:50 +01001360 put_unaligned(cpu_to_be32(block), (unsigned int *) &pc->c[3]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361 pc->c[8] = partition;
Borislav Petkov346331f2008-04-18 00:46:26 +02001362 pc->flags |= PC_FLAG_WAIT_FOR_DSC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363}
1364
Borislav Petkovd236d742008-04-18 00:46:27 +02001365static int idetape_create_prevent_cmd(ide_drive_t *drive,
1366 struct ide_atapi_pc *pc, int prevent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367{
1368 idetape_tape_t *tape = drive->driver_data;
1369
Borislav Petkovb6422012008-02-02 19:56:49 +01001370 /* device supports locking according to capabilities page */
1371 if (!(tape->caps[6] & 0x01))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372 return 0;
1373
1374 idetape_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +01001375 pc->c[0] = ALLOW_MEDIUM_REMOVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376 pc->c[4] = prevent;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377 return 1;
1378}
1379
Borislav Petkovec0fdb02008-04-27 15:38:34 +02001380static void __ide_tape_discard_merge_buffer(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381{
1382 idetape_tape_t *tape = drive->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383
Borislav Petkov54abf372008-02-06 02:57:52 +01001384 if (tape->chrdev_dir != IDETAPE_DIR_READ)
Borislav Petkov97986302008-04-27 15:38:34 +02001385 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386
Borislav Petkovf2e3ab52008-07-23 19:56:01 +02001387 clear_bit(IDE_AFLAG_FILEMARK, &drive->atapi_flags);
Borislav Petkov01a63aeb2008-04-27 15:38:34 +02001388 tape->merge_bh_size = 0;
Borislav Petkov077e3bd2008-04-27 15:38:34 +02001389 if (tape->merge_bh != NULL) {
1390 ide_tape_kfree_buffer(tape);
1391 tape->merge_bh = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392 }
1393
Borislav Petkov54abf372008-02-06 02:57:52 +01001394 tape->chrdev_dir = IDETAPE_DIR_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395}
1396
1397/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001398 * Position the tape to the requested block using the LOCATE packet command.
1399 * A READ POSITION command is then issued to check where we are positioned. Like
1400 * all higher level operations, we queue the commands at the tail of the request
1401 * queue and wait for their completion.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402 */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001403static int idetape_position_tape(ide_drive_t *drive, unsigned int block,
1404 u8 partition, int skip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405{
1406 idetape_tape_t *tape = drive->driver_data;
1407 int retval;
Borislav Petkovd236d742008-04-18 00:46:27 +02001408 struct ide_atapi_pc pc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409
Borislav Petkov54abf372008-02-06 02:57:52 +01001410 if (tape->chrdev_dir == IDETAPE_DIR_READ)
Borislav Petkovec0fdb02008-04-27 15:38:34 +02001411 __ide_tape_discard_merge_buffer(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412 idetape_wait_ready(drive, 60 * 5 * HZ);
1413 idetape_create_locate_cmd(drive, &pc, block, partition, skip);
1414 retval = idetape_queue_pc_tail(drive, &pc);
1415 if (retval)
1416 return (retval);
1417
1418 idetape_create_read_position_cmd(&pc);
1419 return (idetape_queue_pc_tail(drive, &pc));
1420}
1421
Borislav Petkovec0fdb02008-04-27 15:38:34 +02001422static void ide_tape_discard_merge_buffer(ide_drive_t *drive,
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001423 int restore_position)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424{
1425 idetape_tape_t *tape = drive->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426 int seek, position;
1427
Borislav Petkovec0fdb02008-04-27 15:38:34 +02001428 __ide_tape_discard_merge_buffer(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429 if (restore_position) {
1430 position = idetape_read_position(drive);
Borislav Petkov97986302008-04-27 15:38:34 +02001431 seek = position > 0 ? position : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432 if (idetape_position_tape(drive, seek, 0, 0)) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001433 printk(KERN_INFO "ide-tape: %s: position_tape failed in"
Borislav Petkovec0fdb02008-04-27 15:38:34 +02001434 " %s\n", tape->name, __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435 return;
1436 }
1437 }
1438}
1439
1440/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001441 * Generate a read/write request for the block device interface and wait for it
1442 * to be serviced.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443 */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001444static int idetape_queue_rw_tail(ide_drive_t *drive, int cmd, int blocks,
1445 struct idetape_bh *bh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446{
1447 idetape_tape_t *tape = drive->driver_data;
FUJITA Tomonori64ea1b42008-07-15 21:21:43 +02001448 struct request *rq;
1449 int ret, errors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001451 debug_log(DBG_SENSE, "%s: cmd=%d\n", __func__, cmd);
1452
FUJITA Tomonori64ea1b42008-07-15 21:21:43 +02001453 rq = blk_get_request(drive->queue, READ, __GFP_WAIT);
1454 rq->cmd_type = REQ_TYPE_SPECIAL;
Borislav Petkov83dd5732008-07-23 19:56:00 +02001455 rq->cmd[13] = cmd;
FUJITA Tomonori64ea1b42008-07-15 21:21:43 +02001456 rq->rq_disk = tape->disk;
1457 rq->special = (void *)bh;
1458 rq->sector = tape->first_frame;
1459 rq->nr_sectors = blocks;
1460 rq->current_nr_sectors = blocks;
1461 blk_execute_rq(drive->queue, tape->disk, rq, 0);
1462
1463 errors = rq->errors;
1464 ret = tape->blk_size * (blocks - rq->current_nr_sectors);
1465 blk_put_request(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466
1467 if ((cmd & (REQ_IDETAPE_READ | REQ_IDETAPE_WRITE)) == 0)
1468 return 0;
1469
Borislav Petkov077e3bd2008-04-27 15:38:34 +02001470 if (tape->merge_bh)
1471 idetape_init_merge_buffer(tape);
FUJITA Tomonori64ea1b42008-07-15 21:21:43 +02001472 if (errors == IDETAPE_ERROR_GENERAL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473 return -EIO;
FUJITA Tomonori64ea1b42008-07-15 21:21:43 +02001474 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475}
1476
Borislav Petkovd236d742008-04-18 00:46:27 +02001477static void idetape_create_inquiry_cmd(struct ide_atapi_pc *pc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478{
1479 idetape_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +01001480 pc->c[0] = INQUIRY;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001481 pc->c[4] = 254;
Borislav Petkovd236d742008-04-18 00:46:27 +02001482 pc->req_xfer = 254;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483}
1484
Borislav Petkovd236d742008-04-18 00:46:27 +02001485static void idetape_create_rewind_cmd(ide_drive_t *drive,
1486 struct ide_atapi_pc *pc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487{
1488 idetape_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +01001489 pc->c[0] = REZERO_UNIT;
Borislav Petkov346331f2008-04-18 00:46:26 +02001490 pc->flags |= PC_FLAG_WAIT_FOR_DSC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491}
1492
Borislav Petkovd236d742008-04-18 00:46:27 +02001493static void idetape_create_erase_cmd(struct ide_atapi_pc *pc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494{
1495 idetape_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +01001496 pc->c[0] = ERASE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497 pc->c[1] = 1;
Borislav Petkov346331f2008-04-18 00:46:26 +02001498 pc->flags |= PC_FLAG_WAIT_FOR_DSC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499}
1500
Borislav Petkovd236d742008-04-18 00:46:27 +02001501static void idetape_create_space_cmd(struct ide_atapi_pc *pc, int count, u8 cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502{
1503 idetape_init_pc(pc);
Borislav Petkov90699ce2008-02-02 19:56:50 +01001504 pc->c[0] = SPACE;
Borislav Petkov860ff5e2008-02-02 19:56:50 +01001505 put_unaligned(cpu_to_be32(count), (unsigned int *) &pc->c[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506 pc->c[1] = cmd;
Borislav Petkov346331f2008-04-18 00:46:26 +02001507 pc->flags |= PC_FLAG_WAIT_FOR_DSC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001508}
1509
Borislav Petkov97c566c2008-04-27 15:38:25 +02001510/* Queue up a character device originated write request. */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001511static int idetape_add_chrdev_write_request(ide_drive_t *drive, int blocks)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512{
1513 idetape_tape_t *tape = drive->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001515 debug_log(DBG_CHRDEV, "Enter %s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001516
Borislav Petkov0aa4b012008-04-27 15:38:27 +02001517 return idetape_queue_rw_tail(drive, REQ_IDETAPE_WRITE,
Borislav Petkov077e3bd2008-04-27 15:38:34 +02001518 blocks, tape->merge_bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519}
1520
Borislav Petkovd9df9372008-04-27 15:38:34 +02001521static void ide_tape_flush_merge_buffer(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522{
1523 idetape_tape_t *tape = drive->driver_data;
1524 int blocks, min;
1525 struct idetape_bh *bh;
Borislav Petkov55a5d292008-02-02 19:56:49 +01001526
Borislav Petkov54abf372008-02-06 02:57:52 +01001527 if (tape->chrdev_dir != IDETAPE_DIR_WRITE) {
Borislav Petkov077e3bd2008-04-27 15:38:34 +02001528 printk(KERN_ERR "ide-tape: bug: Trying to empty merge buffer"
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001529 " but we are not writing.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001530 return;
1531 }
Borislav Petkov01a63aeb2008-04-27 15:38:34 +02001532 if (tape->merge_bh_size > tape->buffer_size) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533 printk(KERN_ERR "ide-tape: bug: merge_buffer too big\n");
Borislav Petkov01a63aeb2008-04-27 15:38:34 +02001534 tape->merge_bh_size = tape->buffer_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535 }
Borislav Petkov01a63aeb2008-04-27 15:38:34 +02001536 if (tape->merge_bh_size) {
1537 blocks = tape->merge_bh_size / tape->blk_size;
1538 if (tape->merge_bh_size % tape->blk_size) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539 unsigned int i;
1540
1541 blocks++;
Borislav Petkov01a63aeb2008-04-27 15:38:34 +02001542 i = tape->blk_size - tape->merge_bh_size %
Borislav Petkov54bb2072008-02-06 02:57:52 +01001543 tape->blk_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544 bh = tape->bh->b_reqnext;
1545 while (bh) {
1546 atomic_set(&bh->b_count, 0);
1547 bh = bh->b_reqnext;
1548 }
1549 bh = tape->bh;
1550 while (i) {
1551 if (bh == NULL) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001552 printk(KERN_INFO "ide-tape: bug,"
1553 " bh NULL\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554 break;
1555 }
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001556 min = min(i, (unsigned int)(bh->b_size -
1557 atomic_read(&bh->b_count)));
1558 memset(bh->b_data + atomic_read(&bh->b_count),
1559 0, min);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560 atomic_add(min, &bh->b_count);
1561 i -= min;
1562 bh = bh->b_reqnext;
1563 }
1564 }
1565 (void) idetape_add_chrdev_write_request(drive, blocks);
Borislav Petkov01a63aeb2008-04-27 15:38:34 +02001566 tape->merge_bh_size = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567 }
Borislav Petkov077e3bd2008-04-27 15:38:34 +02001568 if (tape->merge_bh != NULL) {
1569 ide_tape_kfree_buffer(tape);
1570 tape->merge_bh = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571 }
Borislav Petkov54abf372008-02-06 02:57:52 +01001572 tape->chrdev_dir = IDETAPE_DIR_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001573}
1574
Borislav Petkov83042b22008-04-27 15:38:27 +02001575static int idetape_init_read(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001576{
1577 idetape_tape_t *tape = drive->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001578 int bytes_read;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579
1580 /* Initialize read operation */
Borislav Petkov54abf372008-02-06 02:57:52 +01001581 if (tape->chrdev_dir != IDETAPE_DIR_READ) {
1582 if (tape->chrdev_dir == IDETAPE_DIR_WRITE) {
Borislav Petkovd9df9372008-04-27 15:38:34 +02001583 ide_tape_flush_merge_buffer(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584 idetape_flush_tape_buffers(drive);
1585 }
Borislav Petkov077e3bd2008-04-27 15:38:34 +02001586 if (tape->merge_bh || tape->merge_bh_size) {
Borislav Petkov01a63aeb2008-04-27 15:38:34 +02001587 printk(KERN_ERR "ide-tape: merge_bh_size should be"
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001588 " 0 now\n");
Borislav Petkov01a63aeb2008-04-27 15:38:34 +02001589 tape->merge_bh_size = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590 }
Borislav Petkov077e3bd2008-04-27 15:38:34 +02001591 tape->merge_bh = ide_tape_kmalloc_buffer(tape, 0, 0);
1592 if (!tape->merge_bh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001593 return -ENOMEM;
Borislav Petkov54abf372008-02-06 02:57:52 +01001594 tape->chrdev_dir = IDETAPE_DIR_READ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001595
1596 /*
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001597 * Issue a read 0 command to ensure that DSC handshake is
1598 * switched from completion mode to buffer available mode.
1599 * No point in issuing this if DSC overlap isn't supported, some
1600 * drives (Seagate STT3401A) will return an error.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001601 */
1602 if (drive->dsc_overlap) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001603 bytes_read = idetape_queue_rw_tail(drive,
1604 REQ_IDETAPE_READ, 0,
Borislav Petkov077e3bd2008-04-27 15:38:34 +02001605 tape->merge_bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606 if (bytes_read < 0) {
Borislav Petkov077e3bd2008-04-27 15:38:34 +02001607 ide_tape_kfree_buffer(tape);
1608 tape->merge_bh = NULL;
Borislav Petkov54abf372008-02-06 02:57:52 +01001609 tape->chrdev_dir = IDETAPE_DIR_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610 return bytes_read;
1611 }
1612 }
1613 }
Borislav Petkov5e69bd92008-04-27 15:38:25 +02001614
Linus Torvalds1da177e2005-04-16 15:20:36 -07001615 return 0;
1616}
1617
Borislav Petkov5bd50dc2008-04-27 15:38:28 +02001618/* called from idetape_chrdev_read() to service a chrdev read request. */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001619static int idetape_add_chrdev_read_request(ide_drive_t *drive, int blocks)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001620{
1621 idetape_tape_t *tape = drive->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001622
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001623 debug_log(DBG_PROCS, "Enter %s, %d blocks\n", __func__, blocks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001624
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001625 /* If we are at a filemark, return a read length of 0 */
Borislav Petkovf2e3ab52008-07-23 19:56:01 +02001626 if (test_bit(IDE_AFLAG_FILEMARK, &drive->atapi_flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001627 return 0;
1628
Borislav Petkov83042b22008-04-27 15:38:27 +02001629 idetape_init_read(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001630
Borislav Petkov5e69bd92008-04-27 15:38:25 +02001631 return idetape_queue_rw_tail(drive, REQ_IDETAPE_READ, blocks,
Borislav Petkov077e3bd2008-04-27 15:38:34 +02001632 tape->merge_bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001633}
1634
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001635static void idetape_pad_zeros(ide_drive_t *drive, int bcount)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636{
1637 idetape_tape_t *tape = drive->driver_data;
1638 struct idetape_bh *bh;
1639 int blocks;
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001640
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641 while (bcount) {
1642 unsigned int count;
1643
Borislav Petkov077e3bd2008-04-27 15:38:34 +02001644 bh = tape->merge_bh;
Borislav Petkovf73850a2008-04-27 15:38:33 +02001645 count = min(tape->buffer_size, bcount);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001646 bcount -= count;
Borislav Petkov54bb2072008-02-06 02:57:52 +01001647 blocks = count / tape->blk_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001648 while (count) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001649 atomic_set(&bh->b_count,
1650 min(count, (unsigned int)bh->b_size));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001651 memset(bh->b_data, 0, atomic_read(&bh->b_count));
1652 count -= atomic_read(&bh->b_count);
1653 bh = bh->b_reqnext;
1654 }
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001655 idetape_queue_rw_tail(drive, REQ_IDETAPE_WRITE, blocks,
Borislav Petkov077e3bd2008-04-27 15:38:34 +02001656 tape->merge_bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001657 }
1658}
1659
Linus Torvalds1da177e2005-04-16 15:20:36 -07001660/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001661 * Rewinds the tape to the Beginning Of the current Partition (BOP). We
1662 * currently support only one partition.
1663 */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001664static int idetape_rewind_tape(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001665{
1666 int retval;
Borislav Petkovd236d742008-04-18 00:46:27 +02001667 struct ide_atapi_pc pc;
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001668 idetape_tape_t *tape;
1669 tape = drive->driver_data;
1670
1671 debug_log(DBG_SENSE, "Enter %s\n", __func__);
1672
Linus Torvalds1da177e2005-04-16 15:20:36 -07001673 idetape_create_rewind_cmd(drive, &pc);
1674 retval = idetape_queue_pc_tail(drive, &pc);
1675 if (retval)
1676 return retval;
1677
1678 idetape_create_read_position_cmd(&pc);
1679 retval = idetape_queue_pc_tail(drive, &pc);
1680 if (retval)
1681 return retval;
1682 return 0;
1683}
1684
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001685/* mtio.h compatible commands should be issued to the chrdev interface. */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001686static int idetape_blkdev_ioctl(ide_drive_t *drive, unsigned int cmd,
1687 unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001688{
1689 idetape_tape_t *tape = drive->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001690 void __user *argp = (void __user *)arg;
1691
Borislav Petkovd59823f2008-02-02 19:56:51 +01001692 struct idetape_config {
1693 int dsc_rw_frequency;
1694 int dsc_media_access_frequency;
1695 int nr_stages;
1696 } config;
1697
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001698 debug_log(DBG_PROCS, "Enter %s\n", __func__);
1699
Linus Torvalds1da177e2005-04-16 15:20:36 -07001700 switch (cmd) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001701 case 0x0340:
1702 if (copy_from_user(&config, argp, sizeof(config)))
1703 return -EFAULT;
1704 tape->best_dsc_rw_freq = config.dsc_rw_frequency;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001705 break;
1706 case 0x0350:
1707 config.dsc_rw_frequency = (int) tape->best_dsc_rw_freq;
Borislav Petkov83042b22008-04-27 15:38:27 +02001708 config.nr_stages = 1;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001709 if (copy_to_user(argp, &config, sizeof(config)))
1710 return -EFAULT;
1711 break;
1712 default:
1713 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001714 }
1715 return 0;
1716}
1717
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001718static int idetape_space_over_filemarks(ide_drive_t *drive, short mt_op,
1719 int mt_count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001720{
1721 idetape_tape_t *tape = drive->driver_data;
Borislav Petkovd236d742008-04-18 00:46:27 +02001722 struct ide_atapi_pc pc;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001723 int retval, count = 0;
Borislav Petkovb6422012008-02-02 19:56:49 +01001724 int sprev = !!(tape->caps[4] & 0x20);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001725
1726 if (mt_count == 0)
1727 return 0;
1728 if (MTBSF == mt_op || MTBSFM == mt_op) {
Borislav Petkovb6422012008-02-02 19:56:49 +01001729 if (!sprev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001730 return -EIO;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001731 mt_count = -mt_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001732 }
1733
Borislav Petkov54abf372008-02-06 02:57:52 +01001734 if (tape->chrdev_dir == IDETAPE_DIR_READ) {
Borislav Petkov01a63aeb2008-04-27 15:38:34 +02001735 tape->merge_bh_size = 0;
Borislav Petkovf2e3ab52008-07-23 19:56:01 +02001736 if (test_and_clear_bit(IDE_AFLAG_FILEMARK, &drive->atapi_flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001737 ++count;
Borislav Petkovec0fdb02008-04-27 15:38:34 +02001738 ide_tape_discard_merge_buffer(drive, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001739 }
1740
Linus Torvalds1da177e2005-04-16 15:20:36 -07001741 switch (mt_op) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001742 case MTFSF:
1743 case MTBSF:
1744 idetape_create_space_cmd(&pc, mt_count - count,
1745 IDETAPE_SPACE_OVER_FILEMARK);
1746 return idetape_queue_pc_tail(drive, &pc);
1747 case MTFSFM:
1748 case MTBSFM:
1749 if (!sprev)
1750 return -EIO;
1751 retval = idetape_space_over_filemarks(drive, MTFSF,
1752 mt_count - count);
1753 if (retval)
1754 return retval;
1755 count = (MTBSFM == mt_op ? 1 : -1);
1756 return idetape_space_over_filemarks(drive, MTFSF, count);
1757 default:
1758 printk(KERN_ERR "ide-tape: MTIO operation %d not supported\n",
1759 mt_op);
1760 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001761 }
1762}
1763
Linus Torvalds1da177e2005-04-16 15:20:36 -07001764/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001765 * Our character device read / write functions.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001766 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001767 * The tape is optimized to maximize throughput when it is transferring an
1768 * integral number of the "continuous transfer limit", which is a parameter of
1769 * the specific tape (26kB on my particular tape, 32kB for Onstream).
Linus Torvalds1da177e2005-04-16 15:20:36 -07001770 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001771 * As of version 1.3 of the driver, the character device provides an abstract
1772 * continuous view of the media - any mix of block sizes (even 1 byte) on the
1773 * same backup/restore procedure is supported. The driver will internally
1774 * convert the requests to the recommended transfer unit, so that an unmatch
1775 * between the user's block size to the recommended size will only result in a
1776 * (slightly) increased driver overhead, but will no longer hit performance.
1777 * This is not applicable to Onstream.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001778 */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001779static ssize_t idetape_chrdev_read(struct file *file, char __user *buf,
1780 size_t count, loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001781{
1782 struct ide_tape_obj *tape = ide_tape_f(file);
1783 ide_drive_t *drive = tape->drive;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001784 ssize_t bytes_read, temp, actually_read = 0, rc;
Daniel Walkerdcd96372006-06-25 05:47:37 -07001785 ssize_t ret = 0;
Borislav Petkovb6422012008-02-02 19:56:49 +01001786 u16 ctl = *(u16 *)&tape->caps[12];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001787
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001788 debug_log(DBG_CHRDEV, "Enter %s, count %Zd\n", __func__, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001789
Borislav Petkov54abf372008-02-06 02:57:52 +01001790 if (tape->chrdev_dir != IDETAPE_DIR_READ) {
Borislav Petkovf2e3ab52008-07-23 19:56:01 +02001791 if (test_bit(IDE_AFLAG_DETECT_BS, &drive->atapi_flags))
Borislav Petkov54bb2072008-02-06 02:57:52 +01001792 if (count > tape->blk_size &&
1793 (count % tape->blk_size) == 0)
1794 tape->user_bs_factor = count / tape->blk_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001795 }
Borislav Petkov83042b22008-04-27 15:38:27 +02001796 rc = idetape_init_read(drive);
Borislav Petkov8d06bfa2008-02-06 02:57:53 +01001797 if (rc < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001798 return rc;
1799 if (count == 0)
1800 return (0);
Borislav Petkov01a63aeb2008-04-27 15:38:34 +02001801 if (tape->merge_bh_size) {
1802 actually_read = min((unsigned int)(tape->merge_bh_size),
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001803 (unsigned int)count);
Borislav Petkov99d74e62008-04-27 15:38:25 +02001804 if (idetape_copy_stage_to_user(tape, buf, actually_read))
Daniel Walkerdcd96372006-06-25 05:47:37 -07001805 ret = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001806 buf += actually_read;
Borislav Petkov01a63aeb2008-04-27 15:38:34 +02001807 tape->merge_bh_size -= actually_read;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001808 count -= actually_read;
1809 }
Borislav Petkovf73850a2008-04-27 15:38:33 +02001810 while (count >= tape->buffer_size) {
Borislav Petkovb6422012008-02-02 19:56:49 +01001811 bytes_read = idetape_add_chrdev_read_request(drive, ctl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001812 if (bytes_read <= 0)
1813 goto finish;
Borislav Petkov99d74e62008-04-27 15:38:25 +02001814 if (idetape_copy_stage_to_user(tape, buf, bytes_read))
Daniel Walkerdcd96372006-06-25 05:47:37 -07001815 ret = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001816 buf += bytes_read;
1817 count -= bytes_read;
1818 actually_read += bytes_read;
1819 }
1820 if (count) {
Borislav Petkovb6422012008-02-02 19:56:49 +01001821 bytes_read = idetape_add_chrdev_read_request(drive, ctl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001822 if (bytes_read <= 0)
1823 goto finish;
1824 temp = min((unsigned long)count, (unsigned long)bytes_read);
Borislav Petkov99d74e62008-04-27 15:38:25 +02001825 if (idetape_copy_stage_to_user(tape, buf, temp))
Daniel Walkerdcd96372006-06-25 05:47:37 -07001826 ret = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001827 actually_read += temp;
Borislav Petkov01a63aeb2008-04-27 15:38:34 +02001828 tape->merge_bh_size = bytes_read-temp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001829 }
1830finish:
Borislav Petkovf2e3ab52008-07-23 19:56:01 +02001831 if (!actually_read && test_bit(IDE_AFLAG_FILEMARK, &drive->atapi_flags)) {
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001832 debug_log(DBG_SENSE, "%s: spacing over filemark\n", tape->name);
1833
Linus Torvalds1da177e2005-04-16 15:20:36 -07001834 idetape_space_over_filemarks(drive, MTFSF, 1);
1835 return 0;
1836 }
Daniel Walkerdcd96372006-06-25 05:47:37 -07001837
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001838 return ret ? ret : actually_read;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001839}
1840
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001841static ssize_t idetape_chrdev_write(struct file *file, const char __user *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001842 size_t count, loff_t *ppos)
1843{
1844 struct ide_tape_obj *tape = ide_tape_f(file);
1845 ide_drive_t *drive = tape->drive;
Daniel Walkerdcd96372006-06-25 05:47:37 -07001846 ssize_t actually_written = 0;
1847 ssize_t ret = 0;
Borislav Petkovb6422012008-02-02 19:56:49 +01001848 u16 ctl = *(u16 *)&tape->caps[12];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001849
1850 /* The drive is write protected. */
1851 if (tape->write_prot)
1852 return -EACCES;
1853
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001854 debug_log(DBG_CHRDEV, "Enter %s, count %Zd\n", __func__, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001855
1856 /* Initialize write operation */
Borislav Petkov54abf372008-02-06 02:57:52 +01001857 if (tape->chrdev_dir != IDETAPE_DIR_WRITE) {
1858 if (tape->chrdev_dir == IDETAPE_DIR_READ)
Borislav Petkovec0fdb02008-04-27 15:38:34 +02001859 ide_tape_discard_merge_buffer(drive, 1);
Borislav Petkov077e3bd2008-04-27 15:38:34 +02001860 if (tape->merge_bh || tape->merge_bh_size) {
Borislav Petkov01a63aeb2008-04-27 15:38:34 +02001861 printk(KERN_ERR "ide-tape: merge_bh_size "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001862 "should be 0 now\n");
Borislav Petkov01a63aeb2008-04-27 15:38:34 +02001863 tape->merge_bh_size = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001864 }
Borislav Petkov077e3bd2008-04-27 15:38:34 +02001865 tape->merge_bh = ide_tape_kmalloc_buffer(tape, 0, 0);
1866 if (!tape->merge_bh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001867 return -ENOMEM;
Borislav Petkov54abf372008-02-06 02:57:52 +01001868 tape->chrdev_dir = IDETAPE_DIR_WRITE;
Borislav Petkov077e3bd2008-04-27 15:38:34 +02001869 idetape_init_merge_buffer(tape);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001870
1871 /*
Borislav Petkov3c98bf32008-02-06 02:57:53 +01001872 * Issue a write 0 command to ensure that DSC handshake is
1873 * switched from completion mode to buffer available mode. No
1874 * point in issuing this if DSC overlap isn't supported, some
1875 * drives (Seagate STT3401A) will return an error.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001876 */
1877 if (drive->dsc_overlap) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001878 ssize_t retval = idetape_queue_rw_tail(drive,
1879 REQ_IDETAPE_WRITE, 0,
Borislav Petkov077e3bd2008-04-27 15:38:34 +02001880 tape->merge_bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001881 if (retval < 0) {
Borislav Petkov077e3bd2008-04-27 15:38:34 +02001882 ide_tape_kfree_buffer(tape);
1883 tape->merge_bh = NULL;
Borislav Petkov54abf372008-02-06 02:57:52 +01001884 tape->chrdev_dir = IDETAPE_DIR_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001885 return retval;
1886 }
1887 }
1888 }
1889 if (count == 0)
1890 return (0);
Borislav Petkov01a63aeb2008-04-27 15:38:34 +02001891 if (tape->merge_bh_size) {
1892 if (tape->merge_bh_size >= tape->buffer_size) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001893 printk(KERN_ERR "ide-tape: bug: merge buf too big\n");
Borislav Petkov01a63aeb2008-04-27 15:38:34 +02001894 tape->merge_bh_size = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001895 }
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001896 actually_written = min((unsigned int)
Borislav Petkov01a63aeb2008-04-27 15:38:34 +02001897 (tape->buffer_size - tape->merge_bh_size),
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001898 (unsigned int)count);
Borislav Petkov8646c882008-04-27 15:38:26 +02001899 if (idetape_copy_stage_from_user(tape, buf, actually_written))
Daniel Walkerdcd96372006-06-25 05:47:37 -07001900 ret = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001901 buf += actually_written;
Borislav Petkov01a63aeb2008-04-27 15:38:34 +02001902 tape->merge_bh_size += actually_written;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001903 count -= actually_written;
1904
Borislav Petkov01a63aeb2008-04-27 15:38:34 +02001905 if (tape->merge_bh_size == tape->buffer_size) {
Daniel Walkerdcd96372006-06-25 05:47:37 -07001906 ssize_t retval;
Borislav Petkov01a63aeb2008-04-27 15:38:34 +02001907 tape->merge_bh_size = 0;
Borislav Petkovb6422012008-02-02 19:56:49 +01001908 retval = idetape_add_chrdev_write_request(drive, ctl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001909 if (retval <= 0)
1910 return (retval);
1911 }
1912 }
Borislav Petkovf73850a2008-04-27 15:38:33 +02001913 while (count >= tape->buffer_size) {
Daniel Walkerdcd96372006-06-25 05:47:37 -07001914 ssize_t retval;
Borislav Petkovf73850a2008-04-27 15:38:33 +02001915 if (idetape_copy_stage_from_user(tape, buf, tape->buffer_size))
Daniel Walkerdcd96372006-06-25 05:47:37 -07001916 ret = -EFAULT;
Borislav Petkovf73850a2008-04-27 15:38:33 +02001917 buf += tape->buffer_size;
1918 count -= tape->buffer_size;
Borislav Petkovb6422012008-02-02 19:56:49 +01001919 retval = idetape_add_chrdev_write_request(drive, ctl);
Borislav Petkovf73850a2008-04-27 15:38:33 +02001920 actually_written += tape->buffer_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001921 if (retval <= 0)
1922 return (retval);
1923 }
1924 if (count) {
1925 actually_written += count;
Borislav Petkov8646c882008-04-27 15:38:26 +02001926 if (idetape_copy_stage_from_user(tape, buf, count))
Daniel Walkerdcd96372006-06-25 05:47:37 -07001927 ret = -EFAULT;
Borislav Petkov01a63aeb2008-04-27 15:38:34 +02001928 tape->merge_bh_size += count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001929 }
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001930 return ret ? ret : actually_written;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001931}
1932
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001933static int idetape_write_filemark(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001934{
Borislav Petkovd236d742008-04-18 00:46:27 +02001935 struct ide_atapi_pc pc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001936
1937 /* Write a filemark */
1938 idetape_create_write_filemark_cmd(drive, &pc, 1);
1939 if (idetape_queue_pc_tail(drive, &pc)) {
1940 printk(KERN_ERR "ide-tape: Couldn't write a filemark\n");
1941 return -EIO;
1942 }
1943 return 0;
1944}
1945
1946/*
Borislav Petkovd99c9da2008-02-02 19:56:51 +01001947 * Called from idetape_chrdev_ioctl when the general mtio MTIOCTOP ioctl is
1948 * requested.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001949 *
Borislav Petkovd99c9da2008-02-02 19:56:51 +01001950 * Note: MTBSF and MTBSFM are not supported when the tape doesn't support
1951 * spacing over filemarks in the reverse direction. In this case, MTFSFM is also
Borislav Petkov5bd50dc2008-04-27 15:38:28 +02001952 * usually not supported.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001953 *
Borislav Petkovd99c9da2008-02-02 19:56:51 +01001954 * The following commands are currently not supported:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001955 *
Borislav Petkovd99c9da2008-02-02 19:56:51 +01001956 * MTFSS, MTBSS, MTWSM, MTSETDENSITY, MTSETDRVBUFFER, MT_ST_BOOLEANS,
1957 * MT_ST_WRITE_THRESHOLD.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001958 */
Borislav Petkovd99c9da2008-02-02 19:56:51 +01001959static int idetape_mtioctop(ide_drive_t *drive, short mt_op, int mt_count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001960{
1961 idetape_tape_t *tape = drive->driver_data;
Borislav Petkovd236d742008-04-18 00:46:27 +02001962 struct ide_atapi_pc pc;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001963 int i, retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001964
Borislav Petkov8004a8c2008-02-06 02:57:51 +01001965 debug_log(DBG_ERR, "Handling MTIOCTOP ioctl: mt_op=%d, mt_count=%d\n",
1966 mt_op, mt_count);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001967
Linus Torvalds1da177e2005-04-16 15:20:36 -07001968 switch (mt_op) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001969 case MTFSF:
1970 case MTFSFM:
1971 case MTBSF:
1972 case MTBSFM:
1973 if (!mt_count)
1974 return 0;
1975 return idetape_space_over_filemarks(drive, mt_op, mt_count);
1976 default:
1977 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001978 }
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001979
Linus Torvalds1da177e2005-04-16 15:20:36 -07001980 switch (mt_op) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001981 case MTWEOF:
1982 if (tape->write_prot)
1983 return -EACCES;
Borislav Petkovec0fdb02008-04-27 15:38:34 +02001984 ide_tape_discard_merge_buffer(drive, 1);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001985 for (i = 0; i < mt_count; i++) {
1986 retval = idetape_write_filemark(drive);
1987 if (retval)
1988 return retval;
1989 }
1990 return 0;
1991 case MTREW:
Borislav Petkovec0fdb02008-04-27 15:38:34 +02001992 ide_tape_discard_merge_buffer(drive, 0);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001993 if (idetape_rewind_tape(drive))
1994 return -EIO;
1995 return 0;
1996 case MTLOAD:
Borislav Petkovec0fdb02008-04-27 15:38:34 +02001997 ide_tape_discard_merge_buffer(drive, 0);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01001998 idetape_create_load_unload_cmd(drive, &pc,
1999 IDETAPE_LU_LOAD_MASK);
2000 return idetape_queue_pc_tail(drive, &pc);
2001 case MTUNLOAD:
2002 case MTOFFL:
2003 /*
2004 * If door is locked, attempt to unlock before
2005 * attempting to eject.
2006 */
2007 if (tape->door_locked) {
2008 if (idetape_create_prevent_cmd(drive, &pc, 0))
2009 if (!idetape_queue_pc_tail(drive, &pc))
2010 tape->door_locked = DOOR_UNLOCKED;
2011 }
Borislav Petkovec0fdb02008-04-27 15:38:34 +02002012 ide_tape_discard_merge_buffer(drive, 0);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002013 idetape_create_load_unload_cmd(drive, &pc,
2014 !IDETAPE_LU_LOAD_MASK);
2015 retval = idetape_queue_pc_tail(drive, &pc);
2016 if (!retval)
Borislav Petkovf2e3ab52008-07-23 19:56:01 +02002017 clear_bit(IDE_AFLAG_MEDIUM_PRESENT, &drive->atapi_flags);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002018 return retval;
2019 case MTNOP:
Borislav Petkovec0fdb02008-04-27 15:38:34 +02002020 ide_tape_discard_merge_buffer(drive, 0);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002021 return idetape_flush_tape_buffers(drive);
2022 case MTRETEN:
Borislav Petkovec0fdb02008-04-27 15:38:34 +02002023 ide_tape_discard_merge_buffer(drive, 0);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002024 idetape_create_load_unload_cmd(drive, &pc,
2025 IDETAPE_LU_RETENSION_MASK | IDETAPE_LU_LOAD_MASK);
2026 return idetape_queue_pc_tail(drive, &pc);
2027 case MTEOM:
2028 idetape_create_space_cmd(&pc, 0, IDETAPE_SPACE_TO_EOD);
2029 return idetape_queue_pc_tail(drive, &pc);
2030 case MTERASE:
2031 (void)idetape_rewind_tape(drive);
2032 idetape_create_erase_cmd(&pc);
2033 return idetape_queue_pc_tail(drive, &pc);
2034 case MTSETBLK:
2035 if (mt_count) {
2036 if (mt_count < tape->blk_size ||
2037 mt_count % tape->blk_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002038 return -EIO;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002039 tape->user_bs_factor = mt_count / tape->blk_size;
Borislav Petkovf2e3ab52008-07-23 19:56:01 +02002040 clear_bit(IDE_AFLAG_DETECT_BS, &drive->atapi_flags);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002041 } else
Borislav Petkovf2e3ab52008-07-23 19:56:01 +02002042 set_bit(IDE_AFLAG_DETECT_BS, &drive->atapi_flags);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002043 return 0;
2044 case MTSEEK:
Borislav Petkovec0fdb02008-04-27 15:38:34 +02002045 ide_tape_discard_merge_buffer(drive, 0);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002046 return idetape_position_tape(drive,
2047 mt_count * tape->user_bs_factor, tape->partition, 0);
2048 case MTSETPART:
Borislav Petkovec0fdb02008-04-27 15:38:34 +02002049 ide_tape_discard_merge_buffer(drive, 0);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002050 return idetape_position_tape(drive, 0, mt_count, 0);
2051 case MTFSR:
2052 case MTBSR:
2053 case MTLOCK:
2054 if (!idetape_create_prevent_cmd(drive, &pc, 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002055 return 0;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002056 retval = idetape_queue_pc_tail(drive, &pc);
2057 if (retval)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002058 return retval;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002059 tape->door_locked = DOOR_EXPLICITLY_LOCKED;
2060 return 0;
2061 case MTUNLOCK:
2062 if (!idetape_create_prevent_cmd(drive, &pc, 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002063 return 0;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002064 retval = idetape_queue_pc_tail(drive, &pc);
2065 if (retval)
2066 return retval;
2067 tape->door_locked = DOOR_UNLOCKED;
2068 return 0;
2069 default:
2070 printk(KERN_ERR "ide-tape: MTIO operation %d not supported\n",
2071 mt_op);
2072 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002073 }
2074}
2075
2076/*
Borislav Petkovd99c9da2008-02-02 19:56:51 +01002077 * Our character device ioctls. General mtio.h magnetic io commands are
2078 * supported here, and not in the corresponding block interface. Our own
2079 * ide-tape ioctls are supported on both interfaces.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002080 */
Borislav Petkovd99c9da2008-02-02 19:56:51 +01002081static int idetape_chrdev_ioctl(struct inode *inode, struct file *file,
2082 unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002083{
2084 struct ide_tape_obj *tape = ide_tape_f(file);
2085 ide_drive_t *drive = tape->drive;
2086 struct mtop mtop;
2087 struct mtget mtget;
2088 struct mtpos mtpos;
Borislav Petkov54bb2072008-02-06 02:57:52 +01002089 int block_offset = 0, position = tape->first_frame;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002090 void __user *argp = (void __user *)arg;
2091
Borislav Petkov8004a8c2008-02-06 02:57:51 +01002092 debug_log(DBG_CHRDEV, "Enter %s, cmd=%u\n", __func__, cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002093
Borislav Petkov54abf372008-02-06 02:57:52 +01002094 if (tape->chrdev_dir == IDETAPE_DIR_WRITE) {
Borislav Petkovd9df9372008-04-27 15:38:34 +02002095 ide_tape_flush_merge_buffer(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002096 idetape_flush_tape_buffers(drive);
2097 }
2098 if (cmd == MTIOCGET || cmd == MTIOCPOS) {
Borislav Petkov01a63aeb2008-04-27 15:38:34 +02002099 block_offset = tape->merge_bh_size /
Borislav Petkov54bb2072008-02-06 02:57:52 +01002100 (tape->blk_size * tape->user_bs_factor);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002101 position = idetape_read_position(drive);
2102 if (position < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002103 return -EIO;
2104 }
2105 switch (cmd) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002106 case MTIOCTOP:
2107 if (copy_from_user(&mtop, argp, sizeof(struct mtop)))
2108 return -EFAULT;
2109 return idetape_mtioctop(drive, mtop.mt_op, mtop.mt_count);
2110 case MTIOCGET:
2111 memset(&mtget, 0, sizeof(struct mtget));
2112 mtget.mt_type = MT_ISSCSI2;
2113 mtget.mt_blkno = position / tape->user_bs_factor - block_offset;
2114 mtget.mt_dsreg =
2115 ((tape->blk_size * tape->user_bs_factor)
2116 << MT_ST_BLKSIZE_SHIFT) & MT_ST_BLKSIZE_MASK;
Borislav Petkov54bb2072008-02-06 02:57:52 +01002117
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002118 if (tape->drv_write_prot)
2119 mtget.mt_gstat |= GMT_WR_PROT(0xffffffff);
2120
2121 if (copy_to_user(argp, &mtget, sizeof(struct mtget)))
2122 return -EFAULT;
2123 return 0;
2124 case MTIOCPOS:
2125 mtpos.mt_blkno = position / tape->user_bs_factor - block_offset;
2126 if (copy_to_user(argp, &mtpos, sizeof(struct mtpos)))
2127 return -EFAULT;
2128 return 0;
2129 default:
2130 if (tape->chrdev_dir == IDETAPE_DIR_READ)
Borislav Petkovec0fdb02008-04-27 15:38:34 +02002131 ide_tape_discard_merge_buffer(drive, 1);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002132 return idetape_blkdev_ioctl(drive, cmd, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002133 }
2134}
2135
Borislav Petkov3cffb9c2008-02-02 19:56:50 +01002136/*
2137 * Do a mode sense page 0 with block descriptor and if it succeeds set the tape
2138 * block size with the reported value.
2139 */
2140static void ide_tape_get_bsize_from_bdesc(ide_drive_t *drive)
2141{
2142 idetape_tape_t *tape = drive->driver_data;
Borislav Petkovd236d742008-04-18 00:46:27 +02002143 struct ide_atapi_pc pc;
Borislav Petkov3cffb9c2008-02-02 19:56:50 +01002144
2145 idetape_create_mode_sense_cmd(&pc, IDETAPE_BLOCK_DESCRIPTOR);
2146 if (idetape_queue_pc_tail(drive, &pc)) {
2147 printk(KERN_ERR "ide-tape: Can't get block descriptor\n");
Borislav Petkov54bb2072008-02-06 02:57:52 +01002148 if (tape->blk_size == 0) {
Borislav Petkov3cffb9c2008-02-02 19:56:50 +01002149 printk(KERN_WARNING "ide-tape: Cannot deal with zero "
2150 "block size, assuming 32k\n");
Borislav Petkov54bb2072008-02-06 02:57:52 +01002151 tape->blk_size = 32768;
Borislav Petkov3cffb9c2008-02-02 19:56:50 +01002152 }
2153 return;
2154 }
Borislav Petkovd236d742008-04-18 00:46:27 +02002155 tape->blk_size = (pc.buf[4 + 5] << 16) +
2156 (pc.buf[4 + 6] << 8) +
2157 pc.buf[4 + 7];
2158 tape->drv_write_prot = (pc.buf[2] & 0x80) >> 7;
Borislav Petkov3cffb9c2008-02-02 19:56:50 +01002159}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002160
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002161static int idetape_chrdev_open(struct inode *inode, struct file *filp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002162{
2163 unsigned int minor = iminor(inode), i = minor & ~0xc0;
2164 ide_drive_t *drive;
2165 idetape_tape_t *tape;
Borislav Petkovd236d742008-04-18 00:46:27 +02002166 struct ide_atapi_pc pc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002167 int retval;
2168
Borislav Petkov8004a8c2008-02-06 02:57:51 +01002169 if (i >= MAX_HWIFS * MAX_DRIVES)
2170 return -ENXIO;
2171
Jonathan Corbet04f4ac92008-05-15 12:01:56 -06002172 lock_kernel();
Borislav Petkov8004a8c2008-02-06 02:57:51 +01002173 tape = ide_tape_chrdev_get(i);
Jonathan Corbet04f4ac92008-05-15 12:01:56 -06002174 if (!tape) {
2175 unlock_kernel();
Borislav Petkov8004a8c2008-02-06 02:57:51 +01002176 return -ENXIO;
Jonathan Corbet04f4ac92008-05-15 12:01:56 -06002177 }
Borislav Petkov8004a8c2008-02-06 02:57:51 +01002178
2179 debug_log(DBG_CHRDEV, "Enter %s\n", __func__);
2180
Linus Torvalds1da177e2005-04-16 15:20:36 -07002181 /*
2182 * We really want to do nonseekable_open(inode, filp); here, but some
2183 * versions of tar incorrectly call lseek on tapes and bail out if that
2184 * fails. So we disallow pread() and pwrite(), but permit lseeks.
2185 */
2186 filp->f_mode &= ~(FMODE_PREAD | FMODE_PWRITE);
2187
Linus Torvalds1da177e2005-04-16 15:20:36 -07002188 drive = tape->drive;
2189
2190 filp->private_data = tape;
2191
Borislav Petkovf2e3ab52008-07-23 19:56:01 +02002192 if (test_and_set_bit(IDE_AFLAG_BUSY, &drive->atapi_flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002193 retval = -EBUSY;
2194 goto out_put_tape;
2195 }
2196
2197 retval = idetape_wait_ready(drive, 60 * HZ);
2198 if (retval) {
Borislav Petkovf2e3ab52008-07-23 19:56:01 +02002199 clear_bit(IDE_AFLAG_BUSY, &drive->atapi_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002200 printk(KERN_ERR "ide-tape: %s: drive not ready\n", tape->name);
2201 goto out_put_tape;
2202 }
2203
2204 idetape_read_position(drive);
Borislav Petkovf2e3ab52008-07-23 19:56:01 +02002205 if (!test_bit(IDE_AFLAG_ADDRESS_VALID, &drive->atapi_flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002206 (void)idetape_rewind_tape(drive);
2207
Linus Torvalds1da177e2005-04-16 15:20:36 -07002208 /* Read block size and write protect status from drive. */
Borislav Petkov3cffb9c2008-02-02 19:56:50 +01002209 ide_tape_get_bsize_from_bdesc(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002210
2211 /* Set write protect flag if device is opened as read-only. */
2212 if ((filp->f_flags & O_ACCMODE) == O_RDONLY)
2213 tape->write_prot = 1;
2214 else
2215 tape->write_prot = tape->drv_write_prot;
2216
2217 /* Make sure drive isn't write protected if user wants to write. */
2218 if (tape->write_prot) {
2219 if ((filp->f_flags & O_ACCMODE) == O_WRONLY ||
2220 (filp->f_flags & O_ACCMODE) == O_RDWR) {
Borislav Petkovf2e3ab52008-07-23 19:56:01 +02002221 clear_bit(IDE_AFLAG_BUSY, &drive->atapi_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002222 retval = -EROFS;
2223 goto out_put_tape;
2224 }
2225 }
2226
Borislav Petkov3c98bf32008-02-06 02:57:53 +01002227 /* Lock the tape drive door so user can't eject. */
Borislav Petkov54abf372008-02-06 02:57:52 +01002228 if (tape->chrdev_dir == IDETAPE_DIR_NONE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002229 if (idetape_create_prevent_cmd(drive, &pc, 1)) {
2230 if (!idetape_queue_pc_tail(drive, &pc)) {
2231 if (tape->door_locked != DOOR_EXPLICITLY_LOCKED)
2232 tape->door_locked = DOOR_LOCKED;
2233 }
2234 }
2235 }
Jonathan Corbet04f4ac92008-05-15 12:01:56 -06002236 unlock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002237 return 0;
2238
2239out_put_tape:
2240 ide_tape_put(tape);
Jonathan Corbet04f4ac92008-05-15 12:01:56 -06002241 unlock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002242 return retval;
2243}
2244
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002245static void idetape_write_release(ide_drive_t *drive, unsigned int minor)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002246{
2247 idetape_tape_t *tape = drive->driver_data;
2248
Borislav Petkovd9df9372008-04-27 15:38:34 +02002249 ide_tape_flush_merge_buffer(drive);
Borislav Petkov077e3bd2008-04-27 15:38:34 +02002250 tape->merge_bh = ide_tape_kmalloc_buffer(tape, 1, 0);
2251 if (tape->merge_bh != NULL) {
Borislav Petkov54bb2072008-02-06 02:57:52 +01002252 idetape_pad_zeros(drive, tape->blk_size *
2253 (tape->user_bs_factor - 1));
Borislav Petkov077e3bd2008-04-27 15:38:34 +02002254 ide_tape_kfree_buffer(tape);
2255 tape->merge_bh = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002256 }
2257 idetape_write_filemark(drive);
2258 idetape_flush_tape_buffers(drive);
2259 idetape_flush_tape_buffers(drive);
2260}
2261
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002262static int idetape_chrdev_release(struct inode *inode, struct file *filp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002263{
2264 struct ide_tape_obj *tape = ide_tape_f(filp);
2265 ide_drive_t *drive = tape->drive;
Borislav Petkovd236d742008-04-18 00:46:27 +02002266 struct ide_atapi_pc pc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002267 unsigned int minor = iminor(inode);
2268
2269 lock_kernel();
2270 tape = drive->driver_data;
Borislav Petkov8004a8c2008-02-06 02:57:51 +01002271
2272 debug_log(DBG_CHRDEV, "Enter %s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002273
Borislav Petkov54abf372008-02-06 02:57:52 +01002274 if (tape->chrdev_dir == IDETAPE_DIR_WRITE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002275 idetape_write_release(drive, minor);
Borislav Petkov54abf372008-02-06 02:57:52 +01002276 if (tape->chrdev_dir == IDETAPE_DIR_READ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002277 if (minor < 128)
Borislav Petkovec0fdb02008-04-27 15:38:34 +02002278 ide_tape_discard_merge_buffer(drive, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002279 }
Borislav Petkovf64eee72008-04-27 15:38:25 +02002280
Borislav Petkovf2e3ab52008-07-23 19:56:01 +02002281 if (minor < 128 && test_bit(IDE_AFLAG_MEDIUM_PRESENT, &drive->atapi_flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002282 (void) idetape_rewind_tape(drive);
Borislav Petkov54abf372008-02-06 02:57:52 +01002283 if (tape->chrdev_dir == IDETAPE_DIR_NONE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002284 if (tape->door_locked == DOOR_LOCKED) {
2285 if (idetape_create_prevent_cmd(drive, &pc, 0)) {
2286 if (!idetape_queue_pc_tail(drive, &pc))
2287 tape->door_locked = DOOR_UNLOCKED;
2288 }
2289 }
2290 }
Borislav Petkovf2e3ab52008-07-23 19:56:01 +02002291 clear_bit(IDE_AFLAG_BUSY, &drive->atapi_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002292 ide_tape_put(tape);
2293 unlock_kernel();
2294 return 0;
2295}
2296
2297/*
Borislav Petkov71071b82008-02-06 02:57:53 +01002298 * check the contents of the ATAPI IDENTIFY command results. We return:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002299 *
Borislav Petkov71071b82008-02-06 02:57:53 +01002300 * 1 - If the tape can be supported by us, based on the information we have so
2301 * far.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002302 *
Borislav Petkov71071b82008-02-06 02:57:53 +01002303 * 0 - If this tape driver is not currently supported by us.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002304 */
Borislav Petkov71071b82008-02-06 02:57:53 +01002305static int idetape_identify_device(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002306{
Borislav Petkov71071b82008-02-06 02:57:53 +01002307 u8 gcw[2], protocol, device_type, removable, packet_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002308
2309 if (drive->id_read == 0)
2310 return 1;
2311
Borislav Petkov71071b82008-02-06 02:57:53 +01002312 *((unsigned short *) &gcw) = drive->id->config;
2313
2314 protocol = (gcw[1] & 0xC0) >> 6;
2315 device_type = gcw[1] & 0x1F;
2316 removable = !!(gcw[0] & 0x80);
2317 packet_size = gcw[0] & 0x3;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002318
Linus Torvalds1da177e2005-04-16 15:20:36 -07002319 /* Check that we can support this device */
Borislav Petkov71071b82008-02-06 02:57:53 +01002320 if (protocol != 2)
Bartlomiej Zolnierkiewicz16422de32008-02-02 19:56:48 +01002321 printk(KERN_ERR "ide-tape: Protocol (0x%02x) is not ATAPI\n",
Borislav Petkov71071b82008-02-06 02:57:53 +01002322 protocol);
2323 else if (device_type != 1)
Bartlomiej Zolnierkiewicz16422de32008-02-02 19:56:48 +01002324 printk(KERN_ERR "ide-tape: Device type (0x%02x) is not set "
Borislav Petkov71071b82008-02-06 02:57:53 +01002325 "to tape\n", device_type);
2326 else if (!removable)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002327 printk(KERN_ERR "ide-tape: The removable flag is not set\n");
Borislav Petkov71071b82008-02-06 02:57:53 +01002328 else if (packet_size != 0) {
Borislav Petkov24d57f82008-02-06 02:57:54 +01002329 printk(KERN_ERR "ide-tape: Packet size (0x%02x) is not 12"
2330 " bytes\n", packet_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002331 } else
2332 return 1;
2333 return 0;
2334}
2335
Borislav Petkov6d29c8f2008-02-02 19:56:49 +01002336static void idetape_get_inquiry_results(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002337{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002338 idetape_tape_t *tape = drive->driver_data;
Borislav Petkovd236d742008-04-18 00:46:27 +02002339 struct ide_atapi_pc pc;
Borislav Petkov41f81d542008-02-06 02:57:52 +01002340 char fw_rev[6], vendor_id[10], product_id[18];
Borislav Petkov6d29c8f2008-02-02 19:56:49 +01002341
Linus Torvalds1da177e2005-04-16 15:20:36 -07002342 idetape_create_inquiry_cmd(&pc);
2343 if (idetape_queue_pc_tail(drive, &pc)) {
Borislav Petkov6d29c8f2008-02-02 19:56:49 +01002344 printk(KERN_ERR "ide-tape: %s: can't get INQUIRY results\n",
2345 tape->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002346 return;
2347 }
Borislav Petkovd236d742008-04-18 00:46:27 +02002348 memcpy(vendor_id, &pc.buf[8], 8);
2349 memcpy(product_id, &pc.buf[16], 16);
2350 memcpy(fw_rev, &pc.buf[32], 4);
Borislav Petkov6d29c8f2008-02-02 19:56:49 +01002351
Borislav Petkov41f81d542008-02-06 02:57:52 +01002352 ide_fixstring(vendor_id, 10, 0);
2353 ide_fixstring(product_id, 18, 0);
2354 ide_fixstring(fw_rev, 6, 0);
2355
Borislav Petkov6d29c8f2008-02-02 19:56:49 +01002356 printk(KERN_INFO "ide-tape: %s <-> %s: %s %s rev %s\n",
Borislav Petkov41f81d542008-02-06 02:57:52 +01002357 drive->name, tape->name, vendor_id, product_id, fw_rev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002358}
2359
2360/*
Borislav Petkovb6422012008-02-02 19:56:49 +01002361 * Ask the tape about its various parameters. In particular, we will adjust our
2362 * data transfer buffer size to the recommended value as returned by the tape.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002363 */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002364static void idetape_get_mode_sense_results(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002365{
2366 idetape_tape_t *tape = drive->driver_data;
Borislav Petkovd236d742008-04-18 00:46:27 +02002367 struct ide_atapi_pc pc;
Borislav Petkovb6422012008-02-02 19:56:49 +01002368 u8 *caps;
2369 u8 speed, max_speed;
Borislav Petkov47314fa2008-02-02 19:56:48 +01002370
Linus Torvalds1da177e2005-04-16 15:20:36 -07002371 idetape_create_mode_sense_cmd(&pc, IDETAPE_CAPABILITIES_PAGE);
2372 if (idetape_queue_pc_tail(drive, &pc)) {
Borislav Petkovb6422012008-02-02 19:56:49 +01002373 printk(KERN_ERR "ide-tape: Can't get tape parameters - assuming"
2374 " some default values\n");
Borislav Petkov54bb2072008-02-06 02:57:52 +01002375 tape->blk_size = 512;
Borislav Petkovb6422012008-02-02 19:56:49 +01002376 put_unaligned(52, (u16 *)&tape->caps[12]);
2377 put_unaligned(540, (u16 *)&tape->caps[14]);
2378 put_unaligned(6*52, (u16 *)&tape->caps[16]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002379 return;
2380 }
Borislav Petkovd236d742008-04-18 00:46:27 +02002381 caps = pc.buf + 4 + pc.buf[3];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002382
Borislav Petkovb6422012008-02-02 19:56:49 +01002383 /* convert to host order and save for later use */
2384 speed = be16_to_cpu(*(u16 *)&caps[14]);
2385 max_speed = be16_to_cpu(*(u16 *)&caps[8]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002386
Borislav Petkovb6422012008-02-02 19:56:49 +01002387 put_unaligned(max_speed, (u16 *)&caps[8]);
2388 put_unaligned(be16_to_cpu(*(u16 *)&caps[12]), (u16 *)&caps[12]);
2389 put_unaligned(speed, (u16 *)&caps[14]);
2390 put_unaligned(be16_to_cpu(*(u16 *)&caps[16]), (u16 *)&caps[16]);
2391
2392 if (!speed) {
2393 printk(KERN_INFO "ide-tape: %s: invalid tape speed "
2394 "(assuming 650KB/sec)\n", drive->name);
2395 put_unaligned(650, (u16 *)&caps[14]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002396 }
Borislav Petkovb6422012008-02-02 19:56:49 +01002397 if (!max_speed) {
2398 printk(KERN_INFO "ide-tape: %s: invalid max_speed "
2399 "(assuming 650KB/sec)\n", drive->name);
2400 put_unaligned(650, (u16 *)&caps[8]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002401 }
2402
Borislav Petkovb6422012008-02-02 19:56:49 +01002403 memcpy(&tape->caps, caps, 20);
2404 if (caps[7] & 0x02)
Borislav Petkov54bb2072008-02-06 02:57:52 +01002405 tape->blk_size = 512;
Borislav Petkovb6422012008-02-02 19:56:49 +01002406 else if (caps[7] & 0x04)
Borislav Petkov54bb2072008-02-06 02:57:52 +01002407 tape->blk_size = 1024;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002408}
2409
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +02002410#ifdef CONFIG_IDE_PROC_FS
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002411static void idetape_add_settings(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002412{
2413 idetape_tape_t *tape = drive->driver_data;
2414
Borislav Petkovb6422012008-02-02 19:56:49 +01002415 ide_add_setting(drive, "buffer", SETTING_READ, TYPE_SHORT, 0, 0xffff,
2416 1, 2, (u16 *)&tape->caps[16], NULL);
Borislav Petkovb6422012008-02-02 19:56:49 +01002417 ide_add_setting(drive, "speed", SETTING_READ, TYPE_SHORT, 0, 0xffff,
2418 1, 1, (u16 *)&tape->caps[14], NULL);
Borislav Petkovf73850a2008-04-27 15:38:33 +02002419 ide_add_setting(drive, "buffer_size", SETTING_READ, TYPE_INT, 0, 0xffff,
2420 1, 1024, &tape->buffer_size, NULL);
Borislav Petkov54bb2072008-02-06 02:57:52 +01002421 ide_add_setting(drive, "tdsc", SETTING_RW, TYPE_INT, IDETAPE_DSC_RW_MIN,
2422 IDETAPE_DSC_RW_MAX, 1000, HZ, &tape->best_dsc_rw_freq,
2423 NULL);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002424 ide_add_setting(drive, "dsc_overlap", SETTING_RW, TYPE_BYTE, 0, 1, 1,
2425 1, &drive->dsc_overlap, NULL);
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002426 ide_add_setting(drive, "avg_speed", SETTING_READ, TYPE_INT, 0, 0xffff,
2427 1, 1, &tape->avg_speed, NULL);
Borislav Petkov8004a8c2008-02-06 02:57:51 +01002428 ide_add_setting(drive, "debug_mask", SETTING_RW, TYPE_INT, 0, 0xffff, 1,
2429 1, &tape->debug_mask, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002430}
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +02002431#else
2432static inline void idetape_add_settings(ide_drive_t *drive) { ; }
2433#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002434
2435/*
Borislav Petkov3c98bf32008-02-06 02:57:53 +01002436 * The function below is called to:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002437 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +01002438 * 1. Initialize our various state variables.
2439 * 2. Ask the tape for its capabilities.
2440 * 3. Allocate a buffer which will be used for data transfer. The buffer size
2441 * is chosen based on the recommendation which we received in step 2.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002442 *
Borislav Petkov3c98bf32008-02-06 02:57:53 +01002443 * Note that at this point ide.c already assigned us an irq, so that we can
2444 * queue requests here and wait for their completion.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002445 */
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002446static void idetape_setup(ide_drive_t *drive, idetape_tape_t *tape, int minor)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002447{
Borislav Petkov83042b22008-04-27 15:38:27 +02002448 unsigned long t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002449 int speed;
Borislav Petkovf73850a2008-04-27 15:38:33 +02002450 int buffer_size;
Borislav Petkov71071b82008-02-06 02:57:53 +01002451 u8 gcw[2];
Borislav Petkovb6422012008-02-02 19:56:49 +01002452 u16 *ctl = (u16 *)&tape->caps[12];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002453
Borislav Petkov776bb022008-07-23 19:55:59 +02002454 drive->pc_callback = ide_tape_callback;
2455
Borislav Petkov54bb2072008-02-06 02:57:52 +01002456 spin_lock_init(&tape->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002457 drive->dsc_overlap = 1;
Bartlomiej Zolnierkiewicz4166c192008-02-01 23:09:30 +01002458 if (drive->hwif->host_flags & IDE_HFLAG_NO_DSC) {
2459 printk(KERN_INFO "ide-tape: %s: disabling DSC overlap\n",
2460 tape->name);
2461 drive->dsc_overlap = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002462 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002463 /* Seagate Travan drives do not support DSC overlap. */
2464 if (strstr(drive->id->model, "Seagate STT3401"))
2465 drive->dsc_overlap = 0;
2466 tape->minor = minor;
2467 tape->name[0] = 'h';
2468 tape->name[1] = 't';
2469 tape->name[2] = '0' + minor;
Borislav Petkov54abf372008-02-06 02:57:52 +01002470 tape->chrdev_dir = IDETAPE_DIR_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002471 tape->pc = tape->pc_stack;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002472 *((unsigned short *) &gcw) = drive->id->config;
Borislav Petkov71071b82008-02-06 02:57:53 +01002473
2474 /* Command packet DRQ type */
2475 if (((gcw[0] & 0x60) >> 5) == 1)
Borislav Petkovf2e3ab52008-07-23 19:56:01 +02002476 set_bit(IDE_AFLAG_DRQ_INTERRUPT, &drive->atapi_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002477
Linus Torvalds1da177e2005-04-16 15:20:36 -07002478 idetape_get_inquiry_results(drive);
2479 idetape_get_mode_sense_results(drive);
Borislav Petkov3cffb9c2008-02-02 19:56:50 +01002480 ide_tape_get_bsize_from_bdesc(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002481 tape->user_bs_factor = 1;
Borislav Petkovf73850a2008-04-27 15:38:33 +02002482 tape->buffer_size = *ctl * tape->blk_size;
2483 while (tape->buffer_size > 0xffff) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002484 printk(KERN_NOTICE "ide-tape: decreasing stage size\n");
Borislav Petkovb6422012008-02-02 19:56:49 +01002485 *ctl /= 2;
Borislav Petkovf73850a2008-04-27 15:38:33 +02002486 tape->buffer_size = *ctl * tape->blk_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002487 }
Borislav Petkovf73850a2008-04-27 15:38:33 +02002488 buffer_size = tape->buffer_size;
Borislav Petkova997a432008-04-27 15:38:33 +02002489 tape->pages_per_buffer = buffer_size / PAGE_SIZE;
Borislav Petkovf73850a2008-04-27 15:38:33 +02002490 if (buffer_size % PAGE_SIZE) {
Borislav Petkova997a432008-04-27 15:38:33 +02002491 tape->pages_per_buffer++;
Borislav Petkovf73850a2008-04-27 15:38:33 +02002492 tape->excess_bh_size = PAGE_SIZE - buffer_size % PAGE_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002493 }
2494
Borislav Petkov83042b22008-04-27 15:38:27 +02002495 /* select the "best" DSC read/write polling freq */
Borislav Petkovb6422012008-02-02 19:56:49 +01002496 speed = max(*(u16 *)&tape->caps[14], *(u16 *)&tape->caps[8]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002497
Borislav Petkovf73850a2008-04-27 15:38:33 +02002498 t = (IDETAPE_FIFO_THRESHOLD * tape->buffer_size * HZ) / (speed * 1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002499
2500 /*
Borislav Petkov3c98bf32008-02-06 02:57:53 +01002501 * Ensure that the number we got makes sense; limit it within
2502 * IDETAPE_DSC_RW_MIN and IDETAPE_DSC_RW_MAX.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002503 */
Harvey Harrisona792bd52008-07-15 21:21:41 +02002504 tape->best_dsc_rw_freq = clamp_t(unsigned long, t, IDETAPE_DSC_RW_MIN,
2505 IDETAPE_DSC_RW_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002506 printk(KERN_INFO "ide-tape: %s <-> %s: %dKBps, %d*%dkB buffer, "
Borislav Petkov83042b22008-04-27 15:38:27 +02002507 "%lums tDSC%s\n",
Borislav Petkovb6422012008-02-02 19:56:49 +01002508 drive->name, tape->name, *(u16 *)&tape->caps[14],
Borislav Petkovf73850a2008-04-27 15:38:33 +02002509 (*(u16 *)&tape->caps[16] * 512) / tape->buffer_size,
2510 tape->buffer_size / 1024,
Borislav Petkov54bb2072008-02-06 02:57:52 +01002511 tape->best_dsc_rw_freq * 1000 / HZ,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002512 drive->using_dma ? ", DMA":"");
2513
2514 idetape_add_settings(drive);
2515}
2516
Russell King4031bbe2006-01-06 11:41:00 +00002517static void ide_tape_remove(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002518{
2519 idetape_tape_t *tape = drive->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002520
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +02002521 ide_proc_unregister_driver(drive, tape->driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002522
2523 ide_unregister_region(tape->disk);
2524
2525 ide_tape_put(tape);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002526}
2527
2528static void ide_tape_release(struct kref *kref)
2529{
2530 struct ide_tape_obj *tape = to_ide_tape(kref);
2531 ide_drive_t *drive = tape->drive;
2532 struct gendisk *g = tape->disk;
2533
Borislav Petkov01a63aeb2008-04-27 15:38:34 +02002534 BUG_ON(tape->merge_bh_size);
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02002535
Linus Torvalds1da177e2005-04-16 15:20:36 -07002536 drive->dsc_overlap = 0;
2537 drive->driver_data = NULL;
Tony Jonesdbc12722007-09-25 02:03:03 +02002538 device_destroy(idetape_sysfs_class, MKDEV(IDETAPE_MAJOR, tape->minor));
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002539 device_destroy(idetape_sysfs_class,
2540 MKDEV(IDETAPE_MAJOR, tape->minor + 128));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002541 idetape_devs[tape->minor] = NULL;
2542 g->private_data = NULL;
2543 put_disk(g);
2544 kfree(tape);
2545}
2546
Bartlomiej Zolnierkiewiczecfd80e2007-05-10 00:01:09 +02002547#ifdef CONFIG_IDE_PROC_FS
Linus Torvalds1da177e2005-04-16 15:20:36 -07002548static int proc_idetape_read_name
2549 (char *page, char **start, off_t off, int count, int *eof, void *data)
2550{
2551 ide_drive_t *drive = (ide_drive_t *) data;
2552 idetape_tape_t *tape = drive->driver_data;
2553 char *out = page;
2554 int len;
2555
2556 len = sprintf(out, "%s\n", tape->name);
2557 PROC_IDE_READ_RETURN(page, start, off, count, eof, len);
2558}
2559
2560static ide_proc_entry_t idetape_proc[] = {
2561 { "capacity", S_IFREG|S_IRUGO, proc_ide_read_capacity, NULL },
2562 { "name", S_IFREG|S_IRUGO, proc_idetape_read_name, NULL },
2563 { NULL, 0, NULL, NULL }
2564};
Linus Torvalds1da177e2005-04-16 15:20:36 -07002565#endif
2566
Russell King4031bbe2006-01-06 11:41:00 +00002567static int ide_tape_probe(ide_drive_t *);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002568
Linus Torvalds1da177e2005-04-16 15:20:36 -07002569static ide_driver_t idetape_driver = {
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02002570 .gen_driver = {
Laurent Riffard4ef3b8f2005-11-18 22:15:40 +01002571 .owner = THIS_MODULE,
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02002572 .name = "ide-tape",
2573 .bus = &ide_bus_type,
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02002574 },
Russell King4031bbe2006-01-06 11:41:00 +00002575 .probe = ide_tape_probe,
2576 .remove = ide_tape_remove,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002577 .version = IDETAPE_VERSION,
2578 .media = ide_tape,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002579 .supports_dsc_overlap = 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002580 .do_request = idetape_do_request,
2581 .end_request = idetape_end_request,
2582 .error = __ide_error,
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +02002583#ifdef CONFIG_IDE_PROC_FS
Linus Torvalds1da177e2005-04-16 15:20:36 -07002584 .proc = idetape_proc,
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +02002585#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002586};
2587
Borislav Petkov3c98bf32008-02-06 02:57:53 +01002588/* Our character device supporting functions, passed to register_chrdev. */
Arjan van de Ven2b8693c2007-02-12 00:55:32 -08002589static const struct file_operations idetape_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002590 .owner = THIS_MODULE,
2591 .read = idetape_chrdev_read,
2592 .write = idetape_chrdev_write,
2593 .ioctl = idetape_chrdev_ioctl,
2594 .open = idetape_chrdev_open,
2595 .release = idetape_chrdev_release,
2596};
2597
2598static int idetape_open(struct inode *inode, struct file *filp)
2599{
2600 struct gendisk *disk = inode->i_bdev->bd_disk;
2601 struct ide_tape_obj *tape;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002602
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002603 tape = ide_tape_get(disk);
2604 if (!tape)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002605 return -ENXIO;
2606
Linus Torvalds1da177e2005-04-16 15:20:36 -07002607 return 0;
2608}
2609
2610static int idetape_release(struct inode *inode, struct file *filp)
2611{
2612 struct gendisk *disk = inode->i_bdev->bd_disk;
2613 struct ide_tape_obj *tape = ide_tape_g(disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002614
2615 ide_tape_put(tape);
2616
2617 return 0;
2618}
2619
2620static int idetape_ioctl(struct inode *inode, struct file *file,
2621 unsigned int cmd, unsigned long arg)
2622{
2623 struct block_device *bdev = inode->i_bdev;
2624 struct ide_tape_obj *tape = ide_tape_g(bdev->bd_disk);
2625 ide_drive_t *drive = tape->drive;
2626 int err = generic_ide_ioctl(drive, file, bdev, cmd, arg);
2627 if (err == -EINVAL)
2628 err = idetape_blkdev_ioctl(drive, cmd, arg);
2629 return err;
2630}
2631
2632static struct block_device_operations idetape_block_ops = {
2633 .owner = THIS_MODULE,
2634 .open = idetape_open,
2635 .release = idetape_release,
2636 .ioctl = idetape_ioctl,
2637};
2638
Russell King4031bbe2006-01-06 11:41:00 +00002639static int ide_tape_probe(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002640{
2641 idetape_tape_t *tape;
2642 struct gendisk *g;
2643 int minor;
2644
2645 if (!strstr("ide-tape", drive->driver_req))
2646 goto failed;
2647 if (!drive->present)
2648 goto failed;
2649 if (drive->media != ide_tape)
2650 goto failed;
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002651 if (!idetape_identify_device(drive)) {
2652 printk(KERN_ERR "ide-tape: %s: not supported by this version of"
2653 " the driver\n", drive->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002654 goto failed;
2655 }
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002656 tape = kzalloc(sizeof(idetape_tape_t), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002657 if (tape == NULL) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002658 printk(KERN_ERR "ide-tape: %s: Can't allocate a tape struct\n",
2659 drive->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002660 goto failed;
2661 }
2662
2663 g = alloc_disk(1 << PARTN_BITS);
2664 if (!g)
2665 goto out_free_tape;
2666
2667 ide_init_disk(g, drive);
2668
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +02002669 ide_proc_register_driver(drive, &idetape_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002670
Linus Torvalds1da177e2005-04-16 15:20:36 -07002671 kref_init(&tape->kref);
2672
2673 tape->drive = drive;
2674 tape->driver = &idetape_driver;
2675 tape->disk = g;
2676
2677 g->private_data = &tape->driver;
2678
2679 drive->driver_data = tape;
2680
Arjan van de Vencf8b8972006-03-23 03:00:45 -08002681 mutex_lock(&idetape_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002682 for (minor = 0; idetape_devs[minor]; minor++)
2683 ;
2684 idetape_devs[minor] = tape;
Arjan van de Vencf8b8972006-03-23 03:00:45 -08002685 mutex_unlock(&idetape_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002686
2687 idetape_setup(drive, tape, minor);
2688
Greg Kroah-Hartman6ecaaf92008-05-21 12:52:33 -07002689 device_create_drvdata(idetape_sysfs_class, &drive->gendev,
2690 MKDEV(IDETAPE_MAJOR, minor), NULL,
2691 "%s", tape->name);
2692 device_create_drvdata(idetape_sysfs_class, &drive->gendev,
2693 MKDEV(IDETAPE_MAJOR, minor + 128), NULL,
2694 "n%s", tape->name);
Will Dysond5dee802005-09-16 02:55:07 -07002695
Linus Torvalds1da177e2005-04-16 15:20:36 -07002696 g->fops = &idetape_block_ops;
2697 ide_register_region(g);
2698
2699 return 0;
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02002700
Linus Torvalds1da177e2005-04-16 15:20:36 -07002701out_free_tape:
2702 kfree(tape);
2703failed:
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02002704 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002705}
2706
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002707static void __exit idetape_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002708{
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02002709 driver_unregister(&idetape_driver.gen_driver);
Will Dysond5dee802005-09-16 02:55:07 -07002710 class_destroy(idetape_sysfs_class);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002711 unregister_chrdev(IDETAPE_MAJOR, "ht");
2712}
2713
Bartlomiej Zolnierkiewicz17514e82005-11-19 22:24:35 +01002714static int __init idetape_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002715{
Will Dysond5dee802005-09-16 02:55:07 -07002716 int error = 1;
2717 idetape_sysfs_class = class_create(THIS_MODULE, "ide_tape");
2718 if (IS_ERR(idetape_sysfs_class)) {
2719 idetape_sysfs_class = NULL;
2720 printk(KERN_ERR "Unable to create sysfs class for ide tapes\n");
2721 error = -EBUSY;
2722 goto out;
2723 }
2724
Linus Torvalds1da177e2005-04-16 15:20:36 -07002725 if (register_chrdev(IDETAPE_MAJOR, "ht", &idetape_fops)) {
Borislav Petkov5a04cfa2008-02-06 02:57:54 +01002726 printk(KERN_ERR "ide-tape: Failed to register chrdev"
2727 " interface\n");
Will Dysond5dee802005-09-16 02:55:07 -07002728 error = -EBUSY;
2729 goto out_free_class;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002730 }
Will Dysond5dee802005-09-16 02:55:07 -07002731
2732 error = driver_register(&idetape_driver.gen_driver);
2733 if (error)
2734 goto out_free_driver;
2735
2736 return 0;
2737
2738out_free_driver:
2739 driver_unregister(&idetape_driver.gen_driver);
2740out_free_class:
2741 class_destroy(idetape_sysfs_class);
2742out:
2743 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002744}
2745
Kay Sievers263756e2005-12-12 18:03:44 +01002746MODULE_ALIAS("ide:*m-tape*");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002747module_init(idetape_init);
2748module_exit(idetape_exit);
2749MODULE_ALIAS_CHARDEV_MAJOR(IDETAPE_MAJOR);
Borislav Petkov9c145762008-02-06 02:57:54 +01002750MODULE_DESCRIPTION("ATAPI Streaming TAPE Driver");
2751MODULE_LICENSE("GPL");